import Mathlib import OAI.Combinatorics.MatchingCount.Complexity.TreePolynomialArithmetic namespace OAI namespace MatchingFPRAS.TreeTyped open Polynomial variable {A B C D : Type} [Codec A] [Codec B] [Codec C] [Codec D] lemma polynomial_fst {f : A → B×C} {F : Realizer f} (h : PolynomialTime F) : PolynomialTime (composition F first) := polynomial_composition h polynomial_first lemma polynomial_snd {f : A → B×C} {F : Realizer f} (h : PolynomialTime F) : PolynomialTime (composition F second) := polynomial_composition h polynomial_second lemma weight_append (as bs : List A) : weight (as++bs)+1=weight as+weight bs := by induction as with | nil => simp only [List.nil_append,weight_nil]; omega | cons a as ih => simp only [List.cons_append,weight_cons]; omega lemma weight_reverse (as : List A) : weight as.reverse=weight as := by simp only [weight,size_encode_list,List.length_reverse,List.map_reverse,List.sum_reverse] lemma weight_prefix {as pre post : List A} (h : as=pre++post) : weight pre≤weight as := by have := weight_append pre post have := weight_pos post rw [h]; omega omit [Codec A] in lemma weight_map_bound {f : A → B} {as : List A} {N : ℕ} (h : ∀ a∈as,weight (f a)≤N) : weight (as.map f)≤as.length*(N+1)+1 := by induction as with | nil => simp | cons a as ih => have ht:=ih (fun b hb=>h b (by simp [hb])) have ha:=h a (by simp) simp only [List.map_cons,weight_cons,List.length_cons] nlinarith lemma polynomial_reverse : PolynomialTime (reverse (A:=A)) := by unfold reverse apply polynomial_congr apply polynomial_composition (polynomial_pair polynomial_identity (polynomial_constant [])) apply polynomial_fold polynomial_listCons (2*X+1) intro as bs pre post he have hp := weight_prefix he have h:=weight_append pre.reverse bs rw [weight_reverse] at h simp only [List.foldl_flip_cons_eq_append'] simp only [Polynomial.eval_add,Polynomial.eval_mul,Polynomial.eval_ofNat,Polynomial.eval_X,weight_prod] omega lemma polynomial_mulStep : PolynomialTime mulStepRealizer := by unfold mulStepRealizer apply polynomial_congr apply polynomial_pair (polynomial_composition polynomial_second polynomial_first) apply polynomial_composition _ polynomial_add apply polynomial_pair · exact polynomial_composition (polynomial_composition polynomial_second polynomial_second) polynomial_double · exact polynomial_choose polynomial_first (polynomial_composition polynomial_second polynomial_first) (polynomial_constant 0) lemma mulStep_size (b : Bool) (n c : ℕ) : (mulStep (b,(n,c))).2.size≤c.size+n.size+3 := by have hh:=nat_size_mul 2 c have ha:=nat_size_add (2*c) (if b then n else 0) have hb : (if b then n else 0).size≤n.size := by cases b <;> simp have h2 : Nat.size 2=2 := by decide have hm : max (2*c).size (if b then n else 0).size≤c.size+n.size+2 := max_le (by omega) (by omega) change (2*c+(if b then n else 0)).size≤_ omega lemma mulStep_fold_first (bs : List Bool) (n c : ℕ) : (bs.foldl (fun z b=>mulStep (b,z)) (n,c)).1=n := by induction bs generalizing c with | nil => rfl | cons b bs ih => exact ih _ lemma mulStep_fold_size (bs : List Bool) (n c : ℕ) : (bs.foldl (fun z b=>mulStep (b,z)) (n,c)).2.size≤c.size+bs.length*(n.size+3) := by induction bs generalizing c with | nil => simp | cons b bs ih => have hs:=mulStep_size b n c have ht:=ih (2*c+(if b then n else 0)) change (bs.foldl _ (mulStep (b,(n,c)))).2.size≤_ simp only [mulStep] at hs simp only [List.length_cons] change (bs.foldl _ (n,2*c+(if b then n else 0))).2.size≤_ nlinarith lemma polynomial_mulFold : PolynomialTime (fold mulStepRealizer) := by apply polynomial_fold polynomial_mulStep (10*(X+1)^2) rintro bs ⟨n,c⟩ pre post he let S:=weight (bs,(n,c)) have hpre : pre.length≤S := (list_length_le_weight pre).trans ((weight_prefix he).trans (by dsimp [S]; simp only [weight_prod]; omega)) have hn : n.size≤S := by have h:=size_le_weight n; dsimp [S]; simp only [weight_prod]; omega have hc : c.size≤S := by have h:=size_le_weight c; dsimp [S]; simp only [weight_prod]; omega have hn' : weight n≤S := by dsimp [S]; simp only [weight_prod]; omega have hs:=mulStep_fold_size pre n c have ho:=weight_nat_le (pre.foldl (fun z b=>mulStep (b,z)) (n,c)).2 have hr:=mulStep_fold_first pre n c rw [←Prod.eta (pre.foldl (fun z b=>mulStep (b,z)) (n,c)),weight_prod,hr] simp only [Polynomial.eval_mul,Polynomial.eval_ofNat,Polynomial.eval_pow,Polynomial.eval_add,Polynomial.eval_X,Polynomial.eval_one] change _≤10*(S+1)^2 have hmul : pre.length*(n.size+3)≤S*(S+3) := Nat.mul_le_mul hpre (by omega) nlinarith lemma polynomial_multiply : PolynomialTime multiply := by unfold multiply apply polynomial_congr apply polynomial_composition _ polynomial_second apply polynomial_composition _ polynomial_mulFold apply polynomial_pair · exact polynomial_composition (polynomial_composition polynomial_first polynomial_toBits) polynomial_reverse · exact polynomial_pair polynomial_second (polynomial_constant 0) end MatchingFPRAS.TreeTyped end OAI