Lecture 6 Monadic Staging of the RE language

  • Slides: 16
Download presentation
Lecture 6: Monadic Staging of the RE language Tim Sheard Oregon Graduate Institute CSE

Lecture 6: Monadic Staging of the RE language Tim Sheard Oregon Graduate Institute CSE 510 Section FSC Winter 2004 Algorithms CSE 532, Spring 2000

Assignments Assignment 4 is now posted on webpage n Due in 1 week on

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

Recall RE Language datatype 'a Maybe = Just of 'a | Nothing; datatype RE

Recall RE Language datatype 'a Maybe = Just of 'a | Nothing; datatype RE = Lit of string | Or of (RE*RE) | Concat of (RE*RE) | Star of RE; fun prefix [] xs = | prefix (y: : ys) if y=x then | prefix ys xs = Cs 510 FSC Winter 2005 Just xs (x: : xs) = prefix ys xs else Nothing; 3

One interpreter fun eval (Lit s) input = (case prefix (explode s) input of

One interpreter fun eval (Lit s) input = (case prefix (explode s) input of Just more => Just(s, more) | Nothing => Nothing) | eval (Or(a, b)) input = (case eval a input of Nothing => eval b input | Just pair => Just pair) | eval (Concat(a, b)) input = (case (eval a input) of Just(zs, rest) => (case eval b rest of Just (bs, more) => Just (zs ^ bs, more) | Nothing => Nothing) | eval (Star x) input = let fun f input = case eval x input of Nothing => ("", input) | Just (cs, zs) => let val (bs, ws) = f zs in (cs ^ bs, ws) end in Just(f input) end; Cs 510 FSC Winter 2005 4

Staging it fun Seval (Lit s) input = <case prefix (explode ~(lift s)) ~input

Staging it fun Seval (Lit s) input = <case prefix (explode ~(lift s)) ~input of Just more => Just(~(lift s), more) | Nothing => Nothing> | Seval (Or(a, b)) input = <case ~(Seval a input) of Nothing => ~(Seval b input) | Just pair => Just pair> | Seval (Concat(a, b)) input = <case ~(Seval a input) of Just(zs, rest) => (case ~(Seval b <rest>) of Just (bs, more) => Just (zs ^ bs, more) | Nothing => Nothing> Cs 510 FSC Winter 2005 5

Staging continued Note how the helper | Seval (Star x) input = function “f”

Staging continued Note how the helper | Seval (Star x) input = function “f” is in the generated code. <let fun f input = case ~(Seval x <input>) Nothing => ("", input) | Just (cs, zs) => let val (bs, ws) = f zs in (cs ^ bs, ws) end in Just(f ~input) end>; Cs 510 FSC Winter 2005 6

What does it generate? val t 1 = Concat(Or(Lit "tim", Lit "mary") , Star

What does it generate? val t 1 = Concat(Or(Lit "tim", Lit "mary") , Star (Lit "x")); fun test x = <fn s => ~(Seval x <s>)>; fun f x = (run(test t 1)) (explode x); test t 1; Cs 510 FSC Winter 2005 7

-| test t 1; val it = <(fn a => (case %prefix (%explode "tim")

-| test t 1; val it = <(fn a => (case %prefix (%explode "tim") a of Just o => Just("tim", o) | Nothing => Nothing) of Nothing => (case %prefix (%explode "mary") a of Just n => Just("mary", n) | Nothing => Nothing) | Just m => Just m) of Just(c, b) => (case let fun f g = (case %prefix (%explode "x") g of Just l => Just("x", l) | Nothing => Nothing) of Nothing => ("", g) | Just(i, h) => let val (k, j) = f h in (i %^ k, j) end) in Just (f b) end of Just(e, d) => Just(c %^ e, d) | Nothing => Nothing))> : <char list -> (string * char list) Maybe> Cs 510 FSC Winter 2005 8

Monadic version <Do %mm { a <- %try (%prefix 2 (%explode "tim")) (%prefix 2

Monadic version <Do %mm { a <- %try (%prefix 2 (%explode "tim")) (%prefix 2 (%explode "mary")) ; b <- %star (%prefix 2 (%explode "x")) ; Return %mm (a %@ %concat b) }> : <char list M> What is the Monad used here? Cs 510 FSC Winter 2005 9

The “meaning” type Eval : : RE -> char list -> (string * char

The “meaning” type Eval : : RE -> char list -> (string * char list) Maybe datatype 'a M = M of (char list -> ('a* char list) Maybe); fun un. M (M x) = x; Eval 2 : : RE -> char list M Cs 510 FSC Winter 2005 10

Implementing the Monad val mm = let fun return x = M (fn s

Implementing the Monad val mm = let fun return x = M (fn s => Just(x, s)); fun bind (M f) g = let fun h s = case f s of Just(a, s 2) => un. M (g a) s 2 | Nothing => Nothing in M h end in Mon(return, bind) end; Cs 510 FSC Winter 2005 11

Operations on the monad fun test (M f) = let fun h s =

Operations on the monad fun test (M f) = let fun h s = case f s of Nothing => Just(Nothing, s) | Just(a, rest) => Just(Just a, rest) in M h end; fun prefix 2 s = let fun h x = case prefix s x of Nothing => Nothing | Just m => Just(s, m) in M h end; fun try (M f) (M g) = let fun h s = case f s of Nothing => g s | Just pair => Just pair in M h end Cs 510 FSC Winter 2005 12

Monadic Version fun star m = Do mm { mx <- test m ;

Monadic Version fun star m = Do mm { mx <- test m ; case mx of Just x => Do mm { xs <- star m ; Return mm (x: : xs) } | Nothing => Return mm []}; fun eval 2 (Lit s) = prefix 2 (explode s) | eval 2 (Or(a, b)) = try (eval 2 a) (eval 2 b) | eval 2 (Concat(a, b)) = Do mm { x <- eval 2 a ; y <- eval 2 b ; Return mm (x@y)} | eval 2 (Star x) = Do mm { xss <- star (eval 2 x) ; Return mm (concat xss) } Cs 510 FSC Winter 2005 13

Staged Monadic Version fun Seval 2 (Lit s) = <prefix 2 (explode ~(lift s))>

Staged Monadic Version fun Seval 2 (Lit s) = <prefix 2 (explode ~(lift s))> | Seval 2 (Or(a, b)) = <try ~(Seval 2 a) ~(Seval 2 b)> | Seval 2 (Concat(a, b)) = <Do mm { x <- ~(Seval 2 a) ; y <- ~(Seval 2 b) ; Return mm (x@y)}> | Seval 2 (Star x) = <Do mm { xss <- star ~(Seval 2 x) ; Return mm (concat xss) }>; Cs 510 FSC Winter 2005 14

Results -| Seval 2 t 1; val it = <Do %mm { a <-

Results -| Seval 2 t 1; val it = <Do %mm { a <- %try (%prefix 2 (%explode "tim")) (%prefix 2 (%explode "mary")) ; b <- %star (%prefix 2 (%explode "x")) ; Return %mm (a %@ %concat b) }> : <char list M> Note calls to the monadic operators “try”, “star”, “test” (embedded in call to “star”), and “prefix 2” Cs 510 FSC Winter 2005 15

Cs 510 FSC Winter 2005 16

Cs 510 FSC Winter 2005 16