Download data/lean/OAI/Combinatorics/IndependentSets/Reduction/FiniteScan.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 13 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/IndependentSets/Reduction/FiniteScan.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/IndependentSets/Reduction/FiniteScan.lean
-
curl -L -o FiniteScan.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/IndependentSets/Reduction/FiniteScan.lean
13 kB
| import Mathlib | |
| import OAI.Combinatorics.IndependentSets.PCP.BoundedIteration | |
| import OAI.Combinatorics.IndependentSets.Reduction.StoredFloyd | |
| namespace OAI | |
| namespace LargeIndependentSets.FiniteScan | |
| open IndependentSetsCut.CounterMachine | |
| open IndependentSetsGames.Foundations.Complexity | |
| open Turing | |
| open scoped BigOperators | |
| structure State (m : β) where | |
| input : List Bool | |
| k : β | |
| history : β β Fin m | |
| def State.value {m : β} (s : State m) (w : β) : β := | |
| if w=0 then s.input.length else if w=1 then s.k else | |
| if w<2+s.input.length then Expr.bitValue s.input (w-2) | |
| else (s.history (w-(2+s.input.length))).val | |
| def State.bits {m : β} (s : State m) : List Bool := | |
| UnaryTables.words s.value (3+2*s.input.length) | |
| def initial {m : β} (q : Fin m) (input : List Bool) : State m := β¨input,0,fun _ => qβ© | |
| def step {m : β} (Ξ΄ : Fin m β Bool β Fin m) (s : State m) : State m where | |
| input := s.input | |
| k := s.k+1 | |
| history := if h : s.k<s.input.length then | |
| Function.update s.history (s.k+1) (Ξ΄ (s.history s.k) s.input[s.k]) else s.history | |
| @[simp] lemma read_n {m : β} (s : State m) : Expr.wordValue s.bits 0 = s.input.length := by | |
| rw [State.bits,UnaryTables.wordValue_words _ _ _ (by omega)] | |
| simp [State.value] | |
| @[simp] lemma read_k {m : β} (s : State m) : Expr.wordValue s.bits 1 = s.k := by | |
| rw [State.bits,UnaryTables.wordValue_words _ _ _ (by omega)] | |
| simp [State.value] | |
| lemma read_input {m : β} (s : State m) (i : β) (hi : i<s.input.length) : | |
| Expr.wordValue s.bits (2+i) = Expr.bitValue s.input i := by | |
| rw [State.bits,UnaryTables.wordValue_words _ _ _ (by omega)] | |
| simp [State.value,show 2+iβ 1 by omega,show 2+i<2+s.input.length by omega] | |
| lemma read_history {m : β} (s : State m) (i : β) (hi : i β€ s.input.length) : | |
| Expr.wordValue s.bits (2+s.input.length+i) = (s.history i).val := by | |
| rw [State.bits,UnaryTables.wordValue_words _ _ _ (by omega)] | |
| simp [State.value, | |
| show 2+s.input.length+iβ 1 by omega,show Β¬2+s.input.length+i<2+s.input.length by omega] | |
| def nExpr : Expr := .word (.const 0) | |
| def kExpr : Expr := .word (.const 1) | |
| def inputExpr (i : Expr) : Expr := .word (.add (.const 2) i) | |
| def historyExpr (i : Expr) : Expr := .word (.add (.add (.const 2) nExpr) i) | |
| def transitionExpr {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q b : Expr) : Expr := | |
| Expr.listSum (List.finRange m) (fun i => .mul (Expr.equal q (.const i.val)) | |
| (Expr.cond b (.const (Ξ΄ i true).val) (.const (Ξ΄ i false).val))) | |
| lemma transitionExpr_eval {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q b : Expr) | |
| (s : List Bool) (args : β β β) (i : Fin m) (c : Bool) | |
| (hq : q.eval s args=i.val) (hb : b.eval s args=if c then 1 else 0) : | |
| (transitionExpr Ξ΄ q b).eval s args = (Ξ΄ i c).val := by | |
| simp only [transitionExpr,Expr.listSum_eval,Expr.eval,Expr.equal_eval,Expr.cond_eval,hq,hb] | |
| cases c <;> simp only [Bool.false_eq_true, βreduceIte, ne_eq, not_true_eq_false, | |
| one_ne_zero, not_false_eq_true] | |
| all_goals | |
| rw [β List.ofFn_eq_map, List.sum_ofFn] | |
| simp [Fin.val_inj] | |
| @[simp] lemma nExpr_eval {m : β} (s : State m) (args : β β β) : | |
| nExpr.eval s.bits args = s.input.length := by simp [nExpr,Expr.eval] | |
| @[simp] lemma kExpr_eval {m : β} (s : State m) (args : β β β) : | |
| kExpr.eval s.bits args = s.k := by simp [kExpr,Expr.eval] | |
| @[simp] lemma inputExpr_eval (i : Expr) (s : List Bool) (args : β β β) : | |
| (inputExpr i).eval s args = Expr.wordValue s (2+i.eval s args) := by simp [inputExpr,Expr.eval] | |
| @[simp] lemma historyExpr_eval (i : Expr) (s : List Bool) (args : β β β) : | |
| (historyExpr i).eval s args = Expr.wordValue s (2+nExpr.eval s args+i.eval s args) := by | |
| simp [historyExpr,Expr.eval] | |
| def countExpr : Expr := .add (.const 3) (.mul (.const 2) nExpr) | |
| def valueExpr {m : β} (Ξ΄ : Fin m β Bool β Fin m) : Expr := | |
| Expr.cond (Expr.equal (.arg 0) (.const 1)) (.add kExpr (.const 1)) | |
| (Expr.cond (.mul (Expr.lt kExpr nExpr) | |
| (Expr.equal (.arg 0) (.add (.add (.const 3) nExpr) kExpr))) | |
| (transitionExpr Ξ΄ (historyExpr kExpr) (inputExpr kExpr)) (.word (.arg 0))) | |
| lemma valueExpr_eval {m : β} (Ξ΄ : Fin m β Bool β Fin m) (s : State m) | |
| (w : β) (hw : w<3+2*s.input.length) : | |
| (valueExpr Ξ΄).eval s.bits (fun _ => w) = (step Ξ΄ s).value w := by | |
| have old := UnaryTables.wordValue_words s.value (3+2*s.input.length) w hw | |
| change Expr.wordValue s.bits w = s.value w at old | |
| by_cases hw1 : w=1 | |
| Β· subst w | |
| simp [valueExpr,Expr.eval,State.value,step] | |
| by_cases hk : s.k<s.input.length | |
| Β· have ht : (transitionExpr Ξ΄ (historyExpr kExpr) (inputExpr kExpr)).eval s.bits (fun _ => w) = | |
| (Ξ΄ (s.history s.k) s.input[s.k]).val := by | |
| apply transitionExpr_eval | |
| Β· simp [read_history s s.k hk.le] | |
| Β· simp [read_input s s.k hk,Expr.bitValue,List.getElem?_eq_getElem hk] | |
| simp only [valueExpr,Expr.cond_eval,Expr.equal_eval,Expr.eval,hw1,ite_false, | |
| ne_eq,not_true_eq_false,ite_true,nExpr_eval,kExpr_eval,Expr.lt_eval,Expr.word_eval, | |
| hk,one_mul,ht,old] | |
| simp only [step,dite_eq_left hk,State.value] | |
| by_cases hw0 : w=0 | |
| Β· subst w; simp [show Β¬0=3+s.input.length+s.k by omega] | |
| by_cases hwl : w<2+s.input.length | |
| Β· have hne : wβ 3+s.input.length+s.k := by omega | |
| simp [hw0,hw1,hwl,hne] | |
| Β· have he : w=3+s.input.length+s.k β w-(2+s.input.length)=s.k+1 := by omega | |
| simp only [hw0,hw1,hwl,ite_false,Function.update_apply] | |
| by_cases hwe : w=3+s.input.length+s.k | |
| Β· rw [ite_eq_left hwe,ite_eq_left (he.mp hwe)]; simp | |
| Β· rw [ite_eq_right hwe,ite_eq_right (mt he.mpr hwe)]; simp | |
| Β· simp [valueExpr,Expr.eval,hk,hw1,old,step,State.value] | |
| lemma body_spec {m : β} (Ξ΄ : Fin m β Bool β Fin m) (s : State m) : | |
| UnaryTables.words (fun j => (valueExpr Ξ΄).eval s.bits (fun _ => j)) | |
| (countExpr.eval s.bits (fun _ => 0)) = (step Ξ΄ s).bits := by | |
| have hn : countExpr.eval s.bits (fun _ => 0) = 3+2*s.input.length := by | |
| simp [countExpr,Expr.eval] | |
| rw [hn] | |
| exact UnaryTables.words_congr (valueExpr_eval Ξ΄ s) | |
| noncomputable def body {m : β} (Ξ΄ : Fin m β Bool β Fin m) : | |
| TM2ComputableInPolyTime State.bits State.bits (step Ξ΄) := | |
| UnaryTables.encodedComputer State.bits State.bits (step Ξ΄) countExpr (valueExpr Ξ΄) (body_spec Ξ΄) | |
| lemma body_finiteAlphabet {m : β} (Ξ΄ : Fin m β Bool β Fin m) : | |
| MachineFiniteAlphabet.FiniteAlphabet (body Ξ΄).tm := | |
| UnaryTables.encodedComputer_finiteAlphabet _ _ _ _ _ _ | |
| lemma iterate_input {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) (s : List Bool) (i : β) : | |
| ((step Ξ΄)^[i] (initial q s)).input = s := by | |
| induction i with | |
| | zero => rfl | |
| | succ i ih => simpa [Function.iterate_succ_apply',step] using ih | |
| lemma iterate_k {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) (s : List Bool) (i : β) : | |
| ((step Ξ΄)^[i] (initial q s)).k = i := by | |
| induction i with | |
| | zero => rfl | |
| | succ i ih => simp [Function.iterate_succ_apply',step,ih] | |
| lemma history_prefix {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) (s : List Bool) | |
| (i : β) (hi : i β€ s.length) (j : β) (hj : j β€ i) : | |
| ((step Ξ΄)^[i] (initial q s)).history j = (s.take j).foldl Ξ΄ q := by | |
| induction i generalizing j with | |
| | zero => | |
| have : j=0 := by omega | |
| subst j | |
| rfl | |
| | succ i ih => | |
| have hk : i<s.length := hi | |
| rw [Function.iterate_succ_apply'] | |
| simp only [step,iterate_k,iterate_input,dite_eq_left hk,Function.update_apply] | |
| by_cases he : j=i+1 | |
| Β· rw [ite_eq_left he] | |
| subst j | |
| rw [ih hk.le i le_rfl,List.take_succ_eq_append_getElem hk,List.foldl_append] | |
| rfl | |
| Β· rw [ite_eq_right he] | |
| exact ih hk.le j (by omega) | |
| def output {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) (s : List Bool) : State m := | |
| (step Ξ΄)^[s.length] (initial q s) | |
| lemma output_history {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) (s : List Bool) | |
| (j : β) (hj : j β€ s.length) : | |
| (output Ξ΄ q s).history j = (s.take j).foldl Ξ΄ q := history_prefix Ξ΄ q s _ le_rfl j hj | |
| lemma value_le {m : β} (s : State m) (w : β) : s.value w β€ s.input.length+s.k+m+1 := by | |
| unfold State.value | |
| split | |
| Β· omega | |
| Β· split | |
| Β· omega | |
| Β· split | |
| Β· unfold Expr.bitValue; split <;> omega | |
| Β· have := (s.history (w-(2+s.input.length))).isLt; omega | |
| lemma bits_length_le {m : β} (s : State m) : | |
| s.bits.length β€ (3+2*s.input.length)*(s.input.length+s.k+m+2) := by | |
| rw [State.bits,UnaryTables.words_length] | |
| unfold UnaryTables.offset | |
| calc | |
| _ β€ β _ β Finset.range (3+2*s.input.length), (s.input.length+s.k+m+2) := by | |
| apply Finset.sum_le_sum | |
| intro w _hw | |
| have := value_le s w | |
| omega | |
| _ = _ := by simp | |
| noncomputable def lengthPolynomial (m : β) : Polynomial β := | |
| (3+2*Polynomial.X)*(2*Polynomial.X+Polynomial.C (m+2)) | |
| lemma lengthPolynomial_eval (m N : β) : | |
| (lengthPolynomial m).eval N = (3+2*N)*(2*N+m+2) := by | |
| simp [lengthPolynomial,Nat.add_assoc] | |
| def countedInput {m : β} (q : Fin m) (s : List Bool) : List Bool := | |
| BoundedIteration.counted List.length State.bits (initial q) s | |
| lemma intermediate_bound {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) (s : List Bool) | |
| (i : β) (hi : i β€ s.length) : | |
| ((step Ξ΄)^[i] (initial q s)).bits.length β€ (lengthPolynomial m).eval (countedInput q s).length := by | |
| rw [lengthPolynomial_eval] | |
| apply (bits_length_le _).trans | |
| rw [iterate_input,iterate_k] | |
| have hn : s.length β€ (countedInput q s).length := | |
| BoundedIteration.count_le List.length State.bits (initial q) s | |
| gcongr; omega | |
| noncomputable def countedCertificate {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) : | |
| TM2ComputableInPolyTime (countedInput q) State.bits (output Ξ΄ q) := | |
| BoundedIteration.certificate State.bits (step Ξ΄) (initial q) List.length (body Ξ΄) | |
| (lengthPolynomial m) (intermediate_bound Ξ΄ q) | |
| lemma countedCertificate_finiteAlphabet {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) : | |
| MachineFiniteAlphabet.FiniteAlphabet (countedCertificate Ξ΄ q).tm := | |
| BoundedIteration.certificate_finiteAlphabet _ _ _ _ _ _ _ (body_finiteAlphabet Ξ΄) | |
| lemma words_cons (f : β β β) (n : β) : | |
| UnaryTables.words f (n+1) = (List.replicate (f 0) true ++ [false]) ++ | |
| UnaryTables.words (fun j => f (j+1)) n := by | |
| simp [UnaryTables.words,List.range_succ_eq_map,List.flatMap_map,Function.comp_def] | |
| def initialCount : Expr := .add (.const 4) (.mul (.const 2) .length) | |
| def initialValue {m : β} (q : Fin m) : Expr := | |
| Expr.cond (Expr.le (.arg 0) (.const 1)) .length | |
| (Expr.cond (Expr.equal (.arg 0) (.const 2)) (.const 0) | |
| (Expr.cond (Expr.lt (.arg 0) (.add (.const 3) .length)) | |
| (.bit (.sub (.arg 0) (.const 3))) (.const q.val))) | |
| lemma initialValue_zero {m : β} (q : Fin m) (s : List Bool) : | |
| (initialValue q).eval s (fun _ => 0) = s.length := by simp [initialValue,Expr.eval] | |
| lemma initialValue_succ {m : β} (q : Fin m) (s : List Bool) (w : β) : | |
| (initialValue q).eval s (fun _ => w+1) = (initial q s).value w := by | |
| simp only [initialValue,Expr.cond_eval,Expr.le_eval,Expr.equal_eval,Expr.lt_eval,Expr.eval] | |
| have h1 : w+1β€1 β w=0 := by omega | |
| have h2 : w+1=2 β w=1 := by omega | |
| have h3 : w+1<3+s.length β w<2+s.length := by omega | |
| simp only [h1,h2,h3] | |
| by_cases hw0 : w=0 | |
| Β· simp [hw0,initial,State.value] | |
| by_cases hw1 : w=1 | |
| Β· simp [hw1,initial,State.value] | |
| have ht : w+1-3=w-2 := by omega | |
| simp [hw0,hw1,ht,initial,State.value,Expr.bitValue] | |
| rfl | |
| lemma initial_spec {m : β} (q : Fin m) (s : List Bool) : | |
| UnaryTables.words (fun j => (initialValue q).eval s (fun _ => j)) | |
| (initialCount.eval s (fun _ => 0)) = countedInput q s := by | |
| have hc : initialCount.eval s (fun _ => 0) = (3+2*s.length)+1 := by | |
| simp [initialCount,Expr.eval]; omega | |
| rw [hc,words_cons,initialValue_zero] | |
| unfold countedInput BoundedIteration.counted State.bits | |
| congr 1 | |
| exact UnaryTables.words_congr (fun j _hj => initialValue_succ q s j) | |
| noncomputable def initialCertificate {m : β} (q : Fin m) : | |
| TM2ComputableInPolyTime (id : List Bool β List Bool) (countedInput q) id := | |
| UnaryTables.encodedComputer id (countedInput q) id initialCount (initialValue q) (initial_spec q) | |
| lemma initialCertificate_finiteAlphabet {m : β} (q : Fin m) : | |
| MachineFiniteAlphabet.FiniteAlphabet (initialCertificate q).tm := | |
| UnaryTables.encodedComputer_finiteAlphabet _ _ _ _ _ _ | |
| noncomputable def certificate {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) : | |
| TM2ComputableInPolyTime (id : List Bool β List Bool) State.bits (output Ξ΄ q) := | |
| MachineSequential.composeBits (initialCertificate q) (countedCertificate Ξ΄ q) | |
| lemma finiteAlphabet {m : β} (Ξ΄ : Fin m β Bool β Fin m) (q : Fin m) : | |
| MachineFiniteAlphabet.FiniteAlphabet (certificate Ξ΄ q).tm := | |
| MachineFiniteAlphabet.composeBits _ _ (initialCertificate_finiteAlphabet q) | |
| (countedCertificate_finiteAlphabet Ξ΄ q) | |
| end LargeIndependentSets.FiniteScan | |
| end OAI | |