Operational Semantics: Rules That Run Programs

Operational semantics gives a program its meaning the same way an interpreter does: by describing how it runs. The difference is that it states that description as a small set of inference rules you can reason about on paper. You will use these rules to predict the result of an expression by hand, to check that an interpreter you write matches its specification, and to prove properties such as determinism. This lesson defines two styles of rules for one small language and shows how they relate.

A small language and its rules


e::=n∣true∣false∣e1+e2∣e1<e2∣if e then e1 else e2e ::= n \mid \text{true} \mid \text{false} \mid e_1 + e_2 \mid e_1 < e_2 \mid \text{if } e \text{ then } e_1 \text{ else } e_2


Here nn ranges over numerals. The values vv are the numerals and the two booleans: they are finished answers. Each rule has premises above a line, a conclusion below it, and a name in parentheses. A rule with nothing above the line is an axiom; every premise is itself a judgement that other rules must prove.

Big-step semantics

Big-step semantics uses the judgement e⇓ve \Downarrow v, read e evaluates to v, which relates an expression straight to its final value.


v⇓v (B-Val)e1⇓n1e2⇓n2e1+e2⇓n (B-Plus, n=n1+n2)\frac{}{v \Downarrow v}\ (\text{B-Val}) \qquad \frac{e_1 \Downarrow n_1 \quad e_2 \Downarrow n_2}{e_1 + e_2 \Downarrow n}\ (\text{B-Plus},\ n = n_1 + n_2)


e⇓truee1⇓vif e then e1 else e2⇓v (B-IfT)e⇓falsee2⇓vif e then e1 else e2⇓v (B-IfF)\frac{e \Downarrow \text{true} \quad e_1 \Downarrow v}{\text{if } e \text{ then } e_1 \text{ else } e_2 \Downarrow v}\ (\text{B-IfT}) \qquad \frac{e \Downarrow \text{false} \quad e_2 \Downarrow v}{\text{if } e \text{ then } e_1 \text{ else } e_2 \Downarrow v}\ (\text{B-IfF})


B-Lt has the same shape as B-Plus and concludes e1<e2⇓be_1 < e_2 \Downarrow b, where bb is true exactly when n1<n2n_1 < n_2. To prove a judgement, you stack rule instances into a derivation tree whose leaves are all axioms.


Example: The judgement 7+2⇓97 + 2 \Downarrow 9 needs B-Plus at the root and a B-Val leaf for each operand.


7⇓72⇓27+2⇓9 (B-Plus)\frac{\dfrac{}{7 \Downarrow 7} \quad \dfrac{}{2 \Downarrow 2}}{7 + 2 \Downarrow 9}\ (\text{B-Plus})


Small-step semantics

Small-step semantics, also called SOS, uses the judgement e⟶e′e \longrightarrow e': exactly one reduction. Computation rules do the actual work; congruence rules say where inside a larger expression the next step happens.


n1+n2⟶n (S-Plus, n=n1+n2)e1⟶e1′e1+e2⟶e1′+e2 (S-Plus1)e2⟶e2′v1+e2⟶v1+e2′ (S-Plus2)\frac{}{n_1 + n_2 \longrightarrow n}\ (\text{S-Plus},\ n = n_1 + n_2) \qquad \frac{e_1 \longrightarrow e_1'}{e_1 + e_2 \longrightarrow e_1' + e_2}\ (\text{S-Plus1}) \qquad \frac{e_2 \longrightarrow e_2'}{v_1 + e_2 \longrightarrow v_1 + e_2'}\ (\text{S-Plus2})


if true then e1 else e2⟶e1 (S-IfT)e⟶e′if e then e1 else e2⟶if e′ then e1 else e2 (S-If)\frac{}{\text{if true then } e_1 \text{ else } e_2 \longrightarrow e_1}\ (\text{S-IfT}) \qquad \frac{e \longrightarrow e'}{\text{if } e \text{ then } e_1 \text{ else } e_2 \longrightarrow \text{if } e' \text{ then } e_1 \text{ else } e_2}\ (\text{S-If})


