-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.go
439 lines (372 loc) · 9.8 KB
/
eval.go
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Evaluation. We assume things have been typechecked.
*/
package main
import (
// "fmt"
"reflect"
)
func evalUnaryExpr(x *UnaryExpr) Expr {
r, _ := reduceExpr(x.right)
int64Ops := map[tokenKind](func(int64) int64){
tokenPlus: func(a int64) int64 { return a },
tokenMinus: func(a int64) int64 { return -a },
}
float64Ops := map[tokenKind](func(float64) float64){
tokenPlus: func(a float64) float64 { return a },
tokenMinus: func(a float64) float64 { return -a },
}
switch x.op {
case tokenPlus:
fallthrough
case tokenMinus:
return &IntExpr{expr{&IntType{typ{}}}, int64Ops[x.op](r.(*IntExpr).v)}
case tokenFPlus:
fallthrough
case tokenFMinus:
return &FloatExpr{expr{&FloatType{typ{}}}, float64Ops[x.op](r.(*FloatExpr).v)}
case tokenExcl:
return &BoolExpr{expr{&BoolType{typ{}}}, !r.(*BoolExpr).v}
default:
panic("TODO: " + x.op.String())
}
return nil
}
func evalBinaryExpr(x *BinaryExpr) Expr {
l, _ := reduceExpr(x.left)
r, _ := reduceExpr(x.right)
int64Ops := map[tokenKind](func(int64, int64) int64){
tokenPlus: func(a, b int64) int64 { return a + b },
tokenStar: func(a, b int64) int64 { return a * b },
tokenMinus: func(a, b int64) int64 { return a - b },
tokenSlash: func(a, b int64) int64 { return a / b },
}
int64CmpOps := map[tokenKind](func(int64, int64) bool){
tokenLess: func(a, b int64) bool { return a < b },
tokenMore: func(a, b int64) bool { return a > b },
tokenLessEq: func(a, b int64) bool { return a <= b },
tokenMoreEq: func(a, b int64) bool { return a >= b },
}
float64Ops := map[tokenKind](func(float64, float64) float64){
tokenPlus: func(a, b float64) float64 { return a + b },
tokenStar: func(a, b float64) float64 { return a * b },
tokenMinus: func(a, b float64) float64 { return a - b },
tokenSlash: func(a, b float64) float64 { return a / b },
}
float64CmpOps := map[tokenKind](func(float64, float64) bool){
tokenFLess: func(a, b float64) bool { return a < b },
tokenFMore: func(a, b float64) bool { return a > b },
tokenFLessEq: func(a, b float64) bool { return a <= b },
tokenFMoreEq: func(a, b float64) bool { return a >= b },
}
boolOps := map[tokenKind](func(bool, bool) bool){
tokenAndAnd: func(a, b bool) bool { return a && b },
tokenOrOr: func(a, b bool) bool { return a || b },
}
switch x.op {
// XXX/TODO: should we allow e.g. x + 3? where x
// is undefined (why not I guess?)
case tokenPlus:
fallthrough
case tokenStar:
fallthrough
case tokenMinus:
fallthrough
case tokenSlash:
return &IntExpr{expr{&IntType{typ{}}},
int64Ops[x.op](l.(*IntExpr).v, r.(*IntExpr).v),
}
case tokenLess:
fallthrough
case tokenMore:
fallthrough
case tokenLessEq:
fallthrough
case tokenMoreEq:
return &BoolExpr{expr{&BoolType{typ{}}},
int64CmpOps[x.op](l.(*IntExpr).v, r.(*IntExpr).v),
}
case tokenFPlus:
fallthrough
case tokenFStar:
fallthrough
case tokenFMinus:
fallthrough
case tokenFSlash:
return &FloatExpr{expr{&FloatType{typ{}}},
float64Ops[x.op](l.(*FloatExpr).v, r.(*FloatExpr).v),
}
case tokenFLess:
fallthrough
case tokenFMore:
fallthrough
case tokenFLessEq:
fallthrough
case tokenFMoreEq:
return &BoolExpr{expr{&BoolType{typ{}}},
float64CmpOps[x.op](l.(*FloatExpr).v, r.(*FloatExpr).v),
}
case tokenAndAnd:
fallthrough
case tokenOrOr:
return &BoolExpr{expr{&BoolType{typ{}}},
boolOps[x.op](l.(*BoolExpr).v, r.(*BoolExpr).v),
}
default:
panic("TODO: " + x.op.String())
}
}
// α-renaming x{b,a}: renaming a as b in x.
//
// renaming is performed in-place (why not I guess?)
func renameExpr(x Expr, b, a string) Expr {
switch x.(type) {
case *UnitExpr:
return x
case *IntExpr:
return x
case *FloatExpr:
return x
case *BoolExpr:
return x
case *ProductExpr:
x.(*ProductExpr).left = renameExpr(x.(*ProductExpr).left, b, a)
x.(*ProductExpr).right = renameExpr(x.(*ProductExpr).right, b, a)
return x
case *UnaryExpr:
x.(*UnaryExpr).right = renameExpr(x.(*UnaryExpr).right, b, a)
return x
case *BinaryExpr:
x.(*BinaryExpr).left = renameExpr(x.(*BinaryExpr).left, b, a)
x.(*BinaryExpr).right = renameExpr(x.(*BinaryExpr).right, b, a)
return x
case *AppExpr:
x.(*AppExpr).left = renameExpr(x.(*AppExpr).left, b, a)
x.(*AppExpr).right = renameExpr(x.(*AppExpr).right, b, a)
return x
case *VarExpr:
if x.(*VarExpr).name == a {
x.(*VarExpr).name = b
}
return x
case *AbsExpr:
if x.(*AbsExpr).name == a {
x.(*AbsExpr).name = b
}
x.(*AbsExpr).right = renameExpr(x.(*AbsExpr).right, b, a)
return x
default:
panic("assert")
}
return nil
}
func copyType(t Type) Type {
switch t.(type) {
case *VarType:
return &VarType{typ{}, t.(*VarType).name}
case *ArrowType:
return &ArrowType{
typ{},
copyType(t.(*ArrowType).left),
copyType(t.(*ArrowType).right),
}
case *ProductType:
return &ProductType{
typ{},
copyType(t.(*ProductType).left),
copyType(t.(*ProductType).right),
}
// "iotas" (unit / primitive types)
case *UnitType:
return &UnitType{typ{}}
case *BoolType:
return &BoolType{typ{}}
case *IntType:
return &IntType{typ{}}
case *FloatType:
return &FloatType{typ{}}
case *typ:
return &typ{}
default:
return nil
// panic("O__o: "+reflect.ValueOf(t).Type().String())
}
return t
}
func copyExpr(x Expr) Expr {
switch x.(type) {
case *UnitExpr:
return &UnitExpr{expr{copyType(x.getType())}}
case *IntExpr:
return &IntExpr{expr{copyType(x.getType())}, x.(*IntExpr).v}
case *FloatExpr:
return &FloatExpr{expr{copyType(x.getType())}, x.(*FloatExpr).v}
case *BoolExpr:
return &BoolExpr{expr{copyType(x.getType())}, x.(*BoolExpr).v}
case *ProductExpr:
return &ProductExpr{
expr{copyType(x.getType())},
copyExpr(x.(*ProductExpr).left),
copyExpr(x.(*ProductExpr).right),
}
case *UnaryExpr:
return &UnaryExpr{
expr{copyType(x.getType())},
x.(*UnaryExpr).op,
copyExpr(x.(*ProductExpr).right),
}
case *BinaryExpr:
return &BinaryExpr{
expr{copyType(x.getType())},
x.(*BinaryExpr).op,
copyExpr(x.(*BinaryExpr).left),
copyExpr(x.(*BinaryExpr).right),
}
case *AppExpr:
return &AppExpr{
expr{copyType(x.getType())},
copyExpr(x.(*AppExpr).left),
copyExpr(x.(*AppExpr).right),
}
case *VarExpr:
return &VarExpr{
expr{copyType(x.getType())},
x.(*VarExpr).name,
}
case *AbsExpr:
return &AbsExpr{
expr{copyType(x.getType())},
copyType(x.(*AbsExpr).typ),
x.(*AbsExpr).name,
copyExpr(x.(*AbsExpr).right),
}
default:
panic("assert")
}
return nil
}
// β-substitution: x[y/a]: substituing a for y in x
func substituteExpr(x, y Expr, a string) Expr {
switch x.(type) {
case *UnitExpr:
return x
case *IntExpr:
return x
case *FloatExpr:
return x
case *BoolExpr:
return x
case *ProductExpr:
x.(*ProductExpr).left = substituteExpr(x.(*ProductExpr).left, y, a)
x.(*ProductExpr).right = substituteExpr(x.(*ProductExpr).right, y, a)
return x
case *UnaryExpr:
x.(*UnaryExpr).right = substituteExpr(x.(*UnaryExpr).right, y, a)
return x
case *BinaryExpr:
x.(*BinaryExpr).left = substituteExpr(x.(*BinaryExpr).left, y, a)
x.(*BinaryExpr).right = substituteExpr(x.(*BinaryExpr).right, y, a)
return x
case *AppExpr:
x.(*AppExpr).left = substituteExpr(x.(*AppExpr).left, y, a)
x.(*AppExpr).right = substituteExpr(x.(*AppExpr).right, y, a)
return x
case *VarExpr:
if x.(*VarExpr).name == a {
// NOTE/TODO: because substitution/evaluation
// are performed in-place, we can't just use the
// same y pointer for every occurence.
//
// However, if we knew they'd be only one use,
// then we could (optimization)
return copyExpr(y)
}
return x
case *AbsExpr:
name := x.(*AbsExpr).name
if name == a {
return x
}
if !isFree(y, name) {
x.(*AbsExpr).right = substituteExpr(x.(*AbsExpr).right, y, a)
return x
}
// bounded variable name of x occurs freely in y:
// if we're about so swap a for y below the current
// abstraction, we need to make sure our name won't
// conflict with what happens in y. Hence, we need
// to get a name which would conflict with nothing in
// x, y or a for that matter.
b := getFresh(allVars(x.(*AbsExpr).right), allVars(y), map[string]bool{a: true})
x.(*AbsExpr).name = b
x.(*AbsExpr).right = substituteExpr(
renameExpr(x.(*AbsExpr).right, b, name), y, a,
)
return x
default:
panic("assert")
}
return nil
}
func reduceExpr(x Expr) (Expr, bool) {
switch x.(type) {
// NOTE: "cannot fallthrough in type switch"
case *UnitExpr:
return x, false
case *IntExpr:
return x, false
case *FloatExpr:
return x, false
case *BoolExpr:
return x, false
case *UnaryExpr:
return evalUnaryExpr(x.(*UnaryExpr)), true
case *BinaryExpr:
return evalBinaryExpr(x.(*BinaryExpr)), true
case *AbsExpr:
var b bool
x.(*AbsExpr).right, b = reduceExpr(x.(*AbsExpr).right)
return x, b
case *VarExpr:
return x, false
case *AppExpr:
// XXX hmm, will this always be an AbsEexpr?
if _, ok := x.(*AppExpr).left.(*AbsExpr); ok {
return substituteExpr(
x.(*AppExpr).left.(*AbsExpr).right,
x.(*AppExpr).right,
x.(*AppExpr).left.(*AbsExpr).name,
), true
}
var bl, br bool
x.(*AppExpr).left, bl = reduceExpr(x.(*AppExpr).left)
x.(*AppExpr).right, br = reduceExpr(x.(*AppExpr).right)
return x, bl || br
default:
panic("assert: " + reflect.ValueOf(x).Type().String())
}
}
// NOTE: we're returning an Expr here.
//
// This is because computation is expected to stop on irreducible
// lambda expressions at some point.
//
// TODO: add a configurable timeout here
// TODO: termination detection feels clumsy as hell; we can't
// compare x with y, as reduction will modify its input in-place.
func evalExpr(x Expr) Expr {
var y Expr
var b bool
for {
// fmt.Printf("%s\n", x)
y, b = reduceExpr(x)
if !b {
return x
}
x = y
}
}
// To ease tests so far
func mustSTypeParse(s string) Expr {
return mustSType(mustParse(s))
}