[project @ 1999-07-15 14:08:03 by keithw]
[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 Var      ( TyVar, tyVarKind )
20 import VarSet
21 import VarEnv   ( TyVarSubstEnv, emptySubstEnv, lookupSubstEnv, extendSubstEnv, 
22                   SubstResult(..)
23                 )
24
25 import Unique   ( Uniquable(..) )
26 import Outputable( panic )
27 import Util     ( snocView )
28 \end{code}
29
30 %************************************************************************
31 %*                                                                      *
32 \subsection{Unification wih a explicit substitution}
33 %*                                                                      *
34 %************************************************************************
35
36 Unify types with an explicit substitution and no monad.
37
38 \begin{code}
39 type MySubst
40    = (TyVarSet,         -- Set of template tyvars
41       TyVarSubstEnv)    -- Not necessarily idempotent
42
43 unifyTysX :: TyVarSet           -- Template tyvars
44           -> Type
45           -> Type
46           -> Maybe TyVarSubstEnv
47 unifyTysX tmpl_tyvars ty1 ty2
48   = uTysX ty1 ty2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
49
50 unifyTyListsX :: TyVarSet -> [Type] -> [Type]
51               -> Maybe TyVarSubstEnv
52 unifyTyListsX tmpl_tyvars tys1 tys2
53   = uTyListsX tys1 tys2 (\(_,s) -> Just s) (tmpl_tyvars, emptySubstEnv)
54
55
56 uTysX :: Type
57       -> Type
58       -> (MySubst -> Maybe result)
59       -> MySubst
60       -> Maybe result
61
62 uTysX (NoteTy _ ty1) ty2 k subst = uTysX ty1 ty2 k subst
63 uTysX ty1 (NoteTy _ ty2) k subst = uTysX ty1 ty2 k subst
64
65         -- Variables; go for uVar
66 uTysX (TyVarTy tyvar1) (TyVarTy tyvar2) k subst 
67   | tyvar1 == tyvar2
68   = k subst
69 uTysX (TyVarTy tyvar1) ty2 k subst@(tmpls,_)
70   | tyvar1 `elemVarSet` tmpls
71   = uVarX tyvar1 ty2 k subst
72 uTysX ty1 (TyVarTy tyvar2) k subst@(tmpls,_)
73   | tyvar2 `elemVarSet` tmpls
74   = uVarX tyvar2 ty1 k subst
75
76         -- Functions; just check the two parts
77 uTysX (FunTy fun1 arg1) (FunTy fun2 arg2) k subst
78   = uTysX fun1 fun2 (uTysX arg1 arg2 k) subst
79
80         -- Type constructors must match
81 uTysX (TyConApp con1 tys1) (TyConApp con2 tys2) k subst
82   | (con1 == con2 && length tys1 == length tys2)
83   = uTyListsX tys1 tys2 k subst
84
85         -- Applications need a bit of care!
86         -- They can match FunTy and TyConApp, so use splitAppTy_maybe
87         -- NB: we've already dealt with type variables and Notes,
88         -- so if one type is an App the other one jolly well better be too
89 uTysX (AppTy s1 t1) ty2 k subst
90   = case splitAppTy_maybe ty2 of
91       Just (s2, t2) -> uTysX s1 s2 (uTysX t1 t2 k) subst
92       Nothing       -> Nothing    -- Fail
93
94 uTysX ty1 (AppTy s2 t2) k subst
95   = case splitAppTy_maybe ty1 of
96       Just (s1, t1) -> uTysX s1 s2 (uTysX t1 t2 k) subst
97       Nothing       -> Nothing    -- Fail
98
99         -- Not expecting for-alls in unification
100 #ifdef DEBUG
101 uTysX (ForAllTy _ _) ty2 k subst = panic "Unify.uTysX subst:ForAllTy (1st arg)"
102 uTysX ty1 (ForAllTy _ _) k subst = panic "Unify.uTysX subst:ForAllTy (2nd arg)"
103 #endif
104
105         -- Anything else fails
106 uTysX ty1 ty2 k subst = Nothing
107
108
109 uTyListsX []         []         k subst = k subst
110 uTyListsX (ty1:tys1) (ty2:tys2) k subst = uTysX ty1 ty2 (uTyListsX tys1 tys2 k) subst
111 uTyListsX tys1       tys2       k subst = Nothing   -- Fail if the lists are different lengths
112 \end{code}
113
114 \begin{code}
115 -- Invariant: tv1 is a unifiable variable
116 uVarX tv1 ty2 k subst@(tmpls, env)
117   = case lookupSubstEnv env tv1 of
118       Just (DoneTy ty1) ->    -- Already bound
119                      uTysX ty1 ty2 k subst
120
121       Nothing        -- Not already bound
122                |  typeKind ty2 == tyVarKind tv1
123                && occur_check_ok ty2
124                ->     -- No kind mismatch nor occur check
125                   k (tmpls, extendSubstEnv env tv1 (DoneTy ty2))
126
127                | otherwise -> Nothing   -- Fail if kind mis-match or occur check
128   where
129     occur_check_ok ty = all occur_check_ok_tv (varSetElems (tyVarsOfType ty))
130     occur_check_ok_tv tv | tv1 == tv = False
131                          | otherwise = case lookupSubstEnv env tv of
132                                          Nothing           -> True
133                                          Just (DoneTy ty)  -> occur_check_ok ty
134 \end{code}
135
136
137
138 %************************************************************************
139 %*                                                                      *
140 \subsection{Matching on types}
141 %*                                                                      *
142 %************************************************************************
143
144 Matching is a {\em unidirectional} process, matching a type against a
145 template (which is just a type with type variables in it).  The
146 matcher assumes that there are no repeated type variables in the
147 template, so that it simply returns a mapping of type variables to
148 types.  It also fails on nested foralls.
149
150 @matchTys@ matches corresponding elements of a list of templates and
151 types.
152
153 \begin{code}
154 matchTy :: TyVarSet                     -- Template tyvars
155         -> Type                         -- Template
156         -> Type                         -- Proposed instance of template
157         -> Maybe TyVarSubstEnv          -- Matching substitution
158                                         
159
160 matchTys :: TyVarSet                    -- Template tyvars
161          -> [Type]                      -- Templates
162          -> [Type]                      -- Proposed instance of template
163          -> Maybe (TyVarSubstEnv,               -- Matching substitution
164                    [Type])              -- Left over instance types
165
166 matchTy tmpls ty1 ty2 = match ty1 ty2 tmpls (\ senv -> Just senv) emptySubstEnv
167
168 matchTys tmpls tys1 tys2 = match_list tys1 tys2 tmpls 
169                                       (\ (senv,tys) -> Just (senv,tys))
170                                       emptySubstEnv
171 \end{code}
172
173 @match@ is the main function.
174
175 \begin{code}
176 match :: Type -> Type                           -- Current match pair
177       -> TyVarSet                               -- Template vars
178       -> (TyVarSubstEnv -> Maybe result)        -- Continuation
179       -> TyVarSubstEnv                          -- Current subst
180       -> Maybe result
181
182 -- When matching against a type variable, see if the variable
183 -- has already been bound.  If so, check that what it's bound to
184 -- is the same as ty; if not, bind it and carry on.
185
186 match (TyVarTy v) ty tmpls k senv
187   | v `elemVarSet` tmpls
188   =     -- v is a template variable
189     case lookupSubstEnv senv v of
190         Nothing -> k (extendSubstEnv senv v (DoneTy ty))
191         Just (DoneTy ty')  | ty' == ty         -> k senv   -- Succeeds
192                            | otherwise         -> Nothing  -- Fails
193
194   | otherwise
195   =     -- v is not a template variable; ty had better match
196         -- Can't use (==) because types differ
197     case ty of
198         TyVarTy v' | v == v' -> k senv    -- Success
199         other                -> Nothing   -- Failure
200
201 match (FunTy arg1 res1) (FunTy arg2 res2) tmpls k senv
202   = match arg1 arg2 tmpls (match res1 res2 tmpls k) senv
203
204 match (AppTy fun1 arg1) ty2 tmpls k senv 
205   = case splitAppTy_maybe ty2 of
206         Just (fun2,arg2) -> match fun1 fun2 tmpls (match arg1 arg2 tmpls k) senv
207         Nothing          -> Nothing     -- Fail
208
209 match (TyConApp tc1 tys1) (TyConApp tc2 tys2) tmpls k senv
210   | tc1 == tc2
211   = match_list tys1 tys2 tmpls k' senv
212   where
213     k' (senv', tys2') | null tys2' = k senv'    -- Succeed
214                       | otherwise  = Nothing    -- Fail 
215
216         -- With type synonyms, we have to be careful for the exact
217         -- same reasons as in the unifier.  Please see the
218         -- considerable commentary there before changing anything
219         -- here! (WDP 95/05)
220 match (NoteTy _ ty1) ty2            tmpls k senv = match ty1 ty2 tmpls k senv
221 match ty1            (NoteTy _ ty2) tmpls k senv = match ty1 ty2 tmpls k senv
222
223 -- Catch-all fails
224 match _ _ _ _ _ = Nothing
225
226 match_list []         tys2       tmpls k senv = k (senv, tys2)
227 match_list (ty1:tys1) []         tmpls k senv = Nothing -- Not enough arg tys => failure
228 match_list (ty1:tys1) (ty2:tys2) tmpls k senv = match ty1 ty2 tmpls (match_list tys1 tys2 tmpls k) senv
229 \end{code}
230