Question

If I want a function to be idempotent, is there any framework, language or extension which can allow me to just mark the function as idempotent and the rest will be taken care of behind the scenes without me actually manually making it idempotent?

I believe it could get quite tricky to accomplish a general solution like that though.

Was it helpful?

Solution

I don't have a proof for this, but my gut feeling tells me that this is reducible to either the Halting Problem or Rice's Theorem, which would mean that such a thing cannot possibly exist.

There's also the very practical problem that "making a function idempotent" that is not already idempotent, will simply mean that the function will now do something completely different, so it is not the same function anymore.

For example, there is no sensible way to make print("Hello World") idempotent, without substantially changing what print("Hello World") means.

OTHER TIPS

"Idempotent" describes an aspect of the behavior of a function... If you want your function to be idempotent, you have to make it behave idempotently. You cannot take a non-idempotent function and make it idempotent by simply marking it with a label or an attribute, because that would have to change its behavior, (as Jörg has mentioned, making it a different function).

Of course a compiler could easily make any function idempotent. Let's say in a variant of C:

idempotent int f(void) {
    static int x = 0;
    return ++x;
}

And the compiler replaces it with the code that would be produced by

int f2(void) {
    static int x = 0;
    return ++x;
}

int f(void) {
    static bool firstCall = true;
    static int result;
    if (firstCall) {
        firstCall = false;
        result = f2();
    }
    return result;
}

It's idempotent. It doesn't return what the function without the "idempotent" keyword would do. You decide if having that as a feature makes any sense. I think it doesn't.

Licensed under: CC-BY-SA with attribution
scroll top