Opened 5 months ago

Closed 4 months ago

#12698 closed bug (fixed)

GHC panic on pattern synonym

Reported by: Iceland_jack Owned by:
Priority: normal Milestone: 8.0.2
Component: Compiler Version: 8.1
Keywords: PatternSynonyms Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

{-# Language ViewPatterns, TypeOperators, KindSignatures, PolyKinds, TypeInType, StandaloneDeriving, GADTs, RebindableSyntax, RankNTypes, LambdaCase, PatternSynonyms, TypeApplications #-}

import GHC.Types
import Prelude hiding ( fromInteger )
import Data.Type.Equality
import Data.Kind
import qualified Prelude

class Ty (a :: k) where ty :: T a
instance Ty Int where ty = TI
instance Ty Bool where ty = TB
instance Ty a => Ty [a] where ty = TA TL ty
instance Ty [] where ty = TL
instance Ty (,) where ty = TP

data AppResult (t :: k) where
  App :: T a -> T b -> AppResult (a b)

data T :: forall k. k -> Type where
  TI :: T Int
  TB :: T Bool
  TL :: T []
  TP :: T (,)
  TA :: T f -> T x -> T (f x)
deriving instance Show (T a)

splitApp :: T a -> Maybe (AppResult a)
splitApp = \case
  TI -> Nothing
  TB -> Nothing
  TL -> Nothing
  TP -> Nothing
  TA f x -> Just (App f x)

data (a :: k1) :~~: (b :: k2) where
  HRefl :: a :~~: a

eqT :: T a -> T b -> Maybe (a :~~: b)
eqT a b =
  case (a, b) of
    (TI, TI) -> Just HRefl
    (TB, TB) -> Just HRefl
    (TL, TL) -> Just HRefl
    (TP, TP) -> Just HRefl

pattern List :: () => [] ~~ b => T b
pattern List <- (eqT (ty @(Type -> Type) @[]) -> Just HRefl)
  where List = ty

pattern Int :: () => Int ~~ b => T b
pattern Int <- (eqT (ty @Type @Int) -> Just HRefl)
  where Int = ty

pattern (:<->:) :: () => fx ~ f x => T f -> T x -> T fx
pattern f :<->: x <- (splitApp -> Just (App f x))
  where f :<->: x = TA f x

pattern Foo <- List :<->: Int

Using GHCi version 8.1.20160930:

G$ ./ghc-stage2 -ignore-dot-ghci --interactive /tmp/td2W.hs 
GHCi, version 8.1.20160930: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( /tmp/td2W.hs, interpreted )
ghc-stage2: panic! (the 'impossible' happened)
  (GHC version 8.1.20160930 for x86_64-unknown-linux):
        ASSERT failed!
  k1_a2mO
  k1_a2mO
  [TCvSubst
     In scope: InScope {k_a2mM fx_a2mN f_a2mP x_a2mQ rep_a2ny r_a2nz
                        scrut_a2nB $cshowsPrec_a2ob $cshow_a2p1 $cshowList_a2p8 $cty_a2pi
                        $cty_a2po $cty_a2pw $cty_a2pJ $cty_a2pP splitApp eqT $fTy(->)(,)
                        $fTy(->)[] $fTyTYPE[] $fTyTYPEBool $fTyTYPEInt $fShowT $mList
                        $bList $mInt $bInt $m:<->: $b:<->:}
     Type env: [a2mN :-> fx_a2mN, a2mP :-> f_a2mP, a2mQ :-> x_a2mQ,
                a2nz :-> r_a2nz]
     Co env: []]
  Call stack:
      CallStack (from HasCallStack):
        prettyCurrentCallStack, called at compiler/utils/Outputable.hs:1076:58 in ghc:Outputable
        callStackDoc, called at compiler/utils/Outputable.hs:1125:22 in ghc:Outputable
        assertPprPanic, called at compiler/types/TyCoRep.hs:2318:46 in ghc:TyCoRep
  Call stack:
      CallStack (from HasCallStack):
        prettyCurrentCallStack, called at compiler/utils/Outputable.hs:1076:58 in ghc:Outputable
        callStackDoc, called at compiler/utils/Outputable.hs:1080:37 in ghc:Outputable
        pprPanic, called at compiler/utils/Outputable.hs:1123:5 in ghc:Outputable
        assertPprPanic, called at compiler/types/TyCoRep.hs:2318:46 in ghc:TyCoRep

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

> 

Change History (4)

comment:1 Changed 5 months ago by Iceland_jack

Works if supplied with type signature

pattern Foo :: () => [Int] ~~ t => T t
pattern Foo = List :<->: Int

comment:2 Changed 4 months ago by Simon Peyton Jones <simonpj@…>

In a693d1cb/ghc:

Correct order of existentials in pattern synonyms

Trac #12698 exposed a nasty bug in the typechecking for
pattern synonmys: the existential type variables weren't
being put in properly-scoped order.

For some reason TcPatSyn.tcCollectEx was colleting them as a
set, not as a list!  Easily fixed.

comment:3 Changed 4 months ago by simonpj

Status: newmerge

Merge to 8.0 if poss.

comment:4 Changed 4 months ago by bgamari

Milestone: 8.0.2
Resolution: fixed
Status: mergeclosed

Merged to ghc-8.0 as 5c02b842fa64bb06766d4e89615af84bc4db992b.

That being said, I am strangely unable to reproduce the reported crash with 8.0.1.

Note: See TracTickets for help on using tickets.