import Mathlib.Data.Nat.Size import Mathlib.Tactic import OAI.Combinatorics.EditApproximation.Definitions namespace OAI /-! Literal Boolean ripple addition. The counter counts Boolean gates and bit writes, not unit-cost arithmetic on unbounded integers. -/ namespace EditApproximation theorem bitAdder_value (a b carry : Bool) : (bitSum a b carry).toNat + 2 * (bitCarry a b carry).toNat = a.toNat + b.toNat + carry.toNat := by cases a <;> cases b <;> cases carry <;> decide theorem bitAddWithWork_value (left right : List Bool) (carry : Bool) : bitWordValue (bitAddWithWork left right carry).1 = bitWordValue left + bitWordValue right + carry.toNat := by induction left generalizing right carry with | nil => simp only [bitAddWithWork] induction right generalizing carry with | nil => cases carry <;> simp [bitAddNilLeftWithWork, bitWordValue] | cons b bs ih => simp only [bitAddNilLeftWithWork, bitWordValue, ih] have h := bitAdder_value false b carry simp only [Bool.toNat_false] at h omega | cons a as ih => cases right with | nil => simp only [bitAddWithWork, bitWordValue, ih] have h := bitAdder_value a false carry simp only [Bool.toNat_false] at h omega | cons b bs => simp only [bitAddWithWork, bitWordValue, ih] have h := bitAdder_value a b carry omega theorem bitAddWithWork_cost (left right : List Bool) (carry : Bool) : (bitAddWithWork left right carry).2 ≤ 9 * (left.length + right.length) + 1 := by induction left generalizing right carry with | nil => simp only [bitAddWithWork] induction right generalizing carry with | nil => cases carry <;> simp [bitAddNilLeftWithWork] | cons b bs ih => simp only [bitAddNilLeftWithWork, List.length_cons, List.length_nil] have h := ih (bitCarry false b carry) simp only [List.length_nil] at h omega | cons a as ih => cases right with | nil => simp only [bitAddWithWork, List.length_cons, List.length_nil] have h := ih [] (bitCarry a false carry) simp only [List.length_nil] at h omega | cons b bs => simp only [bitAddWithWork, List.length_cons] have h := ih bs (bitCarry a b carry) omega theorem bitAddWithWork_length (left right : List Bool) (carry : Bool) : (bitAddWithWork left right carry).1.length ≤ max left.length right.length + 1 := by induction left generalizing right carry with | nil => simp only [bitAddWithWork] induction right generalizing carry with | nil => cases carry <;> simp [bitAddNilLeftWithWork] | cons b bs ih => simp only [bitAddNilLeftWithWork, List.length_cons, List.length_nil, Nat.zero_max] have h := ih (bitCarry false b carry) simp only [List.length_nil, Nat.zero_max] at h omega | cons a as ih => cases right with | nil => simp only [bitAddWithWork, List.length_cons, List.length_nil, Nat.max_zero] have h := ih [] (bitCarry a false carry) simp only [List.length_nil, Nat.max_zero] at h omega | cons b bs => simp only [bitAddWithWork, List.length_cons] have h := ih bs (bitCarry a b carry) omega theorem bitWordValue_bits (n : ℕ) : bitWordValue n.bits = n := by induction n using Nat.binaryRec' with | zero => simp [bitWordValue] | bit bit n h ih => rw [Nat.bits_append_bit n bit h] cases bit <;> simp [bitWordValue, ih, Nat.bit, Nat.add_comm] theorem binaryNaturalAddWithWork_value (a b : ℕ) : bitWordValue (binaryNaturalAddWithWork a b).1 = a + b := by simp [binaryNaturalAddWithWork, bitAddWithWork_value, bitWordValue_bits] theorem binaryNaturalAddWithWork_cost (a b : ℕ) : (binaryNaturalAddWithWork a b).2 ≤ 9 * (Nat.size a + Nat.size b) + 1 := by simpa only [binaryNaturalAddWithWork, Nat.size_eq_bits_len] using bitAddWithWork_cost a.bits b.bits false theorem bitMulWithWork_value (left right : List Bool) : bitWordValue (bitMulWithWork left right).1 = bitWordValue left * bitWordValue right := by induction left with | nil => simp [bitMulWithWork, bitWordValue] | cons bit bits ih => cases bit <;> simp [bitMulWithWork, bitWordValue, bitAddWithWork_value, ih] <;> ring theorem bitMulWithWork_length (left right : List Bool) : (bitMulWithWork left right).1.length ≤ 2 * left.length + right.length + 1 := by induction left with | nil => simp [bitMulWithWork] | cons bit bits ih => cases bit · simp only [bitMulWithWork, Bool.false_eq_true, ↓reduceIte, List.length_cons] omega · simp only [bitMulWithWork, ↓reduceIte, List.length_cons] have h := bitAddWithWork_length (false :: (bitMulWithWork bits right).1) right false simp only [List.length_cons] at h omega theorem bitMulWithWork_cost (left right : List Bool) : (bitMulWithWork left right).2 ≤ 20 * (left.length + 1) * (left.length + right.length + 1) := by induction left with | nil => simp [bitMulWithWork]; omega | cons bit bits ih => have hlength := bitMulWithWork_length bits right have hadd := bitAddWithWork_cost (false :: (bitMulWithWork bits right).1) right false simp only [List.length_cons] at hadd cases bit · simp only [bitMulWithWork, Bool.false_eq_true, ↓reduceIte, List.length_cons] nlinarith only [ih] · simp only [bitMulWithWork, ↓reduceIte, List.length_cons] nlinarith only [ih, hlength, hadd] theorem binaryNaturalMulWithWork_value (a b : ℕ) : bitWordValue (binaryNaturalMulWithWork a b).1 = a * b := by simp [binaryNaturalMulWithWork, bitMulWithWork_value, bitWordValue_bits] theorem binaryNaturalMulWithWork_cost (a b : ℕ) : (binaryNaturalMulWithWork a b).2 ≤ 20 * (Nat.size a + 1) * (Nat.size a + Nat.size b + 1) := by simpa only [binaryNaturalMulWithWork, Nat.size_eq_bits_len] using bitMulWithWork_cost a.bits b.bits end EditApproximation end OAI