-
Notifications
You must be signed in to change notification settings - Fork 3
/
el-elaboration.sml
1530 lines (1482 loc) · 69.1 KB
/
el-elaboration.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* MixML prototype implementation
*
* Based on: Derek Dreyer, Andreas Rossberg, "Mixin' Up the ML Module System"
*
* (c) 2007-2008 Andreas Rossberg
*)
signature EL_ELABORATION =
sig
type context = ELAux.typ_context * ELAux.modl_context
val emptyContext : context
val elab : context -> EL.prog -> ELAux.funct * IL.modl
end
structure ELElaboration : EL_ELABORATION =
struct
open VarOps infix |-> |=> |=>* ++ -- || \/ /\ \
open ILOps
open ELAux infix at
open ELTrace
(* Auxiliaries *)
datatype pass = Stat | Dyn
val dummyP = IL.VarM(rename "_dummy")
val dummyE = IL.IntE(666)
val dummyM = IL.StructM[]
val dummyS = Struct[]
fun renamesAKls alphaks ls =
let
val (alphaks', del) = renamesAK alphaks
val prefix = if List.null ls then "" else String.concatWith "." ls ^ "."
val alphaks'' = mapFst (fn alpha => prefix ^ alpha) alphaks'
val del' = mapRan (fn IL.VarT(alpha) => IL.VarT(prefix ^ alpha) | tau => tau) del
in
(alphaks'', del')
end
fun sort lsigmas = entries(map lsigmas)
fun asStruct(Struct(lsigmas)) = lsigmas
| asStruct _ = raise Fail "asStruct"
fun asTyp(Typ(tau, k, pmo)) = (tau, k, pmo)
| asTyp _ = raise Fail "asTyp"
fun asAbsTyp(Typ(IL.VarT(alpha), _, _)) = alpha
| asAbsTyp _ = raise Fail "asAbsTyp"
(* Solving *)
exception Circular of IL.typvar
fun solve delta (sigma1, sigma2) =
let
val _ = (traceIn(); trace' "solve" "begin"; traceD "delta" delta;
traceS "sigma1" sigma1; traceS "sigma2" sigma2)
val del = solve_ delta (sigma1, sigma2)
in
trace' "solve" "end"; traceR "del" del; traceOut();
del
end
and solve_ delta (sigma1, sigma2) =
let
val loc1 = locator Minus sigma1 -- dom(locator Minus sigma2)
val loc2 = locator Minus sigma2 -- dom(locator Minus sigma1)
val _ = (trace' "solve" "1"; traceL "loc1" loc1; traceL "loc2" loc2)
val eqs = composePartial sigma1 loc2 ++ composePartial sigma2 loc1
val _ = (trace' "solve" "2"; traceR "eqs" eqs)
in
solve' delta (mapRan (normT delta) eqs, map[])
end
and solve' delta (eqs, del) =
if isId eqs then del else
case Map.entries(Map.filter (fn(alpha, tau) => not(member (fvT(tau)) alpha)) eqs) of
[] => raise Circular(#1(List.hd(entries eqs)))
| (alpha, tau)::_ =>
let
val del' = map[alpha |-> tau]
val eqs' =
mapRan (normT delta o substT del') (eqs -- set[alpha])
in
solve' delta (eqs', mapRan (substT del') del ++ del')
end
(* Merging and matching *)
exception Mismatch of EL.labs
fun merge pass delta (sigma1, sigma2) p =
case (sigma1, sigma2) of
(Struct[], _) =>
(sigma2, IL.StructM[], p)
| (_, Struct[]) =>
(sigma1, p, IL.StructM[])
| (Typ(tau1, k1, pmo), Typ(tau2, k2, SOME Minus)) =>
(*if not(k1 = k2 andalso equivT delta (tau1, tau2)) then raise Mismatch [] else*)
(Typ(tau1, k1, pmo), p, p)
| (Typ(tau1, k1, SOME Minus), Typ(tau2, k2, pmo)) =>
(*if not(k1 = k2 andalso equivT delta (tau1, tau2)) then raise Mismatch [] else*)
(Typ(tau2, k2, pmo), p, p)
| (Typ(tau1, k1, pmo as (NONE | SOME Plus)), Typ(tau2, k2, NONE)) =>
if not(k1 = k2 andalso equivT delta (tau1, tau2)) then raise Mismatch [] else
(Typ(tau1, k1, pmo), p, p)
| (Typ(tau1, k1, NONE), Typ(tau2, k2, pmo as SOME Plus)) =>
if not(k1 = k2 andalso equivT delta (tau1, tau2)) then raise Mismatch [] else
(Typ(tau2, k2, pmo), p, p)
| (Term(tau1, Plus), Term(tau2, Minus)) =>
let
val (_, f) =
if pass = Stat then (map[], dummyE) else
matchT (delta, set[]) (tau1, tau2)
in
(Term(tau1, Plus),
p, ILOps.lazyM(IL.TermM(IL.ApplyE(f, IL.ForceM(p))), IL.TermS(tau2)))
end
| (Term(tau1, Minus), Term(tau2, Plus)) =>
let
val (_, f) =
if pass = Stat then (map[], dummyE) else
matchT (delta, set[]) (tau2, tau1)
in
(Term(tau2, Plus),
ILOps.lazyM(IL.TermM(IL.ApplyE(f, IL.ForceM(p))), IL.TermS(tau1)), p)
end
| (Term(tau1, Minus), Term(tau2, Minus)) =>
let
val (tau, m1, m2) =
let
val (_, f) = matchT (delta, set[]) (tau1, tau2)
in
(tau1,
p, ILOps.lazyM(IL.TermM(IL.ApplyE(f, IL.ForceM(p))), IL.TermS(tau2)))
end
handle Mismatch _ =>
let
val (_, f) = matchT (delta, set[]) (tau2, tau1)
in
(tau2,
ILOps.lazyM(IL.TermM(IL.ApplyE(f, IL.ForceM(p))), IL.TermS(tau1)), p)
end
handle Mismatch ls => raise Mismatch ls
in
(Term(tau, Minus), m1, m2)
end
| (Funct(phi1, Plus), Funct(phi2, Minus)) =>
let
val f = matchF pass delta (phi1, phi2)
handle Mismatch ls => raise Mismatch ls
in
(Funct(phi1, Plus), p, ILOps.lazyM(IL.ApplyM(f, IL.ForceM(p)), eraseF(phi2)))
end
| (Funct(phi1, Minus), Funct(phi2, Plus)) =>
let
val f = matchF pass delta (phi2, phi1)
handle Mismatch ls => raise Mismatch ls
in
(Funct(phi2, Plus), ILOps.lazyM(IL.ApplyM(f, IL.ForceM(p)), eraseF(phi1)), p)
end
| (Funct(phi1, Minus), Funct(phi2, Minus)) =>
let
(*
val (phi, m1, m2) =
let
val f = matchF pass delta (phi1, phi2)
in
(phi1, p, ILOps.lazyM(IL.ApplyM(f, IL.ForceM(p)), eraseF(phi2)))
end
handle Mismatch _ =>
let
val f = matchF pass delta (phi2, phi1)
in
(phi2, ILOps.lazyM(IL.ApplyM(f, IL.ForceM(p)), eraseF(phi1)), p)
end
handle Mismatch ls => raise Mismatch ls
in
(Funct(phi, Minus), m1, m2)
*)
val () = if equivF delta (phi1, phi2) then () else raise Mismatch []
in
(Funct(phi1, Minus), p, p)
end
| (Struct(lsigmas1), Struct(lsigmas2)) =>
let
val (lsigmas, lms1, lms2) = merge' pass delta (sort lsigmas1, lsigmas2) p
in
(Struct(lsigmas), IL.StructM(lms1), IL.StructM(lms2))
end
| _ => raise Mismatch []
and merge' pass delta (lsigmas1, lsigmas2) p =
case (lsigmas1, lsigmas2) of
([], []) =>
([], [], [])
| ((l, sigma)::lsigmas1', []) =>
let
val (lsigmas, lms1, lms2) = merge' pass delta (lsigmas1', lsigmas2) p
in
((l, sigma)::lsigmas, (l, IL.DotM(p, l))::lms1, lms2)
end
| ([], (l, sigma)::lsigmas2') =>
let
val (lsigmas, lms1, lms2) = merge' pass delta (lsigmas1, lsigmas2') p
in
((l, sigma)::lsigmas, lms1, (l, IL.DotM(p, l))::lms2)
end
| ((l1, sigma1)::lsigmas1', (l2, sigma2)::lsigmas2') =>
case Var.compare(l1, l2) of
LESS =>
let
val (lsigmas', lms1, lms2) = merge' pass delta (lsigmas1', lsigmas2) p
in
((l1, sigma1)::lsigmas', (l1, IL.DotM(p, l1))::lms1, lms2)
end
| GREATER =>
let
val (lsigmas', lms1, lms2) = merge' pass delta (lsigmas1, lsigmas2') p
in
((l2, sigma2)::lsigmas', lms1, (l2, IL.DotM(p, l2))::lms2)
end
| EQUAL =>
let
val l = l1
val (sigma, m1, m2) = merge pass delta (sigma1, sigma2) (IL.DotM(p, l))
handle Mismatch ls => raise Mismatch(l::ls)
val (lsigmas', lms1', lms2') = merge' pass delta (lsigmas1', lsigmas2') p
in
((l, sigma)::lsigmas', (l, m1)::lms1', (l, m2)::lms2')
end
and matchF pass delta ((alphaks1, betaks1, sigma1), (alphaks2, betaks2, sigma2)) =
let
(* todo: rename? *)
val del = solve (delta ++ map(alphaks1) ++ map(betaks1) ++ map(alphaks2) ++ map(betaks2)) (sigma1, neg sigma2)
handle Circular alpha => raise Mismatch [alpha]
| Locate ls => raise Mismatch ls
val f = matchS pass delta (substS del sigma1, substS del sigma2)
val g = rename "_matchF.G"
val x2 = rename "_matchF.X2"
in
IL.LambdaM(g, eraseF(alphaks1, betaks1, sigma1),
IL.GenDownM(alphaks2, IL.GenUpM(betaks2,
IL.LambdaM(x2, eraseS(sigma2),
IL.NewTypM(betaks1,
ILOps.defEquiM(List.map (fn(beta2, k) =>
(beta2, substT del (IL.VarT(beta2)))) betaks2,
IL.ApplyM(
IL.ApplyM(
f,
IL.InstUpM(
IL.InstDownM(
IL.VarM(g),
List.map (substT del o IL.VarT o #1) alphaks1
),
List.map #1 betaks1
)
),
IL.VarM(x2)
),
IL.StructS[]
)
)
)
))
)
end
and matchS pass delta (sigma1, sigma2) =
let
val x = rename "_matchS.X"
val f = rename "_matchS.F"
val x2 = rename "_matchS.X2"
val x2' = rename "_matchS.X2'"
val (sigma, m1, m2) = merge pass delta (sigma1, neg sigma2) (IL.VarM(x))
in
IL.LambdaM(f, IL.ArrowS(eraseS(sigma1), IL.StructS[]),
IL.LambdaM(x2, eraseS(sigma2),
IL.LetM(x, create(sigma),
IL.LetM(x2', m2,
ILOps.seqM[
copy(IL.VarM(x2'), IL.VarM(x2), sigma2),
IL.ApplyM(IL.VarM(f), m1)
]
)
)
)
)
end
and matchT (delta, alphas) (tau1, tau2) =
let
(* todo: match polymorphic types *)
val x = rename "_matchT.X"
in
if equivT delta (tau1, tau2)
then (map[], IL.LambdaE(x, IL.TermS(tau1), IL.ValE(IL.VarM(x))))
else raise Mismatch []
end
and matchT_ (delta, alphas) (tau1, tau2) =
case (tau1, tau2) of
(IL.UnivT(alphas1, tau1'), IL.UnivT(alphas2, tau2')) =>
let
val (alphas1', del1) = renamesA alphas1
val (alphas2', del2) = renamesA alphas2
val (del, f) =
matchT (delta ++ map(alphas1' |=>* IL.StarK) ++ map(alphas2' |=>* IL.StarK), alphas \/ set(alphas1')) (substT del1 tau1', substT del2 tau2')
val x = rename "_matchT.X"
in
(del,
IL.LambdaE(x, IL.TermS(tau1),
IL.GenE(alphas2',
IL.ApplyE(f,
IL.TermM(
IL.InstE(IL.ValE(IL.VarM(x)),
List.map (substT del o IL.VarT) alphas1'
)
)
)
)
)
)
end
| (IL.UnivT(alphas1, tau1'), _) =>
let
val (alphas1', del1) = renamesA alphas1
val (del, f) = matchT (delta ++ map(alphas1' |=>* IL.StarK), alphas \/ set(alphas1')) (substT del1 tau1', tau2)
val x = rename "_matchT.X"
in
(del,
IL.LambdaE(x, IL.TermS(tau1),
IL.ApplyE(f,
IL.TermM(
IL.InstE(IL.ValE(IL.VarM(x)),
List.map (substT del o IL.VarT) alphas1'
)
)
)
)
)
end
| (_, IL.UnivT([], tau2')) =>
let
val (del, f) = matchT (delta, alphas) (tau1, tau2')
val x = rename "_matchT.X"
in
(del,
IL.LambdaE(x, IL.TermS(tau1),
IL.ApplyE(f,
IL.TermM(
IL.GenE([], IL.ValE(IL.VarM(x)))
)
)
)
)
end
| (_, _) =>
let
val (del, alphas') = unify (delta, map[], alphas) (tau1, tau2)
val x = rename "_unify.X"
in
(del,
IL.LambdaE(x, IL.TermS(tau1), IL.ValE(IL.VarM(x)))
)
end
and unify (delta, del, alphas) (tau1, tau2) =
let
(* todo: match polymorphic types *)
val x = rename "_matchT.X"
in
if equivT delta (tau1, tau2)
then (map[], set[])
else raise Mismatch []
end
(* Kinds *)
fun elabK {it, region} =
case it of
EL.StarK => IL.StarK
| EL.ArrowK(n) => IL.ArrowK(n)
(* Types *)
fun elabT (delta, gamma) {it, region} =
case it of
(*
EL.VarT(alpha) =>
let
val k = lookup delta alpha
handle Lookup => raise EL.Error(region, "unbound type variable " ^ alpha)
in
(IL.VarT(alpha), k)
end
*)
EL.ModT(modl) =>
let
val (sigma, _) = elabMclosed Stat (delta, gamma) modl
val (tau, k) =
(case sigma at ["type"] of
Typ(tau, k, pmo) => (tau, k)
| _ => raise EL.Error(region, "module is not a type")
) handle At => raise EL.Error(region, "module is not a type")
in
(tau, k)
end
| EL.IntT =>
(IL.IntT, IL.StarK)
| EL.StringT =>
(IL.StringT, IL.StarK)
| EL.TupleT(typs) =>
let
val taus = List.map (fn typ => elabT' (delta, gamma) typ) typs
in
(IL.TupleT(taus), IL.StarK)
end
| EL.VariantT(typs) =>
let
val taus = List.map (fn typ => elabT' (delta, gamma) typ) typs
in
(IL.VariantT(taus), IL.StarK)
end
| EL.ArrowT(typ1, typ2) =>
let
val tau1 = elabT' (delta, gamma) typ1
val tau2 = elabT' (delta, gamma) typ2
in
(IL.ArrowT(IL.LazyS(IL.TermS(tau1)), tau2), IL.StarK)
end
| EL.UnivT(alphas, typ) =>
let
val tau = elabT' (delta ++ map(alphas |=>* IL.StarK),
gamma ++ map(List.map (fn alpha => (alpha, typstruct(IL.VarT(alpha), IL.StarK, NONE))) alphas)) typ
in
(IL.UnivT(alphas, tau), IL.StarK)
end
| EL.LambdaT(alphas, typ) =>
let
val tau = elabT' (delta ++ map(alphas |=>* IL.StarK),
gamma ++ map(List.map (fn alpha => (alpha, typstruct(IL.VarT(alpha), IL.StarK, NONE))) alphas)) typ
in
(IL.LambdaT(alphas, tau), IL.ArrowK(List.length alphas))
end
| EL.ApplyT(typ, typs) =>
let
val (tau, n) =
case elabT (delta, gamma) typ of
(tau, IL.ArrowK(n)) => (tau, n)
| (tau, IL.StarK) => raise EL.Error(#region typ, "not a type constructor")
val taus = List.map (fn typ_i => elabT' (delta, gamma) typ_i) typs
in
case Int.compare(List.length taus, n) of
EQUAL => (IL.ApplyT(tau, taus), IL.StarK)
| LESS => raise EL.Error(region, "too few arguments in type application")
| GREATER => raise EL.Error(region, "too many arguments in type application")
end
and elabT' (delta, gamma) typ =
case elabT (delta, gamma) typ of
(tau, IL.StarK) => normT delta tau
| (tau, IL.ArrowK(n)) => raise EL.Error(#region typ, "type not ground")
(* Expressions *)
and elabE (delta, gamma) {it, region} =
case it of
EL.ModE(modl) =>
let
val (tau, m) =
case elabMclosed Dyn (delta, gamma) modl of
(Term(tau, pm), m) => (tau, m)
| _ => raise EL.Error(region, "module is not a value")
in
(tau, IL.ValE(IL.ForceM(m)))
end
| EL.IntE(n) =>
let
in
(IL.IntT, IL.IntE(n))
end
| EL.StringE(s) =>
let
in
(IL.StringT, IL.StringE(s))
end
| EL.PlusE(exp1, exp2) =>
let
val e1 = elabE' (delta, gamma) exp1 IL.IntT "addition operand"
val e2 = elabE' (delta, gamma) exp2 IL.IntT "addition operand"
in
(IL.IntT, IL.PlusE(e1, e2))
end
| EL.MinusE(exp1, exp2) =>
let
val e1 = elabE' (delta, gamma) exp1 IL.IntT "subtraction operand"
val e2 = elabE' (delta, gamma) exp2 IL.IntT "subtraction operand"
in
(IL.IntT, IL.MinusE(e1, e2))
end
| EL.EqualE(exp1, exp2) =>
let
val e1 = elabE' (delta, gamma) exp1 IL.IntT "comparison operand"
val e2 = elabE' (delta, gamma) exp2 IL.IntT "comparison operand"
in
(IL.VariantT[IL.TupleT[], IL.TupleT[]], IL.EqualE(e1, e2))
end
| EL.LessE(exp1, exp2) =>
let
val e1 = elabE' (delta, gamma) exp1 IL.IntT "comparison operand"
val e2 = elabE' (delta, gamma) exp2 IL.IntT "comparison operand"
in
(IL.VariantT[IL.TupleT[], IL.TupleT[]], IL.LessE(e1, e2))
end
| EL.CatE(exp1, exp2) =>
let
val e1 = elabE' (delta, gamma) exp1 IL.StringT "concatenation operand"
val e2 = elabE' (delta, gamma) exp2 IL.StringT "concatenation operand"
in
(IL.StringT, IL.CatE(e1, e2))
end
| EL.TupleE(exps) =>
let
val (taus, es) = ListPair.unzip(List.map (fn exp => elabE (delta, gamma) exp) exps)
in
(IL.TupleT(taus), IL.TupleE(es))
end
| EL.ProjE(exp, i) =>
let
val (taus, e) =
case elabE (delta, gamma) exp of
(IL.TupleT(taus), e) => (taus, e)
| _ => raise EL.Error(region, "expression is not a tuple")
val tau_i = List.nth(taus, i-1)
handle Subscript => raise EL.Error(region, "index out of range")
in
(tau_i, IL.DotE(e, i))
end
| EL.InjE(exp, i, typ) =>
let
val taus =
case elabT' (delta, gamma) typ of
IL.VariantT(taus) => taus
| _ => raise EL.Error(region, "type is not a variant")
val tau_i = List.nth(taus, i-1)
handle Subscript => raise EL.Error(region, "index out of range")
val e = elabE' (delta, gamma) exp tau_i "variant constructor"
in
(IL.VariantT(taus), IL.VariantE(e, i, IL.VariantT(taus)))
end
| EL.CaseE(exp, xexps) =>
let
val (taus, e) =
case elabE (delta, gamma) exp of
(IL.VariantT(taus), e) => (taus, e)
| _ => raise EL.Error(region, " not a variant")
val (tau, xes) =
(case ListPair.mapEq
(fn((x, exp), tau) =>
(x, elabE (delta, gamma ++ map[x |-> Term(tau, Plus)]) exp))
(xexps, taus) of
[] => (IL.VariantT[], [])
| (x, (tau, e))::xtaues =>
if List.all (fn(x, (tau', e)) => equivT delta (tau, tau')) xtaues
then (tau, (x, e) :: List.map (fn(x, (tau, e)) => (x, e)) xtaues)
else raise EL.Error(region, "inconsistent branch types")
) handle ListPair.UnequalLengths => raise EL.Error(region, "wrong number of branches")
in
(tau, IL.CaseE(e, xes))
end
| EL.LambdaE(x, typ, exp) =>
let
val tau1 = elabT' (delta, gamma) typ
val (tau2, e) = elabE (delta, gamma ++ map[x |-> Term(tau1, Plus)]) exp
in
(IL.ArrowT(IL.LazyS(IL.TermS(tau1)), tau2),
IL.LambdaE(x, IL.LazyS(IL.TermS(tau1)), e))
end
| EL.ApplyE(exp1, exp2) =>
let
val ((tau2, tau), e1) =
case elabE (delta, gamma) exp1 of
(IL.ArrowT(IL.LazyS(IL.TermS(tau2)), tau), e1) => ((tau2, tau), e1)
| _ => raise EL.Error(#region exp1, "expression is not a function")
val e2 = elabE' (delta, gamma) exp2 tau2 "function argument"
val x = rename "_apply.X"
in
(tau,
IL.ApplyE(e1, IL.LetM(x, IL.TermM(e2), ILOps.lazyM(IL.VarM(x), IL.TermS(tau2)))))
end
| EL.GenE(alphas, exp) =>
let
val sigmas = List.map (fn alpha => typstruct(IL.VarT(alpha), IL.StarK, NONE)) alphas
val (tau, e) = elabE (delta ++ map(alphas |=>* IL.StarK), gamma ++ map(alphas |=> sigmas)) exp
in
(IL.UnivT(alphas, tau),
IL.GenE(alphas,
ILOps.letE(alphas |=> List.map create sigmas,
e
)
)
)
end
| EL.InstE(exp, typs) =>
let
val ((alphas, tau), e) =
case elabE (delta, gamma) exp of
(IL.UnivT(args), e) => (args, e)
| _ => raise EL.Error(#region exp, "expression is not polymorphic")
val taus = List.map (fn typ => elabT' (delta, gamma) typ) typs
in
(substT (map(alphas |=> taus)) tau, IL.InstE(e, taus))
end
| EL.FoldE(modl, typs, exp) =>
let
val (sigma, _) = elabMclosed Stat (delta, gamma) modl
val alpha =
(case sigma at ["type"] of
Typ(IL.VarT(alpha), k, pmo) => alpha
| _ => raise EL.Error(region, "module is not a data type")
) handle At => raise EL.Error(region, "module is not a data type")
val (alphas, tau1, tau2) =
(case sigma at ["in"] of
Term(IL.PureT(args), pm) => args
| _ => raise EL.Error(region, "module is not a data type")
) handle At => raise EL.Error(region, "module is not a data type")
val taus = List.map (fn typ => elabT' (delta, gamma) typ) typs
val del = map(alphas |=> taus)
handle ListPair.UnequalLengths =>
raise EL.Error(region, "incorrect number of type arguments")
val tau1' = substT del tau1
val tau2' = substT del tau2
val e = elabE' (delta, gamma) exp tau1' "constructor application"
in
(tau2', IL.ConE(IL.FoldE(alpha), taus, e))
end
| EL.UnfoldE(modl, typs, exp) =>
let
val (sigma, _) = elabMclosed Stat (delta, gamma) modl
val alpha =
(case sigma at ["type"] of
Typ(IL.VarT(alpha), k, pmo) => alpha
| _ => raise EL.Error(region, "module is not a data type")
) handle At => raise EL.Error(region, "module is not a data type")
val (alphas, tau1, tau2) =
(case sigma at ["out"] of
Term(IL.PureT(args), pm) => args
| _ => raise EL.Error(region, "module is not a value")
) handle At => raise EL.Error(region, "module is not a data type")
val taus = List.map (fn typ => elabT' (delta, gamma) typ) typs
val del = map(alphas |=> taus)
handle ListPair.UnequalLengths =>
raise EL.Error(region, "incorrect number of type arguments")
val tau1' = substT del tau1
val tau2' = substT del tau2
val e = elabE' (delta, gamma) exp tau1' "deconstructor application"
in
(tau2', IL.ConE(IL.UnfoldE(alpha), taus, e))
end
| EL.LetE(x, modl, exp) =>
let
val (betaks', sigma', f) =
case elabU (delta, gamma) modl of
(([], betaks, sigma), f) => (betaks, sigma, f)
| _ => raise EL.Error(#region modl, "imports in local module")
val (betaks, del) = renamesAK betaks'
val sigma = substS del sigma'
val betas = List.map #1 betaks
val (tau, e) = elabE (delta ++ map(betaks), gamma ++ map[x |-> sigma]) exp
in
if not(equivS delta (sigma, abs sigma))
then raise EL.Error(region, "imports in local module")
else if not(isEmpty(fvT(tau) /\ set(betas)))
then raise EL.Error(region, "local type name escapes scope")
else (tau,
IL.ValE(
IL.NewTypM(betaks,
IL.LetM(x, create(sigma),
ILOps.seqM[
IL.ApplyM(
IL.InstUpM(
IL.InstDownM(f, []),
betas
),
IL.VarM(x)
),
IL.TermM(e)
]
)
)
)
)
end
| EL.PrintE(exp) =>
let
val (tau, e) = elabE (delta, gamma) exp
in
(IL.TupleT[], IL.PrintE(e))
end
and elabE' (delta, gamma) exp tau blame =
let
val (tau', e) = elabE (delta, gamma) exp
in
if equivT delta (tau, tau')
then e
else raise EL.Error(#region exp, "type mismatch in " ^ blame)
end
(* Template Modules *)
and templateM (delta, gamma) modl ls =
let
val _ = (traceIn(); traceModl "templateM" modl "begin";
traceD "delta" delta; traceG "gamma" gamma; traceP "ls" ls)
val (sigma, alphaks, betaks) = templateM_ (delta, gamma) modl ls
in
traceModl "templateM" modl "end"; traceS "sigma" sigma;
traceAKs "alphaks" alphaks; traceAKs "betaks" betaks; traceOut();
assert(fn() => subset(fvF(alphaks, betaks, sigma), dom(delta)));
assert(fn() => equal(dom(locator Minus sigma), dom(map(alphaks))));
(sigma, alphaks, betaks)
end
and templateM_ (delta, gamma) (this as {it, region}) ls =
case it of
EL.VarM(x) =>
let
val sigma = lookup gamma x
handle Lookup => raise EL.Error(region, "unbound variable " ^ x)
in
(abs(staticS sigma), [], [])
end
| EL.EmptyM => (Struct[], [], [])
| EL.ValM(exp) => (Term(staticT, Plus), [], [])
| EL.AbsValM(typ) => (Term(staticT, Minus), [], [])
| EL.TypM(typ) =>
let
val (tau, k) = elabT (delta, gamma) typ
in
(typstruct(tau, k, NONE), [], [])
end
| EL.AbsTypM(kind) =>
let
val k = elabK kind
val alpha = rename(if List.null ls then "_type" else String.concatWith "." ls)
in
(typstruct(IL.VarT(alpha), k, SOME Minus), [alpha |-> k], [])
end
| EL.DatTypM(typ) =>
let
val (tau, k) = elabT (delta, gamma) typ
val beta = rename(if List.null ls then "_data" else String.concatWith "." ls)
in
(typstruct(IL.VarT(beta), k, SOME Plus), [], [beta |-> k])
end
| EL.AbsDatTypM(typ) =>
let
val (tau, k) = elabT (delta, gamma) typ
val alpha = rename(if List.null ls then "_data" else String.concatWith "." ls)
in
(typstruct(IL.VarT(alpha), k, SOME Minus), [alpha |-> k], [])
end
| EL.UnitM(modl) => (Funct(templateU (delta, gamma) modl, Plus), [], [])
| EL.AbsUnitM(sign) => (Funct(templateS (delta, gamma) sign [], Minus), [], [])
| EL.NewM(modl) =>
let
val (alphaks', betaks', sigma') =
case templateM (delta, gamma) modl ls of
(Funct(phi, Plus), [], []) => phi
| (Funct(phi, Minus), _, _) => raise EL.Error(region, "undefined unit")
| _ => raise EL.Error(region, "module not a unit")
val (alphaks, del1) = renamesAKls alphaks' ls
val (betaks, del2) = renamesAKls betaks' ls
in
(substS (del1 ++ del2) sigma', alphaks, betaks)
end
| EL.StructM(l, modl) =>
let
val (sigma, alphaks, betaks) = templateM (delta, gamma) modl (ls@[l])
in
(Struct[(l, sigma)], alphaks, betaks)
end
| EL.DotM(modl, l) =>
let
val (sigma, alphaks, betaks) = templateM (delta, gamma) modl []
in
if not(List.all (fn(_, []) => false | (_, l'::ls) => l' = l)
(entries(locator Minus sigma)))
then raise EL.Error(region, "left-over imports in local module")
else
(sigma at [l], alphaks, betaks)
handle At => raise EL.Error(region, "unknown field " ^ l)
end
| EL.LinkM(x, modl1, modl2) =>
let
val (sigma1', alphaks1'', betaks1) = templateM (delta, gamma) modl1 ls
val (sigma2', alphaks2'', betaks2) =
templateM (delta ++ map(alphaks1'') ++ map(betaks1),
gamma ++ map[x |-> sigma1']) modl2 ls
val del2 = composePartial sigma1' (locator Minus sigma2')
handle Locate ls =>
raise EL.Error(region, "linked modules inconsistent" ^
(if List.null ls then "" else " at path " ^
String.concatWith "." ls))
val sigma2'' = substS del2 sigma2'
val alphas = List.foldr op\/ (set[]) (List.map (fvT o #2) (entries del2))
val del1 = composePartial sigma2'' (locator Minus sigma1' -- alphas)
handle Locate ls =>
raise EL.Error(region, "linked modules inconsistent" ^
(if List.null ls then "" else " at path " ^
String.concatWith "." ls))
val sigma1 = substS del1 sigma1'
val sigma2 = substS del1 sigma2''
val alphaks1 = List.filter (not o member (dom(del1)) o #1) alphaks1''
val alphaks2 = List.filter (not o member (dom(del2)) o #1) alphaks2''
val _ = (traceModl "templateM" this "1";
traceS "sigma1'" sigma1';
traceAKs "alphaks1''" alphaks1''; traceAKs "betaks1" betaks1;
traceS "sigma2'" sigma2';
traceAKs "alphaks2''" alphaks2''; traceAKs "betaks2" betaks2;
traceR "del2" del2; traceS "sigma2''" sigma2'';
traceAs "alphas" (items alphas);
traceR "del1" del1; traceS "sigma1" sigma1; traceS "sigma2" sigma2;
traceAKs "alphaks1" alphaks1; traceAKs "alphaks2" alphaks2)
val (sigma, _, _) = merge Stat (delta ++ map(alphaks1'') ++ map(alphaks2'') ++ map(betaks1) ++ map(betaks2)) (sigma1, sigma2) dummyP
handle Mismatch ls =>
raise EL.Error(region, "linked modules inconsistent" ^
(if List.null ls then "" else " at path " ^
String.concatWith "." ls))
val fv = fvF(alphaks1 @ alphaks2, betaks1 @ betaks2, sigma) \ dom(delta)
in
if isEmpty fv
then (sigma, alphaks1 @ alphaks2, betaks1 @ betaks2)
else raise EL.Error(region, "linking produces circularity for type " ^ choose fv)
end
| EL.OLinkM(x, modl1, modl2) =>
let
val (sigma, alphaks, betaks) = templateM (delta, gamma) modl1 ls
in
(abs(sigma), [], alphaks @ betaks)
end
| EL.SealM(modl, sign) =>
let
val (alphaks, betaks, sigma) = templateS (delta, gamma) sign ls
in
(sigma, alphaks, betaks)
end
(*
| EL.LetM(x, modl1, modl2) =>
let
val (sigma1, alphaks1, betaks1) = templateM (delta, gamma) modl1 ls
val (sigma2, alphaks2, betaks2) =
templateM (delta ++ map(alphaks1) ++ map(betaks1), gamma ++ map[x |-> sigma1])
modl2 ls
in
if not(List.null alphaks1)
then raise EL.Error(region, "imports in local module")
else (sigma2, alphaks2, betaks1 @ betaks2)
end
*)
and templateU (delta, gamma) modl =
let
val _ = (traceIn(); traceModl "templateU" modl "begin";
traceD "delta" delta; traceG "gamma" gamma)
val _ = assert(fn() => subset(fvG(gamma), dom(delta)))
val (sigma, alphaks, betaks) = templateM (delta, gamma) modl []
in
traceModl "templateU" modl "end";
traceF "phi" (alphaks, betaks, sigma); traceOut();
assert(fn() => subset(fvF(alphaks, betaks, sigma), dom(delta)));
assert(fn() => equal(dom(locator Minus sigma), dom(map(alphaks))));
(alphaks, betaks, sigma)
end
and templateS (delta, gamma) sign ls =
let
val _ = (traceIn(); traceSign "templateS" sign "begin";
traceD "delta" delta; traceG "gamma" gamma; traceP "ls" ls)
val _ = assert(fn() => subset(fvG(gamma), dom(delta)))
val phi = templateS_ (delta, gamma) sign ls
in
traceSign "templateS" sign "end"; traceF "phi" phi; traceOut();
assert(fn() => subset(fvF(phi), dom(delta)));
assert(fn() => equal(dom(locator Minus (#3 phi)), dom(map(#1 phi))));
phi
end
and templateS_ (delta, gamma) {it, region} ls =
case it of
EL.ExportS(modl, lss) =>
let
val (sigma', alphaks', betaks') = templateM (delta, gamma) modl ls
val sigma = export lss sigma'
val alphaks = List.filter (member(dom(locator Minus sigma)) o #1) alphaks'
val betaks = List.filter (member(dom(locator Plus sigma)) o #1) alphaks'
in
if not(List.null betaks')
then raise EL.Error(region, "non-abstract module in signature expression")
else (alphaks, betaks, sigma)
end
| EL.ImportS(modl, lss) =>
let
val (sigma', alphaks', betaks') = templateM (delta, gamma) modl ls
val sigma = neg(export lss sigma')
val alphaks = List.filter (member(dom(locator Minus sigma)) o #1) alphaks'
val betaks = List.filter (member(dom(locator Plus sigma)) o #1) alphaks'
in
if not(List.null betaks')
then raise EL.Error(region, "non-abstract module in signature expression")
else (alphaks, betaks, sigma)
end
(* elabM : pass -> typ_context * modl_context * typvar list * sign -> modl -> path ->
sign * typvar list * IL.modl *)
and elabM pass (delta, gamma, betas0, sigma0) modl p =
let
val _ = (traceIn(); traceModl "elabM" modl "begin"; traceB "static" (pass = Stat);
traceD "delta" delta; traceG "gamma" gamma;
traceAs "betas0" betas0; traceS "sigma0" sigma0)
val _ = assert(fn() => subset(fvG(gamma) \/ set(betas0) \/ fvS(sigma0), dom(delta)))
val (sigma, betas, m) = elabM_ pass (delta, gamma, betas0, sigma0) modl p
in
traceModl "elabM" modl "end"; traceS "sigma" sigma; traceAs "betas'" betas;
traceOut();
assert(fn() => subset(fvS(sigma), dom(delta)));
(sigma, betas, m)
end
and elabM_ pass (delta, gamma, betas0, sigma0) (this as {it, region}) p =
case it of
EL.VarM(x) =>
let
val sigma = lookup gamma x
handle Lookup => raise EL.Error(region, "unbound variable " ^ x)
val sigma = if pass = Stat then staticS sigma else sigma
in
(abs(sigma), betas0, copy(IL.VarM(x), p, abs(sigma)))
end
| EL.EmptyM =>
let
in
(Struct[], betas0, IL.StructM[])
end
| EL.ValM(exp) =>
let
val (tau, e) =
if pass = Stat then (staticT, dummyE)
else elabE (delta, gamma) exp
val x = rename "_val.X"
in
(Term(tau, Plus), betas0,
IL.LetM(x, IL.TermM(e), IL.AssignM(p, IL.VarM(x))))
end
| EL.AbsValM(typ) =>
let
val tau =
if pass = Stat then staticT else
case elabT (delta, gamma) typ of
(tau, IL.StarK) => tau
| _ => raise EL.Error(region, "value type is not ground")
in
(Term(tau, Minus), betas0, IL.StructM[])
end
| EL.TypM(typ) =>
let
val (tau, k) = elabT (delta, gamma) typ
in
(typstruct(tau, k, NONE), betas0, IL.StructM[])
end
| EL.AbsTypM(kind) =>
let
val k = elabK kind
(* without realisers:
val alpha = asAbsTyp(sigma0 at ["type"])
in
if lookup delta alpha <> k then raise EL.Error(region, "inconsistent kind")
else (typstruct(IL.VarT(alpha), k, SOME Minus), betas0, IL.StructM[])
*)
val (tau, k', pmo) = asTyp(sigma0 at ["type"])
in
if k' <> k then raise EL.Error(region, "inconsistent kind")
else (typstruct(tau, k, Option.map (fn pm => Minus) pmo), betas0, IL.StructM[])
end
| EL.DatTypM(typ) =>
let
val (tau, k) = elabT (delta, gamma) typ
val beta = List.hd betas0
handle Empty => raise EL.Error(region, "linearity failure at datatype")
in
if lookup delta beta <> k then raise EL.Error(region, "inconsistent kind")
else (datstruct(beta, tau, k, SOME Plus), List.tl betas0,
IL.DefIsoM(beta, tau,
ILOps.seqM[
IL.AssignM(IL.DotM(p, "in"), IL.TermM(IL.FoldE(beta))),