-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.sml
486 lines (461 loc) · 11.4 KB
/
eval.sml
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
structure Eval :> EVAL =
struct
open Core
exception EvalError of string
exception AbortError
val labCount = ref 0
fun newLab () = ((labCount := !labCount + 1);!labCount)
(*fun leq' ((l, _, _, l')::S) l2 =
if l' = l2
then true
else leq' S l2
| leq' nil l2 =
false*)
fun leq ((l, _, _, l')::S) l1 l2 =
if l = l1
then (if l = l2 orelse l' = l2
then true
else leq S l' l2)
else leq S l1 l2
| leq nil l1 l2 =
false
fun lookupLabS l ((l1, tvs, t, l2)::S) =
if l = l1
then (tvs, t, l2)
else lookupLabS l S
| lookupLabS l nil =
raise EvalError "lookupLabS"
fun evalProg (Prog e) =
Prog (evalExpLoop nil nil e)
handle EvalError s => (print ("Evalulation Error: " ^ s ^ "\n"); Prog UnitExp)
| (CoreError s) => (print ("Eval Core Error: " ^ s ^ "\n"); Prog UnitExp)
| AbortError => (print ("Program aborted.\n"); Prog UnitExp)
and evalProgDebug (Prog e) =
Prog (evalExpDebugLoop nil nil e)
handle EvalError s => (print ("Evalulation Error: " ^ s ^ "\n"); Prog UnitExp)
and evalExpDebugLoop S A e =
if isVal e
then e
else let
val _ = print (PrettyPrint.ppCExp e ^ "\n\n")
val (S', A', e') = evalExp NilStExp S A e
in
evalExpDebugLoop S' A' e'
end
and evalExpLoop S A e =
if isVal e
then e
else let
val (S', A', e') = evalExp NilStExp S A e
in
evalExpLoop S' A' e'
end
and evalExp stack S A e =
let
fun evalExp' (AppExp (FunExp (x,t,e1), e2)) =
if isVal e2
then (S, A, substExp e2 x e1)
else let
val (S', A', e2') = evalExp' e2
in
(S', A', AppExp (FunExp (x,t,e1), e2'))
end
| evalExp' (AppExp (e1, e2)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', AppExp (e1', e2))
end
| evalExp' (TyAppExp (TyFunExp (a, e), t)) =
(S, A, tysubstExp t a e)
| evalExp' (TyAppExp (e, t)) =
let
val (S', A', e') = evalExp' e
in
(S', A', TyAppExp (e', t))
end
| evalExp' (TupleExp es) =
let
val (S', A', es') = evalExpList stack S A es
in
(S', A', TupleExp es')
end
| evalExp' (LetExp (xs, TupleExp es, e)) =
if (List.all (fn(e) => isVal e) es)
then (S, A, substExpList es xs e)
else let
val (S', A', es') = evalExpList stack S A es
in
(S', A', LetExp(xs,TupleExp(es'),e))
end
| evalExp' (LetExp (xs, e1, e2)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', LetExp (xs, e1', e2))
end
| evalExp' (PCExp (LabExp l, ts, e)) =
if isVal e
then let
val (tvs, t, l') = lookupLabS l S
(* val _ = print ("calling advComp " ^ PrettyPrint.ppCExp e ^ "\n")*)
val v' = advComp S A l (tysubstTypList ts tvs t)
in
(S, A, AppExp (v', e))
end
else let
val (S', A', e') = evalExp' e
in
(S', A', PCExp (LabExp l, ts, e'))
end
| evalExp' (PCExp (e1, ts, e2)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', PCExp (e1', ts, e2))
end
| evalExp' (NewExp(tvs, t, LabExp l)) =
let
val l' = newLab ()
in
((l', tvs, t, l)::S, A, LabExp l')
end
| evalExp' (NewExp(tvs, t, e)) =
let
val (S', A', e') = evalExp' e
in
(S', A', NewExp (tvs, t, e'))
end
| evalExp' (AdvInstExp e) =
if isVal e
then (S, A@[e], UnitExp)
else let
val (S', A', e') = evalExp' e
in
(S', A', AdvInstExp e')
end
| evalExp' (AdvExp (e1, tvs, x, t, e2)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', AdvExp (e1', tvs, x, t, e2))
end
| evalExp' (TypeCaseExp (a, t1, t2, tes)) =
let
val (T,e) = case (List.find (fn (ti,ei) => (((mgu t2 ti);true)
handle MGUError => false)) tes) of
SOME (ti,ei) => (mgu t2 ti, ei)
| NONE => raise EvalError "TypeCaseExp: not complete match"
val ts = map #1 T
val tvs = map #2 T
in
(S, A, (tysubstExpList ts tvs e))
end
| evalExp' (SetExp es) =
let
val (S', A', es') = evalExpList stack S A es
in
(S', A', SetExp es')
end
| evalExp' (UnionExp (SetExp es1, SetExp es2)) =
if List.all (fn(e) => isVal e) es1
then if List.all (fn(e) => isVal e) es2
then (S, A, SetExp (es1 @ es2))
else let
val (S', A', es2') = evalExpList stack S A es2
in
(S', A', UnionExp (SetExp es1, SetExp es2'))
end
else let
val (S', A', es1') = evalExpList stack S A es1
in
(S', A', UnionExp (SetExp es1', SetExp es2))
end
| evalExp' (UnionExp (e1, e2)) =
if isVal e1
then let
val (S', A', e2') = evalExp' e2
in
(S', A', UnionExp (e1, e2'))
end
else
let
val (S', A', e1') = evalExp' e1
in
(S', A', UnionExp (e1', e2))
end
| evalExp' StackExp =
(S, A, stack)
| evalExp' (PtStExp (e1, ts, e2, e3)) =
if isVal e1
then if isVal e2
then let
val (S', A', e3') = evalExp' e3
in
(S', A', PtStExp (e1, ts, e2, e3'))
end
else let
val (S', A', e2') = evalExp' e2
in
(S', A', PtStExp (e1, ts, e2', e3))
end
else let
val (S', A', e1') = evalExp' e1
in
(S', A', PtStExp (e1', ts, e2, e3))
end
| evalExp' (StoreExp (e1, ts, e2, e3)) =
if isVal e1
then if isVal e2
then if isVal e3
then (S, A, e3)
else let
val (S', A', e3') = evalExp (PtStExp (e1, ts, e2, stack)) S A e3
in
(S', A', StoreExp(e1, ts, e2, e3'))
end
else let
val (S', A', e2') = evalExp' e2
in
(S', A', StoreExp (e1, ts, e2', e3))
end
else let
val (S', A', e1') = evalExp' e1
in
(S', A', StoreExp (e1', ts, e2, e3))
end
| evalExp' (StkCaseExp (e1, pes)) =
if isVal e1
then if List.all isValPat (map #1 pes)
then let
val (p,e) = (case (List.find (fn(pi,_) =>
case (stackMatch S e1 pi) of
SOME _ => true
| NONE => false)
pes) of
SOME x => x
| NONE => raise EvalError "StkCaseExp: not complete match")
val (ts, tvs, vs, xs) = (case (stackMatch S e1 p) of
SOME x => x
| NONE => raise EvalError "StkCaseExp: impossible")
in
(S, A, tysubstExpList ts tvs (substExpList vs xs e))
end
else let
val (S', A', pes') = evalPatExpList stack S A pes
in
(S', A', StkCaseExp (e1, pes'))
end
else let
val (S', A', e1') = evalExp' e1
in
(S', A', StkCaseExp (e1', pes))
end
| evalExp' (PlusExp(IntExp i1, IntExp i2)) =
(S, A, IntExp (i1 + i2))
| evalExp' (PlusExp(IntExp i1, e)) =
let
val (S', A', e') = evalExp' e
in
(S', A', PlusExp(IntExp i1, e'))
end
| evalExp' (PlusExp(e1, e2)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', PlusExp(e1', e2))
end
| evalExp' (IfExp (BoolExp b, e1, e2)) =
(S, A, if b then e1 else e2)
| evalExp' (IfExp (e1, e2, e3)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', IfExp(e1', e2, e3))
end
| evalExp' (PrintExp (StringExp s)) =
((print s); (S, A, UnitExp))
| evalExp' (PrintExp e) =
let
val (S', A', e') = evalExp' e
in
(S', A', PrintExp e')
end
| evalExp' (ItoSExp (IntExp i)) =
(S, A, StringExp (Int.toString i))
| evalExp' (ItoSExp e) =
let
val (S', A', e') = evalExp' e
in
(S', A', ItoSExp e')
end
| evalExp' (ConcatExp(StringExp s1, StringExp s2)) =
(S, A, StringExp (s1 ^ s2))
| evalExp' (ConcatExp(StringExp s1, e)) =
let
val (S', A', e') = evalExp' e
in
(S', A', ConcatExp(StringExp s1, e'))
end
| evalExp' (ConcatExp(e1, e2)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', ConcatExp(e1', e2))
end
| evalExp' (FixExp (f, t, e)) =
(S, A, substExp (FixExp (f, t, e)) f e)
| evalExp' AbortExp =
raise AbortError
| evalExp' (EqExp(StringExp s1, StringExp s2)) =
(S, A, BoolExp (case String.compare (s1, s2) of
EQUAL => true
| _ => false))
| evalExp' (EqExp(StringExp s1, e)) =
let
val (S', A', e') = evalExp' e
in
(S', A', EqExp(StringExp s1, e'))
end
| evalExp' (EqExp(e1, e2)) =
let
val (S', A', e1') = evalExp' e1
in
(S', A', EqExp(e1', e2))
end
| evalExp' (PosExp(pos', e)) =
if isVal e
then (S, A, e)
else let
val (S', A', e') = evalExp' e
in
(S', A', PosExp (pos', e'))
end
| evalExp' e = raise EvalError ("stuck at\n" ^ PrettyPrint.ppCExp e)
in
if isVal e
then raise EvalError "evalExp"
else ((*(print (PrettyPrint.ppCExp e ^ "\n\n")); *)evalExp' e)
end
and evalPat stack S A p =
let
fun evalPat' NilPat =
(S, A, NilPat)
| evalPat' (PtPat(e, tvs, x, t, p)) =
if isVal e
then let
val (S', A', p') = evalPat' p
in
(S', A', PtPat (e, tvs, x, t, p'))
end
else let
val (S', A', e') = evalExp stack S A e
in
(S', A', PtPat (e', tvs, x, t, p))
end
| evalPat' (VarPat x) =
(S, A, VarPat x)
| evalPat' (AllPat p) =
let
val (S', A', p') = evalPat' p
in
(S', A', AllPat p')
end
in
if isValPat p
then raise EvalError "evalPat"
else evalPat' p
end
and advComp S nil l t =
let
val x = Id.freshid "x"
in
FunExp(x, t, VarExp x)
end
| advComp S (AdvExp(SetExp ls, tvs, x, t, e)::A) l t' =
let
val v = advComp S A l t'
in
if List.exists (fn(LabExp li) => leq S l li
| _ => raise EvalError "advComp: AdvExp") ls
then let
val ttvs = findSubst tvs t t'
val ts = map #1 ttvs
val tvs' = map #2 ttvs
(*val _ = print ("found match" ^ PrettyPrint.ppCExp e ^ "\n")*)
in
FunExp(x, t', AppExp (v, tysubstExpList ts tvs' e))
end
else v
end
| advComp _ (e::t) _ _ =
raise EvalError ("AdvComp: " ^ PrettyPrint.ppCExp e)
and stackMatch S =
let
fun stackMatch' NilStExp NilPat =
SOME (nil, nil, nil, nil)
| stackMatch' (PtStExp(LabExp l, ts, v1, v2)) (PtPat(SetExp ls, tvs, x, t1, p)) =
let
val sub = stackMatch' v2 p
in
case sub of SOME (tysub1, tysub2, sub1, sub2) =>
if List.exists (fn(LabExp li) => leq S l li
| _ => raise EvalError "stackMatch: PtStExp PtPat") ls
then let
val (tvs', t2, l') = lookupLabS l S
val otvs = findSubst tvs t1 (tysubstTypList ts tvs' t2)
val os = map #1 otvs
val tvs'' = map #2 otvs
in
SOME (os@tysub1, tvs''@tysub2, v1::sub1, x::sub2)
end
else NONE
| NONE => NONE
end
| stackMatch' (PtStExp(l,ts,v1,v2)) (AllPat p) =
stackMatch' v2 p
| stackMatch' v (VarPat x) =
SOME (nil, nil, [v], [x])
| stackMatch' _ _ =
NONE
in
stackMatch'
end
and evalExpList stack S A =
let
fun evalExpList' (e::es) =
if (isVal e)
then let
val (S', A', es') = evalExpList' es
in
(S', A', e::es')
end
else let
val (S', A', e') = evalExp stack S A e
(* val (S'', A'', es') = evalExpList stack S' A' es*)
in
(S', A', e'::es)
end
| evalExpList' nil = (S, A, nil)
in
evalExpList'
end
and evalPatExpList stack S A =
let
fun evalPatExpList' ((p,e)::pes) =
if (isValPat p)
then let
val (S', A', pes') = evalPatExpList' pes
in
(S', A', (p,e)::pes')
end
else let
val (S', A', p') = evalPat stack S A p
in
(S', A', (p',e)::pes)
end
| evalPatExpList' nil = (S, A, nil)
in
evalPatExpList'
end
end