-
Notifications
You must be signed in to change notification settings - Fork 1
/
typecheck.sml
584 lines (558 loc) · 13.9 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
structure TypeCheck :> TYPECHECK =
struct
open Core ProtDom 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 typeExp pos latt G p =
let
fun typeExp' UnitExp =
UnitTyp
| typeExp' (StringExp s) =
StringTyp
| typeExp' TrueExp =
BoolTyp
| typeExp' FalseExp =
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 if eq_typ t2 t3
then t2
else type_err pos ("IfExp " ^ not_eq t2 t3)
else type_err pos ("IfExp " ^ not_eq t2 t3)
end
| typeExp' (FunExp(p',var,typ,exp)) =
ArrTyp (typ, p', typeExp pos latt (VarG(var,typ)::G) p' exp)
| typeExp' (LabExp l) =
let
val (t,p') = lookupLabG l G
in
LabTyp(t,p')
end
| typeExp' (RefExp r) =
let
val (t,p') = lookupRefG r G
in
RefTyp(t,p')
end
| typeExp' (ObjExp mlist) =
let
val objtyp = ObjTyp (map
(fn(m,t,p',x,e) =>
(m,p',t))
mlist)
in
(app (fn(m,t,p',x,e) =>
let
val t' = typeExp pos latt (VarG(x,objtyp)::G) p' e
in
if (eq_typ t' t)
then ()
else type_err pos ("ObjExp " ^ not_eq t' t)
end)
mlist);
objtyp
end
| typeExp' (VarExp x) =
lookupVarG x G
| typeExp' (SeqExp(e1,e2)) =
let
val t1 = typeExp' e1
in
(*if eq_typ t1 UnitTyp
then *)typeExp' e2
(*else type_err pos ("SeqExp " ^ not_eq t1 UnitTyp)*)
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' (AppExp(e1, e2)) =
(case typeExp' e1 of
ArrTyp (t1,p',t2) =>
let
val t2' = typeExp' e2
in
if eq_typ t1 t2'
then if eq_prot p p'
then t2
else type_err pos "AppExp: bad protection domain"
else type_err pos ("AppExp " ^ not_eq t1 t2')
end
| _ => raise TypeError "AppExp3")
| typeExp' (AdvExp(e1, x, p'', e2)) =
(case typeExp' e1 of
PCTyp(t,p') =>
let
val t2 = typeExp pos latt (VarG(x,t)::G) p'' e2
in
if eq_typ t2 UnitTyp
then if leq_prot latt p'' p'
then AdvTyp p''
else type_err pos "AdvExp: bad protectoin domain"
else type_err pos ("AdvExp: " ^ not_eq t2 UnitTyp)
end
| t => type_err pos ("AdvExp: Not pc - " ^ ppCTyp t))
| typeExp' (AdvInstExp e) =
(case typeExp' e of
AdvTyp p' => if (leq_prot latt p' p)
then UnitTyp
else type_err pos "AdvInstExp: bad protection domains"
| t => type_err pos ("AdvInstExp2: " ^ ppCTyp t ^ " not AdvTyp"))
| typeExp' (NewExp(p', t)) =
LabTyp(t,p')
| typeExp' (PCExp(e1,e2)) =
(case typeExp' e1 of
LabTyp(t,p') =>
let
val t2 = typeExp' e2
in
if eq_typ t2 t
then if leq_prot latt p' p
then UnitTyp
else type_err pos "PCExp: bad protection domains"
else type_err pos ("PCExp: " ^ not_eq t2 t)
end
| _ => raise TypeError "PCExp3")
| typeExp' (NewRefExp(p',e)) =
(if (leq_prot latt p' p)
then RefTyp(typeExp' e, p')
else type_err pos "NewRefExp")
| typeExp' (DerefExp e) =
(case typeExp' e of
RefTyp(t,p') => if (leq_prot latt p p')
then t
else type_err pos "DerefExp1"
| _ => type_err pos "DerefExp e")
| typeExp' (AssignExp(e1,e2)) =
(case typeExp' e1 of
RefTyp(t,p') => if (eq_typ (typeExp' e2) t)
andalso (leq_prot latt p' p)
then t
else type_err pos "AssignExp1"
| _ => type_err pos "AssignExp2")
| typeExp' (LowerExp(p',e)) =
let
val typ = typeExp pos latt G p' e
in
(if (eq_typ typ UnitTyp)
andalso (leq_prot latt p' p)
then UnitTyp
else type_err pos "LowerExp")
end
| typeExp' (TupleExp elist) =
TupleTyp(map (fn(e) => typeExp' e) elist)
| typeExp' (SplitExp(xlist, e1, e2)) =
let
val tuptyp = typeExp' e1
in
case tuptyp of
TupleTyp tlist => typeExp pos latt (splitHelp G xlist tlist) p e2
| _ => raise TypeError "SplitExp"
end
| typeExp' (LabSetExp(elist,p')) =
(let
val tlist = map typeExp' elist
val (plist,t) = extractLab(tlist)
in
(checkP latt plist p'); PCTyp(t,p')
end)
| typeExp' (UnionExp(e1,p',e2)) =
(case (typeExp' e1, typeExp' e2) of
(PCTyp(t1,p1),PCTyp(t2,p2)) =>
if (eq_typ t1 t2)
andalso (leq_prot latt p' p1)
andalso (leq_prot latt p' p2)
then PCTyp(t1,p')
else type_err pos "UnionExp1"
| _ => type_err pos "UnionExp2")
| typeExp' (MembExp(e,m)) =
let
val objtyp = typeExp' e
in
case objtyp of
ObjTyp(mlist) => membHelp p mlist m
|_ => type_err pos "MembExp"
end
| typeExp' StackExp =
StackTyp
| typeExp' NilStExp =
StackTyp
| typeExp' (PtStExp(e1, e2, e3)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
val t3 = typeExp' e3
in
case (t1,t3) of
(LabTyp(t,p'),StackTyp) =>
if (eq_typ t t2)
then StackTyp
else type_err pos "PtStExp1"
| _ => type_err pos "PtStExp2"
end
| typeExp' (StoreExp(e1, e2, e3)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
val t3 = typeExp' e3
in
case t1 of
LabTyp(t,p') =>
if (eq_typ t t2)
then t3
else type_err pos "StoreExp1"
| _ => type_err pos "StoreExp2"
end
| typeExp' (CaseExp(e1, pat, e2, e3)) =
let
val t1 = typeExp' e1
val G' = typePat pos latt G p pat
val G'' = List.concat [G', G]
val t2 = typeExp pos latt G'' p e2
val t3 = typeExp' e3
in
if eq_typ t1 StackTyp
andalso eq_typ t2 t3
then t2
else type_err pos "CaseExp"
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' (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' (EqExp (e1, e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
case (t1, t2) of
(StringTyp, StringTyp) => BoolTyp
| (IntTyp, IntTyp) => BoolTyp
| (BoolTyp, BoolTyp) => BoolTyp
| _ => type_err pos ("EqExp: " ^ not_eq t1 t2)
end
| typeExp' (GTExp (e1, e2)) =
let
val t1 = typeExp' e1
val t2 = typeExp' e2
in
if eq_typ t1 IntTyp
then (if eq_typ t2 IntTyp
then BoolTyp
else type_err pos ("GTExp: " ^ not_eq t2 IntTyp))
else type_err pos ("GTExp: " ^ not_eq t1 IntTyp)
end
| typeExp' (AbortExp e) =
let
val t = typeExp' e
in
if eq_typ t UnitTyp
then UnitTyp
else type_err pos ("AbortExp: " ^ not_eq t UnitTyp)
end
| typeExp' (IntExp i) =
IntTyp
| 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' (PosExp (pos', e)) =
typeExp pos' latt G p e
| typeExp' (SocketExp (st, e)) =
let
val t = typeExp' e
in
if eq_typ t UnitTyp
then SockTyp
else type_err pos ("SocketExp: " ^ not_eq t UnitTyp)
end
| typeExp' (BindExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [SockTyp, StringTyp, IntTyp])
then UnitTyp
else type_err pos ("BindExp: " ^ not_eq t (TupleTyp [SockTyp, StringTyp, IntTyp]))
end
| typeExp' (ListenExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [SockTyp, IntTyp])
then UnitTyp
else type_err pos ("ListenExp: " ^ not_eq t (TupleTyp [SockTyp, IntTyp]))
end
| typeExp' (AcceptExp e) =
let
val t = typeExp' e
in
if eq_typ t SockTyp
then (TupleTyp [SockTyp, StringTyp, IntTyp])
else type_err pos ("AcceptExp: " ^ not_eq t SockTyp)
end
| typeExp' (ConnectExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [SockTyp, StringTyp, IntTyp])
then UnitTyp
else type_err pos ("ConnectExp: " ^ not_eq t (TupleTyp [SockTyp, StringTyp, IntTyp]))
end
| typeExp' (SendExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [SockTyp, StringTyp])
then IntTyp
else type_err pos ("SendExp: " ^ not_eq t (TupleTyp [SockTyp, StringTyp]))
end
| typeExp' (RecvExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [SockTyp, IntTyp])
then StringTyp
else type_err pos ("RecvExp: " ^ not_eq t (TupleTyp [SockTyp, IntTyp]))
end
| typeExp' (SockExp _) =
SockTyp
| typeExp' (NowExp e) =
let
val t = typeExp' e
in
if eq_typ t UnitTyp
then IntTyp
else type_err pos ("NowExp: " ^ not_eq t UnitTyp)
end
| typeExp' (OpenExp (ot,e)) =
let
val t = typeExp' e
in
if eq_typ t StringTyp
then FileTyp
else type_err pos ("OpenExp: " ^ not_eq t StringTyp)
end
| typeExp' (WriteExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [FileTyp, StringTyp])
then IntTyp
else type_err pos ("WriteExp: " ^ not_eq t (TupleTyp [FileTyp, StringTyp]))
end
| typeExp' (ReadExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [FileTyp, IntTyp])
then StringTyp
else type_err pos ("ReadExp: " ^ not_eq t (TupleTyp [FileTyp, IntTyp]))
end
| typeExp' (DeleteExp e) =
let
val t = typeExp' e
in
if eq_typ t StringTyp
then UnitTyp
else type_err pos ("DeleteExp: " ^ not_eq t StringTyp)
end
| typeExp' (RenameExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [StringTyp, StringTyp])
then UnitTyp
else type_err pos ("RenameExp: " ^ not_eq t (TupleTyp [StringTyp, StringTyp]))
end
| typeExp' (FileExp _) =
FileTyp
| typeExp' (SizeExp e) =
let
val t = typeExp' e
in
if eq_typ t StringTyp
then IntTyp
else type_err pos ("SizeExp: " ^ not_eq t StringTyp)
end
| typeExp' (SleepExp e) =
let
val t = typeExp' e
in
if eq_typ t IntTyp
then UnitTyp
else type_err pos ("SleepExp: " ^ not_eq t IntTyp)
end
| typeExp' (IndexOfExp (iot, e)) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [StringTyp, StringTyp])
then IntTyp
else type_err pos ("IndexOfExp: " ^ not_eq t (TupleTyp [StringTyp, StringTyp]))
end
| typeExp' (SubstringExp e) =
let
val t = typeExp' e
in
if eq_typ t (TupleTyp [StringTyp, IntTyp, IntTyp])
then StringTyp
else type_err pos ("SubstringExp: " ^ not_eq t (TupleTyp [StringTyp, IntTyp, IntTyp]))
end
| typeExp' (ExistsExp e) =
let
val t = typeExp' e
in
if eq_typ t StringTyp
then BoolTyp
else type_err pos ("ExistsExp: " ^ not_eq t StringTyp)
end
| typeExp' (MultExp (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 ("MultExp1: " ^ not_eq t2 IntTyp))
else type_err pos ("MultExp2: " ^ not_eq t1 IntTyp)
end
| typeExp' (DivExp (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 ("DivExp1: " ^ not_eq t2 IntTyp))
else type_err pos ("DivExp2: " ^ not_eq t1 IntTyp)
end
| typeExp' (NegExp e) =
let
val t = typeExp' e
in
if eq_typ t IntTyp
then IntTyp
else (if eq_typ t BoolTyp
then BoolTyp
else type_err pos ("NegExp: " ^ not_eq t BoolTyp))
end
in
typeExp'
end
and typePat pos latt G p =
let
fun typePat' NilPat =
nil
| typePat' (PtPat(e,x,pat)) =
let
val t = typeExp pos latt G p e
val G' = typePat' pat
in
case t of
PCTyp(t',p) => VarG(x,t')::G'
| _ => type_err pos "PtPat"
end
| typePat' (AllPat pat) =
typePat' pat
| typePat' (VarPat x) =
[VarG(x,StackTyp)]
in
typePat'
end
and splitHelp G (x::x') (t::t') =
splitHelp ((VarG(x,t))::G) x' t'
| splitHelp G nil nil =
G
| splitHelp _ _ _ =
raise TypeError "splitHelp"
and membHelp p ((m',p',t)::tail) m =
if (Id.eqid m m')
then (if (eq_prot p p')
then t
else raise TypeError "membHelp1")
else membHelp p tail m
| membHelp _ nil _ =
raise TypeError "membHelp2"
and extractLab (LabTyp(t,p)::tail) =
(p::(extractLabHelper tail t),t)
| extractLab _ = raise TypeError "extractLab"
and extractLabHelper (LabTyp(t,p)::tail) t' =
if (eq_typ t t')
then p::(extractLabHelper tail t')
else raise TypeError "extractLabHelper1"
| extractLabHelper nil _ =
nil
| extractLabHelper _ _ =
raise TypeError "extractLabHelper2"
and checkP latt (p::plist) p' =
if (leq_prot latt p' p)
then checkP latt plist p'
else raise TypeError "checkP"
| checkP latt nil p' =
()
(* label -> pc *)
and lookupVarG x (VarG(y,t)::G) =
if (Id.eqid x y)
then t
else lookupVarG x G
| lookupVarG x (_::G) =
lookupVarG x G
| lookupVarG x nil =
raise TypeError "lookupVarG"
and lookupLabG x (LabG(y,t,p)::G) =
if (x = y)
then (t,p)
else lookupLabG x G
| lookupLabG x (_::G) =
lookupLabG x G
| lookupLabG x nil =
raise TypeError "lookupLabG"
and lookupRefG x (RefG(y,t,p)::G) =
if (x = y)
then (t,p)
else lookupRefG x G
| lookupRefG x (_::G) =
lookupRefG x G
| lookupRefG x nil =
raise TypeError "lookupRefG"
fun typecheck latt e =
((typeExp start_pos latt nil (Id.makeid "main") e);
true)
handle (TypeError s) => (print ("Type Error: " ^ s ^ "\n");false)
end