Overlapping instances and multi-parameter classes

Mike Gunter m at ryangunter.com
Fri Mar 5 22:13:29 EST 2004



Given the following:

>
> import Data.Maybe	( fromMaybe )
> 
> data None
> none = error "none evaluated" :: None

these class and instance declarations:

> class    ToMaybe2 b    a where toMaybe2   :: a -> Maybe b
> instance ToMaybe2 a None where toMaybe2 = const Nothing
> instance ToMaybe2 a    a where toMaybe2 = Just

compile fine (given -fallow-undecidable-instances and
-fallow-overlapping-instances).  And the following code:

> mbPutStrLn	:: ToMaybe2 String a => a -> IO ()
> mbPutStrLn	= putStrLn . fromMaybe "None." . toMaybe2
> main = mbPutStrLn none >> mbPutStrLn "Hello, world!"

works as I'd expect.  However, if the parameters to the class
are reversed:

> {- uncomment to see error message
> class    ToMaybe1 a    b where toMaybe1   :: a -> Maybe b
> instance ToMaybe1 None a where toMaybe1 = const Nothing
> instance ToMaybe1 a    a where toMaybe1 = Just
> -}

I get the following error message (from GHC{,i} 6.2):

    Overlapping instance declarations:
      OverlapOrder.lhs:27: ToMaybe1 None a
      OverlapOrder.lhs:28: ToMaybe1 a a

Why is this?

	thanks,
	mike


More information about the Glasgow-haskell-users mailing list