- | Leaf x -> Leaf (newleaf x)
- | Node (l, r) -> Node (treemap newleaf l,
- treemap newleaf r);;
+ | Leaf i -> Leaf (leaf_modifier i)
+ | Node (l, r) -> Node (tree_map leaf_modifier l,
+ tree_map leaf_modifier r);;
and maps that function over all the leaves in the tree, leaving the
structure of the tree unchanged. For instance:
let double i = i + i;;
and maps that function over all the leaves in the tree, leaving the
structure of the tree unchanged. For instance:
let double i = i + i;;
- : int tree =
Node (Node (Leaf 4, Leaf 6), Node (Leaf 10, Node (Leaf 14, Leaf 22)))
- : int tree =
Node (Node (Leaf 4, Leaf 6), Node (Leaf 10, Node (Leaf 14, Leaf 22)))
-We could have built the doubling operation right into the `treemap`
-code. However, because what to do to each leaf is a parameter, we can
+We could have built the doubling operation right into the `tree_map`
+code. However, because we've left what to do to each leaf as a parameter, we can
- : int tree =ppp
Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
- : int tree =ppp
Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
That is, we want to transform the ordinary tree `t1` (of type `int
tree`) into a reader object of type `(int -> int) -> int tree`: something
that, when you apply it to an `int -> int` function `f` returns an `int
That is, we want to transform the ordinary tree `t1` (of type `int
tree`) into a reader object of type `(int -> int) -> int tree`: something
that, when you apply it to an `int -> int` function `f` returns an `int
With previous readers, we always knew which kind of environment to
expect: either an assignment function (the original calculator
simulation), a world (the intensionality monad), an integer (the
With previous readers, we always knew which kind of environment to
expect: either an assignment function (the original calculator
simulation), a world (the intensionality monad), an integer (the
-Jacobson-inspired link monad), etc. In this situation, it will be
-enough for now to expect that our reader will expect a function of
-type `int -> int`.
+Jacobson-inspired link monad), etc. In the present case, it will be
+enough to expect that our "environment" will be some function of type
+`int -> int`.
type 'a reader = (int -> int) -> 'a;; (* mnemonic: e for environment *)
let reader_unit (a : 'a) : 'a reader = fun _ -> a;;
let reader_bind (u: 'a reader) (f : 'a -> 'b reader) : 'b reader = fun e -> f (u e) e;;
type 'a reader = (int -> int) -> 'a;; (* mnemonic: e for environment *)
let reader_unit (a : 'a) : 'a reader = fun _ -> a;;
let reader_bind (u: 'a reader) (f : 'a -> 'b reader) : 'b reader = fun e -> f (u e) e;;
- let int2int_reader : 'a -> 'b reader = fun (a : 'a) -> fun (op : 'a -> 'b) -> op a;;
- int2int_reader 2 (fun i -> i + i);;
+ let int_readerize : int -> int reader = fun (a : int) -> fun (modifier : int -> int) -> modifier a;;
+ int_readerize 2 (fun i -> i + i);;
-But what do we do when the integers are scattered over the leaves of a
-tree? A binary tree is not the kind of thing that we can apply a
+But how do we do the analagous transformation when our `int`s are scattered over the leaves of a tree? How do we turn an `int tree` into a reader?
+A tree is not the kind of thing that we can apply a
- | Leaf x -> reader_bind (f x) (fun x' -> reader_unit (Leaf x'))
- | Node (l, r) -> reader_bind (treemonadizer f l) (fun x ->
- reader_bind (treemonadizer f r) (fun y ->
+ | Leaf i -> reader_bind (f i) (fun i' -> reader_unit (Leaf i'))
+ | Node (l, r) -> reader_bind (tree_monadize f l) (fun x ->
+ reader_bind (tree_monadize f r) (fun y ->
reader_unit (Node (x, y))));;
This function says: give me a function `f` that knows how to turn
something of type `'a` into an `'b reader`, and I'll show you how to
reader_unit (Node (x, y))));;
This function says: give me a function `f` that knows how to turn
something of type `'a` into an `'b reader`, and I'll show you how to
-turn an `'a tree` into an `'a tree reader`. In more fanciful terms,
-the `treemonadizer` function builds plumbing that connects all of the
+turn an `'a tree` into an `'b tree reader`. In more fanciful terms,
+the `tree_monadize` function builds plumbing that connects all of the
- : int tree =
Node (Node (Leaf 4, Leaf 6), Node (Leaf 10, Node (Leaf 14, Leaf 22)))
Here, our environment is the doubling function (`fun i -> i + i`). If
- : int tree =
Node (Node (Leaf 4, Leaf 6), Node (Leaf 10, Node (Leaf 14, Leaf 22)))
Here, our environment is the doubling function (`fun i -> i + i`). If
-we apply the very same `int tree reader` (namely, `treemonadizer
-int2int_reader t1`) to a different `int -> int` function---say, the
+we apply the very same `int tree reader` (namely, `tree_monadize
+int_readerize t1`) to a different `int -> int` function---say, the
- : int tree =
Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
Now that we have a tree transformer that accepts a reader monad as a
parameter, we can see what it would take to swap in a different monad.
- : int tree =
Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
Now that we have a tree transformer that accepts a reader monad as a
parameter, we can see what it would take to swap in a different monad.
For instance, we can use a state monad to count the number of nodes in
the tree.
type 'a state = int -> 'a * int;;
For instance, we can use a state monad to count the number of nodes in
the tree.
type 'a state = int -> 'a * int;;
- let state_unit a = fun i -> (a, i);;
- let state_bind u f = fun i -> let (a, i') = u i in f a (i' + 1);;
+ let state_unit a = fun s -> (a, s);;
+ let state_bind_and_count u f = fun s -> let (a, s') = u s in f a (s' + 1);;
modification whatsoever, except for replacing the (parametric) type
`'b reader` with `'b state`, and substituting in the appropriate unit and bind:
modification whatsoever, except for replacing the (parametric) type
`'b reader` with `'b state`, and substituting in the appropriate unit and bind:
- | Leaf x -> state_bind (f x) (fun x' -> state_unit (Leaf x'))
- | Node (l, r) -> state_bind (treemonadizer f l) (fun x ->
- state_bind (treemonadizer f r) (fun y ->
+ | Leaf i -> state_bind_and_count (f i) (fun i' -> state_unit (Leaf i'))
+ | Node (l, r) -> state_bind_and_count (tree_monadize f l) (fun x ->
+ state_bind_and_count (tree_monadize f r) (fun y ->
state_unit (Node (x, y))));;
Then we can count the number of nodes in the tree:
state_unit (Node (x, y))));;
Then we can count the number of nodes in the tree:
- : int tree * int =
(Node (Node (Leaf 2, Leaf 3), Node (Leaf 5, Node (Leaf 7, Leaf 11))), 13)
- : int tree * int =
(Node (Node (Leaf 2, Leaf 3), Node (Leaf 5, Node (Leaf 7, Leaf 11))), 13)
Now for the main point. What if we wanted to convert a tree to a list
of leaves?
type ('a, 'r) continuation = ('a -> 'r) -> 'r;;
Now for the main point. What if we wanted to convert a tree to a list
of leaves?
type ('a, 'r) continuation = ('a -> 'r) -> 'r;;
- let rec treemonadizer (f : 'a -> ('b, 'r) continuation) (t : 'a tree) : ('b tree, 'r) continuation =
+ let rec tree_monadize (f : 'a -> ('b, 'r) continuation) (t : 'a tree) : ('b tree, 'r) continuation =
- | Leaf x -> continuation_bind (f x) (fun x' -> continuation_unit (Leaf x'))
- | Node (l, r) -> continuation_bind (treemonadizer f l) (fun x ->
- continuation_bind (treemonadizer f r) (fun y ->
+ | Leaf i -> continuation_bind (f i) (fun i' -> continuation_unit (Leaf i'))
+ | Node (l, r) -> continuation_bind (tree_monadize f l) (fun x ->
+ continuation_bind (tree_monadize f r) (fun y ->
continuation_unit (Node (x, y))));;
We use the continuation monad described above, and insert the
continuation_unit (Node (x, y))));;
We use the continuation monad described above, and insert the
We have found a way of collapsing a tree into a list of its leaves.
The continuation monad is amazingly flexible; we can use it to
simulate some of the computations performed above. To see how, first
We have found a way of collapsing a tree into a list of its leaves.
The continuation monad is amazingly flexible; we can use it to
simulate some of the computations performed above. To see how, first
-note that an interestingly uninteresting thing happens if we use the
-continuation unit as our first argument to `treemonadizer`, and then
+note that an interestingly uninteresting thing happens if we use
+`continuation_unit` as our first argument to `tree_monadize`, and then
- : int tree =
Node (Node (Leaf 2, Leaf 3), Node (Leaf 5, Node (Leaf 7, Leaf 11)))
That is, nothing happens. But we can begin to substitute more
- : int tree =
Node (Node (Leaf 2, Leaf 3), Node (Leaf 5, Node (Leaf 7, Leaf 11)))
That is, nothing happens. But we can begin to substitute more
- : int tree =
Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
(* Simulating the int list tree list *)
- : int tree =
Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
(* Simulating the int list tree list *)
- : int list tree =
Node
(Node (Leaf [2; 4], Leaf [3; 9]),
Node (Leaf [5; 25], Node (Leaf [7; 49], Leaf [11; 121])))
(* Counting leaves *)
- : int list tree =
Node
(Node (Leaf [2; 4], Leaf [3; 9]),
Node (Leaf [5; 25], Node (Leaf [7; 49], Leaf [11; 121])))
(* Counting leaves *)
- : int = 5
We could simulate the tree state example too, but it would require
generalizing the type of the continuation monad to
- : int = 5
We could simulate the tree state example too, but it would require
generalizing the type of the continuation monad to
monad, the binary tree monad:
type 'a tree = Leaf of 'a | Node of ('a tree) * ('a tree);;
monad, the binary tree monad:
type 'a tree = Leaf of 'a | Node of ('a tree) * ('a tree);;
| Node (l, r) -> Node ((tree_bind l f), (tree_bind r f));;
For once, let's check the Monad laws. The left identity law is easy:
| Node (l, r) -> Node ((tree_bind l f), (tree_bind r f));;
For once, let's check the Monad laws. The left identity law is easy:
To check the other two laws, we need to make the following
observation: it is easy to prove based on `tree_bind` by a simple
induction on the structure of the first argument that the tree
resulting from `bind u f` is a tree with the same strucure as `u`,
To check the other two laws, we need to make the following
observation: it is easy to prove based on `tree_bind` by a simple
induction on the structure of the first argument that the tree
resulting from `bind u f` is a tree with the same strucure as `u`,
we'll give an example that will show how an inductive proof would
proceed. Let `f a = Node (Leaf a, Leaf a)`. Then
we'll give an example that will show how an inductive proof would
proceed. Let `f a = Node (Leaf a, Leaf a)`. Then
At this point, it should be easy to convince yourself that
using the recipe on the right hand side of the associative law will
At this point, it should be easy to convince yourself that
using the recipe on the right hand side of the associative law will