File size: 5,962 Bytes
e63eb7f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 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
|