Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#9288 closed bug (invalid)

Type class overlapping instances check doesn't understand type equality

Reported by: ezyang Owned by:
Priority: low Milestone:
Component: Compiler (Type checker) Version: 7.9
Keywords: Cc: vogt.adam@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC accepts invalid program Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Here is a sample program which we'd expect to give an overlapping instances error, but does not:

{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
module Foo where
type family F a :: *
type instance F Bool = Int
type instance F Int = Bool
class C a where
instance (F Bool ~ a) => C a where
instance C Int where

Fortunately, GHC does notice when you actually try to use the instance.

Change History (4)

comment:1 Changed 3 years ago by aavogt

A simpler example where ghc-7.8.2 doesn't detect overlapping instances until they are used

{-# LANGUAGE FlexibleInstances #-}
class C a where c :: a
instance C a where c = undefined
instance C Int where c = 2
-- ghc doesn't complain about overlapping instances
-- unless you uncomment c1
-- c1 = c :: Int
c2 = c :: Double

comment:2 Changed 3 years ago by aavogt

Cc: vogt.adam@… added

comment:3 Changed 3 years ago by simonpj

Resolution: invalid
Status: newclosed

This is by design. GHC does not report overlapping instances eagerly (at the instance definition site). Rather it reports them lazily (when used). There is good reason for this:

instance C Int a where
instance C b Bool where

If we reported errors eagerly we'd have to say they overlap. But there is no problem with, say (C Int Char).

It's nothing to do with equalities.

The user manual does not say this explicitly, so I'll fix that. Thanks for raising it.


comment:4 Changed 3 years ago by Simon Peyton Jones <simonpj@…>

In 0fcf060418167e05adfbde174b2f030077cb1c1b/ghc:

Improve documentation of overlapping instances (again)

Prompted by Trac #9288
Note: See TracTickets for help on using tickets.