cfahlgren1's picture
download
raw
35.5 kB
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Computability.TuringMachine.Computable
import Mathlib.Logic.Function.Iterate
import OAI.Computability.UniqueGames.Machines.MachineCompositionLemmas
import OAI.Computability.UniqueGames.Machines.MachineSubroutineLemmas
namespace OAI
section
namespace UniqueGamesTheorem.Foundations.Complexity
def naturalWordsEncoding : Computability.Encoding (List Nat) Bool where
encode := encodeWords
decode := decodeWords
decode_encode := decodeWords_encodeWords
def formulaEncoding : Computability.Encoding Target.Formula Bool where
encode := formulaBits
decode := decodeFormulaBits
decode_encode := decodeFormulaBits_encoded
/-- Concrete forward-table output syntax with a checked parser. -/
def gameEncoding (alphabet : Nat) : Computability.Encoding (Target.Instance alphabet) Bool where
encode := gameBits
decode := decodeGameBits alphabet
decode_encode := decodeGameBits_encoded
/-- A fixed-bit prefixer uses one finite stack, one label, and one internal state.
The bit is fixed in the program; it is not a second, freely accessible input. -/
def prefixBitMachine (bit : Bool) : Turing.FinTM2 where
K := Unit
k₀ := ()
k₁ := ()
Γ _ := Bool
Λ := Unit
main := ()
σ := Unit
initialState := ()
m _ := .push () (fun _ => bit) .halt
/-- A real machine certificate: one transition performs the push and halts.
TM2 counts execution of one finite statement as one transition. Here that
statement contains exactly one stack push. -/
noncomputable def prefixBitPolyTime (bit : Bool) :
Turing.TM2ComputableInPolyTime (id : List Bool → List Bool) id (bit :: ·) where
tm := prefixBitMachine bit
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := 1
outputsFun bs := {
steps := 1
evals_in_steps := by
change some (Turing.TM2.stepAux (.push () (fun _ => bit) .halt) ()
(fun _ : Unit => bs.map id)) =
some { l := none, var := (), stk := fun _ : Unit => (bit :: bs).map id }
simp [Turing.TM2.stepAux]
funext k
cases k
rfl
steps_le_m := by simp
}
/-- Increasing a polynomial bound preserves the actual execution witness. -/
def enlargePolynomialBound {α β αΓ βΓ : Type}
{ea : α → List αΓ} {eb : β → List βΓ} {f : α → β}
(certificate : Turing.TM2ComputableInPolyTime ea eb f)
(bound : Polynomial Nat)
(larger : ∀ n, certificate.time.eval n ≤ bound.eval n) :
Turing.TM2ComputableInPolyTime ea eb f where
toTM2ComputableAux := certificate.toTM2ComputableAux
time := bound
outputsFun a := {
toEvalsTo := (certificate.outputsFun a).toEvalsTo
steps_le_m := Nat.le_trans (certificate.outputsFun a).steps_le_m (larger _)
}
/-- Two verified phases of one transition system compose with added polynomial
budgets, both measured in the original input length. This theorem preserves the
actual sequence of transition steps, not merely the sizes of configurations. -/
def composeMachinePhases {σ : Type} (step : σ → Option σ)
(start intermediate : σ) (finish : Option σ)
(p q : Polynomial Nat) (inputLength : Nat)
(first : StateTransition.EvalsToInTime step start (some intermediate) (p.eval inputLength))
(second : StateTransition.EvalsToInTime step intermediate finish (q.eval inputLength)) :
StateTransition.EvalsToInTime step start finish ((p + q).eval inputLength) := by
have composed := StateTransition.EvalsToInTime.trans step (p.eval inputLength)
(q.eval inputLength) start intermediate finish first second
simpa only [Polynomial.eval_add, Nat.add_comm] using composed
open Target
structure PolynomialGapReduction
(completenessError soundnessError : RationalError)
extends SemanticGapReduction completenessError soundnessError where
computation : Turing.TM2ComputableInPolyTime formulaEncoding.encode
(gameEncoding alphabet).encode reduce
end UniqueGamesTheorem.Foundations.Complexity
end
section
/-!
Verifier-defined NP over finite bitstring machines. This definition does not
mention SAT. Inputs and witnesses use a fixed explicit framing, verifiers return
one Boolean symbol, and all stacks have finite alphabets. The Cook–Levin target
separately requires an actual polynomial-time machine producing the established
`formulaBits` serialization, in addition to satisfiability equivalence.
-/
namespace UniqueGamesTheorem.Foundations.Complexity.CookLevin
/-- A unary length frame followed by input bits and witness bits. -/
def pairBits (input : List Bool × List Bool) : List Bool :=
encodeWord input.1.length ++ input.1 ++ input.2
def decodePairAux : Nat → List Bool → Option (List Bool × List Bool)
| _, [] => none
| read, true :: rest => decodePairAux (read + 1) rest
| read, false :: rest =>
if read ≤ rest.length then some (rest.take read, rest.drop read) else none
def decodePair (bits : List Bool) : Option (List Bool × List Bool) :=
decodePairAux 0 bits
theorem decodePairAux_frame (read count : Nat) (rest : List Bool) :
decodePairAux read (List.replicate count true ++ false :: rest) =
if read + count ≤ rest.length then
some (rest.take (read + count), rest.drop (read + count)) else none := by
induction count generalizing read with
| zero => simp [decodePairAux]
| succ count ih =>
simp only [List.replicate_succ, List.cons_append, decodePairAux, ih]
simp only [Nat.add_assoc, Nat.add_comm 1 count]
@[simp] theorem decodePair_pairBits (input : List Bool × List Bool) :
decodePair (pairBits input) = some input := by
rcases input with ⟨x, witness⟩
simp [decodePair, pairBits, encodeWord, List.append_assoc,
decodePairAux_frame]
theorem pairBits_injective : Function.Injective pairBits := by
intro first second same
have h := congrArg decodePair same
simpa only [decodePair_pairBits, Option.some.injEq] using h
@[simp] theorem pairBits_length (input : List Bool × List Bool) :
(pairBits input).length = 2 * input.1.length + input.2.length + 1 := by
simp only [pairBits, List.length_append, encodeWord_length]
omega
def pairEncoding : Computability.Encoding (List Bool × List Bool) Bool where
encode := pairBits
decode := decodePair
decode_encode := decodePair_pairBits
/-- A standard polynomial-time Boolean verifier. `FinTM2` already requires
finitely many stacks, control labels, and internal states; this field also
requires every stack alphabet to be finite. The actual machine runs on the
explicit paired input and halts with exactly one Boolean output symbol. -/
structure NPVerifier where
witnessBound : Polynomial Nat
verify : (List Bool × List Bool) → Bool
computation : Turing.TM2ComputableInPolyTime pairBits (fun bit => [bit]) verify
finiteAlphabet : (k : computation.tm.K) → Fintype (computation.tm.Γ k)
def NPVerifier.Accepts (verifier : NPVerifier) (input : List Bool) : Prop :=
∃ witness : List Bool,
witness.length ≤ verifier.witnessBound.eval input.length ∧
verifier.verify (input, witness) = true
/-- NP is specified by polynomial witnesses and genuine finite-machine
verification, independently of any reduction or satisfiability problem. -/
def InNP (language : List Bool → Prop) : Prop :=
∃ verifier : NPVerifier, ∀ input, language input ↔ verifier.Accepts input
theorem NPVerifier.acceptedLanguage_inNP (verifier : NPVerifier) :
InNP verifier.Accepts := ⟨verifier, fun _ => Iff.rfl⟩
/-- The demanded Cook–Levin endpoint: both the SAT equivalence and an actual
machine producing the fixed existing unary-token 3CNF encoding. This structure
contains obligations; its inhabitance is not assumed or asserted here. -/
structure PolynomialThreeSATReduction (language : List Bool → Prop) where
reduce : List Bool → Target.Formula
computation : Turing.TM2ComputableInPolyTime id formulaBits reduce
correct : ∀ input, language input ↔ (reduce input).Satisfiable
/-- A common clock for all witnesses of permitted length for one input. -/
noncomputable def NPVerifier.horizon (verifier : NPVerifier) (inputLength : Nat) : Nat :=
verifier.computation.time.eval
(2 * inputLength + verifier.witnessBound.eval inputLength + 1)
theorem NPVerifier.input_time_le_horizon (verifier : NPVerifier)
(input witness : List Bool)
(bounded : witness.length ≤ verifier.witnessBound.eval input.length) :
verifier.computation.time.eval (pairBits (input, witness)).length ≤
verifier.horizon input.length := by
apply MachineComposition.natPolynomial_eval_mono
rw [pairBits_length]
exact Nat.add_le_add_right (Nat.add_le_add_left bounded _) _
/-- A real verifier execution, enlarged to the single input-dependent clock.
The machine and its output are the ones supplied in the verifier definition. -/
def NPVerifier.runWithinHorizon (verifier : NPVerifier)
(input witness : List Bool)
(bounded : witness.length ≤ verifier.witnessBound.eval input.length) :
Turing.TM2OutputsInTime verifier.computation.tm
((pairBits (input, witness)).map verifier.computation.inputAlphabet.invFun)
(some ([verifier.verify (input, witness)].map verifier.computation.outputAlphabet.invFun))
(verifier.horizon input.length) where
toEvalsTo := (verifier.computation.outputsFun (input, witness)).toEvalsTo
steps_le_m := Nat.le_trans
(verifier.computation.outputsFun (input, witness)).steps_le_m
(verifier.input_time_le_horizon input witness bounded)
end UniqueGamesTheorem.Foundations.Complexity.CookLevin
end
section
/-!
Pure list specification for destructive zero-based unary table lookup.
These are serialization identities and bit-count bounds, not TM2 runtime
certificates. The connection to actual transitions is in `MachineLookup`.
-/
namespace UniqueGamesTheorem.Foundations.Complexity.MachineLookupSpec
/-- Saturating removal of one zero-delimited word. -/
def skipWord : List Bool → List Bool
| [] => []
| true :: bs => skipWord bs
| false :: bs => bs
@[simp] theorem skipWord_encodeWord (n : Nat) (bs : List Bool) :
skipWord (encodeWord n ++ bs) = bs := by
induction n with
| zero => simp [encodeWord, skipWord]
| succ n ih =>
simpa [encodeWord, List.replicate_succ, skipWord] using ih
/-- Iterated word removal agrees with zero-based list dropping, even after
running past the end of the table. -/
theorem skipWord_iterate_encodeWords (i : Nat) (values : List Nat) :
(skipWord^[i]) (encodeWords values) = encodeWords (values.drop i) := by
induction i generalizing values with
| zero => rfl
| succ i ih =>
rw [Function.iterate_succ_apply]
cases values with
| nil => simpa [encodeWords, skipWord] using ih []
| cons n ns =>
simpa only [encodeWords, skipWord_encodeWord, List.drop_succ_cons] using ih ns
/-- Copy the next complete word, including its delimiter. Empty input and
unterminated words fail explicitly. -/
def headWord : List Bool → Option (List Bool)
| [] => none
| false :: _ => some [false]
| true :: bs => (headWord bs).map (true :: ·)
@[simp] theorem headWord_encodeWord (n : Nat) (bs : List Bool) :
headWord (encodeWord n ++ bs) = some (encodeWord n) := by
induction n with
| zero => simp [encodeWord, headWord]
| succ n ih =>
simpa [encodeWord, List.replicate_succ, headWord] using
congrArg (Option.map (true :: ·)) ih
@[simp] theorem headWord_encodeWords (values : List Nat) :
headWord (encodeWords values) = values.head?.map encodeWord := by
cases values with
| nil => rfl
| cons n ns => exact headWord_encodeWord n (encodeWords ns)
def lookupBits (i : Nat) (table : List Bool) : Option (List Bool) :=
headWord ((skipWord^[i]) table)
@[simp] theorem lookupBits_encodeWords (i : Nat) (values : List Nat) :
lookupBits i (encodeWords values) = values[i]?.map encodeWord := by
rw [lookupBits, skipWord_iterate_encodeWords, headWord_encodeWords,
List.head?_drop]
/-- The index is on a separate tape and must decode to exactly one word. -/
def lookupEncoded (index table : List Bool) : Option (List Bool) :=
match decodeWords index with
| some [i] => lookupBits i table
| _ => none
@[simp] theorem decodeWords_encodeWord (i : Nat) :
decodeWords (encodeWord i) = some [i] := by
simpa [encodeWords] using decodeWords_encodeWords [i]
@[simp] theorem lookupEncoded_encode (i : Nat) (values : List Nat) :
lookupEncoded (encodeWord i) (encodeWords values) =
values[i]?.map encodeWord := by
simp [lookupEncoded]
theorem lookupEncoded_some (i value : Nat) (values : List Nat)
(h : values[i]? = some value) :
lookupEncoded (encodeWord i) (encodeWords values) = some (encodeWord value) := by
simp [h]
theorem lookupEncoded_none (i : Nat) (values : List Nat)
(h : values.length ≤ i) :
lookupEncoded (encodeWord i) (encodeWords values) = none := by
simp [List.getElem?_eq_none h]
/-- Literal split at the selected word. -/
theorem encoded_selected_split (values : List Nat) (i value : Nat)
(h : values[i]? = some value) :
encodeWords values = encodeWords (values.take i) ++ encodeWord value ++
encodeWords (values.drop (i + 1)) := by
rcases List.getElem?_eq_some_iff.mp h with ⟨hi, hv⟩
have hs : values = values.take i ++ value :: values.drop (i + 1) := by
calc
values = values.take i ++ values.drop i := (List.take_append_drop i values).symm
_ = _ := by rw [List.drop_eq_getElem_cons hi, hv]
simpa only [encodeWords_append, encodeWords, List.append_assoc] using
congrArg encodeWords hs
/-- This counts the bits through the selected word, capped at the whole table
when the index is invalid. It is not a machine step count. -/
def scannedBits (values : List Nat) (i : Nat) : Nat :=
(encodeWords (values.take (i + 1))).length
theorem encoded_prefix_length_le (values : List Nat) (i : Nat) :
(encodeWords (values.take i)).length ≤ (encodeWords values).length := by
have hs : encodeWords (values.take i) ++ encodeWords (values.drop i) =
encodeWords values := by
rw [← encodeWords_append, List.take_append_drop]
have hl := congrArg List.length hs
simp only [List.length_append] at hl
omega
theorem scannedBits_le (values : List Nat) (i : Nat) :
scannedBits values i ≤ (encodeWords values).length :=
encoded_prefix_length_le values (i + 1)
theorem scannedBits_of_some (values : List Nat) (i value : Nat)
(h : values[i]? = some value) :
scannedBits values i = (encodeWords (values.take i)).length + value + 1 := by
rcases List.getElem?_eq_some_iff.mp h with ⟨hi, hv⟩
simp [scannedBits, List.take_succ_eq_append_getElem hi, hv, encodeWords,
Nat.add_assoc]
theorem selected_scan_length_le (values : List Nat) (i value : Nat)
(h : values[i]? = some value) :
(encodeWords (values.take i)).length + value + 1 ≤
(encodeWords values).length := by
rw [← scannedBits_of_some values i value h]
exact scannedBits_le values i
theorem output_length_le (values : List Nat) (i value : Nat)
(h : values[i]? = some value) :
(encodeWord value).length ≤ (encodeWords values).length := by
have hs := selected_scan_length_le values i value h
rw [encodeWord_length]
omega
theorem index_length_le (values : List Nat) (i value : Nat)
(h : values[i]? = some value) :
(encodeWord i).length ≤ (encodeWords values).length := by
rcases List.getElem?_eq_some_iff.mp h with ⟨hi, _⟩
have hp : i ≤ (encodeWords (values.take i)).length := by
simp only [encodeWords_length, List.length_take, Nat.min_eq_left (Nat.le_of_lt hi)]
omega
have hs := selected_scan_length_le values i value h
rw [encodeWord_length]
omega
/-! Bounds on the proposed lookup recurrence. A separate machine proof must
identify these counts with actual transition traces. -/
def steps : List Nat → Nat → Nat
| [], i => 2 * i + 2
| n :: _, 0 => n + 3
| n :: ns, i + 1 => n + 2 + steps ns i
/-- A uniform bound, including indices beyond the end of the table. -/
theorem steps_le_encoded_length_strong (values : List Nat) (i : Nat) :
steps values i ≤ (encodeWords values).length + 2 * i + 2 := by
induction values generalizing i with
| nil => simp [steps, encodeWords]
| cons n ns ih =>
cases i with
| zero =>
simp only [steps, encodeWords, List.length_append, encodeWord_length]
omega
| succ i =>
have ht := ih i
simp only [steps, encodeWords, List.length_append, encodeWord_length]
omega
/-- The requested recurrence bound with one further unit of slack. -/
theorem steps_le_encoded_length (values : List Nat) (i : Nat) :
steps values i ≤ (encodeWords values).length + 2 * i + 3 := by
have h := steps_le_encoded_length_strong values i
omega
/-- Successful lookup uses exactly the consumed table prefix, one guard per
skipped word, and the two final control steps. -/
theorem steps_eq_scannedBits_of_lt (values : List Nat) (i : Nat)
(h : i < values.length) :
steps values i = scannedBits values i + i + 2 := by
induction values generalizing i with
| nil => simp at h
| cons n ns ih =>
cases i with
| zero => simp [steps, scannedBits, encodeWords, Nat.add_assoc]
| succ i =>
have ht := ih i (by simpa using h)
simp only [scannedBits] at ht
simp only [steps, scannedBits, List.take_succ_cons, encodeWords,
List.length_append, encodeWord_length]
omega
theorem steps_eq_scannedBits_of_some (values : List Nat) (i value : Nat)
(h : values[i]? = some value) :
steps values i = scannedBits values i + i + 2 := by
rcases List.getElem?_eq_some_iff.mp h with ⟨hi, _⟩
exact steps_eq_scannedBits_of_lt values i hi
/-- A subtraction-free exact failure formula: the whole table has been
consumed, and every remaining unary index bit costs two steps. -/
theorem steps_add_length_of_invalid (values : List Nat) (i : Nat)
(h : values.length ≤ i) :
steps values i + values.length = (encodeWords values).length + 2 * i + 2 := by
induction values generalizing i with
| nil => simp [steps, encodeWords]
| cons n ns ih =>
cases i with
| zero => simp at h
| succ i =>
have ht := ih i (by simpa using h)
simp only [steps, encodeWords, List.length_append, encodeWord_length,
List.length_cons]
omega
/-- Linear in the combined encoded tape lengths. This is a bound for `steps`,
not yet a theorem about a concrete machine's execution. -/
theorem steps_le_input_encoding_size (values : List Nat) (i : Nat) :
steps values i ≤ (encodeWords values).length + 2 * (encodeWord i).length := by
have h := steps_le_encoded_length_strong values i
rw [encodeWord_length]
omega
end UniqueGamesTheorem.Foundations.Complexity.MachineLookupSpec
end
section
/-!
One actual TM2 transition tests and decrements a zero-delimited unary counter.
The guard peeks before branching: a true bit is popped, while a false delimiter
is retained. Both branches clear the one-bit register and preserve the ambient
state. There is no assumed execution trace or input-dependent finite program.
-/
namespace UniqueGamesTheorem.Foundations.Complexity.MachineUnaryCounter
open Turing
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
/-- A single finite statement: inspect the counter, decrement a positive
counter, and jump to the body or exit. The zero delimiter is never consumed. -/
def guard (counter : K) (bodyLabel exitLabel : Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) :=
.peek counter (fun state head => (state.1, head))
(.branch (fun state => state.2.getD false)
(.pop counter (fun state _ => (state.1, none)) (.goto fun _ => bodyLabel))
(.load (fun state => (state.1, none)) (.goto fun _ => exitLabel)))
/-- An arbitrary caller frame with one tape holding the unary counter and
its unread suffix. Only this tape depends on the counter value. -/
def counterTapes (counter : K) (base : K → List Bool) (n : Nat)
(suffix : List Bool) : K → List Bool :=
Function.update base counter (encodeWord n ++ suffix)
@[simp] theorem counterTapes_counter (counter : K) (base : K → List Bool)
(n : Nat) (suffix : List Bool) :
counterTapes counter base n suffix counter = encodeWord n ++ suffix := by
simp [counterTapes]
theorem counterTapes_other (counter other : K) (hne : other ≠ counter)
(base : K → List Bool) (n : Nat) (suffix : List Bool) :
counterTapes counter base n suffix other = base other := by
simp [counterTapes, hne]
omit [DecidableEq K] in
theorem guardPushBound (counter : K) (bodyLabel exitLabel : Λ) :
Runtime.statementPushBound (guard (σ := σ) counter bodyLabel exitLabel) = 0 := by
rfl
/-- Positive counters lose exactly one true bit; the delimiter and suffix
remain. Ambient state, unrelated tapes, and labels are accounted for explicitly. -/
theorem stepAux_succ (counter : K) (bodyLabel exitLabel : Λ)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.stepAux (guard counter bodyLabel exitLabel) (ambient, register)
(counterTapes counter base (n + 1) suffix) =
⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by
simp [guard, TM2.stepAux, counterTapes, encodeWord, List.replicate_succ]
/-- A zero counter is inspected without removing its delimiter. -/
theorem stepAux_zero (counter : K) (bodyLabel exitLabel : Λ)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.stepAux (guard counter bodyLabel exitLabel) (ambient, register)
(counterTapes counter base 0 suffix) =
⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by
simp [guard, TM2.stepAux, counterTapes, encodeWord]
theorem guardStep_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.step program
⟨some guardLabel, (ambient, register), counterTapes counter base (n + 1) suffix⟩ =
some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by
change some (TM2.stepAux (program guardLabel) (ambient, register)
(counterTapes counter base (n + 1) suffix)) = _
rw [atGuard, stepAux_succ]
theorem guardStep_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.step program
⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩ =
some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by
change some (TM2.stepAux (program guardLabel) (ambient, register)
(counterTapes counter base 0 suffix)) = _
rw [atGuard, stepAux_zero]
/-- The positive branch executes in exactly one transition of the ambient program. -/
theorem guardTrace_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[1]
(some ⟨some guardLabel, (ambient, register),
counterTapes counter base (n + 1) suffix⟩) =
some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard
base n suffix ambient register
theorem guardTrace_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[1]
(some ⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩) =
some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
guardStep_zero counter guardLabel bodyLabel exitLabel program atGuard
base suffix ambient register
def guardInTime_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some guardLabel, (ambient, register), counterTapes counter base (n + 1) suffix⟩
(some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩) 1 where
steps := 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[1] _ = _
exact guardTrace_succ counter guardLabel bodyLabel exitLabel program atGuard
base n suffix ambient register
steps_le_m := Nat.le_refl _
def guardInTime_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩
(some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩) 1 where
steps := 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[1] _ = _
exact guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard
base suffix ambient register
steps_le_m := Nat.le_refl _
end UniqueGamesTheorem.Foundations.Complexity.MachineUnaryCounter
end
section
/-!
Counted loops in one actual TM2 program. A unary guard is executed once before
each caller body and once more at zero. The caller supplies only individual
body traces, each returning to the guard with the same counter remainder.
Schedules are indexed by the remaining count: body `r` takes ambient state
`r + 1` to ambient state `r`. Consequently the run starts at index `n` and
ends at index zero. Bodies may change the ambient state and unprotected tapes.
-/
namespace UniqueGamesTheorem.Foundations.Complexity.MachineCountedLoop
open Turing
open scoped BigOperators
open MachineUnaryCounter
variable {K Λ σ : Type} [DecidableEq K]
def guardConfiguration (counter : K) (guardLabel : Λ) (suffix : List Bool)
(ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (n : Nat) :
TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) :=
⟨some guardLabel, (ambient n, register n), counterTapes counter (base n) n suffix⟩
def bodyConfiguration (counter : K) (bodyLabel : Λ) (suffix : List Bool)
(ambient : Nat → σ) (base : Nat → K → List Bool) (n : Nat) :
TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) :=
⟨some bodyLabel, (ambient (n + 1), none), counterTapes counter (base (n + 1)) n suffix⟩
def exitConfiguration (counter : K) (exitLabel : Λ) (suffix : List Bool)
(ambient : Nat → σ) (base : Nat → K → List Bool) :
TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) :=
⟨some exitLabel, (ambient 0, none), counterTapes counter (base 0) 0 suffix⟩
/-- The hypotheses are actual traces of the caller's body in the same program.
Both endpoints have the same unary remainder and unread suffix. -/
def BodyTraces (counter : K) (guardLabel bodyLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) : Prop :=
∀ r, r < n →
(MachineComposition.advance (TM2.step program))^[cost r]
(some (bodyConfiguration counter bodyLabel suffix ambient base r)) =
some (guardConfiguration counter guardLabel suffix ambient register base r)
/-- Exactly `n` supplied body blocks and `n + 1` concrete guard transitions. -/
def totalSteps (cost : Nat → Nat) (n : Nat) : Nat :=
(∑ r ∈ Finset.range n, cost r) + n + 1
@[simp] theorem totalSteps_zero (cost : Nat → Nat) : totalSteps cost 0 = 1 := by
simp [totalSteps]
theorem totalSteps_succ (cost : Nat → Nat) (n : Nat) :
totalSteps cost (n + 1) = (totalSteps cost n + cost n) + 1 := by
simp only [totalSteps, Finset.sum_range_succ]
omega
/-- The loop trace is derived by composing each actual unary guard transition
with one supplied body trace. No whole-loop execution is assumed. -/
theorem loopTrace (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat)
(bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix
ambient register base cost n) :
(MachineComposition.advance (TM2.step program))^[totalSteps cost n]
(some (guardConfiguration counter guardLabel suffix ambient register base n)) =
some (exitConfiguration counter exitLabel suffix ambient base) := by
revert bodyTraces
induction n with
| zero =>
intro _
simpa only [totalSteps_zero, guardConfiguration, exitConfiguration] using
guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard
(base 0) suffix (ambient 0) (register 0)
| succ n ih =>
intro bodyTraces
rw [totalSteps_succ, Function.iterate_succ_apply]
change (MachineComposition.advance (TM2.step program))^[totalSteps cost n + cost n]
(TM2.step program
⟨some guardLabel, (ambient (n + 1), register (n + 1)),
counterTapes counter (base (n + 1)) (n + 1) suffix⟩) = _
rw [guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard
(base (n + 1)) n suffix (ambient (n + 1)) (register (n + 1)),
Function.iterate_add_apply]
change (MachineComposition.advance (TM2.step program))^[totalSteps cost n]
((MachineComposition.advance (TM2.step program))^[cost n]
(some (bodyConfiguration counter bodyLabel suffix ambient base n))) = _
rw [bodyTraces n (Nat.lt_succ_self n)]
exact ih (fun r hr => bodyTraces r (Nat.lt_trans hr (Nat.lt_succ_self n)))
theorem totalSteps_le (cost : Nat → Nat) (n B : Nat)
(bodyBound : ∀ r, r < n → cost r ≤ B) :
totalSteps cost n ≤ n * (B + 1) + 1 := by
have hsum : (∑ r ∈ Finset.range n, cost r) ≤ n * B := by
calc
(∑ r ∈ Finset.range n, cost r) ≤ ∑ _r ∈ Finset.range n, B :=
Finset.sum_le_sum (fun r hr => bodyBound r (Finset.mem_range.mp hr))
_ = n * B := by simp
simpa only [totalSteps, Nat.mul_add, Nat.mul_one] using
Nat.add_le_add_right (Nat.add_le_add_right hsum n) 1
/-- An actual timed execution witness with the exact composed step count. -/
def loopInTime (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n B : Nat)
(bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix
ambient register base cost n)
(bodyBound : ∀ r, r < n → cost r ≤ B) :
StateTransition.EvalsToInTime (TM2.step program)
(guardConfiguration counter guardLabel suffix ambient register base n)
(some (exitConfiguration counter exitLabel suffix ambient base))
(n * (B + 1) + 1) where
steps := totalSteps cost n
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] _ = _
exact loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix
ambient register base cost n bodyTraces
steps_le_m := totalSteps_le cost n B bodyBound
omit [DecidableEq K] in
/-- Chaining the caller's declared per-body frame equalities. -/
theorem baseFrame (base : Nat → K → List Bool) (frame : K → Prop) (n : Nat)
(bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) :
∀ k, frame k → base n k = base 0 k := by
revert bodyFrame
induction n with
| zero => intro _ k _; rfl
| succ n ih =>
intro bodyFrame k hk
exact (bodyFrame n (Nat.lt_succ_self n) k hk).trans
(ih (fun r hr => bodyFrame r (Nat.lt_trans hr (Nat.lt_succ_self n))) k hk)
/-- Protected tapes are unchanged by the complete loop, including its guard
steps. The counter itself is explicitly excluded from this frame. -/
theorem counterFrame (counter : K) (suffix : List Bool)
(base : Nat → K → List Bool) (frame : K → Prop) (n : Nat)
(counterOutside : ∀ k, frame k → k ≠ counter)
(bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) :
∀ k, frame k →
counterTapes counter (base n) n suffix k = counterTapes counter (base 0) 0 suffix k := by
intro k hk
rw [counterTapes_other counter k (counterOutside k hk),
counterTapes_other counter k (counterOutside k hk)]
exact baseFrame base frame n bodyFrame k hk
/-- Actual loop execution and preservation of an arbitrary declared tape frame. -/
theorem loopTrace_framed (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat)
(bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix
ambient register base cost n)
(frame : K → Prop) (counterOutside : ∀ k, frame k → k ≠ counter)
(bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) :
((MachineComposition.advance (TM2.step program))^[totalSteps cost n]
(some (guardConfiguration counter guardLabel suffix ambient register base n)) =
some (exitConfiguration counter exitLabel suffix ambient base)) ∧
(∀ k, frame k →
(guardConfiguration counter guardLabel suffix ambient register base n).stk k =
(exitConfiguration counter exitLabel suffix ambient base).stk k) :=
⟨loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix
ambient register base cost n bodyTraces,
counterFrame counter suffix base frame n counterOutside bodyFrame⟩
end UniqueGamesTheorem.Foundations.Complexity.MachineCountedLoop
end
end OAI

Xet Storage Details

Size:
35.5 kB
·
Xet hash:
03777e8e2607b14ecff2fdcc5b63ef5e06349067fa4f5ad76fef306eb809b080

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.