Repeated computations under a lambda

Conal Elliott conal at conal.net
Wed Jul 19 16:09:05 UTC 2017


> GHC does this transformation not nilly-willy, .... If that
> code would call some unknown function, or allocate memory, then GHC
> would certainly preserve your intention.

Wonderful. Thanks for the clarification.

-- Conal

On Tue, Jul 18, 2017 at 6:51 PM, Joachim Breitner <mail at joachim-breitner.de>
wrote:

> Hi,
>
> Am Dienstag, den 18.07.2017, 17:01 -0700 schrieb Conal Elliott:
> > 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.
>
> usually it is very effective. GHC does this transformation not nilly-
> willy, but only when its heuristics indicate that it is ok, usually
> because the operation (here sin) is known to be very cheap. If that
> code would call some unknown function, or allocate memory, then GHC
> would certainly preserve your intention.
>
> Greetings,
> Joachim
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20170719/269a5de7/attachment.html>


More information about the ghc-devs mailing list