Files
conflict-range/FormalConflictRange/NegCrossTruncTopTop.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

1039 lines
52 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.NegCrossTruncStrictTop
/-!
# The crossed negative-begin truncated cell with both selectors at the sentinel
This module closes the last forward-truncated cell whose begin and end keys are both
the sentinel `⊤`: a forward read with a negative begin offset and a nonpositive end
offset. Both selectors count every stored key, so the complete window is a contiguous
block of the stored keys near the top.
Writing `q = beginSlack r`, `p = 1 - end.offset` and `t = res.keys.length`, readability
forces `t + p ≤ q + 1`. In the equality branch `t + p = q + 1` the reported keys fill
the complete window exactly, and every key at or above the reported head is necessary:
the minimal range is `[head, ⊤)`. In the strict branch `t + p < q + 1` the reported
keys are a strict prefix, and every genuine key is necessary: the minimal range is
`[⊥, ⊤)`.
-/
open FormalConflictRange
namespace FormalConflictRange
/-- A forward truncated read with a negative begin offset, a nonpositive end offset, and
both selector keys at the sentinel `⊤`. -/
def negCrossTruncTopTop (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
r.begin.key = (⊤ : WithTop Key) ∧ r.end_.key = (⊤ : WithTop Key) ∧
r.end_.offset ≤ 0
/-- The closed form for the both-sentinel truncated cell: `[head, ⊤)` when the reported
keys fill the complete window (`t + p = q + 1`), and the full range otherwise. -/
noncomputable def closedFormNegCrossTruncTopTop (r : RangeRead) (res : Result) :
ConflictRange :=
if res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1 then
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩
else ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩
namespace NegCrossTruncTopTop
variable {r : RangeRead} {res : Result}
theorem hmore (h : negCrossTruncTopTop r res) : res.more = true := h.1
theorem hrev (h : negCrossTruncTopTop r res) : r.reverse = false := h.2.1
theorem hbegin (h : negCrossTruncTopTop r res) : r.begin.offset < 0 := h.2.2.1
theorem hne (h : negCrossTruncTopTop r res) : res.keys ≠ [] := h.2.2.2.1
theorem hBtop (h : negCrossTruncTopTop r res) : r.begin.key = (⊤ : WithTop Key) :=
h.2.2.2.2.1
theorem hEtop (h : negCrossTruncTopTop r res) : r.end_.key = (⊤ : WithTop Key) :=
h.2.2.2.2.2.1
theorem hend (h : negCrossTruncTopTop r res) : r.end_.offset ≤ 0 := h.2.2.2.2.2.2
theorem beginSlack_pos_of_neg {r : RangeRead} (hbo : r.begin.offset < 0) :
0 < beginSlack r := by
unfold beginSlack
have hnn : 0 ≤ 0 - r.begin.offset := by omega
have hcast : (((0 - r.begin.offset).toNat : ℕ) : ℤ) = 0 - r.begin.offset :=
Int.toNat_of_nonneg hnn
omega
theorem endPad_pos_of_nonpos {r : RangeRead} (hend : r.end_.offset ≤ 0) :
0 < (1 - r.end_.offset).toNat := by
have hnn : 0 ≤ 1 - r.end_.offset := by omega
have hcast : (((1 - r.end_.offset).toNat : ℕ) : ℤ) = 1 - r.end_.offset :=
Int.toNat_of_nonneg hnn
omega
theorem beginSlack_cast_of_neg {r : RangeRead} (hbo : r.begin.offset < 0) :
(beginSlack r : ℤ) = -r.begin.offset := by
have hnn : 0 ≤ 0 - r.begin.offset := by omega
unfold beginSlack
rw [Int.toNat_of_nonneg hnn]
ring
theorem endPad_cast_of_nonpos {r : RangeRead} (hend : r.end_.offset ≤ 0) :
((1 - r.end_.offset).toNat : ℤ) = 1 - r.end_.offset := by
have hnn : 0 ≤ 1 - r.end_.offset := by omega
rw [Int.toNat_of_nonneg hnn]
theorem beginSlack_pos (h : negCrossTruncTopTop r res) : 0 < beginSlack r :=
beginSlack_pos_of_neg h.2.2.1
theorem endPad_pos (h : negCrossTruncTopTop r res) : 0 < (1 - r.end_.offset).toNat :=
endPad_pos_of_nonpos h.2.2.2.2.2.2
theorem beginSlack_cast (h : negCrossTruncTopTop r res) :
(beginSlack r : ℤ) = -r.begin.offset :=
beginSlack_cast_of_neg h.2.2.1
theorem endPad_cast (h : negCrossTruncTopTop r res) :
((1 - r.end_.offset).toNat : ℤ) = 1 - r.end_.offset :=
endPad_cast_of_nonpos h.2.2.2.2.2.2
/-- The empty window never reports a truncated nonempty prefix. -/
theorem not_compatible_of_resolve_end_nil {db : DB} {r : RangeRead} {res : Result}
(_hrev : r.reverse = false) (_hmore : res.more = true) (hne : res.keys ≠ [])
(hE : resolve db r.end_ = (([] : Key) : WithTop Key)) :
¬ RangeRead.Compatible db r res := by
intro hc
have hpre := RangeRead.compatible_keys_prefix hc
have hneW : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne
obtain ⟨a, ha⟩ : ∃ a, a ∈ RangeRead.completeKeys db r := by
cases hW : RangeRead.completeKeys db r with
| nil => exact absurd hW hneW
| cons a t => exact ⟨a, by simp []⟩
have hlt := (RangeRead.mem_completeKeys.mp ha).2.2
rw [hE, WithTop.coe_lt_coe] at hlt
exact absurd hlt (List.not_lt_nil a)
/-- The begin anchor with the begin key at the sentinel is `N - q - 1`. -/
theorem anchor_begin_top {db : DB} {r : RangeRead}
(hBtop : r.begin.key = (⊤ : WithTop Key)) (hbo : r.begin.offset < 0) :
r.begin.anchor db = (db.keys.length : ℤ) - (beginSlack r : ℤ) - 1 := by
rw [anchor_of_top (db := db) hBtop, beginSlack_cast_of_neg hbo]
ring
/-- The end anchor with the end key at the sentinel is `N - p`. -/
theorem anchor_end_top {db : DB} {r : RangeRead}
(hEtop : r.end_.key = (⊤ : WithTop Key)) (hend : r.end_.offset ≤ 0) :
r.end_.anchor db = (db.keys.length : ℤ) - ((1 - r.end_.offset).toNat : ℤ) := by
rw [anchor_of_top (db := db) hEtop, endPad_cast_of_nonpos hend]
ring
/-- The begin anchor of the both-sentinel read is `N - q - 1`. -/
theorem anchor_begin_eq (h : negCrossTruncTopTop r res) (db : DB) :
r.begin.anchor db = (db.keys.length : ℤ) - (beginSlack r : ℤ) - 1 :=
anchor_begin_top h.2.2.2.2.1 h.2.2.1
/-- The end anchor of the both-sentinel read is `N - p`. -/
theorem anchor_end_eq (h : negCrossTruncTopTop r res) (db : DB) :
r.end_.anchor db = (db.keys.length : ℤ) - ((1 - r.end_.offset).toNat : ℤ) :=
anchor_end_top h.2.2.2.2.2.1 h.2.2.2.2.2.2
/-- Under `N ≥ p`, the end resolution is the stored key at index `N - p`. Under
`N ≥ q + 1`, the begin resolution is the stored key at index `N - q - 1`. This is the
drop-and-take form of the complete window. -/
theorem completeKeys_eq_drop_take {db : DB} {r : RangeRead}
(hrev : r.reverse = false) (hBtop : r.begin.key = (⊤ : WithTop Key))
(hEtop : r.end_.key = (⊤ : WithTop Key))
(hbo : r.begin.offset < 0) (hend : r.end_.offset ≤ 0)
(hNp : (1 - r.end_.offset).toNat ≤ db.keys.length)
(hNq : beginSlack r < db.keys.length) :
RangeRead.completeKeys db r =
(db.keys.drop (db.keys.length - beginSlack r - 1)).take
(beginSlack r + 1 - (1 - r.end_.offset).toNat) := by
classical
set l := db.keys with hl
set q := beginSlack r with hq
set p := (1 - r.end_.offset).toNat with hp
have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos_of_nonpos hend
have hls : l.SortedLT := by rw [hl]; exact db.sorted
have hqN : q < l.length := hNq
have hpN : p ≤ l.length := hNp
have hanB : (r.begin.anchor db).toNat = l.length - q - 1 := by
rw [anchor_begin_top hBtop hbo, hq, hl]; omega
have hanE : (r.end_.anchor db).toNat = l.length - p := by
rw [anchor_end_top hEtop hend, hp, hl]; omega
have hanBnn : 0 ≤ r.begin.anchor db := by
rw [anchor_begin_top hBtop hbo, ← hq, ← hl]
have hcast : (beginSlack r : ℤ) < (db.keys.length : ℤ) := by exact_mod_cast hNq
omega
have hsorted : (l.drop (l.length - q - 1)).SortedLT :=
List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := l.length - q - 1) hls.pairwise)
-- The count of dropped keys below the end resolution simplifies to `q + 1 - p`.
have hcnt : (l.drop (l.length - q - 1)).countP
(fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) =
q + 1 - p := by
rw [countP_drop_eq hls, countP_lt_resolve, hanE]
have hlen : db.keys.length = l.length := by rw [hl]
rw [hlen]
omega
rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])]
change l.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧
(↑k : WithTop Key) < resolve db r.end_)) = _
have hsplit : l.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧
(↑k : WithTop Key) < resolve db r.end_)) =
(l.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key)))).filter
(fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by
rw [List.filter_filter]
apply List.filter_congr
intro k _
simp only [Bool.decide_and]
exact Bool.and_comm _ _
rw [hsplit, filter_resolve_le_eq_drop, hanB,
filter_lt_eq_take_countP hsorted (resolve db r.end_), hcnt]
/-- When the database has exactly `beginSlack` keys, the begin clamps to the bottom and
the complete window is the first `beginSlack - p` keys. -/
theorem completeKeys_eq_take_of_size_beginSlack {db : DB} {r : RangeRead} {res : Result}
(h : negCrossTruncTopTop r res) (hN : db.keys.length = beginSlack r)
(hpq : (1 - r.end_.offset).toNat ≤ beginSlack r) :
RangeRead.completeKeys db r =
db.keys.take (beginSlack r - (1 - r.end_.offset).toNat) := by
classical
set q := beginSlack r with hq
set p := (1 - r.end_.offset).toNat with hp
have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h
have hltqp : p ≤ q := hpq
have hanB : r.begin.anchor db < 0 := by
rw [anchor_begin_eq h db, hN, hq]; omega
have hrb : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil hanB
have hanE : r.end_.anchor db = (q : ℤ) - (p : ℤ) := by
rw [anchor_end_eq h db, hN, hq, hp]
have hanE0 : 0 ≤ r.end_.anchor db := by rw [hanE]; omega
have hanE_toNat : (r.end_.anchor db).toNat = q - p := by
rw [hanE]
rw [show (q : ℤ) - (p : ℤ) = ((q - p : ℕ) : ℤ) by omega]
exact Int.toNat_natCast _
have hanE_lt : (r.end_.anchor db).toNat < db.keys.length := by
rw [hanE_toNat, hN, hq]; omega
have hre : resolve db r.end_ = (↑db.keys[(r.end_.anchor db).toNat] : WithTop Key) :=
resolve_eq_getElem hanE0 hanE_lt
rw [completeKeys_eq_of_resolve_nil_coe (db := db)
(w := db.keys[(r.end_.anchor db).toNat]) h.2.1 h.2.2.2.2.2.2 hrb hre]
rw [filter_lt_eq_take_countP db.sorted]
have hcnt : db.keys.countP
(fun k : Key => decide ((↑k : WithTop Key) <
(↑db.keys[(r.end_.anchor db).toNat] : WithTop Key))) = q - p := by
rw [countP_lt_coe]
have := countP_lt_eq_of_getElem db.sorted
(x := db.keys[(r.end_.anchor db).toNat]) hanE_lt rfl
rw [this, hanE_toNat]
rw [hcnt]
/-- The complete window of a forward both-sentinel read has at most `q + 1 - p` keys,
whatever the database. -/
theorem completeKeys_length_le_topTop {db : DB} {r : RangeRead}
(hrev : r.reverse = false) (hBtop : r.begin.key = (⊤ : WithTop Key))
(hEtop : r.end_.key = (⊤ : WithTop Key))
(hbo : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) :
(RangeRead.completeKeys db r).length ≤
beginSlack r + 1 - (1 - r.end_.offset).toNat := by
classical
set N := db.keys.length with hN
set q := beginSlack r with hq
set p := (1 - r.end_.offset).toNat with hp
by_cases hpN : p ≤ N
· by_cases hqN : q < N
· have hdt := completeKeys_eq_drop_take hrev hBtop hEtop hbo hend
(by rw [← hp, ← hN]; exact hpN) (by rw [← hq, ← hN]; exact hqN)
rw [hdt, List.length_take, List.length_drop]
omega
· push Not at hqN
-- `N ≤ q`: the begin clamps to the bottom
have hanB : r.begin.anchor db < 0 := by
rw [anchor_begin_top hBtop hbo]
have : (N : ℤ) ≤ q := by exact_mod_cast hqN
omega
have hresB : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil hanB
have hlen : (RangeRead.completeKeys db r).length =
db.keys.countP (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by
rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])]
rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) =
db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧
(↑k : WithTop Key) < resolve db r.end_)) from rfl, hresB]
rw [show db.keys.filter (fun k : Key => decide (((↑([] : Key)) : WithTop Key) ≤ (↑k :
WithTop Key) ∧
(↑k : WithTop Key) < resolve db r.end_)) =
db.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) from by
apply List.filter_congr
intro k _
have h : decide (((↑([] : Key)) : WithTop Key) ≤ (↑k : WithTop Key)) = true := by
rw [decide_eq_true_eq]; exact WithTop.coe_le_coe.mpr (nil_le_key k)
rw [Bool.decide_and, h, Bool.true_and]]
rw [← List.countP_eq_length_filter]
rw [hlen, countP_lt_resolve, anchor_end_top hEtop hend, ← hN, ← hp]
omega
· push Not at hpN
-- `p > N`: the end resolves below the bottom, so the window is empty
have hanE : r.end_.anchor db < 0 := by
rw [anchor_end_top hEtop hend]
have : (N : ℤ) < p := by exact_mod_cast hpN
omega
have hresE : resolve db r.end_ = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil hanE
have hempty : RangeRead.completeKeys db r = [] := by
rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])]
rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) =
db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧
(↑k : WithTop Key) < resolve db r.end_)) from rfl, hresE]
rw [List.filter_eq_nil_iff]
intro k _
simp only [decide_eq_true_eq, not_and]
intro _
exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k))
rw [hempty]
simp
/-- The complete window is empty when the end anchor is negative. -/
theorem completeKeys_eq_nil_of_anchor_end_neg {db : DB} {r : RangeRead}
(h : r.end_.anchor db < 0) : RangeRead.completeKeys db r = [] := by
have hres : resolve db r.end_ = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil h
have hempty : RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = [] := by
rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) =
db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧
(↑k : WithTop Key) < resolve db r.end_)) from rfl, hres]
rw [List.filter_eq_nil_iff]
intro k _
simp only [decide_eq_true_eq, not_and]
intro _
exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k))
rw [RangeRead.completeKeys]
by_cases hrev : r.reverse = true
· rw [ite_eq_left hrev]; exact List.reverse_eq_nil_iff.mpr hempty
· rw [ite_eq_right hrev]; exact hempty
/-- Readability forces the both-sentinel counts to fit: `t + p ≤ q + 1`. -/
theorem readable_counts_topTop (h : negCrossTruncTopTop r res) (hread : Readable r res) :
res.keys.length + (1 - r.end_.offset).toNat ≤ beginSlack r + 1 := by
obtain ⟨db, hcomp⟩ := hread
have hpre := RangeRead.compatible_keys_prefix hcomp
have hlen := hpre.length_le
have hbound := completeKeys_length_le_topTop (db := db)
h.2.1 h.2.2.2.2.1 h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2
have ht1 : 1 ≤ res.keys.length :=
List.length_pos_of_ne_nil (RangeRead.compatible_ne_nil_of_more hcomp h.1)
omega
/-- In the equality branch, compatibility forces the stored keys at or above the
reported head to be the reported keys followed by exactly `p` keys. -/
theorem exists_filter_head_decomp_of_compatible {db : DB} (h : negCrossTruncTopTop r res)
(hne : res.keys ≠ []) (hcomp : RangeRead.Compatible db r res)
(hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) :
∃ G : List Key, db.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) =
res.keys ++ G ∧ G.length = (1 - r.end_.offset).toNat := by
classical
set t := res.keys.length with ht
set p := (1 - r.end_.offset).toNat with hp
have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp h.2.1
have hpre := RangeRead.compatible_keys_prefix hcomp
have hlen := hpre.length_le
have hbound := completeKeys_length_le_topTop (db := db)
h.2.1 h.2.2.2.2.1 h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2
rw [← hp] at hbound
have hcklen : (RangeRead.completeKeys db r).length = t := by
have hq : beginSlack r + 1 - p = t := by omega
have h1 : (RangeRead.completeKeys db r).length ≤ t := by omega
exact le_antisymm h1 (by rw [ht]; exact hlen)
have hckeq : res.keys = RangeRead.completeKeys db r :=
hpre.eq_of_length (by rw [← ht]; exact hcklen.symm)
set N := db.keys.length with hN
have hanEnd : r.end_.anchor db = (N : ℤ) - (p : ℤ) := by
rw [anchor_end_top h.2.2.2.2.2.1 h.2.2.2.2.2.2, ← hN, ← hp]
have hanE_nn : 0 ≤ r.end_.anchor db := by
by_contra hneg
have hnil := completeKeys_eq_nil_of_anchor_end_neg (db := db) (lt_of_not_ge hneg)
rw [← hckeq] at hnil
exact hne hnil
have hanE_toNat : (r.end_.anchor db).toNat = N - p := by
rw [hanEnd]
have hpN : (p : ℤ) ≤ (N : ℤ) := by omega
rw [Int.toNat_of_nonneg (by omega)]
omega
have hanE_lt : (r.end_.anchor db).toNat < N := by rw [hanE_toNat]; omega
have hhead_mem : res.keys.head hne ∈ RangeRead.completeKeys db r := by
rw [← hckeq]; exact List.head_mem hne
have hresB_le : resolve db r.begin ≤ ((res.keys.head hne : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp hhead_mem).2.1
have hhead_lt_resE : ((res.keys.head hne : Key) : WithTop Key) < resolve db r.end_ :=
(RangeRead.mem_completeKeys.mp hhead_mem).2.2
have hhead_min : ∀ k ∈ res.keys, res.keys.head hne ≤ k :=
fun k hk => (hsort.pairwise.imp le_of_lt).rel_head hk
refine ⟨db.keys.drop (N - p), ?_, ?_⟩
· have hTake : db.keys.take (N - p) = db.keys.filter
(fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by
have hcnt : db.keys.countP
(fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) = N - p := by
rw [countP_lt_resolve, hanE_toNat, Nat.min_eq_left (by rw [← hN]; omega)]
rw [← hcnt, filter_lt_eq_take_countP db.sorted]
have hFL : (db.keys.take (N - p)).filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) = res.keys := by
rw [hTake, List.filter_filter]
have hcongr : db.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key)) &&
decide ((↑k : WithTop Key) < resolve db r.end_)) =
db.keys.filter (fun k : Key => decide
(resolve db r.begin ≤ (↑k : WithTop Key)) &&
decide ((↑k : WithTop Key) < resolve db r.end_)) := by
apply List.filter_congr
intro k hkdb
by_cases hh : ((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)
· have hb : resolve db r.begin ≤ ((k : Key) : WithTop Key) := le_trans hresB_le hh
by_cases hl : ((k : Key) : WithTop Key) < resolve db r.end_ <;>
simp [hh, hb, hl]
· by_cases hl : ((k : Key) : WithTop Key) < resolve db r.end_
· have hb : ¬ (resolve db r.begin ≤ ((k : Key) : WithTop Key)) := by
intro hb
have hkck : k ∈ RangeRead.completeKeys db r := by
rw [RangeRead.completeKeys, ite_eq_right (by simp [h.2.1])]
rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) =
db.keys.filter (fun x : Key => decide (resolve db r.begin ≤ (↑x : WithTop Key) ∧
(↑x : WithTop Key) < resolve db r.end_)) from rfl, List.mem_filter]
exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hb, hl⟩⟩
exact hh (WithTop.coe_le_coe.mpr (hhead_min k (by rw [hckeq]; exact hkck)))
simp [hh, hb, hl]
· simp [hh, hl]
have hR : db.keys.filter (fun k : Key => decide
(resolve db r.begin ≤ (↑k : WithTop Key)) &&
decide ((↑k : WithTop Key) < resolve db r.end_)) = res.keys := by
rw [hckeq, RangeRead.completeKeys, ite_eq_right (by simp [h.2.1]), RangeRead.inRange]
simp only [Bool.decide_and]
exact hcongr.trans hR
have hFD : (db.keys.drop (N - p)).filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) =
db.keys.drop (N - p) := by
rw [List.filter_eq_self]
intro k hk
rw [decide_eq_true_eq]
have hdrop : db.keys.drop (N - p) = db.keys.filter (fun k : Key => decide
(resolve db r.end_ ≤ (↑k : WithTop Key))) := by
rw [filter_resolve_le_eq_drop (db := db) (s := r.end_), hanE_toNat]
rw [hdrop] at hk
exact le_trans (le_of_lt hhead_lt_resE)
(of_decide_eq_true (List.mem_filter.mp hk).2)
conv_lhs => rw [← List.take_append_drop (N - p) db.keys]
rw [List.filter_append, hFL, hFD]
· rw [List.length_drop]; omega
/-- In the equality branch, the stored keys at or above the reported head being the
reported keys followed by `p` keys makes the database compatible. -/
theorem compatible_of_filter_head_decomp {db : DB} (h : negCrossTruncTopTop r res)
(hne : res.keys ≠ []) {G : List Key}
(hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1)
(hF : db.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) =
res.keys ++ G)
(hGlen : G.length = (1 - r.end_.offset).toNat)
(hlim : RangeRead.withinLimit r res.keys) :
RangeRead.Compatible db r res := by
classical
set t := res.keys.length with ht
set p := (1 - r.end_.offset).toNat with hp
have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h
set c := db.keys.countP
(fun k : Key => decide ((↑k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key))) with hc
have hdropF : db.keys.drop c = res.keys ++ G := by
rw [← filter_not_lt_eq_drop_countP db.sorted]
rw [← hF]
apply List.filter_congr
intro k _
rw [decide_eq_decide, not_lt, WithTop.coe_le_coe]
have hclen : (res.keys ++ G).length = db.keys.length - c := by
rw [← hdropF, List.length_drop]
set N := db.keys.length with hN
have hNc : N = c + t + p := by
rw [hN]
have : (res.keys ++ G).length = t + p := by rw [List.length_append, ht, hGlen]
omega
have hpN : p ≤ N := by omega
have hqN : beginSlack r < N := by omega
have hck : RangeRead.completeKeys db r = res.keys := by
rw [completeKeys_eq_drop_take (r := r) (db := db) h.2.1 h.2.2.2.2.1
h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 hpN hqN]
have hidx : N - beginSlack r - 1 = c := by omega
have htake : beginSlack r + 1 - p = t := by omega
rw [hidx, htake, hdropF, List.take_append_of_le_length (by omega), ht, List.take_length]
exact ⟨hlim, Or.inl ⟨h.1, hck ▸ List.prefix_refl _, hne⟩⟩
/-- **Soundness in the equality branch.** Agreement on `[head, ⊤)` fixes the stored
keys at or above the reported head, hence compatibility by the decomposition above. -/
theorem sound_top_head_of_branchH (h : negCrossTruncTopTop r res) (hne : res.keys ≠ [])
(hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) :
Sound r res ⟨((res.keys.head hne : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by
classical
intro db₁ db₂ hag
have hagree : ∀ k : Key, ((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) →
(k ∈ db₁.keys ↔ k ∈ db₂.keys) := by
intro k hk
exact (agreeOn_iff _ _ _).mp hag k ⟨hk, WithTop.coe_lt_top k⟩
have hfilter : db₁.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) =
db₂.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) := by
apply List.SortedLT.eq_of_mem_iff
· exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _)
· exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _)
· intro k
rw [List.mem_filter, List.mem_filter]
constructor
· intro hk1
exact ⟨(hagree k (of_decide_eq_true hk1.2)).mp hk1.1, hk1.2⟩
· intro hk2
exact ⟨(hagree k (of_decide_eq_true hk2.2)).mpr hk2.1, hk2.2⟩
constructor
· intro hcomp
obtain ⟨G, hF, hGlen⟩ := exists_filter_head_decomp_of_compatible h hne hcomp hH
exact compatible_of_filter_head_decomp h hne hH (hfilter ▸ hF) hGlen hcomp.1
· intro hcomp
obtain ⟨G, hF, hGlen⟩ := exists_filter_head_decomp_of_compatible h hne hcomp hH
exact compatible_of_filter_head_decomp h hne hH (hfilter ▸ hF) hGlen hcomp.1
/-- **Removal witness above the reported last in the equality branch.** Store the reported
keys followed by `p` fresh keys above the reported last, one of them the target. The
begin anchor is `0`, so the window is exactly the reported keys. Erasing the target
lowers the begin anchor to `-1` (clamping to `⊥`) and pulls the end resolution down onto
the reported last, so the window loses its last key. -/
theorem exists_witness_above_last_branchH {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1)
(hkgt : res.keys.getLast (hne h) < k) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧
¬ RangeRead.Compatible (dbw.erase k) r res := by
classical
obtain ⟨db₀, hcomp₀⟩ := hread
have hne := hne h
have hrev := hrev h
have hmore := hmore h
set t := res.keys.length with ht
set p := (1 - r.end_.offset).toNat with hp
have ht1 : 1 ≤ t := by rw [ht]; exact List.length_pos_of_ne_nil hne
have hp1 : 0 < p := by rw [hp]; exact endPad_pos h
set last := res.keys.getLast hne with hlast
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
(hsort.pairwise.imp le_of_lt).rel_getLast hx
have hcard : ((p - 1 : ℕ) : ℕ∞) + 1 ≤
intervalCard ((keyAfter last : Key) : WithTop Key) (⊤ : WithTop Key) := by
rw [intervalCard_top_right (by simp)]
exact le_top
obtain ⟨dba, hdbaIn, hknot, hdbaLen⟩ :=
exists_db_keysIn_avoid (B := ((keyAfter last : Key) : WithTop Key))
(E := (⊤ : WithTop Key)) (k := k) (m := p - 1) hcard
set G : List Key := (dba.insert k hknot).keys with hG
have hGlen : G.length = p := by
rw [hG, DB.insert_keys, List.orderedInsert_length, hdbaLen]
omega
have hGmem : k ∈ G := by
rw [hG]; exact DB.mem_insert_self dba k hknot
have hGgt : ∀ x ∈ G, last < x := by
intro x hx
rw [hG, DB.insert_keys] at hx
rcases (List.mem_orderedInsert (r := (· ≤ ·))).mp hx with rfl | hx'
· exact hkgt
· have hxge : ((keyAfter last : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) :=
(hdbaIn x hx').1
exact lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hxge)
have hDsort : (res.keys ++ G).SortedLT := by
refine List.sortedLT_append.mpr ⟨hsort, (dba.insert k hknot).sorted, ?_⟩
intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha) (hGgt b hb)
let dbw : DB := ⟨res.keys ++ G, hDsort⟩
have hdbw_keys : dbw.keys = res.keys ++ G := rfl
have hN : dbw.keys.length = t + p := by
rw [hdbw_keys, List.length_append, hGlen, ← ht]
have hq : beginSlack r = t + p - 1 := by omega
have hck : RangeRead.completeKeys dbw r = res.keys := by
rw [completeKeys_eq_drop_take (db := dbw) hrev h.2.2.2.2.1 h.2.2.2.2.2.1
h.2.2.1 h.2.2.2.2.2.2 (by rw [hN]; omega) (by rw [hq, hN]; omega)]
have hidx : dbw.keys.length - beginSlack r - 1 = 0 := by rw [hN, hq]; omega
have htake : beginSlack r + 1 - (1 - r.end_.offset).toNat = t := by
rw [← hp, hq]; omega
rw [hidx, htake, hdbw_keys, List.drop_zero,
List.take_append_of_le_length (by rw [← ht]), ht, List.take_length]
have hcompw : RangeRead.Compatible dbw r res := by
refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩
rw [hck]
have hkdbw : k ∈ dbw.keys := by
rw [hdbw_keys]; exact List.mem_append.mpr (Or.inr hGmem)
have hk_not_res : k ∉ res.keys := fun hx => absurd (hle_last k hx) (not_le.mpr hkgt)
have hkeys' : (dbw.erase k).keys = res.keys ++ G.erase k := by
rw [DB.erase_keys, hdbw_keys, List.erase_append, ite_eq_right hk_not_res]
have hN' : (dbw.erase k).keys.length = t + p - 1 := by
rw [hkeys', List.length_append, List.length_erase_of_mem hGmem, hGlen, ← ht]
omega
have hNq' : (dbw.erase k).keys.length = beginSlack r := by
rw [hN', hq]
have hanB' : r.begin.anchor (dbw.erase k) < 0 := by
rw [anchor_begin_eq h (dbw.erase k), hNq']
omega
have hrb' : resolve (dbw.erase k) r.begin = ((↑([] : Key)) : WithTop Key) :=
resolve_eq_nil hanB'
have hanE' : r.end_.anchor (dbw.erase k) = (t : ℤ) - 1 := by
rw [anchor_end_eq h (dbw.erase k), hN', ← hp]
omega
have hanE'_toNat : (r.end_.anchor (dbw.erase k)).toNat = t - 1 := by
rw [hanE']
rw [show (t : ℤ) - 1 = ((t - 1 : ℕ) : ℤ) by
rw [Nat.cast_sub ht1]; push_cast; ring]
exact Int.toNat_natCast _
have hanE'_lt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by
rw [hanE'_toNat, hN']; omega
have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanE']; omega
have hidx : (r.end_.anchor (dbw.erase k)).toNat = t - 1 := hanE'_toNat
have hlastIdx : res.keys[t - 1]? = some last := by
have hlen : t - 1 < res.keys.length := by rw [← ht]; omega
rw [List.getElem?_eq_getElem hlen, hlast, List.getLast_eq_getElem hne]
rw [resolve, ite_eq_right (not_lt.mpr h0), hkeys', List.getElem?_append,
ite_eq_left (by rw [hidx, ← ht]; omega), hidx, hlastIdx]
have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by
rw [completeKeys_eq_of_resolve_nil_coe (db := dbw.erase k) (w := last) hrev
h.2.2.2.2.2.2 hrb' hre', hkeys', List.filter_append]
have hres_fil : res.keys.filter
(fun x : Key => decide ((↑x : WithTop Key) < (↑last : WithTop Key))) =
res.keys.dropLast := by
have hpred : ∀ x ∈ res.keys,
decide ((↑x : WithTop Key) < (↑last : WithTop Key)) = decide (x < last) := by
intro x _; rw [decide_eq_decide]; exact WithTop.coe_lt_coe
rw [List.filter_congr hpred]
conv_lhs => rw [← List.dropLast_append_getLast hne, hlast]
rw [List.filter_append]
have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) =
res.keys.dropLast := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
have h := hsort.pairwise.rel_dropLast_getLast hx
simpa [hlast] using h
rw [hfil]
simp
have hG_fil : (G.erase k).filter
(fun x : Key => decide ((↑x : WithTop Key) < (↑last : WithTop Key))) = [] := by
rw [List.filter_eq_nil_iff]
intro x hx
simp only [Bool.not_eq_true, decide_eq_false_iff_not, WithTop.coe_lt_coe]
exact not_lt.mpr (le_of_lt (hGgt x (List.mem_of_mem_erase hx)))
rw [hres_fil, hG_fil, List.append_nil]
have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by
intro hc
have hpre := RangeRead.compatible_keys_prefix hc
rw [hckE] at hpre
have hlen := hpre.length_le
rw [List.length_dropLast, ← ht] at hlen
omega
exact ⟨dbw, hcompw, hkdbw, hnot⟩
/-- Above the reported last, in the equality branch, every key is necessary. -/
theorem necessary_above_last_branchH {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1)
(hkgt : res.keys.getLast (hne h) < k) :
Necessary r res k := by
obtain ⟨dbw, hcompw, hkdbw, hnot⟩ :=
exists_witness_above_last_branchH hread h hH hkgt
exact necessary_of_toggle ⟨dbw, hcompw, by
rw [toggle_eq_erase_of_mem hkdbw]
exact hnot⟩
/-- **The equality branch is minimal with range `[head, ⊤)`.** -/
theorem minimal_top_head_of_branchH {r : RangeRead} {res : Result}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) :
Minimal r res ⟨((res.keys.head (hne h) : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by
classical
have hne := hne h
refine minimal_iff_sound_and_necessaryRange.mpr
⟨sound_top_head_of_branchH h hne hH, ?_⟩
intro k hklo _
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 hklo) (WithTop.coe_le_coe.mp hklast)
· push Not at hklast
exact necessary_above_last_branchH hread h hH (WithTop.coe_lt_coe.mp hklast)
/-- Inserting a key above every element of the first list distributes over append. -/
theorem orderedInsert_append_of_forall_lt {l₁ l₂ : List Key} {k : Key}
(h : ∀ x ∈ l₁, x < k) :
(l₁ ++ l₂).orderedInsert (· ≤ ·) k = l₁ ++ l₂.orderedInsert (· ≤ ·) k := by
induction l₁ with
| nil => rfl
| cons a t ih =>
rw [List.cons_append, List.orderedInsert_cons,
ite_eq_right (not_le.mpr (h a (List.mem_cons_self ..))),
ih (fun x hx => h x (List.mem_cons_of_mem a hx))]
exact (List.cons_append).symm
/-- **Removal witness below the reported head in the strict branch.** Store the reported
keys followed by `q - t` fresh keys above the reported last, so the begin clamps and the
window is exactly the reported keys (the end resolves above the last). Inserting
`k < head` lifts the begin anchor to `0`, so the window starts with `k` and the reported
truncated prefix breaks. -/
theorem exists_witness_below_head_branchF {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1)
(hk : ((k : Key) : WithTop Key) < ((res.keys.head (hne h) : Key) : WithTop Key)) :
∃ dbw : DB, ∃ hkdb : k ∉ dbw.keys, RangeRead.Compatible dbw r res ∧
¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by
classical
obtain ⟨db₀, hcomp₀⟩ := hread
have hne := hne h
have hrev := hrev h
have hmore := hmore h
set t := res.keys.length with ht
set q := beginSlack r with hq
set p := (1 - r.end_.offset).toNat with hp
have ht1 : 1 ≤ t := by rw [ht]; exact List.length_pos_of_ne_nil hne
have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h
have htp : t + p ≤ q := by omega
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
set last := res.keys.getLast hne with hlast
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
(hsort.pairwise.imp le_of_lt).rel_getLast hx
have hhead_le_last : res.keys.head hne ≤ last := hle_last _ (List.head_mem hne)
have hhead_le : ∀ x ∈ res.keys, res.keys.head hne ≤ x :=
fun x hx => (hsort.pairwise.imp le_of_lt).rel_head hx
have hcard : (((q - t) + 1 : ℕ) : ℕ∞) ≤
intervalCard ((keyAfter last : Key) : WithTop Key) (⊤ : WithTop Key) := by
rw [intervalCard_top_right (by simp)]
exact le_top
obtain ⟨dba, hdbaIn, hknot, hdbaLen⟩ :=
exists_db_keysIn_avoid (B := ((keyAfter last : Key) : WithTop Key))
(E := (⊤ : WithTop Key)) (k := k) (m := q - t) hcard
set G : List Key := dba.keys with hG
have hGlen : G.length = q - t := hdbaLen
have hGgt : ∀ x ∈ G, last < x := by
intro x hx
rw [hG] at hx
have hxge : ((keyAfter last : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) :=
(hdbaIn x hx).1
exact lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hxge)
have hDsort : (res.keys ++ G).SortedLT := by
refine List.sortedLT_append.mpr ⟨hsort, ?_, ?_⟩
· simpa [hG] using dba.sorted
· intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha) (hGgt b hb)
let dbw : DB := ⟨res.keys ++ G, hDsort⟩
have hdbw_keys : dbw.keys = res.keys ++ G := rfl
have hN : dbw.keys.length = q := by
rw [hdbw_keys, List.length_append, hGlen, ← ht]
omega
have hpq : p ≤ q := by omega
have hck : RangeRead.completeKeys dbw r = dbw.keys.take (q - p) := by
have h0 := completeKeys_eq_take_of_size_beginSlack (r := r) (res := res) (db := dbw) h
(by rw [hN, hq]) (by simpa [← hq, ← hp] using hpq)
simpa [← hq, ← hp] using h0
have hqp : t ≤ q - p := by omega
have htk : List.take (q - p) (res.keys ++ G) =
res.keys ++ List.take (q - p - t) G := by
rw [List.take_append, List.take_of_length_le (by rw [← ht]; omega), ← ht]
have hck' : RangeRead.completeKeys dbw r = res.keys ++ List.take (q - p - t) G := by
rw [hck, hdbw_keys, htk]
have hpre : res.keys <+: RangeRead.completeKeys dbw r := by
rw [hck']; exact List.prefix_append _ _
have hcompw : RangeRead.Compatible dbw r res :=
⟨hcomp₀.1, Or.inl ⟨hmore, hpre, hne⟩⟩
have hk_head : k < res.keys.head hne := WithTop.coe_lt_coe.mp hk
have hk_lt_last : k < last := lt_of_lt_of_le hk_head hhead_le_last
have hk_not_res : k ∉ res.keys := fun hx => absurd (hhead_le k hx) (not_le.mpr hk_head)
have hk_not_G : k ∉ G := fun hx => absurd (hGgt k hx) (not_lt.mpr (le_of_lt hk_lt_last))
have hkdb : k ∉ dbw.keys := by
rw [hdbw_keys]; simp [hk_not_res, hk_not_G]
have hins_keys : (dbw.insert k hkdb).keys = k :: (res.keys ++ G) := by
rw [DB.insert_keys, hdbw_keys]
cases hres : res.keys with
| nil => exact absurd hres hne
| cons a tl =>
have hka : k ≤ a := le_of_lt (WithTop.coe_lt_coe.mp (by simpa [hres] using hk))
rw [List.cons_append]
exact List.orderedInsert_cons_of_le (· ≤ ·) (tl ++ G) hka
have hN' : (dbw.insert k hkdb).keys.length = q + 1 := by
rw [hins_keys, List.length_cons, List.length_append, hGlen, ← ht]
omega
have hNp' : p ≤ (dbw.insert k hkdb).keys.length := by rw [hN']; omega
have hNq' : beginSlack r < (dbw.insert k hkdb).keys.length := by rw [hN', hq]; omega
have hckins : RangeRead.completeKeys (dbw.insert k hkdb) r =
List.take (q + 1 - p) (k :: (res.keys ++ G)) := by
have h0 := completeKeys_eq_drop_take (db := dbw.insert k hkdb) hrev h.2.2.2.2.1
h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 hNp' hNq'
rw [h0]
have hidx : (dbw.insert k hkdb).keys.length - beginSlack r - 1 = 0 := by
rw [hN', hq]; omega
have htake : beginSlack r + 1 - p = q + 1 - p := by rw [hq]
rw [hidx, htake, List.drop_zero, hins_keys]
have hk_le_last : k ≤ last := le_of_lt hk_lt_last
have hfilins : (RangeRead.completeKeys (dbw.insert k hkdb) r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = k :: res.keys := by
rw [hckins, ← hlast]
rw [show q + 1 - p = (q - p) + 1 by omega, List.take_succ_cons, htk]
rw [List.filter_cons_of_pos (by rw [decide_eq_true_eq]; exact hk_le_last),
List.filter_append]
have hres_fil : res.keys.filter (fun x : Key => decide (x ≤ last)) = res.keys := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
exact hle_last x hx
have hG_fil : (List.take (q - p - t) G).filter
(fun x : Key => decide (x ≤ last)) = [] := by
rw [List.filter_eq_nil_iff]
intro x hx
simp only [Bool.not_eq_true, decide_eq_false_iff_not]
exact not_le.mpr (hGgt x (List.mem_of_mem_take hx))
rw [hres_fil, hG_fil, List.append_nil]
have hnot : ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by
intro hc
have heq := ((compatible_iff_truncated_keys hrev hmore hne).mp hc).2
rw [hfilins] at heq
have hlen := congrArg List.length heq
rw [List.length_cons] at hlen
omega
exact ⟨dbw, hkdb, hcompw, hnot⟩
/-- Below the reported head, in the strict branch, every key is necessary. -/
theorem necessary_below_head_branchF {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1)
(hk : ((k : Key) : WithTop Key) < ((res.keys.head (hne h) : Key) : WithTop Key)) :
Necessary r res k := by
obtain ⟨dbw, hkdb, hcompw, hnot⟩ := exists_witness_below_head_branchF hread h hF hk
exact necessary_of_toggle ⟨dbw, hcompw, by
rw [toggle_eq_insert_of_not_mem hkdb]
exact hnot⟩
/-- **Removal witness above the reported last in the strict branch.** Store the reported
keys followed by `q + 1 - t` fresh keys above the reported last; the begin anchor is `0`
and the window starts at the reported head, so the result is compatible. Inserting
`k > last` lifts the begin anchor to `1`, so the window starts above the head and the
reported truncated prefix breaks. -/
theorem exists_witness_above_last_branchF {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1)
(hkgt : res.keys.getLast (hne h) < k) :
∃ dbw : DB, ∃ hkdb : k ∉ dbw.keys, RangeRead.Compatible dbw r res ∧
¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by
classical
obtain ⟨db₀, hcomp₀⟩ := hread
have hne := hne h
have hrev := hrev h
have hmore := hmore h
set t := res.keys.length with ht
set q := beginSlack r with hq
set p := (1 - r.end_.offset).toNat with hp
have ht1 : 1 ≤ t := by rw [ht]; exact List.length_pos_of_ne_nil hne
have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h
have htp : t + p ≤ q := by omega
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
set last := res.keys.getLast hne with hlast
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
(hsort.pairwise.imp le_of_lt).rel_getLast hx
have hcard : (((q + 1 - t) + 1 : ℕ) : ℕ∞) ≤
intervalCard ((keyAfter last : Key) : WithTop Key) (⊤ : WithTop Key) := by
rw [intervalCard_top_right (by simp)]
exact le_top
obtain ⟨dba, hdbaIn, hknot, hdbaLen⟩ :=
exists_db_keysIn_avoid (B := ((keyAfter last : Key) : WithTop Key))
(E := (⊤ : WithTop Key)) (k := k) (m := q + 1 - t) hcard
set G : List Key := dba.keys with hG
have hGlen : G.length = q + 1 - t := hdbaLen
have hGgt : ∀ x ∈ G, last < x := by
intro x hx
rw [hG] at hx
have hxge : ((keyAfter last : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) :=
(hdbaIn x hx).1
exact lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hxge)
have hDsort : (res.keys ++ G).SortedLT := by
refine List.sortedLT_append.mpr ⟨hsort, ?_, ?_⟩
· simpa [hG] using dba.sorted
· intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha) (hGgt b hb)
let dbw : DB := ⟨res.keys ++ G, hDsort⟩
have hdbw_keys : dbw.keys = res.keys ++ G := rfl
have hN : dbw.keys.length = q + 1 := by
rw [hdbw_keys, List.length_append, hGlen, ← ht]
omega
have hNp : p ≤ dbw.keys.length := by rw [hN]; omega
have hNq : beginSlack r < dbw.keys.length := by rw [hN, hq]; omega
have hck : RangeRead.completeKeys dbw r = res.keys ++ List.take (q + 1 - p - t) G := by
have h0 := completeKeys_eq_drop_take (db := dbw) hrev h.2.2.2.2.1 h.2.2.2.2.2.1
h.2.2.1 h.2.2.2.2.2.2 hNp hNq
rw [h0]
have hidx : dbw.keys.length - beginSlack r - 1 = 0 := by rw [hN, hq]; omega
have htake : beginSlack r + 1 - p = q + 1 - p := by rw [hq]
rw [hidx, htake, List.drop_zero, hdbw_keys, List.take_append,
List.take_of_length_le (by rw [← ht]; omega), ← ht]
have hpre : res.keys <+: RangeRead.completeKeys dbw r := by
rw [hck]; exact List.prefix_append _ _
have hcompw : RangeRead.Compatible dbw r res :=
⟨hcomp₀.1, Or.inl ⟨hmore, hpre, hne⟩⟩
have hk_not_res : k ∉ res.keys := fun hx => absurd (hle_last k hx) (not_le.mpr hkgt)
have hk_not_G : k ∉ G := by rw [hG]; exact hknot
have hkdb : k ∉ dbw.keys := by
rw [hdbw_keys]; simp [hk_not_res, hk_not_G]
have hforall : ∀ x ∈ res.keys, x < k := fun x hx => lt_of_le_of_lt (hle_last x hx) hkgt
have hins_keys : (dbw.insert k hkdb).keys =
res.keys ++ G.orderedInsert (· ≤ ·) k := by
rw [DB.insert_keys, hdbw_keys, orderedInsert_append_of_forall_lt hforall]
have hN' : (dbw.insert k hkdb).keys.length = q + 2 := by
rw [hins_keys, List.length_append, List.orderedInsert_length, hGlen, ← ht]
omega
have hNp' : p ≤ (dbw.insert k hkdb).keys.length := by rw [hN']; omega
have hNq' : beginSlack r < (dbw.insert k hkdb).keys.length := by rw [hN', hq]; omega
have hGins_gt : ∀ x ∈ G.orderedInsert (· ≤ ·) k, last < x := by
intro x hx
rcases (List.mem_orderedInsert (r := (· ≤ ·))).mp hx with rfl | hx
· exact hkgt
· exact hGgt x hx
have hckins : RangeRead.completeKeys (dbw.insert k hkdb) r =
List.take (q + 1 - p) (res.keys.tail ++ G.orderedInsert (· ≤ ·) k) := by
have h0 := completeKeys_eq_drop_take (db := dbw.insert k hkdb) hrev h.2.2.2.2.1
h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 hNp' hNq'
rw [h0]
have hidx : (dbw.insert k hkdb).keys.length - beginSlack r - 1 = 1 := by
rw [hN', hq]; omega
have htake : beginSlack r + 1 - p = q + 1 - p := by rw [hq]
rw [hidx, htake, hins_keys, List.drop_one, List.tail_append_of_ne_nil hne]
have htakeins : List.take (q + 1 - p) (res.keys.tail ++ G.orderedInsert (· ≤ ·) k) =
res.keys.tail ++ List.take (q + 1 - p - res.keys.tail.length)
(G.orderedInsert (· ≤ ·) k) := by
rw [List.take_append, List.take_of_length_le (by rw [List.length_tail, ← ht]; omega)]
have hresfil : res.keys.tail.filter (fun x : Key => decide (x ≤ last)) = res.keys.tail := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
exact hle_last x (List.mem_of_mem_tail hx)
have hGfil : (List.take (q + 1 - p - res.keys.tail.length)
(G.orderedInsert (· ≤ ·) k)).filter (fun x : Key => decide (x ≤ last)) = [] := by
rw [List.filter_eq_nil_iff]
intro x hx
simp only [Bool.not_eq_true, decide_eq_false_iff_not]
exact not_le.mpr (hGins_gt x (List.mem_of_mem_take hx))
have hfilins : (RangeRead.completeKeys (dbw.insert k hkdb) r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.tail := by
rw [hckins, ← hlast, htakeins, List.filter_append, hresfil, hGfil, List.append_nil]
have hnot : ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by
intro hc
have heq := ((compatible_iff_truncated_keys hrev hmore hne).mp hc).2
rw [hfilins] at heq
have hlen := congrArg List.length heq
rw [List.length_tail] at hlen
omega
exact ⟨dbw, hkdb, hcompw, hnot⟩
/-- Above the reported last, in the strict branch, every key is necessary. -/
theorem necessary_above_last_branchF {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1)
(hkgt : res.keys.getLast (hne h) < k) :
Necessary r res k := by
obtain ⟨dbw, hkdb, hcompw, hnot⟩ := exists_witness_above_last_branchF hread h hF hkgt
exact necessary_of_toggle ⟨dbw, hcompw, by
rw [toggle_eq_insert_of_not_mem hkdb]
exact hnot⟩
/-- **The strict branch is minimal with the full range `[⊥, ⊤)`.** -/
theorem minimal_full_of_branchF {r : RangeRead} {res : Result}
(hread : Readable r res) (h : negCrossTruncTopTop r res)
(hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1) :
Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by
classical
have hne := hne h
refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_fullRange r res, ?_⟩
intro k _ _
by_cases hkhead : ((k : Key) : WithTop Key) <
((res.keys.head hne : Key) : WithTop Key)
· exact necessary_below_head_branchF hread h hF hkhead
· have hhead_le : res.keys.head hne ≤ k :=
WithTop.coe_le_coe.mp (le_of_not_gt hkhead)
by_cases hklast : k ≤ res.keys.getLast hne
· exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne)
hhead_le hklast
· have hlast_lt_k : res.keys.getLast hne < k := lt_of_not_ge hklast
exact necessary_above_last_branchF hread h hF hlast_lt_k
end NegCrossTruncTopTop
/-- The equality branch of the both-sentinel truncated cell: the reported keys fill the
complete window exactly (`t + p = q + 1`). -/
def negCrossTruncTopTopEq (r : RangeRead) (res : Result) : Prop :=
negCrossTruncTopTop r res ∧
res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1
/-- The closed form of the equality branch: the reported keys are the complete window, so
the minimal range is `[head, ⊤)`. -/
noncomputable def closedFormNegCrossTruncTopTopEq (_r : RangeRead) (res : Result) :
ConflictRange :=
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩
/-- The default head of a nonempty list is its head. -/
theorem headD_eq_head {l : List Key} (h : l ≠ []) : l.headD ([] : Key) = l.head h := by
cases l with
| nil => exact absurd rfl h
| cons a t => rfl
/-- The equality branch is incompatible with `begin.key ≤ ↑k` for a genuine key. -/
theorem not_negCross_trunc_topTop_eq_of_begin_le_coe {r : RangeRead} {res : Result} {k : Key}
(h : r.begin.key ≤ ((k : Key) : WithTop Key)) : ¬ negCrossTruncTopTopEq r res := by
rintro ⟨htop, -⟩
have hB : r.begin.key = (⊤ : WithTop Key) := NegCrossTruncTopTop.hBtop htop
rw [hB] at h
exact absurd h (not_le.mpr (WithTop.coe_lt_top k))
/-- The equality branch is incompatible with a positive end offset. -/
theorem not_negCross_trunc_topTop_eq_of_one_le_end {r : RangeRead} {res : Result}
(h : 1 ≤ r.end_.offset) : ¬ negCrossTruncTopTopEq r res := by
rintro ⟨htop, -⟩
have hend : r.end_.offset ≤ 0 := NegCrossTruncTopTop.hend htop
omega
/-- The equality branch of the both-sentinel truncated cell is minimal with range
`[head, ⊤)`. -/
theorem minimal_closedForm_of_negCross_trunc_topTop_eq {r : RangeRead} {res : Result}
(hread : Readable r res) (h : negCrossTruncTopTopEq r res) :
Minimal r res (closedFormNegCrossTruncTopTopEq r res) := by
obtain ⟨htop, hH⟩ := h
have hne : res.keys ≠ [] := NegCrossTruncTopTop.hne htop
have hhead : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head hne
rw [closedFormNegCrossTruncTopTopEq, hhead]
exact NegCrossTruncTopTop.minimal_top_head_of_branchH hread htop hH
/-- The strict branch of the both-sentinel truncated cell: the reported keys leave room in
the complete window (`t + p < q + 1`). -/
def negCrossTruncTopTopStrict (r : RangeRead) (res : Result) : Prop :=
negCrossTruncTopTop r res ∧
res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1
/-- The closed form of the strict branch: the full range `[⊥, ⊤)`. -/
noncomputable def closedFormNegCrossTruncTopTopStrict (_r : RangeRead) (_res : Result) :
ConflictRange :=
⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩
/-- The strict branch of the both-sentinel truncated cell is minimal with the full range
`[⊥, ⊤)`. -/
theorem minimal_closedForm_of_negCross_trunc_topTop_strict {r : RangeRead} {res : Result}
(hread : Readable r res) (h : negCrossTruncTopTopStrict r res) :
Minimal r res (closedFormNegCrossTruncTopTopStrict r res) := by
obtain ⟨htop, hF⟩ := h
exact NegCrossTruncTopTop.minimal_full_of_branchF hread htop hF
/-- **The whole both-sentinel truncated cell is minimal with the compact closed form.** It
is `[head, ⊤)` when the reported keys fill the complete window, and the full range
otherwise. -/
theorem minimal_closedForm_of_negCross_trunc_topTop {r : RangeRead} {res : Result}
(hread : Readable r res) (h : negCrossTruncTopTop r res) :
Minimal r res (closedFormNegCrossTruncTopTop r res) := by
by_cases hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1
· rw [closedFormNegCrossTruncTopTop, ite_eq_left hH]
exact minimal_closedForm_of_negCross_trunc_topTop_eq hread ⟨h, hH⟩
· have hle := NegCrossTruncTopTop.readable_counts_topTop h hread
have hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1 := by omega
rw [closedFormNegCrossTruncTopTop, ite_eq_right hH]
exact NegCrossTruncTopTop.minimal_full_of_branchF hread h hF
end FormalConflictRange