X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=code%2Ftree_monadize.ml;h=55b0f7b0080a76510a940ff1916281c929472b28;hp=7c419a342402ce77a965eb1c2810e4b332cdfcde;hb=f502dd05e49a51cfb4e6c2f339764d99d0511c82;hpb=c9a9cb39ae6852c63c43d80ed4ebb05aec6c1e05 diff --git a/code/tree_monadize.ml b/code/tree_monadize.ml index 7c419a34..55b0f7b0 100644 --- a/code/tree_monadize.ml +++ b/code/tree_monadize.ml @@ -115,7 +115,7 @@ let t1 = Node module Tree_monadizer(X : sig (* the module we're using as a parameter has to supply function values - * for unit and bind, as well as a monadic type constructor m *) + * for unit and bind, as well as a monadic type constructor *) type 'a monad val unit : 'a -> 'a monad val bind : 'a monad -> ('a -> 'b monad) -> 'b monad @@ -197,7 +197,7 @@ let env = fun i -> i + i in TreeReader.monadize int_readerize t1 env;; (* You can also avoid declaring a separate toplevel TreeReader module - * (or even a separate Reader_monad module) by ysing one of these forms: + * (or even a separate Reader_monad module) by using one of these forms: * ... * let module T = Tree_monadizer(Reader_monad) in * T.monadize int_readerize t1 env;; @@ -269,3 +269,27 @@ TreeCont.monadize (fun a k -> k [a; a*a]) t1 initial_continuation;; let initial_continuation = fun t -> 0 in TreeCont.monadize (fun a k -> 1 + k a) t1 initial_continuation;; +(* +(* Tree monad *) + +(* type 'a tree defined above *) +let tree_unit (a: 'a) : 'a tree = Leaf a;; +let rec tree_bind (u : 'a tree) (f : 'a -> 'b tree) : 'b tree = + match u with + | Leaf a -> f a + | Node (l, r) -> Node (tree_bind l f, tree_bind r f);; + +type ('a) treeT_reader = + 'a tree reader;; + +let unit (a: 'a) : 'a tree reader = + reader_unit (Leaf a);; + +let rec bind (u : 'a tree_reader) (f : 'a -> ('b, M) tree) : ('b, M) tree = + match u with + | Leaf a -> M.bind (f a) (fun b -> M.unit (Leaf b)) + | Node (l, r) -> M.bind (bind l f) (fun l' -> + M.bind (bind r f) (fun r' -> + M.unit (Node (l', r'));; + + *)