[fpc-devel] Pure function Wiki page

J. Gareth Moreton gareth at moreton-family.com
Mon Jul 9 02:15:21 CEST 2018


 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.lists at gmail.com sent:
 On Sun, Jul 8, 2018 at 7:18 PM, Thorsten Engler  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
 vsIf 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 at lists.freepascal.org [2]
 http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[3]">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

 

Links:
------
[1] mailto:thorsten.engler at gmx.net
[2] mailto:fpc-devel at lists.freepascal.org
[3] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180709/ccd5e67f/attachment.html>


More information about the fpc-devel mailing list