---------------------------------
--- let simplification
---------------------------------
-letsimpl, letsimpltop :: Transform
--- Don't simplify a let that evaluates to another let, since this is already
--- normal form (and would cause infinite loops with letflat below).
-letsimpl c expr@(Let _ (Let _ _)) = return expr
--- Put the "in ..." value of a let in its own binding, but not when the
--- expression is already a local variable, or not representable (to prevent loops with inlinenonrep).
-letsimpl c expr@(Let binds res) = do
- repr <- isRepr res
- local_var <- Trans.lift $ is_local_var res
- if not local_var && repr
- then do
- -- If the result is not a local var already (to prevent loops with
- -- ourselves), extract it.
- id <- Trans.lift $ mkBinderFor res "foo"
- change $ Let binds (Let (NonRec id res) (Var id))
- else
- -- If the result is already a local var, don't extract it.
- return expr
-
--- Leave all other expressions unchanged
-letsimpl c expr = return expr
--- Perform this transform everywhere
-letsimpltop = everywhere ("letsimpl", letsimpl)
-