Purity vs Referential transparency

前端 未结 5 1953
旧时难觅i
旧时难觅i 2020-12-04 18:43

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 t

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 19:29

    I'll quote what John Mitchell wrote in his book Concept in programming language. I don't remember it line by line but he defines pure functional language has to pass declarative language test which is:

    "Within the scope of specific deceleration of x1,...,xn , all occurrence of an expression e containing only variables x1,...,xn have the same value."

    In short as everybody else mentioned free from side-effects or no side effects ("Lack" of side effects).

    In linguistics a name or noun phrase is considered referentially transparent if it may be replaced with the another noun phrase with same referent without changing the meaning of the sentence it contains.

    Which in 1st case holds but in 2nd case it gets too weird.

    Case 1: "I saw Walter get into his new car."

    And if Walter own a Centro then we could replace that in the given sentence as:

    "I saw Walter get into his Centro"

    Contrary to first :

    Case #2 : He was called William Rufus because of his read beard.

    Rufus means somewhat red and reference was to William IV of England.

    "He was called William IV because of his read beard." looks too awkward.

    Traditional way to say is, a language is referentially transparent if we may replace one expression with another of equal value anywhere in the program without changing the meaning of the program.

    So, referential transparency is a property of pure functional language. And if your program is free from side effects then this property will hold.

    So give it up is awesome advice but get it on might also look good in this context.

提交回复
热议问题