[Haskell-beginners] bad state monad instances

Keith Sheppard keithshep at gmail.com
Tue Jun 22 22:23:33 EDT 2010


Thanks! This change plus adding FlexibleInstances makes it valid.

On Tue, Jun 22, 2010 at 10:17 PM, Daniel Fischer
<daniel.is.fischer at web.de> wrote:
> 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


More information about the Beginners mailing list