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(..), funTyCon
16 import Type ( 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 )
31 import Util ( snocView )
34 %************************************************************************
36 \subsection{Unification wih a explicit substitution}
38 %************************************************************************
40 Unify types with an explicit substitution and no monad.
44 = (TyVarSet, -- Set of template tyvars
45 TyVarSubstEnv) -- Not necessarily idempotent
47 unifyTysX :: TyVarSet -- Template tyvars
50 -> Maybe TyVarSubstEnv
51 unifyTysX tmpl_tyvars ty1 ty2
52 = uTysX ty1 ty2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
54 unifyTyListsX :: TyVarSet -> [Type] -> [Type]
55 -> Maybe TyVarSubstEnv
56 unifyTyListsX tmpl_tyvars tys1 tys2
57 = uTyListsX tys1 tys2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
62 -> (MySubst -> Maybe result)
66 uTysX (NoteTy _ ty1) ty2 k subst = uTysX ty1 ty2 k subst
67 uTysX ty1 (NoteTy _ ty2) k subst = uTysX ty1 ty2 k subst
69 -- Variables; go for uVar
70 uTysX (TyVarTy tyvar1) (TyVarTy tyvar2) k subst
73 uTysX (TyVarTy tyvar1) ty2 k subst@(tmpls,_)
74 | tyvar1 `elemVarSet` tmpls
75 = uVarX tyvar1 ty2 k subst
76 uTysX ty1 (TyVarTy tyvar2) k subst@(tmpls,_)
77 | tyvar2 `elemVarSet` tmpls
78 = uVarX tyvar2 ty1 k subst
80 -- Functions; just check the two parts
81 uTysX (FunTy fun1 arg1) (FunTy fun2 arg2) k subst
82 = uTysX fun1 fun2 (uTysX arg1 arg2 k) subst
84 -- Type constructors must match
85 uTysX (TyConApp con1 tys1) (TyConApp con2 tys2) k subst
86 | (con1 == con2 && length tys1 == length tys2)
87 = uTyListsX tys1 tys2 k subst
89 -- Applications need a bit of care!
90 -- They can match FunTy and TyConApp, so use splitAppTy_maybe
91 -- NB: we've already dealt with type variables and Notes,
92 -- so if one type is an App the other one jolly well better be too
93 uTysX (AppTy s1 t1) ty2 k subst
94 = case splitAppTy_maybe ty2 of
95 Just (s2, t2) -> uTysX s1 s2 (uTysX t1 t2 k) subst
96 Nothing -> Nothing -- Fail
98 uTysX ty1 (AppTy s2 t2) k subst
99 = case splitAppTy_maybe ty1 of
100 Just (s1, t1) -> uTysX s1 s2 (uTysX t1 t2 k) subst
101 Nothing -> Nothing -- Fail
103 -- Not expecting for-alls in unification
105 uTysX (ForAllTy _ _) ty2 k subst = panic "Unify.uTysX subst:ForAllTy (1st arg)"
106 uTysX ty1 (ForAllTy _ _) k subst = panic "Unify.uTysX subst:ForAllTy (2nd arg)"
109 -- Anything else fails
110 uTysX ty1 ty2 k subst = Nothing
113 uTyListsX [] [] k subst = k subst
114 uTyListsX (ty1:tys1) (ty2:tys2) k subst = uTysX ty1 ty2 (uTyListsX tys1 tys2 k) subst
115 uTyListsX tys1 tys2 k subst = Nothing -- Fail if the lists are different lengths
119 -- Invariant: tv1 is a unifiable variable
120 uVarX tv1 ty2 k subst@(tmpls, env)
121 = case lookupSubstEnv env tv1 of
122 Just (DoneTy ty1) -> -- Already bound
123 uTysX ty1 ty2 k subst
125 Nothing -- Not already bound
126 | typeKind ty2 == tyVarKind tv1
127 && occur_check_ok ty2
128 -> -- No kind mismatch nor occur check
129 k (tmpls, extendSubstEnv env tv1 (DoneTy ty2))
131 | otherwise -> Nothing -- Fail if kind mis-match or occur check
133 occur_check_ok ty = all occur_check_ok_tv (varSetElems (tyVarsOfType ty))
134 occur_check_ok_tv tv | tv1 == tv = False
135 | otherwise = case lookupSubstEnv env tv of
137 Just (DoneTy ty) -> occur_check_ok ty
142 %************************************************************************
144 \subsection{Matching on types}
146 %************************************************************************
148 Matching is a {\em unidirectional} process, matching a type against a
149 template (which is just a type with type variables in it). The
150 matcher assumes that there are no repeated type variables in the
151 template, so that it simply returns a mapping of type variables to
152 types. It also fails on nested foralls.
154 @matchTys@ matches corresponding elements of a list of templates and
158 matchTy :: TyVarSet -- Template tyvars
160 -> Type -- Proposed instance of template
161 -> Maybe TyVarSubstEnv -- Matching substitution
164 matchTys :: TyVarSet -- Template tyvars
165 -> [Type] -- Templates
166 -> [Type] -- Proposed instance of template
167 -> Maybe (TyVarSubstEnv, -- Matching substitution
168 [Type]) -- Left over instance types
170 matchTy tmpls ty1 ty2 = match ty1 ty2 tmpls (\ senv -> Just senv) emptySubstEnv
172 matchTys tmpls tys1 tys2 = match_list tys1 tys2 tmpls
173 (\ (senv,tys) -> Just (senv,tys))
177 @match@ is the main function.
180 match :: Type -> Type -- Current match pair
181 -> TyVarSet -- Template vars
182 -> (TyVarSubstEnv -> Maybe result) -- Continuation
183 -> TyVarSubstEnv -- Current subst
186 -- When matching against a type variable, see if the variable
187 -- has already been bound. If so, check that what it's bound to
188 -- is the same as ty; if not, bind it and carry on.
190 match (TyVarTy v) ty tmpls k senv
191 | v `elemVarSet` tmpls
192 = -- v is a template variable
193 case lookupSubstEnv senv v of
194 Nothing -> k (extendSubstEnv senv v (DoneTy ty))
195 Just (DoneTy ty') | ty' == ty -> k senv -- Succeeds
196 | otherwise -> Nothing -- Fails
199 = -- v is not a template variable; ty had better match
200 -- Can't use (==) because types differ
202 TyVarTy v' | v == v' -> k senv -- Success
203 other -> Nothing -- Failure
205 match (FunTy arg1 res1) (FunTy arg2 res2) tmpls k senv
206 = match arg1 arg2 tmpls (match res1 res2 tmpls k) senv
208 match (AppTy fun1 arg1) ty2 tmpls k senv
209 = case splitAppTy_maybe ty2 of
210 Just (fun2,arg2) -> match fun1 fun2 tmpls (match arg1 arg2 tmpls k) senv
211 Nothing -> Nothing -- Fail
213 match (TyConApp tc1 tys1) (TyConApp tc2 tys2) tmpls k senv
215 = match_list tys1 tys2 tmpls k' senv
217 k' (senv', tys2') | null tys2' = k senv' -- Succeed
218 | otherwise = Nothing -- Fail
220 -- With type synonyms, we have to be careful for the exact
221 -- same reasons as in the unifier. Please see the
222 -- considerable commentary there before changing anything
224 match (NoteTy _ ty1) ty2 tmpls k senv = match ty1 ty2 tmpls k senv
225 match ty1 (NoteTy _ ty2) tmpls k senv = match ty1 ty2 tmpls k senv
228 match _ _ _ _ _ = Nothing
230 match_list [] tys2 tmpls k senv = k (senv, tys2)
231 match_list (ty1:tys1) [] tmpls k senv = Nothing -- Not enough arg tys => failure
232 match_list (ty1:tys1) (ty2:tys2) tmpls k senv = match ty1 ty2 tmpls (match_list tys1 tys2 tmpls k) senv