I'm having trouble understanding the following behavior. The following program compiles:
{-# OPTIONS_GHC -XMultiParamTypeClasses -XFlexibleContexts #-}
import Control.Monad.State
class Has á s where
has :: s -> (á, s)
project :: (MonadState s m, Has á s) => m á
project = do (á, s) <- gets has
put s
return á
f :: (MonadState s m, Has Int s) => m Int
f = do x <- project
return x
However, if you replace the function f with
f :: (MonadState s m, Has Int s) => m Int
f = do x <- project
y <- project
return x
then it fails with the error
Could not deduce (Has á s)
from the context (MonadState s m, Has Int s)
arising from a use of `project'
at /Users/sean/uploads/Weird.hs:16:12-18
Possible fix:
add (Has á s) to the context of the type signature for `f'
In a stmt of a 'do' expression: y <- project
In the expression:
do x <- project
y <- project
return x
In the definition of `f':
f = do x <- project
y <- project
return x
I don't see how the second call to project could possibly make a difference. Could
someone please tell me what I'm doing wrong?
Thanks in advance.
Sean