Download presentation
Presentation is loading. Please wait.
1
Intensional Polymorphism in Type-Erasure Semantics Karl Crary, Stephanie Weirich, Greg Morrisett Presentation by Nate Waisbrot
2
Authors' background Part of Cornell University's TAL (Typed Assembly Language) group Creating intermediate languages to compile into TAL
3
Three 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
4
Passing 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
5
Dynamic types Dynamic(5 int)dynamic value f : dynamic dynamicdynamic function x:dynamic. typecase x of int … … …
6
Passing types Values are described by types Types are described by kinds We could keep going… (System F )
7
Overview of System F . x: .xThe polymorphic identity function . The type of the identity function iden [int] 5Application of the identity function . . x: . y: . (x y)a pair function . . ( )its type pair [int] [string] 0 “zero”application
8
Overview of i ML Start with System F, add the ability to work with types alone . . the pair-type function Type Type Typethe 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
i ML syntax ::= Type | c ::= | int | c c | c c | : .c | c c | Typerec c(c int, c , c ) ::= c | | | : . e ::= x | i | x: .e | fix f: .v | e e | | e | : .v | e[c] | typecase [ . ] … v::= i | x: .e | fix f: .v | | : .v
10
Typing hierarchy 56 x:int.x int int int Type types: values: . Type Type kinds:
11
What'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
12
Solution: type representations Make new type, "representation" Get back all the simplicity of normal value- passing As a bonus, gain some abstraction
13
Overview of type representations R (R int,R int )The representation of int int R( R(int) R(int) )Its type
14
Syntax of R ::= Type | c ::= | int | c c | c c | : .c | c c | R(c) | Typerec c(c int, c , c , c R ) ::= c | | | : . | : . e ::= x | i | x: .e | fix f: .v | e e | | e | : .v | e[c] | pack e as . hiding | unpack = e in e | R int | R (e,e) | R (e,e) | R R (e) | typecase [ .c] … v::= i | x: .e | fix f: .v | | : .v | pack v as . hiding | R int | R (v,v) | R (v,v) | R R (v)
15
TypeToString: dynamic types typeToString : dynamic string x:dynamic. typecase x of int "int" string "string" "function" × "<" + typeToString Dynamic( 1 x) + "," + typeToString Dynamic( 2 x) + ">"
16
TypeToString: type-passing fix typeToString : :Type.string. :Type. typecase [ .string] of int "int" string "string" typeToString [ ] + " " + typeToString [ ] × "<" + typeToString [ ] + "," + typeToString [ ] + ">"
17
TypeToString: type representations fix typeToString : :Type.R( ) string. :Type. x:R( ). typecase [ .string] x of R int "int" R string "string" R (x,y) as typeToString [ ] x + " " + typeToString [ ] y R × (x,y) as × "<" + typeToString [ ] x + "," + typeToString [ ] y + ">"
18
Type erasure A well-typed program in typed -calculus has an equivalent in untyped -calculus Typed: x: .x Untyped: x.x
19
TypeToString: with types erased fix typeToString : :Type.R( ) string. :Type. x:R( ). typecase [ .string] x of R int "int" R string "string" R (x,y) as typeToString [ ] x + " " + typeToString [ ] y R × (x,y) as × "<" + typeToString [ ] x + "," + typeToString [ ] y + ">"
20
Type refinement Do dead-code elimination based on the type of the representation Propagate information about the type of an argument back through the function
21
Monomorphic closure x: .yA function with a free variable Its type (if y has type ) We want to eliminate free variables f = ( (x e): env. y e) The closed function env.(( env) ) envIts type
22
Polymorphic closure The same closed function, with type passing :Kind. env:Type. : . : = . (( env) ) env
23
Closure with representations f’ = x:( R( )). 2 x f’ : ( R( )) f’’ = pack (f’’ R ) as env.(( env) ) env hiding R( ) f’’ : env.(( env) ) env
24
Summary Create a value to represent a type Pass the value, not the type Passing values is easy Knowing types at runtime is useful
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.