Opened 4 years ago

Closed 4 years ago

#8066 closed bug (fixed)

Compiler Panic When Using Instance Derivation With Singletons

Reported by: tvynr Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.6.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Compile-time crash Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


The following code causes a compiler panic:

{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances, TemplateHaskell, GADTs, TypeFamilies, DataKinds, KindSignatures #-}

module Example
( UnqualifiedTVar(..)
, QualifiedTVar(..)
, TVar(..)
, QVar
, UVar
, TQual(..)
) where

import GHC.TypeLits

import qualified Data.Set as Set

type Identifier = String
type Span = (Int,Int)

data QualForm
  = UnqualifiedTVar
  | QualifiedTVar
  deriving (Eq, Ord, Read, Show)
data instance Sing (f :: QualForm) where
  UnqualifiedTVarS :: Sing UnqualifiedTVar
  QualifiedTVarS :: Sing QualifiedTVar

instance Eq (Sing (a::QualForm)) where
  (==) = undefined
instance Ord (Sing (a::QualForm)) where
  compare = undefined
instance SingI UnqualifiedTVar where
  sing = UnqualifiedTVarS

instance SingI QualifiedTVar where
  sing = QualifiedTVarS
data TVar (a :: QualForm)
  = TVar (Sing a) Int [Span]
  deriving (Eq, Ord, Show)
type QVar = TVar QualifiedTVar
type UVar = TVar UnqualifiedTVar

data AnyVar
  = SomeQVar QVar
  | SomeUVar UVar
anyVar :: TVar (a :: QualForm) -> AnyVar
anyVar a =
  let (TVar s _ _) = a in
  case fromSing s of
    UnqualifiedTVarS -> SomeUVar a
    QualifiedTVarS -> SomeQVar a

This has been tested on GHC 7.6.1 and GHC 7.6.3; both give an error message such as

ghc: panic! (the 'impossible' happened)
  (GHC version for i386-unknown-linux):
        find_thing APromotionErr FamDataConPE

This problem seems to have something to do with instance derivation; removing the instances for Eq and Ord causes the problem to go away.

Change History (4)

comment:1 Changed 4 years ago by tvynr

Type of failure: None/UnknownCompile-time crash

comment:2 Changed 4 years ago by simonpj

The HEAD gives

    No instance for (SingE QualForm (KindOf QualForm a))
      arising from the 'deriving' clause of a data type declaration
    Possible fix:
      use a standalone 'deriving instance' declaration,
        so you can specify the instance context yourself
    When deriving the instance for (Show (TVar a))

Does that seem more reasonable? We've fixed lots of kind-related bugs in 7.6.

If the error looks ok, can we close the ticket?


comment:3 Changed 4 years ago by tvynr

That looks great, thanks. :) And for anyone else who encounters this problem or is perhaps about as confused by the TypeLits stuff as I am, adding the following fixes the panic in GHC 7.6.1 (and leads to revealing mistakes in the anyVar function):

instance SingE (Kind :: QualForm) QualForm where
  fromSing x = case x of
    UnqualifiedTVarS -> UnqualifiedTVar
    QualifiedTVarS -> QualifiedTVar
Version 0, edited 4 years ago by tvynr (next)

comment:4 Changed 4 years ago by simonpj

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.