[Haskell-cafe] coherence when overlapping?

Simon Peyton-Jones simonpj at microsoft.com
Wed Apr 12 12:43:33 EDT 2006

| In the GHC documentation which describes the extension of overlapping
| instances, an example similar to the following is given.
| >class C a where
| >   f:a -> a
| >instance C Int where
| >   f=e1
| >instance C a where
| >   f=e2
| >
| >let g x = f x
| >in g 1
| In this case GHC takes an ¡°incoherent¡± decision by taking the second
| instance as an instantiation of function f even it is executed with an input
| of type Int.

No it doesn't (ghc 6.4.1).  I've just tried it.  It uses the C Int instance, for exactly the reason you describe.

There is a flag -fallow-incoherent-instances that _does_ allow incoherence


{-# OPTIONS -fallow-overlapping-instances -fglasgow-exts -fallow-undecidable-instances #-}

module Main where

class C a where
   f::a -> a
instance C Int where
   f x = x+1
instance C a where
   f x = x

main = print (let g x = f x
	      in g (1::Int))

More information about the Haskell-Cafe mailing list