Skip to content

Commit ce1d290

Browse files
committed
feat: add Operational Semantics concept
1 parent a83796a commit ce1d290

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
# Concepts
66

7+
- [Operational Semantics](concepts/operational_semantics.md)
78
- [Free Monad](concepts/free_monad.md)
89
- [Continuation-Passing Style](concepts/cps.md)
910
- [Defunctionalization](concepts/defunctionalization.md)
1011
- [Dependent types](concepts/dependent_types.md)
1112

1213
# Insights
1314

15+
- [Operational Semantics in Context](insights/operational_semantics.md)
1416
- [Mini EVM](insights/evm-alg.md)
1517
- [Free Monad (dependently)](insights/free-monad-dependent.md)
1618

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Operational Semantics
2+
3+
```admonish tip title="Related"
4+
Concepts: [Free Monad](../concepts/free_monad.md), [CPS](../concepts/cps.md), [Defunctionalization](../concepts/defunctionalization.md)
5+
Insights: [Operational Semantics in Context](../insights/operational_semantics.md)
6+
```
7+
8+
### Definition
9+
10+
**Operational semantics** defines the meaning of programs by specifying *how they execute step by step*. It models program execution as transitions between **configurations** - abstract states containing program fragments, environments, or memory.
11+
12+
## Styles of Operational Semantics
13+
14+
### Small-step (Structural Operational Semantics)
15+
16+
Computation is broken into atomic transitions:
17+
18+
```hs
19+
(2 + 3) 5
20+
(2 + 3) \* 4 5 \* 4 20
21+
```
22+
23+
Useful for modeling concurrency, interleaving, and partial execution.
24+
25+
### Big-step (Natural Semantics)
26+
Describes evaluation in terms of final results:
27+
28+
```hs
29+
(2 + 3) \* 4 20
30+
```
31+
32+
Often clearer for reasoning about terminating programs.
33+
34+
## Formal Rules
35+
36+
Operational semantics is typically given with **inference rules**.
37+
For a simple arithmetic language:
38+
39+
```hs
40+
Expr ::= n | Expr + Expr
41+
```
42+
43+
We can write:
44+
45+
```
46+
n1 + n2 → n3 (where n3 = n1 + n2)
47+
```
48+
49+
Evaluation trace:
50+
51+
```hs
52+
(1 + 2) + 3 3 + 3 6
53+
```
54+
55+
## Why It Matters
56+
57+
- Provides a **precise machine-like model** of execution.
58+
- Foundation for interpreters and virtual machines.
59+
- Supports reasoning about correctness, resource use, and safety.
60+
61+
## Relation to Other Concepts
62+
63+
- **Free Monad**: Encodes operational rules as an AST of instructions.
64+
- **CPS**: Makes control flow explicit by turning "rest of the program" into a continuation.
65+
- **Defunctionalization**: Turns continuations into explicit state transitions, directly resembling operational semantics.
66+
- **Dependent Types**: Can enrich operational rules with proofs (e.g., stack safety, gas bounds).
67+
68+
## In Practice
69+
70+
Operational semantics is how interpreters are built:
71+
- The **EVM** can be viewed as a large-step state transition system.
72+
- Small-step rules directly resemble `match` arms in a Rust interpreter.
73+
74+
```
75+
(n1 + n2) → n3
76+
```
77+
78+
can be read as Rust code:
79+
80+
```rust
81+
match expr {
82+
Add(Box::new(Num(n1)), Box::new(Num(n2))) => Num(n1 + n2),
83+
_ => expr,
84+
}
85+
```
86+
87+
## To think about
88+
89+
Thinking about programming in operational ways or using an imperative style—even with a proof—does not enable us to gain deep insights
90+
<iframe width="100%" height="315" src="https://www.youtube.com/embed/n2CBSNAVHVg?si=jx2yItB8ZvudwCr1&amp;clip=UgkxKN5lzq4a3MYoVuIS777H2gAV9GZt7wRz&amp;clipt=EMjRxAUYh-jHBQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
91+
92+
Operational thinking (implementation first) prevent us to express and improve ideas
93+
<iframe width="100%" height="315" src="https://www.youtube.com/embed/n2CBSNAVHVg?si=8imhBQPvY7AS-Nld&amp;clip=UgkxNwvDlj5QXGj_A9GAyZm4hQsq9cXeiOqe&amp;clipt=EL3qrwMYmuSxAw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Operational Semantics in Context
2+
3+
```admonish tip title="Related"
4+
Concepts: [Operational Semantics](../concepts/operational_semantics.md), [Free Monad](../concepts/free_monad.md), [CPS](../concepts/cps.md), [Defunctionalization](../concepts/defunctionalization.md)
5+
```
6+
7+
## Why Operational Semantics Matters
8+
9+
Operational semantics gives us the **step-by-step execution model** of a language.
10+
In ALUX, this plays a central role: it is the bridge between **abstract meaning** and **concrete mechanics**.
11+
12+
## Connection to Free Monads
13+
14+
A free monad describes a program as an **AST of instructions**.
15+
Interpreting this AST is essentially applying operational semantics:
16+
17+
- **Free monad**: syntax of instructions + sequencing
18+
- **Operational semantics**: rules that define how each instruction steps
19+
20+
Thus, an interpreter for a free monad is *exactly* an operational semantics defined as code.
21+
22+
## Connection to CPS
23+
24+
Continuation-Passing Style (CPS) makes the **rest of the computation** explicit.
25+
Operational semantics can be expressed in CPS:
26+
27+
- Small-step rules correspond to continuations being applied after each instruction.
28+
- The operational machine is just a CPS interpreter in which the continuation is made first-class.
29+
30+
This shows how CPS makes operational semantics executable.
31+
32+
## Connection to Defunctionalization
33+
34+
Defunctionalization takes CPS continuations and replaces them with **explicit state transitions**.
35+
This is precisely what operational semantics rules are:
36+
37+
- Continuation-as-function → State-transition-as-data
38+
- Apply function → Pattern match on instruction + next state
39+
40+
The result is a **transition system** that matches the structure of operational semantics directly.
41+
42+
## Real Example: The EVM
43+
44+
The Ethereum Virtual Machine (EVM) can be understood operationally:
45+
46+
- **State**: program counter, stack, memory, storage, gas
47+
- **Transition rules**: one for each opcode (`ADD`, `PUSH`, `SSTORE`, etc.)
48+
- **Execution**: repeatedly apply small-step rules until halting
49+
50+
In ALUX terms:
51+
- The **EVM bytecode** is a defunctionalized free monad program.
52+
- The **EVM interpreter** is its operational semantics.
53+
54+
## Dependent Types as Enriched Semantics
55+
56+
Dependent types can enrich operational semantics with proofs:
57+
58+
- **Stack safety**: `pop` only valid if stack depth > 0
59+
- **Gas constraints**: program type encodes available gas and its consumption
60+
- **Resource invariants**: state transitions guaranteed by types
61+
62+
This turns operational semantics from *rules for execution* into *proofs of correctness*.
63+
64+
## Summary
65+
66+
- Operational semantics is the **execution model** of programs.
67+
- Free monads encode the same idea as syntax + sequencing.
68+
- CPS and defunctionalization provide mechanical ways to express operational semantics.
69+
- Real systems like the EVM are defunctionalized operational semantics in practice.
70+
- Dependent types elevate semantics into **proof-carrying computations**.

0 commit comments

Comments
 (0)