[Haskell-cafe] A possibly stupid doubt about the GHC's overlapping instances flag's

Bas van Dijk v.dijk.bas at gmail.com
Wed Oct 24 04:35:53 EDT 2007


[only replying to haskell-cafe]

On 10/20/07, Rodrigo Geraldo <rodrigogribeiro at gmail.com> wrote:
> Hi!
>
> Suppose that the GHC's flag -fallow-incoherent-instances is enabled. In this
> situation, when a instance will be rejected?
> And if the flag -fallow-overlapping-instances is enabled. When a instance
> will be rejected?
>
> Thanks!
>
> Rodrigo

The following is mainly from the GHC Userguide:
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#instance-overlap

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)'

regards,

Bas.


More information about the Haskell-Cafe mailing list