What is referential transparency?

2019-01-05 06:55发布

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.

13条回答
乱世女痞
2楼-- · 2019-01-05 07:04

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.

查看更多
别忘想泡老子
3楼-- · 2019-01-05 07:04

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:

x = 2 + (3 * 4)

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:

x = 2 + 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:

x = 14

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:

int add(int a, int b) {
        return a + b
    }

int mult(int a, int b) {
        return a * b;
    }

int x = add(2, mult(3, 4));

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:

int x = add(2, 12)

In the same way, add(2, 12) may be replaced with the corresponding return value, 14:

int x = 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:

int add(int a, int b) {
    int result = a + b;
    System.out.println("Returning " + result);
    return result;
}

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:

public static void main(String... args) {
    printFibs(10);
}

public static void printFibs(int limit) {
    Fibs fibs = new Fibs();
    for (int i = 0; i < limit; i++) {
        System.out.println(fibs.next());
    }
}

static class Fibs {
    private int previous = -1;
    private int last = 1;

    public Integer next() {
        last = previous + (previous = last);
        return previous + last;
    }
}

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:

  • pure functions: methods returning values and having no other effects
  • pure effects: methods returning nothing but changing something outside of them)
  • functions with side effects: methods both returning a value and changing something

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.

查看更多
地球回转人心会变
4楼-- · 2019-01-05 07:07

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

查看更多
ら.Afraid
5楼-- · 2019-01-05 07:07

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).

查看更多
仙女界的扛把子
6楼-- · 2019-01-05 07:08

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.

查看更多
Luminary・发光体
7楼-- · 2019-01-05 07:09
  1. Denotational-semantics is based on modelling languages by building domains that constitute denotable values.
  2. Functional Programmers use the term value to describe the convergence of a computation based on the rewrite rules of the language ie. its operational semantics.

In 1 there is a clarity of two languages in question:

  • the one being modelled, the object language
  • the language of modelling, the meta language

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 :-)

In the contexts of functional programming and semantics, the term Referential Transparency is not referentially transparent.

查看更多
登录 后发表回答