cfahlgren1/math / lean /OAI /Computability /MaxCut /PCP /ProfileBridge.lean
cfahlgren1's picture
download
raw
28.6 kB
import Mathlib
import OAI.Computability.MaxCut.PCP.Bounds
import OAI.Computability.MaxCut.Machines.MachineFieldProfile
namespace OAI
namespace MaxCutGames.Foundations.Hastad.SourceProfileBridge
open Target SourceContexts SourceOccurrences SourceLocalSignature
open Complexity
def positionEncoding (u : ℕ) : Encoding (Position u) :=
(Encoding.fin u).prod slotEncoding
def names (F : Formula) {u : ℕ} (c : ClauseContext F u) :
Fin (positionEncoding u).size → ℕ :=
fun i => (occurrenceName F c ((positionEncoding u).code.symm i)).val
def nameWords (F : Formula) {u : ℕ} (c : ClauseContext F u) :
Fin (positionEncoding u).size → List Bool :=
fun i => encodeWord (names F c i)
def reindexProfile (u : ℕ) (profile : MachineFieldProfile.Profile (positionEncoding u).size) :
NameProfile u :=
fun p q => profile ((positionEncoding u).code p) ((positionEncoding u).code q)
theorem computed_profile_eq (F : Formula) {u : ℕ} (c : ClauseContext F u) :
reindexProfile u (MachineFieldProfile.equalityProfile (nameWords F c)) =
contextNameProfile F c := by
funext p q
unfold reindexProfile nameWords
rw [MachineFieldProfile.equalityProfile_encodeWord]
simp only [names, Equiv.symm_apply_apply, contextNameProfile, Fin.ext_iff]
theorem prepared_signature_eq (F : Formula) {u : ℕ} (c : ClauseContext F u)
(s : SlotContext u) :
(⟨fun p => positiveAt (PCP.clauseAt F (c p.1)) p.2,
reindexProfile u (MachineFieldProfile.equalityProfile (nameWords F c)), s⟩ :
Signature u) = ofContext F c s := by
rw [computed_profile_eq]
rfl
/-- Every isolated unary variable-name field is bounded by the true CNF
input encoding, including formulas with unused declared variable names. -/
theorem nameWords_length_le_input (F : Formula) {u : ℕ} (c : ClauseContext F u)
(i : Fin (positionEncoding u).size) :
(nameWords F c i).length ≤ (formulaBits F).length := by
rw [nameWords, encodeWord_length]
exact (Nat.succ_le_of_lt
(occurrenceName F c ((positionEncoding u).code.symm i)).isLt).trans
(SourceBounds.formulaBits_length_ge_variables F)
end MaxCutGames.Foundations.Hastad.SourceProfileBridge
namespace MaxCutGames.Foundations.Hastad.SourceSignLoad
open Turing
open SourceLocalSignature
open MaxCutGames.Foundations.Complexity
/-- An explicit computable enumeration, without an arbitrary finite-type
equivalence. Slots occur first, second, third within each clause coordinate. -/
def positions (u : ℕ) : List (Position u) :=
((SourceOccurrences.Encoding.fin u).prod SourceOccurrences.slotEncoding).enumerate
theorem mem_positions {u : ℕ} (p : Position u) : p ∈ positions u :=
SourceOccurrences.Encoding.mem_enumerate _ p
/-- The mathematical result of the individual finite sign-register writes. -/
def fillSigns {u : ℕ} (values initial : Position u → Bool)
(cells : List (Position u)) : Position u → Bool :=
cells.foldl (fun signs p => Function.update signs p (values p)) initial
theorem fillSigns_apply {u : ℕ} (values initial : Position u → Bool)
(cells : List (Position u)) (p : Position u) :
fillSigns values initial cells p = if p ∈ cells then values p else initial p := by
induction cells generalizing initial with
| nil => simp [fillSigns]
| cons q cells ih =>
change fillSigns values (Function.update initial q (values q)) cells p = _
rw [ih]
by_cases hp : p ∈ cells
· simp [hp]
· by_cases hpq : p = q
· subst q; simp [hp]
· simp [hp, hpq]
theorem fillSigns_positions {u : ℕ} (values initial : Position u → Bool) :
fillSigns values initial (positions u) = values := by
funext p
rw [fillSigns_apply]
simp only [mem_positions, ↓reduceIte]
variable {u : ℕ} {K Λ τ : Type} [DecidableEq K]
abbrev State (u : ℕ) (τ : Type) := Signature u × τ
abbrev Alphabet (_ : K) := Bool
def finish (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State u τ) :=
match exit with
| none => .halt
| some label => .goto fun _ => label
/-- Each statement reads only the head of one supplied polarity field. The
other signature fields and the entire ambient-register component are copied. -/
def peekChain (field : Position u → K) (exit : Option Λ) :
List (Position u) → TM2.Stmt (Alphabet (K := K)) Λ (State u τ)
| [] => finish exit
| p :: rest =>
.peek (field p) (fun state head =>
({state.1 with signs := Function.update state.1.signs p (head.getD false)}, state.2))
(peekChain field exit rest)
def statement (field : Position u → K) (exit : Option Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (State u τ) :=
peekChain field exit (positions u)
/-- The head defaults make the finite program total on malformed tapes too;
the correctness theorem below supplies the actual unary-field representation. -/
def readSigns (field : Position u → K) (tapes : K → List Bool) : Position u → Bool :=
fun p => (tapes (field p)).head?.getD false
theorem stepAux_peekChain (field : Position u → K) (exit : Option Λ)
(cells : List (Position u)) (initial : Signature u) (ambient : τ)
(tapes : K → List Bool) :
TM2.stepAux (peekChain field exit cells) (initial, ambient) tapes =
⟨exit, ({initial with signs := fillSigns (readSigns field tapes) initial.signs cells},
ambient), tapes⟩ := by
induction cells generalizing initial with
| nil => cases exit <;> rfl
| cons p rest ih =>
simp only [peekChain, TM2.stepAux]
rw [ih]
rfl
theorem stepAux_statement (field : Position u → K) (exit : Option Λ)
(initial : Signature u) (ambient : τ) (tapes : K → List Bool) :
TM2.stepAux (statement field exit) (initial, ambient) tapes =
⟨exit, ({initial with signs := readSigns field tapes}, ambient), tapes⟩ := by
rw [statement, stepAux_peekChain, fillSigns_positions]
omit [DecidableEq K] in
theorem peekChain_pushBound (field : Position u → K) (exit : Option Λ)
(cells : List (Position u)) :
Runtime.statementPushBound (peekChain (τ := τ) field exit cells) = 0 := by
induction cells with
| nil => cases exit <;> rfl
| cons p rest ih => exact ih
omit [DecidableEq K] in
theorem statement_pushBound (field : Position u → K) (exit : Option Λ) :
Runtime.statementPushBound (statement (τ := τ) field exit) = 0 :=
peekChain_pushBound field exit (positions u)
omit [DecidableEq K] in
theorem readSigns_of_unary (field : Position u → K) (tapes : K → List Bool)
(values : Position u → Bool) (suffix : Position u → List Bool)
(hfields : ∀ p, tapes (field p) = encodeWord (if values p then 1 else 0) ++ suffix p) :
readSigns field tapes = values := by
funext p
rw [readSigns, hfields]
cases values p <;> rfl
/-- Exact actual caller-program step. Every stored field and unread suffix
is preserved, as are sameName, selected, and all ambient registers. -/
theorem loadStep (field : Position u → K) (entryLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State u τ))
(atEntry : program entryLabel = statement field exit)
(initial : Signature u) (ambient : τ) (tapes : K → List Bool)
(values : Position u → Bool) (suffix : Position u → List Bool)
(hfields : ∀ p, tapes (field p) = encodeWord (if values p then 1 else 0) ++ suffix p) :
TM2.step program ⟨some entryLabel, (initial, ambient), tapes⟩ =
some ⟨exit, ({initial with signs := values}, ambient), tapes⟩ := by
change some (TM2.stepAux (program entryLabel) (initial, ambient) tapes) = _
rw [atEntry, stepAux_statement, readSigns_of_unary field tapes values suffix hfields]
def loadInTime (field : Position u → K) (entryLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State u τ))
(atEntry : program entryLabel = statement field exit)
(initial : Signature u) (ambient : τ) (tapes : K → List Bool)
(values : Position u → Bool) (suffix : Position u → List Bool)
(hfields : ∀ p, tapes (field p) = encodeWord (if values p then 1 else 0) ++ suffix p) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some entryLabel, (initial, ambient), tapes⟩
(some ⟨exit, ({initial with signs := values}, ambient), tapes⟩) 1 where
steps := 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[1] _ = _
simpa only [Function.iterate_one, MachineComposition.advance_some] using
loadStep field entryLabel exit program atEntry initial ambient tapes values suffix hfields
steps_le_m := Nat.le_refl _
omit [DecidableEq K] in
/-- The actual context signature is recovered when equality and selected-slot
data have already been loaded by their separate verified preparation stages. -/
theorem loaded_ofContext (F : Target.Formula) (c : SourceContexts.ClauseContext F u)
(selected : SourceContexts.SlotContext u) (initial : Signature u)
(hnames : initial.sameName = contextNameProfile F c)
(hselected : initial.selected = selected) :
{initial with signs := (ofContext F c selected).signs} = ofContext F c selected := by
cases initial with
| mk signs sameName chosen =>
dsimp only at hnames hselected
cases hnames
cases hselected
rfl
def loadContextInTime (F : Target.Formula) (c : SourceContexts.ClauseContext F u)
(selected : SourceContexts.SlotContext u) (field : Position u → K)
(entryLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State u τ))
(atEntry : program entryLabel = statement field exit)
(initial : Signature u) (hnames : initial.sameName = contextNameProfile F c)
(hselected : initial.selected = selected) (ambient : τ) (tapes : K → List Bool)
(suffix : Position u → List Bool)
(hfields : ∀ p, tapes (field p) =
encodeWord (if positiveAt (PCP.clauseAt F (c p.1)) p.2 then 1 else 0) ++ suffix p) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some entryLabel, (initial, ambient), tapes⟩
(some ⟨exit, (ofContext F c selected, ambient), tapes⟩) 1 := by
have h := loadInTime field entryLabel exit program atEntry initial ambient tapes
(ofContext F c selected).signs suffix hfields
rw [loaded_ofContext F c selected initial hnames hselected] at h
exact h
end MaxCutGames.Foundations.Hastad.SourceSignLoad
namespace MaxCutGames.Foundations.Hastad.SourceSignaturePrepare
open Turing
open Target PCP SourceContexts SourceOccurrences SourceLocalSignature SourceProfileBridge
open MaxCutGames.Foundations.Complexity
abbrev Width (u : ℕ) := (positionEncoding u).size
abbrev Arena (u : ℕ) (Extra : Type) := SourceContextLoad.Tape u Extra
abbrev ProfileState (u : ℕ) := MachineCompare.State (MachineFieldProfile.Profile (Width u))
abbrev State (u : ℕ) (τ : Type) := ProfileState u × (Signature u × τ)
abbrev ProfileLabel (u : ℕ) := MachineFieldProfile.Label (MachineFieldProfile.allPairs (Width u))
inductive Label (u : ℕ)
| profile (label : ProfileLabel u)
| commit
deriving DecidableEq, Fintype
variable {u : ℕ} {Extra τ : Type}
def variableField (p : Position u) : Arena u Extra :=
SourceContextLoad.variableField p.1 (slotEncoding.code p.2)
def polarityField (p : Position u) : Arena u Extra :=
SourceContextLoad.polarityField p.1 (slotEncoding.code p.2)
def place : MachineFieldProfile.Tape (Width u) → Arena u Extra
| .inl i => variableField ((positionEncoding u).code.symm i)
| .inr w => if w = 0 then .index else if w = 1 then .work else .scratch
def unplace : Arena u Extra → Option (MachineFieldProfile.Tape (Width u))
| .index => some (.inr 0)
| .work => some (.inr 1)
| .scratch => some (.inr 2)
| .field t s =>
if s = 0 then some (.inl ((positionEncoding u).code (t, .first)))
else if s = 2 then some (.inl ((positionEncoding u).code (t, .second)))
else if s = 4 then some (.inl ((positionEncoding u).code (t, .third))) else none
| _ => none
@[simp] theorem unplace_place (k : MachineFieldProfile.Tape (Width u)) :
unplace (place (Extra := Extra) k) = some k := by
cases k with
| inl i =>
obtain ⟨p, rfl⟩ := (positionEncoding u).code.surjective i
rcases p with ⟨t, s⟩
simp only [place, Equiv.symm_apply_apply]
cases s <;> rfl
| inr w =>
obtain ⟨w, hw⟩ := w
have h : w = 0 ∨ w = 1 ∨ w = 2 := by omega
rcases h with rfl | rfl | rfl <;> rfl
theorem place_injective : Function.Injective (place (u := u) (Extra := Extra)) := by
intro a b h
have h' := congrArg unplace h
simpa only [unplace_place, Option.some.injEq] using h'
theorem place_unplace (a : Arena u Extra) (k : MachineFieldProfile.Tape (Width u))
(h : unplace a = some k) : place k = a := by
cases a with
| formula => simp [unplace] at h
| index => cases Option.some.inj h; rfl
| work => cases Option.some.inj h; rfl
| scratch => cases Option.some.inj h; rfl
| copyScratch => simp [unplace] at h
| current t => simp [unplace] at h
| remaining t => simp [unplace] at h
| extra e => simp [unplace] at h
| field t s =>
by_cases h0 : s = 0
· subst s
simp only [unplace, ↓reduceIte] at h
cases Option.some.inj h
simp only [place, Equiv.symm_apply_apply]
rfl
· by_cases h2 : s = 2
· subst s
simp only [unplace, ↓reduceIte] at h
cases Option.some.inj h
simp only [place, Equiv.symm_apply_apply]
rfl
· by_cases h4 : s = 4
· subst s
simp only [unplace, ↓reduceIte] at h
cases Option.some.inj h
simp only [place, Equiv.symm_apply_apply]
rfl
· simp [unplace, h0, h2, h4] at h
def fill (base : Arena u Extra → List Bool)
(localTapes : MachineFieldProfile.Tape (Width u) → List Bool) : Arena u Extra → List Bool :=
fun a => match unplace a with
| some k => localTapes k
| none => base a
@[simp] theorem fill_place (base : Arena u Extra → List Bool)
(localTapes : MachineFieldProfile.Tape (Width u) → List Bool)
(k : MachineFieldProfile.Tape (Width u)) : fill base localTapes (place k) = localTapes k := by
simp only [fill, unplace_place]
@[simp] theorem fill_self (base : Arena u Extra → List Bool) :
fill base (base ∘ place) = base := by
funext a
cases h : unplace a with
| none => simp only [fill, h]
| some k =>
simp only [fill, h, Function.comp_apply, place_unplace a k h]
theorem fill_frame (base : Arena u Extra → List Bool)
(localTapes : MachineFieldProfile.Tape (Width u) → List Bool)
(a : Arena u Extra) (h : unplace a = none) : fill base localTapes a = base a := by
simp only [fill, h]
variable [DecidableEq Extra]
theorem fill_update (base : Arena u Extra → List Bool)
(localTapes : MachineFieldProfile.Tape (Width u) → List Bool)
(k : MachineFieldProfile.Tape (Width u)) (value : List Bool) :
fill base (Function.update localTapes k value) =
Function.update (fill base localTapes) (place k) value := by
funext a
cases hu : unplace a with
| none =>
have hne : a ≠ place k := by
intro h
rw [h, unplace_place] at hu
contradiction
simp only [fill, hu, Function.update_of_ne hne]
| some j =>
have ha := place_unplace a j hu
rw [← ha]
by_cases h : j = k
· subst j
simp only [fill_place]
simp [Function.update]
· have hne : place (Extra := Extra) j ≠ place k := fun h' => h (place_injective h')
simp only [fill_place, Function.update_of_ne h, Function.update_of_ne hne]
def placedLabel : Option (ProfileLabel u) → Option (Label u)
| none => some .commit
| some l => some (.profile l)
def placedConfiguration (base : Arena u Extra → List Bool) (metadata : Signature u × τ)
(c : TM2.Cfg (fun _ : MachineFieldProfile.Tape (Width u) => Bool)
(ProfileLabel u) (ProfileState u)) :
TM2.Cfg (fun _ : Arena u Extra => Bool) (Label u) (State u τ) :=
⟨placedLabel c.l, (c.var, metadata), fill base c.stk⟩
def placedStatement :
TM2.Stmt (fun _ : MachineFieldProfile.Tape (Width u) => Bool)
(ProfileLabel u) (ProfileState u) →
TM2.Stmt (fun _ : Arena u Extra => Bool) (Label u) (State u τ)
| .push k f next => .push (place k) (fun state => f state.1) (placedStatement next)
| .peek k f next => .peek (place k) (fun state bit => (f state.1 bit, state.2))
(placedStatement next)
| .pop k f next => .pop (place k) (fun state bit => (f state.1 bit, state.2))
(placedStatement next)
| .load f next => .load (fun state => (f state.1, state.2)) (placedStatement next)
| .branch f yes no => .branch (fun state => f state.1)
(placedStatement yes) (placedStatement no)
| .goto f => .goto (fun state => .profile (f state.1))
| .halt => .goto (fun _ => .commit)
theorem placed_stepAux (base : Arena u Extra → List Bool) (metadata : Signature u × τ)
(q : TM2.Stmt (fun _ : MachineFieldProfile.Tape (Width u) => Bool)
(ProfileLabel u) (ProfileState u))
(state : ProfileState u) (localTapes : MachineFieldProfile.Tape (Width u) → List Bool) :
TM2.stepAux (placedStatement q) (state, metadata) (fill base localTapes) =
placedConfiguration base metadata (TM2.stepAux q state localTapes) := by
induction q generalizing state localTapes with
| push k f next ih =>
simp only [placedStatement, TM2.stepAux, fill_place]
rw [← fill_update]
exact ih state _
| peek k f next ih =>
simpa only [placedStatement, TM2.stepAux, fill_place] using
ih (f state (localTapes k).head?) localTapes
| pop k f next ih =>
simp only [placedStatement, TM2.stepAux, fill_place]
rw [← fill_update]
exact ih (f state (localTapes k).head?) _
| load f next ih =>
simpa only [placedStatement, TM2.stepAux] using ih (f state) localTapes
| branch f yes no ihYes ihNo =>
cases h : f state
· simpa only [placedStatement, TM2.stepAux, h, Bool.cond_false] using ihNo state localTapes
· simpa only [placedStatement, TM2.stepAux, h, Bool.cond_true] using ihYes state localTapes
| goto f => rfl
| halt => rfl
/-- Reassociate finite registers without adding a transition. -/
def signStateEquiv (u : ℕ) (τ : Type) :
SourceSignLoad.State u (ProfileState u × τ) ≃ State u τ where
toFun x := (x.2.1, (x.1, x.2.2))
invFun x := (x.2.1, (x.1, x.2.2))
left_inv _ := rfl
right_inv _ := rfl
def commitStatement : TM2.Stmt (fun _ : Arena u Extra => Bool) (Label u) (State u τ) :=
.load (fun state =>
(state.1, ({state.2.1 with sameName := reindexProfile u state.1.1}, state.2.2)))
(MachineControl.statement id (signStateEquiv u τ)
(SourceSignLoad.statement polarityField none))
def program : Label u → TM2.Stmt (fun _ : Arena u Extra => Bool) (Label u) (State u τ)
| .profile label => placedStatement
(MachineFieldProfile.profileProgram (MachineFieldProfile.allPairs (Width u)) label)
| .commit => commitStatement
theorem placed_step (base : Arena u Extra → List Bool) (metadata : Signature u × τ)
(a b : TM2.Cfg (fun _ : MachineFieldProfile.Tape (Width u) => Bool)
(ProfileLabel u) (ProfileState u))
(h : (MachineFieldProfile.profileMachine (Width u)).step a = some b) :
TM2.step program (placedConfiguration base metadata a) =
some (placedConfiguration base metadata b) := by
rcases a with ⟨label, state, tapes⟩
cases label with
| none => cases h
| some label =>
change some (TM2.stepAux
(MachineFieldProfile.profileProgram (MachineFieldProfile.allPairs (Width u)) label)
state tapes) = some b at h
cases Option.some.inj h
change some (TM2.stepAux (placedStatement _) (state, metadata) (fill base tapes)) = _
rw [placed_stepAux]
theorem stepAux_commit (work : ProfileState u) (initial : Signature u) (ambient : τ)
(tapes : Arena u Extra → List Bool) :
TM2.stepAux commitStatement (work, (initial, ambient)) tapes =
⟨none, (work, (⟨SourceSignLoad.readSigns polarityField tapes,
reindexProfile u work.1, initial.selected⟩, ambient)), tapes⟩ := by
change TM2.stepAux (MachineControl.statement id (signStateEquiv u τ)
(SourceSignLoad.statement polarityField none))
((signStateEquiv u τ)
({initial with sameName := reindexProfile u work.1}, (work, ambient))) tapes = _
rw [MachineControl.stepAux_simulation, SourceSignLoad.stepAux_statement]
rfl
/-- The actual profile execution, placed in the loaded context arena. -/
def profileInTime (tapes : Arena u Extra → List Bool)
(hwork : ∀ w : Fin 3, tapes (place (.inr w)) = [])
(initialProfile : MachineFieldProfile.Profile (Width u))
(initial : Signature u) (ambient : τ) (L : ℕ)
(hL : ∀ i, (tapes (place (.inl i))).length ≤ L) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (.profile (MachineFieldProfile.labelAt (MachineFieldProfile.allPairs (Width u)) 0 0)),
(MachineFieldProfile.normalState initialProfile, (initial, ambient)), tapes⟩
(some ⟨some .commit,
(MachineFieldProfile.normalState
(MachineFieldProfile.equalityProfile (fun i => tapes (place (.inl i)))),
(initial, ambient)), tapes⟩)
(Width u * Width u * (5 * L + 6) + 1) := by
let run := MachineFieldProfile.profileInTime_bounded (Width u) (tapes ∘ place)
hwork initialProfile L hL
have lifted := MachineComposition.liftExecutionInTime _ _
(placedConfiguration tapes (initial, ambient))
(placed_step tapes (initial, ambient)) run
simpa only [placedConfiguration, placedLabel, fill_self, Function.comp_apply] using lifted
def commitInTime (work : ProfileState u) (initial : Signature u) (ambient : τ)
(tapes : Arena u Extra → List Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some .commit, (work, (initial, ambient)), tapes⟩
(some ⟨none, (work, (⟨SourceSignLoad.readSigns polarityField tapes,
reindexProfile u work.1, initial.selected⟩, ambient)), tapes⟩) 1 where
steps := 1
evals_in_steps := by
change TM2.step program ⟨some .commit, (work, (initial, ambient)), tapes⟩ = _
change some (TM2.stepAux commitStatement (work, (initial, ambient)) tapes) = _
rw [stepAux_commit]
steps_le_m := Nat.le_refl _
/-- Actual preparation from isolated name fields and unary sign fields. The
conclusion preserves every tape and arbitrary caller metadata. -/
def prepareInTime (F : Formula) (c : ClauseContext F u) (selected : SlotContext u)
(tapes : Arena u Extra → List Bool)
(hwork : ∀ w : Fin 3, tapes (place (.inr w)) = [])
(hnames : ∀ i, tapes (place (.inl i)) = nameWords F c i)
(suffix : Position u → List Bool)
(hsigns : ∀ p, tapes (polarityField p) =
encodeWord (if positiveAt (clauseAt F (c p.1)) p.2 then 1 else 0) ++ suffix p)
(initialProfile : MachineFieldProfile.Profile (Width u))
(initial : Signature u) (hselected : initial.selected = selected) (ambient : τ) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (.profile (MachineFieldProfile.labelAt (MachineFieldProfile.allPairs (Width u)) 0 0)),
(MachineFieldProfile.normalState initialProfile, (initial, ambient)), tapes⟩
(some ⟨none,
(MachineFieldProfile.normalState (MachineFieldProfile.equalityProfile (nameWords F c)),
(ofContext F c selected, ambient)), tapes⟩)
(Width u * Width u * (5 * (formulaBits F).length + 6) + 2) := by
have hfields : (fun i => tapes (place (.inl i))) = nameWords F c := funext hnames
have hsign : SourceSignLoad.readSigns polarityField tapes =
fun p => positiveAt (clauseAt F (c p.1)) p.2 :=
SourceSignLoad.readSigns_of_unary polarityField tapes _ suffix hsigns
have hL : ∀ i, (tapes (place (.inl i))).length ≤ (formulaBits F).length := by
intro i
rw [hnames]
exact nameWords_length_le_input F c i
have first := profileInTime tapes hwork initialProfile initial ambient
(formulaBits F).length hL
rw [hfields] at first
have last := commitInTime
(MachineFieldProfile.normalState (MachineFieldProfile.equalityProfile (nameWords F c)))
initial ambient tapes
dsimp only [MachineFieldProfile.normalState] at last
rw [hsign, hselected, prepared_signature_eq F c selected] at last
let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ first last
refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ }
have h := run.steps_le_m
omega
omit [DecidableEq Extra] in
theorem loaded_name_field (F : Formula) (c : ClauseContext F u)
(base : Arena u Extra → List Bool)
(hbase : ∀ p : Position u, base (variableField p) = [])
(i : Fin (Width u)) :
SourceContextLoad.stageTapes F c base u (place (.inl i)) = nameWords F c i := by
obtain ⟨p, rfl⟩ := (positionEncoding u).code.surjective i
simp only [place, Equiv.symm_apply_apply]
have ho := SourceContextLoad.output_variable F c base p.1 (slotEncoding.code p.2)
change SourceContextLoad.stageTapes F c base u (variableField p) =
encodeWord (F.clauses[(c p.1).val][(slotEncoding.code p.2).val].variableIndex.val) ++
base (variableField p) at ho
rw [ho, hbase p, List.append_nil]
simp only [nameWords, names, Equiv.symm_apply_apply]
rcases p with ⟨t, s⟩
cases s <;> rfl
omit [DecidableEq Extra] in
/-- Unary sign fields may retain arbitrary old suffixes: only the leading
sign bit is read. -/
theorem loaded_sign_field (F : Formula) (c : ClauseContext F u)
(base : Arena u Extra → List Bool) (p : Position u) :
SourceContextLoad.stageTapes F c base u (polarityField p) =
encodeWord (if positiveAt (clauseAt F (c p.1)) p.2 then 1 else 0) ++
base (polarityField p) := by
have ho := SourceContextLoad.output_polarity F c base p.1 (slotEncoding.code p.2)
change SourceContextLoad.stageTapes F c base u (polarityField p) =
encodeWord (if F.clauses[(c p.1).val][(slotEncoding.code p.2).val].positive then 1 else 0) ++
base (polarityField p) at ho
rw [ho]
rcases p with ⟨t, s⟩
cases s <;> rfl
/-- Exactly the loaded context signature is computed by the concrete finite
program. Scratch is reset and all loaded fields and caller tapes are preserved. -/
def loadedPrepareInTime (F : Formula) (c : ClauseContext F u) (selected : SlotContext u)
(base : Arena u Extra → List Bool)
(hindex : base .index = []) (hwork : base .work = []) (hscratch : base .scratch = [])
(hbase : ∀ p : Position u, base (variableField p) = [])
(initialProfile : MachineFieldProfile.Profile (Width u))
(initial : Signature u) (hselected : initial.selected = selected) (ambient : τ) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (.profile (MachineFieldProfile.labelAt (MachineFieldProfile.allPairs (Width u)) 0 0)),
(MachineFieldProfile.normalState initialProfile, (initial, ambient)),
SourceContextLoad.stageTapes F c base u⟩
(some ⟨none,
(MachineFieldProfile.normalState (MachineFieldProfile.equalityProfile (nameWords F c)),
(ofContext F c selected, ambient)), SourceContextLoad.stageTapes F c base u⟩)
(Width u * Width u * (5 * (formulaBits F).length + 6) + 2) := by
apply prepareInTime F c selected (SourceContextLoad.stageTapes F c base u) _
(loaded_name_field F c base hbase) (fun p => base (polarityField p))
(loaded_sign_field F c base) initialProfile initial hselected ambient
intro w
have hc := SourceContextLoad.stageTapes_clean F c base u hindex hwork
have hs : SourceContextLoad.stageTapes F c base u .scratch = [] := by
rw [SourceContextLoad.stageTapes_frame F c base u .scratch
(by simp) (by simp) (by intros; simp)]
exact hscratch
obtain ⟨w, hw⟩ := w
have h : w = 0 ∨ w = 1 ∨ w = 2 := by omega
rcases h with rfl | rfl | rfl
· exact hc.1
· exact hc.2
· exact hs
/-- Finiteness of tape addresses and registers constructs an actual TM2;
its execution bound is established above by the concrete transitions. -/
def machine (u : ℕ) (Extra τ : Type) [DecidableEq Extra] [Fintype Extra] [Fintype τ]
(initialProfile : MachineFieldProfile.Profile (Width u))
(initial : Signature u) (ambient : τ) : FinTM2 where
K := Arena u Extra
k₀ := .formula
k₁ := .formula
Γ _ := Bool
Λ := Label u
main := .profile (MachineFieldProfile.labelAt (MachineFieldProfile.allPairs (Width u)) 0 0)
σ := State u τ
initialState := (MachineFieldProfile.normalState initialProfile, (initial, ambient))
m := program
end MaxCutGames.Foundations.Hastad.SourceSignaturePrepare
end OAI

Xet Storage Details

Size:
28.6 kB
·
Xet hash:
86b88f7d770c053f61189dd770b8727dda6b5884ecc38c42469c8a84423f2952

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