[GHC] #13879: Strange interaction between higher-rank kinds and type synonyms

#13879: Strange interaction between higher-rank kinds and type synonyms -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Type checker) | Keywords: TypeInType | Operating System: Unknown/Multiple Architecture: | Type of failure: GHC rejects Unknown/Multiple | valid program Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Here's some code with typechecks with GHC 8.0.1, 8.0.2, 8.2.1, and HEAD: {{{#!hs {-# LANGUAGE GADTs #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeInType #-} {-# LANGUAGE TypeOperators #-} module Bug where import Data.Kind data family Sing (a :: k) data (a :: j) :~~: (b :: k) where HRefl :: a :~~: a data instance Sing (z :: a :~~: b) where SHRefl :: Sing HRefl (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> p HRefl -> p r (%:~~:->) SHRefl pHRefl = pHRefl type App f x = f x type HRApp (f :: forall (z :: Type) (y :: z). a :~~: y -> Type) (x :: a :~~: b) = f x }}} Now I want to refactor this so that I use `App`: {{{#!hs (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> App p HRefl -> App p r }}} However, GHC doesn't like that: {{{ $ /opt/ghc/8.2.1/bin/ghci Bug.hs -fprint-explicit-kinds -fprint-explicit- foralls GHCi, version 8.2.0.20170623: http://www.haskell.org/ghc/ :? for help Loaded GHCi configuration from /home/rgscott/.ghci [1 of 1] Compiling Bug ( Bug.hs, interpreted ) Bug.hs:21:20: error: • Expected kind ‘(:~~:) j z a a’, but ‘'HRefl j a’ has kind ‘(:~~:) j j a a’ • In the second argument of ‘App’, namely ‘HRefl’ In the type signature: (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> App p HRefl -> App p r | 21 | -> App p HRefl | ^^^^^ Bug.hs:22:20: error: • Expected kind ‘(:~~:) j z a b’, but ‘r’ has kind ‘(:~~:) j k a b’ • In the second argument of ‘App’, namely ‘r’ In the type signature: (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> App p HRefl -> App p r | 22 | -> App p r | ^ }}} These errors are surprising to me, since it appears that the two higher- rank types, `z` and `y`, are behaving differently here: `y` appears to be willing to unify with other types in applications of `p`, but `z` isn't! I would expect //both// to be willing to unify in applications of `p`. Out of desperation, I tried this other formulation of `(%:~~:->)` which uses `HRApp` instead of `App`: {{{#!hs (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> HRApp p HRefl -> HRApp p r }}} But now I get an even stranger error message: {{{ $ /opt/ghc/8.2.1/bin/ghci Bug.hs -fprint-explicit-kinds -fprint-explicit- foralls GHCi, version 8.2.0.20170623: http://www.haskell.org/ghc/ :? for help Loaded GHCi configuration from /home/rgscott/.ghci [1 of 1] Compiling Bug ( Bug.hs, interpreted ) Bug.hs:21:20: error: • Expected kind ‘forall z (y :: z). (:~~:) j z a y -> *’, but ‘p’ has kind ‘forall z (y :: z). (:~~:) j z a y -> *’ • In the first argument of ‘HRApp’, namely ‘p’ In the type signature: (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> HRApp p HRefl -> HRApp p r | 21 | -> HRApp p HRefl | ^ Bug.hs:21:20: error: • Expected kind ‘forall z (y :: z). (:~~:) j z a y -> *’, but ‘p’ has kind ‘forall z (y :: z). (:~~:) j z a y -> *’ • In the first argument of ‘HRApp’, namely ‘p’ In the type signature: (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> HRApp p HRefl -> HRApp p r | 21 | -> HRApp p HRefl | ^ Bug.hs:22:20: error: • Expected kind ‘forall z (y :: z). (:~~:) j z a y -> *’, but ‘p’ has kind ‘forall z (y :: z). (:~~:) j z a y -> *’ • In the first argument of ‘HRApp’, namely ‘p’ In the type signature: (%:~~:->) :: forall (j :: Type) (k :: Type) (a :: j) (b :: k) (r :: a :~~: b) (p :: forall (z :: Type) (y :: z). a :~~: y -> Type). Sing r -> HRApp p HRefl -> HRApp p r | 22 | -> HRApp p r | ^ }}} That's right, it can't match the kinds: * `forall z (y :: z). (:~~:) j z a y -> *`, and * `forall z (y :: z). (:~~:) j z a y -> *` Uh... what? Those are the same kind! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13879 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13879: Strange interaction between higher-rank kinds and type synonyms -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler (Type | Version: 8.0.1 checker) | Resolution: | Keywords: TypeInType Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by simonpj): I could not resist investigating. Here are my conclusions. * With a DEBUG compiler, I immediately tripped over {{{ ASSERT failed! in_scope InScope {z_aUa y_aUb} tenv [aSo :-> z0_aUa[tau:1], aSp :-> y0_aUb[tau:1]] cenv [] tys [(:~~:) j0_aU1[tau:1] k0_aU2[tau:1] a_aSl[sk:1] y_aSp[sk:1] -> *] cos [] needInScope [aSj :-> j_aSj[sk:1], aSl :-> a_aSl[sk:1], aU1 :-> j_aU1[tau:1], aU2 :-> k_aU2[tau:1]] Call stack: CallStack (from HasCallStack): prettyCurrentCallStack, called at compiler\utils\Outputable.hs:1133:58 in ghc:Outputable callStackDoc, called at compiler\utils\Outputable.hs:1188:22 in ghc:Outputable assertPprPanic, called at compiler\types\TyCoRep.hs:2099:51 in ghc:TyCoRep checkValidSubst, called at compiler\types\TyCoRep.hs:2122:29 in ghc:TyCoRep substTy, called at compiler\typecheck\TcHsType.hs:936:27 in ghc:TcHsType }}} This was just a missing in-scope set setup in `TcHsType.instantiateTyN`. Easy to fix. * Next up, the original program. The bug here was that in a type signature like {{{ f :: forall (p :: forall z (y::z). <blah>). <more blah> }}} when instanting p's kind at occurrences of p in <more blah> it's crucial that the kind we instantiate is fully zonked, else we may fail to substitute properly. But `tcLHsKind` (which converts the `LHsKind` to a `Kind` didn't zonk its result. Also easily fixed, and that makes the original program work. * Next, the mysterious message when you use `HRApp`. This arises because we try to unify the kinds {{{ forall z1 (y1::z1). HR a1 b1 c1 d1 ~# forall z2 (y2::z2). HR a2 b2 c2 d2 }}} (I'm using `HR` instead of infix `:~~:`, just to keep my head straight.) The first thing is that, at least by the time the constraint solver gets it, if we zonked LHS and RHS we'd see they were equal. But that's a small thing. What we do in `TcCanonical` is this: {{{ can_eq_nc' _flat _rdr_env _envs ev eq_rel s1@(ForAllTy {}) _ s2@(ForAllTy {}) _ | CtWanted { ctev_loc = loc, ctev_dest = orig_dest } <- ev = do { let (bndrs1,body1) = tcSplitForAllTyVarBndrs s1 (bndrs2,body2) = tcSplitForAllTyVarBndrs s2 ; kind_cos <- zipWithM (unifyWanted loc Nominal) (map binderKind bndrs1) (map binderKind bndrs2) ; all_co <- deferTcSForAllEq (eqRelRole eq_rel) loc kind_cos (bndrs1,body1) (bndrs2,body2) ; setWantedEq orig_dest all_co ; stopWith ev "Deferred polytype equality" } }}} Look at that `zipWithM`. It's obvously bogus in the case above, because we produce a constraint `z1~z2` which is insoluble. I need Richard's help here; but I think we just need to spit out that kind equality ''inside'' the new implication constraint, and after the alpha-renaming, rather than outside and before. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13879#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13879: Strange interaction between higher-rank kinds and type synonyms
-------------------------------------+-------------------------------------
Reporter: RyanGlScott | Owner: (none)
Type: bug | Status: new
Priority: normal | Milestone:
Component: Compiler (Type | Version: 8.0.1
checker) |
Resolution: | Keywords: TypeInType
Operating System: Unknown/Multiple | Architecture:
Type of failure: GHC rejects | Unknown/Multiple
valid program | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Simon Peyton Jones

