Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tag-Elimination and Jones Optimality Walid Taha (Yale) Henning Makholm (DIKU) John Hughes (Chalmers)

Similar presentations


Presentation on theme: "Tag-Elimination and Jones Optimality Walid Taha (Yale) Henning Makholm (DIKU) John Hughes (Chalmers)"— Presentation transcript:

1 Tag-Elimination and Jones Optimality Walid Taha (Yale) Henning Makholm (DIKU) John Hughes (Chalmers)

2 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

3 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

4 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

5 The Tagging Problem data Val = N Int | F (Val -> 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

6 This Paper: Recovering Optimality Optimal Specialisation tagElim (mix self_interp prog) (typeOf prog) = prog  Val -> Val Eliminates tags, converts to type of prog.

7 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!

8 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.

9 The Language  ::= D | V |  ->  S-expression data Values in interpreted programs data 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 case expression!

10 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  V Run-time type check e and E e evaluate to the same representation! OK, since they have different types.

11 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

12 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 || _ ||

13 The Analysis How do we know ||e|| is well typed? || _ || changes types! E.g. || unFe (Ee `x) (Ee `y) ||`x `y

14 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)

15 Annotated Types c ::= V | E D | F (c -> c)can be produced by ||_|| a term of type V a ::= D | c | a -> acan 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´||

16 Roadmap self_interp prog Well-typed, but not well-annotated. + mix self_interp prog Well-typed, but perhaps not well-annotated. Analysis succeeds? tagElim (mix …) Well-typed, = prog. Some sensible default.

17 Wrapping and Unwrapping W x = x W x = E x W f = F (W f) W a´> f = x. W (f (U x)) W :: ||a|| -> |a| U x = x U x = unE x U f = U (unF f) U a´> f = x. U (f (W x)) U :: |a| -> ||a||

18 Correctness of Tag Elimination Now we can define the tag elimination transformation:  ||e||if e : a tagElim e a =   U |e|otherwise Correctness  |e| : |a|. tagElim e a  U |e|

19 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 : 

20 Typed Partial Evaluation and Optimality The Typed Mix Equation e e´ : |a|  tmix e e´ a  U (e e’) Optimal Typed Specialisation e :   tmix self_interp e (  )  e Satisfied by: tmix e e´ a = tagElim (mix e e´) a

21 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?


Download ppt "Tag-Elimination and Jones Optimality Walid Taha (Yale) Henning Makholm (DIKU) John Hughes (Chalmers)"

Similar presentations


Ads by Google