[MLton-user] Extended Basis Library: proposed patch to MONAD_EX and MkMonad

Geoffrey Alan Washburn geoffw at cis.upenn.edu
Thu Mar 15 06:50:33 PST 2007


    Here is a patch for the additions to MONAD_EX that we had agreed we 
were happy with.  Still, let me know if there are any suggestions for 
improvement.  Again, I am going to go ahead and commit these so I can 
more easily synchronize my multiple working copies.  It should have no 
affect on the correctness of any existing code.

Index: public/concept/monad.sig
===================================================================
--- public/concept/monad.sig    (revision 5429)
+++ public/concept/monad.sig    (working copy)
@@ -38,7 +38,21 @@
    val >>& : 'a monad_ex * 'b monad_ex -> ('a, 'b) Product.t monad_ex
    val >>* : 'a monad_ex * 'b monad_ex -> ('a * 'b) monad_ex
    val >>@ : ('a -> 'b) monad_ex * 'a monad_ex -> 'b monad_ex
+
    val seq : 'a monad_ex List.t -> 'a List.t monad_ex
+   val seqWith : ('a -> 'b monad_ex) -> 'a List.t -> 'b List.t monad_ex
+
+   val app : 'a monad_ex List.t -> unit monad_ex
+   val appWith : ('a -> 'b monad_ex) -> 'a List.t -> unit monad_ex
+
+   val ignore : 'a monad_ex -> unit monad_ex
+   (** {ignore m == (m >> return ())} *)
+
+   val when : bool -> unit monad_ex -> unit monad_ex
+   (** {when b m == if b then m else (return ())} *)
+
+   val unless : bool -> unit monad_ex -> unit monad_ex
+   (** {unless b m == if b then (return ()) else m} *)
 end
 
 signature MONAD = sig
Index: detail/concept/mk-monad.fun
===================================================================
--- detail/concept/mk-monad.fun (revision 5429)
+++ detail/concept/mk-monad.fun (working copy)
@@ -16,6 +16,32 @@
    fun aM >> bM = map #2 (aM >>* bM)
    fun seq [] = return []
      | seq (xM::xMs) = map op :: (xM >>* seq xMs)
+
+   local
+     fun seqWithTail f xs accum =
+         case xs
+           of [] => return (List.rev accum)
+            | x::xs' => (f x) >>= (fn x' => seqWithTail f xs' (x'::accum))
+   in
+     fun seqWith f xs =
+         seqWithTail f xs []
+   end
+
+   fun app (ms : 'a monad list) : unit monad =
+       case ms
+         of [] => return ()
+          | m::ms' => m >> (app ms')
+
+   fun appWith (f : 'a -> 'b monad) (xs : 'a list) : unit monad =
+       case xs
+         of [] => return ()
+          | x::xs' => (f x) >> (appWith f xs')
+
+   fun ignore m = m >> return ()
+
+   fun when b m = if b then m else return ()
+   fun unless b m = if b then return () else m
+
 end
 
 functor MkMonadP (MonadPCore : MONADP_CORE) : MONADP = struct


-- 
[Geoff Washburn|geoffw at cis.upenn.edu|http://www.cis.upenn.edu/~geoffw/]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mlton.org/pipermail/mlton-user/attachments/20070315/92e91425/attachment.htm


More information about the MLton-user mailing list