Title: Intensional Polymorphism in Type-Erasure Semantics
1Intensional Polymorphism in Type-Erasure Semantics
- Karl Crary, Stephanie Weirich, Greg Morrisett
Presentation by Nate Waisbrot
2Authors' background
Part of Cornell University's TAL (Typed Assembly
Language) group Creating intermediate languages
to compile into TAL
3Three styles of polymorphism
- Get a value and inspect its type
- Easy and efficient, but not powerful
- Pass types as first-class objects
- Powerful, but slow and complicated for the
compiler - Pass values which represent types
- Best of both worlds
4Passing values
- Recall the paper "Dynamically Typing in a
Statically Typed Language" (Abadi, Cardelli,
Pierce, and Plotkin) - Types are tied to values -- you can't have one
without the other
5Dynamic types
Dynamic(5 ? int) dynamic value f dynamic ?
dynamic dynamic function ?xdynamic. typecase x
of int ? ??? ? ? ? ? ?
6Passing types
- Values are described by types
- Types are described by kinds
- We could keep going (System F?)
7Overview of System F
??.?x?.x The polymorphic identity
function ??.??? The type of the identity
function iden int 5 Application of the identity
function
??.??.?x?.?y?. (x ? y) a pair function ??.??.
(? ? ?) its type pair int string 0
zero application
8Overview of ?iML
Start with System F, add the ability to work with
types alone ??.??. ? ? ? the pair-type
function Type?Type?Type the kind of the pair-type
function pairT int string this gives us the
type (int ? string) Now add the typecase
construct typecase ?.int ? substitute ? for ?
and return an int int ? 0
9?iML syntax
? Type ??? c ? int c?c c ? c
???.c c c Typerec c(cint, c?, c?) ? c
??? ? ? ? ???.? e x i ?x?.e fix
f?.v e e lte,egt ? e ???.v ec
typecase ?.? v i ?x?.e fix f?.v
ltv,vgt ???.v
10Typing hierarchy
Type?Type
Type
kinds
int
int?int
??.?
types
?xint.x
5
6
values
11What's wrong with type passing?
- Types must be kept separate from values
- Doubles the type-checker's work
- Compiling it down to TAL is hard
- Language semantics require types to always be
passed
12Solution type representations
- Make new type, "representation"
- Get back all the simplicity of normal
value-passing - As a bonus, gain some abstraction
13Overview of type representations
R?(Rint,Rint) The representation of int?int R(
R(int)?R(int) ) Its type
14Syntax of ?R
? Type ??? c ? int c?c c ? c
???.c c c R(c) Typerec c(cint, c?, c?,
cR) ? c ??? ? ? ? ???.? ???.? e
x i ?x?.e fix f?.v e e lte,egt ?
e ???.v ec pack e as ??.? hiding ?
unpack lt?,xgt e in e Rint R?(e,e)
R?(e,e) RR(e) typecase ?.c v i
?x?.e fix f?.v ltv,vgt ???.v pack v as
??.? hiding ? Rint R?(v,v) R?(v,v) RR(v)
15TypeToString dynamic types
typeToString dynamic?string ?xdynamic. typeca
se x of int ? "int" string ? "string" ??? ?
"function" ?? ? "lt" typeToString Dynamic(? ?
?1x) "," typeToString Dynamic(? ? ?2x)
"gt"
?
?
16TypeToString type-passing
fix typeToString ??Type.string
. ??Type. typecase ?.string ? of int ?
"int" string ? "string" ??? ? typeToString
? "?" typeToString ? ?? ? "lt"
typeToString ? "," typeToString ?
"gt"
17TypeToString type representations
fix typeToString ??Type.R(?)?string
. ??Type.?xR(?). typecase ?.string x
of Rint ? "int" Rstring ? "string" R?(x,y)
as ??? ? typeToString ? x "?"
typeToString ? y R(x,y) as ?? ? "lt"
typeToString ? x "," typeToString ?
y "gt"
18Type erasure
- A well-typed program in typed ?-calculus has an
equivalent in untyped ?-calculus - Typed ?x?.x
- Untyped ?x.x
19TypeToString with types erased
fix typeToString ??Type.R(?)?string
. ??Type.?xR(?). typecase ?.string x
of Rint ? "int" Rstring ? "string" R?(x,y)
as ??? ? typeToString ? x "?"
typeToString ? y R(x,y) as ?? ? "lt"
typeToString ? x "," typeToString ?
y "gt"
20Type refinement
- Do dead-code elimination based on the type of the
representation - Propagate information about the type of an
argument back through the function
21Monomorphic closure
?x?.y A function with a free variable ??? Its
type (if y has type ?) We want to eliminate
free variables f (?(x ? e)? ? env.?ye) ?
ltygt The closed function ?env.((? ? env) ? ?) ?
env Its type
22Polymorphic closure
The same closed function, with type
passing ??Kind. ?envType. ???. ????. ((?
? env) ? ?) ? env
23Closure with representations
f ????x(? ? R(?)). ?2 x f (? ? R(?)) ?
? f pack (f ? R?) as ?env.((? ? env) ? ?) ?
env hiding R(?) f ?env.((? ? env) ? ?) ? env
24Summary
- Create a value to represent a type
- Pass the value, not the type
- Passing values is easy
- Knowing types at runtime is useful