Until today I was thinking that xslt functions have no side
effects (provided extensions are not considered).
It has turned out that this is not correct.
That changes the picture (at least for me).
Many possible optimizations are not safe.
Yes, you will find this discussed in the XQuery formal semantics, I believe.
It's a very limited kind of side-effect - but it does have to be taken into
account by an optimizer.