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

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

CS 410 Applied Algorithms Applied Algorithms Lecture #7 Counting.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Tim Sheard Portland State University Lecture 11: C-Mix and partial evaluation CS510 Section FSC Winter 2005 Winter 2005.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #9, Feb. 12, 2007 A data structure for grammars Computing Nullable and First in SML.
Tim Sheard Oregon Graduate Institute Lecture 6: Monads and Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Generating Efficient code Lecture 9. Assignment Homework #6 is now available on the class web-site. –Due next Tuesday, Feb 15, 2005 Read the paper –ML.
Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004.
Advanced Programming Andrew Black and Tim Sheard Lecture 9 More Regular Expressions NFAs.
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
Cse502 Functional Programming 1 6/26/2015 Lecture #11, Nov. 4, 2002 Todays Topics –Using Calculation to prove 2 functions are equal »An example from class.
Cse321, Programming Languages and Compilers 1 6/26/2015 Lecture #4, Jan. 24, 2007 Homework 3 Representing sets as lists the cross product of two sets epsilon.
A Methodology for Generating Verified Combinatorial Circuits CSE 510 Section FSC Winter 2004 Winter 2004 Kiselyov, Swadi, Taha.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction.
Advanced Programming Andrew Black and Tim Sheard Lecture 6 Regular Expressions Unit Testing.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Advanced Functional Programming Tim Sheard 1 Lecture 14 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Advanced Functional Programming Tim Sheard 1 Lecture 6 Functional Programming Tim Sheard & Mark Jones Monads & Interpreters.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
Advanced Functional Programming Tim Sheard 1 Lecture 18 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
A Staged Semantics of Overloading (preliminary) Bill Harrison & Tim Sheard.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Advanced Functional Programming Tim Sheard 1 Lecture 6 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Functional Programming Lecture 3 - Lists Muffy Calder.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Winter 2013.
Koen Lindström Claessen
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Tim Sheard Oregon Graduate Institute
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Advanced Functional Programming
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Advanced Functional Programming
Advanced Functional Programming
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
CSE341: Programming Languages Section 3 Function Patterns Tail Recursion Winter 2018.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Winter 2018.
Advanced Functional Programming
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Functional Programming
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Advanced Functional Programming
Presentation transcript:

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

3Cs510 FSC Winter 2005 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 = Just xs | prefix (y::ys) (x::xs) = if y=x then prefix ys xs else Nothing | prefix ys xs = Nothing;

4Cs510 FSC Winter 2005 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;

5Cs510 FSC Winter 2005 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 ) of Just (bs,more) => Just (zs ^ bs,more) | Nothing => Nothing) | Nothing => Nothing>

6Cs510 FSC Winter 2005 Staging continued | Seval (Star x) input = <let fun f input = case ~(Seval x ) 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.

7Cs510 FSC Winter 2005 What does it generate? val t1 = Concat(Or(Lit "tim",Lit "mary"),Star (Lit "x")); fun test x = ~(Seval x )>; fun f x = (run(test t1)) (explode x); test t1;

8Cs510 FSC Winter | test t1; val it = (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))> : (string * char list) Maybe>

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) }> : What is the Monad used here?

10Cs510 FSC Winter 2005 The “meaning” type Eval :: 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

11Cs510 FSC Winter 2005 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,s2) => unM (g a) s2 | Nothing => Nothing in M h end in Mon(return,bind) end;

12Cs510 FSC Winter 2005 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 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

13Cs510 FSC Winter 2005 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 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 | eval2 (Star x) = Do mm { xss <- star (eval2 x) ; Return mm (concat xss) }

14Cs510 FSC Winter 2005 Staged Monadic Version fun Seval2 (Lit s) = | Seval2 (Or(a,b)) = | Seval2 (Concat(a,b)) = <Do mm { x <- ~(Seval2 a) ; y <- ~(Seval2 b) ; Return mm | Seval2 (Star x) = <Do mm { xss <- star ~(Seval2 x) ; Return mm (concat xss) }>;

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) }> : Note calls to the monadic operators “try”, “star”, “test” (embedded in call to “star”), and “prefix2”

16Cs510 FSC Winter 2005