Title: TagElimination and Jones Optimality
1Tag-Elimination and Jones Optimality
- Walid Taha (Yale)
- Henning Makholm (DIKU)
- John Hughes (Chalmers)
2Compiling by Specialising Interpreters
The Mix Equation (mix prog data) data prog
data data The First Futamura Projection (mix
interp prog) data interp prog data Optimal
Specialisation mix self_interp prog prog
3Compiling by Specialising Interpreters
Typed
The Mix Equation (mix prog data) data prog
data data The First Futamura Projection (mix
interp prog) data interp prog data Optimal
Specialisation mix self_interp prog prog
interp Prog -gt Val -gt Val
??????
Val -gt Val
4Compiling by Specialising Interpreters
Typed
The Mix Equation (mix prog data) data prog
data data The First Futamura Projection (mix
interp prog) data interp prog data Optimal
Specialisation mix self_interp prog prog
interp Prog -gt Val -gt Val
??????
Val -gt Val
5The Tagging Problem
data Val N Int F (Val -gt Val) eval (Const n)
env N n eval (Lam x e) env F (?v. eval
e (bind x v env)) eval (App e e) env unF
(eval e env) (eval e env)
?f.?x.f (f x)
mix
F (?f. F (?x. unF f (unF f x)))
Unwanted tags -- cost 2x
6This Paper Recovering Optimality
Optimal Specialisation tagElim (mix self_interp
prog) (typeOf prog) prog
????????
Val -gt Val
Eliminates tags, converts to type of prog.
7Havent we heard this before?
- Type specialisation (Hughes)
- specialised an interpreter for ?-calculus to
terms without tags -- but not a self-interpreter! - large step away from partial evaluation.
- Tag elimination (Taha)
- eliminated tags (provably), but for a tiny
language -- no self-interpreter! - Optimal typed specialisation (Makholm)
- first-order language, complex -- many stages
- experimentally optimal, but no proof!
8This Paper
- Higher-order language ?-calculus with data
types. - A complete self-interpreter.
- Standard partial evaluation, followed by a simple
tag-elimination post-processor. - Provably optimal specialisation.
- Implementation matching the theory.
9The Language
? D V ? -gt ?
S-expression data
Values in interpreted programs data V E D F
(V -gt V)
s x (s.s) u car cdr atom? o
cons equal? e x e e ??x.e fix x.e
s u e o e e if e e e E e unE e
F e unF e
No case expression!
10The Self-Interpreter
fix eval. ??env. ?e. (if (equal? (car e) quote)
(E (car (cdr e))) (if (equal? (car e) E) (E
(unE (eval env (car (cdr e)))))
??????? V
?????????? D
Run-time type check
????????????? V
e and E e evaluate to the same representation! OK,
since they have different types.
11Tag-Elimination Annotations
- Each tag/untag operation is annotated
- k (keep), or
- e (eliminate)
Erasure
Tag Elimination
Ee e E e Ek e E e unEe e unE
e unEk e unE e
Ee e e Ek e E e unEe e
e unEk e unE e
12Annotating the Self-Interpreter
fix eval. ??env. ?e. (if (equal? (car e) quote)
(Ee (car (cdr e))) (if (equal? (car e) E) (Ek
(unEe (eval env (car (cdr e)))))
mix
eval env (E (quote x))
Ek (unEe (Ee x))
_
E x
13The Analysis
How do we know e is well typed? _
changes types! E.g. unFe (Ee x) (Ee y)
x y
14The Analysis
Richer Annotated type system
How do we know e is well typed? _
changes types! E.g. unFe (Ee x) (Ee y)
x y
??? E D
F a -gt a
unFk (Ek x) (Ek y) unF (E x) (E y)
??? V
V -gt (V -gt V)
15Annotated Types
c V E D F (c -gt c) can be produced by
_ a term of type V a D c a -gt
a can be produced by _
c V D D a -gt a a -gt a
V V E D D F (c -gt c) c -gt
c D D a -gt a a -gt a
16Roadmap
self_interp
Well-typed, but not well-annotated.
prog
mix self_interp prog
Well-typed, but perhaps not well-annotated.
Analysis succeeds?
Some sensible default.
tagElim (mix )
Well-typed, prog.
17Wrapping and Unwrapping
Wltagt a -gt a
Ultagt a -gt a
WltDgt x x WltVgt x x WltE Dgt x E x WltF agt f F
(Wltagt f) Wlta-gtagt f
?x. Wltagt (f (Ultagt x))
UltDgt x x UltVgt x x UltE Dgt x unE x UltF agt f
Ultagt (unF f) Ulta-gtagt f
?x. Ultagt (f (Wltagt x))
18Correctness of Tag Elimination
Now we can define the tag elimination
transformation ? e if e a tagElim e
a ? ? Ultagt e otherwise
Correctness
? e a. tagElim e a ? Ultagt e
19Mix Self_interp A Closer Look
mix self_interp performs a simple syntax-directed
translation ?(?x. e) Fe (?x. ?e) ?(E e) Ek
(unEe (?e)) ?(e e) unFe (?e) (?e) ?(unE e)
Ee (unEk (?e)) Extends to a translation of
types ?D E D ?V V ?(?-gt?) F (??-gt??)
e ? ? ?e ??
20Typed Partial Evaluation and Optimality
The Typed Mix Equation e e a ? tmix e e a
? Ultagt (e e) Optimal Typed Specialisation e
? ? tmix self_interp e (??) ? e
Satisfied by tmix e e a tagElim (mix e e) a
21Conclusions
- The mix equation and Jones-optimality
reformulated to make them consistent in a typed
setting. - Tag elimination is a simple post-processor which
achieves optimality in theory and in practice,
for a fairly rich language, building on existing
PE technology. - Future work
- Polymorphism?
- User-defined datatypes?
- Dynamic tag tests?