Commit 5bc195b1 authored by Vladislav Zavialov's avatar Vladislav Zavialov Committed by Marge Bot

Treat kind/type variables identically, demolish FKTV

Implements GHC Proposal #24: .../ghc-proposals/blob/master/proposals/0024-no-kind-vars.rst
Fixes Trac #16334, Trac #16315

With this patch, scoping rules for type and kind variables have been
unified: kind variables no longer receieve special treatment. This
simplifies both the language and the implementation.

User-facing changes
-------------------

* Kind variables are no longer implicitly quantified when an explicit
  forall is used:

    p ::             Proxy (a :: k)    -- still accepted
    p :: forall k a. Proxy (a :: k)    -- still accepted
    p :: forall   a. Proxy (a :: k)    -- no longer accepted

  In other words, now we adhere to the "forall-or-nothing" rule more
  strictly.

  Related function: RnTypes.rnImplicitBndrs

* The -Wimplicit-kind-vars warning has been deprecated.

* Kind variables are no longer implicitly quantified in constructor
  declarations:

    data T a        = T1 (S (a :: k) | forall (b::k). T2 (S b)  -- no longer accepted
    data T (a :: k) = T1 (S (a :: k) | forall (b::k). T2 (S b)  -- still accepted

  Related function: RnTypes.extractRdrKindSigVars

* Implicitly quantified kind variables are no longer put in front of
  other variables:

    f :: Proxy (a :: k) -> Proxy (b :: j)

    f :: forall k j (a :: k) (b :: j). Proxy a -> Proxy b   -- old order
    f :: forall k (a :: k) j (b :: j). Proxy a -> Proxy b   -- new order

  This is a breaking change for users of TypeApplications. Note that
  we still respect the dpendency order: 'k' before 'a', 'j' before 'b'.
  See "Ordering of specified variables" in the User's Guide.

  Related function: RnTypes.rnImplicitBndrs

* In type synonyms and type family equations, free variables on the RHS
  are no longer implicitly quantified unless used in an outermost kind
  annotation:

    type T = Just (Nothing :: Maybe a)         -- no longer accepted
    type T = Just Nothing :: Maybe (Maybe a)   -- still accepted

  The latter form is a workaround due to temporary lack of an explicit
  quantification method. Ideally, we would write something along these
  lines:

    type T @a = Just (Nothing :: Maybe a)

  Related function: RnTypes.extractHsTyRdrTyVarsKindVars

* Named wildcards in kinds are fixed (Trac #16334):

    x :: (Int :: _t)    -- this compiles, infers (_t ~ Type)

  Related function: RnTypes.partition_nwcs

Implementation notes
--------------------

* One of the key changes is the removal of FKTV in RnTypes:

  - data FreeKiTyVars = FKTV { fktv_kis    :: [Located RdrName]
  -                          , fktv_tys    :: [Located RdrName] }
  + type FreeKiTyVars = [Located RdrName]

  We used to keep track of type and kind variables separately, but
  now that they are on equal footing when it comes to scoping, we
  can put them in the same list.

* extract_lty and family are no longer parametrized by TypeOrKind,
  as we now do not distinguish kind variables from type variables.

* PatSynExPE and the related Note [Pattern synonym existentials do not scope]
  have been removed (Trac #16315). With no implicit kind quantification,
  we can no longer trigger the error.

* reportFloatingKvs and the related Note [Free-floating kind vars]
  have been removed. With no implicit kind quantification,
  we can no longer trigger the error.
parent 4dbacba5
Pipeline #2825 passed with stages
in 268 minutes and 34 seconds
......@@ -3992,7 +3992,8 @@ wWarningFlagsDeps = [
flagSpec "hi-shadowing" Opt_WarnHiShadows,
flagSpec "inaccessible-code" Opt_WarnInaccessibleCode,
flagSpec "implicit-prelude" Opt_WarnImplicitPrelude,
flagSpec "implicit-kind-vars" Opt_WarnImplicitKindVars,
depFlagSpec "implicit-kind-vars" Opt_WarnImplicitKindVars
"it is now an error",
flagSpec "incomplete-patterns" Opt_WarnIncompletePatterns,
flagSpec "incomplete-record-updates" Opt_WarnIncompletePatternsRecUpd,
flagSpec "incomplete-uni-patterns" Opt_WarnIncompleteUniPatterns,
......@@ -4830,7 +4831,6 @@ minusWcompatOpts
= [ Opt_WarnMissingMonadFailInstances
, Opt_WarnSemigroup
, Opt_WarnNonCanonicalMonoidInstances
, Opt_WarnImplicitKindVars
, Opt_WarnStarIsType
]
......
......@@ -727,15 +727,11 @@ rnFamInstEqn doc mb_cls rhs_kvars
; let pat_kity_vars_with_dups = extractHsTyArgRdrKiTyVarsDup pats
-- Use the "...Dups" form because it's needed
-- below to report unsed binder on the LHS
; let pat_kity_vars = rmDupsInRdrTyVars pat_kity_vars_with_dups
-- all pat vars not explicitly bound (see extractHsTvBndrs)
; let mb_imp_kity_vars = extractHsTvBndrs <$> mb_bndrs <*> pure pat_kity_vars
imp_vars = case mb_imp_kity_vars of
-- kind vars are the only ones free if we have an explicit forall
Just nbnd_kity_vars -> freeKiTyVarsKindVars nbnd_kity_vars
-- all pattern vars are free otherwise
Nothing -> freeKiTyVarsAllVars pat_kity_vars
-- Implicitly bound variables, empty if we have an explicit 'forall' according
-- to the "forall-or-nothing" rule.
; let imp_vars | isNothing mb_bndrs = nubL pat_kity_vars_with_dups
| otherwise = []
; imp_var_names <- mapM (newTyVarNameRn mb_cls) imp_vars
; let bndrs = fromMaybe [] mb_bndrs
......@@ -766,7 +762,7 @@ rnFamInstEqn doc mb_cls rhs_kvars
-- See Note [Unused type variables in family instances]
; let groups :: [NonEmpty (Located RdrName)]
groups = equivClasses cmpLocated $
freeKiTyVarsAllVars pat_kity_vars_with_dups
pat_kity_vars_with_dups
; nms_dups <- mapM (lookupOccRn . unLoc) $
[ tv | (tv :| (_:_)) <- groups ]
-- Add to the used variables
......
This diff is collapsed.
......@@ -47,7 +47,6 @@ module TcHsType (
typeLevelMode, kindLevelMode,
kindGeneralize, checkExpectedKind_pp,
reportFloatingKvs,
-- Sort-checking kinds
tcLHsKindSig, badKindSig,
......@@ -1871,13 +1870,6 @@ kcLHsQTyVars_Cusk name flav
-- doesn't work, we catch it here, before an error cascade
; checkValidTelescope tycon
-- If any of the specified tyvars aren't actually mentioned in a binder's
-- kind (or the return kind), then we're in the CUSK case from
-- Note [Free-floating kind vars]
; let unmentioned_kvs = filterOut (`elemVarSet` mentioned_kv_set) specified
; reportFloatingKvs name flav (map binderVar final_tc_binders) unmentioned_kvs
; traceTc "kcLHsQTyVars: cusk" $
vcat [ text "name" <+> ppr name
, text "kv_ns" <+> ppr kv_ns
......@@ -2889,8 +2881,6 @@ promotionErr name err
NoDataKindsTC -> text "perhaps you intended to use DataKinds"
NoDataKindsDC -> text "perhaps you intended to use DataKinds"
PatSynPE -> text "pattern synonyms cannot be promoted"
PatSynExPE -> sep [ text "the existential variables of a pattern synonym"
, text "signature do not scope over the pattern" ]
_ -> text "it is defined and used in the same recursive group"
{-
......@@ -2919,112 +2909,6 @@ badPatTyVarTvs sig_ty bad_tvs
-}
{- Note [Free-floating kind vars]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
data S a = MkS (Proxy (a :: k))
According to the rules around implicitly-bound kind variables,
that k scopes over the whole declaration. The renamer grabs
it and adds it to the hsq_implicits field of the HsQTyVars of the
tycon. So we get
S :: forall {k}. k -> Type
That's fine. But consider this variant:
data T = MkT (forall (a :: k). Proxy a)
-- from test ghci/scripts/T7873
This is not an existential datatype, but a higher-rank one (the forall
to the right of MkT). Again, 'k' scopes over the whole declaration,
but we do not want to get
T :: forall {k}. Type
Why not? Because the kind variable isn't fixed by anything. For
a variable like k to be implicit, it needs to be mentioned in the kind
of a tycon tyvar. But it isn't.
Rejecting T depends on whether or not the datatype has a CUSK.
Non-CUSK (handled in TcTyClsDecls.kcTyClGroup (generalise)):
When generalising the TyCon we check that every Specified 'k'
appears free in the kind of the TyCon; that is, in the kind of
one of its Required arguments, or the result kind.
CUSK (handled in TcHsType.kcLHsQTyVars, the CUSK case):
When we determine the tycon's final, never-to-be-changed kind
in kcLHsQTyVars, we check to make sure all implicitly-bound kind
vars are indeed mentioned in a kind somewhere. If not, error.
We also perform free-floating kind var analysis for type family instances
(see #13985). Here is an interesting example:
type family T :: k
type instance T = (Nothing :: Maybe a)
Upon a cursory glance, it may appear that the kind variable `a` is
free-floating above, since there are no (visible) LHS patterns in `T`. However,
there is an *invisible* pattern due to the return kind, so inside of GHC, the
instance looks closer to this:
type family T @k :: k
type instance T @(Maybe a) = (Nothing :: Maybe a)
Here, we can see that `a` really is bound by a LHS type pattern, so `a` is in
fact not free-floating. Contrast that with this example:
type instance T = Proxy (Nothing :: Maybe a)
This would looks like this inside of GHC:
type instance T @(*) = Proxy (Nothing :: Maybe a)
So this time, `a` is neither bound by a visible nor invisible type pattern on
the LHS, so it would be reported as free-floating.
Finally, here's one more brain-teaser (from #9574). In the example below:
class Funct f where
type Codomain f :: *
instance Funct ('KProxy :: KProxy o) where
type Codomain 'KProxy = NatTr (Proxy :: o -> *)
As it turns out, `o` is not free-floating in this example. That is because `o`
bound by the kind signature of the LHS type pattern 'KProxy. To make this more
obvious, one can also write the instance like so:
instance Funct ('KProxy :: KProxy o) where
type Codomain ('KProxy :: KProxy o) = NatTr (Proxy :: o -> *)
-}
-- See Note [Free-floating kind vars]
reportFloatingKvs :: Name -- of the tycon
-> TyConFlavour -- What sort of TyCon it is
-> [TcTyVar] -- all tyvars, not necessarily zonked
-> [TcTyVar] -- floating tyvars
-> TcM ()
reportFloatingKvs tycon_name flav all_tvs bad_tvs
= unless (null bad_tvs) $ -- don't bother zonking if there's no error
do { all_tvs <- mapM zonkTcTyVarToTyVar all_tvs
; bad_tvs <- mapM zonkTcTyVarToTyVar bad_tvs
; let (tidy_env, tidy_all_tvs) = tidyOpenTyCoVars emptyTidyEnv all_tvs
tidy_bad_tvs = map (tidyTyCoVarOcc tidy_env) bad_tvs
; mapM_ (report tidy_all_tvs) tidy_bad_tvs }
where
report tidy_all_tvs tidy_bad_tv
= addErr $
vcat [ text "Kind variable" <+> quotes (ppr tidy_bad_tv) <+>
text "is implicitly bound in" <+> ppr flav
, quotes (ppr tycon_name) <> comma <+>
text "but does not appear as the kind of any"
, text "of its type variables. Perhaps you meant"
, text "to bind it explicitly somewhere?"
, ppWhen (not (null tidy_all_tvs)) $
hang (text "Type variables with inferred kinds:")
2 (ppr_tv_bndrs tidy_all_tvs) ]
ppr_tv_bndrs tvs = sep (map pp_tv tvs)
pp_tv tv = parens (ppr tv <+> dcolon <+> ppr (tyVarKind tv))
-- | If the inner action emits constraints, report them as errors and fail;
-- otherwise, propagates the return value. Useful as a wrapper around
-- 'tcImplicitTKBndrs', which uses solveLocalEqualities, when there won't be
......
......@@ -384,9 +384,6 @@ tcCheckPatSynDecl psb@PSB{ psb_id = lname@(dL->L _ name), psb_args = details
ASSERT2( equalLength arg_names arg_tys, ppr name $$ ppr arg_names $$ ppr arg_tys )
pushLevelAndCaptureConstraints $
tcExtendTyVarEnv univ_tvs $
tcExtendKindEnvList [(getName (binderVar ex_tv), APromotionErr PatSynExPE)
| ex_tv <- extra_ex] $
-- See Note [Pattern synonym existentials do not scope]
tcPat PatSyn lpat (mkCheckExpType pat_ty) $
do { let in_scope = mkInScopeSet (mkVarSet univ_tvs)
empty_subst = mkEmptyTCvSubst in_scope
......@@ -451,54 +448,6 @@ This should work. But in the matcher we must match against MkT, and then
instantiate its argument 'x', to get a function of type (Int -> Int).
Equality is not enough! Trac #13752 was an example.
Note [Pattern synonym existentials do not scope]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider this (Trac #14498):
pattern SS :: forall (t :: k). () =>
=> forall (a :: kk -> k) (n :: kk).
=> TypeRep n -> TypeRep t
pattern SS n <- (App (Typeable :: TypeRep (a::kk -> k)) n)
Here 'k' is implicitly bound in the signature, but (with
-XScopedTypeVariables) it does still scope over the pattern-synonym
definition. But what about 'kk', which is oexistential? It too is
implicitly bound in the signature; should it too scope? And if so,
what type variable is it bound to?
The trouble is that the type variable to which it is bound is itself
only brought into scope in part the pattern, so it makes no sense for
'kk' to scope over the whole pattern. See the discussion on
Trac #14498, esp comment:16ff. Here is a simpler example:
data T where { MkT :: x -> (x->Int) -> T }
pattern P :: () => forall x. x -> (x->Int) -> T
pattern P a b = (MkT a b, True)
Here it would make no sense to mention 'x' in the True pattern,
like this:
pattern P a b = (MkT a b, True :: x)
The 'x' only makes sense "under" the MkT pattern. Conclusion: the
existential type variables of a pattern-synonym signature should not
scope.
But it's not that easy to implement, because we don't know
exactly what the existentials /are/ until we get to type checking.
(See Note [The pattern-synonym signature splitting rule], and
the partition of implicit_tvs in tcCheckPatSynDecl.)
So we do this:
- The reaner brings all the implicitly-bound kind variables into
scope, without trying to distinguish universal from existential
- tcCheckPatSynDecl uses tcExtendKindEnvList to bind the
implicitly-bound existentials to
APromotionErr PatSynExPE
It's not really a promotion error, but it's a way to bind the Name
(which the renamer has not complained about) to something that, when
looked up, will cause a complaint (in this case
TcHsType.promotionErr)
Note [The pattern-synonym signature splitting rule]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
......@@ -1107,9 +1107,6 @@ data PromotionErr
| PatSynPE -- Pattern synonyms
-- See Note [Don't promote pattern synonyms] in TcEnv
| PatSynExPE -- Pattern synonym existential type variable
-- See Note [Pattern synonym existentials do not scope] in TcPatSyn
| RecDataConPE -- Data constructor in a recursive loop
-- See Note [Recursion and promoting data constructors] in TcTyClsDecls
| NoDataKindsTC -- -XDataKinds not enabled (for a tycon)
......@@ -1303,7 +1300,6 @@ instance Outputable PromotionErr where
ppr ClassPE = text "ClassPE"
ppr TyConPE = text "TyConPE"
ppr PatSynPE = text "PatSynPE"
ppr PatSynExPE = text "PatSynExPE"
ppr FamDataConPE = text "FamDataConPE"
ppr (ConstrainedDataConPE pred) = text "ConstrainedDataConPE"
<+> parens (ppr pred)
......@@ -1322,7 +1318,6 @@ pprPECategory :: PromotionErr -> SDoc
pprPECategory ClassPE = text "Class"
pprPECategory TyConPE = text "Type constructor"
pprPECategory PatSynPE = text "Pattern synonym"
pprPECategory PatSynExPE = text "Pattern synonym existential"
pprPECategory FamDataConPE = text "Data constructor"
pprPECategory ConstrainedDataConPE{} = text "Data constructor"
pprPECategory RecDataConPE = text "Data constructor"
......
......@@ -1252,7 +1252,7 @@ reifyInstances th_nm th_tys
; rdr_ty <- cvt loc (mkThAppTs (TH.ConT th_nm) th_tys)
-- #9262 says to bring vars into scope, like in HsForAllTy case
-- of rnHsTyKi
; let tv_rdrs = freeKiTyVarsAllVars (extractHsTyRdrTyVars rdr_ty)
; let tv_rdrs = extractHsTyRdrTyVars rdr_ty
-- Rename to HsType Name
; ((tv_names, rn_ty), _fvs)
<- checkNoErrs $ -- If there are out-of-scope Names here, then we
......
......@@ -537,7 +537,6 @@ generaliseTcTyCon tc
= setSrcSpan (getSrcSpan tc) $
addTyConCtxt tc $
do { let tc_name = tyConName tc
tc_flav = tyConFlavour tc
tc_res_kind = tyConResKind tc
tc_tvs = tyConTyVars tc
......@@ -610,21 +609,13 @@ generaliseTcTyCon tc
, text "required_tcbs =" <+> ppr required_tcbs
, text "final_tcbs =" <+> ppr final_tcbs ]
-- Step 8: check for floating kind vars
-- See Note [Free-floating kind vars]
-- They are easily identified by the fact that they
-- have not been skolemised by quantifyTyVars
; let floating_specified = filter isTyVarTyVar scoped_tvs
; reportFloatingKvs tc_name tc_flav
scoped_tvs floating_specified
-- Step 9: Check for duplicates
-- Step 8: Check for duplicates
-- E.g. data SameKind (a::k) (b::k)
-- data T (a::k1) (b::k2) = MkT (SameKind a b)
-- Here k1 and k2 start as TyVarTvs, and get unified with each other
; mapM_ report_sig_tv_err (findDupTyVarTvs scoped_tv_pairs)
-- Step 10: Check for validity.
-- Step 9: Check for validity.
-- We do this here because we're about to put the tycon into
-- the environment, and we don't want anything malformed in the
-- environment.
......
......@@ -16,6 +16,34 @@ Full details
Language
~~~~~~~~
- Kind variables are no longer implicitly quantified when an explicit ``forall`` is used, see
`GHC proposal #24
<https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0024-no-kind-vars.rst>`__.
:ghc-flag:`-Wimplicit-kind-vars` is now obsolete.
- Kind variables are no longer implicitly quantified in constructor declarations: ::
data T a = T1 (S (a :: k) | forall (b::k). T2 (S b) -- no longer accepted
data T (a :: k) = T1 (S (a :: k) | forall (b::k). T2 (S b) -- still accepted
- Implicitly quantified kind variables are no longer put in front of other variables: ::
f :: Proxy (a :: k) -> Proxy (b :: j)
ghci> :t +v f -- old order:
f :: forall k j (a :: k) (b :: j). Proxy a -> Proxy b
ghci> :t +v f -- new order:
f :: forall k (a :: k) j (b :: j). Proxy a -> Proxy b
This is a breaking change for users of :extension:`TypeApplications`.
- In type synonyms and type family equations, free variables on the RHS are no longer
implicitly quantified unless used in an outermost kind annotation: ::
type T = Just (Nothing :: Maybe a) -- no longer accepted
type T = Just Nothing :: Maybe (Maybe a) -- still accepted
Compiler
~~~~~~~~
......
......@@ -7574,16 +7574,16 @@ instance for ``GMap`` is ::
In this example, the declaration has only one variant. In general, it
can be any number.
When :extension:`ExplicitForAll` is enabled, type or kind variables used on
When :extension:`ExplicitForAll` is enabled, type and kind variables used on
the left hand side can be explicitly bound. For example: ::
data instance forall a (b :: Proxy a). F (Proxy b) = FProxy Bool
When an explicit ``forall`` is present, all *type* variables mentioned which
are not already in scope must be bound by the ``forall``. Kind variables will
be implicitly bound if necessary, for example: ::
When an explicit ``forall`` is present, *all* type and kind variables mentioned
which are not already in scope must be bound by the ``forall``:
data instance forall (a :: k). F a = FOtherwise
data instance forall (a :: k). F a = FOtherwise -- rejected: k not in scope
data instance forall k (a :: k). F a = FOtherwise -- accepted
When the flag :ghc-flag:`-Wunused-type-patterns` is enabled, type
variables that are mentioned in the patterns on the left hand side, but not
......@@ -9184,17 +9184,74 @@ a type variable ``a`` of kind ``k``. In general, there is no limit to how
deeply nested this sort of dependency can work. However, the dependency must
be well-scoped: ``forall (a :: k) k. ...`` is an error.
For backward compatibility, kind variables *do not* need to be bound explicitly,
even if the type starts with ``forall``.
Implicit quantification in type synonyms and type family instances
------------------------------------------------------------------
Accordingly, the rule for kind quantification in higher-rank contexts has
changed slightly. In GHC 7, if a kind variable was mentioned for the first
time in the kind of a variable bound in a non-top-level ``forall``, the kind
variable was bound there, too.
That is, in ``f :: (forall (a :: k). ...) -> ...``, the ``k`` was bound
by the same ``forall`` as the ``a``. In GHC 8, however, all kind variables
mentioned in a type are bound at the outermost level. If you want one bound
in a higher-rank ``forall``, include it explicitly.
Consider the scoping rules for type synonyms and type family instances, such as
these::
type TS a (b :: k) = <rhs>
type instance TF a (b :: k) = <rhs>
The basic principle is that all variables mentioned on the right hand side
``<rhs>`` must be bound on the left hand side::
type TS a (b :: k) = (k, a, Proxy b) -- accepted
type TS a (b :: k) = (k, a, Proxy b, z) -- rejected: z not in scope
But there is one exception: free variables mentioned in the outermost kind
signature on the right hand side are quantified implicitly. Thus, in the
following example the variables ``a``, ``b``, and ``k`` are all in scope on the
right hand side of ``S``::
type S a b = <rhs> :: k -> k
The reason for this exception is that there may be no other way to bind ``k``.
For example, suppose we wanted ``S`` to have the the following kind with an
*invisible* parameter ``k``::
S :: forall k. Type -> Type -> k -> k
In this case, we could not simply bind ``k`` on the left-hand side, as ``k``
would become a *visible* parameter::
type S k a b = <rhs> :: k -> k
S :: forall k -> Type -> Type -> k -> k
Note that we only look at the *outermost* kind signature to decide which
variables to quantify implicitly. As a counter-example, consider ``M1``: ::
type M1 = 'Just ('Nothing :: Maybe k) -- rejected: k not in scope
Here, the kind signature is hidden inside ``'Just``, and there is no outermost
kind signature. We can fix this example by providing an outermost kind signature: ::
type M2 = 'Just ('Nothing :: Maybe k) :: Maybe (Maybe k)
Here, ``k`` is brought into scope by ``:: Maybe (Maybe k)``.
A kind signature is considered to be outermost regardless of redundant
parentheses: ::
type P = 'Nothing :: Maybe a -- accepted
type P = ((('Nothing :: Maybe a))) -- accepted
Closed type family instances are subject to the same rules: ::
type family F where
F = 'Nothing :: Maybe k -- accepted
type family F where
F = 'Just ('Nothing :: Maybe k) -- rejected: k not in scope
type family F where
F = 'Just ('Nothing :: Maybe k) :: Maybe (Maybe k) -- accepted
type family F :: Maybe (Maybe k) where
F = 'Just ('Nothing :: Maybe k) -- rejected: k not in scope
type family F :: Maybe (Maybe k) where
F @k = 'Just ('Nothing :: Maybe k) -- accepted
Kind-indexed GADTs
------------------
......@@ -10832,19 +10889,6 @@ the rules in the subtler cases:
- If an identifier's type has a ``forall``, then the order of type variables
as written in the ``forall`` is retained.
- If the type signature includes any kind annotations (either on variable
binders or as annotations on types), any variables used in kind
annotations come before any variables never used in kind annotations.
This rule is not recursive: if there is an annotation within an annotation,
then the variables used therein are on equal footing. Examples::
f :: Proxy (a :: k) -> Proxy (b :: j) -> ()
-- as if f :: forall k j a b. ...
g :: Proxy (b :: j) -> Proxy (a :: (Proxy :: (k -> Type) -> Type) Proxy) -> ()
-- as if g :: forall j k b a. ...
-- NB: k is in a kind annotation within a kind annotation
- If any of the variables depend on other variables (that is, if some
of the variables are *kind* variables), the variables are reordered
so that kind variables come before type variables, preserving the
......@@ -10854,13 +10898,11 @@ the rules in the subtler cases:
h :: Proxy (a :: (j, k)) -> Proxy (b :: Proxy a) -> ()
-- as if h :: forall j k a b. ...
In this example, all of ``a``, ``j``, and ``k`` are considered kind
variables and will always be placed before ``b``, a lowly type variable.
(Note that ``a`` is used in ``b``\'s kind.) Yet, even though ``a`` appears
lexically before ``j`` and ``k``, ``j`` and ``k`` are quantified first,
because ``a`` depends on ``j`` and ``k``. Note further that ``j`` and ``k``
are not reordered with respect to each other, even though doing so would
not violate dependency conditions.
In this example, ``a`` depends on ``j`` and ``k``, and ``b`` depends on ``a``.
Even though ``a`` appears lexically before ``j`` and ``k``, ``j`` and ``k``
are quantified first, because ``a`` depends on ``j`` and ``k``. Note further
that ``j`` and ``k`` are not reordered with respect to each other, even
though doing so would not violate dependency conditions.
A "stable topological sort" here, we mean that we perform this algorithm
(which we call *ScopedSort*):
......
......@@ -123,7 +123,6 @@ The following flags are simple ways to select standard "packages" of warnings:
* :ghc-flag:`-Wmissing-monadfail-instances`
* :ghc-flag:`-Wsemigroup`
* :ghc-flag:`-Wnoncanonical-monoid-instances`
* :ghc-flag:`-Wimplicit-kind-vars`
* :ghc-flag:`-Wstar-is-type`
.. ghc-flag:: -Wno-compat
......@@ -776,58 +775,6 @@ of ``-W(no-)*``.
This warning is off by default.
.. ghc-flag:: -Wimplicit-kind-vars
:shortdesc: warn when kind variables are brought into scope implicitly despite
the "forall-or-nothing" rule
:type: dynamic
:reverse: -Wno-implicit-kind-vars
:category:
:since: 8.6
`GHC proposal #24
<https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0024-no-kind-vars.rst>`__
prescribes to treat kind variables and type variables identically in
``forall``, removing the legacy distinction between them.
Consider the following examples: ::
f :: Proxy a -> Proxy b -> ()
g :: forall a b. Proxy a -> Proxy b -> ()
``f`` does not use an explicit ``forall``, so type variables ``a`` and ``b``
are brought into scope implicitly. ``g`` quantifies both ``a`` and ``b``
explicitly. Both ``f`` and ``g`` work today and will continue to work in the
future because they adhere to the "forall-or-nothing" rule: either all type
variables in a function definition are introduced explicitly or implicitly,
there is no middle ground.
A violation of the "forall-or-nothing" rule looks like this: ::
m :: forall a. Proxy a -> Proxy b -> ()
``m`` does not introduce one of the variables, ``b``, and thus is rejected.
However, consider the following example: ::
n :: forall a. Proxy (a :: k) -> ()
While ``n`` uses ``k`` without introducing it and thus violates the rule, it
is currently accepted. This is because ``k`` in ``n`` is considered a kind
variable, as it occurs in a kind signature. In reality, the line between
type variables and kind variables is blurry, as the following example
demonstrates: ::
kindOf :: forall a. Proxy (a :: k) -> Proxy k
In ``kindOf``, the ``k`` variable is used both in a kind position and a type
position. Currently, ``kindOf`` happens to be accepted as well.
In a future release of GHC, both ``n`` and ``kindOf`` will be rejected per
the "forall-or-nothing" rule. This warning, being part of the
:ghc-flag:`-Wcompat` option group, allows to detect this before the actual
breaking change takes place.
.. ghc-flag:: -Wincomplete-patterns
:shortdesc: warn when a pattern match could fail
:type: dynamic
......
......@@ -8,9 +8,9 @@ module Bug where
import GHC.Exts (TYPE, RuntimeRep, Weak#, State#, RealWorld, mkWeak# )
class Foo (a :: TYPE rep) where
bar :: forall (b :: TYPE rep2). (a -> a -> b) -> a -> a -> b
bar :: forall rep2 (b :: TYPE rep2). (a -> a -> b) -> a -> a -> b
baz :: forall (a :: TYPE rep). Foo a => a -> a -> (# a, a #)
baz :: forall rep (a :: TYPE rep). Foo a => a -> a -> (# a, a #)
baz = bar (#,#)
obscure :: (forall (rep1 :: RuntimeRep) (rep2 :: RuntimeRep)
......
T15264.hs:8:22: warning: [-Wimplicit-kind-vars (in -Wcompat)]
An explicit ‘forall’ was used, but the following kind variables are not quantified: ‘k’
Despite this fact, GHC will introduce them into scope, but it will stop doing so in the future.
Suggested fix: add ‘forall k.’
T15264.hs:11:22: warning: [-Wimplicit-kind-vars (in -Wcompat)]
An explicit ‘forall’ was used, but the following kind variables are not quantified: ‘k1’, ‘k2’
Despite this fact, GHC will introduce them into scope, but it will stop doing so in the future.
Suggested fix: add ‘forall k1 k2.’
......@@ -48,7 +48,6 @@ test('T14066a', normal, compile, [''])
test('T14749', normal, compile, [''])
test('T14845_compile', normal, compile, [''])
test('T14991', normal, compile, [''])
test('T15264', normal, compile, [''])
test('DkNameRes', normal, compile, [''])
test('T15346', normal, compile, [''])
test('T15419', normal, compile, [''])
......
......@@ -10,5 +10,5 @@ data SameKind :: k -> k -> *
foo :: forall a k (b :: k). SameKind a b
foo = undefined
bar :: forall a (c :: Proxy b) (d :: Proxy a). Proxy c -> SameKind b d
bar :: forall a k (b :: k) (c :: Proxy b) (d :: Proxy a). Proxy c -> SameKind b d
bar = undefined
......@@ -5,9 +5,9 @@ BadTelescope2.hs:10:8: error:
k (a :: k) (b :: k)
• In the type signature: foo :: forall a k (b :: k). SameKind a b
BadTelescope2.hs:13:70: error:
• Expected kind ‘k0’, but ‘d’ has kind ‘Proxy a’
BadTelescope2.hs:13:81: error:
• Expected kind ‘k’, but ‘d’ has kind ‘Proxy a’
• In the second argument of ‘SameKind’, namely ‘d’
In the type signature:
bar :: forall a (c :: Proxy b) (d :: Proxy a).
bar :: forall a k (b :: k) (c :: Proxy b) (d :: Proxy a).
Proxy c -> SameKind b d
{-# LANGUAGE ExplicitForAll, PolyKinds #-}
{-# OPTIONS -Wcompat -Wno-error=implicit-kind-vars #-}
module T15264 where
......
T15264.hs:7:22: error: Not in scope: type variable ‘k’
T15264.hs:10:22: error: Not in scope: type variable ‘k1’
T15264.hs:10:32: error: Not in scope: type variable ‘k2’
......@@ -10,5 +10,5 @@ module T15825 where
type C k = (forall (x::k). *)
class X (a :: *)
instance forall (a :: C k). X (a :: *)
class X (a :: *)
instance forall k (a :: C k). X (a :: *)
T15825.hs:14:29: error:
T15825.hs:14:31: error:
• Illegal type synonym family application ‘GHC.Types.Any
@k’ in instance:
X (a @(GHC.Types.Any @k))
......
......@@ -39,3 +39,4 @@ test('T15743c', normal, compile_fail, [''])
test('T15743d', normal, compile_fail, [''])
test('T15825', normal, compile_fail, [''])
test('T15859', normal, compile_fail, [''])
test('T15264', normal, compile_fail, [''])
<interactive>:2:1: error:
• Kind variable ‘k’ is implicitly bound in data type
‘D1’, but does not appear as the kind of any
of its type variables. Perhaps you meant
to bind it explicitly somewhere?
Type variables with inferred kinds: (k :: *)
• In the data type declaration for ‘D1’
<interactive>:2:32: error: Not in scope: type variable ‘k’
......@@ -13,7 +13,6 @@ other dynamic, non-language, flag settings:
-fimplicit-import-qualified
-fshow-warning-groups
warning settings:
-Wimplicit-kind-vars
-Wmissing-monadfail-instances
-Wsemigroup
-Wnoncanonical-monoid-instances
......@@ -37,7 +36,6 @@ other dynamic, non-language, flag settings:
-fimplicit-import-qualified
-fshow-warning-groups
warning settings:
-Wimplicit-kind-vars
-Wmissing-monadfail-instances
-Wsemigroup
-Wnoncanonical-monoid-instances
......@@ -60,7 +58,6 @@ other dynamic, non-language, flag settings:
-fimplicit-import-qualified
-fshow-warning-groups
warning settings:
-Wimplicit-kind-vars
-Wmissing-monadfail-instances
-Wsemigroup
-Wnoncanonical-monoid-instances
......@@ -85,7 +82,6 @@ other dynamic, non-language, flag settings:
-fimplicit-import-qualified
-fshow-warning-groups
warning settings:
-Wimplicit-kind-vars
-Wmissing-monadfail-instances
-Wsemigroup