Files
conflict-range/Proof.lean
T
andrew efd1b323e7 Prove minimality is unique and add it to the headlines
- Define `ConflictRange.toSet`, the set of keys lying in a range
- Add the headline `MinimalUnique`: any two minimal sound ranges for the
  same read and result denote the same set of keys
- Transport `FormalConflictRange.Minimal.toSet_eq` across the bridge; the
  statement is unconditional, since `Minimal` already carries soundness
- State `Statement.minimalUnique` in the challenge and add it to the
  comparator's `theorem_names`

`lake build` is warning-free and `lake comparator` accepts both headlines.
2026-11-17 11:39:52 -05:00

141 lines
5.9 KiB
Lean4

import Bridge
/-!
# Transporting the reference theorem to this repository's statement
`Bridge` proves, for each definition of `Statement`, that its reference
counterpart agrees with it under the translation `toRef*`. This file uses those
bridge lemmas to transport the reference library's minimality theorem for the
closed form to `Statement.ClosedFormMinimal`.
-/
namespace Statement
open FormalConflictRange
/-! ## Compatibility and readability -/
theorem compatible_iff (db : DB) (r : RangeRead) (res : Result) :
FormalConflictRange.RangeRead.Compatible (toRefDB db) (toRefRead r) (toRefRes res) ↔
RangeRead.Compatible db r res := by
unfold FormalConflictRange.RangeRead.Compatible RangeRead.Compatible
simp [FormalConflictRange.RangeRead.withinLimit, FormalConflictRange.RangeRead.completeKeys,
RangeRead.completeKeys, FormalConflictRange.RangeRead.inRange, RangeRead.inRange]
theorem readable_iff (r : RangeRead) (res : Result) :
FormalConflictRange.Readable (toRefRead r) (toRefRes res) ↔ Readable r res := by
unfold FormalConflictRange.Readable Readable
constructor
· rintro ⟨db, hdb⟩
exact ⟨ofRefDB db, (compatible_iff (ofRefDB db) r res).mp (by simpa using hdb)⟩
· rintro ⟨db, hdb⟩
exact ⟨toRefDB db, (compatible_iff db r res).mpr hdb⟩
/-! ## Conflict-range keys, agreement and subranges -/
theorem keys_eq (cr : ConflictRange) (db : DB) :
FormalConflictRange.ConflictRange.keys (toRefCR cr) (toRefDB db) =
ConflictRange.keys cr db := by
unfold FormalConflictRange.ConflictRange.keys ConflictRange.keys
unfold RangeRead.inRange
rfl
theorem toSet_eq (cr : ConflictRange) :
(toRefCR cr).toSet = cr.toSet := rfl
theorem agreeOn_iff (cr : ConflictRange) (db₁ db₂ : DB) :
FormalConflictRange.AgreeOn (toRefCR cr) (toRefDB db₁) (toRefDB db₂) ↔
AgreeOn cr db₁ db₂ := by
unfold FormalConflictRange.AgreeOn AgreeOn
rw [keys_eq, keys_eq]
theorem subrange_iff (cr' cr : ConflictRange) :
FormalConflictRange.Subrange (toRefCR cr') (toRefCR cr) ↔ Subrange cr' cr := by
unfold FormalConflictRange.Subrange Subrange
rfl
theorem properSubrange_iff (cr' cr : ConflictRange) :
FormalConflictRange.ProperSubrange (toRefCR cr') (toRefCR cr) ↔
ProperSubrange cr' cr := by
unfold FormalConflictRange.ProperSubrange ProperSubrange
rw [subrange_iff, subrange_iff]
/-! ## Soundness and minimality -/
theorem sound_iff (r : RangeRead) (res : Result) (cr : ConflictRange) :
FormalConflictRange.Sound (toRefRead r) (toRefRes res) (toRefCR cr) ↔
Sound r res cr := by
unfold FormalConflictRange.Sound Sound
constructor
· intro h db₁ db₂ hagree
have h' := h (toRefDB db₁) (toRefDB db₂) ((agreeOn_iff cr db₁ db₂).mpr hagree)
exact ((compatible_iff db₁ r res).symm.trans (h'.trans (compatible_iff db₂ r res)))
· intro h db₁ db₂ hagree
have h' := h (ofRefDB db₁) (ofRefDB db₂)
((agreeOn_iff cr (ofRefDB db₁) (ofRefDB db₂)).mp (by simpa using hagree))
have hc₁ : FormalConflictRange.RangeRead.Compatible db₁ (toRefRead r) (toRefRes res) ↔
RangeRead.Compatible (ofRefDB db₁) r res := by
simpa using compatible_iff (ofRefDB db₁) r res
have hc₂ : FormalConflictRange.RangeRead.Compatible db₂ (toRefRead r) (toRefRes res) ↔
RangeRead.Compatible (ofRefDB db₂) r res := by
simpa using compatible_iff (ofRefDB db₂) r res
exact hc₁.trans (h'.trans hc₂.symm)
theorem minimal_iff (r : RangeRead) (res : Result) (cr : ConflictRange) :
FormalConflictRange.Minimal (toRefRead r) (toRefRes res) (toRefCR cr) ↔
Minimal r res cr := by
unfold FormalConflictRange.Minimal Minimal
constructor
· rintro ⟨hs, hmin⟩
refine ⟨(sound_iff r res cr).mp hs, ?_⟩
intro cr' hproper hs'
exact hmin (toRefCR cr') ((properSubrange_iff cr' cr).mpr hproper)
((sound_iff r res cr').mpr hs')
· rintro ⟨hs, hmin⟩
refine ⟨(sound_iff r res cr).mpr hs, ?_⟩
intro cr' hproper hs'
have hproper' : ProperSubrange (ofRefCR cr') cr :=
(properSubrange_iff (ofRefCR cr') cr).mp (by simpa using hproper)
have hs'' : Sound r res (ofRefCR cr') :=
(sound_iff r res (ofRefCR cr')).mp (by simpa using hs')
exact hmin (ofRefCR cr') hproper' hs''
/-! ## The closed form -/
theorem closedForm_eq (r : RangeRead) (res : Result) :
FormalConflictRange.conflictRangeClosedFormClosed (toRefRead r) (toRefRes res) =
toRefCR (closedForm r res) := by
by_cases hne : res.keys = []
· unfold FormalConflictRange.conflictRangeClosedFormClosed closedForm
simp [hne]
· unfold FormalConflictRange.conflictRangeClosedFormClosed closedForm
simp [hne]
/-! ## The headlines -/
/-- **The closed form is minimal.** This is the reference theorem
`minimal_conflictRangeClosedFormClosed`, transported across the definitional
bridge. -/
theorem closedFormMinimal : ClosedFormMinimal := by
intro r res hread
have hread' : FormalConflictRange.Readable (toRefRead r) (toRefRes res) :=
(readable_iff r res).mpr hread
have hmin := FormalConflictRange.minimal_conflictRangeClosedFormClosed hread'
rw [closedForm_eq r res] at hmin
exact (minimal_iff r res (closedForm r res)).mp hmin
/-- **Minimality is unique.** This is the reference theorem
`FormalConflictRange.Minimal.toSet_eq`, transported across the definitional
bridge. It is unconditional: the reference proof uses only that the
intersection of two sound ranges is sound, so no readability hypothesis is
needed. -/
theorem minimalUnique : MinimalUnique := by
intro r res cr cr' h h'
have h₁ : FormalConflictRange.Minimal (toRefRead r) (toRefRes res) (toRefCR cr) :=
(minimal_iff r res cr).mpr h
have h₂ : FormalConflictRange.Minimal (toRefRead r) (toRefRes res) (toRefCR cr') :=
(minimal_iff r res cr').mpr h'
have hset := FormalConflictRange.Minimal.toSet_eq h₁ h₂
rwa [toSet_eq cr, toSet_eq cr'] at hset
end Statement