-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolic-extended.tex
2128 lines (1976 loc) · 82.3 KB
/
symbolic-extended.tex
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
\chapter{Recursive types and Intersection types}
\subsection{Compiling to \ltiFsub}
There are a least two ways to approach our external language
with symbolic closures compiling to \ltiFsub.
The main challenge and incentive is to remove all symbolic
closure types from our program and to replace them with
explicit annotations.
Our first approach is to copy function code and annotate
parameter types at each usage.
This approach seems promising, except there is a tension between
copying function code and creating runtime closures.
Our second approach is to extend \ltiFsub with intersection types
and ascribe each function an intersection type that
describes all the ways it was used as a symbolic closure.
We now explore our first approach, to copy function code as they
are checked.
First, for those functions ascribed symbolic closure types, we
copy their code and insert them at their usage sites.
For example, \clj{f} in
\begin{cljlisting}
(let [f (fn [x] x)]
(f 1)
(f "a"))
\end{cljlisting}
would be given a symbolic closure type, and
the program would be expanded like so:
\begin{cljlisting}
(let [f (fn [x] x)]
((fn [x] x) 1)
((fn [x] x) "a"))
\end{cljlisting}
First, we notice that all occurrences of \clj{f} have disappeared,
so it is safe to assume \clj{f} will never be called. It seems
reasonable to us to annotate its argument as \clj{Bot}.
\begin{cljlisting}
(let [f (fn [x :- Bot] x)]
((fn [x] x) 1)
((fn [x] x) "a"))
\end{cljlisting}
Next, the two unannotated functions would be checked as symbolic closures.
These checks would succeed, and then we could ascribe a parameter type on
each function.
\begin{cljlisting}
(let [f (fn [x :- Bot] x)]
((fn [x :- Int] x) 1)
((fn [x :- Str] x) "a"))
\end{cljlisting}
This program type checks with the rules of \ltiFsub.
However, subtle variations on this program are much
more puzzling to account for.
If \clj{f} closes over a variable, like \clj{v}
here
\begin{cljlisting}
(let [f (let [v 1] (fn [x] (print v) x))]
(f 1)
(f "a"))
\end{cljlisting}
then simply copying its function code will
not suffice.
If we do so, \clj{v} is no longer in scope:
\begin{cljlisting}
(let [f (let [v 1] (fn [x] (print v) x))]
((fn [x] (print v) x) 1)
((fn [x] (print v) x) "a"))
\end{cljlisting}
We could imagine inlining the value of
\clj{v} to work around this issue, but this is not a
full solution.
If instead, \clj{v} was an annotated function parameter
as in the next example
\begin{cljlisting}
(let [f (fn [v :- Int]
(fn [x] (print v) x))]
((f 42) 1)
((f 42) "a"))
\end{cljlisting}
it's even unclear how to inline the
unannotated function at all.
It seems the only choice is to inline \clj{f}
in its entirety, regardless if it was a symbolic closure.
\begin{cljlisting}
(let [f (fn [v :- Int]
(fn [x] (print v) x))]
(((fn [v :- Int]
(fn [x] (print v) x))
42)
1)
(((fn [v :- Int]
(fn [x] (print v) x))
42)
"a"))
\end{cljlisting}
This way we can at least annotate the missing \clj{Int} and \clj{Str}
parameter types.
It is now tempting to inline \emph{all} local variables with their
definitions.
This doesn't work in a language with side effects.
For example, inlining \clj{f} to ``fix'' the issues
in one of our previous examples would repeat side
effects, like printing \clj{"I only print once"} in the following
program (it, instead, prints thrice if \clj{f} is inlined).
\begin{cljlisting}
(let [f (let [v 1]
(print "I only print once.")
(fn [x] (print v) x))]
(f 1)
(f "a"))
\end{cljlisting}
Symbolic closures can also get their types by being passed
to annotated functions.
For example, \clj{id}
gets the type \clj{[Int -> Int]}
by being passed to \clj{f}, whose definition we will treat
as opaque, emulating a top-level function.
\begin{cljlisting}
(let [f (fn [g :- [Int -> Int]] ...)
id (fn [x] x)]
(f id))
\end{cljlisting}
By inlining \clj{id} and annotating its parameter \clj{Int},
this program does not pose any particular challenge.
\subsubsection{Intersection types}
If we allow ordered function intersection types,
featured in several optional type systems,
we arrive at an impasse.
Here, we assert that \clj{g} must \emph{both}
be
\clj{[Int -> Int]}
and
\clj{[Num -> Num]}.
\begin{cljlisting}
(let [f (fn [g :- (IFn [Int -> Int] [Num -> Num])] ...)
id (fn [x] x)]
(f id))
\end{cljlisting}
Clearly \clj{id} inhabits both these types,
however attempting to inline its definition
gets us nowhere.
Introducing intersection types gives us
one more trick up our sleeve: ascribing
\clj{id} as an \emph{intersection} of function types.
\begin{cljlisting}
(let [f (fn [g :- (IFn [Int -> Int] [Num -> Num])] ...)
id (fn [x :- Bot] x)]
(f (ann (fn [x] x)
(IFn [Int -> Int]
[Num -> Num]))))
\end{cljlisting}
This seems to help immensely, but now it seems a waste
to inline \clj{id} at all.
Instead, we could simplify this program by only annotating \clj{id}'s
right-hand-side.
\begin{cljlisting}
(let [f (fn [g :- (IFn [Int -> Int] [Num -> Num])] ...)
id (ann (fn [x] x)
(IFn [Int -> Int]
[Num -> Num]))]
(f id))
\end{cljlisting}
We can use this technique to help check our previous examples
without having to work around closed-over variables.
For example, our thrice-printing example requires
a single intersection type annotation, based on
the two symbolic reductions of \clj{f}.
\begin{cljlisting}
(let [f (let [v 1]
(print "I only print once.")
(ann (fn [x] (print v) x)
(IFn [Int -> Int]
[Str -> Str])))]
(f 1)
(f "a"))
\end{cljlisting}
Unfortunately, inferring ordered intersection types for unannotated
functions introduce other issues,
most prominently determining the ``best'' ordering of
arities.
Compared to unordered intersections, ordered intersections
have a simple application rule: first arity wins.
A ``best'' ordering would yield the same or more accurate return type
for every possible application, compared to every other ordering.
For now, we assume that a best ordering
exists for every ordered function intersection type, although
we are not sure.
There are several interesting cases we sketch to give an idea
of the character of this algorithm.
First, if arity 1's domain is a subtype of arity 2's
domain, then arity 1 should come first,
using subtyping of the range to break ties in a similar fashion.
For example, \clj{[Int -> Int]}
goes before \clj{[Num -> Int]},
but
\clj{[Int -> Int]}
precedes
\clj{[Int -> Num]}.
Second, if the domains of two arities
are incomparable via subtyping, their ordering does not matter.
For example,
\clj{[(Pair Int Num) -> Int]},
\clj{[(Pair Num Int) -> Num]},
and
\clj{[Int -> Num]}
may occur in any order in relation to each other.
This also accounts for multiple arguments,
by considering them as a list passed to a single argument.
Going back to checking programs, we now explore some
other ways in which inferring ordered function intersections has
interesting interactions with \ltiFsub.
The next example is similar to the one that helped motivate
intersection types earlier, except we omit the annotation
on \clj{f}.
\begin{cljlisting}
(let [f (fn [g]
(g 1)
(g "a"))
id (fn [x] x)]
(f id))
\end{cljlisting}
Here, the annotation on \clj{g} is the interesting part.
\begin{cljlisting}
(let [f (fn [g :- (IFn [Int -> Int] [Str -> Str])]
(g 1)
(g "a"))
id (ann (fn [x] x)
(IFn [Int -> Int] [Str -> Str]))]
(f id))
\end{cljlisting}
Unfortunately, we can't retroactively annotate all programs in this way.
Take the following program.
\begin{cljlisting}
(let [f (fn [g]
(fn [x]
(g x)))]
((f (fn [y] y)) 1)
((f (fn [z] z)) "a"))
\end{cljlisting}
Ideally, we would give \clj{f} the polymorphic type
\clj{(All [a] [[a -> a] -> [a -> a]])}.
Then, we would use the type variable to annotate its return
as \clj{[a -> a]}.
Instead, we infer \clj{f} as type
\begin{cljlisting}
(IFn [[Int -> Int] -> [Int -> Int]]
[[Str -> Str] -> [Str -> Str]])
\end{cljlisting}
This means that \clj{f}'s body will be checked twice.
The first time, \clj{g} will be assumed \clj{[Int -> Int]},
and then return checked as \clj{[Int -> Int]}.
The second, \clj{g} will be assumed \clj{[Str -> Str]},
and then return checked as \clj{[Str -> Str]}.
The problem now is annotating the function in \clj{f}'s body once-and-for-all.
It inhabits the type \clj{(U [Int -> Int] [Str -> Str])},
but that is too broad to be compatible with \clj{f}'s return---on the other hand,
it does not inhabit \clj{(IFn [Int -> Int] [Str -> Str])},
because \clj{g} cannot accept both \clj{Int} and \clj{Str}.
To handle these cases, we borrow \emph{conditional types} from TypeScript.
A conditional type is type-level dependency between types.
It is of the form \clj{(if (subtype? S T) U V)},
and returns type \clj{U} if \clj{S} is a subtype of \clj{T},
and \clj{V} otherwise.
This construct is particularly useful in combination with
the ability to reference the types of \emph{variables}.
In TypeScript, the type \clj{typeof f} resolves to the type of \clj{f}
in the current type environment. Here, we equivalently write \clj{(TypeOf f)}.
Applying these new type constructs to our example, we get the following annotation:
\begin{cljlisting}
(let [f (ann (fn [g]
(fn [x :- (if (subtype? (TypeOf g) [Int -> Int]) Int Str)]
(g x)))
(IFn [[Int -> Int] -> [Int -> Int]]
[[Str -> Str] -> [Str -> Str]]))]
((f (fn [y :- Int] y)) 1)
((f (fn [z :- Str] z)) "a"))
\end{cljlisting}
Now when \clj{f}'s first function type is checked,
\clj{g} will be of type \clj{[Int -> Int]}, which annotates
\clj{x} as \clj{Int}.
Correspondingly for the second function type,
\clj{g} will be of type \clj{[Str -> Str]}, which annotates
\clj{x} as \clj{Str} via the conditional type's else-branch.
We note that special consideration of variable shadowing is required when using \clj{TypeOf}---for
example, if \clj{g} was shadowed above, we would be branching on the wrong type.
\subsubsection{Polymorphism}
We have not addressed how symbolic closures interact with polymorphic types.
For now, we consider a restricted subset of polymorphic functions, but which happens to be
common in Clojure code.
Anecdotally, they are higher-order functions that take in functions cannot be iterated.
For example, the \clj{map} function is roughly of type:
\begin{cljlisting}
(All [a b]
[[a -> b] (Seqable a) -> (Seqable b)])
\end{cljlisting}
We can immediately see the data flow by the occurrences of type variables.
The function argument takes an \clj{a} from the collection argument,
and then returns a \clj{b} to the return collection.
The function argument cannot be called on its own output in the body
of \clj{map} because \clj{b} is not compatible with \clj{a}.
We can draw these dependencies as arrows---notice that there is no arrow
from \clj{b} to \clj{a}. There are implicit dependencies from
\clj{a} to \clj{b} because everything to the right of an arrow type depends
on everything to the left of the arrow (or, output values depend
on input values).
\begin{lstlisting}
(All [a b]
[[(*@\tikz[overlay,remember picture] \node [] (b) {};@*)a -> (*@\tikz[overlay,remember picture] \node [] (c) {};@*)b] (Seqable (*@\tikz[overlay,remember picture] \node [] (a) {};@*)a) -> (Seq (*@\tikz[overlay,remember picture] \node [] (d) {};@*)b)])
\end{lstlisting}
\begin{tikzpicture}[remember picture, overlay,
text width = 2.5cm ]
\coordinate (Start1) at (a);
\coordinate (End1) at (b);
\coordinate (Start2) at (c);
\coordinate (End2) at (d);
\draw[red,->,bend right=-45](Start1.south) to (End1.east);
\draw[blue,->,bend right=-45](Start2.east) to (End2.north east);
\end{tikzpicture}
Inferring the data flow is crucial to checking symbolic closures.
Take the following example, where the function argument is inferred
as a symbolic closure.
\begin{cljlisting}
(map (fn [x] x) [1 2 3])
\end{cljlisting}
We now have two jobs: to infer the type arguments to \clj{map}
and to infer the type of \clj{x}.
Both can be found simultaneously by solving constraints
to find optimal instantiations for \clj{a} and \clj{b}.
First, we collect the constraints that make
\clj{(Closure \{\} (fn [x] x))}
a subtype of
\clj{[a -> b]}.
By checking the function with annotations
\clj{(fn [x :- a] x)},
we know that \clj{a} flows into \clj{b}, so
we get the constraint
\clj{Bot <: a <: b}.
For the second argument to \clj{map}
we infer
\clj{Int <: a <: Top}.
Since both type variables occur invariantly, we use their smallest instantiations,
so the optimal solution to these constraints
is \clj{a = Int, b = Int}.
We use this substitution to both
both provide the type arguments to \clj{map} (via \clj{inst})
and function argument (by substituting away the \clj{a} that
exercised the symbolic closure).
\begin{cljlisting}
((inst map Int Int) (fn [x :- Int] x) [1 2 3])
\end{cljlisting}
We can combine this inference technique with the same approach
we used to check our previous let-bound function examples,
like in the following code.
\begin{cljlisting}
(let [f (fn [x] x)]
(map f [1 2 3])
(map f ["a" "b" "c"]))
\end{cljlisting}
Here, we infer type arguments for each usage of \clj{map},
and combine the information collected for \clj{f} from both
inferences into an intersection type, yielding:
\begin{cljlisting}
(let [f (ann (fn [x] x)
(IFn [Int -> Int]
[Str -> Str]))]
((inst map Int Int) f [1 2 3])
((inst map Str Str) f ["a" "b" "c"]))
\end{cljlisting}
Furthermore, this approach plays nicely with inferring conditional types,
like in the next example:
\begin{cljlisting}
(let [f (fn [g]
(fn [x]
(map g x)))]
((f (fn [y] y)) [1 2 3])
((f (fn [z] z)) ["a" "b" "c"]))
\end{cljlisting}
Now we must both infer an annotation for \clj{x} and
the type arguments to \clj{map}, but, as
in example motivating conditional types,
this is aggravated by \clj{f} being given a
intersection type, and thus forcing its body to be checked twice.
The solution is to to use more conditional types, particularly
as in the instantiation of \clj{map}:
\begin{cljlisting}
(let [f (ann (fn [g]
(fn [x :- (Seqable (if (subtype? (TypeOf g) [Int -> Int]) Int Str))]
((inst map
(if (subtype? (TypeOf x) (Seqable Int)) Int Str)
(if (subtype? (TypeOf x) (Seqable Int)) Int Str))
g x)))
(IFn [[Int -> Int] -> [(Seqable Int) -> (Seqable Int)]]
[[Str -> Str] -> [(Seqable Str) -> (Seqable Str)]]))]
((f (fn [y :- Int] y)) [1 2 3])
((f (fn [z :- Str] z)) ["a" "b" "c"]))
\end{cljlisting}
\subsubsection{Inferring Conditional types}
The most attractive use of conditional types is to check the
same piece of code at different types.
\begin{figure}
$$
\begin{array}{lrll}
\ltiE{}, \ltiF{} &::=& ... \alt
\ltifuntparaminterface{\ova{\ltitvar{}}}
{\ova
{\ltistackmapping{\ltiEnv{}}
{\ltiIFn{\ova{\ltiFn{\ltiT{}}{\ltiT{}}}}}}}
{\ltivar{}}
{\ltiE{}}
\alt
\ltiappinst{\ltiF{}}{\ova{\ltistackmapping{\ltiEnv{}}{\ova{\ltiR{}}}}}{\ltiE{}} \alt
&\mbox{Terms} \\
\ltiappinst{\ltiF{}}{\ova{\ltiR{}}}{\ltiE{}} &\Leftrightarrow&
\ltiappinst{\ltiF{}}{\ltistackmapping{\ltiEmptyEnv{}}{\ova{\ltiR{}}}}{\ltiE{}}\\
\ltifuntparaminterface{\ova{\ltitvar{}}}
{\ltiT{}}
{\ltivar{}}
{\ltiE{}}
&\Leftrightarrow&
\ltifuntparaminterface{\ova{\ltitvar{}}}
{\ltistackmapping{\ltiEmptyEnv{}}{\ltiT{}}}
{\ltivar{}}
{\ltiE{}}
&\mbox{Term abbreviations} \\
\ltiT{}, \ltiS{}, \ltiR{} &::=& ...
\alt
\ltiMu{\ltitvar{}}{\ltiT{}}
\alt
\ltiIFn{\ova{\ltiFn{\ltiT{}}{\ltiT{}}}}
&\mbox{Types} \\
\ltiSubtypeSeen{} &::=& \ova{\ltiSeenEntry{\ltiT{}}{\ltiT{}}}
&\mbox{Subtype Seen List} \\
\end{array}
$$
\caption{Internal Language Syntax Extensions}
\label{symbolic:figure:internal-language-mu-intersection}
\end{figure}
\begin{figure}
\begin{mathpar}
\infer [I-AppInst]
{
\ltitjudgement{\ltiEnv{}}
{\ltiF{}}
{\ltiT{}^f}
{\ltiFp{}}
\\
\ltitjudgement{\ltiEnv{}}
{\ltiE{}}
{\ltiT{}^a}
{\ltiEp{}}
\\
\ltiunfold{\ltiT{}^f}
{\ltiPoly{\ova{\ltitvar{}}}{\ltiIFn{\ova{\ltiFn{\ltiT{}}{\ltiS{}}}^n}}}
\\\\
\exists i \in 1...m.
\ltiunifyContexts{\ltiInternalOrExternalLang{}}{\ltistackmapping{\ltiEnvp{i}}{\ova{\ltiRp{}}_i}}{\ltiEnv{}}{\ova{\ltiR{}}} \}
\\\\
\ova{\ltiSp{}}
=
\left\{ {\ltireplace{\ova{\ltiR{}}}{\ova{\ltitvar{}}}{\ltiS{i}}}
\middle| i \in 1 ... n,
\text{ if }
\ltiisubtype{\ltiEnv{}}{\ltiT{}^a}{\ltireplace{\ova{\ltiR{}}}{\ova{\ltitvar{}}}{\ltiT{i}}}
\right\}
\\
|\ova{\ltiSp{}}|>0
}
{
\ltitjudgement{\ltiEnv{}}
{\ltiappinst{\ltiF{}}
{\ova{\ltistackmapping{\ltiEnvp{}}{\ova{\ltiRp{}}}}^m}
{\ltiE{}}}
{\ltiMeetMany{\ova{\ltiSp{}}}}
{\ltiappinst{\ltiFp{}}
{\ova{\ltistackmapping{\ltiEnvp{}}{\ova{\ltiRp{}}}}^m}
{\ltiEp{}}}
}
\infer [I-App\Bot]
{
\ltitjudgement{\ltiEnv{}}
{\ltiF{}}
{\ltiT{}}
{\ltiFp{}}
\\\\
\ltitjudgement{\ltiEnv{}}
{\ltiE{}}
{\ltiS{}}
{\ltiEp{}}
\\\\
\ltiunfold{\ltiT{}}{\ltiBot}
}
{
\ltitjudgement{\ltiEnv{}}
{\ltiappinst{\ltiF{}}{\ova{\ltistackmapping{\ltiEnvp{}}{\ova{\ltiR{}}}}}{\ltiE{}}}
{\ltiBot{}}
{\ltiappinst{\ltiFp{}}{\ova{\ltistackmapping{\ltiEnvp{}}{\ova{\ltiR{}}}}}{\ltiEp{}}}
}
\infer [I-Abs]
{
\exists i \in 1...m.
\ltiunifyContexts{\ltiInternalOrExternalLang{}}
{\ltistackmapping{\ltiEnvp{i}}{\ltiTp{i}}}
{\ltiEnv{}}
{\ltiIFn{\ova{\ltiFn{\ltiT{}}{\ltiS{}}}^n}}
\\\\
n>0
\\
\overrightarrow{
\ltitjudgement{\ltiEnvConcat{\ltiEnv{}}
{\ltiEnvConcat{\ova{\ltitvar{}}}
{\hastype{\ltivar{}}{\ltiT{i}}}}}
{\ltiE{}}
{\ltiSp{i}}
{\ltiF{i}}
\ \ \
\ltiisubtype{\ltiEnv{}}{\ltiSp{i}}{\ltiS{i}}
}^{1 \leq i \leq n}
\\\\
\ltimergeTaggedTermsLHS{\ltiE{}}{\ltimergeTaggedTermsLHS{\ltiF{1}}{\ltimergeTaggedTermsLHS{...}{\ltiF{n}}}}
= \ltiEp{}
}
{
\ltitjudgement{\ltiEnv{}}
{\ltifuntparaminterface{\ova{\ltitvar{}}}
{\ova{\ltistackmapping{\ltiEnvp{}}{\ltiTp{}}}^m}
{\ltivar{}}
{\ltiE{}}}
{\ltiPoly{\ova{\ltitvar{}}}{\ltiIFn{\ova{\ltiFn{\ltiT{}}{\ltiS{}}}^n}}}
{\ltifuntparaminterface{\ova{\ltitvar{}}}
{\ova{\ltistackmapping{\ltiEnvp{}}{\ltiTp{}}}^m}
{\ltivar{}}
{\ltiEp{}}}
}
\infer [I-Sel]
{
\ltitjudgement{\ltiEnv{}}
{\ltiE{}}
{\ltiS{}}
{\ltiF{}}
\\\\
\ltiunfold{\ltiS{}}
{\ltiRec{\hastype{\ltivar{1}}{\ltiT{1}}, ..., \hastype{\ltivar{i}}{\ltiT{i}} , ..., \hastype{\ltivar{n}}{\ltiT{n}}}}
}
{
\ltitjudgement{\ltiEnv{}}
{\ltisel{\ltiE{}}{\ltivar{i}}}
{\ltiT{i}}
{\ltisel{\ltiF{}}{\ltivar{i}}}
}
\infer [I-Sel\ltiBot]
{
\ltitjudgement{\ltiEnv{}}
{\ltiE{}}
{\ltiT{}}
{\ltiF{}}
\\\\
\ltiunfold{\ltiT{}}{\ltiBot}
}
{
\ltitjudgement{\ltiEnv{}}
{\ltisel{\ltiE{}}{\ltivar{}}}
{\ltiBot}
{\ltisel{\ltiF{}}{\ltivar{}}}
}
\end{mathpar}
\caption{Internal language type system extensions
}
\label{symbolic:figure:internal-language-type-system-mu-intersection}
\end{figure}
\begin{figure}
\begin{mathpar}
\boxed{
\infer[]
{}
{
\ltiunfold{\ltiT{}}{\ltiS{}}
\\\\
\text{ Unfold top-level recursion in \ltiT{} to \ltiS{}.}
}
}
\begin{array}{lcl}
\ltiunfoldalign{\ltiPoly{\ova{\ltitvar{}}}{\ltiT{}}}
{\ltiPoly{\ova{\ltitvar{}}}{\ltiunfoldLHS{\ltiT{}}}}\\
\ltiunfoldalign{\ltiMu{\ltitvar{}}{\ltiT{}}}
{\ltireplace{\ltiMu{\ltitvar{}}{\ltiT{}}}
{\ltitvar{}}
{\ltiunfoldLHS{\ltiT{}}}}
\\
\ltiunfoldalign{\ltiT{}}{\ltiT{}} \text{, otherwise}\\
\end{array}
\boxed{
\infer[]
{}
{
\ltimergeTaggedTerms{\ltiE{1}}{\ltiE{2}}{\ltiF{}}
\\\\
\text{Merge terms \ltiE{i} as \ltiF{}.}
}
}
\boxed{
\infer[]
{}
{
\ltiunifyContexts{\ltiInternalOrExternalLang{}}{\ltistackmapping{\ltiEnvp{}}{\ova{\ltiT{}}}}{\ltiEnv{}}{\ova{\ltiS{}}}
\\\\
\text{Prepare \ova{\ltiT{}} for use in current context \ltiEnv{}.}
}
}
\begin{array}{lllll}
\ltiunifyContextsalign{\ltiInternalOrExternalLang{}}
{\ltistackmapping{\ltiEmptyEnv{}}{\ova{\ltiT{}}}}
{\ltiEnv{}}
{\ova{\ltiT{}}} \\
\ltiunifyContextsalign{\ltiInternalOrExternalLang{}}
{\ltistackmapping{\ltiEnvConcatParen{\ltitvarp{}}{\ltiEnvp{}}}{\ova{\ltiT{}}}}
{\ltiEnvConcatParen{\ltitvar{}}{\ltiEnv{}}}
{\ltiunifyContextsLHS{\ltiInternalOrExternalLang{}}
{\ltireplace{\ltitvar{}}{\ltitvarp{}}
{(\ltistackmapping{\ltiEnvp{}}{\ova{\ltiT{}}})}}
{\ltiEnv{}}}\\
\ltiunifyContextsalign{\ltiInternalOrExternalLang{}}
{\ltistackmapping{\ltiEnvConcatParen{\hastype{\ltivarp{}}{\ltiSp{}}}{\ltiEnvp{}}}{\ova{\ltiT{}}}}
{\ltiEnvConcatParen{\hastype{\ltivar{}}{\ltiS{}}}{\ltiEnv{}}}
{\ltiunifyContextsLHS{\ltiInternalOrExternalLang{}}
{\ltistackmapping{\ltiEnvp{}}{\ova{\ltiT{}}}}
{\ltiEnv{}}},
&\text{ if } \ltiisubtype{\ltiEnvpp{}}{\ltiS{}}{\ltiSp{}}
\end{array}
\begin{array}{llll}
\ltimergeTaggedTermsalign{\ltifuntparaminterface{\ova{\ltitvar{}}}{\ltiT{}}{\ltivar{}}{\ltiE{1}}}
{\ltifuntparaminterface{\ova{\ltitvar{}}}{\ltiT{}}
{\ltivar{}}
{\ltiE{2}}}
{\ltifuntparaminterface{\ova{\ltitvar{}}}{\ltiT{}}
{\ltivar{}}
{\ltimergeTaggedTermsLHS{\ltiE{1}}{\ltiE{2}}}}
\\
\ltimergeTaggedTermsalign{\ltiappinst{\ltiF{}}{\ova{\ltistackmapping{\ltiEnv{}}{\ova{\ltiR{}}}}}{\ltiE{}}}
{\ltiappinst{\ltiFp{}}{\ova{\ltistackmapping{\ltiEnv{}}{\ova{\ltiR{}}}}}{\ltiEp{}}}
{\ltiappinst{\ltimergeTaggedTermsLHS{\ltiF{}}{\ltiFp{}}}
{\ova{\ltistackmapping{\ltiEnv{}}{\ova{\ltiR{}}}}}
{\ltimergeTaggedTermsLHS{\ltiE{}}{\ltiEp{}}}}
\\
\ltimergeTaggedTermsalign{\ltisel{\ltiE{1}}{\ltivar{}}}
{\ltisel{\ltiE{2}}{\ltivar{}}}
{\ltisel{\ltimergeTaggedTermsLHS{\ltiE{1}}{\ltiE{2}}}{\ltivar{}}}
\\
\ltimergeTaggedTermsalign{\ltiRec{\ova{\ltivar{} = \ltiE{}}}}
{\ltiRec{\ova{\ltivar{} = \ltiF{}}}}
{\ltiRec{\ova{\ltivar{} = \ltimergeTaggedTermsLHS{\ltiE{}}{\ltiF{}}}}}
\\
\ltimergeTaggedTermsalign{\ltivar{}}
{\ltivar{}}
{\ltivar{}}
\end{array}
\end{mathpar}
\caption{Extended Type System Metafunctions}
\label{symbolic:figure:internal-language-metafunctions}
\end{figure}
\begin{figure}
\begin{mathpar}
\boxed{
\infer[]
{}
{
\ltiisubtypeseen{\ltiSubtypeSeen{}}{\ltiEnv{}}{\ltiT{}}{\ltiS{}}
\\\\
\text{\ltiT{} is a subtype of \ltiS{},
}
\\\\
\text{with seen queries \ltiSubtypeSeen{}.
}
}
}
\infer [S-MuL]
{
\ltiSeenEntry{\ltiMu{\ltitvar{}}{\ltiT{}}}{\ltiS{}} \in \ltiSubtypeSeen{}
\\\\
\text{ or }
\\\\
\ltiisubtypeseen{\ltiSeenConcat{\ltiSeenEntry{\ltiMu{\ltitvar{}}{\ltiT{}}}
{\ltiS{}}}
{\ltiSubtypeSeen{}}}
{\ltiEnv{}}
{\ltireplace{\ltiMu{\ltitvar{}}{\ltiT{}}}{\ltitvar{}}{\ltiT{}}}{\ltiS{}}
}
{
\ltiisubtypeseen{\ltiSubtypeSeen{}}{\ltiEnv{}}
{\ltiMu{\ltitvar{}}{\ltiT{}}}
{\ltiS{}}
}
\infer [S-MuR]
{
\ltiSeenEntry{\ltiS{}}{\ltiMu{\ltitvar{}}{\ltiT{}}} \in \ltiSubtypeSeen{}
\\\\
\text{ or }
\\\\
\ltiisubtypeseen{\ltiSeenConcat{\ltiSeenEntry{\ltiS{}}
{{\ltiMu{\ltitvar{}}{\ltiT{}}}}}
{\ltiSubtypeSeen{}}}
{\ltiEnv{}}
{\ltiS{}}
{\ltireplace{\ltiMu{\ltitvar{}}{\ltiT{}}}{\ltitvar{}}{\ltiT{}}}
}
{
\ltiisubtypeseen{\ltiSubtypeSeen{}}
{\ltiEnv{}}
{\ltiS{}}
{\ltiMu{\ltitvar{}}{\ltiT{}}}
}
\infer [S-IFn]
{
\forall i \in 1...n.\
\exists j \in 1...m.\
\ltiisubtypeseen{\ltiSubtypeSeen{}}{\ltiEnv{}}{\ltiS{j}}{\ltiT{i}}
}
{ \ltiisubtypeseen{\ltiSubtypeSeen{}}{\ltiEnv{}}
{\ltiIFn{\ova{\ltiS{}}^m}}
{\ltiIFn{\ova{\ltiT{}}^n}}
}
% \infer [SF-ContextBoth]
% {
% \ltiisubtypeseen{\ltiSubtypeSeen{}}
% {\ltiEnvpp{}}
% {\ltiunifyContextsLHS{\ltiinternallabel}{\ltistackmapping{\ltiEnvpp{}}{\ltiS{}}}{\ltiEnvp{}}}
% {\ltiT{}}
% }
% {\ltiisubtypeseen{\ltiSubtypeSeen{}}
% {\ltiEnv{}}
% {(\ltistackmapping{\ltiEnvp{}}{\ltiS{}})}
% {(\ltistackmapping{\ltiEnvpp{}}{\ltiT{}})}
% }
%
% \infer [SF-ContextL]
% {
% \ltiisubtypeseen{\ltiSubtypeSeen{}}
% {\ltiEnv{}}
% {\ltiunifyContextsLHS{\ltiinternallabel}{\ltistackmapping{\ltiEnvp{}}{\ltiS{}}}{\ltiEnv{}}}
% {\ltiT{}}
% }
% {\ltiisubtypeseen{\ltiSubtypeSeen{}}
% {\ltiEnv{}}
% {(\ltistackmapping{\ltiEnvp{}}{\ltiS{}})}
% {\ltiT{}}
% }
%
% \infer [SF-ContextR]
% {
% \ltiisubtypeseen{\ltiSubtypeSeen{}}
% {\ltiEnv{}}
% {\ltiS{}}
% {\ltiunifyContextsLHS{\ltiinternallabel}{\ltistackmapping{\ltiEnvp{}}{\ltiT{}}}{\ltiEnv{}}}
% }
% {\ltiisubtypeseen{\ltiSubtypeSeen{}}
% {\ltiEnv{}}
% {\ltiS{}}
% {(\ltistackmapping{\ltiEnvp{}}{\ltiT{}})}
% }
\end{mathpar}
\caption{Internal language subtyping extensions
}
\label{symbolic:figure:internal-language-subtyping-mu-intersection}
\end{figure}
\begin{figure}
\begin{mathpar}
% \infer [Var]
% {}
% {
% \ltitSdjudgement{\ltiEnv{}}
% {\ltivar{}}
% {\ltiEnvLookup{\ltiEnv{}}{\ltivar{}}}
% {\ltivar{}}
% }
% \infer [Sel]
% {
% \ltitSdjudgement{\ltiEnv{}}
% {\ltiF{}}
% {\ltiS{}}
% {\ltiFp{}}
% \\\\
% \ltiSdsubtype{\ltiEnv{}}{\ltiS{}}{\ltiRec{\hastype{\ltivar{1}}{\ltiT{1}},...,\hastype{\ltivar{i}}{\ltiT{i}},...,\hastype{\ltivar{n}}{\ltiT{n}}}}
% }
% {
% \ltitSdjudgement{\ltiEnv{}}
% {\ltisel{\ltiF{}}{\ltivar{i}}}
% {\ltiT{i}}
% {\ltisel{\ltiFp{}}{\ltivar{i}}}
% }
%
% \infer [Rec]
% {
% \overrightarrow{
% \ltitSdjudgement{\ltiEnv{}}
% {\ltiF{i}}
% {\ltiT{i}}
% {\ltiFp{i}}
% }
% ^{1 \leq i \leq n}
% }
% {
% \ltitSdjudgement{\ltiEnv{}}
% {\ltiRec{\ova{\ltivar{} = \ltiF{}}^n}}
% {\ltiRec{\ova{\hastype{\ltivar{}}{\ltiT{}}}^n}}
% {\ltiRec{\ova{\ltivar{} = \ltiFp{}}^n}}
% }
\infer [E-AppInf]
{
\ltitjudgement{\ltiEnv{}}
{\ltiF{}}
{\ltiS{}^f}
{\ltiFp{}}
\\
\ltitjudgement{\ltiEnv{}}
{\ltiE{}}
{\ltiS{}^a}
{\ltiEp{}}
\\\\
\ltiisubtype{\ltiEnv{}}
{\ltiS{}^f}
{\ltiPoly{\ova{\ltitvar{}}}
{\ltiIFn{\ltiFn{\ltiT{}}{\ltiS{}}}}}
\\
|\ova{\ltitvar{}}|>0
\\\\
\forall \ltiRp{}.
\left(
\begin{array}{lll}
\ltiisubtype{\ltiEnv{}}{\ltiS{}^a}{\ltireplace{\ova{\ltiRp{}}}{\ova{\ltitvar{}}}{\ltiT{}}}
\text{ implies}
\arcr
\ltiisubtype{\ltiEnv{}}{\ltireplace{\ova{\ltiR{}}}{\ova{\ltitvar{}}}{\ltiS{}^a}}
{\ltireplace{\ova{\ltiRp{}}}{\ova{\ltitvar{}}}{\ltiS{}^a}}
\end{array}
\right)
}
{
\ltitjudgement{\ltiEnv{}}
{\ltiapp{\ltiF{}}{\ltiE{}}}
{\ltireplace{\ova{\ltiR{}}}{\ova{\ltitvar{}}}{\ltiS{}}}
{\ltiappinst{\ltiFp{}}
{\ltistackmapping{\ltiEnv{}}{\ova{\ltiR{}}}}
{\ltiEp{}}}
}
\infer [E-UAbs]
{
\exists i \in 1...m.
\ltiunifyContexts{\ltiInternalOrExternalLang{}}
{\ltistackmapping{\ltiEnvp{i}}{\ltiTp{i}}}
{\ltiEnv{}}
{\ltiIFn{\ova{\ltiFn{\ltiT{}}{\ltiS{}}}^n}}
\\\\
n>0
\\\\
\overrightarrow{
\ltitjudgement{\ltiEnvConcat{\ltiEnv{}}
{\ltiEnvConcat{\ova{\ltitvar{}}}
{\hastype{\ltivar{}}{\ova{\ltiT{i}}}}}}
{\ltiE{}}
{\ltiSp{i}}
{\ltiF{i}}
\ \ \
\ltiisubtype{\ltiEnv{}}{\ltiSp{i}}{\ltiS{i}}
}^{1 \leq i \leq n}
\\\\
\ltimergeTaggedTermsLHS{\ltiE{}}{\ltimergeTaggedTermsLHS{\ltiF{1}}{\ltimergeTaggedTermsLHS{...}{\ltiF{n}}}} = \ltiEp{}
}
{
\ltitjudgement{\ltiEnv{}}
{\ltiufun{\ltivar{}}{\ltiE{}}}
{\ltiPoly{\ova{\ltitvar{}}}{\ltiIFn{\ova{\ltiFn{\ltiT{}}{\ltiS{}}}^n}}}
{\ltifuntparaminterface{\ova{\ltitvar{}}}
{\ova{\ltistackmapping{\ltiEnvp{}}{\ltiTp{}}}}
{\ltivar{}}
{\ltiEp{}}}
}
% \infer [AppInst]
% {
% \ltitSdjudgement{\ltiEnv{}}
% {\ltiF{}}
% {\ltiT{}^f}
% {\ltiFp{}}
% \\
% \ltitSdjudgement{\ltiEnv{}}
% {\ltiE{}}
% {\ltiTp{}}
% {\ltiEp{}}
% \\\\
% \ltiSdsubtype{\ltiEnv{}}{
% \ltiresolveLHS{\ltiexternallanglabel}
% {\ltiEnv{}}
% {\ltiT{}^f}}
% {
% {\ltiPoly{\ova{\ltitvar{}}}{\ltiSplitIFn{\ova{\ltiFn{\ltiT{}}{\ltiS{}}}^n}
% {\ova{\ltiContextualFn{\ltiEnvpp{}}{\ltiTpp{}}{\ltiSpp{}}}}}}
% }
% \\\\
% m > 0
% \\
% \ltiLfindTA{\ltiexternallanglabel}{\ltiEnv{}}{\ova{\ltitvar{}}}{\ova{\ltistackmapping{\ltiEnvp{}}{\ova{\ltiRp{}}}}}{\ova{\ltiR{}}}
% \\\\
% \ova{\ltiSp{}}^m
% =
% \{ \ltiS{i}\ |\ i \in 1 ... n, \ltiSdsubtype{\ltiEnv{}}{\ltiT{}^a}{\ltireplace{\ova{\ltiR{}}}{\ova{\ltitvar{}}}{\ltiT{i}}}
% \}
% }