{-# LANGUAGE CPP #-}

module GHC.Core.TyCo.FVs
  (     shallowTyCoVarsOfType, shallowTyCoVarsOfTypes,
        tyCoVarsOfType,        tyCoVarsOfTypes,
        tyCoVarsOfTypeDSet, tyCoVarsOfTypesDSet,

        tyCoFVsBndr, tyCoFVsVarBndr, tyCoFVsVarBndrs,
        tyCoFVsOfType, tyCoVarsOfTypeList,
        tyCoFVsOfTypes, tyCoVarsOfTypesList,
        deepTcvFolder,

        shallowTyCoVarsOfTyVarEnv, shallowTyCoVarsOfCoVarEnv,

        shallowTyCoVarsOfCo, shallowTyCoVarsOfCos,
        tyCoVarsOfCo, tyCoVarsOfCos, tyCoVarsOfMCo,
        coVarsOfType, coVarsOfTypes,
        coVarsOfCo, coVarsOfCos,
        tyCoVarsOfCoDSet,
        tyCoFVsOfCo, tyCoFVsOfCos,
        tyCoVarsOfCoList,

        almostDevoidCoVarOfCo,

        -- Injective free vars
        injectiveVarsOfType, injectiveVarsOfTypes,
        invisibleVarsOfType, invisibleVarsOfTypes,

        -- Any and No Free vars
        anyFreeVarsOfType, anyFreeVarsOfTypes, anyFreeVarsOfCo,
        noFreeVarsOfType, noFreeVarsOfTypes, noFreeVarsOfCo,

        -- * Well-scoped free variables
        scopedSort, tyCoVarsOfTypeWellScoped,
        tyCoVarsOfTypesWellScoped,

        -- * Closing over kinds
        closeOverKindsDSet, closeOverKindsList,
        closeOverKinds,

        -- * Raw materials
        Endo(..), runTyCoVars
  ) where

#include "GhclibHsVersions.h"

import GHC.Prelude

import {-# SOURCE #-} GHC.Core.Type (coreView, partitionInvisibleTypes)

import Data.Monoid as DM ( Endo(..), Any(..) )
import GHC.Core.TyCo.Rep
import GHC.Core.TyCon
import GHC.Types.Var
import GHC.Utils.FV

import GHC.Types.Unique.FM
import GHC.Types.Var.Set
import GHC.Types.Var.Env
import GHC.Utils.Misc
import GHC.Utils.Panic

{-
%************************************************************************
%*                                                                      *
                 Free variables of types and coercions
%*                                                                      *
%************************************************************************
-}

{- Note [Shallow and deep free variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Definitions

* Shallow free variables of a type: the variables
  affected by substitution. Specifically, the (TyVarTy tv)
  and (CoVar cv) that appear
    - In the type and coercions appearing in the type
    - In shallow free variables of the kind of a Forall binder
  but NOT in the kind of the /occurrences/ of a type variable.

* Deep free variables of a type: shallow free variables, plus
  the deep free variables of the kinds of those variables.
  That is,  deepFVs( t ) = closeOverKinds( shallowFVs( t ) )

Examples:

  Type                     Shallow     Deep
  ---------------------------------
  (a : (k:Type))           {a}        {a,k}
  forall (a:(k:Type)). a   {k}        {k}
  (a:k->Type) (b:k)        {a,b}      {a,b,k}
-}


{- Note [Free variables of types]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The family of functions tyCoVarsOfType, tyCoVarsOfTypes etc, returns
a VarSet that is closed over the types of its variables.  More precisely,
  if    S = tyCoVarsOfType( t )
  and   (a:k) is in S
  then  tyCoVarsOftype( k ) is a subset of S

Example: The tyCoVars of this ((a:* -> k) Int) is {a, k}.

We could /not/ close over the kinds of the variable occurrences, and
instead do so at call sites, but it seems that we always want to do
so, so it's easiest to do it here.

It turns out that getting the free variables of types is performance critical,
so we profiled several versions, exploring different implementation strategies.

1. Baseline version: uses FV naively. Essentially:

   tyCoVarsOfType ty = fvVarSet $ tyCoFVsOfType ty

   This is not nice, because FV introduces some overhead to implement
   determinism, and through its "interesting var" function, neither of which
   we need here, so they are a complete waste.

2. UnionVarSet version: instead of reusing the FV-based code, we simply used
   VarSets directly, trying to avoid the overhead of FV. E.g.:

   -- FV version:
   tyCoFVsOfType (AppTy fun arg)    a b c = (tyCoFVsOfType fun `unionFV` tyCoFVsOfType arg) a b c

   -- UnionVarSet version:
   tyCoVarsOfType (AppTy fun arg)    = (tyCoVarsOfType fun `unionVarSet` tyCoVarsOfType arg)

   This looks deceptively similar, but while FV internally builds a list- and
   set-generating function, the VarSet functions manipulate sets directly, and
   the latter performs a lot worse than the naive FV version.

3. Accumulator-style VarSet version: this is what we use now. We do use VarSet
   as our data structure, but delegate the actual work to a new
   ty_co_vars_of_...  family of functions, which use accumulator style and the
   "in-scope set" filter found in the internals of FV, but without the
   determinism overhead.

See #14880.

Note [Closing over free variable kinds]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tyCoVarsOfType and tyCoFVsOfType, while traversing a type, will also close over
free variable kinds. In previous GHC versions, this happened naively: whenever
we would encounter an occurrence of a free type variable, we would close over
its kind. This, however is wrong for two reasons (see #14880):

1. Efficiency. If we have Proxy (a::k) -> Proxy (a::k) -> Proxy (a::k), then
   we don't want to have to traverse k more than once.

2. Correctness. Imagine we have forall k. b -> k, where b has
   kind k, for some k bound in an outer scope. If we look at b's kind inside
   the forall, we'll collect that k is free and then remove k from the set of
   free variables. This is plain wrong. We must instead compute that b is free
   and then conclude that b's kind is free.

An obvious first approach is to move the closing-over-kinds from the
occurrences of a type variable to after finding the free vars - however, this
turns out to introduce performance regressions, and isn't even entirely
correct.

In fact, it isn't even important *when* we close over kinds; what matters is
that we handle each type var exactly once, and that we do it in the right
context.

So the next approach we tried was to use the "in-scope set" part of FV or the
equivalent argument in the accumulator-style `ty_co_vars_of_type` function, to
say "don't bother with variables we have already closed over". This should work
fine in theory, but the code is complicated and doesn't perform well.

But there is a simpler way, which is implemented here. Consider the two points
above:

1. Efficiency: we now have an accumulator, so the second time we encounter 'a',
   we'll ignore it, certainly not looking at its kind - this is why
   pre-checking set membership before inserting ends up not only being faster,
   but also being correct.

2. Correctness: we have an "in-scope set" (I think we should call it it a
  "bound-var set"), specifying variables that are bound by a forall in the type
  we are traversing; we simply ignore these variables, certainly not looking at
  their kind.

So now consider:

    forall k. b -> k

where b :: k->Type is free; but of course, it's a different k! When looking at
b -> k we'll have k in the bound-var set. So we'll ignore the k. But suppose
this is our first encounter with b; we want the free vars of its kind. But we
want to behave as if we took the free vars of its kind at the end; that is,
with no bound vars in scope.

So the solution is easy. The old code was this:

  ty_co_vars_of_type (TyVarTy v) is acc
    | v `elemVarSet` is  = acc
    | v `elemVarSet` acc = acc
    | otherwise          = ty_co_vars_of_type (tyVarKind v) is (extendVarSet acc v)

Now all we need to do is take the free vars of tyVarKind v *with an empty
bound-var set*, thus:

ty_co_vars_of_type (TyVarTy v) is acc
  | v `elemVarSet` is  = acc
  | v `elemVarSet` acc = acc
  | otherwise          = ty_co_vars_of_type (tyVarKind v) emptyVarSet (extendVarSet acc v)
                                                          ^^^^^^^^^^^

And that's it. This works because a variable is either bound or free. If it is bound,
then we won't look at it at all. If it is free, then all the variables free in its
kind are free -- regardless of whether some local variable has the same Unique.
So if we're looking at a variable occurrence at all, then all variables in its
kind are free.
-}

{- *********************************************************************
*                                                                      *
          Endo for free variables
*                                                                      *
********************************************************************* -}

{- Note [Acumulating parameter free variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We can use foldType to build an accumulating-parameter version of a
free-var finder, thus:

    fvs :: Type -> TyCoVarSet
    fvs ty = appEndo (foldType folder ty) emptyVarSet

Recall that
    foldType :: TyCoFolder env a -> env -> Type -> a

    newtype Endo a = Endo (a -> a)   -- In Data.Monoid
    instance Monoid a => Monoid (Endo a) where
       (Endo f) `mappend` (Endo g) = Endo (f.g)

    appEndo :: Endo a -> a -> a
    appEndo (Endo f) x = f x

So `mappend` for Endos is just function composition.

It's very important that, after optimisation, we end up with
* an arity-three function
* that is strict in the accumulator

   fvs env (TyVarTy v) acc
      | v `elemVarSet` env = acc
      | v `elemVarSet` acc = acc
      | otherwise          = acc `extendVarSet` v
   fvs env (AppTy t1 t2)   = fvs env t1 (fvs env t2 acc)
   ...

The "strict in the accumulator" part is to ensure that in the
AppTy equation we don't build a thunk for (fvs env t2 acc).

The optimiser does do all this, but not very robustly. It depends
critially on the basic arity-2 function not being exported, so that
all its calls are visibly to three arguments. This analysis is
done by the Call Arity pass.

TL;DR: check this regularly!
-}

runTyCoVars :: Endo TyCoVarSet -> TyCoVarSet
{-# INLINE runTyCoVars #-}
runTyCoVars :: Endo TyCoVarSet -> TyCoVarSet
runTyCoVars Endo TyCoVarSet
f = Endo TyCoVarSet -> TyCoVarSet -> TyCoVarSet
forall a. Endo a -> a -> a
appEndo Endo TyCoVarSet
f TyCoVarSet
emptyVarSet

noView :: Type -> Maybe Type
noView :: Type -> Maybe Type
noView Type
_ = Maybe Type
forall a. Maybe a
Nothing

{- *********************************************************************
*                                                                      *
          Deep free variables
          See Note [Shallow and deep free variables]
*                                                                      *
********************************************************************* -}

tyCoVarsOfType :: Type -> TyCoVarSet
tyCoVarsOfType :: Type -> TyCoVarSet
tyCoVarsOfType Type
ty = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars (Type -> Endo TyCoVarSet
deep_ty Type
ty)
-- Alternative:
--   tyCoVarsOfType ty = closeOverKinds (shallowTyCoVarsOfType ty)

tyCoVarsOfTypes :: [Type] -> TyCoVarSet
tyCoVarsOfTypes :: [Type] -> TyCoVarSet
tyCoVarsOfTypes [Type]
tys = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars ([Type] -> Endo TyCoVarSet
deep_tys [Type]
tys)
-- Alternative:
--   tyCoVarsOfTypes tys = closeOverKinds (shallowTyCoVarsOfTypes tys)

tyCoVarsOfCo :: Coercion -> TyCoVarSet
-- See Note [Free variables of Coercions]
tyCoVarsOfCo :: Coercion -> TyCoVarSet
tyCoVarsOfCo Coercion
co = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars (Coercion -> Endo TyCoVarSet
deep_co Coercion
co)

tyCoVarsOfMCo :: MCoercion -> TyCoVarSet
tyCoVarsOfMCo :: MCoercion -> TyCoVarSet
tyCoVarsOfMCo MCoercion
MRefl    = TyCoVarSet
emptyVarSet
tyCoVarsOfMCo (MCo Coercion
co) = Coercion -> TyCoVarSet
tyCoVarsOfCo Coercion
co

tyCoVarsOfCos :: [Coercion] -> TyCoVarSet
tyCoVarsOfCos :: [Coercion] -> TyCoVarSet
tyCoVarsOfCos [Coercion]
cos = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars ([Coercion] -> Endo TyCoVarSet
deep_cos [Coercion]
cos)

deep_ty  :: Type       -> Endo TyCoVarSet
deep_tys :: [Type]     -> Endo TyCoVarSet
deep_co  :: Coercion   -> Endo TyCoVarSet
deep_cos :: [Coercion] -> Endo TyCoVarSet
(Type -> Endo TyCoVarSet
deep_ty, [Type] -> Endo TyCoVarSet
deep_tys, Coercion -> Endo TyCoVarSet
deep_co, [Coercion] -> Endo TyCoVarSet
deep_cos) = TyCoFolder TyCoVarSet (Endo TyCoVarSet)
-> TyCoVarSet
-> (Type -> Endo TyCoVarSet, [Type] -> Endo TyCoVarSet,
    Coercion -> Endo TyCoVarSet, [Coercion] -> Endo TyCoVarSet)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo TyCoFolder TyCoVarSet (Endo TyCoVarSet)
deepTcvFolder TyCoVarSet
emptyVarSet

deepTcvFolder :: TyCoFolder TyCoVarSet (Endo TyCoVarSet)
deepTcvFolder :: TyCoFolder TyCoVarSet (Endo TyCoVarSet)
deepTcvFolder = TyCoFolder :: forall env a.
(Type -> Maybe Type)
-> (env -> TyVar -> a)
-> (env -> TyVar -> a)
-> (env -> CoercionHole -> a)
-> (env -> TyVar -> ArgFlag -> env)
-> TyCoFolder env a
TyCoFolder { tcf_view :: Type -> Maybe Type
tcf_view = Type -> Maybe Type
noView
                           , tcf_tyvar :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
tcf_tyvar = TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_tcv, tcf_covar :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
tcf_covar = TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_tcv
                           , tcf_hole :: TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
tcf_hole  = TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
do_hole, tcf_tycobinder :: TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
tcf_tycobinder = TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
forall p. TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr }
  where
    do_tcv :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_tcv TyCoVarSet
is TyVar
v = (TyCoVarSet -> TyCoVarSet) -> Endo TyCoVarSet
forall a. (a -> a) -> Endo a
Endo TyCoVarSet -> TyCoVarSet
do_it
      where
        do_it :: TyCoVarSet -> TyCoVarSet
do_it TyCoVarSet
acc | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
is  = TyCoVarSet
acc
                  | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
acc = TyCoVarSet
acc
                  | Bool
otherwise          = Endo TyCoVarSet -> TyCoVarSet -> TyCoVarSet
forall a. Endo a -> a -> a
appEndo (Type -> Endo TyCoVarSet
deep_ty (TyVar -> Type
varType TyVar
v)) (TyCoVarSet -> TyCoVarSet) -> TyCoVarSet -> TyCoVarSet
forall a b. (a -> b) -> a -> b
$
                                         TyCoVarSet
acc TyCoVarSet -> TyVar -> TyCoVarSet
`extendVarSet` TyVar
v

    do_bndr :: TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr TyCoVarSet
is TyVar
tcv p
_ = TyCoVarSet -> TyVar -> TyCoVarSet
extendVarSet TyCoVarSet
is TyVar
tcv
    do_hole :: TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
do_hole TyCoVarSet
is CoercionHole
hole  = TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_tcv TyCoVarSet
is (CoercionHole -> TyVar
coHoleCoVar CoercionHole
hole)
                       -- See Note [CoercionHoles and coercion free variables]
                       -- in GHC.Core.TyCo.Rep

{- *********************************************************************
*                                                                      *
          Shallow free variables
          See Note [Shallow and deep free variables]
*                                                                      *
********************************************************************* -}


shallowTyCoVarsOfType :: Type -> TyCoVarSet
-- See Note [Free variables of types]
shallowTyCoVarsOfType :: Type -> TyCoVarSet
shallowTyCoVarsOfType Type
ty = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars (Type -> Endo TyCoVarSet
shallow_ty Type
ty)

shallowTyCoVarsOfTypes :: [Type] -> TyCoVarSet
shallowTyCoVarsOfTypes :: [Type] -> TyCoVarSet
shallowTyCoVarsOfTypes [Type]
tys = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars ([Type] -> Endo TyCoVarSet
shallow_tys [Type]
tys)

shallowTyCoVarsOfCo :: Coercion -> TyCoVarSet
shallowTyCoVarsOfCo :: Coercion -> TyCoVarSet
shallowTyCoVarsOfCo Coercion
co = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars (Coercion -> Endo TyCoVarSet
shallow_co Coercion
co)

shallowTyCoVarsOfCos :: [Coercion] -> TyCoVarSet
shallowTyCoVarsOfCos :: [Coercion] -> TyCoVarSet
shallowTyCoVarsOfCos [Coercion]
cos = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars ([Coercion] -> Endo TyCoVarSet
shallow_cos [Coercion]
cos)

-- | Returns free variables of types, including kind variables as
-- a non-deterministic set. For type synonyms it does /not/ expand the
-- synonym.
shallowTyCoVarsOfTyVarEnv :: TyVarEnv Type -> TyCoVarSet
-- See Note [Free variables of types]
shallowTyCoVarsOfTyVarEnv :: TyVarEnv Type -> TyCoVarSet
shallowTyCoVarsOfTyVarEnv TyVarEnv Type
tys = [Type] -> TyCoVarSet
shallowTyCoVarsOfTypes (TyVarEnv Type -> [Type]
forall key elt. UniqFM key elt -> [elt]
nonDetEltsUFM TyVarEnv Type
tys)
  -- It's OK to use nonDetEltsUFM here because we immediately
  -- forget the ordering by returning a set

shallowTyCoVarsOfCoVarEnv :: CoVarEnv Coercion -> TyCoVarSet
shallowTyCoVarsOfCoVarEnv :: CoVarEnv Coercion -> TyCoVarSet
shallowTyCoVarsOfCoVarEnv CoVarEnv Coercion
cos = [Coercion] -> TyCoVarSet
shallowTyCoVarsOfCos (CoVarEnv Coercion -> [Coercion]
forall key elt. UniqFM key elt -> [elt]
nonDetEltsUFM CoVarEnv Coercion
cos)
  -- It's OK to use nonDetEltsUFM here because we immediately
  -- forget the ordering by returning a set

shallow_ty  :: Type       -> Endo TyCoVarSet
shallow_tys :: [Type]     -> Endo TyCoVarSet
shallow_co  :: Coercion   -> Endo TyCoVarSet
shallow_cos :: [Coercion] -> Endo TyCoVarSet
(Type -> Endo TyCoVarSet
shallow_ty, [Type] -> Endo TyCoVarSet
shallow_tys, Coercion -> Endo TyCoVarSet
shallow_co, [Coercion] -> Endo TyCoVarSet
shallow_cos) = TyCoFolder TyCoVarSet (Endo TyCoVarSet)
-> TyCoVarSet
-> (Type -> Endo TyCoVarSet, [Type] -> Endo TyCoVarSet,
    Coercion -> Endo TyCoVarSet, [Coercion] -> Endo TyCoVarSet)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo TyCoFolder TyCoVarSet (Endo TyCoVarSet)
shallowTcvFolder TyCoVarSet
emptyVarSet

shallowTcvFolder :: TyCoFolder TyCoVarSet (Endo TyCoVarSet)
shallowTcvFolder :: TyCoFolder TyCoVarSet (Endo TyCoVarSet)
shallowTcvFolder = TyCoFolder :: forall env a.
(Type -> Maybe Type)
-> (env -> TyVar -> a)
-> (env -> TyVar -> a)
-> (env -> CoercionHole -> a)
-> (env -> TyVar -> ArgFlag -> env)
-> TyCoFolder env a
TyCoFolder { tcf_view :: Type -> Maybe Type
tcf_view = Type -> Maybe Type
noView
                              , tcf_tyvar :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
tcf_tyvar = TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_tcv, tcf_covar :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
tcf_covar = TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_tcv
                              , tcf_hole :: TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
tcf_hole  = TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
forall a p p. Monoid a => p -> p -> a
do_hole, tcf_tycobinder :: TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
tcf_tycobinder = TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
forall p. TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr }
  where
    do_tcv :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_tcv TyCoVarSet
is TyVar
v = (TyCoVarSet -> TyCoVarSet) -> Endo TyCoVarSet
forall a. (a -> a) -> Endo a
Endo TyCoVarSet -> TyCoVarSet
do_it
      where
        do_it :: TyCoVarSet -> TyCoVarSet
do_it TyCoVarSet
acc | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
is  = TyCoVarSet
acc
                  | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
acc = TyCoVarSet
acc
                  | Bool
otherwise          = TyCoVarSet
acc TyCoVarSet -> TyVar -> TyCoVarSet
`extendVarSet` TyVar
v

    do_bndr :: TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr TyCoVarSet
is TyVar
tcv p
_ = TyCoVarSet -> TyVar -> TyCoVarSet
extendVarSet TyCoVarSet
is TyVar
tcv
    do_hole :: p -> p -> a
do_hole p
_ p
_  = a
forall a. Monoid a => a
mempty   -- Ignore coercion holes


{- *********************************************************************
*                                                                      *
          Free coercion variables
*                                                                      *
********************************************************************* -}


{- Note [Finding free coercion varibles]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here we are only interested in the free /coercion/ variables.
We can achieve this through a slightly different TyCo folder.

Notice that we look deeply, into kinds.

See #14880.
-}

coVarsOfType  :: Type       -> CoVarSet
coVarsOfTypes :: [Type]     -> CoVarSet
coVarsOfCo    :: Coercion   -> CoVarSet
coVarsOfCos   :: [Coercion] -> CoVarSet

coVarsOfType :: Type -> TyCoVarSet
coVarsOfType  Type
ty  = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars (Type -> Endo TyCoVarSet
deep_cv_ty Type
ty)
coVarsOfTypes :: [Type] -> TyCoVarSet
coVarsOfTypes [Type]
tys = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars ([Type] -> Endo TyCoVarSet
deep_cv_tys [Type]
tys)
coVarsOfCo :: Coercion -> TyCoVarSet
coVarsOfCo    Coercion
co  = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars (Coercion -> Endo TyCoVarSet
deep_cv_co Coercion
co)
coVarsOfCos :: [Coercion] -> TyCoVarSet
coVarsOfCos   [Coercion]
cos = Endo TyCoVarSet -> TyCoVarSet
runTyCoVars ([Coercion] -> Endo TyCoVarSet
deep_cv_cos [Coercion]
cos)

deep_cv_ty  :: Type       -> Endo CoVarSet
deep_cv_tys :: [Type]     -> Endo CoVarSet
deep_cv_co  :: Coercion   -> Endo CoVarSet
deep_cv_cos :: [Coercion] -> Endo CoVarSet
(Type -> Endo TyCoVarSet
deep_cv_ty, [Type] -> Endo TyCoVarSet
deep_cv_tys, Coercion -> Endo TyCoVarSet
deep_cv_co, [Coercion] -> Endo TyCoVarSet
deep_cv_cos) = TyCoFolder TyCoVarSet (Endo TyCoVarSet)
-> TyCoVarSet
-> (Type -> Endo TyCoVarSet, [Type] -> Endo TyCoVarSet,
    Coercion -> Endo TyCoVarSet, [Coercion] -> Endo TyCoVarSet)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo TyCoFolder TyCoVarSet (Endo TyCoVarSet)
deepCoVarFolder TyCoVarSet
emptyVarSet

deepCoVarFolder :: TyCoFolder TyCoVarSet (Endo CoVarSet)
deepCoVarFolder :: TyCoFolder TyCoVarSet (Endo TyCoVarSet)
deepCoVarFolder = TyCoFolder :: forall env a.
(Type -> Maybe Type)
-> (env -> TyVar -> a)
-> (env -> TyVar -> a)
-> (env -> CoercionHole -> a)
-> (env -> TyVar -> ArgFlag -> env)
-> TyCoFolder env a
TyCoFolder { tcf_view :: Type -> Maybe Type
tcf_view = Type -> Maybe Type
noView
                             , tcf_tyvar :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
tcf_tyvar = TyCoVarSet -> TyVar -> Endo TyCoVarSet
forall a p p. Monoid a => p -> p -> a
do_tyvar, tcf_covar :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
tcf_covar = TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_covar
                             , tcf_hole :: TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
tcf_hole  = TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
do_hole, tcf_tycobinder :: TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
tcf_tycobinder = TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
forall p. TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr }
  where
    do_tyvar :: p -> p -> a
do_tyvar p
_ p
_  = a
forall a. Monoid a => a
mempty
      -- This do_tyvar means we won't see any CoVars in this
      -- TyVar's kind.   This may be wrong; but it's the way it's
      -- always been.  And its awkward to change, because
      -- the tyvar won't end up in the accumulator, so
      -- we'd look repeatedly.  Blargh.

    do_covar :: TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_covar TyCoVarSet
is TyVar
v = (TyCoVarSet -> TyCoVarSet) -> Endo TyCoVarSet
forall a. (a -> a) -> Endo a
Endo TyCoVarSet -> TyCoVarSet
do_it
      where
        do_it :: TyCoVarSet -> TyCoVarSet
do_it TyCoVarSet
acc | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
is  = TyCoVarSet
acc
                  | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
acc = TyCoVarSet
acc
                  | Bool
otherwise          = Endo TyCoVarSet -> TyCoVarSet -> TyCoVarSet
forall a. Endo a -> a -> a
appEndo (Type -> Endo TyCoVarSet
deep_cv_ty (TyVar -> Type
varType TyVar
v)) (TyCoVarSet -> TyCoVarSet) -> TyCoVarSet -> TyCoVarSet
forall a b. (a -> b) -> a -> b
$
                                         TyCoVarSet
acc TyCoVarSet -> TyVar -> TyCoVarSet
`extendVarSet` TyVar
v

    do_bndr :: TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr TyCoVarSet
is TyVar
tcv p
_ = TyCoVarSet -> TyVar -> TyCoVarSet
extendVarSet TyCoVarSet
is TyVar
tcv
    do_hole :: TyCoVarSet -> CoercionHole -> Endo TyCoVarSet
do_hole TyCoVarSet
is CoercionHole
hole  = TyCoVarSet -> TyVar -> Endo TyCoVarSet
do_covar TyCoVarSet
is (CoercionHole -> TyVar
coHoleCoVar CoercionHole
hole)
                       -- See Note [CoercionHoles and coercion free variables]
                       -- in GHC.Core.TyCo.Rep


{- *********************************************************************
*                                                                      *
          Closing over kinds
*                                                                      *
********************************************************************* -}

------------- Closing over kinds -----------------

closeOverKinds :: TyCoVarSet -> TyCoVarSet
-- For each element of the input set,
-- add the deep free variables of its kind
closeOverKinds :: TyCoVarSet -> TyCoVarSet
closeOverKinds TyCoVarSet
vs = (TyVar -> TyCoVarSet -> TyCoVarSet)
-> TyCoVarSet -> TyCoVarSet -> TyCoVarSet
forall a. (TyVar -> a -> a) -> a -> TyCoVarSet -> a
nonDetStrictFoldVarSet TyVar -> TyCoVarSet -> TyCoVarSet
do_one TyCoVarSet
vs TyCoVarSet
vs
  where
    do_one :: TyVar -> TyCoVarSet -> TyCoVarSet
do_one TyVar
v TyCoVarSet
acc = Endo TyCoVarSet -> TyCoVarSet -> TyCoVarSet
forall a. Endo a -> a -> a
appEndo (Type -> Endo TyCoVarSet
deep_ty (TyVar -> Type
varType TyVar
v)) TyCoVarSet
acc

{- --------------- Alternative version 1 (using FV) ------------
closeOverKinds = fvVarSet . closeOverKindsFV . nonDetEltsUniqSet
-}

{- ---------------- Alternative version 2 -------------

-- | Add the kind variables free in the kinds of the tyvars in the given set.
-- Returns a non-deterministic set.
closeOverKinds :: TyCoVarSet -> TyCoVarSet
closeOverKinds vs
   = go vs vs
  where
    go :: VarSet   -- Work list
       -> VarSet   -- Accumulator, always a superset of wl
       -> VarSet
    go wl acc
      | isEmptyVarSet wl = acc
      | otherwise        = go wl_kvs (acc `unionVarSet` wl_kvs)
      where
        k v inner_acc = ty_co_vars_of_type (varType v) acc inner_acc
        wl_kvs = nonDetFoldVarSet k emptyVarSet wl
        -- wl_kvs = union of shallow free vars of the kinds of wl
        --          but don't bother to collect vars in acc

-}

{- ---------------- Alternative version 3 -------------
-- | Add the kind variables free in the kinds of the tyvars in the given set.
-- Returns a non-deterministic set.
closeOverKinds :: TyVarSet -> TyVarSet
closeOverKinds vs = close_over_kinds vs emptyVarSet


close_over_kinds :: TyVarSet  -- Work list
                 -> TyVarSet  -- Accumulator
                 -> TyVarSet
-- Precondition: in any call (close_over_kinds wl acc)
--  for every tv in acc, the shallow kind-vars of tv
--  are either in the work list wl, or in acc
-- Postcondition: result is the deep free vars of (wl `union` acc)
close_over_kinds wl acc
  = nonDetFoldVarSet do_one acc wl
  where
    do_one :: Var -> TyVarSet -> TyVarSet
    -- (do_one v acc) adds v and its deep free-vars to acc
    do_one v acc | v `elemVarSet` acc
                 = acc
                 | otherwise
                 = close_over_kinds (shallowTyCoVarsOfType (varType v)) $
                   acc `extendVarSet` v
-}


{- *********************************************************************
*                                                                      *
          The FV versions return deterministic results
*                                                                      *
********************************************************************* -}

-- | Given a list of tyvars returns a deterministic FV computation that
-- returns the given tyvars with the kind variables free in the kinds of the
-- given tyvars.
closeOverKindsFV :: [TyVar] -> FV
closeOverKindsFV :: [TyVar] -> FV
closeOverKindsFV [TyVar]
tvs =
  (TyVar -> FV) -> [TyVar] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV (Type -> FV
tyCoFVsOfType (Type -> FV) -> (TyVar -> Type) -> TyVar -> FV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyVar -> Type
tyVarKind) [TyVar]
tvs FV -> FV -> FV
`unionFV` [TyVar] -> FV
mkFVs [TyVar]
tvs

-- | Add the kind variables free in the kinds of the tyvars in the given set.
-- Returns a deterministically ordered list.
closeOverKindsList :: [TyVar] -> [TyVar]
closeOverKindsList :: [TyVar] -> [TyVar]
closeOverKindsList [TyVar]
tvs = FV -> [TyVar]
fvVarList (FV -> [TyVar]) -> FV -> [TyVar]
forall a b. (a -> b) -> a -> b
$ [TyVar] -> FV
closeOverKindsFV [TyVar]
tvs

-- | Add the kind variables free in the kinds of the tyvars in the given set.
-- Returns a deterministic set.
closeOverKindsDSet :: DTyVarSet -> DTyVarSet
closeOverKindsDSet :: DTyVarSet -> DTyVarSet
closeOverKindsDSet = FV -> DTyVarSet
fvDVarSet (FV -> DTyVarSet) -> (DTyVarSet -> FV) -> DTyVarSet -> DTyVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TyVar] -> FV
closeOverKindsFV ([TyVar] -> FV) -> (DTyVarSet -> [TyVar]) -> DTyVarSet -> FV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DTyVarSet -> [TyVar]
dVarSetElems

-- | `tyCoFVsOfType` that returns free variables of a type in a deterministic
-- set. For explanation of why using `VarSet` is not deterministic see
-- Note [Deterministic FV] in "GHC.Utils.FV".
tyCoVarsOfTypeDSet :: Type -> DTyCoVarSet
-- See Note [Free variables of types]
tyCoVarsOfTypeDSet :: Type -> DTyVarSet
tyCoVarsOfTypeDSet Type
ty = FV -> DTyVarSet
fvDVarSet (FV -> DTyVarSet) -> FV -> DTyVarSet
forall a b. (a -> b) -> a -> b
$ Type -> FV
tyCoFVsOfType Type
ty

-- | `tyCoFVsOfType` that returns free variables of a type in deterministic
-- order. For explanation of why using `VarSet` is not deterministic see
-- Note [Deterministic FV] in "GHC.Utils.FV".
tyCoVarsOfTypeList :: Type -> [TyCoVar]
-- See Note [Free variables of types]
tyCoVarsOfTypeList :: Type -> [TyVar]
tyCoVarsOfTypeList Type
ty = FV -> [TyVar]
fvVarList (FV -> [TyVar]) -> FV -> [TyVar]
forall a b. (a -> b) -> a -> b
$ Type -> FV
tyCoFVsOfType Type
ty

-- | Returns free variables of types, including kind variables as
-- a deterministic set. For type synonyms it does /not/ expand the
-- synonym.
tyCoVarsOfTypesDSet :: [Type] -> DTyCoVarSet
-- See Note [Free variables of types]
tyCoVarsOfTypesDSet :: [Type] -> DTyVarSet
tyCoVarsOfTypesDSet [Type]
tys = FV -> DTyVarSet
fvDVarSet (FV -> DTyVarSet) -> FV -> DTyVarSet
forall a b. (a -> b) -> a -> b
$ [Type] -> FV
tyCoFVsOfTypes [Type]
tys

-- | Returns free variables of types, including kind variables as
-- a deterministically ordered list. For type synonyms it does /not/ expand the
-- synonym.
tyCoVarsOfTypesList :: [Type] -> [TyCoVar]
-- See Note [Free variables of types]
tyCoVarsOfTypesList :: [Type] -> [TyVar]
tyCoVarsOfTypesList [Type]
tys = FV -> [TyVar]
fvVarList (FV -> [TyVar]) -> FV -> [TyVar]
forall a b. (a -> b) -> a -> b
$ [Type] -> FV
tyCoFVsOfTypes [Type]
tys

-- | The worker for `tyCoFVsOfType` and `tyCoFVsOfTypeList`.
-- The previous implementation used `unionVarSet` which is O(n+m) and can
-- make the function quadratic.
-- It's exported, so that it can be composed with
-- other functions that compute free variables.
-- See Note [FV naming conventions] in "GHC.Utils.FV".
--
-- Eta-expanded because that makes it run faster (apparently)
-- See Note [FV eta expansion] in "GHC.Utils.FV" for explanation.
tyCoFVsOfType :: Type -> FV
-- See Note [Free variables of types]
tyCoFVsOfType :: Type -> FV
tyCoFVsOfType (TyVarTy TyVar
v)        InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar]
acc_list, TyCoVarSet
acc_set)
  | Bool -> Bool
not (InterestingVarFun
f TyVar
v) = ([TyVar]
acc_list, TyCoVarSet
acc_set)
  | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
bound_vars = ([TyVar]
acc_list, TyCoVarSet
acc_set)
  | TyVar
v TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
acc_set = ([TyVar]
acc_list, TyCoVarSet
acc_set)
  | Bool
otherwise = Type -> FV
tyCoFVsOfType (TyVar -> Type
tyVarKind TyVar
v) InterestingVarFun
f
                               TyCoVarSet
emptyVarSet   -- See Note [Closing over free variable kinds]
                               (TyVar
vTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:[TyVar]
acc_list, TyCoVarSet -> TyVar -> TyCoVarSet
extendVarSet TyCoVarSet
acc_set TyVar
v)
tyCoFVsOfType (TyConApp TyCon
_ [Type]
tys)   InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc = [Type] -> FV
tyCoFVsOfTypes [Type]
tys InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc
tyCoFVsOfType (LitTy {})         InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc = FV
emptyFV InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc
tyCoFVsOfType (AppTy Type
fun Type
arg)    InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc = (Type -> FV
tyCoFVsOfType Type
fun FV -> FV -> FV
`unionFV` Type -> FV
tyCoFVsOfType Type
arg) InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc
tyCoFVsOfType (FunTy AnonArgFlag
_ Type
w Type
arg Type
res)  InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc = (Type -> FV
tyCoFVsOfType Type
w FV -> FV -> FV
`unionFV` Type -> FV
tyCoFVsOfType Type
arg FV -> FV -> FV
`unionFV` Type -> FV
tyCoFVsOfType Type
res) InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc
tyCoFVsOfType (ForAllTy TyCoVarBinder
bndr Type
ty) InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc = TyCoVarBinder -> FV -> FV
tyCoFVsBndr TyCoVarBinder
bndr (Type -> FV
tyCoFVsOfType Type
ty)  InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc
tyCoFVsOfType (CastTy Type
ty Coercion
co)     InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc = (Type -> FV
tyCoFVsOfType Type
ty FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
co) InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc
tyCoFVsOfType (CoercionTy Coercion
co)    InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
f TyCoVarSet
bound_vars ([TyVar], TyCoVarSet)
acc

tyCoFVsBndr :: TyCoVarBinder -> FV -> FV
-- Free vars of (forall b. <thing with fvs>)
tyCoFVsBndr :: TyCoVarBinder -> FV -> FV
tyCoFVsBndr (Bndr TyVar
tv ArgFlag
_) FV
fvs = TyVar -> FV -> FV
tyCoFVsVarBndr TyVar
tv FV
fvs

tyCoFVsVarBndrs :: [Var] -> FV -> FV
tyCoFVsVarBndrs :: [TyVar] -> FV -> FV
tyCoFVsVarBndrs [TyVar]
vars FV
fvs = (TyVar -> FV -> FV) -> FV -> [TyVar] -> FV
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr TyVar -> FV -> FV
tyCoFVsVarBndr FV
fvs [TyVar]
vars

tyCoFVsVarBndr :: Var -> FV -> FV
tyCoFVsVarBndr :: TyVar -> FV -> FV
tyCoFVsVarBndr TyVar
var FV
fvs
  = Type -> FV
tyCoFVsOfType (TyVar -> Type
varType TyVar
var)   -- Free vars of its type/kind
    FV -> FV -> FV
`unionFV` TyVar -> FV -> FV
delFV TyVar
var FV
fvs       -- Delete it from the thing-inside

tyCoFVsOfTypes :: [Type] -> FV
-- See Note [Free variables of types]
tyCoFVsOfTypes :: [Type] -> FV
tyCoFVsOfTypes (Type
ty:[Type]
tys) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = (Type -> FV
tyCoFVsOfType Type
ty FV -> FV -> FV
`unionFV` [Type] -> FV
tyCoFVsOfTypes [Type]
tys) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfTypes []       InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = FV
emptyFV InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc

-- | Get a deterministic set of the vars free in a coercion
tyCoVarsOfCoDSet :: Coercion -> DTyCoVarSet
-- See Note [Free variables of types]
tyCoVarsOfCoDSet :: Coercion -> DTyVarSet
tyCoVarsOfCoDSet Coercion
co = FV -> DTyVarSet
fvDVarSet (FV -> DTyVarSet) -> FV -> DTyVarSet
forall a b. (a -> b) -> a -> b
$ Coercion -> FV
tyCoFVsOfCo Coercion
co

tyCoVarsOfCoList :: Coercion -> [TyCoVar]
-- See Note [Free variables of types]
tyCoVarsOfCoList :: Coercion -> [TyVar]
tyCoVarsOfCoList Coercion
co = FV -> [TyVar]
fvVarList (FV -> [TyVar]) -> FV -> [TyVar]
forall a b. (a -> b) -> a -> b
$ Coercion -> FV
tyCoFVsOfCo Coercion
co

tyCoFVsOfMCo :: MCoercion -> FV
tyCoFVsOfMCo :: MCoercion -> FV
tyCoFVsOfMCo MCoercion
MRefl    = FV
emptyFV
tyCoFVsOfMCo (MCo Coercion
co) = Coercion -> FV
tyCoFVsOfCo Coercion
co

tyCoFVsOfCo :: Coercion -> FV
-- Extracts type and coercion variables from a coercion
-- See Note [Free variables of types]
tyCoFVsOfCo :: Coercion -> FV
tyCoFVsOfCo (Refl Type
ty) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = Type -> FV
tyCoFVsOfType Type
ty InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (GRefl Role
_ Type
ty MCoercion
mco) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = (Type -> FV
tyCoFVsOfType Type
ty FV -> FV -> FV
`unionFV` MCoercion -> FV
tyCoFVsOfMCo MCoercion
mco) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (TyConAppCo Role
_ TyCon
_ [Coercion]
cos) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = [Coercion] -> FV
tyCoFVsOfCos [Coercion]
cos InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (AppCo Coercion
co Coercion
arg) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = (Coercion -> FV
tyCoFVsOfCo Coercion
co FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
arg) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (ForAllCo TyVar
tv Coercion
kind_co Coercion
co) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = (TyVar -> FV -> FV
tyCoFVsVarBndr TyVar
tv (Coercion -> FV
tyCoFVsOfCo Coercion
co) FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
kind_co) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (FunCo Role
_ Coercion
w Coercion
co1 Coercion
co2)    InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = (Coercion -> FV
tyCoFVsOfCo Coercion
co1 FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
co2 FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
w) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (CoVarCo TyVar
v) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = TyVar -> FV
tyCoFVsOfCoVar TyVar
v InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (HoleCo CoercionHole
h) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = TyVar -> FV
tyCoFVsOfCoVar (CoercionHole -> TyVar
coHoleCoVar CoercionHole
h) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
    -- See Note [CoercionHoles and coercion free variables]
tyCoFVsOfCo (AxiomInstCo CoAxiom Branched
_ BranchIndex
_ [Coercion]
cos) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = [Coercion] -> FV
tyCoFVsOfCos [Coercion]
cos InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (UnivCo UnivCoProvenance
p Role
_ Type
t1 Type
t2) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = (UnivCoProvenance -> FV
tyCoFVsOfProv UnivCoProvenance
p FV -> FV -> FV
`unionFV` Type -> FV
tyCoFVsOfType Type
t1
                     FV -> FV -> FV
`unionFV` Type -> FV
tyCoFVsOfType Type
t2) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (SymCo Coercion
co)          InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (TransCo Coercion
co1 Coercion
co2)   InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = (Coercion -> FV
tyCoFVsOfCo Coercion
co1 FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
co2) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (NthCo Role
_ BranchIndex
_ Coercion
co)      InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (LRCo LeftOrRight
_ Coercion
co)         InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (InstCo Coercion
co Coercion
arg)     InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = (Coercion -> FV
tyCoFVsOfCo Coercion
co FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
arg) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (KindCo Coercion
co)         InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (SubCo Coercion
co)          InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCo (AxiomRuleCo CoAxiomRule
_ [Coercion]
cs)  InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = [Coercion] -> FV
tyCoFVsOfCos [Coercion]
cs InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc

tyCoFVsOfCoVar :: CoVar -> FV
tyCoFVsOfCoVar :: TyVar -> FV
tyCoFVsOfCoVar TyVar
v InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
  = (TyVar -> FV
unitFV TyVar
v FV -> FV -> FV
`unionFV` Type -> FV
tyCoFVsOfType (TyVar -> Type
varType TyVar
v)) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc

tyCoFVsOfProv :: UnivCoProvenance -> FV
tyCoFVsOfProv :: UnivCoProvenance -> FV
tyCoFVsOfProv (PhantomProv Coercion
co)    InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfProv (ProofIrrelProv Coercion
co) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfProv (PluginProv String
_)      InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = FV
emptyFV InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfProv (CorePrepProv Bool
_)    InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = FV
emptyFV InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc

tyCoFVsOfCos :: [Coercion] -> FV
tyCoFVsOfCos :: [Coercion] -> FV
tyCoFVsOfCos []       InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = FV
emptyFV InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc
tyCoFVsOfCos (Coercion
co:[Coercion]
cos) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc = (Coercion -> FV
tyCoFVsOfCo Coercion
co FV -> FV -> FV
`unionFV` [Coercion] -> FV
tyCoFVsOfCos [Coercion]
cos) InterestingVarFun
fv_cand TyCoVarSet
in_scope ([TyVar], TyCoVarSet)
acc


----- Whether a covar is /Almost Devoid/ in a type or coercion ----

-- | Given a covar and a coercion, returns True if covar is almost devoid in
-- the coercion. That is, covar can only appear in Refl and GRefl.
-- See last wrinkle in Note [Unused coercion variable in ForAllCo] in "GHC.Core.Coercion"
almostDevoidCoVarOfCo :: CoVar -> Coercion -> Bool
almostDevoidCoVarOfCo :: TyVar -> Coercion -> Bool
almostDevoidCoVarOfCo TyVar
cv Coercion
co =
  Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv

almost_devoid_co_var_of_co :: Coercion -> CoVar -> Bool
almost_devoid_co_var_of_co :: Coercion -> InterestingVarFun
almost_devoid_co_var_of_co (Refl {}) TyVar
_ = Bool
True   -- covar is allowed in Refl and
almost_devoid_co_var_of_co (GRefl {}) TyVar
_ = Bool
True  -- GRefl, so we don't look into
                                                -- the coercions
almost_devoid_co_var_of_co (TyConAppCo Role
_ TyCon
_ [Coercion]
cos) TyVar
cv
  = [Coercion] -> InterestingVarFun
almost_devoid_co_var_of_cos [Coercion]
cos TyVar
cv
almost_devoid_co_var_of_co (AppCo Coercion
co Coercion
arg) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
  Bool -> Bool -> Bool
&& Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
arg TyVar
cv
almost_devoid_co_var_of_co (ForAllCo TyVar
v Coercion
kind_co Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
kind_co TyVar
cv
  Bool -> Bool -> Bool
&& (TyVar
v TyVar -> InterestingVarFun
forall a. Eq a => a -> a -> Bool
== TyVar
cv Bool -> Bool -> Bool
|| Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv)
almost_devoid_co_var_of_co (FunCo Role
_ Coercion
w Coercion
co1 Coercion
co2) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
w TyVar
cv
  Bool -> Bool -> Bool
&& Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co1 TyVar
cv
  Bool -> Bool -> Bool
&& Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co2 TyVar
cv
almost_devoid_co_var_of_co (CoVarCo TyVar
v) TyVar
cv = TyVar
v TyVar -> InterestingVarFun
forall a. Eq a => a -> a -> Bool
/= TyVar
cv
almost_devoid_co_var_of_co (HoleCo CoercionHole
h)  TyVar
cv = (CoercionHole -> TyVar
coHoleCoVar CoercionHole
h) TyVar -> InterestingVarFun
forall a. Eq a => a -> a -> Bool
/= TyVar
cv
almost_devoid_co_var_of_co (AxiomInstCo CoAxiom Branched
_ BranchIndex
_ [Coercion]
cos) TyVar
cv
  = [Coercion] -> InterestingVarFun
almost_devoid_co_var_of_cos [Coercion]
cos TyVar
cv
almost_devoid_co_var_of_co (UnivCo UnivCoProvenance
p Role
_ Type
t1 Type
t2) TyVar
cv
  = UnivCoProvenance -> InterestingVarFun
almost_devoid_co_var_of_prov UnivCoProvenance
p TyVar
cv
  Bool -> Bool -> Bool
&& Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
t1 TyVar
cv
  Bool -> Bool -> Bool
&& Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
t2 TyVar
cv
almost_devoid_co_var_of_co (SymCo Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_co (TransCo Coercion
co1 Coercion
co2) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co1 TyVar
cv
  Bool -> Bool -> Bool
&& Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co2 TyVar
cv
almost_devoid_co_var_of_co (NthCo Role
_ BranchIndex
_ Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_co (LRCo LeftOrRight
_ Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_co (InstCo Coercion
co Coercion
arg) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
  Bool -> Bool -> Bool
&& Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
arg TyVar
cv
almost_devoid_co_var_of_co (KindCo Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_co (SubCo Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_co (AxiomRuleCo CoAxiomRule
_ [Coercion]
cs) TyVar
cv
  = [Coercion] -> InterestingVarFun
almost_devoid_co_var_of_cos [Coercion]
cs TyVar
cv

almost_devoid_co_var_of_cos :: [Coercion] -> CoVar -> Bool
almost_devoid_co_var_of_cos :: [Coercion] -> InterestingVarFun
almost_devoid_co_var_of_cos [] TyVar
_ = Bool
True
almost_devoid_co_var_of_cos (Coercion
co:[Coercion]
cos) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
  Bool -> Bool -> Bool
&& [Coercion] -> InterestingVarFun
almost_devoid_co_var_of_cos [Coercion]
cos TyVar
cv

almost_devoid_co_var_of_prov :: UnivCoProvenance -> CoVar -> Bool
almost_devoid_co_var_of_prov :: UnivCoProvenance -> InterestingVarFun
almost_devoid_co_var_of_prov (PhantomProv Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_prov (ProofIrrelProv Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_prov (PluginProv String
_)   TyVar
_ = Bool
True
almost_devoid_co_var_of_prov (CorePrepProv Bool
_) TyVar
_ = Bool
True

almost_devoid_co_var_of_type :: Type -> CoVar -> Bool
almost_devoid_co_var_of_type :: Type -> InterestingVarFun
almost_devoid_co_var_of_type (TyVarTy TyVar
_) TyVar
_ = Bool
True
almost_devoid_co_var_of_type (TyConApp TyCon
_ [Type]
tys) TyVar
cv
  = [Type] -> InterestingVarFun
almost_devoid_co_var_of_types [Type]
tys TyVar
cv
almost_devoid_co_var_of_type (LitTy {}) TyVar
_ = Bool
True
almost_devoid_co_var_of_type (AppTy Type
fun Type
arg) TyVar
cv
  = Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
fun TyVar
cv
  Bool -> Bool -> Bool
&& Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
arg TyVar
cv
almost_devoid_co_var_of_type (FunTy AnonArgFlag
_ Type
w Type
arg Type
res) TyVar
cv
  = Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
w TyVar
cv
  Bool -> Bool -> Bool
&& Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
arg TyVar
cv
  Bool -> Bool -> Bool
&& Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
res TyVar
cv
almost_devoid_co_var_of_type (ForAllTy (Bndr TyVar
v ArgFlag
_) Type
ty) TyVar
cv
  = Type -> InterestingVarFun
almost_devoid_co_var_of_type (TyVar -> Type
varType TyVar
v) TyVar
cv
  Bool -> Bool -> Bool
&& (TyVar
v TyVar -> InterestingVarFun
forall a. Eq a => a -> a -> Bool
== TyVar
cv Bool -> Bool -> Bool
|| Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
ty TyVar
cv)
almost_devoid_co_var_of_type (CastTy Type
ty Coercion
co) TyVar
cv
  = Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
ty TyVar
cv
  Bool -> Bool -> Bool
&& Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv
almost_devoid_co_var_of_type (CoercionTy Coercion
co) TyVar
cv
  = Coercion -> InterestingVarFun
almost_devoid_co_var_of_co Coercion
co TyVar
cv

almost_devoid_co_var_of_types :: [Type] -> CoVar -> Bool
almost_devoid_co_var_of_types :: [Type] -> InterestingVarFun
almost_devoid_co_var_of_types [] TyVar
_ = Bool
True
almost_devoid_co_var_of_types (Type
ty:[Type]
tys) TyVar
cv
  = Type -> InterestingVarFun
almost_devoid_co_var_of_type Type
ty TyVar
cv
  Bool -> Bool -> Bool
&& [Type] -> InterestingVarFun
almost_devoid_co_var_of_types [Type]
tys TyVar
cv



{- *********************************************************************
*                                                                      *
                 Injective free vars
*                                                                      *
********************************************************************* -}

-- | Returns the free variables of a 'Type' that are in injective positions.
-- Specifically, it finds the free variables while:
--
-- * Expanding type synonyms
--
-- * Ignoring the coercion in @(ty |> co)@
--
-- * Ignoring the non-injective fields of a 'TyConApp'
--
--
-- For example, if @F@ is a non-injective type family, then:
--
-- @
-- injectiveTyVarsOf( Either c (Maybe (a, F b c)) ) = {a,c}
-- @
--
-- If @'injectiveVarsOfType' ty = itvs@, then knowing @ty@ fixes @itvs@.
-- More formally, if
-- @a@ is in @'injectiveVarsOfType' ty@
-- and  @S1(ty) ~ S2(ty)@,
-- then @S1(a)  ~ S2(a)@,
-- where @S1@ and @S2@ are arbitrary substitutions.
--
-- See @Note [When does a tycon application need an explicit kind signature?]@.
injectiveVarsOfType :: Bool   -- ^ Should we look under injective type families?
                              -- See Note [Coverage condition for injective type families]
                              -- in "GHC.Tc.Instance.Family".
                    -> Type -> FV
injectiveVarsOfType :: Bool -> Type -> FV
injectiveVarsOfType Bool
look_under_tfs = Type -> FV
go
  where
    go :: Type -> FV
go Type
ty                  | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty
                           = Type -> FV
go Type
ty'
    go (TyVarTy TyVar
v)         = TyVar -> FV
unitFV TyVar
v FV -> FV -> FV
`unionFV` Type -> FV
go (TyVar -> Type
tyVarKind TyVar
v)
    go (AppTy Type
f Type
a)         = Type -> FV
go Type
f FV -> FV -> FV
`unionFV` Type -> FV
go Type
a
    go (FunTy AnonArgFlag
_ Type
w Type
ty1 Type
ty2) = Type -> FV
go Type
w FV -> FV -> FV
`unionFV` Type -> FV
go Type
ty1 FV -> FV -> FV
`unionFV` Type -> FV
go Type
ty2
    go (TyConApp TyCon
tc [Type]
tys)   =
      case TyCon -> Injectivity
tyConInjectivityInfo TyCon
tc of
        Injective [Bool]
inj
          |  Bool
look_under_tfs Bool -> Bool -> Bool
|| Bool -> Bool
not (TyCon -> Bool
isTypeFamilyTyCon TyCon
tc)
          -> (Type -> FV) -> [Type] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV Type -> FV
go ([Type] -> FV) -> [Type] -> FV
forall a b. (a -> b) -> a -> b
$
             [Bool] -> [Type] -> [Type]
forall a. [Bool] -> [a] -> [a]
filterByList ([Bool]
inj [Bool] -> [Bool] -> [Bool]
forall a. [a] -> [a] -> [a]
++ Bool -> [Bool]
forall a. a -> [a]
repeat Bool
True) [Type]
tys
                         -- Oversaturated arguments to a tycon are
                         -- always injective, hence the repeat True
        Injectivity
_ -> FV
emptyFV
    go (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) = Type -> FV
go (TyVar -> Type
tyVarKind TyVar
tv) FV -> FV -> FV
`unionFV` TyVar -> FV -> FV
delFV TyVar
tv (Type -> FV
go Type
ty)
    go LitTy{}                   = FV
emptyFV
    go (CastTy Type
ty Coercion
_)             = Type -> FV
go Type
ty
    go CoercionTy{}              = FV
emptyFV

-- | Returns the free variables of a 'Type' that are in injective positions.
-- Specifically, it finds the free variables while:
--
-- * Expanding type synonyms
--
-- * Ignoring the coercion in @(ty |> co)@
--
-- * Ignoring the non-injective fields of a 'TyConApp'
--
-- See @Note [When does a tycon application need an explicit kind signature?]@.
injectiveVarsOfTypes :: Bool -- ^ look under injective type families?
                             -- See Note [Coverage condition for injective type families]
                             -- in "GHC.Tc.Instance.Family".
                     -> [Type] -> FV
injectiveVarsOfTypes :: Bool -> [Type] -> FV
injectiveVarsOfTypes Bool
look_under_tfs = (Type -> FV) -> [Type] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV (Bool -> Type -> FV
injectiveVarsOfType Bool
look_under_tfs)


{- *********************************************************************
*                                                                      *
                 Invisible vars
*                                                                      *
********************************************************************* -}


-- | Returns the set of variables that are used invisibly anywhere within
-- the given type. A variable will be included even if it is used both visibly
-- and invisibly. An invisible use site includes:
--   * In the kind of a variable
--   * In the kind of a bound variable in a forall
--   * In a coercion
--   * In a Specified or Inferred argument to a function
-- See Note [VarBndrs, TyCoVarBinders, TyConBinders, and visibility] in "GHC.Core.TyCo.Rep"
invisibleVarsOfType :: Type -> FV
invisibleVarsOfType :: Type -> FV
invisibleVarsOfType = Type -> FV
go
  where
    go :: Type -> FV
go Type
ty                 | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty
                          = Type -> FV
go Type
ty'
    go (TyVarTy TyVar
v)        = Type -> FV
go (TyVar -> Type
tyVarKind TyVar
v)
    go (AppTy Type
f Type
a)        = Type -> FV
go Type
f FV -> FV -> FV
`unionFV` Type -> FV
go Type
a
    go (FunTy AnonArgFlag
_ Type
w Type
ty1 Type
ty2) = Type -> FV
go Type
w FV -> FV -> FV
`unionFV` Type -> FV
go Type
ty1 FV -> FV -> FV
`unionFV` Type -> FV
go Type
ty2
    go (TyConApp TyCon
tc [Type]
tys)  = [Type] -> FV
tyCoFVsOfTypes [Type]
invisibles FV -> FV -> FV
`unionFV`
                            [Type] -> FV
invisibleVarsOfTypes [Type]
visibles
      where ([Type]
invisibles, [Type]
visibles) = TyCon -> [Type] -> ([Type], [Type])
partitionInvisibleTypes TyCon
tc [Type]
tys
    go (ForAllTy TyCoVarBinder
tvb Type
ty)  = TyCoVarBinder -> FV -> FV
tyCoFVsBndr TyCoVarBinder
tvb (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ Type -> FV
go Type
ty
    go LitTy{}            = FV
emptyFV
    go (CastTy Type
ty Coercion
co)     = Coercion -> FV
tyCoFVsOfCo Coercion
co FV -> FV -> FV
`unionFV` Type -> FV
go Type
ty
    go (CoercionTy Coercion
co)    = Coercion -> FV
tyCoFVsOfCo Coercion
co

-- | Like 'invisibleVarsOfType', but for many types.
invisibleVarsOfTypes :: [Type] -> FV
invisibleVarsOfTypes :: [Type] -> FV
invisibleVarsOfTypes = (Type -> FV) -> [Type] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV Type -> FV
invisibleVarsOfType


{- *********************************************************************
*                                                                      *
                 Any free vars
*                                                                      *
********************************************************************* -}

{-# INLINE afvFolder #-}   -- so that specialization to (const True) works
afvFolder :: (TyCoVar -> Bool) -> TyCoFolder TyCoVarSet DM.Any
afvFolder :: InterestingVarFun -> TyCoFolder TyCoVarSet Any
afvFolder InterestingVarFun
check_fv = TyCoFolder :: forall env a.
(Type -> Maybe Type)
-> (env -> TyVar -> a)
-> (env -> TyVar -> a)
-> (env -> CoercionHole -> a)
-> (env -> TyVar -> ArgFlag -> env)
-> TyCoFolder env a
TyCoFolder { tcf_view :: Type -> Maybe Type
tcf_view = Type -> Maybe Type
noView
                                , tcf_tyvar :: TyCoVarSet -> TyVar -> Any
tcf_tyvar = TyCoVarSet -> TyVar -> Any
do_tcv, tcf_covar :: TyCoVarSet -> TyVar -> Any
tcf_covar = TyCoVarSet -> TyVar -> Any
do_tcv
                                , tcf_hole :: TyCoVarSet -> CoercionHole -> Any
tcf_hole = TyCoVarSet -> CoercionHole -> Any
forall p p. p -> p -> Any
do_hole, tcf_tycobinder :: TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
tcf_tycobinder = TyCoVarSet -> TyVar -> ArgFlag -> TyCoVarSet
forall p. TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr }
  where
    do_tcv :: TyCoVarSet -> TyVar -> Any
do_tcv TyCoVarSet
is TyVar
tv = Bool -> Any
Any (Bool -> Bool
not (TyVar
tv TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
is) Bool -> Bool -> Bool
&& InterestingVarFun
check_fv TyVar
tv)
    do_hole :: p -> p -> Any
do_hole p
_ p
_  = Bool -> Any
Any Bool
False    -- I'm unsure; probably never happens
    do_bndr :: TyCoVarSet -> TyVar -> p -> TyCoVarSet
do_bndr TyCoVarSet
is TyVar
tv p
_ = TyCoVarSet
is TyCoVarSet -> TyVar -> TyCoVarSet
`extendVarSet` TyVar
tv

anyFreeVarsOfType :: (TyCoVar -> Bool) -> Type -> Bool
anyFreeVarsOfType :: InterestingVarFun -> Type -> Bool
anyFreeVarsOfType InterestingVarFun
check_fv Type
ty = Any -> Bool
DM.getAny (Type -> Any
f Type
ty)
  where (Type -> Any
f, [Type] -> Any
_, Coercion -> Any
_, [Coercion] -> Any
_) = TyCoFolder TyCoVarSet Any
-> TyCoVarSet
-> (Type -> Any, [Type] -> Any, Coercion -> Any, [Coercion] -> Any)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo (InterestingVarFun -> TyCoFolder TyCoVarSet Any
afvFolder InterestingVarFun
check_fv) TyCoVarSet
emptyVarSet

anyFreeVarsOfTypes :: (TyCoVar -> Bool) -> [Type] -> Bool
anyFreeVarsOfTypes :: InterestingVarFun -> [Type] -> Bool
anyFreeVarsOfTypes InterestingVarFun
check_fv [Type]
tys = Any -> Bool
DM.getAny ([Type] -> Any
f [Type]
tys)
  where (Type -> Any
_, [Type] -> Any
f, Coercion -> Any
_, [Coercion] -> Any
_) = TyCoFolder TyCoVarSet Any
-> TyCoVarSet
-> (Type -> Any, [Type] -> Any, Coercion -> Any, [Coercion] -> Any)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo (InterestingVarFun -> TyCoFolder TyCoVarSet Any
afvFolder InterestingVarFun
check_fv) TyCoVarSet
emptyVarSet

anyFreeVarsOfCo :: (TyCoVar -> Bool) -> Coercion -> Bool
anyFreeVarsOfCo :: InterestingVarFun -> Coercion -> Bool
anyFreeVarsOfCo InterestingVarFun
check_fv Coercion
co = Any -> Bool
DM.getAny (Coercion -> Any
f Coercion
co)
  where (Type -> Any
_, [Type] -> Any
_, Coercion -> Any
f, [Coercion] -> Any
_) = TyCoFolder TyCoVarSet Any
-> TyCoVarSet
-> (Type -> Any, [Type] -> Any, Coercion -> Any, [Coercion] -> Any)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo (InterestingVarFun -> TyCoFolder TyCoVarSet Any
afvFolder InterestingVarFun
check_fv) TyCoVarSet
emptyVarSet

noFreeVarsOfType :: Type -> Bool
noFreeVarsOfType :: Type -> Bool
noFreeVarsOfType Type
ty = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Any -> Bool
DM.getAny (Type -> Any
f Type
ty)
  where (Type -> Any
f, [Type] -> Any
_, Coercion -> Any
_, [Coercion] -> Any
_) = TyCoFolder TyCoVarSet Any
-> TyCoVarSet
-> (Type -> Any, [Type] -> Any, Coercion -> Any, [Coercion] -> Any)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo (InterestingVarFun -> TyCoFolder TyCoVarSet Any
afvFolder (Bool -> InterestingVarFun
forall a b. a -> b -> a
const Bool
True)) TyCoVarSet
emptyVarSet

noFreeVarsOfTypes :: [Type] -> Bool
noFreeVarsOfTypes :: [Type] -> Bool
noFreeVarsOfTypes [Type]
tys = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Any -> Bool
DM.getAny ([Type] -> Any
f [Type]
tys)
  where (Type -> Any
_, [Type] -> Any
f, Coercion -> Any
_, [Coercion] -> Any
_) = TyCoFolder TyCoVarSet Any
-> TyCoVarSet
-> (Type -> Any, [Type] -> Any, Coercion -> Any, [Coercion] -> Any)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo (InterestingVarFun -> TyCoFolder TyCoVarSet Any
afvFolder (Bool -> InterestingVarFun
forall a b. a -> b -> a
const Bool
True)) TyCoVarSet
emptyVarSet

noFreeVarsOfCo :: Coercion -> Bool
noFreeVarsOfCo :: Coercion -> Bool
noFreeVarsOfCo Coercion
co = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Any -> Bool
DM.getAny (Coercion -> Any
f Coercion
co)
  where (Type -> Any
_, [Type] -> Any
_, Coercion -> Any
f, [Coercion] -> Any
_) = TyCoFolder TyCoVarSet Any
-> TyCoVarSet
-> (Type -> Any, [Type] -> Any, Coercion -> Any, [Coercion] -> Any)
forall a env.
Monoid a =>
TyCoFolder env a
-> env -> (Type -> a, [Type] -> a, Coercion -> a, [Coercion] -> a)
foldTyCo (InterestingVarFun -> TyCoFolder TyCoVarSet Any
afvFolder (Bool -> InterestingVarFun
forall a b. a -> b -> a
const Bool
True)) TyCoVarSet
emptyVarSet


{- *********************************************************************
*                                                                      *
                 scopedSort
*                                                                      *
********************************************************************* -}

{- Note [ScopedSort]
~~~~~~~~~~~~~~~~~~~~
Consider

  foo :: Proxy a -> Proxy (b :: k) -> Proxy (a :: k2) -> ()

This function type is implicitly generalised over [a, b, k, k2]. These
variables will be Specified; that is, they will be available for visible
type application. This is because they are written in the type signature
by the user.

However, we must ask: what order will they appear in? In cases without
dependency, this is easy: we just use the lexical left-to-right ordering
of first occurrence. With dependency, we cannot get off the hook so
easily.

We thus state:

 * These variables appear in the order as given by ScopedSort, where
   the input to ScopedSort is the left-to-right order of first occurrence.

Note that this applies only to *implicit* quantification, without a
`forall`. If the user writes a `forall`, then we just use the order given.

ScopedSort is defined thusly (as proposed in #15743):
  * Work left-to-right through the input list, with a cursor.
  * If variable v at the cursor is depended on by any earlier variable w,
    move v immediately before the leftmost such w.

INVARIANT: The prefix of variables before the cursor form a valid telescope.

Note that ScopedSort makes sense only after type inference is done and all
types/kinds are fully settled and zonked.

-}

-- | Do a topological sort on a list of tyvars,
--   so that binders occur before occurrences
-- E.g. given  [ a::k, k::*, b::k ]
-- it'll return a well-scoped list [ k::*, a::k, b::k ]
--
-- This is a deterministic sorting operation
-- (that is, doesn't depend on Uniques).
--
-- It is also meant to be stable: that is, variables should not
-- be reordered unnecessarily. This is specified in Note [ScopedSort]
-- See also Note [Ordering of implicit variables] in "GHC.Rename.HsType"

scopedSort :: [TyCoVar] -> [TyCoVar]
scopedSort :: [TyVar] -> [TyVar]
scopedSort = [TyVar] -> [TyCoVarSet] -> [TyVar] -> [TyVar]
go [] []
  where
    go :: [TyCoVar] -- already sorted, in reverse order
       -> [TyCoVarSet] -- each set contains all the variables which must be placed
                       -- before the tv corresponding to the set; they are accumulations
                       -- of the fvs in the sorted tvs' kinds

                       -- This list is in 1-to-1 correspondence with the sorted tyvars
                       -- INVARIANT:
                       --   all (\tl -> all (`subVarSet` head tl) (tail tl)) (tails fv_list)
                       -- That is, each set in the list is a superset of all later sets.

       -> [TyCoVar] -- yet to be sorted
       -> [TyCoVar]
    go :: [TyVar] -> [TyCoVarSet] -> [TyVar] -> [TyVar]
go [TyVar]
acc [TyCoVarSet]
_fv_list [] = [TyVar] -> [TyVar]
forall a. [a] -> [a]
reverse [TyVar]
acc
    go [TyVar]
acc  [TyCoVarSet]
fv_list (TyVar
tv:[TyVar]
tvs)
      = [TyVar] -> [TyCoVarSet] -> [TyVar] -> [TyVar]
go [TyVar]
acc' [TyCoVarSet]
fv_list' [TyVar]
tvs
      where
        ([TyVar]
acc', [TyCoVarSet]
fv_list') = TyVar -> [TyVar] -> [TyCoVarSet] -> ([TyVar], [TyCoVarSet])
insert TyVar
tv [TyVar]
acc [TyCoVarSet]
fv_list

    insert :: TyCoVar       -- var to insert
           -> [TyCoVar]     -- sorted list, in reverse order
           -> [TyCoVarSet]  -- list of fvs, as above
           -> ([TyCoVar], [TyCoVarSet])   -- augmented lists
    insert :: TyVar -> [TyVar] -> [TyCoVarSet] -> ([TyVar], [TyCoVarSet])
insert TyVar
tv []     []         = ([TyVar
tv], [Type -> TyCoVarSet
tyCoVarsOfType (TyVar -> Type
tyVarKind TyVar
tv)])
    insert TyVar
tv (TyVar
a:[TyVar]
as) (TyCoVarSet
fvs:[TyCoVarSet]
fvss)
      | TyVar
tv TyVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
fvs
      , ([TyVar]
as', [TyCoVarSet]
fvss') <- TyVar -> [TyVar] -> [TyCoVarSet] -> ([TyVar], [TyCoVarSet])
insert TyVar
tv [TyVar]
as [TyCoVarSet]
fvss
      = (TyVar
aTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:[TyVar]
as', TyCoVarSet
fvs TyCoVarSet -> TyCoVarSet -> TyCoVarSet
`unionVarSet` TyCoVarSet
fv_tv TyCoVarSet -> [TyCoVarSet] -> [TyCoVarSet]
forall a. a -> [a] -> [a]
: [TyCoVarSet]
fvss')

      | Bool
otherwise
      = (TyVar
tvTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:TyVar
aTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:[TyVar]
as, TyCoVarSet
fvs TyCoVarSet -> TyCoVarSet -> TyCoVarSet
`unionVarSet` TyCoVarSet
fv_tv TyCoVarSet -> [TyCoVarSet] -> [TyCoVarSet]
forall a. a -> [a] -> [a]
: TyCoVarSet
fvs TyCoVarSet -> [TyCoVarSet] -> [TyCoVarSet]
forall a. a -> [a] -> [a]
: [TyCoVarSet]
fvss)
      where
        fv_tv :: TyCoVarSet
fv_tv = Type -> TyCoVarSet
tyCoVarsOfType (TyVar -> Type
tyVarKind TyVar
tv)

       -- lists not in correspondence
    insert TyVar
_ [TyVar]
_ [TyCoVarSet]
_ = String -> ([TyVar], [TyCoVarSet])
forall a. String -> a
panic String
"scopedSort"

-- | Get the free vars of a type in scoped order
tyCoVarsOfTypeWellScoped :: Type -> [TyVar]
tyCoVarsOfTypeWellScoped :: Type -> [TyVar]
tyCoVarsOfTypeWellScoped = [TyVar] -> [TyVar]
scopedSort ([TyVar] -> [TyVar]) -> (Type -> [TyVar]) -> Type -> [TyVar]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> [TyVar]
tyCoVarsOfTypeList

-- | Get the free vars of types in scoped order
tyCoVarsOfTypesWellScoped :: [Type] -> [TyVar]
tyCoVarsOfTypesWellScoped :: [Type] -> [TyVar]
tyCoVarsOfTypesWellScoped = [TyVar] -> [TyVar]
scopedSort ([TyVar] -> [TyVar]) -> ([Type] -> [TyVar]) -> [Type] -> [TyVar]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Type] -> [TyVar]
tyCoVarsOfTypesList