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 ⁣:f∘gP\colon f \circ g, where f ⁣:Y→Zf\colon Y \rightarrow Z, g∈Inj(X,Y)g \in Inj(X, Y), ff and gg are representing expressions.

if ∀u,v∈Y:∃x∈X:u=g(x)=v∧f(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 f∈Inj(Y,Z)f \in Inj(Y, Z) since ∀u,v∈Y: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 x∈N,y∈Nx \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