import PrelInfo ( isStandardClass, isNoDictClass )
import Name ( Name, mkMethodOcc, getOccName, getSrcLoc, isHomePackageName, isInternalName )
import NameSet ( addOneToNameSet )
-import Subst ( substTy, substTyWith, substTheta, mkTyVarSubst )
+import Subst ( substTy, substTyWith, substTheta, mkTopTyVarSubst )
import Literal ( inIntRange )
import Var ( TyVar, tyVarKind )
import VarEnv ( TidyEnv, emptyTidyEnv, lookupSubstEnv, SubstResult(..) )
in
returnM (mkCoercion inst_fn, tau)
-tcInstDataCon :: InstOrigin -> DataCon
+tcInstDataCon :: InstOrigin
+ -> TyVarDetails -- Use this for the existential tyvars
+ -- ExistTv when pattern-matching,
+ -- VanillaTv at a call of the constructor
+ -> DataCon
-> TcM ([TcType], -- Types to instantiate at
[Inst], -- Existential dictionaries to apply to
[TcType], -- Argument types of constructor
TcType, -- Result type
[TyVar]) -- Existential tyvars
-tcInstDataCon orig data_con
+tcInstDataCon orig ex_tv_details data_con
= let
(tvs, stupid_theta, ex_tvs, ex_theta, arg_tys, tycon) = dataConSig data_con
-- We generate constraints for the stupid theta even when
-- pattern matching (as the Report requires)
in
- tcInstTyVars VanillaTv (tvs ++ ex_tvs) `thenM` \ (all_tvs', ty_args', tenv) ->
+ mappM (tcInstTyVar VanillaTv) tvs `thenM` \ tvs' ->
+ mappM (tcInstTyVar ex_tv_details) ex_tvs `thenM` \ ex_tvs' ->
let
+ tv_tys' = mkTyVarTys tvs'
+ ex_tv_tys' = mkTyVarTys ex_tvs'
+ all_tys' = tv_tys' ++ ex_tv_tys'
+
+ tenv = mkTopTyVarSubst (tvs ++ ex_tvs) all_tys'
stupid_theta' = substTheta tenv stupid_theta
ex_theta' = substTheta tenv ex_theta
arg_tys' = map (substTy tenv) arg_tys
-
- n_normal_tvs = length tvs
- ex_tvs' = drop n_normal_tvs all_tvs'
- result_ty = mkTyConApp tycon (take n_normal_tvs ty_args')
+ result_ty' = mkTyConApp tycon tv_tys'
in
newDicts orig stupid_theta' `thenM` \ stupid_dicts ->
newDicts orig ex_theta' `thenM` \ ex_dicts ->
-- we don't otherwise use it at all
extendLIEs stupid_dicts `thenM_`
- returnM (ty_args', ex_dicts, arg_tys', result_ty, ex_tvs')
+ returnM (all_tys', ex_dicts, arg_tys', result_ty', ex_tvs')
newMethodFromName :: InstOrigin -> TcType -> Name -> TcM TcId
newMethodFromName origin ty name
in
mappM mk_ty_arg tyvars `thenM` \ ty_args ->
let
- dfun_rho = substTy (mkTyVarSubst tyvars ty_args) rho
+ dfun_rho = substTy (mkTopTyVarSubst tyvars ty_args) rho
+ -- Since the tyvars are freshly made,
+ -- they cannot possibly be captured by
+ -- any existing for-alls. Hence mkTopTyVarSubst
(theta, _) = tcSplitPhiTy dfun_rho
ty_app = mkHsTyApp (L (instLocSrcSpan loc) (HsVar dfun_id)) ty_args
in
-- the type of dataConWrapId (see note on "stupid context" in DataCon.lhs
-- It's dual to TcPat.tcConstructor
inst_data_con data_con
- = tcInstDataCon orig data_con `thenM` \ (ty_args, ex_dicts, arg_tys, result_ty, _) ->
- extendLIEs ex_dicts `thenM_`
- getSrcSpanM `thenM` \ loc ->
+ = tcInstDataCon orig VanillaTv data_con `thenM` \ (ty_args, ex_dicts, arg_tys, result_ty, _) ->
+ extendLIEs ex_dicts `thenM_`
+ getSrcSpanM `thenM` \ loc ->
returnM (unLoc (mkHsDictApp (mkHsTyApp (L loc (HsVar (dataConWrapId data_con))) ty_args)
(map instToId ex_dicts)),
mkFunTys arg_tys result_ty)
import FieldLabel ( fieldLabelName )
import TcEnv ( tcLookupClass, tcLookupLocatedDataCon, tcLookupId )
import TcMType ( newTyVarTy, arityErr )
-import TcType ( TcType, TcTyVar, TcSigmaType, mkClassPred )
+import TcType ( TcType, TcTyVar, TcSigmaType, TyVarDetails(..), mkClassPred )
import Kind ( argTypeKind, liftedTypeKind )
import TcUnify ( tcSubOff, Expected(..), readExpectedType, zapExpectedType,
unifyTauTy, zapToListTy, zapToPArrTy, zapToTupleTy )
= addErrCtxt (patCtxt pat_in) $
-- Check that it's a constructor, and instantiate it
- tcLookupLocatedDataCon con_name `thenM` \ data_con ->
- tcInstDataCon (PatOrigin pat_in) data_con `thenM` \ (_, ex_dicts1, arg_tys, con_res_ty, ex_tvs) ->
+ tcLookupLocatedDataCon con_name `thenM` \ data_con ->
+ tcInstDataCon (PatOrigin pat_in) ExistTv data_con `thenM` \ (_, ex_dicts1, arg_tys, con_res_ty, ex_tvs) ->
-- Check overall type matches.
-- The pat_ty might be a for-all type, in which
--------------------------------
-- TyVarDetails
- TyVarDetails(..), isUserTyVar, isSkolemTyVar,
+ TyVarDetails(..), isUserTyVar, isSkolemTyVar, isExistentialTyVar,
tyVarBindingInfo,
--------------------------------
| PatSigTv -- Scoped type variable, introduced by a pattern
-- type signature \ x::a -> e
+ | ExistTv -- An existential type variable bound by a pattern for
+ -- a data constructor with an existential type. E.g.
+ -- data T = forall a. Eq a => MkT a
+ -- f (MkT x) = ...
+ -- The pattern MkT x will allocate an existential type
+ -- variable for 'a'. We distinguish these from all others
+ -- on one place, namely InstEnv.lookupInstEnv.
+
| VanillaTv -- Everything else
isUserTyVar :: TcTyVar -> Bool -- Avoid unifying these if possible
isSkolemTyVar :: TcTyVar -> Bool
isSkolemTyVar tv = case tcTyVarDetails tv of
- SigTv -> True
- ClsTv -> True
- InstTv -> True
- oteher -> False
+ SigTv -> True
+ ClsTv -> True
+ InstTv -> True
+ ExistTv -> True
+ other -> False
+
+isExistentialTyVar :: TcTyVar -> Bool
+isExistentialTyVar tv = case tcTyVarDetails tv of
+ ExistTv -> True
+ other -> False
tyVarBindingInfo :: TcTyVar -> SDoc -- Used in checkSigTyVars
tyVarBindingInfo tv
details ClsTv = ptext SLIT("class declaration")
details InstTv = ptext SLIT("instance declaration")
details PatSigTv = ptext SLIT("pattern type signature")
+ details ExistTv = ptext SLIT("existential constructor")
details VanillaTv = ptext SLIT("//vanilla//") -- Ditto
\end{code}