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

1 44 this time monads etc 2 44 what do monads give us a
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Last time: rows

ρ

1/ 44

slide-2
SLIDE 2

This time: monads (etc.) > > =

2/ 44

slide-3
SLIDE 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

slide-4
SLIDE 4

Effects

4/ 44

slide-5
SLIDE 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 in N to l e t x = V in N changes the behaviour then M also performs effects.

5/ 44

slide-6
SLIDE 6

Example effects

Effects available in OCaml Effects unavailable in OCaml (An effect is anything other than mapping inputs to outputs.)

6/ 44

slide-7
SLIDE 7

Example effects

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

6/ 44

slide-8
SLIDE 8

Example effects

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

6/ 44

slide-9
SLIDE 9

Example effects

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

6/ 44

slide-10
SLIDE 10

Example effects

Effects available 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 Effects unavailable in OCaml (An effect is anything other than mapping inputs to outputs.)

6/ 44

slide-11
SLIDE 11

Example effects

Effects available 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 Effects unavailable in OCaml (An effect is anything other than mapping inputs to outputs.)

6/ 44

slide-12
SLIDE 12

Example effects

Effects available 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 Effects unavailable in OCaml non-determinism amb f g h (An effect is anything other than mapping inputs to outputs.)

6/ 44

slide-13
SLIDE 13

Example effects

Effects available 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 Effects unavailable in OCaml non-determinism amb f g h first-class continuations escape x in e (An effect is anything other than mapping inputs to outputs.)

6/ 44

slide-14
SLIDE 14

Example effects

Effects available 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 Effects unavailable in OCaml non-determinism amb f g h first-class continuations escape x in e polymorphic state r := ”one”; r := 2 (An effect is anything other than mapping inputs to outputs.)

6/ 44

slide-15
SLIDE 15

Example effects

Effects available 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 Effects unavailable in OCaml non-determinism amb f g h first-class continuations escape x in e polymorphic state r := ”one”; r := 2 checked exceptions int

IOError

− − − − → bool (An effect is anything other than mapping inputs to outputs.)

6/ 44

slide-16
SLIDE 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 an effectful arrow (or family of arrows) a

E

− − → b and combinators for combining effectful functions composeE : (a

E

− − → b) → (b

E

− − → c) → (a

E

− − → c) ignoreE : (a

E

− − → b) → (a

E

− − → unit) pairE : (a

E

− − → b) → (c

E

− − → d) → (a × c

E

− − → b × d) liftPure : (a → b) → (a

E

− − → b)

7/ 44

slide-17
SLIDE 17

Separating application and invocation

An alternative: Decompose effectful arrows into functions and computations a

E

− − → b becomes a → T b

8/ 44

slide-18
SLIDE 18

Monads

( let . . . in)

9/ 44

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 22

Monad laws: intuition

12/ 44

slide-23
SLIDE 23

Monad laws: intuition

return v > > = k ≡ k v let ! x = v in M ≡ M[x:=v]

12/ 44

slide-24
SLIDE 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

slide-25
SLIDE 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 ! x = (let! y = L in M) in N ≡ let ! y = L in let ! x = M in N

12/ 44

slide-26
SLIDE 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

slide-27
SLIDE 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

slide-28
SLIDE 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

slide-29
SLIDE 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

slide-30
SLIDE 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

slide-31
SLIDE 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

slide-32
SLIDE 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

slide-33
SLIDE 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

slide-34
SLIDE 34

State satisfies the monad laws

return v > > = k

21/ 44

slide-35
SLIDE 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

slide-36
SLIDE 36

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’ ≡ (β) fun s → let s’, a = (s, v) in k a s’

21/ 44

slide-37
SLIDE 37

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’ ≡ (β) fun s → let s’, a = (s, v) in k a s’ ≡ (β for let ) fun s → k v s

21/ 44

slide-38
SLIDE 38

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’ ≡ (β) fun s → let s’, a = (s, v) in k a s’ ≡ (β for let ) fun s → k v s ≡ (η) k v

21/ 44

slide-39
SLIDE 39

Example: exception

module type ERROR = s i g type e r r o r i n c l u d e MONAD v a l r a i s e : e r r o r → ’ a t v a l t r y : ’ a t → catch : ( e r r o r → ’ a ) → ’ a end

22/ 44

slide-40
SLIDE 40

Example: exception

