Executable array/tree-map representation of the same RAM #
Instruction fetch and register lookup use arrays. Sparse memory uses a balanced
Std.TreeMap keyed by decoded word addresses. Updates do not accumulate the
reference machine's nested lookup functions. Registers grow only when a write
uses a previously unallocated index; callers may preallocate their fixed
program's register bound when constructing the initial state.
The representation is not a different cost model. State.toState observes it
as the existing RAM state, and the instruction/step theorems below prove exact
correspondence. Host array growth, tree balancing, and interpreter wall time
are not additional abstract RAM transitions or performance measurements.
Register storage needed by one instruction's explicitly named operands.
Equations
- Ram.Fast.instrRegisterCapacity (Ram.Instr.const dst value) = dst + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.move dst src) = max dst src + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.binop op dst lhs rhs) = max dst (max lhs rhs) + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.load dst addrReg) = max dst addrReg + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.store addrReg src) = max addrReg src + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.jumpReg targetReg) = targetReg + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.branchZero cond target) = cond + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.read dst) = dst + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.write src) = src + 1
- Ram.Fast.instrRegisterCapacity (Ram.Instr.jump target) = 0
- Ram.Fast.instrRegisterCapacity Ram.Instr.halt = 0
Instances For
A single scan supplies an optional preallocation size. This does not limit
the semantics: out-of-capacity writes are still handled by setGrow.
Equations
- Ram.Fast.registerCapacity code = Array.foldl (fun (capacity : Nat) (i : Ram.Instr) => max capacity (Ram.Fast.instrRegisterCapacity i)) 0 code
Instances For
Total array update with zero-filled extension, preserving the reference machine's unbounded register-name semantics rather than dropping writes.
Equations
- Ram.Fast.setGrow xs i value = if h : i < xs.size then xs.set i value h else (xs ++ Array.replicate (i - xs.size) 0).push value
Instances For
Equations
- Ram.Fast.State.initial input registerCapacity = { pc := 0, regs := Array.replicate registerCapacity 0, mem := ∅, input := input, outputRev := [], status := Ram.Status.running }
Instances For
The same fixed instruction vocabulary, using the executable storage
representation. Every branch corresponds to Ram.execInstr.
Equations
- One or more equations did not get rendered due to their size.
- Ram.Fast.execInstr (Ram.Instr.const dst value) s = (s.setReg dst (BitVec.ofNat w value)).next
- Ram.Fast.execInstr (Ram.Instr.move dst src) s = (s.setReg dst (s.getReg src)).next
- Ram.Fast.execInstr (Ram.Instr.binop op dst lhs rhs) s = (s.setReg dst (op.eval (s.getReg lhs) (s.getReg rhs))).next
- Ram.Fast.execInstr (Ram.Instr.load dst addrReg) s = (s.setReg dst (s.getMem (s.getReg addrReg))).next
- Ram.Fast.execInstr (Ram.Instr.store addrReg src) s = (s.setMem (s.getReg addrReg) (s.getReg src)).next
- Ram.Fast.execInstr (Ram.Instr.jump target) s = { pc := target, regs := s.regs, mem := s.mem, input := s.input, outputRev := s.outputRev, status := s.status }
- Ram.Fast.execInstr (Ram.Instr.jumpReg targetReg) s = { pc := BitVec.toNat (s.getReg targetReg), regs := s.regs, mem := s.mem, input := s.input, outputRev := s.outputRev, status := s.status }
- Ram.Fast.execInstr Ram.Instr.halt s = { pc := s.pc, regs := s.regs, mem := s.mem, input := s.input, outputRev := s.outputRev, status := Ram.Status.halted }
Instances For
Fetch from an array; halted/fault states and invalid PCs have no successor, exactly as in the reference transition.
Equations
Instances For
Every executable instruction has exactly the reference instruction's effect, including exhausted input and control flow.
Full transition correspondence, including absence of a successor. Array fetch changes representation only; each successful transition is one RAM step.
The shared budget driver on executable storage. It retains the final state, exact transition count, and stopping reason without decoding the state during execution.
Equations
- Ram.Fast.run code budget s = Ram.Runner.runWith (Ram.Fast.step code) Ram.Fast.State.status budget s