14

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?

12
  • 7
    Control.Monad.State doesn't export the State constructor, use state (with lower-case s).
    – Vitus
    Mar 14, 2012 at 8:31
  • @Vitus Nice, I didn't know that function was exported. I think you should write it as an answer rather than a comment.
    – danr
    Mar 14, 2012 at 8:38
  • 1
    @Vitus: that's odd then, because his code actually compiles and runs well on my GHCI 6.12.3 on windows. Mar 14, 2012 at 8:49
  • 7
    @Riccardo State was deprecated in favor of StateT. Since the State monad can be defined as StateT on the Identity monad, and so it's now a type synonym and there's no State data constructor.
    – is7s
    Mar 14, 2012 at 8:58
  • 2
    ok, so it's just because learnyouahaskell is old? :)
    – Undreren
    Mar 14, 2012 at 8:59

2 Answers 2

20

As I mentioned in comments, you ought to use state instead of State.


The problem is that State is not standalone data type (or rather newtype), but it is the StateT transformer applied to Identity monad. Actually, it's defined as

type State s = StateT s Indentity

and because it's just type synonym, it cannot have State constructor. That's why Control.Monad.State uses state.

1
  • 1
    This no longer works. Does anyone know of the proper syntax? May 27, 2017 at 6:06
1

The accepted answer already mentions to use the state function from Control.Monad.State rather than the State type. However, if you simply try the accepted answer in ghci with the associated mtl package loaded, it will still fail:

Prelude Control.Monad.State> push a = state $ \xs -> ((), a:xs)

<interactive>:5:1: error:
    • Non type-variable argument in the constraint: MonadState [a] m
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        push :: forall a (m :: * -> *). MonadState [a] m => a -> m ()

To resolve this, there are two options here:

  1. Either specify the type signature for the function (which you should anyways).
  2. Add the FlexibleContexts compiler extension, as mentioned in the error.

We can specify the type signature:

module LearnYouAHaskell where

import Control.Monad.State as State

push :: Int -> State.State [Int] ()
push a = State.state $ \xs -> ((), a:xs)

The above compiles fine and functions as expected.

Or we can add the language extension so it can infer the function type.

{-# LANGUAGE FlexibleContexts #-}
module LearnYouAHaskell where

import Control.Monad.State as State

push a = State.state $ \xs -> ((), a:xs)

Which will also work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.