on master help sign in
Add peano numbers
commit8c32be349f6f7807c47247343756ee558bc17fbf authorAleksey Uimanov <s9gf4ult@gmail.com> dateSat 24 Oct 2015, 22:06 (11 years ago) parent5aefb8c
2 files changed+32
src/TypeFun/Data/Peano.hs+30 type-fun.cabal+2
src/TypeFun/Data/Peano.hsnew file+30
@@ -0,0 +1,30 @@
1+module TypeFun.Data.Peano where
2+
3+import Data.Typeable
4+import GHC.Generics (Generic)
5+import GHC.TypeLits
6+
7+data N = Z | S N
8+ deriving ( Eq, Ord, Read, Show
9+ , Generic, Typeable )
10+
11+type family ToNat (a :: N) :: Nat where
12+ ToNat Z = 0
13+ ToNat (S a) = 1 + (ToNat a)
14+
15+type family FromNat (a :: Nat) :: N where
16+ FromNat 0 = Z
17+ FromNat a = S (FromNat (a - 1))
18+
19+type family (:+:) (a :: N) (b :: N) :: N where
20+ Z :+: b = b
21+ (S a) :+: b = a :+: (S b)
22+
23+type family (:-:) (a :: N) (b :: N) :: N where
24+ a :-: Z = a
25+ (S a) :-: (S b) = a :-: b
26+
27+type family (:*:) (a :: N) (b :: N) :: N where
28+ Z :*: b = Z
29+ a :*: Z = Z
30+ (S a) :*: b = b :+: (a :*: b)
type-fun.cabal+2
@@ -20,6 +20,7 @@ library
2020 hs-source-dirs: src
2121 default-extensions: ConstraintKinds
2222 , DataKinds
23+ , DeriveGeneric
2324 , FlexibleInstances
2425 , MultiParamTypeClasses
2526 , PolyKinds
@@ -30,6 +31,7 @@ library
3031 exposed-modules: TypeFun.Data.Eq
3132 , TypeFun.Data.List
3233 , TypeFun.Data.Maybe
34+ , TypeFun.Data.Peano
3335
3436 build-depends: base >=4.7 && <4.9
3537