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
This commit is contained in:
andrew committed 2026-11-16 21:53:59 -05:00
1 parent 7a0826e873
commit a54a5f82f8
78 files changed
+91636 -2

No files matched your search

@@ -0,0 +1,761 @@
/-
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.NegCrossTruncNonposGap
/-!
# The crossed truncated nonpositive-end gap cell at `end_.key < begin.key`
This module closes the non-clamping (`¬ clampNonposBottom`) half of the crossed
negative-begin truncated nonpositive-end gap cell in the sub-region where the end key
lies strictly below the begin key. The minimal sound range is `[head, begin.key)`.
The key structural fact is that readability plus `¬ clampNonposBottom` forces the begin
slack to equal the reported count plus the end overshoot,
`beginSlack r = res.keys.length + clampNonposD r`. Consequently every compatible
database supplies exactly `clampNonposD r + 1` filler keys in `(last, end_.key)`, and
the closed form is proved by the usual erase (for keys below `end_.key`) and insert
(for keys between `end_.key` and `begin.key`) witnesses.
-/
namespace FormalConflictRange
/-- The crossed negative-begin truncated nonpositive-end gap cell in the sub-region
`end_.key < begin.key` with a non-clamping begin selector. -/
def negCrossTruncNonposGapA2aNonbottom (r : RangeRead) (res : Result) : Prop :=
negCrossTruncNonposGap r res ∧ r.end_.key < r.begin.key ∧ ¬ clampNonposBottom r res
/-- The closed form of the crossed nonpositive-end gap cell in the non-clamping
`end_.key < begin.key` sub-region: `[head, begin.key)`. -/
noncomputable def closedFormNegCrossTruncNonposGapA2aNonbottom
(r : RangeRead) (res : Result) : ConflictRange :=
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩
/-- All reported keys lie strictly below the begin key once the reported last does. -/
theorem reportedBelowBegin_eq_length_of_last_lt {r : RangeRead} {res : Result}
(hne : res.keys ≠ []) (hsort : res.keys.SortedLT)
(hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) :
reportedBelowBegin r res = res.keys.length := by
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
have hxle : x ≤ res.keys.getLast hne :=
(hsort.pairwise.imp le_of_lt).rel_getLast hx
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hxle) (by rwa [hlastD] at hlastB)
/-- In the non-clamping gap cell every compatible database resolves its begin selector
onto the reported head. -/
theorem resolve_begin_eq_head_of_nonbottom {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) (hend : r.end_.offset ≤ 0)
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
(hnbot : ¬ clampNonposBottom r res) :
resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
have hanc : 0 ≤ r.begin.anchor db := by
by_contra h
exact hnbot (clampNonposBottom_of_anchor_neg_trunc hcomp hrev hmore hne hbegin hend
hclamp (by omega))
have hpre := RangeRead.compatible_keys_prefix hcomp
have hckne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne
obtain ⟨a, _ha_mem, ha_res, ha_head⟩ :=
RangeRead.completeKeys_head_resolve hrev hckne hanc
have hhead_eq_a : res.keys.head hne = a := by
rw [← ha_head]
exact hpre.head hne
rw [hhead_eq_a]
exact ha_res
/-- In the `end_.key < begin.key` non-clamping sub-region, readability forces the begin
slack to equal the reported count plus the end overshoot. -/
theorem beginSlack_eq_of_A2a_nonbottom {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) (hend : r.end_.offset ≤ 0)
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
(hnbot : ¬ clampNonposBottom r res)
(hEB : r.end_.key < r.begin.key)
(hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key)
(hlastE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key) :
beginSlack r = res.keys.length + clampNonposD r := by
classical
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have 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_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne
have hhead_le_last : head ≤ last := hle_last head hhead_mem
have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB
have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by rwa [hlastD] at hlastE
have hheadB : ((head : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlastB'
have hres : resolve db r.begin = ((head : Key) : WithTop Key) := by
have := resolve_begin_eq_head_of_nonbottom hcomp hrev hmore hne hbegin hend hclamp hnbot
simpa [hhead] using this
have hanc : 0 ≤ r.begin.anchor db := by
by_contra h
exact hnbot (clampNonposBottom_of_anchor_neg_trunc hcomp hrev hmore hne hbegin hend
hclamp (by omega))
have hgap : db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 :=
gap_count_eq_beginSlack_of_resolve_head hbegin hheadB hanc hres
-- stored keys in `[head, last]` are exactly the reported keys
have hresEnd_gt : ((last : Key) : WithTop Key) < resolve db r.end_ := by
have hlast_ck : last ∈ RangeRead.completeKeys db r :=
(RangeRead.compatible_keys_prefix hcomp).subset hlast_mem
exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2
have hfilterComp : (RangeRead.completeKeys db r).filter
(fun k => decide (k ≤ last)) = res.keys :=
((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2
have hfilterBegin : (RangeRead.completeKeys db r).filter
(fun k => decide (k ≤ last)) =
db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) :=
completeKeys_filter_le_of_resolve_begin hrev hres hresEnd_gt
have hlen_head_last : (db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last))).length
= res.keys.length := by
rw [← hfilterBegin, hfilterComp]
have hfilter_ka : db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) =
db.keys.filter (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) := by
apply List.filter_congr
intro k _
rw [decide_eq_decide]
by_cases hk : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)
· simp only [hk, true_and]
exact WithTop.coe_lt_coe.trans (lt_keyAfter_iff k last)
· simp only [hk, false_and]
have hlen_ka : (between db ((head : Key) : WithTop Key)
(((keyAfter last : Key) : WithTop Key))).length = res.keys.length := by
unfold between
rw [hfilter_ka, hlen_head_last]
have hcntB : cnt db r.begin.key =
cnt db ((head : Key) : WithTop Key) +
db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) := by
rw [cnt_eq_add_between db (le_of_lt hheadB), between, List.countP_eq_length_filter]
have hcntKa : cnt db (((keyAfter last : Key) : WithTop Key)) =
cnt db ((head : Key) : WithTop Key) +
(between db ((head : Key) : WithTop Key)
(((keyAfter last : Key) : WithTop Key))).length :=
cnt_eq_add_between db (WithTop.coe_le_coe.mpr
(le_trans hhead_le_last (le_of_lt (keyAfter_self_lt last))))
have hcntKB : cnt db r.begin.key =
cnt db (((keyAfter last : Key) : WithTop Key)) +
(between db (((keyAfter last : Key) : WithTop Key)) r.begin.key).length :=
cnt_eq_add_between db (keyAfter_le_of_lt hlastB')
have hresEnd_ne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by
intro h
rw [h] at hresEnd_gt
exact absurd hresEnd_gt
(not_lt.mpr (coe_nil_le_key_withTop ((last : Key) : WithTop Key)))
have hbetween_len : (between db (resolve db r.end_) r.end_.key).length
= clampNonposD r + 1 :=
between_resolve_end_length hend hresEnd_ne
have hka_le_resEnd : ((keyAfter last : Key) : WithTop Key) ≤ resolve db r.end_ :=
keyAfter_le_of_lt hresEnd_gt
have hG : clampNonposD r + 1 ≤
(between db (((keyAfter last : Key) : WithTop Key)) r.begin.key).length := by
rw [← hbetween_len]
apply List.Sublist.length_le
show List.Sublist
(db.keys.filter (fun k : Key => decide
(resolve db r.end_ ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.end_.key)))
(db.keys.filter (fun k : Key => decide
(((keyAfter last : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)))
apply filter_sublist_filter_of_imp
intro k hk
rw [decide_eq_true_eq] at hk ⊢
exact ⟨le_trans hka_le_resEnd hk.1, lt_trans hk.2 hEB⟩
have hkey : db.keys.countP (fun k : Key => decide
(((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.begin.key)) = res.keys.length +
(between db (((keyAfter last : Key) : WithTop Key)) r.begin.key).length := by
rw [hcntKa, hlen_ka] at hcntKB
omega
have hlow : res.keys.length + clampNonposD r ≤ beginSlack r := by
omega
have hc0 : intervalCard r.begin.key r.end_.key = 0 :=
intervalCard_eq_zero_of_le (le_of_lt hEB)
have hrep : reportedBelowBegin r res = res.keys.length :=
reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB
have hnot : ¬ (res.keys.length + (clampNonposD r + 1) ≤ beginSlack r) := by
intro h
apply hnbot
unfold clampNonposBottom
rw [hc0, hrep, tsub_zero]
exact_mod_cast h
have hup : beginSlack r ≤ res.keys.length + clampNonposD r := by omega
omega
/-- Readability supplies enough room in `(last, end_.key)` for the erase and insert
witnesses: the end-resolution identity puts `clampNonposD + 1` stored keys there. -/
theorem intervalCard_keyAfter_getLast_ge {db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hne : res.keys ≠ [])
(hend : r.end_.offset ≤ 0) :
(((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key := by
classical
set last : Key := res.keys.getLast hne with hlast
have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne
have hresEnd_gt : ((last : Key) : WithTop Key) < resolve db r.end_ := by
have hlast_ck : last ∈ RangeRead.completeKeys db r :=
(RangeRead.compatible_keys_prefix hcomp).subset hlast_mem
exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2
have hresEnd_ne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by
intro h
rw [h] at hresEnd_gt
exact absurd hresEnd_gt
(not_lt.mpr (coe_nil_le_key_withTop ((last : Key) : WithTop Key)))
have hbetween_len : (between db (resolve db r.end_) r.end_.key).length
= clampNonposD r + 1 :=
between_resolve_end_length hend hresEnd_ne
have hka_le_resEnd : ((keyAfter last : Key) : WithTop Key) ≤ resolve db r.end_ :=
keyAfter_le_of_lt hresEnd_gt
have hresEnd_le_E : resolve db r.end_ ≤ r.end_.key :=
resolve_le_key_of_offset_nonpos (db := db) (s := r.end_) hend
have hkaE : ((keyAfter last : Key) : WithTop Key) ≤ r.end_.key :=
le_trans hka_le_resEnd hresEnd_le_E
have hcard := between_length_le_intervalCard (db := db)
(E := ((keyAfter last : Key) : WithTop Key)) (B := r.end_.key) hkaE
have hlen_le : (between db (resolve db r.end_) r.end_.key).length ≤
(between db (((keyAfter last : Key) : WithTop Key)) r.end_.key).length := by
apply List.Sublist.length_le
show List.Sublist
(db.keys.filter (fun k : Key => decide
(resolve db r.end_ ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.end_.key)))
(db.keys.filter (fun k : Key => decide
(((keyAfter last : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < r.end_.key)))
apply filter_sublist_filter_of_imp
intro k hk
rw [decide_eq_true_eq] at hk ⊢
exact ⟨le_trans hka_le_resEnd hk.1, hk.2⟩
calc (((clampNonposD r + 1 : ℕ)) : ℕ∞)
= ((between db (resolve db r.end_) r.end_.key).length : ℕ∞) := by rw [hbetween_len]
_ ≤ ((between db (((keyAfter last : Key) : WithTop Key)) r.end_.key).length : ℕ∞) :=
Nat.cast_le.mpr hlen_le
_ ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key := hcard
/-- The element at the seam of a split list is the head of the right part. -/
theorem getElem_append_length {as bs : List Key} (hbs : bs ≠ []) :
(as ++ bs)[as.length]'(by
rw [List.length_append]; have := List.length_pos_iff.mpr hbs; omega) =
bs.head hbs := by
rw [List.getElem_append_right (as := as) (bs := bs) (i := as.length) (by omega)
(h₂ := by
rw [List.length_append]; have := List.length_pos_iff.mpr hbs; omega)]
simp only [Nat.sub_self]
exact (List.head_eq_getElem_zero hbs).symm
/-- Padding the reported keys with filler keys in `(last, end_.key)` (all of them below
`end_.key` and hence below `begin.key`) gives a compatible database whose begin selector
resolves to the reported head and whose end selector resolves to the first filler. -/
theorem compatible_fillers_A2a {r : RangeRead} {res : Result} {dbF : DB}
(hsort : res.keys.SortedLT) (hrev : r.reverse = false) (hmore : res.more = true)
(hne : res.keys ≠ [])
(hlim : RangeRead.withinLimit r res.keys)
(hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0)
(hslack : beginSlack r = res.keys.length + clampNonposD r)
(hEB : r.end_.key < r.begin.key)
(hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key)
(hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x)
(hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key)
(hFlen : dbF.keys.length = clampNonposD r + 1) :
∃ dbw : DB, dbw.keys = res.keys ++ dbF.keys ∧ RangeRead.Compatible dbw r res ∧
r.begin.anchor dbw = 0 ∧ r.end_.anchor dbw = (res.keys.length : ℤ) := by
classical
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by rw [← hlastD]; exact hlastB
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
(hsort.pairwise.imp le_of_lt).rel_getLast hx
have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by
intro h
rw [h] at hEB
exact absurd hEB (not_lt.mpr le_top)
obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = ((e : Key) : WithTop Key) := by
cases h : r.end_.key with
| top => exact absurd h hEne
| coe e => exact ⟨e, rfl⟩
have hDlen : dbF.keys.length = clampNonposD r + 1 := hFlen
have hFne : dbF.keys ≠ [] := by intro h; rw [h] at hDlen; simp at hDlen
-- sorted concatenation
set D : List Key := res.keys ++ dbF.keys with hD
have hDsort : D.SortedLT := by
rw [hD]
exact List.sortedLT_append.mpr ⟨hsort, dbF.sorted, fun a ha b hb =>
lt_of_le_of_lt (hle_last a ha) (hFlast b hb)⟩
let dbw : DB := ⟨D, hDsort⟩
have hkeyw : dbw.keys = res.keys ++ dbF.keys := rfl
have hnodup : dbw.keys.Nodup := dbw.nodup
-- every filler lies below `begin.key`
have hFltB : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.begin.key := fun x hx =>
lt_of_lt_of_le (hFend x hx) (le_of_lt hEB)
-- begin anchor is zero
have hanchorB : r.begin.anchor dbw = 0 := by
have hc : dbw.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) <
r.begin.key)) = dbw.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
rw [hkeyw] at hx
rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastB'
· exact hFltB x hx
have hc' : ((dbw.keys.length : ℕ) : ℤ) =
res.keys.length + clampNonposD r + 1 := by
rw [hkeyw, List.length_append, hDlen]; push_cast; ring
rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hc]
have hlen : (dbw.keys.length : ℤ) = res.keys.length + clampNonposD r + 1 := by
exact_mod_cast hc'
have hslack' : (beginSlack r : ℤ) = (res.keys.length : ℤ) + clampNonposD r := by
rw [hslack]; push_cast; ring
rw [hlen, hslack']; ring
have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by
have hhead_mem : dbF.keys.head hFne ∈ dbF.keys := List.head_mem hFne
exact lt_trans (WithTop.coe_lt_coe.mpr (hFlast _ hhead_mem)) (hFend _ hhead_mem)
-- end anchor is the reported count
have hcountE : dbw.keys.countP (fun k : Key => decide (k < e)) =
res.keys.length + clampNonposD r + 1 := by
have hcl : dbw.keys.countP (fun k : Key => decide (k < e)) = dbw.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [hkeyw] at hx
rw [decide_eq_true_eq]
rcases List.mem_append.mp hx with hx | hx
· apply WithTop.coe_lt_coe.mp
rw [← he]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E
· apply WithTop.coe_lt_coe.mp
simpa [he] using hFend x hx
rw [hcl, hkeyw, List.length_append, hDlen]
omega
have hanchorE : r.end_.anchor dbw = (res.keys.length : ℤ) := by
rw [anchor_of_coe (s := r.end_) (v := e) he, hcountE]
have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by
rw [clampNonposD_eq]
have hnonneg : 0 ≤ -r.end_.offset := by omega
rw [Int.toNat_of_nonneg hnonneg]; ring
rw [hoff]; push_cast; ring
refine ⟨dbw, hkeyw, ?_, hanchorB, hanchorE⟩
-- compatibility: the complete keys are exactly the reported keys
have hhead_eq : dbw.keys[0]'(by rw [hkeyw, List.length_append]; omega) =
res.keys.head hne := by
have hleft := List.getElem_append_left (as := res.keys) (bs := dbF.keys)
(i := 0) (h := by simp [List.length_pos_iff.mpr hne])
(h' := by simp [List.length_pos_iff.mpr hne])
exact hleft.trans (List.head_eq_getElem_zero hne).symm
have hfill_head_gt : ∀ x ∈ dbF.keys, x ≥ dbF.keys.head hFne := fun x hx =>
(dbF.sorted.pairwise.imp le_of_lt).rel_head hx
have hresolve_end : resolve dbw r.end_ = ((dbF.keys.head hFne : Key) : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor dbw := by rw [hanchorE]; exact_mod_cast Nat.zero_le _
have hlt : (r.end_.anchor dbw).toNat < dbw.keys.length := by
rw [hanchorE, hkeyw, List.length_append, hDlen]
omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.end_.anchor dbw).toNat = res.keys.length := by
rw [hanchorE]; simp
simp only [hidx]
have hget : dbw.keys[res.keys.length]'(by rw [hkeyw, List.length_append, hDlen]; omega) =
dbF.keys.head hFne := by
have h1 : dbw.keys = res.keys ++ dbF.keys := hkeyw
rw [List.getElem_of_eq h1]
exact getElem_append_length hFne
exact congrArg (fun x : Key => (x : WithTop Key)) hget
have hresolve_begin : resolve dbw r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor dbw := by rw [hanchorB]
have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by
rw [hanchorB, hkeyw, List.length_append, hDlen]
simp only [Int.toNat_zero]
omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.begin.anchor dbw).toNat = 0 := by rw [hanchorB]; rfl
simp only [hidx]
exact congrArg (fun x : Key => (x : WithTop Key)) hhead_eq
have hckmem : ∀ k : Key, k ∈ RangeRead.completeKeys dbw r ↔ k ∈ res.keys := by
intro k
rw [RangeRead.mem_completeKeys, hresolve_begin, hresolve_end]
constructor
· rintro ⟨hk, -, hlt⟩
rw [hkeyw] at hk
rcases List.mem_append.mp hk with hk | hk
· exact hk
· exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (hfill_head_gt k hk)))
· intro hk
refine ⟨?_, ?_, ?_⟩
· rw [hkeyw]; exact List.mem_append_left _ hk
· exact WithTop.coe_le_coe.mpr
((hsort.pairwise.imp le_of_lt).rel_head hk)
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk))
(WithTop.coe_lt_coe.mpr (hFlast _ (List.head_mem hFne)))
have hck : RangeRead.completeKeys dbw r = res.keys := by
apply List.SortedLT.eq_of_mem_iff
· rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])]
exact List.sortedLT_iff_pairwise.mpr (RangeRead.inRange_pairwise dbw _ _)
· exact hsort
· exact hckmem
refine ⟨hlim, ?_⟩
left
refine ⟨hmore, ?_, hne⟩
rw [hck]
/-- Inserting a key above every element of a list appends it at the end. -/
theorem orderedInsert_of_forall_lt {l : List Key} {k : Key} (h : ∀ x ∈ l, x < k) :
l.orderedInsert (· ≤ ·) k = l ++ [k] := by
induction l with
| nil => simp
| cons a t ih =>
rw [List.orderedInsert_of_not_le t (r := (· ≤ ·))
(not_le.mpr (h a (List.mem_cons_self ..)))]
have := ih (fun x hx => h x (List.mem_cons_of_mem _ hx))
simp only [this, List.cons_append]
/-- A key strictly between the reported last and `end_.key` has an erase witness in the
A2a non-clamping subcell: the target sits among `clampNonposD + 1` padding keys in
`(last, end_.key)`, so the end selector resolves into the padding; erasing the target
lowers the end resolution to the reported last, which drops the last reported key from
the truncated window. -/
theorem exists_witness_erase_A2a {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aNonbottom r res)
(hne : res.keys ≠ [])
(hkgt : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key))
(hkE : (k : WithTop Key) < r.end_.key) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧
¬ RangeRead.Compatible (dbw.erase k) r res := by
obtain ⟨hcell0, hEB, hnbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlast_lt_k : res.keys.getLast hne < k := WithTop.coe_lt_coe.mp hkgt
have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hlastB
have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hslack : beginSlack r = res.keys.length + clampNonposD r :=
beginSlack_eq_of_A2a_nonbottom hcomp₀ hrev hmore hne hbegin hend hclamp hnbot hEB
hlastB hlastE
have hk_not_res : k ∉ res.keys := by
intro hx
exact absurd hlast_lt_k (not_lt.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx))
-- pad keys in `(last, end_.key)` avoiding `k`
have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤
intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key :=
intervalCard_keyAfter_getLast_ge hcomp₀ hne hend
obtain ⟨dbB, hBmem, hBk, hBlen⟩ :=
exists_db_keysIn_avoid (B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))
(E := r.end_.key) (k := k) (m := clampNonposD r) hcard
have hBgt : ∀ x ∈ dbB.keys, res.keys.getLast hne < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne)))
(hBmem x hx).1)
have hBElt : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hBmem x hx).2
-- assemble `k` with the padding and sort
set raw : List Key := k :: dbB.keys with hraw
have hrawNodup : raw.Nodup := by
rw [hraw]
exact List.nodup_cons.mpr ⟨hBk, dbB.sorted.pairwise.nodup⟩
set F : List Key := raw.insertionSort (· ≤ ·) with hF
have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm
have hFsorted : F.SortedLT := by
rw [hF, List.sortedLT_iff_pairwise]
exact DB.pairwise_lt_of_le_of_nodup
(List.pairwise_insertionSort (r := (· ≤ ·)) raw)
(hFperm.nodup_iff.mp hrawNodup)
let dbF : DB := ⟨F, hFsorted⟩
have hFlen : dbF.keys.length = clampNonposD r + 1 := by
change F.length = clampNonposD r + 1
rw [hF, List.length_insertionSort, hraw, List.length_cons, hBlen]
have hFlt : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by
intro x hx
have hx' := hFperm.mem_iff.mpr hx
rw [hraw] at hx'
rcases List.mem_cons.mp hx' with rfl | hxB
· exact hlast_lt_k
· exact hBgt x hxB
have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := by
intro x hx
have hx' := hFperm.mem_iff.mpr hx
rw [hraw] at hx'
rcases List.mem_cons.mp hx' with rfl | hxB
· exact hkE
· exact hBElt x hxB
obtain ⟨dbw, hkeyw, hcompw, hanchB, hanchE⟩ :=
compatible_fillers_A2a hsort hrev hmore hne hlim hbegin hend hslack hEB hlastB
hFlt hFend hFlen
have hk_in_F : k ∈ dbF.keys :=
hFperm.mem_iff.mp (by rw [hraw]; exact List.mem_cons_self ..)
have hk_in_dbw : k ∈ dbw.keys := by
rw [hkeyw]; exact List.mem_append.mpr (Or.inr hk_in_F)
refine ⟨dbw, hcompw, hk_in_dbw, ?_⟩
intro hcomp'
have herase_keys : (dbw.erase k).keys = res.keys ++ dbF.keys.erase k := by
rw [DB.erase_keys, hkeyw, List.erase_append_right _ hk_not_res]
obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp'
have hcount_erase : (dbw.erase k).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
res.keys.length + clampNonposD r := by
rw [herase_keys, List.countP_append]
have h1 : res.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = res.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_E
have hFall : dbF.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = dbF.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact hFend x hx
have hktrue : (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) k = true := by
rw [decide_eq_true_eq]; exact hkE
have h2 : (dbF.keys.erase k).countP
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = clampNonposD r := by
rw [List.countP_erase, if_pos ⟨hk_in_F, hktrue⟩, hFall, hFlen]
omega
rw [h1, h2]
have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by
rw [clampNonposD_eq]
have hnonneg : 0 ≤ -r.end_.offset := by omega
rw [Int.toNat_of_nonneg hnonneg]; ring
have hanchor_erase : r.end_.anchor (dbw.erase k) = (res.keys.length : ℤ) - 1 := by
cases hE : r.end_.key with
| top => exact absurd hE (ne_of_lt hEne)
| coe e =>
rw [anchor_of_coe (s := r.end_) (v := e) hE]
have hce : (dbw.erase k).keys.countP (fun x => decide (x < e)) =
res.keys.length + clampNonposD r := by
rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE]
exact hcount_erase
rw [hce, hoff]
push_cast
ring
have ht_pos : 0 < res.keys.length := List.length_pos_iff.mpr hne
have hresolve_end : resolve (dbw.erase k) r.end_ =
((res.keys.getLast hne : Key) : WithTop Key) := by
have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanchor_erase]; omega
have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by
rw [herase_keys, List.length_append, List.length_erase, if_pos hk_in_F, hFlen]
omega
have hlt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by
rw [hanchor_erase, hlenErase]
omega
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by
rw [hanchor_erase]
have : ((res.keys.length : ℤ) - 1).toNat = res.keys.length - 1 := by omega
exact this
have hget : (dbw.erase k).keys[(r.end_.anchor (dbw.erase k)).toNat] =
res.keys.getLast hne := by
simp only [hidx, herase_keys]
have hlen : res.keys.length - 1 < res.keys.length := by omega
rw [List.getElem_append_left (i := res.keys.length - 1) (h := hlen)]
exact (List.getLast_eq_getElem hne).symm
rw [hget]
have hlast_not_ck : res.keys.getLast hne ∉
RangeRead.completeKeys (dbw.erase k) r := by
intro hmem
have hlt := (RangeRead.mem_completeKeys.mp hmem).2.2
rw [hresolve_end] at hlt
exact lt_irrefl _ hlt
have hlast_in_filt : res.keys.getLast hne ∈
(RangeRead.completeKeys (dbw.erase k) r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) := by
rw [hfilt']
exact List.getLast_mem hne
rw [List.mem_filter] at hlast_in_filt
exact hlast_not_ck hlast_in_filt.1
/-- A key in `[end_.key, begin.key)` has an insert witness in the A2a non-clamping
subcell: padding with `clampNonposD + 1` keys in `(last, end_.key)` makes the begin
selector resolve to the reported head with anchor zero; inserting the target raises the
begin anchor to one, so the head drops out of the complete window. -/
theorem exists_witness_insert_A2a {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aNonbottom r res)
(hne : res.keys ≠ [])
(hkE : r.end_.key ≤ (k : WithTop Key))
(hkB : (k : WithTop Key) < r.begin.key) :
∃ dbw : DB, RangeRead.Compatible dbw r res ∧
¬ RangeRead.Compatible (toggle dbw k) r res := by
obtain ⟨hcell0, hEB, hnbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hlastB
have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hslack : beginSlack r = res.keys.length + clampNonposD r :=
beginSlack_eq_of_A2a_nonbottom hcomp₀ hrev hmore hne hbegin hend hclamp hnbot hEB
hlastB hlastE
have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤
intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key :=
intervalCard_keyAfter_getLast_ge hcomp₀ hne hend
obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn
(B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) (E := r.end_.key)
(m := clampNonposD r + 1) hcard
have hFlt : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by
intro x hx
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne)))
(hFmem x hx).1)
have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2
obtain ⟨dbw, hkeyw, hcompw, hanchB, hanchE⟩ :=
compatible_fillers_A2a hsort hrev hmore hne hlim hbegin hend hslack hEB hlastB
hFlt hFend hFlen
have hdbw_lt_k : ∀ x ∈ dbw.keys, x < k := by
intro x hx
rw [hkeyw] at hx
rcases List.mem_append.mp hx with hx | hx
· exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx))
(WithTop.coe_lt_coe.mpr (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E hkE))))
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hFend x hx) hkE)
have hk_not_dbw : k ∉ dbw.keys := fun hx => absurd (hdbw_lt_k k hx) (lt_irrefl k)
refine ⟨dbw, hcompw, ?_⟩
rw [toggle_eq_insert_of_not_mem hk_not_dbw]
have hins_keys : (dbw.insert k hk_not_dbw).keys = dbw.keys ++ [k] := by
rw [DB.insert_keys]
exact orderedInsert_of_forall_lt hdbw_lt_k
intro hcomp'
obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp'
have hlen_dbw : 2 ≤ dbw.keys.length := by
rw [hkeyw, List.length_append, hFlen]
have : 1 ≤ res.keys.length := List.length_pos_iff.mpr hne
omega
have hhead0 : dbw.keys[0]'(by omega) = res.keys.head hne := by
have h1 : dbw.keys = res.keys ++ dbF.keys := hkeyw
rw [List.getElem_of_eq h1]
have hpos : 0 < res.keys.length := List.length_pos_iff.mpr hne
rw [List.getElem_append_left (i := 0) (h := hpos)
(h' := by rw [List.length_append]; omega)]
exact (List.head_eq_getElem_zero hne).symm
have hhead_lt : ((res.keys.head hne : Key) : WithTop Key) <
((dbw.keys[1] : Key) : WithTop Key) := by
rw [← hhead0]
have hlt := dbw.sorted.getElem_lt_getElem_of_lt (i := 0) (j := 1)
(hi := by omega) (hj := by omega) (by omega)
exact WithTop.coe_lt_coe.mpr hlt
have hcount_before : dbw.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) =
res.keys.length + clampNonposD r + 1 := by
rw [hkeyw, List.countP_append]
have h1 : res.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B
have h2 : dbF.keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbF.keys.length := by
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_lt_of_le (hFend x hx) (le_of_lt hEB)
rw [h1, h2, hFlen]
omega
have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP
(fun x : Key => decide ((x : WithTop Key) < r.begin.key)) =
res.keys.length + clampNonposD r + 2 := by
rw [hins_keys, List.countP_append, hcount_before, List.countP_singleton,
decide_eq_true_eq.mpr hkB]
norm_num
have hanchor_ins : r.begin.anchor (dbw.insert k hk_not_dbw) = 1 := by
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins]
have hslack' : (beginSlack r : ℤ) = (res.keys.length : ℤ) + clampNonposD r := by
rw [hslack]; push_cast; ring
rw [hslack']; push_cast; ring
have hresolve_ins : resolve (dbw.insert k hk_not_dbw) r.begin =
((dbw.keys[1] : Key) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by rw [hanchor_ins]; norm_num
have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat <
(dbw.insert k hk_not_dbw).keys.length := by
rw [hanchor_ins, hins_keys, List.length_append, List.length_singleton]
exact Nat.lt_succ_of_lt (by omega)
rw [resolve_eq_getElem h0 hlt]
have hidx : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat = 1 := by
rw [hanchor_ins]; rfl
simp only [hidx, hins_keys]
rw [List.getElem_append_left (i := 1) (h := by omega)]
have hhead_in : res.keys.head hne ∈
(RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) := by
rw [hfilt']
exact List.head_mem hne
rw [List.mem_filter] at hhead_in
have hge := (RangeRead.mem_completeKeys.mp hhead_in.1).2.1
rw [hresolve_ins] at hge
exact absurd hhead_lt (not_lt.mpr hge)
/-- **The `end_.key < begin.key` non-clamping subcell of the crossed truncated
nonpositive-end gap cell is minimal**, with closed form `[head, begin.key)`. -/
theorem minimal_closedForm_of_negCross_trunc_nonposGap_A2a_nonbottom
{r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncNonposGapA2aNonbottom r res) :
Minimal r res (closedFormNegCrossTruncNonposGapA2aNonbottom r res) := by
have hcellKeep : negCrossTruncNonposGapA2aNonbottom r res := hcell
obtain ⟨hcell0, hEB, hnbot⟩ := hcell
obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0
have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hEB)
have hcross' : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
have hle : leastReported r res = res.keys.head hne := by
simp [leastReported, hrev, List.head?_eq_some_head hne]
rwa [hle] at hcross
have hclosed : closedFormNegCrossTruncNonposGapA2aNonbottom r res =
⟨((res.keys.head hne : Key) : WithTop Key), r.begin.key⟩ := by
unfold closedFormNegCrossTruncNonposGapA2aNonbottom
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
rw [hclosed]
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
· have hs := sound_headLong_of_nonbottom hrev hmore hne hbegin hcross' hclamp hnbot hend
rwa [hmax] at hs
· intro k hkhead hkB
have hkhead' : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := hkhead
by_cases hkres : k ∈ res.keys
· exact necessary_of_mem_result hread hkres
· by_cases hklast : (k : 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)
· have hlastk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) :=
not_le.mp hklast
by_cases hkE : (k : WithTop Key) < r.end_.key
· obtain ⟨dbw, hcompw, hkdbw, hnot⟩ :=
exists_witness_erase_A2a hread hcellKeep hne hlastk hkE
exact necessary_of_toggle ⟨dbw, hcompw, by
rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩
· have hEk : r.end_.key ≤ (k : WithTop Key) := not_lt.mp hkE
obtain ⟨dbw, hcompw, hnot⟩ :=
exists_witness_insert_A2a hread hcellKeep hne hEk hkB
exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩
end FormalConflictRange