Skip to content

Commit 5cde0a5

Browse files
committed
Add curhnum
1 parent 1cc5805 commit 5cde0a5

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/Base.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
12
module Base (
23
identity,
34
constant,

src/ChurchNum.hs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module ChurchNum (
2+
zero, -- point free
3+
one, -- point free
4+
two, -- point free
5+
inc, -- point free
6+
dec ,
7+
add,
8+
sub,
9+
mul, -- point free
10+
church,
11+
unchurch,-- point free
12+
isZero
13+
) where
14+
15+
import Base
16+
17+
-- zero :: p2 -> t3 -> t3
18+
zero = Base.flip constant
19+
20+
-- one :: (t1 -> t2) -> t1 -> t2
21+
one = apply
22+
23+
-- two :: (t -> t) -> t -> t
24+
two x y = x $ x y
25+
26+
-- inc :: Num a => a -> a
27+
inc = (+1)
28+
29+
-- dec :: Num a => a -> a
30+
dec x = x - 1
31+
32+
-- add :: Num a => a -> a -> a
33+
add = (+)
34+
35+
-- sub :: Num a => a -> a -> a
36+
sub = (-)
37+
38+
-- mult :: Num a => a -> a -> a
39+
mul a b = church a (+b) 0
40+
41+
-- church :: (Eq t1, Num t1) => t1 -> (t2 -> t2) -> t2 -> t2
42+
church 0 = zero
43+
church n = \f x -> f $ church (n -1 ) f x
44+
45+
-- unchurch :: ((Integer -> Integer) -> Integer -> t3) -> t3
46+
unchurch = Base.flip ($ (1 +)) 0
47+
48+
-- isZero :: (Eq a, Num a) => a -> Bool
49+
isZero = (==) 0

0 commit comments

Comments
 (0)