Download data/lean/OAI/Combinatorics/IndependentSets/Machines/Block.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 13.6 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/IndependentSets/Machines/Block.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/IndependentSets/Machines/Block.lean
-
curl -L -o Block.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/IndependentSets/Machines/Block.lean
13.6 kB
| import Mathlib | |
| import OAI.Combinatorics.IndependentSets.Machines.MachineUnaryCounter | |
| import OAI.Combinatorics.IndependentSets.Machines.PoweringMachineRow | |
| namespace OAI | |
| namespace IndependentSetsGames.Foundations.Complexity.PoweringMachineRow | |
| open Turing | |
| open MachineFixedBlockMap | |
| open IndependentSetsGames.Foundations.PCP | |
| section Block | |
| variable {K Ξ Ο : Type} {N M : Nat} | |
| def encodedBlockStmt (src dst : K) (F : Buffer N β Buffer M) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) : | |
| TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N) := | |
| readEncodedSlots src (List.ofFn id) | |
| (writeEncodedSlots dst (fun state => F state.2) (List.ofFn id).reverse | |
| (.load (fun state => (state.1, emptyBuffer N)) next)) | |
| def encodedBlockAt (src dst : K) (F : Buffer N β Buffer M) (exit : Option Ξ) : | |
| TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N) := | |
| encodedBlockStmt src dst F (finishAt exit) | |
| theorem statementPushBound_encodedBlockAt (src dst : K) | |
| (F : Buffer N β Buffer M) (exit : Option Ξ) : | |
| Runtime.statementPushBound (encodedBlockAt (Ο := Ο) src dst F exit) = 2 * M := by | |
| cases exit <;> simp [encodedBlockAt, encodedBlockStmt, | |
| statementPushBound_readEncodedSlots, statementPushBound_writeEncodedSlots, | |
| finishAt, Runtime.statementPushBound] | |
| variable [DecidableEq K] | |
| theorem stepAux_encodedBlockStmt (src dst : K) (F : Buffer N β Buffer M) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) | |
| (hne : src β dst) (bits : Buffer N) (suffix : List Bool) | |
| (state : Ο Γ Buffer N) (tapes : K β List Bool) | |
| (hinput : tapes src = encodeBits (List.ofFn bits) ++ suffix) : | |
| TM2.stepAux (encodedBlockStmt src dst F next) state tapes = | |
| TM2.stepAux next (state.1, emptyBuffer N) | |
| (Function.update (Function.update tapes src suffix) dst | |
| (encodeBits (List.ofFn (F bits)) ++ tapes dst)) := by | |
| unfold encodedBlockStmt | |
| rw [stepAux_readEncodedAll src _ state bits tapes suffix hinput, | |
| stepAux_writeEncodedSlots] | |
| simp only [List.map_reverse, List.map_ofFn, Function.comp_id, List.reverse_reverse, | |
| Function.update_of_ne (Ne.symm hne), TM2.stepAux] | |
| theorem stepAux_encodedBlockAt (src dst : K) (F : Buffer N β Buffer M) | |
| (exit : Option Ξ) (hne : src β dst) (bits : Buffer N) (suffix : List Bool) | |
| (state : Ο Γ Buffer N) (tapes : K β List Bool) | |
| (hinput : tapes src = encodeBits (List.ofFn bits) ++ suffix) : | |
| TM2.stepAux (encodedBlockAt src dst F exit) state tapes = | |
| { l := exit, var := (state.1, emptyBuffer N), | |
| stk := Function.update (Function.update tapes src suffix) dst | |
| (encodeBits (List.ofFn (F bits)) ++ tapes dst) } := by | |
| rw [encodedBlockAt, stepAux_encodedBlockStmt src dst F _ hne bits suffix state tapes hinput] | |
| cases exit <;> rfl | |
| theorem step_encodedBlockAt (src dst : K) (F : Buffer N β Buffer M) | |
| (exit : Option Ξ) | |
| (program : Ξ β TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) | |
| (label : Ξ) (hprogram : program label = encodedBlockAt src dst F exit) | |
| (hne : src β dst) (bits : Buffer N) (suffix : List Bool) | |
| (state : Ο Γ Buffer N) (tapes : K β List Bool) | |
| (hinput : tapes src = encodeBits (List.ofFn bits) ++ suffix) : | |
| TM2.step program { l := some label, var := state, stk := tapes } = | |
| some { l := exit | |
| var := (state.1, emptyBuffer N) | |
| stk := Function.update (Function.update tapes src suffix) dst | |
| (encodeBits (List.ofFn (F bits)) ++ tapes dst) } := by | |
| simp only [TM2.step, hprogram, | |
| stepAux_encodedBlockAt src dst F exit hne bits suffix state tapes hinput] | |
| end Block | |
| def rowAt {K Ξ Ο : Type} {t S q : Nat} (src dst : K) | |
| (labelAt : Fin q β Fin S β GraphTables.Label) (exit : Option Ξ) : | |
| TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer (inputSize t S)) := | |
| encodedBlockAt src dst (rowBlock labelAt) exit | |
| def machine {t S q : Nat} (labelAt : Fin q β Fin S β GraphTables.Label) : FinTM2 where | |
| K := Bool | |
| kβ := false | |
| kβ := true | |
| Ξ _ := Bool | |
| Ξ := Unit | |
| main := () | |
| Ο := Unit Γ Buffer (inputSize t S) | |
| initialState := ((), emptyBuffer (inputSize t S)) | |
| m _ := rowAt false true labelAt none | |
| theorem machine_statementPushBound {t S q : Nat} | |
| (labelAt : Fin q β Fin S β GraphTables.Label) : | |
| Runtime.statementPushBound ((machine (t := t) labelAt).m ()) = 2 * (q * q) := | |
| statementPushBound_encodedBlockAt false true (rowBlock labelAt) none | |
| def machineInTime {t S q : Nat} (labelAt : Fin q β Fin S β GraphTables.Label) | |
| (bits : Buffer (inputSize t S)) (suffix : List Bool) | |
| (register : Buffer (inputSize t S)) (tapes : Bool β List Bool) | |
| (hinput : tapes false = encodeBits (List.ofFn bits) ++ suffix) : | |
| StateTransition.EvalsToInTime (machine (t := t) labelAt).step | |
| β¨some (), ((), register), tapesβ© | |
| (some β¨none, ((), emptyBuffer (inputSize t S)), | |
| Function.update (Function.update tapes false suffix) true | |
| (encodeBits (List.ofFn (rowBlock labelAt bits)) ++ tapes true)β©) 1 where | |
| steps := 1 | |
| evals_in_steps := by | |
| change TM2.step (fun _ : Unit => rowAt false true labelAt none) | |
| { l := some (), var := ((), register), stk := tapes } = | |
| some { l := none | |
| var := ((), emptyBuffer (inputSize t S)) | |
| stk := Function.update (Function.update tapes false suffix) true | |
| (encodeBits (List.ofFn (rowBlock labelAt bits)) ++ tapes true) } | |
| exact step_encodedBlockAt false true (rowBlock labelAt) (none : Option Unit) | |
| (fun _ => rowAt false true labelAt none) () rfl (by decide) | |
| bits suffix ((), register) tapes hinput | |
| steps_le_m := le_rfl | |
| end IndependentSetsGames.Foundations.Complexity.PoweringMachineRow | |
| namespace IndependentSetsGames.Foundations.Complexity.MachineCountedLoop | |
| open Turing | |
| open scoped BigOperators | |
| open MachineUnaryCounter | |
| variable {K Ξ Ο : Type} [DecidableEq K] | |
| def guardConfiguration (counter : K) (guardLabel : Ξ) (suffix : List Bool) | |
| (ambient : Nat β Ο) (register : Nat β Option Bool) | |
| (base : Nat β K β List Bool) (n : Nat) : | |
| TM2.Cfg (Alphabet (K := K)) Ξ (Ο Γ Option Bool) := | |
| β¨some guardLabel, (ambient n, register n), counterTapes counter (base n) n suffixβ© | |
| def bodyConfiguration (counter : K) (bodyLabel : Ξ) (suffix : List Bool) | |
| (ambient : Nat β Ο) (base : Nat β K β List Bool) (n : Nat) : | |
| TM2.Cfg (Alphabet (K := K)) Ξ (Ο Γ Option Bool) := | |
| β¨some bodyLabel, (ambient (n + 1), none), counterTapes counter (base (n + 1)) n suffixβ© | |
| def exitConfiguration (counter : K) (exitLabel : Ξ) (suffix : List Bool) | |
| (ambient : Nat β Ο) (base : Nat β K β List Bool) : | |
| TM2.Cfg (Alphabet (K := K)) Ξ (Ο Γ Option Bool) := | |
| β¨some exitLabel, (ambient 0, none), counterTapes counter (base 0) 0 suffixβ© | |
| def BodyTraces (counter : K) (guardLabel bodyLabel : Ξ) | |
| (program : Ξ β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool)) | |
| (suffix : List Bool) (ambient : Nat β Ο) (register : Nat β Option Bool) | |
| (base : Nat β K β List Bool) (cost : Nat β Nat) (n : Nat) : Prop := | |
| β r, r < n β | |
| (MachineComposition.advance (TM2.step program))^[cost r] | |
| (some (bodyConfiguration counter bodyLabel suffix ambient base r)) = | |
| some (guardConfiguration counter guardLabel suffix ambient register base r) | |
| def totalSteps (cost : Nat β Nat) (n : Nat) : Nat := | |
| (β r β Finset.range n, cost r) + n + 1 | |
| theorem totalSteps_zero (cost : Nat β Nat) : totalSteps cost 0 = 1 := by | |
| simp [totalSteps] | |
| theorem totalSteps_succ (cost : Nat β Nat) (n : Nat) : | |
| totalSteps cost (n + 1) = (totalSteps cost n + cost n) + 1 := by | |
| simp only [totalSteps, Finset.sum_range_succ] | |
| omega | |
| theorem loopTrace (counter : K) (guardLabel bodyLabel exitLabel : Ξ) | |
| (program : Ξ β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool)) | |
| (atGuard : program guardLabel = guard counter bodyLabel exitLabel) | |
| (suffix : List Bool) (ambient : Nat β Ο) (register : Nat β Option Bool) | |
| (base : Nat β K β List Bool) (cost : Nat β Nat) (n : Nat) | |
| (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix | |
| ambient register base cost n) : | |
| (MachineComposition.advance (TM2.step program))^[totalSteps cost n] | |
| (some (guardConfiguration counter guardLabel suffix ambient register base n)) = | |
| some (exitConfiguration counter exitLabel suffix ambient base) := by | |
| revert bodyTraces | |
| induction n with | |
| | zero => | |
| intro _ | |
| simpa only [totalSteps_zero, guardConfiguration, exitConfiguration] using | |
| guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard | |
| (base 0) suffix (ambient 0) (register 0) | |
| | succ n ih => | |
| intro bodyTraces | |
| rw [totalSteps_succ, Function.iterate_succ_apply] | |
| change (MachineComposition.advance (TM2.step program))^[totalSteps cost n + cost n] | |
| (TM2.step program | |
| β¨some guardLabel, (ambient (n + 1), register (n + 1)), | |
| counterTapes counter (base (n + 1)) (n + 1) suffixβ©) = _ | |
| rw [guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard | |
| (base (n + 1)) n suffix (ambient (n + 1)) (register (n + 1)), | |
| Function.iterate_add_apply] | |
| change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] | |
| ((MachineComposition.advance (TM2.step program))^[cost n] | |
| (some (bodyConfiguration counter bodyLabel suffix ambient base n))) = _ | |
| rw [bodyTraces n (Nat.lt_succ_self n)] | |
| exact ih (fun r hr => bodyTraces r (Nat.lt_trans hr (Nat.lt_succ_self n))) | |
| theorem totalSteps_le (cost : Nat β Nat) (n B : Nat) | |
| (bodyBound : β r, r < n β cost r β€ B) : | |
| totalSteps cost n β€ n * (B + 1) + 1 := by | |
| have hsum : (β r β Finset.range n, cost r) β€ n * B := by | |
| calc | |
| (β r β Finset.range n, cost r) β€ β _r β Finset.range n, B := | |
| Finset.sum_le_sum (fun r hr => bodyBound r (Finset.mem_range.mp hr)) | |
| _ = n * B := by simp | |
| simpa only [totalSteps, Nat.mul_add, Nat.mul_one] using | |
| Nat.add_le_add_right (Nat.add_le_add_right hsum n) 1 | |
| def loopInTime (counter : K) (guardLabel bodyLabel exitLabel : Ξ) | |
| (program : Ξ β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool)) | |
| (atGuard : program guardLabel = guard counter bodyLabel exitLabel) | |
| (suffix : List Bool) (ambient : Nat β Ο) (register : Nat β Option Bool) | |
| (base : Nat β K β List Bool) (cost : Nat β Nat) (n B : Nat) | |
| (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix | |
| ambient register base cost n) | |
| (bodyBound : β r, r < n β cost r β€ B) : | |
| StateTransition.EvalsToInTime (TM2.step program) | |
| (guardConfiguration counter guardLabel suffix ambient register base n) | |
| (some (exitConfiguration counter exitLabel suffix ambient base)) | |
| (n * (B + 1) + 1) where | |
| steps := totalSteps cost n | |
| evals_in_steps := by | |
| change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] _ = _ | |
| exact loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix | |
| ambient register base cost n bodyTraces | |
| steps_le_m := totalSteps_le cost n B bodyBound | |
| omit [DecidableEq K] in | |
| theorem baseFrame (base : Nat β K β List Bool) (frame : K β Prop) (n : Nat) | |
| (bodyFrame : β r, r < n β β k, frame k β base (r + 1) k = base r k) : | |
| β k, frame k β base n k = base 0 k := by | |
| revert bodyFrame | |
| induction n with | |
| | zero => intro _ k _; rfl | |
| | succ n ih => | |
| intro bodyFrame k hk | |
| exact (bodyFrame n (Nat.lt_succ_self n) k hk).trans | |
| (ih (fun r hr => bodyFrame r (Nat.lt_trans hr (Nat.lt_succ_self n))) k hk) | |
| theorem counterFrame (counter : K) (suffix : List Bool) | |
| (base : Nat β K β List Bool) (frame : K β Prop) (n : Nat) | |
| (counterOutside : β k, frame k β k β counter) | |
| (bodyFrame : β r, r < n β β k, frame k β base (r + 1) k = base r k) : | |
| β k, frame k β | |
| counterTapes counter (base n) n suffix k = counterTapes counter (base 0) 0 suffix k := by | |
| intro k hk | |
| rw [counterTapes_other counter k (counterOutside k hk), | |
| counterTapes_other counter k (counterOutside k hk)] | |
| exact baseFrame base frame n bodyFrame k hk | |
| theorem loopTrace_framed (counter : K) (guardLabel bodyLabel exitLabel : Ξ) | |
| (program : Ξ β TM2.Stmt (Alphabet (K := K)) Ξ (Ο Γ Option Bool)) | |
| (atGuard : program guardLabel = guard counter bodyLabel exitLabel) | |
| (suffix : List Bool) (ambient : Nat β Ο) (register : Nat β Option Bool) | |
| (base : Nat β K β List Bool) (cost : Nat β Nat) (n : Nat) | |
| (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix | |
| ambient register base cost n) | |
| (frame : K β Prop) (counterOutside : β k, frame k β k β counter) | |
| (bodyFrame : β r, r < n β β k, frame k β base (r + 1) k = base r k) : | |
| ((MachineComposition.advance (TM2.step program))^[totalSteps cost n] | |
| (some (guardConfiguration counter guardLabel suffix ambient register base n)) = | |
| some (exitConfiguration counter exitLabel suffix ambient base)) β§ | |
| (β k, frame k β | |
| (guardConfiguration counter guardLabel suffix ambient register base n).stk k = | |
| (exitConfiguration counter exitLabel suffix ambient base).stk k) := | |
| β¨loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix | |
| ambient register base cost n bodyTraces, | |
| counterFrame counter suffix base frame n counterOutside bodyFrameβ© | |
| end IndependentSetsGames.Foundations.Complexity.MachineCountedLoop | |
| end OAI | |