+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
+