Random-access machines #
A random-access machine (RAM) is a register machine whose registers hold natural numbers of
unbounded size, whose instructions do arithmetic on whole registers, and which can address a
register through the contents of another (indirect addressing), so that a fixed program reaches
unboundedly many registers Cook and Reckhow, Section 1. This file defines the
machine, its single steps and its runs. How long a run takes depends on how instructions are
charged, which is the subject of Cslib.RAM.time and its cost measures.
The model extends cslib's unlimited register machines (Cslib.URM): it uses the same register
contents Cslib.URM.Regs and their read/write API, a program is again a list of instructions,
a state is again a program counter with register contents, a run starts at instruction 0 with
the inputs in registers 0, 1, … (Cslib.RAM.State.init), and a state is halted once its program
counter is past the end of the program. The instruction set is that of Cook and Reckhow
Cook and Reckhow, Table I with three changes: registers hold natural numbers
rather than integers, so subtraction is truncated and the conditional jump tests for a nonzero
register; there are no input and output instructions, since the inputs sit in registers as for
URMs; and there is a multiplication instruction, which permits a separation between unit and
logarithmic time (see Cslib.RAM.exists_mul_pow_lt_of_computesInTime_logCost).
Halting and evaluation
(Cslib.RAM.Halts, Cslib.RAM.HaltsWithResult, Cslib.RAM.eval) are as for URMs, with the
output in register 0. Input initialization and output extraction incur no instruction cost.
Consequently this is a natural-number function model, not Cook and Reckhow's charged
finite-alphabet input/output convention. Complexity comparisons with string machines require
explicit encoding and initial-bitlength assumptions; no such simulation is proved here.
Design notes #
- Shared with
Cslib.URM: the register contentsCslib.URM.Regswithread,write,ofInputsandoutput, hence the conventions for inputs (registers0, 1, …) and output (register0), and the notions of rewriting (Relation.Normal,Relation.Normalizable) through which halting is characterized. - Not shared: the instructions, hence the step relation
Cslib.RAM.Step, which mirrorsCslib.URM.Step; and the states.Cslib.RAM.Statehas the same two fields asCslib.URM.State, butCslib.URM.State.isHaltedtakes a URM program, so a shared structure would leave the halting predicate of RAM programs without the dot notations.isHalted p. The halting and evaluation API repeats URM's statements and names for the RAM step relation, since cslib states it for URMs only rather than for a deterministic machine in general;evalis defined from the run (Nat.find) rather than by choice, and is computable. - Besides the single-step relation
Cslib.RAM.Stepthere is a total step functionCslib.RAM.step, which leaves a halted state fixed, and the runCslib.RAM.run p nis itsn-th iterate. Costs are sums over runs, and a run as a function of its length is what makes those sums definitions rather than relations;Cslib.RAM.steps_iff_exists_runconnects the two views.
Main definitions #
Cslib.RAM.Instr: the instructions: constants, addition, truncated subtraction, multiplication, indirect load and store, and a conditional jump.Cslib.RAM.Program,Cslib.RAM.State: programs and machine states.Cslib.RAM.Instr.exec: the effect of one instruction on a state.Cslib.RAM.step,Cslib.RAM.run: the step function and the run for a given number of steps.Cslib.RAM.Step,Cslib.RAM.Steps: the single-step relation and its reflexive transitive closure.Cslib.RAM.Halts,Cslib.RAM.HaltsWithResult: halting, and halting with a given output.Cslib.RAM.eval: evaluation as a partial function.
Main results #
Cslib.RAM.Step.deterministic: a state has at most one successor.Cslib.RAM.isHalted_iff_normal: a state is halted iff it has no successor.Cslib.RAM.run_add,Cslib.RAM.run_eq_of_le: runs compose, and are stationary once halted.Cslib.RAM.steps_iff_exists_run: the states reachable fromsare the statesrun p n s.Cslib.RAM.halts_iff_exists_run,Cslib.RAM.haltsWithResult_iff_exists_run: halting read on the run.Cslib.RAM.haltsWithResult_iff_eval: a program halts with resultaiff it evaluates toa.
References #
Instructions, programs and states #
RAM instructions, written destination first as in Cook and Reckhow's X_i ← X_j + X_k
Cook and Reckhow, Table I. The register named by a direct operand is part of
the instruction; the register named by an indirect operand is the contents of another
register.
- const (d v : ℕ) : Instr
const d vsets registerdto the constantv. - add (d i j : ℕ) : Instr
add d i jsets registerdto the sum of registersiandj. - sub (d i j : ℕ) : Instr
sub d i jsets registerdto the truncated difference of registersiandj. - mul (d i j : ℕ) : Instr
mul d i jsets registerdto the product of registersiandj. - load (d a : ℕ) : Instr
load d asets registerdto the contents of the register whose index is in registera(an indirect read). - store (a i : ℕ) : Instr
store a isets the register whose index is in registerato the contents of registeri(an indirect write). - jpos (i q : ℕ) : Instr
jpos i qjumps to instructionqif registeriis positive, and otherwise proceeds to the next instruction.
Instances For
A RAM program is a list of instructions, indexed by the program counter.
Instances For
A RAM state: a program counter (0-indexed) and register contents.
Instances For
The initial state on the given inputs: the program counter is 0, and the inputs are in
registers 0, 1, …, all other registers holding 0.
Instances For
A state is halted for a program once its program counter is past the end of the program.
Instances For
Execution #
The state reached by executing an instruction.
Instances For
One step of p from s: execute the current instruction, or stay put if s is halted.
Instances For
The single-step relation of a program, as for URMs (Cslib.URM.Step): a state that is not
halted steps to the result of executing its current instruction.
Instances For
Steps #
The step relation is deterministic: each state has at most one successor.
A state is halted iff it is normal (has no successor) for the step relation.
Runs #
Halting and evaluation #
The halting and evaluation API of Cslib.URM (URM/Execution.lean), for RAM programs.
Halting is normalizability for the step relation.
A program halts with result exactly when its run is halted after some number of steps with
result in register 0.
A program that halts with a result halts.