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

433 lines
23 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.NegCrossClampNonpos
/-!
# The crossed negative-begin complete nonpositive-end nonclamping cell
A forward complete result with a negative begin offset and `end.offset ≤ 0`, where the
begin key is at or below the end key but the reported keys below the begin key exhaust the
begin slack, so no compatible database clamps. The begin selector then resolves onto the
reported head and the minimal conflict range is `[head, end_.key)`.
The key new ingredient is the converse of the clamping capacity bound: any clamping
compatible database forces `clampNonposBottom`. Its contrapositive says that below that
bound every compatible database is nonclamping, which is what makes `[head, end_.key)`
sound.
-/
namespace FormalConflictRange
/-- In the crossed negative-begin complete cell with `begin.key ≤ end.key`, a clamping
compatible database forces the capacity bound `clampNonposBottom`. -/
theorem clampNonposBottom_of_anchor_neg {r : RangeRead} {res : Result} {db : DB}
(hcomp : RangeRead.Compatible db 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)
(hanchor : r.begin.anchor db < 0) :
clampNonposBottom r res := by
have hL : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor
have hres : res.keys =
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
· simp [hmore] at hm
· rw [heq, RangeRead.completeKeys, if_neg (by simp [hrev])]
unfold RangeRead.inRange
rw [hL]
apply List.filter_congr
intro k _
rw [decide_eq_decide]
constructor
· intro h; exact h.2
· intro h; exact ⟨coe_nil_le_key_withTop _, h⟩
have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by
intro hR
apply hne
rw [hres, hR]
rw [List.filter_eq_nil_iff]
intro k _
simp only [decide_eq_true_eq]
exact not_lt.mpr (coe_nil_le_key_withTop _)
have hRE : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
have hlen : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 :=
between_resolve_end_length hend hRne
have hcntE : cnt db r.end_.key =
cnt db (resolve db r.end_) + (clampNonposD r + 1) := by
rw [cnt_eq_add_between db hRE, hlen]
have hcntEB : cnt db r.end_.key =
cnt db r.begin.key + (between db r.begin.key r.end_.key).length :=
cnt_eq_add_between db hB_le_E
have hcntBq : cnt db r.begin.key ≤ beginSlack r := by
have h := hanchor
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] at h
unfold cnt
omega
-- Split the reported count below `begin.key` by whether `resolve end ≤ begin.key`.
have hm_eq : reportedBelowBegin r res =
(db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_ ∧
((k : Key) : WithTop Key) < r.begin.key))).length := by
unfold reportedBelowBegin
rw [hres, List.countP_eq_length_filter, List.filter_filter]
congr 1
apply List.filter_congr
intro k _
simp [Bool.and_comm]
have hmq : reportedBelowBegin r res ≤ beginSlack r := by
by_cases hRB : resolve db r.end_ ≤ r.begin.key
· have hcntB : cnt db r.begin.key =
cnt db (resolve db r.end_) + (between db (resolve db r.end_) r.begin.key).length :=
cnt_eq_add_between db hRB
have hmZ : reportedBelowBegin r res = cnt db (resolve db r.end_) := by
rw [hm_eq, cnt, List.countP_eq_length_filter]
congr 1
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact ⟨fun h => h.1, fun h => ⟨h, lt_of_lt_of_le h hRB⟩⟩
rw [hmZ]
omega
· have hBR : r.begin.key < resolve db r.end_ := lt_of_not_ge hRB
have hmY : reportedBelowBegin r res = cnt db r.begin.key := by
rw [hm_eq, cnt, List.countP_eq_length_filter]
congr 1
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact ⟨fun h => h.2, fun h => ⟨lt_of_lt_of_le h (le_of_lt hBR), h⟩⟩
rw [hmY]
exact hcntBq
have hdnat : clampNonposD r + 1 ≤
(between db r.begin.key r.end_.key).length +
(beginSlack r - reportedBelowBegin r res) := by
by_cases hRB : resolve db r.end_ ≤ r.begin.key
· have hcntB : cnt db r.begin.key =
cnt db (resolve db r.end_) + (between db (resolve db r.end_) r.begin.key).length :=
cnt_eq_add_between db hRB
have hmZ : reportedBelowBegin r res = cnt db (resolve db r.end_) := by
rw [hm_eq, cnt, List.countP_eq_length_filter]
congr 1
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact ⟨fun h => h.1, fun h => ⟨h, lt_of_lt_of_le h hRB⟩⟩
rw [hmZ]
omega
· have hBR : r.begin.key < resolve db r.end_ := lt_of_not_ge hRB
have hmY : reportedBelowBegin r res = cnt db r.begin.key := by
rw [hm_eq, cnt, List.countP_eq_length_filter]
congr 1
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact ⟨fun h => h.2, fun h => ⟨lt_of_lt_of_le h (le_of_lt hBR), h⟩⟩
have hYZ : cnt db r.begin.key ≤ cnt db (resolve db r.end_) :=
cnt_le_cnt_of_le db (le_of_lt hBR)
rw [hmY]
omega
-- Convert the natural inequality to the `ℕ∞` capacity bound.
have hcast : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
((between db r.begin.key r.end_.key).length : ℕ∞) +
((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := by
exact_mod_cast hdnat
have hg' : ((between db r.begin.key r.end_.key).length : ℕ∞) ≤
intervalCard r.begin.key r.end_.key :=
between_length_le_intervalCard hB_le_E
have hmain : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤
intervalCard r.begin.key r.end_.key +
((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) :=
le_trans hcast (add_le_add hg' le_rfl)
have htsub : (((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key ≤
((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := by
rw [tsub_le_iff_left]
exact hmain
unfold clampNonposBottom
calc (reportedBelowBegin r res : ℕ∞) +
((((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key)
≤ (reportedBelowBegin r res : ℕ∞) +
((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) :=
add_le_add le_rfl htsub
_ = (beginSlack r : ℕ∞) := by
rw [← ENat.natCast_add]
congr 1
omega
/-- **Nonclamping resolution onto the head.** A compatible forward complete database whose
begin anchor is nonnegative resolves the begin selector exactly onto the reported head,
without the `beginSlack < reportedBelowBegin` hypothesis of the offset-free form. -/
theorem resolve_begin_eq_head_of_anchor_nonneg {db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = false) (hne : res.keys ≠ [])
(hanchor : 0 ≤ r.begin.anchor db) :
resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
set head : Key := res.keys.head hne with hhead
have hres_eq : res.keys = RangeRead.completeKeys db r := by
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
· simp [hmore] at hm
· exact heq
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
have hhead_ck : head ∈ RangeRead.completeKeys db r := by
rw [← hres_eq]; exact hhead_mem
have hle_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp hhead_ck).2.1
have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db r.end_ :=
(RangeRead.mem_completeKeys.mp hhead_ck).2.2
have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
intro htop
rw [htop] at hle_head
exact absurd hle_head (not_le.mpr (WithTop.coe_lt_top head))
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
by_contra h
exact hnot_top (resolve_eq_top hanchor (le_of_not_gt h))
have hresolve_get : resolve db r.begin =
((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) :=
resolve_eq_getElem hanchor hltlen
set k : Key := db.keys[(r.begin.anchor db).toNat] with hk
have hk_stored : k ∈ db.keys := List.getElem_mem hltlen
have hk_le_head : k ≤ head := WithTop.coe_le_coe.mp (by rw [← hresolve_get]; exact hle_head)
have hk_le : resolve db r.begin ≤ ((k : Key) : WithTop Key) := by rw [hresolve_get]
have hk_lt : ((k : Key) : WithTop Key) < resolve db r.end_ := by
calc ((k : Key) : WithTop Key) = resolve db r.begin := hresolve_get.symm
_ ≤ ((head : Key) : WithTop Key) := hle_head
_ < resolve db r.end_ := hhead_lt_res
have hk_ck : k ∈ RangeRead.completeKeys db r :=
RangeRead.mem_completeKeys.mpr ⟨hk_stored, hk_le, hk_lt⟩
have hk_mem_res : k ∈ res.keys := by rw [hres_eq]; exact hk_ck
have hk_ge_head : head ≤ k :=
(RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk_mem_res
have hk_eq : k = head := le_antisymm hk_le_head hk_ge_head
rw [← hk_eq]
exact hresolve_get
/-- **The begin count identity from a nonnegative anchor.** With a nonnegative begin
anchor exactly `beginSlack r + 1` stored keys lie in `[head, begin.key)`, replacing the
`beginSlack < reportedBelowBegin` hypothesis of `negCross_filter_begin_length`. -/
theorem filter_begin_length_of_anchor_nonneg {db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hanchor : 0 ≤ r.begin.anchor db) :
(db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
beginSlack r + 1 := by
set head : Key := res.keys.head hne with hhead
set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))
with hcB
have hcB_ge : beginSlack r + 1 ≤ cB := by
have h : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] at hanchor
omega
exact_mod_cast h
have hL := resolve_begin_eq_head_of_anchor_nonneg hcomp hrev hmore hne hanchor
have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
intro htop; rw [hL] at htop
exact absurd htop (WithTop.coe_ne_top (a := head))
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
by_contra h
exact hnot_top (resolve_eq_top hanchor (le_of_not_gt h))
have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor hltlen
have hidx : db.keys[(r.begin.anchor db).toNat] = head := by
have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) =
((head : Key) : WithTop Key) := by rw [← hget, hL]
exact WithTop.coe_injective hcoe
have hrank : db.keys.countP (fun k => decide (k < head)) = (r.begin.anchor db).toNat :=
countP_lt_eq_of_getElem db.sorted hltlen hidx
have hanchor_cast : r.begin.anchor db = (cB : ℤ) - (beginSlack r : ℤ) - 1 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB]
have htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by
have hz : r.begin.anchor db = ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by
rw [hanchor_cast]; push_cast; ring
have hz2 : ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) =
((cB - (beginSlack r + 1) : ℕ) : ℤ) := by
rw [← Nat.cast_sub hcB_ge]
rw [hz, hz2, Int.toNat_natCast]
have hcH_add : (db.keys.countP (fun k => decide (k < head))) + (beginSlack r + 1) = cB := by
rw [hrank, htoNat]
exact Nat.sub_add_cancel hcB_ge
have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup
(a := head) (B := r.begin.key) (le_of_lt hcross)
have hsplit' : cB = (db.keys.countP (fun k => decide (k < head))) +
(db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := by
rw [hcB]; exact hsplit
omega
/-- A forward complete crossed negative-begin result with `end.offset ≤ 0`, `begin.key ≤
end.key`, reported keys below the begin key exhausting the begin slack, and no feasible
clamping database. -/
abbrev negCrossNonclampNonpos (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 ∧
reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res
/-- The closed form for the nonclamping negative-begin complete nonpositive-end cell:
`[head, end_.key)`. -/
noncomputable def closedFormNegCrossNonclampNonpos (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩
/-- **The nonclamping negative-begin complete nonpositive-end cell is minimal.** The
capacity bound fails, so every compatible database is nonclamping and resolves the begin
selector onto the reported head; agreement on `[head, end_.key)` fixes the whole complete
window, and every key up to `end_.key` is necessary. -/
theorem minimal_closedForm_of_negCross_nonclamp_nonpos {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossNonclampNonpos r res) :
Minimal r res (closedFormNegCrossNonclampNonpos r res) := by
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcrossD, hmq, hnotbottom⟩ := hcell
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
rw [← hheadD]; exact hcrossD
obtain ⟨db₀, hcomp₀⟩ := hread
set head : Key := res.keys.head hne with hhead
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead
rw [show closedFormNegCrossNonclampNonpos r res =
⟨((head : Key) : WithTop Key), r.end_.key⟩ by
unfold closedFormNegCrossNonclampNonpos; rw [hheadD']]
have hnonclamp : ∀ db, RangeRead.Compatible db r res → 0 ≤ r.begin.anchor db := by
intro db hcomp
by_contra hneg
push_neg at hneg
exact hnotbottom
(clampNonposBottom_of_anchor_neg hcomp hrev hmore hne hbegin hend hB_le_E hneg)
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
· have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.end_.key⟩ 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 hanchor₁ := hnonclamp db₁ hcomp₁
have hhead_stored : head ∈ db₁.keys :=
RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne)
have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) :=
resolve_begin_eq_head_of_anchor_nonneg hcomp₁ hrev hmore hne hanchor₁
have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
beginSlack r + 1 :=
filter_begin_length_of_anchor_nonneg hcomp₁ hrev hmore hne hbegin hcross hanchor₁
have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) =
db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by
simpa [AgreeOn, ConflictRange.keys] using hagree
have hfiltB := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
(a := head) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt
have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) :=
List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩
have hhead_stored₂ : head ∈ db₂.keys :=
List.mem_of_mem_filter (by rw [← hfiltB]; exact hhead_in₁)
have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
beginSlack r + 1 := by rw [← hfiltB]; exact hlen₁
have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) :=
resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂
have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db₁ r.end_ :=
(RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.2
have hres_le : resolve db₁ r.end_ ≤ r.end_.key :=
resolve_le_key_of_offset_nonpos hend
have hhead_lt_E : ((head : Key) : WithTop Key) < r.end_.key :=
lt_of_lt_of_le hhead_lt_res hres_le
have hR : resolve db₂ r.end_ = resolve db₁ r.end_ := by
rcases resolve_eq_nil_or_eq_top_or_mem (db := db₁) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩
· rw [hn] at hhead_lt_res
exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _))
· exact absurd ht (resolve_ne_top_of_offset_nonpos hend)
· have hqne : q ≠ [] := by
intro hqnil
rw [hqnil] at hqeq
rw [hqeq] at hhead_lt_res
exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _))
have hqspec : (db₁.keys.filter (fun k : Key =>
decide (((k : Key) : WithTop Key) < r.end_.key))).reverse[
(-r.end_.offset).toNat]? = some q :=
resolve_filter_reverse_eq_some_of_ne_nil hend hqne hqeq
have hqa : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := by
rw [← hqeq]; exact le_of_lt hhead_lt_res
have hR' : resolve db₂ r.end_ = ((q : Key) : WithTop Key) :=
resolve_eq_of_agree_between (db₁ := db₁) (db₂ := db₂) (s := r.end_) hend rfl
(le_of_lt hhead_lt_E) hfilt hqspec hqa
rw [hR', hqeq]
have hP : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) =
db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) :=
filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
(a := head) (B := resolve db₁ r.end_) (E := r.end_.key) hres_le hfilt
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), hL₁, 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⟩
· intro k hlo hhi
have hk_le : head ≤ k := WithTop.coe_le_coe.mp hlo
by_cases hklast : k ≤ res.keys.getLast hne
· exact necessary_of_between_result ⟨db₀, hcomp₀⟩ hhead_mem (List.getLast_mem hne)
hk_le hklast
· exact fun cr hs => necessary_last_to_end_key ⟨db₀, hcomp₀⟩ hrev hmore hne (by omega)
cr hs (le_of_lt (lt_of_not_ge hklast)) hhi
/-- The reversed traversal of the nonclamping negative-begin complete nonpositive-end
cell. -/
abbrev negCrossNonclampNonposRev (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 ∧
reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res
/-- The reversed closed form for the nonclamping negative-begin complete nonpositive-end
cell: `[lastReported, end_.key)`. -/
noncomputable def closedFormNegCrossNonclampNonposRev (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.end_.key⟩
/-- **The reversed nonclamping negative-begin complete nonpositive-end cell is minimal**,
by flipping the traversal and applying the forward rule. -/
theorem minimal_closedForm_of_negCross_nonclamp_nonpos_rev {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossNonclampNonposRev r res) :
Minimal r res (closedFormNegCrossNonclampNonposRev r res) := by
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcrossD, hmq, hnotbottom⟩ := hcell
have hne' : res.keys.reverse ≠ [] := by simpa using hne
have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by
rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse]
have hcount : reportedBelowBegin { r with reverse := false }
{ keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by
unfold reportedBelowBegin
exact List.Perm.countP_eq _ (List.reverse_perm res.keys)
have hbottom' : clampNonposBottom { r with reverse := false }
{ keys := res.keys.reverse, more := false } ↔ clampNonposBottom r res := by
unfold clampNonposBottom
rw [hcount]
rfl
have hcell' : negCrossNonclampNonpos { r with reverse := false }
{ keys := res.keys.reverse, more := false } := by
refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, ?_, ?_, ?_⟩
· rw [hheadD]; exact hcrossD
· rw [hcount]; exact hmq
· intro h
exact hnotbottom (hbottom'.mp h)
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_nonclamp_nonpos hread' hcell'
have hcr : closedFormNegCrossNonclampNonpos { r with reverse := false }
{ keys := res.keys.reverse, more := false } =
closedFormNegCrossNonclampNonposRev r res := by
unfold closedFormNegCrossNonclampNonpos closedFormNegCrossNonclampNonposRev
rw [hheadD]
rw [hcr] at hmin
exact (minimal_flip (b := true) hrev hmore).mp hmin
end FormalConflictRange