module type ERROR = s i g type e r r o r i n c l u d e MONAD v a l r a i s e : e r r o r → ’ a t v a l t r y : ’ a t → catch : ( e r r o r → ’ a ) → ’ a end type ’ a t = Val : ’ a → ’ a t | Exn : e r r o r → ’ a t l e t r e t u r n v = Val v

23/ 44

slide-41
SLIDE 41

Example: exception

module type ERROR = s i g type e r r o r i n c l u d e MONAD v a l r a i s e : e r r o r → ’ a t v a l t r y : ’ a t → catch : ( e r r o r → ’ a ) → ’ a end type ’ a t = Val : ’ a → ’ a t | Exn : e r r o r → ’ a t l e t (> > =) m k = match m with Val v → k v | Exn e → Exn e

24/ 44

slide-42
SLIDE 42

Example: exception

module type ERROR = s i g type e r r o r i n c l u d e MONAD v a l r a i s e : e r r o r → ’ a t v a l t r y : ’ a t → catch : ( e r r o r → ’ a ) → ’ a end type ’ a t = Val : ’ a → ’ a t | Exn : e r r o r → ’ a t l e t r a i s e e = Exn e

25/ 44

slide-43
SLIDE 43

Example: exception

module type ERROR = s i g type e r r o r i n c l u d e MONAD v a l r a i s e : e r r o r → ’ a t v a l t r y : ’ a t → catch : ( e r r o r → ’ a ) → ’ a end type ’ a t = Val : ’ a → ’ a t | Exn : e r r o r → ’ a t l e t t r y m ˜ catch = match m with Val v → v | Exn e → catch e

26/ 44

slide-44
SLIDE 44

Example: exception

module type ERROR = s i g type e r r o r i n c l u d e MONAD v a l r a i s e : e r r o r → ’ a t v a l t r y : ’ a t → catch : ( e r r o r → ’ a ) → ’ a end module Error (E : s i g type t end ) : ERROR with type e r r o r = E . t = s t r u c t type e r r o r = E . t type ’ a t = Val : ’ a → ’ a t | Exn : e r r o r → ’ a t l e t r e t u r n v = Val v l e t (> > =) m k = match m with Val v → k v | Exn e → Exn e l e t r a i s e e = Exn e l e t t r y m ˜ catch = match m with Val v → v | Exn e → catch e end

27/ 44

slide-45
SLIDE 45

Example: exception

l e t rec mapMTree f = f u n c t i o n Empty → r e t u r n Empty | Tree ( l , v , r ) → mapMTree f l > > = fun l → f v > > = fun v → mapMTree f r > > = fun r → r e t u r n ( Tree ( l , v , r )) l e t check nonzero = mapMTree ( fun v → i f v = 0 then r a i s e Zero e l s e r e t u r n v )

28/ 44

slide-46
SLIDE 46

Exception satisfies the monad laws

v > > = return

29/ 44

slide-47
SLIDE 47

Exception satisfies the monad laws

v > > = return ≡ (definition of return, > > =) match v with Val v → Val v | Exn e → Exn e

29/ 44

slide-48
SLIDE 48

Exception satisfies the monad laws

v > > = return ≡ (definition of return, > > =) match v with Val v → Val v | Exn e → Exn e ≡ (η for sums) v

29/ 44

slide-49
SLIDE 49

Indexed monads

(Γ ⊢ M : A ! e)

30/ 44

slide-50
SLIDE 50

Indexed monads and effect systems

A computation of type (’e, ’a) t performs an effect ’e produces a result of type ’a.

31/ 44

slide-51
SLIDE 51

Strengthening the interface: indexed monads

module type INDEXED MONAD = s i g type ( ’ e , ’ a ) t v a l r e t u r n : ’ a → ( , ’ a ) t v a l (> > =) : ( ’ e , ’ a ) t → ( ’ a → ( ’ e , ’ b) t ) → ( ’ e , ’ b) t end (Laws: as for monads.)

32/ 44

slide-52
SLIDE 52

An indexed monad for exceptions

module type IERROR = s i g i n c l u d e INDEXED MONAD v a l r a i s e : ’ e → ( ’ e , ) t v a l t r y : ( ’ e , ’ a ) t → catch : ( ’ e → ’ a ) → ’ a end

33/ 44

slide-53
SLIDE 53

An indexed monad for exceptions

module I E r r o r : IERROR = s t r u c t type ( ’ e , ’ a ) t = Val : ’ a → ( ’ e , ’ a ) t | Exn : ’ e → ( ’ e , ’ a ) t l e t r e t u r n v = Val v l e t r a i s e e = Exn e l e t (> > =) m k = match m with Val v → k v | Exn e → Exn e l e t t r y m ˜ catch = match m with Val v → v | Exn e → catch e end

