Q:

Automatically convert mutations in imperative code into functional actions?

I wonder if somebody tried creating a language where you can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to pure, Redux-style actions. For example (pseudo-js):
atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:
{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

To clarify, I do not expect this to work for arbitrary code of course. You would apply it to a scope of code that mutates one or more plain-old-data (POD) objects. The language or tool would then create an equivalent "action" object.
I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue" (as an exaggerated example).
You could also convert code to use the command pattern. While not functional, it does contain the mutation of state, and plays a similar role to Redux:
class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple sequential app, and at some point decide to add undo capability, you have to rewrite everything to use the command pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.
Does this idea have a name? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?
(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)

A:

The closest thing to what you are asking is probably Haskell's do notation, which lets you write code that resembles sequences of imperative steps, then translates them internally to a monadic expression.  If you use a free monad, you can then create an interpreter for that expression that does what you want with it.
Another idea that comes very close to what you are asking, especially the deep nesting part, is lenses.  Lenses are difficult to explain concisely, but have the same semantics of creating something that resembles an imperative mutation to the programmer, but that performs a pure operation under the hood.

