Files
conflict-range/FormalConflictRange/NegCrossTruncNonposGapA2aBottom.lean
T
andrew af318a7d32 Fix all Lean build warnings
- Update deprecated lemmas (ite_eq_left/right, ENat casts, List.Sublist, ...)
- Replace push_neg with push Not and resolve unused-variable warnings
- Convert flexible simp/simp_all calls to explicit simp only lists
- Remove unscoped maxHeartbeats options, open scoped Classical, and unused tactics
- Add missing module docstrings and clean up style linter hits
- Wrap over-long lines and flatten the deeply nested
  minimal_conflictRangeClosedForm_total proof

`lake build` now completes with zero warnings.
2026-11-17 10:04:35 -05:00

1782 lines
90 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.
/-
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: FormalConflictRange contributors
-/
import FormalConflictRange.NegCrossTruncNonposGapA2a
/-!
# The crossed truncated nonpositive-end gap cell at `end_.key < begin.key`, clamping half
This module closes the clamping (`clampNonposBottom`) half of the crossed
negative-begin truncated nonpositive-end gap cell in the sub-region where the end key
lies strictly below the begin key.
-/
namespace FormalConflictRange
/-- The crossed negative-begin truncated nonpositive-end gap cell in the sub-region
`end_.key < begin.key` with a clamping begin selector. -/
def negCrossTruncNonposGapA2aBottom (r : RangeRead) (res : Result) : Prop :=
negCrossTruncNonposGap r res ∧ r.end_.key < r.begin.key ∧ clampNonposBottom r res
/-- The gap-capacity condition selecting the `[⊥, begin.key)` branch in the clamping
sub-region. -/
def negCrossTruncNonposGapA2aBottomHigh (r : RangeRead) (res : Result) : Prop :=
negCrossTruncNonposGapA2aBottom r res ∧
((beginSlack r + 1 + (if res.keys.headD ([] : Key) = [] then 1 else 0) : ℕ) : ℕ∞) ≤
(res.keys.length : ℕ∞) +
intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)
r.begin.key
instance instDecidableA2aBottomHigh (r : RangeRead) (res : Result) :
Decidable (negCrossTruncNonposGapA2aBottomHigh r res) := by
unfold negCrossTruncNonposGapA2aBottomHigh negCrossTruncNonposGapA2aBottom
negCrossTruncNonposGap clampNonposBottom
infer_instance
/-- The closed form of the crossed nonpositive-end gap cell in the clamping
`end_.key < begin.key` sub-region. -/
noncomputable def closedFormNegCrossTruncNonposGapA2aBottom
(r : RangeRead) (res : Result) : ConflictRange := by
classical
exact if negCrossTruncNonposGapA2aBottomHigh r res then
⟨(([] : Key) : WithTop Key), r.begin.key⟩
else
⟨(([] : Key) : WithTop Key), r.end_.key⟩
/-- Padding the reported keys with `clampNonposD + 1` fillers in `(last, end_.key)` gives a
compatible database with a clamping begin selector, provided the begin slack absorbs the
whole stored list. -/
theorem compatible_clamp_fillers_A2a {r : RangeRead} {res : Result} {dbF : DB}
(hsort : res.keys.SortedLT) (hrev : r.reverse = false) (hmore : res.more = true)
(hne : res.keys ≠ [])
(hlim : RangeRead.withinLimit r res.keys)
(hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0)
(hEB : r.end_.key < r.begin.key)
(hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x)
(hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key)
(hFlen : dbF.keys.length = clampNonposD r + 1)
(hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r) :
∃ dbw : DB, dbw.keys = res.keys ++ dbF.keys ∧ RangeRead.Compatible dbw r res ∧
r.begin.anchor dbw < 0 ∧ r.end_.anchor dbw = (res.keys.length : ℤ) := by
classical
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
(hsort.pairwise.imp le_of_lt).rel_getLast hx
have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by
intro h
rw [h] at hEB
exact absurd hEB (not_lt.mpr le_top)
obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = ((e : Key) : WithTop Key) := by
cases h : r.end_.key with
| top => exact absurd h hEne
| coe e => exact ⟨e, rfl⟩
have hDlen : dbF.keys.length = clampNonposD r + 1 := hFlen
have hFne : dbF.keys ≠ [] := by intro h; rw [h] at hDlen; simp at hDlen
have hFlast' : ∀ x ∈ dbF.keys, last < x := fun x hx => by
rw [hlast]; exact hFlast x hx
-- the reported last and every filler lie below `begin.key`
have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by
obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil _ hFne
exact lt_trans (WithTop.coe_lt_coe.mpr (hFlast' x hx)) (hFend x hx)
have hlastB' : ((last : Key) : WithTop Key) < r.begin.key :=
lt_of_lt_of_le hlast_lt_E (le_of_lt hEB)
have hFltB : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.begin.key := fun x hx =>
lt_of_lt_of_le (hFend x hx) (le_of_lt hEB)
set D : List Key := res.keys ++ dbF.keys with hD
have hDsort : D.SortedLT := by
rw [hD]
exact List.sortedLT_append.mpr ⟨hsort, dbF.sorted, fun a ha b hb =>
lt_of_le_of_lt (hle_last a ha) (hFlast' b hb)⟩
let dbw : DB := ⟨D, hDsort⟩
have hkeyw : dbw.keys = res.keys ++ dbF.keys := rfl
have hfill_head_gt : ∀ x ∈ dbF.keys, dbF.keys.head hFne ≤ x := fun x hx =>
(dbF.sorted.pairwise.imp le_of_lt).rel_head hx
have hcountAll : dbw.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) <
r.begin.key)) = dbw.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
rw [hkeyw] at hx
rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastB'
· exact hFltB x hx
have hlen : dbw.keys.length = res.keys.length + (clampNonposD r + 1) := by
rw [hkeyw, List.length_append, hDlen]
have hanchorB : r.begin.anchor dbw < 0 := by
rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountAll, hlen]
have hcast : (res.keys.length + (clampNonposD r + 1) : ℤ) ≤ (beginSlack r : ℤ) := by
exact_mod_cast hcount
omega
have hcountE : dbw.keys.countP (fun k : Key => decide (k < e)) =
res.keys.length + clampNonposD r + 1 := by
have hcl : dbw.keys.countP (fun k : Key => decide (k < e)) = dbw.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
rw [hkeyw] at hx
rcases List.mem_append.mp hx with hx | hx
· apply WithTop.coe_lt_coe.mp
rw [← he]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E
· apply WithTop.coe_lt_coe.mp
simpa [he] using hFend x hx
rw [hcl, hkeyw, List.length_append, hDlen]
omega
have hanchorE : r.end_.anchor dbw = (res.keys.length : ℤ) := by
rw [anchor_of_coe (s := r.end_) (v := e) he, hcountE]
have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by
rw [clampNonposD_eq]
have hnonneg : 0 ≤ -r.end_.offset := by omega
rw [Int.toNat_of_nonneg hnonneg]; ring
rw [hoff]; push_cast; ring
have hresolve_begin : resolve dbw r.begin = (([] : Key) : WithTop Key) :=
resolve_eq_nil hanchorB
have hresolve_end : resolve dbw r.end_ = ((dbF.keys.head hFne : Key) : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor dbw := by rw [hanchorE]; exact_mod_cast Nat.zero_le _
have hlt : (r.end_.anchor dbw).toNat < dbw.keys.length := by
rw [hanchorE, hkeyw, List.length_append, hDlen]
omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.end_.anchor dbw).toNat = res.keys.length := by
rw [hanchorE]; simp
simp only [hidx]
have hget : dbw.keys[res.keys.length]'(by rw [hkeyw, List.length_append, hDlen]; omega) =
dbF.keys.head hFne := by
have h1 : dbw.keys = res.keys ++ dbF.keys := hkeyw
rw [List.getElem_of_eq h1]
exact getElem_append_length hFne
exact congrArg (fun x : Key => (x : WithTop Key)) hget
have hckmem : ∀ k : Key, k ∈ RangeRead.completeKeys dbw r ↔ k ∈ res.keys := by
intro k
rw [RangeRead.mem_completeKeys, hresolve_begin, hresolve_end]
constructor
· rintro ⟨hk, -, hlt⟩
rw [hkeyw] at hk
rcases List.mem_append.mp hk with hk | hk
· exact hk
· exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (hfill_head_gt k hk)))
· intro hk
refine ⟨?_, ?_, ?_⟩
· rw [hkeyw]; exact List.mem_append_left _ hk
· exact WithTop.coe_le_coe.mpr (nil_le_key k)
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk))
(WithTop.coe_lt_coe.mpr (hFlast' _ (List.head_mem hFne)))
have hck : RangeRead.completeKeys dbw r = res.keys := by
apply List.SortedLT.eq_of_mem_iff
· rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])]
exact List.sortedLT_iff_pairwise.mpr (RangeRead.inRange_pairwise dbw _ _)
· exact hsort
· exact hckmem
refine ⟨dbw, hkeyw, ?_, hanchorB, hanchorE⟩
rw [compatible_iff_truncated_keys hrev hmore hne]
refine ⟨hlim, ?_⟩
rw [hck]
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
exact hle_last x hx
/-- A clamping compatible database exists in the crossed nonpositive-end gap cell. -/
theorem exists_clamp_compatible_A2aBottom {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) :
∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by
obtain ⟨hcell0, hEB, hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤
intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key :=
intervalCard_keyAfter_getLast_ge hcomp₀ hne hend
obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn
(B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) (E := r.end_.key)
(m := clampNonposD r + 1) hcard
have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne)))
(hFmem x hx).1)
have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2
have hc0 : intervalCard r.begin.key r.end_.key = 0 :=
intervalCard_eq_zero_of_le (le_of_lt hEB)
have hrep : reportedBelowBegin r res = res.keys.length :=
reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB
have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
have h := hbot
unfold clampNonposBottom at h
rw [hc0, hrep, tsub_zero] at h
exact_mod_cast h
obtain ⟨dbw, hkeyw, hcompw, hanchor, -⟩ :=
compatible_clamp_fillers_A2a hsort hrev hmore hne hlim hbegin hend hEB hFlast hFend hFlen
hcount
exact ⟨dbw, hcompw, hanchor⟩
/-- A key strictly below the reported head has an insert witness in the clamping cell:
inserting it into a clamping compatible database adds it to the complete window and hence to
the truncated read-off. -/
theorem exists_witness_below_head_A2aBottom {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res)
(hne : res.keys ≠ [])
(hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧
¬ RangeRead.Compatible (toggle dbw k) r res := by
obtain ⟨hcell0, hEB, hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, -, -, -⟩ := hcell0
have hread' := hread
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne :=
(hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)
have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hlastB
have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlast_lt_B
have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hk hhead_lt_B
have hk_not_res : k ∉ res.keys := by
intro hx
exact absurd hk (not_lt.mpr
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx)))
have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤
intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key :=
intervalCard_keyAfter_getLast_ge hcomp₀ hne hend
obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn
(B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) (E := r.end_.key)
(m := clampNonposD r + 1) hcard
have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne)))
(hFmem x hx).1)
have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2
have hc0 : intervalCard r.begin.key r.end_.key = 0 :=
intervalCard_eq_zero_of_le (le_of_lt hEB)
have hrep : reportedBelowBegin r res = res.keys.length :=
reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB
have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
have h := hbot
unfold clampNonposBottom at h
rw [hc0, hrep, tsub_zero] at h
exact_mod_cast h
obtain ⟨dbw, hkeyw, hcompw, hanchorB, hanchorE⟩ :=
compatible_clamp_fillers_A2a hsort hrev hmore hne hlim hbegin hend hEB hFlast hFend hFlen
hcount
have hk_lt_all : ∀ x ∈ dbw.keys, k < x := by
intro x hx
rw [hkeyw] at hx
rcases List.mem_append.mp hx with hx | hx
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hk
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx)))
· have hk_last : k < res.keys.getLast hne :=
lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk) hhead_le_last
exact lt_trans hk_last (hFlast x hx)
have hk_not_dbw : k ∉ dbw.keys := fun hx => absurd (hk_lt_all k hx) (lt_irrefl k)
refine ⟨dbw, hcompw, ?_⟩
rw [toggle_eq_insert_of_not_mem hk_not_dbw]
intro hcomp'
have hins_keys : (dbw.insert k hk_not_dbw).keys = k :: dbw.keys := by
rw [DB.insert_keys]
cases hkw : dbw.keys with
| nil => simp
| cons a t =>
have hka : k ≤ a := le_of_lt (hk_lt_all a (by rw [hkw]; exact List.mem_cons_self ..))
rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka]
obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp'
have hrescnt : dbw.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) =
res.keys.length + (clampNonposD r + 1) := by
rw [hkeyw, List.countP_append]
have h1 : res.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B
have h2 : dbF.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbF.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_lt_of_le (hFend x hx) (le_of_lt hEB)
rw [h1, h2, hFlen]
have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) =
res.keys.length + (clampNonposD r + 1) + 1 := by
rw [hins_keys, List.countP_cons, decide_eq_true_eq.mpr hk_lt_B, ite_eq_left rfl, hrescnt]
have hanchor_ins : r.begin.anchor (dbw.insert k hk_not_dbw) ≤ 0 := by
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins]
have hcast : (res.keys.length + (clampNonposD r + 1) : ℤ) ≤ (beginSlack r : ℤ) := by
exact_mod_cast hcount
push_cast
omega
have hk_ck : k ∈ RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := by
rw [RangeRead.mem_completeKeys]
refine ⟨by rw [hins_keys]; exact List.mem_cons_self .., ?_, ?_⟩
· by_cases hneg : r.begin.anchor (dbw.insert k hk_not_dbw) < 0
· rw [resolve_eq_nil hneg]
exact WithTop.coe_le_coe.mpr (nil_le_key k)
· have hzero : r.begin.anchor (dbw.insert k hk_not_dbw) = 0 := by omega
have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by omega
have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat <
(dbw.insert k hk_not_dbw).keys.length := by
rw [hzero, hins_keys, List.length_cons]; simp
rw [resolve_eq_getElem h0 hlt]
have hget : (dbw.insert k hk_not_dbw).keys[(r.begin.anchor
(dbw.insert k hk_not_dbw)).toNat] = k := by
simp only [hzero, hins_keys]
rfl
rw [hget]
· have hpre := RangeRead.compatible_keys_prefix hcomp'
have hhead_mem : res.keys.head hne ∈
RangeRead.completeKeys (dbw.insert k hk_not_dbw) r :=
hpre.subset (List.head_mem hne)
exact lt_trans hk (RangeRead.mem_completeKeys.mp hhead_mem).2.2
have hk_le_last : k ≤ res.keys.getLast hne :=
le_trans (le_of_lt (WithTop.coe_lt_coe.mp hk)) hhead_le_last
have hk_filt : k ∈ (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) := by
rw [List.mem_filter]
exact ⟨hk_ck, by rw [decide_eq_true_eq]; exact hk_le_last⟩
rw [hfilt'] at hk_filt
exact hk_not_res hk_filt
/-- A key strictly between the reported last and `end_.key` has an erase witness in the
clamping cell: it sits among the `clampNonposD + 1` fillers, so erasing it lowers the end
resolution onto the reported last and drops the last reported key from the window. -/
theorem exists_witness_last_end_A2aBottom {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res)
(hne : res.keys ≠ [])
(hkgt : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key))
(hkE : (k : WithTop Key) < r.end_.key) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧
¬ RangeRead.Compatible (dbw.erase k) r res := by
obtain ⟨hcell0, hEB, hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, -, -, hend, hclamp, hlastB, hlastE, hEne, -⟩ := hcell0
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlast_lt_k : res.keys.getLast hne < k := WithTop.coe_lt_coe.mp hkgt
have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hlastB
have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hk_not_res : k ∉ res.keys := by
intro hx
exact absurd hlast_lt_k (not_lt.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx))
have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤
intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key :=
intervalCard_keyAfter_getLast_ge hcomp₀ hne hend
obtain ⟨dbB, hBmem, hBk, hBlen⟩ :=
exists_db_keysIn_avoid (B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))
(E := r.end_.key) (k := k) (m := clampNonposD r) hcard
have hBgt : ∀ x ∈ dbB.keys, res.keys.getLast hne < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne)))
(hBmem x hx).1)
have hBElt : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hBmem x hx).2
set raw : List Key := k :: dbB.keys with hraw
have hrawNodup : raw.Nodup := by
rw [hraw]
exact List.nodup_cons.mpr ⟨hBk, dbB.sorted.pairwise.nodup⟩
set F : List Key := raw.insertionSort (· ≤ ·) with hF
have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm
have hFsorted : F.SortedLT := by
rw [hF, List.sortedLT_iff_pairwise]
exact DB.pairwise_lt_of_le_of_nodup
(List.pairwise_insertionSort (r := (· ≤ ·)) raw)
(hFperm.nodup_iff.mp hrawNodup)
let dbF : DB := ⟨F, hFsorted⟩
have hFlen : dbF.keys.length = clampNonposD r + 1 := by
change F.length = clampNonposD r + 1
rw [hF, List.length_insertionSort, hraw, List.length_cons, hBlen]
have hFlt : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by
intro x hx
have hx' := hFperm.mem_iff.mpr hx
rw [hraw] at hx'
rcases List.mem_cons.mp hx' with rfl | hxB
· exact hlast_lt_k
· exact hBgt x hxB
have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := by
intro x hx
have hx' := hFperm.mem_iff.mpr hx
rw [hraw] at hx'
rcases List.mem_cons.mp hx' with rfl | hxB
· exact hkE
· exact hBElt x hxB
have hc0 : intervalCard r.begin.key r.end_.key = 0 :=
intervalCard_eq_zero_of_le (le_of_lt hEB)
have hrep : reportedBelowBegin r res = res.keys.length :=
reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB
have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
have h := hbot
unfold clampNonposBottom at h
rw [hc0, hrep, tsub_zero] at h
exact_mod_cast h
obtain ⟨dbw, hkeyw, hcompw, hanchorB, hanchorE⟩ :=
compatible_clamp_fillers_A2a hsort hrev hmore hne hlim hbegin hend hEB hFlt hFend hFlen
hcount
have hk_in_F : k ∈ dbF.keys :=
hFperm.mem_iff.mp (by rw [hraw]; exact List.mem_cons_self ..)
have hk_in_dbw : k ∈ dbw.keys := by
rw [hkeyw]; exact List.mem_append.mpr (Or.inr hk_in_F)
refine ⟨dbw, hcompw, hk_in_dbw, ?_⟩
intro hcomp'
have herase_keys : (dbw.erase k).keys = res.keys ++ dbF.keys.erase k := by
rw [DB.erase_keys, hkeyw, List.erase_append_right _ hk_not_res]
obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp'
have hcount_erase : (dbw.erase k).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
res.keys.length + clampNonposD r := by
rw [herase_keys, List.countP_append]
have h1 : res.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = res.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_E
have hFall : dbF.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = dbF.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact hFend x hx
have hktrue : (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) k = true := by
rw [decide_eq_true_eq]; exact hkE
have h2 : (dbF.keys.erase k).countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = clampNonposD r := by
rw [List.countP_erase, ite_eq_left ⟨hk_in_F, hktrue⟩, hFall, hFlen]
omega
rw [h1, h2]
have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by
rw [clampNonposD_eq]
have hnonneg : 0 ≤ -r.end_.offset := by omega
rw [Int.toNat_of_nonneg hnonneg]; ring
have hanchor_erase : r.end_.anchor (dbw.erase k) = (res.keys.length : ℤ) - 1 := by
cases hE : r.end_.key with
| top => exact absurd hE (ne_of_lt hEne)
| coe e =>
rw [anchor_of_coe (s := r.end_) (v := e) hE]
have hce : (dbw.erase k).keys.countP (fun x => decide (x < e)) =
res.keys.length + clampNonposD r := by
rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE]
exact hcount_erase
rw [hce, hoff]
push_cast
ring
have ht_pos : 0 < res.keys.length := List.length_pos_iff.mpr hne
have hresolve_end : resolve (dbw.erase k) r.end_ =
((res.keys.getLast hne : Key) : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanchor_erase]; omega
have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by
rw [herase_keys, List.length_append, List.length_erase, ite_eq_left hk_in_F, hFlen]
omega
have hlt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by
rw [hanchor_erase, hlenErase]
omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by
rw [hanchor_erase]
have : ((res.keys.length : ℤ) - 1).toNat = res.keys.length - 1 := by omega
exact this
have hget : (dbw.erase k).keys[(r.end_.anchor (dbw.erase k)).toNat] =
res.keys.getLast hne := by
simp only [hidx, herase_keys]
have hlen : res.keys.length - 1 < res.keys.length := by omega
rw [List.getElem_append_left (i := res.keys.length - 1) (h := hlen)]
exact (List.getLast_eq_getElem hne).symm
rw [hget]
have hanchor_eraseB : r.begin.anchor (dbw.erase k) < 0 := by
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin)]
have hcountB : (dbw.erase k).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) =
(dbw.erase k).keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
rw [herase_keys] at hx
rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr
((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B
· exact lt_of_lt_of_le (hFend x (List.mem_of_mem_erase hx)) (le_of_lt hEB)
have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by
rw [herase_keys, List.length_append, List.length_erase, ite_eq_left hk_in_F, hFlen]
omega
rw [hcountB, hlenErase]
have hcast : (res.keys.length + clampNonposD r : ℤ) ≤ (beginSlack r : ℤ) := by
have := hcount; omega
omega
have hresolve_begin_erase : resolve (dbw.erase k) r.begin = (([] : Key) : WithTop Key) :=
resolve_eq_nil hanchor_eraseB
have hlast_not_ck : res.keys.getLast hne ∉ RangeRead.completeKeys (dbw.erase k) r := by
rw [RangeRead.mem_completeKeys, hresolve_begin_erase, hresolve_end]
rintro ⟨-, -, hlt⟩
exact lt_irrefl _ hlt
have hlast_in_filt : res.keys.getLast hne ∈
(RangeRead.completeKeys (dbw.erase k) r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) := by
rw [hfilt']
exact List.getLast_mem hne
exact hlast_not_ck (List.mem_of_mem_filter hlast_in_filt)
/-- Split `n` stored keys between a lower interval of capacity `cap1` and an upper
interval of capacity `cap2`, keeping at least `D + 1` below. -/
theorem exists_split_below {n D : ℕ} {cap1 cap2 : ℕ∞}
(hD : D + 1 ≤ n) (hcap1 : (D + 1 : ℕ∞) ≤ cap1)
(h : (n : ℕ∞) ≤ cap1 + cap2) :
∃ fb fa : ℕ, fb + fa = n ∧ D + 1 ≤ fb ∧ (fb : ℕ∞) ≤ cap1 ∧ (fa : ℕ∞) ≤ cap2 := by
by_cases hC1 : cap1 = ⊤
· refine ⟨n, 0, by omega, hD, by rw [hC1]; exact le_top, bot_le⟩
by_cases hC2 : cap2 = ⊤
· refine ⟨D + 1, n - (D + 1), by omega, by omega, hcap1, ?_⟩
rw [hC2]; exact le_top
obtain ⟨b, hb⟩ : ∃ b : ℕ, cap1 = (b : ℕ∞) := ⟨cap1.toNat, (ENat.natCast_toNat hC1).symm⟩
obtain ⟨c, hc⟩ : ∃ c : ℕ, cap2 = (c : ℕ∞) := ⟨cap2.toNat, (ENat.natCast_toNat hC2).symm⟩
rw [hb] at hcap1
have hcap1n : D + 1 ≤ b := by exact_mod_cast hcap1
rw [hb, hc, ← Nat.cast_add] at h
have hn : n ≤ b + c := by exact_mod_cast h
have hbc : n - c ≤ b := by rw [Nat.sub_le_iff_le_add]; omega
refine ⟨max (D + 1) (n - c), n - max (D + 1) (n - c), ?_, ?_, ?_, ?_⟩
· omega
· omega
· rw [hb]; exact_mod_cast max_le hcap1n hbc
· rw [hc]; exact_mod_cast (show n - max (D + 1) (n - c) ≤ c by omega)
/-- Split `n` stored keys between a lower interval of capacity `cap1` and an upper
interval of capacity `cap2`, keeping at least `D + 1` below and room for one avoided key
above. -/
theorem exists_split_below_avoid {n D : ℕ} {cap1 cap2 : ℕ∞}
(hD : D + 1 ≤ n) (hcap1 : (D + 1 : ℕ∞) ≤ cap1) (hcap2 : (1 : ℕ∞) ≤ cap2)
(h : ((n + 1 : ℕ) : ℕ∞) ≤ cap1 + cap2) :
∃ fb fa : ℕ, fb + fa = n ∧ D + 1 ≤ fb ∧ (fb : ℕ∞) ≤ cap1 ∧
((fa + 1 : ℕ) : ℕ∞) ≤ cap2 := by
by_cases hC1 : cap1 = ⊤
· refine ⟨n, 0, by omega, hD, by rw [hC1]; exact le_top, by simpa using hcap2⟩
by_cases hC2 : cap2 = ⊤
· refine ⟨D + 1, n - (D + 1), by omega, by omega, hcap1, ?_⟩
rw [hC2]; exact le_top
obtain ⟨b, hb⟩ : ∃ b : ℕ, cap1 = (b : ℕ∞) := ⟨cap1.toNat, (ENat.natCast_toNat hC1).symm⟩
obtain ⟨c, hc⟩ : ∃ c : ℕ, cap2 = (c : ℕ∞) := ⟨cap2.toNat, (ENat.natCast_toNat hC2).symm⟩
rw [hb] at hcap1
rw [hc] at hcap2
have hcap1n : D + 1 ≤ b := by exact_mod_cast hcap1
have hcap2pos : 1 ≤ c := by exact_mod_cast hcap2
rw [hb, hc, ← Nat.cast_add] at h
have hn : n + 1 ≤ b + c := by exact_mod_cast h
have hbc : n + 1 - c ≤ b := by rw [Nat.sub_le_iff_le_add]; omega
refine ⟨max (D + 1) (n + 1 - c), n - max (D + 1) (n + 1 - c), ?_, ?_, ?_, ?_⟩
· omega
· omega
· rw [hb]; exact_mod_cast max_le hcap1n hbc
· rw [hc]; exact_mod_cast (show n - max (D + 1) (n + 1 - c) + 1 ≤ c by omega)
/-- Split `n` stored keys between a lower interval of capacity `cap1` and an upper
interval of capacity `cap2`, keeping at least `D + 1` below and at least one above. -/
theorem exists_split_below_pos {n D : ℕ} {cap1 cap2 : ℕ∞}
(hD : D + 2 ≤ n) (hcap1 : ((D + 1 : ℕ) : ℕ∞) ≤ cap1) (hcap2 : (1 : ℕ∞) ≤ cap2)
(h : (n : ℕ∞) ≤ cap1 + cap2) :
∃ fb fa : ℕ, fb + fa = n ∧ D + 1 ≤ fb ∧ (fb : ℕ∞) ≤ cap1 ∧ 1 ≤ fa ∧
(fa : ℕ∞) ≤ cap2 := by
by_cases hC1 : cap1 = ⊤
· refine ⟨n - 1, 1, by omega, by omega, by rw [hC1]; exact le_top, by omega, ?_⟩
simpa using hcap2
by_cases hC2 : cap2 = ⊤
· refine ⟨D + 1, n - (D + 1), by omega, by omega, hcap1, by omega, by rw [hC2]; exact le_top⟩
obtain ⟨b, hb⟩ : ∃ b : ℕ, cap1 = (b : ℕ∞) := ⟨cap1.toNat, (ENat.natCast_toNat hC1).symm⟩
obtain ⟨c, hc⟩ : ∃ c : ℕ, cap2 = (c : ℕ∞) := ⟨cap2.toNat, (ENat.natCast_toNat hC2).symm⟩
rw [hb] at hcap1
rw [hc] at hcap2
have hcap1n : D + 1 ≤ b := by exact_mod_cast hcap1
have hcap2pos : 1 ≤ c := by exact_mod_cast hcap2
rw [hb, hc, ← Nat.cast_add] at h
have hn : n ≤ b + c := by exact_mod_cast h
refine ⟨max (D + 1) (n - c), n - max (D + 1) (n - c), ?_, ?_, ?_, ?_, ?_⟩
· omega
· omega
· rw [hb]; exact_mod_cast (max_le hcap1n (by omega))
· omega
· rw [hc]; exact_mod_cast (show n - max (D + 1) (n - c) ≤ c by omega)
/-- A key in `[end_.key, begin.key)` has an erase witness in the clamping subcell when
the reported head is nonempty. The witness stores the empty key below the reported head,
so erasing the target drops the begin anchor from one to zero, and the empty key enters
the truncated window. -/
theorem exists_witness_gap_ne_head_A2aBottom {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res)
(hhi : negCrossTruncNonposGapA2aBottomHigh r res)
(hne : res.keys ≠ []) (hhead : res.keys.head hne ≠ [])
(hkE : r.end_.key ≤ (k : WithTop Key)) (hkB : (k : WithTop Key) < r.begin.key) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧
¬ RangeRead.Compatible (dbw.erase k) r res := by
obtain ⟨hcell0, hEB, hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, -⟩ := hcell0
obtain ⟨-, hcond⟩ := hhi
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
set last : Key := res.keys.getLast hne with hlastdef
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlastdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hld : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [hlastD]
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hle_last : ∀ a ∈ res.keys, a ≤ last := by
intro a ha
have := (hsort.pairwise.imp le_of_lt).rel_getLast ha
rwa [← hlastdef] at this
have hlast_lt_B : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB
have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by rwa [hlastD] at hlastE
have hleast : leastReported r res = res.keys.head hne := by
simp [leastReported, hrev, List.head?_eq_some_head hne]
have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
rwa [hleast] at hcross
have hnil_head : ([] : Key) < res.keys.head hne :=
lt_of_le_of_ne (nil_le_key _) (fun h => hhead h.symm)
have hnil_lt_head : ((([] : Key) : WithTop Key)) < ((res.keys.head hne : Key) : WithTop Key) :=
WithTop.coe_lt_coe.mpr hnil_head
have hnil_lt_B : ((([] : Key) : WithTop Key)) < r.begin.key := lt_trans hnil_lt_head hhead_lt_B
have hnil_lt_E : ((([] : Key) : WithTop Key)) < r.end_.key :=
lt_trans hnil_lt_head
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last _ (List.head_mem hne))) hlast_lt_E)
-- capacity condition, with `if` reduced by `hhead`
have hcond' : ((beginSlack r + 1 : ℕ) : ℕ∞) ≤ (res.keys.length : ℕ∞) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
have := hcond
rw [hheadD, ite_eq_right hhead, Nat.add_zero, hlastD] at this
exact this
-- `t + (D+1) ≤ q`
have hBE0 : intervalCard r.begin.key r.end_.key = 0 := intervalCard_eq_zero_of_le (le_of_lt hEB)
have hrep : reportedBelowBegin r res = res.keys.length :=
reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB
have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
have h := hbot
unfold clampNonposBottom at h
rw [hBE0, hrep, tsub_zero] at h
exact_mod_cast h
set t : ℕ := res.keys.length with ht
set Dv : ℕ := clampNonposD r with hDv
set q : ℕ := beginSlack r with hq
set Fcap : ℕ∞ := intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key with hFcap
set Gcap : ℕ∞ := intervalCard r.end_.key r.begin.key with hGcap
have hD2 : Dv + 2 ≤ q + 1 - t := by omega
have hFcapge : ((Dv + 1 : ℕ) : ℕ∞) ≤ Fcap := by
have := intervalCard_keyAfter_getLast_ge hcomp₀ hne hend
rw [← hDv, ← hlastdef] at this
exact this
have hGcapge : (1 : ℕ∞) ≤ Gcap := one_le_intervalCard_of_le_lt hkE hkB
have hcadd : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = Fcap + Gcap := by
have hkaE : ((keyAfter last : Key) : WithTop Key) ≤ r.end_.key := keyAfter_le_of_lt hlast_lt_E
have h := intervalCard_add (b := ((keyAfter last : Key) : WithTop Key)) (m := r.end_.key)
(e := r.begin.key) hkaE (le_of_lt hEB)
rw [← hFcap, ← hGcap] at h
exact h
have hn_le : ((q + 1 - t : ℕ) : ℕ∞) ≤ Fcap + Gcap := by
have hq1 : t ≤ q + 1 := by omega
have hmain : (q + 1 : ℕ∞) ≤ (t : ℕ∞) + (Fcap + Gcap) := by
have := hcond'
rwa [ht, hq, hcadd] at this
have hnsub : ((q + 1 - t : ℕ) : ℕ∞) = (q + 1 : ℕ∞) - (t : ℕ∞) :=
ENat.natCast_sub (q + 1) t
rw [hnsub, tsub_le_iff_left]
simpa [add_comm] using hmain
-- split the required extra keys between fillers and gap keys
obtain ⟨fb, fa, hfb_fa, hfbD, hfbF, hfa1, hfaG⟩ :=
exists_split_below_pos (n := q + 1 - t) (D := Dv) (cap1 := Fcap) (cap2 := Gcap)
hD2 hFcapge hGcapge hn_le
-- filler keys in `(last, end_.key)`
obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn
(B := ((keyAfter last : Key) : WithTop Key)) (E := r.end_.key) (m := fb) hfbF
have hFlt_last : ∀ x ∈ dbF.keys, last < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (hFmem x hx).1)
have hFlt_E : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2
-- gap keys in `[end_.key, begin.key)` including `k`
have hfacap : (((fa - 1 + 1 : ℕ)) : ℕ∞) ≤ Gcap := by
rw [Nat.sub_add_cancel hfa1]; exact hfaG
obtain ⟨dbB, hBmem, hBk, hBlen⟩ := exists_db_keysIn_avoid
(B := r.end_.key) (E := r.begin.key) (k := k) (m := fa - 1) hfacap
have hGge_E : ∀ x ∈ dbB.keys, r.end_.key ≤ (x : WithTop Key) := fun x hx => (hBmem x hx).1
have hGlt_B : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.begin.key := fun x hx => (hBmem x hx).2
let dbG : DB := dbB.insert k hBk
have hGlen : dbG.keys.length = fa := by
rw [DB.insert_keys, List.orderedInsert_length, hBlen]; omega
have hGmem : ∀ x ∈ dbG.keys, r.end_.key ≤ (x : WithTop Key) ∧ (x : WithTop Key) < r.begin.key :=
by
intro x hx
rw [DB.mem_insert_iff hBk] at hx
rcases hx with rfl | hx
· exact ⟨hkE, hkB⟩
· exact ⟨hGge_E x hx, hGlt_B x hx⟩
have hkG : k ∈ dbG.keys := by rw [DB.mem_insert_iff hBk]; exact Or.inl rfl
have hGlast : ∀ b ∈ dbG.keys, last < b := fun b hb =>
WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E (hGmem b hb).1)
-- assemble the witness database
have hres_lt_F : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys, a < b := by
intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha) (hFlt_last b hb)
have hres_lt_G : ∀ a ∈ res.keys, ∀ b ∈ dbG.keys, a < b := by
intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha) (hGlast b hb)
have hres_lt_FG : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys ++ dbG.keys, a < b := by
intro a ha b hb
rcases List.mem_append.mp hb with hb | hb
· exact hres_lt_F a ha b hb
· exact hres_lt_G a ha b hb
have hF_lt_G : ∀ a ∈ dbF.keys, ∀ b ∈ dbG.keys, a < b := by
intro a ha b hb
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hFlt_E a ha) (hGmem b hb).1)
have hFG_sorted : (dbF.keys ++ dbG.keys).SortedLT :=
List.sortedLT_append.mpr ⟨dbF.sorted, dbG.sorted, hF_lt_G⟩
have hX_sorted : (res.keys ++ (dbF.keys ++ dbG.keys)).SortedLT :=
List.sortedLT_append.mpr ⟨hsort, hFG_sorted, hres_lt_FG⟩
have hnilX : ∀ b ∈ res.keys ++ (dbF.keys ++ dbG.keys), ([] : Key) < b := by
intro b hb
rcases List.mem_append.mp hb with hb | hb
· exact lt_of_lt_of_le hnil_head
(hsort.pairwise.imp le_of_lt |>.rel_head hb)
· rcases List.mem_append.mp hb with hb | hb
· exact lt_trans hnil_head (lt_of_le_of_lt (hle_last _ (List.head_mem hne)) (hFlt_last b hb))
· exact lt_trans hnil_head (lt_of_le_of_lt (hle_last _ (List.head_mem hne)) (hGlast b hb))
have hD_sorted : (([] : Key) :: (res.keys ++ (dbF.keys ++ dbG.keys))).SortedLT :=
List.sortedLT_cons.mpr ⟨hnilX, hX_sorted⟩
let dbw : DB := ⟨([] : Key) :: (res.keys ++ (dbF.keys ++ dbG.keys)), hD_sorted⟩
have hDkeys : dbw.keys = ([] : Key) :: (res.keys ++ (dbF.keys ++ dbG.keys)) := rfl
have hDlen : dbw.keys.length = q + 2 := by
rw [hDkeys, List.length_cons, List.length_append, List.length_append, hFlen, hGlen, ← ht]
omega
have hcountAll : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) =
dbw.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
rw [hDkeys] at hx
simp only [List.mem_cons] at hx
rcases hx with rfl | hx
· exact hnil_lt_B
· rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B
· rcases List.mem_append.mp hx with hx | hx
· exact lt_of_lt_of_le (hFlt_E x hx) (le_of_lt hEB)
· exact (hGmem x hx).2
have hanchorB : r.begin.anchor dbw = 1 := by
rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountAll, hDlen, ← hq]
push_cast; ring
have hresolveB : resolve dbw r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor dbw := by rw [hanchorB]; norm_num
have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by rw [hanchorB, hDlen]; norm_num
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.begin.anchor dbw).toNat = 1 := by rw [hanchorB]; rfl
simp only [hidx]
have hget : dbw.keys[1]'(by rw [hDlen]; norm_num) = res.keys.head hne := by
rw [List.getElem_of_eq hDkeys]
rw [List.getElem_cons_succ]
rw [List.getElem_append_left (i := 0) (h := List.length_pos_iff.mpr hne)]
exact (List.head_eq_getElem_zero hne).symm
exact congrArg (fun x : Key => (x : WithTop Key)) hget
-- the end resolution lies above the reported last
have hcountE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
1 + t + fb := by
rw [hDkeys, List.countP_cons_of_pos (by rw [decide_eq_true_eq]; exact hnil_lt_E),
List.countP_append, List.countP_append]
have h1 : res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = t := by
rw [ht, List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E
have h2 : dbF.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = fb := by
rw [← hFlen, List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact hFlt_E x hx
have h3 : dbG.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by
rw [List.countP_eq_zero]
intro x hx
rw [decide_eq_true_eq]
exact not_lt.mpr (hGmem x hx).1
rw [h1, h2, h3]; omega
have hanchorE : r.end_.anchor dbw = (t + fb : ℤ) - Dv := by
unfold Selector.anchor
rw [hcountE]
have hoff : r.end_.offset = -(Dv : ℤ) := by
rw [hDv, clampNonposD_eq]
have hnn : 0 ≤ -r.end_.offset := by omega
rw [Int.toNat_of_nonneg hnn]; ring
rw [hoff]; push_cast; ring
have hanchorE_nonneg : 0 ≤ r.end_.anchor dbw := by
rw [hanchorE]
have : (Dv : ℤ) ≤ t + fb := by
have : Dv ≤ fb := by omega
omega
omega
have hidxE : (r.end_.anchor dbw).toNat = t + fb - Dv := by
rw [hanchorE]
have : ((t + fb : ℤ) - Dv).toNat = t + fb - Dv := by omega
exact this
have hidxE_lt : (r.end_.anchor dbw).toNat < dbw.keys.length := by
rw [hidxE, hDlen]
have : Dv ≤ fb := by omega
omega
have hlast_eq_D : dbw.keys[t]'(by rw [hDlen]; omega) = last := by
rw [List.getElem_of_eq hDkeys, List.getElem_cons]
rw [dite_eq_right (by have : 0 < t := List.length_pos_iff.mpr hne; omega)]
rw [List.getElem_append_left (i := t - 1) (h := by
have : 0 < t := List.length_pos_iff.mpr hne
omega)]
rw [hlastdef, List.getLast_eq_getElem hne]
have hresolveE_gt_last : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by
rw [resolve_eq_getElem hanchorE_nonneg hidxE_lt]
rw [← hlast_eq_D]
have hlt : t < (r.end_.anchor dbw).toNat := by
rw [hidxE]
have : Dv ≤ fb := by omega
omega
exact WithTop.coe_lt_coe.mpr
(dbw.sorted.getElem_lt_getElem_of_lt (i := t) (j := (r.end_.anchor dbw).toNat)
(hi := by rw [hDlen]; omega) (hj := hidxE_lt) hlt)
-- the stored keys in `[head, last]` are the reported keys
have hfilt_between : dbw.keys.filter (fun x : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
(x : WithTop Key) ≤ ((last : Key) : WithTop Key))) = res.keys := by
apply List.SortedLT.eq_of_mem_iff
· exact List.sortedLT_iff_pairwise.mpr (dbw.sorted.pairwise.filter _)
· exact hsort
· intro x
rw [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨hx, hge, hle⟩
rw [hDkeys] at hx
simp only [List.mem_cons] at hx
rcases hx with rfl | hx
· exact absurd hge (not_le.mpr hnil_lt_head)
· rcases List.mem_append.mp hx with hx | hx
· exact hx
· rcases List.mem_append.mp hx with hx | hx
· exact absurd hle (not_le.mpr (WithTop.coe_lt_coe.mpr (hFlt_last x hx)))
· exact absurd hle (not_le.mpr (lt_of_lt_of_le hlast_lt_E (hGmem x hx).1))
· intro hx
refine ⟨?_, ?_, WithTop.coe_le_coe.mpr (hle_last x hx)⟩
· rw [hDkeys]; exact List.mem_cons_of_mem _ (List.mem_append_left _ hx)
· exact WithTop.coe_le_coe.mpr (hsort.pairwise.imp le_of_lt |>.rel_head hx)
-- compatibility
have hfilt_eq : (RangeRead.completeKeys dbw r).filter
(fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) = res.keys := by
apply List.SortedLT.eq_of_mem_iff
· rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])]
exact List.sortedLT_iff_pairwise.mpr
((RangeRead.inRange_pairwise dbw _ _).filter _)
· exact hsort
· intro x
rw [List.mem_filter, RangeRead.mem_completeKeys, hresolveB, hlastD, decide_eq_true_eq]
constructor
· rintro ⟨⟨hx, hge, -⟩, hle⟩
have hb : x ∈ dbw.keys.filter (fun y : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (y : WithTop Key) ∧
(y : WithTop Key) ≤ ((last : Key) : WithTop Key))) := by
rw [List.mem_filter, decide_eq_true_eq]
exact ⟨hx, hge, WithTop.coe_le_coe.mpr hle⟩
rwa [hfilt_between] at hb
· intro hxres
have hb := hfilt_between ▸ hxres
rw [List.mem_filter, decide_eq_true_eq] at hb
obtain ⟨hx, hge, hleW⟩ := hb
exact ⟨⟨hx, hge, lt_of_le_of_lt hleW hresolveE_gt_last⟩, WithTop.coe_le_coe.mp hleW⟩
refine ⟨dbw, ?_, ?_, ?_⟩
· rw [compatible_iff_truncated_keys hrev hmore hne]
refine ⟨hlim, ?_⟩
rw [← hld]
exact hfilt_eq
· rw [hDkeys]; exact List.mem_cons_of_mem _ (List.mem_append_right _ (List.mem_append_right _
hkG))
· intro hcomp'
obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp'
have hknil : k ≠ ([] : Key) :=
ne_of_gt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hnil_lt_E hkE))
have hk_not_res : k ∉ res.keys := by
intro hk
exact absurd hkE (not_le.mpr
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk)) hlast_lt_E))
have hk_not_F : k ∉ dbF.keys := by
intro hk
exact absurd (hFlt_E k hk) (not_lt.mpr hkE)
have herase_keys : (dbw.erase k).keys =
([] : Key) :: (res.keys ++ (dbF.keys ++ dbB.keys)) := by
rw [DB.erase_keys, hDkeys]
rw [List.erase_cons_tail (by simp only [beq_iff_eq]; exact fun h => hknil h.symm)]
congr 1
rw [List.erase_append_right _ hk_not_res]
congr 1
rw [List.erase_append_right _ hk_not_F]
congr 1
have h := DB.erase_insert_keys dbB k hBk
simpa [dbG] using h
have hlenErase : (dbw.erase k).keys.length = q + 1 := by
rw [herase_keys, List.length_cons, List.length_append, List.length_append, hFlen,
hBlen, ← ht]
omega
have hcountB' : (dbw.erase k).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = q + 1 := by
rw [← hlenErase, List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
rw [herase_keys] at hx
simp only [List.mem_cons] at hx
rcases hx with rfl | hx
· exact hnil_lt_B
· rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B
· rcases List.mem_append.mp hx with hx | hx
· exact lt_of_lt_of_le (hFlt_E x hx) (le_of_lt hEB)
· exact hGlt_B x hx
have hanchorB' : r.begin.anchor (dbw.erase k) = 0 := by
rw [anchor_eq_count_sub_slack (dbw.erase k) r (le_of_lt hbegin), hcountB', ← hq]
push_cast; ring
have hresolveB' : resolve (dbw.erase k) r.begin = (([] : Key) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor (dbw.erase k) := by rw [hanchorB']
have hlt : (r.begin.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by
rw [hanchorB', hlenErase]; omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.begin.anchor (dbw.erase k)).toNat = 0 := by rw [hanchorB']; rfl
simp only [hidx, herase_keys, List.getElem_cons_zero]
have hfiltEnd : (dbw.erase k).keys.filter
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
dbw.keys.filter (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by
rw [DB.erase_keys, ← List.erase_filter]
exact List.erase_eq_self_iff.mpr (by
intro hk
rw [List.mem_filter] at hk
exact absurd (of_decide_eq_true hk.2) (not_lt.mpr hkE))
have hresolveE' : resolve (dbw.erase k) r.end_ = resolve dbw r.end_ :=
resolve_eq_of_keys_below (db₁ := dbw.erase k) (db₂ := dbw) (E := r.end_.key)
(s := r.end_) hend (le_refl _) hfiltEnd
have hnil_ck : ([] : Key) ∈ RangeRead.completeKeys (dbw.erase k) r := by
rw [RangeRead.mem_completeKeys]
refine ⟨by rw [herase_keys]; exact List.mem_cons_self .., ?_, ?_⟩
· rw [hresolveB']
· rw [hresolveE']
exact lt_of_le_of_lt (coe_nil_le_key_withTop last) hresolveE_gt_last
have hnil_filt : ([] : Key) ∈ (RangeRead.completeKeys (dbw.erase k) r).filter
(fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) := by
rw [List.mem_filter]
refine ⟨hnil_ck, ?_⟩
rw [decide_eq_true_eq, hlastD]
exact nil_le_key last
rw [hld] at hnil_filt
rw [hfilt'] at hnil_filt
have hnil_notin : ([] : Key) ∉ res.keys := by
intro hk
have hle : res.keys.head hne ≤ [] := (hsort.pairwise.imp le_of_lt).rel_head hk
exact absurd (lt_of_lt_of_le hnil_head hle) (lt_irrefl _)
exact hnil_notin hnil_filt
/-- A key in `[end_.key, begin.key)` has an insert witness in the clamping subcell when
the reported head is the empty key. The witness stores exactly `beginSlack` keys below the
begin key, so the begin resolution is the empty key; inserting the target raises the begin
anchor from zero to one, and the empty key leaves the truncated window. -/
theorem exists_witness_gap_eq_head_A2aBottom {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res)
(hhi : negCrossTruncNonposGapA2aBottomHigh r res)
(hne : res.keys ≠ []) (hhead : res.keys.head hne = [])
(hkE : r.end_.key ≤ (k : WithTop Key)) (hkB : (k : WithTop Key) < r.begin.key) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧
¬ RangeRead.Compatible (toggle dbw k) r res := by
obtain ⟨hcell0, hEB, hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, -⟩ := hcell0
obtain ⟨-, hcond⟩ := hhi
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
set last : Key := res.keys.getLast hne with hlastdef
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlastdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hld : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [hlastD]
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hle_last : ∀ a ∈ res.keys, a ≤ last := by
intro a ha
have := (hsort.pairwise.imp le_of_lt).rel_getLast ha
rwa [← hlastdef] at this
have hlast_lt_B : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB
have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by rwa [hlastD] at hlastE
have hnil_lt_E : ((([] : Key) : WithTop Key)) < r.end_.key := by
have hle : res.keys.head hne ≤ last :=
(hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)
rw [hhead] at hle
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_lt_E
-- capacity condition, with `if` reduced by `hhead`
have hcond' : (((beginSlack r + 1 + 1 : ℕ)) : ℕ∞) ≤ (res.keys.length : ℕ∞) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
have := hcond
rw [hheadD, hhead, hlastD] at this
simpa using this
-- `t + (D+1) ≤ q`
have hBE0 : intervalCard r.begin.key r.end_.key = 0 := intervalCard_eq_zero_of_le (le_of_lt hEB)
have hrep : reportedBelowBegin r res = res.keys.length :=
reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB
have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
have h := hbot
unfold clampNonposBottom at h
rw [hBE0, hrep, tsub_zero] at h
exact_mod_cast h
set t : ℕ := res.keys.length with ht
set Dv : ℕ := clampNonposD r with hDv
set q : ℕ := beginSlack r with hq
set Fcap : ℕ∞ := intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key with hFcap
set Gcap : ℕ∞ := intervalCard r.end_.key r.begin.key with hGcap
have hD2 : Dv + 1 ≤ q + 1 - t := by omega
have hFcapge : ((Dv + 1 : ℕ) : ℕ∞) ≤ Fcap := by
have := intervalCard_keyAfter_getLast_ge hcomp₀ hne hend
rw [← hDv, ← hlastdef] at this
exact this
have hGcapge : (1 : ℕ∞) ≤ Gcap := one_le_intervalCard_of_le_lt hkE hkB
have hcadd : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = Fcap + Gcap := by
have hkaE : ((keyAfter last : Key) : WithTop Key) ≤ r.end_.key := keyAfter_le_of_lt hlast_lt_E
have h := intervalCard_add (b := ((keyAfter last : Key) : WithTop Key)) (m := r.end_.key)
(e := r.begin.key) hkaE (le_of_lt hEB)
rw [← hFcap, ← hGcap] at h
exact h
have hmain : ((q + 2 : ℕ) : ℕ∞) ≤ (t : ℕ∞) + (Fcap + Gcap) := by
have := hcond'
rw [hcadd] at this
have hcast : ((q + 1 + 1 : ℕ) : ℕ∞) = ((q + 2 : ℕ) : ℕ∞) := rfl
rw [hcast] at this
exact this
have hn_le : ((q + 1 - t + 1 : ℕ) : ℕ∞) ≤ Fcap + Gcap := by
have hsub : ((q + 1 - t + 1 : ℕ) : ℕ∞) = ((q + 2 : ℕ) : ℕ∞) - (t : ℕ∞) := by
rw [show q + 1 - t + 1 = q + 2 - t by omega]
exact ENat.natCast_sub (q + 2) t
rw [hsub, tsub_le_iff_left]
simpa using hmain
-- split the extra keys between fillers and gap keys, with room for the avoided target
obtain ⟨fb, fa, hfb_fa, hfbD, hfbF, hfaG⟩ :=
exists_split_below_avoid (n := q + 1 - t) (D := Dv) (cap1 := Fcap) (cap2 := Gcap)
hD2 hFcapge hGcapge hn_le
-- filler keys in `(last, end_.key)`
obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn
(B := ((keyAfter last : Key) : WithTop Key)) (E := r.end_.key) (m := fb) hfbF
have hFlt_last : ∀ x ∈ dbF.keys, last < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (hFmem x hx).1)
have hFlt_E : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2
-- gap keys in `[end_.key, begin.key)` avoiding the target
obtain ⟨dbB, hBmem, hBk, hBlen⟩ := exists_db_keysIn_avoid
(B := r.end_.key) (E := r.begin.key) (k := k) (m := fa) hfaG
have hGge_E : ∀ x ∈ dbB.keys, r.end_.key ≤ (x : WithTop Key) := fun x hx => (hBmem x hx).1
have hGlt_B : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.begin.key := fun x hx => (hBmem x hx).2
-- assemble the witness database
have hres_lt_F : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys, a < b := by
intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha) (hFlt_last b hb)
have hres_lt_G : ∀ a ∈ res.keys, ∀ b ∈ dbB.keys, a < b := by
intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha)
(WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E (hGge_E b hb)))
have hres_lt_FG : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys ++ dbB.keys, a < b := by
intro a ha b hb
rcases List.mem_append.mp hb with hb | hb
· exact hres_lt_F a ha b hb
· exact hres_lt_G a ha b hb
have hF_lt_G : ∀ a ∈ dbF.keys, ∀ b ∈ dbB.keys, a < b := by
intro a ha b hb
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hFlt_E a ha) (hGge_E b hb))
have hFG_sorted : (dbF.keys ++ dbB.keys).SortedLT :=
List.sortedLT_append.mpr ⟨dbF.sorted, dbB.sorted, hF_lt_G⟩
have hX_sorted : (res.keys ++ dbF.keys ++ dbB.keys).SortedLT := by
rw [List.append_assoc]
exact List.sortedLT_append.mpr ⟨hsort, hFG_sorted, hres_lt_FG⟩
let dbw : DB := ⟨res.keys ++ dbF.keys ++ dbB.keys, hX_sorted⟩
have hDkeys : dbw.keys = res.keys ++ dbF.keys ++ dbB.keys := rfl
have hDlen : dbw.keys.length = q + 1 := by
rw [hDkeys, List.length_append, List.length_append, hFlen, hBlen, ← ht]
omega
have hcountAll : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) =
dbw.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
rw [hDkeys] at hx
rcases List.mem_append.mp hx with hx | hx
· rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B
· exact lt_of_lt_of_le (hFlt_E x hx) (le_of_lt hEB)
· exact hGlt_B x hx
have hanchorB : r.begin.anchor dbw = 0 := by
rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountAll, hDlen, ← hq]
push_cast; ring
have hresolveB : resolve dbw r.begin = (([] : Key) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor dbw := by rw [hanchorB]
have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by
rw [hanchorB, hDlen]; omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.begin.anchor dbw).toNat = 0 := by rw [hanchorB]; rfl
simp only [hidx]
have hget : dbw.keys[0]'(by rw [hDlen]; omega) = ([] : Key) := by
rw [List.getElem_of_eq hDkeys]
rw [List.getElem_append_left (i := 0) (h := by
rw [List.length_append]; omega)]
rw [List.getElem_append_left (i := 0) (h := List.length_pos_iff.mpr hne)]
exact (List.head_eq_getElem_zero hne).symm.trans hhead
exact congrArg (fun x : Key => (x : WithTop Key)) hget
-- the end resolution lies above the reported last
have hcountE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
t + fb := by
rw [hDkeys, List.countP_append, List.countP_append]
have h1 : res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = t := by
rw [ht, List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E
have h2 : dbF.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = fb := by
rw [← hFlen, List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact hFlt_E x hx
have h3 : dbB.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by
rw [List.countP_eq_zero]
intro x hx
rw [decide_eq_true_eq]
exact not_lt.mpr (hGge_E x hx)
rw [h1, h2, h3]; omega
have hanchorE : r.end_.anchor dbw = (t + fb : ℤ) - Dv - 1 := by
unfold Selector.anchor
rw [hcountE]
have hoff : r.end_.offset = -(Dv : ℤ) := by
rw [hDv, clampNonposD_eq]
have hnn : 0 ≤ -r.end_.offset := by omega
rw [Int.toNat_of_nonneg hnn]; ring
rw [hoff]; push_cast; ring
have hanchorE_nonneg : 0 ≤ r.end_.anchor dbw := by
rw [hanchorE]
have : Dv + 1 ≤ fb := hfbD
omega
have hidxE : (r.end_.anchor dbw).toNat = t + fb - Dv - 1 := by
rw [hanchorE]
have : ((t + fb : ℤ) - Dv - 1).toNat = t + fb - Dv - 1 := by omega
exact this
have hidxE_lt : (r.end_.anchor dbw).toNat < dbw.keys.length := by
rw [hidxE, hDlen]
have : Dv + 1 ≤ fb := hfbD
omega
have ht_pos : 0 < t := by rw [ht]; exact List.length_pos_iff.mpr hne
have hlast_eq_D : dbw.keys[t - 1]'(by rw [hDlen]; omega) = last := by
rw [List.getElem_of_eq hDkeys]
rw [List.getElem_append_left (i := t - 1) (h := by
rw [List.length_append]; omega)]
rw [List.getElem_append_left (i := t - 1) (h := by omega)]
rw [hlastdef, List.getLast_eq_getElem hne]
have hresolveE_gt_last : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by
rw [resolve_eq_getElem hanchorE_nonneg hidxE_lt]
rw [← hlast_eq_D]
have hlt : t - 1 < (r.end_.anchor dbw).toNat := by
rw [hidxE]
have : Dv + 1 ≤ fb := hfbD
omega
exact WithTop.coe_lt_coe.mpr
(dbw.sorted.getElem_lt_getElem_of_lt (i := t - 1) (j := (r.end_.anchor dbw).toNat)
(hi := by rw [hDlen]; omega) (hj := hidxE_lt) hlt)
-- the stored keys below the end resolution are the reported keys
have hfilt_eq : (RangeRead.completeKeys dbw r).filter
(fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) = res.keys := by
apply List.SortedLT.eq_of_mem_iff
· rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])]
exact List.sortedLT_iff_pairwise.mpr
((RangeRead.inRange_pairwise dbw _ _).filter _)
· exact hsort
· intro x
rw [List.mem_filter, RangeRead.mem_completeKeys, hresolveB, hlastD, decide_eq_true_eq]
constructor
· rintro ⟨⟨hx, -, -⟩, hle⟩
rw [hDkeys] at hx
rcases List.mem_append.mp hx with hx | hx
· rcases List.mem_append.mp hx with hx | hx
· exact hx
· exact absurd hle (not_le.mpr (hFlt_last x hx))
· exact absurd hle (not_le.mpr
(WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E (hGge_E x hx))))
· intro hx
refine ⟨⟨?_, ?_, ?_⟩, hle_last x hx⟩
· rw [hDkeys]; exact List.mem_append_left _ (List.mem_append_left _ hx)
· exact coe_nil_le_key_withTop (x : WithTop Key)
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hresolveE_gt_last
have hcompw : RangeRead.Compatible dbw r res := by
rw [compatible_iff_truncated_keys hrev hmore hne]
refine ⟨hlim, ?_⟩
rw [← hld]
exact hfilt_eq
-- the target is not among the stored keys
have hk_not_res : k ∉ res.keys := by
intro hk
exact absurd hkE (not_le.mpr
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk)) hlast_lt_E))
have hk_not_F : k ∉ dbF.keys := by
intro hk
exact absurd (hFlt_E k hk) (not_lt.mpr hkE)
have hk_not_dbw : k ∉ dbw.keys := by
rw [hDkeys]
intro hk
rcases List.mem_append.mp hk with hk | hk
· rcases List.mem_append.mp hk with hk | hk
· exact hk_not_res hk
· exact hk_not_F hk
· exact hBk hk
refine ⟨dbw, hcompw, ?_⟩
rw [toggle_eq_insert_of_not_mem hk_not_dbw]
intro hcomp'
obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp'
-- the inserted database has one more key below the begin key
have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = q + 2 := by
rw [DB.insert_keys, orderedInsert_eq_insertIdx dbw.sorted hk_not_dbw]
rw [countP_insertIdx_of_lt (l := dbw.keys) (x := k) (v := r.begin.key) dbw.sorted hkB,
hcountAll, hDlen]
have hanchorB_ins : r.begin.anchor (dbw.insert k hk_not_dbw) = 1 := by
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins, ← hq]
push_cast; ring
-- the empty key leaves the begin resolution
have hnil_mem : ([] : Key) ∈ dbw.keys := by
rw [hDkeys]
exact List.mem_append_left _ (List.mem_append_left _ ((hhead ▸ List.head_mem hne : _)))
have hins_len : (dbw.insert k hk_not_dbw).keys.length = q + 2 := by
rw [DB.insert_keys, List.orderedInsert_length, hDlen]
have hnil_lt_one : ((([] : Key) : WithTop Key)) <
(((dbw.insert k hk_not_dbw).keys[1]'(by rw [hins_len]; omega) : Key) : WithTop Key) := by
have hlt := (dbw.insert k hk_not_dbw).sorted.getElem_lt_getElem_of_lt (i := 0) (j := 1)
(hi := by rw [hins_len]; omega) (hj := by rw [hins_len]; omega)
(by omega)
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (nil_le_key _))
(WithTop.coe_lt_coe.mpr hlt)
have hresolveB_ins : ((([] : Key) : WithTop Key)) <
resolve (dbw.insert k hk_not_dbw) r.begin := by
have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by rw [hanchorB_ins]; norm_num
have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat <
(dbw.insert k hk_not_dbw).keys.length := by
rw [hanchorB_ins, hins_len]; omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat = 1 := by
rw [hanchorB_ins]; rfl
simp only [hidx]
exact hnil_lt_one
-- the empty key is reported, so it must survive the truncated filter
have hnil_res : ([] : Key) ∈ res.keys := hhead ▸ List.head_mem hne
have hnil_filt : ([] : Key) ∈ (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter
(fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) := by
rw [hld, hfilt']; exact hnil_res
rw [List.mem_filter] at hnil_filt
have hge := (RangeRead.mem_completeKeys.mp hnil_filt.1).2.1
exact absurd hresolveB_ins (not_lt.mpr hge)
/-- In the crossed nonpositive-end gap cell a clamping begin selector leaves the stored
keys below the end key equal to the reported keys together with the stored keys in
`(last, end_.key)`. -/
theorem cnt_end_eq_of_clamp_A2aBottom {db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = true) (hne : res.keys ≠ [])
(hend : r.end_.offset ≤ 0) (hL : resolve db r.begin = (([] : Key) : WithTop Key)) :
cnt db r.end_.key = res.keys.length +
(between db (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key).length := by
classical
set last : Key := res.keys.getLast hne with hlast
have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne
have hcmpEnd : resolve db r.end_ ≤ r.end_.key :=
resolve_le_key_of_offset_nonpos (db := db) (s := r.end_) hend
have hwin : RangeRead.completeKeys db r =
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by
rw [completeKeys_eq_filter_of_end_nonpos hrev hend, hL, List.filter_filter]
apply List.filter_congr
intro k _
by_cases h1 : (((k : Key) : WithTop Key) < resolve db r.end_)
· have h2 : (((k : Key) : WithTop Key) < r.end_.key) := lt_of_lt_of_le h1 hcmpEnd
have h3 : ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) :=
coe_nil_le_key_withTop _
simp [h1, h2, h3]
· simp [h1]
have hfil : (RangeRead.completeKeys db r).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
rw [hlast]
exact ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2
have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := by
have hmem : last ∈ (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) := by
rw [hfil]; exact hlast_mem
exact (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hmem).1).2.2
have hka_le_re : (((keyAfter last : Key)) : WithTop Key) ≤ resolve db r.end_ :=
keyAfter_le_of_lt hlast_lt_re
have hcntKa : cnt db (((keyAfter last : Key)) : WithTop Key) = res.keys.length := by
rw [cnt, List.countP_eq_length_filter]
have hl : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
(((keyAfter last : Key)) : WithTop Key))) =
(RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) := by
rw [hwin, List.filter_filter]
apply List.filter_congr
intro k _
by_cases h1 : (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key))
· have hkle : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h1)
have h2 : (((k : Key) : WithTop Key) < resolve db r.end_) :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re
simp [h1, h2, hkle]
· have h2 : ¬ ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := by
rintro ⟨hkre, hkle⟩
exact h1 (WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle))
by_cases hA : (((k : Key) : WithTop Key) < resolve db r.end_)
· have hB : ¬ k ≤ last := fun hB => h2 ⟨hA, hB⟩
simp [h1, hA, hB]
· simp [h1, hA]
rw [hl, hfil]
have hkaE : (((keyAfter last : Key)) : WithTop Key) ≤ r.end_.key :=
keyAfter_le_of_lt (lt_of_lt_of_le hlast_lt_re hcmpEnd)
have hsplit := cnt_eq_add_between db hkaE
rw [hcntKa] at hsplit
exact hsplit
/-- **Gap bound at a nonnegative begin anchor.** In the crossed nonpositive-end gap
cell, a compatible database with nonnegative begin anchor resolves the begin selector
onto the reported head, and the slack is bounded by the reported count plus the capacity
of the gap above the reported last. -/
theorem gap_slack_bound_A2aBottom {db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key)
(hanc : 0 ≤ r.begin.anchor db) :
((beginSlack r + 1 : ℕ) : ℕ∞) ≤ (res.keys.length : ℕ∞) +
intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.begin.key := by
classical
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB
have hpre := RangeRead.compatible_keys_prefix hcomp
have hckne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne
obtain ⟨a, _ha_mem, ha_res, ha_head⟩ :=
RangeRead.completeKeys_head_resolve hrev hckne hanc
have hhead_eq_a : res.keys.head hne = a := by
rw [← ha_head]; exact hpre.head hne
have hres : resolve db r.begin = ((head : Key) : WithTop Key) := by
rw [hhead, hhead_eq_a]; exact ha_res
have hhead_le_last : head ≤ last := by
rw [hhead, hlast]
exact (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_getLast
(List.head_mem hne)
have hheadB : ((head : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlastB'
-- the gap count is the slack
have hgap : db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 :=
gap_count_eq_beginSlack_of_resolve_head hbegin hheadB hanc hres
have hresEnd_gt : ((last : Key) : WithTop Key) < resolve db r.end_ := by
have hlast_ck : last ∈ RangeRead.completeKeys db r := hpre.subset (List.getLast_mem hne)
exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2
have hfilterComp : (RangeRead.completeKeys db r).filter
(fun k => decide (k ≤ last)) = res.keys :=
((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2
have hfilterBegin : (RangeRead.completeKeys db r).filter
(fun k => decide (k ≤ last)) =
db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) :=
completeKeys_filter_le_of_resolve_begin hrev hres hresEnd_gt
have hlen_head_last : (db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last))).length
= res.keys.length := by
rw [← hfilterBegin, hfilterComp]
have hfilter_ka : db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) =
db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) := by
apply List.filter_congr
intro k _
rw [decide_eq_decide]
by_cases hk : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)
· simp only [hk, true_and]
exact WithTop.coe_lt_coe.trans (lt_keyAfter_iff k last)
· simp only [hk, false_and]
have hlen_ka : (between db ((head : Key) : WithTop Key)
(((keyAfter last : Key) : WithTop Key))).length = res.keys.length := by
unfold between
rw [hfilter_ka, hlen_head_last]
have hcntB : cnt db r.begin.key =
cnt db ((head : Key) : WithTop Key) +
db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) := by
rw [cnt_eq_add_between db (le_of_lt hheadB), between, List.countP_eq_length_filter]
have hcntKa : cnt db (((keyAfter last : Key)) : WithTop Key) =
cnt db ((head : Key) : WithTop Key) +
(between db ((head : Key) : WithTop Key)
(((keyAfter last : Key)) : WithTop Key)).length :=
cnt_eq_add_between db (WithTop.coe_le_coe.mpr
(le_trans hhead_le_last (le_of_lt (keyAfter_self_lt last))))
have hcntKB : cnt db r.begin.key =
cnt db (((keyAfter last : Key)) : WithTop Key) +
(between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length :=
cnt_eq_add_between db (keyAfter_le_of_lt hlastB')
have hkey : db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) = res.keys.length +
(between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := by
rw [hcntKa, hlen_ka] at hcntKB
omega
have hkeyNat : beginSlack r + 1 =
res.keys.length +
(between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := by
rw [← hgap, hkey]
have hlen_le : (((between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length : ℕ) :
ℕ∞) ≤
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key :=
between_length_le_intervalCard (keyAfter_le_of_lt hlastB')
calc ((beginSlack r + 1 : ℕ) : ℕ∞)
= ((res.keys.length +
(between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length : ℕ) : ℕ∞) := by
rw [hkeyNat]
_ = (res.keys.length : ℕ∞) +
(((between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length : ℕ) : ℕ∞) := by
rw [Nat.cast_add]
_ ≤ (res.keys.length : ℕ∞) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key :=
add_le_add (le_refl (res.keys.length : ℕ∞)) hlen_le
/-- **Every compatible database clamps in the low branch.** In the crossed
nonpositive-end gap cell with a clamping begin selector, if the gap-capacity condition
fails then every compatible database resolves its begin selector to `⊥`. A nonnegative
anchor would force the gap count up to `beginSlack + 1`, hence the empty reported head,
which resolves to `⊥` anyway. -/
theorem resolve_begin_eq_nil_of_low_A2aBottom {r : RangeRead} {res : Result} {db : DB}
(hcell : negCrossTruncNonposGapA2aBottom r res)
(hlow : ¬ negCrossTruncNonposGapA2aBottomHigh r res)
(hcomp : RangeRead.Compatible db r res) :
resolve db r.begin = (([] : Key) : WithTop Key) := by
classical
have hcellKeep := hcell
obtain ⟨hcell0, hEB, _hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hlastD' : res.keys.getLastD ([] : Key) = last := by
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hnotRaw := hlow
unfold negCrossTruncNonposGapA2aBottomHigh at hnotRaw
have hnot' : ¬ (((beginSlack r + 1 +
(if res.keys.headD ([] : Key) = [] then 1 else 0) : ℕ) : ℕ∞)
≤ (res.keys.length : ℕ∞) +
intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)
r.begin.key) := by
intro h; exact hnotRaw ⟨hcellKeep, h⟩
rw [hlastD'] at hnot'
by_cases hanc : 0 ≤ r.begin.anchor db
· have hpre := RangeRead.compatible_keys_prefix hcomp
have hckne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne
obtain ⟨a, _ha_mem, ha_res, ha_head⟩ :=
RangeRead.completeKeys_head_resolve hrev hckne hanc
have hhead_eq_a : res.keys.head hne = a := by
rw [← ha_head]; exact hpre.head hne
have hres : resolve db r.begin = ((head : Key) : WithTop Key) := by
rw [hhead, hhead_eq_a]; exact ha_res
have hbound := gap_slack_bound_A2aBottom hcomp hrev hmore hne hbegin hlastB hanc
have hhead_nil : head = [] := by
by_contra hne_head
have hheadD_ne : res.keys.headD ([] : Key) ≠ [] := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]
exact hne_head
rw [ite_eq_right hheadD_ne] at hnot'
exact hnot' (by simpa using hbound)
rw [hres, hhead_nil]
· exact resolve_eq_nil (not_le.mp hanc)
/-- **Soundness of `[⊥, end_.key)` in the low branch.** If the gap-capacity condition
fails, every compatible database resolves its begin selector to `⊥`; both selectors are
then fixed by agreement below `end_.key`, so the truncated range `[⊥, end_.key)` is
sound. -/
theorem sound_bottomEnd_A2aBottom {r : RangeRead} {res : Result}
(hcell : negCrossTruncNonposGapA2aBottom r res)
(hlow : ¬ negCrossTruncNonposGapA2aBottomHigh r res) :
Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by
classical
have hcellKeep := hcell
obtain ⟨hcell0, hEB, _hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
set δ : ℕ := (if res.keys.headD ([] : Key) = [] then 1 else 0) with hδdef
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hlow' : ¬ (((beginSlack r + 1 + δ : ℕ) : ℕ∞)
≤ (res.keys.length : ℕ∞) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key) := by
have h := hlow
unfold negCrossTruncNonposGapA2aBottomHigh at h
rw [← hδdef] at h
rw [hlastD] at h
intro hineq
exact h ⟨hcellKeep, hineq⟩
have hδle : δ ≤ 1 := by rw [hδdef]; split <;> omega
have step : ∀ db1 db2, AgreeOn (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange)
db1 db2 → RangeRead.Compatible db1 r res → RangeRead.Compatible db2 r res := by
intro db1 db2 hagree hc1
have hfiltE : db1.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
db2.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by
have h := hagree
rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h
exact h
have hL1 : resolve db1 r.begin = (([] : Key) : WithTop Key) :=
resolve_begin_eq_nil_of_low_A2aBottom hcellKeep hlow hc1
have hcntE : cnt db2 r.end_.key = cnt db1 r.end_.key := by
rw [cnt, cnt, List.countP_eq_length_filter, List.countP_eq_length_filter]
exact (congrArg List.length hfiltE).symm
have hcnt1 : cnt db1 r.end_.key = res.keys.length +
(between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length :=
cnt_end_eq_of_clamp_A2aBottom hc1 hrev hmore hne hend hL1
have hb1 : (((between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length : ℕ) : ℕ∞)
≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key :=
between_length_le_intervalCard (keyAfter_le_of_lt hlast_lt_E)
have hsplit2 := cnt_eq_add_between db2 (le_of_lt hEB)
have hb2 : (((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞)
≤ intervalCard r.end_.key r.begin.key :=
between_length_le_intervalCard (le_of_lt hEB)
have hinterval : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key =
intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key +
intervalCard r.end_.key r.begin.key :=
intervalCard_add (keyAfter_le_of_lt hlast_lt_E) (le_of_lt hEB)
have hcnt2_bound : (cnt db2 r.begin.key : ℕ∞) ≤
(res.keys.length : ℕ∞) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
calc (cnt db2 r.begin.key : ℕ∞)
= (cnt db2 r.end_.key : ℕ∞) +
(((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by
rw [hsplit2]; push_cast; ring
_ = (cnt db1 r.end_.key : ℕ∞) +
(((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by rw [hcntE]
_ = ((res.keys.length +
(between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length : ℕ) : ℕ∞) +
(((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by rw [hcnt1]
_ = (res.keys.length : ℕ∞) +
(((between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length : ℕ) : ℕ∞) +
(((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by
push_cast; ring
_ ≤ (res.keys.length : ℕ∞) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key +
intervalCard r.end_.key r.begin.key :=
add_le_add (add_le_add (le_refl _) hb1) hb2
_ = (res.keys.length : ℕ∞) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
rw [hinterval]; ring
have hcnt_lt : (cnt db2 r.begin.key : ℕ∞) <
((beginSlack r + 1 + δ : ℕ) : ℕ∞) := lt_of_le_of_lt hcnt2_bound (not_le.mp hlow')
have hcnt_le : cnt db2 r.begin.key ≤ beginSlack r + δ := by
have := ENat.natCast_lt_natCast.mp hcnt_lt
omega
have hanchor_le : r.begin.anchor db2 ≤ 0 := by
rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)]
have h1 : (cnt db2 r.begin.key : ℤ) ≤ (beginSlack r : ℤ) + (δ : ℤ) := by
exact_mod_cast hcnt_le
have h2 : (δ : ℤ) ≤ 1 := by exact_mod_cast hδle
simp only [cnt] at h1 ⊢
omega
have hL2 : resolve db2 r.begin = (([] : Key) : WithTop Key) := by
rcases lt_or_eq_of_le hanchor_le with hneg | heq
· exact resolve_eq_nil hneg
· have hcnt_eq : cnt db2 r.begin.key = beginSlack r + 1 := by
rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] at heq
have h3 : (cnt db2 r.begin.key : ℤ) = (beginSlack r : ℤ) + 1 := by
simp only [cnt] at heq ⊢
omega
exact_mod_cast h3
have hδ1 : δ = 1 := by omega
have hheadD : res.keys.headD ([] : Key) = [] := by
rw [hδdef] at hδ1
by_contra h
rw [ite_eq_right h] at hδ1
omega
have hhead_nil : head = [] := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne] at hheadD
rw [← hhead] at hheadD
exact hheadD
have hnil_mem1 : ([] : Key) ∈ db1.keys :=
RangeRead.compatible_keys_subset hc1 (hhead_nil ▸ List.head_mem hne)
have hnil_lt_E : ((([] : Key)) : WithTop Key) < r.end_.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (nil_le_key last)) hlast_lt_E
have hnil_mem2 : ([] : Key) ∈ db2.keys := by
have hmem1 : ([] : Key) ∈ db1.keys.filter
(fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by
rw [List.mem_filter]; exact ⟨hnil_mem1, by simpa using hnil_lt_E⟩
rw [hfiltE] at hmem1
exact (List.mem_filter.mp hmem1).1
have hpos : 0 ≤ r.begin.anchor db2 := le_of_eq heq.symm
have hlt : (r.begin.anchor db2).toNat < db2.keys.length := by
rw [heq]; simp only [Int.toNat_zero]
exact List.length_pos_of_mem hnil_mem2
rw [resolve_eq_getElem hpos hlt]
have hidx : (r.begin.anchor db2).toNat = 0 := by rw [heq]; rfl
simp only [hidx]
simpa using keys_getElem_zero_eq_nil_of_mem hnil_mem2
have hR : resolve db1 r.end_ = resolve db2 r.end_ :=
resolve_eq_of_keys_below (db₁ := db1) (db₂ := db2) (E := r.end_.key) (s := r.end_)
hend le_rfl hfiltE
have hck : RangeRead.completeKeys db1 r = RangeRead.completeKeys db2 r := by
rw [completeKeys_eq_filter_of_end_nonpos hrev hend,
completeKeys_eq_filter_of_end_nonpos (db := db2) hrev hend, hL1, hL2, hR, hfiltE]
rcases hc1.2 with ⟨hmore', hpre, hne'⟩ | ⟨hmore', heq'⟩
· exact ⟨hc1.1, Or.inl ⟨hmore', by rw [← hck]; exact hpre, hne'⟩⟩
· exact ⟨hc1.1, Or.inr ⟨hmore', by rw [← hck]; exact heq'⟩⟩
intro db1 db2 hagree
exact ⟨step db1 db2 hagree, step db2 db1 hagree.symm⟩
/-- **Minimality of the clamping crossed nonpositive-end gap closed form.** On the
capacity-high branch the range is `[⊥, begin.key)` and on the low branch `[⊥, end_.key)`;
both are sound and every key below the respective upper endpoint is necessary. -/
theorem minimal_closedForm_of_negCross_trunc_nonposGap_A2aBottom
{r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) :
Minimal r res (closedFormNegCrossTruncNonposGapA2aBottom r res) := by
classical
have hcellKeep : negCrossTruncNonposGapA2aBottom r res := hcell
obtain ⟨hcell0, hEB, hbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0
have hle_begin : r.begin.offset ≤ 0 := le_of_lt hbegin
-- necessity below the end key, for both branches
have nec_below_end : ∀ k : Key, ((k : Key) : WithTop Key) < r.end_.key →
Necessary r res k := by
intro k hkE
by_cases hkres : k ∈ res.keys
· exact necessary_of_mem_result hread hkres
· by_cases hkhead : ((k : Key) : WithTop Key) <
((res.keys.head hne : Key) : WithTop Key)
· obtain ⟨dbw, hcompw, hnot⟩ :=
exists_witness_below_head_A2aBottom hread hcellKeep hne hkhead
exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩
· have hheadk : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) :=
not_lt.mp hkhead
by_cases hklast : ((k : Key) : WithTop Key) ≤
((res.keys.getLast hne : Key) : WithTop Key)
· exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne)
(WithTop.coe_le_coe.mp hheadk) (WithTop.coe_le_coe.mp hklast)
· have hlastk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) :=
not_le.mp hklast
obtain ⟨dbw, hcompw, hkdbw, hnot⟩ :=
exists_witness_last_end_A2aBottom hread hcellKeep hne hlastk hkE
exact necessary_of_toggle ⟨dbw, hcompw, by rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩
by_cases hhi : negCrossTruncNonposGapA2aBottomHigh r res
· have hclosed : closedFormNegCrossTruncNonposGapA2aBottom r res =
⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by
unfold closedFormNegCrossTruncNonposGapA2aBottom; rw [ite_eq_left hhi]
rw [hclosed]
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
· have hs := sound_bottomLong_of_nonpos (r := r) (res := res) hle_begin hend
have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hEB)
rwa [hmax] at hs
· intro k _ hkB
by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key
· exact nec_below_end k hkE
· have hEk : r.end_.key ≤ (k : WithTop Key) := not_lt.mp hkE
by_cases hhead : res.keys.head hne = []
· obtain ⟨dbw, hcompw, hnot⟩ :=
exists_witness_gap_eq_head_A2aBottom hread hcellKeep hhi hne hhead hEk hkB
exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩
· obtain ⟨dbw, hcompw, hkdbw, hnot⟩ :=
exists_witness_gap_ne_head_A2aBottom hread hcellKeep hhi hne hhead hEk hkB
exact necessary_of_toggle ⟨dbw, hcompw,
by rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩
· have hclosed : closedFormNegCrossTruncNonposGapA2aBottom r res =
⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by
unfold closedFormNegCrossTruncNonposGapA2aBottom; rw [ite_eq_right hhi]
rw [hclosed]
refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_bottomEnd_A2aBottom hcellKeep hhi, ?_⟩
intro k _ hkE
exact nec_below_end k hkE
/-! ## `clampLongUpper` is not the gap-cell upper endpoint
The complete-cell endpoint `clampLongUpper` compares the capacity of
`[end_.key, begin.key)` with the slack `C`. In the crossed nonpositive-end gap cell the
interval `(last, end_.key)` also contributes necessity capacity, so `clampLongUpper` can
select `end_.key` while the high-capacity condition holds and the minimal range is
`[⊥, begin.key)`. The witness below is kernel-checked: `beginSlack = 2`,
`clampNonposD = 0`, one reported key (the empty key), `clampLongCap = clampLongC = 1`,
the high condition true, and yet `clampLongUpper` picks `end_.key`. This is why
`compactForwardTruncatedNonpos` dispatches the crossed clamping branch on whether the
reported last lies below `end_.key`. -/
/-- The empty key. -/
def a2bNil : Key := []
/-- The key `[0]`. -/
def a2bZero : Key := [(0 : Fin 256)]
/-- The key `[0,0,0]`, the end selector key. -/
def a2bThree : Key := [(0 : Fin 256), (0 : Fin 256), (0 : Fin 256)]
/-- The key `[0,0,0,0]`, the begin selector key. -/
def a2bFour : Key := [(0 : Fin 256), (0 : Fin 256), (0 : Fin 256), (0 : Fin 256)]
/-- The witness database `{[], [0], [0,0,0]}`. -/
def a2bDB : DB := ⟨[a2bNil, a2bZero, a2bThree], by decide⟩
/-- The witness read: `begin = ⟨[0,0,0,0], -2⟩`, `end = ⟨[0,0,0], 0⟩`. -/
def a2bRead : RangeRead :=
{ begin := ⟨(a2bFour : Key), -2⟩, end_ := ⟨(a2bThree : Key), 0⟩,
limit := none, reverse := false }
/-- The witness result: reports the empty key, truncated. -/
def a2bRes : Result := { keys := [a2bNil], more := true }
/-- The witness database is compatible with the witness read and result. -/
theorem a2b_compatible : RangeRead.Compatible a2bDB a2bRead a2bRes :=
⟨(by unfold RangeRead.withinLimit; simp [a2bRead]),
Or.inl ⟨rfl, by decide, by decide⟩⟩
/-- The witness pair is readable. -/
theorem a2b_readable : Readable a2bRead a2bRes := ⟨a2bDB, a2b_compatible⟩
/-- The witness lies in the crossed nonpositive-end gap clamping cell. -/
theorem a2b_cell : negCrossTruncNonposGapA2aBottom a2bRead a2bRes := by
unfold negCrossTruncNonposGapA2aBottom negCrossTruncNonposGap
decide
/-- The witness satisfies the high-capacity condition, so its minimal range is
`[⊥, begin.key)`. -/
theorem a2b_high : negCrossTruncNonposGapA2aBottomHigh a2bRead a2bRes := by
decide
/-- The complete-cell endpoint `clampLongUpper` selects the end key on the witness. -/
theorem a2b_clampLongUpper :
clampLongUpper a2bRead a2bRes = ((a2bThree : Key) : WithTop Key) := by
unfold clampLongUpper
rw [ite_eq_left]
· rfl
· decide
/-- The gap-cell closed form is `[⊥, begin.key)` on the witness. -/
theorem a2b_closedForm :
closedFormNegCrossTruncNonposGapA2aBottom a2bRead a2bRes =
⟨(([] : Key) : WithTop Key), ((a2bFour : Key) : WithTop Key)⟩ := by
unfold closedFormNegCrossTruncNonposGapA2aBottom
rw [ite_eq_left a2b_high]
rfl
/-- The old clamped branch `[⊥, clampLongUpper]` is not the gap-cell closed form on the
witness. -/
theorem a2b_clampLongUpper_not_gapUpper :
(⟨(([] : Key) : WithTop Key), clampLongUpper a2bRead a2bRes⟩ : ConflictRange) ≠
closedFormNegCrossTruncNonposGapA2aBottom a2bRead a2bRes := by
rw [a2b_clampLongUpper, a2b_closedForm]
intro h
exact absurd (congrArg ConflictRange.end_ h) (by decide)
end FormalConflictRange