1 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE Rank2Types #-}
4 {-# LANGUAGE TypeOperators #-}
5 {-# LANGUAGE TypeFamilies #-}
6 {-# OPTIONS_GHC -fno-warn-missing-methods #-}
7 -- | Natural numbers at the type-level, and of kind @*@.
8 module Language.Symantic.Lib.Data.Peano where
10 import Data.Type.Equality ((:~:)(Refl))
12 -- * Types 'Zero' and 'Succ'
13 -- | Type-level peano numbers of kind '*'.
17 -- ** Type synonyms for a few numbers
24 -- | Singleton for 'Zero' and 'Succ'.
27 SSucc :: SPeano p -> SPeano (Succ p)
30 -- | Implicit construction of 'SPeano'.
33 instance IPeano Zero where
35 instance IPeano p => IPeano (Succ p) where
40 EPeano :: SPeano p -> EPeano
41 instance Eq EPeano where
42 EPeano x == EPeano y =
43 (integral_from_peano x::Integer) ==
46 integral_from_peano :: Integral i => SPeano p -> i
47 integral_from_peano SZero = 0
48 integral_from_peano (SSucc x) = 1 + integral_from_peano x
50 peano_from_integral :: Integral i => i -> (forall p. SPeano p -> ret) -> ret
51 peano_from_integral 0 k = k SZero
52 peano_from_integral i k | i > 0 =
53 peano_from_integral (i - 1) $ \p -> k (SSucc p)
54 peano_from_integral _ _ = error "peano_from_integral"
56 peano_eq :: forall x y. SPeano x -> SPeano y -> Maybe (x :~: y)
57 peano_eq SZero SZero = Just Refl
58 peano_eq (SSucc x) (SSucc y)
59 | Just Refl <- x `peano_eq` y
61 peano_eq _ _ = Nothing
65 type family n <= m :: Bool where
68 Succ n <= Succ m = n <= m
72 data VList :: * -> * -> * where
74 (:::) :: a -> VList p a -> VList (Succ p) a