[project @ 2000-07-14 08:17:36 by simonpj]
[ghc-hetmet.git] / ghc / compiler / types / Unify.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{Unify}
5
6 This module contains a unifier and a matcher, both of which
7 use an explicit substitution
8
9 \begin{code}
10 module Unify ( unifyTysX, unifyTyListsX,
11                match, matchTy, matchTys
12   ) where 
13
14 import TypeRep  ( Type(..) )     -- friend
15 import Type     ( Kind, funTyCon, 
16                   typeKind, tyVarsOfType, splitAppTy_maybe
17                 )
18
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
23
24 import Var      ( TyVar, tyVarKind )
25 import VarSet
26 import VarEnv   ( TyVarSubstEnv, emptySubstEnv, lookupSubstEnv, extendSubstEnv, 
27                   SubstResult(..)
28                 )
29
30 import Outputable( panic )
31 \end{code}
32
33 %************************************************************************
34 %*                                                                      *
35 \subsection{Unification wih a explicit substitution}
36 %*                                                                      *
37 %************************************************************************
38
39 Unify types with an explicit substitution and no monad.
40
41 \begin{code}
42 type MySubst
43    = (TyVarSet,         -- Set of template tyvars
44       TyVarSubstEnv)    -- Not necessarily idempotent
45
46 unifyTysX :: TyVarSet           -- Template tyvars
47           -> Type
48           -> Type
49           -> Maybe TyVarSubstEnv
50 unifyTysX tmpl_tyvars ty1 ty2
51   = uTysX ty1 ty2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
52
53 unifyTyListsX :: TyVarSet -> [Type] -> [Type]
54               -> Maybe TyVarSubstEnv
55 unifyTyListsX tmpl_tyvars tys1 tys2
56   = uTyListsX tys1 tys2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
57
58
59 uTysX :: Type
60       -> Type
61       -> (MySubst -> Maybe result)
62       -> MySubst
63       -> Maybe result
64
65 uTysX (NoteTy _ ty1) ty2 k subst = uTysX ty1 ty2 k subst
66 uTysX ty1 (NoteTy _ ty2) k subst = uTysX ty1 ty2 k subst
67
68         -- Variables; go for uVar
69 uTysX (TyVarTy tyvar1) (TyVarTy tyvar2) k subst 
70   | tyvar1 == tyvar2
71   = 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
78
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
82
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
87
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
96
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
101
102         -- Not expecting for-alls in unification
103 #ifdef DEBUG
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)"
106 #endif
107
108         -- Anything else fails
109 uTysX ty1 ty2 k subst = Nothing
110
111
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
115 \end{code}
116
117 \begin{code}
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
123
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))
129
130                | otherwise -> Nothing   -- Fail if kind mis-match or occur check
131   where
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
135                                          Nothing           -> True
136                                          Just (DoneTy ty)  -> occur_check_ok ty
137 \end{code}
138
139
140
141 %************************************************************************
142 %*                                                                      *
143 \subsection{Matching on types}
144 %*                                                                      *
145 %************************************************************************
146
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.
152
153 @matchTys@ matches corresponding elements of a list of templates and
154 types.
155
156 \begin{code}
157 matchTy :: TyVarSet                     -- Template tyvars
158         -> Type                         -- Template
159         -> Type                         -- Proposed instance of template
160         -> Maybe TyVarSubstEnv          -- Matching substitution
161                                         
162
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
168
169 matchTy tmpls ty1 ty2 = match ty1 ty2 tmpls (\ senv -> Just senv) emptySubstEnv
170
171 matchTys tmpls tys1 tys2 = match_list tys1 tys2 tmpls 
172                                       (\ (senv,tys) -> Just (senv,tys))
173                                       emptySubstEnv
174 \end{code}
175
176 @match@ is the main function.
177
178 \begin{code}
179 match :: Type -> Type                           -- Current match pair
180       -> TyVarSet                               -- Template vars
181       -> (TyVarSubstEnv -> Maybe result)        -- Continuation
182       -> TyVarSubstEnv                          -- Current subst
183       -> Maybe result
184
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.
188
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
196
197   | otherwise
198   =     -- v is not a template variable; ty had better match
199         -- Can't use (==) because types differ
200     case ty of
201         TyVarTy v' | v == v' -> k senv    -- Success
202         other                -> Nothing   -- Failure
203
204 match (FunTy arg1 res1) (FunTy arg2 res2) tmpls k senv
205   = match arg1 arg2 tmpls (match res1 res2 tmpls k) senv
206
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
211
212 match (TyConApp tc1 tys1) (TyConApp tc2 tys2) tmpls k senv
213   | tc1 == tc2
214   = match_list tys1 tys2 tmpls k' senv
215   where
216     k' (senv', tys2') | null tys2' = k senv'    -- Succeed
217                       | otherwise  = Nothing    -- Fail 
218
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
222         -- here! (WDP 95/05)
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
225
226 -- Catch-all fails
227 match _ _ _ _ _ = Nothing
228
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
232 \end{code}
233
234