/- 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.ClosedFormAll import FormalConflictRange.NegCrossTruncClampAny import FormalConflictRange.NegCrossTruncClampLastGe import FormalConflictRange.NegCrossTruncClampLong import FormalConflictRange.NegCrossTruncAnchor import FormalConflictRange.NegCrossTruncBottomGap import FormalConflictRange.NegCrossTruncNonposGapA2a import FormalConflictRange.NegCrossTruncNonposGapA2aBottom import FormalConflictRange.NegCrossTruncNonposGapCrossFree import FormalConflictRange.NegCrossTruncNonclampNonpos import FormalConflictRange.NegCrossTruncStrictTop import FormalConflictRange.NegCrossTruncTopTop /-! # A compact endpoint-oriented closed form The regional development proves many small formulas correct. This module turns those certificates into a smaller algorithm. Its first rule handles every nonempty complete result at a positive begin offset, in forward traversal, without distinguishing cross-free, equal-selector, residual, and crossed reads at the top level. Throughout, `b = begin.offset - 1`, `e = end.offset - 1`, and `t` is the number of reported keys at or above `end.key`. The finite-boundary test is uniformly `d + t = e`, where `d = max 0 (b - intervalCard begin.key end.key)`. On crossed reads the interval cardinality is zero, so this is exactly the old crossed test `t + b = e`. -/ namespace FormalConflictRange /-- The finite-boundary deficit shared by all positive-offset complete reads. -/ noncomputable def compactDeficit (r : RangeRead) : ℕ := residualD r (intervalCard r.begin.key r.end_.key) /-- The shared finite-boundary condition `d + t = e`. -/ def compactFinite (r : RangeRead) (res : Result) : Prop := compactDeficit r + residualT r res = residualE r /-- A nonpositive begin selector at or below the first reported key clamps to the empty key. This is the only lower-endpoint normalization needed by the forward formulas. -/ def clampsToBottom (r : RangeRead) (res : Result) : Prop := r.begin.offset ≤ 0 ∧ r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) /-- The effective lower endpoint after begin clamping. -/ noncomputable def compactBegin (r : RangeRead) (res : Result) : WithTop Key := by classical exact if clampsToBottom r res then (([] : Key) : WithTop Key) else r.begin.key /-- Clamping also makes the crossed-capacity test automatic. -/ noncomputable def compactForwardCapacity (r : RangeRead) (res : Result) : ℕ∞ := by classical exact if clampsToBottom r res then 0 else intervalCard r.end_.key r.begin.key /-- The reported key whose immediate successor bounds a finite complete window. -/ def completeBoundary (r : RangeRead) (res : Result) : Key := if r.reverse then res.keys.headD ([] : Key) else res.keys.getLastD ([] : Key) /-- One compact rule for every forward complete result at a positive begin offset. Below end offset two the answer is the canonical range. On cross-free selectors the lower end is always `begin.key`; `t = e`, an overshooting end, and the general deficit test determine the upper end. On crossed selectors the same deficit test gives the finite answer, while the unique extra capacity test chooses the lower end of an unbounded answer. -/ noncomputable def compactCompleteForward (r : RangeRead) (res : Result) : ConflictRange := by classical exact if r.end_.offset ≤ 1 then ⟨compactBegin r res, r.end_.key⟩ else if clampsToBottom r res ∨ r.begin.key ≤ r.end_.key then if residualT r res = residualE r then ⟨compactBegin r res, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ else if ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key then ⟨compactBegin r res, ⊤⟩ else if compactFinite r res then ⟨compactBegin r res, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ else ⟨compactBegin r res, ⊤⟩ else if compactFinite r res then ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ else ⟨if (crossedOvershootGeneral r res : ℕ∞) ≤ intervalCard r.end_.key r.begin.key then r.end_.key else r.begin.key, ⊤⟩ /-- On cross-free selectors the compact rule is literally the previously certified rule. In particular, the equal-selector branch needs no special case: its gap is zero. -/ theorem compactCompleteForward_eq_crossFree {r : RangeRead} {res : Result} (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : compactCompleteForward r res = closedFormCompleteCrossFree r res := by classical have hclamp : ¬ clampsToBottom r res := by simp only [clampsToBottom, not_and] intro h; omega have hlower : compactBegin r res = r.begin.key := by simp [compactBegin, hclamp] unfold compactCompleteForward rw [hlower] simp only [hclamp, false_or] by_cases hend : r.end_.offset ≤ 1 · simp [closedFormCompleteCrossFree, canonicalRange, closedFormOffsetOne, hend] · have htail : (if r.begin.key = r.end_.key then closedFormKeyEqComplete r res else closedFormResidualGeneral r res (intervalCard r.begin.key r.end_.key)) = if compactFinite r res then truncatedRange r (res.keys.getLastD ([] : Key)) else topRange r := by by_cases hkey : r.begin.key = r.end_.key · rw [ite_eq_left hkey] have hgap : intervalCard r.begin.key r.end_.key = 0 := intervalCard_eq_zero_of_le (le_of_eq hkey.symm) simp [closedFormKeyEqComplete, compactFinite, compactDeficit, hgap, residualD, residualB, residualT, residualE, Nat.add_comm] · rw [ite_eq_right hkey] unfold closedFormResidualGeneral compactFinite compactDeficit by_cases hfin : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res = residualE r · simp [hfin] · simp [hfin] by_cases ht : residualT r res = residualE r · have ht' : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (r.end_.offset - 1).toNat := by simpa only [residualT, residualE] using ht have hoff : closedFormOffsetOne r res = truncatedRange r (res.keys.getLastD ([] : Key)) := by unfold closedFormOffsetOne rw [ite_eq_right hend] dsimp only rw [ite_eq_left ht'] simp [closedFormCompleteCrossFree, truncatedRange, hend, hcross, ht, hoff] · have ht' : ¬ res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (r.end_.offset - 1).toNat := by simpa only [residualT, residualE] using ht have hoff : closedFormOffsetOne r res = topRange r := by unfold closedFormOffsetOne rw [ite_eq_right hend] dsimp only rw [ite_eq_right ht'] by_cases hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key · simp only [hend, ↓reduceIte, hcross, ht, List.getLastD_eq_getLast?, closedFormCompleteCrossFree, false_or, hoff, topRange] rw [ite_eq_left hbelow, ite_eq_left hbelow] · simpa [closedFormCompleteCrossFree, truncatedRange, topRange, hend, hcross, ht, hbelow] using htail.symm /-- On crossed selectors the same compact rule is the previously certified crossed rule. Here `intervalCard begin.key end.key = 0`, so `compactFinite` is precisely the old `t + b = e` branch. -/ theorem compactCompleteForward_eq_crossed {r : RangeRead} {res : Result} (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) : compactCompleteForward r res = completeForwardCrossedGeneral r res := by classical have hclamp : ¬ clampsToBottom r res := by simp only [clampsToBottom, not_and] intro h; omega have hlower : compactBegin r res = r.begin.key := by simp [compactBegin, hclamp] unfold compactCompleteForward rw [hlower] simp only [hclamp, false_or] have hgap : intervalCard r.begin.key r.end_.key = 0 := intervalCard_eq_zero_of_le (le_of_lt hcross) have hfin : compactFinite r res ↔ reportedAtEnd r res + (r.begin.offset - 1).toNat = endOvershoot r := by simp [compactFinite, compactDeficit, residualD, hgap, residualB, residualT, residualE, reportedAtEnd, endOvershoot, Nat.add_comm] by_cases hend : r.end_.offset ≤ 1 · simp [completeForwardCrossedGeneral, canonicalRange, hend] · by_cases hf : compactFinite r res · have hf' : reportedAtEnd r res + (r.begin.offset - 1).toNat = endOvershoot r := hfin.mp hf have hf'' : reportedAtEnd r res + (r.begin.offset.toNat - 1) = endOvershoot r := by simpa using hf' simp [completeForwardCrossedGeneral, hend, not_le.mpr hcross, hf, hf''] · have hf' : ¬ reportedAtEnd r res + (r.begin.offset - 1).toNat = endOvershoot r := fun h => hf (hfin.mpr h) have hf'' : ¬ reportedAtEnd r res + (r.begin.offset.toNat - 1) = endOvershoot r := by simpa using hf' simp [completeForwardCrossedGeneral, hend, not_le.mpr hcross, hf, hf''] /-- A nonpositive begin offset has zero residual deficit, regardless of the key gap. -/ theorem compactDeficit_of_nonpos {r : RangeRead} (hbegin : r.begin.offset ≤ 0) : compactDeficit r = 0 := by simp [compactDeficit, residualD, residualB] omega /-- On a clamped nonpositive read, the compact complete rule is exactly the old bottom-anchored three-branch formula. -/ theorem compactCompleteForward_eq_nonpos {r : RangeRead} {res : Result} (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hlo : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : compactCompleteForward r res = completeNonposCrossFree r res := by classical have hclamp : clampsToBottom r res := ⟨hbegin, hlo⟩ have hlower : compactBegin r res = (([] : Key) : WithTop Key) := by simp [compactBegin, hclamp] have hfin : compactFinite r res ↔ residualT r res = residualE r := by simp [compactFinite, compactDeficit_of_nonpos hbegin] unfold compactCompleteForward completeNonposCrossFree rw [ite_eq_right hne, hlower] simp only [hclamp, true_or, ite_true] by_cases hend : r.end_.offset ≤ 1 · rw [ite_eq_left hend, ite_eq_left hend] · rw [ite_eq_right hend, ite_eq_right hend] by_cases ht : residualT r res = residualE r · rw [ite_eq_left ht, ite_eq_left ht] rfl · rw [ite_eq_right ht, ite_eq_right ht] by_cases hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key · rw [ite_eq_left hhi] · rw [ite_eq_right hhi, ite_eq_right (fun hf => ht (hfin.mp hf))] /-- The compact forward rule is minimal on every readable nonempty complete result at a positive begin offset. -/ theorem compactCompleteForward_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : Minimal r res (compactCompleteForward r res) := by by_cases hcross : r.begin.key ≤ r.end_.key · rw [compactCompleteForward_eq_crossFree hbegin hcross] exact minimal_closedForm_of_complete_crossFree hread hmore hrev hne hbegin hcross · have hcrossed : r.end_.key < r.begin.key := lt_of_not_ge hcross rw [compactCompleteForward_eq_crossed hbegin hcrossed] exact minimal_completeForwardCrossedGeneral hread hmore hne hrev hbegin hcrossed /-- Normalize a complete observation to forward traversal. Compatibility is unchanged by `compatible_flip`; only the order of the reported keys changes. -/ def normalizeCompleteResult (r : RangeRead) (res : Result) : Result := { keys := if r.reverse then res.keys.reverse else res.keys, more := false } /-- One complete-result algorithm for both traversals: normalize to forward traversal, then apply the single forward formula. -/ noncomputable def compactComplete (r : RangeRead) (res : Result) : ConflictRange := compactCompleteForward { r with reverse := false } (normalizeCompleteResult r res) /-- The normalized complete formula is minimal in either traversal order. -/ theorem compactComplete_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : Minimal r res (compactComplete r res) := by by_cases hrev : r.reverse = true · let r' : RangeRead := { r with reverse := false } let res' : Result := { keys := res.keys.reverse, more := false } have hread' : Readable r' res' := by obtain ⟨db, hcomp⟩ := hread exact ⟨db, (compatible_flip (b := true) hrev hmore).mpr hcomp⟩ have hne' : res'.keys ≠ [] := by simpa [res'] using hne have hmin' : Minimal r' res' (compactCompleteForward r' res') := compactCompleteForward_minimal hread' rfl rfl hne' hbegin have hmin := (minimal_flip (r := r) (res := res) (cr := compactCompleteForward r' res') (b := true) hrev hmore).mp hmin' simpa [compactComplete, normalizeCompleteResult, hrev, r', res'] using hmin · have hrevf : r.reverse = false := by cases hr : r.reverse <;> simp_all have hr : { r with reverse := false } = r := by cases r simp_all have hres : normalizeCompleteResult r res = res := by cases res simp_all [normalizeCompleteResult] unfold compactComplete rw [hr, hres] exact compactCompleteForward_minimal hread hmore hrevf hne hbegin /-- Complete-result normalization also subsumes the old nonpositive cross-free rule: after reversing the reported keys when necessary, it is literally the same forward formula with its lower endpoint clamped to `⊥`. -/ theorem compactComplete_eq_nonpos_crossFree {r : RangeRead} {res : Result} (h : nonposCompleteCrossFree r res) : compactComplete r res = closedFormCompleteNonposCrossFree r res := by obtain ⟨hmore, hne, hbegin, hloD⟩ := h by_cases hrev : r.reverse = true · let r' : RangeRead := { r with reverse := false } let res' : Result := { keys := res.keys.reverse, more := false } have hloD' : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by simpa [leastReported, hrev] using hloD have hlo' : r'.begin.key ≤ ((res'.keys.headD ([] : Key) : Key) : WithTop Key) := by simpa [r', res'] using hloD' have heq := compactCompleteForward_eq_nonpos (r := r') (res := res') (by simpa [res'] using hne) (by simpa [r'] using hbegin) hlo' simpa [compactComplete, normalizeCompleteResult, hrev, r', res', closedFormCompleteNonposCrossFree, completeNonposCrossFreeRev] using heq · have hrevf : r.reverse = false := by cases hr : r.reverse <;> simp_all have hlo : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by simpa [leastReported, hrevf] using hloD have hr : { r with reverse := false } = r := by cases r; simp_all have hres : normalizeCompleteResult r res = res := by cases res simp_all [normalizeCompleteResult] unfold compactComplete rw [hr, hres] unfold closedFormCompleteNonposCrossFree rw [ite_eq_right (by simp [hrevf])] exact compactCompleteForward_eq_nonpos hne hbegin hlo /-- The compact complete rule is minimal on the whole nonpositive cross-free cell. -/ theorem compactComplete_minimal_nonpos {r : RangeRead} {res : Result} (hread : Readable r res) (h : nonposCompleteCrossFree r res) : Minimal r res (compactComplete r res) := by rw [compactComplete_eq_nonpos_crossFree h] exact minimal_closedForm_of_complete_nonpos_crossFree hread h /-- One forward-truncated rule at every positive begin offset. The same capacity comparison handles crossed and cross-free selectors: in the latter case the interval `[end.key, begin.key)` is empty, so the comparison is automatic and the lower endpoint is `begin.key`. -/ noncomputable def compactForwardTruncated (r : RangeRead) (res : Result) : ConflictRange := if r.end_.offset ≤ 0 then ⟨compactBegin r res, r.end_.key⟩ else if compactForwardCapacity r res ≤ (((residualE r - res.keys.length - residualB r : ℕ) : ℕ∞)) then ⟨compactBegin r res, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ else ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ /-- The unified capacity test reduces to the old cross-free truncated rule. -/ theorem compactForwardTruncated_eq_crossFree {r : RangeRead} {res : Result} (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : compactForwardTruncated r res = closedFormTruncatedGeneral r res := by classical have hclamp : ¬ clampsToBottom r res := by simp only [clampsToBottom, not_and] intro h; omega have hlower : compactBegin r res = r.begin.key := by simp [compactBegin, hclamp] have hcap : compactForwardCapacity r res = intervalCard r.end_.key r.begin.key := by simp [compactForwardCapacity, hclamp] unfold compactForwardTruncated rw [hlower, hcap] have hgap : intervalCard r.end_.key r.begin.key = 0 := intervalCard_eq_zero_of_le hcross by_cases hend : r.end_.offset ≤ 0 · simp [closedFormTruncatedGeneral, canonicalRange, hend] · simp [closedFormTruncatedGeneral, truncatedRange, hend, hgap, residualE, residualB] /-- On crossed selectors the unified rule is the certified capacity formula. -/ theorem compactForwardTruncated_eq_crossed {r : RangeRead} {res : Result} (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) : compactForwardTruncated r res = truncatedForwardCrossedGeneral r res := by classical have hclamp : ¬ clampsToBottom r res := by simp only [clampsToBottom, not_and] intro h; omega have hlower : compactBegin r res = r.begin.key := by simp [compactBegin, hclamp] have hcap : compactForwardCapacity r res = intervalCard r.end_.key r.begin.key := by simp [compactForwardCapacity, hclamp] unfold compactForwardTruncated rw [hlower, hcap] have hend0 : ¬ r.end_.offset ≤ 0 := by omega simp [truncatedForwardCrossedGeneral, truncatedRange, hend0, residualE, residualB] /-- The compact forward-truncated rule is minimal at every positive begin offset. -/ theorem compactForwardTruncated_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : Minimal r res (compactForwardTruncated r res) := by by_cases hcross : r.begin.key ≤ r.end_.key · rw [compactForwardTruncated_eq_crossFree hbegin hcross] exact minimal_closedForm_of_truncated_general hread hmore hrev hbegin hcross · have hcrossed : r.end_.key < r.begin.key := lt_of_not_ge hcross have hend : 2 ≤ r.end_.offset := crossed_end_offset_ge_two_truncated hread hmore hbegin hcrossed rw [compactForwardTruncated_eq_crossed hbegin (by omega)] exact minimal_truncatedForwardCrossedGeneral hread hmore hrev hne hbegin hcrossed hend /-- The compact forward-truncated rule denotes exactly the general necessary-key hull. -/ theorem compactForwardTruncated_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : (compactForwardTruncated r res).toSet = (conflictRangeGeneral r res).toSet := (compactForwardTruncated_minimal hread hmore hrev hne hbegin).toSet_eq (conflictRangeGeneral_minimal hread) /-- The compact forward-truncated rule agrees with the executable reference. -/ theorem compactForwardTruncated_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : (compactForwardTruncated r res).toSet = (simpleConflictRange r res).toSet := (compactForwardTruncated_minimal hread hmore hrev hne hbegin).toSet_eq (simpleConflictRange_minimal hread) /-- On a clamped nonpositive forward read, the unified capacity formula is exactly the bottom-anchored truncated formula, with no split on the selector keys. -/ theorem compactForwardTruncated_eq_nonpos {r : RangeRead} {res : Result} (hbegin : r.begin.offset ≤ 0) (hlo : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : compactForwardTruncated r res = if r.end_.offset ≤ 0 then ⟨(([] : Key) : WithTop Key), r.end_.key⟩ else truncatedRangeBottom (res.keys.getLastD ([] : Key)) := by classical have hclamp : clampsToBottom r res := ⟨hbegin, hlo⟩ have hlower : compactBegin r res = (([] : Key) : WithTop Key) := by simp [compactBegin, hclamp] have hcap : compactForwardCapacity r res = 0 := by simp [compactForwardCapacity, hclamp] unfold compactForwardTruncated rw [hlower, hcap] by_cases hend : r.end_.offset ≤ 0 · rw [ite_eq_left hend, ite_eq_left hend] · rw [ite_eq_right hend, ite_eq_right hend, ite_eq_left (show (0 : ℕ∞) ≤ _ from by simp)] rfl /-- The same forward-truncated formula is minimal on every clamped nonpositive read. -/ theorem compactForwardTruncated_minimal_nonpos {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hbegin : r.begin.offset ≤ 0) (hlo : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : Minimal r res (compactForwardTruncated r res) := by have hloD : r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) := by simpa [leastReported, hrev] using hlo rw [compactForwardTruncated_eq_nonpos hbegin hlo] by_cases hend : r.end_.offset ≤ 0 · rw [ite_eq_left hend] exact minimal_closedForm_of_nonpos_fwdTruncNonpos hread ⟨hmore, hrev, hbegin, hloD, hend⟩ · have hendpos : 1 ≤ r.end_.offset := by omega rw [ite_eq_right hend] exact minimal_closedForm_of_nonpos_fwdTrunc hread ⟨hmore, hrev, hbegin, hloD, hendpos⟩ /-- The extra gap capacity allowed when the reported head is the empty key. The empty key itself accounts for the one-unit difference between the two clamping thresholds. -/ def compactClampSlack (r : RangeRead) (res : Result) : ℕ := beginSlack r + 1 - res.keys.length + if res.keys.headD ([] : Key) = ([] : Key) then 1 else 0 /-- The original small-capacity test: the clamping gap is small enough to lower the effective begin key to the successor of the reported last key. -/ noncomputable def compactClampGapSmall (r : RangeRead) (res : Result) : Prop := intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key ≤ (compactClampSlack r res : ℕ∞) /-- A nonempty-head gap at exact capacity whose empty-key end window has exactly one position per reported key. This boundary case has the same shorter range as a small gap. -/ noncomputable def compactClampGapExactSaturated (r : RangeRead) (res : Result) : Prop := res.keys.headD ([] : Key) ≠ ([] : Key) ∧ r.end_.key = (([] : Key) : WithTop Key) ∧ (r.end_.offset - 1).toNat = res.keys.length ∧ intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) r.begin.key = ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) /-- The clamping cases whose upper endpoint is the successor of the reported last key. -/ noncomputable def compactClampGapLow (r : RangeRead) (res : Result) : Prop := compactClampGapSmall r res ∨ compactClampGapExactSaturated r res /-- The upper endpoint of a clamped nonpositive forward-truncated result. -/ noncomputable def compactClampUpper (r : RangeRead) (res : Result) : WithTop Key := by classical exact if compactClampGapLow r res then ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) else r.begin.key /-- A nonclamping low end is saturated when the gap below the reported head and the reported keys together need more than the `end.offset - 1` available positions. -/ noncomputable def compactForwardSaturated (r : RangeRead) (res : Result) : Prop := 0 < residualE r ∧ r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧ (residualE r : ℕ∞) < intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key) + (res.keys.length : ℕ∞) /-- One arithmetic rule for every nonpositive-begin forward-truncated result. The number of reported keys below `begin.key` decides whether the begin selector clamps. A clamped result starts at `⊥`; its positive-end upper endpoint is governed by the gap capacity above the reported last key. Otherwise the upper endpoint is the larger of `begin.key` and that successor, and only a saturated gap below the reported head moves the lower endpoint down to `end.key`. -/ noncomputable def compactForwardTruncatedNonpos (r : RangeRead) (res : Result) : ConflictRange := by classical exact if reportedBelowBegin r res ≤ beginSlack r then if r.end_.offset ≤ 0 then if r.end_.key < r.begin.key then if clampNonposBottom r res then if ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key then closedFormNegCrossTruncNonposGapA2aBottom r res else ⟨(([] : Key) : WithTop Key), clampLongUpper r res⟩ else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ else if negCrossTruncTopTopEq r res then closedFormNegCrossTruncTopTopEq r res else if clampNonposBottom r res then ⟨(([] : Key) : WithTop Key), r.end_.key⟩ else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ else ⟨(([] : Key) : WithTop Key), compactClampUpper r res⟩ else if r.end_.offset ≤ 0 then ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), max r.begin.key r.end_.key⟩ else ⟨if compactForwardSaturated r res then r.end_.key else ((res.keys.headD ([] : Key) : Key) : WithTop Key), max r.begin.key ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)⟩ /-- Clamping reduces the nonpositive rule to its bottom-anchored branch, provided the both-sentinel equality branch is not active. -/ theorem compactForwardTruncatedNonpos_eq_clamped {r : RangeRead} {res : Result} (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hnA : ¬ negCrossTruncTopTopEq r res) : compactForwardTruncatedNonpos r res = if r.end_.offset ≤ 0 then if r.end_.key < r.begin.key then if clampNonposBottom r res then if ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key then closedFormNegCrossTruncNonposGapA2aBottom r res else ⟨(([] : Key) : WithTop Key), clampLongUpper r res⟩ else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ else if clampNonposBottom r res then ⟨(([] : Key) : WithTop Key), r.end_.key⟩ else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ else ⟨(([] : Key) : WithTop Key), compactClampUpper r res⟩ := by classical simp [compactForwardTruncatedNonpos, hclamp, hnA] /-- In the both-sentinel equality branch the clamping rule is the closed form `[head, ⊤)`. -/ theorem compactForwardTruncatedNonpos_eq_topTopEq {r : RangeRead} {res : Result} (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hA : negCrossTruncTopTopEq r res) : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncTopTopEq r res := by classical have hend : r.end_.offset ≤ 0 := NegCrossTruncTopTop.hend hA.1 have hnotlt : ¬ r.end_.key < r.begin.key := by rw [NegCrossTruncTopTop.hEtop hA.1, NegCrossTruncTopTop.hBtop hA.1] exact not_lt.mpr le_rfl simp [compactForwardTruncatedNonpos, hclamp, hend, hnotlt, hA] open Classical in /-- Nonclamping at a positive end offset reduces to the reported-boundary rule, with the single saturation test on the lower endpoint. -/ theorem compactForwardTruncatedNonpos_eq_nonclamp {r : RangeRead} {res : Result} (hclamp : ¬ reportedBelowBegin r res ≤ beginSlack r) (hend : 1 ≤ r.end_.offset) : compactForwardTruncatedNonpos r res = ⟨if compactForwardSaturated r res then r.end_.key else ((res.keys.headD ([] : Key) : Key) : WithTop Key), max r.begin.key ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)⟩ := by classical have hend0 : ¬ r.end_.offset ≤ 0 := by omega simp [compactForwardTruncatedNonpos, hclamp, hend0] /-- A nonclamping nonpositive end has the reported head and the larger selector key as its two endpoints. -/ theorem compactForwardTruncatedNonpos_eq_nonclamp_nonpos {r : RangeRead} {res : Result} (hclamp : ¬ reportedBelowBegin r res ≤ beginSlack r) (hend : r.end_.offset ≤ 0) : compactForwardTruncatedNonpos r res = ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by classical simp [compactForwardTruncatedNonpos, hclamp, hend] /-- The low-capacity clamping branch ends immediately after the reported last key. -/ theorem compactClampUpper_eq_keyAfter {r : RangeRead} {res : Result} (hlow : compactClampGapLow r res) : compactClampUpper r res = ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) := by classical simp [compactClampUpper, hlow] /-- The high-capacity clamping branch ends at the original begin key. -/ theorem compactClampUpper_eq_begin {r : RangeRead} {res : Result} (hlow : ¬ compactClampGapLow r res) : compactClampUpper r res = r.begin.key := by classical simp [compactClampUpper, hlow] /-- When there is no gap, either capacity branch gives the same upper endpoint. -/ theorem compactClampUpper_eq_begin_of_noGap {r : RangeRead} {res : Result} (hgap : r.begin.key = ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) : compactClampUpper r res = r.begin.key := by classical simp [compactClampUpper, compactClampGapLow, hgap] /-- The ordinary low-capacity inequality implies the unified threshold, independently of whether the reported head is empty. -/ theorem compactClampGapLow_of_le {r : RangeRead} {res : Result} (hlow : intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key ≤ ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞)) : compactClampGapLow r res := by apply Or.inl unfold compactClampGapSmall compactClampSlack apply hlow.trans exact_mod_cast (show beginSlack r + 1 - res.keys.length ≤ beginSlack r + 1 - res.keys.length + (if res.keys.headD ([] : Key) = ([] : Key) then 1 else 0) by omega) /-- Exhausting the begin slack by one reported key is exactly the nonclamping side of the compact clamp test. -/ theorem not_clamp_of_reportedBelowBegin_eq {r : RangeRead} {res : Result} (h : reportedBelowBegin r res = beginSlack r + 1) : ¬ reportedBelowBegin r res ≤ beginSlack r := by omega /-- At begin offset zero, a reported head below `begin.key` already rules out clamping. -/ theorem not_clamp_of_zero_cross {r : RangeRead} {res : Result} (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) : ¬ reportedBelowBegin r res ≤ beginSlack r := by have hpos : 0 < reportedBelowBegin r res := countP_pos_of_mem (headD_mem hne) (by rw [decide_eq_true_eq]; exact hcross) have hq : beginSlack r = 0 := by simp [beginSlack, hbegin] omega /-- Every reported key of a forward end-offset-one result lies strictly below the end key, whether or not the result is complete. -/ theorem reported_below_end_of_end_one {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) (hend : r.end_.offset = 1) : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by intro k hk have hkck := (RangeRead.compatible_keys_prefix hcomp).subset hk rw [completeKeys_eq_filter_of_end_one hrev hend] at hkck exact of_decide_eq_true (List.mem_filter.mp (List.mem_filter.mp hkck).1).2 /-- In a crossed negative-begin truncated read, the number of reported keys below `begin.key` exceeds the begin slack by at most one. In the nonclamping case the begin selector resolves onto the reported head; its rank equation then shows that exactly `beginSlack + 1` stored keys lie in `[head, begin.key)`. -/ theorem truncated_reportedBelowBegin_le_succ {r : RangeRead} {res : Result} (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) : reportedBelowBegin r res ≤ beginSlack r + 1 := by obtain ⟨db, hcomp⟩ := hread by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r · omega · have hnon : beginSlack r < reportedBelowBegin r res := lt_of_not_ge hclamp have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hcross' : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by rwa [hheadD] at hcross set head : Key := res.keys.head hne with hhead set cB : ℕ := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) with hcB set N : ℕ := (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length with hN have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev have hres_count : res.keys.countP (fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := by unfold reportedBelowBegin rw [List.countP_eq_length_filter, List.countP_eq_length_filter] congr 1 apply List.filter_congr intro k hk have hhead_le : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := WithTop.coe_le_coe.mpr ((RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk) rw [decide_eq_decide] exact and_iff_right hhead_le have hmin_le : reportedBelowBegin r res ≤ N := by have h := List.Sublist.countP_le hsub (p := fun k : Key => decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) rw [hres_count, List.countP_eq_length_filter, ← hN] at h exact h have hm_cB : reportedBelowBegin r res ≤ cB := by have h := List.Sublist.countP_le hsub (p := fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) exact h have hcB_ge : beginSlack r + 1 ≤ cB := by omega have hanchor : 0 ≤ r.begin.anchor db := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge omega have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne have hhead_ck : head ∈ RangeRead.completeKeys db r := hpre.subset hhead_mem have hle_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) := (RangeRead.mem_completeKeys.mp hhead_ck).2.1 have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by intro htop rw [htop] at hle_head exact absurd hle_head (not_le.mpr (WithTop.coe_lt_top head)) have hltlen : (r.begin.anchor db).toNat < db.keys.length := by by_contra h exact hnot_top (resolve_eq_top hanchor (le_of_not_gt h)) have hL : resolve db r.begin = ((head : Key) : WithTop Key) := resolve_begin_eq_head_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin hcross' hnon have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor hltlen have hval : db.keys[(r.begin.anchor db).toNat] = head := WithTop.coe_inj.mp (hget.symm.trans hL) have hcH : db.keys.countP (fun k : Key => decide (k < head)) = (r.begin.anchor db).toNat := countP_lt_eq_of_getElem db.sorted hltlen hval have htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by have hanchor_cast : r.begin.anchor db = (cB : ℤ) - (beginSlack r : ℤ) - 1 := by rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] have hz : r.begin.anchor db = ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by rw [hanchor_cast]; push_cast; ring have hz2 : ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) = ((cB - (beginSlack r + 1) : ℕ) : ℤ) := by rw [← Nat.cast_sub hcB_ge] rw [hz, hz2, Int.toNat_natCast] have hsplit : cB = db.keys.countP (fun k : Key => decide (k < head)) + N := by rw [hcB, hN] exact countP_lt_eq_add_filter_withTop db.keys db.nodup (le_of_lt hcross') have hN_eq : N = beginSlack r + 1 := by omega omega /-- The compact nonpositive rule is minimal on every crossed begin-zero truncated read. -/ theorem compactForwardTruncatedNonpos_minimal_zero {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcross have hclamp := not_clamp_of_zero_cross hne hbegin hcrossD by_cases hend : r.end_.offset ≤ 0 · rw [compactForwardTruncatedNonpos_eq_nonclamp_nonpos hclamp hend] exact minimal_closedForm_of_zeroCross_trunc_nonposEnd hread ⟨hbegin, hrev, hmore, hcross, hend⟩ · have hendpos : 1 ≤ r.end_.offset := by omega rw [compactForwardTruncatedNonpos_eq_nonclamp hclamp hendpos] by_cases hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) · by_cases hend1 : r.end_.offset = 1 · have hsat : ¬ compactForwardSaturated r res := by simp [compactForwardSaturated, residualE, hend1] rw [ite_eq_right hsat] exact minimal_closedForm_of_zeroCross_trunc_readAllEnd hread ⟨hbegin, hrev, hmore, hcross, Or.inl hend1⟩ · have hend2 : 2 ≤ r.end_.offset := by omega by_cases hsat : compactForwardSaturated r res · rw [ite_eq_left hsat] exact minimal_closedForm_of_zeroCross_trunc_lowEndBig hread ⟨hbegin, hrev, hmore, hcross, hend2, by simpa [leastReported, hrev] using hEH, by simpa [leastReported, hrev, residualE] using hsat.2.2⟩ · rw [ite_eq_right hsat] have hcap : intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key) + (res.keys.length : ℕ∞) ≤ (residualE r : ℕ∞) := by apply le_of_not_gt intro h exact hsat ⟨by unfold residualE; omega, hEH, h⟩ exact minimal_closedForm_of_zeroCross_trunc_lowEndSmall hread ⟨hbegin, hrev, hmore, hcross, hend2, by simpa [leastReported, hrev] using hEH, by simpa [leastReported, hrev, residualE] using hcap⟩ · have hsat : ¬ compactForwardSaturated r res := fun h => hEH h.2.1 rw [ite_eq_right hsat] exact minimal_closedForm_of_zeroCross_trunc_endGeHead hread ⟨hbegin, hrev, hmore, hcross, hendpos, by simpa [leastReported, hrev] using not_lt.mp hEH⟩ /-- On a bottom-clamped read, the unified gap test is automatic and recovers the cross-free nonpositive formula. -/ theorem compactForwardTruncatedNonpos_minimal_bottom {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hlo : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical obtain ⟨db, hcomp⟩ := hread have hread' : Readable r res := ⟨db, hcomp⟩ have hzero : reportedBelowBegin r res = 0 := by rw [reportedBelowBegin, List.countP_eq_zero] intro k hk rw [decide_eq_true_eq] intro hkB have hheadk : res.keys.headD ([] : Key) ≤ k := headD_le_of_pairwise (RangeRead.compatible_sortedLT hcomp hrev).pairwise hk exact (not_lt.mpr (le_trans hlo (WithTop.coe_le_coe.mpr hheadk))) hkB have hclamp : reportedBelowBegin r res ≤ beginSlack r := by omega rw [compactForwardTruncatedNonpos_eq_clamped hclamp (not_negCross_trunc_topTop_eq_of_begin_le_coe hlo)] have hloD : r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) := by simpa [leastReported, hrev] using hlo by_cases hend : r.end_.offset ≤ 0 · have hhead_ck : res.keys.headD ([] : Key) ∈ RangeRead.completeKeys db r := (RangeRead.compatible_keys_prefix hcomp).subset (headD_mem hne) have hheadE : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.end_.key := lt_end_key_of_lt_resolve_end_nonpos hend (RangeRead.mem_completeKeys.mp hhead_ck).1 (RangeRead.mem_completeKeys.mp hhead_ck).2.2 have hBE : r.begin.key ≤ r.end_.key := le_trans hlo (le_of_lt hheadE) have hhead_res : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < resolve db r.end_ := (RangeRead.mem_completeKeys.mp hhead_ck).2.2 have hB_le_R : r.begin.key ≤ resolve db r.end_ := le_trans hlo (le_of_lt hhead_res) have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by intro hR rw [hR] at hhead_res exact absurd hhead_res (not_lt.mpr (coe_nil_le_key_withTop _)) have hcb : clampNonposBottom r res := clampNonposBottom_of_le_resolve_end hend hRne hB_le_R hclamp rw [ite_eq_left hend, ite_eq_right (not_lt_of_ge hBE), ite_eq_left hcb] exact minimal_closedForm_of_nonpos_fwdTruncNonpos hread' ⟨hmore, hrev, hbegin, hloD, hend⟩ · have hendpos : 1 ≤ r.end_.offset := by omega have hheadlast : res.keys.headD ([] : Key) ≤ res.keys.getLastD ([] : Key) := headD_le_of_pairwise (RangeRead.compatible_sortedLT hcomp hrev).pairwise (getLastD_mem hne) have hgap : intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key = 0 := intervalCard_eq_zero_of_le (le_trans hlo (WithTop.coe_le_coe.mpr hheadlast)) have hlow : compactClampGapLow r res := by apply Or.inl unfold compactClampGapSmall rw [hgap] exact zero_le rw [ite_eq_right hend, compactClampUpper_eq_keyAfter hlow] exact minimal_closedForm_of_nonpos_fwdTrunc hread' ⟨hmore, hrev, hbegin, hloD, hendpos⟩ /-- The compact nonpositive rule is minimal on every crossed negative-begin truncated nonclamping read with a positive end offset. -/ theorem compactForwardTruncatedNonpos_minimal_neg_nonclamp {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hbegin : r.begin.offset < 0) (hend : 1 ≤ r.end_.offset) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical have hclamp := not_clamp_of_reportedBelowBegin_eq hnonclamp rw [compactForwardTruncatedNonpos_eq_nonclamp hclamp hend] by_cases hend1 : r.end_.offset = 1 · have hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by obtain ⟨db, hcomp⟩ := hread exact reported_below_end_of_end_one hcomp hrev hend1 have hsat : ¬ compactForwardSaturated r res := by simp [compactForwardSaturated, residualE, hend1] rw [ite_eq_right hsat] exact minimal_closedForm_of_negCross_trunc_nonclamp_below hread ⟨hmore, hrev, hbegin, hend, hbelow, hcross, hnonclamp⟩ · have hend2 : 2 ≤ r.end_.offset := by omega by_cases hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) · by_cases hsat : compactForwardSaturated r res · rw [ite_eq_left hsat] exact minimal_closedForm_of_negCross_trunc_nonclamp_lowEndBig hread ⟨hmore, hrev, hbegin, hend2, by simpa [leastReported, hrev] using hEH, hcross, hnonclamp, by simpa [leastReported, hrev, residualE] using hsat.2.2⟩ · rw [ite_eq_right hsat] have hcap : intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key) + (res.keys.length : ℕ∞) ≤ (residualE r : ℕ∞) := by apply le_of_not_gt intro h exact hsat ⟨by unfold residualE; omega, hEH, h⟩ exact minimal_closedForm_of_negCross_trunc_nonclamp_lowEndSmall hread ⟨hmore, hrev, hbegin, hend2, by simpa [leastReported, hrev] using hEH, hcross, hnonclamp, by simpa [leastReported, hrev, residualE] using hcap⟩ · have hsat : ¬ compactForwardSaturated r res := fun h => hEH h.2.1 rw [ite_eq_right hsat] exact minimal_closedForm_of_negCross_trunc_nonclamp_endGeHead hread ⟨hmore, hrev, hbegin, hend, by simpa [leastReported, hrev] using not_lt.mp hEH, hcross, hnonclamp⟩ /-- The compact nonpositive rule is minimal on every crossed negative-begin truncated nonclamping read with a nonpositive end offset. -/ theorem compactForwardTruncatedNonpos_minimal_neg_nonclamp_nonpos {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical have hclamp : ¬ reportedBelowBegin r res ≤ beginSlack r := by omega rw [compactForwardTruncatedNonpos_eq_nonclamp_nonpos hclamp hend] have hmin := minimal_closedForm_of_negCross_trunc_nonclamp_nonposEnd hread ⟨hmore, hrev, hbegin, hne, hcross, hend, hnonclamp⟩ simpa [closedFormNegCrossTruncNonclampNonpos] using hmin /-- If the reported last reaches `begin.key`, the clamping gap has zero capacity and the compact rule is the bottom-anchored reported range. This holds for an arbitrary positive end key: filtering a compatible database below the reported head supplies the necessity witness even when the end key lies inside the reported window. -/ theorem compactForwardTruncatedNonpos_minimal_neg_clamp_lastGe {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 1 ≤ r.end_.offset) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hBlast : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical obtain ⟨db, hcomp⟩ := hread have hread' : Readable r res := ⟨db, hcomp⟩ have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa only [leastReported, hrev, Bool.false_eq_true, ite_false] using hcross rw [hheadD] at hcrossD have hcard : intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key = 0 := intervalCard_eq_zero_of_le hBlast have hlow : compactClampGapLow r res := by apply Or.inl unfold compactClampGapSmall rw [hcard] exact zero_le rw [compactForwardTruncatedNonpos_eq_clamped hclamp (not_negCross_trunc_topTop_eq_of_begin_le_coe hBlast), ite_eq_right (by omega), compactClampUpper_eq_keyAfter hlow] have hcr : (⟨(([] : Key) : WithTop Key), ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)⟩ : ConflictRange) = truncatedRangeBottom (res.keys.getLast hne) := by rw [hlastD] rfl rw [hcr] refine minimal_iff_sound_and_necessaryRange.mpr ⟨ sound_truncatedRangeBottom_of_nonpos_more hrev (by omega) hmore hne (by rwa [← hlastD]) hend, ?_⟩ intro k _ hkend have hkle : k ≤ res.keys.getLast hne := (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hkend) by_cases hkhead : k < res.keys.head hne · exact necessary_below_head_of_negCross_trunc_clamp_lastGe hread' hrev hmore hne hbegin hend hcrossD hclamp (by rwa [← hlastD]) (WithTop.coe_lt_coe.mpr hkhead) · exact necessary_of_between_result hread' (List.head_mem hne) (List.getLast_mem hne) (le_of_not_gt hkhead) hkle /-- The compact nonpositive rule is minimal on the crossed negative-begin truncated clamping cell with a nonpositive end offset when the reported last reaches `begin.key`. The end key then lies strictly above `begin.key`, so the compact rule selects its bottom-anchored branch `[⊥, end_.key)`. -/ theorem compactForwardTruncatedNonpos_minimal_neg_clamp_nonpos_lastGe {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hBlast : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical obtain ⟨db, hcomp⟩ := hread have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa only [leastReported, hrev, Bool.false_eq_true, ite_false] using hcross have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db r := (RangeRead.compatible_keys_prefix hcomp).subset (List.getLast_mem hne) obtain ⟨hlast_mem, -, hlast_res⟩ := RangeRead.mem_completeKeys.mp hlast_ck have hlastE : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res have hB_le_last : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by rw [← hlastD]; exact hBlast have hnotlt : ¬ r.end_.key < r.begin.key := by intro hlt exact absurd (lt_of_le_of_lt hB_le_last hlastE) (not_lt.mpr (le_of_lt hlt)) have hB_le_R : r.begin.key ≤ resolve db r.end_ := le_trans hB_le_last (le_of_lt hlast_res) have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by intro hR rw [hR] at hlast_res exact absurd hlast_res (not_lt.mpr (coe_nil_le_key_withTop _)) have hcb : clampNonposBottom r res := clampNonposBottom_of_le_resolve_end hend hRne hB_le_R hclamp rw [compactForwardTruncatedNonpos_eq_clamped hclamp (not_negCross_trunc_topTop_eq_of_begin_le_coe hBlast), ite_eq_left hend, ite_eq_right hnotlt, ite_eq_left hcb] simpa only [closedFormNegCrossTruncClampLastGe] using minimal_closedForm_of_negCross_trunc_clamp_lastGe ⟨db, hcomp⟩ ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hclamp, hBlast⟩ /-- The compact nonpositive rule is minimal on every crossed negative-begin truncated clamping read with positive end offset. Small gaps and saturated exact-capacity gaps end at `keyAfter last`; insertion-capacity and unsaturated padded gaps end at `begin.key`. -/ theorem compactForwardTruncatedNonpos_minimal_neg_clamp {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 1 ≤ r.end_.offset) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical obtain ⟨db, hcomp⟩ := hread have hread' : Readable r res := ⟨db, hcomp⟩ rw [compactForwardTruncatedNonpos_eq_clamped hclamp (not_negCross_trunc_topTop_eq_of_one_le_end hend), ite_eq_right (by omega)] by_cases hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key · by_cases hlow : compactClampGapLow r res · rw [compactClampUpper_eq_keyAfter hlow] by_cases hbase : intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key ≤ ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) · exact minimal_closedForm_of_negCross_trunc_clamp_gapLow hread' ⟨hmore, hrev, hbegin, hne, hend, hcross, hgap, hclamp, hbase⟩ · by_cases hheadbot : res.keys.headD ([] : Key) = ([] : Key) · have hsmall : compactClampGapSmall r res := by rcases hlow with hsmall | hsat · exact hsmall · exact absurd hheadbot hsat.1 have hheadbot' : res.keys.head?.getD ([] : Key) = ([] : Key) := by simpa only [List.headD_eq_head?] using hheadbot have hmid : intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key ≤ (((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞) := by simpa [compactClampGapSmall, compactClampSlack, hheadbot, hheadbot'] using hsmall exact minimal_closedForm_of_negCross_trunc_clamp_gapBotMid hread' ⟨hmore, hrev, hbegin, hne, hend, hcross, hgap, hclamp, hheadbot, hmid⟩ · have hheadne' : res.keys.head?.getD ([] : Key) ≠ ([] : Key) := by simpa only [List.headD_eq_head?] using hheadbot have hsat : compactClampGapExactSaturated r res := by rcases hlow with hsmall | hsat · have hbase' : intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key ≤ ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) := by simpa [compactClampGapSmall, compactClampSlack, hheadbot, hheadne'] using hsmall exact absurd hbase' hbase · exact hsat exact minimal_closedForm_of_negCross_trunc_clamp_gapExactSaturated hread' ⟨hmore, hrev, hbegin, hne, hend, hcross, hgap, hclamp, hsat.2.1, hsat.2.2.1, hsat.2.2.2⟩ · rw [compactClampUpper_eq_begin hlow] have hsmall : ¬ compactClampGapSmall r res := fun h => hlow (Or.inl h) by_cases hins : (((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞) ≤ intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) r.begin.key · exact minimal_beginRange_of_gap_insert_capacity hread' hmore hrev hne hbegin hend hcross hgap hclamp hins · have hAfin : intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) r.begin.key ≠ ⊤ := by intro htop apply hins rw [htop] exact le_top have hAle : intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) r.begin.key ≤ ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) := by apply (ENat.lt_add_one_iff (n := ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞)) (by simp)).mp simpa only [ENat.natCast_add, ENat.natCast_one] using lt_of_not_ge hins by_cases hheadbot : res.keys.headD ([] : Key) = ([] : Key) · apply False.elim apply hsmall unfold compactClampGapSmall compactClampSlack rw [ite_eq_left hheadbot, intervalCard_last_eq_keyAfter_add_one hgap] simpa only [ENat.natCast_add, ENat.natCast_one] using add_le_add_left hAle 1 · have hT_le_A : ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) ≤ intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) r.begin.key := by by_contra hnot apply hsmall have hAlt : intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) r.begin.key < ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) := lt_of_not_ge hnot have hcard : intervalCard ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.begin.key ≤ ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) := by rw [intervalCard_last_eq_keyAfter_add_one hgap] exact (ENat.add_one_le_iff hAfin).mpr hAlt have hheadne' : res.keys.head?.getD ([] : Key) ≠ ([] : Key) := by simpa only [List.headD_eq_head?] using hheadbot simpa [compactClampGapSmall, compactClampSlack, hheadbot, hheadne'] using hcard have hAeq : intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) r.begin.key = ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) := le_antisymm hAle hT_le_A by_cases hsat : r.end_.key = (([] : Key) : WithTop Key) ∧ (r.end_.offset - 1).toNat = res.keys.length · exact absurd (Or.inr ⟨hheadbot, hsat.1, hsat.2, hAeq⟩ : compactClampGapLow r res) hlow · apply minimal_beginRange_of_gap_padded hread' refine ⟨hmore, hrev, hbegin, hne, hend, hcross, hgap, hclamp, hheadbot, hT_le_A, ?_⟩ by_cases hE : r.end_.key = (([] : Key) : WithTop Key) · exact Or.inr (fun hcount => hsat ⟨hE, hcount⟩) · exact Or.inl hE · have hgapEq : r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) := le_antisymm (not_lt.mp hgap) (keyAfter_le_of_lt hlastB) rw [compactClampUpper_eq_begin_of_noGap hgapEq] exact minimal_closedForm_of_negCross_trunc_clamp_noGap hread' ⟨hmore, hrev, hbegin, hne, hend, hcross, hgapEq, hclamp⟩ /-- The low-capacity clamping branch is certified for an arbitrary end key. This regional wrapper is retained for callers, though the general clamping theorem now subsumes it. -/ theorem compactForwardTruncatedNonpos_minimal_neg_clamp_low {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 1 ≤ r.end_.offset) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) (_hlow : compactClampGapLow r res) : Minimal r res (compactForwardTruncatedNonpos r res) := compactForwardTruncatedNonpos_minimal_neg_clamp hread hmore hrev hne hbegin hend hcross hclamp hlastB /-- The no-gap clamping branch is also certified for an arbitrary end key, including the case where the unified capacity test happens to select the high branch. -/ theorem compactForwardTruncatedNonpos_minimal_neg_clamp_noGap {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 1 ≤ r.end_.offset) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hclamp : reportedBelowBegin r res ≤ beginSlack r) (hgap : r.begin.key = ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical rw [compactForwardTruncatedNonpos_eq_clamped hclamp (not_negCross_trunc_topTop_eq_of_one_le_end hend), ite_eq_right (by omega), compactClampUpper_eq_begin_of_noGap hgap] exact minimal_closedForm_of_negCross_trunc_clamp_noGap hread ⟨hmore, hrev, hbegin, hne, hend, hcross, hgap, hclamp⟩ /-- Convenience wrapper for the below-end clamping region. The boundary hypothesis is retained in this regional interface, but the general clamping proof no longer needs it. -/ theorem compactForwardTruncatedNonpos_minimal_neg_clamp_below {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 1 ≤ r.end_.offset) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) (hclamp : reportedBelowBegin r res ≤ beginSlack r) (_hlastE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical by_cases hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key · exact compactForwardTruncatedNonpos_minimal_neg_clamp hread hmore hrev hne hbegin hend hcross hclamp hlastB · exact compactForwardTruncatedNonpos_minimal_neg_clamp_lastGe hread hmore hrev hne hbegin hend hcross hclamp (not_lt.mp hlastB) /-- The exact residual left by the compact nonpositive forward-truncation certificates: a crossed negative-begin read with a nonpositive end offset that also clamps, except for the sentinel-end strict cell, the both-sentinel cell, the crossed short cell where a reported key lies at or above the end key, the bottom-gap cell where the end key lies at or above the begin key with enough gap capacity, and the `end_.key < begin.key` non-clamping gap cell. Every nonclamping nonpositive-end cell, every positive-end cell (including arbitrary-end clamping gaps at exact capacity), the sentinel-end strict cell with `begin.key < ⊤`, the whole both-sentinel cell, the whole crossed short cell, the crossed bottom-gap cell, and the crossed nonpositive-end non-clamping gap cell are certified. -/ def compactForwardNonposResidual (r : RangeRead) (res : Result) : Prop := res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ ¬ (r.end_.key = (⊤ : WithTop Key) ∧ r.begin.key < r.end_.key) ∧ ¬ negCrossTruncTopTop r res ∧ ¬ negCrossTruncShortNonpos r res ∧ ¬ negCrossTruncBottomGap r res ∧ ¬ negCrossTruncNonposGapA2aNonbottom r res ∧ ¬ negCrossTruncNonposGapA2aBottom r res ∧ ¬ negCrossTruncNonposGapCrossFree r res /-- The forward-truncated nonpositive residual of the compact formula is empty: every cell it describes falls under one of the certified certificates. -/ theorem not_compactForwardNonposResidual {r : RangeRead} {res : Result} : ¬ compactForwardNonposResidual r res := by rintro ⟨hmore, hrev, hneg, hne, hcross, hend, hclamp, hlastB, hS, hT, hshort, hBG, hA2a, hA2ab, hCF⟩ have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcross by_cases hBE : r.begin.key ≤ r.end_.key · by_cases hEne : r.end_.key ≠ (⊤ : WithTop Key) · have hEne' : r.end_.key < (⊤ : WithTop Key) := lt_of_le_of_ne le_top hEne have hlastE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le hlastB hBE by_cases hcap : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key · exact hBG ⟨hmore, hrev, hneg, hne, hlastB, hend, hclamp, hBE, hEne, hcap⟩ · have hgap : negCrossTruncNonposGap r res := ⟨hmore, hrev, hneg, hne, hcross, hend, hclamp, hlastB, hlastE, hEne', Or.inr (not_le.mp hcap)⟩ exact hCF ⟨hgap, hBE⟩ · have hEtop : r.end_.key = (⊤ : WithTop Key) := by by_contra h exact hEne h have hBtop : r.begin.key = (⊤ : WithTop Key) := by by_contra hb exact hS ⟨hEtop, by rw [hEtop]; exact lt_of_le_of_ne le_top hb⟩ exact hT ⟨hmore, hrev, hneg, hne, hBtop, hEtop, hend⟩ · have hEB' : r.end_.key < r.begin.key := not_le.mp hBE have hEne : r.end_.key < (⊤ : WithTop Key) := lt_of_lt_of_le hEB' le_top have hlastE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key := by by_contra h exact hshort ⟨hmore, hrev, hneg, hne, hcrossD, hend, hclamp, hEB', not_lt.mp h⟩ have hgap : negCrossTruncNonposGap r res := ⟨hmore, hrev, hneg, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, Or.inl hEB'⟩ by_cases hcb : clampNonposBottom r res · exact hA2ab ⟨hgap, hEB', hcb⟩ · exact hA2a ⟨hgap, hEB', hcb⟩ /-- The compact nonpositive forward-truncated formula is minimal outside the explicitly specified residual. The proof uses no regional dispatcher: bottom clamping, begin zero, nonclamping, and clamping below the end key are four arithmetic certificates. -/ theorem compactForwardTruncatedNonpos_minimal_of_not_residual {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hresidual : ¬ compactForwardNonposResidual r res) : Minimal r res (compactForwardTruncatedNonpos r res) := by classical by_cases hlo : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) · exact compactForwardTruncatedNonpos_minimal_bottom hread hmore hrev hne hbegin hlo · have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := not_le.mp hlo have hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcrossD by_cases hzero : r.begin.offset = 0 · exact compactForwardTruncatedNonpos_minimal_zero hread hmore hrev hne hzero hcross · have hneg : r.begin.offset < 0 := by omega by_cases hend : r.end_.offset ≤ 0 · by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r · by_cases hBlast : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) · exact compactForwardTruncatedNonpos_minimal_neg_clamp_nonpos_lastGe hread hmore hrev hne hneg hend hcross hclamp hBlast · by_cases hT : negCrossTruncTopTop r res · by_cases hA : negCrossTruncTopTopEq r res · have hEq : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncTopTopEq r res := compactForwardTruncatedNonpos_eq_topTopEq hclamp hA rw [hEq] exact minimal_closedForm_of_negCross_trunc_topTop_eq hread hA · have hnotlt : ¬ r.end_.key < r.begin.key := by rw [NegCrossTruncTopTop.hEtop hT, NegCrossTruncTopTop.hBtop hT] exact not_lt.mpr le_rfl have hnotH : ¬ (res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) := fun hH => hA ⟨hT, hH⟩ have hrepTop : reportedBelowBegin r res = res.keys.length := by unfold reportedBelowBegin rw [NegCrossTruncTopTop.hBtop hT, List.countP_eq_length] intro k _ rw [decide_eq_true_eq] exact WithTop.coe_lt_top k have hpEq : (1 - r.end_.offset).toNat = clampNonposD r + 1 := by have hp_cast : (((1 - r.end_.offset).toNat : ℕ) : ℤ) = 1 - r.end_.offset := NegCrossTruncTopTop.endPad_cast_of_nonpos hend have hd_cast : ((clampNonposD r : ℕ) : ℤ) = 0 - r.end_.offset := by rw [clampNonposD_eq] have hnn : 0 ≤ -r.end_.offset := by omega have h := Int.toNat_of_nonneg hnn omega have hz : (((1 - r.end_.offset).toNat : ℕ) : ℤ) = ((clampNonposD r + 1 : ℕ) : ℤ) := by push_cast omega exact_mod_cast hz have hcap0 : intervalCard r.begin.key r.end_.key = 0 := by rw [NegCrossTruncTopTop.hBtop hT] exact intervalCard_top_left r.end_.key have hle := NegCrossTruncTopTop.readable_counts_topTop hT hread have hlt : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1 := lt_of_le_of_ne hle hnotH have hcb : clampNonposBottom r res := by unfold clampNonposBottom rw [hrepTop, hcap0] have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by have hh := hlt rw [hpEq] at hh omega have hcast : ((res.keys.length + (clampNonposD r + 1) : ℕ) : ℕ∞) ≤ (beginSlack r : ℕ∞) := Nat.cast_le.mpr hnat simpa [ENat.natCast_add, add_comm, add_left_comm, add_assoc] using hcast have hEq : compactForwardTruncatedNonpos r res = ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by rw [compactForwardTruncatedNonpos_eq_clamped hclamp hA, ite_eq_left hend, ite_eq_right hnotlt, ite_eq_left hcb] have hcf : closedFormNegCrossTruncTopTop r res = ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by rw [closedFormNegCrossTruncTopTop, ite_eq_right hnotH] rw [hEq, NegCrossTruncTopTop.hEtop hT, ← hcf] exact minimal_closedForm_of_negCross_trunc_topTop hread hT · by_cases hS : r.end_.key = (⊤ : WithTop Key) ∧ r.begin.key < r.end_.key · obtain ⟨htop, hBltE⟩ := hS have hnA : ¬ negCrossTruncTopTopEq r res := by rintro ⟨hA', -⟩ have hthis : r.begin.key < r.end_.key := hBltE rw [NegCrossTruncTopTop.hBtop hA', NegCrossTruncTopTop.hEtop hA'] at hthis exact absurd hthis (not_lt.mpr le_top) have hBne : r.begin.key ≠ (⊤ : WithTop Key) := by rw [← htop] exact ne_of_lt hBltE have hcb : clampNonposBottom r res := by unfold clampNonposBottom rw [htop, intervalCard_top_right hBne] simp only [ENat.sub_top, add_zero] exact Nat.cast_le.mpr hclamp have hEq : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncStrictTop r res := by rw [compactForwardTruncatedNonpos_eq_clamped hclamp hnA, ite_eq_left hend] rw [ite_eq_right (by rw [htop]; exact not_lt.mpr le_top), ite_eq_left hcb] rw [htop] rfl rw [hEq] exact minimal_closedForm_of_negCross_trunc_strict_top hread ⟨hmore, hrev, hneg, hne, hcrossD, hend, hclamp, htop, not_le.mp hBlast, hBltE⟩ · by_cases hcellL : negCrossTruncShortNonpos r res · have hnA : ¬ negCrossTruncTopTopEq r res := by rintro ⟨hA', -⟩ obtain ⟨-, -, -, -, -, -, -, hlong, -⟩ := hcellL rw [NegCrossTruncTopTop.hEtop hA', NegCrossTruncTopTop.hBtop hA'] at hlong exact (not_lt.mpr le_rfl) hlong have hkeep : negCrossTruncShortNonpos r res := hcellL obtain ⟨-, -, -, -, -, -, -, hlong, hlastE⟩ := hcellL have hEq : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncShortNonpos r res := by unfold closedFormNegCrossTruncShortNonpos by_cases hcb : clampNonposBottom r res · rw [compactForwardTruncatedNonpos_eq_clamped hclamp hnA, ite_eq_left hend, ite_eq_left hlong, ite_eq_left hcb, ite_eq_right (not_lt.mpr hlastE), ite_eq_left hcb] · rw [compactForwardTruncatedNonpos_eq_clamped hclamp hnA, ite_eq_left hend, ite_eq_left hlong, ite_eq_right hcb, ite_eq_right hcb] rw [hEq] exact minimal_closedForm_of_negCross_trunc_short_nonpos hread hkeep · by_cases hBE : r.begin.key ≤ r.end_.key · by_cases hEne : r.end_.key ≠ (⊤ : WithTop Key) · by_cases hcap : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key · have hcell : negCrossTruncBottomGap r res := ⟨hmore, hrev, hneg, hne, not_le.mp hBlast, hend, hclamp, hBE, hEne, hcap⟩ have hnA : ¬ negCrossTruncTopTopEq r res := fun hA => hT hA.1 have hnotlt : ¬ r.end_.key < r.begin.key := not_lt.mpr hBE have hcb : clampNonposBottom r res := clampNonposBottom_of_negCross_trunc_bottomGap hcell have hEq : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncBottomGap r res := by rw [compactForwardTruncatedNonpos_eq_clamped hclamp hnA, ite_eq_left hend, ite_eq_right hnotlt, ite_eq_left hcb] rfl rw [hEq] exact minimal_closedForm_of_negCross_trunc_bottomGap hread hcell · have hlastE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le (not_le.mp hBlast) hBE have hgap : negCrossTruncNonposGap r res := ⟨hmore, hrev, hneg, hne, hcross, hend, hclamp, not_le.mp hBlast, hlastE, lt_of_le_of_ne le_top hEne, Or.inr (not_le.mp hcap)⟩ have hcellCF : negCrossTruncNonposGapCrossFree r res := ⟨hgap, hBE⟩ have hnA : ¬ negCrossTruncTopTopEq r res := fun hA => hT hA.1 have hEq : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncNonposGapCrossFree r res := by rw [compactForwardTruncatedNonpos_eq_clamped hclamp hnA, ite_eq_left hend, ite_eq_right (not_lt.mpr hBE)] unfold closedFormNegCrossTruncNonposGapCrossFree closedFormNegCrossTruncNonposGap rw [max_eq_right hBE] rw [hEq] exact minimal_closedForm_of_negCross_trunc_nonposGap_crossFree hread hcellCF · exact absurd ⟨hmore, hrev, hneg, hne, hcross, hend, hclamp, not_le.mp hBlast, hS, hT, hcellL, fun hcell => by obtain ⟨-, -, -, -, -, -, -, -, hEne', -⟩ := hcell exact hEne hEne', fun hA2a => by obtain ⟨hA, -, -⟩ := hA2a obtain ⟨-, -, -, -, -, -, -, -, -, hEne', -⟩ := hA exact hEne (ne_of_lt hEne'), fun hBot => by obtain ⟨hA, -, -⟩ := hBot obtain ⟨-, -, -, -, -, -, -, -, -, hEne', -⟩ := hA exact hEne (ne_of_lt hEne'), fun hcf => by obtain ⟨hgap, -⟩ := hcf obtain ⟨-, -, -, -, -, -, -, -, -, hEne', -⟩ := hgap exact hEne (ne_of_lt hEne')⟩ hresidual · have hEB' : r.end_.key < r.begin.key := not_le.mp hBE have hlastE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key := by by_contra h exact hcellL ⟨hmore, hrev, hneg, hne, hcrossD, hend, hclamp, hEB', not_lt.mp h⟩ have hEne : r.end_.key < (⊤ : WithTop Key) := lt_of_lt_of_le hEB' le_top by_cases hcb : clampNonposBottom r res · have hcellBot : negCrossTruncNonposGapA2aBottom r res := ⟨⟨hmore, hrev, hneg, hne, hcross, hend, hclamp, not_le.mp hBlast, hlastE, hEne, Or.inl hEB'⟩, hEB', hcb⟩ have hnA : ¬ negCrossTruncTopTopEq r res := fun hA => hT hA.1 have hEq : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncNonposGapA2aBottom r res := by rw [compactForwardTruncatedNonpos_eq_clamped hclamp hnA, ite_eq_left hend, ite_eq_left hEB', ite_eq_left hcb, ite_eq_left hlastE] rw [hEq] exact minimal_closedForm_of_negCross_trunc_nonposGap_A2aBottom hread hcellBot · have hcellA2a : negCrossTruncNonposGapA2aNonbottom r res := ⟨⟨hmore, hrev, hneg, hne, hcross, hend, hclamp, not_le.mp hBlast, hlastE, hEne, Or.inl hEB'⟩, hEB', hcb⟩ have hnA : ¬ negCrossTruncTopTopEq r res := fun hA => hT hA.1 have hEq : compactForwardTruncatedNonpos r res = closedFormNegCrossTruncNonposGapA2aNonbottom r res := by rw [compactForwardTruncatedNonpos_eq_clamped hclamp hnA, ite_eq_left hend, ite_eq_left hEB', ite_eq_right hcb] rfl rw [hEq] exact minimal_closedForm_of_negCross_trunc_nonposGap_A2a_nonbottom hread hcellA2a · have hm := truncated_reportedBelowBegin_le_succ hread hrev hmore hne hneg hcrossD have hnonclamp : reportedBelowBegin r res = beginSlack r + 1 := by omega exact compactForwardTruncatedNonpos_minimal_neg_nonclamp_nonpos hread hmore hrev hne hneg hend hcross hnonclamp · have hendpos : 1 ≤ r.end_.offset := by omega have hm := truncated_reportedBelowBegin_le_succ hread hrev hmore hne hneg hcrossD by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r · by_cases hBlast : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) · exact compactForwardTruncatedNonpos_minimal_neg_clamp_lastGe hread hmore hrev hne hneg hendpos hcross hclamp hBlast · exact compactForwardTruncatedNonpos_minimal_neg_clamp hread hmore hrev hne hneg hendpos hcross hclamp (not_le.mp hBlast) · have hnonclamp : reportedBelowBegin r res = beginSlack r + 1 := by omega exact compactForwardTruncatedNonpos_minimal_neg_nonclamp hread hmore hrev hneg hendpos hcross hnonclamp /-- Outside the explicit residual, the compact nonpositive forward rule denotes the general necessary-key hull. -/ theorem compactForwardTruncatedNonpos_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hresidual : ¬ compactForwardNonposResidual r res) : (compactForwardTruncatedNonpos r res).toSet = (conflictRangeGeneral r res).toSet := (compactForwardTruncatedNonpos_minimal_of_not_residual hread hmore hrev hne hbegin hresidual).toSet_eq (conflictRangeGeneral_minimal hread) /-- Outside the explicit residual, the compact nonpositive forward rule agrees with the executable reference. -/ theorem compactForwardTruncatedNonpos_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hresidual : ¬ compactForwardNonposResidual r res) : (compactForwardTruncatedNonpos r res).toSet = (simpleConflictRange r res).toSet := (compactForwardTruncatedNonpos_minimal_of_not_residual hread hmore hrev hne hbegin hresidual).toSet_eq (simpleConflictRange_minimal hread) /-- The reported count in a reversed truncated finite window. On cross-free reads only reported keys at or above `end.key` contribute; on crossed reads every reported key does. -/ def compactReverseCount (r : RangeRead) (res : Result) : ℕ := if r.end_.key < r.begin.key then res.keys.length else residualT r res /-- The finite-boundary condition for reversed truncation, using the same deficit as the complete rule. -/ def compactReverseFinite (r : RangeRead) (res : Result) : Prop := compactDeficit r + compactReverseCount r res = residualE r /-- The crossed reversed top branch has lower endpoint `end.key` exactly when the gap `[end.key, last reported)` has room for the end overshoot. -/ noncomputable def compactCrossedRoom (r : RangeRead) (res : Result) : Prop := (((residualE r - res.keys.length : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) /-- One reversed-truncated rule at `begin.offset ≥ 2`. A nonpositive end cut is canonical and an overshoot below every reported key is unbounded. Otherwise the shared deficit test chooses a finite reported-head boundary; in the unbounded crossed case one capacity comparison chooses between the two selector keys as lower endpoint. -/ noncomputable def compactReverseTruncatedSkip (r : RangeRead) (res : Result) : ConflictRange := by classical exact if r.end_.offset ≤ 1 then canonicalRange r else if ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key then topRange r else if compactReverseFinite r res then ⟨if r.end_.key < r.begin.key then r.end_.key else r.begin.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ else ⟨if r.end_.key < r.begin.key ∧ compactCrossedRoom r res then r.end_.key else r.begin.key, ⊤⟩ /-- The compact reversed-truncated rule is minimal whenever `begin.offset ≥ 2`. -/ theorem compactReverseTruncatedSkip_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : 2 ≤ r.begin.offset) : Minimal r res (compactReverseTruncatedSkip r res) := by classical obtain ⟨db, hcomp⟩ := hread have hread' : Readable r res := ⟨db, hcomp⟩ unfold compactReverseTruncatedSkip by_cases hend : r.end_.offset ≤ 1 · rw [ite_eq_left hend] exact minimal_canonicalRange_of_end_offset_le_one_rev_more hread' hmore hrev hne hbegin hend · rw [ite_eq_right hend] have hend2 : 2 ≤ r.end_.offset := by omega by_cases hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key · rw [ite_eq_left hhi] exact minimal_topRange_of_end_offset_ge_two_rev_more hread' hmore hrev hbegin hend2 hhi · rw [ite_eq_right hhi] by_cases hcrossed : r.end_.key < r.begin.key · have hgap : intervalCard r.begin.key r.end_.key = 0 := intervalCard_eq_zero_of_le (le_of_lt hcrossed) have hfin_iff : compactReverseFinite r res ↔ res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat := by simp [compactReverseFinite, compactReverseCount, compactDeficit, residualD, residualB, residualE, hgap, hcrossed, Nat.add_comm] by_cases hfin : compactReverseFinite r res · have ht := hfin_iff.mp hfin rw [ite_eq_left hfin, ite_eq_left hcrossed] exact minimal_truncatedRevCrossed_finite_general hread' hmore hrev (by omega) hcrossed (by omega) hne ht · rw [ite_eq_right hfin] have hne_eq : res.keys.length + (r.begin.offset - 1).toNat ≠ (r.end_.offset - 1).toNat := fun h => hfin (hfin_iff.mpr h) have hle := crossed_rev_res_length_add_le_end (db := db) hcomp hrev (by omega) hcrossed (by omega) hne have hlt : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat := by omega have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne have hroom_iff : compactCrossedRoom r res ↔ crossedResidualCond r res hne := by unfold compactCrossedRoom crossedResidualCond residualE rw [hlast] by_cases hroom : compactCrossedRoom r res · rw [ite_eq_left ⟨hcrossed, hroom⟩] exact minimal_crossedResidual_cond hread' hmore hrev hbegin hcrossed (by omega) hne hlt (hroom_iff.mp hroom) · have hcond : ¬ (r.end_.key < r.begin.key ∧ compactCrossedRoom r res) := fun h => hroom h.2 rw [ite_eq_right hcond] exact minimal_crossedResidual_not_cond hread' hmore hrev hbegin hcrossed (by omega) hne hlt (fun h => hroom (hroom_iff.mpr h)) · have hcross : r.begin.key ≤ r.end_.key := not_lt.mp hcrossed have htpos : 0 < residualT r res := by simp only [not_forall, not_lt] at hhi obtain ⟨k, hk, hkge⟩ := hhi exact countP_pos_of_mem hk (by rw [decide_eq_true_eq]; exact hkge) have hfin_iff : compactReverseFinite r res ↔ residualD r (intervalCard r.begin.key r.end_.key) + residualT r res = residualE r := by simp [compactReverseFinite, compactReverseCount, compactDeficit, hcrossed] by_cases hfin : compactReverseFinite r res · have hfin' := hfin_iff.mp hfin rw [ite_eq_left hfin, ite_eq_right hcrossed] have hmin := minimal_closedForm_of_crossfree_residual hread' hmore hrev hbegin hcross hend2 hne htpos unfold closedFormCrossFreeResidual at hmin rw [ite_eq_left hfin'] at hmin exact hmin · rw [ite_eq_right hfin] have hcond : ¬ (r.end_.key < r.begin.key ∧ compactCrossedRoom r res) := fun h => hcrossed h.1 rw [ite_eq_right hcond] have hfin' : ¬ residualD r (intervalCard r.begin.key r.end_.key) + residualT r res = residualE r := fun h => hfin (hfin_iff.mpr h) have hmin := minimal_closedForm_of_crossfree_residual hread' hmore hrev hbegin hcross hend2 hne htpos unfold closedFormCrossFreeResidual at hmin rw [ite_eq_right hfin'] at hmin exact hmin /-- The compact reversed-truncated rule denotes exactly the general necessary-key hull. -/ theorem compactReverseTruncatedSkip_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : 2 ≤ r.begin.offset) : (compactReverseTruncatedSkip r res).toSet = (conflictRangeGeneral r res).toSet := (compactReverseTruncatedSkip_minimal hread hmore hrev hne hbegin).toSet_eq (conflictRangeGeneral_minimal hread) /-- The compact reversed-truncated rule agrees with the executable reference. -/ theorem compactReverseTruncatedSkip_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : 2 ≤ r.begin.offset) : (compactReverseTruncatedSkip r res).toSet = (simpleConflictRange r res).toSet := (compactReverseTruncatedSkip_minimal hread hmore hrev hne hbegin).toSet_eq (simpleConflictRange_minimal hread) /-- Reversed truncation with no begin skip. The finite branch uses the reported-head successor. On the unbounded branch, `[end.key, last reported)` must contain the unused end positions before the lower endpoint can move from the reported last to `end.key`. -/ def compactReverseTruncatedZero (r : RangeRead) (res : Result) : ConflictRange := if r.end_.offset ≤ 1 then truncatedRangeRev r (res.keys.getLastD ([] : Key)) else let L : WithTop Key := (res.keys.getLastD ([] : Key) : WithTop Key) let H : Key := res.keys.headD ([] : Key) if residualT r res = residualE r then ⟨min L r.end_.key, ((keyAfter H : Key) : WithTop Key)⟩ else match r.end_.key with | some E => if keyAfterN (residualE r - residualT r res - 1) E < res.keys.getLastD ([] : Key) then ⟨(E : WithTop Key), ⊤⟩ else ⟨L, ⊤⟩ | none => ⟨L, ⊤⟩ /-- The no-skip rule is the direct arithmetic form of the certified offset-one rule. -/ theorem compactReverseTruncatedZero_eq_offsetOne (r : RangeRead) (res : Result) : compactReverseTruncatedZero r res = closedFormOffsetOneTruncatedRevFullC r res := by rfl /-- One reversed-truncated algorithm at every positive begin offset. The only boundary distinction is whether the begin selector skips a positive number of keys. -/ noncomputable def compactReverseTruncated (r : RangeRead) (res : Result) : ConflictRange := if residualB r = 0 then compactReverseTruncatedZero r res else compactReverseTruncatedSkip r res /-- At begin offset one the total reversed rule is the no-skip formula. -/ theorem compactReverseTruncated_eq_offsetOne {r : RangeRead} {res : Result} (hbegin : r.begin.offset = 1) : compactReverseTruncated r res = closedFormOffsetOneTruncatedRevFullC r res := by simp [compactReverseTruncated, compactReverseTruncatedZero_eq_offsetOne, residualB, hbegin] /-- With a positive begin skip the total reversed rule is the selector-capacity formula. -/ theorem compactReverseTruncated_eq_skip {r : RangeRead} {res : Result} (hbegin : 2 ≤ r.begin.offset) : compactReverseTruncated r res = compactReverseTruncatedSkip r res := by have hb : residualB r ≠ 0 := by unfold residualB omega simp [compactReverseTruncated, hb] /-- The compact reversed-truncated rule is minimal at every positive begin offset. -/ theorem compactReverseTruncated_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : Minimal r res (compactReverseTruncated r res) := by by_cases hone : r.begin.offset = 1 · rw [compactReverseTruncated_eq_offsetOne hone] by_cases hcross : r.begin.key ≤ r.end_.key · exact minimal_closedForm_of_offset_one_truncated_rev_fullC hread hmore hrev hone hcross · have hcrossed : r.end_.key < r.begin.key := lt_of_not_ge hcross have hend : 2 ≤ r.end_.offset := crossed_end_offset_ge_two_truncated hread hmore (by omega) hcrossed exact minimal_closedForm_of_offset_one_truncated_rev_fullC_crossed hread hmore hrev hone hcrossed hend · rw [compactReverseTruncated_eq_skip (by omega)] exact compactReverseTruncatedSkip_minimal hread hmore hrev hne (by omega) /-- The total compact reversed rule denotes exactly the general necessary-key hull. -/ theorem compactReverseTruncated_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : (compactReverseTruncated r res).toSet = (conflictRangeGeneral r res).toSet := (compactReverseTruncated_minimal hread hmore hrev hne hbegin).toSet_eq (conflictRangeGeneral_minimal hread) /-- The total compact reversed rule agrees with the executable reference. -/ theorem compactReverseTruncated_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : (compactReverseTruncated r res).toSet = (simpleConflictRange r res).toSet := (compactReverseTruncated_minimal hread hmore hrev hne hbegin).toSet_eq (simpleConflictRange_minimal hread) /-- The shared result-shape dispatch of the compact algorithm: complete results use the normalized complete rule, forward truncations the forward rule, and reversed truncations the supplied traversal-specific rule. This is the common body of `compactPositive` and `compactClamped`, which differ only in their reversed-truncation rule. -/ noncomputable def compactSplit (revRule : RangeRead → Result → ConflictRange) (r : RangeRead) (res : Result) : ConflictRange := if res.more = false then compactComplete r res else if r.reverse = false then compactForwardTruncated r res else revRule r res /-- The compact algorithm for every readable result at a positive begin offset. Complete results are normalized to forward traversal. Forward and reversed truncation each have one total arithmetic rule; neither delegates to the regional dispatcher. -/ noncomputable def compactPositive (r : RangeRead) (res : Result) : ConflictRange := if res.keys = [] then emptyValue r else compactSplit compactReverseTruncated r res /-- The compact positive-offset algorithm is minimal for every readable result. -/ theorem compactPositive_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (hbegin : 1 ≤ r.begin.offset) : Minimal r res (compactPositive r res) := by classical unfold compactPositive by_cases hne : res.keys = [] · rw [ite_eq_left hne] exact minimal_emptyValue_of_emptyInference emptyInference hread hne · rw [ite_eq_right hne] unfold compactSplit by_cases hmore : res.more = false · rw [ite_eq_left hmore] exact compactComplete_minimal hread hmore hne hbegin · rw [ite_eq_right hmore] have hm : res.more = true := by cases h : res.more <;> simp_all by_cases hrev : r.reverse = false · rw [ite_eq_left hrev] exact compactForwardTruncated_minimal hread hm hrev hne hbegin · rw [ite_eq_right hrev] have hrevt : r.reverse = true := by cases h : r.reverse <;> simp_all exact compactReverseTruncated_minimal hread hm hrevt hne hbegin /-- The compact positive-offset algorithm denotes exactly the general necessary-key hull. -/ theorem compactPositive_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hbegin : 1 ≤ r.begin.offset) : (compactPositive r res).toSet = (conflictRangeGeneral r res).toSet := (compactPositive_minimal hread hbegin).toSet_eq (conflictRangeGeneral_minimal hread) /-- The compact positive-offset algorithm agrees with the executable reference. -/ theorem compactPositive_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hbegin : 1 ≤ r.begin.offset) : (compactPositive r res).toSet = (simpleConflictRange r res).toSet := (compactPositive_minimal hread hbegin).toSet_eq (simpleConflictRange_minimal hread) /-- A nonempty read whose nonpositive begin selector is at or below the least reported key. These reads clamp to the same bottom-anchored core regardless of selector crossing. -/ abbrev clampedNonpos (r : RangeRead) (res : Result) : Prop := res.keys ≠ [] ∧ r.begin.offset ≤ 0 ∧ r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) /-- One algorithm for all clamped nonpositive reads. This single rule subsumes the old complete, forward-truncated positive-end, forward-truncated nonpositive-end, and reversed truncated dispatcher branches. -/ noncomputable def compactClamped (r : RangeRead) (res : Result) : ConflictRange := compactSplit closedFormNonposRevTrunc r res /-- The clamped nonpositive algorithm is minimal in either traversal and for either result completeness flag. -/ theorem compactClamped_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (h : clampedNonpos r res) : Minimal r res (compactClamped r res) := by obtain ⟨hne, hbegin, hloD⟩ := h unfold compactClamped compactSplit by_cases hmore : res.more = false · rw [ite_eq_left hmore] exact compactComplete_minimal_nonpos hread ⟨hmore, hne, hbegin, hloD⟩ · rw [ite_eq_right hmore] have hm : res.more = true := by cases h : res.more <;> simp_all by_cases hrev : r.reverse = false · rw [ite_eq_left hrev] have hlo : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by simpa [leastReported, hrev] using hloD exact compactForwardTruncated_minimal_nonpos hread hm hrev hbegin hlo · rw [ite_eq_right hrev] have hrevt : r.reverse = true := by cases h : r.reverse <;> simp_all exact minimal_closedForm_of_nonpos_revTrunc hread ⟨hm, hrevt, hbegin, hloD⟩ /-- The clamped nonpositive algorithm denotes exactly the general necessary-key hull. -/ theorem compactClamped_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (h : clampedNonpos r res) : (compactClamped r res).toSet = (conflictRangeGeneral r res).toSet := (compactClamped_minimal hread h).toSet_eq (conflictRangeGeneral_minimal hread) /-- The clamped nonpositive algorithm agrees with the executable reference. -/ theorem compactClamped_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (h : clampedNonpos r res) : (compactClamped r res).toSet = (simpleConflictRange r res).toSet := (compactClamped_minimal hread h).toSet_eq (simpleConflictRange_minimal hread) /-- The compact complete algorithm denotes exactly the general necessary-key hull. -/ theorem compactComplete_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : (compactComplete r res).toSet = (conflictRangeGeneral r res).toSet := (compactComplete_minimal hread hmore hne hbegin).toSet_eq (conflictRangeGeneral_minimal hread) /-- The compact complete algorithm agrees with the executable reference algorithm. -/ theorem compactComplete_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) : (compactComplete r res).toSet = (simpleConflictRange r res).toSet := (compactComplete_minimal hread hmore hne hbegin).toSet_eq (simpleConflictRange_minimal hread) /-! ## Totality of the compact algorithm The rules above certify every nonempty readable result at a positive begin offset, every clamped nonpositive read, and every nonpositive forward-truncated read outside the explicit crossed nonpositive-end residual `compactForwardNonposResidual`. The remaining nonpositive cells — crossed complete results, crossed reversed truncation, and the forward residual — are exposed as `compactResidual`; they are answered by the certified total closed-form dispatcher `conflictRangeClosedForm`, so the compact body itself never mentions the executable reference. The dispatcher's own innermost fallback is the only remaining executable call, and it is confined to a handful of crossed cells. -/ /-! ## A genuinely closed total dispatcher The certified dispatcher `conflictRangeClosedForm` has one remaining fallback to the general algorithm on the crossed nonpositive truncated cells. On the compact residual those cells are unreachable, and on the remaining fallback cells the read is a forward truncation, whose compact rule `compactForwardTruncatedNonpos` is already certified. The dispatcher below replaces that fallback by the compact forward rule, so it contains no reference to the general or executable algorithm, and it is proved minimal on every readable pair. -/ /-- The shared unbounded-upper closed form of the crossed nonpositive complete overshoot cells. The lower end is `⊥` exactly in the clamping regime and the reported least key otherwise; both the crossed begin-zero overshoot and the crossed negative-begin overshoot have this endpoint pair, so one definition serves both. -/ noncomputable def closedFormNegCrossTopUnified (r : RangeRead) (res : Result) : ConflictRange := ⟨if reportedBelowBegin r res ≤ beginSlack r then (([] : Key) : WithTop Key) else ((leastReported r res : Key) : WithTop Key), ⊤⟩ /-- In the crossed begin-zero overshoot cell the clamping test is vacuous, so the shared top closed form is the certified begin-zero rule. -/ theorem closedFormNegCrossTopUnified_eq_zeroCross {r : RangeRead} {res : Result} (h : zeroCrossOvershootComplete r res) : closedFormNegCrossTopUnified r res = closedFormZeroCrossOvershoot r res := by obtain ⟨hb, _hmore, hrev, hov, hcross⟩ := h obtain ⟨_hend, hne, _hbelow⟩ := hov have hslack : beginSlack r = 0 := by simp [beginSlack, hb] have hmem : leastReported r res ∈ res.keys := by have hL : leastReported r res = res.keys.head hne := by simp only [leastReported, hrev] exact headD_eq_head_of_ne hne rw [hL] exact List.head_mem hne have hpos : 0 < reportedBelowBegin r res := by unfold reportedBelowBegin refine countP_pos_of_mem hmem ?_ rw [decide_eq_true_eq] exact hcross have hcond : ¬ reportedBelowBegin r res ≤ beginSlack r := by omega simp [closedFormNegCrossTopUnified, closedFormZeroCrossOvershoot, topOfLast, hcond] /-- In the crossed negative-begin overshoot cell the shared top closed form is the certified negative-begin rule. -/ theorem closedFormNegCrossTopUnified_eq_negCross {r : RangeRead} {res : Result} (h : negCrossOvershoot r res) : closedFormNegCrossTopUnified r res = closedFormNegCrossOvershoot r res := by obtain ⟨hrev, _hbeg, _hmore, _hne, _hend, _hbelow, _hcross⟩ := h unfold closedFormNegCrossTopUnified closedFormNegCrossOvershoot by_cases hcond : reportedBelowBegin r res ≤ beginSlack r · simp [hcond] · simp [hcond, topOfLast, leastReported, hrev] /-- The shared closed form of the crossed nonpositive complete nonclamping cells: the reported head up to the larger selector key. The low and offset-one `end` cells both end at `end_.key`, and the long `end` cells end at `begin_.key`, so one endpoint pair serves all five. -/ noncomputable def closedFormNegCrossNonclampUnified (r : RangeRead) (res : Result) : ConflictRange := ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), max r.end_.key r.begin.key⟩ /-- On the non-long cells the shared form is the certified `[head, end_.key)` rule. -/ theorem closedFormNegCrossNonclampUnified_eq_of_le {r : RangeRead} {res : Result} (h : r.begin.key ≤ r.end_.key) : closedFormNegCrossNonclampUnified r res = (⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by unfold closedFormNegCrossNonclampUnified rw [max_eq_left h] /-- On the long cells the shared form is the certified `[head, begin_.key)` rule. -/ theorem closedFormNegCrossNonclampUnified_eq_of_lt {r : RangeRead} {res : Result} (h : r.end_.key < r.begin.key) : closedFormNegCrossNonclampUnified r res = (⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) := by unfold closedFormNegCrossNonclampUnified rw [max_eq_right (le_of_lt h)] /-- On a readable nonempty complete forward crossed read at begin offset zero with a nonpositive end offset, the begin selector key lies at or below the end selector key. Otherwise the end resolution, a stored key below `end_.key`, would lie below `begin.key` and hence at or below the begin resolution, contradicting the nonempty complete window. -/ theorem zeroCrossEndNonpos_begin_le_end {r : RangeRead} {res : Result} (hread : Readable r res) (h : zeroCrossEndNonpos r res) : r.begin.key ≤ r.end_.key := by obtain ⟨hbegin, hmore, hrev, hend, hne, hcrossD⟩ := h obtain ⟨db, hcomp⟩ := hread have hleast : leastReported r res = res.keys.head hne := by simp only [leastReported, hrev] exact headD_eq_head_of_ne hne have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by rw [← hleast]; exact hcrossD have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne have hhead_stored : res.keys.head hne ∈ db.keys := RangeRead.compatible_keys_subset hcomp hhead_mem have hres_eq : res.keys = RangeRead.completeKeys db r := by rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ · simp [hmore] at hm · exact heq have hhead_ck := RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem) have hhead_lt_res : ((res.keys.head hne : Key) : WithTop Key) < resolve db r.end_ := hhead_ck.2.2 have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by apply le_antisymm · exact hhead_ck.2.1 · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross (res.keys.head hne) hhead_stored hcross have hres_le_E : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend by_contra hcon have hEB : r.end_.key < r.begin.key := lt_of_not_ge hcon rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hn | ht | ⟨k, hkdb, hkres⟩ · rw [hn] at hhead_lt_res exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _)) · exact absurd ht (resolve_ne_top_of_offset_nonpos hend) · have hk_lt_B : (k : WithTop Key) < r.begin.key := lt_of_le_of_lt (by rw [← hkres]; exact hres_le_E) hEB have hk_le_head : (k : WithTop Key) ≤ ((res.keys.head hne : Key) : WithTop Key) := by have hb := resolve_zero_begin_le_of_mem_below (db := db) hbegin hhead_stored hcross k hkdb hk_lt_B rwa [hL] at hb have hhead_lt_k : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) := by rw [← hkres]; exact hhead_lt_res exact absurd hk_le_head (not_le.mpr hhead_lt_k) /-- The crossed begin-zero complete under-end cells with `begin.key ≤ end_.key` share the certified `[head, end_.key)` nonclamping form. -/ theorem closedFormNegCrossNonclampUnified_eq_zeroCrossEndOne {r : RangeRead} {res : Result} (h : zeroCrossEndOne r res) : closedFormNegCrossNonclampUnified r res = closedFormZeroCrossEndOne r res := by obtain ⟨-, -, hrev, -, -, -, hle⟩ := h rw [closedFormNegCrossNonclampUnified_eq_of_le hle] simp [closedFormZeroCrossEndOne, leastReported, hrev] /-- The crossed begin-zero complete nonpositive-end cell also has `begin.key ≤ end_.key`, so it shares the same `[head, end_.key)` form. -/ theorem closedFormNegCrossNonclampUnified_eq_zeroCrossEndNonpos {r : RangeRead} {res : Result} (hread : Readable r res) (h : zeroCrossEndNonpos r res) : closedFormNegCrossNonclampUnified r res = closedFormZeroCrossEndNonpos r res := by have hrev : r.reverse = false := h.2.2.1 rw [closedFormNegCrossNonclampUnified_eq_of_le (zeroCrossEndNonpos_begin_le_end hread h)] simp [closedFormZeroCrossEndNonpos, leastReported, hrev] /-- The crossed begin-zero complete long under-end cell has `end_.key < begin.key`, so it shares the `[head, begin_.key)` form. -/ theorem closedFormNegCrossNonclampUnified_eq_zeroCrossEndOneLong {r : RangeRead} {res : Result} (h : zeroCrossEndOneLong r res) : closedFormNegCrossNonclampUnified r res = closedFormZeroCrossEndOneLong r res := by obtain ⟨-, -, hrev, -, -, -, hlt⟩ := h rw [closedFormNegCrossNonclampUnified_eq_of_lt hlt] simp [closedFormZeroCrossEndOneLong, leastReported, hrev] /-- The crossed nonpositive complete clamping cells all have lower endpoint `⊥`. The offset-one cell can raise the upper endpoint to `begin.key`; the nonpositive-end bottom group ends at `end_.key`; and the long nonpositive cell can raise it to `begin.key`. The checks are ordered as in the dispatcher. -/ noncomputable def closedFormNegCrossClampUnified (r : RangeRead) (res : Result) : ConflictRange := ⟨(([] : Key) : WithTop Key), if negCrossClampEndOne r res then clampEndOneUpper r res else if (negCrossHeadBottom r res ∨ negCrossClampNonposInfinite r res ∨ negCrossClampNonposLastGe r res ∨ negCrossClampNonposBottom r res) then r.end_.key else if negCrossClampLongNonpos r res then clampLongUpper r res else r.end_.key⟩ /-- In the clamp end-one cell the shared form is the certified rule. -/ theorem closedFormNegCrossClampUnified_eq_endOne {r : RangeRead} {res : Result} (h : negCrossClampEndOne r res) : closedFormNegCrossClampUnified r res = closedFormNegCrossClampEndOne r res := by unfold closedFormNegCrossClampUnified closedFormNegCrossClampEndOne rw [ite_eq_left h] /-- In the bottom group the shared form is the certified `[⊥, end_.key)` rule. -/ theorem closedFormNegCrossClampUnified_eq_bottom {r : RangeRead} {res : Result} (h : negCrossHeadBottom r res ∨ negCrossClampNonposInfinite r res ∨ negCrossClampNonposLastGe r res ∨ negCrossClampNonposBottom r res) : closedFormNegCrossClampUnified r res = closedFormNegCrossHeadBottom r res := by have hend : r.end_.offset ≤ 0 := by rcases h with h | h | h | h · exact h.2.2.2.2.1 · exact h.2.2.2.2.1 · exact h.2.2.2.2.1 · exact h.2.2.2.2.1 have hnot : ¬ negCrossClampEndOne r res := by rintro he unfold negCrossClampEndOne at he omega unfold closedFormNegCrossClampUnified closedFormNegCrossHeadBottom rw [ite_eq_right hnot, ite_eq_left h] /-- In the long nonpositive cell the shared form is the certified rule. -/ theorem closedFormNegCrossClampUnified_eq_long {r : RangeRead} {res : Result} (hbot : ¬ (negCrossHeadBottom r res ∨ negCrossClampNonposInfinite r res ∨ negCrossClampNonposLastGe r res ∨ negCrossClampNonposBottom r res)) (h : negCrossClampLongNonpos r res) : closedFormNegCrossClampUnified r res = closedFormNegCrossClampLongNonpos r res := by have hnot : ¬ negCrossClampEndOne r res := by rintro he unfold negCrossClampEndOne at he have hend : r.end_.offset ≤ 0 := h.2.2.2.2.1 omega unfold closedFormNegCrossClampUnified closedFormNegCrossClampLongNonpos rw [ite_eq_right hnot, ite_eq_right hbot, ite_eq_left h] /-- The crossed complete begin-zero residual cells (`end_.offset ≥ 2`). The under-end and long cells are the two sides of `begin.key ≤ end_.key` versus `end_.key < begin.key`; the under-end check comes first, matching the dispatcher. -/ noncomputable def closedFormZeroCrossResidual (r : RangeRead) (res : Result) : ConflictRange := if zeroCrossUnderEnd r res then closedFormZeroCrossUnderEnd r res else closedFormZeroCrossLong r res /-- In the under-end cell the shared form is the certified anchor rule. -/ theorem closedFormZeroCrossResidual_eq_under {r : RangeRead} {res : Result} (h : zeroCrossUnderEnd r res) : closedFormZeroCrossResidual r res = closedFormZeroCrossUnderEnd r res := by unfold closedFormZeroCrossResidual rw [ite_eq_left h] /-- In the long cells the shared form is the certified long rule. -/ theorem closedFormZeroCrossResidual_eq_long {r : RangeRead} {res : Result} (hnot : ¬ zeroCrossUnderEnd r res) (_h : zeroCrossLongTop r res ∨ zeroCrossLongFinite r res) : closedFormZeroCrossResidual r res = closedFormZeroCrossLong r res := by unfold closedFormZeroCrossResidual rw [ite_eq_right hnot] /-- The crossed negative-begin complete cells with `2 ≤ end_.offset`: the high cell and the residual. The high check comes first, matching the dispatcher. -/ noncomputable def closedFormNegCrossCompleteHighResidual (r : RangeRead) (res : Result) : ConflictRange := if negCrossCompleteHigh r res then closedFormNegCrossCompleteHigh r res else closedFormNegCrossCompleteResidual r res /-- In the high cell the shared form is the certified high rule. -/ theorem closedFormNegCrossCompleteHighResidual_eq_high {r : RangeRead} {res : Result} (h : negCrossCompleteHigh r res) : closedFormNegCrossCompleteHighResidual r res = closedFormNegCrossCompleteHigh r res := by unfold closedFormNegCrossCompleteHighResidual rw [ite_eq_left h] /-- In the residual cell the shared form is the certified residual rule. -/ theorem closedFormNegCrossCompleteHighResidual_eq_residual {r : RangeRead} {res : Result} (h : ¬ negCrossCompleteHigh r res) : closedFormNegCrossCompleteHighResidual r res = closedFormNegCrossCompleteResidual r res := by unfold closedFormNegCrossCompleteHighResidual rw [ite_eq_right h] /-- The eight low-end crossed complete cells (`end_.offset ≤ 1`) share the closed form `[head, max end begin)`. Their union has a compact description: at `end_.offset = 1` the begin selector must be non-clamping, and at `end_.offset ≤ 0` the only excluded subcase is the clamping bottom cell. -/ abbrev crossedCompleteEndLeOne (r : RangeRead) (res : Result) : Prop := r.reverse = false ∧ res.more = false ∧ res.keys ≠ [] ∧ r.begin.offset ≤ 0 ∧ ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ ((r.end_.offset = 1 ∧ beginSlack r < reportedBelowBegin r res) ∨ (r.end_.offset ≤ 0 ∧ ¬ (reportedBelowBegin r res ≤ beginSlack r ∧ clampNonposBottom r res))) /-- The compact low-end predicate is exactly the union of the eight routed cells. -/ theorem crossedCompleteEndLeOne_iff (r : RangeRead) (res : Result) : crossedCompleteEndLeOne r res ↔ (zeroCrossEndOne r res ∨ zeroCrossEndNonpos r res ∨ zeroCrossEndOneLong r res ∨ negCrossLowFwd r res ∨ negCrossEndOne r res ∨ negCrossNonclampNonpos r res ∨ negCrossEndOneLong r res ∨ negCrossNonclampLongNonpos r res) := by classical constructor · rintro ⟨hrev, hmore, hne, hbeginle, hcross, h1 | h1⟩ · obtain ⟨hend1, hslack⟩ := h1 by_cases hb0 : r.begin.offset = 0 · by_cases hBE : r.begin.key ≤ r.end_.key · exact Or.inl ⟨hb0, hmore, hrev, hend1, hne, by simpa [leastReported, hrev] using hcross, hBE⟩ · exact Or.inr (Or.inr (Or.inl ⟨hb0, hmore, hrev, hend1, hne, by simpa [leastReported, hrev] using hcross, not_le.mp hBE⟩)) · have hbneg : r.begin.offset < 0 := by omega by_cases hBE : r.begin.key ≤ r.end_.key · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨hrev, hbneg, hmore, hne, hend1, hcross, hslack, hBE⟩)))) · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨hrev, hbneg, hmore, hne, hend1, hcross, hslack, not_le.mp hBE⟩)))))) · obtain ⟨hend0, hnot⟩ := h1 by_cases hb0 : r.begin.offset = 0 · exact Or.inr (Or.inl ⟨hb0, hmore, hrev, hend0, hne, by simpa [leastReported, hrev] using hcross⟩) · have hbneg : r.begin.offset < 0 := by omega by_cases hslack : beginSlack r < reportedBelowBegin r res · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨hrev, hbneg, hmore, hne, hend0, hcross, hslack⟩))) · have hle : reportedBelowBegin r res ≤ beginSlack r := by omega have hnb : ¬ clampNonposBottom r res := fun hc => hnot ⟨hle, hc⟩ by_cases hBE : r.begin.key ≤ r.end_.key · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨hrev, hbneg, hmore, hne, hend0, hBE, hcross, hle, hnb⟩))))) · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨hrev, hbneg, hmore, hne, hend0, hcross, hle, hnb, not_le.mp hBE⟩)))))) · rintro (h | h | h | h | h | h | h | h) · obtain ⟨hb0, hmore, hrev, hend1, hne, hcrossL, hBE⟩ := h have hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcrossL have hnb : ¬ reportedBelowBegin r res ≤ beginSlack r := not_clamp_of_zero_cross hne hb0 hcross exact ⟨hrev, hmore, hne, by omega, hcross, Or.inl ⟨hend1, by omega⟩⟩ · obtain ⟨hb0, hmore, hrev, hend0, hne, hcrossL⟩ := h have hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcrossL have hnb : ¬ reportedBelowBegin r res ≤ beginSlack r := not_clamp_of_zero_cross hne hb0 hcross exact ⟨hrev, hmore, hne, by omega, hcross, Or.inr ⟨hend0, fun hc => hnb hc.1⟩⟩ · obtain ⟨hb0, hmore, hrev, hend1, hne, hcrossL, hlt⟩ := h have hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcrossL have hnb : ¬ reportedBelowBegin r res ≤ beginSlack r := not_clamp_of_zero_cross hne hb0 hcross exact ⟨hrev, hmore, hne, by omega, hcross, Or.inl ⟨hend1, by omega⟩⟩ · obtain ⟨hrev, hbneg, hmore, hne, hend0, hcross, hslack⟩ := h exact ⟨hrev, hmore, hne, by omega, hcross, Or.inr ⟨hend0, fun hc => by omega⟩⟩ · obtain ⟨hrev, hbneg, hmore, hne, hend1, hcross, hslack, hBE⟩ := h exact ⟨hrev, hmore, hne, by omega, hcross, Or.inl ⟨hend1, hslack⟩⟩ · obtain ⟨hrev, hbneg, hmore, hne, hend0, hBE, hcross, hle, hnb⟩ := h exact ⟨hrev, hmore, hne, by omega, hcross, Or.inr ⟨hend0, fun hc => hnb hc.2⟩⟩ · obtain ⟨hrev, hbneg, hmore, hne, hend1, hcross, hslack, hlt⟩ := h exact ⟨hrev, hmore, hne, by omega, hcross, Or.inl ⟨hend1, hslack⟩⟩ · obtain ⟨hrev, hbneg, hmore, hne, hend0, hcross, hle, hnb, hlt⟩ := h exact ⟨hrev, hmore, hne, by omega, hcross, Or.inr ⟨hend0, fun hc => hnb hc.2⟩⟩ /-- The crossed complete overshoot cells: every reported key lies below the end key at `end_.offset ≥ 2`. -/ abbrev crossedCompleteOvershoot (r : RangeRead) (res : Result) : Prop := zeroCrossOvershootComplete r res ∨ negCrossOvershoot r res /-- The crossed complete begin-zero non-overshoot cells at `end_.offset ≥ 2`. -/ abbrev crossedCompleteBeginZero (r : RangeRead) (res : Result) : Prop := zeroCrossUnderEnd r res ∨ zeroCrossLongTop r res ∨ zeroCrossLongFinite r res /-- The crossed complete clamping cells at `end_.offset ≤ 1`. -/ abbrev crossedCompleteClamp (r : RangeRead) (res : Result) : Prop := negCrossClampEndOne r res ∨ negCrossHeadBottom r res ∨ negCrossClampNonposInfinite r res ∨ negCrossClampNonposLastGe r res ∨ negCrossClampNonposBottom r res ∨ negCrossClampLongNonpos r res open Classical in /-- The complete-result part of the crossed closed form, with the read in forward traversal; the reversed case reuses it on the flipped read. The five rows are the crossed complete cells: overshoot, the low end (`end_.offset ≤ 1`), the begin-zero non-overshoot, the clamping cells, and the high / residual fallback. The fallback is unreachable on readable crossed nonpositive complete forward reads (`nonposCompleteForward_false`). -/ noncomputable def compactCrossedComplete (r : RangeRead) (res : Result) : ConflictRange := if crossedCompleteOvershoot r res then closedFormNegCrossTopUnified r res else if crossedCompleteEndLeOne r res then closedFormNegCrossNonclampUnified r res else if crossedCompleteBeginZero r res then closedFormZeroCrossResidual r res else if crossedCompleteClamp r res then closedFormNegCrossClampUnified r res else closedFormNegCrossCompleteHighResidual r res /-- The legacy name for the complete-result part, kept as the proof-side reference for the multi-row dispatcher. -/ noncomputable def conflictRangeClosedFormResidualCompleteFwd (r : RangeRead) (res : Result) : ConflictRange := compactCrossedComplete r res /-- The sentinel-begin cell shares the large-capacity crossed closed form: its begin key is `⊤`, so `[last, begin.key)` and `[last, ⊤)` coincide. -/ theorem closedFormNegCrossTruncRevCrossedNonposLarge_eq_beginTop {r : RangeRead} {res : Result} (h : negCrossTruncRevBeginTop r res) : closedFormNegCrossTruncRevCrossedNonposLarge r res = closedFormNegCrossTruncRevBeginTop r res := by obtain ⟨_, _, _, _, _, hBtop, _⟩ := h unfold closedFormNegCrossTruncRevCrossedNonposLarge closedFormNegCrossTruncRevBeginTop topOfLast rw [hBtop] /-- The finite under and over cells of the reversed residual share their upper endpoint; only the lower endpoint moves from the reported last to `end_.key`. -/ noncomputable def closedFormNegCrossTruncRevResidualFiniteUnified (r : RangeRead) (res : Result) : ConflictRange := if negCrossTruncRevResidualFiniteOver r res then ⟨r.end_.key, (closedFormNegCrossTruncRevResidualFiniteUnder r res).end_⟩ else closedFormNegCrossTruncRevResidualFiniteUnder r res /-- In the finite under cell the shared finite closed form is the certified under rule. -/ theorem closedFormNegCrossTruncRevResidualFiniteUnified_eq_under {r : RangeRead} {res : Result} (h : negCrossTruncRevResidualFiniteUnder r res) : closedFormNegCrossTruncRevResidualFiniteUnified r res = closedFormNegCrossTruncRevResidualFiniteUnder r res := by have hover : ¬ negCrossTruncRevResidualFiniteOver r res := by rintro ⟨_, _, _, _, _, hEL, _⟩ exact absurd hEL (not_lt.mpr h.2.2.2.2.2.1) unfold closedFormNegCrossTruncRevResidualFiniteUnified rw [ite_eq_right hover] /-- In the finite over cell the shared finite closed form is the certified over rule. -/ theorem closedFormNegCrossTruncRevResidualFiniteUnified_eq_over {r : RangeRead} {res : Result} (h : negCrossTruncRevResidualFiniteOver r res) : closedFormNegCrossTruncRevResidualFiniteUnified r res = closedFormNegCrossTruncRevResidualFiniteOver r res := by unfold closedFormNegCrossTruncRevResidualFiniteUnified closedFormNegCrossTruncRevResidualFiniteOver closedFormNegCrossTruncRevResidualFiniteUnder rw [ite_eq_left h] /-- The two large-gap top subcells share the unbounded upper endpoint; only the sentinel-begin cell can lower the lower endpoint to the reported last. -/ noncomputable def closedFormNegCrossTruncRevResidualTopLargeUnified (r : RangeRead) (res : Result) : ConflictRange := by classical exact if negCrossTruncRevResidualTopLargeSentinel r res then closedFormNegCrossTruncRevResidualTopLargeSentinel r res else ⟨r.end_.key, ⊤⟩ /-- In the crossed large-gap subcell the shared form is the certified `[end_.key, ⊤)` rule. -/ theorem closedFormNegCrossTruncRevResidualTopLargeUnified_eq_crossed {r : RangeRead} {res : Result} (h : negCrossTruncRevResidualTopLargeCrossed r res) : closedFormNegCrossTruncRevResidualTopLargeUnified r res = closedFormNegCrossTruncRevResidualTopLargeCrossed r res := by have hnot : ¬ negCrossTruncRevResidualTopLargeSentinel r res := by rintro ⟨-, htop⟩ exact h.2.2 htop unfold closedFormNegCrossTruncRevResidualTopLargeUnified closedFormNegCrossTruncRevResidualTopLargeCrossed rw [ite_eq_right hnot] /-- In the sentinel-begin large-gap subcell the shared form is the certified conditional rule. -/ theorem closedFormNegCrossTruncRevResidualTopLargeUnified_eq_sentinel {r : RangeRead} {res : Result} (h : negCrossTruncRevResidualTopLargeSentinel r res) : closedFormNegCrossTruncRevResidualTopLargeUnified r res = closedFormNegCrossTruncRevResidualTopLargeSentinel r res := by unfold closedFormNegCrossTruncRevResidualTopLargeUnified rw [ite_eq_left h] /-- The low reversed-truncated cells at `end_.offset ≤ 1`: the finite under-end range `[last, end_.key)` (`Under`, `CrossedNonposSmall` and `CrossedSmall` are the same range definitionally). -/ abbrev revTruncLeOneLow (r : RangeRead) (res : Result) : Prop := negCrossTruncRevUnder r res ∨ negCrossTruncRevCrossedNonposSmall r res ∨ negCrossTruncRevCrossedSmall r res /-- The high reversed-truncated cells at `end_.offset ≤ 1`: the large-capacity range `[last, begin.key)` (`CrossedNonposLarge` and `CrossedLarge` are the same range; the sentinel-begin cell joins because its begin key is `⊤`). -/ abbrev revTruncLeOneHigh (r : RangeRead) (res : Result) : Prop := negCrossTruncRevBeginTop r res ∨ negCrossTruncRevCrossedNonposLarge r res ∨ negCrossTruncRevCrossedLarge r res /-- The reversed-truncated cells with end offset at most one. The nonpositive and offset-one groups share their closed forms definitionally, so they are merged into one table. -/ noncomputable def conflictRangeClosedFormResidualRevTruncLeOne (r : RangeRead) (res : Result) : ConflictRange := if revTruncLeOneLow r res then closedFormNegCrossTruncRevUnder r res else closedFormNegCrossTruncRevCrossedNonposLarge r res /-- The top reversed-truncated cells at `end_.offset ≥ 2`: the unbounded range `[last, ⊤)` (below-end first). -/ abbrev revTruncGeTwoTop (r : RangeRead) (res : Result) : Prop := negCrossTruncRevBelowEnd r res ∨ negCrossTruncRevResidualRoom r res ∨ negCrossTruncRevResidualTopCrossedSmallGap r res ∨ negCrossTruncRevResidualTopCrossed r res /-- The large-gap reversed-truncated cells at `end_.offset ≥ 2`. -/ abbrev revTruncGeTwoLarge (r : RangeRead) (res : Result) : Prop := negCrossTruncRevResidualTopLargeCrossed r res ∨ negCrossTruncRevResidualTopLargeSentinel r res /-- The reversed-truncated cells with end offset at least two. -/ noncomputable def conflictRangeClosedFormResidualRevTruncGeTwo (r : RangeRead) (res : Result) : ConflictRange := if revTruncGeTwoTop r res then closedFormNegCrossTruncRevBelowEnd r res else if revTruncGeTwoLarge r res then closedFormNegCrossTruncRevResidualTopLargeUnified r res else closedFormNegCrossTruncRevResidualFiniteUnified r res /-- The reversed-truncated part of the residual dispatcher, factored by the end offset. The begin-zero crossed cell is checked first because it is offset-agnostic; the remaining cells are grouped by `end_.offset ≤ 1` and `2 ≤ end_.offset`. The fallback is unreachable on readable crossed nonpositive reversed-truncated reads (`nonposTruncRev_false`). -/ noncomputable def conflictRangeClosedFormResidualRevTrunc (r : RangeRead) (res : Result) : ConflictRange := if zeroCrossTruncRevCrossed r res then closedFormZeroCrossTruncRevCrossed r res else if r.end_.offset ≤ 1 then conflictRangeClosedFormResidualRevTruncLeOne r res else conflictRangeClosedFormResidualRevTruncGeTwo r res /-- The multi-row residual dispatcher, kept as the proof-side reference for the compact closed form. The algorithm itself uses `compactCrossed`, whose body is this dispatcher with the three sub-dispatchers inlined. -/ noncomputable def conflictRangeClosedFormResidualAux (r : RangeRead) (res : Result) : ConflictRange := if res.more = false then if r.reverse = false then conflictRangeClosedFormResidualCompleteFwd r res else conflictRangeClosedFormResidualCompleteFwd { r with reverse := false } { keys := res.keys.reverse, more := false } else if r.reverse = true then conflictRangeClosedFormResidualRevTrunc r res else compactForwardTruncatedNonpos r res open Classical in /-- **The crossed nonpositive closed form, as a single self-contained function.** The complete cells come from the shared `compactCrossedComplete` rule, used directly on a forward read and on the flipped read for a reversed complete result. The reversed truncation is one flat priority chain: the offset-agnostic begin-zero cell first, then the `end_.offset ≤ 1` cells, then the `2 ≤ end_.offset` cells. The two groups are disjoint because every `LeOne` cell carries `end_.offset ≤ 1` and every `GeTwo` cell carries `2 ≤ end_.offset`, so no offset test is needed between them. The forward truncation falls back to the certified compact rule. No branch calls the general or executable algorithm. -/ noncomputable def compactCrossed (r : RangeRead) (res : Result) : ConflictRange := if res.more = false then if r.reverse = false then compactCrossedComplete r res else compactCrossedComplete { r with reverse := false } { keys := res.keys.reverse, more := false } else if r.reverse = true then if zeroCrossTruncRevCrossed r res then closedFormZeroCrossTruncRevCrossed r res else if revTruncLeOneLow r res then closedFormNegCrossTruncRevUnder r res else if r.end_.offset ≤ 1 then closedFormNegCrossTruncRevCrossedNonposLarge r res else if revTruncGeTwoTop r res then closedFormNegCrossTruncRevBelowEnd r res else if revTruncGeTwoLarge r res then closedFormNegCrossTruncRevResidualTopLargeUnified r res else closedFormNegCrossTruncRevResidualFiniteUnified r res else compactForwardTruncatedNonpos r res /-- The crossed nonpositive closed form is the single entry point: the legacy name `conflictRangeClosedFormResidual` is now a one-line alias. -/ noncomputable def conflictRangeClosedFormResidual (r : RangeRead) (res : Result) : ConflictRange := compactCrossed r res /-- The single closed form is definitionally the legacy residual name. -/ theorem compactCrossed_eq_residual (r : RangeRead) (res : Result) : compactCrossed r res = conflictRangeClosedFormResidual r res := rfl /-- The single closed form agrees with the multi-row proof-side dispatcher. The complete and forward-truncated branches are definitional; on the reversed-truncated branch the flat chain agrees with the offset-split tables because every `LeOne` cell carries `end_.offset ≤ 1` and every `GeTwo` cell carries `2 ≤ end_.offset`. -/ theorem compactCrossed_eq_residualAux (r : RangeRead) (res : Result) : compactCrossed r res = conflictRangeClosedFormResidualAux r res := by classical unfold compactCrossed conflictRangeClosedFormResidualAux by_cases hmore : res.more = false · simp only [ite_eq_left hmore] by_cases hrev : r.reverse = false · simp only [ite_eq_left hrev] rfl · simp only [ite_eq_right hrev] rfl · simp only [ite_eq_right hmore] by_cases hrev : r.reverse = true · simp only [ite_eq_left hrev] unfold conflictRangeClosedFormResidualRevTrunc conflictRangeClosedFormResidualRevTruncLeOne conflictRangeClosedFormResidualRevTruncGeTwo by_cases hz : zeroCrossTruncRevCrossed r res · simp only [ite_eq_left hz] · simp only [ite_eq_right hz] by_cases hle : r.end_.offset ≤ 1 · simp only [ite_eq_left hle] · simp only [ite_eq_right hle] have hlow : ¬ revTruncLeOneLow r res := by rintro (h | h | h) · exact absurd h.2.2.2.2.2.2 hle · exact absurd h.2.2.2.2.2.2.1 (by omega) · exact absurd (le_of_eq h.2.2.2.2.2.2.1) hle simp only [ite_eq_right hlow] · simp only [ite_eq_right hrev] /-- The genuinely closed total dispatcher. It factors through the compact top-level split: the empty result, the positive-offset algorithm, and the clamped nonpositive algorithm are each certified separately, and the remaining crossed complete, reversed-truncated, and forward-truncated cells are answered by the single closed form `compactCrossed` (whose own fallback is the certified compact forward-truncation rule). No branch calls the general or executable algorithm. -/ noncomputable def conflictRangeClosedFormClosed (r : RangeRead) (res : Result) : ConflictRange := if res.keys = [] then emptyValue r else if 1 ≤ r.begin.offset then compactPositive r res else if clampedNonpos r res then compactClamped r res else compactCrossed r res /-- Coverage of the nonpositive-begin crossed complete forward cell by the closed-form dispatcher predicates: if all the corresponding predicates fail, there is no readable forward complete result. -/ theorem nonposCompleteForward_false {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) (h_ov : ¬ zeroCrossOvershootComplete r res) (h_e1 : ¬ zeroCrossEndOne r res) (h_e1l : ¬ zeroCrossEndOneLong r res) (h_en : ¬ zeroCrossEndNonpos r res) (h_ue : ¬ zeroCrossUnderEnd r res) (h_lt : ¬ zeroCrossLongTop r res) (h_lf : ¬ zeroCrossLongFinite r res) (h_low : ¬ negCrossLowFwd r res) (h_lge : ¬ negCrossClampNonposLastGe r res) (h_bot : ¬ negCrossClampNonposBottom r res) (h_nc : ¬ negCrossNonclampNonpos r res) (h_cl : ¬ negCrossClampLongNonpos r res) (h_ncl : ¬ negCrossNonclampLongNonpos r res) (h_eo : ¬ negCrossEndOne r res) (h_eol : ¬ negCrossEndOneLong r res) (h_ce : ¬ negCrossClampEndOne r res) (h_ov2 : ¬ negCrossOvershoot r res) (h_hs : ¬ negCrossCompleteHighSmall r res) (h_hf : ¬ negCrossCompleteHighClampFinite r res) (h_res : ¬ negCrossCompleteResidual r res) : False := by classical obtain ⟨db, hcomp⟩ := hread have hread' : Readable r res := ⟨db, hcomp⟩ have hlastle : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := fun k hk => WithTop.coe_le_coe.mpr (le_getLastD_of_pairwise (RangeRead.compatible_sortedLT hcomp hrev).pairwise hk) have hcrossL : ((leastReported r res : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcross by_cases h0 : r.begin.offset = 0 · -- begin offset zero by_cases hend0 : r.end_.offset ≤ 0 · exact h_en ⟨h0, hmore, hrev, hend0, hne, hcrossL⟩ · have hend1 : 1 ≤ r.end_.offset := by omega by_cases hend1eq : r.end_.offset = 1 · by_cases hBE : r.begin.key ≤ r.end_.key · exact h_e1 ⟨h0, hmore, hrev, hend1eq, hne, hcrossL, hBE⟩ · exact h_e1l ⟨h0, hmore, hrev, hend1eq, hne, hcrossL, not_le.mp hBE⟩ · have hend2 : 2 ≤ r.end_.offset := by omega by_cases hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key · exact h_ov ⟨h0, hmore, hrev, ⟨hend2, hne, hhi⟩, hcrossL⟩ · have hElast : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by push Not at hhi obtain ⟨k, hk, hkge⟩ := hhi exact le_trans hkge (hlastle k hk) by_cases hBE : r.begin.key ≤ r.end_.key · exact h_ue ⟨h0, hmore, hrev, hend2, hne, hcrossL, hBE⟩ · have hEB : r.end_.key < r.begin.key := not_le.mp hBE by_cases hBtop : r.begin.key = (⊤ : WithTop Key) · exact h_lt ⟨h0, hmore, hrev, hend2, hne, hBtop⟩ · exact h_lf ⟨h0, hmore, hrev, hend2, hne, hBtop, hEB, hcrossL⟩ · have hneg : r.begin.offset < 0 := by omega by_cases hend0 : r.end_.offset ≤ 0 · -- end offset nonpositive by_cases hnonclamp : beginSlack r < reportedBelowBegin r res · exact h_low ⟨hrev, hneg, hmore, hne, hend0, hcross, hnonclamp⟩ · have hclamp : reportedBelowBegin r res ≤ beginSlack r := not_lt.mp hnonclamp by_cases hlastB : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) · exact h_lge ⟨hrev, hneg, hmore, hne, hend0, hcross, hlastB, hclamp⟩ · have hlastlt : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key := not_le.mp hlastB by_cases hBE : r.begin.key ≤ r.end_.key · by_cases hbot : clampNonposBottom r res · exact h_bot ⟨hrev, hneg, hmore, hne, hend0, hBE, hcross, hbot⟩ · exact h_nc ⟨hrev, hneg, hmore, hne, hend0, hBE, hcross, hclamp, hbot⟩ · have hEB : r.end_.key < r.begin.key := not_le.mp hBE by_cases hbot : clampNonposBottom r res · exact h_cl ⟨hrev, hneg, hmore, hne, hend0, hcross, hclamp, hbot, hEB⟩ · exact h_ncl ⟨hrev, hneg, hmore, hne, hend0, hcross, hclamp, hbot, hEB⟩ · have hendpos : 1 ≤ r.end_.offset := by omega by_cases hend1eq : r.end_.offset = 1 · by_cases hnonclamp : beginSlack r < reportedBelowBegin r res · by_cases hBE : r.begin.key ≤ r.end_.key · exact h_eo ⟨hrev, hneg, hmore, hne, hend1eq, hcross, hnonclamp, hBE⟩ · exact h_eol ⟨hrev, hneg, hmore, hne, hend1eq, hcross, hnonclamp, not_le.mp hBE⟩ · have hclamp : reportedBelowBegin r res ≤ beginSlack r := not_lt.mp hnonclamp exact h_ce ⟨hrev, hneg, hmore, hne, hend1eq, hcross, hclamp⟩ · have hend2 : 2 ≤ r.end_.offset := by omega by_cases hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key · exact h_ov2 ⟨hrev, hneg, hmore, hne, hend2, hhi, hcross⟩ · have hElast : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by push Not at hhi obtain ⟨k, hk, hkge⟩ := hhi exact le_trans hkge (hlastle k hk) by_cases hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) · have hhigh : negCrossCompleteHigh r res := ⟨hrev, hneg, hmore, hne, hend2, hcross, hEH⟩ have htle := high_length_le_end hread' hrev hmore hne hend2 hEH by_cases hte : res.keys.length = (r.end_.offset - 1).toNat · by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r · exact h_hf ⟨hhigh, hclamp, hte⟩ · exact h_hs (Or.inr (Or.inl ⟨hhigh, not_le.mp hclamp, hte⟩)) · have htlt : res.keys.length < (r.end_.offset - 1).toNat := lt_of_le_of_ne htle hte by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r · exact h_hs (Or.inl ⟨hhigh, hclamp, htlt⟩) · exact h_hs (Or.inr (Or.inr ⟨hhigh, not_le.mp hclamp, htlt⟩)) · have hEHle : ((res.keys.headD ([] : Key) : Key) : WithTop Key) ≤ r.end_.key := not_lt.mp hEH exact h_res ⟨hrev, hneg, hmore, hne, hend2, hcross, hEHle, hElast⟩ /-- Coverage of the nonpositive-begin crossed complete reversed cell by the closed-form dispatcher predicates. -/ theorem nonposCompleteReverse_false {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hcross : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) (h_zcr : ¬ zeroCrossCompleteRev r res) (h_ovr : ¬ negCrossOvershootRev r res) (h_lowr : ¬ negCrossLowFwdRev r res) (h_lger : ¬ negCrossClampNonposLastGeRev r res) (h_botr : ¬ negCrossClampNonposBottomRev r res) (h_ncr : ¬ negCrossNonclampNonposRev r res) (h_clr : ¬ negCrossClampLongNonposRev r res) (h_nclr : ¬ negCrossNonclampLongNonposRev r res) (h_eor : ¬ negCrossEndOneRev r res) (h_eolr : ¬ negCrossEndOneLongRev r res) (h_cer : ¬ negCrossClampEndOneRev r res) (h_hr : ¬ negCrossCompleteHighRev r res) (h_resr : ¬ negCrossCompleteResidualRev r res) : False := by classical obtain ⟨db, hcomp⟩ := hread have hne' : res.keys.reverse ≠ [] := by simpa using hne have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse] have hlastD : res.keys.reverse.getLastD ([] : Key) = res.keys.headD ([] : Key) := by rw [List.getLastD_eq_getLast?, List.headD_eq_head?, List.getLast?_reverse] have hcountP : (res.keys.reverse).countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = res.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := List.Perm.countP_eq _ (List.reverse_perm res.keys) have hcrossL : ((leastReported r res : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcross by_cases h0 : r.begin.offset = 0 · exact h_zcr ⟨h0, hmore, hrev, hne, hcrossL⟩ · have hneg : r.begin.offset < 0 := by omega apply nonposCompleteForward_false (r := { r with reverse := false }) (res := { keys := res.keys.reverse, more := false }) · exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ · rfl · rfl · exact hne' · simpa using hbegin · simpa [hheadD] using hcross · intro h; exact h0 (by simpa using h.1) · intro h; exact h0 (by simpa using h.1) · intro h; exact h0 (by simpa using h.1) · intro h; exact h0 (by simpa using h.1) · intro h; exact h0 (by simpa using h.1) · intro h; exact h0 (by simpa using h.1) · intro h; exact h0 (by simpa using h.1) · intro h obtain ⟨-, -, -, -, hend, hcross', hnonclamp⟩ := h refine h_lowr ⟨hrev, hneg, hmore, hne, hend, ?_, ?_⟩ · simpa [hheadD] using hcross' · simpa only [beginSlack, reportedBelowBegin, hcountP] using hnonclamp · intro h obtain ⟨-, -, -, -, hend, hcross', hlastB, hclamp⟩ := h refine h_lger ⟨hrev, hneg, hmore, hne, hend, ?_, ?_, ?_⟩ · simpa [hheadD] using hcross' · simpa [hlastD] using hlastB · simpa only [beginSlack, reportedBelowBegin, hcountP] using hclamp · intro h obtain ⟨-, -, -, -, hend, hBE, hcross', hbottom⟩ := h refine h_botr ⟨hrev, hneg, hmore, hne, hend, hBE, ?_, ?_⟩ · simpa [hheadD] using hcross' · simpa only [clampNonposBottom, clampNonposD, beginSlack, reportedBelowBegin, hcountP] using hbottom · intro h obtain ⟨-, -, -, -, hend, hBE, hcross', hclamp, hnotbottom⟩ := h refine h_ncr ⟨hrev, hneg, hmore, hne, hend, hBE, ?_, ?_, ?_⟩ · simpa [hheadD] using hcross' · simpa only [beginSlack, reportedBelowBegin, hcountP] using hclamp · simpa only [clampNonposBottom, clampNonposD, beginSlack, reportedBelowBegin, hcountP] using hnotbottom · intro h obtain ⟨-, -, -, -, hend, hcross', hclamp, hbottom, hlong⟩ := h refine h_clr ⟨hrev, hneg, hmore, hne, hend, ?_, ?_, ?_, hlong⟩ · simpa [hheadD] using hcross' · simpa only [beginSlack, reportedBelowBegin, hcountP] using hclamp · simpa only [clampNonposBottom, clampNonposD, beginSlack, reportedBelowBegin, hcountP] using hbottom · intro h obtain ⟨-, -, -, -, hend, hcross', hclamp, hnotbottom, hlong⟩ := h refine h_nclr ⟨hrev, hneg, hmore, hne, hend, ?_, ?_, ?_, hlong⟩ · simpa [hheadD] using hcross' · simpa only [beginSlack, reportedBelowBegin, hcountP] using hclamp · simpa only [clampNonposBottom, clampNonposD, beginSlack, reportedBelowBegin, hcountP] using hnotbottom · intro h obtain ⟨-, -, -, -, hend, hcross', hnonclamp, hBE⟩ := h refine h_eor ⟨hrev, hneg, hmore, hne, hend, ?_, ?_, hBE⟩ · simpa [hheadD] using hcross' · simpa only [beginSlack, reportedBelowBegin, hcountP] using hnonclamp · intro h obtain ⟨-, -, -, -, hend, hcross', hnonclamp, hlong⟩ := h refine h_eolr ⟨hrev, hneg, hmore, hne, hend, ?_, ?_, hlong⟩ · simpa [hheadD] using hcross' · simpa only [beginSlack, reportedBelowBegin, hcountP] using hnonclamp · intro h obtain ⟨-, -, -, -, hend, hcross', hclamp⟩ := h refine h_cer ⟨hrev, hneg, hmore, hne, hend, ?_, ?_⟩ · simpa [hheadD] using hcross' · simpa only [beginSlack, reportedBelowBegin, hcountP] using hclamp · intro h obtain ⟨-, -, -, -, hend, hhi, hcross'⟩ := h exact h_ovr ⟨hrev, hneg, hmore, hne, hend, (fun k hk => hhi k (by simpa using hk)), by simpa [hheadD] using hcross'⟩ · intro h rcases h with hc | hc | hc · exact h_hr ⟨hrev, hmore, hne, hc.1⟩ · exact h_hr ⟨hrev, hmore, hne, hc.1⟩ · exact h_hr ⟨hrev, hmore, hne, hc.1⟩ · intro h exact h_hr ⟨hrev, hmore, hne, h.1⟩ · intro h obtain ⟨-, -, -, -, hend, hcross', hheadE, hEL⟩ := h exact h_resr ⟨hrev, hneg, hmore, hne, hend, by simpa [hheadD] using hcross', by simpa [hheadD] using hheadE, by simpa [hlastD] using hEL⟩ /-- Reported-key count bound: at a positive end offset, a compatible result reports at most `end.offset - 1` keys at or above `end.key`. -/ theorem reportedCount_le_endOffset {db : DB} {r : RangeRead} {res : Result} (hcomp : RangeRead.Compatible db r res) (hend : 1 ≤ r.end_.offset) : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ (r.end_.offset - 1).toNat := by have hpre := (RangeRead.compatible_keys_prefix hcomp).sublist exact le_trans (List.Sublist.countP_le hpre) (completeKeys_count_ge_le_end_offset (db := db) (r := r) hend) /-- In a strictly descending list, the last entry is at most the head. -/ theorem getLastD_le_headD_of_sortedGT {res : Result} (hne : res.keys ≠ []) (hsort : res.keys.SortedGT) : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by rw [getLastD_eq_getLast_of_ne hne, headD_eq_head_of_ne hne] exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne)) /-- In a strictly descending list, every entry is at most the head. -/ theorem le_headD_of_sortedGT {res : Result} (hne : res.keys ≠ []) (hsort : res.keys.SortedGT) : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by intro k hk rw [headD_eq_head_of_ne hne] exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hk) /-- In a strictly descending list, the last entry is at most every entry. -/ theorem getLastD_le_of_sortedGT {res : Result} (hne : res.keys ≠ []) (hsort : res.keys.SortedGT) : ∀ k ∈ res.keys, ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by intro k hk rw [getLastD_eq_getLast_of_ne hne] exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hk) /-- Complement of a capacity bound in `ℕ∞`. -/ theorem capacity_complement {a b : ℕ∞} (h : ¬ a ≤ b + 1) : b + 2 ≤ a := by have h1 : b + 1 < a := not_le.mp h have hne : b + 1 ≠ ⊤ := by intro hb rw [hb] at h1 exact (not_lt_of_ge le_top) h1 have h2 : (b + 1) + 1 ≤ a := (ENat.add_one_le_iff hne).mpr h1 calc b + 2 = b + (1 + 1) := by rw [one_add_one_eq_two] _ = (b + 1) + 1 := by rw [add_assoc] _ ≤ a := h2 /-- Coverage of the nonpositive-begin crossed reversed truncated cell by the closed-form dispatcher predicates. -/ theorem nonposTruncRev_false {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hcross : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) (h_zc : ¬ zeroCrossTruncRevCrossed r res) (h_be : ¬ negCrossTruncRevBelowEnd r res) (h_cs : ¬ negCrossTruncRevCrossedSmall r res) (h_cl : ¬ negCrossTruncRevCrossedLarge r res) (h_un : ¬ negCrossTruncRevUnder r res) (h_cns : ¬ negCrossTruncRevCrossedNonposSmall r res) (h_cnl : ¬ negCrossTruncRevCrossedNonposLarge r res) (h_tcs : ¬ negCrossTruncRevResidualTopCrossedSmallGap r res) (h_tc : ¬ negCrossTruncRevResidualTopCrossed r res) (h_tlc : ¬ negCrossTruncRevResidualTopLargeCrossed r res) (h_tls : ¬ negCrossTruncRevResidualTopLargeSentinel r res) (h_fuc : ¬ negCrossTruncRevResidualFiniteUnderCrossed r res) (h_func : ¬ negCrossTruncRevResidualFiniteUnderNotCrossed r res) (h_fo : ¬ negCrossTruncRevResidualFiniteOver r res) : False := by classical obtain ⟨db, hcomp⟩ := hread have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev set L : Key := res.keys.getLastD ([] : Key) with hLdef set H : Key := res.keys.headD ([] : Key) with hHdef set cnt : ℕ := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hcntdef set e : ℕ := (r.end_.offset - 1).toNat with hedef have hcrossL : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross by_cases h0 : r.begin.offset = 0 · exact h_zc ⟨h0, hrev, hmore, hne, by simpa [leastReported, hrev, hLdef] using hcrossL⟩ · have hneg : r.begin.offset < 0 := by omega by_cases hend0 : r.end_.offset ≤ 0 · by_cases hEB : r.end_.key < r.begin.key · by_cases hsmall : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + ((1 - r.end_.offset).toNat : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1 · exact h_cns ⟨hmore, hrev, hneg, hne, hcrossL, hEB, hend0, hsmall⟩ · exact h_cnl ⟨hmore, hrev, hneg, hne, hcrossL, hEB, hend0, capacity_complement hsmall⟩ · exact h_un ⟨hmore, hrev, hneg, hne, hcrossL, not_lt.mp hEB, by omega⟩ · by_cases hend1eq : r.end_.offset = 1 · by_cases hEB : r.end_.key < r.begin.key · by_cases hsmall : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1 · exact h_cs ⟨hmore, hrev, hneg, hne, hcrossL, hEB, hend1eq, hsmall⟩ · exact h_cl ⟨hmore, hrev, hneg, hne, hcrossL, hEB, hend1eq, capacity_complement hsmall⟩ · exact h_un ⟨hmore, hrev, hneg, hne, hcrossL, not_lt.mp hEB, by omega⟩ · have hend2 : 2 ≤ r.end_.offset := by omega have hcnt_le : cnt ≤ e := by rw [hcntdef, hedef] exact reportedCount_le_endOffset hcomp (by omega) by_cases hEtop : r.end_.key = (⊤ : WithTop Key) · -- the sentinel end is subsumed by the under / below-end cells by_cases hoff : r.end_.offset ≤ 1 · exact h_un ⟨hmore, hrev, hneg, hne, hcrossL, by rw [hEtop]; exact le_top, hoff⟩ · exact h_be ⟨hmore, hrev, hneg, hne, hcrossL, by omega, by intro k hk; rw [hEtop]; exact WithTop.coe_lt_top _⟩ · by_cases hLE : ((L : Key) : WithTop Key) ≤ r.end_.key · by_cases hcntlt : cnt < e · exact h_tc ⟨hmore, hrev, hneg, hne, hend2, hLE, by simpa [hcntdef, hedef] using hcntlt⟩ · have hcnte : cnt = e := le_antisymm hcnt_le (not_lt.mp hcntlt) have hbase : negCrossTruncRevResidualFiniteUnder r res := ⟨hmore, hrev, hneg, hne, hend2, hLE, hcrossL, by simpa [hcntdef, hedef] using hcnte⟩ by_cases hka : (((keyAfter H : Key)) : WithTop Key) < r.begin.key · exact h_fuc ⟨hbase, by simpa [hHdef] using hka⟩ · exact h_func ⟨hbase, by simpa [hHdef] using hka⟩ · have hEL : r.end_.key < ((L : Key) : WithTop Key) := not_le.mp hLE by_cases hcnte : cnt = e · exact h_fo ⟨hmore, hrev, hneg, hne, hend2, hEL, hcrossL, by simpa [hcntdef, hedef] using hcnte⟩ · have hcntlt : cnt < e := lt_of_le_of_ne hcnt_le hcnte by_cases hcap : ((e : ℕ) : ℕ∞) ≤ intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) · have hlarge : negCrossTruncRevResidualTopLarge r res := ⟨hmore, hrev, hneg, hne, hend2, hEL, by simpa [hcntdef, hedef] using hcntlt, by simpa [hedef, hLdef] using hcap⟩ by_cases hBtop : r.begin.key = (⊤ : WithTop Key) · exact h_tls ⟨hlarge, hBtop⟩ · exact h_tlc ⟨hlarge, hcrossL, hBtop⟩ · have hsmallgap : negCrossTruncRevResidualTopCrossedSmallGap r res := ⟨hmore, hrev, hneg, hne, hend2, hEL, by have hh : intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) < ((e : ℕ) : ℕ∞) := not_le.mp hcap simpa [hedef, hLdef] using hh⟩ exact h_tcs hsmallgap /-- The cross-free nonpositive complete predicate is false on a crossed read. -/ theorem not_nonposCompleteCrossFree_of_crossed {r : RangeRead} {res : Result} (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : ¬ nonposCompleteCrossFree r res := by rintro ⟨-, -, -, hle⟩ exact absurd hle (not_le.mpr hcross) /-- The cross-free nonpositive reversed-truncated predicate is false on a crossed read. -/ theorem not_nonposRevTrunc_of_crossed {r : RangeRead} {res : Result} (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : ¬ nonposRevTrunc r res := by rintro ⟨-, -, -, hle⟩ exact absurd hle (not_le.mpr hcross) /-- `negCrossTruncRevBeginTopGeTwo` is subsumed by `negCrossTruncRevBelowEnd`. -/ theorem negCrossTruncRevBelowEnd_of_beginTopGeTwo {r : RangeRead} {res : Result} (h : negCrossTruncRevBeginTopGeTwo r res) : negCrossTruncRevBelowEnd r res := by obtain ⟨hm, hrev, hneg, hne, hBtop, hend2, hendlt, hbelow⟩ := h refine ⟨hm, hrev, hneg, hne, ?_, hend2, hbelow⟩ rw [hBtop] exact WithTop.coe_lt_top _ /-- `negCrossTruncRevTop` is subsumed by `negCrossTruncRevUnder` or `negCrossTruncRevBelowEnd`. -/ theorem negCrossTruncRevUnder_or_belowEnd_of_top {r : RangeRead} {res : Result} (h : negCrossTruncRevTop r res) : negCrossTruncRevUnder r res ∨ negCrossTruncRevBelowEnd r res := by obtain ⟨hm, hrev, hneg, hne, hcross, hEtop⟩ := h by_cases hoff : r.end_.offset ≤ 1 · left exact ⟨hm, hrev, hneg, hne, hcross, by rw [hEtop]; exact le_top, hoff⟩ · right have hend2 : 2 ≤ r.end_.offset := by omega refine ⟨hm, hrev, hneg, hne, hcross, hend2, ?_⟩ intro k hk rw [hEtop] exact WithTop.coe_lt_top _ set_option maxHeartbeats 0 in -- This declaration performs an expensive finite case analysis. /-- **The residual dispatcher is minimal on the residual region.** The residual is crossed and either complete or reversed truncated, so all forward-truncated, clamped and cross-free branches have already been discharged by the compact certificates. -/ theorem minimal_conflictRangeClosedFormResidualCompleteFwd {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) (hempty : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) : Minimal r res (conflictRangeClosedFormResidualCompleteFwd r res) := by classical unfold conflictRangeClosedFormResidualCompleteFwd compactCrossedComplete by_cases h1 : crossedCompleteOvershoot r res · rw [ite_eq_left h1] rcases h1 with h1 | h1 · rw [closedFormNegCrossTopUnified_eq_zeroCross h1] exact minimal_closedForm_of_zeroCross_overshoot hread h1 · rw [closedFormNegCrossTopUnified_eq_negCross h1] have hcross' : ((res.keys.head h1.2.2.2.1 : Key) : WithTop Key) < r.begin.key := by have h := h1.2.2.2.2.2.2 rwa [headD_eq_head_of_ne h1.2.2.2.1] at h exact minimal_closedForm_of_negCross_overshoot hread h1.1 h1.2.1 h1.2.2.1 h1.2.2.2.1 h1.2.2.2.2.1 h1.2.2.2.2.2.1 hcross' · rw [ite_eq_right h1] by_cases h2 : crossedCompleteEndLeOne r res · rw [ite_eq_left h2] have h2' := (crossedCompleteEndLeOne_iff r res).mp h2 rcases h2' with h2 | h2 | h2 | h2 | h2 | h2 | h2 | h2 · rw [closedFormNegCrossNonclampUnified_eq_zeroCrossEndOne h2] exact minimal_closedForm_of_zeroCross_endOne hread h2 · rw [closedFormNegCrossNonclampUnified_eq_zeroCrossEndNonpos hread h2] exact minimal_closedForm_of_zeroCross_endNonpos hread h2 · rw [closedFormNegCrossNonclampUnified_eq_zeroCrossEndOneLong h2] exact minimal_closedForm_of_zeroCross_endOne_long hread h2 · have hcell := h2 obtain ⟨hrev7, hbeg7, hmore7, hne7, hend7, hcrossD7, hnonclamp7⟩ := h2 obtain ⟨db₀, hcomp₀⟩ := hread have hheadD : res.keys.headD ([] : Key) = res.keys.head hne7 := headD_eq_head_of_ne hne7 have hcross7 : ((res.keys.head hne7 : Key) : WithTop Key) < r.begin.key := by rw [← hheadD]; exact hcrossD7 have hBleE : r.begin.key ≤ r.end_.key := negCross_begin_le_end hcomp₀ hrev7 hmore7 hne7 hbeg7 hcross7 hnonclamp7 hend7 rw [closedFormNegCrossNonclampUnified_eq_of_le hBleE] exact minimal_closedForm_of_negCross_lowFwd ⟨db₀, hcomp₀⟩ hcell · rw [closedFormNegCrossNonclampUnified_eq_of_le h2.2.2.2.2.2.2.2] exact minimal_closedForm_of_negCross_endOne hread h2 · rw [closedFormNegCrossNonclampUnified_eq_of_le h2.2.2.2.2.2.1] exact minimal_closedForm_of_negCross_nonclamp_nonpos hread h2 · rw [closedFormNegCrossNonclampUnified_eq_of_lt h2.2.2.2.2.2.2.2] exact minimal_closedForm_of_negCross_endOne_long hread h2 · rw [closedFormNegCrossNonclampUnified_eq_of_lt h2.2.2.2.2.2.2.2.2] exact minimal_closedForm_of_negCross_nonclamp_long_nonpos hread h2 · rw [ite_eq_right h2] by_cases h4 : crossedCompleteBeginZero r res · rw [ite_eq_left h4] by_cases h5 : zeroCrossUnderEnd r res · rw [closedFormZeroCrossResidual_eq_under h5] exact minimal_closedForm_of_zeroCross_underEnd hread h5 · have h5' : zeroCrossLongTop r res ∨ zeroCrossLongFinite r res := by rcases h4 with h | h | h · exact absurd h h5 · exact Or.inl h · exact Or.inr h rw [closedFormZeroCrossResidual_eq_long h5 h5'] rcases h5' with h5' | h5' · exact minimal_closedForm_of_zeroCross_longTop hread h5' · exact minimal_closedForm_of_zeroCross_longFinite hread h5' · rw [ite_eq_right h4] by_cases h9 : crossedCompleteClamp r res · rw [ite_eq_left h9] by_cases he : negCrossClampEndOne r res · rw [closedFormNegCrossClampUnified_eq_endOne he] exact minimal_closedForm_of_negCross_clampEndOne hread he · by_cases hb : negCrossHeadBottom r res ∨ negCrossClampNonposInfinite r res ∨ negCrossClampNonposLastGe r res ∨ negCrossClampNonposBottom r res · rw [closedFormNegCrossClampUnified_eq_bottom hb] rcases hb with hb | hb | hb | hb · exact minimal_closedForm_of_negCross_headBottom hread hb · exact minimal_closedForm_of_negCross_clampNonpos_infinite hread hb · exact minimal_closedForm_of_negCross_clampNonpos_lastGe hread hb · exact minimal_closedForm_of_negCross_clampNonpos_bottom hread hb · have hl : negCrossClampLongNonpos r res := by rcases h9 with h | h | h | h | h | h · exact absurd h he · exact absurd (Or.inl h) hb · exact absurd (Or.inr (Or.inl h)) hb · exact absurd (Or.inr (Or.inr (Or.inl h))) hb · exact absurd (Or.inr (Or.inr (Or.inr h))) hb · exact h rw [closedFormNegCrossClampUnified_eq_long hb hl] exact minimal_closedForm_of_negCross_clamp_long_nonpos hread hl · rw [ite_eq_right h9] by_cases h12 : negCrossCompleteHigh r res · rw [closedFormNegCrossCompleteHighResidual_eq_high h12] exact minimal_closedForm_of_negCrossCompleteHigh hread h12 · rw [closedFormNegCrossCompleteHighResidual_eq_residual h12] by_cases h13 : negCrossCompleteResidual r res · exact minimal_closedForm_of_negCross_complete_residual hread h13 · exfalso exact nonposCompleteForward_false hread hmore hrev hempty hbegin hcross (fun h => h1 (Or.inl h)) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inl h))) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inr (Or.inr (Or.inl h))))) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inr (Or.inl h)))) (fun h => h4 (Or.inl h)) (fun h => h4 (Or.inr (Or.inl h))) (fun h => h4 (Or.inr (Or.inr h))) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) (fun h => h9 (Or.inr (Or.inr (Or.inr (Or.inl h))))) (fun h => h9 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))))))) (fun h => h9 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h)))))) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))))))) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))))) (fun h => h2 ((crossedCompleteEndLeOne_iff r res).mpr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h))))))))) (fun h => h9 (Or.inl h)) (fun h => h1 (Or.inr h)) (fun h => by rcases h with h | h | h · exact h12 h.1 · exact h12 h.1 · exact h12 h.1) (fun h => h12 h.1) h13 theorem minimal_conflictRangeClosedFormResidualRevTruncLeOne {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hempty : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hcross : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) (h_zc : ¬ zeroCrossTruncRevCrossed r res) (hend : r.end_.offset ≤ 1) : Minimal r res (conflictRangeClosedFormResidualRevTruncLeOne r res) := by classical unfold conflictRangeClosedFormResidualRevTruncLeOne by_cases h1 : revTruncLeOneLow r res · rw [ite_eq_left h1] rcases h1 with h1 | h1 | h1 · exact minimal_closedForm_of_negCross_trunc_rev_under hread h1 · exact minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_small hread h1 · exact minimal_closedForm_of_negCross_trunc_rev_crossedSmall hread h1 · rw [ite_eq_right h1] by_cases h4 : revTruncLeOneHigh r res · rcases h4 with h4 | h4 | h4 · rw [closedFormNegCrossTruncRevCrossedNonposLarge_eq_beginTop h4] exact minimal_closedForm_of_negCross_trunc_rev_beginTop hread h4 · exact minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_large hread h4 · exact minimal_closedForm_of_negCross_trunc_rev_crossedLarge hread h4 · exfalso have hbe : ¬ negCrossTruncRevBelowEnd r res := fun h => by unfold negCrossTruncRevBelowEnd at h omega have htcs : ¬ negCrossTruncRevResidualTopCrossedSmallGap r res := fun h => by unfold negCrossTruncRevResidualTopCrossedSmallGap at h omega have htc : ¬ negCrossTruncRevResidualTopCrossed r res := fun h => by unfold negCrossTruncRevResidualTopCrossed at h omega have htlc : ¬ negCrossTruncRevResidualTopLargeCrossed r res := fun h => by unfold negCrossTruncRevResidualTopLargeCrossed negCrossTruncRevResidualTopLarge at h omega have htls : ¬ negCrossTruncRevResidualTopLargeSentinel r res := fun h => by unfold negCrossTruncRevResidualTopLargeSentinel negCrossTruncRevResidualTopLarge at h omega have hfuc : ¬ negCrossTruncRevResidualFiniteUnderCrossed r res := fun h => by unfold negCrossTruncRevResidualFiniteUnderCrossed negCrossTruncRevResidualFiniteUnder at h omega have hfunc : ¬ negCrossTruncRevResidualFiniteUnderNotCrossed r res := fun h => by unfold negCrossTruncRevResidualFiniteUnderNotCrossed negCrossTruncRevResidualFiniteUnder at h omega have hfo : ¬ negCrossTruncRevResidualFiniteOver r res := fun h => by unfold negCrossTruncRevResidualFiniteOver at h omega exact nonposTruncRev_false hread hmore hrev hempty hbegin hcross h_zc hbe (fun h => h1 (Or.inr (Or.inr h))) (fun h => h4 (Or.inr (Or.inr h))) (fun h => h1 (Or.inl h)) (fun h => h1 (Or.inr (Or.inl h))) (fun h => h4 (Or.inr (Or.inl h))) htcs htc htlc htls hfuc hfunc hfo theorem minimal_conflictRangeClosedFormResidualRevTruncGeTwo {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hempty : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hcross : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) (h_zc : ¬ zeroCrossTruncRevCrossed r res) (hend : 2 ≤ r.end_.offset) : Minimal r res (conflictRangeClosedFormResidualRevTruncGeTwo r res) := by classical unfold conflictRangeClosedFormResidualRevTruncGeTwo by_cases h1 : revTruncGeTwoTop r res · rw [ite_eq_left h1] rcases h1 with h1 | h1 | h1 | h1 · exact minimal_closedForm_of_negCross_trunc_rev_belowEnd hread h1 · exact minimal_closedForm_of_negCross_trunc_rev_residual_room hread h1 · exact minimal_closedForm_of_negCross_trunc_rev_residual_top_crossed_smallGap hread h1 · exact minimal_closedForm_of_negCross_trunc_rev_residual_top_crossed hread h1 · rw [ite_eq_right h1] by_cases h2 : revTruncGeTwoLarge r res · rw [ite_eq_left h2] rcases h2 with h2 | h2 · rw [closedFormNegCrossTruncRevResidualTopLargeUnified_eq_crossed h2] exact minimal_closedForm_of_negCross_trunc_rev_residual_top_large_crossed hread h2 · rw [closedFormNegCrossTruncRevResidualTopLargeUnified_eq_sentinel h2] exact minimal_closedForm_of_negCross_trunc_rev_residual_top_large_sentinel hread h2 · rw [ite_eq_right h2] by_cases h4 : negCrossTruncRevResidualFiniteUnderCrossed r res ∨ negCrossTruncRevResidualFiniteUnderNotCrossed r res ∨ negCrossTruncRevResidualFiniteOver r res · rcases h4 with h4 | h4 | h4 · rw [closedFormNegCrossTruncRevResidualFiniteUnified_eq_under h4.1] exact minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_crossed hread h4 · rw [closedFormNegCrossTruncRevResidualFiniteUnified_eq_under h4.1] exact minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_notCrossed hread h4 · rw [closedFormNegCrossTruncRevResidualFiniteUnified_eq_over h4] exact minimal_closedForm_of_negCross_trunc_rev_residualFinite_over hread h4 · exfalso have hcs : ¬ negCrossTruncRevCrossedSmall r res := fun h => by unfold negCrossTruncRevCrossedSmall at h omega have hcl : ¬ negCrossTruncRevCrossedLarge r res := fun h => by unfold negCrossTruncRevCrossedLarge at h omega have hun : ¬ negCrossTruncRevUnder r res := fun h => by unfold negCrossTruncRevUnder at h omega have hcns : ¬ negCrossTruncRevCrossedNonposSmall r res := fun h => by unfold negCrossTruncRevCrossedNonposSmall at h omega have hcnl : ¬ negCrossTruncRevCrossedNonposLarge r res := fun h => by unfold negCrossTruncRevCrossedNonposLarge at h omega exact nonposTruncRev_false hread hmore hrev hempty hbegin hcross h_zc (fun h => h1 (Or.inl h)) hcs hcl hun hcns hcnl (fun h => h1 (Or.inr (Or.inr (Or.inl h)))) (fun h => h1 (Or.inr (Or.inr (Or.inr h)))) (fun h => h2 (Or.inl h)) (fun h => h2 (Or.inr h)) (fun h => h4 (Or.inl h)) (fun h => h4 (Or.inr (Or.inl h))) (fun h => h4 (Or.inr (Or.inr h))) theorem minimal_conflictRangeClosedFormResidualRevTrunc {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hempty : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hcross : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) : Minimal r res (conflictRangeClosedFormResidualRevTrunc r res) := by classical unfold conflictRangeClosedFormResidualRevTrunc by_cases hzc : zeroCrossTruncRevCrossed r res · rw [ite_eq_left hzc] exact minimal_closedForm_of_zeroCross_trunc_revCrossed hread hzc · rw [ite_eq_right hzc] by_cases hn : r.end_.offset ≤ 1 · rw [ite_eq_left hn] exact minimal_conflictRangeClosedFormResidualRevTruncLeOne hread hmore hrev hempty hbegin hcross hzc hn · rw [ite_eq_right hn] have hg : 2 ≤ r.end_.offset := by omega exact minimal_conflictRangeClosedFormResidualRevTruncGeTwo hread hmore hrev hempty hbegin hcross hzc hg theorem minimal_conflictRangeClosedFormResidualAux {r : RangeRead} {res : Result} (hread : Readable r res) (hbegin : r.begin.offset ≤ 0) (hempty : res.keys ≠ []) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : Minimal r res (conflictRangeClosedFormResidualAux r res) := by classical unfold conflictRangeClosedFormResidualAux by_cases hmore : res.more = false · rw [ite_eq_left hmore] by_cases hrev : r.reverse = false · rw [ite_eq_left hrev] have hcrossH : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcross exact minimal_conflictRangeClosedFormResidualCompleteFwd hread hmore hrev hempty hbegin hcrossH · rw [ite_eq_right hrev] have hrvt : r.reverse = true := by cases h : r.reverse with | false => exact absurd h hrev | true => rfl have hcrossL : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrvt] using hcross have hread' : Readable { r with reverse := false } { keys := res.keys.reverse, more := false } := by obtain ⟨db, hcomp⟩ := hread exact ⟨db, (compatible_flip (b := true) hrvt hmore).mpr hcomp⟩ have hne' : ({ keys := res.keys.reverse, more := false } : Result).keys ≠ [] := by rwa [List.reverse_ne_nil_iff] have hcross' : ((res.keys.reverse.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by have hr : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse] rw [hr]; exact hcrossL have hmin' := minimal_conflictRangeClosedFormResidualCompleteFwd hread' rfl rfl hne' hbegin hcross' exact (minimal_flip (b := true) hrvt hmore).mp hmin' · rw [ite_eq_right hmore] have hm : res.more = true := by cases h : res.more with | false => exact absurd h hmore | true => rfl by_cases hrev : r.reverse = true · rw [ite_eq_left hrev] have hcrossL : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key := by simpa [leastReported, hrev] using hcross exact minimal_conflictRangeClosedFormResidualRevTrunc hread hm hrev hempty hbegin hcrossL · rw [ite_eq_right hrev] have hrvf : r.reverse = false := by cases h : r.reverse with | false => rfl | true => exact absurd h hrev exact compactForwardTruncatedNonpos_minimal_of_not_residual hread hm hrvf hempty hbegin not_compactForwardNonposResidual /-- **The single crossed nonpositive closed form is minimal.** Transported across the definitional equality with the residual dispatcher. -/ theorem compactCrossed_minimal {r : RangeRead} {res : Result} (hread : Readable r res) (hbegin : r.begin.offset ≤ 0) (hempty : res.keys ≠ []) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : Minimal r res (compactCrossed r res) := by rw [compactCrossed_eq_residualAux r res] exact minimal_conflictRangeClosedFormResidualAux hread hbegin hempty hcross /-- The single closed form denotes the same key set as the residual dispatcher. -/ theorem compactCrossed_toSet_eq_residual {r : RangeRead} {res : Result} (_hread : Readable r res) (_hbegin : r.begin.offset ≤ 0) (_hempty : res.keys ≠ []) (_hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : (compactCrossed r res).toSet = (conflictRangeClosedFormResidual r res).toSet := by rw [compactCrossed_eq_residual r res] /-- Minimality under the legacy residual name (a one-line alias of `compactCrossed`). -/ theorem minimal_conflictRangeClosedFormResidual {r : RangeRead} {res : Result} (hread : Readable r res) (hbegin : r.begin.offset ≤ 0) (hempty : res.keys ≠ []) (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : Minimal r res (conflictRangeClosedFormResidual r res) := compactCrossed_minimal hread hbegin hempty hcross /-- **The closed dispatcher is minimal for every readable result.** It factors through the four compact certificates, and the residual branch is the single closed form `compactCrossed`. -/ theorem minimal_conflictRangeClosedFormClosed {r : RangeRead} {res : Result} (hread : Readable r res) : Minimal r res (conflictRangeClosedFormClosed r res) := by classical unfold conflictRangeClosedFormClosed by_cases hempty : res.keys = [] · rw [ite_eq_left hempty] exact minimal_emptyValue_of_emptyInference emptyInference hread hempty · rw [ite_eq_right hempty] by_cases hpos : 1 ≤ r.begin.offset · rw [ite_eq_left hpos] exact compactPositive_minimal hread hpos · rw [ite_eq_right hpos] by_cases hclamp : clampedNonpos r res · rw [ite_eq_left hclamp] exact compactClamped_minimal hread hclamp · rw [ite_eq_right hclamp] have hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key := by by_contra h exact hclamp ⟨hempty, by omega, not_lt.mp h⟩ exact compactCrossed_minimal hread (by omega) hempty hcross /-- The explicit residual of the compact dispatcher: nonempty nonpositive-begin cells outside the clamped certificate. These are exactly the crossed cells, where the compact certificates do not apply and `compactCrossed` takes over. -/ def compactResidual (r : RangeRead) (res : Result) : Prop := res.keys ≠ [] ∧ r.begin.offset ≤ 0 ∧ ¬ clampedNonpos r res /-- The total compact closed form. It is the certified dispatcher `conflictRangeClosedFormClosed`: three certificate branches (empty, positive offset, clamped nonpositive) plus the crossed nonpositive residual, whose own fallback is the certified compact forward rule. Thus the body contains no reference to the general or executable algorithm. -/ noncomputable def compact (r : RangeRead) (res : Result) : ConflictRange := conflictRangeClosedFormClosed r res /-- The residual branch is literally the residual-only dispatcher. -/ theorem compact_eq_closedFormClosed_of_residual {r : RangeRead} {res : Result} (_h : compactResidual r res) : compact r res = conflictRangeClosedFormClosed r res := rfl /-- The residual branch agrees with the certified total dispatcher in key-set terms. -/ theorem compact_eq_closedForm_of_residual {r : RangeRead} {res : Result} (hread : Readable r res) (h : compactResidual r res) : (compact r res).toSet = (conflictRangeClosedForm r res).toSet := by rw [compact_eq_closedFormClosed_of_residual h] exact (minimal_conflictRangeClosedFormClosed hread).toSet_eq (minimal_conflictRangeClosedForm_total hread) /-- The residual is exactly the crossed nonpositive region: a nonempty result at a nonpositive begin offset whose least reported key lies strictly below `begin.key`. -/ theorem compactResidual_iff_crossed {r : RangeRead} {res : Result} : compactResidual r res ↔ res.keys ≠ [] ∧ r.begin.offset ≤ 0 ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key := by unfold compactResidual clampedNonpos constructor · rintro ⟨hne, hnonpos, hclamp⟩ exact ⟨hne, hnonpos, not_le.mp (fun hle => hclamp ⟨hne, hnonpos, hle⟩)⟩ · rintro ⟨hne, hnonpos, hcross⟩ exact ⟨hne, hnonpos, fun h => absurd h.2.2 (not_le.mpr hcross)⟩ /-- The total compact algorithm is minimal on every readable pair. -/ theorem compact_minimal {r : RangeRead} {res : Result} (hread : Readable r res) : Minimal r res (compact r res) := minimal_conflictRangeClosedFormClosed hread /-- The total compact algorithm denotes exactly the general necessary-key hull. -/ theorem compact_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) : (compact r res).toSet = (conflictRangeGeneral r res).toSet := (compact_minimal hread).toSet_eq (conflictRangeGeneral_minimal hread) /-- The total compact algorithm agrees with the executable reference. -/ theorem compact_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) : (compact r res).toSet = (simpleConflictRange r res).toSet := (compact_minimal hread).toSet_eq (simpleConflictRange_minimal hread) /-- The compact algorithm and the exhaustive closed-form dispatcher denote the same range: the residual branch of `compact` is answered by `conflictRangeClosedForm`. -/ theorem compact_toSet_eq_closedForm {r : RangeRead} {res : Result} (hread : Readable r res) : (compact r res).toSet = (conflictRangeClosedForm r res).toSet := (compact_minimal hread).toSet_eq (minimal_conflictRangeClosedForm_total hread) /-- The simplified compact closed form is not merely equal in key set but is the same `ConflictRange` as the exhaustive closed-form dispatcher. On an empty result both are `emptyValue`; on a nonempty result both are sound minimal ranges, so both contain the reported keys and are nonempty, and `ConflictRange.eq_of_toSet_eq_of_nonempty` upgrades the key-set equality to a structure equality. This lets the simplification be certified by extensional equality rather than by re-deriving minimality of every merged branch. -/ theorem compact_eq_closedForm {r : RangeRead} {res : Result} (hread : Readable r res) : compact r res = conflictRangeClosedForm r res := by classical by_cases hne : res.keys = [] · simp [compact, conflictRangeClosedFormClosed, conflictRangeClosedForm, hne] · have hmem : res.keys.head hne ∈ compact r res := mem_of_sound_of_mem_result hread (compact_minimal hread).sound (res.keys.head hne) (List.head_mem hne) exact ConflictRange.eq_of_toSet_eq_of_nonempty (compact_toSet_eq_closedForm hread) ⟨res.keys.head hne, hmem⟩ end FormalConflictRange