@@ -1,5 +1,6 @@
11module TypeFun.Data.Peano
22 ( N(..)
3+ , KnownPeano(..)
34 , ToNat
45 , FromNat
56 , (:+:)
@@ -7,14 +8,24 @@ module TypeFun.Data.Peano
78 , (:*:)
89 ) where
910
10−import Data.Typeable
11−import GHC.Generics (Generic)
12−import GHC.TypeLits
11+import Data.Typeable
12+import GHC.Generics (Generic)
13+import GHC.TypeLits
1314
1415data N = Z | S N
1516 deriving ( Eq, Ord, Read, Show
1617 , Generic, Typeable )
1718
19+-- | @since 0.1.2
20+class KnownPeano (p :: N) where
21+ peanoVal :: proxy p -> Integer
22+
23+instance KnownPeano Z where
24+ peanoVal _ = 0
25+
26+instance (KnownPeano n) => KnownPeano (S n) where
27+ peanoVal _ = succ $ peanoVal (Proxy :: Proxy n)
28+
1829type family ToNat (a :: N) :: Nat where
1930 ToNat Z = 0
2031 ToNat (S a) = 1 + (ToNat a)