From 6e3a742c52769356bf6a0914349ea07f99a7f3e0 Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Tue, 20 Oct 2026 12:07:33 -0400 Subject: [PATCH] feat(interop): bridge Mathlib TM2 computations to canonical FP Prove quadratic transition overhead for the existing Mathlib stack-to-tape compiler using the RationalHitting simulation tools in place. Add binary and finite-alphabet input/output adapters, malformed-code rejection, and DFA-guarded FP transfer. Include API regressions, blueprint entries, documentation, and axiom checks. --- AGENTS.md | 2 +- CONTRIBUTING.md | 4 +- Complexitylib/Encoding/FiniteAlphabet.lean | 72 +++++ .../Encoding/FiniteAlphabet/Defs.lean | 70 +++++ .../Encoding/FiniteAlphabet/Internal.lean | 153 ++++++++++ Complexitylib/Interop.lean | 21 ++ Complexitylib/Interop/Cslib/Guard.lean | 78 ++++++ Complexitylib/Interop/Cslib/Guard/Defs.lean | 60 ++++ .../Interop/Cslib/Guard/Internal.lean | 172 ++++++++++++ .../Interop/Mathlib/TM0/Alphabet.lean | 70 +++++ .../Interop/Mathlib/TM0/Alphabet/Defs.lean | 151 ++++++++++ .../Mathlib/TM0/Alphabet/Internal/Copy.lean | 218 +++++++++++++++ .../TM0/Alphabet/Internal/Emission.lean | 148 ++++++++++ .../TM0/Alphabet/Internal/Initialization.lean | 141 ++++++++++ .../Mathlib/TM0/Alphabet/Internal/Output.lean | 109 ++++++++ .../TM0/Alphabet/Internal/Parsing.lean | 167 +++++++++++ .../TM0/Alphabet/Internal/Simulation.lean | 60 ++++ .../Interop/Mathlib/TM0/Internal/Output.lean | 34 +++ Complexitylib/Interop/Mathlib/TM0/Marked.lean | 64 +++++ .../Interop/Mathlib/TM0/Marked/Defs.lean | 125 +++++++++ .../TM0/Marked/Internal/Initialization.lean | 209 ++++++++++++++ .../Mathlib/TM0/Marked/Internal/Output.lean | 57 ++++ .../TM0/Marked/Internal/Simulation.lean | 76 +++++ Complexitylib/Interop/Mathlib/TM2.lean | 91 ++++++ Complexitylib/Interop/Mathlib/TM2/Defs.lean | 25 ++ Complexitylib/Interop/Mathlib/TM2/FP.lean | 82 ++++++ .../Interop/Mathlib/TM2/FP/Defs.lean | 62 +++++ .../TM2/FP/Internal/AlphabetCompiler.lean | 140 ++++++++++ .../Mathlib/TM2/FP/Internal/Compiler.lean | 66 +++++ .../Mathlib/TM2/FP/Internal/Encoding.lean | 71 +++++ .../Mathlib/TM2/FP/Internal/Guard.lean | 71 +++++ .../Mathlib/TM2/Internal/Compiler.lean | 199 +++++++++++++ .../Mathlib/TM2/Internal/Simulation.lean | 74 +++++ ROADMAP.md | 27 +- blueprint/src/chapters/polytime.tex | 134 +++++++++ docs/OpenAIMath.md | 19 ++ docs/OpenAIMathTriage.md | 27 +- lakefile.toml | 2 + scripts/AxiomGuard.lean | 23 ++ scripts/TM2ApiCheck.lean | 53 ++++ scripts/TM2FPApiCheck.lean | 261 ++++++++++++++++++ 41 files changed, 3674 insertions(+), 14 deletions(-) create mode 100644 Complexitylib/Encoding/FiniteAlphabet.lean create mode 100644 Complexitylib/Encoding/FiniteAlphabet/Defs.lean create mode 100644 Complexitylib/Encoding/FiniteAlphabet/Internal.lean create mode 100644 Complexitylib/Interop/Cslib/Guard.lean create mode 100644 Complexitylib/Interop/Cslib/Guard/Defs.lean create mode 100644 Complexitylib/Interop/Cslib/Guard/Internal.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet/Defs.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Copy.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Emission.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Initialization.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Output.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Parsing.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Simulation.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Marked.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Marked/Defs.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Initialization.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Output.lean create mode 100644 Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Simulation.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/Defs.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/FP.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/FP/Defs.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/FP/Internal/AlphabetCompiler.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/FP/Internal/Compiler.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/FP/Internal/Encoding.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/FP/Internal/Guard.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/Internal/Compiler.lean create mode 100644 Complexitylib/Interop/Mathlib/TM2/Internal/Simulation.lean create mode 100644 scripts/TM2ApiCheck.lean create mode 100644 scripts/TM2FPApiCheck.lean diff --git a/AGENTS.md b/AGENTS.md index 9a00e3dd3..e81533dc8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -27,7 +27,7 @@ lake build --wfail Complexitylib \ ``` Always verify the library, all five executable validation roots, and the -seven isolated `ApiChecks` modules pass before considering a change complete. +nine isolated `ApiChecks` modules pass before considering a change complete. One invocation shares Lake's dependency-graph work across the targets and builds the upstream `runLinter` executable needed below. Validation and API regressions remain outside the public import graph. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2484ebf2a..c91611377 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -167,7 +167,7 @@ lake env lean scripts/BlueprintCheck.lean All commands must pass before submitting changes; CI runs them on every push. The build checks the complete library, all five executable regression suites, -and the seven separate API-check modules, treating warnings (including proof +and the nine separate API-check modules, treating warnings (including proof placeholders) as failures. Both kinds of regressions remain outside the public import graph. One Lake invocation shares dependency-graph setup; unchanged API checks reuse normal Lake traces instead of re-elaborating every time. @@ -237,7 +237,7 @@ on every contributor. For an isolated API regression, use `lake build --wfail VerifierApiCheck` (or the corresponding module name). `lake build --wfail ApiChecks` runs all -seven as separate modules, preserving each file's public-only or compatibility +nine as separate modules, preserving each file's public-only or compatibility imports. Lake rebuilds relevant changed imports before reusing a test artifact. The axiom and blueprint scripts are still executed on every full verification; they are deliberately excluded from the cached API target. diff --git a/Complexitylib/Encoding/FiniteAlphabet.lean b/Complexitylib/Encoding/FiniteAlphabet.lean new file mode 100644 index 000000000..81134daba --- /dev/null +++ b/Complexitylib/Encoding/FiniteAlphabet.lean @@ -0,0 +1,72 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Encoding.FiniteAlphabet.Defs +import Complexitylib.Encoding.FiniteAlphabet.Internal + +/-! +# Exact binary coding of finite alphabets + +An explicit enumeration gives a delimited, palindromic unary code for each +letter. Encodings are injective, have linear length bounds, commute with word +reversal, and are exactly the strings accepted by the partial decoder. Empty +alphabets are supported, and only the empty word then has a valid encoding. +-/ + +public section + +namespace Complexity.FiniteAlphabetCode + +variable {α β : Type} (c : FiniteAlphabetCode α) + +/-- Every encoded word is decoded exactly. -/ +@[simp] theorem decode_encode (w : List α) : c.decode (c.encode w) = some w := + decode_encode_proof c w + +/-- The decoder accepts precisely the chosen encodings. -/ +theorem decode_eq_some_iff {bits : List Bool} {w : List α} : + c.decode bits = some w ↔ c.encode w = bits := by + constructor + · exact encode_decode_proof c + · rintro rfl + exact c.decode_encode w + +/-- Encoding distinct words gives distinct binary strings. -/ +theorem encode_injective : Function.Injective c.encode := by + intro xs ys h + have he := congrArg c.decode h + simpa only [decode_encode, Option.some.injEq] using he + +/-- Encoded size is at least the source word length. -/ +theorem length_le_encode (w : List α) : w.length ≤ (c.encode w).length := + encode_length_lower c w + +/-- The alphabet enumeration determines a constant linear size bound. -/ +theorem length_encode_le (w : List α) : + (c.encode w).length ≤ (c.size + 1) * w.length := + encode_length_upper c w + +/-- Palindromic symbol codes commute with reversal of the whole word. -/ +theorem encode_reverse_eq (w : List α) : c.encode w.reverse = (c.encode w).reverse := + encode_reverse c w + +/-- Reversal preserves validity and reverses the decoded letters. -/ +theorem decode_reverse_eq (bits : List Bool) : + c.decode bits.reverse = (c.decode bits).map List.reverse := + decode_reverse c bits + +/-- A coded function has exactly its specified output on every encoded input. -/ +@[simp] theorem map_encode (output : FiniteAlphabetCode β) (f : List α → List β) (w : List α) : + c.map output f (c.encode w) = output.encode (f w) := by + simp [map] + +/-- A coded function returns empty output on every invalid code. -/ +theorem map_invalid (output : FiniteAlphabetCode β) (f : List α → List β) + {bits : List Bool} (h : c.decode bits = none) : c.map output f bits = [] := by + simp [map, h] + +end Complexity.FiniteAlphabetCode diff --git a/Complexitylib/Encoding/FiniteAlphabet/Defs.lean b/Complexitylib/Encoding/FiniteAlphabet/Defs.lean new file mode 100644 index 000000000..21ff9a34f --- /dev/null +++ b/Complexitylib/Encoding/FiniteAlphabet/Defs.lean @@ -0,0 +1,70 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Mathlib.Data.Fintype.EquivFin +public import Mathlib.Data.List.Basic + +/-! +# Explicit binary codes for finite alphabets + +An enumeration assigns index `i` to the palindromic codeword `false, true^i, +false`. Concatenation encodes words, including the empty word. Decoding rejects +missing delimiters and indices outside the enumeration. Reversing an encoding +therefore reverses its letters without changing their codes. +-/ + +@[expose] public section + +namespace Complexity + +/-- An explicit enumeration of an alphabet, including an empty alphabet. -/ +structure FiniteAlphabetCode (α : Type) where + /-- Number of letters. -/ + size : ℕ + /-- Chosen numbering of the letters. -/ + index : α ≃ Fin size + +namespace FiniteAlphabetCode + +/-- Choose a code from a finite-alphabet instance. -/ +noncomputable def ofFintype (α : Type) [Fintype α] : FiniteAlphabetCode α := + ⟨Fintype.card α, Fintype.equivFin α⟩ + +/-- A delimited unary symbol code, identical to its reversal. -/ +def symbol {α : Type} (c : FiniteAlphabetCode α) (a : α) : List Bool := + false :: List.replicate (c.index a).val true ++ [false] + +/-- Concatenate the symbol codes of a word. -/ +def encode {α : Type} (c : FiniteAlphabetCode α) (w : List α) : List Bool := + w.flatMap c.symbol + +/-- Decode a suffix while waiting for an opening delimiter or counting an open codeword. -/ +def decodeFrom {α : Type} (c : FiniteAlphabetCode α) : Option ℕ → List Bool → Option (List α) + | none, [] => some [] + | some _, [] => none + | none, false :: bs => c.decodeFrom (some 0) bs + | none, true :: _ => none + | some i, true :: bs => + if i + 1 < c.size then c.decodeFrom (some (i + 1)) bs else none + | some i, false :: bs => + if h : i < c.size then + (c.decodeFrom none bs).map (c.index.symm ⟨i, h⟩ :: ·) + else none + +/-- Decode an entire word, rejecting every malformed code. -/ +def decode {α : Type} (c : FiniteAlphabetCode α) (bits : List Bool) : Option (List α) := + c.decodeFrom none bits + +/-- Encode a function's result on valid inputs and return empty output on invalid codes. -/ +def map {α β : Type} (input : FiniteAlphabetCode α) (output : FiniteAlphabetCode β) + (f : List α → List β) (bits : List Bool) : List Bool := + match input.decode bits with + | none => [] + | some w => output.encode (f w) + +end FiniteAlphabetCode +end Complexity diff --git a/Complexitylib/Encoding/FiniteAlphabet/Internal.lean b/Complexitylib/Encoding/FiniteAlphabet/Internal.lean new file mode 100644 index 000000000..38cd308a4 --- /dev/null +++ b/Complexitylib/Encoding/FiniteAlphabet/Internal.lean @@ -0,0 +1,153 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Encoding.FiniteAlphabet.Defs +import Mathlib.Tactic.Linarith + +/-! +# Correctness and size of finite-alphabet codes + +Delimited unary parsing is exact. Its codeword lengths are bounded by an +alphabet-dependent constant and its palindromic framing commutes with reversal. +-/ + +@[expose] public section + +namespace Complexity.FiniteAlphabetCode + +variable {α : Type} (c : FiniteAlphabetCode α) + +lemma encode_nil : c.encode [] = [] := rfl + +lemma encode_cons (a : α) (w : List α) : c.encode (a :: w) = c.symbol a ++ c.encode w := rfl + +lemma symbol_length (a : α) : (c.symbol a).length = (c.index a).val + 2 := by + simp [symbol, Nat.add_assoc] + +lemma decodeFrom_trues (i k : ℕ) (h : i + k < c.size) (rest : List Bool) : + c.decodeFrom (some i) (List.replicate k true ++ rest) = + c.decodeFrom (some (i + k)) rest := by + induction k generalizing i with + | zero => simp + | succ k ih => + rw [List.replicate_succ, List.cons_append, decodeFrom, ite_eq_left (by omega)] + simpa [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using ih (i + 1) (by omega) + +lemma decodeFrom_symbol (a : α) (rest : List Bool) : + c.decodeFrom none (c.symbol a ++ rest) = + (c.decodeFrom none rest).map (a :: ·) := by + simp only [symbol, List.cons_append, decodeFrom, List.append_assoc] + rw [decodeFrom_trues c 0 (c.index a).val (by omega)] + simp [decodeFrom] + +lemma decode_encode_proof (w : List α) : c.decode (c.encode w) = some w := by + change c.decodeFrom none (c.encode w) = some w + induction w with + | nil => rfl + | cons a w ih => + rw [encode_cons, decodeFrom_symbol, ih] + rfl + +lemma encode_length_lower (w : List α) : w.length ≤ (c.encode w).length := by + induction w with + | nil => simp [encode] + | cons a w ih => + rw [encode_cons, List.length_append, symbol_length] + simp only [List.length_cons] + omega + +lemma encode_length_upper (w : List α) : + (c.encode w).length ≤ (c.size + 1) * w.length := by + induction w with + | nil => simp [encode] + | cons a w ih => + have ha := (c.index a).isLt + rw [encode_cons, List.length_append, symbol_length] + simp only [List.length_cons, Nat.mul_add, Nat.mul_one] + omega + +lemma symbol_reverse (a : α) : (c.symbol a).reverse = c.symbol a := by + simp [symbol, List.reverse_append, List.reverse_replicate] + +lemma encode_append (xs ys : List α) : c.encode (xs ++ ys) = c.encode xs ++ c.encode ys := by + simp [encode] + +lemma encode_reverse (w : List α) : c.encode w.reverse = (c.encode w).reverse := by + induction w with + | nil => rfl + | cons a w ih => + simp only [List.reverse_cons, encode_append, encode_cons, encode_nil, + List.append_nil, ih, List.reverse_append, symbol_reverse] + +lemma encode_count_false (w : List α) : (c.encode w).count false = 2 * w.length := by + induction w with + | nil => simp [encode] + | cons a w ih => + have hz : (List.replicate (c.index a).val true).count false = 0 := + List.count_eq_zero_of_not_mem (by simp) + rw [encode_cons, List.count_append, ih] + simp [symbol, List.count_append, hz, Nat.mul_add, Nat.add_comm] + +/-- Prefix already consumed when the parser is inside a codeword. -/ +def pendingPrefix : Option ℕ → List Bool + | none => [] + | some i => false :: List.replicate i true + +lemma decodeFrom_exact (bits : List Bool) (pending : Option ℕ) (w : List α) + (h : c.decodeFrom pending bits = some w) : + pendingPrefix pending ++ bits = c.encode w := by + induction bits generalizing pending w with + | nil => + cases pending with + | none => simp only [decodeFrom, Option.some.injEq] at h; subst w; rfl + | some i => simp [decodeFrom] at h + | cons b bits ih => + cases pending with + | none => + cases b with + | true => simp [decodeFrom] at h + | false => simpa [pendingPrefix] using ih (some 0) w h + | some i => + cases b with + | true => + simp only [decodeFrom] at h + split_ifs at h with hi + · simpa [pendingPrefix, List.replicate_add, List.append_assoc] using + ih (some (i + 1)) w h + | false => + simp only [decodeFrom] at h + split_ifs at h with hi + · cases hr : c.decodeFrom none bits with + | none => simp [hr] at h + | some rest => + simp only [hr, Option.map_some, Option.some.injEq] at h + subst w + have he := ih none rest hr + simpa [pendingPrefix, encode, symbol, List.append_assoc] using + congrArg (fun bs => (false :: List.replicate i true) ++ false :: bs) he + +lemma encode_decode_proof {bits : List Bool} {w : List α} + (h : c.decode bits = some w) : c.encode w = bits := + (decodeFrom_exact c bits none w h).symm + +lemma decode_reverse (bits : List Bool) : + c.decode bits.reverse = (c.decode bits).map List.reverse := by + cases hw : c.decode bits with + | some w => + rw [← encode_decode_proof c hw, ← encode_reverse, decode_encode_proof] + rfl + | none => + cases hr : c.decode bits.reverse with + | none => rfl + | some w => + have he : c.encode w.reverse = bits := by + rw [encode_reverse, encode_decode_proof c hr, List.reverse_reverse] + have hc := decode_encode_proof c w.reverse + rw [he, hw] at hc + contradiction + +end Complexity.FiniteAlphabetCode diff --git a/Complexitylib/Interop.lean b/Complexitylib/Interop.lean index f52a76e7f..208099db6 100644 --- a/Complexitylib/Interop.lean +++ b/Complexitylib/Interop.lean @@ -12,8 +12,13 @@ public import Complexitylib.Interop.Cslib.FromMultiTape.PEquiv public import Complexitylib.Interop.Cslib.Circuit public import Complexitylib.Interop.Cslib.CircuitClasses public import Complexitylib.Interop.Cslib.CircuitDepth +public import Complexitylib.Interop.Cslib.Guard public import Complexitylib.Interop.Mathlib.TM0 public import Complexitylib.Interop.Mathlib.TM0.Guard +public import Complexitylib.Interop.Mathlib.TM0.Marked +public import Complexitylib.Interop.Mathlib.TM0.Alphabet +public import Complexitylib.Interop.Mathlib.TM2 +public import Complexitylib.Interop.Mathlib.TM2.FP /-! # Interoperability @@ -42,4 +47,20 @@ Aggregation module for bridges to CSLib and Mathlib. output length on every input imply canonical `FP` membership. Its `Guard` interface validates regular input formats and returns empty output on malformed strings, extending source bounds on valid inputs to `FP`. +- `Complexitylib.Interop.Mathlib.TM2` proves quadratic transition overhead for + Mathlib's existing stack-to-tape compiler, preserving the complete stack-track + input and output tapes of bundled `FinTM2` computations. +- `Complexitylib.Interop.Mathlib.TM2.FP` transfers total binary-string TM2 + certificates with finite working alphabets to canonical `FP`, including the + marked empty-input representation and both reversals of stack order. + Its DFA-guarded theorem requires source bounds only on valid strings and + returns empty output on malformed strings. + Its encoded interface supports independent finite input and output alphabets, + including empty alphabets, through explicit palindromic symbol codes. +- `Complexitylib.Interop.Mathlib.TM0.Marked` handles distinct input and output + formats, including first-cell and empty-input markers. +- `Complexitylib.Interop.Mathlib.TM0.Alphabet` parses and emits codes for finite + source alphabets, rejecting invalid symbol codes before source execution. +- `Complexitylib.Interop.Cslib.Guard` validates regular input domains before + starting an arbitrary binary CSLib machine. -/ diff --git a/Complexitylib/Interop/Cslib/Guard.lean b/Complexitylib/Interop/Cslib/Guard.lean new file mode 100644 index 000000000..0518594b2 --- /dev/null +++ b/Complexitylib/Interop/Cslib/Guard.lean @@ -0,0 +1,78 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Cslib.Guard.Defs +public import Complexitylib.Interop.Cslib.FromMultiTape +import Complexitylib.Interop.Cslib.Guard.Internal + +/-! +# Polynomial-time computation on a regular input domain + +A DFA guard extends a binary CSLib computation to all strings, returning empty +output outside its regular domain. Only accepted inputs need source runtime +bounds, since rejection never starts the source machine. +-/ + +public section + +namespace Complexity.CslibGuard + +/-- Accepted inputs incur just linear validation and rewind overhead. -/ +theorem computes_accepted {k : ℕ} {σ Q : Type} + (M : Turing.MultiTapeTM k Bool Q) (D : DFA Bool σ) + {w output : List Bool} {t s : ℕ} (hw : w ∈ D.accepts) + (h : M.ComputesInTimeAndSpace w output t s) : + (machine M D).ComputesInTimeAndSpace w output (2 * w.length + t + 2) + ((machine M D).spaceUsed ((machine M D).initCfg w) (2 * w.length + t + 2)) := by + have hr : (machine M D).runFrom ((machine M D).initCfg w) (2 * w.length + t + 2) = + liftCfg (σ := σ) (M.runFrom (M.initCfg w) t) := by + rw [show 2 * w.length + t + 2 = (2 * w.length + 2) + t by lia, + run_add, prepare M D hw, run_lift] + refine ⟨?_, ?_, rfl⟩ + · rw [hr] + simp only [liftCfg, Turing.Cfg.mapState, h.1, Option.map_none] + · rw [hr] + exact h.2.1 + +/-- Rejected inputs halt with empty output after a single scan. -/ +theorem computes_rejected {k : ℕ} {σ Q : Type} + (M : Turing.MultiTapeTM k Bool Q) (D : DFA Bool σ) + {w : List Bool} (hw : w ∉ D.accepts) : + (machine M D).ComputesInTimeAndSpace w [] (w.length + 1) + ((machine M D).spaceUsed ((machine M D).initCfg w) (w.length + 1)) := + ⟨(rejected M D hw).1, (rejected M D hw).2, rfl⟩ + +/-- Polynomial source bounds on a regular domain give a total canonical FP function. -/ +theorem mem_FP_on_dfa {k : ℕ} {σ Q : Type} [Fintype σ] [Fintype Q] + (M : Turing.MultiTapeTM k Bool Q) (D : DFA Bool σ) + [DecidablePred (· ∈ D.accepts)] {f : List Bool → List Bool} {p : Polynomial ℕ} + (h : ∀ w ∈ D.accepts, ∃ t ≤ p.eval w.length, ∃ s, + M.ComputesInTimeAndSpace w (f w) t s) : + (fun w => if w ∈ D.accepts then f w else []) ∈ FP := by + let time (w : List Bool) := 2 * w.length + p.eval w.length + 2 + refine mem_FP_of_computableInTimeAndSpace + (p := Polynomial.C 2 * Polynomial.X + p + Polynomial.C 2) + (s := fun w => (machine M D).spaceUsed ((machine M D).initCfg w) (time w)) + ⟨k, State σ Q, inferInstance, machine M D, ?_⟩ + intro w + refine ⟨time w, by simp [time], _, le_rfl, ?_⟩ + change (machine M D).ComputesInTimeAndSpace w (if w ∈ D.accepts then f w else []) + (time w) ((machine M D).spaceUsed ((machine M D).initCfg w) (time w)) + by_cases hw : w ∈ D.accepts + · rw [ite_eq_left hw] + obtain ⟨t, ht, s, hs⟩ := h w hw + have hc := computes_accepted M D hw hs + have heq := (machine M D).runFrom_eq_of_halt ((machine M D).initCfg w) + (by dsimp [time]; lia : 2 * w.length + t + 2 ≤ time w) hc.1 + exact ⟨by rw [heq]; exact hc.1, by rw [heq]; exact hc.2.1, rfl⟩ + · rw [ite_eq_right hw] + have hc := computes_rejected M D hw + have heq := (machine M D).runFrom_eq_of_halt ((machine M D).initCfg w) + (by dsimp [time]; lia : w.length + 1 ≤ time w) hc.1 + exact ⟨by rw [heq]; exact hc.1, by rw [heq]; exact hc.2.1, rfl⟩ + +end Complexity.CslibGuard diff --git a/Complexitylib/Interop/Cslib/Guard/Defs.lean b/Complexitylib/Interop/Cslib/Guard/Defs.lean new file mode 100644 index 000000000..8bb2bf858 --- /dev/null +++ b/Complexitylib/Interop/Cslib/Guard/Defs.lean @@ -0,0 +1,60 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Cslib.Computability.Machines.Turing.MultiTape.Deterministic +public import Mathlib.Computability.DFA +public import Mathlib.Tactic.DeriveFintype + +/-! +# A regular-domain guard for binary CSLib machines + +The guard scans the read-only input with a DFA. Rejection halts with empty +output. Acceptance rewinds the input and starts the original machine with +untouched work tapes and output. +-/ + +@[expose] public section + +namespace Complexity.CslibGuard + +/-- Validation, rewind, and source execution control. -/ +inductive State (σ Q : Type) where + /-- Scan using the current DFA state. -/ + | scan : σ → State σ Q + /-- Return to the left input boundary. -/ + | rewind + /-- Execute the source machine. -/ + | run : Q → State σ Q + deriving Fintype + +/-- Change control and input position without changing work tapes or output. -/ +def idle {k : ℕ} {σ Q : Type} (q : Option (State σ Q)) (move : SignType) : + Turing.Action k Bool (State σ Q) where + state := q + inputTape := move + output := none + workTapes := fun _ => (none, 0) + +/-- Validate a regular input format before executing a binary CSLib machine. -/ +noncomputable def machine {k : ℕ} {σ Q : Type} + (M : Turing.MultiTapeTM k Bool Q) (D : DFA Bool σ) : + Turing.MultiTapeTM k Bool (State σ Q) := by + classical + exact { + q₀ := .scan D.start + tr := fun q input work => match q with + | .scan q => match input with + | some b => idle (some (.scan (D.step q b))) 1 + | none => if q ∈ D.accept then idle (some .rewind) (-1) else idle none 0 + | .rewind => match input with + | some _ => idle (some .rewind) (-1) + | none => idle (some (.run M.q₀)) 1 + | .run q => + let a := M.tr q input work + { a with state := a.state.map State.run } } + +end Complexity.CslibGuard diff --git a/Complexitylib/Interop/Cslib/Guard/Internal.lean b/Complexitylib/Interop/Cslib/Guard/Internal.lean new file mode 100644 index 000000000..75a344a2f --- /dev/null +++ b/Complexitylib/Interop/Cslib/Guard/Internal.lean @@ -0,0 +1,172 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Cslib.Guard.Defs + +/-! +# Correctness and linear cost of regular-domain validation + +The DFA scan and input rewind leave every work tape and the output empty. +The execution phase then reproduces the original machine's complete run. +-/ + +@[expose] public section + +namespace Complexity.CslibGuard + +variable {k : ℕ} {σ Q : Type} (M : Turing.MultiTapeTM k Bool Q) (D : DFA Bool σ) + +/-- An otherwise initial configuration at a specified input position and control. -/ +def baseCfg (w : List Bool) (q : Option (State σ Q)) (p : Fin (w.length + 2)) : + Turing.Cfg k Bool (State σ Q) w := ⟨q, p, fun _ _ => none, fun _ => 0, []⟩ + +/-- The DFA scan after the first `t` input bits. -/ +def scanCfg (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + Turing.Cfg k Bool (State σ Q) w := + baseCfg w (some (.scan (D.eval (w.take t)))) ⟨t + 1, by lia⟩ + +/-- Rewinding with the input head at native position `t`. -/ +def rewindCfg (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + Turing.Cfg k Bool (State σ Q) w := baseCfg w (some .rewind) ⟨t, by lia⟩ + +/-- Embed source execution without changing any tape or head. -/ +def liftCfg {w : List Bool} (c : Turing.Cfg k Bool Q w) : + Turing.Cfg k Bool (State σ Q) w := c.mapState (Option.map State.run) + +lemma step_lift {w : List Bool} (c : Turing.Cfg k Bool Q w) : + (machine M D).step (liftCfg (σ := σ) c) = liftCfg (σ := σ) (M.step c) := by + cases hs : c.state with + | none => simp [Turing.MultiTapeTM.step, liftCfg, Turing.Cfg.mapState, hs] + | some q => + simp only [Turing.MultiTapeTM.step, liftCfg, Turing.Cfg.mapState, hs, Option.map_some] + rfl + +lemma run_lift {w : List Bool} (c : Turing.Cfg k Bool Q w) (t : ℕ) : + (machine M D).runFrom (liftCfg (σ := σ) c) t = liftCfg (σ := σ) (M.runFrom c t) := by + induction t generalizing c with + | zero => rfl + | succ t ih => + simp only [Turing.MultiTapeTM.runFrom, Function.iterate_succ_apply, step_lift] + exact ih _ + +lemma scan_zero (w : List Bool) : scanCfg (k := k) (Q := Q) D w 0 (by lia) = + (machine M D).initCfg w := by + simp [scanCfg, baseCfg, Turing.MultiTapeTM.initCfg, Turing.Cfg.init, machine, DFA.eval] + +lemma scan_step (w : List Bool) (t : ℕ) (ht : t < w.length) : + (machine M D).step (scanCfg (k := k) (Q := Q) D w t ht.le) = + scanCfg D w (t + 1) (by lia) := by + have hin : (scanCfg (k := k) (Q := Q) D w t ht.le).inputSymbol = some w[t] := + Turing.inputSymbolInner t (by simp [scanCfg, baseCfg, Nat.add_comm]) ht + have hp : (scanCfg (k := k) (Q := Q) D w t ht.le).inputPos.val ≠ w.length + 1 := by + simp [scanCfg, baseCfg]; lia + have hw := (List.take_concat_get' w t ht).symm + rw [Turing.MultiTapeTM.step_apply_of_state (q := .scan (D.eval (w.take t))) rfl] + simp only [machine, hin] + apply Turing.Cfg.ext + · change some (State.scan (D.step (D.eval (w.take t)) w[t])) = + some (State.scan (D.eval (w.take (t + 1)))) + rw [hw, DFA.eval_append_singleton] + · simpa [Turing.Action.apply, idle, scanCfg, baseCfg] using + Turing.moveInputPos_pos_of_ne_right (scanCfg (k := k) (Q := Q) D w t ht.le).inputPos hp + · rfl + · simp [Turing.Action.apply, idle, scanCfg, baseCfg] + · rfl + +lemma run_succ {w : List Bool} (c : Turing.Cfg k Bool (State σ Q) w) (t : ℕ) : + (machine M D).runFrom c (t + 1) = (machine M D).step ((machine M D).runFrom c t) := + Function.iterate_succ_apply' _ _ _ + +lemma run_add {w : List Bool} (c : Turing.Cfg k Bool (State σ Q) w) (t u : ℕ) : + (machine M D).runFrom c (t + u) = + (machine M D).runFrom ((machine M D).runFrom c t) u := by + simp only [Turing.MultiTapeTM.runFrom, Nat.add_comm t u, Function.iterate_add_apply] + +lemma run_scan (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + (machine M D).runFrom ((machine M D).initCfg w) t = scanCfg D w t ht := by + induction t with + | zero => exact (scan_zero M D w).symm + | succ t ih => rw [run_succ, ih (by lia), scan_step M D w t (by lia)] + +lemma rewind_step (w : List Bool) (t : ℕ) (ht : t + 1 ≤ w.length) : + (machine M D).step (rewindCfg (k := k) (σ := σ) (Q := Q) w (t + 1) ht) = + rewindCfg w t (by lia) := by + have hin : (rewindCfg (k := k) (σ := σ) (Q := Q) w (t + 1) ht).inputSymbol = + some (w[t]'(by lia)) := + Turing.inputSymbolInner t (by simp [rewindCfg, baseCfg, Nat.add_comm]) (by lia) + rw [Turing.MultiTapeTM.step_apply_of_state (q := .rewind) rfl] + simp only [machine, hin] + apply Turing.Cfg.ext + · rfl + · have hp : (rewindCfg (k := k) (σ := σ) (Q := Q) w (t + 1) ht).inputPos ≠ 0 := by + intro he + have := congrArg Fin.val he + simp [rewindCfg, baseCfg] at this + simpa [idle, Turing.Action.apply, rewindCfg, baseCfg] using + Turing.moveInputPos_neg_of_ne_left + (rewindCfg (k := k) (σ := σ) (Q := Q) w (t + 1) ht).inputPos hp + · rfl + · simp [idle, Turing.Action.apply, rewindCfg, baseCfg] + · rfl + +lemma run_rewind (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + (machine M D).runFrom (rewindCfg (k := k) (σ := σ) (Q := Q) w t ht) t = + rewindCfg w 0 (by lia) := by + induction t with + | zero => rfl + | succ t ih => + change (machine M D).step^[t + 1] _ = _ + rw [Function.iterate_succ_apply, rewind_step M D w t ht] + exact ih (by lia) + +lemma prepare {w : List Bool} (hw : w ∈ D.accepts) : + (machine M D).runFrom ((machine M D).initCfg w) (2 * w.length + 2) = + liftCfg (σ := σ) (M.initCfg w) := by + have hin : (scanCfg (k := k) (Q := Q) D w w.length le_rfl).inputSymbol = none := + Turing.inputSymbol_eq_none_of_boundary (Or.inr rfl) + have hend : (machine M D).step (scanCfg (k := k) (Q := Q) D w w.length le_rfl) = + rewindCfg w w.length le_rfl := by + have ha : D.eval w ∈ D.accept := hw + rw [Turing.MultiTapeTM.step_apply_of_state (q := .scan (D.eval (w.take w.length))) rfl] + simp only [machine, hin, List.take_length, ha, ↓reduceIte] + apply Turing.Cfg.ext + · rfl + · have hp : (scanCfg (k := k) (Q := Q) D w w.length le_rfl).inputPos ≠ 0 := by + intro he + have := congrArg Fin.val he + simp [scanCfg, baseCfg] at this + simpa [idle, Turing.Action.apply, scanCfg, rewindCfg, baseCfg] using + Turing.moveInputPos_neg_of_ne_left + (scanCfg (k := k) (Q := Q) D w w.length le_rfl).inputPos hp + · rfl + · simp [idle, Turing.Action.apply, scanCfg, rewindCfg, baseCfg] + · rfl + have hzero : (machine M D).step (rewindCfg (k := k) (σ := σ) (Q := Q) w 0 (by lia)) = + liftCfg (σ := σ) (M.initCfg w) := by + have hz : (rewindCfg (k := k) (σ := σ) (Q := Q) w 0 (by lia)).inputSymbol = none := + Turing.inputSymbol_eq_none_of_boundary (Or.inl rfl) + rw [Turing.MultiTapeTM.step_apply_of_state (q := .rewind) rfl] + simp only [machine, hz] + simp [idle, Turing.Action.apply, rewindCfg, baseCfg, liftCfg, + Turing.Cfg.mapState, Turing.MultiTapeTM.initCfg, Turing.Cfg.init] + simpa using Turing.moveInputPos_pos_of_ne_right (0 : Fin (w.length + 2)) (by simp) + have ht : 2 * w.length + 2 = ((w.length + 1) + w.length) + 1 := by lia + rw [ht, run_succ, run_add, run_succ, run_scan M D w w.length le_rfl, hend, + run_rewind, hzero] + +lemma rejected {w : List Bool} (hw : w ∉ D.accepts) : + ((machine M D).runFrom ((machine M D).initCfg w) (w.length + 1)).state = none ∧ + ((machine M D).runFrom ((machine M D).initCfg w) (w.length + 1)).output = [] := by + have hin : (scanCfg (k := k) (Q := Q) D w w.length le_rfl).inputSymbol = none := + Turing.inputSymbol_eq_none_of_boundary (Or.inr rfl) + have ha : D.eval w ∉ D.accept := hw + rw [run_succ, run_scan M D w w.length le_rfl, + Turing.MultiTapeTM.step_apply_of_state (q := .scan (D.eval (w.take w.length))) rfl] + simp only [machine, hin, List.take_length, ha, ↓reduceIte] + simp [idle, Turing.Action.apply, scanCfg, baseCfg] + +end Complexity.CslibGuard diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet.lean new file mode 100644 index 000000000..90e887447 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet.lean @@ -0,0 +1,70 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Defs +public import Complexitylib.Encoding.FiniteAlphabet +public import Complexitylib.Interop.Cslib.FromMultiTape +public import Complexitylib.Interop.Cslib.Guard +import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Output + +/-! +# Finite input and output alphabets transfer to canonical FP + +Explicit finite-alphabet codes supply a total binary interface: the machine +rejects malformed codewords with empty output before running the source. +Input and output alphabets can differ and may be empty. Marked input and +complete decoded output add only linear encoding and parsing overhead. +-/ + +public section + +namespace Complexity.MathlibTM0.AlphabetMachine + +variable {Input Output : Type} + +/-- Parse input codes, preserve the complete source output, and emit its exact binary encoding. -/ +theorem computes_cslib (M : AlphabetMachine Input Output) + (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + {input : List Input} {output : List Output} {time : ℕ} + (h : M.OutputsWithin input output time) : + (machine M ci co).ComputesInTimeAndSpace (ci.encode input) (co.encode output) + (2 * (ci.encode input).length + time + (co.size + 1) * output.length + 4) + ((machine M ci co).spaceUsed ((machine M ci co).initCfg (ci.encode input)) + (2 * (ci.encode input).length + time + (co.size + 1) * output.length + 4)) := + computes_proof M ci co h + +/-- Polynomial-time source word computations give total binary FP functions under explicit codes. -/ +theorem mem_FP {M : AlphabetMachine Input Output} + (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + {f : List Input → List Output} {p q : Polynomial ℕ} + (h : ∀ xs, M.OutputsWithin xs (f xs) (p.eval xs.length)) + (hlen : ∀ xs, (f xs).length ≤ q.eval xs.length) : ci.map co f ∈ FP := by + let r := Polynomial.C 2 * Polynomial.X + p + Polynomial.C (co.size + 1) * q + 4 + refine mem_FP_of_computableInTimeAndSpace (p := r) + (s := fun w => (machine M ci co).spaceUsed ((machine M ci co).initCfg w) (r.eval w.length)) + ⟨_, Phase ci.size co.size M.State, inferInstance, machine M ci co, ?_⟩ + intro w + refine ⟨r.eval w.length, le_rfl, _, le_rfl, ?_⟩ + simpa [r] using computes_with_bounds M ci co (P := fun _ => True) + (fun xs _ => h xs) (fun xs _ => hlen xs) w trivial + +/-- Source bounds restricted to a regular binary input domain suffice after DFA validation. -/ +theorem mem_FP_on_dfa {M : AlphabetMachine Input Output} + (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + {σ : Type} [Fintype σ] (D : DFA Bool σ) [DecidablePred (· ∈ D.accepts)] + {f : List Input → List Output} {p q : Polynomial ℕ} + (h : ∀ xs, ci.encode xs ∈ D.accepts → M.OutputsWithin xs (f xs) (p.eval xs.length)) + (hlen : ∀ xs, ci.encode xs ∈ D.accepts → (f xs).length ≤ q.eval xs.length) : + (fun bits => if bits ∈ D.accepts then ci.map co f bits else []) ∈ FP := by + let r := Polynomial.C 2 * Polynomial.X + p + Polynomial.C (co.size + 1) * q + 4 + apply CslibGuard.mem_FP_on_dfa (machine M ci co) D (p := r) + intro w hw + refine ⟨r.eval w.length, le_rfl, + (machine M ci co).spaceUsed ((machine M ci co).initCfg w) (r.eval w.length), ?_⟩ + simpa [r] using computes_with_bounds M ci co h hlen w hw + +end Complexity.MathlibTM0.AlphabetMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet/Defs.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Defs.lean new file mode 100644 index 000000000..df14a9b0a --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Defs.lean @@ -0,0 +1,151 @@ +/- +Copyright (c) 2026 Samuel Schlesinger, Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Samuel Schlesinger, Erik Demaine +-/ + +module +public import Complexitylib.Encoding.FiniteAlphabet.Defs +public import Complexitylib.Interop.Mathlib.TM0.Marked.Defs + +/-! +# Marked tape computations over finite input and output alphabets + +The binary simulator parses explicit finite-alphabet codewords before writing +source cells. It rejects malformed codes before starting the source. Source +execution reuses symbol tracks, and output emission writes complete codewords. +Input and output alphabets and their chosen numberings are independent. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0 + +/-- A finite tape machine with marked input symbols and a separate output alphabet. -/ +structure AlphabetMachine (Input Output : Type) where + /-- Source tape alphabet. -/ + Alphabet : Type + /-- Source control states. -/ + State : Type + /-- Source alphabet finiteness. -/ + [alphabetFinite : Fintype Alphabet] + /-- Default source symbol is blank. -/ + [alphabetInhabited : Inhabited Alphabet] + /-- Source control finiteness. -/ + [stateFinite : Fintype State] + /-- Default control is initial. -/ + [stateInhabited : Inhabited State] + /-- Encode an input letter, distinguishing the first cell. -/ + inputSymbol : Bool → Input → Alphabet + /-- The single cell representing empty input. -/ + empty : Alphabet + /-- Decode an output letter, stopping on a symbol with no output letter. -/ + readSymbol : Alphabet → Option Output + /-- Blank terminates output. -/ + readSymbol_blank : readSymbol default = none + /-- Source transition function. -/ + code : Turing.TM0.Machine Alphabet State + +attribute [instance] AlphabetMachine.alphabetFinite AlphabetMachine.alphabetInhabited + AlphabetMachine.stateFinite AlphabetMachine.stateInhabited + +namespace AlphabetMachine + +variable {Input Output : Type} + +/-- Reuse the existing source simulator; its binary endpoints are never entered. -/ +abbrev simulatorSource (M : AlphabetMachine Input Output) : MarkedMachine where + Alphabet := M.Alphabet + State := M.State + alphabetInhabited := M.alphabetInhabited + stateInhabited := M.stateInhabited + bit _ _ := default + empty := M.empty + readBit _ := none + readBit_blank := rfl + code := M.code + +/-- Source cells for a complete input or an unmarked suffix. -/ +def inputCells (M : AlphabetMachine Input Output) : Bool → List Input → List M.Alphabet + | first, [] => if first then [M.empty] else [] + | first, a :: xs => M.inputSymbol first a :: M.inputCells false xs + +/-- A bounded source run specifying every decoded output cell. -/ +def OutputsWithin (M : AlphabetMachine Input Output) (input : List Input) + (output : List Output) (time : ℕ) : Prop := + ∃ t ≤ time, ∃ c : M.simulatorSource.Config, + M.simulatorSource.run t (Turing.TM0.init (M.inputCells true input)) = some c ∧ + Turing.TM0.step M.code c = none ∧ + ∀ j : ℕ, M.readSymbol (c.Tape.nth j) = output[j]? + +/-- Parse a symbol code, rewind, run the existing simulator, or finish a codeword. -/ +inductive Phase (inputSize outputSize : ℕ) (Q : Type) where + /-- `none` waits for an opening delimiter; `some i` counts unary index bits. -/ + | copy : Bool → Option (Fin (inputSize + 1)) → Phase inputSize outputSize Q + /-- Moving backwards alternates between closing and opening delimiters. -/ + | rewind : Bool → Phase inputSize outputSize Q + /-- Existing source execution control; emission is replaced by encoded output. -/ + | source : Control Q → Phase inputSize outputSize Q + /-- Emit the remaining unary index bits and then the closing delimiter. -/ + | emit : Fin (outputSize + 1) → Phase inputSize outputSize Q + deriving Fintype + +/-- An action preserving every work tape and head. -/ +def pause {k : ℕ} {Q : Type} (state : Option Q) (output : Option Bool) + (move : SignType) : Turing.Action k Bool Q where + state := state + inputTape := move + output := output + workTapes := fun _ => (none, 0) + +/-- Parse finite-alphabet inputs, simulate the source, and encode its complete output. -/ +noncomputable def machine (M : AlphabetMachine Input Output) + (input : FiniteAlphabetCode Input) (output : FiniteAlphabetCode Output) : + Turing.MultiTapeTM (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase input.size output.size M.State) where + q₀ := .copy true none + tr q bit work := match q with + | .copy first count => match count, bit with + | none, some false => pause (some (.copy first (some 0))) none 1 + | none, some true => pause none none 0 + | some i, some true => + if h : i.val + 1 < input.size then + pause (some (.copy first (some ⟨i.val + 1, by omega⟩))) none 1 + else pause none none 0 + | some i, some false => + if h : i.val < input.size then + { state := some (.copy false none), inputTape := 1, output := none, + workTapes := fun j => + (some (SymbolTracks.encode + (.source (M.inputSymbol first (input.index.symm ⟨i.val, h⟩))) + (trackEquiv M.simulatorSource.binary j)), 1) } + else pause none none 0 + | some _, none => pause none none 0 + | none, none => + { state := some (.rewind true), inputTape := -1, output := none, + workTapes := fun j => + (if first then some (SymbolTracks.encode (.source M.empty) + (trackEquiv M.simulatorSource.binary j)) else none, 0) } + | .rewind closing => match bit with + | none => pause (some (.source (.run default))) none 0 + | some b => + { state := some (.rewind (if b then closing else !closing)), + inputTape := -1, output := none, + workTapes := fun _ => (none, if !b && closing then -1 else 0) } + | .source .emit => + match M.readSymbol (MarkedMachine.Symbol.value (SymbolTracks.decode + (fun a => work ((trackEquiv M.simulatorSource.binary).symm a)))) with + | none => pause none none 0 + | some a => + { state := some (.emit ⟨(output.index a).val, by have := (output.index a).isLt; omega⟩), + inputTape := 0, output := some false, + workTapes := fun _ => (none, 1) } + | .source q => + let a := (MathlibTM0.machine M.simulatorSource.binary).tr q bit work + { a with state := a.state.map Phase.source } + | .emit i => + if h : i.val = 0 then pause (some (.source .emit)) (some false) 0 + else pause (some (.emit ⟨i.val - 1, by omega⟩)) (some true) 0 + +end AlphabetMachine +end Complexity.MathlibTM0 diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Copy.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Copy.lean new file mode 100644 index 000000000..5519ea853 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Copy.lean @@ -0,0 +1,218 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Defs +public import Complexitylib.Encoding.FiniteAlphabet.Internal +public import Complexitylib.Interop.Mathlib.TM0.Marked.Internal.Initialization + +/-! +# Parsing finite-alphabet inputs onto source symbol tracks + +The parser's finite control records an opening delimiter and a bounded unary +index. Each completed codeword writes one marked source cell. Invalid codes +halt with empty output before the source is started. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.AlphabetMachine + +open FiniteAlphabetCode + +variable {Input Output : Type} (M : AlphabetMachine Input Output) +variable (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + +/-- Source cells written before the final empty-input check. -/ +def prefixCells : List Input → List M.Alphabet + | [] => [] + | a :: xs => M.inputSymbol true a :: xs.map (M.inputSymbol false) + +lemma prefixCells_length (xs : List Input) : (prefixCells M xs).length = xs.length := by + cases xs <;> simp [prefixCells] + +lemma prefixCells_append (xs : List Input) (a : Input) : + prefixCells M (xs ++ [a]) = prefixCells M xs ++ [M.inputSymbol xs.isEmpty a] := by + cases xs <;> simp [prefixCells] + +lemma inputCells_false (xs : List Input) : + M.inputCells false xs = xs.map (M.inputSymbol false) := by + induction xs <;> simp_all [inputCells] + +lemma inputCells_true (xs : List Input) : + M.inputCells true xs = if xs.isEmpty then [M.empty] else prefixCells M xs := by + cases xs <;> simp [inputCells, prefixCells, inputCells_false] + +/-- Number of input bits already consumed by a partial parse. -/ +def copyPrefix (xs : List Input) (count : Option (Fin (ci.size + 1))) : List Bool := + ci.encode xs ++ pendingPrefix (count.map Fin.val) + +/-- State after writing `xs` and reading the indicated prefix of the next codeword. -/ +noncomputable def copyCfg (w : List Bool) (xs : List Input) + (count : Option (Fin (ci.size + 1))) (h : (copyPrefix ci xs count).length ≤ w.length) : + Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w where + state := some (.copy xs.isEmpty count) + inputPos := ⟨(copyPrefix ci xs count).length + 1, by omega⟩ + workTapes := MarkedMachine.contents M.simulatorSource (prefixCells M xs) + workTapePos := fun _ => xs.length + output := [] + +/-- End-of-input configuration before rewinding the parsed input. -/ +noncomputable def endCfg (w : List Bool) (xs : List Input) : + Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w where + state := some (.rewind true) + inputPos := ⟨w.length, by omega⟩ + workTapes := MarkedMachine.contents M.simulatorSource (M.inputCells true xs) + workTapePos := fun _ => xs.length + output := [] + +lemma copyCfg_read {w : List Bool} (xs : List Input) (count : Option (Fin (ci.size + 1))) + (h : (copyPrefix ci xs count).length ≤ w.length) (rest : List Bool) + (hw : w = copyPrefix ci xs count ++ rest) : + (copyCfg M ci co w xs count h).inputSymbol = rest.head? := by + cases rest with + | nil => + have he : (copyPrefix ci xs count).length = w.length := by simp [hw] + exact Turing.inputSymbol_eq_none_of_boundary (Or.inr (by simp [copyCfg, he])) + | cons b rest => + have ht : (copyPrefix ci xs count).length < w.length := by simp [hw] + have hb : w[(copyPrefix ci xs count).length] = b := by simp [hw] + rw [← hb] + exact Turing.inputSymbolInner _ (by simp [copyCfg, Nat.add_comm]) ht + +lemma copyCfg_zero (w : List Bool) : + copyCfg M ci co w [] none (by simp [copyPrefix, encode, pendingPrefix]) = + (machine M ci co).initCfg w := by + simp [copyCfg, copyPrefix, encode, pendingPrefix, prefixCells, + Turing.MultiTapeTM.initCfg, Turing.Cfg.init, machine] + exact MarkedMachine.contents_nil M.simulatorSource + +lemma run_add {w : List Bool} + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) (s t : ℕ) : + (machine M ci co).runFrom d (s + t) = + (machine M ci co).runFrom ((machine M ci co).runFrom d s) t := by + simp only [Turing.MultiTapeTM.runFrom, Nat.add_comm s t, Function.iterate_add_apply] + +lemma copy_open {w : List Bool} (xs : List Input) (rest : List Bool) + (hw : w = copyPrefix ci xs none ++ false :: rest) + (h : (copyPrefix ci xs none).length ≤ w.length) + (h' : (copyPrefix ci xs (some 0)).length ≤ w.length) : + (machine M ci co).step (copyCfg M ci co w xs none h) = + copyCfg M ci co w xs (some 0) h' := by + have hin := copyCfg_read M ci co xs none h (false :: rest) hw + have hp : (copyCfg M ci co w xs none h).inputPos.val ≠ w.length + 1 := by + simp only [copyCfg] + simp only [hw, List.length_append, List.length_cons] + omega + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty none) rfl] + simp only [machine, hin, List.head?_cons] + apply Turing.Cfg.ext + · rfl + · simpa [pause, Turing.Action.apply, copyCfg, copyPrefix, pendingPrefix, + List.length_append, Nat.add_assoc] using + Turing.moveInputPos_pos_of_ne_right (copyCfg M ci co w xs none h).inputPos hp + · rfl + · simp [pause, Turing.Action.apply, copyCfg] + · rfl + +lemma copy_true {w : List Bool} (xs : List Input) (i : Fin (ci.size + 1)) + (rest : List Bool) (hw : w = copyPrefix ci xs (some i) ++ true :: rest) + (hi : i.val + 1 < ci.size) + (h : (copyPrefix ci xs (some i)).length ≤ w.length) + (h' : (copyPrefix ci xs (some ⟨i.val + 1, by omega⟩)).length ≤ w.length) : + (machine M ci co).step (copyCfg M ci co w xs (some i) h) = + copyCfg M ci co w xs (some ⟨i.val + 1, by omega⟩) h' := by + have hin := copyCfg_read M ci co xs (some i) h (true :: rest) hw + have hp : (copyCfg M ci co w xs (some i) h).inputPos.val ≠ w.length + 1 := by + simp only [copyCfg] + simp only [hw, List.length_append, List.length_cons] + omega + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty (some i)) rfl] + simp only [machine, hin, List.head?_cons, dite_eq_ite, hi] + apply Turing.Cfg.ext + · rfl + · simpa [pause, Turing.Action.apply, copyCfg, copyPrefix, pendingPrefix, + List.length_append, Nat.add_assoc] using + Turing.moveInputPos_pos_of_ne_right (copyCfg M ci co w xs (some i) h).inputPos hp + · rfl + · simp [pause, Turing.Action.apply, copyCfg] + · rfl + +lemma copy_close_prefix (xs : List Input) (i : Fin (ci.size + 1)) (hi : i.val < ci.size) : + copyPrefix ci (xs ++ [ci.index.symm ⟨i.val, hi⟩]) none = + copyPrefix ci xs (some i) ++ [false] := by + simp [copyPrefix, pendingPrefix, encode_append, encode_cons, encode_nil, + symbol, List.append_assoc] + +lemma copy_close {w : List Bool} (xs : List Input) (i : Fin (ci.size + 1)) + (rest : List Bool) (hw : w = copyPrefix ci xs (some i) ++ false :: rest) + (hi : i.val < ci.size) + (h : (copyPrefix ci xs (some i)).length ≤ w.length) + (h' : (copyPrefix ci (xs ++ [ci.index.symm ⟨i.val, hi⟩]) none).length ≤ w.length) : + (machine M ci co).step (copyCfg M ci co w xs (some i) h) = + copyCfg M ci co w (xs ++ [ci.index.symm ⟨i.val, hi⟩]) none h' := by + have hin := copyCfg_read M ci co xs (some i) h (false :: rest) hw + have hp : (copyCfg M ci co w xs (some i) h).inputPos.val ≠ w.length + 1 := by + simp only [copyCfg] + simp only [hw, List.length_append, List.length_cons] + omega + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty (some i)) rfl] + simp only [machine, hin, List.head?_cons, hi, dite_true] + apply Turing.Cfg.ext + · simp [copyCfg] + · simpa [Turing.Action.apply, copyCfg, copy_close_prefix ci xs i hi] using + Turing.moveInputPos_pos_of_ne_right (copyCfg M ci co w xs (some i) h).inputPos hp + · funext j + simp only [Turing.Action.apply, copyCfg, prefixCells_append] + simpa only [prefixCells_length] using + (MarkedMachine.contents_append M.simulatorSource (prefixCells M xs) + (M.inputSymbol xs.isEmpty (ci.index.symm ⟨i.val, hi⟩)) j).symm + · funext j + simp [Turing.Action.apply, copyCfg, Nat.cast_add] + · rfl + +lemma copy_end {w : List Bool} (xs : List Input) (hw : w = ci.encode xs) + (h : (copyPrefix ci xs none).length ≤ w.length) : + (machine M ci co).step (copyCfg M ci co w xs none h) = endCfg M ci co w xs := by + have hin := copyCfg_read M ci co xs none h [] (by simpa [copyPrefix, pendingPrefix] using hw) + have he : (copyPrefix ci xs none).length = w.length := by + simp [copyPrefix, pendingPrefix, hw] + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty none) rfl] + simp only [machine, hin, List.head?_nil] + apply Turing.Cfg.ext + · rfl + · have hp : (copyCfg M ci co w xs none h).inputPos ≠ 0 := by + intro hh + have := congrArg Fin.val hh + simp [copyCfg] at this + simpa [Turing.Action.apply, copyCfg, endCfg, he] using + Turing.moveInputPos_neg_of_ne_left (copyCfg M ci co w xs none h).inputPos hp + · funext j + cases xs with + | nil => + simpa [Turing.Action.apply, copyCfg, endCfg, inputCells, prefixCells, + MarkedMachine.contents_nil] using + (MarkedMachine.contents_append M.simulatorSource [] M.empty j).symm + | cons a xs => simp [Turing.Action.apply, copyCfg, endCfg, inputCells_true] + · rfl + · rfl + +lemma run_rejected {w : List Bool} + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) (t : ℕ) + (h : ((machine M ci co).step d).state = none ∧ + ((machine M ci co).step d).output = []) : + ((machine M ci co).runFrom d (t + 1)).state = none ∧ + ((machine M ci co).runFrom d (t + 1)).output = [] := by + have he := (machine M ci co).runFrom_eq_of_halt d + (by omega : 1 ≤ t + 1) (by simpa [Turing.MultiTapeTM.runFrom] using h.1) + rw [he] + exact h + +end Complexity.MathlibTM0.AlphabetMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Emission.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Emission.lean new file mode 100644 index 000000000..03a7ba8b6 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Emission.lean @@ -0,0 +1,148 @@ +/- +Copyright (c) 2026 Samuel Schlesinger, Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Samuel Schlesinger, Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Parsing + +/-! +# Emitting finite-alphabet codewords + +Reading a source letter emits its opening delimiter. Finite control then emits +its unary index and closing delimiter. A complete output costs exactly its +binary encoded length plus one final halting transition. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.AlphabetMachine + +variable {Input Output : Type} (M : AlphabetMachine Input Output) +variable (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + +/-- Output emission from the represented source tape. -/ +structure Emitting {w : List Bool} (tape : Turing.Tape M.simulatorSource.binary.Alphabet) + (emitted : List Bool) + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) : Prop where + /-- Ready to read the next source letter. -/ + state : d.state = some (.source .emit) + /-- Previously emitted bits. -/ + output : d.output = emitted + /-- Remaining source tape relative to the work heads. -/ + tape : ∀ i z, d.workTapes i (d.workTapePos i + z) = + SymbolTracks.encode (tape.nth z) (trackEquiv M.simulatorSource.binary i) + +/-- A symbol's opening delimiter has been emitted; its index and closing delimiter remain. -/ +structure Pending {w : List Bool} (n : Fin (co.size + 1)) + (tape : Turing.Tape M.simulatorSource.binary.Alphabet) (emitted : List Bool) + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) : Prop where + /-- Remaining unary index bits. -/ + state : d.state = some (.emit n) + /-- Previously emitted bits. -/ + output : d.output = emitted + /-- Source tracks are already positioned at the following letter. -/ + tape : ∀ i z, d.workTapes i (d.workTapePos i + z) = + SymbolTracks.encode (tape.nth z) (trackEquiv M.simulatorSource.binary i) + +lemma Emitting.read {w : List Bool} {tape : Turing.Tape M.simulatorSource.binary.Alphabet} + {emitted : List Bool} + {d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w} (h : Emitting M ci co tape emitted d) : + SymbolTracks.decode (fun a => d.workTapeSymbols + ((trackEquiv M.simulatorSource.binary).symm a)) = tape.head := by + have he : (fun a => d.workTapeSymbols ((trackEquiv M.simulatorSource.binary).symm a)) = + SymbolTracks.encode tape.head := by + funext a + simpa [Turing.Cfg.workTapeSymbols] using h.tape ((trackEquiv M.simulatorSource.binary).symm a) 0 + rw [he, SymbolTracks.decode_encode] + +lemma pending_run (n : ℕ) (hn : n < co.size + 1) {w : List Bool} + {tape : Turing.Tape M.simulatorSource.binary.Alphabet} {emitted : List Bool} + {d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w} + (h : Pending M ci co ⟨n, hn⟩ tape emitted d) : + Emitting M ci co tape (emitted ++ List.replicate n true ++ [false]) + ((machine M ci co).runFrom d (n + 1)) := by + induction n generalizing emitted d with + | zero => + change Emitting M ci co tape (emitted ++ [] ++ [false]) ((machine M ci co).step d) + rw [Turing.MultiTapeTM.step_apply_of_state h.state] + refine ⟨rfl, ?_, ?_⟩ + · simp [machine, pause, Turing.Action.apply, h.output] + · simpa [machine, pause, Turing.Action.apply] using h.tape + | succ n ih => + have hs : Pending M ci co ⟨n, by omega⟩ tape (emitted ++ [true]) + ((machine M ci co).step d) := by + rw [Turing.MultiTapeTM.step_apply_of_state h.state] + refine ⟨rfl, ?_, ?_⟩ + · simp [machine, pause, Turing.Action.apply, h.output] + · simpa [machine, pause, Turing.Action.apply] using h.tape + rw [run_succ_first] + simpa [List.replicate_succ, List.append_assoc] using ih (by omega) hs + +lemma Emitting.start {w : List Bool} {tape : Turing.Tape M.simulatorSource.binary.Alphabet} + {emitted : List Bool} + {d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w} (h : Emitting M ci co tape emitted d) + (a : Output) (ha : M.readSymbol (MarkedMachine.Symbol.value tape.head) = some a) : + Pending M ci co ⟨(co.index a).val, by have := (co.index a).isLt; omega⟩ + (tape.move .right) (emitted ++ [false]) ((machine M ci co).step d) := by + rw [Turing.MultiTapeTM.step_apply_of_state h.state] + simp only [machine, Emitting.read M ci co h, ha] + refine ⟨rfl, ?_, ?_⟩ + · simp [Turing.Action.apply, h.output] + · intro i z + simp only [Turing.Action.apply, Turing.Tape.move_right_nth, SignType.coe_one] + convert h.tape i (z + 1) using 1 + congr 1 + omega + +lemma Emitting.halt {w : List Bool} {tape : Turing.Tape M.simulatorSource.binary.Alphabet} + {emitted : List Bool} + {d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w} (h : Emitting M ci co tape emitted d) + (ha : M.readSymbol (MarkedMachine.Symbol.value tape.head) = none) : + ((machine M ci co).step d).state = none ∧ + ((machine M ci co).step d).output = emitted := by + rw [Turing.MultiTapeTM.step_apply_of_state h.state] + simp [machine, Emitting.read M ci co h, ha, pause, Turing.Action.apply, h.output] + +lemma Emitting.run {w : List Bool} (output : List Output) + {tape : Turing.Tape M.simulatorSource.binary.Alphabet} {emitted : List Bool} + {d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w} (h : Emitting M ci co tape emitted d) + (ho : ∀ j : ℕ, M.readSymbol (MarkedMachine.Symbol.value (tape.nth j)) = output[j]?) : + ((machine M ci co).runFrom d ((co.encode output).length + 1)).state = none ∧ + ((machine M ci co).runFrom d ((co.encode output).length + 1)).output = + emitted ++ co.encode output := by + induction output generalizing tape emitted d with + | nil => + have hb : M.readSymbol (MarkedMachine.Symbol.value tape.head) = none := by simpa using ho 0 + simpa [FiniteAlphabetCode.encode, Turing.MultiTapeTM.runFrom] using + Emitting.halt M ci co h hb + | cons a output ih => + have hb : M.readSymbol (MarkedMachine.Symbol.value tape.head) = some a := by simpa using ho 0 + have hs := Emitting.start M ci co h a hb + have hp : Emitting M ci co (tape.move .right) (emitted ++ co.symbol a) + ((machine M ci co).runFrom ((machine M ci co).step d) ((co.index a).val + 1)) := by + simpa [FiniteAlphabetCode.symbol, List.append_assoc] using + pending_run M ci co (co.index a).val (by have := (co.index a).isLt; omega) hs + have ht : ∀ j : ℕ, + M.readSymbol (MarkedMachine.Symbol.value ((tape.move .right).nth j)) = output[j]? := by + intro j + rw [Turing.Tape.move_right_nth] + simpa only [List.getElem?_cons_succ, Nat.cast_add, Nat.cast_one] using ho (j + 1) + have hr := ih hp ht + rw [← run_add, ← run_succ_first] at hr + have he : (co.encode (a :: output)).length + 1 = + ((co.index a).val + 1 + ((co.encode output).length + 1)) + 1 := by + rw [FiniteAlphabetCode.encode_cons, List.length_append, FiniteAlphabetCode.symbol_length] + omega + rw [he] + simpa only [FiniteAlphabetCode.encode_cons, List.append_assoc] using hr + +end Complexity.MathlibTM0.AlphabetMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Initialization.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Initialization.lean new file mode 100644 index 000000000..ccbf4f39d --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Initialization.lean @@ -0,0 +1,141 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Simulation +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Parsing + +/-! +# Rewinding a parsed finite-alphabet input + +Rewinding crosses one source cell for each closing delimiter. The parity of +the delimiter count identifies closing delimiters while reading backwards. +Initialization takes twice the binary input length plus two transitions. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.AlphabetMachine + +variable {Input Output : Type} (M : AlphabetMachine Input Output) +variable (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + +/-- Rewind the input and its decoded symbol tracks together. -/ +noncomputable def rewindCfg (w : List Bool) (xs : List Input) (t : ℕ) (ht : t ≤ w.length) : + Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w where + state := some (.rewind ((w.take t).count false % 2 == 0)) + inputPos := ⟨t, by omega⟩ + workTapes := MarkedMachine.contents M.simulatorSource (M.inputCells true xs) + workTapePos := fun _ => ((w.take t).count false / 2 : ℕ) + output := [] + +/-- The existing simulator with the complete decoded source input already loaded. -/ +noncomputable def readyCfg (w : List Bool) (xs : List Input) : + Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool (Control M.State) w where + state := some (.run default) + inputPos := 0 + workTapes := MarkedMachine.contents M.simulatorSource (M.inputCells true xs) + workTapePos := fun _ => 0 + output := [] + +lemma endCfg_eq_rewind (xs : List Input) : + endCfg M ci co (ci.encode xs) xs = + rewindCfg M ci co (ci.encode xs) xs (ci.encode xs).length le_rfl := by + simp [endCfg, rewindCfg, FiniteAlphabetCode.encode_count_false] + +lemma rewindCfg_zero (w : List Bool) (xs : List Input) : + (machine M ci co).step (rewindCfg M ci co w xs 0 (by omega)) = + liftCfg M ci co (readyCfg M w xs) := by + have hin : (rewindCfg M ci co w xs 0 (by omega)).inputSymbol = none := + Turing.inputSymbol_eq_none_of_boundary (Or.inl rfl) + rw [Turing.MultiTapeTM.step_apply_of_state (q := .rewind true) rfl] + simp only [machine, hin] + simp [pause, Turing.Action.apply, rewindCfg, liftCfg, readyCfg, Turing.Cfg.mapState] + +lemma ready_sim (w : List Bool) (xs : List Input) : + Sim M.simulatorSource.binary + (MarkedMachine.sourceCfg M.simulatorSource (Turing.TM0.init (M.inputCells true xs))) + (readyCfg M w xs) := by + refine ⟨rfl, rfl, ?_⟩ + intro i z + simp [readyCfg, MarkedMachine.contents, MarkedMachine.sourceCfg, + Turing.TM0.Cfg.map, Turing.TM0.init, Turing.Tape.map_mk₁] + +lemma rewindCfg_step (w : List Bool) (xs : List Input) (t : ℕ) (ht : t + 1 ≤ w.length) : + (machine M ci co).step (rewindCfg M ci co w xs (t + 1) ht) = + rewindCfg M ci co w xs t (by omega) := by + have hin : (rewindCfg M ci co w xs (t + 1) ht).inputSymbol = some (w[t]'(by omega)) := + Turing.inputSymbolInner t (by simp [rewindCfg, Nat.add_comm]) (by omega) + have hc : (w.take (t + 1)).count false = + (w.take t).count false + if w[t]'(by omega) then 0 else 1 := by + rw [← List.take_concat_get' w t (by omega), List.count_append] + cases w[t]'(by omega) <;> simp + have hphase : + (if w[t]'(by omega) then ((w.take (t + 1)).count false % 2 == 0) + else !((w.take (t + 1)).count false % 2 == 0)) = + ((w.take t).count false % 2 == 0) := by + cases hb : w[t]'(by omega) with + | true => simp [hc, hb] + | false => + by_cases hp : (w.take t).count false % 2 = 0 + · have hn : ((w.take t).count false + 1) % 2 = 1 := by omega + simp [hc, hb, hp, hn] + · have hn : ((w.take t).count false + 1) % 2 = 0 := by omega + simp [hc, hb, hp, hn] + have hwork : (((w.take (t + 1)).count false / 2 : ℕ) : ℤ) + + ((if !(w[t]'(by omega)) && ((w.take (t + 1)).count false % 2 == 0) + then -1 else 0 : SignType) : ℤ) = ((w.take t).count false / 2 : ℕ) := by + cases hb : w[t]'(by omega) with + | true => simp [hc, hb] + | false => + by_cases hp : (w.take t).count false % 2 = 0 + · have hn : ((w.take t).count false + 1) % 2 = 1 := by omega + have hd : ((w.take t).count false + 1) / 2 = (w.take t).count false / 2 := by omega + simp [hc, hb, hn, hd] + · have hn : ((w.take t).count false + 1) % 2 = 0 := by omega + have hd : ((w.take t).count false + 1) / 2 = (w.take t).count false / 2 + 1 := by omega + simp [hc, hb, hn, hd] + rw [Turing.MultiTapeTM.step_apply_of_state + (q := .rewind ((w.take (t + 1)).count false % 2 == 0)) rfl] + simp only [machine, hin] + apply Turing.Cfg.ext + · simpa only [Turing.Action.apply, rewindCfg] using + congrArg (fun b => some (Phase.rewind (inputSize := ci.size) + (outputSize := co.size) (Q := M.State) b)) hphase + · have hp : (rewindCfg M ci co w xs (t + 1) ht).inputPos ≠ 0 := by + intro he + have := congrArg Fin.val he + simp [rewindCfg] at this + simpa [Turing.Action.apply, rewindCfg] using + Turing.moveInputPos_neg_of_ne_left (rewindCfg M ci co w xs (t + 1) ht).inputPos hp + · rfl + · funext j + exact hwork + · rfl + +lemma run_rewind (w : List Bool) (xs : List Input) (t : ℕ) (ht : t ≤ w.length) : + (machine M ci co).runFrom (rewindCfg M ci co w xs t ht) t = + rewindCfg M ci co w xs 0 (by omega) := by + induction t with + | zero => rfl + | succ t ih => + rw [run_succ_first, rewindCfg_step M ci co w xs t ht] + exact ih (by omega) + +lemma init_ready (xs : List Input) : + (machine M ci co).runFrom ((machine M ci co).initCfg (ci.encode xs)) + (2 * (ci.encode xs).length + 2) = + liftCfg M ci co (readyCfg M (ci.encode xs) xs) := by + have ht : 2 * (ci.encode xs).length + 2 = + ((ci.encode xs).length + 1) + (ci.encode xs).length + 1 := by omega + rw [ht, Turing.MultiTapeTM.runFrom, Function.iterate_succ_apply'] + change (machine M ci co).step + ((machine M ci co).runFrom ((machine M ci co).initCfg (ci.encode xs)) + (((ci.encode xs).length + 1) + (ci.encode xs).length)) = _ + rw [run_add, copy_encoded, endCfg_eq_rewind, run_rewind, rewindCfg_zero] + +end Complexity.MathlibTM0.AlphabetMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Output.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Output.lean new file mode 100644 index 000000000..d995c91b2 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Output.lean @@ -0,0 +1,109 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Initialization +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Emission +public import Complexitylib.Interop.Cslib.FromMultiTape +import Complexitylib.Asymptotics + +/-! +# Complete finite-alphabet computations on binary inputs + +Parsing, source simulation, and encoded emission preserve complete words with +linear endpoint overhead. Invalid inputs halt before source execution. Source +bounds need only hold when the corresponding binary input satisfies the +chosen validity predicate. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.AlphabetMachine + +variable {Input Output : Type} (M : AlphabetMachine Input Output) +variable (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + +lemma computes_proof {input : List Input} {output : List Output} {time : ℕ} + (h : M.OutputsWithin input output time) : + (machine M ci co).ComputesInTimeAndSpace (ci.encode input) (co.encode output) + (2 * (ci.encode input).length + time + (co.size + 1) * output.length + 4) + ((machine M ci co).spaceUsed ((machine M ci co).initCfg (ci.encode input)) + (2 * (ci.encode input).length + time + (co.size + 1) * output.length + 4)) := by + obtain ⟨t, ht, c, hr, hh, ho⟩ := h + have hm : M.simulatorSource.binary.run t + (MarkedMachine.sourceCfg M.simulatorSource (Turing.TM0.init (M.inputCells true input))) = + some (MarkedMachine.sourceCfg M.simulatorSource c) := by + rw [MarkedMachine.source_run, hr] + rfl + have hs := (ready_sim M (ci.encode input) input).run t hm + have he := hs.halt (by rw [MarkedMachine.source_step, hh]; rfl) + have hem : Emitting M ci co (MarkedMachine.sourceCfg M.simulatorSource c).Tape [] + (liftCfg M ci co ((MathlibTM0.machine M.simulatorSource.binary).step + ((MathlibTM0.machine M.simulatorSource.binary).runFrom + (readyCfg M (ci.encode input) input) t))) := by + refine ⟨?_, he.output, he.tape⟩ + simp [liftCfg, Turing.Cfg.mapState, he.state] + have hout := Emitting.run M ci co output hem (fun j => by + rw [MarkedMachine.source_nth] + exact ho j) + have hrun : (machine M ci co).runFrom ((machine M ci co).initCfg (ci.encode input)) + ((2 * (ci.encode input).length + 2) + t + 1) = + liftCfg M ci co ((MathlibTM0.machine M.simulatorSource.binary).step + ((MathlibTM0.machine M.simulatorSource.binary).runFrom + (readyCfg M (ci.encode input) input) t)) := by + rw [run_succ_last, run_add, init_ready, + lift_source_run M ci co (ready_sim M (ci.encode input) input) t hm] + exact lift_step_of_run M ci co _ _ hs.state + rw [← hrun, ← run_add] at hout + have heq : ((2 * (ci.encode input).length + 2) + t + 1) + + ((co.encode output).length + 1) = + 2 * (ci.encode input).length + t + (co.encode output).length + 4 := by omega + rw [heq] at hout + have hbound : 2 * (ci.encode input).length + t + (co.encode output).length + 4 ≤ + 2 * (ci.encode input).length + time + (co.size + 1) * output.length + 4 := by + have := FiniteAlphabetCode.encode_length_upper co output + omega + have hfreeze := (machine M ci co).runFrom_eq_of_halt + ((machine M ci co).initCfg (ci.encode input)) hbound hout.1 + exact ⟨by rw [hfreeze]; exact hout.1, by rw [hfreeze]; exact hout.2, rfl⟩ + +lemma computes_with_bounds {f : List Input → List Output} {p q : Polynomial ℕ} + {P : List Bool → Prop} + (h : ∀ xs, P (ci.encode xs) → M.OutputsWithin xs (f xs) (p.eval xs.length)) + (hlen : ∀ xs, P (ci.encode xs) → (f xs).length ≤ q.eval xs.length) + (w : List Bool) (hw : P w) : + (machine M ci co).ComputesInTimeAndSpace w (ci.map co f w) + (2 * w.length + p.eval w.length + (co.size + 1) * q.eval w.length + 4) + ((machine M ci co).spaceUsed ((machine M ci co).initCfg w) + (2 * w.length + p.eval w.length + (co.size + 1) * q.eval w.length + 4)) := by + cases hd : ci.decode w with + | none => + have hc := copy_invalid M ci co w hd + have he := (machine M ci co).runFrom_eq_of_halt ((machine M ci co).initCfg w) + (by omega : w.length + 1 ≤ + 2 * w.length + p.eval w.length + (co.size + 1) * q.eval w.length + 4) hc.1 + refine ⟨by rw [he]; exact hc.1, ?_, rfl⟩ + rw [he] + simpa [FiniteAlphabetCode.map, hd] using hc.2 + | some xs => + have he := FiniteAlphabetCode.encode_decode_proof ci hd + have hp : P (ci.encode xs) := by rw [he]; exact hw + have hc := computes_proof M ci co (h xs hp) + rw [he] at hc + have hsize : xs.length ≤ w.length := by + simpa only [he] using FiniteAlphabetCode.encode_length_lower ci xs + have hbound : 2 * w.length + p.eval xs.length + (co.size + 1) * (f xs).length + 4 ≤ + 2 * w.length + p.eval w.length + (co.size + 1) * q.eval w.length + 4 := by + have hp' := polynomial_eval_mono_nat p hsize + have hq' := (hlen xs hp).trans (polynomial_eval_mono_nat q hsize) + exact Nat.add_le_add_right (Nat.add_le_add + (Nat.add_le_add_left hp' _) (Nat.mul_le_mul_left _ hq')) 4 + have hf := (machine M ci co).runFrom_eq_of_halt ((machine M ci co).initCfg w) hbound hc.1 + refine ⟨by rw [hf]; exact hc.1, ?_, rfl⟩ + rw [hf] + simpa [FiniteAlphabetCode.map, hd] using hc.2.1 + +end Complexity.MathlibTM0.AlphabetMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Parsing.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Parsing.lean new file mode 100644 index 000000000..d8d2caa86 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Parsing.lean @@ -0,0 +1,167 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Copy + +/-! +# The binary input parser implements the finite-alphabet decoder + +An induction on the unread suffix proves both successful copying and early +rejection. All inputs finish this phase within their length plus one step. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.AlphabetMachine + +open FiniteAlphabetCode + +variable {Input Output : Type} (M : AlphabetMachine Input Output) +variable (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + +/-- Successful parsing loads the complete input; failed parsing halts with empty output. -/ +def Parsed (w : List Bool) (xs : List Input) (result : Option (List Input)) + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) : Prop := + match result with + | none => d.state = none ∧ d.output = [] + | some ys => d = endCfg M ci co w (xs ++ ys) + +lemma parsed_cons {w : List Bool} (xs : List Input) (a : Input) (result : Option (List Input)) + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) : + Parsed M ci co w (xs ++ [a]) result d ↔ Parsed M ci co w xs (result.map (a :: ·)) d := by + cases result <;> simp [Parsed, List.append_assoc] + +lemma run_succ_first {w : List Bool} + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) (t : ℕ) : + (machine M ci co).runFrom d (t + 1) = + (machine M ci co).runFrom ((machine M ci co).step d) t := + Function.iterate_succ_apply _ _ _ + +lemma run_succ_last {w : List Bool} + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w) (t : ℕ) : + (machine M ci co).runFrom d (t + 1) = + (machine M ci co).step ((machine M ci co).runFrom d t) := + Function.iterate_succ_apply' _ _ _ + +lemma run_copy_decode (w : List Bool) (bits : List Bool) (xs : List Input) + (count : Option (Fin (ci.size + 1))) + (hw : w = copyPrefix ci xs count ++ bits) + (h : (copyPrefix ci xs count).length ≤ w.length) : + Parsed M ci co w xs (ci.decodeFrom (count.map Fin.val) bits) + ((machine M ci co).runFrom (copyCfg M ci co w xs count h) (bits.length + 1)) := by + induction bits generalizing xs count with + | nil => + cases count with + | none => + change (machine M ci co).runFrom (copyCfg M ci co w xs none h) 1 = + endCfg M ci co w (xs ++ []) + simpa [Turing.MultiTapeTM.runFrom] using + copy_end M ci co xs (by simpa [copyPrefix, pendingPrefix] using hw) h + | some i => + have hin := copyCfg_read M ci co xs (some i) h [] hw + change ((machine M ci co).step (copyCfg M ci co w xs (some i) h)).state = none ∧ + ((machine M ci co).step (copyCfg M ci co w xs (some i) h)).output = [] + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty (some i)) rfl] + simp only [machine, hin, List.head?_nil] + simp [pause, Turing.Action.apply, copyCfg] + | cons b bits ih => + have hin := copyCfg_read M ci co xs count h (b :: bits) hw + cases count with + | none => + cases b with + | true => + change ((machine M ci co).runFrom (copyCfg M ci co w xs none h) + (bits.length + 1 + 1)).state = none ∧ + ((machine M ci co).runFrom (copyCfg M ci co w xs none h) + (bits.length + 1 + 1)).output = [] + apply run_rejected + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty none) rfl] + simp only [machine, hin, List.head?_cons] + simp [pause, Turing.Action.apply, copyCfg] + | false => + have hp : copyPrefix ci xs (some 0) = copyPrefix ci xs none ++ [false] := by + simp [copyPrefix, pendingPrefix] + have hw' : w = copyPrefix ci xs (some 0) ++ bits := by + simpa [hp, List.append_assoc] using hw + have h' : (copyPrefix ci xs (some 0)).length ≤ w.length := by simp [hw'] + change Parsed M ci co w xs (ci.decodeFrom (some 0) bits) + ((machine M ci co).runFrom (copyCfg M ci co w xs none h) (bits.length + 1 + 1)) + rw [run_succ_first, copy_open M ci co xs bits hw h h'] + exact ih xs (some 0) hw' h' + | some i => + cases b with + | true => + by_cases hi : i.val + 1 < ci.size + · have hp : + copyPrefix ci xs (some ⟨i.val + 1, by omega⟩) = + copyPrefix ci xs (some i) ++ [true] := by + simp [copyPrefix, pendingPrefix, List.replicate_add, List.append_assoc] + have hw' : w = copyPrefix ci xs (some ⟨i.val + 1, by omega⟩) ++ bits := by + simpa [hp, List.append_assoc] using hw + have h' : (copyPrefix ci xs (some ⟨i.val + 1, by omega⟩)).length ≤ w.length := by + simp [hw'] + change Parsed M ci co w xs + (if i.val + 1 < ci.size then ci.decodeFrom (some (i.val + 1)) bits else none) + ((machine M ci co).runFrom (copyCfg M ci co w xs (some i) h) + (bits.length + 1 + 1)) + rw [ite_eq_left hi, run_succ_first, copy_true M ci co xs i bits hw hi h h'] + exact ih xs (some ⟨i.val + 1, by omega⟩) hw' h' + · change Parsed M ci co w xs + (if i.val + 1 < ci.size then ci.decodeFrom (some (i.val + 1)) bits else none) + ((machine M ci co).runFrom (copyCfg M ci co w xs (some i) h) + (bits.length + 1 + 1)) + rw [ite_eq_right hi] + apply run_rejected + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty (some i)) rfl] + simp only [machine, hin, List.head?_cons] + simp [hi, pause, Turing.Action.apply, copyCfg] + | false => + by_cases hi : i.val < ci.size + · let a := ci.index.symm ⟨i.val, hi⟩ + have hw' : w = copyPrefix ci (xs ++ [a]) none ++ bits := by + simpa [a, copy_close_prefix ci xs i hi, List.append_assoc] using hw + have h' : (copyPrefix ci (xs ++ [a]) none).length ≤ w.length := by simp [hw'] + change Parsed M ci co w xs + (if hi : i.val < ci.size then + (ci.decodeFrom none bits).map (ci.index.symm ⟨i.val, hi⟩ :: ·) else none) + ((machine M ci co).runFrom (copyCfg M ci co w xs (some i) h) + (bits.length + 1 + 1)) + rw [dite_eq_left hi, run_succ_first, copy_close M ci co xs i bits hw hi h h'] + exact (parsed_cons M ci co xs a _ _).mp (ih (xs ++ [a]) none hw' h') + · change Parsed M ci co w xs + (if hi : i.val < ci.size then + (ci.decodeFrom none bits).map (ci.index.symm ⟨i.val, hi⟩ :: ·) else none) + ((machine M ci co).runFrom (copyCfg M ci co w xs (some i) h) + (bits.length + 1 + 1)) + rw [dite_eq_right hi] + apply run_rejected + rw [Turing.MultiTapeTM.step_apply_of_state (q := .copy xs.isEmpty (some i)) rfl] + simp only [machine, hin, List.head?_cons] + simp [hi, pause, Turing.Action.apply, copyCfg] + +lemma copy_encoded (xs : List Input) : + (machine M ci co).runFrom ((machine M ci co).initCfg (ci.encode xs)) + ((ci.encode xs).length + 1) = endCfg M ci co (ci.encode xs) xs := by + have h := run_copy_decode M ci co (ci.encode xs) (ci.encode xs) [] none + (by simp [copyPrefix, pendingPrefix, encode]) (by simp [copyPrefix, pendingPrefix, encode]) + have hd : ci.decodeFrom none (ci.encode xs) = some xs := decode_encode_proof ci xs + simpa only [Option.map_none, hd, Parsed, + List.nil_append, copyCfg_zero] using h + +lemma copy_invalid (w : List Bool) (hw : ci.decode w = none) : + ((machine M ci co).runFrom ((machine M ci co).initCfg w) (w.length + 1)).state = none ∧ + ((machine M ci co).runFrom ((machine M ci co).initCfg w) (w.length + 1)).output = [] := by + have h := run_copy_decode M ci co w w [] none + (by simp [copyPrefix, pendingPrefix, encode]) (by simp [copyPrefix, pendingPrefix, encode]) + have hd : ci.decodeFrom none w = none := hw + simpa only [Option.map_none, hd, Parsed, copyCfg_zero] using h + +end Complexity.MathlibTM0.AlphabetMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Simulation.lean b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Simulation.lean new file mode 100644 index 000000000..b169cbae9 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Alphabet/Internal/Simulation.lean @@ -0,0 +1,60 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Copy + +/-! +# Reusing source-step simulation with finite-alphabet endpoints + +During source execution, the new machine embeds the existing simulator without +changing its work tapes, input position, output, or transition count. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.AlphabetMachine + +variable {Input Output : Type} (M : AlphabetMachine Input Output) +variable (ci : FiniteAlphabetCode Input) (co : FiniteAlphabetCode Output) + +/-- Embed existing source-execution or emission control. -/ +def liftCfg {w : List Bool} + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool (Control M.State) w) : + Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool + (Phase ci.size co.size M.State) w := + d.mapState (Option.map Phase.source) + +lemma lift_step_of_run {w : List Bool} + (d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool (Control M.State) w) + (q : M.State) (hq : d.state = some (.run q)) : + (machine M ci co).step (liftCfg M ci co d) = + liftCfg M ci co ((MathlibTM0.machine M.simulatorSource.binary).step d) := by + rw [Turing.MultiTapeTM.step_apply_of_state (q := .source (.run q)) + (by simp [liftCfg, Turing.Cfg.mapState, hq]), + Turing.MultiTapeTM.step_apply_of_state hq] + rfl + +lemma lift_source_run {w : List Bool} + {d : Turing.Cfg (Fintype.card M.simulatorSource.binary.Alphabet) Bool (Control M.State) w} + {c c' : M.simulatorSource.binary.Config} + (h : Sim M.simulatorSource.binary c d) (t : ℕ) + (hr : M.simulatorSource.binary.run t c = some c') : + (machine M ci co).runFrom (liftCfg M ci co d) t = + liftCfg M ci co ((MathlibTM0.machine M.simulatorSource.binary).runFrom d t) := by + induction t generalizing c d with + | zero => rfl + | succ t ih => + cases hs : Turing.TM0.step M.simulatorSource.binary.code c with + | none => simp [BinaryMachine.run, hs] at hr + | some next => + have hn : M.simulatorSource.binary.run t next = some c' := by + simpa only [BinaryMachine.run, hs, Option.bind_some] using hr + simp only [Turing.MultiTapeTM.runFrom, Function.iterate_succ_apply] + rw [lift_step_of_run M ci co d c.q h.state] + exact ih (h.step hs) hn + +end Complexity.MathlibTM0.AlphabetMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Internal/Output.lean b/Complexitylib/Interop/Mathlib/TM0/Internal/Output.lean index 0b19e9967..dff091ab8 100644 --- a/Complexitylib/Interop/Mathlib/TM0/Internal/Output.lean +++ b/Complexitylib/Interop/Mathlib/TM0/Internal/Output.lean @@ -98,6 +98,40 @@ lemma Emitting.run {M : BinaryMachine} {input : List Bool} (output : List Bool) have hr := ih (h.step_bit hb) ht simpa [Turing.MultiTapeTM.runFrom, Function.iterate_succ_apply, List.append_assoc] using hr +/-- Output emission only needs the decoded symbols, allowing distinct input and output formats. -/ +lemma Emitting.run_decoded {M : BinaryMachine} {input : List Bool} (output : List Bool) + {tape : Turing.Tape M.Alphabet} {emitted : List Bool} + {d : Turing.Cfg (Fintype.card M.Alphabet) Bool (Control M.State) input} + (h : Emitting M tape emitted d) + (ho : ∀ j : ℕ, M.readBit (tape.nth j) = output[j]?) : + ((machine M).runFrom d (output.length + 1)).state = none ∧ + ((machine M).runFrom d (output.length + 1)).output = emitted ++ output := by + induction output generalizing tape emitted d with + | nil => + have hb : M.readBit tape.head = none := by simpa using ho 0 + change ((machine M).step d).state = none ∧ + ((machine M).step d).output = emitted ++ [] + rw [Turing.MultiTapeTM.step_apply_of_state h.state] + simp [machine, h.read, hb, idle, Turing.Action.apply, h.output] + | cons b output ih => + have hb : M.readBit tape.head = some b := by simpa using ho 0 + have hs : Emitting M (tape.move .right) (emitted ++ [b]) ((machine M).step d) := by + rw [Turing.MultiTapeTM.step_apply_of_state h.state] + simp only [machine, h.read, hb] + refine ⟨rfl, ?_, ?_⟩ + · simp [Turing.Action.apply, h.output] + · intro i z + simp only [Turing.Action.apply, Turing.Tape.move_right_nth, SignType.coe_one] + convert h.tape i (z + 1) using 1 + congr 1 + lia + have ht : ∀ j : ℕ, M.readBit ((tape.move .right).nth j) = output[j]? := by + intro j + rw [Turing.Tape.move_right_nth] + simpa only [List.getElem?_cons_succ, Nat.cast_add, Nat.cast_one] using ho (j + 1) + simpa [Turing.MultiTapeTM.runFrom, Function.iterate_succ_apply, List.append_assoc] using + ih hs ht + lemma computes_proof (M : BinaryMachine) {input output : List Bool} {time : ℕ} (h : M.OutputsWithin input output time) : (machine M).ComputesInTimeAndSpace input output diff --git a/Complexitylib/Interop/Mathlib/TM0/Marked.lean b/Complexitylib/Interop/Mathlib/TM0/Marked.lean new file mode 100644 index 000000000..8f3ae34f7 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Marked.lean @@ -0,0 +1,64 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Marked.Defs +public import Complexitylib.Interop.Cslib.FromMultiTape +public import Complexitylib.Interop.Cslib.Guard +import Complexitylib.Interop.Mathlib.TM0.Marked.Internal.Output + +/-! +# Marked finite-tape computations transfer to canonical FP + +The source input may distinguish its first symbol and use a marker for the +empty word. Output is read through a separate decoder. The existing binary-track +simulator handles these formats with linear initialization and emission cost. +-/ + +public section + +namespace Complexity.MathlibTM0.MarkedMachine + +/-- Marked input and a separate output decoder add only linear simulation overhead. -/ +theorem computes_cslib (M : MarkedMachine) {input output : List Bool} {time : ℕ} + (h : M.OutputsWithin input output time) : + (machine M).ComputesInTimeAndSpace input output + (2 * input.length + time + output.length + 4) + ((machine M).spaceUsed ((machine M).initCfg input) + (2 * input.length + time + output.length + 4)) := + computes_proof M h + +/-- Polynomial source time and decoded output length imply canonical FP membership. -/ +theorem mem_FP {M : MarkedMachine} {f : List Bool → List Bool} {p q : Polynomial ℕ} + (h : ∀ input, M.OutputsWithin input (f input) (p.eval input.length)) + (hlen : ∀ input, (f input).length ≤ q.eval input.length) : f ∈ FP := by + let time (input : List Bool) := + 2 * input.length + p.eval input.length + (f input).length + 4 + refine mem_FP_of_computableInTimeAndSpace + (p := Polynomial.C 2 * Polynomial.X + p + q + Polynomial.C 4) + (s := fun input => (machine M).spaceUsed ((machine M).initCfg input) (time input)) + ⟨_, TargetState M, inferInstance, machine M, ?_⟩ + intro input + refine ⟨time input, ?_, _, le_rfl, M.computes_cslib (h input)⟩ + simp only [time, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X] + exact Nat.add_le_add_right (Nat.add_le_add_left (hlen input) _) 4 + +/-- Source bounds only on a regular domain suffice after rejecting malformed inputs. -/ +theorem mem_FP_on_dfa {M : MarkedMachine} {σ : Type} [Fintype σ] + (D : DFA Bool σ) [DecidablePred (· ∈ D.accepts)] + {f : List Bool → List Bool} {p q : Polynomial ℕ} + (h : ∀ w ∈ D.accepts, M.OutputsWithin w (f w) (p.eval w.length)) + (hlen : ∀ w ∈ D.accepts, (f w).length ≤ q.eval w.length) : + (fun w => if w ∈ D.accepts then f w else []) ∈ FP := by + apply CslibGuard.mem_FP_on_dfa (machine M) D + (p := Polynomial.C 2 * Polynomial.X + p + q + Polynomial.C 4) + intro w hw + refine ⟨2 * w.length + p.eval w.length + (f w).length + 4, ?_, _, + M.computes_cslib (h w hw)⟩ + simp only [Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X] + exact Nat.add_le_add_right (Nat.add_le_add_left (hlen w hw) _) 4 + +end Complexity.MathlibTM0.MarkedMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Marked/Defs.lean b/Complexitylib/Interop/Mathlib/TM0/Marked/Defs.lean new file mode 100644 index 000000000..2818d182c --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Marked/Defs.lean @@ -0,0 +1,125 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Defs + +/-! +# Finite tape machines with a marked first input cell + +Stack encodings distinguish the first cell and represent the empty word by a +marker. This bundle keeps that format explicit, while allowing output symbols +to differ from input symbols. The binary-track simulator can decode the output +without rewriting the source tape. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0 + +/-- A finite TM0 with a first-cell input marker and a separate output decoder. -/ +structure MarkedMachine where + /-- Source tape alphabet. -/ + Alphabet : Type + /-- Source control states. -/ + State : Type + /-- The source alphabet is finite. -/ + [alphabetFinite : Fintype Alphabet] + /-- The default symbol is blank. -/ + [alphabetInhabited : Inhabited Alphabet] + /-- The source control is finite. -/ + [stateFinite : Fintype State] + /-- The default state is initial. -/ + [stateInhabited : Inhabited State] + /-- Encode a bit, distinguishing the first cell from subsequent cells. -/ + bit : Bool → Bool → Alphabet + /-- The single cell representing empty input. -/ + empty : Alphabet + /-- Decode output, stopping at the first symbol with no decoded bit. -/ + readBit : Alphabet → Option Bool + /-- Blank terminates output. -/ + readBit_blank : readBit default = none + /-- Source transitions. -/ + code : Turing.TM0.Machine Alphabet State + +attribute [instance] MarkedMachine.alphabetFinite MarkedMachine.alphabetInhabited + MarkedMachine.stateFinite MarkedMachine.stateInhabited + +namespace MarkedMachine + +/-- Source symbols plus unused raw bits needed by the existing simulator bundle. -/ +inductive Symbol (Γ : Type) where + /-- A raw bit in the simulator's ordinary binary interface. -/ + | raw : Bool → Symbol Γ + /-- An actual source symbol. -/ + | source : Γ → Symbol Γ + deriving Fintype + +instance {Γ : Type} [Inhabited Γ] : Inhabited (Symbol Γ) := ⟨.source default⟩ + +/-- Forget the unused raw-bit symbols. -/ +def Symbol.value {Γ : Type} [Inhabited Γ] : Symbol Γ → Γ + | .raw _ => default + | .source a => a + +/-- Format an input suffix; only an empty complete input needs the empty marker. -/ +def inputCells (M : MarkedMachine) : Bool → List Bool → List M.Alphabet + | first, [] => if first then [M.empty] else [] + | first, b :: bs => M.bit first b :: M.inputCells false bs + +/-- A source configuration with the bundle's blank and initial-state conventions. -/ +abbrev Config (M : MarkedMachine) := Turing.TM0.Cfg M.Alphabet M.State + +/-- Execute exactly the specified number of source transitions. -/ +def run (M : MarkedMachine) : ℕ → M.Config → Option M.Config + | 0, c => some c + | t + 1, c => (Turing.TM0.step M.code c).bind (M.run t) + +/-- A bounded source run whose complete decoded right-of-head output is specified. -/ +def OutputsWithin (M : MarkedMachine) (input output : List Bool) (time : ℕ) : Prop := + ∃ t ≤ time, ∃ c : M.Config, + M.run t (Turing.TM0.init (M.inputCells true input)) = some c ∧ + Turing.TM0.step M.code c = none ∧ + ∀ j : ℕ, M.readBit (c.Tape.nth j) = output[j]? + +/-- Add unused raw-bit symbols to reuse the existing binary-machine simulation. -/ +abbrev binary (M : MarkedMachine) : BinaryMachine where + Alphabet := Symbol M.Alphabet + alphabetInhabited := ⟨.source default⟩ + State := M.State + bit := .raw + readBit a := match a with + | .raw b => some b + | .source a => M.readBit a + readBit_bit _ := rfl + readBit_blank := M.readBit_blank + code := M.code.map ⟨Symbol.source, rfl⟩ ⟨Symbol.value, rfl⟩ id id + +/-- The new copying phase, followed by the existing rewind, run, and emit phases. -/ +abbrev TargetState (M : MarkedMachine) := Bool ⊕ Control M.State + +/-- Copy a marked source input, then delegate to the existing tape simulator. -/ +noncomputable def machine (M : MarkedMachine) : + Turing.MultiTapeTM (Fintype.card M.binary.Alphabet) Bool (TargetState M) where + q₀ := .inl true + tr q input work := match q with + | .inr q => + let a := (MathlibTM0.machine M.binary).tr q input work + { a with state := a.state.map Sum.inr } + | .inl first => match input with + | some b => + { state := some (.inl false), inputTape := 1, output := none, + workTapes := fun i => + (some (SymbolTracks.encode (.source (M.bit first b)) (trackEquiv M.binary i)), 1) } + | none => + { state := some (.inr .rewind), inputTape := -1, output := none, + workTapes := fun i => + (if first then + some (SymbolTracks.encode (.source M.empty) (trackEquiv M.binary i)) + else none, 0) } + +end MarkedMachine +end Complexity.MathlibTM0 diff --git a/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Initialization.lean b/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Initialization.lean new file mode 100644 index 000000000..1abc3d4d9 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Initialization.lean @@ -0,0 +1,209 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Marked.Internal.Simulation + +/-! +# Copying a first-cell-marked input + +The copier distinguishes the first input bit, writes the empty-input marker +when necessary, and then uses the existing simulator's rewind phase. The total +initialization cost remains exactly `2 * input.length + 2` transitions. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.MarkedMachine + +/-- Cells copied before the end of input is encountered. -/ +def prefixCells (M : MarkedMachine) : List Bool → List M.Alphabet + | [] => [] + | b :: bs => M.bit true b :: bs.map (M.bit false) + +lemma prefixCells_length (M : MarkedMachine) (w : List Bool) : + (M.prefixCells w).length = w.length := by + cases w <;> simp [prefixCells] + +lemma prefixCells_append (M : MarkedMachine) (w : List Bool) (b : Bool) : + M.prefixCells (w ++ [b]) = M.prefixCells w ++ [M.bit w.isEmpty b] := by + cases w <;> simp [prefixCells] + +lemma inputCells_false (M : MarkedMachine) (w : List Bool) : + M.inputCells false w = w.map (M.bit false) := by + induction w <;> simp_all [inputCells] + +lemma inputCells_true (M : MarkedMachine) (w : List Bool) : + M.inputCells true w = if w.isEmpty then [M.empty] else M.prefixCells w := by + cases w <;> simp [inputCells, prefixCells, inputCells_false] + +/-- Symbol tracks containing the specified complete source cells. -/ +noncomputable def contents (M : MarkedMachine) (cells : List M.Alphabet) + (i : Track M.binary) (z : ℤ) : Option Bool := + SymbolTracks.encode + ((Turing.Tape.mk₁ (cells.map Symbol.source) : Turing.Tape (Symbol M.Alphabet)).nth z) + (trackEquiv M.binary i) + +lemma contents_nil (M : MarkedMachine) : contents M [] = fun _ _ => none := by + funext i z + simp [contents, nth_mk₁, SymbolTracks.encode_default] + +lemma contents_append (M : MarkedMachine) (cells : List M.Alphabet) (a : M.Alphabet) + (i : Track M.binary) : + contents M (cells ++ [a]) i = Function.update (contents M cells i) cells.length + (SymbolTracks.encode (.source a) (trackEquiv M.binary i)) := by + funext z + simp only [contents, List.map_append, List.map_singleton, + nth_mk₁_append_singleton, List.length_map] + by_cases hz : z = cells.length <;> simp [hz, contents] + +/-- The marked copier after reading the first `t` input bits. -/ +noncomputable def copyCfg (M : MarkedMachine) (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (TargetState M) w where + state := some (.inl (t == 0)) + inputPos := ⟨t + 1, by lia⟩ + workTapes := contents M (M.prefixCells (w.take t)) + workTapePos := fun _ => t + output := [] + +/-- A rewind configuration with the complete marked input already on the tracks. -/ +noncomputable def rewindCfg (M : MarkedMachine) (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (TargetState M) w where + state := some (.inr .rewind) + inputPos := ⟨t, by lia⟩ + workTapes := contents M (M.inputCells true w) + workTapePos := fun _ => t + output := [] + +lemma copyCfg_zero (M : MarkedMachine) (w : List Bool) : + copyCfg M w 0 (by lia) = (machine M).initCfg w := by + simp [copyCfg, prefixCells, Turing.MultiTapeTM.initCfg, Turing.Cfg.init, + machine, contents_nil] + +lemma copyCfg_step (M : MarkedMachine) (w : List Bool) (t : ℕ) (ht : t < w.length) : + (machine M).step (copyCfg M w t ht.le) = copyCfg M w (t + 1) (by lia) := by + have hin : (copyCfg M w t ht.le).inputSymbol = some w[t] := + Turing.inputSymbolInner t (by simp [copyCfg, Nat.add_comm]) ht + have hpos : (copyCfg M w t ht.le).inputPos.val ≠ w.length + 1 := by simp [copyCfg]; lia + have hw : w.take (t + 1) = w.take t ++ [w[t]] := (List.take_concat_get' w t ht).symm + have hflag : (w.take t).isEmpty = (t == 0) := by + cases t <;> cases w <;> simp_all + rw [Turing.MultiTapeTM.step_apply_of_state (q := .inl (t == 0)) rfl] + simp only [machine, hin] + apply Turing.Cfg.ext + · simp [copyCfg] + · simpa [Turing.Action.apply, copyCfg] using + Turing.moveInputPos_pos_of_ne_right (copyCfg M w t ht.le).inputPos hpos + · funext i + simp only [Turing.Action.apply, copyCfg, hw, prefixCells_append, contents_append, hflag] + simp [prefixCells_length, List.length_take, min_eq_left ht.le] + · funext i + simp [Turing.Action.apply, copyCfg, Nat.cast_add, Nat.cast_one] + · rfl + +lemma copyCfg_end (M : MarkedMachine) (w : List Bool) : + (machine M).step (copyCfg M w w.length le_rfl) = rewindCfg M w w.length le_rfl := by + have hin : (copyCfg M w w.length le_rfl).inputSymbol = none := + Turing.inputSymbol_eq_none_of_boundary (Or.inr rfl) + rw [Turing.MultiTapeTM.step_apply_of_state (q := .inl (w.length == 0)) rfl] + simp only [machine, hin] + apply Turing.Cfg.ext + · rfl + · have hp : (copyCfg M w w.length le_rfl).inputPos ≠ 0 := by + intro he + have := congrArg Fin.val he + simp [copyCfg] at this + simpa [Turing.Action.apply, copyCfg, rewindCfg] using + Turing.moveInputPos_neg_of_ne_left (copyCfg M w w.length le_rfl).inputPos hp + · funext i + cases w with + | nil => + simpa [Turing.Action.apply, copyCfg, rewindCfg, inputCells, prefixCells, contents_nil] + using (contents_append M [] M.empty i).symm + | cons b bs => + simp [Turing.Action.apply, copyCfg, rewindCfg, inputCells_true] + · funext i + simp [Turing.Action.apply, copyCfg, rewindCfg] + · rfl + +lemma rewindCfg_step (M : MarkedMachine) (w : List Bool) + (t : ℕ) (ht : t + 1 ≤ w.length) : + (machine M).step (rewindCfg M w (t + 1) ht) = rewindCfg M w t (by lia) := by + have hin : (rewindCfg M w (t + 1) ht).inputSymbol = some (w[t]'(by lia)) := + Turing.inputSymbolInner t (by simp [rewindCfg, Nat.add_comm]) (by lia) + rw [Turing.MultiTapeTM.step_apply_of_state (q := .inr .rewind) rfl] + simp only [machine, MathlibTM0.machine, hin] + apply Turing.Cfg.ext + · rfl + · have hp : (rewindCfg M w (t + 1) ht).inputPos ≠ 0 := by + intro he + have := congrArg Fin.val he + simp [rewindCfg] at this + simpa [idle, Turing.Action.apply, rewindCfg] using + Turing.moveInputPos_neg_of_ne_left (rewindCfg M w (t + 1) ht).inputPos hp + · rfl + · funext i + simp [Turing.Action.apply, rewindCfg] + · rfl + +lemma run_add (M : MarkedMachine) {w : List Bool} + (d : Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (TargetState M) w) (t u : ℕ) : + (machine M).runFrom d (t + u) = (machine M).runFrom ((machine M).runFrom d t) u := by + simp only [Turing.MultiTapeTM.runFrom, Nat.add_comm t u, Function.iterate_add_apply] + +lemma run_succ (M : MarkedMachine) {w : List Bool} + (d : Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (TargetState M) w) (t : ℕ) : + (machine M).runFrom d (t + 1) = (machine M).step ((machine M).runFrom d t) := + Function.iterate_succ_apply' _ _ _ + +lemma run_copy (M : MarkedMachine) (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + (machine M).runFrom ((machine M).initCfg w) t = copyCfg M w t ht := by + induction t with + | zero => exact (copyCfg_zero M w).symm + | succ t ih => rw [run_succ, ih (by lia), copyCfg_step M w t (by lia)] + +lemma run_rewind (M : MarkedMachine) (w : List Bool) (t : ℕ) (ht : t ≤ w.length) : + (machine M).runFrom (rewindCfg M w t ht) t = rewindCfg M w 0 (by lia) := by + induction t with + | zero => rfl + | succ t ih => + change (machine M).step^[t + 1] (rewindCfg M w (t + 1) ht) = _ + rw [Function.iterate_succ_apply, rewindCfg_step M w t ht] + exact ih (by lia) + +/-- The legacy simulator configuration at the start of source execution. -/ +noncomputable def readyCfg (M : MarkedMachine) (w : List Bool) : + Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (Control M.State) w where + state := some (.run default) + inputPos := 0 + workTapes := contents M (M.inputCells true w) + workTapePos := fun _ => 0 + output := [] + +lemma ready_sim (M : MarkedMachine) (w : List Bool) : + Sim M.binary (sourceCfg M (Turing.TM0.init (M.inputCells true w))) (readyCfg M w) := by + refine ⟨rfl, rfl, ?_⟩ + intro i z + simp [readyCfg, contents, sourceCfg, Turing.TM0.Cfg.map, Turing.TM0.init, + Turing.Tape.map_mk₁] + +lemma rewindCfg_zero (M : MarkedMachine) (w : List Bool) : + (machine M).step (rewindCfg M w 0 (by lia)) = liftCfg M (readyCfg M w) := by + have hin : (rewindCfg M w 0 (by lia)).inputSymbol = none := + Turing.inputSymbol_eq_none_of_boundary (Or.inl rfl) + rw [Turing.MultiTapeTM.step_apply_of_state (q := .inr .rewind) rfl] + simp only [machine, MathlibTM0.machine, hin] + simp [idle, Turing.Action.apply, + rewindCfg, liftCfg, readyCfg, Turing.Cfg.mapState] + +lemma init_ready (M : MarkedMachine) (w : List Bool) : + (machine M).runFrom ((machine M).initCfg w) (2 * w.length + 2) = + liftCfg M (readyCfg M w) := by + have ht : 2 * w.length + 2 = ((w.length + 1) + w.length) + 1 := by lia + rw [ht, run_succ, run_add, run_succ, run_copy M w w.length le_rfl, + copyCfg_end, run_rewind, rewindCfg_zero] + +end Complexity.MathlibTM0.MarkedMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Output.lean b/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Output.lean new file mode 100644 index 000000000..111b613ac --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Output.lean @@ -0,0 +1,57 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Marked.Internal.Initialization + +/-! +# Complete decoded output from a marked-input machine + +After marked input copying, the existing source simulation and output emitter +run without modification. Input preparation and output emission have linear +cost, and every source transition takes one target transition. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.MarkedMachine + +lemma computes_proof (M : MarkedMachine) {input output : List Bool} {time : ℕ} + (h : M.OutputsWithin input output time) : + (machine M).ComputesInTimeAndSpace input output + (2 * input.length + time + output.length + 4) + ((machine M).spaceUsed ((machine M).initCfg input) + (2 * input.length + time + output.length + 4)) := by + obtain ⟨t, ht, c, hr, hh, ho⟩ := h + have hs := (ready_sim M input).run t (by rw [source_run, hr]; rfl) + have he := hs.halt (by rw [source_step, hh]; rfl) + have hy := he.run_decoded output (fun j => by + rw [source_nth] + exact ho j) + rw [← MathlibTM0.runFrom_succ M.binary, ← MathlibTM0.runFrom_add M.binary] at hy + have hrun : (machine M).runFrom ((machine M).initCfg input) + ((2 * input.length + 2) + (t + 1 + (output.length + 1))) = + liftCfg M ((MathlibTM0.machine M.binary).runFrom (readyCfg M input) + (t + 1 + (output.length + 1))) := by + rw [run_add, init_ready, lift_run] + have hhalt : ((machine M).runFrom ((machine M).initCfg input) + (2 * input.length + t + output.length + 4)).state = none := by + have ht' : 2 * input.length + t + output.length + 4 = + (2 * input.length + 2) + (t + 1 + (output.length + 1)) := by lia + rw [ht', hrun] + simp [liftCfg, Turing.Cfg.mapState, hy.1] + have hout : ((machine M).runFrom ((machine M).initCfg input) + (2 * input.length + t + output.length + 4)).output = output := by + have ht' : 2 * input.length + t + output.length + 4 = + (2 * input.length + 2) + (t + 1 + (output.length + 1)) := by lia + rw [ht', hrun] + simpa [liftCfg, Turing.Cfg.mapState] using hy.2 + have heq := (machine M).runFrom_eq_of_halt ((machine M).initCfg input) + (by lia : 2 * input.length + t + output.length + 4 ≤ + 2 * input.length + time + output.length + 4) hhalt + exact ⟨by rw [heq]; exact hhalt, by rw [heq]; exact hout, rfl⟩ + +end Complexity.MathlibTM0.MarkedMachine diff --git a/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Simulation.lean b/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Simulation.lean new file mode 100644 index 000000000..7a956ff10 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM0/Marked/Internal/Simulation.lean @@ -0,0 +1,76 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM0.Marked.Defs +public import Complexitylib.Interop.Mathlib.TM0.Internal.Output + +/-! +# Reusing the binary-track source simulation + +The marked-input machine embeds the existing simulator's control after copying. +Mapping source symbols to a tagged alphabet adds no source transitions. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM0.MarkedMachine + +/-- Embed a source configuration in the alphabet with unused raw-bit symbols. -/ +def sourceCfg (M : MarkedMachine) (c : M.Config) : M.binary.Config := + c.map ⟨Symbol.source, rfl⟩ id + +lemma source_init (M : MarkedMachine) (cells : List M.Alphabet) : + sourceCfg M (Turing.TM0.init cells) = + Turing.TM0.init (cells.map Symbol.source) := by + exact Turing.TM0.map_init ⟨Symbol.source, rfl⟩ ⟨id, rfl⟩ cells + +lemma source_nth (M : MarkedMachine) (c : M.Config) (j : ℕ) : + (sourceCfg M c).Tape.nth j = Symbol.source (c.Tape.nth j) := by + cases j <;> simp [sourceCfg, Turing.TM0.Cfg.map, Turing.Tape.map, + Turing.Tape.nth, Turing.ListBlank.nth_map] + +lemma source_step (M : MarkedMachine) (c : M.Config) : + Turing.TM0.step M.binary.code (sourceCfg M c) = + (Turing.TM0.step M.code c).map (sourceCfg M) := by + exact (Turing.TM0.Machine.map_step M.code + ⟨Symbol.source, rfl⟩ ⟨Symbol.value, rfl⟩ id id + (S := Set.univ) (fun _ => rfl) (fun _ _ => rfl) c (Set.mem_univ _)).symm + +lemma source_run (M : MarkedMachine) (t : ℕ) (c : M.Config) : + M.binary.run t (sourceCfg M c) = (M.run t c).map (sourceCfg M) := by + induction t generalizing c with + | zero => rfl + | succ t ih => + simp only [BinaryMachine.run, run, source_step] + cases Turing.TM0.step M.code c <;> simp_all + +/-- Embed a configuration of the existing simulator in the marked-input machine. -/ +def liftCfg (M : MarkedMachine) {w : List Bool} + (d : Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (Control M.State) w) : + Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (TargetState M) w := + d.mapState (Option.map Sum.inr) + +lemma lift_step (M : MarkedMachine) {w : List Bool} + (d : Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (Control M.State) w) : + (machine M).step (liftCfg M d) = liftCfg M ((MathlibTM0.machine M.binary).step d) := by + cases hs : d.state with + | none => simp [Turing.MultiTapeTM.step, liftCfg, Turing.Cfg.mapState, hs] + | some q => + simp only [Turing.MultiTapeTM.step, liftCfg, Turing.Cfg.mapState, hs, Option.map_some] + rfl + +lemma lift_run (M : MarkedMachine) {w : List Bool} + (d : Turing.Cfg (Fintype.card M.binary.Alphabet) Bool (Control M.State) w) (t : ℕ) : + (machine M).runFrom (liftCfg M d) t = + liftCfg M ((MathlibTM0.machine M.binary).runFrom d t) := by + induction t generalizing d with + | zero => rfl + | succ t ih => + simp only [Turing.MultiTapeTM.runFrom, Function.iterate_succ_apply, lift_step] + exact ih _ + +end Complexity.MathlibTM0.MarkedMachine diff --git a/Complexitylib/Interop/Mathlib/TM2.lean b/Complexitylib/Interop/Mathlib/TM2.lean new file mode 100644 index 000000000..e323d3d0d --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2.lean @@ -0,0 +1,91 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.Defs +import Complexitylib.Interop.Mathlib.TM2.Internal.Compiler + +/-! +# Mathlib's stack-to-tape compiler has polynomial overhead + +Mathlib already compiles `TM2 → TM1 → TM0`. This module proves that its compiler +is efficient: a fixed finitely supported stack program has one finite-state +tape simulator running in `C * (input.length + sourceTime + 1)^2` transitions. +The constant depends only on the program. The stack-track alphabet is finite +whenever there are finitely many stacks and each stack alphabet is finite. + +`exists_tm0_computer` applies directly to Mathlib's bundled `FinTM2` and +`TM2OutputsInTime`. Both the input and the complete output tape are exactly +`TM2to1.trInit` of the corresponding stack word, including its bottom marker. +This format stores stacks bottom first. The separate +`Complexitylib.Interop.Mathlib.TM2.FP` interface supplies the binary input/output +adapters and canonical `FP` transfer for finite working alphabets. + +The timing proofs are reused from the rational hitting-list development +and remain in their original modules and namespaces. +-/ + +public section + +namespace Complexity.MathlibTM2 + +open Turing + +/-- Every output stack has length at most the initial input length plus a fixed +multiple of the source transition count. -/ +theorem exists_output_length_bound (tm : FinTM2) : + ∃ C : ℕ, 0 < C ∧ ∀ (input : List (tm.Γ tm.k₀)) (output : List (tm.Γ tm.k₁)) (time : ℕ), + TM2OutputsInTime tm input (some output) time → + output.length ≤ input.length + C * time := + output_length_bound tm + +/-- Mathlib's existing stack compiler simulates every terminating source run +with quadratic transition overhead, retaining the entire represented stack state. -/ +theorem exists_tm0_simulator {K : Type} {Γ : K → Type} {Λ σ : Type} + [DecidableEq K] [Inhabited Λ] [Inhabited σ] [Fintype σ] + (M : Λ → TM2.Stmt Γ Λ σ) (F : Finset Λ) (hF : TM2.Supports M F) : + ∃ (m : ℕ) (G : TM0.Machine (TM2to1.Γ' K Γ) (Fin (m + 1))) (C : ℕ), + 0 < C ∧ ∀ (k : K) (w : List (Γ k)) (b : TM2.Cfg Γ Λ σ) (t : ℕ), + exec (TM2.step M) t (TM2.init k w) = some b → TM2.step M b = none → + ∃ u ≤ C * (w.length + t + 1) ^ 2, ∃ a, + exec (TM0.step G) u (TM0.init (TM2to1.trInit k w)) = some a ∧ + TM0.step G a = none ∧ + ∃ c : TM1.Cfg (TM2to1.Γ' K Γ) (TM2to1.Λ' K Γ Λ σ) σ, + TM2to1.TrCfg b c ∧ a.Tape = c.Tape := + compile_source M F hF + +/-- A bundled Mathlib TM2 has a fixed finite-state tape computer with quadratic +overhead and exact input/output stack-track tapes, including empty words. -/ +theorem exists_tm0_computer (tm : FinTM2) : + ∃ (m : ℕ) (G : TM0.Machine (TM2to1.Γ' tm.K tm.Γ) (Fin (m + 1))) (C : ℕ), + 0 < C ∧ ∀ (input : List (tm.Γ tm.k₀)) (output : List (tm.Γ tm.k₁)) (time : ℕ), + TM2OutputsInTime tm input (some output) time → + ∃ u ≤ C * (input.length + time + 1) ^ 2, ∃ a, + exec (TM0.step G) u (TM0.init (TM2to1.trInit tm.k₀ input)) = some a ∧ + TM0.step G a = none ∧ a.Tape = Tape.mk₁ (TM2to1.trInit tm.k₁ output) := + compile_bundled tm + +/-- A Mathlib polynomial-time certificate yields one finite-state TM0 and a +polynomial time bound, with its encoded input and complete output tapes explicit. -/ +theorem exists_tm0_polynomial_computer {α β Γ Δ : Type} + {input : α → List Γ} {output : β → List Δ} {f : α → β} + (computer : TM2ComputableInPolyTime input output f) : + ∃ (m : ℕ) + (G : TM0.Machine (TM2to1.Γ' computer.tm.K computer.tm.Γ) (Fin (m + 1))) + (p : Polynomial ℕ), ∀ a, + ∃ u ≤ p.eval (input a).length, ∃ c, + exec (TM0.step G) u + (TM0.init (TM2to1.trInit computer.tm.k₀ + ((input a).map computer.inputAlphabet.symm))) = some c ∧ + TM0.step G c = none ∧ + c.Tape = Tape.mk₁ (TM2to1.trInit computer.tm.k₁ + ((output (f a)).map computer.outputAlphabet.symm)) := by + obtain ⟨m, G, C, _, hG⟩ := exists_tm0_computer computer.tm + refine ⟨m, G, Polynomial.C C * (Polynomial.X + computer.time + 1) ^ 2, ?_⟩ + intro a + simpa using hG _ _ _ (computer.outputsFun a) + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/Defs.lean b/Complexitylib/Interop/Mathlib/TM2/Defs.lean new file mode 100644 index 000000000..75fca5c04 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/Defs.lean @@ -0,0 +1,25 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Mathlib.Computability.TuringMachine.Computable +public import Complexitylib.RationalHitting.Internal.StackLanguage + +/-! +# Execution of finite Mathlib stack machines + +Reuse the exact-run definition from the rational hitting-list development. +The TM2 bridge keeps its original definition and proofs in place and exposes +that execution convention alongside the bundled Mathlib computation types. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +export RationalHitting.TMTime (exec) + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/FP.lean b/Complexitylib/Interop/Mathlib/TM2/FP.lean new file mode 100644 index 000000000..9b2e227b2 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/FP.lean @@ -0,0 +1,82 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2 +public import Complexitylib.Interop.Mathlib.TM2.FP.Defs +public import Complexitylib.Classes.P.Defs +public import Mathlib.Computability.DFA +public import Complexitylib.Encoding.FiniteAlphabet +import Complexitylib.Interop.Mathlib.TM2.FP.Internal.Compiler +import Complexitylib.Interop.Mathlib.TM2.FP.Internal.Guard +import Complexitylib.Interop.Mathlib.TM2.FP.Internal.AlphabetCompiler + +/-! +# Mathlib TM2 string computations belong to canonical FP + +A polynomial-time Mathlib certificate for a total binary-string function yields +membership in Complexitylib's canonical machine-based FP. All working stack +alphabets are explicitly required to be finite; Mathlib's FinTM2 bundle itself +only includes input-alphabet finiteness. Marked input copying, complete output +decoding, and string reversal account for the stack representation. + +The total-function theorem uses the identity encoding on all binary strings. +For a regular domain, `mem_FP_on_dfa` checks the input format and returns empty +output outside the domain; the source time bound is required only inside it. +`mem_FP_encoded` supports independent finite input and output alphabets through +explicit binary symbol codes, including empty alphabets. Invalid symbol codes +return empty output before the source starts. `mem_FP_encoded_on_dfa` additionally +validates a regular binary input domain. Typed problem encodings still need +their own validity and encoding-correspondence arguments. +-/ + +public section + +namespace Complexity.MathlibTM2 + +/-- Total Mathlib TM2 binary-string computations with finite working alphabets +transfer to the canonical FP class, with no separate output-length assumption. -/ +theorem mem_FP {f : List Bool → List Bool} + (computer : Turing.TM2ComputableInPolyTime id id f) + [∀ k, Fintype (computer.tm.Γ k)] : f ∈ FP := + mem_FP_proof computer + +/-- Validate a regular input domain and return empty output on malformed strings. +Only valid inputs need a source time bound; output length is bounded automatically. -/ +theorem mem_FP_on_dfa {f : List Bool → List Bool} + (computer : Turing.TM2ComputableAux Bool Bool) + [∀ k, Fintype (computer.tm.Γ k)] + {σ : Type} [Fintype σ] (D : DFA Bool σ) [DecidablePred (· ∈ D.accepts)] + {p : Polynomial ℕ} + (h : ∀ w ∈ D.accepts, + Turing.TM2OutputsInTime computer.tm (w.map computer.inputAlphabet.symm) + (some ((f w).map computer.outputAlphabet.symm)) (p.eval w.length)) : + (fun w => if w ∈ D.accepts then f w else []) ∈ FP := + mem_FP_on_dfa_proof computer D h + +/-- Polynomial-time word functions over independent finite alphabets give canonical FP functions +under explicit binary codes. Invalid codewords return empty output; +no output-size bound is needed. -/ +theorem mem_FP_encoded {α β : Type} {f : List α → List β} + (ci : FiniteAlphabetCode α) (co : FiniteAlphabetCode β) + (computer : Turing.TM2ComputableInPolyTime id id f) + [∀ k, Fintype (computer.tm.Γ k)] : ci.map co f ∈ FP := + mem_FP_encoded_proof ci co computer + +/-- Validate a regular binary domain as well as finite-alphabet symbol codes. Source bounds are +required only for source words whose binary encodings lie in the domain. -/ +theorem mem_FP_encoded_on_dfa {α β : Type} {f : List α → List β} + (ci : FiniteAlphabetCode α) (co : FiniteAlphabetCode β) + (computer : Turing.TM2ComputableAux α β) [∀ k, Fintype (computer.tm.Γ k)] + {σ : Type} [Fintype σ] (D : DFA Bool σ) [DecidablePred (· ∈ D.accepts)] + {p : Polynomial ℕ} + (h : ∀ xs, ci.encode xs ∈ D.accepts → + Turing.TM2OutputsInTime computer.tm (xs.map computer.inputAlphabet.symm) + (some ((f xs).map computer.outputAlphabet.symm)) (p.eval xs.length)) : + (fun bits => if bits ∈ D.accepts then ci.map co f bits else []) ∈ FP := + mem_FP_encoded_on_dfa_proof ci co computer D h + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/FP/Defs.lean b/Complexitylib/Interop/Mathlib/TM2/FP/Defs.lean new file mode 100644 index 000000000..8e650b22a --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/FP/Defs.lean @@ -0,0 +1,62 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.Defs +public import Complexitylib.Interop.Mathlib.TM0.Marked.Defs +public import Complexitylib.Interop.Mathlib.TM0.Alphabet.Defs + +/-! +# Input and output alphabets for the compiled stack machine + +The adapter reads bits in stack-bottom order. Its input is therefore reversed +before interpreting it as a TM2 stack, and its output is emitted in reverse +stack order. Canonical string reversal accounts for both conventions. The +finite-alphabet adapter keeps input and output alphabets independent; explicit +binary coding is supplied by the endpoint simulator. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +open Turing + +/-- Give a compiled stack machine an explicit marked-input and decoded-output interface. -/ +abbrev binaryAdapter (tm : FinTM2) [∀ k, Fintype (tm.Γ k)] + (input : tm.Γ tm.k₀ ≃ Bool) (output : tm.Γ tm.k₁ ≃ Bool) + {Q : Type} [Fintype Q] [Inhabited Q] + (G : TM0.Machine (TM2to1.Γ' tm.K tm.Γ) Q) : MathlibTM0.MarkedMachine where + Alphabet := TM2to1.Γ' tm.K tm.Γ + alphabetFinite := by + let : Fintype tm.K := tm.kFin + infer_instance + alphabetInhabited := ⟨(false, fun _ => none)⟩ + State := Q + bit first b := (first, Function.update (fun _ => none) tm.k₀ (some (input.symm b))) + empty := (true, fun _ => none) + readBit a := (a.2 tm.k₁).map output + readBit_blank := rfl + code := G + +/-- Marked input and decoded output for independent finite source alphabets. -/ +abbrev alphabetAdapter {α β : Type} (tm : FinTM2) [∀ k, Fintype (tm.Γ k)] + (input : tm.Γ tm.k₀ ≃ α) (output : tm.Γ tm.k₁ ≃ β) + {Q : Type} [Fintype Q] [Inhabited Q] + (G : TM0.Machine (TM2to1.Γ' tm.K tm.Γ) Q) : MathlibTM0.AlphabetMachine α β where + Alphabet := TM2to1.Γ' tm.K tm.Γ + alphabetFinite := by + let : Fintype tm.K := tm.kFin + infer_instance + alphabetInhabited := ⟨(false, fun _ => none)⟩ + State := Q + inputSymbol first a := (first, Function.update (fun _ => none) tm.k₀ (some (input.symm a))) + empty := (true, fun _ => none) + readSymbol a := (a.2 tm.k₁).map output + readSymbol_blank := rfl + code := G + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/FP/Internal/AlphabetCompiler.lean b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/AlphabetCompiler.lean new file mode 100644 index 000000000..0c6d11459 --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/AlphabetCompiler.lean @@ -0,0 +1,140 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.FP.Internal.Compiler +public import Complexitylib.Interop.Mathlib.TM0.Alphabet +import Complexitylib.Interop.Mathlib.TM0.Alphabet.Internal.Copy +import Complexitylib.Classes.P.Composition +import Complexitylib.Classes.P.Cobham.Internal.Reverse +import Mathlib.Computability.NFA + +/-! +# Canonical FP transfer with finite input and output alphabets + +The compiler's stack tracks are unchanged. Explicit symbol codes add linear +parsing and emission cost. Palindromic codewords let ordinary binary reversal +restore the stack order, even when input and output alphabets differ. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +open Turing + +lemma alphabetAdapter_input {α β : Type} (tm : FinTM2) [∀ k, Fintype (tm.Γ k)] + (input : tm.Γ tm.k₀ ≃ α) (output : tm.Γ tm.k₁ ≃ β) + {Q : Type} [Fintype Q] [Inhabited Q] + (G : TM0.Machine (TM2to1.Γ' tm.K tm.Γ) Q) (w : List α) : + (alphabetAdapter tm input output G).inputCells true w = + TM2to1.trInit tm.k₀ (w.reverse.map input.symm) := by + cases w with + | nil => rfl + | cons a xs => + simp only [TM2to1.trInit, List.map_reverse, List.reverse_reverse] + change (alphabetAdapter tm input output G).inputSymbol true a :: + (alphabetAdapter tm input output G).inputCells false xs = _ + rw [MathlibTM0.AlphabetMachine.inputCells_false] + change (true, Function.update (fun _ => none) tm.k₀ (some (input.symm a))) :: + xs.map (fun x => (false, Function.update (fun _ => none) tm.k₀ (some (input.symm x)))) = + (true, Function.update (fun _ => none) tm.k₀ (some (input.symm a))) :: + (xs.map input.symm).map (fun b => (false, Function.update (fun _ => none) tm.k₀ (some b))) + rw [List.map_map] + rfl + +lemma coded_reversal {α β : Type} (ci : FiniteAlphabetCode α) (co : FiniteAlphabetCode β) + (f : List α → List β) : + (List.reverse ∘ ci.map co (fun xs => (f xs.reverse).reverse) ∘ List.reverse) = + ci.map co f := by + funext w + simp only [Function.comp_def, FiniteAlphabetCode.map, FiniteAlphabetCode.decode_reverse_eq] + cases ci.decode w <;> simp [FiniteAlphabetCode.encode_reverse_eq] + +lemma mem_FP_encoded_proof {α β : Type} {f : List α → List β} + (ci : FiniteAlphabetCode α) (co : FiniteAlphabetCode β) + (computer : TM2ComputableInPolyTime id id f) [∀ k, Fintype (computer.tm.Γ k)] : + ci.map co f ∈ FP := by + obtain ⟨m, G, C, _, hG⟩ := exists_tm0_computer computer.tm + obtain ⟨D, _, hD⟩ := exists_output_length_bound computer.tm + let M := alphabetAdapter computer.tm computer.inputAlphabet computer.outputAlphabet G + have hg : ci.map co (fun xs => (f xs.reverse).reverse) ∈ FP := by + apply MathlibTM0.AlphabetMachine.mem_FP (M := M) ci co + (p := Polynomial.C C * (Polynomial.X + computer.time + 1) ^ 2) + (q := Polynomial.X + Polynomial.C D * computer.time) + · intro xs + obtain ⟨u, hu, c, hc, hh, ho⟩ := hG _ _ _ (computer.outputsFun xs.reverse) + refine ⟨u, ?_, c, ?_, hh, ?_⟩ + · simpa using hu + · rw [marked_run_eq_exec] + change exec (TM0.step G) u (TM0.init (M.inputCells true xs)) = some c + dsimp only [M] + rw [alphabetAdapter_input] + exact hc + · intro j + change ((c.Tape.nth j).2 computer.tm.k₁).map computer.outputAlphabet = _ + rw [ho, decode_trInit] + simp [List.map_reverse] + · intro xs + have hd := hD _ _ _ (computer.outputsFun xs.reverse) + simpa using hd + have h := mem_FP_comp (mem_FP_comp reverse_mem_FP hg) reverse_mem_FP + rw [coded_reversal] at h + exact h + +lemma mem_FP_encoded_on_dfa_proof {α β : Type} {f : List α → List β} + (ci : FiniteAlphabetCode α) (co : FiniteAlphabetCode β) + (computer : TM2ComputableAux α β) [∀ k, Fintype (computer.tm.Γ k)] + {σ : Type} [Fintype σ] (D : DFA Bool σ) [DecidablePred (· ∈ D.accepts)] + {p : Polynomial ℕ} + (h : ∀ xs, ci.encode xs ∈ D.accepts → + TM2OutputsInTime computer.tm (xs.map computer.inputAlphabet.symm) + (some ((f xs).map computer.outputAlphabet.symm)) (p.eval xs.length)) : + (fun bits => if bits ∈ D.accepts then ci.map co f bits else []) ∈ FP := by + classical + obtain ⟨m, G, C, _, hG⟩ := exists_tm0_computer computer.tm + obtain ⟨L, _, hL⟩ := exists_output_length_bound computer.tm + let M := alphabetAdapter computer.tm computer.inputAlphabet computer.outputAlphabet G + let D' := D.toNFA.reverse.toDFA + have accepts (w : List Bool) : w ∈ D'.accepts ↔ w.reverse ∈ D.accepts := by simp [D'] + have valid (xs : List α) (hx : ci.encode xs ∈ D'.accepts) : + ci.encode xs.reverse ∈ D.accepts := by + rw [FiniteAlphabetCode.encode_reverse_eq] + exact (accepts _).mp hx + let g (xs : List α) := (f xs.reverse).reverse + have hg : (fun bits => if bits ∈ D'.accepts then ci.map co g bits else []) ∈ FP := by + apply MathlibTM0.AlphabetMachine.mem_FP_on_dfa (M := M) ci co D' + (p := Polynomial.C C * (Polynomial.X + p + 1) ^ 2) + (q := Polynomial.X + Polynomial.C L * p) + · intro xs hx + obtain ⟨u, hu, c, hc, hh, ho⟩ := hG _ _ _ (h xs.reverse (valid xs hx)) + refine ⟨u, ?_, c, ?_, hh, ?_⟩ + · simpa using hu + · rw [marked_run_eq_exec] + change exec (TM0.step G) u (TM0.init (M.inputCells true xs)) = some c + dsimp only [M] + rw [alphabetAdapter_input] + exact hc + · intro j + change ((c.Tape.nth j).2 computer.tm.k₁).map computer.outputAlphabet = _ + rw [ho, decode_trInit] + simp [g, List.map_reverse] + · intro xs hx + have hl := hL _ _ _ (h xs.reverse (valid xs hx)) + simpa [g] using hl + have hr := mem_FP_comp (mem_FP_comp reverse_mem_FP hg) reverse_mem_FP + have heq : + (List.reverse ∘ (fun bits => if bits ∈ D'.accepts then ci.map co g bits else []) ∘ + List.reverse) = (fun bits => if bits ∈ D.accepts then ci.map co f bits else []) := by + funext bits + by_cases hw : bits ∈ D.accepts + · simpa only [Function.comp_def, accepts, List.reverse_reverse, hw, ite_true] using + congrFun (coded_reversal ci co f) bits + · simp [Function.comp_def, accepts, hw] + rw [heq] at hr + exact hr + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Compiler.lean b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Compiler.lean new file mode 100644 index 000000000..b96c0e74b --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Compiler.lean @@ -0,0 +1,66 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.FP.Internal.Encoding +public import Complexitylib.Interop.Mathlib.TM2 +public import Complexitylib.Interop.Mathlib.TM0.Marked +import Complexitylib.Classes.P.Composition +import Complexitylib.Classes.P.Cobham.Internal.Reverse + +/-! +# Canonical FP membership from a Mathlib TM2 string certificate + +Compile the source once and supply its exact stack-track input through the +marked copier. The target computes reversal composed with the source function +and reversal. Closure of FP under composition removes those representation +conventions. A source stack-growth bound supplies the required output length. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +open Turing + +lemma marked_run_eq_exec (M : MathlibTM0.MarkedMachine) (t : ℕ) (c : M.Config) : + M.run t c = exec (TM0.step M.code) t c := by + induction t generalizing c with + | zero => rfl + | succ t ih => + simp only [MathlibTM0.MarkedMachine.run, exec] + cases TM0.step M.code c <;> simp_all + +lemma mem_FP_proof {f : List Bool → List Bool} + (computer : TM2ComputableInPolyTime id id f) [∀ k, Fintype (computer.tm.Γ k)] : + f ∈ FP := by + obtain ⟨m, G, C, _, hG⟩ := exists_tm0_computer computer.tm + obtain ⟨D, _, hD⟩ := exists_output_length_bound computer.tm + let M := binaryAdapter computer.tm computer.inputAlphabet computer.outputAlphabet G + let g (w : List Bool) := (f w.reverse).reverse + have hg : g ∈ FP := by + apply MathlibTM0.MarkedMachine.mem_FP (M := M) + (p := Polynomial.C C * (Polynomial.X + computer.time + 1) ^ 2) + (q := Polynomial.X + Polynomial.C D * computer.time) + · intro w + obtain ⟨u, hu, c, hc, hh, ho⟩ := hG _ _ _ (computer.outputsFun w.reverse) + refine ⟨u, ?_, c, ?_, hh, ?_⟩ + · simpa using hu + · rw [marked_run_eq_exec] + dsimp only [M] + rw [binaryAdapter_input] + exact hc + · intro j + change ((c.Tape.nth j).2 computer.tm.k₁).map computer.outputAlphabet = _ + rw [ho, decode_trInit] + simp [g, List.map_reverse] + · intro w + have hd := hD _ _ _ (computer.outputsFun w.reverse) + simpa [g] using hd + have h := mem_FP_comp (mem_FP_comp reverse_mem_FP hg) reverse_mem_FP + simpa [Function.comp_def, g] using h + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Encoding.lean b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Encoding.lean new file mode 100644 index 000000000..3900c4b0a --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Encoding.lean @@ -0,0 +1,71 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.FP.Defs +import Complexitylib.Interop.Mathlib.TM0.Marked.Internal.Initialization + +/-! +# Exact marked-input and output decoding identities + +Copying a binary word in order builds the stack representation of its reverse. +Reading a clean final stack tape in order likewise emits the reversed stack. +Both identities include the single marker cell used for an empty stack. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +open Turing + +lemma binaryAdapter_input (tm : FinTM2) [∀ k, Fintype (tm.Γ k)] + (input : tm.Γ tm.k₀ ≃ Bool) (output : tm.Γ tm.k₁ ≃ Bool) + {Q : Type} [Fintype Q] [Inhabited Q] + (G : TM0.Machine (TM2to1.Γ' tm.K tm.Γ) Q) (w : List Bool) : + (binaryAdapter tm input output G).inputCells true w = + TM2to1.trInit tm.k₀ (w.reverse.map input.symm) := by + cases w with + | nil => rfl + | cons b bs => + simp only [TM2to1.trInit, List.map_reverse, List.reverse_reverse] + change (binaryAdapter tm input output G).bit true b :: + (binaryAdapter tm input output G).inputCells false bs = _ + rw [MathlibTM0.MarkedMachine.inputCells_false] + change (true, Function.update (fun _ => none) tm.k₀ (some (input.symm b))) :: + bs.map (fun x => (false, Function.update (fun _ => none) tm.k₀ (some (input.symm x)))) = + (true, Function.update (fun _ => none) tm.k₀ (some (input.symm b))) :: + (bs.map input.symm).map (fun a => (false, Function.update (fun _ => none) tm.k₀ (some a))) + rw [List.map_map] + rfl + +lemma decode_trInit {K α : Type} [DecidableEq K] {Γ : K → Type} + (k : K) (e : Γ k ≃ α) (w : List (Γ k)) (j : ℕ) : + (((Tape.mk₁ (TM2to1.trInit k w)).nth j).2 k).map e = (w.reverse.map e)[j]? := by + rw [MathlibTM0.nth_mk₁] + simp only [Int.natCast_nonneg, not_lt_of_ge, ite_false, Int.toNat_natCast] + cases hw : w.reverse with + | nil => + simp only [TM2to1.trInit, hw] + change ((([(true, fun _ => none)] : List (TM2to1.Γ' K Γ)).getI j).2 k).map e = + ([] : List α)[j]? + cases j <;> rfl + | cons b bs => + simp only [TM2to1.trInit, hw] + change ((((true, Function.update (fun _ => none) k (some b)) :: + bs.map (fun a => (false, Function.update (fun _ => none) k (some a)))).getI j).2 k).map e = + ((b :: bs).map e)[j]? + cases j with + | zero => simp + | succ j => + simp only [List.map_cons, + List.getElem?_cons_succ, List.getI_eq_getElem?_getD, + List.getElem?_map] + cases bs[j]? with + | none => simp; rfl + | some a => simp + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Guard.lean b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Guard.lean new file mode 100644 index 000000000..e96967aaa --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/FP/Internal/Guard.lean @@ -0,0 +1,71 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.FP.Internal.Compiler +import Complexitylib.Classes.P.Composition +import Complexitylib.Classes.P.Cobham.Internal.Reverse +import Mathlib.Computability.NFA + +/-! +# Regular-domain TM2 computations + +Reverse the input DFA as well as the string representation, guard the marked +simulator, and then use polynomial-time reversal to restore the original order. +The source need only have a runtime bound on accepted strings. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +open Turing + +lemma mem_FP_on_dfa_proof {f : List Bool → List Bool} + (computer : TM2ComputableAux Bool Bool) [∀ k, Fintype (computer.tm.Γ k)] + {σ : Type} [Fintype σ] (D : DFA Bool σ) [DecidablePred (· ∈ D.accepts)] + {p : Polynomial ℕ} + (h : ∀ w ∈ D.accepts, TM2OutputsInTime computer.tm (w.map computer.inputAlphabet.symm) + (some ((f w).map computer.outputAlphabet.symm)) (p.eval w.length)) : + (fun w => if w ∈ D.accepts then f w else []) ∈ FP := by + classical + obtain ⟨m, G, C, _, hG⟩ := exists_tm0_computer computer.tm + obtain ⟨L, _, hL⟩ := exists_output_length_bound computer.tm + let M := binaryAdapter computer.tm computer.inputAlphabet computer.outputAlphabet G + let D' := D.toNFA.reverse.toDFA + have accepts (w : List Bool) : w ∈ D'.accepts ↔ w.reverse ∈ D.accepts := by + simp [D'] + let g (w : List Bool) := (f w.reverse).reverse + have hg : (fun w => if w ∈ D'.accepts then g w else []) ∈ FP := by + apply MathlibTM0.MarkedMachine.mem_FP_on_dfa (M := M) D' + (p := Polynomial.C C * (Polynomial.X + p + 1) ^ 2) + (q := Polynomial.X + Polynomial.C L * p) + · intro w hw + have hs := h w.reverse ((accepts w).mp hw) + obtain ⟨u, hu, c, hc, hh, ho⟩ := hG _ _ _ hs + refine ⟨u, ?_, c, ?_, hh, ?_⟩ + · simpa using hu + · rw [marked_run_eq_exec] + dsimp only [M] + rw [binaryAdapter_input] + exact hc + · intro j + change ((c.Tape.nth j).2 computer.tm.k₁).map computer.outputAlphabet = _ + rw [ho, decode_trInit] + simp [g, List.map_reverse] + · intro w hw + have hl := hL _ _ _ (h w.reverse ((accepts w).mp hw)) + simpa [g] using hl + have hr := mem_FP_comp (mem_FP_comp reverse_mem_FP hg) reverse_mem_FP + have heq : + (List.reverse ∘ (fun w => if w ∈ D'.accepts then g w else []) ∘ List.reverse) = + (fun w => if w ∈ D.accepts then f w else []) := by + funext w + by_cases hw : w ∈ D.accepts <;> simp [Function.comp_def, accepts, hw, g] + rw [heq] at hr + exact hr + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/Internal/Compiler.lean b/Complexitylib/Interop/Mathlib/TM2/Internal/Compiler.lean new file mode 100644 index 000000000..664a175fd --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/Internal/Compiler.lean @@ -0,0 +1,199 @@ +/- +Copyright (c) 2026 OpenAI, Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: OpenAI, Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.Internal.Simulation +import Mathlib.Algebra.Order.BigOperators.Group.Finset +import Mathlib.Tactic.Linarith +import Mathlib.Tactic.Positivity + +/-! +# Finite-state compilation of arbitrary supported TM2 programs + +This packages the existing Mathlib compiler and its timing lemmas for any +finitely supported stack program with finite auxiliary state. The target's +alphabet is exactly Mathlib's stack-track alphabet; no external recoding is +hidden in the input or output contract. The timing lemmas remain in the +rational hitting-list development; `compile_source` generalizes the assembly +used in its `FiniteCompiler` module. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +open Turing +open scoped BigOperators + +variable {K : Type} {Γ : K → Type} {Λ σ : Type} +variable [DecidableEq K] [Inhabited Λ] [Inhabited σ] [Fintype σ] + +lemma compile_source (M : Λ → TM2.Stmt Γ Λ σ) (F : Finset Λ) (hF : TM2.Supports M F) : + ∃ (m : ℕ) (G : TM0.Machine (TM2to1.Γ' K Γ) (Fin (m + 1))) (C : ℕ), + 0 < C ∧ ∀ (k : K) (w : List (Γ k)) (b : TM2.Cfg Γ Λ σ) (t : ℕ), + exec (TM2.step M) t (TM2.init k w) = some b → TM2.step M b = none → + ∃ u ≤ C * (w.length + t + 1) ^ 2, ∃ a, + exec (TM0.step G) u (TM0.init (TM2to1.trInit k w)) = some a ∧ + TM0.step G a = none ∧ + ∃ c : TM1.Cfg (TM2to1.Γ' K Γ) (TM2to1.Λ' K Γ Λ σ) σ, + TM2to1.TrCfg b c ∧ a.Tape = c.Tape := by + classical + let A := 1 + ∑ l ∈ F, (StackToOne.stackWeight (M l) + 1) + have hA : 1 ≤ A := by simp [A] + have hAC : ∀ l ∈ F, StackToOne.stackWeight (M l) + 1 ≤ A := by + intro l hl + exact (Finset.single_le_sum (f := fun l => StackToOne.stackWeight (M l) + 1) + (fun _ _ => Nat.zero_le _) hl).trans (Nat.le_add_left _ _) + let M₁ := TM2to1.tr M + let F₁ := TM2to1.trSupp M F + have hF₁ : TM1.Supports M₁ F₁ := TM2to1.tr_supports M hF + let B := 1 + ∑ l ∈ F₁, OneToZero.weight (M₁ l) + have hB : 1 ≤ B := by simp [B] + have hBC : ∀ l ∈ F₁, OneToZero.weight (M₁ l) ≤ B := by + intro l hl + exact (Finset.single_le_sum (f := fun l => OneToZero.weight (M₁ l)) + (fun _ _ => Nat.zero_le _) hl).trans (Nat.le_add_left _ _) + let F₀ := TM1to0.trStmts M₁ F₁ + have hF₀ : TM0.Supports (TM1to0.tr M₁) (F₀ : Set (TM1to0.Λ' M₁)) := + TM1to0.tr_supports M₁ hF₁ + obtain ⟨m, G, hG⟩ := FiniteStates.finite_machine (TM1to0.tr M₁) F₀ hF₀ + refine ⟨m, G, 10 * B * (A + 1) ^ 2, by positivity, ?_⟩ + intro k w b t hr hh + have hlen : ∀ j, ((TM2.init k w : TM2.Cfg Γ Λ σ).stk j).length ≤ w.length := by + intro j + by_cases hj : j = k + · subst j; simp [TM2.init] + · simp [TM2.init, Function.update_of_ne hj] + obtain ⟨u, c, hu, hc, he⟩ := StackToOne.execution_time M F hF A hAC t _ b + (TM1.init (TM2to1.trInit k w)) w.length + (Finset.some_mem_insertNone.mpr hF.1) hlen (TM2to1.trCfg_init k w) hr + obtain ⟨v, hv, he'⟩ := OneToZero.execution_time M₁ F₁ hF₁ B hBC u _ c + (Finset.some_mem_insertNone.mpr hF₁.1) he + have hinit : TM1to0.trCfg M₁ (TM1.init (TM2to1.trInit k w)) = + TM0.init (TM2to1.trInit k w) := rfl + rw [hinit] at he' + obtain ⟨a, ha, hhalt, htape⟩ := hG (TM2to1.trInit k w) v _ he' + (OneToZero.halts M₁ c (StackToOne.halts M b c hc hh)) + refine ⟨v, ?_, a, ha, hhalt, c, hc, htape⟩ + have ht : t ≤ w.length + t + 1 := by omega + have hs : w.length + A * t ≤ (A + 1) * (w.length + t + 1) := by + nlinarith only [Nat.zero_le (A * w.length)] + have hscan : 2 * (w.length + A * t) + 4 ≤ + 4 * (A + 1) * (w.length + t + 1) := by + nlinarith only [hs, hA, Nat.zero_le w.length, Nat.zero_le t] + calc v ≤ B * u := hv + _ ≤ B * (A * t * (2 * (w.length + A * t) + 4)) := Nat.mul_le_mul_left _ hu + _ ≤ B * (A * (w.length + t + 1) * (4 * (A + 1) * (w.length + t + 1))) := + Nat.mul_le_mul_left _ (Nat.mul_le_mul (Nat.mul_le_mul_left _ ht) hscan) + _ ≤ 10 * B * (A + 1) ^ 2 * (w.length + t + 1) ^ 2 := by + nlinarith [Nat.zero_le (B * (w.length + t + 1) ^ 2)] + +omit [DecidableEq K] [Inhabited Λ] [Inhabited σ] [Fintype σ] in +lemma tape_unique {a b : TM2.Cfg Γ Λ σ} + {c d : TM1.Cfg (TM2to1.Γ' K Γ) (TM2to1.Λ' K Γ Λ σ) σ} + (h : TM2to1.TrCfg a c) (h' : TM2to1.TrCfg b d) (hs : a.stk = b.stk) : + c.Tape = d.Tape := by + cases h with + | mk L hL => + cases h' with + | mk L' hL' => + have he : L = L' := by + apply ListBlank.ext + intro i + funext k + have hh := (hL k).trans + ((congrArg (fun S => ListBlank.mk ((S k).map some).reverse) hs).trans (hL' k).symm) + have hh' := congrArg (fun T => T.nth i) hh + simpa only [ListBlank.nth_map, proj, PointedMap.mk_val] using hh' + rw [he] + +lemma exec_eq_iterate {α : Type*} (step : α → Option α) (t : ℕ) (a : α) : + exec step t a = (fun s => s.bind step)^[t] (some a) := by + suffices h : ∀ s : Option α, s.bind (exec step t) = + (fun s : Option α => s.bind step)^[t] s by + exact h (some a) + intro s + induction t generalizing s with + | zero => cases s <;> rfl + | succ t ih => + rw [Function.iterate_succ_apply] + cases s with + | none => exact ih none + | some a => exact ih (step a) + +lemma output_length_bound (tm : FinTM2) : + ∃ C : ℕ, 0 < C ∧ ∀ (input : List (tm.Γ tm.k₀)) (output : List (tm.Γ tm.k₁)) (time : ℕ), + TM2OutputsInTime tm input (some output) time → + output.length ≤ input.length + C * time := by + classical + let : Inhabited tm.Λ := ⟨tm.main⟩ + let F : Finset tm.Λ := @Finset.univ tm.Λ tm.ΛFin + have supported : ∀ q : tm.Stmt, TM2.SupportsStmt F q := by + intro q + induction q <;> simp_all [TM2.SupportsStmt, F] + have hF : TM2.Supports tm.m F := ⟨by simp [F], fun l _ => supported (tm.m l)⟩ + let C := 1 + ∑ l ∈ F, (StackToOne.stackWeight (tm.m l) + 1) + have hC : ∀ l ∈ F, StackToOne.stackWeight (tm.m l) + 1 ≤ C := by + intro l hl + exact (Finset.single_le_sum (f := fun l => StackToOne.stackWeight (tm.m l) + 1) + (fun _ _ => Nat.zero_le _) hl).trans (Nat.le_add_left _ _) + refine ⟨C, by simp [C], ?_⟩ + intro input output time h + change StateTransition.EvalsToInTime tm.step (initList tm input) + (some (haltList tm output)) time at h + have hr := (exec_eq_iterate tm.step h.steps (initList tm input)).trans h.evals_in_steps + have hlen : ∀ k, ((initList tm input).stk k).length ≤ input.length := by + intro k + by_cases hk : k = tm.k₀ + · subst k; simp [initList] + · simp [initList, hk] + have hb := StackToOne.execution_length tm.m F hF C hC h.steps + (initList tm input) (haltList tm output) input.length + (by change some tm.main ∈ Finset.insertNone F; simp [F]) hlen hr tm.k₁ + have hb' : output.length ≤ input.length + C * h.steps := by simpa [haltList] using hb + exact hb'.trans (Nat.add_le_add_left (Nat.mul_le_mul_left C h.steps_le_m) _) + +lemma compile_bundled (tm : FinTM2) : + ∃ (m : ℕ) (G : TM0.Machine (TM2to1.Γ' tm.K tm.Γ) (Fin (m + 1))) (C : ℕ), + 0 < C ∧ ∀ (input : List (tm.Γ tm.k₀)) (output : List (tm.Γ tm.k₁)) (time : ℕ), + TM2OutputsInTime tm input (some output) time → + ∃ u ≤ C * (input.length + time + 1) ^ 2, ∃ a, + exec (TM0.step G) u (TM0.init (TM2to1.trInit tm.k₀ input)) = some a ∧ + TM0.step G a = none ∧ a.Tape = Tape.mk₁ (TM2to1.trInit tm.k₁ output) := by + classical + let : Inhabited tm.Λ := ⟨tm.main⟩ + let : Inhabited tm.σ := ⟨tm.initialState⟩ + let : Fintype tm.σ := tm.σFin + let F : Finset tm.Λ := @Finset.univ tm.Λ tm.ΛFin + have supported : ∀ q : tm.Stmt, TM2.SupportsStmt F q := by + intro q + induction q <;> simp_all [TM2.SupportsStmt, F] + have hF : TM2.Supports tm.m F := ⟨by simp [F], fun l _ => supported (tm.m l)⟩ + obtain ⟨m, G, C, hC, hG⟩ := compile_source tm.m F hF + refine ⟨m, G, C, hC, ?_⟩ + intro input output time h + change StateTransition.EvalsToInTime tm.step (initList tm input) + (some (haltList tm output)) time at h + have hi : initList tm input = (TM2.init tm.k₀ input : tm.Cfg) := by + change TM2.Cfg.mk _ _ _ = TM2.Cfg.mk _ _ _ + congr 1 + funext k + by_cases hk : k = tm.k₀ + · subst k; simp + · simp [hk] + have run := (exec_eq_iterate tm.step h.steps (initList tm input)).trans h.evals_in_steps + have hr := (congrArg (fun c : tm.Cfg => exec tm.step h.steps c) hi).symm.trans run + obtain ⟨u, hu, a, ha, hh, c, hc, ht⟩ := hG tm.k₀ input (haltList tm output) + h.steps hr rfl + refine ⟨u, hu.trans (Nat.mul_le_mul_left C + (Nat.pow_le_pow_left (by have := h.steps_le_m; omega) 2)), a, ha, hh, ht.trans ?_⟩ + apply tape_unique hc (TM2to1.trCfg_init tm.k₁ output) + funext k + by_cases hk : k = tm.k₁ + · subst k; simp [haltList, TM2.init] + · simp [haltList, TM2.init, hk] + +end Complexity.MathlibTM2 diff --git a/Complexitylib/Interop/Mathlib/TM2/Internal/Simulation.lean b/Complexitylib/Interop/Mathlib/TM2/Internal/Simulation.lean new file mode 100644 index 000000000..a9d175cfd --- /dev/null +++ b/Complexitylib/Interop/Mathlib/TM2/Internal/Simulation.lean @@ -0,0 +1,74 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.Defs +public import Complexitylib.RationalHitting.Internal.StackSimulation + +/-! +# Stack-growth bounds using the existing simulation tools + +The rational hitting-list development already proves timing bounds for +Mathlib's TM2-to-TM1 and TM1-to-TM0 compilers, together with finite-state +restriction. Those proofs remain in their original modules and namespaces. +This module reuses them and adds a bound on the length of each stack. +-/ + +@[expose] public section + +namespace Complexity.MathlibTM2 + +open Turing + +export RationalHitting.TMTime (exec_zero exec_add exec_trans exec_positive_congr) + +namespace OneToZero +export RationalHitting.TMTime.OneToZero (weight weight_pos statement_time execution_time halts) +end OneToZero + +namespace StackToOne +export RationalHitting.TMTime.StackToOne (go_time return_time stackWeight stackWeight_run + stWrite_length updated_length continuation_time statement_length step_time execution_time halts) + +variable {K : Type*} {Γ : K → Type*} {Λ σ : Type*} +variable [DecidableEq K] (M : Λ → TM2.Stmt Γ Λ σ) + +/-- Each stack grows at most linearly in the number of source transitions. -/ +theorem execution_length [Inhabited Λ] (F : Finset Λ) (hF : TM2.Supports M F) + (C : ℕ) (hC : ∀ l ∈ F, stackWeight (M l) + 1 ≤ C) + (t : ℕ) (a b : TM2.Cfg Γ Λ σ) (L : ℕ) + (ha : a.l ∈ Finset.insertNone F) (hS : ∀ k, (a.stk k).length ≤ L) + (hr : exec (TM2.step M) t a = some b) : + ∀ k, (b.stk k).length ≤ L + C * t := by + induction t generalizing L a with + | zero => + have he : a = b := Option.some.inj hr + subst b + simpa using hS + | succ t ih => + obtain ⟨l, v, S⟩ := a + cases l with + | none => simp [exec, TM2.step] at hr + | some l => + have hl := Finset.some_mem_insertNone.mp ha + have hnext : (TM2.stepAux (M l) v S).l ∈ Finset.insertNone F := + TM2.step_supports M hF (by rfl) ha + have hlen : ∀ k, ((TM2.stepAux (M l) v S).stk k).length ≤ L + C := by + intro k + exact (statement_length (M l) v S L hS k).trans (by have := hC l hl; omega) + change exec (TM2.step M) t (TM2.stepAux (M l) v S) = some b at hr + intro k + have hk := ih _ (L + C) hnext hlen hr k + simpa [Nat.mul_add, Nat.add_assoc, Nat.add_left_comm, Nat.add_comm] using hk + +end StackToOne + +namespace FiniteStates +export RationalHitting.TMTime.FiniteStates (stateEncode stateDecode stateEncode_default + stateDecode_encode machine cfg cfg_init step_cfg execution run_support finite_machine) +end FiniteStates + +end Complexity.MathlibTM2 diff --git a/ROADMAP.md b/ROADMAP.md index 8d0f8228a..cab3834a6 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -763,7 +763,11 @@ The machine infrastructure is reusable: the complete binary outputs, the [regular-domain guard](Complexitylib/Interop/Mathlib/TM0/Guard.lean) handles malformed inputs, and the CSLib bridge now transfers full string computations -in both directions at the level of polynomial-time classes. +in both directions at the level of polynomial-time classes. The +[Mathlib `TM2` bridge](Complexitylib/Interop/Mathlib/TM2/FP.lean) adds quadratic +stack-to-tape compilation and canonical `FP` transfer for finite working +alphabets, with independent input/output alphabets, complete outputs, and +regular-domain guards. ### Next: choiceless polynomial time with counting @@ -786,14 +790,31 @@ Complete the following layers before asserting the standard-model separation: - [ ] **OAI-CPT-STRUCTURES:** Connect the eight binary relations to `FinStruct` and an invariant `DecisionProblem`. Account for the library's `card ≥ 2` convention, prove encoding correspondence, and handle malformed strings. -- [ ] **OAI-TM2-BRIDGE:** Transfer finite Mathlib `TM2` stack computations to +- [x] **OAI-TM2-BRIDGE:** Transfer finite Mathlib `TM2` stack computations to the canonical machine model with a proved polynomial overhead and complete input/output contracts. Reuse the existing CSLib bridges where applicable. - The `TM0` bridge and regular-domain guard alone do not cover this source. + The [efficient Mathlib compiler](Complexitylib/Interop/Mathlib/TM2.lean) now + transfers supported TM2 programs with finite auxiliary state and bundled + polynomial-time certificates to finite-state TM0 machines with quadratic + overhead and exact stack-track input/output tapes. The + [canonical FP interface](Complexitylib/Interop/Mathlib/TM2/FP.lean) supplies + marked input copying, output decoding, and both string reversals. It covers + independent finite input/output alphabets through explicit palindromic binary + symbol codes, with malformed codes rejected before source execution. The + original binary interface remains available without recoding. Both interfaces + support regular-domain source contracts and derive output-size bounds from + source time. Working-alphabet finiteness is explicit, since Mathlib's bundle + only supplies input-alphabet finiteness. This completes the reusable machine + bridge. Applying it to the choiceless source and its structure encoding remains + part of `OAI-CPT-STRUCTURES` and `OAI-CPT-SEPARATION`. - [ ] **OAI-CPT-SEPARATION:** Prove membership of the full encoded query language in canonical `P`, combine it with the audited non-definability theorem, and add blueprint nodes and headline axiom checks. Do not identify a source machine witness with canonical `P` before these bridges are proved. + Instantiate the TM2 bridge on the source witness, translate the input/output + encodings, and prove polynomial-time handling of arbitrary binary inputs. + Canonical structure-encoding validation already belongs to `P`; a DFA guard + alone does not validate the source's structure format. ### Following layers diff --git a/blueprint/src/chapters/polytime.tex b/blueprint/src/chapters/polytime.tex index 61b6218df..dc88bd9d6 100644 --- a/blueprint/src/chapters/polytime.tex +++ b/blueprint/src/chapters/polytime.tex @@ -348,6 +348,140 @@ \section{Time classes} finite-machine simulation and its complete-output polynomial bound. \end{proof} +\begin{theorem}[Mathlib's stack compiler has quadratic overhead] + \label{thm:mathlib-tm2-efficient-compiler} + \lean{Complexity.MathlibTM2.exists_tm0_simulator, + Complexity.MathlibTM2.exists_tm0_computer, + Complexity.MathlibTM2.exists_tm0_polynomial_computer} + \leanok + A fixed finitely supported Mathlib TM2 program with finite auxiliary state + has one finite-state TM0 simulator. A terminating source run on a stack word + $w$ in $t$ steps takes at most $C(|w|+t+1)^2$ target transitions, for a + program-dependent constant $C$. The simulator preserves the complete + stack-track tape. For bundled computation certificates, both input and output + tapes are exactly Mathlib's bottom-first stack encoding with its bottom marker, + including for empty words. The track alphabet is finite when the stack index + set and each stack alphabet are finite. Polynomial-time source certificates + therefore give polynomial-time TM0 certificates in these explicit encodings; + the next theorem accounts for canonical binary-string conversion. +\end{theorem} +\begin{proof} + \leanok + Use Mathlib's existing TM2-to-TM1-to-TM0 compiler. A fixed source statement + accesses boundedly many stack cells, and stack lengths grow at most linearly + with the source step count. The scans in the TM2-to-TM1 simulation therefore + have quadratic total cost. TM1-to-TM0 compilation adds a fixed linear factor, + and restricting to the finite supported control states adds no overhead. + The stack-configuration relation determines the whole final tape, so a clean + output stack gives the exact output encoding, not only a projection of it. +\end{proof} + +\begin{definition}[Binary codes for finite alphabets] + \label{def:finite-alphabet-code} + \lean{Complexity.FiniteAlphabetCode, + Complexity.FiniteAlphabetCode.symbol, + Complexity.FiniteAlphabetCode.encode, + Complexity.FiniteAlphabetCode.decode, + Complexity.FiniteAlphabetCode.map} + \leanok + Choose a numbering of an alphabet $A$ by $\{0,\ldots,a-1\}$. + Letter $i$ has binary code $0\,1^i\,0$, and a word is encoded by + concatenating its letter codes. The decoder rejects malformed delimiters + and out-of-range indices. For independent input and output codes, the coded + version of a word function returns the encoded result on valid inputs and + the empty word on invalid codes. An empty alphabet has only the empty word. +\end{definition} + +\begin{lemma}[Exact finite-alphabet coding] + \label{lem:finite-alphabet-code} + \lean{Complexity.FiniteAlphabetCode.decode_encode, + Complexity.FiniteAlphabetCode.decode_eq_some_iff, + Complexity.FiniteAlphabetCode.encode_injective, + Complexity.FiniteAlphabetCode.length_le_encode, + Complexity.FiniteAlphabetCode.length_encode_le, + Complexity.FiniteAlphabetCode.encode_reverse_eq, + Complexity.FiniteAlphabetCode.decode_reverse_eq} + \leanok + Decoding recognizes exactly the chosen encodings. Encoding is injective, + and a word of length $n$ has encoded length between $n$ and $(a+1)n$. + Encoding commutes with reversal. Reversing any binary string preserves + whether it is a valid encoding and reverses its decoded word when valid. +\end{lemma} +\begin{proof} + \leanok + Induct on the input suffix and the parser's pending unary index. Each letter + has a nonempty code of length at most $a+1$. The codewords are palindromes, + so reversal reverses their concatenation without changing individual codes. +\end{proof} + +\begin{theorem}[Mathlib TM2 computations transfer to canonical FP] + \label{thm:mathlib-tm2-canonical-fp} + \lean{Complexity.MathlibTM2.exists_output_length_bound, + Complexity.MathlibTM2.mem_FP, + Complexity.MathlibTM2.mem_FP_on_dfa, + Complexity.MathlibTM2.mem_FP_encoded, + Complexity.MathlibTM2.mem_FP_encoded_on_dfa} + \leanok + A polynomial-time Mathlib TM2 certificate for a total binary-string function + gives membership in canonical $\cc{FP}$ when every working stack alphabet is + finite. An output stack has length at most $n+Ct$ on input length $n$ after + $t$ source transitions, so no separate output-size hypothesis is needed. + More generally, a polynomial source bound on a DFA-recognized input domain + gives a total $\cc{FP}$ function equal to the source result on that domain and + the empty word elsewhere. Invalid inputs need no source halting bound. + Independent finite input and output alphabets use the explicit binary codes + of Definition~\ref{def:finite-alphabet-code}, with invalid symbol codes + rejected before source execution. Empty alphabets are included. The direct + binary-string interface does not require this recoding. +\end{theorem} +\begin{proof} + \leanok + Compile the source using Theorem~\ref{thm:mathlib-tm2-efficient-compiler}. + A marked-input copier constructs the stack-track representation in linear + time, including its empty-input marker. A separate decoder emits the complete + output. These operations leave both words in stack-bottom order; polynomial-time + reversal before and after the simulation restores their original order. + Stack-growth bounds give a polynomial output bound. For a regular domain, + guard the simulation with the reversed DFA before performing either source + execution or output emission; rejected inputs halt with empty output. + For finite alphabets, parse complete symbol codes before writing source cells + and emit complete symbol codes afterward. Lemma~\ref{lem:finite-alphabet-code} + bounds both costs linearly and lets binary reversal restore the source order. +\end{proof} + +\begin{theorem}[Marked tape formats and regular-domain validation] + \label{thm:marked-tape-fp} + \lean{Complexity.MathlibTM0.MarkedMachine.computes_cslib, + Complexity.MathlibTM0.MarkedMachine.mem_FP, + Complexity.MathlibTM0.MarkedMachine.mem_FP_on_dfa, + Complexity.MathlibTM0.AlphabetMachine.computes_cslib, + Complexity.MathlibTM0.AlphabetMachine.mem_FP, + Complexity.MathlibTM0.AlphabetMachine.mem_FP_on_dfa, + Complexity.CslibGuard.computes_accepted, + Complexity.CslibGuard.computes_rejected, + Complexity.CslibGuard.mem_FP_on_dfa} + \leanok + A finite TM0 with a first-cell input marker, a marker for empty input, and + a separate output decoder is simulated by a binary CSLib machine in at most + $2n+t+m+4$ transitions, where $n,t,m$ are input length, source time, and output + length. A DFA guard adds $2n+2$ transitions on accepted inputs and rejects + other inputs with empty output in $n+1$ transitions. Polynomial source and + output bounds on the regular domain therefore imply canonical $\cc{FP}$ + membership for the function extended by the empty word. + For finite input and output alphabets, the encoded simulator takes at most + $2N+t+(b+1)m+4$ transitions, where $N$ is binary input length, $m$ is the + number of output letters, and $b$ is the output alphabet size. Invalid symbol + codes halt with empty output during the initial scan. +\end{theorem} +\begin{proof} + \leanok + Copy the marked input to symbol tracks and rewind. Reuse the existing TM0 + source-step simulation and decoded output emission. The DFA guard scans the + read-only input without changing work tapes or output; acceptance rewinds to + the original initial configuration, while rejection halts before the source + starts. Apply the CSLib-to-canonical machine simulation. +\end{proof} + \section{Closure properties of P and FP} The projections of the pairing codec, the pairing of two $\cc{FP}$ functions, diff --git a/docs/OpenAIMath.md b/docs/OpenAIMath.md index 3e69890c5..898180c64 100644 --- a/docs/OpenAIMath.md +++ b/docs/OpenAIMath.md @@ -221,6 +221,25 @@ without changing the input, rejects with empty output, and rewinds accepted words before executing the source. The extra source time is `2 * input.length + 2`. The hitting generator instantiates this with a checked five-state unary validator. +The reusable [Mathlib TM2 compiler](../Complexitylib/Interop/Mathlib/TM2.lean) +imports the stack-compiler timing arguments from the rational hitting-list +development, leaving those proofs in their original modules and namespaces. +For a fixed supported source program with finite auxiliary state, a run on a word of length `n` in +`t` source steps takes at most +`C * (n + t + 1)^2` TM0 transitions and preserves the complete stack-track tape. +The [TM2-to-FP interface](../Complexitylib/Interop/Mathlib/TM2/FP.lean) adds marked +input copying, output decoding, and string reversal. It transfers total +word-function certificates with finite working alphabets to canonical `FP` +without a separate output-length assumption. Independent input and output +alphabets use explicit delimited, palindromic binary symbol codes; invalid codes +halt with empty output before the source starts. Empty alphabets are supported, +and the original binary-string interface remains available without recoding. +The DFA-guarded variants require source bounds only on accepted encoded inputs +and return empty output elsewhere. +This reusable layer does not complete the deferred choiceless source's structure +encoding and arbitrary-input contracts; those remain tracked in +[the triage notes](OpenAIMathTriage.md#deferred-choiceless-polynomial-time-with-counting). + New proofs in the [evaluation interface](../Complexitylib/RationalHitting/Evaluation.lean) establish several consequences: diff --git a/docs/OpenAIMathTriage.md b/docs/OpenAIMathTriage.md index e2f712320..8e7324b6c 100644 --- a/docs/OpenAIMathTriage.md +++ b/docs/OpenAIMathTriage.md @@ -23,7 +23,7 @@ Shared dependencies mean the counts cannot be added to estimate total work. | [Two-way complementation](https://github.com/openai/math/blob/adc7f1241b42e322a6451854ab7e4b4c146bf78a/lean/OAI/Combinatorics/TwoWayAutomata/Main.lean) | Core: 19 modules; 4,088 lines. Extended explicit-family package: 41 modules; 8,477 lines. | Core integrated with a state-preserving `NMachine` bridge. New arbitrary-word-encoding and binary-NFA complementation bounds reuse the existing scanner, and rule out a polynomial binary complementation bound. The extended package's separate deterministic proof was not duplicated. | | [Rational hitting lists](https://github.com/openai/math/blob/adc7f1241b42e322a6451854ab7e4b4c146bf78a/lean/OAI/Computability/RationalHitting/Main.lean) | 53 modules; 15,304 lines | Integrated: canonical `FP` generator with exact unary input, complete rational binary output, and explicit malformed-input handling; executable matrix evaluation, finite nonzeroness and inverse-domain tests. Evaluation bit complexity remains separate. | | [Exact Fourier circuits](https://github.com/openai/math/blob/adc7f1241b42e322a6451854ab7e4b4c146bf78a/lean/OAI/Computability/FourierCircuit/Main.lean) | 51 modules; 19,103 lines | Integrated: complete source proof, CSLib translation, cofinal upper bound, and synthesis with the existing linear lower bound. Documentation, blueprint links, and all repository gates pass. The result concerns unrestricted scalar operations and arbitrarily large sizes. | -| [Choiceless polynomial time](https://github.com/openai/math/blob/adc7f1241b42e322a6451854ab7e4b4c146bf78a/lean/OAI/ModelTheory/Choiceless/Separation.lean) | 26 modules; 18,461 lines | Deferred; incomplete port excluded from this batch. Finish the source port, semantic comparison, structure encodings, and finite `TM2` bridge before stating a canonical separation. See roadmap markers `OAI-CPT-*` and `OAI-TM2-BRIDGE`. | +| [Choiceless polynomial time](https://github.com/openai/math/blob/adc7f1241b42e322a6451854ab7e4b4c146bf78a/lean/OAI/ModelTheory/Choiceless/Separation.lean) | 26 modules; 18,461 lines | Deferred; incomplete port excluded from this batch. The reusable finite-alphabet `TM2` bridge is implemented. Finish the source port, semantic comparison, structure encodings, and source-specific bridge application before stating a canonical separation. See roadmap markers `OAI-CPT-*` and `OAI-TM2-BRIDGE`. | ## Larger algorithms and hardness packages @@ -103,13 +103,24 @@ Inspection of the source definitions identifies three distinct obligations: with finite stack alphabets and a polynomial bound measured in the encoded input length. This is a different source model from the hitting generator's `TM0`. Its input has a unary size header and a relation table; its runtime - theorem is stated on encoded structures. A canonical `P` integration needs - a stack-machine simulation and explicit handling of arbitrary binary inputs. - The existing regular-domain guard does not by itself supply that parser. - -These are source-level findings, not a completed port or a checked bridge to -complexitylib's descriptive-complexity and machine interfaces. The separate -witnessed-choice theorem is outside this initial dependency closure. + theorem is stated on encoded structures. The reusable + [stack-machine compiler](../Complexitylib/Interop/Mathlib/TM2.lean) now proves + quadratic transition overhead with exact stack-track input/output tapes. Its + [canonical `FP` interface](../Complexitylib/Interop/Mathlib/TM2/FP.lean) handles + independent finite input and output alphabets through explicit binary symbol + codes, as well as the original binary interface and DFA-recognized domains. + Working-alphabet finiteness is explicit and output-length bounds are automatic. A canonical + `P` integration still needs the source witness instantiated through this + compiler, encoding correspondence, and polynomial-time handling of arbitrary + binary inputs. The regular-domain guard does not by itself supply the + structure parser or prove that its valid encodings form a regular language. + The existing [canonical structure validator](../Complexitylib/DescriptiveComplexity/Encoding/Validity.lean) + is polynomial-time and can be reused after proving encoding correspondence. + +These source-level findings do not establish a completed choiceless port or a +checked connection from its definitions to complexitylib's descriptive-complexity +and canonical machine interfaces. The separate witnessed-choice theorem is +outside this initial dependency closure. For the model comparison, Dawar, Richerby, and Rossman, [*Choiceless Polynomial Time, Counting and the Cai–Fürer–Immerman Graphs*, diff --git a/lakefile.toml b/lakefile.toml index bb768db93..f71bbb02e 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -27,4 +27,6 @@ roots = [ "StackEncodingCheck", "PairBuildApiCheck", "PairBuildCompatibilityCheck", + "TM2ApiCheck", + "TM2FPApiCheck", ] diff --git a/scripts/AxiomGuard.lean b/scripts/AxiomGuard.lean index 47467a468..d01174114 100644 --- a/scripts/AxiomGuard.lean +++ b/scripts/AxiomGuard.lean @@ -128,6 +128,29 @@ def headlineTheorems : List Name := [ `Complexity.MathlibTM0.BinaryMachine.computes_cslib, `Complexity.MathlibTM0.BinaryMachine.mem_FP, `Complexity.MathlibTM0.BinaryMachine.mem_FP_on_dfa, + `Complexity.MathlibTM0.MarkedMachine.computes_cslib, + `Complexity.MathlibTM0.MarkedMachine.mem_FP, + `Complexity.MathlibTM0.MarkedMachine.mem_FP_on_dfa, + `Complexity.FiniteAlphabetCode.decode_eq_some_iff, + `Complexity.FiniteAlphabetCode.encode_injective, + `Complexity.FiniteAlphabetCode.length_encode_le, + `Complexity.FiniteAlphabetCode.encode_reverse_eq, + `Complexity.FiniteAlphabetCode.decode_reverse_eq, + `Complexity.MathlibTM0.AlphabetMachine.computes_cslib, + `Complexity.MathlibTM0.AlphabetMachine.mem_FP, + `Complexity.MathlibTM0.AlphabetMachine.mem_FP_on_dfa, + `Complexity.CslibGuard.computes_accepted, + `Complexity.CslibGuard.computes_rejected, + `Complexity.CslibGuard.mem_FP_on_dfa, + -- Efficient use of Mathlib's existing stack-to-tape compiler + `Complexity.MathlibTM2.exists_tm0_simulator, + `Complexity.MathlibTM2.exists_tm0_computer, + `Complexity.MathlibTM2.exists_tm0_polynomial_computer, + `Complexity.MathlibTM2.exists_output_length_bound, + `Complexity.MathlibTM2.mem_FP, + `Complexity.MathlibTM2.mem_FP_on_dfa, + `Complexity.MathlibTM2.mem_FP_encoded, + `Complexity.MathlibTM2.mem_FP_encoded_on_dfa, -- Cobham's characterization of polynomial-time functions `Complexity.Cobham.cobham_iff_FPn, `Complexity.CobhamFP_eq_FP, diff --git a/scripts/TM2ApiCheck.lean b/scripts/TM2ApiCheck.lean new file mode 100644 index 000000000..a46334dfc --- /dev/null +++ b/scripts/TM2ApiCheck.lean @@ -0,0 +1,53 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2 + +/-! +# Public stack-compiler API regression checks + +The public import alone supplies the Mathlib-certificate bridge. The identity +example uses one fixed compiled machine for all words, including the empty word. +Concrete tape examples make the bottom marker and reversed stack order visible. +-/ + +namespace Complexity.TM2ApiCheck + +open Turing MathlibTM2 + +example : TM2to1.trInit (Γ := fun _ : Unit => Bool) () [] = [(true, fun _ => none)] := rfl + +example : TM2to1.trInit (Γ := fun _ : Unit => Bool) () [true, false] = + [(true, fun _ => some false), (false, fun _ => some true)] := by + change [(true, Function.update (fun _ : Unit => none) () (some false)), + (false, Function.update (fun _ : Unit => none) () (some true))] = _ + congr 2 + +example : + ∃ (m : ℕ) + (G : TM0.Machine (TM2to1.Γ' (idComputer Bool).K (idComputer Bool).Γ) (Fin (m + 1))) + (C : ℕ), 0 < C ∧ ∀ w : List Bool, + ∃ t ≤ C * (w.length + 2) ^ 2, ∃ c, + exec (TM0.step G) t (TM0.init (TM2to1.trInit (idComputer Bool).k₀ w)) = some c ∧ + TM0.step G c = none ∧ + c.Tape = Tape.mk₁ (TM2to1.trInit (idComputer Bool).k₁ w) := by + obtain ⟨m, G, C, hC, hG⟩ := exists_tm0_computer (idComputer Bool) + refine ⟨m, G, C, hC, ?_⟩ + intro w + have h : TM2OutputsInTime (idComputer Bool) w (some w) 1 := + ⟨⟨1, rfl⟩, le_rfl⟩ + obtain ⟨u, hu, c, hc, hh, ho⟩ := hG w w 1 h + dsimp only [idComputer] at hu + have htime : w.length + 1 + 1 = w.length + 2 := by omega + rw [htime] at hu + exact ⟨u, hu, c, hc, hh, ho⟩ + +-- The higher-level bridge also accepts a typed problem with a binary encoding. +example := exists_tm0_polynomial_computer + (idComputableInPolyTime (α := ℕ) Computability.encodeNat) + +end Complexity.TM2ApiCheck diff --git a/scripts/TM2FPApiCheck.lean b/scripts/TM2FPApiCheck.lean new file mode 100644 index 000000000..1726a60d6 --- /dev/null +++ b/scripts/TM2FPApiCheck.lean @@ -0,0 +1,261 @@ +/- +Copyright (c) 2026 Erik Demaine. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Erik Demaine +-/ + +module +public import Complexitylib.Interop.Mathlib.TM2.FP + +/-! +# Canonical TM2-to-FP API checks + +Only the public surface is imported. A one-step stack program prepends a bit; +its certificate covers every word, including empty input, and needs no separate +output-length certificate. Prepending also distinguishes the two string orders. +-/ + +namespace Complexity.TM2FPApiCheck + +open Turing + +/-- The one-stack identity machine with one push before halting. -/ +def prependMachine : FinTM2 := + { idComputer Bool with m := fun _ => .push () (fun _ => false) .halt } + +/-- A source certificate for prepending a bit on every binary string. -/ +noncomputable def prependComputer : + TM2ComputableInPolyTime id id (fun w : List Bool => false :: w) where + tm := prependMachine + inputAlphabet := Equiv.refl Bool + outputAlphabet := Equiv.refl Bool + time := 1 + outputsFun _ := ⟨⟨1, rfl⟩, by simp⟩ + +example : (fun w : List Bool => false :: w) ∈ FP := by + let : ∀ k, Fintype (prependComputer.tm.Γ k) := fun _ => by + change Fintype Bool + infer_instance + exact MathlibTM2.mem_FP prependComputer + +example : (fun w : List Bool => w) ∈ FP := by + let computer := idComputableInPolyTime (fun w : List Bool => w) + let : ∀ k, Fintype (computer.tm.Γ k) := fun _ => by + change Fintype Bool + infer_instance + exact MathlibTM2.mem_FP computer + +/-- A regular domain that is not invariant under reversal. -/ +def startsTrue : DFA Bool (Option Bool) where + step q b := match q with + | none => some b + | some x => some x + start := none + accept := {some true} + +lemma startsTrue_accepts (w : List Bool) : + w ∈ startsTrue.accepts ↔ w.head? = some true := by + have stable (b : Bool) (bs : List Bool) : startsTrue.evalFrom (some b) bs = some b := by + induction bs <;> simp_all [DFA.evalFrom_cons, DFA.evalFrom_nil, startsTrue] + cases w with + | nil => rfl + | cons b bs => + change startsTrue.evalFrom (some b) bs = some true ↔ some b = some true + rw [stable] + +/-- On true-headed inputs prepend false; otherwise loop forever. -/ +def partialMachine : FinTM2 where + K := Unit + k₀ := () + k₁ := () + Γ _ := Bool + Λ := Unit + main := () + σ := Bool + initialState := false + m _ := .peek () (fun _ b => b.getD false) + (.branch id (.load (fun _ => false) (.push () (fun _ => false) .halt)) + (.goto (fun _ => ()))) + +example (w : List Bool) (hw : w.head? ≠ some true) : + partialMachine.step (initList partialMachine w) = some (initList partialMachine w) := by + cases w with + | nil => rfl + | cons b bs => + cases b with + | false => rfl + | true => exact False.elim (hw rfl) + +example : (fun w : List Bool => if w.head? = some true then false :: w else []) ∈ FP := by + classical + let computer : TM2ComputableAux Bool Bool := + ⟨partialMachine, Equiv.refl Bool, Equiv.refl Bool⟩ + let : ∀ k, Fintype (computer.tm.Γ k) := fun _ => by + change Fintype Bool + infer_instance + have h : (fun w => if w ∈ startsTrue.accepts then false :: w else []) ∈ FP := by + apply MathlibTM2.mem_FP_on_dfa computer startsTrue (p := 1) + intro w hw + have hh := (startsTrue_accepts w).mp hw + cases w with + | nil => simp at hh + | cons b bs => + have hb : b = true := Option.some.inj hh + subst b + exact ⟨⟨1, rfl⟩, by simp⟩ + simpa only [startsTrue_accepts] using h + +/-- Explicit four-letter code; the choice of numbering is visible to clients. -/ +def fourCode : FiniteAlphabetCode (Fin 4) := ⟨4, Equiv.refl _⟩ + +/-- A different output alphabet with just one letter. -/ +def oneCode : FiniteAlphabetCode (Fin 1) := ⟨1, Equiv.refl _⟩ + +example : fourCode.encode [0, 3, 1] = + [false, false, false, true, true, true, false, false, true, false] := rfl + +example : fourCode.decode [true] = none := rfl +example : fourCode.decode [false] = none := rfl +example : fourCode.decode [false, true, true, true, true, false] = none := rfl +example : fourCode.decode [] = some [] := rfl + +example (xs : List (Fin 4)) : fourCode.decode (fourCode.encode xs) = some xs := + fourCode.decode_encode xs + +/-- Prepending a nonzero letter distinguishes word reversal from symbol coding. -/ +def prependFourMachine : FinTM2 := + { idComputer (Fin 4) with m := fun _ => .push () (fun _ => (3 : Fin 4)) .halt } + +/-- A four-letter source certificate, with no encoded-output size premise. -/ +noncomputable def prependFourComputer : + TM2ComputableInPolyTime id id (fun xs : List (Fin 4) => 3 :: xs) where + tm := prependFourMachine + inputAlphabet := Equiv.refl _ + outputAlphabet := Equiv.refl _ + time := 1 + outputsFun _ := ⟨⟨1, rfl⟩, by simp⟩ + +example : fourCode.map fourCode (fun xs => 3 :: xs) ∈ FP := by + let : ∀ k, Fintype (prependFourComputer.tm.Γ k) := fun _ => by + change Fintype (Fin 4) + infer_instance + exact MathlibTM2.mem_FP_encoded fourCode fourCode prependFourComputer + +example : fourCode.map fourCode (fun xs => 3 :: xs) [true] = [] := rfl + +/-- A domain sensitive to the second bit, hence to which end of the word is read first. -/ +def secondTrue : DFA Bool (Option (Option Bool)) where + step q b := match q with + | none => some none + | some none => some (some b) + | some (some a) => some (some a) + start := none + accept := {some (some true)} + +noncomputable instance : DecidablePred (· ∈ secondTrue.accepts) := Classical.decPred _ + +example : + (fun bits => if bits ∈ secondTrue.accepts then + fourCode.map fourCode (fun xs => 3 :: xs) bits else []) ∈ FP := by + classical + let computer : TM2ComputableAux (Fin 4) (Fin 4) := + ⟨prependFourMachine, Equiv.refl _, Equiv.refl _⟩ + let : ∀ k, Fintype (computer.tm.Γ k) := fun _ => by + change Fintype (Fin 4) + infer_instance + exact MathlibTM2.mem_FP_encoded_on_dfa fourCode fourCode computer secondTrue + (p := 1) (fun _ _ => ⟨⟨1, rfl⟩, by simp⟩) + +/-- Clear a four-letter input stack, then return one letter on a different singleton stack. -/ +public abbrev constantMachine : FinTM2 where + K := Bool + k₀ := false + k₁ := true + Γ k := match k with + | false => Fin 4 + | true => Fin 1 + Λ := Unit + main := () + σ := Bool + initialState := false + m _ := .pop false (fun _ a => a.isSome) + (.branch id (.goto (fun _ => ())) (.push true (fun _ => (0 : Fin 1)) .halt)) + +/-- Source states while consuming the input. -/ +def constantCfg (xs : List (Fin 4)) (v : Bool) : constantMachine.Cfg where + l := some () + var := v + stk k := match k with + | false => xs + | true => [] + +lemma constant_nil (v : Bool) : + constantMachine.step (constantCfg [] v) = some (haltList constantMachine [(0 : Fin 1)]) := by + apply congrArg some + change TM2.Cfg.mk _ _ _ = TM2.Cfg.mk _ _ _ + congr 1 + funext k + cases k <;> rfl + +lemma constant_cons (a : Fin 4) (xs : List (Fin 4)) (v : Bool) : + constantMachine.step (constantCfg (a :: xs) v) = some (constantCfg xs true) := by + apply congrArg some + change TM2.Cfg.mk _ _ _ = TM2.Cfg.mk _ _ _ + congr 1 + funext k + cases k <;> rfl + +lemma constant_iterate (xs : List (Fin 4)) (v : Bool) : + (fun s : Option constantMachine.Cfg => s.bind constantMachine.step)^[xs.length + 1] + (some (constantCfg xs v)) = some (haltList constantMachine [(0 : Fin 1)]) := by + induction xs generalizing v with + | nil => exact constant_nil v + | cons a xs ih => + change (fun s : Option constantMachine.Cfg => s.bind constantMachine.step)^[xs.length + 1 + 1] + (some (constantCfg (a :: xs) v)) = _ + rw [Function.iterate_succ_apply] + simp only [Option.bind_some, constant_cons] + exact ih true + +/-- A polynomial source certificate whose input and output alphabet sizes differ. -/ +noncomputable def constantComputer : + TM2ComputableInPolyTime id id (fun _ : List (Fin 4) => [(0 : Fin 1)]) where + tm := constantMachine + inputAlphabet := Equiv.refl _ + outputAlphabet := Equiv.refl _ + time := Polynomial.X + 1 + outputsFun xs := by + refine ⟨⟨xs.length + 1, ?_⟩, by simp⟩ + have hi : initList constantMachine xs = constantCfg xs false := by + change TM2.Cfg.mk _ _ _ = TM2.Cfg.mk _ _ _ + congr 1 + funext k + cases k <;> rfl + simp only [Equiv.refl, id_eq, Option.map_some] + change (fun s : Option constantMachine.Cfg => s.bind constantMachine.step)^[xs.length + 1] + (some (initList constantMachine (xs.map (@id (Fin 4))))) = + some (haltList constantMachine [(0 : Fin 1)]) + rw [List.map_id, hi] + exact constant_iterate xs false + +example : fourCode.map oneCode (fun _ => [(0 : Fin 1)]) ∈ FP := by + let : ∀ k, Fintype (constantComputer.tm.Γ k) := fun k => by + change Fintype (constantMachine.Γ k) + cases k with + | false => change Fintype (Fin 4); infer_instance + | true => change Fintype (Fin 1); infer_instance + exact MathlibTM2.mem_FP_encoded fourCode oneCode constantComputer + +example : fourCode.map oneCode (fun _ => [(0 : Fin 1)]) [] = [false, false] := rfl +example : fourCode.map oneCode (fun _ => [(0 : Fin 1)]) [false] = [] := rfl + +example : + (FiniteAlphabetCode.ofFintype Empty).map (FiniteAlphabetCode.ofFintype Empty) + (fun xs => xs) ∈ FP := by + let computer := idComputableInPolyTime (fun xs : List Empty => xs) + let : ∀ k, Fintype (computer.tm.Γ k) := fun _ => by + change Fintype Empty + infer_instance + exact MathlibTM2.mem_FP_encoded _ _ computer + +end Complexity.TM2FPApiCheck