I was reading Learn You a Haskell's guide on the state monad, but I had trouble understanding it since the stack example couldn't compile. In the guide, he used the following piece of code:
import Control.Monad.State
type Stack = [Int]
pop :: State Stack Int
pop = State $ \(x:xs) -> (x,xs)
push :: Int -> State Stack ()
push a = State $ \xs -> ((),a:xs)
While I understand what it's supposed to do, it won't compile. I have no idea why. The error message is:
Stack.hs:6:7: Not in scope: data constructor `State'
Stack.hs:9:10: Not in scope: data constructor `State'
This makes no sense to me, since "State" is, to my knowledge, in fact a data constructor, defined as
newtype State s a = State { runState :: s -> (a,s) }
Is the guide "wrong", and if so, how do I fix it?
Control.Monad.State
doesn't export theState
constructor, usestate
(with lower-cases
).State
was deprecated in favor ofStateT
. Since theState
monad can be defined asStateT
on theIdentity
monad, and so it's now a type synonym and there's noState
data constructor.