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

Steve Downey sdowney at gmail.com
Fri Dec 22 20:37:05 EST 2006


OK, but is msg always a string? Admidtedly, in the concrete case I
have at hand (follow up posting RSN), that would be fine.
I think I've also been looking at the map lookup case, where not only
is lookup failure to be expected, it's almost an imposition to say
something other than Nothing.

On 12/22/06, Stefan O'Rear <stefanor at cox.net> 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)
>


More information about the Haskell-Cafe mailing list