A word RAM with signed addresses
Lax350013.WordRAM · concepts/Lax350013/WordRAM.lean · lax-350013
No public endorsements yet.
Loading review…
Sign in with ORCIDNatural Language Statement
Definition
A deterministic random-access machine with -bit words, signed integer addresses, modular addition, subtraction and multiplication, indirect loads and stores, a negative-word branch, and acceptance or rejection. Every instruction costs one step. The initial memory contains the input followed by zeros; all negative addresses initially contain zero. The only literal instruction writes the constant .
Concept map
Lean source view on GitHub
| 1 | /- |
| 2 | Copyright (c) 2026 Anthropic, PBC. All rights reserved. |
| 3 | Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | SPDX-License-Identifier: Apache-2.0 |
| 5 | -/ |
| 6 | /- |
| 7 | Modified for the independent Lax packaging by Édouard Bonnet, 2026. |
| 8 | Derived from 3sum-apsp/EndStatement.lean / PaperStatements.lean at upstream commit e1a4e6508154ea59f030480661590a9fe3018011. |
| 9 | Changes: Lax module/namespace layout, separated concepts and proofs, archive |
| 10 | annotations, and compatibility with the archive Lean/mathlib environment. |
| 11 | See NOTICE and README.md in the submission root for provenance and scope. |
| 12 | -/ |
| 13 | |
| 14 | |
| 15 | /-! |
| 16 | --- |
| 17 | title: A word RAM with signed addresses |
| 18 | type: definition |
| 19 | --- |
| 20 | A deterministic random-access machine with -bit words, signed integer addresses, modular addition, subtraction and multiplication, indirect loads and stores, a negative-word branch, and acceptance or rejection. Every instruction costs one step. The initial memory contains the input followed by zeros; all negative addresses initially contain zero. The only literal instruction writes the constant . |
| 21 | -/ |
| 22 | |
| 23 | namespace Lax350013.WordRAM |
| 24 | |
| 25 | |
| 26 | /-- `i j k` name cells, `l` a position in the program, `[i]` is the word in cell `i`. The only constant is 1. -/ |
| 27 | inductive Instr where |
| 28 | | one (i : Int) -- [i] := 1 |
| 29 | | add (i j k : Int) -- [i] := [j] + [k] |
| 30 | | sub (i j k : Int) -- [i] := [j] - [k] |
| 31 | | mul (i j k : Int) -- [i] := [j] * [k] |
| 32 | | load (i j : Int) -- [i] := [[j]] |
| 33 | | store (i j : Int) -- [[i]] := [j] |
| 34 | | bltz (i : Int) (l : Nat) -- if [i] < 0, go to position l |
| 35 | | accept |
| 36 | | reject |
| 37 | |
| 38 | /-- The verdict and the final memory, if `P`, run from position `pc` on memory `m`, halts within `t` steps, the halting |
| 39 | step counted; past its end `P` rejects. Addresses are read signed; `bltz` jumps on a negative word; `write` runs on. -/ |
| 40 | def exec {W : Nat} (P : List Instr) : (t pc : Nat) → (m : Int → BitVec W) → Option (Bool × (Int → BitVec W)) |
| 41 | | 0, _, _ => none |
| 42 | | t + 1, pc, m => |
| 43 | let write (i : Int) (v : BitVec W) := exec P t (pc + 1) fun x => if x = i then v else m x |
| 44 | match P.getD pc .reject with |
| 45 | | .one i => write i 1 |
| 46 | | .add i j k => write i (m j + m k) |
| 47 | | .sub i j k => write i (m j - m k) |
| 48 | | .mul i j k => write i (m j * m k) |
| 49 | | .load i j => write i (m (m j).toInt) |
| 50 | | .store i j => write (m i).toInt (m j) |
| 51 | | .bltz i l => exec P t (if (m i).toInt < 0 then l else pc + 1) m |
| 52 | | .accept => some (true, m) |
| 53 | | .reject => some (false, m) |
| 54 | |
| 55 | /-- The memory at the start: `ws` in cells 0, 1, 2, …, and 0 in every other cell. -/ |
| 56 | def loadWords (W : Nat) (ws : List Int) : Int → BitVec W := |
| 57 | fun a => if a < 0 then 0 else BitVec.ofInt W (ws.getD a.toNat 0) |
| 58 | |
| 59 | end Lax350013.WordRAM |
| 60 |
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above.
0 comments