-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.sml
1314 lines (1112 loc) · 42.8 KB
/
internal.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
structure Internal = struct
structure Kind = struct
datatype t
= KBase
| KArrow of t * t
exception Mismatch of t * t
exception AppBase
fun lift xs y = foldr (fn (x, acc) => KArrow(x, acc)) y xs
fun unify k1 k2 =
if k1 = k2
then ()
else raise Mismatch(k1, k2)
fun args KBase = []
| args (KArrow(k1, k2)) = k1 :: args k2
fun get_base f =
fn KBase => ()
| k => raise f k
val get_arrow =
fn KArrow p => p
| _ => raise AppBase
local open Pretty in
fun show_prec n =
fn KBase => "Type"
| KArrow(x, y) => paren (2 < n) $ show_prec 3 x <+> "->" <+> show_prec 2 y
fun show k = show_prec 0 k
end
end
datatype kind = datatype Kind.t
structure Quantified :> sig
type 'a t
type info
datatype ident
= T of type_ident
| V of value_ident
val from_body : 'a -> 'a t
val quantify : info list -> 'a -> 'a t
val quantify1 : kind -> ident -> 'a -> 'a t
val map_with_location : (ident location -> ident location option) -> ('a -> 'b) -> 'a t -> 'b t
val map : ('a -> 'b) -> 'a t -> 'b t
val apply : (kind list * 'a -> 'b) -> 'a t -> 'b
val proj : 'a t -> info list * 'a
val get_body : 'a t -> 'a
val all_alive : 'a t -> bool
structure Info : sig
val extract : info -> kind
val get_location : info -> ident location
val map : (kind -> kind) -> info -> info
val make_obsolete : (ident location -> bool) -> info -> info
end
end = struct
datatype ident
= T of type_ident
| V of value_ident
datatype status
= Obsolete
| Alive
type info = kind * ident location * status
type 'a t = info list * 'a
fun from_body x = ([], x)
fun quantify xs x = (xs, x)
fun quantify1 k id x = ([(k, ([], id), Alive)], x)
fun all_alive (xs, _) =
List.all (fn (_, _, s) => s = Alive) xs
fun map_with_location f g (xs, v) =
let
fun go (k, loc, Obsolete) = (k, loc, Obsolete)
| go (k, loc, Alive) =
case f loc of
SOME loc' => (k, loc', Alive)
| NONE => (k, loc, Obsolete)
in
(List.map go xs, g v)
end
fun map f (xs, x) = (xs, f x)
fun apply f ((xs, x) : 'a t) =
f (List.map #1 xs : kind list, x)
fun proj x = x
fun get_body (_, x) = x
structure Info = struct
fun extract (k, _, _) = k
fun get_location (_, loc, _) = loc
fun map f (k, x, y) = (f k, x, y)
fun make_obsolete _ (k, loc, Obsolete) = (k, loc, Obsolete)
| make_obsolete f (k, loc, Alive) =
if f loc
then (k, loc, Obsolete)
else (k, loc, Alive)
end
end
type 'a existential = 'a Quantified.t
type 'a universal = 'a Quantified.t
structure FVar :> sig
type t
val equal : t -> t -> bool
val compare : t * t -> order
val nth : t list * t -> int option
val get_kind : t -> kind
val fresh : kind -> t
val show : t -> string
structure Set : SET where type elem = t
structure Map : MAP where type key = t
structure Lwd : LWD where type elem = t
end = struct
type t = int * kind
val counter = ref 0
fun equal (x, _) (y, _) = x = y
fun compare ((x, _), (y, _)) = Int.compare (x, y)
fun fst (x, _) = x
fun nth _ ([], _) = NONE
| nth i (x :: xs, y) =
if fst x = fst y
then SOME i
else nth (i + 1) (xs, y)
val nth = fn p => nth 0 p
fun get_kind (_, k) = k
fun fresh k =
let
val n = !counter
in
counter := n + 1; (n, k)
end
fun show (n, _) = "!" ^ Int.toString n
structure Set = Set (type t = t val compare = compare)
structure Map = Map (type t = t val compare = compare)
structure Lwd = Lwd (Set)
end
type fvar = FVar.t
structure UVar :> sig
type t
val equal : t -> t -> bool
val compare : t * t -> order
val fresh : unit -> t
val show : t -> string
structure Set : SET where type elem = t
structure Lwd : LWD where type elem = t
end = struct
type t = int
val counter = ref 0
fun equal x y = x = y
val compare = Int.compare
fun fresh () =
let
val n = !counter
in
counter := n + 1; n
end
fun show n = "?" ^ Int.toString n
structure Set = Set (type t = t val compare = compare)
structure Lwd = Lwd (Set)
end
type uvar = UVar.t
structure Purity = struct
datatype t
= Impure
| Pure
fun show Impure = "impure"
| show Pure = "pure"
end
datatype purity = datatype Purity.t
datatype tycon
= Bound of int * int
| Free of fvar
| Unif of uvar * unif ref
| Pack of asig
| Abs of kind * tycon
| App of tycon * tycon
| Arrow of tycon * tycon
| Unit
| Prod of tycon * tycon
| Sum of tycon * tycon
and unif
= Defined of tycon
| Undefined
and semsig
= Struct of struct_
| IArrow of (semsig * asig) universal
| PArrow of (semsig * semsig) universal
and asig
= Exist of semsig existential
(* We just let semantic paths be (unrestricted) type constructors
* because we don't want to bother about normalization after substitution. *)
and path
= Path of tycon
withtype scheme = int * tycon
and struct_ =
{ m : semsig ModuleIdent.Map.t
, s : asig SignatureIdent.Map.t
, v : (path * (int * tycon)) ValueIdent.Map.t
, t : (tycon * kind) TypeIdent.Map.t
}
structure U = Product (struct
type a = uvar
val compare = UVar.compare
type e = unif ref
end)
exception Found of fvar
exception Occur of uvar * tycon
exception TypeMismatch of tycon * tycon
exception PackageASigMismatch of asig * asig
exception PackageSigMismatch of semsig * semsig
exception SchemeMismatch of scheme * scheme
exception NoSuchModuleComponent of module_ident
exception NoSuchSignatureComponent of signature_ident
exception NoSuchValueComponent of value_ident
exception NoSuchTypeComponent of type_ident
exception ProjFromFunctor of semsig
exception MissingModuleInSubSignature of module_ident
exception MissingSignatureInSubSignature of signature_ident
exception MissingValueInSubSignature of value_ident
exception MissingTypeInSubSignature of type_ident
exception ImpureIsNotSubtypeOfPure of semsig * semsig
exception NotSubtype of semsig * semsig
exception CannotLookupType of semsig * semsig
exception DuplicateModuleComponent of module_ident
exception DuplicateSignatureComponent of signature_ident
exception DuplicateValueComponent of value_ident
exception DuplicateTypeComponent of type_ident
exception NotProvenToBeExplicit of asig
structure M : sig
val show_tycon : tycon -> string
val show_asig : asig -> string
val show_semsig : semsig -> string
val show_path : path -> string
val show_scheme : scheme -> string
val open_tycon : int -> tycon list -> tycon -> tycon
val open_asig : int -> tycon list -> asig -> asig
val open_semsig : int -> tycon list -> semsig -> semsig
val open_struct : int -> tycon list -> struct_ -> struct_
val open_path : int -> tycon list -> path -> path
val open_scheme : int -> tycon list -> scheme -> scheme
val close_tycon : int -> fvar list -> tycon -> tycon
val close_asig : int -> fvar list -> asig -> asig
val close_semsig : int -> fvar list -> semsig -> semsig
val close_struct : int -> fvar list -> struct_ -> struct_
val close_path : int -> fvar list -> path -> path
val close_scheme : int -> fvar list -> scheme -> scheme
val subst_semsig : (fvar * tycon) list -> semsig -> semsig
val app_list : tycon -> tycon list -> tycon
val abs_list : fvar list -> tycon -> tycon
val from_path : path -> tycon
val join : purity -> purity -> purity
val empty_struct : struct_
val insert_module : module_ident -> semsig -> struct_ -> struct_
val insert_signature : signature_ident -> asig -> struct_ -> struct_
val insert_value : value_ident -> path * scheme -> struct_ -> struct_
val insert_type : type_ident -> tycon * kind -> struct_ -> struct_
val disjoint_union : struct_ -> struct_ -> struct_
val union : struct_ -> struct_ -> struct_ (* Right-biased. *)
val reduce : tycon -> tycon
val find_tycon : fvar list -> tycon -> unit (* Found *)
val find_asig : fvar list -> asig -> unit (* Found *)
val find_semsig : fvar list -> semsig -> unit (* Found *)
val find_path : fvar list -> path -> unit (* Found *)
val find_scheme : fvar list -> scheme -> unit (* Found *)
(* Returns non-spurious unification variables in the canonical order. *)
val uvs_tycon : tycon -> U.Lwd.t
val uvs_asig : asig -> U.Lwd.t
val uvs_semsig : semsig -> U.Lwd.t
val uvs_path : path -> U.Lwd.t
val uvs_scheme : scheme -> U.Lwd.t
(* Returns non-spurious free variables in the canonical order. *)
val fvs_tycon : FVar.Set.t -> tycon -> FVar.Lwd.t
val fvs_semsig : FVar.Set.t -> semsig -> FVar.Lwd.t
val unify : tycon -> tycon -> kind -> unit
val str_unify : tycon -> tycon -> kind
val get_structure : (semsig -> exn) -> semsig -> struct_
val get_functor : (semsig -> exn) -> semsig -> (semsig * asig) universal * purity
val proj_module : semsig -> module_ident -> semsig
val proj_signature : semsig -> signature_ident -> asig
val proj_value : semsig -> value_ident -> path * scheme
val proj_type : semsig -> type_ident -> tycon * kind
val proj_module_loc : semsig -> module_ident location -> semsig
val proj_value_loc : semsig -> value_ident location -> path * scheme
val proj_type_loc : semsig -> type_ident location -> tycon * kind
val lookup_type : Quantified.ident location -> semsig -> semsig -> tycon
val lookup_types : (Quantified.ident location * fvar) list -> semsig -> semsig -> tycon list
val is_instance_of : scheme -> scheme -> unit
val subtype_semsig : semsig -> semsig -> unit
val subtype_asig : asig -> asig -> unit
val match : semsig -> asig -> tycon list
val instantiate : scheme -> tycon
val norm_asig : asig -> asig
val explicit_asig : asig -> unit
val explicit_semsig : semsig -> unit
val relative_location : module_ident location -> 'a location -> 'a location option
end = struct
fun open_tycon j tys =
fn Bound(n, i) =>
if n = j
then List.nth (tys, i)
else Bound(n, i)
| Free fv => Free fv
| Unif(v, r) =>
let in
case !r of
Defined ty => open_tycon j tys ty
| Undefined => Unif(v, r)
end
| Pack asig => Pack (open_asig j tys asig)
| Abs(k, x) => Abs(k, open_tycon (j + 1) tys x)
| App(x, y) => App(open_tycon j tys x, open_tycon j tys y)
| Arrow(x, y) => Arrow(open_tycon j tys x, open_tycon j tys y)
| Unit => Unit
| Prod(x, y) => Prod(open_tycon j tys x, open_tycon j tys y)
| Sum(x, y) => Sum(open_tycon j tys x, open_tycon j tys y)
and open_asig j tys (Exist e) = Exist (Quantified.map (open_semsig (j + 1) tys) e)
and open_semsig j tys =
fn Struct s => Struct (open_struct j tys s)
| IArrow u =>
IArrow
(Quantified.map
(fn (x, y) => (open_semsig (j + 1) tys x, (open_asig (j + 1) tys y)))
u
)
| PArrow u =>
PArrow
(Quantified.map
(fn (x, y) => (open_semsig (j + 1) tys x, (open_semsig (j + 1) tys y)))
u
)
and open_struct j tys {m, s, v, t} =
{ m = ModuleIdent.Map.map (open_semsig j tys) m
, s = SignatureIdent.Map.map (open_asig j tys) s
, v = ValueIdent.Map.map (fn (p, scheme) => (open_path j tys p, open_scheme j tys scheme)) v
, t = TypeIdent.Map.map (fn (ty, k) => (open_tycon j tys ty, k)) t
}
and open_path j tys (Path ty) = Path (open_tycon j tys ty)
and open_scheme j tys (n, ty) =
(n, open_tycon (j + 1) tys ty)
fun close_tycon j fvs =
fn Bound(n, i) => Bound(n, i)
| Free fv =>
let in
case FVar.nth (fvs, fv) of
SOME i => Bound(j, i)
| NONE => Free fv
end
| Unif(v, r) =>
let in
case !r of
Defined ty => close_tycon j fvs ty
| Undefined => Unif(v, r)
end
| Pack asig => Pack (close_asig j fvs asig)
| Abs(k, x) => Abs(k, close_tycon (j + 1) fvs x)
| App(x, y) => App(close_tycon j fvs x, close_tycon j fvs y)
| Arrow(x, y) => Arrow(close_tycon j fvs x, close_tycon j fvs y)
| Unit => Unit
| Prod(x, y) => Prod(close_tycon j fvs x, close_tycon j fvs y)
| Sum(x, y) => Sum(close_tycon j fvs x, close_tycon j fvs y)
and close_asig j fvs (Exist e) = Exist (Quantified.map (close_semsig (j + 1) fvs) e)
and close_semsig j fvs =
fn Struct s => Struct (close_struct j fvs s)
| IArrow u =>
IArrow
(Quantified.map
(fn (x, y) => (close_semsig (j + 1) fvs x, (close_asig (j + 1) fvs y)))
u
)
| PArrow u =>
PArrow
(Quantified.map
(fn (x, y) => (close_semsig (j + 1) fvs x, (close_semsig (j + 1) fvs y)))
u
)
and close_struct j fvs {m, s, v, t} =
{ m = ModuleIdent.Map.map (close_semsig j fvs) m
, s = SignatureIdent.Map.map (close_asig j fvs) s
, v = ValueIdent.Map.map (fn (p, scheme) => (close_path j fvs p, close_scheme j fvs scheme)) v
, t = TypeIdent.Map.map (fn (ty, k) => (close_tycon j fvs ty, k)) t
}
and close_path j fvs (Path ty) = Path (close_tycon j fvs ty)
and close_scheme j fvs (n, ty) =
(n, close_tycon (j + 1) fvs ty)
fun subst_semsig (xs : (fvar * tycon) list) x =
open_semsig 0 (map #2 xs)
(close_semsig 0 (map #1 xs) x)
fun app_list x [] = x
| app_list x (y :: ys) = app_list (App(x, y)) ys
fun abs_list fvs x =
foldr (fn (fv, acc) => Abs(FVar.get_kind fv, close_tycon 0 [fv] acc)) x fvs
fun from_path (Path ty) = ty
fun join Pure Pure = Pure
| join _ _ = Impure
val empty_struct =
{ m = ModuleIdent.Map.empty
, s = SignatureIdent.Map.empty
, v = ValueIdent.Map.empty
, t = TypeIdent.Map.empty
}
fun insert_module id x {m, s, v, t} =
{ m = ModuleIdent.Map.insert id x m
, s = s
, v = v
, t = t
}
fun insert_signature id x {m, s, v, t} =
{ m = m
, s = SignatureIdent.Map.insert id x s
, v = v
, t = t
}
fun insert_value id x {m, s, v, t} =
{ m = m
, s = s
, v = ValueIdent.Map.insert id x v
, t = t
}
fun insert_type id x {m, s, v, t} =
{ m = m
, s = s
, v = v
, t = TypeIdent.Map.insert id x t
}
fun disjoint_union (x : struct_) (y : struct_) =
let
fun fm (k, v, acc) =
if ModuleIdent.Map.member k (#m y)
then raise DuplicateModuleComponent k
else ModuleIdent.Map.insert k v acc
fun fs (k, v, acc) =
if SignatureIdent.Map.member k (#s y)
then raise DuplicateSignatureComponent k
else SignatureIdent.Map.insert k v acc
fun fv (k, v, acc) =
if ValueIdent.Map.member k (#v y)
then raise DuplicateValueComponent k
else ValueIdent.Map.insert k v acc
fun ft (k, v, acc) =
if TypeIdent.Map.member k (#t y)
then raise DuplicateTypeComponent k
else TypeIdent.Map.insert k v acc
in
{ m = ModuleIdent.Map.fold fm (#m y) (#m x)
, s = SignatureIdent.Map.fold fs (#s y) (#s x)
, v = ValueIdent.Map.fold fv (#v y) (#v x)
, t = TypeIdent.Map.fold ft (#t y) (#t x)
}
end
fun union (x : struct_) (y : struct_) =
let
fun fm (k, v, acc) =
if ModuleIdent.Map.member k (#m y)
then acc
else ModuleIdent.Map.insert k v acc
fun fs (k, v, acc) =
if SignatureIdent.Map.member k (#s y)
then acc
else SignatureIdent.Map.insert k v acc
fun fv (k, v, acc) =
if ValueIdent.Map.member k (#v y)
then acc
else ValueIdent.Map.insert k v acc
fun ft (k, v, acc) =
if TypeIdent.Map.member k (#t y)
then acc
else TypeIdent.Map.insert k v acc
in
{ m = ModuleIdent.Map.fold fm (#m y) (#m x)
, s = SignatureIdent.Map.fold fs (#s y) (#s x)
, v = ValueIdent.Map.fold fv (#v y) (#v x)
, t = TypeIdent.Map.fold ft (#t y) (#t x)
}
end
fun reduce (App(x, y)) = reduce' (reduce x) y
| reduce (Unif(v, r)) =
let in
case !r of
Defined ty => reduce ty
| Undefined => Unif(v, r)
end
| reduce ty = ty
and reduce' (Abs(_, x)) y = reduce (open_tycon 0 [y] x)
| reduce' x y = App(x, y)
fun find_tycon' fvs =
fn Bound _ => ()
| Free fv =>
let in
case FVar.nth (fvs, fv) of
SOME _ => raise Found fv
| NONE => ()
end
| Unif _ => () (* Assume the argument has been `reduce`d. *)
| Pack asig => find_asig fvs asig
| Abs(k, x) => find_tycon fvs (open_tycon 0 [Free (FVar.fresh k)] x)
| App(x, y) => find_tycon' fvs x before find_tycon fvs y
| Arrow(x, y) => find_tycon fvs x before find_tycon fvs y
| Unit => ()
| Prod(x, y) => find_tycon fvs x before find_tycon fvs y
| Sum(x, y) => find_tycon fvs x before find_tycon fvs y
and find_tycon fvs ty = find_tycon' fvs (reduce ty)
and find_asig fvs (Exist e) =
let
val s = Quantified.apply (fn (ks, s) => open_semsig 0 (map (Free o FVar.fresh) ks) s) e
in
find_semsig fvs s
end
and find_semsig fvs =
fn Struct {m, s, v, t} =>
( ModuleIdent.Map.app (find_semsig fvs) m
; SignatureIdent.Map.app (find_asig fvs) s
; ValueIdent.Map.app (fn (p, s) => find_path fvs p before find_scheme fvs s) v
; TypeIdent.Map.app (fn (ty, _) => find_tycon fvs ty) t
)
| IArrow u =>
let
fun f (ks, (s, asig)) =
let val xs = map (Free o FVar.fresh) ks in
( open_semsig 0 xs s
, open_asig 0 xs asig
)
end
val (s, asig) = Quantified.apply f u
in
find_semsig fvs s; find_asig fvs asig
end
| PArrow u =>
let
fun f (ks, (s, s')) =
let val xs = map (Free o FVar.fresh) ks in
( open_semsig 0 xs s
, open_semsig 0 xs s'
)
end
val (s, s') = Quantified.apply f u
in
find_semsig fvs s; find_semsig fvs s'
end
and find_path fvs (Path ty) = find_tycon fvs ty
and find_scheme fvs (n, ty) =
find_tycon fvs (open_tycon 0 (List.tabulate (n, fn _ => Free (FVar.fresh KBase))) ty)
fun uvs_tycon' ty : U.Lwd.t =
let val op@ = U.Lwd.append in
case ty of
Bound _ => U.Lwd.empty
| Free _ => U.Lwd.empty
| Unif(v, r) => U.Lwd.singleton (v, r)
| Pack asig => uvs_asig asig
| Abs(k, x) => uvs_tycon (open_tycon 0 [Free (FVar.fresh k)] x)
| App(x, y) => uvs_tycon' x @ uvs_tycon y
| Arrow(x, y) => uvs_tycon x @ uvs_tycon y
| Unit => U.Lwd.empty
| Prod(x, y) => uvs_tycon x @ uvs_tycon y
| Sum(x, y) => uvs_tycon x @ uvs_tycon y
end
and uvs_tycon ty = uvs_tycon' (reduce ty)
and uvs_asig (Exist e) =
let
val s = Quantified.apply (fn (ks, s) => open_semsig 0 (map (Free o FVar.fresh) ks) s) e
in
uvs_semsig s
end
and uvs_semsig s =
let val op@ = U.Lwd.append in
case s of
Struct {m, s, v, t} =>
ModuleIdent.Map.fold (fn (_, s, acc) => acc @ uvs_semsig s) U.Lwd.empty m
@ SignatureIdent.Map.fold (fn (_, asig, acc) => acc @ uvs_asig asig) U.Lwd.empty s
@ ValueIdent.Map.fold (fn (_, (p, scheme), acc) => acc @ uvs_path p @ uvs_scheme scheme) U.Lwd.empty v
@ TypeIdent.Map.fold (fn (_, (ty, _), acc) => acc @ uvs_tycon ty) U.Lwd.empty t
| IArrow u =>
let
fun f (ks, (s, asig)) =
let val fvs = map (Free o FVar.fresh) ks in
U.Lwd.append
( uvs_semsig (open_semsig 0 fvs s)
, uvs_asig (open_asig 0 fvs asig)
)
end
in
Quantified.apply f u
end
| PArrow u =>
let
fun f (ks, (s, s')) =
let val fvs = map (Free o FVar.fresh) ks in
U.Lwd.append
( uvs_semsig (open_semsig 0 fvs s)
, uvs_semsig (open_semsig 0 fvs s')
)
end
in
Quantified.apply f u
end
end
and uvs_path (Path ty) = uvs_tycon ty
and uvs_scheme (n, ty) =
uvs_tycon (open_tycon 0 (List.tabulate (n, fn _ => Free (FVar.fresh KBase))) ty)
fun fvs_tycon' pred ty : FVar.Lwd.t =
let val op@ = FVar.Lwd.append in
case ty of
Bound _ => FVar.Lwd.empty
| Free fv => if FVar.Set.member fv pred then FVar.Lwd.singleton fv else FVar.Lwd.empty
| Unif _ => FVar.Lwd.empty
| Pack asig => fvs_asig pred asig
| Abs(k, x) => fvs_tycon pred (open_tycon 0 [Free (FVar.fresh k)] x)
| App(x, y) => fvs_tycon' pred x @ fvs_tycon pred y
| Arrow(x, y) => fvs_tycon pred x @ fvs_tycon pred y
| Unit => FVar.Lwd.empty
| Prod(x, y) => fvs_tycon pred x @ fvs_tycon pred y
| Sum(x, y) => fvs_tycon pred x @ fvs_tycon pred y
end
and fvs_tycon pred ty = fvs_tycon' pred (reduce ty)
and fvs_asig pred (Exist e) =
let
val s = Quantified.apply (fn (ks, s) => open_semsig 0 (map (Free o FVar.fresh) ks) s) e
in
fvs_semsig pred s
end
and fvs_semsig pred s =
let val op@ = FVar.Lwd.append in
case s of
Struct {m, s, v, t} =>
ModuleIdent.Map.fold (fn (_, s, acc) => acc @ fvs_semsig pred s) FVar.Lwd.empty m
@ SignatureIdent.Map.fold (fn (_, asig, acc) => acc @ fvs_asig pred asig) FVar.Lwd.empty s
@ ValueIdent.Map.fold (fn (_, (p, scheme), acc) => acc @ fvs_path pred p @ fvs_scheme pred scheme) FVar.Lwd.empty v
@ TypeIdent.Map.fold (fn (_, (ty, _), acc) => acc @ fvs_tycon pred ty) FVar.Lwd.empty t
| IArrow u =>
let
fun f (ks, (s, asig)) =
let val fvs = map (Free o FVar.fresh) ks in
FVar.Lwd.append
( fvs_semsig pred (open_semsig 0 fvs s)
, fvs_asig pred (open_asig 0 fvs asig)
)
end
in
Quantified.apply f u
end
| PArrow u =>
let
fun f (ks, (s, s')) =
let val fvs = map (Free o FVar.fresh) ks in
FVar.Lwd.append
( fvs_semsig pred (open_semsig 0 fvs s)
, fvs_semsig pred (open_semsig 0 fvs s')
)
end
in
Quantified.apply f u
end
end
and fvs_path pred (Path ty) = fvs_tycon pred ty
and fvs_scheme pred (n, ty) =
fvs_tycon pred (open_tycon 0 (List.tabulate (n, fn _ => Free (FVar.fresh KBase))) ty)
fun unify_uvar ((v, r) : U.t) (y : tycon) : unit =
case y of
Unif(v', _) =>
if UVar.equal v v'
then ()
else r := Defined y
| _ =>
if U.Lwd.member (v, r) (uvs_tycon y)
then raise Occur(v, y)
else r := Defined y
fun unify ty1 ty2 k =
case k of
KBase => ignore (str_unify (reduce ty1) (reduce ty2))
| KArrow(k1, k2) =>
let val fv = Free (FVar.fresh k1) in
unify (App(ty1, fv)) (App(ty2, fv)) k2
end
and str_unify ty1 ty2 =
case (ty1, ty2) of
(Free v1, Free v2) =>
if FVar.equal v1 v2
then FVar.get_kind v1
else raise TypeMismatch(ty1, ty2)
| (Unif x, _) => KBase before unify_uvar x ty2
| (_, Unif x) => KBase before unify_uvar x ty1
| (Pack asig1, Pack asig2) => KBase before unify_asig asig1 asig2
| (App(x1, x2), App(y1, y2)) =>
let in
case str_unify x1 y1 of
KBase => raise Kind.AppBase
| KArrow(k1, k2) => k2 before unify x2 y2 k1
end
| (Arrow(x1, x2), Arrow(y1, y2)) =>
KBase
before unify x1 y1 KBase
before unify x2 y2 KBase
| (Unit, Unit) => KBase
| (Prod(x1, x2), Prod(y1, y2)) =>
KBase
before unify x1 y1 KBase
before unify x2 y2 KBase
| (Sum(x1, x2), Sum(y1, y2)) =>
KBase
before unify x1 y1 KBase
before unify x2 y2 KBase
| _ => raise TypeMismatch(ty1, ty2)
and unify_asig (Exist asig1) (Exist asig2) : unit =
let
val (xs, s1) = Quantified.proj asig1
val (ys, s2) = Quantified.proj asig2
fun f (i1, i2) =
let
val k1 = Quantified.Info.extract i1
val k2 = Quantified.Info.extract i2
in
if k1 = k2
then Free (FVar.fresh k1)
else raise PackageASigMismatch(Exist asig1, Exist asig2)
end
val fvs = ListPair.mapEq f (xs, ys)
handle ListPair.UnequalLengths => raise PackageASigMismatch(Exist asig1, Exist asig2)
in
unify_semsig (open_semsig 0 fvs s1) (open_semsig 0 fvs s2)
end
and unify_semsig s1 s2 : unit =
case (s1, s2) of
(Struct x, Struct y) =>
( ModuleIdent.Map.app_eq (fn (x, y) => unify_semsig x y) (#m x) (#m y)
; SignatureIdent.Map.app_eq (fn (x, y) => unify_asig x y) (#s x) (#s y)
; ValueIdent.Map.app_eq (fn ((p1, s1), (p2, s2)) => unify_path p1 p2 before unify_scheme s1 s2) (#v x) (#v y)
; TypeIdent.Map.app_eq (fn ((ty1, k1), (ty2, k2)) => Kind.unify k1 k2 before unify ty1 ty2 k1) (#t x) (#t y)
)
| (IArrow u1, IArrow u2) =>
let
val (xs, (s1, asig1)) = Quantified.proj u1
val (ys, (s2, asig2)) = Quantified.proj u2
fun f (i1, i2) =
let
val k1 = Quantified.Info.extract i1
val k2 = Quantified.Info.extract i2
in
if k1 = k2
then Free (FVar.fresh k1)
else raise PackageSigMismatch(IArrow u1, IArrow u2)
end
val fvs = ListPair.mapEq f (xs, ys)
handle ListPair.UnequalLengths => raise PackageSigMismatch(IArrow u1, IArrow u2)
in
unify_semsig (open_semsig 0 fvs s1) (open_semsig 0 fvs s2);
unify_asig (open_asig 0 fvs asig1) (open_asig 0 fvs asig2)
end
| (PArrow u1, PArrow u2) =>
let
val (xs, (s1, s1')) = Quantified.proj u1
val (ys, (s2, s2')) = Quantified.proj u2
fun f (i1, i2) =
let
val k1 = Quantified.Info.extract i1
val k2 = Quantified.Info.extract i2
in
if k1 = k2
then Free (FVar.fresh k1)
else raise PackageSigMismatch(PArrow u1, PArrow u2)
end
val fvs = ListPair.mapEq f (xs, ys)
handle ListPair.UnequalLengths => raise PackageSigMismatch(PArrow u1, PArrow u2)
in
unify_semsig (open_semsig 0 fvs s1) (open_semsig 0 fvs s2);
unify_semsig (open_semsig 0 fvs s1') (open_semsig 0 fvs s2')
end
| _ => raise PackageSigMismatch(s1, s2)
and unify_path p1 p2 =
unify (from_path p1) (from_path p2) KBase
and unify_scheme (s1 as (n1, x)) (s2 as (n2, y)) =
if n1 <> n2
then raise SchemeMismatch(s1, s2)
else
let
val fvs = List.tabulate (n1, fn _ => Free (FVar.fresh KBase))
val x = open_tycon 0 fvs x
val y = open_tycon 0 fvs y
in
unify x y KBase
end
val quantify_codomain : (semsig * semsig) universal -> (semsig * asig) universal =
Quantified.map (fn (x, y) => (x, Exist (Quantified.from_body y)))
fun get_structure f =
fn Struct s => s
| s => raise f s
fun get_functor f =
fn IArrow u => (u, Impure)
| PArrow u => (quantify_codomain u, Pure)
| s => raise f s
fun proj_module s id =
valOf (ModuleIdent.Map.lookup id (#m (get_structure ProjFromFunctor s)))
handle Option => raise NoSuchModuleComponent id
fun proj_signature s id =
valOf (SignatureIdent.Map.lookup id (#s (get_structure ProjFromFunctor s)))
handle Option => raise NoSuchSignatureComponent id
fun proj_value s id =
valOf (ValueIdent.Map.lookup id (#v (get_structure ProjFromFunctor s)))
handle Option => raise NoSuchValueComponent id
fun proj_type s id =
valOf (TypeIdent.Map.lookup id (#t (get_structure ProjFromFunctor s)))
handle Option => raise NoSuchTypeComponent id
fun proj_module_loc s (mids, mid) =
let val s' = foldl (fn (mid, acc) => proj_module acc mid) s mids in
proj_module s' mid
end
fun proj_value_loc s (mids, vid) =
let val s' = foldl (fn (mid, acc) => proj_module acc mid) s mids in
proj_value s' vid
end
fun proj_type_loc s (mids, tid) =
let val s' = foldl (fn (mid, acc) => proj_module acc mid) s mids in
proj_type s' tid
end
fun lookup_type loc s1 s2 =
case (s1, s2) of
(Struct x, Struct y) =>
let in
case loc of
(mid :: mids, id) =>
let in
lookup_type
(mids, id)
(valOf (ModuleIdent.Map.lookup mid (#m x)))
(valOf (ModuleIdent.Map.lookup mid (#m y)))
handle Option => raise CannotLookupType(s1, s2)
end
| ([], Quantified.V vid) =>
let
val (p1, _) = valOf (ValueIdent.Map.lookup vid (#v x))
handle Option => raise CannotLookupType(s1, s2)
in
from_path p1
end
| ([], Quantified.T tid) =>
let
val (ty1, k1) = valOf (TypeIdent.Map.lookup tid (#t x))
handle Option => raise CannotLookupType(s1, s2)
val (_, k2) = valOf (TypeIdent.Map.lookup tid (#t y))
in
Kind.unify k1 k2; ty1
end
end
| (PArrow u1, PArrow u2) =>
let