OK, back to our walk-through of "A State Monad Tutorial". What shall we use for a store? Instead of a plain `int`, let's suppose our store is a structure of two values: a running total, and a count of how many times the store has been modified. We'll implement this with a record. Hence:
- type store' = { total : int; modifications: int };;
+ type store' = { total : int; modifications: int }
State monads employing this store will then have *three* salient values at any point in the computation: the `total` and `modifications` field in the store, and also the `'a` payload that is currently wrapped in the monadic box.
Here is how you'd have to do it using our OCaml/Juli8 monad library:
- # (* have Juli8 loaded, as explained elsewhere *)
+ (* have Juli8 loaded, as explained elsewhere *)
# module S = Monad.State(struct type store = store' end);;
# let increment_store'' : 'a S.t =
S.(get >>= fun cur ->
You should start to see here how chaining monadic values together gives us a kind of programming language. Of course, it's a cumbersome programming language. It'd be much easier to write, directly in OCaml:
let value = s0.total
- in (value, { total = s0.total + 2; modifications = s0.modifications + 2};;
+ in (value, { total = s0.total + 2; modifications = s0.modifications + 2})
or, using pattern-matching on the record (you don't have to specify every field in the record):
let { total = value; _ } = s0
- in (value, { total = s0.total + 2; modifications = s0.modifications + 2};;
+ in (value, { total = s0.total + 2; modifications = s0.modifications + 2})
But **the point of learning how to do this monadically** is that (1) monads show us how to embed more sophisticated programming techniques, such as imperative state and continuations, into frameworks that don't natively possess them (such as the set-theoretic metalanguage of Groenendijk, Stokhof and Veltman's paper); (2) becoming familiar with monads will enable you to see patterns you'd otherwise miss, and implement some seemingly complex computations using the same simple patterns (same-fringe is an example); and finally, of course (3) monads are delicious.
increment_store >> get >>= fun cur ->
State (fun s -> ((), { total = s.total / 2; modifications = succ s.modifications })) >>
- increment_store >> mid cur.total
+ increment_store >> mid cur.total
Or, as you'd have to write it using our OCaml monad library:
increment_store'' >> get >>= fun cur ->
put { total = cur.total / 2; modifications = succ cur.modifications } >>
- increment_store'' >> mid cur.total
+ increment_store'' >> mid cur.total
The last topic covered in "A State Monad Tutorial" is the use of do-notation to work with monads in Haskell. We discuss that on our [translation page](/translating_between_OCaml_Scheme_and_Haskell).