
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.

Shawn P. Garbett writes: : | 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: (* -> * -> *) -> * Let's compare your declaration of St with the type signatures in class Monad. class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b -- etc. If we instantiate m as St, we get a type of a -> St a for return, which lacks the state variable s. In turn, s corresponds to the third * in the inferred kind in the error message. Try partially applying St to its state variable, and declaring a Monad instance of that partial application, which will have the right kind *->*. Regards, Tom

Tom Pledger writes: | Shawn P. Garbett writes: : | | Inferred kind: (* -> * -> *) -> * : | In turn, s corresponds to the third * in the inferred kind in the | error message. Oops! Sorry, the third * is the result of applying St to two types. The second of those two types, s, corresponds to the second *.

On 2002-08-08T14:11:54-0500, Shawn P. Garbett wrote:
newtype St a s = MkSt (s -> (a, s)) instance Monad St where
This line should say instance Monad (St a) where because it is (St a) that is a Monad, not St by itself. -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig http://www.ethnologue.com/
participants (3)
-
Ken Shan
-
Shawn P. Garbett
-
Tom Pledger