Opened 6 years ago

Closed 6 years ago

#5287 closed bug (fixed)

The 'impossible' happened. solveDerivEqns: probable loop

Reported by: agocorona Owned by: simonpj
Priority: normal Milestone: 7.4.1
Component: Compiler Version: 7.0.3
Keywords: solveDerivEqns Cc: gale@…
Operating System: Windows Architecture: x86
Type of failure: Compile-time crash Test Case: deriving/should_fail/T5287
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by igloo)

This code:

{-# LANGUAGE   FlexibleInstances, UndecidableInstances
               , MultiParamTypeClasses

class Serializable a b 

class IResource a     --The rest of the instance definitions does not matter for the error

instance  Serializable a b => IResource a 

data DBRef a=  DBRef String   a

instance  (IResource a) => Read (DBRef a) 

data   Votation a= Votation{
  content         :: DBRef a
} deriving (Read)

gives the following error at compilation time:

tests>runghc impossiblelloop.hs
ghc: panic! (the 'impossible' happened)
  (GHC version 7.0.3 for i386-unknown-mingw32):
        solveDerivEqns: probable loop
    (impossiblelloop.hs:20:13-16 main:Main.$fReadVotation{v rhI} [a{tv abB} [tv]
] base:GHC.Read.Read{tc 2d} [main:Main.Votation{tc rbo}

                               a{tv abB} [tv]] = [base:GHC.Read.Read{tc 2d}

                                                    (main:Main.DBRef{tc rbu}

                                                       a{tv abB} [tv])])
    [[main:Main.Serializable{tc rbA} a{tv abB} [tv] b{tv ajE} [tcs]]]

Please report this as a GHC bug:

Change History (10)

comment:1 Changed 6 years ago by igloo

Description: modified (diff)

comment:2 Changed 6 years ago by YitzGale

I think this is a minimal case:

{-# LANGUAGE MultiParamTypeClasses, UndecidableInstances #-}
module Bug where
class A a oops
data D d = D d
instance A a oops => Read (D a)
data E e = E (D e) deriving Read

What exactly should it mean when there are free type variables as parameters of a multi-parameter class in the superclass context of an instance declaration?


instance A a oops => B (D a)

should mean something like

-- Illegal instance syntax
instance (forall oops. A a oops => B (D a))

Has this ever been specified anywhere?

Haskell 98 seems to allow free type variables in the instance head for single-parameter type classes, but there you can just ignore those parts of the context.

comment:3 Changed 6 years ago by YitzGale

Cc: gale@… added

comment:4 Changed 6 years ago by YitzGale

Discussion thread:

Daniel Fischer reports:

  • The bug also occurs when deriving Eq and Show
  • The bug also occurs in 7.0.2 and 7.0.4.
  • In 6.12.3, GHC reports: "Can't derive instances where the instance context mentions type variables that are not data type parameters".

comment:5 Changed 6 years ago by simonpj

Owner: set to simonpj

comment:6 Changed 6 years ago by simonpj

I'm on this, but it'll have to wait till I'm back from holiday.

comment:7 Changed 6 years ago by igloo

Milestone: 7.4.1

comment:8 Changed 6 years ago by simonpj@…

commit 49861e71e3873bafddbd7b0c21041a8b7902af4b

Author: Simon Peyton Jones <>
Date:   Sat Jul 23 18:30:46 2011 +0100

    Reject bad 'deriving' directives (fixes Trac #5287)
    The 'deriving' mechanism that infers the context for
    an instance declarations was going into a loop, as a
    result of an instance like
           instance C a b => D [a]
    where the 'b' isn't mentioned in the head.
    This patch identifies those cases.  I also needed to make
    TcErrors generate a suitable error message.  On the way
    I improved the reporting of "ambiguous" variables;
    for example arrowfail001 now generates a better message.

 compiler/typecheck/TcDeriv.lhs    |   22 +--------
 compiler/typecheck/TcErrors.lhs   |   95 ++++++++++++++++++-------------------
 compiler/typecheck/TcMType.lhs    |   38 ++++++++++-----
 compiler/typecheck/TcSimplify.lhs |    5 +-
 compiler/typecheck/TcType.lhs     |   20 ++++++--
 5 files changed, 91 insertions(+), 89 deletions(-)

comment:9 Changed 6 years ago by simonpj

Status: newmerge
Test Case: deriving/should_fail/T5287

Done. Thanks for identifying this error.

Ideally, merge into 7.2, but no big deal


comment:10 Changed 6 years ago by igloo

Resolution: fixed
Status: mergeclosed

Not merged

Note: See TracTickets for help on using tickets.