[Haskell-cafe] Combining computations

Tillmann Rendel rendel at cs.au.dk
Sun May 3 18:02:09 EDT 2009


Claus Reinke wrote:
>>  mplus' :: MonadPlus m => Maybe a -> m a -> m a
>>  mplus' m l = maybeToMonad m `mplus` l
>>
>>  maybeToMonad :: Monad m => Maybe a -> m a
>>  maybeToMonad = maybe (fail "Nothing") return
>>
>> In general, however, this operation can't be done.  For example,
>> how would you write:
>>
>>  mplus' :: IO a -> [a] -> [a]
> 
> Perhaps the question should be: is there an interesting structure
> that would allow us to capture when this kind of merging Monads
> is possible? 

For me, it seems that Foldable is the other side of Alternative. A 
functor F supports Alternative if (F a) supports a monoidal structure 
for the construction of values, and it supports Foldable if (F a) 
supports a monoidal structure for the decomposition of values. That 
means that we can give a translation from every Foldable functor to 
every Alternative functor as follows:

   foldable2alternative = foldr (<|>) empty . fmap pure

Tillmann


More information about the Haskell-Cafe mailing list