Files
conflict-range/Proof.lean
T

192 lines
8.4 KiB
Lean4

/-
Released under Apache 2.0 license as described in the file LICENSE.
-/
import Bridge
/-!
# Transporting the reference theorem to the public 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 -/
/-- At a nonpositive begin offset the reversed-truncated rule with no begin skip
agrees with the clamped reference rule: both reduce to `compactReverseTruncatedZero`. -/
theorem ref_closedFormNonposRevTrunc_eq (r : FormalConflictRange.RangeRead)
(res : FormalConflictRange.Result) (h : r.begin.offset ≤ 0)
(hm : res.more = true) (hr : r.reverse = true) :
FormalConflictRange.closedFormNonposRevTrunc r res =
FormalConflictRange.compactReverseTruncated r res := by
have hB : FormalConflictRange.residualB r = 0 := by
unfold FormalConflictRange.residualB
rw [Int.toNat_eq_zero]
omega
simp only [FormalConflictRange.closedFormNonposRevTrunc,
FormalConflictRange.compactReverseTruncated, hB, ite_true,
FormalConflictRange.closedFormOffsetOneTotal,
FormalConflictRange.nonposRevReduced,
FormalConflictRange.closedFormOffsetOneUniversal, hm, hr]
split <;> rfl
/-- On a clamped nonpositive read the reference positive and clamped rules are the
same function: `compactClamped` differs from `compactPositive` only in the
reversed-truncated branch, where the begin offset is nonpositive and the clamp
makes the two rules coincide. -/
theorem ref_compactClamped_eq_compactPositive (r : FormalConflictRange.RangeRead)
(res : FormalConflictRange.Result) (h : FormalConflictRange.clampedNonpos r res) :
FormalConflictRange.compactClamped r res =
FormalConflictRange.compactPositive r res := by
obtain ⟨hne, hbegin, _hlo⟩ := h
unfold FormalConflictRange.compactClamped FormalConflictRange.compactPositive
FormalConflictRange.compactSplit
by_cases hm : res.more = false
· simp [hm, hne]
· have hm' : res.more = true := by cases h : res.more <;> simp_all
simp only [hm]
by_cases hr : r.reverse = false
· simp [hr, hne]
· have hr' : r.reverse = true := by cases h : r.reverse <;> simp_all
simp [hr, hne, ref_closedFormNonposRevTrunc_eq r res hbegin hm' hr']
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]
· by_cases hpos : 1 ≤ r.begin.offset
· unfold FormalConflictRange.conflictRangeClosedFormClosed closedForm
simp [hne, hpos]
· by_cases hclamp : clampedNonpos r res
· have hclampRef : FormalConflictRange.clampedNonpos (toRefRead r) (toRefRes res) := by
obtain ⟨hb, hk⟩ := hclamp
exact ⟨hne, hb, hk⟩
unfold FormalConflictRange.conflictRangeClosedFormClosed closedForm
rw [ref_compactClamped_eq_compactPositive _ _ hclampRef]
simp [hne, hpos, hclamp, FormalConflictRange.clampedNonpos, Statement.clampedNonpos]
· unfold FormalConflictRange.conflictRangeClosedFormClosed closedForm
simp [hne, hpos, hclamp]
/-! ## 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