[Haskell-cafe] Re: New to haskell: unresolved overloading question

Christian Maeder maeder at tzi.de
Tue Feb 22 05:39:35 EST 2005


Keean Schupke wrote:
> There are problems with this approach... 

> instance (Ord a, Num a) => ApproxEq a where x ~= y = (abs (x-y) < 1)

> However it should do what you want with just -foverlapping-instances 
> -fundecidable-instances.

Only -fallow-incoherent-instances allowes to resolve

  3.5 ~= 2.6

without type-annotations! But when adding:

  instance ApproxEq Double where x~=y = False

the above result remained True (via Defaulting) whereas

3.5 ~= (2.6 :: Double)

became False. (An the other hand there was no need to add such an 
instance for Double.)

It is also not possible to add a further instance like:

  instance (Ord a, Fractional a) => ApproxEq a where
    x ~= y = (abs (x-y) < 0.1)

(hoping that fractionals are treated more accurate that nums) Only one 
instance for "ApproxEq a" is allowed.

Since the subject is "New to haskell" I recommend to stay Haskell98 
compliant, i.e. avoid overlapping instances and add type signatures as 
necessary.

If you add the option -Wall (or -fwarn-type-defaults) to ghci you'll get 
a warning whenever "defaulting" occurs (like in 1.5 == 1.5 to Double)

(Putting "default ()" in your source file prevents defaulting, but I 
don't recommend to do so only to force types on decls like "f = 1.5")

Christian


More information about the Haskell-Cafe mailing list