Commit 0971055c authored by Simon Peyton Jones's avatar Simon Peyton Jones
Browse files

Eta expand partial applications

When we have
     x = \v -> map g
we want to eta-expand to
     x = \v y -> map g y

We weren't doing so, and that led to worse code and,
perhaps, #5285.  I need to check the latter, but I
was certainly seeing one similar error.  Anyway
this looks like a definite improvement
parent f88b20f4
......@@ -1118,17 +1118,20 @@ tryEtaExpand env bndr rhs
return (new_arity, new_rhs) }
where
try_expand dflags
| exprIsTrivial rhs
= return (exprArity rhs, rhs)
| sm_eta_expand (getMode env) -- Provided eta-expansion is on
, not (exprIsTrivial rhs)
, let dicts_cheap = dopt Opt_DictsCheap dflags
new_arity = findArity dicts_cheap bndr rhs old_arity
, new_arity > rhs_arity
, new_arity > manifest_arity -- And the curent manifest arity isn't enough
-- See Note [Eta expansion to manifes arity]
= do { tick (EtaExpansion bndr)
; return (new_arity, etaExpand new_arity rhs) }
| otherwise
= return (rhs_arity, rhs)
= return (manifest_arity, rhs)
rhs_arity = exprArity rhs
manifest_arity = manifestArity rhs
old_arity = idArity bndr
_dmd_arity = length $ fst $ splitStrictSig $ idStrictness bndr
......@@ -1217,6 +1220,23 @@ because then 'genMap' will inline, and it really shouldn't: at least
as far as the programmer is concerned, it's not applied to two
arguments!
Note [Eta expansion to manifest arity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Eta expansion does *not* eta-expand trivial RHSs, like
x = y
because these will get substituted out in short order. (Indeed
we *eta-contract* if that yields a trivial RHS.)
Otherwise we eta-expand to produce enough manifest lambdas.
This *does* eta-expand partial applications. eg
x = map g --> x = \v -> map g v
y = \_ -> map g --> y = \_ v -> map g v
One benefit this is that in the definition of y there was
a danger that full laziness would transform to
lvl = map g
y = \_ -> lvl
which is stupid. This doesn't happen in the eta-expanded form.
Note [Arity analysis]
~~~~~~~~~~~~~~~~~~~~~
The motivating example for arity analysis is this:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment