Tim%20Sheard - PowerPoint PPT Presentation

About This Presentation
Title:

Tim%20Sheard

Description:

Staged Computation. Lecture 6: Monadic Staging of the RE language ... Note calls to the monadic operators 'try', 'star', 'test' (embedded in call to ' ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 17
Provided by: markp77
Learn more at: http://web.cecs.pdx.edu
Category:
Tags: 20sheard | monadic | tim

less

Transcript and Presenter's Notes

Title: Tim%20Sheard


1
Fundamentals of
Staged Computation
Lecture 6 Monadic Staging of the RE language
  • Tim Sheard
  • Oregon Graduate Institute

CSE 510 Section FSC Winter 2004
2
Assignments
  • Assignment 4 is now posted on webpage
  • Due in 1 week on Feb 1, 2005
  • Reading Assignment now on webpage
  • Staging Algebraic Datatypes
  • By Tim Sheard and Iavor Diatchki
  • Must be read by next Tueday Feb. 1
  • Volunteers for presentation?

3
Recall RE Language
  • datatype 'a Maybe Just of 'a Nothing
  • datatype RE
  • Lit of string
  • Or of (RERE)
  • Concat of (RERE)
  • Star of RE
  • fun prefix xs Just xs
  • prefix (yys) (xxs)
  • if yx then prefix ys xs else Nothing
  • prefix ys xs Nothing

4
One interpreter
  • fun eval (Lit s) input
  • (case prefix (explode s) input of
  • Just more gt Just(s,more)
  • Nothing gt Nothing)
  • eval (Or(a,b)) input
  • (case eval a input of
  • Nothing gt eval b input
  • Just pair gt Just pair)
  • eval (Concat(a,b)) input
  • (case (eval a input) of
  • Just(zs,rest) gt
  • (case eval b rest of
  • Just (bs,more) gt Just (zs bs,more)
  • Nothing gt Nothing)
  • Nothing gt Nothing)
  • eval (Star x) input
  • let fun f input case eval x input of
  • Nothing gt ("",input)
  • Just (cs,zs) gt let val
    (bs,ws) f zs

5
Staging it
  • fun Seval (Lit s) input
  • ltcase prefix (explode (lift s)) input of
  • Just more gt Just((lift s),more)
  • Nothing gt Nothinggt
  • Seval (Or(a,b)) input
  • ltcase (Seval a input) of
  • Nothing gt (Seval b input)
  • Just pair gt Just pairgt
  • Seval (Concat(a,b)) input
  • ltcase (Seval a input) of
  • Just(zs,rest) gt
  • (case (Seval b ltrestgt) of
  • Just (bs,more) gt Just (zs bs,more)
  • Nothing gt Nothing)
  • Nothing gt Nothinggt

6
Staging continued
Note how the helper function f is in the
generated code.
  • Seval (Star x) input
  • ltlet fun f input
  • case (Seval x ltinputgt)
  • Nothing gt ("",input)
  • Just (cs,zs) gt
  • let val (bs,ws) f zs
  • in (cs bs,ws) end
  • in Just(f input) endgt

7
What does it generate?
  • val t1
  • Concat(Or(Lit "tim",Lit "mary")
  • ,Star (Lit "x"))
  • fun test x
  • ltfn s gt (Seval x ltsgt)gt
  • fun f x (run(test t1)) (explode x)
  • test t1

8
  • - test t1
  • val it
  • lt(fn a gt
  • (case (case (case prefix (explode "tim") a
    of
  • Just o gt Just("tim",o)
  • Nothing gt Nothing) of
  • Nothing gt
  • (case prefix (explode "mary") a
    of
  • Just n gt Just("mary",n)
  • Nothing gt Nothing)
  • Just m gt Just m) of
  • Just(c,b) gt
  • (case let fun f g (case (case prefix
    (explode "x") g of
  • Just l gt
    Just("x",l)
  • Nothing gt
    Nothing) of
  • Nothing gt
    ("",g)
  • Just(i,h) gt let
    val (k,j) f h
  • in
    (i k,j) end)
  • in Just (f b) end of

9
Monadic version
  • ltDo mm
  • a lt- try (prefix2 (explode "tim"))
  • (prefix2 (explode "mary"))
  • b lt- star (prefix2 (explode "x"))
  • Return mm (a _at_ concat b)
  • gt
  • ltchar list Mgt
  • What is the Monad used here?

10
The meaning type
  • Eval RE -gt char list -gt
  • (string char list) Maybe
  • datatype 'a M
  • M of (char list -gt ('a char list) Maybe)
  • fun unM (M x) x
  • Eval2 RE -gt char list M

11
Implementing the Monad
  • val mm
  • let fun return x M (fn s gt Just(x,s))
  • fun bind (M f) g
  • let fun h s
  • case f s of
  • Just(a,s2) gt unM (g a) s2
  • Nothing gt Nothing
  • in M h end
  • in Mon(return,bind) end

12
Operations on the monad
  • fun test (M f)
  • let fun h s case f s of
  • Nothing gt Just(Nothing,s)
  • Just(a,rest) gt Just(Just
    a,rest)
  • in M h end
  • fun prefix2 s
  • let fun h x case prefix s x of
  • Nothing gt Nothing
  • Just m gt Just(s,m)
  • in M h end
  • fun try (M f) (M g)
  • let fun h s case f s of
  • Nothing gt g s
  • Just pair gt Just pair
  • in M h end

13
Monadic Version
  • fun star m
  • Do mm mx lt- test m
  • case mx of
  • Just x gt Do mm xs lt- star m
  • Return mm (xxs)
  • Nothing gt Return mm
  • fun eval2 (Lit s) prefix2 (explode s)
  • eval2 (Or(a,b)) try (eval2 a) (eval2 b)
  • eval2 (Concat(a,b))
  • Do mm x lt- eval2 a
  • y lt- eval2 b
  • Return mm (x_at_y)
  • eval2 (Star x)
  • Do mm xss lt- star (eval2 x)
  • Return mm (concat xss)

14
Staged Monadic Version
  • fun Seval2 (Lit s) ltprefix2 (explode (lift
    s))gt
  • Seval2 (Or(a,b))
  • lttry (Seval2 a) (Seval2 b)gt
  • Seval2 (Concat(a,b))
  • ltDo mm x lt- (Seval2 a)
  • y lt- (Seval2 b)
  • Return mm (x_at_y)gt
  • Seval2 (Star x)
  • ltDo mm xss lt- star (Seval2 x)
  • Return mm (concat xss)
  • gt

15
Results
  • - Seval2 t1
  • val it
  • ltDo mm
  • a lt- try (prefix2 (explode "tim"))
  • (prefix2 (explode "mary"))
  • b lt- star (prefix2 (explode "x"))
  • Return mm (a _at_ concat b)
  • gt
  • ltchar list Mgt
  • Note calls to the monadic operators try,
    star, test (embedded in call to star), and
    prefix2

16
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com