[Haskell-cafe] maybeToM

Bjorn Bringert bringert at cs.chalmers.se
Tue Dec 19 05:41:50 EST 2006


On 18 dec 2006, at 18.22, Stefan O'Rear wrote:

>>> I can't see how such a generalization could look like, especially  
>>> since
>>> "maybe" can be used with arbitrary monad:
>>>     maybe (fail "Nothing") return
>>
>> Well, that???s a possible implementation of a maybeToM. The  
>> question is:
>> Is it useful enough for a name on it???s own?
>
> I thought it was useful enough in genericserialize:
>
> module Data.Generics.Serialization.Standard ...
>
> -- |Convert a 'Maybe' object into any monad, using the imbedding  
> defined by
> -- fail and return.
> fromMaybeM :: Monad m => String -> Maybe a -> m a
> fromMaybeM st = maybe (fail st) return

I agree that this is useful. Several of my applications and libraries  
include this somewhere (with different names: maybeM and maybeToM).  
Though the RHS isn't much shorter than the LHS, unless its name is  
made shorter.

/Björn


More information about the Haskell-Cafe mailing list