Newbie on instance of Monad

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) instance Monad (RandomMonad g) where return = RandomMonad . const RandomMonad f1 >>= f2 = RandomMonad f3 where f3 a = f2f1 a (next a) RandomMonad f2f1 = f2 . f1 ------ I get this error message: Could not deduce (RandomGen g) from the context (Monad (RandomMonad g)) arising from a use of `RandomMonad' at src/encherDB.hs:10:11-21 Possible fix: add (RandomGen g) to the context of the type signature for `return' In the first argument of `(.)', namely `RandomMonad' In the expression: RandomMonad . const In the definition of `return': return = RandomMonad . const but I'm not smart enough to understand what it means. Thanks a lot, MaurĂcio

On Fri, 2008-10-31 at 18:43 -0200, 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)
instance Monad (RandomMonad g) where return = RandomMonad . const RandomMonad f1 >>= f2 = RandomMonad f3 where f3 a = f2f1 a (next a) RandomMonad f2f1 = f2 . f1 ------
I get this error message:
Could not deduce (RandomGen g) from the context (Monad (RandomMonad g)) arising from a use of `RandomMonad' at src/encherDB.hs:10:11-21 Possible fix: add (RandomGen g) to the context of the type signature for `return' In the first argument of `(.)', namely `RandomMonad' In the expression: RandomMonad . const In the definition of `return': return = RandomMonad . const
but I'm not smart enough to understand what it means.
Yikes. Just search the archives for `Set not a Monad'; you have the same issue. Hint: data RandomGen g => RandomMonad g a means nothing at all like what you think it means. jcc

On Fri, 31 Oct 2008, Jonathan Cast wrote:
On Fri, 2008-10-31 at 18:43 -0200, 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)
instance Monad (RandomMonad g) where return = RandomMonad . const RandomMonad f1 >>= f2 = RandomMonad f3 where f3 a = f2f1 a (next a) RandomMonad f2f1 = f2 . f1
Yikes. Just search the archives for `Set not a Monad'; you have the same issue.
I think that this would be the answer if he is after a constraint for 'a', but he wants to constraint 'g'.
Hint: data RandomGen g => RandomMonad g a means nothing at all like what you think it means.

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.

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'.
you need to make (RandomGen g) a constraint of the instance:
instance RandomGen g => Monad (RandomMonad g) where
Nice. I wasn't so wrong as I though :) I didn't understand why can't the compiler deduce that g is always going to be of type RandomGen. Even if I use 'undefined', it has to be a RandomGen undefined.
Btw. RandomMonad looks like Control.Monad.Reader.
You are right, I'll try that instead so I learn a little bit more. But I can't understand this syntax: class (Monad m) => MonadReader r m | m -> r where What is the '|' doing there? I'll post that in a new thread since it's a different question. Thanks, MaurĂcio
participants (3)
-
Henning Thielemann
-
Jonathan Cast
-
Mauricio