Note [Type checking recursive type and class declarations]
At this point we have completed *kind-checking* of a mutually
recursive group of type/class decls (done in kcTyClGroup). However,
we discarded the kind-checked types (eg RHSs of data type decls);
note that kcTyClDecl returns (). There are two reasons:
* It's convenient, because we don't have to rebuild a
kinded HsDecl (a fairly elaborate type)
* It's necessary, because after kind-generalisation, the
TyCons/Classes may now be kind-polymorphic, and hence need
to be given kind arguments.
Example:
data T f a = MkT (f a) (T f a)
During kind-checking, we give T the kind T :: k1 -> k2 -> *
and figure out constraints on k1, k2 etc. Then we generalise
to get T :: forall k. (k->*) -> k -> *
So now the (T f a) in the RHS must be elaborated to (T k f a).
However, during tcTyClDecl of T (above) we will be in a recursive
"knot". So we aren't allowed to look at the TyCon T itself; we are only
allowed to put it (lazily) in the returned structures. But when
kind-checking the RHS of T's decl, we *do* need to know T's kind (so
that we can correctly elaborate (T k f a). How can we get T's kind
without looking at T? Delicate answer: during tcTyClDecl, we extend
*Global* env with T -> ATyCon (the (not yet built) final TyCon for T)
*Local* env with T -> ATcTyCon (TcTyCon with the polymorphic kind of T)
Then:
* During GHC.Tc.Gen.HsType.tcTyVar we look in the *local* env, to get the
fully-known, not knot-tied TcTyCon for T.
* Then, in GHC.Tc.Zonk.Type.zonkTcTypeToType (and zonkTcTyCon in particular)
we look in the *global* env to get the TyCon.
This fancy footwork (with two bindings for T) is only necessary for the
TyCons or Classes of this recursive group. Earlier, finished groups,
live in the global env only.
See also Note [Kind checking recursive type and class declarations] References 1
Referenced by 7
- GHC.Core.TyCo.Rep call site
- GHC.Core.Type call site
- GHC.Tc.Gen.HsType call site
- GHC.Tc.TyCl call site
- TcTyCon, MonoTcTyCon, and PolyTcTyCon GHC.Tc.TyCl
- GHC.Tc.Types.BasicTypes call site
- Sharing when zonking to Type GHC.Tc.Zonk.Type