[commit: ghc] master: Type-indexed Typeable (8fa4bf9)
git at git.haskell.org
git at git.haskell.org
Sat Feb 18 05:10:58 UTC 2017
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/8fa4bf9ab3f4ea4b208f4a43cc90857987e6d497/ghc
>---------------------------------------------------------------
commit 8fa4bf9ab3f4ea4b208f4a43cc90857987e6d497
Author: Ben Gamari <ben at smart-cactus.org>
Date: Thu Feb 2 01:29:26 2017 -0500
Type-indexed Typeable
This at long last realizes the ideas for type-indexed Typeable discussed in A
Reflection on Types (#11011). The general sketch of the project is described on
the Wiki (Typeable/BenGamari). The general idea is that we are adding a type
index to `TypeRep`,
data TypeRep (a :: k)
This index allows the typechecker to reason about the type represented by the `TypeRep`.
This index representation mechanism is exposed as `Type.Reflection`, which also provides
a number of patterns for inspecting `TypeRep`s,
```lang=haskell
pattern TRFun :: forall k (fun :: k). ()
=> forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
(arg :: TYPE r1) (res :: TYPE r2).
(k ~ Type, fun ~~ (arg -> res))
=> TypeRep arg
-> TypeRep res
-> TypeRep fun
pattern TRApp :: forall k2 (t :: k2). ()
=> forall k1 (a :: k1 -> k2) (b :: k1). (t ~ a b)
=> TypeRep a -> TypeRep b -> TypeRep t
-- | Pattern match on a type constructor.
pattern TRCon :: forall k (a :: k). TyCon -> TypeRep a
-- | Pattern match on a type constructor including its instantiated kind
-- variables.
pattern TRCon' :: forall k (a :: k). TyCon -> [SomeTypeRep] -> TypeRep a
```
In addition, we give the user access to the kind of a `TypeRep` (#10343),
typeRepKind :: TypeRep (a :: k) -> TypeRep k
Moreover, all of this plays nicely with 8.2's levity polymorphism, including the
newly levity polymorphic (->) type constructor.
Library changes
---------------
The primary change here is the introduction of a Type.Reflection module to base.
This module provides access to the new type-indexed TypeRep introduced in this
patch. We also continue to provide the unindexed Data.Typeable interface, which
is simply a type synonym for the existentially quantified SomeTypeRep,
data SomeTypeRep where SomeTypeRep :: TypeRep a -> SomeTypeRep
Naturally, this change also touched Data.Dynamic, which can now export the
Dynamic data constructor. Moreover, I removed a blanket reexport of
Data.Typeable from Data.Dynamic (which itself doesn't even import Data.Typeable
now).
We also add a kind heterogeneous type equality type, (:~~:), to
Data.Type.Equality.
Implementation
--------------
The implementation strategy is described in Note [Grand plan for Typeable] in
TcTypeable. None of it was difficult, but it did exercise a number of parts of
the new levity polymorphism story which had not yet been exercised, which took
some sorting out.
The rough idea is that we augment the TyCon produced for each type constructor
with information about the constructor's kind (which we call a KindRep). This
allows us to reconstruct the monomorphic result kind of an particular
instantiation of a type constructor given its kind arguments.
Unfortunately all of this takes a fair amount of work to generate and send
through the compilation pipeline. In particular, the KindReps can unfortunately
get quite large. Moreover, the simplifier will float out various pieces of them,
resulting in numerous top-level bindings. Consequently we mark the KindRep
bindings as noinline, ensuring that the float-outs don't make it into the
interface file. This is important since there is generally little benefit to
inlining KindReps and they would otherwise strongly affect compiler performance.
Performance
-----------
Initially I was hoping to also clear up the remaining holes in Typeable's
coverage by adding support for both unboxed tuples (#12409) and unboxed sums
(#13276). While the former was fairly straightforward, the latter ended up being
quite difficult: while the implementation can support them easily, enabling this
support causes thousands of Typeable bindings to be emitted to the GHC.Types as
each arity-N sum tycon brings with it N promoted datacons, each of which has a
KindRep whose size which itself scales with N. Doing this was simply too
expensive to be practical; consequently I've disabled support for the time
being.
Even after disabling sums this change regresses compiler performance far more
than I would like. In particular there are several testcases in the testsuite
which consist mostly of types which regress by over 30% in compiler allocations.
These include (considering the "bytes allocated" metric),
* T1969: +10%
* T10858: +23%
* T3294: +19%
* T5631: +41%
* T6048: +23%
* T9675: +20%
* T9872a: +5.2%
* T9872d: +12%
* T9233: +10%
* T10370: +34%
* T12425: +30%
* T12234: +16%
* 13035: +17%
* T4029: +6.1%
I've spent quite some time chasing down the source of this regression and while
I was able to make som improvements, I think this approach of generating
Typeable bindings at time of type definition is doomed to give us unnecessarily
large compile-time overhead.
In the future I think we should consider moving some of all of the Typeable
binding generation logic back to the solver (where it was prior to
91c6b1f54aea658b0056caec45655475897f1972). I've opened #13261 documenting this
proposal.
>---------------------------------------------------------------
8fa4bf9ab3f4ea4b208f4a43cc90857987e6d497
compiler/backpack/RnModIface.hs | 3 +
compiler/basicTypes/MkId.hs | 2 +-
compiler/basicTypes/OccName.hs | 12 +-
compiler/coreSyn/CoreLint.hs | 2 +-
compiler/deSugar/DsBinds.hs | 101 +--
compiler/prelude/KnownUniques.hs | 47 +-
compiler/prelude/PrelNames.hs | 149 ++++-
compiler/prelude/THNames.hs | 32 +-
compiler/prelude/TysWiredIn.hs | 4 +-
compiler/rename/RnSource.hs | 29 +-
compiler/typecheck/TcBackpack.hs | 4 +-
compiler/typecheck/TcEvidence.hs | 19 +-
compiler/typecheck/TcHsSyn.hs | 12 +-
compiler/typecheck/TcInteract.hs | 46 +-
compiler/typecheck/TcRnDriver.hs | 6 +-
compiler/typecheck/TcTypeable.hs | 493 +++++++++++---
compiler/types/Kind.hs | 14 +-
compiler/types/TyCon.hs | 6 +-
compiler/types/Type.hs | 4 +-
compiler/types/Type.hs-boot | 5 +
compiler/utils/Binary.hs | 177 ++++-
compiler/utils/Fingerprint.hsc | 1 +
libraries/base/Data/Dynamic.hs | 55 +-
libraries/base/Data/Type/Equality.hs | 8 +
libraries/base/Data/Typeable.hs | 226 +++++--
libraries/base/Data/Typeable/Internal.hs | 736 ++++++++++++++-------
libraries/base/GHC/Conc/Sync.hs | 4 -
libraries/base/GHC/Show.hs | 4 +-
libraries/base/Type/Reflection.hs | 67 ++
libraries/base/Type/Reflection/Unsafe.hs | 22 +
libraries/base/base.cabal | 4 +-
libraries/base/changelog.md | 9 +
libraries/base/tests/T11334a.stdout | 2 +-
libraries/base/tests/all.T | 2 +-
libraries/base/tests/dynamic002.hs | 5 +
libraries/base/tests/dynamic002.stdout | 2 +-
libraries/base/tests/dynamic004.hs | 1 -
libraries/ghc-boot/GHC/Serialized.hs | 15 +-
libraries/ghc-prim/GHC/Classes.hs | 8 +-
libraries/ghc-prim/GHC/Types.hs | 40 +-
libraries/ghci/GHCi/Message.hs | 6 +-
libraries/ghci/GHCi/TH/Binary.hs | 171 +++++
.../tests/deSugar/should_compile/T2431.stderr | 90 ++-
.../tests/dependent/should_compile/RaeJobTalk.hs | 52 +-
testsuite/tests/dependent/should_compile/T11711.hs | 10 +-
.../dependent/should_compile/dynamic-paper.hs | 16 +-
testsuite/tests/deriving/perf/all.T | 5 +-
.../tests/ghci.debugger/scripts/print019.stderr | 6 +-
.../should_compile/DumpTypecheckedAst.stderr | 238 +++++--
testsuite/tests/patsyn/should_compile/T12698.hs | 2 +-
testsuite/tests/perf/compiler/all.T | 115 ++--
testsuite/tests/perf/haddock/all.T | 6 +-
testsuite/tests/perf/should_run/all.T | 3 +-
testsuite/tests/perf/space_leaks/all.T | 6 +-
testsuite/tests/polykinds/T8132.hs | 5 +-
testsuite/tests/polykinds/T8132.stderr | 2 +-
testsuite/tests/roles/should_compile/Roles1.stderr | 156 ++++-
.../tests/roles/should_compile/Roles13.stderr | 144 +++-
.../tests/roles/should_compile/Roles14.stderr | 22 +-
testsuite/tests/roles/should_compile/Roles2.stderr | 44 +-
testsuite/tests/roles/should_compile/Roles3.stderr | 89 ++-
testsuite/tests/roles/should_compile/Roles4.stderr | 46 +-
testsuite/tests/roles/should_compile/T8958.stderr | 80 ++-
.../tests/safeHaskell/unsafeLibs/GoodImport03.hs | 3 +-
.../tests/simplCore/should_compile/T7360.stderr | 141 ++--
.../tests/simplCore/should_compile/T8274.stdout | 29 +-
testsuite/tests/th/TH_Roles2.stderr | 14 +-
testsuite/tests/typecheck/should_compile/tc167.hs | 6 +-
.../should_fail/TcStaticPointersFail02.stderr | 4 +-
.../typecheck/should_run/TestTypeableBinary.hs | 37 ++
.../typecheck/should_run/TestTypeableBinary.stdout | 15 +
testsuite/tests/typecheck/should_run/TypeOf.stdout | 6 +-
testsuite/tests/typecheck/should_run/TypeRep.hs | 12 +-
.../tests/typecheck/should_run/TypeRep.stdout | 6 +-
testsuite/tests/typecheck/should_run/Typeable1.hs | 23 +
.../tests/typecheck/should_run/Typeable1.stderr | 25 +
testsuite/tests/typecheck/should_run/TypeableEq.hs | 79 +++
.../tests/typecheck/should_run/TypeableEq.stdout | 10 +
testsuite/tests/typecheck/should_run/all.T | 3 +
79 files changed, 3095 insertions(+), 1010 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 8fa4bf9ab3f4ea4b208f4a43cc90857987e6d497
More information about the ghc-commits
mailing list