/- Released under Apache 2.0 license as described in the file LICENSE. -/ import FormalConflictRange.CrossedRevGeneral /-! # The crossed reversed truncated residual This module closes the last crossed reversed truncated cell: `end_.key < begin_.key`, `begin.offset ≥ 2`, `end.offset ≥ 2`, a nonempty truncated result with a reported key at or above `end_.key`, and `#reported + (begin.offset - 1) < end.offset - 1`. The minimal range always has upper endpoint `⊤`; its lower endpoint is `end_.key` when the key lattice has enough room (a capacity condition on `intervalCard`), and `begin_.key` otherwise. -/ open FormalConflictRange namespace FormalConflictRange /-- In a compatible crossed reversed truncated read, the reported length plus the begin offset shift is at most the end offset shift. -/ theorem crossed_rev_res_length_add_le_end {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) : res.keys.length + (r.begin.offset - 1).toNat ≤ (r.end_.offset - 1).toNat := by set E : WithTop Key := r.end_.key with hE set e : Nat := (r.end_.offset - 1).toNat with he set b : Nat := (r.begin.offset - 1).toNat with hb set g : Nat := (between db E r.begin.key).length with hg set lE : List Key := above db E with hlE have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by simpa [E, e, b, lE, g] using completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by rw [hformula, List.length_reverse, List.length_drop, List.length_take] have htle : res.keys.length ≤ min e lE.length - (g + b) := by rw [← hlen] exact hpre.length_le have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne have hlt : g + b < min e lE.length := by omega have hle : res.keys.length + (g + b) ≤ min e lE.length := by omega have h1 : min e lE.length ≤ e := Nat.min_le_left _ _ omega /-- The `b = begin.offset - 1` keys at or above `begin.key` that are dropped by the crossed reversed window all lie below the least reported key, so the interval from `begin.key` to that key contains at least `b` keys. -/ theorem crossed_rev_b_le_capBL {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) : (((r.begin.offset - 1).toNat : ℕ) : ℕ∞) ≤ intervalCard r.begin.key (((res.keys.getLast hne : Key)) : WithTop Key) := by set E : WithTop Key := r.end_.key with hE set B : WithTop Key := r.begin.key with hB set e : Nat := (r.end_.offset - 1).toNat with he set b : Nat := (r.begin.offset - 1).toNat with hb set g : Nat := (between db E B).length with hg set lE : List Key := above db E with hlE set lB : List Key := above db B with hlB set L : Key := res.keys.getLast hne with hL have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) have hdrop : lB = lE.drop g := by rw [hlB, hlE, hg] exact above_eq_drop_between hEB have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by simpa [E, e, b, lE, g] using completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by rw [hformula, List.length_reverse, List.length_drop, List.length_take] have htle : res.keys.length ≤ min e lE.length - (g + b) := by rw [← hlen] exact hpre.length_le have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne have hlt : g + b < min e lE.length := by omega have hgle : g ≤ lE.length := by omega have hlBlen : lB.length = lE.length - g := by rw [hdrop, List.length_drop] have hblt : b < lB.length := by omega -- the window starts at the `b`-th key at or above `begin.key` have hb_le_e : b ≤ e := by omega have hge_le : g + b ≤ e := by omega have hW : (lE.take e).drop (g + b) = (lB.drop b).take (e - (g + b)) := by rw [List.drop_take, ← List.drop_drop, ← hdrop] have hlenW : ((lB.drop b).take (e - (g + b))).length = min e lE.length - (g + b) := by rw [← hW, List.length_drop, List.length_take] have hWne : (lB.drop b).take (e - (g + b)) ≠ [] := by intro hnil rw [hnil] at hlenW simp at hlenW omega have hdropne : lB.drop b ≠ [] := by intro hnil exact hWne (by rw [hnil]; simp) have hsorte : lE.SortedLT := by rw [hlE]; exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) have hlBsort : lB.SortedLT := by rw [hlB]; exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) have hWpos : 0 < e - (g + b) := by have : 0 < ((lB.drop b).take (e - (g + b))).length := List.length_pos_of_ne_nil hWne rw [List.length_take] at this omega have hsublE : ((lE.take e).drop (g + b)).Sublist lE := (List.drop_sublist _ _).trans (List.take_sublist _ _) have hsortW : ((lB.drop b).take (e - (g + b))).SortedLT := by rw [← hW] exact (List.Pairwise.sublist hsublE hsorte.pairwise).sortedLT have hW0 : ((lB.drop b).take (e - (g + b)))[0]'(List.length_pos_of_ne_nil hWne) = lB[b]'hblt := by rw [List.getElem_take] rw [List.getElem_drop] simp have hLmemR : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne have hCK : RangeRead.completeKeys db r = ((lB.drop b).take (e - (g + b))).reverse := by rw [hformula, hW] have hLmemW : L ∈ (lB.drop b).take (e - (g + b)) := by rw [← List.mem_reverse, ← hCK] exact hpre.subset hLmemR have hhead_le_L : lB[b]'hblt ≤ L := by obtain ⟨j, hj, hjeq⟩ := List.mem_iff_getElem.mp hLmemW have h0len : 0 < ((lB.drop b).take (e - (g + b))).length := List.length_pos_of_ne_nil hWne have hle : ((lB.drop b).take (e - (g + b)))[0]'h0len ≤ ((lB.drop b).take (e - (g + b)))[j]'hj := by by_cases hj0 : j = 0 · subst hj0; rfl · exact le_of_lt ((hsortW.getElem_lt_getElem_iff).mpr (Nat.pos_of_ne_zero hj0)) rw [hW0] at hle rw [← hjeq] exact hle have hpad : ∀ k ∈ lB.take b, k < L := by intro k hk obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hk have htake_len : (lB.take b).length = b := by rw [List.length_take, Nat.min_eq_left (le_of_lt hblt)] have hib : i < b := by omega have hilt : i < lB.length := lt_trans hib hblt have hltake : (lB.take b)[i] = lB[i] := List.getElem_take have hklt : lB[i] < lB[b] := (List.SortedLT.getElem_lt_getElem_iff (l := lB) hlBsort).mpr hib rw [← hik, hltake] exact lt_of_lt_of_le hklt hhead_le_L have hbetween : between db B (((L : Key)) : WithTop Key) = lB.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) := by rw [between_eq_filter_above, ← hlB] have hfilter_len : b ≤ (lB.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key)))).length := by have hall : (lB.take b).filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) = lB.take b := by rw [List.filter_eq_self] intro k hk rw [decide_eq_true_eq] exact WithTop.coe_lt_coe.mpr (hpad k hk) have hsplit : lB.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) = (lB.take b).filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) ++ (lB.drop b).filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) := by rw [← List.filter_append, List.take_append_drop] have htake_len : (lB.take b).length = b := by rw [List.length_take, Nat.min_eq_left (le_of_lt hblt)] rw [hsplit, hall, List.length_append] omega have hBL : B ≤ (((L : Key)) : WithTop Key) := crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L hLmemR have hcard := between_length_le_intervalCard (db := db) (E := B) (B := (((L : Key)) : WithTop Key)) hBL have hble : ((b : ℕ) : ℕ∞) ≤ (((between db B (((L : Key)) : WithTop Key)).length : ℕ) : ℕ∞) := by rw [hbetween] exact_mod_cast hfilter_len have hLB : lB = above db B := hlB calc ((b : ℕ) : ℕ∞) ≤ (((between db B (((L : Key)) : WithTop Key)).length : ℕ) : ℕ∞) := hble _ ≤ intervalCard B (((L : Key)) : WithTop Key) := hcard _ = intervalCard r.begin.key (((res.keys.getLast hne : Key)) : WithTop Key) := by simp [B, L] /-- The crossed residual capacity condition: the end overshoot `e - t` does not exceed the number of keys in `[end_.key, last reported)`. When it holds the minimal range starts at `end_.key`, otherwise at `begin_.key`. -/ def crossedResidualCond (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : Prop := (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) /-- When the keys at or above `end_.key` fit inside the end overshoot, the complete crossed reversed window is the reversed keys at or above `begin.key`, with the begin shift dropped. In particular the gap count no longer appears. -/ theorem crossed_rev_window_eq_above_drop {db : DB} {r : RangeRead} (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hle : (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat) : RangeRead.completeKeys db r = ((above db r.begin.key).drop (r.begin.offset - 1).toNat).reverse := by set E : WithTop Key := r.end_.key with hE set b : Nat := (r.begin.offset - 1).toNat with hb set e : Nat := (r.end_.offset - 1).toNat with he set g : Nat := (between db E r.begin.key).length with hg have hEB : E ≤ r.begin.key := le_of_lt (by simpa [E] using hcross) have hformula : RangeRead.completeKeys db r = (((above db E).take e).drop (g + b)).reverse := by simpa [E, e, b, g] using completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend have htake : (above db E).take e = above db E := List.take_of_length_le (by simpa [E, e] using hle) rw [hformula, htake, above_eq_drop_between hEB, List.drop_drop] /-- Under the failure of the capacity condition, every compatible crossed reversed truncated database has fewer than `end.offset - 1` keys at or above `end_.key`. The end overshoot is too short to fit the reported keys plus the required room below the least reported key. -/ theorem crossed_residual_above_lt_end_of_not_cond {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : (above db r.end_.key).length < (r.end_.offset - 1).toNat := by set E : WithTop Key := r.end_.key with hE set B : WithTop Key := r.begin.key with hB set e : Nat := (r.end_.offset - 1).toNat with he set b : Nat := (r.begin.offset - 1).toNat with hb set g : Nat := (between db E B).length with hg set A : List Key := above db E with hA set L : Key := res.keys.getLast hne with hL set W : List Key := (A.take e).drop (g + b) with hW set t : Nat := res.keys.length with ht set k : Nat := W.length - t with hk have hformula : RangeRead.completeKeys db r = W.reverse := by rw [hW] simpa [E, e, b, g, hA] using completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp have hWlen : W.length = min e A.length - (g + b) := by rw [hW, List.length_drop, List.length_take] have hlen_le : t ≤ W.length := by rw [ht] have h := hpre.length_le rwa [hformula, List.length_reverse] at h have htpos : 0 < t := by rw [ht]; exact List.length_pos_of_ne_nil hne have hkpos : k < W.length := by rw [hk]; omega have hres_eq : res.keys = (W.drop k).reverse := by obtain ⟨suf, hsuf⟩ := hpre have hck : W.reverse = res.keys ++ suf := hformula.symm.trans hsuf.symm have htake : (W.reverse).take t = res.keys := by rw [hck, ht, List.take_left] rw [← htake, List.take_reverse, hk] have hWne : W ≠ [] := by intro hnil rw [hnil] at hkpos simp at hkpos have hgetA : A[g + b + k] = L := by have hltW : k < W.length := hkpos have hltAtake : g + b + k < (A.take e).length := by rw [hW] at hltW rw [List.length_drop] at hltW omega have hltA : g + b + k < A.length := lt_of_lt_of_le hltAtake (List.take_sublist e A).length_le have hWk : W[k]'hltW = A[g + b + k]'hltA := by simp only [hW, List.getElem_drop, List.getElem_take] have hLget : res.keys.getLast hne = W[k]'hltW := by have hdropne : W.drop k ≠ [] := by intro h; rw [List.drop_eq_nil_iff] at h; omega have h1 : res.keys.getLast? = some (res.keys.getLast hne) := List.getLast?_eq_some_getLast hne have h2 : res.keys.getLast? = some (W[k]'hltW) := by rw [hres_eq, List.getLast?_reverse, List.head?_eq_some_head hdropne, List.head_drop hdropne] exact Option.some.inj (h1.symm.trans h2) rw [hL, hLget, hWk] have hsA : A.SortedLT := by rw [hA] exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) have hcount : A.countP (fun x : Key => decide (x < L)) = g + b + k := by have hltA : g + b + k < A.length := by have hltAtake : g + b + k < (A.take e).length := by have hltW : k < W.length := hkpos rw [hW] at hltW rw [List.length_drop] at hltW omega exact lt_of_lt_of_le hltAtake (List.take_sublist e A).length_le exact countP_lt_eq_of_getElem hsA hltA hgetA have hbetween_len : (between db E ((L : Key) : WithTop Key)).length = g + b + k := by have hfil : between db E ((L : Key) : WithTop Key) = A.filter (fun x : Key => decide (x < L)) := by rw [between_eq_filter_above, ← hA] apply List.filter_congr intro x _ rw [decide_eq_decide] exact WithTop.coe_lt_coe rw [hfil, ← List.countP_eq_length_filter] exact hcount have hEL : E ≤ ((L : Key) : WithTop Key) := by have hBL : B ≤ ((L : Key) : WithTop Key) := crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by rw [hL]; exact List.getLast_mem hne) exact le_trans (le_of_lt (by simpa [E, B] using hcross)) hBL have hcard : (((g + b + k : ℕ)) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by have := between_length_le_intervalCard (db := db) hEL rw [hbetween_len] at this exact this by_contra hge have heA : e ≤ A.length := le_of_not_gt hge have hWlen' : W.length = e - (g + b) := by rw [hWlen, Nat.min_eq_left heA] have hkW : k = (e - (g + b)) - t := by rw [hk, hWlen'] have htle' : t ≤ e - (g + b) := by rw [← hWlen']; exact hlen_le have hsum : g + b + k = e - t := by omega apply hnot rw [crossedResidualCond] have hcast : (((e - t : ℕ)) : ℕ∞) = (((g + b + k : ℕ)) : ℕ∞) := by rw [hsum] rw [hcast] simpa [E, L] using hcard /-- Under the failure of the capacity condition, the stored keys at or above the reported last are exactly the reversed reported keys. -/ theorem crossed_residual_above_last_eq_rev {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : above db (((res.keys.getLast hne : Key)) : WithTop Key) = res.keys.reverse := by set E : WithTop Key := r.end_.key with hE set B : WithTop Key := r.begin.key with hB set e : Nat := (r.end_.offset - 1).toNat with he set b : Nat := (r.begin.offset - 1).toNat with hb set L : Key := res.keys.getLast hne with hL set t : Nat := res.keys.length with ht set A : List Key := above db B with hA have hle : (above db E).length ≤ e := le_of_lt (crossed_residual_above_lt_end_of_not_cond hcomp hrev hbegin hcross hend hne hnot) have hwin : RangeRead.completeKeys db r = ((above db B).drop b).reverse := crossed_rev_window_eq_above_drop (db := db) hrev hbegin hcross hend hle have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp obtain ⟨suf, hsuf⟩ := hpre have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev have hBL : B ≤ ((L : Key) : WithTop Key) := crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by rw [hL]; exact List.getLast_mem hne) have hWdecomp : (above db B).drop b = suf.reverse ++ res.keys.reverse := by have hr := congrArg List.reverse hsuf rw [List.reverse_append] at hr rw [hwin, List.reverse_reverse] at hr exact hr.symm have hAdecomp : A = A.take b ++ (suf.reverse ++ res.keys.reverse) := by calc A = A.take b ++ A.drop b := (List.take_append_drop b A).symm _ = A.take b ++ (above db B).drop b := by rw [hA] _ = A.take b ++ (suf.reverse ++ res.keys.reverse) := by rw [hWdecomp] have hAsorted : A.SortedLT := by rw [hA] exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) have hpair := hAsorted.pairwise rw [hAdecomp, List.pairwise_append] at hpair obtain ⟨_, hSuffix, hcrosslt⟩ := hpair obtain ⟨_, _, hcrossSuf⟩ := List.pairwise_append.mp hSuffix have hLmem : L ∈ res.keys.reverse := by rw [List.mem_reverse] rw [hL] exact List.getLast_mem hne have haboveL_eq : above db ((L : Key) : WithTop Key) = A.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ (k : WithTop Key))) := by rw [hA] unfold above rw [List.filter_filter] apply List.filter_congr intro k _ by_cases hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) · have hBk : B ≤ (k : WithTop Key) := le_trans hBL hLk simp [hLk, hBk] · simp [hLk] have hfilter : A.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ (k : WithTop Key))) = res.keys.reverse := by rw [hAdecomp, List.filter_append, List.filter_append] have hPnil : (A.take b).filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ (k : WithTop Key))) = [] := by rw [List.filter_eq_nil_iff] intro x hx hge have hLmem' : L ∈ suf.reverse ++ res.keys.reverse := List.mem_append.mpr (Or.inr hLmem) have hlt : x < L := hcrosslt x hx L hLmem' exact absurd (of_decide_eq_true hge) (not_le.mpr (WithTop.coe_lt_coe.mpr hlt)) have hSnil : suf.reverse.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ (k : WithTop Key))) = [] := by rw [List.filter_eq_nil_iff] intro x hx hge have hlt : x < L := hcrossSuf x hx L hLmem exact absurd (of_decide_eq_true hge) (not_le.mpr (WithTop.coe_lt_coe.mpr hlt)) have hRself : res.keys.reverse.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ (k : WithTop Key))) = res.keys.reverse := by rw [List.filter_eq_self] intro x hx rw [decide_eq_true_eq] have hxmem : x ∈ res.keys := (List.mem_reverse.mp hx) exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hxmem) rw [hPnil, hSnil, hRself, List.nil_append, List.nil_append] exact haboveL_eq.trans hfilter /-- Under the failure of the capacity condition, the stored keys at or above `begin.key` are bounded by the key-lattice gap below the reported last plus the number of reported keys. -/ theorem crossed_residual_above_begin_le_card_add {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : (((above db r.begin.key).length : ℕ) : ℕ∞) ≤ intervalCard r.begin.key (((res.keys.getLast hne : Key)) : WithTop Key) + (res.keys.length : ℕ∞) := by set B : WithTop Key := r.begin.key with hB set L : Key := res.keys.getLast hne with hL set t : Nat := res.keys.length with ht set A : List Key := above db B with hA have hBL : B ≤ ((L : Key) : WithTop Key) := crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by rw [hL]; exact List.getLast_mem hne) have haboveL : above db ((L : Key) : WithTop Key) = res.keys.reverse := crossed_residual_above_last_eq_rev hcomp hrev hbegin hcross hend hne hnot have hpart : A.length = (between db B ((L : Key) : WithTop Key)).length + t := by have h := length_above_eq_between_add_above (db := db) (E := B) (L := ((L : Key) : WithTop Key)) hBL rw [haboveL, List.length_reverse, ← ht] at h simpa [A] using h have hbetween_le : (((between db B ((L : Key) : WithTop Key)).length : ℕ) : ℕ∞) ≤ intervalCard B ((L : Key) : WithTop Key) := between_length_le_intervalCard (db := db) hBL have hcast : ((A.length : ℕ) : ℕ∞) ≤ intervalCard B ((L : Key) : WithTop Key) + (t : ℕ∞) := by rw [hpart, Nat.cast_add] exact add_le_add_left hbetween_le (t : ℕ∞) simpa [A, B, L, t] using hcast /-- If a database agrees with a compatible database on `[begin.key, ⊤)` in the crossed reversed truncated residual under the failure of the capacity condition, it has fewer than `end.offset - 1` keys at or above `end_.key`. This is exactly the room needed for the crossed reversed window to ignore the gap. -/ theorem crossed_residual_above_end_lt_of_agree {db db' : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) (hagree : above db' r.begin.key = above db r.begin.key) : (above db' r.end_.key).length < (r.end_.offset - 1).toNat := by set E : WithTop Key := r.end_.key with hE set B : WithTop Key := r.begin.key with hB set e : Nat := (r.end_.offset - 1).toNat with he set t : Nat := res.keys.length with ht set L : Key := res.keys.getLast hne with hL have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) have hBL : B ≤ ((L : Key) : WithTop Key) := crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by rw [hL]; exact List.getLast_mem hne) have hA' : (above db' B).length = (above db B).length := by rw [hagree] have hAle : (((above db' B).length : ℕ) : ℕ∞) ≤ intervalCard B ((L : Key) : WithTop Key) + (t : ℕ∞) := by rw [hA'] exact crossed_residual_above_begin_le_card_add hcomp hrev hbegin hcross hend hne hnot have hpart : (above db' E).length = (between db' E B).length + (above db' B).length := length_above_eq_between_add_above (db := db') hEB have hbetween_le : (((between db' E B).length : ℕ) : ℕ∞) ≤ intervalCard E B := between_length_le_intervalCard (db := db') hEB have hsumcast : ((above db' E).length : ℕ∞) ≤ intervalCard E B + intervalCard B ((L : Key) : WithTop Key) + (t : ℕ∞) := by rw [hpart, Nat.cast_add] have h := add_le_add hbetween_le hAle simpa [add_assoc] using h have hadd : intervalCard E B + intervalCard B ((L : Key) : WithTop Key) = intervalCard E ((L : Key) : WithTop Key) := (intervalCard_add hEB hBL).symm rw [hadd] at hsumcast have hnotC : ¬ ((((e - t : ℕ)) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key)) := by rw [crossedResidualCond] at hnot simpa [E, L, e, t] using hnot have hnot' : intervalCard E ((L : Key) : WithTop Key) < ((e - t : ℕ) : ℕ∞) := not_le.mp hnotC have htle : t ≤ e := by have h := crossed_rev_res_length_add_le_end hcomp hrev hbegin hcross hend hne omega have hlt : ((above db' E).length : ℕ∞) < (e : ℕ∞) := by calc ((above db' E).length : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := hsumcast _ < ((e - t : ℕ) : ℕ∞) + (t : ℕ∞) := WithTop.add_lt_add_right (z := (t : ℕ∞)) (ENat.natCast_ne_top t) hnot' _ = (e : ℕ∞) := by rw [← Nat.cast_add]; congr 1; omega have := ENat.natCast_lt_natCast.mp hlt simpa [E, e] using this /-- Soundness of the crossed reversed truncated residual's `begin.key` top branch: under the failure of the capacity condition, agreement on `[begin.key, ⊤)` fixes the complete keys, because both databases have fewer than `end.offset - 1` keys at or above `end_.key` and the window then collapses to the keys at or above `begin.key`. -/ theorem sound_crossedResidual_top_begin {r : RangeRead} {res : Result} (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : Sound r res ⟨r.begin.key, ⊤⟩ := by set B : WithTop Key := r.begin.key with hB set b : Nat := (r.begin.offset - 1).toNat with hb intro db₁ db₂ hagree have habove : above db₁ B = above db₂ B := by have h := hagree rw [AgreeOn] at h simpa [ConflictRange.keys, above, B] using h have hkeys (db : DB) (hc : RangeRead.Compatible db r res) : RangeRead.completeKeys db r = ((above db B).drop b).reverse := by have hlt : (above db r.end_.key).length < (r.end_.offset - 1).toNat := crossed_residual_above_lt_end_of_not_cond hc hrev hbegin hcross hend hne hnot simpa [B, b] using crossed_rev_window_eq_above_drop (db := db) hrev hbegin hcross hend (le_of_lt hlt) constructor · intro hc₁ have hlt₂ : (above db₂ r.end_.key).length < (r.end_.offset - 1).toNat := crossed_residual_above_end_lt_of_agree hc₁ hrev hbegin hcross hend hne hnot habove.symm have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by rw [hkeys db₁ hc₁] have h2 : RangeRead.completeKeys db₂ r = ((above db₂ B).drop b).reverse := by simpa [B, b] using crossed_rev_window_eq_above_drop (db := db₂) hrev hbegin hcross hend (le_of_lt hlt₂) rw [h2, habove] simpa [RangeRead.Compatible, hck] using hc₁ · intro hc₂ have hlt₁ : (above db₁ r.end_.key).length < (r.end_.offset - 1).toNat := crossed_residual_above_end_lt_of_agree hc₂ hrev hbegin hcross hend hne hnot habove have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by rw [hkeys db₂ hc₂] have h1 : RangeRead.completeKeys db₁ r = ((above db₁ B).drop b).reverse := by simpa [B, b] using crossed_rev_window_eq_above_drop (db := db₁) hrev hbegin hcross hend (le_of_lt hlt₁) rw [h1, habove] simpa [RangeRead.Compatible, hck] using hc₂ /-- In a compatible crossed reversed truncated read, more than `begin.offset - 1` stored keys lie at or above `begin.key`. -/ theorem crossed_rev_above_begin_length_gt_shift {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) : (r.begin.offset - 1).toNat < (above db r.begin.key).length := by set E : WithTop Key := r.end_.key with hE set B : WithTop Key := r.begin.key with hB set e : Nat := (r.end_.offset - 1).toNat with he set b : Nat := (r.begin.offset - 1).toNat with hb set g : Nat := (between db E B).length with hg set A : List Key := above db E with hA have hformula : RangeRead.completeKeys db r = ((A.take e).drop (g + b)).reverse := by simpa [E, B, e, b, g, A] using completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp have hlen : (RangeRead.completeKeys db r).length = min e A.length - (g + b) := by rw [hformula, List.length_reverse, List.length_drop, List.length_take] have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne have htle : res.keys.length ≤ min e A.length - (g + b) := by rw [← hlen]; exact hpre.length_le have hmin : g + b < min e A.length := by omega have hAle : min e A.length ≤ A.length := Nat.min_le_right _ _ have hApart : A.length = g + (above db B).length := by have h := length_above_eq_between_add_above (db := db) (E := E) (L := B) (le_of_lt (by simpa [E, B] using hcross)) simpa [A, g] using h omega /-- In the crossed reversed truncated residual, every key in `[begin.key, last reported)` is necessary. The witness database has exactly `begin.offset - 1` stored keys in `[begin.key, last reported)`, one of them the target key `k`, followed by the reversed reported keys; erasing `k` shifts the window by one and drops the last reported key. -/ theorem crossed_residual_necessary_below_last_not_cond {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : 2 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hres : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) {k : Key} (hBk : r.begin.key ≤ (k : WithTop Key)) (hkL : ((k : Key) : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread set E : WithTop Key := r.end_.key with hE set B : WithTop Key := r.begin.key with hB set e : Nat := (r.end_.offset - 1).toNat with he set b : Nat := (r.begin.offset - 1).toNat with hb set L : Key := res.keys.getLast hne with hL have hbegin1 : 1 ≤ r.begin.offset := by omega have hres' : res.keys.length + b < e := by omega have hbpos : 0 < b := by omega have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev have hLle : ∀ x ∈ res.keys, L ≤ x := by intro x hx rw [hL] exact (hsort.pairwise.imp le_of_lt).rel_getLast hx have hBltL : B < ((L : Key) : WithTop Key) := lt_of_le_of_lt hBk hkL have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) have hbcard : (((b : ℕ)) : ℕ∞) ≤ intervalCard B ((L : Key) : WithTop Key) := by simpa [B, L, b] using crossed_rev_b_le_capBL (db := db₀) (r := r) (res := res) hcomp₀ hrev hbegin1 hcross hend hne have hsub : b - 1 + 1 = b := Nat.sub_add_cancel (Nat.succ_le_of_lt hbpos) have hcast : (((b - 1 : ℕ)) : ℕ∞) + 1 = ((b : ℕ) : ℕ∞) := by exact_mod_cast hsub have hcard' : (((b - 1 : ℕ)) : ℕ∞) + 1 ≤ intervalCard B ((L : Key) : WithTop Key) := by rw [hcast] exact hbcard obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ := exists_db_keysIn_avoid (B := B) (E := ((L : Key) : WithTop Key)) (k := k) (m := b - 1) hcard' set P : List Key := (pad.insert k hkpad).keys with hP have hPmem : ∀ x ∈ P, B ≤ (x : WithTop Key) ∧ ((x : Key) : WithTop Key) < ((L : Key) : WithTop Key) := by intro x hx have hx' : x ∈ List.orderedInsert (fun x1 x2 => x1 ≤ x2) k pad.keys := by rw [hP, DB.insert_keys] at hx exact hx rw [List.mem_orderedInsert] at hx' rcases hx' with rfl | hx' · exact ⟨hBk, hkL⟩ · simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx' have hPsort : P.SortedLT := by rw [hP] exact (pad.insert k hkpad).sorted have hPlen : P.length = b := by rw [hP, DB.insert_keys, List.orderedInsert_length, hpadlen, hsub] have hkP : k ∈ P := by rw [hP, DB.insert_keys] rw [List.mem_orderedInsert] exact Or.inl rfl set R : List Key := res.keys.reverse with hR have hRsort : R.SortedLT := by rw [hR] exact hsort.reverse have hcrossPR : ∀ x ∈ P, ∀ y ∈ R, x < y := by intro x hx y hy have hxL : x < L := WithTop.coe_lt_coe.mp (hPmem x hx).2 have hyres : y ∈ res.keys := by rw [hR] at hy exact List.mem_reverse.mp hy exact lt_of_lt_of_le hxL (hLle y hyres) have hfullSort : (P ++ R).SortedLT := List.sortedLT_append.mpr ⟨hPsort, hRsort, hcrossPR⟩ set dbfull : DB := ⟨P ++ R, hfullSort⟩ with hdbfull have hfullKeys : dbfull.keys = P ++ R := rfl have hRmem : ∀ y ∈ R, L ≤ y := by intro y hy have hyres : y ∈ res.keys := by rw [hR] at hy exact List.mem_reverse.mp hy exact hLle y hyres have haboveFullB : above dbfull B = P ++ R := by rw [above_eq_filter, hfullKeys, List.filter_eq_self] intro x hx rw [List.mem_append] at hx rcases hx with hx | hx · exact decide_eq_true (hPmem x hx).1 · exact decide_eq_true (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx))) have haboveFullE : above dbfull E = P ++ R := by rw [above_eq_filter, hfullKeys, List.filter_eq_self] intro x hx rw [List.mem_append] at hx rcases hx with hx | hx · exact decide_eq_true (le_trans hEB (hPmem x hx).1) · exact decide_eq_true (le_trans hEB (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx)))) have hfullElen : (above dbfull E).length ≤ e := by rw [haboveFullE, List.length_append, hPlen, hR, List.length_reverse] omega have hckFull : RangeRead.completeKeys dbfull r = res.keys := by rw [crossed_rev_window_eq_above_drop (db := dbfull) hrev hbegin1 hcross hend hfullElen, haboveFullB, List.drop_append_of_le_length (le_of_eq hPlen.symm), List.drop_eq_nil_of_le (le_of_eq hPlen), List.nil_append, hR, List.reverse_reverse] have hcompFull : RangeRead.Compatible dbfull r res := by refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ simp [hckFull] refine necessary_of_toggle ⟨dbfull, hcompFull, ?_⟩ have hkFull : k ∈ dbfull.keys := by rw [hfullKeys] exact List.mem_append.mpr (Or.inl hkP) rw [toggle_eq_erase_of_mem hkFull] intro hcompA have hka : k ∉ R := by intro hkR have hkres : k ∈ res.keys := by rw [hR] at hkR exact List.mem_reverse.mp hkR exact absurd (hLle k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkL)) have hAkeys : (dbfull.erase k).keys = P.erase k ++ R := by rw [DB.erase_keys, hfullKeys, List.erase_append_left R hkP] have hPlen' : (P.erase k).length = b - 1 := by rw [List.length_erase_of_mem hkP, hPlen] have haboveAB : above (dbfull.erase k) B = P.erase k ++ R := by rw [above_eq_filter, hAkeys, List.filter_eq_self] intro x hx rw [List.mem_append] at hx rcases hx with hx | hx · exact decide_eq_true (hPmem x (List.mem_of_mem_erase hx)).1 · exact decide_eq_true (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx))) have haboveAE : above (dbfull.erase k) E = P.erase k ++ R := by rw [above_eq_filter, hAkeys, List.filter_eq_self] intro x hx rw [List.mem_append] at hx rcases hx with hx | hx · exact decide_eq_true (le_trans hEB (hPmem x (List.mem_of_mem_erase hx)).1) · exact decide_eq_true (le_trans hEB (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx)))) have hAelen : (above (dbfull.erase k) E).length ≤ e := by rw [haboveAE, List.length_append, hPlen', hR, List.length_reverse] omega have hckA : RangeRead.completeKeys (dbfull.erase k) r = res.keys.dropLast := by rw [crossed_rev_window_eq_above_drop (db := dbfull.erase k) hrev hbegin1 hcross hend hAelen, haboveAB, List.drop_append] have hdropP : (P.erase k).drop b = [] := List.drop_eq_nil_of_le (by rw [hPlen']; omega) rw [hdropP, List.nil_append, hPlen'] have hb1 : b - (b - 1) = 1 := by omega rw [hb1, hR, List.drop_reverse, List.reverse_reverse, List.dropLast_eq_take] have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne rcases hcompA.2 with ⟨_, hpre, _⟩ | ⟨hm, _⟩ · rw [hckA] at hpre have hle := hpre.length_le rw [List.length_dropLast] at hle omega · rw [hmore] at hm simp at hm /-- Inserting a key above every element of a sorted list appends it. -/ theorem orderedInsert_eq_append_of_forall_lt {l : List Key} {k : Key} (h : ∀ x ∈ l, x < k) : List.orderedInsert (fun x1 x2 => x1 ≤ x2) k l = l ++ [k] := by induction l with | nil => simp | cons a t ih => rw [List.orderedInsert_cons] have hat : a < k := h a (by simp) rw [ite_eq_right (not_le.mpr hat), ih (fun x hx => h x (by simp [hx]))] rfl /-- Under the failure of the capacity condition, every key above the reported head is necessary: inserting it appends it to the complete window, whose first element would then have to be the reported head. -/ theorem crossed_residual_necessary_above_head_not_cond {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) {k : Key} (hkH : res.keys.head hne < k) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread set E : WithTop Key := r.end_.key with hE set B : WithTop Key := r.begin.key with hB set e : Nat := (r.end_.offset - 1).toNat with he set b : Nat := (r.begin.offset - 1).toNat with hb set H : Key := res.keys.head hne with hH set A : List Key := above db₀ B with hA have hlt₀ : (above db₀ E).length < e := crossed_residual_above_lt_end_of_not_cond hcomp₀ hrev hbegin hcross hend hne hnot have hwin₀ : RangeRead.completeKeys db₀ r = (A.drop b).reverse := by simpa [A, B, b, E, e] using crossed_rev_window_eq_above_drop (db := db₀) hrev hbegin hcross hend (le_of_lt hlt₀) have hpre₀ : res.keys <+: (A.drop b).reverse := by rw [← hwin₀] exact RangeRead.compatible_keys_prefix hcomp₀ have hpre₀len : res.keys.length ≤ (A.drop b).reverse.length := List.IsPrefix.length_le hpre₀ obtain ⟨suf, hsuf⟩ := hpre₀ have hAdrop : A.drop b = suf.reverse ++ res.keys.reverse := by have h := congrArg List.reverse hsuf rw [List.reverse_append, List.reverse_reverse] at h exact h.symm have hAsort : A.SortedLT := by rw [hA] exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne have hAdrop_ne : A.drop b ≠ [] := by intro hnil have h : res.keys ++ suf = ([] : List Key) := by rw [hsuf, hnil]; simp have hlen := congrArg List.length h rw [List.length_append, List.length_nil] at hlen omega have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev have hLeH : ∀ y ∈ res.keys, y ≤ H := by intro y hy have := (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hy simpa [hH] using this have hdrop_le_H : ∀ y ∈ A.drop b, y ≤ H := by intro y hy rw [hAdrop, List.mem_append] at hy have hRne : res.keys.reverse ≠ [] := by intro h rw [List.reverse_eq_nil_iff] at h exact hne h rcases hy with hy | hy · have hsufSort : (suf.reverse ++ res.keys.reverse).SortedLT := by rw [← hAdrop] exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop hAsort.pairwise) have hcrossS : ∀ a ∈ suf.reverse, ∀ z ∈ res.keys.reverse, a < z := (List.sortedLT_append.mp hsufSort).2.2 have hylt : y < res.keys.reverse.head hRne := hcrossS y hy _ (List.head_mem hRne) have hhead : res.keys.reverse.head hRne = res.keys.getLast hne := by rw [List.head_reverse] have hLH : res.keys.reverse.head hRne ≤ H := by rw [hhead] exact hLeH _ (List.getLast_mem hne) exact le_trans (le_of_lt hylt) hLH · exact hLeH y (List.mem_reverse.mp hy) have hAall : ∀ x ∈ A, x ≤ H := by intro x hx have hAeq : A = A.take b ++ A.drop b := (List.take_append_drop b A).symm rw [hAeq, List.mem_append] at hx rcases hx with hx | hx · have hcrossT : ∀ a ∈ A.take b, ∀ z ∈ A.drop b, a < z := by have hs := List.sortedLT_append.mp (by rw [← hAeq]; exact hAsort) exact hs.2.2 exact le_trans (le_of_lt (hcrossT x hx _ (List.head_mem hAdrop_ne))) (hdrop_le_H _ (List.head_mem hAdrop_ne)) · exact hdrop_le_H x hx have hBH : B ≤ ((H : Key) : WithTop Key) := by simpa [B, H] using crossed_rev_res_ge_begin_general (db := db₀) (r := r) (res := res) hcomp₀ hbegin H (by rw [hH]; exact List.head_mem hne) have hHltk : (H : WithTop Key) < (k : WithTop Key) := WithTop.coe_lt_coe.mpr hkH have hEk : E ≤ (k : WithTop Key) := le_trans (le_of_lt (by simpa [E, B] using hcross)) (le_trans hBH (le_of_lt hHltk)) have hk_not : k ∉ db₀.keys := by intro hkdb have hkA : k ∈ A := by rw [hA, mem_above] exact ⟨hkdb, le_trans hBH (le_of_lt hHltk)⟩ exact absurd hkH (not_lt_of_ge (hAall k hkA)) have hdb_all : ∀ x ∈ db₀.keys, x < k := by intro x hx by_cases hxB : B ≤ (x : WithTop Key) · have hxA : x ∈ A := by rw [hA, mem_above] exact ⟨hx, hxB⟩ exact lt_of_le_of_lt (hAall x hxA) hkH · have hlt : (x : WithTop Key) < B := lt_of_not_ge hxB exact WithTop.coe_lt_coe.mp (lt_trans hlt (lt_of_le_of_lt hBH hHltk)) set dbfull' : DB := db₀.insert k hk_not with hdbfull' have hins : dbfull'.keys = db₀.keys ++ [k] := by rw [hdbfull', DB.insert_keys] exact orderedInsert_eq_append_of_forall_lt hdb_all have habove'B : above dbfull' B = A ++ [k] := by rw [above_eq_filter, hins] simp [above_eq_filter, hA, decide_eq_true (le_trans hBH (le_of_lt hHltk))] have habove'E : above dbfull' E = above db₀ E ++ [k] := by rw [above_eq_filter, hins] simp [above_eq_filter, decide_eq_true hEk] have hAb : b < A.length := by simpa [A, B, b] using crossed_rev_above_begin_length_gt_shift (db := db₀) (r := r) (res := res) hcomp₀ hrev hbegin hcross hend hne have hle₁ : (above dbfull' E).length ≤ e := by rw [habove'E, List.length_append, List.length_singleton] omega have hck₁ : RangeRead.completeKeys dbfull' r = [k] ++ (res.keys ++ suf) := by rw [crossed_rev_window_eq_above_drop (db := dbfull') hrev hbegin hcross hend hle₁, habove'B, List.drop_append_of_le_length (le_of_lt hAb), List.reverse_append] simp only [List.reverse_singleton] rw [← hsuf] refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ rw [toggle_eq_insert_of_not_mem hk_not, ← hdbfull'] intro hcomp' have hpre' : res.keys <+: [k] ++ (res.keys ++ suf) := by rcases hcomp'.2 with ⟨_, hp, _⟩ | ⟨hm, _⟩ · rwa [hck₁] at hp · rw [hmore] at hm simp at hm have h0 := hpre'.getElem (i := 0) htpos have hlhs : res.keys[0] = H := by simpa [hH] using (List.getElem_zero htpos) have hrhs : ([k] ++ (res.keys ++ suf))[0] = k := by simp [List.getElem_cons_zero] rw [hlhs, hrhs] at h0 exact absurd hkH (not_lt_of_ge (le_of_eq h0.symm)) /-- Under the failure of the capacity condition, the crossed reversed truncated residual has minimal range `[begin.key, ⊤)`. -/ theorem minimal_crossedResidual_not_cond {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : 2 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) (hres : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) (hnot : ¬ crossedResidualCond r res hne) : Minimal r res ⟨r.begin.key, ⊤⟩ := by have hbegin1 : 1 ≤ r.begin.offset := by omega refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ · exact sound_crossedResidual_top_begin hrev hbegin1 hcross hend hne hnot · intro k hk rw [ConflictRange.mem_iff] at hk obtain ⟨hkB, _⟩ := hk set L : Key := res.keys.getLast hne with hL set H : Key := res.keys.head hne with hH by_cases hkL : (k : WithTop Key) < ((L : Key) : WithTop Key) · exact crossed_residual_necessary_below_last_not_cond hread hmore hrev hbegin hcross hend hne hres hkB hkL · have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hkL by_cases hkHle : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) · exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne) (by rw [hH]; exact List.head_mem hne) (WithTop.coe_le_coe.mp hLk) (WithTop.coe_le_coe.mp hkHle) · have hHk : ((H : Key) : WithTop Key) < (k : WithTop Key) := lt_of_not_ge hkHle exact crossed_residual_necessary_above_head_not_cond hread hmore hrev hbegin1 hcross hend hne hnot (WithTop.coe_lt_coe.mp hHk) end FormalConflictRange