[GHC] #14266: AllowAmbiguousTypes doesn't play well with default class methods

GHC ghc-devs at haskell.org
Tue Oct 3 14:03:45 UTC 2017


#14266: AllowAmbiguousTypes doesn't play well with default class methods
-------------------------------------+-------------------------------------
        Reporter:  chris-martin      |                Owner:  (none)
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.0.2
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by simonpj):

 Here's my problem.  Consider
 {{{
 class A t where
   f :: forall x m. Monoid x => t m -> m

 instance A [] where
   f :: forall m x. Semigroup x => [m] -> m
   f = blah
 }}}

 (Reminder: `SemiGroup` is a superclass of `Monoid`.)  Assume that
 `blah` really only uses `SemiGroup`.  So in this instance declaration the
 definition of `f`, and its
 type signature, are strictly more general than the
 ones required.  Just for fun I put the type arguments in a different
 order.

 So this should typecheck.  But there is some real impedance matching to
 do.  If I write it out with explicit type and dictionary applications
 it might be like this
 {{{
 inst_f :: forall m x. Semigroup x => [m] -> m
 inst_f = /\ m x. \(ds:SemiGroup x). blah<mentions ds>

 instance A [] where
   f = /\ x m. \(dm:Monoid x). inst_f @m @x (sc_select dm)
 }}}
 Here `inst_f` is the function as declared by the user in the
 instance decl.  The code in the instance decl itself I have to swizzle the
 type arguments, and do a superclass selection on the dictionary argument
 before calling `inst_f`.

 So there is work to do!  GHC has to work out how to get a `[W] SemiGroup
 x0` from
 a `[G] Monoid x`, where `x0` is unification variable.  A good guess is to
 set
 `x0 := x` but GHC's solver doesn't guess.

 Do you see the problem?

 But the programmer says "I wasn't doing any of this more-general-type
 nonsense. I wrote down precisely the instantiated type so it's ''obvious''
 how to match things up".  And that seems like a reasonable observation.

 I suppose that we could say that when the instantiated method type
 ''precisely matches'' the user-specified signature, then we just match
 things up in the obvious way.  That seems like a very ''ad hoc'' hack.
 But I can't see any other way.  Any other ideas?

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


More information about the ghc-tickets mailing list