Modification of State Transformer

Shawn P. Garbett listman@garbett.org
Thu, 8 Aug 2002 14:11:54 -0500


I'm trying to modify Richard Bird's state transformer. The example in his 
book (_Introduction_to_Functional_Programming_using_Haskell_) has State 
defined as a explicit type. 

I.e. Here's the relevant snippet:

-- State transformer definition

newtype St a = MkSt (State -> (a, State))
type State   = Int

-- State transformer applied to state
apply             :: St a -> State -> (a, State)
apply (MkSt f) s  = f s

-- State monad

instance Monad St where
  return x  = MkSt f where f s = (x,s)
  p >>= q   = MkSt f where f s = apply (q x) s'
                                 where (x, s') = apply p s
-----------------------------------------


What I want is something like this, so that the state transformer has a 
generic state type: 

newtype St a s = MkSt (s -> (a, s))

apply             :: St a s -> s -> (a, s)
apply (MkSt f) s  = f s

instance Monad St where
  return x  = MkSt f where f s = (x,s)
  p >>= q   = MkSt f where f s = apply (q x) s'
                                 where (x, s') = apply p s
-----------------------------------------------------------
The trouble occurs on the instance line
    Couldn't match `*' against `* -> *'
        Expected kind: (* -> *) -> *
        Inferred kind: (* -> * -> *) -> *
    When checking kinds in `Monad St'
    In the instance declaration for `Monad St'
Failed, modules loaded: none.