[project @ 2004-09-30 10:35:15 by simonpj]
[ghc-hetmet.git] / ghc / compiler / types / Unify.lhs
1 \begin{code}
2 module Unify ( 
3         -- Matching and unification
4         matchTys, matchTyX, matchTysX,
5         unifyTys, unifyTysX,
6
7         tcRefineTys, tcMatchTys, tcMatchPreds, coreRefineTys,
8
9         -- Re-export
10         MaybeErr(..)
11    ) where
12
13 #include "HsVersions.h"
14
15 import Var              ( Var, TyVar, tyVarKind )
16 import VarEnv
17 import VarSet
18 import Kind             ( isSubKind )
19 import Type             ( predTypeRep, newTypeRep, typeKind, 
20                           tyVarsOfType, tyVarsOfTypes, 
21                           TvSubstEnv, TvSubst(..), substTy )
22 import TypeRep          ( Type(..), PredType(..), funTyCon )
23 import Util             ( snocView )
24 import ErrUtils         ( Message )
25 import Outputable
26 import Maybes
27 \end{code}
28
29
30 %************************************************************************
31 %*                                                                      *
32                 External interface
33 %*                                                                      *
34 %************************************************************************
35
36 \begin{code}
37 ----------------------------
38 tcRefineTys, tcMatchTys 
39         :: [TyVar]                      -- Try to unify these
40         -> TvSubstEnv                   -- Not idempotent
41         -> [Type] -> [Type]
42         -> MaybeErr TvSubstEnv Message  -- Not idempotent
43 -- This one is used by the type checker.  Neither the input nor result
44 -- substitition is idempotent
45 tcRefineTys ex_tvs subst tys1 tys2
46   = initUM (tryToBind (mkVarSet ex_tvs)) (unify_tys Src subst tys1 tys2)
47
48 tcMatchTys ex_tvs subst tys1 tys2
49   = initUM (bindOnly (mkVarSet ex_tvs)) (unify_tys Src subst tys1 tys2)
50
51 tcMatchPreds
52         :: [TyVar]                      -- Bind these
53         -> [PredType] -> [PredType]
54         -> Maybe TvSubstEnv
55 tcMatchPreds tvs preds1 preds2
56   = maybeErrToMaybe $ initUM (bindOnly (mkVarSet tvs)) $
57     unify_preds Src emptyVarEnv preds1 preds2
58
59 ----------------------------
60 coreRefineTys :: [TyVar]        -- Try to unify these
61               -> TvSubst        -- A full-blown apply-once substitition
62               -> Type           -- A fixed point of the incoming substitution
63               -> Type
64               -> Maybe TvSubstEnv       -- In-scope set is unaffected
65 -- Used by Core Lint and the simplifier.  Takes a full apply-once substitution.
66 -- The incoming substitution's in-scope set should mention all the variables free 
67 -- in the incoming types
68 coreRefineTys ex_tvs subst@(TvSubst in_scope orig_env) ty1 ty2
69   = maybeErrToMaybe $ initUM (tryToBind (mkVarSet ex_tvs)) $
70     do  {       -- Apply the input substitution; nothing int ty2
71           let ty1' = substTy subst ty1  
72                 -- Run the unifier, starting with an empty env
73         ; extra_env <- unify Src emptyTvSubstEnv ty1' ty2
74
75                 -- Find the fixed point of the resulting non-idempotent
76                 -- substitution, and apply it to the 
77         ; let extra_subst     = TvSubst in_scope extra_env_fixpt
78               extra_env_fixpt = mapVarEnv (substTy extra_subst) extra_env
79               orig_env'       = mapVarEnv (substTy extra_subst) orig_env
80         ; return (orig_env' `plusVarEnv` extra_env_fixpt) }
81     
82
83 ----------------------------
84 matchTys :: TyVarSet            -- Template tyvars
85          -> [Type]              -- Template
86          -> [Type]              -- Target
87          -> Maybe TvSubstEnv    -- Idempotent, because when matching
88                                 --      the range and domain are distinct
89
90 -- PRE-CONDITION for matching: template variables are not free in the target
91
92 matchTys tmpls tys1 tys2
93   = ASSERT2( not (intersectsVarSet tmpls (tyVarsOfTypes tys2)),
94              ppr tmpls $$ ppr tys1 $$ ppr tys2 )
95     maybeErrToMaybe $ initUM (bindOnly tmpls)
96                              (unify_tys Src emptyTvSubstEnv tys1 tys2)
97
98 matchTyX :: TyVarSet            -- Template tyvars
99          -> TvSubstEnv          -- Idempotent substitution to extend
100          -> Type                -- Template
101          -> Type                -- Target
102          -> Maybe TvSubstEnv    -- Idempotent
103
104 matchTyX tmpls env ty1 ty2
105   = ASSERT( not (intersectsVarSet tmpls (tyVarsOfType ty2)) )
106     maybeErrToMaybe $ initUM (bindOnly tmpls)
107                              (unify Src env ty1 ty2)
108
109 matchTysX :: TyVarSet           -- Template tyvars
110           -> TvSubstEnv         -- Idempotent substitution to extend
111           -> [Type]             -- Template
112           -> [Type]             -- Target
113           -> Maybe TvSubstEnv   -- Idempotent
114
115 matchTysX tmpls env tys1 tys2
116   = ASSERT( not (intersectsVarSet tmpls (tyVarsOfTypes tys2)) )
117     maybeErrToMaybe $ initUM (bindOnly tmpls) 
118                              (unify_tys Src env tys1 tys2)
119
120
121 ----------------------------
122 unifyTys :: TyVarSet -> [Type] -> [Type] -> Maybe TvSubstEnv
123 unifyTys bind_these tys1 tys2
124   = maybeErrToMaybe $ initUM (bindOnly bind_these) $
125     unify_tys Src emptyTvSubstEnv tys1 tys2
126
127 unifyTysX :: TyVarSet -> TvSubstEnv -> [Type] -> [Type] -> Maybe TvSubstEnv
128 unifyTysX bind_these subst tys1 tys2
129   = maybeErrToMaybe $ initUM (bindOnly bind_these) $
130     unify_tys Src subst tys1 tys2
131
132 ----------------------------
133 tryToBind, bindOnly :: TyVarSet -> TyVar -> BindFlag
134 tryToBind tv_set tv | tv `elemVarSet` tv_set = BindMe
135                     | otherwise              = AvoidMe
136
137 bindOnly tv_set tv | tv `elemVarSet` tv_set = BindMe
138                    | otherwise              = DontBindMe
139
140 emptyTvSubstEnv :: TvSubstEnv
141 emptyTvSubstEnv = emptyVarEnv
142 \end{code}
143
144
145 %************************************************************************
146 %*                                                                      *
147                 The workhorse
148 %*                                                                      *
149 %************************************************************************
150
151 \begin{code}
152 unify :: SrcFlag                -- True, unifying source types, false core types.
153       -> TvSubstEnv             -- An existing substitution to extend
154       -> Type -> Type           -- Types to be unified
155       -> UM TvSubstEnv          -- Just the extended substitution, 
156                                 -- Nothing if unification failed
157 -- We do not require the incoming substitution to be idempotent,
158 -- nor guarantee that the outgoing one is.  That's fixed up by
159 -- the wrappers.
160
161 -- ToDo: remove debugging junk
162 unify s subst ty1 ty2 = -- pprTrace "unify" (ppr subst <+> pprParendType ty1 <+> pprParendType ty2) $
163                         unify_ s subst ty1 ty2
164
165 -- Look through NoteTy in the obvious fashion
166 unify_ s subst (NoteTy _ ty1) ty2  = unify s subst ty1 ty2
167 unify_ s subst ty1 (NoteTy _ ty2)  = unify s subst ty1 ty2
168
169 -- In Core mode, look through NewTcApps and Preds
170 unify_ Core subst (NewTcApp tc tys) ty2 = unify Core subst (newTypeRep tc tys) ty2
171 unify_ Core subst ty1 (NewTcApp tc tys) = unify Core subst ty1 (newTypeRep tc tys)
172
173 unify_ Core subst (PredTy p) ty2 = unify Core subst (predTypeRep p) ty2
174 unify_ Core subst ty1 (PredTy p) = unify Core subst ty1 (predTypeRep p)
175
176 -- From now on, any NewTcApps/Preds should be taken at face value
177
178 unify_ s subst (TyVarTy tv1) ty2  = uVar s False subst tv1 ty2
179 unify_ s subst ty1 (TyVarTy tv2)  = uVar s True  subst tv2 ty1
180
181 unify_ s subst (PredTy p1) (PredTy p2) = unify_pred s subst p1 p2
182
183 unify_ s subst t1@(TyConApp tyc1 tys1) t2@(TyConApp tyc2 tys2) 
184   | tyc1 == tyc2 = unify_tys s subst tys1 tys2
185 unify_ Src subst t1@(NewTcApp tc1 tys1) t2@(NewTcApp tc2 tys2)  
186   | tc1 == tc2 = unify_tys Src subst tys1 tys2
187 unify_ s subst (FunTy ty1a ty1b) (FunTy ty2a ty2b) 
188   = do { subst' <- unify s subst ty1a ty2a
189        ; unify s subst' ty1b ty2b }
190
191         -- Applications need a bit of care!
192         -- They can match FunTy and TyConApp, so use splitAppTy_maybe
193         -- NB: we've already dealt with type variables and Notes,
194         -- so if one type is an App the other one jolly well better be too
195 unify_ s subst (AppTy ty1a ty1b) ty2
196   | Just (ty2a, ty2b) <- unifySplitAppTy_maybe ty2
197   = do  { subst' <- unify s subst ty1a ty2a
198         ; unify s subst' ty1b ty2b }
199
200 unify_ s subst ty1 (AppTy ty2a ty2b)
201   | Just (ty1a, ty1b) <- unifySplitAppTy_maybe ty1
202   = do  { subst' <- unify s subst ty1a ty2a
203         ; unify s subst' ty1b ty2b }
204
205 unify_ s subst ty1 ty2 = failWith (misMatch ty1 ty2)
206
207 ------------------------------
208 unify_pred s subst (ClassP c1 tys1) (ClassP c2 tys2)
209   | c1 == c2 = unify_tys s subst tys1 tys2
210 unify_pred s subst (IParam n1 t1) (IParam n2 t2)
211   | n1 == n2 = unify s subst t1 t2
212  
213 ------------------------------
214 unifySplitAppTy_maybe :: Type -> Maybe (Type,Type)
215 -- NoteTy is already dealt with; take NewTcApps at face value
216 unifySplitAppTy_maybe (FunTy ty1 ty2)   = Just (TyConApp funTyCon [ty1], ty2)
217 unifySplitAppTy_maybe (AppTy ty1 ty2)   = Just (ty1, ty2)
218 unifySplitAppTy_maybe (TyConApp tc tys) = case snocView tys of
219                                                 Just (tys', ty') -> Just (TyConApp tc tys', ty')
220                                                 Nothing          -> Nothing
221 unifySplitAppTy_maybe (NewTcApp tc tys) = case snocView tys of
222                                                 Just (tys', ty') -> Just (NewTcApp tc tys', ty')
223                                                 Nothing          -> Nothing
224 unifySplitAppTy_maybe other = Nothing
225
226 ------------------------------
227 unify_tys s   = unifyList (unify s)
228
229 unify_preds :: SrcFlag -> TvSubstEnv -> [PredType] -> [PredType] -> UM TvSubstEnv
230 unify_preds s = unifyList (unify_pred s)
231
232 unifyList :: Outputable a 
233           => (TvSubstEnv -> a -> a -> UM TvSubstEnv)
234           -> TvSubstEnv -> [a] -> [a] -> UM TvSubstEnv
235 unifyList unifier subst orig_xs orig_ys
236   = go subst orig_xs orig_ys
237   where
238     go subst []     []     = return subst
239     go subst (x:xs) (y:ys) = do { subst' <- unifier subst x y
240                                 ; go subst' xs ys }
241     go subst _      _      = failWith (lengthMisMatch orig_xs orig_ys)
242
243 ------------------------------
244 uVar :: SrcFlag         -- True, unifying source types, false core types.
245      -> Bool            -- Swapped
246      -> TvSubstEnv      -- An existing substitution to extend
247      -> TyVar           -- Type variable to be unified
248      -> Type            -- with this type
249      -> UM TvSubstEnv
250
251 uVar s swap subst tv1 ty
252  = -- check to see whether tv1 is refined
253    case (lookupVarEnv subst tv1) of
254      -- yes, call back into unify'
255      Just ty' | swap      -> unify s subst ty ty' 
256               | otherwise -> unify s subst ty' ty
257      -- No, continue
258      Nothing          -> uUnrefined subst tv1 ty
259
260
261 uUnrefined :: TvSubstEnv          -- An existing substitution to extend
262            -> TyVar               -- Type variable to be unified
263            -> Type                -- with this type
264            -> UM TvSubstEnv
265
266 -- We know that tv1 isn't refined
267 uUnrefined subst tv1 ty2@(TyVarTy tv2)
268   | tv1 == tv2    -- Same, do nothing
269   = return subst
270
271     -- Check to see whether tv2 is refined
272   | Just ty' <- lookupVarEnv subst tv2
273   = uUnrefined subst tv1 ty'
274
275   -- So both are unrefined; next, see if the kinds force the direction
276   | k1 == k2    -- Can update either; so check the bind-flags
277   = do  { b1 <- tvBindFlag tv1
278         ; b2 <- tvBindFlag tv2
279         ; case (b1,b2) of
280             (DontBindMe, DontBindMe) -> failWith (misMatch ty1 ty2)
281             (DontBindMe, _)          -> bindTv subst tv2 ty1
282             (BindMe, _)              -> bindTv subst tv1 ty2
283             (AvoidMe, BindMe)        -> bindTv subst tv2 ty1
284             (AvoidMe, _)             -> bindTv subst tv1 ty2
285         }
286
287   | k1 `isSubKind` k2   -- Must update tv2
288   = do  { b2 <- tvBindFlag tv2
289         ; case b2 of
290             DontBindMe -> failWith (misMatch ty1 ty2)
291             other      -> bindTv subst tv2 ty1
292         }
293
294   | k2 `isSubKind` k1   -- Must update tv1
295   = do  { b1 <- tvBindFlag tv1
296         ; case b1 of
297             DontBindMe -> failWith (misMatch ty1 ty2)
298             other      -> bindTv subst tv1 ty2
299         }
300
301   | otherwise = failWith (kindMisMatch tv1 ty2)
302   where
303     ty1 = TyVarTy tv1
304     k1 = tyVarKind tv1
305     k2 = tyVarKind tv2
306
307 uUnrefined subst tv1 ty2        -- ty2 is not a type variable
308         -- Do occurs check...
309   | tv1 `elemVarSet` substTvSet subst (tyVarsOfType ty2)
310   = failWith (occursCheck tv1 ty2)
311         -- And a kind check...
312   | k2 `isSubKind` k1
313   = do  { b1 <- tvBindFlag tv1
314         ; case b1 of            -- And  check that tv1 is bindable
315             DontBindMe -> failWith (misMatch ty1 ty2)
316             other      -> bindTv subst tv1 ty2
317         }
318   | otherwise
319   = pprTrace "kind" (ppr tv1 <+> ppr k1 $$ ppr ty2 <+> ppr k2) $
320     failWith (kindMisMatch tv1 ty2)
321   where
322     ty1 = TyVarTy tv1
323     k1 = tyVarKind tv1
324     k2 = typeKind ty2
325
326 substTvSet :: TvSubstEnv -> TyVarSet -> TyVarSet
327 -- Apply the non-idempotent substitution to a set of type variables,
328 -- remembering that the substitution isn't necessarily idempotent
329 substTvSet subst tvs
330   = foldVarSet (unionVarSet . get) emptyVarSet tvs
331   where
332     get tv = case lookupVarEnv subst tv of
333                 Nothing -> unitVarSet tv
334                 Just ty -> substTvSet subst (tyVarsOfType ty)
335
336 bindTv subst tv ty = return (extendVarEnv subst tv ty)
337 \end{code}
338
339 %************************************************************************
340 %*                                                                      *
341                 Unification monad
342 %*                                                                      *
343 %************************************************************************
344
345 \begin{code}
346 data SrcFlag = Src | Core       -- Unifying at the source level, or core level?
347
348 data BindFlag = BindMe | AvoidMe | DontBindMe
349
350 isCore Core = True
351 isCore Src  = False
352
353 newtype UM a = UM { unUM :: (TyVar -> BindFlag)
354                          -> MaybeErr a Message }
355
356 instance Monad UM where
357   return a = UM (\tvs -> Succeeded a)
358   fail s   = UM (\tvs -> Failed (text s))
359   m >>= k  = UM (\tvs -> case unUM m tvs of
360                            Failed err -> Failed err
361                            Succeeded v  -> unUM (k v) tvs)
362
363 initUM :: (TyVar -> BindFlag) -> UM a -> MaybeErr a Message
364 initUM badtvs um = unUM um badtvs
365
366 tvBindFlag :: TyVar -> UM BindFlag
367 tvBindFlag tv = UM (\tv_fn -> Succeeded (tv_fn tv))
368
369 failWith :: Message -> UM a
370 failWith msg = UM (\tv_fn -> Failed msg)
371
372 maybeErrToMaybe :: MaybeErr succ fail -> Maybe succ
373 maybeErrToMaybe (Succeeded a) = Just a
374 maybeErrToMaybe (Failed m)    = Nothing
375 \end{code}
376
377
378 %************************************************************************
379 %*                                                                      *
380                 Error reporting
381         We go to a lot more trouble to tidy the types
382         in TcUnify.  Maybe we'll end up having to do that
383         here too, but I'll leave it for now.
384 %*                                                                      *
385 %************************************************************************
386
387 \begin{code}
388 misMatch t1 t2
389   = ptext SLIT("Can't match types") <+> quotes (ppr t1) <+> 
390     ptext SLIT("and") <+> quotes (ppr t2)
391
392 lengthMisMatch tys1 tys2
393   = sep [ptext SLIT("Can't match unequal length lists"), 
394          nest 2 (ppr tys1), nest 2 (ppr tys2) ]
395
396 kindMisMatch tv1 t2
397   = vcat [ptext SLIT("Can't match kinds") <+> quotes (ppr (tyVarKind tv1)) <+> 
398             ptext SLIT("and") <+> quotes (ppr (typeKind t2)),
399           ptext SLIT("when matching") <+> quotes (ppr tv1) <+> 
400                 ptext SLIT("with") <+> quotes (ppr t2)]
401
402 occursCheck tv ty
403   = hang (ptext SLIT("Can't construct the infinite type"))
404        2 (ppr tv <+> equals <+> ppr ty)
405 \end{code}