[Haskell-cafe] Monad Stack - State + Rand?

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Sat Jun 18 04:31:49 UTC 2016


On 18 June 2016 at 14:22, Christopher Howard <ch.howard at zoho.com> wrote:
> Hi. I'm working through "Haskell Design Patterns" and got inspired to
> try to create my first monad stack. What I really wanted though (not
> shown in the book) was to combine State and Rand. I daresay I got
> something to compile:
>
> walk :: RandomGen g => StateT (Float, Float) (Rand g) (Float, Float)
> walk = do (x, y) <- get
>           put (x + 1, y + 1)
>           get >>= return

Can you provide more details?  At the minimum, what imports/packages
did you use (strict or lazy State?  Which MonadRand did you use?).

Note also that "get >>= return" is equivalent to just "get"; you could
even combine the first two lines using the "modify" function.

> However, the moment I try to insert a getRandomR or something in it, I
> get an error
>
> Could not deduce (MonadRandom (StateT (Float, Float) (Rand g)))
>       arising from a use of `getRandomR' <...snip...>
> add an instance declaration for
>       (MonadRandom (StateT (Float, Float) (Rand g)))
>
> I see there are instances
>
> MonadRandom m => MonadRandom (StateT s m)
> RandomGen g => MonadRandom (Rand g)
>
> in Control.Monad.Random.Class, so I am not quite sure what is expected
> of me.

Is this the MonadRandom package?  If so, I don't see the StateT instance there.


-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
http://IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list