[Haskell-cafe] Control.Monad.Error with a custom error type

Tom Pledger tpledger at ihug.co.nz
Sat Aug 14 23:30:36 EDT 2004


Brian Smith wrote:

> [...]
>
> > instance MonadError (Either ReferenceError)
>
>  Kind error: `Either ReferenceError' is not applied to enough type 
> arguments
>  When checking kinds in `MonadError (Either ReferenceError)'
>  In the instance declaration for `MonadError (Either ReferenceError)' 


MonadError takes two parameters: the error type (with kind *) and the 
monad (with kind *->*).  So:

    instance MonadError ReferenceError (Either ReferenceError)

Unfortunately this takes you into the realm of Overlapping Instances.  I 
recommend that you instead extend ReferenceError so that it /can/ be 
made an instance of Error:

    data ReferenceError = {- what you already had -} | NonRefError String

...and bear in mind that NonRefError will only occur when the fail 
method or the mzero method is used, which won't happen much if you 
consistently use throwError instead.  It sometimes helps to define a 
function which catches one sort of error and lets others through:

    -- untested
    m `onRefError` h
        = m `catchError` \e ->
          case e of
              RefError{} -> h (expectedType e) (pointOfError e)
              _          -> throwError e

HTH.
Tom




More information about the Haskell-Cafe mailing list