[Haskell-cafe] flexible contexts problem

Sean McLaughlin seanmcl at gmail.com
Sat Sep 12 20:17:02 EDT 2009

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.
someone please tell me what I'm doing wrong?

Thanks in advance.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090912/7c66fea8/attachment.html

More information about the Haskell-Cafe mailing list