2 % (c) The University of Glasgow 2006
8 -- the "tc" prefix indicates that matching always
9 -- respects newtypes (rather than looking through them)
10 tcMatchTys, tcMatchTyX, ruleMatchTyX, tcMatchPreds, MatchEnv(..)
13 #include "HsVersions.h"
25 %************************************************************************
29 %************************************************************************
32 Matching is much tricker than you might think.
34 1. The substitution we generate binds the *template type variables*
35 which are given to us explicitly.
37 2. We want to match in the presence of foralls;
38 e.g (forall a. t1) ~ (forall b. t2)
40 That is what the RnEnv2 is for; it does the alpha-renaming
41 that makes it as if a and b were the same variable.
42 Initialising the RnEnv2, so that it can generate a fresh
43 binder when necessary, entails knowing the free variables of
46 3. We must be careful not to bind a template type variable to a
47 locally bound variable. E.g.
48 (forall a. x) ~ (forall b. b)
49 where x is the template type variable. Then we do not want to
50 bind x to a/b! This is a kind of occurs check.
51 The necessary locals accumulate in the RnEnv2.
56 = ME { me_tmpls :: VarSet -- Template tyvars
57 , me_env :: RnEnv2 -- Renaming envt for nested foralls
58 } -- In-scope set includes template tyvars
60 tcMatchTys :: TyVarSet -- Template tyvars
63 -> Maybe TvSubst -- One-shot; in principle the template
64 -- variables could be free in the target
66 tcMatchTys tmpls tys1 tys2
67 = case match_tys menv emptyTvSubstEnv tys1 tys2 of
68 Just subst_env -> Just (TvSubst in_scope subst_env)
71 menv = ME { me_tmpls = tmpls, me_env = mkRnEnv2 in_scope }
72 in_scope = mkInScopeSet (tmpls `unionVarSet` tyVarsOfTypes tys2)
73 -- We're assuming that all the interesting
74 -- tyvars in tys1 are in tmpls
76 -- This is similar, but extends a substitution
77 tcMatchTyX :: TyVarSet -- Template tyvars
78 -> TvSubst -- Substitution to extend
82 tcMatchTyX tmpls (TvSubst in_scope subst_env) ty1 ty2
83 = case match menv subst_env ty1 ty2 of
84 Just subst_env -> Just (TvSubst in_scope subst_env)
87 menv = ME {me_tmpls = tmpls, me_env = mkRnEnv2 in_scope}
90 :: [TyVar] -- Bind these
91 -> [PredType] -> [PredType]
93 tcMatchPreds tmpls ps1 ps2
94 = match_list (match_pred menv) emptyTvSubstEnv ps1 ps2
96 menv = ME { me_tmpls = mkVarSet tmpls, me_env = mkRnEnv2 in_scope_tyvars }
97 in_scope_tyvars = mkInScopeSet (tyVarsOfTheta ps1 `unionVarSet` tyVarsOfTheta ps2)
99 -- This one is called from the expression matcher, which already has a MatchEnv in hand
100 ruleMatchTyX :: MatchEnv
101 -> TvSubstEnv -- Substitution to extend
106 ruleMatchTyX menv subst ty1 ty2 = match menv subst ty1 ty2 -- Rename for export
109 Now the internals of matching
112 match :: MatchEnv -- For the most part this is pushed downwards
113 -> TvSubstEnv -- Substitution so far:
114 -- Domain is subset of template tyvars
115 -- Free vars of range is subset of
116 -- in-scope set of the RnEnv2
117 -> Type -> Type -- Template and target respectively
119 -- This matcher works on source types; that is,
120 -- it respects NewTypes and PredType
122 match menv subst ty1 ty2 | Just ty1' <- tcView ty1 = match menv subst ty1' ty2
123 match menv subst ty1 ty2 | Just ty2' <- tcView ty2 = match menv subst ty1 ty2'
125 match menv subst (TyVarTy tv1) ty2
126 | tv1 `elemVarSet` me_tmpls menv
127 = case lookupVarEnv subst tv1' of
128 Nothing | any (inRnEnvR rn_env) (varSetElems (tyVarsOfType ty2))
129 -> Nothing -- Occurs check
130 | not (typeKind ty2 `isSubKind` tyVarKind tv1)
131 -> Nothing -- Kind mis-match
133 -> Just (extendVarEnv subst tv1 ty2)
135 Just ty1' | tcEqTypeX (nukeRnEnvL rn_env) ty1' ty2
136 -- ty1 has no locally-bound variables, hence nukeRnEnvL
137 -- Note tcEqType...we are doing source-type matching here
142 | otherwise -- tv1 is not a template tyvar
144 TyVarTy tv2 | tv1' == rnOccR rn_env tv2 -> Just subst
148 tv1' = rnOccL rn_env tv1
150 match menv subst (ForAllTy tv1 ty1) (ForAllTy tv2 ty2)
151 = match menv' subst ty1 ty2
152 where -- Use the magic of rnBndr2 to go under the binders
153 menv' = menv { me_env = rnBndr2 (me_env menv) tv1 tv2 }
155 match menv subst (PredTy p1) (PredTy p2)
156 = match_pred menv subst p1 p2
157 match menv subst (TyConApp tc1 tys1) (TyConApp tc2 tys2)
158 | tc1 == tc2 = match_tys menv subst tys1 tys2
159 match menv subst (FunTy ty1a ty1b) (FunTy ty2a ty2b)
160 = do { subst' <- match menv subst ty1a ty2a
161 ; match menv subst' ty1b ty2b }
162 match menv subst (AppTy ty1a ty1b) ty2
163 | Just (ty2a, ty2b) <- repSplitAppTy_maybe ty2
164 = do { subst' <- match menv subst ty1a ty2a
165 ; match menv subst' ty1b ty2b }
167 match menv subst ty1 ty2
171 match_tys menv subst tys1 tys2 = match_list (match menv) subst tys1 tys2
174 match_list :: (TvSubstEnv -> a -> a -> Maybe TvSubstEnv)
175 -> TvSubstEnv -> [a] -> [a] -> Maybe TvSubstEnv
176 match_list fn subst [] [] = Just subst
177 match_list fn subst (ty1:tys1) (ty2:tys2) = do { subst' <- fn subst ty1 ty2
178 ; match_list fn subst' tys1 tys2 }
179 match_list fn subst tys1 tys2 = Nothing
182 match_pred menv subst (ClassP c1 tys1) (ClassP c2 tys2)
183 | c1 == c2 = match_tys menv subst tys1 tys2
184 match_pred menv subst (IParam n1 t1) (IParam n2 t2)
185 | n1 == n2 = match menv subst t1 t2
186 match_pred menv subst p1 p2 = Nothing