Files
conflict-range/FormalConflictRange/NegCrossTruncNonclampNonpos.lean
T
andrew a54a5f82f8 Prove conflict-range closed-form minimality
- Vendor the FormalConflictRange reference library needed by the proof
- Add Bridge.lean with simp lemmas connecting the Statement definitions
  to the reference library
- Prove Statement.closedFormMinimal in Proof.lean
- Register the new libraries in the Lake config
2026-11-16 21:53:59 -05:00

931 lines
46 KiB
Lean4
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/-
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: FormalConflictRange contributors
-/
import FormalConflictRange.NegCrossTruncNonclamp
/-!
# Crossed negative-begin truncated nonclamping nonpositive-end cell
This module closes the forward, crossed, truncated, nonclamping cell at a negative
begin offset with a nonpositive end offset. In this cell the begin selector resolves
onto the reported head, there are no *unreported* stored keys between the reported last
and `begin.key`, and the minimal sound conflict range is
`[head, max begin.key end.key)`. When `end.key < begin.key` no compatible database
exists, so the maximal branch reduces to `[head, end.key)`.
The proof adapts the begin-zero nonpositive-end argument of `ZeroCrossTrunc.lean`. The
begin-zero fact "no stored key lies between the reported head and `begin.key`" is
replaced by the count identity
`count_lt_begin_eq_of_negCross_trunc_nonclamp`, which says the stored keys in that gap
are exactly the reported ones, and by an anchor computation locating the begin
resolution at the reported head.
-/
open FormalConflictRange
namespace FormalConflictRange
/-- In the compatible nonclamping crossed truncated negative-begin cell, the stored
keys in `[head, begin.key)` are exactly the reported keys there. Equivalently, no
unreported stored key lies above the reported last and below `begin.key`. -/
theorem filter_between_head_begin_eq_of_negCross_trunc_nonclamp
{db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1) :
db.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧
(k : WithTop Key) < r.begin.key)) =
res.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧
(k : WithTop Key) < r.begin.key)) := by
set head : Key := res.keys.head hne with hhead
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross
-- The count of stored keys below `begin.key` splits at the head.
have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup
(a := head) (B := r.begin.key) (le_of_lt hcross')
have hsplit' : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) =
db.keys.countP (fun k : Key => decide (k < head)) +
db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) := by
rw [hsplit]
rw [← List.countP_eq_length_filter]
-- The count identity from the nonclamping cell.
have hcount := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin
hcross' hnonclamp
rw [← hhead] at hcount
-- The reported count in the gap.
have hres_count : res.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := by
unfold reportedBelowBegin
rw [List.countP_eq_length_filter, List.countP_eq_length_filter]
congr 1
apply List.filter_congr
intro k hk
have hhead_le : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
WithTop.coe_le_coe.mpr
((RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk)
rw [decide_eq_decide]
exact and_iff_right hhead_le
-- Equal counts and a sublist give equal filters.
have hlen_eq : (db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key))).length =
(res.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key))).length := by
have h1 : (db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key))).length =
db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) := by
rw [List.countP_eq_length_filter]
have h2 : (res.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key))).length =
res.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) := by
rw [List.countP_eq_length_filter]
rw [h1, h2, hres_count]
omega
have hsub : List.Sublist (res.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)))
(db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key))) :=
List.Sublist.filter _ (result_sublist_db_of_compatible hcomp hrev)
exact (hsub.eq_of_length hlen_eq.symm).symm
/-- In the compatible nonclamping crossed truncated negative-begin cell, no stored key
lies strictly above the reported last and strictly below `begin.key`. -/
theorem not_stored_between_last_begin_of_negCross_trunc_nonclamp
{db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1)
{x : Key} (hx : x ∈ db.keys) (hxlast : res.keys.getLast hne < x)
(hxB : (x : WithTop Key) < r.begin.key) : False := by
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hhead_le_last : head ≤ last := by
simp only [hhead, hlast]
exact (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt
|>.rel_getLast (List.head_mem hne)
have hxhead : ((head : Key) : WithTop Key) ≤ (x : WithTop Key) :=
WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hhead_le_last hxlast))
have heq := filter_between_head_begin_eq_of_negCross_trunc_nonclamp hcomp hrev hmore
hne hbegin hcross hnonclamp
have hxdb : x ∈ db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧
(k : WithTop Key) < r.begin.key)) := by
rw [List.mem_filter]
exact ⟨hx, by rw [decide_eq_true_eq]; exact ⟨hxhead, hxB⟩⟩
rw [heq] at hxdb
have hxres : x ∈ res.keys := (List.mem_filter.mp hxdb).1
have hxle : x ≤ last :=
((RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt).rel_getLast hxres
exact absurd hxle (not_le.mpr hxlast)
/-- In the compatible nonclamping crossed truncated negative-begin cell with a
nonpositive end offset, the end key lies at or above `begin.key`. Otherwise the
complete window `[head, resolve end)` would hold at most `beginSlack` keys, while the
reported prefix has at least `beginSlack + 1`. -/
theorem begin_le_end_of_negCross_trunc_nonclamp_nonpos
{db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1)
(hend : r.end_.offset ≤ 0) :
r.begin.key ≤ r.end_.key := by
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross
have hpre : res.keys <+: RangeRead.completeKeys db r :=
RangeRead.compatible_keys_prefix hcomp
by_contra hcon
have hEB : r.end_.key < r.begin.key := lt_of_not_ge hcon
-- The reported prefix is nonempty, so the complete window is nonempty.
have hck_ne : RangeRead.completeKeys db r ≠ [] := by
intro hnil
have : res.keys = [] := by
have hpre' := hpre
rw [hnil] at hpre'
exact List.eq_nil_of_prefix_nil hpre'
exact hne this
obtain ⟨k, hkck⟩ := List.exists_mem_of_ne_nil _ hck_ne
obtain ⟨-, hkb, hke⟩ := RangeRead.mem_completeKeys.mp hkck
have hlt_res : resolve db r.begin < resolve db r.end_ := lt_of_le_of_lt hkb hke
-- The begin resolution is the reported head.
have hrb : resolve db r.begin = ((head : Key) : WithTop Key) :=
resolve_begin_eq_head_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin hcross'
(by omega)
have hcnt_head : cnt db (resolve db r.begin) =
db.keys.countP (fun k : Key => decide (k < head)) := by
rw [hrb]
simp only [cnt, WithTop.coe_lt_coe]
-- The end resolution is a genuine stored key; name it and its rank.
have hne_top : resolve db r.end_ ≠ (⊤ : WithTop Key) :=
resolve_ne_top_of_offset_nonpos hend
obtain ⟨q, hq⟩ := WithTop.ne_top_iff_exists.mp hne_top
have hanchor_nonneg : 0 ≤ r.end_.anchor db := by
by_contra hneg
have hnil : resolve db r.end_ = (([] : Key) : WithTop Key) :=
resolve_eq_nil (lt_of_not_ge hneg)
rw [hnil] at hq
have hqnil : q = [] := WithTop.coe_inj.mp hq
rw [hrb, hnil] at hlt_res
exact absurd hlt_res (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key head)))
have hltlen : (r.end_.anchor db).toNat < db.keys.length := by
by_contra hle
exact hne_top (resolve_eq_top hanchor_nonneg (le_of_not_gt hle))
have hresget : resolve db r.end_ = ((db.keys[(r.end_.anchor db).toNat] : Key) : WithTop Key) :=
resolve_eq_getElem hanchor_nonneg hltlen
have hqval : db.keys[(r.end_.anchor db).toNat] = q := by
apply WithTop.coe_inj.mp
rw [← hresget, hq]
have hcnt_q : cnt db (resolve db r.end_) = (r.end_.anchor db).toNat := by
rw [hresget]
simp only [cnt, WithTop.coe_lt_coe, hqval]
exact countP_lt_eq_of_getElem db.sorted hltlen hqval
-- Bound the complete window length by the begin slack.
have hlen : (RangeRead.completeKeys db r).length =
cnt db (resolve db r.end_) - cnt db (resolve db r.begin) := by
have hb : RangeRead.completeKeys db r =
between db (resolve db r.begin) (resolve db r.end_) := by
unfold RangeRead.completeKeys RangeRead.inRange between
rw [ite_eq_right (by simp [hrev])]
rw [hb]
exact (cnt_sub_eq_between_length (le_of_lt hlt_res)).symm
-- Count below `begin.key` and below `end.key`.
set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))
with hcB
set cE := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))
with hcE
set cH := db.keys.countP (fun k : Key => decide (k < head)) with hcH
have hcount : cB = cH + reportedBelowBegin r res := by
have h := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin hcross'
hnonclamp
rw [← hhead] at h
exact h
have hcE_le : cE ≤ cB := by
apply countP_le_countP_of_imp
intro k _
rw [decide_eq_true_eq, decide_eq_true_eq]
exact fun h => lt_trans h hEB
-- The end anchor in terms of the end count.
cases hE : r.end_.key with
| top =>
rw [hE] at hEB
exact absurd hEB (not_lt.mpr (le_top))
| coe e =>
have hcE' : cE = db.keys.countP (fun k : Key => decide (k < e)) := by
rw [hcE, hE, countP_lt_coe]
have hance : r.end_.anchor db = (cE : ℤ) - ((-r.end_.offset).toNat : ℤ) - 1 := by
rw [anchor_of_coe (s := r.end_) hE, ← hcE']
have hoff : ((-r.end_.offset).toNat : ℤ) = -r.end_.offset :=
Int.toNat_of_nonneg (by omega)
rw [hoff]
ring
-- From `cE ≤ cB = cH + (beginSlack + 1)` and the end anchor bound.
have hreport : reportedBelowBegin r res = beginSlack r + 1 := hnonclamp
have hqle : (r.end_.anchor db).toNat ≤ cE - (-r.end_.offset).toNat - 1 := by
rw [hance]
have : 0 ≤ (cE : ℤ) - ((-r.end_.offset).toNat : ℤ) - 1 := by
have := hanchor_nonneg; rw [hance] at this; exact this
omega
have hlenle : (RangeRead.completeKeys db r).length ≤ beginSlack r := by
rw [hlen, hcnt_q, hcnt_head]
have hcE_bound : cE ≤ cH + beginSlack r + 1 := by
have h1 : cE ≤ cH + reportedBelowBegin r res := le_trans hcE_le (le_of_eq hcount)
rw [hreport] at h1
omega
have hnat : cE - (-r.end_.offset).toNat - 1 ≤ cH + beginSlack r := by omega
omega
have ht_ge : beginSlack r + 1 ≤ (RangeRead.completeKeys db r).length := by
have := hpre.length_le
have hrep : beginSlack r + 1 ≤ res.keys.length := by
rw [← hreport]
unfold reportedBelowBegin
exact List.countP_le_length
omega
omega
/-- Given agreement on the gap `[head, begin.key)`, the begin selector of a compatible
nonclamping database resolves onto the reported head in every database that also stores
the head. Both counts below `begin.key` split at the head, and the gap length is exactly
`beginSlack + 1`, so the anchor is the rank of the head. -/
theorem resolve_begin_eq_head_of_agree_negCross_trunc_nonclamp_nonpos
{db₁ db₂ : DB} {r : RangeRead} {res : Result}
(hcomp₁ : RangeRead.Compatible db₁ r res) (hrev : r.reverse = false)
(hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1)
(hagree : db₁.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧
(k : WithTop Key) < r.begin.key)) =
db₂.keys.filter (fun k : Key => decide
(((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧
(k : WithTop Key) < r.begin.key)))
(hhead_mem₂ : res.keys.head hne ∈ db₂.keys) :
resolve db₂ r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
set head : Key := res.keys.head hne with hhead
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross
-- Split the stored keys below `begin.key` at the head in both databases.
have hsplit₁ := countP_lt_eq_add_filter_withTop db₁.keys db₁.nodup
(a := head) (B := r.begin.key) (le_of_lt hcross')
have hsplit₂ := countP_lt_eq_add_filter_withTop db₂.keys db₂.nodup
(a := head) (B := r.begin.key) (le_of_lt hcross')
have hsplit₁' : db₁.keys.countP (fun k : Key => decide
(((k : Key) : WithTop Key) < r.begin.key)) =
db₁.keys.countP (fun k : Key => decide (k < head)) +
db₁.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) := by
rw [hsplit₁, ← List.countP_eq_length_filter]
have hsplit₂' : db₂.keys.countP (fun k : Key => decide
(((k : Key) : WithTop Key) < r.begin.key)) =
db₂.keys.countP (fun k : Key => decide (k < head)) +
db₂.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) := by
rw [hsplit₂, ← List.countP_eq_length_filter]
have hcount₁ := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp₁ hrev hmore hne hbegin
hcross' hnonclamp
rw [← hhead] at hcount₁
-- The gap length is `beginSlack + 1`.
have hgap₁ : db₁.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by
omega
-- Agreement preserves the gap count.
have hgap₂ : db₂.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by
rw [List.countP_eq_length_filter, ← hagree, ← List.countP_eq_length_filter]
exact hgap₁
-- The anchor is the rank of the head.
have hanchor₂ : r.begin.anchor db₂ =
(db₂.keys.countP (fun k : Key => decide (k < head)) : ℤ) := by
rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hsplit₂', hgap₂]
push_cast
ring
have hlt : db₂.keys.countP (fun k : Key => decide (k < head)) < db₂.keys.length := by
obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_mem₂
rw [countP_lt_eq_of_getElem db₂.sorted hi hix]
exact hi
have hnat : (r.begin.anchor db₂).toNat =
db₂.keys.countP (fun k : Key => decide (k < head)) := by
rw [hanchor₂, Int.toNat_natCast]
have h0 : 0 ≤ r.begin.anchor db₂ := by
rw [hanchor₂]; exact Int.natCast_nonneg _
have hlt' : (r.begin.anchor db₂).toNat < db₂.keys.length := by
rw [hnat]; exact hlt
have hget : db₂.keys[(r.begin.anchor db₂).toNat] = head := by
rw [getElem_congr_idx (h' := hnat)]
exact getElem_countP_lt db₂.sorted hhead_mem₂
rw [resolve_eq_getElem h0 hlt']
exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hget
/-- The closed form for the crossed negative-begin truncated nonclamping cell with a
nonpositive end offset: the reported head up to the larger of `begin.key` and
`end_.key`. -/
noncomputable def closedFormNegCrossTruncNonclampNonpos (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key),
max r.begin.key r.end_.key⟩
/-- **Compatibility transfers across agreement on the nonpositive-end crossed
truncated negative-begin nonclamping range.** -/
theorem compatible_of_agree_negCross_trunc_nonclamp_nonpos
{r : RangeRead} {res : Result}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1)
(hend : r.end_.offset ≤ 0)
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
(hagree : AgreeOn (closedFormNegCrossTruncNonclampNonpos r res) db1 db2) :
RangeRead.Compatible db2 r res := by
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hheadD : res.keys.headD ([] : Key) = head := by
rw [hhead, List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
rw [hhead]; exact hcross
have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by
rw [hhead]
exact resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross
(by omega)
have hcr : closedFormNegCrossTruncNonclampNonpos r res =
⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by
unfold closedFormNegCrossTruncNonclampNonpos
rw [hheadD]
set upper : WithTop Key := max r.begin.key r.end_.key with hupper
have hhead_upper : ((head : Key) : WithTop Key) < upper := by
rw [hupper]
exact lt_of_lt_of_le hcross' (le_max_left _ _)
have hlast_mem_res : last ∈ res.keys := by
rw [hlast]; exact List.getLast_mem hne
have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r :=
(RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
(RangeRead.mem_completeKeys.mp hlast_ck1).2.2
have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt
have hhead_le_last : head ≤ last := by
simp only [hhead, hlast]
exact hsort.rel_getLast (List.head_mem hne)
have hlast_upper : ((last : Key) : WithTop Key) < upper := by
have hle := resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend
exact lt_of_lt_of_le (lt_of_lt_of_le hlt1 hle) (by rw [hupper]; exact le_max_right _ _)
have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampNonpos r res) db1 db2).mp hagree
have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) →
(k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by
intro k hk1 hk2
refine hmem k ?_
rw [hcr]
exact ⟨hk1, hk2⟩
have hhead_db1 : head ∈ db1.keys :=
RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne)
have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1
-- Agreement on the gap `[head, begin.key)` forces the second begin resolution.
have hB_upper : r.begin.key ≤ upper := by rw [hupper]; exact le_max_left _ _
have hgap : db1.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧
(k : WithTop Key) < r.begin.key)) =
db2.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧
(k : WithTop Key) < r.begin.key)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨hk1, hge, hlt⟩
exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge, hlt⟩
· rintro ⟨hk2, hge, hlt⟩
exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge, hlt⟩
have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) :=
resolve_begin_eq_head_of_agree_negCross_trunc_nonclamp_nonpos hc1 hrev hmore hne hbegin
hcross hnonclamp hgap hhead_db2
-- The end resolution transfers through the agreement filter below `end_.key`.
have hne_top : resolve db1 r.end_ ≠ (⊤ : WithTop Key) :=
resolve_ne_top_of_offset_nonpos hend
obtain ⟨q, hq⟩ := WithTop.ne_top_iff_exists.mp hne_top
have hlast_q : last < q := by
have h := hlt1
rw [← hq] at h
exact WithTop.coe_lt_coe.mp h
have hqne : q ≠ ([] : Key) := by
intro hnil
have h : last < ([] : Key) := by rwa [hnil] at hlast_q
exact absurd h (List.not_lt_nil last)
have hq_le_E : ((q : Key) : WithTop Key) ≤ r.end_.key := by
rw [hq]
exact resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend
have hhead_q : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) :=
WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hhead_le_last hlast_q))
have hEnd_upper : r.end_.key ≤ upper := by
rw [hupper]; exact le_max_right _ _
have hagreeEnd : db1.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) =
db2.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨hk1, hge, hlt⟩
exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mp hk1, hge, hlt⟩
· rintro ⟨hk2, hge, hlt⟩
exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mpr hk2, hge, hlt⟩
have hqget := resolve_filter_reverse_eq_some_of_ne_nil (db := db1) (s := r.end_) hend hqne hq.symm
have hresEnd2 : resolve db2 r.end_ = ((q : Key) : WithTop Key) :=
resolve_eq_of_agree_between (db₁ := db1) (db₂ := db2) (s := r.end_) hend
(E := r.end_.key) rfl (le_trans hhead_q hq_le_E) hagreeEnd hqget hhead_q
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by
rw [hresEnd2]
exact WithTop.coe_lt_coe.mpr hlast_q
have hres1len : (RangeRead.completeKeys db1 r).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
rwa [← hlast] at h
have hck1 : (RangeRead.completeKeys db1 r).filter
(fun k : Key => decide (k ≤ last)) =
db1.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1
have hck2 : (RangeRead.completeKeys db2 r).filter
(fun k : Key => decide (k ≤ last)) =
db2.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2
have heq : db1.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) =
db2.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨hk1, hge, hle⟩
exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1,
hge, hle⟩
· rintro ⟨hk2, hge, hle⟩
exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2,
hge, hle⟩
have h2 : (RangeRead.completeKeys db2 r).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
rw [hck2, ← heq, ← hck1, hres1len]
rw [compatible_iff_truncated_keys hrev hmore hne]
exact ⟨by simpa using hc1.1, h2⟩
/-- When the database is the reported keys followed by fillers at or above
`begin.key`, the negative begin anchor lands on the reported head. -/
theorem resolve_begin_eq_head_of_append_ge_begin {db : DB} {r : RangeRead} {res : Result}
{F : List Key} (hdb : db.keys = res.keys ++ F)
(hF : ∀ x ∈ F, r.begin.key ≤ (x : WithTop Key))
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1) :
resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
have hFzero : F.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by
rw [List.countP_eq_zero]
intro x hx
rw [decide_eq_true_eq]
exact not_lt.mpr (hF x hx)
have hcnt : db.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
reportedBelowBegin r res := by
rw [hdb, List.countP_append, hFzero, Nat.add_zero]
rfl
have hanchor : r.begin.anchor db = 0 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt, hnonclamp]
push_cast
ring
have htoNat : (r.begin.anchor db).toNat = 0 := by rw [hanchor]; rfl
have hpos : 0 < db.keys.length := by
rw [hdb, List.length_append]
exact Nat.lt_of_lt_of_le (List.length_pos_of_ne_nil hne) (Nat.le_add_right _ _)
have hlt : (r.begin.anchor db).toNat < db.keys.length := by rw [htoNat]; exact hpos
have hopt0 : db.keys[0]? = some (res.keys.head hne) := by
rw [hdb, List.getElem?_append]
rw [if_pos (List.length_pos_of_ne_nil hne)]
rw [← List.head?_eq_getElem?]
exact List.head?_eq_some_head hne
have hopt : db.keys[(r.begin.anchor db).toNat]? = some (res.keys.head hne) := by
rw [htoNat]
exact hopt0
have hval : db.keys[(r.begin.anchor db).toNat] = res.keys.head hne :=
Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans hopt)
rw [resolve_eq_getElem (db := db) (s := r.begin) (by rw [hanchor]) hlt]
exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval
/-- **Above-begin keys are necessary in the nonpositive-end crossed truncated
nonclamping cell.** A witness stores the reported keys together with
`(1 - end.offset)` fillers above the reported last; erasing any filler lowers the end
anchor by one, so the end resolution drops from above the last reported key to the last
reported key itself. -/
theorem exists_witness_negCross_trunc_nonclamp_nonposEnd {r : RangeRead} {res : Result}
{k : Key}
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1)
(hend : r.end_.offset ≤ 0)
(hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key)
(hkge : r.begin.key ≤ (k : WithTop Key)) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧
¬ RangeRead.Compatible (dbw.erase k) r res := by
obtain ⟨db, hcomp⟩ := hread
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
(hsort.pairwise.imp le_of_lt).rel_getLast hx
have hhead_le_last : head ≤ last := by
simp only [hhead, hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
rw [hhead]; exact hcross
have hlast_ck : last ∈ RangeRead.completeKeys db r :=
(RangeRead.compatible_keys_prefix hcomp).subset (List.getLast_mem hne)
obtain ⟨hlast_mem, -, hlast_res⟩ := RangeRead.mem_completeKeys.mp hlast_ck
have hlastE : ((last : Key) : WithTop Key) < r.end_.key :=
lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res
have hfill := filler_count_nonpos_more_nonpos_end hcomp hrev hmore hne
(le_of_lt hbegin) hend
set m := (1 - r.end_.offset).toNat with hm
have hm1 : 1 ≤ m := by rw [hm]; omega
set G : List Key := db.keys.filter
(fun x : Key => decide (last < x ∧ (x : WithTop Key) < r.end_.key)) with hG
have hGlen : m ≤ G.length := hfill
have hGnodup : G.Nodup := by
rw [hG]; exact db.sorted.pairwise.nodup.filter _
have hGmem : ∀ x ∈ G, last < x ∧ (x : WithTop Key) < r.end_.key := by
intro x hx
rw [hG] at hx
exact of_decide_eq_true (List.mem_filter.mp hx).2
have hGdb : ∀ x ∈ G, x ∈ db.keys := by
intro x hx
rw [hG] at hx
exact (List.mem_filter.mp hx).1
-- Nonclamping places every stored key above the reported last at or above `begin.key`.
have hGge : ∀ x ∈ G, r.begin.key ≤ (x : WithTop Key) := by
intro x hx
by_contra hcon
exact not_stored_between_last_begin_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin
hcross hnonclamp (hGdb x hx) (hGmem x hx).1 (lt_of_not_ge hcon)
set raw : List Key := (k :: G.erase k).take m with hraw
have hrawNodup : raw.Nodup := by
rw [hraw]
refine (List.take_sublist m (k :: G.erase k)).nodup ?_
exact List.nodup_cons.mpr ⟨hGnodup.not_mem_erase, hGnodup.erase k⟩
have hrawLen : raw.length = m := by
rw [hraw, List.length_take]
apply Nat.min_eq_left
by_cases hkG : k ∈ G
· rw [List.length_cons, List.length_erase_of_mem hkG]; omega
· rw [List.length_cons, List.erase_of_not_mem hkG]; omega
have hrawSub : ∀ x ∈ raw, x = k ∨ x ∈ G := by
intro x hx
rw [hraw] at hx
rcases List.mem_cons.mp (List.mem_of_mem_take hx) with rfl | hx'
· exact Or.inl rfl
· exact Or.inr (List.mem_of_mem_erase hx')
have hrawk : k ∈ raw := by
rw [hraw, List.mem_take_iff_getElem]
refine ⟨0, ?_, rfl⟩
rw [List.length_cons]
omega
have hrawGt : ∀ x ∈ raw, last < x := by
intro x hx
rcases hrawSub x hx with rfl | hx'
· exact hkgt
· exact (hGmem x hx').1
have hrawLt : ∀ x ∈ raw, (x : WithTop Key) < r.end_.key := by
intro x hx
rcases hrawSub x hx with rfl | hx'
· exact hkE
· exact (hGmem x hx').2
have hrawGe : ∀ x ∈ raw, r.begin.key ≤ (x : WithTop Key) := by
intro x hx
rcases hrawSub x hx with rfl | hx'
· exact hkge
· exact hGge x hx'
set F : List Key := raw.insertionSort (· ≤ ·) with hF
have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm
have hFsorted : F.SortedLT := by
rw [hF, List.sortedLT_iff_pairwise]
exact DB.pairwise_lt_of_le_of_nodup
(List.pairwise_insertionSort (r := (· ≤ ·)) raw)
(hFperm.nodup_iff.mp hrawNodup)
have hFlen : F.length = m := by rw [hF, List.length_insertionSort, hrawLen]
have hFk : k ∈ F := hFperm.mem_iff.mp hrawk
have hFmem : ∀ x ∈ F, last < x ∧ (x : WithTop Key) < r.end_.key := by
intro x hx
exact ⟨hrawGt x (hFperm.mem_iff.mpr hx), hrawLt x (hFperm.mem_iff.mpr hx)⟩
have hFge : ∀ x ∈ F, r.begin.key ≤ (x : WithTop Key) := fun x hx =>
hrawGe x (hFperm.mem_iff.mpr hx)
have hFne : F ≠ [] := by
intro h; rw [h] at hFlen; simp at hFlen; omega
set D : List Key := res.keys ++ F with hD
have hDsort : D.SortedLT := by
rw [hD]
refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩
intro a ha b hb
exact lt_of_le_of_lt (hle_last a ha) (hFmem b hb).1
let dbw : DB := ⟨D, hDsort⟩
have hdbw_keys : dbw.keys = D := rfl
-- The begin selector still resolves to the reported head.
have hrb : resolve dbw r.begin = ((head : Key) : WithTop Key) := by
rw [hhead]
exact resolve_begin_eq_head_of_append_ge_begin hdbw_keys hFge hne hbegin hnonclamp
-- every stored key is below the end key
have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by
intro x hx
rw [hD] at hx
rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE
· exact (hFmem x hx).2
have hlenD : D.length = res.keys.length + m := by
rw [hD, List.length_append, hFlen]
have hm_int : (m : Int) = 1 - r.end_.offset := by
rw [hm]; exact Int.toNat_of_nonneg (by omega)
have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
D.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact hDlt x hx
have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by
cases hE : r.end_.key with
| top =>
rw [anchor_of_top (s := r.end_) hE, hlenD]
push_cast
rw [hm_int]
ring
| coe e =>
have hce : D.countP (fun x : Key => decide (x < e)) = D.length := by
rw [← countP_lt_coe (l := D) e, ← hE]
exact hcount_end
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD]
push_cast
rw [hm_int]
ring
have ht_lt : res.keys.length < D.length := by rw [hlenD]; omega
have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by
change D[res.keys.length]? = some (F.head hFne)
rw [hD, List.getElem?_append]
rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self]
rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne]
have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by
rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)]
rw [hanchor_end, Int.toNat_natCast, hDget?]
have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by
rw [hre]
exact WithTop.coe_lt_coe.mpr (hFmem (F.head hFne) (List.head_mem hFne)).1
-- complete keys of the witness are the reported keys
have hckw : (RangeRead.completeKeys dbw r).filter
(fun x : Key => decide (x ≤ last)) = res.keys := by
rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF]
rw [hdbw_keys, hD, List.filter_append]
have hres_fil : res.keys.filter (fun x : Key => decide
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
exact ⟨WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx), hle_last x hx⟩
have hF_fil : F.filter (fun x : Key => decide
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by
rw [List.filter_eq_nil_iff]
intro x hx
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and]
exact fun _ => not_le.mpr (hFmem x hx).1
rw [hres_fil, hF_fil, List.append_nil]
have hcompw : RangeRead.Compatible dbw r res := by
rw [compatible_iff_truncated_keys hrev hmore hne]
exact ⟨hcomp.1, hckw⟩
have hkdbw : k ∈ dbw.keys := by
rw [hdbw_keys, hD]
exact List.mem_append.mpr (Or.inr hFk)
-- now the erased database
have hk_not_mem_res : k ∉ res.keys := by
intro hk
exact absurd (hle_last k hk) (not_le.mpr hkgt)
have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by
rw [DB.erase_keys, hdbw_keys, hD]
exact List.erase_append_right F hk_not_mem_res
have hF'ge : ∀ x ∈ F.erase k, r.begin.key ≤ (x : WithTop Key) :=
fun x hx => hFge x (List.mem_of_mem_erase hx)
have hrb' : resolve (dbw.erase k) r.begin = ((head : Key) : WithTop Key) := by
rw [hhead]
exact resolve_begin_eq_head_of_append_ge_begin hD' hF'ge hne hbegin hnonclamp
have hD'lt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by
intro x hx
exact hDlt x (by
rw [DB.erase_keys, hdbw_keys] at hx
exact List.mem_of_mem_erase hx)
have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by
rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen]
omega
have hcount_end' : (dbw.erase k).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact hD'lt x hx
have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by
cases hE : r.end_.key with
| top =>
rw [anchor_of_top (s := r.end_) hE, hlenD']
rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)]
push_cast
rw [hm_int]
ring
| coe e =>
have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) =
(dbw.erase k).keys.length := by
rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE]
exact hcount_end'
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD']
rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)]
push_cast
rw [hm_int]
ring
have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne
have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by
rw [Nat.cast_sub ht1]; norm_num
have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by
rw [hanchor_end', hEqInt, Int.toNat_natCast]
have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by
rw [hD', List.getElem?_append]
rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)]
rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)]
congr 1
rw [hlast, List.getLast_eq_getElem hne]
have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by
rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)]
rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?]
have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev]), hrb', hre']
have hpred : ∀ x ∈ (dbw.erase k).keys,
decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧
(x : WithTop Key) < ((↑last : Key) : WithTop Key)) =
decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ x < last) := by
intro x _
rw [decide_eq_decide]
exact and_congr_right fun _ => WithTop.coe_lt_coe
rw [List.filter_congr hpred, hD', List.filter_append]
have hhead_le_all : ∀ x ∈ res.keys, ((head : Key) : WithTop Key) ≤ (x : WithTop Key) := by
intro x hx
exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx)
rw [show res.keys.filter (fun x : Key => decide
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) =
res.keys.filter (fun x : Key => decide (x < last)) from by
apply List.filter_congr
intro x hx
rw [decide_eq_decide]
exact ⟨fun h => h.2, fun h => ⟨hhead_le_all x hx, h⟩⟩]
conv_lhs => rw [← List.dropLast_append_getLast hne, hlast]
rw [List.filter_append]
have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
have h := hsort.pairwise.rel_dropLast_getLast hx
simpa [hlast] using h
rw [hfil]
have hFnil : (F.erase k).filter (fun x : Key => decide
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) = [] := by
rw [List.filter_eq_nil_iff]
intro x hx
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and]
intro _
exact not_lt.mpr (le_of_lt (hFmem x (List.mem_of_mem_erase hx)).1)
rw [hFnil, List.append_nil]
simp
have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by
rw [compatible_iff_truncated_keys hrev hmore hne]
intro h
have heq := h.2
rw [hckE] at heq
have hfil : res.keys.dropLast.filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
rw [← hlast]
exact hle_last x (List.mem_of_mem_dropLast hx)
rw [hfil] at heq
have hlen := congrArg List.length heq
rw [List.length_dropLast] at hlen
omega
exact ⟨dbw, hcompw, hkdbw, hnot⟩
/-- A key at or above `begin.key` and below `end.key` is necessary in the crossed
negative-begin truncated nonclamping cell with a nonpositive end offset. -/
theorem necessary_negCross_trunc_above_begin_nonposEnd {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hnonclamp : reportedBelowBegin r res = beginSlack r + 1)
(hend : r.end_.offset ≤ 0)
(hkgt : res.keys.getLast hne < k)
(hkE : (k : WithTop Key) < r.end_.key)
(hkge : r.begin.key ≤ (k : WithTop Key)) :
Necessary r res k := by
obtain ⟨dbw, hcompw, hkdbw, hnot⟩ :=
exists_witness_negCross_trunc_nonclamp_nonposEnd hread hrev hmore hne hbegin hcross
hnonclamp hend hkgt hkE hkge
exact necessary_of_toggle ⟨dbw, hcompw, by
rw [toggle_eq_erase_of_mem hkdbw]
exact hnot⟩
/-- The crossed negative-begin truncated nonclamping cell with a nonpositive end
offset. -/
def negCrossTruncNonclampNonposEnd (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
r.end_.offset ≤ 0 ∧ reportedBelowBegin r res = beginSlack r + 1
/-- **The crossed negative-begin truncated nonclamping nonpositive-end closed form is
minimal.** -/
theorem minimal_closedForm_of_negCross_trunc_nonclamp_nonposEnd {r : RangeRead} {res : Result}
(hread : Readable r res) (h : negCrossTruncNonclampNonposEnd r res) :
Minimal r res (closedFormNegCrossTruncNonclampNonpos r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hnonclamp⟩ := h
obtain ⟨db₀, hcomp₀⟩ := hread
have hread' : Readable r res := ⟨db₀, hcomp₀⟩
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hheadD : res.keys.headD ([] : Key) = head := by
rw [hhead, List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hcross : ((head : Key) : WithTop Key) < r.begin.key := by
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
simp [leastReported, hrev]
rw [hleast, hheadD] at hcrossD
exact hcrossD
have hcr : closedFormNegCrossTruncNonclampNonpos r res =
⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by
unfold closedFormNegCrossTruncNonclampNonpos
rw [hheadD]
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
· intro db1 db2 hagree
constructor
· intro hc1
exact compatible_of_agree_negCross_trunc_nonclamp_nonpos hrev hmore hne hbegin hcross
hnonclamp hend hc1 hagree
· intro hc2
exact compatible_of_agree_negCross_trunc_nonclamp_nonpos hrev hmore hne hbegin hcross
hnonclamp hend hc2 (AgreeOn.symm hagree)
· intro k hklo hkhi
rw [hcr] at hklo hkhi
have hhead_nec : Necessary r res head :=
necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne)
have hlast_nec : Necessary r res last :=
necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne)
have hhead_le_last : head ≤ last := by
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
simp only [hhead, hlast]
exact hsort.rel_getLast (List.head_mem hne)
by_cases hkle : k ≤ last
· exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle
· have hkgt : last < k := lt_of_not_ge hkle
by_cases hkB : (k : WithTop Key) < r.begin.key
· exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross
hnonclamp (by rw [← hlast]; exact hkgt) hkB
· have hkge : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hkB
by_cases hleE : r.begin.key ≤ r.end_.key
· have hmax : max r.begin.key r.end_.key = r.end_.key := max_eq_right hleE
have hkE : (k : WithTop Key) < r.end_.key := by rw [← hmax]; exact hkhi
exact necessary_negCross_trunc_above_begin_nonposEnd hread' hrev hmore hne hbegin
hcross hnonclamp hend (by rw [← hlast]; exact hkgt) hkE hkge
· exfalso
have hltE : r.end_.key < r.begin.key := lt_of_not_ge hleE
have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hltE)
rw [hmax] at hkhi
exact absurd hkhi (not_lt.mpr hkge)
end FormalConflictRange