[commit: ghc] master: Revert "Remove kind generalisation from tcRnType" (09740d5)

git at git.haskell.org git at git.haskell.org
Mon Oct 29 16:37:40 UTC 2018


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/09740d50e74ac93a0309544a62e9efc52b27adff/ghc

>---------------------------------------------------------------

commit 09740d50e74ac93a0309544a62e9efc52b27adff
Author: Richard Eisenberg <rae at cs.brynmawr.edu>
Date:   Mon Oct 29 12:15:58 2018 -0400

    Revert "Remove kind generalisation from tcRnType"
    
    This reverts commit 3a51abd04432ea3d13e4ea3c5a592f038bd57432.
    
    I had hit the wrong button when trying to validate the original
    commit... and ended up committing it prematurely instead.
    This reversion commit
    also updates the comments to explain why we kind-generalise.


>---------------------------------------------------------------

09740d50e74ac93a0309544a62e9efc52b27adff
 compiler/main/HscMain.hs         |  3 +--
 compiler/typecheck/TcRnDriver.hs | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/compiler/main/HscMain.hs b/compiler/main/HscMain.hs
index 77bcd76..9dd7507 100644
--- a/compiler/main/HscMain.hs
+++ b/compiler/main/HscMain.hs
@@ -1693,8 +1693,7 @@ hscTcExpr hsc_env0 mode expr = runInteractiveHsc hsc_env0 $ do
   parsed_expr <- hscParseExpr expr
   ioMsgMaybe $ tcRnExpr hsc_env mode parsed_expr
 
--- | Find the kind of a type
--- Currently this does *not* generalise the kinds of the type
+-- | Find the kind of a type, after generalisation
 hscKcType
   :: HscEnv
   -> Bool            -- ^ Normalise the type
diff --git a/compiler/typecheck/TcRnDriver.hs b/compiler/typecheck/TcRnDriver.hs
index 9b4565f..4fa1723 100644
--- a/compiler/typecheck/TcRnDriver.hs
+++ b/compiler/typecheck/TcRnDriver.hs
@@ -2375,7 +2375,7 @@ tcRnType :: HscEnv
          -> IO (Messages, Maybe (Type, Kind))
 tcRnType hsc_env normalise rdr_type
   = runTcInteractive hsc_env $
-    setXOptM LangExt.PolyKinds $   -- See Note [Turn on PolyKinds in tcRnType]
+    setXOptM LangExt.PolyKinds $   -- See Note [Kind-generalise in tcRnType]
     do { (HsWC { hswc_ext = wcs, hswc_body = rn_type }, _fvs)
                <- rnHsWcType GHCiCtx (mkHsWildCardBndrs rdr_type)
                   -- The type can have wild cards, but no implicit
@@ -2386,13 +2386,16 @@ tcRnType hsc_env normalise rdr_type
         -- It can have any rank or kind
         -- First bring into scope any wildcards
        ; traceTc "tcRnType" (vcat [ppr wcs, ppr rn_type])
-       ; ((ty, _), lie)  <-
+       ; ((ty, kind), lie)  <-
                        captureConstraints $
                        tcWildCardBinders wcs $ \ wcs' ->
                        do { emitWildCardHoleConstraints wcs'
                           ; tcLHsTypeUnsaturated rn_type }
        ; _ <- checkNoErrs (simplifyInteractive lie)
 
+       -- Do kind generalisation; see Note [Kind-generalise in tcRnType]
+       ; kind <- zonkTcType kind
+       ; kvs <- kindGeneralize kind
        ; ty  <- zonkTcTypeToType ty
 
        -- Do validity checking on type
@@ -2405,7 +2408,7 @@ tcRnType hsc_env normalise rdr_type
                         ; return ty' }
                 else return ty ;
 
-       ; return (ty', typeKind ty') }
+       ; return (ty', mkInvForAllTys kvs (typeKind ty')) }
 
 {- Note [TcRnExprMode]
 ~~~~~~~~~~~~~~~~~~~~~~
@@ -2465,7 +2468,7 @@ considers this example, with -fprint-explicit-foralls enabled:
   modified to include an element that is both Num and Monoid, the defaulting
   would succeed, of course.)
 
-Note [Turn on PolyKinds in tcRnType]
+Note [Kind-generalise in tcRnType]
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 We switch on PolyKinds when kind-checking a user type, so that we will
 kind-generalise the type, even when PolyKinds is not otherwise on.
@@ -2476,6 +2479,16 @@ anything unexpected, but the apparent *loss* of polymorphism, for
 types that you know are polymorphic, is quite surprising.  See Trac
 #7688 for a discussion.
 
+Note that the goal is to generalise the *kind of the type*, not
+the type itself! Example:
+  ghci> data SameKind :: k -> k -> Type
+  ghci> :k SameKind _
+
+We want to get `k -> Type`, not `Any -> Type`, which is what we would
+get without kind-generalisation. Note that `:k SameKind` is OK, as
+GHC will not instantiate SameKind here, and so we see its full kind
+of `forall k. k -> k -> Type`.
+
 ************************************************************************
 *                                                                      *
                  tcRnDeclsi



More information about the ghc-commits mailing list