[GHC] #16322: "deriving newtype instance" generates an infinite loop

GHC ghc-devs at haskell.org
Fri Feb 15 18:02:28 UTC 2019


#16322: "deriving newtype instance" generates an infinite loop
-------------------------------------+-------------------------------------
        Reporter:  paf31             |                Owner:  (none)
            Type:  bug               |               Status:  infoneeded
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.6.3
      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 paf31):

 This loops in the REPL:


 {{{
 :set -XDerivingStrategies
 :set -XFlexibleInstances
 :set -XFunctionalDependencies
 :set -XGeneralizedNewtypeDeriving
 :set -XMultiParamTypeClasses
 :set -XStandaloneDeriving
 :set -XUndecidableInstances

 import Data.Proxy

 class C a b | a -> b, b -> a where c :: Proxy a -> Int
 instance C Int Int where c _ = 1

 newtype X a b = X { unX :: b }
 deriving newtype instance C a b => C (X String a) (X String b)
 }}}

 when I evaluate

 {{{
 > c (Proxy :: Proxy (X String Int))
 }}}

 Most likely `UndecidableInstances` is the culprit here, since I can't
 derive the instance otherwise, but it seems like it should be possible to
 derive anyway.

 `-ddump-deriv` shows this:

 {{{
 Derived class instances:
   instance Ghci1.C a b =>
            Ghci1.C
              (Ghci3.X GHC.Base.String a) (Ghci3.X GHC.Base.String b) where
     Ghci1.c
       = GHC.Prim.coerce
           @((Data.Proxy.Proxy (Ghci3.X GHC.Base.String a_a1Ei) :: TYPE
 GHC.Types.LiftedRep)
             -> GHC.Types.Int)
           @((Data.Proxy.Proxy (Ghci3.X GHC.Base.String a_a1Ei) :: TYPE
 GHC.Types.LiftedRep)
             -> GHC.Types.Int)
           Ghci1.c ::
           (Data.Proxy.Proxy (Ghci3.X GHC.Base.String a_a1Ei) :: TYPE
 GHC.Types.LiftedRep)
           -> GHC.Types.Int
 }}}

 which identifies the issue: GHC is not coercing away the newtype, but
 deriving an identity coercion.

 Apologies again if I'm missing something obvious here.

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


More information about the ghc-tickets mailing list