math / data /lean /OAI /Algorithms /SubexponentialSAT /SqrtLogSpace /AttainedSignatureRescan.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
54.4 kB
import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.AttainedSignatureProgram
import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.CounterLoops
/-! Actual bounded-word predecessor rescan and its uniform source-prefix storage. -/
namespace OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.AttainedSignatureProgram
open OAI.Algorithms.SubexponentialSAT.BitProgram OAI.Algorithms.SubexponentialSAT.StructuredCalls OAI.Algorithms.SubexponentialSAT.Subexp.MatrixProgramSupport
open OAI.Algorithms.SubexponentialSAT.SqrtLog.BucketProgram
namespace Rescan
theorem workState_fuel_update (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (candidate saved predecessor fuel newFuel : List Bool)
(fresh : Bool) :
(workState regs W rows candidate saved predecessor newFuel fresh [] []).stacks =
Function.update (workState regs W rows candidate saved predecessor fuel fresh [] []).stacks
19 newFuel := by
funext i
fin_cases i <;> simp [workState]
theorem nextState_workState (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (candidate saved predecessor fuel : List Bool)
(fresh : Bool) :
CounterForPrograms.nextState countdownPorts
(workState regs W rows candidate saved predecessor fuel fresh [] []) =
workState (CounterForPrograms.nextState countdownPorts
(workState regs W rows candidate saved predecessor fuel fresh [] [])).regs
W rows candidate saved predecessor (CounterPrograms.ripple false fuel) fresh [] [] := by
let a := workState regs W rows candidate saved predecessor fuel fresh [] []
have hs : (CounterForPrograms.nextState countdownPorts a).stacks =
Function.update a.stacks 19 (CounterPrograms.ripple false fuel) := by
funext i
by_cases hc : i = 19
Β· subst i
rw [Function.update_self]
exact CounterForPrograms.nextState_counter countdownPorts a
rw [Function.update_of_ne hc]
by_cases he : i = 20
Β· subst i
exact CounterForPrograms.nextState_scratch countdownPorts a
exact (CounterForPrograms.nextState_outside countdownPorts a i hc he).symm
apply congrArg (StructuredProgram.State.mk _)
exact hs.trans (workState_fuel_update _ W rows candidate saved predecessor fuel _ fresh).symm
theorem body_scan_eval (W : List Bool) (rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (i : β„•) (hi : i < candidate.val)
(fuel : List Bool) (regs : Fin 16 β†’ Bool) :
βˆƒ nb ≀ bodyBound W (naturalKey W rows h candidate) rows h B, βˆƒ rb,
Eval body nb
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h i) fuel
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate i)) [] [])
(workState rb W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h (i + 1)) fuel
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate (i + 1))) [] []) := by
have hil : i < 2 ^ h := hi.trans candidate.isLt
have hinext : i + 1 < 2 ^ h := by have := candidate.isLt; omega
let current := ExecCNFTupleEnumeration.fixedBits h i
let saved := naturalKey W rows h candidate
let fresh := !(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate i)
obtain ⟨nb, hnb, rb, hb⟩ := body_eval regs W rows
(ExecCNFTupleEnumeration.fixedBits h candidate) saved current fuel fresh
(by simpa [current] using hlen)
have hbound : nb ≀ bodyBound W saved rows h B :=
hnb.trans (bodyCost_le W current saved rows h B (by simp [current]) hlen hwidth)
have hword : CounterPrograms.ripple true current =
ExecCNFTupleEnumeration.fixedBits h (i + 1) :=
Counters.increment_word (⟨i, hil⟩ : Fin (2 ^ h)) hinext
have hfresh : (fresh && !decide (signatureValue W current rows = saved)) =
!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate (i + 1)) := by
simp [AttainedSignatures.duplicateScan, fresh, saved, current, naturalKey, Bool.not_or]
rfl
rw [hword, hfresh] at hb
exact ⟨nb, hbound, rb, hb⟩
inductive ScanInvariant (W : List Bool) (rows : List (List (List Bool))) (h : β„•)
(candidate : Fin (2 ^ h)) (i : β„•) (a : Data) : Prop where
| intro (fuel : List Bool) (hlen : fuel.length = h)
(hvalue : i + CounterPrograms.value fuel = candidate.val)
(he : a = workState a.regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h i) fuel
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate i)) [] []) :
ScanInvariant W rows h candidate i a
theorem scan_spec (W : List Bool) (rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B) (candidate : Fin (2 ^ h)) :
βˆ€ i a, ScanInvariant W rows h candidate i a β†’ a.stacks countdownPorts.scratch = [] β†’
0 < CounterPrograms.value (a.stacks countdownPorts.counter) β†’
βˆƒ nb e, nb ≀ bodyBound W (naturalKey W rows h candidate) rows h B ∧ Eval body nb a e ∧
e.stacks countdownPorts.counter = a.stacks countdownPorts.counter ∧
e.stacks countdownPorts.scratch = [] ∧
ScanInvariant W rows h candidate (i + 1) (CounterForPrograms.nextState countdownPorts e) := by
intro i a hI _ hp
obtain ⟨fuel, hl, hv, he⟩ := hI
have hcounter : a.stacks countdownPorts.counter = fuel :=
congrArg (fun d : Data => d.stacks countdownPorts.counter) he
have hp' : 0 < CounterPrograms.value fuel := by simpa only [hcounter] using hp
have hi : i < candidate.val := by omega
obtain ⟨nb, hnb, rb, hb⟩ := body_scan_eval W rows h B hlen hwidth candidate i hi fuel a.regs
let e := workState rb W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h (i + 1)) fuel
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate (i + 1))) [] []
refine ⟨nb, e, hnb, ?_, ?_, rfl, ?_⟩
Β· exact Eq.mpr (congrArg (fun d : Data => Eval body nb d e) he) hb
Β· exact hcounter.symm
· refine ⟨CounterPrograms.ripple false fuel,
(CounterPrograms.decrement_length hp').trans hl, ?_, ?_⟩
Β· have hd := CounterPrograms.value_decrement hp'
omega
Β· exact nextState_workState rb W rows _ _ _ fuel _
theorem visits_exists (W : List Bool) (rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (remaining : β„•)
(i : β„•) (fuel : List Bool) (regs : Fin 16 β†’ Bool)
(htotal : i + remaining = candidate.val) (hl : fuel.length = h)
(hv : CounterPrograms.value fuel = remaining)
(hf : regs 15 = !(CounterPrograms.isZero fuel)) :
βˆƒ costs n out,
CounterForPrograms.Visits countdownPorts body costs n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h i) fuel
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate i)) [] [])
(workState out W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h candidate)
(List.replicate h false)
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []) ∧
costs.sum ≀ remaining * bodyBound W (naturalKey W rows h candidate) rows h B := by
let a := workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h i) fuel
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate i)) [] []
have hI : ScanInvariant W rows h candidate i a := ⟨fuel, hl, by omega, rfl⟩
obtain ⟨costs, n, e, hrun, hIe, _, hcost⟩ := CounterLoops.visits_exists_next
countdownPorts body (ScanInvariant W rows h candidate)
(fun _ => bodyBound W (naturalKey W rows h candidate) rows h B)
(scan_spec W rows h B hlen hwidth candidate) remaining i a hv rfl hf hI
obtain ⟨finalFuel, hfl, hfv, he⟩ := hIe
have hzero : CounterPrograms.value finalFuel = 0 := by omega
have hz : finalFuel = List.replicate h false :=
ExecCNFTupleEnumeration.word_eq_of_length_value (by simpa using hfl) (by simpa using hzero)
rw [htotal, hz] at he
rw [he] at hrun
rw [CounterForPrograms.budgetSum_const] at hcost
exact ⟨costs, n, e.regs, hrun, hcost⟩
theorem for_eval (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) :
βˆƒ n ≀ candidate.val *
(bodyBound W (naturalKey W rows h candidate) rows h B + 16 * h + 16) + 8 * h + 8,
βˆƒ out, Eval (CounterForPrograms.forCmd countdownPorts body) n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
(ExecCNFTupleEnumeration.fixedBits h candidate) true [] [])
(workState out W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h candidate)
(List.replicate h false)
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []) := by
let a := workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
(ExecCNFTupleEnumeration.fixedBits h candidate) true [] []
let t := CounterForPrograms.testResult countdownPorts a
have ht : t = workState t.regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
(ExecCNFTupleEnumeration.fixedBits h candidate) true [] [] := by
apply congrArg (StructuredProgram.State.mk t.regs)
exact CounterForPrograms.testResult_stacks countdownPorts a rfl
have hf : t.regs 15 = !(CounterPrograms.isZero (ExecCNFTupleEnumeration.fixedBits h candidate)) :=
CounterForPrograms.testResult_flag countdownPorts a
obtain ⟨costs, ns, out, hv, hc⟩ := visits_exists W rows h B hlen hwidth candidate candidate.val
0 (ExecCNFTupleEnumeration.fixedBits h candidate) t.regs (by omega) (by simp)
(ExecCNFTupleEnumeration.fixedBits_value h candidate candidate.isLt) hf
simp only [AttainedSignatures.duplicateScan, Bool.not_false] at hv
rw [← ht] at hv
have htest := CounterForPrograms.test_eval countdownPorts a (by rfl)
have hrun := htest.seq hv.eval
obtain ⟨hcount, _, _, htime⟩ := hv.bounds
have hlen' : (t.stacks countdownPorts.counter).length = h := by
rw [ht]
simp [workState, countdownPorts]
have hval : CounterPrograms.value (t.stacks countdownPorts.counter) = candidate.val := by
rw [ht]
exact ExecCNFTupleEnumeration.fixedBits_value h candidate candidate.isLt
rw [hval] at hcount
rw [hlen', hcount] at htime
refine ⟨_, ?_, out, hrun⟩
change 8 * (ExecCNFTupleEnumeration.fixedBits h candidate).length + 7 + ns ≀ _
rw [ExecCNFTupleEnumeration.fixedBits_length]
nlinarith
/-- All live arrays and all four retained words are included. -/
theorem workState_stackBits (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (candidate saved predecessor fuel : List Bool)
(fresh : Bool) :
stackBits (workState regs W rows candidate saved predecessor fuel fresh [] []).stacks =
W.length + (words (rows.map words)).length + candidate.length + saved.length +
predecessor.length + fuel.length + 1 := by
simp [stackBits, workState, Fin.sum_univ_succ, Nat.add_assoc]
def boundaryBound (W saved : List Bool) (rows : List (List (List Bool))) (h : β„•) : β„• :=
W.length + (words (rows.map words)).length + saved.length + 3 * h + 1
theorem boundary_stackBits (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h c i : β„•) (fuel : List Bool)
(hf : fuel.length = h) (fresh : Bool) :
stackBits (workState regs W rows (ExecCNFTupleEnumeration.fixedBits h c)
(naturalKey W rows h c) (ExecCNFTupleEnumeration.fixedBits h i) fuel fresh [] []).stacks =
boundaryBound W (naturalKey W rows h c) rows h := by
rw [workState_stackBits]
simp only [ExecCNFTupleEnumeration.fixedBits_length, hf, boundaryBound]
omega
inductive Boundary (W : List Bool) (rows : List (List (List Bool))) (h c phase : β„•)
(a : Data) : Prop where
| intro (i : β„•) (fuel : List Bool) (fresh : Bool) (hi : i ≀ c)
(hv : i + CounterPrograms.value fuel = c + phase) (hl : fuel.length = h)
(he : a = workState a.regs W rows (ExecCNFTupleEnumeration.fixedBits h c)
(naturalKey W rows h c) (ExecCNFTupleEnumeration.fixedBits h i) fuel fresh [] []) :
Boundary W rows h c phase a
theorem Boundary.stackBits {W : List Bool} {rows : List (List (List Bool))}
{h c phase : β„•} {a : Data} (ha : Boundary W rows h c phase a) :
stackBits a.stacks = boundaryBound W (naturalKey W rows h c) rows h := by
obtain ⟨i, fuel, fresh, _, _, hf, he⟩ := ha
rw [he]
exact boundary_stackBits a.regs W rows h c i fuel hf fresh
theorem boundary_after_body (W : List Bool) (rows : List (List (List Bool)))
(h B : β„•) (hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B) (candidate : Fin (2 ^ h))
{a b : Data} {n : β„•} (hb : Eval body n a b)
(ha : Boundary W rows h candidate 0 a)
(hp : 0 < CounterPrograms.value (a.stacks countdownPorts.counter)) :
Boundary W rows h candidate 1 b ∧
n ≀ bodyBound W (naturalKey W rows h candidate) rows h B := by
obtain ⟨i, fuel, fresh, hi, hv, hl, he⟩ := ha
have hp' : 0 < CounterPrograms.value fuel := by
rw [he] at hp
exact hp
have hil : i < 2 ^ h := lt_of_le_of_lt hi candidate.isLt
have hn : i + 1 < 2 ^ h := by have := candidate.isLt; omega
obtain ⟨nb, hnb, rb, hb'⟩ := body_eval a.regs W rows
(ExecCNFTupleEnumeration.fixedBits h candidate) (naturalKey W rows h candidate)
(ExecCNFTupleEnumeration.fixedBits h i) fuel fresh (by simpa using hlen)
have hb0 := hb
rw [he] at hb0
have hn' := StreamResources.eval_steps_eq 0 0 hb0 hb'
have hf := hb0.final_unique 0 hb'
have hw := Counters.increment_word (⟨i, hil⟩ : Fin (2 ^ h)) hn
constructor
· refine ⟨i + 1, fuel,
fresh && !decide (naturalKey W rows h i = naturalKey W rows h candidate),
by omega, by omega, hl, ?_⟩
rw [hf]
exact congrArg (fun w => workState rb W rows
(ExecCNFTupleEnumeration.fixedBits h candidate) (naturalKey W rows h candidate)
w fuel (fresh && !decide (naturalKey W rows h i = naturalKey W rows h candidate)) [] []) hw
Β· rw [hn']
exact hnb.trans (bodyCost_le W _ _ rows h B (by simp) hlen hwidth)
theorem boundary_next (W : List Bool) (rows : List (List (List Bool)))
(h c : β„•) {a : Data} (ha : Boundary W rows h c 1 a)
(hp : 0 < CounterPrograms.value (a.stacks countdownPorts.counter)) :
Boundary W rows h c 0 (CounterForPrograms.nextState countdownPorts a) := by
obtain ⟨i, fuel, fresh, hi, hv, hl, he⟩ := ha
have hp' : 0 < CounterPrograms.value fuel := by
rw [he] at hp
exact hp
have hd := CounterPrograms.value_decrement hp'
refine ⟨i, CounterPrograms.ripple false fuel, fresh, hi, by omega,
(CounterPrograms.decrement_length hp').trans hl, ?_⟩
rw [he]
exact nextState_workState a.regs W rows _ _ _ fuel fresh
/-- The exponential count of visits does not occur in this all-prefix bound. -/
theorem visits_stack_bound (W : List Bool) (rows : List (List (List Bool)))
(h B : β„•) (hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B) (candidate : Fin (2 ^ h))
{costs : List β„•} {n : β„•} {a b : Data}
(hv : CounterForPrograms.Visits countdownPorts body costs n a b)
(ha : Boundary W rows h candidate 0 a) :
βˆ€ prefixSteps intermediate, prefixSteps ≀ n β†’
Trace (compiled (CounterForPrograms.loopCmd countdownPorts body) 19 19)
prefixSteps (a.withPC (some (compiled
(CounterForPrograms.loopCmd countdownPorts body) 19 19).entry)) intermediate β†’
βˆ€ i, (intermediate.stacks i).length ≀
max (boundaryBound W (naturalKey W rows h candidate) rows h +
bodyBound W (naturalKey W rows h candidate) rows h B)
(40 * boundaryBound W (naturalKey W rows h candidate) rows h + 16) := by
let base := boundaryBound W (naturalKey W rows h candidate) rows h
let callbackBudget := bodyBound W (naturalKey W rows h candidate) rows h B
have hbound (phase : β„•) (a : Data) (ha : Boundary W rows h candidate phase a)
(i : Fin 24) : (a.stacks i).length ≀ base := by
have hs := Finset.single_le_sum (fun j _ => Nat.zero_le ((a.stacks j).length))
(Finset.mem_univ i)
change (a.stacks i).length ≀ stackBits a.stacks at hs
rw [ha.stackBits] at hs
exact hs
have hcallback : βˆ€ a b nb, Eval body nb a b β†’ Boundary W rows h candidate 0 a β†’
a.regs (countdownPorts.register 2) = true β†’
0 < CounterPrograms.value (a.stacks countdownPorts.counter) β†’
Boundary W rows h candidate 1 b ∧
βˆ€ prefixSteps intermediate, prefixSteps ≀ nb β†’
Trace (compiled body 19 19) prefixSteps
(a.withPC (some (compiled body 19 19).entry)) intermediate β†’
βˆ€ i, (intermediate.stacks i).length ≀ base + callbackBudget := by
intro a b nb hb ha _ hp
obtain ⟨hfinal, htime⟩ := boundary_after_body W rows h B hlen hwidth candidate hb ha hp
refine ⟨hfinal, ?_⟩
intro prefixSteps intermediate hle ht i
apply StructuredSpace.local_stack_bound (compiled body 19 19)
(steps := callbackBudget) (initialBound := base) _ prefixSteps intermediate
(hle.trans htime) ht i
exact le_of_eq ha.stackBits
have ht := Enumerators.countdown_visits_stack_bound countdownPorts body
(Boundary W rows h candidate 0) (Boundary W rows h candidate 1)
base (base + callbackBudget) (hbound 0) (hbound 1) hcallback
(fun a ha hp => boundary_next W rows h candidate ha hp) hv ha
intro prefixSteps intermediate hle htrace i
have hi := ht prefixSteps intermediate hle htrace i
exact hi.trans (by dsimp [base, callbackBudget]; omega)
def loopSpace (W : List Bool) (rows : List (List (List Bool))) (h B c : β„•) : β„• :=
max (boundaryBound W (naturalKey W rows h c) rows h +
bodyBound W (naturalKey W rows h c) rows h B)
(40 * boundaryBound W (naturalKey W rows h c) rows h + 16)
/-- Every prefix of the actual counted rescan, including test and halt. -/
theorem for_stack_bound (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) {n : β„•} {finish : Data}
(heval : Eval (CounterForPrograms.forCmd countdownPorts body) n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
(ExecCNFTupleEnumeration.fixedBits h candidate) true [] []) finish) :
βˆ€ prefixSteps intermediate, prefixSteps ≀ n + 1 β†’
Trace (compiled (CounterForPrograms.forCmd countdownPorts body) 19 19)
prefixSteps ((workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
(ExecCNFTupleEnumeration.fixedBits h candidate) true [] []).withPC
(some (compiled (CounterForPrograms.forCmd countdownPorts body) 19 19).entry))
intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀ loopSpace W rows h B candidate := by
let a := workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
(ExecCNFTupleEnumeration.fixedBits h candidate) true [] []
let t := CounterForPrograms.testResult countdownPorts a
have ht : t = workState t.regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
(ExecCNFTupleEnumeration.fixedBits h candidate) true [] [] := by
apply congrArg (StructuredProgram.State.mk t.regs)
exact CounterForPrograms.testResult_stacks countdownPorts a rfl
have hf : t.regs 15 = !(CounterPrograms.isZero (ExecCNFTupleEnumeration.fixedBits h candidate)) :=
CounterForPrograms.testResult_flag countdownPorts a
obtain ⟨costs, ns, out, hv, _⟩ := visits_exists W rows h B hlen hwidth candidate candidate.val
0 (ExecCNFTupleEnumeration.fixedBits h candidate) t.regs (by omega) (by simp)
(ExecCNFTupleEnumeration.fixedBits_value h candidate candidate.isLt) hf
simp only [AttainedSignatures.duplicateScan, Bool.not_false] at hv
rw [← ht] at hv
have hI : Boundary W rows h candidate 0 t := by
refine ⟨0, ExecCNFTupleEnumeration.fixedBits h candidate, true, by omega, ?_, by simp, ht⟩
rw [ExecCNFTupleEnumeration.fixedBits_value h candidate candidate.isLt]
omega
have hloop := visits_stack_bound W rows h B hlen hwidth candidate hv hI
have hinitial : βˆ€ i, (a.stacks i).length ≀
boundaryBound W (naturalKey W rows h candidate) rows h := by
intro i
have hs := Finset.single_le_sum (fun j _ => Nat.zero_le ((a.stacks j).length))
(Finset.mem_univ i)
change (a.stacks i).length ≀ stackBits a.stacks at hs
have hb := boundary_stackBits regs W rows h candidate 0
(ExecCNFTupleEnumeration.fixedBits h candidate) (by simp) true
rw [hb] at hs
exact hs
have hb := Enumerators.countdown_for_stack_bound countdownPorts body (by rfl)
hinitial hv hloop
have htest := CounterForPrograms.test_eval countdownPorts a (by rfl)
have hsame := StreamResources.eval_steps_eq 19 19 heval (htest.seq hv.eval)
intro prefixSteps intermediate hle htrace i
have hle' : prefixSteps ≀ 8 * (a.stacks countdownPorts.counter).length + 7 + ns + 1 := by
rw [hsame] at hle
exact hle
have hi := hb prefixSteps intermediate hle' htrace i
exact hi.trans (by unfold loopSpace; omega)
def setup : Code := .seq (copy 16 19) (storeFresh true)
def cleanup : Code := .seq (clear 18) (.call (StackRoutines.transferProgram 19 18 0 1))
/-- The candidate word supplies the binary predecessor count. Its width is h;
the final zero countdown word resets the predecessor without a family table. -/
def command : Code := .seq setup
(.seq (CounterForPrograms.forCmd countdownPorts body) cleanup)
theorem setup_eval (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (candidate saved predecessor : List Bool) (fresh : Bool) :
βˆƒ out, Eval setup (7 * candidate.length + 12)
(workState regs W rows candidate saved predecessor [] fresh [] [])
(workState out W rows candidate saved predecessor candidate true [] []) := by
let a := workState regs W rows candidate saved predecessor [] fresh [] []
let r₁ := readRegs regs 0 1 none
have hcopy : Eval (copy 16 19) (7 * candidate.length + 5) a
(workState r₁ W rows candidate saved predecessor candidate fresh [] []) := by
have ht := copy_eval 16 19 a (by decide) (by decide) (by decide) rfl
have he : (⟨readRegs a.regs 0 1 none, StackRoutines.copyStacks 16 19 a.stacks⟩ : Data) =
workState r₁ W rows candidate saved predecessor candidate fresh [] [] := by
apply congrArg (StructuredProgram.State.mk r₁)
funext i
fin_cases i <;> simp [a, workState, StackRoutines.copyStacks]
rw [he] at ht
exact ht
obtain ⟨rβ‚‚, hf⟩ := storeFresh_eval true r₁ W rows candidate saved predecessor candidate fresh [] []
exact ⟨rβ‚‚, by simpa only [setup] using hcopy.seq hf⟩
theorem cleanup_eval (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (candidate saved : List Bool) (h : β„•) (answer : Bool) :
βˆƒ out, Eval cleanup (2 * candidate.length + 3 * h + 6)
(workState regs W rows candidate saved candidate (List.replicate h false) answer [] [])
(workState out W rows candidate saved (List.replicate h false) [] answer [] []) := by
let r₁ := readRegs regs 0 1 none
have hclear : Eval (clear 18) (2 * candidate.length + 3)
(workState regs W rows candidate saved candidate (List.replicate h false) answer [] [])
(workState r₁ W rows candidate saved [] (List.replicate h false) answer [] []) := by
have ht := clear_eval 18
(workState regs W rows candidate saved candidate (List.replicate h false) answer [] [])
have he : StackRoutines.clearStacks 18
(workState regs W rows candidate saved candidate (List.replicate h false) answer [] []).stacks =
(workState r₁ W rows candidate saved [] (List.replicate h false) answer [] []).stacks := by
funext i
fin_cases i <;> simp [workState, StackRoutines.clearStacks]
rw [he] at ht
exact ht
let b := workState r₁ W rows candidate saved [] (List.replicate h false) answer [] []
let rβ‚‚ := readRegs r₁ 0 1 none
have htransfer : Eval (.call (StackRoutines.transferProgram 19 18 0 1)) (3 * h + 3) b
(workState rβ‚‚ W rows candidate saved (List.replicate h false) [] answer [] []) := by
have ht : Eval (.call (StackRoutines.transferProgram 19 18 0 1))
(3 * (b.stacks 19).length + 3) b
⟨readRegs b.regs 0 1 none, StackRoutines.transferStacks 19 18 b.stacks⟩ :=
.call _ (ListRegisterProgram.transfer_run (19 : Fin 24) 18
(0 : Fin 16) 1 (by decide) (by decide) b.regs b.stacks)
have he : StackRoutines.transferStacks 19 18 b.stacks =
(workState rβ‚‚ W rows candidate saved (List.replicate h false) [] answer [] []).stacks := by
funext i
fin_cases i <;> simp [b, workState, StackRoutines.transferStacks]
rw [he] at ht
have htarget :
(⟨readRegs b.regs 0 1 none,
(workState rβ‚‚ W rows candidate saved (List.replicate h false) [] answer [] []).stacks⟩ :
Data) =
workState rβ‚‚ W rows candidate saved (List.replicate h false) [] answer [] [] := by
apply congrArgβ‚‚ StructuredProgram.State.mk <;> rfl
rw [htarget] at ht
change Eval (.call (StackRoutines.transferProgram 19 18 0 1))
(3 * (List.replicate h false).length + 3) b _ at ht
simpa only [List.length_replicate] using ht
refine ⟨rβ‚‚, ?_⟩
convert hclear.seq htransfer using 1
Β· rfl
Β· omega
theorem command_eval (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (fresh : Bool) :
βˆƒ n ≀ candidate.val *
(bodyBound W (naturalKey W rows h candidate) rows h B + 16 * h + 16) + 20 * h + 26,
βˆƒ out, Eval command n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
[] fresh [] [])
(workState out W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (List.replicate h false) []
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []) := by
obtain ⟨r₁, hs⟩ := setup_eval regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0) fresh
obtain ⟨nf, hnf, rf, hf⟩ := for_eval r₁ W rows h B hlen hwidth candidate
obtain ⟨rβ‚‚, hc⟩ := cleanup_eval rf W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) h
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate))
refine ⟨_, ?_, rβ‚‚, hs.seq (hf.seq hc)⟩
simp only [ExecCNFTupleEnumeration.fixedBits_length]
omega
theorem sequence_stack_bound {first second : Code} {n m bound : β„•} {a b c : Data}
(hf : Eval first n a b) (hs : Eval second m b c)
(hfirst : βˆ€ p q, p ≀ n β†’ Trace (compiled first 19 19) p
(a.withPC (some (compiled first 19 19).entry)) q β†’ βˆ€ i, (q.stacks i).length ≀ bound)
(hsecond : βˆ€ p q, p ≀ m β†’ Trace (compiled second 19 19) p
(b.withPC (some (compiled second 19 19).entry)) q β†’ βˆ€ i, (q.stacks i).length ≀ bound) :
βˆ€ p q, p ≀ n + m β†’ Trace (compiled (.seq first second) 19 19) p
(a.withPC (some (compiled (.seq first second) 19 19).entry)) q β†’
βˆ€ i, (q.stacks i).length ≀ bound :=
StructuredSpace.sequence_stack_bound hf hs 19 19 hfirst hsecond
(compiled (.seq first second) 19 19) (siteAddress _) (exitAddress _)
(compiled_site _ 19 19)
theorem control_space_le (W : List Bool) (rows : List (List (List Bool))) (h B c t : β„•)
(ht : t ≀ 7 * h + 12) :
boundaryBound W (naturalKey W rows h c) rows h + t ≀ loopSpace W rows h B c := by
unfold loopSpace boundaryBound
omega
theorem command_stack_bound (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (fresh : Bool) {n : β„•} {finish : Data}
(heval : Eval command n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
[] fresh [] []) finish) :
βˆ€ p q, p ≀ n + 1 β†’ Trace (compiled command 19 19) p
((workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
[] fresh [] []).withPC (some (compiled command 19 19).entry)) q β†’
βˆ€ i, (q.stacks i).length ≀ loopSpace W rows h B candidate := by
obtain ⟨r₁, hs⟩ := setup_eval regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0) fresh
obtain ⟨nf, _, rf, hf⟩ := for_eval r₁ W rows h B hlen hwidth candidate
obtain ⟨rβ‚‚, hc⟩ := cleanup_eval rf W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) h
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate))
simp only [ExecCNFTupleEnumeration.fixedBits_length] at hs hc
let base := boundaryBound W (naturalKey W rows h candidate) rows h
have hsetup : βˆ€ p q, p ≀ 7 * h + 12 β†’ Trace (compiled setup 19 19) p
((workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
[] fresh [] []).withPC (some (compiled setup 19 19).entry)) q β†’
βˆ€ i, (q.stacks i).length ≀ loopSpace W rows h B candidate := by
intro p q hp hq i
have hb : stackBits (workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
[] fresh [] []).stacks ≀ base := by
rw [workState_stackBits]
simp only [ExecCNFTupleEnumeration.fixedBits_length, List.length_nil]
dsimp [base, boundaryBound]
omega
have hh := StructuredSpace.local_stack_bound (compiled setup 19 19)
(a := (workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
[] fresh [] []).withPC (some (compiled setup 19 19).entry))
(steps := 7 * h + 12) hb p q hp hq i
exact hh.trans (control_space_le W rows h B candidate _ le_rfl)
have hcleanup : βˆ€ p q, p ≀ 5 * h + 6 β†’ Trace (compiled cleanup 19 19) p
((workState rf W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h candidate)
(List.replicate h false)
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []).withPC
(some (compiled cleanup 19 19).entry)) q β†’
βˆ€ i, (q.stacks i).length ≀ loopSpace W rows h B candidate := by
intro p q hp hq i
have hb := boundary_stackBits rf W rows h candidate candidate
(List.replicate h false) (by simp)
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate))
have hh := StructuredSpace.local_stack_bound (compiled cleanup 19 19)
(a := (workState rf W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (ExecCNFTupleEnumeration.fixedBits h candidate)
(List.replicate h false)
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []).withPC
(some (compiled cleanup 19 19).entry))
(steps := 5 * h + 6) (le_of_eq hb) p q hp hq i
exact hh.trans (control_space_le W rows h B candidate _ (by omega))
have hloop := for_stack_bound r₁ W rows h B hlen hwidth candidate hf
have hcleanupCost : 2 * h + 3 * h + 6 = 5 * h + 6 := by omega
rw [hcleanupCost] at hc
have htail := sequence_stack_bound hf hc
(fun p q hp hq i => hloop p q (by omega) hq i) hcleanup
have hwhole := sequence_stack_bound hs (hf.seq hc) hsetup htail
have hhalt := StructuredSpace.compiled_halt_stack_bound (hs.seq (hf.seq hc)) 19 19 hwhole
have htime := StreamResources.eval_steps_eq 19 19 heval (hs.seq (hf.seq hc))
intro p q hp hq i
rw [htime] at hp
exact hhalt p q hp hq i
/-- Compute the candidate's whole key once and retain it through the rescan. -/
def prepareKey : Code := .seq (copy 16 12) (.seq keyCall
(.seq (copy 14 17) (.seq (clear 12) (clear 14))))
theorem prepareKey_eval (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (candidate predecessor : List Bool) (fresh : Bool)
(hlen : βˆ€ row ∈ rows, row.length = candidate.length) :
βˆƒ n ≀ cost W candidate rows + 9 * candidate.length +
9 * (signatureValue W candidate rows).length + 17, βˆƒ out,
Eval prepareKey n (workState regs W rows candidate [] predecessor [] fresh [] [])
(workState out W rows candidate (signatureValue W candidate rows)
predecessor [] fresh [] []) := by
let a := workState regs W rows candidate [] predecessor [] fresh [] []
let r₁ := readRegs regs 0 1 none
have hcopy : Eval (copy 16 12) (7 * candidate.length + 5) a
(workState r₁ W rows candidate [] predecessor [] fresh candidate []) := by
have ht := copy_eval 16 12 a (by decide) (by decide) (by decide) rfl
have he : (⟨readRegs a.regs 0 1 none, StackRoutines.copyStacks 16 12 a.stacks⟩ : Data) =
workState r₁ W rows candidate [] predecessor [] fresh candidate [] := by
apply congrArg (StructuredProgram.State.mk r₁)
funext i
fin_cases i <;> simp [a, workState, StackRoutines.copyStacks]
rw [he] at ht
exact ht
obtain ⟨nk, hnk, rk, hk⟩ := keyCall_eval r₁ W rows candidate [] predecessor []
fresh candidate [] hlen
simp only [List.append_nil] at hk
let key := signatureValue W candidate rows
let rβ‚‚ := readRegs rk 0 1 none
have hsave : Eval (copy 14 17) (7 * key.length + 5)
(workState rk W rows candidate [] predecessor [] fresh candidate key)
(workState rβ‚‚ W rows candidate key predecessor [] fresh candidate key) := by
have ht := copy_eval 14 17
(workState rk W rows candidate [] predecessor [] fresh candidate key)
(by decide) (by decide) (by decide) rfl
have he : StackRoutines.copyStacks 14 17
(workState rk W rows candidate [] predecessor [] fresh candidate key).stacks =
(workState rβ‚‚ W rows candidate key predecessor [] fresh candidate key).stacks := by
funext i
fin_cases i <;> simp [workState, StackRoutines.copyStacks]
rw [he] at ht
exact ht
let r₃ := readRegs rβ‚‚ 0 1 none
have hbits : Eval (clear 12) (2 * candidate.length + 3)
(workState rβ‚‚ W rows candidate key predecessor [] fresh candidate key)
(workState r₃ W rows candidate key predecessor [] fresh [] key) := by
have ht := clear_eval 12 (workState rβ‚‚ W rows candidate key predecessor [] fresh candidate key)
have he : StackRoutines.clearStacks 12
(workState rβ‚‚ W rows candidate key predecessor [] fresh candidate key).stacks =
(workState r₃ W rows candidate key predecessor [] fresh [] key).stacks := by
funext i
fin_cases i <;> simp [workState, StackRoutines.clearStacks]
rw [he] at ht
exact ht
let rβ‚„ := readRegs r₃ 0 1 none
have hkey : Eval (clear 14) (2 * key.length + 3)
(workState r₃ W rows candidate key predecessor [] fresh [] key)
(workState rβ‚„ W rows candidate key predecessor [] fresh [] []) := by
have ht := clear_eval 14 (workState r₃ W rows candidate key predecessor [] fresh [] key)
have he : StackRoutines.clearStacks 14
(workState r₃ W rows candidate key predecessor [] fresh [] key).stacks =
(workState rβ‚„ W rows candidate key predecessor [] fresh [] []).stacks := by
funext i
fin_cases i <;> simp [workState, StackRoutines.clearStacks]
rw [he] at ht
exact ht
refine ⟨_, ?_, rβ‚„, hcopy.seq (hk.seq (hsave.seq (hbits.seq hkey)))⟩
dsimp [key] at *
omega
def candidateCommand : Code := .seq prepareKey command
theorem candidateCommand_eval (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (fresh : Bool) :
βˆƒ n ≀ cost W (ExecCNFTupleEnumeration.fixedBits h candidate) rows +
9 * (naturalKey W rows h candidate).length +
candidate.val * (bodyBound W (naturalKey W rows h candidate) rows h B + 16 * h + 16) +
29 * h + 43, βˆƒ out,
Eval candidateCommand n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] [])
(workState out W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (List.replicate h false) []
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []) := by
obtain ⟨np, hnp, rp, hp⟩ := prepareKey_eval regs W rows
(ExecCNFTupleEnumeration.fixedBits h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
fresh (by simpa using hlen)
obtain ⟨nc, hnc, rc, hc⟩ := command_eval rp W rows h B hlen hwidth candidate fresh
refine ⟨np + nc, ?_, rc, hp.seq hc⟩
simp only [ExecCNFTupleEnumeration.fixedBits_length] at hnp
change np ≀ cost W (ExecCNFTupleEnumeration.fixedBits h candidate) rows +
9 * h + 9 * (naturalKey W rows h candidate).length + 17 at hnp
omega
def candidateProgram := compiled candidateCommand 16 22
theorem candidate_stack_bound (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (fresh : Bool) {n : β„•} {finish : Data}
(heval : Eval candidateCommand n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] []) finish) :
βˆ€ p q, p ≀ n + 1 β†’ Trace candidateProgram p
((workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] []).withPC
(some candidateProgram.entry)) q β†’
βˆ€ i, (q.stacks i).length ≀ loopSpace W rows h B candidate := by
obtain ⟨np, hnp, rp, hp⟩ := prepareKey_eval regs W rows
(ExecCNFTupleEnumeration.fixedBits h candidate) (ExecCNFTupleEnumeration.fixedBits h 0)
fresh (by simpa using hlen)
obtain ⟨nc, _, rc, hc⟩ := command_eval rp W rows h B hlen hwidth candidate fresh
let base := boundaryBound W (naturalKey W rows h candidate) rows h
have htime : np ≀ bodyBound W (naturalKey W rows h candidate) rows h B := by
have hs := cost_le W (ExecCNFTupleEnumeration.fixedBits h candidate) rows B
(by simpa using hlen) hwidth
simp only [ExecCNFTupleEnumeration.fixedBits_length] at hs hnp
unfold bodyBound naturalKey
omega
have hprepare : βˆ€ p q, p ≀ np β†’ Trace (compiled prepareKey 19 19) p
((workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] []).withPC
(some (compiled prepareKey 19 19).entry)) q β†’
βˆ€ i, (q.stacks i).length ≀ loopSpace W rows h B candidate := by
intro p q hp' hq i
have hb : stackBits (workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] []).stacks ≀ base := by
rw [workState_stackBits]
simp only [ExecCNFTupleEnumeration.fixedBits_length, List.length_nil]
dsimp [base, boundaryBound]
omega
have hh := StructuredSpace.local_stack_bound (compiled prepareKey 19 19)
(a := (workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] []).withPC
(some (compiled prepareKey 19 19).entry))
(steps := bodyBound W (naturalKey W rows h candidate) rows h B)
hb p q (hp'.trans htime) hq i
exact hh.trans (Nat.le_max_left _ _)
have hscan := command_stack_bound rp W rows h B hlen hwidth candidate fresh hc
have hwhole := sequence_stack_bound hp hc hprepare
(fun p q hp' hq i => hscan p q (by omega) hq i)
have hconverted := StructuredSpace.command_stack_bound (hp.seq hc) 19 19 hwhole
candidateProgram (siteAddress candidateCommand) (exitAddress candidateCommand)
(compiled_site candidateCommand 16 22)
have hhalt := StructuredSpace.compiled_halt_stack_bound (hp.seq hc) 16 22 hconverted
have hn := StreamResources.eval_steps_eq 16 22 heval (hp.seq hc)
intro p q hp' hq i
rw [hn] at hp'
exact hhalt p q hp' hq i
theorem naturalKey_eq_iff {h d : β„•} (W : List Bool)
(c : Fin d β†’ Fin h β†’ List Bool) (A : Fin d β†’ Fin h β†’ β„€)
(hc : βˆ€ j i, (BinaryArithmetic.toNat (c j i) : β„€) =
(BinaryArithmetic.toNat W : β„€) + A j i) (i j : Fin (2 ^ h)) :
naturalKey W (List.ofFn (fun row => List.ofFn (c row))) h i =
naturalKey W (List.ofFn (fun row => List.ofFn (c row))) h j ↔
AttainedSignatures.integerKey A i = AttainedSignatures.integerKey A j := by
unfold naturalKey
rw [← AttainedSignatures.assignment_word h i, ← AttainedSignatures.assignment_word h j]
exact key_eq_iff W c A _ _ hc
/-- Lexicographic comparison with coordinate priority hβˆ’1,...,0: compare
higher coordinates first, and use the low bit only when their tails agree. -/
inductive ReverseLex : List Bool β†’ List Bool β†’ Prop where
| higher {a b : Bool} {xs ys : List Bool} (h : ReverseLex xs ys) :
ReverseLex (a :: xs) (b :: ys)
| low (xs : List Bool) : ReverseLex (false :: xs) (true :: xs)
theorem reverseLex_iff_value_lt (xs ys : List Bool) (hlen : xs.length = ys.length) :
ReverseLex xs ys ↔ CounterPrograms.value xs < CounterPrograms.value ys := by
induction xs generalizing ys with
| nil =>
cases ys with
| nil => constructor <;> intro h <;> cases h
| cons b ys => simp at hlen
| cons a xs ih =>
cases ys with
| nil => simp at hlen
| cons b ys =>
have hl : xs.length = ys.length := by simpa using hlen
constructor
Β· intro h
cases h with
| higher h =>
have hv := (ih ys hl).mp h
cases a <;> cases b <;> simp only [CounterPrograms.value, Bool.toNat_false, Bool.toNat_true] <;> omega
| low => simp [CounterPrograms.value]
Β· intro hv
by_cases ht : CounterPrograms.value xs < CounterPrograms.value ys
Β· exact .higher ((ih ys hl).mpr ht)
have he : CounterPrograms.value xs = CounterPrograms.value ys := by
cases a <;> cases b <;> simp only [CounterPrograms.value, Bool.toNat_false, Bool.toNat_true] at hv <;> omega
have hw := ExecCNFTupleEnumeration.word_eq_of_length_value hl he
subst ys
cases a <;> cases b
Β· simp [CounterPrograms.value] at hv
Β· exact .low xs
Β· simp [CounterPrograms.value] at hv
Β· simp [CounterPrograms.value] at hv
theorem fixedBits_lex_iff (h : β„•) (i j : Fin (2 ^ h)) :
ReverseLex (ExecCNFTupleEnumeration.fixedBits h i)
(ExecCNFTupleEnumeration.fixedBits h j) ↔ i < j := by
rw [reverseLex_iff_value_lt _ _ (by simp),
ExecCNFTupleEnumeration.fixedBits_value h i i.isLt,
ExecCNFTupleEnumeration.fixedBits_value h j j.isLt]
rfl
theorem actual_increment_lex (h : β„•) (i : Fin (2 ^ h)) (hi : i.val + 1 < 2 ^ h) :
ReverseLex (ExecCNFTupleEnumeration.fixedBits h i)
(CounterPrograms.ripple true (ExecCNFTupleEnumeration.fixedBits h i)) := by
have hw : CounterPrograms.ripple true (ExecCNFTupleEnumeration.fixedBits h i) =
ExecCNFTupleEnumeration.fixedBits h (i.val + 1) := Counters.increment_word i hi
rw [hw]
exact (fixedBits_lex_iff h i ⟨i.val + 1, hi⟩).mpr (show i.val < i.val + 1 from Nat.lt_succ_self _)
/-- A uniform polynomial local budget, before the exponential scan counts. -/
def polynomialBudget (W : List Bool) (rows : List (List (List Bool))) (h B : β„•) : β„• :=
14 * (words (rows.map words)).length +
2 * (rows.length * (signatureRowBound W h B + 2)) +
38 * keyBound W h rows.length B + 62 * h + 129
theorem naturalKey_length_le (W : List Bool) (rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B) (c : β„•) :
(naturalKey W rows h c).length ≀ keyBound W h rows.length B := by
have ht := signatureValue_length_bound W (ExecCNFTupleEnumeration.fixedBits h c) rows B
(by simpa using hlen) hwidth
simpa only [naturalKey, keyBound,
ExecCNFTupleEnumeration.fixedBits_length] using ht
theorem candidate_time_uniform (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (fresh : Bool) :
βˆƒ n ≀ (candidate.val + 1) * polynomialBudget W rows h B, βˆƒ out,
Eval candidateCommand n
(workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] [])
(workState out W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (List.replicate h false) []
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []) := by
obtain ⟨n, hn, out, ht⟩ := candidateCommand_eval regs W rows h B hlen hwidth candidate fresh
have hs := cost_le W (ExecCNFTupleEnumeration.fixedBits h candidate) rows B
(by simpa using hlen) hwidth
simp only [ExecCNFTupleEnumeration.fixedBits_length] at hs
have hk := naturalKey_length_le W rows h B hlen hwidth candidate
have hbody : bodyBound W (naturalKey W rows h candidate) rows h B + 16 * h + 16 ≀
polynomialBudget W rows h B := by unfold bodyBound polynomialBudget; omega
have hsetup : cost W (ExecCNFTupleEnumeration.fixedBits h candidate) rows +
9 * (naturalKey W rows h candidate).length + 29 * h + 43 ≀
polynomialBudget W rows h B := by unfold polynomialBudget; omega
refine ⟨n, ?_, out, ht⟩
calc
n ≀ candidate.val * polynomialBudget W rows h B + polynomialBudget W rows h B := by
have hm := Nat.mul_le_mul_left candidate.val hbody
omega
_ = (candidate.val + 1) * polynomialBudget W rows h B := by rw [Nat.add_mul, one_mul]
theorem complete_candidate_budget (W : List Bool) (rows : List (List (List Bool))) (h B : β„•) :
(βˆ‘ candidate : Fin (2 ^ h),
(candidate.val + 1) * polynomialBudget W rows h B) ≀
polynomialBudget W rows h B * 4 ^ h := by
calc
_ ≀ βˆ‘ _candidate : Fin (2 ^ h), (2 ^ h) * polynomialBudget W rows h B := by
exact Finset.sum_le_sum (fun candidate _ => Nat.mul_le_mul_right _ (by omega))
_ = polynomialBudget W rows h B * 4 ^ h := by
simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul]
rw [← Nat.mul_assoc, ← mul_pow]
ring
def spaceBudget (W : List Bool) (rows : List (List (List Bool))) (h B : β„•) : β„• :=
40 * (W.length + (words (rows.map words)).length + keyBound W h rows.length B + 3 * h + 1) +
polynomialBudget W rows h B + 16
theorem loopSpace_le (W : List Bool) (rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B) (c : β„•) :
loopSpace W rows h B c ≀ spaceBudget W rows h B := by
have hk := naturalKey_length_le W rows h B hlen hwidth c
have hb : bodyBound W (naturalKey W rows h c) rows h B ≀ polynomialBudget W rows h B := by
unfold bodyBound polynomialBudget
omega
unfold loopSpace spaceBudget boundaryBound
omega
/-- One fixed actual candidate machine returns the canonical full key and
freshness bit, with time and every-prefix storage bounds on that same run. -/
theorem candidate_run (regs : Fin 16 β†’ Bool) (W : List Bool)
(rows : List (List (List Bool))) (h B : β„•)
(hlen : βˆ€ row ∈ rows, row.length = h)
(hwidth : βˆ€ row ∈ rows, βˆ€ x ∈ row, x.length ≀ B)
(candidate : Fin (2 ^ h)) (fresh : Bool) :
βˆƒ n ≀ (candidate.val + 1) * polynomialBudget W rows h B + 1, βˆƒ out,
Trace candidateProgram n
((workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] []).withPC
(some candidateProgram.entry))
((workState out W rows (ExecCNFTupleEnumeration.fixedBits h candidate)
(naturalKey W rows h candidate) (List.replicate h false) []
(!(AttainedSignatures.duplicateScan (naturalKey W rows h) candidate candidate)) [] []).withPC none) ∧
βˆ€ prefixSteps intermediate, prefixSteps ≀ n β†’
Trace candidateProgram prefixSteps
((workState regs W rows (ExecCNFTupleEnumeration.fixedBits h candidate) []
(ExecCNFTupleEnumeration.fixedBits h 0) [] fresh [] []).withPC
(some candidateProgram.entry)) intermediate β†’
βˆ€ i, (intermediate.stacks i).length ≀ spaceBudget W rows h B := by
obtain ⟨steps, hsteps, out, he⟩ := candidate_time_uniform regs W rows h B hlen hwidth candidate fresh
refine ⟨steps + 1, Nat.add_le_add_right hsteps 1, out, compiled_halt_trace he 16 22, ?_⟩
intro prefixSteps intermediate hle ht i
exact (candidate_stack_bound regs W rows h B hlen hwidth candidate fresh he
prefixSteps intermediate hle ht i).trans (loopSpace_le W rows h B hlen hwidth candidate)
/-- Include each concrete compiled invocation's halt in the full generator
charge; no emitted-key count multiplies the raw candidate scan. -/
theorem complete_candidate_run_budget (W : List Bool) (rows : List (List (List Bool)))
(h B : β„•) :
(βˆ‘ candidate : Fin (2 ^ h),
((candidate.val + 1) * polynomialBudget W rows h B + 1)) ≀
(polynomialBudget W rows h B + 1) * 4 ^ h := by
calc
_ ≀ βˆ‘ _candidate : Fin (2 ^ h), (2 ^ h) * (polynomialBudget W rows h B + 1) := by
apply Finset.sum_le_sum
intro candidate _
have hi : candidate.val + 1 ≀ 2 ^ h := by have := candidate.isLt; omega
have hm := Nat.mul_le_mul_right (polynomialBudget W rows h B) hi
have hp : 1 ≀ 2 ^ h := Nat.one_le_pow' h 1
nlinarith
_ = _ := by
simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul]
rw [← Nat.mul_assoc, ← mul_pow]
ring
/-- Both concrete resource expressions have one fixed degree, independent of
head width, matrix dimensions, coefficient values and the candidate cursor. -/
theorem budgets_le_monomial (W : List Bool) (rows : List (List (List Bool))) (h B S : β„•)
(hS : 1 ≀ S) (hW : W.length ≀ S) (hd : rows.length ≀ S) (hh : h ≀ S)
(hB : B ≀ S) (hM : (words (rows.map words)).length ≀ S) :
polynomialBudget W rows h B ≀ 100000 * S ^ 6 ∧
spaceBudget W rows h B ≀ 100000 * S ^ 6 := by
let width := 1 + S * (S + 1 + S)
let row := (S + 1) * (200 * (width + 2) ^ 2 + 34) + 9 * width + 22
let signature := 9 * (S * (2 * S + 1)) + 7 + (7 * S + 5) + 2 +
(row + 1) + 8 * (width + 1) + 8
let key := S * (2 * (width + 1) + 1)
let total := 14 * S + 2 * (S * (signature + 2)) + 38 * key + 62 * S + 129
have hp : polynomialBudget W rows h B ≀ total := by
dsimp [polynomialBudget, keyBound, signatureRowBound, rowCostBound, widthBound,
total, signature, row, key, width]
gcongr
have hk : keyBound W h rows.length B ≀ key := by
dsimp [keyBound, widthBound, key, width]
gcongr
have hs : spaceBudget W rows h B ≀ 40 * (S + S + key + 3 * S + 1) + total + 16 := by
unfold spaceBudget
gcongr
have h0 : 1 ≀ S ^ 6 := one_le_powβ‚€ hS
have h1 : S ≀ S ^ 6 := Nat.le_self_pow (by decide) _
have h2 : S ^ 2 ≀ S ^ 6 := Nat.pow_le_pow_right hS (by decide)
have h3 : S ^ 3 ≀ S ^ 6 := Nat.pow_le_pow_right hS (by decide)
have h4 : S ^ 4 ≀ S ^ 6 := Nat.pow_le_pow_right hS (by decide)
have h5 : S ^ 5 ≀ S ^ 6 := Nat.pow_le_pow_right hS (by decide)
dsimp [total, signature, row, key, width] at hp hs
constructor <;> nlinarith
end Rescan
end OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.AttainedSignatureProgram