How does initial state gets picked up in State Monad? - haskell

I was reading LYAHFG and I can't get around understanding state monad.
In the book, state monad is defined as
newtype State s a = State { runState :: s -> (a,s) }
instance Monad (State s) where
return x = State $ \s -> (x,s)
(State h) >>= f = State $ \s -> let (a, newState) = h s
(State g) = f a
in g newState
pop :: State Stack Int
pop = State $ \(x:xs) -> (x,xs)
push :: Int -> State Stack ()
push a = State $ \xs -> ((),a:xs)
stackManip :: State Stack Int
stackManip = do
push 3
a <- pop
pop
runState stackManip [5,8,2,1]
The book explains this by using a stack, where stack is the state and an element is the result.
My question particularty is how does runState stackManip [5,8,2,1] work?
runState takes one argument, that's fine but stackManip doesn't take any argument. How does initial state [5,8,2,1] gets picked up?

runState takes two arguments actually. When you declare a record
newtype State s a = State { runState :: s -> (a, s) }
This defines the function runState with the following signature:
runState :: State s a -> s -> (a, s)
runState (State run) = run
You get the s -> (a, s) type alone when manipulating records explicitly (pattern-matching, construction, update):
pop = State { runState = \(x : s) -> (x, s) }

A value of type State is just a (wrapped) function. The Monad instance for State "composes" those functions, rather than actually working directly with state. runState simply "unwraps" the function, giving you something you can directly apply to an initial state.
Another way of looking at it is that runState is an application operator, like ($). It applies a State (i.e., a state transformer) to an initial state.
Let's start with evalState, though, which just produces the result, not the new state.
head $ [1,2,3] -- 1
pop `evalState` [1,2,3] -- 1
runState just gives you a tuple consisting of the result and the new state.
pop `runState` [1,2,3] -- (1,[2,3])

Related

Understanding the state argument in the State Monad

