What's this have to do with tree\_mondadize?
--------------------------------------------
-So we've defined a Tree monad
+So we've defined a Tree monad:
type 'a tree = Leaf of 'a | Node of ('a tree) * ('a tree);;
let tree_unit (a: 'a) : 'a tree = Leaf a;;
... and so on for different monads?
-The answer is that each of those `tree_monadize` functions is adding a Tree monad *layer* to a pre-existing Reader (and so on) monad. So far, we've defined monads as single-layered things. (Though in the Groenendijk, Stokhoff, and Veltmann application, we had to figure out how to combine Reader, State, and Set monads in an ad-hoc way.) But in practice, one often wants to combine the abilities of several monads. Corresponding to each monad like Reader, there's a corresponding ReaderT **monad transformer**. That takes an existing monad M and adds a Reader monad layer to it. The way these are defined parallels the way the single-layer versions are defined. For example, here's the Reader monad:
+The answer is that each of those `tree_monadize` functions is adding a Tree monad *layer* to a pre-existing Reader (and so on) monad. So far, we've defined monads as single-layered things. Though in the Groenendijk, Stokhoff, and Veltmann homework, we had to figure out how to combine Reader, State, and Set monads in an ad-hoc way. In practice, one often wants to combine the abilities of several monads. Corresponding to each monad like Reader, there's a corresponding ReaderT **monad transformer**. That takes an existing monad M and adds a Reader monad layer to it. The way these are defined parallels the way the single-layer versions are defined. For example, here's the Reader monad:
(* monadic operations for the Reader monad *)
let bind (u: 'a reader) (f : 'a -> 'b reader) : 'b reader =
fun e -> (fun v -> f v e) (u e);;
-We've just beta-expanded the familiar `f (u e) e` into `(fun v -> f v e) (u e)` to factor out the parts where any Reader monad is being supplied as an argument to another function. Then if we want instead to add a Reader layer to some arbitrary other monad M, with its own M.unit and M.bind, here's how we do it:
+We've just beta-expanded the familiar `f (u e) e` into `(fun v -> f v e) (u e)`, in order to factor out the parts where any Reader monad is being supplied as an argument to another function. Then if we want instead to add a Reader layer to some arbitrary other monad M, with its own M.unit and M.bind, here's how we do it:
(* monadic operations for the ReaderT monadic transformer *)
(* We're not giving valid OCaml code, but rather something
* that's conceptually easier to digest.
- * How you really need to write this in OCaml is more circuitious...
+ * How you really need to write this in OCaml is more circuitous...
* see http://lambda.jimpryor.net/code/tree_monadize.ml for some details. *)
type ('a, M) readerT =
env -> 'a M;;
- (* this is just an 'a M reader; but that doesn't generalize *)
+ (* this is just an 'a M reader; but don't rely on that pattern to generalize *)
let unit (a : 'a) : ('a, M) readerT =
fun e -> M.unit a;;
let bind (u : ('a, M) readerT) (f : 'a -> ('b, M) readerT) : ('b, M) readerT =
fun e -> M.bind (u e) (fun v -> f v e);;
-Notice the key differences: where before we just returned `a`, now we return `M.unit a`. Where before we just supplied a value `u e` of type `'a reader` as an argument to a function, now we instead `M.bind` the `'a reader` to that function. Notice also the differences in the types.
+Notice the key differences: where before we just returned `a`, now we instead return `M.unit a`. Where before we just supplied value `u e` of type `'a reader` as an argument to a function, now we instead `M.bind` the `'a reader` to that function. Notice also the differences in the types.
What is the relation between Reader and ReaderT? Well, suppose you started with the Identity monad:
let rec bind (u : 'a tree) (f : 'a -> 'b tree) : 'b tree =
match u with
- | Leaf a -> (fun b -> Leaf b) (f a)
+ | Leaf a -> f a;;
| Node (l, r) -> (fun l' r' -> Node (l', r')) (bind l f) (bind r f);;
(* monadic operations for the TreeT monadic transformer *)
+ (* NOTE THIS IS NOT YET WORKING --- STILL REFINING *)
type ('a, M) treeT =
'a tree M;;