2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[Unify]{Unifier}
6 The unifier is now squarely in the typechecker monad (because of the
7 updatable substitution).
10 #include "HsVersions.h"
12 module Unify ( unifyTauTy, unifyTauTyList, unifyTauTyLists ) where
14 IMPORT_Trace -- ToDo: rm (debugging only)
21 import CmdLineOpts ( GlobalSwitch(..) )
22 import Errors ( unifyErr, UnifyErrInfo(..), UnifyErrContext )
23 import Id ( Id, DataCon(..), Inst )
24 import Maybes ( Maybe(..) )
25 import Subst ( extendSubst, SubstResult(..), Subst )
26 #if USE_ATTACK_PRAGMAS
27 import Class ( Class(..), cmpClass ) -- .. for pragmas only
28 import TyCon ( TyCon(..), isBoxedTyCon, isVisibleSynTyCon, cmpTyCon )
29 -- .. on TyCon is for pragmas only
30 import TyVar -- make all visible for pragmas
31 import UniTyFuns ( pprUniType, pprTyCon )
33 import Class ( Class )
34 import TyVar ( TyVar(..), TyVarTemplate )
35 import TyCon ( TyCon, isBoxedTyCon, isVisibleSynTyCon )
37 import UniType ( UniType(..), TauType(..)
38 IF_ATTACK_PRAGMAS(COMMA cmpUniType)
43 %************************************************************************
45 \subsection[Unify-spec]{Specification}
47 %************************************************************************
49 CLAIM: the unifier works correctly even if the types to be unified are not
50 fixed points of the substitution.
52 %************************************************************************
54 \subsection[Unify-exported]{Exported unification functions}
56 %************************************************************************
58 The exported functions are all defined as versions of some
59 non-exported generic functions.
61 Unify two @TauType@s. Dead straightforward.
64 unifyTauTy :: TauType -> TauType -> UnifyErrContext -> TcM ()
66 unifyTauTy ty1 ty2 err_ctxt = uTys ty1 ty1 ty2 ty2 err_ctxt
69 @unifyTauTyLists@ unifies corresponding elements of its two list
70 arguments. The lists should be of equal length.
73 unifyTauTyLists :: [TauType] -> [TauType] -> UnifyErrContext -> TcM ()
75 unifyTauTyLists tys1 tys2 err_ctxt = uList tys1 tys2 err_ctxt
78 @unifyTauTyList@ takes a single list of @TauType@s and unifies them
79 all together. It is used, for example, when typechecking explicit
80 lists, when all the elts should be of the same type.
83 unifyTauTyList :: [TauType] -> UnifyErrContext -> TcM ()
85 unifyTauTyList [] _ = returnTc ()
86 unifyTauTyList [ty] _ = returnTc ()
88 unifyTauTyList (ty1:tys@(ty2:_)) err_ctxt
89 = unifyTauTy ty1 ty2 err_ctxt `thenTc_`
90 unifyTauTyList tys err_ctxt
93 %************************************************************************
95 \subsection[Unify-lists-of-types]{@uList@}
97 %************************************************************************
99 @uList@ unifies corresponding elements of two lists of @TauType@s. It
100 uses @uTys@ to do the real work. We charge down the list explicitly
101 so that we can complain if their lengths differ.
104 uList :: [TauType] -> [TauType]
108 uList [] [] _ = returnTc ()
110 uList (ty1:tys1) (ty2:tys2) err_ctxt
111 = uTys ty1 ty1 ty2 ty2 err_ctxt `thenTc_`
112 uList tys1 tys2 err_ctxt
114 uList ty1s ty2s _ = panic "Unify.uList: mismatched type lists!"
117 %************************************************************************
119 \subsection[Unify-uTys]{@uTys@: getting down to business}
121 %************************************************************************
123 @uTys@ is the heart of the unifier. Each arg happens twice, because
124 we want to report errors in terms of synomyms if poss. The first of
125 the pair is used in error messages only; it is always the same as the
126 second, except that if the first is a synonym then the second may be a
127 de-synonym'd version. This way we get better error messages.
129 We call the first one \tr{ps_ty1}, \tr{ps_ty2} for ``possible synomym''.
132 uTys :: TauType -> TauType -- Error reporting ty1 and real ty1
133 -> TauType -> TauType -- Error reporting ty2 and real ty2
138 %********************************************************
140 Sanity check: should never find a UniTyVarTemplate
142 %********************************************************
147 uTys ps_ty1 ty1@(UniTyVarTemplate tv1) ps_ty2 ty2 err_ctxt
148 = pprPanic "Unify:uTys:unifying w/ UniTyVarTemplate(1):" (ppCat [ppr PprDebug tv1, ppr PprDebug ty2])
150 uTys ps_ty1 ty1 ps_ty2 ty2@(UniTyVarTemplate tv2) err_ctxt
151 = pprPanic "Unify:uTys:unifying w/ UniTyVarTemplate(2):" (ppCat [ppr PprDebug ty1, ppr PprDebug tv2])
156 %********************************************************
160 %********************************************************
163 uTys ps_ty1 (UniTyVar tyvar1) ps_ty2 ty2 err_ctxt = uVar tyvar1 ps_ty2 ty2 err_ctxt
164 uTys ps_ty1 ty1 ps_ty2 (UniTyVar tyvar2) err_ctxt = uVar tyvar2 ps_ty1 ty1 err_ctxt
167 %********************************************************
169 Both function constructors:
171 %********************************************************
174 uTys _ (UniFun fun1 arg1) _ (UniFun fun2 arg2) err_ctxt
175 = uList [fun1, arg1] [fun2, arg2] err_ctxt
178 %********************************************************
180 Both datatype constructors:
182 %********************************************************
185 uTys ps_ty1 ty1@(UniData con1 args1) ps_ty2 ty2@(UniData con2 args2) err_ctxt
186 = if (con1 == con2) then
187 -- Same constructors, just unify the arguments
188 uList args1 args2 err_ctxt
190 -- Different constructors: disaster
191 getSrcLocTc `thenNF_Tc` \ src_loc ->
192 failTc (unifyErr (UnifyMisMatch ps_ty1 ps_ty2) err_ctxt src_loc)
195 %********************************************************
199 %********************************************************
201 If just one or the other is a synonym, just expand it.
204 uTys ps_ty1 (UniSyn con1 args1 ty1) ps_ty2 ty2 err_ctxt
205 | isVisibleSynTyCon con1
206 = uTys ps_ty1 ty1 ps_ty2 ty2 err_ctxt
208 uTys ps_ty1 ty1 ps_ty2 (UniSyn con2 args2 ty2) err_ctxt
209 | isVisibleSynTyCon con2
210 = uTys ps_ty1 ty1 ps_ty2 ty2 err_ctxt
213 If you are tempted to make a short cut on synonyms, as in this
217 uTys (UniSyn con1 args1 ty1) (UniSyn con2 args2 ty2)
218 = if (con1 == con2) then
219 -- Good news! Same synonym constructors, so we can shortcut
220 -- by unifying their arguments and ignoring their expansions.
223 -- Never mind. Just expand them and try again
227 then THINK AGAIN. Here is the whole story, as detected and reported
228 by Chris Okasaki \tr{<Chris_Okasaki@loch.mess.cs.cmu.edu>}:
230 Here's a test program that should detect the problem:
234 x = (1 :: Bogus Char) :: Bogus Bool
237 The problem with [the attempted shortcut code] is that
241 is not a sufficient condition to be able to use the shortcut!
242 You also need to know that the type synonym actually USES all
243 its arguments. For example, consider the following type synonym
244 which does not use all its arguments.
249 If you ever tried unifying, say, \tr{Bogus Char} with \tr{Bogus Bool},
250 the unifier would blithely try to unify \tr{Char} with \tr{Bool} and
251 would fail, even though the expanded forms (both \tr{Int}) should
254 Similarly, unifying \tr{Bogus Char} with \tr{Bogus t} would
255 unnecessarily bind \tr{t} to \tr{Char}.
257 ... You could explicitly test for the problem synonyms and mark them
258 somehow as needing expansion, perhaps also issuing a warning to the
262 Still, if the synonym is abstract, we can only just go ahead and try!
265 uTys ps_ty1 (UniSyn con1 args1 ty1) ps_ty2 (UniSyn con2 args2 ty2) err_ctxt
266 -- Both must be abstract (i.e., non "visible" -- not done yet)
267 = if (con1 == con2) then
268 -- Good news! Same synonym constructors, so we can shortcut
269 -- by unifying their arguments and ignoring their expansions.
270 uList args1 args2 err_ctxt
272 -- Bad news; mis-matched type constructors
273 getSrcLocTc `thenNF_Tc` \ src_loc ->
274 failTc (unifyErr (UnifyMisMatch ps_ty1 ps_ty2) err_ctxt src_loc)
277 %********************************************************
279 Catch-all case---just fails:
281 %********************************************************
283 Anything else fails. For example, matching a @UniFun@ against
286 uTys ps_ty1 ty1 ps_ty2 ty2 err_ctxt
287 = getSrcLocTc `thenNF_Tc` \ src_loc ->
288 failTc (unifyErr (UnifyMisMatch ps_ty1 ps_ty2) err_ctxt src_loc)
291 %************************************************************************
293 \subsection[Unify-uVar]{@uVar@: unifying with a type variable}
295 %************************************************************************
297 @uVar@ is called when at least one of the types being unified is a
298 variable. It does {\em not} assume that the variable is a fixed point
299 of the substitution; rather, notice that @bindTo@ (defined below) nips
300 back into @uTys@ if it turns out that the variable is already bound.
302 There is a slight worry that one might try to @bindTo@ a (say) Poly
303 tyvar (as tv1) with an Open tyvar (as ty2) which is already unified to
304 an unboxed type. In fact this can't happen, because the Open ones are
305 always the ones which are unified away.
309 -> UniType -> UniType -- printing and real versions
313 uVar tv1 ps_ty2 ty2 err_ctxt
317 do _ (UniSyn _ _ ty2) = do tv1 ty2
319 -- Commit any open type variable
320 do (OpenSysTyVar _) ty2 = tv1 `bindTo` ps_ty2
321 do _ ty2@(UniTyVar tv2@(OpenSysTyVar _)) = tv2 `bindTo` ty1
323 -- Eliminate Poly in favour of User
324 do (PolySysTyVar _) ty2@(UniTyVar (UserTyVar _ _)) = tv1 `bindTo` ps_ty2
325 do (PolySysTyVar _) ty2@(UniTyVar (PolySysTyVar _)) = tv1 `bindTo` ps_ty2
326 do (UserTyVar _ _) ty2@(UniTyVar tv2@(PolySysTyVar _)) = tv2 `bindTo` ty1
327 do (UserTyVar _ _) ty2@(UniTyVar (UserTyVar _ _)) = tv1 `bindTo` ps_ty2
329 -- Matching for boxed data types
330 do (PolySysTyVar _) ty2@(UniData con _) | isBoxedTyCon con = tv1 `bindTo` ps_ty2
331 do (UserTyVar _ _) ty2@(UniData con _) | isBoxedTyCon con = tv1 `bindTo` ps_ty2
333 -- Matching for unboxed data types:
334 -- requires specialisation w.r.t. the unboxed type
335 do (PolySysTyVar _) ty2@(UniData con _) = tv1 `bindToUnboxed` ps_ty2
336 do (UserTyVar _ _) ty2@(UniData con _) = tv1 `bindToUnboxed` ps_ty2
338 -- Matching for function types
339 do (PolySysTyVar _) ty2@(UniFun _ _) = tv1 `bindTo` ps_ty2
340 do (UserTyVar _ _) ty2@(UniFun _ _) = tv1 `bindTo` ps_ty2
343 do _ _ = getSrcLocTc `thenNF_Tc` \ src_loc ->
344 failTc (unifyErr (UnifyMisMatch ty1 ps_ty2) err_ctxt src_loc)
346 ----------- END OF CASES ---------------
351 = extendSubstTc tyvar1 ty2 err_ctxt
353 tyvar1 `bindToUnboxed` ty2
354 = getSwitchCheckerTc `thenNF_Tc` \ sw_chkr ->
355 if sw_chkr SpecialiseUnboxed then
356 extendSubstTc tyvar1 ty2 err_ctxt
358 getSrcLocTc `thenNF_Tc` \ src_loc ->
359 failTc (unifyErr (UnifyMisMatch ty1 ps_ty2) err_ctxt src_loc)