- Vendor the FormalConflictRange reference library needed by the proof - Add Bridge.lean with simp lemmas connecting the Statement definitions to the reference library - Prove Statement.closedFormMinimal in Proof.lean - Register the new libraries in the Lake config
3205 lines
163 KiB
Lean4
3205 lines
163 KiB
Lean4
/-
|
||
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
|
||
|
||
/-!
|
||
# When is every result of a read empty?
|
||
|
||
A read is *trivially empty* when no database admits a non-empty compatible
|
||
result. This file characterizes that property purely in terms of the read's two
|
||
selector keys and offsets, with no mention of any database.
|
||
|
||
The bridge is `completeKeys_ne_nil_iff`: the complete keys of a read are nonempty
|
||
exactly when the begin selector's clamped anchor index is strictly below the end
|
||
selector's. Since a nonempty result always exists once the complete keys are
|
||
nonempty (`limit` is a positive natural, so a length-one prefix reporting `more`
|
||
is compatible), trivial emptiness is equivalent to
|
||
|
||
`∀ db, completeKeys db r = []`,
|
||
|
||
which is further equivalent to the arithmetic side condition `¬ countFeasible r`
|
||
developed below: `countFeasible` quantifies over the counts of stored keys below
|
||
the two selector keys and the database length, constrained only by the interval
|
||
cardinalities, and asserts that the clamped anchors can be ordered.
|
||
|
||
The two halves are `countFeasible_of_exists_db` (a witness database yields
|
||
feasible counts) and `countFeasible_complete` (feasible counts are realized by a
|
||
database), glued together by `alwaysEmpty_iff_not_countFeasible`.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
/-- The clamped anchor index of a selector against a database: the begin
|
||
selector of a read targets this index, clamped to the database length. It is the
|
||
`Nat`-valued counterpart of `Selector.anchor`, with the negative clamp to the
|
||
empty key and the past-the-end clamp to `⊤` made explicit by `min` and `toNat`. -/
|
||
def clampIndex (db : DB) (s : Selector) : ℕ :=
|
||
min (s.anchor db).toNat db.keys.length
|
||
|
||
/-- A stored key at index `j` lies strictly below the resolution of `s` exactly
|
||
when `j` is below the clamped anchor index. -/
|
||
theorem getElem_lt_resolve_iff {db : DB} {s : Selector} {j : ℕ}
|
||
(hj : j < db.keys.length) :
|
||
(↑(db.keys[j]) : WithTop Key) < resolve db s ↔ j < clampIndex db s := by
|
||
unfold clampIndex
|
||
by_cases hneg : s.anchor db < 0
|
||
· have h0 : (s.anchor db).toNat = 0 := by omega
|
||
rw [resolve_eq_nil hneg, h0, Nat.zero_min]
|
||
exact iff_of_false
|
||
(fun hc => List.not_lt_nil (db.keys[j]) (WithTop.coe_lt_coe.mp hc)) (by omega)
|
||
· have h0 : 0 ≤ s.anchor db := by omega
|
||
rcases lt_or_ge (s.anchor db).toNat db.keys.length with hlt | hge
|
||
· rw [resolve_eq_getElem h0 hlt, WithTop.coe_lt_coe, db.sorted.getElem_lt_getElem_iff,
|
||
Nat.min_eq_left (le_of_lt hlt)]
|
||
· rw [resolve_eq_top h0 hge, Nat.min_eq_right hge]
|
||
exact iff_of_true (WithTop.coe_lt_top _) hj
|
||
|
||
/-- The resolution of `s` lies at or below a stored key at index `j` exactly when
|
||
the clamped anchor index is at most `j`. -/
|
||
theorem resolve_le_getElem_iff {db : DB} {s : Selector} {j : ℕ}
|
||
(hj : j < db.keys.length) :
|
||
resolve db s ≤ (↑(db.keys[j]) : WithTop Key) ↔ clampIndex db s ≤ j := by
|
||
unfold clampIndex
|
||
by_cases hneg : s.anchor db < 0
|
||
· have h0 : (s.anchor db).toNat = 0 := by omega
|
||
rw [resolve_eq_nil hneg, h0, Nat.zero_min]
|
||
exact iff_of_true (WithTop.coe_le_coe.mpr (nil_le_key _)) (Nat.zero_le j)
|
||
· have h0 : 0 ≤ s.anchor db := by omega
|
||
rcases lt_or_ge (s.anchor db).toNat db.keys.length with hlt | hge
|
||
· rw [resolve_eq_getElem h0 hlt, WithTop.coe_le_coe, db.sorted.getElem_le_getElem_iff,
|
||
Nat.min_eq_left (le_of_lt hlt)]
|
||
· rw [resolve_eq_top h0 hge, Nat.min_eq_right hge]
|
||
exact iff_of_false (not_le.mpr (WithTop.coe_lt_top _)) (by omega)
|
||
|
||
/-- The number of stored keys strictly below a `WithTop Key` bound. This is the
|
||
count a selector's anchor is built from: nothing below the empty key, the whole
|
||
database below `⊤`, and the `countP (· < k)` below a genuine key. -/
|
||
def cnt (db : DB) (w : WithTop Key) : ℕ :=
|
||
db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < w))
|
||
|
||
/-- Nothing is stored below the empty key. -/
|
||
theorem cnt_nil (db : DB) :
|
||
cnt db (([] : Key) : WithTop Key) = 0 := countP_lt_nil
|
||
|
||
/-- Everything is stored below `⊤`. -/
|
||
theorem cnt_top (db : DB) : cnt db ⊤ = db.keys.length := countP_lt_top
|
||
|
||
/-- A count is bounded by the database length. -/
|
||
theorem cnt_le_length (db : DB) (w : WithTop Key) : cnt db w ≤ db.keys.length :=
|
||
List.countP_le_length
|
||
|
||
/-- The stored keys in the half-open `WithTop Key` interval `[w₁, w₂)`. -/
|
||
def between (db : DB) (w₁ w₂ : WithTop Key) : List Key :=
|
||
db.keys.filter (fun (k : Key) => decide (w₁ ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < w₂))
|
||
|
||
/-- The stored keys in `[w₁, w₂)`, as a set of genuine keys, lie in `keysIn w₁ w₂`. -/
|
||
theorem between_toFinset_subset_keysIn (db : DB) (w₁ w₂ : WithTop Key) :
|
||
((between db w₁ w₂).toFinset : Set Key) ⊆ keysIn w₁ w₂ := by
|
||
intro x hx
|
||
simp only [Finset.mem_coe, List.mem_toFinset, between, List.mem_filter,
|
||
decide_eq_true_eq] at hx
|
||
exact hx.2
|
||
|
||
/-- For a list, the keys below `w₂` split at `w₁ ≤ w₂` into the keys below `w₁`
|
||
and those in `[w₁, w₂)`. -/
|
||
theorem filter_lt_toFinset_union_top (l : List Key) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) :
|
||
(l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < w₂))).toFinset =
|
||
(l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < w₁))).toFinset ∪
|
||
(l.filter (fun (k : Key) =>
|
||
decide (w₁ ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < w₂))).toFinset := by
|
||
ext x
|
||
simp only [List.mem_toFinset, List.mem_filter, decide_eq_true_eq, Finset.mem_union]
|
||
constructor
|
||
· rintro ⟨hx, hxe⟩
|
||
by_cases hxb : (↑x : WithTop Key) < w₁
|
||
· exact Or.inl ⟨hx, hxb⟩
|
||
· exact Or.inr ⟨hx, le_of_not_gt hxb, hxe⟩
|
||
· rintro (⟨hx, hxb⟩ | ⟨hx, hxb, hxe⟩)
|
||
· exact ⟨hx, lt_of_lt_of_le hxb h⟩
|
||
· exact ⟨hx, hxe⟩
|
||
|
||
/-- The two pieces of the preceding split are disjoint. -/
|
||
theorem disjoint_filters_top (l : List Key) (w₁ w₂ : WithTop Key) :
|
||
Disjoint (l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < w₁))).toFinset
|
||
(l.filter (fun (k : Key) =>
|
||
decide (w₁ ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < w₂))).toFinset := by
|
||
rw [Finset.disjoint_left]
|
||
intro x hx1 hx2
|
||
simp only [List.mem_toFinset, List.mem_filter, decide_eq_true_eq] at hx1 hx2
|
||
exact absurd hx1.2 (not_lt.mpr hx2.2.1)
|
||
|
||
/-- Counting below `w₂` splits at `w₁ ≤ w₂` into the keys below `w₁` and those in
|
||
`[w₁, w₂)`. -/
|
||
theorem cnt_eq_add_between (db : DB) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) :
|
||
cnt db w₂ = cnt db w₁ + (between db w₁ w₂).length := by
|
||
simp only [cnt, between, List.countP_eq_length_filter]
|
||
rw [← List.toFinset_card_of_nodup (db.nodup.filter _),
|
||
← List.toFinset_card_of_nodup (db.nodup.filter _),
|
||
← List.toFinset_card_of_nodup (db.nodup.filter _),
|
||
filter_lt_toFinset_union_top db.keys h,
|
||
Finset.card_union_of_disjoint (disjoint_filters_top db.keys w₁ w₂)]
|
||
|
||
/-- The number of stored keys below a `WithTop Key` bound is at most the
|
||
cardinality of the interval from the empty key. -/
|
||
theorem cnt_le_intervalCard_nil (db : DB) (w : WithTop Key) :
|
||
(cnt db w : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) w := by
|
||
cases w with
|
||
| top =>
|
||
rw [cnt_top, intervalCard_top_right (by simp)]
|
||
exact le_top
|
||
| coe ko =>
|
||
have hnodup : (db.keys.filter (fun k => decide (k < ko))).Nodup := db.nodup.filter _
|
||
have hset : ((db.keys.filter (fun k => decide (k < ko))).toFinset : Set Key) ⊆
|
||
keysIn (↑([] : Key) : WithTop Key) (↑ko : WithTop Key) := by
|
||
intro x hx
|
||
simp only [Finset.mem_coe, List.mem_toFinset, List.mem_filter, decide_eq_true_eq] at hx
|
||
exact ⟨WithTop.coe_le_coe.mpr (nil_le_key x), WithTop.coe_lt_coe.mpr hx.2⟩
|
||
have h1 := Set.encard_le_encard hset
|
||
rw [Set.encard_coe_eq_coe_finsetCard, List.toFinset_card_of_nodup hnodup] at h1
|
||
have hcnt : cnt db (↑ko : WithTop Key) =
|
||
(db.keys.filter (fun k => decide (k < ko))).length := by
|
||
rw [cnt, List.countP_eq_length_filter]
|
||
congr 1
|
||
apply List.filter_congr
|
||
intro k _
|
||
simp [WithTop.coe_lt_coe]
|
||
rw [hcnt, intervalCard_eq_encard_keysIn]
|
||
exact h1
|
||
|
||
/-- The excess of the count below `w₂` over the count below `w₁ ≤ w₂` is the
|
||
number of stored keys in `[w₁, w₂)`, bounded by the interval's cardinality. -/
|
||
theorem cnt_sub_le_intervalCard (db : DB) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) :
|
||
((cnt db w₂ - cnt db w₁ : ℕ) : ℕ∞) ≤ intervalCard w₁ w₂ := by
|
||
have hnodup : (between db w₁ w₂).Nodup := by
|
||
unfold between
|
||
exact db.nodup.filter _
|
||
have h1 : ((between db w₁ w₂).length : ℕ∞) ≤ (keysIn w₁ w₂).encard := by
|
||
calc ((between db w₁ w₂).length : ℕ∞)
|
||
= ((between db w₁ w₂).toFinset.card : ℕ∞) := by
|
||
rw [List.toFinset_card_of_nodup hnodup]
|
||
_ = ((between db w₁ w₂).toFinset : Set Key).encard :=
|
||
(Set.encard_coe_eq_coe_finsetCard _).symm
|
||
_ ≤ (keysIn w₁ w₂).encard :=
|
||
Set.encard_le_encard (between_toFinset_subset_keysIn db w₁ w₂)
|
||
have h2 := cnt_eq_add_between db h
|
||
have hlen : (between db w₁ w₂).length = cnt db w₂ - cnt db w₁ := by omega
|
||
rw [intervalCard_eq_encard_keysIn, ← hlen]
|
||
exact h1
|
||
|
||
/-- A count is monotone in the bound: a higher bound counts weakly more keys. -/
|
||
theorem cnt_le_cnt_of_le (db : DB) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) :
|
||
cnt db w₁ ≤ cnt db w₂ := by
|
||
have := cnt_eq_add_between db h
|
||
omega
|
||
|
||
|
||
/-- Below a genuine key, a count is the `Key`-level `countP`. -/
|
||
theorem cnt_coe (db : DB) (k : Key) :
|
||
cnt db (↑k : WithTop Key) = db.keys.countP (fun x => decide (x < k)) :=
|
||
countP_lt_coe (l := db.keys) k
|
||
|
||
/-- If every stored key is at or above `w`, nothing is counted below `w`. -/
|
||
theorem cnt_eq_zero_of_forall_ge (db : DB) {w : WithTop Key}
|
||
(h : ∀ x ∈ db.keys, w ≤ (x : WithTop Key)) : cnt db w = 0 := by
|
||
unfold cnt
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (h x hx)
|
||
|
||
/-- If every stored key is strictly below `w`, all of them are counted below `w`. -/
|
||
theorem cnt_eq_length_of_forall_lt (db : DB) {w : WithTop Key}
|
||
(h : ∀ x ∈ db.keys, (x : WithTop Key) < w) : cnt db w = db.keys.length := by
|
||
unfold cnt
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact h x hx
|
||
|
||
/-- The clamped anchor index, in terms of a prescribed count below the selector
|
||
key. -/
|
||
theorem clampIndex_eq (db : DB) (β : WithTop Key) (o : ℤ) (c : ℕ)
|
||
(hc : cnt db β = c) :
|
||
clampIndex db ⟨β, o⟩ = min ((c : ℤ) + o - 1).toNat db.keys.length := by
|
||
unfold clampIndex
|
||
have h : Selector.anchor db (⟨β, o⟩ : Selector) = (cnt db β : ℤ) + o - 1 := by
|
||
simp only [Selector.anchor, cnt]
|
||
rw [h, hc]
|
||
|
||
/-- A selector at offset one clamps to at most the count of stored keys below its
|
||
own key: its anchor is that count exactly, and the clamp to the database length
|
||
only lowers it. -/
|
||
theorem clampIndex_le_cnt_of_offset_one (db : DB) (β : WithTop Key) :
|
||
clampIndex db (⟨β, 1⟩ : Selector) ≤ cnt db β := by
|
||
rw [clampIndex_eq db β 1 (cnt db β) rfl]
|
||
have h : (((cnt db β : ℤ) + 1 - 1).toNat) = cnt db β := by
|
||
rw [show ((cnt db β : ℤ) + 1 - 1) = (cnt db β : ℤ) by ring, Int.toNat_natCast]
|
||
rw [h]
|
||
exact Nat.min_le_left _ _
|
||
|
||
/-- The clamped anchor index of the begin selector at offset one: its anchor is
|
||
exactly the count of stored keys strictly below `begin.key`, clamped to the
|
||
database length. -/
|
||
theorem clampIndex_begin_eq {db : DB} {r : RangeRead} (h : r.begin.offset = 1) :
|
||
clampIndex db r.begin = min (cnt db r.begin.key) db.keys.length := by
|
||
unfold clampIndex
|
||
have han : r.begin.anchor db = (cnt db r.begin.key : ℤ) := by
|
||
unfold Selector.anchor cnt; rw [h]; ring
|
||
rw [han, Int.toNat_natCast]
|
||
|
||
/-- The clamped anchor index of the end selector: its anchor is the count of
|
||
stored keys strictly below `end_.key` plus `end_.offset - 1`, clamped to the
|
||
database length. -/
|
||
theorem clampIndex_end_eq {db : DB} {r : RangeRead} :
|
||
clampIndex db r.end_ =
|
||
min (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) db.keys.length := by
|
||
unfold clampIndex
|
||
have han : r.end_.anchor db = (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by
|
||
unfold Selector.anchor cnt; ring
|
||
rw [han]
|
||
|
||
/-- The keys of `[B, ↑e)` are contained in those of `[B, E)` whenever `↑e ≤ E`, so
|
||
the interval cardinality is monotone in the upper end. -/
|
||
theorem intervalCard_le_intervalCard_of_end_le {B : WithTop Key} {e : Key} {E : WithTop Key}
|
||
(h : (↑e : WithTop Key) ≤ E) :
|
||
intervalCard B (↑e) ≤ intervalCard B E := by
|
||
rw [intervalCard_eq_encard_keysIn, intervalCard_eq_encard_keysIn]
|
||
exact Set.encard_le_encard fun k hk => ⟨hk.1, lt_of_lt_of_le hk.2 h⟩
|
||
|
||
/-- Every database length is realizable. -/
|
||
theorem exists_db_length (n : ℕ) : ∃ db : DB, db.keys.length = n :=
|
||
⟨⟨(List.range n).map (chain ([] : Key)), sortedLT_map_chain _ _⟩, by simp⟩
|
||
|
||
/-- **Realizing `⊤` with a genuine end key.** A database of length `n` with
|
||
exactly `cE` stored keys below `e`, for any `cE` fitting below `e` and `cE ≤ n`.
|
||
The first `cE` successors of the empty key stay below `e`; the remainder are
|
||
successors of `e` itself, at or above it. -/
|
||
theorem exists_db_top_coe {e : Key} {cE n : ℕ}
|
||
(hcE : (cE : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑e : WithTop Key))
|
||
(hlen : cE ≤ n) :
|
||
∃ db : DB, db.keys.length = n ∧ cnt db (⊤ : WithTop Key) = n ∧ cnt db (↑e) = cE := by
|
||
have hchainnil : ∀ i < cE, chain ([] : Key) i < e := fun i hi =>
|
||
(chain_nil_lt_iff e i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hcE)
|
||
have hsorted : ((List.range cE).map (chain ([] : Key)) ++
|
||
(List.range (n - cE)).map (chain e)).SortedLT := by
|
||
rw [List.sortedLT_append]
|
||
refine ⟨sortedLT_map_chain _ _, sortedLT_map_chain _ _, ?_⟩
|
||
intro a ha b hb
|
||
rw [List.mem_map] at ha hb
|
||
obtain ⟨i, hi, rfl⟩ := ha
|
||
obtain ⟨j, -, rfl⟩ := hb
|
||
exact lt_of_lt_of_le (hchainnil i (List.mem_range.mp hi)) (chain_le e j)
|
||
refine ⟨⟨_, hsorted⟩, ?_, ?_, ?_⟩
|
||
· rw [List.length_append, List.length_map, List.length_map, List.length_range,
|
||
List.length_range]
|
||
omega
|
||
· rw [cnt_top, List.length_append, List.length_map, List.length_map, List.length_range,
|
||
List.length_range]
|
||
omega
|
||
· rw [cnt_coe, List.countP_append]
|
||
have h1 : ((List.range cE).map (chain ([] : Key))).countP (fun x => decide (x < e)) = cE := by
|
||
rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range]
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨i, hi, rfl⟩ := hx
|
||
exact hchainnil i (List.mem_range.mp hi)
|
||
have h2 : ((List.range (n - cE)).map (chain e)).countP (fun x => decide (x < e)) = 0 := by
|
||
rw [countP_lt_eq_zero_of_forall_ge]
|
||
intro x hx
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨j, -, rfl⟩ := hx
|
||
exact chain_le e j
|
||
rw [h1, h2, Nat.add_zero]
|
||
|
||
/-- **Realizing a genuine begin with `⊤`.** A database of length `n` with exactly
|
||
`cB` stored keys below `b`, for any `cB` fitting below `b` and `cB ≤ n`. -/
|
||
theorem exists_db_coe_top {b : Key} {cB n : ℕ}
|
||
(hcB : (cB : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑b : WithTop Key))
|
||
(hlen : cB ≤ n) :
|
||
∃ db : DB, db.keys.length = n ∧ cnt db (↑b) = cB ∧ cnt db (⊤ : WithTop Key) = n := by
|
||
have hchainnil : ∀ i < cB, chain ([] : Key) i < b := fun i hi =>
|
||
(chain_nil_lt_iff b i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hcB)
|
||
have hsorted : ((List.range cB).map (chain ([] : Key)) ++
|
||
(List.range (n - cB)).map (chain b)).SortedLT := by
|
||
rw [List.sortedLT_append]
|
||
refine ⟨sortedLT_map_chain _ _, sortedLT_map_chain _ _, ?_⟩
|
||
intro a ha b' hb'
|
||
rw [List.mem_map] at ha hb'
|
||
obtain ⟨i, hi, rfl⟩ := ha
|
||
obtain ⟨j, -, rfl⟩ := hb'
|
||
exact lt_of_lt_of_le (hchainnil i (List.mem_range.mp hi)) (chain_le b j)
|
||
refine ⟨⟨_, hsorted⟩, ?_, ?_, ?_⟩
|
||
· rw [List.length_append, List.length_map, List.length_map, List.length_range,
|
||
List.length_range]
|
||
omega
|
||
· rw [cnt_coe, List.countP_append]
|
||
have h1 : ((List.range cB).map (chain ([] : Key))).countP (fun x => decide (x < b)) = cB := by
|
||
rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range]
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨i, hi, rfl⟩ := hx
|
||
exact hchainnil i (List.mem_range.mp hi)
|
||
have h2 : ((List.range (n - cB)).map (chain b)).countP (fun x => decide (x < b)) = 0 := by
|
||
rw [countP_lt_eq_zero_of_forall_ge]
|
||
intro x hx
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨j, -, rfl⟩ := hx
|
||
exact chain_le b j
|
||
rw [h1, h2, Nat.add_zero]
|
||
· rw [cnt_top, List.length_append, List.length_map, List.length_map, List.length_range,
|
||
List.length_range]
|
||
omega
|
||
|
||
/-- **Realizing a pair of counts.** For genuine keys `lo ≤ hi`, a database of any
|
||
prescribed length `n ≥ chi` can be built with exactly `clo` stored keys below
|
||
`lo` and `chi` below `hi`, provided `clo` keys fit below `lo` and `chi - clo` fit
|
||
in `[lo, hi)`. The witness is three successor chains: `clo` keys below `lo`, then
|
||
`chi - clo` keys in `[lo, hi)`, then `n - chi` keys at or above `hi`. -/
|
||
theorem exists_db_counts_of_le {lo hi : Key} (h : lo ≤ hi) {clo chi n : ℕ}
|
||
(hcl : clo ≤ chi)
|
||
(hlo : (clo : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑lo : WithTop Key))
|
||
(hgap : ((chi - clo : ℕ) : ℕ∞) ≤ intervalCard (↑lo : WithTop Key) (↑hi : WithTop Key))
|
||
(hlen : chi ≤ n) :
|
||
∃ db : DB, db.keys.length = n ∧ cnt db (↑lo : WithTop Key) = clo ∧
|
||
cnt db (↑hi : WithTop Key) = chi := by
|
||
have hchainLo : ∀ i < clo, chain ([] : Key) i < lo := fun i hi =>
|
||
(chain_nil_lt_iff lo i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hlo)
|
||
have hchainGap : ∀ j < chi - clo, chain lo j < hi := fun j hj =>
|
||
(chain_lt_iff lo hi j).mpr (lt_of_lt_of_le (by exact_mod_cast hj) hgap)
|
||
let A : List Key := (List.range clo).map (chain ([] : Key))
|
||
let B : List Key := (List.range (chi - clo)).map (chain lo)
|
||
let C : List Key := (List.range (n - chi)).map (chain hi)
|
||
have hA : A.SortedLT := sortedLT_map_chain _ _
|
||
have hB : B.SortedLT := sortedLT_map_chain _ _
|
||
have hC : C.SortedLT := sortedLT_map_chain _ _
|
||
have hAB : ∀ a ∈ A, ∀ b ∈ B, a < b := by
|
||
intro a ha b hb
|
||
rw [List.mem_map] at ha hb
|
||
obtain ⟨i, hi, rfl⟩ := ha
|
||
obtain ⟨j, -, rfl⟩ := hb
|
||
exact lt_of_lt_of_le (hchainLo i (List.mem_range.mp hi)) (chain_le lo j)
|
||
have hAC : ∀ a ∈ A, ∀ c ∈ C, a < c := by
|
||
intro a ha c hc
|
||
rw [List.mem_map] at ha hc
|
||
obtain ⟨i, hiM, rfl⟩ := ha
|
||
obtain ⟨j, -, rfl⟩ := hc
|
||
exact lt_of_lt_of_le
|
||
(lt_of_lt_of_le (hchainLo i (List.mem_range.mp hiM)) h) (chain_le hi j)
|
||
have hBC : ∀ b ∈ B, ∀ c ∈ C, b < c := by
|
||
intro b hb c hc
|
||
rw [List.mem_map] at hb hc
|
||
obtain ⟨j, hj, rfl⟩ := hb
|
||
obtain ⟨k, -, rfl⟩ := hc
|
||
exact lt_of_lt_of_le (hchainGap j (List.mem_range.mp hj)) (chain_le hi k)
|
||
have hAB_sorted : (A ++ B).SortedLT := List.sortedLT_append.mpr ⟨hA, hB, hAB⟩
|
||
have hABC : (A ++ B ++ C).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hAB_sorted, hC, by
|
||
intro a ha c hc
|
||
rw [List.mem_append] at ha
|
||
rcases ha with ha | ha
|
||
· exact hAC a ha c hc
|
||
· exact hBC a ha c hc⟩
|
||
refine ⟨⟨A ++ B ++ C, hABC⟩, ?_, ?_, ?_⟩
|
||
· simp only [List.length_append, List.length_map, List.length_range, A, B, C]
|
||
omega
|
||
· have hcA : A.countP (fun x => decide (x < lo)) = clo := by
|
||
rw [countP_lt_eq_length_of_forall_lt (fun x hx => by
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨i, hi, rfl⟩ := hx
|
||
exact hchainLo i (List.mem_range.mp hi))]
|
||
simp [A]
|
||
have hcB : B.countP (fun x => decide (x < lo)) = 0 :=
|
||
countP_lt_eq_zero_of_forall_ge (fun x hx => by
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨j, -, rfl⟩ := hx
|
||
exact chain_le lo j)
|
||
have hcC : C.countP (fun x => decide (x < lo)) = 0 :=
|
||
countP_lt_eq_zero_of_forall_ge (fun x hx => by
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨k, -, rfl⟩ := hx
|
||
exact le_trans h (chain_le hi k))
|
||
rw [cnt_coe]
|
||
change (A ++ B ++ C).countP (fun x => decide (x < lo)) = clo
|
||
rw [List.countP_append, List.countP_append, hcA, hcB, hcC]
|
||
omega
|
||
· have hcA : A.countP (fun x => decide (x < hi)) = clo := by
|
||
rw [countP_lt_eq_length_of_forall_lt (fun x hx => by
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨i, hi, rfl⟩ := hx
|
||
exact lt_of_lt_of_le (hchainLo i (List.mem_range.mp hi)) h)]
|
||
simp [A]
|
||
have hcB : B.countP (fun x => decide (x < hi)) = chi - clo := by
|
||
rw [countP_lt_eq_length_of_forall_lt (fun x hx => by
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨j, hj, rfl⟩ := hx
|
||
exact hchainGap j (List.mem_range.mp hj))]
|
||
simp [B]
|
||
have hcC : C.countP (fun x => decide (x < hi)) = 0 :=
|
||
countP_lt_eq_zero_of_forall_ge (fun x hx => by
|
||
rw [List.mem_map] at hx
|
||
obtain ⟨k, -, rfl⟩ := hx
|
||
exact chain_le hi k)
|
||
rw [cnt_coe]
|
||
change (A ++ B ++ C).countP (fun x => decide (x < hi)) = chi
|
||
rw [List.countP_append, List.countP_append, hcA, hcB, hcC]
|
||
omega
|
||
|
||
/-- Cancelling a `+ 1`/`- 1` in `ℕ∞`. -/
|
||
private theorem enat_add_one_tsub_one (m : ℕ) : ((m : ℕ∞) + 1) - 1 = (m : ℕ∞) := by
|
||
rw [← ENat.natCast_one, ← ENat.natCast_add, ← ENat.natCast_sub]
|
||
simp
|
||
|
||
/-- From any `m + 1` elements of a set, `m` can be chosen avoiding a given member.
|
||
Take an `m + 1`-element subset; if it contains the member, dropping that member
|
||
leaves `m`, and if not, an `m`-subset of it avoids the member. -/
|
||
private theorem exists_subset_avoid_card {S : Set Key} {k : Key} {m : ℕ}
|
||
(hcard : (m + 1 : ℕ∞) ≤ S.encard) :
|
||
∃ t : Set Key, t ⊆ S ∧ k ∉ t ∧ t.encard = (m : ℕ∞) := by
|
||
obtain ⟨T, hTS, hTcard⟩ := Set.exists_subset_encard_eq hcard
|
||
by_cases hkT : k ∈ T
|
||
· refine ⟨T \ {k}, fun x hx => hTS hx.1, by simp, ?_⟩
|
||
rw [Set.encard_sdiff_singleton_of_mem hkT, hTcard, enat_add_one_tsub_one]
|
||
· obtain ⟨t, htT, htcard⟩ := Set.exists_subset_encard_eq (k := (m : ℕ∞))
|
||
(by rw [hTcard]; exact le_add_right le_rfl)
|
||
exact ⟨t, htT.trans hTS, fun hkt => hkT (htT hkt), htcard⟩
|
||
|
||
/-- A database of exactly `m` keys, all inside the half-open interval `keysIn B E`,
|
||
avoiding a chosen key `k` of that interval, exists whenever the interval holds at
|
||
least `m + 1` keys. The finite subset is listed in increasing order. -/
|
||
theorem exists_db_keysIn_avoid {B E : WithTop Key} {k : Key} {m : ℕ}
|
||
(hcard : (m + 1 : ℕ∞) ≤ intervalCard B E) :
|
||
∃ db : DB, (∀ x ∈ db.keys, x ∈ keysIn B E) ∧ k ∉ db.keys ∧
|
||
db.keys.length = m := by
|
||
rw [intervalCard_eq_encard_keysIn] at hcard
|
||
obtain ⟨t, htS, hkt, htcard⟩ := exists_subset_avoid_card hcard
|
||
have hfin : t.Finite := Set.finite_of_encard_eq_coe htcard
|
||
let s : Finset Key := hfin.toFinset
|
||
have hmem : ∀ x, x ∈ s ↔ x ∈ t := fun x => hfin.mem_toFinset
|
||
refine ⟨⟨s.sort (· ≤ ·), Finset.sortedLT_sort s⟩, ?_, ?_, ?_⟩
|
||
· intro x hx
|
||
exact htS ((hmem x).mp ((Finset.mem_sort (· ≤ ·)).mp hx))
|
||
· intro hk'
|
||
exact hkt ((hmem k).mp ((Finset.mem_sort (· ≤ ·)).mp hk'))
|
||
· rw [Finset.length_sort]
|
||
have h1 : (s.card : ℕ∞) = (m : ℕ∞) := by
|
||
rw [← Set.encard_coe_eq_coe_finsetCard, hfin.coe_toFinset, htcard]
|
||
exact Nat.cast_injective h1
|
||
|
||
/-- A database of exactly `m` keys, all inside the half-open interval `keysIn B E`,
|
||
exists whenever the interval holds at least `m` keys. The finite subset is listed in
|
||
increasing order. -/
|
||
theorem exists_db_keysIn {B E : WithTop Key} {m : ℕ}
|
||
(hcard : (m : ℕ∞) ≤ intervalCard B E) :
|
||
∃ db : DB, (∀ x ∈ db.keys, x ∈ keysIn B E) ∧ db.keys.length = m := by
|
||
rw [intervalCard_eq_encard_keysIn] at hcard
|
||
obtain ⟨t, htS, htcard⟩ := Set.exists_subset_encard_eq hcard
|
||
have hfin : t.Finite := Set.finite_of_encard_eq_coe htcard
|
||
let s : Finset Key := hfin.toFinset
|
||
have hmem : ∀ x, x ∈ s ↔ x ∈ t := fun x => hfin.mem_toFinset
|
||
refine ⟨⟨s.sort (· ≤ ·), Finset.sortedLT_sort s⟩, ?_, ?_⟩
|
||
· intro x hx
|
||
exact htS ((hmem x).mp ((Finset.mem_sort (· ≤ ·)).mp hx))
|
||
· rw [Finset.length_sort]
|
||
have h1 : (s.card : ℕ∞) = (m : ℕ∞) := by
|
||
rw [← Set.encard_coe_eq_coe_finsetCard, hfin.coe_toFinset, htcard]
|
||
exact Nat.cast_injective h1
|
||
|
||
/-- The count-level feasibility system for a read: there are counts `cB, cE` of
|
||
stored keys below the two selector keys and a database length `n` satisfying the
|
||
order/interval constraints, the edge pins (`⊤` counts everything, the empty key
|
||
counts nothing) and the strict anchor comparison. -/
|
||
def countFeasible (β ε : WithTop Key) (oB oE : ℤ) : Prop :=
|
||
∃ cB cE n : ℕ,
|
||
(β = ⊤ → cB = n) ∧ (ε = ⊤ → cE = n) ∧
|
||
cB ≤ n ∧ cE ≤ n ∧
|
||
(cB : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) β ∧
|
||
(cE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε ∧
|
||
((cB - cE : ℕ) : ℕ∞) ≤ intervalCard ε β ∧
|
||
((cE - cB : ℕ) : ℕ∞) ≤ intervalCard β ε ∧
|
||
min (((cB : ℤ) + oB - 1).toNat) n < min (((cE : ℤ) + oE - 1).toNat) n
|
||
|
||
/-- **Soundness of the count system.** Every database whose begin anchor index
|
||
falls below its end anchor index yields counts satisfying `countFeasible`. -/
|
||
theorem countFeasible_of_exists_db {β ε : WithTop Key} {oB oE : ℤ}
|
||
(h : ∃ db : DB, clampIndex db ⟨β, oB⟩ < clampIndex db ⟨ε, oE⟩) :
|
||
countFeasible β ε oB oE := by
|
||
obtain ⟨db, hlt⟩ := h
|
||
refine ⟨cnt db β, cnt db ε, db.keys.length, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
|
||
· intro htop; rw [htop, cnt_top]
|
||
· intro htop; rw [htop, cnt_top]
|
||
· exact cnt_le_length db β
|
||
· exact cnt_le_length db ε
|
||
· exact cnt_le_intervalCard_nil db β
|
||
· exact cnt_le_intervalCard_nil db ε
|
||
· rcases le_total ε β with hle | hle
|
||
· exact cnt_sub_le_intervalCard db hle
|
||
· have hmono := cnt_le_cnt_of_le db hle
|
||
rw [Nat.sub_eq_zero_of_le hmono]
|
||
simp
|
||
· rcases le_total β ε with hle | hle
|
||
· exact cnt_sub_le_intervalCard db hle
|
||
· have hmono := cnt_le_cnt_of_le db hle
|
||
rw [Nat.sub_eq_zero_of_le hmono]
|
||
simp
|
||
· have h1 : Selector.anchor db (⟨β, oB⟩ : Selector) = (cnt db β : ℤ) + oB - 1 := by
|
||
simp only [Selector.anchor, cnt]
|
||
have h2 : Selector.anchor db (⟨ε, oE⟩ : Selector) = (cnt db ε : ℤ) + oE - 1 := by
|
||
simp only [Selector.anchor, cnt]
|
||
change min ((Selector.anchor db ⟨β, oB⟩).toNat) db.keys.length <
|
||
min ((Selector.anchor db ⟨ε, oE⟩).toNat) db.keys.length
|
||
rw [h1, h2]
|
||
exact hlt
|
||
|
||
/-- **Completeness of the count system.** Any feasible counts are realized by a
|
||
database whose begin anchor index falls below its end anchor index. -/
|
||
theorem countFeasible_complete {β ε : WithTop Key} {oB oE : ℤ}
|
||
(hf : countFeasible β ε oB oE) :
|
||
∃ db : DB, clampIndex db ⟨β, oB⟩ < clampIndex db ⟨ε, oE⟩ := by
|
||
obtain ⟨cB, cE, n, hbt, hct, hBn, hEn, hBint, hEint, hgB, hgE, hmin⟩ := hf
|
||
cases β with
|
||
| top =>
|
||
cases ε with
|
||
| top =>
|
||
obtain ⟨db, hlen⟩ := exists_db_length n
|
||
have hB : cnt db (⊤ : WithTop Key) = cB := by
|
||
rw [cnt_top, hlen, ← hbt rfl]
|
||
have hE : cnt db (⊤ : WithTop Key) = cE := by
|
||
rw [cnt_top, hlen, ← hct rfl]
|
||
refine ⟨db, ?_⟩
|
||
rw [clampIndex_eq db ⊤ oB cB hB, clampIndex_eq db ⊤ oE cE hE, hlen]
|
||
exact hmin
|
||
| coe e =>
|
||
have hcE : (cE : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑e : WithTop Key) := hEint
|
||
obtain ⟨db, hlen, htop, hcoe⟩ := exists_db_top_coe (e := e) hcE hEn
|
||
have hB : cnt db (⊤ : WithTop Key) = cB := by rw [htop, ← hbt rfl]
|
||
refine ⟨db, ?_⟩
|
||
rw [clampIndex_eq db ⊤ oB cB hB, clampIndex_eq db (↑e) oE cE hcoe, hlen]
|
||
exact hmin
|
||
| coe b =>
|
||
cases ε with
|
||
| top =>
|
||
have hcB : (cB : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑b : WithTop Key) := hBint
|
||
obtain ⟨db, hlen, hcoe, htop⟩ := exists_db_coe_top (b := b) hcB hBn
|
||
have hE : cnt db (⊤ : WithTop Key) = cE := by rw [htop, ← hct rfl]
|
||
refine ⟨db, ?_⟩
|
||
rw [clampIndex_eq db (↑b) oB cB hcoe, clampIndex_eq db ⊤ oE cE hE, hlen]
|
||
exact hmin
|
||
| coe e =>
|
||
rcases le_total b e with hbe | hbe
|
||
· have hcl : cB ≤ cE := by
|
||
by_contra hcon
|
||
have hzero : intervalCard (↑e : WithTop Key) (↑b : WithTop Key) = 0 :=
|
||
intervalCard_eq_zero_of_le (WithTop.coe_le_coe.mpr hbe)
|
||
have hgg := hgB
|
||
rw [hzero] at hgg
|
||
have : cB - cE = 0 := by
|
||
by_contra hne
|
||
have hpos : 0 < cB - cE := Nat.pos_of_ne_zero hne
|
||
exact absurd hgg (not_le.mpr (by
|
||
exact_mod_cast hpos : (0 : ℕ∞) < (↑(cB - cE) : ℕ∞)))
|
||
omega
|
||
obtain ⟨db, hlen, hlo, hhi⟩ := exists_db_counts_of_le hbe hcl hBint hgE hEn
|
||
refine ⟨db, ?_⟩
|
||
rw [clampIndex_eq db (↑b) oB cB hlo, clampIndex_eq db (↑e) oE cE hhi, hlen]
|
||
exact hmin
|
||
· have hcl : cE ≤ cB := by
|
||
by_contra hcon
|
||
have hzero : intervalCard (↑b : WithTop Key) (↑e : WithTop Key) = 0 :=
|
||
intervalCard_eq_zero_of_le (WithTop.coe_le_coe.mpr hbe)
|
||
have hgg := hgE
|
||
rw [hzero] at hgg
|
||
have : cE - cB = 0 := by
|
||
by_contra hne
|
||
have hpos : 0 < cE - cB := Nat.pos_of_ne_zero hne
|
||
exact absurd hgg (not_le.mpr (by
|
||
exact_mod_cast hpos : (0 : ℕ∞) < (↑(cE - cB) : ℕ∞)))
|
||
omega
|
||
obtain ⟨db, hlen, hlo, hhi⟩ := exists_db_counts_of_le hbe hcl hEint hgB hBn
|
||
refine ⟨db, ?_⟩
|
||
rw [clampIndex_eq db (↑b) oB cB hhi, clampIndex_eq db (↑e) oE cE hlo, hlen]
|
||
exact hmin
|
||
|
||
/-- A read's complete keys are nonempty exactly when the begin selector's clamped
|
||
anchor index is strictly below the end selector's. -/
|
||
theorem completeKeys_ne_nil_iff {db : DB} {r : RangeRead} :
|
||
RangeRead.completeKeys db r ≠ [] ↔ clampIndex db r.begin < clampIndex db r.end_ := by
|
||
constructor
|
||
· intro hne
|
||
obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne
|
||
obtain ⟨hkm, hkb, hke⟩ := RangeRead.mem_completeKeys.mp hk
|
||
obtain ⟨j, hj, rfl⟩ := List.mem_iff_getElem.mp hkm
|
||
have hbj : clampIndex db r.begin ≤ j := (resolve_le_getElem_iff hj).mp hkb
|
||
have hje : j < clampIndex db r.end_ := (getElem_lt_resolve_iff hj).mp hke
|
||
omega
|
||
· intro hlt
|
||
have hB : clampIndex db r.begin < db.keys.length := by
|
||
unfold clampIndex at hlt ⊢
|
||
omega
|
||
have hk : db.keys[clampIndex db r.begin] ∈ RangeRead.completeKeys db r :=
|
||
RangeRead.mem_completeKeys.mpr
|
||
⟨List.getElem_mem hB, (resolve_le_getElem_iff hB).mpr (le_refl _),
|
||
(getElem_lt_resolve_iff hB).mpr hlt⟩
|
||
intro hempty
|
||
rw [hempty] at hk
|
||
exact List.not_mem_nil hk
|
||
|
||
/-- A read is trivially empty when every database yields no complete keys. This is
|
||
the database-free form of `NoNonemptyResult`: by `alwaysEmpty_iff_noNonemptyResult`
|
||
the two coincide because the limit is a positive natural. -/
|
||
def AlwaysEmpty (r : RangeRead) : Prop := ∀ db : DB, RangeRead.completeKeys db r = []
|
||
|
||
/-- A read admits no non-empty compatible result: for every database and every
|
||
result compatible with the read, the result reports no keys. This is the property
|
||
the module docstring names "trivially empty", stated directly against
|
||
`RangeRead.Compatible` rather than through the complete keys. -/
|
||
def NoNonemptyResult (r : RangeRead) : Prop :=
|
||
∀ (db : DB) (res : Result), RangeRead.Compatible db r res → res.keys = []
|
||
|
||
/-- `AlwaysEmpty` is exactly the faithful, compatibility-level form
|
||
`NoNonemptyResult`: because `limit` is a positive natural, a nonempty complete-keys
|
||
list always yields a compatible nonempty result — its one-element prefix reporting
|
||
`more` — so no database admits a nonempty compatible result precisely when every
|
||
database's complete keys are empty. -/
|
||
theorem alwaysEmpty_iff_noNonemptyResult (r : RangeRead) :
|
||
AlwaysEmpty r ↔ NoNonemptyResult r := by
|
||
constructor
|
||
· intro h db res hc
|
||
unfold RangeRead.Compatible at hc
|
||
rw [h db] at hc
|
||
rcases hc.2 with ⟨_, hpre, hne⟩ | ⟨_, heq⟩
|
||
· exact absurd (List.eq_nil_of_prefix_nil hpre) hne
|
||
· exact heq
|
||
· intro h db
|
||
by_contra hne
|
||
have htake : (RangeRead.completeKeys db r).take 1 ≠ [] := by
|
||
intro h0
|
||
have hlen := congrArg List.length h0
|
||
rw [List.length_take, List.length_nil] at hlen
|
||
have hpos := List.length_pos_of_ne_nil hne
|
||
omega
|
||
have hcomp : RangeRead.Compatible db r
|
||
{ keys := (RangeRead.completeKeys db r).take 1, more := true } := by
|
||
refine ⟨?_, Or.inl ⟨rfl, List.take_prefix 1 _, htake⟩⟩
|
||
unfold RangeRead.withinLimit
|
||
cases hlim : r.limit with
|
||
| none => trivial
|
||
| some n =>
|
||
rw [List.length_take]
|
||
exact le_trans (Nat.min_le_left 1 _) (Nat.succ_le_of_lt n.pos)
|
||
exact htake (h db _ hcomp)
|
||
|
||
/-- **The characterization.** A read is trivially empty exactly when the count
|
||
system is infeasible: no lengths and stored-key counts below the selector keys
|
||
can make the begin anchor fall strictly below the end anchor. This is a
|
||
decidable-in-principle condition on the read alone. -/
|
||
theorem alwaysEmpty_iff_not_countFeasible (r : RangeRead) :
|
||
AlwaysEmpty r ↔
|
||
¬ countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset := by
|
||
constructor
|
||
· intro h hf
|
||
obtain ⟨db, hlt⟩ := countFeasible_complete hf
|
||
exact ((completeKeys_ne_nil_iff (db := db) (r := r)).mpr hlt) (h db)
|
||
· intro hf db
|
||
by_contra hne
|
||
exact hf (countFeasible_of_exists_db
|
||
⟨db, (completeKeys_ne_nil_iff (db := db) (r := r)).mp hne⟩)
|
||
|
||
/-- **Room below the end key.** A feasible count system with `begin.offset = 1`
|
||
and `end.offset ≤ 1` forces the count difference `cE - cB` to be at least `2 - oE`,
|
||
and the difference cap bounds it by the canonical interval `[B, E)`. The end anchor
|
||
must stay strictly above the begin anchor, and `oE ≤ 1` keeps that excess within
|
||
the `B`-to-`E` gap, so the begin count can never equal the end count. -/
|
||
theorem two_sub_oE_le_intervalCard_of_countFeasible {B E : WithTop Key} {oE : ℤ}
|
||
(hoE : oE ≤ 1) (hfe : countFeasible B E 1 oE) :
|
||
((2 - oE).toNat : ℕ∞) ≤ intervalCard B E := by
|
||
obtain ⟨cB, cE, n, -, -, -, -, -, -, hgB, hgE, hmin⟩ := hfe
|
||
have hA : (((cB : ℤ) + 1 - 1).toNat) = cB := by
|
||
rw [show (cB : ℤ) + 1 - 1 = (cB : ℤ) by ring, Int.toNat_natCast]
|
||
rw [hA] at hmin
|
||
have hmin1 : cB < (((cE : ℤ) + oE - 1).toNat) := by omega
|
||
have hInt : (cB : ℤ) < (cE : ℤ) + oE - 1 := by
|
||
by_cases h : (cE : ℤ) + oE - 1 ≤ 0
|
||
· have hz : (((cE : ℤ) + oE - 1).toNat) = 0 := Int.toNat_of_nonpos h
|
||
rw [hz] at hmin1; omega
|
||
· have hz : ((((cE : ℤ) + oE - 1).toNat : ℕ) : ℤ) = (cE : ℤ) + oE - 1 :=
|
||
Int.toNat_of_nonneg (le_of_not_ge h)
|
||
omega
|
||
have hcBcE : cB ≤ cE := by omega
|
||
have hle2 : (2 - oE).toNat ≤ cE - cB := by
|
||
have h2 : (((2 - oE).toNat : ℕ) : ℤ) = 2 - oE := Int.toNat_of_nonneg (by omega)
|
||
have h3 : ((cE - cB : ℕ) : ℤ) = (cE : ℤ) - cB := Nat.cast_sub hcBcE
|
||
omega
|
||
exact le_trans (by exact_mod_cast hle2) hgE
|
||
|
||
/-- The canonical interval `[begin.key, end.key)` of a non-trivially-empty
|
||
begin-offset-one read whose end offset is at most one holds at least `2 - oE` keys.
|
||
The count system is feasible by `alwaysEmpty_iff_not_countFeasible`, and
|
||
`two_sub_oE_le_intervalCard_of_countFeasible` transfers the room to the canonical
|
||
interval. -/
|
||
theorem two_sub_oE_le_intervalCard_canonical {r : RangeRead}
|
||
(hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) (hne : ¬ AlwaysEmpty r) :
|
||
((2 - r.end_.offset).toNat : ℕ∞) ≤ 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)
|
||
rw [hbegin] at hfe
|
||
exact two_sub_oE_le_intervalCard_of_countFeasible hend hfe
|
||
|
||
/-- **Room in `[B, E)` for the cross-free case.** A feasible count system with
|
||
`B ≤ E`, `1 ≤ oB`, `1 ≤ oE` and `e ≤ b` (where `b = (oB-1).toNat`,
|
||
`e = (oE-1).toNat`) has `b - e + 1 ≤ intervalCard B E`: the strict anchor comparison
|
||
forces the `[B, E)` count `c_E - c_B` to exceed `b - e`, so it reaches `b - e + 1`,
|
||
and the difference cap bounds it by the interval. This is the cross-free analogue of
|
||
`two_sub_oE_le_intervalCard_of_countFeasible`. -/
|
||
theorem tsub_add_one_le_intervalCard_of_countFeasible {B E : WithTop Key} {oB oE : ℤ}
|
||
(hBE : B ≤ E) (hoB : 1 ≤ oB) (hoE : 1 ≤ oE)
|
||
(hed : (oE - 1).toNat ≤ (oB - 1).toNat)
|
||
(hfe : countFeasible B E oB oE) :
|
||
(((oB - 1).toNat - (oE - 1).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard B E := by
|
||
obtain ⟨cB, cE, n, -, -, -, -, -, -, hgB, hgE, hmin⟩ := hfe
|
||
have hb : (((cB : ℤ) + oB - 1).toNat) = cB + (oB - 1).toNat := by
|
||
rw [show ((cB : ℤ) + oB - 1) = (cB : ℤ) + (oB - 1) by ring,
|
||
Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast]
|
||
have he : (((cE : ℤ) + oE - 1).toNat) = cE + (oE - 1).toNat := by
|
||
rw [show ((cE : ℤ) + oE - 1) = (cE : ℤ) + (oE - 1) by ring,
|
||
Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast]
|
||
rw [hb, he] at hmin
|
||
have hsub : (cB - cE : ℕ) = 0 := by
|
||
have h := hgB
|
||
rw [intervalCard_eq_zero_of_le hBE] at h
|
||
have h0 : ((cB - cE : ℕ) : ℕ∞) = ((0 : ℕ) : ℕ∞) := by simpa using le_antisymm h (by simp)
|
||
exact Nat.cast_injective h0
|
||
have hcBcE : cB ≤ cE := by omega
|
||
have hlt : cB + (oB - 1).toNat < cE + (oE - 1).toNat := by omega
|
||
have hle : (oB - 1).toNat - (oE - 1).toNat + 1 ≤ cE - cB := by omega
|
||
exact le_trans (by exact_mod_cast hle) hgE
|
||
|
||
/-- The canonical interval `[begin.key, end.key)` of a non-trivially-empty
|
||
cross-free read with `begin.offset ≥ 1`, `end.offset ≥ 2` and `e ≤ b` holds at
|
||
least `b - e + 1` keys, where `b = (begin.offset-1).toNat`, `e = (end.offset-1).toNat`.
|
||
The count system is feasible by `alwaysEmpty_iff_not_countFeasible`, and
|
||
`tsub_add_one_le_intervalCard_of_countFeasible` transfers the room. -/
|
||
theorem tsub_add_one_le_intervalCard_of_le_general {r : RangeRead}
|
||
(hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r)
|
||
(hed : (r.end_.offset - 1).toNat ≤ (r.begin.offset - 1).toNat) :
|
||
(((r.begin.offset - 1).toNat - (r.end_.offset - 1).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 hle hbegin (by omega) hed hfe
|
||
|
||
/-- **Room in `[B, E)` for the begin-positive `underEnd` case.** A feasible count
|
||
system with `B ≤ E`, `1 ≤ oB` and `oE ≤ 1` has
|
||
`(oB - 1).toNat + (1 - oE).toNat + 1 ≤ intervalCard B E`: the strict anchor comparison
|
||
forces the `[B, E)` count `cE - cB` to exceed `(oB - 1).toNat + (1 - oE).toNat`, and the
|
||
difference cap bounds it by the interval. This is the off-one analogue of
|
||
`two_sub_oE_le_intervalCard_of_countFeasible`. -/
|
||
theorem add_add_one_le_intervalCard_of_countFeasible {B E : WithTop Key} {oB oE : ℤ}
|
||
(hBE : B ≤ E) (hoB : 1 ≤ oB) (hoE : oE ≤ 1)
|
||
(hfe : countFeasible B E oB oE) :
|
||
(((oB - 1).toNat + (1 - oE).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard B E := by
|
||
obtain ⟨cB, cE, n, -, -, -, -, -, -, hgB, hgE, hmin⟩ := hfe
|
||
have hb : (((cB : ℤ) + oB - 1).toNat) = cB + (oB - 1).toNat := by
|
||
rw [show ((cB : ℤ) + oB - 1) = (cB : ℤ) + (oB - 1) by ring,
|
||
Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast]
|
||
have he : (((cE : ℤ) + oE - 1).toNat) = cE - (1 - oE).toNat := by
|
||
have h1 : ((1 - oE).toNat : ℤ) = 1 - oE := Int.toNat_of_nonneg (by omega)
|
||
rw [show ((cE : ℤ) + oE - 1) = (cE : ℤ) - ((1 - oE).toNat : ℤ) by rw [h1]; ring,
|
||
Int.toNat_sub]
|
||
rw [hb, he] at hmin
|
||
have hsub : (cB - cE : ℕ) = 0 := by
|
||
have h := hgB
|
||
rw [intervalCard_eq_zero_of_le hBE] at h
|
||
have h0 : ((cB - cE : ℕ) : ℕ∞) = ((0 : ℕ) : ℕ∞) := by simpa using le_antisymm h (by simp)
|
||
exact Nat.cast_injective h0
|
||
have hcBcE : cB ≤ cE := by omega
|
||
have hlt : cB + (oB - 1).toNat < cE - (1 - oE).toNat := by omega
|
||
have hle : (oB - 1).toNat + (1 - oE).toNat + 1 ≤ cE - cB := by omega
|
||
exact le_trans (by exact_mod_cast hle) hgE
|
||
|
||
/-- The canonical interval `[begin.key, end.key)` of a non-trivially-empty read
|
||
with `begin.offset ≥ 1`, `end.offset ≤ 1` and `begin.key ≤ end.key` holds at least
|
||
`(begin.offset - 1).toNat + (1 - end.offset).toNat + 1` keys. The count system is
|
||
feasible by `alwaysEmpty_iff_not_countFeasible`, and
|
||
`add_add_one_le_intervalCard_of_countFeasible` transfers the room. -/
|
||
theorem add_add_one_le_intervalCard_canonical {r : RangeRead}
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1)
|
||
(hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r) :
|
||
(((r.begin.offset - 1).toNat + (1 - r.end_.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 add_add_one_le_intervalCard_of_countFeasible hle hbegin hend hfe
|
||
|
||
/-- **The empty-result `underEnd` case.** A realizable, non-trivially-empty read
|
||
with `begin.offset = 1` and `end_.offset ≤ 1` whose complete result is empty has the
|
||
canonical range `[begin.key, end.key)` as its minimal sound conflict range. Soundness
|
||
is the bound equivalence `compatible_empty_iff_length_le_any`; necessity takes a
|
||
database storing exactly `m = (1 - end_.offset).toNat` keys of the canonical interval
|
||
and omitting the chosen key, then inserts it to break the bound. The hypothesis
|
||
`¬ AlwaysEmpty r` is what supplies the room `m + 1 ≤ intervalCard begin.key end_.key`;
|
||
without it the canonical range may be nonempty while the empty range is already
|
||
minimal. -/
|
||
theorem minimal_canonicalRange_of_underEnd_empty {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hbegin : r.begin.offset = 1)
|
||
(hend : r.end_.offset ≤ 1) (hmore : res.more = false) (hempty : res.keys = []) :
|
||
Minimal r res (canonicalRange r) := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
set m : ℕ := (1 - r.end_.offset).toNat with hm
|
||
have hm1 : m + 1 = (2 - r.end_.offset).toNat := by
|
||
have h1 : (0 : ℤ) ≤ 1 - r.end_.offset := by omega
|
||
have h2 : (0 : ℤ) ≤ 2 - r.end_.offset := by omega
|
||
apply Nat.cast_injective (R := ℤ)
|
||
rw [Nat.cast_add, Nat.cast_one, hm, Int.toNat_of_nonneg h1, Int.toNat_of_nonneg h2]
|
||
omega
|
||
have hroom : (m + 1 : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by
|
||
have h := two_sub_oE_le_intervalCard_canonical hbegin hend hgap
|
||
rwa [← hm1] at h
|
||
refine (minimal_iff_sound_and_necessaryRange).mpr ⟨?_, ?_⟩
|
||
· intro db₁ db₂ hagree
|
||
have hkey : ((canonicalRange r).keys db₁).length =
|
||
((canonicalRange r).keys db₂).length := by
|
||
rw [show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree]
|
||
rw [compatible_empty_iff_length_le_any (db := db₁) hbegin hend,
|
||
compatible_empty_iff_length_le_any (db := db₂) hbegin hend, hkey]
|
||
· intro k hlo hhi
|
||
have hkcan : k ∈ canonicalRange r := ConflictRange.mem_iff.mpr ⟨hlo, hhi⟩
|
||
obtain ⟨db₀, hdb₀range, hkndb, hlen⟩ :=
|
||
exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) hroom
|
||
have hcan₀ : (canonicalRange r).keys db₀ = db₀.keys :=
|
||
canonicalRange_keys_eq_self_of_forall fun x hx => hdb₀range x hx
|
||
have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := by
|
||
rw [compatible_empty_iff_length_le_any (db := db₀) hbegin hend, hcan₀, hlen]
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hagree : AgreeOn cr db₀ (db₀.insert k hkndb) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₀ hkndb hkcr).symm
|
||
have hcomp₁ : RangeRead.Compatible (db₀.insert k hkndb) r
|
||
{ keys := [], more := false } := (hs db₀ _ hagree).mp hcomp₀
|
||
have hcan₁ : (canonicalRange r).keys (db₀.insert k hkndb) =
|
||
(db₀.insert k hkndb).keys := by
|
||
refine canonicalRange_keys_eq_self_of_forall fun x hx => ?_
|
||
rw [DB.mem_insert_iff hkndb] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hkcan
|
||
· exact hdb₀range x hx
|
||
have hlen₁ : ((canonicalRange r).keys (db₀.insert k hkndb)).length = m + 1 := by
|
||
rw [hcan₁, DB.insert_keys, List.orderedInsert_length, hlen]
|
||
have hnot : ¬ RangeRead.Compatible (db₀.insert k hkndb) r
|
||
{ keys := [], more := false } := by
|
||
rw [compatible_empty_iff_length_le_any (db := db₀.insert k hkndb) hbegin hend, hlen₁]
|
||
omega
|
||
exact hnot hcomp₁
|
||
|
||
/-- **A crossed read with `begin.offset ≥ 1` and `end.offset ≤ 1` is trivially
|
||
empty.** If `end.key < begin.key`, then `cend ≤ cbegin`, and with `oE ≤ 1 ≤ oB`
|
||
the end anchor is at most the begin anchor at every database, so the window is
|
||
always empty. -/
|
||
theorem alwaysEmpty_of_begin_gt_end {r : RangeRead} (hbegin : 1 ≤ r.begin.offset)
|
||
(hend : r.end_.offset ≤ 1) (hcross : r.end_.key < r.begin.key) : AlwaysEmpty r := by
|
||
intro db
|
||
rw [← not_ne_iff, completeKeys_ne_nil_iff]
|
||
have hea : r.end_.anchor db ≤ r.begin.anchor db := by
|
||
have hc : (cnt db r.end_.key : ℤ) ≤ cnt db r.begin.key := by
|
||
exact_mod_cast cnt_le_cnt_of_le db (le_of_lt hcross)
|
||
have ho : r.end_.offset ≤ r.begin.offset := by omega
|
||
simp only [Selector.anchor, cnt] at hc ⊢
|
||
omega
|
||
exact not_lt.mpr (min_le_min (Int.toNat_le_toNat hea) le_rfl)
|
||
|
||
/-- **A non-trivially-empty read with `begin.offset ≥ 1` and `end.offset ≤ 1` is
|
||
cross-free.** The contrapositive of `alwaysEmpty_of_begin_gt_end`: if `end.key < begin.key`
|
||
the read is trivially empty, so a non-trivially-empty read has `begin.key ≤ end.key`. -/
|
||
theorem begin_key_le_end_key_of_not_alwaysEmpty {r : RangeRead}
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) (hgap : ¬ AlwaysEmpty r) :
|
||
r.begin.key ≤ r.end_.key := by
|
||
by_contra h
|
||
exact hgap (alwaysEmpty_of_begin_gt_end hbegin hend (not_le.mp h))
|
||
|
||
/-- **The empty-result `underEnd` case at a general positive begin offset.** A
|
||
realizable, non-trivially-empty read with `1 ≤ begin.offset` and `end_.offset ≤ 1`,
|
||
`begin.key ≤ end.key`, whose complete result is empty, has the canonical range
|
||
`[begin.key, end.key)` as its minimal sound conflict range. Soundness is the bound
|
||
equivalence `compatible_empty_iff_length_sub_le_any`. Necessity takes a database of
|
||
exactly `b + k` keys of the canonical interval, where `b = (begin.offset - 1).toNat`
|
||
and `k = (1 - end.offset).toNat`, avoiding the chosen key, then inserts it: the
|
||
canonical count rises by one, so the complete keys, a left-and-right truncation of
|
||
the canonical keys, become nonempty. The hypothesis `¬ AlwaysEmpty r` supplies the
|
||
room `b + k + 1 ≤ intervalCard begin.key end.key`. -/
|
||
theorem minimal_canonicalRange_of_underEnd_empty_general {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hbegin : 1 ≤ r.begin.offset)
|
||
(hend : r.end_.offset ≤ 1) (hle : r.begin.key ≤ r.end_.key)
|
||
(hmore : res.more = false) (hempty : res.keys = []) :
|
||
Minimal r res (canonicalRange r) := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
set b : ℕ := (r.begin.offset - 1).toNat with hb
|
||
set k : ℕ := (1 - r.end_.offset).toNat with hk
|
||
have hroom : (b + k + 1 : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by
|
||
rw [hb, hk]; exact add_add_one_le_intervalCard_canonical hbegin hend hle hgap
|
||
refine (minimal_iff_sound_and_necessaryRange).mpr ⟨?_, ?_⟩
|
||
· intro db₁ db₂ hagree
|
||
have hkey : ((canonicalRange r).keys db₁).length =
|
||
((canonicalRange r).keys db₂).length := by
|
||
rw [show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree]
|
||
rw [compatible_empty_iff_length_sub_le_any (db := db₁) hbegin hend,
|
||
compatible_empty_iff_length_sub_le_any (db := db₂) hbegin hend, hkey]
|
||
· intro m hlo hhi
|
||
have hmcan : m ∈ canonicalRange r := ConflictRange.mem_iff.mpr ⟨hlo, hhi⟩
|
||
obtain ⟨db₀, hdb₀range, hmndb, hlen⟩ :=
|
||
exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := m) (m := b + k) hroom
|
||
have hcan₀ : (canonicalRange r).keys db₀ = db₀.keys :=
|
||
canonicalRange_keys_eq_self_of_forall fun x hx => hdb₀range x hx
|
||
have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := by
|
||
rw [compatible_empty_iff_length_sub_le_any (db := db₀) hbegin hend, hcan₀, hlen]
|
||
omega
|
||
intro cr hs
|
||
by_contra hmcr
|
||
have hagree : AgreeOn cr db₀ (db₀.insert m hmndb) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₀ hmndb hmcr).symm
|
||
have hcomp₁ : RangeRead.Compatible (db₀.insert m hmndb) r
|
||
{ keys := [], more := false } := (hs db₀ _ hagree).mp hcomp₀
|
||
have hcan₁ : (canonicalRange r).keys (db₀.insert m hmndb) =
|
||
(db₀.insert m hmndb).keys := by
|
||
refine canonicalRange_keys_eq_self_of_forall fun x hx => ?_
|
||
rw [DB.mem_insert_iff hmndb] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hmcan
|
||
· exact hdb₀range x hx
|
||
have hlen₁ : ((canonicalRange r).keys (db₀.insert m hmndb)).length = b + k + 1 := by
|
||
rw [hcan₁, DB.insert_keys, List.orderedInsert_length, hlen]
|
||
have hnot : ¬ RangeRead.Compatible (db₀.insert m hmndb) r
|
||
{ keys := [], more := false } := by
|
||
rw [compatible_empty_iff_length_sub_le_any (db := db₀.insert m hmndb) hbegin hend,
|
||
hlen₁]
|
||
omega
|
||
exact hnot hcomp₁
|
||
|
||
/-- The minimal canonical range in the empty-result `underEnd` case is unique up to
|
||
set equality. -/
|
||
theorem unique_minimal_canonicalRange_of_underEnd_empty {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hbegin : r.begin.offset = 1)
|
||
(hend : r.end_.offset ≤ 1) (hmore : res.more = false) (hempty : res.keys = []) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal
|
||
(minimal_canonicalRange_of_underEnd_empty hread hgap hbegin hend hmore hempty)
|
||
|
||
/-- **The empty result at an overshooting end, cross-free.** With `begin.offset = 1`,
|
||
`end_.offset ≥ 2` and `begin.key ≤ end_.key`, an empty complete result and the top
|
||
range `[begin.key, ⊤)` are compatible with exactly the same databases: if any stored
|
||
key reached `begin.key`, the first such key would be `resolve begin`, and the
|
||
overshoot would put it strictly below `resolve end_`, so it would have been reported.
|
||
Hence an empty result holds exactly when the top range stores nothing. -/
|
||
theorem completeKeys_eq_nil_iff_topRange_keys_eq_nil {db : DB} {r : RangeRead}
|
||
(hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) :
|
||
RangeRead.completeKeys db r = [] ↔ (topRange r).keys db = [] := by
|
||
constructor
|
||
· intro hce
|
||
rw [List.eq_nil_iff_forall_not_mem]
|
||
intro x hx
|
||
rw [ConflictRange.mem_keys_iff] at hx
|
||
obtain ⟨hxdb, hxge, -⟩ := hx
|
||
have hclt : cnt db r.begin.key < db.keys.length := by
|
||
rw [cnt, List.countP_lt_length_iff]
|
||
exact ⟨x, hxdb, by rw [decide_eq_false_iff_not, not_lt]; exact hxge⟩
|
||
have hnotlt : ¬ (clampIndex db r.begin < clampIndex db r.end_) :=
|
||
fun h => (completeKeys_ne_nil_iff (db := db) (r := r)).mpr h hce
|
||
have hleC : clampIndex db r.end_ ≤ clampIndex db r.begin := not_lt.mp hnotlt
|
||
have htoNat : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat)
|
||
= cnt db r.end_.key + (r.end_.offset - 1).toNat := by
|
||
rw [show ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) =
|
||
(cnt db r.end_.key : ℤ) + (r.end_.offset - 1) by ring,
|
||
Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast]
|
||
rw [clampIndex_begin_eq hbegin, Nat.min_eq_left (le_of_lt hclt),
|
||
clampIndex_end_eq, htoNat] at hleC
|
||
have hmono := cnt_le_cnt_of_le db hle
|
||
have ho1 : 1 ≤ (r.end_.offset - 1).toNat := by omega
|
||
have hA : cnt db r.end_.key + (r.end_.offset - 1).toNat ≤ cnt db r.begin.key := by
|
||
rcases le_total (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length with h | h
|
||
· rwa [Nat.min_eq_left h] at hleC
|
||
· rw [Nat.min_eq_right h] at hleC; omega
|
||
omega
|
||
· intro htr
|
||
have hcBn : cnt db r.begin.key = db.keys.length := by
|
||
rw [cnt, List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
by_contra hlt
|
||
have : x ∈ (topRange r).keys db :=
|
||
ConflictRange.mem_keys_iff.mpr ⟨hx, le_of_not_gt hlt, WithTop.coe_lt_top x⟩
|
||
rw [htr] at this; exact List.not_mem_nil this
|
||
have hnotlt : ¬ (clampIndex db r.begin < clampIndex db r.end_) := by
|
||
rw [clampIndex_begin_eq hbegin, hcBn, Nat.min_self]
|
||
exact not_lt.mpr (by rw [clampIndex_end_eq]; exact Nat.min_le_right _ _)
|
||
by_contra hne
|
||
exact hnotlt ((completeKeys_ne_nil_iff (db := db) (r := r)).mp hne)
|
||
|
||
/-- **The minimal top range for an empty complete result, cross-free.** With
|
||
`begin.offset = 1`, `end_.offset ≥ 2`, `begin.key ≤ end_.key` and a non-reversed
|
||
read, the unbounded top `[begin.key, ⊤)` is the minimal sound conflict range for the
|
||
empty complete result. Both soundness and necessity reduce to
|
||
`completeKeys_eq_nil_iff_topRange_keys_eq_nil`: compatibility is equivalent to the
|
||
top range storing nothing, so any two databases agreeing on the top range agree on
|
||
compatibility, and inserting a missing `k` into the top range makes it nonempty and
|
||
so breaks the empty result. -/
|
||
theorem minimal_topRange_of_end_offset_ge_two_empty {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (_hrev : r.reverse = false)
|
||
(hempty : res.keys = [])
|
||
(hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) :
|
||
Minimal r res (topRange r) := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
|
||
· intro db₁ db₂ hagree
|
||
rw [compatible_empty_iff_complete_nil (db := db₁) (r := r),
|
||
compatible_empty_iff_complete_nil (db := db₂) (r := r),
|
||
completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₁) hbegin hend hle,
|
||
completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₂) hbegin hend hle,
|
||
show (topRange r).keys db₁ = (topRange r).keys db₂ from hagree]
|
||
· intro k hk cr hs
|
||
obtain ⟨hklo, _⟩ := ConflictRange.mem_iff.mp hk
|
||
by_contra hkcr
|
||
obtain ⟨db₀, hdb₀⟩ := hread
|
||
have h0nil : (topRange r).keys db₀ = [] :=
|
||
(completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₀) hbegin hend hle).mp
|
||
((compatible_empty_iff_complete_nil (db := db₀) (r := r)).mp hdb₀)
|
||
have hknot : k ∉ db₀.keys := by
|
||
intro hkmem
|
||
have : k ∈ (topRange r).keys db₀ := ConflictRange.mem_keys_iff.mpr ⟨hkmem, hk⟩
|
||
rw [h0nil] at this; exact List.not_mem_nil this
|
||
have hagree : AgreeOn cr db₀ (db₀.insert k hknot) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₀ hknot hkcr).symm
|
||
have hcomp₁ : RangeRead.Compatible (db₀.insert k hknot) r { keys := [], more := false } :=
|
||
(hs db₀ _ hagree).mp hdb₀
|
||
have h1ne : (topRange r).keys (db₀.insert k hknot) ≠ [] := by
|
||
intro hnil
|
||
have : k ∈ (topRange r).keys (db₀.insert k hknot) :=
|
||
ConflictRange.mem_keys_iff.mpr ⟨DB.mem_insert_self _ _ _, hk⟩
|
||
rw [hnil] at this; exact List.not_mem_nil this
|
||
exact h1ne ((completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₀.insert k hknot)
|
||
hbegin hend hle).mp ((compatible_empty_iff_complete_nil
|
||
(db := db₀.insert k hknot) (r := r)).mp hcomp₁))
|
||
|
||
/-- The minimal top range for an empty complete result at an overshooting end is
|
||
unique up to set equality. -/
|
||
theorem unique_minimal_topRange_of_end_offset_ge_two_empty {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false)
|
||
(hempty : res.keys = []) (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal
|
||
(minimal_topRange_of_end_offset_ge_two_empty hread hmore hrev hempty hbegin hend hle)
|
||
|
||
/-!
|
||
## A non-trivial always-empty read
|
||
|
||
The `countFeasible` characterization is exact, so `AlwaysEmpty` catches reads that
|
||
no offset comparison alone can. The witness is `readPinned`: both selectors sit at
|
||
the empty key with the end offset ahead of the begin offset, yet the count below the
|
||
empty key is zero, so both clamped anchors land at `0` and the window is empty for
|
||
every database.
|
||
-/
|
||
|
||
/-- A read whose two selectors both sit at the empty key, with begin offset zero and
|
||
end offset one, is always empty: the count below the empty key is zero, so the begin
|
||
anchor is `-1` and resolves to the empty key, while the end anchor is `0` and
|
||
resolves to the first stored key (or `⊤` against the empty database). The interval
|
||
`[⊥, resolve end)` therefore holds no key: nothing is stored strictly below the
|
||
least stored key, even though the end offset is ahead of the begin offset. -/
|
||
def readPinned : RangeRead where
|
||
begin := { key := (([] : Key) : WithTop Key), offset := 0 }
|
||
end_ := { key := (([] : Key) : WithTop Key), offset := 1 }
|
||
limit := none
|
||
reverse := false
|
||
|
||
/-- `readPinned` is always empty: the begin anchor is `-1` (its count below the
|
||
empty key is zero) and the end anchor is `0`, so both clamped indices are `0`. -/
|
||
theorem alwaysEmpty_readPinned : AlwaysEmpty readPinned := by
|
||
intro db
|
||
by_contra hne
|
||
have hlt := (completeKeys_ne_nil_iff (db := db) (r := readPinned)).mp hne
|
||
have hb : clampIndex db readPinned.begin = 0 := by
|
||
have h := clampIndex_eq db (([] : Key) : WithTop Key) readPinned.begin.offset 0
|
||
(cnt_nil db)
|
||
have : readPinned.begin = ⟨(([] : Key) : WithTop Key), readPinned.begin.offset⟩ := rfl
|
||
rw [this, h]
|
||
norm_num [readPinned]
|
||
have he : clampIndex db readPinned.end_ = 0 := by
|
||
have h := clampIndex_eq db (([] : Key) : WithTop Key) readPinned.end_.offset 0
|
||
(cnt_nil db)
|
||
have : readPinned.end_ = ⟨(([] : Key) : WithTop Key), readPinned.end_.offset⟩ := rfl
|
||
rw [this, h]
|
||
norm_num [readPinned]
|
||
rw [hb, he] at hlt
|
||
exact Nat.lt_irrefl 0 hlt
|
||
|
||
/-- **Non-vacuity of the feasible side.** `readAll` can return a key: against the
|
||
one-key database `[[0]]` it returns that key. -/
|
||
theorem not_alwaysEmpty_readAll : ¬ AlwaysEmpty RangeRead.readAll := by
|
||
intro h
|
||
have hsorted : ([[(0 : Byte)]] : List Key).SortedLT := by
|
||
rw [List.sortedLT_cons]
|
||
exact ⟨by simp, List.sortedLT_nil⟩
|
||
have hk : RangeRead.completeKeys (⟨[[(0 : Byte)]], hsorted⟩ : DB) RangeRead.readAll =
|
||
[[(0 : Byte)]] := by
|
||
rw [RangeRead.completeKeys_readAll]
|
||
exact (by decide : ([[(0 : Byte)]] : List Key) ≠ []) (hk ▸ h _)
|
||
|
||
/-- The count system is feasible for `readAll`. -/
|
||
theorem countFeasible_readAll :
|
||
countFeasible RangeRead.readAll.begin.key RangeRead.readAll.end_.key
|
||
RangeRead.readAll.begin.offset RangeRead.readAll.end_.offset := by
|
||
refine countFeasible_of_exists_db ⟨⟨[[(0 : Byte)]], ?_⟩, ?_⟩
|
||
· rw [List.sortedLT_cons]
|
||
exact ⟨by simp, List.sortedLT_nil⟩
|
||
· decide
|
||
|
||
/-!
|
||
## Deciding the system: eliminating the database length
|
||
|
||
`countFeasible` quantifies over `cB`, `cE` and `n`, and the conjuncts are all
|
||
difference constraints `x - y ≤ k` once `n` is handled. The only conjunct that
|
||
couples `n` to the anchors is `min a n < min b n`, and
|
||
|
||
`min a n < min b n ↔ a < b ∧ a < n`
|
||
|
||
isolates the length into the single side condition `a < n`. Since the remaining
|
||
`n`-constraints are `cB ≤ n`, `cE ≤ n` and the edge pins `c = n`, the length can be
|
||
chosen minimally as the maximum of the lower bounds whenever the anchor inequality
|
||
is satisfiable, so `n` is eliminable and `countFeasible` reduces to a bounded
|
||
condition on `cB` and `cE` alone.
|
||
-/
|
||
|
||
/-- The `min`-with-length conjunct is a pure anchor ordering plus a lower bound on
|
||
the length: `min a n < min b n` exactly when `a < b` and `a` is still below `n`. -/
|
||
theorem min_min_lt_iff (a b n : ℕ) : min a n < min b n ↔ a < b ∧ a < n := by
|
||
omega
|
||
|
||
/-- The `min`-with-length predicate is monotone in the length: if it holds at `n` it
|
||
holds at any larger length, since `a < n` persists. -/
|
||
theorem min_min_lt_mono {a b : ℕ} {n m : ℕ} (hnm : n ≤ m) (h : min a n < min b n) :
|
||
min a m < min b m := by
|
||
rw [min_min_lt_iff] at h ⊢
|
||
exact ⟨h.1, lt_of_lt_of_le h.2 hnm⟩
|
||
|
||
/-- The length can be clamped upward to at least `a + 1` once the `min`-conjunct
|
||
holds: the only role of the length is to exceed the begin anchor `a`. -/
|
||
theorem min_min_lt_max_succ {a b n : ℕ} (h : min a n < min b n) :
|
||
min a (max n (a + 1)) < min b (max n (a + 1)) :=
|
||
min_min_lt_mono (le_max_left _ _) h
|
||
|
||
/-- The anchor `(c + o - 1).toNat` is below the length `n` exactly when `n` is
|
||
positive and the signed anchor is below `n`. This turns the length lower bound into
|
||
ordinary difference constraints on `c` and `n`. -/
|
||
theorem toNat_anchor_lt_iff (c : ℕ) (o : ℤ) (n : ℕ) :
|
||
((c : ℤ) + o - 1).toNat < n ↔ 0 < n ∧ (c : ℤ) + o - 1 < (n : ℤ) := by
|
||
rcases lt_or_ge 0 ((c : ℤ) + o - 1) with h | h
|
||
· rw [Int.toNat_lt (le_of_lt h)]
|
||
omega
|
||
· have hz : ((c : ℤ) + o - 1).toNat = 0 := Int.toNat_of_nonpos h
|
||
rw [hz]
|
||
omega
|
||
|
||
/-- Two anchors are ordered below the length exactly when the end anchor is
|
||
positive, the begin anchor precedes it, the length is positive, and the begin
|
||
anchor is below the length. The clamped-`Nat` comparison collapses to a pure
|
||
integer condition: the clamp to the empty key is subsumed by `0 < end` and
|
||
`begin < end`. -/
|
||
theorem clamp_lt_iff (cB cE n : ℕ) (oB oE : ℤ) :
|
||
min (((cB : ℤ) + oB - 1).toNat) n < min (((cE : ℤ) + oE - 1).toNat) n ↔
|
||
0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧
|
||
0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ) := by
|
||
rw [min_min_lt_iff]
|
||
have hb : ((cB : ℤ) + oB - 1).toNat < ((cE : ℤ) + oE - 1).toNat ↔
|
||
0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 := by
|
||
constructor
|
||
· intro h
|
||
exact ⟨by omega, Int.lt_of_toNat_lt h⟩
|
||
· rintro ⟨h0, hlt⟩
|
||
exact (Int.toNat_lt_toNat h0).mpr hlt
|
||
rw [hb, toNat_anchor_lt_iff]
|
||
tauto
|
||
|
||
/-- **The empty result at a nonpositive begin offset.** With `begin.offset ≤ 0` the
|
||
begin anchor is `cB + oB - 1 ≤ cB - 1 < n`, so it never clamps at `⊤` and the clamped
|
||
comparison collapses to `cE + oE - 1 ≤ (cB + oB - 1).toNat`, with the sole extra
|
||
boundary being the empty database. Here `cB, cE` are the stored counts below the two
|
||
selector keys. This is the case where the canonical range is unsound, so the value
|
||
built from it below is a genuinely different candidate. -/
|
||
theorem compatible_empty_iff_clamp {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 ≤
|
||
(((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat : ℤ) := by
|
||
have hiff : RangeRead.Compatible db r { keys := [], more := false } ↔
|
||
¬ (clampIndex db r.begin < clampIndex db r.end_) := by
|
||
rw [compatible_empty_iff_complete_nil, ← completeKeys_ne_nil_iff]; simp only [not_ne_iff]
|
||
rw [hiff, clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl,
|
||
clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl]
|
||
have halt : ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) < (db.keys.length : ℤ) := by
|
||
have := cnt_le_length db r.begin.key
|
||
omega
|
||
have han : ((((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat)) ≤ db.keys.length :=
|
||
Int.toNat_le.mpr (le_of_lt halt)
|
||
rw [Nat.min_eq_left han, not_lt]
|
||
constructor
|
||
· intro h
|
||
by_cases hn0 : db.keys.length = 0
|
||
· exact Or.inl (by rw [List.eq_nil_iff_length_eq_zero]; exact hn0)
|
||
· refine Or.inr ?_
|
||
-- Here the begin anchor `aNat < n`, so `min eNat n ≤ aNat` forces `eNat ≤ aNat`.
|
||
have haLt : (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) < db.keys.length := by
|
||
rcases lt_or_ge 0 ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) with h0 | h0
|
||
· rw [Int.toNat_lt (le_of_lt h0)]; exact halt
|
||
· rw [Int.toNat_of_nonpos h0]; omega
|
||
have hEA : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤
|
||
(((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) := by
|
||
by_cases hEn : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤ db.keys.length
|
||
· rw [Nat.min_eq_left hEn] at h; exact h
|
||
· exfalso
|
||
rw [Nat.min_eq_right (le_of_lt (not_le.mp hEn))] at h
|
||
omega
|
||
have hEmax : ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) ≤
|
||
((((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat : ℕ) : ℤ) := by
|
||
rw [Int.toNat_eq_max]; omega
|
||
omega
|
||
· rintro (hnil | hle)
|
||
· rw [List.eq_nil_iff_length_eq_zero] at hnil
|
||
rw [hnil]; simp
|
||
· have : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤
|
||
(((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) := by
|
||
simpa using Int.toNat_le_toNat hle
|
||
exact le_trans (Nat.min_le_left _ _) this
|
||
|
||
/-- The arithmetic core of `countFeasible`, with the clamped comparison replaced by
|
||
its pure integer form. This is the difference-constraint system on `cB`, `cE`, `n`
|
||
whose feasibility `countFeasible` decides. -/
|
||
def countFeasibleInt (β ε : WithTop Key) (oB oE : ℤ) : Prop :=
|
||
∃ cB cE n : ℕ,
|
||
(β = ⊤ → cB = n) ∧ (ε = ⊤ → cE = n) ∧
|
||
cB ≤ n ∧ cE ≤ n ∧
|
||
(cB : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) β ∧
|
||
(cE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε ∧
|
||
((cB - cE : ℕ) : ℕ∞) ≤ intervalCard ε β ∧
|
||
((cE - cB : ℕ) : ℕ∞) ≤ intervalCard β ε ∧
|
||
0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧
|
||
0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ)
|
||
|
||
/-- `countFeasible` has the same solutions as its pure integer form: the clamped
|
||
comparison is equivalent to the integer conjuncts by `clamp_lt_iff`. -/
|
||
theorem countFeasible_iff_int (β ε : WithTop Key) (oB oE : ℤ) :
|
||
countFeasible β ε oB oE ↔ countFeasibleInt β ε oB oE := by
|
||
unfold countFeasible countFeasibleInt
|
||
refine exists_congr fun cB => exists_congr fun cE => exists_congr fun n => ?_
|
||
constructor
|
||
· rintro ⟨hbt, hct, h1, h2, h3, h4, h5, h6, h7⟩
|
||
exact ⟨hbt, hct, h1, h2, h3, h4, h5, h6, (clamp_lt_iff cB cE n oB oE).mp h7⟩
|
||
· rintro ⟨hbt, hct, h1, h2, h3, h4, h5, h6, h7⟩
|
||
exact ⟨hbt, hct, h1, h2, h3, h4, h5, h6, (clamp_lt_iff cB cE n oB oE).mpr h7⟩
|
||
|
||
/-!
|
||
## A finite witness bound
|
||
|
||
The witnesses to `countFeasible` can be taken bounded by the offsets alone:
|
||
`|oB| + |oE| + 3`, independently of the interval cardinalities. Intuitively the
|
||
counts only matter up to their order, so the least feasible triple is determined
|
||
by how far the two anchors can be pushed apart, which the offsets control; the
|
||
caps only forbid large values, never require them.
|
||
|
||
The proof fixes the *smallest* `cE` forced by the end anchor (`2 - oE`) and the
|
||
room the difference needs (`oB - oE + 1`), then takes `cB` as the smallest value
|
||
allowed by the difference cap `intervalCard β ε`, and the length as the least
|
||
value exceeding both counts and the begin anchor.
|
||
-/
|
||
|
||
/-- A count `c` is at most an `ℕ∞` cap exactly when it is at most the cap's
|
||
`toNat` or the cap is `⊤`. -/
|
||
theorem natCast_le_iff_toNat (c : ℕ) (κ : ℕ∞) : (c : ℕ∞) ≤ κ ↔ c ≤ κ.toNat ∨ κ = ⊤ := by
|
||
cases κ with
|
||
| top => simp
|
||
| coe k => simp
|
||
|
||
/-- A truncated difference is at most an `ℕ∞` cap exactly when it is at most the
|
||
cap's `toNat` or the cap is `⊤`. -/
|
||
theorem tsub_natCast_le_iff_toNat (a b : ℕ) (κ : ℕ∞) :
|
||
((a - b : ℕ) : ℕ∞) ≤ κ ↔ a - b ≤ κ.toNat ∨ κ = ⊤ := by
|
||
cases κ with
|
||
| top => simp
|
||
| coe k =>
|
||
simp only [ENat.toNat_natCast]
|
||
constructor
|
||
· intro h; exact Or.inl (ENat.natCast_le_natCast.mp h)
|
||
· rintro (h | h)
|
||
· exact_mod_cast h
|
||
· exact absurd h (by simp)
|
||
|
||
/-- The offset datum `max (2 - oE) (oB - oE + 1)` as a natural number: the least
|
||
end count `cE` compatible with the end anchor and with room for the begin anchor.
|
||
Any feasible solution has `cE` at least this. -/
|
||
def cELo (oB oE : ℤ) : ℕ := max ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat
|
||
|
||
/-- `cELo` is a lower bound for any feasible `cE`. -/
|
||
theorem cELo_le {oB oE : ℤ} {cB cE : ℕ}
|
||
(h1 : 0 < (cE : ℤ) + oE - 1) (h2 : (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1) :
|
||
cELo oB oE ≤ cE := by
|
||
unfold cELo
|
||
have ha : ((2 : ℤ) - oE).toNat ≤ cE := by omega
|
||
have hb : (oB - oE + 1).toNat ≤ cE := by omega
|
||
omega
|
||
|
||
/-- `cELo` still satisfies the end-anchor positivity and can carry the begin
|
||
anchor at any `cB ≤ cELo + (oE - oB) - 1`; in particular it is within
|
||
`|oB| + |oE| + 3`. -/
|
||
theorem cELo_anchor_le {oB oE : ℤ} :
|
||
(cELo oB oE : ℤ) + oE - 1 < (oB.natAbs + oE.natAbs + 3 : ℕ) := by
|
||
have ha : (((2 : ℤ) - oE).toNat : ℤ) + oE - 1 < (oB.natAbs : ℤ) + oE.natAbs + 3 := by
|
||
rcases lt_or_ge oE 2 with h | h
|
||
· have : (0 : ℤ) ≤ 2 - oE := by omega
|
||
rw [Int.toNat_of_nonneg this]
|
||
have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega
|
||
· have : ((2 : ℤ) - oE).toNat = 0 := Int.toNat_of_nonpos (by omega)
|
||
rw [this]
|
||
have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega
|
||
have hb : (((oB - oE + 1).toNat : ℤ)) + oE - 1 < (oB.natAbs : ℤ) + oE.natAbs + 3 := by
|
||
rcases lt_or_ge 0 (oB - oE + 1) with h | h
|
||
· rw [Int.toNat_of_nonneg (by omega)]
|
||
have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega
|
||
· have hz : ((oB - oE + 1).toNat : ℤ) = 0 := by
|
||
rw [Int.toNat_of_nonpos (by omega)]; simp
|
||
rw [hz]
|
||
have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega
|
||
unfold cELo
|
||
rcases le_total ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat with h | h
|
||
· rw [max_eq_right h]; exact hb
|
||
· rw [max_eq_left h]; exact ha
|
||
|
||
theorem cELo_spec {oB oE : ℤ} :
|
||
0 < (cELo oB oE : ℤ) + oE - 1 ∧ cELo oB oE ≤ oB.natAbs + oE.natAbs + 3 := by
|
||
unfold cELo
|
||
constructor <;> omega
|
||
|
||
/-- The end anchor at `cELo` sits strictly above the begin anchor, so `cELo` leaves
|
||
room for some `cB`: `oB - oE < cELo oB oE` in `ℤ`. -/
|
||
theorem cELo_gt (oB oE : ℤ) : oB - oE < (cELo oB oE : ℤ) := by
|
||
unfold cELo
|
||
have h := le_max_right ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat
|
||
have hle : (((oB - oE + 1).toNat : ℕ) : ℤ) ≤
|
||
(max ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat : ℕ) := by exact_mod_cast h
|
||
rcases lt_or_ge 0 (oB - oE + 1) with hpos | hneg
|
||
· have : ((oB - oE + 1).toNat : ℤ) = oB - oE + 1 := Int.toNat_of_nonneg (by omega)
|
||
omega
|
||
· have : ((oB - oE + 1).toNat : ℤ) = 0 := by
|
||
rw [Int.toNat_of_nonpos (by omega)]; simp
|
||
omega
|
||
|
||
/-- **Core witness reduction, no edge pins.** Given any feasible triple for the
|
||
count system, there is one with `cE` the offset datum `cELo`, `cB` the least value
|
||
allowed by the difference cap `intervalCard β ε`, and all three bounded by
|
||
`|oB| + |oE| + 3`. The caps only forbid large values, so shrinking to the least
|
||
compatible counts preserves feasibility. -/
|
||
theorem exists_bounded_of_noPin {KB KE KEB KBE : ℕ∞} {oB oE : ℤ}
|
||
(h : ∃ cB cE n : ℕ,
|
||
(cB : ℕ∞) ≤ KB ∧ (cE : ℕ∞) ≤ KE ∧
|
||
((cB - cE : ℕ) : ℕ∞) ≤ KEB ∧ ((cE - cB : ℕ) : ℕ∞) ≤ KBE ∧
|
||
cB ≤ n ∧ cE ≤ n ∧
|
||
0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧
|
||
0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ)) :
|
||
∃ cB cE n : ℕ,
|
||
(cB : ℕ∞) ≤ KB ∧ (cE : ℕ∞) ≤ KE ∧
|
||
((cB - cE : ℕ) : ℕ∞) ≤ KEB ∧ ((cE - cB : ℕ) : ℕ∞) ≤ KBE ∧
|
||
cB ≤ n ∧ cE ≤ n ∧
|
||
0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧
|
||
0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ) ∧
|
||
cB ≤ oB.natAbs + oE.natAbs + 3 ∧ cE ≤ oB.natAbs + oE.natAbs + 3 ∧
|
||
n ≤ oB.natAbs + oE.natAbs + 3 := by
|
||
obtain ⟨cB, cE, n, hKB, hKE, hKEB, hKBE, h1, h2, h3, h4, h5, h6⟩ := h
|
||
have hce_lo : cELo oB oE ≤ cE := cELo_le h3 h4
|
||
have hspec := cELo_spec (oB := oB) (oE := oE)
|
||
have hd2 : cE - cB ≤ KBE.toNat ∨ KBE = ⊤ := (tsub_natCast_le_iff_toNat cE cB KBE).mp hKBE
|
||
have hcb_cap : cB ≤ KB.toNat ∨ KB = ⊤ := (natCast_le_iff_toNat cB KB).mp hKB
|
||
have hce_cap : cE ≤ KE.toNat ∨ KE = ⊤ := (natCast_le_iff_toNat cE KE).mp hKE
|
||
set e0 := cELo oB oE with he0
|
||
set b0 : ℕ := if KBE = ⊤ then 0 else max 0 (e0 - KBE.toNat) with hb0
|
||
have hb0_cB : b0 ≤ cB := by
|
||
by_cases hBE : KBE = ⊤
|
||
· simp only [hb0, hBE, ↓reduceIte]; omega
|
||
· simp only [hb0, hBE, ↓reduceIte]
|
||
rcases hd2 with hd | hd
|
||
· omega
|
||
· exact absurd hd hBE
|
||
have hb0_e0 : b0 ≤ e0 := by
|
||
by_cases hBE : KBE = ⊤ <;> simp only [hb0, hBE, ↓reduceIte] <;> omega
|
||
have hb0_KB : (b0 : ℕ∞) ≤ KB := by
|
||
rw [natCast_le_iff_toNat]
|
||
by_cases hB : KB = ⊤
|
||
· exact Or.inr hB
|
||
· left; rcases hcb_cap with h | h
|
||
· omega
|
||
· exact absurd h hB
|
||
have he0_KE : (e0 : ℕ∞) ≤ KE := by
|
||
rw [natCast_le_iff_toNat]
|
||
rcases hce_cap with h | h
|
||
· exact Or.inl (le_trans hce_lo h)
|
||
· exact Or.inr h
|
||
have hd1' : ((b0 - e0 : ℕ) : ℕ∞) ≤ KEB := by
|
||
have : b0 - e0 = 0 := Nat.sub_eq_zero_of_le hb0_e0
|
||
rw [this]; exact bot_le
|
||
have hd2' : ((e0 - b0 : ℕ) : ℕ∞) ≤ KBE := by
|
||
rw [tsub_natCast_le_iff_toNat]
|
||
by_cases hBE : KBE = ⊤
|
||
· exact Or.inr hBE
|
||
· left
|
||
have hb : b0 = max 0 (e0 - KBE.toNat) := by simp only [hb0, hBE, ↓reduceIte]
|
||
omega
|
||
have he0_ge : (0 : ℤ) < (e0 : ℤ) + oE - 1 := by rw [he0]; exact hspec.1
|
||
have he0gt : oB - oE < (e0 : ℤ) := by rw [he0]; exact cELo_gt oB oE
|
||
have hord : (b0 : ℤ) + oB - 1 < (e0 : ℤ) + oE - 1 := by
|
||
by_cases hBE : KBE = ⊤
|
||
· have hb : b0 = 0 := by simp only [hb0, hBE, ↓reduceIte]
|
||
rw [hb]; omega
|
||
· have hb : b0 = max 0 (e0 - KBE.toNat) := by simp only [hb0, hBE, ↓reduceIte]
|
||
rw [hb]
|
||
have hlt : oB - oE < (KBE.toNat : ℤ) := by
|
||
rcases hd2 with hd | hd
|
||
· omega
|
||
· exact absurd hd hBE
|
||
by_cases hle : e0 ≤ KBE.toNat
|
||
· rw [max_eq_left (by omega)]; omega
|
||
· rw [max_eq_right (by omega)]; omega
|
||
have he0_bound : e0 ≤ oB.natAbs + oE.natAbs + 3 := by rw [he0]; exact hspec.2
|
||
have hb0_bound : b0 ≤ oB.natAbs + oE.natAbs + 3 := le_trans hb0_e0 he0_bound
|
||
have hBpos : 0 < oB.natAbs + oE.natAbs + 3 := by positivity
|
||
have hA : (e0 : ℤ) + oE - 1 < (oB.natAbs + oE.natAbs + 3 : ℕ) := by
|
||
rw [he0]; exact cELo_anchor_le
|
||
have hbeg : (b0 : ℤ) + oB - 1 < (oB.natAbs + oE.natAbs + 3 : ℕ) :=
|
||
lt_trans hord hA
|
||
exact ⟨b0, e0, oB.natAbs + oE.natAbs + 3, hb0_KB, he0_KE, hd1', hd2',
|
||
hb0_bound, he0_bound, he0_ge, hord, hBpos, hbeg,
|
||
hb0_bound, he0_bound, le_refl _⟩
|
||
|
||
/-- The begin count of the canonical no-pin witness: the least value within the
|
||
difference cap `KBE` of the least end count `cELo`. -/
|
||
def noPinB (KBE : ℕ∞) (oB oE : ℤ) : ℕ :=
|
||
if KBE = ⊤ then 0 else max 0 (cELo oB oE - KBE.toNat)
|
||
|
||
/-- The canonical no-pin length bound. -/
|
||
def noPinN (oB oE : ℤ) : ℕ := oB.natAbs + oE.natAbs + 3
|
||
|
||
/-- **No-pin closed form.** For the genuine/genuine case the count system is
|
||
feasible exactly when its canonical candidate — end count `cELo`, begin count
|
||
`noPinB`, length `noPinN` — satisfies the quantifier-free constraints. The caps
|
||
only forbid large counts, so the least compatible counts witness feasibility. -/
|
||
theorem exists_bounded_of_noPin_iff {KB KE KEB KBE : ℕ∞} {oB oE : ℤ} :
|
||
(∃ cB cE n : ℕ,
|
||
(cB : ℕ∞) ≤ KB ∧ (cE : ℕ∞) ≤ KE ∧
|
||
((cB - cE : ℕ) : ℕ∞) ≤ KEB ∧ ((cE - cB : ℕ) : ℕ∞) ≤ KBE ∧
|
||
cB ≤ n ∧ cE ≤ n ∧
|
||
0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧
|
||
0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ)) ↔
|
||
((noPinB KBE oB oE : ℕ∞) ≤ KB ∧ (cELo oB oE : ℕ∞) ≤ KE ∧
|
||
((noPinB KBE oB oE - cELo oB oE : ℕ) : ℕ∞) ≤ KEB ∧
|
||
((cELo oB oE - noPinB KBE oB oE : ℕ) : ℕ∞) ≤ KBE ∧
|
||
0 < (cELo oB oE : ℤ) + oE - 1 ∧
|
||
(noPinB KBE oB oE : ℤ) + oB - 1 < (cELo oB oE : ℤ) + oE - 1) := by
|
||
constructor
|
||
· intro h
|
||
obtain ⟨cB, cE, n, hKB, hKE, hKEB, hKBE, h1, h2, h3, h4, h5, h6⟩ := h
|
||
-- Re-run the construction, extracting the body for the canonical candidate.
|
||
have hce_lo : cELo oB oE ≤ cE := cELo_le h3 h4
|
||
have hspec := cELo_spec (oB := oB) (oE := oE)
|
||
have hd2 : cE - cB ≤ KBE.toNat ∨ KBE = ⊤ := (tsub_natCast_le_iff_toNat cE cB KBE).mp hKBE
|
||
have hcb_cap : cB ≤ KB.toNat ∨ KB = ⊤ := (natCast_le_iff_toNat cB KB).mp hKB
|
||
have hce_cap : cE ≤ KE.toNat ∨ KE = ⊤ := (natCast_le_iff_toNat cE KE).mp hKE
|
||
have hb0_cB : noPinB KBE oB oE ≤ cB := by
|
||
unfold noPinB
|
||
by_cases hBE : KBE = ⊤
|
||
· simp only [hBE, ↓reduceIte]; omega
|
||
· simp only [hBE, ↓reduceIte]
|
||
rcases hd2 with hd | hd
|
||
· omega
|
||
· exact absurd hd hBE
|
||
have hb0_e0 : noPinB KBE oB oE ≤ cELo oB oE := by
|
||
unfold noPinB
|
||
by_cases hBE : KBE = ⊤ <;> simp only [hBE, ↓reduceIte] <;> omega
|
||
refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩
|
||
· rw [natCast_le_iff_toNat]
|
||
by_cases hB : KB = ⊤
|
||
· exact Or.inr hB
|
||
· left; rcases hcb_cap with hc | hc
|
||
· omega
|
||
· exact absurd hc hB
|
||
· rw [natCast_le_iff_toNat]
|
||
rcases hce_cap with hc | hc
|
||
· exact Or.inl (le_trans hce_lo hc)
|
||
· exact Or.inr hc
|
||
· rw [tsub_natCast_le_iff_toNat]
|
||
left
|
||
have : noPinB KBE oB oE - cELo oB oE = 0 := Nat.sub_eq_zero_of_le hb0_e0
|
||
omega
|
||
· rw [tsub_natCast_le_iff_toNat]
|
||
by_cases hBE : KBE = ⊤
|
||
· exact Or.inr hBE
|
||
· left
|
||
unfold noPinB
|
||
simp only [hBE, ↓reduceIte]
|
||
omega
|
||
· exact hspec.1
|
||
· -- anchor ordering for the canonical candidate
|
||
have he0_ge := hspec.1
|
||
have hord : (noPinB KBE oB oE : ℤ) + oB - 1 < (cELo oB oE : ℤ) + oE - 1 := by
|
||
have he0gt : oB - oE < (cELo oB oE : ℤ) := cELo_gt oB oE
|
||
unfold noPinB
|
||
by_cases hBE : KBE = ⊤
|
||
· simp only [hBE, ↓reduceIte]; omega
|
||
· simp only [hBE, ↓reduceIte]
|
||
have hlt : oB - oE < (KBE.toNat : ℤ) := by
|
||
rcases hd2 with hd | hd
|
||
· omega
|
||
· exact absurd hd hBE
|
||
by_cases hle : cELo oB oE ≤ KBE.toNat
|
||
· rw [max_eq_left (by omega)]; omega
|
||
· rw [max_eq_right (by omega)]; omega
|
||
exact hord
|
||
· rintro ⟨hKB, hKE, hKEB, hKBE, hge, hord⟩
|
||
refine ⟨noPinB KBE oB oE, cELo oB oE, noPinN oB oE, hKB, hKE, hKEB, hKBE,
|
||
?_, ?_, hge, hord, ?_, ?_⟩
|
||
· -- noPinB ≤ noPinN
|
||
unfold noPinB noPinN
|
||
by_cases hBE : KBE = ⊤ <;> simp only [hBE, ↓reduceIte]
|
||
· omega
|
||
· have := cELo_spec (oB := oB) (oE := oE); omega
|
||
· -- cELo ≤ noPinN
|
||
exact cELo_spec.2
|
||
· -- 0 < noPinN
|
||
unfold noPinN; omega
|
||
· -- begin anchor below noPinN
|
||
have hA : (cELo oB oE : ℤ) + oE - 1 < (noPinN oB oE : ℕ) := by
|
||
unfold noPinN; exact cELo_anchor_le
|
||
have hbeg : (noPinB KBE oB oE : ℤ) + oB - 1 < (noPinN oB oE : ℕ) :=
|
||
lt_trans hord hA
|
||
exact hbeg
|
||
|
||
/-!
|
||
## The closed form by endpoint case
|
||
|
||
`countFeasibleInt` unfolds by whether each endpoint is `⊤` or a genuine key. The
|
||
genuine/genuine case is `exists_bounded_of_noPin_iff`; the three cases with a `⊤`
|
||
endpoint each collapse to a simple offset condition, because `intervalCard ⊤ _ = 0`
|
||
pins the counts together and `intervalCard _ ⊤ = ⊤` drops the difference bounds.
|
||
-/
|
||
|
||
/-- The least end count when the begin endpoint is `⊤`: only the end-anchor
|
||
positivity `2 - oE ≤ cE` constrains it, since the begin count is then free. -/
|
||
def cEminP (_oB oE : ℤ) : ℕ := ((2 : ℤ) - oE).toNat
|
||
|
||
/-- `cEminP` satisfies the end-anchor positivity. -/
|
||
theorem cEminP_ge (oB oE : ℤ) : (2 : ℤ) - oE ≤ (cEminP oB oE : ℤ) := by
|
||
unfold cEminP
|
||
rcases lt_or_ge oE 2 with h | h
|
||
· rw [Int.toNat_of_nonneg (by omega)]
|
||
· have : ((2 : ℤ) - oE).toNat = 0 := Int.toNat_of_nonpos (by omega)
|
||
rw [this]; omega
|
||
|
||
/-- `β = ⊤`, `ε = ↑e`: only `cB = n` is pinned, so the system is feasible exactly
|
||
when `oB ≤ 0` (the begin anchor cannot exceed the length), `oB < oE` (the end anchor
|
||
gets ahead), and the least end count `cEminP` fits below the end cap. -/
|
||
theorem top_coe_iff {e : Key} {oB oE : ℤ} :
|
||
countFeasibleInt (⊤ : WithTop Key) (↑e) oB oE ↔
|
||
oB ≤ 0 ∧ oB < oE ∧
|
||
(cEminP oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) (↑e) := by
|
||
unfold countFeasibleInt
|
||
constructor
|
||
· rintro ⟨cB, cE, n, hpin, -, h3, h4, -, h6, -, h8, hge, hord, h11, h12⟩
|
||
have hcB : cB = n := hpin rfl
|
||
rw [hcB] at h8 h12
|
||
have hKBE : ((cE - n : ℕ) : ℕ∞) ≤ (0 : ℕ∞) := by
|
||
simpa only [intervalCard_top_left] using h8
|
||
have hcEn : cE ≤ n := by
|
||
by_contra hc
|
||
have hpos : 0 < cE - n := Nat.sub_pos_of_lt (lt_of_not_ge hc)
|
||
have : (0 : ℕ∞) < ((cE - n : ℕ) : ℕ∞) := by exact_mod_cast hpos
|
||
omega
|
||
have hcE : cEminP oB oE ≤ cE := by
|
||
unfold cEminP; rw [Int.toNat_le]; omega
|
||
exact ⟨by omega, by omega, le_trans (by exact_mod_cast hcE) h6⟩
|
||
· rintro ⟨hoB, hoBoE, hKE⟩
|
||
refine ⟨max (cEminP oB oE) 1, cEminP oB oE, max (cEminP oB oE) 1, ?_⟩
|
||
have hge' : (0 : ℤ) < (cEminP oB oE : ℤ) + oE - 1 := by
|
||
have := cEminP_ge oB oE; omega
|
||
have hord' : ((max (cEminP oB oE) 1 : ℕ) : ℤ) + oB - 1 <
|
||
(cEminP oB oE : ℤ) + oE - 1 := by
|
||
by_cases hle : cEminP oB oE ≤ 1
|
||
· have hmax : max (cEminP oB oE) 1 = 1 := max_eq_right hle
|
||
rw [hmax]
|
||
have hz : cEminP oB oE = 0 ∨ cEminP oB oE = 1 := by omega
|
||
rcases hz with h0 | h1
|
||
· have : 2 ≤ oE := by
|
||
unfold cEminP at h0
|
||
have := (Int.toNat_eq_zero.mp h0); omega
|
||
omega
|
||
· omega
|
||
· have hmax : max (cEminP oB oE) 1 = cEminP oB oE := max_eq_left (by omega)
|
||
rw [hmax]; omega
|
||
exact ⟨fun _ => rfl, fun h => absurd h (by simp), le_refl _, le_max_left _ _,
|
||
le_top, hKE, le_top, (by rw [Nat.sub_eq_zero_of_le (le_max_left _ _)]; exact bot_le),
|
||
hge', hord', lt_of_lt_of_le (by norm_num) (le_max_right _ _), by omega⟩
|
||
|
||
/-- `ε = ⊤`: always feasible, since the end count can be taken as large as the
|
||
database length and the begin count as `0`. -/
|
||
theorem coe_top_iff {b : Key} {oB oE : ℤ} :
|
||
countFeasibleInt (↑b) (⊤ : WithTop Key) oB oE ↔ True := by
|
||
unfold countFeasibleInt
|
||
refine iff_true_intro ?_
|
||
let N : ℕ := oB.natAbs + oE.natAbs + 3
|
||
have hNpos : 0 < N := by unfold N; omega
|
||
have hNoB : oB ≤ (N : ℤ) := by unfold N; omega
|
||
have hNoE : (0 : ℤ) < (N : ℤ) + oE - 1 := by unfold N; omega
|
||
have hNord : (0 : ℤ) + oB - 1 < (N : ℤ) + oE - 1 := by unfold N; omega
|
||
exact ⟨0, N, N, fun h => absurd h (by simp), fun _ => rfl, Nat.zero_le _, le_refl _,
|
||
by simp, le_top, by simp, le_top, hNoE, hNord, hNpos, by omega⟩
|
||
|
||
/-- Both endpoints `⊤`: the counts are pinned equal to the length, so the system is
|
||
feasible exactly when the begin anchor can be pushed to `≤ 0` and below the end
|
||
anchor, i.e. `oB ≤ 0` and `oB < oE`. -/
|
||
theorem top_top_iff {oB oE : ℤ} :
|
||
countFeasibleInt (⊤ : WithTop Key) (⊤ : WithTop Key) oB oE ↔
|
||
oB ≤ 0 ∧ oB < oE := by
|
||
unfold countFeasibleInt
|
||
constructor
|
||
· rintro ⟨cB, cE, n, hpinB, hpinE, -, -, -, -, h7, h8, hge, hord, h11, h12⟩
|
||
have hB : cB = n := hpinB rfl
|
||
have hE : cE = n := hpinE rfl
|
||
subst hB; subst hE
|
||
exact ⟨by omega, by omega⟩
|
||
· rintro ⟨hoB, hoBoE⟩
|
||
let N : ℕ := oE.natAbs + 2
|
||
refine ⟨N, N, N, fun _ => rfl, fun _ => rfl, le_refl _, le_refl _,
|
||
le_top, le_top, ?_, ?_, ?_, ?_, ?_, ?_⟩
|
||
· rw [Nat.sub_self]; exact bot_le
|
||
· rw [Nat.sub_self]; exact bot_le
|
||
· unfold N; omega
|
||
· unfold N; omega
|
||
· unfold N; omega
|
||
· unfold N; omega
|
||
|
||
/-- The genuine/genuine body in the form `exists_bounded_of_noPin_iff` expects. -/
|
||
theorem coe_coe_iff {b e : Key} {oB oE : ℤ} :
|
||
countFeasibleInt (↑b) (↑e) oB oE ↔
|
||
(∃ cB cE n : ℕ,
|
||
(cB : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) (↑b) ∧
|
||
(cE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) (↑e) ∧
|
||
((cB - cE : ℕ) : ℕ∞) ≤ intervalCard (↑e) (↑b) ∧
|
||
((cE - cB : ℕ) : ℕ∞) ≤ intervalCard (↑b) (↑e) ∧
|
||
cB ≤ n ∧ cE ≤ n ∧
|
||
0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧
|
||
0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ)) := by
|
||
unfold countFeasibleInt
|
||
refine exists_congr fun cB => exists_congr fun cE => exists_congr fun n => ?_
|
||
constructor
|
||
· rintro ⟨-, -, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10⟩
|
||
exact ⟨h3, h4, h5, h6, h1, h2, h7, h8, h9, h10⟩
|
||
· rintro ⟨h3, h4, h5, h6, h1, h2, h7, h8, h9, h10⟩
|
||
exact ⟨by simp, by simp, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10⟩
|
||
|
||
/-!
|
||
## Deciding feasibility
|
||
|
||
Collecting the four endpoint cases into a single quantifier-free predicate
|
||
`closedForm` makes `countFeasibleInt` decidable: every case is a Boolean
|
||
combination of `intervalCard` comparisons and integer arithmetic on the read's
|
||
own data, with no search over counts.
|
||
-/
|
||
|
||
/-- The quantifier-free decision predicate: `countFeasibleInt` unfolded by the
|
||
endpoint case, with the genuine/genuine case evaluated at its canonical witness.
|
||
It is an `abbrev` so the decidable instance for its `if` guards unfolds. -/
|
||
abbrev closedForm (β ε : WithTop Key) (oB oE : ℤ) : Prop :=
|
||
if β = ⊤ then
|
||
if ε = ⊤ then oB ≤ 0 ∧ oB < oE
|
||
else oB ≤ 0 ∧ oB < oE ∧
|
||
(cEminP oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε
|
||
else if ε = ⊤ then True
|
||
else
|
||
(noPinB (intervalCard β ε) oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) β ∧
|
||
(cELo oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε ∧
|
||
((noPinB (intervalCard β ε) oB oE - cELo oB oE : ℕ) : ℕ∞) ≤ intervalCard ε β ∧
|
||
((cELo oB oE - noPinB (intervalCard β ε) oB oE : ℕ) : ℕ∞) ≤ intervalCard β ε ∧
|
||
0 < (cELo oB oE : ℤ) + oE - 1 ∧
|
||
(noPinB (intervalCard β ε) oB oE : ℤ) + oB - 1 < (cELo oB oE : ℤ) + oE - 1
|
||
|
||
/-- `closedForm` decides `countFeasibleInt`. The genuine/genuine case routes
|
||
through `coe_coe_iff` and `exists_bounded_of_noPin_iff`, which replace the
|
||
existential over counts by the canonical candidate. -/
|
||
theorem countFeasibleInt_iff_closedForm (β ε : WithTop Key) (oB oE : ℤ) :
|
||
countFeasibleInt β ε oB oE ↔ closedForm β ε oB oE := by
|
||
unfold closedForm
|
||
by_cases hβ : β = ⊤
|
||
· subst hβ
|
||
by_cases hε : ε = ⊤
|
||
· subst hε; exact top_top_iff
|
||
· rw [ite_eq_left rfl, ite_eq_right hε]
|
||
cases ε with
|
||
| top => exact absurd rfl hε
|
||
| coe e => exact top_coe_iff
|
||
· cases β with
|
||
| top => exact absurd rfl hβ
|
||
| coe b =>
|
||
by_cases hε : ε = ⊤
|
||
· subst hε; rw [ite_eq_right hβ, ite_eq_left rfl]
|
||
exact coe_top_iff
|
||
· rw [ite_eq_right hβ, ite_eq_right hε]
|
||
cases ε with
|
||
| top => exact absurd rfl hε
|
||
| coe e =>
|
||
rw [coe_coe_iff]
|
||
exact exists_bounded_of_noPin_iff
|
||
|
||
/-- `countFeasibleInt` is decidable: `closedForm` is a quantifier-free Boolean
|
||
combination of interval cardinalities and integer comparisons. -/
|
||
instance instDecidableCountFeasibleInt (β ε : WithTop Key) (oB oE : ℤ) :
|
||
Decidable (countFeasibleInt β ε oB oE) :=
|
||
decidable_of_iff (closedForm β ε oB oE) (countFeasibleInt_iff_closedForm β ε oB oE).symm
|
||
|
||
/-- `countFeasible` is decidable. -/
|
||
instance instDecidableCountFeasible (β ε : WithTop Key) (oB oE : ℤ) :
|
||
Decidable (countFeasible β ε oB oE) :=
|
||
decidable_of_iff (countFeasibleInt β ε oB oE) (countFeasible_iff_int β ε oB oE).symm
|
||
|
||
/-- **A read is decidable as trivially empty.** The characterization
|
||
`alwaysEmpty_iff_not_countFeasible` and the decidability of `countFeasible` yield a
|
||
decision procedure for `AlwaysEmpty r` computable from the read alone. -/
|
||
instance instDecidableAlwaysEmpty (r : RangeRead) : Decidable (AlwaysEmpty r) :=
|
||
decidable_of_iff (¬ countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset)
|
||
(alwaysEmpty_iff_not_countFeasible r).symm
|
||
|
||
/-!
|
||
## The empty range for an always-empty read
|
||
|
||
`AlwaysEmpty r` is the exact, database-free characterization of reads that can
|
||
return no key, so it certifies the empty range for every such read: the empty
|
||
interval is sound and minimal whenever the read is trivially empty.
|
||
-/
|
||
|
||
/-- Under trivial emptiness, compatibility is exactly the empty complete result:
|
||
every database is compatible with `⟨[], false⟩`, and nothing else. -/
|
||
theorem compatible_iff_nil_of_alwaysEmpty {db : DB} {r : RangeRead} {res : Result}
|
||
(h : AlwaysEmpty r) :
|
||
RangeRead.Compatible db r res ↔ (res.more = false ∧ res.keys = []) := by
|
||
constructor
|
||
· intro hc
|
||
rcases hc.2 with ⟨hm, hpre, hne⟩ | ⟨hm, heq⟩
|
||
· rw [h db] at hpre
|
||
exact absurd (List.eq_nil_of_prefix_nil hpre) hne
|
||
· exact ⟨hm, by rw [heq, h db]⟩
|
||
· rintro ⟨hm, hk⟩
|
||
refine ⟨?_, Or.inr ⟨hm, ?_⟩⟩
|
||
· rw [hk]
|
||
unfold RangeRead.withinLimit
|
||
split <;> simp
|
||
· rw [hk, h db]
|
||
|
||
/-- The empty range is sound for an always-empty read: every database's
|
||
compatibility is the same empty-result condition, independent of the database. -/
|
||
theorem sound_emptyRange_of_alwaysEmpty {r : RangeRead} {res : Result}
|
||
(h : AlwaysEmpty r) : Sound r res (emptyRange r) := by
|
||
intro db₁ db₂ _
|
||
rw [compatible_iff_nil_of_alwaysEmpty (db := db₁) h,
|
||
compatible_iff_nil_of_alwaysEmpty (db := db₂) h]
|
||
|
||
/-- The empty range is minimal for an always-empty read: it is sound, and no proper
|
||
subrange exists because a range with no keys admits no separating key. -/
|
||
theorem minimal_emptyRange_of_alwaysEmpty {r : RangeRead} {res : Result}
|
||
(h : AlwaysEmpty r) : Minimal r res (emptyRange r) := by
|
||
refine ⟨sound_emptyRange_of_alwaysEmpty h, fun cr' hproper _ => ?_⟩
|
||
exact hproper.2 fun k hk => (not_mem_emptyRange r k hk).elim
|
||
|
||
/-- With trivial emptiness, the empty range is *the* minimal sound conflict range
|
||
up to set equality: it is minimal by `minimal_emptyRange_of_alwaysEmpty`, and every
|
||
minimal range denotes the same set by the unconditional `Minimal.toSet_eq`. -/
|
||
theorem unique_minimal_emptyRange_of_alwaysEmpty {r : RangeRead} {res : Result}
|
||
(h : AlwaysEmpty r) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal (minimal_emptyRange_of_alwaysEmpty h)
|
||
|
||
/-!
|
||
## The emptiness predicate `ψ` in full generality
|
||
|
||
The count criterion `alwaysEmpty_iff_not_countFeasible` decides emptiness, but the
|
||
concrete predicates used to build conflict ranges — `Crossed`'s
|
||
`completeKeys_eq_nil_iff_counts` at `begin.offset = 1`, and its `above`/`between`
|
||
form — fix the begin offset. This section derives the predicate `ψ` for *all*
|
||
offsets, purely from the clamped-anchor characterization, so no offset hypothesis is
|
||
needed.
|
||
|
||
Emptiness is exactly `clampIndex db r.begin ≥ clampIndex db r.end_`. Writing
|
||
`a = (c_B + oB - 1).toNat` and `e = (c_E + oE - 1).toNat` for the two signed
|
||
anchors, the clamped comparison is `¬ (min a n < min e n)`, and
|
||
`min_min_lt_iff` (`min a n < min e n ↔ a < e ∧ a < n`) turns its negation into the
|
||
two-disjunct predicate
|
||
|
||
`ψ(db) = (n ≤ a) ∨ (e ≤ a)`.
|
||
|
||
The first disjunct says the begin anchor reaches the database length; the second
|
||
says it already dominates the end anchor. At a positive offset, `toNat` splits as
|
||
`(c + o - 1).toNat = c + (o - 1).toNat`, giving the count form
|
||
`(n ≤ c_B + (oB-1).toNat) ∨ (c_E + (oE-1).toNat ≤ c_B + (oB-1).toNat)`, which at
|
||
`oB = 1` collapses to `(c_B = n) ∨ (c_E + (oE-1) ≤ c_B)` — the predicate of
|
||
`completeKeys_eq_nil_iff_counts`. So `ψ` unifies the offset-one, cross-free,
|
||
and crossed empty cases directly in terms of the read's own data.
|
||
|
||
Rewriting `ψ` through `above` and `between` is in
|
||
`completeKeys_eq_nil_iff_above_between`; the crossed soundness and candidate it
|
||
yields for all positive offsets are `sound_top_end_of_crossed_general`,
|
||
`sound_top_begin_of_short_general`, and `crossedRangeGeneral`, below.
|
||
|
||
With `b = (oB-1).toNat` and `g = (oE-1).toNat - (oB-1).toNat`, the crossed `ψ`
|
||
reads `(#stored ≥ B ≤ b) ∨ (#stored in [E,B) ≥ g)`. Both disjuncts are functions
|
||
of the keys at or above `E`, so `[E,⊤)` is sound; `[B,⊤)` is sound only in the
|
||
short gap `intervalCard E B < g`, where the second disjunct is unsatisfiable. The
|
||
long gap's necessity needs the slack `b`, not the offset-one empty database:
|
||
`necessary_of_begin_le_general` and `necessary_of_mem_interval_general` build the
|
||
witnesses, and `minimal_crossedRangeGeneral_of_empty` assembles them.
|
||
|
||
The cross-free sibling `completeKeys_eq_nil_iff_above_between_le` rewrites the
|
||
*same* `ψ` through `[B,E)` instead of `[E,B)`: for `B ≤ E` the count difference
|
||
`c_E - c_B` is the number of stored keys in `[B,E)`, so the predicate becomes
|
||
`(above B).length ≤ b ∨ #(between B E) + e ≤ b`, both disjuncts functions of the
|
||
keys at or above `B`. Soundness of `[B,⊤)` is therefore direct
|
||
(`sound_topRange_of_le_general`), and necessity (`necessary_of_begin_le_of_le_general`)
|
||
uses the slack `b` on keys at or above `E` while spending `[B,E)` keys to flip the
|
||
second disjunct. The `¬ AlwaysEmpty` hypothesis is essential — unlike the offset-one
|
||
case, a positive `B` can be genuine while `ψ` is vacuous (`e ≤ b`), and then the
|
||
minimal range is `∅`. `minimal_topRange_of_le_empty` assembles the general
|
||
certificate; `minimal_topRange_of_end_offset_ge_two_empty` is its `b = 0` case.
|
||
|
||
There is no order-reversing symmetry to transport the end-offset theory to the
|
||
begin side directly. `Key` is a finite-byte-string type with a least element and no
|
||
greatest, so an order reversal would have to send `[]` to a nonexistent greatest
|
||
key; byte complement reverses order only on fixed-length keys and preserves the
|
||
prefix order, so it fails to reverse `anchor`/index order across unequal lengths.
|
||
The extension is therefore direct, through the clamped comparison.
|
||
-/
|
||
|
||
/-- The clamped anchor index with the offset split off: at a positive offset the
|
||
signed anchor is the count below the selector key plus `offset - 1`, and `toNat`
|
||
distributes over the sum. -/
|
||
theorem clampIndex_eq_min_add (db : DB) (β : WithTop Key) (o : ℤ) (h : 1 ≤ o)
|
||
(c : ℕ) (hc : cnt db β = c) :
|
||
clampIndex db (⟨β, o⟩ : Selector) = min (c + (o - 1).toNat) db.keys.length := by
|
||
rw [clampIndex_eq db β o c hc]
|
||
congr 1
|
||
rw [show ((c : ℤ) + o - 1) = (c : ℤ) + (o - 1) by ring,
|
||
Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast]
|
||
|
||
/-- A count is at most the database length, so `n ≤ cnt db β` forces equality. -/
|
||
theorem cnt_eq_length_of_length_le (db : DB) (β : WithTop Key)
|
||
(h : db.keys.length ≤ cnt db β) : cnt db β = db.keys.length :=
|
||
le_antisymm (cnt_le_length db β) h
|
||
|
||
/-- **The emptiness predicate `ψ`, all offsets, raw form.** A complete result is
|
||
empty exactly when the begin anchor reaches the database length or dominates the end
|
||
anchor, in the raw `toNat` form. No hypothesis on either offset is needed: this is
|
||
just the negation of the clamped anchor comparison `min_min_lt_iff`. -/
|
||
theorem completeKeys_eq_nil_iff_psi_raw {db : DB} {r : RangeRead} :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
db.keys.length ≤ (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) ∨
|
||
(((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤
|
||
(((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) := by
|
||
have hkey : (RangeRead.completeKeys db r = []) ↔
|
||
¬ (clampIndex db r.begin < clampIndex db r.end_) := by
|
||
rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff]
|
||
rw [hkey,
|
||
clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl,
|
||
clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl,
|
||
min_min_lt_iff]
|
||
omega
|
||
|
||
/-- **The emptiness predicate `ψ`, positive offsets.** With both offsets at least
|
||
one, `ψ` takes the count form `(n ≤ c_B + (oB-1)) ∨ (c_E + (oE-1) ≤ c_B + (oB-1))`,
|
||
where `c_B, c_E` are the stored-key counts below the two selector keys. This
|
||
generalizes `completeKeys_eq_nil_iff_counts` (the `oB = 1` case, where
|
||
`oB - 1 = 0`) and hence both the cross-free and crossed empty cases. -/
|
||
theorem completeKeys_eq_nil_iff_psi {db : DB} {r : RangeRead}
|
||
(hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
db.keys.length ≤ cnt db r.begin.key + (r.begin.offset - 1).toNat ∨
|
||
cnt db r.end_.key + (r.end_.offset - 1).toNat ≤
|
||
cnt db r.begin.key + (r.begin.offset - 1).toNat := by
|
||
have hkey : (RangeRead.completeKeys db r = []) ↔
|
||
¬ (clampIndex db r.begin < clampIndex db r.end_) := by
|
||
rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff]
|
||
rw [hkey,
|
||
clampIndex_eq_min_add db r.begin.key r.begin.offset hB (cnt db r.begin.key) rfl,
|
||
clampIndex_eq_min_add db r.end_.key r.end_.offset hE (cnt db r.end_.key) rfl,
|
||
min_min_lt_iff]
|
||
omega
|
||
|
||
/-- At `begin.offset = 1` the general predicate recovers the existing count form
|
||
`completeKeys_eq_nil_iff_counts`: `(c_B = n) ∨ (c_E + (oE-1) ≤ c_B)`. -/
|
||
theorem completeKeys_eq_nil_iff_psi_one_begin {db : DB} {r : RangeRead}
|
||
(hB : r.begin.offset = 1) (hE : 1 ≤ r.end_.offset) :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
cnt db r.begin.key = db.keys.length ∨
|
||
cnt db r.end_.key + (r.end_.offset - 1).toNat ≤ cnt db r.begin.key := by
|
||
rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) (by omega) hE, hB]
|
||
norm_num
|
||
constructor
|
||
· rintro (h | h)
|
||
· exact Or.inl (le_antisymm (cnt_le_length db r.begin.key) h)
|
||
· exact Or.inr h
|
||
· rintro (h | h)
|
||
· exact Or.inl (le_of_eq h.symm)
|
||
· exact Or.inr h
|
||
|
||
/-- **`ψ` at offset one on both ends.** For `begin.offset = end.offset = 1`, a
|
||
complete result is empty exactly when no stored key reaches `begin.key` (`c_B = n`)
|
||
or the end count does not exceed the begin count (`c_E ≤ c_B`). -/
|
||
theorem completeKeys_eq_nil_iff_psi_offset_one {db : DB} {r : RangeRead}
|
||
(hB : r.begin.offset = 1) (hE : r.end_.offset = 1) :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
cnt db r.begin.key = db.keys.length ∨ cnt db r.end_.key ≤ cnt db r.begin.key := by
|
||
rw [completeKeys_eq_nil_iff_psi_one_begin (db := db) (r := r) hB (by omega), hE]
|
||
norm_num
|
||
|
||
/-- The stored keys at or above `E`. -/
|
||
def above (db : DB) (E : WithTop Key) : List Key :=
|
||
db.keys.filter (fun (k : Key) => decide (E ≤ (k : WithTop Key)))
|
||
|
||
/-- A key is stored at or above `E` exactly when it is stored and at or above `E`. -/
|
||
theorem mem_above {db : DB} {E : WithTop Key} {k : Key} :
|
||
k ∈ above db E ↔ k ∈ db.keys ∧ E ≤ (k : WithTop Key) := by
|
||
unfold above
|
||
rw [List.mem_filter, decide_eq_true_eq]
|
||
|
||
/-- The stored keys of the top range `[E, ⊤)` are the stored keys at or above `E`. -/
|
||
theorem keys_top_eq_above (db : DB) (E : WithTop Key) :
|
||
(ConflictRange.mk E ⊤).keys db = above db E := by
|
||
unfold above ConflictRange.keys
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact and_iff_left (WithTop.coe_lt_top k)
|
||
|
||
/-- The top range's stored keys are a function of the keys at or above `E`. -/
|
||
theorem above_eq_filter (db : DB) {E : WithTop Key} :
|
||
above db E = db.keys.filter (fun (k : Key) => decide (E ≤ (k : WithTop Key))) := rfl
|
||
|
||
/-- For `E ≤ B`, the keys at or above `B` are the `B`-filter of the keys at or
|
||
above `E`. -/
|
||
theorem above_B_eq_filter {db : DB} {E B : WithTop Key} (hEB : E ≤ B) :
|
||
above db B = (above db E).filter (fun (k : Key) => decide (B ≤ (k : WithTop Key))) := by
|
||
rw [above_eq_filter, above_eq_filter, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases h : B ≤ (k : WithTop Key)
|
||
· simp [h, le_trans hEB h]
|
||
· simp [h]
|
||
|
||
/-- The keys in `[E, B)` are the `< B`-filter of the keys at or above `E`. -/
|
||
theorem between_eq_filter_above (db : DB) (E B : WithTop Key) :
|
||
between db E B = (above db E).filter (fun (k : Key) => decide ((k : WithTop Key) < B)) := by
|
||
rw [above_eq_filter]
|
||
unfold between
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases h : (k : WithTop Key) < B <;> by_cases h2 : E ≤ (k : WithTop Key) <;> simp [h, h2]
|
||
|
||
/-- No stored key reaches `B` exactly when all of them lie strictly below it. -/
|
||
theorem cnt_eq_length_iff_above_nil (db : DB) (B : WithTop Key) :
|
||
cnt db B = db.keys.length ↔ above db B = [] := by
|
||
rw [cnt, List.countP_eq_length, above, List.filter_eq_nil_iff]
|
||
constructor
|
||
· intro h a ha
|
||
simp only [decide_eq_true_eq]
|
||
exact not_le.mpr (of_decide_eq_true (h a ha))
|
||
· intro h a ha
|
||
have hh := h a ha
|
||
simp only [decide_eq_true_eq] at hh ⊢
|
||
exact lt_of_not_ge hh
|
||
|
||
/-- For `E ≤ B`, the count difference `c_B - c_E` is the number of stored keys in
|
||
`[E, B)`. -/
|
||
theorem cnt_sub_eq_between_length {db : DB} {E B : WithTop Key} (hEB : E ≤ B) :
|
||
cnt db B - cnt db E = (between db E B).length := by
|
||
have := cnt_eq_add_between db hEB
|
||
omega
|
||
|
||
/-- For `E ≤ B`, the second disjunct `c_E + k ≤ c_B` is exactly "`[E, B)` stores at
|
||
least `k` keys". -/
|
||
theorem cnt_add_le_iff_between {db : DB} {E B : WithTop Key} (hEB : E ≤ B) (k : ℕ) :
|
||
cnt db E + k ≤ cnt db B ↔ k ≤ (between db E B).length := by
|
||
rw [cnt_eq_add_between db hEB, Nat.add_le_add_iff_left]
|
||
|
||
/-- The number of stored keys in `[E, B)` is bounded by the interval cardinality. -/
|
||
theorem between_length_le_intervalCard {db : DB} {E B : WithTop Key} (hEB : E ≤ B) :
|
||
((between db E B).length : ℕ∞) ≤ intervalCard E B := by
|
||
have h := cnt_sub_le_intervalCard db hEB
|
||
rwa [cnt_sub_eq_between_length hEB] at h
|
||
|
||
/-- In the short-gap branch the second disjunct is never satisfied: fewer than `k`
|
||
keys can lie in `[E, B)`. -/
|
||
theorem between_length_lt_of_intervalCard_lt {db : DB} {E B : WithTop Key} (hEB : E ≤ B)
|
||
{k : ℕ} (hgap : intervalCard E B < (k : ℕ∞)) :
|
||
(between db E B).length < k := by
|
||
have h := between_length_le_intervalCard (db := db) hEB
|
||
have hlt : ((between db E B).length : ℕ∞) < (k : ℕ∞) := lt_of_le_of_lt h hgap
|
||
exact_mod_cast hlt
|
||
|
||
/-- The count below `B` plus the number of stored keys at or above `B` is the
|
||
database length: the two filters partition the stored keys. -/
|
||
private theorem countP_lt_add_filter_ge_length (l : List Key) (B : WithTop Key) :
|
||
l.countP (fun k => decide (((k : Key) : WithTop Key) < B)) +
|
||
(l.filter (fun k => decide (B ≤ ((k : Key) : WithTop Key)))).length =
|
||
l.length := by
|
||
have hperm := List.filter_append_perm
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < B)) l
|
||
have hcnt : l.countP (fun k => decide (((k : Key) : WithTop Key) < B)) =
|
||
(l.filter (fun k => decide (((k : Key) : WithTop Key) < B))).length :=
|
||
List.countP_eq_length_filter
|
||
have hge : l.filter (fun k => decide (B ≤ ((k : Key) : WithTop Key))) =
|
||
l.filter (fun k => !decide (((k : Key) : WithTop Key) < B)) := by
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [show (!decide (((k : Key) : WithTop Key) < B)) =
|
||
decide (¬ (((k : Key) : WithTop Key) < B)) by rw [decide_not],
|
||
decide_eq_decide]
|
||
exact not_lt.symm
|
||
rw [hcnt, hge, ← List.length_append]
|
||
exact hperm.length_eq
|
||
|
||
/-- The count below `B` plus the number of stored keys at or above `B` is the
|
||
database length: the two filters partition the stored keys. -/
|
||
theorem cnt_add_above_length (db : DB) (B : WithTop Key) :
|
||
cnt db B + (above db B).length = db.keys.length := by
|
||
rw [above, cnt]
|
||
exact countP_lt_add_filter_ge_length db.keys B
|
||
|
||
/-- **The `above`-form of the first `ψ` disjunct.** The begin anchor reaching the
|
||
database length, `n ≤ c_B + b`, is exactly "at most `b` stored keys lie at or above
|
||
`begin.key`". -/
|
||
theorem length_le_cnt_add_iff_above_le (db : DB) (B : WithTop Key) (b : ℕ) :
|
||
db.keys.length ≤ cnt db B + b ↔ (above db B).length ≤ b := by
|
||
have h := cnt_add_above_length db B
|
||
omega
|
||
|
||
/-- **The `between`-form of the second `ψ` disjunct, crossed case.** For
|
||
`E ≤ B`, the end anchor dominating the begin anchor, `c_E + e ≤ c_B + b`, is
|
||
exactly `e - b ≤ #(stored in [E, B))`, the count difference being the number of
|
||
stored keys in `[E, B)`. The natural-number subtraction handles `e < b` with no
|
||
positivity hypothesis on `e - b`. -/
|
||
theorem cnt_add_le_iff_between_tsub {db : DB} {E B : WithTop Key} (hEB : E ≤ B)
|
||
(b e : ℕ) :
|
||
cnt db E + e ≤ cnt db B + b ↔ e - b ≤ (between db E B).length := by
|
||
have h := cnt_eq_add_between db hEB
|
||
omega
|
||
|
||
/-- **The `between`-form of the second `ψ` disjunct, cross-free case.** For
|
||
`B ≤ E`, the end anchor dominating the begin anchor, `c_E + e ≤ c_B + b`, is
|
||
exactly `#(stored in [B, E)) + e ≤ b`, the count `c_E - c_B` being the number of
|
||
stored keys in `[B, E)`. -/
|
||
theorem cnt_add_le_iff_between_le {db : DB} {B E : WithTop Key} (hBE : B ≤ E)
|
||
(b e : ℕ) :
|
||
cnt db E + e ≤ cnt db B + b ↔ (between db B E).length + e ≤ b := by
|
||
have h := cnt_eq_add_between db hBE
|
||
omega
|
||
|
||
/-- **The general crossed emptiness predicate.** With both offsets at least one and
|
||
the selectors crossed (`end_.key ≤ begin_.key`), a complete result is empty exactly
|
||
when at most `(oB - 1).toNat` stored keys lie at or above `begin.key`, or the gap
|
||
`(oE - 1).toNat - (oB - 1).toNat` is at most the number of stored keys in
|
||
`[end_.key, begin_.key)`. This is the all-positive-offset extension of
|
||
`completeKeys_eq_nil_iff_above` (the `oB = 1`, so `(oB-1).toNat = 0` case)
|
||
and unifies the cross-free and crossed empty predicates. -/
|
||
theorem completeKeys_eq_nil_iff_above_between {db : DB} {r : RangeRead}
|
||
(hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset)
|
||
(hEB : r.end_.key ≤ r.begin.key) :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
(above db r.begin.key).length ≤ (r.begin.offset - 1).toNat ∨
|
||
(r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat ≤
|
||
(between db r.end_.key r.begin.key).length := by
|
||
rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hB hE,
|
||
length_le_cnt_add_iff_above_le, cnt_add_le_iff_between_tsub hEB]
|
||
|
||
/-- The keys in `[B, E)` are the `< E`-filter of the keys at or above `B`. -/
|
||
theorem between_le_eq_filter_above (db : DB) (B E : WithTop Key) :
|
||
between db B E = (above db B).filter (fun (k : Key) => decide ((k : WithTop Key) < E)) := by
|
||
rw [above_eq_filter]
|
||
unfold between
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hB : B ≤ (k : WithTop Key) <;> by_cases hE : (k : WithTop Key) < E <;> simp [hB, hE]
|
||
|
||
/-- **The cross-free `ψ` depends only on the keys at or above `B`.** With both
|
||
offsets positive and the selectors cross-free (`begin.key ≤ end_.key`), the second
|
||
disjunct of `ψ` is a function of `above db begin.key`, since `between begin.key
|
||
end_.key` is the `< end_.key`-filter of that list. -/
|
||
theorem completeKeys_eq_nil_iff_le_of_agree {db₁ db₂ : DB} {r : RangeRead}
|
||
(hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key)
|
||
(habove : above db₁ r.begin.key = above db₂ r.begin.key) :
|
||
(RangeRead.completeKeys db₁ r = [] ↔ RangeRead.completeKeys db₂ r = []) := by
|
||
have hbetween : between db₁ r.begin.key r.end_.key =
|
||
between db₂ r.begin.key r.end_.key := by
|
||
rw [between_le_eq_filter_above, between_le_eq_filter_above, habove]
|
||
rw [completeKeys_eq_nil_iff_psi (db := db₁) (r := r) hB hE,
|
||
completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hB hE,
|
||
length_le_cnt_add_iff_above_le, length_le_cnt_add_iff_above_le, habove,
|
||
cnt_add_le_iff_between_le hle, cnt_add_le_iff_between_le hle, hbetween]
|
||
|
||
/-- **The cross-free emptiness predicate, general positive offsets.** With
|
||
`begin.key ≤ end_.key`, a complete result is empty exactly when at most
|
||
`(begin.offset - 1).toNat` stored keys lie at or above `begin.key`, or the
|
||
`[begin.key, end_.key)` count plus `(end_.offset - 1).toNat` is at most
|
||
`(begin.offset - 1).toNat`. This is the cross-free sibling
|
||
of `completeKeys_eq_nil_iff_above_between`, with `[begin.key, end_.key)` in place of
|
||
`[end_.key, begin.key)`. -/
|
||
theorem completeKeys_eq_nil_iff_above_between_le {db : DB} {r : RangeRead}
|
||
(hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
(above db r.begin.key).length ≤ (r.begin.offset - 1).toNat ∨
|
||
(between db r.begin.key r.end_.key).length + (r.end_.offset - 1).toNat ≤
|
||
(r.begin.offset - 1).toNat := by
|
||
rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hB hE,
|
||
length_le_cnt_add_iff_above_le, cnt_add_le_iff_between_le hle]
|
||
|
||
/-- **Soundness of `[B, ⊤)` for the cross-free general predicate.** With the
|
||
selectors cross-free (`begin.key ≤ end_.key`), the predicate `ψ` of
|
||
`completeKeys_eq_nil_iff_above_between_le` is a function of the stored keys at or
|
||
above `begin.key`: `above begin.key` is exactly that set, and `between begin.key
|
||
end_.key` is its `< end_.key`-filter. Agreement on `[begin.key, ⊤)` therefore fixes
|
||
`ψ`, and with it whether the empty result is compatible, whatever the begin
|
||
offset. -/
|
||
theorem sound_topRange_of_le_general {r : RangeRead} {res : Result}
|
||
(hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) (hmore : res.more = false) (hempty : res.keys = []) :
|
||
Sound r res ⟨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 h : above db₁ r.begin.key = above db₂ r.begin.key := by
|
||
rw [← keys_top_eq_above, hagree, keys_top_eq_above]
|
||
rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil]
|
||
exact completeKeys_eq_nil_iff_le_of_agree hB hE hle h
|
||
|
||
/-- **Soundness of `[E, ⊤)` for the general crossed predicate.** With the selectors
|
||
crossed (`end_.key ≤ begin_.key`), the predicate `ψ` of
|
||
`completeKeys_eq_nil_iff_above_between` is a function of the stored keys at or above
|
||
`end_.key`: both `above begin.key` and `between end_.key begin.key` are filters of
|
||
`above end_.key` (`above_B_eq_filter`, `between_eq_filter_above`). Agreement on
|
||
`[end_.key, ⊤)` therefore fixes `ψ`, and with it whether the empty result is
|
||
compatible, whatever the begin offset. -/
|
||
theorem sound_top_end_of_crossed_general {r : RangeRead} {res : Result}
|
||
(hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset)
|
||
(hEB : r.end_.key ≤ r.begin.key) (hmore : res.more = false) (hempty : res.keys = []) :
|
||
Sound r res ⟨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 h : above db₁ r.end_.key = above db₂ r.end_.key := by
|
||
rw [← keys_top_eq_above, hagree, keys_top_eq_above]
|
||
have hB' : above db₁ r.begin.key = above db₂ r.begin.key := by
|
||
rw [above_B_eq_filter hEB, above_B_eq_filter hEB, h]
|
||
have hbet : between db₁ r.end_.key r.begin.key = between db₂ r.end_.key r.begin.key := by
|
||
rw [between_eq_filter_above, between_eq_filter_above, h]
|
||
rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_above_between (db := db₁) hB hE hEB,
|
||
completeKeys_eq_nil_iff_above_between (db := db₂) hB hE hEB, hB', hbet]
|
||
|
||
/-- **The general crossed candidate.** `[end_.key, ⊤)` in the long gap
|
||
(`(oE-1) - (oB-1) ≤ intervalCard end_.key begin_.key`) and `[begin_.key, ⊤)` in the
|
||
short gap. This extends `crossedRange` to all positive begin offsets: at
|
||
`begin.offset = 1` the gap `(oE-1) - 0` is `crossedRange`'s `(oE-1).toNat`, so the
|
||
two agree there (the bridge `crossedRangeGeneral_eq_crossedRange`, below). -/
|
||
def crossedRangeGeneral (r : RangeRead) : ConflictRange :=
|
||
if ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat) ≤
|
||
intervalCard r.end_.key r.begin.key
|
||
then ⟨r.end_.key, ⊤⟩ else ⟨r.begin.key, ⊤⟩
|
||
|
||
/-- In the short gap the second disjunct of the general crossed `ψ` is never
|
||
satisfiable: the gap `(oE-1) - (oB-1)` exceeds the interval cardinality, hence the
|
||
number of stored keys in `[E, B)`. -/
|
||
theorem between_length_lt_of_intervalCard_lt_tsub {db : DB} {E B : WithTop Key} (hEB : E ≤ B)
|
||
{b e : ℕ} (hgap : intervalCard E B < (e - b : ℕ)) :
|
||
(between db E B).length < e - b := by
|
||
have h := between_length_le_intervalCard (db := db) hEB
|
||
have hlt : ((between db E B).length : ℕ∞) < ((e - b : ℕ) : ℕ∞) :=
|
||
lt_of_le_of_lt h (by exact_mod_cast hgap)
|
||
exact_mod_cast hlt
|
||
|
||
/-- **Soundness of `[B, ⊤)` in the short gap, general offsets.** With
|
||
`intervalCard E B < (oE-1) - (oB-1)` the second disjunct of `ψ` is unsatisfiable, so
|
||
`ψ` reduces to `above B`'s length being at most `(oB-1).toNat`, a function of the
|
||
stored keys at or above `B`. This extends `sound_top_begin_of_short` to all
|
||
positive begin offsets. -/
|
||
theorem sound_top_begin_of_short_general {r : RangeRead} {res : Result}
|
||
(hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset)
|
||
(hEB : r.end_.key ≤ r.begin.key)
|
||
(hgap : intervalCard r.end_.key r.begin.key <
|
||
((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat))
|
||
(hmore : res.more = false) (hempty : res.keys = []) :
|
||
Sound r res ⟨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 h : above db₁ r.begin.key = above db₂ r.begin.key := by
|
||
rw [← keys_top_eq_above, hagree, keys_top_eq_above]
|
||
have red : ∀ db : DB, (RangeRead.completeKeys db r = [] ↔
|
||
(above db r.begin.key).length ≤ (r.begin.offset - 1).toNat) := by
|
||
intro db
|
||
rw [completeKeys_eq_nil_iff_above_between (db := db) hB hE hEB]
|
||
have hlt := between_length_lt_of_intervalCard_lt_tsub (db := db) hEB hgap
|
||
exact ⟨fun h => h.elim id fun hc => absurd hc (not_le.mpr hlt), Or.inl⟩
|
||
rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil,
|
||
red db₁, red db₂, h]
|
||
|
||
/-!
|
||
## The crossed empty case
|
||
|
||
The empty complete result at `begin.offset = 1`, `end_.offset ≥ 2`, with the
|
||
selectors *crossed* (`end_.key < begin.key`). The cross-free overshoot case is
|
||
`minimal_topRange_of_end_offset_ge_two_empty`; here the top range `[begin.key, ⊤)`
|
||
need not be sound, because emptiness can depend on the stored keys in
|
||
`[end_.key, begin.key)`.
|
||
|
||
Writing `B = begin.key`, `E = end_.key`, `oE = end_.offset`, `n = |db.keys|`,
|
||
`cₖ = #{stored < k}`, the empty result is compatible with a database exactly when
|
||
`ψ(db) = (c_B = n) ∨ (c_E + (oE - 1) ≤ c_B)`; equivalently `ψ` is the disjunction
|
||
"no stored key `≥ B`" or "at least `oE - 1` stored keys in `[E, B)`". Both
|
||
disjuncts depend only on the stored keys at or above `E`, so `[E, ⊤)` is sound.
|
||
The minimal range is `[E, ⊤)` in the *long gap* (`oE - 1 ≤ intervalCard E B`) and
|
||
`[B, ⊤)` in the *short gap* (`intervalCard E B < oE - 1`), where the second
|
||
disjunct is unsatisfiable and `ψ` reduces to "no stored key `≥ B`".
|
||
-/
|
||
|
||
/-- **The emptiness predicate.** At `begin.offset = 1` and `end_.offset ≥ 1`, an
|
||
empty complete result is compatible with `db` exactly when either no stored key
|
||
reaches `begin.key` (`c_B = n`) or the count difference is at least
|
||
`end_.offset - 1`. This is the formal counterpart of the predicate `ψ` derived in
|
||
the notes, and it holds regardless of the relative order of the selector keys. -/
|
||
theorem completeKeys_eq_nil_iff_counts {db : DB} {r : RangeRead}
|
||
(hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset) :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
cnt db r.begin.key = db.keys.length ∨
|
||
cnt db r.end_.key + (r.end_.offset - 1).toNat ≤ cnt db r.begin.key := by
|
||
have htoNat : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) =
|
||
cnt db r.end_.key + (r.end_.offset - 1).toNat := by
|
||
have h1 : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
rw [show ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) =
|
||
(cnt db r.end_.key : ℤ) + (r.end_.offset - 1) by ring,
|
||
Int.toNat_add (Int.natCast_nonneg _) h1, Int.toNat_natCast]
|
||
have key : (RangeRead.completeKeys db r = []) ↔
|
||
¬ (clampIndex db r.begin < clampIndex db r.end_) := by
|
||
rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff]
|
||
rw [key, not_lt, clampIndex_begin_eq hbegin, clampIndex_end_eq, htoNat,
|
||
Nat.min_eq_left (cnt_le_length db r.begin.key)]
|
||
constructor
|
||
· intro h
|
||
rcases min_le_iff.mp h with h | h
|
||
· exact Or.inr h
|
||
· exact Or.inl (le_antisymm (cnt_le_length db r.begin.key) h)
|
||
· rintro (h | h)
|
||
· rw [← h]; exact Nat.min_le_right _ _
|
||
· exact le_trans (Nat.min_le_left _ _) h
|
||
|
||
/-- Inserting a key strictly below `v` increments the count of stored keys below
|
||
`v`. -/
|
||
theorem cnt_insert_lt (db : DB) (x : Key) (hx : x ∉ db.keys) {v : WithTop Key}
|
||
(hxv : (x : WithTop Key) < v) :
|
||
cnt (db.insert x hx) v = cnt db v + 1 := by
|
||
unfold cnt
|
||
rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hx]
|
||
exact countP_insertIdx_of_lt db.sorted hxv
|
||
|
||
/-- Inserting a key not strictly below `v` leaves the count of stored keys below
|
||
`v` unchanged. -/
|
||
theorem cnt_insert_not_lt (db : DB) (x : Key) (hx : x ∉ db.keys) {v : WithTop Key}
|
||
(hxv : ¬ (x : WithTop Key) < v) :
|
||
cnt (db.insert x hx) v = cnt db v := by
|
||
unfold cnt
|
||
rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hx]
|
||
exact countP_insertIdx (l := db.keys) (fun k => decide ((k : WithTop Key) < v))
|
||
(db.keys.countP (fun k => decide (k < x))) x (decide_eq_false_iff_not.mpr hxv)
|
||
|
||
/-- For `E ≤ B`, the empty complete result at begin offset one is equivalent to:
|
||
no stored key reaches `B`, or the keys in `[E, B)` number at least `end_.offset - 1`.
|
||
The first disjunct is the `c_B = n` case, the second the count difference. -/
|
||
theorem completeKeys_eq_nil_iff_above {db : DB} {r : RangeRead}
|
||
(hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset)
|
||
(hEB : r.end_.key ≤ r.begin.key) :
|
||
RangeRead.completeKeys db r = [] ↔
|
||
above db r.begin.key = [] ∨
|
||
(r.end_.offset - 1).toNat ≤ (between db r.end_.key r.begin.key).length := by
|
||
rw [completeKeys_eq_nil_iff_counts hbegin hoE, cnt_eq_length_iff_above_nil,
|
||
cnt_add_le_iff_between hEB]
|
||
|
||
/-- In the short gap the second disjunct is never satisfied, so an empty complete
|
||
result is exactly "no stored key reaches `B`". -/
|
||
theorem completeKeys_eq_nil_iff_above_B {db : DB} {r : RangeRead}
|
||
(hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset)
|
||
(hEB : r.end_.key ≤ r.begin.key)
|
||
(hgap : intervalCard r.end_.key r.begin.key < ((r.end_.offset - 1).toNat : ℕ∞)) :
|
||
RangeRead.completeKeys db r = [] ↔ above db r.begin.key = [] := by
|
||
rw [completeKeys_eq_nil_iff_above hbegin hoE hEB]
|
||
have hlt := between_length_lt_of_intervalCard_lt (db := db) hEB hgap
|
||
constructor
|
||
· rintro (h | h)
|
||
· exact h
|
||
· exact absurd h (not_le.mpr hlt)
|
||
· exact Or.inl
|
||
|
||
/-- **Soundness of `[E, ⊤)` in the crossed case.** The empty result's compatibility
|
||
is the predicate `ψ`, which reads only the stored keys at or above `E`; agreement on
|
||
`[E, ⊤)` fixes exactly those. -/
|
||
theorem sound_top_end_of_crossed {r : RangeRead} {res : Result}
|
||
(hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset)
|
||
(hEB : r.end_.key ≤ r.begin.key) (hmore : res.more = false)
|
||
(hempty : res.keys = []) :
|
||
Sound r res ⟨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 h : above db₁ r.end_.key = above db₂ r.end_.key := by
|
||
rw [← keys_top_eq_above, hagree, keys_top_eq_above]
|
||
have hB : above db₁ r.begin.key = above db₂ r.begin.key := by
|
||
rw [above_B_eq_filter hEB, above_B_eq_filter hEB, h]
|
||
have hbet : between db₁ r.end_.key r.begin.key = between db₂ r.end_.key r.begin.key := by
|
||
rw [between_eq_filter_above, between_eq_filter_above, h]
|
||
rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_above hbegin hoE hEB, completeKeys_eq_nil_iff_above hbegin hoE hEB,
|
||
hB, hbet]
|
||
|
||
/-- **Soundness of `[B, ⊤)` in the short gap.** With `intervalCard E B < oE - 1` the
|
||
second disjunct of `ψ` is unsatisfiable, so `ψ` reduces to "no stored key reaches
|
||
`B`", which agreement on `[B, ⊤)` fixes. -/
|
||
theorem sound_top_begin_of_short {r : RangeRead} {res : Result}
|
||
(hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset)
|
||
(hEB : r.end_.key ≤ r.begin.key)
|
||
(hgap : intervalCard r.end_.key r.begin.key < ((r.end_.offset - 1).toNat : ℕ∞))
|
||
(hmore : res.more = false) (hempty : res.keys = []) :
|
||
Sound r res ⟨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 h : above db₁ r.begin.key = above db₂ r.begin.key := by
|
||
rw [← keys_top_eq_above, hagree, keys_top_eq_above]
|
||
rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_above_B hbegin hoE hEB hgap,
|
||
completeKeys_eq_nil_iff_above_B hbegin hoE hEB hgap]
|
||
rw [h]
|
||
|
||
/-- Any finite list of keys misses a key at or above `b`: the `|l| + 1` successor
|
||
keys `chain b 0, …, chain b |l|` are distinct and at or above `b`, so one of them
|
||
is unstored. -/
|
||
theorem exists_le_not_mem (l : List Key) (b : Key) : ∃ x : Key, b ≤ x ∧ x ∉ l := by
|
||
by_contra h
|
||
push Not at h
|
||
have hsub : (Finset.range (l.length + 1)).image (chain b) ⊆ l.toFinset := by
|
||
intro x hx
|
||
rw [Finset.mem_image] at hx
|
||
obtain ⟨i, _, rfl⟩ := hx
|
||
exact List.mem_toFinset.mpr (h (chain b i) (chain_le b i))
|
||
have hcard := Finset.card_le_card hsub
|
||
rw [Finset.card_image_of_injective _ (chain_injective b), Finset.card_range] at hcard
|
||
have := List.toFinset_card_le l
|
||
omega
|
||
|
||
/-- A read whose begin selector is keyed at `⊤` with positive offset is trivially
|
||
empty: `resolve begin` is the sentinel for every database, so the window is
|
||
empty. -/
|
||
theorem alwaysEmpty_of_begin_key_top {r : RangeRead} (hbegin : 1 ≤ r.begin.offset)
|
||
(htop : r.begin.key = (⊤ : WithTop Key)) : AlwaysEmpty r := by
|
||
intro db
|
||
have hres : resolve db r.begin = (⊤ : WithTop Key) :=
|
||
resolve_eq_top_of_key_eq_top htop hbegin
|
||
have hin : RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = [] := by
|
||
unfold RangeRead.inRange
|
||
rw [hres, List.filter_eq_nil_iff]
|
||
intro k _ hk
|
||
exact absurd (of_decide_eq_true hk).1 (not_le.mpr (WithTop.coe_lt_top k))
|
||
unfold RangeRead.completeKeys
|
||
rw [hin]
|
||
cases r.reverse <;> rfl
|
||
|
||
/-- A non-trivially-empty read with positive begin offset has a genuine begin key. -/
|
||
theorem begin_key_ne_top_of_not_alwaysEmpty {r : RangeRead} (hbegin : 1 ≤ r.begin.offset)
|
||
(h : ¬ AlwaysEmpty r) : r.begin.key ≠ (⊤ : WithTop Key) :=
|
||
fun htop => h (alwaysEmpty_of_begin_key_top hbegin htop)
|
||
|
||
/-- The empty database. -/
|
||
def emptyDB : DB := ⟨[], List.sortedLT_nil⟩
|
||
|
||
/-- The empty complete result. -/
|
||
def emptyRes : Result := { keys := [], more := false }
|
||
|
||
/-- The empty database's complete keys are empty, so it is compatible with the
|
||
empty result. -/
|
||
theorem compatible_emptyDB_emptyRes (r : RangeRead) :
|
||
RangeRead.Compatible emptyDB r emptyRes :=
|
||
compatible_empty_iff_complete_nil.mpr (by simp [emptyDB, RangeRead.completeKeys,
|
||
RangeRead.inRange])
|
||
|
||
/-- **Necessity at or above `B`.** Every key `k ≥ B` lies in every sound range: the
|
||
empty database is compatible with the empty result (no key reaches `B`), while
|
||
inserting `k` makes a key reach `B` and keeps the `[E, B)` count below `oE - 1`, so
|
||
the two databases, which differ only in the omitted `k`, disagree on compatibility.
|
||
No realizability hypothesis is needed: the empty database is the witness directly. -/
|
||
theorem necessary_of_begin_le_crossed {r : RangeRead} {res : Result}
|
||
(hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset)
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
{k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) :
|
||
Necessary r res k := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hk0 : k ∉ emptyDB.keys := by simp [emptyDB]
|
||
have hkB : ¬ (k : WithTop Key) < r.begin.key := not_lt.mpr hk
|
||
have hcntB : cnt (emptyDB.insert k hk0) r.begin.key = 0 := by
|
||
rw [cnt_insert_not_lt emptyDB k hk0 hkB]
|
||
simp [cnt, emptyDB]
|
||
have hlen : (emptyDB.insert k hk0).keys.length = 1 := by
|
||
rw [DB.insert_keys]; simp [emptyDB]
|
||
have hcomp1 : RangeRead.Compatible emptyDB r { keys := [], more := false } :=
|
||
compatible_emptyDB_emptyRes r
|
||
have hagree : AgreeOn cr emptyDB (emptyDB.insert k hk0) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr emptyDB hk0 hkcr).symm
|
||
have hcomp2 : RangeRead.Compatible (emptyDB.insert k hk0) r
|
||
{ keys := [], more := false } := (hs emptyDB _ hagree).mp hcomp1
|
||
have hnot2 : ¬ RangeRead.Compatible (emptyDB.insert k hk0) r
|
||
{ keys := [], more := false } := by
|
||
intro hc
|
||
rw [compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_counts hbegin (by omega)] at hc
|
||
have h1 : 1 ≤ (r.end_.offset - 1).toNat := by omega
|
||
rw [hcntB, hlen] at hc
|
||
omega
|
||
exact hnot2 hcomp2
|
||
|
||
/-- **Necessity inside `[E, B)`.** In the long-gap branch (`oE - 1 ≤ intervalCard E B`)
|
||
every genuine key `k` with `E ≤ k < B` is necessary. The witnesses are two
|
||
databases differing only in `k`: `db₂` holds `oE - 2` keys of `[E, B)` together with
|
||
a key `j ≥ B`, so `ψ(db₂)` fails on both disjuncts, while inserting `k` makes the
|
||
`[E, B)` count reach `oE - 1` and satisfies the second disjunct. -/
|
||
theorem necessary_of_mem_interval_crossed {r : RangeRead} {res : Result}
|
||
(hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset)
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hne : ¬ AlwaysEmpty r) (_hEB : r.end_.key ≤ r.begin.key)
|
||
(hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key r.begin.key)
|
||
{k : Key} (hkE : r.end_.key ≤ (k : WithTop Key))
|
||
(hkB : (k : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
have hBne : r.begin.key ≠ (⊤ : WithTop Key) :=
|
||
begin_key_ne_top_of_not_alwaysEmpty (by omega) hne
|
||
obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by
|
||
cases hkb : r.begin.key with
|
||
| top => exact absurd hkb hBne
|
||
| coe b => exact ⟨b, rfl⟩
|
||
have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by
|
||
intro htop
|
||
rw [htop] at hkE
|
||
exact not_le.mpr (WithTop.coe_lt_top k) hkE
|
||
obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (↑e : WithTop Key) := by
|
||
cases hke : r.end_.key with
|
||
| top => exact absurd hke hEne
|
||
| coe e => exact ⟨e, rfl⟩
|
||
have hkltb : k < b := by rw [hb] at hkB; exact WithTop.coe_lt_coe.mp hkB
|
||
have hke' : e ≤ k := by rw [he] at hkE; exact WithTop.coe_le_coe.mp hkE
|
||
have hee : e ≤ b := le_trans hke' (le_of_lt hkltb)
|
||
intro cr hs
|
||
by_contra hkcr
|
||
-- A database of `oE - 2` keys of `[E, B)` avoiding `k`.
|
||
set m : ℕ := (r.end_.offset - 2).toNat with hm
|
||
have hm1 : m + 1 = (r.end_.offset - 1).toNat := by
|
||
rw [hm]
|
||
have h3 : (0 : ℤ) ≤ r.end_.offset - 2 := by omega
|
||
have h4 : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
have : (((r.end_.offset - 2).toNat + 1 : ℕ) : ℤ) = (r.end_.offset - 1).toNat := by
|
||
rw [Nat.cast_add, Nat.cast_one, Int.toNat_of_nonneg h3, Int.toNat_of_nonneg h4]
|
||
ring
|
||
exact_mod_cast this
|
||
have hcard : (m + 1 : ℕ∞) ≤ intervalCard r.end_.key r.begin.key :=
|
||
calc (m + 1 : ℕ∞) = ((m + 1 : ℕ) : ℕ∞) := rfl
|
||
_ = ((r.end_.offset - 1).toNat : ℕ∞) := by rw [hm1]
|
||
_ ≤ intervalCard r.end_.key r.begin.key := hgap
|
||
obtain ⟨dbK, hdbKrange, hkK, hlenK⟩ :=
|
||
exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := m) hcard
|
||
have hdbKe : ∀ x ∈ dbK.keys, e ≤ x := by
|
||
intro x hx
|
||
have h := (hdbKrange x hx).1
|
||
rw [he] at h
|
||
exact WithTop.coe_le_coe.mp h
|
||
have hdbKb : ∀ x ∈ dbK.keys, x < b := by
|
||
intro x hx
|
||
have h := (hdbKrange x hx).2
|
||
rw [hb] at h
|
||
exact WithTop.coe_lt_coe.mp h
|
||
have hcntE : cnt dbK (↑e : WithTop Key) = 0 := by
|
||
rw [cnt_coe]
|
||
exact countP_lt_eq_zero_of_forall_ge hdbKe
|
||
have hcntB : cnt dbK (↑b : WithTop Key) = m := by
|
||
rw [cnt_coe, countP_lt_eq_length_of_forall_lt hdbKb, hlenK]
|
||
-- `j ≥ b`, not already stored.
|
||
obtain ⟨j, hjb, hjK⟩ := exists_le_not_mem dbK.keys b
|
||
-- `db₂ = dbK.insert j`.
|
||
have hjnotin : j ∉ dbK.keys := hjK
|
||
have hcntE₂ : cnt (dbK.insert j hjnotin) (↑e : WithTop Key) = 0 := by
|
||
rw [cnt_insert_not_lt dbK j hjnotin (v := (↑e : WithTop Key))]
|
||
· rw [hcntE]
|
||
· rw [WithTop.coe_lt_coe]; intro hlt
|
||
exact absurd hlt (not_lt.mpr (le_trans hee hjb))
|
||
have hcntB₂ : cnt (dbK.insert j hjnotin) (↑b : WithTop Key) = m := by
|
||
rw [cnt_insert_not_lt dbK j hjnotin (v := (↑b : WithTop Key))]
|
||
· rw [hcntB]
|
||
· rw [WithTop.coe_lt_coe]; exact not_lt.mpr hjb
|
||
have hlen₂ : (dbK.insert j hjnotin).keys.length = m + 1 := by
|
||
rw [DB.insert_keys, List.orderedInsert_length, hlenK]
|
||
-- `k` is absent from `db₂`.
|
||
have hk_db2 : k ∉ (dbK.insert j hjnotin).keys := by
|
||
rw [DB.mem_insert_iff hjnotin]
|
||
rintro (rfl | hk)
|
||
· exact absurd hkltb (not_lt.mpr hjb)
|
||
· exact hkK hk
|
||
-- `db₁ = db₂.insert k`.
|
||
have hcntB₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2)
|
||
(↑b : WithTop Key) = (r.end_.offset - 1).toNat := by
|
||
rw [cnt_insert_lt _ k hk_db2 (v := (↑b : WithTop Key)) (WithTop.coe_lt_coe.mpr hkltb),
|
||
hcntB₂]
|
||
exact hm1
|
||
have hcntE₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2)
|
||
(↑e : WithTop Key) = 0 := by
|
||
rw [cnt_insert_not_lt _ k hk_db2 (v := (↑e : WithTop Key))]
|
||
· exact hcntE₂
|
||
· rw [WithTop.coe_lt_coe]; exact not_lt.mpr hke'
|
||
-- `ψ(db₁)` holds by the second disjunct, so `db₁` is compatible.
|
||
have hψ₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2) r.end_.key +
|
||
(r.end_.offset - 1).toNat ≤
|
||
cnt ((dbK.insert j hjnotin).insert k hk_db2) r.begin.key := by
|
||
rw [hb, he, hcntE₁, hcntB₁, zero_add]
|
||
have hcomp₁ : RangeRead.Compatible ((dbK.insert j hjnotin).insert k hk_db2) r
|
||
{ keys := [], more := false } :=
|
||
compatible_empty_iff_complete_nil.mpr
|
||
((completeKeys_eq_nil_iff_counts hbegin (by omega)).mpr (Or.inr hψ₁))
|
||
-- `ψ(db₂)` fails, so `db₂` is incompatible.
|
||
have hnot₂ : ¬ RangeRead.Compatible (dbK.insert j hjnotin) r
|
||
{ keys := [], more := false } := by
|
||
intro hc
|
||
rw [compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_counts hbegin (by omega)] at hc
|
||
have hcntE₂' : cnt (dbK.insert j hjnotin) r.end_.key = 0 := by rw [he]; exact hcntE₂
|
||
have hcntB₂' : cnt (dbK.insert j hjnotin) r.begin.key = m := by rw [hb]; exact hcntB₂
|
||
rcases hc with hc | hc
|
||
· rw [hcntB₂', hlen₂] at hc; omega
|
||
· rw [hcntE₂', hcntB₂'] at hc; omega
|
||
have hagree : AgreeOn cr (dbK.insert j hjnotin) ((dbK.insert j hjnotin).insert k hk_db2) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr (dbK.insert j hjnotin) hk_db2 hkcr).symm
|
||
exact hnot₂ ((hs (dbK.insert j hjnotin) _ hagree).mpr hcomp₁)
|
||
|
||
/-- A genuine key `b` has infinitely many keys at or above it, so the interval
|
||
`[b, ⊤)` realizes any finite count. -/
|
||
theorem natCast_le_intervalCard_coe_top (b : Key) (m : ℕ) :
|
||
(m : ℕ∞) ≤ intervalCard (↑b : WithTop Key) (⊤ : WithTop Key) := by
|
||
rw [intervalCard_top_right (b := (↑b : WithTop Key)) WithTop.coe_ne_top]
|
||
exact le_top
|
||
|
||
/-- A database of `m` keys, all at or above `b`, avoiding the key `k`. Built from
|
||
`exists_db_keysIn_avoid` on the half-infinite interval `[b, ⊤)`. -/
|
||
theorem exists_db_ge_avoid (b : Key) (m : ℕ) (k : Key) :
|
||
∃ db : DB, (∀ x ∈ db.keys, b ≤ x) ∧ k ∉ db.keys ∧ db.keys.length = m := by
|
||
obtain ⟨db, hdb, hk, hlen⟩ :=
|
||
exists_db_keysIn_avoid (B := (↑b : WithTop Key)) (E := ⊤) (k := k) (m := m)
|
||
(natCast_le_intervalCard_coe_top b (m + 1))
|
||
refine ⟨db, fun x hx => ?_, hk, hlen⟩
|
||
have h := hdb x hx
|
||
rw [keysIn_coe_top] at h
|
||
exact h
|
||
|
||
/-- **Necessity at or above `B`, general positive offsets.** Every key
|
||
`k ≥ B` lies in every sound range. The witnesses are two databases differing only
|
||
in `k`: the base stores `b = (begin.offset-1).toNat` keys at or above `B` and
|
||
satisfies `ψ` on the first disjunct (`#stored ≥ B = b`), while inserting `k` pushes
|
||
that count to `b + 1` and leaves the `[E, B)` count at `0`, so both disjuncts fail
|
||
(`b + 1 > b` and `e > b`). The offset-one empty-database witness is the case
|
||
`b = 0`. -/
|
||
theorem necessary_of_begin_le_general {r : RangeRead} {res : Result}
|
||
(hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset)
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hne : ¬ AlwaysEmpty r) (hEB : r.end_.key ≤ r.begin.key)
|
||
{k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) :
|
||
Necessary r res k := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
have hBne : r.begin.key ≠ (⊤ : WithTop Key) :=
|
||
begin_key_ne_top_of_not_alwaysEmpty hbegin hne
|
||
obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by
|
||
cases hkb : r.begin.key with
|
||
| top => exact absurd hkb hBne
|
||
| coe b => exact ⟨b, rfl⟩
|
||
have hbk : b ≤ k := by rw [hb] at hk; exact WithTop.coe_le_coe.mp hk
|
||
rw [hb] at hEB
|
||
-- `¬ AlwaysEmpty` forces `(end.offset-1).toNat > (begin.offset-1).toNat`, else
|
||
-- the second disjunct is vacuous.
|
||
have heb : ¬ ((r.end_.offset - 1).toNat ≤ (r.begin.offset - 1).toNat) := by
|
||
intro hle
|
||
apply hne
|
||
intro db
|
||
rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hbegin (by omega)]
|
||
exact Or.inr (by
|
||
have hmono : cnt db r.end_.key ≤ cnt db r.begin.key :=
|
||
cnt_le_cnt_of_le db (by rw [hb]; exact hEB)
|
||
omega)
|
||
refine Necessary.of_readable ⟨emptyDB, compatible_emptyDB_emptyRes r⟩ ?_
|
||
intro db₀ _ cr hs
|
||
by_contra hkcr
|
||
-- Base: `(begin.offset-1).toNat` keys at or above `B`, avoiding `k`.
|
||
obtain ⟨db₂, hdb₂, hk₂, hlen₂⟩ :=
|
||
exists_db_ge_avoid b ((r.begin.offset - 1).toNat) k
|
||
have hcntB₂ : cnt db₂ r.begin.key = 0 := by
|
||
rw [hb, cnt_coe, List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (hdb₂ x hx)
|
||
have hcntE₂ : cnt db₂ r.end_.key = 0 := by
|
||
rw [cnt, List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (le_trans hEB (WithTop.coe_le_coe.mpr (hdb₂ x hx)))
|
||
have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key)]
|
||
· exact hcntB₂
|
||
· rw [hb]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hbk)
|
||
have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = 0 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key)]
|
||
· exact hcntE₂
|
||
· exact not_lt.mpr (le_trans hEB (WithTop.coe_le_coe.mpr hbk))
|
||
have hlen₁ : (db₂.insert k hk₂).keys.length = (r.begin.offset - 1).toNat + 1 := by
|
||
rw [DB.insert_keys, List.orderedInsert_length, hlen₂]
|
||
-- `ψ(db₂)` holds on the first disjunct; `ψ(db₁)` fails both.
|
||
have hψ₂ : RangeRead.completeKeys db₂ r = [] :=
|
||
(completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr
|
||
(Or.inl (by rw [hlen₂, hcntB₂]; omega))
|
||
have hbig₁ : ¬ ((db₂.insert k hk₂).keys.length ≤
|
||
cnt (db₂.insert k hk₂) r.begin.key + (r.begin.offset - 1).toNat) := by
|
||
rw [hlen₁, hcntB₁]; omega
|
||
have hnotψ₁ : ¬ (cnt (db₂.insert k hk₂) r.end_.key + (r.end_.offset - 1).toNat ≤
|
||
cnt (db₂.insert k hk₂) r.begin.key + (r.begin.offset - 1).toNat) := by
|
||
rw [hcntB₁, hcntE₁]
|
||
have := Nat.lt_of_not_le heb
|
||
omega
|
||
have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm
|
||
have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } :=
|
||
compatible_empty_iff_complete_nil.mpr hψ₂
|
||
have hcomp₁ : RangeRead.Compatible (db₂.insert k hk₂) r
|
||
{ keys := [], more := false } := (hs db₂ _ hagree).mp hcomp₂
|
||
have hψ₁ := (completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r)
|
||
hbegin (by omega)).mp ((compatible_empty_iff_complete_nil
|
||
(db := db₂.insert k hk₂)).mp hcomp₁)
|
||
exact hψ₁.elim hbig₁ hnotψ₁
|
||
|
||
/-- **Necessity inside `[E, B)`, general positive offsets.** In the long-gap branch
|
||
(`g := (oE-1) - (oB-1) ≤ intervalCard E B`) every genuine key `k` with `E ≤ k < B` is
|
||
necessary. The base database stores `g - 1` keys of `[E, B) ∖ {k}` together with
|
||
`b + 1` keys at or above `B` (where `b = (oB-1).toNat`), so it fails both disjuncts
|
||
of `ψ` (`f = g - 1 < g` and `#stored ≥ B = b + 1 > b`); inserting `k` brings `f` to
|
||
`g` and flips `ψ` true. The two databases differ only in `k`, so they agree on any
|
||
range omitting it. -/
|
||
theorem necessary_of_mem_interval_general {r : RangeRead} {res : Result}
|
||
(hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset)
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hne : ¬ AlwaysEmpty r) (hEB : r.end_.key ≤ r.begin.key)
|
||
(hgap : ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat : ℕ∞) ≤
|
||
intervalCard r.end_.key r.begin.key)
|
||
{k : Key} (hkE : r.end_.key ≤ (k : WithTop Key))
|
||
(hkB : (k : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
have hBne : r.begin.key ≠ (⊤ : WithTop Key) :=
|
||
begin_key_ne_top_of_not_alwaysEmpty hbegin hne
|
||
obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by
|
||
cases hkb : r.begin.key with
|
||
| top => exact absurd hkb hBne
|
||
| coe b => exact ⟨b, rfl⟩
|
||
have hkb : k < b := by rw [hb] at hkB; exact WithTop.coe_lt_coe.mp hkB
|
||
obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (↑e : WithTop Key) := by
|
||
cases hke : r.end_.key with
|
||
| top => rw [hke] at hkE; exact absurd hkE (not_le.mpr (WithTop.coe_lt_top k))
|
||
| coe e => exact ⟨e, rfl⟩
|
||
have hke : e ≤ k := by rw [he] at hkE; exact WithTop.coe_le_coe.mp hkE
|
||
have heb : e ≤ b := le_trans hke (le_of_lt hkb)
|
||
have hEB' : r.end_.key ≤ r.begin.key := hEB
|
||
rw [hb] at hEB
|
||
-- `g := (oE-1) - (oB-1) ≥ 1`.
|
||
obtain ⟨g, hg⟩ : ∃ g : ℕ, g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat :=
|
||
⟨_, rfl⟩
|
||
have hgpos : 1 ≤ g := by
|
||
have hlt : (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat :=
|
||
Nat.lt_of_not_le (by
|
||
intro hle
|
||
apply hne
|
||
intro db
|
||
rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hbegin (by omega)]
|
||
exact Or.inr (by
|
||
have hmono : cnt db r.end_.key ≤ cnt db r.begin.key :=
|
||
cnt_le_cnt_of_le db hEB'
|
||
omega))
|
||
rw [hg]; exact Nat.sub_pos_of_lt hlt
|
||
-- Room for `g - 1` interval keys: `g ≤ intervalCard E B`.
|
||
have hcardA : (((g - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by
|
||
rw [Nat.sub_add_cancel hgpos, hg]; exact hgap
|
||
obtain ⟨dbA, hdbA, hkA, hlenA⟩ :=
|
||
exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := g - 1)
|
||
hcardA
|
||
-- `b + 1` keys at or above `B`, avoiding `k`.
|
||
obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid b ((r.begin.offset - 1).toNat + 1) k
|
||
-- Combine: every `A`-key is below `B`, every `J`-key at or above it.
|
||
have hAe : ∀ x ∈ dbA.keys, e ≤ x := fun x hx => by
|
||
have h := (hdbA x hx).1
|
||
rw [he] at h
|
||
exact WithTop.coe_le_coe.mp h
|
||
have hAb : ∀ x ∈ dbA.keys, x < b := fun x hx => by
|
||
have h := (hdbA x hx).2
|
||
rw [hb] at h
|
||
exact WithTop.coe_lt_coe.mp h
|
||
have hcross : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y :=
|
||
fun x hx y hy => lt_of_lt_of_le (hAb x hx) (hdbJ y hy)
|
||
let ks : List Key := dbA.keys ++ dbJ.keys
|
||
have hsorted : ks.SortedLT := List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcross⟩
|
||
let db₂ : DB := ⟨ks, hsorted⟩
|
||
have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl
|
||
have hk₂ : k ∉ db₂.keys := by
|
||
rw [hkeys₂, List.mem_append]
|
||
rintro (h | h)
|
||
· exact hkA h
|
||
· exact hkJ h
|
||
have hlen₂ : db₂.keys.length = g + (r.begin.offset - 1).toNat := by
|
||
rw [hkeys₂, List.length_append, hlenA, hlenJ]
|
||
have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg
|
||
omega
|
||
have hcntB₂ : cnt db₂ r.begin.key = g - 1 := by
|
||
rw [hb, cnt_coe, hkeys₂, List.countP_append]
|
||
have h1 : dbA.keys.countP (fun x => decide (x < b)) = (g - 1) := by
|
||
rw [countP_lt_eq_length_of_forall_lt hAb, hlenA]
|
||
have h2 : dbJ.keys.countP (fun x => decide (x < b)) = 0 :=
|
||
countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx)
|
||
rw [h1, h2, Nat.add_zero]
|
||
have hcntE₂ : cnt db₂ r.end_.key = 0 := by
|
||
rw [he, cnt_coe, hkeys₂, List.countP_append]
|
||
rw [countP_lt_eq_zero_of_forall_ge (l := dbA.keys) hAe,
|
||
countP_lt_eq_zero_of_forall_ge (l := dbJ.keys)
|
||
(fun x hx => le_trans heb (hdbJ x hx)), Nat.add_zero]
|
||
-- `ψ(db₂)` fails both disjuncts.
|
||
have hbig₂ : ¬ (db₂.keys.length ≤ cnt db₂ r.begin.key + (r.begin.offset - 1).toNat) := by
|
||
rw [hlen₂, hcntB₂]
|
||
have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg
|
||
omega
|
||
have hnotψ₂ : ¬ (cnt db₂ r.end_.key + (r.end_.offset - 1).toNat ≤
|
||
cnt db₂ r.begin.key + (r.begin.offset - 1).toNat) := by
|
||
rw [hcntE₂, hcntB₂]
|
||
have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg
|
||
omega
|
||
-- `db₁ = db₂.insert k`.
|
||
have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = g := by
|
||
rw [cnt_insert_lt db₂ k hk₂ (v := r.begin.key) (by rw [hb]; exact WithTop.coe_lt_coe.mpr hkb),
|
||
hcntB₂]
|
||
omega
|
||
have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = 0 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key)
|
||
(by rw [he]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hke)),
|
||
hcntE₂]
|
||
have hlen₁ : (db₂.insert k hk₂).keys.length = db₂.keys.length + 1 := by
|
||
rw [DB.insert_keys, List.orderedInsert_length]
|
||
-- `ψ(db₁)` holds on the second disjunct.
|
||
have hψ₁ : (cnt (db₂.insert k hk₂) r.end_.key + (r.end_.offset - 1).toNat ≤
|
||
cnt (db₂.insert k hk₂) r.begin.key + (r.begin.offset - 1).toNat) := by
|
||
rw [hcntE₁, hcntB₁]
|
||
have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg
|
||
omega
|
||
-- `db₂.insert k` is compatible (`ψ` true on the second disjunct); toggling `k`
|
||
-- out yields the incompatible `db₂`, so any sound range must contain `k`.
|
||
have hcomp₀ : RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } :=
|
||
compatible_empty_iff_complete_nil.mpr
|
||
((completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)).mpr
|
||
(Or.inr hψ₁))
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm
|
||
have hcomp₁ : RangeRead.Compatible db₂ r { keys := [], more := false } :=
|
||
(hs db₂ (db₂.insert k hk₂) hagree).mpr hcomp₀
|
||
rw [compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)] at hcomp₁
|
||
exact hcomp₁.elim hbig₂ hnotψ₂
|
||
|
||
/-- **The candidate range for the crossed empty case.** `[E, ⊤)` in the long gap
|
||
(`oE - 1 ≤ intervalCard E B`) and `[B, ⊤)` in the short gap. -/
|
||
def crossedRange (r : RangeRead) : ConflictRange :=
|
||
if ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key r.begin.key
|
||
then ⟨r.end_.key, ⊤⟩ else ⟨r.begin.key, ⊤⟩
|
||
|
||
/-- At `begin.offset = 1` the all-positive-offset `crossedRangeGeneral` agrees with
|
||
`crossedRange`. -/
|
||
theorem crossedRangeGeneral_eq_crossedRange {r : RangeRead} (hB : r.begin.offset = 1) :
|
||
crossedRangeGeneral r = crossedRange r := by
|
||
unfold crossedRangeGeneral crossedRange
|
||
rw [hB]
|
||
norm_num
|
||
|
||
/-- **Minimality of the crossed candidate.** In the crossed empty case the range
|
||
`crossedRange r` is the minimal sound conflict range: `[E, ⊤)` in the long gap,
|
||
where both disjuncts of `ψ` are functions of the stored keys at or above `E`, and
|
||
`[B, ⊤)` in the short gap, where `ψ` collapses to "no stored key reaches `B`". -/
|
||
theorem minimal_crossedRange_of_empty {r : RangeRead} {res : Result}
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset)
|
||
(hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) :
|
||
Minimal r res (crossedRange r) := by
|
||
have hEB : r.end_.key ≤ r.begin.key := le_of_lt hcross
|
||
unfold crossedRange
|
||
by_cases hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key r.begin.key
|
||
· rw [ite_eq_left hgap]
|
||
refine (minimal_iff_sound_and_necessaryRange).mpr
|
||
⟨sound_top_end_of_crossed hbegin (by omega) hEB hmore hempty, ?_⟩
|
||
intro k hkE _
|
||
by_cases hkB : (k : WithTop Key) < r.begin.key
|
||
· exact necessary_of_mem_interval_crossed hbegin hoE hmore hempty hne hEB hgap hkE hkB
|
||
· exact necessary_of_begin_le_crossed hbegin hoE hmore hempty (le_of_not_gt hkB)
|
||
· rw [ite_eq_right hgap]
|
||
refine (minimal_iff_sound_and_necessaryRange).mpr
|
||
⟨sound_top_begin_of_short hbegin (by omega) hEB (not_le.mp hgap) hmore hempty, ?_⟩
|
||
intro k hkB _
|
||
exact necessary_of_begin_le_crossed hbegin hoE hmore hempty hkB
|
||
|
||
/-- The minimal crossed range is unique up to set equality. -/
|
||
theorem unique_minimal_crossedRange_of_empty {r : RangeRead} {res : Result}
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset)
|
||
(hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal
|
||
(minimal_crossedRange_of_empty hmore hempty hbegin hoE hcross hne)
|
||
|
||
/-- **Minimality of the crossed candidate, all positive offsets.** The
|
||
all-offset extension of `minimal_crossedRange_of_empty`: in the crossed empty case
|
||
with `begin.offset ≥ 1` and `end.offset ≥ 2`, the range `crossedRangeGeneral r` is
|
||
the minimal sound conflict range — `[E, ⊤)` in the long gap
|
||
(`(oE-1) - (oB-1) ≤ intervalCard E B`) and `[B, ⊤)` in the short gap. Soundness is
|
||
`sound_top_end_of_crossed_general`/`sound_top_begin_of_short_general`; necessity of
|
||
a key `≥ E` splits at `B`, using `necessary_of_mem_interval_general` inside `[E, B)`
|
||
and `necessary_of_begin_le_general` at or above it. -/
|
||
theorem minimal_crossedRangeGeneral_of_empty {r : RangeRead} {res : Result}
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset)
|
||
(hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) :
|
||
Minimal r res (crossedRangeGeneral r) := by
|
||
have hEB : r.end_.key ≤ r.begin.key := le_of_lt hcross
|
||
unfold crossedRangeGeneral
|
||
by_cases hgap : ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat) ≤
|
||
intervalCard r.end_.key r.begin.key
|
||
· rw [ite_eq_left hgap]
|
||
refine (minimal_iff_sound_and_necessaryRange).mpr
|
||
⟨sound_top_end_of_crossed_general hbegin (by omega) hEB hmore hempty, ?_⟩
|
||
intro k hkE _
|
||
by_cases hkB : (k : WithTop Key) < r.begin.key
|
||
· exact necessary_of_mem_interval_general hbegin hoE hmore hempty hne hEB hgap hkE hkB
|
||
· exact necessary_of_begin_le_general hbegin hoE hmore hempty hne hEB (le_of_not_gt hkB)
|
||
· rw [ite_eq_right hgap]
|
||
refine (minimal_iff_sound_and_necessaryRange).mpr
|
||
⟨sound_top_begin_of_short_general hbegin (by omega) hEB (not_le.mp hgap) hmore hempty, ?_⟩
|
||
intro k hkB _
|
||
exact necessary_of_begin_le_general hbegin hoE hmore hempty hne hEB hkB
|
||
|
||
/-- The minimal general crossed range is unique up to set equality. -/
|
||
theorem unique_minimal_crossedRangeGeneral_of_empty {r : RangeRead} {res : Result}
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset)
|
||
(hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal
|
||
(minimal_crossedRangeGeneral_of_empty hmore hempty hbegin hoE hcross hne)
|
||
|
||
/-- **Necessity at or above `B`, cross-free, general positive offsets.** Every
|
||
genuine key `k ≥ begin.key` lies in every sound range, when `begin.key ≤ end.key`,
|
||
`begin.offset ≥ 1`, `end.offset ≥ 2` and the read is not trivially empty. The
|
||
witnesses differ only in `k`: they store some keys in `[B, E)` and the slack
|
||
`b = (oB-1).toNat` worth of keys at or above `E`, and inserting `k` flips exactly
|
||
one disjunct of the cross-free predicate
|
||
`(above B).length ≤ b ∨ #(between B E) + e ≤ b`. -/
|
||
theorem necessary_of_begin_le_of_le_general {r : RangeRead} {res : Result}
|
||
(hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset)
|
||
(hmore : res.more = false) (hempty : res.keys = [])
|
||
(hne : ¬ AlwaysEmpty r) (hle : r.begin.key ≤ r.end_.key)
|
||
{k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) :
|
||
Necessary r res k := by
|
||
have hres : res = { keys := [], more := false } := by
|
||
cases res with
|
||
| mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl
|
||
subst hres
|
||
have hBne : r.begin.key ≠ (⊤ : WithTop Key) :=
|
||
begin_key_ne_top_of_not_alwaysEmpty hbegin hne
|
||
obtain ⟨b₀, hb₀⟩ : ∃ b₀ : Key, r.begin.key = (↑b₀ : WithTop Key) := by
|
||
cases h : r.begin.key with
|
||
| top => exact absurd h hBne
|
||
| coe b₀ => exact ⟨b₀, rfl⟩
|
||
have hbk : b₀ ≤ k := by rw [hb₀] at hk; exact WithTop.coe_le_coe.mp hk
|
||
have hkb : ¬ (k : WithTop Key) < r.begin.key := by
|
||
rw [hb₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hbk)
|
||
set b : ℕ := (r.begin.offset - 1).toNat with hbdef
|
||
set e : ℕ := (r.end_.offset - 1).toNat with hedef
|
||
have he1 : 1 ≤ e := by omega
|
||
intro cr hs
|
||
by_contra hkcr
|
||
by_cases hEtop : r.end_.key = ⊤
|
||
· -- `E = ⊤`: `ψ` reads `#(stored ≥ B) ≤ b`.
|
||
obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid b₀ b k
|
||
let db₂ : DB := ⟨dbJ.keys, dbJ.sorted⟩
|
||
have hkeys₂ : db₂.keys = dbJ.keys := rfl
|
||
have hk₂ : k ∉ db₂.keys := by rw [hkeys₂]; exact hkJ
|
||
have hcntB₂ : cnt db₂ r.begin.key = 0 := by
|
||
rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx)]
|
||
have hcntE₂ : cnt db₂ r.end_.key = b := by
|
||
rw [hEtop, cnt_top, hkeys₂, hlenJ]
|
||
have hlen₂ : db₂.keys.length = b := hlenJ
|
||
have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } :=
|
||
compatible_empty_iff_complete_nil.mpr
|
||
((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr
|
||
(Or.inl (by rw [hlen₂, hcntB₂]; omega)))
|
||
have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂]
|
||
have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by
|
||
rw [compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)]
|
||
rintro (hh | hh)
|
||
· rw [DB.insert_keys, List.orderedInsert_length, hlen₂, hcntB₁] at hh; omega
|
||
· rw [hEtop, cnt_top, DB.insert_keys, List.orderedInsert_length, hlen₂, hcntB₁] at hh
|
||
omega
|
||
have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm
|
||
exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂)
|
||
· -- `E = ↑e₀` genuine.
|
||
obtain ⟨e₀, he₀⟩ : ∃ e₀ : Key, r.end_.key = (↑e₀ : WithTop Key) := by
|
||
cases h : r.end_.key with
|
||
| top => exact absurd h hEtop
|
||
| coe e₀ => exact ⟨e₀, rfl⟩
|
||
have hbe₀ : b₀ ≤ e₀ := by
|
||
have hh := hle; rw [hb₀, he₀] at hh; exact WithTop.coe_le_coe.mp hh
|
||
rcases lt_or_ge b e with hlt_be | hge_be
|
||
· -- `b < e`: the second disjunct is always false, so `ψ` is `#(stored ≥ B) ≤ b`.
|
||
obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ b k
|
||
let db₂ : DB := ⟨dbJ.keys, dbJ.sorted⟩
|
||
have hkeys₂ : db₂.keys = dbJ.keys := rfl
|
||
have hk₂ : k ∉ db₂.keys := by rw [hkeys₂]; exact hkJ
|
||
have hlen₂ : db₂.keys.length = b := by rw [hkeys₂, hlenJ]
|
||
have hcntB₂ : cnt db₂ r.begin.key = 0 := by
|
||
rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge]
|
||
intro x hx
|
||
exact le_trans hbe₀ (hdbJ x hx)
|
||
have hcntE₂ : cnt db₂ r.end_.key = 0 := by
|
||
rw [he₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx)]
|
||
have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by
|
||
rw [DB.insert_keys, List.orderedInsert_length, hlen₂]
|
||
have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂]
|
||
have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } :=
|
||
compatible_empty_iff_complete_nil.mpr
|
||
((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr
|
||
(Or.inl (by rw [hlen₂, hcntB₂]; omega)))
|
||
have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by
|
||
rw [compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)]
|
||
rintro (hh | hh)
|
||
· rw [hlen₁, hcntB₁] at hh; omega
|
||
· rw [hcntB₁] at hh
|
||
by_cases hkE : r.end_.key ≤ (k : WithTop Key)
|
||
· rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) (by
|
||
rw [he₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr
|
||
(by rw [he₀] at hkE; exact WithTop.coe_le_coe.mp hkE))), hcntE₂] at hh
|
||
omega
|
||
· have hke₀ : k < e₀ := by
|
||
rw [he₀] at hkE; exact WithTop.coe_lt_coe.mp (lt_of_not_ge hkE)
|
||
rw [cnt_insert_lt db₂ k hk₂ (v := r.end_.key) (by
|
||
rw [he₀]; exact WithTop.coe_lt_coe.mpr hke₀), hcntE₂] at hh
|
||
omega
|
||
have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm
|
||
exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂)
|
||
· -- `e ≤ b`.
|
||
have hed : e ≤ b := hge_be
|
||
have hroom := tsub_add_one_le_intervalCard_of_le_general hbegin hoE hle hne hed
|
||
by_cases hkE : r.end_.key ≤ (k : WithTop Key)
|
||
· -- `k ≥ E`: `b - e + 1` keys of `[B, E)` and `e - 1` keys `≥ E`.
|
||
have hke₀ : e₀ ≤ k := by rw [he₀] at hkE; exact WithTop.coe_le_coe.mp hkE
|
||
have hkEnlt : ¬ (k : WithTop Key) < r.end_.key := by
|
||
rw [he₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hke₀)
|
||
obtain ⟨dbA, hdbA, hlenA⟩ :=
|
||
exists_db_keysIn (B := r.begin.key) (E := r.end_.key) (m := b - e + 1)
|
||
(by simpa only [← hbdef, ← hedef] using hroom)
|
||
have hAhi : ∀ x ∈ dbA.keys, x < e₀ := fun x hx => by
|
||
have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).2
|
||
have hAlo : ∀ x ∈ dbA.keys, b₀ ≤ x := fun x hx => by
|
||
have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).1
|
||
have hAk : k ∉ dbA.keys := fun h => absurd (hAhi k h) (not_lt.mpr hke₀)
|
||
obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ (e - 1) k
|
||
have hcrossJ : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y :=
|
||
fun x hx y hy => lt_of_lt_of_le (hAhi x hx) (hdbJ y hy)
|
||
let db₂ : DB := ⟨dbA.keys ++ dbJ.keys,
|
||
List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcrossJ⟩⟩
|
||
have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl
|
||
have hk₂ : k ∉ db₂.keys := by
|
||
rw [hkeys₂, List.mem_append]; rintro (h | h); exacts [hAk h, hkJ h]
|
||
have hlen₂ : db₂.keys.length = b := by
|
||
rw [hkeys₂, List.length_append, hlenA, hlenJ]; simp only [hbdef, hedef]; omega
|
||
have hcntB₂ : cnt db₂ r.begin.key = 0 := by
|
||
rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge]
|
||
intro x hx
|
||
rw [List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· exact hAlo x hx
|
||
· exact le_trans hbe₀ (hdbJ x hx)
|
||
have hcntE₂ : cnt db₂ r.end_.key = b - e + 1 := by
|
||
rw [he₀, cnt_coe, hkeys₂, List.countP_append,
|
||
countP_lt_eq_length_of_forall_lt hAhi, hlenA,
|
||
countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx), Nat.add_zero]
|
||
have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂]
|
||
have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = b - e + 1 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) hkEnlt, hcntE₂]
|
||
have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by
|
||
rw [DB.insert_keys, List.orderedInsert_length, hlen₂]
|
||
have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } :=
|
||
compatible_empty_iff_complete_nil.mpr
|
||
((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr
|
||
(Or.inl (by rw [hlen₂, hcntB₂]; omega)))
|
||
have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by
|
||
rw [compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)]
|
||
rintro (hh | hh)
|
||
· rw [hlen₁, hcntB₁] at hh; omega
|
||
· rw [hcntE₁, hcntB₁, hbdef, hedef] at hh; omega
|
||
have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm
|
||
exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂)
|
||
· -- `k ∈ [B, E)`: `b - e` keys of `[B, E) \ {k}` and `e` keys `≥ E`.
|
||
have hke₀ : k < e₀ := by
|
||
rw [he₀] at hkE; exact WithTop.coe_lt_coe.mp (lt_of_not_ge hkE)
|
||
have hkElt : (k : WithTop Key) < r.end_.key := by
|
||
rw [he₀]; exact WithTop.coe_lt_coe.mpr hke₀
|
||
obtain ⟨dbA, hdbA, hkA, hlenA⟩ :=
|
||
exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := k) (m := b - e)
|
||
(by simpa only [Nat.cast_add, Nat.cast_one, hbdef, hedef] using hroom)
|
||
have hAhi : ∀ x ∈ dbA.keys, x < e₀ := fun x hx => by
|
||
have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).2
|
||
have hAlo : ∀ x ∈ dbA.keys, b₀ ≤ x := fun x hx => by
|
||
have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).1
|
||
obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ e k
|
||
have hcrossJ : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y :=
|
||
fun x hx y hy => lt_of_lt_of_le (hAhi x hx) (hdbJ y hy)
|
||
let db₂ : DB := ⟨dbA.keys ++ dbJ.keys,
|
||
List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcrossJ⟩⟩
|
||
have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl
|
||
have hk₂ : k ∉ db₂.keys := by
|
||
rw [hkeys₂, List.mem_append]; rintro (h | h); exacts [hkA h, hkJ h]
|
||
have hlen₂ : db₂.keys.length = b := by
|
||
rw [hkeys₂, List.length_append, hlenA, hlenJ]; simp only [hbdef, hedef]; omega
|
||
have hcntB₂ : cnt db₂ r.begin.key = 0 := by
|
||
rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge]
|
||
intro x hx
|
||
rw [List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· exact hAlo x hx
|
||
· exact le_trans hbe₀ (hdbJ x hx)
|
||
have hcntE₂ : cnt db₂ r.end_.key = b - e := by
|
||
rw [he₀, cnt_coe, hkeys₂, List.countP_append,
|
||
countP_lt_eq_length_of_forall_lt hAhi, hlenA,
|
||
countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx), Nat.add_zero]
|
||
have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by
|
||
rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂]
|
||
have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = b - e + 1 := by
|
||
rw [cnt_insert_lt db₂ k hk₂ (v := r.end_.key) hkElt, hcntE₂]
|
||
have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by
|
||
rw [DB.insert_keys, List.orderedInsert_length, hlen₂]
|
||
have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } :=
|
||
compatible_empty_iff_complete_nil.mpr
|
||
((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr
|
||
(Or.inr (by rw [hcntE₂, hcntB₂, hbdef, hedef]; omega)))
|
||
have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by
|
||
rw [compatible_empty_iff_complete_nil,
|
||
completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)]
|
||
rintro (hh | hh)
|
||
· rw [hlen₁, hcntB₁] at hh; omega
|
||
· rw [hcntE₁, hcntB₁, hbdef, hedef] at hh; omega
|
||
have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm
|
||
exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂)
|
||
|
||
/-- **The cross-free empty result at all positive begin offsets.** With
|
||
`begin.offset ≥ 1`, `end.offset ≥ 2`, `begin.key ≤ end.key` and a non-trivially-empty
|
||
read, the top range `[begin.key, ⊤)` is the minimal sound conflict range for the
|
||
empty complete result. Soundness is `sound_topRange_of_le_general`; necessity is
|
||
`necessary_of_begin_le_of_le_general`. At `begin.offset = 1` this is
|
||
`minimal_topRange_of_end_offset_ge_two_empty`. -/
|
||
theorem minimal_topRange_of_le_empty {r : RangeRead} {res : Result}
|
||
(_hread : Readable r res) (hmore : res.more = false) (_hrev : r.reverse = false)
|
||
(hempty : res.keys = []) (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r) :
|
||
Minimal r res (topRange r) := by
|
||
refine (minimal_iff_sound_and_necessaryRange).mpr
|
||
⟨sound_topRange_of_le_general hbegin (by omega) hle hmore hempty, ?_⟩
|
||
intro k hlo _
|
||
exact necessary_of_begin_le_of_le_general hbegin hend hmore hempty hne hle hlo
|
||
|
||
/-- The minimal top range for an empty complete result at all positive begin
|
||
offsets is unique up to set equality. -/
|
||
theorem unique_minimal_topRange_of_le_empty {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false)
|
||
(hempty : res.keys = []) (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset)
|
||
(hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal
|
||
(minimal_topRange_of_le_empty hread hmore hrev hempty hbegin hend hle hne)
|
||
|
||
end FormalConflictRange
|