Files
conflict-range/FormalConflictRange/NegCrossTruncClampLong.lean
T

186 lines
9.6 KiB
Lean4

/-
Released under Apache 2.0 license as described in the file LICENSE.
-/
import FormalConflictRange.NegCrossClampLongNonpos
import FormalConflictRange.NonposRevClosedForm
/-!
# The crossed negative-begin truncated long clamp cell at a nonpositive end offset
A forward truncated result with `end_.key < begin.key`, a negative begin offset,
`end.offset ≤ 0`, a clamping begin selector, and at least one reported key at or above
`end_.key`. In that situation every stored key in the complete window lies below
`resolve end ≤ end_.key ≤ last`, so the truncated result is compatible exactly when the
corresponding complete result is. The minimal sound range is therefore the same as in
the complete long clamp cell: `[⊥, clampLongUpper r res]`.
The two ingredient lemmas are generic. The first says that when the end selector cuts at
or below the reported last, truncation is invisible: compatibility of the truncated
result is compatibility of the complete result. The second transfers minimality across a
change of the result when the compatible databases are the same.
-/
namespace FormalConflictRange
/-- Minimality is invariant under a change of the result that preserves compatibility. -/
theorem minimal_congr_of_compatible_iff_result {r : RangeRead} {res 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 := by
intro cr
unfold Sound
constructor
· intro hs db₁ db₂ hagree
exact (h db₁).symm.trans ((hs db₁ db₂ hagree).trans (h db₂))
· intro hs db₁ db₂ hagree
exact (h db₁).trans ((hs db₁ db₂ hagree).trans (h db₂).symm)
unfold Minimal
refine ⟨fun ⟨hs, hn⟩ => ⟨(hS cr).mp hs, ?_⟩, fun ⟨hs, hn⟩ => ⟨(hS cr).mpr hs, ?_⟩⟩
· intro cr' hsub hs'
exact hn cr' hsub ((hS cr').mpr hs')
· intro cr' hsub hs'
exact hn cr' hsub ((hS cr').mp hs')
/-- When the end selector has a nonpositive offset and its key is at or below the
reported last, the truncated result is compatible exactly when the complete result is:
every stored key of the complete window lies below `resolve end ≤ end_.key ≤ last`. -/
theorem compatible_truncated_iff_complete_of_end_le_last {db : DB} {r : RangeRead}
{res : Result}
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
(hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0)
(hlast : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) :
RangeRead.Compatible db r res ↔
RangeRead.Compatible db r { res with more := false } := by
have hlast' : r.end_.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by
rwa [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] at hlast
constructor
· intro hc
obtain ⟨hlim, hfil⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hc
have hck : RangeRead.completeKeys db r = res.keys := by
have hself : (RangeRead.completeKeys db r).filter
(fun (k : Key) => decide (k ≤ res.keys.getLast hne)) =
RangeRead.completeKeys db r := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
have hxlt : ((x : Key) : WithTop Key) < resolve db r.end_ :=
(RangeRead.mem_completeKeys.mp hx).2.2
have h1 : ((x : Key) : WithTop Key) < r.end_.key :=
lt_of_lt_of_le hxlt (resolve_le_key_of_offset_nonpos hend)
have h2 : ((x : Key) : WithTop Key) ≤
((res.keys.getLast hne : Key) : WithTop Key) :=
le_of_lt (lt_of_lt_of_le h1 hlast')
exact WithTop.coe_le_coe.mp h2
rw [hself] at hfil
exact hfil
exact ⟨hlim, Or.inr ⟨rfl, hck.symm⟩⟩
· intro hc
have hlim : RangeRead.withinLimit r res.keys := hc.1
have heq : res.keys = RangeRead.completeKeys db r := by
rcases hc.2 with ⟨hm, _⟩ | ⟨-, heq⟩
· exact absurd hm (by simp)
· exact heq
have hfil : (RangeRead.completeKeys db r).filter
(fun (k : Key) => decide (k ≤ res.keys.getLast hne)) = res.keys := by
rw [← heq]
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
exact (compatible_iff_truncated_keys hrev hmore hne).mpr ⟨hlim, hfil⟩
/-- The crossed negative-begin truncated long clamp cell at `end.offset ≤ 0`: a
clamping begin selector, `end_.key < begin.key`, and at least one reported key at or above
`end_.key`. -/
def negCrossTruncClampLongNonpos (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧
clampNonposBottom r res ∧ r.end_.key < r.begin.key ∧
r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)
/-- **The crossed negative-begin truncated long clamp cell is minimal with the complete
long clamp closed form `[⊥, clampLongUpper]`.** -/
theorem minimal_closedForm_of_negCross_trunc_clamp_long_nonpos {r : RangeRead}
{res : Result} (hread : Readable r res) (hcell : negCrossTruncClampLongNonpos r res) :
Minimal r res (closedFormNegCrossClampLongNonpos r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hmq, hbottom, hlong, hlastE⟩ := hcell
let res' : Result := { res with more := false }
have hcompat : ∀ db, RangeRead.Compatible db r res ↔
RangeRead.Compatible db r res' :=
fun db => compatible_truncated_iff_complete_of_end_le_last hread hrev hmore hne hend hlastE
have hread' : Readable r res' := by
obtain ⟨db, hcomp⟩ := hread
exact ⟨db, (hcompat db).mp hcomp⟩
have hcell' : negCrossClampLongNonpos r res' := by
refine ⟨hrev, hbegin, rfl, hne, hend, ?_, hmq, hbottom, hlong⟩
simpa [res'] using hcrossD
have hmin := minimal_closedForm_of_negCross_clamp_long_nonpos hread' hcell'
have hcf : closedFormNegCrossClampLongNonpos r res' =
closedFormNegCrossClampLongNonpos r res := by
unfold closedFormNegCrossClampLongNonpos clampLongUpper clampLongCap clampLongC
simp only [res']
rfl
refine (minimal_congr_of_compatible_iff_result (r := r) (res := res) (res' := res')
(cr := closedFormNegCrossClampLongNonpos r res) hcompat).mpr ?_
rwa [← hcf]
/-- The crossed negative-begin truncated short cell at `end.offset ≤ 0`: `end_.key <
begin.key` and at least one reported key at or above `end_.key`. Truncation is invisible
here, because `resolve end ≤ end_.key ≤ last`. Both capacity regimes are included. -/
def negCrossTruncShortNonpos (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧
r.end_.key < r.begin.key ∧
r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)
/-- The closed form of the crossed negative-begin truncated short cell: the complete long
clamp rule when the begin selector clamps, and the nonclamping full-window rule
`[head, begin.key)` otherwise. -/
noncomputable def closedFormNegCrossTruncShortNonpos (r : RangeRead) (res : Result) :
ConflictRange :=
if clampNonposBottom r res then
⟨(([] : Key) : WithTop Key), clampLongUpper r res⟩
else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩
/-- **The crossed negative-begin truncated short cell is minimal.** Truncation is
invisible (`compatible_truncated_iff_complete_of_end_le_last`), so both capacity regimes
transfer from the complete crossed nonpositive-end theorems. -/
theorem minimal_closedForm_of_negCross_trunc_short_nonpos {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncShortNonpos r res) :
Minimal r res (closedFormNegCrossTruncShortNonpos r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hmq, hlong, hlastE⟩ := hcell
by_cases hbottom : clampNonposBottom r res
· rw [closedFormNegCrossTruncShortNonpos, ite_eq_left hbottom]
exact minimal_closedForm_of_negCross_trunc_clamp_long_nonpos hread
⟨hmore, hrev, hbegin, hne, hcrossD, hend, hmq, hbottom, hlong, hlastE⟩
· rw [closedFormNegCrossTruncShortNonpos, ite_eq_right hbottom]
let res' : Result := { res with more := false }
have hcompat : ∀ db, RangeRead.Compatible db r res ↔
RangeRead.Compatible db r res' :=
fun db => compatible_truncated_iff_complete_of_end_le_last hread hrev hmore hne hend hlastE
have hread' : Readable r res' := by
obtain ⟨db, hcomp⟩ := hread
exact ⟨db, (hcompat db).mp hcomp⟩
have hcell' : negCrossNonclampLongNonpos r res' := by
refine ⟨hrev, hbegin, rfl, hne, hend, ?_, ?_, ?_, hlong⟩
· exact hcrossD
· exact hmq
· exact hbottom
have hmin := minimal_closedForm_of_negCross_nonclamp_long_nonpos hread' hcell'
have hcf : closedFormNegCrossNonclampLongNonpos r res' =
(⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ :
ConflictRange) := by
unfold closedFormNegCrossNonclampLongNonpos
rfl
rw [hcf] at hmin
exact (minimal_congr_of_compatible_iff_result (r := r) (res := res) (res' := res')
(cr := ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩)
hcompat).mpr hmin
end FormalConflictRange