Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purity vs Referential transparency

The terms do appear to be defined differently, but I've always thought of one implying the other; I can't think of any case when an expression is referentially transparent but not pure, or vice-versa.

Wikipedia maintains separate articles for these concepts and says:

From Referential transparency:

If all functions involved in the expression are pure functions, then the expression is referentially transparent. Also, some impure functions can be included in the expression if their values are discarded and their side effects are insignificant.

From Pure expressions:

Pure functions are required to construct pure expressions. [...] Pure expressions are often referred to as being referentially transparent.

I find these statements confusing. If the side effects from a so-called "impure function" are insignificant enough to allow not performing them (i.e. replace a call to such a function with its value) without materially changing the program, it's the same as if it were pure in the first place, isn't it?

Is there a simpler way to understand the differences between a pure expression and a referentially transparent one, if any? If there is a difference, an example expression that clearly demonstrates it would be appreciated.

like image 549
Ani Avatar asked Feb 01 '11 17:02

Ani


People also ask

What are the advantages of referential transparency?

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.

What is not referential transparency?

Contrast to imperative programmingIf the substitution of an expression with its value is valid only at a certain point in the execution of the program, then the expression is not referentially transparent.

How is referential transparency related to functional side effects?

Another benefit of referential transparency is that it eliminates side-effects from your code. Referential transparency requires that functions be free of any code that can modify the program state outside of the function.

What is referential transparency in Haskell?

From HaskellWiki. Referential transparency is an oft-touted property of (pure) functional languages, which makes it easier to reason about the behavior of programs.


1 Answers

If I gather in one place any three theorists of my acquaintance, at least two of them disagree on the meaning of the term "referential transparency." And when I was a young student, a mentor of mine gave me a paper explaining that even if you consider only the professional literature, the phrase "referentially transparent" is used to mean at least three different things. (Unfortunately that paper is somewhere in a box of reprints that have yet to be scanned. I searched Google Scholar for it but I had no success.)

I cannot inform you, but I can advise you to give up: Because even the tiny cadre of pointy-headed language theorists can't agree on what it means, the term "referentially transparent" is not useful. So don't use it.


P.S. On any topic to do with the semantics of programming languages, Wikipedia is unreliable. I have given up trying to fix it; the Wikipedian process seems to regard change and popular voting over stability and accuracy.

like image 68
Norman Ramsey Avatar answered Nov 11 '22 15:11

Norman Ramsey