[project @ 2000-07-11 16:24:57 by simonmar]
[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(..), funTyCon
15                 )  -- friend
16 import Type     ( 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 import Util     ( snocView )
32 \end{code}
33
34 %************************************************************************
35 %*                                                                      *
36 \subsection{Unification wih a explicit substitution}
37 %*                                                                      *
38 %************************************************************************
39
40 Unify types with an explicit substitution and no monad.
41
42 \begin{code}
43 type MySubst
44    = (TyVarSet,         -- Set of template tyvars
45       TyVarSubstEnv)    -- Not necessarily idempotent
46
47 unifyTysX :: TyVarSet           -- Template tyvars
48           -> Type
49           -> Type
50           -> Maybe TyVarSubstEnv
51 unifyTysX tmpl_tyvars ty1 ty2
52   = uTysX ty1 ty2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
53
54 unifyTyListsX :: TyVarSet -> [Type] -> [Type]
55               -> Maybe TyVarSubstEnv
56 unifyTyListsX tmpl_tyvars tys1 tys2
57   = uTyListsX tys1 tys2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
58
59
60 uTysX :: Type
61       -> Type
62       -> (MySubst -> Maybe result)
63       -> MySubst
64       -> Maybe result
65
66 uTysX (NoteTy _ ty1) ty2 k subst = uTysX ty1 ty2 k subst
67 uTysX ty1 (NoteTy _ ty2) k subst = uTysX ty1 ty2 k subst
68
69         -- Variables; go for uVar
70 uTysX (TyVarTy tyvar1) (TyVarTy tyvar2) k subst 
71   | tyvar1 == tyvar2
72   = 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
79
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
83
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
88
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
97
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
102
103         -- Not expecting for-alls in unification
104 #ifdef DEBUG
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)"
107 #endif
108
109         -- Anything else fails
110 uTysX ty1 ty2 k subst = Nothing
111
112
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
116 \end{code}
117
118 \begin{code}
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
124
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))
130
131                | otherwise -> Nothing   -- Fail if kind mis-match or occur check
132   where
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
136                                          Nothing           -> True
137                                          Just (DoneTy ty)  -> occur_check_ok ty
138 \end{code}
139
140
141
142 %************************************************************************
143 %*                                                                      *
144 \subsection{Matching on types}
145 %*                                                                      *
146 %************************************************************************
147
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.
153
154 @matchTys@ matches corresponding elements of a list of templates and
155 types.
156
157 \begin{code}
158 matchTy :: TyVarSet                     -- Template tyvars
159         -> Type                         -- Template
160         -> Type                         -- Proposed instance of template
161         -> Maybe TyVarSubstEnv          -- Matching substitution
162                                         
163
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
169
170 matchTy tmpls ty1 ty2 = match ty1 ty2 tmpls (\ senv -> Just senv) emptySubstEnv
171
172 matchTys tmpls tys1 tys2 = match_list tys1 tys2 tmpls 
173                                       (\ (senv,tys) -> Just (senv,tys))
174                                       emptySubstEnv
175 \end{code}
176
177 @match@ is the main function.
178
179 \begin{code}
180 match :: Type -> Type                           -- Current match pair
181       -> TyVarSet                               -- Template vars
182       -> (TyVarSubstEnv -> Maybe result)        -- Continuation
183       -> TyVarSubstEnv                          -- Current subst
184       -> Maybe result
185
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.
189
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
197
198   | otherwise
199   =     -- v is not a template variable; ty had better match
200         -- Can't use (==) because types differ
201     case ty of
202         TyVarTy v' | v == v' -> k senv    -- Success
203         other                -> Nothing   -- Failure
204
205 match (FunTy arg1 res1) (FunTy arg2 res2) tmpls k senv
206   = match arg1 arg2 tmpls (match res1 res2 tmpls k) senv
207
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
212
213 match (TyConApp tc1 tys1) (TyConApp tc2 tys2) tmpls k senv
214   | tc1 == tc2
215   = match_list tys1 tys2 tmpls k' senv
216   where
217     k' (senv', tys2') | null tys2' = k senv'    -- Succeed
218                       | otherwise  = Nothing    -- Fail 
219
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
223         -- here! (WDP 95/05)
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
226
227 -- Catch-all fails
228 match _ _ _ _ _ = Nothing
229
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
233 \end{code}
234