views:

1181

answers:

5

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.

+4  A: 

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.

Barry Kelly
+4  A: 

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.

CMS
+18  A: 

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.

Brian R. Bondy
Just a heads up, it is possible to have a fully referentially transparent object, with referentially transparent member functions. See http://okmij.org/ftp/Scheme/oop-in-fp.txt
Jonathan Arkell
And here is the code that is being talked about in that article: http://okmij.org/ftp/Scheme/pure-oo-system.scm
Jonathan Arkell
In the case of a fully referentially transparent class, you would probably have all member functions static.
Brian R. Bondy
Good links! lots of people conflate object-oriented with imperative. They are orthogonal to each other.
Claudiu
+4  A: 

A referentially transparent function is one which only depends on its input.

Draemon
Upvoted for conciseness.
Sergio Acosta
+1  A: 

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.

Andrew Birkett