On Wed, May 21, 2008 at 8:42 AM, Dmitri O.Kondratiev <dokondr@gmail.com> wrote:
So let's start with fundamental and most intriguing (to me) things:First line above declares a data type:
getAny :: (Random a) => State StdGen a
getAny = do g <- get -- magically get the current StdGen
State StdGen a
which is constructed with the function:
State {runState :: (StdGen -> (a, StdGen))}
Q1: Where in the example (http://www.haskell.org/all_about_monads/examples/example15.hs) data of this type *actually gets constructed* ?
Looking at example15.hs code we see the following sequence:
1) makeRandomValue g -- where g is a StdGen instance, ok
2) makeRandomValue g ~> expands into ~>
~> (runState (do { ...; b <- getAny;...})) g
This last expression puzzles me. I can understand, for example, this:
State StdGen a :: aState
StdGen:: g1
(v, g2) = (runStae aState) g1 -- this returns a state function which is then passed a generator g1, and as result returns pair (value, new generaor)
But '(runState (do ...)) g' implies that expression (do ...) must be somehow of type 'State StdGen a' ?
Yet, when we call 'makeRandomValue g' we just pass to this function g::StgGen
So, my next question:
Q2: How (do {...;b <- getAny;...}) becomes an *instance* of type 'State StdGen a' ?