
Hi, I'm working on understanding the state monad, and I got stumped pretty much right away. When I run the following script (with instances copied verbatim from http://www.haskell.org/all_about_monads/html/statemonad.html ) #!/usr/bin/env runhaskell \begin{code} {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} import Control.Monad.State(Monad, MonadState(..)) newtype State s a = State { runState :: (s -> (a,s)) } instance Monad (State s) where return a = State $ \s -> (a,s) (State x) >>= f = State $ \s -> let (v,s') = x s in runState (f v) s' instance MonadState (State s) s where get = State $ \s -> (s,s) put s = State $ \_ -> ((),s) main :: IO () main = putStrLn "hello" \end{code} It fails with: statemonadtest.lhs:11:20: `State s' is not applied to enough type arguments Expected kind `*', but `State s' has kind `* -> *' In the instance declaration for `MonadState (State s) s' Can you see what I'm doing wrong? I must be making a really basic mistake but I'm not sure what it is. Thanks, Keith -- keithsheppard.name

Change the instance head to
instance MonadState s (State s) where
It looks like the tutorial has the two parameters for MonadState in
the opposite order as does the mtl package.
Alex
On Tue, Jun 22, 2010 at 6:53 PM, Keith Sheppard
Hi,
I'm working on understanding the state monad, and I got stumped pretty much right away. When I run the following script (with instances copied verbatim from http://www.haskell.org/all_about_monads/html/statemonad.html )
#!/usr/bin/env runhaskell \begin{code} {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} import Control.Monad.State(Monad, MonadState(..))
newtype State s a = State { runState :: (s -> (a,s)) }
instance Monad (State s) where return a = State $ \s -> (a,s) (State x) >>= f = State $ \s -> let (v,s') = x s in runState (f v) s'
instance MonadState (State s) s where get = State $ \s -> (s,s) put s = State $ \_ -> ((),s)
main :: IO () main = putStrLn "hello"
\end{code}
It fails with: statemonadtest.lhs:11:20: `State s' is not applied to enough type arguments Expected kind `*', but `State s' has kind `* -> *' In the instance declaration for `MonadState (State s) s'
Can you see what I'm doing wrong? I must be making a really basic mistake but I'm not sure what it is.
Thanks, Keith -- keithsheppard.name _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wednesday 23 June 2010 03:53:14, Keith Sheppard wrote:
Hi,
I'm working on understanding the state monad, and I got stumped pretty much right away. When I run the following script (with instances copied verbatim from http://www.haskell.org/all_about_monads/html/statemonad.html )
#!/usr/bin/env runhaskell \begin{code} {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} import Control.Monad.State(Monad, MonadState(..))
newtype State s a = State { runState :: (s -> (a,s)) }
instance Monad (State s) where return a = State $ \s -> (a,s) (State x) >>= f = State $ \s -> let (v,s') = x s in runState (f v) s'
instance MonadState (State s) s where get = State $ \s -> (s,s) put s = State $ \_ -> ((),s)
main :: IO () main = putStrLn "hello"
\end{code}
It fails with: statemonadtest.lhs:11:20: `State s' is not applied to enough type arguments Expected kind `*', but `State s' has kind `* -> *' In the instance declaration for `MonadState (State s) s'
Can you see what I'm doing wrong? I must be making a really basic mistake but I'm not sure what it is.
Wrong argument order in the MonadState instance, class (Monad m) => MonadState s m | m -> s where get :: m s put :: s -> m () The state type comes first, then the Monad. Make it instance MonadState s (State s) where ... I don't know if that's been changed at some point or if it was a typo in the tutorial from the beginning.
Thanks, Keith

Thanks! This change plus adding FlexibleInstances makes it valid.
On Tue, Jun 22, 2010 at 10:17 PM, Daniel Fischer
On Wednesday 23 June 2010 03:53:14, Keith Sheppard wrote:
Hi,
I'm working on understanding the state monad, and I got stumped pretty much right away. When I run the following script (with instances copied verbatim from http://www.haskell.org/all_about_monads/html/statemonad.html )
#!/usr/bin/env runhaskell \begin{code} {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} import Control.Monad.State(Monad, MonadState(..))
newtype State s a = State { runState :: (s -> (a,s)) }
instance Monad (State s) where return a = State $ \s -> (a,s) (State x) >>= f = State $ \s -> let (v,s') = x s in runState (f v) s'
instance MonadState (State s) s where get = State $ \s -> (s,s) put s = State $ \_ -> ((),s)
main :: IO () main = putStrLn "hello"
\end{code}
It fails with: statemonadtest.lhs:11:20: `State s' is not applied to enough type arguments Expected kind `*', but `State s' has kind `* -> *' In the instance declaration for `MonadState (State s) s'
Can you see what I'm doing wrong? I must be making a really basic mistake but I'm not sure what it is.
Wrong argument order in the MonadState instance,
class (Monad m) => MonadState s m | m -> s where get :: m s put :: s -> m ()
The state type comes first, then the Monad.
Make it
instance MonadState s (State s) where ...
I don't know if that's been changed at some point or if it was a typo in the tutorial from the beginning.
Thanks, Keith
-- keithsheppard.name
participants (3)
-
Alexander Dunlap
-
Daniel Fischer
-
Keith Sheppard