X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FcoreSyn%2FCoreTidy.lhs;h=ba604667e7f22289f0e66799c48e68bcc74fe5e6;hb=28a464a75e14cece5db40f2765a29348273ff2d2;hp=76d1bd3dff9dbaa34db1e8101c7a3b8a8ec09d71;hpb=1f7da30204a9b735e8bc543a5bacf03135bcc9c7;p=ghc-hetmet.git diff --git a/ghc/compiler/coreSyn/CoreTidy.lhs b/ghc/compiler/coreSyn/CoreTidy.lhs index 76d1bd3..ba60466 100644 --- a/ghc/compiler/coreSyn/CoreTidy.lhs +++ b/ghc/compiler/coreSyn/CoreTidy.lhs @@ -1,32 +1,28 @@ % % (c) The AQUA Project, Glasgow University, 1996-1998 % -%************************************************************************ -%* * -\section[PprCore]{Printing of Core syntax, including for interfaces} -%* * -%************************************************************************ \begin{code} module CoreTidy ( - tidyExpr, tidyVarOcc, - tidyIdRules, pprTidyIdRules + tidyExpr, tidyVarOcc, tidyRule, tidyRules ) where #include "HsVersions.h" import CoreSyn import CoreUtils ( exprArity ) -import PprCore ( pprIdRules ) +import Unify ( coreRefineTys ) +import DataCon ( DataCon, isVanillaDataCon ) import Id ( Id, mkUserLocal, idInfo, setIdInfo, idUnique, - idType, idCoreRules ) + idType, setIdType ) import IdInfo ( setArityInfo, vanillaIdInfo, newStrictnessInfo, setAllStrictnessInfo, newDemandInfo, setNewDemandInfo ) -import Type ( tidyType, tidyTyVarBndr ) -import Var ( Var ) +import Type ( Type, tidyType, tidyTyVarBndr, substTy, mkOpenTvSubst ) +import Var ( Var, TyVar, varName ) import VarEnv -import Name ( getOccName ) +import UniqFM ( lookupUFM ) +import Name ( Name, getOccName ) import OccName ( tidyOccName ) import SrcLoc ( noSrcLoc ) import Maybes ( orElse ) @@ -71,43 +67,72 @@ tidyExpr env (Let b e) = tidyBind env b =: \ (env', b') -> Let b' (tidyExpr env' e) --- gaw 2004 tidyExpr env (Case e b ty alts) = tidyBndr env b =: \ (env', b) -> --- gaw 2004 - Case (tidyExpr env e) b (tidyType env ty) (map (tidyAlt env') alts) + Case (tidyExpr env e) b (tidyType env ty) + (map (tidyAlt b env') alts) tidyExpr env (Lam b e) = tidyBndr env b =: \ (env', b) -> Lam b (tidyExpr env' e) ------------ Case alternatives -------------- -tidyAlt env (con, vs, rhs) +tidyAlt case_bndr env (DataAlt con, vs, rhs) + | not (isVanillaDataCon con) -- GADT case + = tidyBndrs env tvs =: \ (env1, tvs') -> + let + env2 = refineTidyEnv env con tvs' scrut_ty + in + tidyBndrs env2 ids =: \ (env3, ids') -> + (DataAlt con, tvs' ++ ids', tidyExpr env3 rhs) + where + (tvs, ids) = span isTyVar vs + scrut_ty = idType case_bndr + +tidyAlt case_bndr env (con, vs, rhs) = tidyBndrs env vs =: \ (env', vs) -> (con, vs, tidyExpr env' rhs) +refineTidyEnv :: TidyEnv -> DataCon -> [TyVar] -> Type -> TidyEnv +-- Refine the TidyEnv in the light of the type refinement from coreRefineTys +refineTidyEnv tidy_env@(occ_env, var_env) con tvs scrut_ty + = case coreRefineTys con tvs scrut_ty of + Nothing -> tidy_env + Just (tv_subst, all_bound_here) + | all_bound_here -- Local type refinement only + -> tidy_env + | otherwise -- Apply the refining subst to the tidy env + -- This ensures that occurences have the most refined type + -- And that means that exprType will work right everywhere + -> (occ_env, mapVarEnv (refine subst) var_env) + where + subst = mkOpenTvSubst tv_subst + where + refine subst var | isId var = setIdType var (substTy subst (idType var)) + | otherwise = var + ------------ Notes -------------- tidyNote env (Coerce t1 t2) = Coerce (tidyType env t1) (tidyType env t2) tidyNote env note = note - ------------ Rules -------------- -tidyIdRules :: TidyEnv -> [IdCoreRule] -> [IdCoreRule] -tidyIdRules env [] = [] -tidyIdRules env (IdCoreRule fn is_orph rule : rules) +tidyRules :: TidyEnv -> [CoreRule] -> [CoreRule] +tidyRules env [] = [] +tidyRules env (rule : rules) = tidyRule env rule =: \ rule -> - tidyIdRules env rules =: \ rules -> - (IdCoreRule (tidyVarOcc env fn) is_orph rule : rules) + tidyRules env rules =: \ rules -> + (rule : rules) tidyRule :: TidyEnv -> CoreRule -> CoreRule -tidyRule env rule@(BuiltinRule _ _) = rule -tidyRule env (Rule name act vars tpl_args rhs) - = tidyBndrs env vars =: \ (env', vars) -> - map (tidyExpr env') tpl_args =: \ tpl_args -> - (Rule name act vars tpl_args (tidyExpr env' rhs)) - -pprTidyIdRules :: Id -> SDoc -pprTidyIdRules id = pprIdRules (tidyIdRules emptyTidyEnv (idCoreRules id)) +tidyRule env rule@(BuiltinRule {}) = rule +tidyRule env rule@(Rule { ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs, + ru_fn = fn, ru_rough = mb_ns }) + = tidyBndrs env bndrs =: \ (env', bndrs) -> + map (tidyExpr env') args =: \ args -> + rule { ru_bndrs = bndrs, ru_args = args, + ru_rhs = tidyExpr env' rhs, + ru_fn = tidyNameOcc env fn, + ru_rough = map (fmap (tidyNameOcc env')) mb_ns } \end{code} @@ -118,6 +143,13 @@ pprTidyIdRules id = pprIdRules (tidyIdRules emptyTidyEnv (idCoreRules id)) %************************************************************************ \begin{code} +tidyNameOcc :: TidyEnv -> Name -> Name +-- In rules and instances, we have Names, and we must tidy them too +-- Fortunately, we can lookup in the VarEnv with a name +tidyNameOcc (_, var_env) n = case lookupUFM var_env n of + Nothing -> n + Just v -> varName v + tidyVarOcc :: TidyEnv -> Var -> Var tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v