[Haskell-cafe] Newbie on instance of Monad

Henning Thielemann lemming at henning-thielemann.de
Fri Oct 31 16:49:59 EDT 2008


On Fri, 31 Oct 2008, Mauricio wrote:

> Hi,
>
> After a lot of thinking, I can't get what I
> am doing wrong in this code:
>
> ------
> data ( RandomGen g ) => RandomMonad g a = RandomMonad (g -> a)

RandomGen g is considered the constraint for the application of 
RandomMonad constructor, but GHC does not conclude that every value of 
(RandomMonad g a) fulfills this constraint. Actually, 'undefined' is 
available for any 'g'.

> instance Monad (RandomMonad g) where
>  return = RandomMonad . const
>  RandomMonad f1 >>= f2 = RandomMonad f3 where
>    f3 a = f2f1 a (next a)
>    RandomMonad f2f1 = f2 . f1

you need to make (RandomGen g) a constraint of the instance:

instance RandomGen g => Monad (RandomMonad g) where


Btw. RandomMonad looks like Control.Monad.Reader.


More information about the Haskell-Cafe mailing list