- 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.
1229 lines
62 KiB
Lean4
1229 lines
62 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.NegCrossBottom
|
||
|
||
/-!
|
||
# The crossed negative-begin complete nonpositive-end clamp cell
|
||
|
||
A forward complete result with a negative begin offset, `end.offset ≤ 0`, the begin key
|
||
at or below the end key but with an infinite capacity in `[begin.key, end.key)`, and
|
||
reported keys below `begin.key` within the begin slack. The begin selector clamps to the
|
||
empty key, so the minimal conflict range is `[⊥, end_.key)`. The reversed traversal is
|
||
the flip of this cell.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
/-- A forward complete result with a negative begin offset, `end.offset ≤ 0`, the begin
|
||
key at or below the end key but with infinite capacity between them, and reported keys
|
||
below the begin key within the begin slack. -/
|
||
abbrev negCrossClampNonposInfinite (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧
|
||
intervalCard r.begin.key r.end_.key = ⊤ ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
reportedBelowBegin r res ≤ beginSlack r
|
||
|
||
/-- The closed form for the infinite-capacity negative-begin complete clamp cell:
|
||
`[⊥, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossClampNonposInfinite (r : RangeRead) (_res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **Soundness of the infinite-capacity negative-begin complete clamp cell.** Agreement
|
||
on `[⊥, end_.key)` fixes all keys below the end key, hence both resolutions and the whole
|
||
complete window, because `begin.key ≤ end_.key`. -/
|
||
theorem sound_clampNonposInfinite {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hbegin : r.begin.offset < 0)
|
||
(hend : r.end_.offset ≤ 0) (hmore : res.more = false)
|
||
(hB_le_E : r.begin.key ≤ r.end_.key) :
|
||
Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by
|
||
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 hfiltE : 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 = resolve db₂ r.begin :=
|
||
resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.begin)
|
||
(le_of_lt hbegin) hB_le_E hfiltE
|
||
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 hfiltE
|
||
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.end_.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
|
||
simp [hk, hkE]
|
||
· 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.end_.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
|
||
simp [hk, hkE]
|
||
· simp [hk]
|
||
rw [h₁, h₂, hfiltE]
|
||
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⟩
|
||
|
||
|
||
theorem intervalCard_keyAfter_eq_top_of_eq_top {B : Key} {E : WithTop Key}
|
||
(h : intervalCard (B : WithTop Key) E = ⊤) :
|
||
intervalCard ((keyAfter B : Key) : WithTop Key) E = ⊤ := by
|
||
have hBltE : (B : WithTop Key) < E := by
|
||
rcases lt_or_ge (B : WithTop Key) E with hlt | hge
|
||
· exact hlt
|
||
· rw [intervalCard_eq_zero_of_le hge] at h
|
||
exact absurd h (by simp)
|
||
have hle1 : (B : WithTop Key) ≤ ((keyAfter B : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt B))
|
||
have hle2 : ((keyAfter B : Key) : WithTop Key) ≤ E := keyAfter_le_of_lt hBltE
|
||
have hadd := intervalCard_add hle1 hle2
|
||
have hone : intervalCard (B : WithTop Key) ((keyAfter B : Key) : WithTop Key) = 1 := by
|
||
rw [keyAfter]
|
||
simpa using intervalCard_succChain B 1
|
||
have hsum : (1 : ℕ∞) + intervalCard ((keyAfter B : Key) : WithTop Key) E = ⊤ := by
|
||
have := hadd.symm.trans h
|
||
simpa [hone] using this
|
||
rcases ENat.add_eq_top.mp hsum with h1 | h2
|
||
· exact absurd h1 (by simp)
|
||
· exact h2
|
||
|
||
/-- The buffer size `d = -end.offset`. -/
|
||
def clampNonposD (r : RangeRead) : ℕ := (0 - r.end_.offset).toNat
|
||
|
||
theorem clampNonposD_eq (r : RangeRead) : clampNonposD r = (-r.end_.offset).toNat := by
|
||
unfold clampNonposD; rw [zero_sub]
|
||
|
||
/-- Whether a clamping compatible database can exist in the `end.offset ≤ 0` crossed
|
||
complete cell. -/
|
||
def clampNonposBottom (r : RangeRead) (res : Result) : Prop :=
|
||
(reportedBelowBegin r res : ℕ∞) +
|
||
((((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key) ≤
|
||
(beginSlack r : ℕ∞)
|
||
|
||
instance instDecidableClampNonposBottom (r : RangeRead) (res : Result) :
|
||
Decidable (clampNonposBottom r res) := by
|
||
unfold clampNonposBottom
|
||
infer_instance
|
||
|
||
/-- The stored keys in `[resolve end, end.key)` number `d+1` at a nonpositive end
|
||
offset. -/
|
||
theorem between_resolve_end_length {db : DB} {r : RangeRead}
|
||
(hend : r.end_.offset ≤ 0)
|
||
(hne : resolve db r.end_ ≠ (([] : Key) : WithTop Key)) :
|
||
(between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := by
|
||
set d := clampNonposD r with hd
|
||
set L := db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))
|
||
with hL
|
||
have hLsorted : L.SortedLT :=
|
||
List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
have hdval : (d : ℤ) = -r.end_.offset := by
|
||
rw [hd, clampNonposD_eq]
|
||
rw [Int.toNat_of_nonneg (by omega)]
|
||
have hrev := resolve_eq_filter_lt_reverse (db := db) (s := r.end_) hend
|
||
rw [← hL, ← clampNonposD_eq, ← hd] at hrev
|
||
cases hsome : L.reverse[d]? with
|
||
| none =>
|
||
rw [hsome] at hrev
|
||
change resolve db r.end_ = (([] : Key) : WithTop Key) at hrev
|
||
exact absurd hrev hne
|
||
| some qv =>
|
||
rw [hsome] at hrev
|
||
change resolve db r.end_ = ((qv : Key) : WithTop Key) at hrev
|
||
have hqv : resolve db r.end_ = ((qv : Key) : WithTop Key) := hrev
|
||
obtain ⟨hdlt, hget⟩ := List.getElem?_eq_some_iff.mp hsome
|
||
have hdlt' : d < L.length := by simpa using hdlt
|
||
have hrev2 : L.reverse[d]? = L[L.length - 1 - d]? :=
|
||
List.getElem?_reverse (l := L) (i := d) hdlt'
|
||
rw [hsome] at hrev2
|
||
obtain ⟨hilt, higet⟩ := List.getElem?_eq_some_iff.mp hrev2.symm
|
||
have hcount : L.countP (fun k : Key => decide (k < qv)) = L.length - 1 - d :=
|
||
countP_lt_eq_of_getElem hLsorted hilt higet
|
||
-- length of the filter of keys at or above qv
|
||
have hfilt : L.filter (fun k : Key => decide (qv ≤ k)) =
|
||
L.filter (fun k : Key => decide (¬ (((k : Key) : WithTop Key) < ((qv : Key) : WithTop
|
||
Key)))) := by
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => not_lt.mpr (WithTop.coe_le_coe.mpr h),
|
||
fun h => WithTop.coe_le_coe.mp (le_of_not_gt h)⟩
|
||
have hdrop := filter_not_lt_eq_drop_countP (l := L) hLsorted ((qv : Key) : WithTop Key)
|
||
have hcnt' : L.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < ((qv : Key) : WithTop Key))) =
|
||
L.length - 1 - d := by
|
||
rw [← hcount]
|
||
apply List.countP_congr
|
||
intro k _
|
||
rw [decide_eq_true_eq, decide_eq_true_eq]
|
||
exact WithTop.coe_lt_coe
|
||
have hlen : (L.filter (fun k : Key => decide (qv ≤ k))).length = d + 1 := by
|
||
rw [hfilt, hdrop, List.length_drop, hcnt']
|
||
omega
|
||
have hbetween : between db (resolve db r.end_) r.end_.key =
|
||
L.filter (fun k : Key => decide (qv ≤ k)) := by
|
||
rw [hqv]
|
||
unfold between
|
||
rw [hL, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
have h1 : decide (((qv : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) =
|
||
decide (qv ≤ k) := by
|
||
rw [decide_eq_decide]
|
||
exact WithTop.coe_le_coe
|
||
rw [Bool.decide_and]
|
||
rw [h1]
|
||
rw [hbetween, hlen]
|
||
|
||
|
||
|
||
theorem exists_clamp_compat_nonpos_infinite {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hend : r.end_.offset ≤ 0) (_hB_le_E : r.begin.key ≤ r.end_.key)
|
||
(hinf : intervalCard r.begin.key r.end_.key = ⊤)
|
||
(_hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key)
|
||
(hclamp : reportedBelowBegin r res ≤ beginSlack r) :
|
||
∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne :=
|
||
fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by
|
||
cases h : r.begin.key with
|
||
| top => rw [h, intervalCard_top_left] at hinf; exact absurd hinf (by simp)
|
||
| coe b => exact ⟨b, rfl⟩
|
||
rw [hb] at hinf
|
||
have hb_lt_E : (b : WithTop Key) < r.end_.key := by
|
||
rcases lt_or_ge (b : WithTop Key) r.end_.key with hlt | hge
|
||
· exact hlt
|
||
· rw [intervalCard_eq_zero_of_le hge] at hinf
|
||
exact absurd hinf (by simp)
|
||
by_cases hlastB : res.keys.getLast hne < b
|
||
· -- Option A: every reported key lies below `b`.
|
||
have hrepA : reportedBelowBegin r res = res.keys.length := by
|
||
unfold reportedBelowBegin
|
||
rw [hb]
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (hlast_le x hx) hlastB)
|
||
have hcard : ((clampNonposD r : ℕ) : ℕ∞) ≤
|
||
intervalCard ((keyAfter b : Key) : WithTop Key) r.end_.key := by
|
||
have hka := intervalCard_keyAfter_eq_top_of_eq_top (B := b) hinf
|
||
rw [hka]; exact le_top
|
||
obtain ⟨pad, hpadIn, hpadlen⟩ :=
|
||
exists_db_keysIn (B := ((keyAfter b : Key) : WithTop Key)) (E := r.end_.key)
|
||
(m := clampNonposD r) hcard
|
||
set F : List Key := pad.keys with hF
|
||
have hFsorted : F.SortedLT := pad.sorted
|
||
have hFmem : ∀ x ∈ F, ((keyAfter b : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx
|
||
have hFb : ∀ x ∈ F, b < x := by
|
||
intro x hx
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt b)) (hFmem x hx).1)
|
||
have hFlen : F.length = clampNonposD r := hpadlen
|
||
have hresb : ∀ x ∈ res.keys, x < b :=
|
||
fun x hx => lt_of_le_of_lt (hlast_le x hx) hlastB
|
||
have hbF_sorted : (b :: F).SortedLT := by
|
||
rw [List.sortedLT_cons]
|
||
exact ⟨fun y hy => hFb y hy, hFsorted⟩
|
||
have hcrossA : ∀ x ∈ res.keys, ∀ y ∈ b :: F, x < y := by
|
||
intro x hx y hy
|
||
rcases List.mem_cons.mp hy with hy | hy
|
||
· rw [hy]; exact hresb x hx
|
||
· exact lt_trans (hresb x hx) (hFb y hy)
|
||
have hAppSorted : (res.keys ++ (b :: F)).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hsort, hbF_sorted, hcrossA⟩
|
||
set dbA : DB := ⟨res.keys ++ (b :: F), hAppSorted⟩ with hdbA
|
||
have hfiltE : dbA.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
|
||
res.keys ++ (b :: F) := by
|
||
rw [hdbA]
|
||
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 (WithTop.coe_lt_coe.mpr hlastB) hb_lt_E)
|
||
· rcases List.mem_cons.mp hx with hx | hx
|
||
· rw [hx]; exact hb_lt_E
|
||
· exact (hFmem x hx).2
|
||
have hrevlist : (res.keys ++ (b :: F)).reverse =
|
||
F.reverse ++ (b :: res.keys.reverse) := by
|
||
rw [List.reverse_append, List.reverse_cons, List.append_assoc]
|
||
rfl
|
||
have hgetd : (res.keys ++ (b :: F)).reverse[clampNonposD r]? = some b := by
|
||
rw [hrevlist, List.getElem?_append]
|
||
have hFl : F.reverse.length = clampNonposD r := by rw [List.length_reverse, hFlen]
|
||
rw [ite_eq_right (by omega : ¬ clampNonposD r < F.reverse.length)]
|
||
simp [hFl]
|
||
have hR_A : resolve dbA r.end_ = ((b : Key) : WithTop Key) := by
|
||
rw [resolve_eq_filter_lt_reverse (db := dbA) (s := r.end_) hend, hfiltE,
|
||
← clampNonposD_eq, hgetd]
|
||
have hcntA : dbA.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
res.keys.length := by
|
||
rw [hb, hdbA]
|
||
rw [List.countP_append, List.countP_cons]
|
||
have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) =
|
||
res.keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_lt_coe.mpr (hresb x hx)
|
||
have h2 : decide (((b : Key) : WithTop Key) < (↑b : WithTop Key)) = false := by simp
|
||
have h3 : F.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_lt (hFb x hx)))
|
||
rw [h1, h2, h3]
|
||
simp
|
||
have hanchorA : r.begin.anchor dbA < 0 := by
|
||
rw [anchor_eq_count_sub_slack dbA r (le_of_lt hbegin), hcntA]
|
||
have hle : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) := by
|
||
have := hclamp; rw [hrepA] at this; exact_mod_cast this
|
||
omega
|
||
have hL_A : resolve dbA r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorA
|
||
have hfilt_b : dbA.keys.filter (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop
|
||
Key))) =
|
||
res.keys := by
|
||
rw [hdbA]
|
||
rw [List.filter_append, List.filter_cons]
|
||
have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) =
|
||
res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_lt_coe.mpr (hresb x hx)
|
||
have h2 : decide (((b : Key) : WithTop Key) < (↑b : WithTop Key)) = false := by simp
|
||
have h3 : F.filter (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_lt (hFb x hx)))
|
||
rw [h1, h2, h3]
|
||
simp
|
||
have hckA : RangeRead.completeKeys dbA r = res.keys := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hL_A, hR_A]
|
||
rw [filter_between_bot_eq]
|
||
exact hfilt_b
|
||
exact ⟨dbA, ⟨hlim, Or.inr ⟨hmore, hckA.symm⟩⟩, hanchorA⟩
|
||
· -- Option B: the reported last lies at or above `b`. Use the keys of `db₀` above it.
|
||
push Not at hlastB
|
||
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 _))
|
||
obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by
|
||
rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩
|
||
· exact absurd hn hR_ne_nil
|
||
· exact absurd ht (resolve_ne_top_of_offset_nonpos hend)
|
||
· exact ⟨q, hqeq⟩
|
||
have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key)
|
||
:= by
|
||
rw [← hr₀]; exact hlast_lt_R₀
|
||
have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by
|
||
rw [← hr₀]
|
||
exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil
|
||
set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL
|
||
have hLlen : L.length = clampNonposD r + 1 := by
|
||
rw [hL]; exact between_resolve_end_length hend hR_ne_nil
|
||
have hLsorted : L.SortedLT := by
|
||
rw [hL]
|
||
exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)
|
||
have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hL] at hx
|
||
exact of_decide_eq_true (List.mem_filter.mp hx).2
|
||
have hresL : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by
|
||
intro x hx y hy
|
||
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₀ (hLmem y hy).1))
|
||
have hAppSorted : (res.keys ++ L).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hsort, hLsorted, hresL⟩
|
||
set dbB : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdbB
|
||
have hresE : ∀ x ∈ res.keys, (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(lt_trans hlast_lt_r₀ hr₀_lt_E)
|
||
have hfiltEB : dbB.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
|
||
res.keys ++ L := by
|
||
rw [hdbB]
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact hresE x hx
|
||
· exact (hLmem x hx).2
|
||
have hbetweenB : dbB.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||
rw [hdbB]
|
||
rw [List.filter_append]
|
||
have h1 : res.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.1
|
||
(not_le.mpr (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
hlast_lt_r₀))
|
||
have h2 : L.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [← hr₀]
|
||
exact hLmem x hx
|
||
rw [h1, h2]
|
||
simp
|
||
have hbetween₀ : db₀.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||
rw [hL, ← hr₀]
|
||
rfl
|
||
have hqspec : (db₀.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
r.end_.key))).reverse[
|
||
(-r.end_.offset).toNat]? = some r₀ :=
|
||
resolve_filter_reverse_eq_some_of_ne_nil hend (by simpa [hr₀] using hR_ne_nil) hr₀
|
||
have hR_B : resolve dbB r.end_ = ((r₀ : Key) : WithTop Key) := by
|
||
refine resolve_eq_of_agree_between (db₁ := db₀) (db₂ := dbB) (s := r.end_) hend rfl
|
||
(le_of_lt hr₀_lt_E) ?_ hqspec (le_refl _)
|
||
rw [hbetween₀, hbetweenB]
|
||
have hcntB : dbB.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := by
|
||
rw [hb, hdbB]
|
||
rw [List.countP_append]
|
||
have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) =
|
||
reportedBelowBegin r res := by
|
||
unfold reportedBelowBegin
|
||
rw [hb]
|
||
have h2 : L.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
have hbx : (b : WithTop Key) ≤ ((x : Key) : WithTop Key) := by
|
||
refine le_trans (WithTop.coe_le_coe.mpr hlastB) ?_
|
||
exact le_of_lt (lt_of_lt_of_le hlast_lt_R₀ (hLmem x hx).1)
|
||
exact not_lt.mpr hbx
|
||
rw [h1, h2, Nat.add_zero]
|
||
have hanchorB : r.begin.anchor dbB < 0 := by
|
||
rw [anchor_eq_count_sub_slack dbB r (le_of_lt hbegin), hcntB]
|
||
have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
|
||
omega
|
||
have hL_B : resolve dbB r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorB
|
||
have hfilt_r : dbB.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((r₀ : Key) : WithTop Key))) = res.keys := by
|
||
rw [hdbB]
|
||
rw [List.filter_append]
|
||
have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((r₀ : Key) : WithTop Key))) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_lt_coe.mpr
|
||
(lt_of_le_of_lt (hlast_le x hx) (WithTop.coe_lt_coe.mp hlast_lt_r₀))
|
||
have h2 : L.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((r₀ : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
rw [← hr₀]
|
||
exact not_lt.mpr (hLmem x hx).1
|
||
rw [h1, h2]
|
||
simp
|
||
have hckB : RangeRead.completeKeys dbB r = res.keys := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hL_B, hR_B]
|
||
rw [filter_between_bot_eq]
|
||
exact hfilt_r
|
||
exact ⟨dbB, ⟨hlim, Or.inr ⟨hmore, hckB.symm⟩⟩, hanchorB⟩
|
||
|
||
|
||
/-- **The infinite-capacity negative-begin complete clamp cell is minimal.** -/
|
||
theorem minimal_closedForm_of_negCross_clampNonpos_infinite {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampNonposInfinite r res) :
|
||
Minimal r res (closedFormNegCrossClampNonposInfinite r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hinf, hcrossD, hclamp⟩ := hcell
|
||
obtain ⟨db, hcomp, hanchor⟩ :=
|
||
exists_clamp_compat_nonpos_infinite hread hrev hmore hne hbegin hend hB_le_E hinf hcrossD hclamp
|
||
refine minimal_iff_sound_and_necessaryRange.mpr
|
||
⟨sound_clampNonposInfinite hrev hbegin hend hmore hB_le_E, ?_⟩
|
||
intro k hkbot hkE
|
||
by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)
|
||
· exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead
|
||
· push Not at hkhead
|
||
by_cases hklast : ((k : Key) : WithTop Key) ≤
|
||
((res.keys.getLast hne : Key) : WithTop Key)
|
||
· exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne)
|
||
(WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast)
|
||
· push Not at hklast
|
||
intro cr hs
|
||
exact necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs
|
||
(le_of_lt (WithTop.coe_lt_coe.mp hklast)) hkE
|
||
/-- A reversed complete result in the same cell. -/
|
||
abbrev negCrossClampNonposInfiniteRev (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧
|
||
intervalCard r.begin.key r.end_.key = ⊤ ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
reportedBelowBegin r res ≤ beginSlack r
|
||
|
||
/-- The closed form for the reversed cell: `[⊥, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossClampNonposInfiniteRev (r : RangeRead) (_res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The reversed infinite-capacity negative-begin complete clamp cell is minimal**, by
|
||
flipping the traversal and applying the forward rule. -/
|
||
theorem minimal_closedForm_of_negCross_clampNonpos_infinite_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampNonposInfiniteRev r res) :
|
||
Minimal r res (closedFormNegCrossClampNonposInfiniteRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hinf, hcrossD, hclamp⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
have hlastD : 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 hcell' : negCrossClampNonposInfinite { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, hinf, ?_, ?_⟩
|
||
· rw [hlastD]; exact hcrossD
|
||
· rw [hcount]; exact hclamp
|
||
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_clampNonpos_infinite hread' hcell'
|
||
have hcr : closedFormNegCrossClampNonposInfinite { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } =
|
||
closedFormNegCrossClampNonposInfiniteRev r res := by
|
||
unfold closedFormNegCrossClampNonposInfinite closedFormNegCrossClampNonposInfiniteRev
|
||
rfl
|
||
rw [hcr] at hmin
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
/-- A forward complete result at a negative begin offset with `end.offset ≤ 0`, reported
|
||
keys reaching at or above `begin.key`, and reported keys below `begin.key` within the begin
|
||
slack. -/
|
||
abbrev negCrossClampNonposLastGe (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 ∧
|
||
r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧
|
||
reportedBelowBegin r res ≤ beginSlack r
|
||
|
||
/-- The closed form for the cell: `[⊥, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossClampNonposLastGe (r : RangeRead) (_res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
theorem exists_clamp_compat_nonpos_lastGe {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hend : r.end_.offset ≤ 0)
|
||
(hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key))
|
||
(hclamp : reportedBelowBegin r res ≤ beginSlack r) :
|
||
∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||
rw [List.getLastD_eq_getLast?]
|
||
simp [List.getLast?_eq_some_getLast hne]
|
||
have hlastB : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by
|
||
rw [← hlastD]; exact hB_le_last
|
||
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 _))
|
||
obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by
|
||
rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩
|
||
· exact absurd hn hR_ne_nil
|
||
· exact absurd ht (resolve_ne_top_of_offset_nonpos hend)
|
||
· exact ⟨q, hqeq⟩
|
||
have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key) := by
|
||
rw [← hr₀]; exact hlast_lt_R₀
|
||
have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by
|
||
rw [← hr₀]
|
||
exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil
|
||
set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL
|
||
have hLlen : L.length = clampNonposD r + 1 := by
|
||
rw [hL]; exact between_resolve_end_length hend hR_ne_nil
|
||
have hLsorted : L.SortedLT := by
|
||
rw [hL]
|
||
exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)
|
||
have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hL] 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 hresL : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by
|
||
intro x hx y hy
|
||
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 (le_of_eq hr₀.symm) (hLmem y hy).1)))
|
||
have hAppSorted : (res.keys ++ L).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hsort, hLsorted, hresL⟩
|
||
set db : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdb
|
||
have hL_ge_r₀ : ∀ x ∈ L, ((r₀ : Key) : WithTop Key) ≤ (x : WithTop Key) := by
|
||
intro x hx
|
||
rw [← hr₀]
|
||
exact (hLmem x hx).1
|
||
have hL_lt_E : ∀ x ∈ L, (x : WithTop Key) < r.end_.key := fun x hx => (hLmem x hx).2
|
||
have hres_lt_r₀ : ∀ x ∈ res.keys, x < r₀ := by
|
||
intro x hx
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlast_lt_r₀)
|
||
have hfilt : db.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||
rw [hdb]
|
||
rw [List.filter_append]
|
||
have h1 : res.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
intro hcon
|
||
exact absurd hcon.1 (not_le.mpr (WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx)))
|
||
have h2 : L.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact ⟨hL_ge_r₀ x hx, hL_lt_E x hx⟩
|
||
rw [h1, h2]
|
||
simp
|
||
have hfilt₀ : db₀.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||
rw [hL, ← hr₀]
|
||
rfl
|
||
have hqspec : (db₀.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).reverse[
|
||
(-r.end_.offset).toNat]? = some r₀ :=
|
||
resolve_filter_reverse_eq_some_of_ne_nil hend (by simpa [hr₀] using hR_ne_nil) hr₀
|
||
have hR : resolve db r.end_ = ((r₀ : Key) : WithTop Key) := by
|
||
refine resolve_eq_of_agree_between (db₁ := db₀) (db₂ := db) (s := r.end_) hend rfl
|
||
(le_of_lt hr₀_lt_E) ?_ hqspec (le_refl _)
|
||
rw [hfilt₀, hfilt]
|
||
have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := by
|
||
rw [hdb]
|
||
rw [List.countP_append]
|
||
have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := rfl
|
||
have h2 : L.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr
|
||
(le_trans hlastB (le_trans (le_of_lt hlast_lt_r₀)
|
||
(le_trans (le_of_eq hr₀.symm) (hLmem x hx).1)))
|
||
rw [h1, h2, Nat.add_zero]
|
||
have hanchor : r.begin.anchor db < 0 := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt]
|
||
have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
|
||
omega
|
||
have hL_db : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor
|
||
have hfilt_b : db.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((r₀ : Key) : WithTop Key))) = res.keys := by
|
||
rw [hdb]
|
||
rw [List.filter_append]
|
||
have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((r₀ : Key) : WithTop Key))) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx)
|
||
have h2 : L.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((r₀ : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
rw [← hr₀]
|
||
exact not_lt.mpr (hLmem x hx).1
|
||
rw [h1, h2]
|
||
simp
|
||
have hck : RangeRead.completeKeys db r = res.keys := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hL_db, hR]
|
||
rw [filter_between_bot_eq]
|
||
exact hfilt_b
|
||
exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩, hanchor⟩
|
||
|
||
theorem minimal_closedForm_of_negCross_clampNonpos_lastGe {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampNonposLastGe r res) :
|
||
Minimal r res (closedFormNegCrossClampNonposLastGe r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hB_le_last, hclamp⟩ := hcell
|
||
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||
rw [List.getLastD_eq_getLast?]
|
||
simp [List.getLast?_eq_some_getLast hne]
|
||
obtain ⟨db, hcomp, hanchor⟩ :=
|
||
exists_clamp_compat_nonpos_lastGe hread hrev hmore hne hbegin hend hB_le_last hclamp
|
||
have hB_le_E : r.begin.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
|
||
have hlast_lt : ((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 hres_le : resolve db₀ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
have hB_le_last' : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by
|
||
rw [← hlastD]; exact hB_le_last
|
||
exact le_trans hB_le_last' (le_trans (le_of_lt hlast_lt) hres_le)
|
||
refine minimal_iff_sound_and_necessaryRange.mpr
|
||
⟨sound_clampNonposInfinite hrev hbegin hend hmore hB_le_E, ?_⟩
|
||
intro k hkbot hkE
|
||
by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)
|
||
· exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead
|
||
· push Not at hkhead
|
||
by_cases hklast : ((k : Key) : WithTop Key) ≤
|
||
((res.keys.getLast hne : Key) : WithTop Key)
|
||
· exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne)
|
||
(WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast)
|
||
· push Not at hklast
|
||
intro cr hs
|
||
exact necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs
|
||
(le_of_lt (WithTop.coe_lt_coe.mp hklast)) hkE
|
||
|
||
/-- A reversed complete result at a negative begin offset with `end.offset ≤ 0`, reported
|
||
keys reaching at or above `begin.key` (in the reversed order, the reported head), and
|
||
reported keys below `begin.key` within the begin slack. -/
|
||
abbrev negCrossClampNonposLastGeRev (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 ∧
|
||
r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧
|
||
reportedBelowBegin r res ≤ beginSlack r
|
||
|
||
/-- The closed form for the reversed cell: `[⊥, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossClampNonposLastGeRev (r : RangeRead) (_res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The reversed cell is minimal**, by flipping the traversal. -/
|
||
theorem minimal_closedForm_of_negCross_clampNonpos_lastGe_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampNonposLastGeRev r res) :
|
||
Minimal r res (closedFormNegCrossClampNonposLastGeRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hB_le_head, hclamp⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
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 hcrossD' : ((res.keys.reverse.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by
|
||
rw [List.headD_eq_head?, List.head?_reverse]
|
||
simpa [List.getLastD_eq_getLast?] using hcrossD
|
||
have hB_le_last : r.begin.key ≤
|
||
((res.keys.reverse.getLastD ([] : Key) : Key) : WithTop Key) := by
|
||
rw [List.getLastD_eq_getLast?, List.getLast?_reverse]
|
||
simpa [List.headD_eq_head?] using hB_le_head
|
||
have hcell' : negCrossClampNonposLastGe { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, hcrossD', hB_le_last, ?_⟩
|
||
rw [hcount]; exact hclamp
|
||
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_clampNonpos_lastGe hread' hcell'
|
||
have hcr : closedFormNegCrossClampNonposLastGe { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } =
|
||
closedFormNegCrossClampNonposLastGeRev r res := by
|
||
unfold closedFormNegCrossClampNonposLastGe closedFormNegCrossClampNonposLastGeRev
|
||
rfl
|
||
rw [hcr] at hmin
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
/-- Split a capacity bound `n ≤ A + C` into pads below and above a cut: `a` pads fit in
|
||
`C` and the remaining `b = n - C` fit in `A`. -/
|
||
theorem exists_pad_split {n : ℕ} {A C : ℕ∞}
|
||
(h : (n : ℕ∞) ≤ A + C) :
|
||
∃ a b : ℕ, a + b = n ∧ (a : ℕ∞) ≤ C ∧ (b : ℕ∞) ≤ A ∧
|
||
(b : ℕ∞) = (n : ℕ∞) - C := by
|
||
by_cases hC : C = ⊤
|
||
· refine ⟨n, 0, by omega, ?_, by simp, ?_⟩
|
||
· rw [hC]; exact le_top
|
||
· rw [hC, ENat.sub_top]; simp
|
||
· have hCeq : ((C.toNat : ℕ) : ℕ∞) = C := ENat.natCast_toNat hC
|
||
by_cases hcn : n ≤ C.toNat
|
||
· refine ⟨n, 0, by omega, ?_, by simp, ?_⟩
|
||
· rw [← hCeq]; exact_mod_cast hcn
|
||
· rw [← hCeq, ← ENat.natCast_sub, Nat.sub_eq_zero_of_le hcn]
|
||
· push Not at hcn
|
||
have hble : ((n - C.toNat : ℕ) : ℕ∞) ≤ A := by
|
||
rw [← hCeq] at h
|
||
rw [ENat.natCast_sub, tsub_le_iff_left, add_comm]
|
||
exact h
|
||
refine ⟨C.toNat, n - C.toNat, by omega, ?_, hble, ?_⟩
|
||
· rw [← hCeq]; exact le_rfl
|
||
· calc ((n - C.toNat : ℕ) : ℕ∞) = (n : ℕ∞) - ((C.toNat : ℕ) : ℕ∞) :=
|
||
ENat.natCast_sub n C.toNat
|
||
_ = (n : ℕ∞) - C := by rw [hCeq]
|
||
|
||
/-- A finite-capacity clamping database exists in the crossed negative-begin complete
|
||
nonpositive-end cell whenever the capacity bound `clampNonposBottom` holds. -/
|
||
theorem exists_clamp_compat_nonpos_bottom {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hend : r.end_.offset ≤ 0)
|
||
(hB_le_E : r.begin.key ≤ r.end_.key)
|
||
(hbottom : clampNonposBottom r res) :
|
||
∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
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_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 _))
|
||
obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by
|
||
rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩
|
||
· exact absurd hn hR_ne_nil
|
||
· exact absurd ht (resolve_ne_top_of_offset_nonpos hend)
|
||
· exact ⟨q, hqeq⟩
|
||
have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key) := by
|
||
rw [← hr₀]; exact hlast_lt_R₀
|
||
have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by
|
||
rw [← hr₀]
|
||
exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil
|
||
set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL
|
||
have hLlen : L.length = clampNonposD r + 1 := by
|
||
rw [hL]; exact between_resolve_end_length hend hR_ne_nil
|
||
have hcap_r₀ : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
|
||
intervalCard ((r₀ : Key) : WithTop Key) r.end_.key := by
|
||
have h := between_length_le_intervalCard (db := db₀)
|
||
(E := ((r₀ : Key) : WithTop Key)) (B := r.end_.key) (le_of_lt hr₀_lt_E)
|
||
have h' : ((between db₀ (resolve db₀ r.end_) r.end_.key).length : ℕ∞) ≤
|
||
intervalCard ((r₀ : Key) : WithTop Key) r.end_.key := by
|
||
rw [hr₀]; exact h
|
||
rw [← hL, hLlen] at h'
|
||
exact h'
|
||
set last : Key := res.keys.getLast hne with hlastdef
|
||
set X : Key := keyAfter last with hXdef
|
||
have hlast_lt_X : last < X := keyAfter_self_lt last
|
||
have hX_le_r₀ : ((X : Key) : WithTop Key) ≤ ((r₀ : Key) : WithTop Key) :=
|
||
keyAfter_le_of_lt hlast_lt_r₀
|
||
have hX_le_E : ((X : Key) : WithTop Key) ≤ r.end_.key :=
|
||
le_of_lt (lt_of_le_of_lt hX_le_r₀ hr₀_lt_E)
|
||
have hcapX : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
|
||
intervalCard ((X : Key) : WithTop Key) r.end_.key := by
|
||
calc (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
|
||
intervalCard ((r₀ : Key) : WithTop Key) r.end_.key := hcap_r₀
|
||
_ ≤ intervalCard ((X : Key) : WithTop Key) r.end_.key := by
|
||
rw [intervalCard_add hX_le_r₀ (le_of_lt hr₀_lt_E)]
|
||
exact le_add_left le_rfl
|
||
have hres_lt_X : ∀ x ∈ res.keys, x < X := by
|
||
intro x hx
|
||
exact lt_of_le_of_lt (hlast_le x hx) hlast_lt_X
|
||
by_cases hBX : r.begin.key ≤ ((X : Key) : WithTop Key)
|
||
· -- Case 1: all pads can go at or above `begin.key`.
|
||
obtain ⟨pad, hpadIn, hpadlen⟩ :=
|
||
exists_db_keysIn (B := ((X : Key) : WithTop Key)) (E := r.end_.key)
|
||
(m := clampNonposD r + 1) hcapX
|
||
set P : List Key := pad.keys with hP
|
||
have hPsorted : P.SortedLT := pad.sorted
|
||
have hPmem : ∀ x ∈ P, ((X : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx
|
||
have hPlen : P.length = clampNonposD r + 1 := hpadlen
|
||
have hP_gt_last : ∀ x ∈ P, last < x := by
|
||
intro x hx
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) (hPmem x hx).1)
|
||
have hP_ge_B : ∀ x ∈ P, r.begin.key ≤ (x : WithTop Key) := by
|
||
intro x hx
|
||
exact le_trans hBX (hPmem x hx).1
|
||
have hcross : ∀ x ∈ res.keys, ∀ y ∈ P, x < y := by
|
||
intro x hx y hy
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (hres_lt_X x hx)) (hPmem y hy).1)
|
||
have hAppSorted : (res.keys ++ P).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hsort, hPsorted, hcross⟩
|
||
set db : DB := ⟨res.keys ++ P, hAppSorted⟩ with hdb
|
||
have hPne : P ≠ [] := by
|
||
rw [← List.length_pos_iff, hPlen]; omega
|
||
have hfiltE : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
|
||
res.keys ++ P := by
|
||
rw [hdb]
|
||
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_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) hX_le_E)
|
||
· exact (hPmem x hx).2
|
||
have hget : (res.keys ++ P).reverse[clampNonposD r]? =
|
||
some (P.head hPne) := by
|
||
rw [List.reverse_append, List.getElem?_append]
|
||
have hPlrev : P.reverse.length = clampNonposD r + 1 := by
|
||
rw [List.length_reverse, hPlen]
|
||
rw [ite_eq_left (by omega : clampNonposD r < P.reverse.length)]
|
||
rw [List.getElem?_reverse (l := P) (i := clampNonposD r) (by omega)]
|
||
have hidx : P.length - 1 - clampNonposD r = 0 := by omega
|
||
rw [hidx]
|
||
match P, hPne with
|
||
| [], h => exact absurd rfl h
|
||
| a :: t, _ => rfl
|
||
have hR : resolve db r.end_ = ((P.head hPne : Key) : WithTop Key) := by
|
||
rw [resolve_eq_filter_lt_reverse (db := db) (s := r.end_) hend, hfiltE,
|
||
← clampNonposD_eq, hget]
|
||
have hhead_ge_X : ((X : Key) : WithTop Key) ≤ ((P.head hPne : Key) : WithTop Key) :=
|
||
(hPmem (P.head hPne) (List.head_mem hPne)).1
|
||
have hhead_le : ∀ x ∈ P, ((P.head hPne : Key) : WithTop Key) ≤ (x : WithTop Key) := by
|
||
intro x hx
|
||
exact WithTop.coe_le_coe.mpr ((hPsorted.pairwise.imp le_of_lt).rel_head hx)
|
||
have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := by
|
||
rw [hdb, List.countP_append]
|
||
have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := rfl
|
||
have h2 : P.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (hP_ge_B x hx)
|
||
rw [h1, h2, Nat.add_zero]
|
||
have hzero : ((((clampNonposD r + 1 : ℕ)) : ℕ∞) -
|
||
intervalCard r.begin.key r.end_.key) = 0 := by
|
||
rw [tsub_eq_zero_iff_le]
|
||
calc (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
|
||
intervalCard ((X : Key) : WithTop Key) r.end_.key := hcapX
|
||
_ ≤ intervalCard r.begin.key r.end_.key := by
|
||
rw [intervalCard_add hBX hX_le_E]
|
||
exact le_add_left le_rfl
|
||
have hanchor : r.begin.anchor db < 0 := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt]
|
||
have hq : (reportedBelowBegin r res : ℕ∞) ≤ (beginSlack r : ℕ∞) := by
|
||
have hb := hbottom
|
||
unfold clampNonposBottom at hb
|
||
rw [hzero, add_zero] at hb
|
||
exact hb
|
||
have hq' : reportedBelowBegin r res ≤ beginSlack r := by
|
||
exact_mod_cast hq
|
||
have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hq'
|
||
omega
|
||
have hLdb : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor
|
||
have hfilt_head : db.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((P.head hPne : Key) : WithTop Key))) = res.keys := by
|
||
rw [hdb, List.filter_append]
|
||
have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((P.head hPne : Key) : WithTop Key))) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) hhead_ge_X)
|
||
have h2 : P.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((P.head hPne : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (hhead_le x hx)
|
||
rw [h1, h2]
|
||
simp
|
||
have hck : RangeRead.completeKeys db r = res.keys := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hLdb, hR]
|
||
rw [filter_between_bot_eq]
|
||
exact hfilt_head
|
||
exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩, hanchor⟩
|
||
· -- Case 2: pads split between `[X, begin.key)` and `[begin.key, end.key)`.
|
||
push Not at hBX
|
||
have hXB : ((X : Key) : WithTop Key) < r.begin.key := hBX
|
||
have hadd : intervalCard ((X : Key) : WithTop Key) r.end_.key =
|
||
intervalCard ((X : Key) : WithTop Key) r.begin.key +
|
||
intervalCard r.begin.key r.end_.key :=
|
||
intervalCard_add (le_of_lt hXB) hB_le_E
|
||
have hcap' : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
|
||
intervalCard ((X : Key) : WithTop Key) r.begin.key +
|
||
intervalCard r.begin.key r.end_.key := by
|
||
rw [← hadd]; exact hcapX
|
||
obtain ⟨a, b, hab, haC, hbA, hbeq⟩ := exists_pad_split hcap'
|
||
obtain ⟨padQ, hpadQIn, hpadQlen⟩ :=
|
||
exists_db_keysIn (B := r.begin.key) (E := r.end_.key) (m := a) haC
|
||
obtain ⟨padP, hpadPIn, hpadPlen⟩ :=
|
||
exists_db_keysIn (B := ((X : Key) : WithTop Key)) (E := r.begin.key) (m := b) hbA
|
||
set Q : List Key := padQ.keys with hQ
|
||
set R : List Key := padP.keys with hRset
|
||
have hQsorted : Q.SortedLT := padQ.sorted
|
||
have hRsorted : R.SortedLT := padP.sorted
|
||
have hQmem : ∀ x ∈ Q, r.begin.key ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
simpa only [keysIn, Set.mem_ofPred_eq] using hpadQIn x hx
|
||
have hRmem : ∀ x ∈ R, ((X : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.begin.key := by
|
||
intro x hx
|
||
simpa only [keysIn, Set.mem_ofPred_eq] using hpadPIn x hx
|
||
have hQlen : Q.length = a := hpadQlen
|
||
have hRlen : R.length = b := hpadPlen
|
||
have hR_gt_last : ∀ x ∈ R, last < x := by
|
||
intro x hx
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) (hRmem x hx).1)
|
||
have hQ_gt_last : ∀ x ∈ Q, last < x := by
|
||
intro x hx
|
||
exact lt_trans hlast_lt_X
|
||
(WithTop.coe_lt_coe.mp (lt_of_lt_of_le hXB (hQmem x hx).1))
|
||
have hcross : ∀ x ∈ R, ∀ y ∈ Q, x < y := by
|
||
intro x hx y hy
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (hRmem x hx).2 (hQmem y hy).1)
|
||
set Lp : List Key := R ++ Q with hLp
|
||
have hLpSorted : Lp.SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hRsorted, hQsorted, hcross⟩
|
||
have hLplen : Lp.length = clampNonposD r + 1 := by
|
||
rw [hLp, List.length_append, hQlen, hRlen]
|
||
omega
|
||
have hLpne : Lp ≠ [] := by
|
||
rw [← List.length_pos_iff, hLplen]; omega
|
||
have hLpmem : ∀ x ∈ Lp, last < x ∧ (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hLp] at hx
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact ⟨hR_gt_last x hx, lt_of_lt_of_le (hRmem x hx).2 hB_le_E⟩
|
||
· exact ⟨hQ_gt_last x hx, (hQmem x hx).2⟩
|
||
have hcross' : ∀ x ∈ res.keys, ∀ y ∈ Lp, x < y := by
|
||
intro x hx y hy
|
||
have hXy : ((X : Key) : WithTop Key) ≤ (y : WithTop Key) := by
|
||
rw [hXdef]
|
||
exact keyAfter_le_of_lt (WithTop.coe_lt_coe.mpr (hLpmem y hy).1)
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (hres_lt_X x hx)) hXy)
|
||
have hAppSorted : (res.keys ++ Lp).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hsort, hLpSorted, hcross'⟩
|
||
set db : DB := ⟨res.keys ++ Lp, hAppSorted⟩ with hdb
|
||
have hfiltE : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
|
||
res.keys ++ Lp := by
|
||
rw [hdb]
|
||
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_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) hX_le_E)
|
||
· exact (hLpmem x hx).2
|
||
have hget : (res.keys ++ Lp).reverse[clampNonposD r]? =
|
||
some (Lp.head hLpne) := by
|
||
rw [List.reverse_append, List.getElem?_append]
|
||
have hLplrev : Lp.reverse.length = clampNonposD r + 1 := by
|
||
rw [List.length_reverse, hLplen]
|
||
rw [ite_eq_left (by omega : clampNonposD r < Lp.reverse.length)]
|
||
rw [List.getElem?_reverse (l := Lp) (i := clampNonposD r) (by omega)]
|
||
have hidx : Lp.length - 1 - clampNonposD r = 0 := by omega
|
||
rw [hidx]
|
||
match Lp, hLpne with
|
||
| [], h => exact absurd rfl h
|
||
| a :: t, _ => rfl
|
||
have hR : resolve db r.end_ = ((Lp.head hLpne : Key) : WithTop Key) := by
|
||
rw [resolve_eq_filter_lt_reverse (db := db) (s := r.end_) hend, hfiltE,
|
||
← clampNonposD_eq, hget]
|
||
have hhead_ge_last : last < Lp.head hLpne :=
|
||
(hLpmem (Lp.head hLpne) (List.head_mem hLpne)).1
|
||
have hhead_le : ∀ x ∈ Lp, ((Lp.head hLpne : Key) : WithTop Key) ≤ (x : WithTop Key) := by
|
||
intro x hx
|
||
exact WithTop.coe_le_coe.mpr ((hLpSorted.pairwise.imp le_of_lt).rel_head hx)
|
||
have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res + b := by
|
||
rw [hdb, List.countP_append]
|
||
have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := rfl
|
||
rw [h1]
|
||
rw [hLp, List.countP_append]
|
||
have h2 : R.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = b := by
|
||
rw [← hRlen, List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact (hRmem x hx).2
|
||
have h3 : Q.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (hQmem x hx).1
|
||
rw [h2, h3, Nat.add_zero]
|
||
have hanchor : r.begin.anchor db < 0 := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt]
|
||
have hq : ((reportedBelowBegin r res + b : ℕ) : ℕ∞) ≤ (beginSlack r : ℕ∞) := by
|
||
rw [Nat.cast_add, hbeq]
|
||
simpa only [clampNonposBottom] using hbottom
|
||
have hq' : reportedBelowBegin r res + b ≤ beginSlack r := by
|
||
exact_mod_cast hq
|
||
have hle : ((reportedBelowBegin r res + b : ℕ) : ℤ) ≤ (beginSlack r : ℤ) := by
|
||
exact_mod_cast hq'
|
||
rw [Nat.cast_add] at hle
|
||
omega
|
||
have hLdb : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor
|
||
have hfilt_head : db.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((Lp.head hLpne : Key) : WithTop Key))) = res.keys := by
|
||
rw [hdb, List.filter_append]
|
||
have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((Lp.head hLpne : Key) : WithTop Key))) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||
(WithTop.coe_lt_coe.mpr hhead_ge_last)
|
||
have h2 : Lp.filter (fun k : Key => decide ((k : WithTop Key) <
|
||
((Lp.head hLpne : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [decide_eq_true_eq]
|
||
exact not_lt.mpr (hhead_le x hx)
|
||
rw [h1, h2]
|
||
simp
|
||
have hck : RangeRead.completeKeys db r = res.keys := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hLdb, hR]
|
||
rw [filter_between_bot_eq]
|
||
exact hfilt_head
|
||
exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩, hanchor⟩
|
||
|
||
/-- The finite-capacity crossed negative-begin complete nonpositive-end clamp cell. -/
|
||
abbrev negCrossClampNonposBottom (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
clampNonposBottom r res
|
||
|
||
/-- The closed form for the finite-capacity clamp cell: `[⊥, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossClampNonposBottom (r : RangeRead) (_res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The finite-capacity negative-begin complete clamp cell is minimal.** -/
|
||
theorem minimal_closedForm_of_negCross_clampNonpos_bottom {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampNonposBottom r res) :
|
||
Minimal r res (closedFormNegCrossClampNonposBottom r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcross, hbottom⟩ := hcell
|
||
obtain ⟨db, hcomp, hanchor⟩ :=
|
||
exists_clamp_compat_nonpos_bottom hread hrev hmore hne hbegin hend hB_le_E hbottom
|
||
refine minimal_iff_sound_and_necessaryRange.mpr
|
||
⟨sound_clampNonposInfinite hrev hbegin hend hmore hB_le_E, ?_⟩
|
||
intro k hkbot hkE
|
||
by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)
|
||
· exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead
|
||
· push Not at hkhead
|
||
by_cases hklast : ((k : Key) : WithTop Key) ≤
|
||
((res.keys.getLast hne : Key) : WithTop Key)
|
||
· exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne)
|
||
(WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast)
|
||
· push Not at hklast
|
||
intro cr hs
|
||
exact necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs
|
||
(le_of_lt (WithTop.coe_lt_coe.mp hklast)) hkE
|
||
|
||
/-- The reversed traversal of the finite-capacity clamp cell. -/
|
||
abbrev negCrossClampNonposBottomRev (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
clampNonposBottom r res
|
||
|
||
/-- The closed form for the reversed finite-capacity clamp cell. -/
|
||
noncomputable def closedFormNegCrossClampNonposBottomRev (r : RangeRead) (_res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The reversed finite-capacity clamp cell is minimal**, by flipping the traversal. -/
|
||
theorem minimal_closedForm_of_negCross_clampNonpos_bottom_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampNonposBottomRev r res) :
|
||
Minimal r res (closedFormNegCrossClampNonposBottomRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcrossD, hbottom⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
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 hcrossD' : ((res.keys.reverse.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by
|
||
rw [List.headD_eq_head?, List.head?_reverse]
|
||
simpa [List.getLastD_eq_getLast?] using hcrossD
|
||
have hbottom' : clampNonposBottom { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
unfold clampNonposBottom
|
||
rw [hcount]
|
||
exact hbottom
|
||
have hcell' : negCrossClampNonposBottom { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, hcrossD', 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_clampNonpos_bottom hread' hcell'
|
||
have hcr : closedFormNegCrossClampNonposBottom { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } =
|
||
closedFormNegCrossClampNonposBottomRev r res := by
|
||
unfold closedFormNegCrossClampNonposBottom closedFormNegCrossClampNonposBottomRev
|
||
rfl
|
||
rw [hcr] at hmin
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
end FormalConflictRange
|