Referential Transparency

An essential advantage of referential transparency in programming languages is that mathematical reasoning is available for referentially transparent programs (which includes a fortiori all programs written in a referentially transparent programming language).

Therefore, referential transparency also facilitates program transformation.

For me this is important mainly in the context of the graphically interactive term graph programming and program transformation system HOPS I am developing.


The following definitions of referential transparency are quoted from a USENET News article by Tom DeBoni <deboni@llnl.gov>

"The phrase 'referentially transparent' is used to describe notations where only the meaning (value) of immediate component expressions (sentences/phrases) is significant in determining the meaning of a compound expression (sentence/phrase). Since expressions are equal if and only if they have the same meaning, referential transparency means that substitutivity of equality holds (i.e. equal subexpressions can be interchanged in the context of a larger expression to give equal results)." Reade, Chris; Elements of Functional Programming, p. 10; Addison-Wesley, 1989.

"The fundamental property of mathematical functions which enables us to plug together block boxes [he is referring here to 'built-in functions or primitives' described in a previous paragraph] in this way is the property of 'referential transparency.' There are a number of intuitive reading of the term, but essentially it means that each expression denotes a single value which cannot be changed by evaluating the expression or by allowing different parts of a program to share the expression. Evaluation of the expression simply changes the form of the expression but never its value. All references to the value are therefore equivalent to the value itself and the fact that the expression may be referred to from other parts of the program is of no concern." Field, Anthony, and Harrison, Peter G.; Functional Programming, p. 10; Addison-Wesley, 1988.

"The most important feature of mathematical notation is that an expression is used solely to describe (or denote) a value. In other words, the meaning of an expression is its value and there are no other effects, hidden or otherwise, in any procedure for actually obtaining it. Furthermore, the value of an expression depends only on the values of its constituent expressions (if any) and these subexpressions may be replaced freely by others possessing the same value. ... The characteristic property of mathematical expressions described here is called "referential transparency." Bird, Richard, and Wadler, Philip; Introduction to Functional Programming, p. 4; Prentice Hall, 1988.

My informal read on the term in question is that there are no side effects. A function written in a truly functional language, which evaluates an expression and returns its value, does nothing else. A routine written in an imperative language, such as Fortran, Pascal, or C, which evaluates the same expression, may also change a value in a memory location having nothing to do with the expression being evaluated; for instance, it may set a Boolean value indicating that the expression has indeed been evaluated. While it may appear harmless, this side effect prevents the substitution of the expression's value for the invocation of the routine that evaluates it. Thus, the imperative language routine is (potentially) referentially opaque. In functional languages, referential opaqueness is forbidden and referential transparency is mandatory. There's just no other way to do things; if there is, the language is not functional.

--- end quote ---


Wolfram Kahl