-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChurchEncoding.idr
78 lines (56 loc) · 2.14 KB
/
ChurchEncoding.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module ChurchEncoding
import LambdaCalculus
%access public export
tx: TVar
tx = TV "X"
ty: TVar
ty = TV "Y"
tz: TVar
tz = TV "Z"
x: Var tx
x = MkVar "x"
y: Var tx
y = MkVar "y"
ctor: List LambdaType -> LambdaType
ctor ts = functionType ts tx
churchEncoding: List (List LambdaType) -> LambdaType
churchEncoding ts = Pi tx $ ctor (map ctor ts)
export
bool: LambdaType
bool = churchEncoding [Nil, Nil]
public export
true: LambdaExpression ChurchEncoding.bool
true = TypeAbs tx true' where
true': LambdaExpression (tx ->> tx ->> tx)
true' = λ x (λ y x)
public export
false: LambdaExpression ChurchEncoding.bool
false = Λ tx false' where
false': LambdaExpression(tx ->> tx ->> tx)
false' = λ x (λ y y)
export
ite: LambdaExpression (Pi ChurchEncoding.ty $ ChurchEncoding.bool ->> ChurchEncoding.ty ->> ChurchEncoding.ty ->> ChurchEncoding.ty )
ite = let b = (MkVar{type=bool} "b")
t = (MkVar{type=ty} "t")
f = (MkVar{type=ty} "f")
in Λ ty $ λ b $ λ t $ λ f $ ?hole --((b `TypeApp` ty) `App` t) `App` f where
--ite: (LambdaExpression ChurchEncoding.bool) -> (LambdaExpression a) -> (LambdaExpression a) -> LambdaExpression a
--ite {a} b t f = betaReduce $ App (App (TypeApp b a) t) f
-- pair a b = ΠX.(a -> b -> X) -> X
prod: LambdaType -> LambdaType -> LambdaType
prod a b = churchEncoding [[a, b]]
mkProd: LambdaExpression(Pi ty $ Pi tz $ ty --> tz --> (prod ty tz))
mkProd = Λ ty $ Λ tz $ λ y $ λ z $ ?hole
--mkProd: LambdaExpression a -> LambdaExpression b -> LambdaExpression (prod a b)
--mkProd x y = TypeAbs tx $ Abs (MkVar "f") $ ((MkVar "f") `App` x) `App` y
fst: LambdaExpression (prod a b) -> LambdaExpression a
fst {a} pair = App (TypeApp pair a) fst' where
fst' = Abs (MkVar "x") $ Abs (MkVar "y") (MkVar "x")
snd: LambdaExpression (prod a b) -> LambdaExpression b
snd {b} pair = App (TypeApp pair b) snd' where
snd' = Abs (MkVar "x") $ Abs (MkVar "y") (MkVar "y")
list: LambdaType -> LambdaType
list a = churchEncoding [Nil, [a]]
nil: LambdaExpression (Pi ChurchEncoding.ty $ list ChurchEncoding.ty)
nil = TypeAbs ty $ TypeAbs tx $ Abs (MkVar "z") $ Abs (MkVar "c") $ MkVar "z"
--cons: LambdaExpression