X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Ftypes%2FUnify.lhs;h=d5d6d1dc932d0e94cbd53ccba79c465899604ba4;hb=cdea99491a8dedfc53fc2e8c4c8fbaf209802b27;hp=a8b893c9250b9870a7dcf74a59b35c4c67659b22;hpb=20e39e0e07e4a8e9395894b2785d6675e4e3e3b3;p=ghc-hetmet.git diff --git a/ghc/compiler/types/Unify.lhs b/ghc/compiler/types/Unify.lhs index a8b893c..d5d6d1d 100644 --- a/ghc/compiler/types/Unify.lhs +++ b/ghc/compiler/types/Unify.lhs @@ -1,11 +1,13 @@ \begin{code} module Unify ( -- Matching and unification - tcMatchTys, tcMatchTyX, tcMatchPreds, MatchEnv(..), + tcMatchTys, tcMatchTyX, ruleMatchTyX, tcMatchPreds, MatchEnv(..), - tcUnifyTys, tcUnifyTysX, + tcUnifyTys, - gadtRefineTys, gadtMatchTys, coreRefineTys, + gadtRefineTys, BindFlag(..), + + coreRefineTys, TypeRefinement, -- Re-export MaybeErr(..) @@ -17,9 +19,11 @@ import Var ( Var, TyVar, tyVarKind ) import VarEnv import VarSet import Kind ( isSubKind ) -import Type ( typeKind, tyVarsOfType, tyVarsOfTypes, tyVarsOfTheta, - TvSubstEnv, TvSubst(..), substTy, tcEqTypeX ) +import Type ( typeKind, tyVarsOfType, tyVarsOfTypes, tyVarsOfTheta, mkTyVarTys, + TvSubstEnv, emptyTvSubstEnv, TvSubst(..), substTy, tcEqTypeX, + tcView ) import TypeRep ( Type(..), PredType(..), funTyCon ) +import DataCon ( DataCon, dataConInstResTy ) import Util ( snocView ) import ErrUtils ( Message ) import Outputable @@ -65,18 +69,32 @@ data MatchEnv tcMatchTys :: TyVarSet -- Template tyvars -> [Type] -- Template -> [Type] -- Target - -> Maybe TvSubstEnv -- One-shot; in principle the template + -> Maybe TvSubst -- One-shot; in principle the template -- variables could be free in the target tcMatchTys tmpls tys1 tys2 - = match_tys (ME { me_tmpls = tmpls, me_env = mkRnEnv2 in_scope_tyvars}) - emptyTvSubstEnv - tys1 tys2 + = case match_tys menv emptyTvSubstEnv tys1 tys2 of + Just subst_env -> Just (TvSubst in_scope subst_env) + Nothing -> Nothing where - in_scope_tyvars = mkInScopeSet (tmpls `unionVarSet` tyVarsOfTypes tys2) + menv = ME { me_tmpls = tmpls, me_env = mkRnEnv2 in_scope } + in_scope = mkInScopeSet (tmpls `unionVarSet` tyVarsOfTypes tys2) -- We're assuming that all the interesting -- tyvars in tys1 are in tmpls +-- This is similar, but extends a substitution +tcMatchTyX :: TyVarSet -- Template tyvars + -> TvSubst -- Substitution to extend + -> Type -- Template + -> Type -- Target + -> Maybe TvSubst +tcMatchTyX tmpls (TvSubst in_scope subst_env) ty1 ty2 + = case match menv subst_env ty1 ty2 of + Just subst_env -> Just (TvSubst in_scope subst_env) + Nothing -> Nothing + where + menv = ME {me_tmpls = tmpls, me_env = mkRnEnv2 in_scope} + tcMatchPreds :: [TyVar] -- Bind these -> [PredType] -> [PredType] @@ -87,13 +105,14 @@ tcMatchPreds tmpls ps1 ps2 menv = ME { me_tmpls = mkVarSet tmpls, me_env = mkRnEnv2 in_scope_tyvars } in_scope_tyvars = mkInScopeSet (tyVarsOfTheta ps1 `unionVarSet` tyVarsOfTheta ps2) -tcMatchTyX :: MatchEnv +-- This one is called from the expression matcher, which already has a MatchEnv in hand +ruleMatchTyX :: MatchEnv -> TvSubstEnv -- Substitution to extend -> Type -- Template -> Type -- Target -> Maybe TvSubstEnv -tcMatchTyX menv subst ty1 ty2 = match menv subst ty1 ty2 -- Rename for export +ruleMatchTyX menv subst ty1 ty2 = match menv subst ty1 ty2 -- Rename for export \end{code} Now the internals of matching @@ -109,8 +128,8 @@ match :: MatchEnv -- For the most part this is pushed downwards -- This matcher works on source types; that is, -- it respects NewTypes and PredType -match menv subst (NoteTy _ ty1) ty2 = match menv subst ty1 ty2 -match menv subst ty1 (NoteTy _ ty2) = match menv subst ty1 ty2 +match menv subst ty1 ty2 | Just ty1' <- tcView ty1 = match menv subst ty1' ty2 +match menv subst ty1 ty2 | Just ty2' <- tcView ty2 = match menv subst ty1 ty2' match menv subst (TyVarTy tv1) ty2 | tv1 `elemVarSet` me_tmpls menv @@ -179,66 +198,76 @@ match_pred menv subst p1 p2 = Nothing %************************************************************************ %* * - The workhorse + Unification %* * %************************************************************************ \begin{code} -gadtRefineTys, gadtMatchTys - :: [TyVar] -- Try to unify these - -> TvSubstEnv -- Not idempotent - -> [Type] -> [Type] - -> MaybeErr Message TvSubstEnv -- Not idempotent --- This one is used by the type checker. Neither the input nor result --- substitition is idempotent -gadtRefineTys ex_tvs subst tys1 tys2 - = initUM (tryToBind (mkVarSet ex_tvs)) (unify_tys subst tys1 tys2) - -gadtMatchTys ex_tvs subst tys1 tys2 - = initUM (bindOnly (mkVarSet ex_tvs)) (unify_tys subst tys1 tys2) +tcUnifyTys :: (TyVar -> BindFlag) + -> [Type] -> [Type] + -> Maybe TvSubst -- A regular one-shot substitution +-- The two types may have common type variables, and indeed do so in the +-- second call to tcUnifyTys in FunDeps.checkClsFD +tcUnifyTys bind_fn tys1 tys2 + = maybeErrToMaybe $ initUM bind_fn $ + do { subst_env <- unify_tys emptyTvSubstEnv tys1 tys2 + + -- Find the fixed point of the resulting non-idempotent substitution + ; let in_scope = mkInScopeSet (tvs1 `unionVarSet` tvs2) + subst = TvSubst in_scope subst_env_fixpt + subst_env_fixpt = mapVarEnv (substTy subst) subst_env + ; return subst } + where + tvs1 = tyVarsOfTypes tys1 + tvs2 = tyVarsOfTypes tys2 ---------------------------- -coreRefineTys :: [TyVar] -- Try to unify these - -> TvSubst -- A full-blown apply-once substitition - -> Type -- Both types should be a fixed point - -> Type -- of the incoming substitution - -> Maybe TvSubstEnv -- In-scope set is unaffected --- Used by Core Lint and the simplifier. Takes a full apply-once substitution. --- The incoming substitution's in-scope set should mention all the variables free --- in the incoming types -coreRefineTys ex_tvs subst@(TvSubst in_scope orig_env) ty1 ty2 - = maybeErrToMaybe $ initUM (tryToBind (mkVarSet ex_tvs)) $ +coreRefineTys :: InScopeSet -- Superset of free vars of either type + -> DataCon -> [TyVar] -- Case pattern (con tv1 .. tvn ...) + -> Type -- Type of scrutinee + -> Maybe TypeRefinement + +type TypeRefinement = (TvSubstEnv, Bool) + -- The Bool is True iff all the bindings in the + -- env are for the pattern type variables + -- In this case, there is no type refinement + -- for already-in-scope type variables + +-- Used by Core Lint and the simplifier. +coreRefineTys in_scope con tvs scrut_ty + = maybeErrToMaybe $ initUM (tryToBind tv_set) $ do { -- Run the unifier, starting with an empty env - ; extra_env <- unify emptyTvSubstEnv ty1 ty2 + ; subst_env <- unify emptyTvSubstEnv pat_res_ty scrut_ty - -- Find the fixed point of the resulting non-idempotent - -- substitution, and apply it to the incoming substitution - ; let extra_subst = TvSubst in_scope extra_env_fixpt - extra_env_fixpt = mapVarEnv (substTy extra_subst) extra_env - orig_env' = mapVarEnv (substTy extra_subst) orig_env - ; return (orig_env' `plusVarEnv` extra_env_fixpt) } + -- Find the fixed point of the resulting non-idempotent substitution + ; let subst = TvSubst in_scope subst_env_fixpt + subst_env_fixpt = mapVarEnv (substTy subst) subst_env + + ; return (subst_env_fixpt, all_bound_here subst_env) } + where + pat_res_ty = dataConInstResTy con (mkTyVarTys tvs) ----------------------------- -tcUnifyTys :: TyVarSet -> [Type] -> [Type] -> Maybe TvSubstEnv -tcUnifyTys bind_these tys1 tys2 - = maybeErrToMaybe $ initUM (bindOnly bind_these) $ - unify_tys emptyTvSubstEnv tys1 tys2 + -- 'tvs' are the tyvars bound by the pattern + tv_set = mkVarSet tvs + all_bound_here env = all bound_here (varEnvKeys env) + bound_here uniq = elemVarSetByKey uniq tv_set + -tcUnifyTysX :: TyVarSet -> TvSubstEnv -> [Type] -> [Type] -> Maybe TvSubstEnv -tcUnifyTysX bind_these subst tys1 tys2 - = maybeErrToMaybe $ initUM (bindOnly bind_these) $ - unify_tys subst tys1 tys2 +---------------------------- +gadtRefineTys + :: (TyVar -> BindFlag) -- Try to unify these + -> TvSubstEnv -- Not idempotent + -> [Type] -> [Type] + -> MaybeErr Message TvSubstEnv -- Not idempotent +-- This one is used by the type checker. Neither the input nor result +-- substitition is idempotent +gadtRefineTys bind_fn subst tys1 tys2 + = initUM bind_fn (unify_tys subst tys1 tys2) ---------------------------- -tryToBind, bindOnly :: TyVarSet -> TyVar -> BindFlag +tryToBind :: TyVarSet -> TyVar -> BindFlag tryToBind tv_set tv | tv `elemVarSet` tv_set = BindMe | otherwise = AvoidMe - -bindOnly tv_set tv | tv `elemVarSet` tv_set = BindMe - | otherwise = DontBindMe - -emptyTvSubstEnv :: TvSubstEnv -emptyTvSubstEnv = emptyVarEnv \end{code} @@ -266,8 +295,8 @@ unify subst ty1 ty2 = -- pprTrace "unify" (ppr subst <+> pprParendType ty1 <+> p unify_ subst (TyVarTy tv1) ty2 = uVar False subst tv1 ty2 unify_ subst ty1 (TyVarTy tv2) = uVar True subst tv2 ty1 -unify_ subst (NoteTy _ ty1) ty2 = unify subst ty1 ty2 -unify_ subst ty1 (NoteTy _ ty2) = unify subst ty1 ty2 +unify_ subst ty1 ty2 | Just ty1' <- tcView ty1 = unify subst ty1' ty2 +unify_ subst ty1 ty2 | Just ty2' <- tcView ty2 = unify subst ty1 ty2' unify_ subst (PredTy p1) (PredTy p2) = unify_pred subst p1 p2 @@ -323,79 +352,77 @@ uVar :: Bool -- Swapped -> UM TvSubstEnv uVar swap subst tv1 ty - = -- check to see whether tv1 is refined + = -- Check to see whether tv1 is refined by the substitution case (lookupVarEnv subst tv1) of - -- yes, call back into unify' + -- Yes, call back into unify' Just ty' | swap -> unify subst ty ty' | otherwise -> unify subst ty' ty -- No, continue - Nothing -> uUnrefined subst tv1 ty + Nothing -> uUnrefined subst tv1 ty ty uUnrefined :: TvSubstEnv -- An existing substitution to extend -> TyVar -- Type variable to be unified -> Type -- with this type + -> Type -- (de-noted version) -> UM TvSubstEnv -- We know that tv1 isn't refined -uUnrefined subst tv1 ty2@(TyVarTy tv2) - | tv1 == tv2 -- Same, do nothing + +uUnrefined subst tv1 ty2 ty2' + | Just ty2'' <- tcView ty2' + = uUnrefined subst tv1 ty2 ty2'' -- Unwrap synonyms + -- This is essential, in case we have + -- type Foo a = a + -- and then unify a :=: Foo a + +uUnrefined subst tv1 ty2 (TyVarTy tv2) + | tv1 == tv2 -- Same type variable = return subst -- Check to see whether tv2 is refined | Just ty' <- lookupVarEnv subst tv2 - = uUnrefined subst tv1 ty' + = uUnrefined subst tv1 ty' ty' -- So both are unrefined; next, see if the kinds force the direction | k1 == k2 -- Can update either; so check the bind-flags = do { b1 <- tvBindFlag tv1 ; b2 <- tvBindFlag tv2 ; case (b1,b2) of - (DontBindMe, DontBindMe) -> failWith (misMatch ty1 ty2) - (DontBindMe, _) -> bindTv subst tv2 ty1 - (BindMe, _) -> bindTv subst tv1 ty2 - (AvoidMe, BindMe) -> bindTv subst tv2 ty1 - (AvoidMe, _) -> bindTv subst tv1 ty2 - } + (BindMe, _) -> bind tv1 ty2 - | k1 `isSubKind` k2 -- Must update tv2 - = do { b2 <- tvBindFlag tv2 - ; case b2 of - DontBindMe -> failWith (misMatch ty1 ty2) - other -> bindTv subst tv2 ty1 - } + (AvoidMe, BindMe) -> bind tv2 ty1 + (AvoidMe, _) -> bind tv1 ty2 - | k2 `isSubKind` k1 -- Must update tv1 - = do { b1 <- tvBindFlag tv1 - ; case b1 of - DontBindMe -> failWith (misMatch ty1 ty2) - other -> bindTv subst tv1 ty2 + (WildCard, WildCard) -> return subst + (WildCard, Skolem) -> return subst + (WildCard, _) -> bind tv2 ty1 + + (Skolem, WildCard) -> return subst + (Skolem, Skolem) -> failWith (misMatch ty1 ty2) + (Skolem, _) -> bind tv2 ty1 } + | k1 `isSubKind` k2 = bindTv subst tv2 ty1 -- Must update tv2 + | k2 `isSubKind` k1 = bindTv subst tv1 ty2 -- Must update tv1 + | otherwise = failWith (kindMisMatch tv1 ty2) where ty1 = TyVarTy tv1 k1 = tyVarKind tv1 k2 = tyVarKind tv2 + bind tv ty = return (extendVarEnv subst tv ty) -uUnrefined subst tv1 ty2 -- ty2 is not a type variable - -- Do occurs check... - | tv1 `elemVarSet` substTvSet subst (tyVarsOfType ty2) - = failWith (occursCheck tv1 ty2) - -- And a kind check... - | k2 `isSubKind` k1 - = do { b1 <- tvBindFlag tv1 - ; case b1 of -- And check that tv1 is bindable - DontBindMe -> failWith (misMatch ty1 ty2) - other -> bindTv subst tv1 ty2 - } +uUnrefined subst tv1 ty2 ty2' -- ty2 is not a type variable + | tv1 `elemVarSet` substTvSet subst (tyVarsOfType ty2') + = failWith (occursCheck tv1 ty2) -- Occurs check + | not (k2 `isSubKind` k1) + = failWith (kindMisMatch tv1 ty2) -- Kind check | otherwise - = pprTrace "kind" (ppr tv1 <+> ppr k1 $$ ppr ty2 <+> ppr k2) $ - failWith (kindMisMatch tv1 ty2) + = bindTv subst tv1 ty2 -- Bind tyvar to the synonym if poss where - ty1 = TyVarTy tv1 k1 = tyVarKind tv1 - k2 = typeKind ty2 + k2 = typeKind ty2' substTvSet :: TvSubstEnv -> TyVarSet -> TyVarSet -- Apply the non-idempotent substitution to a set of type variables, @@ -407,7 +434,13 @@ substTvSet subst tvs Nothing -> unitVarSet tv Just ty -> substTvSet subst (tyVarsOfType ty) -bindTv subst tv ty = return (extendVarEnv subst tv ty) +bindTv subst tv ty -- ty is not a type variable + = do { b <- tvBindFlag tv + ; case b of + Skolem -> failWith (misMatch (TyVarTy tv) ty) + WildCard -> return subst + other -> return (extendVarEnv subst tv ty) + } \end{code} %************************************************************************ @@ -417,7 +450,15 @@ bindTv subst tv ty = return (extendVarEnv subst tv ty) %************************************************************************ \begin{code} -data BindFlag = BindMe | AvoidMe | DontBindMe +data BindFlag + = BindMe -- A regular type variable + | AvoidMe -- Like BindMe but, given the choice, avoid binding it + + | Skolem -- This type variable is a skolem constant + -- Don't bind it; it only matches itself + + | WildCard -- This type variable matches anything, + -- and does not affect the substitution newtype UM a = UM { unUM :: (TyVar -> BindFlag) -> MaybeErr Message a }