| 1 | % |
|---|
| 2 | % (c) The University of Glasgow 2006 |
|---|
| 3 | % |
|---|
| 4 | |
|---|
| 5 | \begin{code} |
|---|
| 6 | {-# OPTIONS -fno-warn-tabs #-} |
|---|
| 7 | -- The above warning supression flag is a temporary kludge. |
|---|
| 8 | -- While working on this module you are encouraged to remove it and |
|---|
| 9 | -- detab the module (please do the detabbing in a separate patch). See |
|---|
| 10 | -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces |
|---|
| 11 | -- for details |
|---|
| 12 | |
|---|
| 13 | module Unify ( |
|---|
| 14 | -- Matching of types: |
|---|
| 15 | -- the "tc" prefix indicates that matching always |
|---|
| 16 | -- respects newtypes (rather than looking through them) |
|---|
| 17 | tcMatchTy, tcMatchTys, tcMatchTyX, |
|---|
| 18 | ruleMatchTyX, tcMatchPreds, |
|---|
| 19 | |
|---|
| 20 | MatchEnv(..), matchList, |
|---|
| 21 | |
|---|
| 22 | typesCantMatch, |
|---|
| 23 | |
|---|
| 24 | -- Side-effect free unification |
|---|
| 25 | tcUnifyTys, BindFlag(..), |
|---|
| 26 | niFixTvSubst, niSubstTvSet |
|---|
| 27 | |
|---|
| 28 | ) where |
|---|
| 29 | |
|---|
| 30 | #include "HsVersions.h" |
|---|
| 31 | |
|---|
| 32 | import Var |
|---|
| 33 | import VarEnv |
|---|
| 34 | import VarSet |
|---|
| 35 | import Kind |
|---|
| 36 | import Type |
|---|
| 37 | import TyCon |
|---|
| 38 | import TypeRep |
|---|
| 39 | import Outputable |
|---|
| 40 | import ErrUtils |
|---|
| 41 | import Util |
|---|
| 42 | import Maybes |
|---|
| 43 | import FastString |
|---|
| 44 | \end{code} |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | %************************************************************************ |
|---|
| 48 | %* * |
|---|
| 49 | Matching |
|---|
| 50 | %* * |
|---|
| 51 | %************************************************************************ |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | Matching is much tricker than you might think. |
|---|
| 55 | |
|---|
| 56 | 1. The substitution we generate binds the *template type variables* |
|---|
| 57 | which are given to us explicitly. |
|---|
| 58 | |
|---|
| 59 | 2. We want to match in the presence of foralls; |
|---|
| 60 | e.g (forall a. t1) ~ (forall b. t2) |
|---|
| 61 | |
|---|
| 62 | That is what the RnEnv2 is for; it does the alpha-renaming |
|---|
| 63 | that makes it as if a and b were the same variable. |
|---|
| 64 | Initialising the RnEnv2, so that it can generate a fresh |
|---|
| 65 | binder when necessary, entails knowing the free variables of |
|---|
| 66 | both types. |
|---|
| 67 | |
|---|
| 68 | 3. We must be careful not to bind a template type variable to a |
|---|
| 69 | locally bound variable. E.g. |
|---|
| 70 | (forall a. x) ~ (forall b. b) |
|---|
| 71 | where x is the template type variable. Then we do not want to |
|---|
| 72 | bind x to a/b! This is a kind of occurs check. |
|---|
| 73 | The necessary locals accumulate in the RnEnv2. |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | \begin{code} |
|---|
| 77 | data MatchEnv |
|---|
| 78 | = ME { me_tmpls :: VarSet -- Template variables |
|---|
| 79 | , me_env :: RnEnv2 -- Renaming envt for nested foralls |
|---|
| 80 | } -- In-scope set includes template variables |
|---|
| 81 | -- Nota Bene: MatchEnv isn't specific to Types. It is used |
|---|
| 82 | -- for matching terms and coercions as well as types |
|---|
| 83 | |
|---|
| 84 | tcMatchTy :: TyVarSet -- Template tyvars |
|---|
| 85 | -> Type -- Template |
|---|
| 86 | -> Type -- Target |
|---|
| 87 | -> Maybe TvSubst -- One-shot; in principle the template |
|---|
| 88 | -- variables could be free in the target |
|---|
| 89 | |
|---|
| 90 | tcMatchTy tmpls ty1 ty2 |
|---|
| 91 | = case match menv emptyTvSubstEnv ty1 ty2 of |
|---|
| 92 | Just subst_env -> Just (TvSubst in_scope subst_env) |
|---|
| 93 | Nothing -> Nothing |
|---|
| 94 | where |
|---|
| 95 | menv = ME { me_tmpls = tmpls, me_env = mkRnEnv2 in_scope } |
|---|
| 96 | in_scope = mkInScopeSet (tmpls `unionVarSet` tyVarsOfType ty2) |
|---|
| 97 | -- We're assuming that all the interesting |
|---|
| 98 | -- tyvars in tys1 are in tmpls |
|---|
| 99 | |
|---|
| 100 | tcMatchTys :: TyVarSet -- Template tyvars |
|---|
| 101 | -> [Type] -- Template |
|---|
| 102 | -> [Type] -- Target |
|---|
| 103 | -> Maybe TvSubst -- One-shot; in principle the template |
|---|
| 104 | -- variables could be free in the target |
|---|
| 105 | |
|---|
| 106 | tcMatchTys tmpls tys1 tys2 |
|---|
| 107 | = case match_tys menv emptyTvSubstEnv tys1 tys2 of |
|---|
| 108 | Just subst_env -> Just (TvSubst in_scope subst_env) |
|---|
| 109 | Nothing -> Nothing |
|---|
| 110 | where |
|---|
| 111 | menv = ME { me_tmpls = tmpls, me_env = mkRnEnv2 in_scope } |
|---|
| 112 | in_scope = mkInScopeSet (tmpls `unionVarSet` tyVarsOfTypes tys2) |
|---|
| 113 | -- We're assuming that all the interesting |
|---|
| 114 | -- tyvars in tys1 are in tmpls |
|---|
| 115 | |
|---|
| 116 | -- This is similar, but extends a substitution |
|---|
| 117 | tcMatchTyX :: TyVarSet -- Template tyvars |
|---|
| 118 | -> TvSubst -- Substitution to extend |
|---|
| 119 | -> Type -- Template |
|---|
| 120 | -> Type -- Target |
|---|
| 121 | -> Maybe TvSubst |
|---|
| 122 | tcMatchTyX tmpls (TvSubst in_scope subst_env) ty1 ty2 |
|---|
| 123 | = case match menv subst_env ty1 ty2 of |
|---|
| 124 | Just subst_env -> Just (TvSubst in_scope subst_env) |
|---|
| 125 | Nothing -> Nothing |
|---|
| 126 | where |
|---|
| 127 | menv = ME {me_tmpls = tmpls, me_env = mkRnEnv2 in_scope} |
|---|
| 128 | |
|---|
| 129 | tcMatchPreds |
|---|
| 130 | :: [TyVar] -- Bind these |
|---|
| 131 | -> [PredType] -> [PredType] |
|---|
| 132 | -> Maybe TvSubstEnv |
|---|
| 133 | tcMatchPreds tmpls ps1 ps2 |
|---|
| 134 | = matchList (match menv) emptyTvSubstEnv ps1 ps2 |
|---|
| 135 | where |
|---|
| 136 | menv = ME { me_tmpls = mkVarSet tmpls, me_env = mkRnEnv2 in_scope_tyvars } |
|---|
| 137 | in_scope_tyvars = mkInScopeSet (tyVarsOfTypes ps1 `unionVarSet` tyVarsOfTypes ps2) |
|---|
| 138 | |
|---|
| 139 | -- This one is called from the expression matcher, which already has a MatchEnv in hand |
|---|
| 140 | ruleMatchTyX :: MatchEnv |
|---|
| 141 | -> TvSubstEnv -- Substitution to extend |
|---|
| 142 | -> Type -- Template |
|---|
| 143 | -> Type -- Target |
|---|
| 144 | -> Maybe TvSubstEnv |
|---|
| 145 | |
|---|
| 146 | ruleMatchTyX menv subst ty1 ty2 = match menv subst ty1 ty2 -- Rename for export |
|---|
| 147 | \end{code} |
|---|
| 148 | |
|---|
| 149 | Now the internals of matching |
|---|
| 150 | |
|---|
| 151 | \begin{code} |
|---|
| 152 | match :: MatchEnv -- For the most part this is pushed downwards |
|---|
| 153 | -> TvSubstEnv -- Substitution so far: |
|---|
| 154 | -- Domain is subset of template tyvars |
|---|
| 155 | -- Free vars of range is subset of |
|---|
| 156 | -- in-scope set of the RnEnv2 |
|---|
| 157 | -> Type -> Type -- Template and target respectively |
|---|
| 158 | -> Maybe TvSubstEnv |
|---|
| 159 | |
|---|
| 160 | match menv subst ty1 ty2 | Just ty1' <- coreView ty1 = match menv subst ty1' ty2 |
|---|
| 161 | | Just ty2' <- coreView ty2 = match menv subst ty1 ty2' |
|---|
| 162 | |
|---|
| 163 | match menv subst (TyVarTy tv1) ty2 |
|---|
| 164 | | Just ty1' <- lookupVarEnv subst tv1' -- tv1' is already bound |
|---|
| 165 | = if eqTypeX (nukeRnEnvL rn_env) ty1' ty2 |
|---|
| 166 | -- ty1 has no locally-bound variables, hence nukeRnEnvL |
|---|
| 167 | then Just subst |
|---|
| 168 | else Nothing -- ty2 doesn't match |
|---|
| 169 | |
|---|
| 170 | | tv1' `elemVarSet` me_tmpls menv |
|---|
| 171 | = if any (inRnEnvR rn_env) (varSetElems (tyVarsOfType ty2)) |
|---|
| 172 | then Nothing -- Occurs check |
|---|
| 173 | else do { subst1 <- match_kind menv subst (tyVarKind tv1) (typeKind ty2) |
|---|
| 174 | -- Note [Matching kinds] |
|---|
| 175 | ; return (extendVarEnv subst1 tv1' ty2) } |
|---|
| 176 | |
|---|
| 177 | | otherwise -- tv1 is not a template tyvar |
|---|
| 178 | = case ty2 of |
|---|
| 179 | TyVarTy tv2 | tv1' == rnOccR rn_env tv2 -> Just subst |
|---|
| 180 | _ -> Nothing |
|---|
| 181 | where |
|---|
| 182 | rn_env = me_env menv |
|---|
| 183 | tv1' = rnOccL rn_env tv1 |
|---|
| 184 | |
|---|
| 185 | match menv subst (ForAllTy tv1 ty1) (ForAllTy tv2 ty2) |
|---|
| 186 | = do { subst' <- match_kind menv subst (tyVarKind tv1) (tyVarKind tv2) |
|---|
| 187 | ; match menv' subst' ty1 ty2 } |
|---|
| 188 | where -- Use the magic of rnBndr2 to go under the binders |
|---|
| 189 | menv' = menv { me_env = rnBndr2 (me_env menv) tv1 tv2 } |
|---|
| 190 | |
|---|
| 191 | match menv subst (TyConApp tc1 tys1) (TyConApp tc2 tys2) |
|---|
| 192 | | tc1 == tc2 = match_tys menv subst tys1 tys2 |
|---|
| 193 | match menv subst (FunTy ty1a ty1b) (FunTy ty2a ty2b) |
|---|
| 194 | = do { subst' <- match menv subst ty1a ty2a |
|---|
| 195 | ; match menv subst' ty1b ty2b } |
|---|
| 196 | match menv subst (AppTy ty1a ty1b) ty2 |
|---|
| 197 | | Just (ty2a, ty2b) <- repSplitAppTy_maybe ty2 |
|---|
| 198 | -- 'repSplit' used because the tcView stuff is done above |
|---|
| 199 | = do { subst' <- match menv subst ty1a ty2a |
|---|
| 200 | ; match menv subst' ty1b ty2b } |
|---|
| 201 | |
|---|
| 202 | match _ subst (LitTy x) (LitTy y) | x == y = return subst |
|---|
| 203 | |
|---|
| 204 | match _ _ _ _ |
|---|
| 205 | = Nothing |
|---|
| 206 | |
|---|
| 207 | -------------- |
|---|
| 208 | match_kind :: MatchEnv -> TvSubstEnv -> Kind -> Kind -> Maybe TvSubstEnv |
|---|
| 209 | -- Match the kind of the template tyvar with the kind of Type |
|---|
| 210 | -- Note [Matching kinds] |
|---|
| 211 | match_kind menv subst k1 k2 |
|---|
| 212 | | k2 `isSubKind` k1 |
|---|
| 213 | = return subst |
|---|
| 214 | |
|---|
| 215 | | otherwise |
|---|
| 216 | = match menv subst k1 k2 |
|---|
| 217 | |
|---|
| 218 | -- Note [Matching kinds] |
|---|
| 219 | -- ~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 220 | -- For ordinary type variables, we don't want (m a) to match (n b) |
|---|
| 221 | -- if say (a::*) and (b::*->*). This is just a yes/no issue. |
|---|
| 222 | -- |
|---|
| 223 | -- For coercion kinds matters are more complicated. If we have a |
|---|
| 224 | -- coercion template variable co::a~[b], where a,b are presumably also |
|---|
| 225 | -- template type variables, then we must match co's kind against the |
|---|
| 226 | -- kind of the actual argument, so as to give bindings to a,b. |
|---|
| 227 | -- |
|---|
| 228 | -- In fact I have no example in mind that *requires* this kind-matching |
|---|
| 229 | -- to instantiate template type variables, but it seems like the right |
|---|
| 230 | -- thing to do. C.f. Note [Matching variable types] in Rules.lhs |
|---|
| 231 | |
|---|
| 232 | -------------- |
|---|
| 233 | match_tys :: MatchEnv -> TvSubstEnv -> [Type] -> [Type] -> Maybe TvSubstEnv |
|---|
| 234 | match_tys menv subst tys1 tys2 = matchList (match menv) subst tys1 tys2 |
|---|
| 235 | |
|---|
| 236 | -------------- |
|---|
| 237 | matchList :: (env -> a -> b -> Maybe env) |
|---|
| 238 | -> env -> [a] -> [b] -> Maybe env |
|---|
| 239 | matchList _ subst [] [] = Just subst |
|---|
| 240 | matchList fn subst (a:as) (b:bs) = do { subst' <- fn subst a b |
|---|
| 241 | ; matchList fn subst' as bs } |
|---|
| 242 | matchList _ _ _ _ = Nothing |
|---|
| 243 | \end{code} |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | %************************************************************************ |
|---|
| 247 | %* * |
|---|
| 248 | GADTs |
|---|
| 249 | %* * |
|---|
| 250 | %************************************************************************ |
|---|
| 251 | |
|---|
| 252 | Note [Pruning dead case alternatives] |
|---|
| 253 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 254 | Consider data T a where |
|---|
| 255 | T1 :: T Int |
|---|
| 256 | T2 :: T a |
|---|
| 257 | |
|---|
| 258 | newtype X = MkX Int |
|---|
| 259 | newtype Y = MkY Char |
|---|
| 260 | |
|---|
| 261 | type family F a |
|---|
| 262 | type instance F Bool = Int |
|---|
| 263 | |
|---|
| 264 | Now consider case x of { T1 -> e1; T2 -> e2 } |
|---|
| 265 | |
|---|
| 266 | The question before the house is this: if I know something about the type |
|---|
| 267 | of x, can I prune away the T1 alternative? |
|---|
| 268 | |
|---|
| 269 | Suppose x::T Char. It's impossible to construct a (T Char) using T1, |
|---|
| 270 | Answer = YES we can prune the T1 branch (clearly) |
|---|
| 271 | |
|---|
| 272 | Suppose x::T (F a), where 'a' is in scope. Then 'a' might be instantiated |
|---|
| 273 | to 'Bool', in which case x::T Int, so |
|---|
| 274 | ANSWER = NO (clearly) |
|---|
| 275 | |
|---|
| 276 | Suppose x::T X. Then *in Haskell* it's impossible to construct a (non-bottom) |
|---|
| 277 | value of type (T X) using T1. But *in FC* it's quite possible. The newtype |
|---|
| 278 | gives a coercion |
|---|
| 279 | CoX :: X ~ Int |
|---|
| 280 | So (T CoX) :: T X ~ T Int; hence (T1 `cast` sym (T CoX)) is a non-bottom value |
|---|
| 281 | of type (T X) constructed with T1. Hence |
|---|
| 282 | ANSWER = NO we can't prune the T1 branch (surprisingly) |
|---|
| 283 | |
|---|
| 284 | Furthermore, this can even happen; see Trac #1251. GHC's newtype-deriving |
|---|
| 285 | mechanism uses a cast, just as above, to move from one dictionary to another, |
|---|
| 286 | in effect giving the programmer access to CoX. |
|---|
| 287 | |
|---|
| 288 | Finally, suppose x::T Y. Then *even in FC* we can't construct a |
|---|
| 289 | non-bottom value of type (T Y) using T1. That's because we can get |
|---|
| 290 | from Y to Char, but not to Int. |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | Here's a related question. data Eq a b where EQ :: Eq a a |
|---|
| 294 | Consider |
|---|
| 295 | case x of { EQ -> ... } |
|---|
| 296 | |
|---|
| 297 | Suppose x::Eq Int Char. Is the alternative dead? Clearly yes. |
|---|
| 298 | |
|---|
| 299 | What about x::Eq Int a, in a context where we have evidence that a~Char. |
|---|
| 300 | Then again the alternative is dead. |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | Summary |
|---|
| 304 | |
|---|
| 305 | We are really doing a test for unsatisfiability of the type |
|---|
| 306 | constraints implied by the match. And that is clearly, in general, a |
|---|
| 307 | hard thing to do. |
|---|
| 308 | |
|---|
| 309 | However, since we are simply dropping dead code, a conservative test |
|---|
| 310 | suffices. There is a continuum of tests, ranging from easy to hard, that |
|---|
| 311 | drop more and more dead code. |
|---|
| 312 | |
|---|
| 313 | For now we implement a very simple test: type variables match |
|---|
| 314 | anything, type functions (incl newtypes) match anything, and only |
|---|
| 315 | distinct data types fail to match. We can elaborate later. |
|---|
| 316 | |
|---|
| 317 | \begin{code} |
|---|
| 318 | typesCantMatch :: [(Type,Type)] -> Bool |
|---|
| 319 | typesCantMatch prs = any (\(s,t) -> cant_match s t) prs |
|---|
| 320 | where |
|---|
| 321 | cant_match :: Type -> Type -> Bool |
|---|
| 322 | cant_match t1 t2 |
|---|
| 323 | | Just t1' <- coreView t1 = cant_match t1' t2 |
|---|
| 324 | | Just t2' <- coreView t2 = cant_match t1 t2' |
|---|
| 325 | |
|---|
| 326 | cant_match (FunTy a1 r1) (FunTy a2 r2) |
|---|
| 327 | = cant_match a1 a2 || cant_match r1 r2 |
|---|
| 328 | |
|---|
| 329 | cant_match (TyConApp tc1 tys1) (TyConApp tc2 tys2) |
|---|
| 330 | | isDistinctTyCon tc1 && isDistinctTyCon tc2 |
|---|
| 331 | = tc1 /= tc2 || typesCantMatch (zipEqual "typesCantMatch" tys1 tys2) |
|---|
| 332 | |
|---|
| 333 | cant_match (FunTy {}) (TyConApp tc _) = isDistinctTyCon tc |
|---|
| 334 | cant_match (TyConApp tc _) (FunTy {}) = isDistinctTyCon tc |
|---|
| 335 | -- tc can't be FunTyCon by invariant |
|---|
| 336 | |
|---|
| 337 | cant_match (AppTy f1 a1) ty2 |
|---|
| 338 | | Just (f2, a2) <- repSplitAppTy_maybe ty2 |
|---|
| 339 | = cant_match f1 f2 || cant_match a1 a2 |
|---|
| 340 | cant_match ty1 (AppTy f2 a2) |
|---|
| 341 | | Just (f1, a1) <- repSplitAppTy_maybe ty1 |
|---|
| 342 | = cant_match f1 f2 || cant_match a1 a2 |
|---|
| 343 | |
|---|
| 344 | cant_match (LitTy x) (LitTy y) = x /= y |
|---|
| 345 | |
|---|
| 346 | cant_match _ _ = False -- Safe! |
|---|
| 347 | |
|---|
| 348 | -- Things we could add; |
|---|
| 349 | -- foralls |
|---|
| 350 | -- look through newtypes |
|---|
| 351 | -- take account of tyvar bindings (EQ example above) |
|---|
| 352 | \end{code} |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | %************************************************************************ |
|---|
| 356 | %* * |
|---|
| 357 | Unification |
|---|
| 358 | %* * |
|---|
| 359 | %************************************************************************ |
|---|
| 360 | |
|---|
| 361 | \begin{code} |
|---|
| 362 | tcUnifyTys :: (TyVar -> BindFlag) |
|---|
| 363 | -> [Type] -> [Type] |
|---|
| 364 | -> Maybe TvSubst -- A regular one-shot (idempotent) substitution |
|---|
| 365 | -- The two types may have common type variables, and indeed do so in the |
|---|
| 366 | -- second call to tcUnifyTys in FunDeps.checkClsFD |
|---|
| 367 | -- |
|---|
| 368 | tcUnifyTys bind_fn tys1 tys2 |
|---|
| 369 | = maybeErrToMaybe $ initUM bind_fn $ |
|---|
| 370 | do { subst <- unifyList emptyTvSubstEnv tys1 tys2 |
|---|
| 371 | |
|---|
| 372 | -- Find the fixed point of the resulting non-idempotent substitution |
|---|
| 373 | ; return (niFixTvSubst subst) } |
|---|
| 374 | \end{code} |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | %************************************************************************ |
|---|
| 378 | %* * |
|---|
| 379 | Non-idempotent substitution |
|---|
| 380 | %* * |
|---|
| 381 | %************************************************************************ |
|---|
| 382 | |
|---|
| 383 | Note [Non-idempotent substitution] |
|---|
| 384 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 385 | During unification we use a TvSubstEnv that is |
|---|
| 386 | (a) non-idempotent |
|---|
| 387 | (b) loop-free; ie repeatedly applying it yields a fixed point |
|---|
| 388 | |
|---|
| 389 | \begin{code} |
|---|
| 390 | niFixTvSubst :: TvSubstEnv -> TvSubst |
|---|
| 391 | -- Find the idempotent fixed point of the non-idempotent substitution |
|---|
| 392 | -- ToDo: use laziness instead of iteration? |
|---|
| 393 | niFixTvSubst env = f env |
|---|
| 394 | where |
|---|
| 395 | f e | not_fixpoint = f (mapVarEnv (substTy subst) e) |
|---|
| 396 | | otherwise = subst |
|---|
| 397 | where |
|---|
| 398 | range_tvs = foldVarEnv (unionVarSet . tyVarsOfType) emptyVarSet e |
|---|
| 399 | subst = mkTvSubst (mkInScopeSet range_tvs) e |
|---|
| 400 | not_fixpoint = foldVarSet ((||) . in_domain) False range_tvs |
|---|
| 401 | in_domain tv = tv `elemVarEnv` e |
|---|
| 402 | |
|---|
| 403 | niSubstTvSet :: TvSubstEnv -> TyVarSet -> TyVarSet |
|---|
| 404 | -- Apply the non-idempotent substitution to a set of type variables, |
|---|
| 405 | -- remembering that the substitution isn't necessarily idempotent |
|---|
| 406 | -- This is used in the occurs check, before extending the substitution |
|---|
| 407 | niSubstTvSet subst tvs |
|---|
| 408 | = foldVarSet (unionVarSet . get) emptyVarSet tvs |
|---|
| 409 | where |
|---|
| 410 | get tv = case lookupVarEnv subst tv of |
|---|
| 411 | Nothing -> unitVarSet tv |
|---|
| 412 | Just ty -> niSubstTvSet subst (tyVarsOfType ty) |
|---|
| 413 | \end{code} |
|---|
| 414 | |
|---|
| 415 | %************************************************************************ |
|---|
| 416 | %* * |
|---|
| 417 | The workhorse |
|---|
| 418 | %* * |
|---|
| 419 | %************************************************************************ |
|---|
| 420 | |
|---|
| 421 | \begin{code} |
|---|
| 422 | unify :: TvSubstEnv -- An existing substitution to extend |
|---|
| 423 | -> Type -> Type -- Types to be unified, and witness of their equality |
|---|
| 424 | -> UM TvSubstEnv -- Just the extended substitution, |
|---|
| 425 | -- Nothing if unification failed |
|---|
| 426 | -- We do not require the incoming substitution to be idempotent, |
|---|
| 427 | -- nor guarantee that the outgoing one is. That's fixed up by |
|---|
| 428 | -- the wrappers. |
|---|
| 429 | |
|---|
| 430 | -- Respects newtypes, PredTypes |
|---|
| 431 | |
|---|
| 432 | -- in unify, any NewTcApps/Preds should be taken at face value |
|---|
| 433 | unify subst (TyVarTy tv1) ty2 = uVar subst tv1 ty2 |
|---|
| 434 | unify subst ty1 (TyVarTy tv2) = uVar subst tv2 ty1 |
|---|
| 435 | |
|---|
| 436 | unify subst ty1 ty2 | Just ty1' <- tcView ty1 = unify subst ty1' ty2 |
|---|
| 437 | unify subst ty1 ty2 | Just ty2' <- tcView ty2 = unify subst ty1 ty2' |
|---|
| 438 | |
|---|
| 439 | unify subst (TyConApp tyc1 tys1) (TyConApp tyc2 tys2) |
|---|
| 440 | | tyc1 == tyc2 = unify_tys subst tys1 tys2 |
|---|
| 441 | |
|---|
| 442 | unify subst (FunTy ty1a ty1b) (FunTy ty2a ty2b) |
|---|
| 443 | = do { subst' <- unify subst ty1a ty2a |
|---|
| 444 | ; unify subst' ty1b ty2b } |
|---|
| 445 | |
|---|
| 446 | -- Applications need a bit of care! |
|---|
| 447 | -- They can match FunTy and TyConApp, so use splitAppTy_maybe |
|---|
| 448 | -- NB: we've already dealt with type variables and Notes, |
|---|
| 449 | -- so if one type is an App the other one jolly well better be too |
|---|
| 450 | unify subst (AppTy ty1a ty1b) ty2 |
|---|
| 451 | | Just (ty2a, ty2b) <- repSplitAppTy_maybe ty2 |
|---|
| 452 | = do { subst' <- unify subst ty1a ty2a |
|---|
| 453 | ; unify subst' ty1b ty2b } |
|---|
| 454 | |
|---|
| 455 | unify subst ty1 (AppTy ty2a ty2b) |
|---|
| 456 | | Just (ty1a, ty1b) <- repSplitAppTy_maybe ty1 |
|---|
| 457 | = do { subst' <- unify subst ty1a ty2a |
|---|
| 458 | ; unify subst' ty1b ty2b } |
|---|
| 459 | |
|---|
| 460 | unify subst (LitTy x) (LitTy y) | x == y = return subst |
|---|
| 461 | |
|---|
| 462 | unify _ ty1 ty2 = failWith (misMatch ty1 ty2) |
|---|
| 463 | -- ForAlls?? |
|---|
| 464 | |
|---|
| 465 | ------------------------------ |
|---|
| 466 | unify_tys :: TvSubstEnv -> [Type] -> [Type] -> UM TvSubstEnv |
|---|
| 467 | unify_tys subst xs ys = unifyList subst xs ys |
|---|
| 468 | |
|---|
| 469 | unifyList :: TvSubstEnv -> [Type] -> [Type] -> UM TvSubstEnv |
|---|
| 470 | unifyList subst orig_xs orig_ys |
|---|
| 471 | = go subst orig_xs orig_ys |
|---|
| 472 | where |
|---|
| 473 | go subst [] [] = return subst |
|---|
| 474 | go subst (x:xs) (y:ys) = do { subst' <- unify subst x y |
|---|
| 475 | ; go subst' xs ys } |
|---|
| 476 | go _ _ _ = failWith (lengthMisMatch orig_xs orig_ys) |
|---|
| 477 | |
|---|
| 478 | --------------------------------- |
|---|
| 479 | uVar :: TvSubstEnv -- An existing substitution to extend |
|---|
| 480 | -> TyVar -- Type variable to be unified |
|---|
| 481 | -> Type -- with this type |
|---|
| 482 | -> UM TvSubstEnv |
|---|
| 483 | |
|---|
| 484 | -- PRE-CONDITION: in the call (uVar swap r tv1 ty), we know that |
|---|
| 485 | -- if swap=False (tv1~ty) |
|---|
| 486 | -- if swap=True (ty~tv1) |
|---|
| 487 | |
|---|
| 488 | uVar subst tv1 ty |
|---|
| 489 | = -- Check to see whether tv1 is refined by the substitution |
|---|
| 490 | case (lookupVarEnv subst tv1) of |
|---|
| 491 | Just ty' -> unify subst ty' ty -- Yes, call back into unify' |
|---|
| 492 | Nothing -> uUnrefined subst -- No, continue |
|---|
| 493 | tv1 ty ty |
|---|
| 494 | |
|---|
| 495 | uUnrefined :: TvSubstEnv -- An existing substitution to extend |
|---|
| 496 | -> TyVar -- Type variable to be unified |
|---|
| 497 | -> Type -- with this type |
|---|
| 498 | -> Type -- (version w/ expanded synonyms) |
|---|
| 499 | -> UM TvSubstEnv |
|---|
| 500 | |
|---|
| 501 | -- We know that tv1 isn't refined |
|---|
| 502 | |
|---|
| 503 | uUnrefined subst tv1 ty2 ty2' |
|---|
| 504 | | Just ty2'' <- tcView ty2' |
|---|
| 505 | = uUnrefined subst tv1 ty2 ty2'' -- Unwrap synonyms |
|---|
| 506 | -- This is essential, in case we have |
|---|
| 507 | -- type Foo a = a |
|---|
| 508 | -- and then unify a ~ Foo a |
|---|
| 509 | |
|---|
| 510 | uUnrefined subst tv1 ty2 (TyVarTy tv2) |
|---|
| 511 | | tv1 == tv2 -- Same type variable |
|---|
| 512 | = return subst |
|---|
| 513 | |
|---|
| 514 | -- Check to see whether tv2 is refined |
|---|
| 515 | | Just ty' <- lookupVarEnv subst tv2 |
|---|
| 516 | = uUnrefined subst tv1 ty' ty' |
|---|
| 517 | |
|---|
| 518 | | otherwise |
|---|
| 519 | |
|---|
| 520 | = do { -- So both are unrefined; unify the kinds |
|---|
| 521 | ; subst' <- unify subst (tyVarKind tv1) (tyVarKind tv2) |
|---|
| 522 | |
|---|
| 523 | -- And then bind one or the other, |
|---|
| 524 | -- depending on which is bindable |
|---|
| 525 | -- NB: unlike TcUnify we do not have an elaborate sub-kinding |
|---|
| 526 | -- story. That is relevant only during type inference, and |
|---|
| 527 | -- (I very much hope) is not relevant here. |
|---|
| 528 | ; b1 <- tvBindFlag tv1 |
|---|
| 529 | ; b2 <- tvBindFlag tv2 |
|---|
| 530 | ; let ty1 = TyVarTy tv1 |
|---|
| 531 | ; case (b1, b2) of |
|---|
| 532 | (Skolem, Skolem) -> failWith (misMatch ty1 ty2) |
|---|
| 533 | (BindMe, _) -> return (extendVarEnv subst' tv1 ty2) |
|---|
| 534 | (_, BindMe) -> return (extendVarEnv subst' tv2 ty1) } |
|---|
| 535 | |
|---|
| 536 | uUnrefined subst tv1 ty2 ty2' -- ty2 is not a type variable |
|---|
| 537 | | tv1 `elemVarSet` niSubstTvSet subst (tyVarsOfType ty2') |
|---|
| 538 | = failWith (occursCheck tv1 ty2) -- Occurs check |
|---|
| 539 | | otherwise |
|---|
| 540 | = do { subst' <- unify subst k1 k2 |
|---|
| 541 | ; bindTv subst' tv1 ty2 } -- Bind tyvar to the synonym if poss |
|---|
| 542 | where |
|---|
| 543 | k1 = tyVarKind tv1 |
|---|
| 544 | k2 = typeKind ty2' |
|---|
| 545 | |
|---|
| 546 | bindTv :: TvSubstEnv -> TyVar -> Type -> UM TvSubstEnv |
|---|
| 547 | bindTv subst tv ty -- ty is not a type variable |
|---|
| 548 | = do { b <- tvBindFlag tv |
|---|
| 549 | ; case b of |
|---|
| 550 | Skolem -> failWith (misMatch (TyVarTy tv) ty) |
|---|
| 551 | BindMe -> return $ extendVarEnv subst tv ty |
|---|
| 552 | } |
|---|
| 553 | \end{code} |
|---|
| 554 | |
|---|
| 555 | %************************************************************************ |
|---|
| 556 | %* * |
|---|
| 557 | Binding decisions |
|---|
| 558 | %* * |
|---|
| 559 | %************************************************************************ |
|---|
| 560 | |
|---|
| 561 | \begin{code} |
|---|
| 562 | data BindFlag |
|---|
| 563 | = BindMe -- A regular type variable |
|---|
| 564 | |
|---|
| 565 | | Skolem -- This type variable is a skolem constant |
|---|
| 566 | -- Don't bind it; it only matches itself |
|---|
| 567 | \end{code} |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | %************************************************************************ |
|---|
| 571 | %* * |
|---|
| 572 | Unification monad |
|---|
| 573 | %* * |
|---|
| 574 | %************************************************************************ |
|---|
| 575 | |
|---|
| 576 | \begin{code} |
|---|
| 577 | newtype UM a = UM { unUM :: (TyVar -> BindFlag) |
|---|
| 578 | -> MaybeErr MsgDoc a } |
|---|
| 579 | |
|---|
| 580 | instance Monad UM where |
|---|
| 581 | return a = UM (\_tvs -> Succeeded a) |
|---|
| 582 | fail s = UM (\_tvs -> Failed (text s)) |
|---|
| 583 | m >>= k = UM (\tvs -> case unUM m tvs of |
|---|
| 584 | Failed err -> Failed err |
|---|
| 585 | Succeeded v -> unUM (k v) tvs) |
|---|
| 586 | |
|---|
| 587 | initUM :: (TyVar -> BindFlag) -> UM a -> MaybeErr MsgDoc a |
|---|
| 588 | initUM badtvs um = unUM um badtvs |
|---|
| 589 | |
|---|
| 590 | tvBindFlag :: TyVar -> UM BindFlag |
|---|
| 591 | tvBindFlag tv = UM (\tv_fn -> Succeeded (tv_fn tv)) |
|---|
| 592 | |
|---|
| 593 | failWith :: MsgDoc -> UM a |
|---|
| 594 | failWith msg = UM (\_tv_fn -> Failed msg) |
|---|
| 595 | |
|---|
| 596 | maybeErrToMaybe :: MaybeErr fail succ -> Maybe succ |
|---|
| 597 | maybeErrToMaybe (Succeeded a) = Just a |
|---|
| 598 | maybeErrToMaybe (Failed _) = Nothing |
|---|
| 599 | \end{code} |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | %************************************************************************ |
|---|
| 603 | %* * |
|---|
| 604 | Error reporting |
|---|
| 605 | We go to a lot more trouble to tidy the types |
|---|
| 606 | in TcUnify. Maybe we'll end up having to do that |
|---|
| 607 | here too, but I'll leave it for now. |
|---|
| 608 | %* * |
|---|
| 609 | %************************************************************************ |
|---|
| 610 | |
|---|
| 611 | \begin{code} |
|---|
| 612 | misMatch :: Type -> Type -> SDoc |
|---|
| 613 | misMatch t1 t2 |
|---|
| 614 | = ptext (sLit "Can't match types") <+> quotes (ppr t1) <+> |
|---|
| 615 | ptext (sLit "and") <+> quotes (ppr t2) |
|---|
| 616 | |
|---|
| 617 | lengthMisMatch :: [Type] -> [Type] -> SDoc |
|---|
| 618 | lengthMisMatch tys1 tys2 |
|---|
| 619 | = sep [ptext (sLit "Can't match unequal length lists"), |
|---|
| 620 | nest 2 (ppr tys1), nest 2 (ppr tys2) ] |
|---|
| 621 | |
|---|
| 622 | occursCheck :: TyVar -> Type -> SDoc |
|---|
| 623 | occursCheck tv ty |
|---|
| 624 | = hang (ptext (sLit "Can't construct the infinite type")) |
|---|
| 625 | 2 (ppr tv <+> equals <+> ppr ty) |
|---|
| 626 | \end{code} |
|---|