[Haskell-cafe] partial functions / failure,
Maybe and MonadError and good style
Stefan O'Rear
stefanor at cox.net
Fri Dec 22 21:01:46 EST 2006
On Sat, Dec 23, 2006 at 02:33:51AM +0100, Lennart Augustsson wrote:
> I would not advocate using the fail operation in a monad. It doesn't
> belong there, and hopefully it will go away some day. :)
On Fri, Dec 22, 2006 at 08:37:05PM -0500, Steve Downey wrote:
> 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.
There is a more general form of monads that support failure,
Control.Monad.Error.MonadError in the extended standard libraries;
throwError noMsg :: (Error e, MonadError e m) => m a
throwError . strMsg :: (Error e, MonadError e m) => String -> m a
Error is any type that strings can be mapped into (I'm not talking about
an injection, I just can't find a better word), such as String and IOError.
Unfortunately there does not seem to be a standard instance for Maybe...
-- This isn't in the standard library, and thus I conjecture that it
-- is seriously flawed in a way I haven't noticed.
instance Error () where noMsg = () ; strMsg _ = ()
instance MonadError () Maybe where
throwError _ = Nothing
Just x `catchError` f = Just x
Nothing `catchError` f = f ()
More information about the Haskell-Cafe
mailing list