Can we find a definition that allows authors of functions to derive=
their "pure" and "side-effect-free" status by only loo=
king at their interface and implementation? It seems like these attributes =
would both be useful for documentation purposes as well as for optimization=
, and they have seen some use outside of Emacs core (e.g. in dash.el), so t=
heir semantics should be formalized and documented. How about something lik=
e this:
A side-effect-free function is one where a=
ll of the following is true:
- After exiting the function (whethe=
r normally or nonlocally), the `match-data' are `equal' to the `mat=
ch-data' before entering the function.
- After exiting the fu=
nction, all dynamic variables, their default values, their symbol cells, et=
c., are `eq' to the respective values before entering the function. Exc=
eptions are variables only used for debugging/tracing, such as `cons-cells-=
consed'.
- All buffer contents, markers, point values etc. ar=
e `equal-including-properties' to their previous values.
- Th=
e same buffers and threads still exist.
A side-eff=
ect-free-and-error-free function is one that is side-effect-free and also d=
oesn't signal any errors except `memory-full'.
=
A pure function is a side-effect-free function with the following addi=
tional properties:
- The function recursively only accepts and re=
turns numbers, symbols, lists, vectors, hashtables, and strings; not buffer=
s or processes since they are naturally stateful.
- Two invocatio=
ns with arguments that are pairwise `equal-including-properties' (or, i=
n the case of hashtables, have keys and values that are `equal-including-pr=
operties') will either both exit nonlocally or return values that are a=
gain `equal-including-properties'.