|
| 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