Tim Sheard - PowerPoint PPT Presentation

About This Presentation
Title:

Tim Sheard

Description:

Title: No Slide Title Author: Mark P Jones Last modified by: sheard Created Date: 3/19/1999 7:24:51 AM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 36
Provided by: MarkP207
Learn more at: http://web.cecs.pdx.edu
Category:
Tags: adding | integers | sheard | tim

less

Transcript and Presenter's Notes

Title: Tim Sheard


1
Fundamentals of
Staged Computation
Lecture 8 Operational Semantics of MetaML
  • Tim Sheard
  • Oregon Graduate Institute

CSE 510 Section FSC Winter 2005
2
Assignments
  • Reading Assignment
  • A Methodology for Generating Verified
    Combinatorial Circuits.
  • By Kiseyov, Swadi, and Taha.
  • Available on the readings page of the course
    web-site
  • Homework 5
  • assigned Tuesday Feb. 1, 2005
  • due in class Tuesday Feb. 8, 2005
  • Details on the assignment page of the course
    web-site.

3
Map of todays lecture
  • Thanks to Walid Taha. Todays lecture comes from
    chapter 4 of his thesis
  • Simple interpreter for lambda calculus
  • Addition of constants for
  • Operators
  • If-then-else over integer
  • Y combinator
  • Add staging annotations
  • Fix up environment mess

4
Terms and Values
  • datatype exp
  • EI of int ( integers )
  • EA of exp exp ( applications )
  • EL of string exp ( lambda-abs )
  • EV of string ( variables )
  • datatype value
  • VI of int
  • VF of (value -gt value)

5
Example terms
fn x gt x
  • val ID EL("x",EV "x")
  • val COMPOSE
  • EL("f",
  • EL("g",
  • EL("x",EA(EV "f",
  • EA(EV "g",EV "x")))))

fn f gt fn g gt fn x gt f(g x)
6
Example Values
  • val id VF(fn x gt x)
  • val compose
  • VF(fn (VF f) gt
  • VF (fn (VF g) gt
  • VF(fn x gt f(g x))))
  • val square VF (fn (VI x) gt VI(x x))
  • val bang
  • let fun fact n if n0 then 1 else n(fact
    (n-1))
  • in VF (fn (VI x) gt VI(fact x)) end

Note how primitive functions can be implemented
7
An interpreter
  • The interpreter
  • ev0 (string -gt value) -gt exp -gt value
  • Environments
  • type env string -gt value
  • exception NotBound of string
  • val env0
  • fn x gt (print x raise (NotBound x))
  • fun ext env x v
  • (fn y gt if xy then v else env y)

8
Semantics in 8 lines
  • fun ev0 env e
  • case e of
  • EI i gt VI i
  • EA(e1,e2) gt (case (ev0 env e1,ev0 env e2) of
  • (VF f,v) gt f v)
  • EL(x,e1) gt VF(fn v gt ev0 (ext env x v) e1)
  • EV x gt env x
  • - val ex1 ev0 env0 (EA(ID,EI 5))
  • val ex1 VI 5 value

9
Printing Terms
  • fun showe e
  • case e of
  • EI n gt show n
  • EA(f,x as EA(_,_)) gt
  • (showe f)" ("(showe x)")"
  • EA(f,x) gt (showe f)" "(showe x)
  • EL(x,e) gt "(fn "x" gt "(showe e)")"
  • EV x gt x

10
Making it useful
  • The interpreter is good for the lambda calculus
    with integer constants, but we have no operations
    on integers
  • Need to add primitive operations
  • Arithmetic
  • Case analysis over integers
  • Recursion over integers
  • This is accomplished by adding an initial
    environment with values for some constants

11
Arithmetic
  • val plus
  • VF(fn (VI x) gt VF(fn (VI y) gt VI(xy)))
  • val minus
  • VF(fn (VI x) gt VF(fn (VI y) gt VI(x-y)))
  • val times
  • VF(fn (VI x) gt VF(fn (VI y) gt VI(xy)))
  • val env1 ext env0 "" times
  • val SQUARE EL("x",EA(EA(EV "",EV "x"),EV
    "x"))
  • - val ex2 ev0 env1 (EA (SQUARE,EI 5))
  • val ex2 VI 25 value

12
If-zero function
  • fun Z n tf ff if n0 then tf 0 else ff n
  • val Z int -gt (int -gt 'a ) -gt (int -gt 'a ) -gt
    'a
  • val ifzero
  • VF(fn (VI n) gt
  • VF(fn (VF tf) gt
  • VF(fn (VF ff) gt if n0 then tf(VI 0) else
    ff(VI n))))