#13879: Strange interaction between higher-rank kinds and type synonyms
-------------------------------------+-------------------------------------
Reporter: RyanGlScott | Owner: (none)
Type: bug | Status: new
Priority: normal | Milestone:
Component: Compiler (Type | Version: 8.0.1
checker) |
Resolution: | Keywords: TypeInType
Operating System: Unknown/Multiple | Architecture:
Type of failure: GHC rejects | Unknown/Multiple
valid program | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Simon Peyton Jones

#13879: Strange interaction between higher-rank kinds and type synonyms
-------------------------------------+-------------------------------------
Reporter: RyanGlScott | Owner: (none)
Type: bug | Status: new
Priority: normal | Milestone:
Component: Compiler (Type | Version: 8.0.1
checker) |
Resolution: | Keywords: TypeInType
Operating System: Unknown/Multiple | Architecture:
Type of failure: GHC rejects | Unknown/Multiple
valid program | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Simon Peyton Jones

#13879: Strange interaction between higher-rank kinds and type synonyms -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: (none) Type: bug | Status: merge Priority: normal | Milestone: Component: Compiler (Type | Version: 8.0.1 checker) | Resolution: | Keywords: TypeInType Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | typecheck/should_compile/T13879 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by simonpj): * testcase: => typecheck/should_compile/T13879 * status: new => merge Comment: All done! Probably worth merging these patches; I'm pretty sure they won't trigger new bugs. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13879#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13879: Strange interaction between higher-rank kinds and type synonyms -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: (none) Type: bug | Status: merge Priority: normal | Milestone: 8.2.1 Component: Compiler (Type | Version: 8.0.1 checker) | Resolution: | Keywords: TypeInType Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | typecheck/should_compile/T13879 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * milestone: => 8.2.1 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13879#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13879: Strange interaction between higher-rank kinds and type synonyms -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: 8.2.1 Component: Compiler (Type | Version: 8.0.1 checker) | Resolution: fixed | Keywords: TypeInType Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: GHC rejects | Test Case: valid program | typecheck/should_compile/T13879 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: merge => closed * resolution: => fixed Comment: Merged in c1de0758157097c47de2787c46030174744422b6, 3fedc0fb5820243a1f47c883bb76b829a4a24c85, and 7d9ca50e184f2bc5da531646673fa7eecfd39862. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13879#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC