[Haskell-cafe] wierd type errros with MPTCs

Simon Peyton-Jones simonpj at microsoft.com
Fri Jan 28 04:17:00 EST 2005

The kind error seems fair enough.  GHC looks at the class decl, and
decides that indexVal is of kind *.  Then the instance declaration uses
it with kind (*->*).   You may say that it should look at the instance
decl too, before deciding kinds, but the instance decl might be in
another file.

Uncommenting insertIndex allows the compiler to see that indexVal must
be of kind (*->*), since it's applied to item in the type of
insertIndex.  So the compiler does do this much lookahead; the class ops
can't be in another module.

In GHC you can say

	class Table table (indexVal :: * -> *) where ...

if you like, to make it 100% clear.

When you declare a default method, GHC makes up a definition like this:

	$dmunion :: Table table indexVal => table item -> table item ->
table item
	$dmunion t1 t2 = t1

("dm" for default method)

Then, in the instance decl, if you don't give a declaration for union,
GHC makes one up, as if you had written

	instance Table DBTable IVal where
	  union = $dmunion

Now you can see where the error arises.  The call to $dmunion gives the
constraint (Table DBTable ??) with no way for it to figure out what the
?? is.  The type of $dmunion is ambiguous in fact.

Now, I agree that the error message, mentioning $dmunion, is rather
unhelpful.  But I can't see an easy way to improve it.  Meanwhile, I
hope this helps you make progress.


| -----Original Message-----
| From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of S.
| Alexander Jacobson
| Sent: 27 January 2005 17:53
| To: haskell-cafe at haskell.org
| Subject: [Haskell-cafe] wierd type errros with MPTCs
| This code gives me a Kind error because IVal isn't
| applied to enough type arguments.
|        import qualified Set
|        class Table table indexVal | indexVal->table where
|            --insertIndex::item->indexVal item -> table item ->table
|            union::table item -> table item -> table item
|            --union t1 t2 = t1
|        data DBTable item = DBTable
|        data IVal item = Name item
|        instance Table DBTable (IVal ) where
| Weirdly, when I uncomment the insertIndex
| function, things work.  But, if I then uncomment
| the default implementation of union, I get:
|        No instance for (Table DBTable indexVal)
|          arising from use of `Main.$dmunion' at example.hs:13
|        In the definition of `union': union = Main.$dmunion
|        In the definition for method `union'
|        In the instance declaration for `Table DBTable IVal'
| I don't know what this error even means.  But it
| goes away if I put the union implementation in the
| instance rather than in the class.
| Bot these error messages seem unreasonable.  Can
| someone clarify?
| Note: I am using GHC 6.2.2
| -Alex-
| ______________________________________________________________
| S. Alexander Jacobson tel:917-770-6565 http://alexjacobson.com
| _______________________________________________
| Haskell-Cafe mailing list
| Haskell-Cafe at haskell.org
| http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list