[fpc-devel] Pure function development
J. Gareth Moreton
gareth at moreton-family.com
Sat May 2 20:27:03 CEST 2020
Well, as I've found, there is no straightforward method to actually
determine if a function is pure or not. For example, if it stumbles
upon a procedural call, which may be itself (recursion), it doesn't
immediately know if that call is to a procedure that is itself pure or
not. There are also problems if calculating the value of a pure
function may raise an exception (either by explicitly calling 'raise' or
doing an integer division by zero, for example), something that breaks
things down when assigning pure function results to constant
definitions. And let's not get started if the pure function contains a
deliberate infinite loop!
Gareth aka. Kit
On 02/05/2020 18:51, Florian Klämpfl wrote:
> Am 01.05.20 um 11:41 schrieb J. Gareth Moreton:
>> I'm still learning these things - bear with me! I'll get one set up
>> when I have something preliminary working.
>> At the moment I haven't been able to unite the constant propagation
>> code with my pure functions because they work in fundamentally
>> different ways - for inline functions constant propagation makes a
>> copy of a node tree then transmorphs them as it propagates the
>> constants, while my code just takes the node tree and steps through
>> it without modifying it.
> I see no sense in doing so. It adds another method to do constant
> folding which needs to be maintained.
> There needs to be a procedure/approach to check if a function can be
> pure. That's it. Rest can be done using the currently available
> constant folding/propagation code.
>> I guess there's merits for both approaches, but because of pure
>> functions' ability to be recursive, I'm worried about malicious
>> functions causing a massive ballooning of nodes and memory issues
>> before the compiler detects an infinite loop (or one that is simply
>> too long) before destroying all of the nodes again, throwing a
>> warning and keeping the original call node. Also, there are places
>> where nodes can't actually be used, like in the definition of
>> constants, and they simply have to have their results calculated.
> fpc-devel maillist - fpc-devel at lists.freepascal.org
This email has been checked for viruses by Avast antivirus software.
More information about the fpc-devel