-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtyped.sml
1916 lines (1878 loc) · 76.4 KB
/
typed.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
(*
* Copyright (c) 2022 ARATA Mizuki
* This file is part of LunarML.
*)
structure TypedSyntax :>
sig
datatype VId = MkVId of string * int
datatype TyVar = MkTyVar of string * int
datatype TyName = MkTyName of string * int
datatype StrId = MkStrId of string * int
datatype FunId = MkFunId of string * int
datatype LongVId =
MkShortVId of VId
| MkLongVId of StrId * Syntax.StrId list * Syntax.VId
datatype LongStrId = MkLongStrId of StrId * Syntax.StrId list
val eqUTyVar: TyVar * TyVar -> bool
val eqTyName: TyName * TyName -> bool
val eqVId: VId * VId -> bool
val tyVarAdmitsEquality: TyVar -> bool
structure TyVarSet: ORD_SET where type Key.ord_key = TyVar
structure TyVarMap: ORD_MAP where type Key.ord_key = TyVar
structure VIdKey: ORD_KEY where type ord_key = VId
structure VIdSet: ORD_SET where type Key.ord_key = VId
structure VIdMap: ORD_MAP where type Key.ord_key = VId
structure VIdTable: MONO_HASH_TABLE where type Key.hash_key = VId
structure VIdSCC:
sig
val components: ('node -> VIdSet.set) * 'node VIdMap.map -> VIdSet.set list
end
structure TyNameSet: ORD_SET where type Key.ord_key = TyName
structure TyNameMap: ORD_MAP where type Key.ord_key = TyName
type level = int
datatype UnaryConstraint =
IsRecord of (* excluded fields *) Syntax.Label list
| IsEqType
| IsIntegral (* Int, Word; div, mod; defaults to int *)
| IsSignedReal (* Int, Real; abs; defaults to int *)
| IsRing (* Int, Word, Real; *, +, -, ~; defaults to int *)
| IsOrdered (* NumTxt; <, >, <=, >=; defaults to int *)
| IsInt (* Int; defaults to int *)
| IsWord (* Word; defaults to word *)
| IsReal (* Real; defaults to real *)
| IsChar (* Char; defaults to char *)
| IsString (* String; defaults to string *)
datatype class =
Record of (* excluded fields *) Syntax.LabelSet.set
| NumTxt (* Int, Word, Real, Char, String *)
| IntWordReal (* Int, Word, Real *)
| IntWord (* Int, Word *)
| IntReal (* Int, Real *)
| Int
| Word
| Real
| Char
| String
type tyvar_constraint =
{sourceSpan: SourcePos.span, equalityRequired: bool, class: class option}
datatype Ty =
TyVar of SourcePos.span * TyVar (* named type variable *)
| AnonymousTyVar of
SourcePos.span
* TyVarData ref (* anonymous type variable; only used during type checking *)
| RecordType of
SourcePos.span * Ty Syntax.LabelMap.map (* record type expression *)
| TyCon of SourcePos.span * Ty list * TyName (* type construction *)
| FnType of SourcePos.span * Ty * Ty (* function type expression *)
| RecordExtType of
SourcePos.span
* Ty Syntax.LabelMap.map
* Ty (* only used during type checking *)
and TyVarData =
Unbound of tyvar_constraint * level
| Link of Ty
type AnonymousTyVar = TyVarData ref
val PairType: SourcePos.span * Ty * Ty -> Ty
val TupleType: SourcePos.span * Ty list -> Ty
datatype TypeFunction = TypeFunction of TyVar list * Ty
datatype TypeScheme = TypeScheme of (TyVar * UnaryConstraint option) list * Ty
type ValEnv =
(TypeScheme * Syntax.ValueConstructorInfo Syntax.IdStatus) Syntax.VIdMap.map
val emptyValEnv: ValEnv
type TypeStructure = {typeFunction: TypeFunction, valEnv: ValEnv}
datatype Signature' =
MkSignature of
{ valMap: (TypeScheme * Syntax.ValueConstructorInfo Syntax.IdStatus) Syntax.VIdMap.map
, tyConMap: TypeStructure Syntax.TyConMap.map
, strMap: Signature' Syntax.StrIdMap.map
}
type Signature =
{ valMap: (TypeScheme * Syntax.ValueConstructorInfo Syntax.IdStatus) Syntax.VIdMap.map
, tyConMap: TypeStructure Syntax.TyConMap.map
, strMap: Signature' Syntax.StrIdMap.map
}
type QSignature =
{ s: Signature
, bound: {arity: int, admitsEquality: bool, longtycon: Syntax.LongTyCon} TyNameMap.map
}
type PackedSignature =
{ s: Signature
, bound: {tyname: TyName, arity: int, admitsEquality: bool} list
}
type FunSig =
{ bound:
{ tyname: TyName
, arity: int
, admitsEquality: bool
, longtycon: Syntax.LongTyCon
} list
, paramSig: Signature
, resultSig: PackedSignature
}
datatype Pat =
WildcardPat of SourcePos.span
| SConPat of SourcePos.span * Syntax.SCon * Ty (* special constant *)
| VarPat of SourcePos.span * VId * Ty (* variable *)
| RecordPat of
{ sourceSpan: SourcePos.span
, fields: (Syntax.Label * Pat) list
, ellipsis: Pat option
, wholeRecordType: Ty
}
| ConPat of
{ sourceSpan: SourcePos.span
, longvid: LongVId
, payload: (Ty * Pat) option
, tyargs: Ty list
, valueConstructorInfo: Syntax.ValueConstructorInfo option
}
| TypedPat of SourcePos.span * Pat * Ty (* typed *)
| LayeredPat of SourcePos.span * VId * Ty * Pat (* layered *)
| VectorPat of
SourcePos.span * Pat vector * bool * Ty (* [extension] vector pattern *)
datatype TypBind = TypBind of SourcePos.span * TyVar list * Syntax.TyCon * Ty
datatype ConBind =
ConBind of SourcePos.span * VId * Ty option * Syntax.ValueConstructorInfo
datatype DatBind =
DatBind of
SourcePos.span
* TyVar list
* TyName
* ConBind list
* (* admits equality? *) bool
datatype ExBind =
ExBind of SourcePos.span * VId * Ty option (* <op> vid <of ty> *)
| ExReplication of SourcePos.span * VId * LongVId * Ty option
datatype match_type = CASE | VAL | HANDLE
datatype valdesc_origin = VALDESC_COMMENT | VALDESC_SEQUENCE
datatype Exp =
SConExp of SourcePos.span * Syntax.SCon * Ty (* special constant *)
| VarExp of
SourcePos.span
* LongVId
* Syntax.ValueConstructorInfo Syntax.IdStatus
* (Ty * UnaryConstraint option) list (* identifiers with type arguments *)
| RecordExp of SourcePos.span * (Syntax.Label * Exp) list (* record *)
| RecordExtExp of
{ sourceSpan: SourcePos.span
, fields: (Syntax.Label * Exp) list
, baseExp: Exp
, baseTy: Ty
} (* record extension *)
| LetInExp of SourcePos.span * Dec list * Exp (* local declaration *)
| AppExp of SourcePos.span * Exp * Exp (* function, argument *)
| TypedExp of SourcePos.span * Exp * Ty
| HandleExp of SourcePos.span * Exp * (Pat * Exp) list * Ty
| RaiseExp of SourcePos.span * Ty * Exp (* result type, exception *)
| IfThenElseExp of SourcePos.span * Exp * Exp * Exp
| CaseExp of
{ sourceSpan: SourcePos.span
, subjectExp: Exp
, subjectTy: Ty
, matches: (Pat * Exp) list
, matchType: match_type
, resultTy: Ty
}
| FnExp of
SourcePos.span * VId * Ty * Exp (* parameter name, parameter type, body *)
| ProjectionExp of
{ sourceSpan: SourcePos.span
, label: Syntax.Label
, recordTy: Ty
, fieldTy: Ty
}
| ListExp of SourcePos.span * Exp vector * Ty
| VectorExp of SourcePos.span * Exp vector * Ty
| PrimExp of SourcePos.span * Primitives.PrimOp * Ty vector * Exp vector
| BogusExp of SourcePos.span * Ty (* undefined identifier *)
and Dec =
ValDec of SourcePos.span * ValBind list (* non-recursive *)
| RecValDec of SourcePos.span * ValBind list (* recursive (val rec) *)
| IgnoreDec of SourcePos.span * Exp * Ty (* val _ = ... *)
| TypeDec of SourcePos.span * TypBind list (* not used by the type checker *)
| DatatypeDec of SourcePos.span * DatBind list
| ExceptionDec of SourcePos.span * ExBind list
| OverloadDec of
SourcePos.span
* Syntax.OverloadClass
* TyName
* Exp Syntax.OverloadKeyMap.map
| EqualityDec of SourcePos.span * TyVar list * TyName * Exp
| ValDescDec of
{ sourceSpan: SourcePos.span
, expected: TypeScheme
, actual: TypeScheme
, origin: valdesc_origin
}
| ESImportDec of
{ sourceSpan: SourcePos.span
, pure: bool
, specs: (Syntax.ESImportName * VId * Ty) list
, moduleName: string
}
and ValBind =
TupleBind of
SourcePos.span
* (VId * Ty) list
* Exp (* monomorphic binding; produced during type-check *)
| PolyVarBind of
SourcePos.span
* VId
* TypeScheme
* Exp (* polymorphic binding; produced during type-check *)
datatype StrExp =
StructExp of
{ sourceSpan: SourcePos.span
, valMap: (LongVId * Syntax.ValueConstructorInfo Syntax.IdStatus) Syntax.VIdMap.map
, tyConMap: TypeStructure Syntax.TyConMap.map
, strMap: LongStrId Syntax.StrIdMap.map
}
| StrIdExp of SourcePos.span * LongStrId
| PackedStrExp of
{ sourceSpan: SourcePos.span
, strExp: StrExp
, payloadTypes: TypeFunction list
, packageSig: PackedSignature
}
| FunctorAppExp of
{ sourceSpan: SourcePos.span
, funId: FunId
, argumentTypes: {typeFunction: TypeFunction, admitsEquality: bool} list
, argumentStr: StrExp
, packageSig: PackedSignature
}
| LetInStrExp of SourcePos.span * StrDec list * StrExp
and StrDec =
CoreDec of SourcePos.span * Dec
| StrBindDec of SourcePos.span * StrId * StrExp * PackedSignature
type FunExp =
{tyname: TyName, arity: int, admitsEquality: bool} list
* StrId
* Signature
* StrExp
datatype TopDec = StrDec of StrDec | FunDec of FunId * FunExp
type Program = (TopDec list) list
val TupleExp: SourcePos.span * Exp list -> Exp
val getSourceSpanOfTy: Ty -> SourcePos.span
val getSourceSpanOfExp: Exp -> SourcePos.span
structure PrettyPrint:
sig
val print_TyVar: TyVar -> string
val print_AnonymousTyVar: AnonymousTyVar -> string
val print_TyName: TyName -> string
val print_Ty: Ty -> string
end
val print_VId: VId -> string
val print_LongVId: LongVId -> string
val print_TyVar: TyVar -> string
val print_AnonymousTyVar: AnonymousTyVar -> string
val print_TyName: TyName -> string
val print_Ty: Ty -> string
val freeTyVarsInTy: TyVarSet.set * Ty -> TyVarSet.set
val freeAnonymousTyVarsInTy: Ty -> AnonymousTyVar list
val applySubstTy: Ty TyVarMap.map -> Ty -> Ty
val applySubstTyInExpOrDec: Ty TyVarMap.map
-> {doExp: Exp -> Exp, doDec: Dec -> Dec}
val substVId:
(SourcePos.span
* Syntax.ValueConstructorInfo Syntax.IdStatus
* (Ty * UnaryConstraint option) list
-> Exp) VIdMap.map
-> { doExp: Exp -> Exp
, doDec: Dec -> VIdSet.set * Dec
, doDecs: Dec list -> VIdSet.set * Dec list
}
val forceTy: Ty -> Ty
val forceTyIn:
{ nextTyVar: int ref
, nextVId: 'a
, matchContext: 'b
, messageHandler: 'c
, languageOptions: 'd
}
-> { doExp: Exp -> Exp
, doDec: Dec -> Dec
, doDecs: Dec list -> Dec list
, doTopDec: TopDec -> TopDec
, doTopDecs: TopDec list -> TopDec list
}
val freeTyVarsInDecs: 'a * Dec list -> AnonymousTyVar list
val filterVarsInPat: (VId -> bool) -> Pat -> Pat
val renameVarsInPat: VId VIdMap.map -> Pat -> Pat
end =
struct
datatype VId = MkVId of string * int
datatype TyVar = MkTyVar of string * int
datatype TyName = MkTyName of string * int
datatype StrId = MkStrId of string * int
datatype FunId = MkFunId of string * int
datatype LongVId =
MkShortVId of VId
| MkLongVId of StrId * Syntax.StrId list * Syntax.VId
datatype LongStrId = MkLongStrId of StrId * Syntax.StrId list
fun eqUTyVar (MkTyVar (name, a), MkTyVar (name', b)) =
name = name' andalso a = b
fun eqTyName (MkTyName (_, a), MkTyName (_, b)) = a = b
fun eqVId (a, b: VId) = a = b
fun tyVarAdmitsEquality (MkTyVar (name, _)) = String.isPrefix "''" name
structure TyVarKey =
struct
type ord_key = TyVar
fun compare (MkTyVar (x, a), MkTyVar (y, b)) =
(case Int.compare (a, b) of
EQUAL => String.compare (x, y)
| ord => ord)
end : ORD_KEY
structure TyVarSet = RedBlackSetFn(TyVarKey)
structure TyVarMap = RedBlackMapFn(TyVarKey)
structure VIdKey =
struct
type ord_key = VId
fun compare (MkVId (x, a), MkVId (y, b)) =
case Int.compare (a, b) of
EQUAL => String.compare (x, y)
| ord => ord
end : ORD_KEY
structure VIdSet = RedBlackSetFn(VIdKey)
structure VIdMap = RedBlackMapFn(VIdKey)
structure VIdTable =
HashTableFn
(struct
type hash_key = VId
fun hashVal (MkVId (_, i)) = Word.fromInt i
val sameKey = eqVId
end)
structure VIdSCC =
StronglyConnectedComponents
(type t = VId structure Map = VIdMap structure Set = VIdSet)
structure TyNameKey =
struct
type ord_key = TyName
fun compare (MkTyName (x, a), MkTyName (y, b)) =
case String.compare (x, y) of
EQUAL => Int.compare (a, b)
| ord => ord
end : ORD_KEY
structure TyNameSet = RedBlackSetFn(TyNameKey)
structure TyNameMap = RedBlackMapFn(TyNameKey)
structure StrIdKey =
struct
type ord_key = StrId
fun compare (MkStrId (x, a), MkStrId (y, b)) =
case String.compare (x, y) of
EQUAL => Int.compare (a, b)
| ord => ord
end : ORD_KEY
(* structure StrIdSet = RedBlackSetFn(StrIdKey) *)
(* structure StrIdMap = RedBlackMapFn(StrIdKey) *)
type level = int
datatype UnaryConstraint =
IsRecord of (* excluded fields *) Syntax.Label list
| IsEqType
| IsIntegral (* Int, Word; div, mod; defaults to int *)
| IsSignedReal (* Int, Real; abs; defaults to int *)
| IsRing (* Int, Word, Real; *, +, -, ~; defaults to int *)
| IsOrdered (* NumTxt; <, >, <=, >=; defaults to int *)
| IsInt (* Int; defaults to int *)
| IsWord (* Word; defaults to word *)
| IsReal (* Real; defaults to real *)
| IsChar (* Char; defaults to char *)
| IsString (* String; defaults to string *)
datatype class =
Record of (* excluded fields *) Syntax.LabelSet.set
| NumTxt (* Int, Word, Real, Char, String *)
| IntWordReal (* Int, Word, Real *)
| IntWord (* Int, Word *)
| IntReal (* Int, Real *)
| Int
| Word
| Real
| Char
| String
type tyvar_constraint =
{sourceSpan: SourcePos.span, equalityRequired: bool, class: class option}
datatype Ty =
TyVar of SourcePos.span * TyVar (* named type variable *)
| AnonymousTyVar of
SourcePos.span
* TyVarData ref (* anonymous type variable; only used during type checking *)
| RecordType of
SourcePos.span * Ty Syntax.LabelMap.map (* record type expression *)
| TyCon of SourcePos.span * Ty list * TyName (* type construction *)
| FnType of SourcePos.span * Ty * Ty (* function type expression *)
| RecordExtType of
SourcePos.span
* Ty Syntax.LabelMap.map
* Ty (* only used during type checking *)
and TyVarData =
Unbound of tyvar_constraint * level
| Link of Ty
type AnonymousTyVar = TyVarData ref
fun PairType (span, a, b) =
RecordType (span, Syntax.LabelMapFromList
[(Syntax.NumericLabel 1, a), (Syntax.NumericLabel 2, b)])
datatype TypeFunction = TypeFunction of TyVar list * Ty
datatype TypeScheme = TypeScheme of (TyVar * UnaryConstraint option) list * Ty
type ValEnv =
(TypeScheme * Syntax.ValueConstructorInfo Syntax.IdStatus) Syntax.VIdMap.map
val emptyValEnv: ValEnv = Syntax.VIdMap.empty
type TypeStructure = {typeFunction: TypeFunction, valEnv: ValEnv}
datatype Signature' = MkSignature of Signature
withtype Signature =
{ valMap: (TypeScheme * Syntax.ValueConstructorInfo Syntax.IdStatus) Syntax.VIdMap.map
, tyConMap: TypeStructure Syntax.TyConMap.map
, strMap: Signature' Syntax.StrIdMap.map
}
type QSignature =
{ s: Signature
, bound: {arity: int, admitsEquality: bool, longtycon: Syntax.LongTyCon} TyNameMap.map
}
type PackedSignature =
{ s: Signature
, bound: {tyname: TyName, arity: int, admitsEquality: bool} list
}
type FunSig =
{ bound:
{ tyname: TyName
, arity: int
, admitsEquality: bool
, longtycon: Syntax.LongTyCon
} list
, paramSig: Signature
, resultSig: PackedSignature
}
datatype Pat =
WildcardPat of SourcePos.span
| SConPat of SourcePos.span * Syntax.SCon * Ty (* special constant *)
| VarPat of SourcePos.span * VId * Ty (* variable *)
| RecordPat of
{ sourceSpan: SourcePos.span
, fields: (Syntax.Label * Pat) list
, ellipsis: Pat option
, wholeRecordType: Ty
}
| ConPat of
{ sourceSpan: SourcePos.span
, longvid: LongVId
, payload: (Ty * Pat) option
, tyargs: Ty list
, valueConstructorInfo: Syntax.ValueConstructorInfo option
}
| TypedPat of SourcePos.span * Pat * Ty (* typed *)
| LayeredPat of SourcePos.span * VId * Ty * Pat (* layered *)
| VectorPat of
SourcePos.span * Pat vector * bool * Ty (* [extension] vector pattern *)
datatype TypBind = TypBind of SourcePos.span * TyVar list * Syntax.TyCon * Ty
datatype ConBind =
ConBind of SourcePos.span * VId * Ty option * Syntax.ValueConstructorInfo
datatype DatBind =
DatBind of
SourcePos.span
* TyVar list
* TyName
* ConBind list
* (* admits equality? *) bool
datatype ExBind =
ExBind of SourcePos.span * VId * Ty option (* <op> vid <of ty> *)
| ExReplication of SourcePos.span * VId * LongVId * Ty option
datatype match_type = CASE | VAL | HANDLE
datatype valdesc_origin = VALDESC_COMMENT | VALDESC_SEQUENCE
datatype Exp =
SConExp of SourcePos.span * Syntax.SCon * Ty (* special constant *)
| VarExp of
SourcePos.span
* LongVId
* Syntax.ValueConstructorInfo Syntax.IdStatus
* (Ty * UnaryConstraint option) list (* identifiers with type arguments *)
| RecordExp of SourcePos.span * (Syntax.Label * Exp) list (* record *)
| RecordExtExp of
{ sourceSpan: SourcePos.span
, fields: (Syntax.Label * Exp) list
, baseExp: Exp
, baseTy: Ty
} (* record extension *)
| LetInExp of SourcePos.span * Dec list * Exp (* local declaration *)
| AppExp of SourcePos.span * Exp * Exp (* function, argument *)
| TypedExp of SourcePos.span * Exp * Ty
| HandleExp of SourcePos.span * Exp * (Pat * Exp) list * Ty
| RaiseExp of SourcePos.span * Ty * Exp (* result type, exception *)
| IfThenElseExp of SourcePos.span * Exp * Exp * Exp
| CaseExp of
{ sourceSpan: SourcePos.span
, subjectExp: Exp
, subjectTy: Ty
, matches: (Pat * Exp) list
, matchType: match_type
, resultTy: Ty
}
| FnExp of
SourcePos.span * VId * Ty * Exp (* parameter name, parameter type, body *)
| ProjectionExp of
{ sourceSpan: SourcePos.span
, label: Syntax.Label
, recordTy: Ty
, fieldTy: Ty
}
| ListExp of SourcePos.span * Exp vector * Ty
| VectorExp of SourcePos.span * Exp vector * Ty
| PrimExp of SourcePos.span * Primitives.PrimOp * Ty vector * Exp vector
| BogusExp of SourcePos.span * Ty (* undefined identifier *)
and Dec =
ValDec of SourcePos.span * ValBind list (* non-recursive *)
| RecValDec of SourcePos.span * ValBind list (* recursive (val rec) *)
| IgnoreDec of SourcePos.span * Exp * Ty (* val _ = ... *)
| TypeDec of SourcePos.span * TypBind list (* not used by the type checker *)
| DatatypeDec of SourcePos.span * DatBind list
| ExceptionDec of SourcePos.span * ExBind list
| OverloadDec of
SourcePos.span
* Syntax.OverloadClass
* TyName
* Exp Syntax.OverloadKeyMap.map
| EqualityDec of SourcePos.span * TyVar list * TyName * Exp
| ValDescDec of
{ sourceSpan: SourcePos.span
, expected: TypeScheme
, actual: TypeScheme
, origin: valdesc_origin
}
| ESImportDec of
{ sourceSpan: SourcePos.span
, pure: bool
, specs: (Syntax.ESImportName * VId * Ty) list
, moduleName: string
}
and ValBind =
TupleBind of
SourcePos.span
* (VId * Ty) list
* Exp (* monomorphic binding; produced during type-check *)
| PolyVarBind of
SourcePos.span
* VId
* TypeScheme
* Exp (* polymorphic binding; produced during type-check *)
datatype StrExp =
StructExp of
{ sourceSpan: SourcePos.span
, valMap: (LongVId * Syntax.ValueConstructorInfo Syntax.IdStatus) Syntax.VIdMap.map
, tyConMap: TypeStructure Syntax.TyConMap.map
, strMap: LongStrId Syntax.StrIdMap.map
}
| StrIdExp of SourcePos.span * LongStrId
| PackedStrExp of
{ sourceSpan: SourcePos.span
, strExp: StrExp
, payloadTypes: TypeFunction list
, packageSig: PackedSignature
}
| FunctorAppExp of
{ sourceSpan: SourcePos.span
, funId: FunId
, argumentTypes: {typeFunction: TypeFunction, admitsEquality: bool} list
, argumentStr: StrExp
, packageSig: PackedSignature
}
| LetInStrExp of SourcePos.span * StrDec list * StrExp
and StrDec =
CoreDec of SourcePos.span * Dec
| StrBindDec of SourcePos.span * StrId * StrExp * PackedSignature
type FunExp =
{tyname: TyName, arity: int, admitsEquality: bool} list
* StrId
* Signature
* StrExp
datatype TopDec = StrDec of StrDec | FunDec of FunId * FunExp
type Program = (TopDec list) list
fun TupleType (span, xs) =
RecordType (span, #2
(List.foldl
(fn (ty, (i, m)) =>
(i + 1, Syntax.LabelMap.insert (m, Syntax.NumericLabel i, ty)))
(1, Syntax.LabelMap.empty) xs))
local
fun doFields _ nil = nil
| doFields i (x :: xs) =
(Syntax.NumericLabel i, x) :: doFields (i + 1) xs
in
fun TupleExp (span, xs) =
RecordExp (span, doFields 1 xs)
end
fun getSourceSpanOfTy (TyVar (span, _)) = span
| getSourceSpanOfTy (AnonymousTyVar (span, _)) = span
| getSourceSpanOfTy (RecordType (span, _)) = span
| getSourceSpanOfTy (TyCon (span, _, _)) = span
| getSourceSpanOfTy (FnType (span, _, _)) = span
| getSourceSpanOfTy (RecordExtType (span, _, _)) = span
fun getSourceSpanOfExp (SConExp (span, _, _)) = span
| getSourceSpanOfExp (VarExp (span, _, _, _)) = span
| getSourceSpanOfExp (RecordExp (span, _)) = span
| getSourceSpanOfExp (RecordExtExp {sourceSpan, ...}) = sourceSpan
| getSourceSpanOfExp (LetInExp (span, _, _)) = span
| getSourceSpanOfExp (AppExp (span, _, _)) = span
| getSourceSpanOfExp (TypedExp (span, _, _)) = span
| getSourceSpanOfExp (HandleExp (span, _, _, _)) = span
| getSourceSpanOfExp (RaiseExp (span, _, _)) = span
| getSourceSpanOfExp (IfThenElseExp (span, _, _, _)) = span
| getSourceSpanOfExp (CaseExp {sourceSpan, ...}) = sourceSpan
| getSourceSpanOfExp (FnExp (span, _, _, _)) = span
| getSourceSpanOfExp (ProjectionExp {sourceSpan, ...}) = sourceSpan
| getSourceSpanOfExp (ListExp (span, _, _)) = span
| getSourceSpanOfExp (VectorExp (span, _, _)) = span
| getSourceSpanOfExp (PrimExp (span, _, _, _)) = span
| getSourceSpanOfExp (BogusExp (span, _)) = span
(* pretty printing *)
structure PrettyPrint =
struct
fun print_VId (MkVId (name, n)) = name ^ "@" ^ Int.toString n
fun print_StrId (MkStrId (name, n)) = name ^ "@" ^ Int.toString n
fun print_FunId (MkFunId (name, n)) = name ^ "@" ^ Int.toString n
fun print_LongVId (MkShortVId (vid)) = print_VId vid
| print_LongVId (MkLongVId (strid, strids, vid)) =
"MkLongVId(" ^ print_StrId strid ^ ","
^ Syntax.print_list Syntax.print_StrId strids ^ ","
^ Syntax.print_VId vid ^ ")"
fun print_LongStrId (MkLongStrId (strid, strids)) =
String.concatWith "."
(print_StrId strid :: List.map (fn Syntax.MkStrId name => name) strids)
fun print_TyVar (MkTyVar (tvname, n)) =
"MkTyVar(\"" ^ String.toString tvname ^ "\"," ^ Int.toString n ^ ")"
fun print_AnonymousTyVar _ =
"<AnonymousTyVar>" (* (MkAnonymousTyVar n) = "AnonymousTyVar(" ^ Int.toString n ^ ")" *)
fun print_TyName (MkTyName ("int", 0)) = "primTyName_int"
| print_TyName (MkTyName ("word", 1)) = "primTyName_word"
| print_TyName (MkTyName ("real", 2)) = "primTyName_real"
| print_TyName (MkTyName ("string", 3)) = "primTyName_string"
| print_TyName (MkTyName ("char", 4)) = "primTyName_char"
| print_TyName (MkTyName ("exn", 5)) = "primTyName_exn"
| print_TyName (MkTyName ("bool", 6)) = "primTyName_bool"
| print_TyName (MkTyName ("ref", 7)) = "primTyName_ref"
| print_TyName (MkTyName ("list", 8)) = "primTyName_list"
| print_TyName (MkTyName (tyconname, n)) =
"MkTyName(\"" ^ String.toString tyconname ^ "\"," ^ Int.toString n
^ ")"
fun print_Ty (TyVar (_, x)) =
"TyVar(" ^ print_TyVar x ^ ")"
| print_Ty (AnonymousTyVar (_, ref (Unbound (_, level)))) =
"AnonymousTyVar(" ^ Int.toString level ^ ")"
| print_Ty (AnonymousTyVar (_, ref (Link ty))) = "*" ^ print_Ty ty
| print_Ty (RecordType (_, xs)) =
let
val xs = Syntax.LabelMap.listItemsi xs
in
case Syntax.extractTuple (1, xs) of
NONE =>
"RecordType "
^
Syntax.print_list
(Syntax.print_pair (Syntax.print_Label, print_Ty)) xs
| SOME ys => "TupleType " ^ Syntax.print_list print_Ty ys
end
| print_Ty (RecordExtType (_, xs, baseTy)) =
let
val xs = Syntax.LabelMap.listItemsi xs
in
"RecordExtType("
^
Syntax.print_list (Syntax.print_pair (Syntax.print_Label, print_Ty))
xs ^ "," ^ print_Ty baseTy ^ ")"
end
| print_Ty (TyCon (_, [], MkTyName ("int", 0))) = "primTy_int"
| print_Ty (TyCon (_, [], MkTyName ("word", 1))) = "primTy_word"
| print_Ty (TyCon (_, [], MkTyName ("real", 2))) = "primTy_real"
| print_Ty (TyCon (_, [], MkTyName ("string", 3))) = "primTy_string"
| print_Ty (TyCon (_, [], MkTyName ("char", 4))) = "primTy_char"
| print_Ty (TyCon (_, [], MkTyName ("exn", 5))) = "primTy_exn"
| print_Ty (TyCon (_, [], MkTyName ("bool", 6))) = "primTy_bool"
| print_Ty (TyCon (_, x, y)) =
"TyCon(" ^ Syntax.print_list print_Ty x ^ "," ^ print_TyName y ^ ")"
| print_Ty (FnType (_, x, y)) =
"FnType(" ^ print_Ty x ^ "," ^ print_Ty y ^ ")"
fun print_Pat (WildcardPat _) = "WildcardPat"
| print_Pat (SConPat (_, x, _)) =
"SConPat(" ^ Syntax.print_SCon x ^ ")"
| print_Pat (VarPat (_, vid, ty)) =
"VarPat(" ^ print_VId vid ^ "," ^ print_Ty ty ^ ")"
| print_Pat (TypedPat (_, pat, ty)) =
"TypedPat(" ^ print_Pat pat ^ "," ^ print_Ty ty ^ ")"
| print_Pat (LayeredPat (_, vid, ty, pat)) =
"TypedPat(" ^ print_VId vid ^ "," ^ print_Ty ty ^ "," ^ print_Pat pat
^ ")"
| print_Pat (ConPat {longvid, payload, tyargs, ...}) =
"ConPat(" ^ print_LongVId longvid ^ ","
^
Syntax.print_option (Syntax.print_pair (print_Ty, print_Pat)) payload
^ "," ^ Syntax.print_list print_Ty tyargs ^ ")"
| print_Pat (RecordPat {fields = x, ellipsis = NONE, ...}) =
(case Syntax.extractTuple (1, x) of
NONE =>
"RecordPat("
^
Syntax.print_list
(Syntax.print_pair (Syntax.print_Label, print_Pat)) x
^ ",NONE)"
| SOME ys => "TuplePat " ^ Syntax.print_list print_Pat ys)
| print_Pat (RecordPat {fields = x, ellipsis = SOME basePat, ...}) =
"RecordPat("
^
Syntax.print_list (Syntax.print_pair (Syntax.print_Label, print_Pat))
x ^ ",SOME(" ^ print_Pat basePat ^ "))"
| print_Pat (VectorPat _) = "VectorPat"
(* | print_Pat _ = "<Pat>" *)
fun print_Exp (SConExp (_, x, _)) =
"SConExp(" ^ Syntax.print_SCon x ^ ")"
| print_Exp (VarExp (_, x, idstatus, tyargs)) =
"VarExp(" ^ print_LongVId x ^ "," ^ Syntax.print_IdStatus idstatus
^ ","
^
Syntax.print_list
(Syntax.print_pair
(print_Ty, Syntax.print_option print_UnaryConstraint)) tyargs
^ ")"
| print_Exp (RecordExp (_, x)) =
(case Syntax.extractTuple (1, x) of
NONE =>
"RecordExp "
^
Syntax.print_list
(Syntax.print_pair (Syntax.print_Label, print_Exp)) x
| SOME ys => "TupleExp " ^ Syntax.print_list print_Exp ys)
| print_Exp (RecordExtExp {sourceSpan = _, fields, baseExp, baseTy = _}) =
"RecordExtExp("
^
Syntax.print_list (Syntax.print_pair (Syntax.print_Label, print_Exp))
fields ^ "," ^ print_Exp baseExp ^ ")"
| print_Exp (LetInExp (_, decls, x)) =
"LetInExp(" ^ Syntax.print_list print_Dec decls ^ "," ^ print_Exp x
^ ")"
| print_Exp (AppExp (_, x, y)) =
"AppExp(" ^ print_Exp x ^ "," ^ print_Exp y ^ ")"
| print_Exp (TypedExp (_, x, y)) =
"TypedExp(" ^ print_Exp x ^ "," ^ print_Ty y ^ ")"
| print_Exp (HandleExp (_, x, y, _)) =
"HandleExp(" ^ print_Exp x ^ ","
^ Syntax.print_list (Syntax.print_pair (print_Pat, print_Exp)) y ^ ")"
| print_Exp (RaiseExp (_, ty, x)) =
"RaiseExp(" ^ print_Ty ty ^ "," ^ print_Exp x ^ ")"
| print_Exp (IfThenElseExp (_, x, y, z)) =
"IfThenElseExp(" ^ print_Exp x ^ "," ^ print_Exp y ^ "," ^ print_Exp z
^ ")"
| print_Exp (CaseExp {subjectExp, subjectTy, matches, ...}) =
"CaseExp(" ^ print_Exp subjectExp ^ "," ^ print_Ty subjectTy ^ ","
^ Syntax.print_list (Syntax.print_pair (print_Pat, print_Exp)) matches
^ ")"
| print_Exp (FnExp (_, pname, pty, body)) =
"FnExp(" ^ print_VId pname ^ "," ^ print_Ty pty ^ "," ^ print_Exp body
^ ")"
| print_Exp
(ProjectionExp
{label = label, recordTy = recordTy, fieldTy = fieldTy, ...}) =
"ProjectionExp{label=" ^ Syntax.print_Label label ^ ",recordTy="
^ print_Ty recordTy ^ ",fieldTy=" ^ print_Ty fieldTy ^ "}"
| print_Exp (ListExp _) = "ListExp"
| print_Exp (VectorExp _) = "VectorExp"
| print_Exp (PrimExp _) = "PrimExp"
| print_Exp (BogusExp _) = "BogusExp"
and print_Dec (ValDec (_, valbinds)) =
"ValDec(" ^ Syntax.print_list print_ValBind valbinds ^ ")"
| print_Dec (RecValDec (_, valbinds)) =
"RecValDec(" ^ Syntax.print_list print_ValBind valbinds ^ ")"
| print_Dec (IgnoreDec _) = "IgnoreDec"
| print_Dec (TypeDec (_, typbinds)) =
"TypeDec(" ^ Syntax.print_list print_TypBind typbinds ^ ")"
| print_Dec (DatatypeDec (_, datbinds)) =
"DatatypeDec(" ^ Syntax.print_list print_DatBind datbinds ^ ")"
| print_Dec (ExceptionDec (_, _)) = "ExceptionDec"
| print_Dec (OverloadDec _) = "OverloadDec"
| print_Dec (EqualityDec _) = "EqualityDec"
| print_Dec (ValDescDec _) = "ValDescDec"
| print_Dec (ESImportDec _) = "ESImportDec"
and print_TypBind (TypBind (_, tyvars, tycon, ty)) =
"TypBind(" ^ Syntax.print_list print_TyVar tyvars ^ ","
^ Syntax.print_TyCon tycon ^ "," ^ print_Ty ty ^ ")"
and print_DatBind (DatBind (_, tyvars, tycon, conbinds, _)) =
"DatBind(" ^ Syntax.print_list print_TyVar tyvars ^ ","
^ print_TyName tycon ^ "," ^ Syntax.print_list print_ConBind conbinds
^ ")"
and print_ConBind (ConBind (_, vid, NONE, _)) =
"ConBind(" ^ print_VId vid ^ ",NONE)"
| print_ConBind (ConBind (_, vid, SOME ty, _)) =
"ConBind(" ^ print_VId vid ^ ",SOME " ^ print_Ty ty ^ ")"
and print_ValBind (TupleBind (_, xs, exp)) =
"TupleBind("
^ Syntax.print_list (Syntax.print_pair (print_VId, print_Ty)) xs ^ ","
^ print_Exp exp ^ ")"
| print_ValBind (PolyVarBind (_, name, tysc, exp)) =
"PolyVarBind(" ^ print_VId name ^ "," ^ print_TypeScheme tysc ^ ","
^ print_Exp exp ^ ")"
(* and print_TyVarMap print_elem x = Syntax.print_list (Syntax.print_pair (print_TyVar,print_elem)) (TyVarMap.foldri (fn (k,x,ys) => (k,x) :: ys) [] x) *)
(* and print_VIdMap print_elem x = Syntax.print_list (Syntax.print_pair (print_VId,print_elem)) (VIdMap.foldri (fn (k,x,ys) => (k,x) :: ys) [] x) *)
and print_UnaryConstraint (IsRecord labels) =
"IsEqType(" ^ Syntax.print_list Syntax.print_Label labels ^ ")"
| print_UnaryConstraint IsEqType = "IsEqType"
| print_UnaryConstraint IsIntegral = "IsIntegral"
| print_UnaryConstraint IsSignedReal = "IsSignedReal"
| print_UnaryConstraint IsRing = "IsRing"
| print_UnaryConstraint IsOrdered = "IsOrdered"
| print_UnaryConstraint IsInt = "IsInt"
| print_UnaryConstraint IsWord = "IsWord"
| print_UnaryConstraint IsReal = "IsReal"
| print_UnaryConstraint IsChar = "IsChar"
| print_UnaryConstraint IsString = "IsString"
and print_TypeScheme (TypeScheme (tyvars, ty)) =
"TypeScheme("
^
Syntax.print_list
(Syntax.print_pair
(print_TyVar, Syntax.print_option print_UnaryConstraint)) tyvars
^ "," ^ print_Ty ty ^ ")"
(* and print_ValEnv env = print_VIdMap (Syntax.print_pair (print_TypeScheme,Syntax.print_IdStatus)) env *)
(* fun print_TyVarSet x = Syntax.print_list print_TyVar (TyVarSet.foldr (fn (x,ys) => x :: ys) [] x) *)
(* fun print_TyNameMap print_elem x = Syntax.print_list (Syntax.print_pair (print_TyName,print_elem)) (TyNameMap.foldri (fn (k,x,ys) => (k,x) :: ys) [] x) *)
fun print_TypeFunction (TypeFunction (tyvars, ty)) =
"TypeFunction(" ^ Syntax.print_list print_TyVar tyvars ^ "," ^ print_Ty ty
^ ")"
(* val print_Decs = Syntax.print_list print_Dec *)
(* fun print_Constraint(EqConstr(span,ty1,ty2)) = "EqConstr(" ^ print_Ty ty1 ^ "," ^ print_Ty ty2 ^ ")"
| print_Constraint(UnaryConstraint(span,ty,ct)) = "Unary(" ^ print_Ty ty ^ "," ^ print_UnaryConstraint ct ^ ")" *)
fun print_Signature {valMap, tyConMap = _, strMap = _} =
"{valMap="
^
Syntax.print_list
(Syntax.print_pair
( Syntax.print_VId
, Syntax.print_pair (print_TypeScheme, Syntax.print_IdStatus)
)) (Syntax.VIdMap.listItemsi valMap) ^ ",tyConMap=..."
^ ",strMap=..." ^ "}"
fun print_PackedSignature {s, bound} =
"{s=" ^ print_Signature s ^ ",bound="
^
Syntax.print_list
(fn {tyname, arity, admitsEquality} =>
"(" ^ print_TyName tyname ^ "," ^ Int.toString arity ^ ","
^ Bool.toString admitsEquality ^ ")") bound ^ "}"
fun print_StrExp
(StructExp {sourceSpan = _, valMap = _, tyConMap = _, strMap = _}) =
"StructExp"
| print_StrExp (StrIdExp (_, longstrid)) =
"StrIdExp(" ^ print_LongStrId longstrid ^ ")"
| print_StrExp
(PackedStrExp {sourceSpan = _, strExp, payloadTypes, packageSig}) =
"PackedStrExp(" ^ print_StrExp strExp ^ ","
^ Syntax.print_list print_TypeFunction payloadTypes ^ ","
^ print_PackedSignature packageSig ^ ")"
| print_StrExp
(FunctorAppExp
{sourceSpan = _, funId, argumentTypes, argumentStr, packageSig}) =
"FunctorAppExp(" ^ print_FunId funId ^ ","
^
Syntax.print_list
(fn {typeFunction, admitsEquality} =>
"(" ^ print_TypeFunction typeFunction ^ ","
^ Bool.toString admitsEquality ^ ")") argumentTypes ^ ","
^ print_StrExp argumentStr ^ "," ^ print_PackedSignature packageSig
^ ")"
| print_StrExp (LetInStrExp (_, strdecs, strexp)) =
"LetInStrExp(" ^ Syntax.print_list print_StrDec strdecs ^ ","
^ print_StrExp strexp ^ ")"
and print_StrDec (CoreDec (_, dec)) = print_Dec dec
| print_StrDec (StrBindDec (_, strid, strexp, _)) =
"StrBindDec(" ^ print_StrId strid ^ "," ^ print_StrExp strexp ^ ")"
(* fun print_TopDec (StrDec strdec) = print_StrDec strdec
| print_TopDec (FunDec (funid, (typarams, strid, s, strexp))) = "FunDec(" ^ print_FunId funid ^ ",(" ^ Syntax.print_list (fn { tyname, arity, admitsEquality } => "(" ^ print_TyName tyname ^ "," ^ Int.toString arity ^ "," ^ Bool.toString admitsEquality ^ ")") typarams ^ "," ^ print_Signature s ^ "," ^ print_StrExp strexp ^ "))" *)
end (* structure PrettyPrint *)
open PrettyPrint
(*: val freeTyVarsInTy : TyVarSet.set * Ty -> TyVarSet.set *)
fun freeTyVarsInTy (bound, ty) =
(case ty of
TyVar (_, tv) =>
if TyVarSet.member (bound, tv) then TyVarSet.empty
else TyVarSet.singleton tv
| AnonymousTyVar (_, ref (Link ty)) => freeTyVarsInTy (bound, ty)
| AnonymousTyVar (_, ref (Unbound _)) => TyVarSet.empty
| RecordType (_, xs) =>
Syntax.LabelMap.foldl
(fn (ty, set) => TyVarSet.union (freeTyVarsInTy (bound, ty), set))
TyVarSet.empty xs
| TyCon (_, xs, _) =>
List.foldl
(fn (ty, set) => TyVarSet.union (freeTyVarsInTy (bound, ty), set))
TyVarSet.empty xs
| FnType (_, s, t) =>
TyVarSet.union (freeTyVarsInTy (bound, s), freeTyVarsInTy (bound, t))
| RecordExtType (_, xs, baseTy) =>
Syntax.LabelMap.foldl
(fn (ty, set) => TyVarSet.union (freeTyVarsInTy (bound, ty), set))
(freeTyVarsInTy (bound, baseTy)) xs)
(*: val freeAnonymousTyVarsInTy : Ty -> AnonymousTyVar list *)
fun freeAnonymousTyVarsInTy ty =
(case ty of
AnonymousTyVar (_, tv as ref (Unbound _)) => [tv]
| AnonymousTyVar (_, ref (Link ty)) => freeAnonymousTyVarsInTy ty
| TyVar _ => []
| RecordType (_, xs) =>
Syntax.LabelMap.foldl
(fn (ty, set) => freeAnonymousTyVarsInTy ty @ set) [] xs
| TyCon (_, xs, _) =>
List.foldl (fn (ty, set) => freeAnonymousTyVarsInTy ty @ set) [] xs
| FnType (_, s, t) => freeAnonymousTyVarsInTy s @ freeAnonymousTyVarsInTy t
| RecordExtType (_, xs, baseTy) =>
Syntax.LabelMap.foldl
(fn (ty, set) => freeAnonymousTyVarsInTy ty @ set)
(freeAnonymousTyVarsInTy baseTy) xs)
(*: val applySubstTy : Ty TyVarMap.map -> Ty -> Ty *)
fun applySubstTy subst =
let
fun substTy (ty as TyVar (_, tv')) =
(case TyVarMap.find (subst, tv') of
NONE => ty
| SOME replacement => replacement)
| substTy (AnonymousTyVar (_, ref (Link ty))) = substTy ty
| substTy (ty as AnonymousTyVar (_, ref (Unbound _))) = ty
| substTy (RecordType (span, fields)) =
RecordType (span, Syntax.LabelMap.map substTy fields)
| substTy (TyCon (span, tyargs, tycon)) =
TyCon (span, List.map substTy tyargs, tycon)
| substTy (FnType (span, ty1, ty2)) =
FnType (span, substTy ty1, substTy ty2)
| substTy (RecordExtType (span, fields, baseTy)) =
RecordExtType
(span, Syntax.LabelMap.map substTy fields, substTy baseTy)
in
substTy
end
fun applySubstTyInPat subst =
let
val doTy = applySubstTy subst
fun doPat (pat as WildcardPat _) = pat
| doPat (SConPat (span, scon, ty)) =
SConPat (span, scon, doTy ty)
| doPat (VarPat (span, vid, ty)) =
VarPat (span, vid, doTy ty)
| doPat (RecordPat {sourceSpan, fields, ellipsis, wholeRecordType}) =
RecordPat
{ sourceSpan = sourceSpan
, fields = List.map (fn (label, pat) => (label, doPat pat)) fields
, ellipsis = Option.map doPat ellipsis
, wholeRecordType = doTy wholeRecordType
}
| doPat
(ConPat {sourceSpan, longvid, payload, tyargs, valueConstructorInfo}) =
ConPat
{ sourceSpan = sourceSpan
, longvid = longvid
, payload =
Option.map (fn (ty, pat) => (doTy ty, doPat pat)) payload
, tyargs = List.map doTy tyargs
, valueConstructorInfo = valueConstructorInfo
}
| doPat (TypedPat (span, pat, ty)) =
TypedPat (span, doPat pat, doTy ty)
| doPat (LayeredPat (span, vid, ty, pat)) =