Generate Typeable info at definition sites
authorBen Gamari <bgamari.foss@gmail.com>
Fri, 30 Oct 2015 19:22:42 +0000 (20:22 +0100)
committerBen Gamari <ben@smart-cactus.org>
Fri, 30 Oct 2015 19:22:44 +0000 (20:22 +0100)
commit91c6b1f54aea658b0056caec45655475897f1972
treeaeb80a04e102e51dfd41343d4f697baf34c95739
parent59e728bc0b47116e3c9a8b21b14dc3198531b9a9
Generate Typeable info at definition sites

This is the second attempt at merging D757.

This patch implements the idea floated in Trac #9858, namely that we
should generate type-representation information at the data type
declaration site, rather than when solving a Typeable constraint.

However, this turned out quite a bit harder than I expected. I still
think it's the right thing to do, and it's done now, but it was quite
a struggle.

See particularly

 * Note [Grand plan for Typeable] in TcTypeable (which is a new module)
 * Note [The overall promotion story] in DataCon (clarifies existing
stuff)

The most painful bit was that to generate Typeable instances (ie
TyConRepName bindings) for every TyCon is tricky for types in ghc-prim
etc:

 * We need to have enough data types around to *define* a TyCon
 * Many of these types are wired-in

Also, to minimise the code generated for each data type, I wanted to
generate pure data, not CAFs with unpackCString# stuff floating about.

Performance
~~~~~~~~~~~
Three perf/compiler tests start to allocate quite a bit more. This isn't
surprising, because they all allocate zillions of data types, with
practically no other code, esp. T1969

 * T1969:    GHC allocates 19% more
 * T4801:    GHC allocates 13% more
 * T5321FD:  GHC allocates 13% more
 * T9675:    GHC allocates 11% more
 * T783:     GHC allocates 11% more
 * T5642:    GHC allocates 10% more

I'm treating this as acceptable. The payoff comes in Typeable-heavy
code.

Remaining to do
~~~~~~~~~~~~~~~

 * I think that "TyCon" and "Module" are over-generic names to use for
   the runtime type representations used in GHC.Typeable. Better might
be
   "TrTyCon" and "TrModule". But I have not yet done this

 * Add more info the the "TyCon" e.g. source location where it was
   defined

 * Use the new "Module" type to help with Trac Trac #10068

 * It would be possible to generate TyConRepName (ie Typeable
   instances) selectively rather than all the time. We'd need to persist
   the information in interface files. Lacking a motivating reason I
have
   not done this, but it would not be difficult.

Refactoring
~~~~~~~~~~~
As is so often the case, I ended up refactoring more than I intended.
In particular

 * In TyCon, a type *family* (whether type or data) is repesented by a
   FamilyTyCon
     * a algebraic data type (including data/newtype instances) is
       represented by AlgTyCon This wasn't true before; a data family
       was represented as an AlgTyCon. There are some corresponding
       changes in IfaceSyn.

     * Also get rid of the (unhelpfully named) tyConParent.

 * In TyCon define 'Promoted', isomorphic to Maybe, used when things are
   optionally promoted; and use it elsewhere in GHC.

 * Cleanup handling of knownKeyNames

 * Each TyCon, including promoted TyCons, contains its TyConRepName, if
   it has one. This is, in effect, the name of its Typeable instance.

Updates haddock submodule

Test Plan: Let Harbormaster validate

Reviewers: austin, hvr, goldfire

Subscribers: goldfire, thomie

Differential Revision: https://phabricator.haskell.org/D1404