S-IfF matches S-IfT with false and steps to e2e_2; S-Lt, S-Lt1 and S-Lt2 mirror the three addition rules. The left operand in S-Plus2 is written v1v_1, so the right operand may step only after the left is a value, which fixes left-to-right order. Running a program means chaining steps, ee ⟶∗\longrightarrow^* vv. A non-value expression with no applicable rule, such as true+1\text{true} + 1, is stuck.



Comparing the two styles

Every expression in this finite expression language eventually reaches a value or becomes stuck; it cannot diverge, meaning run forever. The comparison with looping below applies to extensions with loops or recursion, using the same styles of semantics.


Question

Big-step

Small-step

Judgement

e⇓ve \Downarrow v

e⟶e′e \longrightarrow e'

What one proof shows

the whole evaluation, as one derivation tree

one reduction; a run is a sequence of them

Evaluation order

not recorded, since premises are unordered

fixed by congruence rules such as S-Plus2

Stuck versus looping in extensions

neither has a derivation of a final value, so they look alike

a stuck run ends early; a looping run never ends

Typical use

a recursive interpreter with one case per rule

type-safety proofs by progress and preservation



What to remember

  • Operational semantics defines meaning with inference rules that describe running a program.
  • Big-step semantics proves e⇓ve \Downarrow v with one derivation tree for the whole evaluation.
  • Small-step semantics proves single reductions e⟶e′e \longrightarrow e' using computation and congruence rules; evaluation is ⟶∗\longrightarrow^*.
  • The value restriction v1v_1 in the conclusion of S-Plus2 ensures that the right operand steps only after the left becomes a value.
  • A stuck expression is a non-value with no applicable rule. In languages that can diverge, small-step distinguishes getting stuck from running forever; ordinary big-step judgements of final values do not.

Example: Tracing an if-expression in both styles

Use the rules for the small language of numerals, booleans, ++, << and if-then-else. The small-step rules are S-Plus, S-Plus1, S-Plus2, the matching S-Lt rules, S-IfT, S-IfF and S-If. The big-step rules are B-Val, B-Plus, B-Lt, B-IfT and B-IfF. Consider


e=if (2+3)<4 then 10+20 else 1+6e = \text{if } (2 + 3) < 4 \text{ then } 10 + 20 \text{ else } 1 + 6


a) Write the complete small-step sequence from ee to a value, naming the rules used in each transition.


Only the guard can move at first, so every early transition has S-If as its outermost rule.


Transition 1. The guard (2+3)<4(2 + 3) < 4 is not a value, and neither is its left operand, so S-If wraps S-Lt1, which wraps the computation rule S-Plus. The step rewrites 2 + 3 to ________.


Transition 2. Both operands of the comparison are now numerals, so S-If wraps the computation rule S-Lt, and the guard becomes ________.


Transition 3. The guard is a boolean value, so the axiom S-IfF applies at the top level, discarding the then-branch and leaving ________.


Transition 4. Both operands are numerals, so S-Plus adds them.


e⟶if 5<4 then 10+20 else 1+6(S-If, S-Lt1, S-Plus)⟶if false then 10+20 else 1+6(S-If, S-Lt)⟶1+6(S-IfF)⟶7(S-Plus)\begin{aligned} e &\longrightarrow \text{if } 5 < 4 \text{ then } 10 + 20 \text{ else } 1 + 6 && (\text{S-If, S-Lt1, S-Plus}) \\ &\longrightarrow \text{if false then } 10 + 20 \text{ else } 1 + 6 && (\text{S-If, S-Lt}) \\ &\longrightarrow 1 + 6 && (\text{S-IfF}) \\ &\longrightarrow 7 && (\text{S-Plus}) \end{aligned}


The expression reaches the value 7 after four transitions.


b) Build the big-step derivation tree that proves e⇓ve \Downarrow v, and count its rule instances.


