What Is Referential Transparency?
Table of Contents
In functional programming, referential transparency is generally defined as the fact that an expression, in a program, may be replaced by its value (or anything having the same value) without changing the result of the program. This implies that methods should always return the same value for a given argument, without having any other effect. This functional programming concept also applies to imperative programming, though, and can help you make your code clearer.
To understand this article you should know how to write simple classes and methods in Java. Knowing what a side effect is helps but if you don’t, you’ll learn it on the way.
Referential Transparency
The expression referential transparency is used in various domains, such as mathematics, logic, linguistics, philosophy and programming. It has quite different meanings in each of these domain. Here, we will deal only with computer programs, although we will show analogy with maths (simple maths, don’t worry). Note, however, that computer scientists do not agree on the meaning of referential transparency in programming. What we will look at is referential transparency as it is used by functional programmers.
Referential Transparency in Maths
In maths, referential transparency is the property of expressions that can be replaced by other expressions having the same value without changing the result in any way. Consider the following example:
x = 2 + (3 * 4)
We may replace the subexpression (3 * 4)
with any other expression having the same value without changing the result (the value of x
). The most evident expression to use, is of course 12
:
x = 2 + 12
Any other expression having the value 12
(maybe (5 + 7)
) could be used without changing the result. As a consequence, the subexpression (3 * 4)
is referentially transparent.
We may also replace the expression 2 + 12
by another expression having the same value without changing the value of x
, so it is referentially transparent too:
x = 14
You can easily see the benefit of referential transparency: It allows reasoning. Without it, we could not resolve any expression without considering some other elements.
Referential Transparency in Programming
In programming, referential transparency applies to programs. As programs are composed of subprograms, which are programs themselves, it applies to those subprograms, too. Subprograms may be represented, among other things, by methods. That means method can be referentially transparent, which is the case if a call to this method may be replaced by its return value:
int add(int a, int b) {
return a + b
}
int mult(int a, int b) {
return a * b;
}
int x = add(2, mult(3, 4));
In this example, the mult
method is referentially transparent because any call to it may be replaced with the corresponding return value. This may be observed by replacing mult(3, 4)
with 12
:
int x = add(2, 12)
In the same way, add(2, 12)
may be replaced with the corresponding return value, 14
:
int x = 14;
None of these replacements will change the result of the program, whatever it does. Note that we could use any other expression having the same value, which is useful when refactoring.
On the other hand, consider the following program:
int add(int a, int b) {
int result = a + b;
System.out.println("Returning " + result);
return result;
}
Replacing a call to the add
method with the corresponding return value will change the result of the program, since the message will no longer be printed. In that case, it would only remove the side effect but in other cases, it might change the value returned by the method:
public static void main(String... args) {
printFibs(10);
}
public static void printFibs(int limit) {
Fibs fibs = new Fibs();
for (int i = 0; i < limit; i++) {
System.out.println(fibs.next());
}
}
static class Fibs {
private int previous = -1;
private int last = 1;
public Integer next() {
last = previous + (previous = last);
return previous + last;
}
}
Here, the next
method can’t be replaced with anything having the same value, since the method is designed to return a different value on each call.
Using such non referentially transparent methods requires a strong discipline in order not to share the mutable state involved in the computation. Functional style avoids such methods in favor of referentially transparent versions.
Referential Transparency in Imperative Programming
Both imperative and functional programming use functions. Although functional programming uses only functions, imperative programming uses:
- pure functions: methods returning values and having no other effects
- pure effects: methods returning nothing but changing something outside of them)
- functions with side effects: methods both returning a value and changing something
As we all know, it is good practice to avoid functions with side effects. This leaves imperative programmers with pure functions and pure effects. Referential transparency is then a powerful tool for imperative programmers to make their programs easier to reason about, and easier to test.
Summary
Referential transparency means that a function call can be replaced by its value or another referentially transparent call with the same result. It makes reasoning about programs easier. It also makes each subprogram independent, which greatly simplifies unit testing and refactoring. As an additional benefit, referentially transparent programs are easier to read and understand, which is one reason for why functional programs need less comments.
To learn more about the subject, you can have a look at the following documents: