> But isn't it the case in
any programming language that impure operations only have side effects when they are executed?
That's a great question! This framing is indeed universally true, which suggests there's something less useful about one or more of the definitions it relies on. For my money, I'm betting on our definitions of "executed" being different.
What exactly does it mean to execute a program? For most of us practitioners, it means using a program for its intended purpose. I think this is closest to the meaning you're using. But this embodies everything at all about a program, so it is quite coarse grained.
Can we break "execution" into smaller pieces? Yes, at least conceptually. First, we can speak of "reduction": it's what happens when you simplify "1 + 1". Second, we can speak of "interaction": it's what happens when you substitute for "x" in "x + 1".
Reduction is purely internal: the program state evolves purely on the basis of the information it already has. We perform reduction in order to turn the program into a more useful form, much as in algebra we might put a linear expression into slope-intercept form.
Interaction operates explicitly on the exchange of information between two parties: the program and its environment. We perform interaction when we need to make some information known by one party available to the other.
Personally, I take my definition of "side-effect" to mean "requires interaction". For instance, take the (JavaScript) program `(x) => x + 2`. This program cannot be reduced; in order to reduce it further, we need to interact with it by providing an argument. That interaction transforms the program to, say, `((x) => x + 2)(42)`, and now we can reduce down to `44`, which cannot reduce and cannot interact.
Of course, if my program was `((x) => x + 2)(42)` from the beginning, there is no side-effect. At least, not one visible from the outside: the interaction between programs `(x) => x + 2` and `42` has been promoted to a reduction via composition.
This kind of interaction allows us to implement request-response protocols between the program and an environment, so long as we have a runtime to mediate the interaction. But it's also somewhat painful to author programs in this style: we have to return a callback all the way back to the top of the program whenever we want to interact.
If we add interaction primitives to the language itself, as in `prompt("Enter a number:")`, we gain the ability to interact from anywhere in the program, in exchange for the added complexity of a runtime that has to keep track of where in the program the interaction is occurring. This is great! But can we promote the `prompt` interaction to an internal reduction as we did with function application?
Well, in JavaScript, _kind of_. Since most symbols are dynamically looked up, you could overwrite `window.prompt` before running the inner program, and reset it afterward. But this impacts every call to `prompt`, not just the one (out of potentially many) you wanted to intercept. Providing an internal `prompt` interaction becomes a bit fraught; it's not very composable. And in other languages, you couldn't do this at all. (Dynamic lookup is itself a kind of primitive effect system, so you can backdoor a surprising number of things through it.)
Suppose that, instead of adding interaction primitives directly to the language, we stick with the original idea of "a returned top-level function is a request for interaction", but try to dress it up a bit more nicely. We still want to be able to perform interaction deep within the program, but we need some out-of-the-way machinery that propagates that interaction up to the top level for us, and manages remembering how to get back to where we were. In other words, we need to implement the system that was previously in the runtime in our program instead. Only the actual doing of actions is left in the runtime.
Without burying the lede, if you do this you end up in one of a few algebraic systems, one of which is monads, and all of which have the property that you can promote interaction to reduction in a composable fashion. (With monads, you need to define your own monad capturing the interactions you care about, and then write an interpreter for that monad that turns your monad's interactions into IO interactions.) Basically, you can mock out the environment in a particularly clean way.
Monads end up being useful for quite a lot besides managing IO. It's very appealing as a solution that doesn't seem to be special-cased for IO specifically. I think that has a lot to do with how they allow you to in general program against interactions and then promote to reductions. It's harder to do that in JavaScript, since there are so many effectful operations, and while you can promote some of them, you can't promote all of them.