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

GHC ghc-devs at haskell.org
Fri Sep 22 11:12:15 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):

 Several things to say here.

 First, the signature for `f` is deeply suspicious
 {{{
 class A t where
   f :: forall x m. Monoid x => t m -> m
 }}}
 Any call of `f` will require a `Monoid x` constraint, but does not fix
 `x` in any way. So all calls will be ambiguous unless you use visible
 type application.

 Second, Ryan's account of what GHC does with default methods is absolutely
 right.

 Third, yes, it is a bit awarkard that there doesn't seem to be a way to
 make
 this (very odd) program "work".  Ryan tried visible type application:
 {{{
 instance A [] where
   f :: forall x m. Monoid x => [m] -> m
   f = df @[] @x @m
 }}}
 But when you write a type signature in an instance declaration, you are
 free to
 make it more genreal than the one required.  For example:
 {{{
 instance Num Wombat where
   (+) :: forall a b. a -> b -> a
   (+) x y = x
 }}}
 We are obliged to provide a function of type `Wombat -> Wombat -> Wombat`,
 but we
 are perfectly free to provide a more polymorphic one.  Equivalently you
 could write
 {{{
 instance Num Wombat where
   (+) = (\x y -> x) :: forall a b. a -> b -> a
 }}}
 So GHC still has to check that the type you have supplied is more
 polymorphic
 than the one required and alas in ''that'' test you can't do visible type
 application.
 That's why Ryan found that "this just shifts the error around".  (And it
 also explains why the "top-level function" version is ok.

 It was a surprise to me that I can see no way to allow to write an
 instance when the method has a a locally-polymorphic but ambiguous
 method types. If there was a good reason to want them, maybe we should
 think about it more.  If it's just a curiousity, then it's just
 curious.

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


More information about the ghc-tickets mailing list