Trying to avoid duplicate instances

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 ...
Thanks, Eric

Hello Eric, Tuesday, May 13, 2008, 10:16:48 PM, you wrote:
-fallow-overlapping-instances doesn't convince GHC. Is there a way around this other than manually writing out all the instances I want?
afaik, no. typeclasses are not the same as OOP classes. i suggest you to look into http://haskell.org/haskellwiki/OOP_vs_type_classes and in particular papers mentioned at the end - one describes implementation of type classes and other emphasize differences between t.c. and classes. these should shed the light :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (2)
-
Bulat Ziganshin
-
Eric Stansifer