/- Released under Apache 2.0 license as described in the file LICENSE. -/ import FormalConflictRange.NegCrossCompleteHigh /-! # The clamping finite crossed negative-begin complete high cell This module closes the last crossed negative-begin complete cell: `begin.offset < 0`, `end.offset ≥ 2`, `end.key < head`, `reportedBelowBegin ≤ beginSlack` and `#reported = (end.offset - 1).toNat`. Writing `r = reportedBelowBegin`, `q = beginSlack`, `t = #reported`, `C = q + 1 - r` and `K = keyAfter (last reported)`, the minimal range is * `[⊥, begin.key)` when `[K, begin.key)` holds at least `C` keys (at least `C + 1` when `end.key` is the empty key), * `[⊥, K)` otherwise. The proof separates the two possible begin resolutions (`⊥` and the reported head). In the second case the database stores exactly `C` extra keys above the reported last before `begin.key`; in the first case it stores at most `C - 1` such keys. This gives a complete description of compatibility by the keys below `begin.key`, which yields soundness. Necessity inside `[K, begin.key)` is shown by inserting one more gap key (or, in the tight-capacity case, by removing one together with a key below `end.key`). -/ namespace FormalConflictRange variable {db db₁ db₂ : DB} {r : RangeRead} {res : Result} /-- In the clamping finite crossed negative-begin complete high cell, the begin resolution is either the empty key or the reported head. -/ theorem clamp_begin_resolve_bot_or_head (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (_hbegin : r.begin.offset < 0) (_hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) : resolve db r.begin = (([] : Key) : WithTop Key) ∨ resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by set H : Key := res.keys.headD ([] : Key) with hH have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hwin : res.keys = RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by rw [hres, completeKeys_eq_inRange_of_not_rev hrev] have hHin : H ∈ db.keys ∧ resolve db r.begin ≤ ((H : Key) : WithTop Key) ∧ ((H : Key) : WithTop Key) < resolve db r.end_ := by have h := hwin ▸ hHmem simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using h by_cases hneg : r.begin.anchor db < 0 · left; exact resolve_eq_nil hneg · have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hneg rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with hnil | htop | ⟨k, hkdb, hresk⟩ · left; exact hnil · rw [htop] at hHin exact absurd hHin.2.1 (not_le.mpr (WithTop.coe_lt_top H)) · right have hle : ((k : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := by rw [← hresk]; exact hHin.2.1 have hkH : k ≤ H := WithTop.coe_le_coe.mp hle have hkwin : k ∈ RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by rw [RangeRead.inRange, List.mem_filter] refine ⟨hkdb, ?_⟩ rw [decide_eq_true_eq] refine ⟨?_, ?_⟩ · exact le_of_eq hresk · exact lt_of_le_of_lt hle hHin.2.2 have hkres : k ∈ res.keys := by rw [hwin]; exact hkwin have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hHle : H ≤ k := by rw [hH, hHD] exact head_le_of_sortedLT' hsort hne hkres have hkH' : k ≤ H := WithTop.coe_le_coe.mp hle have hkH : k = H := le_antisymm hkH' hHle rw [hresk, hkH] /-- In the crossed negative-begin complete high cell with a finite end, if the begin resolution is the reported head then the count below the head equals the count below the end key. -/ theorem high_cnt_head_eq_cnt_end_of_resolve (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) = cnt db r.end_.key := by set H : Key := res.keys.headD ([] : Key) with hH have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne have hhead_mem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hres_eq : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by simpa [hH] using hhead have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL] rfl have hhead_lt_hi : ((H : Key) : WithTop Key) < resolve db r.end_ := by have hmem : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by rw [← hwin]; exact hhead_mem have hcond := of_decide_eq_true (List.mem_filter.mp hmem).2 exact hcond.2 have hcnt_hi : cnt db (resolve db r.end_) = cnt db (((H : Key) : WithTop Key)) + res.keys.length := by rw [cnt_eq_add_between db (le_of_lt hhead_lt_hi), ← hwin] have hcnt_hi_le : cnt db (resolve db r.end_) ≤ cnt db r.end_.key + (r.end_.offset - 1).toNat := by have h : cnt db (resolve db r.end_) = min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length := by rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl, countP_lt_resolve (db := db) (s := r.end_), anchor_toNat_eq (s := r.end_) (by omega)] rfl rw [h]; exact Nat.min_le_left _ _ rw [hcnt_hi, hfinite] at hcnt_hi_le have hcntE_le : cnt db r.end_.key ≤ cnt db (((H : Key) : WithTop Key)) := cnt_le_cnt_of_le db (le_of_lt hEH) omega /-- The begin-resolution alternatives together with the window description give that no stored key lies in `[end.key, head)` in the clamping finite high cell. -/ theorem clamp_between_end_head_eq_nil (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : between db r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key) = [] := by classical set H : Key := res.keys.headD ([] : Key) with hH have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hwin : res.keys = RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by rw [hres, completeKeys_eq_inRange_of_not_rev hrev] rcases clamp_begin_resolve_bot_or_head hcomp hrev hmore hne hbegin hcrossD with hbot | hhead · -- The begin resolution is `⊥`; the window starts at `⊥` and contains nothing -- below the reported head. rw [between, List.filter_eq_nil_iff] intro x hxdb hdec have hxEH : r.end_.key ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec have hHin : H ∈ db.keys ∧ resolve db r.begin ≤ ((H : Key) : WithTop Key) ∧ ((H : Key) : WithTop Key) < resolve db r.end_ := by have h := hwin ▸ hHmem simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using h have hxwin : x ∈ RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by rw [RangeRead.inRange, List.mem_filter] refine ⟨hxdb, ?_⟩ rw [decide_eq_true_eq] refine ⟨?_, lt_trans hxEH.2 hHin.2.2⟩ rw [hbot] exact WithTop.coe_le_coe.mpr (nil_le_key x) have hxres : x ∈ res.keys := by rw [hwin]; exact hxwin have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hHle : H ≤ x := by rw [hH, hHD] exact head_le_of_sortedLT' hsort hne hxres exact absurd (WithTop.coe_lt_coe.mp hxEH.2) (not_lt.mpr hHle) · -- The begin resolution is the reported head. Count below the head equals the -- count below the end key. have hcntHE : cnt db (((H : Key) : WithTop Key)) = cnt db r.end_.key := high_cnt_head_eq_cnt_end_of_resolve hcomp hrev hmore hne hend hEH hhead hfinite rw [between, List.filter_eq_nil_iff] intro x hxdb hdec have hxEH : r.end_.key ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec have hsplit := cnt_eq_add_between db (le_of_lt hEH) rw [hcntHE] at hsplit simp only [between] at hsplit have hlen : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)))).length = 0 := by omega have hfilter_nil := List.length_eq_zero_iff.mp hlen have hxmem : x ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) := List.mem_filter.mpr ⟨hxdb, hdec⟩ rw [hfilter_nil] at hxmem exact absurd hxmem (by simp) /-- The slack `C = beginSlack + 1 - reportedBelowBegin` is positive in the clamping cell. -/ theorem clampHigh_C_pos (hclamp : reportedBelowBegin r res ≤ beginSlack r) : 0 < clampHighC r res := by unfold clampHighC omega /-- If the capacity is below the threshold, then it is at most `C`, and either it is strictly below `C` or the end key is the empty key. -/ theorem clampHigh_thresh_gt_facts (hth : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : clampHighCap r res ≤ (clampHighC r res : ℕ∞) ∧ (clampHighCap r res < (clampHighC r res : ℕ∞) ∨ r.end_.key = (([] : Key) : WithTop Key)) := by have hlt : clampHighCap r res < ((clampHighC r res + (if r.end_.key = (([] : Key) : WithTop Key) then 1 else 0) : ℕ) : ℕ∞) := lt_of_not_ge hth by_cases hE : r.end_.key = (([] : Key) : WithTop Key) · have hlt' : clampHighCap r res < (clampHighC r res : ℕ∞) + 1 := by simpa [clampHighThresh, hE, Nat.cast_add, Nat.cast_one] using hlt have hcapne : clampHighCap r res ≠ (⊤ : ℕ∞) := by intro htop rw [htop] at hlt' exact not_top_lt hlt' exact ⟨(ENat.lt_add_one_iff (m := clampHighCap r res) (n := (clampHighC r res : ℕ∞)) (by simp)).mp hlt', Or.inr hE⟩ · have hlt' : clampHighCap r res < (clampHighC r res : ℕ∞) := by simpa [clampHighThresh, hE] using hlt exact ⟨le_of_lt hlt', Or.inl hlt'⟩ /-- Every reported key lies strictly below `keyAfter (last reported)`. -/ theorem clampHigh_reported_lt_keyAfter {_r : RangeRead} {res : Result} (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) := by intro k hk have hklast : k ≤ res.keys.getLastD ([] : Key) := by rw [getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_getLast hk exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hklast (keyAfter_self_lt _)) /-- In the crossed negative-begin complete high cell, the reported keys are exactly the stored keys in `[head, keyAfter last)`. -/ theorem clamp_keys_HK_eq {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) : between db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) = res.keys := by classical set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set a : WithTop Key := resolve db r.begin with ha set z : WithTop Key := resolve db r.end_ with hz have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hwin : res.keys = between db a z := by rw [hres, completeKeys_eq_inRange_of_not_rev hrev]; rfl have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne have haH : a ≤ ((H : Key) : WithTop Key) := by have h := hwin ▸ hHmem exact (of_decide_eq_true (List.mem_filter.mp h).2).1 have hLz : ((L : Key) : WithTop Key) < z := by have h := hwin ▸ hLmem exact (of_decide_eq_true (List.mem_filter.mp h).2).2 have hKz : ((K : Key) : WithTop Key) ≤ z := by rw [hK]; exact keyAfter_le_of_lt hLz refine List.Subset.antisymm_of_sortedLT ?_ ?_ (List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)) hsort · intro k hk rw [between, List.mem_filter] at hk obtain ⟨hkdb, hkcond⟩ := hk rw [decide_eq_true_eq] at hkcond obtain ⟨hkH, hkK⟩ := hkcond have hkz : ((k : Key) : WithTop Key) < z := lt_of_lt_of_le hkK hKz have hmem : k ∈ between db a z := by rw [between, List.mem_filter] exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨le_trans haH hkH, hkz⟩⟩ rw [hwin]; exact hmem · intro k hk rw [between, List.mem_filter] have hkdb : k ∈ db.keys := RangeRead.compatible_keys_subset hcomp hk have hHle : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := WithTop.coe_le_coe.mpr (by rw [hH, headD_eq_head_of_ne hne] exact head_le_of_sortedLT' hsort hne hk) have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := clampHigh_reported_lt_keyAfter (_r := r) hsort hne k hk exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hHle, hkK⟩⟩ /-- No stored key lies in `[keyAfter last, resolve end)` in the crossed negative-begin complete high cell. -/ theorem clamp_between_K_resolve_end_eq_nil {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) : between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) (resolve db r.end_) = [] := by classical set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set a : WithTop Key := resolve db r.begin with ha set z : WithTop Key := resolve db r.end_ with hz have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hwin : res.keys = between db a z := by rw [hres, completeKeys_eq_inRange_of_not_rev hrev]; rfl have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have haH : a ≤ ((H : Key) : WithTop Key) := by have h := hwin ▸ hHmem exact (of_decide_eq_true (List.mem_filter.mp h).2).1 have hall_le_L : ∀ k ∈ res.keys, k ≤ L := by intro k hk rw [hL, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_getLast hk have hHL : H ≤ L := by rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) rw [between, List.filter_eq_nil_iff] intro x hxdb hdec have hxcond : ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < z := of_decide_eq_true hdec have hKx : K ≤ x := WithTop.coe_le_coe.mp hxcond.1 have hHx : H ≤ x := le_trans hHL (le_trans (by rw [hK]; exact le_of_lt (keyAfter_self_lt L)) hKx) have hmem : x ∈ between db a z := by rw [between, List.mem_filter] refine ⟨hxdb, ?_⟩ rw [decide_eq_true_eq] exact ⟨le_trans haH (WithTop.coe_le_coe.mpr hHx), hxcond.2⟩ rw [← hwin] at hmem have hxL : x ≤ L := hall_le_L x hmem exact absurd (le_trans hKx hxL) (not_le.mpr (by rw [hK]; exact keyAfter_self_lt L)) /-- The number of stored keys below `begin.key` in the clamping finite crossed high cell is `#reported + #gap`, splitting at `keyAfter last`, provided `keyAfter last` lies below `begin.key`. This is the counting core of the `U = keyAfter last` branch. -/ theorem clamp_cnt_begin_split {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) (hKleB : ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) ≤ r.begin.key) : cnt db r.begin.key = cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) + res.keys.length + (between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) r.begin.key).length := by classical set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK have hHK : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := clamp_keys_HK_eq hcomp hrev hmore hne have hHL : H ≤ L := by have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) have hHKle : ((H : Key) : WithTop Key) ≤ ((K : Key) : WithTop Key) := by rw [WithTop.coe_le_coe, hK] exact le_trans hHL (le_of_lt (keyAfter_self_lt L)) have hHleB : ((H : Key) : WithTop Key) ≤ r.begin.key := by rw [hH]; exact le_of_lt hcrossD have hKleB' : ((K : Key) : WithTop Key) ≤ r.begin.key := by rw [hK]; exact hKleB have hsplit1 := cnt_eq_add_between db hHKle have hsplit2 := cnt_eq_add_between db hKleB' rw [hsplit2, hsplit1, hHK] /-- If the begin resolution is the reported head, the begin count below `begin.key` equals the count below the head plus the slack plus one. -/ theorem clamp_anchor_eq_cnt_head_of_resolve {db : DB} {r : RangeRead} {res : Result} (hbegin : r.begin.offset < 0) (_hne : res.keys ≠ []) (hHne : res.keys.headD ([] : Key) ≠ []) (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : cnt db r.begin.key = cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) + beginSlack r + 1 := by set H : Key := res.keys.headD ([] : Key) with hH have hHne' : H ≠ [] := by rw [hH]; exact hHne have hanc := anchor_eq_count_sub_slack db r (le_of_lt hbegin) have hHidx : r.begin.anchor db = (cnt db ((H : Key) : WithTop Key) : ℤ) := by by_cases hneg : r.begin.anchor db < 0 · rw [resolve_eq_nil hneg] at hhead have hHeq : H = [] := WithTop.coe_injective hhead.symm exact absurd hHeq hHne' · have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hneg by_cases hltlen : (r.begin.anchor db).toNat < db.keys.length · have hget := resolve_eq_getElem (db := db) (s := r.begin) h0 hltlen rw [hhead] at hget have hidx : db.keys[(r.begin.anchor db).toNat] = H := WithTop.coe_injective hget.symm have hcnt := countP_lt_eq_of_getElem db.sorted hltlen hidx have hcoe : cnt db ((H : Key) : WithTop Key) = db.keys.countP (fun k : Key => decide (k < H)) := countP_lt_coe H rw [hcoe, hcnt, Int.toNat_of_nonneg h0] · have htop := resolve_eq_top h0 (le_of_not_gt hltlen) rw [hhead] at htop exact absurd htop (WithTop.coe_ne_top (a := H)) have hEq : (cnt db r.begin.key : ℤ) = (cnt db ((H : Key) : WithTop Key) : ℤ) + (beginSlack r : ℤ) + 1 := by rw [hHidx] at hanc simp only [cnt] at hanc ⊢ linarith have hgoal : ((cnt db r.begin.key : ℕ) : ℤ) = ((cnt db ((H : Key) : WithTop Key) + beginSlack r + 1 : ℕ) : ℤ) := by push_cast linarith exact_mod_cast hgoal /-- In the `U = keyAfter last` branch with `end.key ≠ ⊥`, the begin resolution is not the reported head: that would require at least `clampHighC` gap keys, hence capacity at least the threshold. -/ theorem clampHigh_begin_resolve_ne_head {db : DB} {r : RangeRead} {res : Result} (_hread : Readable r res) (hcomp : RangeRead.Compatible db r res) (hcell : negCrossCompleteHighClampFinite r res) (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) (hEbot : r.end_.key ≠ (([] : Key) : WithTop Key)) : resolve db r.begin ≠ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by intro hhead have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK have hHne : res.keys.headD ([] : Key) ≠ [] := by intro h have hlt : r.end_.key < (([] : Key) : WithTop Key) := by rw [← h]; exact hEH exact absurd hlt (not_lt.mpr (coe_nil_le_key_withTop r.end_.key)) have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hcntB : cnt db r.begin.key = cnt db ((H : Key) : WithTop Key) + beginSlack r + 1 := clamp_anchor_eq_cnt_head_of_resolve (r := r) (res := res) hbegin hne hHne (by rw [← hH]; exact hhead) have hHK : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := clamp_keys_HK_eq hcomp hrev hmore hne have hHleB : ((H : Key) : WithTop Key) ≤ r.begin.key := by rw [hH]; exact le_of_lt hcrossD -- Split on the relative position of `begin.key`. by_cases hBK : r.begin.key ≤ ((K : Key) : WithTop Key) · -- `B ≤ K`: the stored keys `[H, B)` are the reported keys below `B`, count `rb` have hsplit := cnt_eq_add_between db hHleB have hfilter : between db ((H : Key) : WithTop Key) r.begin.key = res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by calc between db ((H : Key) : WithTop Key) r.begin.key = (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by rw [between, between, List.filter_filter] apply List.filter_congr intro k _ by_cases hlt : ((k : Key) : WithTop Key) < r.begin.key · have hleK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := lt_of_lt_of_le hlt hBK simp [hlt, hleK] · simp [hlt] _ = _ := by rw [hHK] have hlen : (between db ((H : Key) : WithTop Key) r.begin.key).length = reportedBelowBegin r res := by rw [hfilter, reportedBelowBegin, ← List.countP_eq_length_filter] have hcntEq : cnt db r.begin.key = cnt db ((H : Key) : WithTop Key) + reportedBelowBegin r res := by rw [hsplit, hlen] have hrbeq : reportedBelowBegin r res = beginSlack r + 1 := by omega have hC0 : clampHighC r res = 0 := by unfold clampHighC; omega have hCpos := clampHigh_C_pos (r := r) (res := res) hclamp omega · -- `K < B`: `t + gap = q + 1`, so `cap ≥ C` push Not at hBK have hKleB : ((K : Key) : WithTop Key) ≤ r.begin.key := le_of_lt hBK have hsplit := clamp_cnt_begin_split hcomp hrev hmore hne hcrossD (by rw [← hK]; exact hKleB) have hrb : reportedBelowBegin r res = res.keys.length := by unfold reportedBelowBegin rw [List.countP_eq_length] intro k hk rw [decide_eq_true_eq] have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by simpa [hK, hL] using clampHigh_reported_lt_keyAfter (_r := r) hsort hne k hk exact lt_trans hkK hBK have hgap : (between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) r.begin.key).length = clampHighC r res := by have hcntB' : cnt db r.begin.key = cnt db (((res.keys.headD ([] : Key)) : WithTop Key)) + beginSlack r + 1 := by simpa [hH] using hcntB have hgapEq : res.keys.length + (between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) r.begin.key).length = beginSlack r + 1 := by have h := hsplit rw [hcntB'] at h omega unfold clampHighC rw [hrb] omega have hle := between_length_le_intervalCard (db := db) (E := (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key))) (B := r.begin.key) hKleB rw [hgap] at hle -- `thresh = C` since `E ≠ ⊥` have hth : clampHighThresh r res = clampHighC r res := by unfold clampHighThresh rw [ite_eq_right hEbot] exact Nat.add_zero _ apply hnotcap rw [hth] exact hle /-- In the `U = keyAfter last` branch (`¬ thresh ≤ cap`), every compatible database has exactly the reported keys below `keyAfter last`. -/ theorem clampHigh_K_filter_of_compatible {db : DB} {r : RangeRead} {res : Result} (hread : Readable r res) (hcomp : RangeRead.Compatible db r res) (hcell : negCrossCompleteHighClampFinite r res) (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys := by classical have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set E : WithTop Key := r.end_.key with hE have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hres_eq : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hwin : res.keys = between db (resolve db r.begin) (resolve db r.end_) := by rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev]; rfl have hHK : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := clamp_keys_HK_eq hcomp hrev hmore hne have hKz : between db ((K : Key) : WithTop Key) (resolve db r.end_) = [] := clamp_between_K_resolve_end_eq_nil hcomp hrev hmore hne have hEHn : between db E ((H : Key) : WithTop Key) = [] := by rw [hE, hH] exact clamp_between_end_head_eq_nil hcomp hrev hmore hne hbegin hend hEH hcrossD hfinite -- `H ≤ L` and `H < K`. have hHL : H ≤ L := by rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) have hHz : ((H : Key) : WithTop Key) < resolve db r.end_ := by have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne have hLz : ((L : Key) : WithTop Key) < resolve db r.end_ := by have h := hwin ▸ hLmem exact (of_decide_eq_true (List.mem_filter.mp h).2).2 exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hHL) hLz refine eq_of_forall_mem_iff_of_pairwise (db.sorted.pairwise.filter _) hsort.pairwise ?_ intro k constructor · -- stored keys below `K` are reported intro hx rw [List.mem_filter] at hx obtain ⟨hxdb, hxdec⟩ := hx rw [decide_eq_true_eq] at hxdec by_cases hxH : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) · have : k ∈ between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) := by rw [between, List.mem_filter] exact ⟨hxdb, by rw [decide_eq_true_eq]; exact ⟨hxH, hxdec⟩⟩ rw [hHK] at this; exact this · -- `k < H`: ruled out have hxltH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) := lt_of_not_ge hxH by_cases hEbot : E = (([] : Key) : WithTop Key) · have hxmem : k ∈ between db E ((H : Key) : WithTop Key) := by rw [between, List.mem_filter] refine ⟨hxdb, ?_⟩ rw [decide_eq_true_eq] exact ⟨by rw [hEbot]; exact coe_nil_le_key_withTop _, hxltH⟩ rw [hEHn] at hxmem exact absurd hxmem (by simp) · -- `E ≠ ⊥`: show the begin resolution is `⊥`, so `k` is in the window have hbot : resolve db r.begin = (([] : Key) : WithTop Key) := by rcases clamp_begin_resolve_bot_or_head hcomp hrev hmore hne hbegin (by rw [← hH]; exact hcrossD) with hab | hah · exact hab · exact absurd hah (clampHigh_begin_resolve_ne_head hread hcomp hcell hnotcap (by rw [← hE]; exact hEbot)) -- with `a = ⊥`, `k` lies in the window have hxwin : k ∈ between db (resolve db r.begin) (resolve db r.end_) := by rw [between, List.mem_filter] refine ⟨hxdb, ?_⟩ rw [decide_eq_true_eq] exact ⟨by rw [hbot]; exact coe_nil_le_key_withTop _, lt_trans hxltH hHz⟩ rw [← hwin] at hxwin have hHle : H ≤ k := by rw [hH, headD_eq_head_of_ne hne] exact head_le_of_sortedLT' hsort hne hxwin exact absurd (WithTop.coe_le_coe.mpr hHle) (not_le.mpr hxltH) · -- reported keys are stored below `K` intro hk rw [List.mem_filter] exact ⟨RangeRead.compatible_keys_subset hcomp hk, by rw [decide_eq_true_eq] exact clampHigh_reported_lt_keyAfter (_r := r) hsort hne k hk⟩ /-- Filtering at `⊥` is the same as dropping the lower-bound conjunct. -/ theorem clampHigh_filter_bot_eq (db : DB) (U : WithTop Key) : db.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < U)) = db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < U)) := by apply List.filter_congr intro k _ rw [decide_eq_decide] exact ⟨fun h => h.2, fun h => ⟨coe_nil_le_key_withTop _, h⟩⟩ /-- In the `U = keyAfter last` branch, no stored key lies below the reported head. -/ theorem clampHigh_no_below_head_of_filter {db : DB} {res : Result} {H K : Key} (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) (hH : H = res.keys.headD ([] : Key)) (hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key)) (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys) : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = [] := by rw [List.filter_eq_nil_iff] intro x hxdb hdec have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec have hxK : ((x : Key) : WithTop Key) < ((K : Key) : WithTop Key) := lt_trans hxH hHltK have hxmem : x ∈ db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) := by rw [List.mem_filter] exact ⟨hxdb, by rw [decide_eq_true_eq]; exact hxK⟩ rw [hfilter] at hxmem have hHle : ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := WithTop.coe_le_coe.mpr (by rw [hH, headD_eq_head_of_ne hne] exact head_le_of_sortedLT' hsort hne hxmem) exact absurd hxH (not_lt.mpr hHle) /-- In the `U = keyAfter last` branch, the stored keys in `[head, keyAfter last)` are exactly the reported keys. -/ theorem clampHigh_between_head_keyAfter_of_filter {db : DB} {res : Result} {H K : Key} (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) (hH : H = res.keys.headD ([] : Key)) (_hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key)) (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys) : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := by refine eq_of_forall_mem_iff_of_pairwise (db.sorted.pairwise.filter _) hsort.pairwise ?_ intro x constructor · intro hx rw [between, List.mem_filter] at hx obtain ⟨hxdb, hxdec⟩ := hx rw [decide_eq_true_eq] at hxdec rw [← hfilter, List.mem_filter] exact ⟨hxdb, by rw [decide_eq_true_eq]; exact hxdec.2⟩ · intro hx rw [← hfilter, List.mem_filter] at hx obtain ⟨hxdb, hxdec⟩ := hx rw [decide_eq_true_eq] at hxdec have hxres : x ∈ res.keys := by rw [← hfilter, List.mem_filter] exact ⟨hxdb, by rw [decide_eq_true_eq]; exact hxdec⟩ rw [between, List.mem_filter] refine ⟨hxdb, ?_⟩ rw [decide_eq_true_eq] exact ⟨WithTop.coe_le_coe.mpr (by rw [hH, headD_eq_head_of_ne hne] exact head_le_of_sortedLT' hsort hne hxres), hxdec⟩ /-- In the `U = keyAfter last` branch, nothing is stored below the reported head. -/ theorem clampHigh_cnt_head_eq_zero_of_filter {db : DB} {res : Result} {H K : Key} (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) (hH : H = res.keys.headD ([] : Key)) (hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key)) (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys) : cnt db ((H : Key) : WithTop Key) = 0 := by have hno := clampHigh_no_below_head_of_filter hsort hne hH hHltK hfilter rw [cnt, List.countP_eq_length_filter, hno] rfl /-- In the `U = keyAfter last` branch, the stored keys below `begin.key` number at most `beginSlack + 1`. -/ theorem clampHigh_cnt_begin_le_of_filter {db : DB} {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys) : cnt db r.begin.key ≤ beginSlack r + 1 := by classical obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 obtain ⟨db0, hcomp0⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK have hfilter' : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys := by simpa [hK, hL] using hfilter have hHL : H ≤ L := by rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) have hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by rw [hK] exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hHL) (WithTop.coe_lt_coe.mpr (keyAfter_self_lt L)) have hbetweenHK := clampHigh_between_head_keyAfter_of_filter hsort hne hH hHltK hfilter' have hcntH := clampHigh_cnt_head_eq_zero_of_filter hsort hne hH hHltK hfilter' by_cases hBK : r.begin.key ≤ ((K : Key) : WithTop Key) · -- `begin.key ≤ keyAfter last`: every reported key below `begin.key` is stored there. have hHB : between db ((H : Key) : WithTop Key) r.begin.key = res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by rw [show between db ((H : Key) : WithTop Key) r.begin.key = (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) from by unfold between rw [List.filter_filter] apply List.filter_congr intro k _ by_cases hkB : ((k : Key) : WithTop Key) < r.begin.key · have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := lt_of_lt_of_le hkB hBK simp [hkB, hkK] · simp [hkB]] rw [hbetweenHK] have hlen : (between db ((H : Key) : WithTop Key) r.begin.key).length = reportedBelowBegin r res := by rw [hHB, reportedBelowBegin, ← List.countP_eq_length_filter] have hcntB : cnt db r.begin.key = reportedBelowBegin r res := by have hsplit := cnt_eq_add_between db (le_of_lt hcrossD) rw [hsplit, hcntH, hlen]; omega omega · -- `keyAfter last < begin.key`: the gap keys plus the reported keys. push Not at hBK have hKleB : ((K : Key) : WithTop Key) ≤ r.begin.key := le_of_lt hBK have hcntK : cnt db ((K : Key) : WithTop Key) = res.keys.length := by rw [cnt_eq_add_between db (le_of_lt hHltK), hcntH, hbetweenHK] exact Nat.zero_add _ have hcntB : cnt db r.begin.key = res.keys.length + (between db ((K : Key) : WithTop Key) r.begin.key).length := by rw [cnt_eq_add_between db hKleB, hcntK] have hr : reportedBelowBegin r res = res.keys.length := by unfold reportedBelowBegin rw [List.countP_eq_length] intro k hk rw [decide_eq_true_eq] have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by simpa [hK, hL] using clampHigh_reported_lt_keyAfter (_r := r) hsort hne k hk exact lt_trans hkK hBK have hcapC : (between db ((K : Key) : WithTop Key) r.begin.key).length ≤ clampHighC r res := by have hle := between_length_le_intervalCard (db := db) (E := ((K : Key) : WithTop Key)) (B := r.begin.key) hKleB have hfacts := clampHigh_thresh_gt_facts (r := r) (res := res) hnotcap have hcap : ((between db ((K : Key) : WithTop Key) r.begin.key).length : ℕ∞) ≤ (clampHighC r res : ℕ∞) := le_trans hle hfacts.1 exact_mod_cast hcap have hC : clampHighC r res = beginSlack r + 1 - res.keys.length := by unfold clampHighC rw [hr] have htle : res.keys.length ≤ beginSlack r + 1 := by have := hclamp rw [hr] at this omega have hbnd : res.keys.length + (between db ((K : Key) : WithTop Key) r.begin.key).length ≤ beginSlack r + 1 := by calc res.keys.length + (between db ((K : Key) : WithTop Key) r.begin.key).length ≤ res.keys.length + clampHighC r res := Nat.add_le_add_left hcapC _ _ = beginSlack r + 1 := by rw [hC]; omega omega /-- In the `U = keyAfter last` branch, the filter condition characterises compatibility. -/ theorem clampHigh_K_compatible_of_keys {db : DB} {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys) : RangeRead.Compatible db r res := by classical obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 obtain ⟨db0, hcomp0⟩ := hread have hlim : RangeRead.withinLimit r res.keys := hcomp0.1 have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set E : WithTop Key := r.end_.key with hE set t : ℕ := res.keys.length with ht have hfilter' : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys := by simpa [hK, hL] using hfilter have hHL : H ≤ L := by rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) have hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by rw [hK] exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hHL) (WithTop.coe_lt_coe.mpr (keyAfter_self_lt L)) have hbetweenHK := clampHigh_between_head_keyAfter_of_filter hsort hne hH hHltK hfilter' have hcntH := clampHigh_cnt_head_eq_zero_of_filter hsort hne hH hHltK hfilter' have hcntE : cnt db E = 0 := by have hle : cnt db E ≤ cnt db ((H : Key) : WithTop Key) := cnt_le_cnt_of_le db (le_of_lt hEH) rw [hcntH] at hle; omega -- the stored keys below `keyAfter last` are the reported keys have h1 := filter_lt_eq_take_countP db.sorted ((K : Key) : WithTop Key) have htakeEq : db.keys.take (db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key)))) = res.keys := by rw [← h1]; exact hfilter' have hcntK' : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = t := by have hlen := congrArg List.length htakeEq rw [List.length_take] at hlen have hle : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) ≤ db.keys.length := List.countP_le_length rw [ht]; omega have hcntK : cnt db ((K : Key) : WithTop Key) = t := by simpa [cnt] using hcntK' have htake : db.keys.take t = res.keys := by rw [← hcntK']; exact htakeEq have ht_le : t ≤ db.keys.length := by have h := congrArg List.length htake rw [List.length_take] at h rw [ht] at h ⊢ omega -- the end resolution sits exactly past the first `t` stored keys have hanchorNat : (r.end_.anchor db).toNat = t := by rw [anchor_toNat_eq (s := r.end_) (by omega)] have hce : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by simpa [cnt, hE] using hcntE rw [hce, Nat.zero_add, ← hfinite, ht] have htpos : 0 < t := by rw [ht]; exact List.length_pos_of_ne_nil hne have h0end : 0 ≤ r.end_.anchor db := by by_contra h have hlt : r.end_.anchor db < 0 := lt_of_not_ge h rw [Int.toNat_of_nonpos (le_of_lt hlt)] at hanchorNat omega have hcntz : cnt db (resolve db r.end_) = t := by rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl, countP_lt_resolve (db := db) (s := r.end_), hanchorNat] exact Nat.min_eq_left ht_le have hcntz' : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = t := by simpa [cnt] using hcntz have hfilterZ : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys := by rw [filter_lt_eq_take_countP db.sorted (resolve db r.end_), hcntz', htake] -- the begin resolution is at most the reported head have hHdb : H ∈ db.keys := by have hHres : H ∈ res.keys := by rw [hH]; exact headD_mem hne rw [← hfilter', List.mem_filter] at hHres exact hHres.1 have hH0 : db.keys[0]'(List.length_pos_of_mem hHdb) = H := by obtain ⟨j, hj, hjH⟩ := List.mem_iff_getElem.mp hHdb have hcntHkey : db.keys.countP (fun k : Key => decide (k < H)) = 0 := by rw [← countP_lt_coe H]; exact hcntH have hcnt_eq_j : db.keys.countP (fun k : Key => decide (k < H)) = j := countP_lt_eq_of_getElem db.sorted hj hjH have hj0 : j = 0 := by rw [← hcnt_eq_j]; exact hcntHkey subst hj0 simpa using hjH have hcntBle := clampHigh_cnt_begin_le_of_filter ⟨db0, hcomp0⟩ hcell hnotcap hfilter have hanchor_le : r.begin.anchor db ≤ 0 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] have hc : ((db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) ≤ (beginSlack r : ℤ) + 1 := by have := hcntBle simp only [cnt] at this exact_mod_cast this linarith have haH : resolve db r.begin ≤ ((H : Key) : WithTop Key) := by by_cases hneg : r.begin.anchor db < 0 · rw [resolve_eq_nil hneg]; exact coe_nil_le_key_withTop _ · have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hneg have hzero : r.begin.anchor db = 0 := le_antisymm hanchor_le h0 have hltlen : (r.begin.anchor db).toNat < db.keys.length := by rw [hzero, Int.toNat_zero]; exact List.length_pos_of_mem hHdb have hidx : (r.begin.anchor db).toNat = 0 := by rw [hzero, Int.toNat_zero] have hget : db.keys[(r.begin.anchor db).toNat]'hltlen = db.keys[0]'(List.length_pos_of_mem hHdb) := by congr 1 rw [resolve_eq_getElem h0 hltlen, hget, hH0] have hcnta : cnt db (resolve db r.begin) = 0 := by have hle : cnt db (resolve db r.begin) ≤ cnt db ((H : Key) : WithTop Key) := cnt_le_cnt_of_le db haH rw [hcntH] at hle; omega -- the complete window is the reported list have hwin : between db (resolve db r.begin) (resolve db r.end_) = res.keys := by refine eq_of_forall_mem_iff_of_pairwise (db.sorted.pairwise.filter _) hsort.pairwise ?_ intro k constructor · intro hk rw [between, List.mem_filter] at hk obtain ⟨hkdb, hdec⟩ := hk rw [decide_eq_true_eq] at hdec rw [← hfilterZ, List.mem_filter] exact ⟨hkdb, by rw [decide_eq_true_eq]; exact hdec.2⟩ · intro hk have hkdb : k ∈ db.keys := by have : k ∈ db.keys.take t := by rw [htake]; exact hk exact List.mem_of_mem_take this have hka : resolve db r.begin ≤ ((k : Key) : WithTop Key) := by refine le_trans haH ?_ exact WithTop.coe_le_coe.mpr (by rw [hH, headD_eq_head_of_ne hne] exact head_le_of_sortedLT' hsort hne hk) have hkz : ((k : Key) : WithTop Key) < resolve db r.end_ := by have hmem : k ∈ db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by rw [hfilterZ]; exact hk rw [List.mem_filter] at hmem exact of_decide_eq_true hmem.2 rw [between, List.mem_filter] exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hka, hkz⟩⟩ have hck : res.keys = RangeRead.completeKeys db r := by rw [completeKeys_eq_inRange_of_not_rev hrev] exact hwin.symm exact ⟨hlim, Or.inr ⟨hmore, hck⟩⟩ /-- The `U = keyAfter last` branch: compatibility is exactly the filter below `keyAfter last`. -/ theorem clampHigh_K_compatible_iff_filter {db : DB} {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : RangeRead.Compatible db r res ↔ db.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys := by have hiff : ∀ db : DB, RangeRead.Compatible db r res ↔ db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys := fun db => ⟨fun hcomp => clampHigh_K_filter_of_compatible hread hcomp hcell hnotcap, fun hf => clampHigh_K_compatible_of_keys hread hcell hnotcap hf⟩ rw [hiff db, ← clampHigh_filter_bot_eq db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key))] /-- **The `U = keyAfter last` branch of the clamping finite crossed high cell is minimal.** -/ theorem minimal_clampHigh_K {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : Minimal r res ⟨(([] : Key) : WithTop Key), (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key))⟩ := minimal_of_compatible_iff_filter hread (fun _db => clampHigh_K_compatible_iff_filter hread hcell hnotcap) /-- A database splits into its keys below a threshold and those at or above it. -/ theorem keys_eq_filter_lt_append_filter_not_lt (db : DB) (B : WithTop Key) : db.keys = db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) ++ db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < B)) := by have h1 := filter_lt_eq_take_countP db.sorted B have h2 := filter_not_lt_eq_drop_countP db.sorted B rw [h1, h2] exact (List.take_append_drop _ _).symm /-- The capacity condition of the `U = begin.key` branch forces `keyAfter last` to lie strictly below `begin.key`. -/ theorem clampHigh_K_lt_begin {r : RangeRead} {res : Result} (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) < r.begin.key := by by_contra hcon push Not at hcon have hzero : clampHighCap r res = 0 := by unfold clampHighCap exact intervalCard_eq_zero_of_le hcon have hpos : 0 < clampHighThresh r res := by have := clampHigh_C_pos (r := r) (res := res) hcell.2.1 unfold clampHighThresh omega rw [hzero] at hcap exact absurd hcap (not_le.mpr (ENat.natCast_lt_natCast.mpr hpos)) /-- Every reported key lies strictly below `begin.key` once the capacity condition of the `U = begin.key` branch holds. -/ theorem clampHigh_reported_lt_begin {r : RangeRead} {res : Result} (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.begin.key := by intro k hk have hK := clampHigh_K_lt_begin (r := r) (res := res) hcell hcap have hkL : k ≤ res.keys.getLastD ([] : Key) := by rw [getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_getLast hk have hkK : ((k : Key) : WithTop Key) < (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) := WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkL (keyAfter_self_lt _)) exact lt_trans hkK hK section BeginKeyBranch variable {db : DB} {r : RangeRead} {res : Result} /-- In the `U = begin.key` branch, a compatible database whose begin selector is clamped stores nothing below the reported head. -/ theorem clampHigh_B_bot_pad_eq_nil (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbot : resolve db r.begin = (([] : Key) : WithTop Key)) : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key))) = [] := by classical set H : Key := res.keys.headD ([] : Key) with hH set z : WithTop Key := resolve db r.end_ with hz have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hwin : res.keys = between db (([] : Key) : WithTop Key) z := by rw [hres, completeKeys_eq_inRange_of_not_rev hrev, hbot]; rfl have hHz : ((H : Key) : WithTop Key) < z := by have h := hwin ▸ hHmem exact (of_decide_eq_true (List.mem_filter.mp h).2).2 rw [List.filter_eq_nil_iff] intro x hxdb hdec have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec have hxz : ((x : Key) : WithTop Key) < z := lt_trans hxH hHz have hxmem : x ∈ between db (([] : Key) : WithTop Key) z := by rw [between, List.mem_filter] exact ⟨hxdb, by rw [decide_eq_true_eq]; exact ⟨coe_nil_le_key_withTop _, hxz⟩⟩ rw [← hwin] at hxmem have hxH' : ((x : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := by rw [← hheadD]; exact hxH exact absurd (WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hxmem)) (not_le.mpr hxH') /-- In the `U = begin.key` branch, if the begin selector resolves onto the reported head, then the keys below that head are exactly the stored keys below `end.key`, and in particular all lie below `end.key`. -/ theorem clampHigh_B_head_pad_lt_end (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hend : 1 ≤ r.end_.offset) (hfinite : res.keys.length = (r.end_.offset - 1).toNat) (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : ∀ x ∈ db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key))), ((x : Key) : WithTop Key) < r.end_.key := by classical set H : Key := res.keys.headD ([] : Key) with hH set z : WithTop Key := resolve db r.end_ with hz set t : ℕ := res.keys.length with ht set e : ℕ := (r.end_.offset - 1).toNat with he set cE : ℕ := cnt db r.end_.key with hcE set cH : ℕ := cnt db ((H : Key) : WithTop Key) with hcH have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hwin : res.keys = between db ((H : Key) : WithTop Key) z := by rw [hres, completeKeys_eq_inRange_of_not_rev hrev, hhead]; rfl have hHz : ((H : Key) : WithTop Key) < z := by have h := hwin ▸ hHmem exact (of_decide_eq_true (List.mem_filter.mp h).2).2 have hcntz : cnt db z = cH + t := by have h := cnt_eq_add_between db (le_of_lt hHz) rw [← hcH, ← hwin, ← ht] at h exact h have hane : (r.end_.anchor db).toNat = cE + t := by rw [anchor_toNat_eq (s := r.end_) hend] change cnt db r.end_.key + (r.end_.offset - 1).toNat = cE + t have het : (r.end_.offset - 1).toNat = t := hfinite.symm.trans ht.symm rw [← hcE, het] have hmin : min (cE + t) db.keys.length = cH + t := by have h := countP_lt_resolve (db := db) (s := r.end_) rw [← hz, hane] at h rw [show db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < z)) = cnt db z from rfl, hcntz] at h exact h.symm have hcEle : cE ≤ cH := by have := cnt_le_cnt_of_le db (le_of_lt hEH) simpa [hcE, hcH] using this have hceq : cE = cH := by by_contra hcontra have hlt : cE < cH := lt_of_le_of_ne hcEle hcontra have hle := Nat.min_le_left (cE + t) db.keys.length omega intro x hx rw [List.mem_filter] at hx have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hx.2 by_contra hcon push Not at hcon have hcntsplit := cnt_eq_add_between db (le_of_lt hEH) rw [← hcH, ← hcE, hceq] at hcntsplit have hlen0 : (between db r.end_.key ((H : Key) : WithTop Key)).length = 0 := by omega have hnil : between db r.end_.key ((H : Key) : WithTop Key) = [] := List.eq_nil_of_length_eq_zero hlen0 have hmem : x ∈ between db r.end_.key ((H : Key) : WithTop Key) := by rw [between, List.mem_filter] exact ⟨hx.1, by rw [decide_eq_true_eq]; exact ⟨hcon, hxH⟩⟩ rw [hnil] at hmem exact absurd hmem (by simp) /-- In the `U = begin.key` branch, if the begin selector resolves to the empty key, then at most `beginSlack + 1` stored keys lie below `begin.key`. -/ theorem clampHigh_B_bot_cnt_le {db : DB} {r : RangeRead} {res : Result} (_hcomp : RangeRead.Compatible db r res) (_hrev : r.reverse = false) (_hmore : res.more = false) (_hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hbot : resolve db r.begin = (([] : Key) : WithTop Key)) : cnt db r.begin.key ≤ beginSlack r + 1 := by by_cases hneg : r.begin.anchor db < 0 · have hanchor : r.begin.anchor db = (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] rfl rw [hanchor] at hneg omega · push Not at hneg have hlt : (r.begin.anchor db).toNat < db.keys.length := by by_contra hcon push Not at hcon rw [resolve_eq_top hneg hcon] at hbot exact absurd hbot (by simp) have hget : db.keys[(r.begin.anchor db).toNat] = ([] : Key) := by have := resolve_eq_getElem hneg hlt rw [hbot] at this exact (WithTop.coe_inj.mp this).symm have hidx : (r.begin.anchor db).toNat = 0 := by have h := countP_lt_eq_of_getElem db.sorted hlt hget have hz : db.keys.countP (fun k : Key => decide (k < ([] : Key))) = 0 := by rw [List.countP_eq_zero] intro k _ simp [List.not_lt_nil] rw [← h, hz] have hle : r.begin.anchor db ≤ 0 := Int.toNat_eq_zero.mp hidx have hanchor : r.begin.anchor db = (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] rfl rw [hanchor] at hle omega end BeginKeyBranch section BeginKeyDecomp variable {db : DB} {r : RangeRead} {res : Result} /-- When the begin selector resolves onto the reported head in the crossed negative-begin complete high cell, the number of stored keys below `begin.key` is the number below the head plus `beginSlack + 1`. -/ theorem clampHigh_B_head_cnt_begin (_hcomp : RangeRead.Compatible db r res) (_hrev : r.reverse = false) (_hmore : res.more = false) (_hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : cnt db r.begin.key = cnt db ((res.keys.headD ([] : Key) : Key) : WithTop Key) + beginSlack r + 1 := by classical set H : Key := res.keys.headD ([] : Key) with hH set cH : ℕ := cnt db ((H : Key) : WithTop Key) with hcH set cB : ℕ := cnt db r.begin.key with hcB set q : ℕ := beginSlack r with hq have hHne : H ≠ [] := by intro hnil rw [hnil] at hEH exact absurd hEH (not_lt.mpr (coe_nil_le_key_withTop r.end_.key)) have hanchor : r.begin.anchor db = (cB : ℤ) - (q : ℤ) - 1 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcB, hq] rfl have hnonneg : 0 ≤ r.begin.anchor db := by by_contra hneg rw [resolve_eq_nil (lt_of_not_ge hneg)] at hhead exact hHne (WithTop.coe_inj.mp hhead.symm) have hltlen : (r.begin.anchor db).toNat < db.keys.length := by have hcalc : (r.begin.anchor db).toNat = cB - q - 1 := by rw [hanchor] omega rw [hcalc] exact lt_of_lt_of_le (by omega) (cnt_le_length db r.begin.key) have hget : db.keys[(r.begin.anchor db).toNat] = H := by have := resolve_eq_getElem hnonneg hltlen rw [hhead] at this exact WithTop.coe_inj.mp this.symm have hidx : (r.begin.anchor db).toNat = cH := by have := countP_lt_eq_of_getElem db.sorted hltlen hget simpa [cnt, hcH] using this.symm have hcast : (r.begin.anchor db) = (cH : ℤ) := by conv_lhs => rw [← Int.toNat_of_nonneg hnonneg] rw [hidx] rw [hcast] at hanchor have : (cH : ℤ) = (cB : ℤ) - (q : ℤ) - 1 := hanchor omega /-- **Decomposition of a compatible database in the `U = begin.key` branch.** The stored keys split as a (possibly empty) pad below `end.key`, the reported keys, a gap in `[keyAfter last, begin.key)`, and keys at or above `begin.key`. The gap has length `C` when the begin selector resolves onto the reported head, and the pad is empty with a shorter gap when it clamps. -/ theorem clampHigh_B_decomp_of_compatible (_hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) (hcomp : RangeRead.Compatible db r res) : ∃ pad gap rest : List Key, db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = pad ++ res.keys ++ gap ∧ db.keys = pad ++ res.keys ++ gap ++ rest ∧ (∀ x ∈ pad, ((x : Key) : WithTop Key) < r.end_.key) ∧ (∀ x ∈ gap, (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) ∧ (∀ x ∈ rest, r.begin.key ≤ ((x : Key) : WithTop Key)) ∧ (gap.length = clampHighC r res ∨ (gap.length < clampHighC r res ∧ pad = [])) := by classical obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hclamp := hcell.2.1 have hfinite := hcell.2.2 have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set q : ℕ := beginSlack r with hq set t : ℕ := res.keys.length with ht have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne have hHL : H ≤ L := by rw [hH, hL, hheadD, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) have hHKle : ((H : Key) : WithTop Key) ≤ ((K : Key) : WithTop Key) := by rw [hK] exact WithTop.coe_le_coe.mpr (le_trans hHL (le_of_lt (keyAfter_self_lt L))) have hKLB : ((K : Key) : WithTop Key) < r.begin.key := by have := clampHigh_K_lt_begin (r := r) (res := res) hcell hcap simpa [hK, hL] using this -- the reported block `[head, keyAfter last)` have hHK_eq : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := by simpa [hH, hK, hL] using clamp_keys_HK_eq (db := db) (r := r) (res := res) hcomp hrev hmore hne -- filter below `keyAfter last` have hfK : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) ++ res.keys := by rw [filter_lt_eq_filter_lt_append_between db.sorted hHKle] simp only [between] at hHK_eq rw [← hHK_eq] -- filter below `begin.key` have hfB : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) ++ res.keys ++ between db ((K : Key) : WithTop Key) r.begin.key := by rw [filter_lt_eq_filter_lt_append_between db.sorted (le_of_lt hKLB), hfK] rfl -- split the database into below and at-or-above `begin.key` have hsplit := keys_eq_filter_lt_append_filter_not_lt db r.begin.key rw [hfB] at hsplit refine ⟨db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))), between db ((K : Key) : WithTop Key) r.begin.key, db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < r.begin.key)), hfB, hsplit, ?_, ?_, ?_, ?_⟩ · -- pad keys lie below `end.key` have hresol := clamp_begin_resolve_bot_or_head (db := db) (r := r) (res := res) hcomp hrev hmore hne hbegin hcrossD rcases hresol with hbot | hhead · have hnil := clampHigh_B_bot_pad_eq_nil (db := db) (r := r) (res := res) hcomp hrev hmore hne hbot have hpadnil : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = [] := by simpa [hH] using hnil intro x hx rw [hpadnil] at hx exact absurd hx (by simp) · have hp := clampHigh_B_head_pad_lt_end (db := db) (r := r) (res := res) hcomp hrev hmore hne (by omega) hfinite hEH hhead intro x hx exact hp x (by simpa [hH] using hx) · -- gap keys lie in `[keyAfter last, begin.key)` intro x hx rw [between, List.mem_filter] at hx exact of_decide_eq_true hx.2 · -- remaining keys lie at or above `begin.key` intro x hx rw [List.mem_filter] at hx exact le_of_not_gt (of_decide_eq_true hx.2) · -- length condition on the gap have hresol := clamp_begin_resolve_bot_or_head (db := db) (r := r) (res := res) hcomp hrev hmore hne hbegin hcrossD have hr : reportedBelowBegin r res = t := by rw [reportedBelowBegin, ht, List.countP_eq_length] intro k hk rw [decide_eq_true_eq] exact clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap k hk have hcBsplit : cnt db r.begin.key = (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)))).length + t + (between db ((K : Key) : WithTop Key) r.begin.key).length := by rw [show cnt db r.begin.key = (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))).length from by simp [cnt, List.countP_eq_length_filter], hfB] simp only [List.length_append, ht] rcases hresol with hbot | hhead · -- clamped: the pad is empty and the gap has at most `C` keys have hnil := clampHigh_B_bot_pad_eq_nil (db := db) (r := r) (res := res) hcomp hrev hmore hne hbot have hpadnil : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = [] := by simpa [hH] using hnil have hcntle := clampHigh_B_bot_cnt_le (db := db) (r := r) (res := res) hcomp hrev hmore hne hbegin hbot have hpadlen : (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)))).length = 0 := by rw [hpadnil]; rfl rw [hpadlen] at hcBsplit simp only [Nat.zero_add] at hcBsplit have hC : clampHighC r res = q + 1 - t := by unfold clampHighC rw [hq, hr] have hgaple : (between db ((K : Key) : WithTop Key) r.begin.key).length ≤ clampHighC r res := by rw [hC]; omega rcases lt_or_eq_of_le hgaple with hlt | heq · exact Or.inr ⟨hlt, hpadnil⟩ · exact Or.inl heq · -- resolved onto the head: the gap has length exactly `C` have hcnt := clampHigh_B_head_cnt_begin (db := db) (r := r) (res := res) hcomp hrev hmore hne hbegin hEH hhead have hcH : cnt db ((res.keys.headD ([] : Key) : Key) : WithTop Key) = (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)))).length := by have : ((res.keys.headD ([] : Key) : Key) : WithTop Key) = ((H : Key) : WithTop Key) := by rw [hH] rw [this] simp [cnt, List.countP_eq_length_filter] rw [hcH] at hcnt left have hC : clampHighC r res = q + 1 - t := by unfold clampHighC rw [hq, hr] rw [hC] rw [hcBsplit] at hcnt simp only [ ht] at hcnt omega end BeginKeyDecomp section ShapeTools /-- A list all of whose entries lie below `B` is fixed by filtering below `B`. -/ theorem filter_lt_eq_self_of_forall {l : List Key} {B : WithTop Key} (h : ∀ x ∈ l, ((x : Key) : WithTop Key) < B) : l.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = l := by rw [List.filter_eq_self] intro k hk rw [decide_eq_true_eq] exact h k hk /-- A list none of whose entries lie below `B` is emptied by filtering below `B`. -/ theorem filter_lt_eq_nil_of_forall {l : List Key} {B : WithTop Key} (h : ∀ x ∈ l, ¬ ((x : Key) : WithTop Key) < B) : l.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = [] := by rw [List.filter_eq_nil_iff] intro k hk rw [decide_eq_true_eq] exact h k hk end ShapeTools section BetweenTools /-- A list all of whose entries lie in `[a, z)` is fixed by filtering to that range. -/ theorem filter_between_eq_self {l : List Key} {a z : WithTop Key} (h : ∀ x ∈ l, a ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < z) : l.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < z)) = l := by rw [List.filter_eq_self] intro k hk rw [decide_eq_true_eq] exact h k hk /-- A list none of whose entries lie in `[a, z)` is emptied by filtering to that range. -/ theorem filter_between_eq_nil {l : List Key} {a z : WithTop Key} (h : ∀ x ∈ l, ¬(a ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < z)) : l.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < z)) = [] := by rw [List.filter_eq_nil_iff] intro k hk rw [decide_eq_true_eq] exact h k hk end BetweenTools section BeginKeyConverse variable {db : DB} {r : RangeRead} {res : Result} /-- The `U = begin.key` branch, converse direction: a database whose keys below `begin.key` are a pad, the reported keys and a gap of the prescribed size is compatible. -/ theorem clampHigh_B_compatible_of_shape (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) {pad gap rest : List Key} (hkeys : db.keys = pad ++ res.keys ++ gap ++ rest) (hpad : ∀ x ∈ pad, ((x : Key) : WithTop Key) < r.end_.key) (hgap : ∀ x ∈ gap, (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) (hrest : ∀ x ∈ rest, r.begin.key ≤ ((x : Key) : WithTop Key)) (hgapcond : gap.length = clampHighC r res ∨ (gap.length < clampHighC r res ∧ pad = [])) (hwl : RangeRead.withinLimit r res.keys) : RangeRead.Compatible db r res := by classical obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hfinite := hcell.2.2 have hclamp := hcell.2.1 obtain ⟨db0, hcomp0⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set B : WithTop Key := r.begin.key with hB set E : WithTop Key := r.end_.key with hE set t : ℕ := res.keys.length with ht set q : ℕ := beginSlack r with hq have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne have hres_ge_H : ∀ k ∈ res.keys, ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by intro k hk rw [hH, headD_eq_head_of_ne hne] exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk) have hres_le_L : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := by intro k hk rw [hL, getLastD_eq_getLast_of_ne hne] exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hk) have hHL : ((H : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := hres_ge_H L hLmem have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH have hLK : ((L : Key) : WithTop Key) < ((K : Key) : WithTop Key) := WithTop.coe_lt_coe.mpr (keyAfter_self_lt L) have hKB : ((K : Key) : WithTop Key) < B := by rw [hB] have := clampHigh_K_lt_begin (r := r) (res := res) hcell hcap simpa [hK, hL] using this have hreported : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < B := by rw [hB] exact clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap have hEHb : E < B := lt_trans hEH' hcrossD have hEK : E < ((K : Key) : WithTop Key) := lt_trans (lt_of_lt_of_le hEH' hHL) hLK have hres_sub : ∀ k ∈ res.keys, k ∈ db.keys := by intro k hk rw [hkeys] exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hk))))) -- filters below `E` and `B` have hfiltE : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < E)) = pad := by rw [hkeys, List.filter_append, List.filter_append, List.filter_append] rw [filter_lt_eq_self_of_forall (B := E) (by intro x hx; exact hpad x hx)] rw [filter_lt_eq_nil_of_forall (B := E) (by intro x hx; exact not_lt.mpr (le_of_lt (lt_of_lt_of_le hEH' (hres_ge_H x hx))))] rw [filter_lt_eq_nil_of_forall (B := E) (by intro x hx; exact not_lt.mpr (le_of_lt (lt_of_lt_of_le hEK (hgap x hx).1)))] rw [filter_lt_eq_nil_of_forall (B := E) (by intro x hx; exact not_lt.mpr (le_of_lt (lt_of_lt_of_le hEHb (hrest x hx))))] simp have hcntE : cnt db E = pad.length := by rw [show cnt db E = (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < E))).length from by simp only [cnt, List.countP_eq_length_filter]] rw [hfiltE] have hfiltB : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = pad ++ res.keys ++ gap := by rw [hkeys, List.filter_append, List.filter_append, List.filter_append] rw [filter_lt_eq_self_of_forall (B := B) (by intro x hx; exact lt_trans (hpad x hx) hEHb)] rw [filter_lt_eq_self_of_forall (B := B) (by intro x hx; exact hreported x hx)] rw [filter_lt_eq_self_of_forall (B := B) (by intro x hx; exact (hgap x hx).2)] rw [filter_lt_eq_nil_of_forall (B := B) (by intro x hx; exact not_lt.mpr (hrest x hx))] simp have hcntB : cnt db B = pad.length + t + gap.length := by rw [show cnt db B = (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B))).length from by simp only [cnt, List.countP_eq_length_filter]] rw [hfiltB, List.length_append, List.length_append, ht] have hr : reportedBelowBegin r res = t := by rw [reportedBelowBegin, ht, List.countP_eq_length] intro k hk rw [decide_eq_true_eq] exact by rw [← hB]; exact hreported k hk have hCval : clampHighC r res = q + 1 - t := by unfold clampHighC rw [hq, hr] -- the end anchor, in natural numbers have hane : (r.end_.anchor db).toNat = pad.length + t := by rw [anchor_toNat_eq (s := r.end_) (by omega)] have hcountE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = cnt db E := by rw [hE]; rfl rw [hcountE, hcntE] have het : (r.end_.offset - 1).toNat = t := by omega rw [het] -- the begin anchor have hanchorB : r.begin.anchor db = (cnt db B : ℤ) - (q : ℤ) - 1 := by rw [hB, anchor_eq_count_sub_slack db r (le_of_lt hbegin), hq] rfl -- split on the two admissible shapes of the gap rcases hgapcond with hgEq | ⟨hgLt, hpadnil⟩ · -- `gap.length = C`, the begin selector resolves onto the reported head have hgle : gap.length = q + 1 - t := by rw [← hCval]; exact hgEq have hCpos := clampHigh_C_pos (r := r) (res := res) hclamp have hCvalpos : 0 < q + 1 - t := by rw [← hCval]; exact hCpos have hgapne : gap ≠ [] := by intro hnil; rw [hnil, List.length_nil] at hgle; omega -- the filter `[head, begin.key)` has `q + 1` entries have hfiltHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B))).length = q + 1 := by have heq : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) = res.keys ++ gap := by rw [hkeys, List.filter_append, List.filter_append, List.filter_append] rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) (z := B) (by intro x hx; exact fun hcon => absurd hcon.1 (not_le.mpr (lt_trans (hpad x hx) hEH')))] rw [filter_between_eq_self (a := ((H : Key) : WithTop Key)) (z := B) (by intro x hx; exact ⟨hres_ge_H x hx, hreported x hx⟩)] rw [filter_between_eq_self (a := ((H : Key) : WithTop Key)) (z := B) (by intro x hx; exact ⟨le_trans (le_trans hHL (le_of_lt hLK)) (hgap x hx).1, (hgap x hx).2⟩)] rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) (z := B) (by intro x hx hcon; exact absurd hcon.2 (not_lt.mpr (hrest x hx)))] simp rw [heq, List.length_append, hgle] omega have haresolve : resolve db r.begin = ((H : Key) : WithTop Key) := resolve_begin_eq_head_of_filter_len (db := db) (r := r) (head := H) hbegin (hres_sub H hHmem) hcrossD hfiltHB -- the end selector resolves to the first gap key have hEidx : (r.end_.anchor db).toNat = pad.length + t := hane have hlen_lt : pad.length + t < db.keys.length := by rw [hkeys, List.length_append, List.length_append, List.length_append] have : 0 < gap.length := List.length_pos_of_ne_nil hgapne omega have hnonnegE : 0 ≤ r.end_.anchor db := by unfold Selector.anchor omega have hz : resolve db r.end_ = ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key) := by rw [resolve_eq_getElem hnonnegE (by rw [hane]; exact hlen_lt)] have hidx : db.keys[(r.end_.anchor db).toNat] = db.keys[pad.length + t] := by have h1 : db.keys[(r.end_.anchor db).toNat]? = some (db.keys[(r.end_.anchor db).toNat]) := List.getElem?_eq_getElem _ have h2 : db.keys[pad.length + t]? = some (db.keys[pad.length + t]) := List.getElem?_eq_getElem hlen_lt have h3 : db.keys[(r.end_.anchor db).toNat]? = db.keys[pad.length + t]? := by rw [hane] rw [h1, h2] at h3 exact Option.some.inj h3 rw [hidx] congr 1 have hge : db.keys[pad.length + t] = gap[0]'(List.length_pos_of_ne_nil hgapne) := by have hsome : db.keys[pad.length + t]? = gap[0]? := by rw [hkeys, List.append_assoc] rw [List.getElem?_append_right (l₁ := pad ++ res.keys) (l₂ := gap ++ rest) (by rw [List.length_append, ← ht])] rw [show (pad.length + t) - (pad ++ res.keys).length = 0 from by rw [List.length_append, ← ht]; omega] rw [List.getElem?_append_left (List.length_pos_of_ne_nil hgapne)] have hzgap : gap[0]? = some (gap[0]'(List.length_pos_of_ne_nil hgapne)) := List.getElem?_eq_getElem (List.length_pos_of_ne_nil hgapne) have hs : some (db.keys[pad.length + t]) = some (gap[0]'(List.length_pos_of_ne_nil hgapne)) := by rw [← List.getElem?_eq_getElem hlen_lt, hsome, hzgap] exact Option.some.inj hs exact hge -- the gap block is sorted, so its head is its least element have hsub : gap.Sublist db.keys := by rw [hkeys] exact (List.sublist_append_left gap rest).trans (by simp [List.append_assoc]) have hgapSorted : gap.SortedLT := List.sortedLT_iff_pairwise.mpr (List.Pairwise.sublist hsub (List.sortedLT_iff_pairwise.mp db.sorted)) -- the range `[head, first gap key)` is exactly the reported keys have hbetween : between db (resolve db r.begin) (resolve db r.end_) = res.keys := by rw [haresolve, hz, between, hkeys, List.filter_append, List.filter_append, List.filter_append] rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by intro x hx; exact fun hcon => absurd hcon.1 (not_le.mpr (lt_trans (hpad x hx) hEH')))] rw [filter_between_eq_self (a := ((H : Key) : WithTop Key)) (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by intro x hx refine ⟨hres_ge_H x hx, ?_⟩ exact lt_of_lt_of_le (lt_of_le_of_lt (hres_le_L x hx) hLK) (hgap _ (List.getElem_mem _)).1)] rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by intro x hx have hhead : ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by rw [← List.head_eq_getElem hgapne] exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hgapSorted hgapne hx) exact fun hcon => absurd hcon.2 (not_lt.mpr hhead))] rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by intro x hx hcon have hzB : ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key) < B := (hgap _ (List.getElem_mem _)).2 exact absurd hcon.2 (not_lt.mpr (le_of_lt (lt_of_lt_of_le hzB (hrest x hx)))))] simp have hck : res.keys = RangeRead.completeKeys db r := by rw [completeKeys_eq_inRange_of_not_rev hrev] exact hbetween.symm exact ⟨hwl, Or.inr ⟨hmore, hck⟩⟩ · -- `gap.length < C` and the pad is empty have hpadl : pad.length = 0 := by rw [hpadnil]; rfl have hgle : gap.length < q + 1 - t := by rw [← hCval]; exact hgLt -- the begin selector clamps to `⊥` have hanchor_neg : r.begin.anchor db < 0 := by rw [hanchorB, hcntB, hpadl] have : (t + gap.length : ℤ) - (q : ℤ) - 1 < 0 := by have : t + gap.length < q + 1 := by omega omega simpa using this have haresolve : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor_neg -- the end selector resolves to the first stored key at or after `begin.key` (or `⊤`) have hEidx : (r.end_.anchor db).toNat = t := by rw [hane, hpadl]; omega -- keys below `begin.key` are exactly the reported keys have hfiltBpad : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = res.keys ++ gap := by rw [hfiltB, hpadnil]; simp -- `between` is the take of the count below the end resolution have hcntz : cnt db (resolve db r.end_) = t := by rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl] rw [show db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = t from by rw [countP_lt_resolve (db := db) (s := r.end_), hEidx] exact Nat.min_eq_left (by have : t ≤ db.keys.length := by rw [hkeys, List.length_append, List.length_append, List.length_append, hpadl] omega exact this)] have hbetween : between db (resolve db r.begin) (resolve db r.end_) = res.keys := by rw [haresolve] have hbot : between db (([] : Key) : WithTop Key) (resolve db r.end_) = db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by unfold between apply List.filter_congr intro k _ rw [decide_eq_decide] exact ⟨fun h => h.2, fun h => ⟨coe_nil_le_key_withTop _, h⟩⟩ rw [hbot, filter_lt_eq_take_countP db.sorted] have hcnt' : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = t := by rw [countP_lt_resolve (db := db) (s := r.end_), hEidx] exact Nat.min_eq_left (by have : t ≤ db.keys.length := by rw [hkeys, List.length_append, List.length_append, List.length_append, hpadl] omega exact this) rw [hcnt'] have htake : db.keys.take t = res.keys := by rw [hkeys, hpadnil] simp only [List.nil_append] rw [List.append_assoc] exact List.take_left' ht.symm exact htake have hck : res.keys = RangeRead.completeKeys db r := by rw [completeKeys_eq_inRange_of_not_rev hrev] exact hbetween.symm exact ⟨hwl, Or.inr ⟨hmore, hck⟩⟩ end BeginKeyConverse section BeginKeySound variable {db : DB} {r : RangeRead} {res : Result} /-- Soundness of the `U = begin.key` branch: agreement below `begin.key` determines compatibility. -/ theorem clampHigh_B_sound (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : Sound r res (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) := by have step : ∀ dba dbc : DB, AgreeOn (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) dba dbc → RangeRead.Compatible dba r res → RangeRead.Compatible dbc r res := by intro dba dbc hagree hcomp have hfilt : dba.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = dbc.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by have h := hagree rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h exact h obtain ⟨pad, gap, rest₁, hfB, hkeys, hpad, hgap, _, hgapcond⟩ := clampHigh_B_decomp_of_compatible hread hcell hcap hcomp set rest₂ : List Key := dbc.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < r.begin.key)) with hrest₂ have hkeys₂ : dbc.keys = pad ++ res.keys ++ gap ++ rest₂ := by have hsplit := keys_eq_filter_lt_append_filter_not_lt dbc r.begin.key rw [← hfilt, hfB] at hsplit rw [hsplit, hrest₂] have hrest₂' : ∀ x ∈ rest₂, r.begin.key ≤ ((x : Key) : WithTop Key) := by intro x hx rw [hrest₂, List.mem_filter] at hx exact le_of_not_gt (of_decide_eq_true hx.2) exact clampHigh_B_compatible_of_shape hread hcell hcap hkeys₂ hpad hgap hrest₂' hgapcond hcomp.1 intro db₁ db₂ hagree exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ end BeginKeySound section BeginKeyNecessary variable {db : DB} {r : RangeRead} {res : Result} /-- In the `U = begin.key` branch the reported keys themselves form a compatible database: the begin selector clamps to `⊥` and the end selector overshoots. -/ theorem clampHigh_B_keys_compatible {hsort : res.keys.SortedLT} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) : RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hclamp := hcell.2.1 have hfinite := hcell.2.2 have hwl : RangeRead.withinLimit r res.keys := by obtain ⟨db0, hcomp0⟩ := hread exact RangeRead.compatible_withinLimit hcomp0 set db : DB := ⟨res.keys, hsort⟩ with hdb have hdbkeys : db.keys = res.keys := rfl have hgt : ∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) := high_reported_gt_end hsort hne hEH have hcE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by rw [hdbkeys] rw [List.countP_eq_zero] intro k hk rw [decide_eq_true_eq] exact not_lt.mpr (le_of_lt (hgt k hk)) have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := rfl have hneg : r.begin.anchor db < 0 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcB] have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp omega have hbot : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hneg have hepos : 1 ≤ r.end_.offset := by omega have hanchorE : (r.end_.anchor db).toNat = (r.end_.offset - 1).toNat := by rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add] have hEtop : resolve db r.end_ = (⊤ : WithTop Key) := by have hnonneg : 0 ≤ r.end_.anchor db := by unfold Selector.anchor omega refine resolve_eq_top hnonneg ?_ rw [hanchorE, ← hfinite, hdbkeys] have hck : res.keys = RangeRead.completeKeys db r := by rw [completeKeys_eq_inRange_of_not_rev hrev] unfold RangeRead.inRange rw [hbot, hEtop] have htrue : db.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = db.keys := by rw [List.filter_eq_self] intro k _ rw [decide_eq_true_eq] exact ⟨coe_nil_le_key_withTop _, WithTop.coe_lt_top k⟩ rw [htrue, hdbkeys] exact ⟨hwl, Or.inr ⟨hmore, hck⟩⟩ end BeginKeyNecessary section BeginKeyNecessaryTools variable {db : DB} {r : RangeRead} {res : Result} /-- The threshold splits as `C` away from the bottom and `C + 1` at the bottom. -/ theorem clampHigh_thresh_of_ne_bot (hE : r.end_.key ≠ (([] : Key) : WithTop Key)) : clampHighThresh r res = clampHighC r res := by unfold clampHighThresh rw [ite_eq_right hE] exact Nat.add_zero _ theorem clampHigh_thresh_of_bot (hE : r.end_.key = (([] : Key) : WithTop Key)) : clampHighThresh r res = clampHighC r res + 1 := by unfold clampHighThresh rw [ite_eq_left hE] /-- Away from the gap, a key above the last reported key is at or above `keyAfter (last reported)`. -/ theorem clampHigh_B_gap_of_not_le_last {k : Key} (hkL : ¬ ((k : Key) : WithTop Key) ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ ((k : Key) : WithTop Key) := by exact le_of_not_gt (fun h => hkL (WithTop.coe_le_coe.mpr ((lt_keyAfter_iff (k := k) (l := res.keys.getLastD ([] : Key))).mp (WithTop.coe_lt_coe.mp h)))) /-- The begin anchor of the reported-keys database is negative in the clamping branch. -/ theorem clampHigh_B_keys_anchor_neg {hsort : res.keys.SortedLT} (_hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) : r.begin.anchor (⟨res.keys, hsort⟩ : DB) < 0 := by obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hclamp := hcell.2.1 have hcB : (⟨res.keys, hsort⟩ : DB).keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := rfl rw [anchor_eq_count_sub_slack (⟨res.keys, hsort⟩ : DB) r (le_of_lt hbegin), hcB] have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp omega end BeginKeyNecessaryTools section BeginKeyGapNecessary variable {db : DB} {r : RangeRead} {res : Result} /-- **Every gap key is necessary when `end.key` is the empty key.** Build a compatible database with the reported keys and exactly `C` gap keys avoiding `k` (possible because the threshold is `C + 1`), then insert `k`: the gap grows to `C + 1`, which no compatible database admits. -/ theorem clampHigh_B_necessary_gap_bot {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) (hEbot : r.end_.key = (([] : Key) : WithTop Key)) (hkB : ((k : Key) : WithTop Key) < r.begin.key) (hkK : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ ((k : Key) : WithTop Key)) (hknot : k ∉ res.keys) : Necessary r res k := by obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hclamp := hcell.2.1 have hfinite := hcell.2.2 obtain ⟨db0, hcomp0⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev have hwl := RangeRead.compatible_withinLimit hcomp0 have hread_keep : Readable r res := ⟨db0, hcomp0⟩ set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set B : WithTop Key := r.begin.key with hB set C : ℕ := clampHighC r res with hC set t : ℕ := res.keys.length with ht have hth : clampHighThresh r res = C + 1 := by rw [clampHigh_thresh_of_bot (r := r) (res := res) hEbot, hC] have hcap' : ((C + 1 : ℕ) : ℕ∞) ≤ clampHighCap r res := by rw [← hth]; exact hcap have hcapK : ((C + 1 : ℕ) : ℕ∞) ≤ intervalCard ((K : Key) : WithTop Key) B := by simpa [clampHighCap, hK, hL, hB] using hcap' obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ := exists_db_keysIn_avoid (B := ((K : Key) : WithTop Key)) (E := B) (k := k) (m := C) hcapK have hJlo : ∀ x ∈ dbJ.keys, ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := fun x hx => (hJrange x hx).1 have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < B := fun x hx => (hJrange x hx).2 have hcross : ∀ a ∈ res.keys, ∀ b ∈ dbJ.keys, a < b := by intro a ha b hb have haL : a ≤ L := by rw [hL, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_getLast ha have haK : a < K := by rw [hK] exact lt_of_le_of_lt haL (keyAfter_self_lt L) have hKb : ((K : Key) : WithTop Key) ≤ ((b : Key) : WithTop Key) := hJlo b hb exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr haK) hKb) have hsortedA : (res.keys ++ dbJ.keys).SortedLT := List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hcross⟩ let dbA : DB := ⟨res.keys ++ dbJ.keys, hsortedA⟩ have hcompA : RangeRead.Compatible dbA r res := by refine clampHigh_B_compatible_of_shape (db := dbA) hread_keep hcell hcap (pad := []) (gap := dbJ.keys) (rest := []) (hwl := hwl) ?_ ?_ ?_ ?_ ?_ · simp [dbA] · intro x hx; exact absurd hx (by simp) · intro x hx; exact ⟨hJlo x hx, hJhi x hx⟩ · intro x hx; exact absurd hx (by simp) · left rw [hJlen, hC] have hkA : k ∉ dbA.keys := by simp [dbA, hknot, hkJ] let db1 : DB := dbA.insert k hkA have hkeys1 : db1.keys = (res.keys ++ dbJ.keys).orderedInsert (· ≤ ·) k := by simp [db1, dbA] have hall1 : ∀ x ∈ db1.keys, ((x : Key) : WithTop Key) < B := by intro x hx rw [hkeys1, List.mem_orderedInsert] at hx rcases hx with rfl | hx · exact hkB · rw [List.mem_append] at hx rcases hx with hx | hx · have hlt := clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx simpa [hB] using hlt · exact hJhi x hx have hcnt1 : db1.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = t + (C + 1) := by rw [List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq]; exact hall1 x hx)] rw [hkeys1, List.orderedInsert_length, List.length_append, hJlen] simp only [ht] omega have hnot : ¬ RangeRead.Compatible db1 r res := by intro hcomp1 obtain ⟨pad', gap1, rest', -, hkeys', hpad', hgap', hrest', hgapcond'⟩ := clampHigh_B_decomp_of_compatible hread_keep hcell hcap hcomp1 have hpadnil : pad' = [] := by by_contra hne' obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil pad' hne' have hlt := hpad' x hx rw [hEbot] at hlt exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key x))) have hpadlen : pad'.length = 0 := by rw [hpadnil]; rfl have hEB : r.end_.key < B := lt_trans hEH hcrossD have hcount' : db1.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = pad'.length + t + gap1.length := by rw [hkeys', List.countP_append, List.countP_append, List.countP_append] have hp : pad'.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = pad'.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq] exact lt_trans (hpad' x hx) hEB) have hr : res.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = res.keys.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq] exact by simpa [hB] using clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx) have hg : gap1.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = gap1.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq]; exact (hgap' x hx).2) have hs : rest'.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = 0 := List.countP_eq_zero.mpr (by intro x hx; rw [decide_eq_true_eq]; exact not_lt.mpr (hrest' x hx)) rw [hp, hr, hg, hs, ht] simp have hcnt_eq : pad'.length + t + gap1.length = t + (C + 1) := by rw [← hcount', hcnt1] rw [hpadlen, Nat.zero_add] at hcnt_eq have hgap1 : gap1.length = C + 1 := by omega rcases hgapcond' with hgC | ⟨hgLt, -⟩ · omega · omega exact necessary_of_toggle ⟨dbA, hcompA, by rwa [toggle_eq_insert_of_not_mem hkA]⟩ end BeginKeyGapNecessary /-- **Every gap key is necessary when `end.key` is not the empty key.** Build a compatible database with one pad key below `end.key` and exactly `C` gap keys containing `k` (possible because the threshold is `C`), so erasing `k` leaves a nonempty pad with a too-short gap, which no compatible database admits. -/ theorem clampHigh_B_necessary_gap_ne_bot {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) (hE : r.end_.key ≠ (([] : Key) : WithTop Key)) (hkB : ((k : Key) : WithTop Key) < r.begin.key) (hkK : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ ((k : Key) : WithTop Key)) (hknot : k ∉ res.keys) : Necessary r res k := by obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 have hclamp := hcell.2.1 obtain ⟨db0, hcomp0⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev have hwl := RangeRead.compatible_withinLimit hcomp0 have hread_keep : Readable r res := ⟨db0, hcomp0⟩ set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL set K : Key := keyAfter L with hK set B : WithTop Key := r.begin.key with hB set C : ℕ := clampHighC r res with hC set t : ℕ := res.keys.length with ht have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hHL : H ≤ L := by rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) have hHL' : ((H : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := WithTop.coe_le_coe.mpr hHL have hLK : ((L : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by rw [hK]; exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt L) have hE_lt_K : r.end_.key < ((K : Key) : WithTop Key) := lt_of_lt_of_le hEH (le_trans hHL' (le_of_lt hLK)) have hres_ge_H : ∀ x ∈ res.keys, ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by intro x hx rw [hH, headD_eq_head_of_ne hne] exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx) have hres_lt_K : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by intro x hx have hxL : ((x : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := by rw [hL, getLastD_eq_getLast_of_ne hne] exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx) exact lt_of_le_of_lt hxL hLK have hnilE : ((([] : Key) : WithTop Key)) < r.end_.key := lt_of_le_of_ne (coe_nil_le_key_withTop _) (Ne.symm hE) have hnil_lt_K : ((([] : Key) : WithTop Key)) < ((K : Key) : WithTop Key) := lt_trans hnilE hE_lt_K have hth : clampHighThresh r res = C := by rw [clampHigh_thresh_of_ne_bot (r := r) (res := res) hE, hC] have hcap' : (C : ℕ∞) ≤ clampHighCap r res := by rw [← hth]; exact hcap have hcapK : (C : ℕ∞) ≤ intervalCard ((K : Key) : WithTop Key) B := by simpa [clampHighCap, hK, hL, hB] using hcap' have hCpos : 0 < C := clampHigh_C_pos (r := r) (res := res) hclamp have hcapsub : ((C - 1 + 1 : ℕ) : ℕ∞) ≤ intervalCard ((K : Key) : WithTop Key) B := by have hsub : C - 1 + 1 = C := by omega rw [hsub]; exact hcapK obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ := exists_db_keysIn_avoid (B := ((K : Key) : WithTop Key)) (E := B) (k := k) (m := C - 1) hcapsub have hJlo : ∀ x ∈ dbJ.keys, ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := fun x hx => (hJrange x hx).1 have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < B := fun x hx => (hJrange x hx).2 set dbG : DB := dbJ.insert k hkJ with hdbG have hGkeys : dbG.keys = dbJ.keys.orderedInsert (· ≤ ·) k := DB.insert_keys dbJ k hkJ have hGrange : ∀ x ∈ dbG.keys, ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B := by intro x hx rw [hGkeys, List.mem_orderedInsert] at hx rcases hx with rfl | hx · exact ⟨hkK, hkB⟩ · exact ⟨hJlo x hx, hJhi x hx⟩ have hGlen : dbG.keys.length = C := by rw [hGkeys, List.orderedInsert_length, hJlen] omega have hkG : k ∈ dbG.keys := by rw [hGkeys]; exact (List.mem_orderedInsert (· ≤ ·)).mpr (Or.inl rfl) have hnil_lt_res : ∀ b ∈ res.keys, ([] : Key) < b := by intro b hb have hlt : ((([] : Key) : WithTop Key)) < ((b : Key) : WithTop Key) := lt_of_lt_of_le (lt_trans hnilE hEH) (hres_ge_H b hb) exact WithTop.coe_lt_coe.mp hlt have hnil_lt_gap : ∀ b ∈ dbG.keys, ([] : Key) < b := by intro b hb have hKb : ((K : Key) : WithTop Key) ≤ ((b : Key) : WithTop Key) := (hGrange b hb).1 exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hnil_lt_K hKb) have hcross_res_gap : ∀ a ∈ res.keys, ∀ b ∈ dbG.keys, a < b := by intro a ha b hb have haK : ((a : Key) : WithTop Key) < ((K : Key) : WithTop Key) := hres_lt_K a ha have hKb : ((K : Key) : WithTop Key) ≤ ((b : Key) : WithTop Key) := (hGrange b hb).1 exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le haK hKb) have hsing : ([[]] : List Key).SortedLT := List.sortedLT_cons.mpr ⟨by intro b hb; exact absurd hb (by simp), List.sortedLT_nil⟩ have hcross_nil : ∀ a ∈ ([[]] : List Key), ∀ b ∈ res.keys ++ dbG.keys, a < b := by intro a ha b hb simp only [List.mem_singleton] at ha subst ha rw [List.mem_append] at hb rcases hb with hb | hb · exact hnil_lt_res b hb · exact hnil_lt_gap b hb have hinner : (res.keys ++ dbG.keys).SortedLT := List.sortedLT_append.mpr ⟨hsort, dbG.sorted, hcross_res_gap⟩ have hsortedA : ([[]] ++ res.keys ++ dbG.keys).SortedLT := by rw [List.append_assoc] exact List.sortedLT_append.mpr ⟨hsing, hinner, hcross_nil⟩ let dbA : DB := ⟨[[]] ++ res.keys ++ dbG.keys, hsortedA⟩ have hkeysA : dbA.keys = [[]] ++ res.keys ++ dbG.keys := rfl have hcompA : RangeRead.Compatible dbA r res := by refine clampHigh_B_compatible_of_shape (db := dbA) hread_keep hcell hcap (pad := [[]]) (gap := dbG.keys) (rest := []) (hwl := hwl) ?_ ?_ ?_ ?_ ?_ · simp [hkeysA] · intro x hx rw [List.mem_singleton] at hx subst hx exact hnilE · intro x hx; exact hGrange x hx · intro x hx; exact absurd hx (by simp) · left; exact hGlen have hkA : k ∈ dbA.keys := by rw [hkeysA] exact List.mem_append_right ([[]] ++ res.keys) hkG have hk_nil : k ≠ ([] : Key) := by intro h have hlt := hnil_lt_gap k hkG rw [h] at hlt exact absurd hlt (lt_irrefl _) have hk_not_padres : k ∉ ([[]] : List Key) ++ res.keys := by rw [List.mem_append] rintro (h | h) · rw [List.mem_singleton] at h; exact hk_nil h · exact hknot h have hdb1keys : (dbA.erase k).keys = [[]] ++ res.keys ++ dbG.keys.erase k := by rw [DB.erase_keys] change (([[]] ++ res.keys ++ dbG.keys).erase k) = [[]] ++ res.keys ++ dbG.keys.erase k rw [List.erase_append_right dbG.keys hk_not_padres] have hall1 : ∀ x ∈ (dbA.erase k).keys, ((x : Key) : WithTop Key) < B := by intro x hx rw [hdb1keys, List.mem_append, List.mem_append] at hx rcases hx with (hx | hx) | hx · rw [List.mem_singleton] at hx subst hx exact lt_trans hnilE (lt_trans hEH hcrossD) · exact by simpa [hB] using clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx · exact (hGrange x (List.mem_of_mem_erase hx)).2 have hlenA : dbA.keys.length = 1 + t + C := by rw [hkeysA, List.length_append, List.length_append, hGlen, ht] simp have hcnt1 : (dbA.erase k).keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = t + C := by rw [List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq]; exact hall1 x hx), DB.erase_keys, List.length_erase_of_mem hkA, hlenA] omega have hnot : ¬ RangeRead.Compatible (dbA.erase k) r res := by intro hcomp1 obtain ⟨pad', gap1, rest', -, hkeys', hpad', hgap', hrest', hgapcond'⟩ := clampHigh_B_decomp_of_compatible hread_keep hcell hcap hcomp1 have hcount' : (dbA.erase k).keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = pad'.length + t + gap1.length := by rw [hkeys', List.countP_append, List.countP_append, List.countP_append] have hp : pad'.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = pad'.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq] exact lt_trans (lt_trans (hpad' x hx) hEH) hcrossD) have hr : res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = res.keys.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq] exact by simpa [hB] using clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx) have hg : gap1.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = gap1.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq]; exact (hgap' x hx).2) have hs : rest'.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = 0 := List.countP_eq_zero.mpr (by intro x hx; rw [decide_eq_true_eq]; exact not_lt.mpr (hrest' x hx)) rw [hp, hr, hg, hs, ht] simp have hpadgap : pad'.length + gap1.length = C := by have h := hcount'.symm.trans hcnt1 omega have hcntK : (dbA.erase k).keys.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = C - 1 := by rw [hdb1keys, List.countP_append, List.countP_append] have hnil : ([[]] : List Key).countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = 0 := by rw [List.countP_eq_zero] intro x hx rw [List.mem_singleton] at hx subst hx rw [decide_eq_true_eq] exact fun hcon => absurd hcon.1 (not_le.mpr hnil_lt_K) have hres : res.keys.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = 0 := by rw [List.countP_eq_zero] intro x hx rw [decide_eq_true_eq] exact fun hcon => absurd hcon.1 (not_le.mpr (hres_lt_K x hx)) have hG : (dbG.keys.erase k).countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = C - 1 := by rw [List.countP_erase] have hall : dbG.keys.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = dbG.keys.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq]; exact hGrange x hx) rw [hall, hGlen] rw [ite_eq_left ⟨hkG, by rw [decide_eq_true_eq]; exact ⟨hkK, hkB⟩⟩] rw [hnil, hres, hG] omega have hcnt_decomp : (dbA.erase k).keys.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = gap1.length := by rw [hkeys', List.countP_append, List.countP_append, List.countP_append] have hp : pad'.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = 0 := by rw [List.countP_eq_zero] intro x hx rw [decide_eq_true_eq] exact fun hcon => absurd hcon.1 (not_le.mpr (lt_trans (hpad' x hx) hE_lt_K)) have hr : res.keys.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = 0 := by rw [List.countP_eq_zero] intro x hx rw [decide_eq_true_eq] exact fun hcon => absurd hcon.1 (not_le.mpr (hres_lt_K x hx)) have hg : gap1.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = gap1.length := List.countP_eq_length.mpr (by intro x hx; rw [decide_eq_true_eq]; exact hgap' x hx) have hs : rest'.countP (fun x : Key => decide (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < B)) = 0 := by rw [List.countP_eq_zero] intro x hx rw [decide_eq_true_eq] exact fun hcon => absurd hcon.2 (not_lt.mpr (hrest' x hx)) rw [hp, hr, hg, hs] simp have hgap1 : gap1.length = C - 1 := hcnt_decomp.symm.trans hcntK rcases hgapcond' with hgC | ⟨hgLt, hpadnil⟩ · omega · rw [hpadnil] at hpadgap simp at hpadgap omega exact necessary_of_toggle ⟨dbA, hcompA, by rw [toggle_eq_erase_of_mem hkA] exact hnot⟩ /-- **Every key below `begin.key` is necessary in the `U = begin.key` branch.** Split by position relative to the reported head and last keys: below the head a clamping witness makes the key necessary, between them it is reported (so necessary), and strictly above the last reported key it lies in the gap (handled by the two toggle constructions, according to whether `end.key` is the empty key). -/ theorem clampHigh_B_necessary {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) {k : Key} (hkB : ((k : Key) : WithTop Key) < r.begin.key) : Necessary r res k := by obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 obtain ⟨db0, hcomp0⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev have hread_keep : Readable r res := ⟨db0, hcomp0⟩ set H : Key := res.keys.headD ([] : Key) with hH set L : Key := res.keys.getLastD ([] : Key) with hL have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne by_cases hkres : k ∈ res.keys · exact necessary_of_mem_result hread_keep hkres · by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) · refine necessary_below_head_of_anchor_nonpos_negCross (⟨res.keys, hsort⟩ : DB) (clampHigh_B_keys_compatible (hsort := hsort) hread_keep hcell) hrev hne (clampHigh_B_keys_anchor_neg (hsort := hsort) hread_keep hcell) ?_ rw [← headD_eq_head_of_ne hne, ← hH] exact hkH · have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH by_cases hkL : ((k : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) · exact necessary_of_between_result hread_keep hHmem hLmem (WithTop.coe_le_coe.mp hHk) (WithTop.coe_le_coe.mp hkL) · have hkK : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ ((k : Key) : WithTop Key) := clampHigh_B_gap_of_not_le_last (k := k) (by rw [← hL]; exact hkL) by_cases hE : r.end_.key = (([] : Key) : WithTop Key) · exact clampHigh_B_necessary_gap_bot hread_keep hcell hcap hE hkB hkK hkres · exact clampHigh_B_necessary_gap_ne_bot hread_keep hcell hcap hE hkB hkK hkres /-- **The `U = begin.key` branch of the clamping finite crossed high cell is minimal.** -/ theorem minimal_clampHigh_B {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : Minimal r res ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := (minimal_iff_sound_and_necessary).mpr ⟨clampHigh_B_sound hread hcell hcap, fun _ hk => clampHigh_B_necessary hread hcell hcap (ConflictRange.mem_iff.mp hk).2⟩ /-- **The clamping finite branch of the crossed negative-begin complete high cell is closed**: the closed-form offset rule is minimal there. -/ theorem minimal_closedForm_of_negCrossCompleteHigh_clampFinite {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) : Minimal r res (closedFormNegCrossCompleteHigh r res) := by rw [closedForm_clampFinite_eq hcell] by_cases hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res) · have hu : clampHighU r res = r.begin.key := by unfold clampHighU; rw [ite_eq_left hcap] rw [hu] exact minimal_clampHigh_B hread hcell hcap · have hu : clampHighU r res = (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) := by unfold clampHighU; rw [ite_eq_right hcap] rw [hu] exact minimal_clampHigh_K hread hcell hcap /-- **The crossed negative-begin complete high cell is closed in every branch**: the clamping low and finite branches and the non-clamping low and finite branches exhaust the cell. -/ theorem minimal_closedForm_of_negCrossCompleteHigh {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHigh r res) : Minimal r res (closedFormNegCrossCompleteHigh r res) := by have hhigh : negCrossCompleteHigh r res := hcell obtain ⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩ := hcell have htle := high_length_le_end hread hrev hmore hne hend2 hEH by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r · by_cases hfin : res.keys.length = (r.end_.offset - 1).toNat · exact minimal_closedForm_of_negCrossCompleteHigh_clampFinite hread ⟨hhigh, hclamp, hfin⟩ · have hlow : res.keys.length < (r.end_.offset - 1).toNat := by omega exact minimal_closedForm_of_negCrossCompleteHigh_clampLow hread ⟨hhigh, hclamp, hlow⟩ · have hnc : beginSlack r < reportedBelowBegin r res := not_le.mp hclamp by_cases hfin : res.keys.length = (r.end_.offset - 1).toNat · exact minimal_closedForm_of_negCrossCompleteHigh_nonclampFinite hread ⟨hhigh, hnc, hfin⟩ · have hlow : res.keys.length < (r.end_.offset - 1).toNat := by omega exact minimal_closedForm_of_negCrossCompleteHigh_nonclampLow hread ⟨hhigh, hnc, hlow⟩ /-- The reversed traversal of the crossed negative-begin complete high cell: flipping the read gives the forward high cell. -/ abbrev negCrossCompleteHighRev (r : RangeRead) (res : Result) : Prop := r.reverse = true ∧ res.more = false ∧ res.keys ≠ [] ∧ negCrossCompleteHigh { r with reverse := false } { keys := res.keys.reverse, more := false } /-- The reversed crossed negative-begin complete high closed form. -/ noncomputable def closedFormNegCrossCompleteHighRev (r : RangeRead) (res : Result) : ConflictRange := closedFormNegCrossCompleteHigh { r with reverse := false } { keys := res.keys.reverse, more := false } /-- **The reversed crossed negative-begin complete high cell is minimal**, by flipping the traversal and applying the forward rule. -/ theorem minimal_closedForm_of_negCrossCompleteHigh_rev {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossCompleteHighRev r res) : Minimal r res (closedFormNegCrossCompleteHighRev r res) := by obtain ⟨hrev, hmore, hne, hhigh⟩ := hcell have hread' : Readable { r with reverse := false } { keys := res.keys.reverse, more := false } := by obtain ⟨db, hcomp⟩ := hread exact ⟨db, (compatible_flip (b := true) hrev hmore).mpr hcomp⟩ have hmin := minimal_closedForm_of_negCrossCompleteHigh hread' hhigh exact (minimal_flip (b := true) hrev hmore).mp hmin end FormalConflictRange