You point is that the (C Int) dictionary has (C String) as a superclass, and (C String) has (C Int) as a superclass. So the two instances are mutually recursive, but that’s ok.

 

That is not unreasonable. But it is dangerous. Consider

         class C [a] => C a

Then any dictionary for (C a) would contain a dictionary for (C [a]) which would contain a dictionary for C [[a]], and so on.  Haskell is lazy so we might even be able to build this infinite dictionary, but it *is* infinite.

 

It’s a bit like the “recursive instance” stuff introduced in “Scrap your boilerplate with class”.

 

After 5 mins thought I can’t see a reason why this could not be made to work.  But it’d take work to do.  If you have a compelling application maybe you can open a feature request ticket, describing it, and referring this thread?

 

Has anyone else come across this?


Simon

 

From: glasgow-haskell-users-bounces@haskell.org [mailto:glasgow-haskell-users-bounces@haskell.org] On Behalf Of Ryan Trinkle
Sent: 20 July 2011 17:37
To: glasgow-haskell-users@haskell.org
Subject: Superclass Cycle via Associated Type

 

The following code doesn't compile, but it seems sensible enough to me.  Is this a limitation of GHC or is there something I'm missing?

 

 
class C (A x) => C x where
  type A x :: *
 
instance C Int where
  type A Int = String
 
instance C String where
  type A String = Int
 
 
The error I get is:
 
 
SuperclassCycle.hs:1:1:
    Cycle in class declarations (via superclasses):
      SuperclassCycle.hs:(1,1)-(2,15): class C (A x) => C x where {
                                           type family A x :: *; }


 
 
 
Ryan