<br><font size=2 face="sans-serif">Hello,</font>
<br>
<br><font size=2 face="sans-serif">&nbsp; You might want to look at the
scrap your boilerplate papers and/or their implementation in GHC in Data.Generics.</font>
<br>
<br><font size=2 face="sans-serif">-Jeff</font>
<br>
<br><tt><font size=2>haskell-cafe-bounces@haskell.org wrote on 03/19/2007
01:11:19 PM:<br>
<br>
&gt; Hey,<br>
&gt; <br>
&gt; I have a structure containing Xs in various places, like so<br>
&gt; <br>
&gt; data X<br>
&gt; data Structure = Structure .. [X] .. [X] ..<br>
&gt; <br>
&gt; And I defined mapStructure <br>
&gt; <br>
&gt; mapStructure :: (X -&gt; X) -&gt; (Structure -&gt; Structure)<br>
&gt; <br>
&gt; I then wanted to use mapStructure to define queries as well as<br>
&gt; transformations on structures. I generalized mapStructure to<br>
&gt; mapStructureM:<br>
&gt; <br>
&gt; mapStructure :: Monad m =&gt; (X -&gt; m X) -&gt; (Structure -&gt;
m Structure)<br>
&gt; <br>
&gt; and then defined the following search monad:<br>
&gt; <br>
&gt; &gt; data Search f a b = Found (f a) b<br>
&gt; &gt; <br>
&gt; &gt; class Monad (m a) =&gt; SearchMonad m a where<br>
&gt; &gt; &nbsp; &nbsp; found :: a -&gt; m a a<br>
&gt; &gt; <br>
&gt; &gt; fromSearch :: Search f a b -&gt; f a<br>
&gt; &gt; fromSearch (Found a _) = a<br>
&gt; &gt; <br>
&gt; &gt; search :: (SearchMonad m a) =&gt; (a -&gt; Bool) -&gt; a -&gt;
m a a<br>
&gt; &gt; search f a<br>
&gt; &gt; &nbsp; &nbsp; | f a = found a<br>
&gt; &gt; &nbsp; &nbsp; | otherwise = return a<br>
&gt; <br>
&gt; Instances of the monad for finding one and for finding all elements:<br>
&gt; <br>
&gt; &gt; instance SearchMonad (Search Maybe) a where<br>
&gt; &gt; &nbsp; &nbsp; found a = Found (Just a) a<br>
&gt; &gt; <br>
&gt; &gt; instance SearchMonad (Search []) a where<br>
&gt; &gt; &nbsp; &nbsp; found a = Found [a] a<br>
&gt; &gt; <br>
&gt; &gt; instance Monad (Search Maybe a) where<br>
&gt; &gt; &nbsp; &nbsp; return b = Found Nothing b<br>
&gt; &gt; &nbsp; &nbsp; Found (Just a) a' &gt;&gt;= f = case f a' of<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; Found _ b -&gt; Found (Just a) b<br>
&gt; &gt; &nbsp; &nbsp; Found Nothing a' &gt;&gt;= f = f a'<br>
&gt; &gt; <br>
&gt; &gt; instance Monad (Search [] a) where<br>
&gt; &gt; &nbsp; &nbsp; return b = Found [] b<br>
&gt; &gt; &nbsp; &nbsp; Found as a' &gt;&gt;= f = case f a' of<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; Found as' b -&gt; Found (as ++ as')
b<br>
&gt; <br>
&gt; Here is a simple sample session with ghci<br>
&gt; <br>
&gt; *Util&gt; fromSearch $ mapM (search even) [1,3,5] :: Maybe Int<br>
&gt; Nothing<br>
&gt; *Util&gt; fromSearch $ mapM (search even) [1,2,3,4,5] :: Maybe Int<br>
&gt; Just 2<br>
&gt; *Util&gt; fromSearch $ mapM (search even) [1,2,3,4,5] :: [Int]<br>
&gt; [2,4]<br>
&gt; <br>
&gt; What I'm wondering about is if this monad is an instance of a more
<br>
&gt; general monad(if so, which one?), and generally if people have any
<br>
&gt; comments about these definitions.<br>
&gt; <br>
&gt; Thanks!<br>
&gt; <br>
&gt; Edsko <br>
&gt; _______________________________________________<br>
&gt; Haskell-Cafe mailing list<br>
&gt; Haskell-Cafe@haskell.org<br>
&gt; http://www.haskell.org/mailman/listinfo/haskell-cafe<br>
</font></tt>
<br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">---</span><br>
<br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">This e-mail may contain confidential and/or privileged information. If you </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">are not the intended recipient (or have received this e-mail in error) </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">please notify the sender immediately and destroy this e-mail. Any </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">unauthorized copying, disclosure or distribution of the material in this </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">e-mail is strictly forbidden.</span><br>