-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypecheck.sml
561 lines (537 loc) · 15.3 KB
/
typecheck.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
structure TypeCheck :> TYPECHECK =
struct
open Core PrettyPrint Util
exception TypeError of string
fun not_eq t1 t2 =
ppCTyp t1 ^ " != " ^ ppCTyp t2
fun type_err pos s =
raise (TypeError ("Position " ^ string_of_pos pos ^ ": " ^ s))
fun lookupVarG pos x (VarG(y,t)::G) =
if (Id.eqid x y)
then t
else lookupVarG pos x G
| lookupVarG pos x (_::G) =
lookupVarG pos x G
| lookupVarG pos x nil =
type_err pos ("lookupVarG " ^ ppVar x)
fun lookupLabG pos x (LabG(y,tvs,t)::G) =
if (x = y)
then (tvs,t)
else lookupLabG pos x G
| lookupLabG pos x (_::G) =
lookupLabG pos x G
| lookupLabG pos x nil =
type_err pos "lookupLabG"
val Ubefore = Id.makeid "Ubefore"
val Ustk = Id.makeid "Ustk"
val Uafter = Id.makeid "Uafter"
fun completePatMatch pos ps =
let
fun completePatMatch' true nil =
(false, nil)
| completePatMatch' false nil =
type_err pos ("CompletePatMatch: match not complete")
| completePatMatch' _ (NilPat::ps) =
completePatMatch' true ps
| completePatMatch' _ ((VarPat _)::ps) =
(true,nil)
| completePatMatch' hasNil ((AllPat p)::ps) =
let
val (b, ps') = completePatMatch' hasNil ps
in
(b, p::ps')
end
| completePatMatch' hasNil ((PtPat(LetExp ([_, x, _],
TupleExp [SetExp [VarExp Ubefore],
SetExp [VarExp Ustk],
SetExp [VarExp Uafter]], VarExp y),
_,
_,
_,
p))::ps) =
if (Id.eqid x y)
then let
val (b, ps') = completePatMatch' hasNil ps
in
(b,p::ps')
end
else completePatMatch' hasNil ps
| completePatMatch' hasNil (_::ps) =
completePatMatch' hasNil ps
val (b,ps') = completePatMatch' false ps
in
if b
then true
else completePatMatch pos ps'
end
fun completeTypMatch pos ts =
let
fun completeTypMatch' ftvss ts =
let
fun hasVarTyp (ftvs::ftvss) ((VarTyp b)::ts) =
if inList b ftvs
then true
else hasVarTyp ftvss ts
| hasVarTyp (_::ftvss) (_::ts) =
hasVarTyp ftvss ts
| hasVarTyp _ _ =
false
fun hasBaseTyp t (t'::ts) =
if eq_typ t t'
then true
else hasBaseTyp t ts
| hasBaseTyp _ nil =
false
fun filterArrTyp (ftvs::ftvss) ((ArrTyp (t1, t2))::ts) =
let
val (ftvss', t1s::t2s::nil) = filterArrTyp ftvss ts
in
(ftvs::ftvss', [t1::t1s, t2::t2s])
end
| filterArrTyp (ftvs::ftvss) (t::ts) =
filterArrTyp ftvss ts
| filterArrTyp _ _ =
(nil, [nil, nil])
fun filterTupleTyp (ftvs::ftvss) ((TupleTyp ts)::ts') =
let
val (ftvss', tss) = filterTupleTyp ftvss ts'
in
case tss of
nil =>
(ftvs::ftvss', [ts])
| tss' =>
(ftvs::ftvss', map (fn(ts,ts') => ts::ts')
(zip ts tss))
end
| filterTupleTyp (ftvs::ftvss) (t::ts) =
filterTupleTyp ftvss ts
| filterTupleTyp _ _ =
(nil, nil)
fun hasTupleTyp (ftvss, tss) =
List.all (fn (ts) => completeTypMatch' ftvss ts) tss
fun filterForAllTyp (ftvs::ftvss) ((ForAllTyp (tv, t))::ts) =
let
val (ftvss', ts') = filterForAllTyp ftvss ts
in
((subList ftvs [tv])::ftvss', t::ts')
end
| filterForAllTyp (ftvs::ftvss) (t::ts) =
filterForAllTyp ftvss ts
| filterForAllTyp _ _ =
(nil, nil)
fun filterPCTyp (ftvs::ftvss) ((PCTyp (tvs, t))::ts) =
let
val (ftvss', ts') = filterPCTyp ftvss ts
in
((subList ftvs tvs)::ftvss', t::ts')
end
| filterPCTyp (ftvs::ftvss) (t::ts) =
filterPCTyp ftvss ts
| filterPCTyp _ _ =
(nil, nil)
fun filterLabTyp (ftvs::ftvss) ((LabTyp (tvs, t))::ts) =
let
val (ftvss', ts') = filterLabTyp ftvss ts
in
((subList ftvs tvs)::ftvss', t::ts')
end
| filterLabTyp (ftvs::ftvss) (t::ts) =
filterLabTyp ftvss ts
| filterLabTyp _ _ =
(nil, nil)
fun hasComplexTyp (ftvss, ts) =
completeTypMatch' ftvss ts
in
if hasVarTyp ftvss ts
then true
else (if hasBaseTyp UnitTyp ts
andalso hasBaseTyp StringTyp ts
andalso hasBaseTyp StackTyp ts
andalso hasBaseTyp AdvTyp ts
andalso hasBaseTyp IntTyp ts
andalso hasBaseTyp BoolTyp ts
andalso hasTupleTyp (filterArrTyp ftvss ts)
andalso hasTupleTyp (filterTupleTyp ftvss ts)
andalso hasComplexTyp (filterForAllTyp ftvss ts)
andalso hasComplexTyp (filterLabTyp ftvss ts)
andalso hasComplexTyp (filterPCTyp ftvss ts)
then false
else type_err pos ("CompleteTypMatch: match not complete"))
end
val ftvss = map (FTV) ts
in
completeTypMatch' ftvss ts
end
fun typeTyp pos D =
let
fun typeTyp' UnitTyp =
true
| typeTyp' StringTyp =
true
| typeTyp' (VarTyp a) =
inList a D
| typeTyp' (ArrTyp (t1, t2)) =
typeTyp' t1 andalso typeTyp' t2
| typeTyp' (ForAllTyp (tvs, t)) =
typeTyp pos (tvs::D) t
| typeTyp' (LabTyp (tvs, t)) =
typeTyp pos (tvs @ D) t
| typeTyp' (PCTyp (tvs, t)) =
typeTyp pos (tvs @ D) t
| typeTyp' AdvTyp =
true
| typeTyp' StackTyp =
true
| typeTyp' (TupleTyp ts) =
List.all (fn(t) => typeTyp' t) ts
| typeTyp' IntTyp =
true
| typeTyp' BoolTyp =
true
in
typeTyp'
end
fun typeExp pos D G =
let
fun typeExp' UnitExp =
UnitTyp
| typeExp' (StringExp s) =
StringTyp
| typeExp' (VarExp x) =
lookupVarG pos x G
| typeExp' (FunExp (x, t, e)) =
if typeTyp pos D t
then
let
val t' = typeExp pos D (VarG(x,t)::G) e
in
ArrTyp (t, t')
end
else type_err pos ("FunExp: bad type " ^ ppCTyp t)
| typeExp' (AppExp (e1, e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
case t1 of
ArrTyp (t1a, t2a) =>
if eq_typ t2 t1a
then t2a
else type_err pos ("AppExp: " ^ not_eq t1a t2)
| _ => type_err pos ("AppExp: not -> " ^ ppCTyp t1)
end
| typeExp' (TyFunExp (a,e)) =
let
val t = typeExp pos (a::D) G e
in
ForAllTyp(a,t)
end
| typeExp' (TyAppExp (e, t)) =
let
val t' = typeExp' e
in
if typeTyp pos D t
then case t' of
ForAllTyp(a,t'') => tysubstTyp t a t''
| _ => type_err pos ("TyAppExp: not forall " ^ ppCTyp t')
else type_err pos ("TyAppExp: bad type " ^ ppCTyp t)
end
| typeExp' (TupleExp es) =
TupleTyp(map (fn(e) => typeExp' e) es)
| typeExp' (LetExp(xs, e1, e2)) =
let
val t1 = typeExp' e1
(* val _ = print (ppCExp (LetExp(xs,e1,e2)) ^ "\n")*)
in
case t1 of
TupleTyp ts => typeExp pos D (letHelp pos G xs ts) e2
| _ => type_err pos ("LetExp: not tuple " ^ ppCTyp t1)
end
| typeExp' (LabExp l) =
let
val (tvs, t) = lookupLabG pos l G
in
LabTyp (tvs, t)
end
| typeExp' (PCExp (e1,ts,e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
if List.all (fn(t) => typeTyp pos D t) ts
then case t1 of
LabTyp (tvs, t) => if eq_typ t2 (tysubstTypList ts tvs t)
then t2
else type_err pos ("PCExp: " ^ not_eq t2 (tysubstTypList ts tvs t))
| _ => type_err pos ("PCTyp: not lab " ^ ppCTyp t1)
else type_err pos ("PCExp: bad type")
end
| typeExp' (NewExp (tvs, t, e)) =
let
val t' = typeExp' e
in
case t' of
LabTyp (tvs', t'') => if check_gen_typ pos D tvs' t'' tvs t
then LabTyp (tvs,t)
else type_err pos ("NewExp: doesn't generalize " ^ ppCTyp t'' ^ " and " ^ ppCTyp t)
| _ => type_err pos ("NewExp: not label " ^ ppCTyp t')
end
| typeExp' (AdvInstExp e) =
let
val t = typeExp' e
in
case t of
AdvTyp => UnitTyp
| _ => type_err pos ("AdvInstExp: not advice " ^ ppCTyp t)
end
| typeExp' (AdvExp (e1, tvs, x, t, e2)) =
let
val t1 = typeExp' e1
val (tvs', t') = case t1 of
PCTyp (tvs', t') => (tvs', t')
| _ => type_err pos ("AdvExp: not PCTyp " ^ ppCTyp t1)
val t2 = typeExp pos (tvs @ D) (VarG(x,t)::G) e2
val (tvs', t') = case t1 of
PCTyp (tvs', t') => (tvs', t')
| _ => type_err pos ("AdvExp: not PCTyp " ^ ppCTyp t1)
in
if eq_typ t t2
then (if check_gen_typ pos D tvs t tvs' t'
then AdvTyp
else type_err pos ("AdvExp: not gen " ^ ppCTyp t ^ " and " ^ ppCTyp t'))
else type_err pos ("AdvExp1: " ^ not_eq t t2)
end
| typeExp' (TypeCaseExp(a, t1, t2, tes)) =
if typeTyp pos (a::D) t1
then if typeTyp pos D t2
then let
val _ = if completeTypMatch pos (map #1 tes) then true else type_err pos "TypeCaseExp: incomplete type match"
val Ds = map (fn (ti) => subList (FTV ti) D) (map #1 tes)
val Ts = map (fn (ti) => (SOME (mgu t2 ti) handle MGUError => NONE)) (map #1 tes)
val tis' = map (fn (SOME Ti, Di, ei) =>
SOME (typeExp pos
(Di@D)
(tysubstGList (map #1 Ti) (map #2 Ti) G)
(tysubstExpList (map #1 Ti) (map #2 Ti) ei))
| (NONE, _, _) => NONE)
(zip3 Ts Ds (map #2 tes))
val tis'' = map (fn (SOME Ti, ti) =>
SOME (tysubstTypList
(map #1 Ti)
(map #2 Ti)
(tysubstTyp ti a t1))
| (NONE, _) => NONE)
(zip Ts (map #1 tes))
in
if List.all (fn (SOME ti', SOME ti'') => eq_typ ti' ti''
| (_, _) => true)
(zip tis' tis'')
then (if List.all (fn (Di, SOME Ts') => List.all (fn (r,d) => typeTyp pos (Di@D) r) Ts'
| (_, NONE) => true)
(zip Ds Ts)
then tysubstTyp t2 a t1
else type_err pos "TypeCheckExp: cod not closed")
else type_err pos "TypeCheckExp: not correct branch type"
end
else type_err pos ("TypeCaseExp: bad type " ^ ppCTyp t2)
else type_err pos ("TypeCaseExp: bad type " ^ ppCTyp t1)
| typeExp' (SetExp es) =
let
val ts = map typeExp' es
val tvsts = map (fn(t) => case t of LabTyp(tvs, t) => (tvs, t) | _ => type_err pos ("SetExp: bad type " ^ ppCTyp t) ) ts
val (tvs', t) = gen_list tvsts
in
PCTyp (tvs',t)
end
| typeExp' (UnionExp (e1, e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
case t1 of
LabTyp (tvs, t1') =>
(case t2 of
LabTyp (tvs', t2') => let
val (tvs'',t) = gen_list [(tvs,t1'),(tvs',t2')]
in
LabTyp (tvs'', t)
end
| _ => type_err pos ("UnionExp: bad type " ^ ppCTyp t1))
| _ => type_err pos ("UnionExp: bad type " ^ ppCTyp t1)
end
| typeExp' StackExp =
StackTyp
| typeExp' NilStExp =
StackTyp
| typeExp' (PtStExp (e1, ts, e2, e3)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
val t3 = typeExp' e3
in
if List.all (fn(t) => typeTyp pos D t) ts
then case t1 of
LabTyp (tvs, t) =>
if eq_typ t2 (tysubstTypList ts tvs t)
then if eq_typ t3 StackTyp
then StackTyp
else type_err pos ("PtStExp: " ^ not_eq t3 StackTyp)
else type_err pos ("PtStExp: " ^ not_eq t2 (tysubstTypList ts tvs t))
| _ => type_err pos( "PtStExp: not label " ^ ppCTyp t1)
else type_err pos "PtStExp: bad type "
end
| typeExp' (StoreExp (e1, ts, e2, e3)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
val t3 = typeExp' e3
in
if List.all (fn(t) => typeTyp pos D t) ts
then case t1 of
LabTyp (tvs, t) =>
if eq_typ t2 (tysubstTypList ts tvs t)
then t3
else type_err pos ("PtStExp: " ^ not_eq t2 (tysubstTypList ts tvs t))
| _ => type_err pos ("PtStExp: not label " ^ ppCTyp t1)
else type_err pos "PtStExp: bad type "
end
| typeExp' (StkCaseExp (e1, pes)) =
let
val t1 = typeExp' e1
val _ = if completePatMatch pos (map #1 pes) then true else type_err pos "StkCaseExp: incomplete stack match"
val ts = map (fn (pi,ei) => let
val (Di, Gi) = typePat pos D G pi
in
typeExp pos (Di@D) (Gi@G) ei
end)
pes
in
if eq_typ t1 StackTyp
then (join_typ_list ts handle (CoreError s) => type_err pos "StkCaseExp: cases not all eq")
else type_err pos ("StkCaseExp: " ^ not_eq t1 StackTyp)
end
| typeExp' (IntExp i) =
IntTyp
| typeExp' (PlusExp (e1, e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
if eq_typ t1 IntTyp
then (if eq_typ t2 IntTyp
then IntTyp
else type_err pos ("PlusExp: " ^ not_eq t2 IntTyp))
else type_err pos ("PlusExp: " ^ not_eq t1 IntTyp)
end
| typeExp' (BoolExp b) =
BoolTyp
| typeExp' (IfExp (e1, e2, e3)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
val t3 = typeExp' e3
in
if eq_typ t1 BoolTyp
then (join_typ t2 t3 handle (CoreError s) => type_err pos ("IfExp: " ^ not_eq t2 t3))
else type_err pos ("IfExp: " ^ not_eq t1 BoolTyp)
end
| typeExp' (PrintExp e) =
let
val t = typeExp' e
in
if eq_typ t StringTyp
then UnitTyp
else type_err pos ("PrintExp: " ^ not_eq t StringTyp)
end
| typeExp' (ItoSExp e) =
let
val t = typeExp' e
in
if eq_typ t IntTyp
then StringTyp
else type_err pos ("PrintExp: " ^ not_eq t StringTyp)
end
| typeExp' (ConcatExp (e1, e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
if eq_typ t1 StringTyp
then (if eq_typ t2 StringTyp
then t1
else type_err pos ("ConcatExp: " ^ not_eq t2 StringTyp))
else type_err pos ("ConcatExp: " ^ not_eq t1 StringTyp)
end
| typeExp' (FixExp (f, t, e)) =
let
val t' = typeExp pos D (VarG(f,t)::G) e
in
if eq_typ t' t
then t
else type_err pos ("FixExp: " ^ not_eq t' t)
end
| typeExp' AbortExp =
UnitTyp
| typeExp' (EqExp (e1, e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
if eq_typ t1 StringTyp
then (if eq_typ t2 StringTyp
then BoolTyp
else type_err pos ("EqExp: " ^ not_eq t2 StringTyp))
else type_err pos ("EqExp: " ^ not_eq t1 StringTyp)
end
| typeExp' (PosExp (pos', e)) =
typeExp pos' D G e
in
typeExp'
end
and typePat pos D G =
let
fun typePat' NilPat =
(nil, nil)
| typePat' (PtPat(e1, tvs, x, t, p)) =
let
val t1 = typeExp pos D G e1
val (tvs', t') = case t1 of
PCTyp (tvs', t') => (tvs', t')
| _ => type_err pos ("AdvExp: not PCTyp " ^ ppCTyp t1)
val (D',G') = typePat' p
in
if check_gen_typ pos D tvs t tvs' t'
then ((tvs @ D'), (VarG(x,t)::G'))
else type_err pos ("PtPat: not gen " ^ ppList (map ppTyvar tvs) ^ "." ^ ppCTyp t ^ " and " ^ ppList (map ppTyvar tvs') ^ "." ^ ppCTyp t')
end
| typePat' (VarPat x) =
(nil, [VarG(x,StackTyp)])
| typePat' (AllPat p) =
typePat' p
in
typePat'
end
and letHelp pos G (x::x') (t::t') =
letHelp pos ((VarG(x,t))::G) x' t'
| letHelp pos G nil nil =
G
| letHelp pos G (x::x') nil =
type_err pos ("letHelp: var " ^ ppVar x)
| letHelp pos G nil (t::t') =
type_err pos ("letHelp: type " ^ ppCTyp t)
and check_gen_typ pos D tvs1 t1 tvs2 t2=
if typeTyp pos (tvs1@D) t1
then (if typeTyp pos (tvs2@D) t2
then check_core_gen_typ tvs1 t1 tvs2 t2
else false)
else false
fun typeProg (Prog e) =
let
val a = Id.freshid "a"
in
((typeExp start_pos nil [LabG(U, [a], VarTyp a)] e);true)
handle (TypeError s) => (print ("Type Error: " ^ s ^ "\n"); false)
| (CoreError s) => (print ("Type Core Error: " ^ s ^ "\n"); false)
(* | (CoreDebugError (t1, t2)) => (print ("Type CoreDebug Error: " ^ ppCTyp t1 ^ " and " ^ ppCTyp t2 ^ "\n"); false)*)
end
end