[Haskell-cafe] partial functions / failure,
Maybe and MonadError and good style
Stefan O'Rear
stefanor at cox.net
Fri Dec 22 20:21:04 EST 2006
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)
More information about the Haskell-Cafe
mailing list