13
Recursion
  • fun Y f f (fn v gt (Y f) v)
  • Y (('b -gt 'a ) -gt 'b -gt 'a ) -gt 'b -gt 'a
  • val recur
  • let fun recur' (VF f)
  • f(VF (fn vgt
  • (case (recur' (VF f)) of
  • VF fp gt fp v
  • w gt error not function )))
  • in VF recur' end

14
New initial environment
  • val env1
  • ext(ext(ext(ext(ext env0 "" plus)
  • "-" minus)
  • "" times)
  • "Z" ifzero)
  • "Y" recur

15
Using the constants
  • Start with a recursive function in ML
  • Transform it removing ML features
  • Example function
  • fun h1 n z
  • if n0
  • then z
  • else (fn x gt (h1 (n-1) (xz))) n

16
Steps
  • Remove recursion by using Y
  • val h1
  • Y(fn h1' gt fn n gt fn z gt
  • if n0 then z
  • else (fn x gt (h1' (n-1) (xz)))
    n)
  • Remove if
  • val h1
  • Y(fn h1' gt fn n gt fn z gt
  • Z n (fn _ gt z)
  • (fn n gt (fn x gt (h1' (n-1) (xz)))
    n))

17
Make a term
  • fun minus x y EA(EA(EV "-",x),y)
  • fun plus x y EA(EA(EV "",x),y)
  • val H1
  • EA(EV "Y",EL("h1",EL("n",EL("z",
  • EA(EA(EA(EV "Z",EV "n")
  • ,EL("w",EV "z"))
  • ,EL("n",EA(EL("x",EA(EA(EV "h1",
  • minus (EV "n") (EI
    1))
  • ,plus (EV "x") (EV "z")))
  • ,EV "n")))))))

18
What does it look like?
  • - print(showe H1)
  • Y (fn h1 gt
  • (fn n gt
  • (fn z gt
  • Z n (fn w gt z)
  • (fn n gt
  • (fn x gt h1 (- n 1)( x z))
  • n))))

19
Running it
  • - val IT EA(EA(H1,EI 3),EI 4)
  • - val answer ev0 env1 IT
  • val answer VI 10 value

20
Adding Staging
  • What are the new kinds of values
  • Code
  • Represented as embedding exp in value
  • What are the new kinds of terms
  • Brackets
  • Escapes
  • Run
  • Cross stage persistent constants

21
Terms and values
  • datatype value
  • VI of int
  • VF of (value -gt value)
  • VC of exp
  • and exp
  • EI of int ( integers )
  • EA of exp exp ( applications )
  • EL of string exp ( lambda-abstractions )
  • EV of string ( variables )
  • EB of exp ( brackets )
  • ES of exp ( escape )
  • ER of exp ( run )
  • EC of string value ( cross-stage
    constants )

22
Generalizing environments
  • type env string -gt exp
  • fun env0 x EV x
  • val env1
  • ext(ext(ext(ext(ext env0 "" (EC("",plus)))
  • "-" (EC("-",minus)))
  • "" (EC("",times)))
  • "Z" (EC("if",ifzero)))
  • "Y" (EC("Y",recur))
  • Note the initial values in the environment are
    cross-stage persistent Expressions. Other values
    will also be injected using EC

23
Printing terms
  • fun showe e
  • case e of
  • EI n gt show n
  • EA(f,x as EA(_,_)) gt
  • (showe f)" ("(showe x)")"
  • EA(f,x) gt (showe f)" "(showe x)
  • EL(x,e) gt "(fn "x" gt "(showe e)")"
  • EV x gt x
  • EB e gt "lt"(showe e)"gt"
  • ES(EV x) gt ""x
  • ES e gt "("(showe e)")"
  • ER e gt "run("(showe e)")"
  • EC(s,v) gt ""s

24
Fresh Variables
  • val ctr ref 0
  • fun NextVar s
  • let val _ ctr (!ctr) 1
  • in s(toString (! ctr)) end
  • - NextVar "x"
  • val it "x1" string
  • - NextVar "x"
  • val it "x2" string

25
The interpreter
  • fun ev1 env e case e of
  • EI i gt VI i
  • EA(e1,e2) gt
  • (case (ev1 env e1,ev1 env e2) of
  • (VF f,v) gt f v)
  • EL(x,e1) gt
  • VF(fn v gt ev1 (ext env x (EC(x,v))) e1)
  • EV x gt (case env x of EC(_,v) gt v w gt VC
    w)
  • EB e1 gt VC(eb1 1 env e1)
  • ER e1 gt
  • (case ev1 env e1 of VC e2 gt ev1 env0 e2)
  • EC(_,v) gt v
  • ES e1 gt error "escape at level 0"

26
Rebuilding terms
  • and eb1 n env e case e of
  • EI i gt EI i
  • EA(e1,e2) gt EA(eb1 n env e1,eb1 n env e2)
  • EL(x,e1) gt
  • let val x' NextVar x
  • in EL(x',eb1 n (ext env x (EV x')) e1) end
  • EV y gt env y
  • EB e1 gt EB(eb1 (n1) env e1)
  • ES e1 gt if n1
  • then (case ev1 env e1 of VC e gt e)
  • else ES(eb1 (n-1) env e1)
  • ER e1 gt ER(eb1 n env e1)
  • EC(s,v) gt EC(s,v)

27
Staging H1
  • val H2 let fun minus x y EA(EA(EV "-",x),y)
  • fun plus x y EA(EA(EV "",x),y)
  • in EA(EV "Y",EL("h1",EL("n",EL("z",
  • EA(EA(EA(EV "Z",EV "n")
  • ,EL("w",EV "z"))
  • ,EL("n",EB(EA(EL("x",ES(EA(EA(EV "h1",
  • minus (EV
    "n")
  • (EI
    1))
  • ,EB(plus (EV
    "x")
  • (ES
    (EV "z"))))))
  • ,EV "n"))))))))
  • end
  • - print(showe H2)
  • Y (fn h1 gt (fn n gt (fn z gt
  • Z n (fn w gt z)
  • (fn n gt lt(fn x gt (h1 (- n 1) lt x
    zgt)) ngt))))

28
Generating code
  • val VC answer ev1 env1 IT
  • - val answer EA (EL ("x7",
  • EA (EL ("x8",
  • EA (EL ("x9",
  • EA (EA (EC ("",VF fn
    ),EV "x9"),
  • EA (EA (EC ("",VF fn
    ),EV "x8"),
  • EA (EA (EC
    ("",VF fn ),EV "x7"),EI 4)))),
  • EC ("n",VI 1))),
  • EC ("n",VI 2))),
  • EC ("n",VI 3))
  • - showe answer
  • val it
  • "(fn x7 gt (fn x8 gt (fn x9 gt
  • x9 ( x8 ( x7 4))) n) n) n"