GHC Trac Issues: #9858
111 files changed:
compiler/basicTypes/ConLike.hs
compiler/basicTypes/DataCon.hs
compiler/basicTypes/OccName.hs
compiler/basicTypes/Unique.hs
compiler/coreSyn/MkCore.hs
compiler/deSugar/DsBinds.hs
compiler/deSugar/DsExpr.hs
compiler/deSugar/DsUtils.hs
compiler/ghc.cabal.in
compiler/hsSyn/HsUtils.hs
compiler/iface/BuildTyCl.hs
compiler/iface/IfaceSyn.hs
compiler/iface/MkIface.hs
compiler/iface/TcIface.hs
compiler/main/HscMain.hs
compiler/main/HscTypes.hs
compiler/prelude/PrelInfo.hs
compiler/prelude/PrelNames.hs
compiler/prelude/THNames.hs
compiler/prelude/TysPrim.hs
compiler/prelude/TysWiredIn.hs
compiler/simplCore/FloatIn.hs
compiler/typecheck/TcBinds.hs
compiler/typecheck/TcEnv.hs
compiler/typecheck/TcEvidence.hs
compiler/typecheck/TcGenGenerics.hs
compiler/typecheck/TcHsSyn.hs
compiler/typecheck/TcHsType.hs
compiler/typecheck/TcInstDcls.hs
compiler/typecheck/TcInteract.hs
compiler/typecheck/TcPatSyn.hs
compiler/typecheck/TcRnDriver.hs
compiler/typecheck/TcRnMonad.hs
compiler/typecheck/TcRnTypes.hs
compiler/typecheck/TcTyClsDecls.hs
compiler/typecheck/TcTyDecls.hs
compiler/typecheck/TcTypeNats.hs
compiler/typecheck/TcTypeable.hs [new file with mode: 0644]
compiler/types/TyCon.hs
compiler/types/Type.hs
compiler/utils/Binary.hs
compiler/vectorise/Vectorise/Generic/PData.hs
compiler/vectorise/Vectorise/Type/Env.hs
compiler/vectorise/Vectorise/Type/TyConDecl.hs
ghc/InteractiveUI.hs
libraries/base/Data/Typeable.hs
libraries/base/Data/Typeable/Internal.hs
libraries/base/GHC/Show.hs
libraries/base/GHC/Stack/Types.hs
libraries/ghc-prim/GHC/Classes.hs
libraries/ghc-prim/GHC/IntWord64.hs
libraries/ghc-prim/GHC/Magic.hs
libraries/ghc-prim/GHC/Tuple.hs
libraries/ghc-prim/GHC/Types.hs
testsuite/tests/codeGen/should_run/cgrun057.stderr
testsuite/tests/deSugar/should_compile/T2431.stderr
testsuite/tests/deriving/should_fail/T9687.stderr
testsuite/tests/ghci.debugger/scripts/T2740.stdout
testsuite/tests/ghci.debugger/scripts/break006.stderr
testsuite/tests/ghci.debugger/scripts/break009.stdout
testsuite/tests/ghci.debugger/scripts/break010.stdout
testsuite/tests/ghci.debugger/scripts/break011.stdout
testsuite/tests/ghci.debugger/scripts/break012.stdout
testsuite/tests/ghci.debugger/scripts/break018.stdout
testsuite/tests/ghci.debugger/scripts/break022/break022.stdout
testsuite/tests/ghci.debugger/scripts/break028.stdout
testsuite/tests/ghci.debugger/scripts/print018.stdout
testsuite/tests/ghci.debugger/scripts/print019.stderr
testsuite/tests/ghci.debugger/scripts/print031.stdout
testsuite/tests/ghci/scripts/T4175.stdout
testsuite/tests/ghci/scripts/T5417.stdout
testsuite/tests/ghci/scripts/T8674.stdout
testsuite/tests/indexed-types/should_compile/T3017.stderr
testsuite/tests/numeric/should_compile/T7116.stdout
testsuite/tests/overloadedlists/should_fail/overloadedlistsfail01.stderr
testsuite/tests/partial-sigs/should_compile/DataFamilyInstanceLHS.stderr
testsuite/tests/perf/compiler/all.T
testsuite/tests/perf/should_run/all.T
testsuite/tests/polykinds/T8132.stderr
testsuite/tests/quasiquotation/T7918.stdout
testsuite/tests/roles/should_compile/Roles1.stderr
testsuite/tests/roles/should_compile/Roles13.stderr
testsuite/tests/roles/should_compile/Roles14.stderr
testsuite/tests/roles/should_compile/Roles2.stderr
testsuite/tests/roles/should_compile/Roles3.stderr
testsuite/tests/roles/should_compile/Roles4.stderr
testsuite/tests/roles/should_compile/T8958.stderr
testsuite/tests/simplCore/should_compile/T3234.stderr
testsuite/tests/simplCore/should_compile/T3717.stderr
testsuite/tests/simplCore/should_compile/T3772.stdout
testsuite/tests/simplCore/should_compile/T4908.stderr
testsuite/tests/simplCore/should_compile/T4930.stderr
testsuite/tests/simplCore/should_compile/T7360.stderr
testsuite/tests/simplCore/should_compile/T8274.stdout
testsuite/tests/simplCore/should_compile/T9400.stderr
testsuite/tests/simplCore/should_compile/rule2.stderr
testsuite/tests/simplCore/should_compile/spec-inline.stderr
testsuite/tests/stranal/should_compile/T10694.stdout
testsuite/tests/stranal/sigs/BottomFromInnerLambda.stderr
testsuite/tests/stranal/sigs/DmdAnalGADTs.stderr
testsuite/tests/stranal/sigs/HyperStrUse.stderr
testsuite/tests/stranal/sigs/StrAnalExample.stderr
testsuite/tests/stranal/sigs/T8569.stderr
testsuite/tests/stranal/sigs/T8598.stderr
testsuite/tests/stranal/sigs/UnsatFun.stderr
testsuite/tests/th/TH_Roles2.stderr
testsuite/tests/typecheck/should_compile/holes2.stderr
testsuite/tests/typecheck/should_fail/T5095.stderr
testsuite/tests/typecheck/should_fail/tcfail072.stderr
testsuite/tests/typecheck/should_fail/tcfail133.stderr
utils/haddock