[Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?

Stephen Tetley stephen.tetley at gmail.com
Tue Jul 26 18:46:36 CEST 2011


For:

instance (Ord a) => Max a where
 maximum = max

The same could more simply be achieved with a function:

maximum :: Ord a => a
maximum = max

Now, you probably wanted both a base-case using max and type specific,
special cases:

instance Max Int where
  maximum = 2^16

If you have both instances defined in the same module, GHC should
always pick the special case for Int if overlapping instances is
turned on. However, I've never found a description of how it resolves
instance selection if you have the specialized cases in different
modules. Unspecified [*] behaviour is not something I'd want to rely
on, so I always avoid Overlapping Instances.


[*] Of course, the multiple module behaviour might be specified somewhere...



More information about the Haskell-Cafe mailing list