/- From Prove2Me, definition `SmaleNinth_BSSMachine` (https://prove2.me/definitions/f7cd5dc5-4abd-49e9-8a22-b5a0dd1fa9bf). Apache-2.0 per Prove2Me's terms. -/ import Mathlib.Data.Real.Basic import Mathlib.Data.Matrix.Mul import Mathlib.Logic.Function.Iterate /-! A register machine over the real numbers (Blum–Shub–Smale style), with unit-cost exact real arithmetic — the model in which Smale poses his 9th problem. Source: S. Smale, *Mathematical problems for the next century*, Mathematical Intelligencer 20(2):7–15, 1998, Problem 9: "Is there a polynomial-time algorithm over the real numbers which decides the feasibility of the linear system of inequalities $Ax \ge b$?" — where "algorithm over the real numbers" is the machine model of L. Blum, M. Shub, S. Smale, *On a theory of computation and complexity over the real numbers*, Bull. AMS 21(1):1–46, 1989. The model formalized here: - The machine state is a program counter together with a **bi-infinite tape** `ℤ → ℝ` of real registers (the state space `ℝ_∞` of BSS §1; a bi-infinite tape with two-sided shifts is the standard presentation giving full Turing-style access to unboundedly many registers). - A program is a finite list of instructions. Arithmetic instructions (`const`, `add`, `sub`, `mul`, `div`) act on tape cells at **fixed** addresses hard-coded in the instruction — together with the two shift instructions this yields exactly the BSS computation nodes (rational maps with built-in real machine constants, applied in a finite window that the shifts move along the tape). `jle i target` is the BSS branch node (branch on the sign test `x_i ≤ 0`); `accept` / `reject` are output nodes. - Division is Lean-total (`x / 0 = 0`), a benign totalization: a genuine BSS machine can guard every division by a sign test at no asymptotic cost. - **Cost = number of executed instructions** (unit cost per arithmetic operation, comparison, or shift — the algebraic/arithmetic complexity measure). The input is `k` real numbers laid out in tape cells `0, …, k−1`; "polynomial time" means a step count polynomial in `k`. `encodeLP` fixes the input convention for the LP feasibility problem: for an `m × n` system `Ax ≥ b`, the tape holds `m` and `n` (as reals) in cells `0, 1`, the entries of `A` row-major in cells `2, …, mn+1`, and `b` in cells `mn+2, …, mn+m+1`; all other cells are `0`. -/ namespace SmaleNinth /-- An instruction of a BSS register machine over `ℝ`: real-constant loads, field arithmetic on fixed tape addresses, two-sided tape shifts, a sign-test branch, and the two output instructions. -/ inductive BSSInstr : Type /-- `x[dst] := c` — load a machine constant (an arbitrary real, per BSS). -/ | const (dst : ℤ) (c : ℝ) /-- `x[dst] := x[i] + x[j]`. -/ | add (dst i j : ℤ) /-- `x[dst] := x[i] − x[j]`. -/ | sub (dst i j : ℤ) /-- `x[dst] := x[i] * x[j]`. -/ | mul (dst i j : ℤ) /-- `x[dst] := x[i] / x[j]` (Lean-total: division by zero yields `0`). -/ | div (dst i j : ℤ) /-- Shift the whole tape one cell to the left: new `x[k] = ` old `x[k+1]`. -/ | shiftL /-- Shift the whole tape one cell to the right: new `x[k] = ` old `x[k−1]`. -/ | shiftR /-- If `x[i] ≤ 0` jump to instruction `target`, else fall through. -/ | jle (i : ℤ) (target : ℕ) /-- Halt and accept. -/ | accept /-- Halt and reject. -/ | reject /-- A BSS program: a finite list of instructions, executed from position `0`. -/ abbrev BSSProgram := List BSSInstr /-- A machine configuration: program counter and the real-register tape. -/ structure BSSConfig where /-- The program counter (an index into the program list). -/ pc : ℕ /-- The bi-infinite tape of real registers. -/ tape : ℤ → ℝ /-- One execution step. A configuration whose `pc` carries `accept`/`reject` (or points outside the program) is halted: the step leaves it unchanged. -/ noncomputable def BSSStep (P : BSSProgram) (s : BSSConfig) : BSSConfig := match P[s.pc]? with | none => s | some ins => match ins with | .const dst c => ⟨s.pc + 1, Function.update s.tape dst c⟩ | .add dst i j => ⟨s.pc + 1, Function.update s.tape dst (s.tape i + s.tape j)⟩ | .sub dst i j => ⟨s.pc + 1, Function.update s.tape dst (s.tape i - s.tape j)⟩ | .mul dst i j => ⟨s.pc + 1, Function.update s.tape dst (s.tape i * s.tape j)⟩ | .div dst i j => ⟨s.pc + 1, Function.update s.tape dst (s.tape i / s.tape j)⟩ | .shiftL => ⟨s.pc + 1, fun k => s.tape (k + 1)⟩ | .shiftR => ⟨s.pc + 1, fun k => s.tape (k - 1)⟩ | .jle i target => if s.tape i ≤ 0 then ⟨target, s.tape⟩ else ⟨s.pc + 1, s.tape⟩ | .accept => s | .reject => s /-- The configuration reached after `t` steps of `P` from initial tape `x` (program counter starting at `0`). -/ noncomputable def BSSRun (P : BSSProgram) (x : ℤ → ℝ) (t : ℕ) : BSSConfig := (BSSStep P)^[t] ⟨0, x⟩ /-- The configuration `s` is halted with boolean output `b` — its program counter carries `accept` (for `b = true`) or `reject` (for `b = false`). Halted configurations are fixed points of `BSSStep`. -/ def BSSHaltedWith (P : BSSProgram) (s : BSSConfig) (b : Bool) : Prop := P[s.pc]? = some (if b then BSSInstr.accept else BSSInstr.reject) /-- `P`, run on initial tape `x`, halts with output `b` within `T` steps. `T` is the unit-cost (arithmetic) running time bound. -/ def BSSDecidesInTime (P : BSSProgram) (x : ℤ → ℝ) (T : ℕ) (b : Bool) : Prop := ∃ t ≤ T, BSSHaltedWith P (BSSRun P x t) b /-- The `k`th entry of the row-major listing of a matrix (junk value `0` out of range): `matrixEntryRM A (i*n + j) = A i j`. -/ noncomputable def matrixEntryRM {m n : ℕ} (A : Matrix (Fin m) (Fin n) ℝ) (k : ℕ) : ℝ := if h : k < m * n ∧ 0 < n then A ⟨k / n, by have h1 := h.1 rw [Nat.mul_comm] at h1 exact Nat.div_lt_of_lt_mul h1⟩ ⟨k % n, Nat.mod_lt _ h.2⟩ else 0 /-- The input tape encoding the LP feasibility instance `∃x, Ax ≥ b`: cell `0` holds `m`, cell `1` holds `n`, cells `2, …, mn+1` hold `A` row-major, cells `mn+2, …, mn+m+1` hold `b`, and every other cell is `0`. The input occupies `mn + m + 2` cells. -/ noncomputable def encodeLP {m n : ℕ} (A : Matrix (Fin m) (Fin n) ℝ) (b : Fin m → ℝ) : ℤ → ℝ := fun k => if k = 0 then (m : ℝ) else if k = 1 then (n : ℝ) else if 2 ≤ k ∧ k < 2 + (m : ℤ) * n then matrixEntryRM A (k - 2).toNat else if h : 2 + (m : ℤ) * n ≤ k ∧ k < 2 + (m : ℤ) * n + m then b ⟨(k - (2 + (m : ℤ) * n)).toNat, by omega⟩ else 0 end SmaleNinth