<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Thanks for all the help, everyone.<br><br>I think this stuff is starting to come together.<br><br>Michael<br><br>--- On <b>Sun, 5/3/09, Tillmann Rendel <i>&lt;rendel@cs.au.dk&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Tillmann Rendel &lt;rendel@cs.au.dk&gt;<br>Subject: Re: [Haskell-cafe] Combining computations<br>To: "michael rice" &lt;nowgate@yahoo.com&gt;<br>Cc: haskell-cafe@haskell.org<br>Date: Sunday, May 3, 2009, 7:33 AM<br><br><div class="plainMail">Hi,<br><br>normally, one uses monads to express and combine computations in the same monad. However, you can convert between some monads, e.g. from Maybe to List:<br><br>&nbsp; import Data.Maybe (maybeToList)<br><br>&nbsp; &gt; let m1 = Nothing<br>&nbsp; &gt; let m2 = [1]<br>&nbsp; &gt; let m3 = maybeToList m1 `mplus`
 m2<br><br>&nbsp; &gt; let m1 = Just 1<br>&nbsp; &gt; let m2 = []<br>&nbsp; &gt; let m3 = maybeToList m1 `mplus` m2<br><br><br>In fact, you can convert from Maybe to any MonadPlus.<br><br>&nbsp; maybeToMonadPlus Nothing = mzero<br>&nbsp; maybeToMonadPlus (Just x) = return x<br><br>And you can convert from List to any MonadPlus:<br><br>&nbsp; listToMonadPlus Nothing&nbsp; = []<br>&nbsp; listToMonadPlus (x : xs) = return x `mplus` listToMonadPlus xs<br><br>Now you should be able to do:<br><br>&nbsp; m1 = maybeToMonadPlus (Just 1)<br>&nbsp; m2 = listtoMonadPlus [2, 3]<br>&nbsp; m3 = m1 `mplus` m2 :: Just Int -- Just 1<br>&nbsp; m4 = m1 `mplus` m2 :: [Int]&nbsp; &nbsp; -- [1, 2, 3]<br><br>The reason this is possible is that Maybe and List do not support additional effects beyond what is common to all MonadPlus instances.<br><br><br>Another option is to never specify which monad your computations are in in the first place. Instead, only specify which
 computational effects the monad should support.<br><br>&nbsp; m1 = mzero&nbsp; &nbsp; :: MonadPlus m =&gt; m a<br>&nbsp; m2 = return 1 :: (Monad m, Num a) =&gt; m a<br>&nbsp; m3 = m1 `mplus` m2 `mplus` Just 2 -- Just 1<br>&nbsp; m4 = m1 `mplus` m2 `mplus` [2, 3] -- [1, 2, 3]<br><br>In this version, m1 and m2 are polymorphic computations, which can be used together with List computations, Maybe computations, or any other MonadPlus instances. m1 needs MonadPlus, while m2 is happy with any Monad instance. This fact is encoded in their type.<br><br>&nbsp; Tillmann<br></div></blockquote></td></tr></table><br>