- 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.
1477 lines
74 KiB
Lean4
1477 lines
74 KiB
Lean4
/-
|
||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
Authors: FormalConflictRange contributors
|
||
-/
|
||
import FormalConflictRange.NegCrossComplete
|
||
import FormalConflictRange.NegCrossLongNonpos
|
||
|
||
/-!
|
||
# The crossed negative-begin complete long clamp cell at a nonpositive end offset
|
||
|
||
A forward complete result with `end_.key < begin.key`, a negative begin offset and
|
||
`end.offset ≤ 0`, where the begin selector clamps. Unlike the short case, the gap
|
||
`[end_.key, begin.key)` lies *above* the end key, so agreement below `end_.key` does not
|
||
fix the gap. A compatible database stores `P` gap keys; the begin anchor is `P - C`
|
||
(with `C = beginSlack - reportedBelowBegin - clampNonposD`), so compatibility forces
|
||
`P ≤ C`. The upper endpoint of the minimal range is `end_.key` when no compatible
|
||
database can fill the gap (`cap < C`, or `cap = C` with the reported head at the empty
|
||
key), and `begin.key` otherwise.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
/-- The capacity slack `C = beginSlack - reportedBelowBegin - clampNonposD` of the
|
||
crossed negative-begin complete long clamp cell. -/
|
||
def clampLongC (r : RangeRead) (res : Result) : ℕ :=
|
||
beginSlack r - reportedBelowBegin r res - clampNonposD r
|
||
|
||
/-- The capacity `[end_.key, begin.key)` of the gap above the end key. -/
|
||
def clampLongCap (r : RangeRead) : ℕ∞ :=
|
||
intervalCard r.end_.key r.begin.key
|
||
|
||
/-- The upper endpoint of the crossed negative-begin complete long clamp cell. -/
|
||
noncomputable def clampLongUpper (r : RangeRead) (res : Result) : WithTop Key :=
|
||
if clampLongCap r < (clampLongC r res : ℕ∞) ∨
|
||
(clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = [])
|
||
then r.end_.key else r.begin.key
|
||
|
||
/-- The crossed negative-begin complete long clamp cell at `end.offset ≤ 0`. -/
|
||
abbrev negCrossClampLongNonpos (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
reportedBelowBegin r res ≤ beginSlack r ∧ clampNonposBottom r res ∧
|
||
r.end_.key < r.begin.key
|
||
|
||
/-- The closed form for the crossed negative-begin complete long clamp cell. -/
|
||
noncomputable def closedFormNegCrossClampLongNonpos (r : RangeRead) (res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), clampLongUpper r res⟩
|
||
|
||
section Structural
|
||
|
||
variable {r : RangeRead} {res : Result}
|
||
|
||
theorem clampLong_headD_eq {hne : res.keys ≠ []} :
|
||
res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
|
||
/-- Every reported key lies below `end_.key` in the crossed negative-begin complete
|
||
long clamp cell. -/
|
||
theorem clampLong_reported_lt_end (hread : Readable r res)
|
||
(_hrev : r.reverse = false) (hmore : res.more = false) (hend : r.end_.offset ≤ 0) :
|
||
∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
intro k hk
|
||
have hklt := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hk)).2.2
|
||
exact lt_of_lt_of_le hklt (resolve_le_key_of_offset_nonpos hend)
|
||
|
||
theorem clampLong_reported_eq_length (hread : Readable r res)
|
||
(hcell : negCrossClampLongNonpos r res) :
|
||
reportedBelowBegin r res = res.keys.length := by
|
||
obtain ⟨hrev, -, hmore, -, hend, -, -, -, hlong⟩ := hcell
|
||
unfold reportedBelowBegin
|
||
rw [List.countP_eq_length]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
exact lt_trans (clampLong_reported_lt_end hread hrev hmore hend k hk) hlong
|
||
|
||
theorem clampLong_nat_bottom (hcell : negCrossClampLongNonpos r res) :
|
||
reportedBelowBegin r res + (clampNonposD r + 1) ≤ beginSlack r := by
|
||
obtain ⟨-, -, -, -, -, -, -, hbottom, hlong⟩ := hcell
|
||
have hcap0 : intervalCard r.begin.key r.end_.key = 0 :=
|
||
intervalCard_eq_zero_of_le (le_of_lt hlong)
|
||
have h := hbottom
|
||
unfold clampNonposBottom at h
|
||
rw [hcap0] at h
|
||
have h' : ((reportedBelowBegin r res + (clampNonposD r + 1) : ℕ) : ℕ∞) ≤
|
||
(beginSlack r : ℕ∞) := by
|
||
simpa [ENat.natCast_add, add_assoc] using h
|
||
exact ENat.natCast_le_natCast.mp h'
|
||
|
||
theorem clampLong_C_pos (hcell : negCrossClampLongNonpos r res) :
|
||
0 < clampLongC r res := by
|
||
have hnat := clampLong_nat_bottom hcell
|
||
unfold clampLongC
|
||
omega
|
||
|
||
/-- The complete window of a compatible database is the half-open interval between the
|
||
resolutions. -/
|
||
theorem clampLong_complete_eq_between {db : DB} (hcomp : RangeRead.Compatible db r res)
|
||
(hrev : r.reverse = false) (hmore : res.more = false) :
|
||
res.keys = between db (resolve db r.begin) (resolve db r.end_) := by
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
rw [hres_eq, RangeRead.completeKeys, ite_eq_right (by simp [hrev])]
|
||
unfold RangeRead.inRange between
|
||
rfl
|
||
|
||
/-- The begin anchor in terms of the gap count, once the count below the begin
|
||
resolution is known. -/
|
||
theorem clampLong_anchor_eq {db : DB} (hread : Readable r res)
|
||
(hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res)
|
||
{b : ℕ} (hb : cnt db (resolve db r.begin) = b) :
|
||
r.begin.anchor db =
|
||
(b : ℤ) + ((between db r.end_.key r.begin.key).length : ℤ) -
|
||
(clampLongC r res : ℤ) := by
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, -, -, -, hlong⟩ := hcell
|
||
have hresb := clampLong_complete_eq_between hcomp hrev hmore
|
||
have hle : resolve db r.begin ≤ resolve db r.end_ := by
|
||
obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne
|
||
have hk' : k ∈ between db (resolve db r.begin) (resolve db r.end_) := hresb ▸ hk
|
||
unfold between at hk'
|
||
rw [List.mem_filter] at hk'
|
||
rcases (of_decide_eq_true hk'.2) with ⟨h1, h2⟩
|
||
exact le_of_lt (lt_of_le_of_lt h1 h2)
|
||
have ht : (between db (resolve db r.begin) (resolve db r.end_)).length = res.keys.length := by
|
||
rw [← hresb]
|
||
have hcntR : cnt db (resolve db r.end_) = b + res.keys.length := by
|
||
have h := cnt_eq_add_between db hle
|
||
rw [hb, ht] at h
|
||
omega
|
||
have hRle : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||
intro hR
|
||
have hhead : res.keys.head hne ∈ between db (resolve db r.begin) (resolve db r.end_) :=
|
||
hresb ▸ List.head_mem hne
|
||
unfold between at hhead
|
||
rw [List.mem_filter] at hhead
|
||
rw [hR] at hhead
|
||
exact absurd (of_decide_eq_true hhead.2).2
|
||
(not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne)))
|
||
have hd : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 :=
|
||
between_resolve_end_length hend hRne
|
||
have hcntE : cnt db r.end_.key = b + res.keys.length + (clampNonposD r + 1) := by
|
||
have h := cnt_eq_add_between db hRle
|
||
rw [hcntR, hd] at h
|
||
omega
|
||
have hEB : r.end_.key ≤ r.begin.key := le_of_lt hlong
|
||
have hcntB : cnt db r.begin.key =
|
||
b + res.keys.length + (clampNonposD r + 1) +
|
||
(between db r.end_.key r.begin.key).length := by
|
||
have h := cnt_eq_add_between db hEB
|
||
rw [hcntE] at h
|
||
omega
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
|
||
have hcntB' : db.keys.countP
|
||
(fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
b + res.keys.length + (clampNonposD r + 1) +
|
||
(between db r.end_.key r.begin.key).length := by
|
||
simpa [cnt] using hcntB
|
||
rw [hcntB']
|
||
have hrep : reportedBelowBegin r res = res.keys.length :=
|
||
clampLong_reported_eq_length hread hcell_keep
|
||
have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
|
||
rw [← hrep]
|
||
exact clampLong_nat_bottom hcell_keep
|
||
have hC : (clampLongC r res : ℤ) =
|
||
(beginSlack r : ℤ) - (res.keys.length : ℤ) - (clampNonposD r : ℤ) := by
|
||
unfold clampLongC
|
||
rw [hrep]
|
||
omega
|
||
rw [hC]
|
||
push_cast
|
||
ring
|
||
|
||
/-- In the long clamp cell the gap count equals the capacity slack for any compatible
|
||
database with a nonnegative begin anchor. Such a database is nonclamping, resolves the
|
||
begin selector onto the reported head, and its anchor is exactly the number of stored
|
||
keys below that head; the anchor identity then forces the gap count to `C`. -/
|
||
theorem clampLong_P_eq_C_of_anchor_nonneg {db : DB} (hread : Readable r res)
|
||
(hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hanchor : 0 ≤ r.begin.anchor db) :
|
||
(between db r.end_.key r.begin.key).length = clampLongC r res := by
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, -, -, -, -, -⟩ := hcell
|
||
set head : Key := res.keys.head hne with hhead
|
||
have hL : resolve db r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_anchor_nonneg hcomp hrev hmore hne hanchor
|
||
have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
|
||
rw [hL]; exact WithTop.coe_ne_top
|
||
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
|
||
by_contra h
|
||
exact hnot_top (resolve_eq_top hanchor (le_of_not_gt h))
|
||
have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor hltlen
|
||
have hidx : db.keys[(r.begin.anchor db).toNat] = head := by
|
||
have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) =
|
||
((head : Key) : WithTop Key) := by rw [← hget, hL]
|
||
exact WithTop.coe_injective hcoe
|
||
have hrank : db.keys.countP (fun k => decide (k < head)) = (r.begin.anchor db).toNat :=
|
||
countP_lt_eq_of_getElem db.sorted hltlen hidx
|
||
have hb : cnt db (resolve db r.begin) = (r.begin.anchor db).toNat := by
|
||
rw [hL]
|
||
unfold cnt
|
||
simpa using hrank
|
||
have hEq := clampLong_anchor_eq hread hcell_keep hcomp hb
|
||
have hEq' : (r.begin.anchor db : ℤ) =
|
||
((r.begin.anchor db).toNat : ℤ) +
|
||
((between db r.end_.key r.begin.key).length : ℤ) -
|
||
(clampLongC r res : ℤ) := hEq
|
||
rw [Int.toNat_of_nonneg hanchor] at hEq'
|
||
have hP : ((between db r.end_.key r.begin.key).length : ℤ) = (clampLongC r res : ℤ) := by
|
||
omega
|
||
exact_mod_cast hP
|
||
|
||
/-- In the long clamp cell a compatible database clamps exactly when its gap count is
|
||
below the capacity slack `C`. -/
|
||
theorem clampLong_anchor_neg_iff {db : DB} (hread : Readable r res)
|
||
(hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res) :
|
||
r.begin.anchor db < 0 ↔ (between db r.end_.key r.begin.key).length < clampLongC r res := by
|
||
constructor
|
||
· intro hneg
|
||
have hb : cnt db (resolve db r.begin) = 0 := by
|
||
rw [resolve_eq_nil hneg]; exact cnt_nil db
|
||
have hEq := clampLong_anchor_eq hread hcell hcomp hb
|
||
have hEq' : (r.begin.anchor db : ℤ) =
|
||
(0 : ℤ) + ((between db r.end_.key r.begin.key).length : ℤ) -
|
||
(clampLongC r res : ℤ) := hEq
|
||
have : ((between db r.end_.key r.begin.key).length : ℤ) < (clampLongC r res : ℤ) := by
|
||
omega
|
||
exact_mod_cast this
|
||
· intro hP
|
||
by_contra hcon
|
||
have hanchor : 0 ≤ r.begin.anchor db := le_of_not_gt hcon
|
||
have hEq := clampLong_P_eq_C_of_anchor_nonneg hread hcell hcomp hanchor
|
||
omega
|
||
|
||
/-- **A clamping compatible database exists in the long clamp cell.** Take the reported
|
||
keys and the `d + 1` pads that any compatible database stores in
|
||
`[resolve end, end_.key)`; no gap keys are stored, so the begin anchor is `-C < 0`. -/
|
||
theorem exists_clampLong_db {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res) :
|
||
∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by
|
||
have hread_keep : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by
|
||
rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2
|
||
have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||
intro hbot
|
||
rw [hbot] at hlast_lt_R₀
|
||
exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _))
|
||
set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hLdef
|
||
have hLlen : L.length = clampNonposD r + 1 := by
|
||
rw [hLdef]; exact between_resolve_end_length hend hR_ne_nil
|
||
have hLne : L ≠ [] := by rw [← List.length_pos_iff, hLlen]; omega
|
||
have hLsorted : L.SortedLT :=
|
||
List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)
|
||
have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hLdef] at hx
|
||
exact of_decide_eq_true (List.mem_filter.mp hx).2
|
||
have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne :=
|
||
fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hres_lt_L : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by
|
||
intro x hx y hy
|
||
have hxle : ((x : Key) : WithTop Key) ≤ ((res.keys.getLast hne : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (hlast_le x hx)
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (lt_of_le_of_lt hxle hlast_lt_R₀) (hLmem y hy).1)
|
||
have hAppSorted : (res.keys ++ L).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hsort, hLsorted, hres_lt_L⟩
|
||
set db : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdb
|
||
have hrep : reportedBelowBegin r res = res.keys.length :=
|
||
clampLong_reported_eq_length hread_keep ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq,
|
||
hbottom, hlong⟩
|
||
have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
|
||
rw [← hrep]
|
||
exact clampLong_nat_bottom ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩
|
||
have hslack : beginSlack r = res.keys.length + clampLongC r res + clampNonposD r := by
|
||
unfold clampLongC
|
||
rw [hrep]
|
||
omega
|
||
have hR_lt_E : resolve db₀ r.end_ < r.end_.key :=
|
||
lt_of_le_of_lt (hLmem _ (List.head_mem hLne)).1 (hLmem _ (List.head_mem hLne)).2
|
||
have hdcast : (clampNonposD r : ℤ) = -r.end_.offset := by
|
||
rw [clampNonposD_eq]
|
||
rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ -r.end_.offset by omega)]
|
||
have hslackcast : (beginSlack r : ℤ) = -r.begin.offset := by
|
||
unfold beginSlack
|
||
rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ 0 - r.begin.offset by omega)]
|
||
ring
|
||
have hcntE : cnt db r.end_.key = res.keys.length + (clampNonposD r + 1) := by
|
||
unfold cnt
|
||
rw [List.countP_eq_length_filter]
|
||
have hdbk : db.keys = res.keys ++ L := by rw [hdb]
|
||
rw [hdbk]
|
||
have hf : (res.keys ++ L).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = res.keys ++ L := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(lt_trans hlast_lt_R₀ hR_lt_E)
|
||
· exact (hLmem x hx).2
|
||
rw [hf, List.length_append, hLlen]
|
||
have hcntB : cnt db r.begin.key = res.keys.length + (clampNonposD r + 1) := by
|
||
unfold cnt
|
||
rw [List.countP_eq_length_filter]
|
||
have hdbk : db.keys = res.keys ++ L := by rw [hdb]
|
||
rw [hdbk]
|
||
have hf : (res.keys ++ L).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = res.keys ++ L := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact lt_trans (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(lt_trans hlast_lt_R₀ hR_lt_E)) hlong
|
||
· exact lt_trans (hLmem x hx).2 hlong
|
||
rw [hf, List.length_append, hLlen]
|
||
have hanchorE : r.end_.anchor db = (res.keys.length : ℤ) := by
|
||
unfold Selector.anchor
|
||
have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.end_.key)) =
|
||
res.keys.length + (clampNonposD r + 1) := by simpa [cnt] using hcntE
|
||
rw [hc]
|
||
push_cast
|
||
rw [hdcast]
|
||
ring
|
||
have hanchorE_nonneg : 0 ≤ r.end_.anchor db := by rw [hanchorE]; exact Int.natCast_nonneg _
|
||
have htoNatE : (r.end_.anchor db).toNat = res.keys.length := by
|
||
rw [hanchorE, Int.toNat_natCast]
|
||
have hltlenE : (r.end_.anchor db).toNat < db.keys.length := by
|
||
rw [htoNatE, hdb]
|
||
simp only [List.length_append]
|
||
omega
|
||
have hdbk : db.keys = res.keys ++ L := by rw [hdb]
|
||
have hRE : resolve db r.end_ = ((L.head hLne : Key) : WithTop Key) := by
|
||
rw [resolve_eq_getElem hanchorE_nonneg
|
||
(by rw [htoNatE, hdbk]; simp only [List.length_append]; omega)]
|
||
simp only [htoNatE, hdbk]
|
||
rw [List.getElem_append_right (show res.keys.length ≤ res.keys.length by omega)]
|
||
simp [List.head_eq_getElem]
|
||
have hanchorB : r.begin.anchor db < 0 := by
|
||
unfold Selector.anchor
|
||
have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length + (clampNonposD r + 1) := by simpa [cnt] using hcntB
|
||
rw [hc]
|
||
have hCpos : 0 < clampLongC r res :=
|
||
clampLong_C_pos ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩
|
||
have hbo : (r.begin.offset : ℤ) = -((res.keys.length + clampLongC r res +
|
||
clampNonposD r : ℕ) : ℤ) := by
|
||
omega
|
||
rw [hbo]
|
||
push_cast
|
||
omega
|
||
have hLB : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorB
|
||
have hcomplete : res.keys = RangeRead.completeKeys db r := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hLB, hRE, hdbk]
|
||
have hf : (res.keys ++ L).filter (fun k : Key => decide
|
||
((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) :
|
||
WithTop Key))) = res.keys := by
|
||
rw [List.filter_append]
|
||
have h1 : res.keys.filter (fun k : Key => decide
|
||
((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) :
|
||
WithTop Key))) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
refine ⟨coe_nil_le_key_withTop x, ?_⟩
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (hres_lt_L x hx _
|
||
(List.head_mem _))) le_rfl
|
||
have h2 : L.filter (fun k : Key => decide
|
||
((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) :
|
||
WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq, not_and]
|
||
intro _
|
||
exact not_lt.mpr (WithTop.coe_le_coe.mpr
|
||
((hLsorted.pairwise.imp le_of_lt).rel_head hx))
|
||
rw [h1, h2, List.append_nil]
|
||
rw [hf]
|
||
exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hcomplete⟩⟩, hanchorB⟩
|
||
|
||
/-- **A compatible database with prescribed below-head keys and gap keys.** Given a
|
||
sorted list `H` of keys below the reported head and a sorted list `G` of gap keys in
|
||
`[end_.key, begin.key)` of length exactly the capacity slack `C`, the concatenation
|
||
`H ++ reported ++ pads ++ G` (with the pads coming from any compatible database) is
|
||
compatible. Its begin anchor is `H.length`, so it resolves onto the reported head. -/
|
||
theorem exists_long_compat_db {r : RangeRead} {res : Result} {db₀ : DB}
|
||
(hcomp₀ : RangeRead.Compatible db₀ r res)
|
||
(hcell : negCrossClampLongNonpos r res)
|
||
(H G : List Key)
|
||
(hHsorted : H.SortedLT)
|
||
(hHlt : ∀ x ∈ H, x < res.keys.headD ([] : Key))
|
||
(hGsorted : G.SortedLT)
|
||
(hGlo : ∀ x ∈ G, r.end_.key ≤ ((x : Key) : WithTop Key))
|
||
(hGhi : ∀ x ∈ G, ((x : Key) : WithTop Key) < r.begin.key)
|
||
(hGlen : G.length = clampLongC r res) :
|
||
∃ db : DB, RangeRead.Compatible db r res ∧
|
||
db.keys = H ++ res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key ++ G := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by
|
||
rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2
|
||
have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||
intro hbot
|
||
rw [hbot] at hlast_lt_R₀
|
||
exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _))
|
||
have hR_le_E : resolve db₀ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hLdef
|
||
have hLlen : L.length = clampNonposD r + 1 := by
|
||
rw [hLdef]; exact between_resolve_end_length hend hR_ne_nil
|
||
have hLne : L ≠ [] := by rw [← List.length_pos_iff, hLlen]; omega
|
||
have hLsorted : L.SortedLT :=
|
||
List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)
|
||
have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hLdef] at hx
|
||
exact of_decide_eq_true (List.mem_filter.mp hx).2
|
||
have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne :=
|
||
fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hheadD_eq : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
have hhead_le : ∀ x ∈ res.keys, res.keys.head hne ≤ x :=
|
||
fun x hx => (hsort.pairwise.imp le_of_lt).rel_head hx
|
||
have hhead_lt_R : ((res.keys.head hne : Key) : WithTop Key) < resolve db₀ r.end_ :=
|
||
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hhead_le _ (List.getLast_mem hne)))
|
||
hlast_lt_R₀
|
||
have hhead_lt_E : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key :=
|
||
lt_of_lt_of_le hhead_lt_R hR_le_E
|
||
have hHlt' : ∀ x ∈ H, x < res.keys.head hne := by
|
||
intro x hx
|
||
have := hHlt x hx
|
||
rwa [hheadD_eq] at this
|
||
have hHltE : ∀ x ∈ H, ((x : Key) : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
exact lt_trans (WithTop.coe_lt_coe.mpr (hHlt' x hx)) hhead_lt_E
|
||
have hres_lt_L : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by
|
||
intro x hx y hy
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (lt_of_le_of_lt
|
||
(WithTop.coe_le_coe.mpr (hlast_le x hx)) hlast_lt_R₀) (hLmem y hy).1)
|
||
have hH_lt_res : ∀ x ∈ H, ∀ y ∈ res.keys, x < y := by
|
||
intro x hx y hy
|
||
exact lt_of_lt_of_le (hHlt' x hx) (hhead_le y hy)
|
||
have hHsorted_res : (H ++ res.keys).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hHsorted, hsort, hH_lt_res⟩
|
||
have hA_lt_L : ∀ x ∈ H ++ res.keys, ∀ y ∈ L, x < y := by
|
||
intro x hx y hy
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
|
||
(lt_trans (WithTop.coe_lt_coe.mpr (hHlt' x hx)) hhead_lt_R) (hLmem y hy).1)
|
||
· exact hres_lt_L x hx y hy
|
||
have hB_sorted : ((H ++ res.keys) ++ L).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hHsorted_res, hLsorted, hA_lt_L⟩
|
||
have hB_lt_G : ∀ x ∈ (H ++ res.keys) ++ L, ∀ y ∈ G, x < y := by
|
||
intro x hx y hy
|
||
have hyE := hGlo y hy
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· rcases List.mem_append.mp hx with hx | hx
|
||
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltE x hx) hyE)
|
||
· exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(lt_of_lt_of_le hlast_lt_R₀ (le_trans hR_le_E hyE)))
|
||
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hLmem x hx).2 hyE)
|
||
have hAppSorted : (((H ++ res.keys) ++ L) ++ G).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hB_sorted, hGsorted, hB_lt_G⟩
|
||
set B : List Key := (H ++ res.keys) ++ L with hBdef
|
||
set db : DB := ⟨B ++ G, hAppSorted⟩ with hdb
|
||
have hdbk : db.keys = B ++ G := by rw [hdb]
|
||
have hrep : reportedBelowBegin r res = res.keys.length :=
|
||
clampLong_reported_eq_length ⟨db₀, hcomp₀⟩
|
||
⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩
|
||
have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
|
||
rw [← hrep]
|
||
exact clampLong_nat_bottom ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩
|
||
have hslack : beginSlack r = res.keys.length + clampLongC r res + clampNonposD r := by
|
||
unfold clampLongC
|
||
rw [hrep]
|
||
omega
|
||
have hdcast : (clampNonposD r : ℤ) = -r.end_.offset := by
|
||
rw [clampNonposD_eq]
|
||
rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ -r.end_.offset by omega)]
|
||
have hslackcast : (beginSlack r : ℤ) = -r.begin.offset := by
|
||
unfold beginSlack
|
||
rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ 0 - r.begin.offset by omega)]
|
||
ring
|
||
have hBlength : B.length = H.length + res.keys.length + (clampNonposD r + 1) := by
|
||
rw [hBdef]
|
||
simp only [List.length_append]
|
||
rw [hLlen]
|
||
have hcntE : cnt db r.end_.key = B.length := by
|
||
unfold cnt
|
||
rw [List.countP_eq_length_filter, hdbk]
|
||
have hBf : B.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = B := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [hBdef] at hx
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· rcases List.mem_append.mp hx with hx | hx
|
||
· exact hHltE x hx
|
||
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(lt_of_lt_of_le hlast_lt_R₀ hR_le_E)
|
||
· exact (hLmem x hx).2
|
||
have hGf : G.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hGlo x hx)
|
||
rw [List.filter_append, hBf, hGf, List.append_nil]
|
||
have hcntB : cnt db r.begin.key = B.length + G.length := by
|
||
unfold cnt
|
||
rw [List.countP_eq_length_filter, hdbk]
|
||
have hBf : B.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = B := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [hBdef] at hx
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· rcases List.mem_append.mp hx with hx | hx
|
||
· exact lt_trans (hHltE x hx) hlong
|
||
· exact lt_trans (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(lt_of_lt_of_le hlast_lt_R₀ hR_le_E)) hlong
|
||
· exact lt_trans (hLmem x hx).2 hlong
|
||
have hGf : G.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = G := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hGhi x hx
|
||
rw [List.filter_append, hBf, hGf, List.length_append]
|
||
have hanchorE : r.end_.anchor db = ((H.length + res.keys.length : ℕ) : ℤ) := by
|
||
unfold Selector.anchor
|
||
have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.end_.key)) = B.length
|
||
:= by
|
||
simpa [cnt] using hcntE
|
||
rw [hc, hBlength]
|
||
push_cast
|
||
rw [hdcast]
|
||
ring
|
||
have htoNatE : (r.end_.anchor db).toNat = H.length + res.keys.length := by
|
||
rw [hanchorE]
|
||
exact Int.toNat_natCast _
|
||
have hanchorE_nonneg : 0 ≤ r.end_.anchor db := by rw [hanchorE]; exact Int.natCast_nonneg _
|
||
have hltlenE : (r.end_.anchor db).toNat < db.keys.length := by
|
||
rw [htoNatE, hdbk, List.length_append, hBlength]; omega
|
||
have hgetE : db.keys[(r.end_.anchor db).toNat]? = some (L.head hLne) := by
|
||
rw [htoNatE, hdbk]
|
||
rw [List.getElem?_append_left (show H.length + res.keys.length < B.length by
|
||
rw [hBlength]; omega)]
|
||
rw [hBdef]
|
||
rw [List.getElem?_append_right (show (H ++ res.keys).length ≤ H.length + res.keys.length by
|
||
simp)]
|
||
have hsub : H.length + res.keys.length - (H ++ res.keys).length = 0 := by simp
|
||
rw [hsub]
|
||
simp [List.head_eq_getElem]
|
||
have hvalE : db.keys[(r.end_.anchor db).toNat]'(hltlenE) = L.head hLne := by
|
||
apply Option.some.inj
|
||
rw [← List.getElem?_eq_getElem (h := hltlenE)]
|
||
exact hgetE
|
||
have hRE : resolve db r.end_ = ((L.head hLne : Key) : WithTop Key) := by
|
||
rw [resolve_eq_getElem hanchorE_nonneg hltlenE]
|
||
exact congrArg (fun z => ((z : Key) : WithTop Key)) hvalE
|
||
have hanchorB : r.begin.anchor db = ((H.length : ℕ) : ℤ) := by
|
||
unfold Selector.anchor
|
||
have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
B.length + G.length := by simpa [cnt] using hcntB
|
||
rw [hc]
|
||
have hbo : (r.begin.offset : ℤ) = -((res.keys.length + clampLongC r res +
|
||
clampNonposD r : ℕ) : ℤ) := by omega
|
||
rw [hbo, hBlength, hGlen]
|
||
push_cast
|
||
ring
|
||
have hltlenB : (r.begin.anchor db).toNat < db.keys.length := by
|
||
rw [hanchorB, Int.toNat_natCast, hdbk, List.length_append, hBlength]; omega
|
||
have hgetB : db.keys[(r.begin.anchor db).toNat]? = some (res.keys.head hne) := by
|
||
rw [hanchorB, Int.toNat_natCast, hdbk]
|
||
rw [List.getElem?_append_left (show H.length < B.length by rw [hBlength]; omega)]
|
||
rw [hBdef]
|
||
rw [List.getElem?_append_left (show H.length < (H ++ res.keys).length by
|
||
rw [List.length_append]; have := List.length_pos_of_ne_nil hne; omega)]
|
||
rw [List.getElem?_append_right (show H.length ≤ H.length by omega)]
|
||
rw [show H.length - H.length = 0 by omega]
|
||
rw [List.getElem?_eq_getElem (h := List.length_pos_iff.mpr hne)]
|
||
exact congrArg some (List.head_eq_getElem hne).symm
|
||
have hvalB : db.keys[(r.begin.anchor db).toNat]'(hltlenB) = res.keys.head hne := by
|
||
apply Option.some.inj
|
||
rw [← List.getElem?_eq_getElem (h := hltlenB)]
|
||
exact hgetB
|
||
have hLB : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
|
||
rw [resolve_eq_getElem (by rw [hanchorB]; exact Int.natCast_nonneg _) hltlenB]
|
||
exact congrArg (fun z => ((z : Key) : WithTop Key)) hvalB
|
||
have hcomplete : res.keys = RangeRead.completeKeys db r := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hLB, hRE, hdbk]
|
||
have hf : (B ++ G).filter (fun k : Key => decide
|
||
(((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = res.keys := by
|
||
rw [hBdef]
|
||
rw [List.filter_append, List.filter_append, List.filter_append]
|
||
have hHf : H.filter (fun k : Key => decide
|
||
(((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro h
|
||
exact not_le.mpr (hHlt' x hx) (WithTop.coe_le_coe.mp h.1)
|
||
have hresf : res.keys.filter (fun k : Key => decide
|
||
(((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact ⟨WithTop.coe_le_coe.mpr (hhead_le x hx),
|
||
WithTop.coe_lt_coe.mpr (hres_lt_L x hx _ (List.head_mem hLne))⟩
|
||
have hLf : L.filter (fun k : Key => decide
|
||
(((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_and.mpr (fun _ => not_lt.mpr (WithTop.coe_le_coe.mpr
|
||
((hLsorted.pairwise.imp le_of_lt).rel_head hx)))
|
||
have hGf : G.filter (fun k : Key => decide
|
||
(((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_and.mpr (fun _ => not_lt.mpr
|
||
(le_trans (le_of_lt (hLmem _ (List.head_mem hLne)).2) (hGlo x hx)))
|
||
rw [hHf, hresf, hLf, hGf]
|
||
simp
|
||
rw [hf]
|
||
exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hcomplete⟩⟩, by rw [hdbk, hBdef]⟩
|
||
|
||
/-- A sorted database that stores the empty key has the empty key as its first key. -/
|
||
theorem keys_getElem_zero_eq_nil_of_mem {db : DB} (h : ([] : Key) ∈ db.keys) :
|
||
db.keys[0]'(List.length_pos_of_mem h) = ([] : Key) := by
|
||
obtain ⟨i, hi, hidx⟩ := List.getElem_of_mem h
|
||
have hcount := countP_lt_eq_of_getElem db.sorted hi hidx
|
||
have hzero : db.keys.countP (fun k => decide (k < ([] : Key))) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro k _
|
||
simp
|
||
rw [hzero] at hcount
|
||
have hi0 : i = 0 := hcount.symm
|
||
subst i
|
||
exact hidx
|
||
|
||
/-- Under the short-gap condition every compatible database clamps the begin selector:
|
||
below the end key the short-gap condition rules out a full gap, and the empty-key
|
||
exception makes a nonclamping resolution land on the empty key anyway. -/
|
||
theorem resolve_begin_eq_nil_of_clampLong_short {db : DB} (hread : Readable r res)
|
||
(hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hbranch : clampLongCap r < (clampLongC r res : ℕ∞) ∨
|
||
(clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = [])) :
|
||
resolve db r.begin = (([] : Key) : WithTop Key) := by
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
by_cases hclamp : r.begin.anchor db < 0
|
||
· exact resolve_eq_nil hclamp
|
||
· have hnn : 0 ≤ r.begin.anchor db := le_of_not_gt hclamp
|
||
have hP := clampLong_P_eq_C_of_anchor_nonneg hread hcell_keep hcomp hnn
|
||
have hle : ((clampLongC r res : ℕ) : ℕ∞) ≤ clampLongCap r := by
|
||
rw [← hP]
|
||
simpa [clampLongCap] using
|
||
between_length_le_intervalCard (db := db) (E := r.end_.key) (B := r.begin.key)
|
||
(le_of_lt hlong)
|
||
rcases hbranch with hlt | ⟨heq, hhead⟩
|
||
· exact absurd (lt_of_le_of_lt hle hlt) (lt_irrefl _)
|
||
· rw [resolve_begin_eq_head_of_anchor_nonneg hcomp hrev hmore hne hnn,
|
||
← clampLong_headD_eq (hne := hne)]
|
||
exact congrArg (fun k : Key => ((k : Key) : WithTop Key)) hhead
|
||
|
||
/-- When the begin selector clamps, the count below the end key is the reported count
|
||
plus the `d + 1` end pads. -/
|
||
theorem clampLong_cnt_end_of_clamp {db : DB} (hcomp : RangeRead.Compatible db r res)
|
||
(hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hend : r.end_.offset ≤ 0) (hL : resolve db r.begin = (([] : Key) : WithTop Key)) :
|
||
cnt db r.end_.key = res.keys.length + (clampNonposD r + 1) := by
|
||
have hresb := clampLong_complete_eq_between hcomp hrev hmore
|
||
have ht : (between db (resolve db r.begin) (resolve db r.end_)).length = res.keys.length := by
|
||
rw [← hresb]
|
||
rw [hL] at ht
|
||
have hcntR : cnt db (resolve db r.end_) = res.keys.length := by
|
||
have h := cnt_eq_add_between db (w₁ := (([] : Key) : WithTop Key))
|
||
(w₂ := resolve db r.end_) (coe_nil_le_key_withTop _)
|
||
rw [cnt_nil, Nat.zero_add, ht] at h
|
||
exact h
|
||
have hRle : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||
intro hR
|
||
have hhead : res.keys.head hne ∈ between db (resolve db r.begin) (resolve db r.end_) :=
|
||
hresb ▸ List.head_mem hne
|
||
unfold between at hhead
|
||
rw [List.mem_filter] at hhead
|
||
rw [hL, hR] at hhead
|
||
exact absurd (of_decide_eq_true hhead.2).2
|
||
(not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne)))
|
||
have hd : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 :=
|
||
between_resolve_end_length hend hRne
|
||
have h := cnt_eq_add_between db hRle
|
||
rw [hcntR, hd] at h
|
||
exact h
|
||
|
||
/-- The begin anchor of any database in terms of its gap count, once the count below the
|
||
end key is known to be the reported count plus the end pads. -/
|
||
theorem clampLong_anchor_eq_of_cnt {db : DB} (hread : Readable r res)
|
||
(hcell : negCrossClampLongNonpos r res)
|
||
(hcnt : cnt db r.end_.key = res.keys.length + (clampNonposD r + 1)) :
|
||
r.begin.anchor db =
|
||
((between db r.end_.key r.begin.key).length : ℤ) - (clampLongC r res : ℤ) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
have hEB : r.end_.key ≤ r.begin.key := le_of_lt hlong
|
||
have hsplit := cnt_eq_add_between db hEB
|
||
have hcntB : db.keys.countP
|
||
(fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length + (clampNonposD r + 1) +
|
||
(between db r.end_.key r.begin.key).length := by
|
||
have h := hsplit
|
||
rw [hcnt] at h
|
||
unfold cnt at h
|
||
exact h
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcntB]
|
||
have hrep : reportedBelowBegin r res = res.keys.length :=
|
||
clampLong_reported_eq_length hread
|
||
⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩
|
||
have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
|
||
rw [← hrep]
|
||
exact clampLong_nat_bottom
|
||
⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩
|
||
have hC : (clampLongC r res : ℤ) =
|
||
(beginSlack r : ℤ) - (res.keys.length : ℤ) - (clampNonposD r : ℤ) := by
|
||
unfold clampLongC
|
||
rw [hrep]
|
||
omega
|
||
rw [hC]
|
||
push_cast
|
||
ring
|
||
|
||
/-- **Soundness of `[⊥, end_.key)` in the crossed negative-begin complete long clamp
|
||
cell.** Under the short-gap condition every database with the same keys below
|
||
`end_.key` also clamps the begin selector: the gap can contribute at most `cap ≤ C`
|
||
keys, and in the equality case the reported head is the empty key, which the agreed
|
||
prefix stores below `end_.key`. The end selector is fixed by agreement below
|
||
`end_.key`, and the complete window below it is then determined. -/
|
||
theorem sound_clampLong_nonpos_end {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res)
|
||
(hbranch : clampLongCap r < (clampLongC r res : ℕ∞) ∨
|
||
(clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = [])) :
|
||
Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
have hhead_eq : res.keys.headD ([] : Key) = res.keys.head hne :=
|
||
clampLong_headD_eq (hne := hne)
|
||
have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne
|
||
have step : ∀ db₁ db₂, AgreeOn (⟨(([] : Key) : WithTop Key), r.end_.key⟩ :
|
||
ConflictRange) db₁ db₂ → RangeRead.Compatible db₁ r res →
|
||
RangeRead.Compatible db₂ r res := by
|
||
intro db₁ db₂ hagree hcomp₁
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1
|
||
have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by
|
||
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hfilt : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
|
||
db₂.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 hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) :=
|
||
resolve_begin_eq_nil_of_clampLong_short hread hcell_keep hcomp₁ hbranch
|
||
have hcnt₁ : cnt db₁ r.end_.key = res.keys.length + (clampNonposD r + 1) :=
|
||
clampLong_cnt_end_of_clamp hcomp₁ hrev hmore hne hend hL₁
|
||
have hcnt₂ : cnt db₂ r.end_.key = res.keys.length + (clampNonposD r + 1) := by
|
||
rw [← hcnt₁]
|
||
unfold cnt
|
||
rw [List.countP_eq_length_filter, List.countP_eq_length_filter, hfilt]
|
||
have hanchor₂ : r.begin.anchor db₂ =
|
||
((between db₂ r.end_.key r.begin.key).length : ℤ) - (clampLongC r res : ℤ) :=
|
||
clampLong_anchor_eq_of_cnt hread hcell_keep hcnt₂
|
||
have hPle : ((between db₂ r.end_.key r.begin.key).length : ℕ∞) ≤ clampLongCap r :=
|
||
between_length_le_intervalCard (le_of_lt hlong)
|
||
have hL₂ : resolve db₂ r.begin = (([] : Key) : WithTop Key) := by
|
||
rcases hbranch with hlt | ⟨heq, hheadD⟩
|
||
· have hlt' : ((between db₂ r.end_.key r.begin.key).length : ℕ∞) <
|
||
(clampLongC r res : ℕ∞) := lt_of_le_of_lt hPle hlt
|
||
have hlt'' : (between db₂ r.end_.key r.begin.key).length < clampLongC r res :=
|
||
ENat.natCast_lt_natCast.mp hlt'
|
||
exact resolve_eq_nil (by rw [hanchor₂]; omega)
|
||
· have hPle' : (between db₂ r.end_.key r.begin.key).length ≤ clampLongC r res := by
|
||
have h := hPle
|
||
rw [heq] at h
|
||
exact ENat.natCast_le_natCast.mp h
|
||
have hAle : r.begin.anchor db₂ ≤ 0 := by rw [hanchor₂]; omega
|
||
have hhead_nil : res.keys.head hne = ([] : Key) := by
|
||
rw [← hhead_eq]; exact hheadD
|
||
have hltE : (([] : Key) : WithTop Key) < r.end_.key := by
|
||
rw [← hhead_nil]
|
||
exact clampLong_reported_lt_end hread hrev hmore hend _ hhead_mem
|
||
have hnil₁ : ([] : Key) ∈ db₁.keys := by
|
||
have hck : ([] : Key) ∈ RangeRead.completeKeys db₁ r := by
|
||
rw [← hres_eq, ← hhead_nil]
|
||
exact hhead_mem
|
||
revert hck
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev])]
|
||
intro hck
|
||
exact (List.mem_filter.mp hck).1
|
||
have hnil₂ : ([] : Key) ∈ db₂.keys := by
|
||
have hmem : ([] : Key) ∈
|
||
db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by
|
||
rw [List.mem_filter]
|
||
refine ⟨hnil₁, ?_⟩
|
||
rw [decide_eq_true_eq]
|
||
exact hltE
|
||
rw [hfilt] at hmem
|
||
exact (List.mem_filter.mp hmem).1
|
||
rcases lt_or_eq_of_le hAle with hneg | hzero
|
||
· exact resolve_eq_nil hneg
|
||
· have hpos : 0 < db₂.keys.length := List.length_pos_of_mem hnil₂
|
||
have hltlen : (r.begin.anchor db₂).toNat < db₂.keys.length := by
|
||
rw [hzero]; simpa using hpos
|
||
rw [resolve_eq_getElem (le_of_eq hzero.symm) hltlen]
|
||
have hz? : db₂.keys[(r.begin.anchor db₂).toNat]? = some ([] : Key) := by
|
||
rw [hzero, Int.toNat_zero]
|
||
rw [List.getElem?_eq_getElem (h := List.length_pos_of_mem hnil₂)]
|
||
exact congrArg some (keys_getElem_zero_eq_nil_of_mem hnil₂)
|
||
have hz : db₂.keys[(r.begin.anchor db₂).toNat]'(hltlen) = ([] : Key) := by
|
||
apply Option.some.inj
|
||
rw [← List.getElem?_eq_getElem (h := hltlen)]
|
||
exact hz?
|
||
exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hz
|
||
have hR : resolve db₁ r.end_ = resolve db₂ r.end_ :=
|
||
resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.end_)
|
||
hend le_rfl hfilt
|
||
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||
rw [completeKeys_eq_filter_of_end_nonpos (db := db₁) hrev hend,
|
||
completeKeys_eq_filter_of_end_nonpos (db := db₂) hrev hend,
|
||
hfilt, hL₁, hL₂, hR]
|
||
exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
|
||
/-- **Soundness of `[⊥, begin.key)` in the crossed negative-begin complete long clamp
|
||
cell.** Agreement below `begin.key` fixes both selector resolutions (both selector
|
||
keys are at or below it) and every complete window lies below `begin.key`. -/
|
||
theorem sound_clampLong_nonpos_begin {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hbegin : r.begin.offset < 0)
|
||
(hend : r.end_.offset ≤ 0) (hmore : res.more = false)
|
||
(hlong : r.end_.key < r.begin.key) :
|
||
Sound r res (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) := by
|
||
have step : ∀ db₁ db₂, AgreeOn (⟨(([] : Key) : WithTop Key), r.begin.key⟩ :
|
||
ConflictRange) db₁ db₂ → RangeRead.Compatible db₁ r res →
|
||
RangeRead.Compatible db₂ r res := by
|
||
intro db₁ db₂ hagree hcomp₁
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1
|
||
have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by
|
||
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hfilt : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by
|
||
have h := hagree
|
||
rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h
|
||
exact h
|
||
have hL : resolve db₁ r.begin = resolve db₂ r.begin :=
|
||
resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.begin.key) (s := r.begin)
|
||
(le_of_lt hbegin) le_rfl hfilt
|
||
have hR : resolve db₁ r.end_ = resolve db₂ r.end_ :=
|
||
resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.begin.key) (s := r.end_)
|
||
hend (le_of_lt hlong) hfilt
|
||
have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
have hP : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < 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_)) := by
|
||
have h₁ : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < resolve db₁ r.end_)) =
|
||
(db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key))).filter
|
||
(fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < resolve db₁ r.end_)) := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < resolve db₁ r.end_
|
||
· have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le
|
||
have hkB : (k : WithTop Key) < r.begin.key := lt_trans hkE hlong
|
||
simp [hk, hkB]
|
||
· simp [hk]
|
||
have h₂ : db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < resolve db₁ r.end_)) =
|
||
(db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key))).filter
|
||
(fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < resolve db₁ r.end_)) := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < resolve db₁ r.end_
|
||
· have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le
|
||
have hkB : (k : WithTop Key) < r.begin.key := lt_trans hkE hlong
|
||
simp [hk, hkB]
|
||
· simp [hk]
|
||
rw [h₁, h₂, hfilt]
|
||
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), ← hL, ← hR]
|
||
exact hP
|
||
exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
|
||
/-- A database of `m` keys in `[B, E)` that contains a prescribed key `k ∈ [B, E)`,
|
||
whenever the interval has exactly `m` keys. -/
|
||
theorem exists_db_keysIn_mem {B E : WithTop Key} {k : Key} {m : ℕ}
|
||
(hcard : (m : ℕ∞) = intervalCard B E) (hk : k ∈ keysIn B E) :
|
||
∃ db : DB, (∀ x ∈ db.keys, x ∈ keysIn B E) ∧ k ∈ db.keys ∧
|
||
db.keys.length = m := by
|
||
obtain ⟨db, hrange, hlen⟩ := exists_db_keysIn (B := B) (E := E) (m := m) (le_of_eq hcard)
|
||
have hkdb : k ∈ db.keys := by
|
||
by_contra hknot
|
||
have hsub : ((db.keys.toFinset ∪ {k} : Finset Key) : Set Key) ⊆ keysIn B E := by
|
||
intro x hx
|
||
simp only [Finset.mem_coe, Finset.mem_union, List.mem_toFinset,
|
||
Finset.mem_singleton] at hx
|
||
rcases hx with hx | rfl
|
||
· exact hrange x hx
|
||
· exact hk
|
||
have hcardle : ((db.keys.toFinset ∪ {k} : Finset Key).card : ℕ∞) ≤ intervalCard B E := by
|
||
rw [intervalCard_eq_encard_keysIn, ← Set.encard_coe_eq_coe_finsetCard]
|
||
exact Set.encard_le_encard hsub
|
||
have hcard' : (db.keys.toFinset ∪ {k} : Finset Key).card = m + 1 := by
|
||
rw [Finset.card_union_of_disjoint]
|
||
· rw [List.toFinset_card_of_nodup db.nodup, hlen]; simp
|
||
· rw [Finset.disjoint_singleton_right, List.mem_toFinset]
|
||
exact hknot
|
||
rw [hcard', ← hcard] at hcardle
|
||
exact absurd hcardle
|
||
(not_le.mpr (ENat.natCast_lt_natCast.mpr (Nat.lt_succ_self m)))
|
||
exact ⟨db, hrange, hkdb, hlen⟩
|
||
|
||
/-- A database whose keys are the reported keys followed by a nonempty tail, with begin
|
||
anchor one, is incompatible: the begin resolution moves to the second stored key, so the
|
||
reported head leaves the complete window. -/
|
||
theorem clampLong_not_compat_of_anchor_one {db : DB} {Tail : List Key}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res)
|
||
(hkeys : db.keys = res.keys ++ Tail) (hTail : Tail ≠ [])
|
||
(hanchor : r.begin.anchor db = 1) :
|
||
¬ RangeRead.Compatible db r res := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
have hlenpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne
|
||
have hlen : 1 < db.keys.length := by
|
||
rw [hkeys, List.length_append]
|
||
have := List.length_pos_of_ne_nil hTail
|
||
omega
|
||
have h0 : db.keys[0]'(by omega) = res.keys.head hne := by
|
||
have h0? : db.keys[0]? = some (res.keys.head hne) := by
|
||
rw [hkeys, List.getElem?_append_left hlenpos, ← List.head?_eq_getElem?,
|
||
List.head?_eq_some_head hne]
|
||
have hlen0 : 0 < db.keys.length := by omega
|
||
rw [List.getElem?_eq_getElem hlen0] at h0?
|
||
exact Option.some.inj h0?
|
||
have hstep : db.keys[0]'(by omega) < db.keys[1]'(by omega) :=
|
||
(db.sorted.getElem_lt_getElem_iff).mpr (by omega)
|
||
have hgt : ((res.keys.head hne : Key) : WithTop Key) <
|
||
((db.keys[1] : Key) : WithTop Key) := by
|
||
rw [← h0]
|
||
exact WithTop.coe_lt_coe.mpr hstep
|
||
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
|
||
rw [hanchor]; simpa using hlen
|
||
have hget : db.keys[(r.begin.anchor db).toNat]? = some (db.keys[1]'(by omega)) := by
|
||
rw [hanchor]
|
||
norm_num
|
||
have hval : db.keys[(r.begin.anchor db).toNat]'(hltlen) = db.keys[1]'(by omega) := by
|
||
apply Option.some.inj
|
||
rw [← List.getElem?_eq_getElem (h := hltlen)]
|
||
exact hget
|
||
have hres_begin : resolve db r.begin = ((db.keys[1] : Key) : WithTop Key) := by
|
||
rw [resolve_eq_getElem (by rw [hanchor]; norm_num) hltlen]
|
||
exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval
|
||
have hhead_lt_E : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key :=
|
||
clampLong_reported_lt_end hread hrev hmore hend _ (List.head_mem hne)
|
||
intro hcomp
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hnot : res.keys.head hne ∉ RangeRead.completeKeys db r := by
|
||
rw [completeKeys_eq_filter_of_end_nonpos (db := db) hrev hend]
|
||
intro hmem
|
||
simp only [List.mem_filter, decide_eq_true_eq] at hmem
|
||
have hle : ((db.keys[1] : Key) : WithTop Key) ≤
|
||
((res.keys.head hne : Key) : WithTop Key) := by
|
||
rw [← hres_begin]; exact hmem.2.1
|
||
exact absurd hle (not_le.mpr hgt)
|
||
exact hnot (by rw [← hres_eq]; exact List.head_mem hne)
|
||
|
||
/-- A database whose least key is the empty key, with begin anchor zero, is incompatible
|
||
with a complete result whose head is not the empty key: the begin resolution moves to the
|
||
empty key, which enters the complete window. -/
|
||
theorem clampLong_not_compat_of_anchor_zero_bottom {db : DB} {Tail : List Key}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res)
|
||
(hheadne : res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
(hkeys : db.keys = ([] : Key) :: Tail)
|
||
(hanchor : r.begin.anchor db = 0) :
|
||
¬ RangeRead.Compatible db r res := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
have hhead : res.keys.head hne ≠ ([] : Key) := by
|
||
rw [← clampLong_headD_eq (hne := hne)]; exact hheadne
|
||
have hbot_lt_head : ([] : Key) < res.keys.head hne := by
|
||
cases hh : res.keys.head hne with
|
||
| nil => exact absurd hh hhead
|
||
| cons a t => exact List.nil_lt_cons a t
|
||
have hnil_lt_E : (([] : Key) : WithTop Key) < r.end_.key := by
|
||
have hh := clampLong_reported_lt_end hread hrev hmore hend _ (List.head_mem hne)
|
||
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head)) hh
|
||
have hlen : 0 < db.keys.length := by rw [hkeys]; simp
|
||
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
|
||
rw [hanchor]; simpa using hlen
|
||
have hget : db.keys[(r.begin.anchor db).toNat]? = some ([] : Key) := by
|
||
rw [hanchor]
|
||
norm_num
|
||
rw [hkeys]
|
||
simp
|
||
have hval : db.keys[(r.begin.anchor db).toNat]'(hltlen) = ([] : Key) := by
|
||
apply Option.some.inj
|
||
rw [← List.getElem?_eq_getElem (h := hltlen)]
|
||
exact hget
|
||
have hres_begin : resolve db r.begin = (([] : Key) : WithTop Key) := by
|
||
rw [resolve_eq_getElem (by rw [hanchor]) hltlen]
|
||
exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval
|
||
intro hcomp
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db r := by
|
||
rw [← hres_eq]; exact List.head_mem hne
|
||
rw [completeKeys_eq_filter_of_end_nonpos (db := db) hrev hend] at hhead_ck
|
||
simp only [List.mem_filter, decide_eq_true_eq] at hhead_ck
|
||
have hhead_lt_resEnd : ((res.keys.head hne : Key) : WithTop Key) < resolve db r.end_ :=
|
||
hhead_ck.2.2
|
||
have hnil_lt_resEnd : (([] : Key) : WithTop Key) < resolve db r.end_ :=
|
||
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head)) hhead_lt_resEnd
|
||
have hnil_ck : ([] : Key) ∈ RangeRead.completeKeys db r := by
|
||
rw [completeKeys_eq_filter_of_end_nonpos (db := db) hrev hend]
|
||
rw [List.mem_filter]
|
||
refine ⟨?_, ?_⟩
|
||
· rw [List.mem_filter]
|
||
exact ⟨by rw [hkeys]; exact List.mem_cons_self ..
|
||
, by rw [decide_eq_true_eq]; exact hnil_lt_E⟩
|
||
· rw [decide_eq_true_eq]
|
||
exact ⟨by rw [hres_begin], hnil_lt_resEnd⟩
|
||
have hnil_not_res : ([] : Key) ∉ res.keys := by
|
||
intro h
|
||
have hle := head_le_of_sortedLT' (RangeRead.compatible_sortedLT hcomp hrev) hne h
|
||
exact absurd hle (not_le.mpr hbot_lt_head)
|
||
exact hnil_not_res (by rw [hres_eq]; exact hnil_ck)
|
||
|
||
/-- **Every gap key is necessary when the gap is strictly larger than the slack.** Use
|
||
a compatible database storing exactly `C` gap keys and avoiding `k`, then insert `k`:
|
||
the begin anchor rises to one, so the reported head leaves the complete window. -/
|
||
theorem clampLong_necessary_gap_of_cap_gt {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res)
|
||
(hgt : (clampLongC r res : ℕ∞) < clampLongCap r)
|
||
(hkE : r.end_.key ≤ ((k : Key) : WithTop Key))
|
||
(hkB : ((k : Key) : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
have hread_keep : Readable r res := hread
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hcard : ((clampLongC r res + 1 : ℕ) : ℕ∞) ≤ clampLongCap r := by
|
||
rw [Nat.cast_add, Nat.cast_one]
|
||
exact (ENat.add_one_le_iff (by simp)).mpr hgt
|
||
obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ :=
|
||
exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k)
|
||
(m := clampLongC r res) hcard
|
||
have hJlo : ∀ x ∈ dbJ.keys, r.end_.key ≤ ((x : Key) : WithTop Key) :=
|
||
fun x hx => (hJrange x hx).1
|
||
have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < r.begin.key :=
|
||
fun x hx => (hJrange x hx).2
|
||
obtain ⟨dbA, hAcomp, hAkeys⟩ :=
|
||
exists_long_compat_db hcomp₀ hcell_keep [] dbJ.keys List.sortedLT_nil
|
||
(by intro x hx; simp at hx) dbJ.sorted hJlo hJhi hJlen
|
||
have hres_lt_E := clampLong_reported_lt_end hread_keep hrev hmore hend
|
||
have hPads_lt : ∀ x ∈ between db₀ (resolve db₀ r.end_) r.end_.key,
|
||
((x : Key) : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
exact (of_decide_eq_true (List.mem_filter.mp hx).2).2
|
||
have hkA : k ∉ dbA.keys := by
|
||
rw [hAkeys]
|
||
intro hk
|
||
simp only [List.nil_append, List.mem_append] at hk
|
||
rcases hk with (hk | hk) | hk
|
||
· exact absurd hkE (not_le.mpr (hres_lt_E k hk))
|
||
· exact absurd hkE (not_le.mpr (hPads_lt k hk))
|
||
· exact hkJ hk
|
||
let Lb : List Key := dbJ.keys.orderedInsert (· ≤ ·) k
|
||
have hall_lt : ∀ a ∈ res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key, a < k := by
|
||
intro a ha
|
||
have haE : ((a : Key) : WithTop Key) < r.end_.key := by
|
||
rcases List.mem_append.mp ha with ha | ha
|
||
· exact hres_lt_E a ha
|
||
· exact hPads_lt a ha
|
||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le haE hkE)
|
||
have hBkeys : (toggle dbA k).keys =
|
||
res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key ++ Lb := by
|
||
rw [toggle_eq_insert_of_not_mem hkA, DB.insert_keys, hAkeys]
|
||
simp only [List.nil_append]
|
||
rw [orderedInsert_append_of_all_lt hall_lt]
|
||
have hLblo : ∀ x ∈ Lb, r.end_.key ≤ ((x : Key) : WithTop Key) := by
|
||
intro x hx
|
||
rw [List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hkE
|
||
· exact hJlo x hx
|
||
have hLbhi : ∀ x ∈ Lb, ((x : Key) : WithTop Key) < r.begin.key := by
|
||
intro x hx
|
||
rw [List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hkB
|
||
· exact hJhi x hx
|
||
have hLblen : Lb.length = clampLongC r res + 1 := by
|
||
simp [Lb, List.orderedInsert_length, hJlen]
|
||
have hpadlen : (between db₀ (resolve db₀ r.end_) r.end_.key).length =
|
||
clampNonposD r + 1 := by
|
||
have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by
|
||
rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := by
|
||
rw [← hres_eq₀]; exact List.head_mem hne
|
||
have hhead_lt_resEnd : ((res.keys.head hne : Key) : WithTop Key) < resolve db₀ r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hhead_ck).2.2
|
||
have hRne : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||
intro hR
|
||
rw [hR] at hhead_lt_resEnd
|
||
exact absurd hhead_lt_resEnd
|
||
(not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne)))
|
||
exact between_resolve_end_length hend hRne
|
||
have hcntB : (toggle dbA k).keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length + (clampNonposD r + 1) + Lb.length := by
|
||
rw [hBkeys]
|
||
rw [List.countP_append, List.countP_append]
|
||
have h1 : res.keys.countP
|
||
(fun x : Key => decide (((x : Key) : 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_lt_of_le (hres_lt_E x hx) (le_of_lt hlong)
|
||
have h2 : (between db₀ (resolve db₀ r.end_) r.end_.key).countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
(between db₀ (resolve db₀ r.end_) r.end_.key).length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact lt_trans (hPads_lt x hx) hlong
|
||
have h3 : Lb.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = Lb.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hLbhi x hx
|
||
rw [h1, h2, h3, hpadlen]
|
||
have hreplen : reportedBelowBegin r res = res.keys.length :=
|
||
clampLong_reported_eq_length hread_keep hcell_keep
|
||
have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
|
||
rw [← hreplen]; exact clampLong_nat_bottom hcell_keep
|
||
have hanchor : r.begin.anchor (toggle dbA k) = 1 := by
|
||
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcntB]
|
||
have hslack : beginSlack r =
|
||
res.keys.length + clampLongC r res + clampNonposD r := by
|
||
unfold clampLongC
|
||
rw [hreplen]
|
||
omega
|
||
rw [hLblen, hslack]
|
||
push_cast
|
||
ring
|
||
have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res :=
|
||
clampLong_not_compat_of_anchor_one (Tail := between db₀ (resolve db₀ r.end_) r.end_.key ++ Lb)
|
||
hread_keep hcell_keep (by rw [hBkeys, List.append_assoc]) (by
|
||
intro h
|
||
have hlen := congrArg List.length h
|
||
rw [List.length_append, hLblen] at hlen
|
||
simp at hlen) hanchor
|
||
exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩
|
||
|
||
/-- **Every gap key is necessary when the gap exactly matches the slack and the reported
|
||
head is not the empty key.** Use a compatible database with one bottom key below the
|
||
head and all gap keys stored, then erase `k`: the begin anchor returns to zero, the
|
||
resolution lands on the bottom key, which enters the complete window although it is not
|
||
reported. -/
|
||
theorem clampLong_necessary_gap_of_cap_eq {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res)
|
||
(hcap : clampLongCap r = (clampLongC r res : ℕ∞))
|
||
(hheadne : res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
(hkE : r.end_.key ≤ ((k : Key) : WithTop Key))
|
||
(hkB : ((k : Key) : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
have hread_keep : Readable r res := hread
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hhead : res.keys.head hne ≠ ([] : Key) := by
|
||
rw [← clampLong_headD_eq (hne := hne)]; exact hheadne
|
||
have hbot_lt_head : ([] : Key) < res.keys.head hne := by
|
||
cases hh : res.keys.head hne with
|
||
| nil => exact absurd hh hhead
|
||
| cons a t => exact List.nil_lt_cons a t
|
||
have hk_interval : k ∈ keysIn r.end_.key r.begin.key := ⟨hkE, hkB⟩
|
||
obtain ⟨dbJ, hJrange, hkJmem, hJlen⟩ :=
|
||
exists_db_keysIn_mem (B := r.end_.key) (E := r.begin.key) (k := k)
|
||
(m := clampLongC r res) (by unfold clampLongCap at hcap; exact hcap.symm) hk_interval
|
||
have hJlo : ∀ x ∈ dbJ.keys, r.end_.key ≤ ((x : Key) : WithTop Key) :=
|
||
fun x hx => (hJrange x hx).1
|
||
have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < r.begin.key :=
|
||
fun x hx => (hJrange x hx).2
|
||
have hnil_lt_head : (([] : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) :=
|
||
WithTop.coe_lt_coe.mpr hbot_lt_head
|
||
obtain ⟨dbA, hAcomp, hAkeys⟩ :=
|
||
exists_long_compat_db hcomp₀ hcell_keep [(([] : Key))] dbJ.keys
|
||
(sortedLT_singleton _)
|
||
(by
|
||
intro x hx
|
||
simp only [List.mem_singleton] at hx
|
||
subst x
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne :=
|
||
clampLong_headD_eq (hne := hne)
|
||
rw [hheadD]
|
||
exact hbot_lt_head)
|
||
dbJ.sorted hJlo hJhi hJlen
|
||
have hres_lt_E := clampLong_reported_lt_end hread_keep hrev hmore hend
|
||
have hPads_lt : ∀ x ∈ between db₀ (resolve db₀ r.end_) r.end_.key,
|
||
((x : Key) : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
exact (of_decide_eq_true (List.mem_filter.mp hx).2).2
|
||
have hkA : k ∈ dbA.keys := by
|
||
rw [hAkeys]
|
||
simp only [List.singleton_append]
|
||
exact List.mem_append.mpr (Or.inr hkJmem)
|
||
have hk_res : k ∉ res.keys := fun h => absurd hkE (not_le.mpr (hres_lt_E k h))
|
||
have hk_pads : k ∉ between db₀ (resolve db₀ r.end_) r.end_.key :=
|
||
fun h => absurd hkE (not_le.mpr (hPads_lt k h))
|
||
have hnil_ne_k : ([] : Key) ≠ k := by
|
||
intro h
|
||
have hlt : (([] : Key) : WithTop Key) < r.end_.key :=
|
||
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head))
|
||
(hres_lt_E _ (List.head_mem hne))
|
||
rw [h] at hlt
|
||
exact absurd hkE (not_le.mpr hlt)
|
||
have hnil_beq : ¬(([] : Key) == k) = true := by
|
||
rw [beq_iff_eq]; exact hnil_ne_k
|
||
have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res := by
|
||
have hBkeys : (toggle dbA k).keys = ([] : Key) ::
|
||
(res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key ++ dbJ.keys.erase k) := by
|
||
rw [toggle_eq_erase_of_mem hkA, DB.erase_keys, hAkeys]
|
||
simp only [List.cons_append, List.nil_append]
|
||
rw [List.erase_cons_tail hnil_beq]
|
||
congr 1
|
||
rw [List.erase_append_right (l₁ := res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key)
|
||
(a := k) dbJ.keys]
|
||
intro h
|
||
rcases List.mem_append.mp h with h | h
|
||
· exact hk_res h
|
||
· exact hk_pads h
|
||
have hanchor : r.begin.anchor (toggle dbA k) = 0 := by
|
||
have hpadlen : (between db₀ (resolve db₀ r.end_) r.end_.key).length =
|
||
clampNonposD r + 1 := by
|
||
have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by
|
||
rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := by
|
||
rw [← hres_eq₀]; exact List.head_mem hne
|
||
have hhead_lt_resEnd : ((res.keys.head hne : Key) : WithTop Key) <
|
||
resolve db₀ r.end_ := (RangeRead.mem_completeKeys.mp hhead_ck).2.2
|
||
have hRne : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||
intro hR
|
||
rw [hR] at hhead_lt_resEnd
|
||
exact absurd hhead_lt_resEnd
|
||
(not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne)))
|
||
exact between_resolve_end_length hend hRne
|
||
have herase_len : (dbJ.keys.erase k).length = clampLongC r res - 1 := by
|
||
rw [List.length_erase_of_mem hkJmem, hJlen]
|
||
-- count below begin.key after erasure
|
||
have hcntB : (toggle dbA k).keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length + (clampNonposD r + 1) + clampLongC r res := by
|
||
rw [hBkeys, List.countP_cons_of_pos]
|
||
· rw [List.countP_append, List.countP_append]
|
||
have h1 : res.keys.countP
|
||
(fun x : Key => decide (((x : Key) : 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_lt_of_le (hres_lt_E x hx) (le_of_lt hlong)
|
||
have h2 : (between db₀ (resolve db₀ r.end_) r.end_.key).countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
(between db₀ (resolve db₀ r.end_) r.end_.key).length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact lt_trans (hPads_lt x hx) hlong
|
||
have h3 : (dbJ.keys.erase k).countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
(dbJ.keys.erase k).length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hJhi x (List.mem_of_mem_erase hx)
|
||
rw [h1, h2, h3, hpadlen, herase_len]
|
||
have hCpos : 0 < clampLongC r res := clampLong_C_pos hcell_keep
|
||
omega
|
||
· rw [decide_eq_true_eq]
|
||
have hlt : (([] : Key) : WithTop Key) <
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) := by
|
||
rw [clampLong_headD_eq (hne := hne)]
|
||
exact WithTop.coe_lt_coe.mpr hbot_lt_head
|
||
exact lt_trans hlt hcrossD
|
||
have hreplen : reportedBelowBegin r res = res.keys.length :=
|
||
clampLong_reported_eq_length hread_keep hcell_keep
|
||
have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
|
||
rw [← hreplen]; exact clampLong_nat_bottom hcell_keep
|
||
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcntB]
|
||
have hslack : beginSlack r =
|
||
res.keys.length + clampLongC r res + clampNonposD r := by
|
||
unfold clampLongC
|
||
rw [hreplen]
|
||
have hle : res.keys.length + clampNonposD r ≤ beginSlack r := by omega
|
||
omega
|
||
rw [hslack]
|
||
omega
|
||
exact clampLong_not_compat_of_anchor_zero_bottom hread_keep hcell_keep hheadne
|
||
hBkeys hanchor
|
||
exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩
|
||
|
||
/-- **Every key below the end key is necessary in the crossed negative-begin complete
|
||
long clamp cell.** Below the reported head it is necessary because a clamping witness
|
||
stores nothing there; between reported keys it is convex; above the last reported key it
|
||
is fixed by the complete window. -/
|
||
theorem necessary_below_end_clampLong {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res) {k : Key}
|
||
(hkE : ((k : Key) : WithTop Key) < r.end_.key) :
|
||
Necessary r res k := by
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)
|
||
· obtain ⟨db, hcomp, hneg⟩ := exists_clampLong_db hread hcell_keep
|
||
exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hneg hkhead
|
||
· have hkhead_le : ((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
|
||
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)
|
||
(WithTop.coe_le_coe.mp hkhead_le) hklast
|
||
· have hkgt : res.keys.getLast hne < k := lt_of_not_ge hklast
|
||
exact fun cr hs => necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs
|
||
(le_of_lt hkgt) hkE
|
||
|
||
/-- **Minimality of the crossed negative-begin complete long clamp cell.** -/
|
||
theorem minimal_closedForm_of_negCross_clamp_long_nonpos {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonpos r res) :
|
||
Minimal r res (closedFormNegCrossClampLongNonpos r res) := by
|
||
have hcell_keep : negCrossClampLongNonpos r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
by_cases hb : clampLongCap r < (clampLongC r res : ℕ∞) ∨
|
||
(clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = [])
|
||
· have hcr : closedFormNegCrossClampLongNonpos r res =
|
||
(⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by
|
||
unfold closedFormNegCrossClampLongNonpos clampLongUpper
|
||
rw [ite_eq_left hb]
|
||
rw [hcr]
|
||
exact minimal_iff_sound_and_necessaryRange.mpr
|
||
⟨sound_clampLong_nonpos_end hread hcell_keep hb,
|
||
fun k _ hk => necessary_below_end_clampLong hread hcell_keep hk⟩
|
||
· have hcr : closedFormNegCrossClampLongNonpos r res =
|
||
(⟨(([] : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) := by
|
||
unfold closedFormNegCrossClampLongNonpos clampLongUpper
|
||
rw [ite_eq_right hb]
|
||
rw [hcr]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr
|
||
⟨sound_clampLong_nonpos_begin hrev hbegin hend hmore hlong, ?_⟩
|
||
intro k _ hkB
|
||
by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key
|
||
· exact necessary_below_end_clampLong hread hcell_keep hkE
|
||
· have hkE_le : r.end_.key ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkE
|
||
have hle : (clampLongC r res : ℕ∞) ≤ clampLongCap r := by
|
||
by_contra h
|
||
exact hb (Or.inl (lt_of_not_ge h))
|
||
rcases lt_or_eq_of_le hle with hlt | heq
|
||
· exact clampLong_necessary_gap_of_cap_gt hread hcell_keep hlt hkE_le hkB
|
||
· have hheadne : res.keys.headD ([] : Key) ≠ [] := by
|
||
intro h
|
||
exact hb (Or.inr ⟨heq.symm, h⟩)
|
||
exact clampLong_necessary_gap_of_cap_eq hread hcell_keep heq.symm hheadne hkE_le hkB
|
||
|
||
end Structural
|
||
|
||
/-- The reversed crossed negative-begin complete long clamp cell at `end.offset ≤ 0`. -/
|
||
abbrev negCrossClampLongNonposRev (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
reportedBelowBegin r res ≤ beginSlack r ∧ clampNonposBottom r res ∧
|
||
r.end_.key < r.begin.key
|
||
|
||
/-- The reversed closed form for the crossed negative-begin complete long clamp cell. -/
|
||
noncomputable def closedFormNegCrossClampLongNonposRev (r : RangeRead) (res : Result) :
|
||
ConflictRange :=
|
||
closedFormNegCrossClampLongNonpos { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false }
|
||
|
||
/-- **The reversed crossed negative-begin complete long clamp cell is minimal**, by
|
||
flipping the traversal. -/
|
||
theorem minimal_closedForm_of_negCross_clamp_long_nonpos_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampLongNonposRev r res) :
|
||
Minimal r res (closedFormNegCrossClampLongNonposRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by
|
||
rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse]
|
||
have hcount : reportedBelowBegin { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by
|
||
unfold reportedBelowBegin
|
||
exact List.Perm.countP_eq _ (List.reverse_perm res.keys)
|
||
have hbottom' : clampNonposBottom { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } ↔ clampNonposBottom r res := by
|
||
unfold clampNonposBottom
|
||
rw [hcount]
|
||
rfl
|
||
have hcell' : negCrossClampLongNonpos { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, ?_, hlong⟩
|
||
· rw [hheadD]; exact hcrossD
|
||
· rw [hcount]; exact hmq
|
||
· exact hbottom'.mpr hbottom
|
||
have hread' : Readable { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩
|
||
have hmin := minimal_closedForm_of_negCross_clamp_long_nonpos hread' hcell'
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
end FormalConflictRange
|