29
A problem
  • - val puzzle
  • run ((run ltfn a gt
  • ( (fn x gt ltxgt)
  • (fn w gt ltagt) )
  • 5gt)
  • 3)
  • - val puzzle 3 int

30
In our language
  • ( (fn x gt ltxgt) )
  • val t1 EL("x",EB(EV "x"))
  • ( (fn w gt ltagt) )
  • val t2 EL("w",EB(EV "a"))
  • ( ltfn a gt ( (fn x gt ltxgt) (fn w gt ltagt) ) 5gt
    )
  • val t3 EB(EL("a",EA(ES(EA(t1,t2)),EI 5)))
  • val puzzle ER(EA(ER t3,EI 3))
  • - showe puzzle
  • val it
  • "run(run(lt(fn a gt ((fn x gt ltxgt) (fn w gt ltagt))
    5)gt) 3)

31
Running it
  • run(run(lt(fn a gt ((fn x gt ltxgt)
  • (fn w gt ltagt))
  • 5)gt)
  • 3)
  • - ev1 env1 puzzle
  • val it VC EV "a14" value
  • Whats gone wrong?

32
Covers
  • fun coverE env e
  • case e of
  • EI i gt EI i
  • EA(e1,e2) gt EA(coverE env e1,coverE env e2)
  • EL(x,e1) gt EL(x,coverE env e1)
  • EV y gt env y
  • EB e1 gt EB(coverE env e1)
  • ES e1 gt ES(coverE env e1)
  • ER e1 gt ER(coverE env e1)
  • EC(s,v) gt EC(s,coverV env v)
  • and coverV env v
  • case v of
  • VI i gt VI i
  • VF f gt VF((coverV env) o f)
  • VC e gt VC(coverE env e)

33
Final interpreter
  • fun ev1 env e
  • case e of
  • EI i gt VI i
  • EA(e1,e2) gt
  • (case (ev1 env e1,ev1 env e2) of (VF f,v) gt f
    v)
  • EL(x,e1) gt
  • VF(fn v gt ev1 (ext env x (EC(x,v))) e1)
  • EV x gt (case env x of EC(_,v) gt v w gt VC
    w)
  • EB e1 gt VC(eb1 1 env e1)
  • ER e1 gt (case ev1 env e1 of VC e2 gt ev1 env0
    e2)
  • EC(s,v) gt coverV env v

34
Final rebuilder
  • and eb1 n env e case e of
  • EI i gt EI i
  • EA(e1,e2) gt EA(eb1 n env e1,eb1 n env e2)
  • EL(x,e1) gt
  • let val x' NextVar x
  • in EL(x',eb1 n (ext env x (EV x')) e1) end
  • EV y gt env y
  • EB e1 gt EB(eb1 (n1) env e1)
  • ES e1 gt if n1
  • then (case ev1 env e1 of VC e gt e)
  • else ES(eb1 (n-1) env e1)
  • ER e1 gt ER(eb1 n env e1)
  • EC(s,v) gt EC(s,coverV env v)

35
Now it works
  • - ev1 env1 puzzle
  • val it VI 3 value
Write a Comment
User Comments (0)
About PowerShow.com