-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_inference.rkt
2081 lines (1733 loc) · 80.4 KB
/
type_inference.rkt
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
#lang racket
(require redex
"./grammar.rkt"
"./typing_lang_theory.rkt"
"./desugar/parser.rkt")
;
;
;
; ;
; ; ; ;
; ; ;
; ;;; ;;; ; ;;;; ;;;;; ;;;;;; ; ;;; ;;;; ;;; ; ;;;; ;;;;;; ;;;;;
; ; ; ; ; ;; ;; ; ; ; ;; ; ; ; ; ;; ;; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ;;;; ; ; ;;;;;; ; ; ; ; ;;;;
; ; ; ; ; ; ;;; ; ; ;; ; ; ; ; ; ;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ;
; ;;; ;;; ; ; ;;;;; ;;; ; ;;;; ; ;;;;;;; ; ; ;;; ;;;;;
;
;
;
;
; Membership, to be able to use Cs as a set
(define-metafunction core-lang-typed
cons_in : Cs c -> any
[(cons_in () c)
#f]
[(cons_in (c_1 c_2 ...) c_1)
#t]
[(cons_in (c_1 c_2 ...) c_3)
(cons_in (c_2 ...) c_3)]
)
(define-metafunction core-lang-typed
cons_un : Cs Cs -> Cs
[(cons_un (c_1 ...) (c_2 ...))
(c ...)
(where (c ...) ,(remove-duplicates (term (c_1 ... c_2 ...))))]
)
; Replaces occurrences of $actfunc by a given typevar:
; each return statements add restrictions that refer to $actfunc; we replace
; $actfunc by the typevar that represents this function
(define-metafunction core-lang-typed
replace_actfunc : Cs τ -> Cs
[(replace_actfunc () τ)
()]
[(replace_actfunc ((($actfunc returntypevar) <: ρ) c_1 ...) τ)
(((τ returntypevar) <: ρ) c_2 ...)
(where (c_2 ...) (replace_actfunc (c_1 ...) τ))
]
[(replace_actfunc ((τ_1 <: ($actfunc returntypevar)) c_1 ...) τ_2)
((τ_1 <: (τ_2 returntypevar)) c_2 ...)
(where (c_2 ...) (replace_actfunc (c_1 ...) τ_2))
]
[(replace_actfunc (c_1 c_2 ...) τ)
(c_1 c_3 ...)
(where (c_3 ...) (replace_actfunc (c_2 ...) τ))
]
)
; Creates the corresponding typevars required to express the domain type of a
; function; updates γ correspondigly
(define-metafunction core-lang-typed
fun-domain-typevar : γ (Name ...) -> (γ ((Name Number typevar) ...))
[(fun-domain-typevar γ ())
(γ ())]
[(fun-domain-typevar γ_1 (Name_1 Name_2 ...))
(γ_3 ((Name_1 Number typevar) τ ...))
(where Number ,(+ 1 (term (index-γ γ_1 Name_1))))
(where γ_2 (set γ_1 Name_1 Number))
(where (γ_3 (τ ...)) (fun-domain-typevar γ_2 (Name_2 ...)))]
)
(define-metafunction core-lang-typed
extract_ret_type : Cs τ -> (Cs τ)
[(extract_ret_type (c_1 ...
(τ_1 <: ((function Name_1 ((Name_2 label typevar)) τ_2
end)
returntypevar))
c_2 ...)
(function Name_1 ((Name_2 label typevar)) τ_2
end))
((c_1 ... c_2 ...) τ_1)]
[(extract_ret_type Cs _)
(Cs nil)]
)
;
;
;
;
;
;
; ;;; ;;;; ; ;;; ;;;; ;;;;; ;;;; ; ;;;
; ; ; ;; ;; ;; ; ; ; ;; ;; ;; ;; ;; ;
; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ;;;; ; ; ;;;;;; ; ;
; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ;; ;; ; ; ; ; ;; ;; ;; ; ; ;
; ;;; ;;;; ; ; ;;;; ;;; ; ;;;; ; ;
; ;
; ; ;
; ;;;
;
; constraints for a list of expression (for example, for actual parameters of
; a fun call)
(define-judgment-form
core-lang-typed
#:mode (cons_gen_el I I O O O)
#:contract (cons_gen_el γ (e ...) (τ ...) γ Cs)
[(cons_gen γ_1 e τ γ_2 Cs)
--------------------------------------------------------
(cons_gen_el γ_1 (e) (τ) γ_2 Cs)]
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen_el γ_2 (e_2 e_3 ...) (τ_2 ...) γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
-----------------------------------------------------------
(cons_gen_el γ_1 (e_1 e_2 e_3 ...) (τ_1 τ_2 ...) γ_3 Cs_3)]
)
; constraints gen. for table fields
(define-judgment-form
core-lang-typed
#:mode (cons_gen_table_field I I O O O)
#:contract (cons_gen_table_field γ (field_1 field_2 ...) (τ ...) γ Cs)
[(cons_gen γ_1 e τ γ_2 Cs)
; dummy type var for the non-existent key
(where label ,(+ 1 (term (index-γ γ_1 $dummyt))))
(where γ_3 (set γ_2 $dummyt label))
(where Cs_2 (cons_un Cs
(; key must be constrained to num type
(($dummyt label typevar) <: num)
)))
--------------------------------------------------------
(cons_gen_table_field γ_1 (e) ((\[ ($dummyt label typevar) \] = τ))
γ_2 Cs_2)]
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
----------------------------------------
(cons_gen_table_field γ_1 ((\[ e_1 \] = e_2)) ((\[ τ_1 \] = τ_2)) γ_3 Cs_3)]
[(cons_gen_table_field γ_1 (field_1) (τ_1) γ_2 Cs_1)
(cons_gen_table_field γ_2 (field_2 field_3 ...) (τ_2 τ_3 ...) γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
------------------------------------------------------------
(cons_gen_table_field γ_1
(field_1 field_2 field_3 ...)
(τ_1 τ_2 τ_3 ...) γ_3 Cs_3)]
)
(define-judgment-form
core-lang-typed
#:mode (cons_gen I I O O O)
#:contract (cons_gen γ any τ γ Cs)
;
;
;
;
;
;
; ;;;; ;; ;; ;;;;; ;;;;
; ;; ;; ; ; ;; ;; ; ;
; ; ; ;; ; ; ;
; ;;;;;; ;; ; ; ;;;;
; ; ;; ; ; ;
; ;; ; ; ; ;; ;; ; ;
; ;;;; ;; ;; ;;;;; ;;;;
; ;
; ;
; ;
;
; Base types
[-------------------------------------
(cons_gen γ nil nil γ ((nil <: (nil : nil))))]
[-------------------------------------
(cons_gen γ Number Number γ ((Number <: (Number : num))))]
[------------------------------------------------------------------
(cons_gen γ String String γ ((String <: (String : str))))]
[--------------------------------------------------
(cons_gen γ Boolean Boolean γ ((Boolean <: (Boolean : bool))))]
; id
[(where label (index-γ γ Name))
-------------------------------------
(cons_gen γ Name (Name label typevar) γ ())]
; funDef
[; we are not using numeric label for identifying formal parameters: we use
; the function label
(where (γ_2 ((Name_4 Number_1 typevar) (Name_5 Number_2 typevar) ...))
(fun-domain-typevar γ_1
(Name_2 Name_3 ...)))
(cons_gen γ_2 s τ_1 γ_3 Cs_1)
; each return statements add restrictions that refer to $actfunc; we replace
; $actfunc by the typevar that represents this function
(where Cs_2 (replace_actfunc
Cs_1
(function Name_1 ((Name_4 Number_1 typevar)
(Name_5 Number_2 typevar) ...) τ_1 end)
))
(where
Cs_3
(cons_un
Cs_2
(; Constraint over fdef
((function Name_1 ((Name_4 Number_1 typevar)
(Name_5 Number_2 typevar) ...) τ_1 end)
<:
(((function Name_1 ((Name_4 Number_1 typevar)
(Name_5 Number_2 typevar) ...) τ_1 end) paramtypevar)
->
((function Name_1 ((Name_4 Number_1 typevar)
(Name_5 Number_2 typevar) ...) τ_1 end)
returntypevar))
)
; To carry restrictions over the function's type to and from restrictions
; over the parameter's type
(((function Name_1 ((Name_4 Number_1 typevar)
(Name_5 Number_2 typevar) ...) τ_1 end) paramtypevar)
<: ($tup (Name_4 Number_1 typevar) (Name_5 Number_2 typevar) ...))
)))
-----------------------------------------------------------
(cons_gen γ_1
(function Name_1 (Name_2 Name_3 ...) s end)
(function Name_1 ((Name_4 Number_1 typevar)
(Name_5 Number_2 typevar) ...) τ_1 end)
γ_3
Cs_3)]
[(cons_gen γ_1 s τ_1 γ_2 Cs_1)
; each return statements add restrictions that refer to $actfunc; we replace
; $actfunc by the typevar that represents this function
(where Cs_2 (replace_actfunc
Cs_1
(function Name_1 () τ_1 end)
))
(where
Cs_3
(cons_un
Cs_2
(; Constraint over fdef
((function Name_1 () τ_1 end)
<:
(((function Name_1 () τ_1 end) paramtypevar)
->
((function Name_1 () τ_1 end)
returntypevar))
)
; To carry restrictions over the function's type to and from restrictions
; over the parameter's type
(((function Name_1 () τ_1 end) paramtypevar)
<: ($tup))
)))
-----------------------------------------------------------
(cons_gen γ_1
(function Name_1 () s end)
(function Name_1 () τ_1 end)
γ_2
Cs_3)]
; vararg
; [-------------------------------------
; (cons_gen γ <<< _ _ _)]
; table field
; Assumption: indexation of tables fields only over the variable identifier
; bound to the table
[(cons_gen γ_1 e_2 τ_2 γ_2 Cs_1)
; new label
(where label ,(+ 1 (term (index-γ γ_2 Name))))
(where γ_3 (set γ_2 Name label))
(where Cs_2 (cons_un Cs_1
; τ_1 should be a subtype of a table type that, at least,
; contains a field with a key that is a subtype of τ_2,
; and value with a subtype of (τ_1 \[ τ_2 \])
(; TODO: this constraint is superflous, it can be
; inferred from the following constraint
((Name label typevar)
<: (\[ τ_2 \] : ((Name label typevar) \[ τ_2 \])))
; we require for the occurrences of Name to have the
; same members constrained for the previous occurrence
; of Name
((Name label typevar)
<: τ_2 (Name (index-γ γ_2 Name) typevar))
)))
--------------------------------------------------------------
(cons_gen γ_1 (Name \[ e_2 \]) ((Name label typevar) \[ τ_2 \]) γ_3 Cs_2)]
; funCall
[(cons_gen γ_1 prefixexp τ_1 γ_2 Cs_1)
(cons_gen_el γ_2 (e_1 e_2 ...) (τ_2 τ_3 ...) γ_3 Cs_2)
(where Cs_3 (cons_un (cons_un Cs_1 Cs_2)
(; Constraint over params' type: it is ok if the call
; passes to the function an actual parameter with a
; type <: according to subtyping
(($tup τ_2 τ_3 ...) <: (τ_1 paramtypevar))
((τ_1 returntypevar) <: (τ_1 (τ_2 τ_3 ...)))
; prefixexp should have a function type
(τ_1 <: ((τ_1 paramtypevar) -> (τ_1 returntypevar)))
)))
--------------------------------------------------------
(cons_gen γ_1 (prefixexp (e_1 e_2 ...))
(τ_1 (τ_2 τ_3 ...))
γ_3 Cs_3)]
[(cons_gen γ_1 prefixexp τ_1 γ_2 Cs_1)
(where Cs_2 (cons_un Cs_1
(; Constraint over params' type: it is ok if the call
; passes to the function an actual parameter with a
; type <: according to subtyping
(($tup) <: (τ_1 paramtypevar))
((τ_1 returntypevar) <: (τ_1 ()))
; prefixexp should have a function type
(τ_1 <: ((τ_1 paramtypevar) -> (τ_1 returntypevar)))
)))
--------------------------------------------------------
(cons_gen γ_1 (prefixexp ())
(τ_1 ())
γ_2 Cs_2)]
; parenthesized exp
[(cons_gen γ_1 e τ γ_2 Cs_1)
; Type of the parenthesized expression should be the same as the expression,
; if it is not a tuple value
; TODO: tuple value case!
(where Cs_2 (cons_un Cs_1
(
((\( τ \)) <: τ)
(τ <: (\( τ \)))
)))
----------------------------------------
(cons_gen γ_1 (\( e \)) (\( τ \)) γ_2 Cs_2)]
; tableconstructor
[(cons_gen_table_field γ_1 (field_1 field_2 ...) ((\[ τ_1 \] = τ_2) ...) γ_2
Cs_1)
(where Cs_2 (cons_un Cs_1
; TODO: add weakness info, as in the empty table cons.
; case?
(((\{ (\[ τ_1 \] = τ_2) ... \})
<: (\[ τ_1 \] : τ_2)) ...
)))
------------------------------------------------------
(cons_gen γ_1 (\{ field_1 field_2 ... \}) (\{ (\[ τ_1 \] = τ_2) ... \})
γ_2 Cs_2)]
[------------------------------------------------------
(cons_gen γ (\{ \}) (\{ \}) γ (((\{ \}) <: ((\{ \}) strong))))]
; binops
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un (cons_un Cs_1 Cs_2)
; operands must be a subtype of the expected operands'
; type for an arithop: included for improved type
; inference capabilities
((τ_1 <: num)
(τ_2 <: num)
((τ_1 arithop τ_2) <: num))))
----------------------------------
(cons_gen γ_1
(e_1 arithop e_2)
(τ_1 arithop τ_2) γ_3 Cs_3)]
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un (cons_un Cs_1 Cs_2)
; operands must be of the same subtype
((τ_1 <: τ_2)
(τ_2 <: τ_1)
((τ_1 relop τ_2) <: bool))))
----------------------------------
(cons_gen γ_1 (e_1 relop e_2)
(τ_1 relop τ_2) γ_3 Cs_3)]
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un (cons_un Cs_1 Cs_2)
; operands must be a subtype of the expected operands'
; type for string concat op
((τ_1 <: str)
(τ_2 <: str)
((τ_1 .. τ_2) <: str))))
----------------------------------
(cons_gen γ_1 (e_1 .. e_2)
(τ_1 .. τ_2) γ_3 Cs_3)]
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un (cons_un Cs_1 Cs_2)
; operands must be of the same subtype
((τ_1 <: τ_2)
(τ_2 <: τ_1)
((τ_1 == τ_2) <: bool))))
----------------------------------
(cons_gen γ_1 (e_1 == e_2)
(τ_1 == τ_2) γ_3 Cs_3)]
; and, or operator
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
; TODO: tuple type to impose some restriction over the
; result: ((τ_1 and τ_2) <: (unt τ_1 τ_2))
----------------------------------
(cons_gen γ_1
(e_1 and e_2)
(τ_1 and τ_2) γ_3 Cs_3)]
[(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
; TODO: tuple type to impose some restriction over the
; result: ((τ_1 or τ_2) <: (unt τ_1 τ_2))
----------------------------------
(cons_gen γ_1
(e_1 or e_2)
(τ_1 or τ_2) γ_3 Cs_3)]
; unops
[(cons_gen γ_1 e τ γ_2 Cs_1)
(where Cs_2 (cons_un Cs_1
; operand must be a number
((τ <: num)
((- τ) <: num))))
----------------------------------
(cons_gen γ_1 (- e) (- τ) γ_2 Cs_2)]
[(cons_gen γ_1 e τ γ_2 Cs_1)
(where Cs_2 (cons_un Cs_1
(((not τ) <: bool))))
------------------------------------
(cons_gen γ_1 (not e) (not τ) γ_2 Cs_2)]
[(cons_gen γ_1 e τ γ_2 Cs_1)
(where Cs_2 (cons_un Cs_1
; operand could be a str or table; we do not impose
; restrictions
(((\# τ) <: num))))
----------------------------------
(cons_gen γ_1 (\# e) (\# τ) γ_2 Cs_2)]
;
;
;
;
; ; ;
; ; ;
; ;;;; ;;;;;; ;;; ;;;;;; ;;;;
; ; ; ; ; ; ; ; ;
; ; ; ; ; ;
; ;;;; ; ;;;;; ; ;;;;
; ; ; ; ; ; ;
; ; ; ; ; ;; ; ; ;
; ;;;; ;;; ;;; ; ;;; ;;;;
;
;
;
;
[-----------------------
(cons_gen γ \; \; γ ())]
[-----------------------
(cons_gen γ break break γ ())]
; TOOD: only one returned value
[; referring to actual function through $actfunc
(cons_gen γ_1 e τ γ_2 Cs_1)
(where Cs_2 (cons_un Cs_1
((τ <: ($actfunc returntypevar)))))
-------------------------------------------------------------------
(cons_gen γ_1 (return e) (return τ) γ_2 Cs_2) ]
[; referring to actual function through $actfunc
-------------------------------------------------------------------
(cons_gen γ (return) (return) γ ()) ]
[(cons_gen γ_1 s τ γ_2 Cs)
-----------------------------------------
(cons_gen γ_1 (do s end) (do τ end) γ_1 Cs)]
[(cons_gen γ_1 e τ_1 γ_2 Cs_1)
(cons_gen γ_2 s_1 τ_2 γ_3 Cs_2)
(cons_gen γ_3 s_2 τ_3 γ_4 Cs_3)
(where Cs_4 (cons_un (cons_un Cs_1 Cs_2) Cs_3))
--------------------------------------------------------
(cons_gen γ_1 (if e then s_1 else s_2 end)
(if τ_1 then τ_2 else τ_3 end) γ_4 Cs_4)]
[(cons_gen γ_1 e τ_1 γ_2 Cs_1)
(cons_gen γ_2 s τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
--------------------------------------------------------
(cons_gen γ_1 (while e do s end)
(while τ_1 do τ_2 end) γ_3 Cs_3)]
; local var def
; TODO: only one variable
[(cons_gen γ_1 e τ_1 γ_2 Cs_1)
(where label ,(+ 1 (term (index-γ γ_2 Name))))
(where γ_3 (set γ_2 Name label))
(cons_gen γ_3 s τ_2 γ_4 Cs_2)
(where Cs_3 (cons_un (cons_un Cs_1 Cs_2)
(; Constraint over the type of the variables
(τ_1 <: (Name label typevar))
)))
----------------------------------------------------------------
(cons_gen γ_1 (local Name = e in s end)
(local (Name label typevar) = τ_1 in τ_2 end)
γ_2
Cs_3)]
; TODO: only one variable
[; The dynamic semantics indicates that e is evaluated before assignment;
; hence the type variable for Name shouldn't be the new one
(cons_gen γ_1 e τ γ_2 Cs_1)
(where Number ,(term (index-γ γ_2 Name)))
(where Cs_3 (cons_un Cs_1
((τ <: (Name Number typevar)))))
-----------------------------------------------------------------
(cons_gen γ_1 (Name = e) ((Name Number typevar) = τ) γ_2 Cs_3)]
[(cons_gen γ_1 e_1 τ_2 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_3 γ_3 Cs_2)
; new label
(where label ,(+ 1 (term (index-γ γ_3 Name))))
(where γ_4 (set γ_3 Name label))
(where Cs_3
(cons_un
(cons_un Cs_1 Cs_2)
(; we require (Name label typevar) to have a member τ_2 with type
; ((Name label typevar) \[ τ_2 \])
((Name label typevar)
<: (\[ τ_2 \] : ((Name label typevar) \[ τ_2 \])))
; we require for the occurrences of Name to have the same members
; constrained for the previous occurrence of Name
((Name label typevar) <: τ_2 (Name (index-γ γ_3 Name) typevar))
(τ_3 <: ((Name label typevar) \[ τ_2 \]))
)))
-----------------------------------------------------------
(cons_gen γ_1
((Name \[ e_1 \]) = e_2)
(((Name label typevar) \[ τ_2 \]) = τ_3)
γ_4 Cs_3) ]
[(side-condition ,(not (redex-match? core-lang-typed
Name
(term e_1))))
(cons_gen γ_1 e_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 e_2 τ_2 γ_3 Cs_2)
(cons_gen γ_3 e_3 τ_3 γ_4 Cs_3)
(where Cs_4 (cons_un (cons_un (cons_un Cs_1 Cs_2) Cs_3)
((τ_3 <: (τ_1 \[ τ_2 \]))
(τ_1 <: (\[ τ_2 \] : (τ_1 \[ τ_2 \]))))))
-----------------------------------------------------------
(cons_gen γ_1
((e_1 \[ e_2 \]) = e_3)
((τ_1 \[ τ_2 \]) = τ_3)
γ_4 Cs_4) ]
[(cons_gen γ_1 prefixexp τ_1 γ_2 Cs_1)
(cons_gen_el γ_2 (e_1 e_2 ...) (τ_2 τ_3 ...) γ_3 Cs_2)
(where Cs_3 (cons_un (cons_un Cs_1 Cs_2)
(; Constraint over params' type: it is ok if the call
; passes to the function an actual parameter with a
; type <: according to subtyping
(($tup τ_2 τ_3 ...) <: (τ_1 paramtypevar))
((τ_1 returntypevar) <: (τ_1 (τ_2 τ_3 ...)))
; prefixexp should have a function type
(τ_1 <: ((τ_1 paramtypevar) -> (τ_1 returntypevar)))
)))
--------------------------------------------------------
(cons_gen γ_1 ($statFunCall prefixexp (e_1 e_2 ...))
($statFunCall τ_1 (τ_2 τ_3 ...))
γ_3 Cs_3)]
[(cons_gen γ_1 prefixexp τ_1 γ_2 Cs_1)
(where Cs_2 (cons_un Cs_1
(; Constraint over params' type: it is ok if the call
; passes to the function an actual parameter with a
; type <: according to subtyping
(($tup) <: (τ_1 paramtypevar))
((τ_1 returntypevar) <: (τ_1 ()))
; prefixexp should have a function type
(τ_1 <: ((τ_1 paramtypevar) -> (τ_1 returntypevar)))
)))
--------------------------------------------------------
(cons_gen γ_1 ($statFunCall prefixexp ())
($statFunCall τ_1 ())
γ_2 Cs_2)]
[(cons_gen γ_1 s_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 s_2 τ_2 γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
--------------------------------------
(cons_gen γ_1 (s_1 s_2) (τ_1 τ_2) γ_3 Cs_3)]
[(cons_gen γ_1 s_1 τ_1 γ_2 Cs_1)
(cons_gen γ_2 (s_2 s_3 s_4 ...) (τ_2 ...) γ_3 Cs_2)
(where Cs_3 (cons_un Cs_1 Cs_2))
--------------------------------------
(cons_gen γ_1 (s_1 s_2 s_3 s_4 ...) (τ_1 τ_2 ...) γ_3 Cs_3)]
)
(provide cons_gen)
;
;
; ;;;
; ;
; ;
; ;
; ;;; ;;;; ; ;;; ;;;; ;;; ; ;;;; ;;;;
; ; ; ;; ;; ;; ; ; ; ; ; ; ;; ;; ; ;
; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ;;;; ; ; ; ; ;;;;
; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ;; ;; ; ; ; ; ; ; ; ;; ;; ; ;
; ;;; ;;;; ; ; ;;;; ;;; ;;; ;;;; ;;;;
;
;
;
;
(define-metafunction core-lang-typed
combine_clos_steps : Cs -> Cs
; Base case
[(combine_clos_steps Cs)
Cs
; Cs is already a closed set of constraints: no new constraint can be
; inferred from C
(where () ,(judgment-holds (cons_clos_step Cs c) c))]
[(combine_clos_steps (c_1 ...))
(combine_clos_steps (c_4 ...))
(where (c_2 c_3 ...) ,(judgment-holds (cons_clos_step (c_1 ...) c) c))
; remove duplicates
(where (c_4 ...) ,(remove-duplicates (term (c_1 ... c_2 c_3 ...))))]
)
(provide combine_clos_steps)
; FUNDAMENTAL PROPERTY: a solution for a given set C, must also be a solution
; for the closure set of C
(define-judgment-form
core-lang-typed
#:mode (cons_clos_step I O)
#:contract (cons_clos_step Cs c)
; constraint implied by transitivity of subtyping
[(where (c_1 ... (τ_1 <: τ_2) c_2 ...) (c ...))
(where (c_3 ... (τ_2 <: ρ) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_1 <: ρ)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_1) (term ρ))))
-----------------------------------------------------------
(cons_clos_step (c ...) (τ_1 <: ρ)) ]
; to transfer constraints about required fields, to new occurrences of
; a single variable constrained to have table type
[(where (c_1 ... (τ_1 <: τ_2 τ_3) c_2 ...) (c ...))
(where (c_3 ... (τ_3 <: (\[ τ_4 \] : τ_5)) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_1 <: (\[ τ_4 \] : τ_5))))))
---------------------------------------------------------------------------
(cons_clos_step (c ...) (τ_1 <: (\[ τ_4 \] : τ_5)))]
; closeFunc: subtyping for functions reduces to equality
[(where (c_1 ... (τ_1 <: (τ_2 -> τ_3)) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: (τ_4 -> τ_5)) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_2 <: τ_4)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_2) (term τ_4))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_2 <: τ_4))]
[(where (c_1 ... (τ_1 <: (τ_2 -> τ_3)) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: (τ_4 -> τ_5)) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_4 <: τ_2)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_4) (term τ_2))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_4 <: τ_2))]
[(where (c_1 ... (τ_1 <: (τ_2 -> τ_3)) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: (τ_4 -> τ_5)) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_3 <: τ_5)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_3) (term τ_5))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_3 <: τ_5))]
[(where (c_1 ... (τ_1 <: (τ_2 -> τ_3)) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: (τ_4 -> τ_5)) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_5 <: τ_3)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_5) (term τ_3))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_5 <: τ_3))]
; Tables: for a given key, force same value type over every type var that
; refers to the associated value
[(where (c_1 ... (τ_1 <: (\[ τ_2 \] : τ_3)) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: (\[ τ_2 \] : τ_4)) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_3 <: τ_4)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_3) (term τ_4))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_3 <: τ_4))]
[(where (c_1 ... (τ_1 <: (\[ τ_2 \] : τ_3)) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: (\[ τ_2 \] : τ_4)) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_4 <: τ_3)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_3) (term τ_4))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_4 <: τ_3))]
; tables are indexed by singleton types: associated values must be equivalent
[(where (c_1 ... (τ_1 <: ((\{ (\[ τ_2 \] : τ_3) ...
(\[ τ_4 \] : τ_5)
(\[ τ_6 \] : τ_7) ... \}) weakness)) c_2 ...)
(c ...))
(where (c_3 ... (τ_1 <: (\[ τ_4 \] : τ_10)) c_4 ...)
(c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_5 <: τ_10)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_5) (term τ_10))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_5 <: τ_10))]
[(where (c_1 ... (τ_1 <: ((\{ (\[ τ_2 \] : τ_3) ...
(\[ τ_4 \] : τ_5)
(\[ τ_6 \] : τ_7) ... \}) weakness)) c_2 ...)
(c ...))
(where (c_3 ... (τ_1 <: (\[ τ_4 \] : τ_10)) c_4 ...)
(c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (τ_10 <: τ_5)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_5) (term τ_10))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_10 <: τ_5))]
; closeBalance:
; subtype constraints with supertypes that are also minimals
[(where (c_1 ... (τ_1 <: τ_2) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: υ) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (term (cons_in (c ...) (υ <: τ_2)))))
; to avoid useless constraints
(side-condition ,(not (term (cons_in (c ...) (τ_2 <: υ)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term υ) (term τ_2))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (υ <: τ_2))]
; subtype constraints with supertypes which belong to ϕ imply a disjunction
; of type constraints, given the structure of our subtyping relation
[(where (c_1 ... (τ_1 <: τ_2) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: ϕ) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (redex-match? core-lang-typed
bt
(term ξ))))
(side-condition ,(not (term (cons_in (c ...) (τ_2 <: ϕ ∨ ϕ <: τ_2)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_2) (term ϕ))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_2 <: ϕ ∨ ϕ <: τ_2))]
[(where (c_1 ... (τ_1 <: τ_2) c_2 ...) (c ...))
(where (c_3 ... (τ_1 <: ϕ ∨ ϕ <: τ_1) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (redex-match? core-lang-typed
bt
(term ξ))))
(side-condition ,(not (term (cons_in (c ...) (τ_2 <: ϕ ∨ ϕ <: τ_2)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_2) (term ϕ))))
-----------------------------------------------------------------------
(cons_clos_step (c ...) (τ_2 <: ϕ ∨ ϕ <: τ_2))]
; subtyping rel. for tuples:
; using named ellipses to force same length lists
[(where (c_1 ...
(($tup τ_1 ..._1 τ_2 τ_3 ..._2) <: ($tup τ_4 ..._1 τ_5 τ_6 ..._2))
c_2 ...) (c ...))
(side-condition ,(not (term (cons_in (c ...) (τ_2 <: τ_5)))))
; TODO: better way to deal with this?
(side-condition ,(not (equal? (term τ_2) (term τ_5))))
-----------------------------------------------------------
(cons_clos_step (c ...) (τ_2 <: τ_5)) ]
)
(provide cons_clos_step)
(define-metafunction core-lang-typed
combine_clos_refine_steps : Cs -> Cs
; Base case
[(combine_clos_refine_steps Cs)
Cs
; Cs is already a closed set of constraints: no new constraint can be
; inferred from C
(where () ,(judgment-holds (cons_refine_step Cs c) c))]
[(combine_clos_refine_steps (c_1 ...))
(combine_clos_refine_steps (c_4 ...))
(where (c_2 c_3 ...) ,(judgment-holds (cons_refine_step (c_1 ...) c) c))
; remove constraints
(where (c_4 ...) ,(remove* (term (c_2 c_3 ...))
(term (c_1 ...))))]
)
(provide combine_clos_refine_steps)
(define-judgment-form
core-lang-typed
#:mode (cons_refine_step I O)
#:contract (cons_refine_step Cs c)
[(where (c_1 ... (τ <: υ) c_2 ...) (c ...))
(where (c_3 ... (τ <: χ) c_4 ...) (c_1 ... c_2 ...))
(side-condition ,(not (redex-match?
core-lang-typed
υ
(term χ))))
-----------------------------------------------------------
(cons_refine_step (c ...) (τ <: χ)) ]
)
;
;
; ;;; ;;; ;; ;
; ; ; ; ;
; ; ; ; ;
; ; ; ; ;
; ; ; ;;;; ; ; ;;;;; ;;;; ;;;; ;;;;;;; ;;;; ;;;;;
; ; ; ;; ;; ; ; ; ;; ;; ;; ; ; ; ; ;; ;; ;; ;;
; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ;; ; ;;;;;; ; ; ;;; ; ; ; ; ; ; ; ;;;;;; ; ;
; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ;; ; ; ; ; ;; ;; ; ; ; ; ;; ; ;; ;;
; ; ; ;;;; ;;; ;;; ; ;;;; ; ; ; ; ;;;; ;;;;;
;
;
;
;
(define-judgment-form
core-lang-typed
#:mode (well_form_cons_set I)
#:contract (well_form_cons_set Cs)
; For constructions for which there are no constraints to be applied
[------------------------
(well_form_cons_set ())]
[; TODO: cómo decidimos si Cs es closed? (side-condition (cons_clos_step Cs ?))
; TODO: cómo pedimos por well_form_cons para todas las constraints?
(well_form_cons (c_1 c_2 ...) c_1)
(well_form_cons (c_1 c_2 ...) c_2) ...
------------------------------------------
(well_form_cons_set (c_1 c_2 ...))]
)
(provide well_form_cons_set)
(define-metafunction core-lang-typed
nil_chain : (τ Cs) -> any
[(nil_chain (side-condition
(τ_1 (c_1 ... c c_2 ...))
(or (redex-match core-lang-typed
(side-condition (τ_2 <: χ)
(and
(equal? (term τ_1) (term τ_2))
(not (is_nil_chain? (term χ))))
)
(term c))
(redex-match core-lang-typed
(side-condition (χ <: τ_2)
(and
(equal? (term τ_1) (term τ_2))
(not (is_nil_chain? (term χ))))
)
(term c))
(redex-match core-lang-typed
(side-condition (τ_2 <: χ ∨ χ <: τ_2)
(and
(equal? (term τ_1) (term τ_2))
(not (is_nil_chain? (term χ))))
)
(term c))
(redex-match core-lang-typed
(side-condition (τ_2 <: (\[ τ_3 \] : τ_4))
(equal? (term τ_1) (term τ_2)))
(term c))
(redex-match core-lang-typed
(side-condition (τ_2 <: τ_3 τ_4)
(equal? (term τ_1) (term τ_2)))
(term c)))))
#f]
[(nil_chain _)
#t]
)
(define-metafunction core-lang-typed
num_chain : (τ Cs) -> any
[(num_chain (side-condition
(τ_1 (c_1 ... c c_2 ...))
(or (redex-match core-lang-typed
(side-condition (τ_2 <: χ)
(and
(equal? (term τ_1) (term τ_2))
(not (is_num_chain? (term χ))))
)
(term c))
(redex-match core-lang-typed
(side-condition (χ <: τ_2)
(and
(equal? (term τ_1) (term τ_2))
(not (is_num_chain? (term χ))))
)
(term c))
(redex-match core-lang-typed
(side-condition (τ_2 <: χ ∨ χ <: τ_2)
(and
(equal? (term τ_1) (term τ_2))
(not (is_num_chain? (term χ))))
)
(term c))
(redex-match core-lang-typed
(side-condition (τ_2 <: (\[ τ_3 \] : τ_4))
(equal? (term τ_1) (term τ_2)))
(term c))
(redex-match core-lang-typed