Skip to main content

Referential Transparency (Computer Science)

If an expression can be substituted for a value without changing the program's computational result, the expression is referential transparent.

The expression needs to be pure in order to achieve Referential Transparency.

Informal Description

let PP be the referential transparent context, the Program, with P ⁣:fgP\colon f \circ g, where f ⁣:YZf\colon Y \rightarrow Z, gInj(X,Y)g \in Inj(X, Y), ff and gg are representing expressions.

if u,vY:xX:u=g(x)=vf(v)=f(y)\forall u,v \in Y: \exists x \in X: u=g(x)=v \land f(v) = f(y), then gg is referential transparent. Furthermore, it implies that fInj(Y,Z)f \in Inj(Y, Z) since u,vY:f(v)=f(v)    u=v\forall u,v \in Y: f(v) = f(v) \implies u=v.

Referential Transparency, Definiteness and Unfoldability

A more formal introduction to this topic is available from Sondergaard and Sestoft in Referential Transparency, Definiteness and Unfoldability:

"By this, an operator is referentially transparent if it preserves applicability of Leibniz's law, or substitutivity of identity: the principle that any subexpression can be replaced by any other equal in value. "

Example

For xN,yNx \in \N, y \in \N:


function f (y) {
return y * y;
}

function g (x) {
return x + 1;
}

// f(?) = 64
// 64 = 8 * 8; 64 = 8 * 8; 8 = 7 + 1; 8 = g (7); -> ? = 8 || g(7)
f(g(7)) === f (8);

see also