Staging Algebraic Datatypes - PowerPoint PPT Presentation

About This Presentation
Title:

Staging Algebraic Datatypes

Description:

match3 (IntP x) (term as (IntS (Stat y))) env k ... match3 (IntP x) (term as (IntS (Dyn (y, isNot)))) env k = if elem x isNot. then k (Fail term) ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 15
Provided by: x7366
Learn more at: http://web.cecs.pdx.edu
Category:

less

Transcript and Presenter's Notes

Title: Staging Algebraic Datatypes


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
  • Dont 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 ExamplePattern Matching
  • Three versions in the paper
  • Unstaged
  • Staged
  • Staged w/ staged datatypes
  • The unstaged version is straightforward lets
    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 ? ltTermgt ? ltTermgt Subst
  • ? ((ltTermgt Subst) option
    ? ltagt) ? ltagt )

6
Pattern Matching (II)
  • fun match2 (VarP x) t env k
  • (case lookup x env of
  • NONE gt k (SOME ((x,t)env))
  • SOME t gt ltif termEq t t then
    (k (SOME env))

  • else (k NONE)gt)
  • match2 (IntP x) term env k
  • ltcase term of
  • Int i gt if i (lift x) then (k
    (SOME env))

  • else (k NONE)
  • Con (con,x,y) gt (k NONE)gt

7
Pattern Matching (II)
  • match2 (ConP (conp,xp,yp)) term env k
  • ltcase term of
  • Int i gt (k NONE)
  • Con (con,x,y) gt if con (lift
    conp)
  • then (match2 xp ltxgt env
  • (fn NONE
    gt k NONE
  • SOME
    env gt match2 yp ltygt env k))
  • else (k NONE)gt

8
Pattern Matching (II)
  • match2 (AndP (p1, p2)) term env k
  • match2 p1 term env (fn NONE gt k NONE

  • SOME e2 gt match2 p2 term e2 k)
  • match2 (OrP (p1, p2)) term env k
  • match2 p1 term env (fn NONE gt match2 p2
    term env k

  • SOME e2 gt 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 lt(fn a gt (case a of
  • Int i gt (case a of ...)
  • Con(d,c,b) gt if d -
  • then
    SOME ((y,b),(x,c))
  • else
    (case a of ...)))gt
  • ltTerm ? ((string Term) list) optiongt

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 ltagt
    a list
  • ( Stat tells what it is Dyn tells what its
    not! )
  • datatype TermS IntS of int Partial
  • ConS of (string
    Partial) TermS TermS
  • TermS of ltTermgt
  • datatype Result Fail of TermS Ok of (TermS
    Subst) TermS
  • ( both constructors include partial information
    about a term )
  • (match3 Pat ? TermS ? TermS Subst ?
    (Result?ltagt) ? ltagt)

12
Pattern Matching (III)
  • ( match2 (IntP x) term env k
  • ltcase term of
  • Int i gt if i (lift x) then (k
    (SOME env))

  • else (k NONE)
  • Con (con,x,y) gt (k NONE)gt )
  • match3 (IntP x) (TermS term) env k
  • ltcase term of
  • Int i gt if i (lift x)
  • then (k (Ok
    (env, IntS (Stat x))))
  • else (k (Fail
    (IntS (Dyn (ltigt, 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 ltif y (lift x)
  • then (k (Ok
    (env, IntS (Stat x))))
  • else (k (Fail
    (IntS (Dyn (y, (xisNot))))))gt

14
Pattern Matching (III)
  • - test3 p3
  • val it
  • lt(fn Int d gt NONE
  • Con(c,b,a) gt
  • if c -
  • then SOME ((y,a),(x,b))
  • else if c then SOME
    ((y,a),(x,b)) else NONE)gt
  • ltTerm ? (string Term) list optiongt
  • No more redundant checks!
Write a Comment
User Comments (0)
About PowerShow.com