(* we assume that the store type has already been declared *)
type 'a state = State of (store -> ('a * store))
-Then a function expecting an `'a store` will look for a value with the structure `State ...` rather than just one with the structure `...`.
+Then a function expecting an `'a state` will look for a value with the structure `State ...` rather than just one with the structure `...`.
To take a `State (s -> (a,s))` and get at the `s -> (a,s)` it wraps, you use the same techniques you use to take an `Some int` and get at the `int` it wraps:
Of course you can do this:
- let xx = someMonad.(...) in
+ let xx = SomeMonad.(...) in
let intermediate_result = SomeMonad.run xx 0 in
let yy = SomeMonad.(xx >>= ...) in
let final_result = SomeMonad.run yy 0 in
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` value that is then wrapped in the monadic box.
+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's a monadic box that encodes the operation of incrementing the store's `total` and wrapping the value that was the former `total`:
+Here's a monadic value that encodes the operation of incrementing the store's `total` and wrapping the value that was the former `total`:
let increment_store : store' -> (int * store') = fun s ->
let value = s.total in
What are the special-purpose operations that the `State_monad` module defines for us?
-* `get` is a monadic value that passes through the existing store unchanged, and also wraps that same store as its boxed value. You use it like this:
+* `get` is a monadic value that passes through the existing store unchanged, and also wraps that same store as its boxed payload. You use it like this:
... >>= fun _ -> get >>= fun cur -> ... here we can use cur ...
... >> put new_store >> fun () -> ...
- As that code snippet suggests, the boxed value after the application of `modify new_store` is just `()`. If you want to preserve the existing boxed value but replace the store, do this:
+ As that code snippet suggests, the boxed payload after the application of `modify new_store` is just `()`. If you want to preserve the existing payload but replace the store, do this:
... >>= fun value -> put new_store >> unit value >>= ...
-* Finally, `modify modifier` applies `modifier` to whatever the existing store is, and substitutes that as the new store. As with `put`, the boxed value afterwards is `()`.
+* Finally, `modify modifier` applies `modifier` to whatever the existing store is, and substitutes that as the new store. As with `put`, the boxed payload afterwards is `()`.
<!--
Haskell calls this operation `modify`. We've called it `puts` because it seems to fit naturally with the convention of `get` vs `gets`. (See also `ask` vs `asks` in `Reader_monad`, which are also the names used in Haskell.)