2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 This module contains a unifier and a matcher, both of which
7 use an explicit substitution
10 module Unify ( unifyTysX, unifyTyListsX,
11 match, matchTy, matchTys
14 import TypeRep ( Type(..) ) -- friend
15 import Type ( Kind, funTyCon,
16 typeKind, tyVarsOfType, splitAppTy_maybe
19 import PprType () -- Instances
20 -- This import isn't strictly necessary, but it makes sure that
21 -- PprType is below Unify in the hierarchy, which in turn makes
22 -- fewer modules boot-import PprType
24 import Var ( TyVar, tyVarKind )
26 import VarEnv ( TyVarSubstEnv, emptySubstEnv, lookupSubstEnv, extendSubstEnv,
30 import Outputable( panic )
33 %************************************************************************
35 \subsection{Unification wih a explicit substitution}
37 %************************************************************************
39 Unify types with an explicit substitution and no monad.
43 = (TyVarSet, -- Set of template tyvars
44 TyVarSubstEnv) -- Not necessarily idempotent
46 unifyTysX :: TyVarSet -- Template tyvars
49 -> Maybe TyVarSubstEnv
50 unifyTysX tmpl_tyvars ty1 ty2
51 = uTysX ty1 ty2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
53 unifyTyListsX :: TyVarSet -> [Type] -> [Type]
54 -> Maybe TyVarSubstEnv
55 unifyTyListsX tmpl_tyvars tys1 tys2
56 = uTyListsX tys1 tys2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
61 -> (MySubst -> Maybe result)
65 uTysX (NoteTy _ ty1) ty2 k subst = uTysX ty1 ty2 k subst
66 uTysX ty1 (NoteTy _ ty2) k subst = uTysX ty1 ty2 k subst
68 -- Variables; go for uVar
69 uTysX (TyVarTy tyvar1) (TyVarTy tyvar2) k subst
72 uTysX (TyVarTy tyvar1) ty2 k subst@(tmpls,_)
73 | tyvar1 `elemVarSet` tmpls
74 = uVarX tyvar1 ty2 k subst
75 uTysX ty1 (TyVarTy tyvar2) k subst@(tmpls,_)
76 | tyvar2 `elemVarSet` tmpls
77 = uVarX tyvar2 ty1 k subst
79 -- Functions; just check the two parts
80 uTysX (FunTy fun1 arg1) (FunTy fun2 arg2) k subst
81 = uTysX fun1 fun2 (uTysX arg1 arg2 k) subst
83 -- Type constructors must match
84 uTysX (TyConApp con1 tys1) (TyConApp con2 tys2) k subst
85 | (con1 == con2 && length tys1 == length tys2)
86 = uTyListsX tys1 tys2 k subst
88 -- Applications need a bit of care!
89 -- They can match FunTy and TyConApp, so use splitAppTy_maybe
90 -- NB: we've already dealt with type variables and Notes,
91 -- so if one type is an App the other one jolly well better be too
92 uTysX (AppTy s1 t1) ty2 k subst
93 = case splitAppTy_maybe ty2 of
94 Just (s2, t2) -> uTysX s1 s2 (uTysX t1 t2 k) subst
95 Nothing -> Nothing -- Fail
97 uTysX ty1 (AppTy s2 t2) k subst
98 = case splitAppTy_maybe ty1 of
99 Just (s1, t1) -> uTysX s1 s2 (uTysX t1 t2 k) subst
100 Nothing -> Nothing -- Fail
102 -- Not expecting for-alls in unification
104 uTysX (ForAllTy _ _) ty2 k subst = panic "Unify.uTysX subst:ForAllTy (1st arg)"
105 uTysX ty1 (ForAllTy _ _) k subst = panic "Unify.uTysX subst:ForAllTy (2nd arg)"
108 -- Anything else fails
109 uTysX ty1 ty2 k subst = Nothing
112 uTyListsX [] [] k subst = k subst
113 uTyListsX (ty1:tys1) (ty2:tys2) k subst = uTysX ty1 ty2 (uTyListsX tys1 tys2 k) subst
114 uTyListsX tys1 tys2 k subst = Nothing -- Fail if the lists are different lengths
118 -- Invariant: tv1 is a unifiable variable
119 uVarX tv1 ty2 k subst@(tmpls, env)
120 = case lookupSubstEnv env tv1 of
121 Just (DoneTy ty1) -> -- Already bound
122 uTysX ty1 ty2 k subst
124 Nothing -- Not already bound
125 | typeKind ty2 == tyVarKind tv1
126 && occur_check_ok ty2
127 -> -- No kind mismatch nor occur check
128 k (tmpls, extendSubstEnv env tv1 (DoneTy ty2))
130 | otherwise -> Nothing -- Fail if kind mis-match or occur check
132 occur_check_ok ty = all occur_check_ok_tv (varSetElems (tyVarsOfType ty))
133 occur_check_ok_tv tv | tv1 == tv = False
134 | otherwise = case lookupSubstEnv env tv of
136 Just (DoneTy ty) -> occur_check_ok ty
141 %************************************************************************
143 \subsection{Matching on types}
145 %************************************************************************
147 Matching is a {\em unidirectional} process, matching a type against a
148 template (which is just a type with type variables in it). The
149 matcher assumes that there are no repeated type variables in the
150 template, so that it simply returns a mapping of type variables to
151 types. It also fails on nested foralls.
153 @matchTys@ matches corresponding elements of a list of templates and
157 matchTy :: TyVarSet -- Template tyvars
159 -> Type -- Proposed instance of template
160 -> Maybe TyVarSubstEnv -- Matching substitution
163 matchTys :: TyVarSet -- Template tyvars
164 -> [Type] -- Templates
165 -> [Type] -- Proposed instance of template
166 -> Maybe (TyVarSubstEnv, -- Matching substitution
167 [Type]) -- Left over instance types
169 matchTy tmpls ty1 ty2 = match ty1 ty2 tmpls (\ senv -> Just senv) emptySubstEnv
171 matchTys tmpls tys1 tys2 = match_list tys1 tys2 tmpls
172 (\ (senv,tys) -> Just (senv,tys))
176 @match@ is the main function.
179 match :: Type -> Type -- Current match pair
180 -> TyVarSet -- Template vars
181 -> (TyVarSubstEnv -> Maybe result) -- Continuation
182 -> TyVarSubstEnv -- Current subst
185 -- When matching against a type variable, see if the variable
186 -- has already been bound. If so, check that what it's bound to
187 -- is the same as ty; if not, bind it and carry on.
189 match (TyVarTy v) ty tmpls k senv
190 | v `elemVarSet` tmpls
191 = -- v is a template variable
192 case lookupSubstEnv senv v of
193 Nothing -> k (extendSubstEnv senv v (DoneTy ty))
194 Just (DoneTy ty') | ty' == ty -> k senv -- Succeeds
195 | otherwise -> Nothing -- Fails
198 = -- v is not a template variable; ty had better match
199 -- Can't use (==) because types differ
201 TyVarTy v' | v == v' -> k senv -- Success
202 other -> Nothing -- Failure
204 match (FunTy arg1 res1) (FunTy arg2 res2) tmpls k senv
205 = match arg1 arg2 tmpls (match res1 res2 tmpls k) senv
207 match (AppTy fun1 arg1) ty2 tmpls k senv
208 = case splitAppTy_maybe ty2 of
209 Just (fun2,arg2) -> match fun1 fun2 tmpls (match arg1 arg2 tmpls k) senv
210 Nothing -> Nothing -- Fail
212 match (TyConApp tc1 tys1) (TyConApp tc2 tys2) tmpls k senv
214 = match_list tys1 tys2 tmpls k' senv
216 k' (senv', tys2') | null tys2' = k senv' -- Succeed
217 | otherwise = Nothing -- Fail
219 -- With type synonyms, we have to be careful for the exact
220 -- same reasons as in the unifier. Please see the
221 -- considerable commentary there before changing anything
223 match (NoteTy _ ty1) ty2 tmpls k senv = match ty1 ty2 tmpls k senv
224 match ty1 (NoteTy _ ty2) tmpls k senv = match ty1 ty2 tmpls k senv
227 match _ _ _ _ _ = Nothing
229 match_list [] tys2 tmpls k senv = k (senv, tys2)
230 match_list (ty1:tys1) [] tmpls k senv = Nothing -- Not enough arg tys => failure
231 match_list (ty1:tys1) (ty2:tys2) tmpls k senv = match ty1 ty2 tmpls (match_list tys1 tys2 tmpls k) senv