[GHC] #15791: typeKind confuses Type and Constraint

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- The `typeKind` function takes a type and reports its kind. It gets this wrong, sometimes: if you ask for `typeKind` of the quantified constraint `Eq a => C a` (where `C` is a class), you get `Type`. This is wrong, though I don't have a concrete case where GHC misfires. We ''could'' fix `typeKind` to get this right, but doing so would require making the `FunTy` case recursive (right now, it always returns `Type`). This is wasteful, because most clients of `typeKind` do not care about the distinction between `Type` and `Constraint`. Instead, I propose making a new `tcTypeKind` which behaves correctly in this case. We could also muse about making `tcTypeKind` monadic so that it can look through unification variables. This might also simplify the story around `Note [The tcType invariant]`. Regardless of the "make monadic" idea, I claim that, when this is done, we should have `isPredTy` be functionally equivalent to `(== Constraint) . tcTypeKind`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 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 simonpj): I tried this, but got stuck here, in `GHC.Err`: {{{ undefined :: forall (r :: RuntimeRep). forall (a :: TYPE r). HasCallStack => a }}} What is the kind of `undefined`'s type? With `typeKind` we get `Type`. But with `tcTypeKind` we dive into the body of the `=>`, and take the kind of `a`; but alas that mentions the universally quantified `r`, so we get a panic. And indeed the type `forall r (a::TYPE r). a` is indeed ill-formed. But with the `HasCallStack` it is actually fine. What to do? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 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 simonpj): I've pushed my work in progress to `wip/T15791` -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Branch: Wiki Page: | wip/T15791 -------------------------------------+------------------------------------- Changes (by simonpj): * differential: => Branch: wip/T15791 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Branch: Wiki Page: | wip/T15791 -------------------------------------+------------------------------------- Comment (by goldfire): Here are the typing rules, as I see it: {{{ t1 : Constraint t2 : TYPE rep -- rep is a metavariable ---------------- t1 => t2 : Type t1 : Constraint t2 : Constraint --------------------- t1 => t2 : Constraint ty : TYPE rep `a` is not free in rep ----------------------- forall a. ty : TYPE rep ty : Constraint ------------------------- forall a. ty : Constraint }}} The rules might be missing some side conditions for type safety, etc., but this should be enough to get `typeKind` working. Specifically: because the kind of `a` is `TYPE blah`, then `HasCallStack => a` has kind `Type`. Maybe you were thinking that `t1 => t2` has the kind of `t2`? That wouldn't work. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Branch: Wiki Page: | wip/T15791 -------------------------------------+------------------------------------- Comment (by simonpj):
Maybe you were thinking that t1 => t2 has the kind of t2? That wouldn't work.
Yes, that's what the code currently says. OK; now I think I see what to do. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Branch: Wiki Page: | wip/T15791 -------------------------------------+------------------------------------- Comment (by simonpj): Fixed by this commit (though I put the wrong ticket number in the commit message). In [changeset:"03d4852658e1b7407abb4da84b1b03bfa6f6db3b/ghc" 03d48526/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="03d4852658e1b7407abb4da84b1b03bfa6f6db3b" Introduce tcTypeKind, and use it In the type checker Constraint and * are distinct; and the function that takes the kind of a type should respect that distinction (Trac #15971). This patch implements the change: * Introduce Type.tcTypeKind, and use it throughout the type inference engine * Add new Note [Kinding rules for types] for the kinding rules, especially for foralls. * Redefine isPredTy ty = tcIsConstraintKind (tcTypeKind ty) (it had a much more complicated definition before) Some miscellaneous refactoring * Get rid of TyCoRep.isTYPE, Kind.isTYPEApp, in favour of TyCoRep.kindRep, kindRep_maybe * Rename Type.getRuntimeRepFromKind_maybe to getRuntimeRep_maybe I did some spot-checks on compiler perf, and it really doesn't budge (as expected). }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15791: typeKind confuses Type and Constraint -------------------------------------+------------------------------------- Reporter: goldfire | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.6.1 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Branch: Wiki Page: | wip/T15791 -------------------------------------+------------------------------------- Changes (by simonpj): * status: new => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15791#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC