{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

-}


{-# LANGUAGE MultiWayIf #-}

-- | Functions for inferring (and simplifying) the context for derived instances.
module GHC.Tc.Deriv.Infer
   ( inferConstraints
   , simplifyInstanceContexts
   )
where

import GHC.Prelude

import GHC.Tc.Deriv.Utils
import GHC.Tc.Utils.Env
import GHC.Tc.Deriv.Generate
import GHC.Tc.Deriv.Functor
import GHC.Tc.Deriv.Generics
import GHC.Tc.Utils.TcMType
import GHC.Tc.Utils.Monad
import GHC.Tc.Types.Origin
import GHC.Tc.Types.Constraint
import GHC.Tc.Utils.TcType
import GHC.Tc.Solver
import GHC.Tc.Solver.Monad ( runTcS )
import GHC.Tc.Validity (validDerivPred)
import GHC.Tc.Utils.Unify (buildImplicationFor)
import GHC.Tc.Zonk.TcType ( zonkWC )
import GHC.Tc.Zonk.Env ( ZonkFlexi(..), initZonkEnv )

import GHC.Core.Class
import GHC.Core.DataCon
import GHC.Core.TyCon
import GHC.Core.TyCo.Ppr (pprTyVars)
import GHC.Core.Type
import GHC.Core.Predicate
import GHC.Core.Unify (tcUnifyTy)

import GHC.Data.Pair
import GHC.Builtin.Names
import GHC.Builtin.Types (typeToTypeKind)

import GHC.Utils.Error
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Misc

import GHC.Types.Basic
import GHC.Types.Var

import GHC.Data.Bag

import Control.Monad
import Control.Monad.Trans.Class  (lift)
import Control.Monad.Trans.Reader (ask)
import Data.Function              (on)
import Data.Functor.Classes       (liftEq)
import Data.List                  (sortBy)
import Data.Maybe

----------------------

inferConstraints :: DerivSpecMechanism
                 -> DerivM (ThetaSpec, [TyVar], [TcType], DerivSpecMechanism)
-- inferConstraints figures out the constraints needed for the
-- instance declaration generated by a 'deriving' clause on a
-- data type declaration. It also returns the new in-scope type
-- variables and instance types, in case they were changed due to
-- the presence of functor-like constraints.
-- See Note [Inferring the instance context]

-- e.g. inferConstraints
--        C Int (T [a])    -- Class and inst_tys
--        :RTList a        -- Rep tycon and its arg tys
-- where T [a] ~R :RTList a
--
-- Generate a sufficiently large set of constraints that typechecking the
-- generated method definitions should succeed.   This set will be simplified
-- before being used in the instance declaration
inferConstraints :: DerivSpecMechanism
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism)
inferConstraints DerivSpecMechanism
mechanism
  = do { DerivEnv { denv_tvs      = tvs
                  , denv_cls      = main_cls
                  , denv_inst_tys = inst_tys } <- ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
       ; wildcard <- isStandaloneWildcardDeriv
       ; let infer_constraints :: DerivM (ThetaSpec, [TyVar], [TcType], DerivSpecMechanism)
             infer_constraints =
               case DerivSpecMechanism
mechanism of
                 DerivSpecStock{dsm_stock_dit :: DerivSpecMechanism -> DerivInstTys
dsm_stock_dit = DerivInstTys
dit}
                   -> do (thetas, tvs, inst_tys, dit') <- DerivInstTys
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivInstTys)
inferConstraintsStock DerivInstTys
dit
                         pure ( thetas, tvs, inst_tys
                              , mechanism{dsm_stock_dit = dit'} )
                 DerivSpecMechanism
DerivSpecAnyClass
                   -> DerivM ThetaSpec
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism)
infer_constraints_simple DerivM ThetaSpec
inferConstraintsAnyclass
                 DerivSpecNewtype { dsm_newtype_dit :: DerivSpecMechanism -> DerivInstTys
dsm_newtype_dit =
                                      DerivInstTys{dit_cls_tys :: DerivInstTys -> [PredType]
dit_cls_tys = [PredType]
cls_tys}
                                  , dsm_newtype_rep_ty :: DerivSpecMechanism -> PredType
dsm_newtype_rep_ty = PredType
rep_ty }
                   -> DerivM ThetaSpec
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism)
infer_constraints_simple (DerivM ThetaSpec
 -> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism))
-> DerivM ThetaSpec
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism)
forall a b. (a -> b) -> a -> b
$
                      [PredType] -> PredType -> DerivM ThetaSpec
inferConstraintsCoerceBased [PredType]
cls_tys PredType
rep_ty
                 DerivSpecVia { dsm_via_cls_tys :: DerivSpecMechanism -> [PredType]
dsm_via_cls_tys = [PredType]
cls_tys
                              , dsm_via_ty :: DerivSpecMechanism -> PredType
dsm_via_ty = PredType
via_ty }
                   -> DerivM ThetaSpec
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism)
infer_constraints_simple (DerivM ThetaSpec
 -> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism))
-> DerivM ThetaSpec
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivSpecMechanism)
forall a b. (a -> b) -> a -> b
$
                      [PredType] -> PredType -> DerivM ThetaSpec
inferConstraintsCoerceBased [PredType]
cls_tys PredType
via_ty

             -- Most deriving strategies do not need to do anything special to
             -- the type variables and arguments to the class in the derived
             -- instance, so they can pass through unchanged. The exception to
             -- this rule is stock deriving. See
             -- Note [Inferring the instance context].
             infer_constraints_simple
               :: DerivM ThetaSpec
               -> DerivM (ThetaSpec, [TyVar], [TcType], DerivSpecMechanism)
             infer_constraints_simple DerivM ThetaSpec
infer_thetas = do
               thetas <- DerivM ThetaSpec
infer_thetas
               pure (thetas, tvs, inst_tys, mechanism)

             -- Constraints arising from superclasses
             -- See Note [Superclasses of derived instance]
             cls_tvs  = Class -> [TyVar]
classTyVars Class
main_cls
             sc_constraints = Bool -> SDoc -> ThetaSpec -> ThetaSpec
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr ([TyVar] -> [PredType] -> Bool
forall a b. [a] -> [b] -> Bool
equalLength [TyVar]
cls_tvs [PredType]
inst_tys)
                                        (Class -> SDoc
forall a. Outputable a => a -> SDoc
ppr Class
main_cls SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> [PredType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [PredType]
inst_tys) (ThetaSpec -> ThetaSpec) -> ThetaSpec -> ThetaSpec
forall a b. (a -> b) -> a -> b
$
                              CtOrigin -> TypeOrKind -> [PredType] -> ThetaSpec
mkDirectThetaSpec
                                (Bool -> CtOrigin
mkDerivOrigin Bool
wildcard) TypeOrKind
TypeLevel
                                (HasDebugCallStack => Subst -> [PredType] -> [PredType]
Subst -> [PredType] -> [PredType]
substTheta Subst
cls_subst (Class -> [PredType]
classSCTheta Class
main_cls))
             cls_subst = Bool -> Subst -> Subst
forall a. HasCallStack => Bool -> a -> a
assert ([TyVar] -> [PredType] -> Bool
forall a b. [a] -> [b] -> Bool
equalLength [TyVar]
cls_tvs [PredType]
inst_tys) (Subst -> Subst) -> Subst -> Subst
forall a b. (a -> b) -> a -> b
$
                         [TyVar] -> [PredType] -> Subst
HasDebugCallStack => [TyVar] -> [PredType] -> Subst
zipTvSubst [TyVar]
cls_tvs [PredType]
inst_tys

       ; (inferred_constraints, tvs', inst_tys', mechanism')
           <- infer_constraints
       ; lift $ traceTc "inferConstraints" $ vcat
              [ ppr main_cls <+> ppr inst_tys'
              , ppr inferred_constraints
              ]
       ; return ( sc_constraints ++ inferred_constraints
                , tvs', inst_tys', mechanism' ) }

-- | Like 'inferConstraints', but used only in the case of the @stock@ deriving
-- strategy. The constraints are inferred by inspecting the fields of each data
-- constructor. In this example:
--
-- > data Foo = MkFoo Int Char deriving Show
--
-- We would infer the following constraints ('ThetaSpec's):
--
-- > (Show Int, Show Char)
--
-- Note that this function also returns the type variables ('TyVar's) and
-- class arguments ('TcType's) for the resulting instance. This is because
-- when deriving 'Functor'-like classes, we must sometimes perform kind
-- substitutions to ensure the resulting instance is well kinded, which may
-- affect the type variables and class arguments. In this example:
--
-- > newtype Compose (f :: k -> Type) (g :: Type -> k) (a :: Type) =
-- >   Compose (f (g a)) deriving stock Functor
--
-- We must unify @k@ with @Type@ in order for the resulting 'Functor' instance
-- to be well kinded, so we return @[]@/@[Type, f, g]@ for the
-- 'TyVar's/'TcType's, /not/ @[k]@/@[k, f, g]@.
-- See Note [Inferring the instance context].
inferConstraintsStock :: DerivInstTys
                      -> DerivM (ThetaSpec, [TyVar], [TcType], DerivInstTys)
inferConstraintsStock :: DerivInstTys
-> DerivM (ThetaSpec, [TyVar], [PredType], DerivInstTys)
inferConstraintsStock dit :: DerivInstTys
dit@(DerivInstTys { dit_cls_tys :: DerivInstTys -> [PredType]
dit_cls_tys     = [PredType]
cls_tys
                                        , dit_tc :: DerivInstTys -> TyCon
dit_tc          = TyCon
tc
                                        , dit_tc_args :: DerivInstTys -> [PredType]
dit_tc_args     = [PredType]
tc_args
                                        , dit_rep_tc :: DerivInstTys -> TyCon
dit_rep_tc      = TyCon
rep_tc
                                        , dit_rep_tc_args :: DerivInstTys -> [PredType]
dit_rep_tc_args = [PredType]
rep_tc_args })
  = do DerivEnv { denv_tvs      = tvs
                , denv_cls      = main_cls
                , denv_inst_tys = inst_tys } <- ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
       wildcard <- isStandaloneWildcardDeriv

       let inst_ty    = TyCon -> [PredType] -> PredType
mkTyConApp TyCon
tc [PredType]
tc_args
           tc_binders = TyCon -> [TyConBinder]
tyConBinders TyCon
rep_tc
           choose_level TyConBinder
bndr
             | TyConBinder -> Bool
isNamedTyConBinder TyConBinder
bndr = TypeOrKind
KindLevel
             | Bool
otherwise               = TypeOrKind
TypeLevel
           t_or_ks = (TyConBinder -> TypeOrKind) -> [TyConBinder] -> [TypeOrKind]
forall a b. (a -> b) -> [a] -> [b]
map TyConBinder -> TypeOrKind
choose_level [TyConBinder]
tc_binders [TypeOrKind] -> [TypeOrKind] -> [TypeOrKind]
forall a. [a] -> [a] -> [a]
++ TypeOrKind -> [TypeOrKind]
forall a. a -> [a]
repeat TypeOrKind
TypeLevel
              -- want to report *kind* errors when possible

              -- Constraints arising from the arguments of each constructor
           con_arg_constraints
             :: ([TyVar] -> CtOrigin
                         -> TypeOrKind
                         -> Type
                         -> [(ThetaSpec, Maybe Subst)])
             -> (ThetaSpec, [TyVar], [TcType], DerivInstTys)
           con_arg_constraints [TyVar]
-> CtOrigin -> TypeOrKind -> PredType -> [(ThetaSpec, Maybe Subst)]
get_arg_constraints
             = let -- Constraints from the fields of each data constructor.
                   ([ThetaSpec]
predss, [Maybe Subst]
mbSubsts) = [(ThetaSpec, Maybe Subst)] -> ([ThetaSpec], [Maybe Subst])
forall a b. [(a, b)] -> ([a], [b])
unzip
                     [ (ThetaSpec, Maybe Subst)
preds_and_mbSubst
                     | DataCon
data_con <- TyCon -> [DataCon]
tyConDataCons TyCon
rep_tc
                     , (Int
arg_n, TypeOrKind
arg_t_or_k, PredType
arg_ty)
                         <- [Int]
-> [TypeOrKind] -> [PredType] -> [(Int, TypeOrKind, PredType)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Int
1..] [TypeOrKind]
t_or_ks ([PredType] -> [(Int, TypeOrKind, PredType)])
-> [PredType] -> [(Int, TypeOrKind, PredType)]
forall a b. (a -> b) -> a -> b
$
                            DataCon -> DerivInstTys -> [PredType]
derivDataConInstArgTys DataCon
data_con DerivInstTys
dit
                       -- No constraints for unlifted types
                       -- See Note [Deriving and unboxed types]
                     , Bool -> Bool
not (HasDebugCallStack => PredType -> Bool
PredType -> Bool
isUnliftedType PredType
arg_ty)
                     , let orig :: CtOrigin
orig = DataCon -> Int -> Bool -> CtOrigin
DerivOriginDC DataCon
data_con Int
arg_n Bool
wildcard
                     , (ThetaSpec, Maybe Subst)
preds_and_mbSubst
                         <- [TyVar]
-> CtOrigin -> TypeOrKind -> PredType -> [(ThetaSpec, Maybe Subst)]
get_arg_constraints (DataCon -> [TyVar]
dataConUnivTyVars DataCon
data_con)
                                                CtOrigin
orig TypeOrKind
arg_t_or_k PredType
arg_ty
                     ]
                   -- Stupid constraints from DatatypeContexts. Note that we
                   -- must gather these constraints from the data constructors,
                   -- not from the parent type constructor, as the latter could
                   -- lead to redundant constraints due to thinning.
                   -- See Note [The stupid context] in GHC.Core.DataCon.
                   stupid_theta :: [PredType]
stupid_theta =
                     [ [TyVar] -> [PredType] -> PredType -> PredType
HasDebugCallStack => [TyVar] -> [PredType] -> PredType -> PredType
substTyWith (DataCon -> [TyVar]
dataConUnivTyVars DataCon
data_con)
                                   (DataCon -> [PredType] -> [PredType]
dataConInstUnivs DataCon
data_con [PredType]
rep_tc_args)
                                   PredType
stupid_pred
                     | DataCon
data_con <- TyCon -> [DataCon]
tyConDataCons TyCon
rep_tc
                     , PredType
stupid_pred <- DataCon -> [PredType]
dataConStupidTheta DataCon
data_con
                     ]

                   preds :: ThetaSpec
preds = [ThetaSpec] -> ThetaSpec
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ThetaSpec]
predss
                   -- If the constraints require a subtype to be of kind
                   -- (* -> *) (which is the case for functor-like
                   -- constraints), then we explicitly unify the subtype's
                   -- kinds with (* -> *).
                   -- See Note [Inferring the instance context]
                   subst :: Subst
subst        = (Subst -> Subst -> Subst) -> Subst -> [Subst] -> Subst
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Subst -> Subst -> Subst
composeTCvSubst
                                         Subst
emptySubst ([Maybe Subst] -> [Subst]
forall a. [Maybe a] -> [a]
catMaybes [Maybe Subst]
mbSubsts)
                   unmapped_tvs :: [TyVar]
unmapped_tvs = (TyVar -> Bool) -> [TyVar] -> [TyVar]
forall a. (a -> Bool) -> [a] -> [a]
filter (\TyVar
v -> TyVar
v TyVar -> Subst -> Bool
`notElemSubst` Subst
subst
                                             Bool -> Bool -> Bool
&& Bool -> Bool
not (TyVar
v TyVar -> Subst -> Bool
`isInScope` Subst
subst)) [TyVar]
tvs
                   (Subst
subst', [TyVar]
_)  = HasDebugCallStack => Subst -> [TyVar] -> (Subst, [TyVar])
Subst -> [TyVar] -> (Subst, [TyVar])
substTyVarBndrs Subst
subst [TyVar]
unmapped_tvs
                   stupid_theta_origin :: ThetaSpec
stupid_theta_origin = CtOrigin -> TypeOrKind -> [PredType] -> ThetaSpec
mkDirectThetaSpec
                                           CtOrigin
deriv_origin TypeOrKind
TypeLevel
                                           (HasDebugCallStack => Subst -> [PredType] -> [PredType]
Subst -> [PredType] -> [PredType]
substTheta Subst
subst' [PredType]
stupid_theta)
                   preds' :: ThetaSpec
preds'       = (PredSpec -> PredSpec) -> ThetaSpec -> ThetaSpec
forall a b. (a -> b) -> [a] -> [b]
map (HasCallStack => Subst -> PredSpec -> PredSpec
Subst -> PredSpec -> PredSpec
substPredSpec Subst
subst') ThetaSpec
preds
                   inst_tys' :: [PredType]
inst_tys'    = HasDebugCallStack => Subst -> [PredType] -> [PredType]
Subst -> [PredType] -> [PredType]
substTys Subst
subst' [PredType]
inst_tys
                   dit' :: DerivInstTys
dit'         = Subst -> DerivInstTys -> DerivInstTys
substDerivInstTys Subst
subst' DerivInstTys
dit
                   tvs' :: [TyVar]
tvs'         = [PredType] -> [TyVar]
tyCoVarsOfTypesWellScoped [PredType]
inst_tys'
               in ( ThetaSpec
stupid_theta_origin ThetaSpec -> ThetaSpec -> ThetaSpec
forall a. [a] -> [a] -> [a]
++ ThetaSpec
preds'
                  , [TyVar]
tvs', [PredType]
inst_tys', DerivInstTys
dit' )

           is_generic  = Class
main_cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
genClassKey
           is_generic1 = Class
main_cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
gen1ClassKey
           -- is_functor_like: see Note [Inferring the instance context]
           is_functor_like = HasDebugCallStack => PredType -> PredType
PredType -> PredType
typeKind PredType
inst_ty HasDebugCallStack => PredType -> PredType -> Bool
PredType -> PredType -> Bool
`tcEqKind` PredType
typeToTypeKind
                          Bool -> Bool -> Bool
|| Bool
is_generic1

           get_gen1_constraints ::
                Class
             -> [TyVar] -- The universally quantified type variables for the
                        -- data constructor
             -> CtOrigin -> TypeOrKind -> Type
             -> [(ThetaSpec, Maybe Subst)]
           get_gen1_constraints Class
functor_cls [TyVar]
dc_univs CtOrigin
orig TypeOrKind
t_or_k PredType
ty
              = CtOrigin
-> TypeOrKind -> Class -> [PredType] -> [(ThetaSpec, Maybe Subst)]
mk_functor_like_constraints CtOrigin
orig TypeOrKind
t_or_k Class
functor_cls ([PredType] -> [(ThetaSpec, Maybe Subst)])
-> [PredType] -> [(ThetaSpec, Maybe Subst)]
forall a b. (a -> b) -> a -> b
$
                TyVar -> PredType -> [PredType]
get_gen1_constrained_tys TyVar
last_dc_univ PredType
ty
             where
               -- If we are deriving an instance of 'Generic1' and have made
               -- it this far, then there should be at least one universal type
               -- variable, making this use of 'last' safe.
               last_dc_univ :: TyVar
last_dc_univ = Bool -> TyVar -> TyVar
forall a. HasCallStack => Bool -> a -> a
assert (Bool -> Bool
not ([TyVar] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TyVar]
dc_univs)) (TyVar -> TyVar) -> TyVar -> TyVar
forall a b. (a -> b) -> a -> b
$
                              [TyVar] -> TyVar
forall a. HasCallStack => [a] -> a
last [TyVar]
dc_univs

           get_std_constrained_tys ::
                [TyVar] -- The universally quantified type variables for the
                        -- data constructor
             -> CtOrigin -> TypeOrKind -> Type
             -> [(ThetaSpec, Maybe Subst)]
           get_std_constrained_tys [TyVar]
dc_univs CtOrigin
orig TypeOrKind
t_or_k PredType
ty
               | Bool
is_functor_like
               = CtOrigin
-> TypeOrKind -> Class -> [PredType] -> [(ThetaSpec, Maybe Subst)]
mk_functor_like_constraints CtOrigin
orig TypeOrKind
t_or_k Class
main_cls ([PredType] -> [(ThetaSpec, Maybe Subst)])
-> [PredType] -> [(ThetaSpec, Maybe Subst)]
forall a b. (a -> b) -> a -> b
$
                 TyVar -> PredType -> [PredType]
deepSubtypesContaining TyVar
last_dc_univ PredType
ty
               | Bool
otherwise
               = [( [CtOrigin -> TypeOrKind -> Class -> PredType -> PredSpec
mk_cls_pred CtOrigin
orig TypeOrKind
t_or_k Class
main_cls PredType
ty]
                  , Maybe Subst
forall a. Maybe a
Nothing )]
             where
               -- If 'is_functor_like' holds, then there should be at least one
               -- universal type variable, making this use of 'last' safe.
               last_dc_univ :: TyVar
last_dc_univ = Bool -> TyVar -> TyVar
forall a. HasCallStack => Bool -> a -> a
assert (Bool -> Bool
not ([TyVar] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TyVar]
dc_univs)) (TyVar -> TyVar) -> TyVar -> TyVar
forall a b. (a -> b) -> a -> b
$
                              [TyVar] -> TyVar
forall a. HasCallStack => [a] -> a
last [TyVar]
dc_univs

           mk_functor_like_constraints :: CtOrigin -> TypeOrKind
                                       -> Class -> [Type]
                                       -> [(ThetaSpec, Maybe Subst)]
           -- 'cls' is usually main_cls (Functor or Traversable etc), but if
           -- main_cls = Generic1, then 'cls' can be Functor; see
           -- get_gen1_constraints
           --
           -- For each type, generate two constraints,
           -- [cls ty, kind(ty) ~ (*->*)], and a kind substitution that results
           -- from unifying  kind(ty) with * -> *. If the unification is
           -- successful, it will ensure that the resulting instance is well
           -- kinded. If not, the second constraint will result in an error
           -- message which points out the kind mismatch.
           -- See Note [Inferring the instance context]
           mk_functor_like_constraints CtOrigin
orig TypeOrKind
t_or_k Class
cls
              = (PredType -> (ThetaSpec, Maybe Subst))
-> [PredType] -> [(ThetaSpec, Maybe Subst)]
forall a b. (a -> b) -> [a] -> [b]
map ((PredType -> (ThetaSpec, Maybe Subst))
 -> [PredType] -> [(ThetaSpec, Maybe Subst)])
-> (PredType -> (ThetaSpec, Maybe Subst))
-> [PredType]
-> [(ThetaSpec, Maybe Subst)]
forall a b. (a -> b) -> a -> b
$ \PredType
ty -> let ki :: PredType
ki = HasDebugCallStack => PredType -> PredType
PredType -> PredType
typeKind PredType
ty in
                             ( [ CtOrigin -> TypeOrKind -> Class -> PredType -> PredSpec
mk_cls_pred CtOrigin
orig TypeOrKind
t_or_k Class
cls PredType
ty
                               , SimplePredSpec
                                   { sps_pred :: PredType
sps_pred = PredType -> PredType -> PredType
mkPrimEqPred PredType
ki PredType
typeToTypeKind
                                   , sps_origin :: CtOrigin
sps_origin = CtOrigin
orig
                                   , sps_type_or_kind :: TypeOrKind
sps_type_or_kind = TypeOrKind
KindLevel
                                   }
                               ]
                             , PredType -> PredType -> Maybe Subst
tcUnifyTy PredType
ki PredType
typeToTypeKind
                             )

           -- Extra Data constraints
           -- The Data class (only) requires that for
           --    instance (...) => Data (T t1 t2)
           -- IF   t1:*, t2:*
           -- THEN (Data t1, Data t2) are among the (...) constraints
           -- Reason: when the IF holds, we generate a method
           --             dataCast2 f = gcast2 f
           --         and we need the Data constraints to typecheck the method
           extra_constraints
                 | Class
main_cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
dataClassKey
                 , (PredType -> Bool) -> [PredType] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (PredType -> Bool
isLiftedTypeKind (PredType -> Bool) -> (PredType -> PredType) -> PredType -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => PredType -> PredType
PredType -> PredType
typeKind) [PredType]
rep_tc_args
                 = [ CtOrigin -> TypeOrKind -> Class -> PredType -> PredSpec
mk_cls_pred CtOrigin
deriv_origin TypeOrKind
t_or_k Class
main_cls PredType
ty
                   | (TypeOrKind
t_or_k, PredType
ty) <- [TypeOrKind] -> [PredType] -> [(TypeOrKind, PredType)]
forall a b. [a] -> [b] -> [(a, b)]
zip [TypeOrKind]
t_or_ks [PredType]
rep_tc_args]
                 | Bool
otherwise
                 = []

           mk_cls_pred CtOrigin
orig TypeOrKind
t_or_k Class
cls PredType
ty
                -- Don't forget to apply to cls_tys' too
              = SimplePredSpec
                  { sps_pred :: PredType
sps_pred = Class -> [PredType] -> PredType
mkClassPred Class
cls ([PredType]
cls_tys' [PredType] -> [PredType] -> [PredType]
forall a. [a] -> [a] -> [a]
++ [PredType
ty])
                  , sps_origin :: CtOrigin
sps_origin = CtOrigin
orig
                  , sps_type_or_kind :: TypeOrKind
sps_type_or_kind = TypeOrKind
t_or_k
                  }
           cls_tys' | Bool
is_generic1 = []
                      -- In the awkward Generic1 case, cls_tys' should be
                      -- empty, since we are applying the class Functor.

                    | Bool
otherwise   = [PredType]
cls_tys

           deriv_origin = Bool -> CtOrigin
mkDerivOrigin Bool
wildcard

       if    -- Generic constraints are easy
          |  is_generic
           -> return ([], tvs, inst_tys, dit)

             -- Generic1 needs Functor
             -- See Note [Getting base classes]
          |  is_generic1
           -> assert (tyConTyVars rep_tc `lengthExceeds` 0) $
              -- Generic1 has a single kind variable
              assert (cls_tys `lengthIs` 1) $
              do { functorClass <- lift $ tcLookupClass functorClassName
                 ; pure $ con_arg_constraints
                        $ get_gen1_constraints functorClass }

             -- The others are a bit more complicated
          |  otherwise
           -> do { let (arg_constraints, tvs', inst_tys', dit')
                         = con_arg_constraints get_std_constrained_tys
                 ; lift $ traceTc "inferConstraintsStock" $ vcat
                        [ ppr main_cls <+> ppr inst_tys'
                        , ppr arg_constraints
                        ]
                 ; return ( extra_constraints ++ arg_constraints
                          , tvs', inst_tys', dit' ) }

-- | Like 'inferConstraints', but used only in the case of @DeriveAnyClass@,
-- which gathers its constraints based on the type signatures of the class's
-- methods instead of the types of the data constructor's field.
--
-- See Note [Gathering and simplifying constraints for DeriveAnyClass]
-- for an explanation of how these constraints are used to determine the
-- derived instance context.
inferConstraintsAnyclass :: DerivM ThetaSpec
inferConstraintsAnyclass :: DerivM ThetaSpec
inferConstraintsAnyclass
  = do { DerivEnv { denv_cls       = cls
                  , denv_inst_tys  = inst_tys } <- ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
       ; let gen_dms = [ (TyVar
sel_id, PredType
dm_ty)
                       | (TyVar
sel_id, Just (Name
_, GenericDM PredType
dm_ty)) <- Class -> [(TyVar, DefMethInfo)]
classOpItems Class
cls ]
       ; wildcard <- isStandaloneWildcardDeriv

       ; let meth_pred :: (Id, Type) -> PredSpec
               -- (Id,Type) are the selector Id and the generic default method type
               -- NB: the latter is /not/ quantified over the class variables
               -- See Note [Gathering and simplifying constraints for DeriveAnyClass]
             meth_pred (TyVar
sel_id, PredType
gen_dm_ty)
               = let ([TyVar]
sel_tvs, PredType
_cls_pred, PredType
meth_ty) = PredType -> ([TyVar], PredType, PredType)
tcSplitMethodTy (TyVar -> PredType
varType TyVar
sel_id)
                     meth_ty' :: PredType
meth_ty'   = [TyVar] -> [PredType] -> PredType -> PredType
HasDebugCallStack => [TyVar] -> [PredType] -> PredType -> PredType
substTyWith [TyVar]
sel_tvs [PredType]
inst_tys PredType
meth_ty
                     gen_dm_ty' :: PredType
gen_dm_ty' = [TyVar] -> [PredType] -> PredType -> PredType
HasDebugCallStack => [TyVar] -> [PredType] -> PredType -> PredType
substTyWith [TyVar]
sel_tvs [PredType]
inst_tys PredType
gen_dm_ty in
                 -- This is the only place where a SubTypePredSpec is
                 -- constructed instead of a SimplePredSpec. See
                 -- Note [Gathering and simplifying constraints for DeriveAnyClass]
                 -- for a more in-depth explanation.
                 SubTypePredSpec { stps_ty_actual :: PredType
stps_ty_actual = PredType
gen_dm_ty'
                                 , stps_ty_expected :: PredType
stps_ty_expected = PredType
meth_ty'
                                 , stps_origin :: CtOrigin
stps_origin = Bool -> CtOrigin
mkDerivOrigin Bool
wildcard
                                 }

       ; pure $ map meth_pred gen_dms }

-- Like 'inferConstraints', but used only for @GeneralizedNewtypeDeriving@ and
-- @DerivingVia@. Since both strategies generate code involving 'coerce', the
-- inferred constraints set up the scaffolding needed to typecheck those uses
-- of 'coerce'. In this example:
--
-- > newtype Age = MkAge Int deriving newtype Num
--
-- We would infer the following constraints ('ThetaSpec'):
--
-- > (Num Int, Coercible Age Int)
inferConstraintsCoerceBased :: [Type] -> Type
                            -> DerivM ThetaSpec
inferConstraintsCoerceBased :: [PredType] -> PredType -> DerivM ThetaSpec
inferConstraintsCoerceBased [PredType]
cls_tys PredType
rep_ty = do
  DerivEnv { denv_tvs      = tvs
           , denv_cls      = cls
           , denv_inst_tys = inst_tys } <- ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
  sa_wildcard <- isStandaloneWildcardDeriv
  let -- The following functions are polymorphic over the representation
      -- type, since we might either give it the underlying type of a
      -- newtype (for GeneralizedNewtypeDeriving) or a @via@ type
      -- (for DerivingVia).
      rep_tys PredType
ty  = [PredType]
cls_tys [PredType] -> [PredType] -> [PredType]
forall a. [a] -> [a] -> [a]
++ [PredType
ty]
      rep_pred PredType
ty = Class -> [PredType] -> PredType
mkClassPred Class
cls (PredType -> [PredType]
rep_tys PredType
ty)
      rep_pred_o PredType
ty = SimplePredSpec { sps_pred :: PredType
sps_pred = PredType -> PredType
rep_pred PredType
ty
                                     , sps_origin :: CtOrigin
sps_origin = CtOrigin
deriv_origin
                                     , sps_type_or_kind :: TypeOrKind
sps_type_or_kind = TypeOrKind
TypeLevel
                                     }
              -- rep_pred is the representation dictionary, from where
              -- we are going to get all the methods for the final
              -- dictionary
      deriv_origin = Bool -> CtOrigin
mkDerivOrigin Bool
sa_wildcard

      -- Next we collect constraints for the class methods
      -- If there are no methods, we don't need any constraints
      -- Otherwise we need (C rep_ty), for the representation methods,
      -- and constraints to coerce each individual method
      meth_preds :: Type -> ThetaSpec
      meth_preds PredType
ty
        | [TyVar] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TyVar]
meths = [] -- No methods => no constraints
                          -- (#12814)
        | Bool
otherwise = PredType -> PredSpec
rep_pred_o PredType
ty PredSpec -> ThetaSpec -> ThetaSpec
forall a. a -> [a] -> [a]
: PredType -> ThetaSpec
coercible_constraints PredType
ty
      meths = Class -> [TyVar]
classMethods Class
cls
      coercible_constraints PredType
ty
        = [ SimplePredSpec
              { sps_pred :: PredType
sps_pred = PredType -> PredType -> PredType
mkReprPrimEqPred PredType
t1 PredType
t2
              , sps_origin :: CtOrigin
sps_origin = TyVar -> PredType -> PredType -> Bool -> CtOrigin
DerivOriginCoerce TyVar
meth PredType
t1 PredType
t2 Bool
sa_wildcard
              , sps_type_or_kind :: TypeOrKind
sps_type_or_kind = TypeOrKind
TypeLevel
              }
          | TyVar
meth <- [TyVar]
meths
          , let (Pair PredType
t1 PredType
t2) = Class
-> [TyVar] -> [PredType] -> PredType -> TyVar -> Pair PredType
mkCoerceClassMethEqn Class
cls [TyVar]
tvs
                                       [PredType]
inst_tys PredType
ty TyVar
meth ]

  pure (meth_preds rep_ty)

{- Note [Inferring the instance context]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There are two sorts of 'deriving', as represented by the two constructors
for DerivContext:

  * InferContext mb_wildcard: This can either be:
    - The deriving clause for a data type.
        (e.g, data T a = T1 a deriving( Eq ))
      In this case, mb_wildcard = Nothing.
    - A standalone declaration with an extra-constraints wildcard
        (e.g., deriving instance _ => Eq (Foo a))
      In this case, mb_wildcard = Just loc, where loc is the location
      of the extra-constraints wildcard.

    Here we must infer an instance context,
    and generate instance declaration
      instance Eq a => Eq (T a) where ...

  * SupplyContext theta: standalone deriving
      deriving instance Eq a => Eq (T a)
    Here we only need to fill in the bindings;
    the instance context (theta) is user-supplied

For the InferContext case, we must figure out the
instance context (inferConstraintsStock). Suppose we are inferring
the instance context for
    C t1 .. tn (T s1 .. sm)
There are two cases

  * (T s1 .. sm) :: *         (the normal case)
    Then we behave like Eq and guess (C t1 .. tn t)
    for each data constructor arg of type t.  More
    details below.

  * (T s1 .. sm) :: * -> *    (the functor-like case)
    Then we behave like Functor.

In both cases we produce a bunch of un-simplified constraints
and them simplify them in simplifyInstanceContexts; see
Note [Simplifying the instance context].

In the functor-like case, we may need to unify some kind variables with * in
order for the generated instance to be well-kinded. An example from #10524:

  newtype Compose (f :: k2 -> *) (g :: k1 -> k2) (a :: k1)
    = Compose (f (g a)) deriving Functor

Earlier in the deriving pipeline, GHC unifies the kind of Compose f g
(k1 -> *) with the kind of Functor's argument (* -> *), so k1 := *. But this
alone isn't enough, since k2 wasn't unified with *:

  instance (Functor (f :: k2 -> *), Functor (g :: * -> k2)) =>
    Functor (Compose f g) where ...

The two Functor constraints are ill-kinded. To ensure this doesn't happen, we:

  1. Collect all of a datatype's subtypes which require functor-like
     constraints.
  2. For each subtype, create a substitution by unifying the subtype's kind
     with (* -> *).
  3. Compose all the substitutions into one, then apply that substitution to
     all of the in-scope type variables and the instance types.

Note [Getting base classes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Functor and Typeable are defined in package 'base', and that is not available
when compiling 'ghc-prim'.  So we must be careful that 'deriving' for stuff in
ghc-prim does not use Functor or Typeable implicitly via these lookups.

Note [Deriving and unboxed types]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We have some special hacks to support things like
   data T = MkT Int# deriving ( Show )

Specifically, we use GHC.Tc.Deriv.Generate.box to box the Int# into an Int
(which we know how to show), and append a '#'. Parentheses are not required
for unboxed values (`MkT -3#` is a valid expression).

Note [Superclasses of derived instance]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In general, a derived instance decl needs the superclasses of the derived
class too.  So if we have
        data T a = ...deriving( Ord )
then the initial context for Ord (T a) should include Eq (T a).  Often this is
redundant; we'll also generate an Ord constraint for each constructor argument,
and that will probably generate enough constraints to make the Eq (T a) constraint
be satisfied too.  But not always; consider:

 data S a = S
 instance Eq (S a)
 instance Ord (S a)

 data T a = MkT (S a) deriving( Ord )
 instance Num a => Eq (T a)

The derived instance for (Ord (T a)) must have a (Num a) constraint!
Similarly consider:
        data T a = MkT deriving( Data )
Here there *is* no argument field, but we must nevertheless generate
a context for the Data instances:
        instance Typeable a => Data (T a) where ...


************************************************************************
*                                                                      *
         Finding the fixed point of deriving equations
*                                                                      *
************************************************************************

Note [Simplifying the instance context]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider

        data T a b = C1 (Foo a) (Bar b)
                   | C2 Int (T b a)
                   | C3 (T a a)
                   deriving (Eq)

We want to come up with an instance declaration of the form

        instance (Ping a, Pong b, ...) => Eq (T a b) where
                x == y = ...

It is pretty easy, albeit tedious, to fill in the code "...".  The
trick is to figure out what the context for the instance decl is,
namely Ping, Pong and friends.

Let's call the context reqd for the T instance of class C at types
(a,b, ...)  C (T a b).  Thus:

        Eq (T a b) = (Ping a, Pong b, ...)

Now we can get a (recursive) equation from the data decl.  This part
is done by inferConstraintsStock.

        Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
                   u Eq (T b a) u Eq Int        -- From C2
                   u Eq (T a a)                 -- From C3


Foo and Bar may have explicit instances for Eq, in which case we can
just substitute for them.  Alternatively, either or both may have
their Eq instances given by deriving clauses, in which case they
form part of the system of equations.

Now all we need do is simplify and solve the equations, iterating to
find the least fixpoint.  This is done by simplifyInstanceConstraints.
Notice that the order of the arguments can
switch around, as here in the recursive calls to T.

Let's suppose Eq (Foo a) = Eq a, and Eq (Bar b) = Ping b.

We start with:

        Eq (T a b) = {}         -- The empty set

Next iteration:
        Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
                   u Eq (T b a) u Eq Int        -- From C2
                   u Eq (T a a)                 -- From C3

        After simplification:
                   = Eq a u Ping b u {} u {} u {}
                   = Eq a u Ping b

Next iteration:

        Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
                   u Eq (T b a) u Eq Int        -- From C2
                   u Eq (T a a)                 -- From C3

        After simplification:
                   = Eq a u Ping b
                   u (Eq b u Ping a)
                   u (Eq a u Ping a)

                   = Eq a u Ping b u Eq b u Ping a

The next iteration gives the same result, so this is the fixpoint.  We
need to make a canonical form of the RHS to ensure convergence.  We do
this by simplifying the RHS to a form in which

        - the classes constrain only tyvars
        - the list is sorted by tyvar (major key) and then class (minor key)
        - no duplicates, of course

Note [Deterministic simplifyInstanceContexts]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Canonicalisation uses nonDetCmpType which is nondeterministic. Sorting
with nonDetCmpType puts the returned lists in a nondeterministic order.
If we were to return them, we'd get class constraints in
nondeterministic order.

Consider:

  data ADT a b = Z a b deriving Eq

The generated code could be either:

  instance (Eq a, Eq b) => Eq (Z a b) where

Or:

  instance (Eq b, Eq a) => Eq (Z a b) where

To prevent the order from being nondeterministic we only
canonicalize when comparing and return them in the same order as
simplifyDeriv returned them.
See also Note [nonDetCmpType nondeterminism]
-}


simplifyInstanceContexts :: [DerivSpec ThetaSpec]
                         -> TcM [DerivSpec ThetaType]
-- Used only for deriving clauses or standalone deriving with an
-- extra-constraints wildcard (InferContext)
-- See Note [Simplifying the instance context]

simplifyInstanceContexts :: [DerivSpec ThetaSpec] -> TcM [DerivSpec [PredType]]
simplifyInstanceContexts [] = [DerivSpec [PredType]] -> TcM [DerivSpec [PredType]]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return []

simplifyInstanceContexts [DerivSpec ThetaSpec]
infer_specs
  = do  { String -> SDoc -> TcRn ()
traceTc String
"simplifyInstanceContexts" (SDoc -> TcRn ()) -> SDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat ((DerivSpec ThetaSpec -> SDoc) -> [DerivSpec ThetaSpec] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map DerivSpec ThetaSpec -> SDoc
forall theta. Outputable theta => DerivSpec theta -> SDoc
pprDerivSpec [DerivSpec ThetaSpec]
infer_specs)
        ; final_specs <- Int -> [[PredType]] -> TcM [DerivSpec [PredType]]
iterate_deriv Int
1 [[PredType]]
initial_solutions
          -- After simplification finishes, zonk the TcTyVars as described
          -- in Note [Overlap and deriving].
        ; initZonkEnv DefaultFlexi $ traverse zonkDerivSpec final_specs }
  where
    ------------------------------------------------------------------
        -- The initial solutions for the equations claim that each
        -- instance has an empty context; this solution is certainly
        -- in canonical form.
    initial_solutions :: [ThetaType]
    initial_solutions :: [[PredType]]
initial_solutions = [ [] | DerivSpec ThetaSpec
_ <- [DerivSpec ThetaSpec]
infer_specs ]

    ------------------------------------------------------------------
        -- iterate_deriv calculates the next batch of solutions,
        -- compares it with the current one; finishes if they are the
        -- same, otherwise recurses with the new solutions.
        -- It fails if any iteration fails
    iterate_deriv :: Int -> [ThetaType] -> TcM [DerivSpec ThetaType]
    iterate_deriv :: Int -> [[PredType]] -> TcM [DerivSpec [PredType]]
iterate_deriv Int
n [[PredType]]
current_solns
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
20  -- Looks as if we are in an infinite loop
                -- This can happen if we have -XUndecidableInstances
                -- (See GHC.Tc.Solver.tcSimplifyDeriv.)
      = String -> SDoc -> TcM [DerivSpec [PredType]]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"solveDerivEqns: probable loop"
                 ([SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat ((DerivSpec ThetaSpec -> SDoc) -> [DerivSpec ThetaSpec] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map DerivSpec ThetaSpec -> SDoc
forall theta. Outputable theta => DerivSpec theta -> SDoc
pprDerivSpec [DerivSpec ThetaSpec]
infer_specs) SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [[PredType]] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [[PredType]]
current_solns)
      | Bool
otherwise
      = do {      -- Extend the inst info from the explicit instance decls
                  -- with the current set of solutions, and simplify each RHS
             inst_specs <- ([PredType]
 -> DerivSpec ThetaSpec -> IOEnv (Env TcGblEnv TcLclEnv) ClsInst)
-> [[PredType]]
-> [DerivSpec ThetaSpec]
-> IOEnv (Env TcGblEnv TcLclEnv) [ClsInst]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (\[PredType]
soln -> DerivSpec [PredType] -> IOEnv (Env TcGblEnv TcLclEnv) ClsInst
newDerivClsInst (DerivSpec [PredType] -> IOEnv (Env TcGblEnv TcLclEnv) ClsInst)
-> (DerivSpec ThetaSpec -> DerivSpec [PredType])
-> DerivSpec ThetaSpec
-> IOEnv (Env TcGblEnv TcLclEnv) ClsInst
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PredType] -> DerivSpec ThetaSpec -> DerivSpec [PredType]
forall theta' theta. theta' -> DerivSpec theta -> DerivSpec theta'
setDerivSpecTheta [PredType]
soln)
                                    [[PredType]]
current_solns [DerivSpec ThetaSpec]
infer_specs
           ; new_solns <- checkNoErrs $
                          extendLocalInstEnv inst_specs $
                          mapM simplifyDeriv infer_specs

           ; if (current_solns `eqSolution` new_solns) then
                return [ setDerivSpecTheta soln spec
                       | (spec, soln) <- zip infer_specs current_solns ]
             else
                iterate_deriv (n+1) new_solns }

    eqSolution :: [[PredType]] -> [[PredType]] -> Bool
eqSolution = (([PredType] -> [PredType] -> Bool)
-> [[PredType]] -> [[PredType]] -> Bool
forall a b. (a -> b -> Bool) -> [a] -> [b] -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq (([PredType] -> [PredType] -> Bool)
 -> [[PredType]] -> [[PredType]] -> Bool)
-> ((PredType -> PredType -> Bool)
    -> [PredType] -> [PredType] -> Bool)
-> (PredType -> PredType -> Bool)
-> [[PredType]]
-> [[PredType]]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PredType -> PredType -> Bool) -> [PredType] -> [PredType] -> Bool
forall a b. (a -> b -> Bool) -> [a] -> [b] -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq) PredType -> PredType -> Bool
eqType ([[PredType]] -> [[PredType]] -> Bool)
-> ([[PredType]] -> [[PredType]])
-> [[PredType]]
-> [[PredType]]
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` [[PredType]] -> [[PredType]]
canSolution
       -- Canonicalise for comparison
       -- See Note [Deterministic simplifyInstanceContexts]
    canSolution :: [[PredType]] -> [[PredType]]
canSolution = ([PredType] -> [PredType]) -> [[PredType]] -> [[PredType]]
forall a b. (a -> b) -> [a] -> [b]
map ((PredType -> PredType -> Ordering) -> [PredType] -> [PredType]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy PredType -> PredType -> Ordering
nonDetCmpType)

derivInstCtxt :: PredType -> SDoc
derivInstCtxt :: PredType -> SDoc
derivInstCtxt PredType
pred
  = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"When deriving the instance for" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
parens (PredType -> SDoc
forall a. Outputable a => a -> SDoc
ppr PredType
pred)

{-
***********************************************************************************
*                                                                                 *
*            Simplify derived constraints
*                                                                                 *
***********************************************************************************
-}


-- | Given @instance (wanted) => C inst_ty@, simplify 'wanted' as much
-- as possible. Fail if not possible.
simplifyDeriv :: DerivSpec ThetaSpec
              -> TcM ThetaType -- ^ Needed constraints (after simplification),
                               -- i.e. @['PredType']@.
simplifyDeriv :: DerivSpec ThetaSpec -> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
simplifyDeriv (DS { ds_loc :: forall theta. DerivSpec theta -> SrcSpan
ds_loc = SrcSpan
loc, ds_tvs :: forall theta. DerivSpec theta -> [TyVar]
ds_tvs = [TyVar]
tvs
                  , ds_cls :: forall theta. DerivSpec theta -> Class
ds_cls = Class
clas, ds_tys :: forall theta. DerivSpec theta -> [PredType]
ds_tys = [PredType]
inst_tys, ds_theta :: forall theta. DerivSpec theta -> theta
ds_theta = ThetaSpec
deriv_rhs
                  , ds_skol_info :: forall theta. DerivSpec theta -> SkolemInfo
ds_skol_info = SkolemInfo
skol_info, ds_user_ctxt :: forall theta. DerivSpec theta -> UserTypeCtxt
ds_user_ctxt = UserTypeCtxt
user_ctxt })
  = SrcSpan
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc  (IOEnv (Env TcGblEnv TcLclEnv) [PredType]
 -> IOEnv (Env TcGblEnv TcLclEnv) [PredType])
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
forall a b. (a -> b) -> a -> b
$
    SDoc
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (PredType -> SDoc
derivInstCtxt (Class -> [PredType] -> PredType
mkClassPred Class
clas [PredType]
inst_tys)) (IOEnv (Env TcGblEnv TcLclEnv) [PredType]
 -> IOEnv (Env TcGblEnv TcLclEnv) [PredType])
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
-> IOEnv (Env TcGblEnv TcLclEnv) [PredType]
forall a b. (a -> b) -> a -> b
$
    do {
       -- See [STEP DAC BUILD]
       -- Generate the implication constraints, one for each method, to solve
       -- with the skolemized variables.  Start "one level down" because
       -- we are going to wrap the result in an implication with tvs,
       -- in step [DAC RESIDUAL]
       ; (tc_lvl, wanteds) <- UserTypeCtxt -> ThetaSpec -> TcM (TcLevel, WantedConstraints)
captureThetaSpecConstraints UserTypeCtxt
user_ctxt ThetaSpec
deriv_rhs

       ; traceTc "simplifyDeriv inputs" $
         vcat [ pprTyVars tvs $$ ppr deriv_rhs $$ ppr wanteds, ppr skol_info ]

       -- See [STEP DAC SOLVE]
       -- Simplify the constraints, starting at the same level at which
       -- they are generated (c.f. the call to runTcSWithEvBinds in
       -- simplifyInfer)
       ; (solved_wanteds, _) <- setTcLevel tc_lvl $
                                runTcS            $
                                solveWanteds wanteds

       -- It's not yet zonked!  Obviously zonk it before peering at it
       ; solved_wanteds <- liftZonkM $ zonkWC solved_wanteds

       -- See [STEP DAC HOIST]
       -- From the simplified constraints extract a subset 'good' that will
       -- become the context 'min_theta' for the derived instance.
       ; let residual_simple = Bool -> WantedConstraints -> Cts
approximateWC Bool
True WantedConstraints
solved_wanteds
             head_size       = Class -> [PredType] -> PatersonSize
pSizeClassPred Class
clas [PredType]
inst_tys
             good = (Ct -> Maybe PredType) -> Cts -> Bag PredType
forall a b. (a -> Maybe b) -> Bag a -> Bag b
mapMaybeBag Ct -> Maybe PredType
get_good Cts
residual_simple

             -- Returns @Just p@ (where @p@ is the type of the Ct) if a Ct is
             -- suitable to be inferred in the context of a derived instance.
             -- Returns @Nothing@ if the Ct is too exotic.
             -- See (VD2) in Note [Valid 'deriving' predicate] in
             -- GHC.Tc.Validity for what constitutes an exotic constraint.
             get_good :: Ct -> Maybe PredType
             get_good Ct
ct | PatersonSize -> PredType -> Bool
validDerivPred PatersonSize
head_size PredType
p = PredType -> Maybe PredType
forall a. a -> Maybe a
Just PredType
p
                         | Bool
otherwise                  = Maybe PredType
forall a. Maybe a
Nothing
               where p :: PredType
p = Ct -> PredType
ctPred Ct
ct

       ; traceTc "simplifyDeriv outputs" $
         vcat [ ppr tvs, ppr residual_simple, ppr good ]

       -- Return the good unsolved constraints (unskolemizing on the way out.)
       ; let min_theta = (PredType -> PredType) -> [PredType] -> [PredType]
forall a. (a -> PredType) -> [a] -> [a]
mkMinimalBySCs PredType -> PredType
forall a. a -> a
id (Bag PredType -> [PredType]
forall a. Bag a -> [a]
bagToList Bag PredType
good)
             -- An important property of mkMinimalBySCs (used above) is that in
             -- addition to removing constraints that are made redundant by
             -- superclass relationships, it also removes _duplicate_
             -- constraints.
             -- See Note [Gathering and simplifying constraints for
             --           DeriveAnyClass]

       -- See [STEP DAC RESIDUAL]
       -- Ensure that min_theta is enough to solve /all/ the constraints in
       -- solved_wanteds, by solving the implication constraint
       --
       --    forall tvs. min_theta => solved_wanteds
       ; min_theta_vars <- mapM newEvVar min_theta
       ; (leftover_implic, _)
           <- buildImplicationFor tc_lvl (getSkolemInfo skol_info) tvs
                                  min_theta_vars solved_wanteds
       -- This call to simplifyTop is purely for error reporting
       -- See Note [Error reporting for deriving clauses]
       -- See also Note [Valid 'deriving' predicate] in GHC.Tc.Validity, as this
       -- line of code catches "exotic" constraints like the ones described in
       -- (VD2) of that Note.
       ; simplifyTopImplic leftover_implic

       ; traceTc "GHC.Tc.Deriv" (ppr deriv_rhs $$ ppr min_theta)

         -- Claim: the result instance declaration is guaranteed valid
         -- Hence no need to call:
         --     checkValidInstance tyvars theta clas inst_tys
         -- See Note [Valid 'deriving' predicate] in GHC.Tc.Validity

       ; return min_theta }

{-
Note [Overlap and deriving]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider some overlapping instances:
  instance Show a => Show [a] where ..
  instance Show [Char] where ...

Now a data type with deriving:
  data T a = MkT [a] deriving( Show )

We want to get the derived instance
  instance Show [a] => Show (T a) where...
and NOT
  instance Show a => Show (T a) where...
so that the (Show (T Char)) instance does the Right Thing

It's very like the situation when we're inferring the type
of a function
   f x = show [x]
and we want to infer
   f :: Show [a] => a -> String

As a result, we use vanilla, non-overlappable skolems when inferring the
context for the derived instances. Hence, we instantiate the type variables
using tcInstSkolTyVars, not tcInstSuperSkolTyVars.

We do this skolemisation in GHC.Tc.Deriv.mkEqnHelp, a function which occurs
very early in the deriving pipeline, so that by the time GHC needs to infer the
instance context, all of the types in the computed DerivSpec have been
skolemised appropriately. After the instance context inference has completed,
GHC zonks the TcTyVars in the DerivSpec to ensure that types like
a[sk:1] do not appear in -ddump-deriv output.

All of this is only needed when inferring an instance context, i.e., the
InferContext case. For the SupplyContext case, we don't bother skolemising
at all.

Note [Gathering and simplifying constraints for DeriveAnyClass]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DeriveAnyClass works quite differently from stock and newtype deriving in
the way it gathers and simplifies constraints to be used in a derived
instance's context. Stock and newtype deriving gather constraints by looking
at the data constructors of the data type for which we are deriving an
instance. But DeriveAnyClass doesn't need to know about a data type's
definition at all!

To see why, consider this example of DeriveAnyClass:

  class Foo a where
    bar :: forall b. Ix b => a -> b -> String
    default bar :: (Show a, Ix c) => a -> c -> String
    bar x y = show x ++ show (range (y,y))

    baz :: Eq a => a -> a -> Bool
    default baz :: (Ord a, Show a) => a -> a -> Bool
    baz x y = compare x y == EQ

Because 'bar' and 'baz' have default signatures, this generates a top-level
definition for these generic default methods

  $gdm_bar :: forall a. Foo a
           => forall c. (Show a, Ix c)
           => a -> c -> String
  $gdm_bar x y = show x ++ show (range (y,y))

(and similarly for baz).  Now consider a 'deriving' clause
  data Maybe s = ... deriving anyclass Foo

This derives an instance of the form:
  instance (CX) => Foo (Maybe s) where
    bar = $gdm_bar
    baz = $gdm_baz

Now it is GHC's job to fill in a suitable instance context (CX).  If
GHC were typechecking the binding
   bar = $gdm_bar
it would
   * skolemise the expected type of bar
   * instantiate the type of $gdm_bar with meta-type variables
   * build an implication constraint

[STEP DAC BUILD]
So that's what we do. Fortunately, there is already functionality within GHC
to that does all of the above—namely, tcSubTypeSigma. In the example above,
we want to use tcSubTypeSigma to check the following subtyping relation:

     forall c. (Show a, Ix c) => Maybe s -> c -> String -- actual type
  <= forall b.         (Ix b) => Maybe s -> b -> String -- expected type

That is, we check that the type of $gdm_bar (the actual type) is more
polymorphic than the type of bar (the expected type). We use SubTypePredSpec,
a special form of PredSpec that is only used by DeriveAnyClass, to store
the actual and expected types.

(Aside: having a separate SubTypePredSpec is not strictly necessary, as we
could theoretically construct this implication constraint by hand and store it
in a SimplePredSpec. In fact, GHC used to do this. However, this is easier
said than done, and there were numerous subtle bugs that resulted from getting
this step wrong, such as #20719. Ultimately, we decided that special-casing a
PredSpec specifically for DeriveAnyClass was worth it.)

tcSubTypeSigma will ultimately spit out an implication constraint, which will
look something like this (call it C1):

   forall[2] b. Ix b => (Show (Maybe s), Ix cc,
                        Maybe s -> b -> String
                            ~ Maybe s -> cc -> String)

Here:
* The level of this forall constraint is forall[2], because we are later
  going to wrap it in a forall[1] in [STEP DAC RESIDUAL]

* The 'b' comes from the quantified type variable in the expected type
  of bar. The 'cc' is a unification variable that comes from instantiating the
  quantified type variable 'c' in $gdm_bar's type. The finer details of
  skolemisation and metavariable instantiation are handled behind the scenes
  by tcSubTypeSigma.

* It is important that `b` be distinct from `cc`. In this example, this is
  clearly the case, but it is not always so obvious when the type variables are
  hidden behind type synonyms. Suppose the example were written like this,
  for example:

    type Method a = forall b. Ix b => a -> b -> String
    class Foo a where
      bar :: Method a
      default bar :: Show a => Method a
      bar = ...

  Both method signatures quantify a `b` once the `Method` type synonym is
  expanded. To ensure that GHC doesn't confuse the two `b`s during
  typechecking, tcSubTypeSigma instantiates the `b` in the original signature
  with a fresh skolem and the `b` in the default signature with a fresh
  unification variable. Doing so prevents #20719 from happening.

* The (Ix b) constraint comes from the context of bar's type. The
  (Show (Maybe s)) and (Ix cc) constraints come from the context of $gdm_bar's
  type.

* The equality constraint (Maybe s -> b -> String) ~ (Maybe s -> cc -> String)
  comes from marrying up the instantiated type of $gdm_bar with the specified
  type of bar. Notice that the type variables from the instance, 's' in this
  case, are global to this constraint.

Note that it is vital that we instantiate the `c` in $gdm_bar's type with a new
unification variable for each iteration of simplifyDeriv. If we re-use the same
unification variable across multiple iterations, then bad things can happen,
such as #14933.

Similarly for 'baz', tcSubTypeSigma gives the constraint C2

   forall[2]. Eq (Maybe s) => (Ord a, Show a,
                              Maybe s -> Maybe s -> Bool
                                ~ Maybe s -> Maybe s -> Bool)

In this case baz has no local quantification, so the implication
constraint has no local skolems and there are no unification
variables.

[STEP DAC SOLVE]
We can combine these two implication constraints into a single
constraint (C1, C2), and simplify, unifying cc:=b, to get:

   forall[2] b. Ix b => Show a
   /\
   forall[2]. Eq (Maybe s) => (Ord a, Show a)

[STEP DAC HOIST]
Let's call that (C1', C2').  Now we need to hoist the unsolved
constraints out of the implications to become our candidate for
(CX). That is done by approximateWC, which will return:

  (Show a, Ord a, Show a)

Now we can use mkMinimalBySCs to remove superclasses and duplicates, giving

  (Show a, Ord a)

And that's what GHC uses for CX.

[STEP DAC RESIDUAL]
In this case we have solved all the leftover constraints, but what if
we don't?  Simple!  We just form the final residual constraint

   forall[1] s. CX => (C1',C2')

and simplify that. In simple cases it'll succeed easily, because CX
literally contains the constraints in C1', C2', but if there is anything
more complicated it will be reported in a civilised way.

Note [Error reporting for deriving clauses]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A surprisingly tricky aspect of deriving to get right is reporting sensible
error messages. In particular, if simplifyDeriv reaches a constraint that it
cannot solve, which might include:

1. Insoluble constraints
2. "Exotic" constraints (See Note [Valid 'deriving' predicate] in
   GHC.Tc.Validity)

Then we report an error immediately in simplifyDeriv.

Another possible choice is to punt and let another part of the typechecker
(e.g., simplifyInstanceContexts) catch the errors. But this tends to lead
to worse error messages, so we do it directly in simplifyDeriv.

simplifyDeriv checks for errors in a clever way. If the deriving machinery
infers the context (Foo a)--that is, if this instance is to be generated:

  instance Foo a => ...

Then we form an implication of the form:

  forall a. Foo a => <residual_wanted_constraints>

And pass it to the simplifier. If the context (Foo a) is enough to discharge
all the constraints in <residual_wanted_constraints>, then everything is
hunky-dory. But if <residual_wanted_constraints> contains, say, an insoluble
constraint, then (Foo a) won't be able to solve it, causing GHC to error.
-}