![Page 1: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/1.jpg)
LX: A Language for Flexible Type Analysis
Stephanie WeirichCornell University
joint work with Karl Crary (CMU)
![Page 2: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/2.jpg)
Terms Types
Source
IL
Machine
Typed Compilation
A series of translations between typed languages, propagating a set of invariants throughout the entire compilation process.Types are used for a variety of optimizations and provide safety assurances about the output of compiler.
![Page 3: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/3.jpg)
Because any array may be passed to a polymorphic function, all arrays must look the same, no matter the type of their elements.
A:array int B:array bool
sub = Fn a:Type => fn (A:array a,i:int) => wordsub(A,i)
Polymorphic Subscript
![Page 4: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/4.jpg)
In languages such as C, the type of an array is always known at compilation. We can pack boolean values into integer arrays.
A[2]
B[2]
intsub(A,2)
intsub(B,0)&(1<<2) <> 0
int A[4]
bool B[4]
Monomorphic subscript
![Page 5: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/5.jpg)
sub = Fn a:Type => fn (A:packedarray a,i:int) => typecase a of int => wordsub(A,i)
| bool => (wordsub(A,i div 32) & (1<<(i mod 32))) <> 0
Type Analysis ( iML )
A:array int
B:array bool
type packedarray(a:Type) = typecase a of int => array int
| bool => array int
![Page 6: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/6.jpg)
A Problem
int bool
int
SourceLanguage
TargetLanguage
type compilation
truefalse
0 1
term compilation
What if, during typed compilation, two source types map to the same target type?
![Page 7: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/7.jpg)
An initial attempt
Target language contains both source language and target language types, and has a built-in type constructor interp to translate between them.
sub = Fn a:S => fn (A:array (interp a),i:int) => typecase a of [int]S => wordsub(A,i)
| [bool]S => (wordsub(A,i div 32)
& (1<<(i mod 32))) <> 0
![Page 8: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/8.jpg)
Issues in Compilation
How do we preserve the meaning of typecase when the types themselves change?– type translation may not be injective– in TALx86, int int may be compiled into a
variety of types depending on the calling convention, register allocation, etc
– In closure conversion, a b converted to c. (a * c b) * c
• larger type takes longer to analyze• typecase is no longer exhaustive
![Page 9: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/9.jpg)
Goal
• Need a facility to describe the types of another language, and describe a translation of those types into the types of the current language.
• Need a way to examine those representations the term level
![Page 10: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/10.jpg)
Example
datakind S = SInt | SBool
interp : S -> Typeinterp = fn a:S =>
case a of SInt => int
| SBool => int
sub = Fn a:S => fn (A:array (interp a),i:int) =>
ccase a of SInt => wordsub(A,i) | SBool => (wordsub(A,i div 32)
& (1<<(i mod 32))) <> 0
![Page 11: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/11.jpg)
LX Language
• Type analysis is just a programming idiom
• System F augmented with building blocks for datakinds – tuples– sums– primitive recursion
• Strongly Normalizing so that type checking is decidable
• Term-level ccase
![Page 12: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/12.jpg)
Another Example
datakind M = Int
| Prod of M * M | Arrow of M * M
interp : M -> Type
fun interp ( a:M ) =
case a of Int => int
| Prod (c1,c2)) => interp(c1) * interp(c2)
| Arrow (c1,c2)) => interp(c1) interp(c2)
![Page 13: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/13.jpg)
Examplefun printf [a:M] (x:interp a) =>
ccase a of Int => (* x is of type int *)
print_int x
Prod(b,c) =>
print “<“; printf [b] (fst x);
print “,”; printf [c] (snd x);
print “>”
Arrow(b,c) =>
(* x is of type interp(b)interp(c) *) print “fun”
(* x is of type interp(Prod(b,c)) *)(* x is of type interp(b) * interp(c) *)
![Page 14: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/14.jpg)
LXvcaseRtype-erasuresemantics
LX
analyzeconstructed types
iMLtype-passing
semantics
analyzenative types
Type-Analyzing Languages
![Page 15: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/15.jpg)
Type-Passing Semantics
– Types are necessary at run time
– Requires sophisticated machinery to describe low level languages
Abstract kinds and translucent sums for polymorphic typed closure conversion
[MMH 96]
Type-Erasure Semantics
– Types may be erased prior to run time
– Standard type theory constructs suffice
Simpler typed closure conversion
[ MWCG 97][CWM 98]
![Page 16: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/16.jpg)
sub = Fn a:Type => fn (A:packedarray a,i:int) => typecase a of int => wordsub(A,i)
| bool => (wordsub(A,i div 32) & (1<<(i mod 32))) <> 0
Type Passing Example ( i
ML )
type packedarray(a:Type) = typecase a of int => array int
| bool => array int
![Page 17: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/17.jpg)
Type Erasure Example ( R )
type packedarray(a:Type) = typecase a of int => array int
| bool => array int
sub = Fn a:Type => fn (A:packedarray a, i:int, rx:R(a)) => typecase rx of
Rint => wordsub(A,i) | Rbool => (wordsub(A,i div 32)
& (1<<(i mod 32))) <> 0
![Page 18: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/18.jpg)
Example
datakind S = SInt | SBool
interp : S Type
interp = fn a:S =>
case a of SInt => int
| SBool => int
datatype SRep = RInt | RBool
![Page 19: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/19.jpg)
Example
sub = Fn a:S => fn (A:array (interp a),i:int, rx:SRep) => case rx of
RInt => ccase a of
SInt => wordsub(A,i)
| SBool => impossible
| RBool => ccase a of
SInt => impossible
| SBool => (wordsub(A,i div 32)
& (1<<(i mod 32))) <> 0
![Page 20: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/20.jpg)
Second Try
datatype SRep [a:S ] = RInt of
(case a of SInt => unit
| SBool => void)
| RBool of
(case a of SInt => void
| SBool => unit)
![Page 21: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/21.jpg)
Second Try
sub = Fn a:S => fn (A:array (interp a),i:int, rx:SRep(a)) => case rx of
RInt y => vcase a of
SInt => wordsub(A,i)
| SBool => dead y
| RBool y => vcase a of
Sint => dead y
| SBool => (wordsub(A,i div 32)
& (1<<(i mod 32))) <> 0
![Page 22: LX: A Language for Flexible Type Analysis Stephanie Weirich Cornell University joint work with Karl Crary (CMU)](https://reader030.vdocuments.us/reader030/viewer/2022033106/56649d785503460f94a5b5a2/html5/thumbnails/22.jpg)
Related Work
Inductive TypesMendler 87, Werner 94, Howard 92, 96,
Gordon 94
Type AnalysisHarper/Morrisett 95, Duggan 98, etc…
Type Erasure Crary/Weirich/Morrisett 98
Typed CompilationTIL - FLINT - TAL - Church