/- Copyright (c) 2026 FormalConflictRange contributors. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: FormalConflictRange contributors -/ import FormalConflictRange.NegCrossComplete import FormalConflictRange.ZeroCrossTrunc import FormalConflictRange.NonposRevClosedForm /-! # Crossed negative-begin truncated nonclamping unbounded-end cell This module closes the forward, crossed, truncated, nonclamping cell at a negative begin offset with an unbounded end selector (`end_.key = ⊤`, `1 ≤ end_.offset`). In this cell the begin anchor is exactly one past the reported keys below `begin.key`, so every compatible database resolves the begin selector onto the reported head, and the minimal sound conflict range is `[head, max begin.key (keyAfter last))`. The key arithmetic is `count_lt_begin_eq_of_negCross_trunc_nonclamp`: the stored keys below `begin.key` split at the head with exactly `reportedBelowBegin` keys in `[head, begin.key)`. Soundness then follows because agreement on that range fixes both the gap count and the truncated complete keys, and the gap keys `(last, begin.key)` are necessary by a toggle witness on the reported keys. -/ open FormalConflictRange set_option maxHeartbeats 1200000 namespace FormalConflictRange /-- In the nonclamping truncated crossed negative-begin cell, a compatible database resolves its begin selector onto the reported head. -/ theorem resolve_begin_eq_head_of_negCross_trunc_nonclamp {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (_hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp : beginSlack r < reportedBelowBegin r res) : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by set head : Key := res.keys.head hne with hhead set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) with hcB have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne have hhead_ck : head ∈ RangeRead.completeKeys db r := hpre.subset hhead_mem have hle_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) := (RangeRead.mem_completeKeys.mp hhead_ck).2.1 have hhead_db : head ∈ db.keys := (RangeRead.mem_completeKeys.mp hhead_ck).1 have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross -- Count identity: stored keys below `begin.key` split at the head. have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup (a := head) (B := r.begin.key) (le_of_lt hcross') have hres_count : res.keys.countP (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := by unfold reportedBelowBegin rw [List.countP_eq_length_filter, List.countP_eq_length_filter] congr 1 apply List.filter_congr intro k hk have hhead_le : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := WithTop.coe_le_coe.mpr ((RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk) rw [decide_eq_decide] exact and_iff_right hhead_le have hmin_le : reportedBelowBegin r res ≤ (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := by have h := List.Sublist.countP_le hsub (p := fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) rw [hres_count, List.countP_eq_length_filter] at h exact h have hcB_ge : beginSlack r + 1 ≤ cB := by have hN : beginSlack r + 1 ≤ (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) rw [hcB, hsplit] omega have hanchor_ge : 0 ≤ r.begin.anchor db := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge omega have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by intro htop rw [htop] at hle_head exact absurd hle_head (not_le.mpr (WithTop.coe_lt_top head)) have hltlen : (r.begin.anchor db).toNat < db.keys.length := by by_contra h exact hnot_top (resolve_eq_top hanchor_ge (le_of_not_gt h)) have hresolve_get : resolve db r.begin = ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) := resolve_eq_getElem hanchor_ge hltlen -- The head's rank is below the anchor index, so the resolved key is at least the head. have hcH_le : db.keys.countP (fun k : Key => decide (k < head)) ≤ (r.begin.anchor db).toNat := by have hanchor_cast : r.begin.anchor db = (cB : ℤ) - (beginSlack r : ℤ) - 1 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] have htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by have hz : r.begin.anchor db = ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by rw [hanchor_cast]; push_cast; ring have hz2 : ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) = ((cB - (beginSlack r + 1) : ℕ) : ℤ) := by rw [← Nat.cast_sub hcB_ge] rw [hz, hz2, Int.toNat_natCast] rw [htoNat] have hsplit' : cB = db.keys.countP (fun k : Key => decide (k < head)) + (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := by rw [hcB]; exact hsplit omega have hhead_le_resolve : ((head : Key) : WithTop Key) ≤ resolve db r.begin := by rw [hresolve_get] exact WithTop.coe_le_coe.mpr (le_getElem_of_countP_le db.sorted hcH_le hltlen) exact le_antisymm hle_head hhead_le_resolve /-- The forward, crossed, truncated, nonclamping, unbounded-end negative-begin cell. -/ abbrev negCrossTruncNonclampTop (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ reportedBelowBegin r res = beginSlack r + 1 /-- The closed form for the cell: from the reported head up to the larger of `begin.key` and the successor of the reported last. -/ noncomputable def closedFormNegCrossTruncNonclampTop (r : RangeRead) (res : Result) : ConflictRange := ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩ /-- The reported keys themselves are compatible with the cell's read. -/ theorem compatible_self_of_negCross_trunc_nonclamp_top {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncNonclampTop r res) : RangeRead.Compatible ⟨res.keys, RangeRead.compatible_sortedLT (Classical.choose_spec hread) hcell.2.1⟩ r res := by classical obtain ⟨hmore, hrev, hbegin, hendkey, hendoff, hcrossD, hnonclamp⟩ := hcell obtain ⟨db₀, hcomp₀⟩ := hread have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev set head : Key := res.keys.head hne with hhead let db : DB := ⟨res.keys, hsort⟩ have hdb : db.keys = res.keys := rfl have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hcrossD' : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcrossD have hcross : ((head : Key) : WithTop Key) < r.begin.key := by rw [← hheadD]; exact hcrossD' have hcB : res.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := rfl have hanchor : r.begin.anchor db = 0 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hdb, hcB, hnonclamp] push_cast; ring have hanchor_nat : (r.begin.anchor db).toNat = 0 := by rw [hanchor]; rfl have hL : resolve db r.begin = ((head : Key) : WithTop Key) := by have hlt : (r.begin.anchor db).toNat < db.keys.length := by rw [hanchor_nat, hdb]; exact List.length_pos_of_ne_nil hne have hget := resolve_eq_getElem (db := db) (s := r.begin) (by rw [hanchor]) hlt rw [hget] have hidx : db.keys[(r.begin.anchor db).toNat] = head := by rw [getElem_congr_idx (h' := hanchor_nat)] simpa [hhead] using (List.getElem_zero_eq_head (l := res.keys) (h := List.length_pos_of_ne_nil hne)) rw [hidx] have hck : RangeRead.completeKeys db r = res.keys := by rw [completeKeys_eq_filter_ge_of_end_top (db := db) hrev hendkey hendoff hL, hdb] rw [List.filter_eq_self] intro k hk rw [decide_eq_true_eq] exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hk) exact ⟨RangeRead.compatible_withinLimit hcomp₀, Or.inl ⟨hmore, by rw [hck], hne⟩⟩ /-- In the nonclamping truncated crossed negative-begin cell, the number of stored keys below `begin.key` splits at the reported head with exactly `reportedBelowBegin` keys in `[head, begin.key)`. -/ theorem count_lt_begin_eq_of_negCross_trunc_nonclamp {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db.keys.countP (fun k : Key => decide (k < res.keys.head hne)) + reportedBelowBegin r res := by set head : Key := res.keys.head hne with hhead set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) with hcB have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne have hhead_ck : head ∈ RangeRead.completeKeys db r := hpre.subset hhead_mem have hle_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) := (RangeRead.mem_completeKeys.mp hhead_ck).2.1 have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup (a := head) (B := r.begin.key) (le_of_lt hcross') have hres_count : res.keys.countP (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := by unfold reportedBelowBegin rw [List.countP_eq_length_filter, List.countP_eq_length_filter] congr 1 apply List.filter_congr intro k hk have hhead_le : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := WithTop.coe_le_coe.mpr ((RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk) rw [decide_eq_decide] exact and_iff_right hhead_le have hmin_le : reportedBelowBegin r res ≤ (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := by have h := List.Sublist.countP_le hsub (p := fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) rw [hres_count, List.countP_eq_length_filter] at h exact h have hcB_ge : beginSlack r + 1 ≤ cB := by have hN : beginSlack r + 1 ≤ (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) rw [hcB, hsplit] omega have hanchor_ge : 0 ≤ r.begin.anchor db := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge omega have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by intro htop rw [htop] at hle_head exact absurd hle_head (not_le.mpr (WithTop.coe_lt_top head)) have hltlen : (r.begin.anchor db).toNat < db.keys.length := by by_contra h exact hnot_top (resolve_eq_top hanchor_ge (le_of_not_gt h)) have hL := resolve_begin_eq_head_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin hcross hnonclamp have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor_ge hltlen have hval : db.keys[(r.begin.anchor db).toNat] = head := WithTop.coe_inj.mp (hget.symm.trans hL) have hcountP : db.keys.countP (fun k : Key => decide (k < head)) = (r.begin.anchor db).toNat := countP_lt_eq_of_getElem db.sorted hltlen hval have htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by have hanchor_cast : r.begin.anchor db = (cB : ℤ) - (beginSlack r : ℤ) - 1 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] have hz : r.begin.anchor db = ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by rw [hanchor_cast]; push_cast; ring have hz2 : ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) = ((cB - (beginSlack r + 1) : ℕ) : ℤ) := by rw [← Nat.cast_sub hcB_ge] rw [hz, hz2, Int.toNat_natCast] rw [hcB] omega /-- **Compatibility transfers across agreement on the nonclamping truncated crossed negative-begin range `[head, max begin.key (keyAfter last))`.** Agreement on the range fixes the reported head and the stored keys in `[head, begin.key)`, hence the begin resolution after the nonclamp count identity, and fixes the truncated complete keys. -/ theorem compatible_of_agree_negCross_trunc_nonclamp_top {r : RangeRead} {res : Result} (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) (hagree : AgreeOn (closedFormNegCrossTruncNonclampTop r res) db1 db2) : RangeRead.Compatible db2 r res := by set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), upper⟩ := by unfold closedFormNegCrossTruncNonclampTop rw [hheadD, hlastD, hupper] have hB_upper : r.begin.key ≤ upper := le_max_left _ _ have hlast_upper : ((last : Key) : WithTop Key) < upper := lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampTop r res) db1 db2).mp hagree have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by intro k hk1 hk2 refine hmem k ?_ rw [hcr] exact ⟨hk1, hk2⟩ have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp have hE1 : resolve db1 r.end_ = (⊤ : WithTop Key) := resolve_eq_top_of_key_eq_top hendkey hendoff have hE2 : resolve db2 r.end_ = (⊤ : WithTop Key) := resolve_eq_top_of_key_eq_top hendkey hendoff have hhead_db1 : head ∈ db1.keys := RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) have hhead_upper : ((head : Key) : WithTop Key) < upper := lt_of_lt_of_le hcross hB_upper have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 -- Count identity for `db1`, and the equal gap counts. have hcount1 := count_lt_begin_eq_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp_eq have hcount1' : db1.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db1.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by simpa [hhead] using hcount1 have hsplit1 := countP_lt_eq_add_filter_withTop db1.keys db1.nodup (a := head) (B := r.begin.key) (le_of_lt hcross) have hgaplen1 : (db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by rw [hcount1'] at hsplit1 omega have hgap_eq : db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by apply eq_of_forall_mem_iff_of_pairwise · exact db1.sorted.pairwise.filter _ · exact db2.sorted.pairwise.filter _ · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨hk1, hge, hlt⟩ exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge, hlt⟩ · rintro ⟨hk2, hge, hlt⟩ exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge, hlt⟩ have hgaplen2 : (db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by rw [← hgap_eq, hgaplen1] have hsplit2 := countP_lt_eq_add_filter_withTop db2.keys db2.nodup (a := head) (B := r.begin.key) (le_of_lt hcross) have hcount2 : db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db2.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by rw [hsplit2, hgaplen2] have cB2_ge : beginSlack r + 1 ≤ db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by rw [hcount2, ← hnonclamp_eq]; omega have hanchor2 : 0 ≤ r.begin.anchor db2 := by rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] have : (beginSlack r : ℤ) + 1 ≤ ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))) : ℤ) := by exact_mod_cast cB2_ge omega have htoNat2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - (beginSlack r + 1) := by have hz : r.begin.anchor db2 = ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)]; push_cast; ring have hz2 : ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ) = ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - (beginSlack r + 1) : ℕ) : ℤ) := by rw [← Nat.cast_sub cB2_ge] rw [hz, hz2, Int.toNat_natCast] have hidx_eq2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k => decide (k < head)) := by rw [htoNat2, hcount2, hnonclamp_eq] omega have hltlen2 : (r.begin.anchor db2).toNat < db2.keys.length := by rw [hidx_eq2] obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_db2 rw [countP_lt_eq_of_getElem db2.sorted hi hix] exact hi have hval2 : db2.keys[(r.begin.anchor db2).toNat]'hltlen2 = head := by simpa [hidx_eq2] using getElem_countP_lt db2.sorted hhead_db2 have hL2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := by rw [resolve_eq_getElem (db := db2) (s := r.begin) hanchor2 hltlen2] exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval2 have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := by rw [hE1]; exact WithTop.coe_lt_top last have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by rw [hE2]; exact WithTop.coe_lt_top last have hfilter1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 have hfilter2 : (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) = db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 have hres1fil : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = res.keys := by have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 rwa [← hlast] at h have hfeq : db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by apply eq_of_forall_mem_iff_of_pairwise · exact db1.sorted.pairwise.filter _ · exact db2.sorted.pairwise.filter _ · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨hk1, hge, hle⟩ exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, hge, hle⟩ · rintro ⟨hk2, hge, hle⟩ exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, hge, hle⟩ have hres2fil : (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) = res.keys := by rw [hfilter2, ← hfeq, ← hfilter1, hres1fil] rw [compatible_iff_truncated_keys hrev hmore hne] exact ⟨by simpa using hc1.1, by simpa [hlast] using hres2fil⟩ /-- Inserting a key strictly above every element of a list appends it. -/ theorem orderedInsert_append_of_forall_lt' {l : List Key} {k : Key} (h : ∀ x ∈ l, x < k) : l.orderedInsert (· ≤ ·) k = l ++ [k] := by induction l with | nil => simp | cons a t ih => have ha : ¬ k ≤ a := fun hka => absurd hka (not_le.mpr (h a (List.mem_cons_self ..))) rw [List.orderedInsert_cons, ite_eq_right ha, ih (fun x hx => h x (List.mem_cons_of_mem a hx))] simp /-- **Gap keys are necessary.** A key strictly between the reported last and `begin.key` is necessary: inserting it into the database of reported keys moves the begin anchor from `0` to `1`, so the reported head is no longer the resolved begin. -/ theorem necessary_negCross_trunc_gap {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) (hkgt : res.keys.getLast hne < k) (hkB : (k : WithTop Key) < r.begin.key) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev let db : DB := ⟨res.keys, hsort⟩ have hdb : db.keys = res.keys := rfl have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hcell : negCrossTruncNonclampTop r res := ⟨hmore, hrev, hbegin, hendkey, hendoff, by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD]; exact hcross, hnonclamp_eq⟩ have hcomp : RangeRead.Compatible db r res := compatible_self_of_negCross_trunc_nonclamp_top ⟨db₀, hcomp₀⟩ hcell have hle_last : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx have hk_notmem : k ∉ db.keys := by rw [hdb] exact fun hk => absurd (hle_last k hk) (not_le.mpr hkgt) refine necessary_of_toggle ⟨db, hcomp, ?_⟩ intro hcompk have htoggle : toggle db k = db.insert k hk_notmem := by unfold toggle rw [dite_eq_right hk_notmem] rw [htoggle] at hcompk set db' : DB := db.insert k hk_notmem with hdb' have hkeys' : db'.keys = res.keys ++ [k] := by rw [hdb', DB.insert_keys] exact orderedInsert_append_of_forall_lt' (fun x hx => lt_of_le_of_lt (hle_last x hx) hkgt) have hcount' : db'.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res + 1 := by rw [hkeys', List.countP_append] have hkdec : decide (((k : Key) : WithTop Key) < r.begin.key) = true := by rw [decide_eq_true_eq]; exact hkB rw [List.countP_singleton, hkdec, ite_eq_left rfl] have hres_count : res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := rfl rw [hres_count] have hanchor' : r.begin.anchor db' = 1 := by rw [anchor_eq_count_sub_slack db' r (le_of_lt hbegin), hcount', hnonclamp_eq] push_cast; ring have hL' : resolve db' r.begin = ((res.keys.head hne : Key) : WithTop Key) := resolve_begin_eq_head_of_negCross_trunc_nonclamp hcompk hrev hmore hne hbegin hcross (by rw [hnonclamp_eq]; omega) -- But the count identity for the compatible `db'` would force one stored key -- below the reported head, while `db'.keys = res.keys ++ [k]` has none. have hcount_lem := count_lt_begin_eq_of_negCross_trunc_nonclamp hcompk hrev hmore hne hbegin hcross hnonclamp_eq have hzero_count : db'.keys.countP (fun x : Key => decide (x < res.keys.head hne)) = 0 := by apply countP_lt_eq_zero_of_forall_ge intro x hx rw [hkeys'] at hx rcases List.mem_append.mp hx with hx | hx · exact (hsort.pairwise.imp le_of_lt).rel_head hx · simp only [List.mem_singleton] at hx subst hx exact le_trans ((hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)) (le_of_lt hkgt) rw [hcount', hzero_count] at hcount_lem omega /-- **Gap keys are necessary for any end selector.** A key strictly between the reported last and `begin.key` is necessary: toggling it changes the number of stored keys below `begin.key` by one while leaving the number below the reported head unchanged, so the nonclamp count identity cannot hold on the toggled database. -/ theorem necessary_negCross_trunc_gap_general {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) (hkgt : res.keys.getLast hne < k) (hkB : (k : WithTop Key) < r.begin.key) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread have hsort := RangeRead.compatible_sortedLT hcomp₀ hrev have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) have hhead_lt_k : res.keys.head hne < k := lt_of_le_of_lt hhead_le_last hkgt have hhead_not_lt : ¬ ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := by rw [not_lt] exact WithTop.coe_le_coe.mpr (le_of_lt hhead_lt_k) refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ intro hcompk have hiden₀ := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp₀ hrev hmore hne hbegin hcross hnonclamp_eq rw [← countP_lt_coe] at hiden₀ change cnt db₀ r.begin.key = cnt db₀ (res.keys.head hne) + reportedBelowBegin r res at hiden₀ have hiden₁ := count_lt_begin_eq_of_negCross_trunc_nonclamp hcompk hrev hmore hne hbegin hcross hnonclamp_eq rw [← countP_lt_coe] at hiden₁ change cnt (toggle db₀ k) r.begin.key = cnt (toggle db₀ k) (res.keys.head hne) + reportedBelowBegin r res at hiden₁ by_cases hkmem : k ∈ db₀.keys · have htoggle : toggle db₀ k = db₀.erase k := toggle_eq_erase_of_mem hkmem rw [htoggle] at hiden₁ rw [cnt_erase_lt db₀ hkmem hkB, cnt_erase_not_lt db₀ hkmem hhead_not_lt] at hiden₁ have hpos : 1 ≤ cnt db₀ r.begin.key := by rw [hiden₀] have hq : 0 < beginSlack r := by unfold beginSlack rw [← Int.pos_iff_toNat_pos] omega omega omega · have htoggle : toggle db₀ k = db₀.insert k hkmem := toggle_eq_insert_of_not_mem hkmem rw [htoggle] at hiden₁ rw [cnt_insert_lt db₀ k hkmem hkB, cnt_insert_not_lt db₀ k hkmem hhead_not_lt] at hiden₁ omega /-- **Closed form for the forward crossed negative-begin truncated nonclamping unbounded-end cell.** The minimal sound range is `[head, max begin.key (keyAfter last))`. -/ theorem minimal_closedForm_of_negCross_trunc_nonclamp_top {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncNonclampTop r res) : Minimal r res (closedFormNegCrossTruncNonclampTop r res) := by obtain ⟨hmore, hrev, hbegin, hendkey, hendoff, hcrossD, hnonclamp_eq⟩ := hcell obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hcross : ((head : Key) : WithTop Key) < r.begin.key := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hcrossD exact hcrossD let upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), upper⟩ := by unfold closedFormNegCrossTruncNonclampTop rw [hheadD, hlastD] have hsound : Sound r res (closedFormNegCrossTruncNonclampTop r res) := by intro db1 db2 hagree exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_top hrev hmore hne hbegin hendkey hendoff hcross hnonclamp_eq h hagree, fun h => compatible_of_agree_negCross_trunc_nonclamp_top hrev hmore hne hbegin hendkey hendoff hcross hnonclamp_eq h hagree.symm⟩ rw [hcr] at hsound ⊢ refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ intro k hkbeg hkend by_cases hkle : k ≤ last · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) (by rw [hlast]; exact List.getLast_mem hne) (WithTop.coe_le_coe.mp hkbeg) hkle · have hkgt : res.keys.getLast hne < k := by simpa [hlast] using lt_of_not_ge hkle have hkltB : (k : WithTop Key) < r.begin.key := by have hkmax : (k : WithTop Key) < max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend rcases lt_max_iff.mp hkmax with h | h · exact h · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) exact absurd hklast hkle exact necessary_negCross_trunc_gap hread' hrev hmore hne hbegin hendkey hendoff hcross hnonclamp_eq hkgt hkltB /-- **A key below `end_.key` lies below the end resolution at any positive end offset.** With offset at least one the end anchor is at or past the count of stored keys below `end_.key`, so the resolution is at or past every key below `end_.key`. -/ theorem lt_resolve_end_of_lt_end_key {db : DB} {r : RangeRead} (hend : 1 ≤ r.end_.offset) {k : Key} (hk : (k : WithTop Key) < r.end_.key) : (k : WithTop Key) < resolve db r.end_ := by set c := db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) with hc have hanc : r.end_.anchor db = (c : ℤ) + r.end_.offset - 1 := by unfold Selector.anchor; rw [hc] have hanchor_nonneg : 0 ≤ r.end_.anchor db := by rw [hanc]; have : (0 : ℤ) ≤ c := Int.natCast_nonneg _; omega have hcnt_le : db.keys.countP (fun x : Key => decide (x < keyAfter k)) ≤ c := by rw [hc] apply List.countP_mono_left intro x _ hxd rw [decide_eq_true_eq] at hxd ⊢ exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr ((lt_keyAfter_iff x k).mp hxd)) hk have hanchor_ge : (db.keys.countP (fun x : Key => decide (x < keyAfter k)) : ℤ) ≤ r.end_.anchor db := by rw [hanc] have hcle : (db.keys.countP (fun x : Key => decide (x < keyAfter k)) : ℤ) ≤ (c : ℤ) := by exact_mod_cast hcnt_le have hoff : (1 : ℤ) ≤ r.end_.offset := by exact_mod_cast hend omega rcases lt_or_ge (r.end_.anchor db).toNat db.keys.length with hlt | hge · have hres := resolve_eq_getElem (db := db) (s := r.end_) hanchor_nonneg hlt rw [hres] have hcount_le_anchor : db.keys.countP (fun x : Key => decide (x < keyAfter k)) ≤ (r.end_.anchor db).toNat := by omega have hle : keyAfter k ≤ db.keys[(r.end_.anchor db).toNat] := le_getElem_of_countP_le db.sorted hcount_le_anchor hlt exact WithTop.coe_lt_coe.mpr (lt_of_lt_of_le (keyAfter_self_lt k) hle) · have htop := resolve_eq_top (db := db) (s := r.end_) hanchor_nonneg hge rw [htop]; exact WithTop.coe_lt_top k /-- **Dropping the end bound at `end_.offset = 1` when every reported key lies below `end_.key`.** At offset one the end selector resolves to the first stored key at or above `end_.key`, so every stored key at or below the reported last lies below the resolution; replacing `end_.key` by `⊤` therefore leaves compatibility unchanged. -/ theorem compatible_iff_end_top_of_offset_one {db : DB} {r : RangeRead} {res : Result} (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hend : r.end_.offset = 1) (hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : RangeRead.Compatible db r res ↔ RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := by set r' : RangeRead := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } with hr' have hlast_lt : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := hbelow _ (List.getLast_mem hne) have hlt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := lt_resolve_end_of_end_one hend hlast_lt have hend' : r'.end_.offset = 1 := by rw [hr']; exact hend have htop : resolve db r'.end_ = (⊤ : WithTop Key) := resolve_eq_top_of_key_eq_top (db := db) (s := r'.end_) (by rw [hr']) (by rw [hend']) have hlt' : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r'.end_ := by rw [htop]; exact WithTop.coe_lt_top _ have hrev' : r'.reverse = false := by rw [hr']; exact hrev have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = (RangeRead.completeKeys db r').filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by rw [completeKeys_filter_le_eq (db := db) (r := r) hrev hlt, completeKeys_filter_le_eq (db := db) (r := r') hrev' hlt'] rw [compatible_iff_truncated_keys (r := r) hrev hmore hne, compatible_iff_truncated_keys (r := r') hrev' hmore hne, hfil] constructor <;> rintro ⟨h, w⟩ <;> exact ⟨h, w⟩ /-- The forward, crossed, truncated, nonclamping negative-begin cell with end offset one and every reported key below `end_.key`. -/ abbrev negCrossTruncNonclampEndOne (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ r.end_.offset = 1 ∧ (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ reportedBelowBegin r res = beginSlack r + 1 /-- The closed form for the end-one cell, obtained by replacing the end key with `⊤`. -/ noncomputable def closedFormNegCrossTruncNonclampEndOne (r : RangeRead) (res : Result) : ConflictRange := closedFormNegCrossTruncNonclampTop { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res /-- **Closed form for the forward crossed negative-begin truncated nonclamping end-offset-one cell.** Every reported key lies below `end_.key`, so the end selector can be dropped to `⊤` and the unbounded-end rule applies. -/ theorem minimal_closedForm_of_negCross_trunc_nonclamp_endOne {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncNonclampEndOne r res) : Minimal r res (closedFormNegCrossTruncNonclampEndOne r res) := by obtain ⟨hmore, hrev, hbegin, hend, hbelow, hcrossD, hnonclamp_eq⟩ := hcell obtain ⟨db, hcomp⟩ := hread have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore have hcompiff : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := fun db => compatible_iff_end_top_of_offset_one hrev hmore hne hend hbelow have hcellTop : negCrossTruncNonclampTop { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := ⟨hmore, hrev, hbegin, rfl, by simpa using (le_of_eq hend.symm), hcrossD, hnonclamp_eq⟩ have hread' : Readable { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := ⟨db, (hcompiff db).mp hcomp⟩ have hmin := minimal_closedForm_of_negCross_trunc_nonclamp_top hread' hcellTop unfold closedFormNegCrossTruncNonclampEndOne exact (minimal_congr_of_compatible_iff (r := r) (r' := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) (res := res) (cr := closedFormNegCrossTruncNonclampTop { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res) hcompiff).mpr hmin /-- **Dropping the end bound when every reported key lies below `end_.key`.** -/ theorem completeKeys_filter_le_eq_of_below_end {db : DB} {r : RangeRead} {last : Key} (hrev : r.reverse = false) (hend : 1 ≤ r.end_.offset) (hbelow : (last : WithTop Key) < r.end_.key) : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = (RangeRead.completeKeys db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }).filter (fun k : Key => decide (k ≤ last)) := by have hlt : (last : WithTop Key) < resolve db r.end_ := lt_resolve_end_of_lt_end_key hend hbelow have htop : resolve db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }.end_ = (⊤ : WithTop Key) := resolve_eq_top_of_key_eq_top (db := db) (s := _) rfl (by simpa using hend) have hlt' : (last : WithTop Key) < resolve db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }.end_ := by rw [htop]; exact WithTop.coe_lt_top last rw [completeKeys_filter_le_eq (db := db) (r := r) hrev hlt, completeKeys_filter_le_eq (db := db) (r := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) hrev hlt'] /-- **Dropping the end bound whenever every reported key is below `end_.key`.** -/ theorem compatible_iff_end_top_of_below {db : DB} {r : RangeRead} {res : Result} (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hend : 1 ≤ r.end_.offset) (hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : RangeRead.Compatible db r res ↔ RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := by have hlast_lt : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := hbelow _ (List.getLast_mem hne) have hfil := completeKeys_filter_le_eq_of_below_end (db := db) (r := r) (last := res.keys.getLast hne) hrev hend hlast_lt rw [compatible_iff_truncated_keys (r := r) hrev hmore hne, compatible_iff_truncated_keys (r := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) hrev hmore hne, hfil] constructor <;> rintro ⟨h, w⟩ <;> exact ⟨h, w⟩ /-- The forward, crossed, truncated, nonclamping negative-begin cell with positive end offset and every reported key below `end_.key`. -/ abbrev negCrossTruncNonclampBelowEnd (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 1 ≤ r.end_.offset ∧ (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ reportedBelowBegin r res = beginSlack r + 1 /-- The closed form for the below-end cell, obtained by replacing the end key with `⊤`. -/ noncomputable def closedFormNegCrossTruncNonclampBelowEnd (r : RangeRead) (res : Result) : ConflictRange := closedFormNegCrossTruncNonclampTop { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res /-- **Closed form for the forward crossed negative-begin truncated nonclamping below-end cell.** Every reported key lies below `end_.key`, so the end selector can be dropped to `⊤` and the unbounded-end rule applies. -/ theorem minimal_closedForm_of_negCross_trunc_nonclamp_below {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncNonclampBelowEnd r res) : Minimal r res (closedFormNegCrossTruncNonclampBelowEnd r res) := by obtain ⟨hmore, hrev, hbegin, hend, hbelow, hcrossD, hnonclamp_eq⟩ := hcell obtain ⟨db, hcomp⟩ := hread have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore have hcompiff : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := fun db => compatible_iff_end_top_of_below hrev hmore hne hend hbelow have hcellTop : negCrossTruncNonclampTop { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := ⟨hmore, hrev, hbegin, rfl, by simpa using hend, hcrossD, hnonclamp_eq⟩ have hread' : Readable { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := ⟨db, (hcompiff db).mp hcomp⟩ have hmin := minimal_closedForm_of_negCross_trunc_nonclamp_top hread' hcellTop unfold closedFormNegCrossTruncNonclampBelowEnd exact (minimal_congr_of_compatible_iff (r := r) (r' := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) (res := res) (cr := closedFormNegCrossTruncNonclampTop { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res) hcompiff).mpr hmin /-- **Compatibility transfers across agreement on the nonclamping truncated crossed negative-begin range when the end key is at or above the reported head.** Agreement on `[head, max begin.key (keyAfter last))` fixes the begin resolution through the nonclamp count identity, and the end filter count fixes the end resolution above `last`. -/ theorem compatible_of_agree_negCross_trunc_nonclamp_endGeHead {r : RangeRead} {res : Result} (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hge : 1 ≤ r.end_.offset) (hheadE : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) (hagree : AgreeOn (closedFormNegCrossTruncNonclampTop r res) db1 db2) : RangeRead.Compatible db2 r res := by set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), upper⟩ := by unfold closedFormNegCrossTruncNonclampTop rw [hheadD, hlastD, hupper] have hB_upper : r.begin.key ≤ upper := le_max_left _ _ have hlast_upper : ((last : Key) : WithTop Key) < upper := lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampTop r res) db1 db2).mp hagree have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by intro k hk1 hk2 refine hmem k ?_ rw [hcr] exact ⟨hk1, hk2⟩ have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp have hhead_db1 : head ∈ db1.keys := RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) have hhead_upper : ((head : Key) : WithTop Key) < upper := lt_of_lt_of_le hcross hB_upper have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 -- Count identity for `db1`, and the equal gap counts. have hcount1 := count_lt_begin_eq_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp_eq have hcount1' : db1.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db1.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by simpa [hhead] using hcount1 have hsplit1 := countP_lt_eq_add_filter_withTop db1.keys db1.nodup (a := head) (B := r.begin.key) (le_of_lt hcross) have hgaplen1 : (db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by rw [hcount1'] at hsplit1 omega have hgap_eq : db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by apply eq_of_forall_mem_iff_of_pairwise · exact db1.sorted.pairwise.filter _ · exact db2.sorted.pairwise.filter _ · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨hk1, hge', hlt⟩ exact ⟨(hmk k hge' (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge', hlt⟩ · rintro ⟨hk2, hge', hlt⟩ exact ⟨(hmk k hge' (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge', hlt⟩ have hgaplen2 : (db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by rw [← hgap_eq, hgaplen1] have hsplit2 := countP_lt_eq_add_filter_withTop db2.keys db2.nodup (a := head) (B := r.begin.key) (le_of_lt hcross) have hcount2 : db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db2.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by rw [hsplit2, hgaplen2] have cB2_ge : beginSlack r + 1 ≤ db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by rw [hcount2, ← hnonclamp_eq]; omega have hanchor2 : 0 ≤ r.begin.anchor db2 := by rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] have : (beginSlack r : ℤ) + 1 ≤ ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))) : ℤ) := by exact_mod_cast cB2_ge omega have htoNat2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - (beginSlack r + 1) := by have hz : r.begin.anchor db2 = ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)]; push_cast; ring have hz2 : ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ) = ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - (beginSlack r + 1) : ℕ) : ℤ) := by rw [← Nat.cast_sub cB2_ge] rw [hz, hz2, Int.toNat_natCast] have hidx_eq2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k => decide (k < head)) := by rw [htoNat2, hcount2, hnonclamp_eq] omega have hltlen2 : (r.begin.anchor db2).toNat < db2.keys.length := by rw [hidx_eq2] obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_db2 rw [countP_lt_eq_of_getElem db2.sorted hi hix] exact hi have hval2 : db2.keys[(r.begin.anchor db2).toNat]'hltlen2 = head := by simpa [hidx_eq2] using getElem_countP_lt db2.sorted hhead_db2 have hL2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := by rw [resolve_eq_getElem (db := db2) (s := r.begin) hanchor2 hltlen2] exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval2 -- The end resolution is above `last` for both databases, by the end filter count. have hheadE' : ((head : Key) : WithTop Key) ≤ r.end_.key := by rw [hhead]; exact hheadE have hlast_mem_res : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 have hfiltereq : (db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last)) = (db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last)) := by apply List.SortedLT.eq_of_mem_iff · exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _) · exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _) · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨⟨hk1, hEk⟩, hkle⟩ exact ⟨⟨(hmk k (le_trans hheadE' hEk) (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mp hk1, hEk⟩, hkle⟩ · rintro ⟨⟨hk2, hEk⟩, hkle⟩ exact ⟨⟨(hmk k (le_trans hheadE' hEk) (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mpr hk2, hEk⟩, hkle⟩ have hcnt1 : ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := filter_length_le_offset_of_resolve_end_gt (db := db1) hge hlt1 have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by rw [← hfiltereq] exact hcnt1 have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := resolve_end_gt_of_filter_length_le (db := db2) hge hcnt2 have hfilter1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 have hfilter2 : (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) = db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 have hres1fil : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = res.keys := by have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 rwa [← hlast] at h have hfeq : db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by apply eq_of_forall_mem_iff_of_pairwise · exact db1.sorted.pairwise.filter _ · exact db2.sorted.pairwise.filter _ · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨hk1, hge', hle⟩ exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, hge', hle⟩ · rintro ⟨hk2, hge', hle⟩ exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, hge', hle⟩ have hres2fil : (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) = res.keys := by rw [hfilter2, ← hfeq, ← hfilter1, hres1fil] rw [compatible_iff_truncated_keys hrev hmore hne] exact ⟨by simpa using hc1.1, by simpa [hlast] using hres2fil⟩ /-- The forward, crossed, truncated, nonclamping negative-begin cell with positive end offset and the end key at or above the reported head. -/ abbrev negCrossTruncNonclampEndGeHead (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 1 ≤ r.end_.offset ∧ ((leastReported r res : Key) : WithTop Key) ≤ r.end_.key ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ reportedBelowBegin r res = beginSlack r + 1 /-- **Closed form for the forward crossed negative-begin truncated nonclamping end-at-or-above-head cell.** The minimal sound range is again `[head, max begin.key (keyAfter last))`. -/ theorem minimal_closedForm_of_negCross_trunc_nonclamp_endGeHead {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncNonclampEndGeHead r res) : Minimal r res (closedFormNegCrossTruncNonclampTop r res) := by obtain ⟨hmore, hrev, hbegin, hge, hheadED, hcrossD, hnonclamp_eq⟩ := hcell obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hcross : ((head : Key) : WithTop Key) < r.begin.key := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hcrossD exact hcrossD have hheadE : ((head : Key) : WithTop Key) ≤ r.end_.key := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hheadED exact hheadED set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), upper⟩ := by unfold closedFormNegCrossTruncNonclampTop rw [hheadD, hlastD] have hsound : Sound r res (closedFormNegCrossTruncNonclampTop r res) := by intro db1 db2 hagree exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_endGeHead hrev hmore hne hbegin hge hheadE hcross hnonclamp_eq h hagree, fun h => compatible_of_agree_negCross_trunc_nonclamp_endGeHead hrev hmore hne hbegin hge hheadE hcross hnonclamp_eq h hagree.symm⟩ rw [hcr] at hsound ⊢ refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ intro k hkbeg hkend by_cases hkle : k ≤ last · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) (by rw [hlast]; exact List.getLast_mem hne) (WithTop.coe_le_coe.mp hkbeg) hkle · have hkgt : res.keys.getLast hne < k := by simpa [hlast] using lt_of_not_ge hkle have hkltB : (k : WithTop Key) < r.begin.key := by have hkmax : (k : WithTop Key) < max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend rcases lt_max_iff.mp hkmax with h | h · exact h · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) exact absurd hklast hkle exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross hnonclamp_eq hkgt hkltB /-- **Begin resolution and reported window from agreement in the nonclamping crossed negative-begin truncated cell.** Agreement on `[head, max begin.key (keyAfter last))` forces the nonclamp count identity on the second database, hence the same begin resolution `head`, and fixes the stored keys in `[head, last]`. -/ theorem begin_eq_head_and_window_of_agree_negCross_trunc_nonclamp {r : RangeRead} {res : Result} (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) (hmk : ∀ k : Key, ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) → (k : WithTop Key) < max r.begin.key (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) → (k ∈ db1.keys ↔ k ∈ db2.keys)) : resolve db2 r.begin = ((res.keys.head hne : Key) : WithTop Key) ∧ db2.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ res.keys.getLast hne)) = res.keys := by set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper have hB_upper : r.begin.key ≤ upper := le_max_left _ _ have hmk' : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by intro k hk1 hk2 exact hmk k (by simpa [hhead] using hk1) (by simpa [hhead, hlast, hupper] using hk2) have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp have hhead_db1 : head ∈ db1.keys := RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) have hhead_upper : ((head : Key) : WithTop Key) < upper := lt_of_lt_of_le hcross hB_upper have hhead_db2 : head ∈ db2.keys := (hmk' head le_rfl hhead_upper).mp hhead_db1 have hcount1 := count_lt_begin_eq_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp_eq have hcount1' : db1.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db1.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by simpa [hhead] using hcount1 have hsplit1 := countP_lt_eq_add_filter_withTop db1.keys db1.nodup (a := head) (B := r.begin.key) (le_of_lt hcross) have hgaplen1 : (db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by rw [hcount1'] at hsplit1 omega have hgap_eq : db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by apply eq_of_forall_mem_iff_of_pairwise · exact db1.sorted.pairwise.filter _ · exact db2.sorted.pairwise.filter _ · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨hk1, hge', hlt⟩ exact ⟨(hmk' k hge' (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge', hlt⟩ · rintro ⟨hk2, hge', hlt⟩ exact ⟨(hmk' k hge' (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge', hlt⟩ have hgaplen2 : (db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by rw [← hgap_eq, hgaplen1] have hsplit2 := countP_lt_eq_add_filter_withTop db2.keys db2.nodup (a := head) (B := r.begin.key) (le_of_lt hcross) have hcount2 : db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = db2.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by rw [hsplit2, hgaplen2] have cB2_ge : beginSlack r + 1 ≤ db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by rw [hcount2, ← hnonclamp_eq]; omega have hanchor2 : 0 ≤ r.begin.anchor db2 := by rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] have : (beginSlack r : ℤ) + 1 ≤ ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))) : ℤ) := by exact_mod_cast cB2_ge omega have htoNat2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - (beginSlack r + 1) := by have hz : r.begin.anchor db2 = ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)]; push_cast; ring have hz2 : ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ) = ((db2.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - (beginSlack r + 1) : ℕ) : ℤ) := by rw [← Nat.cast_sub cB2_ge] rw [hz, hz2, Int.toNat_natCast] have hidx_eq2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k => decide (k < head)) := by rw [htoNat2, hcount2, hnonclamp_eq] omega have hltlen2 : (r.begin.anchor db2).toNat < db2.keys.length := by rw [hidx_eq2] obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_db2 rw [countP_lt_eq_of_getElem db2.sorted hi hix] exact hi have hval2 : db2.keys[(r.begin.anchor db2).toNat]'hltlen2 = head := by simpa [hidx_eq2] using getElem_countP_lt db2.sorted hhead_db2 have hL2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := by rw [resolve_eq_getElem (db := db2) (s := r.begin) hanchor2 hltlen2] exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval2 have hlast_mem_res : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 have hres1fil : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = res.keys := by have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 rwa [← hlast] at h have hfilter1 := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 have hpart1 : db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by rw [← hfilter1]; exact hres1fil have hpart2 : db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by have heq : db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = db1.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by apply List.SortedLT.eq_of_mem_iff · exact List.sortedLT_iff_pairwise.mpr (db2.sorted.pairwise.filter _) · exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _) · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨hk2, hHk, hklast⟩ exact ⟨(hmk' k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _)))).mpr hk2, hHk, hklast⟩ · rintro ⟨hk1, hHk, hklast⟩ exact ⟨(hmk' k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _)))).mp hk1, hHk, hklast⟩ rw [heq, hpart1] exact ⟨hL2, hpart2⟩ /-- The closed form for the low-end (saturated gap) crossed negative-begin truncated nonclamping cell: from `end.key` instead of the reported head. -/ noncomputable def closedFormNegCrossTruncNonclampLowEnd (r : RangeRead) (res : Result) : ConflictRange := ⟨r.end_.key, max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩ /-- **Compatibility transfers across agreement on the low-end crossed negative-begin truncated nonclamping range when the gap absorbs the reported keys.** -/ theorem compatible_of_agree_negCross_trunc_nonclamp_lowEndSmall {r : RangeRead} {res : Result} (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hge : 2 ≤ r.end_.offset) (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) (hsmall : intervalCard r.end_.key (((res.keys.head hne : Key)) : WithTop Key) + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞)) {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) (hagree : AgreeOn (closedFormNegCrossTruncNonclampTop r res) db1 db2) : RangeRead.Compatible db2 r res := by set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by unfold closedFormNegCrossTruncNonclampTop rw [hheadD, hlastD] set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper have hhead_upper : ((head : Key) : WithTop Key) < upper := lt_of_lt_of_le hcross (le_max_left _ _) have hlast_upper : ((last : Key) : WithTop Key) < upper := lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampTop r res) db1 db2).mp hagree have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by intro k hk1 hk2 refine hmem k ?_ rw [hcr] exact ⟨hk1, hk2⟩ obtain ⟨hL2, hpart2⟩ := begin_eq_head_and_window_of_agree_negCross_trunc_nonclamp hrev hmore hne hbegin hcross hnonclamp_eq hc1 (fun k hk1 hk2 => hmk k hk1 (by simpa [hupper] using hk2)) have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead have hsmall' : intervalCard r.end_.key ((head : Key) : WithTop Key) + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by simpa [hhead] using hsmall have hhead_le_last : head ≤ last := by have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt simp only [hhead, hlast] exact hsort.rel_getLast (List.head_mem hne) have hsplit := filter_end_le_split_between (db := db2) (E := r.end_.key) (head := head) (last := last) (le_of_lt hEhead') hhead_le_last have hbetween : (((between db2 r.end_.key ((head : Key) : WithTop Key)).length : ℕ∞)) ≤ intervalCard r.end_.key ((head : Key) : WithTop Key) := between_length_le_intervalCard (db := db2) (le_of_lt hEhead') have hsum : ((((between db2 r.end_.key ((head : Key) : WithTop Key)).length + res.keys.length : ℕ)) : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by rw [Nat.cast_add] exact le_trans (add_le_add hbetween le_rfl) hsmall' have hpart2' : db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by simpa [hhead, hlast] using hpart2 have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by have hlen : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))).length = (between db2 r.end_.key ((head : Key) : WithTop Key)).length + res.keys.length := by rw [hsplit, hpart2', List.length_append] rw [hlen] exact_mod_cast hsum have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2 have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 rw [compatible_iff_truncated_keys hrev hmore hne] exact ⟨by simpa using hc1.1, by rw [hck2, hpart2']⟩ /-- **Compatibility transfers across agreement on the saturated low-end crossed negative-begin truncated nonclamping range.** -/ theorem compatible_of_agree_negCross_trunc_nonclamp_lowEndBig {r : RangeRead} {res : Result} (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hge : 2 ≤ r.end_.offset) (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) (hagree : AgreeOn (closedFormNegCrossTruncNonclampLowEnd r res) db1 db2) : RangeRead.Compatible db2 r res := by set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hcr : closedFormNegCrossTruncNonclampLowEnd r res = ⟨r.end_.key, max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by unfold closedFormNegCrossTruncNonclampLowEnd rw [hlastD] set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper have hhead_upper : ((head : Key) : WithTop Key) < upper := lt_of_lt_of_le hcross (le_max_left _ _) have hlast_upper : ((last : Key) : WithTop Key) < upper := lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead have hE_upper : r.end_.key < upper := lt_trans hEhead' hhead_upper have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampLowEnd r res) db1 db2).mp hagree have hmkE : ∀ k : Key, r.end_.key ≤ (k : WithTop Key) → (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by intro k hk1 hk2 refine hmem k ?_ rw [hcr] exact ⟨hk1, hk2⟩ have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by intro k hk1 hk2 exact hmkE k (le_trans (le_of_lt hEhead') hk1) hk2 obtain ⟨hL2, hpart2⟩ := begin_eq_head_and_window_of_agree_negCross_trunc_nonclamp hrev hmore hne hbegin hcross hnonclamp_eq hc1 (fun k hk1 hk2 => hmk k hk1 (by simpa [hupper] using hk2)) have hpart2' : db2.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by simpa [hhead, hlast] using hpart2 have hlast_mem_res : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 have hcnt1 : ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := filter_length_le_offset_of_resolve_end_gt (db := db1) (by omega) hlt1 have hcnt_eq : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))) = ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))) := by apply List.SortedLT.eq_of_mem_iff · exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _) · exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _) · intro k simp only [List.mem_filter, decide_eq_true_eq] constructor · rintro ⟨⟨hk2, hEk⟩, hklast⟩ exact ⟨⟨(hmkE k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2, hEk⟩, hklast⟩ · rintro ⟨⟨hk1, hEk⟩, hklast⟩ exact ⟨⟨(hmkE k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1, hEk⟩, hklast⟩ have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by rw [hcnt_eq]; exact hcnt1 have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2 have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 rw [compatible_iff_truncated_keys hrev hmore hne] exact ⟨by simpa using hc1.1, by rw [hck2, hpart2']⟩ /-- **Gap keys below the reported head are necessary in the saturated low-end cell.** A key `k` with `end.key ≤ k < head` is necessary: building a witness with `n` gap keys in `[end.key, head)` excluding `k` puts the begin anchor exactly at the reported head and saturates the end offset. Toggling `k` on then overflows the end offset. -/ theorem necessary_negCross_trunc_between_end_head {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) (hge : 2 ≤ r.end_.offset) (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) (hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + (res.keys.length : ℕ∞)) (hkE : r.end_.key ≤ (k : WithTop Key)) (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast set e1 : ℕ := (r.end_.offset - 1).toNat with he1 set rl : ℕ := res.keys.length with hrl set n : ℕ := e1 - rl with hn have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by rw [hhead]; exact hcross have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead have hkH' : (k : WithTop Key) < ((head : Key) : WithTop Key) := by rw [hhead]; exact hkH have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev have hres_pair : res.keys.Pairwise (· ≤ ·) := hres_sorted.pairwise.imp le_of_lt have hhead_le : ∀ x ∈ res.keys, head ≤ x := by intro x hx have := hres_pair.rel_head hx simpa [hhead] using this have hle_last : ∀ x ∈ res.keys, x ≤ last := by intro x hx have := hres_pair.rel_getLast hx simpa [hlast] using this have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne have hhead_le_last : head ≤ last := hhead_le last hlast_mem -- The reported length is bounded by the end offset. have hlast_ck0 : last ∈ RangeRead.completeKeys db₀ r := (RangeRead.compatible_keys_prefix hcomp₀).subset hlast_mem have hlt0 : ((last : Key) : WithTop Key) < resolve db₀ r.end_ := (RangeRead.mem_completeKeys.mp hlast_ck0).2.2 have hcnt0 : ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter (fun x : Key => decide (x ≤ last))).length ≤ e1 := by simpa [he1] using filter_length_le_offset_of_resolve_end_gt (db := db₀) (by omega) hlt0 have hres_sub : res.keys ⊆ ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter (fun x : Key => decide (x ≤ last))) := by intro x hx have hxdb : x ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hx have hxE : r.end_.key ≤ (x : WithTop Key) := le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)) have hxl : x ≤ last := hle_last x hx rw [List.mem_filter, List.mem_filter] exact ⟨⟨hxdb, by simpa using hxE⟩, by simpa using hxl⟩ have hrle : rl ≤ e1 := by have := hres_sorted.nodup.length_le_of_subset hres_sub exact le_trans (by simpa [hrl] using this) hcnt0 have hbig' : (e1 : ℕ∞) < intervalCard r.end_.key ((head : Key) : WithTop Key) + (rl : ℕ∞) := by simpa [he1, hrl, hhead] using hbig have hcard : ((n + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((head : Key) : WithTop Key) := by by_cases htop : intervalCard r.end_.key ((head : Key) : WithTop Key) = (⊤ : ℕ∞) · rw [htop]; exact le_top · obtain ⟨G, hG⟩ := ENat.ne_top_iff_exists.mp htop have hlt : e1 < G + rl := by have hb := hbig' rw [← hG] at hb rw [← ENat.coe_add] at hb exact ENat.coe_lt_coe.mp hb have hGle : n + 1 ≤ G := by rw [hn]; omega rw [← hG] exact ENat.coe_le_coe.mpr hGle obtain ⟨dbK, hKrange, hkK, hKlen⟩ := exists_db_keysIn_avoid (B := r.end_.key) (E := ((head : Key) : WithTop Key)) (k := k) (m := n) hcard have hK_ge : ∀ x ∈ dbK.keys, r.end_.key ≤ (x : WithTop Key) := fun x hx => (hKrange x hx).1 have hK_lt : ∀ x ∈ dbK.keys, x < head := fun x hx => WithTop.coe_lt_coe.mp (hKrange x hx).2 -- The witness database. have hsort_append : (dbK.keys ++ res.keys).SortedLT := by refine List.sortedLT_append.mpr ⟨dbK.sorted, hres_sorted, ?_⟩ intro a ha b hb exact lt_of_lt_of_le (hK_lt a ha) (hhead_le b hb) let dbw : DB := ⟨dbK.keys ++ res.keys, hsort_append⟩ have hdbw : dbw.keys = dbK.keys ++ res.keys := rfl have hk_notmem : k ∉ dbw.keys := by rw [hdbw, List.mem_append] rintro (hkK' | hkres) · exact hkK hkK' · exact absurd (hhead_le k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkH')) have hhead_dbw : head ∈ dbw.keys := by rw [hdbw]; exact List.mem_append.mpr (Or.inr hhead_mem) -- The begin selector resolves onto the reported head because the anchor is `n`. have hcntB_dbw : cnt dbw r.begin.key = n + reportedBelowBegin r res := by unfold cnt rw [hdbw, List.countP_append] have hKcount : dbK.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = dbK.keys.length := by rw [List.countP_eq_length] intro x hx rw [decide_eq_true_eq] exact lt_trans (WithTop.coe_lt_coe.mpr (hK_lt x hx)) hcross' rw [hKcount, hKlen] rfl have hdbw_len : dbw.keys.length = n + rl := by rw [hdbw, List.length_append, hKlen, hrl] have hanchorB : r.begin.anchor dbw = (n : ℤ) := by have hcountP : dbw.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = n + reportedBelowBegin r res := by simpa only [cnt] using hcntB_dbw rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountP, hnonclamp_eq] push_cast; ring have hanchorB_nonneg : 0 ≤ r.begin.anchor dbw := by rw [hanchorB]; exact Int.natCast_nonneg n have hltlenB : (r.begin.anchor dbw).toNat < dbw.keys.length := by rw [hanchorB, Int.toNat_natCast, hdbw_len] have hpos : 0 < rl := by rw [hrl]; exact List.length_pos_of_ne_nil hne omega have hltlenB_n : n < dbw.keys.length := by rw [hanchorB, Int.toNat_natCast] at hltlenB exact hltlenB have hres_eq_head : res.keys = head :: res.keys.tail := by rw [hhead]; exact (List.cons_head_tail hne).symm have hsplitw : dbw.keys = dbK.keys ++ head :: res.keys.tail := by rw [hdbw, hres_eq_head] rfl have hval_n : dbw.keys[n]'hltlenB_n = head := by have := List.getElem_of_append (l := dbw.keys) (l₁ := dbK.keys) (a := head) (l₂ := res.keys.tail) hsplitw hKlen simpa using this have hidx : (r.begin.anchor dbw).toNat = n := by rw [hanchorB, Int.toNat_natCast] have hresb : resolve dbw r.begin = ((head : Key) : WithTop Key) := by rw [resolve_eq_getElem (db := dbw) (s := r.begin) hanchorB_nonneg hltlenB] simp only [hidx] exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval_n have hmem_all_E : ∀ x ∈ dbw.keys, r.end_.key ≤ (x : WithTop Key) := by intro x hx rw [hdbw, List.mem_append] at hx rcases hx with hx | hx · exact hK_ge x hx · exact le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)) have hcntE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by rw [List.countP_eq_zero] intro x hx rw [decide_eq_true_eq] exact not_lt.mpr (hmem_all_E x hx) have hdbw_len_e1 : dbw.keys.length = e1 := by rw [hdbw_len, hn] exact Nat.sub_add_cancel hrle have hanchorE : (r.end_.anchor dbw).toNat = e1 := by rw [anchor_toNat_eq (db := dbw) (s := r.end_) (by omega), hcntE, ← he1] omega have hrestop : resolve dbw r.end_ = (⊤ : WithTop Key) := by apply resolve_eq_top · exact anchor_nonneg_of_one_le (db := dbw) (s := r.end_) (by omega) · rw [hanchorE, hdbw_len_e1] have hck : RangeRead.completeKeys dbw r = res.keys := by unfold RangeRead.completeKeys RangeRead.inRange rw [ite_eq_right (by simp [hrev]), hresb, hrestop] have hpred : (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < (⊤ : WithTop Key))) = (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) := by funext x; simp rw [hpred, hdbw, List.filter_append] have hleft : dbK.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = [] := by rw [List.filter_eq_nil_iff] intro x hx rw [decide_eq_true_eq] exact not_le.mpr (WithTop.coe_lt_coe.mpr (hK_lt x hx)) have hright : res.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = res.keys := by apply List.filter_eq_self.mpr intro x hx rw [decide_eq_true_eq] exact WithTop.coe_le_coe.mpr (hhead_le x hx) rw [hleft, hright, List.nil_append] have hfilter_le : res.keys.filter (fun x : Key => decide (x ≤ last)) = res.keys := by apply List.filter_eq_self.mpr intro x hx rw [decide_eq_true_eq] exact hle_last x hx have hcompw : RangeRead.Compatible dbw r res := by rw [compatible_iff_truncated_keys hrev hmore hne] exact ⟨by simpa using hcomp₀.1, by rw [hck, hfilter_le]⟩ -- Toggling the target on overflows the end offset. have hdb' : toggle dbw k = dbw.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem have hkeys_toggle : (toggle dbw k).keys = dbw.keys.orderedInsert (· ≤ ·) k := by rw [hdb', DB.insert_keys] have hlen_toggle : (toggle dbw k).keys.length = e1 + 1 := by rw [hkeys_toggle, List.orderedInsert_length, hdbw_len_e1] have hmem_all : ∀ x ∈ (toggle dbw k).keys, r.end_.key ≤ (x : WithTop Key) ∧ x ≤ last := by intro x hx rw [hkeys_toggle, List.mem_orderedInsert] at hx rcases hx with rfl | hx · exact ⟨hkE, le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkH')) hhead_le_last⟩ · rw [hdbw, List.mem_append] at hx rcases hx with hx | hx · exact ⟨hK_ge x hx, le_trans (le_of_lt (hK_lt x hx)) hhead_le_last⟩ · exact ⟨le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)), hle_last x hx⟩ have hcount_toggle : ((toggle dbw k).keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter (fun x : Key => decide (x ≤ last)) = (toggle dbw k).keys := by have h1 : (toggle dbw k).keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) = (toggle dbw k).keys := by apply List.filter_eq_self.mpr intro x hx rw [decide_eq_true_eq] exact (hmem_all x hx).1 rw [h1] apply List.filter_eq_self.mpr intro x hx rw [decide_eq_true_eq] exact (hmem_all x hx).2 have hnot : ¬ RangeRead.Compatible (toggle dbw k) r res := by intro hc have hlast_ck : last ∈ RangeRead.completeKeys (toggle dbw k) r := (RangeRead.compatible_keys_prefix hc).subset hlast_mem have hlt : ((last : Key) : WithTop Key) < resolve (toggle dbw k) r.end_ := (RangeRead.mem_completeKeys.mp hlast_ck).2.2 have hcnt := filter_length_le_offset_of_resolve_end_gt (db := toggle dbw k) (by omega) hlt rw [hcount_toggle, hlen_toggle, ← he1] at hcnt omega exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩ /-- The forward, crossed, truncated, nonclamping negative-begin low-end cell with positive end offset and the end key strictly below the reported head, in the branch where the gap to the head absorbs the reported keys. -/ abbrev negCrossTruncNonclampLowEndSmall (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 2 ≤ r.end_.offset ∧ r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ reportedBelowBegin r res = beginSlack r + 1 ∧ intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) /-- The forward, crossed, truncated, nonclamping negative-begin low-end cell with positive end offset and the end key strictly below the reported head, in the saturated branch. -/ abbrev negCrossTruncNonclampLowEndBig (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 2 ≤ r.end_.offset ∧ r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ reportedBelowBegin r res = beginSlack r + 1 ∧ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) + (res.keys.length : ℕ∞) /-- **Closed form for the low-end absorbing branch.** The minimal range is `[head, max begin.key (keyAfter last))`. -/ theorem minimal_closedForm_of_negCross_trunc_nonclamp_lowEndSmall {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncNonclampLowEndSmall r res) : Minimal r res (closedFormNegCrossTruncNonclampTop r res) := by obtain ⟨hmore, hrev, hbegin, hge, hEheadD, hcrossD, hnonclamp_eq, hsmallD⟩ := hcell obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hcross : ((head : Key) : WithTop Key) < r.begin.key := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hcrossD exact hcrossD have hEhead : r.end_.key < ((head : Key) : WithTop Key) := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hEheadD exact hEheadD have hsmall : intervalCard r.end_.key ((head : Key) : WithTop Key) + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hsmallD exact hsmallD set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), upper⟩ := by unfold closedFormNegCrossTruncNonclampTop rw [hheadD, hlastD] have hsound : Sound r res (closedFormNegCrossTruncNonclampTop r res) := by intro db1 db2 hagree exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndSmall hrev hmore hne hbegin hge hEhead hcross hnonclamp_eq hsmall h hagree, fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndSmall hrev hmore hne hbegin hge hEhead hcross hnonclamp_eq hsmall h hagree.symm⟩ rw [hcr] at hsound ⊢ refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ intro k hkbeg hkend by_cases hkle : k ≤ last · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) (by rw [hlast]; exact List.getLast_mem hne) (WithTop.coe_le_coe.mp hkbeg) hkle · have hkgt : res.keys.getLast hne < k := by simpa [hlast] using lt_of_not_ge hkle have hkltB : (k : WithTop Key) < r.begin.key := by have hkmax : (k : WithTop Key) < max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend rcases lt_max_iff.mp hkmax with h | h · exact h · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) exact absurd hklast hkle exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross hnonclamp_eq hkgt hkltB /-- **Closed form for the saturated low-end branch.** The minimal range is `[end.key, max begin.key (keyAfter last))`. -/ theorem minimal_closedForm_of_negCross_trunc_nonclamp_lowEndBig {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncNonclampLowEndBig r res) : Minimal r res (closedFormNegCrossTruncNonclampLowEnd r res) := by obtain ⟨hmore, hrev, hbegin, hge, hEheadD, hcrossD, hnonclamp_eq, hbigD⟩ := hcell obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore set head : Key := res.keys.head hne with hhead set last : Key := res.keys.getLast hne with hlast have hheadD : res.keys.headD ([] : Key) = head := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hlastD : res.keys.getLastD ([] : Key) = last := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hcross : ((head : Key) : WithTop Key) < r.begin.key := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hcrossD exact hcrossD have hEhead : r.end_.key < ((head : Key) : WithTop Key) := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hEheadD exact hEheadD have hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < intervalCard r.end_.key ((head : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] rw [hle, hheadD] at hbigD exact hbigD set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) have hcr : closedFormNegCrossTruncNonclampLowEnd r res = ⟨r.end_.key, upper⟩ := by unfold closedFormNegCrossTruncNonclampLowEnd rw [hlastD] have hsound : Sound r res (closedFormNegCrossTruncNonclampLowEnd r res) := by intro db1 db2 hagree exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndBig hrev hmore hne hbegin hge hEhead hcross hnonclamp_eq h hagree, fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndBig hrev hmore hne hbegin hge hEhead hcross hnonclamp_eq h hagree.symm⟩ rw [hcr] at hsound ⊢ refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ intro k hkbeg hkend have hhead_nec : Necessary r res head := necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne) have hlast_nec : Necessary r res last := necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne) have hhead_le_last : head ≤ last := by have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt simp only [hhead, hlast] exact hsort.rel_getLast (List.head_mem hne) by_cases hkle : k ≤ last · by_cases hkhead : k < head · exact necessary_negCross_trunc_between_end_head hread' hrev hmore hne hbegin hcross hnonclamp_eq hge hEhead hbig hkbeg (WithTop.coe_lt_coe.mpr hkhead) · exact Necessary.of_between hhead_nec hlast_nec (le_of_not_gt hkhead) hkle · have hkgt : res.keys.getLast hne < k := by simpa [hlast] using lt_of_not_ge hkle have hkltB : (k : WithTop Key) < r.begin.key := by have hkmax : (k : WithTop Key) < max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend rcases lt_max_iff.mp hkmax with h | h · exact h · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) exact absurd hklast hkle exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross hnonclamp_eq hkgt hkltB end FormalConflictRange