Skip to main content
โšก Calmops

Operational Semantics: Execution and Computation Models

Introduction

Operational semantics defines the meaning of a program by specifying how it executes. Rather than assigning abstract mathematical meanings (denotational) or stating properties (axiomatic), operational semantics describes the concrete steps a program takes during execution. This approach is intuitive, directly implementable, and particularly useful for understanding program behavior and proving properties about execution.

This article explores the foundations of operational semantics, including transition systems, evaluation rules, and practical applications.

Historical Context

Operational semantics emerged from work in the 1970s and 1980s, particularly through the contributions of Gordon Plotkin and others. Plotkin’s structural operational semantics (SOS) provided a systematic framework for defining execution rules. This approach has become the standard method for specifying programming language semantics in practice.

Core Concepts

Transition Systems

A transition system models computation as a sequence of state transitions:

Definition: A transition system is a tuple (S, โ†’) where:

  • S: A set of states (configurations)
  • โ†’ โІ S ร— S: A transition relation (s โ†’ s’ means we can move from s to s')

Execution: An execution is a sequence of states sโ‚€ โ†’ sโ‚ โ†’ sโ‚‚ โ†’ … where sโ‚€ is an initial state.

Termination: A state s is terminal if there is no s’ such that s โ†’ s'.

Configurations

A configuration represents the current state of computation:

Components:

  • Current expression/statement being evaluated
  • Current environment (variable bindings)
  • Current store (memory state)
  • Call stack (for function calls)

Example: For evaluating 2 + 3:

Configuration: โŸจ2 + 3, ฯƒโŸฉ
where ฯƒ is the current state

Evaluation Relations

An evaluation relation specifies how expressions and statements are evaluated:

Notation:

  • โŸจe, ฯƒโŸฉ โ‡“ v means “expression e in state ฯƒ evaluates to value v”
  • โŸจs, ฯƒโŸฉ โ†’ โŸจs’, ฯƒ’โŸฉ means “statement s in state ฯƒ transitions to s’ in state ฯƒ'”

Structural Operational Semantics (SOS)

Structural Operational Semantics provides a systematic way to define evaluation rules using inference rules.

Inference Rules

Format:

Premiseโ‚  Premiseโ‚‚  ...  Premiseโ‚™
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        Conclusion

Interpretation: If all premises are true, then the conclusion is true.

Evaluation Rules for Expressions

Atomic Values:

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจn, ฯƒโŸฉ โ‡“ n

(A number evaluates to itself)

Variables:

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจx, ฯƒโŸฉ โ‡“ ฯƒ(x)

(A variable evaluates to its value in the state)

Binary Operations:

โŸจeโ‚, ฯƒโŸฉ โ‡“ vโ‚    โŸจeโ‚‚, ฯƒโŸฉ โ‡“ vโ‚‚
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจeโ‚ + eโ‚‚, ฯƒโŸฉ โ‡“ vโ‚ + vโ‚‚

(Evaluate both operands, then apply the operation)

Conditional:

โŸจb, ฯƒโŸฉ โ‡“ true    โŸจeโ‚, ฯƒโŸฉ โ‡“ v
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจif b then eโ‚ else eโ‚‚, ฯƒโŸฉ โ‡“ v
โŸจb, ฯƒโŸฉ โ‡“ false    โŸจeโ‚‚, ฯƒโŸฉ โ‡“ v
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจif b then eโ‚ else eโ‚‚, ฯƒโŸฉ โ‡“ v

Evaluation Rules for Statements

Skip:

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจskip, ฯƒโŸฉ โ†’ โŸจฯƒโŸฉ

(Skip does nothing, returns the state unchanged)

Assignment:

โŸจe, ฯƒโŸฉ โ‡“ v
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจx := e, ฯƒโŸฉ โ†’ โŸจฯƒ[x โ†ฆ v]โŸฉ

(Evaluate the expression, update the state)

Sequence:

โŸจsโ‚, ฯƒโŸฉ โ†’ โŸจฯƒ'โŸฉ    โŸจsโ‚‚, ฯƒ'โŸฉ โ†’ โŸจฯƒ''โŸฉ
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจsโ‚; sโ‚‚, ฯƒโŸฉ โ†’ โŸจฯƒ''โŸฉ

(Execute first statement, then second)

If Statement:

โŸจb, ฯƒโŸฉ โ‡“ true    โŸจsโ‚, ฯƒโŸฉ โ†’ โŸจฯƒ'โŸฉ
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจif b then sโ‚ else sโ‚‚, ฯƒโŸฉ โ†’ โŸจฯƒ'โŸฉ

While Loop:

โŸจb, ฯƒโŸฉ โ‡“ false
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจwhile b do s, ฯƒโŸฉ โ†’ โŸจฯƒโŸฉ
โŸจb, ฯƒโŸฉ โ‡“ true    โŸจs, ฯƒโŸฉ โ†’ โŸจฯƒ'โŸฉ    โŸจwhile b do s, ฯƒ'โŸฉ โ†’ โŸจฯƒ''โŸฉ
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจwhile b do s, ฯƒโŸฉ โ†’ โŸจฯƒ''โŸฉ

Evaluation Strategies

Different evaluation strategies lead to different operational semantics:

Eager Evaluation (Strict)

Evaluate arguments before applying functions:

โŸจeโ‚, ฯƒโŸฉ โ‡“ vโ‚    โŸจeโ‚‚, ฯƒโŸฉ โ‡“ vโ‚‚
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจf(eโ‚, eโ‚‚), ฯƒโŸฉ โ‡“ โŸฆfโŸง(vโ‚, vโ‚‚)

Characteristics:

  • All arguments evaluated
  • Predictable performance
  • May evaluate unnecessary expressions

Lazy Evaluation

Delay evaluation until needed:

โŸจeโ‚, ฯƒโŸฉ โ‡“ ฮปx. e    โŸจeโ‚‚, ฯƒโŸฉ โ‡“ vโ‚‚    โŸจe[x โ†ฆ vโ‚‚], ฯƒโŸฉ โ‡“ v
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจeโ‚(eโ‚‚), ฯƒโŸฉ โ‡“ v

Characteristics:

  • Arguments passed unevaluated
  • Avoids unnecessary computation
  • More complex to reason about

Call-by-Value vs Call-by-Name

Call-by-Value: Evaluate arguments, pass values

โŸจeโ‚, ฯƒโŸฉ โ‡“ ฮปx. e    โŸจeโ‚‚, ฯƒโŸฉ โ‡“ v
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจeโ‚(eโ‚‚), ฯƒโŸฉ โ‡“ โŸจe[x โ†ฆ v], ฯƒโŸฉ

Call-by-Name: Pass unevaluated expressions

โŸจeโ‚, ฯƒโŸฉ โ‡“ ฮปx. e
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจeโ‚(eโ‚‚), ฯƒโŸฉ โ‡“ โŸจe[x โ†ฆ eโ‚‚], ฯƒโŸฉ

Big-Step vs Small-Step Semantics

Big-Step Semantics (Natural Semantics)

Describes complete evaluation from start to finish:

Notation: โŸจe, ฯƒโŸฉ โ‡“ v (e evaluates to v in one big step)

Advantages:

  • Concise and intuitive
  • Directly implementable as interpreter
  • Easier to reason about termination

Disadvantage:

  • Difficult to express non-terminating computations
  • Hard to reason about intermediate states

Small-Step Semantics (Structural Operational Semantics)

Describes individual computation steps:

Notation: โŸจe, ฯƒโŸฉ โ†’ โŸจe’, ฯƒ’โŸฉ (one step of computation)

Advantages:

  • Handles non-termination naturally
  • Captures intermediate states
  • Useful for debugging and analysis

Disadvantage:

  • More verbose
  • Requires multiple steps for complete evaluation

Relationship: Big-step evaluation is the transitive closure of small-step:

โŸจe, ฯƒโŸฉ โ‡“ v iff โŸจe, ฯƒโŸฉ โ†’* โŸจv, ฯƒ'โŸฉ

Practical Example: Simple Imperative Language

Language:

e ::= n | x | eโ‚ + eโ‚‚ | eโ‚ * eโ‚‚
b ::= true | false | eโ‚ = eโ‚‚ | eโ‚ < eโ‚‚ | ยฌb | bโ‚ โˆง bโ‚‚
s ::= skip | x := e | sโ‚; sโ‚‚ | if b then sโ‚ else sโ‚‚ | while b do s

Small-Step Semantics:

Arithmetic:
โŸจn, ฯƒโŸฉ โ†’ n
โŸจx, ฯƒโŸฉ โ†’ ฯƒ(x)
โŸจeโ‚ + eโ‚‚, ฯƒโŸฉ โ†’ โŸจeโ‚', ฯƒโŸฉ + eโ‚‚  if โŸจeโ‚, ฯƒโŸฉ โ†’ โŸจeโ‚', ฯƒโŸฉ
โŸจvโ‚ + eโ‚‚, ฯƒโŸฉ โ†’ โŸจvโ‚ + eโ‚‚', ฯƒโŸฉ  if โŸจeโ‚‚, ฯƒโŸฉ โ†’ โŸจeโ‚‚', ฯƒโŸฉ
โŸจvโ‚ + vโ‚‚, ฯƒโŸฉ โ†’ vโ‚ + vโ‚‚

Statements:
โŸจskip, ฯƒโŸฉ โ†’ ฯƒ
โŸจx := e, ฯƒโŸฉ โ†’ ฯƒ[x โ†ฆ v]  if โŸจe, ฯƒโŸฉ โ†’* v
โŸจsโ‚; sโ‚‚, ฯƒโŸฉ โ†’ โŸจsโ‚', ฯƒ'โŸฉ; sโ‚‚  if โŸจsโ‚, ฯƒโŸฉ โ†’ โŸจsโ‚', ฯƒ'โŸฉ
โŸจskip; sโ‚‚, ฯƒโŸฉ โ†’ โŸจsโ‚‚, ฯƒโŸฉ

Applications

Language Implementation

Operational semantics directly guides interpreter implementation:

def eval_expr(e, state):
    if isinstance(e, int):
        return e
    elif isinstance(e, str):  # variable
        return state[e]
    elif e[0] == '+':
        return eval_expr(e[1], state) + eval_expr(e[2], state)
    # ... more cases

Program Verification

Prove properties about program execution:

Example: Prove that x := 5; y := x + 1 results in y = 6

โŸจx := 5; y := x + 1, ฯƒโŸฉ 
โ†’ โŸจy := x + 1, ฯƒ[x โ†ฆ 5]โŸฉ
โ†’ ฯƒ[x โ†ฆ 5, y โ†ฆ 6]

Equivalence Checking

Prove two programs are equivalent:

Example: x := 5; y := x โ‰ก x := 5; y := 5

Both evaluate to the same final state.

Debugging and Tracing

Small-step semantics enables step-by-step execution:

  • Debuggers use small-step semantics
  • Trace execution for analysis
  • Identify where errors occur

Advanced Topics

Concurrency

For concurrent programs, extend transition systems:

โŸจsโ‚, ฯƒโŸฉ โ†’ โŸจsโ‚', ฯƒ'โŸฉ
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจsโ‚ โˆฅ sโ‚‚, ฯƒโŸฉ โ†’ โŸจsโ‚' โˆฅ sโ‚‚, ฯƒ'โŸฉ

(Either process can take a step)

Exception Handling

Add exception states:

โŸจe, ฯƒโŸฉ โ†’ exception
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจtry e catch h, ฯƒโŸฉ โ†’ โŸจh, ฯƒโŸฉ

Non-Determinism

Allow multiple possible transitions:

โŸจe, ฯƒโŸฉ โ†’ โŸจeโ‚', ฯƒ'โŸฉ  or  โŸจe, ฯƒโŸฉ โ†’ โŸจeโ‚‚', ฯƒ'โŸฉ

Glossary

Big-Step Semantics: Evaluation relation describing complete computation from start to finish

Configuration: The current state of computation (expression, environment, store)

Evaluation Relation: A relation specifying how expressions and statements are evaluated

Inference Rule: A rule with premises and conclusion used to define evaluation

Operational Semantics: Defining meaning through execution steps

Small-Step Semantics: Transition relation describing individual computation steps

State: An assignment of values to variables

Structural Operational Semantics (SOS): Systematic framework for defining evaluation rules

Terminal State: A state with no outgoing transitions

Transition System: A set of states with a transition relation

Practice Problems

Problem 1: Define small-step semantics for 2 + 3 * 4 and show the evaluation steps.

Solution:

โŸจ2 + 3 * 4, ฯƒโŸฉ 
โ†’ โŸจ2 + 12, ฯƒโŸฉ  (evaluate 3 * 4)
โ†’ โŸจ14, ฯƒโŸฉ      (evaluate 2 + 12)

Problem 2: Write operational semantics rules for a repeat-until loop: repeat s until b.

Solution:

โŸจs, ฯƒโŸฉ โ†’ โŸจฯƒ'โŸฉ    โŸจb, ฯƒ'โŸฉ โ‡“ false    โŸจrepeat s until b, ฯƒ'โŸฉ โ†’ โŸจฯƒ''โŸฉ
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจrepeat s until b, ฯƒโŸฉ โ†’ โŸจฯƒ''โŸฉ

โŸจs, ฯƒโŸฉ โ†’ โŸจฯƒ'โŸฉ    โŸจb, ฯƒ'โŸฉ โ‡“ true
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โŸจrepeat s until b, ฯƒโŸฉ โ†’ โŸจฯƒ'โŸฉ

Problem 3: Prove that while false do s is equivalent to skip.

Solution:

โŸจwhile false do s, ฯƒโŸฉ โ†’ โŸจฯƒโŸฉ  (by while rule with false condition)
โŸจskip, ฯƒโŸฉ โ†’ โŸจฯƒโŸฉ
Both evaluate to the same state, so they're equivalent.

Conclusion

Operational semantics provides a concrete, intuitive way to specify and reason about program execution. By defining evaluation rules that describe how programs compute step-by-step, operational semantics bridges the gap between informal program understanding and formal mathematical reasoning.

The flexibility of operational semanticsโ€”supporting both big-step and small-step approaches, handling various evaluation strategies, and extending to concurrent and exceptional programsโ€”makes it the most widely used approach for specifying programming language semantics in practice.

Understanding operational semantics is essential for language designers, compiler developers, and anyone involved in formal verification. It provides the foundation for implementing interpreters, proving program properties, and understanding the precise behavior of programming languages.

Comments