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

Maciej Piechotka uzytkownik2 at gmail.com
Sun Dec 13 04:50:22 EST 2009


> Thanks for your help.
> 
> I have try your code ghci and I think there is a mistake in the
> definition of (>>=) operator.
> 

Hmm. I belive I tested code before (at least type-check) sending but it
is possible.

> test1 m f = IOException $ runIOException m >>= runIOException . f
> :t test1
> test1 :: IOException a
>          -> (Exception a -> IOException a)
>          -> IOException a
> 
> I think the defintion is:
> test :: IOException a -> ( a -> IOException a) -> IOException a
> 

Ups. You're right. Quick'n'dirty version with do:
instance Monad IOException where
   return = IOException . return . return
   m >>= f = IOException $ do m' <- runIOException m
                              case m' of
                                Success _ v -> runIOException $ f v
                                Exception e -> return $ Exception e

> 1. why I am not using monoid? I try to bind C code and the function
> return only one warning. But if I change i little the structure of
> IOException, it may have sense to use a List.
> newtype IOException a = IOException {runIOException :: ([Warning] ->
> IO (Exception a))}
> 

Well. The problem with returning one - what should be combination of
warnings. It is of course problem-dependent.

Regards




More information about the Beginners mailing list