[Haskell-beginners] Re: How to create a monad instance

Maciej Piechotka uzytkownik2 at gmail.com
Fri Dec 11 20:38:13 EST 2009


On Sat, 2009-12-12 at 01:17 +0100, Marco De Oliveira wrote:
> Hi,
> 
> Is it possible to create an instance of Monad for IOException?
> Each time a try I stay blocked by IO monad.
> 
> with the definition:
> 
> data IOException a = IOExceptionCons (IO (Exception a))
> 
> data Exception a = SuccessCons (Maybe Warning) a
>                           | ErrorCons Error
> 
> data Warning = WarningCons1
>                     | WarningCons2
> 
> data Error = ErrorCons1
>                 | ErrorCons2
> 
> Regards

Yes - to begin with:

newtype IOException a = IOException {runIOException :: IO (Exception a)}

data Exception a = Success (Maybe Warning) a
                 | Exception Error

data Warning = Warning1
             | Warning2
data Error = Error1
           | Error2

instance Monad Exception where
    return = Success Nothing
    (Success w v) >>= f = f v
--    (Success w v) >>= f = case f v of
--                            Success _ v' -> Success w v'
--                            Exception e  -> Exception e
    (Exception e) >>= _ = Exception e
instance Monad IOException where
    return = IOException . return . return
    m >>= f = IOException $ runIOException m >>= runIOException . f

However:
1. What should be the combination of warnings I'd rather threat them as
some 'proper' monoid (for ex. List instead of Maybe)
2. It is much easier to use mtl:
type IOException = ErrorT (Either Error) (WriterT [Warning] IO)

Regards




More information about the Beginners mailing list