1 / 14

Staging Algebraic Datatypes

Staging Algebraic Datatypes. Tim Sheard and Iavor S. Diatchki. Metadata. Paper type: Synthesis Builds on previous work on partial evaluation (Mogensen 1988) Adapts technique to staged programming Background: Staging MetaML. The Big Idea. Don’t discard information!

kevyn
Download Presentation

Staging Algebraic Datatypes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Staging Algebraic Datatypes Tim Sheard and Iavor S. Diatchki

  2. Metadata • Paper type: Synthesis • Builds on previous work on partial evaluation (Mogensen 1988) • Adapts technique to staged programming • Background: • Staging • MetaML

  3. The Big Idea • Don’t discard information! • Use what is known so far to specialize code • Datatypes encode partial information about runtime values • What is known about the structure of the code? • What values are possible? • What values are impossible?

  4. Running Example:Pattern Matching • Three versions in the paper: • Unstaged • Staged • Staged w/ staged datatypes • The unstaged version is straightforward; let’s examine the other two.

  5. Pattern Matching (II) datatype Term = Int of int | Con of string * Term * Term datatype Pat = IntP of int | ConP of string * Pat * Pat | VarP of string | AndP of Pat * Pat | OrP of Pat * Pat type ’a Subst = (string * ’a) list (* match2 : Pat → <Term> → <Term> Subst → ((<Term> Subst) option → <a>) → <a> *)

  6. Pattern Matching (II) fun match2 (VarP x) t env k = (case lookup x env of NONE => k (SOME ((x,t)::env)) | SOME t’ => <if termEq ~t ~t’ then ~(k (SOME env)) else ~(k NONE)>) | match2 (IntP x) term env k = <case ~term of Int i => if i = ~(lift x) then ~(k (SOME env)) else ~(k NONE) | Con (con,x,y) => ~(k NONE)>

  7. Pattern Matching (II) | match2 (ConP (conp,xp,yp)) term env k = <case ~term of Int i => ~(k NONE) | Con (con,x,y) => if con = ~(lift conp) then ~(match2 xp <x> env (fn NONE => k NONE | SOME env’ => match2 yp <y> env’ k)) else ~(k NONE)>

  8. Pattern Matching (II) | match2 (AndP (p1, p2)) term env k = match2 p1 term env (fn NONE => k NONE | SOME e2 => match2 p2 term e2 k) | match2 (OrP (p1, p2)) term env k = match2 p1 term env (fn NONE => match2 p2 term env k | SOME e2 => k (SOME e2)) | match2 _ _ env k = k NONE

  9. Pattern Matching (II) val p3 = OrP (ConP (“-”, VarP “x”, VarP “y”), ConP (“+”, VarP “x”, VarP “y”)) -| test2 p3; val it = <(fn a => (case a of Int i => (case a of ...) | Con(d,c,b) => if d = “-” then SOME ([(“y”,b),(“x”,c)]) else (case a of ...)))> : <Term → ((string * Term) list) option>

  10. Room for Improvement • There are three different places in the generated code where a is cased over. • The outermost one should be sufficient! • Two of the case arms can never be reached! • This code throws away useful information.

  11. Pattern Matching (III) datatype ’a Partial = Stat of ’a | Dyn of <’a> * ’a list (* Stat tells what it is; Dyn tells what it’s not! *) datatype TermS = IntS of int Partial | ConS of (string Partial) * TermS * TermS | TermS of <Term> datatype Result = Fail of TermS | Ok of (TermS Subst) * TermS (* both constructors include partial information about a term *) (*match3: Pat → TermS → TermS Subst → (Result→<a>) → <a>*)

  12. Pattern Matching (III) (* match2 (IntP x) term env k = <case ~term of Int i => if i = ~(lift x) then ~(k (SOME env)) else ~(k NONE) | Con (con,x,y) => ~(k NONE)> *) match3 (IntP x) (TermS term) env k = <case ~term of Int i => if i = ~(lift x) then ~(k (Ok (env, IntS (Stat x)))) else ~(k (Fail (IntS (Dyn (<i>, [x])))))

  13. Pattern Matching (III) | match3 (IntP x) (term as (IntS (Stat y))) env k = if x = y then k (Ok (env, term)) else k (Fail term) | match3 (IntP x) (term as (IntS (Dyn (y, isNot)))) env k = if elem x isNot then k (Fail term) else <if ~y = ~(lift x) then ~(k (Ok (env, IntS (Stat x)))) else ~(k (Fail (IntS (Dyn (y, (x::isNot))))))>

  14. Pattern Matching (III) -| test3 p3; val it = <(fn Int d => NONE | Con(c,b,a) => if c = “-” then SOME ([(“y”,a),(“x”,b)]) else if c = “+” then SOME ([(“y”,a),(“x”,b)]) else NONE)> : <Term → (string * Term) list option> • No more redundant checks!

More Related