What does the term referential transparency mean? I've heard it described as "it means you can replace equals with equals" but this seems like an inadequate explanation.
A referentially transparent function is one which acts like a mathematical function; given the same inputs, it will always produce the same outputs. It implies that the state passed in is not modified, and that the function has no state of its own.
An expression is referentially transparent if it can be replaced with its value, without changing the algorithm, yielding an algorithm that has the same effects and output on the same input.
Referential transparency, a term commonly used in functional programming, means that given a function and an input value, you will always receive the same output. That is to say there is no external state used in the function.
Here is an example of a referential transparent function:
int plusOne(int x)
{
return x+1;
}
With a referential transparent function, given an input and a function, you could replace it with a value instead of calling the function. So instead of calling plusOne with a paremter of 5, we could just replace that with 6.
Another good example is mathematics in general. In mathematics given a function and an input value, it will always map to the same output value. f(x) = x + 1. Therefore functions in mathematics are referentially transparent.
This concept is important to researchers because it means that when you have a referentially transparent function, it lends itself to easy automatic parallelization and caching.
Referential transparency is used always in functional languages like Haskell.
--
In contrast there is the concept of referential opaqueness. This means the opposite. Calling the function may not always produce the same output.
//global G
int G = 10;
int plusG(int x)
{//G can be modified externally returning different values.
return x + G;
}
Another example, is a member function in an object oriented programming language. Member functions commonly operate on its member variables and therefore would be referential opaque. Member functions though can of course be referentially transparent.
Yet another example is a function that reads from a text file and prints the output. This external text file could change at any time so the function would be referentially opaque.
A referentially transparent function is one which only depends on its input.
If you're interested in the etymology (ie. why does this concept have this particular name), have a look at my blog post on the topic. The terminology comes from the philosopher/logician Quine.