- | Leaf i -> Leaf (newleaf i)
- | 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)))
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
- | Node (l, r) -> reader_bind (treemonadizer f l) (fun x ->
- reader_bind (treemonadizer f r) (fun y ->
+ | 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.
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);;
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:
- | Node (l, r) -> state_bind_and_count (treemonadizer f l) (fun x ->
- state_bind_and_count (treemonadizer f r) (fun y ->
+ | 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)
- 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 =
- | Node (l, r) -> continuation_bind (treemonadizer f l) (fun x ->
- continuation_bind (treemonadizer f r) (fun y ->
+ | 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 *)
resulting from `bind u f` is a tree with the same strucure as `u`,
except that each leaf `a` has been replaced with `f a`:
resulting from `bind u f` is a tree with the same strucure as `u`,
except that each leaf `a` has been replaced with `f a`:
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