[Haskell-cafe] state and exception or types again...

tpledger at ihug.co.nz tpledger at ihug.co.nz
Mon Aug 28 23:32:09 EDT 2006

Andrea Rossato wrote:
> Now I'm trying to create a statefull evaluator, with
output and
> exception, but I'm facing a problem I seem not to be able
> conceptually solve.

If a computation fails in your monad, do you still want to
return a value of the result type?  I'd expect not, and
hence remove the 'a' from the 'Raise' constructor.

    data Eval_SOI a
        = Raise (State ->    (State, Output))
        | SOIE  (State -> (a, State, Output))

The above is very similar to using the monad foundation

    import Control.Monad.Error
    import Control.Monad.State hiding (State)
    import Control.Monad.Writer

    type Eval_SOI
        = ErrorT String
              (StateT State (Writer Output))

...assuming that you're happy to send back a descriptive
String when a computation fails.

Have a look at the Control.Monad.Error source code, to see
how 'instance Error a => Monad (Either a)' is defined.  It's
the sort of thing you were trying to do in your 'instance
Monad Eval_SOI'.


More information about the Haskell-Cafe mailing list