-
Notifications
You must be signed in to change notification settings - Fork 6
/
lisp.scala
303 lines (241 loc) · 9.58 KB
/
lisp.scala
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// A Lisp-Like Front-End
object Lisp {
import Base._
import scala.util.parsing.combinator._
// s-expr parser
object parser extends JavaTokenParsers with PackratParsers {
override val whiteSpace = """(\s|(;[^\n]*))+""".r
def S(x:String) = Str(x)
def P(x:Val,y:Val) = Tup(x,y)
def I(x:Int) = Cst(x)
val N = Str(".")
lazy val exp: Parser[Val] =
wholeNumber ^^ { case s => I(s.toInt) } |
"""[^\s\(\)'"]+""".r ^^ { case s => S(s) } |
"'" ~> exp ^^ { case s => P(S("quote"), P(s, N)) } |
"()" ^^ { case _ => N } |
"(" ~> exps <~ ")" ^^ { case vs => vs }
lazy val exps: Parser[Val] =
exp ~ opt(exps) ^^ { case v~Some(vs) => P(v, vs) case v~None => P(v,N) }
}
import parser._
def parseExp(s: String) = {
val Success(v, _) = parseAll(exp, s)
v
}
// ********************* convert exp encoded as val --> proper exp *********************
def trans(e: Val, env: List[String]): Exp = e match {
case Cst(n) => Lit(n)
case Str(s) => val i = env.lastIndexOf(s); assert(i>=0, s + " not in " + env); Var(i)
case Tup(Str("quote"), Tup(Str(s),N)) => Sym(s)
case Tup(Str("+"), Tup(a,Tup(b,N))) => Plus(trans(a,env),trans(b,env))
case Tup(Str("-"), Tup(a,Tup(b,N))) => Minus(trans(a,env),trans(b,env))
case Tup(Str("*"), Tup(a,Tup(b,N))) => Times(trans(a,env),trans(b,env))
// (let x a b)
case Tup(Str("let"), Tup(Str(x),Tup(a,Tup(b,N)))) => Let(trans(a,env),trans(b,env:+x))
// (lambda f x e)
case Tup(Str("lambda"), Tup(Str(f),Tup(Str(x),Tup(e,N)))) => Lam(trans(e,env:+f:+x))
case Tup(Str("if"), Tup(c,Tup(a,Tup(b,N)))) => If(trans(c,env),trans(a,env),trans(b,env))
case Tup(Str("num?"), Tup(a,N)) => IsNum(trans(a,env))
case Tup(Str("sym?"), Tup(a,N)) => IsStr(trans(a,env))
case Tup(Str("pair?"), Tup(a,N)) => IsCons(trans(a,env))
case Tup(Str("cons"), Tup(a,Tup(b,N))) => Cons(trans(a,env),trans(b,env))
case Tup(Str("car"), Tup(a,N)) => Fst(trans(a,env))
case Tup(Str("cdr"), Tup(a,N)) => Snd(trans(a,env))
case Tup(Str("cadr"), Tup(a,N)) => Fst(Snd(trans(a,env)))
case Tup(Str("caddr"), Tup(a,N)) => Fst(Snd(Snd(trans(a,env))))
case Tup(Str("cadddr"), Tup(a,N)) => Fst(Snd(Snd(Snd(trans(a,env)))))
case Tup(Str("lift"), Tup(a,N)) => Lift(trans(a,env))
case Tup(Str("nolift"), Tup(a,N)) => trans(a,env)
case Tup(Str("eq?"), Tup(a,Tup(b,N))) => Equ(trans(a,env),trans(b,env))
case Tup(Str("run"), Tup(b,Tup(a,N))) => Run(trans(b,env),trans(a,env))
case Tup(Str("log"), Tup(b,Tup(a,N))) => Log(trans(b,env),trans(a,env))
case Tup(Str("quote"), Tup(a,N)) => Special(benv => a)
case Tup(Str("trans"), Tup(a,N)) =>
Special(benv => Code(trans(evalms(benv, trans(a,env)), env)))
case Tup(Str("lift-ref"),Tup(a,N)) =>
Special(benv => Code(Special(b2 => evalms(benv,trans(a,env)))))
case Tup(a,Tup(b,N)) => App(trans(a,env),trans(b,env))
}
def ev(src: String) = {
val prog_src = src
val prog_val = parseExp(prog_src)
val prog_exp = trans(prog_val,Nil)
reifyv(evalms(Nil,prog_exp))
}
def checkrun(src: String, dst: String) = {
val res = ev(src)
check(res)(dst)
res
}
def checkcode(src: String, dst: String) = {
val Code(res) = ev(src)
check(pretty(res, Nil))(dst)
res
}
def prettycode(e: Exp) = pretty(e, Nil)
def checkrunlog(src: String, dst: String, expected_log: String) = {
val oldLog = Base.log
try {
var s = ""
Base.log = {x => s += x.toString+";" }
val res = checkrun(src, dst)
check(s)(expected_log)
res
} finally {
Base.log = oldLog
}
}
// basic test cases
def test() = {
println("// ------- Lisp.test --------")
// Some aspects of our multi-stage language may appear
// a bit counterintuitive, but are essential for
// understanding how things work -- especially how
// `lift` and `run` interact.
// Broadly we can do two things very easily:
// - Return code from the main program: (lift ...)
// - Build code and run it: (run 0 (.. (lift ..) ..))
// The key thing to avoid is calling `run` on code
// that is generated outside of the `run` statement.
// Below are some concrete examples.
// (1)
// Below, operator `lift` will `reflect` its argument,
// i.e. create a new binding Var(0) = Lam(Var(1))
// in the current scope of expressions being generated.
// The present-stage variable `code` will be bound to
// expression Code(Var(0)). Whole-program evaluation
// assembles all generated bindings into a nested let:
checkrun(s"""
(let code (lift (lambda f x x))
code)
""","Code(Let(Lam(Var(1)),Var(0)))")
// Pretty-printing hides the let if the expression
// is of the form (let x e x)
checkcode(s"""
(let code (lift (lambda f x x))
code)
""","(lambda f0 x1 x1)")
// So the result of evaluating the whole expression is
// a block of code.
// We can't ignore code that is reflected. For example,
// (let code (lift ..) 3)
// Would throw an exception.
// (2)
// Things may get surprising if we try to generate
// and run code at the same time -- in general, one
// needs to avoid running code that is generated
// outside of the `run` statement.
// Intuitively we might expect the following code to
// return a plain closure (create a lifted closure,
// then eval it to a value).
checkrun(s"""
(let code (lift (lambda f x x))
(run 0 code))
""","Code(Let(Lam(Var(1)),Var(0)))")
// However, that's not what happens. Again, variable
// `code` is bound to Code(Var(0)), the symbol generated
// from reflecting the `(lift (lambda ...))`.
// So `run` is called with Code(Var(0)), i.e. Var(0)
// is the expression being evaluated, and that
// happends to be exactly variable `code`, which is
// again mapped to Code(Var(0)). Hence, the same
// result as in (1).
// Let's test our understanding further:
checkrun(s"""
(let z (lift 42)
(let code (lift (lambda f x x))
(run 0 code)))
""","Code(Let(Lam(Var(1)),Lit(42)))")
// Yes, this one returns 42, wrapped in a let that
// declares the lambda.
// (Again, we have to use lift 42 instead of just 42,
// since we have to return a code value.)
// Future versions may entirely prohibit such uses,
// at the expense of additional checks that associate
// a particular run-scope (or the top level) with
// each Code value, and prevent mixing of levels.
// (3)
// Now how do we actually achieve the desired behavior:
// declare a (closed) piece of code, then evaluate it?
// Simple, we just wrap it in a function:
checkrun(s"""
(let code (lambda _ _ (lift (lambda f x x)))
(run 0 (code 'dummy)))
""","Clo(List(Clo(List(),Lift(Lam(Var(3))))),Var(2))")
// Disregarding the embedded closure environment that
// includes the value of `code`, the result can be
// read more conveniently as:
// Clo(code/Var0=..., (lambda f/Var1 x/Var2. x/Var))
// The key is to ensure that all code pieces are reflected
// within the **dynamic** scope of evaluating `run`'s
// argument, including functions called within that
// dynamic extent.
// (4)
// What if we want to run the same code multiple times
// instead re-generating it? Simple, generate a function
// and call it multiple times.
// (simple exercise -- note that we're already generating
// functions)
// (5)
// Note that generated code can refer to present-stage
// variables, using `quote` and `trans`.
// This has a similar effect to standard quotation,
// and comes with similar hygiene issues (some are
// aggravated by our use of De Bruijn levels for
// variable names).
checkrun(s"""
(let z 21
(let code (trans '(+ z z))
(run 0 code)))
""","Cst(42)")
// In contrast to `lift`, `trans` creates a closed
// code value directly.
// Observe how the piece of code refers to variable
// z = Var(0) directly:
checkrun(s"""
(let z 21
(let code (trans '(+ z z))
code))
""","Code(Plus(Var(0),Var(0)))")
// Now let's take a look at the interaction with
// bindings inside a `trans` block:
checkrun(s"""
(let z 21
(let code (trans '(lambda _ x (+ z x)))
code))
""","Code(Lam(Plus(Var(0),Var(2))))")
// When we try to run this code, there's
// again a problem:
checkrun(s"""
(let z 20
(let code (trans '(lambda _ x (+ z x)))
(let var2 10
(let f (run 0 code)
(f 22)))))
""","Cst(30)")
// Intuitively we'd expect to get result 42,
// but we obtain 30. This happens because
// `x` inside the `trans`ed lambda is
// Var(2) at this position, but when `run`
// is called, Var(2) actually corresponds
// to `var2`.
// A more elaborate representation of bound
// and free names would fix this (e.g. locally
// nameless) at the expense of complexity and
// runtime overhead. Other hygiene issues
// (e.g. variables going out of scope) would
// remain.
// Right now, the recommendation is to use
// `trans` only as a direct argument to `run`:
checkrun(s"""
(let z 20
(let var2 10
(let f (run 0 (trans '(lambda _ x (+ z x))))
(f 22))))
""","Cst(42)")
// This is good enough for the key use case
// of `trans` in `EM` (see pink.scala).
testDone()
}
}