Thinking about it some more, this might also interact with loop unrolling.

 

e.g.:

 

for i := 0 to 9 do

  x := x * SomeFunc(i);

 

If the loop is being unrolled, what looks like a non-const expression becomes a 
const expression. So if SomeFunc is marked as pure, the compiler might be able 
to omit the call completely.

 

From: fpc-devel <fpc-devel-boun...@lists.freepascal.org> On Behalf Of J. Gareth 
Moreton
Sent: Monday, 9 July 2018 10:15
To: FPC developers' list <fpc-devel@lists.freepascal.org>
Subject: Re: [fpc-devel] Pure function Wiki page

 

If I had to give a realistic example of a pure function in such a conditional 
expression, how about?

"if (decay_constant * time_elapsed) >= ln(2) then ..."

This is a formula related to radioactive decay - if the condition is true, then 
over half of the original sample has decayed (i.e. time_elapsed is greater than 
the sample's half-life).

Yes, if any parameters are variables, then the function is not evaluated.  My 
intention is that the purity of a function is only determined when it comes to 
evaluating it in an expression, but because of how complex functions can 
become, the "pure" directive hints to the compiler that the given function is 
pure and it should attempt the laborous task of evaluating it, rather than the 
opposite approach of attempting to evaluate all functions with constant actual 
parameters and potentially increasing the compilation time by several orders of 
magnitude (don't forget it might be attempting to do the same thing with system 
functions if the project is undergoing a full build).

 

Gareth



On Mon 09/07/18 01:21 , Dmitry Boyarintsev skalogryz.li...@gmail.com 
<mailto:skalogryz.li...@gmail.com>  sent:

On Sun, Jul 8, 2018 at 7:18 PM, Thorsten Engler <thorsten.eng...@gmx.net 
<mailto:thorsten.eng...@gmx.net> > wrote:

Maybe you don’t understand what “determine the purity of a function” means?

 

It means that every time any function is called, the compiler has to try to 
execute the function at compile time (by working through the nodes like an 
interpreter) until it hits a point that is non-deterministic. This can 
potentially take forever (the halting problem is real!), so the only thing 
limiting it is basically some form of timeout (defined as either time spend or 
nodes traverse). 

 

If you are talking about always considering every function as pure until proven 
otherwise, you are talking about slowing down the compiler by orders of 
magnitude.

 

I'm taking about considering every function that's **used to calculate a 
constant expression** as a pure function (not just every function). 

(how many of:

If FunctionCall(42) > 0 then

vs

If FunctionCall(x) > 0 then

is out there?)

 

Naturally, if a parameters of pure function are variable of any kind, the 
evaluation cannot be done in compile time anyway.

 

So, if a function (even if it's a pure function) is not used for constant 
expression valuation, there's no point in the actual "determination of purity" 
(in it's full meaning).

 

The compiler knows if a function potentially impure w/o the actual evaluation 
or interpretation, solely based on what data it's using.  

 

Even if you deal with functions marked by a developer as "pure", the problem of 
full determination of purity remains. 

 

thanks,

Dmitry

 

_______________________________________________
fpc-devel maillist - fpc-devel@lists.freepascal.org 
<mailto:fpc-devel@lists.freepascal.org> 
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel";>http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to