openAI-math-dataset / lean /OAI /Computability /DirectedFeedback /Machines /MachinePreservingLookupClean.lean
roseking's picture
Add lean source and binary files (part 61)
ea76aac verified
Raw History Blame Contribute Delete
39.8 kB
import Mathlib
import OAI.Computability.DirectedFeedback.Machines.MachineStateEquiv
import OAI.Computability.DirectedFeedback.Machines.MachineCloudPadding
namespace OAI
namespace DFVSGames.Foundations.Complexity.MachinePreservingLookupClean
open Turing
open MachineComposition
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
inductive Label
| run (label : MachinePreservingLookup.Label)
| cleanup
deriving DecidableEq, Fintype
def statement (tape : Fin 5 → K) (labels : Label → Λ) (exit : Option Λ) :
Label → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)
| .run label => MachineSubroutine.statement (fun label => labels (.run label))
(some (labels .cleanup)) (MachinePreservingLookup.program tape label)
| .cleanup => MachineDrain.drain (tape 2) (labels .cleanup) exit
def program (tape : Fin 5 → K) :
Label → TM2.Stmt (Alphabet (K := K)) Label (σ × Option Bool) :=
statement tape id none
def steps (values : List Nat) (index : Nat) : Nat :=
(2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values index + 1) +
((encodeWords (values.drop (index + 1))).length + 1)
theorem remaining_length_le (values : List Nat) (index : Nat) :
(encodeWords (values.drop (index + 1))).length ≤ (encodeWords values).length := by
have h := congrArg (fun words : List Nat => (encodeWords words).length)
(List.take_append_drop (index + 1) values)
simp only [encodeWords_append, List.length_append] at h
omega
theorem steps_le (values : List Nat) (index value : Nat)
(selected : values[index]? = some value) :
steps values index ≤ 6 * (encodeWords values).length + 4 := by
have hc := MachinePreservingLookup.preservingLookup_steps_le values index value selected
have hd := remaining_length_le values index
unfold steps
omega
noncomputable def timePolynomial : Polynomial Nat := 6 * Polynomial.X + 4
theorem steps_le_timePolynomial (values : List Nat) (index value : Nat)
(selected : values[index]? = some value) :
steps values index ≤ timePolynomial.eval (encodeWords values).length := by
simpa only [timePolynomial, Polynomial.eval_add, Polynomial.eval_mul,
Polynomial.eval_ofNat, Polynomial.eval_X] using steps_le values index value selected
theorem traceAt (tape : Fin 5 → K) (distinct : Function.Injective tape)
(labels : Label → Λ) (exit : Option Λ)
(target : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(code : ∀ label, target (labels label) = statement tape labels exit label)
(base : K → List Bool) (values : List Nat)
(tableWord : base (tape 0) = encodeWords values)
(scratchEmpty : base (tape 4) = [])
(index value : Nat) (selected : values[index]? = some value)
(indexSuffix output : List Bool) (ambient : σ) (register : Option Bool) :
(advance (TM2.step target))^[steps values index]
(some ⟨some (labels (.run .copyFirst)), (ambient, register),
MachinePreservingLookup.initialTapes tape base index indexSuffix [] output⟩) =
some ⟨exit, (ambient, none),
MachinePreservingLookup.initialTapes tape base 0 indexSuffix []
(encodeWord value ++ output)⟩ := by
have hd (a b : Fin 5) (hne : a ≠ b) : tape a ≠ tape b :=
fun h => hne (distinct h)
have raw := MachinePreservingLookup.preservingLookupTrace tape distinct
MachinePreservingLookup.Label.copyFirst MachinePreservingLookup.Label.copySecond
MachinePreservingLookup.Label.lookup none (MachinePreservingLookup.program tape)
rfl rfl (fun _ => rfl) base values tableWord scratchEmpty index value selected
indexSuffix [] output ambient register
have run := MachineSubroutine.trace (fun label => labels (.run label))
(some (labels .cleanup)) (MachinePreservingLookup.program tape) target
(fun label => code (.run label))
(2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values index + 1)
_ _ raw
simp only [MachineSubroutine.configuration, MachineSubroutine.label] at run
have cleanup := MachineDrain.drainTrace (tape 2) (labels .cleanup) exit target
(code .cleanup)
(MachinePreservingLookup.finalTapes tape base values index value indexSuffix [] output)
(encodeWords (values.drop (index + 1))) ambient none
have word : MachinePreservingLookup.finalTapes tape base values index value
indexSuffix [] output (tape 2) = encodeWords (values.drop (index + 1)) := by
simp only [MachinePreservingLookup.finalTapes,
MachineLookup.tapes_source _ _ _ (hd 2 3 (by decide)), List.append_nil]
have start : Function.update
(MachinePreservingLookup.finalTapes tape base values index value indexSuffix [] output)
(tape 2) (encodeWords (values.drop (index + 1))) =
MachinePreservingLookup.finalTapes tape base values index value indexSuffix [] output := by
rw [← word, Function.update_eq_self]
have finish : Function.update
(MachinePreservingLookup.finalTapes tape base values index value indexSuffix [] output)
(tape 2) [] = MachinePreservingLookup.initialTapes tape base 0 indexSuffix []
(encodeWord value ++ output) := by
unfold MachinePreservingLookup.finalTapes MachinePreservingLookup.initialTapes
rw [MachineLookup.update_source _ _ _ (hd 2 3 (by decide))]
rw [start, finish] at cleanup
rw [show steps values index =
((encodeWords (values.drop (index + 1))).length + 1) +
(2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values index + 1) by
unfold steps
omega,
Function.iterate_add_apply, run]
exact cleanup
def inTimeAt (tape : Fin 5 → K) (distinct : Function.Injective tape)
(labels : Label → Λ) (exit : Option Λ)
(target : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(code : ∀ label, target (labels label) = statement tape labels exit label)
(base : K → List Bool) (values : List Nat)
(tableWord : base (tape 0) = encodeWords values)
(scratchEmpty : base (tape 4) = [])
(index value : Nat) (selected : values[index]? = some value)
(indexSuffix output : List Bool) (ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step target)
⟨some (labels (.run .copyFirst)), (ambient, register),
MachinePreservingLookup.initialTapes tape base index indexSuffix [] output⟩
(some ⟨exit, (ambient, none),
MachinePreservingLookup.initialTapes tape base 0 indexSuffix []
(encodeWord value ++ output)⟩)
(timePolynomial.eval (encodeWords values).length) where
steps := steps values index
evals_in_steps := traceAt tape distinct labels exit target code base values tableWord
scratchEmpty index value selected indexSuffix output ambient register
steps_le_m := steps_le_timePolynomial values index value selected
theorem initialTapes_eq (tape : Fin 5 → K) (base : K → List Bool)
(index : Nat) (indexSuffix : List Bool)
(counterWord : base (tape 1) = encodeWord index ++ indexSuffix)
(workEmpty : base (tape 2) = []) :
MachinePreservingLookup.initialTapes tape base index indexSuffix [] (base (tape 3)) =
base := by
simp only [MachinePreservingLookup.initialTapes, MachineLookup.tapes,
← counterWord, ← workEmpty, Function.update_eq_self]
theorem traceAt_fromTapes (tape : Fin 5 → K) (distinct : Function.Injective tape)
(labels : Label → Λ) (exit : Option Λ)
(target : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(code : ∀ label, target (labels label) = statement tape labels exit label)
(base : K → List Bool) (values : List Nat)
(tableWord : base (tape 0) = encodeWords values)
(scratchEmpty : base (tape 4) = [])
(index value : Nat) (selected : values[index]? = some value)
(indexSuffix : List Bool)
(counterWord : base (tape 1) = encodeWord index ++ indexSuffix)
(workEmpty : base (tape 2) = []) (ambient : σ) (register : Option Bool) :
(advance (TM2.step target))^[steps values index]
(some ⟨some (labels (.run .copyFirst)), (ambient, register), base⟩) =
some ⟨exit, (ambient, none),
MachinePreservingLookup.initialTapes tape base 0 indexSuffix []
(encodeWord value ++ base (tape 3))⟩ := by
have h := traceAt tape distinct labels exit target code base values tableWord scratchEmpty
index value selected indexSuffix (base (tape 3)) ambient register
rw [initialTapes_eq tape base index indexSuffix counterWord workEmpty] at h
exact h
def machine : FinTM2 where
K := Fin 5
k₀ := 0
k₁ := 3
Γ _ := Bool
Λ := Label
main := .run .copyFirst
σ := Unit × Option Bool
initialState := ((), none)
m := program id
def machineInTime (base : Fin 5 → List Bool) (values : List Nat)
(tableWord : base 0 = encodeWords values) (scratchEmpty : base 4 = [])
(index value : Nat) (selected : values[index]? = some value)
(indexSuffix output : List Bool) (register : Option Bool) :
StateTransition.EvalsToInTime machine.step
⟨some (.run .copyFirst), ((), register),
MachinePreservingLookup.initialTapes id base index indexSuffix [] output⟩
(some ⟨none, ((), none),
MachinePreservingLookup.initialTapes id base 0 indexSuffix []
(encodeWord value ++ output)⟩)
(timePolynomial.eval (encodeWords values).length) :=
inTimeAt id (fun _ _ h => h) id none (program id) (fun _ => rfl)
base values tableWord scratchEmpty index value selected indexSuffix output () register
end DFVSGames.Foundations.Complexity.MachinePreservingLookupClean
namespace DFVSGames.Foundations.Complexity.MachineAppendAt
open Turing
variable {K Λ σ β : Type} [DecidableEq K]
abbrev Alphabet (β : Type) (_ : K) := β
def appendTapes (source destination : K) (base : K → List β) : K → List β :=
Reduction.MachineTransfer.tapesAt source destination base []
(base destination ++ base source)
@[simp] theorem appendTapes_source (source destination : K)
(distinct : source ≠ destination) (base : K → List β) :
appendTapes source destination base source = [] := by
simp [appendTapes, distinct]
@[simp] theorem appendTapes_destination (source destination : K) (base : K → List β) :
appendTapes source destination base destination = base destination ++ base source := by
simp [appendTapes]
theorem appendTapes_other (source destination k : K)
(notSource : k ≠ source) (notDestination : k ≠ destination) (base : K → List β) :
appendTapes source destination base k = base k := by
simp [appendTapes, Reduction.MachineTransfer.tapesAt, notSource, notDestination]
theorem appendTapes_scratch (source destination scratch : K)
(sourceScratch : source ≠ scratch) (destinationScratch : destination ≠ scratch)
(base : K → List β) (scratchEmpty : base scratch = []) :
appendTapes source destination base scratch = [] := by
rw [appendTapes_other source destination scratch (Ne.symm sourceScratch)
(Ne.symm destinationScratch) base, scratchEmpty]
private def firstTapes_inline_MachineAppendAt (destination scratch : K) (base : K → List β) : K → List β :=
Reduction.MachineTransfer.tapesAt destination scratch base [] (base destination).reverse
private def secondTapes_inline_MachineAppendAt (source destination scratch : K) (base : K → List β) : K → List β :=
Reduction.MachineTransfer.tapesAt source scratch (firstTapes_inline_MachineAppendAt destination scratch base) []
((base source).reverse ++ (base destination).reverse)
private theorem finalTapes_eq_inline_MachineAppendAt (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch) (base : K → List β)
(scratchEmpty : base scratch = []) :
Reduction.MachineTransfer.tapesAt scratch destination
(secondTapes_inline_MachineAppendAt source destination scratch base) [] (base destination ++ base source) =
appendTapes source destination base := by
funext k
by_cases hs : k = source
· subst k
simp [appendTapes, secondTapes_inline_MachineAppendAt, firstTapes_inline_MachineAppendAt, Reduction.MachineTransfer.tapesAt,
sourceDestination, sourceScratch]
· by_cases hd : k = destination
· subst k
simp [appendTapes, Reduction.MachineTransfer.tapesAt]
· by_cases ht : k = scratch
· subst k
simp [appendTapes, Reduction.MachineTransfer.tapesAt, Ne.symm sourceScratch,
Ne.symm destinationScratch, scratchEmpty]
· simp [appendTapes, secondTapes_inline_MachineAppendAt, firstTapes_inline_MachineAppendAt, Reduction.MachineTransfer.tapesAt,
hs, hd, ht]
theorem appendTrace (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch)
(fallback : β) (firstLabel secondLabel thirdLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atFirst : program firstLabel = Reduction.MachineTransfer.loopAt destination scratch
id fallback firstLabel (some secondLabel))
(atSecond : program secondLabel = Reduction.MachineTransfer.loopAt source scratch
id fallback secondLabel (some thirdLabel))
(atThird : program thirdLabel = Reduction.MachineTransfer.loopAt scratch destination
id fallback thirdLabel exit)
(base : K → List β) (scratchEmpty : base scratch = [])
(ambient : σ) (register : Option β) :
(MachineComposition.advance (TM2.step program))^[
2 * ((base source).length + (base destination).length) + 3]
(some ⟨some firstLabel, (ambient, register), base⟩) =
some ⟨exit, (ambient, none), appendTapes source destination base⟩ := by
have first := Reduction.MachineTransfer.transferAt_fromTapes destination scratch
destinationScratch id fallback firstLabel (some secondLabel) program atFirst
base ambient register
change (MachineComposition.advance (TM2.step program))^[(base destination).length + 1]
(some ⟨some firstLabel, (ambient, register), base⟩) = _ at first
simp only [List.map_id, scratchEmpty, List.append_nil] at first
change (MachineComposition.advance (TM2.step program))^[(base destination).length + 1]
(some ⟨some firstLabel, (ambient, register), base⟩) =
some ⟨some secondLabel, (ambient, none), firstTapes_inline_MachineAppendAt destination scratch base⟩ at first
have firstSource : firstTapes_inline_MachineAppendAt destination scratch base source = base source := by
simp [firstTapes_inline_MachineAppendAt, Reduction.MachineTransfer.tapesAt, sourceDestination, sourceScratch]
have firstScratch : firstTapes_inline_MachineAppendAt destination scratch base scratch =
(base destination).reverse := by
simp [firstTapes_inline_MachineAppendAt]
have second := Reduction.MachineTransfer.transferAt_fromTapes source scratch
sourceScratch id fallback secondLabel (some thirdLabel) program atSecond
(firstTapes_inline_MachineAppendAt destination scratch base) ambient none
change (MachineComposition.advance (TM2.step program))^[
(firstTapes_inline_MachineAppendAt destination scratch base source).length + 1]
(some ⟨some secondLabel, (ambient, none), firstTapes_inline_MachineAppendAt destination scratch base⟩) = _
at second
rw [firstSource, firstScratch] at second
simp only [List.map_id] at second
change (MachineComposition.advance (TM2.step program))^[(base source).length + 1]
(some ⟨some secondLabel, (ambient, none), firstTapes_inline_MachineAppendAt destination scratch base⟩) =
some ⟨some thirdLabel, (ambient, none), secondTapes_inline_MachineAppendAt source destination scratch base⟩
at second
have secondScratch : secondTapes_inline_MachineAppendAt source destination scratch base scratch =
(base source).reverse ++ (base destination).reverse := by
simp [secondTapes_inline_MachineAppendAt]
have secondDestination : secondTapes_inline_MachineAppendAt source destination scratch base destination = [] := by
simp [secondTapes_inline_MachineAppendAt, firstTapes_inline_MachineAppendAt, Reduction.MachineTransfer.tapesAt,
Ne.symm sourceDestination, destinationScratch]
have third := Reduction.MachineTransfer.transferAt_fromTapes scratch destination
(Ne.symm destinationScratch) id fallback thirdLabel exit program atThird
(secondTapes_inline_MachineAppendAt source destination scratch base) ambient none
change (MachineComposition.advance (TM2.step program))^[
(secondTapes_inline_MachineAppendAt source destination scratch base scratch).length + 1]
(some ⟨some thirdLabel, (ambient, none), secondTapes_inline_MachineAppendAt source destination scratch base⟩) = _
at third
rw [secondScratch, secondDestination] at third
simp only [List.map_id, List.reverse_append, List.reverse_reverse, List.append_nil,
List.length_append, List.length_reverse] at third
rw [finalTapes_eq_inline_MachineAppendAt source destination scratch sourceDestination sourceScratch
destinationScratch base scratchEmpty] at third
rw [show 2 * ((base source).length + (base destination).length) + 3 =
((base source).length + (base destination).length + 1) +
(((base source).length + 1) + ((base destination).length + 1)) by omega]
rw [Function.iterate_add_apply]
rw [Function.iterate_add_apply (m := (base source).length + 1)
(n := (base destination).length + 1), first, second]
exact third
def appendInTime (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch)
(fallback : β) (firstLabel secondLabel thirdLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atFirst : program firstLabel = Reduction.MachineTransfer.loopAt destination scratch
id fallback firstLabel (some secondLabel))
(atSecond : program secondLabel = Reduction.MachineTransfer.loopAt source scratch
id fallback secondLabel (some thirdLabel))
(atThird : program thirdLabel = Reduction.MachineTransfer.loopAt scratch destination
id fallback thirdLabel exit)
(base : K → List β) (scratchEmpty : base scratch = [])
(ambient : σ) (register : Option β) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some firstLabel, (ambient, register), base⟩
(some ⟨exit, (ambient, none), appendTapes source destination base⟩)
(2 * ((base source).length + (base destination).length) + 3) where
steps := 2 * ((base source).length + (base destination).length) + 3
evals_in_steps := appendTrace source destination scratch sourceDestination sourceScratch
destinationScratch fallback firstLabel secondLabel thirdLabel exit program atFirst
atSecond atThird base scratchEmpty ambient register
steps_le_m := Nat.le_refl _
end DFVSGames.Foundations.Complexity.MachineAppendAt
namespace DFVSGames.Foundations.Complexity.MachineRegularTable
open Turing MachineComposition
namespace Lift
open MachineCloudPadding
variable {K K' Λ Λ' σ τ υ : Type}
def statement (tape : K → K') (labels : Λ → Λ') (exit : Option Λ')
(registers : (σ × τ) ≃ υ) (q : TM2.Stmt (fun _ : K => Bool) Λ σ) :
TM2.Stmt (fun _ : K' => Bool) Λ' υ :=
MachineStateEquiv.statement registers
(MachineStateFrame.frameStatement (Placement.statement tape labels exit q))
def configuration (view : K' → Option K) (labels : Λ → Λ') (exit : Option Λ')
(registers : (σ × τ) ≃ υ) (ambient : τ) (extra : K' → List Bool)
(c : TM2.Cfg (fun _ : K => Bool) Λ σ) : TM2.Cfg (fun _ : K' => Bool) Λ' υ :=
⟨Placement.label labels exit c.l, registers (c.var, ambient),
Placement.tapes view c.stk extra⟩
variable [DecidableEq K] [DecidableEq K']
theorem stepAux (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(q : TM2.Stmt (fun _ : K => Bool) Λ σ) (state : σ) (source : K → List Bool) :
TM2.stepAux (statement tape labels exit registers q) (registers (state, ambient))
(Placement.tapes view source extra) =
configuration view labels exit registers ambient extra (TM2.stepAux q state source) := by
rw [statement, MachineStateEquiv.stepAux_transport, MachineStateFrame.frame_stepAux,
Placement.stepAux_simulation tape view left right]
rfl
theorem step (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(source : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
(target : Λ' → TM2.Stmt (fun _ : K' => Bool) Λ' υ)
(code : ∀ l, target (labels l) = statement tape labels exit registers (source l))
(a b : TM2.Cfg (fun _ : K => Bool) Λ σ) (h : TM2.step source a = some b) :
TM2.step target (configuration view labels exit registers ambient extra a) =
some (configuration view labels exit registers ambient extra b) := by
cases a with
| mk label state sourceTapes =>
cases label with
| none => simp [TM2.step] at h
| some label =>
have hb : TM2.stepAux (source label) state sourceTapes = b := Option.some.inj h
rw [← hb]
change some (TM2.stepAux (target (labels label)) (registers (state, ambient))
(Placement.tapes view sourceTapes extra)) = _
rw [code, stepAux tape view left right]
theorem trace (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(source : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
(target : Λ' → TM2.Stmt (fun _ : K' => Bool) Λ' υ)
(code : ∀ l, target (labels l) = statement tape labels exit registers (source l))
(steps : Nat) (a b : TM2.Cfg (fun _ : K => Bool) Λ σ)
(run : (advance (TM2.step source))^[steps] (some a) = some b) :
(advance (TM2.step target))^[steps]
(some (configuration view labels exit registers ambient extra a)) =
some (configuration view labels exit registers ambient extra b) :=
liftSuccessfulTrace _ _ _
(step tape view left right labels exit registers ambient extra source target code) steps a b run
end Lift
end DFVSGames.Foundations.Complexity.MachineRegularTable
namespace DFVSGames.Foundations.Complexity.MachineCloudRank
open Turing MachineComposition MachineCloudCount
variable {σ : Type}
@[simp] theorem update_memory_spare (a b c d e f x : List Bool) :
Function.update (memory a b c d e f) .spare x = memory a b c d e x := by
funext k
cases k <;> rfl
inductive RankLabel
| queryFirst | querySecond | querySkip | tableFirst | tableSecond
| headerFirst | headerSecond | row | field | restore | done
| skip (i : Fin 4097)
deriving DecidableEq, Fintype
def rankSkip (i : Nat) : RankLabel :=
if h : i < 4097 then .skip ⟨i, h⟩ else .row
def rankProgram : RankLabel → TM2.Stmt Alphabet RankLabel (State σ)
| .queryFirst => Reduction.MachineTransfer.loopAt
.target .scratch id false .queryFirst (some .querySecond)
| .querySecond => MachineCopy.forkLoop
.scratch .target .spare false .querySecond (some .querySkip)
| .querySkip => MachineLookup.discard .spare .querySkip .tableFirst
| .tableFirst => Reduction.MachineTransfer.loopAt
.original .scratch id false .tableFirst (some .tableSecond)
| .tableSecond => MachineCopy.forkLoop
.scratch .original .work false .tableSecond (some .headerFirst)
| .headerFirst => MachineLookup.discard .work .headerFirst .headerSecond
| .headerSecond => MachineLookup.discard .work .headerSecond .row
| .row => MachineUnaryCounter.guard .spare .field .done
| .field => fieldLoop .field .restore
| .restore => Reduction.MachineTransfer.loopAt
.scratch .target id false .restore (some (rankSkip 0))
| .skip i => MachineLookup.discard .work (.skip i) (rankSkip (i.val + 1))
| .done => .halt
theorem rankProgram_skip {i : Nat} (hi : i < 4097) :
rankProgram (σ := σ) (rankSkip i) =
MachineLookup.discard .work (rankSkip i) (rankSkip (i + 1)) := by
simp only [rankSkip, dite_eq_left hi, rankProgram]
theorem rowGuard_succ (original work target scratch count fuelSuffix : List Bool)
(k : Nat) (ambient : σ) (flag : Bool) (register : Option Bool) :
TM2.step (rankProgram (σ := σ))
⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord (k + 1) ++ fuelSuffix)⟩ =
some ⟨some .field, ((ambient, flag), none),
memory original work target scratch count (encodeWord k ++ fuelSuffix)⟩ := by
change some (TM2.stepAux (rankProgram .row) _ _) = _
simp [rankProgram, MachineUnaryCounter.guard, TM2.stepAux, encodeWord,
List.replicate_succ]
theorem rowGuard_zero (original work target scratch count fuelSuffix : List Bool)
(ambient : σ) (flag : Bool) (register : Option Bool) :
TM2.step (rankProgram (σ := σ))
⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩ =
some ⟨some .done, ((ambient, flag), none),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩ := rfl
theorem zeroRowsTrace (original work target scratch count fuelSuffix : List Bool)
(ambient : σ) (flag : Bool) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[2]
(some ⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩) =
some ⟨none, ((ambient, flag), none),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩ := by
rw [Function.iterate_succ_apply]
change advance (TM2.step (rankProgram (σ := σ)))
(TM2.step rankProgram ⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩) = _
rw [rowGuard_zero]
rfl
theorem rankRowTrace (r : Row) (hwidth : r.2.length = 4097) (v k : Nat)
(original suffix targetSuffix fuelSuffix count : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[rowTime v r]
(some ⟨some .row, ((ambient, false), register),
memory original (encodeWords (rowWords r) ++ suffix) (encodeWord v ++ targetSuffix)
[] count (encodeWord (k + 1) ++ fuelSuffix)⟩) =
some ⟨some .row, ((ambient, false), none),
memory original suffix (encodeWord v ++ targetSuffix) []
(List.replicate (if r.1 = v then 1 else 0) true ++ count)
(encodeWord k ++ fuelSuffix)⟩ := by
have hfield := preservingFieldTrace RankLabel.field RankLabel.restore (rankSkip 0)
rankProgram rfl rfl original (encodeWords r.2 ++ suffix) targetSuffix count
(encodeWord k ++ fuelSuffix) r.1 v ambient none
have hskip := discardFieldsTrace rankSkip (rankProgram (σ := σ)) r.2 0
(by intro i hi; simpa only [Nat.zero_add] using rankProgram_skip (hwidth ▸ hi))
original suffix (encodeWord v ++ targetSuffix) []
(List.replicate (if r.1 = v then 1 else 0) true ++ count)
(encodeWord k ++ fuelSuffix) ambient false none
have hnonempty : r.2 ≠ [] := by intro h; simp [h] at hwidth
simp only [Nat.zero_add, hwidth, rankSkip, lt_self_iff_false, ↓reduceDIte,
hnonempty, ite_false] at hskip
have htime : rowTime v r = (encodeWords r.2).length + (r.1 + min r.1 v + 2) + 1 := by
simp only [rowTime, rowWords, encodeWords, List.length_append, encodeWord_length]
omega
rw [htime, Function.iterate_succ_apply]
change (advance (TM2.step (rankProgram (σ := σ))))^[
(encodeWords r.2).length + (r.1 + min r.1 v + 2)]
(TM2.step rankProgram ⟨some .row, ((ambient, false), register),
memory original (encodeWords (rowWords r) ++ suffix) (encodeWord v ++ targetSuffix)
[] count (encodeWord (k + 1) ++ fuelSuffix)⟩) = _
rw [rowGuard_succ, Function.iterate_add_apply]
simp only [rowWords, encodeWords, List.append_assoc]
rw [hfield]
exact hskip
def prefixTime (v : Nat) : List Row → Nat
| [] => 2
| r :: rs => prefixTime v rs + rowTime v r
theorem prefixTrace (rs : List Row) (hwidth : ∀ r ∈ rs, r.2.length = 4097) (v : Nat)
(original suffix targetSuffix fuelSuffix count : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[prefixTime v rs]
(some ⟨some .row, ((ambient, false), register),
memory original (encodeWords (rs.flatMap rowWords) ++ suffix)
(encodeWord v ++ targetSuffix) [] count (encodeWord rs.length ++ fuelSuffix)⟩) =
some ⟨none, ((ambient, false), none),
memory original suffix (encodeWord v ++ targetSuffix) []
(List.replicate (rowsHits v rs) true ++ count) (encodeWord 0 ++ fuelSuffix)⟩ := by
induction rs generalizing count register with
| nil =>
simpa only [prefixTime, List.flatMap_nil, encodeWords, List.nil_append,
List.length_nil, rowsHits, List.replicate_zero] using
zeroRowsTrace original suffix (encodeWord v ++ targetSuffix) [] count fuelSuffix
ambient false register
| cons r rs ih =>
have hrow := rankRowTrace r (hwidth r (by simp)) v rs.length original
(encodeWords (rs.flatMap rowWords) ++ suffix) targetSuffix fuelSuffix count ambient register
have hrest := ih (by intro x hx; exact hwidth x (by simp [hx]))
(List.replicate (if r.1 = v then 1 else 0) true ++ count) none
simp only [prefixTime, List.flatMap_cons, encodeWords_append, List.append_assoc,
List.length_cons]
rw [Function.iterate_add_apply, hrow]
simpa only [rowsHits, ← List.append_assoc, List.replicate_append_replicate,
Nat.add_comm] using hrest
theorem prefixTime_le (v : Nat) (rs : List Row) :
prefixTime v rs ≤ 3 * (encodeWords (rs.flatMap rowWords)).length + 2 := by
induction rs with
| nil => simp only [prefixTime, List.flatMap_nil, encodeWords, List.length_nil,
Nat.mul_zero, Nat.zero_add, le_refl]
| cons r rs ih =>
have hr := rowTime_le v r
simp only [prefixTime, List.flatMap_cons, encodeWords_append, List.length_append]
omega
def queryWord (v k : Nat) (suffix : List Bool) : List Bool :=
encodeWord v ++ (encodeWord k ++ suffix)
theorem encoded_rows_split (rs : List Row) (k : Nat) :
encodeWords (rs.flatMap rowWords) =
encodeWords ((rs.take k).flatMap rowWords) ++
encodeWords ((rs.drop k).flatMap rowWords) := by
have h := congrArg (fun xs : List Row => encodeWords (xs.flatMap rowWords))
(List.take_append_drop k rs)
simpa only [List.flatMap_append, encodeWords_append] using h.symm
def rankSteps (n m v k : Nat) (rs : List Row) (querySuffix : List Bool) : Nat :=
2 * ((queryWord v k querySuffix).length + 1) + (v + 1) +
2 * ((inputWord n m rs).length + 1) + (n + 1) + (m + 1) +
prefixTime v (rs.take k)
theorem rankTrace (n m v k : Nat) (rs : List Row) (hk : k ≤ rs.length)
(hwidth : ∀ r ∈ rs, r.2.length = 4097) (querySuffix count : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[rankSteps n m v k rs querySuffix]
(some ⟨some .queryFirst, ((ambient, false), register),
memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count []⟩) =
some ⟨none, ((ambient, false), none),
memory (inputWord n m rs) (encodeWords ((rs.drop k).flatMap rowWords))
(queryWord v k querySuffix) []
(List.replicate (rowsHits v (rs.take k)) true ++ count)
(encodeWord 0 ++ querySuffix)⟩ := by
have hquery := MachineCopy.copyTrace Tape.target Tape.spare Tape.scratch
(by decide) (by decide) (by decide) false RankLabel.queryFirst RankLabel.querySecond
(some RankLabel.querySkip) rankProgram rfl rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count [])
rfl (ambient, false) register
simp only [memory_target, memory_spare, List.append_nil, update_memory_spare] at hquery
have hquerySkip := MachineLookup.discardTrace Tape.spare RankLabel.querySkip RankLabel.tableFirst
rankProgram rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count (queryWord v k querySuffix))
v (encodeWord k ++ querySuffix) rfl (ambient, false) none
simp only [update_memory_spare] at hquerySkip
have hcopy := MachineCopy.copyTrace Tape.original Tape.work Tape.scratch
(by decide) (by decide) (by decide) false RankLabel.tableFirst RankLabel.tableSecond
(some RankLabel.headerFirst) rankProgram rfl rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count
(encodeWord k ++ querySuffix)) rfl (ambient, false) none
simp only [memory_original, memory_work, List.append_nil, update_memory_work] at hcopy
have hfirst := MachineLookup.discardTrace Tape.work RankLabel.headerFirst RankLabel.headerSecond
rankProgram rfl
(memory (inputWord n m rs) (inputWord n m rs) (queryWord v k querySuffix) [] count
(encodeWord k ++ querySuffix))
n (encodeWord m ++ encodeWords (rs.flatMap rowWords))
(inputWord_eq n m rs) (ambient, false) none
simp only [update_memory_work] at hfirst
have hsecond := MachineLookup.discardTrace Tape.work RankLabel.headerSecond RankLabel.row
rankProgram rfl
(memory (inputWord n m rs) (encodeWord m ++ encodeWords (rs.flatMap rowWords))
(queryWord v k querySuffix) [] count (encodeWord k ++ querySuffix))
m (encodeWords (rs.flatMap rowWords)) rfl (ambient, false) none
simp only [update_memory_work] at hsecond
have hprefix := prefixTrace (rs.take k)
(by intro r hr; exact hwidth r (List.mem_of_mem_take hr)) v
(inputWord n m rs) (encodeWords ((rs.drop k).flatMap rowWords))
(encodeWord k ++ querySuffix) querySuffix count ambient none
have htake : (rs.take k).length = k := by
simp only [List.length_take, Nat.min_eq_left hk]
rw [htake, ← encoded_rows_split rs k] at hprefix
rw [show rankSteps n m v k rs querySuffix = prefixTime v (rs.take k) +
((m + 1) + ((n + 1) + (2 * ((inputWord n m rs).length + 1) +
((v + 1) + 2 * ((queryWord v k querySuffix).length + 1))))) by
unfold rankSteps; omega,
Function.iterate_add_apply _ (prefixTime v (rs.take k)),
Function.iterate_add_apply _ (m + 1), Function.iterate_add_apply _ (n + 1),
Function.iterate_add_apply _ (2 * ((inputWord n m rs).length + 1)),
Function.iterate_add_apply _ (v + 1), hquery, hquerySkip, hcopy, hfirst, hsecond]
exact hprefix
theorem rankSteps_le (n m v k : Nat) (rs : List Row) (querySuffix : List Bool) :
rankSteps n m v k rs querySuffix ≤
5 * (inputWord n m rs).length + 3 * (queryWord v k querySuffix).length + 6 := by
have hprefix := prefixTime_le v (rs.take k)
have hsplit := congrArg List.length (encoded_rows_split rs k)
simp only [List.length_append] at hsplit
have hinput := inputWord_length n m rs
have hquery : v + 1 ≤ (queryWord v k querySuffix).length := by
simp only [queryWord, List.length_append, encodeWord_length]
omega
unfold rankSteps
omega
theorem idxOf_filter_prefix {α : Type*} [BEq α] [LawfulBEq α]
(p : α → Bool) (a : α) (hp : p a = true) :
∀ xs : List α, a ∈ xs →
(xs.filter p).idxOf a = ((xs.take (xs.idxOf a)).filter p).length := by
classical
intro xs
induction xs with
| nil => intro ha; simp at ha
| cons b xs ih =>
intro ha
by_cases hba : b = a
· subst b
simp [hp]
· have ha' : a ∈ xs := by simpa [hba, Ne.symm hba] using ha
cases hb : p b <;>
simp [hba, hb, ih ha']
open DFVSGames.Foundations.PCP
theorem tableRows_length (t : GraphTables.Table) : (tableRows t).length = t.darts := by
simp only [tableRows, List.length_map, GraphTables.rowList_length]
theorem cloudRank_eq_prefix_count (t : GraphTables.Table) (v : Fin t.vertices)
(e : DegreeReplacement.Cloud (GraphTables.semantics t) v) :
(PreprocessingCloudIndex.cloudRank t v e).val =
rowsHits v.val ((tableRows t).take e.val.val) := by
have hindex := idxOf_filter_prefix (fun x : Fin t.darts => decide (t.rows[x].tail = v))
e.val (by simpa using e.property) (List.finRange t.darts) (List.mem_finRange e.val)
simp only [List.idxOf_finRange] at hindex
change (PreprocessingCloudIndex.cloudDarts t v).idxOf e.val = _
unfold PreprocessingCloudIndex.cloudDarts
rw [hindex, rowsHits_eq_filter]
simp only [tableRows, rowList_eq_finRange_map, ← List.map_take, List.filter_map,
List.length_map, Function.comp_def, Fin.val_inj]
theorem cloudRankTrace (t : GraphTables.Table) (v : Fin t.vertices)
(e : DegreeReplacement.Cloud (GraphTables.semantics t) v)
(querySuffix countSuffix : List Bool) (ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[
rankSteps t.vertices t.darts v.val e.val.val (tableRows t) querySuffix]
(some ⟨some .queryFirst, ((ambient, false), register),
memory (GraphTables.tableBits t) [] (queryWord v.val e.val.val querySuffix) []
(encodeWord 0 ++ countSuffix) []⟩) =
some ⟨none, ((ambient, false), none),
memory (GraphTables.tableBits t)
(encodeWords (((tableRows t).drop e.val.val).flatMap rowWords))
(queryWord v.val e.val.val querySuffix) []
(encodeWord (PreprocessingCloudIndex.cloudRank t v e).val ++ countSuffix)
(encodeWord 0 ++ querySuffix)⟩ := by
have hk : e.val.val ≤ (tableRows t).length := by
rw [tableRows_length]
exact e.val.isLt.le
have h := rankTrace t.vertices t.darts v.val e.val.val (tableRows t) hk
(tableRows_width t) querySuffix (encodeWord 0 ++ countSuffix) ambient register
rw [tableRows_input, ← cloudRank_eq_prefix_count t v e, ← List.append_assoc,
replicate_encodeWord, Nat.add_zero] at h
exact h
def cloudRankInTime (t : GraphTables.Table) (v : Fin t.vertices)
(e : DegreeReplacement.Cloud (GraphTables.semantics t) v)
(querySuffix countSuffix : List Bool) (ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step (rankProgram (σ := σ)))
⟨some .queryFirst, ((ambient, false), register),
memory (GraphTables.tableBits t) [] (queryWord v.val e.val.val querySuffix) []
(encodeWord 0 ++ countSuffix) []⟩
(some ⟨none, ((ambient, false), none),
memory (GraphTables.tableBits t)
(encodeWords (((tableRows t).drop e.val.val).flatMap rowWords))
(queryWord v.val e.val.val querySuffix) []
(encodeWord (PreprocessingCloudIndex.cloudRank t v e).val ++ countSuffix)
(encodeWord 0 ++ querySuffix)⟩)
(5 * (GraphTables.tableBits t).length +
3 * (queryWord v.val e.val.val querySuffix).length + 6) where
steps := rankSteps t.vertices t.darts v.val e.val.val (tableRows t) querySuffix
evals_in_steps := cloudRankTrace t v e querySuffix countSuffix ambient register
steps_le_m := by
simpa only [tableRows_input] using
rankSteps_le t.vertices t.darts v.val e.val.val (tableRows t) querySuffix
end DFVSGames.Foundations.Complexity.MachineCloudRank
end OAI