2508 lines
130 KiB
Lean4
2508 lines
130 KiB
Lean4
/-
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
-/
|
||
import FormalConflictRange.NegCrossCompleteResidual
|
||
|
||
/-!
|
||
# The crossed negative-begin complete high cell
|
||
|
||
This module closes the remaining crossed negative-begin complete cells with
|
||
`2 ≤ end.offset` and `end.key < head`: the end selector key lies strictly below every
|
||
reported key.
|
||
|
||
**The four-branch `max` rule is false.** An earlier version of this module advertised
|
||
the minimal range as `[⊥, max begin.key (keyAfter last))` in the clamping finite cell,
|
||
`[⊥, ⊤)` in the clamping low cell, and `[end.key, …)` in the nonclamping cells. The
|
||
clamping finite branch is genuinely capacity-dependent, not `max`-shaped. With `L` the
|
||
last reported key, `q = beginSlack` and `r = reportedBelowBegin`, put `C = q + 1 - r`
|
||
(so `C ≥ 1` in the clamping cell). The upper endpoint is `begin.key` when the interval
|
||
`[keyAfter L, begin.key)` holds `C` keys (or `C + 1` keys when `end.key = ⊥`), and
|
||
`keyAfter L` otherwise. The witness `res = [[5], [6]]`, `end.key = [1]`,
|
||
`begin.key = [6,0,0]`, `begin.offset = -3`, `end.offset = 3` refutes the rule:
|
||
every compatible database then has
|
||
`begin.key = [6,0,0]` strictly above `keyAfter last = [6,0]`, but the extra key `[6,0]`
|
||
lies in no sound range, so the `max` rule over-collects it.
|
||
|
||
The present module proves the clamp / low-offset branch `[⊥, ⊤)`. The witness database
|
||
is the reported list itself: clamping makes the begin selector resolve to `⊥`, and
|
||
`t < e` makes the end selector resolve past the end of that list. Flipping any key
|
||
outside the reported list therefore either becomes the new minimum (below the head) or
|
||
joins the reported window (above the last), so it is necessary. The whole range is thus
|
||
minimal, and soundness is the trivial soundness of the full range.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
/-- The upper endpoint `M = max begin.key (keyAfter last)` of the crossed
|
||
negative-begin complete high cell. -/
|
||
noncomputable def negCrossCompleteHighM (r : RangeRead) (res : Result) : WithTop Key :=
|
||
max r.begin.key
|
||
((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)
|
||
|
||
/-- The crossed negative-begin complete high cell. -/
|
||
abbrev negCrossCompleteHigh (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
2 ≤ r.end_.offset ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)
|
||
|
||
/-- The excess `D = (end.offset - 1).toNat - #reported` of the end anchor over the
|
||
reported length in the low branch of the crossed negative-begin complete high cell. -/
|
||
def negCrossHighD (r : RangeRead) (res : Result) : ℕ :=
|
||
(r.end_.offset - 1).toNat - res.keys.length
|
||
|
||
/-- The number of keys available in the half-open gap `[end.key, head)` of the crossed
|
||
negative-begin complete high cell. -/
|
||
noncomputable def negCrossHighGapCap (r : RangeRead) (res : Result) : ℕ∞ :=
|
||
intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)
|
||
|
||
/-- In the nonclamping low branch the minimal range starts at `end.key` when the gap
|
||
`[end.key, head)` can hold the excess `D` and the begin key admits a key above it (or
|
||
when the gap strictly exceeds `D`). Otherwise it starts at the reported head. -/
|
||
noncomputable def negCrossHighUsesEnd (r : RangeRead) (res : Result) : Prop :=
|
||
((negCrossHighD r res : ℕ∞) < negCrossHighGapCap r res) ∨
|
||
((negCrossHighD r res : ℕ∞) ≤ negCrossHighGapCap r res ∧
|
||
r.begin.key < (⊤ : WithTop Key))
|
||
|
||
/-- The slack beyond the reported keys below `begin.key` in the crossed negative-begin
|
||
complete high cell. -/
|
||
def clampHighC (r : RangeRead) (res : Result) : ℕ :=
|
||
beginSlack r + 1 - reportedBelowBegin r res
|
||
|
||
/-- The capacity of the key gap `[keyAfter last reported, begin.key)`. -/
|
||
noncomputable def clampHighCap (r : RangeRead) (res : Result) : ℕ∞ :=
|
||
intervalCard ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key
|
||
|
||
/-- The required capacity in the clamping finite branch: `clampHighC`, plus one when
|
||
`end.key` is the empty key (there is then no key below it to anchor the begin
|
||
resolution, so one extra gap slot is needed). -/
|
||
def clampHighThresh (r : RangeRead) (res : Result) : ℕ :=
|
||
clampHighC r res + if r.end_.key = (([] : Key) : WithTop Key) then 1 else 0
|
||
|
||
/-- The upper endpoint of the clamping finite branch of the crossed negative-begin
|
||
complete high cell. -/
|
||
noncomputable def clampHighU (r : RangeRead) (res : Result) : WithTop Key :=
|
||
if (clampHighThresh r res : ℕ∞) ≤ clampHighCap r res then r.begin.key
|
||
else ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)
|
||
|
||
/-- The closed form of the crossed negative-begin complete high cell. -/
|
||
noncomputable def closedFormNegCrossCompleteHigh (r : RangeRead) (res : Result) :
|
||
ConflictRange := by
|
||
classical
|
||
exact
|
||
if reportedBelowBegin r res ≤ beginSlack r then
|
||
if res.keys.length = (r.end_.offset - 1).toNat then
|
||
⟨(([] : Key) : WithTop Key), clampHighU r res⟩
|
||
else ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩
|
||
else
|
||
if res.keys.length = (r.end_.offset - 1).toNat then
|
||
⟨r.end_.key, negCrossCompleteHighM r res⟩
|
||
else if negCrossHighUsesEnd r res then ⟨r.end_.key, (⊤ : WithTop Key)⟩
|
||
else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩
|
||
|
||
/-- The clamping finite branch of the crossed negative-begin complete high cell. -/
|
||
abbrev negCrossCompleteHighClampFinite (r : RangeRead) (res : Result) : Prop :=
|
||
negCrossCompleteHigh r res ∧ reportedBelowBegin r res ≤ beginSlack r ∧
|
||
res.keys.length = (r.end_.offset - 1).toNat
|
||
|
||
/-- The closed form evaluates to `[⊥, clampHighU r res)` in the clamping finite branch. -/
|
||
theorem closedForm_clampFinite_eq {r : RangeRead} {res : Result}
|
||
(hcell : negCrossCompleteHighClampFinite r res) :
|
||
closedFormNegCrossCompleteHigh r res =
|
||
(⟨(([] : Key) : WithTop Key), clampHighU r res⟩ : ConflictRange) := by
|
||
obtain ⟨-, hclamp, hfinite⟩ := hcell
|
||
unfold closedFormNegCrossCompleteHigh
|
||
rw [ite_eq_left hclamp, ite_eq_left hfinite]
|
||
|
||
section Structural
|
||
|
||
variable {r : RangeRead} {res : Result}
|
||
|
||
/-- Every reported key lies strictly above `end.key` in the high cell. -/
|
||
theorem high_reported_gt_end (hsort : res.keys.SortedLT) (hne : res.keys ≠ [])
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) :
|
||
∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) := by
|
||
intro k hk
|
||
have hhead_le : res.keys.head hne ≤ k := head_le_of_sortedLT' hsort hne hk
|
||
have hcoe : ((res.keys.headD ([] : Key) : Key) : WithTop Key) =
|
||
((res.keys.head hne : Key) : WithTop Key) := by
|
||
rw [headD_eq_head_of_ne hne]
|
||
rw [hcoe] at hEH
|
||
exact lt_of_lt_of_le hEH (WithTop.coe_le_coe.mpr hhead_le)
|
||
|
||
end Structural
|
||
|
||
/-- In the clamping, low-offset branch the reported list itself is a compatible
|
||
database: the begin selector clamps to `⊥` and the end selector overshoots the finite
|
||
reported list. -/
|
||
theorem high_witness_compatible_clamp_low {r : RangeRead} {res : Result}
|
||
{hsort : res.keys.SortedLT}
|
||
(hwl : RangeRead.withinLimit r res.keys) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat) :
|
||
RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by
|
||
set db : DB := ⟨res.keys, hsort⟩ with hdb
|
||
have hdbkeys : db.keys = res.keys := rfl
|
||
have hgt : ∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) :=
|
||
high_reported_gt_end hsort hne hEH
|
||
have hcE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0
|
||
:= by
|
||
rw [hdbkeys, List.countP_eq_zero]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (le_of_lt (hgt k hk))
|
||
have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := rfl
|
||
have hanchorB : r.begin.anchor db < 0 := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcB]
|
||
have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
|
||
omega
|
||
have hlo : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorB
|
||
have hepos : 1 ≤ r.end_.offset := by
|
||
-- `end.offset ≥ 1` because the end offset is not nonpositive in this cell
|
||
by_contra h
|
||
have hle : r.end_.offset ≤ 0 := by omega
|
||
have hz : (r.end_.offset - 1).toNat = 0 := by omega
|
||
rw [hz] at hlow
|
||
exact Nat.not_lt_zero _ hlow
|
||
have hanchorE_toNat : (r.end_.anchor db).toNat = (r.end_.offset - 1).toNat := by
|
||
rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add]
|
||
have hEi0 : 0 ≤ r.end_.anchor db := by
|
||
by_contra hneg
|
||
have hlt : r.end_.anchor db < 0 := lt_of_not_ge hneg
|
||
have hz : (r.end_.anchor db).toNat = 0 := Int.toNat_of_nonpos (le_of_lt hlt)
|
||
rw [hanchorE_toNat] at hz
|
||
rw [hz] at hlow
|
||
exact Nat.not_lt_zero _ hlow
|
||
have hlen : db.keys.length ≤ (r.end_.anchor db).toNat := by
|
||
rw [hanchorE_toNat]
|
||
exact le_of_lt hlow
|
||
have hhi : resolve db r.end_ = (⊤ : WithTop Key) := resolve_eq_top hEi0 hlen
|
||
refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩
|
||
rw [RangeRead.completeKeys, hrev]
|
||
simp only [Bool.false_eq_true, ite_false]
|
||
rw [hlo, hhi]
|
||
exact (RangeRead.inRange_unbounded res.keys).symm
|
||
|
||
/-- Count of keys strictly below a selector key after inserting a fresh key. -/
|
||
theorem countP_orderedInsert_begin {l : List Key} {B : WithTop Key} (hs : l.SortedLT)
|
||
{k : Key} (hk : k ∉ l) :
|
||
(l.orderedInsert (· ≤ ·) k).countP
|
||
(fun a : Key => decide (((a : Key) : WithTop Key) < B)) =
|
||
l.countP (fun a : Key => decide (((a : Key) : WithTop Key) < B)) +
|
||
(if ((k : Key) : WithTop Key) < B then 1 else 0) := by
|
||
rw [orderedInsert_eq_insertIdx hs hk]
|
||
by_cases hkB : ((k : Key) : WithTop Key) < B
|
||
· rw [ite_eq_left hkB]
|
||
exact countP_insertIdx_of_lt hs hkB
|
||
· rw [ite_eq_right hkB]
|
||
exact countP_insertIdx (fun a : Key => decide (((a : Key) : WithTop Key) < B))
|
||
(l.countP (fun a => decide (a < k))) k (by simp [hkB])
|
||
|
||
/-- Any key outside the reported list is necessary in the clamping, low-offset
|
||
branch: inserting it into the reported list either becomes the new minimum or joins the
|
||
window, so the result changes. -/
|
||
theorem necessary_of_high_not_mem_clamp_low {r : RangeRead} {res : Result}
|
||
{hsort : res.keys.SortedLT}
|
||
(hwl : RangeRead.withinLimit r res.keys) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
{k : Key} (hk : k ∉ res.keys) :
|
||
Necessary r res k := by
|
||
have hcomp₀ := high_witness_compatible_clamp_low (hsort := hsort) hwl hrev hmore hne
|
||
hbegin hEH hclamp hlow
|
||
set db : DB := ⟨res.keys, hsort⟩ with hdb
|
||
have hkdb : k ∉ db.keys := by simpa [hdb] using hk
|
||
let db₁ : DB := db.insert k hkdb
|
||
have hdb₁keys : db₁.keys = res.keys.orderedInsert (· ≤ ·) k := rfl
|
||
refine necessary_of_toggle ⟨db, hcomp₀, ?_⟩
|
||
rw [toggle_eq_insert_of_not_mem hkdb]
|
||
intro hcomp₁
|
||
have hck : RangeRead.completeKeys db₁ r = res.keys := by
|
||
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq.symm
|
||
have hk₁ : k ∈ db₁.keys := DB.mem_insert_self db k hkdb
|
||
have hne₁ : db₁.keys ≠ [] := by
|
||
intro hnil
|
||
have hlen0 : db₁.keys.length = 0 := by rw [hnil]; rfl
|
||
rw [hdb₁keys, List.orderedInsert_length] at hlen0
|
||
omega
|
||
-- The end resolution overshoots the finite inserted list.
|
||
have hepos : 1 ≤ r.end_.offset := by
|
||
have hle : 0 ≤ (r.end_.offset - 1).toNat := Nat.zero_le _
|
||
omega
|
||
have hlen₁ : db₁.keys.length = res.keys.length + 1 := by
|
||
rw [hdb₁keys, List.orderedInsert_length]
|
||
have htoNat₁ : (r.end_.anchor db₁).toNat =
|
||
db₁.keys.countP (fun a : Key => decide (((a : Key) : WithTop Key) < r.end_.key)) +
|
||
(r.end_.offset - 1).toNat := anchor_toNat_eq (s := r.end_) hepos
|
||
have hElen : db₁.keys.length ≤ (r.end_.anchor db₁).toNat := by
|
||
rw [htoNat₁, hlen₁]
|
||
omega
|
||
have hEi0₁ : 0 ≤ r.end_.anchor db₁ := by
|
||
unfold Selector.anchor
|
||
have hc : (0 : ℤ) ≤ (db₁.keys.countP
|
||
(fun a : Key => decide (((a : Key) : WithTop Key) < r.end_.key)) : ℤ) :=
|
||
Int.natCast_nonneg _
|
||
have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
linarith
|
||
have hhi₁ : resolve db₁ r.end_ = (⊤ : WithTop Key) := resolve_eq_top hEi0₁ hElen
|
||
-- The begin resolution lands at or below the inserted key.
|
||
have hcB₁ : db₁.keys.countP
|
||
(fun a : Key => decide (((a : Key) : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res +
|
||
(if ((k : Key) : WithTop Key) < r.begin.key then 1 else 0) := by
|
||
rw [hdb₁keys]
|
||
simpa [reportedBelowBegin] using
|
||
(countP_orderedInsert_begin (B := r.begin.key) hsort (by simpa [hdb] using hk))
|
||
have hanchorB₁ : r.begin.anchor db₁ ≤ 0 := by
|
||
rw [anchor_eq_count_sub_slack db₁ r (le_of_lt hbegin), hcB₁]
|
||
have hcl : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by
|
||
exact_mod_cast hclamp
|
||
split_ifs with hkB <;> push_cast <;> omega
|
||
have hlo₁ : resolve db₁ r.begin ≤ ((k : Key) : WithTop Key) := by
|
||
rcases lt_or_eq_of_le hanchorB₁ with hlt | heq
|
||
· rw [resolve_eq_nil hlt]
|
||
exact WithTop.coe_le_coe.mpr (nil_le_key k)
|
||
· have h0 : 0 ≤ r.begin.anchor db₁ := le_of_eq heq.symm
|
||
have hpos : 0 < db₁.keys.length := by rw [hlen₁]; omega
|
||
have hltlen : (r.begin.anchor db₁).toNat < db₁.keys.length := by
|
||
rw [heq]; simpa using hpos
|
||
have hhead_le : db₁.keys.head hne₁ ≤ k :=
|
||
head_le_of_sortedLT' db₁.sorted hne₁ hk₁
|
||
have hzero : (r.begin.anchor db₁).toNat = 0 := by rw [heq]; rfl
|
||
have hresolve : resolve db₁ r.begin =
|
||
((db₁.keys[(r.begin.anchor db₁).toNat] : Key) : WithTop Key) :=
|
||
resolve_eq_getElem h0 hltlen
|
||
rw [hresolve]
|
||
have hget : db₁.keys[(r.begin.anchor db₁).toNat] = db₁.keys.head hne₁ := by
|
||
simp only [hzero]
|
||
exact List.getElem_zero _
|
||
rw [hget]
|
||
exact WithTop.coe_le_coe.mpr hhead_le
|
||
have hkck : k ∈ RangeRead.completeKeys db₁ r := by
|
||
rw [RangeRead.mem_completeKeys]
|
||
exact ⟨hk₁, hlo₁, by rw [hhi₁]; exact WithTop.coe_lt_top k⟩
|
||
rw [hck] at hkck
|
||
exact hk hkck
|
||
|
||
/-- **The clamping, low-offset branch of the crossed negative-begin complete high cell
|
||
is the full range.** -/
|
||
theorem minimal_fullRange_of_negCrossHigh_clamp_low {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hbegin : r.begin.offset < 0)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(_hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat) :
|
||
Minimal r res (⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ : ConflictRange) := by
|
||
obtain ⟨dbw, hcompw⟩ := hread
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcompw hrev
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨sound_fullRange r res, ?_⟩
|
||
intro k _
|
||
by_cases hk : k ∈ res.keys
|
||
· exact necessary_of_mem_result ⟨dbw, hcompw⟩ hk
|
||
· exact necessary_of_high_not_mem_clamp_low (hsort := hsort) hcompw.1 hrev hmore hne
|
||
hbegin hEH hclamp hlow hk
|
||
|
||
/-- The clamping, low-offset branch of the crossed negative-begin complete high cell. -/
|
||
abbrev negCrossCompleteHighClampLow (r : RangeRead) (res : Result) : Prop :=
|
||
negCrossCompleteHigh r res ∧ reportedBelowBegin r res ≤ beginSlack r ∧
|
||
res.keys.length < (r.end_.offset - 1).toNat
|
||
|
||
/-- **The clamping, low-offset branch of the crossed negative-begin complete high cell
|
||
is minimal** for the total closed form. -/
|
||
theorem minimal_closedForm_of_negCrossCompleteHigh_clampLow {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossCompleteHighClampLow r res) :
|
||
Minimal r res (closedFormNegCrossCompleteHigh r res) := by
|
||
obtain ⟨⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩, hclamp, hlow⟩ := hcell
|
||
have hmin := minimal_fullRange_of_negCrossHigh_clamp_low hread hrev hbegin hmore hne
|
||
hcrossD hEH hclamp hlow
|
||
unfold closedFormNegCrossCompleteHigh
|
||
rw [ite_eq_left hclamp]
|
||
rw [ite_eq_right (ne_of_lt hlow)]
|
||
exact hmin
|
||
|
||
|
||
|
||
variable {r : RangeRead} {res : Result}
|
||
|
||
/-- A key of `db.keys` at or above `head` and below `begin.key` is counted. -/
|
||
private theorem countP_between_head_begin_eq_filter_length (db : DB) (head : Key)
|
||
(B : WithTop Key) :
|
||
db.keys.countP (fun k : Key =>
|
||
decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < B)) =
|
||
(db.keys.filter (fun k : Key =>
|
||
decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < B))).length :=
|
||
List.countP_eq_length_filter
|
||
|
||
/-- In the crossed negative-begin complete high cell, the number of reported keys
|
||
below `begin.key` exceeds the slack by at most one. -/
|
||
theorem high_reportedBelowBegin_le_succ {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) :
|
||
reportedBelowBegin r res ≤ beginSlack r + 1 := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r
|
||
· omega
|
||
· have hnon : beginSlack r < reportedBelowBegin r res := lt_of_not_ge hclamp
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
have hcross' : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
rwa [hheadD] at hcross
|
||
have hlen := negCross_filter_begin_length hcomp hrev hmore hne hbegin hcross' hnon
|
||
have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev
|
||
-- The reported count below `begin.key` is at most the stored interval count.
|
||
have hfilter_res : res.keys.filter (fun k : Key =>
|
||
decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
apply List.filter_congr
|
||
intro k hk
|
||
have hheadle : ((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (head_le_of_sortedLT'
|
||
(RangeRead.compatible_sortedLT hcomp hrev) hne hk)
|
||
simp only [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨hheadle, h⟩⟩
|
||
have hcnt_res : res.keys.countP (fun k : Key =>
|
||
decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := by
|
||
rw [reportedBelowBegin, List.countP_eq_length_filter (p := fun k : Key =>
|
||
decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)),
|
||
List.countP_eq_length_filter (p := fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < r.begin.key)), hfilter_res]
|
||
have hle_count : res.keys.countP (fun k : Key =>
|
||
decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) ≤
|
||
db.keys.countP (fun k : Key =>
|
||
decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) :=
|
||
List.Sublist.countP_le hsub
|
||
have hcnt_db : db.keys.countP (fun k : Key =>
|
||
decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by
|
||
rw [countP_between_head_begin_eq_filter_length]
|
||
exact hlen
|
||
omega
|
||
|
||
|
||
/-- In the crossed negative-begin complete high cell the reported list is no longer
|
||
than the end offset allows: `#reported ≤ (end.offset - 1).toNat`. The window is a
|
||
contiguous stored block above `end.key`, and the end anchor adds only `(end.offset - 1)`
|
||
to the count below `end.key`. -/
|
||
theorem high_length_le_end {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) :
|
||
res.keys.length ≤ (r.end_.offset - 1).toNat := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
set lo : WithTop Key := resolve db r.begin with hlo
|
||
set hi : WithTop Key := resolve db r.end_ with hhi
|
||
have hkeys : res.keys = RangeRead.inRange db.keys lo hi := by
|
||
have heq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, he⟩
|
||
· simp [hmore] at hm
|
||
· exact he
|
||
exact heq.trans (completeKeys_eq_inRange_of_not_rev hrev)
|
||
have hgt : ∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) :=
|
||
high_reported_gt_end (RangeRead.compatible_sortedLT hcomp hrev) hne hEH
|
||
have hbetween_eq : between db lo hi = res.keys := by
|
||
rw [hkeys]
|
||
rfl
|
||
have hheadmem : res.keys.headD ([] : Key) ∈ res.keys := headD_mem hne
|
||
have hhead_in : (res.keys.headD ([] : Key)) ∈ db.keys ∧
|
||
lo ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < hi := by
|
||
have hmem : (res.keys.headD ([] : Key)) ∈ RangeRead.inRange db.keys lo hi := by
|
||
rw [← hkeys]; exact hheadmem
|
||
simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hmem
|
||
have hEH' : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) := hEH
|
||
have hE_hi : r.end_.key < hi := lt_trans hEH' hhead_in.2.2
|
||
have hlo_hi : lo ≤ hi := le_trans hhead_in.2.1 (le_of_lt hhead_in.2.2)
|
||
have hcnt_hi : cnt db hi = cnt db lo + res.keys.length := by
|
||
rw [cnt_eq_add_between db hlo_hi, hbetween_eq]
|
||
have hcnt_hi' : cnt db hi =
|
||
min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length := by
|
||
rw [hhi]
|
||
change db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) <
|
||
resolve db r.end_)) = min (cnt db r.end_.key + (r.end_.offset - 1).toNat)
|
||
db.keys.length
|
||
rw [countP_lt_resolve (db := db) (s := r.end_),
|
||
anchor_toNat_eq (s := r.end_) (by omega)]
|
||
rfl
|
||
have hcntE_le : cnt db r.end_.key ≤ cnt db lo := by
|
||
rcases le_total r.end_.key lo with hEl | hloE
|
||
· exact cnt_le_cnt_of_le db hEl
|
||
· have hb0 : between db lo r.end_.key = [] := by
|
||
rw [between, List.filter_eq_nil_iff]
|
||
intro k hkdb hcon
|
||
simp only [decide_eq_true_eq] at hcon
|
||
obtain ⟨hklo, hkE⟩ := hcon
|
||
have hkhi : ((k : Key) : WithTop Key) < hi := lt_trans hkE hE_hi
|
||
have hkres : k ∈ res.keys := by
|
||
rw [hkeys]
|
||
simp only [RangeRead.inRange, List.mem_filter, decide_eq_true_eq]
|
||
exact ⟨hkdb, hklo, hkhi⟩
|
||
exact absurd (hgt k hkres) (not_lt.mpr (le_of_lt hkE))
|
||
have h := cnt_eq_add_between db hloE
|
||
rw [hb0, List.length_nil, Nat.add_zero] at h
|
||
exact le_of_eq h
|
||
rw [hcnt_hi] at hcnt_hi'
|
||
rcases Nat.le_total (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length with h | h
|
||
· rw [Nat.min_eq_left h] at hcnt_hi'
|
||
omega
|
||
· rw [Nat.min_eq_right h] at hcnt_hi'
|
||
omega
|
||
|
||
/-- **A sufficient shape for minimality.** If agreement with the result is exactly
|
||
equality of the stored keys in a fixed interval `[E, U)`, then that interval is minimal.
|
||
Soundness is immediate because agreement on the interval is equality of those filtered
|
||
lists; necessity follows by flipping any key of the interval into a compatible database,
|
||
which perturbs the filtered list. -/
|
||
theorem minimal_of_compatible_iff_filter {r : RangeRead} {res : Result}
|
||
{E U : WithTop Key} (hread : Readable r res)
|
||
(hchar : ∀ db : DB, RangeRead.Compatible db r res ↔
|
||
db.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < U)) = res.keys) :
|
||
Minimal r res ⟨E, U⟩ := by
|
||
classical
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
have hf : db1.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < U)) =
|
||
db2.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < U)) := by
|
||
simpa only [AgreeOn, ConflictRange.keys] using hagree
|
||
rw [hchar db1, hchar db2, hf]
|
||
· intro k hk
|
||
obtain ⟨hkE, hkU⟩ := ConflictRange.mem_iff.mp hk
|
||
by_cases hkres : k ∈ res.keys
|
||
· exact necessary_of_mem_result hread hkres
|
||
· obtain ⟨db0, hcomp0⟩ := hread
|
||
have hknot : k ∉ db0.keys := by
|
||
intro hkdb
|
||
have hmem : k ∈ db0.keys.filter (fun k : Key =>
|
||
decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < U)) := by
|
||
rw [List.mem_filter]
|
||
exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkE, hkU⟩⟩
|
||
rw [(hchar db0).mp hcomp0] at hmem
|
||
exact hkres hmem
|
||
refine necessary_of_toggle ⟨db0, hcomp0, ?_⟩
|
||
intro hcomp1
|
||
have hkf : k ∈ (toggle db0 k).keys.filter (fun k : Key =>
|
||
decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < U)) := by
|
||
rw [toggle_eq_insert_of_not_mem hknot, List.mem_filter]
|
||
exact ⟨DB.mem_insert_self db0 k hknot, by rw [decide_eq_true_eq]; exact ⟨hkE, hkU⟩⟩
|
||
rw [(hchar _).mp hcomp1] at hkf
|
||
exact hkres hkf
|
||
|
||
/-- In the crossed negative-begin complete high cell with a finite end, the begin
|
||
resolution equals the reported head, and no stored keys lie between `end.key` and
|
||
the head. -/
|
||
theorem high_cnt_head_eq_cnt_end {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hfinite : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) = cnt db r.end_.key := by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hhead_mem : H ∈ res.keys := by rw [hH]; exact headD_mem hne
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hL' : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by rw [hL', ← hHD]
|
||
have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL]
|
||
rfl
|
||
have hhead_lt_hi : ((H : Key) : WithTop Key) < resolve db r.end_ := by
|
||
have hmem : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [← hwin]; exact hhead_mem
|
||
have hcond := of_decide_eq_true (List.mem_filter.mp hmem).2
|
||
exact hcond.2
|
||
have hcnt_hi : cnt db (resolve db r.end_) =
|
||
cnt db (((H : Key) : WithTop Key)) + res.keys.length := by
|
||
rw [cnt_eq_add_between db (le_of_lt hhead_lt_hi), ← hwin]
|
||
have hcnt_hi_le : cnt db (resolve db r.end_) ≤
|
||
cnt db r.end_.key + (r.end_.offset - 1).toNat := by
|
||
have h : cnt db (resolve db r.end_) =
|
||
min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length := by
|
||
rw [show cnt db (resolve db r.end_) = db.keys.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl,
|
||
countP_lt_resolve (db := db) (s := r.end_),
|
||
anchor_toNat_eq (s := r.end_) (by omega)]
|
||
rfl
|
||
rw [h]; exact Nat.min_le_left _ _
|
||
rw [hcnt_hi, hfinite] at hcnt_hi_le
|
||
have hcntE_le : cnt db r.end_.key ≤ cnt db (((H : Key) : WithTop Key)) :=
|
||
cnt_le_cnt_of_le db (le_of_lt hEH)
|
||
omega
|
||
|
||
/-- In the crossed negative-begin complete high cell with a finite end, no stored key
|
||
lies in `[end.key, head)`. -/
|
||
theorem high_between_end_head_eq_nil {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hfinite : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
between db r.end_.key (((res.keys.headD ([] : Key) : Key) : WithTop Key)) = [] := by
|
||
have hcnt := high_cnt_head_eq_cnt_end hcomp hrev hmore hne hbegin hend hEH hnonclamp hfinite
|
||
have hsplit := cnt_eq_add_between db (le_of_lt hEH)
|
||
rw [hcnt, between] at hsplit
|
||
have hlen : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key)))).length = 0
|
||
:= by
|
||
omega
|
||
exact List.length_eq_zero_iff.mp hlen
|
||
|
||
/-- In the crossed negative-begin complete high cell the reported-below-begin count is
|
||
exactly the begin slack plus one. -/
|
||
theorem high_reportedBelowBegin_eq_slack_succ {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res) :
|
||
reportedBelowBegin r res = beginSlack r + 1 := by
|
||
have hle := high_reportedBelowBegin_le_succ hread hrev hmore hne hbegin hcrossD
|
||
omega
|
||
|
||
/-- In the crossed negative-begin complete high cell with a finite end and a begin key
|
||
above `keyAfter last`, no stored key lies in `[keyAfter last, begin.key)`. -/
|
||
theorem high_between_keyAfter_begin_eq_nil {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0) (_hend : 2 ≤ r.end_.offset)
|
||
(_hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hKleB : ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) ≤ r.begin.key) :
|
||
between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) r.begin.key = []
|
||
:= by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set L : Key := res.keys.getLastD ([] : Key) with hL
|
||
set K : Key := keyAfter L with hK
|
||
have hHhead : H = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hLlast : L = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hHhead]; exact hcrossD
|
||
-- The reported-below count is the slack successor.
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ ⟨db, hcomp⟩ hrev hmore hne hbegin hcrossD hnonclamp
|
||
-- Length of `[H, begin.key)`.
|
||
have hlenHB : (between db ((H : Key) : WithTop Key) r.begin.key).length = beginSlack r + 1 := by
|
||
rw [hHhead]
|
||
exact negCross_filter_begin_length hcomp hrev hmore hne hbegin hcross hnonclamp
|
||
-- `res.keys` below `begin.key` are exactly the `res.keys` at or below the last.
|
||
have hcount_res : (res.keys.filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < r.begin.key))).length = beginSlack r + 1 := by
|
||
rw [← List.countP_eq_length_filter, ← hr]; rfl
|
||
-- Every reported key is at or below the last.
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
|
||
have hall_le_last : ∀ k ∈ res.keys, k ≤ L := by
|
||
intro k hk; rw [hLlast]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
|
||
have hHleL : H ≤ L := by
|
||
rw [hHhead, hLlast]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne)
|
||
have hH_le_K : ((H : Key) : WithTop Key) ≤ ((K : Key) : WithTop Key) := by
|
||
rw [WithTop.coe_le_coe]; exact le_trans hHleL (le_of_lt (keyAfter_self_lt L))
|
||
-- All reported keys below `begin.key` are stored in `[H, K)`.
|
||
have hsub : ∀ k ∈ res.keys.filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < r.begin.key)), k ∈ between db ((H : Key) : WithTop Key)
|
||
((K : Key) : WithTop Key) := by
|
||
intro k hk
|
||
rw [List.mem_filter] at hk
|
||
have hkres := hk.1
|
||
have hklt : ((k : Key) : WithTop Key) < r.begin.key := by
|
||
rw [decide_eq_true_eq] at hk; exact hk.2
|
||
have hkH : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by
|
||
rw [hHhead]; exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hkres)
|
||
have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by
|
||
rw [WithTop.coe_lt_coe]; exact lt_of_le_of_lt (hall_le_last k hkres) (keyAfter_self_lt L)
|
||
exact List.mem_filter.mpr ⟨RangeRead.compatible_keys_subset hcomp hkres,
|
||
by rw [decide_eq_true_eq]; exact ⟨hkH, hkK⟩⟩
|
||
-- Hence `[H, K)` holds at least `beginSlack + 1` stored keys.
|
||
have hlenHK_ge : beginSlack r + 1 ≤ (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop
|
||
Key)).length := by
|
||
have hfin : (res.keys.filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < r.begin.key))).toFinset ⊆
|
||
(between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).toFinset := by
|
||
intro k hk
|
||
rw [List.mem_toFinset] at hk ⊢
|
||
exact hsub k hk
|
||
have hcard := Finset.card_le_card hfin
|
||
rw [List.toFinset_card_of_nodup (hsort.nodup.filter _)] at hcard
|
||
simp only [between] at hcard
|
||
rw [List.toFinset_card_of_nodup (db.nodup.filter _)] at hcard
|
||
rwa [hcount_res] at hcard
|
||
-- Counts: `K ≤ begin.key`, so `[K, begin.key)` adds nothing.
|
||
have hcnt : cnt db r.begin.key = cnt db ((H : Key) : WithTop Key) +
|
||
(between db ((H : Key) : WithTop Key) r.begin.key).length :=
|
||
cnt_eq_add_between db (le_of_lt hcrossD)
|
||
have hcntHB : cnt db r.begin.key = cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) := by
|
||
rw [hcnt, hlenHB]
|
||
have hcntHK : cnt db ((K : Key) : WithTop Key) = cnt db ((H : Key) : WithTop Key) +
|
||
(between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).length :=
|
||
cnt_eq_add_between db hH_le_K
|
||
have hK_le_ck : cnt db ((K : Key) : WithTop Key) ≤ cnt db r.begin.key :=
|
||
cnt_le_cnt_of_le db hKleB
|
||
have hlenKB : (between db ((K : Key) : WithTop Key) r.begin.key).length = 0 := by
|
||
have hsplit := cnt_eq_add_between db hKleB
|
||
rw [hcntHB] at hsplit
|
||
have hle : cnt db ((K : Key) : WithTop Key) ≤
|
||
cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) := by rw [← hcntHB]; exact hK_le_ck
|
||
have hge : cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) ≤
|
||
cnt db ((K : Key) : WithTop Key) := by
|
||
rw [hcntHK]; exact Nat.add_le_add_left hlenHK_ge _
|
||
have heq : cnt db ((K : Key) : WithTop Key) =
|
||
cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) := le_antisymm hle hge
|
||
rw [heq] at hsplit
|
||
omega
|
||
exact List.length_eq_zero_iff.mp (by simpa [hH, hL, hK] using hlenKB)
|
||
|
||
|
||
/-- Forward direction: in the crossed negative-begin complete high cell with a finite
|
||
end and a nonclamping begin, the stored keys in `[end.key, M)` are exactly the
|
||
reported keys, where `M = max begin.key (keyAfter last)`. -/
|
||
theorem high_nonclamp_finite_filter_eq {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hfinite : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < negCrossCompleteHighM r res)) = res.keys := by
|
||
classical
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set L : Key := res.keys.getLastD ([] : Key) with hL
|
||
set K : Key := keyAfter L with hK
|
||
set M : WithTop Key := negCrossCompleteHighM r res with hM
|
||
have hMdef : M = max r.begin.key ((K : Key) : WithTop Key) := by
|
||
rw [hM]
|
||
unfold negCrossCompleteHighM
|
||
rw [← hL, ← hK]
|
||
have hHhead : H = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hLlast : L = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hHhead]; exact hcrossD
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hLresolve : resolve db r.begin = ((H : Key) : WithTop Key) := by
|
||
rw [hHhead]
|
||
exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hLresolve]
|
||
rfl
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
|
||
have hLmem : L ∈ res.keys := by rw [hLlast]; exact List.getLast_mem hne
|
||
have hLlt : ((L : Key) : WithTop Key) < resolve db r.end_ := by
|
||
have hmem : L ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [← hwin]; exact hLmem
|
||
exact (of_decide_eq_true (List.mem_filter.mp hmem).2).2
|
||
have hKleR : ((K : Key) : WithTop Key) ≤ resolve db r.end_ := keyAfter_le_of_lt hLlt
|
||
have hnilEH : between db r.end_.key ((H : Key) : WithTop Key) = [] :=
|
||
high_between_end_head_eq_nil hcomp hrev hmore hne hbegin hend hEH hnonclamp hfinite
|
||
refine List.Subset.antisymm_of_sortedLT ?_ ?_
|
||
(List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)) hsort
|
||
· intro k hk
|
||
rw [List.mem_filter] at hk
|
||
obtain ⟨hkdb, hkcond⟩ := hk
|
||
rw [decide_eq_true_eq] at hkcond
|
||
obtain ⟨hkE, hkM⟩ := hkcond
|
||
by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)
|
||
· have hmem : k ∈ between db r.end_.key ((H : Key) : WithTop Key) := by
|
||
rw [between, List.mem_filter]
|
||
exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkE, hkH⟩⟩
|
||
rw [hnilEH] at hmem
|
||
exact absurd hmem (by simp)
|
||
· have hHle : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH
|
||
have hkltR : ((k : Key) : WithTop Key) < resolve db r.end_ := by
|
||
by_contra hnot
|
||
have hRk : resolve db r.end_ ≤ ((k : Key) : WithTop Key) := le_of_not_gt hnot
|
||
have hkK : ((K : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_trans hKleR hRk
|
||
have hkB : ((k : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hMdef] at hkM
|
||
rcases le_total r.begin.key ((K : Key) : WithTop Key) with hBK | hBK
|
||
· rw [max_eq_right hBK] at hkM
|
||
exact absurd hkM (not_lt.mpr hkK)
|
||
· rw [max_eq_left hBK] at hkM
|
||
exact hkM
|
||
have hnilKB : between db ((K : Key) : WithTop Key) r.begin.key = [] :=
|
||
high_between_keyAfter_begin_eq_nil hcomp hrev hmore hne hbegin hend hEH hcrossD
|
||
hnonclamp (le_trans hkK (le_of_lt hkB))
|
||
have hmem : k ∈ between db ((K : Key) : WithTop Key) r.begin.key := by
|
||
rw [between, List.mem_filter]
|
||
exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkK, hkB⟩⟩
|
||
rw [hnilKB] at hmem
|
||
exact absurd hmem (by simp)
|
||
have hmem : k ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [between, List.mem_filter]
|
||
exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hHle, hkltR⟩⟩
|
||
rw [hwin]; exact hmem
|
||
· intro k hk
|
||
rw [List.mem_filter]
|
||
have hkdb : k ∈ db.keys := RangeRead.compatible_keys_subset hcomp hk
|
||
have hkE : r.end_.key < ((k : Key) : WithTop Key) :=
|
||
high_reported_gt_end hsort hne hEH k hk
|
||
have hkL : k ≤ L := by
|
||
rw [hLlast]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
|
||
have hkK : k < K := by
|
||
rw [hK]; exact lt_of_le_of_lt hkL (keyAfter_self_lt L)
|
||
have hkM : ((k : Key) : WithTop Key) < M := by
|
||
rw [hMdef]
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hkK) (le_max_right _ _)
|
||
exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨le_of_lt hkE, hkM⟩⟩
|
||
|
||
|
||
/-- If `p` implies `q` pointwise, then `p`-filtering equals `q`-filtering followed
|
||
by `p`-filtering. -/
|
||
theorem filter_eq_filter_filter_of_imp {α : Type*} {l : List α} {p q : α → Bool}
|
||
(h : ∀ a, p a = true → q a = true) :
|
||
l.filter p = (l.filter q).filter p := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro a _
|
||
cases hp : p a with
|
||
| false => simp []
|
||
| true => simp [ h a hp]
|
||
|
||
/-- Backward direction: in the crossed negative-begin complete high cell with a finite
|
||
end and a nonclamping begin, equality of the stored keys in `[end.key, M)` with the
|
||
reported keys forces compatibility. -/
|
||
theorem high_nonclamp_finite_compatible_of_filter {db : DB} {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hfinite : res.keys.length = (r.end_.offset - 1).toNat)
|
||
(hfilter : db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < negCrossCompleteHighM r res)) = res.keys) :
|
||
RangeRead.Compatible db r res := by
|
||
classical
|
||
obtain ⟨db0, hcomp0⟩ := hread
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp0.1
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set L : Key := res.keys.getLastD ([] : Key) with hL
|
||
set K : Key := keyAfter L with hK
|
||
set M : WithTop Key := negCrossCompleteHighM r res with hM
|
||
have hMdef : M = max r.begin.key ((K : Key) : WithTop Key) := by
|
||
rw [hM]; unfold negCrossCompleteHighM; rw [← hL, ← hK]
|
||
have hHhead : H = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hLlast : L = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hHhead]; exact hcrossD
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ ⟨db0, hcomp0⟩ hrev hmore hne hbegin hcrossD hnonclamp
|
||
have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne
|
||
have hHmem_db : H ∈ db.keys := by
|
||
have : H ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < M)) := by rw [hfilter]; exact hHmem
|
||
exact (List.mem_filter.mp this).1
|
||
have hHleM : ((H : Key) : WithTop Key) ≤ M := by
|
||
rw [hMdef]; exact le_trans (le_of_lt hcrossD) (le_max_left _ _)
|
||
-- No stored key lies in `[end.key, H)`.
|
||
have hnilEH : between db r.end_.key ((H : Key) : WithTop Key) = [] := by
|
||
rw [between, List.filter_eq_nil_iff]
|
||
intro k hk hdec
|
||
have hcond : r.end_.key ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) <
|
||
((H : Key) : WithTop Key) := of_decide_eq_true hdec
|
||
have hkP : k ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < M)) := by
|
||
rw [List.mem_filter]
|
||
exact ⟨hk, by rw [decide_eq_true_eq]; exact ⟨hcond.1, lt_of_lt_of_le hcond.2 hHleM⟩⟩
|
||
rw [hfilter] at hkP
|
||
have hmin : H ≤ k := by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hkP
|
||
exact absurd (WithTop.coe_lt_coe.mp hcond.2) (not_lt.mpr hmin)
|
||
have hcntHE : cnt db ((H : Key) : WithTop Key) = cnt db r.end_.key := by
|
||
have h := cnt_eq_add_between db (le_of_lt hEH)
|
||
rw [hnilEH] at h
|
||
simpa using h
|
||
-- The stored keys in `[H, M)` are the reported keys.
|
||
have hkeysHM : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M)) = res.keys := by
|
||
have hstep : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M)) =
|
||
(db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < M))).filter
|
||
(fun k : Key => decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < M)) := by
|
||
apply filter_eq_filter_filter_of_imp
|
||
intro k hk
|
||
rw [decide_eq_true_eq] at hk ⊢
|
||
exact ⟨le_trans (le_of_lt hEH) hk.1, hk.2⟩
|
||
rw [hstep, hfilter]
|
||
apply List.filter_eq_self.mpr
|
||
intro k hk
|
||
have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hk)
|
||
have hkL : k ≤ L := by
|
||
rw [hLlast]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
|
||
have hkM : ((k : Key) : WithTop Key) < M := by
|
||
rw [hMdef]
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkL (keyAfter_self_lt L)))
|
||
(le_max_right _ _)
|
||
rw [decide_eq_true_eq]; exact ⟨hHk, hkM⟩
|
||
-- The stored keys in `[H, begin.key)` are exactly the reported keys there.
|
||
have hkeysHB : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
have hstep : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
(db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M))).filter
|
||
(fun k : Key => decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
apply filter_eq_filter_filter_of_imp
|
||
intro k hk
|
||
rw [decide_eq_true_eq] at hk ⊢
|
||
exact ⟨hk.1, lt_of_lt_of_le hk.2 (by rw [hMdef]; exact le_max_left _ _)⟩
|
||
rw [hstep, hkeysHM]
|
||
have hlenFilter : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
rw [hkeysHB]
|
||
have hresEq : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
apply List.filter_congr
|
||
intro k hk
|
||
have hHk : H ≤ k := by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hk
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr hHk, h⟩⟩
|
||
rw [hresEq, ← List.countP_eq_length_filter]
|
||
exact hr
|
||
have hhhead : res.keys.head hne ∈ db.keys := by rw [← hHhead]; exact hHmem_db
|
||
have hresB : resolve db r.begin = ((H : Key) : WithTop Key) := by
|
||
have hlenFilter' : (db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) :
|
||
WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
simpa only [hHhead] using hlenFilter
|
||
rw [hHhead]
|
||
exact resolve_begin_eq_head_of_filter_len hbegin hhhead hcross hlenFilter'
|
||
have hdropH : db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) <
|
||
((H : Key) : WithTop Key))) = db.keys.drop (cnt db r.end_.key) := by
|
||
rw [filter_not_lt_eq_drop_countP db.sorted ((H : Key) : WithTop Key)]
|
||
change db.keys.drop (cnt db ((H : Key) : WithTop Key)) = db.keys.drop (cnt db r.end_.key)
|
||
rw [hcntHE]
|
||
-- The first `#reported` stored keys from `H` are the reported keys.
|
||
have hfilterM : (db.keys.drop (cnt db r.end_.key)).filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < M)) = res.keys := by
|
||
rw [← hdropH]
|
||
have hstep : (db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) <
|
||
((H : Key) : WithTop Key)))).filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < M)) =
|
||
db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M)) := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)
|
||
· have hnot : ¬ (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := not_le.mpr hkH
|
||
simp [hkH, hnot]
|
||
· have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH
|
||
simp [hkH, hHk]
|
||
rw [hstep, hkeysHM]
|
||
have htake : (db.keys.drop (cnt db r.end_.key)).take (res.keys.length) = res.keys := by
|
||
have hsorted : (db.keys.drop (cnt db r.end_.key)).SortedLT :=
|
||
List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := cnt db r.end_.key)
|
||
db.sorted.pairwise)
|
||
have hcount : (db.keys.drop (cnt db r.end_.key)).countP (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < M)) = res.keys.length := by
|
||
rw [List.countP_eq_length_filter, hfilterM]
|
||
have htake0 := filter_lt_eq_take_countP hsorted M
|
||
rw [hfilterM, hcount] at htake0
|
||
exact htake0.symm
|
||
-- The drop has at least `#reported` keys, so the end anchor stays in range.
|
||
have hresSubDrop : res.keys ⊆ db.keys.drop (cnt db r.end_.key) := by
|
||
intro k hk
|
||
have hkdb : k ∈ db.keys := by
|
||
have : k ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < M)) := by rw [hfilter]; exact hk
|
||
exact (List.mem_filter.mp this).1
|
||
have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hk)
|
||
rw [← hdropH, List.mem_filter]
|
||
exact ⟨hkdb, by rw [decide_eq_true_eq]; exact not_lt.mpr hHk⟩
|
||
have hlen_ge : res.keys.length ≤ (db.keys.drop (cnt db r.end_.key)).length := by
|
||
have hfin : res.keys.toFinset ⊆ (db.keys.drop (cnt db r.end_.key)).toFinset := by
|
||
intro k hk; rw [List.mem_toFinset] at hk ⊢; exact hresSubDrop hk
|
||
have hcard := Finset.card_le_card hfin
|
||
rw [List.toFinset_card_of_nodup hsort.nodup,
|
||
List.toFinset_card_of_nodup (List.Nodup.sublist (List.drop_sublist _ _) db.nodup)] at hcard
|
||
exact hcard
|
||
have hce_le : cnt db r.end_.key + res.keys.length ≤ db.keys.length := by
|
||
rw [List.length_drop] at hlen_ge
|
||
omega
|
||
have hanchorE_toNat : (r.end_.anchor db).toNat =
|
||
cnt db r.end_.key + (r.end_.offset - 1).toNat := by
|
||
rw [anchor_toNat_eq (s := r.end_) (by omega)]
|
||
rfl
|
||
have hcntR : db.keys.countP (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < resolve db r.end_)) =
|
||
cnt db r.end_.key + res.keys.length := by
|
||
rw [countP_lt_resolve (db := db) (s := r.end_), hanchorE_toNat, hfinite]
|
||
exact Nat.min_eq_left (by omega)
|
||
have hHR : ((H : Key) : WithTop Key) ≤ resolve db r.end_ := by
|
||
by_contra h
|
||
have hle : resolve db r.end_ ≤ ((H : Key) : WithTop Key) := le_of_not_ge h
|
||
have hmono := cnt_le_cnt_of_le db hle
|
||
change db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) <
|
||
resolve db r.end_)) ≤ cnt db ((H : Key) : WithTop Key) at hmono
|
||
rw [hcntR, hcntHE] at hmono
|
||
have hpos : 0 < res.keys.length := List.length_pos_iff.mpr hne
|
||
omega
|
||
-- Split the database at `cnt end.key`; the tail starts at the reported head.
|
||
have hER : r.end_.key ≤ resolve db r.end_ := le_trans (le_of_lt hEH) hHR
|
||
have htakeE : db.keys.take (cnt db r.end_.key) =
|
||
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) :=
|
||
(filter_lt_eq_take_countP db.sorted r.end_.key).symm
|
||
have hcountTake : (db.keys.take (cnt db r.end_.key)).countP (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < resolve db r.end_)) = cnt db r.end_.key := by
|
||
rw [htakeE]
|
||
have hlen : (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
|
||
r.end_.key))).countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) =
|
||
(db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))).length :=
|
||
List.countP_eq_length.mpr (by
|
||
intro k hk
|
||
rw [List.mem_filter] at hk
|
||
exact decide_eq_true (lt_of_lt_of_le (of_decide_eq_true hk.2) hER))
|
||
rw [hlen, ← List.countP_eq_length_filter]
|
||
rfl
|
||
have hcountR : (db.keys.drop (cnt db r.end_.key)).countP (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys.length := by
|
||
have hsplit := List.countP_append (p := fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < resolve db r.end_))
|
||
(l₁ := db.keys.take (cnt db r.end_.key)) (l₂ := db.keys.drop (cnt db r.end_.key))
|
||
rw [List.take_append_drop] at hsplit
|
||
rw [hcountTake, hcntR] at hsplit
|
||
omega
|
||
have hwindow : between db ((H : Key) : WithTop Key) (resolve db r.end_) = res.keys := by
|
||
have hsorted : (db.keys.drop (cnt db r.end_.key)).SortedLT :=
|
||
List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := cnt db r.end_.key)
|
||
db.sorted.pairwise)
|
||
have hstep : between db ((H : Key) : WithTop Key) (resolve db r.end_) =
|
||
(db.keys.drop (cnt db r.end_.key)).filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by
|
||
unfold between
|
||
rw [← hdropH, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)
|
||
· have hnot : ¬ (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := not_le.mpr hkH
|
||
simp [hkH, hnot]
|
||
· have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH
|
||
simp [hkH, hHk]
|
||
rw [hstep, filter_lt_eq_take_countP hsorted (resolve db r.end_), hcountR, htake]
|
||
have hck : RangeRead.completeKeys db r = res.keys := by
|
||
rw [completeKeys_eq_inRange_of_not_rev hrev, hresB, RangeRead.inRange]
|
||
exact hwindow
|
||
exact ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩
|
||
|
||
|
||
/-- Nonclamping finite high cell: compatibility is exactly the `[end.key, M)` filter
|
||
equality. -/
|
||
theorem high_nonclamp_finite_compatible_iff_filter {db : DB} {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hfinite : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
RangeRead.Compatible db r res ↔
|
||
db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < negCrossCompleteHighM r res)) = res.keys :=
|
||
⟨fun hcomp => high_nonclamp_finite_filter_eq hcomp hrev hmore hne hbegin hend hEH hcrossD
|
||
hnonclamp hfinite,
|
||
high_nonclamp_finite_compatible_of_filter hread hrev hmore hne hbegin hend hEH hcrossD
|
||
hnonclamp hfinite⟩
|
||
|
||
/-- Nonclamping finite crossed negative-begin complete high cell: the minimal range is
|
||
`[end.key, max begin.key (keyAfter last))`. -/
|
||
theorem minimal_closedForm_of_negCrossCompleteHigh_nonclamp_finite {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hfinite : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
Minimal r res ⟨r.end_.key, negCrossCompleteHighM r res⟩ :=
|
||
minimal_of_compatible_iff_filter hread (fun _db =>
|
||
high_nonclamp_finite_compatible_iff_filter hread hrev hmore hne hbegin hend hEH hcrossD
|
||
hnonclamp hfinite)
|
||
|
||
|
||
/-- The nonclamping finite branch of the crossed negative-begin complete high cell. -/
|
||
abbrev negCrossCompleteHighNonclampFinite (r : RangeRead) (res : Result) : Prop :=
|
||
negCrossCompleteHigh r res ∧ beginSlack r < reportedBelowBegin r res ∧
|
||
res.keys.length = (r.end_.offset - 1).toNat
|
||
|
||
/-- The nonclamping low-offset branch of the crossed negative-begin complete high
|
||
cell. -/
|
||
abbrev negCrossCompleteHighNonclampLow (r : RangeRead) (res : Result) : Prop :=
|
||
negCrossCompleteHigh r res ∧ beginSlack r < reportedBelowBegin r res ∧
|
||
res.keys.length < (r.end_.offset - 1).toNat
|
||
|
||
/-- **The nonclamping finite branch of the crossed negative-begin complete high cell is
|
||
minimal** for the total closed form. -/
|
||
theorem minimal_closedForm_of_negCrossCompleteHigh_nonclampFinite {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossCompleteHighNonclampFinite r res) :
|
||
Minimal r res (closedFormNegCrossCompleteHigh r res) := by
|
||
obtain ⟨⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩, hnonclamp, hfinite⟩ := hcell
|
||
have hmin := minimal_closedForm_of_negCrossCompleteHigh_nonclamp_finite hread hrev hmore hne
|
||
hbegin hend2 hEH hcrossD hnonclamp hfinite
|
||
unfold closedFormNegCrossCompleteHigh
|
||
rw [ite_eq_right (not_le.mpr hnonclamp)]
|
||
rw [ite_eq_left hfinite]
|
||
exact hmin
|
||
|
||
|
||
/-- Filtering at or above a bound and below `⊤` is the `above` list. -/
|
||
theorem filter_le_top_eq_above_aux (db : DB) (E : WithTop Key) :
|
||
db.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = above db E := by
|
||
rw [above_eq_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact and_iff_left (WithTop.coe_lt_top k)
|
||
|
||
/-- The end resolution overshoots in the nonclamping low branch with no capacity. -/
|
||
theorem high_nonclamp_low_resolveEnd_top_of_compat {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hno : ¬ (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤
|
||
intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)) :
|
||
resolve db r.end_ = (⊤ : WithTop Key) := by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
set D : ℕ := e - t with hD
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL]
|
||
rfl
|
||
-- `m = #stored in [E, H)`.
|
||
have hEHcoe : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH
|
||
have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEHcoe
|
||
set m : ℕ := (between db E ((H : Key) : WithTop Key)).length with hm
|
||
-- count identity at the end resolution
|
||
have hanchor : (r.end_.anchor db).toNat = cnt db E + e := by
|
||
rw [anchor_toNat_eq (s := r.end_) (by omega), hE, he]
|
||
rfl
|
||
have hcnt_end : cnt db (resolve db r.end_) = min (cnt db E + e) db.keys.length := by
|
||
rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl,
|
||
countP_lt_resolve (db := db) (s := r.end_), hanchor]
|
||
have hcntH_split : cnt db ((H : Key) : WithTop Key) = cnt db E + m := by
|
||
rw [hm]; exact cnt_eq_add_between db hEB
|
||
have hHmem_between : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [← hwin]; rw [hH]; exact headD_mem hne
|
||
have hH_le_res : ((H : Key) : WithTop Key) ≤ resolve db r.end_ :=
|
||
le_of_lt (of_decide_eq_true (List.mem_filter.mp hHmem_between).2).2
|
||
have hcnt_win : cnt db (resolve db r.end_) =
|
||
cnt db ((H : Key) : WithTop Key) + t := by
|
||
rw [cnt_eq_add_between db hH_le_res, ← hwin, ← ht]
|
||
-- `m ≤ intervalCard E H < D`, hence `m + t < e`.
|
||
have hmcard : ((m : ℕ) : ℕ∞) ≤ intervalCard E ((H : Key) : WithTop Key) := by
|
||
rw [hm]; exact between_length_le_intervalCard hEB
|
||
have hcardD : intervalCard E ((H : Key) : WithTop Key) < ((D : ℕ) : ℕ∞) := by
|
||
rw [hE, hH, hD, he, ht] at hno ⊢
|
||
exact lt_of_not_ge hno
|
||
have hmD : m < D := by
|
||
have : ((m : ℕ) : ℕ∞) < ((D : ℕ) : ℕ∞) := lt_of_le_of_lt hmcard hcardD
|
||
exact_mod_cast this
|
||
have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow)
|
||
have hmte : m + t < e := by omega
|
||
-- Now compute the count of the end resolution and show it is `⊤`.
|
||
have hcnt_lower : cnt db (resolve db r.end_) = cnt db E + m + t := by
|
||
rw [hcnt_win, hcntH_split]
|
||
by_contra hnot
|
||
have hlt : (r.end_.anchor db).toNat < db.keys.length := by
|
||
by_contra hge
|
||
exact hnot (resolve_eq_top (by omega) (le_of_not_gt hge))
|
||
have hmin : min (cnt db E + e) db.keys.length = cnt db E + e :=
|
||
Nat.min_eq_left (by rw [← hanchor]; exact le_of_lt hlt)
|
||
rw [hcnt_end, hmin] at hcnt_lower
|
||
omega
|
||
|
||
/-- **No-cap nonclamp branch: compatibility is exactly agreement of the keys at or
|
||
above the reported head with the result.** -/
|
||
theorem high_nonclamp_low_aboveH_iff {db : DB} {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hno : ¬ (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤
|
||
intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)) :
|
||
RangeRead.Compatible db r res ↔ above db ((res.keys.headD ([] : Key) : Key) : WithTop Key) =
|
||
res.keys := by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
set D : ℕ := e - t with hD
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne
|
||
have hEHcoe : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH
|
||
have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEHcoe
|
||
have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow)
|
||
constructor
|
||
· intro hcomp
|
||
have htop := high_nonclamp_low_resolveEnd_top_of_compat hcomp hrev hmore hne hbegin hend
|
||
hEH hnonclamp hlow hno
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL]; rfl
|
||
rw [hwin, htop]
|
||
exact (filter_le_top_eq_above_aux db ((H : Key) : WithTop Key)).symm
|
||
· intro habove
|
||
-- forward construction of compatibility
|
||
have hwl : RangeRead.withinLimit r res.keys := by
|
||
obtain ⟨db0, hcomp0⟩ := hread
|
||
exact RangeRead.compatible_withinLimit hcomp0
|
||
have hHmem_db : H ∈ db.keys := by
|
||
have : H ∈ above db ((H : Key) : WithTop Key) := by rw [habove]; exact hHmem
|
||
exact (mem_above.mp this).1
|
||
have hcross : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD
|
||
-- filter `[H, begin.key)` has length `q + 1`.
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ hread hrev hmore hne hbegin hcrossD hnonclamp
|
||
have haboveH : above db ((H : Key) : WithTop Key) = res.keys := habove
|
||
have hfilterHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
have h1 : db.keys.filter (fun k : Key =>
|
||
decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) =
|
||
(above db ((H : Key) : WithTop Key)).filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
rw [above_eq_filter, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) <;>
|
||
simp [hk]
|
||
rw [h1, haboveH]
|
||
rw [reportedBelowBegin, List.countP_eq_length_filter] at hr
|
||
exact hr
|
||
have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by
|
||
exact resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem_db hcross hfilterHB
|
||
-- the end resolution is `⊤`
|
||
have hanchor : (r.end_.anchor db).toNat = cnt db E + e := by
|
||
rw [anchor_toNat_eq (s := r.end_) (by omega), hE, he]
|
||
rfl
|
||
have hcnt_above : (above db E).length = (between db E ((H : Key) : WithTop Key)).length +
|
||
(above db ((H : Key) : WithTop Key)).length := by
|
||
have h1 := cnt_add_above_length db E
|
||
have h2 := cnt_eq_add_between db hEB
|
||
have h3 := cnt_add_above_length db ((H : Key) : WithTop Key)
|
||
omega
|
||
have haboveHlen : (above db ((H : Key) : WithTop Key)).length = t := by
|
||
rw [haboveH, ht]
|
||
have hlenE : (above db E).length = (between db E ((H : Key) : WithTop Key)).length + t := by
|
||
rw [hcnt_above, haboveHlen]
|
||
have hmcard : ((between db E ((H : Key) : WithTop Key)).length : ℕ∞) ≤
|
||
intervalCard E ((H : Key) : WithTop Key) := between_length_le_intervalCard hEB
|
||
have hcardD : intervalCard E ((H : Key) : WithTop Key) < ((D : ℕ) : ℕ∞) := by
|
||
rw [hE, hH, hD, he, ht] at hno ⊢
|
||
exact lt_of_not_ge hno
|
||
have hmD : (between db E ((H : Key) : WithTop Key)).length < D := by
|
||
have : ((between db E ((H : Key) : WithTop Key)).length : ℕ∞) < ((D : ℕ) : ℕ∞) :=
|
||
lt_of_le_of_lt hmcard hcardD
|
||
exact_mod_cast this
|
||
have hlen_lt : db.keys.length < cnt db E + e := by
|
||
have hlen : db.keys.length = cnt db E + (above db E).length :=
|
||
(cnt_add_above_length db E).symm
|
||
rw [hlen, hlenE]
|
||
omega
|
||
have htop : resolve db r.end_ = (⊤ : WithTop Key) := by
|
||
apply resolve_eq_top (by omega)
|
||
rw [hanchor]; omega
|
||
refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩
|
||
rw [RangeRead.completeKeys, hrev]
|
||
simp only [Bool.false_eq_true, ite_false]
|
||
rw [hL, htop]
|
||
-- between H top = above H = res.keys
|
||
change res.keys = List.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) db.keys
|
||
exact haboveH.symm.trans (filter_le_top_eq_above_aux db ((H : Key) : WithTop Key)).symm
|
||
|
||
/-- **The no-cap nonclamping low branch is `[head, ⊤)`.** -/
|
||
theorem minimal_nonclampLow_noCap {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hno : ¬ (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤
|
||
intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)) :
|
||
Minimal r res ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), ⊤⟩ := by
|
||
refine minimal_of_compatible_iff_filter hread (fun db => ?_)
|
||
rw [high_nonclamp_low_aboveH_iff hread hrev hmore hne hbegin hend hEH hcrossD hnonclamp hlow hno,
|
||
filter_le_top_eq_above_aux]
|
||
|
||
/-- A genuine key strictly above the empty key is nonempty. -/
|
||
theorem ne_nil_of_bot_lt {E : WithTop Key} {H : Key} (h : E < (H : WithTop Key)) :
|
||
H ≠ [] := by
|
||
intro hH
|
||
rw [hH] at h
|
||
exact (not_lt.mpr (coe_nil_le_key_withTop E)) h
|
||
|
||
/-- **Soundness of `[end_.key, ⊤)` in the nonclamping low branch of the crossed
|
||
negative-begin complete high cell.** Agreement on the keys at or above `end_.key`
|
||
fixes the end resolution (positive offset) and, through the reported head and the
|
||
interval `[end_.key, begin.key)`, the begin resolution (negative offset). The complete
|
||
window then starts at the reported head, which lies at or above `end_.key`, so it too is
|
||
fixed. -/
|
||
theorem sound_topRange_endKey_nonclamp_low {r : RangeRead} {res : Result}
|
||
(_hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hend : 2 ≤ r.end_.offset) :
|
||
Sound r res ⟨r.end_.key, ⊤⟩ := by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hEH' : r.end_.key < ((H : Key) : WithTop Key) := by rw [hH]; exact hEH
|
||
have hE_le_H : r.end_.key ≤ ((H : Key) : WithTop Key) := le_of_lt hEH'
|
||
have hcrossE : r.end_.key < r.begin.key := lt_trans hEH' hcrossD
|
||
have step : ∀ db1 db2 : DB, AgreeOn ⟨r.end_.key, ⊤⟩ db1 db2 →
|
||
RangeRead.Compatible db1 r res → RangeRead.Compatible db2 r res := by
|
||
intro db1 db2 hagree hcomp1
|
||
have habove : above db1 r.end_.key = above db2 r.end_.key := by
|
||
have h := hagree
|
||
rw [AgreeOn, keys_top_eq_above, keys_top_eq_above] at h
|
||
exact h
|
||
have hag : ∀ k : Key, r.end_.key ≤ (k : WithTop Key) →
|
||
(k ∈ db1.keys ↔ k ∈ db2.keys) := by
|
||
intro k hk
|
||
have h := congrArg (fun l : List Key => k ∈ l) habove
|
||
rw [mem_above, mem_above] at h
|
||
exact ⟨fun hk1 => (h.mp ⟨hk1, hk⟩).1, fun hk2 => (h.mpr ⟨hk2, hk⟩).1⟩
|
||
have hRend : resolve db1 r.end_ = resolve db2 r.end_ :=
|
||
resolve_eq_of_agree_ge (db₁ := db1) (db₂ := db2) (s := r.end_) (by omega) hag
|
||
have hHne : H ≠ [] := ne_nil_of_bot_lt
|
||
(lt_of_le_of_lt (coe_nil_le_key_withTop r.end_.key) hEH')
|
||
have hRbeg1 : resolve db1 r.begin = ((H : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact resolve_begin_eq_head_of_negCross_compat' hcomp1 hrev hmore hne hbegin hnonclamp
|
||
obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (e : WithTop Key) := by
|
||
cases h : r.end_.key with
|
||
| top => rw [h] at hEH; exact absurd hEH (not_lt.mpr le_top)
|
||
| coe e => exact ⟨e, rfl⟩
|
||
have hbetween_agree : between db1 r.end_.key r.begin.key =
|
||
between db2 r.end_.key r.begin.key := by
|
||
rw [between_eq_filter_above, between_eq_filter_above, habove]
|
||
have hq := resolve_filter_reverse_eq_some_of_ne_nil (db := db1) (s := r.begin)
|
||
(le_of_lt hbegin) hHne hRbeg1
|
||
have hRbeg2 : resolve db2 r.begin = ((H : Key) : WithTop Key) := by
|
||
refine resolve_eq_of_agree_between (db₁ := db1) (db₂ := db2) (s := r.begin)
|
||
(le_of_lt hbegin) (a := e) (E := r.begin.key) (q := H) rfl ?_ ?_ hq ?_
|
||
· rw [← he]; exact le_of_lt hcrossE
|
||
· simpa [he, between] using hbetween_agree
|
||
· rw [← he]; exact le_of_lt hEH'
|
||
have hck : RangeRead.completeKeys db1 r = RangeRead.completeKeys db2 r := by
|
||
have h1 : RangeRead.completeKeys db1 r =
|
||
between db1 ((H : Key) : WithTop Key) (resolve db1 r.end_) := by
|
||
rw [completeKeys_eq_inRange_of_not_rev hrev, hRbeg1]; rfl
|
||
have h2 : RangeRead.completeKeys db2 r =
|
||
between db2 ((H : Key) : WithTop Key) (resolve db2 r.end_) := by
|
||
rw [completeKeys_eq_inRange_of_not_rev hrev, hRbeg2]; rfl
|
||
rw [h1, h2, hRend, between_eq_filter_above, between_eq_filter_above,
|
||
above_B_eq_filter (db := db1) hE_le_H, above_B_eq_filter (db := db2) hE_le_H,
|
||
habove]
|
||
exact (RangeRead.compatible_congr hck).mp hcomp1
|
||
intro db1 db2 hagree
|
||
exact ⟨step db1 db2 hagree, step db2 db1 hagree.symm⟩
|
||
|
||
/-- The stored keys of the top range starting at `E` are the stored keys at or above `E`. -/
|
||
theorem between_top_eq_above (db : DB) (E : WithTop Key) :
|
||
between db E (⊤ : WithTop Key) = above db E := by
|
||
unfold between above
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact and_iff_left (WithTop.coe_lt_top k)
|
||
|
||
/-- In the nonclamping low branch of the crossed negative-begin complete high cell, the
|
||
reported list itself is a compatible database. The begin selector resolves onto the
|
||
reported head and the end selector overshoots the finite list. -/
|
||
theorem high_nonclamp_low_base_compatible {r : RangeRead} {res : Result}
|
||
{hsort : res.keys.SortedLT}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat) :
|
||
RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne
|
||
have hcross : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD
|
||
have hwl : RangeRead.withinLimit r res.keys := by
|
||
obtain ⟨db0, hcomp0⟩ := hread
|
||
exact RangeRead.compatible_withinLimit hcomp0
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ hread hrev hmore hne hbegin hcrossD hnonclamp
|
||
have hfilter : (res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
have h2 : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
apply List.filter_congr
|
||
intro k hk
|
||
have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk)
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨hHk, h⟩⟩
|
||
rw [reportedBelowBegin, List.countP_eq_length_filter] at hr
|
||
rw [h2]
|
||
exact hr
|
||
have hL : resolve (⟨res.keys, hsort⟩ : DB) r.begin = ((H : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem hcross hfilter
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
have hcE : (⟨res.keys, hsort⟩ : DB).keys.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (le_of_lt (high_reported_gt_end hsort hne hEH k hk))
|
||
have hepos : 1 ≤ r.end_.offset := by omega
|
||
have hDpos : 0 < e - t := by rw [he, ht]; omega
|
||
have hanchor : (r.end_.anchor (⟨res.keys, hsort⟩ : DB)).toNat = e := by
|
||
rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add, he]
|
||
have htop : resolve (⟨res.keys, hsort⟩ : DB) r.end_ = (⊤ : WithTop Key) := by
|
||
apply resolve_eq_top
|
||
· unfold Selector.anchor
|
||
rw [hcE]
|
||
have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
simpa using this
|
||
· rw [hanchor]
|
||
change res.keys.length ≤ e
|
||
rw [← ht]
|
||
omega
|
||
have hbetween : between (⟨res.keys, hsort⟩ : DB) ((H : Key) : WithTop Key)
|
||
(⊤ : WithTop Key) = res.keys := by
|
||
rw [between_top_eq_above]
|
||
unfold above
|
||
rw [List.filter_eq_self]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_le_coe.mpr (by
|
||
rw [hH, hheadD]
|
||
exact head_le_of_sortedLT' hsort hne hk)
|
||
refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩
|
||
rw [RangeRead.completeKeys, hrev]
|
||
simp only [Bool.false_eq_true, ite_false]
|
||
rw [hL, htop]
|
||
exact hbetween.symm
|
||
|
||
/-- Keys at or above the reported head are necessary in the nonclamping low branch of the
|
||
crossed negative-begin complete high cell. -/
|
||
theorem necessary_above_head_nonclamp_low {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hkH : ((res.keys.headD ([] : Key) : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key))
|
||
(hknot : k ∉ res.keys) :
|
||
Necessary r res k := by
|
||
have hread0 : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread0
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
have hEH' : r.end_.key < ((H : Key) : WithTop Key) := by rw [hH]; exact hEH
|
||
have hbase : RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res :=
|
||
high_nonclamp_low_base_compatible (hsort := hsort) hread hrev hmore hne hbegin hEH hcrossD
|
||
hnonclamp hlow
|
||
have hknot0 : k ∉ (⟨res.keys, hsort⟩ : DB).keys := hknot
|
||
refine necessary_of_toggle ⟨⟨res.keys, hsort⟩, hbase, ?_⟩
|
||
rw [toggle_eq_insert_of_not_mem hknot0]
|
||
intro hcompIns
|
||
have hRb := resolve_begin_eq_head_of_negCross_compat' hcompIns hrev hmore hne hbegin hnonclamp
|
||
set dbIns : DB := (⟨res.keys, hsort⟩ : DB).insert k hknot0 with hdbIns
|
||
have hcE : dbIns.keys.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [hdbIns, DB.insert_keys, List.countP_eq_zero]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact not_lt.mpr (le_trans (le_of_lt hEH') hkH)
|
||
· exact not_lt.mpr (le_of_lt (high_reported_gt_end hsort hne hEH x hx))
|
||
have hepos : 1 ≤ r.end_.offset := by omega
|
||
have hanchor : (r.end_.anchor dbIns).toNat = (r.end_.offset - 1).toNat := by
|
||
rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add]
|
||
have hlen : dbIns.keys.length = res.keys.length + 1 := by
|
||
rw [hdbIns, DB.insert_keys, List.orderedInsert_length]
|
||
have htop : resolve dbIns r.end_ = (⊤ : WithTop Key) := by
|
||
apply resolve_eq_top
|
||
· unfold Selector.anchor
|
||
rw [hcE]
|
||
have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
simpa using this
|
||
· rw [hanchor, hlen]; omega
|
||
have hcomplete : res.keys = RangeRead.completeKeys dbIns r := by
|
||
rcases hcompIns.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hheadHeq : ((res.keys.head hne : Key) : WithTop Key) = ((H : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
have hbetween : RangeRead.completeKeys dbIns r = above dbIns ((H : Key) : WithTop Key) := by
|
||
rw [completeKeys_eq_inRange_of_not_rev hrev, hRb, htop, hheadHeq]
|
||
exact between_top_eq_above dbIns ((H : Key) : WithTop Key)
|
||
have hkabove : k ∈ above dbIns ((H : Key) : WithTop Key) := by
|
||
rw [mem_above]
|
||
exact ⟨by rw [hdbIns, DB.insert_keys]; exact (List.mem_orderedInsert (· ≤ ·)).mpr (Or.inl rfl),
|
||
hkH⟩
|
||
rw [← hbetween, ← hcomplete] at hkabove
|
||
exact hknot hkabove
|
||
|
||
theorem high_nonclamp_low_pad_compatible {r : RangeRead} {res : Result} {pad : List Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hpad : ∀ x ∈ pad, r.end_.key ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hps : pad.SortedLT)
|
||
(hlen : pad.length ≤ negCrossHighD r res) :
|
||
∃ hs : (pad ++ res.keys).SortedLT,
|
||
RangeRead.Compatible ⟨pad ++ res.keys, hs⟩ r res := by
|
||
have hread0 : Readable r res := hread
|
||
obtain ⟨db0, hcomp0⟩ := hread
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev
|
||
have hwl : RangeRead.withinLimit r res.keys :=
|
||
RangeRead.compatible_withinLimit hcomp0
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
set D : ℕ := e - t with hD
|
||
have hDdef : negCrossHighD r res = D := by
|
||
unfold negCrossHighD; rw [← he, ← ht, ← hD]
|
||
have hhlen : pad.length ≤ D := hDdef ▸ hlen
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH
|
||
have hcross : ∀ x ∈ pad, ∀ y ∈ res.keys, x < y := by
|
||
intro x hx y hy
|
||
have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by
|
||
rw [hH]; exact (hpad x hx).2
|
||
have hyH : ((H : Key) : WithTop Key) ≤ ((y : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hy)
|
||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hxH hyH)
|
||
refine ⟨List.sortedLT_append.mpr ⟨hps, hsort, hcross⟩, ?_⟩
|
||
set db : DB := ⟨pad ++ res.keys, List.sortedLT_append.mpr ⟨hps, hsort, hcross⟩⟩ with hdb
|
||
have hkeys : db.keys = pad ++ res.keys := rfl
|
||
-- all keys are at or above `E`
|
||
have hge : ∀ x ∈ db.keys, E ≤ ((x : Key) : WithTop Key) := by
|
||
intro x hx
|
||
rw [hkeys, List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· rw [hE]; exact (hpad x hx).1
|
||
· rw [hE]
|
||
exact le_of_lt (lt_of_lt_of_le hEH' (by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx)))
|
||
have hcntE : db.keys.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
rw [decide_eq_true_eq, ← hE]
|
||
exact not_lt.mpr (hge x hx)
|
||
have hepos : 1 ≤ r.end_.offset := by omega
|
||
have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow)
|
||
have hlenDB : db.keys.length = pad.length + t := by rw [hkeys, List.length_append, ht]
|
||
-- the end selector overshoots
|
||
have hanchorE : (r.end_.anchor db).toNat = e := by
|
||
rw [anchor_toNat_eq (s := r.end_) hepos, hcntE, Nat.zero_add, he]
|
||
have htop : resolve db r.end_ = (⊤ : WithTop Key) := by
|
||
apply resolve_eq_top
|
||
· unfold Selector.anchor
|
||
rw [hcntE]
|
||
have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
simpa using this
|
||
· rw [hanchorE, hlenDB]
|
||
omega
|
||
-- the begin selector resolves onto `H`
|
||
have hHmem : H ∈ db.keys := by
|
||
rw [hkeys, List.mem_append]
|
||
exact Or.inr (by rw [hH, hheadD]; exact List.head_mem hne)
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ hread0 hrev hmore hne hbegin hcrossD hnonclamp
|
||
have hfilter : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
rw [hkeys, List.filter_append]
|
||
have hnil : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.1 (not_le.mpr (by rw [hH]; exact (hpad x hx).2))
|
||
rw [hnil, List.nil_append]
|
||
have hcongr : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
apply List.filter_congr
|
||
intro k hk
|
||
have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk)
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨hHk, h⟩⟩
|
||
rw [hcongr]
|
||
rw [reportedBelowBegin, List.countP_eq_length_filter] at hr
|
||
exact hr
|
||
have hRbeg : resolve db r.begin = ((H : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem
|
||
(by rw [hH]; exact hcrossD) hfilter
|
||
-- the complete window is the reported list
|
||
have hcomplete : RangeRead.completeKeys db r = res.keys := by
|
||
rw [RangeRead.completeKeys, hrev]
|
||
simp only [Bool.false_eq_true, ite_false]
|
||
rw [hRbeg, htop]
|
||
unfold RangeRead.inRange
|
||
rw [filter_le_top_eq_above_aux]
|
||
unfold above
|
||
rw [hkeys, List.filter_append]
|
||
have hnil : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_le.mpr (by rw [hH]; exact (hpad x hx).2)
|
||
rw [hnil, List.nil_append]
|
||
rw [List.filter_eq_self]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk)
|
||
exact ⟨hwl, Or.inr ⟨hmore, hcomplete.symm⟩⟩
|
||
|
||
/-- In the nonclamping low branch, every compatible database stores at most
|
||
`D = (end.offset - 1).toNat - #reported` keys in the gap `[end.key, reported head)`. -/
|
||
theorem high_nonclamp_low_pad_count_le {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat) :
|
||
(between db r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)).length ≤
|
||
negCrossHighD r res := by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
set D : ℕ := e - t with hD
|
||
have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD]
|
||
rw [hDdef]
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL]
|
||
rfl
|
||
have hEHcoe : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH
|
||
have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEHcoe
|
||
set m : ℕ := (between db E ((H : Key) : WithTop Key)).length with hm
|
||
have hanchor : (r.end_.anchor db).toNat = cnt db E + e := by
|
||
rw [anchor_toNat_eq (s := r.end_) (by omega), hE, he]
|
||
rfl
|
||
have hcnt_end : cnt db (resolve db r.end_) = min (cnt db E + e) db.keys.length := by
|
||
rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl,
|
||
countP_lt_resolve (db := db) (s := r.end_), hanchor]
|
||
have hcntH_split : cnt db ((H : Key) : WithTop Key) = cnt db E + m := by
|
||
rw [hm]; exact cnt_eq_add_between db hEB
|
||
have hHmem_between : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [← hwin]; rw [hH]; exact headD_mem hne
|
||
have hH_le_res : ((H : Key) : WithTop Key) ≤ resolve db r.end_ :=
|
||
le_of_lt (of_decide_eq_true (List.mem_filter.mp hHmem_between).2).2
|
||
have hcnt_win : cnt db (resolve db r.end_) =
|
||
cnt db ((H : Key) : WithTop Key) + t := by
|
||
rw [cnt_eq_add_between db hH_le_res, ← hwin, ← ht]
|
||
have hcnt_lower : cnt db (resolve db r.end_) = cnt db E + m + t := by
|
||
rw [hcnt_win, hcntH_split]
|
||
have hmte : m + t ≤ e := by
|
||
rw [hcnt_lower, hcnt_end] at *
|
||
have := Nat.min_le_left (cnt db E + e) db.keys.length
|
||
omega
|
||
have hmD : m ≤ D := by rw [hD]; omega
|
||
exact hmD
|
||
|
||
/-- Gap keys `[end.key, head)` are necessary in the nonclamping low branch when the gap
|
||
strictly exceeds the excess `D`: inserting the key into a maximal pad makes one key too
|
||
many. -/
|
||
theorem necessary_gap_nonclamp_low_lt_cap {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hkE : r.end_.key ≤ ((k : Key) : WithTop Key))
|
||
(hkH : ((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hknot : k ∉ res.keys)
|
||
(hlt : ((negCrossHighD r res : ℕ) : ℕ∞) < negCrossHighGapCap r res) :
|
||
Necessary r res k := by
|
||
have hcap : (((negCrossHighD r res + 1 : ℕ) : ℕ∞)) ≤ negCrossHighGapCap r res := by
|
||
have h := (ENat.add_one_le_iff (by simp)).mpr hlt
|
||
simpa [Nat.cast_add, Nat.cast_one] using h
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
have hcap' : (((negCrossHighD r res + 1 : ℕ) : ℕ∞)) ≤ intervalCard E ((H : Key) : WithTop Key)
|
||
:= by
|
||
simpa [negCrossHighGapCap, hE, hH] using hcap
|
||
obtain ⟨dbPad, hdbPadIn, hkpad, hpadlen⟩ :=
|
||
exists_db_keysIn_avoid (B := E) (E := ((H : Key) : WithTop Key)) (k := k)
|
||
(m := negCrossHighD r res) hcap'
|
||
set pad : List Key := dbPad.keys with hpaddef
|
||
have hpadIn : ∀ x ∈ pad, E ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by
|
||
intro x hx
|
||
simpa only [pad, keysIn, Set.mem_ofPred_eq] using hdbPadIn x hx
|
||
have hps : pad.SortedLT := dbPad.sorted
|
||
have hplen : pad.length = negCrossHighD r res := hpadlen
|
||
obtain ⟨hs0, hcomp0⟩ := high_nonclamp_low_pad_compatible (pad := pad)
|
||
hread hrev hmore hne hbegin hEH hcrossD hnonclamp hlow hpadIn hps (le_of_eq hplen)
|
||
set db0 : DB := ⟨pad ++ res.keys, hs0⟩ with hdb0
|
||
have hknot0 : k ∉ db0.keys := by
|
||
rw [hdb0]
|
||
simp only
|
||
rw [List.mem_append]
|
||
push Not
|
||
exact ⟨hkpad, hknot⟩
|
||
refine necessary_of_toggle ⟨db0, hcomp0, ?_⟩
|
||
rw [toggle_eq_insert_of_not_mem hknot0]
|
||
intro hcompIns
|
||
set db1 : DB := db0.insert k hknot0 with hdb1
|
||
have hle := high_nonclamp_low_pad_count_le hcompIns hrev hmore hne hbegin (by omega)
|
||
hEH hnonclamp hlow
|
||
-- reduce to the two count values
|
||
have hcntH1 : cnt db1 ((H : Key) : WithTop Key) = cnt db0 ((H : Key) : WithTop Key) + 1 := by
|
||
rw [hdb1]
|
||
unfold cnt
|
||
rw [DB.insert_keys]
|
||
have := countP_orderedInsert_begin (l := db0.keys) db0.sorted hknot0
|
||
(B := ((H : Key) : WithTop Key))
|
||
have hkH' : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by rw [hH]; exact hkH
|
||
simpa [hkH'] using this
|
||
have hcntE1 : cnt db1 E = cnt db0 E := by
|
||
rw [hdb1]
|
||
unfold cnt
|
||
rw [DB.insert_keys]
|
||
have := countP_orderedInsert_begin (l := db0.keys) db0.sorted hknot0 (B := E)
|
||
have hkE' : ¬ ((k : Key) : WithTop Key) < E := not_lt.mpr (by rw [hE]; exact hkE)
|
||
simpa [hkE'] using this
|
||
have hbetween1 : (between db1 E ((H : Key) : WithTop Key)).length =
|
||
(between db0 E ((H : Key) : WithTop Key)).length + 1 := by
|
||
have hEB : E ≤ ((H : Key) : WithTop Key) := by rw [hE, hH]; exact le_of_lt hEH
|
||
have h1 := cnt_eq_add_between (db := db1) hEB
|
||
have h0 := cnt_eq_add_between (db := db0) hEB
|
||
rw [hcntH1, hcntE1] at h1
|
||
omega
|
||
have hbetween0 : (between db0 E ((H : Key) : WithTop Key)).length = negCrossHighD r res := by
|
||
rw [hdb0]
|
||
unfold between
|
||
rw [List.filter_append]
|
||
have hpadf : pad.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = pad := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hpadIn x hx
|
||
have hresf : res.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.2 (not_lt.mpr (by
|
||
rw [hH]
|
||
exact WithTop.coe_le_coe.mpr (by
|
||
rw [headD_eq_head_of_ne hne]
|
||
exact head_le_of_sortedLT'
|
||
(RangeRead.compatible_sortedLT hcomp0 hrev) hne hx)))
|
||
rw [hpadf, hresf, List.append_nil]
|
||
exact hplen
|
||
rw [hbetween0] at hbetween1
|
||
have hle' : (between db1 E ((H : Key) : WithTop Key)).length ≤ negCrossHighD r res := by
|
||
simpa only [← hE, ← hH] using hle
|
||
omega
|
||
|
||
/-- **A compatible database in the nonclamping low branch, with a tail key.** Building on
|
||
the maximal pad inside `[end.key, head)`, appending the reported keys and one key `y`
|
||
above both the reported last and `begin.key` keeps the result compatible: the end
|
||
selector resolves onto `y`, so the complete window is still exactly the reported keys. -/
|
||
theorem high_nonclamp_low_tail_compatible {r : RangeRead} {res : Result}
|
||
{pad : List Key} {y : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hpad : ∀ x ∈ pad, r.end_.key ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hps : pad.SortedLT)
|
||
(hlen : pad.length = negCrossHighD r res)
|
||
(hys : ∀ x ∈ res.keys, x < y)
|
||
(hyB : r.begin.key < ((y : Key) : WithTop Key)) :
|
||
∃ hs : (pad ++ res.keys ++ [y]).SortedLT,
|
||
RangeRead.Compatible ⟨pad ++ res.keys ++ [y], hs⟩ r res := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hread0 : Readable r res := ⟨db₀, hcomp₀⟩
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hwl : RangeRead.withinLimit r res.keys := RangeRead.compatible_withinLimit hcomp₀
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
set D : ℕ := e - t with hD
|
||
have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD]
|
||
have hlenD : pad.length = D := by rw [hlen, hDdef]
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne
|
||
have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH
|
||
have hcrossH : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD
|
||
have hHy : H < y := hys H hHmem
|
||
have hcrosspadres : ∀ x ∈ pad, ∀ z ∈ res.keys, x < z := by
|
||
intro x hx z hz
|
||
have hxH : x < H := WithTop.coe_lt_coe.mp (hpad x hx).2
|
||
have hHz : ((H : Key) : WithTop Key) ≤ ((z : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hz)
|
||
exact lt_of_lt_of_le hxH (WithTop.coe_le_coe.mp hHz)
|
||
have hcrossPR_y : ∀ x ∈ pad ++ res.keys, x < y := by
|
||
intro x hx
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact lt_trans (WithTop.coe_lt_coe.mp (hpad x hx).2) hHy
|
||
· exact hys x hx
|
||
have hs : (pad ++ res.keys ++ [y]).SortedLT := by
|
||
rw [List.sortedLT_append]
|
||
refine ⟨?_, sortedLT_singleton y, ?_⟩
|
||
· rw [List.sortedLT_append]
|
||
exact ⟨hps, hsort, hcrosspadres⟩
|
||
· intro x hx z hz
|
||
rw [List.mem_singleton] at hz
|
||
subst hz
|
||
exact hcrossPR_y x hx
|
||
let db : DB := ⟨pad ++ res.keys ++ [y], hs⟩
|
||
have hkeys : db.keys = pad ++ res.keys ++ [y] := rfl
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ hread0 hrev hmore hne hbegin hcrossD hnonclamp
|
||
-- begin resolution via the filter length
|
||
have hHmem_db : H ∈ db.keys := by
|
||
rw [hkeys]
|
||
exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hHmem)))
|
||
have hfilterHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
rw [hkeys, List.append_assoc, List.filter_append, List.filter_append]
|
||
have hpadf : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.1 (not_le.mpr (by rw [hH]; exact (hpad x hx).2))
|
||
have hresf : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
apply List.filter_congr
|
||
intro k hk
|
||
have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk)
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨hHk, h⟩⟩
|
||
have hyf : [y].filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [List.mem_singleton] at hx
|
||
subst hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.2 (not_lt.mpr (le_of_lt hyB))
|
||
rw [hpadf, hresf, hyf]
|
||
simp only [List.nil_append, List.append_nil]
|
||
rw [reportedBelowBegin, List.countP_eq_length_filter] at hr
|
||
exact hr
|
||
have hRbeg : resolve db r.begin = ((H : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem_db hcrossH hfilterHB
|
||
-- end resolution: all keys are at or above `E`
|
||
have hge : ∀ x ∈ db.keys, E ≤ ((x : Key) : WithTop Key) := by
|
||
intro x hx
|
||
rw [hkeys, List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· rcases List.mem_append.mp hx with hx | hx
|
||
· exact (hpad x hx).1
|
||
· have hHx : ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx)
|
||
exact le_of_lt (lt_of_lt_of_le hEH' hHx)
|
||
· rw [List.mem_singleton] at hx
|
||
subst hx
|
||
exact le_of_lt (lt_trans hEH' (WithTop.coe_lt_coe.mpr hHy))
|
||
have hcntE : db.keys.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
rw [decide_eq_true_eq, ← hE]
|
||
exact not_lt.mpr (hge x hx)
|
||
have hepos : 1 ≤ r.end_.offset := by omega
|
||
have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow)
|
||
have hlenDB : db.keys.length = D + t + 1 := by
|
||
rw [hkeys]
|
||
simp only [List.length_append, List.length_singleton]
|
||
rw [hlenD, ← ht]
|
||
have hanchorE : (r.end_.anchor db).toNat = e := by
|
||
rw [anchor_toNat_eq (s := r.end_) hepos, hcntE, Nat.zero_add, he]
|
||
have hgetE : db.keys[e]? = some y := by
|
||
rw [hkeys, List.append_assoc]
|
||
rw [List.getElem?_append_right (by rw [hlenD]; omega)]
|
||
have heD : e - pad.length = t := by rw [hlenD]; omega
|
||
rw [heD]
|
||
rw [List.getElem?_append_right (by rw [ht])]
|
||
simp [ht]
|
||
have hRend : resolve db r.end_ = ((y : Key) : WithTop Key) := by
|
||
have h0 : 0 ≤ r.end_.anchor db := by
|
||
have hepos' : 0 < e := by omega
|
||
omega
|
||
rw [resolve, ite_eq_right (not_lt.mpr h0), hanchorE, hgetE]
|
||
-- complete window
|
||
have hcomplete : res.keys = RangeRead.completeKeys db r := by
|
||
rw [RangeRead.completeKeys, hrev]
|
||
simp only [Bool.false_eq_true, ite_false]
|
||
rw [RangeRead.inRange, hRbeg, hRend]
|
||
rw [hkeys, List.filter_append, List.filter_append]
|
||
have hpadf : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((y : Key) : WithTop Key))) = []
|
||
:= by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.1 (not_le.mpr (by rw [hH]; exact (hpad x hx).2))
|
||
have hresf : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((y : Key) : WithTop Key))) =
|
||
res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact ⟨by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx),
|
||
WithTop.coe_lt_coe.mpr (hys x hx)⟩
|
||
have hyf : [y].filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((y : Key) : WithTop Key))) = []
|
||
:= by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [List.mem_singleton] at hx
|
||
subst hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.2 (lt_irrefl _)
|
||
rw [hpadf, hyf]
|
||
simp only [List.nil_append, List.append_nil]
|
||
rw [hresf]
|
||
exact ⟨hs, hwl, Or.inr ⟨hmore, hcomplete⟩⟩
|
||
|
||
/-- In the nonclamping low branch with a full gap and a finite begin key, gap keys are
|
||
necessary: a database filling the gap and carrying a tail key above the reported last is
|
||
compatible, but erasing the gap key makes the end selector overshoot and exposes the
|
||
tail key in the complete window. -/
|
||
theorem necessary_gap_nonclamp_low_eq_cap {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hkE : r.end_.key ≤ ((k : Key) : WithTop Key))
|
||
(hkH : ((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hknot : k ∉ res.keys)
|
||
(hbeginTop : r.begin.key < (⊤ : WithTop Key))
|
||
(hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hread0 : Readable r res := ⟨db₀, hcomp₀⟩
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
set D : ℕ := e - t with hD
|
||
have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD]
|
||
have hDpos : 1 ≤ D := by rw [hD]; omega
|
||
have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow)
|
||
have hcapD : ((D : ℕ) : ℕ∞) = intervalCard E ((H : Key) : WithTop Key) := by
|
||
have h := hcap
|
||
rw [hDdef] at h
|
||
simpa [negCrossHighGapCap, hE, hH] using h
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne
|
||
have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH
|
||
have hHleL : H ≤ res.keys.getLastD ([] : Key) := by
|
||
rw [hH]
|
||
exact le_getLastD_of_pairwise (List.sortedLT_iff_pairwise.mp hsort) (headD_mem hne)
|
||
-- a pad of `D - 1` keys in `[end.key, head)` avoiding `k`
|
||
obtain ⟨padkDb, hpadkIn, hkpadk, hpadklen⟩ :=
|
||
exists_db_keysIn_avoid (B := E) (E := ((H : Key) : WithTop Key)) (k := k) (m := D - 1)
|
||
(by
|
||
have hsub : D - 1 + 1 = D := Nat.sub_add_cancel hDpos
|
||
have hcast : (((D - 1 : ℕ)) : ℕ∞) + 1 = (D : ℕ∞) := by
|
||
have : (((D - 1 : ℕ)) : ℕ∞) + 1 = (((D - 1 + 1 : ℕ)) : ℕ∞) := by
|
||
rw [Nat.cast_add, Nat.cast_one]
|
||
rw [this, hsub]
|
||
rw [hcast]
|
||
exact le_of_eq hcapD)
|
||
set padk : List Key := padkDb.keys with hpadkdef
|
||
have hpadkIn' : ∀ x ∈ padk, E ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by
|
||
intro x hx
|
||
simpa only [padk, keysIn, Set.mem_ofPred_eq] using hpadkIn x hx
|
||
have hpadks : padk.SortedLT := padkDb.sorted
|
||
have hpadklen' : padk.length = D - 1 := hpadklen
|
||
-- insert `k` into the pad
|
||
set padDb : DB := (⟨padk, hpadks⟩ : DB).insert k hkpadk with hpadDb
|
||
have hpadDbkeys : padDb.keys = padk.orderedInsert (· ≤ ·) k := rfl
|
||
set pad : List Key := padDb.keys with hpaddef
|
||
have hps : pad.SortedLT := padDb.sorted
|
||
have hpadIn : ∀ x ∈ pad, E ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by
|
||
intro x hx
|
||
rw [hpadDbkeys, List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact ⟨by rw [hE]; exact hkE, by rw [hH]; exact hkH⟩
|
||
· exact hpadkIn' x hx
|
||
have hpadlen : pad.length = D := by
|
||
rw [hpadDbkeys, List.orderedInsert_length, hpadklen']
|
||
omega
|
||
have hkpad : k ∈ pad := by
|
||
rw [hpadDbkeys, List.mem_orderedInsert]
|
||
exact Or.inl rfl
|
||
-- a tail key above the reported last and the begin key
|
||
set L : Key := res.keys.getLastD ([] : Key) with hL
|
||
have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne
|
||
have hLle : ∀ x ∈ res.keys, x ≤ L := by
|
||
intro x hx
|
||
rw [hL]
|
||
exact le_getLastD_of_pairwise (List.sortedLT_iff_pairwise.mp hsort) hx
|
||
obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = ((b : Key) : WithTop Key) := by
|
||
cases hbk : r.begin.key with
|
||
| top => rw [hbk] at hbeginTop; exact absurd hbeginTop (lt_irrefl _)
|
||
| coe b => exact ⟨b, rfl⟩
|
||
set m : Key := if L ≤ b then b else L with hm
|
||
have hLm : L ≤ m := by
|
||
rw [hm]
|
||
by_cases h : L ≤ b
|
||
· rw [ite_eq_left h]; exact h
|
||
· rw [ite_eq_right h]
|
||
have hbm : b ≤ m := by
|
||
rw [hm]
|
||
by_cases h : L ≤ b
|
||
· rw [ite_eq_left h]
|
||
· rw [ite_eq_right h]; exact le_of_lt (lt_of_not_ge h)
|
||
set y : Key := keyAfter m with hy
|
||
have hLy : L < y := lt_of_le_of_lt hLm (by rw [hy]; exact keyAfter_self_lt m)
|
||
have hby : b < y := lt_of_le_of_lt hbm (by rw [hy]; exact keyAfter_self_lt m)
|
||
have hys : ∀ x ∈ res.keys, x < y := fun x hx => lt_of_le_of_lt (hLle x hx) hLy
|
||
have hyB : r.begin.key < ((y : Key) : WithTop Key) := by
|
||
rw [hb]; exact WithTop.coe_lt_coe.mpr hby
|
||
have hk_ne_y : k ≠ y := by
|
||
intro hky
|
||
subst hky
|
||
have h1 : y < H := WithTop.coe_lt_coe.mp hkH
|
||
exact absurd (lt_trans (lt_of_lt_of_le h1 hHleL) hLy) (lt_irrefl y)
|
||
-- the compatible witness with the full pad and the tail
|
||
obtain ⟨hs0, hcomp0⟩ :=
|
||
high_nonclamp_low_tail_compatible (pad := pad) (y := y)
|
||
hread0 hrev hmore hne hbegin hEH hcrossD hnonclamp hlow hpadIn hps hpadlen hys hyB
|
||
set db0 : DB := ⟨pad ++ res.keys ++ [y], hs0⟩ with hdb0
|
||
have hk0 : k ∈ db0.keys := by
|
||
rw [hdb0]
|
||
exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl hkpad)))
|
||
refine necessary_of_toggle ⟨db0, hcomp0, ?_⟩
|
||
rw [toggle_eq_erase_of_mem hk0]
|
||
-- the erased database's keys
|
||
have hks0 : db0.keys = pad ++ res.keys ++ [y] := rfl
|
||
have hkrest : k ∉ res.keys ++ [y] := by
|
||
rw [List.mem_append, List.mem_singleton]
|
||
push Not
|
||
exact ⟨hknot, hk_ne_y⟩
|
||
have hpadErase : pad.erase k = padk := by
|
||
have h := DB.erase_insert_keys (⟨padk, hpadks⟩ : DB) k hkpadk
|
||
rw [← hpadDb, DB.erase_keys, ← hpaddef] at h
|
||
simpa using h
|
||
have herase0 : (pad ++ res.keys ++ [y]).erase k = padk ++ res.keys ++ [y] := by
|
||
rw [List.erase_append_left (l₁ := pad ++ res.keys) [y]
|
||
(List.mem_append.mpr (Or.inl hkpad))]
|
||
rw [List.erase_append_left (l₁ := pad) res.keys hkpad]
|
||
rw [hpadErase]
|
||
have hkeysErase : (db0.erase k).keys = padk ++ res.keys ++ [y] := by
|
||
rw [DB.erase_keys, hks0]
|
||
exact herase0
|
||
set dbe : DB := db0.erase k with hdbe
|
||
have hHmemE : H ∈ dbe.keys := by
|
||
rw [hkeysErase]
|
||
exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hHmem)))
|
||
have hcrossH : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ hread0 hrev hmore hne hbegin hcrossD hnonclamp
|
||
have hfilterHB : (dbe.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
rw [hkeysErase, List.filter_append, List.filter_append]
|
||
have hpadf : padk.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.1 (not_le.mpr (hpadkIn' x hx).2)
|
||
have hresf : res.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) := by
|
||
apply List.filter_congr
|
||
intro x hx
|
||
have hHx : ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx)
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨hHx, h⟩⟩
|
||
have hyf : [y].filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [List.mem_singleton] at hx
|
||
subst hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.2 (not_lt.mpr (le_of_lt hyB))
|
||
rw [hpadf, hresf, hyf]
|
||
simp only [List.nil_append, List.append_nil]
|
||
rw [reportedBelowBegin, List.countP_eq_length_filter] at hr
|
||
exact hr
|
||
have hRbegE : resolve dbe r.begin = ((H : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmemE hcrossH hfilterHB
|
||
have hgeE : ∀ x ∈ dbe.keys, E ≤ ((x : Key) : WithTop Key) := by
|
||
intro x hx
|
||
rw [hkeysErase, List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· rcases List.mem_append.mp hx with hx | hx
|
||
· exact (hpadkIn' x hx).1
|
||
· exact le_of_lt (high_reported_gt_end hsort hne hEH x hx)
|
||
· rw [List.mem_singleton] at hx
|
||
subst hx
|
||
exact le_of_lt (lt_trans hEH (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hHleL hLy)))
|
||
have hcntEE : dbe.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
rw [decide_eq_true_eq, ← hE]
|
||
exact not_lt.mpr (hgeE x hx)
|
||
have hepos : 1 ≤ r.end_.offset := by omega
|
||
have hanchorE : (r.end_.anchor dbe).toNat = e := by
|
||
rw [anchor_toNat_eq (s := r.end_) hepos, hcntEE, Nat.zero_add, he]
|
||
have hlenE : dbe.keys.length = e := by
|
||
rw [hkeysErase, List.length_append, List.length_append, hpadklen']
|
||
simp only [List.length_singleton]
|
||
omega
|
||
have htopE : resolve dbe r.end_ = (⊤ : WithTop Key) := by
|
||
apply resolve_eq_top
|
||
· unfold Selector.anchor
|
||
rw [hcntEE]
|
||
have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
simpa using this
|
||
· rw [hanchorE, hlenE]
|
||
have hbetweenE : between dbe ((H : Key) : WithTop Key) (⊤ : WithTop Key) =
|
||
res.keys ++ [y] := by
|
||
rw [between_top_eq_above]
|
||
unfold above
|
||
rw [hkeysErase, List.filter_append, List.filter_append]
|
||
have hpadf : padk.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon (not_le.mpr (hpadkIn' x hx).2)
|
||
have hresf : res.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key))) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [hH, hheadD]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx)
|
||
have hyf : [y].filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key))) = [y] := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [List.mem_singleton] at hx
|
||
subst hx
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_le_coe.mpr (le_trans hHleL (le_of_lt hLy))
|
||
rw [hpadf, hresf, hyf]
|
||
simp
|
||
have hckE : RangeRead.completeKeys dbe r = res.keys ++ [y] := by
|
||
rw [completeKeys_eq_inRange_of_not_rev hrev, hRbegE, htopE]
|
||
simpa [RangeRead.inRange, between] using hbetweenE
|
||
intro hcompE
|
||
have hcontr : res.keys = res.keys ++ [y] := by
|
||
rcases hcompE.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· rw [hckE] at heq
|
||
exact heq
|
||
have hlen := congrArg List.length hcontr
|
||
rw [List.length_append, List.length_singleton] at hlen
|
||
omega
|
||
|
||
/-- **The `begin.key = ⊤`, full-gap nonclamp low branch: compatibility is exactly
|
||
agreement of the keys at or above the reported head with the result.** Here the begin
|
||
selector, whose key is `⊤`, lands exactly on the reported head, so the reported keys are
|
||
the top `t` stored keys; the end selector always overshoots because the gap can hold at
|
||
most `D` keys. -/
|
||
theorem high_nonclamp_low_aboveH_iff_top {db : DB} {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hbeginTop : r.begin.key = (⊤ : WithTop Key))
|
||
(hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res) :
|
||
RangeRead.Compatible db r res ↔
|
||
above db ((res.keys.headD ([] : Key) : Key) : WithTop Key) = res.keys := by
|
||
set H : Key := res.keys.headD ([] : Key) with hH
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set t : ℕ := res.keys.length with ht
|
||
set e : ℕ := (r.end_.offset - 1).toNat with he
|
||
set D : ℕ := e - t with hD
|
||
have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD]
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne
|
||
have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH
|
||
have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEH'
|
||
have hcrossH : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD
|
||
have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne
|
||
have hrb : reportedBelowBegin r res = t := by
|
||
unfold reportedBelowBegin; rw [hbeginTop, ht]; exact countP_lt_top
|
||
have hr : reportedBelowBegin r res = beginSlack r + 1 :=
|
||
high_reportedBelowBegin_eq_slack_succ hread hrev hmore hne hbegin hcrossH hnonclamp
|
||
have hq : beginSlack r + 1 = t := by omega
|
||
have hcapD : ((D : ℕ) : ℕ∞) = intervalCard E ((H : Key) : WithTop Key) := by
|
||
have h := hcap
|
||
rw [hDdef] at h
|
||
simpa [negCrossHighGapCap, hE, hH] using h
|
||
have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow)
|
||
constructor
|
||
· intro hcomp
|
||
-- the begin resolution lands on the head, so the top `t` stored keys are reported
|
||
have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by
|
||
rw [hH, hheadD]
|
||
exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev
|
||
have htlen : t ≤ db.keys.length := by rw [ht]; exact hsub.length_le
|
||
have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
db.keys.length := by
|
||
rw [hbeginTop]; exact countP_lt_top
|
||
have hanchor_cast : r.begin.anchor db =
|
||
(db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) -
|
||
(beginSlack r : ℤ) - 1 := anchor_eq_count_sub_slack db r (le_of_lt hbegin)
|
||
have hcB_ge : beginSlack r + 1 ≤ db.keys.length := by omega
|
||
have hanchor_ge : 0 ≤ r.begin.anchor db := by
|
||
rw [hanchor_cast, hcB]
|
||
have : (beginSlack r : ℤ) + 1 ≤ (db.keys.length : ℤ) := by exact_mod_cast hcB_ge
|
||
omega
|
||
have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
|
||
intro htop; rw [hL] at htop
|
||
exact absurd htop (WithTop.coe_ne_top (a := H))
|
||
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
|
||
by_contra h
|
||
exact hnot_top (resolve_eq_top hanchor_ge (le_of_not_gt h))
|
||
have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor_ge hltlen
|
||
have hidx : db.keys[(r.begin.anchor db).toNat] = H := by
|
||
have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) =
|
||
((H : Key) : WithTop Key) := by rw [← hget, hL]
|
||
exact WithTop.coe_injective hcoe
|
||
have hrank : db.keys.countP (fun k : Key => decide (k < H)) = (r.begin.anchor db).toNat :=
|
||
countP_lt_eq_of_getElem db.sorted hltlen hidx
|
||
have htoNat : (r.begin.anchor db).toNat = db.keys.length - t := by
|
||
have hz : r.begin.anchor db = ((db.keys.length : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by
|
||
rw [hanchor_cast, hcB]; push_cast; ring
|
||
have hz2 : ((db.keys.length : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) =
|
||
((db.keys.length - (beginSlack r + 1) : ℕ) : ℤ) := by
|
||
rw [← Nat.cast_sub hcB_ge]
|
||
rw [hz, hz2, Int.toNat_natCast, hq]
|
||
have hcnt : cnt db ((H : Key) : WithTop Key) = db.keys.length - t := by
|
||
unfold cnt
|
||
rw [show (fun k : Key => decide ((↑k : WithTop Key) < ((H : Key) : WithTop Key))) =
|
||
(fun k : Key => decide (k < H)) from by
|
||
funext k; rw [decide_eq_decide]; exact WithTop.coe_lt_coe]
|
||
rw [htoNat] at hrank
|
||
exact hrank
|
||
have haboveLen : (above db ((H : Key) : WithTop Key)).length = t := by
|
||
have h := cnt_add_above_length db ((H : Key) : WithTop Key)
|
||
rw [hcnt] at h
|
||
omega
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL]; rfl
|
||
have hfilterPrefix : res.keys = (above db ((H : Key) : WithTop Key)).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by
|
||
rw [hwin, between_eq_filter_above]
|
||
have hfilterLen : ((above db ((H : Key) : WithTop Key)).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_))).length = t := by
|
||
rw [← hfilterPrefix, ht]
|
||
have hfilterAll : (above db ((H : Key) : WithTop Key)).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) =
|
||
above db ((H : Key) : WithTop Key) :=
|
||
List.Sublist.eq_of_length List.filter_sublist (by rw [hfilterLen, haboveLen])
|
||
exact hfilterAll.symm.trans hfilterPrefix.symm
|
||
· intro habove
|
||
have hsort : res.keys.SortedLT := by
|
||
obtain ⟨db0, hcomp0⟩ := hread
|
||
exact RangeRead.compatible_sortedLT hcomp0 hrev
|
||
have hwl : RangeRead.withinLimit r res.keys := by
|
||
obtain ⟨db0, hcomp0⟩ := hread
|
||
exact RangeRead.compatible_withinLimit hcomp0
|
||
-- the begin selector resolves onto the head
|
||
have hHabove : H ∈ above db ((H : Key) : WithTop Key) := by rw [habove]; exact hHmem
|
||
have hHdb : H ∈ db.keys := (mem_above.mp hHabove).1
|
||
have hfilterHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
have h1 : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
above db ((H : Key) : WithTop Key) := by
|
||
rw [above_eq_filter, hbeginTop]
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact and_iff_left (WithTop.coe_lt_top k)
|
||
rw [h1, habove, ← ht, hq]
|
||
have hL : resolve db r.begin = ((H : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len (head := H) hbegin hHdb hcrossH hfilterHB
|
||
-- the end selector always overshoots
|
||
have hbetweenLen : (between db E ((H : Key) : WithTop Key)).length ≤ D := by
|
||
have hcard : (((between db E ((H : Key) : WithTop Key)).length : ℕ) : ℕ∞) ≤
|
||
intervalCard E ((H : Key) : WithTop Key) := between_length_le_intervalCard hEB
|
||
have : (((between db E ((H : Key) : WithTop Key)).length : ℕ) : ℕ∞) ≤ (D : ℕ∞) := by
|
||
rw [hcapD]; exact hcard
|
||
exact_mod_cast this
|
||
have hlenEnd : db.keys.length ≤ cnt db E + e := by
|
||
have h1 : cnt db ((H : Key) : WithTop Key) = cnt db E +
|
||
(between db E ((H : Key) : WithTop Key)).length := cnt_eq_add_between db hEB
|
||
have h2 : cnt db ((H : Key) : WithTop Key) +
|
||
(above db ((H : Key) : WithTop Key)).length = db.keys.length :=
|
||
cnt_add_above_length db ((H : Key) : WithTop Key)
|
||
have h3 : (above db ((H : Key) : WithTop Key)).length = t := by rw [habove, ht]
|
||
rw [h3] at h2
|
||
omega
|
||
have htop : resolve db r.end_ = (⊤ : WithTop Key) := by
|
||
apply resolve_eq_top
|
||
· unfold Selector.anchor
|
||
have h1 : (0 : ℤ) ≤ r.end_.offset - 1 := by omega
|
||
have h2 : (0 : ℤ) ≤ (db.keys.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ) := by
|
||
exact_mod_cast Nat.zero_le _
|
||
linarith
|
||
· rw [anchor_toNat_eq (s := r.end_) (by omega), ← he]
|
||
exact hlenEnd
|
||
refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩
|
||
rw [RangeRead.completeKeys, hrev]
|
||
simp only [Bool.false_eq_true, ite_false]
|
||
rw [hL, htop]
|
||
change res.keys = between db ((H : Key) : WithTop Key) (⊤ : WithTop Key)
|
||
rw [between_top_eq_above]
|
||
exact habove.symm
|
||
|
||
/-- Every key at or above `end.key` is necessary in the nonclamping low branch of the
|
||
crossed negative-begin complete high cell when `end.key` is the chosen lower endpoint. -/
|
||
theorem necessary_topRange_endKey_nonclamp_low {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (_hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(huses : negCrossHighUsesEnd r res)
|
||
(hkE : r.end_.key ≤ ((k : Key) : WithTop Key)) :
|
||
Necessary r res k := by
|
||
by_cases hkres : k ∈ res.keys
|
||
· exact necessary_of_mem_result hread hkres
|
||
· by_cases hkH : ((k : Key) : WithTop Key) <
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key)
|
||
· by_cases hlt : ((negCrossHighD r res : ℕ) : ℕ∞) < negCrossHighGapCap r res
|
||
· exact necessary_gap_nonclamp_low_lt_cap hread hrev hmore hne hbegin hEH hcrossD
|
||
hnonclamp hlow hkE hkH hkres hlt
|
||
· have hsecond : (((negCrossHighD r res : ℕ) : ℕ∞) ≤ negCrossHighGapCap r res) ∧
|
||
r.begin.key < (⊤ : WithTop Key) := by
|
||
rcases huses with h | h
|
||
· exact absurd h hlt
|
||
· exact h
|
||
have hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res :=
|
||
le_antisymm hsecond.1 (le_of_not_gt hlt)
|
||
exact necessary_gap_nonclamp_low_eq_cap hread hrev hmore hne hbegin hEH hcrossD
|
||
hnonclamp hlow hkE hkH hkres hsecond.2 hcap
|
||
· have hkHle : ((res.keys.headD ([] : Key) : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) := le_of_not_gt hkH
|
||
exact necessary_above_head_nonclamp_low hread hrev hmore hne hbegin hEH hcrossD
|
||
hnonclamp hlow hkHle hkres
|
||
|
||
/-- **The `[end.key, ⊤)` branch of the nonclamping low crossed negative-begin complete
|
||
high cell is minimal** when the gap can absorb the excess and the begin key admits a key
|
||
above it. -/
|
||
theorem minimal_nonclampLow_usesEnd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(huses : negCrossHighUsesEnd r res) :
|
||
Minimal r res ⟨r.end_.key, (⊤ : WithTop Key)⟩ := by
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
|
||
· exact sound_topRange_endKey_nonclamp_low hread hrev hmore hne hbegin hEH hcrossD
|
||
hnonclamp hend
|
||
· intro k hk
|
||
exact necessary_topRange_endKey_nonclamp_low hread hrev hmore hne hbegin hend hEH
|
||
hcrossD hnonclamp hlow huses (ConflictRange.mem_iff.mp hk).1
|
||
|
||
/-- **The `[head, ⊤)` branch of the nonclamping low crossed negative-begin complete high
|
||
cell is minimal** when the gap is too small or the begin key is `⊤`. -/
|
||
theorem minimal_nonclampLow_notUsesEnd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset)
|
||
(hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key))
|
||
(hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hlow : res.keys.length < (r.end_.offset - 1).toNat)
|
||
(hnot : ¬ negCrossHighUsesEnd r res) :
|
||
Minimal r res ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by
|
||
by_cases hle : ((negCrossHighD r res : ℕ) : ℕ∞) ≤ negCrossHighGapCap r res
|
||
· have htopB : ¬ (r.begin.key < (⊤ : WithTop Key)) := by
|
||
intro hB
|
||
exact hnot (Or.inr ⟨hle, hB⟩)
|
||
have hBtop : r.begin.key = (⊤ : WithTop Key) := le_antisymm le_top (le_of_not_gt htopB)
|
||
have hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res := by
|
||
rcases lt_or_ge ((negCrossHighD r res : ℕ) : ℕ∞) (negCrossHighGapCap r res) with hlt | hge
|
||
· exact absurd (Or.inl hlt) hnot
|
||
· exact le_antisymm hle hge
|
||
refine minimal_of_compatible_iff_filter hread (fun db => ?_)
|
||
rw [high_nonclamp_low_aboveH_iff_top hread hrev hmore hne hbegin hend hEH hcrossD
|
||
hnonclamp hlow hBtop hcap, filter_le_top_eq_above_aux]
|
||
· exact minimal_nonclampLow_noCap hread hrev hmore hne hbegin hend hEH hcrossD
|
||
hnonclamp hlow hle
|
||
|
||
/-- **The nonclamping low-offset branch of the crossed negative-begin complete high cell
|
||
is minimal** for the total closed form. -/
|
||
theorem minimal_closedForm_of_negCrossCompleteHigh_nonclampLow {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossCompleteHighNonclampLow r res) :
|
||
Minimal r res (closedFormNegCrossCompleteHigh r res) := by
|
||
obtain ⟨⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩, hnonclamp, hlow⟩ := hcell
|
||
unfold closedFormNegCrossCompleteHigh
|
||
rw [ite_eq_right (not_le.mpr hnonclamp)]
|
||
rw [ite_eq_right (ne_of_lt hlow)]
|
||
by_cases huses : negCrossHighUsesEnd r res
|
||
· rw [ite_eq_left huses]
|
||
exact minimal_nonclampLow_usesEnd hread hrev hmore hne hbegin hend2 hEH hcrossD
|
||
hnonclamp hlow huses
|
||
· rw [ite_eq_right huses]
|
||
exact minimal_nonclampLow_notUsesEnd hread hrev hmore hne hbegin hend2 hEH hcrossD
|
||
hnonclamp hlow huses
|
||
|
||
/-- A small closed-form region of the crossed negative-begin complete high cell: the
|
||
clamping low branch, the nonclamping finite branch and the nonclamping low branch. -/
|
||
abbrev negCrossCompleteHighSmall (r : RangeRead) (res : Result) : Prop :=
|
||
negCrossCompleteHighClampLow r res ∨ negCrossCompleteHighNonclampFinite r res ∨
|
||
negCrossCompleteHighNonclampLow r res
|
||
|
||
/-- **The small closed-form region of the crossed negative-begin complete high cell is
|
||
minimal** for the total closed form. -/
|
||
theorem minimal_closedForm_of_negCrossCompleteHigh_small {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossCompleteHighSmall r res) :
|
||
Minimal r res (closedFormNegCrossCompleteHigh r res) := by
|
||
rcases hcell with h | h | h
|
||
· exact minimal_closedForm_of_negCrossCompleteHigh_clampLow hread h
|
||
· exact minimal_closedForm_of_negCrossCompleteHigh_nonclampFinite hread h
|
||
· exact minimal_closedForm_of_negCrossCompleteHigh_nonclampLow hread h
|
||
|
||
end FormalConflictRange
|