/- Released under Apache 2.0 license as described in the file LICENSE. -/ import FormalConflictRange.General import FormalConflictRange.Sound import FormalConflictRange.ComputableAlgorithm /-! # The compact closed form at begin offset one `FormalConflictRange.Sound.closedFormOffsetOne` is a three-line formula for the conflict range of a complete, nonempty, non-reversed read with `begin.offset = 1` and `begin.key ≤ end_.key`. This module records that the formula denotes the same key set as the general conflict range and as the executable algorithm. The reversed traversal uses the same rule with the result's head in place of its last (`closedFormOffsetOneRev`, proved by flipping the read), and `closedFormOffsetOneAny` selects between the two by `r.reverse`, giving one compact rule for complete results in either traversal order. The complete rule also holds on the widened regions `1 ≤ begin.offset`, `end_.offset ≤ 1` (`minimal_closedForm_of_end_le_one`) and `1 ≤ begin.offset`, `2 ≤ end_.offset` with all reported keys below `end_.key` (`minimal_closedForm_of_reported_below`). For non-reversed truncated results, `closedFormOffsetOneTruncated` is `canonicalRange r` when `end_.offset ≤ 0` and `[begin.key, keyAfter (last reported))` otherwise (`minimal_closedForm_of_offset_one_truncated`). `closedFormOffsetOneResult` dispatches on `res.more`, yielding one compact rule for every non-reversed readable result at begin offset one (`minimal_closedForm_of_offset_one_result`). For reversed truncated results, `closedFormOffsetOneTruncatedRev` uses `[last reported, end.key)` when `end_.offset ≤ 1` and `[last reported, ⊤)` when `end_.offset ≥ 2` with every reported key below `end.key`; its minimality theorem carries that disjunction as an explicit hypothesis. The full rule `closedFormOffsetOneTruncatedRevFull` extends this with the reported head `H`, the reported last `L`, `t` the number of reported keys at or above `end_.key`, and `e = (end_.offset - 1).toNat`: it is `[L, end_.key)` when `end_.offset ≤ 1`, `[min L end_.key, keyAfter H)` when `t = e`, and `[min L end_.key, ⊤)` otherwise. It is minimal on `end_.offset ≤ 1` and on `H < end_.key` (`minimal_closedForm_of_offset_one_truncated_rev_full`). The corrected rule `closedFormOffsetOneTruncatedRevFullC` replaces the last branch by `[E, ⊤)` when `keyAfterN (e - t - 1) E < L` and `[L, ⊤)` otherwise; it is minimal on the whole reversed truncated region, with no residual cell (`minimal_closedForm_of_offset_one_truncated_rev_fullC`). `closedFormOffsetOneUniversal` assembles the complete, forward-truncated and corrected full reversed-truncated rules into one result-aware rule (`minimal_closedForm_of_offset_one_universal`), unconditional on the offset-one cross-free region. The branch `end_.offset ≤ 1` is `[begin.key, end_.key)`. Otherwise, with `t` the number of reported keys at or above `end_.key` and `e = (end_.offset - 1).toNat`, the range is `[begin.key, keyAfter (last reported))` when `t = e`, and `[begin.key, ⊤)` when `t < e`. The proof is `minimal_closedForm_of_offset_one`. -/ open FormalConflictRange namespace FormalConflictRange /-- On the offset-one cross-free complete forward region, the compact closed form denotes the same key set as the general conflict range. -/ theorem closedFormOffsetOne_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOne r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one hread hmore hne hrev hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- On the same region, the compact closed form denotes the same key set as the executable `simpleConflictRange`. -/ theorem closedFormOffsetOne_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOne r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one hread hmore hne hrev hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- The reversed closed form denotes the same key set as the general conflict range on the offset-one cross-free complete region. -/ theorem closedFormOffsetOneRev_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneRev r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_rev hread hmore hne hrev hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- The reversed closed form denotes the same key set as the executable algorithm on the offset-one cross-free complete region. -/ theorem closedFormOffsetOneRev_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneRev r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_rev hread hmore hne hrev hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- The traversal-aware closed form denotes the same key set as the general conflict range on the offset-one cross-free complete region, for either traversal order. -/ theorem closedFormOffsetOneAny_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneAny r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_any hread hmore hne hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- The traversal-aware closed form denotes the same key set as the executable algorithm on the offset-one cross-free complete region. -/ theorem closedFormOffsetOneAny_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneAny r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_any hread hmore hne hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- On the `end_.offset ≤ 1` half-plane at any positive begin offset and either traversal, the traversal-aware closed form denotes the general conflict range. -/ theorem closedFormOffsetOneAny_toSet_eq_general_of_end_le_one {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : (closedFormOffsetOneAny r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_end_le_one hread hmore hne hbegin hend).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneAny_toSet_eq_simple_of_end_le_one {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : (closedFormOffsetOneAny r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_end_le_one hread hmore hne hbegin hend).toSet_eq (simpleConflictRange_minimal hread) /-- On the positive-begin overshoot region whose reported keys all lie below `end_.key`, the traversal-aware closed form denotes the general conflict range. -/ theorem closedFormOffsetOneAny_toSet_eq_general_of_reported_below {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : (closedFormOffsetOneAny r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_reported_below hread hmore hne hbegin hend hhi).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneAny_toSet_eq_simple_of_reported_below {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : (closedFormOffsetOneAny r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_reported_below hread hmore hne hbegin hend hhi).toSet_eq (simpleConflictRange_minimal hread) /-- The truncated-result closed form denotes the general conflict range at begin offset one on the cross-free truncated region. -/ theorem closedFormOffsetOneTruncated_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneTruncated r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_truncated hread hmore hrev hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneTruncated_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneTruncated r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_truncated hread hmore hrev hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- The result-aware closed form denotes the general conflict range for every readable result at begin offset one on the cross-free region. -/ theorem closedFormOffsetOneResult_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hne : res.keys ≠ []) (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneResult r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_result hread hne hrev hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneResult_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hne : res.keys ≠ []) (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneResult r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_result hread hne hrev hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- The reversed truncated rule denotes the general conflict range on its certified cells. -/ theorem closedFormOffsetOneTruncatedRev_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcase : r.end_.offset ≤ 1 ∨ (2 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)) : (closedFormOffsetOneTruncatedRev r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_truncated_rev hread hmore hrev hbegin hcase).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneTruncatedRev_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcase : r.end_.offset ≤ 1 ∨ (2 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)) : (closedFormOffsetOneTruncatedRev r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_truncated_rev hread hmore hrev hbegin hcase).toSet_eq (simpleConflictRange_minimal hread) /-- The full reversed truncated rule denotes the general conflict range on its certified cells. -/ theorem closedFormOffsetOneTruncatedRevFull_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) (hcase : r.end_.offset ≤ 1 ∨ res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (r.end_.offset - 1).toNat ∨ ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.end_.key) : (closedFormOffsetOneTruncatedRevFull r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_truncated_rev_full_of_cases hread hmore hrev hbegin hcross hcase).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneTruncatedRevFull_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) (hcase : r.end_.offset ≤ 1 ∨ res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (r.end_.offset - 1).toNat ∨ ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.end_.key) : (closedFormOffsetOneTruncatedRevFull r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_truncated_rev_full_of_cases hread hmore hrev hbegin hcross hcase).toSet_eq (simpleConflictRange_minimal hread) /-- The corrected full reversed truncated rule denotes the general conflict range on the whole offset-one reversed-truncated region. -/ theorem closedFormOffsetOneTruncatedRevFullC_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneTruncatedRevFullC r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_truncated_rev_fullC hread hmore hrev hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneTruncatedRevFullC_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneTruncatedRevFullC r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_truncated_rev_fullC hread hmore hrev hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- The universal result-aware rule denotes the general conflict range on the offset-one cross-free region, with no residual cell. -/ theorem closedFormOffsetOneUniversal_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneUniversal r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_universal hread hne hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOneUniversal_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : (closedFormOffsetOneUniversal r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_universal hread hne hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- The universal rule denotes the general conflict range unconditionally on the automatic region (forward results, complete results, under-end results, and reads ending at `⊤`). -/ theorem closedFormOffsetOneUniversal_of_auto_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) (hauto : r.reverse = false ∨ res.more = false ∨ r.end_.offset ≤ 1 ∨ r.end_.key = (⊤ : WithTop Key)) : (closedFormOffsetOneUniversal r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_offset_one_universal_of_auto hread hne hbegin hcross hauto).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same unconditional identification with the executable algorithm. -/ theorem closedFormOffsetOneUniversal_of_auto_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) (hauto : r.reverse = false ∨ res.more = false ∨ r.end_.offset ≤ 1 ∨ r.end_.key = (⊤ : WithTop Key)) : (closedFormOffsetOneUniversal r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_offset_one_universal_of_auto hread hne hbegin hcross hauto).toSet_eq (simpleConflictRange_minimal hread) /-- The total forward-truncated rule denotes the general conflict range on the whole cross-free truncated region, for every positive begin offset. -/ theorem closedFormTruncatedGeneral_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : (closedFormTruncatedGeneral r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_truncated_general hread hmore hrev hbegin hcross).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormTruncatedGeneral_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : (closedFormTruncatedGeneral r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_truncated_general hread hmore hrev hbegin hcross).toSet_eq (simpleConflictRange_minimal hread) /-- The three-branch complete-result rule denotes the general conflict range on its automatic region at any positive begin offset. -/ theorem closedFormOffsetOne_toSet_eq_general_of_complete_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) (hcase : r.end_.offset ≤ 1 ∨ res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (r.end_.offset - 1).toNat ∨ (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)) : (closedFormOffsetOne r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_complete_general hread hmore hne hrev hbegin hcross hcase).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormOffsetOne_toSet_eq_simple_of_complete_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) (hcase : r.end_.offset ≤ 1 ∨ res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (r.end_.offset - 1).toNat ∨ (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)) : (closedFormOffsetOne r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_complete_general hread hmore hne hrev hbegin hcross hcase).toSet_eq (simpleConflictRange_minimal hread) /-- The equal-selector complete rule denotes the general conflict range. -/ theorem closedFormKeyEqComplete_toSet_eq_general {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) (hkey : r.begin.key = r.end_.key) (hend : 2 ≤ r.end_.offset) : (closedFormKeyEqComplete r res).toSet = (conflictRangeGeneral r res).toSet := (minimal_closedForm_of_key_eq_complete_general hread hmore hrev hne hbegin hkey hend).toSet_eq (conflictRangeGeneral_minimal hread) /-- The same identification with the executable algorithm. -/ theorem closedFormKeyEqComplete_toSet_eq_simple {r : RangeRead} {res : Result} (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) (hkey : r.begin.key = r.end_.key) (hend : 2 ≤ r.end_.offset) : (closedFormKeyEqComplete r res).toSet = (simpleConflictRange r res).toSet := (minimal_closedForm_of_key_eq_complete_general hread hmore hrev hne hbegin hkey hend).toSet_eq (simpleConflictRange_minimal hread) end FormalConflictRange