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

+388
View File
@@ -0,0 +1,388 @@
/-
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.NegCrossResidual
/-!
# The crossed negative-begin complete long cell at a nonpositive end offset
A forward complete result with a negative begin offset, `end.offset ≤ 0`, the end key
*below* the begin key (`end_.key < begin_.key`), reported keys below the begin key, and no
feasible clamping database. Every compatible database is nonclamping and resolves the
begin selector onto the reported head; the end selector stays at or below `end_.key`, so
agreement on `[head, begin_.key)` fixes the whole complete window. Every key from the head
up to `begin_.key` is necessary: the reported keys by convexity, keys below `end_.key` by
`necessary_last_to_end_key`, and keys in `[end_.key, begin_.key)` because inserting one
lifts the begin resolution above the reported head.
The minimal conflict range is therefore `[head, begin_.key)`.
-/
namespace FormalConflictRange
/-- **The capacity bound from a negative begin anchor, in the long cell.** In the crossed
complete cell with `end_.key < begin_.key` and `end.offset ≤ 0`, a clamping compatible
database forces `clampNonposBottom`. The interval `[begin.key, end.key)` contributes
nothing, so the stored keys split into the reported keys below `resolve end` and the
`d + 1` pad keys in `[resolve end, end.key)`, all below `begin.key`. -/
theorem clampNonposBottom_of_anchor_neg_long {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)
(hlong : r.end_.key < r.begin.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 htR : res.keys.length = cnt db (resolve db r.end_) := by
rw [hres, cnt, List.countP_eq_length_filter]
have hrep : reportedBelowBegin r res = res.keys.length := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
rw [hres] at hk
have hk' : ((k : Key) : WithTop Key) < resolve db r.end_ :=
of_decide_eq_true (List.mem_filter.mp hk).2
exact lt_trans hk' (lt_of_le_of_lt hRE hlong)
have hcB : 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
have hRB : resolve db r.end_ ≤ r.begin.key := le_trans hRE (le_of_lt hlong)
have hEB : r.end_.key ≤ r.begin.key := le_of_lt hlong
have h1 : cnt db r.end_.key =
cnt db (resolve db r.end_) + (between db (resolve db r.end_) r.end_.key).length :=
cnt_eq_add_between db hRE
have h2 : cnt db r.begin.key =
cnt db r.end_.key + (between db r.end_.key r.begin.key).length :=
cnt_eq_add_between db hEB
have hbd : (clampNonposD r + 1 : ℕ) ≤ beginSlack r - reportedBelowBegin r res := by
rw [hrep]
have h3 : res.keys.length + (clampNonposD r + 1) ≤ cnt db r.begin.key := by
rw [← htR, hlen] at h1
omega
omega
have hcap0 : intervalCard r.begin.key r.end_.key = 0 :=
intervalCard_eq_zero_of_le (le_of_lt hlong)
unfold clampNonposBottom
rw [hrep, hcap0]
have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by
have := hbd
omega
have hcast : ((res.keys.length + (clampNonposD r + 1) : ℕ) : ℕ∞) ≤ (beginSlack r : ℕ∞) :=
Nat.cast_le.mpr hnat
simpa [ENat.natCast_add, add_comm, add_left_comm, add_assoc] using hcast
/-- The nonclamping crossed negative-begin complete long cell at a nonpositive end
offset: `end_.key < begin.key`. -/
abbrev negCrossNonclampLongNonpos (r : RangeRead) (res : Result) : Prop :=
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
r.end_.offset ≤ 0 ∧
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res ∧
r.end_.key < r.begin.key
/-- The closed form for the nonclamping crossed negative-begin complete long cell at a
nonpositive end offset: `[head, begin.key)`. -/
noncomputable def closedFormNegCrossNonclampLongNonpos (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩
/-- **The nonclamping crossed negative-begin complete long cell at a nonpositive end
offset is minimal.** Failure of the capacity bound makes every compatible database
nonclamping. The complete window is `[head, resolve end)`, so agreement on
`[head, begin.key)` fixes it. Every key from the head up to `begin.key` is necessary:
reported keys by convexity, keys below `end_.key` by `necessary_last_to_end_key`, and keys
in `[end_.key, begin.key)` because the capacity failure leaves `[end_.key, begin.key)`
empty in any compatible database, while inserting one lifts the begin resolution above
the reported head. -/
theorem minimal_closedForm_of_negCross_nonclamp_long_nonpos {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossNonclampLongNonpos r res) :
Minimal r res (closedFormNegCrossNonclampLongNonpos r res) := by
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hnotbottom, hlong⟩ := 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 closedFormNegCrossNonclampLongNonpos r res =
⟨((head : Key) : WithTop Key), r.begin.key⟩ by
unfold closedFormNegCrossNonclampLongNonpos; 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_long hcomp hrev hmore hne hbegin hend hlong hneg)
have hread₀ : Readable r res := ⟨db₀, hcomp₀⟩
have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by
rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
· simp [hmore] at hm
· exact heq
have hhead_stored₀ : head ∈ db₀.keys :=
RangeRead.compatible_keys_subset hcomp₀ (by rw [hhead]; exact List.head_mem hne)
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 hhead_le_res₀ : ((head : Key) : WithTop Key) ≤ resolve db₀ r.end_ := le_of_lt hhead_lt_res₀
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 hRne₀ : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by
intro hR
rw [hR] at hhead_lt_res₀
exact absurd hhead_lt_res₀ (not_lt.mpr (coe_nil_le_key_withTop (head : Key)))
have hL₀ : resolve db₀ r.begin = ((head : Key) : WithTop Key) :=
resolve_begin_eq_head_of_anchor_nonneg hcomp₀ hrev hmore hne (hnonclamp db₀ hcomp₀)
have hres_filters : res.keys = between db₀ (head : WithTop Key) (resolve db₀ r.end_) := by
rw [hres_eq₀]
unfold RangeRead.completeKeys RangeRead.inRange between
rw [ite_eq_right (by simp [hrev]), hL₀]
have hlen_end : (between db₀ (head : WithTop Key) r.end_.key).length =
res.keys.length + (clampNonposD r + 1) := by
have hL1 := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key)) (w₂ := r.end_.key)
(le_of_lt hhead_lt_E₀)
have hL2 := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key))
(w₂ := resolve db₀ r.end_) hhead_le_res₀
have hL3 := cnt_eq_add_between db₀ (w₁ := resolve db₀ r.end_) (w₂ := r.end_.key) hres_le₀
have hL4 : (between db₀ (head : WithTop Key) (resolve db₀ r.end_)).length =
res.keys.length := by rw [← hres_filters]
have hp : (between db₀ (resolve db₀ r.end_) r.end_.key).length = clampNonposD r + 1 :=
between_resolve_end_length hend hRne₀
omega
have hlen_begin : (between db₀ (head : WithTop Key) r.begin.key).length = beginSlack r + 1 :=
filter_begin_length_of_anchor_nonneg hcomp₀ hrev hmore hne hbegin hcross
(hnonclamp db₀ hcomp₀)
have hall_reported_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by
intro k hk
have hklt := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hk)).2.2
exact lt_of_lt_of_le hklt hres_le₀
have hreported_eq : reportedBelowBegin r res = res.keys.length := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
exact lt_trans (hall_reported_lt_E k hk) hlong
have hnat : beginSlack r < res.keys.length + (clampNonposD r + 1) := by
have hcap0 : intervalCard r.begin.key r.end_.key = 0 :=
intervalCard_eq_zero_of_le (le_of_lt hlong)
have hnotle : ¬ ((reportedBelowBegin r res : ℕ∞) +
(((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ (beginSlack r : ℕ∞)) := by
intro hle
apply hnotbottom
unfold clampNonposBottom
rw [hcap0, tsub_zero]
exact hle
have hltE : (beginSlack r : ℕ∞) < (reportedBelowBegin r res : ℕ∞) +
(((clampNonposD r + 1 : ℕ)) : ℕ∞) := not_le.mp hnotle
rw [hreported_eq, ← ENat.natCast_add] at hltE
exact ENat.natCast_lt_natCast.mp hltE
have hP0 : (between db₀ r.end_.key r.begin.key).length = 0 := by
have hb := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key)) (w₂ := r.begin.key)
(le_trans (le_of_lt hhead_lt_E₀) (le_of_lt hlong))
have he := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key)) (w₂ := r.end_.key)
(le_of_lt hhead_lt_E₀)
have hbe := cnt_eq_add_between db₀ (w₁ := r.end_.key) (w₂ := r.begin.key) (le_of_lt hlong)
rw [hlen_begin] at hb
rw [hlen_end] at he
omega
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
· have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.begin.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 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 (hnonclamp db₁ hcomp₁)
have hfilt : db₁.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)) := by
simpa [AgreeOn, ConflictRange.keys] using hagree
have hfiltE := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
(a := head) (B := r.end_.key) (E := r.begin.key) (le_of_lt hlong) hfilt
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
(hnonclamp db₁ hcomp₁)
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 [← hfilt]; 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 [← hfilt]; 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) hfiltE 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 hfiltE
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), hL₁, 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_head : head ≤ k := WithTop.coe_le_coe.mp hlo
have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := hhi
by_cases hklast : k ≤ res.keys.getLast hne
· exact necessary_of_between_result hread₀ hhead_mem (List.getLast_mem hne) hk_head hklast
· have hk_gt_last : res.keys.getLast hne < k := lt_of_not_ge hklast
have hhead_le_last : head ≤ res.keys.getLast hne := by
have hsrt : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
rw [hhead, List.head_eq_getElem hne, List.getLast_eq_getElem hne]
exact hsrt.getElem_le_getElem_iff.mpr (by
have := List.length_pos_of_ne_nil hne
omega)
have hk_gt_head : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last
by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key
· exact fun cr hs => necessary_last_to_end_key hread₀ hrev hmore hne (by omega)
cr hs (le_of_lt hk_gt_last) hkE
· have hkE_le : r.end_.key ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkE
intro cr hs
by_contra hkcr
have hknot : k ∉ db₀.keys := by
intro hkdb
have hkmem : k ∈ between db₀ r.end_.key r.begin.key := by
unfold between
exact List.mem_filter.mpr ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkE_le, hk_lt_B⟩⟩
have hpos : 0 < (between db₀ r.end_.key r.begin.key).length :=
List.length_pos_of_mem hkmem
omega
have hgt : ((head : Key) : WithTop Key) < resolve (db₀.insert k hknot) r.begin :=
resolve_begin_insert_gt hbegin hhead_stored₀ hcross hknot hk_gt_head hk_lt_B hlen_begin
have hhead_notin : head ∉ RangeRead.completeKeys (db₀.insert k hknot) r := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev])]
intro hmem
simp only [List.mem_filter, decide_eq_true_eq] at hmem
exact absurd hmem.2.1 (not_le.mpr hgt)
have hcomp₁ : RangeRead.Compatible (db₀.insert k hknot) r res :=
(hs db₀ _ (ConflictRange.keys_insert_of_not_mem cr db₀ hknot hkcr).symm).mp hcomp₀
have hres_eq₁ : res.keys = RangeRead.completeKeys (db₀.insert k hknot) r := by
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
· simp [hmore] at hm
· exact heq
exact hhead_notin (by rw [← hres_eq₁]; exact hhead_mem)
/-- The reversed nonclamping crossed negative-begin complete long cell at a nonpositive
end offset. -/
abbrev negCrossNonclampLongNonposRev (r : RangeRead) (res : Result) : Prop :=
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
r.end_.offset ≤ 0 ∧
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res ∧
r.end_.key < r.begin.key
/-- The reversed closed form for the nonclamping crossed negative-begin complete long
cell at a nonpositive end offset: `[last reported, begin.key)`. -/
noncomputable def closedFormNegCrossNonclampLongNonposRev (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.begin.key⟩
/-- **The reversed nonclamping crossed negative-begin complete long cell at a nonpositive
end offset is minimal**, by flipping the traversal. -/
theorem minimal_closedForm_of_negCross_nonclamp_long_nonpos_rev {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossNonclampLongNonposRev r res) :
Minimal r res (closedFormNegCrossNonclampLongNonposRev r res) := by
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hnotbottom, hlong⟩ := hcell
have hne' : res.keys.reverse ≠ [] := by simpa using hne
have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by
rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse]
have hcount : reportedBelowBegin { r with reverse := false }
{ keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by
unfold reportedBelowBegin
exact List.Perm.countP_eq _ (List.reverse_perm res.keys)
have hbottom' : clampNonposBottom { r with reverse := false }
{ keys := res.keys.reverse, more := false } ↔ clampNonposBottom r res := by
unfold clampNonposBottom
rw [hcount]
rfl
have hcell' : negCrossNonclampLongNonpos { r with reverse := false }
{ keys := res.keys.reverse, more := false } := by
refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, ?_, hlong⟩
· rw [hheadD]; exact hcrossD
· rw [hcount]; exact hmq
· 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_long_nonpos hread' hcell'
have hcr : closedFormNegCrossNonclampLongNonpos { r with reverse := false }
{ keys := res.keys.reverse, more := false } =
closedFormNegCrossNonclampLongNonposRev r res := by
unfold closedFormNegCrossNonclampLongNonpos closedFormNegCrossNonclampLongNonposRev
rw [hheadD]
rw [hcr] at hmin
exact (minimal_flip (b := true) hrev hmore).mp hmin
end FormalConflictRange