This C code can conceptually be described as creating a new array identical to an input array but with 1 as the first element:
int* retire_and_update(int* arr) {
arr[0] = 1;
return arr;
}
This is a pure function (wink wink nudge nudge) so long as no further references are made to the input array and its elements. The C type system won't enforce that for us but it seems enforceable in principle.
The code that gcc generates is simple and efficient:
retire_and_update:
movq %rdi, %rax
movl $1, (%rdi)
ret
Our function achieves the seemingly impossible by creating a whole new array in constant time and using no additional memory. Nice. Can a Haskell function with array-like input and output be written that could be validly implemented with similar code? Is there a way to express "this is the last reference to this variable" so that a pure function can cannibalize the variable behind the scenes?
If the function gets inlined then nothing interesting needs to happen here so let's suppose the caller and function will be compiled separately.