[GHC] #9242: Implement {-# OVERLAPPABLE #-} and {-# INCOHERENT #-} pragmas

GHC ghc-devs at haskell.org
Tue Jul 29 08:58:55 UTC 2014


#9242: Implement {-# OVERLAPPABLE #-} and {-# INCOHERENT #-} pragmas
-------------------------------------+-------------------------------------
              Reporter:  simonpj     |            Owner:  diatchki
                  Type:  feature     |           Status:  new
  request                            |        Milestone:
              Priority:  normal      |          Version:  7.8.2
             Component:  Compiler    |         Keywords:
            Resolution:              |     Architecture:  Unknown/Multiple
      Operating System:              |       Difficulty:  Unknown
  Unknown/Multiple                   |       Blocked By:
       Type of failure:              |  Related Tickets:
  None/Unknown                       |
             Test Case:              |
              Blocking:              |
Differential Revisions:              |
-------------------------------------+-------------------------------------

Comment (by simonpj):

 One more thing.  Look at this note in `InstEnv`:
 {{{
 Note [Incoherent instances]
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 For some classes, the choice of a particular instance does not matter, any
 one
 is good. E.g. consider

         class D a b where { opD :: a -> b -> String }
         instance D Int b where ...
         instance D a Int where ...

         g (x::Int) = opD x x  -- Wanted: D Int Int

 For such classes this should work (without having to add an "instance D
 Int
 Int", and using -XOverlappingInstances, which would then work). This is
 what
 -XIncoherentInstances is for: Telling GHC "I don't care which instance you
 use;
 if you can use one, use it."

 Should this logic only work when *all* candidates have the incoherent
 flag, or
 even when all but one have it? The right choice is the latter, which can
 be
 justified by comparing the behaviour with how -XIncoherentInstances worked
 when
 it was only about the unify-check (note [Overlapping instances]):

 Example:
         class C a b c where foo :: (a,b,c)
         instance C [a] b Int
         instance [incoherent] [Int] b c
         instance [incoherent] C a Int c
 Thanks to the incoherent flags,
         [Wanted]  C [a] b Int
 works: Only instance one matches, the others just unify, but are marked
 incoherent.

 So I can write
         (foo :: ([a],b,Int)) :: ([Int], Int, Int).
 but if that works then I really want to be able to write
         foo :: ([Int], Int, Int)
 as well. Now all three instances from above match. None is more specific
 than
 another, so none is ruled out by the normal overlapping rules. One of them
 is
 not incoherent, but we still want this to compile. Hence the
 "all-but-one-logic".
 }}}
 That makes sense, but it doesn't implement the rule in the manual
 (comment:20).
 I think the last bullet should therefore read
 {{{
  * If exactly one non-incoherent candidate remains, pick it.  If all
    remaining candidates are incoherent, pick an arbitary
    one. Otherwise fail.
 }}}
 Do you agree?

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9242#comment:21>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list