Counter programs with random access to the input #
A counter program (Turing.CounterProgram) is a finite list of instructions over a fixed
number of registers holding natural numbers. An instruction assigns to a register the value of an
arithmetic expression (Turing.CounterProgram.Expr: constants, registers, sums, truncated
differences, products, quotients, remainders and binary digit tests), branches on a comparison of
two expressions, branches on the input bit at the position given by an expression (or on its
absence past the end of the input), emits an output bit, or halts with a
final output word. Labels are natural numbers; a label outside the program halts.
Counter programs are the classical description of logarithmic-space algorithms: a constant number
of counters, each bounded by a polynomial in the input length, with random read-only access to
the input. A program whose registers stay at most (n + 1) ^ d on inputs of length n computes a
function in logarithmic space (Turing.CounterProgram.computableInLogSpace), as a query machine
(Turing.QueryMachine) whose steps are computed in space linear in the binary registers. For
programs that never emit, the hypotheses can be established with a reachability predicate
(Turing.CounterProgram.Reaches) that records an invariant of the states passed through
(Turing.CounterProgram.computableInLogSpace_of_reaches). Programs that emit are treated with
the reachability predicate that records the emitted bits (Turing.CounterProgram.ReachesOut).
The parts of the simulation by a query machine live in the namespace
Turing.CounterProgram.Simulation.
Main definitions #
Turing.CounterProgram.Expr,Turing.CounterProgram.Instr: expressions and instructions.Turing.CounterProgram: counter programs.Turing.CounterProgram.Config: configurations, a label and the values of the registers.Turing.CounterProgram.step,Turing.CounterProgram.Reaches: steps and reachability.Turing.CounterProgram.Running: the configurations at running instructions, with bounded registers.Turing.CounterProgram.CodeAt: a list of instructions placed at a label.Turing.CounterProgram.toQueryMachine: a counter program as a query machine.
Main results #
Turing.CounterProgram.Expr.eval_inSpace: expressions are evaluated in linear space.Turing.CounterProgram.Reaches.assign,Turing.CounterProgram.Reaches.lt,Turing.CounterProgram.Reaches.read: stepping through running instructions.Turing.CounterProgram.computableInLogSpace: counter programs with polynomially bounded registers run in logarithmic space.Turing.CounterProgram.computableInLogSpace_of_reaches: the same, for programs without emissions, from a reachability statement.
References #
Arithmetic expressions over the registers.
- const (k : ℕ) : Expr
A constant.
- reg (i : ℕ) : Expr
The value of a register.
- add (a b : Expr) : Expr
A sum.
- sub (a b : Expr) : Expr
Truncated subtraction.
- bit (a i : Expr) : Expr
A binary digit, with value zero or one.
- mul (a b : Expr) : Expr
A product.
- div (a b : Expr) : Expr
A quotient,
0for the divisor0. - mod (a b : Expr) : Expr
A remainder; the remainder of
aby0isa.
Instances For
The sum of a list of expressions.
Instances For
Instructions of counter programs, with natural numbers as labels.
- assign (i : ℕ) (e : Expr) (next : ℕ) : Instr
Assign to register
ithe value ofe, then go tonext. - lt (a b : Expr) (yes no : ℕ) : Instr
Go to
yesif the value ofais less than that ofb, tonootherwise. - read (e : Expr) (onTrue onFalse onEnd : ℕ) : Instr
Read the input bit at the position given by
e: go toonTrueontrue,onFalseonfalse, andonEndpast the end of the input. - emit (b : Bool) (next : ℕ) : Instr
Emit the bit
b, then go tonext. - halt (out : List Bool) : Instr
Halt, emitting the word
out.
Instances For
The instructions that neither emit nor halt.
Instances For
Halting with output [true], the verdict of a decider that accepts.
Instances For
Halting with output [false], the verdict of a decider that rejects.
Instances For
The largest label an instruction jumps to.
Instances For
A counter program: a list of instructions, indexed by their labels, over a fixed number of registers.
A program assembled from blocks of code writes its code as List.flatten [b₀, b₁, …]. Then
rfl quickly reads off the instruction at a label (Turing.CounterProgram.instr), and
Turing.CounterProgram.codeAt_of_prefix places a block. Written as b₀ ++ b₁ ++ ⋯, which
associates to the left, the same code makes each such lookup far slower.
The instructions; the instruction at label
cis thec-th one.- registers : ℕ
The number of registers.
Instances For
The configurations: a label and the values of the registers.
Instances For
The instruction at a label; outside the program, a halt with empty output.
Instances For
The initial configuration: label 0, all registers 0.
Instances For
The bits an instruction emits.
Instances For
The final word of a halting instruction.
Instances For
The position read in a configuration, or 0 if the instruction does not read.
Instances For
The configuration after the instruction at the current label, given the input bit read.
Instances For
The bits emitted by the instruction at the current label.
Instances For
Whether the instruction at the current label halts.
Instances For
The final word of a halting configuration.
Instances For
A counter program as a query machine.
Instances For
A step of a counter program on the input w.
Instances For
The configuration after t steps on the input w.
Instances For
Reachability #
P.Reaches w Q s s': from s, the program reaches s' on the input w, and every
configuration before s' satisfies Q. This is the reflexive and transitive closure of the steps
from configurations satisfying Q, stated with a number of steps, the form in which the runs of
Turing.CounterProgram.computableInLogSpace are given.
Instances For
One step from a configuration satisfying Q.
Running configurations #
The configurations at an assignment, a comparison or a read, with all registers at most
B.
Instances For
Stepping through an assignment.
Stepping through a comparison.
Stepping through a read.
Placing code #
P.CodeAt b l: the instructions of l are at the labels from b on.
Instances For
The blocks of a code made of blocks of a fixed length.
Code read off a program: l is at b when the code from label b on starts with l. For
code written as List.flatten of its blocks, codeAt_of_prefix ⟨_, rfl⟩ places a block.
The parts of a step, in linear space #
Expressions are evaluated in linear space in the binary registers.
The parts of the simulation of a counter program by a query machine.
Logarithmic space #
The largest label the program jumps to.
Instances For
The labels of a run are bounded by the largest label of the program.
The number of registers is fixed.
Counter programs run in logarithmic space. Let a counter program halt on every encoded
input enc a, first after time a steps, with output encOut (f a). If its registers along these
runs stay at most (n + 1) ^ d, where n is the length of the input, then f is computable in
logarithmic space.
Counter programs without emissions run in logarithmic space, stated with reachability: if
on every encoded input enc a the program reaches a halting instruction with final word
encOut (f a), passing only through assignments, comparisons and reads, and its registers stay
at most (n + 1) ^ d until then, where n is the length of the input, then f is computable in
logarithmic space.