ugliness with state parameter in ST stuff

Brian Huffman bhuffman@galois.com
Tue, 6 Aug 2002 12:28:57 -0700


On Tuesday 06 August 2002 11:19 am, Hal Daume III wrote:
> Now, I want to make (Bar s) and instance of Foo.  But I can't seem to do
> this.  If I use:
> > instance Foo (Bar s) where
> >     foo = stToIO . getFirst
>
> GHC complains:
>
>     Cannot unify the type-signature variable `s'
> 	with the type `RealWorld'
> 	Expected type: Bar s -> IO Int
> 	Inferred type: Bar RealWorld -> IO Int
>     In the expression: stToIO . getFirst
>     In the definition of `foo': stToIO . getFirst

I think that you really meant "instance Foo (forall s . Bar s)", but 
unfortunately ghc doesn't allow that kind of polymorphism in instance 
declarations. Instead, your instance as written has the free type variable 
's' in it, which tells ghc that the following definitions should work for all 
possible values of 's'. But instances for (Bar Int) or (Bar Bool), etc. 
certainly would not work.

Try this instead:

> instance Foo (Bar RealWorld) where
>      foo = stToIO . getFirst

It should work OK, since it doesn't have the free type variable.
- Brian Huffman