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:
1 parent
7a0826e873
commit
a54a5f82f8
78 files changed
+91636
-2
No files matched your search
@@ -0,0 +1,172 @@
|
||||
/-
|
||||
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.NonposClosedForm
|
||||
import FormalConflictRange.ClosedFormTotal
|
||||
|
||||
/-!
|
||||
# Cross-free nonpositive-begin reversed truncated closed form
|
||||
|
||||
At a nonpositive begin offset the begin selector resolves at or below `begin.key`, so
|
||||
when `begin.key` lies at or below the least reported key every reported key is at or
|
||||
above the resolution. For a reversed truncated result the compatible databases are
|
||||
then exactly those whose stored keys at or above the reported last agree, and that
|
||||
filter is unchanged by replacing the begin selector with an offset-one selector keyed
|
||||
at the reported last (`completeKeys_filter_ge_eq_nonposRevReduced`). This reduces the
|
||||
whole cross-free nonpositive reversed truncated region — every end offset, overshoot or
|
||||
not — to the already-total begin-offset-one rule `closedFormOffsetOneTotal`. Soundness
|
||||
and minimality transfer because the two reads have the same compatible databases.
|
||||
-/
|
||||
|
||||
open FormalConflictRange
|
||||
|
||||
set_option maxHeartbeats 1200000
|
||||
|
||||
namespace FormalConflictRange
|
||||
|
||||
/-- The reduced read used to close cross-free nonpositive reversed truncated cells:
|
||||
its begin selector sits at offset one on the reported last key. -/
|
||||
noncomputable def nonposRevReduced (r : RangeRead) (res : Result) : RangeRead :=
|
||||
{ r with begin := { key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key), offset := 1 } }
|
||||
|
||||
/-- At offset one keyed at the reported last, a stored key lies at or above the
|
||||
resolution exactly when it lies at or above the reported last. -/
|
||||
theorem last_le_resolve_reduced_iff {db : DB} {last : Key} {k : Key} (hk : k ∈ db.keys) :
|
||||
last ≤ k ↔ resolve db ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) ≤
|
||||
((k : Key) : WithTop Key) := by
|
||||
simpa using key_le_resolve_iff_of_offset_one (db := db)
|
||||
(s := ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector)) rfl hk
|
||||
|
||||
/-- The stored keys at or above the reported last are unchanged by replacing a
|
||||
nonpositive cross-free begin selector with the offset-one selector keyed at the last. -/
|
||||
theorem completeKeys_filter_ge_eq_nonposRevReduced {db : DB} {r : RangeRead} {res : Result}
|
||||
(hrev : r.reverse = true) (hbegin : r.begin.offset ≤ 0)
|
||||
(hlo : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) :
|
||||
(RangeRead.completeKeys db r).filter
|
||||
(fun k : Key => decide (res.keys.getLastD ([] : Key) ≤ k)) =
|
||||
(RangeRead.completeKeys db (nonposRevReduced r res)).filter
|
||||
(fun k : Key => decide (res.keys.getLastD ([] : Key) ≤ k)) := by
|
||||
set last := res.keys.getLastD ([] : Key)
|
||||
have hlo' : r.begin.key ≤ ((last : Key) : WithTop Key) := hlo
|
||||
have hred_rev : (nonposRevReduced r res).reverse = true := by
|
||||
simp [nonposRevReduced, hrev]
|
||||
have hred_beg : (nonposRevReduced r res).begin =
|
||||
({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) := rfl
|
||||
have hred_end : (nonposRevReduced r res).end_ = r.end_ := rfl
|
||||
rw [RangeRead.completeKeys, RangeRead.completeKeys, hrev, hred_rev]
|
||||
simp only [ite_true]
|
||||
rw [List.filter_reverse, List.filter_reverse, List.reverse_inj]
|
||||
unfold RangeRead.inRange
|
||||
rw [List.filter_filter, List.filter_filter]
|
||||
apply List.filter_congr
|
||||
intro k hk
|
||||
have hkey := last_le_resolve_reduced_iff (db := db) (last := last) (k := k) hk
|
||||
have hstart : resolve db r.begin ≤ ((last : Key) : WithTop Key) :=
|
||||
le_trans (resolve_le_key_of_offset_nonpos hbegin) hlo'
|
||||
rw [hred_beg, hred_end]
|
||||
by_cases hlk : ((last : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)
|
||||
· have hred : resolve db r.begin ≤ ((k : Key) : WithTop Key) ↔
|
||||
resolve db ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) ≤
|
||||
((k : Key) : WithTop Key) := by
|
||||
constructor
|
||||
· intro _; exact hkey.mp (WithTop.coe_le_coe.mp hlk)
|
||||
· intro _; exact le_trans hstart hlk
|
||||
have hconj : (resolve db r.begin ≤ ((k : Key) : WithTop Key) ∧
|
||||
((k : Key) : WithTop Key) < resolve db r.end_) ↔
|
||||
(resolve db ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) ≤
|
||||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db r.end_) := by
|
||||
constructor <;> rintro ⟨h1, h2⟩
|
||||
· exact ⟨hred.mp h1, h2⟩
|
||||
· exact ⟨hred.mpr h1, h2⟩
|
||||
rw [decide_eq_true (WithTop.coe_le_coe.mp hlk), Bool.true_and, Bool.true_and,
|
||||
decide_eq_decide.mpr hconj]
|
||||
· rw [show decide (last ≤ k) = false
|
||||
from decide_eq_false_iff_not.mpr (fun h => hlk (WithTop.coe_le_coe.mpr h)),
|
||||
Bool.false_and, Bool.false_and]
|
||||
|
||||
/-- Compatibility is invariant under the reversed nonpositive reduction. -/
|
||||
theorem compatible_iff_nonposRevReduced {db : DB} {r : RangeRead} {res : Result}
|
||||
(hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||||
(hbegin : r.begin.offset ≤ 0)
|
||||
(hlo : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) :
|
||||
RangeRead.Compatible db r res ↔ RangeRead.Compatible db (nonposRevReduced r res) res := by
|
||||
have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||||
have hred_rev : (nonposRevReduced r res).reverse = true := by
|
||||
simp [nonposRevReduced, hrev]
|
||||
rw [compatible_iff_truncated_keys_rev hrev hmore hne,
|
||||
compatible_iff_truncated_keys_rev (db := db) (r := nonposRevReduced r res) hred_rev hmore hne]
|
||||
have hf := completeKeys_filter_ge_eq_nonposRevReduced (db := db) (r := r) (res := res)
|
||||
hrev hbegin hlo
|
||||
rw [hlast] at hf
|
||||
have hlim : (nonposRevReduced r res).withinLimit res.keys ↔ r.withinLimit res.keys := by
|
||||
simp [nonposRevReduced, RangeRead.withinLimit]
|
||||
rw [hf]
|
||||
constructor
|
||||
· rintro ⟨h1, h2⟩
|
||||
exact ⟨hlim.mpr h1, h2⟩
|
||||
· rintro ⟨h1, h2⟩
|
||||
exact ⟨hlim.mp h1, h2⟩
|
||||
|
||||
/-- Range soundness is invariant under a change of read that preserves compatibility. -/
|
||||
theorem sound_congr_of_compatible_iff {r r' : RangeRead} {res : Result} {cr : ConflictRange}
|
||||
(h : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res) :
|
||||
Sound r res cr ↔ Sound r' res cr := by
|
||||
unfold Sound
|
||||
constructor <;> intro hs db₁ db₂ hagree
|
||||
· exact (h db₁).symm.trans ((hs db₁ db₂ hagree).trans (h db₂))
|
||||
· exact (h db₁).trans ((hs db₁ db₂ hagree).trans (h db₂).symm)
|
||||
|
||||
/-- Minimality is invariant under a change of read that preserves compatibility. -/
|
||||
theorem minimal_congr_of_compatible_iff {r r' : RangeRead} {res : Result} {cr : ConflictRange}
|
||||
(h : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res) :
|
||||
Minimal r res cr ↔ Minimal r' res cr := by
|
||||
have hS : ∀ cr, Sound r res cr ↔ Sound r' res cr :=
|
||||
fun cr => sound_congr_of_compatible_iff (cr := cr) h
|
||||
have hN : ∀ k, Necessary r res k ↔ Necessary r' res k := by
|
||||
intro k
|
||||
unfold Necessary
|
||||
exact ⟨fun hk cr hs => hk cr ((hS cr).mpr hs),
|
||||
fun hk cr hs => hk cr ((hS cr).mp hs)⟩
|
||||
rw [minimal_iff_sound_and_necessary, minimal_iff_sound_and_necessary]
|
||||
constructor
|
||||
· rintro ⟨hs, hnec⟩
|
||||
exact ⟨(hS cr).mp hs, fun k hk => (hN k).mp (hnec k hk)⟩
|
||||
· rintro ⟨hs, hnec⟩
|
||||
exact ⟨(hS cr).mpr hs, fun k hk => (hN k).mpr (hnec k hk)⟩
|
||||
|
||||
/-- The cross-free nonpositive-begin reversed truncated closed form, via the
|
||||
offset-one reduction. -/
|
||||
noncomputable def closedFormNonposRevTrunc (r : RangeRead) (res : Result) : ConflictRange :=
|
||||
closedFormOffsetOneTotal (nonposRevReduced r res) res
|
||||
|
||||
/-- A nonempty *truncated reversed* result at a nonpositive begin offset, cross-free,
|
||||
with the begin key at or below the least reported key. Every end offset is allowed. -/
|
||||
abbrev nonposRevTrunc (r : RangeRead) (res : Result) : Prop :=
|
||||
res.more = true ∧ r.reverse = true ∧ r.begin.offset ≤ 0 ∧
|
||||
r.begin.key ≤ ((leastReported r res : Key) : WithTop Key)
|
||||
|
||||
/-- **The cross-free nonpositive-begin reversed truncated closed form is minimal.** -/
|
||||
theorem minimal_closedForm_of_nonpos_revTrunc {r : RangeRead} {res : Result}
|
||||
(hread : Readable r res) (h : nonposRevTrunc r res) :
|
||||
Minimal r res (closedFormNonposRevTrunc r res) := by
|
||||
obtain ⟨hmore, hrev, hbegin, hloD⟩ := h
|
||||
obtain ⟨db, hcomp⟩ := hread
|
||||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore
|
||||
have hlo : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by
|
||||
simpa [leastReported, hrev] using hloD
|
||||
have hcompiff : ∀ db, RangeRead.Compatible db r res ↔
|
||||
RangeRead.Compatible db (nonposRevReduced r res) res :=
|
||||
fun db => compatible_iff_nonposRevReduced hrev hmore hne hbegin hlo
|
||||
have hread' : Readable (nonposRevReduced r res) res := ⟨db, (hcompiff db).mp hcomp⟩
|
||||
have hmin' : Minimal (nonposRevReduced r res) res
|
||||
(closedFormOffsetOneTotal (nonposRevReduced r res) res) :=
|
||||
minimal_closedForm_of_offset_one_total hread' hne rfl
|
||||
unfold closedFormNonposRevTrunc
|
||||
exact (minimal_congr_of_compatible_iff (r := r) (r' := nonposRevReduced r res)
|
||||
(res := res) (cr := closedFormOffsetOneTotal (nonposRevReduced r res) res) hcompiff).mpr hmin'
|
||||
|
||||
end FormalConflictRange
|
||||
Reference in new issue
Block a user