import OAI.Combinatorics.EditApproximation.Binary.BinaryFractionWork import OAI.Combinatorics.EditApproximation.Binary.BinaryCanonicalWork import OAI.Combinatorics.EditApproximation.Definitions namespace OAI /-! Fractions retain their unreduced numerator and denominator, but discard leading zero bits after each operation. This exposes actual storage length without charging or performing rational normalization. -/ namespace EditApproximation namespace BinaryFraction theorem canonicalizeWithWork_representation (a : BinaryFraction) : (canonicalizeWithWork a).1.representation = a.representation := by apply UnreducedRational.eq_of_num_den · simp only [canonicalizeWithWork, representation, SignedBinary.value, trimBitWordWithWork_value] · exact trimBitWordWithWork_value a.denominator theorem canonicalizeWithWork_width (a : BinaryFraction) : (canonicalizeWithWork a).1.width = max (Nat.size a.representation.num.natAbs) (Nat.size a.representation.den) := by have hnum : a.numerator.value.natAbs = bitWordValue a.numerator.bits := by cases h : a.numerator.negative <;> simp [SignedBinary.value, signedMagnitude, h] simp only [canonicalizeWithWork, width, trimBitWordWithWork_length, representation, hnum] theorem canonicalizeWithWork_cost (a : BinaryFraction) : (canonicalizeWithWork a).2 ≤ 6 * a.width + 4 := by have hn : a.numerator.bits.length ≤ a.width := le_max_left _ _ have hd : a.denominator.length ≤ a.width := le_max_right _ _ simp only [canonicalizeWithWork, trimBitWordWithWork_cost] omega theorem canonicalAddWithWork_representation (a b : BinaryFraction) : (canonicalAddWithWork a b).1.representation = a.representation.add b.representation := by rw [canonicalAddWithWork, canonicalizeWithWork_representation, addWithWork_representation] theorem canonicalMulWithWork_representation (a b : BinaryFraction) : (canonicalMulWithWork a b).1.representation = a.representation.mul b.representation := by rw [canonicalMulWithWork, canonicalizeWithWork_representation, mulWithWork_representation] theorem rawAdd_width (a b : BinaryFraction) (B : ℕ) (ha : a.width ≤ B) (hb : b.width ≤ B) : (a.addWithWork b).1.width ≤ 3 * B + 2 := by have han : a.numerator.bits.length ≤ B := (le_max_left _ _).trans ha have had : a.denominator.length ≤ B := (le_max_right _ _).trans ha have hbn : b.numerator.bits.length ≤ B := (le_max_left _ _).trans hb have hbd : b.denominator.length ≤ B := (le_max_right _ _).trans hb have hfirst := SignedBinary.mulWithWork_length a.numerator b.denominatorInteger have hsecond := SignedBinary.mulWithWork_length b.numerator a.denominatorInteger have hsum := SignedBinary.addWithWork_length (a.numerator.mulWithWork b.denominatorInteger).1 (b.numerator.mulWithWork a.denominatorInteger).1 have hden := bitMulWithWork_length a.denominator b.denominator simp only [denominatorInteger] at hfirst hsecond dsimp only [addWithWork, width, denominatorInteger] simp only [denominatorInteger] at hsum apply max_le <;> omega theorem rawMul_width (a b : BinaryFraction) (B : ℕ) (ha : a.width ≤ B) (hb : b.width ≤ B) : (a.mulWithWork b).1.width ≤ 3 * B + 1 := by have han : a.numerator.bits.length ≤ B := (le_max_left _ _).trans ha have had : a.denominator.length ≤ B := (le_max_right _ _).trans ha have hbn : b.numerator.bits.length ≤ B := (le_max_left _ _).trans hb have hbd : b.denominator.length ≤ B := (le_max_right _ _).trans hb have hnum := SignedBinary.mulWithWork_length a.numerator b.numerator have hden := bitMulWithWork_length a.denominator b.denominator dsimp only [mulWithWork, width] apply max_le <;> omega theorem canonicalAddWithWork_cost (a b : BinaryFraction) (B : ℕ) (ha : a.width ≤ B) (hb : b.width ≤ B) : (canonicalAddWithWork a b).2 ≤ 6000 * (B + 1) ^ 2 := by have hr := addWithWork_cost a b B ha hb have hw := rawAdd_width a b B ha hb have hc := canonicalizeWithWork_cost (a.addWithWork b).1 change (a.addWithWork b).2 + (canonicalizeWithWork (a.addWithWork b).1).2 ≤ _ nlinarith only [hr, hw, hc] theorem canonicalMulWithWork_cost (a b : BinaryFraction) (B : ℕ) (ha : a.width ≤ B) (hb : b.width ≤ B) : (canonicalMulWithWork a b).2 ≤ 2000 * (B + 1) ^ 2 := by have hr := mulWithWork_cost a b B ha hb have hw := rawMul_width a b B ha hb have hc := canonicalizeWithWork_cost (a.mulWithWork b).1 change (a.mulWithWork b).2 + (canonicalizeWithWork (a.mulWithWork b).1).2 ≤ _ nlinarith only [hr, hw, hc] theorem canonicalizeWithWork_width_le_storageBits (a : BinaryFraction) : (canonicalizeWithWork a).1.width ≤ a.representation.storageBits := by rw [canonicalizeWithWork_width] unfold UnreducedRational.storageBits exact max_le (by omega) (by omega) theorem canonicalAddWithWork_width_le_storageBits (a b : BinaryFraction) : (canonicalAddWithWork a b).1.width ≤ (a.representation.add b.representation).storageBits := by have h := canonicalizeWithWork_width_le_storageBits (a.addWithWork b).1 rw [addWithWork_representation] at h exact h theorem canonicalMulWithWork_width_le_storageBits (a b : BinaryFraction) : (canonicalMulWithWork a b).1.width ≤ (a.representation.mul b.representation).storageBits := by have h := canonicalizeWithWork_width_le_storageBits (a.mulWithWork b).1 rw [mulWithWork_representation] at h exact h theorem canonicalAddWithWork_self_width (a b : BinaryFraction) : (canonicalAddWithWork a b).1.width ≤ (canonicalAddWithWork a b).1.representation.storageBits := by rw [canonicalAddWithWork_representation] exact canonicalAddWithWork_width_le_storageBits a b theorem canonicalMulWithWork_self_width (a b : BinaryFraction) : (canonicalMulWithWork a b).1.width ≤ (canonicalMulWithWork a b).1.representation.storageBits := by rw [canonicalMulWithWork_representation] exact canonicalMulWithWork_width_le_storageBits a b end BinaryFraction end EditApproximation end OAI