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.
相关问题
- Relation between Function1 and Reader Monad
- scala passing function with underscore produces a
- Combining n vectors into one vector of n-tuples
- Improve this code by eliminating nested for cycles
- Redefine list monad instance
相关文章
- Is there something like the threading macro from C
- Algorithm for partially filling a polygonal mesh
- Learning F#: What books using other programming la
- Creating a list of functions using a loop in R
- When to use interfaces, and when to use higher ord
- Functors in Ocaml
- Proving correctness of multithread algorithms
- Java Lambda Referencing Enclosing Object: Replace
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.
In functional programming, referential transparency is generally defined as the fact that an expression, in a program, may be replaced by its value (or anything having the same value) without changing the result of the program. This implies that methods should always return the same value for a given argument, without having any other effect. This functional programming concept also applies to imperative programming, though, and can help you make your code clearer.
Referential Transparency
The expression referential transparency is used in various domains, such as mathematics, logic, linguistics, philosophy and programming. It has quite different meanings in each of these domain. Here, we will deal only with computer programs, although we will show analogy with maths (simple maths, don’t worry). Note, however, that computer scientists do not agree on the meaning of referential transparency in programming. What we will look at is referential transparency as it is used by functional programmers.
Referential Transparency in Maths In maths, referential transparency is the property of expressions that can be replaced by other expressions having the same value without changing the result in any way. Consider the following example:
We may replace the subexpression (3 * 4) with any other expression having the same value without changing the result (the value of x). The most evident expression to use, is of course 12:
Any other expression having the value 12 (maybe (5 + 7)) could be used without changing the result. As a consequence, the subexpression (3 * 4) is referentially transparent.
We may also replace the expression 2 + 12 by another expression having the same value without changing the value of x, so it is referentially transparent too:
You can easily see the benefit of referential transparency: It allows reasoning. Without it, we could not resolve any expression without considering some other elements.
Referential Transparency in Programming In programming, referential transparency applies to programs. As programs are composed of subprograms, which are programs themselves, it applies to those subprograms, too. Subprograms may be represented, among other things, by methods. That means method can be referentially transparent, which is the case if a call to this method may be replaced by its return value:
In this example, the mult method is referentially transparent because any call to it may be replaced with the corresponding return value. This may be observed by replacing mult(3, 4) with 12:
In the same way, add(2, 12) may be replaced with the corresponding return value, 14:
None of these replacements will change the result of the program, whatever it does. Note that we could use any other expression having the same value, which is useful when refactoring.
On the other hand, consider the following program:
Replacing a call to the add method with the corresponding return value will change the result of the program, since the message will no longer be printed. In that case, it would only remove the side effect but in other cases, it might change the value returned by the method:
Here, the next method can’t be replaced with anything having the same value, since the method is designed to return a different value on each call.
Using such non referentially transparent methods requires a strong discipline in order not to share the mutable state involved in the computation. Functional style avoids such methods in favor of referentially transparent versions.
Referential Transparency in Imperative Programming Both imperative and functional programming use functions. Although functional programming uses only functions, imperative programming uses:
As we all know, it is good practice to avoid functions with side effects. This leaves imperative programmers with pure functions and pure effects. Referential transparency is then a powerful tool for imperative programmers to make their programs easier to reason about, and easier to test.
A referentially transparent function is one which only depends on its input.
Note that this concept of "meaning" is something that happens in the mind of the observer. Thus, the same "reference" can mean different things to different people. So, for example, we have an Edinburgh disambiguation page in Wikipedia.
A related issue which can show up in the context of programming might be polymorphism.
And perhaps we should have a name for the special case of polymorphism (or perhaps even casting) where for our purposes the differing polymorphic cases are semantically equivalent (as opposed to just being similar. For example, the number 1 -- which might be represented using an integer type, or a complex type or any of a variety of other types -- can be treated polymorphically).
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.
In 1 there is a clarity of two languages in question:
In 2, thanks to the closeness of the object and metalanguages, they can be confused.
As a language implementer, I find that I need to constantly remember this distinction.
So Prof. Reddy may I paraphrase you thus :-)