Haskell is a pure - in another word, a stateless computation, where a program makde of cuntions that can't change any global any global state or variabble, they can only do some computations and return them results.
rember that we dealt with the Random numbers before. suppose that we want to generate three randome numbers to simuate three coin toss, and here is one code.
threeCoins :: StdGen -> (Bool, Bool, Bool) threeCoins gen = let (firstCoin, newGen) = random gen (secondCoin, newGen') = random newGen (thirdCoin, newGen'') = random newGen' in (firstCoin, secondCoin, thirdCoin)
as you can see, If we wanted to generate several random numbers, we always had to use the random generator that a previous function returned along with its result, so there is some requirement to preserve some state along the calls.
so to help us to understand the concept, we will go ahead and give the ma type, we'll say that a stateful computation is a funciton that takes some state and returns a value along with some new sttae, that function would have the following.
s -> (a,s)
s is the type of the state and a the result of the stateful computations.
we will examine the stateful computation by an example of stack and Stone (stack manipulation)
type Stack = [Int] pop :: Stack -> (Int,Stack) pop (x:xs) = (x,xs) push :: Int -> Stack -> ((),Stack) push a xs = ((),a:xs)
when we pop value from a stack, the stack left is the new state, and the top value is returned as the result. and with the push operation, () is returned as the resutl. and the x:xs is the new state.
stackManip :: Stack -> (Int, Stack) stackManip stack = let ((),newStack1) = push 3 stack (a ,newStack2) = pop newStack1 in pop newStack2
and let's do a test on the method:
ghci> stackManip [5,8,2,1] (5,[8,2,1])
and the code above is kind of tedious, with do Monad , and the State monad, we might be able to write as following.
stackManip = do push 3 a <- pop pop
The state Monad
Now let 's formally introduce the state monad, the State monad is defined in the Control.MOnad.State, and provde a new type that wraps stateful computations, here is the definition.
newtype State s a = State { runState :: s -> (a,s) }
and State type is part of the Monad Instance.
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
the return method is simple as it is, it just return a State monad ( a stateful computatoin) with the value as it resutls (any state is just wrapped as the State monad's context).
and what about >>=, it looks a bit more complex than the return functions, we start off with lambda, where it willl be our stateful computation (remember that hte newtype wrapper takes a lambda and returns a new State monad) . Because we are in a stateful computation, we can give the stateful computation h our current state s, which result in a pair of result and new state, (a, newState), evry time so far when we wre implementing >>=, once we had the extracted result from the monad value, we applied the function f to it to get the new monad value, in Writer, after doing that and getting the new monadic value, we still had to make sure that the context was taken care of by mappending the old monoid value with the new one. Here, we do f a and we get a new stateful computation g. Now that we have a new stateful computation and a new state (goes by the name of newState) we just apply that stateful computation g to the newState. The result is a tuple of final result and final state!
SO, here is the revised version with Monad state computation,
import Control.Monad.State pop :: State Stack Int pop = State $ \(x:xs) -> (x,xs) push :: Int -> State Stack () push a = State $ \xs -> ((),a:xs)
because of the "()" notation above, which means that the result type of the State monad is a void type.
import Control.Monad.State stackManip :: State Stack Int stackManip = do push 3 a <- pop pop
to extract the final result of the state monad, we can call the runState method to extract the value.
ghci> runState stackManip [5,8,2,1] (5,[8,2,1])
and you don't even need to bind to the second operation, as the 'a' returned value is not used anywhere.
stackManip :: State Stack Int stackManip = do push 3 pop pop
and with the Do monad , we can do more complex things, here is one example, where it first pop a value from the stack and check its vlaue, if it is 5, then continue to do more ..
stackStuff :: State Stack () stackStuff = do a <- pop if a == 5 then push 5 else do push 3 push 8
Remember, do expressions result in monadic values and with the State monad, a single do expression is also a stateful function.
the Control.Monad.State module provides a type class, that's called MonadState, and it features two useful functions, namely "get" and "put", for State, the get function is implemented like this:
get = State $ \s -> (s,s)
so it just takes the current state and presents it as the result, the put functions takes some state and make s astateful function that replace the current state with it ... (Pretty cool!!)
put newState = State $ \s -> ((),newState)
with this,we can do ... (stackyStack)
stackyStack :: State Stack () stackyStack = do stackNow <- get if stackNow == [1,2,3] then put [8,3,1] else put [9,2,1]
It's worh examing what the type of >>= would be if it only worked for State values. (teh partial specification for State monad)
(>>=) :: State s a -> (a -> State s b) -> State s b
this means we can glue together several stateful computation whose result are of different types but the types of the state has to stay the same. so what is that? given the type is Maybe, >>= may gives us :
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
Randomness and the State monad.
we know it is a bit awkward because every random function takes a generator and returns a random number along with a new Generator, and we have to use the new returned generator because otherwise we will get a pseudo number.
the random function is like this:
random :: (RandomGen g, Random a) => g -> (a, g)
and we can tell that this is a stateful computation. we can make a function as such.
import System.Random import Control.Monad.State randomSt :: (RandomGen g, Random a) => State g a randomSt = State random
and with this , we can rewrite the TheeCoins function as such ..
import System.Random import Control.Monad.State threeCoins :: State StdGen (Bool,Bool,Bool) threeCoins = do a <- randomSt b <- randomSt c <- randomSt return (a,b,c)
we can check on whether it is working as we expected, by the following method cal.
ghci> runState threeCoins (mkStdGen 33) ((True,False,True),680029187 2103410263)