Files
conflict-range/FormalConflictRange/NegCrossTruncRevResidualLargeSentinel.lean
T

1134 lines
55 KiB
Lean4
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/-
Released under Apache 2.0 license as described in the file LICENSE.
-/
import FormalConflictRange.NegCrossTruncRevResidualLargeTop
/-!
# The sentinel-begin large-gap top cell of the reversed truncated residual
This module closes the sentinel-begin companion of
`NegCrossTruncRevResidualLargeTop.lean`: the reversed crossed negative-begin
truncated residual at end offset at least two, with the end key strictly below
the reported last and the large-gap condition, but with `begin.key = ⊤`.
Writing `E := r.end_.key`, `L` for the reported last, `t := #reported`,
`e := (r.end_.offset - 1).toNat` and `card := intervalCard E L`, the minimal
range is
* `[L, ⊤)` when `e = card + t` **and** `t + r.begin.offset = 1`;
* `[E, ⊤)` otherwise.
In the first (tight) case compatibility forces `resolve end = ⊤` and
`resolve begin = L`, so the cell has the same compatible databases as the
sentinel-end read; in the second case the gap keys below `L` are necessary and
the range must start at `E`.
-/
namespace FormalConflictRange
/-- The first optional entry of a nonempty list is its head. -/
theorem getElem?_zero_eq_head {l : List Key} (hne : l ≠ []) :
l[0]? = some (l.head hne) := by
cases l with
| nil => exact absurd rfl hne
| cons a t => simp
/-! ### The cell and its closed form -/
/-- The sentinel-begin large-gap top subcell of the reversed truncated residual. -/
abbrev negCrossTruncRevResidualTopLargeSentinel (r : RangeRead) (res : Result) : Prop :=
negCrossTruncRevResidualTopLarge r res ∧ r.begin.key = (⊤ : WithTop Key)
/-- The tightness condition of the sentinel-begin large-gap top cell. -/
noncomputable def sentinelLargeTight (r : RangeRead) (res : Result) : Prop :=
let L : WithTop Key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)
((r.end_.offset - 1).toNat : ℕ∞) = intervalCard r.end_.key L + (res.keys.length : ℕ∞) ∧
(res.keys.length : Int) + r.begin.offset = 1
/-- The closed form of the sentinel-begin large-gap top cell. -/
noncomputable def closedFormNegCrossTruncRevResidualTopLargeSentinel
(r : RangeRead) (res : Result) : ConflictRange := by
classical
let L : WithTop Key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)
exact if sentinelLargeTight r res then ⟨L, ⊤⟩ else ⟨r.end_.key, ⊤⟩
/-! ### Converting the tight condition to natural numbers -/
/-- The tight `ℕ∞` identity, read in `ℕ`. -/
theorem tight_nat {r : RangeRead} {res : Result} {L : Key}
(htight : ((r.end_.offset - 1).toNat : ℕ∞) =
intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞)) :
(r.end_.offset - 1).toNat =
(intervalCard r.end_.key ((L : Key) : WithTop Key)).toNat + res.keys.length := by
have hcardtop : intervalCard r.end_.key ((L : Key) : WithTop Key) ≠ ⊤ := by
intro h
rw [h] at htight
have htop : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) = ⊤ := by
rw [htight]; simp
exact ENat.natCast_ne_top _ htop
have h := congrArg ENat.toNat htight
rw [ENat.toNat_natCast, ENat.toNat_add hcardtop (ENat.natCast_ne_top _),
ENat.toNat_natCast] at h
exact h
/-! ### The tight counting core -/
/-- **The end resolution is the sentinel in the tight compatible cell.**
Every compatible database in the tight cell resolves its end selector to `⊤`.
If the end resolution were a genuine key, the stored keys in `[L, resolve end)`
would be exactly the reported keys, so the stored keys in `[E, L)` would be the
whole interval `card = e - t`; the remaining stored keys above `resolve end`
would then shift the begin anchor, because `begin.key = ⊤`, past the reported
last, contradicting that the reported last lies in the complete keys. -/
theorem resolve_end_eq_top_of_tight_forward {db : DB} {r : RangeRead} {res : Result}
{L : Key}
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
(hrev : r.reverse = true) (hne : res.keys ≠ [])
(hL : res.keys.getLast hne = L) (hbtop : r.begin.key = (⊤ : WithTop Key))
(hend : 2 ≤ r.end_.offset)
(hEL : r.end_.key < ((L : Key) : WithTop Key))
(hoff : (res.keys.length : Int) + r.begin.offset = 1) :
resolve db r.end_ = (⊤ : WithTop Key) := by
classical
have htpos : 0 < res.keys.length := List.length_pos_iff.mpr hne
have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by
have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
simpa [hL] using h
have hLmem : L ∈ res.keys := hL ▸ List.getLast_mem hne
have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem
have hLck : L ∈ RangeRead.completeKeys db r := by
have h := hLmem
rw [← hfil] at h
exact (List.mem_filter.mp h).1
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp hLck).2.1
obtain ⟨j, hjlt, hjeq⟩ := List.mem_iff_getElem.mp hLdb
have hcntL : cnt db ((L : Key) : WithTop Key) = j := by
rw [cnt_coe]
exact countP_lt_eq_of_getElem db.sorted hjlt hjeq
by_contra hne_top
have hbetween_eq : between db ((L : Key) : WithTop Key) (resolve db r.end_) =
res.keys.reverse := by
have h := res_keys_eq_reverse_between_resolve_end (db := db) (r := r) hcomp hmore hrev hne
rw [hL] at h
simpa [List.reverse_reverse] using (congrArg List.reverse h).symm
have hlenL : (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length =
res.keys.length := by
rw [hbetween_eq, List.length_reverse]
have hlenE : (between db r.end_.key (resolve db r.end_)).length =
(r.end_.offset - 1).toNat :=
length_between_end_resolve_eq_of_ne_top (db := db) (r := r) hend hne_top
have hLre : ((L : Key) : WithTop Key) < resolve db r.end_ := by
have hpos : 0 < (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length := by
rw [hlenL]; exact htpos
obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ (List.length_pos_iff.mp hpos)
have hk' := List.mem_filter.mp hk
rcases (of_decide_eq_true hk'.2) with ⟨hLk, hkre⟩
exact lt_of_le_of_lt hLk hkre
have hEre : r.end_.key ≤ resolve db r.end_ :=
le_of_lt (lt_trans hEL hLre)
have hcnt1 : cnt db (resolve db r.end_) =
cnt db ((L : Key) : WithTop Key) +
(between db ((L : Key) : WithTop Key) (resolve db r.end_)).length :=
cnt_eq_add_between db (le_of_lt hLre)
have hcnt2 : cnt db ((L : Key) : WithTop Key) =
cnt db r.end_.key + (between db r.end_.key ((L : Key) : WithTop Key)).length :=
cnt_eq_add_between db (le_of_lt hEL)
have hcnt3 : cnt db (resolve db r.end_) =
cnt db r.end_.key + (between db r.end_.key (resolve db r.end_)).length :=
cnt_eq_add_between db hEre
have hcntTop1 : cnt db (⊤ : WithTop Key) =
cnt db (resolve db r.end_) +
(between db (resolve db r.end_) (⊤ : WithTop Key)).length :=
cnt_eq_add_between db le_top
have hanchor : r.begin.anchor db = (cnt db (⊤ : WithTop Key) : Int) + r.begin.offset - 1 := by
change (cnt db r.begin.key : Int) + r.begin.offset - 1 = _
rw [hbtop]
have hanchor_eq : r.begin.anchor db =
(cnt db ((L : Key) : WithTop Key) : Int) +
((between db (resolve db r.end_) (⊤ : WithTop Key)).length : Int) := by
rw [hanchor]
have hN : cnt db (⊤ : WithTop Key) =
cnt db ((L : Key) : WithTop Key) + res.keys.length +
(between db (resolve db r.end_) (⊤ : WithTop Key)).length := by
have := hcntTop1
rw [hcnt1, hlenL] at this
exact this
rw [hN]
have hoff' : r.begin.offset = 1 - (res.keys.length : Int) := by linarith
rw [hoff']
push_cast
ring
have hanchor_nonneg : 0 ≤ r.begin.anchor db := by
rw [hanchor_eq]
have h1 : (0 : Int) ≤ (cnt db ((L : Key) : WithTop Key) : Int) := by
exact_mod_cast Nat.zero_le _
have h2 : (0 : Int) ≤ ((between db (resolve db r.end_) (⊤ : WithTop Key)).length : Int) := by
exact_mod_cast Nat.zero_le _
linarith
have hanchor_le : r.begin.anchor db ≤ (cnt db ((L : Key) : WithTop Key) : Int) := by
have hj := (resolve_le_getElem_iff (db := db) (s := r.begin) hjlt).mp (by rw [hjeq]; exact hrb)
have htoNat : (r.begin.anchor db).toNat ≤ j := by
unfold clampIndex at hj
by_cases h : (r.begin.anchor db).toNat ≤ db.keys.length
· rwa [Nat.min_eq_left h] at hj
· rw [Nat.min_eq_right (by omega)] at hj; omega
have hanchor_le_j : r.begin.anchor db ≤ (j : Int) := by
have h1 : r.begin.anchor db ≤ ((r.begin.anchor db).toNat : Int) := by
rw [Int.toNat_of_nonneg hanchor_nonneg]
have h2 : ((r.begin.anchor db).toNat : Int) ≤ (j : Int) := by exact_mod_cast htoNat
linarith
rwa [hcntL]
have hzpos : 0 < (between db (resolve db r.end_) (⊤ : WithTop Key)).length := by
rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hnil | htop | ⟨q, hq, hqeq⟩
· rw [hnil]
have hbw : between db (([] : Key) : WithTop Key) (⊤ : WithTop Key) = db.keys := by
unfold between
rw [List.filter_eq_self]
intro k _
rw [decide_eq_true_eq]
exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), WithTop.coe_lt_top k⟩
rw [hbw]
obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil _ hne
have hxdb : x ∈ db.keys := RangeRead.compatible_keys_subset hcomp (by simpa using hx)
rw [List.length_pos_iff]
exact fun hnil' => by rw [hnil'] at hxdb; exact absurd hxdb (by simp)
· exact absurd htop hne_top
· rw [hqeq]
have hmem : q ∈ between db ((q : Key) : WithTop Key) (⊤ : WithTop Key) := by
unfold between
rw [List.mem_filter]
exact ⟨hq, decide_eq_true ⟨le_refl _, WithTop.coe_lt_top q⟩⟩
rw [List.length_pos_iff]
exact fun hnil' => by rw [hnil'] at hmem; exact absurd hmem (by simp)
rw [hanchor_eq] at hanchor_le
have hz0 : (between db (resolve db r.end_) (⊤ : WithTop Key)).length = 0 := by
have : ((between db (resolve db r.end_) (⊤ : WithTop Key)).length : Int) ≤ 0 := by linarith
omega
omega
/-! ### The end resolution from agreement above the reported last -/
/-- **The end resolution is the sentinel in the tight cell, from keys at or above the
reported last.** If every stored key at or above the reported last is one of the
reported keys then, under tightness, the end selector overshoots: the whole gap
`[end_.key, last)` holds at most `card` keys, so the keys at or above `end_.key` number
at most `card + #reported = end_.offset - 1`, exactly the capacity of the end anchor. -/
theorem resolve_end_eq_top_of_above {db : DB} {r : RangeRead} {res : Result} {L : Key}
(habove : above db ((L : Key) : WithTop Key) = res.keys.reverse)
(_hend : 2 ≤ r.end_.offset)
(hEL : r.end_.key < ((L : Key) : WithTop Key))
(htight : ((r.end_.offset - 1).toNat : ℕ∞) =
intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞)) :
resolve db r.end_ = (⊤ : WithTop Key) := by
have hnat := tight_nat htight
have hcardtop : intervalCard r.end_.key ((L : Key) : WithTop Key) ≠ ⊤ := by
intro h
rw [h] at htight
have htop : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) = ⊤ := by
rw [htight]; simp
exact ENat.natCast_ne_top _ htop
-- `between L ⊤` is `above L`
have hbetweenTop : between db ((L : Key) : WithTop Key) (⊤ : WithTop Key) =
above db ((L : Key) : WithTop Key) := by
unfold between above
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact and_iff_left (WithTop.coe_lt_top k)
-- the total key count
have hcnt : cnt db (⊤ : WithTop Key) =
cnt db r.end_.key + (between db r.end_.key ((L : Key) : WithTop Key)).length +
res.keys.length := by
have h1 := cnt_eq_add_between db (le_of_lt hEL :
r.end_.key ≤ ((L : Key) : WithTop Key))
have h2 := cnt_eq_add_between (db := db)
(w₁ := ((L : Key) : WithTop Key)) (w₂ := (⊤ : WithTop Key)) le_top
rw [hbetweenTop, habove, List.length_reverse] at h2
rw [h1] at h2
omega
-- the gap holds at most `card` keys
have hgaple : (between db r.end_.key ((L : Key) : WithTop Key)).length ≤
(intervalCard r.end_.key ((L : Key) : WithTop Key)).toNat := by
have h := between_length_le_intervalCard (db := db) (E := r.end_.key)
(B := ((L : Key) : WithTop Key)) (le_of_lt hEL)
rw [← ENat.natCast_toNat hcardtop] at h
exact ENat.natCast_le_natCast.mp h
-- the anchor of the end selector
have hoff : ((r.end_.offset - 1).toNat : Int) = r.end_.offset - 1 :=
Int.toNat_of_nonneg (by omega)
have hanchor : r.end_.anchor db =
((cnt db r.end_.key + (r.end_.offset - 1).toNat : ℕ) : Int) := by
unfold Selector.anchor cnt
rw [Nat.cast_add, hoff]
ring
have hanchor_nonneg : 0 ≤ r.end_.anchor db := by
rw [hanchor]; exact_mod_cast Nat.zero_le _
have hanchor_toNat : (r.end_.anchor db).toNat =
cnt db r.end_.key + (r.end_.offset - 1).toNat := by
rw [hanchor, Int.toNat_natCast]
refine resolve_eq_top hanchor_nonneg ?_
rw [hanchor_toNat, ← cnt_top db, hcnt]
omega
/-! ### Recovering agreement above the reported last -/
/-- **A compatible database sees exactly the reported keys above the last.** When the
end selector resolves to the sentinel, the complete keys above the reported last are all
stored keys above it, and compatibility identifies them with the reported keys. -/
theorem above_eq_reverse_of_compat_end_top {db : DB} {r : RangeRead} {res : Result} {L : Key}
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) (hrev : r.reverse = true)
(hne : res.keys ≠ []) (hL : res.keys.getLast hne = L)
(hre : resolve db r.end_ = (⊤ : WithTop Key)) :
above db ((L : Key) : WithTop Key) = res.keys.reverse := by
have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by
have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
simpa [hL] using h
have hLmem : L ∈ res.keys := hL ▸ List.getLast_mem hne
have hLck : L ∈ RangeRead.completeKeys db r := by
have h := hLmem
rw [← hfil] at h
exact (List.mem_filter.mp h).1
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp hLck).2.1
have h := completeKeys_filter_ge_eq_of_resolve_le (db := db) (r := r) (last := L)
hrev hre hrb
rw [hfil] at h
have hfilUp : db.keys.filter (fun k : Key => decide (L ≤ k)) =
above db ((L : Key) : WithTop Key) := by
rw [above_eq_filter]
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact WithTop.coe_le_coe.symm
rw [hfilUp] at h
have h2 : res.keys.reverse = above db ((L : Key) : WithTop Key) := by
rw [h, List.reverse_reverse]
exact h2.symm
/-! ### Reducing the tight cell to the sentinel-end read -/
/-- **In the tight cell the end selector is equivalent to the sentinel.** Replacing the
end selector by `⟨⊤, 1⟩` does not change the compatible databases: forward, `t + off = 1`
forces the end resolution to the sentinel; backward, agreement with the reported keys
above the last plus tightness does the same. -/
theorem compatible_iff_sentinelEnd_of_tight {r : RangeRead} {res : Result} {L : Key}
(hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ [])
(hL : res.keys.getLast hne = L) (hbtop : r.begin.key = (⊤ : WithTop Key))
(hend : 2 ≤ r.end_.offset)
(hEL : r.end_.key < ((L : Key) : WithTop Key))
(hoff : (res.keys.length : Int) + r.begin.offset = 1)
(htight : ((r.end_.offset - 1).toNat : ℕ∞) =
intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞)) :
∀ db, RangeRead.Compatible db r res ↔
RangeRead.Compatible db { r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } res := by
intro db
have htopEnd : resolve db ({ r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } : RangeRead).end_ =
(⊤ : WithTop Key) := resolve_end_eq_top_of_key_top rfl (by norm_num)
have hck : resolve db r.end_ = (⊤ : WithTop Key) →
RangeRead.completeKeys db r =
RangeRead.completeKeys db { r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } := by
intro hre
unfold RangeRead.completeKeys RangeRead.inRange
rw [hre, htopEnd]
constructor
· intro hc
have hre := resolve_end_eq_top_of_tight_forward hc hmore hrev hne hL hbtop hend hEL hoff
have hckeq := hck hre
unfold RangeRead.Compatible at hc ⊢
rw [hckeq] at hc
exact hc
· intro hc
have habove := above_eq_reverse_of_compat_end_top hc hmore hrev hne hL htopEnd
have hre := resolve_end_eq_top_of_above (db := db) habove hend hEL htight
have hckeq := hck hre
unfold RangeRead.Compatible at hc ⊢
rw [← hckeq] at hc
exact hc
/-- **The tight sentinel-begin large-gap top cell is minimal with range `[last, ⊤)`.** -/
theorem minimal_topOfLast_of_sentinel_large_tight {r : RangeRead} {res : Result}
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
(hbeg : r.begin.offset < 0) (hne : res.keys ≠ [])
(hbtop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset)
(hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key))
(hoff : (res.keys.length : Int) + r.begin.offset = 1)
(htight : ((r.end_.offset - 1).toNat : ℕ∞) =
intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) +
(res.keys.length : ℕ∞)) :
Minimal r res (topOfLast (res.keys.getLast hne)) := by
set L : Key := res.keys.getLast hne
set r' : RangeRead := { r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } with hr'
have hiff := compatible_iff_sentinelEnd_of_tight (r := r) (res := res) (L := L)
hmore hrev hne rfl hbtop hend hEL hoff htight
have hread' : Readable r' res := by
obtain ⟨db, hc⟩ := hread
exact ⟨db, (hiff db).mp hc⟩
have hbeg' : r'.begin.offset < 0 := hbeg
have hmin' : Minimal r' res (topOfLast (res.keys.getLast hne)) :=
minimal_topOfLast_of_rev_negTrunc_top_nocross hread' hmore hrev hne hbeg' rfl (by norm_num)
exact (minimal_congr_of_compatible_iff (r := r) (r' := r') hiff).mpr hmin'
/-! ### The offset bound in the sentinel-begin cell -/
/-- **The reported length and begin offset cannot both be large.** In a compatible
sentinel-begin truncated result the begin anchor `N + offset - 1` lands at or below the
reported last, and only `N - index(last)` keys are at or above that last, so
`#reported + offset ≤ 1`. -/
theorem sentinel_begin_offset_bound {r : RangeRead} {res : Result} {db : DB}
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
(hrev : r.reverse = true) (hne : res.keys ≠ [])
(hbtop : r.begin.key = (⊤ : WithTop Key)) :
(res.keys.length : Int) + r.begin.offset ≤ 1 := by
classical
set L : Key := res.keys.getLast hne with hL
set t : Nat := res.keys.length with ht
set N : Nat := db.keys.length with hN
have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by
have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
simpa [hL] using h
have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne
have hLck : L ∈ RangeRead.completeKeys db r := by
have h := hLmem
rw [← hfil] at h
exact (List.mem_filter.mp h).1
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp hLck).2.1
have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem
obtain ⟨j, hjlt, hjeq⟩ := List.mem_iff_getElem.mp hLdb
have hcntL : cnt db ((L : Key) : WithTop Key) = j := by
rw [cnt_coe]
exact countP_lt_eq_of_getElem db.sorted hjlt hjeq
-- the reported keys fit inside the database
have htN : t ≤ N := by
have hpre := RangeRead.compatible_keys_prefix hcomp
refine le_trans hpre.length_le ?_
rw [(RangeRead.completeKeys_perm_inRange db r).length_eq]
exact List.length_filter_le _ _
-- the number of stored keys at or above the last
have haboveLen : (above db ((L : Key) : WithTop Key)).length = N - j := by
have hbT : between db ((L : Key) : WithTop Key) (⊤ : WithTop Key) =
above db ((L : Key) : WithTop Key) := by
unfold between above
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact and_iff_left (WithTop.coe_lt_top k)
have h := cnt_eq_add_between (db := db)
(w₁ := ((L : Key) : WithTop Key)) (w₂ := (⊤ : WithTop Key)) le_top
rw [hbT, cnt_top, ← hN, hcntL] at h
omega
-- the reported keys are a subset of the keys at or above the last
have hsubAbove : res.keys ⊆ above db ((L : Key) : WithTop Key) := by
intro k hk
rw [mem_above]
refine ⟨RangeRead.compatible_keys_subset hcomp hk, ?_⟩
have hsort := RangeRead.compatible_sortedGT hcomp hrev
have hLk : L ≤ k := (hsort.pairwise.imp le_of_lt).rel_getLast hk
exact WithTop.coe_le_coe.mpr hLk
have htNj : t ≤ N - j := by
have hnd : res.keys.Nodup := by
rw [← hfil]
exact (RangeRead.completeKeys_nodup db r).filter _
have := (List.subperm_of_subset (l₁ := res.keys)
(l₂ := above db ((L : Key) : WithTop Key)) hnd hsubAbove).length_le
rw [haboveLen] at this
exact this
have hanchor : r.begin.anchor db = (N : Int) + r.begin.offset - 1 := by
change (cnt db r.begin.key : Int) + r.begin.offset - 1 = _
rw [hbtop, cnt_top]
by_cases hneg : r.begin.anchor db < 0
· -- the anchor is negative: `offset < 1 - N`
have : (t : Int) + r.begin.offset ≤ 1 := by
have htN' : (t : Int) ≤ (N : Int) := by exact_mod_cast htN
have hoff : (r.begin.offset : Int) < 1 - N := by
rw [hanchor] at hneg; linarith
linarith
exact this
· -- the anchor is nonnegative: it lands at or below `last`
have hnonneg : 0 ≤ r.begin.anchor db := le_of_not_gt hneg
have hclamp : clampIndex db r.begin ≤ j :=
(resolve_le_getElem_iff (db := db) (s := r.begin) hjlt).mp (by rw [hjeq]; exact hrb)
have htoNat_le : (r.begin.anchor db).toNat ≤ j := by
unfold clampIndex at hclamp
by_cases h : (r.begin.anchor db).toNat ≤ N
· rwa [Nat.min_eq_left h] at hclamp
· rw [Nat.min_eq_right (by omega)] at hclamp; omega
have hanchor_le_j : r.begin.anchor db ≤ (j : Int) := by
have h1 : r.begin.anchor db = ((r.begin.anchor db).toNat : Int) :=
(Int.toNat_of_nonneg hnonneg).symm
rw [h1]
exact_mod_cast htoNat_le
have hoff : (r.begin.offset : Int) ≤ (j : Int) - N + 1 := by
rw [hanchor] at hanchor_le_j; linarith
have htNj' : (t : Int) ≤ (N : Int) - (j : Int) := by
have h1 : (t : Int) ≤ ((N - j : Nat) : Int) := by exact_mod_cast htNj
have h2 : ((N - j : Nat) : Int) = (N : Int) - (j : Int) := by
rw [Nat.cast_sub (le_of_lt hjlt)]
rwa [h2] at h1
linarith
/-! ### Gap-key necessity in regime A -/
/-- **Gap-key necessity in the sentinel-begin large-gap top cell, regime A.**
When `#reported + begin.offset = 1` and the gap is not tight, the witness stores `D = e - t`
gap keys in `[end_.key, last)` avoiding the target, then the reversed reported keys. Its
begin anchor is exactly `D`, so it resolves onto the reported last, and its end resolves to
the sentinel. Inserting the target raises the end resolution onto the reported head,
which leaves the complete keys and breaks the reported prefix. -/
theorem necessary_gap_of_sentinel_large_A {r : RangeRead} {res : Result}
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
(hbeg : r.begin.offset < 0) (hne : res.keys ≠ [])
(hbtop : r.begin.key = (⊤ : WithTop Key))
(hend : 2 ≤ r.end_.offset)
(hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key))
(ht : res.keys.length < (r.end_.offset - 1).toNat)
(hstrict : ((r.end_.offset - 1).toNat : ℕ∞) <
intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) +
(res.keys.length : ℕ∞))
(hoff : (res.keys.length : Int) + r.begin.offset = 1)
{k : Key} (hEk : r.end_.key ≤ (k : WithTop Key))
(hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) :
Necessary r res k := by
classical
obtain ⟨db₀, hcomp₀⟩ := hread
set E : WithTop Key := r.end_.key with hE
set L : Key := res.keys.getLast hne with hL
set H : Key := res.keys.head hne with hH
set e : Nat := (r.end_.offset - 1).toNat with he
set t : Nat := res.keys.length with ht'
set D : Nat := e - t with hD
have htn : 0 < t := List.length_pos_of_ne_nil hne
have ht_lt : t < e := by simpa [t, e] using ht
have hDt : D + t = e := by omega
have hD1 : 1 ≤ D := by omega
have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne
have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne
have hEL' : E < ((L : Key) : WithTop Key) := by simpa [E, L] using hEL
have hkL' : ((k : Key) : WithTop Key) < ((L : Key) : WithTop Key) := hkL
-- room for `D + 1` gap keys avoiding the target
have hD_lt_card : (D : ℕ∞) < intervalCard E ((L : Key) : WithTop Key) := by
have hstrict' : (e : ℕ∞) < intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := by
simpa [E, L, e, t] using hstrict
have hcast : ((D : ℕ∞) + (t : ℕ∞)) = (e : ℕ∞) := by
rw [← Nat.cast_add, hDt]
rw [← hcast] at hstrict'
exact (ENat.add_lt_add_iff_right (ENat.natCast_ne_top t)).mp hstrict'
have hroom : (((D + 1 : ℕ) : ℕ∞)) ≤ intervalCard E ((L : Key) : WithTop Key) := by
have := (ENat.add_one_le_iff (m := (D : ℕ∞))
(n := intervalCard E ((L : Key) : WithTop Key)) (ENat.natCast_ne_top D)).mpr hD_lt_card
simpa using this
obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ :=
exists_db_keysIn_avoid (B := E) (E := ((L : Key) : WithTop Key)) (k := k) (m := D) hroom
set G : List Key := pad.keys with hGdef
have hGmem : ∀ x ∈ G, E ≤ (x : WithTop Key) ∧
(x : WithTop Key) < ((L : Key) : WithTop Key) := by
intro x hx
simpa only [G, keysIn, Set.mem_ofPred_eq] using hpadIn x (by simpa [hGdef] using hx)
have hGsort : G.SortedLT := by rw [hGdef]; exact pad.sorted
have hGlen : G.length = D := by rw [hGdef]; exact hpadlen
have hkG : k ∉ G := by rw [hGdef]; exact hkpad
-- the reported keys, ascending
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev
set R : List Key := res.keys.reverse with hR
have hRsort : R.SortedLT := by rw [hR]; exact hsort.reverse
have hRmem : ∀ y ∈ R, L ≤ y := by
intro y hy
rw [hR] at hy
exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_reverse.mp hy)
have hRleH : ∀ y ∈ R, y ≤ H := by
intro y hy
rw [hR] at hy
exact (hsort.pairwise.imp le_of_lt).rel_head (List.mem_reverse.mp hy)
have hLleH : L ≤ H := hRmem H (by rw [hR, List.mem_reverse]; exact hHmem)
have hGR : ∀ x ∈ G, ∀ y ∈ R, x < y := by
intro x hx y hy
exact WithTop.coe_lt_coe.mp
(lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr (hRmem y hy)))
set T : List Key := G ++ R with hT
have hTsort : T.SortedLT := by
rw [hT]
exact List.sortedLT_append.mpr ⟨hGsort, hRsort, hGR⟩
let db_a : DB := ⟨T, hTsort⟩
have hfullKeys : db_a.keys = T := rfl
have hTlen : T.length = e := by
rw [hT, List.length_append, hGlen, hR, List.length_reverse, ← ht']
omega
have hlen_a : db_a.keys.length = e := by rw [hfullKeys, hTlen]
have hcnt0 : cnt db_a E = 0 := by
apply cnt_eq_zero_of_forall_ge
intro x hx
rw [hfullKeys, hT] at hx
rcases List.mem_append.mp hx with hx | hx
· exact (hGmem x hx).1
· exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx))
have hanchorEnd : (r.end_.anchor db_a).toNat = e := by
have hanc : r.end_.anchor db_a = r.end_.offset - 1 := by
unfold Selector.anchor
rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) db_a.keys =
0 from by
rw [← hE]; exact hcnt0]
ring
rw [hanc]
have hre_a : resolve db_a r.end_ = (⊤ : WithTop Key) := by
refine resolve_eq_top (by
change 0 ≤ (cnt db_a r.end_.key : Int) + r.end_.offset - 1
rw [show cnt db_a r.end_.key = 0 from by simpa [hE] using hcnt0]
push_cast; omega) ?_
rw [hanchorEnd, hlen_a]
have hoff' : r.begin.offset = 1 - (t : Int) := by linarith
have hanchorBegin : r.begin.anchor db_a = (D : Int) := by
change (cnt db_a r.begin.key : Int) + r.begin.offset - 1 = (D : Int)
have hecast : (e : Int) = (D : Int) + (t : Int) := by exact_mod_cast hDt.symm
rw [hbtop, cnt_top, hlen_a, hecast, hoff']
ring
have hDltN : D < db_a.keys.length := by rw [hlen_a]; omega
have hgetD : db_a.keys[D] = L := by
have h1 : db_a.keys[D]? = some L := by
rw [hfullKeys, hT]
rw [List.getElem?_append_right (show G.length ≤ D by rw [hGlen]), hGlen, Nat.sub_self]
rw [hR, ← List.head?_eq_getElem?, List.head?_reverse, List.getLast?_eq_some_getLast hne, hL]
rw [List.getElem?_eq_getElem hDltN] at h1
exact Option.some.inj h1
have hrb_a : resolve db_a r.begin = ((L : Key) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor db_a := by rw [hanchorBegin]; exact_mod_cast Nat.zero_le _
have hlt : (r.begin.anchor db_a).toNat < db_a.keys.length := by
rw [hanchorBegin]; exact_mod_cast hDltN
rw [resolve_eq_getElem h0 hlt]
rw [WithTop.coe_eq_coe]
have hidx : (r.begin.anchor db_a).toNat = D := by rw [hanchorBegin]; rfl
have h1 : db_a.keys[(r.begin.anchor db_a).toNat]? = some L := by
have h2 : db_a.keys[(r.begin.anchor db_a).toNat]? = db_a.keys[D]? := by rw [hidx]
rw [h2, List.getElem?_eq_getElem hDltN]
exact congrArg some hgetD
exact Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans h1)
have hfilter_a : db_a.keys.filter (fun x : Key => decide (L ≤ x)) = R := by
rw [hfullKeys, hT, List.filter_append]
have hGnil : List.filter (fun x : Key => decide (L ≤ x)) G = [] := by
rw [List.filter_eq_nil_iff]
intro x hx hlx
exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx))
(not_le.mpr (hGmem x hx).2)
have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by
rw [List.filter_eq_self]
intro x hx
exact decide_eq_true (hRmem x hx)
rw [hGnil, hRself, List.nil_append]
have hcomp_a : RangeRead.Compatible db_a r res := by
refine (compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mpr
⟨hcomp₀.1, ?_⟩
rw [completeKeys_filter_ge_eq_resolve_end (db := db_a) (r := r) (last := L) hrev
(le_of_eq hrb_a), hfilter_a, hR, hre_a]
simp
have hk_a : k ∉ db_a.keys := by
rw [hfullKeys, hT]
intro hmem
rcases List.mem_append.mp hmem with hx | hx
· exact hkG hx
· exact absurd (WithTop.coe_le_coe.mpr (hRmem k hx)) (not_le.mpr hkL')
-- insert the target
let db_b : DB := db_a.insert k hk_a
have hkR : ∀ x ∈ R, k < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hkL' (WithTop.coe_le_coe.mpr (hRmem x hx)))
have hins : T.orderedInsert (· ≤ ·) k = G.orderedInsert (· ≤ ·) k ++ R := by
rw [hT]
exact orderedInsert_append_of_forall_lt hkR
have hb_keys : db_b.keys = G.orderedInsert (· ≤ ·) k ++ R := by
rw [show db_b.keys = db_a.keys.orderedInsert (· ≤ ·) k from rfl, hfullKeys, hins]
have hb_len : db_b.keys.length = e + 1 := by
rw [hb_keys, List.length_append, List.orderedInsert_length, hGlen, hR,
List.length_reverse, ← ht']
omega
have hb_cnt0 : cnt db_b E = 0 := by
apply cnt_eq_zero_of_forall_ge
intro x hx
rw [hb_keys] at hx
rcases List.mem_append.mp hx with hx | hx
· rw [List.mem_orderedInsert] at hx
rcases hx with rfl | hx
· exact hEk
· exact (hGmem x hx).1
· exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx))
have hancEnd_b : (r.end_.anchor db_b).toNat = e := by
have hanc : r.end_.anchor db_b = r.end_.offset - 1 := by
unfold Selector.anchor
rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) db_b.keys =
0 from by
rw [← hE]; exact hb_cnt0]
ring
rw [hanc]
have hre_b : resolve db_b r.end_ = ((H : Key) : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor db_b := by
change 0 ≤ (cnt db_b r.end_.key : Int) + r.end_.offset - 1
rw [show cnt db_b r.end_.key = 0 from by simpa [hE] using hb_cnt0]
push_cast; omega
have hlt : (r.end_.anchor db_b).toNat < db_b.keys.length := by
rw [hancEnd_b, hb_len]; omega
rw [resolve_eq_getElem h0 hlt, WithTop.coe_eq_coe]
have hidx : (r.end_.anchor db_b).toNat = e := hancEnd_b
have hG'len : (G.orderedInsert (· ≤ ·) k).length = D + 1 := by
rw [List.orderedInsert_length, hGlen]
have he_ge : (G.orderedInsert (· ≤ ·) k).length ≤ e := by rw [hG'len]; omega
have h1 : db_b.keys[(r.end_.anchor db_b).toNat]? = some H := by
have h2 : db_b.keys[(r.end_.anchor db_b).toNat]? = db_b.keys[e]? := by rw [hidx]
rw [h2, hb_keys, List.getElem?_append_right he_ge]
have hsub : e - (G.orderedInsert (· ≤ ·) k).length = t - 1 := by
rw [hG'len, ← hDt]; omega
rw [hsub, hR]
rw [List.getElem?_reverse (by rw [← ht']; omega)]
rw [show res.keys.length - 1 - (t - 1) = 0 by rw [← ht']; omega]
have hh := getElem?_zero_eq_head (l := res.keys) hne
rwa [← hH] at hh
exact Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans h1)
have hHmem_b : H ∈ db_b.keys := by
rw [hb_keys]
exact List.mem_append_right _ (by rw [hR, List.mem_reverse]; exact hHmem)
refine necessary_of_toggle ⟨db_a, hcomp_a, ?_⟩
rw [toggle_eq_insert_of_not_mem hk_a]
intro hcomp'
have hfil := (compatible_iff_truncated_keys_rev (db := db_b) hrev hmore hne).mp hcomp' |>.2
have hHck : H ∈ RangeRead.completeKeys db_b r := by
have h := hHmem
rw [← hfil] at h
exact (List.mem_filter.mp h).1
exact absurd (RangeRead.mem_completeKeys.mp hHck).2.2 (by rw [hre_b]; exact lt_irrefl _)
/-! ### Gap-key necessity in regime B -/
/-- **Gap-key necessity in the sentinel-begin large-gap top cell, regime B.**
When `#reported + begin.offset ≤ 0`, the witness stores `D = e - t` gap keys in
`[end_.key, last)` including the target, then the reversed reported keys, then a key
above the reported head. With the target present the end selector resolves onto the
last witness key, so the keys above the reported last are exactly the reported ones.
Erasing the target lowers the database length by one; the end anchor now overshoots and
the witness key above the head enters the complete window, breaking the reported
prefix. -/
theorem necessary_gap_of_sentinel_large_B {r : RangeRead} {res : Result}
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
(_hbeg : r.begin.offset < 0) (hne : res.keys ≠ [])
(hbtop : r.begin.key = (⊤ : WithTop Key))
(hend : 2 ≤ r.end_.offset)
(hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key))
(htg : res.keys.length < (r.end_.offset - 1).toNat)
(hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤
intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) +
(res.keys.length : ℕ∞))
(hoff : (res.keys.length : Int) + r.begin.offset ≤ 0)
{k : Key} (hEk : r.end_.key ≤ (k : WithTop Key))
(hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) :
Necessary r res k := by
classical
obtain ⟨db₀, hcomp₀⟩ := hread
set E : WithTop Key := r.end_.key with hE
set L : Key := res.keys.getLast hne with hL
set H : Key := res.keys.head hne with hH
set e : Nat := (r.end_.offset - 1).toNat with he
set t : Nat := res.keys.length with ht'
set D : Nat := e - t with hD
have htn : 0 < t := List.length_pos_of_ne_nil hne
have ht_lt : t < e := by simpa [t, e] using htg
have hDt : D + t = e := by omega
have hD1 : 1 ≤ D := by omega
have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne
have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne
have hEL' : E < ((L : Key) : WithTop Key) := by simpa [E, L] using hEL
have hkL' : ((k : Key) : WithTop Key) < ((L : Key) : WithTop Key) := hkL
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev
set R : List Key := res.keys.reverse with hR
have hRsort : R.SortedLT := by rw [hR]; exact hsort.reverse
have hRmem : ∀ y ∈ R, L ≤ y := by
intro y hy
rw [hR] at hy
exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_reverse.mp hy)
have hRleH : ∀ y ∈ R, y ≤ H := by
intro y hy
rw [hR] at hy
exact (hsort.pairwise.imp le_of_lt).rel_head (List.mem_reverse.mp hy)
have hLleH : L ≤ H := hRmem H (by rw [hR, List.mem_reverse]; exact hHmem)
-- room for `D` gap keys, one of them the target
have hcapD : (D : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by
have hgap' : (e : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := by
simpa [E, L, e, t] using hgap
have hcast : ((D : ℕ∞) + (t : ℕ∞)) = (e : ℕ∞) := by
rw [← Nat.cast_add, hDt]
rw [← hcast] at hgap'
exact (ENat.add_le_add_iff_right (ENat.natCast_ne_top t)).mp hgap'
have hroom : (((D - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by
rw [Nat.sub_add_cancel hD1]; exact hcapD
obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ :=
exists_db_keysIn_avoid (B := E) (E := ((L : Key) : WithTop Key)) (k := k)
(m := D - 1) hroom
set Gp : List Key := pad.keys with hGpdef
have hGpmem : ∀ x ∈ Gp, E ≤ (x : WithTop Key) ∧
(x : WithTop Key) < ((L : Key) : WithTop Key) := by
intro x hx
simpa only [Gp, keysIn, Set.mem_ofPred_eq] using hpadIn x (by simpa [hGpdef] using hx)
have hGpsort : Gp.SortedLT := by rw [hGpdef]; exact pad.sorted
have hGplen : Gp.length = D - 1 := by rw [hGpdef]; exact hpadlen
have hkGp : k ∉ Gp := by rw [hGpdef]; exact hkpad
-- insert the target into the gap keys
let dbG : DB := pad.insert k hkGp
set G : List Key := dbG.keys with hGdef
have hGsort : G.SortedLT := by rw [hGdef]; exact dbG.sorted
have hGins : G = Gp.orderedInsert (· ≤ ·) k := by
rw [hGdef]; exact DB.insert_keys pad k hkGp
have hGlen : G.length = D := by
rw [hGins, List.orderedInsert_length, hGplen]; omega
have hGmem : ∀ x ∈ G, E ≤ (x : WithTop Key) ∧
(x : WithTop Key) < ((L : Key) : WithTop Key) := by
intro x hx
rw [hGins, List.mem_orderedInsert] at hx
rcases hx with rfl | hx
· exact ⟨hEk, hkL'⟩
· exact hGpmem x hx
have hkG : k ∈ G := by
rw [hGins]; exact (List.mem_orderedInsert (· ≤ ·)).mpr (Or.inl rfl)
-- a key above the reported head
set Q : Key := keyAfter H with hQ
have hHQ : H < Q := by rw [hQ]; exact keyAfter_self_lt H
have hRQ : ∀ y ∈ R, y < Q := fun y hy => lt_of_le_of_lt (hRleH y hy) hHQ
have hGQ : ∀ x ∈ G, x < Q := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hGmem x hx).2
(WithTop.coe_le_coe.mpr (le_trans hLleH (le_of_lt hHQ))))
have hGR : ∀ x ∈ G, ∀ y ∈ R, x < y := by
intro x hx y hy
exact WithTop.coe_lt_coe.mp
(lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr (hRmem y hy)))
-- assemble the witness database
set T : List Key := G ++ R ++ [Q] with hT
have hGRsort : (G ++ R).SortedLT :=
List.sortedLT_append.mpr ⟨hGsort, hRsort, hGR⟩
have hGRQ : ∀ x ∈ G ++ R, ∀ y ∈ [Q], x < y := by
intro x hx y hy
rw [List.mem_singleton] at hy
subst hy
rcases List.mem_append.mp hx with hx | hx
· exact hGQ x hx
· exact hRQ x hx
have hTsort : T.SortedLT := by
rw [hT]
exact List.sortedLT_append.mpr ⟨hGRsort, by simp [List.sortedLT_iff_pairwise], hGRQ⟩
let db_a : DB := ⟨T, hTsort⟩
have hfullKeys : db_a.keys = T := rfl
have hTlen : T.length = D + t + 1 := by
rw [hT]
simp [List.length_append, hGlen, hR, ht']
omega
have hlen_a : db_a.keys.length = D + t + 1 := by rw [hfullKeys, hTlen]
have hcnt0 : cnt db_a E = 0 := by
apply cnt_eq_zero_of_forall_ge
intro x hx
rw [hfullKeys, hT] at hx
rcases List.mem_append.mp hx with hx | hx
· rcases List.mem_append.mp hx with hx | hx
· exact (hGmem x hx).1
· exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx))
· rw [List.mem_singleton] at hx
subst hx
exact le_trans (le_of_lt hEL')
(WithTop.coe_le_coe.mpr (le_trans hLleH (le_of_lt hHQ)))
have hanchorEnd : (r.end_.anchor db_a).toNat = e := by
have hanc : r.end_.anchor db_a = r.end_.offset - 1 := by
unfold Selector.anchor
rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))
db_a.keys = 0 from by rw [← hE]; exact hcnt0]
ring
rw [hanc]
have hQidx : db_a.keys[e]? = some Q := by
rw [hfullKeys, hT]
rw [List.getElem?_append_right (show (G ++ R).length ≤ e by
rw [List.length_append, hGlen, hR, List.length_reverse]; omega)]
rw [List.length_append, hGlen, hR, List.length_reverse]
rw [show e - (D + res.keys.length) = 0 by omega]
rfl
have hre_a : resolve db_a r.end_ = ((Q : Key) : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor db_a := by
change 0 ≤ (cnt db_a r.end_.key : Int) + r.end_.offset - 1
rw [show cnt db_a r.end_.key = 0 from by simpa [hE] using hcnt0]
push_cast; omega
have hlt : (r.end_.anchor db_a).toNat < db_a.keys.length := by
rw [hanchorEnd, hlen_a]; omega
rw [resolve_eq_getElem h0 hlt]
rw [WithTop.coe_eq_coe]
have hidx : (r.end_.anchor db_a).toNat = e := hanchorEnd
have h1 : db_a.keys[(r.end_.anchor db_a).toNat]? = some Q := by
have h2 : db_a.keys[(r.end_.anchor db_a).toNat]? = db_a.keys[e]? := by rw [hidx]
rw [h2]; exact hQidx
exact Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans h1)
-- the reported last sits at index `D`
have hLidx : db_a.keys[D]? = some L := by
rw [hfullKeys, hT, List.append_assoc]
rw [List.getElem?_append_right (show G.length ≤ D by rw [hGlen])]
rw [hGlen, Nat.sub_self]
rw [List.getElem?_append_left (by rw [hR, List.length_reverse, ← ht']; omega)]
rw [hR, ← List.head?_eq_getElem?, List.head?_reverse, List.getLast?_eq_some_getLast hne, hL]
have hrb_a : resolve db_a r.begin ≤ ((L : Key) : WithTop Key) := by
have hDltN : D < db_a.keys.length := by rw [hlen_a]; omega
have hclamp : clampIndex db_a r.begin ≤ D := by
unfold clampIndex
have hanchor_le : r.begin.anchor db_a ≤ (D : Int) := by
change (cnt db_a r.begin.key : Int) + r.begin.offset - 1 ≤ (D : Int)
rw [hbtop, cnt_top, hlen_a]
have hcast : ((D + t + 1 : Nat) : Int) = (D : Int) + (t : Int) + 1 := by push_cast; ring
rw [hcast]; linarith
have htoNat : (r.begin.anchor db_a).toNat ≤ D := (Int.toNat_le).mpr hanchor_le
exact le_trans (Nat.min_le_left _ _) htoNat
have hget : db_a.keys[D] = L := by
rw [List.getElem?_eq_getElem hDltN] at hLidx
exact Option.some.inj hLidx
rw [← hget]
exact (resolve_le_getElem_iff (db := db_a) (s := r.begin) hDltN).mpr hclamp
have hfilter_a : db_a.keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by
rw [hfullKeys, hT, List.filter_append, List.filter_append]
have hGnil : List.filter (fun x : Key => decide (L ≤ x)) G = [] := by
rw [List.filter_eq_nil_iff]
intro x hx hlx
exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx))
(not_le.mpr (hGmem x hx).2)
have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by
rw [List.filter_eq_self]
intro x hx
exact decide_eq_true (hRmem x hx)
have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by
simp [decide_eq_true (le_trans hLleH (le_of_lt hHQ))]
rw [hGnil, hRself, hQself]
simp
have hinner_a : (R ++ [Q]).filter
(fun x : Key => decide ((x : WithTop Key) < resolve db_a r.end_)) = R := by
rw [hre_a, List.filter_append]
have hRself : List.filter (fun x : Key =>
decide ((x : WithTop Key) < ((Q : Key) : WithTop Key))) R = R := by
rw [List.filter_eq_self]
intro x hx
exact decide_eq_true (WithTop.coe_lt_coe.mpr (hRQ x hx))
have hQnil : List.filter (fun x : Key =>
decide ((x : WithTop Key) < ((Q : Key) : WithTop Key))) [Q] = [] := by
simp
rw [hRself, hQnil, List.append_nil]
have hcomp_a : RangeRead.Compatible db_a r res := by
refine (compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mpr
⟨hcomp₀.1, ?_⟩
rw [completeKeys_filter_ge_eq_resolve_end (db := db_a) (r := r) (last := L) hrev hrb_a,
hfilter_a, hinner_a, hR, List.reverse_reverse]
-- erase the target
have hk_a : k ∈ db_a.keys := by
rw [hfullKeys, hT]
exact List.mem_append_left _ (List.mem_append_left _ hkG)
let db_b : DB := db_a.erase k
have hb_keys : db_b.keys = G.erase k ++ R ++ [Q] := by
change (db_a.keys.erase k) = _
rw [hfullKeys, hT, List.append_assoc, List.erase_append_left (R ++ [Q]) hkG,
List.append_assoc]
have hb_len : db_b.keys.length = D + t := by
change (db_a.keys.erase k).length = D + t
rw [List.length_erase_of_mem hk_a, hlen_a]
omega
have hb_cnt0 : cnt db_b E = 0 := by
apply cnt_eq_zero_of_forall_ge
intro x hx
rw [hb_keys] at hx
rcases List.mem_append.mp hx with hx | hx
· rcases List.mem_append.mp hx with hx | hx
· exact (hGmem x (List.mem_of_mem_erase hx)).1
· exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx))
· rw [List.mem_singleton] at hx
subst hx
exact le_trans (le_of_lt hEL')
(WithTop.coe_le_coe.mpr (le_trans hLleH (le_of_lt hHQ)))
have hanchorEnd_b : (r.end_.anchor db_b).toNat = e := by
have hanc : r.end_.anchor db_b = r.end_.offset - 1 := by
unfold Selector.anchor
rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))
db_b.keys = 0 from by rw [← hE]; exact hb_cnt0]
ring
rw [hanc]
have hre_b : resolve db_b r.end_ = (⊤ : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor db_b := by
change 0 ≤ (cnt db_b r.end_.key : Int) + r.end_.offset - 1
rw [show cnt db_b r.end_.key = 0 from by simpa [hE] using hb_cnt0]
push_cast; omega
refine resolve_eq_top h0 ?_
rw [hanchorEnd_b, hb_len]
exact le_of_eq hDt
have hEraseLen : (G.erase k).length = D - 1 := by
rw [List.length_erase_of_mem hkG, hGlen]
have hLidx_b : db_b.keys[D - 1]? = some L := by
rw [hb_keys, List.append_assoc]
rw [List.getElem?_append_right (show (G.erase k).length ≤ D - 1 by rw [hEraseLen])]
rw [hEraseLen, Nat.sub_self]
rw [List.getElem?_append_left (by rw [hR, List.length_reverse, ← ht']; omega)]
rw [hR, ← List.head?_eq_getElem?, List.head?_reverse, List.getLast?_eq_some_getLast hne, hL]
have hrb_b : resolve db_b r.begin ≤ ((L : Key) : WithTop Key) := by
have hjlt : D - 1 < db_b.keys.length := by rw [hb_len]; omega
have hget : db_b.keys[D - 1] = L := by
rw [List.getElem?_eq_getElem hjlt] at hLidx_b
exact Option.some.inj hLidx_b
rw [← hget]
apply (resolve_le_getElem_iff (db := db_b) (s := r.begin) hjlt).mpr
unfold clampIndex
have hanchor_le : r.begin.anchor db_b ≤ ((D - 1 : Nat) : Int) := by
change (cnt db_b r.begin.key : Int) + r.begin.offset - 1 ≤ ((D - 1 : Nat) : Int)
rw [hbtop, cnt_top, hb_len]
have hcast : ((D + t : Nat) : Int) = (D : Int) + (t : Int) := by push_cast; ring
rw [hcast]
have hcast2 : ((D - 1 : Nat) : Int) = (D : Int) - 1 := by
rw [Nat.cast_sub hD1]; norm_num
rw [hcast2]
linarith
have htoNat : (r.begin.anchor db_b).toNat ≤ D - 1 := (Int.toNat_le).mpr hanchor_le
exact le_trans (Nat.min_le_left _ _) htoNat
have hfilter_b : (RangeRead.completeKeys db_b r).filter
(fun k => decide (L ≤ k)) = Q :: res.keys := by
rw [completeKeys_filter_ge_eq_resolve_end (db := db_b) (r := r) (last := L) hrev hrb_b,
hre_b]
have hbF : db_b.keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by
rw [hb_keys, List.filter_append, List.filter_append]
have hGnil : List.filter (fun x : Key => decide (L ≤ x)) (G.erase k) = [] := by
rw [List.filter_eq_nil_iff]
intro x hx hlx
exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx))
(not_le.mpr (hGmem x (List.mem_of_mem_erase hx)).2)
have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by
rw [List.filter_eq_self]
intro x hx
exact decide_eq_true (hRmem x hx)
have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by
simp [decide_eq_true (le_trans hLleH (le_of_lt hHQ))]
rw [hGnil, hRself, hQself]
simp
rw [hbF]
have hinner : (R ++ [Q]).filter
(fun x : Key => decide ((x : WithTop Key) < (⊤ : WithTop Key))) = R ++ [Q] := by
rw [List.filter_eq_self]
intro x hx
exact decide_eq_true (WithTop.coe_lt_top x)
rw [hinner, List.reverse_append, List.reverse_singleton, hR]
simp
refine necessary_of_toggle ⟨db_a, hcomp_a, ?_⟩
rw [toggle_eq_erase_of_mem hk_a]
intro hcomp'
have hfil := (compatible_iff_truncated_keys_rev (db := db_b) hrev hmore hne).mp hcomp' |>.2
rw [hfilter_b] at hfil
have hlen_eq := congrArg List.length hfil
simp at hlen_eq
/-! ### Assembling the sentinel-begin cell -/
/-- **The sentinel-begin large-gap top cell closed form is minimal.** When the cell is
tight the range is `[last, ⊤)`, with the compatible databases reduced to the sentinel-end
read. Otherwise it is `[end_.key, ⊤)`: soundness is
`sound_topRange_endKey_of_residual_large`; necessity splits `[end_.key, ⊤)` at the
reported last and head, using the gap-key witnesses of regime A or B (decided by the
offset bound `sentinel_begin_offset_bound`), convexity for reported keys, and the
above-head witness. -/
theorem minimal_closedForm_of_negCross_trunc_rev_residual_top_large_sentinel
{r : RangeRead} {res : Result}
(hread : Readable r res) (h : negCrossTruncRevResidualTopLargeSentinel r res) :
Minimal r res (closedFormNegCrossTruncRevResidualTopLargeSentinel r res) := by
obtain ⟨hlarge, hbtop⟩ := h
obtain ⟨hmore, hrev, hbeg, hne, hend, hELD, htopD, hgapD⟩ := hlarge
obtain ⟨db₀, hcomp₀⟩ := hread
have hread' : Readable r res := ⟨db₀, hcomp₀⟩
set L : Key := res.keys.getLast hne with hLdef
set H : Key := res.keys.head hne with hHdef
have hlastD : res.keys.getLastD ([] : Key) = L := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]
simp [hLdef]
have hEL : r.end_.key < ((L : Key) : WithTop Key) := by
rw [hlastD] at hELD
exact hELD
have hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤
intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by
rw [hlastD] at hgapD
exact hgapD
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev
have hLle : ∀ x ∈ res.keys, L ≤ x := by
intro x hx
rw [hLdef]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne
have hcountP : res.keys.countP
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by
rw [List.countP_eq_length]
intro x hx
exact decide_eq_true (le_of_lt (lt_of_lt_of_le hEL (WithTop.coe_le_coe.mpr (hLle x hx))))
have ht : res.keys.length < (r.end_.offset - 1).toNat := by
rw [hcountP] at htopD
exact htopD
by_cases htight : sentinelLargeTight r res
· -- tight branch: the range is `[last, ⊤)`
have htight' : ((r.end_.offset - 1).toNat : ℕ∞) =
intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) ∧
(res.keys.length : Int) + r.begin.offset = 1 := by
simpa only [sentinelLargeTight, hlastD] using htight
have hmin := minimal_topOfLast_of_sentinel_large_tight hread' hmore hrev hbeg hne
hbtop hend (by simpa [hLdef] using hEL) htight'.2 htight'.1
have hcf : closedFormNegCrossTruncRevResidualTopLargeSentinel r res =
topOfLast (res.keys.getLast hne) := by
unfold closedFormNegCrossTruncRevResidualTopLargeSentinel topOfLast
dsimp only
rw [ite_eq_left htight]
simp [List.getLast?_eq_some_getLast hne]
rw [hcf]
exact hmin
· -- non-tight branch: the range is `[end_.key, ⊤)`
have hmin : Minimal r res ⟨r.end_.key, ⊤⟩ := by
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
· exact sound_topRange_endKey_of_residual_large hmore hrev hbeg hne hend hEL
· intro k hk
rw [ConflictRange.mem_iff] at hk
obtain ⟨hEk, _⟩ := hk
by_cases hkL : (k : WithTop Key) < ((L : Key) : WithTop Key)
· -- a gap key below the reported last
have hbound : (res.keys.length : Int) + r.begin.offset ≤ 1 :=
sentinel_begin_offset_bound hcomp₀ hmore hrev hne hbtop
rcases lt_or_eq_of_le hbound with hlt1 | heq
· -- regime B: both lengths cannot be large
have hB : (res.keys.length : Int) + r.begin.offset ≤ 0 := by omega
exact necessary_gap_of_sentinel_large_B hread' hmore hrev hbeg hne hbtop hend
(by simpa [hLdef] using hEL) ht hgap hB hEk hkL
· -- regime A: the gap is strict
have hstrict : ((r.end_.offset - 1).toNat : ℕ∞) <
intervalCard r.end_.key ((L : Key) : WithTop Key) +
(res.keys.length : ℕ∞) := by
have hnt' : ¬ (((r.end_.offset - 1).toNat : ℕ∞) =
intervalCard r.end_.key ((L : Key) : WithTop Key) +
(res.keys.length : ℕ∞) ∧
(res.keys.length : Int) + r.begin.offset = 1) := by
intro hc
exact htight (by simpa only [sentinelLargeTight, hlastD] using hc)
have hle : ((r.end_.offset - 1).toNat : ℕ∞) ≤
intervalCard r.end_.key ((L : Key) : WithTop Key) +
(res.keys.length : ℕ∞) := hgap
exact lt_of_le_of_ne hle (fun he => hnt' ⟨he, heq⟩)
exact necessary_gap_of_sentinel_large_A hread' hmore hrev hbeg hne hbtop hend
(by simpa [hLdef] using hEL) ht hstrict heq hEk hkL
· -- a key at or above the reported last
have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hkL
have hkKey : L ≤ k := WithTop.coe_le_coe.mp hLk
by_cases hkH : (k : WithTop Key) ≤ ((H : Key) : WithTop Key)
· exact necessary_of_between_result hread' hLmem hHmem hkKey
(WithTop.coe_le_coe.mp hkH)
· have hHk : H < k := WithTop.coe_lt_coe.mp (lt_of_not_ge hkH)
exact necessary_above_head_of_residual_top_large hread' hmore hrev hbeg hne
hend (by simpa [hLdef] using hEL) ht
(by rw [hbtop]; exact WithTop.coe_lt_top L)
(by simpa [hHdef] using hHk)
have hcf : closedFormNegCrossTruncRevResidualTopLargeSentinel r res =
⟨r.end_.key, ⊤⟩ := by
unfold closedFormNegCrossTruncRevResidualTopLargeSentinel
dsimp only
rw [ite_eq_right htight]
rw [hcf]
exact hmin
end FormalConflictRange