type specialisation john hughes chalmers university
Post on 21-Dec-2015
223 views
TRANSCRIPT
Type Specialisation
John Hughes
Chalmers University
Type Specialisation(With thanks to Neil Jones)
John Hughes
Chalmers University
1985: A Weird Program Called MIX!
The Mix Equation
(mix prog x) y = prog x y
The Futamura Projections
Specialised program
mix interp prog = codebecause
code data = interp prog data
1985: A Weird Program Called MIX!
The Mix Equation
(mix prog x) y = prog x y
The Futamura Projections
Specialised program
mix mix interp = compilerbecause
compiler prog = mix interp prog = code
1985: A Weird Program Called MIX!
The Mix Equation
(mix prog x) y = prog x y
The Futamura Projections
Specialised program
mix mix mix = compiler-generatorbecause
compiler-generator interp = mix mix interp = compiler
Why Did He Use Scheme?
Why not:
•A language with lazy evaluation?
•A language with types?
1987: Partial Evaluation and Mixed Computation
1987: Partial Evaluation and Mixed Computation
A ”Hughes”
•a measure of the irrelevance of a talk to the workshop topic.
1987: Partial Evaluation and Mixed Computation
Challenging Problems
•eliminating tags from typed interpreters
data V = N Int | F (V -> V)
eval (Const n) = N neval (Lam x e) = F (v. eval (bind x v ) e)…
Lam ”x” (Const 3)
F (v. N 3)
mix
1987: Partial Evaluation and Mixed Computation
Challenging Problems
•eliminating tags from typed interpreters
•type specialisation
data V = N Int | F (V -> V)…
Int
Int->Int
(Int->Int)->Int
1987: Partial Evaluation and Mixed Computation
Challenging Problems
•eliminating tags from typed interpreters
•type specialisation
•optimal specialisation of typed interpreters
mix self-interp prog prog x. 3
F (x. N 3)
1995: Neil’s Seminar @ Chalmers
•How can we recognise an interpreter for a strongly typed language?
•How can we derive a type-checker from an interpreter?
•An approach for a first-order language.
1995: Neil’s Seminar @ Chalmers
•How can we recognise an interpreter for a strongly typed language?
•How can we derive a type-checker from an interpreter?
•An approach for a first-order language.
Surely one can dobetter?
1995: Neil’s Seminar @ Chalmers
•How can we recognise an interpreter for a strongly typed language?
•How can we derive a type-checker from an interpreter?
•An approach for a first-order language.
Invitation to Dagstuhl Seminar on Partial Evaluation!
Key Idea: Offline Partial Evaluation
Annotations in the source code classify operations as
•static -- performed by mix
•dynamic -- built into residual program
eval (Const n) = N (lift n)eval (Lam x e) = F (v. eval (bind x v ) e)… bind x v = y. if x=y then v else y
Underlinedoperations are
dynamic
Key Idea: Offline Partial Evaluation
Annotations in the source code classify operations as
•static -- performed by mix
•dynamic -- built into residual program
eval (Const n) = N (lift n)eval (Lam x e) = F (v. eval (bind x v ) e)… bind x v = y. if x=y then v else y
Lam ”x” (Const 3)
F (v. N 3)
mix
Key Idea: Offline Partial Evaluation
Annotations in the source code classify operations as
•static -- performed by mix
•dynamic -- built into residual program
eval (Const n) = N (lift n)eval (Lam x e) = F (v. eval (bind x v ) e)… bind x v = y. if x=y then v else y
Don’t underlinethe tags!
But we have to!
Interesting Properties
N (z+lift 1) N Int
x. case x of N n -> N (n+lift 1) F f -> …
N Int -> N Int
f @ N (z+lift 1) N Int
Keep Track of Tags by Type Inference!
e e´ : ´ ´ ::= Int | ´-> ´ | C ´
Residual types
|- e e´ : ´
|- C e e´ : C ´
|- e e´ : Ck 1´
, xk e´ : 1´ |- ek ek´ : 2´
|- case e of {Cj xj -> ej} ek´ : 2´
Reformulate Partial Evaluation in Terms of Type Inference
e e´ : ´ ´ ::= Int | ´-> ´ | C ´ | n | …
Residual types
x. x+1 x. : 2 -> 3
x. x+z <z> : close <z : Int> in x. x+z
Real Increase in Power
Type specialisation=
Type-based analysis + type-directed transformation
Examples
•Monomorphisation
•Typed defunctionalisation
•Constructor specialisation for -calculus
By type-specialising a suitable interpreter.
Continuing Work
•Simple type specialiser as a back-end for PE to achieve optimality (PADO II).
•Principal type specialisations.
•Strategies for controlling polyvariance (type-based cloning)
•…
Continuing Work
•Simple type specialiser as a back-end for PE to achieve optimality (PADO II).
•Principal type specialisations & polymorphism.
•Strategies for controlling polyvariance (type-based cloning)
•…
Thank you Neil!