I'm trying so hard to wrap my head around the State Monad, and I do not understand the following:
Given the implementation of return and (>>=), when you say State $ \s ->...., where does s come from? I mean, when you start performing >>= ... >>=, doesn't it mean that somewhere in your beginning of the chain you somehow have to provide for that initial parameter?
newtype State s a=State { runState::s->(a,s) }
instance Monad (State s) where
return a=State $ \s->(a,s)
(>>=) m g=State $ \s -> let (a,s')= runState m s in
runState (g a) s'
In (>>=) you say State $ \s -> runState m s, and I do not get when is that initial (\s -> ...) argument (with a REAL argument) called?
Can someone explain, please?
Later Edit:
Can someone show me how would the initial state be set, let's say if it needs to get a value using getLine?
main::IO()
main=do
argument<-getLine
--how do i set initial state with argument?
m >> f1 >> f2 >> f3
when you say State $ \s ->...., where does s come from ?
It will come from the invocation, when runState will supply the initial state value to the state-monadic value, to run the combined computation it describes:
st = do { x <- get ; return (x+1) }
x = runState st 0 -- x is (1,0)
I also sense another possible misunderstanding on your part: you write: "when is that initial (\s -> ...) argument called?" There's no "initial" lambda: the lambdas are all nested inside!
do { a <- as; b <- bs; c <- foo b; return c }
translates as
as >>= (\a -> bs >>= (\b -> foo b >>= (\c -> return c)))
so it's not "initial", that's one combined all-enclosing lambda that is called with the initial state!
And then it will call
let (a,s1) = runState as s0
etc. with that "initial" as in the do block.
the do block does not perform any stateful computation - it only assembles some smaller stateful computations into one bigger stateful computation. At the do level, the actual state does not exist.
It would be simpler and maybe even more accurate if the monad was called "a stateful computation". Or "a function that takes state of type S and returns another state of the same type alongside its actual result". Then you could imagine >>= as "combines two functions of the aforementioned type into one, such that the state returned by the first one is be passed as a parameter to the second one".
State is just a wrapper around functions of type s -> (a, s). runState doesn't actually "run" anything; it just gives back the function wrapped by the State constructor. You can, however, compare runState to the ($) operator for functions.
($) f x = f x
runState (State f) s = f s
That makes (=<<) = flip (>>=) similar to (<<<) = (.); just rather than taking two functions and returning a third function, it takes a function (that returns a State) and a State and produces a second State.
However, we'll make a direct comparison of (>>=) to (>>>) = flip (.) so that the types align better. (Similarly, you could compare (.) to (=<<).)
-- f :: t -> a
-- g :: a -> b
f >>> g = \t -> let a = ($) f t
in ($) g a
-- m :: State s a
-- g :: a -> State s b
m >>= g = State $ \s -> let (a, s') = runState m s
in runState (g a) s'

State Monad and 'put' function in Haskell

The Documentation about the State Monad says:
put :: s -> m ()
Replace the state inside the monad.
I cannot understand that. Does it mean that function replace state inside Monad? And The second issue: Why returned value is m () and not m s
The easiest way to understand the state monad, I think, is just to write your own and play around with it a bit. Study this code, play with other people's examples, and come back and review it from time to time until you're able to write it from memory:
-- | 'State' is just a newtype wrapper around the type #s -> (a, s)#.
-- These are functions which are fed a state value (type #s#) as input,
-- and produce as a pair of an #a# (the *result* of the state action)
-- and an #s# (the *new state* after the action).
--
-- The 'State' type is fundamentally a shortcut for chaining functions
-- of types like that.
newtype State s a = State { runState :: s -> (a, s) }
instance Functor (State s) where
fmap f (State g) = State $ \s0 ->
let (a, s1) = g s
in (f a, s1)
instance Applicative (State s) where
pure a = State $ \s -> (a, s)
State ff <*> State fa = State $ \s0 ->
let (s1, f) = ff s0
(s2, a) = fa s1
in (s2, f a)
instance Monad (State s) where
return = pure
State fa >>= f = State $ \s0 ->
let (s1, a) = fa s0
(s2, b) = runState (f a) s1
in (s2, b)
-- | 'get' is just a wrapper around a function that takes the
-- incoming #s# value and exposes it in the position where #a#
-- normally goes.
get :: State s s
get = State $ \s -> (s, s)
-- | 'put' is a wrapper around a function that discards the
-- the incoming #s# value and replaces it with another.
put :: s -> State s ()
put s = State $ \_ -> ((), s)
This is written directly in terms of a State type without using the MonadState class, which is a bit simpler to understand at first. As an exercise, once you feel comfortable with this, you can try writing it with the MonadState class.
And the second issue: Why returned value is m () and not m s?
It's mostly an arbitrary design choice, as far as I can tell. If I were designing the State type I might have written get and put like this, which is more similar to your expectation:
-- | Modify the incoming state by applying the given function to it.
-- Produces the previous, now discarded state as a result, which is
-- often useful.
modify :: (s -> s) -> State s s
modify f = State $ \s0 -> (s, f s)
-- Now 'get' and 'put' can be written in terms of 'modify':
get :: State s s
get = modify (\s -> s)
-- | This version of 'put' returns the original, discarded state,
-- which again is often useful.
put :: s -> State s s
put s = modify (\_ -> s)
If you have the standard 'get' and 'put' you can use that to write my modified 'put' as well:
-- | 'get' the incoming state, 'put' a new one in, and 'return' the old one.
replace :: s -> State s s
replace s1 = do
s0 <- get
put s1
return s0
So it doesn't make a big difference whether put produces () or s, anyway.

Haskell: Join on State Monad

How to formally calculate/interpret the following expression?
runState (join (State $ \s -> (push 10,1:2:s))) [0,0,0]
I understand the informal explanation, which says: first run the outer stateful computation and then the resulting one.
Well, that's quite strange to me since if I follow the join and >>= definitions, it looks to me like I have to start from the internal monad (push 10) as the parameter of the id, and then do... hmmmm... well... I'm not sure what.... in order to get what is supposedly the result:
((),[10,1,2,0,0,0])
However how to explain it by the formal definitions:
instance Monad (State s) where
return x = State $ \s -> (x,s)
(State h) >>= f = State $ \s -> let (a, newState) = h s
(State g) = f a
in g newState
and
join :: Monad m => m (m a) -> m a
join n = n >>= id
Also, the definition of the State Monad's bind (>>=) is quite hard to grasp as having some "intuitive"/visual meaning (as opposed to just a formal definition that would satisfy the Monad laws). Does it have a less formal and more intuitive meaning?
The classic definition of State is pretty simple.
newtype State s a = State {runState :: s -> (a,s) }
A State s a is a "computation" (actually just a function) that takes something of type s (the initial state) and produces something of type a (the result) and something of type s (the final state).
The definition you give in your question for >>= makes State s a a "lazy state transformer". This is useful for some things, but a little harder to understand and less well-behaved than the strict version, which goes like this:
m >>= f = State $ \s ->
case runState m s of
(x, s') -> runState (f x) s'
I've removed the laziness and also taken the opportunity to use a record selector rather than pattern matching on State.
What's this say? Given an initial state, I runState m s to get a result x and a new state s'. I apply f to x to get a state transformer, and then run that with initial state s'.
The lazy version just uses lazy pattern matching on the tuple. This means that the function f can try to produce a state transformer without inspecting its argument, and that transformer can try to run without looking at the initial state. You can use this laziness in some cases to tie recursive knots, implement funny functions like mapAccumR, and use state in lazy incremental stream processing, but most of the time you don't really want/need that.
Lee explains pretty well what join does, I think.
If you specialise the type of join for State s you get:
join :: State s (State s a) -> State s a
so given a stateful computation which returns a result which is another stateful computation, join combines them into a single one.
The definition of push is not given in your question but I assume it looks like:
push :: a -> State [a] ()
push x = modify (x:)
along with some State type like
data State s a = State (s -> (a, s))
A value of State s a is a function which, given a value for the current state of type s returns a pair containing a result of type a and a new state value. Therefore
State $ \s -> (push 10,1:2:s)
has type State [Int] (State [Int] ()) (or some other numeric type other than Int. The outer State function returns as its result another State computation, and updates the state to have the values 1 and 2 pushed onto it.
An implementation of join for this State type would look like:
join :: State s (State s a) -> State s a
join outer = State $ \s ->
let (inner, s') = runState outer s
in runState inner s'
so it constructs a new stateful computation which first runs the outer computation to return a pair containing the inner computation and the new state. The inner computation is then run with the intermediate state.
If you plug your example into this definition then
outer = (State $ \s -> (push 10,1:2:s))
s = [0,0,0]
inner = push 10
s' = [1,2,0,0,0]
and the result is therefore the result of runState (push 10) [1,2,0,0,0] which is ((),[10,1,2,0,0,0])
You mentioned following the definitions for join and >>=, so, let's try that.
runState (join (State $ \s -> (push 10,1:2:s))) [0,0,0] = ?
The definitions are, again
instance Monad (State s) where
-- return :: a -> State s a
return x = State $ \s -> (x,s)
so for x :: a, State $ \s -> (x,s) :: State s a; (*) ---->
(State h) >>= f = State $ \s -> let (a, newState) = h s
(State g) = f a
in g newState
join m = m >>= id
and runState :: State s a -> s -> (a, s), i.e. it should be (*) <----
runState (State g) s = g s. So, following the definitions we have
runState (join (State $ \s -> (push 10,1:2:s))) [0,0,0]
= runState (State g) [0,0,0]
where (State g) = join (State $ \s -> (push 10,1:2:s))
= (State $ \s -> (push 10,1:2:s)) >>= id
-- (State h ) >>= f
= State $ \s -> let (a, newState) = h s
(State g) = id a
h s = (push 10,1:2:s)
in g newState
= State $ \s -> let (a, newState) = (push 10,1:2:s)
(State g) = a
in g newState
= State $ \s -> let (State g) = push 10
in g (1:2:s)
Now, push 10 :: State s a is supposed to match with State g where g :: s -> (a, s); most probably it's defined as push 10 = State \s-> ((),(10:) s); so we have
= State $ \s -> let (State g) = State \s-> ((),(10:) s)
in g (1:2:s)
= State $ \s -> let g s = ((),(10:) s)
in g (1:2:s)
= State $ \s -> ((),(10:) (1:2:s))
= runState (State $ \s -> ((),(10:) (1:2:s)) ) [0,0,0]
= (\s -> ((),(10:) (1:2:s))) [0,0,0]
= ((), 10:1:2:[0,0,0])
. So you see that push 10 is first produced as a result-value (with (a, newState) = (push 10,1:2:s)); then it is treated as the computation-description of type State s a, so is run last (not first, as you thought).
As Lee describes, join :: State s (State s a) -> State s a; the meaning of this type is, a computation of type State s (State s a) is one that produces State s a as its result-value, and that is push 10; we can run it only after we get hold of it.

Understanding the symbol "<-" when using State Monad?

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) ?
I'm not entirely sure about the phrasing of your question, so correct me if I misunderstood.
There is an equivalence between the syntactic sugar for the do-notation syntax <- and the overloaded bind operator (>>=).
do { a <- f ; m } ≡ f >>= \a -> do { m }
So if you were to desugar the binds, it would look like:
rollDie' :: GeneratorState Int
rollDie' =
get >>= \generator ->
let (value, newGenerator) = randomR (1,6) generator in
put newGenerator >>= \_ ->
return value
If you understand how the State monad works, in the implementation it threads around the state in an implicit argument for each bind (i.e. >>=). A simplified implementation might look like:
newtype State s a = State { runState :: s -> (a,s) }
instance Monad (State s) where
return a = State $ \s -> (a, s)
State act >>= k = State $ \s ->
let (a, s') = act s
in runState (k a) s'
get :: State s s
get = State $ \s -> (s, s)
put :: s -> State s ()
put s = State $ \_ -> ((), s)
So the bind operator when specialized to this specific State monad has the type:
(>>=) :: State s a -> (a -> State s b) -> State s b
The function get is simply the function that returns the state as an argument so you can inspect it, so it has to match the s type of the monad it lives in.
-- +--- State
-- | +- Return value (inner state)
-- | |
get :: State s s

Increment function for state monad in Haskell

I have the following defined State Monad, with which I am trying to implement an increment function:
data State a = State (Int -> (a, Int))
instance Monad State where
return x = State $ \s -> (x, s)
(State f) >>= k = State $ \s ->
let
(x, s') = f s
State f' = k x
in f' s'
get :: State Int
get = State $ \s -> (s, s)
put :: Int -> State ()
put s = State $ \_ -> ((), s)
I have done the following:
increment :: State ()
increment = do
a <- get
put(a+1)
And this appears to work.
Is this correct, and how can I verify that the state is indeed being incremented? Perhaps more generally, how do I use get an put?
You need some way to extract the inner function of the state. You can either do this by pattern matching on State a like you do in your bind definition or you can define State using record syntax data State a = State {runState :: Int -> (a, Int)}. Once you have runState you can easily test your increment function using runState increment 1. Your use of get and put seems to be just fine, not quite sure what you want to know there.
Also you should add an Applicative instance for State because Applicative will be a superclass of Monad as of ghc 7.10.
Let's add a few utility functions to this code:
-- Given a `State` computation and a starting state, run the computation
-- and obtain the result value and final state.
runState :: State a -> Int -> (a, Int)
runState (State f) init = f init
-- Given a `State` computation and a starting state, run the computation
-- and obtain the result value.
evalState :: State a -> Int -> a
evalState st i = fst (runState st i)
-- Given a `State` computation and a starting state, run the computation
-- and obtain the final state.
execState :: State a -> Int -> Int
execState st i = snd (runState st i)
Now, using one of these functions, how would you write a function that tests whether increment does in fact increment the state by one?

Resources