tim sheard oregon graduate institute lecture 6: monadic staging of the re language cse 510 section...

16
Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC CSE 510 Section FSC Winter 2004 Winter 2004

Upload: jayson-young

Post on 11-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

Tim SheardOregon Graduate Institute

Lecture 6: Monadic Staging of the RE language

CSE 510 Section FSCCSE 510 Section FSC

Winter 2004Winter 2004

Page 2: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

2Cs510 FSC Winter 2005

AssignmentsAssignment 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?

Page 3: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

3Cs510 FSC Winter 2005

Recall RE Languagedatatype '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 = Just xs | prefix (y::ys) (x::xs) = if y=x then prefix ys xs else Nothing | prefix ys xs = Nothing;

Page 4: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

4Cs510 FSC Winter 2005

One interpreterfun 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) | 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;

Page 5: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

5Cs510 FSC Winter 2005

Staging itfun 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) | Nothing => Nothing>

Page 6: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

6Cs510 FSC Winter 2005

Staging continued | Seval (Star x) input = <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>;

Note how the helper function “f” is in the generated

code.

Page 7: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

7Cs510 FSC Winter 2005

What does it generate?val t1 =

Concat(Or(Lit "tim",Lit "mary")

,Star (Lit "x"));

fun test x =

<fn s => ~(Seval x <s>)>;

fun f x = (run(test t1)) (explode x);

test t1;

Page 8: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

8Cs510 FSC Winter 2005

-| test t1;val it = <(fn a => (case (case (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 (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) | Nothing => Nothing))> : <char list -> (string * char list) Maybe>

Page 9: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

9Cs510 FSC Winter 2005

Monadic version<Do %mm { a <- %try (%prefix2 (%explode "tim")) (%prefix2 (%explode "mary")) ; b <- %star (%prefix2 (%explode "x")) ; Return %mm (a %@ %concat b) }> : <char list M>

What is the Monad used here?

Page 10: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

10Cs510 FSC Winter 2005

The “meaning” typeEval :: RE -> char list ->

(string * char list) Maybe

datatype 'a M =

M of (char list -> ('a* char list) Maybe);

fun unM (M x) = x;

Eval2 :: RE -> char list M

Page 11: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

11Cs510 FSC Winter 2005

Implementing the Monadval 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,s2) => unM (g a) s2 | Nothing => Nothing in M h end in Mon(return,bind) end;

Page 12: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

12Cs510 FSC Winter 2005

Operations on the monadfun 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 prefix2 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

Page 13: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

13Cs510 FSC Winter 2005

Monadic Versionfun 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 eval2 (Lit s) = prefix2 (explode s) | eval2 (Or(a,b)) = try (eval2 a) (eval2 b) | eval2 (Concat(a,b)) = Do mm { x <- eval2 a ; y <- eval2 b ; Return mm (x@y)} | eval2 (Star x) = Do mm { xss <- star (eval2 x) ; Return mm (concat xss) }

Page 14: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

14Cs510 FSC Winter 2005

Staged Monadic Versionfun Seval2 (Lit s) = <prefix2 (explode ~(lift s))>

| Seval2 (Or(a,b)) = <try ~(Seval2 a) ~(Seval2 b)> | Seval2 (Concat(a,b)) = <Do mm { x <- ~(Seval2 a) ; y <- ~(Seval2 b) ; Return mm (x@y)}> | Seval2 (Star x) = <Do mm { xss <- star ~(Seval2 x) ; Return mm (concat xss) }>;

Page 15: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

15Cs510 FSC Winter 2005

Results-| Seval2 t1;val it = <Do %mm { a <- %try (%prefix2 (%explode "tim")) (%prefix2 (%explode "mary")) ; b <- %star (%prefix2 (%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 “prefix2”

Page 16: Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004

16Cs510 FSC Winter 2005