/- 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.NegCrossTruncClampLastGe import FormalConflictRange.NegCrossClampNonpos import FormalConflictRange.NegCrossClosedForm /-! # The crossed negative-begin truncated bottom-gap cell A forward truncated result with all reported keys strictly below `begin.key`, a negative begin offset, a nonpositive end offset, a clamping begin selector, `begin.key ≤ end_.key`, and enough gap capacity above `begin.key` for the end overshoot. In this cell the begin selector clamps to the empty key and the minimal sound range is `[⊥, end_.key)`. The cell is the complement of the crossed short cell in the region where the reported keys do not reach the begin key. Its closed form is proved by a case analysis on the target key `k < end_.key`: * reported keys are necessary by `necessary_of_mem_result`; * keys between the reported head and last by `necessary_of_between_result`; * keys below the reported head by a toggle witness: padding the reported keys with `clampNonposD + 1` gap keys above `begin.key` gives a compatible database, and inserting a key below the head adds it to the truncated window; * keys between the reported last and `end_.key` by an erase witness: a database formed from the reported keys, the target, and the gap pads resolves its end selector onto the target, and erasing the target drops the end resolution onto the reported last, which removes the last reported key from the truncated window. -/ namespace FormalConflictRange /-- A forward truncated, crossed, negative-begin read with a nonpositive end offset that clamps, whose reported keys all lie below `begin.key`, and whose gap above `begin.key` has room for the end overshoot. -/ def negCrossTruncBottomGap (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧ r.begin.key ≤ r.end_.key ∧ r.end_.key ≠ (⊤ : WithTop Key) ∧ ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key /-- The gap-capacity condition forces `clampNonposBottom` in the bottom-gap cell. -/ theorem clampNonposBottom_of_negCross_trunc_bottomGap {r : RangeRead} {res : Result} (hcell : negCrossTruncBottomGap r res) : clampNonposBottom r res := by obtain ⟨-, -, -, -, -, -, hclamp, -, -, hcap⟩ := hcell unfold clampNonposBottom have hzero : (((clampNonposD r + 1 : ℕ) : ℕ∞) - intervalCard r.begin.key r.end_.key) = 0 := tsub_eq_zero_of_le hcap rw [hzero, add_zero] exact Nat.cast_le.mpr hclamp /-- The reported last key is the last of the result, and it lies below `begin.key`. -/ theorem negCrossTruncBottomGap_last {r : RangeRead} {res : Result} (hne : res.keys ≠ []) (_hcell : negCrossTruncBottomGap r res) : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl /-- Padding the reported keys with `clampNonposD + 1` keys that lie below `end_.key` and above the reported last key — with at most one of them below `begin.key` — gives a compatible database. The begin selector clamps (or resolves to the reported head), and the end selector resolves to the first padding key, so the truncated filter is exactly the reported keys. -/ theorem exists_padDb_compatible {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hBE : r.begin.key ≤ r.end_.key) (hEne : r.end_.key ≠ (⊤ : WithTop Key)) (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) (dbF : DB) (k : Key) (hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x) (hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key) (hFge : ∀ x ∈ dbF.keys, x = k ∨ r.begin.key ≤ (x : WithTop Key)) (hFlen : dbF.keys.length = clampNonposD r + 1) : ∃ dbw : DB, dbw.keys = res.keys ++ dbF.keys ∧ RangeRead.Compatible dbw r res ∧ r.end_.anchor dbw = (res.keys.length : ℤ) ∧ dbw.keys.length = res.keys.length + (clampNonposD r + 1) := by classical obtain ⟨db₀, hcomp₀⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev set last : Key := res.keys.getLast hne with hlast have hlastD : res.keys.getLastD ([] : Key) = last := by rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by rw [← hlastD]; exact hlastB have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx have hlastE : ((last : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le hlastB' hBE have hFne : dbF.keys ≠ [] := by intro h; rw [h] at hFlen; simp at hFlen -- count of padding keys below `begin.key` is at most one have hFcnt_le : dbF.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) ≤ 1 := by rw [List.countP_eq_length_filter] set l := dbF.keys.filter (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) with hl have hnodup : l.Nodup := hl ▸ (dbF.sorted.pairwise.nodup.filter _) have hall : ∀ x ∈ l, x = k := by intro x hx rw [hl] at hx obtain ⟨hxmem, hxlt⟩ := List.mem_filter.mp hx have hxlt' : (x : WithTop Key) < r.begin.key := of_decide_eq_true hxlt rcases hFge x hxmem with hk | hge · exact hk · exact absurd (lt_of_lt_of_le hxlt' hge) (lt_irrefl (x : WithTop Key)) by_contra h have h2 : 1 < l.length := by omega have heq : l[0] = l[1] := by rw [hall l[0] (List.getElem_mem (by omega)), hall l[1] (List.getElem_mem (by omega))] exact (by omega : (0 : ℕ) ≠ 1) ((hnodup.getElem_inj).mp heq) -- sorted concatenation set D : List Key := res.keys ++ dbF.keys with hD have hDsort : D.SortedLT := by rw [hD] exact List.sortedLT_append.mpr ⟨hsort, dbF.sorted, fun a ha b hb => lt_of_le_of_lt (hle_last a ha) (hFlast b hb)⟩ let dbw : DB := ⟨D, hDsort⟩ have hdbw_keys : dbw.keys = D := rfl -- the begin anchor is nonpositive have hcount_le : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) ≤ reportedBelowBegin r res + 1 := by rw [hdbw_keys, hD, List.countP_append] have h1 : res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := rfl rw [h1] exact Nat.add_le_add_left hFcnt_le _ have hanchor_le : r.begin.anchor dbw ≤ 0 := by rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin)] have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp have hle' : (dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) : ℤ) ≤ (reportedBelowBegin r res : ℤ) + 1 := by exact_mod_cast hcount_le omega -- the begin resolution is the empty key or the reported head set H : Key := if r.begin.anchor dbw < 0 then ([] : Key) else res.keys.head hne with hH have hHle : H ≤ res.keys.head hne := by by_cases hneg : r.begin.anchor dbw < 0 · rw [hH]; simp only [hneg, ↓reduceIte]; exact nil_le_key _ · rw [hH]; simp only [hneg, ↓reduceIte] exact le_rfl have hrb : resolve dbw r.begin = ((H : Key) : WithTop Key) := by by_cases hneg : r.begin.anchor dbw < 0 · rw [resolve_eq_nil hneg] rw [hH]; simp only [hneg, ↓reduceIte] · have hzero : r.begin.anchor dbw = 0 := by omega have h0 : 0 ≤ r.begin.anchor dbw := by omega have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by rw [hzero, hdbw_keys, hD, List.length_append, hFlen] simp rw [resolve_eq_getElem h0 hlt] have hz : (r.begin.anchor dbw).toNat = 0 := by rw [hzero]; rfl have hget : dbw.keys[(r.begin.anchor dbw).toNat] = res.keys.head hne := by simp only [hzero, hdbw_keys, hD] have hpos : 0 < res.keys.length := List.length_pos_iff.mpr hne rw [List.getElem_append_left (i := (0 : Int).toNat) (h := by simpa using hpos)] exact (List.head_eq_getElem hne).symm rw [hget] rw [hH]; simp only [hneg, ↓reduceIte] -- every stored key is below the end key have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by intro x hx rw [hD] at hx rcases List.mem_append.mp hx with hx | hx · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE · exact hFend x hx have hDlen : D.length = res.keys.length + (clampNonposD r + 1) := by rw [hD, List.length_append, hFlen] have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = D.length := by rw [List.countP_eq_length] intro x hx rw [decide_eq_true_eq] exact hDlt x hx have hd_int : ((clampNonposD r : ℕ) : ℤ) = -r.end_.offset := by rw [clampNonposD_eq] exact Int.toNat_of_nonneg (by omega) have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by cases hE : r.end_.key with | top => exact absurd hE hEne | coe e => have hce : D.countP (fun x => decide (x < e)) = D.length := by rw [← countP_lt_coe (l := D) e, ← hE] exact hcount_end rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hDlen] push_cast omega have ht_lt : res.keys.length < D.length := by rw [hDlen]; omega have hDget? : dbw.keys[res.keys.length]? = some (dbF.keys.head hFne) := by change D[res.keys.length]? = some (dbF.keys.head hFne) rw [hD, List.getElem?_append] rw [ite_eq_right (not_lt.mpr (le_refl _)), Nat.sub_self] rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] have hre : resolve dbw r.end_ = ((dbF.keys.head hFne : Key) : WithTop Key) := by rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)] rw [hanchor_end, Int.toNat_natCast, hDget?] have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by rw [hre] exact WithTop.coe_lt_coe.mpr (hFlast _ (List.head_mem hFne)) -- the truncated filter is the reported keys have hck : (RangeRead.completeKeys dbw r).filter (fun x : Key => decide (x ≤ last)) = res.keys := by rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF] rw [hdbw_keys, hD, List.filter_append] have hres_fil : res.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by rw [List.filter_eq_self] intro x hx rw [decide_eq_true_eq] exact ⟨le_trans (WithTop.coe_le_coe.mpr (hHle.trans (by exact (hsort.pairwise.imp le_of_lt).rel_head hx))) le_rfl, hle_last x hx⟩ have hF_fil : dbF.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by rw [List.filter_eq_nil_iff] intro x hx simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] exact fun _ => not_le.mpr (hFlast x hx) rw [hres_fil, hF_fil, List.append_nil] have hcompw : RangeRead.Compatible dbw r res := by rw [compatible_iff_truncated_keys hrev hmore hne] exact ⟨hcomp₀.1, hck⟩ exact ⟨dbw, by rw [hdbw_keys, hD], hcompw, hanchor_end, by rw [hdbw_keys, hDlen]⟩ /-- A key strictly below the reported head has a flip witness in the bottom-gap cell: the padded database is compatible, and inserting the key adds it to the truncated window because the begin selector resolves to the empty key or to the inserted key itself. -/ theorem exists_witness_below_head_bottomGap {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hcell : negCrossTruncBottomGap r res) (hne : res.keys ≠ []) (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ ¬ RangeRead.Compatible (toggle dbw k) r res := by obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hBE, hEne, hcap⟩ := hcell have hread' := hread obtain ⟨db₀, hcomp₀⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hkHead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := hk 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 hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by rw [← hlastD]; exact hcross have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlast_lt_B have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hkHead hhead_lt_B have hk_not_res : k ∉ res.keys := by intro hx exact absurd hkHead (not_lt.mpr (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn (B := r.begin.key) (E := r.end_.key) (m := clampNonposD r + 1) hcap have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by intro x hx exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B (hFmem x hx).1) have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2 have hFge : ∀ x ∈ dbF.keys, x = k ∨ r.begin.key ≤ (x : WithTop Key) := fun x hx => Or.inr (hFmem x hx).1 obtain ⟨dbw, hkeyw, hcompw, -, hlenw⟩ := exists_padDb_compatible hread' hrev hmore hne hbegin hend hclamp hBE hEne hcross dbF k hFlast hFend hFge hFlen -- every stored key is strictly above `k` have hk_lt_all : ∀ x ∈ dbw.keys, k < x := by intro x hx rw [hkeyw] at hx rcases List.mem_append.mp hx with hx | hx · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hkHead (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hk_lt_B (hFmem x hx).1) have hk_not_dbw : k ∉ dbw.keys := fun hx => absurd (hk_lt_all k hx) (lt_irrefl k) refine ⟨dbw, hcompw, ?_⟩ rw [toggle_eq_insert_of_not_mem hk_not_dbw] intro hcomp' have hins_keys : (dbw.insert k hk_not_dbw).keys = k :: dbw.keys := by rw [DB.insert_keys] cases hkw : dbw.keys with | nil => simp | cons a t => have hka : k ≤ a := le_of_lt (hk_lt_all a (by rw [hkw]; exact List.mem_cons_self ..)) rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka] obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' -- the begin anchor of the inserted database is still nonpositive have hrescnt : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by rw [hkeyw, List.countP_append] have h1 : res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by rw [List.countP_eq_length] intro x hx rw [decide_eq_true_eq] exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B have h2 : dbF.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by rw [List.countP_eq_zero] intro x hx simp only [decide_eq_true_eq] exact not_lt.mpr (hFmem x hx).1 rw [h1, h2, Nat.add_zero] have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length + 1 := by rw [hins_keys, List.countP_cons, decide_eq_true_eq.mpr hk_lt_B, ite_eq_left rfl, hrescnt] have hreported : reportedBelowBegin r res = res.keys.length := by rw [reportedBelowBegin, List.countP_eq_length] intro x hx rw [decide_eq_true_eq] exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B have hanchor_ins : r.begin.anchor (dbw.insert k hk_not_dbw) ≤ 0 := by rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins] have hle : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) := by rw [hreported] at hclamp exact_mod_cast hclamp push_cast omega have hk_ck : k ∈ RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := by rw [RangeRead.mem_completeKeys] refine ⟨by rw [hins_keys]; exact List.mem_cons_self .., ?_, ?_⟩ · by_cases hneg : r.begin.anchor (dbw.insert k hk_not_dbw) < 0 · rw [resolve_eq_nil hneg] exact WithTop.coe_le_coe.mpr (nil_le_key k) · have hzero : r.begin.anchor (dbw.insert k hk_not_dbw) = 0 := by omega have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by omega have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat < (dbw.insert k hk_not_dbw).keys.length := by rw [hzero, hins_keys, List.length_cons]; simp rw [resolve_eq_getElem h0 hlt] have hget : (dbw.insert k hk_not_dbw).keys[(r.begin.anchor (dbw.insert k hk_not_dbw)).toNat] = k := by simp only [hzero, hins_keys] rfl rw [hget] · have hpre := RangeRead.compatible_keys_prefix hcomp' have hhead_mem : res.keys.head hne ∈ RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := hpre.subset (List.head_mem hne) exact lt_trans hkHead (RangeRead.mem_completeKeys.mp hhead_mem).2.2 have hk_le_last : k ≤ res.keys.getLast hne := le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkHead)) hhead_le_last have hk_filt : k ∈ (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by rw [List.mem_filter] exact ⟨hk_ck, by rw [decide_eq_true_eq]; exact hk_le_last⟩ rw [hfilt'] at hk_filt exact hk_not_res hk_filt /-- A key strictly above the reported last and strictly below the end key has an erase witness in the bottom-gap cell: the target key sits among `clampNonposD + 1` padding keys in `[begin.key, end_.key)`, so the end selector resolves into the padding; erasing the target lowers the end resolution to the reported last, which drops the last reported key from the truncated window. -/ theorem exists_witness_erase_bottomGap {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hcell : negCrossTruncBottomGap r res) (hne : res.keys ≠ []) (hkgt : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) (hkE : (k : WithTop Key) < r.end_.key) : ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ ¬ RangeRead.Compatible (dbw.erase k) r res := by obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hBE, hEne, hcap⟩ := hcell have hread' := hread obtain ⟨db₀, hcomp₀⟩ := hread have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hlast_lt_k : res.keys.getLast hne < k := WithTop.coe_lt_coe.mp hkgt have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by rw [← hlastD]; exact hcross have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le hlast_lt_B hBE have hk_not_res : k ∉ res.keys := by intro hx exact absurd hlast_lt_k (not_lt.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) -- pad keys in `[begin.key, end_.key)` avoiding `k` obtain ⟨dbB, hBmem, hBk, hBlen⟩ := exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := k) (m := clampNonposD r) hcap set raw : List Key := k :: dbB.keys with hraw have hrawNodup : raw.Nodup := by rw [hraw] exact List.nodup_cons.mpr ⟨hBk, dbB.sorted.pairwise.nodup⟩ set F : List Key := raw.insertionSort (· ≤ ·) with hF have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm have hFsorted : F.SortedLT := by rw [hF, List.sortedLT_iff_pairwise] exact DB.pairwise_lt_of_le_of_nodup (List.pairwise_insertionSort (r := (· ≤ ·)) raw) (hFperm.nodup_iff.mp hrawNodup) let dbF : DB := ⟨F, hFsorted⟩ have hFlen : dbF.keys.length = clampNonposD r + 1 := by change F.length = clampNonposD r + 1 rw [hF, List.length_insertionSort, hraw, List.length_cons, hBlen] have hFmem' : ∀ x ∈ dbF.keys, x = k ∨ (r.begin.key ≤ (x : WithTop Key) ∧ (x : WithTop Key) < r.end_.key) := by intro x hx have hx' := hFperm.mem_iff.mpr hx rw [hraw] at hx' rcases List.mem_cons.mp hx' with rfl | hxB · exact Or.inl rfl · exact Or.inr ⟨(hBmem x hxB).1, (hBmem x hxB).2⟩ have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by intro x hx rcases hFmem' x hx with rfl | ⟨hxB, -⟩ · exact hlast_lt_k · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B hxB) have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := by intro x hx rcases hFmem' x hx with rfl | ⟨-, hxE⟩ · exact hkE · exact hxE have hFge : ∀ x ∈ dbF.keys, x = k ∨ r.begin.key ≤ (x : WithTop Key) := by intro x hx rcases hFmem' x hx with rfl | ⟨hxB, -⟩ · exact Or.inl rfl · exact Or.inr hxB obtain ⟨dbw, hkeyw, hcompw, hanchw, hlenw⟩ := exists_padDb_compatible hread' hrev hmore hne hbegin hend hclamp hBE hEne hcross dbF k hFlast hFend hFge hFlen have hk_in_F : k ∈ dbF.keys := hFperm.mem_iff.mp (by rw [hraw]; exact List.mem_cons_self ..) have hk_in_dbw : k ∈ dbw.keys := by rw [hkeyw]; exact List.mem_append.mpr (Or.inr hk_in_F) refine ⟨dbw, hcompw, hk_in_dbw, ?_⟩ intro hcomp' have herase_keys : (dbw.erase k).keys = res.keys ++ dbF.keys.erase k := by rw [DB.erase_keys, hkeyw, List.erase_append_right _ hk_not_res] obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' -- the erased end resolution is the reported last have hcount_erase : (dbw.erase k).keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = res.keys.length + clampNonposD r := by rw [herase_keys, List.countP_append] have h1 : res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = res.keys.length := by rw [List.countP_eq_length] intro x hx rw [decide_eq_true_eq] exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_E have hFall : dbF.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = dbF.keys.length := by rw [List.countP_eq_length] intro x hx rw [decide_eq_true_eq] exact hFend x hx have hktrue : (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) k = true := by rw [decide_eq_true_eq]; exact hkE have h2 : (dbF.keys.erase k).countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = clampNonposD r := by rw [List.countP_erase, ite_eq_left ⟨hk_in_F, hktrue⟩, hFall, hFlen] omega rw [h1, h2] have hd_int : ((clampNonposD r : ℕ) : ℤ) = -r.end_.offset := by rw [clampNonposD_eq] exact Int.toNat_of_nonneg (by omega) have hanchor_erase : r.end_.anchor (dbw.erase k) = (res.keys.length : ℤ) - 1 := by cases hE : r.end_.key with | top => exact absurd hE hEne | coe e => rw [anchor_of_coe (s := r.end_) (v := e) hE] have hce : (dbw.erase k).keys.countP (fun x => decide (x < e)) = res.keys.length + clampNonposD r := by rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] exact hcount_erase rw [hce] push_cast omega have ht_pos : 0 < res.keys.length := List.length_pos_iff.mpr hne have hresolve_end : resolve (dbw.erase k) r.end_ = ((res.keys.getLast hne : Key) : WithTop Key) := by have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanchor_erase]; omega have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by rw [herase_keys, List.length_append, List.length_erase, ite_eq_left hk_in_F, hFlen] omega have hlt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by rw [hanchor_erase, hlenErase] omega rw [resolve_eq_getElem h0 hlt] have hidx : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by rw [hanchor_erase] have : ((res.keys.length : ℤ) - 1).toNat = res.keys.length - 1 := by omega exact this have hget : (dbw.erase k).keys[(r.end_.anchor (dbw.erase k)).toNat] = res.keys.getLast hne := by simp only [hidx, herase_keys] have hlen : res.keys.length - 1 < res.keys.length := by omega rw [List.getElem_append_left (i := res.keys.length - 1) (h := hlen)] exact (List.getLast_eq_getElem hne).symm rw [hget] have hlast_not_ck : res.keys.getLast hne ∉ RangeRead.completeKeys (dbw.erase k) r := by intro hmem have hlt := (RangeRead.mem_completeKeys.mp hmem).2.2 rw [hresolve_end] at hlt exact lt_irrefl _ hlt have hlast_in_filt : res.keys.getLast hne ∈ (RangeRead.completeKeys (dbw.erase k) r).filter (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by rw [hfilt'] exact List.getLast_mem hne rw [List.mem_filter] at hlast_in_filt exact hlast_not_ck hlast_in_filt.1 /-- The closed form of the bottom-gap cell: `[⊥, end_.key)`. -/ noncomputable def closedFormNegCrossTruncBottomGap (r : RangeRead) (_res : Result) : ConflictRange := ⟨(([] : Key) : WithTop Key), r.end_.key⟩ /-- **The crossed negative-begin truncated bottom-gap cell is minimal.** -/ theorem minimal_closedForm_of_negCross_trunc_bottomGap {r : RangeRead} {res : Result} (hread : Readable r res) (hcell : negCrossTruncBottomGap r res) : Minimal r res (closedFormNegCrossTruncBottomGap r res) := by obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hBE, hEne, hcap⟩ := hcell have hcell' : negCrossTruncBottomGap r res := ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hBE, hEne, hcap⟩ refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_bottomEnd_of_clamp_nonpos_prefix hrev hbegin hend hBE, ?_⟩ intro k _ hkE by_cases hkres : k ∈ res.keys · exact necessary_of_mem_result hread hkres · by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) · exact necessary_of_toggle (exists_witness_below_head_bottomGap hread hcell' hne hkhead) · push Not at hkhead by_cases hklast : ((k : Key) : WithTop Key) ≤ ((res.keys.getLast hne : Key) : WithTop Key) · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) (WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast) · push Not at hklast obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := exists_witness_erase_bottomGap hread hcell' hne hklast hkE exact necessary_of_toggle ⟨dbw, hcompw, by rw [toggle_eq_erase_of_mem hkdbw] exact hnot⟩ end FormalConflictRange