[Haskell-beginners] State Monad - Not in scope: data constructor `State'

Brent Yorgey byorgey at seas.upenn.edu
Sat Oct 29 22:09:52 CEST 2011


On Sat, Oct 29, 2011 at 08:48:28PM +0100, Hugo Ferreira wrote:
> Hello,
> 
> I am trying to use some State Monad code
> in order to learn about this. However in
> GHCi I cannot use the "State" constructor.
> 
> import qualified Control.Monad.State
> 
> >:{
> | let fromStoAandS c | c `mod` 5 == 0 = ("foo",c+1)
> |                | otherwise      = ("bar",c+1)
> | :}
> > :t fromStoAandS
> >fromStoAandS :: Integral t => t -> ([Char], t)
> 
> >:{
> | let stateIntString = State fromStoAandS
> >Prelude Control.Monad.State| :}
> >
> ><interactive>:1:22: Not in scope: data constructor `State'
> 
> I have found a message stating that this is not possible
> because their is "no State data constructor", it is only "defined
> to be a type alias". 

That is correct.  State used to be defined directly; now it is just an
alias for StateT s Identity.  You don't really have to worry about
what this means, but the point is that there is no longer a real State
constructor.

The author of the message then says we should use:
> 
> http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-State-Lazy.html#v:state
> 
> instead. Still lost however.
> So, how do I create the "Sate" above?

You simply replace 'State' by 'state', a function provided to act like
the old State constructor.  So you write

  let stateIntString = state fromStoAandS

-Brent



More information about the Beginners mailing list