X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=code%2Ftree_monadize.ml;h=7c419a342402ce77a965eb1c2810e4b332cdfcde;hp=c751b36e73426177958b2bfeccd3920eb703073a;hb=ff95f5a38d61a6fa0b9c5e4da4253a6a3266a7dc;hpb=6bdd5bdd6e49ead61951cecff822c161f28098ca diff --git a/code/tree_monadize.ml b/code/tree_monadize.ml index c751b36e..7c419a34 100644 --- a/code/tree_monadize.ml +++ b/code/tree_monadize.ml @@ -184,7 +184,7 @@ module TreeCont = Tree_monadizer2(Continuation_monad);; *) -let int_readerize : int -> int reader = +let int_readerize : int -> int Reader_monad.monad = fun (a : int) -> fun (env : int -> int) -> env a;; (* int_readerize takes an int and returns a Reader monad that @@ -196,13 +196,43 @@ let int_readerize : int -> int reader = 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: + * ... + * let module T = Tree_monadizer(Reader_monad) in + * T.monadize int_readerize t1 env;; + * or: + * ... + * let env = fun i -> i + i in + * let module Monad = struct + * type env = int -> int;; + * type 'a monad = env -> 'a;; + * let unit a : 'a monad = fun e -> a;; + * let bind (u : 'a monad) (f : 'a -> 'b monad) : 'b monad = + * fun e -> f (u e) e;; + * end in + * let module T = Tree_monadizer(Monad) in + * T.monadize int_readerize t1 env;; + * or: + * ... + * let module T = Tree_monadizer(struct + * type env = int -> int;; + * type 'a monad = env -> 'a;; + * let unit a : 'a monad = fun e -> a;; + * let bind (u : 'a monad) (f : 'a -> 'b monad) : 'b monad = + * fun e -> f (u e) e;; + * end) in + * T.monadize int_readerize t1 env;; + *) + + (* square each leaf *) let env = fun i -> i * i in TreeReader.monadize int_readerize t1 env;; -let incrementer : int -> int state = +let incrementer : int -> int State_monad.monad = fun (a : int) -> fun s -> (a, s+1);; (* incrementer takes an 'a and returns it wrapped in a @@ -221,7 +251,7 @@ TreeList.monadize (fun i -> [ [i;i*i] ]) t1;; (* do nothing *) let initial_continuation = fun t -> t in -TreeCont.monadize cont_unit t1 initial_continuation;; +TreeCont.monadize Continuation_monad.unit t1 initial_continuation;; (* convert tree to list of leaves *) let initial_continuation = fun t -> [] in @@ -239,4 +269,3 @@ 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;; -