liveBook · Manning

Substitution model

As explained in Functional Programming in Scala, Second Edition

The substitution model is a method of evaluating expressions by replacing a function call with the result of substituting its arguments into the function body.

The substitution model evaluates a referentially transparent expression by replacing each term with an equivalent value, expanding variables and function calls until the expression reaches its simplest form. Computation therefore resembles algebra: substitute equals for equals, and preserve the program’s meaning at every step. section 1.3

The method works only when expressions have no observable effects beyond returning their values. A pure expression can be replaced by its result in any surrounding program; an operation that charges a credit card, mutates an object, or changes a builder cannot be safely replaced this way because evaluation changes the world or later results. section 1.3 section 1.5

Replacing an immutable expression

Given `val x = "Hello, World"`, evaluating `x.reverse` produces `"dlroW ,olleH"`. Substituting the value of `x` yields `"Hello, World".reverse`, which produces the same result. Repeating the expression remains equivalent because the original string is unchanged. By contrast, substituting repeated calls to a mutable `StringBuilder` changes its accumulated contents, revealing the failure of referential transparency. section 1.3

scala> val x = "Hello, World"
x: java.lang.String = Hello, World
 
scala> val r1 = x.reverse
r1: String = dlroW ,olleH
 
scala> val r2 = x.reverse     #1
r2: String = dlroW ,olleH

Compare a variable-based expression with its repeated evaluation in the Scala REPL. section 1.3

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Works through algebra-like replacement and contrasts immutable expressions with mutation, stopping short of formal operational semantics.
Examples
Uses Scala REPL evaluations with immutable strings and mutable StringBuilder values.
What it assumes
Assumes basic expressions and values while introducing referential transparency through concrete Scala evaluations.
What you can do after
Evaluate/understand whether an expression can be replaced by its value without changing program behavior.
How it connects
Connects referential transparency, pure functions, side effects, equational reasoning, and local reasoning.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.