Machines over an ordered ring (BSS model, §2)
DefinitionBSSMachineThe BSS machine model over an ordered ring, from §2 of the paper (pp. 10–13).
Rinf R is , the countable direct sum of with itself, realized as finitely
supported sequences indexed by . A PolyMapInf R is a polynomial map
of some dimension : its first coordinates are polynomials in the
first coordinates of the argument and every later coordinate is left unchanged. A machine
has finitely many nodes, each an output node, a computation node (a polynomial map on the
part plus an increment-or-reset operation on each of the two index registers), a
branch node (a polynomial in the first coordinates, with the negative branch taken when its
value is ), or a fifth node (copy the entry at the index in register into the position
named by register ). The state space is
.
On input the machine starts at its entry node with both registers at the first index, the
length of in coordinate and in coordinates . The output node is a fixed
point of the step map, so a halted computation stays halted. haltingTime is the least step
count at which the output node is reached, output is the part of the state there
(the natural projection of the normal form of §3), and outputVal its coordinate , the
answer slot used by decision machines. A set is R.E. over when it is some machine's halting
set, and decidable when it and its complement both are.
import Mathlib
/-!
# Machines over an ordered ring (Blum–Shub–Smale, §2)
Formalization of the machine model of
L. Blum, M. Shub, S. Smale, *On a theory of computation and complexity over the
real numbers: NP-completeness, recursive functions and universal machines*,
Bull. Amer. Math. Soc. **21** (1989), no. 1, 1–46, §2 (pp. 10–13).
The machine is presented in the normal form of §3 (p. 15): a single input node,
which is folded into the initial configuration, and a single kind of output node,
whose associated output map is the natural projection onto the `R^∞` part of the
state.
-/
namespace BSS
/-- `R^∞`, the countable direct sum of `R` with itself: sequences `x₀, x₁, …` of
elements of `R` with `xₖ = 0` for all sufficiently large `k`. -/
abbrev Rinf (R : Type) [CommRing R] := ℕ →₀ R
/-- The two operations a computation node may perform on an index register:
increment it, or reset it to the first index (BSS §2, p. 12: `i'(i,j) = i+1` or `1`). -/
inductive ShiftOp : Type
| inc : ShiftOp
| reset : ShiftOp
deriving DecidableEq
/-- The action of a shift operation on an index register. Registers are `0`-based,
so `reset` returns to index `0`. -/
def ShiftOp.apply : ShiftOp → ℕ → ℕ
| ShiftOp.inc, i => i + 1
| ShiftOp.reset, _ => 0
/-- A polynomial map `R^∞ → R^∞` of dimension `dim` (BSS §2, p. 10): the first
`dim` coordinates of the value are polynomials in the first `dim` coordinates of
the argument, and all further coordinates are left unchanged. -/
structure PolyMapInf (R : Type) [CommRing R] : Type where
/-- The number of active variables and coordinates of the map. -/
dim : ℕ
/-- The polynomial computing each active coordinate. -/
coord : Fin dim → MvPolynomial (Fin dim) R
/-- The map `R^∞ → R^∞` determined by a `PolyMapInf`. -/
noncomputable def PolyMapInf.eval {R : Type} [CommRing R]
(g : PolyMapInf R) (x : Rinf R) : Rinf R :=
Finsupp.onFinset (α := ℕ) (M := R) (x.support ∪ Finset.range g.dim)
(fun i : ℕ => if h : i < g.dim then
MvPolynomial.eval (fun j : Fin g.dim => x (j : ℕ)) (g.coord ⟨i, h⟩)
else x i)
(by
intro a ha
by_cases h : a < g.dim
· exact Finset.mem_union_right _ (Finset.mem_range.mpr h)
· simp only [dif_neg h] at ha
exact Finset.mem_union_left _ (Finsupp.mem_support_iff.mpr ha))
/-- The value of a branch node's test polynomial at a state (BSS §2, p. 11:
`hₙ : S → R` depends on the first `k` coordinates only). -/
noncomputable def branchValue {R : Type} [CommRing R] {k : ℕ}
(h : MvPolynomial (Fin k) R) (x : Rinf R) : R :=
MvPolynomial.eval (fun j : Fin k => x (j : ℕ)) h
/-- The nodes of a machine with `N` nodes (BSS §2, pp. 11–12). The input node of
the paper is folded into the initial configuration, so it does not appear here. -/
inductive Node (R : Type) [CommRing R] (N : ℕ) : Type
/-- An output node: no outgoing edge; the computation halts here. -/
| output : Node R N
/-- A computation node: applies a polynomial map to the `R^∞` part of the state
and a shift operation to each of the two index registers. -/
| computation (g : PolyMapInf R) (iOp jOp : ShiftOp) (next : Fin N) : Node R N
/-- A branch node: goes to `negNext` when the test polynomial is negative at the
state, and to `posNext` otherwise. -/
| branch (k : ℕ) (h : MvPolynomial (Fin k) R) (negNext posNext : Fin N) : Node R N
/-- A fifth node: copies the entry at the index held in the first register into
the position held in the second register (BSS §2, p. 12). -/
| fifth (next : Fin N) : Node R N
/-- The output nodes. -/
def Node.IsOutput {R : Type} [CommRing R] {N : ℕ} : Node R N → Prop
| Node.output => True
| Node.computation _ _ _ _ => False
| Node.branch _ _ _ _ => False
| Node.fifth _ => False
/-- A machine over `R`: finitely many nodes, together with the node at which the
computation starts (the successor of the paper's input node). -/
structure Machine (R : Type) [CommRing R] : Type where
/-- The number of nodes. -/
numNodes : ℕ
/-- The node carried by each label. -/
nodes : Fin numNodes → Node R numNodes
/-- The node the computation enters after the input map is applied. -/
entry : Fin numNodes
/-- The state space `S = ℤ⁺ + ℤ⁺ + R^∞` of a machine (BSS §2, p. 12): two index
registers together with a point of `R^∞`. -/
abbrev State (R : Type) [CommRing R] := ℕ × ℕ × Rinf R
/-- The length of `x ∈ R^∞`: the largest index carrying a nonzero entry, plus one.
The length of `0` is `1` (BSS §2, p. 12; §5, p. 23). -/
noncomputable def lengthInf {R : Type} [CommRing R] (x : Rinf R) : ℕ :=
if h : x.support.Nonempty then x.support.max' h + 1 else 1
/-- Shift `y ∈ R^∞` up by one coordinate and insert `v` in coordinate `0`. -/
noncomputable def consInf {R : Type} [CommRing R] (v : R) (y : Rinf R) : Rinf R :=
Finsupp.onFinset (α := ℕ) (M := R) (insert 0 (y.support.image (fun k : ℕ => k + 1)))
(fun i : ℕ => if i = 0 then v else y (i - 1))
(by
intro a ha
by_cases h : a = 0
· subst h
exact Finset.mem_insert_self 0 _
· simp only [if_neg h] at ha
refine Finset.mem_insert_of_mem (Finset.mem_image.mpr ⟨a - 1, ?_, ?_⟩)
· exact Finsupp.mem_support_iff.mpr ha
· omega)
/-- The state a machine over `R` starts from on input `y ∈ R^∞`: both index
registers hold the first index, coordinate `0` of the working space holds the
length of `y`, and `y` itself is placed in coordinates `1, 2, …`
(BSS §2, p. 12: the input map records the length of the input and leaves working
space). -/
noncomputable def inputState {R : Type} [CommRing R] (y : Rinf R) : State R :=
(0, 0, consInf ((lengthInf y : ℕ) : R) y)
/-- A configuration of `M`: the current node together with the current state. -/
abbrev Machine.Config {R : Type} [CommRing R] (M : Machine R) : Type :=
Fin M.numNodes × State R
/-- One step of the computation of `M`. An output node is a fixed point. -/
noncomputable def Machine.step {R : Type} [CommRing R] [LinearOrder R]
(M : Machine R) (c : M.Config) : M.Config :=
match M.nodes c.1 with
| Node.output => c
| Node.computation g iOp jOp next =>
(next, iOp.apply c.2.1, jOp.apply c.2.2.1, g.eval c.2.2.2)
| Node.branch _ h negNext posNext =>
(if branchValue h c.2.2.2 < 0 then negNext else posNext, c.2)
| Node.fifth next =>
(next, c.2.1, c.2.2.1, c.2.2.2.update c.2.2.1 (c.2.2.2 c.2.1))
/-- The configuration of `M` after `T` steps on input `y`. -/
noncomputable def Machine.run {R : Type} [CommRing R] [LinearOrder R]
(M : Machine R) (y : Rinf R) (T : ℕ) : M.Config :=
M.step^[T] (M.entry, inputState y)
/-- `M` has reached an output node on input `y` within exactly `T` steps. -/
def Machine.HaltsBy {R : Type} [CommRing R] [LinearOrder R]
(M : Machine R) (y : Rinf R) (T : ℕ) : Prop :=
(M.nodes (M.run y T).1).IsOutput
/-- The halting set `Ω_M` of `M` (BSS §2, p. 12). -/
def Machine.Halts {R : Type} [CommRing R] [LinearOrder R]
(M : Machine R) (y : Rinf R) : Prop :=
∃ T : ℕ, M.HaltsBy y T
/-- The halting time `T_M(y)`: the least `T` with `nT = N` (BSS §4, p. 19).
It is `0` when `M` does not halt on `y`. -/
noncomputable def Machine.haltingTime {R : Type} [CommRing R] [LinearOrder R]
(M : Machine R) (y : Rinf R) : ℕ :=
sInf {T : ℕ | M.HaltsBy y T}
/-- The value `φ_M(y)` output by `M` on input `y`: the `R^∞` part of the halting
state, the output map being the natural projection (BSS §3, p. 15, normal form). -/
noncomputable def Machine.output {R : Type} [CommRing R] [LinearOrder R]
(M : Machine R) (y : Rinf R) : Rinf R :=
(M.run y (M.haltingTime y)).2.2.2
/-- The first coordinate of the output, used for the answers `1` (yes) and `0` (no)
of a decision machine (BSS §5, p. 25). -/
noncomputable def Machine.outputVal {R : Type} [CommRing R] [LinearOrder R]
(M : Machine R) (y : Rinf R) : R :=
M.output y 0
/-- A set is R.E. over `R` when it is the halting set of a machine over `R`
(BSS §2, p. 13). -/
def IsREOver (R : Type) [CommRing R] [LinearOrder R] (S : Set (Rinf R)) : Prop :=
∃ M : Machine R, {y : Rinf R | M.Halts y} = S
/-- A set is decidable over `R` when it and its complement are both R.E. over `R`
(BSS §2, p. 13). -/
def IsDecidableOver (R : Type) [CommRing R] [LinearOrder R] (S : Set (Rinf R)) : Prop :=
IsREOver R S ∧ IsREOver R Sᶜ
end BSS
Read-back
What the Lean code literally says, in plain math · NON-BLIND: written by the drafting agent (Aristotle, Harmonic), not an independent auditor
⚠️ NON-BLIND READ-BACK — NOT INDEPENDENT TESTIMONY. This read-back was not written by an independent blind auditor. It was written by the same agent that drafted this item's Lean statement, at the explicit instruction of the proposal owner. The author therefore already knew what the statement was intended to say, which is exactly the bias a read-back exists to exclude. Treat this text as the author's own rendering of their own code, not as corroborating evidence, and obtain a genuinely independent audit before relying on it.
This file sets up a model of computation over a commutative ring carrying a linear order.
The ambient space. is defined as the finitely supported functions : sequences that vanish at all but finitely many indices. Indexing starts at .
Polynomial maps. A polynomial map of is a pair consisting of a natural number (its dimension) and a family of polynomials , each in variables with coefficients in . It acts on by
Note is permitted, in which case the map is the identity. A branch value is for a polynomial in variables; is permitted, giving a constant.
Nodes and machines. For a natural number , a node is exactly one of four things: an output node; a computation node carrying a polynomial map , two tags each equal to either increment or reset, and a successor label in ; a branch node carrying a number , a polynomial in variables and two successor labels; or a fifth node carrying a successor label. A machine over is a natural number , a family of nodes labelled whose successors are labels in that same range, and a distinguished entry label. Since the entry label must be an element of , there are no machines with .
States. A state is a triple with and . A configuration is a node label together with a state.
Length and the initial state. The length of is when , and is when . Given , the initial state on input is where is the image in of the natural number and for every . Both index registers therefore start at .
One step. From a configuration whose label carries node :
- if is an output node, the configuration is unchanged (output nodes are fixed points);
- if is a computation node with map , tags and successor , the new configuration is , where increment sends to and reset sends to ;
- if is a branch node with polynomial and successors , the state is unchanged and the new label is when and otherwise (so the test is strict, and takes the positive branch);
- if is a fifth node with successor , the new configuration is where agrees with except that . Note the direction: the entry at the index held in the first register is written into the position held in the second.
Running. is the configuration obtained by applying the step map times to the initial configuration at the entry label. halts by on when the label of carries an output node; halts on when it halts by some .
Halting time and output. The halting time of on is the infimum of as a set of natural numbers. This is a total function: when never halts on the set is empty and the infimum is , so the halting time is both for a machine that halts immediately and for one that never halts. The output of on is the component of the configuration reached after exactly that many steps, and the output value is coordinate of the output. Both are likewise total and take junk values on non-halting inputs.
R.E. and decidable. A subset is R.E. over when there exists a machine over whose set of halting inputs is exactly . It is decidable over when both and its complement are R.E. over .