-
-%************************************************************************
-%* *
-\subsection{Eta expansion and reduction}
-%* *
-%************************************************************************
-
-We try for eta reduction here, but *only* if we get all the
-way to an exprIsTrivial expression.
-We don't want to remove extra lambdas unless we are going
-to avoid allocating this thing altogether
-
-\begin{code}
-tryEtaReduce :: [OutBndr] -> OutExpr -> Maybe OutExpr
-tryEtaReduce bndrs body
- -- We don't use CoreUtils.etaReduce, because we can be more
- -- efficient here:
- -- (a) we already have the binders
- -- (b) we can do the triviality test before computing the free vars
- = go (reverse bndrs) body
- where
- go (b : bs) (App fun arg) | ok_arg b arg = go bs fun -- Loop round
- go [] fun | ok_fun fun = Just fun -- Success!
- go _ _ = Nothing -- Failure!
-
- ok_fun fun = exprIsTrivial fun
- && not (any (`elemVarSet` (exprFreeVars fun)) bndrs)
- && (exprIsHNF fun || all ok_lam bndrs)
- ok_lam v = isTyVar v || isDictId v
- -- The exprIsHNF is because eta reduction is not
- -- valid in general: \x. bot /= bot
- -- So we need to be sure that the "fun" is a value.
- --
- -- However, we always want to reduce (/\a -> f a) to f
- -- This came up in a RULE: foldr (build (/\a -> g a))
- -- did not match foldr (build (/\b -> ...something complex...))
- -- The type checker can insert these eta-expanded versions,
- -- with both type and dictionary lambdas; hence the slightly
- -- ad-hoc isDictTy
-
- ok_arg b arg = varToCoreExpr b `cheapEqExpr` arg
-\end{code}
-
-
- Try eta expansion for RHSs
-
-We go for:
- f = \x1..xn -> N ==> f = \x1..xn y1..ym -> N y1..ym
- (n >= 0)
-
-where (in both cases)
-
- * The xi can include type variables
-
- * The yi are all value variables
-
- * N is a NORMAL FORM (i.e. no redexes anywhere)
- wanting a suitable number of extra args.
-
-We may have to sandwich some coerces between the lambdas
-to make the types work. exprEtaExpandArity looks through coerces
-when computing arity; and etaExpand adds the coerces as necessary when
-actually computing the expansion.
-
-\begin{code}
-tryEtaExpansion :: DynFlags -> OutExpr -> SimplM OutExpr
--- There is at least one runtime binder in the binders
-tryEtaExpansion dflags body
- = getUniquesSmpl `thenSmpl` \ us ->
- returnSmpl (etaExpand fun_arity us body (exprType body))
- where
- fun_arity = exprEtaExpandArity dflags body
-\end{code}
-
-