[fpc-devel] Pure function Wiki page
Martin
fpc at mfriebe.de
Sun Jul 8 20:52:02 CEST 2018
On 08/07/2018 19:22, J. Gareth Moreton wrote:
> At the same time, I would argue that, really, the programmer should
> code something like this:
>
> c := sin(x);
> if c > y then b := c;
>
> There probably isn't any harm in supporting such optimisation though.
>
Talking about this.... Maybe it is worth to also detect if an inpure
function is side effect free (e.g. random() ).
if sin(globalvar) > foo() then b := sin(globalvar);
If foo is side-effect free, then the optimization can still be done.
(even if foo is not pure).
And since side-effect free is anyway part of being pure, you are going
to write those checks anyway. It may be worth organizing the checks, so
that side-effect free can be detected on its own too.
This also allows more optimization around full bool eval or short cut
bool eval.
if true or foo() then
Full bool eval will call foo, despite it will never change the result.
This is needed for various reasons:
- side effects
- security (cryptography etc)
In case of the former, a mode switch could be introduced to shortcut
bool eval only for side effect free expressions. (though that is very
specific, and may not be wanted)
In any case knowing that a function is side effect free will later help
with data flow analysis.
More information about the fpc-devel
mailing list