1 44 this time monads etc 2 44 what do monads give us a
play

1/ 44 This time: monads (etc.) = > > 2/ 44 What do - PowerPoint PPT Presentation

Last time: rows 1/ 44 This time: monads (etc.) = > > 2/ 44 What do monads give us? A general approach to implementing custom effects A reusable interface to computation A way to structure effectful programs in a functional


  1. Last time: rows ρ 1/ 44

  2. This time: monads (etc.) = > > 2/ 44

  3. What do monads give us? A general approach to implementing custom effects A reusable interface to computation A way to structure effectful programs in a functional language 3/ 44

  4. Effects 4/ 44

  5. What’s an effect? An effect is anything a function does besides mapping inputs to outputs. If an expression M evaluates to a value V and changing l e t x = M l e t x = V to in N in N changes the behaviour then M also performs effects. 5/ 44

  6. Example effects Effects available in OCaml Effects unavailable in OCaml (An effect is anything other than mapping inputs to outputs.) 6/ 44

  7. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state r := f; !r () (An effect is anything other than mapping inputs to outputs.) 6/ 44

  8. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state r := f; !r () exceptions raise Not found (An effect is anything other than mapping inputs to outputs.) 6/ 44

  9. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state r := f; !r () exceptions raise Not found I/O of various sorts input byte stdin (An effect is anything other than mapping inputs to outputs.) 6/ 44

  10. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state r := f; !r () exceptions raise Not found I/O of various sorts input byte stdin concurrency (interleaving) Gc. finalise v f (An effect is anything other than mapping inputs to outputs.) 6/ 44

  11. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state r := f; !r () exceptions raise Not found I/O of various sorts input byte stdin concurrency (interleaving) Gc. finalise v f non-termination let rec f x = f x (An effect is anything other than mapping inputs to outputs.) 6/ 44

  12. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state non-determinism r := f; !r () amb f g h exceptions raise Not found I/O of various sorts input byte stdin concurrency (interleaving) Gc. finalise v f non-termination let rec f x = f x (An effect is anything other than mapping inputs to outputs.) 6/ 44

  13. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state non-determinism r := f; !r () amb f g h exceptions first-class continuations raise Not found escape x in e I/O of various sorts input byte stdin concurrency (interleaving) Gc. finalise v f non-termination let rec f x = f x (An effect is anything other than mapping inputs to outputs.) 6/ 44

  14. Example effects Effects available in OCaml Effects unavailable in OCaml (higher-order) state non-determinism r := f; !r () amb f g h exceptions first-class continuations raise Not found escape x in e I/O of various sorts polymorphic state input byte stdin r := ”one”; r := 2 concurrency (interleaving) Gc. finalise v f non-termination let rec f x = f x (An effect is anything other than mapping inputs to outputs.) 6/ 44

  15. Example effects Effects unavailable in OCaml Effects available in OCaml non-determinism (higher-order) state amb f g h r := f; !r () first-class continuations exceptions escape x in e raise Not found polymorphic state I/O of various sorts r := ”one”; r := 2 input byte stdin checked exceptions concurrency (interleaving) IOError Gc. finalise v f int − − − − → bool non-termination let rec f x = f x (An effect is anything other than mapping inputs to outputs.) 6/ 44

  16. Capturing effects in the types Some languages capture effects in the type system. We might have two function arrows: a pure arrow a → b E an effectful arrow (or family of arrows) a − − → b and combinators for combining effectful functions E E E composeE : ( a − − → b ) → ( b − − → c ) → ( a − − → c ) E E ignoreE : ( a − − → b ) → ( a − − → unit ) E E E pairE : ( a − − → b ) → ( c − − → d ) → ( a × c − − → b × d ) E liftPure : ( a → b ) → ( a − − → b ) 7/ 44

  17. Separating application and invocation An alternative: Decompose effectful arrows into functions and computations E a − − → b becomes a → T b 8/ 44

  18. Monads ( let . . . in) 9/ 44

  19. Programming with monads An imperative program l e t id = ! counter in l e t () = counter := id + 1 in s t r i n g o f i n t id A monadic program get > > = fun id → put ( id + 1) > > = fun () → r e t u r n ( s t r i n g o f i n t id ) 10/ 44

  20. Monads module type MONAD = s i g type ’ a t v a l r e t u r n : ’ a → ’ a t v a l ( > =) : ’ a t → ( ’ a → ’b t ) → ’b t > end 11/ 44

  21. Monads module type MONAD = s i g type ’ a t v a l r e t u r n : ’ a → ’ a t v a l ( > > =) : ’ a t → ( ’ a → ’b t ) → ’b t end Laws : return v > = k ≡ k v > v > > = return ≡ v (m > = f) > = g ≡ m > = (fun x → f x > = g) > > > > 11/ 44

  22. Monad laws: intuition 12/ 44

  23. Monad laws: intuition return v > > = k ≡ k v let ! x = v in M ≡ M[x:=v] 12/ 44

  24. Monad laws: intuition return v > > = k ≡ k v let ! x = v in M ≡ M[x:=v] v > > = return ≡ v let ! x = M in x ≡ M 12/ 44

  25. Monad laws: intuition return v > = k ≡ k v > let ! x = v in M ≡ M[x:=v] v > = return ≡ v > let ! x = M in x ≡ M (m > = f) > = g ≡ m > = (fun x → f x > = g) > > > > let ! y = L in let ! x = (let! y = L in M) ≡ let ! x = M in in N N 12/ 44

  26. Example: a state monad module type STATE = s i g type s t a t e i n c l u d e MONAD v a l get : s t a t e t v a l put : s t a t e → u n i t t v a l runState : ’ a t → i n i t : s t a t e → s t a t e ∗ ’ a end 13/ 44

  27. Example: a state monad module type STATE = s i g type s t a t e i n c l u d e MONAD v a l get : s t a t e t v a l put : s t a t e → u n i t t v a l runState : ’ a t → i n i t : s t a t e → s t a t e ∗ ’ a end type ’ a t = s t a t e → s t a t e ∗ ’ a l e t r e t u r n v s = ( s , v ) 14/ 44

  28. Example: a state monad module type STATE = s i g type s t a t e i n c l u d e MONAD v a l get : s t a t e t v a l put : s t a t e → u n i t t v a l runState : ’ a t → i n i t : s t a t e → s t a t e ∗ ’ a end type ’ a t = s t a t e → s t a t e ∗ ’ a l e t ( > > =) m k s = l e t s ’ , a = m s in k a s ’ 15/ 44

  29. Example: a state monad module type STATE = s i g type s t a t e i n c l u d e MONAD v a l get : s t a t e t v a l put : s t a t e → u n i t t v a l runState : ’ a t → i n i t : s t a t e → s t a t e ∗ ’ a end type ’ a t = s t a t e → s t a t e ∗ ’ a l e t get s = ( s , s ) 16/ 44

  30. Example: a state monad module type STATE = s i g type s t a t e i n c l u d e MONAD v a l get : s t a t e t v a l put : s t a t e → u n i t t v a l runState : ’ a t → i n i t : s t a t e → s t a t e ∗ ’ a end type ’ a t = s t a t e → s t a t e ∗ ’ a l e t put s ’ = ( s ’ , ( ) ) 17/ 44

  31. Example: a state monad module type STATE = s i g type s t a t e i n c l u d e MONAD v a l get : s t a t e t v a l put : s t a t e → u n i t t v a l runState : ’ a t → i n i t : s t a t e → s t a t e ∗ ’ a end type ’ a t = s t a t e → s t a t e ∗ ’ a l e t runState m ˜ i n i t = m i n i t 18/ 44

  32. Example: a state monad module type STATE = s i g type s t a t e i n c l u d e MONAD v a l get : s t a t e t v a l put : s t a t e → u n i t t v a l runState : ’ a t → i n i t : s t a t e → s t a t e ∗ ’ a end module State (S : s i g type t end ) : STATE with type s t a t e = S . t = s t r u c t type s t a t e = S . t type ’ a t = s t a t e → s t a t e ∗ ’ a l e t r e t u r n v s = ( s , v ) l e t ( > > =) m k s = l e t s ’ , a = m s in k a s ’ l e t get s = ( s , s ) l e t put s ’ = ( s ’ , ( ) ) l e t runState m ˜ i n i t = m i n i t end 19/ 44

  33. Example: a state monad type ’ a t r e e = Empty : ’ a t r e e | Tree : ’ a t r e e ∗ ’ a ∗ ’ a t r e e → ’ a t r e e module I S t a t e = State ( s t r u c t type t = i n t end ) l e t fresh name : s t r i n g I S t a t e . t = get = fun i → > > put ( i + 1) > = fun () → > r e t u r n ( P r i n t f . s p r i n t f ”x%d” i ) l e t rec l a b e l t r e e : ’ a t r e e → s t r i n g t r e e I S t a t e . t = f u n c t i o n Empty → r e t u r n Empty | Tree ( l , v , r ) → l a b e l t r e e l > = fun l → > fresh name = fun name → > > l a b e l t r e e r > = fun r → > r e t u r n ( Tree ( l , name , r )) 20/ 44

  34. State satisfies the monad laws return v > > = k 21/ 44

  35. State satisfies the monad laws return v > > = k ≡ (definition of return, > =) > fun s → let s’, a = (fun s → (s, v)) s in k a s’ 21/ 44

Recommend


More recommend