/- Released under Apache 2.0 license as described in the file LICENSE. -/ import FormalConflictRange.NegCrossTruncRevResidual /-! # The large-gap crossed top cell of the reversed truncated residual This module closes the last *top-branch* subcell of the reversed crossed negative-begin truncated residual. The setting is * `res.more = true`, `r.reverse = true`, `r.begin.offset < 0`, `res.keys ≠ []`; * `2 ≤ r.end_.offset`; * the end key lies strictly below the reported last (`r.end_.key < last`); * the top branch holds (`#reported ≥ end_.key < end_.offset - 1`); * the crossed gap is *large*: the interval `[end_.key, last)` has room for the `end_.offset - 1 - #reported` keys that a genuine end resolution needs. In this cell the minimal range is `[end_.key, ⊤)`. The reported last cannot be the begin of the range: a database can store extra gap keys below it, while inserting a key at `end_.key` changes which stored key the end selector lands on and breaks the reported prefix. So the range must start at or below `end_.key`. The soundness proof shows agreement on `[end_.key, ⊤)` fixes both resolutions: the end selector agrees by `resolve_eq_of_agree_ge`, and the begin selector stays at or below the reported last on both databases, so `completeKeys_filter_ge_eq_resolve_end` finishes. -/ namespace FormalConflictRange /-- The large-gap crossed top subcell of the reversed truncated residual. -/ abbrev negCrossTruncRevResidualTopLarge (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ 2 ≤ r.end_.offset ∧ r.end_.key < ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < (r.end_.offset - 1).toNat ∧ ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + (res.keys.length : ℕ∞) /-! ### Soundness of `[end_.key, ⊤)` -/ /-- **Soundness of the top range ending at the end key in the large-gap cell.** Agreement on `[end_.key, ⊤)` fixes the end resolution and keeps the begin resolution at or below the reported last, so the truncated complete keys are determined. -/ theorem sound_topRange_endKey_of_residual_large {r : RangeRead} {res : Result} (hmore : res.more = true) (hrev : r.reverse = true) (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) : Sound r res ⟨r.end_.key, ⊤⟩ := by intro db₁ db₂ hagree -- agreement at or above `end_.key` have habove : above db₁ r.end_.key = above db₂ r.end_.key := by have h := hagree rw [AgreeOn] at h rw [keys_top_eq_above, keys_top_eq_above] at h exact h -- agreement at or above the reported last (a sub-filter of `above end_.key`) have haboveL : above db₁ ((res.keys.getLast hne : Key) : WithTop Key) = above db₂ ((res.keys.getLast hne : Key) : WithTop Key) := by rw [above_B_eq_filter (db := db₁) (E := r.end_.key) (B := ((res.keys.getLast hne : Key) : WithTop Key)) (le_of_lt hEL), above_B_eq_filter (db := db₂) (E := r.end_.key) (B := ((res.keys.getLast hne : Key) : WithTop Key)) (le_of_lt hEL), habove] -- the end selector agrees have hresend : resolve db₁ r.end_ = resolve db₂ r.end_ := by refine resolve_eq_of_agree_ge (db₁ := db₁) (db₂ := db₂) (s := r.end_) (by omega) ?_ intro k hk have h1 : k ∈ above db₁ r.end_.key ↔ k ∈ above db₂ r.end_.key := by rw [habove] rw [mem_above, mem_above] at h1 exact ⟨fun h => (h1.mp ⟨h, hk⟩).1, fun h => (h1.mpr ⟨h, hk⟩).1⟩ -- a single forward step, used in both directions have step : ∀ (db db' : DB), RangeRead.Compatible db r res → resolve db r.end_ = resolve db' r.end_ → above db ((res.keys.getLast hne : Key) : WithTop Key) = above db' ((res.keys.getLast hne : Key) : WithTop Key) → RangeRead.Compatible db' r res := by intro db db' hc hrend habL obtain ⟨hlim, hfil⟩ := (compatible_iff_truncated_keys_rev hrev hmore hne).mp hc -- agreement on `[last, ⊤)` propagated to the key-level filter have hl : db.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k)) = db'.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k)) := by have h := habL simp only [above] at h rw [show (fun k : Key => decide (((res.keys.getLast hne : Key) : WithTop Key) ≤ (k : WithTop Key))) = (fun k : Key => decide (res.keys.getLast hne ≤ k)) from by funext k; simp [WithTop.coe_le_coe]] at h exact h have hLmem : (res.keys.getLast hne) ∈ db.keys := RangeRead.compatible_keys_subset hc (List.getLast_mem hne) have hrb : resolve db r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by by_cases hB : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key · exact resolve_begin_le_of_between_bound hLmem hB (between_add_offset_le_one hc hmore hrev hne hB) · exact le_trans (resolve_le_key_of_offset_nonpos (db := db) (s := r.begin) (by omega)) (le_of_not_gt hB) have hLmem' : (res.keys.getLast hne) ∈ db'.keys := by have h1 : (res.keys.getLast hne) ∈ above db ((res.keys.getLast hne : Key) : WithTop Key) := by rw [mem_above]; exact ⟨hLmem, le_refl _⟩ rw [habL] at h1 rw [mem_above] at h1 exact h1.1 have hrb' : resolve db' r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by by_cases hB : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key · have hbeq : between db ((res.keys.getLast hne : Key) : WithTop Key) r.begin.key = between db' ((res.keys.getLast hne : Key) : WithTop Key) r.begin.key := by apply between_eq_of_agree · unfold AgreeOn rw [keys_top_eq_above, keys_top_eq_above] exact habL · exact le_of_lt hB · exact le_top have hbound := between_add_offset_le_one hc hmore hrev hne hB rw [hbeq] at hbound exact resolve_begin_le_of_between_bound hLmem' hB hbound · exact le_trans (resolve_le_key_of_offset_nonpos (db := db') (s := r.begin) (by omega)) (le_of_not_gt hB) have hw := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) (last := res.keys.getLast hne) hrev hrb have hw' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) (last := res.keys.getLast hne) hrev hrb' have hinner : (db.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k))).filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = (db'.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k))).filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db' r.end_)) := by rw [hl, hrend] rw [compatible_iff_truncated_keys_rev hrev hmore hne] refine ⟨hlim, ?_⟩ rw [hw', ← hinner, ← hw] exact hfil constructor · intro hc exact step db₁ db₂ hc hresend haboveL · intro hc exact step db₂ db₁ hc hresend.symm haboveL.symm /-! ### Above-head necessity -/ /-- **Every key above the reported head is necessary in the large-gap crossed top cell.** The database of reported keys (ascending) is compatible; inserting a key above the reported head makes it the head of the reversed complete window, breaking the reported prefix. -/ theorem necessary_above_head_of_residual_top_large {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (_hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) (ht : res.keys.length < (r.end_.offset - 1).toNat) (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) {k : Key} (hkH : res.keys.head hne < k) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread set H : Key := res.keys.head hne with hHdef set L : Key := res.keys.getLast hne with hLdef have hbound : (reportedBelowBegin r res : Int) + r.begin.offset - 1 ≤ 0 := by have h1 := reportedBelowBegin_le_between (db := db₀) hcomp₀ hmore hrev hne have h2 := between_add_offset_le_one (db := db₀) hcomp₀ hmore hrev hne hcross linarith have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev have hHge : ∀ x ∈ res.keys, x ≤ H := by intro x hx; rw [hHdef]; exact (hsort.pairwise.imp le_of_lt).rel_head hx have hLle : ∀ x ∈ res.keys, L ≤ x := by intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx have hk_notin : k ∉ res.keys := by intro hx have hle := hHge k hx have hleH : k ≤ res.keys.head hne := by rw [← hHdef]; exact hle exact absurd hleH (not_le.mpr hkH) have ha_sorted : res.keys.reverse.SortedLT := hsort.reverse let db_a : DB := ⟨res.keys.reverse, ha_sorted⟩ have ha_keys : db_a.keys = res.keys.reverse := rfl have hcnt0 : cnt db_a r.end_.key = 0 := by apply cnt_eq_zero_of_forall_ge intro x hx rw [ha_keys] at hx exact le_trans (le_of_lt hEL) (WithTop.coe_le_coe.mpr (hLle x (List.mem_reverse.mp hx))) have haresolve : resolve db_a r.begin ≤ ((L : Key) : WithTop Key) := by have hanchor : r.begin.anchor db_a = (reportedBelowBegin r res : Int) + r.begin.offset - 1 := by have hcnt : cnt db_a r.begin.key = reportedBelowBegin r res := by unfold cnt reportedBelowBegin rw [ha_keys, List.countP_reverse] change (cnt db_a r.begin.key : Int) + r.begin.offset - 1 = _ rw [hcnt] have hle : r.begin.anchor db_a ≤ 0 := by rw [hanchor]; linarith by_cases hneg : r.begin.anchor db_a < 0 · rw [resolve_eq_nil hneg] exact WithTop.coe_le_coe.mpr (nil_le_key L) · have hanc0 : r.begin.anchor db_a = 0 := le_antisymm hle (le_of_not_gt hneg) have hlen : 0 < db_a.keys.length := by rw [ha_keys, List.length_reverse] exact List.length_pos_of_ne_nil hne have h0lt : (r.begin.anchor db_a).toNat < db_a.keys.length := by rw [hanc0]; simpa using hlen have hget0 : db_a.keys[(r.begin.anchor db_a).toNat] = L := by have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanc0]; rfl have h0 : db_a.keys[(r.begin.anchor db_a).toNat]? = some L := by rw [hidx, ha_keys, ← List.head?_eq_getElem?] rw [List.head?_reverse, List.getLast?_eq_some_getLast hne] have h1 := List.getElem?_eq_getElem h0lt rw [h0] at h1 exact (Option.some.inj h1).symm rw [resolve_eq_getElem (le_of_not_gt hneg) h0lt, hget0] have hre_a : resolve db_a r.end_ = (⊤ : WithTop Key) := by have hanc : (r.end_.anchor db_a).toNat = (r.end_.offset - 1).toNat := by rw [anchor_toNat_eq (db := db_a) (s := r.end_) (by omega)] simp only [cnt] at hcnt0 rw [hcnt0, Nat.zero_add] refine resolve_eq_top (by change 0 ≤ (cnt db_a r.end_.key : Int) + r.end_.offset - 1 rw [hcnt0]; push_cast; omega) ?_ rw [hanc, ha_keys, List.length_reverse] exact le_of_lt ht have hck_a : RangeRead.completeKeys db_a r = res.keys := by unfold RangeRead.completeKeys RangeRead.inRange rw [hrev, hre_a] simp only [ite_true] have hpred : db_a.keys.filter (fun x => decide (resolve db_a r.begin ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < (⊤ : WithTop Key))) = db_a.keys := by rw [List.filter_eq_self] intro x hx rw [decide_eq_true_eq] have hx' : L ≤ x := by rw [ha_keys] at hx exact hLle x (List.mem_reverse.mp hx) exact ⟨le_trans haresolve (WithTop.coe_le_coe.mpr hx'), WithTop.coe_lt_top x⟩ rw [hpred, ha_keys, List.reverse_reverse] have hacompat : RangeRead.Compatible db_a r res := by refine (compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mpr ⟨hcomp₀.1, ?_⟩ rw [hck_a, List.filter_eq_self] intro x hx rw [decide_eq_true_eq] exact hLle x hx have hk_notin_a : k ∉ db_a.keys := by rw [ha_keys, List.mem_reverse] exact hk_notin -- insert the target key above the head let db_b : DB := db_a.insert k hk_notin_a have hb_keys : db_b.keys = res.keys.reverse ++ [k] := by rw [DB.insert_keys] exact orderedInsert_append_of_forall_lt' (fun x hx => by have hx' : x ∈ res.keys := by have hx'' : x ∈ res.keys.reverse := by simpa [db_a] using hx exact List.mem_reverse.mp hx'' exact lt_of_le_of_lt (hHge x hx') hkH) have hre_b : resolve db_b r.end_ = (⊤ : WithTop Key) := by have hcnt_b0 : cnt db_b r.end_.key = 0 := by apply cnt_eq_zero_of_forall_ge intro x hx rw [hb_keys, List.mem_append, List.mem_singleton] at hx rcases hx with hx | rfl · have hx' : x ∈ res.keys := List.mem_reverse.mp hx exact le_trans (le_of_lt hEL) (WithTop.coe_le_coe.mpr (hLle x hx')) · exact le_of_lt (lt_trans hEL (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hLle H (List.head_mem hne))) (WithTop.coe_lt_coe.mpr hkH))) have hanc : (r.end_.anchor db_b).toNat = (r.end_.offset - 1).toNat := by rw [anchor_toNat_eq (db := db_b) (s := r.end_) (by omega)] simp only [cnt] at hcnt_b0 rw [hcnt_b0, Nat.zero_add] refine resolve_eq_top (by change 0 ≤ (cnt db_b r.end_.key : Int) + r.end_.offset - 1 rw [hcnt_b0]; push_cast; omega) ?_ rw [hanc, hb_keys, List.length_append, List.length_reverse, List.length_singleton] omega have hrb_b : resolve db_b r.begin ≤ ((k : Key) : WithTop Key) := by have hlenpos : 0 < res.keys.reverse.length := by rw [List.length_reverse] exact List.length_pos_of_ne_nil hne have hkb : res.keys.reverse.length < db_b.keys.length := by rw [hb_keys, List.length_append]; simp have hkget : db_b.keys[res.keys.reverse.length] = k := by have hkget? : db_b.keys[res.keys.reverse.length]? = some k := by rw [hb_keys] simp have h1 := List.getElem?_eq_getElem hkb rw [hkget?] at h1 exact (Option.some.inj h1).symm have hclamp : clampIndex db_b r.begin ≤ res.keys.reverse.length := by unfold clampIndex have htoNat : (r.begin.anchor db_b).toNat ≤ res.keys.reverse.length := by by_cases h : 0 ≤ r.begin.anchor db_b · have hcnt_b : cnt db_b r.begin.key ≤ reportedBelowBegin r res + 1 := by unfold cnt reportedBelowBegin rw [hb_keys, List.countP_append] have h1 : res.keys.reverse.countP (fun x => decide (((x : Key) : WithTop Key) < r.begin.key)) = res.keys.countP (fun x => decide (((x : Key) : WithTop Key) < r.begin.key)) := List.countP_reverse rw [h1] have h2 : ([k].countP (fun x => decide (((x : Key) : WithTop Key) < r.begin.key))) ≤ 1 := by rw [List.countP_singleton] split <;> omega omega have hanchor_b : r.begin.anchor db_b ≤ ((res.keys.reverse.length : ℕ) : Int) := by change (cnt db_b r.begin.key : Int) + r.begin.offset - 1 ≤ _ have h1 : (cnt db_b r.begin.key : Int) ≤ (reportedBelowBegin r res : Int) + 1 := by exact_mod_cast hcnt_b have hlen1 : (1 : Int) ≤ ((res.keys.reverse.length : ℕ) : Int) := by have hp : 0 < res.keys.reverse.length := hlenpos omega linarith exact (Int.toNat_le).mpr hanchor_b · have hz : (r.begin.anchor db_b).toNat = 0 := by omega rw [hz]; omega exact le_trans (Nat.min_le_left _ _) htoNat have hle := (resolve_le_getElem_iff (db := db_b) (s := r.begin) hkb).mpr hclamp rwa [hkget] at hle have hkb_mem : k ∈ RangeRead.completeKeys db_b r := by rw [RangeRead.mem_completeKeys] exact ⟨DB.mem_insert_self db_a k hk_notin_a, hrb_b, by rw [hre_b]; exact WithTop.coe_lt_top k⟩ have hck_pair : (RangeRead.completeKeys db_b r).Pairwise (· > ·) := by have h := RangeRead.completeKeys_pairwise (db := db_b) (r := r) rw [hrev] at h exact h have hhead_max : ∀ x ∈ RangeRead.completeKeys db_b r, x ≤ k := by intro x hx have hxdb := (RangeRead.mem_completeKeys.mp hx).1 rw [hb_keys, List.mem_append, List.mem_singleton] at hxdb rcases hxdb with hx' | rfl · have hx'' : x ∈ res.keys := by have hxx : x ∈ res.keys.reverse := by simpa [db_a] using hx' exact List.mem_reverse.mp hxx exact le_of_lt (lt_of_le_of_lt (hHge x hx'') hkH) · exact le_rfl have hhead_eq : (RangeRead.completeKeys db_b r).head (List.ne_nil_of_mem hkb_mem) = k := by apply le_antisymm (hhead_max _ (List.head_mem _)) exact (hck_pair.imp le_of_lt).rel_head hkb_mem intro cr hs by_contra hkcr have hagree : AgreeOn cr db_a db_b := (ConflictRange.keys_insert_of_not_mem cr db_a hk_notin_a hkcr).symm have hcomp_b : RangeRead.Compatible db_b r res := (hs db_a db_b hagree).mp hacompat have hpre := RangeRead.compatible_keys_prefix hcomp_b have hhead := hpre.head hne have hHk : H = k := by rw [hHdef] exact hhead.trans hhead_eq have hHklt : H < k := by rw [hHdef]; exact hkH exact absurd hHk (ne_of_lt hHklt) /-! ### Gap-key necessity -/ /-- **Every gap key is necessary in the large-gap crossed top cell.** A witness stores `end_.offset - 1 - #reported` extra keys in `[end_.key, last)` including the target, the reversed reported keys, and one key above the reported head and at or above the begin key. The end selector resolves onto that top key. Erasing the target shortens the window by one, pushes the end selector past the end, and lets the top key enter the reported filter, so the truncated result changes. -/ theorem necessary_gap_of_residual_top_large_finite {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (_hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) (ht : res.keys.length < (r.end_.offset - 1).toNat) (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) (hBfin : r.begin.key ≠ ⊤) (hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + (res.keys.length : ℕ∞)) {k : Key} (hEk : r.end_.key ≤ (k : WithTop Key)) (hkL : (k : 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 t : Nat := res.keys.length with ht' set L : Key := res.keys.getLast hne with hL set H : Key := res.keys.head hne with hH set D : Nat := e - t with hD have ht_lt : t < e := by simpa [t, e] using ht have hD1 : 1 ≤ D := by omega have hDt : D + t = e := by omega have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne have hEL' : E < ((L : Key) : WithTop Key) := by simpa [E, L] using hEL have hLB : ((L : Key) : WithTop Key) ≤ B := le_of_lt (by simpa [B, L] using hcross) have hbound : (reportedBelowBegin r res : Int) + r.begin.offset - 1 ≤ 0 := by have h1 := reportedBelowBegin_le_between (db := db₀) hcomp₀ hmore hrev hne have h2 := between_add_offset_le_one (db := db₀) hcomp₀ hmore hrev hne hcross linarith have hcap : ((D : Nat) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by have hgap' : ((e : ℕ∞)) ≤ intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := by simpa [E, L, e, t] using hgap have hcast : ((D : ℕ∞) + (t : ℕ∞)) = (e : ℕ∞) := by rw [← Nat.cast_add, hDt] rw [← hcast] at hgap' exact (ENat.add_le_add_iff_right (ENat.natCast_ne_top t)).mp hgap' -- the `D` gap keys in `[E, last)`, one of them the target obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ := exists_db_keysIn_avoid (B := E) (E := ((L : Key) : WithTop Key)) (k := k) (m := D - 1) (by have hsub : D - 1 + 1 = D := Nat.sub_add_cancel hD1 have h : (((D - 1 + 1 : ℕ)) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by rw [hsub] exact hcap simpa only [Nat.cast_add, Nat.cast_one] using h) set G : List Key := (pad.insert k hkpad).keys with hGdef have hGmem : ∀ x ∈ G, E ≤ (x : WithTop Key) ∧ (x : 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 [hGdef, DB.insert_keys] at hx exact hx rw [List.mem_orderedInsert] at hx' rcases hx' with rfl | hx' · exact ⟨hEk, hkL⟩ · simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx' have hGsort : G.SortedLT := by rw [hGdef] exact (pad.insert k hkpad).sorted have hGlen : G.length = D := by rw [hGdef, DB.insert_keys, List.orderedInsert_length, hpadlen] omega have hkG : k ∈ G := by rw [hGdef, DB.insert_keys, List.mem_orderedInsert] exact Or.inl rfl -- a key above the head and at or above the begin key obtain ⟨b, hb⟩ := WithTop.ne_top_iff_exists.mp hBfin have hbB : ((b : Key) : WithTop Key) = B := hb.trans hB.symm set Q : Key := keyAfter (max H b) with hQ have hHQ : H < Q := by rw [hQ] exact lt_of_le_of_lt (le_max_left _ _) (keyAfter_self_lt _) have hbQ : b < Q := by rw [hQ] exact lt_of_le_of_lt (le_max_right _ _) (keyAfter_self_lt _) have hBQ : B < ((Q : Key) : WithTop Key) := by rw [← hbB] exact WithTop.coe_lt_coe.mpr hbQ set R : List Key := res.keys.reverse with hR have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev have hLleH : L ≤ H := by have h := (hsort.pairwise.imp le_of_lt).rel_getLast hHmem simpa [hL, hH] using h have hRsort : R.SortedLT := by rw [hR] exact hsort.reverse 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 (hsort.pairwise.imp le_of_lt).rel_getLast hyres have hRleH : ∀ y ∈ R, y ≤ H := by intro y hy have hyres : y ∈ res.keys := by rw [hR] at hy exact List.mem_reverse.mp hy simpa [hH] using (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hyres have hGR : ∀ x ∈ G, ∀ y ∈ R, x < y := by intro x hx y hy exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr (hRmem y hy))) set T : List Key := G ++ R with hT have hTsort : T.SortedLT := by rw [hT] exact List.sortedLT_append.mpr ⟨hGsort, hRsort, hGR⟩ have hTQ : ∀ x ∈ T, x < Q := by intro x hx rw [hT] at hx rcases List.mem_append.mp hx with hx | hx · exact lt_trans (WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr hLleH))) hHQ · exact lt_of_le_of_lt (hRleH x hx) hHQ have hQsort : ([Q] : List Key).SortedLT := sortedLT_singleton Q have hfullSort : (T ++ [Q]).SortedLT := List.sortedLT_append.mpr ⟨hTsort, hQsort, by intro x hx y hy rw [List.mem_singleton] at hy subst y exact hTQ x hx⟩ set dbfull : DB := ⟨T ++ [Q], hfullSort⟩ with hdbfull have hfullKeys : dbfull.keys = T ++ [Q] := rfl have hTlen : T.length = e := by rw [hT, List.length_append, hGlen, hR, List.length_reverse, ← ht'] omega have haboveE : above dbfull E = T ++ [Q] := by rw [above_eq_filter, hfullKeys, List.filter_eq_self] intro x hx rw [decide_eq_true_eq] rcases List.mem_append.mp hx with hx | hx · rw [hT] at hx rcases List.mem_append.mp hx with hx | hx · exact (hGmem x hx).1 · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) · rw [List.mem_singleton] at hx subst x exact le_trans (le_of_lt hEL') (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hHQ))) have hcnt0 : cnt dbfull E = 0 := by apply cnt_eq_zero_of_forall_ge intro x hx rw [hfullKeys, List.mem_append, List.mem_singleton] at hx rcases hx with hx | rfl · rw [hT] at hx rcases List.mem_append.mp hx with hx | hx · exact (hGmem x hx).1 · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) · exact le_trans (le_of_lt hEL') (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hHQ))) have hancEnd : (r.end_.anchor dbfull).toNat = e := by rw [anchor_toNat_eq (db := dbfull) (s := r.end_) (by omega)] have h0 : dbfull.keys.countP (fun (x : Key) => decide ((↑x : WithTop Key) < r.end_.key)) = 0 := by simpa [cnt, hE] using hcnt0 rw [h0, Nat.zero_add, he] have hq : dbfull.keys[e]? = some Q := by rw [hfullKeys, List.getElem?_append_right (l₁ := T) (l₂ := [Q]) (i := e) (le_of_eq hTlen)] rw [show e - T.length = 0 from by rw [hTlen]; omega] simp have hre : resolve dbfull r.end_ = ((Q : Key) : WithTop Key) := by have h0 : 0 ≤ r.end_.anchor dbfull := by change 0 ≤ (cnt dbfull r.end_.key : Int) + r.end_.offset - 1 rw [show cnt dbfull r.end_.key = 0 from by simpa only [hE] using hcnt0] push_cast omega rw [resolve, ite_eq_right (not_lt.mpr h0)] rw [show dbfull.keys[(r.end_.anchor dbfull).toNat]? = some Q from by rw [hancEnd]; exact hq] -- the begin resolution stays at or below the reported last have hcntB : cnt dbfull B = D + reportedBelowBegin r res := by unfold cnt reportedBelowBegin rw [hfullKeys, hT, List.countP_append, List.countP_append] have hG : G.countP (fun x : Key => decide ((x : WithTop Key) < B)) = D := by have h : G.countP (fun x : Key => decide ((x : WithTop Key) < B)) = G.length := (List.countP_eq_length (l := G) (p := fun x : Key => decide ((x : WithTop Key) < B))).mpr (by intro x hx exact decide_eq_true (lt_of_lt_of_le (hGmem x hx).2 hLB)) rw [h, hGlen] have hRc : R.countP (fun x : Key => decide ((x : WithTop Key) < B)) = res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) := by rw [hR, List.countP_reverse] have hQc : ([Q].countP (fun x : Key => decide ((x : WithTop Key) < B))) = 0 := by rw [List.countP_singleton] have hnot : ¬ ((Q : Key) : WithTop Key) < B := not_lt.mpr (le_of_lt hBQ) simp [hnot] rw [hG, hRc, hQc] omega have hanchorB : r.begin.anchor dbfull = (D : Int) + (reportedBelowBegin r res : Int) + r.begin.offset - 1 := by change (cnt dbfull r.begin.key : Int) + r.begin.offset - 1 = _ rw [show cnt dbfull r.begin.key = D + reportedBelowBegin r res from by simpa only [hB] using hcntB] push_cast ring have hanchorB_le : r.begin.anchor dbfull ≤ (D : Int) := by rw [hanchorB] linarith have hbetween : between dbfull ((L : Key) : WithTop Key) B = R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by unfold between rw [hfullKeys, hT, List.append_assoc, List.filter_append, List.filter_append] have hGnil : List.filter (fun x : Key => decide (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) G = [] := by rw [List.filter_eq_nil_iff] intro x hx hconj exact absurd (of_decide_eq_true hconj).1 (not_le.mpr (hGmem x hx).2) have hRself : List.filter (fun x : Key => decide (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) R = R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by apply List.filter_congr intro x hx apply Bool.decide_congr exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr (hRmem x hx), h⟩⟩ have hQnil : List.filter (fun x : Key => decide (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) [Q] = [] := by rw [List.filter_eq_nil_iff] intro x hx hconj rw [List.mem_singleton] at hx subst x exact absurd (of_decide_eq_true hconj).2 (not_lt.mpr (le_of_lt hBQ)) rw [hGnil, hRself, hQnil] simp have hbetweenLen : (between dbfull ((L : Key) : WithTop Key) B).length = reportedBelowBegin r res := by rw [hbetween, ← List.countP_eq_length_filter, hR, List.countP_reverse] rfl have hLmemR : L ∈ R := by rw [hR] exact List.mem_reverse.mpr hLmem have hLmemFull : L ∈ dbfull.keys := by rw [hfullKeys, hT] exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hLmemR))) have hrb : resolve dbfull r.begin ≤ ((L : Key) : WithTop Key) := by refine resolve_begin_le_of_between_bound (db := dbfull) (r := r) (last := L) hLmemFull ?_ ?_ · simpa [L] using hcross · have h := hbound rw [show between dbfull ((L : Key) : WithTop Key) r.begin.key = between dbfull ((L : Key) : WithTop Key) B from by rw [hB]] rw [hbetweenLen] exact h have hcompFull : RangeRead.Compatible dbfull r res := by refine (compatible_iff_truncated_keys_rev (db := dbfull) hrev hmore hne).mpr ⟨hcomp₀.1, ?_⟩ rw [completeKeys_filter_ge_eq_resolve_end (db := dbfull) (r := r) (last := L) hrev hrb] have hfiltL : dbfull.keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by rw [hfullKeys, hT, List.append_assoc, List.filter_append, List.filter_append] have hGnil : List.filter (fun x : Key => decide (L ≤ x)) G = [] := by rw [List.filter_eq_nil_iff] intro x hx hlx exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx)) (not_le.mpr (hGmem x hx).2) have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by rw [List.filter_eq_self] intro x hx exact decide_eq_true (hRmem x hx) have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by rw [List.filter_eq_self] intro x hx rw [List.mem_singleton] at hx subst x exact decide_eq_true (le_trans hLleH (le_of_lt hHQ)) rw [hGnil, hRself, hQself] simp rw [hfiltL, List.filter_append, hre] have hRself : List.filter (fun y : Key => decide ((y : WithTop Key) < ((Q : Key) : WithTop Key))) R = R := by rw [List.filter_eq_self] intro x hx exact decide_eq_true (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (hRleH x hx) hHQ)) have hQnil : List.filter (fun y : Key => decide ((y : WithTop Key) < ((Q : Key) : WithTop Key))) [Q] = [] := by rw [List.filter_eq_nil_iff] intro x hx hlt rw [List.mem_singleton] at hx subst x exact absurd (of_decide_eq_true hlt) (not_lt.mpr le_rfl) rw [hRself, hQnil, List.append_nil, hR, List.reverse_reverse] -- the erased database resolves the end past the end and reports the top key too have hkeysErase : (dbfull.erase k).keys = G.erase k ++ (R ++ [Q]) := by rw [DB.erase_keys, hfullKeys, hT, List.append_assoc, List.erase_append_left (l₁ := G) (l₂ := R ++ [Q]) hkG] have hcnt0Erase : cnt (dbfull.erase k) E = 0 := by apply cnt_eq_zero_of_forall_ge intro x hx rw [hkeysErase] at hx rcases List.mem_append.mp hx with hx | hx · exact (hGmem x (List.mem_of_mem_erase hx)).1 · rcases List.mem_append.mp hx with hx | hx · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) · rw [List.mem_singleton] at hx subst x exact le_trans (le_of_lt hEL') (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hHQ))) have hlenErase : (dbfull.erase k).keys.length = e := by rw [hkeysErase, List.length_append, List.length_append, List.length_erase_of_mem hkG, hGlen, hR, List.length_reverse, ← ht', List.length_singleton] omega have hancEndErase : (r.end_.anchor (dbfull.erase k)).toNat = e := by rw [anchor_toNat_eq (db := dbfull.erase k) (s := r.end_) (by omega)] have h0 : (dbfull.erase k).keys.countP (fun (x : Key) => decide ((↑x : WithTop Key) < r.end_.key)) = 0 := by simpa [cnt, hE] using hcnt0Erase rw [h0, Nat.zero_add, he] have hreErase : resolve (dbfull.erase k) r.end_ = (⊤ : WithTop Key) := by refine resolve_eq_top (by change 0 ≤ (cnt (dbfull.erase k) r.end_.key : Int) + r.end_.offset - 1 rw [show cnt (dbfull.erase k) r.end_.key = 0 from by simpa only [hE] using hcnt0Erase] push_cast omega) ?_ rw [hancEndErase, hlenErase] have hbetweenErase : between (dbfull.erase k) ((L : Key) : WithTop Key) B = R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by unfold between rw [hkeysErase, List.filter_append, List.filter_append] have hGnil : List.filter (fun x : Key => decide (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) (G.erase k) = [] := by rw [List.filter_eq_nil_iff] intro x hx hconj exact absurd (of_decide_eq_true hconj).1 (not_le.mpr (hGmem x (List.mem_of_mem_erase hx)).2) have hRself : List.filter (fun x : Key => decide (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) R = R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by apply List.filter_congr intro x hx apply Bool.decide_congr exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr (hRmem x hx), h⟩⟩ have hQnil : List.filter (fun x : Key => decide (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) [Q] = [] := by rw [List.filter_eq_nil_iff] intro x hx hconj rw [List.mem_singleton] at hx subst x exact absurd (of_decide_eq_true hconj).2 (not_lt.mpr (le_of_lt hBQ)) rw [hGnil, hRself, hQnil] simp have hbetweenLenErase : (between (dbfull.erase k) ((L : Key) : WithTop Key) B).length = reportedBelowBegin r res := by rw [hbetweenErase, ← List.countP_eq_length_filter, hR, List.countP_reverse] rfl have hLmemErase : L ∈ (dbfull.erase k).keys := by rw [hkeysErase] exact List.mem_append.mpr (Or.inr (List.mem_append.mpr (Or.inl hLmemR))) have hrbErase : resolve (dbfull.erase k) r.begin ≤ ((L : Key) : WithTop Key) := by refine resolve_begin_le_of_between_bound (db := dbfull.erase k) (r := r) (last := L) hLmemErase ?_ ?_ · simpa [L] using hcross · have h := hbound rw [show between (dbfull.erase k) ((L : Key) : WithTop Key) r.begin.key = between (dbfull.erase k) ((L : Key) : WithTop Key) B from by rw [hB]] rw [hbetweenLenErase] exact h have hfilterErase : (dbfull.erase k).keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by rw [hkeysErase, List.filter_append, List.filter_append] have hGnil : List.filter (fun x : Key => decide (L ≤ x)) (G.erase k) = [] := by rw [List.filter_eq_nil_iff] intro x hx hlx exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx)) (not_le.mpr (hGmem x (List.mem_of_mem_erase hx)).2) have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by rw [List.filter_eq_self] intro x hx exact decide_eq_true (hRmem x hx) have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by rw [List.filter_eq_self] intro x hx rw [List.mem_singleton] at hx subst x exact decide_eq_true (le_trans hLleH (le_of_lt hHQ)) rw [hGnil, hRself, hQself] simp have hckErase : (RangeRead.completeKeys (dbfull.erase k) r).filter (fun x : Key => decide (L ≤ x)) = Q :: res.keys := by rw [completeKeys_filter_ge_eq_resolve_end (db := dbfull.erase k) (r := r) (last := L) hrev hrbErase] rw [hfilterErase, hreErase] have hid : (R ++ [Q]).filter (fun x : Key => decide ((x : WithTop Key) < (⊤ : WithTop Key))) = R ++ [Q] := by rw [List.filter_eq_self] intro x hx rw [decide_eq_true_eq] exact WithTop.coe_lt_top x rw [hid, List.reverse_append, hR, List.reverse_reverse, List.reverse_singleton] rfl refine necessary_of_toggle ⟨dbfull, hcompFull, ?_⟩ have hTmem : k ∈ T := by rw [hT] exact List.mem_append.mpr (Or.inl hkG) have hkFull : k ∈ dbfull.keys := by rw [hfullKeys] exact List.mem_append.mpr (Or.inl hTmem) rw [toggle_eq_erase_of_mem hkFull] intro hcomp' have hfil := (compatible_iff_truncated_keys_rev (db := dbfull.erase k) hrev hmore hne).mp hcomp' |>.2 rw [hckErase] at hfil have hlen := congrArg List.length hfil simp at hlen /-! ### The routed finite crossed subcell -/ /-- The routed subcell: the large-gap crossed top cell together with a finite crossed begin selector (`last < begin.key ≠ ⊤`). The cross-free part is already covered by the nonpositive reversed truncated rule, and the sentinel begin key is a separate obstruction. -/ abbrev negCrossTruncRevResidualTopLargeCrossed (r : RangeRead) (res : Result) : Prop := negCrossTruncRevResidualTopLarge r res ∧ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ r.begin.key ≠ ⊤ /-- The closed form of the routed large-gap crossed top subcell: `[end_.key, ⊤)`. -/ noncomputable def closedFormNegCrossTruncRevResidualTopLargeCrossed (r : RangeRead) (_res : Result) : ConflictRange := ⟨r.end_.key, ⊤⟩ /-- **The large-gap crossed top subcell closed form is minimal.** Soundness is `sound_topRange_endKey_of_residual_large`. Necessity splits the range `[end_.key, ⊤)` at the reported last and head: gap keys use the witness of `necessary_gap_of_residual_top_large_finite`, reported keys use convexity, and keys above the head use `necessary_above_head_of_residual_top_large`. -/ theorem minimal_closedForm_of_negCross_trunc_rev_residual_top_large_crossed {r : RangeRead} {res : Result} (hread : Readable r res) (h : negCrossTruncRevResidualTopLargeCrossed r res) : Minimal r res (closedFormNegCrossTruncRevResidualTopLargeCrossed r res) := by obtain ⟨hlarge, hcrossD, hBfin⟩ := h obtain ⟨hmore, hrev, hbeg, hne, hend, hELD, htopD, hgapD⟩ := hlarge obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ set L : Key := res.keys.getLast hne with hLdef set H : Key := res.keys.head hne with hHdef have hlastD : res.keys.getLastD ([] : Key) = L := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] exact hLdef.symm have hEL : r.end_.key < ((L : Key) : WithTop Key) := by rw [hlastD] at hELD exact hELD have hcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hlastD] at hcrossD exact hcrossD have hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by rw [hlastD] at hgapD exact hgapD have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev have hLle : ∀ x ∈ res.keys, L ≤ x := by intro x hx rw [hLdef] exact (hsort.pairwise.imp le_of_lt).rel_getLast hx have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne have hcountP : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by rw [List.countP_eq_length] intro x hx exact decide_eq_true (le_of_lt (lt_of_lt_of_le hEL (WithTop.coe_le_coe.mpr (hLle x hx)))) have ht : res.keys.length < (r.end_.offset - 1).toNat := by rw [hcountP] at htopD exact htopD refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ · simpa [closedFormNegCrossTruncRevResidualTopLargeCrossed] using sound_topRange_endKey_of_residual_large (r := r) (res := res) hmore hrev hbeg hne hend (by simpa [hLdef] using hEL) · intro k hk rw [ConflictRange.mem_iff] at hk obtain ⟨hEk, _⟩ := hk by_cases hkL : (k : WithTop Key) < ((L : Key) : WithTop Key) · exact necessary_gap_of_residual_top_large_finite hread' hmore hrev hbeg hne hend (by simpa [hLdef] using hEL) ht (by simpa [hLdef] using hcross) hBfin (by simpa [hLdef] using hgap) hEk hkL · have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hkL have hkKey : L ≤ k := WithTop.coe_le_coe.mp hLk by_cases hkH : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) · exact necessary_of_between_result hread' hLmem hHmem hkKey (WithTop.coe_le_coe.mp hkH) · have hHk : H < k := WithTop.coe_lt_coe.mp (lt_of_not_ge hkH) exact necessary_above_head_of_residual_top_large hread' hmore hrev hbeg hne hend (by simpa [hLdef] using hEL) ht (by simpa [hLdef] using hcross) (by simpa [hHdef] using hHk) end FormalConflictRange