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,252 @@
/-
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.NegCrossTruncRevNonpos
/-!
# Reversed crossed negative-begin truncated cells with a small crossed gap
This module closes the reversed crossed negative-begin truncated cell at end offset one
whose capacity is small:
* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`;
* the crossed selectors `end_.key < begin.key`, with every reported key at or above the
reported last and below `end_.key`;
* `end_.offset = 1`;
* `intervalCard end_.key begin.key + |res.keys| ≤ beginSlack + 1`.
The minimal range is `[last reported, end_.key)`, the reversed truncated range.
The reduction to the begin selector at the reported last is the same as in the
under-end cell. The only new ingredient is soundness: agreement on `[last, end_.key)`
fixes the stored keys there (exactly the `|res.keys|` reported keys), while the stored
keys in `[end_.key, begin.key)` number at most the interval cardinality. The capacity
bound therefore keeps the total count in `[last, begin.key)` at most `beginSlack + 1`,
so the begin resolution still lies at or below the reported last.
Necessity is identical to the under-end cell: reported keys are necessary by
`necessary_of_mem_result`, keys between the reported last and head by
`necessary_of_between_result`, and keys from the head to `end_.key` by
`necessary_head_to_end_key`.
-/
namespace FormalConflictRange
set_option maxHeartbeats 1200000
/-- **The length of `between` splits at an intermediate bound.** For `L ≤ E ≤ B` the
stored keys in `[L, B)` are those in `[L, E)` followed by those in `[E, B)`. -/
theorem between_length_add {db : DB} {L E B : WithTop Key} (hLE : L ≤ E) (hEB : E ≤ B) :
(between db L B).length = (between db L E).length + (between db E B).length := by
have h1 := cnt_sub_eq_between_length (db := db) hLE
have h2 := cnt_sub_eq_between_length (db := db) hEB
have h3 := cnt_sub_eq_between_length (db := db) (le_trans hLE hEB)
have hmono : cnt db L ≤ cnt db E := cnt_le_cnt_of_le db hLE
have hmono' : cnt db E ≤ cnt db B := cnt_le_cnt_of_le db hEB
omega
/-- **The reversed crossed negative-begin truncated small-gap cell.** -/
theorem minimal_truncatedRangeRev_of_negCross_crossedSmall {r : RangeRead} {res : Result}
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
(_hbeg : r.begin.offset < 0) (hne : res.keys ≠ [])
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
(hcrossE : r.end_.key < r.begin.key)
(hend : r.end_.offset = 1)
(hcap : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) ≤
(beginSlack r : ℕ∞) + 1) :
Minimal r res (truncatedRangeRev r (res.keys.getLast hne)) := by
set L : Key := res.keys.getLast hne with hLdef
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross
obtain ⟨db₀, hcomp₀⟩ := hread
have hreadAll : Readable r res := ⟨db₀, hcomp₀⟩
let r' : RangeRead :=
{ r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } }
have hr'_rev : r'.reverse = true := by simp [r', hrev]
have hr'_end : r'.end_ = r.end_ := rfl
have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) :=
rfl
have hr'_off : r'.begin.offset = 1 := rfl
-- The reduced read sees exactly the reported keys.
have hreduce : ∀ (db : DB), RangeRead.Compatible db r res →
RangeRead.Compatible db r' res := by
intro db hcomp
have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) =
res.keys :=
(compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by
rw [hfil]; exact hLmem
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1
have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem
have hres' : resolve db r'.begin = ((L : Key) : WithTop Key) := by
rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb
have hLck_filter : (RangeRead.completeKeys db r').filter (fun k => decide (L ≤ k)) =
RangeRead.completeKeys db r' := by
rw [List.filter_eq_self]
intro k hk
rw [decide_eq_true_eq]
have hk' := (RangeRead.mem_completeKeys.mp hk).2.1
exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk')
have hck' : RangeRead.completeKeys db r' = res.keys := by
rw [← hLck_filter]
exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) (r₂ := r')
hrev hr'_rev hr'_end hrb (le_of_eq hres')).symm.trans hfil
refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩
rw [hck']
have hread' : Readable r' res := ⟨db₀, hreduce db₀ hcomp₀⟩
have hgetLastD : res.keys.getLastD ([] : Key) = L := by
rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hmin' : Minimal r' res (truncatedRangeRev r L) := by
have hm := minimal_truncatedRangeRev_of_offset_one (r := r') hread' hmore
hr'_rev hr'_off hend
rwa [hgetLastD] at hm
have hS' : Sound r' res (truncatedRangeRev r L) := hmin'.sound
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
· -- **Soundness.** Agreement on `[last, end_.key)` plus the capacity bound keeps the
-- begin resolution at or below the reported last.
have step : ∀ db db', AgreeOn (truncatedRangeRev r L) db db' →
RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by
intro db db' hag' hcomp
have hcomp' : RangeRead.Compatible db r' res := hreduce db hcomp
have hcomp'' : RangeRead.Compatible db' r' res := (hS' db db' hag').mp hcomp'
have hbetweenEq' : between db ((L : Key) : WithTop Key) r.end_.key =
between db' ((L : Key) : WithTop Key) r.end_.key := by
have h := hag'
unfold AgreeOn ConflictRange.keys at h
unfold between
exact h
-- The reported last is a stored key lying below `end_.key`.
have hpre : res.keys <+: RangeRead.completeKeys db r :=
RangeRead.compatible_keys_prefix hcomp
have hLc : L ∈ RangeRead.completeKeys db r := hpre.subset hLmem
have hLinfo := RangeRead.mem_completeKeys.mp hLc
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := hLinfo.2.1
have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ := hLinfo.2.2
have hLE : ((L : Key) : WithTop Key) < r.end_.key :=
lt_end_key_of_lt_resolve_end_one hend hLinfo.1 hLres
have hEre : r.end_.key ≤ resolve db r.end_ := key_le_resolve_of_one_le (by omega)
-- The stored keys in `[last, end_.key)` are exactly the reported ones.
have hbetweenFil : between db ((L : Key) : WithTop Key) r.end_.key =
(RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).filter
(fun k => decide (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key))) := by
unfold between RangeRead.inRange
rw [List.filter_filter]
apply List.filter_congr
intro k hk
rw [← Bool.decide_and, decide_eq_decide]
constructor
· rintro ⟨hLk, hkE⟩
exact ⟨hLk, le_trans hrb hLk, lt_of_lt_of_le hkE hEre⟩
· rintro ⟨hLk, _, hkre⟩
exact ⟨hLk, lt_end_key_of_lt_resolve_end_one hend hk hkre⟩
have hbetweenLen : (between db ((L : Key) : WithTop Key) r.end_.key).length =
res.keys.length := by
have hfilW : (RangeRead.completeKeys db r).filter
(fun k => decide (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key))) =
res.keys := by
have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
rw [← hLdef] at h
simpa only [WithTop.coe_le_coe] using h
have hlen := congrArg List.length hfilW
have hck : RangeRead.completeKeys db r =
(RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by
unfold RangeRead.completeKeys
simp [hrev]
rw [hck, List.filter_reverse, List.length_reverse] at hlen
rw [hbetweenFil]
exact hlen
have hbetweenLen' : (between db' ((L : Key) : WithTop Key) r.end_.key).length =
res.keys.length := by
rw [← hbetweenEq']; exact hbetweenLen
-- The extra stored keys above `end_.key` are bounded by the interval cardinality.
have hcapNat : (between db' r.end_.key r.begin.key).length + res.keys.length ≤
beginSlack r + 1 := by
have hb : ((between db' r.end_.key r.begin.key).length : ℕ∞) ≤
intervalCard r.end_.key r.begin.key :=
between_length_le_intervalCard (le_of_lt hcrossE)
have hsum : (((between db' r.end_.key r.begin.key).length + res.keys.length : ℕ) :
ℕ∞) ≤ ((beginSlack r + 1 : ℕ) : ℕ∞) := by
rw [Nat.cast_add]
calc ((between db' r.end_.key r.begin.key).length : ℕ∞) +
(res.keys.length : ℕ∞)
≤ intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) := by
gcongr
_ ≤ (beginSlack r : ℕ∞) + 1 := hcap
_ = ((beginSlack r + 1 : ℕ) : ℕ∞) := by rw [Nat.cast_add, Nat.cast_one]
exact_mod_cast hsum
have hsplit := between_length_add (db := db') (L := ((L : Key) : WithTop Key))
(E := r.end_.key) (B := r.begin.key) (le_of_lt hLE) (le_of_lt hcrossE)
have hboundLen : (between db' ((L : Key) : WithTop Key) r.begin.key).length ≤
beginSlack r + 1 := by
rw [hsplit, hbetweenLen']; omega
have hboundInt :
((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) +
r.begin.offset - 1 ≤ 0 := by
have hs : (beginSlack r : Int) = -r.begin.offset := by
unfold beginSlack
rw [Int.toNat_of_nonneg (show (0 : Int) ≤ 0 - r.begin.offset by omega)]
ring
have hble : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤
(beginSlack r : Int) + 1 := by exact_mod_cast hboundLen
linarith
have hLdb' : L ∈ db'.keys := RangeRead.compatible_keys_subset hcomp'' hLmem
have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) :=
resolve_begin_le_of_between_bound hLdb' hLcross hboundInt
have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by
rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb'
rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne]
refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp''.1, ?_⟩
have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) hrev hrb'
have h2' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r') hr'_rev
(le_of_eq hres'_db')
have h2 := (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp
hcomp'' |>.2
rw [h1, ← h2']
exact h2
intro db₁ db₂ hagree
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
· -- **Necessity** is the under-end argument; it does not use `begin.key ≤ end_.key`.
intro k hk cr hs
have hkL : L ≤ k := WithTop.coe_le_coe.mp hk.1
have hkend : ((k : Key) : WithTop Key) < r.end_.key := hk.2
by_cases hkres : k ∈ res.keys
· exact necessary_of_mem_result hreadAll hkres cr hs
· by_cases hkhead : k ≤ res.keys.head hne
· exact necessary_of_between_result hreadAll hLmem (List.head_mem hne) hkL hkhead cr hs
· have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead)
exact necessary_head_to_end_key hreadAll hrev hne (le_of_eq hend) cr hs hheadk hkend
/-- The reversed crossed negative-begin truncated small-gap cell. -/
abbrev negCrossTruncRevCrossedSmall (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
r.end_.key < r.begin.key ∧ r.end_.offset = 1 ∧
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) ≤
(beginSlack r : ℕ∞) + 1
/-- The closed form of the reversed crossed negative-begin truncated small-gap cell:
`[last reported, end_.key)`. -/
noncomputable def closedFormNegCrossTruncRevCrossedSmall (_r : RangeRead) (res : Result) :
ConflictRange :=
truncatedRangeRev _r (res.keys.getLastD ([] : Key))
/-- **The reversed crossed negative-begin truncated small-gap closed form is
minimal.** -/
theorem minimal_closedForm_of_negCross_trunc_rev_crossedSmall {r : RangeRead}
{res : Result} (hread : Readable r res) (h : negCrossTruncRevCrossedSmall r res) :
Minimal r res (closedFormNegCrossTruncRevCrossedSmall r res) := by
obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hcap⟩ := h
have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
rw [← hlast]; exact hcrossD
unfold closedFormNegCrossTruncRevCrossedSmall
rw [hlast]
exact minimal_truncatedRangeRev_of_negCross_crossedSmall hread hmore hrev hbeg hne hcross
hcrossE hend hcap
end FormalConflictRange