Skip to content

erroneous overlapping instances reported with FunDeps

When a superclass constraint has functional dependencies, in certain cases GHC-7.6 erroneously reports that duplicate instances are found.

file Foo.hs

{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}

module Foo where

import Data.ByteString as B
import Data.Word

class Foo a b | a -> b

class (Foo a b) => Bar a b | a -> b

instance Foo [a] a
instance Bar [a] a
instance Foo ByteString Word8
instance Bar ByteString Word8

test :: Bar full item => full -> full
test inp = inp

-- this works
-- _test_x :: ByteString -> ByteString
-- _test_x = test

file Main.hs

module Main where

import Foo
import Data.ByteString

-- this works
-- test1 :: [Int] -> [Int]
-- test1 = test

-- this fails
test2 :: ByteString -> ByteString
test2 = test

ghc reports

Main.hs:12:9:
    Overlapping instances for Foo ByteString GHC.Word.Word8
      arising from a use of `test'
    Matching instances:
      instance Foo ByteString GHC.Word.Word8 -- Defined at Foo.hs:20:10
    There exists a (perhaps superclass) match:
    (The choice depends on the instantiation of `'
     To pick the first instance above, use -XIncoherentInstances
     when compiling the other instance declarations)
    In the expression: test
    In an equation for `test2': test2 = test

For this to manifest, I think that at least the following must be true:

  • a function with class constraints must be called from a separate module
  • the type class instance must be monomorphic in the fundep-to parameter

This example works in ghc-7.4.2.

Trac metadata
Trac field Value
Version 7.6.1-rc1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (Type checker)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC jwlato@gmail.com
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information