-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.hs
116 lines (89 loc) · 2.78 KB
/
examples.hs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- import Interp
-- import Defunc
import Continuation
-- import DefuncCont
-- Examples
esucc = EVar "succ"
eequal = EVar "equal"
czero = EConst 0
s = EVar "s"
z = EVar "z"
y = EVar "y"
x = EVar "x"
zero,zero', one, one' :: Exp
zero = lambda "s" $ lambda "z" $ z
zero' = zero `app` esucc `app` czero
one = lambda "s" $ lambda "z" $ app s z
one' = one `app` esucc `app` czero
identity = lambda "x" x
dontimes =
lambda "n" $
lambda "exp" $
lambda "base" $
letrec "ntimes" "num_iters"
(
cond (eequal `app` EVar "num_iters" `app` EVar "n")
(EVar "base")
(app (EVar "exp") $ app (EVar "ntimes") $ esucc `app` EVar "num_iters")
)
(EVar "ntimes" `app` czero)
eplus =
lambda "x" $
lambda "y" $
dontimes `app` x `app` esucc `app` y
eplus' n m = eplus `app` EConst n `app` EConst m
etimes =
lambda "x" $
lambda "y" $
dontimes `app` x `app` (eplus `app` y) `app` EConst 0
etimes' n m = etimes `app` EConst n `app` EConst m
etriple = etimes `app` EConst 3
edouble = etimes `app` EConst 2
etrue = eequal `app` EConst 0 `app` EConst 0
efalse = eequal `app` EConst 1 `app` EConst 0
churchpred =
let s f = lambda "g" $ lambda "h" $ EVar "h" `app` (EVar "g" `app` f) in
let z x = lambda "" $ x in
lambda "n" $ lambda "f"$ lambda "x" $
(((EVar "n" `app` (s $ EVar "f")) `app` (z x)) `app` identity)
epred =
lambda "n" $ churchpred `app` (dontimes `app` EVar "n") `app` esucc `app` czero
eIsZero = lambda "n" $ eequal `app` EVar "n" `app` EConst 0
eIsOne = lambda "n" $ eequal `app` EVar "n" `app` EConst 1
minustwo = lambda "n" $ epred `app` (epred `app` EVar "n")
eeven =
letrec "even" "n"
(cond (eIsZero `app` EVar "n") etrue
(cond (eIsOne `app` EVar "n") efalse
(
eeven `app` (minustwo `app` EVar "n")
)
)
) {-in-} (EVar "even")
ehalf =
lambda "n" $
letrec "search" "i" {-be-}
(cond
{-if-} (eequal `app` EVar "n" `app` (edouble `app` EVar "i"))
{-then-} (EVar "i")
{-else-} (EVar "search" `app` (esucc `app` EVar "i"))
) {-in-} (EVar "search" `app` EConst 0)
hotpo n = cond
{-if-} (eeven `app` n)
{-then-} (ehalf `app` n)
{-else-} (esucc `app` (etriple `app` n))
collatz :: Exp
collatz = letrec "hotpo" {-be-}
{- fun -} "y" {- ==> -} (cond (eequal `app` y `app` EConst 1) y $
EVar "hotpo" `app` hotpo y)
{-in-}
(EVar "hotpo")
omega = let omega' = lambda "x" $ app x x in
app omega' omega'
cbn_inherit :: Exp
cbn_inherit = (lambda "x" $ lambda "y" $ y)
`app` omega
`app` EConst 47
closure_example = lambda "x" (lambda "y" x) `app` EConst 5
-- continu_example = continu (Function (lambda "x" (lambda "y" x) `Appl` EConst 5) Init Fin)
-- (eval (lambda "x" (lambda "y" x)) Init Fin)