3
$\begingroup$

An idea I had for one of my languages recently was this: function purity makes it easier to do certain optimizations. However, the language I'm designing is an impure imperative language that has an implicit purity checker. The model of purity is pretty restrictive, as in this answer. That's fine because purity is implicit, and not exposed to the end user. When the language manages to catalog the pure functions, it can then apply stuff like constant folding, compile-time memoization, and loop hoisting with those functions. Are there any downsides to my idea?

Bonus question: do any languages actually do this?

$\endgroup$

3 Answers 3

6
$\begingroup$

Even if purity is only used for optimisations, users may still rely on those optimisations such that it's an unacceptable performance regression if the same code ceases to be optimisable. There is also the reverse case, where the correctness of a library function depends on a user-provided callback function being pure, where it's better for everyone involved if this is a checked part of the API.

If users will rely on a function being pure (either for correctness or performance), then purity is a part of a library's API. Having it be explicit (e.g. with different keywords for pure functions vs. impure procedures) makes this fact obvious to readers, allows it to be included in generated documentation, and allows the compiler to detect when a promise of purity is broken. If such promises only exist in documentation comments then they can't be checked.


The same issue about purity being part of the API also affects dynamic linking. Suppose module A makes a call to a function from module B within a loop. If the function happens to be pure (as an implementation detail), then module A may be compiled with the function call hoisted out of the loop, but still dynamically linked to module B.

If module B is changed so that the function is no longer pure, then module A must be recompiled or its behaviour will be incorrect, even though the function's signature in module B didn't change ─ that is, it's harder for the author of module B to know whether a change will break code which depends on it.

While this issue could be addressed by compiling module A without the assumption that the dynamically-linked function is pure, addressing it by making purity explicit allows for your performance optimisations to be applied across modules.


There is also a minor issue of compiler performance: it is faster to check only functions labelled as pure, rather than infer for all functions whether they are pure.

$\endgroup$
3
  • $\begingroup$ Hrm, I thought my plan was bulletproof... this is what this site exists for lol $\endgroup$
    – Seggan
    Jun 20, 2023 at 17:48
  • $\begingroup$ @Seggan I mean, it's still a decent plan, and all of the points raised in this answer can be accepted as limitations (or perhaps addressed in other ways, e.g. by doing these optimisations in a JIT instead of AOT) if that's how you want your language to work. $\endgroup$
    – kaya3
    Jun 20, 2023 at 18:06
  • 1
    $\begingroup$ @Seggan you could address this point by adding an optional way to signify purity. Then the compiler could complain if it cannot prove purity for an explicitly marked function $\endgroup$
    – abel1502
    Jun 20, 2023 at 18:44
3
$\begingroup$

In many cases, what may be more useful than specifying that a function is pure, may be a means of indicating that if a program's inputs will cause a certain function to be invoked with certain values, a compiler's generated code may, at the compiler's leisure, call the function with those values any number of times it sees fit, whenever it sees fit, except that timing of such calls might be limited by certain sequencing directives.

Note that any function which is free of outside side effects would naturally satisfy this criterion, but the criterion also applies to many functions which are not pure, but could benefit from the same optimizations as pure functions.

While it might be useful for compilers to recognize functions that can be easily proven to be pure, I think it would in general be better to have programs specify which functions specify the earlier-mentioned criterion regarding sequencing. If treating a function as "pure" would result in a compiler performing optimizations that swap the order in which two log entries get generated, but both orderings would be equally acceptable, a compiler could easily perform the optimizations if it was told that such reordering was acceptable, but could not be expected to accurately know when such orderings would be acceptable,

$\endgroup$
2
  • $\begingroup$ Log entries are a side effect; therefore the example is moot, as pure functions cannot log $\endgroup$
    – Seggan
    Jul 17, 2023 at 1:57
  • $\begingroup$ @Seggan-OnStrike: If one draws a three-way partition: "Functions with side effects that are necessary for correctness; those without any side effects that are relevant to application correctness; those without any side-effects whatsoever", I would view the second category as being useful more often than the third. If one is e.g. trying to identify performance problems, being able to tell how often an "essentially pure" function is called may be a necessary part of that. Having to mark a function as impure in order to add logging would defeat the whole purpose of profiling. $\endgroup$
    – supercat
    Jul 17, 2023 at 15:02
2
$\begingroup$

rather than tracking purity, you may want to track at a more granular level. Julia added an effects system in 1.8 that has been very nice for compile and runtime performance. it tracks a number of properties:

  1. consistency (can other functions mess with you)
  2. effect free (can you mess with data seen by others)
  3. nothrow (might you throw an exception)
  4. termination.

this separation is important because (for example) dead code does not need to be consistent to be removed (but it does need to be nothrow), while computer time memoization is fine with code that throws but requires code to probably terminate.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .