[Haskell-cafe] partial functions / failure, Maybe and MonadError and good style

Lennart Augustsson lennart at augustsson.net
Fri Dec 22 20:33:51 EST 2006


I would not advocate using the fail operation in a monad.  It doesn't  
belong there, and hopefully it will go away some day. :)

	-- Lennart

On Dec 23, 2006, at 02:21 , Stefan O'Rear wrote:

> On Fri, Dec 22, 2006 at 08:05:08PM -0500, Steve Downey wrote:
>> Although terse, the subject really says it all.
>> If i've a partial function, like a parser, what is considered good
>> style for a library. The tradeoffs that I can see are that Maybe is a
>> binary operation, while Error can communicate more information in the
>> type  of the error case.
>> Is there some way to defer the error handling Monad to the calling  
>> context?
>
> Your title answers the question. :)
>
> All monads provide a "fail" operation, and any function that uses  
> fail will
> be able to work with any monad's error handling mechanism.
>
> * Maybe      - fail msg is Nothing (ignores the message)
> * Either str - fail msg is Left msg (stores the message)
> * IO         - fail msg is ioError (userError msg) (throws message  
> as exception)
>
> For instance, Data.Map.minView is Monad m => Set a -> m (Set a, a);  
> so..
>
> minView :: Set a -> Maybe (Set a, a)   -- gives Nothing on empty set
> minView :: Set a -> [(Set a, a)]       -- gives [] on empty set
> minView :: Set a -> IO (Set a, a)      -- throws an ioError on  
> empty set
> ...
>
> (note that the behaivor of lookup et al, which are specific to  
> Maybe, is considered
> outdated style)
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list