-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtyping.rkt
More file actions
1671 lines (1394 loc) · 64.2 KB
/
typing.rkt
File metadata and controls
1671 lines (1394 loc) · 64.2 KB
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"
"./desugar/parser.rkt"
"./typing_lang_theory.rkt"
"./type_inference.rkt"
"./data_flow_analysis.rkt"
"./reaching_defs.rkt")
;
;
;
;
;
;
; ;;;; ;; ;; ;;;;; ;;;;
; ;; ;; ; ; ;; ;; ; ;
; ; ; ;; ; ; ;
; ;;;;;; ;; ; ; ;;;;
; ; ;; ; ; ;
; ;; ; ; ; ;; ;; ; ;
; ;;;; ;; ;; ;;;;; ;;;;
; ;
; ;
; ;
;
; extension to type_e, to manage generation of contexts for fun call params.
(define-judgment-form
lang-reach-defs
#:mode (type_fun_call_param I I I I I I O O)
#:contract (type_fun_call_param Γ Π cfgKG C C any Γ any)
[(type_e Γ_1 Π cfgKG (in-hole C_1 (prefixexp (e_2 ... hole)))
e_3 Γ_2 t_1)
--------------------------------------------------------------------
(type_fun_call_param Γ_1 Π cfgKG C_1 (prefixexp (e_2 ... hole)) e_3 Γ_2
(t_1))]
[(type_e Γ_1 Π cfgKG (in-hole C_1 (prefixexp (e_2 ... hole e_3 e_4 ...)))
e_5 Γ_2 t_1)
(type_fun_call_param Γ_2 Π cfgKG C_1 (prefixexp (e_2 ... e_5 hole e_4 ...))
e_3 Γ_3 (t_2 ...))
--------------------------------------------------------------------
(type_fun_call_param Γ_1 Π cfgKG C_1 (prefixexp (e_2 ... hole e_3 e_4 ...))
e_5 Γ_3 (t_1 t_2 ...))]
)
(define-judgment-form
lang-reach-defs
#:mode (type_method_call_param I I I I I I O O)
#:contract (type_method_call_param Γ Π cfgKG C C any Γ any)
[(type_e Γ_1 Π cfgKG (in-hole C_1 (prefixexp : Name (e_2 ... hole)))
e_3 Γ_2 t_1)
--------------------------------------------------------------------
(type_method_call_param Γ_1 Π cfgKG C_1 (prefixexp : Name (e_2 ... hole))
e_3 Γ_2 (t_1))]
[(type_e Γ_1 Π cfgKG (in-hole C_1
(prefixexp : Name (e_2 ... hole e_3 e_4 ...)))
e_5 Γ_2 t_1)
(type_method_call_param Γ_2 Π cfgKG C_1
(prefixexp : Name (e_2 ... e_5 hole e_4 ...))
e_3 Γ_3 (t_2 ...))
--------------------------------------------------------------------
(type_method_call_param Γ_1 Π cfgKG C_1
(prefixexp : Name (e_2 ... hole e_3 e_4 ...))
e_5 Γ_3 (t_1 t_2 ...))]
)
; extension to type_e, to manage generation of contexts for table fields.
(define-judgment-form
lang-reach-defs
#:mode (type_table_fields I I I I I I O O)
#:contract (type_table_fields Γ Π cfgKG C C any Γ any)
[(type_e Γ_1 Π cfgKG (in-hole C_1
(\{ (\[ e_3 \] = e_4) ...
(\[ hole \] = e_2)
(\[ e_5 \] = e_6)
(\[ e_7 \] = e_8) ... \})
) e_1 Γ_2
t_1)
(type_e Γ_2 Π cfgKG (in-hole C_1
(\{ (\[ e_3 \] = e_4) ...
(\[ e_1 \] = hole)
(\[ e_5 \] = e_6)
(\[ e_7 \] = e_8) ... \})) e_2 Γ_3
t_2)
; there are fields left
(type_table_fields Γ_3 Π cfgKG C_1
(\{ (\[ e_3 \] = e_4) ... (\[ e_1 \] = e_2)
hole
(\[ e_7 \] = e_8) ... \})
(\[ e_5 \] = e_6) Γ_4
((\[ t_3 \] : t_4) ...))
--------------------------------------------------------------------
(type_table_fields Γ_1 Π cfgKG C_1
(\{ (\[ e_3 \] = e_4) ... hole (\[ e_5 \] = e_6)
(\[ e_7 \] = e_8) ... \})
(\[ e_1 \] = e_2) Γ_3
((\[ t_1 \] : t_2) (\[ t_3 \] : t_4) ... ))]
; no fields left
[(type_e Γ_1 Π cfgKG (in-hole C_1
(\{ (\[ e_3 \] = e_4) ...
(\[ hole \] = e_2) \})) e_1 Γ_2
t_1)
(type_e Γ_2 Π cfgKG (in-hole C_1
(\{ (\[ e_3 \] = e_4) ...
(\[ e_1 \] = hole) \})) e_2 Γ_3
t_2)
--------------------------------------------------------------------
(type_table_fields Γ_1 Π cfgKG C_1 (\{ (\[ e_3 \] = e_4) ... hole \})
(\[ e_1 \] = e_2) Γ_3
((\[ t_1 \] : t_2)))]
)
(define-judgment-form
lang-reach-defs
#:mode (type_e I I I I I O O)
; TODO: define a typing rel for table fields, change any for t
#:contract (type_e Γ Π cfgKG C any Γ any)
[-----------------------------
(type_e Γ Π cfgKG C nil Γ (nil : nil))]
[-----------------------------
(type_e Γ Π cfgKG C Boolean Γ (Boolean : bool))]
[--------------------------
(type_e Γ Π cfgKG C Number Γ (Number : num))]
[-------------------------------
(type_e Γ Π cfgKG C String Γ (String : str))]
[-----------------------------------
(type_e Γ Π cfgKG C Name Γ (index Γ Name))]
; TODO: I'm assuming that the variables in the environment of the closure
; have the same type
[--------------------------------------------------------
(type_e Γ_1 Π cfgKG C (t_1 function Name_1 () s end) Γ_1
(($tup) -> t_1))]
[--------------------------------------------------------
(type_e Γ_1 Π cfgKG C (t_1 function Name_1
((Name_2 : t_2) (Name_3 : t_3) ...)
s end) Γ_1
(($tup t_2 t_3 ...) -> t_1))]
[; TODO: better way to assert that we are not calling setmetatable?
(side-condition ,(not (equal? (term ($ENV |[| "setmetatable" |]|))
(term prefixexp))))
; TODO: recursive function?
(type_e Γ_1 Π cfgKG C prefixexp Γ_2 (($tup) -> t_2))
-------------------------------------------------
(type_e Γ_1 Π cfgKG C (prefixexp ()) Γ_2 t_2)]
[; TODO: better way to assert that we are not calling setmetatable?
(side-condition ,(not (equal? (term ($ENV |[| "setmetatable" |]|))
(term prefixexp))))
; TODO: recursive function?
(type_e Γ_1 Π cfgKG C prefixexp Γ_2 (($tup t_1 ...) -> t_2))
(type_fun_call_param Γ_2 Π cfgKG C (prefixexp (hole e_2 ...)) e_1 Γ_3
(t_3 ...))
; actual parameters are a subtype of formal parameters
(side-condition (subtyping_rel ($tup t_3 ...) ($tup t_1 ...)))
-----------------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (prefixexp (e_1 e_2 ...)) Γ_3 t_2)]
; Extract info about the weakness of a table assumptions:
; - first argument, Name, is bound to a table
; - a call to setmetatable with an expression that can be typed as a table
; - strings as key and value of the __mode field
; - _ENV is populated with library services, bound to the expected
; identifiers
[(type_e Γ_1 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (hole e)))
Name Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) _))
(type_e Γ_2 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (Name hole)))
e Γ_3 ((\{ _ ... (\[ ("__mode" : str) \] : (String : str)) _ ...
\}) _))
(side-condition ,(string-contains? (term String) "k"))
(where Γ_4 (set Γ_3 Name ((\{ (\[ t_1 \] : t_2) ... \}) wk)))
---------------------------------------------------------------------------
(type_e Γ_1 Π cfgKG C
(($ENV |[| "setmetatable" |]|) (Name e))
Γ_4
((\{ (\[ t_1 \] : t_2) ... \}) wk))]
[(type_e Γ_1 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (hole e)))
Name Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) _))
(type_e Γ_2 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (Name hole)))
e Γ_3 ((\{ _ ... (\[ ("__mode" : str) \] : (String : str)) _ ...
\}) _))
(side-condition ,(string-contains? (term String) "v"))
(where Γ_4 (set Γ_3 Name ((\{ (\[ t_1 \] : t_2) ... \}) wv)))
---------------------------------------------------------------------------
(type_e Γ_1 Π cfgKG C
(($ENV |[| "setmetatable" |]|) (Name e))
Γ_4
((\{ (\[ t_1 \] : t_2) ... \}) wv))]
[(type_e Γ_1 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (hole e)))
Name Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) weakness))
(type_e Γ_2 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (Name hole)))
e Γ_3 ((\{ _ ... (\[ ("__mode" : str) \] : (String : str)) _ ...
\}) _))
(side-condition ,(string-contains? (term String) "k"))
(side-condition ,(string-contains? (term String) "v"))
(where Γ_4 (set Γ_3 Name ((\{ (\[ t_1 \] : t_2) ... \}) wkv)))
---------------------------------------------------------------------------
(type_e Γ_1 Π cfgKG C
(($ENV |[| "setmetatable" |]|) (Name e))
Γ_4
((\{ (\[ t_1 \] : t_2) ... \}) wkv))]
; meta-table with not weakness information
[(type_e Γ_1 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (hole e)))
Name Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) weakness_1))
(type_e Γ_2 Π cfgKG (in-hole C (($ENV |[| "setmetatable" |]|) (Name hole)))
e Γ_3 ((\{ (\[ t_3 \] : t_4) ... \}) weakness_2))
(side-condition
,(not (redex-match? lang-reach-defs
(side-condition
((\{ _ ... (\[ ("__mode" : str) \] : (String : str))
_ ... \}) _)
(not (or (string-contains? (term String) "k")
(string-contains? (term String) "v"))))
(term ((\{ (\[ t_3 \] : t_4) ... \})
weakness_2)))))
; dynamic semantics dictates that the table weakness changes to strong
(where Γ_4 (set Γ_3 Name ((\{ (\[ t_1 \] : t_2) ... \}) strong)))
---------------------------------------------------------------------------
(type_e Γ_1 Π cfgKG C
(($ENV |[| "setmetatable" |]|) (Name e))
Γ_4
((\{ (\[ t_1 \] : t_2) ... \}) strong))]
; TODO: not checking for membership of Name to prefixexp
[; TODO: better way to assert that we are not calling setmetatable?
(side-condition ,(or (not (equal? (term $ENV)
(term prefixexp)))
(not (equal? (term Name)
(term setmetatable)))))
; TODO: recursive function?
; prefixexp is an object with the corresponding method
(type_e Γ_1 Π cfgKG C prefixexp Γ_2 ((\{ any_1 ...
(\[ t_1 \] : (($tup) -> t_2))
any_2 ... \}) weakness))
(side-condition (subtyping_rel t_1 str))
-------------------------------------------------
(type_e Γ_1 Π cfgKG C (prefixexp : Name ()) Γ_2 t_2)]
[; TODO: better way to assert that we are not calling setmetatable?
(side-condition ,(not (equal? (term ($ENV |[| "setmetatable" |]|))
(term prefixexp))))
; TODO: recursive function?
; prefixexp is an object with the corresponding method
(type_e Γ_1 Π cfgKG C prefixexp Γ_2
((\{ any_1 ...
(\[ t_1 \] : (($tup t_2 ...) -> t_3))
any_2 ... \}) weakness))
(side-condition (subtyping_rel t_1 str))
; actual parameters are a subtype of formal parameters
(type_method_call_param Γ_2 Π cfgKG C (prefixexp : Name (hole e_2 ...)) e_1
Γ_3 (t_4 ...))
(side-condition (subtyping_rel ($tup t_4 ...) ($tup t_2 ...)))
-----------------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (prefixexp : Name (e_1 e_2 ...)) Γ_3 t_3)]
; It doesn't matter if <<< is bound to weak values: into the closure, there
; will be a list of strong references bound to the weak values, mapped to
; <<< by the environment
[---------------------------------
(type_e Γ Π cfgKG C <<< Γ (index Π <<<))]
; TODO: would we need to give a typing rule for every service?
; [(type_e Γ_1 Π e Γ_2 ncte) ...
; -------------------------------------------------------
; (type_e Γ_1 Π ($builtIn builtinserv (e ...)) Γ_1 num)]
; TODO: I'm not considering tuple types
[(type_e Γ_1 Π cfgKG (in-hole C (\( hole \))) e Γ_2 t)
---------------------------------------
(type_e Γ_1 Π cfgKG C (\( e \)) Γ_2 t)]
; TOOD: compute correct contexts for each field
[(type_table_fields Γ_1 Π cfgKG C (\{ hole field_2 ... \}) field_1 Γ_2
(any ...))
----------------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (\{ field_1 field_2 ... \}) Γ_2
((\{ any ... \}) strong))]
[-------------------------------------------------
(type_e Γ Π cfgKG C (\{ \}) Γ ((\{ \}) strong))]
; TODO: coercion?
[(type_e Γ_1 Π cfgKG (in-hole C (hole arithop e_2)) e_1 Γ_2 t_1)
(side-condition (subtyping_rel t_1 num))
(type_e Γ_2 Π cfgKG (in-hole C (e_1 arithop hole)) e_2 Γ_3 t_2)
(side-condition (subtyping_rel t_2 num))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 arithop e_2) Γ_3 num)]
[(type_e Γ_1 Π cfgKG (in-hole C (hole .. e_2)) e_1 Γ_2 t_1)
(type_e Γ_2 Π cfgKG (in-hole C (e_1 .. hole)) e_2 Γ_3 t_2)
(side-condition ,(and (term (subtyping_rel t_1 str))
(term (subtyping_rel t_2 str))))
; supremum of both string types
(where t_3 (supremum_type t_1 t_2))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 .. e_2) Γ_3 t_3)]
[(type_e Γ_1 Π cfgKG (in-hole C (hole relop e_2)) e_1 Γ_2 t_1)
(type_e Γ_2 Π cfgKG (in-hole C (e_1 relop hole)) e_2 Γ_3 t_2)
; numeric or string subtype
(side-condition ,(or (and (term (subtyping_rel t_1 num))
(term (subtyping_rel t_2 num)))
(and (term (subtyping_rel t_1 str))
(term (subtyping_rel t_2 str)))))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 relop e_2) Γ_3 bool)]
[(type_e Γ_1 Π cfgKG (in-hole C (hole and e_2)) e_1 Γ_2 t_1)
(type_e Γ_2 Π cfgKG (in-hole C (e_1 and hole)) e_2 Γ_3 t_2)
; supremum of both types
(where t_3 (supremum_type t_1 t_2))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 and e_2) Γ_3 t_3)]
[(type_e Γ_1 Π cfgKG (in-hole C (hole or e_2)) e_1 Γ_2 t_1)
(type_e Γ_2 Π cfgKG (in-hole C (e_1 or hole)) e_2 Γ_3 t_2)
; supremum of both types
(where t_3 (supremum_type t_1 t_2))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 or e_2) Γ_3 t_3)]
[(type_e Γ_1 Π cfgKG (in-hole C (hole or e_2)) e_1 Γ_2 t)
(type_e Γ_2 Π cfgKG (in-hole C (e_1 or hole)) e_2 Γ_3 t)
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 == e_2) Γ_3 bool)]
; TODO: coercion?
[(type_e Γ_1 Π cfgKG (in-hole C (- hole)) e Γ_2 t)
(side-condition (subtyping_rel t num))
--------------------------------------
(type_e Γ_1 Π cfgKG C (- e) Γ_2 num)]
[(type_e Γ_1 Π cfgKG (in-hole C (not hole)) e Γ_2 t)
----------------------------------------------------
(type_e Γ_1 Π cfgKG C (not e) Γ_2 bool)]
[(type_e Γ_1 Π cfgKG (in-hole C (\# hole)) e Γ_2 t)
; table type or string subtype
(side-condition ,(or (is_tablet? (term t))
(term (subtyping_rel t str))))
----------------------------------------------------
(type_e Γ_1 Π cfgKG C (\# e) Γ_2 num)]
;
;
; ;;
; ;
; ; ;
; ; ;
; ; ;;; ;;;; ;;;;;; ;;;; ;;; ;;;;; ;;;;
; ;; ; ;; ;; ; ; ; ; ; ; ;; ;;
; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ;;;; ;;;;; ; ;;;;;;
; ; ; ; ; ; ; ; ; ; ;
; ; ; ;; ;; ; ; ; ; ;; ; ;; ;
; ; ; ;;;; ;;; ;;;; ;;; ; ; ;;;;
;
;
;
;
;
;
; ;; ; ;;; ; ; ;
; ; ; ; ;
; ; ; ; ;
; ; ; ; ;
; ;;;;; ;;; ;;;; ; ;;;;; ;;; ; ;;; ;;;;; ;;;; ;; ;;
; ; ; ;; ;; ; ;; ;; ; ;; ; ;; ;; ;; ;; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;
; ; ; ;;;;;; ; ; ; ; ; ; ; ; ;;;;;; ;;
; ; ; ; ; ; ; ; ; ; ; ; ; ;;
; ; ; ;; ; ; ;; ;; ; ; ; ;; ;; ;; ; ; ;
; ; ;;;;; ;;;; ;;; ;;;;; ;;;;; ; ; ;;;;; ;;;; ;; ;;
;
;
;
;
; assumption: we can type check only the case where the field is accessed
; through the same expression that appears into the table's keys, and for a
; table with keys with different types
[(type_e Γ_1 Π cfgKG (in-hole C (hole \[ e_2 \]))
e_1 Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) strong))
(type_e Γ_2 Π cfgKG (in-hole C (e_1 \[ hole \]))
e_2 Γ_3 t_3)
; access the field with key of type t_3
(where (\{ _ ...
(\[ t_3 \] : t_4)
_ ... \}) (\{ (\[ t_1 \] : t_2) ... \}))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 \[ e_2 \]) Γ_3 t_4)]
; assumption: tables indexed only through a variable bound to them
[; it is a weak table
(type_e Γ_1 Π cfgKG (in-hole C (hole \[ e_1 \]))
Name_1 Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) wv))
(type_e Γ_2 Π cfgKG (in-hole C (Name_1 \[ hole \]))
e_1 Γ_3 t_3)
; the field contains a cte; we will need to check for reachability
(where (_ ...
(\[ t_3 \] : ctet)
_ ...) ((\[ t_1 \] : t_2) ...))
; determine reach. of the value, since it is referred by a weak ref.
; get. reach defs.
(where ((Name_2 = e_2) ...) (get_reach_defs cfgKG C))
(where ioTree (build_tree ((Name_2 = e_2) ...)))
(side-condition ,(if (term (reachCte_stat ((Name_1 \[ e_1 \]) ioTree Γ_3)))
#t
(begin
(println "Access to: ")
(println (term (Name_1 \[ e_1 \])))
; TODO: too verbose, change for line number
;(println "from: ")
;(println (term C))
(println "may exhibit non-deterministic behavior")
#t)))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (Name_1 \[ e_1 \]) Γ_3 ctet)]
[(type_e Γ_1 Π cfgKG (in-hole C (hole \[ e_1 \]))
Name_1 Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) wv))
(type_e Γ_2 Π cfgKG (in-hole C (Name_1 \[ hole \]))
e_1 Γ_3 t_3)
; the field does not contain a cte; no need to check for reachability
(where (\{ _ ...
(\[ t_3 \] : t_4)
_ ... \}) (\{ (\[ t_1 \] : t_2) ... \}))
(side-condition ,(not (redex-match? lang-reach-defs
ctet
(term t_4))))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (Name_1 \[ e_1 \]) Γ_3 t_4)]
; ephemerons
[; it is a weak table
(type_e Γ_1 Π cfgKG (in-hole C (hole \[ e_1 \]))
Name_1 Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) wk))
(type_e Γ_2 Π cfgKG (in-hole C (Name_1 \[ hole \]))
e_1 Γ_3 t_3)
; the key is not a cte
(side-condition ,(not (redex-match? lang-reach-defs
ctet
(term t_3))))
; look for the type of the value
(where (\{ _ ...
(\[ t_3 \] : t_4)
_ ... \}) (\{ (\[ t_1 \] : t_2) ... \}))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (Name_1 \[ e_1 \]) Γ_3 t_4)]
[(type_e Γ_1 Π cfgKG (in-hole C (hole \[ e_1 \]))
Name_1 Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) wk))
; the key is a cte
(type_e Γ_2 Π cfgKG (in-hole C (Name_1 \[ hole \]))
e_1 Γ_3 ctet)
; the reachability of the value depends on the reachability of the key
; determine reach. of the key, since it is referred by a weak ref.
; get. reach defs.
(where ((Name_2 = e_2) ...) (get_reach_defs cfgKG C))
(where ioTree (build_tree ((Name_2 = e_2) ...)))
(side-condition ,(if (term (reachCte_stat (e_1 ioTree Γ_3)))
#t
(begin
(println "Access to: ")
(println (term (Name_1 \[ e_1 \])))
; TODO: too verbose, change for line number
;(println "from: ")
;(println (term C))
(println "may exhibit non-deterministic behavior")
)))
; extract the type the value
(where (\{ _ ...
(\[ ctet \] : t_3)
_ ... \}) (\{ (\[ t_1 \] : t_2) ... \}))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (Name_1 \[ e_1 \]) Γ_3 t_3)]
; key does not belong to table
; assumption: table does not have a meta-table who will take care of
; the indexing
[(type_e Γ_1 Π cfgKG (in-hole C (hole \[ e_2 \]))
e_1 Γ_2 ((\{ (\[ t_1 \] : t_2) ... \}) weakness))
(type_e Γ_2 Π cfgKG (in-hole C (e_1 \[ hole \]))
e_2 Γ_3 t_3)
(side-condition
,(not (redex-match?
lang-reach-defs
(_ ...
(\[ (side-condition t_4 (equal? (term t_4) (term t_3))) \] : t_5)
_ ...)
(term ((\[ t_1 \] : t_2) ...)))))
----------------------------------------------------------------
(type_e Γ_1 Π cfgKG C (e_1 \[ e_2 \]) Γ_3 (nil : nil))]
)
(provide type_e)
;
;
;
;
; ; ;
; ; ;
; ;;;; ;;;;;; ;;; ;;;;;; ;;;;
; ; ; ; ; ; ; ; ;
; ; ; ; ; ;
; ;;;; ; ;;;;; ; ;;;;
; ; ; ; ; ; ;
; ; ; ; ; ;; ; ; ;
; ;;;; ;;; ;;; ; ;;; ;;;;
;
;
;
;
; extension to type_s, to manage generation of contexts for concat. stats.
(define-judgment-form
lang-reach-defs
#:mode (type_conc_stats I I I I I I O)
#:contract (type_conc_stats Γ Π cfgKG C C any Γ)
[(type_s Γ_1 Π cfgKG (in-hole C_1 (s_2 ... hole s_3 s_4 ...)) s_1 Γ_2)
(type_conc_stats Γ_2 Π cfgKG C_1 (s_2 ... s_1 hole s_4 ...) s_3 Γ_3)
--------------------------------------------------------------------
(type_conc_stats Γ_1 Π cfgKG C_1 (s_2 ... hole s_3 s_4 ...) s_1 Γ_3)]
[(type_s Γ_1 Π cfgKG (in-hole C_1 (s_2 ... hole)) s_1 Γ_2)
--------------------------------------------------------------------
(type_conc_stats Γ_1 Π cfgKG C_1 (s_2 ... hole) s_1 Γ_2)]
)
; type mult. assignment
; before typing assigment: type lvalues and rvalues
(define-judgment-form
lang-reach-defs
#:mode (type_lrvalues I I I I I I O O O)
#:contract (type_lrvalues Γ Π cfgKG C C any Γ
(any ...) ; lvalues: Name ... (Name \[ t \]) ...
(t ...) ; rvalues
)
[(type_lrvalues Γ_1 Π cfgKG C
(var_1 ... Name hole var_3 ... = e ...)
var_2 Γ_2
(any ...)
(t_2 ...))
--------------------------------------------------------------------
(type_lrvalues Γ_1 Π cfgKG C (var_1 ... hole var_2 var_3 ... = e ...)
Name Γ_2
(Name any ...)
(t_2 ...))]
[(type_e Γ_1 Π cfgKG
(in-hole C
(var_1 ... (Name \[ hole \]) var_2 var_3 ... = e_1 ...))
e_2 Γ_2 t_1)
(type_lrvalues Γ_2 Π cfgKG C
(var_1 ... (Name \[ e_2 \]) hole var_3 ... = e_1 ...)
var_2 Γ_3
(any ...)
(t_2 ...))
--------------------------------------------------------------------
(type_lrvalues Γ_1 Π cfgKG C (var_1 ... hole var_2 var_3 ... = e_1 ...)
(Name \[ e_2 \]) Γ_2
((Name \[ t_1 \]) any ...)
(t_2 ...))]
[(type_lrvalues Γ_1 Π cfgKG C
(var_1 ... Name = hole e_2 ...)
e_1 Γ_2
(any ...)
(t ...))
--------------------------------------------------------------------
(type_lrvalues Γ_1 Π cfgKG C (var_1 ... hole = e_1 e_2 ...)
Name Γ_2
(Name any ...)
(t ...))]
[(type_e Γ_1 Π cfgKG
(in-hole C
(var_1 ... (Name \[ hole \]) = e_1 e_2 ...))
e_3 Γ_2 t_1)
(type_lrvalues Γ_2 Π cfgKG C
(var_1 ... (Name \[ e_3 \]) = hole e_2 ...)
e_1 Γ_3
(any ...)
(t_2 ...))
--------------------------------------------------------------------
(type_lrvalues Γ_1 Π cfgKG C (var_1 ... hole = e_1 e_2 ...)
(Name \[ e_3 \]) Γ_2
((Name \[ t_1 \]) any ...)
(t_2 ...))]
[(type_e Γ_1 Π cfgKG
(in-hole C_1
(var ... = e_1 ... hole e_2 e_3 ...))
e_4 Γ_2 t_1)
(type_lrvalues Γ_2 Π cfgKG C_1
(var ... = e_1 ... e_4 hole e_3 ...)
e_2 Γ_3
(any ...)
(t_2 ...))
--------------------------------------------------------------------
(type_lrvalues Γ_1 Π cfgKG C_1 (var ... = e_1 ... hole e_2 e_3 ...)
e_4 Γ_3
(any ...)
(t_1 t_2 ...))]
[(type_e Γ_1 Π cfgKG
(in-hole C_1
(var ... = e_1 ... hole))
e_2 Γ_2 t_1)
--------------------------------------------------------------------
(type_lrvalues Γ_1 Π cfgKG C_1 (var ... = e_1 ... hole)
e_2 Γ_2
()
(t_1))]
)
; after typing lvalues and rvalues, performs type assignment
(define-judgment-form
lang-reach-defs
#:mode (type_assign I I I I I O)
#:contract (type_assign Γ
; lvalues: Name ... (Name \[ t \]) ...
(any ... hole any ...)
any
; rvalues
(t ... hole ... t ...)
t
Γ)
; table field
; assign field
[; var. bound to table
(where ((\{ (\[ t_6 \] : t_7) ... \}) weakness)
(index Γ_1 Name))
; index type
(where (any_4 ... (\[ t_1 \] : t_8) any_5 ...) ((\[ t_6 \] : t_7) ...))
; type assigned is a subtype of the values' type
(side-condition (subtyping_rel t_5 t_8))
(type_assign Γ_1
(any_1 ... (Name \[ t_1 \]) hole any_3 ...)
any_2
(t_2 ... t_5 hole t_4 ...)
t_3
Γ_2)
--------------------------------------------------------------------
(type_assign Γ_1
(any_1 ... hole any_2 any_3 ...)
(Name \[ t_1 \])
(t_2 ... hole t_3 t_4 ...)
t_5
Γ_2)]
[; var. bound to table
(where ((\{ (\[ t_5 \] : t_6) ... \}) weakness)
(index Γ Name))
; index type
(where (any_1 ... (\[ t_1 \] : t_7) any_2 ...) ((\[ t_5 \] : t_6) ...))
; type assigned is a subtype of the values' type
(side-condition (subtyping_rel t_4 t_7))
--------------------------------------------------------------------
(type_assign Γ
(any_1 ... hole)
(Name \[ t_1 \])
(t_2 ... hole t_3 ...)
t_4
Γ)]
; new field
[; var. bound to table
(where ((\{ (\[ t_6 \] : t_7) ... \}) weakness)
(index Γ_1 Name))
;new field
(side-condition
,(not (redex-match? lang-reach-defs
(any_1 ... (\[ (side-condition t_8
(equal? (term t_8)
(term t_1)))
\] : t_9) any_2 ...)
(term ((\[ t_6 \] : t_7) ...)))))
; update environment
(where Γ_2 (set Γ_1 Name ((\{ (\[ t_1 \] : t_5) (\[ t_6 \] : t_7) ... \})
weakness)))
(type_assign Γ_2
(any_1 ... (Name \[ t_1 \]) hole any_3 ...)
any_2
(t_2 ... t_5 hole t_4 ...)
t_3
Γ_3)
--------------------------------------------------------------------
(type_assign Γ_1
(any_1 ... hole any_2 any_3 ...)
(Name \[ t_1 \])
(t_2 ... hole t_3 t_4 ...)
t_5
Γ_3)]
[; var. bound to table
(where ((\{ (\[ t_6 \] : t_7) ... \}) weakness)
(index Γ_1 Name))
;new field
(side-condition
,(not (redex-match? lang-reach-defs
(any_1 ... (\[ (side-condition t_8
(equal? (term t_8)
(term t_1)))
\] : t_9) any_2 ...)
(term ((\[ t_6 \] : t_7) ...)))))
; update environment
(where Γ_2 (set Γ_1 Name ((\{ (\[ t_1 \] : t_5) (\[ t_6 \] : t_7) ... \})
weakness)))
--------------------------------------------------------------------
(type_assign Γ_1
(any_1 ... hole)
(Name \[ t_1 \])
(t_2 ... hole t_3 ...)
t_5
Γ_2)]
; local var
[(where t_5 (index Γ_1 Name))
; type assigned is a subtype
(side-condition (subtyping_rel t_4 t_5))
(type_assign Γ_1
(any_1 ... Name hole any_3 ...)
any_2
(t_1 ... t_4 hole t_3 ...)
t_2
Γ_2)
--------------------------------------------------------------------
(type_assign Γ_1
(any_1 ... hole any_2 any_3 ...)
Name
(t_1 ... hole t_2 t_3 ...)
t_4
Γ_2)]
[(where t_4 (index Γ Name))
; type assigned is a subtype
(side-condition (subtyping_rel t_3 t_4))
--------------------------------------------------------------------
(type_assign Γ
(any_1 ... hole)
Name
(t_1 ... hole t_2 ...)
t_3
Γ)]
; no rvalues left: variables are assigned nil values; no need to check
; table field
; field assign
[; var. bound to table
(where ((\{ (\[ t_4 \] : t_5) ... \}) weakness)
(index Γ Name))
; index type
(where (any_4 ... (\[ t_1 \] : t_6) any_5 ...) ((\[ t_4 \] : t_5) ...))
; type assigned is a subtype of the values' type
(side-condition (subtyping_rel t_3 t_6))
--------------------------------------------------------------------
(type_assign Γ
(any_1 ... hole any_2 any_3 ...)
(Name \[ t_1 \])
(t_2 ... hole)
t_3
Γ)]
; new field
[; var. bound to table
(where ((\{ (\[ t_4 \] : t_5) ... \}) weakness)
(index Γ_1 Name))
;new field
(side-condition
,(not (redex-match? lang-reach-defs
(any_1 ... (\[ (side-condition t_6
(equal? (term t_6)
(term t_1)))
\] : t_7) any_2 ...)
(term ((\[ t_4 \] : t_5) ...)))))
; update environment
(where Γ_2 (set Γ_1 Name ((\{ (\[ t_1 \] : t_3) (\[ t_4 \] : t_5) ... \})
weakness)))
--------------------------------------------------------------------
(type_assign Γ_1
(any_1 ... hole any_2 any_3 ...)
(Name \[ t_1 \])
(t_2 ... hole)
t_3
Γ_2)]
; loc. var
[(where t_3 (index Γ Name))
; type assigned is a subtype
(side-condition (subtyping_rel t_2 t_3))
--------------------------------------------------------------------
(type_assign Γ
(any_1 ... hole any_2 any_3 ...)
Name
(t_1 ... hole)
t_2
Γ)]
)
; extension to type_s, to manage generation of contexts for return stat.
(define-judgment-form
lang-reach-defs
#:mode (type_return I I I I I I O)
#:contract (type_return Γ Π cfgKG C C any Γ)
[(type_e Γ_1 Π cfgKG (in-hole C_1 (return e_2 ... hole e_3 e_4 ...)) e_1 Γ_2
_)
(type_return Γ_2 Π cfgKG C_1 (return e_2 ... e_1 hole e_4 ...) e_3 Γ_3)
--------------------------------------------------------------------
(type_return Γ_1 Π cfgKG C_1 (return e_2 ... hole e_3 e_4 ...) e_1 Γ_3)]
[(type_e Γ_1 Π cfgKG (in-hole C_1 (return e_2 ... hole)) e_1 Γ_2 _)
--------------------------------------------------------------------
(type_return Γ_1 Π cfgKG C_1 (return e_2 ... hole) e_1 Γ_2)]
)
; typecheck rvalues in local var. def.
(define-judgment-form
lang-reach-defs
#:mode (type_loc_rval I I I I I I O O)
#:contract (type_loc_rval Γ Π cfgKG C C any Γ (t ...))
[(type_e Γ_1 Π cfgKG
(in-hole C
(local (Name : t) ... = e_1 ... hole e_2 e_3 ... in
s end))
e_4 Γ_2 t_1)
(type_loc_rval Γ_2 Π cfgKG C
(local (Name : t) ... = e_1 ... e_4 hole e_3 ... in
s end)
e_2 Γ_3 (t_2 ...))
--------------------------------------------------------------------
(type_loc_rval Γ_1 Π cfgKG C
(local (Name : t) ... = e_1 ... hole e_2 e_3 ... in
s end)
e_4 Γ_3 (t_1 t_2 ...))]
[(type_e Γ_1 Π cfgKG
(in-hole C_1
(local (Name : t_1) ... = e_1 ... hole in
s end))
e_2 Γ_2 t_2)
--------------------------------------------------------------------
(type_loc_rval Γ_1 Π cfgKG C_1
(local (Name : t_1) ... = e_1 ... hole in s end)
e_2 Γ_2 (t_2))]
)
; bind type of rvalues with local vars, in Γ
(define-judgment-form
lang-reach-defs
#:mode (bind_loc_vars I I I I I O)
#:contract (bind_loc_vars Γ
((Name : t)... hole (Name : t) ...)
(Name : t)
(t ... hole ... t ...)
t
Γ)
; rvalues left to bind with Name
; Not the last var
[; check subtyping
(side-condition (subtyping_rel t_8 t_4))
; bind Name_4 with t_4
(where Γ_2 (set Γ_1 Name_4 t_4))
; bind remaining loc. vars.
(bind_loc_vars Γ_2 ((Name_1 : t_1) ... (Name_4 : t_4) hole (Name_3 : t_3)
...) (Name_2 : t_2)
(t_5 ... t_8 hole t_7 ...)
t_6 Γ_3)
--------------------------------------------------------------------
(bind_loc_vars Γ_1
((Name_1 : t_1) ... hole (Name_2 : t_2) (Name_3 : t_3) ...)
(Name_4 : t_4)
(t_5 ... hole t_6 t_7 ...)
t_8 Γ_3)]
; Last var
[; check subtyping
(side-condition (subtyping_rel t_5 t_2))
; bind Name_4 with t_4
(where Γ_2 (set Γ_1 Name_2 t_2))
--------------------------------------------------------------------
(bind_loc_vars Γ_1
((Name_1 : t_1) ... hole)
(Name_2 : t_2)
(t_3 ... hole t_4 ...)
t_5 Γ_2)]
; last rvalues
; Not the last var
[; check subtyping
(side-condition (subtyping_rel t_6 t_4))
; bind Name_4 with t_4
(where Γ_2 (set Γ_1 Name_4 t_4))
; bind remaining loc. vars.
(bind_loc_vars Γ_2 ((Name_1 : t_1) ... (Name_4 : t_4) hole (Name_3 : t_3)
...) (Name_2 : t_2)
(t_5 ... t_6)
t_6 Γ_3)