Files
conflict-range/FormalConflictRange/NegCrossBottom.lean
T
andrew af318a7d32 Fix all Lean build warnings
- Update deprecated lemmas (ite_eq_left/right, ENat casts, List.Sublist, ...)
- Replace push_neg with push Not and resolve unused-variable warnings
- Convert flexible simp/simp_all calls to explicit simp only lists
- Remove unscoped maxHeartbeats options, open scoped Classical, and unused tactics
- Add missing module docstrings and clean up style linter hits
- Wrap over-long lines and flatten the deeply nested
  minimal_conflictRangeClosedForm_total proof

`lake build` now completes with zero warnings.
2026-11-17 10:04:35 -05:00

158 lines
8.9 KiB
Lean4

import FormalConflictRange.NegCrossComplete
/-!
# The negative-begin complete bottom cell.
-/
open FormalConflictRange
namespace FormalConflictRange
/-- The negative-begin complete bottom cell: forward, complete, nonempty, nonpositive
end, `begin.key ≤ end.key`, and reported head the empty key. -/
abbrev negCrossHeadBottom (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)
/-- The closed form for the negative-begin complete bottom cell: `[⊥, end_.key)`. -/
noncomputable def closedFormNegCrossHeadBottom (r : RangeRead) (_res : Result) :
ConflictRange :=
⟨(([] : Key) : WithTop Key), r.end_.key⟩
/-- Filtering keys below `E` is the same as filtering the half-open interval
`[⊥, E)`. -/
theorem filter_between_bot_eq {l : List Key} {E : WithTop Key} :
l.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
((k : Key) : WithTop Key) < E)) =
l.filter (fun k : Key => decide (((k : Key) : WithTop Key) < E)) := by
apply List.filter_congr
intro k _
rw [decide_eq_decide]
exact ⟨fun h => h.2, fun h => ⟨coe_nil_le_key_withTop _, h⟩⟩
/-- **The negative-begin complete bottom cell is minimal.** The reported head is the
empty key, so the begin selector resolves to `⊥` in every compatible database; the
complete window is a prefix of the keys below `end_.key`, hence agreement on
`[⊥, end_.key)` determines compatibility. Every key in `[⊥, end_.key)` is necessary:
the reported keys and the gaps between them by convexity, and the keys above the
reported last by `necessary_last_to_end_key`. -/
theorem minimal_closedForm_of_negCross_headBottom {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossHeadBottom r res) :
Minimal r res (closedFormNegCrossHeadBottom r res) := by
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hheadD⟩ := hcell
obtain ⟨db₀, hcomp₀⟩ := hread
have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne
have hhead_empty : (res.keys.head hne : Key) = ([] : Key) := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne] at hheadD
simpa using hheadD
rw [show closedFormNegCrossHeadBottom r res =
⟨(([] : Key) : WithTop Key), r.end_.key⟩ by rfl]
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
· have step : ∀ db₁ db₂, AgreeOn ⟨(([] : 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 hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db₁ r := by
rw [← hres_eq]; exact hhead_mem
have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) := by
have hle : resolve db₁ r.begin ≤ (([] : Key) : WithTop Key) := by
have h := (RangeRead.mem_completeKeys.mp hhead_ck).2.1
rwa [hhead_empty] at h
exact le_antisymm hle (coe_nil_le_key_withTop _)
have hfilt : db₁.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) =
db₂.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by
simpa [AgreeOn, ConflictRange.keys] using hagree
have hfiltE : db₁.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
r.end_.key)) =
db₂.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) := by
rw [← filter_between_bot_eq, ← filter_between_bot_eq]
exact hfilt
have hR : resolve db₂ r.end_ = resolve db₁ r.end_ := by
rw [resolve_eq_filter_lt_reverse (db := db₁) hend,
resolve_eq_filter_lt_reverse (db := db₂) hend, hfiltE]
have hfiltB : db₁.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
db₂.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) :=
filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
(a := ([] : Key)) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt
have hfiltB0 : db₁.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
r.begin.key)) =
db₂.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
r.begin.key)) := by
rw [← filter_between_bot_eq (E := r.begin.key),
← filter_between_bot_eq (E := r.begin.key)]
exact hfiltB
have hL₂ : resolve db₂ r.begin = resolve db₁ r.begin := by
rw [resolve_eq_filter_lt_reverse (db := db₁) (s := r.begin) (le_of_lt hbegin),
resolve_eq_filter_lt_reverse (db := db₂) (s := r.begin) (le_of_lt hbegin),
hfiltB0]
have hL₂' : resolve db₂ r.begin = (([] : Key) : WithTop Key) := by rw [hL₂, hL₁]
have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
have hP : db₁.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) =
db₂.keys.filter (fun k : Key => decide ((([] : 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 := ([] : Key)) (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_bot : ([] : Key) ≤ 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)
(by rw [hhead_empty]; exact hk_bot) 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 negative-begin complete bottom cell: the reported last is the empty
key. -/
abbrev negCrossHeadBottomRev (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)
/-- The closed form for the reversed negative-begin complete bottom cell. -/
noncomputable def closedFormNegCrossHeadBottomRev (r : RangeRead) (_res : Result) :
ConflictRange :=
⟨(([] : Key) : WithTop Key), r.end_.key⟩
/-- **The reversed negative-begin complete bottom cell is minimal**, by flipping the
traversal. -/
theorem minimal_closedForm_of_negCross_headBottom_rev {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossHeadBottomRev r res) :
Minimal r res (closedFormNegCrossHeadBottomRev r res) := by
obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hlastD⟩ := 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 hcell' : negCrossHeadBottom {r with reverse := false}
{ keys := res.keys.reverse, more := false } := by
refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, ?_⟩
rw [hheadD]; exact hlastD
have hread' : Readable {r with reverse := false}
{ keys := res.keys.reverse, more := false } := by
obtain ⟨db, hcomp⟩ := hread
exact ⟨db, (compatible_flip (db := db) (r := r) (res := res) (b := true) hrev hmore).mpr hcomp⟩
have hmin' : Minimal {r with reverse := false}
{ keys := res.keys.reverse, more := false }
(closedFormNegCrossHeadBottomRev r res) := by
simpa [closedFormNegCrossHeadBottomRev, closedFormNegCrossHeadBottom] using
minimal_closedForm_of_negCross_headBottom hread' hcell'
exact (minimal_flip (r := r) (res := res) (cr := closedFormNegCrossHeadBottomRev r res)
(b := true) hrev hmore).mp hmin'
end FormalConflictRange