[Haskell-cafe] Trying to avoid duplicate instances

Eric Stansifer stansife at caltech.edu
Tue May 13 14:16:48 EDT 2008

I am using a bunch of empty type classes to categorize some objects:

> class FiniteSolidObject o
> class FinitePatchObject o
> class InfiniteSolidObject o
> instance FiniteSolidObject Box
> instance FiniteSolidObject Sphere
> instance FinitePatchObject Mesh
> instance InfiniteSolidObject Plane

and would like to write a function elsewhere with a type signature like:
> intersection :: (SolidObject o1, SolidObject o2) => o1 -> o2 -> Intersection o1 o2

These classes are not exposed to the user (which will be me, but
that's besides the point) so the the user (me) does not accidentally
try to intersect two meshes, etc.  So long as I define my instances
correctly the compiler will verify this at compile time.

Since "solid objects" are exactly "finite solid objects" plus
"infinite solid objects", there is an obvious way to code up this
logical relationship.  So I try to write:
> class SolidObject o
> instance FiniteSolidObject o => SolidObject o
> instance InfiniteSolidObject o => SolidObject o

but inevitably GHC complains that I have duplicate instance
declarations.  I had mistakenly thought that GHC would only give such
an error if it could exhibit a specific type 'o' which satisfied both
the context 'FiniteSoildObject o' and the context 'InfiniteSolidObject
o' (and I know that there does not exist any such 'o' because I define
my instances such that 'FiniteSolidObject' and 'InfiniteSolidObject'
will never coincide, and I do not export any of these type classes),
but I see now that this is not the case.  Adding flags like
-fallow-overlapping-instances doesn't convince GHC.  Is there a way
around this other than manually writing out all the instances I want?
That is,

> instance FiniteSolidObject Box
> instance FiniteObject Box
> instance SolidObject Box
> instance UnionableObject Box
> instance Object Box
> instance FiniteSolidObject Sphere


More information about the Haskell-Cafe mailing list