Talk:Referential transparency
From Wikipedia, the free encyclopedia
Wouldn't something like getTime() or random() be a better example for something that is free of side effects but not referential transparent?
There should be some more detailed discussion of the possible benefits of referential transparency. For example, call-by-need is difficult to implement without it, as are many optimizations, such as memoization, and other code changes and refactorings.
Also, the use of the word 'enforced' seems strange to me. Perhaps 'guaranteed' would be better?
The description given in the article seems incorrect to me. Referential transparency is a property of a syntactic element, typically expressions, namely that elaboration of the expression results in the same value independently of location of the expression and time of evaluation. The property is assured if an expression is constructed entirely from constants and applications of pure functions. In particular, functions cannot be referentially transparent, only pure: it is the application of the function which may or may not be transparent.
Also the article describes an impure function which depends on a global variable, but doesn't clearly distinguish this case from a function with side-effects. The C construction termed a function may have side-effects on application, yet still its return value may depend only on its arguments. To account for this, I would use the adjective functional, meaning side-effect free. Referential transparency then depends on application of functions which are both functional and pure, or plainly said just "purely functional". The computer programming language Haskell claims to allow only the construction of purely functional function values, and thus all expressions in Haskell are referentially transparent.
Note that in some languages such as ML, functions are first class and can be passed as values, in this context an expression containing a function being passed as a value could be referentially transparent, even though the function itself depends on variables and has side-effects.
For this reason, the article is dangerously incorrect in attributing referential transparency to a 'function', since it fails to properly distinguish a value of a function type, from the application of such a value. In particular in Ocaml, there is no such thing as a function: equivalent constructions such as
let v = ref 0 let f x = print_endline "Hello"; x + !v let f = fun x -> print_endline "Hello"; x + !v
are in fact both initialising f with a value of function type, and the expression
f
is referentially transparent. The application
f 1
is certainly not transparent, and you might say that f is not pure or even functional, but you cannot say that f is not transparent because that is incorrect: the expression f is certainly transparent since it is a constant. In fact, the purity or functional nature of a 'function' refers to a value of function type, there is no such thing as a function in Ocaml, it uses a let-binding to construct values of function type.
In C there are functions, but this is a reference to a syntactic construction used to define a value of function pointer type.
All very tricky and technical, but the bottom line is that referential transparency is a semantic property of the syntactic element 'expression', not a property of a function, indeed it isn't even clear exactly what a 'function' is and the term is probably best avoided altogether in programming contexts.
--Skaller 03:19, 9 August 2005 (UTC)
- This article has had some discussion on Lambda the Ultimate (http://lambda-the-ultimate.org/node/view/1237), where they also point out that the definition of on this page is inaccurate. I've tagged the article as having "disputed factual content", since that seemed to be the most appropriate template in Wikipedia:Template_messages. I'll try to improve the article soon, if nobody else does it first. -- Kimbly 23:57, 19 January 2006 (UTC)
- There seems to be a better definition of referential transparency on the page about Functional Programming --Randomity 23:51, 21 March 2006 (UTC)
[edit] removed disputation
I removed the disputation template for the main page because it seems like someone had updated the definition a while back to better reflect what true definition of Referential Transparency is thought to be.--Andrew Eisenberg 22:44, 8 August 2006 (UTC)