/- 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.Empty import FormalConflictRange.Reduction /-! # The empty theory at a nonpositive begin offset `emptyValue`'s canonical branch is unsound when `begin.offset ≤ 0` (`Empty.lean`, `compatible_empty_iff_clamp`): the resolved begin depends on stored keys strictly below `begin.key`, outside the canonical range, and may reach the empty key. This file begins the replacement theory in the difference-constraint language. The emptiness predicate at `begin.offset ≤ 0` is a single scalar inequality in the counts below the two selector keys (`compatible_empty_iff_nonpos`); at `end.offset ≤ 1` the empty-database disjunct is subsumed (`compatible_empty_iff_nonpos_endle`). Soundness of the candidate bottom range `[⊥, max begin.key end.key)` then follows because agreement on it fixes both counts (`sound_bottomRange_of_nonpos_underEnd`), with `[⊥, end.key)` the cross-free special case (`sound_bottomRange_of_nonpos_underEnd_le`). The candidates are minimal where the empty theory can separate keys. At `end.offset = 1` the empty database is a universal flip witness: toggling any key below `end.key` yields the singleton, whose complete keys are nonempty, so `[⊥, end.key)` is minimal (`minimal_bottomRange_of_nonpos_end_one`). Cross-free with `end.offset ≥ 2` the empty result is compatible exactly with the empty database, so every key is necessary and `[⊥, ⊤)` is minimal (`minimal_fullRange_of_nonpos_overshoot`). -/ namespace FormalConflictRange open RangeRead /-- **The nonpositive-begin empty criterion, in pure `max` form.** With `begin.offset ≤ 0` the begin anchor `a = cnt begin.key + begin.offset - 1` is always below the length, so the clamped comparison collapses to `cnt end.key + end.offset - 1 ≤ max a 0`, with the empty database as the only extra boundary. -/ theorem compatible_empty_iff_nonpos {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) : RangeRead.Compatible db r { keys := [], more := false } ↔ db.keys = [] ∨ (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ max (((cnt db r.begin.key : ℤ) + r.begin.offset - 1)) 0 := by rw [compatible_empty_iff_clamp hbegin] constructor · rintro (hnil | hle) · exact Or.inl hnil · refine Or.inr ?_ have htoNat : ((((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat : ℤ)) = max ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) 0 := by rw [Int.toNat_eq_max] linarith [htoNat.symm ▸ hle] · rintro (hnil | hle) · exact Or.inl hnil · refine Or.inr ?_ have htoNat : ((((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat : ℤ)) = max ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) 0 := by rw [Int.toNat_eq_max] linarith [htoNat.symm ▸ hle] /-- **The nonpositive-begin empty criterion at end offset at most one.** When `end.offset ≤ 1` the end anchor is `cnt end.key + end.offset - 1 ≤ cnt end.key`, so the empty-database disjunct of `compatible_empty_iff_nonpos` is subsumed: an empty database gives `end.offset - 1 ≤ 0 ≤ max … 0`. Emptiness is therefore the single inequality `cnt end.key + end.offset - 1 ≤ max (cnt begin.key + begin.offset - 1) 0`. -/ theorem compatible_empty_iff_nonpos_endle {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) : RangeRead.Compatible db r { keys := [], more := false } ↔ (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ max (((cnt db r.begin.key : ℤ) + r.begin.offset - 1)) 0 := by rw [compatible_empty_iff_nonpos hbegin] refine ⟨fun h => h.elim (fun hnil => ?_) id, fun h => Or.inr h⟩ have hE : cnt db r.end_.key = 0 := by unfold cnt rw [hnil] simp have : (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ 0 := by omega exact le_trans this (le_max_right _ _) /-- **The cross-free nonpositive-end criterion, in slack form.** With `begin.offset ≤ 0` and `end.offset ≤ 0`, the empty result is compatible exactly when `cnt end.key - (1-end.offset) ≤ max (cnt begin.key - (1-begin.offset)) 0`. -/ theorem compatible_empty_iff_nonpos_counts {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) : RangeRead.Compatible db r { keys := [], more := false } ↔ (cnt db r.end_.key : ℤ) - ((1 - r.end_.offset).toNat : ℤ) ≤ max ((cnt db r.begin.key : ℤ) - ((1 - r.begin.offset).toNat : ℤ)) 0 := by rw [compatible_empty_iff_nonpos_endle (db := db) hbegin (by omega)] have hb : ((1 - r.begin.offset).toNat : ℤ) = 1 - r.begin.offset := Int.toNat_of_nonneg (by omega) have he : ((1 - r.end_.offset).toNat : ℤ) = 1 - r.end_.offset := Int.toNat_of_nonneg (by omega) rw [show (cnt db r.end_.key : ℤ) + r.end_.offset - 1 = (cnt db r.end_.key : ℤ) - ((1 - r.end_.offset).toNat : ℤ) by rw [he]; ring, show (cnt db r.begin.key : ℤ) + r.begin.offset - 1 = (cnt db r.begin.key : ℤ) - ((1 - r.begin.offset).toNat : ℤ) by rw [hb]; ring] /-- **The crossed short-gap arithmetic.** If `b ≤ 0` and `s < 1 - b`, then no `cE` satisfies `cE ≤ max (cE + s + b - 1) 0` except `cE = 0`: the max's first argument is strictly below `cE`, so it can only be the `0` branch, forcing `cE ≤ 0`. -/ theorem le_max_self_add_offset_iff (cE s : ℕ) (b : ℤ) (hb : b ≤ 0) (hst : s < (1 - b).toNat) : ((cE : ℤ) ≤ max ((cE : ℤ) + (s : ℤ) + b - 1) 0) ↔ cE = 0 := by have hsz : (s : ℤ) < 1 - b := by have h : (s : ℤ) < (((1 - b).toNat : ℕ) : ℤ) := by exact_mod_cast hst rwa [Int.toNat_of_nonneg (by omega : (0 : ℤ) ≤ 1 - b)] at h have hlt : (cE : ℤ) + (s : ℤ) + b - 1 < (cE : ℤ) := by omega constructor · intro h by_contra hne have hpos : (0 : ℤ) < (cE : ℤ) := by have : 0 < cE := Nat.pos_of_ne_zero hne exact_mod_cast this by_cases h0 : 0 ≤ (cE : ℤ) + (s : ℤ) + b - 1 · rw [max_eq_left h0] at h; omega · rw [max_eq_right (le_of_lt (not_le.mp h0))] at h; omega · intro h rw [h] simp only [Nat.cast_zero, zero_add] exact le_max_right _ _ /-- **The crossed `end.offset = 1` arithmetic, both gaps.** With `b ≤ 0`, the criterion `cE ≤ max (cE + s + b - 1) 0` is exactly "the gap `s` reaches `1 - b`, or `cE = 0`": if `cE > 0` the max's `0` branch cannot witness the inequality, so the first argument must be nonnegative and at least `cE`, i.e. `s ≥ 1 - b`. -/ theorem le_max_self_add_offset_iff' (cE s : ℕ) (b : ℤ) (hb : b ≤ 0) : ((cE : ℤ) ≤ max ((cE : ℤ) + (s : ℤ) + b - 1) 0) ↔ ((1 - b).toNat ≤ s ∨ cE = 0) := by have hbz : ((1 - b).toNat : ℤ) = 1 - b := Int.toNat_of_nonneg (by omega) constructor · intro h by_cases hc : cE = 0 · exact Or.inr hc · refine Or.inl ?_ have hpos : (0 : ℤ) < (cE : ℤ) := by have : 0 < cE := Nat.pos_of_ne_zero hc exact_mod_cast this have h0 : 0 ≤ (cE : ℤ) + (s : ℤ) + b - 1 := by by_contra hneg rw [max_eq_right (le_of_lt (not_le.mp hneg))] at h; omega rw [max_eq_left h0] at h have hs_ge : (((1 - b).toNat : ℤ)) ≤ (s : ℤ) := by rw [hbz]; omega exact_mod_cast hs_ge · rintro (hs | rfl) · have hs_ge : (1 : ℤ) - b ≤ (s : ℤ) := by have : ((1 - b).toNat : ℤ) ≤ (s : ℤ) := by exact_mod_cast hs rwa [hbz] at this have hge : (cE : ℤ) ≤ (cE : ℤ) + (s : ℤ) + b - 1 := by omega exact le_trans hge (le_max_left _ _) · simp only [Nat.cast_zero, zero_add] exact le_max_right _ _ /-- **The crossed, `end.offset = 1` emptiness criterion, both gaps.** With `begin.offset ≤ 0`, `end.offset = 1` and crossed selectors, the empty result is compatible exactly when the stored keys in `[end.key, begin.key)` reach `1 - begin.offset`, or no stored key lies below `end.key`. Both quantities are functions of the keys below `begin.key`, so `[⊥, begin.key)` is sound; the short gap is `compatible_empty_iff_nonpos_end_one_short`. -/ theorem compatible_empty_iff_nonpos_end_one_crossed {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) : RangeRead.Compatible db r { keys := [], more := false } ↔ ((1 - r.begin.offset).toNat ≤ (between db r.end_.key r.begin.key).length ∨ cnt db r.end_.key = 0) := by rw [compatible_empty_iff_nonpos_endle (db := db) hbegin (le_of_eq hend), hend] set cE := cnt db r.end_.key with hcE set s := (between db r.end_.key r.begin.key).length with hs have hsplit : cnt db r.begin.key = cE + s := by rw [hcE, hs]; exact cnt_eq_add_between db hEB rw [hsplit, Nat.cast_add] have := le_max_self_add_offset_iff' cE s r.begin.offset hbegin norm_num at this ⊢ exact this /-- **The crossed, `end.offset = 1` emptiness criterion, count form.** Rewriting `compatible_empty_iff_nonpos_end_one_crossed` through `cnt begin.key - cnt end.key = #keys in [end.key, begin.key)` expresses the criterion purely in the two counts below the selector keys, which agreement on `[⊥, begin.key)` fixes. -/ theorem compatible_empty_iff_nonpos_end_one_crossed' {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) : RangeRead.Compatible db r { keys := [], more := false } ↔ ((1 - r.begin.offset).toNat ≤ cnt db r.begin.key - cnt db r.end_.key ∨ cnt db r.end_.key = 0) := by rw [compatible_empty_iff_nonpos_end_one_crossed (db := db) hbegin hend hEB] have hlen : (between db r.end_.key r.begin.key).length = cnt db r.begin.key - cnt db r.end_.key := by have h := cnt_eq_add_between db hEB; omega rw [hlen] /-- **The crossed, `end.offset = 1` emptiness criterion, short gap.** With `begin.offset ≤ 0`, `end.offset = 1`, crossed selectors and a short gap `intervalCard end.key begin.key < 1 - begin.offset`, the begin anchor can never be lifted to zero, so the empty result is compatible exactly when no stored key lies below `end.key`. This is the exact minimality criterion in the crossed short gap: the coupled counts `cE` and `s` collapse to `cE = 0` because `[end.key, begin.key)` is too short to supply the `1 - begin.offset` keys the second disjunct needs. -/ theorem compatible_empty_iff_nonpos_end_one_short {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) (hgap : intervalCard r.end_.key r.begin.key < (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) : RangeRead.Compatible db r { keys := [], more := false } ↔ cnt db r.end_.key = 0 := by rw [compatible_empty_iff_nonpos_end_one_crossed (db := db) hbegin hend hEB] have hst : (between db r.end_.key r.begin.key).length < (1 - r.begin.offset).toNat := by have h1 := between_length_le_intervalCard (db := db) hEB have h2 : ((between db r.end_.key r.begin.key).length : ℕ∞) < (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) := lt_of_le_of_lt h1 hgap exact_mod_cast h2 exact ⟨fun h => h.elim (fun hc => absurd hc (not_le.mpr hst)) id, Or.inr⟩ /-- **Offset arithmetic for the nonpositive-end region.** With both offsets at most zero, `(end.offset - begin.offset).toNat` is the difference of the slack counts `(1 - begin.offset).toNat` and `(1 - end.offset).toNat`. -/ theorem offset_sub_toNat (oB oE : ℤ) (hB : oB ≤ 0) (hE : oE ≤ 0) : (oE - oB).toNat = (1 - oB).toNat - (1 - oE).toNat := by have hb : ((1 - oB).toNat : ℤ) = 1 - oB := Int.toNat_of_nonneg (by omega) have he : ((1 - oE).toNat : ℤ) = 1 - oE := Int.toNat_of_nonneg (by omega) have hd : (oE - oB) = ((1 - oB).toNat : ℤ) - ((1 - oE).toNat : ℤ) := by omega have h1 : (((1 - oB).toNat : ℤ)).toNat = (1 - oB).toNat := Int.toNat_natCast _ have h2 : (((1 - oE).toNat : ℤ)).toNat = (1 - oE).toNat := Int.toNat_natCast _ rw [hd, Int.toNat_sub'' (by positivity) (by positivity), h1, h2] /-- **Integer arithmetic of the crossed nonpositive-end short gap.** If `cE ≤ cB` and the gap `cB - cE` is below `b - e`, the inequality `cE - e ≤ max (cB - b) 0` is exactly `cE ≤ e`: a positive left side would force `cB - b ≥ cE - e`, contradicting the short gap. -/ theorem cross_short_arith (cB cE b e : ℕ) (hmono : cE ≤ cB) (hle : cB - cE < b - e) : ((cE : ℤ) - (e : ℤ) ≤ max ((cB : ℤ) - (b : ℤ)) 0 ↔ cE ≤ e) := by by_cases hbc : b ≤ cB · have hmax : (0 : ℤ) ≤ (cB : ℤ) - (b : ℤ) := by omega rw [max_eq_left hmax] constructor · intro h by_contra hce omega · intro hce; omega · have hmax : (cB : ℤ) - (b : ℤ) < 0 := by omega rw [max_eq_right (le_of_lt hmax)] constructor · intro h; omega · intro hce; omega /-- **Integer arithmetic of the crossed nonpositive-end long gap.** For crossed counts (`cE ≤ cB`), the inequality `cE - e ≤ max (cB - b) 0` is exactly "the end count fits below `e`, or the begin count reaches `b` and the gap `b - e` fits in `cB - cE`". -/ theorem cross_long_arith (cB cE b e : ℕ) (hmono : cE ≤ cB) : ((cE : ℤ) - (e : ℤ) ≤ max ((cB : ℤ) - (b : ℤ)) 0 ↔ (cE ≤ e ∨ (b ≤ cB ∧ b - e ≤ cB - cE))) := by by_cases hce : cE ≤ e · constructor · intro _; exact Or.inl hce · intro _; omega · push Not at hce by_cases hbc : b ≤ cB · by_cases heb : e ≤ b · rw [max_eq_left (by omega : (0 : ℤ) ≤ (cB : ℤ) - (b : ℤ))] constructor · intro h; exact Or.inr ⟨hbc, by omega⟩ · intro h have hsub1 : ((b - e : ℕ) : ℤ) = (b : ℤ) - (e : ℤ) := Nat.cast_sub heb have hsub2 : ((cB - cE : ℕ) : ℤ) = (cB : ℤ) - (cE : ℤ) := Nat.cast_sub hmono omega · push Not at heb have hsub0 : b - e = 0 := Nat.sub_eq_zero_of_le (le_of_lt heb) rw [max_eq_left (by omega : (0 : ℤ) ≤ (cB : ℤ) - (b : ℤ)), hsub0] constructor · intro _; exact Or.inr ⟨hbc, Nat.zero_le _⟩ · intro _; omega · push Not at hbc have hmax0 : max ((cB : ℤ) - (b : ℤ)) 0 = 0 := max_eq_right (by omega) rw [hmax0] constructor · intro h; exact absurd h (by omega) · rintro (h | ⟨hb, -⟩) · exact absurd h (not_le.mpr hce) · exact absurd hb (not_le.mpr hbc) /-- **The crossed nonpositive-end short-gap criterion.** With `begin.offset ≤ 0`, `end.offset ≤ 0`, crossed selectors (`end.key ≤ begin.key`) and a gap too short to lift the begin anchor (`intervalCard end.key begin.key < end.offset - begin.offset`), the empty result is compatible exactly when `cnt end.key ≤ 1 - end.offset`. The second disjunct of `compatible_empty_iff_nonpos_counts` can never fire, so the criterion collapses to the end-count bound. -/ theorem compatible_empty_iff_nonpos_crossed_short {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hcross : r.end_.key ≤ r.begin.key) (hgap : intervalCard r.end_.key r.begin.key < (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞)) : RangeRead.Compatible db r { keys := [], more := false } ↔ cnt db r.end_.key ≤ (1 - r.end_.offset).toNat := by rw [compatible_empty_iff_nonpos_counts (db := db) hbegin hend] have hlen : (between db r.end_.key r.begin.key).length = cnt db r.begin.key - cnt db r.end_.key := by have := cnt_eq_add_between db hcross; omega have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := cnt_le_cnt_of_le db hcross have hle : cnt db r.begin.key - cnt db r.end_.key < (1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat := by have hcard := between_length_le_intervalCard (db := db) hcross rw [hlen] at hcard have hoff : (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) = ((((1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat : ℕ)) : ℕ∞) := by rw [offset_sub_toNat r.begin.offset r.end_.offset hbegin hend] rw [hoff] at hgap exact_mod_cast lt_of_le_of_lt hcard hgap exact cross_short_arith _ _ _ _ hmono hle /-- **The crossed nonpositive-end long-gap criterion.** With `begin.offset ≤ 0`, `end.offset ≤ 0` and crossed selectors, the empty result is compatible exactly when `cnt end.key ≤ 1 - end.offset`, or `cnt begin.key ≥ 1 - begin.offset` and the gap `1 - begin.offset - (1 - end.offset)` is at most the stored keys in `[end.key, begin.key)`. -/ theorem compatible_empty_iff_nonpos_crossed_long {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hcross : r.end_.key ≤ r.begin.key) : RangeRead.Compatible db r { keys := [], more := false } ↔ (cnt db r.end_.key ≤ (1 - r.end_.offset).toNat ∨ ((1 - r.begin.offset).toNat ≤ cnt db r.begin.key ∧ (1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat ≤ (between db r.end_.key r.begin.key).length)) := by rw [compatible_empty_iff_nonpos_counts (db := db) hbegin hend] have hlen : (between db r.end_.key r.begin.key).length = cnt db r.begin.key - cnt db r.end_.key := by have := cnt_eq_add_between db hcross; omega have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := cnt_le_cnt_of_le db hcross rw [hlen] exact cross_long_arith _ _ _ _ hmono /-- The full range `[⊥, ⊤)` stores exactly the whole database. -/ theorem keys_bottom_top_eq (db : DB) : (⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ : ConflictRange).keys db = db.keys := by unfold ConflictRange.keys rw [List.filter_eq_self] intro k _ refine decide_eq_true ?_ exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), WithTop.coe_lt_top k⟩ /-- **The nonpositive-begin empty criterion at end offset at least two, cross-free.** With `begin.offset ≤ 0`, `end.offset ≥ 2` and `begin.key ≤ end.key`, the second disjunct of `compatible_empty_iff_nonpos` is unsatisfiable: the end anchor is `cE + oE - 1 ≥ cB + 1` while the begin side `max (cB + oB - 1) 0 ≤ cB`. Emptiness is therefore exactly the empty database. -/ theorem compatible_empty_iff_nonpos_overshoot {db : DB} {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (hle : r.begin.key ≤ r.end_.key) : RangeRead.Compatible db r { keys := [], more := false } ↔ db.keys = [] := by rw [compatible_empty_iff_nonpos hbegin] refine ⟨fun h => h.elim id (fun hcon => absurd hcon ?_), fun h => Or.inl h⟩ intro hcon have hcB_le : cnt db r.begin.key ≤ cnt db r.end_.key := cnt_le_cnt_of_le db hle have hmax : max ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) 0 ≤ (cnt db r.begin.key : ℤ) := max_le (by omega) (by positivity) have he : (cnt db r.begin.key : ℤ) + 1 ≤ (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by omega linarith /-- Below the empty key bound, a range's stored keys are exactly the stored keys strictly below `w`; its cardinality is therefore `cnt db w`. -/ theorem keys_bottom_eq {db : DB} {w : WithTop Key} : (⟨(([] : Key) : WithTop Key), w⟩ : ConflictRange).keys db = db.keys.filter (fun (k : Key) => decide ((k : WithTop Key) < w)) := by unfold ConflictRange.keys apply List.filter_congr intro k _ by_cases h : (k : WithTop Key) < w · simp [h, nil_le_key] · simp [h] /-- The count below `w` is the cardinality of the bottom range to `w`. -/ theorem cnt_eq_keys_bottom (db : DB) (w : WithTop Key) : cnt db w = ((⟨(([] : Key) : WithTop Key), w⟩ : ConflictRange).keys db).length := by rw [keys_bottom_eq, cnt, List.countP_eq_length_filter] /-- Agreement on the bottom range `[⊥, E)` fixes the count below `E`. -/ theorem cnt_eq_of_agree_bottom {db₁ db₂ : DB} {E : WithTop Key} (h : AgreeOn (⟨(([] : Key) : WithTop Key), E⟩ : ConflictRange) db₁ db₂) : cnt db₁ E = cnt db₂ E := by rw [cnt_eq_keys_bottom, cnt_eq_keys_bottom, h] /-- Agreement on the bottom range `[⊥, E)` fixes the count below `B` for any `B ≤ E`, since the keys below `B` are the keys below `E` that are below `B`. -/ theorem cnt_eq_of_agree_bottom_le {db₁ db₂ : DB} {B E : WithTop Key} (hBE : B ≤ E) (h : AgreeOn (⟨(([] : Key) : WithTop Key), E⟩ : ConflictRange) db₁ db₂) : cnt db₁ B = cnt db₂ B := by have hsub : ∀ db : DB, (⟨(([] : Key) : WithTop Key), B⟩ : ConflictRange).keys db = ((⟨(([] : Key) : WithTop Key), E⟩ : ConflictRange).keys db).filter (fun (k : Key) => decide ((k : WithTop Key) < B)) := by intro db rw [keys_bottom_eq, keys_bottom_eq, List.filter_filter] apply List.filter_congr intro k _ by_cases hkB : (k : WithTop Key) < B · have hkE : (k : WithTop Key) < E := lt_of_lt_of_le hkB hBE simp [hkB, hkE] · simp [hkB] have h₁ := cnt_eq_keys_bottom db₁ B have h₂ := cnt_eq_keys_bottom db₂ B rw [h₁, h₂, hsub db₁, hsub db₂, h] /-- **Soundness of the bottom range `[⊥, begin.key)` in the crossed long gap.** Agreement on `[⊥, begin.key)` fixes both counts `cnt begin.key` and `cnt end.key`, hence the count criterion of `compatible_empty_iff_nonpos_end_one_crossed'`, whatever the gap. -/ theorem sound_bottomRange_of_nonpos_end_one_crossed {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres intro db₁ db₂ hagree have hB : cnt db₁ r.begin.key = cnt db₂ r.begin.key := cnt_eq_of_agree_bottom hagree have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := cnt_eq_of_agree_bottom_le hEB hagree rw [compatible_empty_iff_nonpos_end_one_crossed' (db := db₁) hbegin hend hEB, compatible_empty_iff_nonpos_end_one_crossed' (db := db₂) hbegin hend hEB, hB, hE] /-- **Soundness of the bottom range `[⊥, end.key)` in the crossed short gap.** Agreement on `[⊥, end.key)` fixes `cnt end.key`, and the short-gap criterion (`compatible_empty_iff_nonpos_end_one_short`) says compatibility is exactly `cnt end.key = 0`. -/ theorem sound_bottomRange_of_nonpos_end_one_short {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) (hgap : intervalCard r.end_.key r.begin.key < (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres intro db₁ db₂ hagree have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := cnt_eq_of_agree_bottom hagree rw [compatible_empty_iff_nonpos_end_one_short (db := db₁) hbegin hend hEB hgap, compatible_empty_iff_nonpos_end_one_short (db := db₂) hbegin hend hEB hgap, hE] /-- **Soundness of the bottom range at a nonpositive begin offset, end offset at most one.** With `begin.offset ≤ 0` and `end.offset ≤ 1` (cross-free or crossed), a database is compatible with the empty result exactly when `cnt end.key + end.offset - 1 ≤ max (cnt begin.key + begin.offset - 1) 0` (`compatible_empty_iff_nonpos_endle`). When `end.offset ≤ 0` and the selectors are crossed the same criterion is expressed by `compatible_empty_iff_nonpos_crossed_long`/`_short`, both functions of `cnt end.key` and `cnt begin.key`. All are fixed by the stored keys below `max begin.key end.key`, so agreement on `⟨⊥, max begin.key end.key⟩` gives soundness. -/ theorem sound_bottomRange_of_nonpos_underEnd {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres intro db₁ db₂ hagree have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := cnt_eq_of_agree_bottom_le (le_max_right _ _) (E := max r.begin.key r.end_.key) hagree have hB : cnt db₁ r.begin.key = cnt db₂ r.begin.key := cnt_eq_of_agree_bottom_le (le_max_left _ _) (E := max r.begin.key r.end_.key) hagree by_cases hE0 : r.end_.offset ≤ 0 · by_cases hcross : r.end_.key ≤ r.begin.key · by_cases hgap : (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key · have hb' : (between db₁ r.end_.key r.begin.key).length = (between db₂ r.end_.key r.begin.key).length := by rw [← cnt_sub_eq_between_length hcross, ← cnt_sub_eq_between_length hcross, hB, hE] rw [compatible_empty_iff_nonpos_crossed_long (db := db₁) hbegin hE0 hcross, compatible_empty_iff_nonpos_crossed_long (db := db₂) hbegin hE0 hcross, hB, hE, hb'] · have hshort := not_le.mp hgap rw [compatible_empty_iff_nonpos_crossed_short (db := db₁) hbegin hE0 hcross hshort, compatible_empty_iff_nonpos_crossed_short (db := db₂) hbegin hE0 hcross hshort, hE] · rw [compatible_empty_iff_nonpos_endle (db := db₁) hbegin hend, compatible_empty_iff_nonpos_endle (db := db₂) hbegin hend, hE, hB] · rw [compatible_empty_iff_nonpos_endle (db := db₁) hbegin hend, compatible_empty_iff_nonpos_endle (db := db₂) hbegin hend, hE, hB] /-- **Soundness of the bottom range at a nonpositive begin offset, end offset at most one, cross-free.** The cross-free special case of `sound_bottomRange_of_nonpos_underEnd`, with candidate `[⊥, end.key)`. -/ theorem sound_bottomRange_of_nonpos_underEnd_le {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) (hle : r.begin.key ≤ r.end_.key) (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by have h : (⟨(([] : Key) : WithTop Key), max r.begin.key r.end_.key⟩ : ConflictRange) = ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by rw [max_eq_right hle] rw [← h] exact sound_bottomRange_of_nonpos_underEnd hbegin hend hmore hempty /-- **Soundness of the crossed nonpositive-end bottom range `[⊥, end.key)`.** With `begin.offset ≤ 0`, `end.offset ≤ 0`, crossed selectors and a short gap, compatibility is exactly `cnt end.key ≤ 1 - end.offset`, fixed by agreement on `[⊥, end.key)` (`compatible_empty_iff_nonpos_crossed_short`). -/ theorem sound_bottomRange_of_nonpos_crossed_short {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hcross : r.end_.key ≤ r.begin.key) (hgap : intervalCard r.end_.key r.begin.key < (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞)) (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres intro db₁ db₂ hagree have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := cnt_eq_of_agree_bottom hagree rw [compatible_empty_iff_nonpos_crossed_short (db := db₁) hbegin hend hcross hgap, compatible_empty_iff_nonpos_crossed_short (db := db₂) hbegin hend hcross hgap, hE] /-- **Soundness of the crossed nonpositive-end bottom range `[⊥, begin.key)`.** With `begin.offset ≤ 0`, `end.offset ≤ 0` and crossed selectors, compatibility is the long-gap criterion `compatible_empty_iff_nonpos_crossed_long`, a function of `cnt end.key` and `cnt begin.key` (via the gap length), both fixed by agreement on `[⊥, begin.key)`. -/ theorem sound_bottomRange_of_nonpos_crossed_long {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hcross : r.end_.key ≤ r.begin.key) (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres intro db₁ db₂ hagree have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := cnt_eq_of_agree_bottom_le hcross hagree have hB : cnt db₁ r.begin.key = cnt db₂ r.begin.key := cnt_eq_of_agree_bottom hagree have hlen : (between db₁ r.end_.key r.begin.key).length = (between db₂ r.end_.key r.begin.key).length := by rw [← cnt_sub_eq_between_length hcross, ← cnt_sub_eq_between_length hcross, hB, hE] rw [compatible_empty_iff_nonpos_crossed_long (db := db₁) hbegin hend hcross, compatible_empty_iff_nonpos_crossed_long (db := db₂) hbegin hend hcross, hB, hE, hlen] /-- The database holding exactly the key `k`. -/ def singleton (k : Key) : DB where keys := [k] sorted := by rw [List.sortedLT_cons] exact ⟨by simp, List.sortedLT_nil⟩ @[simp] theorem singleton_keys (k : Key) : (singleton k).keys = [k] := rfl /-- The anchor of a selector against the singleton `{k}`. -/ theorem anchor_singleton (k : Key) (s : Selector) : Selector.anchor (singleton k) s = (if (k : WithTop Key) < s.key then (1 : ℤ) else 0) + s.offset - 1 := by unfold Selector.anchor rw [singleton_keys, List.countP_cons, List.countP_nil] by_cases h : (k : WithTop Key) < s.key <;> simp [h] /-- **`end.key = ⊥` makes the crossed read trivially empty at a nonpositive begin.** If `end.key` is the empty key then `cnt db end.key = 0` for every database, so the emptiness criterion `s ≥ 1 - begin.offset ∨ cnt end.key = 0` holds for every database: the read is `AlwaysEmpty`. This is why a key below `end.key` exists in every non-trivially-empty crossed long-gap read, the hypothesis the gap witness needs. -/ theorem alwaysEmpty_of_end_key_eq_bot {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) (hbot : r.end_.key = ((([] : Key)) : WithTop Key)) : AlwaysEmpty r := by intro db rw [← compatible_empty_iff_complete_nil] exact (compatible_empty_iff_nonpos_end_one_crossed (db := db) hbegin hend hEB).mpr (Or.inr (by rw [hbot]; simp [cnt])) /-- **`AlwaysEmpty` when the whole bottom range fits below the end anchor.** With `begin.offset ≤ 0`, `end.offset ≤ 0` and `intervalCard ⊥ end.key ≤ 1 - end.offset`, every database has `cnt end.key ≤ 1 - end.offset`, so the end anchor `cnt end.key + end.offset - 1` is at most zero and the emptiness inequality holds against `max … 0`. This is the unconditional room fact behind the cross-free nonpositive-end residual: whenever the read is *not* always empty, the bottom range `[⊥, end.key)` must be strictly longer than `1 - end.offset`. -/ theorem alwaysEmpty_of_intervalCard_bot_le {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hcard : intervalCard ((([] : Key)) : WithTop Key) r.end_.key ≤ (((1 - r.end_.offset).toNat : ℕ) : ℕ∞)) : AlwaysEmpty r := by intro db rw [← compatible_empty_iff_complete_nil] rw [compatible_empty_iff_nonpos_endle (db := db) hbegin (by omega)] have hcE : cnt db r.end_.key ≤ (1 - r.end_.offset).toNat := by have h := cnt_le_intervalCard_nil db r.end_.key exact Nat.cast_le.mp (le_trans h hcard) have : (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ 0 := by omega exact le_trans this (le_max_right _ _) /-- Contrapositive of `alwaysEmpty_of_intervalCard_bot_le`: a non-trivially-empty cross-free read with both offsets nonpositive has `1 - end.offset + 1 ≤ intervalCard ⊥ end.key`. This is the room in `[⊥, end.key)` the cross-free witness spends. -/ theorem not_alwaysEmpty_intervalCard_bot {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hne : ¬ AlwaysEmpty r) : (((1 - r.end_.offset).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) r.end_.key := by have hnot : ¬ intervalCard ((([] : Key)) : WithTop Key) r.end_.key ≤ (((1 - r.end_.offset).toNat : ℕ) : ℕ∞) := fun h => hne (alwaysEmpty_of_intervalCard_bot_le hbegin hend h) rw [not_le] at hnot rw [show (((1 - r.end_.offset).toNat + 1 : ℕ) : ℕ∞) = (((1 - r.end_.offset).toNat : ℕ) : ℕ∞) + 1 by push_cast; ring] exact ENat.add_one_le_iff (by simp) |>.mpr hnot /-- **`AlwaysEmpty` when the begin slack is at most the end slack, crossed.** With both offsets nonpositive, crossed selectors and `(1-begin.offset) ≤ (1-end.offset)`, the empty result is compatible with every database: if `cnt end.key` fits below `1-end.offset` the end clamps, and otherwise the crossed monotonicity `cnt end.key ≤ cnt begin.key` plus `1-begin.offset ≤ 1-end.offset` satisfies the long-gap disjunct. Consequently a non-trivially-empty crossed read must have `1-begin.offset > 1-end.offset`. -/ theorem alwaysEmpty_of_slack_le_slack {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hcross : r.end_.key ≤ r.begin.key) (hed : (1 - r.begin.offset).toNat ≤ (1 - r.end_.offset).toNat) : AlwaysEmpty r := by intro db rw [← compatible_empty_iff_complete_nil] rw [compatible_empty_iff_nonpos_crossed_long (db := db) hbegin hend hcross] by_cases hce : cnt db r.end_.key ≤ (1 - r.end_.offset).toNat · exact Or.inl hce · refine Or.inr ⟨?_, ?_⟩ · have hmono := cnt_le_cnt_of_le db hcross omega · have hsub : (1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat = 0 := Nat.sub_eq_zero_of_le hed rw [hsub]; exact Nat.zero_le _ /-- **Gap room for the cross-free nonpositive-end residual.** With `B ≤ E`, `oB ≤ 0`, `oE ≤ 0` and `b ≤ e` (where `b = (1-oB).toNat`, `e = (1-oE).toNat`), a non-trivially-empty read has `e - b + 1 ≤ intervalCard B E`: the strict anchor comparison forces the `[B, E)` count `cE - cB` past `e - b`, and the difference cap bounds it by the interval. This is the room the cross-free witness spends in `[B, E)`. -/ theorem tsub_add_one_le_intervalCard_of_countFeasible_nonpos {B E : WithTop Key} {oB oE : ℤ} (hoB : oB ≤ 0) (hoE : oE ≤ 0) (hed : (1 - oB).toNat ≤ (1 - oE).toNat) (hfe : countFeasible B E oB oE) : (((1 - oE).toNat - (1 - oB).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard B E := by obtain ⟨cB, cE, n, -, -, hcBn, -, -, -, hgB, hgE, hmin⟩ := hfe set b : ℕ := (1 - oB).toNat with hb set e : ℕ := (1 - oE).toNat with he have hb1 : ((1 - oB).toNat : ℤ) = 1 - oB := Int.toNat_of_nonneg (by omega) have he1 : ((1 - oE).toNat : ℤ) = 1 - oE := Int.toNat_of_nonneg (by omega) have hbToNat : (((cB : ℤ) + oB - 1).toNat) = cB - b := by rw [show (cB : ℤ) + oB - 1 = (cB : ℤ) - (b : ℤ) by rw [hb, hb1]; ring, Int.toNat_sub] have heToNat : (((cE : ℤ) + oE - 1).toNat) = cE - e := by rw [show (cE : ℤ) + oE - 1 = (cE : ℤ) - (e : ℤ) by rw [he, he1]; ring, Int.toNat_sub] rw [hbToNat, heToNat] at hmin have hle1 : cB - b ≤ n := le_trans (Nat.sub_le _ _) hcBn have h1 : min (cB - b) n = cB - b := Nat.min_eq_left hle1 have hlt : cB - b < cE - e := by have h2 : cB - b < min (cE - e) n := by rw [← h1]; exact hmin exact lt_of_lt_of_le h2 (Nat.min_le_left _ _) have hbge : (b : ℤ) ≤ (e : ℤ) := by rw [hb, hb1, he, he1]; omega have hnat : e - b + 1 ≤ cE - cB := by have hcast : ((e - b + 1 : ℕ) : ℤ) ≤ ((cE - cB : ℕ) : ℤ) := by omega exact_mod_cast hcast exact le_trans (by exact_mod_cast hnat) hgE /-- The read-level form of `tsub_add_one_le_intervalCard_of_countFeasible_nonpos`: a non-trivially-empty cross-free read with both offsets nonpositive and `1-oB ≤ 1-oE` has the gap room `1-oE - (1-oB) + 1 ≤ intervalCard begin.key end.key`. Feasibility is `alwaysEmpty_iff_not_countFeasible`. -/ theorem tsub_add_one_le_intervalCard_of_not_alwaysEmpty_nonpos {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hed : (1 - r.begin.offset).toNat ≤ (1 - r.end_.offset).toNat) (hne : ¬ AlwaysEmpty r) : (((1 - r.end_.offset).toNat - (1 - r.begin.offset).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by have hfe : countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset := by by_contra hc exact hne ((alwaysEmpty_iff_not_countFeasible r).mpr hc) exact tsub_add_one_le_intervalCard_of_countFeasible_nonpos hbegin hend hed hfe /-- Erasing a stored key strictly below `w` lowers the count below `w` by one. -/ theorem cnt_erase_lt (db : DB) {k : Key} (hk : k ∈ db.keys) {w : WithTop Key} (hkw : (k : WithTop Key) < w) : cnt (db.erase k) w = cnt db w - 1 := by unfold cnt rw [DB.erase_keys, List.countP_erase, ite_eq_left ⟨hk, by simpa using hkw⟩] /-- Erasing a stored key not strictly below `w` leaves the count below `w` unchanged. -/ theorem cnt_erase_not_lt (db : DB) {k : Key} (_hk : k ∈ db.keys) {w : WithTop Key} (hkw : ¬ (k : WithTop Key) < w) : cnt (db.erase k) w = cnt db w := by unfold cnt rw [DB.erase_keys, List.countP_erase, ite_eq_right (by rintro ⟨-, hp⟩; exact hkw (of_decide_eq_true hp)), Nat.sub_zero] /-- The count below `w` in the singleton `{k}`. -/ theorem cnt_singleton (k : Key) (w : WithTop Key) : cnt (singleton k) w = if (k : WithTop Key) < w then 1 else 0 := by unfold cnt rw [singleton_keys, List.countP_cons, List.countP_nil] by_cases h : (k : WithTop Key) < w <;> simp [h] /-- A genuine key in `[B, E)` certifies the interval is nonempty. -/ theorem one_le_intervalCard_of_le_lt {B E : WithTop Key} {k : Key} (hkB : B ≤ (k : WithTop Key)) (hkE : (k : WithTop Key) < E) : (1 : ℕ∞) ≤ intervalCard B E := by rw [intervalCard_eq_encard_keysIn, Order.one_le_iff_ne_zero, Set.encard_ne_zero] exact ⟨k, by simp only [keysIn, Set.mem_ofPred_eq]; exact ⟨hkB, hkE⟩⟩ /-- **Realizing a count pair avoiding a key below `B`.** For `B ≤ E` and a key `k < B`, a database with `clo` keys in `[⊥, B)`, `chi - clo` keys in `[B, E)`, and no `k`, exists whenever `clo + 1` keys fit in `[⊥, B)` (so one can be spared for `k`) and `chi - clo` fit in `[B, E)`. Built by concatenating the two intervals. -/ theorem exists_db_below_gap_avoid_below {B E : WithTop Key} (hBE : B ≤ E) {k : Key} (hkB : (k : WithTop Key) < B) {clo chi : ℕ} (hcl : clo ≤ chi) (hlo : ((clo + 1 : ℕ) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) B) (hgap : (((chi - clo : ℕ)) : ℕ∞) ≤ intervalCard B E) : ∃ db : DB, k ∉ db.keys ∧ cnt db B = clo ∧ cnt db E = chi := by obtain ⟨dbA, hA, hkA, hlenA⟩ := exists_db_keysIn_avoid (B := ((([] : Key)) : WithTop Key)) (E := B) (k := k) (m := clo) hlo obtain ⟨dbC, hC, hlenC⟩ := exists_db_keysIn (B := B) (E := E) (m := chi - clo) hgap have hAlo : ∀ x ∈ dbA.keys, ((([] : Key)) : WithTop Key) ≤ (x : WithTop Key) := fun x hx => (hA x hx).1 have hAhi : ∀ x ∈ dbA.keys, (x : WithTop Key) < B := fun x hx => (hA x hx).2 have hClo : ∀ x ∈ dbC.keys, B ≤ (x : WithTop Key) := fun x hx => (hC x hx).1 have hChi : ∀ x ∈ dbC.keys, (x : WithTop Key) < E := fun x hx => (hC x hx).2 have hcross : ∀ x ∈ dbA.keys, ∀ y ∈ dbC.keys, x < y := by intro x hx y hy exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hAhi x hx) (hClo y hy)) let db : DB := ⟨dbA.keys ++ dbC.keys, List.sortedLT_append.mpr ⟨dbA.sorted, dbC.sorted, hcross⟩⟩ have hkeys : db.keys = dbA.keys ++ dbC.keys := rfl have hk : k ∉ db.keys := by rw [hkeys, List.mem_append]; rintro (h | h) · exact hkA h · exact absurd hkB (not_lt.mpr (hClo k h)) have hcntA_B : cnt dbA B = clo := by rw [cnt_eq_length_of_forall_lt dbA hAhi, hlenA] have hcntC_B : cnt dbC B = 0 := cnt_eq_zero_of_forall_ge dbC hClo have hcntA_E : cnt dbA E = clo := by rw [cnt_eq_length_of_forall_lt dbA (fun x hx => lt_of_lt_of_le (hAhi x hx) hBE), hlenA] have hcntC_E : cnt dbC E = chi - clo := by rw [cnt_eq_length_of_forall_lt dbC hChi, hlenC] refine ⟨db, hk, ?_, ?_⟩ · have : cnt db B = cnt dbA B + cnt dbC B := by unfold cnt; rw [hkeys, List.countP_append] rw [this, hcntA_B, hcntC_B, Nat.add_zero] · have : cnt db E = cnt dbA E + cnt dbC E := by unfold cnt; rw [hkeys, List.countP_append] rw [this, hcntA_E, hcntC_E]; omega /-- **Realizing a count pair avoiding a key in `[B, E)`.** For `B ≤ E` and a key `B ≤ k < E`, a database with `clo` keys in `[⊥, B)`, `chi - clo` keys in `[B, E)`, and no `k`, exists whenever `clo` fit in `[⊥, B)` and `chi - clo + 1` fit in `[B, E)`. -/ theorem exists_db_below_gap_avoid_gap {B E : WithTop Key} (hBE : B ≤ E) {k : Key} (hkB : B ≤ (k : WithTop Key)) (_hkE : (k : WithTop Key) < E) {clo chi : ℕ} (hcl : clo ≤ chi) (hlo : ((clo : ℕ) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) B) (hgap : (((chi - clo + 1 : ℕ)) : ℕ∞) ≤ intervalCard B E) : ∃ db : DB, k ∉ db.keys ∧ cnt db B = clo ∧ cnt db E = chi := by obtain ⟨dbA, hA, hlenA⟩ := exists_db_keysIn (B := ((([] : Key)) : WithTop Key)) (E := B) (m := clo) hlo obtain ⟨dbC, hC, hkC, hlenC⟩ := exists_db_keysIn_avoid (B := B) (E := E) (k := k) (m := chi - clo) hgap have hAlo : ∀ x ∈ dbA.keys, ((([] : Key)) : WithTop Key) ≤ (x : WithTop Key) := fun x hx => (hA x hx).1 have hAhi : ∀ x ∈ dbA.keys, (x : WithTop Key) < B := fun x hx => (hA x hx).2 have hClo : ∀ x ∈ dbC.keys, B ≤ (x : WithTop Key) := fun x hx => (hC x hx).1 have hChi : ∀ x ∈ dbC.keys, (x : WithTop Key) < E := fun x hx => (hC x hx).2 have hcross : ∀ x ∈ dbA.keys, ∀ y ∈ dbC.keys, x < y := by intro x hx y hy exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hAhi x hx) (hClo y hy)) let db : DB := ⟨dbA.keys ++ dbC.keys, List.sortedLT_append.mpr ⟨dbA.sorted, dbC.sorted, hcross⟩⟩ have hkeys : db.keys = dbA.keys ++ dbC.keys := rfl have hk : k ∉ db.keys := by rw [hkeys, List.mem_append]; rintro (h | h) · exact absurd hkB (not_le.mpr (hAhi k h)) · exact hkC h have hcntA_B : cnt dbA B = clo := by rw [cnt_eq_length_of_forall_lt dbA hAhi, hlenA] have hcntC_B : cnt dbC B = 0 := cnt_eq_zero_of_forall_ge dbC hClo have hcntA_E : cnt dbA E = clo := by rw [cnt_eq_length_of_forall_lt dbA (fun x hx => lt_of_lt_of_le (hAhi x hx) hBE), hlenA] have hcntC_E : cnt dbC E = chi - clo := by rw [cnt_eq_length_of_forall_lt dbC hChi, hlenC] refine ⟨db, hk, ?_, ?_⟩ · have : cnt db B = cnt dbA B + cnt dbC B := by unfold cnt; rw [hkeys, List.countP_append] rw [this, hcntA_B, hcntC_B, Nat.add_zero] · have : cnt db E = cnt dbA E + cnt dbC E := by unfold cnt; rw [hkeys, List.countP_append] rw [this, hcntA_E, hcntC_E]; omega /-- **Soundness of the full range at a nonpositive begin offset, end offset at least two, cross-free.** With `begin.offset ≤ 0`, `end.offset ≥ 2` and `begin.key ≤ end.key`, compatibility with the empty result is exactly emptiness of the database (`compatible_empty_iff_nonpos_overshoot`), a property of the whole database and hence fixed by agreement on `[⊥, ⊤)`. -/ theorem sound_fullRange_of_nonpos_overshoot {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (hle : r.begin.key ≤ r.end_.key) (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres intro db₁ db₂ hagree rw [compatible_empty_iff_nonpos_overshoot (db := db₁) hbegin hend hle, compatible_empty_iff_nonpos_overshoot (db := db₂) hbegin hend hle] have halign : db₁.keys = db₂.keys := by have h := hagree simp only [AgreeOn] at h rw [keys_bottom_top_eq db₁, keys_bottom_top_eq db₂] at h exact h rw [halign] /-- At `end.offset ≥ 2`, a nonpositive begin and cross-free selectors, no singleton is compatible with the empty result: every singleton database is nonempty. -/ theorem not_compatible_singleton_overshoot {r : RangeRead} {k : Key} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (hle : r.begin.key ≤ r.end_.key) : ¬ RangeRead.Compatible (singleton k) r { keys := [], more := false } := by rw [compatible_empty_iff_nonpos_overshoot (db := singleton k) hbegin hend hle] simp [singleton] /-- At `end.offset ≥ 2` and a nonpositive begin, no singleton is compatible with the empty result, whatever the order of the selector keys: in `{k}` the end anchor is at least one, so the resolved end is past `k`, while the begin anchor is at most `1 + begin.offset - 1 ≤ 0`, so the resolved begin is at or below `k`; `k` therefore lies in the complete keys and the result is nonempty. This is the crossed counterpart of `not_compatible_singleton_overshoot` and the key fact behind the full-range minimality at a nonpositive begin offset with an overshooting end. -/ theorem not_compatible_singleton_overshoot_all {r : RangeRead} {k : Key} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) : ¬ RangeRead.Compatible (singleton k) r { keys := [], more := false } := by rw [compatible_empty_iff_complete_nil] intro h have hmem : k ∈ RangeRead.completeKeys (singleton k) r := by rw [RangeRead.mem_completeKeys] refine ⟨by simp [singleton], ?_, ?_⟩ · -- `resolve begin ≤ k`: the begin anchor is at most zero. rw [resolve, anchor_singleton] by_cases hB : (k : WithTop Key) < r.begin.key · rw [ite_eq_left hB] by_cases han : (1 : ℤ) + r.begin.offset - 1 < 0 · rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) · rw [ite_eq_right han] have hz : (1 : ℤ) + r.begin.offset - 1 = 0 := by omega rw [hz]; simp · rw [ite_eq_right hB] have han : (0 : ℤ) + r.begin.offset - 1 < 0 := by omega rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) · -- `k < resolve end`: the end anchor is at least one, so `resolve` is past `k`. have hpos : (1 : ℤ) ≤ r.end_.anchor (singleton k) := by rw [anchor_singleton]; split <;> omega have h0 : 0 ≤ r.end_.anchor (singleton k) := by omega have hge : (singleton k).keys.length ≤ (r.end_.anchor (singleton k)).toNat := by have hle : (1 : ℤ) ≤ ((r.end_.anchor (singleton k)).toNat : ℤ) := by rw [Int.toNat_of_nonneg h0]; exact hpos rw [singleton_keys] exact_mod_cast hle rw [resolve_eq_top h0 hge] exact WithTop.coe_lt_top k rw [h] at hmem exact List.not_mem_nil hmem /-- **Necessity of every key, nonpositive begin, overshoot, any selector order.** With `begin.offset ≤ 0` and `end.offset ≥ 2`, every key is necessary whatever the order of the selector keys: the empty database is compatible, and toggling any key into it yields the singleton `{k}`, incompatible by `not_compatible_singleton_overshoot_all`. This is the crossed counterpart of `necessary_of_nonpos_overshoot`: weaselbot's singleton `{k}` is incompatible for every `k`, so the full range is minimal in the crossed case too. -/ theorem necessary_of_nonpos_overshoot_all {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (k : Key) : Necessary r { keys := [], more := false } k := by refine necessary_of_toggle ⟨emptyDB, compatible_emptyDB_emptyRes r, ?_⟩ have htoggle : toggle emptyDB k = singleton k := by unfold toggle rw [dite_eq_right (by simp [emptyDB])] rfl rw [htoggle] exact not_compatible_singleton_overshoot_all hbegin hend /-- **Soundness of the full range at a nonpositive begin offset, end offset at least two, any selector order.** Agreement on `[⊥, ⊤)` fixes the entire database, hence whether the empty result is compatible, for any order of the selector keys. -/ theorem sound_fullRange_of_nonpos_overshoot_all {r : RangeRead} {res : Result} (hmore : res.more = false) (hempty : res.keys = []) : Sound r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres exact sound_of_keys_eq (fun db => keys_bottom_top_eq db) /-- **Necessity of the full range's keys, nonpositive begin, overshoot, any order.** Every genuine key lies in `[⊥, ⊤)` and is necessary by `necessary_of_nonpos_overshoot_all`. -/ theorem necessaryRange_fullRange_of_nonpos_overshoot {r : RangeRead} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) : NecessaryRange r { keys := [], more := false } (([] : Key) : WithTop Key) (⊤ : WithTop Key) := fun k _ _ => necessary_of_nonpos_overshoot_all hbegin hend k /-- **The nonpositive-begin overshoot case, any selector order.** A read with `begin.offset ≤ 0`, `end.offset ≥ 2` and an empty complete result has `[⊥, ⊤)` as its minimal sound conflict range, cross-free or crossed. This closes the crossed half of the overshoot region that weaselbot flagged (his item 2). -/ theorem minimal_fullRange_of_nonpos_overshoot_all {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (hmore : res.more = false) (hempty : res.keys = []) : Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_fullRange_of_nonpos_overshoot_all rfl rfl, necessaryRange_fullRange_of_nonpos_overshoot hbegin hend⟩ /-- **The nonpositive-begin overshoot case, cross-free.** A read with `begin.offset ≤ 0`, `end.offset ≥ 2`, `begin.key ≤ end.key` and an empty complete result has `[⊥, ⊤)` as its minimal sound conflict range. -/ theorem minimal_fullRange_of_nonpos_overshoot {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (_hle : r.begin.key ≤ r.end_.key) (hmore : res.more = false) (hempty : res.keys = []) : Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := minimal_fullRange_of_nonpos_overshoot_all hbegin hend hmore hempty /-! ## The empty-database witness At `end.offset = 1` the empty database is compatible with the empty result, and inserting any key `k` below `end.key` makes a nonempty result: the singleton `{k}` resolves its end past itself and its begin at or below itself. So the factor-toggle lemma (`Reduction.necessary_of_toggle`) makes every such `k` necessary. -/ /-- At `end.offset = 1`, a key `k` below `end.key` lies in the complete keys of the singleton `{k}`: the end anchor is `1`, resolving past the only key to `⊤`, while the begin anchor is at most `0`, resolving at or below `k`. -/ theorem mem_completeKeys_singleton_self {r : RangeRead} {k : Key} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hk : (k : WithTop Key) < r.end_.key) : k ∈ RangeRead.completeKeys (singleton k) r := by rw [RangeRead.mem_completeKeys] refine ⟨by simp [singleton], ?_, ?_⟩ · -- `resolve begin ≤ k` rw [resolve, anchor_singleton] by_cases hB : (k : WithTop Key) < r.begin.key · rw [ite_eq_left hB] by_cases han : (1 : ℤ) + r.begin.offset - 1 < 0 · rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) · rw [ite_eq_right han] have hz : (1 : ℤ) + r.begin.offset - 1 = 0 := by omega rw [hz]; simp · rw [ite_eq_right hB] have han : (0 : ℤ) + r.begin.offset - 1 < 0 := by omega rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) · -- `k < resolve end` rw [resolve, anchor_singleton] rw [ite_eq_left hk] have hge : ¬ ((1 : ℤ) + r.end_.offset - 1 < 0) := by omega rw [ite_eq_right hge] have hidx : ((1 : ℤ) + r.end_.offset - 1).toNat = 1 := by omega rw [hidx]; simp /-- At `end.offset = 1`, the singleton `{k}` of a key below `end.key` is incompatible with the empty result: its complete keys are nonempty. -/ theorem not_compatible_singleton_self {r : RangeRead} {k : Key} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hk : (k : WithTop Key) < r.end_.key) : ¬ RangeRead.Compatible (singleton k) r { keys := [], more := false } := by rw [compatible_empty_iff_complete_nil] intro h have := mem_completeKeys_singleton_self (r := r) hbegin hend hk rw [h] at this exact List.not_mem_nil this /-- **Necessity below `end.key` at `end.offset = 1`, nonpositive begin.** Every stored key `k` with `k < end.key` is necessary for the empty result: the empty database is compatible, and toggling `k` into it yields the singleton `{k}`, whose complete keys are nonempty. -/ theorem necessary_of_nonpos_end_one {r : RangeRead} {k : Key} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hk : (k : WithTop Key) < r.end_.key) : Necessary r { keys := [], more := false } k := by refine necessary_of_toggle ⟨emptyDB, compatible_emptyDB_emptyRes r, ?_⟩ have htoggle : toggle emptyDB k = singleton k := by unfold toggle rw [dite_eq_right (by simp [emptyDB])] rfl rw [htoggle] exact not_compatible_singleton_self hbegin hend hk /-- **The nonpositive-begin `underEnd` case at `end.offset = 1`, cross-free.** A read with `begin.offset ≤ 0`, `end.offset = 1`, `begin.key ≤ end.key` and an empty complete result has `⟨⊥, end.key⟩` as its minimal sound conflict range. Soundness is `sound_bottomRange_of_nonpos_underEnd` (both counts fixed by the stored keys below `end.key`); necessity is `necessary_of_nonpos_end_one`, since the empty database and the singleton witnesses separate every key of the range. -/ theorem minimal_bottomRange_of_nonpos_end_one {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hle : r.begin.key ≤ r.end_.key) (hmore : res.more = false) (hempty : res.keys = []) : Minimal r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_bottomRange_of_nonpos_underEnd_le hbegin (le_of_eq hend) hle rfl rfl, ?_⟩ intro k _ hk exact necessary_of_nonpos_end_one hbegin hend hk /-- **The nonpositive-begin `end.offset = 1` crossed short-gap case.** A read with `begin.offset ≤ 0`, `end.offset = 1`, crossed selectors (`end.key ≤ begin.key`) and a gap too short to lift the begin anchor (`intervalCard end.key begin.key < 1 - begin.offset`) has `⟨⊥, end.key⟩` as its minimal sound conflict range. Soundness is `sound_bottomRange_of_nonpos_end_one_short`; necessity is `necessary_of_nonpos_end_one`, the same below-`end.key` witnesses as the cross-free case. Weaselbot's second counterexample is the `end.key = [0]`, `begin.key = [0,0]`, `begin.offset = -2` instance: the gap is `1 < 3`, and indeed `[⊥, [0])` is minimal. -/ theorem minimal_bottomRange_of_nonpos_end_one_short {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) (hgap : intervalCard r.end_.key r.begin.key < (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) (hmore : res.more = false) (hempty : res.keys = []) : Minimal r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by have hres : res = { keys := [], more := false } := by cases res with | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl subst hres refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_bottomRange_of_nonpos_end_one_short hbegin hend hEB hgap rfl rfl, ?_⟩ intro k _ hk exact necessary_of_nonpos_end_one hbegin hend hk end FormalConflictRange