[Haskell-cafe] Duplicate Instance problem

Jason Dagit dagit at eecs.oregonstate.edu
Wed Sep 27 17:47:55 EDT 2006


Hello,

I tried to create a type class for making instances of Show display a
custom way.  After using my class for a while I found that sometimes
RealFloats would display as 'NaN' and this is unacceptable.  So at
this point I had something like:

{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances
-fallow-overlapping-instances #-}
class Show a => StringValue a where
  -- | Convert a showable value to a string
  toString :: a -> String

instance Num a => StringValue a where
  toString = show

and I added an instance for RealFloat:

instance RealFloat a => StringValue a where
  toString x
    | isNaN x = "" -- as an example
    | otherwise = show x

Now, I get the error:
    Duplicate instance declarations:
      instance [overlap ok] (Num a) => StringValue a
	-- Defined at ...
      instance [overlap ok] (RealFloat a) => StringValue a
	-- Defined at ...

I've also tried spliting Num into an instance for Integral and
RealFloat but that doesn't work either (same error).

For now I'm using an instance for RealFloat and an instance for Int
and Integer (which are identical) and that seems to be okay but
suboptimal.

Why does the error say they are duplicates?  I would have expected
something about them overlapping but not duplicates.   Is there a way
to still do this without duplicates?

Thanks,
Jason


More information about the Haskell-Cafe mailing list