Repeated computations under a lambda
Simon Peyton Jones
simonpj at microsoft.com
Wed Jul 19 07:50:20 UTC 2017
I opened a ticket and replied:
From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Conal Elliott
Sent: 19 July 2017 01:01
To: David Feuer <david at well-typed.com>
Cc: ghc-devs at haskell.org
Subject: Re: Repeated computations under a lambda
Here's the code in question, slightly rephrased:
> exampleC t = \ x -> x + s where s = sin t
I wrote it this way so that `sin t` would be computed once per `t` and reused for each value of `x`. The intermediate result `s` has type `Double`---not a function. Without `-fno-do-lambda-eta-expansion`, phase 2 of `ghc -O` causes the `s = sin t` binding to be moved under the `\ x -> ...`. I've been using this programming style for this purpose for longer than I can remember, and apparently I've been mistaken about its effectiveness at least part of that time.
On Tue, Jul 18, 2017 at 4:52 PM, David Feuer <david at well-typed.com<mailto:david at well-typed.com>> wrote:
On Tuesday, July 18, 2017 3:55:28 PM EDT Conal Elliott wrote:
> Hi Sebastian,
> Thanks for the reply. It's that I don't want `exampleC` to be eta-expanded.
> Apparently GHC does by default even when doing so moves computation under
> lambda. I've thought otherwise for a very long time.
GHC really likes to eta-expand, because that can be very good for allocation,
unboxing, and I don't know what else. Do you really need to represent the
intermediate result by a *function*? Would it work just to save the Double
itself? I suspect you could likely convince GHC to leave it alone.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the ghc-devs