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

Stefan O'Rear stefanor at cox.net
Fri Dec 22 21:52:29 EST 2006


On Fri, Dec 22, 2006 at 09:44:35PM -0500, Steve Downey wrote:
> I suppose this is why Maybe is a monad rather than just another
> algebraic data type.

Maybe is an algebraic datatype.  A monad is simply a structure defined on
the datatype, reflecting a normal pattern of use.

> This is exactly where I'm stuck. I've an eval1 function of ArithExpr
> -> m ArithExpr, where m started out as Maybe. Now only the calling
> eval function mentions Maybe.  And I was wondering if I could keep
> eval1 generic.

That should be easy - anything you can do in Maybe or Either can be
done in an arbitrary MonadError.

f :: (MonadError e m, Error e) => Maybe a -> m a
f Nothing = throwError noMsg
f (Just x) = return x

g :: (MonadError e m, Error e) => Either String a -> m a
g (Left e) = throwError $ strMsg e
g (Right x) = return x

h :: (MonadError e m) => Either e a -> m a
h (Left e) = throwError e
h (Right x) = return x

> OTOH, eval1 should really pass through information from the underlying 
> parser...

If you have a type of errors, pattern 'h' above should be applicable?


More information about the Haskell-Cafe mailing list