Idempotence
Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. The concept of idempotence arises in a number of places in abstract algebra and functional programming.
The term was introduced by Benjamin Peirce in the context of elements of algebras that remain invariant when raised to a positive integer power, and literally means " the same power", from + .
Definition
An element x of a magma is said to be idempotent if:If all elements are idempotent with respect to •, then • is called idempotent.
The formula ∀x, is called the idempotency law for •.
Examples
- The natural number 1 is an idempotent element with respect to multiplication, and so is 0, but no other natural number is. For the latter reason, multiplication of natural numbers is not an idempotent operation. More formally, in the monoid, idempotent elements are just 0 and 1.
- In a magma, an identity element e or an absorbing element a, if it exists, is idempotent. Indeed, and.
- In a group, the identity element e is the only idempotent element. Indeed, if x is an element of G such that, then and finally x = e by multiplying on the left by the inverse element of x.
- Taking the intersection x∩y of two sets x and y is an idempotent operation, since x∩x always equals x. This means that the idempotency law ∀x, x∩x = x is true. Similarly, taking the union of two sets is an idempotent operation. Formally, in the monoids and of the power set of the set E with the set union ∪ and set intersection ∩ respectively, all elements are idempotent; hence ∪ and ∩ are idempotent operations on ?.
- In the monoids and of the Boolean domain with the logical disjunction ∨ and the logical conjunction ∧ respectively, all elements are idempotent.
- In a Boolean ring, multiplication is idempotent.
- In a Tropical semiring, addition is idempotent.
Idempotent functions
- Taking the absolute value abs of an integer number x is an idempotent function for the following reason: abs = abs is true for each integer number x. This means that abs ∘ abs = abs holds, that is, abs is an idempotent element in the set of all functions with respect to function composition. Therefore, abs satisfies the above [|definition] of an idempotent function.
- the identity function is idempotent;
- constant functions are idempotent;
- the floor, ceiling and fractional part functions are idempotent;
- the subgroup generated function from the power set of a group to itself is idempotent;
- the convex hull function from the power set of an affine space over the reals to itself is idempotent;
- the closure and interior functions of the power set of a topological space to itself are idempotent;
- the Kleene star and Kleene plus functions of the power set of a monoid to itself are idempotent;
- the idempotent endomorphisms of a vector space are its projections.
is the total number of possible idempotent functions on the set. The integer sequence of the number of idempotent functions as given by the sum above for n = 0, 1, 2, 3, 4, 5, 6, 7, 8, … starts with 1, 1, 3, 10, 41, 196, 1057, 6322, 41393, ….
Neither the property of being idempotent nor that of being not is preserved under function composition. As an example for the former, mod 3 and g = max are both idempotent, but is not, although happens to be. As an example for the latter, the negation function ¬ on the Boolean domain is not idempotent, but is. Similarly, unary negation of real numbers is not idempotent, but is.
Computer science meaning
In computer science, the term idempotence may have a different meaning depending on the context in which it is applied:- in imperative programming, a subroutine with side effects is idempotent if the system state remains the same after one or several calls, in other words if the function from the system state space to itself associated to the subroutine is idempotent in the mathematical sense given in the definition;
- in functional programming, a pure function is idempotent if it is idempotent in the mathematical sense given in the definition.
Computer science examples
A function looking up a customer's name and address in a database is typically idempotent, since this will not cause the database to change. Similarly, changing a customer's address to XYZ is typically idempotent, because the final address will be the same no matter how many times XYZ is submitted. However, placing an order for a cart for the customer is typically not idempotent, since running the call several times will lead to several orders being placed. Canceling an order is idempotent, because the order remains canceled no matter how many requests are made.A composition of idempotent methods or subroutines, however, is not necessarily idempotent if a later method in the sequence changes a value that an earlier method depends on – idempotence is not closed under composition. For example, suppose the initial value of a variable is 3 and there is a sequence that reads the variable, then changes it to 5, and then reads it again. Each step in the sequence is idempotent: both steps reading the variable have no side effects and changing a variable to 5 will always have the same effect no matter how many times it is executed. Nonetheless, executing the entire sequence once produces the output, but executing it a second time produces the output, so the sequence is not idempotent.
In the Hypertext Transfer Protocol, idempotence and safety are the major attributes that separate HTTP verbs. Of the major HTTP verbs, GET, PUT, and DELETE should be implemented in an idempotent manner according to the standard, but POST need not be. GET retrieves a resource; PUT stores content at a resource; and DELETE eliminates a resource. As in the example above, reading data usually has no side effects, so it is idempotent. Storing and deleting a given set of content are each usually idempotent as long as the request specifies a location or identifier that uniquely identifies that resource and only that resource again in the future. The PUT and DELETE operations with unique identifiers reduce to the simple case of assignment to an immutable variable of either a value or the null-value, respectively, and are idempotent for the same reason; the end result is always the same as the result of the initial execution, even if the response differs.
Violation of the unique identification requirement in storage or deletion typically causes violation of idempotence. For example, storing or deleting a given set of content without specifying a unique identifier: POST requests, which do not need to be idempotent, often do not contain unique identifiers, so the creation of the identifier is delegated to the receiving system which then creates a corresponding new record. Similarly, PUT and DELETE requests with nonspecific criteria may result in different outcomes depending on the state of the system - for example, a request to delete the most recent record. In each case, subsequent executions will further modify the state of the system, so they are not idempotent.
In Event stream processing, idempotence refers to the ability of a system to produce the same outcome, even if the same file, event or message is received more than once.
In a load-store architecture, instructions that might possibly cause a page fault are idempotent. So if a page fault occurs, the OS can load the page from disk and then simply re-execute the faulted instruction. In a processor where such instructions are not idempotent, dealing with page faults is much more complex.
When reformatting output, pretty-printing is expected to be idempotent. In other words, if the output is already "pretty", there should be nothing to do for the pretty-printer.
In service-oriented architecture, a multiple-step orchestration process composed entirely of idempotent steps can be replayed without side-effects if any part of that process fails.