34/ 44

slide-54
SLIDE 54

Indexed monads and rows

l e t rec f i n d p = f u n c t i o n [ ] → r a i s e ` Not found | x : : when p x → r e t u r n x | : : xs → f i n d p xs l e t pop = f u n c t i o n [ ] → r a i s e ( `Empty ”pop ”) | x : : xs → r e t u r n ( x , xs ) l e t gt 0 x = x > 0 pop [ ] > > = fun ( , xs ) → f i n d gt 0 xs > > = fun y → r e t u r n y l e t rec f i n d p = f u n c t i o n [ ] → r a i s e Not found | x : : when p x → x | : : xs → f i n d p xs l e t pop = f u n c t i o n [ ] → r a i s e ( Empty ”pop ”) | x : : xs → ( x , xs ) l e t gt 0 x = x > 0 l e t , xs = pop [ ] i n l e t y = f i n d gt 0 xs i n y

35/ 44

slide-55
SLIDE 55

Parameterised monads

({P} C {Q})

36/ 44

slide-56
SLIDE 56

Parameterised monads and Hoare Logic

A computation of type (’p, ’q, ’a) t has precondition ’p has postcondition ’q produces a result of type ’a. i.e. (’p, ’q, ’a) t is a kind of Hoare triple {P} M {Q}.

37/ 44

slide-57
SLIDE 57

Strengthening the interface: parameterised monads

module type PARAMETERISED MONAD = s i g type ( ’ s , ’ t , ’ a ) t v a l r e t u r n : ’ a → ( ’ s , ’ s , ’ a ) t v a l (> > =) : ( ’ r , ’ s , ’ a ) t → ( ’ a → ( ’ s , ’ t , ’ b) t ) → ( ’ r , ’ t , ’ b) t end (Laws: as for monads.)

38/ 44

slide-58
SLIDE 58

A parameterised monad for state

module type PSTATE = s i g i n c l u d e PARAMETERISED MONAD v a l get : ( ’ s , ’ s , ’ s ) t v a l put : ’ s → ( , ’ s , u n i t ) t v a l runState : ( ’ s , ’ t , ’ a ) t → i n i t : ’ s → ’ t ∗ ’ a end

39/ 44

slide-59
SLIDE 59

A parameterised monad for state

module PState : PSTATE = s t r u c t type ( ’ s , ’ t , ’ a ) t = ’ s → ’ t ∗ ’ a l e t r e t u r n v s = ( s , v ) l e t (> > =) m k s = l e t t , a = m s in k a t l e t put s = ( s , ( ) ) l e t get s = ( s , s ) l e t runState m ˜ i n i t = m i n i t end

40/ 44

slide-60
SLIDE 60

Programming with polymorphic state

type ( , ) i n s t r = Add : ( i n t ∗ ( i n t ∗ ’ s ) , i n t ∗ ’ s ) i n s t r | I f : ( bool ∗ ( ’ a ∗ ( ’ a ∗ ’ s )) , ’ a ∗ ’ s ) i n s t r | PushConst : ’ a → ( ’ s , ’ a ∗ ’ s ) i n s t r type ( , ) i n s t r s = Stop : ( ’ s , ’ s ) i n s t r s | : : : ( ’ s1 , ’ s2 ) i n s t r ∗ ( ’ s2 , ’ s3 ) i n s t r s → ( ’ s1 , ’ s3 ) i n s t r s l e t program = PushConst 3 : : PushConst 4 : : PushConst 5 : : PushConst true : : I f : : Add : : Stop

41/ 44

slide-61
SLIDE 61

Programming with polymorphic state

l e t add ( x , ( y , s )) = ( x+y , s ) l e t i f ( c , ( t , ( e , s ) ) ) = (( i f c then t e l s e e ) , s ) l e t push const k s = (k , s ) l e t applyS f = get > >= fun s → put ( f s ) l e t exec1 : type a b . ( a , b) i n s t r → (a , b , u n i t ) Pstate . t = f u n c t i o n Add → applyS add | I f → applyS i f | PushConst k → applyS ( push const k ) l e t rec exec : type a b . i n t → (a , b) i n s t r s → (a , b , i n t ) Pstate . t = fun c → f u n c t i o n i : : i s → exec1 i > >= fun () → exec ( succ c ) i s | Stop → r e t u r n c

42/ 44

slide-62
SLIDE 62

Next time:

The struggle for power

43/ 44