[Haskell-cafe] Confused about missing data constructor
Antoine Latter
aslatter at gmail.com
Wed Nov 24 09:18:29 EST 2010
On Tue, Nov 23, 2010 at 6:58 PM, Adam Miezianko <adam at theorylounge.org> wrote:
> I'm working through Learn You a Haskell for Great Good [1] and getting
> a compiler error while playing around with some of the code. I have
> this:
>
> -- BEGIN state.hs
> import Control.Monad.State
>
> type Stack = [Int]
>
> pop :: State Stack Int
> pop = State $ \(x:xs) -> (x,xs)
>
> push :: Int -> State Stack ()
> push x = State $ \xs -> ((), x:xs)
>
> stackManip :: State Stack Int
> stackManip = do
> push 3
> pop
> pop
> -- END state.hs
>
> But when I try to load it into ghci I get the following errors:
>
> Prelude> :load "/home/admi/.pe/state.hs"
> [1 of 1] Compiling Main ( /home/admi/.pe/state.hs, interpreted )
>
> /home/admi/.pe/state.hs:6:6: Not in scope: data constructor `State'
>
> /home/admi/.pe/state.hs:9:9: Not in scope: data constructor `State'
> Failed, modules loaded: none.
> Prelude>
>
> Now, I'm not exactly sure how to read the documentation for
> Control.Monad.State [2] but it seems that newtype State s a = State
> {...} defines a constructor, or am I wrong on that point too? So,
> what am I missing here? In case it matters, I am using mtl-2.0.1.0
> and ghci 6.12.3.
>
> [1] http://learnyouahaskell.com/for-a-few-monads-more
> [2] http://cvs.haskell.org/Hugs/pages/libraries/mtl/Control-Monad-State.html
>
It looks like your documentation doesn't match the library you're using.
The documentation for mtl-2.x is here: http://hackage.haskell.org/package/mtl
Starting in version 2.0, the mtl package no longer exports a
constructor called 'State' - but there is a function 'state' which
servers the same purpose.
However you might be better served using the 'modify' function.
I hope that helps,
Antoine
> --
> Adam Miezianko
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list