/- 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.Sound import FormalConflictRange.Empty import FormalConflictRange.CrossedGeneral /-! # The crossed complete begin-zero cell with `end.key < begin.key` At `begin.offset = 0` a crossed complete result (`begin.key` strictly above the reported head) whose `end.key` lies below `begin.key` is the last complete-result cell at a nonpositive begin offset. Writing * `H` for the reported head and `L` for the reported last, * `E = end_.key`, `B = begin.key`, `e = (end_.offset - 1).toNat`, * `t = residualT r res` for the number of reported keys at or above `E`, and * `g = intervalCard E H`, the minimal range is * `[min E H, ⊤)` when `t < e` and `e ≤ t + g`, * `[H, ⊤)` when `t < e` and `e > t + g`, * `[min E H, max (keyAfter L) B)` when `t = e`. The structural core is `completeKeys_eq_filter_zero_crossed`: in every compatible database the complete window is exactly the stored keys in `[H, keyAfter L)`. The gap `[H, B)` cannot contain a stored key, and the end resolution is the first stored key at or above `keyAfter L` (or `⊤`). The lower endpoint is governed by whether the `e - t` keys needed in `[E, H)` can be placed there without filling the gap; the upper endpoint is `⊤` exactly when the un-filled gap lets one insert a key above `L` without changing the end resolution. -/ namespace FormalConflictRange /-- The empty key is at or below every `WithTop Key` bound. -/ theorem coe_nil_le_zero_cross_long (t : WithTop Key) : ((([] : Key) : WithTop Key)) ≤ t := by cases t with | top => exact le_top | coe k => exact WithTop.coe_le_coe.mpr (nil_le_key k) /-- `getLastD` agrees with `getLast` on a nonempty list. -/ theorem getLastD_eq_getLast_zero_cross_long {l : List Key} (h : l ≠ []) : l.getLastD ([] : Key) = l.getLast h := by cases l with | nil => exact absurd rfl h | cons a t => cases t with | nil => rfl | cons b t => simp [List.getLastD, List.getLast] /-- At begin offset zero, the crossed complete begin resolution is the reported head. -/ theorem resolve_begin_eq_head_of_zero_crossed {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (_hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db r := by rw [← hres]; exact hhead_mem have hhead_db : res.keys.head hne ∈ db.keys := (RangeRead.mem_completeKeys.mp hhead_ck).1 exact le_antisymm (RangeRead.mem_completeKeys.mp hhead_ck).2.1 (resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross (res.keys.head hne) hhead_db hcross) /-- In a crossed complete begin-zero read, no stored key lies strictly between the reported head and `begin.key`. -/ theorem no_stored_between_head_begin_of_zero_crossed {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne have hhead_db : res.keys.head hne ∈ db.keys := RangeRead.compatible_keys_subset hcomp hhead_mem have hL := resolve_begin_eq_head_of_zero_crossed hcomp hrev hmore hne hbegin hcross intro k hk hkb have hle := resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross k hk hkb.2 rw [hL] at hle exact absurd hle (not_le.mpr hkb.1) /-- If a database stores `H` below `begin.key`, has no key in `(H, begin.key)`, and the begin offset is zero, then its begin selector resolves to `H`. -/ theorem resolve_begin_eq_of_no_between_zero {db : DB} {r : RangeRead} {H : Key} (h0 : r.begin.offset = 0) (hH : H ∈ db.keys) (hlt : ((H : Key) : WithTop Key) < r.begin.key) (hno : ∀ k ∈ db.keys, ¬ ((((H : Key) : WithTop Key)) < (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key)) : resolve db r.begin = ((H : Key) : WithTop Key) := by apply le_antisymm · rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with hn | ht | ⟨k, hk, hke⟩ · rw [hn]; exact coe_nil_le_zero_cross_long _ · exact absurd ht (resolve_ne_top_of_offset_nonpos (by rw [h0])) · rw [hke] by_contra hcon have hgt : ((H : Key) : WithTop Key) < (k : WithTop Key) := lt_of_not_ge hcon have hres := resolve_zero_begin_lt_key (db := db) h0 hH hlt have hkB : (k : WithTop Key) < r.begin.key := by rw [hke] at hres; exact hres exact absurd ⟨hgt, hkB⟩ (hno k hk) · exact resolve_zero_begin_le_of_mem_below h0 hH hlt H hH hlt /-- **The crossed complete begin-zero window.** In a compatible database the complete keys are exactly the stored keys in `[head, keyAfter (last reported))`. In particular the end resolution is the first stored key at or above `keyAfter (last reported)`, or `⊤`. -/ theorem completeKeys_eq_filter_zero_crossed {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : RangeRead.completeKeys db r = db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := by have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev have hmax : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk have hHL : (res.keys.head hne : Key) ≤ res.keys.getLast hne := hmax _ hhead_mem have hLck : res.keys.getLast hne ∈ RangeRead.completeKeys db r := by rw [← hres]; exact hlast_mem have hLlt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := (RangeRead.mem_completeKeys.mp hLck).2.2 have hLresolve := resolve_begin_eq_head_of_zero_crossed hcomp hrev hmore hne hbegin hcross have hno : ∀ k ∈ db.keys, ¬ (((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_) := by intro k hk hkb have hHle : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := le_trans (WithTop.coe_le_coe.mpr hHL) (le_of_lt hkb.1) have hkck : k ∈ RangeRead.completeKeys db r := RangeRead.mem_completeKeys.mpr ⟨hk, by rw [hLresolve]; exact hHle, hkb.2⟩ have hkres : k ∈ res.keys := by rw [hres]; exact hkck exact absurd (hmax k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkb.1)) have hkeyAfter_le : ((keyAfter (res.keys.getLast hne) : Key) : WithTop Key) ≤ resolve db r.end_ := by rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hn | ht | ⟨q, hq, hqe⟩ · rw [hn] at hLlt exact absurd hLlt (not_lt.mpr (coe_nil_le_zero_cross_long _)) · rw [ht]; exact le_top · rw [hqe] exact keyAfter_le_of_lt (by rw [hqe] at hLlt; exact hLlt) have hck : RangeRead.completeKeys db r = db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_)) := by unfold RangeRead.completeKeys RangeRead.inRange rw [ite_eq_right (by simp [hrev]), hLresolve] rw [hck] apply List.filter_congr intro k hk have hiff : ((((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_) ↔ (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := by constructor · rintro ⟨h1, h2⟩ refine ⟨h1, ?_⟩ by_contra hcon have hge : (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hcon have hLk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) := lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt _)) hge exact absurd ⟨hLk, h2⟩ (hno k hk) · rintro ⟨h1, h2⟩ exact ⟨h1, lt_of_lt_of_le h2 hkeyAfter_le⟩ rw [decide_eq_decide.mpr (hiff)] /-! ### Compatibility with the offset-one anchor At begin offset zero, a crossed complete read and its offset-one anchor at the reported head have the same compatible databases on the long-cell region: the read forbids stored keys in `(head, begin.key)`, and the anchor is the same read with the begin selector moved to the reported head. -/ /-- The begin selector of the offset-one anchor is the offset-one selector at the reported head. -/ theorem zeroBeginAnchor_begin_zero_cross_long (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : (zeroBeginAnchor r res).begin = ({ key := ((res.keys.head hne : Key) : WithTop Key), offset := 1 } : Selector) := by unfold zeroBeginAnchor simp only rw [headD_eq_head_sound hne] /-- Compatibility passes from a crossed complete begin-zero read to its offset-one anchor at the reported head. -/ theorem compatible_anchor_of_zero_crossed {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : RangeRead.Compatible db (zeroBeginAnchor r res) res := by have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne have hhead_stored : res.keys.head hne ∈ db.keys := (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).1 have hanchor_begin := zeroBeginAnchor_begin_zero_cross_long r res hne have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by apply le_antisymm · exact (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).2.1 · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross (res.keys.head hne) hhead_stored hcross have hL' : resolve db (zeroBeginAnchor r res).begin = ((res.keys.head hne : Key) : WithTop Key) := by rw [hanchor_begin] exact resolve_offset_one_key_of_mem hhead_stored refine ⟨?_, ?_⟩ · simpa [RangeRead.withinLimit, zeroBeginAnchor] using hcomp.1 · right refine ⟨hmore, ?_⟩ have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db (zeroBeginAnchor r res) := by unfold RangeRead.completeKeys RangeRead.inRange zeroBeginAnchor rw [hrev] simp only [Bool.false_eq_true, ite_false] rw [hL] congr 1 funext k change decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_) = decide (resolve db (zeroBeginAnchor r res).begin ≤ (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_) rw [hL'] rw [← hck] exact hres /-- Compatibility passes from the offset-one anchor back to a crossed complete begin-zero read, provided the database stores nothing in `(head, begin.key)`. -/ theorem compatible_of_anchor_of_no_between {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db (zeroBeginAnchor r res) res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hno : ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key)) : RangeRead.Compatible db r res := by have hres : res.keys = RangeRead.completeKeys db (zeroBeginAnchor r res) := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne have hhead_stored : res.keys.head hne ∈ db.keys := (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).1 have hanchor_begin := zeroBeginAnchor_begin_zero_cross_long r res hne have hL' : resolve db (zeroBeginAnchor r res).begin = ((res.keys.head hne : Key) : WithTop Key) := by rw [hanchor_begin] exact resolve_offset_one_key_of_mem hhead_stored have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := resolve_begin_eq_of_no_between_zero hbegin hhead_stored hcross hno refine ⟨?_, ?_⟩ · simpa [RangeRead.withinLimit, zeroBeginAnchor] using hcomp.1 · right refine ⟨hmore, ?_⟩ have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db (zeroBeginAnchor r res) := by unfold RangeRead.completeKeys RangeRead.inRange zeroBeginAnchor rw [hrev] simp only [Bool.false_eq_true, ite_false] rw [hL] congr 1 funext k change decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_) = decide (resolve db (zeroBeginAnchor r res).begin ≤ (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_) rw [hL'] rw [hck] exact hres /-- Agreement on a range passes to any subrange. -/ theorem agreeOn_of_subrange {cr cr' : ConflictRange} (h : Subrange cr' cr) {db₁ db₂ : DB} (hagree : AgreeOn cr db₁ db₂) : AgreeOn cr' db₁ db₂ := by rw [agreeOn_iff] at hagree ⊢ intro k hk exact hagree k (h k hk) /-- **Soundness transfer from the offset-one anchor.** If the anchor's range is sound for the anchor read and is contained in `cr`, and `cr` covers the reported head and the whole gap up to `begin.key`, then `cr` is sound for the original read. -/ theorem sound_of_anchor_sound {r : RangeRead} {res : Result} {cr crA : ConflictRange} (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hlo : cr.begin ≤ ((res.keys.head hne : Key) : WithTop Key)) (hhi : r.begin.key ≤ cr.end_) (hsub : Subrange crA cr) (hsoundA : Sound (zeroBeginAnchor r res) res crA) : Sound r res cr := by intro db₁ db₂ hagree have hAgreeA : AgreeOn crA db₁ db₂ := agreeOn_of_subrange hsub hagree have hmem_cr : ∀ k : Key, ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) → (k : WithTop Key) < r.begin.key → k ∈ cr := by intro k h1 h2 exact ConflictRange.mem_iff.mpr ⟨le_trans hlo (le_of_lt h1), lt_of_lt_of_le h2 hhi⟩ have htransfer : ∀ {db db' : DB}, RangeRead.Compatible db r res → AgreeOn cr db db' → RangeRead.Compatible db' (zeroBeginAnchor r res) res → RangeRead.Compatible db' r res := by intro db db' hc hag hcA' have hno : ∀ k ∈ db'.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by intro k hk hkb have hno_db := no_stored_between_head_begin_of_zero_crossed hc hrev hmore hne hbegin hcross have hkbd : k ∈ db.keys := ((agreeOn_iff cr db db').mp hag) k (hmem_cr k hkb.1 hkb.2) |>.mpr hk exact hno_db k hkbd hkb exact compatible_of_anchor_of_no_between hcA' hrev hmore hne hbegin hcross hno constructor · intro hc₁ have hcA₁ := compatible_anchor_of_zero_crossed hc₁ hrev hmore hne hbegin hcross have hcA₂ := (hsoundA db₁ db₂ hAgreeA).mp hcA₁ exact htransfer hc₁ hagree hcA₂ · intro hc₂ have hcA₂ := compatible_anchor_of_zero_crossed hc₂ hrev hmore hne hbegin hcross have hcA₁ := (hsoundA db₁ db₂ hAgreeA).mpr hcA₂ have hagree' : AgreeOn cr db₂ db₁ := AgreeOn.symm hagree exact htransfer hc₂ hagree' hcA₁ /-! ### The long-cell closed form and its anchor The explicit rule is the anchor's minimal range with the upper endpoint raised to at least `begin.key`. Writing `H` for the head, `L` for the last, `E = end_.key`, `e = (end_.offset - 1).toNat`, `t` for the reported keys at or above `E`, and `g = intervalCard E H`, it is * `[min E H, ⊤)` when `t < e` and `e ≤ t + g`, * `[H, ⊤)` when `t < e` and `e > t + g`, * `[min E H, max (keyAfter L) begin.key)` when `t = e`. -/ /-- The anchor-based form of the long-cell value: the offset-one crossed or cross-free complete rule at the reported head. -/ noncomputable def zeroCrossLongAnchor (r : RangeRead) (res : Result) : ConflictRange := if r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) then completeForwardCrossedGeneral (zeroBeginAnchor r res) res else closedFormOffsetOne (zeroBeginAnchor r res) res /-- The crossed complete begin-zero long-cell closed form. It is the anchor's minimal range with the upper endpoint raised to at least `begin.key`, because keys in `(head, begin.key)` are never stored but inserting one shifts the begin resolution off the reported head. Writing `H` for the head, `L` for the last, `E = end_.key`, `e = (end_.offset - 1).toNat`, `t` for the reported keys at or above `E`, and `g = intervalCard E H`, the value is * `[min E H, ⊤)` when `t < e` and `e ≤ t + g`, * `[H, ⊤)` when `t < e` and `e > t + g`, * `[min E H, max (keyAfter L) begin.key)` when `t = e`. -/ noncomputable def closedFormZeroCrossLong (r : RangeRead) (res : Result) : ConflictRange := if r.begin.key = (⊤ : WithTop Key) then let H : WithTop Key := ((res.keys.headD ([] : Key) : Key) : WithTop Key) let e := (r.end_.offset - 1).toNat let t := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) let g := intervalCard r.end_.key H ⟨if r.end_.key < H ∧ e - t < g then r.end_.key else H, ⊤⟩ else ⟨(zeroCrossLongAnchor r res).begin, max (zeroCrossLongAnchor r res).end_ r.begin.key⟩ /-- The anchor-based long-cell value is minimal on the long-cell region. -/ theorem minimal_zeroCrossLongAnchor {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (_hend : 2 ≤ r.end_.offset) : Minimal (zeroBeginAnchor r res) res (zeroCrossLongAnchor r res) := by obtain ⟨db₀, hcomp₀⟩ := hread have hH : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_sound hne have hreadA : Readable (zeroBeginAnchor r res) res := ⟨db₀, compatible_anchor_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross⟩ have hbeginA : (zeroBeginAnchor r res).begin.offset = 1 := by simp [zeroBeginAnchor] have hrevA : (zeroBeginAnchor r res).reverse = false := by simp [zeroBeginAnchor, hrev] unfold zeroCrossLongAnchor rw [hH] by_cases hE : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) · rw [ite_eq_left hE] have hEA : (zeroBeginAnchor r res).end_.key < (zeroBeginAnchor r res).begin.key := by change r.end_.key < (zeroBeginAnchor r res).begin.key rw [zeroBeginAnchor_begin_zero_cross_long r res hne] exact hE exact minimal_completeForwardCrossedGeneral hreadA hmore hne hrevA (by omega) hEA · rw [ite_eq_right hE] have hle : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key := not_lt.mp hE have hleA : (zeroBeginAnchor r res).begin.key ≤ (zeroBeginAnchor r res).end_.key := by change (zeroBeginAnchor r res).begin.key ≤ r.end_.key rw [zeroBeginAnchor_begin_zero_cross_long r res hne] exact hle exact minimal_closedForm_of_offset_one hreadA hmore hne hrevA hbeginA hleA /-! ### Endpoint structure of the anchor The anchor is either the offset-one crossed complete rule (when `end_.key` lies below the reported head) or the offset-one cross-free rule. These lemmas record its two endpoints in terms of the reported head `H`, the reported last `L`, the number `t` of reported keys at or above `end_.key`, and the end overshoot `e`. -/ theorem zeroBeginAnchor_end_key_eq (r : RangeRead) (res : Result) : (zeroBeginAnchor r res).end_.key = r.end_.key := rfl theorem zeroBeginAnchor_end_offset_eq (r : RangeRead) (res : Result) : (zeroBeginAnchor r res).end_.offset = r.end_.offset := rfl theorem zeroBeginAnchor_reported_eq (r : RangeRead) (res : Result) : reportedAtEnd (zeroBeginAnchor r res) res = reportedAtEnd r res := rfl theorem zeroBeginAnchor_overshoot_eq (r : RangeRead) (res : Result) : endOvershoot (zeroBeginAnchor r res) = endOvershoot r := rfl theorem zeroBeginAnchor_interval_eq (r : RangeRead) (res : Result) (H : WithTop Key) : intervalCard (zeroBeginAnchor r res).end_.key H = intervalCard r.end_.key H := rfl theorem zeroBeginAnchor_begin_key_eq (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : (zeroBeginAnchor r res).begin.key = ((res.keys.head hne : Key) : WithTop Key) := congrArg Selector.key (zeroBeginAnchor_begin_zero_cross_long r res hne) theorem zeroBeginAnchor_begin_offset_eq (r : RangeRead) (res : Result) : (zeroBeginAnchor r res).begin.offset = 1 := by simp [zeroBeginAnchor] /-- The begin endpoint of the long-cell anchor, with `E < H`. -/ theorem zeroCrossLongAnchor_begin_crossed {r : RangeRead} {res : Result} (hne : res.keys ≠ []) (_hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : (zeroCrossLongAnchor r res).begin = if reportedAtEnd r res = endOvershoot r ∨ (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) then r.end_.key else ((res.keys.head hne : Key) : WithTop Key) := by unfold zeroCrossLongAnchor rw [headD_eq_head_sound hne, ite_eq_left hEH] show (completeForwardCrossedGeneral (zeroBeginAnchor r res) res).begin = _ unfold completeForwardCrossedGeneral rw [ite_eq_right (by rw [zeroBeginAnchor_end_offset_eq]; omega)] rw [zeroBeginAnchor_reported_eq, zeroBeginAnchor_overshoot_eq, zeroBeginAnchor_begin_offset_eq, show (((1 : ℤ) - 1).toNat) = 0 by norm_num, Nat.add_zero, zeroBeginAnchor_begin_key_eq r res hne] rw [zeroBeginAnchor_interval_eq] by_cases ht : reportedAtEnd r res = endOvershoot r · rw [ite_eq_left ht, ite_eq_left (Or.inl ht), zeroBeginAnchor_end_key_eq] · rw [ite_eq_right ht] have hso : crossedOvershootGeneral (zeroBeginAnchor r res) res = endOvershoot r - reportedAtEnd r res := by unfold crossedOvershootGeneral rw [zeroBeginAnchor_overshoot_eq, zeroBeginAnchor_reported_eq, zeroBeginAnchor_begin_offset_eq, show (((1 : ℤ) - 1).toNat) = 0 by norm_num, Nat.sub_zero] rw [hso] by_cases hc : (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) · rw [ite_eq_left hc, ite_eq_left (Or.inr hc), zeroBeginAnchor_end_key_eq] · rw [ite_eq_right hc, ite_eq_right (fun h => h.elim ht hc)] /-- The end endpoint of the long-cell anchor, with `E < H`. -/ theorem zeroCrossLongAnchor_end_crossed {r : RangeRead} {res : Result} (hne : res.keys ≠ []) (_hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : (zeroCrossLongAnchor r res).end_ = if reportedAtEnd r res = endOvershoot r then (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) else ⊤ := by unfold zeroCrossLongAnchor rw [headD_eq_head_sound hne, ite_eq_left hEH] show (completeForwardCrossedGeneral (zeroBeginAnchor r res) res).end_ = _ unfold completeForwardCrossedGeneral rw [ite_eq_right (by rw [zeroBeginAnchor_end_offset_eq]; omega)] rw [zeroBeginAnchor_reported_eq, zeroBeginAnchor_overshoot_eq, zeroBeginAnchor_begin_offset_eq, show (((1 : ℤ) - 1).toNat) = 0 by norm_num, Nat.add_zero] by_cases ht : reportedAtEnd r res = endOvershoot r · rw [ite_eq_left ht, ite_eq_left ht] · rw [ite_eq_right ht, ite_eq_right ht] /-- The begin endpoint of the long-cell anchor, with `H ≤ E`. -/ theorem zeroCrossLongAnchor_begin_crossfree {r : RangeRead} {res : Result} (hne : res.keys ≠ []) (_hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) (hEH : ¬ (r.end_.key < ((res.keys.head hne : Key) : WithTop Key))) : (zeroCrossLongAnchor r res).begin = (((res.keys.head hne : Key)) : WithTop Key) := by unfold zeroCrossLongAnchor rw [headD_eq_head_sound hne, ite_eq_right hEH] show (closedFormOffsetOne (zeroBeginAnchor r res) res).begin = _ unfold closedFormOffsetOne rw [ite_eq_right (by rw [zeroBeginAnchor_end_offset_eq]; omega)] by_cases ht : res.keys.countP (fun k : Key => decide ((zeroBeginAnchor r res).end_.key ≤ (k : WithTop Key))) = ((zeroBeginAnchor r res).end_.offset - 1).toNat · rw [ite_eq_left ht] simp [truncatedRange, zeroBeginAnchor_begin_key_eq r res hne] · rw [ite_eq_right ht] simp [topRange, zeroBeginAnchor_begin_key_eq r res hne] /-- The end endpoint of the long-cell anchor, with `H ≤ E`. -/ theorem zeroCrossLongAnchor_end_crossfree {r : RangeRead} {res : Result} (hne : res.keys ≠ []) (_hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) (hEH : ¬ (r.end_.key < ((res.keys.head hne : Key) : WithTop Key))) : (zeroCrossLongAnchor r res).end_ = if reportedAtEnd r res = endOvershoot r then (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) else ⊤ := by unfold zeroCrossLongAnchor rw [headD_eq_head_sound hne, ite_eq_right hEH] show (closedFormOffsetOne (zeroBeginAnchor r res) res).end_ = _ unfold closedFormOffsetOne rw [ite_eq_right (by rw [zeroBeginAnchor_end_offset_eq]; omega)] have hcnt : res.keys.countP (fun k : Key => decide ((zeroBeginAnchor r res).end_.key ≤ (k : WithTop Key))) = res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by rw [zeroBeginAnchor_end_key_eq] have hoff : ((zeroBeginAnchor r res).end_.offset - 1).toNat = endOvershoot r := by rw [zeroBeginAnchor_end_offset_eq]; rfl by_cases ht : res.keys.countP (fun k : Key => decide ((zeroBeginAnchor r res).end_.key ≤ (k : WithTop Key))) = ((zeroBeginAnchor r res).end_.offset - 1).toNat · rw [ite_eq_left ht, ite_eq_left (by unfold reportedAtEnd; rw [← hcnt, ← hoff]; exact ht)] simp [truncatedRange] · rw [ite_eq_right ht, ite_eq_right (by unfold reportedAtEnd; rw [← hcnt, ← hoff]; exact ht)] simp [topRange] /-- At `begin.offset = 0` and `begin.key = ⊤`, the begin selector resolves to the greatest stored key. -/ theorem resolve_begin_eq_getLast_of_top {db : DB} {r : RangeRead} (h0 : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hpos : 0 < db.keys.length) : resolve db r.begin = ((db.keys.getLast (List.ne_nil_of_length_pos hpos) : Key) : WithTop Key) := by set hne : db.keys ≠ [] := List.ne_nil_of_length_pos hpos with hne_def set M : Key := db.keys.getLast hne with hM have hanc : Selector.anchor db r.begin = (db.keys.length : Int) - 1 := by rw [anchor_of_top htop]; omega have h0le : 0 ≤ Selector.anchor db r.begin := by rw [hanc]; omega have hidx : (Selector.anchor db r.begin).toNat = db.keys.length - 1 := by rw [hanc] have hcast : (db.keys.length : Int) - 1 = ((db.keys.length - 1 : ℕ) : Int) := by omega rw [hcast, Int.toNat_natCast] have hget? : db.keys[(Selector.anchor db r.begin).toNat]? = some M := by rw [hidx, ← List.getLast?_eq_getElem?] rw [hM] exact (List.getLast_eq_iff_getLast?_eq_some hne).mp rfl rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0le), hget?] /-- `resolve` is `⊤` exactly when the anchor index is at or past the end. -/ theorem resolve_eq_top_iff_le_anchor {db : DB} {s : Selector} (h0 : 0 ≤ Selector.anchor db s) : resolve db s = (⊤ : WithTop Key) ↔ db.keys.length ≤ (Selector.anchor db s).toNat := by constructor · intro h by_contra hlt have hlt' : (Selector.anchor db s).toNat < db.keys.length := not_le.mp hlt rw [resolve_eq_getElem h0 hlt'] at h exact WithTop.coe_ne_top h · intro h; exact resolve_eq_top h0 h /-- The end selector resolves past the end exactly when at most `end.offset - 1` stored keys lie at or above `end.key`. -/ theorem resolve_end_eq_top_iff_above_le {db : DB} {r : RangeRead} (hend : 1 ≤ r.end_.offset) : resolve db r.end_ = (⊤ : WithTop Key) ↔ (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := by rw [resolve_eq_top_iff_le_anchor (anchor_nonneg_of_one_le hend), anchor_toNat_eq hend] have hco : db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = cnt db r.end_.key := rfl rw [hco] exact length_le_cnt_add_iff_above_le db r.end_.key _ /-- A list all of whose elements equal `H`, containing `H`, and with no duplicates, is the singleton `[H]`. -/ theorem eq_singleton_of_forall_eq {l : List Key} {H : Key} (hnd : l.Nodup) (hH : H ∈ l) (hall : ∀ k ∈ l, k = H) : l = [H] := by have hrep : l = List.replicate l.length H := List.eq_replicate_of_mem hall have hnodup : l.Nodup := hnd rw [hrep] at hnodup have hle : l.length ≤ 1 := List.nodup_replicate.mp hnodup have hpos : 0 < l.length := List.length_pos_of_ne_nil (List.ne_nil_of_mem hH) have hlen : l.length = 1 := by omega rw [hrep, hlen, List.replicate_one] /-- At begin offset zero with `begin.key = ⊤` in a complete forward read, the result is the singleton reported head, the head is the greatest stored key, and the end selector resolves past the end. -/ theorem zeroCrossLongTop_structure {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (h0 : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) : res.keys = [res.keys.head hne] ∧ (∀ k ∈ db.keys, k ≤ res.keys.head hne) ∧ res.keys.head hne ∈ db.keys ∧ resolve db r.end_ = (⊤ : WithTop Key) := by have hres : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hHmem : res.keys.head hne ∈ res.keys := List.head_mem hne have hHck : res.keys.head hne ∈ RangeRead.completeKeys db r := by rw [← hres]; exact hHmem have hHdb : res.keys.head hne ∈ db.keys := (RangeRead.mem_completeKeys.mp hHck).1 have hpos : 0 < db.keys.length := List.length_pos_of_ne_nil (List.ne_nil_of_mem hHdb) set M : Key := db.keys.getLast (List.ne_nil_of_length_pos hpos) with hM have hMax : ∀ k ∈ db.keys, k ≤ M := by intro k hk exact (db.sorted.pairwise.imp le_of_lt).rel_getLast hk have hL : resolve db r.begin = ((M : Key) : WithTop Key) := by rw [hM]; exact resolve_begin_eq_getLast_of_top h0 htop hpos have hall : ∀ k ∈ res.keys, k = M := by intro k hk have hkck : k ∈ RangeRead.completeKeys db r := by rw [← hres]; exact hk have hle := (RangeRead.mem_completeKeys.mp hkck).2.1 rw [hL] at hle have hMle : M ≤ k := WithTop.coe_le_coe.mp hle exact le_antisymm (hMax k (RangeRead.mem_completeKeys.mp hkck).1) hMle have hH_eq : res.keys.head hne = M := hall _ hHmem have hsingle : res.keys = [M] := by have hnodup : res.keys.Nodup := (RangeRead.compatible_sortedLT hcomp hrev).pairwise.nodup exact eq_singleton_of_forall_eq hnodup (hH_eq ▸ hHmem) hall have hend_gt : ((M : Key) : WithTop Key) < resolve db r.end_ := by have h := (RangeRead.mem_completeKeys.mp hHck).2.2 rwa [hH_eq] at h have hend_top : resolve db r.end_ = (⊤ : WithTop Key) := by rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hn | ht | ⟨k, hk, hke⟩ · rw [hn] at hend_gt exact absurd hend_gt (not_lt.mpr (coe_nil_le_zero_cross_long _)) · exact ht · rw [hke] at hend_gt have hkM := hMax k hk exact absurd (WithTop.coe_lt_coe.mp hend_gt) (not_lt.mpr hkM) refine ⟨?_, ?_, ?_, hend_top⟩ · calc res.keys = [M] := hsingle _ = [res.keys.head hne] := by rw [hH_eq] · intro k hk; rw [hH_eq]; exact hMax k hk · simpa [hH_eq] using (RangeRead.mem_completeKeys.mp hHck).1 /-- The result of a complete begin-zero `begin.key = ⊤` read is the reported head. -/ theorem zeroCrossLongTop_keys_singleton {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) : res.keys = [res.keys.head hne] := by obtain ⟨db, hcomp⟩ := hread exact (zeroCrossLongTop_structure hcomp hrev hmore hne hbegin htop).1 /-- The stored keys at or above `H` are `[H]` when `H` is stored and is the maximum. -/ theorem above_eq_singleton_of_max {db : DB} {H : Key} (hH : H ∈ db.keys) (hall : ∀ k ∈ db.keys, k ≤ H) : above db (H : WithTop Key) = [H] := by apply eq_singleton_of_forall_eq · exact db.nodup.filter _ · rw [mem_above]; exact ⟨hH, le_rfl⟩ · intro k hk rw [mem_above] at hk exact le_antisymm (hall k hk.1) (WithTop.coe_le_coe.mp hk.2) /-- When every stored key is at or below `H`, the keys at or above `E` are exactly the keys in `[E, keyAfter H)`. -/ theorem above_eq_between_keyAfter (db : DB) (E : WithTop Key) (H : Key) (hall : ∀ k ∈ db.keys, k ≤ H) : above db E = between db E ((keyAfter H : Key) : WithTop Key) := by rw [above_eq_filter, between] apply List.filter_congr intro k hk have hkH : k ≤ H := hall k hk have hlt : ((k : Key) : WithTop Key) < ((keyAfter H : Key) : WithTop Key) := WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkH (keyAfter_self_lt H)) by_cases hE : E ≤ (k : WithTop Key) <;> simp [hE, hlt] /-- The interval `[E, keyAfter H)` has at most one more key than `[E, H)`. -/ theorem intervalCard_keyAfter_le (E : WithTop Key) (H : Key) : intervalCard E ((keyAfter H : Key) : WithTop Key) ≤ intervalCard E (H : WithTop Key) + 1 := by by_cases hEH : E ≤ (H : WithTop Key) · have hadd := intervalCard_add (b := E) (m := (H : WithTop Key)) (e := ((keyAfter H : Key) : WithTop Key)) hEH (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt H))) rw [hadd] have hone : intervalCard (H : WithTop Key) ((keyAfter H : Key) : WithTop Key) = 1 := by have := intervalCard_succChain H 1 simpa [keyAfter, List.replicate_one] using this rw [hone] · have hHltE : (H : WithTop Key) < E := lt_of_not_ge hEH have hkeyE : ((keyAfter H : Key) : WithTop Key) ≤ E := keyAfter_le_of_lt hHltE rw [intervalCard_eq_zero_of_le hkeyE] simp /-- The number of stored keys at or above `E` is at most `intervalCard E (keyAfter H)` when every stored key is at or below `H`. -/ theorem above_length_le_intervalCard_keyAfter {db : DB} {E : WithTop Key} {H : Key} (hall : ∀ k ∈ db.keys, k ≤ H) : ((above db E).length : ℕ∞) ≤ intervalCard E ((keyAfter H : Key) : WithTop Key) := by rw [above_eq_between_keyAfter db E H hall] by_cases hEB : E ≤ ((keyAfter H : Key) : WithTop Key) · exact between_length_le_intervalCard hEB · have hnil : between db E ((keyAfter H : Key) : WithTop Key) = [] := by rw [between, List.filter_eq_nil_iff] intro k hk hcond rw [decide_eq_true_eq] at hcond have hkeyE : ((keyAfter H : Key) : WithTop Key) ≤ E := le_of_not_ge hEB exact absurd (le_trans hkeyE hcond.1) (not_le.mpr hcond.2) rw [hnil] simp /-- If every stored key is at or below `H`, and `intervalCard E H + 1 ≤ n`, then at most `n` stored keys lie at or above `E`. -/ theorem above_length_le_of_max {db : DB} {E : WithTop Key} {H : Key} (hall : ∀ k ∈ db.keys, k ≤ H) {n : ℕ} (hge : intervalCard E (H : WithTop Key) + 1 ≤ (n : ℕ∞)) : (above db E).length ≤ n := by have h1 := above_length_le_intervalCard_keyAfter (db := db) (E := E) (H := H) hall have h2 := intervalCard_keyAfter_le E H have h3 : ((above db E).length : ℕ∞) ≤ (n : ℕ∞) := le_trans h1 (le_trans h2 hge) exact_mod_cast h3 /-- In the complete begin-zero `begin.key = ⊤` cell, compatibility is exactly: every stored key is at or below the reported head, the head is stored, and at most `end.offset - 1` stored keys lie at or above `end.key`. -/ theorem compatible_zero_cross_long_top_iff {db : DB} {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 1 ≤ r.end_.offset) : RangeRead.Compatible db r res ↔ RangeRead.withinLimit r res.keys ∧ (∀ k ∈ db.keys, k ≤ res.keys.head hne) ∧ res.keys.head hne ∈ db.keys ∧ (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := by have hsingle : res.keys = [res.keys.head hne] := zeroCrossLongTop_keys_singleton hread hrev hmore hne hbegin htop constructor · intro hcomp obtain ⟨-, hmax, hHmem, hRtop⟩ := zeroCrossLongTop_structure hcomp hrev hmore hne hbegin htop exact ⟨hcomp.1, hmax, hHmem, (resolve_end_eq_top_iff_above_le (db := db) (r := r) hend).mp hRtop⟩ · rintro ⟨hW, hmax, hHmem, habove⟩ have hpos : 0 < db.keys.length := List.length_pos_of_ne_nil (List.ne_nil_of_mem hHmem) have hHlast : db.keys.getLast (List.ne_nil_of_length_pos hpos) = res.keys.head hne := by apply le_antisymm · exact hmax _ (List.getLast_mem _) · exact (db.sorted.pairwise.imp le_of_lt).rel_getLast hHmem have hLbegin : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by rw [resolve_begin_eq_getLast_of_top hbegin htop hpos, hHlast] have hLend : resolve db r.end_ = (⊤ : WithTop Key) := (resolve_end_eq_top_iff_above_le (db := db) (r := r) hend).mpr habove have hck : RangeRead.completeKeys db r = [res.keys.head hne] := by unfold RangeRead.completeKeys rw [hrev] simp only [Bool.false_eq_true, ite_false] unfold RangeRead.inRange rw [hLbegin, hLend] apply eq_singleton_of_forall_eq · exact db.nodup.filter _ · rw [List.mem_filter] exact ⟨hHmem, by simp⟩ · intro k hk rw [List.mem_filter] at hk rw [decide_eq_true_eq] at hk exact le_antisymm (hmax k hk.1) (WithTop.coe_le_coe.mp hk.2.1) refine ⟨hW, Or.inr ⟨hmore, ?_⟩⟩ rw [hsingle, hck] /-- Agreement on the keys at or above `E` determines the keys at or above `E`. -/ theorem above_eq_of_agree {db₁ db₂ : DB} {E : WithTop Key} (h : ∀ (k : Key), E ≤ (k : WithTop Key) → (k ∈ db₁.keys ↔ k ∈ db₂.keys)) : above db₂ E = above db₁ E := by apply List.SortedLT.eq_of_mem_iff · exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) · exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _) · intro k rw [mem_above, mem_above] constructor · rintro ⟨hk2, hge⟩; exact ⟨(h k hge).mpr hk2, hge⟩ · rintro ⟨hk1, hge⟩; exact ⟨(h k hge).mp hk1, hge⟩ /-- In the crossed long cell the end overshoot is positive. -/ theorem zero_cross_long_top_e_pos {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) (hElth : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : 1 ≤ (r.end_.offset - 1).toNat := by obtain ⟨db₀, hcomp₀⟩ := hread obtain ⟨-, -, hHmem₀, hRtop₀⟩ := zeroCrossLongTop_structure hcomp₀ hrev hmore hne hbegin htop have hHabove : res.keys.head hne ∈ above db₀ r.end_.key := by rw [mem_above]; exact ⟨hHmem₀, le_of_lt hElth⟩ have hpos := List.length_pos_of_ne_nil (List.ne_nil_of_mem hHabove) have habove₀ := (resolve_end_eq_top_iff_above_le (db := db₀) (r := r) (by omega)).mp hRtop₀ omega /-- **Soundness of `[end.key, ⊤)` in the crossed long cell with room.** -/ theorem sound_zero_cross_long_top_E {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) (hElth : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : Sound r res ⟨r.end_.key, ⊤⟩ := by intro db₁ db₂ hagree have hg : ∀ (k : Key), r.end_.key ≤ (k : WithTop Key) → (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by intro k hk exact (agreeOn_iff ⟨r.end_.key, ⊤⟩ db₁ db₂).mp hagree k (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) have hiff : ∀ db : DB, RangeRead.Compatible db r res ↔ RangeRead.withinLimit r res.keys ∧ (∀ (k : Key), k ∈ db.keys → k ≤ res.keys.head hne) ∧ res.keys.head hne ∈ db.keys ∧ (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := fun db => compatible_zero_cross_long_top_iff (db := db) hread hrev hmore hne hbegin htop (by omega) have habove12 : above db₂ r.end_.key = above db₁ r.end_.key := above_eq_of_agree hg constructor · intro hc1 obtain ⟨hW, hmax1, hHmem1, habove1⟩ := (hiff db₁).mp hc1 refine (hiff db₂).mpr ⟨hW, ?_, ?_, ?_⟩ · intro k hk by_cases hkE : r.end_.key ≤ (k : WithTop Key) · exact hmax1 k ((hg k hkE).mpr hk) · exact WithTop.coe_le_coe.mp (le_trans (le_of_lt (lt_of_not_ge hkE)) (le_of_lt hElth)) · exact (hg _ (le_of_lt hElth)).mp hHmem1 · rw [habove12]; exact habove1 · intro hc2 obtain ⟨hW, hmax2, hHmem2, habove2⟩ := (hiff db₂).mp hc2 refine (hiff db₁).mpr ⟨hW, ?_, ?_, ?_⟩ · intro k hk by_cases hkE : r.end_.key ≤ (k : WithTop Key) · exact hmax2 k ((hg k hkE).mp hk) · exact WithTop.coe_le_coe.mp (le_trans (le_of_lt (lt_of_not_ge hkE)) (le_of_lt hElth)) · exact (hg _ (le_of_lt hElth)).mpr hHmem2 · rw [← habove12]; exact habove2 /-- **Soundness of `[head, ⊤)` in the crossed long cell without room.** -/ theorem sound_zero_cross_long_top_H {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) (hbound : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 1 ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞)) : Sound r res ⟨((res.keys.head hne : Key) : WithTop Key), ⊤⟩ := by intro db₁ db₂ hagree have hg : ∀ (k : Key), ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) → (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by intro k hk exact (agreeOn_iff ⟨((res.keys.head hne : Key) : WithTop Key), ⊤⟩ db₁ db₂).mp hagree k (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) have hiff : ∀ db : DB, RangeRead.Compatible db r res ↔ RangeRead.withinLimit r res.keys ∧ (∀ (k : Key), k ∈ db.keys → k ≤ res.keys.head hne) ∧ res.keys.head hne ∈ db.keys ∧ (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := fun db => compatible_zero_cross_long_top_iff (db := db) hread hrev hmore hne hbegin htop (by omega) constructor · intro hc1 obtain ⟨hW, hmax1, hHmem1, -⟩ := (hiff db₁).mp hc1 have hmax2 : ∀ k ∈ db₂.keys, k ≤ res.keys.head hne := by intro k hk by_cases hkH : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) · exact hmax1 k ((hg k hkH).mpr hk) · exact WithTop.coe_le_coe.mp (le_of_lt (lt_of_not_ge hkH)) refine (hiff db₂).mpr ⟨hW, hmax2, ?_, ?_⟩ · exact (hg _ le_rfl).mp hHmem1 · exact above_length_le_of_max (db := db₂) (E := r.end_.key) (H := res.keys.head hne) hmax2 hbound · intro hc2 obtain ⟨hW, hmax2, hHmem2, -⟩ := (hiff db₂).mp hc2 have hmax1 : ∀ k ∈ db₁.keys, k ≤ res.keys.head hne := by intro k hk by_cases hkH : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) · exact hmax2 k ((hg k hkH).mp hk) · exact WithTop.coe_le_coe.mp (le_of_lt (lt_of_not_ge hkH)) refine (hiff db₁).mpr ⟨hW, hmax1, ?_, ?_⟩ · exact (hg _ le_rfl).mpr hHmem2 · exact above_length_le_of_max (db := db₁) (E := r.end_.key) (H := res.keys.head hne) hmax1 hbound /-- **Necessity of `[end.key, ⊤)` in the crossed long cell with room.** -/ theorem necessary_zero_cross_long_top_E {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) (hElth : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) (hcond : (r.end_.offset - 1).toNat - res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) : NecessaryRange r res r.end_.key ⊤ := by intro k hlo hhi obtain ⟨db₀, hcomp₀⟩ := hread have hrd : Readable r res := ⟨db₀, hcomp₀⟩ have hsingle := zeroCrossLongTop_keys_singleton hrd hrev hmore hne hbegin htop obtain ⟨-, hmax₀, hHmem₀, hRtop₀⟩ := zeroCrossLongTop_structure hcomp₀ hrev hmore hne hbegin htop have hepos : 1 ≤ (r.end_.offset - 1).toNat := zero_cross_long_top_e_pos hrd hrev hmore hne hbegin htop hend hElth have ht1 : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = 1 := by rw [hsingle]; simp [le_of_lt hElth] have hge : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by rw [ht1] at hcond have hadd := (ENat.add_one_le_iff (m := (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞)) (by simp)).mpr hcond have hsucc : (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) + 1 = (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by rw [show (1 : ℕ∞) = ((1 : ℕ) : ℕ∞) by simp, ← Nat.cast_add, Nat.sub_add_cancel hepos] rwa [hsucc] at hadd have hcard : (((r.end_.offset - 1).toNat - 1 + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by rwa [Nat.sub_add_cancel hepos] by_cases hkH : k = res.keys.head hne · subst hkH exact necessary_of_mem_result hrd (List.head_mem hne) · by_cases hkgt : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) · -- the key lies above the head have hknotin : k ∉ db₀.keys := by intro hk exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hmax₀ k hk))) refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ intro hct have hC := (compatible_zero_cross_long_top_iff (db := toggle db₀ k) hrd hrev hmore hne hbegin htop (by omega)).mp hct have hkint : k ∈ (toggle db₀ k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknotin⟩) exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hC.2.1 k hkint))) · -- `end.key ≤ k < head`: fill `[end.key, head)` maximally obtain ⟨d, hdmem, hdk, hdlen⟩ := exists_db_keysIn_avoid (B := r.end_.key) (E := ((res.keys.head hne : Key) : WithTop Key)) (k := k) (m := (r.end_.offset - 1).toNat - 1) hcard let db1 : DB := ⟨d.keys ++ [res.keys.head hne], by refine List.sortedLT_append.mpr ⟨d.sorted, sortedLT_singleton _, ?_⟩ intro a ha b hb rw [List.mem_singleton] at hb subst hb exact WithTop.coe_lt_coe.mp (hdmem a ha).2⟩ have hdb1keys : db1.keys = d.keys ++ [res.keys.head hne] := rfl have hdb1_le : ∀ x ∈ db1.keys, x ≤ res.keys.head hne := by intro x hx rw [hdb1keys, List.mem_append, List.mem_singleton] at hx rcases hx with hx | rfl · exact le_of_lt (WithTop.coe_lt_coe.mp (hdmem x hx).2) · exact le_rfl have hHdb1 : res.keys.head hne ∈ db1.keys := by rw [hdb1keys]; exact List.mem_append.mpr (Or.inr (List.mem_singleton.mpr rfl)) have hknot1 : k ∉ db1.keys := by rw [hdb1keys, List.mem_append, List.mem_singleton] rintro (hk | hk) · exact hdk hk · exact hkH hk have habove1 : above db1 r.end_.key = d.keys ++ [res.keys.head hne] := by rw [above_eq_filter, hdb1keys, List.filter_append] congr 1 · rw [List.filter_eq_self] intro x hx rw [decide_eq_true_eq] exact (hdmem x hx).1 · simp [le_of_lt hElth] have habove_len : (above db1 r.end_.key).length = (r.end_.offset - 1).toNat := by rw [habove1, List.length_append, List.length_singleton, hdlen, Nat.sub_add_cancel hepos] have hcomp1 : RangeRead.Compatible db1 r res := (compatible_zero_cross_long_top_iff (db := db1) hrd hrev hmore hne hbegin htop (by omega)).mpr ⟨hcomp₀.1, hdb1_le, hHdb1, by rw [habove_len]⟩ refine necessary_of_toggle ⟨db1, hcomp1, ?_⟩ intro hct have hC := (compatible_zero_cross_long_top_iff (db := toggle db1 k) hrd hrev hmore hne hbegin htop (by omega)).mp hct have hkint : k ∈ (toggle db1 k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknot1⟩) have hkabove : k ∈ above (toggle db1 k) r.end_.key := by rw [mem_above]; exact ⟨hkint, hlo⟩ have hsub : (above db1 r.end_.key).Sublist (above (toggle db1 k) r.end_.key) := by unfold above apply List.Sublist.filter rw [toggle_eq_insert_of_not_mem hknot1, DB.insert_keys] exact List.sublist_orderedInsert k db1.keys have hnotmem : k ∉ above db1 r.end_.key := by rw [mem_above]; intro h; exact hknot1 h.1 have hne_len : (above db1 r.end_.key).length ≠ (above (toggle db1 k) r.end_.key).length := by intro heq have heqlist := hsub.eq_of_length heq exact hnotmem (heqlist ▸ hkabove) have hlt : (above db1 r.end_.key).length < (above (toggle db1 k) r.end_.key).length := lt_of_le_of_ne hsub.length_le hne_len have hlow := hC.2.2.2 rw [habove_len] at hlt omega /-- **Necessity of `[head, ⊤)` in the crossed long cell without room.** -/ theorem necessary_zero_cross_long_top_H {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) : NecessaryRange r res ((res.keys.head hne : Key) : WithTop Key) ⊤ := by intro k hlo hhi obtain ⟨db₀, hcomp₀⟩ := hread have hrd : Readable r res := ⟨db₀, hcomp₀⟩ obtain ⟨-, hmax₀, -, -⟩ := zeroCrossLongTop_structure hcomp₀ hrev hmore hne hbegin htop by_cases hkH : k = res.keys.head hne · subst hkH exact necessary_of_mem_result hrd (List.head_mem hne) · have hkgt : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) := by rcases lt_or_eq_of_le hlo with h | h · exact h · exact absurd (WithTop.coe_inj.mp h).symm hkH have hknotin : k ∉ db₀.keys := by intro hk exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hmax₀ k hk))) refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ intro hct have hC := (compatible_zero_cross_long_top_iff (db := toggle db₀ k) hrd hrev hmore hne hbegin htop (by omega)).mp hct have hkint : k ∈ (toggle db₀ k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknotin⟩) exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hC.2.1 k hkint))) /-- In the crossed long cell with `begin.key = ⊤`, the closed form is minimal. -/ theorem minimal_closedForm_of_zero_cross_long_top {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) : Minimal r res (closedFormZeroCrossLong r res) := by have hHd : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_sound hne have hsingle : res.keys = [res.keys.head hne] := zeroCrossLongTop_keys_singleton hread hrev hmore hne hbegin htop have ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (if r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) then 1 else 0) := by conv_lhs => rw [hsingle] by_cases hEH : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) <;> simp [hEH] unfold closedFormZeroCrossLong rw [ite_eq_left htop, hHd] dsimp only rw [ht] by_cases hcond : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) ∧ (((r.end_.offset - 1).toNat : ℕ∞) - ((if r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) then 1 else 0 : ℕ) : ℕ∞)) < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) · rw [ite_eq_left hcond] have hcle : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) := le_of_lt hcond.1 have ht1 : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = 1 := by conv_lhs => rw [hsingle] simp [hcle] have hcond' : (r.end_.offset - 1).toNat - res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by rw [ht1] simpa [hcle] using hcond.2 refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_zero_cross_long_top_E hread hrev hmore hne hbegin htop hend hcond.1, necessary_zero_cross_long_top_E hread hrev hmore hne hbegin htop hend hcond.1 hcond'⟩ · rw [ite_eq_right hcond] have hepos : 1 ≤ (r.end_.offset - 1).toNat := by omega have hbound : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 1 ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) · have hcle : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) := le_of_lt hEH have hnot : ¬ (((r.end_.offset - 1).toNat : ℕ∞) - ((if r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) then 1 else 0 : ℕ) : ℕ∞) < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) := fun hc => hcond ⟨hEH, hc⟩ have hge : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) ≤ (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) := by rw [ENat.natCast_sub] simpa [hcle] using not_lt.mp hnot have hsucc : (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) + 1 = (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by rw [show (1 : ℕ∞) = ((1 : ℕ) : ℕ∞) by simp, ← Nat.cast_add, Nat.sub_add_cancel hepos] calc intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 1 ≤ (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) + 1 := by simpa [add_comm] using add_le_add_right hge 1 _ = (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := hsucc · have hHE : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key := le_of_not_gt hEH rw [intervalCard_eq_zero_of_le hHE, zero_add] exact_mod_cast hepos refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_zero_cross_long_top_H hread hrev hmore hne hbegin htop hend hbound, necessary_zero_cross_long_top_H hread hrev hmore hne hbegin htop hend⟩ end FormalConflictRange namespace FormalConflictRange /-- In the crossed complete begin-zero long cell, the number of reported keys at or above `end_.key` is at most the end overshoot. -/ theorem reportedAtEnd_le_endOvershoot_zero_cross_long {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hend : 2 ≤ r.end_.offset) : reportedAtEnd r res ≤ endOvershoot r := by obtain ⟨db₀, hcomp₀⟩ := hread have hcompA₀ : RangeRead.Compatible db₀ (zeroBeginAnchor r res) res := compatible_anchor_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross have hrevA : (zeroBeginAnchor r res).reverse = false := by simp [zeroBeginAnchor, hrev] have hbeginA : (zeroBeginAnchor r res).begin.offset = 1 := by simp [zeroBeginAnchor] have hendA : 2 ≤ (zeroBeginAnchor r res).end_.offset := by simpa [zeroBeginAnchor] using hend have hres : res.keys = RangeRead.completeKeys db₀ (zeroBeginAnchor r res) := by rcases hcompA₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq unfold reportedAtEnd endOvershoot by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) · have hEH' : (zeroBeginAnchor r res).end_.key < (zeroBeginAnchor r res).begin.key := by rw [zeroBeginAnchor_end_key_eq, zeroBeginAnchor_begin_key_eq r res hne] exact hEH have h := crossed_complete_t_le_e_general (r := zeroBeginAnchor r res) ⟨db₀, hcompA₀⟩ hmore hrevA hne (by omega) hEH' (by omega) simpa [zeroBeginAnchor, endOvershoot] using h · have hle : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key := not_lt.mp hEH have hleA : (zeroBeginAnchor r res).begin.key ≤ (zeroBeginAnchor r res).end_.key := by rw [zeroBeginAnchor_begin_key_eq r res hne, zeroBeginAnchor_end_key_eq] exact hle have h := completeKeys_count_ge_le_general (db := db₀) (r := zeroBeginAnchor r res) hrevA (by omega) hleA (by omega) rw [hres] simpa [zeroBeginAnchor, endOvershoot] using h /-- In the crossed complete begin-zero long cell, `res.keys` itself is a compatible database: it stores exactly the reported keys, the begin selector resolves to the reported head, and the end selector resolves past the end. -/ theorem compatible_res_keys_zero_cross_long {r : RangeRead} {res : Result} (hsort : res.keys.SortedLT) (hr2 : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hend : 2 ≤ r.end_.offset) : RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by obtain ⟨db₀, hcomp₀⟩ := hr2 let db : DB := ⟨res.keys, hsort⟩ have hr2' : Readable r res := ⟨db₀, hcomp₀⟩ have hres : res.keys = RangeRead.completeKeys db₀ r := by rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hfilter := completeKeys_eq_filter_zero_crossed hcomp₀ hrev hmore hne hbegin hcross have hresfil : res.keys = db₀.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := hres.trans hfilter have hHmem : res.keys.head hne ∈ res.keys := List.head_mem hne have hHdb : res.keys.head hne ∈ db.keys := hHmem have hno : ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by intro k hk have hkdb : k ∈ db₀.keys := by have hk' : k ∈ RangeRead.completeKeys db₀ r := hres ▸ hk exact (RangeRead.mem_completeKeys.mp hk').1 exact no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross k hkdb have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := resolve_begin_eq_of_no_between_zero hbegin hHdb hcross hno have htle := reportedAtEnd_le_endOvershoot_zero_cross_long hr2' hrev hmore hne hbegin hcross hend have habove : (above db r.end_.key).length ≤ endOvershoot r := by unfold above rw [← List.countP_eq_length_filter] simpa [reportedAtEnd, endOvershoot] using htle have htop : resolve db r.end_ = (⊤ : WithTop Key) := (resolve_end_eq_top_iff_above_le (db := db) (r := r) (by omega)).mpr habove refine ⟨?_, ?_⟩ · simpa [RangeRead.withinLimit] using hcomp₀.1 · right refine ⟨hmore, ?_⟩ unfold RangeRead.completeKeys RangeRead.inRange rw [hrev] simp only [Bool.false_eq_true, ite_false] rw [hL, htop] symm apply List.filter_eq_self.mpr intro k hk rw [decide_eq_true_eq] have hkfil : k ∈ db₀.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := by rw [← hresfil]; exact hk exact ⟨(List.mem_filter.mp hkfil).2 |> of_decide_eq_true |> And.left, WithTop.coe_lt_top k⟩ end FormalConflictRange namespace FormalConflictRange /-- A key between the reported head and `begin.key` is necessary in the crossed complete begin-zero long cell. -/ theorem necessary_zero_cross_long_gap {r : RangeRead} {res : Result} {k : Key} (hsort : res.keys.SortedLT) (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hend : 2 ≤ r.end_.offset) (hkH : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key)) (hkB : (k : WithTop Key) < r.begin.key) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ have hres : res.keys = RangeRead.completeKeys db₀ r := by rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq let db : DB := ⟨res.keys, hsort⟩ have hcompR : RangeRead.Compatible db r res := compatible_res_keys_zero_cross_long hsort hread' hrev hmore hne hbegin hcross hend have hknot : k ∉ db.keys := by intro hk have hk' : k ∈ RangeRead.completeKeys db₀ r := by rw [← hres]; exact hk have hkdb : k ∈ db₀.keys := (RangeRead.mem_completeKeys.mp hk').1 exact no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross k hkdb ⟨hkH, hkB⟩ refine necessary_of_toggle ⟨db, hcompR, ?_⟩ intro hct have hkres' : res.keys = RangeRead.completeKeys (toggle db k) r := by rcases hct.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hkmem : k ∈ (toggle db k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknot⟩) have hHnot : res.keys.head hne ∉ RangeRead.completeKeys (toggle db k) r := by intro hmem unfold RangeRead.completeKeys RangeRead.inRange at hmem rw [hrev] at hmem simp only [Bool.false_eq_true, ite_false] at hmem have hcond := (List.mem_filter.mp hmem).2 rw [decide_eq_true_eq] at hcond have hkr : ((k : Key) : WithTop Key) ≤ resolve (toggle db k) r.begin := resolve_zero_begin_le_of_mem_below hbegin hkmem hkB k hkmem hkB exact absurd (le_trans hkr hcond.1) (not_le.mpr hkH) exact hHnot (hkres' ▸ List.head_mem hne) /-- A key between `begin.key` and the reported last is necessary in the crossed complete begin-zero long cell. -/ theorem necessary_zero_cross_long_between_last {r : RangeRead} {res : Result} {k : Key} (hread : Readable r res) (hne : res.keys ≠ []) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hkB : r.begin.key ≤ (k : WithTop Key)) (hkL : k ≤ res.keys.getLast hne) : Necessary r res k := necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) (WithTop.coe_le_coe.mp (le_trans (le_of_lt hcross) hkB)) hkL end FormalConflictRange namespace FormalConflictRange /-- A key above the reported last is necessary in the crossed complete begin-zero long cell when the end overshoots (`t < e`). -/ theorem necessary_zero_cross_long_above_last {r : RangeRead} {res : Result} {k : Key} (hsort : res.keys.SortedLT) (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hlong : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) (ht : reportedAtEnd r res < endOvershoot r) (hkB : r.begin.key ≤ (k : WithTop Key)) (hkL : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ let db : DB := ⟨res.keys, hsort⟩ have hcompR : RangeRead.Compatible db r res := compatible_res_keys_zero_cross_long hsort hread' hrev hmore hne hbegin hcross hend have hkLle : ∀ a ∈ res.keys, a ≤ res.keys.getLast hne := by intro a ha simpa using (hsort.pairwise.imp le_of_lt).rel_getLast ha have hknot : k ∉ db.keys := by intro hk exact absurd (WithTop.coe_le_coe.mpr (hkLle k hk)) (not_le.mpr hkL) have hkE : r.end_.key ≤ (k : WithTop Key) := le_trans (le_of_lt hlong) hkB refine necessary_of_toggle ⟨db, hcompR, ?_⟩ intro hct have hres' : res.keys = RangeRead.completeKeys (toggle db k) r := by rcases hct.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have htk : toggle db k = db.insert k hknot := toggle_eq_insert_of_not_mem hknot have hab : (above db r.end_.key).length = reportedAtEnd r res := by unfold reportedAtEnd above rw [List.countP_eq_length_filter] have hlen' : (above (toggle db k) r.end_.key).length ≤ endOvershoot r := by rw [← length_le_cnt_add_iff_above_le (toggle db k) r.end_.key (endOvershoot r)] rw [htk, cnt_insert_not_lt db k hknot (not_lt.mpr hkE), DB.insert_keys, List.orderedInsert_length] have hcnt := cnt_add_above_length db r.end_.key rw [hab] at hcnt omega have hktop : resolve (toggle db k) r.end_ = (⊤ : WithTop Key) := (resolve_end_eq_top_iff_above_le (db := toggle db k) (r := r) (by omega)).mpr hlen' have hHdb : res.keys.head hne ∈ (toggle db k).keys := mem_toggle_keys.mpr (Or.inr ⟨by intro h exact absurd hkL (not_lt.mpr (WithTop.coe_le_coe.mpr (h ▸ (hkLle _ (List.head_mem hne))))), List.head_mem hne⟩) have hresbeg : resolve (toggle db k) r.begin < r.begin.key := resolve_zero_begin_lt_key hbegin hHdb hcross have hkdb' : k ∈ (toggle db k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknot⟩) have hkck : k ∈ RangeRead.completeKeys (toggle db k) r := RangeRead.mem_completeKeys.mpr ⟨hkdb', le_trans (le_of_lt hresbeg) hkB, by rw [hktop]; exact WithTop.coe_lt_top k⟩ have : k ∈ res.keys := by rw [hres']; exact hkck exact hknot this end FormalConflictRange namespace FormalConflictRange /-- A key between `end_.key` and the reported head is necessary in the crossed complete begin-zero long cell when the result is finite (`t = e`). -/ theorem necessary_zero_cross_long_below_head_eq {r : RangeRead} {res : Result} {k : Key} (hsort : res.keys.SortedLT) (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (_hlong : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) (hte : reportedAtEnd r res = endOvershoot r) (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) (hkE : r.end_.key ≤ (k : WithTop Key)) (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : Necessary r res k := by obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ let db : DB := ⟨res.keys, hsort⟩ have hcompR : RangeRead.Compatible db r res := compatible_res_keys_zero_cross_long hsort hread' hrev hmore hne hbegin hcross hend have hhead_le : ∀ q ∈ res.keys, ((res.keys.head hne : Key) : WithTop Key) ≤ (q : WithTop Key) := by intro q hq have := (hsort.pairwise.imp le_of_lt).rel_head hq simpa using this have hkLle : ∀ a ∈ res.keys, a ≤ res.keys.getLast hne := by intro a ha simpa using (hsort.pairwise.imp le_of_lt).rel_getLast ha have hknot : k ∉ db.keys := by intro hk exact absurd (hhead_le k hk) (not_le.mpr hkH) have hkres : k ∉ res.keys := hknot have htk : toggle db k = db.insert k hknot := toggle_eq_insert_of_not_mem hknot refine necessary_of_toggle ⟨db, hcompR, ?_⟩ intro hct have hres' : res.keys = RangeRead.completeKeys (toggle db k) r := by rcases hct.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq have hHmem : res.keys.head hne ∈ (toggle db k).keys := mem_toggle_keys.mpr (Or.inr ⟨by intro h; exact (lt_irrefl (k : WithTop Key)) (h ▸ hkH), List.head_mem hne⟩) have hno : ∀ q ∈ (toggle db k).keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (q : WithTop Key) ∧ (q : WithTop Key) < r.begin.key) := by intro q hq rcases (mem_toggle_keys.mp hq) with ⟨rfl, _⟩ | ⟨_, hqmem⟩ · exact fun h => absurd h.1 (not_lt.mpr (le_of_lt hkH)) · have hqdb : q ∈ db₀.keys := (RangeRead.mem_completeKeys.mp (by rw [← (by rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · exact heq : res.keys = RangeRead.completeKeys db₀ r)] exact hqmem)).1 exact no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross q hqdb have hLbegin : resolve (toggle db k) r.begin = ((res.keys.head hne : Key) : WithTop Key) := resolve_begin_eq_of_no_between_zero hbegin hHmem hcross hno have hHL : res.keys.head hne ≤ res.keys.getLast hne := hkLle _ (List.head_mem hne) have hmax : ∀ q ∈ (toggle db k).keys, q ≤ res.keys.getLast hne := by intro q hq rcases (mem_toggle_keys.mp hq) with ⟨rfl, _⟩ | ⟨_, hqmem⟩ · exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkH)) hHL · exact hkLle q hqmem have habove_all : above (toggle db k) r.end_.key = (toggle db k).keys := by unfold above apply List.filter_eq_self.mpr intro q hq rw [decide_eq_true_eq] rcases (mem_toggle_keys.mp hq) with ⟨rfl, _⟩ | ⟨_, hqmem⟩ · exact hkE · exact le_trans (le_of_lt hEH) (hhead_le q hqmem) have habove_len : (above (toggle db k) r.end_.key).length = res.keys.length + 1 := by rw [habove_all, htk, DB.insert_keys, List.orderedInsert_length] have htlen : reportedAtEnd r res = res.keys.length := by unfold reportedAtEnd rw [List.countP_eq_length] intro q hq rw [decide_eq_true_eq] exact le_trans (le_of_lt hEH) (hhead_le q hq) have hnot_top : resolve (toggle db k) r.end_ ≠ (⊤ : WithTop Key) := by intro htop have hiff := (resolve_end_eq_top_iff_above_le (db := toggle db k) (r := r) (by omega)).mp htop rw [habove_len] at hiff have hte' : res.keys.length = (r.end_.offset - 1).toNat := by unfold endOvershoot at hte exact htlen.symm.trans hte omega have hresle : resolve (toggle db k) r.end_ ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by rcases resolve_eq_nil_or_eq_top_or_mem (db := toggle db k) (s := r.end_) with hn | ht' | ⟨q, hq, hqe⟩ · rw [hn]; exact coe_nil_le_zero_cross_long _ · exact absurd ht' hnot_top · rw [hqe]; exact WithTop.coe_le_coe.mpr (hmax q hq) have hLnot : res.keys.getLast hne ∉ RangeRead.completeKeys (toggle db k) r := by intro hmem unfold RangeRead.completeKeys RangeRead.inRange at hmem rw [hrev] at hmem simp only [Bool.false_eq_true, ite_false] at hmem have hcond := (List.mem_filter.mp hmem).2 rw [decide_eq_true_eq] at hcond exact absurd hcond.2 (not_lt.mpr hresle) have : res.keys.getLast hne ∈ RangeRead.completeKeys (toggle db k) r := by rw [← hres']; exact List.getLast_mem hne exact hLnot this /-- A key in `[end_.key, head)` is necessary in the crossed complete begin-zero long cell when the end overshoots (`t < e`) and there is enough room (`e - t ≤ g`). -/ theorem necessary_zero_cross_long_below_head_lt {r : RangeRead} {res : Result} {k : Key} (hsort : res.keys.SortedLT) (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hend : 2 ≤ r.end_.offset) (ht : reportedAtEnd r res < endOvershoot r) (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) (hroom : (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) (hB : r.begin.key ≠ (⊤ : WithTop Key)) (hkE : r.end_.key ≤ (k : WithTop Key)) (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : Necessary r res k := by obtain ⟨Bk, hBk⟩ : ∃ Bk : Key, r.begin.key = ((Bk : Key) : WithTop Key) := by cases h : r.begin.key with | top => exact absurd h hB | coe b => exact ⟨b, rfl⟩ obtain ⟨db₀, hcomp₀⟩ := hread have hread' : Readable r res := ⟨db₀, hcomp₀⟩ have hhead_le : ∀ q ∈ res.keys, ((res.keys.head hne : Key) : WithTop Key) ≤ (q : WithTop Key) := by intro q hq simpa using (hsort.pairwise.imp le_of_lt).rel_head hq have htlen : reportedAtEnd r res = res.keys.length := by unfold reportedAtEnd rw [List.countP_eq_length] intro q hq rw [decide_eq_true_eq] exact le_trans (le_of_lt hEH) (hhead_le q hq) have hrest_ge_B : ∀ q ∈ res.keys, q ≠ res.keys.head hne → ((Bk : Key) : WithTop Key) ≤ (q : WithTop Key) := by intro q hq hqH have hqdb : q ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hq have hno := no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross q hqdb have hleH := hhead_le q hq have hltH : ((res.keys.head hne : Key) : WithTop Key) < (q : WithTop Key) := lt_of_le_of_ne hleH (fun hEq => hqH (WithTop.coe_injective hEq.symm)) have hnotlt : ¬ (q : WithTop Key) < r.begin.key := fun h => hno ⟨hltH, h⟩ rw [hBk] at hnotlt exact not_lt.mp hnotlt have hHltBk : res.keys.head hne < Bk := by have h := hcross rw [hBk] at h exact WithTop.coe_lt_coe.mp h set s : ℕ := endOvershoot r - reportedAtEnd r res with hsdef have hs : 0 < s := by unfold s omega have hroom' : ((s : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by simpa [s] using hroom have hcard : (((s - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by rw [Nat.sub_add_cancel (by omega : 1 ≤ s)] exact hroom' obtain ⟨dbH0, hH0range, hkH0, hH0len⟩ := exists_db_keysIn_avoid (B := r.end_.key) (E := ((res.keys.head hne : Key) : WithTop Key)) (k := k) (m := s - 1) hcard have hH0geE : ∀ x ∈ dbH0.keys, r.end_.key ≤ (x : WithTop Key) := fun x hx => (hH0range x hx).1 have hH0ltH : ∀ x ∈ dbH0.keys, (x : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := fun x hx => (hH0range x hx).2 obtain ⟨C, hCge, hCnotin⟩ := exists_le_not_mem (res.keys ++ dbH0.keys) (max Bk (res.keys.getLast hne)) have hCnotres : C ∉ res.keys := fun h => hCnotin (List.mem_append_left _ h) have hCnotH0 : C ∉ dbH0.keys := fun h => hCnotin (List.mem_append_right _ h) have hCBk : Bk ≤ C := le_trans (le_max_left _ _) hCge have hCL : res.keys.getLast hne ≤ C := le_trans (le_max_right _ _) hCge have hCH : ((res.keys.head hne : Key) : WithTop Key) ≤ (C : WithTop Key) := le_trans (le_of_lt hcross) (by rw [hBk]; exact WithTop.coe_le_coe.mpr hCBk) have hCgtRes : ∀ q ∈ res.keys, q < C := by intro q hq have hle : q ≤ res.keys.getLast hne := by simpa using (hsort.pairwise.imp le_of_lt).rel_getLast hq exact lt_of_le_of_ne (le_trans hle hCL) (fun h => hCnotres (h ▸ hq)) have hkC : k < C := WithTop.coe_lt_coe.mp (lt_of_lt_of_le hkH hCH) -- membership facts for the extended head block let dbH : DB := dbH0.insert k hkH0 have hmem_dbH : ∀ x, x ∈ dbH.keys ↔ x = k ∨ x ∈ dbH0.keys := by intro x rw [show dbH.keys = dbH0.keys.orderedInsert (· ≤ ·) k from DB.insert_keys dbH0 k hkH0] rw [List.mem_orderedInsert (· ≤ ·)] have hHgeE : ∀ x ∈ dbH.keys, r.end_.key ≤ (x : WithTop Key) := by intro x hx rcases (hmem_dbH x).mp hx with rfl | hx0 · exact hkE · exact hH0geE x hx0 have hHltH : ∀ x ∈ dbH.keys, (x : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := by intro x hx rcases (hmem_dbH x).mp hx with rfl | hx0 · exact hkH · exact hH0ltH x hx0 have hHnotin_res : ∀ x ∈ dbH.keys, x ∉ res.keys := by intro x hx hxr have hxH := hHltH x hx have hHx := hhead_le x hxr exact absurd hHx (not_le.mpr hxH) have hHsort : dbH.keys.SortedLT := dbH.sorted have hdbH_res : (dbH.keys ++ res.keys).SortedLT := List.sortedLT_append.mpr ⟨hHsort, hsort, by intro a ha c hc exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltH a ha) (hhead_le c hc))⟩ have hsingle : ([C] : List Key).SortedLT := by rw [List.sortedLT_cons] exact ⟨by simp, List.sortedLT_nil⟩ have hcrossList : ∀ a ∈ (dbH.keys ++ res.keys), ∀ b ∈ ([C] : List Key), a < b := by intro a ha b hb rw [List.mem_singleton] at hb subst hb rcases List.mem_append.mp ha with haH | haR · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltH a haH) hCH) · exact hCgtRes a haR have hsortBig : (dbH.keys ++ res.keys ++ [C]).SortedLT := List.sortedLT_append.mpr ⟨hdbH_res, hsingle, hcrossList⟩ let dbBig : DB := ⟨dbH.keys ++ res.keys ++ [C], hsortBig⟩ have hkeysBig : dbBig.keys = dbH.keys ++ res.keys ++ [C] := rfl have hkBig : k ∈ dbBig.keys := by rw [hkeysBig] exact List.mem_append_left _ (List.mem_append_left _ ((hmem_dbH k).mpr (Or.inl rfl))) have hHBig : res.keys.head hne ∈ dbBig.keys := by rw [hkeysBig] exact List.mem_append_left _ (List.mem_append_right _ (List.head_mem hne)) have hnoBig : ∀ q ∈ dbBig.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (q : WithTop Key) ∧ (q : WithTop Key) < r.begin.key) := by intro q hq hqb rw [hkeysBig] at hq rcases List.mem_append.mp hq with hq' | hqC · rcases List.mem_append.mp hq' with hqH | hqR · exact absurd hqb.1 (not_lt.mpr (le_of_lt (hHltH q hqH))) · have hqeq : q = res.keys.head hne := by by_contra hne' have hge := hrest_ge_B q hqR hne' rw [hBk] at hqb exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (WithTop.coe_le_coe.mp hge))) exact absurd (hqeq ▸ hqb.1) (lt_irrefl _) · rw [List.mem_singleton] at hqC subst hqC rw [hBk] at hqb exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr hCBk)) have hLbegin : resolve dbBig r.begin = ((res.keys.head hne : Key) : WithTop Key) := resolve_begin_eq_of_no_between_zero hbegin hHBig hcross hnoBig -- the end selector resolves to `C` have hcEBig : cnt dbBig r.end_.key = 0 := by apply cnt_eq_zero_of_forall_ge intro x hx rw [hkeysBig] at hx rcases List.mem_append.mp hx with hx' | hxC · rcases List.mem_append.mp hx' with hxH | hxR · exact hHgeE x hxH · exact le_trans (le_of_lt hEH) (hhead_le x hxR) · rw [List.mem_singleton] at hxC subst hxC exact le_trans (le_of_lt hEH) hCH have hlenBig : dbBig.keys.length = endOvershoot r + 1 := by have hHlen : dbH.keys.length = s := by rw [show dbH.keys = dbH0.keys.orderedInsert (· ≤ ·) k from DB.insert_keys dbH0 k hkH0, List.orderedInsert_length, hH0len] omega rw [hkeysBig, List.length_append, List.length_append, hHlen, List.length_singleton] have : reportedAtEnd r res + s = endOvershoot r := by unfold s omega omega have hanchorBig : (r.end_.anchor dbBig).toNat = endOvershoot r := by have h := anchor_toNat_eq (s := r.end_) (db := dbBig) (by omega : 1 ≤ r.end_.offset) rw [h] have hc : dbBig.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by simpa [cnt] using hcEBig rw [hc, Nat.zero_add] rfl have hresend : resolve dbBig r.end_ = ((C : Key) : WithTop Key) := by have hbig : dbBig.keys = (dbH.keys ++ res.keys) ++ [C] := by rw [hkeysBig, List.append_assoc] have hlenpre : (dbH.keys ++ res.keys).length = endOvershoot r := by rw [List.length_append] have hHlen : dbH.keys.length = s := by rw [show dbH.keys = dbH0.keys.orderedInsert (· ≤ ·) k from DB.insert_keys dbH0 k hkH0, List.orderedInsert_length, hH0len] omega rw [hHlen] unfold s omega have hget : dbBig.keys[endOvershoot r]? = some C := by rw [hbig, show endOvershoot r = (dbH.keys ++ res.keys).length from hlenpre.symm] simp rw [resolve, ite_eq_right (not_lt.mpr (anchor_nonneg_of_one_le (by omega : 1 ≤ r.end_.offset)))] simp only [hanchorBig] rw [hget] have hckBig : RangeRead.completeKeys dbBig r = res.keys := by unfold RangeRead.completeKeys RangeRead.inRange rw [hrev] simp only [Bool.false_eq_true, ite_false] rw [hLbegin, hresend] rw [hkeysBig] have hfil : (dbH.keys ++ res.keys ++ [C]).filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < ((C : Key) : WithTop Key))) = res.keys := by rw [List.filter_append, List.filter_append] have hHnil : dbH.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < ((C : Key) : WithTop Key))) = [] := by apply List.filter_eq_nil_iff.mpr intro x hx hcond rw [decide_eq_true_eq] at hcond exact absurd hcond.1 (not_le.mpr (hHltH x hx)) rw [hHnil, List.nil_append] have hresfil : res.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < ((C : Key) : WithTop Key))) = res.keys := by apply List.filter_eq_self.mpr intro x hx rw [decide_eq_true_eq] exact ⟨hhead_le x hx, WithTop.coe_lt_coe.mpr (hCgtRes x hx)⟩ rw [hresfil] have hCnil : ([C] : List Key).filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < ((C : Key) : WithTop Key))) = [] := by apply List.filter_eq_nil_iff.mpr intro x hx hcond rw [List.mem_singleton] at hx rw [hx] at hcond rw [decide_eq_true_eq] at hcond exact absurd hcond.2 (lt_irrefl (C : WithTop Key)) rw [hCnil] simp rw [hfil] have hcompBig : RangeRead.Compatible dbBig r res := ⟨by simpa [RangeRead.withinLimit, hkeysBig] using hcomp₀.1, Or.inr ⟨hmore, hckBig.symm⟩⟩ -- erasing `k` breaks compatibility have hEraseKeys : (dbBig.erase k).keys = dbH0.keys ++ res.keys ++ [C] := by rw [DB.erase_keys, hkeysBig] have hHerase : (dbH.keys).erase k = dbH0.keys := DB.erase_insert_keys dbH0 k hkH0 have hnotinRest : k ∉ res.keys ++ [C] := by simp only [List.mem_append, List.mem_singleton] rintro (hkr | rfl) · exact hHnotin_res k ((hmem_dbH k).mpr (Or.inl rfl)) hkr · exact absurd hkC (lt_irrefl k) rw [List.append_assoc, List.erase_append, ite_eq_left ((hmem_dbH k).mpr (Or.inl rfl)), hHerase] simp [List.append_assoc] let dbErase : DB := dbBig.erase k have hkeysErase : dbErase.keys = dbH0.keys ++ res.keys ++ [C] := hEraseKeys have hnotcompErase : ¬ RangeRead.Compatible dbErase r res := by intro hc have hck := hc.2 rcases hck with ⟨hm, _, _⟩ | ⟨_, heq⟩ · simp [hmore] at hm · have hlenEq : (RangeRead.completeKeys dbErase r).length = res.keys.length := by rw [heq] -- compute completeKeys length on dbErase have hcEe : cnt dbErase r.end_.key = 0 := by apply cnt_eq_zero_of_forall_ge intro x hx rw [hkeysErase] at hx rcases List.mem_append.mp hx with hx' | hxC · rcases List.mem_append.mp hx' with hxH | hxR · exact hH0geE x hxH · exact le_trans (le_of_lt hEH) (hhead_le x hxR) · rw [List.mem_singleton] at hxC subst hxC exact le_trans (le_of_lt hEH) hCH have hlenErase : dbErase.keys.length = endOvershoot r := by rw [hkeysErase, List.length_append, List.length_append, hH0len, List.length_singleton] have : reportedAtEnd r res + s = endOvershoot r := by unfold s omega omega have hanchorE : (r.end_.anchor dbErase).toNat = endOvershoot r := by have h := anchor_toNat_eq (s := r.end_) (db := dbErase) (by omega : 1 ≤ r.end_.offset) rw [h] have hce : dbErase.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by simpa [cnt] using hcEe rw [hce, Nat.zero_add] rfl have htopErase : resolve dbErase r.end_ = (⊤ : WithTop Key) := by rw [resolve_eq_top (anchor_nonneg_of_one_le (by omega : 1 ≤ r.end_.offset)) (by rw [hanchorE, hlenErase])] have hLerase : resolve dbErase r.begin = ((res.keys.head hne : Key) : WithTop Key) := by apply resolve_begin_eq_of_no_between_zero hbegin · rw [hkeysErase] exact List.mem_append_left _ (List.mem_append_right _ (List.head_mem hne)) · exact hcross · intro q hq hqb rw [hkeysErase] at hq rcases List.mem_append.mp hq with hq' | hqC · rcases List.mem_append.mp hq' with hqH | hqR · exact absurd hqb.1 (not_lt.mpr (le_of_lt (hH0ltH q hqH))) · have hqeq : q = res.keys.head hne := by by_contra hne' have hge := hrest_ge_B q hqR hne' rw [hBk] at hqb exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (WithTop.coe_le_coe.mp hge))) exact absurd (hqeq ▸ hqb.1) (lt_irrefl _) · rw [List.mem_singleton] at hqC subst hqC rw [hBk] at hqb exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr hCBk)) have hckErase : RangeRead.completeKeys dbErase r = res.keys ++ [C] := by unfold RangeRead.completeKeys RangeRead.inRange rw [hrev] simp only [Bool.false_eq_true, ite_false] rw [hLerase, htopErase, hkeysErase] have hfil : (dbH0.keys ++ res.keys ++ [C]).filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (⊤ : WithTop Key))) = res.keys ++ [C] := by rw [List.filter_append, List.filter_append] have hHnil : dbH0.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (⊤ : WithTop Key))) = [] := by apply List.filter_eq_nil_iff.mpr intro x hx hcond rw [decide_eq_true_eq] at hcond exact absurd hcond.1 (not_le.mpr (hH0ltH x hx)) rw [hHnil, List.nil_append] have hresfil : res.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (⊤ : WithTop Key))) = res.keys := by apply List.filter_eq_self.mpr intro x hx rw [decide_eq_true_eq] exact ⟨hhead_le x hx, WithTop.coe_lt_top x⟩ rw [hresfil] have hCsingle : ([C] : List Key).filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < (⊤ : WithTop Key))) = [C] := by apply List.filter_eq_self.mpr intro x hx rw [decide_eq_true_eq] rw [List.mem_singleton] at hx rw [hx] exact ⟨hCH, WithTop.coe_lt_top C⟩ rw [hCsingle] rw [hfil] rw [hckErase, List.length_append, List.length_singleton] at hlenEq omega refine necessary_of_toggle ⟨dbBig, hcompBig, ?_⟩ rw [toggle_eq_erase_of_mem hkBig] exact hnotcompErase /-- The end endpoint of the long-cell anchor, in terms of the reported last. -/ theorem zeroCrossLongAnchor_end_eq (r : RangeRead) (res : Result) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) : (zeroCrossLongAnchor r res).end_ = if reportedAtEnd r res = endOvershoot r then (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) else ⊤ := by by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) · rw [zeroCrossLongAnchor_end_crossed hne hbegin hend hEH, getLastD_eq_getLast_zero_cross_long hne] · rw [zeroCrossLongAnchor_end_crossfree hne hbegin hend hEH, getLastD_eq_getLast_zero_cross_long hne] /-- The crossed complete begin-zero long cell at finite `begin.key`: the closed form `closedFormZeroCrossLong` is minimal. -/ theorem minimal_closedForm_of_zero_cross_long_finite {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) (hend : 2 ≤ r.end_.offset) (hlong : r.end_.key < r.begin.key) (hB : r.begin.key ≠ (⊤ : WithTop Key)) : Minimal r res (closedFormZeroCrossLong r res) := by have hsort : res.keys.SortedLT := by obtain ⟨db, hc⟩ := hread exact RangeRead.compatible_sortedLT hc hrev have hminA := minimal_zeroCrossLongAnchor hread hrev hmore hne hbegin hcross hend have hsoundA : Sound (zeroBeginAnchor r res) res (zeroCrossLongAnchor r res) := hminA.1 have hsub : Subrange (zeroCrossLongAnchor r res) ⟨(zeroCrossLongAnchor r res).begin, max (zeroCrossLongAnchor r res).end_ r.begin.key⟩ := subrange_of_le le_rfl (le_max_left _ _) have hsound : Sound r res (closedFormZeroCrossLong r res) := by unfold closedFormZeroCrossLong rw [ite_eq_right hB] refine sound_of_anchor_sound hrev hmore hne hbegin hcross ?_ ?_ hsub hsoundA · by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH] by_cases hc : (reportedAtEnd r res = endOvershoot r ∨ (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) · rw [ite_eq_left hc] exact le_of_lt hEH · rw [ite_eq_right hc] · rw [zeroCrossLongAnchor_begin_crossfree hne hbegin hend hEH] · exact le_max_right _ _ refine minimal_iff_sound_and_necessaryRange.mpr ⟨hsound, ?_⟩ intro k hklo hkhi unfold closedFormZeroCrossLong at hklo hkhi rw [ite_eq_right hB] at hklo hkhi dsimp only at hklo hkhi have htle := reportedAtEnd_le_endOvershoot_zero_cross_long hread hrev hmore hne hbegin hcross hend by_cases hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) · -- below the reported head have hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) := by by_contra hcon rw [zeroCrossLongAnchor_begin_crossfree hne hbegin hend hcon] at hklo exact absurd hklo (not_le.mpr hkH) have hkE : r.end_.key ≤ (k : WithTop Key) := by by_cases hc : (reportedAtEnd r res = endOvershoot r ∨ (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH, ite_eq_left hc] at hklo exact hklo · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH, ite_eq_right hc] at hklo exact absurd hklo (not_le.mpr hkH) by_cases hte : reportedAtEnd r res = endOvershoot r · exact necessary_zero_cross_long_below_head_eq hsort hread hrev hmore hne hbegin hcross hlong hend hte hEH hkE hkH · have hroom : (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) := by by_cases hc : (reportedAtEnd r res = endOvershoot r ∨ (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) · exact hc.resolve_left hte · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH, ite_eq_right hc] at hklo exact absurd hklo (not_le.mpr hkH) exact necessary_zero_cross_long_below_head_lt hsort hread hrev hmore hne hbegin hcross hend (lt_of_le_of_ne htle hte) hEH hroom hB hkE hkH · push Not at hkH by_cases hkB : (k : WithTop Key) < r.begin.key · by_cases hkHeq : ((res.keys.head hne : Key) : WithTop Key) = (k : WithTop Key) · have hkEq : k = res.keys.head hne := WithTop.coe_injective hkHeq.symm exact necessary_of_mem_result hread (hkEq ▸ List.head_mem hne) · have hHlt : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) := lt_of_le_of_ne hkH hkHeq exact necessary_zero_cross_long_gap hsort hread hrev hmore hne hbegin hcross hend hHlt hkB · push Not at hkB by_cases hkL : (k : WithTop Key) ≤ ((res.keys.getLast hne : Key) : WithTop Key) · exact necessary_zero_cross_long_between_last hread hne hcross hkB (WithTop.coe_le_coe.mp hkL) · push Not at hkL have ht : reportedAtEnd r res < endOvershoot r := by by_contra hcon push Not at hcon have hte : reportedAtEnd r res = endOvershoot r := le_antisymm htle hcon have hltmax := hkhi rw [lt_max_iff] at hltmax rcases hltmax with hlt | hlt · rw [zeroCrossLongAnchor_end_eq r res hne hbegin hend, ite_eq_left hte] at hlt exact absurd (lt_keyAfter_iff k (res.keys.getLast hne) |>.mp (WithTop.coe_lt_coe.mp hlt)) (not_le.mpr (WithTop.coe_lt_coe.mp hkL)) · exact absurd hlt (not_lt.mpr hkB) exact necessary_zero_cross_long_above_last hsort hread hrev hmore hne hbegin hcross hlong hend ht hkB hkL end FormalConflictRange