+ add_export bndr
+ | isLocalId bndr && dont_discard bndr = setIdLocalExported bndr
+ -- The isLocalId check is to avoid fiddling with
+ -- locally-defined Ids like data cons and class ops
+ -- which are "born" as GlobalIds
+ | otherwise = bndr
+
+ orph_rhs_fvs = unionVarSets [ ruleRhsFreeVars rule
+ | IdCoreRule _ is_orphan_rule rule <- rules,
+ is_orphan_rule ]
+ -- An orphan rule keeps alive the free vars of its right-hand side.
+ -- Non-orphan rules are (later, after gentle simplification)
+ -- attached to the Id and that keeps the rhs free vars alive
+
+ dont_discard bndr = is_exported name
+ || name `elemNameSet` keep_alive
+ || bndr `elemVarSet` orph_rhs_fvs
+ where
+ name = idName bndr
+
+ -- In interactive mode, we don't want to discard any top-level
+ -- entities at all (eg. do not inline them away during
+ -- simplification), and retain them all in the TypeEnv so they are
+ -- available from the command line.
+ --
+ -- isExternalName separates the user-defined top-level names from those
+ -- introduced by the type checker.
+ is_exported :: Name -> Bool
+ is_exported | ghci_mode == Interactive = isExternalName
+ | otherwise = (`elemNameSet` exports)