-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch7-closures.hs
178 lines (122 loc) · 3.08 KB
/
ch7-closures.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module Ch7Closures where
type Symbol = String
data ExprC
= NumC Int
| IdC Symbol
| AppC ExprC ExprC
| PlusC ExprC ExprC
| MultC ExprC ExprC
| LamC Symbol ExprC
deriving (Eq, Show)
data Value
= NumV Int
| ClosV Symbol ExprC Env
deriving (Eq, Show)
-- Environment
data Binding
= Bind Symbol Value
deriving (Eq, Show)
type Env = [Binding]
mtEnv :: Env
mtEnv = []
extendEnv :: Binding -> Env -> Env
extendEnv = (:)
lookupEnv :: Symbol -> Env -> Value
lookupEnv s [] = error $ "unbound: " ++ s
lookupEnv s ((Bind t v):env) = if s == t then v else lookupEnv s env
-- Interpreter
interp :: ExprC -> Env -> Value
interp expr env =
case expr of
NumC n ->
NumV n
IdC s ->
lookupEnv s env
AppC e a ->
let
c = interp e env
in
apply c (interp a env)
PlusC l r ->
add (interp l env) (interp r env)
MultC l r ->
mul (interp l env) (interp r env)
LamC p b ->
ClosV p b env
test1 =
(==)
(interp (PlusC (NumC 10) (AppC (LamC "_" (NumC 5)) (NumC 10)))
mtEnv)
(NumV 15)
test2 =
(==)
(interp (AppC (LamC "x" (AppC (LamC "y" (PlusC (IdC "x") (IdC "y")))
(NumC 4)))
(NumC 3))
mtEnv)
(NumV 7)
nested1 =
LamC "x"
(LamC "x" (PlusC (IdC "x") (IdC "x")))
appNested1 =
AppC nested1 (NumC 5)
appNested2 =
AppC appNested1 (NumC 4)
test3 =
(==)
(interp appNested1 mtEnv)
(ClosV "x" (PlusC (IdC "x") (IdC "x")) [Bind "x" (NumV 5)])
-- Since the new binding of x will come before the old binding of x
-- i.e. [Bind "x" (NumV 4), Bind "x" (NumV 5)]
-- the result will be 8 instead of 10
test4 =
(==)
(interp appNested2 mtEnv)
(NumV 8)
nested2 =
LamC "x"
(LamC "y" (PlusC (IdC "x") (IdC "y")))
appNested3 =
AppC nested2 (NumC 5)
appNested4 =
AppC appNested3 (NumC 4)
test5 =
(==)
(interp appNested3 mtEnv)
(ClosV "y" (PlusC (IdC "x") (IdC "y")) [Bind "x" (NumV 5)])
test6 =
(==)
(interp appNested4 mtEnv)
(NumV 9)
-- error: unbound x
-- Why?
--
-- (\f (\x (f 10)))(\y (x + y))(5) ; env = []
-- (\x (f 10))(5) ; env = f: (\y (x + y)) []
-- (f 10) ; env = x: 5, f: (\y (x + y)) []
--
-- Since f is a closure it will be interpreted in its environment which is
-- empty
--
-- ((\y (x + y)) 10) ; env = []
-- x + y ; env = y: 10
--
-- But x is still free, hence the result
test7 = interp (AppC (AppC (LamC "f" (LamC "x" (AppC (IdC "f") (NumC 10))))
(LamC "y" (PlusC (IdC "x") (IdC "y"))))
(NumC 5))
mtEnv
-- This test shows that environments automatically implement capture-free
-- substitution
--
-- Why? Because closures are interpreted in their saved environments
-- Helpers
apply :: Value -> Value -> Value
apply (ClosV p b env) a = interp b (extendEnv (Bind p a) env)
apply _ _ = error "not a closure"
add :: Value -> Value -> Value
add (NumV a) (NumV b) = NumV (a + b)
add _ _ = error "one argument was not a number"
mul :: Value -> Value -> Value
mul (NumV a) (NumV b) = NumV (a * b)
mul _ _ = error "one argument was not a number"