Tag-Elimination and Jones Optimality Walid Taha (Yale) Henning Makholm (DIKU) John Hughes (Chalmers)
Dec 22, 2015
Tag-Elimination and Jones Optimality
Walid Taha (Yale)
Henning Makholm (DIKU)
John Hughes (Chalmers)
Compiling 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
Compiling 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
Typed
interp : Prog -> Val -> Val
Val -> Val
Compiling 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
Typed
interp : Prog -> Val -> Val
Val -> Val
The Tagging Problem
data Val = N Int | F (Val -> Val)
eval (Const n) env = N neval (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
This Paper: Recovering Optimality
Optimal Specialisation
tagElim (mix self_interp prog) (typeOf prog) = prog
Val -> Val
Eliminates tags,converts to type of
prog.
Haven’t 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!
This 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.
The Language
::= D | V | ->
S-expressiondata
Values in interpreted programsdata V = E D | F (V -> 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 caseexpression!
The 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
VRun-time type check
e and E e evaluate to the same representation!OK, since they have different types.
Tag-Elimination Annotations
Each tag/untag operation is annotated
•k (keep), or
•e (eliminate)
|Ee e| = E |e||Ek e| = E |e|
|unEe e| = unE |e||unEk e| = unE |e|
…
Erasure
||Ee e|| = ||e||||Ek e|| = E ||e||||unEe e|| = ||e||
||unEk e|| = unE ||e||…
Tag Elimination
Annotating 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)))))
…
eval env `(E (quote x)) Ek (unEe (Ee `x))mix
E `x
|| _ ||
The Analysis
How do we know ||e|| is well typed?
|| _ || changes types!
E.g. || unFe (Ee `x) (Ee `y) || `x `y
The Analysis
How do we know ||e|| is well typed?
|| _ || changes types!
E.g. || unFe (Ee `x) (Ee `y) || `x `y
Richer“Annotated”type system
E D
F a -> a
|| unFk (Ek `x) (Ek `y) || unF (E `x) (E `y)V
V -> (V -> V)
Annotated Types
c ::= V | E D | F (c -> c) can be produced by ||_||a term of type V
a ::= D | c | a -> a can be produced by ||_||
|c| = V|D| = D
|a -> a´| = |a| -> |a´|
||V|| = V||E D|| = D
||F (c -> c´)|| = ||c|| -> ||c´||||D|| = D
||a -> a´|| = ||a|| -> ||a´||
Roadmap
self_interp
prog
Well-typed, but not well-annotated.
+
mix self_interp prog Well-typed, but perhaps not well-annotated.
Analysissucceeds?
tagElim (mix …) Well-typed, = prog.
Some sensible default.
Wrapping and Unwrapping
W<D> x = xW<V> x = x
W<E D> x = E xW<F a> f = F (W<a> f)
W<a->a´> f = x. W<a´> (f (U<a> x))
W<a> :: ||a|| -> |a|
U<D> x = xU<V> x = x
U<E D> x = unE xU<F a> f = U<a> (unF f)
U<a->a´> f = x. U<a´> (f (W<a> x))
U<a> :: |a| -> ||a||
Correctness of Tag Elimination
Now we can define the tag elimination transformation:
||e|| if e : atagElim e a =
U<a> |e| otherwise
Correctness
|e| : |a|. tagElim e a U<a> |e|
Mix 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
(->´) = F (->)
e : e :
Typed Partial Evaluation and Optimality
The Typed Mix Equation
e e´ : |a| tmix e e´ a U<a> (e e’)
Optimal Typed Specialisation
e : tmix self_interp e () e
Satisfied by: tmix e e´ a = tagElim (mix e e´) a
Conclusions
•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?