B-IfF has two premises: the guard evaluates to false, and the else-branch evaluates to the final value. Build the guard's derivation first. B-Lt needs the premise that 2 + 3 evaluates to ________, which comes from B-Plus over two B-Val leaves, together with a B-Val leaf for 4.


D1=2⇓23⇓32+3⇓5 (B-Plus)4⇓4(2+3)<4⇓false (B-Lt)\mathcal{D}_1 = \dfrac{\dfrac{\dfrac{}{2 \Downarrow 2} \quad \dfrac{}{3 \Downarrow 3}}{2 + 3 \Downarrow 5}\ (\text{B-Plus}) \quad \dfrac{}{4 \Downarrow 4}}{(2 + 3) < 4 \Downarrow \text{false}}\ (\text{B-Lt})


The else-branch 1+61 + 6 uses B-Plus over leaves for 1 and 6. Because the guard concluded false, the root rule is ________.


D11⇓16⇓61+6⇓7 (B-Plus)e⇓7 (B-IfF)\dfrac{\mathcal{D}_1 \quad \dfrac{\dfrac{}{1 \Downarrow 1} \quad \dfrac{}{6 \Downarrow 6}}{1 + 6 \Downarrow 7}\ (\text{B-Plus})}{e \Downarrow 7}\ (\text{B-IfF})


The complete tree has 9 rule instances: the root and the guard's B-Lt node, two B-Plus nodes, and five B-Val leaves for 2, 3, 4, 1 and 6. So e⇓7e \Downarrow 7, matching the small-step result.


c) Explain why 10+2010 + 20 is never evaluated in either style, and say what would change if the then-branch were the stuck expression true+1\text{true} + 1.


In the small-step trace, S-If only ever rewrites the guard, and S-IfF is an axiom with ________, so it drops the then-branch without looking inside it. In the big-step tree, the premises of B-IfF mention only the guard and e2e_2; no premise mentions e1e_1, so no derivation for 10+2010 + 20 is ever needed.


Replacing the then-branch with true+1\text{true} + 1 therefore changes nothing: the same four transitions reach 7, and the same tree proves e⇓7e \Downarrow 7. A stuck subexpression matters only if evaluation reaches it.

Match each term used in the operational semantics of a small expression language with the description that fits it. Each description is used exactly once.

Match each term
A.

A non-value term to which no reduction rule applies, such as true + 1

B.

Describes one reduction at a time, so a run is a chain of transitions

C.

Says which subexpression takes the next step when the whole term cannot reduce directly

D.

An inference rule with no premises above the line

E.

Relates an expression directly to its final result in a single judgement

F.

A proof built by stacking rule instances until every leaf has nothing above it

Small-step semantics

Big-step semantics

Congruence rule

Stuck expression

Derivation tree

Axiom

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A language has numerals and addition with the following small-step rules, where v1v_1 stands for any value and n=n1+n2n = n_1 + n_2:


n1+n2⟶n (S-Plus)e1⟶e1′e1+e2⟶e1′+e2 (S-Plus1)e2⟶e2′v1+e2⟶v1+e2′ (S-Plus2)\frac{}{n_1 + n_2 \longrightarrow n}\ (\text{S-Plus}) \qquad \frac{e_1 \longrightarrow e_1'}{e_1 + e_2 \longrightarrow e_1' + e_2}\ (\text{S-Plus1}) \qquad \frac{e_2 \longrightarrow e_2'}{v_1 + e_2 \longrightarrow v_1 + e_2'}\ (\text{S-Plus2})


Which single transition does the expression (1+2)+(3+4)(1 + 2) + (3 + 4) take under these rules?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A language has numerals, booleans, ++, << and if-then-else with standard small-step rules. An operator reduces its left operand first, its right operand steps only once the left is a value, and a computation rule fires when both operands are numerals. An if-expression reduces its guard to a boolean, then S-IfT or S-IfF selects a branch in one step. How many single-step transitions take the expression below to a value? Answer as a whole number.


if (1+1)<3 then (2+3)+4 else 0\text{if } (1 + 1) < 3 \text{ then } (2 + 3) + 4 \text{ else } 0

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice