![Page 1: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/1.jpg)
Type Specialisation
John Hughes
Chalmers University
![Page 2: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/2.jpg)
Type Specialisation(With thanks to Neil Jones)
John Hughes
Chalmers University
![Page 3: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/3.jpg)
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
![Page 4: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/4.jpg)
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
![Page 5: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/5.jpg)
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
![Page 6: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/6.jpg)
Why Did He Use Scheme?
Why not:
•A language with lazy evaluation?
•A language with types?
![Page 7: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/7.jpg)
1987: Partial Evaluation and Mixed Computation
![Page 8: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/8.jpg)
1987: Partial Evaluation and Mixed Computation
A ”Hughes”
•a measure of the irrelevance of a talk to the workshop topic.
![Page 9: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/9.jpg)
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
![Page 10: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/10.jpg)
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
![Page 11: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/11.jpg)
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)
![Page 12: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/12.jpg)
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.
![Page 13: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/13.jpg)
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?
![Page 14: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/14.jpg)
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!
![Page 15: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/15.jpg)
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
![Page 16: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/16.jpg)
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
![Page 17: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/17.jpg)
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!
![Page 18: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/18.jpg)
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
![Page 19: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/19.jpg)
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´
![Page 20: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/20.jpg)
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
![Page 21: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/21.jpg)
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.
![Page 22: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/22.jpg)
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)
•…
![Page 23: Type Specialisation John Hughes Chalmers University](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d625503460f94a4469c/html5/thumbnails/23.jpg)
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!