
Am Mittwoch, 24. Oktober 2007 10:35 schrieb Bas van Dijk:
Suppose you have:
{-# OPTIONS_GHC -fglasgow-exts -fallow-overlapping-instances #-}
class C a b where foo :: a -> b -> (a, b)
instance C Int a where foo n x = (n+1, x) -- (A) instance C a Bool where foo x b = (x, not b) -- (B) instance C Int [a] where foo n xs = (n+1, xs) -- (C) instance C Int [Int] where foo n ns = (n+1, map (+1) ns) -- (D)
f :: [b] -> [b] f xs = snd $ foo (1 :: Int) xs
In the right hand sight of 'f', 'foo' is applied to an Int and a [b] so it seems that instance C should match. However GHC rejects this program because in a later call 'f' can be applied to a list of Ints (like in: g = f ([1,2,3] :: [Int])) by which 'b' instantiates to an Int, by which instance D should really match.
If you enable -fallow-incoherent-instances then 'f' will use instance C without complaining about the problem of subsequent instantiations.
However if you then define 'g' you will get the error:
Couldn't match expected type `Int' against inferred type `[a]' In the first argument of `f', namely `([1, 2, 3] :: Int)'
This seems to be a typo. g = f ([1,2,3] :: [Int]) is accepted. g = f ([1,2,3] :: Int) can never be, overlapping/incoherent instances or not
regards,
Bas.
Cheers, Daniel