cfahlgren1's picture
download
raw
10.6 kB
import OAI.Combinatorics.EditApproximation.Binary.BinaryLocalGrid
namespace OAI
/-! Binary work for the actual bounded endpoint-index loops. The bound is
linear in the emitted one-dimensional grid size and quadratic in bit width. -/
namespace EditApproximation
private theorem wordValue_size_le (bits : List Bool) : Nat.size (bitWordValue bits) ≤ bits.length :=
Nat.size_le.mpr (bitWordValue_lt_pow_length bits)
theorem binaryNaturalAdd_size_le (a b : ℕ) :
Nat.size (a + b) ≤ max (Nat.size a) (Nat.size b) + 2 := by
have hv := wordValue_size_le (binaryNaturalAddWithWork a b).1
have hl := bitAddWithWork_length a.bits b.bits false
rw [binaryNaturalAddWithWork_value] at hv
have hsmall : Nat.size (a + b) ≤ max (Nat.size a) (Nat.size b) + 1 := by
simpa only [binaryNaturalAddWithWork, Nat.size_eq_bits_len] using hv.trans hl
omega
def gridPointsBitBudget (n B : ℕ) : ℕ :=
1000 * (B + 2) ^ 2 + n * (100 * (B + 3) ^ 2 + 5)
theorem gridPointsWithWork_cost (g lo hi B : ℕ) (hg : 0 < g)
(hgs : Nat.size g ≤ B) (hlo : Nat.size lo ≤ B) (hhi : Nat.size hi ≤ B) :
(gridPointsWithWork g lo hi).2 ≤ gridPointsBitBudget (gridPointsBetween g lo hi).length B := by
let lower := bitCeilDivWithWork g.bits lo.bits
let upper := binaryNaturalDivModWithWork hi g
let stop := binaryNaturalAddWithWork (bitWordValue upper.1) 1
let count := saturatingSubtractWithWork (bitWordValue stop.1) (bitWordValue lower.1)
let indices := List.range' (bitWordValue lower.1) (bitWordValue count.1)
have hlowers : Nat.size (bitWordValue lower.1) ≤ B + 2 := by
have h := (wordValue_size_le lower.1).trans (bitCeilDivWithWork_length g.bits lo.bits)
exact h.trans (by simpa only [Nat.size_eq_bits_len] using Nat.add_le_add_right hlo 2)
have huppers : Nat.size (bitWordValue upper.1) ≤ B := by
rw [(binaryNaturalDivModWithWork_spec hi g hg).1]
exact (Nat.size_le_size (Nat.div_le_self hi g)).trans hhi
have hstops : Nat.size (bitWordValue stop.1) ≤ B + 3 := by
rw [binaryNaturalAddWithWork_value]
have h := binaryNaturalAdd_size_le (bitWordValue upper.1) 1
have hone : Nat.size 1 = 1 := by decide
rw [hone] at h
omega
have hindex (i : ℕ) (hi' : i ∈ indices) : Nat.size i ≤ B + 3 := by
have hmem := List.mem_range'.mp hi'
have hcount : bitWordValue count.1 = bitWordValue stop.1 - bitWordValue lower.1 :=
saturatingSubtractWithWork_value _ _
obtain ⟨j, hj, hij⟩ := hmem
have hle : i ≤ bitWordValue stop.1 := by omega
exact (Nat.size_le_size hle).trans hstops
let points := arithmeticMapWithWork (fun i =>
let product := binaryNaturalMulWithWork g i
(bitWordValue product.1, product.2 + Nat.size i + 2)) indices
have hmap := arithmeticMapWithWork_cost (fun i =>
let product := binaryNaturalMulWithWork g i
(bitWordValue product.1, product.2 + Nat.size i + 2)) indices
(100 * (B + 3) ^ 2 + 2) (by
intro i hi'
have hcost := binaryNaturalMulWithWork_cost g i
have his := hindex i hi'
change (binaryNaturalMulWithWork g i).2 + Nat.size i + 2 ≤ _
nlinarith only [hcost, hgs, his, Nat.zero_le B])
have hlength : indices.length = (gridPointsBetween g lo hi).length := by
have hl : bitWordValue lower.1 = lo ⌈/⌉ g := by
-- The ceiling start is independent of the upper endpoint.
rw [bitCeilDivWithWork_value _ _ (by simpa only [bitWordValue_bits] using hg),
bitWordValue_bits, bitWordValue_bits]
exact naturalCeiling_div_eq_ceilDiv lo g hg
simp only [indices, List.length_range', count, saturatingSubtractWithWork_value,
stop, binaryNaturalAddWithWork_value, upper,
(binaryNaturalDivModWithWork_spec hi g hg).1, hl, gridPointsBetween_length]
rw [hlength] at hmap
change points.2 ≤ _ at hmap
simp only [Nat.add_assoc, Nat.reduceAdd] at hmap
have hlcost := bitCeilDivWithWork_cost g.bits lo.bits
have hucost := binaryNaturalDivModWithWork_cost hi g
have hscost := binaryNaturalAddWithWork_cost (bitWordValue upper.1) 1
have hccost := saturatingSubtractWithWork_cost (bitWordValue stop.1) (bitWordValue lower.1)
simp only [Nat.size_eq_bits_len] at hlcost
change lower.2 + upper.2.2 + stop.2 + count.2 +
points.2 + Nat.size g + Nat.size lo + Nat.size hi + 5 ≤ _
change lower.2 ≤ _ at hlcost
change upper.2.2 ≤ _ at hucost
change stop.2 ≤ _ at hscost
change count.2 ≤ _ at hccost
have hlpow := Nat.pow_le_pow_left (show Nat.size lo + Nat.size g + 1 ≤ 2 * B + 1 by omega) 2
have huprod := Nat.mul_le_mul (show Nat.size hi + 1 ≤ B + 1 by omega)
(show Nat.size hi + Nat.size g + 1 ≤ 2 * B + 1 by omega)
simp only [Nat.size_one] at hscost
have hsetup : lower.2 + upper.2.2 + stop.2 + count.2 +
Nat.size g + Nat.size lo + Nat.size hi + 5 ≤ 1000 * (B + 2) ^ 2 := by
nlinarith only [hlcost, hucost, hscost, hccost, hlpow, huprod, hgs, hlo, hhi,
hlowers, huppers, hstops, Nat.zero_le B]
unfold gridPointsBitBudget
omega
theorem makeStateWithWork_cost (n lo hi B : ℕ)
(hn : Nat.size n ≤ B) (hl : Nat.size lo ≤ B) (hh : Nat.size hi ≤ B) :
(makeStateWithWork n lo hi).2 ≤ 16 * B + 8 := by
have hfirst := wordLEWithWork_cost lo.bits hi.bits
have hsecond := wordLEWithWork_cost hi.bits n.bits
simp only [Nat.size_eq_bits_len] at hfirst hsecond
unfold makeStateWithWork
dsimp only
split_ifs <;> omega
def gridStateBoxBitBudget (count B : ℕ) : ℕ :=
96 * (B + 1) + 2 * gridPointsBitBudget count (B + 2) +
count * (count * (16 * (B + 2) + 11) + count + 3) + 6
theorem gridStateBoxWithWork_cost (n g radius lo hi B : ℕ) (hg : 0 < g)
(hn : Nat.size n ≤ B) (hgs : Nat.size g ≤ B) (hr : Nat.size radius ≤ B)
(hl : Nat.size lo ≤ B) (hh : Nat.size hi ≤ B) :
(gridStateBoxWithWork n g radius lo hi).2 ≤
gridStateBoxBitBudget (2 * radius / g + 2) B := by
let leftLow := saturatingSubtractWithWork lo radius
let leftHigh := binaryNaturalAddWithWork lo radius
let rightLow := saturatingSubtractWithWork hi radius
let rightHigh := binaryNaturalAddWithWork hi radius
let left := gridPointsWithWork g (bitWordValue leftLow.1) (bitWordValue leftHigh.1)
let right := gridPointsWithWork g (bitWordValue rightLow.1) (bitWordValue rightHigh.1)
let count := 2 * radius / g + 2
have hleft : left.1 = gridPointsBetween g (lo - radius) (lo + radius) := by
simp only [left, leftLow, leftHigh, gridPointsWithWork_value _ _ _ hg,
saturatingSubtractWithWork_value, binaryNaturalAddWithWork_value]
have hright : right.1 = gridPointsBetween g (hi - radius) (hi + radius) := by
simp only [right, rightLow, rightHigh, gridPointsWithWork_value _ _ _ hg,
saturatingSubtractWithWork_value, binaryNaturalAddWithWork_value]
have hlen (p : ℕ) : (gridPointsBetween g (p - radius) (p + radius)).length ≤ count := by
have h := gridPointsBetween_length_le g (p - radius) (p + radius) hg
have hsub : p + radius - (p - radius) ≤ 2 * radius := by omega
exact h.trans (Nat.add_le_add_right (Nat.div_le_div_right hsub) 2)
have hllen : left.1.length ≤ count := by rw [hleft]; exact hlen lo
have hrlen : right.1.length ≤ count := by rw [hright]; exact hlen hi
have hleftWidth (s : ℕ) (hs : s ∈ left.1) : Nat.size s ≤ B + 2 := by
have hp := (gridPointsBetween_sound hg (hleft ▸ hs)).2.1
exact (Nat.size_le_size hp).trans ((binaryNaturalAdd_size_le lo radius).trans (by omega))
have hrightWidth (s : ℕ) (hs : s ∈ right.1) : Nat.size s ≤ B + 2 := by
have hp := (gridPointsBetween_sound hg (hright ▸ hs)).2.1
exact (Nat.size_le_size hp).trans ((binaryNaturalAdd_size_le hi radius).trans (by omega))
have hstate (s : ℕ) (hs : s ∈ left.1) :
(filterMapWithWork (makeStateWithWork n s) right.1).2 ≤
count * (16 * (B + 2) + 11) := by
have h := filterMapWithWork_cost (makeStateWithWork n s) right.1 (16 * (B + 2) + 8)
(fun t ht => makeStateWithWork_cost n s t (B + 2) (by omega) (hleftWidth s hs) (hrightWidth t ht))
exact h.trans (Nat.mul_le_mul_right _ hrlen)
have hstateLen (s : ℕ) : (filterMapWithWork (makeStateWithWork n s) right.1).1.length ≤ count := by
rw [filterMapWithWork_value]
exact (List.length_filterMap_le _ _).trans hrlen
have hflat := arithmeticFlatMapWithWork_cost
(fun s => filterMapWithWork (makeStateWithWork n s) right.1) left.1
(count * (16 * (B + 2) + 11)) count hstate (fun s _ => hstateLen s)
have hflat' := hflat.trans (Nat.mul_le_mul_right _ hllen)
have hlc := gridPointsWithWork_cost g (bitWordValue leftLow.1) (bitWordValue leftHigh.1) (B + 2) hg
(by omega) (by rw [saturatingSubtractWithWork_value]; exact (Nat.size_le_size (Nat.sub_le _ _)).trans (by omega))
(by rw [binaryNaturalAddWithWork_value]; exact (binaryNaturalAdd_size_le lo radius).trans (by omega))
have hrc := gridPointsWithWork_cost g (bitWordValue rightLow.1) (bitWordValue rightHigh.1) (B + 2) hg
(by omega) (by rw [saturatingSubtractWithWork_value]; exact (Nat.size_le_size (Nat.sub_le _ _)).trans (by omega))
(by rw [binaryNaturalAddWithWork_value]; exact (binaryNaturalAdd_size_le hi radius).trans (by omega))
conv_rhs at hlc =>
simp only [leftLow, leftHigh, saturatingSubtractWithWork_value, binaryNaturalAddWithWork_value]
conv_rhs at hrc =>
simp only [rightLow, rightHigh, saturatingSubtractWithWork_value, binaryNaturalAddWithWork_value]
have hlc' : left.2 ≤ gridPointsBitBudget count (B + 2) := by
apply hlc.trans
unfold gridPointsBitBudget
exact Nat.add_le_add_left (Nat.mul_le_mul_right _ (hlen lo)) _
have hrc' : right.2 ≤ gridPointsBitBudget count (B + 2) := by
apply hrc.trans
unfold gridPointsBitBudget
exact Nat.add_le_add_left (Nat.mul_le_mul_right _ (hlen hi)) _
have hll := saturatingSubtractWithWork_cost lo radius
have hlh := binaryNaturalAddWithWork_cost lo radius
have hrl := saturatingSubtractWithWork_cost hi radius
have hrh := binaryNaturalAddWithWork_cost hi radius
change leftLow.2 ≤ _ at hll
change leftHigh.2 ≤ _ at hlh
change rightLow.2 ≤ _ at hrl
change rightHigh.2 ≤ _ at hrh
let states := arithmeticFlatMapWithWork (fun s => filterMapWithWork (makeStateWithWork n s) right.1) left.1
change states.2 ≤ _ at hflat'
change leftLow.2 + leftHigh.2 + rightLow.2 + rightHigh.2 + left.2 + right.2 + states.2 + 6 ≤ _
unfold gridStateBoxBitBudget
change _ ≤ 96 * (B + 1) + 2 * gridPointsBitBudget count (B + 2) +
count * (count * (16 * (B + 2) + 11) + count + 3) + 6
omega
end EditApproximation
end OAI

Xet Storage Details

Size:
10.6 kB
·
Xet hash:
8d887ff86067bc5752cf6cb1cae14c804756f6a02b2cbfc9193de477a6d82659

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.