Re: Writing for both State and StateT

Mark Carroll wrote:
On Tue, 22 Apr 2003, Jon Cast wrote:
When you have two identical methods like this, you can try removing the type from one and seeing what hugs or ghci thinks the type is. Then, you can specialize that if you want.
Unfortunately, in this case, with that function in isolation I get,
Ambiguous type variable(s) `s', `m' in the constraint `MonadState s m' arising from use of `get' ....
Ah, yes the good old monomorphism restriction. Should have thought about that. The way around this is to make the declaration a function:
next_random_probability () = do old_rng <- get let (random_probability, new_rng) = randomR (0, 1) old_rng put new_rng return random_probability
This will typecheck, giving most general type (MonadState s m, Num a, Random a, RandomGen s) => () -> m a, according to ghci. Then, you can take off the argument type to get the resulting type. <snip> Jon Cast

On Tue, 22 Apr 2003, Jon Cast wrote: (snip)
Ah, yes the good old monomorphism restriction. Should have thought about that. The way around this is to make the declaration a function:
next_random_probability () = (snip)
Wow, bizarre. Thanks! (And also to Alastair and Derek.) I didn't realise that my example uses multiparameter typeclasses - maybe that's why I've had difficulty using the state monads, because I hadn't studied multiparameter typeclasses, I'd just recognised that something new-to-me was going on. -- Mark
participants (2)
-
Jon Cast
-
Mark Carroll