Function Currying
Reiner Pope
reiner.pope at REMOVE.THIS.gmail.com
Wed Nov 15 21:39:27 PST 2006
Bill Baxter wrote:
> From what I gather from that web page and a quick google for "partial
> evaluation", it is a much more advanced beast than partial function
> application. In partial evaluation you'd take something like
> triple_integral(fx,fy,fz) that integrates over three variables, and your
> partial evaluation usage would be:
>
> single_integral = partial_eval(triple_integral, gx, gy);
> answer = single_integral(gz);
>
> Looks just like partial application, except in partial evaluation, the
> first two variables would actually be precomputed (integrated out) to
> create a new function that really did not require gx and gy to perform
> its computation.
>
> Is that the way you understand it, too, David?
I understand partial evaluation (PE) to be a specific form of
optimization which is just inlining + const-folding in the simple cases.
While it would be able to optimize your example, its strengths lie also
in places where it isn't so explicit. For instance, you could write:
writefln("a=[%d]",a);
and running the partial evaluator would reduce that to:
pustr("a=[");
putint(a);
putstr("]");
That sort of optimization is pretty easy for a
compiler to do (note that PE really is a compiler technique, not a
library technique), because you just have to inline, convert to static
single assignment, const-fold and you're mostly done, except for tricky
situations with reference semantics.
The really interesting things, though, come when you want to set up
partial evaluation with non-const values (ie set up code to optimize
away things which will be constant, but only known at runtime). 'Tempo'
for C does this.
A lot of stuff in D templates is really just a way to force the compiler
to inline things (like, say, templated regexps). If the optimizer were
sufficiently powerful (perhaps we could even look into something like
guaranteed optimization) then the need for these *should* disappear,
since you could trust the compiler to partially evaluate them.
PE could open a lot of opportunities, but the problem is that you only
really notice it in speed and early catching of errors; there are no
cool language features that you have to show for it.
Cheers,
Reiner
More information about the Digitalmars-d
mailing list