Purity vs Referential transparency

前端 未结 5 1959
旧时难觅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:31

    These slides from one ACCU2015 talk have a great summary on the topic of referential transparency.

    From one of the slides:

    A language is referentially transparent if (a) every subexpression can be replaced by any other that’s equal to it in value and (b) all occurrences of an expression within a given context yield the same value.

    You can have, for instance, a function that logs its computation to the program standard output (so, it won't be a pure function), but you can replace calls for this function by a similar function that doesn't log its computation. Therefore, this function have the referential transparency property. But... the above definition is about languages, not expressions, as the slides emphasize.

    [...] it's the same as if it were pure in the first place, isn't it?

    From the definitions we have, no, it is not.

    Is there a simpler way to understand the differences between a pure expression and a referentially transparent one, if any?

    Try the slides I mentioned above.

提交回复
热议问题