import Mathlib import OAI.Combinatorics.MatchingCount.Arithmetic.BinaryNormalize namespace OAI namespace MatchingFPRAS.BinaryDivision open StackAddition (value value_bits) def low (b : Bool) (B R : List Bool) : Bool := decide (value (b::R) (Q,R) | b::A,B,Q,R => run A B (nextQ b B Q R) (nextR b B R) lemma value_bits_length (A : List Bool) : (value A).bits.length≤A.length := by simpa only [BinaryNormalize.normalize_bits] using BinaryNormalize.normalize_length A lemma bits_length_mono {m n : ℕ} (h : m≤n) : m.bits.length≤n.bits.length := by simpa only [Nat.size_eq_bits_len] using Nat.size_le_size h lemma nextR_length (b : Bool) (B R : List Bool) : (nextR b B R).length≤R.length+1 := by unfold nextR split_ifs · simp · exact (bits_length_mono (Nat.sub_le _ _)).trans (value_bits_length (b::R)) lemma nextQ_length (b : Bool) (B Q R : List Bool) : (nextQ b B Q R).length=Q.length+1 := rfl lemma step_remainder (b : Bool) (B R : List Bool) (h : value R decide unfold nextR low split_ifs with hl · simpa using hl · rw [value_bits] simp only [value] at * omega lemma step_mass (b : Bool) (B Q R : List Bool) : value (nextQ b B Q R)*value B+value (nextR b B R)= 2*(value Q*value B+value R)+b.toNat := by by_cases hh : value (b::R) simp [value] | cons a A ih => simp only [List.cons_append,value,ih,List.length_cons,pow_succ]; ring lemma value_reverse_cons (b : Bool) (A : List Bool) : value (b::A).reverse=value A.reverse+2^A.length*b.toNat := by rw [List.reverse_cons,value_append] simp [value,List.length_reverse] lemma run_mass (A B Q R : List Bool) : value (run A B Q R).1*value B+value (run A B Q R).2= (value Q*value B+value R)*2^A.length+value A.reverse := by induction A generalizing Q R with | nil => simp [run,value] | cons b A ih => rw [run,ih,step_mass,value_reverse_cons,List.length_cons,pow_succ] ring lemma run_remainder (A B Q R : List Bool) (h : value R exact h | cons b A ih => exact ih _ _ (step_remainder b B R h) lemma run_lengths (A B Q R : List Bool) : (run A B Q R).1.length=Q.length+A.length ∧ (run A B Q R).2.length≤R.length+A.length := by induction A generalizing Q R with | nil => simp [run] | cons b A ih => obtain ⟨hQ,hR⟩:=ih (nextQ b B Q R) (nextR b B R) have hr:=nextR_length b B R simp only [run,List.length_cons] rw [hQ,nextQ_length] exact ⟨by omega,by omega⟩ theorem correct (m n : ℕ) (hn : 0