I have a large program where I would like to include disjoint type classes into a new type class. My problem can be reduced to the following example (taken from [1]):
class Num a => Dividable a where dividedBy :: a -> a -> a
instance Fractional a => Dividable a where dividedBy = (/)
instance Integral a => Dividable a where dividedBy = div
Which doesn't work because Haskell doesn't detect that "Integral" and "Fractional" are disjoint. Is there a way to implement these type classes with current Haskell implementations? [1] K. Glynn, M. Sulzmann, P.J. Stuckey Type Classes and Constraint Handling Rules http://www.cs.mu.oz.au/tr_submit/test/cover_db/mu_TR_2000_7.html Best regards, Jose Labra
Thu, 18 Feb 1988 08:17:58 +0100, Jose Emilio Labra Gayo <labra@lsi.uniovi.es> pisze:
Which doesn't work because Haskell doesn't detect that "Integral" and "Fractional" are disjoint.
And indeed there is no disjointness concept in the language. Nothing prevents from making a type an instance of both.
Is there a way to implement these type classes with current Haskell implementations?
I'm afraid not. In the standard way you must decide for each type separately. At most you could use ghc with -fallow-overlapping-instances -fallow-undecidable-instances, which would allow to define one of your instances - the other would have to be defined for each type separately; but these options are not nice. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
participants (2)
-
Jose Emilio Labra Gayo -
qrczak@knm.org.pl