Superclass Cycle via Associated Type

Dan Doel dan.doel at gmail.com
Wed Jul 20 22:15:06 CEST 2011


This isn't a GHC limitation. The report specifies that the class
hierarchy must be a DAG. So C cannot require itself as a prerequisite,
even if it's on a 'different' type.

Practically, in the implementation strategy that GHC (and doubtless
other compilers) use, the declaration:

    class C (A x) => C x ...

means that a C x dictionary contains a C (A x) dictionary, which
contains a C (A (A x)) dictionary.... And dictionaries are strictly
evaluated, so this sort of infinite definition cannot work.

-- Dan

On Wed, Jul 20, 2011 at 12:37 PM, Ryan Trinkle <ryant5000 at gmail.com> wrote:
> 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
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
>
>



More information about the Glasgow-haskell-users mailing list