roseking's picture
Add lean source and binary files (part 61)
ea76aac verified
Raw History Blame Contribute Delete
28.3 kB
import Mathlib
import OAI.Computability.MaxCut.Machines.MachineSubroutine
import OAI.Computability.MaxCut.Encoding.Encoding2
namespace OAI
namespace MaxCutGames.Reduction.SourceEquationLookup
open Turing
open MaxCutGames.Foundations.Complexity
open MaxCutGames.Foundations.Hastad
open MachineComposition SourceMachine SourceEncoding
open SourceClauseLookup (discardFieldsTrace)
inductive Tape
| source | index | work | scratch | field (slot : Fin 4)
deriving DecidableEq, Fintype
inductive Label
| copyOut | copyBack | headerFirst | headerSecond | guard
| skip (slot : Fin 4)
| read (slot : Fin 5 × Bool)
deriving DecidableEq, Fintype
def fieldDestination (r : Nat) : Tape := .field ⟨min r 3, by omega⟩
def readStart (r : Nat) : Label := .read (SourceFieldArray.boundedIndex 4 r, false)
def readLoop (r : Nat) : Label := .read (SourceFieldArray.boundedIndex 4 r, true)
def skipLabel (r : Nat) : Label := if h : r < 4 then .skip ⟨r, h⟩ else .guard
def headerLabel (r : Nat) : Label :=
if r = 0 then .headerFirst else if r = 1 then .headerSecond else .guard
variable {σ : Type}
/-- Eight binary stacks and fixed finite control. Every loop scans one actual bit
or tests one actual counter bit. The arbitrary ambient state is preserved. -/
def program : Label → TM2.Stmt (fun _ : Tape => Bool) Label (σ × Option Bool)
| .copyOut => Reduction.MachineTransfer.loopAt .source .scratch id false
.copyOut (some .copyBack)
| .copyBack => MachineCopy.forkLoop .scratch .source .work false
.copyBack (some .headerFirst)
| .headerFirst => MachineLookup.discard .work .headerFirst .headerSecond
| .headerSecond => MachineLookup.discard .work .headerSecond .guard
| .guard => MachineUnaryCounter.guard .index (skipLabel 0) (readStart 0)
| .skip slot => MachineLookup.discard .work (.skip slot) (skipLabel (slot.val + 1))
| .read slot =>
if slot.1.val < 4 then
if slot.2 then
fieldLoop .work (fieldDestination slot.1.val) (.read (slot.1, true))
(some (readStart (slot.1.val + 1)))
else fieldStart (fieldDestination slot.1.val) (.read (slot.1, true))
else SourceFieldArray.finish .work none
theorem atSkip (r : Nat) (hr : r < 4) :
program (σ := σ) (skipLabel r) = MachineLookup.discard .work (skipLabel r) (skipLabel (r + 1)) := by
simp [skipLabel, hr, program]
theorem atReadStart (r : Nat) (hr : r < 4) :
program (σ := σ) (readStart r) = fieldStart (fieldDestination r) (readLoop r) := by
simp [program, readStart, readLoop, SourceFieldArray.boundedIndex_val hr.le, hr]
theorem atReadLoop (r : Nat) (hr : r < 4) :
program (σ := σ) (readLoop r) = fieldLoop .work (fieldDestination r) (readLoop r)
(some (readStart (r + 1))) := by
simp [program, readStart, readLoop, SourceFieldArray.boundedIndex_val hr.le, hr]
theorem atReadDone : program (σ := σ) (readStart 4) = SourceFieldArray.finish .work none := by
simp [program, readStart, SourceFieldArray.boundedIndex]
/-- The two header fields are physically consumed from the work copy. -/
theorem discardHeadersTrace (base : Tape → List Bool) (variableCount equations : Nat)
(suffix : List Bool) (hinput : base .work = encodeWords [variableCount, equations] ++ suffix) (ambient : σ) :
(advance (TM2.step program))^[variableCount + equations + 2]
(some ⟨some .headerFirst, (ambient, none), base⟩) =
some ⟨some .guard, (ambient, none), Function.update base .work suffix⟩ := by
have ht := discardFieldsTrace .work headerLabel program base 0 [variableCount, equations]
suffix (by
intro r hr
have he : r = 0 ∨ r = 1 := by simp at hr; omega
rcases he with rfl | rfl <;> rfl) hinput ambient
simpa [encodeWords_length, headerLabel] using ht
/-- The fixed skip body consumes four complete unary fields and returns to the
same counter guard, preserving the counter and every other tape. -/
theorem discardFourTrace (base : Tape → List Bool) (values : List Nat)
(hlen : values.length = 4) (suffix : List Bool)
(hinput : base .work = encodeWords values ++ suffix) (ambient : σ) :
(advance (TM2.step program))^[(encodeWords values).length]
(some ⟨some (skipLabel 0), (ambient, none), base⟩) =
some ⟨some .guard, (ambient, none), Function.update base .work suffix⟩ := by
have ht := discardFieldsTrace .work skipLabel program base 0 values suffix
(by intro r hr; simpa only [Nat.zero_add] using atSkip r (by omega)) hinput ambient
simpa [hlen, skipLabel] using ht
/-- Read the selected four fields using the already checked scanner. -/
theorem readFourTrace (base : Tape → List Bool) (values : List Nat)
(hlen : values.length = 4) (suffix : List Bool)
(hinput : base .work = encodeWords values ++ suffix) (ambient : σ) :
(advance (TM2.step program))^[(encodeWords values).length + 5]
(some ⟨some (readStart 0), (ambient, none), base⟩) =
some ⟨none, (ambient, none),
SourceFieldArray.sequenceTapes .work fieldDestination base 0 values suffix⟩ := by
have ht := SourceFieldArray.sequenceTrace .work fieldDestination readStart readLoop none
program base 0 values suffix (by intro r hr; simp [fieldDestination])
(by intro r hr; simpa only [Nat.zero_add] using atReadStart r (by omega))
(by intro r hr; simpa only [Nat.zero_add] using atReadLoop r (by omega))
(by simpa [hlen] using atReadDone) hinput ambient none
have htime : values.sum + 2 * values.length + 1 = (encodeWords values).length + 5 := by
rw [encodeWords_length, hlen]
simpa only [Nat.zero_add, htime] using ht
/-- Normal form for the changing counter and consumed work-copy prefix. -/
def scanTapes (base : Tape → List Bool) (index : Nat)
(indexSuffix input : List Bool) : Tape → List Bool :=
MachineUnaryCounter.counterTapes .index (Function.update base .work input) index indexSuffix
@[simp] theorem scanTapes_work (base : Tape → List Bool) (i : Nat)
(indexSuffix input : List Bool) : scanTapes base i indexSuffix input .work = input := by
simp [scanTapes, MachineUnaryCounter.counterTapes]
@[simp] theorem scanTapes_index (base : Tape → List Bool) (i : Nat)
(indexSuffix input : List Bool) :
scanTapes base i indexSuffix input .index = encodeWord i ++ indexSuffix := by
simp [scanTapes, MachineUnaryCounter.counterTapes]
theorem scanTapes_other (base : Tape → List Bool) (i : Nat)
(indexSuffix input : List Bool) (p : Tape) (hi : p ≠ .index) (hw : p ≠ .work) :
scanTapes base i indexSuffix input p = base p := by
simp [scanTapes, MachineUnaryCounter.counterTapes, hi, hw]
theorem update_scanTapes_work (base : Tape → List Bool) (i : Nat)
(indexSuffix input replacement : List Bool) :
Function.update (scanTapes base i indexSuffix input) .work replacement =
scanTapes base i indexSuffix replacement := by
funext p
cases p <;> simp [scanTapes, MachineUnaryCounter.counterTapes]
/-- Each equation-index bit causes one genuine guard transition and exactly four
field-discard loops. The final zero guard enters the field reader. -/
theorem skipEquationsTrace {n : Nat} (base : Tape → List Bool)
(prior : List (CloneGap.Equation (Fin n))) (indexSuffix suffix : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step program))^[(encodeWords (prior.flatMap equationWords)).length + prior.length + 1]
(some ⟨some .guard, (ambient, register), scanTapes base prior.length indexSuffix
(encodeWords (prior.flatMap equationWords) ++ suffix)⟩) =
some ⟨some (readStart 0), (ambient, none), scanTapes base 0 indexSuffix suffix⟩ := by
induction prior generalizing register with
| nil =>
simpa only [List.flatMap_nil, List.length_nil, encodeWords, List.nil_append,
Nat.zero_add, Function.iterate_one, advance_some, scanTapes] using
MachineUnaryCounter.guardStep_zero .index .guard (skipLabel 0) (readStart 0)
program rfl (Function.update base .work suffix) indexSuffix ambient register
| cons equation prior ih =>
have hencoding : encodeWords ((equation :: prior).flatMap equationWords) ++ suffix =
encodeWords (equationWords equation) ++ (encodeWords (prior.flatMap equationWords) ++ suffix) := by
simp only [List.flatMap_cons, encodeWords_append, List.append_assoc]
have htime : (encodeWords ((equation :: prior).flatMap equationWords)).length +
(equation :: prior).length + 1 =
((encodeWords (prior.flatMap equationWords)).length + prior.length + 1 +
(encodeWords (equationWords equation)).length) + 1 := by
simp only [List.flatMap_cons, encodeWords_append, List.length_append, List.length_cons]
omega
rw [htime, Function.iterate_succ_apply]
change (advance (TM2.step program))^[
(encodeWords (prior.flatMap equationWords)).length + prior.length + 1 +
(encodeWords (equationWords equation)).length]
(TM2.step program ⟨some .guard, (ambient, register),
MachineUnaryCounter.counterTapes .index
(Function.update base .work (encodeWords ((equation :: prior).flatMap equationWords) ++ suffix))
(prior.length + 1) indexSuffix⟩) = _
rw [MachineUnaryCounter.guardStep_succ .index .guard (skipLabel 0) (readStart 0)
program rfl]
rw [Function.iterate_add_apply]
have hd := discardFourTrace
(scanTapes base prior.length indexSuffix
(encodeWords ((equation :: prior).flatMap equationWords) ++ suffix))
(equationWords equation) (equationWords_length equation)
(encodeWords (prior.flatMap equationWords) ++ suffix) (by
rw [scanTapes_work, hencoding]) ambient
change (advance (TM2.step program))^[
(encodeWords (prior.flatMap equationWords)).length + prior.length + 1]
((advance (TM2.step program))^[(encodeWords (equationWords equation)).length]
(some ⟨some (skipLabel 0), (ambient, none), scanTapes base prior.length indexSuffix
(encodeWords ((equation :: prior).flatMap equationWords) ++ suffix)⟩)) = _
rw [hd, update_scanTapes_work]
exact ih none
theorem fieldDestination_eq (j : Fin 4) : fieldDestination j.val = .field j := by
unfold fieldDestination
congr 1
apply Fin.ext
exact Nat.min_eq_left (by omega)
theorem fieldDestination_injective_below (r s : Nat) (hr : r < 4) (hs : s < 4)
(he : fieldDestination r = fieldDestination s) : r = s := by
have hv := congrArg (fun t : Tape => match t with | .field j => j.val | _ => 4) he
simpa [fieldDestination, Nat.min_eq_left (show r ≤ 3 by omega),
Nat.min_eq_left (show s ≤ 3 by omega)] using hv
theorem readFour_output_field (base : Tape → List Bool) (values : List Nat)
(hlen : values.length = 4) (suffix : List Bool) (j : Fin 4) :
SourceFieldArray.sequenceTapes .work fieldDestination base 0 values suffix (.field j) =
encodeWord (values[j.val]'(by omega)) ++ base (.field j) := by
have ht := SourceFieldArray.sequenceTapes_selected .work fieldDestination base 0 values
suffix j.val (by omega) (by intro r hr; simp [fieldDestination])
(by
intro r s hr hs he
apply fieldDestination_injective_below r s (by omega) (by omega)
simpa only [Nat.zero_add] using he)
simpa only [Nat.zero_add, fieldDestination_eq] using ht
theorem readFour_output_work (base : Tape → List Bool) (values : List Nat)
(_hlen : values.length = 4) (suffix : List Bool)
(hinput : base .work = encodeWords values ++ suffix) :
SourceFieldArray.sequenceTapes .work fieldDestination base 0 values suffix .work = suffix := by
apply SourceFieldArray.sequenceTapes_source _ _ _ _ _ _ _ hinput
intro r hr
simp [fieldDestination]
theorem readFour_output_frame (base : Tape → List Bool) (values : List Nat)
(suffix : List Bool) (p : Tape) (hwork : p ≠ .work)
(hfield : ∀ j : Fin 4, p ≠ .field j) :
SourceFieldArray.sequenceTapes .work fieldDestination base 0 values suffix p = base p := by
apply SourceFieldArray.sequenceTapes_other _ _ _ hwork
intro r hr
exact hfield _
def outputTapes {n : Nat} (base : Tape → List Bool) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) : Tape → List Bool :=
SourceFieldArray.sequenceTapes .work fieldDestination
(scanTapes base 0 indexSuffix (encodeWords (equationWords equation) ++ suffix))
0 (equationWords equation) suffix
def scanSteps {n : Nat} (variableCount declaredEquations : Nat)
(prior : List (CloneGap.Equation (Fin n))) (equation : CloneGap.Equation (Fin n)) : Nat :=
variableCount + declaredEquations + 2 +
(encodeWords (prior.flatMap equationWords)).length + prior.length + 1 +
(encodeWords (equationWords equation)).length + 5
theorem scanReadTrace {n : Nat} (base : Tape → List Bool) (variableCount declaredEquations : Nat)
(prior : List (CloneGap.Equation (Fin n))) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) (ambient : σ) :
(advance (TM2.step program))^[scanSteps variableCount declaredEquations prior equation]
(some ⟨some .headerFirst, (ambient, none), scanTapes base prior.length indexSuffix
(encodeWords [variableCount, declaredEquations] ++
(encodeWords (prior.flatMap equationWords) ++
(encodeWords (equationWords equation) ++ suffix)))⟩) =
some ⟨none, (ambient, none), outputTapes base equation indexSuffix suffix⟩ := by
have hh := discardHeadersTrace
(scanTapes base prior.length indexSuffix
(encodeWords [variableCount, declaredEquations] ++
(encodeWords (prior.flatMap equationWords) ++ (encodeWords (equationWords equation) ++ suffix))))
variableCount declaredEquations
(encodeWords (prior.flatMap equationWords) ++ (encodeWords (equationWords equation) ++ suffix))
(by simp) ambient
have hs := skipEquationsTrace base prior indexSuffix (encodeWords (equationWords equation) ++ suffix) ambient none
have hr := readFourTrace
(scanTapes base 0 indexSuffix (encodeWords (equationWords equation) ++ suffix))
(equationWords equation) (equationWords_length equation) suffix (by simp) ambient
rw [show scanSteps variableCount declaredEquations prior equation =
((encodeWords (equationWords equation)).length + 5 +
((encodeWords (prior.flatMap equationWords)).length + prior.length + 1)) +
(variableCount + declaredEquations + 2) by unfold scanSteps; omega]
rw [Function.iterate_add_apply, hh, update_scanTapes_work,
Function.iterate_add_apply, hs]
exact hr
def lookupSteps {n : Nat} (F : SourceEncoding.Input) (prior : List (CloneGap.Equation (Fin n)))
(equation : CloneGap.Equation (Fin n)) : Nat :=
2 * ((inputBits F).length + 1) + scanSteps F.«variables» F.equations.length prior equation
theorem lookupWithSplitTrace (F : SourceEncoding.Input) (prior : List (CloneGap.Equation (Fin F.«variables»)))
(equation : CloneGap.Equation (Fin F.«variables»)) (after : List (CloneGap.Equation (Fin F.«variables»)))
(hsplit : F.equations = prior ++ equation :: after)
(base : Tape → List Bool) (indexSuffix : List Bool)
(hsource : base .source = inputBits F)
(hindex : base .index = encodeWord prior.length ++ indexSuffix)
(hwork : base .work = []) (hscratch : base .scratch = [])
(ambient : σ) (register : Option Bool) :
(advance (TM2.step program))^[lookupSteps F prior equation]
(some ⟨some .copyOut, (ambient, register), base⟩) =
some ⟨none, (ambient, none),
outputTapes base equation indexSuffix (encodeWords (after.flatMap equationWords))⟩ := by
have hbits : inputBits F = encodeWords [F.«variables», F.equations.length] ++
(encodeWords (prior.flatMap equationWords) ++
(encodeWords (equationWords equation) ++ encodeWords (after.flatMap equationWords))) := by
simp only [inputBits, inputWords, encodeWords_append]
rw [hsplit]
simp only [List.flatMap_append, List.flatMap_cons, encodeWords_append]
have hc := MachineCopy.copyTrace .source .work .scratch (by decide) (by decide)
(by decide) false .copyOut .copyBack (some .headerFirst) program rfl rfl
base hscratch ambient register
have htapes : Function.update base .work (base .source ++ base .work) =
scanTapes base prior.length indexSuffix (inputBits F) := by
funext p
cases p <;> simp [scanTapes, MachineUnaryCounter.counterTapes, hsource, hindex, hwork]
rw [htapes, hsource] at hc
rw [lookupSteps, Nat.add_comm, Function.iterate_add_apply, hc, hbits]
exact scanReadTrace base F.«variables» F.equations.length prior equation indexSuffix
(encodeWords (after.flatMap equationWords)) ambient
theorem output_source {n : Nat} (base : Tape → List Bool) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) :
outputTapes base equation indexSuffix suffix .source = base .source := by
rw [outputTapes, readFour_output_frame _ _ _ .source (by decide) (by intro j; simp)]
exact scanTapes_other _ _ _ _ _ (by decide) (by decide)
theorem output_index {n : Nat} (base : Tape → List Bool) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) :
outputTapes base equation indexSuffix suffix .index = encodeWord 0 ++ indexSuffix := by
rw [outputTapes, readFour_output_frame _ _ _ .index (by decide) (by intro j; simp),
scanTapes_index]
theorem output_work {n : Nat} (base : Tape → List Bool) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) :
outputTapes base equation indexSuffix suffix .work = suffix := by
apply readFour_output_work _ _ (equationWords_length equation) _
simp
theorem output_field {n : Nat} (base : Tape → List Bool) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) (j : Fin 4) :
outputTapes base equation indexSuffix suffix (.field j) =
encodeWord ((equationWords equation)[j.val]'(by simp)) ++ base (.field j) := by
rw [outputTapes, readFour_output_field _ _ (equationWords_length equation)]
rw [scanTapes_other _ _ _ _ (.field j) (by simp) (by simp)]
theorem output_first {n : Nat} (base : Tape → List Bool)
(equation : CloneGap.Equation (Fin n)) (indexSuffix suffix : List Bool) :
outputTapes base equation indexSuffix suffix (.field 0) =
encodeWord equation.first.val ++ base (.field 0) := by
simpa [equationWords] using output_field base equation indexSuffix suffix (0 : Fin 4)
theorem output_second {n : Nat} (base : Tape → List Bool)
(equation : CloneGap.Equation (Fin n)) (indexSuffix suffix : List Bool) :
outputTapes base equation indexSuffix suffix (.field 1) =
encodeWord equation.second.val ++ base (.field 1) := by
simpa [equationWords] using output_field base equation indexSuffix suffix (1 : Fin 4)
theorem output_third {n : Nat} (base : Tape → List Bool)
(equation : CloneGap.Equation (Fin n)) (indexSuffix suffix : List Bool) :
outputTapes base equation indexSuffix suffix (.field 2) =
encodeWord equation.third.val ++ base (.field 2) := by
simpa [equationWords] using output_field base equation indexSuffix suffix (2 : Fin 4)
theorem output_rhs {n : Nat} (base : Tape → List Bool)
(equation : CloneGap.Equation (Fin n)) (indexSuffix suffix : List Bool) :
outputTapes base equation indexSuffix suffix (.field 3) =
encodeWord (if equation.rhs then 1 else 0) ++ base (.field 3) := by
simpa [equationWords] using output_field base equation indexSuffix suffix (3 : Fin 4)
theorem output_frame {n : Nat} (base : Tape → List Bool) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) (p : Tape) (hindex : p ≠ .index) (hwork : p ≠ .work)
(hfield : ∀ j : Fin 4, p ≠ .field j) :
outputTapes base equation indexSuffix suffix p = base p := by
rw [outputTapes, readFour_output_frame _ _ _ p hwork hfield]
exact scanTapes_other _ _ _ _ p hindex hwork
theorem equationWords_four {n : Nat} (equation : CloneGap.Equation (Fin n)) :
equationWords equation =
[equation.first.val, equation.second.val, equation.third.val,
if equation.rhs then 1 else 0] := rfl
def fieldWords (tapes : Tape → List Bool) : List (List Bool) :=
[tapes (.field 0), tapes (.field 1), tapes (.field 2), tapes (.field 3)]
theorem output_fieldWords {n : Nat} (base : Tape → List Bool) (equation : CloneGap.Equation (Fin n))
(indexSuffix suffix : List Bool) (hempty : ∀ j : Fin 4, base (.field j) = []) :
fieldWords (outputTapes base equation indexSuffix suffix) =
(equationWords equation).map encodeWord := by
simp [fieldWords, output_field, hempty, equationWords_four]
theorem lookupSteps_le_input (F : SourceEncoding.Input) (prior : List (CloneGap.Equation (Fin F.«variables»)))
(equation : CloneGap.Equation (Fin F.«variables»)) (after : List (CloneGap.Equation (Fin F.«variables»)))
(hsplit : F.equations = prior ++ equation :: after) :
lookupSteps F prior equation ≤ 4 * (inputBits F).length + 8 := by
have hlength : (inputBits F).length = F.«variables» + F.equations.length + 2 +
(encodeWords (prior.flatMap equationWords)).length +
(encodeWords (equationWords equation)).length +
(encodeWords (after.flatMap equationWords)).length := by
simp only [inputBits, inputWords, encodeWords_append, List.length_append,
encodeWords, encodeWord_length, List.length_nil]
rw [hsplit]
simp only [List.flatMap_append, List.flatMap_cons, encodeWords_append, List.length_append]
omega
have hp : prior.length ≤ F.equations.length := by
rw [hsplit, List.length_append, List.length_cons]
omega
unfold lookupSteps scanSteps
omega
noncomputable def timePolynomial : Polynomial Nat := Polynomial.C 4 * Polynomial.X + Polynomial.C 8
theorem timePolynomial_eval (L : Nat) : timePolynomial.eval L = 4 * L + 8 := by
simp [timePolynomial]
/-- A polynomial bound on the actual complete copy/skip/read execution. -/
def lookupWithSplitInTime (F : SourceEncoding.Input) (prior : List (CloneGap.Equation (Fin F.«variables»)))
(equation : CloneGap.Equation (Fin F.«variables»)) (after : List (CloneGap.Equation (Fin F.«variables»)))
(hsplit : F.equations = prior ++ equation :: after)
(base : Tape → List Bool) (indexSuffix : List Bool)
(hsource : base .source = inputBits F)
(hindex : base .index = encodeWord prior.length ++ indexSuffix)
(hwork : base .work = []) (hscratch : base .scratch = [])
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some .copyOut, (ambient, register), base⟩
(some ⟨none, (ambient, none),
outputTapes base equation indexSuffix (encodeWords (after.flatMap equationWords))⟩)
(timePolynomial.eval (inputBits F).length) where
steps := lookupSteps F prior equation
evals_in_steps := lookupWithSplitTrace F prior equation after hsplit base indexSuffix
hsource hindex hwork hscratch ambient register
steps_le_m := by
rw [timePolynomial_eval]
exact lookupSteps_le_input F prior equation after hsplit
def lookupInTime (F : SourceEncoding.Input) (i : Fin F.equations.length)
(base : Tape → List Bool) (indexSuffix : List Bool)
(hsource : base .source = inputBits F)
(hindex : base .index = encodeWord i.val ++ indexSuffix)
(hwork : base .work = []) (hscratch : base .scratch = [])
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some .copyOut, (ambient, register), base⟩
(some ⟨none, (ambient, none), outputTapes base F.equations[i.val] indexSuffix
(encodeWords ((F.equations.drop (i.val + 1)).flatMap equationWords))⟩)
(timePolynomial.eval (inputBits F).length) := by
have hp : (F.equations.take i.val).length = i.val := by
rw [List.length_take, Nat.min_eq_left i.isLt.le]
have hs : F.equations = F.equations.take i.val ++ F.equations[i.val] :: F.equations.drop (i.val + 1) := by
rw [List.getElem_cons_drop i.isLt, List.take_append_drop]
apply lookupWithSplitInTime F (F.equations.take i.val) F.equations[i.val]
(F.equations.drop (i.val + 1)) hs base indexSuffix hsource
(by simpa only [hp] using hindex) hwork hscratch ambient register
def machine : FinTM2 where
K := Tape
k₀ := .source
k₁ := .field 0
Γ _ := Bool
Λ := Label
main := .copyOut
σ := Unit × Option Bool
initialState := ((), none)
m := program (σ := Unit)
/-- The same proved run stated directly for the concrete finite machine. -/
def machineInTime (F : SourceEncoding.Input) (i : Fin F.equations.length)
(base : Tape → List Bool) (indexSuffix : List Bool)
(hsource : base .source = inputBits F)
(hindex : base .index = encodeWord i.val ++ indexSuffix)
(hwork : base .work = []) (hscratch : base .scratch = [])
(register : Option Bool) :
StateTransition.EvalsToInTime machine.step
⟨some .copyOut, ((), register), base⟩
(some ⟨none, ((), none), outputTapes base F.equations[i.val] indexSuffix
(encodeWords ((F.equations.drop (i.val + 1)).flatMap equationWords))⟩)
(timePolynomial.eval (inputBits F).length) := by
exact lookupInTime F i base indexSuffix hsource hindex hwork hscratch () register
/-- Same-tape placement into the caller's finite control. Choosing the ambient
type to be `τ × Unit` gives the shared state `(τ × Unit) × Option Bool` used by
the field-template machinery. The lookup's final transition enters `exit`. -/
noncomputable def placedLookupInTime {Λ' : Type}
(labels : Label → Λ') (exit : Option Λ')
(target : Λ' → TM2.Stmt (fun _ : Tape => Bool) Λ' (σ × Option Bool))
(atLabels : ∀ l, target (labels l) =
MachineSubroutine.statement labels exit (program (σ := σ) l))
(input : SourceEncoding.Input) (i : Fin input.equations.length)
(base : Tape → List Bool) (indexSuffix : List Bool)
(hsource : base .source = inputBits input)
(hindex : base .index = encodeWord i.val ++ indexSuffix)
(hwork : base .work = []) (hscratch : base .scratch = [])
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step target)
⟨some (labels .copyOut), (ambient, register), base⟩
(some ⟨exit, (ambient, none), outputTapes base input.equations[i.val] indexSuffix
(encodeWords ((input.equations.drop (i.val + 1)).flatMap equationWords))⟩)
(timePolynomial.eval (inputBits input).length) :=
MachineSubroutine.execution labels exit program target atLabels
(lookupInTime input i base indexSuffix hsource hindex hwork hscratch ambient register)
/-- An optional disjoint-stack placement preserves arbitrary extra tapes and
state. It reuses the same generic lookup and introduces no extra transitions. -/
noncomputable def embeddedLookupInTime {E Λextra τ : Type} [DecidableEq E]
(continuation : Option (Label ⊕ Λextra)) (extraState : τ)
(extraTapes : E → List Bool)
(extra : Λextra → TM2.Stmt
(MachineEmbedding.Alphabet (fun _ : Tape => Bool) (fun _ : E => Bool))
(Label ⊕ Λextra) ((σ × Option Bool) × τ))
(input : SourceEncoding.Input) (i : Fin input.equations.length)
(base : Tape → List Bool) (indexSuffix : List Bool)
(hsource : base .source = inputBits input)
(hindex : base .index = encodeWord i.val ++ indexSuffix)
(hwork : base .work = []) (hscratch : base .scratch = [])
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime
(TM2.step (MachineEmbedding.program continuation program extra))
(MachineEmbedding.configuration continuation extraState extraTapes
⟨some .copyOut, (ambient, register), base⟩)
(some (MachineEmbedding.configuration continuation extraState extraTapes
⟨none, (ambient, none), outputTapes base input.equations[i.val] indexSuffix
(encodeWords ((input.equations.drop (i.val + 1)).flatMap equationWords))⟩))
(timePolynomial.eval (inputBits input).length) :=
MachineComposition.embeddedExecution continuation extraState extraTapes program extra
(lookupInTime input i base indexSuffix hsource hindex hwork hscratch ambient register)
end MaxCutGames.Reduction.SourceEquationLookup
end OAI