[fpc-devel] Pure function development

J. Gareth Moreton gareth at moreton-family.com
Wed Apr 29 23:59:19 CEST 2020


I'm glad we agree that one benefit of pure functions is loop-hoisting.  
I call them pure functions because that's the technical name for them: 
https://en.wikipedia.org/wiki/Pure_function - they're analogous to a 
mathematical function.  'Read-only' sounds a little ambiguous to me and 
not immediately certain as to what is actually read-only. Granted, the 
meaning of 'pure' doesn't immediately jump out either, but the lack of 
side-effects and consistent outputs for a given inputs are the necessary 
criteria to be defined as such.

The "pure" directive, or the lack of it in most functions, helps takes 
pressure off the compiler, while its presence aids the programmer by 
explicitly stating that 'this function is pure' and can be used as 
such.  If a third-party library removes the purity of a function 
compared to an earlier version, I would put the blame on the programmer 
of the library rather than the language itself.  There will always be 
cases where you've screwed yourself over and have to change things, like 
the addition of a new parameter in a subroutine.

I suppose node emulation would be equivalent to constant propagation.  I 
sense we might need to refactor things afterwards so the same code can 
be reused for both purposes.  My current design for the emulation would 
introduce a new virtual method for TNode that descendants override to 
perform their specific behaviour, rather than have it all in a 
monolithic 'propagate' procedure.  By default, this method would return 
a 'fail' code to indicate the function is not pure (a function that is 
marked as 'pure' but is discovered not to be will raise a compiler 
warning, and an error if it's part of a constant assignment), so you 
don't have to program something for every single node and the 
introduction of a new node type won't break the emulator.

I think the difference between constant propagation and node emulation 
in this regard is that the former is trying to simplify nodes and 
eliminate conditional branches that will not be executed, among other 
things, while the latter is trying to generate a single output (or 
multiple if the routine has "out" parameters).

Gareth aka. Kit


-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the fpc-devel mailing list