<br><font size=2 face="sans-serif">Hello,</font>
<br>
<br><font size=2 face="sans-serif"> 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>
> Hey,<br>
> <br>
> I have a structure containing Xs in various places, like so<br>
> <br>
> data X<br>
> data Structure = Structure .. [X] .. [X] ..<br>
> <br>
> And I defined mapStructure <br>
> <br>
> mapStructure :: (X -> X) -> (Structure -> Structure)<br>
> <br>
> I then wanted to use mapStructure to define queries as well as<br>
> transformations on structures. I generalized mapStructure to<br>
> mapStructureM:<br>
> <br>
> mapStructure :: Monad m => (X -> m X) -> (Structure ->
m Structure)<br>
> <br>
> and then defined the following search monad:<br>
> <br>
> > data Search f a b = Found (f a) b<br>
> > <br>
> > class Monad (m a) => SearchMonad m a where<br>
> > found :: a -> m a a<br>
> > <br>
> > fromSearch :: Search f a b -> f a<br>
> > fromSearch (Found a _) = a<br>
> > <br>
> > search :: (SearchMonad m a) => (a -> Bool) -> a ->
m a a<br>
> > search f a<br>
> > | f a = found a<br>
> > | otherwise = return a<br>
> <br>
> Instances of the monad for finding one and for finding all elements:<br>
> <br>
> > instance SearchMonad (Search Maybe) a where<br>
> > found a = Found (Just a) a<br>
> > <br>
> > instance SearchMonad (Search []) a where<br>
> > found a = Found [a] a<br>
> > <br>
> > instance Monad (Search Maybe a) where<br>
> > return b = Found Nothing b<br>
> > Found (Just a) a' >>= f = case f a' of<br>
> > Found _ b -> Found (Just a) b<br>
> > Found Nothing a' >>= f = f a'<br>
> > <br>
> > instance Monad (Search [] a) where<br>
> > return b = Found [] b<br>
> > Found as a' >>= f = case f a' of<br>
> > Found as' b -> Found (as ++ as')
b<br>
> <br>
> Here is a simple sample session with ghci<br>
> <br>
> *Util> fromSearch $ mapM (search even) [1,3,5] :: Maybe Int<br>
> Nothing<br>
> *Util> fromSearch $ mapM (search even) [1,2,3,4,5] :: Maybe Int<br>
> Just 2<br>
> *Util> fromSearch $ mapM (search even) [1,2,3,4,5] :: [Int]<br>
> [2,4]<br>
> <br>
> What I'm wondering about is if this monad is an instance of a more
<br>
> general monad(if so, which one?), and generally if people have any
<br>
> comments about these definitions.<br>
> <br>
> Thanks!<br>
> <br>
> Edsko <br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> Haskell-Cafe@haskell.org<br>
> 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>