[commit: ghc] master: Taming the Kind Inference Monster (2257a86)

git at git.haskell.org git at git.haskell.org
Thu Nov 29 17:28:14 UTC 2018


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/2257a86daa72db382eb927df12a718669d5491f8/ghc

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

commit 2257a86daa72db382eb927df12a718669d5491f8
Author: Simon Peyton Jones <simonpj at microsoft.com>
Date:   Wed Nov 28 16:06:15 2018 +0000

    Taming the Kind Inference Monster
    
    My original goal was (Trac #15809) to move towards using level numbers
    as the basis for deciding which type variables to generalise, rather
    than searching for the free varaibles of the environment.  However
    it has turned into a truly major refactoring of the kind inference
    engine.
    
    Let's deal with the level-numbers part first:
    
    * Augment quantifyTyVars to calculate the type variables to
      quantify using level numbers, and compare the result with
      the existing approach.  That is; no change in behaviour,
      just a WARNing if the two approaches give different answers.
    
    * To do this I had to get the level number right when calling
      quantifyTyVars, and this entailed a bit of care, especially
      in the code for kind-checking type declarations.
    
    * However, on the way I was able to eliminate or simplify
      a number of calls to solveEqualities.
    
    This work is incomplete: I'm not /using/ level numbers yet.
    When I subsequently get rid of any remaining WARNings in
    quantifyTyVars, that the level-number answers differ from
    the current answers, then I can rip out the current
    "free vars of the environment" stuff.
    
    Anyway, this led me into deep dive into kind inference for type and
    class declarations, which is an increasingly soggy part of GHC.
    Richard already did some good work recently in
    
       commit 5e45ad10ffca1ad175b10f6ef3327e1ed8ba25f3
       Date:   Thu Sep 13 09:56:02 2018 +0200
    
        Finish fix for #14880.
    
        The real change that fixes the ticket is described in
        Note [Naughty quantification candidates] in TcMType.
    
    but I kept turning over stones. So this patch has ended up
    with a pretty significant refactoring of that code too.
    
    Kind inference for types and classes
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    * Major refactoring in the way we generalise the inferred kind of
      a TyCon, in kcTyClGroup.  Indeed, I made it into a new top-level
      function, generaliseTcTyCon.  Plus a new Note to explain it
      Note [Inferring kinds for type declarations].
    
    * We decided (Trac #15592) not to treat class type variables specially
      when dealing with Inferred/Specified/Required for associated types.
      That simplifies things quite a bit. I also rewrote
      Note [Required, Specified, and Inferred for types]
    
    * Major refactoring of the crucial function kcLHsQTyVars:
      I split it into
           kcLHsQTyVars_Cusk  and  kcLHsQTyVars_NonCusk
      because the two are really quite different. The CUSK case is
      almost entirely rewritten, and is much easier because of our new
      decision not to treat the class variables specially
    
    * I moved all the error checks from tcTyClTyVars (which was a bizarre
      place for it) into generaliseTcTyCon and/or the CUSK case of
      kcLHsQTyVars.  Now tcTyClTyVars is extremely simple.
    
    * I got rid of all the all the subtleties in tcImplicitTKBndrs. Indeed
      now there is no difference between tcImplicitTKBndrs and
      kcImplicitTKBndrs; there is now a single bindImplicitTKBndrs.
      Same for kc/tcExplicitTKBndrs.  None of them monkey with level
      numbers, nor build implication constraints.  scopeTyVars is gone
      entirely, as is kcLHsQTyVarBndrs. It's vastly simpler.
    
      I found I could get rid of kcLHsQTyVarBndrs entirely, in favour of
      the bnew bindExplicitTKBndrs.
    
    Quantification
    ~~~~~~~~~~~~~~
    * I now deal with the "naughty quantification candidates"
      of the previous patch in candidateQTyVars, rather than in
      quantifyTyVars; see Note [Naughty quantification candidates]
      in TcMType.
    
      I also killed off closeOverKindsCQTvs in favour of the same
      strategy that we use for tyCoVarsOfType: namely, close over kinds
      at the occurrences.
    
      And candidateQTyVars no longer needs a gbl_tvs argument.
    
    * Passing the ContextKind, rather than the expected kind itself,
      to tc_hs_sig_type_and_gen makes it easy to allocate the expected
      result kind (when we are in inference mode) at the right level.
    
    Type families
    ~~~~~~~~~~~~~~
    * I did a major rewrite of the impenetrable tcFamTyPats. The result
      is vastly more comprehensible.
    
    * I got rid of kcDataDefn entirely, quite a big function.
    
    * I re-did the way that checkConsistentFamInst works, so
      that it allows alpha-renaming of invisible arguments.
    
    * The interaction of kind signatures and family instances is tricky.
        Type families: see Note [Apparently-nullary families]
        Data families: see Note [Result kind signature for a data family instance]
                       and Note [Eta-reduction for data families]
    
    * The consistent instantation of an associated type family is tricky.
      See Note [Checking consistent instantiation] and
          Note [Matching in the consistent-instantation check]
      in TcTyClsDecls.  It's now checked in TcTyClsDecls because that is
      when we have the relevant info to hand.
    
    * I got tired of the compromises in etaExpandFamInst, so I did the
      job properly by adding a field cab_eta_tvs to CoAxBranch.
      See Coercion.etaExpandCoAxBranch.
    
    tcInferApps and friends
    ~~~~~~~~~~~~~~~~~~~~~~~
    * I got rid of the mysterious and horrible ClsInstInfo argument
      to tcInferApps, checkExpectedKindX, and various checkValid
      functions.  It was horrible!
    
    * I got rid of [Type] result of tcInferApps.  This list was used
      only in tcFamTyPats, when checking the LHS of a type instance;
      and if there is a cast in the middle, the list is meaningless.
      So I made tcInferApps simpler, and moved the complexity
      (not much) to tcInferApps.
    
      Result: tcInferApps is now pretty comprehensible again.
    
    * I refactored the many function in TcMType that instantiate skolems.
    
    Smaller things
    
    * I rejigged the error message in checkValidTelescope; I think it's
      quite a bit better now.
    
    * checkValidType was not rejecting constraints in a kind signature
         forall (a :: Eq b => blah). blah2
      That led to further errors when we then do an ambiguity check.
      So I make checkValidType reject it more aggressively.
    
    * I killed off quantifyConDecl, instead calling kindGeneralize
      directly.
    
    * I fixed an outright bug in tyCoVarsOfImplic, where we were not
      colleting the tyvar of the kind of the skolems
    
    * Renamed ClsInstInfo to AssocInstInfo, and made it into its
      own data type
    
    * Some fiddling around with pretty-printing of family
      instances which was trickier than I thought.  I wanted
      wildcards to print as plain "_" in user messages, although
      they each need a unique identity in the CoAxBranch.
    
    Some other oddments
    
    * Refactoring around the trace messages from reportUnsolved.
    * A bit of extra tc-tracing in TcHsSyn.commitFlexi
    
    This patch fixes a raft of bugs, and includes tests for them.
    
     * #14887
     * #15740
     * #15764
     * #15789
     * #15804
     * #15817
     * #15870
     * #15874
     * #15881


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

2257a86daa72db382eb927df12a718669d5491f8
 compiler/basicTypes/DataCon.hs                     |    3 +-
 compiler/basicTypes/OccName.hs                     |   34 +-
 compiler/basicTypes/VarEnv.hs                      |    5 +-
 compiler/coreSyn/CoreLint.hs                       |    5 +-
 compiler/hsSyn/HsDecls.hs                          |   62 +-
 compiler/hsSyn/HsTypes.hs                          |   68 +-
 compiler/iface/IfaceSyn.hs                         |   47 +-
 compiler/iface/MkIface.hs                          |   33 +-
 compiler/iface/TcIface.hs                          |   22 +-
 compiler/nativeGen/CFG.hs                          |    1 -
 compiler/parser/RdrHsSyn.hs                        |    8 +-
 compiler/prelude/TysPrim.hs                        |   19 +-
 compiler/typecheck/ClsInst.hs                      |   28 +-
 compiler/typecheck/FamInst.hs                      |   44 +-
 compiler/typecheck/Inst.hs                         |   31 +-
 compiler/typecheck/TcBackpack.hs                   |    2 +-
 compiler/typecheck/TcBinds.hs                      |   24 -
 compiler/typecheck/TcClassDcl.hs                   |    5 +-
 compiler/typecheck/TcDeriv.hs                      |   56 +-
 compiler/typecheck/TcDerivInfer.hs                 |    2 +-
 compiler/typecheck/TcEnv.hs                        |   10 +-
 compiler/typecheck/TcErrors.hs                     |   36 +-
 compiler/typecheck/TcGenDeriv.hs                   |    9 +-
 compiler/typecheck/TcGenGenerics.hs                |    2 +-
 compiler/typecheck/TcHsSyn.hs                      |   35 +-
 compiler/typecheck/TcHsType.hs                     | 1319 +++++++++----------
 compiler/typecheck/TcInstDcls.hs                   |  504 +++++---
 compiler/typecheck/TcMType.hs                      |  509 ++++----
 compiler/typecheck/TcPat.hs                        |    2 +-
 compiler/typecheck/TcPatSyn.hs                     |   56 +-
 compiler/typecheck/TcRnDriver.hs                   |   13 +-
 compiler/typecheck/TcRnMonad.hs                    |    6 +-
 compiler/typecheck/TcRnTypes.hs                    |   16 +-
 compiler/typecheck/TcRules.hs                      |   36 +-
 compiler/typecheck/TcSMonad.hs                     |    4 +-
 compiler/typecheck/TcSigs.hs                       |   69 +-
 compiler/typecheck/TcSimplify.hs                   |   64 +-
 compiler/typecheck/TcSplice.hs                     |   29 +-
 compiler/typecheck/TcTyClsDecls.hs                 | 1339 ++++++++++----------
 compiler/typecheck/TcUnify.hs                      |   33 +-
 compiler/typecheck/TcValidity.hs                   |  707 ++++++-----
 compiler/types/CoAxiom.hs                          |   63 +-
 compiler/types/Coercion.hs                         |  140 +-
 compiler/types/FamInstEnv.hs                       |  181 ++-
 compiler/types/TyCoRep.hs                          |   78 +-
 compiler/types/TyCon.hs                            |   15 +-
 compiler/types/Type.hs                             |  126 +-
 compiler/types/Type.hs-boot                        |    4 +-
 compiler/types/Unify.hs                            |   43 +-
 compiler/utils/FastString.hs                       |    4 +
 compiler/utils/Util.hs                             |    8 +-
 .../tests/dependent/should_compile/T15743.stderr   |    2 +-
 .../tests/dependent/should_compile/T15743e.stderr  |    8 +-
 .../dependent/should_fail/BadTelescope.stderr      |    7 +-
 .../dependent/should_fail/BadTelescope3.stderr     |    6 +-
 .../dependent/should_fail/BadTelescope4.stderr     |   13 +-
 .../tests/dependent/should_fail/T13895.stderr      |   37 +-
 .../tests/dependent/should_fail/T14066f.stderr     |    6 +-
 .../tests/dependent/should_fail/T14066g.stderr     |    8 +-
 .../tests/dependent/should_fail/T15591b.stderr     |    9 +-
 .../tests/dependent/should_fail/T15591c.stderr     |    9 +-
 .../tests/dependent/should_fail/T15743c.stderr     |   13 +-
 .../tests/dependent/should_fail/T15743d.stderr     |   13 +-
 testsuite/tests/ghci/scripts/T10059.stdout         |    6 +-
 testsuite/tests/ghci/scripts/T15591.hs             |    8 +-
 testsuite/tests/ghci/scripts/T15591.stdout         |    6 +-
 testsuite/tests/ghci/scripts/T15743b.stdout        |    2 +-
 testsuite/tests/ghci/scripts/T6018ghcifail.stderr  |    4 +-
 testsuite/tests/ghci/scripts/T7873.stderr          |    2 +-
 testsuite/tests/ghci/scripts/ghci059.stdout        |    2 +-
 .../indexed-types/should_compile/T15711.stderr     |    4 +-
 .../tests/indexed-types/should_compile/T15740a.hs  |   12 +
 .../tests/indexed-types/should_compile/T15764a.hs  |   14 +
 .../indexed-types/should_compile/T15852.stderr     |   11 +-
 .../indexed-types/should_compile/T3017.stderr      |   11 +-
 testsuite/tests/indexed-types/should_compile/all.T |    2 +
 .../should_fail/ExplicitForAllFams4a.stderr        |   12 +-
 .../should_fail/ExplicitForAllFams4b.hs            |    1 +
 .../should_fail/ExplicitForAllFams4b.stderr        |   87 +-
 .../indexed-types/should_fail/SimpleFail13.stderr  |    2 +-
 .../indexed-types/should_fail/SimpleFail2a.hs      |    2 +-
 .../indexed-types/should_fail/SimpleFail2a.stderr  |    7 +-
 .../tests/indexed-types/should_fail/SimpleFail9.hs |    4 +-
 .../indexed-types/should_fail/SimpleFail9.stderr   |    4 +-
 .../tests/indexed-types/should_fail/T10817.stderr  |    9 +-
 .../tests/indexed-types/should_fail/T10899.stderr  |    3 +-
 .../tests/indexed-types/should_fail/T11450.stderr  |    4 +-
 .../tests/indexed-types/should_fail/T12041.stderr  |   10 +-
 .../indexed-types/should_fail/T13092/T13092.stderr |    2 +-
 .../should_fail/T13092c/T13092c.stderr             |    2 +-
 .../tests/indexed-types/should_fail/T13972.hs      |    6 +
 .../tests/indexed-types/should_fail/T13972.stderr  |    7 -
 .../tests/indexed-types/should_fail/T14045a.hs     |    5 +
 .../tests/indexed-types/should_fail/T14045a.stderr |    7 -
 .../tests/indexed-types/should_fail/T14179.stderr  |    4 +-
 .../tests/indexed-types/should_fail/T14887.hs      |   14 +
 .../tests/indexed-types/should_fail/T14887.stderr  |   12 +
 .../tests/indexed-types/should_fail/T15740.hs      |   12 +
 .../tests/indexed-types/should_fail/T15740.stderr  |   11 +
 .../tests/indexed-types/should_fail/T15764.hs      |   14 +
 .../tests/indexed-types/should_fail/T15764.stderr  |   11 +
 .../tests/indexed-types/should_fail/T15870.hs      |   32 +
 .../tests/indexed-types/should_fail/T15870.stderr  |    6 +
 .../tests/indexed-types/should_fail/T7536.stderr   |    9 +-
 testsuite/tests/indexed-types/should_fail/T7938.hs |    0
 .../tests/indexed-types/should_fail/T7938.stderr   |    2 +-
 .../tests/indexed-types/should_fail/T9160.stderr   |   12 +-
 testsuite/tests/indexed-types/should_fail/all.T    |    8 +-
 .../tests/partial-sigs/should_compile/ADT.stderr   |    2 +-
 .../should_compile/DataFamilyInstanceLHS.stderr    |   10 +-
 .../partial-sigs/should_compile/Meltdown.stderr    |    2 +-
 .../NamedWildcardInDataFamilyInstanceLHS.stderr    |    9 +-
 .../NamedWildcardInTypeFamilyInstanceLHS.stderr    |    6 +-
 .../partial-sigs/should_compile/SkipMany.stderr    |    2 +-
 .../should_compile/TypeFamilyInstanceLHS.stderr    |   14 +-
 .../tests/partial-sigs/should_fail/T14040a.stderr  |    8 +-
 testsuite/tests/polykinds/T11203.stderr            |    2 +-
 testsuite/tests/polykinds/T11821a.stderr           |    2 +-
 testsuite/tests/polykinds/T12593.stderr            |    8 +-
 testsuite/tests/polykinds/T13985.hs                |    1 +
 testsuite/tests/polykinds/T13985.stderr            |   41 +-
 testsuite/tests/polykinds/T14450.stderr            |   11 +-
 testsuite/tests/polykinds/T14846.stderr            |   36 +-
 testsuite/tests/polykinds/T14887a.hs               |   16 +
 .../T9730.stderr => polykinds/T14887a.stderr}      |    0
 testsuite/tests/polykinds/T15592.stderr            |    2 +-
 testsuite/tests/polykinds/T15592b.stderr           |    4 +-
 testsuite/tests/polykinds/T15789.hs                |   10 +
 testsuite/tests/polykinds/T15789.stderr            |    6 +
 testsuite/tests/polykinds/T15804.hs                |    5 +
 testsuite/tests/polykinds/T15804.stderr            |    4 +
 testsuite/tests/polykinds/T15817.hs                |   10 +
 testsuite/tests/polykinds/T15874.hs                |   18 +
 testsuite/tests/polykinds/T15881.hs                |    8 +
 testsuite/tests/polykinds/T15881.stderr            |    5 +
 testsuite/tests/polykinds/T15881a.hs               |    8 +
 testsuite/tests/polykinds/T15881a.stderr           |    4 +
 testsuite/tests/polykinds/T8616.stderr             |    9 +
 testsuite/tests/polykinds/all.T                    |    7 +
 testsuite/tests/roles/should_compile/Roles1.stderr |   14 +-
 .../tests/roles/should_compile/Roles14.stderr      |    4 +-
 testsuite/tests/roles/should_compile/Roles2.stderr |    4 +-
 testsuite/tests/roles/should_compile/Roles3.stderr |   27 +-
 testsuite/tests/roles/should_compile/Roles4.stderr |   11 +-
 testsuite/tests/roles/should_compile/T8958.stderr  |    8 +-
 testsuite/tests/showIface/Orphans.stdout           |    4 +-
 testsuite/tests/th/TH_Roles2.stderr                |    2 +-
 .../tests/th/TH_reifyExplicitForAllFams.stderr     |    0
 .../tests/typecheck/should_compile/T12763.stderr   |    4 +-
 .../tests/typecheck/should_compile/tc231.stderr    |   10 +-
 .../typecheck/should_fail/LevPolyBounded.stderr    |    5 +
 .../tests/typecheck/should_fail/T13983.stderr      |    2 +-
 testsuite/tests/typecheck/should_fail/T14607.hs    |    2 +-
 .../tests/typecheck/should_fail/T14607.stderr      |   17 +-
 testsuite/tests/typecheck/should_fail/T2688.stderr |    6 +-
 .../tests/typecheck/should_fail/T6018fail.stderr   |    4 +-
 testsuite/tests/typecheck/should_fail/all.T        |    2 +-
 157 files changed, 3781 insertions(+), 3110 deletions(-)

Diff suppressed because of size. To see it, use:

    git diff-tree --root --patch-with-stat --no-color --find-copies-harder --ignore-space-at-eol --cc 2257a86daa72db382eb927df12a718669d5491f8


More information about the ghc-commits mailing list