In Haskell/Understanding monads/State there is a snippet code:
type GeneratorState = State StdGen
rollDie :: GeneratorState Int
rollDie = do generator <- get
let (value, newGenerator) = randomR (1,6) generator
put newGenerator
return value
About the Symbol <-
in the above third line, there is an explanation:
we take out the pseudo-random generator with <-
in conjunction with get. get overwrites the monadic value (The a
in m a
) with the state, binding the generator to the state. (If in doubt, recall the definition of get and >>=
above).
I do not understand: (1) generator
is corresponding to the first type parameter of the definition State
? (2) why generator
is just one of the two parameters of State
, not two? Of course, from the context, the answer is obvious, but I do not know the concrete rules about <-
.
To my knowledge, when evaluating evalState rollDie (mkStdGen 600)
, get
will be replaced by State (mkStdGen 0) (mkStdGen 0)
, and, according to RWH's description "<-
pulls things out of monads", things here are not (mkStdGen 0) (mkStdGen 0)
?