diff --git a/Bridge.lean b/Bridge.lean new file mode 100644 index 0000000..5d690e8 --- /dev/null +++ b/Bridge.lean @@ -0,0 +1,2520 @@ +import Statement +import FormalConflictRange.SimpleClosedForm + +set_option maxHeartbeats 4000000 +set_option linter.unusedVariables false +set_option linter.unusedSimpArgs false +set_option linter.unreachableTactic false +set_option linter.unusedTactic false +set_option linter.unnecessarySeqFocus false + +namespace Statement +open FormalConflictRange + +def toRefSelector (s : Selector) : FormalConflictRange.Selector := { key := s.key, offset := s.offset } +def toRefRead (r : RangeRead) : FormalConflictRange.RangeRead := + { begin := toRefSelector r.begin, end_ := toRefSelector r.end_, limit := none, reverse := r.reverse } +def toRefDB (db : DB) : FormalConflictRange.DB := { keys := db.keys, sorted := db.sorted } +def toRefCR (cr : ConflictRange) : FormalConflictRange.ConflictRange := { begin := cr.begin, end_ := cr.end_ } +def toRefRes (res : Result) : FormalConflictRange.Result := { keys := res.keys, more := res.more } + +def ofRefSelector (s : FormalConflictRange.Selector) : Selector := { key := s.key, offset := s.offset } +def ofRefRead (r : FormalConflictRange.RangeRead) : RangeRead := + { begin := ofRefSelector r.begin, end_ := ofRefSelector r.end_, reverse := r.reverse } +def ofRefDB (db : FormalConflictRange.DB) : DB := { keys := db.keys, sorted := db.sorted } +def ofRefCR (cr : FormalConflictRange.ConflictRange) : ConflictRange := { begin := cr.begin, end_ := cr.end_ } +def ofRefRes (res : FormalConflictRange.Result) : Result := { keys := res.keys, more := res.more } + +@[simp] theorem toRefRead_begin (r : RangeRead) : (toRefRead r).begin = toRefSelector r.begin := rfl +@[simp] theorem toRefRead_end (r : RangeRead) : (toRefRead r).end_ = toRefSelector r.end_ := rfl +@[simp] theorem toRefRead_reverse (r : RangeRead) : (toRefRead r).reverse = r.reverse := rfl +@[simp] theorem toRefRead_limit (r : RangeRead) : (toRefRead r).limit = none := rfl +@[simp] theorem toRefSelector_key (s : Selector) : (toRefSelector s).key = s.key := rfl +@[simp] theorem toRefSelector_offset (s : Selector) : (toRefSelector s).offset = s.offset := rfl +@[simp] theorem toRefDB_keys (db : DB) : (toRefDB db).keys = db.keys := rfl +@[simp] theorem toRefDB_sorted (db : DB) : (toRefDB db).sorted = db.sorted := rfl +@[simp] theorem toRefRes_keys (res : Result) : (toRefRes res).keys = res.keys := rfl +@[simp] theorem toRefRes_more (res : Result) : (toRefRes res).more = res.more := rfl +@[simp] theorem toRefCR_begin (cr : ConflictRange) : (toRefCR cr).begin = cr.begin := rfl +@[simp] theorem toRefCR_end (cr : ConflictRange) : (toRefCR cr).end_ = cr.end_ := rfl + +@[simp] theorem toRefCR_mk (a b : WithTop Key) : + toRefCR (⟨a, b⟩ : ConflictRange) = (⟨a, b⟩ : FormalConflictRange.ConflictRange) := rfl +@[simp] theorem toRefCR_ite {c : Prop} [Decidable c] (a b : ConflictRange) : + toRefCR (if c then a else b) = + (if c then toRefCR a else toRefCR b : FormalConflictRange.ConflictRange) := by + split <;> rfl + +-- Rewrite reference-side structure literals back into translations of statement +-- literals, so the per-definition bridge lemmas can fire on them. +@[simp] theorem ofRefSelector_toRefSelector (s : Selector) : + ofRefSelector (toRefSelector s) = s := rfl +@[simp] theorem ofRefSelector_lit (k : WithTop Key) (o : Int) : + ofRefSelector (⟨k, o⟩ : FormalConflictRange.Selector) = (⟨k, o⟩ : Selector) := rfl +@[simp] theorem ofRefRes_lit (ks : List Key) (m : Bool) : + ofRefRes (⟨ks, m⟩ : FormalConflictRange.Result) = (⟨ks, m⟩ : Result) := rfl +@[simp] theorem ofRefCR_lit (a b : WithTop Key) : + ofRefCR (⟨a, b⟩ : FormalConflictRange.ConflictRange) = (⟨a, b⟩ : ConflictRange) := rfl +@[simp] theorem toRefRead_mk_lit (b e : FormalConflictRange.Selector) (rev : Bool) : + (⟨b, e, none, rev⟩ : FormalConflictRange.RangeRead) = + toRefRead (⟨ofRefSelector b, ofRefSelector e, rev⟩ : RangeRead) := rfl +@[simp] theorem toRefRes_mk_lit (ks : List Key) (m : Bool) : + (⟨ks, m⟩ : FormalConflictRange.Result) = toRefRes (⟨ks, m⟩ : Result) := rfl + +-- inverses +@[simp] theorem ofRefCR_toRefCR (cr : ConflictRange) : ofRefCR (toRefCR cr) = cr := rfl +@[simp] theorem toRefCR_ofRefCR (cr : FormalConflictRange.ConflictRange) : toRefCR (ofRefCR cr) = cr := rfl +@[simp] theorem ofRefRes_toRefRes (res : Result) : ofRefRes (toRefRes res) = res := rfl +@[simp] theorem toRefRes_ofRefRes (res : FormalConflictRange.Result) : toRefRes (ofRefRes res) = res := rfl +@[simp] theorem ofRefDB_toRefDB (db : DB) : ofRefDB (toRefDB db) = db := rfl +@[simp] theorem toRefDB_ofRefDB (db : FormalConflictRange.DB) : toRefDB (ofRefDB db) = db := rfl +@[simp] theorem ofRefRead_toRefRead (r : RangeRead) : ofRefRead (toRefRead r) = r := rfl + +-- Alias lemmas for reference names that merely duplicate another reference +-- definition (with identical bodies). +@[simp] theorem ref_closedFormZeroCrossEndOne_eq + (r : FormalConflictRange.RangeRead) (res : FormalConflictRange.Result) : + FormalConflictRange.closedFormZeroCrossEndOne r res = + FormalConflictRange.closedFormZeroCrossEndNonpos r res := rfl +@[simp] theorem ref_compactReverseTruncatedZero_eq + (r : FormalConflictRange.RangeRead) (res : FormalConflictRange.Result) : + FormalConflictRange.compactReverseTruncatedZero r res = + FormalConflictRange.closedFormOffsetOneTruncatedRevFullC r res := + FormalConflictRange.compactReverseTruncatedZero_eq_offsetOne r res + +@[simp] theorem ite_or_same {c₁ c₂ : Prop} [Decidable c₁] [Decidable c₂] {α : Type} (a b : α) : + (if c₁ then a else if c₂ then a else b) = if c₁ ∨ c₂ then a else b := by + by_cases h₁ : c₁ <;> by_cases h₂ : c₂ <;> simp [h₁, h₂] + +@[simp] theorem normalizeCompleteResult_keys (r : RangeRead) (res : Result) : + (Statement.normalizeCompleteResult r res).keys = + (if r.reverse = true then res.keys.reverse else res.keys) := rfl + +theorem normalizeCompleteResult_keys_ne (r : RangeRead) (res : Result) (h : res.keys ≠ []) : + (Statement.normalizeCompleteResult r res).keys ≠ [] := by + unfold Statement.normalizeCompleteResult + split <;> simp_all + +attribute [simp] normalizeCompleteResult_keys_ne + +@[simp] theorem tr_keyAfterN (n : ℕ) (k : Key) : + FormalConflictRange.keyAfterN n k = Statement.keyAfterN n k := by + induction n generalizing k with + | zero => rfl + | succ n ih => simp [FormalConflictRange.keyAfterN, Statement.keyAfterN, ih] <;> rfl + +-- SKIP (no reference) Selector.anchor -> Selector.anchor +@[simp] theorem tr_resolve (db : DB) (s : Selector) : + FormalConflictRange.resolve (toRefDB db) (toRefSelector s) = Statement.resolve (db) (s) := by + unfold FormalConflictRange.resolve Statement.resolve + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +-- SKIP (no reference) RangeRead.inRange -> RangeRead.inRange +-- SKIP (no reference) RangeRead.completeKeys -> RangeRead.completeKeys +@[simp] theorem tr_keyAfter (k : Key) : + FormalConflictRange.keyAfter (k) = Statement.keyAfter (k) := by + unfold FormalConflictRange.keyAfter Statement.keyAfter + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_beginSlack (r : RangeRead) : + FormalConflictRange.beginSlack (toRefRead r) = Statement.beginSlack (r) := by + unfold FormalConflictRange.beginSlack Statement.beginSlack + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_cELo (oB : ℤ) (oE : ℤ) : + FormalConflictRange.cELo (oB) (oE) = Statement.cELo (oB) (oE) := by + unfold FormalConflictRange.cELo Statement.cELo + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_cEminP (_oB : ℤ) (oE : ℤ) : + FormalConflictRange.cEminP (_oB) (oE) = Statement.cEminP (_oB) (oE) := by + unfold FormalConflictRange.cEminP Statement.cEminP + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_canonicalRange (r : RangeRead) : + FormalConflictRange.canonicalRange (toRefRead r) = toRefCR (Statement.canonicalRange (r)) := by + unfold FormalConflictRange.canonicalRange Statement.canonicalRange + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_intervalCard (b : WithTop Key) (e : WithTop Key) : + FormalConflictRange.intervalCard (b) (e) = Statement.intervalCard (b) (e) := by + unfold FormalConflictRange.intervalCard Statement.intervalCard + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampCap (r : RangeRead) : + FormalConflictRange.clampEndOneCap (toRefRead r) = Statement.clampCap (r) := by + unfold FormalConflictRange.clampEndOneCap Statement.clampCap + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_overshoot (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.overshoot (toRefRead r) (toRefRes res) ↔ Statement.overshoot (r) (res) := by + unfold FormalConflictRange.overshoot Statement.overshoot + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_reportedBelowBegin (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.reportedBelowBegin (toRefRead r) (toRefRes res) = Statement.reportedBelowBegin (r) (res) := by + unfold FormalConflictRange.reportedBelowBegin Statement.reportedBelowBegin + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampSlack (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampEndOneSlack (toRefRead r) (toRefRes res) = Statement.clampSlack (r) (res) := by + unfold FormalConflictRange.clampEndOneSlack Statement.clampSlack + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampEndOneUpper (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampEndOneUpper (toRefRead r) (toRefRes res) = Statement.clampEndOneUpper (r) (res) := by + unfold FormalConflictRange.clampEndOneUpper Statement.clampEndOneUpper + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampHighCap (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampHighCap (toRefRead r) (toRefRes res) = Statement.clampHighCap (r) (res) := by + unfold FormalConflictRange.clampHighCap Statement.clampHighCap + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampHighThresh (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampHighThresh (toRefRead r) (toRefRes res) = Statement.clampHighThresh (r) (res) := by + unfold FormalConflictRange.clampHighThresh Statement.clampHighThresh + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampHighU (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampHighU (toRefRead r) (toRefRes res) = Statement.clampHighU (r) (res) := by + unfold FormalConflictRange.clampHighU Statement.clampHighU + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampNonposD (r : RangeRead) : + FormalConflictRange.clampNonposD (toRefRead r) = Statement.clampNonposD (r) := by + unfold FormalConflictRange.clampNonposD Statement.clampNonposD + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampLongC (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampLongC (toRefRead r) (toRefRes res) = Statement.clampLongC (r) (res) := by + unfold FormalConflictRange.clampLongC Statement.clampLongC + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampLongUpper (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampLongUpper (toRefRead r) (toRefRes res) = Statement.clampLongUpper (r) (res) := by + unfold FormalConflictRange.clampLongUpper Statement.clampLongUpper + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampNonposBottom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampNonposBottom (toRefRead r) (toRefRes res) ↔ Statement.clampNonposBottom (r) (res) := by + unfold FormalConflictRange.clampNonposBottom Statement.clampNonposBottom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_leastReported (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.leastReported (toRefRead r) (toRefRes res) = Statement.leastReported (r) (res) := by + unfold FormalConflictRange.leastReported Statement.leastReported + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampedNonpos (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampedNonpos (toRefRead r) (toRefRes res) ↔ Statement.clampedNonpos (r) (res) := by + unfold FormalConflictRange.clampedNonpos Statement.clampedNonpos + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_clampsToBottom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.clampsToBottom (toRefRead r) (toRefRes res) ↔ Statement.clampsToBottom (r) (res) := by + unfold FormalConflictRange.clampsToBottom Statement.clampsToBottom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_noPinB (KBE : ℕ∞) (oB : ℤ) (oE : ℤ) : + FormalConflictRange.noPinB (KBE) (oB) (oE) = Statement.noPinB (KBE) (oB) (oE) := by + unfold FormalConflictRange.noPinB Statement.noPinB + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossClampEndOne (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossClampEndOne (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossClampEndOne (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossClampEndOne Statement.closedFormNegCrossClampEndOne + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossClampEndOne (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossClampEndOne (toRefRead r) (toRefRes res) ↔ Statement.negCrossClampEndOne (r) (res) := by + unfold FormalConflictRange.negCrossClampEndOne Statement.negCrossClampEndOne + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossHeadBottom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossHeadBottom (toRefRead r) (toRefRes res) ↔ Statement.negCrossHeadBottom (r) (res) := by + unfold FormalConflictRange.negCrossHeadBottom Statement.negCrossHeadBottom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossClampNonposInfinite (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossClampNonposInfinite (toRefRead r) (toRefRes res) ↔ Statement.negCrossClampNonposInfinite (r) (res) := by + unfold FormalConflictRange.negCrossClampNonposInfinite Statement.negCrossClampNonposInfinite + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossClampNonposLastGe (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossClampNonposLastGe (toRefRead r) (toRefRes res) ↔ Statement.negCrossClampNonposLastGe (r) (res) := by + unfold FormalConflictRange.negCrossClampNonposLastGe Statement.negCrossClampNonposLastGe + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossClampNonposBottom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossClampNonposBottom (toRefRead r) (toRefRes res) ↔ Statement.negCrossClampNonposBottom (r) (res) := by + unfold FormalConflictRange.negCrossClampNonposBottom Statement.negCrossClampNonposBottom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossClampLongNonpos (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossClampLongNonpos (toRefRead r) (toRefRes res) ↔ Statement.negCrossClampLongNonpos (r) (res) := by + unfold FormalConflictRange.negCrossClampLongNonpos Statement.negCrossClampLongNonpos + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossClampUnified (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossClampUnified (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossClampUnified (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossClampUnified Statement.closedFormNegCrossClampUnified + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossCompleteHighM (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossCompleteHighM (toRefRead r) (toRefRes res) = Statement.negCrossCompleteHighM (r) (res) := by + unfold FormalConflictRange.negCrossCompleteHighM Statement.negCrossCompleteHighM + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossHighD (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossHighD (toRefRead r) (toRefRes res) = Statement.negCrossHighD (r) (res) := by + unfold FormalConflictRange.negCrossHighD Statement.negCrossHighD + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossHighGapCap (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossHighGapCap (toRefRead r) (toRefRes res) = Statement.negCrossHighGapCap (r) (res) := by + unfold FormalConflictRange.negCrossHighGapCap Statement.negCrossHighGapCap + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossHighUsesEnd (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossHighUsesEnd (toRefRead r) (toRefRes res) ↔ Statement.negCrossHighUsesEnd (r) (res) := by + unfold FormalConflictRange.negCrossHighUsesEnd Statement.negCrossHighUsesEnd + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossCompleteHigh (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossCompleteHigh (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossCompleteHigh (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossCompleteHigh Statement.closedFormNegCrossCompleteHigh + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossCompleteHigh (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossCompleteHigh (toRefRead r) (toRefRes res) ↔ Statement.negCrossCompleteHigh (r) (res) := by + unfold FormalConflictRange.negCrossCompleteHigh Statement.negCrossCompleteHigh + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormDeficit (r : RangeRead) : + FormalConflictRange.compactDeficit (toRefRead r) = Statement.closedFormDeficit (r) := by + unfold FormalConflictRange.compactDeficit Statement.closedFormDeficit + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_completeResidualReduced (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.completeResidualReduced (toRefRead r) (toRefRes res) = toRefRead (Statement.completeResidualReduced (r) (res)) := by + unfold FormalConflictRange.completeResidualReduced Statement.completeResidualReduced + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_topOfLast (last : Key) : + FormalConflictRange.topOfLast (last) = toRefCR (Statement.topOfLast (last)) := by + unfold FormalConflictRange.topOfLast Statement.topOfLast + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossOvershoot (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossOvershoot (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossOvershoot (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossOvershoot Statement.closedFormNegCrossOvershoot + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossEndOne (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossEndOne (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossEndOne (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossEndOne Statement.closedFormNegCrossEndOne + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossEndOneLong (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossEndOneLong (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossEndOneLong (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossEndOneLong Statement.closedFormNegCrossEndOneLong + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossCompleteResidual (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossCompleteResidual (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossCompleteResidual (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossCompleteResidual Statement.closedFormNegCrossCompleteResidual + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossCompleteHighResidual (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossCompleteHighResidual (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossCompleteHighResidual (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossCompleteHighResidual Statement.closedFormNegCrossCompleteHighResidual + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossNonclampUnified (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossNonclampUnified (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossNonclampUnified (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossNonclampUnified Statement.closedFormNegCrossNonclampUnified + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTopUnified (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTopUnified (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTopUnified (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTopUnified Statement.closedFormNegCrossTopUnified + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncNonposGap (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncNonposGap (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncNonposGap (r) (res) := by + unfold FormalConflictRange.negCrossTruncNonposGap Statement.negCrossTruncNonposGap + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncNonposGapA2aBottom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncNonposGapA2aBottom (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncNonposGapA2aBottom (r) (res) := by + unfold FormalConflictRange.negCrossTruncNonposGapA2aBottom Statement.negCrossTruncNonposGapA2aBottom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncNonposGapA2aBottomHigh (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncNonposGapA2aBottomHigh (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncNonposGapA2aBottomHigh (r) (res) := by + unfold FormalConflictRange.negCrossTruncNonposGapA2aBottomHigh Statement.negCrossTruncNonposGapA2aBottomHigh + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncNonposGapA2aBottom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncNonposGapA2aBottom (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncNonposGapA2aBottom (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncNonposGapA2aBottom Statement.closedFormNegCrossTruncNonposGapA2aBottom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncRevBelowEnd (_r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncRevBelowEnd (toRefRead _r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncRevBelowEnd (_r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncRevBelowEnd Statement.closedFormNegCrossTruncRevBelowEnd + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncRevCrossedNonposLarge (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncRevCrossedNonposLarge (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncRevCrossedNonposLarge (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncRevCrossedNonposLarge Statement.closedFormNegCrossTruncRevCrossedNonposLarge + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_finiteUnderCond (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.finiteUnderCond (toRefRead r) (toRefRes res) ↔ Statement.finiteUnderCond (r) (res) := by + unfold FormalConflictRange.finiteUnderCond Statement.finiteUnderCond + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncRevResidualFiniteUnder (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncRevResidualFiniteUnder (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncRevResidualFiniteUnder (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncRevResidualFiniteUnder Statement.closedFormNegCrossTruncRevResidualFiniteUnder + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevResidualFiniteOver (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevResidualFiniteOver (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevResidualFiniteOver (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevResidualFiniteOver Statement.negCrossTruncRevResidualFiniteOver + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncRevResidualFiniteUnified (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncRevResidualFiniteUnified (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncRevResidualFiniteUnified (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncRevResidualFiniteUnified Statement.closedFormNegCrossTruncRevResidualFiniteUnified + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_sentinelLargeTight (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.sentinelLargeTight (toRefRead r) (toRefRes res) ↔ Statement.sentinelLargeTight (r) (res) := by + unfold FormalConflictRange.sentinelLargeTight Statement.sentinelLargeTight + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncRevResidualTopLargeSentinel (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncRevResidualTopLargeSentinel (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncRevResidualTopLargeSentinel (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncRevResidualTopLargeSentinel Statement.closedFormNegCrossTruncRevResidualTopLargeSentinel + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevResidualTopLarge (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevResidualTopLarge (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevResidualTopLarge (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevResidualTopLarge Statement.negCrossTruncRevResidualTopLarge + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevResidualTopLargeSentinel (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevResidualTopLargeSentinel (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevResidualTopLargeSentinel (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevResidualTopLargeSentinel Statement.negCrossTruncRevResidualTopLargeSentinel + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncRevResidualTopLargeUnified (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncRevResidualTopLargeUnified (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncRevResidualTopLargeUnified (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncRevResidualTopLargeUnified Statement.closedFormNegCrossTruncRevResidualTopLargeUnified + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_truncatedRangeRev (r : RangeRead) (last : Key) : + FormalConflictRange.truncatedRangeRev (toRefRead r) (last) = toRefCR (Statement.truncatedRangeRev (r) (last)) := by + unfold FormalConflictRange.truncatedRangeRev Statement.truncatedRangeRev + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncRevUnder (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncRevUnder (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncRevUnder (r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncRevUnder Statement.closedFormNegCrossTruncRevUnder + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNegCrossTruncTopTopEq (_r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNegCrossTruncTopTopEq (toRefRead _r) (toRefRes res) = toRefCR (Statement.closedFormNegCrossTruncTopTopEq (_r) (res)) := by + unfold FormalConflictRange.closedFormNegCrossTruncTopTopEq Statement.closedFormNegCrossTruncTopTopEq + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormReverseTruncatedZero (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormOffsetOneTruncatedRevFullC (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormReverseTruncatedZero (r) (res)) := by + unfold FormalConflictRange.closedFormOffsetOneTruncatedRevFullC Statement.closedFormReverseTruncatedZero + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_truncatedRange (r : RangeRead) (last : Key) : + FormalConflictRange.truncatedRange (toRefRead r) (last) = toRefCR (Statement.truncatedRange (r) (last)) := by + unfold FormalConflictRange.truncatedRange Statement.truncatedRange + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_truncatedForwardClosed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.truncatedForwardClosed (toRefRead r) (toRefRes res) = toRefCR (Statement.truncatedForwardClosed (r) (res)) := by + unfold FormalConflictRange.truncatedForwardClosed Statement.truncatedForwardClosed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_gapOvershoot (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.gapOvershoot (toRefRead r) (toRefRes res) = Statement.gapOvershoot (r) (res) := by + unfold FormalConflictRange.gapOvershoot Statement.gapOvershoot + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_completeReversedClosed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.completeReversedClosed (toRefRead r) (toRefRes res) = toRefCR (Statement.completeReversedClosed (r) (res)) := by + unfold FormalConflictRange.completeReversedClosed Statement.completeReversedClosed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_completeForwardClosed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.completeForwardClosed (toRefRead r) (toRefRes res) = toRefCR (Statement.completeForwardClosed (r) (res)) := by + unfold FormalConflictRange.completeForwardClosed Statement.completeForwardClosed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_completeClosed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.completeClosed (toRefRead r) (toRefRes res) = toRefCR (Statement.completeClosed (r) (res)) := by + unfold FormalConflictRange.completeClosed Statement.completeClosed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormOffsetOneTruncated (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormOffsetOneTruncated (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormOffsetOneTruncated (r) (res)) := by + unfold FormalConflictRange.closedFormOffsetOneTruncated Statement.closedFormOffsetOneTruncated + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_topRange (r : RangeRead) : + FormalConflictRange.topRange (toRefRead r) = toRefCR (Statement.topRange (r)) := by + unfold FormalConflictRange.topRange Statement.topRange + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormOffsetOneRev (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormOffsetOneRev (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormOffsetOneRev (r) (res)) := by + unfold FormalConflictRange.closedFormOffsetOneRev Statement.closedFormOffsetOneRev + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormOffsetOne (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormOffsetOne (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormOffsetOne (r) (res)) := by + unfold FormalConflictRange.closedFormOffsetOne Statement.closedFormOffsetOne + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormOffsetOneAny (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormOffsetOneAny (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormOffsetOneAny (r) (res)) := by + unfold FormalConflictRange.closedFormOffsetOneAny Statement.closedFormOffsetOneAny + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormOffsetOneUniversal (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormOffsetOneUniversal (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormOffsetOneUniversal (r) (res)) := by + unfold FormalConflictRange.closedFormOffsetOneUniversal Statement.closedFormOffsetOneUniversal + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormOffsetOneTotal (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormOffsetOneTotal (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormOffsetOneTotal (r) (res)) := by + unfold FormalConflictRange.closedFormOffsetOneTotal Statement.closedFormOffsetOneTotal + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_nonposRevReduced (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.nonposRevReduced (toRefRead r) (toRefRes res) = toRefRead (Statement.nonposRevReduced (r) (res)) := by + unfold FormalConflictRange.nonposRevReduced Statement.nonposRevReduced + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormNonposRevTrunc (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormNonposRevTrunc (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormNonposRevTrunc (r) (res)) := by + unfold FormalConflictRange.closedFormNonposRevTrunc Statement.closedFormNonposRevTrunc + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossEndOneLong (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossEndOneLong (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossEndOneLong (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossEndOneLong Statement.closedFormZeroCrossEndOneLong + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossEnd (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossEndNonpos (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossEnd (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossEndNonpos Statement.closedFormZeroCrossEnd + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_crossedOvershootGeneral (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.crossedOvershootGeneral (toRefRead r) (toRefRes res) = Statement.crossedOvershootGeneral (r) (res) := by + unfold FormalConflictRange.crossedOvershootGeneral Statement.crossedOvershootGeneral + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_completeForwardCrossedGeneral (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.completeForwardCrossedGeneral (toRefRead r) (toRefRes res) = toRefCR (Statement.completeForwardCrossedGeneral (r) (res)) := by + unfold FormalConflictRange.completeForwardCrossedGeneral Statement.completeForwardCrossedGeneral + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_zeroBeginAnchor (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.zeroBeginAnchor (toRefRead r) (toRefRes res) = toRefRead (Statement.zeroBeginAnchor (r) (res)) := by + unfold FormalConflictRange.zeroBeginAnchor Statement.zeroBeginAnchor + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_zeroCrossLongAnchor (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.zeroCrossLongAnchor (toRefRead r) (toRefRes res) = toRefCR (Statement.zeroCrossLongAnchor (r) (res)) := by + unfold FormalConflictRange.zeroCrossLongAnchor Statement.zeroCrossLongAnchor + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossLong (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossLong (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossLong (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossLong Statement.closedFormZeroCrossLong + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossUnderEnd (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossUnderEnd (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossUnderEnd (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossUnderEnd Statement.closedFormZeroCrossUnderEnd + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossComplete (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossComplete (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossComplete (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossComplete Statement.closedFormZeroCrossComplete + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossCompleteRev (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossCompleteRev (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossCompleteRev (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossCompleteRev Statement.closedFormZeroCrossCompleteRev + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_zeroCrossUnderEnd (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.zeroCrossUnderEnd (toRefRead r) (toRefRes res) ↔ Statement.zeroCrossUnderEnd (r) (res) := by + unfold FormalConflictRange.zeroCrossUnderEnd Statement.zeroCrossUnderEnd + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossResidual (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossResidual (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossResidual (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossResidual Statement.closedFormZeroCrossResidual + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormZeroCrossTruncRevCrossed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.closedFormZeroCrossTruncRevCrossed (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormZeroCrossTruncRevCrossed (r) (res)) := by + unfold FormalConflictRange.closedFormZeroCrossTruncRevCrossed Statement.closedFormZeroCrossTruncRevCrossed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_AlwaysEmpty (r : RangeRead) : + FormalConflictRange.AlwaysEmpty (toRefRead r) ↔ Statement.AlwaysEmpty (r) := by + rw [FormalConflictRange.alwaysEmpty_iff_not_countFeasible, + FormalConflictRange.countFeasible_iff_int, + FormalConflictRange.countFeasibleInt_iff_closedForm] + simp [FormalConflictRange.closedForm, Statement.AlwaysEmpty, Statement.bottom] + +@[simp] theorem tr_emptyRange (r : RangeRead) : + FormalConflictRange.emptyRange (toRefRead r) = toRefCR (Statement.emptyRange (r)) := by + unfold FormalConflictRange.emptyRange Statement.emptyRange + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_crossedRangeGeneral (r : RangeRead) : + FormalConflictRange.crossedRangeGeneral (toRefRead r) = toRefCR (Statement.crossedRangeGeneral (r)) := by + unfold FormalConflictRange.crossedRangeGeneral Statement.crossedRangeGeneral + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_emptyValue (r : RangeRead) : + FormalConflictRange.emptyValue (toRefRead r) = toRefCR (Statement.emptyValue (r)) := by + unfold FormalConflictRange.emptyValue Statement.emptyValue + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormBegin (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactBegin (toRefRead r) (toRefRes res) = Statement.closedFormBegin (r) (res) := by + unfold FormalConflictRange.compactBegin Statement.closedFormBegin + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormFinite (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactFinite (toRefRead r) (toRefRes res) ↔ Statement.closedFormFinite (r) (res) := by + unfold FormalConflictRange.compactFinite Statement.closedFormFinite + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormCompleteForward (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactCompleteForward (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormCompleteForward (r) (res)) := by + unfold FormalConflictRange.compactCompleteForward Statement.closedFormCompleteForward + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_normalizeCompleteResult (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.normalizeCompleteResult (toRefRead r) (toRefRes res) = toRefRes (Statement.normalizeCompleteResult (r) (res)) := by + unfold FormalConflictRange.normalizeCompleteResult Statement.normalizeCompleteResult + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormComplete (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactComplete (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormComplete (r) (res)) := by + unfold FormalConflictRange.compactComplete Statement.closedFormComplete + have hne' : (Statement.normalizeCompleteResult r res).keys ≠ [] := + normalizeCompleteResult_keys_ne r res hne + exact tr_closedFormCompleteForward (Statement.forwardRead r) (Statement.normalizeCompleteResult r res) hne' + +@[simp] theorem tr_closedFormForwardCapacity (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactForwardCapacity (toRefRead r) (toRefRes res) = Statement.closedFormForwardCapacity (r) (res) := by + unfold FormalConflictRange.compactForwardCapacity Statement.closedFormForwardCapacity + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormForwardTruncated (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactForwardTruncated (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormForwardTruncated (r) (res)) := by + unfold FormalConflictRange.compactForwardTruncated Statement.closedFormForwardTruncated + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormReverseCount (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactReverseCount (toRefRead r) (toRefRes res) = Statement.closedFormReverseCount (r) (res) := by + unfold FormalConflictRange.compactReverseCount Statement.closedFormReverseCount + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormReverseFinite (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactReverseFinite (toRefRead r) (toRefRes res) ↔ Statement.closedFormReverseFinite (r) (res) := by + unfold FormalConflictRange.compactReverseFinite Statement.closedFormReverseFinite + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormCrossedRoom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactCrossedRoom (toRefRead r) (toRefRes res) ↔ Statement.closedFormCrossedRoom (r) (res) := by + unfold FormalConflictRange.compactCrossedRoom Statement.closedFormCrossedRoom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormReverseTruncatedSkip (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactReverseTruncatedSkip (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormReverseTruncatedSkip (r) (res)) := by + unfold FormalConflictRange.compactReverseTruncatedSkip Statement.closedFormReverseTruncatedSkip + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormReverseTruncated (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactReverseTruncated (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormReverseTruncated (r) (res)) := by + unfold FormalConflictRange.compactReverseTruncated Statement.closedFormReverseTruncated + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormPositive (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactPositive (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormPositive (r) (res)) := by + unfold FormalConflictRange.compactPositive Statement.closedFormPositive + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormClamped (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactClamped (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormClamped (r) (res)) := by + unfold FormalConflictRange.compactClamped Statement.closedFormClamped + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_zeroCrossOvershootComplete (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.zeroCrossOvershootComplete (toRefRead r) (toRefRes res) ↔ Statement.zeroCrossOvershootComplete (r) (res) := by + unfold FormalConflictRange.zeroCrossOvershootComplete Statement.zeroCrossOvershootComplete + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossOvershoot (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossOvershoot (toRefRead r) (toRefRes res) ↔ Statement.negCrossOvershoot (r) (res) := by + unfold FormalConflictRange.negCrossOvershoot Statement.negCrossOvershoot + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_crossedCompleteOvershoot (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.crossedCompleteOvershoot (toRefRead r) (toRefRes res) ↔ Statement.crossedCompleteOvershoot (r) (res) := by + unfold FormalConflictRange.crossedCompleteOvershoot Statement.crossedCompleteOvershoot + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_crossedCompleteEndLeOne (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.crossedCompleteEndLeOne (toRefRead r) (toRefRes res) ↔ Statement.crossedCompleteEndLeOne (r) (res) := by + unfold FormalConflictRange.crossedCompleteEndLeOne Statement.crossedCompleteEndLeOne + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_zeroCrossLongTop (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.zeroCrossLongTop (toRefRead r) (toRefRes res) ↔ Statement.zeroCrossLongTop (r) (res) := by + unfold FormalConflictRange.zeroCrossLongTop Statement.zeroCrossLongTop + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_zeroCrossLongFinite (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.zeroCrossLongFinite (toRefRead r) (toRefRes res) ↔ Statement.zeroCrossLongFinite (r) (res) := by + unfold FormalConflictRange.zeroCrossLongFinite Statement.zeroCrossLongFinite + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_crossedCompleteBeginZero (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.crossedCompleteBeginZero (toRefRead r) (toRefRes res) ↔ Statement.crossedCompleteBeginZero (r) (res) := by + unfold FormalConflictRange.crossedCompleteBeginZero Statement.crossedCompleteBeginZero + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_crossedCompleteClamp (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.crossedCompleteClamp (toRefRead r) (toRefRes res) ↔ Statement.crossedCompleteClamp (r) (res) := by + unfold FormalConflictRange.crossedCompleteClamp Statement.crossedCompleteClamp + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormCrossedComplete (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactCrossedComplete (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormCrossedComplete (r) (res)) := by + unfold FormalConflictRange.compactCrossedComplete Statement.closedFormCrossedComplete + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_zeroCrossTruncRevCrossed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.zeroCrossTruncRevCrossed (toRefRead r) (toRefRes res) ↔ Statement.zeroCrossTruncRevCrossed (r) (res) := by + unfold FormalConflictRange.zeroCrossTruncRevCrossed Statement.zeroCrossTruncRevCrossed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevUnder (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevUnder (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevUnder (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevUnder Statement.negCrossTruncRevUnder + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevCrossedNonposSmall (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevCrossedNonposSmall (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevCrossedNonposSmall (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevCrossedNonposSmall Statement.negCrossTruncRevCrossedNonposSmall + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevCrossedSmall (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevCrossedSmall (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevCrossedSmall (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevCrossedSmall Statement.negCrossTruncRevCrossedSmall + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_revTruncLeOneLow (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.revTruncLeOneLow (toRefRead r) (toRefRes res) ↔ Statement.revTruncLeOneLow (r) (res) := by + unfold FormalConflictRange.revTruncLeOneLow Statement.revTruncLeOneLow + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevBelowEnd (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevBelowEnd (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevBelowEnd (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevBelowEnd Statement.negCrossTruncRevBelowEnd + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevResidualRoom (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevResidualRoom (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevResidualRoom (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevResidualRoom Statement.negCrossTruncRevResidualRoom + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevResidualTopCrossedSmallGap (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevResidualTopCrossedSmallGap (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevResidualTopCrossedSmallGap (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevResidualTopCrossedSmallGap Statement.negCrossTruncRevResidualTopCrossedSmallGap + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevResidualTopCrossed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevResidualTopCrossed (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevResidualTopCrossed (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevResidualTopCrossed Statement.negCrossTruncRevResidualTopCrossed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_revTruncGeTwoTop (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.revTruncGeTwoTop (toRefRead r) (toRefRes res) ↔ Statement.revTruncGeTwoTop (r) (res) := by + unfold FormalConflictRange.revTruncGeTwoTop Statement.revTruncGeTwoTop + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncRevResidualTopLargeCrossed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncRevResidualTopLargeCrossed (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncRevResidualTopLargeCrossed (r) (res) := by + unfold FormalConflictRange.negCrossTruncRevResidualTopLargeCrossed Statement.negCrossTruncRevResidualTopLargeCrossed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_revTruncGeTwoLarge (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.revTruncGeTwoLarge (toRefRead r) (toRefRes res) ↔ Statement.revTruncGeTwoLarge (r) (res) := by + unfold FormalConflictRange.revTruncGeTwoLarge Statement.revTruncGeTwoLarge + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncTopTop (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncTopTop (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncTopTop (r) (res) := by + unfold FormalConflictRange.negCrossTruncTopTop Statement.negCrossTruncTopTop + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_negCrossTruncTopTopEq (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.negCrossTruncTopTopEq (toRefRead r) (toRefRes res) ↔ Statement.negCrossTruncTopTopEq (r) (res) := by + unfold FormalConflictRange.negCrossTruncTopTopEq Statement.negCrossTruncTopTopEq + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormClampSlack (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactClampSlack (toRefRead r) (toRefRes res) = Statement.closedFormClampSlack (r) (res) := by + unfold FormalConflictRange.compactClampSlack Statement.closedFormClampSlack + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormClampGapSmall (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactClampGapSmall (toRefRead r) (toRefRes res) ↔ Statement.closedFormClampGapSmall (r) (res) := by + unfold FormalConflictRange.compactClampGapSmall Statement.closedFormClampGapSmall + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormClampGapExactSaturated (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactClampGapExactSaturated (toRefRead r) (toRefRes res) ↔ Statement.closedFormClampGapExactSaturated (r) (res) := by + unfold FormalConflictRange.compactClampGapExactSaturated Statement.closedFormClampGapExactSaturated + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormClampGapLow (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactClampGapLow (toRefRead r) (toRefRes res) ↔ Statement.closedFormClampGapLow (r) (res) := by + unfold FormalConflictRange.compactClampGapLow Statement.closedFormClampGapLow + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormClampUpper (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactClampUpper (toRefRead r) (toRefRes res) = Statement.closedFormClampUpper (r) (res) := by + unfold FormalConflictRange.compactClampUpper Statement.closedFormClampUpper + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormForwardSaturated (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactForwardSaturated (toRefRead r) (toRefRes res) ↔ Statement.closedFormForwardSaturated (r) (res) := by + unfold FormalConflictRange.compactForwardSaturated Statement.closedFormForwardSaturated + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormForwardTruncatedNonpos (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactForwardTruncatedNonpos (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormForwardTruncatedNonpos (r) (res)) := by + unfold FormalConflictRange.compactForwardTruncatedNonpos Statement.closedFormForwardTruncatedNonpos + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +@[simp] theorem tr_closedFormCrossed (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : + FormalConflictRange.compactCrossed (toRefRead r) (toRefRes res) = toRefCR (Statement.closedFormCrossed (r) (res)) := by + unfold FormalConflictRange.compactCrossed Statement.closedFormCrossed + simp [Statement.firstKey, Statement.lastKey, Statement.first, Statement.last, Statement.afterFirst, Statement.afterLast, Statement.bottom, Statement.range, Statement.forwardRead, Statement.reversedResult, Statement.crossedBegin, Statement.completeClosedWith, Statement.closedFormOffsetOneWith, Statement.closedFormSplit, Statement.residualE, Statement.residualT, Statement.residualB, Statement.residualD, FormalConflictRange.compactSplit, FormalConflictRange.residualE, FormalConflictRange.residualT, FormalConflictRange.residualB, FormalConflictRange.residualD, FormalConflictRange.endOvershoot, FormalConflictRange.reportedAtEnd, hne] + all_goals (try rfl) + all_goals (try (intros; rfl)) + all_goals (try (intros; exact Iff.rfl)) + all_goals (try (intros; simp_all)) + all_goals (try (split_ifs <;> simp_all)) + all_goals (try (split <;> simp_all)) + all_goals (try order) + all_goals (try omega) + all_goals (try contradiction) + all_goals (try (exfalso; order)) + all_goals (try (exfalso; omega)) + all_goals (try rfl) + +end Statement \ No newline at end of file diff --git a/FormalConflictRange/Algorithm.lean b/FormalConflictRange/Algorithm.lean new file mode 100644 index 0000000..62f62ff --- /dev/null +++ b/FormalConflictRange/Algorithm.lean @@ -0,0 +1,4236 @@ +/- +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.Empty +import FormalConflictRange.Nonpos +import FormalConflictRange.Reduction + +/-! +# The conflict-range algorithm + +The conflict-range inference has two layers. + +* A certified-region partial algorithm `conflictRangePartial`, built on the + (unconditional) empty-result theory of `FormalConflictRange.Empty` and + `FormalConflictRange.Nonpos`: a result with no keys is answered by the empty-result + value `emptyValue`, and the remaining certified offset cases are handled as in + `FormalConflictRange.Sound`. **This is the object of record**: the correctness + statement is `conflictRangePartial_spec`, and the open frontier is exactly the + set of inputs on which it returns `none`. When that set is empty the algorithm is + total and is promoted to the `ConflictRange`-valued algorithm. +* The provisional totalization `conflictRangeVal`, assembled by *reduction to the + empty reads* rather than case analysis. It is the every-input wrapper used to + *state* the general reduction: it dispatches on emptiness, truncation, and + traversal and returns a `ConflictRange` on the certified regions. Off them its + value is a placeholder whose hull fallback is not sound in general (see + `FormalConflictRange.Counterexample`), and its `_spec` theorems are relative to + the open `EmptyTheoryComplete`. It is kept only until `conflictRangePartial` is + total, at which point it is replaced. It dispatches as follows: + + - an empty result is answered by the empty theory's value `emptyValue`; + - a forward truncated result is `[w, keyAfter kₙ)`: the begin selector's empty + contribution plus the reported keys, with the bare successor upper end; + - a reverse truncated result is `[kₙ, u)`: the reported keys plus the end + selector's empty contribution, the begin selector dropping out; + - a complete result in the canonical region `1 ≤ begin.offset ∧ end.offset ≤ 1` is + the canonical range `[begin.key, end.key)` (`completeValue`), which is minimal + outright by `minimal_canonicalRange_of_end_offset_le_one_general`; + - a complete result in the overshoot region `1 ≤ begin.offset` with every reported + key below `end.key` is the unbounded top range `[begin.key, ⊤)`, minimal by + `minimal_topRange_of_end_offset_ge_two_general`; + - a complete result elsewhere is the hull of the two end ranges, + `[w, k₀) ∪ [keyAfter kₙ, u)`. + + Here `w = lowBound r` and `u = highBound r` are the empty theory's begin and end + endpoints (`⊥` or `begin.key`; `end.key`, or the crossed/overshoot answer). + + The algorithm reduces to the single `EmptyInference` oracle: `emptyValue` is minimal + for every realizable empty result. The empty branch is discharged outright + (`minimal_emptyValue_of_emptyInference`); the reported interval is necessary + unconditionally (`necessaryRange_reportedRange`, from `necessary_of_mem_result` and + convexity), so a complete result's necessity is entirely the two boundary ranges + stretched to the complete value's endpoints (`BoundaryNecessity`), the content the + empty inference supplies as `ψ` at the two boundary reads. Hull soundness is derived + from `EmptyInference` for all bridge factors except the bracket (`BracketFactor`), + which is the notes' observability residue. The complete branch is then + `minimal_completeRange_of_factors`/`_rev`, with the interior derived rather than + assumed; `GeneralObligation` adds only the two truncated dispatch values. Its + conditional correctness statement is `conflictRangeVal_spec` (relative to + `EmptyTheoryComplete`); the algorithm of record remains `conflictRangePartial`. In the + canonical region (`begin.offset = 1`, `end.offset ≤ 1`, canonical endpoints) the complete + branch is unconditional + (`minimal_completeRange_canonical`), needing neither boundary hypothesis. There are + no `sorry`s: the open content appears only as those explicit hypotheses. + + The reduction identity itself is `necessaryRange_completeRange_of_reported`: the + complete value's necessary range is the lower boundary, the reported interval, and the + upper boundary, in that order, with the middle supplied by pointwise reported necessity + and convexity. Because reported necessity is unconditional + (`necessary_of_mem_result`), the general complete case is exactly "reported keys + the + two empty ends" (`minimal_completeRange_of_emptyEnds`). The boundary half is stated at + the witness level too: `LowerJointBase`/`UpperJointBase` (and their reversed twins) are + the flip witnesses the factor-toggle needs, `BoundaryJointBases` packages them by + traversal, and `minimal_completeRange_of_factors_joint`/`_rev` and + `minimal_completeRange_of_witness_joint`/`_rev` assemble the complete branch from them. + `EmptyInferenceWitness` records the empty theory in the witness-exposing form the graft + consumes and implies `EmptyInference` (`emptyInference_of_witness`). The one genuine + extraction — turning an abstract `Minimal` for a boundary read into a full-compatible + witness database — remains the length-sensitive joint-base graft; it is the reason the + boundary content is a hypothesis and not a consequence of `EmptyInference` alone. (It is + in fact *false*, not merely unproved: + `FormalConflictRange.Counterexample.cw_notBoundaryJointBases` refutes `BoundaryJointBases` + on the `cwRead`/`cwRes` witness.) + + The pushed-down interface makes the confinement of offsets explicit. The complete + branch's nonempty content is `CompleteObligation`, whose fields are the generic graft + (`joint : BoundaryJointBases`) and the bracket (`bracket : BracketFactor`); correctness + is `conflictRangeVal_spec_of_witness`, taking only the witness oracle, the two + obligations, and `Readable` — no offset, no `EmptyInference`, no finished + `BoundaryNecessity`. The abstract `conflictRangeVal_spec` remains as the legacy form. + + Finally, `EmptyTheoryComplete` bundles the entire residual into a single hypothesis: + the witness-exposing empty oracle, the generic graft, the bracket gap split, and the + truncated one-sided graft. From it, `conflictRangeVal_spec_complete` proves the general + algorithm correct with `Readable` the only other hypothesis, and + `exists_minimal_conflict_range_of_complete` gives the existence conjecture. The graft + fields (`joint`, `truncated`) are guarded by `Readable`: a bridge database exists only + when the reported keys are the complete keys of some database, so for an unrealizable + pair `BoundaryJointBases`/`GeneralObligation` are outright false (a duplicate reported + key can never satisfy `midEq`). The unconditional headline claim is *not* proved here: it + is stated with explicit `sorry`s in `FormalConflictRange.Open` (which is built but + deliberately not imported by this module). All three non-witness fields of + `EmptyTheoryComplete` are in fact *refuted* — `bracket` and `truncated` as before, and + `joint` (`BoundaryJointBases`) by the same `cwRead`/`cwRes` witness that refutes the hull + (`FormalConflictRange.Counterexample.cw_notBoundaryJointBases`), so the reduction is + uninhabited rather than merely unproved. + + On the empty theory itself, the witness-exposing oracle is proved at + `begin.offset = 1` (`emptyWitness_beginOne`), throughout the whole `end.offset ≤ 1` + region at any positive begin offset (`emptyWitness_underEnd_gen`, from the slice + characterization and the room bound in `FormalConflictRange.Empty`), and throughout + the overshoot region (`emptyWitness_overshoot`, `begin.offset ≥ 1`, `end.offset ≥ 2`, + any traversal). The empty-result case proofs are restated at the witness level + (`emptyWitness_underEnd`, `emptyWitness_underEnd_general`, `emptyWitness_crossFree`, + `emptyWitness_crossed_beginLe`, `emptyWitness_crossed_interval`, and the + positive-offset `emptyWitness_beginLe_of_le_general`, `emptyWitness_beginLe_general`, + `emptyWitness_mem_interval_general`), and traversal invariance transports them. The + nonpositive begin offset now has its own `emptyValue` candidate (`FormalConflictRange.Nonpos`). + An overshooting end (`end.offset ≥ 2`) is handled uniformly in the selector order: the + full range `[⊥, ⊤)` is sound and minimal (`minimal_fullRange_of_nonpos_overshoot_all`), + with every key witnessed by the singleton (`emptyWitness_nonpos_overshoot`). With + `end.offset = 1` the value is the bottom range `[⊥, end.key)` cross-free and in the short + gap, and `[⊥, begin.key)` in the crossed long gap, with flip witnesses throughout the + region (`emptyWitness_nonpos_end_one_crossFree`, `emptyWitness_nonpos_end_one_crossedShort`, + `emptyWitness_nonpos_end_one_crossedLong`). The cross-free `end.offset ≤ 0` region is also + closed (`emptyWitness_nonpos_end_nonpos_crossFree`): the value is `[⊥, end.key)`, its keys + split into the region where the criterion collapses to `cnt end.key ≤ 1 - end.offset` and + the region where the gap room `1 - end.offset - (1 - begin.offset) + 1 ≤ intervalCard + begin.key end.key` supplies it. The last nonpositive begin-and-end region with crossed + selectors -- where the end anchor can clamp as well -- is now closed too: off the trivially + empty reads `1 - begin.offset > 1 - end.offset`, and the gap splits into the short-gap case + `intervalCard end.key begin.key < end.offset - begin.offset`, whose criterion collapses to + `cnt end.key ≤ 1 - end.offset` (`emptyWitness_nonpos_crossed_short`, singleton witnesses), + and the long-gap case `emptyWitness_nonpos_crossed_long` with the coupled count witnesses + (`emptyWitness_nonpos_crossed_long_below`, `_interval`). The residual + `EmptyInferenceWitnessResidual` is therefore vacuous: the whole oracle is + `emptyInferenceWitness`, unconditionally, and `emptyInference : EmptyInference` follows. + What remains of the empty theory is only the graft and gap-split fields of + `EmptyTheoryComplete`, packaged by `emptyTheoryComplete_of_witness`; those fields are the + open `sorry`s of `FormalConflictRange.Open`. The gap-split field `BracketFactor` is in + fact *false* as stated on the old hull, by the kernel-checked witness in + `FormalConflictRange.Counterexample`; `completeValue` routes the canonical region + `1 ≤ begin.offset ∧ end.offset ≤ 1` around the hull precisely so that this pair is + answered correctly without it. + +The reduction is the subject of section 7 of `notes/main.tex`. +-/ + +namespace FormalConflictRange + +/-! +## A partial algorithm: the certified cases + +The general existence conjecture is open, but the theorems above certify a region +of reads. Package them into one computable `Option`-valued function, +`conflictRangePartial`, returning `some` range exactly where a certificate exists +and `none` otherwise. This is the honest encoding of "proved only for certain +cases": it is total in type but makes absence explicit, and `none` carries no +pretense of being a range. The certificate `conflictRangePartial_spec` proves +`some cr` is minimal whenever the value is defined. + +The certified region is: an empty result (any offsets and traversal, answered by the +empty-result value `emptyValue`, whose minimality is the unconditional `emptyInference`), +or else a nonempty result with begin at offset one and either the end at offset at +most one (all traversal and truncation combinations at end offset one, and — for +`end.offset ≤ 0` — the complete results, the truncated forward results, whose canonical +range is certified by `minimal_canonicalRange_of_truncated_end_offset_nonpos`, and the +truncated reversed results, whose reversed truncated range is certified by +`minimal_truncatedRangeRev_of_end_offset_nonpos`) or an +overshooting end (`end_.offset ≥ 2`) whose reported keys all lie below `end_.key` (all +traversal and truncation combinations too), or a nonempty complete result at any positive +begin offset (the canonical range at `end.offset ≤ 1`, the unbounded top at an +overshoot), or — at any positive begin offset — a truncated *forward* result with +`end.offset ≤ 0`, or the nonempty truncated *reversed* and complete cells at `begin.offset +≥ 2` (canonical, top-range, or reversed-truncated by `end.offset`), or the cross-free +nonempty complete overshoot at `begin.offset ≤ 0` (`[⊥, ⊤)`), the cross-free nonempty +complete under-end result at `begin.offset ≤ 0` (`[⊥, end_.key)`), or the cross-free nonempty +truncated forward result at `begin.offset ≤ 0` with a pinned end (`[⊥, keyAfter last +reported)`), or the crossed nonempty complete forward overshoot at `begin.offset = 0` (the +top range from the least reported key `[head, ⊤)`), or the crossed nonempty complete forward +result at `begin.offset = 0`, `end.offset = 1` with `begin.key ≤ end_.key` (the head range +`[head, end_.key)`). The +empty branch is tried first and needs no offset hypothesis; it certifies every +realizable empty result through the boundary theory rather than the offset-one cases. +-/ + +/-- The begin selector sits at offset one, the offset at which `resolve begin` is +threshold-defined and `key_le_resolve_of_offset_one` applies. -/ +abbrev beginOne (r : RangeRead) : Prop := r.begin.offset = 1 + +/-- The end selector sits at offset one, the offset-one end case. -/ +abbrev endOne (r : RangeRead) : Prop := r.end_.offset = 1 + +/-- An overshooting end: the end offset is at least two and every reported key +lies below `end_.key`. This is the region where the overshoot certificates apply, +in all four traversal/completeness combinations. -/ +abbrev overshoot (r : RangeRead) (res : Result) : Prop := + 2 ≤ r.end_.offset ∧ res.keys ≠ [] ∧ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + +/-- An end selector at offset at most one: the end family where `resolve end_` is +the threshold-defined case of `minimal_canonicalRange_of_end_offset_le_one`, as +opposed to the offset at least two of `overshoot`. -/ +abbrev underEnd (r : RangeRead) : Prop := r.end_.offset ≤ 1 + +/-- The least reported key of a result, regardless of traversal: the head of an +ascending result, the last of a descending (reversed) one. -/ +def leastReported (r : RangeRead) (res : Result) : Key := + if r.reverse then res.keys.getLastD ([] : Key) else res.keys.headD ([] : Key) + +/-- The greatest reported key of a result, regardless of traversal: the last of an +ascending result, the head of a descending (reversed) one. -/ +def greatestReported (r : RangeRead) (res : Result) : Key := + if r.reverse then res.keys.headD ([] : Key) else res.keys.getLastD ([] : Key) + +/-- A nonempty *complete* result at a nonpositive begin offset with an overshooting +end (every reported key below `end_.key`) whose begin key lies at or below the least +reported key. On this region the unbounded full range `[⊥, ⊤)` is minimal +(`minimal_fullRange_of_nonpos_overshoot_nonempty` and its reversed twin): the empty +key is forced by `begin.key ≤ least reported`, the reported keys and everything +between them by convexity, and every key at or above the least reported key by the +overshoot necessity `necessary_of_end_offset_ge_two`. This is the cross-free half of +the nonpositive-begin nonempty region. -/ +abbrev nonposOvershootComplete (r : RangeRead) (res : Result) : Prop := + 2 ≤ r.end_.offset ∧ res.more = false ∧ res.keys ≠ [] ∧ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) + +/-- A nonempty *complete* result at a nonpositive begin offset with `end_.offset ≤ 1` whose +begin key lies at or below the least reported key. On this region the bottom range +`[⊥, end_.key)` is minimal (`minimal_bottomRange_of_nonpos_underEnd_nonempty` and its +reversed twin): the empty key is forced by `begin.key ≤ least reported`, the reported keys +and everything between them by convexity, and every key from the reported last up to +`end_.key` by the end selector's reach. This is the under-end companion of +`nonposOvershootComplete` on the nonpositive-begin nonempty complete region. -/ +abbrev nonposUnderEndComplete (r : RangeRead) (res : Result) : Prop := + r.end_.offset ≤ 1 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) + +/-- A nonempty *truncated forward* result at a nonpositive begin offset whose begin key lies +at or below the least reported key, with the end pinned above the reported keys +(`1 ≤ end_.offset` and every reported key strictly below `end_.key`). On this region the bottom +truncated range `[⊥, keyAfter last reported)` is minimal +(`minimal_truncatedRangeBottom_of_nonpos`): the empty key is forced by `begin.key ≤ least +reported`, the reported keys and everything between them by convexity, and nothing past the +successor of the reported last because the result reports a prefix. The `1 ≤ end_.offset` +condition puts `end_.key ≤ resolve end_`, so every key at or below the reported last is below +`resolve end_`; the pinning keeps the resolved end out of the value. This is the truncated +forward companion of `nonposOvershootComplete`/`nonposUnderEndComplete` on the nonpositive-begin +nonempty region. The `1 ≤ end_.offset` and `end_.key`-overshoot conditions may hold vacuously at +`end_.offset = 1`, where realizability forces the reported keys below `end_.key`. -/ +abbrev nonposFwdTruncPinned (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) ∧ + 1 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key + +/-- A nonempty *truncated reversed* result at a nonpositive begin offset whose begin key lies +at or below the least (last) reported key, with an overshooting end (`2 ≤ end_.offset` and +every reported key strictly below `end_.key`). On this region the unbounded top from the least +reported key, `[last reported, ⊤)`, is minimal +(`minimal_topOfLast_of_end_offset_ge_two_rev_nonpos`): the overshoot forces every witness +window to end at the sentinel, so compatibility depends only on the stored keys at or above the +least reported key; those keys, the reported ones, and everything convexly between them are +necessary, as is every key at or above the reported head by `necessary_of_end_offset_ge_two_rev`. +The cross-free hypothesis `begin.key ≤ least reported` replaces the `begin.offset = 1` +hypothesis of the positive-offset twin, since a nonpositive begin offset resolves at or below +`begin.key`. This is the reversed truncated overshoot companion of `nonposFwdTruncPinned` on the +nonpositive-begin nonempty region. -/ +abbrev nonposRevTruncOvershoot (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) ∧ + 2 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key + +/-- A nonempty *truncated reversed* result at a nonpositive begin offset whose begin key lies +at or below the least (last) reported key, with the end at offset one. On this region the +reversed truncated range `[last reported, end_.key)` is minimal +(`minimal_truncatedRangeRev_of_end_offset_one_nonpos`): the reported keys and everything +convexly between them are necessary, and every key at or above the reported head up to +`end_.key` by `necessary_head_to_end_key`. Soundness is that `end_.offset = 1` makes the cut +at `resolve end_` the cut at `end_.key`, so agreement on `[last, end_.key)` fixes the stored +keys the reversed prefix can observe. This is the reversed truncated under-end companion of +`nonposRevTruncOvershoot` and the nonpositive-begin companion of the offset-one reversed +truncated cell. -/ +abbrev nonposRevTruncEndOne (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) ∧ + r.end_.offset = 1 + +/-- A nonempty *complete forward* result at `begin.offset = 0` whose `begin.key` lies +strictly above the least reported key (the crossed subcase) and whose end overshoots +(`2 ≤ end_.offset`, every reported key below `end_.key`). On this region the top range +from the least reported key, `[head, ⊤)`, is minimal +(`minimal_topOfHead_of_zero_begin_crossed_overshoot`): at offset zero the begin selector +resolves onto the greatest stored key strictly below `begin.key`; compatibility forces +that key to be the reported head, so the window is `[head, ⊤)` and agreement on +`[head, ⊤)` fixes the complete keys. Every key at or above the reported last is necessary +by `necessary_of_end_offset_ge_two`, and convexity fills the gap down to the head. This +is the crossed companion of `nonposOvershootComplete`: the cross-free hypothesis +`begin.key ≤ least reported` there is here replaced by its negation. -/ +abbrev zeroCrossOvershootComplete (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = false ∧ overshoot r res ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key + +/-- A nonempty *complete forward* result at `begin.offset = 0` whose `begin.key` lies strictly +above the least reported key (the crossed subcase) and whose end sits at offset one. On this +region the range from the least reported key up to `end_.key`, `[head, end_.key)`, is minimal +(`minimal_headToEnd_of_zero_begin_crossed_endOne`): the begin selector lands on the reported +head, and with `end.offset = 1` the end selector stops at the first stored key at or above +`end_.key`, so the complete keys are exactly the stored keys in `[head, end_.key)`. Unlike the +cross-free under-end region the empty key is not forced, so the value starts at the head. The +extra hypothesis `begin.key ≤ end_.key` rules out the case where a compatible database can store +keys between `end_.key` and `begin.key` that agreement on `[head, end_.key)` cannot see; there +`[head, end_.key)` is not sound. This is the `end.offset = 1` companion of +`zeroCrossOvershootComplete`. -/ +abbrev zeroCrossEndOne (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = false ∧ + r.end_.offset = 1 ∧ res.keys ≠ [] ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key ≤ r.end_.key + +/-- A nonempty *complete forward* result at `begin.offset = 0` whose `begin.key` lies strictly +above both the least reported key (the crossed subcase) and `end_.key`, with the end at offset one. +On this region the range from the least reported key up to `begin.key`, `[head, begin.key)`, is +minimal (`minimal_headToBeginKey_of_zero_begin_crossed_endOne_long`): the begin selector lands on +the reported head, while a compatible database storing a key between `end_.key` and `begin.key` +would have the begin selector resolve above the head, so those keys are necessary and the value +ends at `begin.key` rather than `end_.key`. This is the `end_.key < begin.key` companion of +`zeroCrossEndOne`. -/ +abbrev zeroCrossEndOneLong (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = false ∧ + r.end_.offset = 1 ∧ res.keys ≠ [] ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.end_.key < r.begin.key + +/-- A nonempty *complete forward* result at `begin.offset = 0` whose `begin.key` lies strictly +above the least reported key (the crossed subcase) and whose end offset is nonpositive. On this +region the range from the least reported key up to `end_.key`, `[head, end_.key)`, is minimal +(`minimal_headToEnd_of_zero_begin_crossed_endNonpos`): agreement on it fixes the top +`(-end_.offset)` stored keys below `end_.key`, hence the end resolution, while the extra keys +between the end resolution and `end_.key` stay out of the complete window. This is the +`end.offset ≤ 0` companion of `zeroCrossEndOne` and `zeroCrossEndOneLong`. -/ +abbrev zeroCrossEndNonpos (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = false ∧ + r.end_.offset ≤ 0 ∧ res.keys ≠ [] ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key + +/-- A nonempty *complete forward* result at `begin.offset = 0`, crossed +(`begin.key` strictly above the reported head), with a positive end offset and +`begin.key ≤ end.key`. On this region the crossed read has the same compatible +databases as the read with its begin selector moved to offset one on the reported +head (`compatible_zero_crossed_iff_anchor`), so the offset-one closed form answers +it. This is the `end.offset ≥ 2` companion of `zeroCrossEndOne` on the crossed +begin-zero region with `begin.key ≤ end.key`. -/ +abbrev zeroCrossUnderEnd (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = false ∧ + 2 ≤ r.end_.offset ∧ res.keys ≠ [] ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key ≤ r.end_.key + +/-- A nonempty *complete forward* result at `begin.offset = 0` with `begin.key = ⊤` +and a positive end offset of at least two. On this region the begin selector resolves +onto the greatest stored key, compatibility forces the result to be the singleton +reported head with that head maximal, and the end selector resolves past the end. The +minimal range is the closed form `closedFormZeroCrossLong`, `[end.key, ⊤)` when +`end.key < head` and the end capacity reaches it, and `[head, ⊤)` otherwise. This is +the `begin.key = ⊤` tail of the crossed complete begin-zero long cell. -/ +abbrev zeroCrossLongTop (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = false ∧ + 2 ≤ r.end_.offset ∧ res.keys ≠ [] ∧ r.begin.key = (⊤ : WithTop Key) + +/-- A nonempty *complete forward* result at `begin.offset = 0`, crossed +(`begin.key` strictly above the reported head), in the long cell `end_.key < +begin.key`, with finite `begin.key`. The minimal range is again the closed form +`closedFormZeroCrossLong`, the anchor's range with the upper endpoint raised to +`begin.key` (`minimal_closedForm_of_zero_cross_long_finite`). This is the finite +`begin.key` companion of `zeroCrossLongTop`. -/ +abbrev zeroCrossLongFinite (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = false ∧ + 2 ≤ r.end_.offset ∧ res.keys ≠ [] ∧ r.begin.key ≠ (⊤ : WithTop Key) ∧ + r.end_.key < r.begin.key ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key + +/-- A nonempty *complete reversed* result at `begin.offset = 0`, crossed +(`begin.key` strictly above the least reported key). Flipping the traversal gives a +forward crossed complete result at begin offset zero, so the closed form +`closedFormZeroCrossCompleteRev` answers it (`minimal_closedForm_of_zeroCross_complete_rev`). -/ +abbrev zeroCrossCompleteRev (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ res.more = false ∧ r.reverse = true ∧ + res.keys ≠ [] ∧ ((leastReported r res : Key) : WithTop Key) < r.begin.key + +/-! +## The empty-result value and its inference + +The empty-result value `emptyValue` and the standing hypothesis `EmptyInference` are +declared here, before `conflictRangePartial`, because the algorithm of record consults +them directly: an empty result is answered by `emptyValue r`, and the certified branch +of the partial algorithm is exactly the empty-result inference. The witness oracle and +the unconditional proof of `EmptyInference` live later in this file, so the inference is +carried as a hypothesis here and discharged in `conflictRangePartial_spec` below. +-/ + +/-- The empty result's value. At a nonpositive begin offset the canonical range is +unsound (`compatible_empty_iff_clamp`), so the theory uses a different candidate. An +overshooting end offset (`2 ≤ end.offset`) is empty exactly for the empty database, +whatever the selector order, so the full range `[⊥, ⊤)` is minimal; the test precedes the +cross-free/crossed split. Otherwise (`end.offset ≤ 1`) the begin anchor may clamp to `⊥`, + so the value is the bottom range `[⊥, end.key)` cross-free, and crossed the bottom range + `[⊥, max begin.key end.key)` except when the gap is short + (`intervalCard end.key begin.key < end.offset - begin.offset`), where the begin anchor + can never be lifted and the value is `[⊥, end.key)`. At a positive begin offset it is + the canonical +range for an end at offset at most one, the top range cross-free, and the crossed candidate +otherwise. -/ +def emptyValue (r : RangeRead) : ConflictRange := + if AlwaysEmpty r then emptyRange r + else if r.begin.offset ≤ 0 then + if 2 ≤ r.end_.offset then ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + else if r.begin.key ≤ r.end_.key then ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + else if intervalCard r.end_.key r.begin.key < + (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) then + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + else ⟨(([] : Key) : WithTop Key), max r.begin.key r.end_.key⟩ + else if r.end_.offset ≤ 1 then canonicalRange r + else if r.begin.key ≤ r.end_.key then topRange r + else crossedRangeGeneral r + +/-- **The empty-result inference, as a standing hypothesis.** `emptyValue` returns a +minimal sound conflict range for every realizable read whose complete result is empty. +It is discharged unconditionally later in this file (`emptyInference`); it is stated +here so that `conflictRangePartial` can consult `emptyValue` directly. -/ +def EmptyInference : Prop := + ∀ r' : RangeRead, Readable r' { keys := [], more := false } → + Minimal r' { keys := [], more := false } (emptyValue r') + +/-- **The empty branch, from the empty inference.** A realizable result with no keys +must be `⟨[], false⟩` — a truncated (`more = true`) result cannot be empty — so the +oracle applies directly. -/ +theorem minimal_emptyValue_of_emptyInference (h : EmptyInference) + {r : RangeRead} {res : Result} (hread : Readable r res) (hempty : res.keys = []) : + Minimal r res (emptyValue r) := by + obtain ⟨db, hcomp⟩ := hread + have hmore : res.more = false := by + rcases hcomp.2 with ⟨hm, _, hne⟩ | ⟨hm, _⟩ + · exact absurd hempty hne + · exact hm + have hres : res = ({ keys := [], more := false } : Result) := by + cases res + simp_all + subst hres + exact h r ⟨db, hcomp⟩ + +/-- The partial conflict-range algorithm. It returns `some` range on the certified +region and `none` elsewhere. A result with no keys is answered by the empty-result +value `emptyValue r`, which is minimal for every realizable empty result +(`minimal_emptyValue_of_emptyInference`); this subsumes the trivially empty reads and +covers the empty result at *every* offset and traversal. For a nonempty result at begin +offset one it splits by the end selector. At end offset one the offset-one algorithm +returns the canonical range for a complete result, the truncated range for a truncated +non-reversed result, and the reversed truncated range from the last reported key for a +truncated reversed result. At an overshooting end it returns the unbounded top for a +complete result — reversed or not — the truncated range for a truncated non-reversed +result, and the top from the last reported key for a truncated reversed result. Elsewhere +(`end_.offset ≤ 0`) a complete result returns the canonical range, a truncated +*forward* result returns the canonical range +(`minimal_canonicalRange_of_truncated_end_offset_nonpos`), which is where FoundationDB's +bare successor is unsound, and a truncated *reversed* result returns the reversed +truncated range `[last reported, end.key)` +(`minimal_truncatedRangeRev_of_end_offset_nonpos`). + +The guards form an exhaustive, mutually exclusive `if _h : …` chain. The top level +splits on `res.keys = []`, then `beginOne`; within begin offset one it splits on +`endOne`, then `overshoot`, then `underEnd`, leaving the uncertified region — a +non-overshooting end at an offset at least two — as a plain `none` branch. A separate +`1 ≤ begin.offset` branch +certifies every nonempty complete result there — canonical for `end.offset ≤ 1`, the +unbounded top for an overshoot — and a truncated non-reversed overshoot +(`minimal_truncatedRange_of_end_offset_ge_two_general`); for `end.offset ≤ 0` the truncated +*forward* result too, through the general positive-offset theorems, and for `end.offset = 1` +the truncated *forward* result (`minimal_truncatedRange_of_end_offset_one`). At begin offset +at least two the truncated *reversed* result is the canonical range for `end.offset ≤ 1` +(`minimal_canonicalRange_of_end_offset_le_one_rev_more`) and the unbounded top for an +overshoot (`minimal_topRange_of_end_offset_ge_two_rev_more`). Finally, at +`begin.offset ≤ 0` the cross-free nonempty complete overshoot returns the full range +`[⊥, ⊤)` (`minimal_fullRange_of_nonpos_overshoot_nonempty` and its reversed twin), the +cross-free nonempty complete under-end result returns the bottom range `[⊥, end_.key)` +(`minimal_bottomRange_of_nonpos_underEnd_nonempty` and its reversed twin), and the +cross-free nonempty *truncated forward* result with a pinned end (`1 ≤ end_.offset` and +every reported key below `end_.key`) returns the bottom truncated range +`[⊥, keyAfter last reported)` (`minimal_truncatedRangeBottom_of_nonpos`), and the +cross-free nonempty *truncated reversed* result with an overshooting end +(`2 ≤ end_.offset` and every reported key below `end_.key`) returns the unbounded top +from the least reported key, `[last reported, ⊤)` +(`minimal_topOfLast_of_end_offset_ge_two_rev_nonpos`), and the cross-free nonempty +*truncated reversed* result with `end_.offset = 1` returns the reversed truncated range +`[last reported, end_.key)` (`minimal_truncatedRangeRev_of_end_offset_one_nonpos`), and the +crossed nonempty complete forward result at `begin.offset = 0`, `end.offset = 1` with +`begin.key ≤ end_.key` returns the head range `[head, end_.key)` +(`minimal_headToEnd_of_zero_begin_crossed_endOne`), and with `end_.key < begin.key` the +head-to-`begin.key` range `[head, begin.key)` +(`minimal_headToBeginKey_of_zero_begin_crossed_endOne_long`). The +remaining `none` branches are the non-overshooting `end.offset ≥ 2` results and the +crossed nonempty results at `begin.offset ≤ 0` beyond these two certified cells. +Naming each guard, rather than sharing one catch-all, keeps +the branches disjoint and makes deleting or weakening a guard leave a hole the definition +cannot fill. -/ +def conflictRangePartial (r : RangeRead) (res : Result) : Option ConflictRange := + if _h : res.keys = [] then + some (emptyValue r) + else if _h : beginOne r then + if _h : endOne r then + some (conflictRangeOffsetOne r res) + else if _h : overshoot r res then + if _h : res.more = false then + some (topRange r) + else if _h : r.reverse = false then + some (truncatedRange r (res.keys.getLastD ([] : Key))) + else + some (topOfLast (res.keys.getLastD ([] : Key))) + else if _h : underEnd r then + -- `end_.offset ≤ 0` (the `end.offset = 1` case was taken by `endOne` above). A + -- complete result has the canonical range + -- (`minimal_canonicalRange_of_end_offset_le_one_general`). A truncated + -- (`more = true`) forward result now has it too + -- (`minimal_canonicalRange_of_truncated_end_offset_nonpos`): FoundationDB's bare + -- successor `[begin.key, keyAfter (last reported))` is *unsound* there + -- (`FormalConflictRange.Counterexample.tr_notSound`) because the resolved end + -- clamps below `end_.key`, but the canonical upper end `end_.key` is correct. A + -- truncated *reversed* result has the reversed truncated range + -- `[last reported, end_.key)` (`minimal_truncatedRangeRev_of_end_offset_nonpos`): + -- the top `(1 - end_.offset)` canonical keys are dropped, but filtering at or + -- above the last reported key selects the same suffix, so agreement on + -- `[last reported, end_.key)` still fixes compatibility. + if _h : res.more = false then + some (canonicalRange r) + else if _h : r.reverse = false then + some (canonicalRange r) + else + some (truncatedRangeRev r (res.keys.getLastD ([] : Key))) + else + -- `end_.offset ≥ 2` without overshoot. FoundationDB sends + -- `[begin.key, keyAfter (last reported))` non-reversed and + -- `[more ? last reported : begin.key, keyAfter (head reported))` reversed + -- (`getRangeFinished`, `NativeAPI.actor.cpp:4770-4785`). Left uncertified: + -- an `end_.offset ≥ 2` can run past the data even when a reported key lies at + -- or above `end_.key`, so the end anchor is not always observable and the + -- candidate need not be sound. The cell has no single shape: the `cwMin` value + -- (bare successor of the greatest reported key) is minimal at `end.offset = 2` + -- but fails for larger slack, where the window runs past the reported last + -- (`FormalConflictRange.Counterexample.ns_cand_notSound`, true value the top). + none /- end offset at least two but not overshooting: no certificate -/ + else if _h : 1 ≤ r.begin.offset then + -- A nonempty result at `begin.offset ≥ 1` (here `begin.offset ≠ 1`, so at least two) + -- and `end.offset ≤ 1`: a complete result has the canonical range + -- (`minimal_canonicalRange_of_end_offset_le_one_general`), and an overshooting end + -- (every reported key below `end.key`) a complete result has the unbounded top + -- (`minimal_topRange_of_end_offset_ge_two_general` and its reversed twin). The + -- truncated *forward* result additionally has the canonical range at `end.offset ≤ 0` + -- (`minimal_canonicalRange_of_truncated_end_offset_nonpos`) and the truncated range at + -- `end.offset = 1` (`minimal_truncatedRange_of_end_offset_one`). The remaining + -- truncated *reversed* cases and the non-overshooting `end.offset ≥ 2` results stay + -- uncertified. + if _h : r.end_.offset ≤ 0 then + if _h : res.more = false then + some (canonicalRange r) + else if _h : r.reverse = false then + some (canonicalRange r) + else + -- Truncated reversed at `begin.offset ≥ 2`, `end_.offset ≤ 0`: the canonical range + -- is minimal (`minimal_canonicalRange_of_end_offset_le_one_rev_more`). The begin + -- selector skips `b = begin.offset - 1` canonical keys, so the `b` keys below the + -- reported last must be tracked; convexity from the begin key through the reported + -- last, and `necessary_head_to_end_key` above it, force the whole canonical range. + some (canonicalRange r) + else if _h : underEnd r then + -- `end.offset = 1`: complete is the canonical range; the truncated *forward* + -- result is the truncated range (`minimal_truncatedRange_of_end_offset_one`), and + -- the truncated *reversed* result is the canonical range + -- (`minimal_canonicalRange_of_end_offset_le_one_rev_more`). + if _h : res.more = false then + some (canonicalRange r) + else if _h : r.reverse = false then + some (truncatedRange r (res.keys.getLastD ([] : Key))) + else + -- Truncated reversed at `begin.offset ≥ 2`, `end_.offset = 1`: same value, the + -- canonical range (`minimal_canonicalRange_of_end_offset_le_one_rev_more`). + some (canonicalRange r) + else if _h : overshoot r res then + if _h : res.more = false then + some (topRange r) + else if _h : r.reverse = false then + some (truncatedRange r (res.keys.getLastD ([] : Key))) + else + -- Truncated *reversed* at `begin.offset ≠ 1`, `end_.offset ≥ 2`, overshooting: the + -- reversed value `topOfLast` is unsound here (`Counterexample.ev_topOfLast_notSound`), + -- because the begin selector skips keys below the reported last that the candidate + -- cannot see. The minimal value is instead the unbounded top from `begin.key`, + -- `[begin.key, ⊤)` (`minimal_topRange_of_end_offset_ge_two_rev_more`): the begin + -- key is necessary, and convexity through the reported keys with + -- `necessary_of_end_offset_ge_two_rev` above the head forces the whole interval. + some (topRange r) + else + -- `begin.offset ≠ 1`, `end_.offset ≥ 2`, not overshooting. The resolved begin can + -- land above `begin.key`, so the canonical range need not be sound. Left + -- uncertified. + none /- begin offset at least two, end offset at least two, not overshooting: no + certificate -/ + else + -- `begin.offset ≤ 0`, nonempty result. The empty key can be forced into every sound + -- range (`necessary_nil_of_begin_offset_nonpos`), so the canonical range is not + -- minimal, and the resolved begin can land below `begin.key`. In the cross-free + -- overshoot subcase (`begin.key ≤` the least reported key, every reported key below + -- `end_.key`) the full range `[⊥, ⊤)` is minimal + -- (`minimal_fullRange_of_nonpos_overshoot_nonempty` and its reversed twin); in the + -- cross-free non-overshooting subcase (`begin.key ≤` the least reported key, + -- `end_.offset ≤ 1`) the bottom range `[⊥, end_.key)` is minimal + -- (`minimal_bottomRange_of_nonpos_underEnd_nonempty` and its reversed twin); and in the + -- cross-free truncated forward subcase (`begin.key ≤` the least reported key, end pinned + -- above the reported keys) the bottom truncated range `[⊥, keyAfter last reported)` is + -- minimal (`minimal_truncatedRangeBottom_of_nonpos`); and in the + -- cross-free truncated reversed subcase (`begin.key ≤` the least reported key, an + -- overshooting end) the unbounded top from the least reported key `[last reported, ⊤)` is + -- minimal (`minimal_topOfLast_of_end_offset_ge_two_rev_nonpos`); and in the cross-free + -- truncated reversed subcase with `end_.offset = 1` the reversed truncated range + -- `[last reported, end_.key)` is minimal + -- (`minimal_truncatedRangeRev_of_end_offset_one_nonpos`). In the crossed complete + -- forward overshoot subcase at `begin.offset = 0` (`begin.key` strictly above the least + -- reported key) the top range from the least reported key `[head, ⊤)` is minimal + -- (`minimal_topOfHead_of_zero_begin_crossed_overshoot`). In the crossed complete forward + -- subcase at `begin.offset = 0`, `end.offset = 1` (`begin.key` strictly above the least + -- reported key, `begin.key ≤ end_.key`) the head range `[head, end_.key)` is minimal + -- (`minimal_headToEnd_of_zero_begin_crossed_endOne`); in the companion subcase with + -- `end_.key < begin.key` the keys in `(end_.key, begin.key)` are forced, so the head-to- + -- `begin.key` range `[head, begin.key)` is minimal + -- (`minimal_headToBeginKey_of_zero_begin_crossed_endOne_long`). The remaining crossed cases + -- are left uncertified. + if _h : nonposOvershootComplete r res then + some ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + else if _h : nonposUnderEndComplete r res then + some ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + else if _h : nonposFwdTruncPinned r res then + some (truncatedRangeBottom (res.keys.getLastD ([] : Key))) + else if _h : nonposRevTruncOvershoot r res then + some (topOfLast (res.keys.getLastD ([] : Key))) + else if _h : nonposRevTruncEndOne r res then + some (truncatedRangeRev r (res.keys.getLastD ([] : Key))) + else if _h : zeroCrossOvershootComplete r res then + some (topOfLast (res.keys.headD ([] : Key))) + else if _h : zeroCrossEndOne r res then + some ⟨(((res.keys.headD ([] : Key)) : Key) : WithTop Key), r.end_.key⟩ + else if _h : zeroCrossEndOneLong r res then + some ⟨(((res.keys.headD ([] : Key)) : Key) : WithTop Key), r.begin.key⟩ + else + none /- nonempty result at begin offset at most zero: no certificate -/ + +/-! +## FoundationDB's formula, and the remaining holes + +`conflictRangePartial` returns `none` on several regions. FoundationDB's client +returns a range on all of them; `fdbRange` below transcribes its +`getRangeFinished` (`NativeAPI.actor.cpp:4739-4794`), including the +`readToBegin`/`readThroughEnd` side-channel flags that carry the endpoint +facts the formal development instead derives. It is recorded here as +documentation of FoundationDB's behaviour, not as a certified value. The +empty-result branch is now unconditional: `conflictRangePartial` answers any empty +result with `emptyValue r` via the proved `emptyInference`, so the empty result is +certified at every offset and traversal and the `none` regions below concern only +nonempty results. The empty-result `underEnd` case, once the sole remaining conjecture, +is also proved outright (`minimal_canonicalRange_of_underEnd_empty_conj` delegates to +`minimal_canonicalRange_of_underEnd_empty`), and the truncated reversed `underEnd` +case at begin offset one is certified by +`minimal_truncatedRangeRev_of_end_offset_nonpos`, the truncated reversed cell at begin +offset at least two with `end.offset ≤ 1` by +`minimal_canonicalRange_of_end_offset_le_one_rev_more`, and its overshooting companion by +`minimal_topRange_of_end_offset_ge_two_rev_more`. The cross-free nonempty complete +overshoot at a nonpositive begin offset is certified by +`minimal_fullRange_of_nonpos_overshoot_nonempty` (and its reversed twin), the full range +`[⊥, ⊤)`, and the cross-free nonempty complete under-end result there by +`minimal_bottomRange_of_nonpos_underEnd_nonempty` (and its reversed twin), the bottom range +`[⊥, end_.key)`. The cross-free nonempty truncated *forward* result with a pinned end there +is certified by `minimal_truncatedRangeBottom_of_nonpos` (the bottom truncated range +`[⊥, keyAfter last reported)`), and the cross-free nonempty truncated *reversed* result with +an overshooting end by `minimal_topOfLast_of_end_offset_ge_two_rev_nonpos` (the unbounded top +from the least reported key, `[last reported, ⊤)`), and the cross-free nonempty truncated +*reversed* result with `end.offset = 1` by +`minimal_truncatedRangeRev_of_end_offset_one_nonpos` (the reversed truncated range +`[last reported, end_.key)`), and the crossed nonempty complete *forward* overshoot at +`begin.offset = 0` by `minimal_topOfHead_of_zero_begin_crossed_overshoot` (the top range +from the least reported key, `[head, ⊤)`), and the crossed nonempty complete *forward* result at +`begin.offset = 0`, `end.offset = 1` with `begin.key ≤ end_.key` by +`minimal_headToEnd_of_zero_begin_crossed_endOne` (the head range `[head, end_.key)`). The +remaining branches were found +to be refutable as stated (an unobservable end anchor, or a bottom lower end that +is only forced when the empty key is necessary), so they are not posed as +conjectures. + +The transcription follows FoundationDB's conventions: its `RangeResult` is +always ascending, so `result[0]` is the least reported key and +`result.end()[-1]` the greatest, while a reversed formal `Result.keys` is +descending, so `res.keys.head` is the greatest and `res.keys.getLast` the +least. The two `rangeBegin`/`rangeEnd` clamp steps mirror the +`end.getKey() < rangeBegin` and `begin.getKey() > rangeEnd` adjustments at +`NativeAPI.actor.cpp:4776` and `:4788`. -/ + +/-- FoundationDB's client conflict range (`getRangeFinished`, +`NativeAPI.actor.cpp:4739-4794`), parameterized by the `readToBegin` and +`readThroughEnd` flags. A `true` flag replaces the corresponding endpoint by +`⊥`/`⊤`. This records FoundationDB's choice; the formal development does not +assert it minimal on the open branches (see the section note above), except for +`minimal_canonicalRange_of_underEnd_empty_conj` on the empty `underEnd` case. -/ +def fdbRange (r : RangeRead) (res : Result) (readToBegin readThroughEnd : Bool) : + ConflictRange := + let b := r.begin.key + let e := r.end_.key + let head : WithTop Key := res.keys.headD ([] : Key) + let rangeBegin : WithTop Key := + if readToBegin then (↑([] : Key) : WithTop Key) + else if (((r.reverse = false ∨ res.more = false ∨ 1 < r.begin.offset) ∧ + 0 < r.begin.offset) ∨ res.keys = []) then b + else head + let rangeBegin := if r.end_.offset > r.begin.offset ∧ e < rangeBegin then e else rangeBegin + let rangeEnd : WithTop Key := + if readThroughEnd then ⊤ + else if (((r.reverse = true ∨ res.more = false ∨ r.end_.offset ≤ 0) ∧ + r.end_.offset ≤ 1) ∨ res.keys = []) then e + else (↑(keyAfter (res.keys.getLastD ([] : Key))) : WithTop Key) + let rangeEnd := if r.begin.offset < r.end_.offset ∧ rangeEnd < b then b else rangeEnd + ⟨rangeBegin, rangeEnd⟩ + +/-- **The empty result, `begin.offset = 1`, `end_.offset ≤ 1`.** A read whose +universal window is empty (so the result is `⟨[], false⟩`) has the canonical range +`[begin.key, end.key)` as its minimal sound range, provided the read is not +trivially empty. The `hgap : ¬ AlwaysEmpty r` hypothesis is essential, not +cosmetic: under `AlwaysEmpty` the empty range is already minimal +(`minimal_emptyRange_of_alwaysEmpty`), so `Minimal.toSet_eq` forces every minimal +range to denote `∅`, and a nonempty canonical range would contradict it. +FoundationDB reaches the same range through the `result.size() == 0` arm of +`getRangeFinished` (`NativeAPI.actor.cpp:4770` and `:4782`), which takes both +endpoints from the selector keys; here it is stated as the canonical range so it +hooks directly into the `underEnd`/empty branch of `conflictRangePartial`. This is +the former conjecture, now discharged by `minimal_canonicalRange_of_underEnd_empty`. -/ +theorem minimal_canonicalRange_of_underEnd_empty_conj {r : RangeRead} {res : Result} + (hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hb : beginOne r) (he : underEnd r) + (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res (canonicalRange r) := + minimal_canonicalRange_of_underEnd_empty hread hgap hb he hmore hempty + +/-- **The empty flip witness for a key.** A database compatible with the empty result +whose membership flip at `k` breaks compatibility. This is the object the factor-toggle +lemma consumes, and an abstract `Minimal` does not expose it. -/ +def EmptyWitness (r : RangeRead) (k : Key) : Prop := + ∃ db₀ : DB, RangeRead.Compatible db₀ r { keys := [], more := false } ∧ + ¬ RangeRead.Compatible (toggle db₀ k) r { keys := [], more := false } + +/-- **The witness-exposing empty inference.** The empty theory in the form the general +reduction actually consumes: `emptyValue` is sound for every realizable empty read, and +every one of its keys comes with a flip witness. This is strictly stronger than +`EmptyInference` — and it is the strength the joint-base graft needs, because the +passage from a boundary read to the full pair is a factor-toggle, which requires a +witness database that `Minimal` alone does not carry. -/ +def EmptyInferenceWitness : Prop := + ∀ r' : RangeRead, Readable r' { keys := [], more := false } → + Sound r' { keys := [], more := false } (emptyValue r') ∧ + ∀ k : Key, k ∈ emptyValue r' → EmptyWitness r' k + +/-- **The witness oracle implies the abstract oracle.** Soundness is the first component; +each key of `emptyValue` is necessary by its flip witness (`necessary_of_toggle`); the +two give `Minimal` by `minimal_iff_sound_and_necessary`. -/ +theorem emptyInference_of_witness (h : EmptyInferenceWitness) : EmptyInference := by + intro r' hr + obtain ⟨hs, hwit⟩ := h r' hr + exact minimal_iff_sound_and_necessary.mpr + ⟨hs, fun k hk => necessary_of_toggle (hwit k hk)⟩ + +/-! +### Extracting the empty flip witnesses + +The empty theory's necessity proofs already build, for each key of their value, a pair +of databases differing in that key and separated by compatibility. Those pairs are +exactly `EmptyWitness`; the lemmas below expose them, which is what the general +reduction's graft consumes. The constructions are the same as the corresponding +minimality proofs, restated at the witness level. +-/ + +/-- **The `underEnd` empty witness.** For a realizable non-trivially-empty read with +`begin.offset = 1`, `end.offset ≤ 1`, and an empty result, every key of the canonical +range has a flip witness: a database of exactly `m = (1 - end.offset).toNat` keys of +`[begin.key, end.key)` avoiding the key, compatible with the empty result, whose flip at +the key exceeds the bound. -/ +theorem emptyWitness_underEnd {r : RangeRead} (hgap : ¬ AlwaysEmpty r) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) {k : Key} + (hk : k ∈ canonicalRange r) : EmptyWitness r k := by + set m : ℕ := (1 - r.end_.offset).toNat with hm + have hm1 : m + 1 = (2 - r.end_.offset).toNat := by + have h1 : (0 : ℤ) ≤ 1 - r.end_.offset := by omega + have h2 : (0 : ℤ) ≤ 2 - r.end_.offset := by omega + apply Nat.cast_injective (R := ℤ) + rw [Nat.cast_add, Nat.cast_one, hm, Int.toNat_of_nonneg h1, Int.toNat_of_nonneg h2] + omega + have hroom : (m + 1 : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + have h := two_sub_oE_le_intervalCard_canonical hbegin hend hgap + rwa [← hm1] at h + obtain ⟨db₀, hdb₀range, hkndb, hlen⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) hroom + have hcan₀ : (canonicalRange r).keys db₀ = db₀.keys := + canonicalRange_keys_eq_self_of_forall fun x hx => hdb₀range x hx + have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := by + rw [compatible_empty_iff_length_le_any (db := db₀) hbegin hend, hcan₀, hlen] + have hcan₁ : (canonicalRange r).keys (db₀.insert k hkndb) = + (db₀.insert k hkndb).keys := by + refine canonicalRange_keys_eq_self_of_forall fun x hx => ?_ + rw [DB.mem_insert_iff hkndb] at hx + rcases hx with rfl | hx + · exact hk + · exact hdb₀range x hx + have hlen₁ : ((canonicalRange r).keys (db₀.insert k hkndb)).length = m + 1 := by + rw [hcan₁, DB.insert_keys, List.orderedInsert_length, hlen] + have hnot : ¬ RangeRead.Compatible (db₀.insert k hkndb) r + { keys := [], more := false } := by + rw [compatible_empty_iff_length_le_any (db := db₀.insert k hkndb) hbegin hend, hlen₁] + omega + refine ⟨db₀, hcomp₀, ?_⟩ + rwa [show toggle db₀ k = db₀.insert k hkndb from by + unfold toggle; rw [dite_eq_right hkndb]] + +/-- **The `underEnd` empty witness at a general positive begin offset.** For a +realizable non-trivially-empty read with `1 ≤ begin.offset`, `end.offset ≤ 1`, and +`begin.key ≤ end.key`, every key of the canonical range has a flip witness: a +database of exactly `b + k` keys of `[begin.key, end.key)` avoiding the key, where +`b = (begin.offset - 1).toNat` and `k = (1 - end.offset).toNat`, compatible with the +empty result (its canonical count minus `b` is `k`), whose flip at the key exceeds +the bound. This generalizes `emptyWitness_underEnd` to positive begin offsets. -/ +theorem emptyWitness_underEnd_general {r : RangeRead} (hgap : ¬ AlwaysEmpty r) + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) (hle : r.begin.key ≤ r.end_.key) + {k : Key} (hk : k ∈ canonicalRange r) : EmptyWitness r k := by + set b : ℕ := (r.begin.offset - 1).toNat with hb + set kk : ℕ := (1 - r.end_.offset).toNat with hkk + have hroom : (b + kk + 1 : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + rw [hb, hkk]; exact add_add_one_le_intervalCard_canonical hbegin hend hle hgap + obtain ⟨db₀, hdb₀range, hkndb, hlen⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := k) (m := b + kk) hroom + have hcan₀ : (canonicalRange r).keys db₀ = db₀.keys := + canonicalRange_keys_eq_self_of_forall fun x hx => hdb₀range x hx + have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := by + rw [compatible_empty_iff_length_sub_le_any (db := db₀) hbegin hend, hcan₀, hlen] + omega + have hcan₁ : (canonicalRange r).keys (db₀.insert k hkndb) = + (db₀.insert k hkndb).keys := by + refine canonicalRange_keys_eq_self_of_forall fun x hx => ?_ + rw [DB.mem_insert_iff hkndb] at hx + rcases hx with rfl | hx + · exact hk + · exact hdb₀range x hx + have hlen₁ : ((canonicalRange r).keys (db₀.insert k hkndb)).length = b + kk + 1 := by + rw [hcan₁, DB.insert_keys, List.orderedInsert_length, hlen] + have hnot : ¬ RangeRead.Compatible (db₀.insert k hkndb) r + { keys := [], more := false } := by + rw [compatible_empty_iff_length_sub_le_any (db := db₀.insert k hkndb) hbegin hend, hlen₁] + omega + refine ⟨db₀, hcomp₀, ?_⟩ + rwa [show toggle db₀ k = db₀.insert k hkndb from by + unfold toggle; rw [dite_eq_right hkndb]] + +/-- **The `underEnd` empty witness oracle, any positive begin offset and traversal.** +With `1 ≤ begin.offset` and `end.offset ≤ 1`, `emptyValue` is the canonical range +unless the read is trivially empty. Soundness is the any-traversal bound equivalence +`compatible_empty_iff_length_sub_le_any`; if `begin.key ≤ end.key` every canonical key +has a flip witness by `emptyWitness_underEnd_general`, and if `begin.key > end.key` the +read is trivially empty (`alwaysEmpty_of_begin_gt_end`), so `emptyValue` is the empty +range and has no keys to witness. -/ +theorem emptyWitness_underEnd_gen (r : RangeRead) (hbegin : 1 ≤ r.begin.offset) + (hend : r.end_.offset ≤ 1) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hle : r.begin.key ≤ r.end_.key := by + by_contra h + exact hAE (alwaysEmpty_of_begin_gt_end hbegin hend (not_le.mp h)) + have hv : emptyValue r = canonicalRange r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), ite_eq_left hend] + refine ⟨?_, ?_⟩ + · rw [hv] + intro db₁ db₂ hagree + rw [compatible_empty_iff_length_sub_le_any (db := db₁) hbegin hend, + compatible_empty_iff_length_sub_le_any (db := db₂) hbegin hend, + show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree] + · intro k hk + rw [hv] at hk + exact emptyWitness_underEnd_general hAE hbegin hend hle hk + +/-- **The cross-free overshoot empty witness.** With `begin.offset = 1`, +`end.offset ≥ 2`, `begin.key ≤ end.key`, and an empty result, every key of the top range +`[begin.key, ⊤)` has a flip witness: the empty result is compatible exactly when the top +range stores nothing, so inserting the key into any compatible database makes the top +range (and hence the complete keys) nonempty. -/ +theorem emptyWitness_crossFree {r : RangeRead} + (hread : Readable r { keys := [], more := false }) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) (hle : r.begin.key ≤ r.end_.key) + {k : Key} (hk : k ∈ topRange r) : EmptyWitness r k := by + obtain ⟨db₀, hdb₀⟩ := hread + have h0nil : (topRange r).keys db₀ = [] := + (completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₀) hbegin hend hle).mp + ((compatible_empty_iff_complete_nil (db := db₀) (r := r)).mp hdb₀) + have hknot : k ∉ db₀.keys := by + intro hkmem + have : k ∈ (topRange r).keys db₀ := ConflictRange.mem_keys_iff.mpr ⟨hkmem, hk⟩ + rw [h0nil] at this; exact List.not_mem_nil this + refine ⟨db₀, hdb₀, ?_⟩ + rw [show toggle db₀ k = db₀.insert k hknot from by + unfold toggle; rw [dite_eq_right hknot]] + intro hcomp₁ + have h1ne : (topRange r).keys (db₀.insert k hknot) ≠ [] := by + intro hnil + have : k ∈ (topRange r).keys (db₀.insert k hknot) := + ConflictRange.mem_keys_iff.mpr ⟨DB.mem_insert_self _ _ _, hk⟩ + rw [hnil] at this; exact List.not_mem_nil this + exact h1ne ((completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₀.insert k hknot) + hbegin hend hle).mp ((compatible_empty_iff_complete_nil + (db := db₀.insert k hknot) (r := r)).mp hcomp₁)) + +/-- **The crossed empty witness at or above `B`.** At `begin.offset = 1`, +`end.offset ≥ 2`, `end.key < begin.key`, the empty database is compatible and inserting +any `k ≥ begin.key` is incompatible, so every such key of `[begin.key, ⊤)` has a flip +witness. This covers the `[begin.key, ⊤)` branch of `crossedRange`. -/ +theorem emptyWitness_crossed_beginLe {r : RangeRead} + (hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset) + {k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) : EmptyWitness r k := by + have hk0 : k ∉ emptyDB.keys := by simp [emptyDB] + refine ⟨emptyDB, compatible_emptyDB_emptyRes r, ?_⟩ + rw [show toggle emptyDB k = emptyDB.insert k hk0 from by + unfold toggle; rw [dite_eq_right hk0]] + intro hc + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_counts hbegin (by omega)] at hc + have hkB : ¬ (k : WithTop Key) < r.begin.key := not_lt.mpr hk + have hcntB : cnt (emptyDB.insert k hk0) r.begin.key = 0 := by + rw [cnt_insert_not_lt emptyDB k hk0 hkB] + simp [cnt, emptyDB] + have hlen : (emptyDB.insert k hk0).keys.length = 1 := by + rw [DB.insert_keys]; simp [emptyDB] + have h1 : 1 ≤ (r.end_.offset - 1).toNat := by omega + rw [hcntB, hlen] at hc + omega + +/-- **The crossed empty witness inside `[E, B)`.** In the long-gap branch every genuine +`k` with `end.key ≤ k < begin.key` has a flip witness: the base stores `oE - 2` keys of +`[E, B)` plus a key `j ≥ B`, so `ψ` fails, while inserting `k` makes the `[E, B)` count +reach `oE - 1` and satisfies the second disjunct. -/ +theorem emptyWitness_crossed_interval {r : RangeRead} + (hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset) (hne : ¬ AlwaysEmpty r) + (hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key r.begin.key) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : EmptyWitness r k := by + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty (by omega) hne + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by + cases hkb : r.begin.key with + | top => exact absurd hkb hBne + | coe b => exact ⟨b, rfl⟩ + have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by + intro htop; rw [htop] at hkE; exact not_le.mpr (WithTop.coe_lt_top k) hkE + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (↑e : WithTop Key) := by + cases hke : r.end_.key with + | top => exact absurd hke hEne + | coe e => exact ⟨e, rfl⟩ + have hkltb : k < b := by rw [hb] at hkB; exact WithTop.coe_lt_coe.mp hkB + have hke' : e ≤ k := by rw [he] at hkE; exact WithTop.coe_le_coe.mp hkE + have hee : e ≤ b := le_trans hke' (le_of_lt hkltb) + set m : ℕ := (r.end_.offset - 2).toNat with hm + have hm1 : m + 1 = (r.end_.offset - 1).toNat := by + rw [hm] + have h3 : (0 : ℤ) ≤ r.end_.offset - 2 := by omega + have h4 : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + have : (((r.end_.offset - 2).toNat + 1 : ℕ) : ℤ) = (r.end_.offset - 1).toNat := by + rw [Nat.cast_add, Nat.cast_one, Int.toNat_of_nonneg h3, Int.toNat_of_nonneg h4] + ring + exact_mod_cast this + have hcard : (m + 1 : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := + calc (m + 1 : ℕ∞) = ((m + 1 : ℕ) : ℕ∞) := rfl + _ = ((r.end_.offset - 1).toNat : ℕ∞) := by rw [hm1] + _ ≤ intervalCard r.end_.key r.begin.key := hgap + obtain ⟨dbK, hdbKrange, hkK, hlenK⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := m) hcard + have hdbKe : ∀ x ∈ dbK.keys, e ≤ x := by + intro x hx + have h := (hdbKrange x hx).1 + rw [he] at h; exact WithTop.coe_le_coe.mp h + have hdbKb : ∀ x ∈ dbK.keys, x < b := by + intro x hx + have h := (hdbKrange x hx).2 + rw [hb] at h; exact WithTop.coe_lt_coe.mp h + have hcntE : cnt dbK (↑e : WithTop Key) = 0 := by + rw [cnt_coe]; exact countP_lt_eq_zero_of_forall_ge hdbKe + have hcntB : cnt dbK (↑b : WithTop Key) = m := by + rw [cnt_coe, countP_lt_eq_length_of_forall_lt hdbKb, hlenK] + obtain ⟨j, hjb, hjK⟩ := exists_le_not_mem dbK.keys b + have hjnotin : j ∉ dbK.keys := hjK + have hcntE₂ : cnt (dbK.insert j hjnotin) (↑e : WithTop Key) = 0 := by + rw [cnt_insert_not_lt dbK j hjnotin (v := (↑e : WithTop Key)), hcntE] + rw [WithTop.coe_lt_coe]; intro hlt + exact absurd hlt (not_lt.mpr (le_trans hee hjb)) + have hcntB₂ : cnt (dbK.insert j hjnotin) (↑b : WithTop Key) = m := by + rw [cnt_insert_not_lt dbK j hjnotin (v := (↑b : WithTop Key)), hcntB] + rw [WithTop.coe_lt_coe]; exact not_lt.mpr hjb + have hlen₂ : (dbK.insert j hjnotin).keys.length = m + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlenK] + have hk_db2 : k ∉ (dbK.insert j hjnotin).keys := by + rw [DB.mem_insert_iff hjnotin] + rintro (rfl | hk) + · exact absurd hkltb (not_lt.mpr hjb) + · exact hkK hk + have hnot₂ : ¬ RangeRead.Compatible (dbK.insert j hjnotin) r + { keys := [], more := false } := by + intro hc + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_counts hbegin (by omega)] at hc + have hcntE₂' : cnt (dbK.insert j hjnotin) r.end_.key = 0 := by rw [he]; exact hcntE₂ + have hcntB₂' : cnt (dbK.insert j hjnotin) r.begin.key = m := by rw [hb]; exact hcntB₂ + rcases hc with hc | hc + · rw [hcntB₂', hlen₂] at hc; omega + · rw [hcntE₂', hcntB₂'] at hc; omega + have hcntB₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2) + (↑b : WithTop Key) = (r.end_.offset - 1).toNat := by + rw [cnt_insert_lt _ k hk_db2 (v := (↑b : WithTop Key)) (WithTop.coe_lt_coe.mpr hkltb), + hcntB₂] + exact hm1 + have hcntE₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2) + (↑e : WithTop Key) = 0 := by + rw [cnt_insert_not_lt _ k hk_db2 (v := (↑e : WithTop Key)), hcntE₂] + rw [WithTop.coe_lt_coe]; exact not_lt.mpr hke' + have hcomp₁ : RangeRead.Compatible ((dbK.insert j hjnotin).insert k hk_db2) r + { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_counts hbegin (by omega)).mpr (Or.inr (by + rw [hb, he, hcntE₁, hcntB₁, zero_add]))) + refine ⟨(dbK.insert j hjnotin).insert k hk_db2, hcomp₁, ?_⟩ + have hkeys : (toggle ((dbK.insert j hjnotin).insert k hk_db2) k).keys = + (dbK.insert j hjnotin).keys := by + rw [toggle, dite_eq_left (DB.mem_insert_self _ _ _)] + exact DB.erase_insert_keys _ k hk_db2 + intro hc + exact hnot₂ ((RangeRead.compatible_congr + (RangeRead.completeKeys_congr hkeys r)).mp hc) + +/-- The reported interval `[k₀, keyAfter kₙ)` of a nonempty result: from the least +reported key through just past the greatest. This is the middle component of the +complete value. -/ +def reportedRange (r : RangeRead) (res : Result) : ConflictRange := + ⟨(leastReported r res : WithTop Key), + (keyAfter (greatestReported r res) : WithTop Key)⟩ + +/-- **Necessity of the reported interval, any traversal.** Every key of the reported +interval `[k₀, keyAfter kₙ)` is necessary for the full pair, unconditionally: the two +extremes are reported keys (`necessary_of_mem_result`) and every key between them is +convex (`Necessary.of_between`). This is the middle factor of the assembled range and +needs no empty theory. -/ +theorem necessaryRange_reportedRange {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) : + NecessaryRange r res (reportedRange r res).begin (reportedRange r res).end_ := by + intro k hlo hhi + simp only [reportedRange] at hlo hhi + have hle : leastReported r res ≤ k := WithTop.coe_le_coe.mp hlo + have hkg : k ≤ greatestReported r res := + (lt_keyAfter_iff k (greatestReported r res)).mp (WithTop.coe_lt_coe.mp hhi) + have hmem_lo : leastReported r res ∈ res.keys := by + unfold leastReported + split + · exact getLastD_mem hne + · exact headD_mem hne + have hmem_hi : greatestReported r res ∈ res.keys := by + unfold greatestReported + split + · exact headD_mem hne + · exact getLastD_mem hne + exact (necessary_of_mem_result hread hmem_lo).of_between + (necessary_of_mem_result hread hmem_hi) hle hkg + +/-- **The begin (low) contribution: the lower boundary read's empty value.** This is +the empty-result inference applied to `boundaryReadLo r k₀`, the read that keeps the +full begin selector and ends at the least reported key `k₀`. -/ +def lowBound (r : RangeRead) (res : Result) : ConflictRange := + emptyValue (boundaryReadLo r (leastReported r res)) + +/-- **The end (high) contribution: the upper boundary read's empty value.** This is +the empty-result inference applied to `boundaryReadHi r kₙ`, the read that begins just +past the greatest reported key `kₙ` and keeps the full end selector. Its crossed branch +is what carries the case where a reported key is at or above `end.key`. -/ +def highBound (r : RangeRead) (res : Result) : ConflictRange := + emptyValue (boundaryReadHi r (greatestReported r res)) + +/-- Forward truncated value: `[w, keyAfter kₙ)`, the begin selector's empty +contribution plus the reported keys, with the bare successor upper end. -/ +def forwardTruncatedRange (r : RangeRead) (res : Result) : ConflictRange := + ⟨(lowBound r res).begin, (keyAfter (greatestReported r res) : WithTop Key)⟩ + +/-- Reverse truncated value: `[kₙ, u)`, the reported keys plus the end selector's +empty contribution, with the bare end upper end and no begin contribution. -/ +def reverseTruncatedRange (r : RangeRead) (res : Result) : ConflictRange := + ⟨(leastReported r res : WithTop Key), (highBound r res).end_⟩ + +/-- Complete value: the hull of the lower boundary contribution, the reported +interval, and the upper boundary contribution: `hull (hull W M) U`. The reported +interval is an explicit component, so its keys are always covered and a degenerate +boundary contribution cannot drag an endpoint past it (`hull` drops empty +components). -/ +def completeRange (r : RangeRead) (res : Result) : ConflictRange := + hull (hull (lowBound r res) (reportedRange r res)) (highBound r res) + +/-- **The empty-result boundary hypothesis: `ψ` extended from the complete value's own +endpoints to the reported keys.** Everything from the complete value's lower end up to +the least reported key is necessary, and everything from just past the greatest reported +key up to the complete value's upper end is necessary. This is the empty inference's +`ψ` at the two boundary reads, transferred to the *full* pair and stretched across the +half-open gaps on either side of the reported keys (the joint-base transfer of section +7). It is a hypothesis precisely because an abstract `Minimal` statement about the +boundary pair does not expose the flip witness that transfer needs. -/ +def BoundaryNecessity (r : RangeRead) (res : Result) : Prop := + NecessaryRange r res (completeRange r res).begin (reportedRange r res).begin ∧ + NecessaryRange r res (reportedRange r res).end_ (completeRange r res).end_ + +/-- **The bracket-observability residue.** Agreement on the complete value fixes the +bracketing predicate, i.e. whether the resolved endpoints straddle the reported span. +This is the notes' equation~(obs): the one bridge factor that the emptiness and interval +equality factors do not imply. It is discharged by the empty theory's gap split at +positive offsets (the stored counts in `[begin.key, k₀)` and `[end.key, kₙ)`), which is +why the pins `resolve begin = k₀` / `resolve end_ = keyAfter kₙ` do not by themselves +remove it. -/ +def BracketObservable (r : RangeRead) (res : Result) : Prop := + ∀ db₁ db₂ : DB, AgreeOn (completeRange r res) db₁ db₂ → + (bracket r res.keys db₁ ↔ bracket r res.keys db₂) + +/-- The bracket-observability residue for a reversed read: the bridge is stated on the +ascending list `res.keys.reverse`, so the bracket is taken there. -/ +def BracketObservableRev (r : RangeRead) (res : Result) : Prop := + ∀ db₁ db₂ : DB, AgreeOn (completeRange r res) db₁ db₂ → + (bracket r res.keys.reverse db₁ ↔ bracket r res.keys.reverse db₂) + +/-- The bracket-observability residue, selected by traversal. It is the only soundness +factor not derived from `EmptyInference`; the complete branch's minimality is reduced to +it in `minimal_completeRange_of_factors`/`_rev`. -/ +def BracketFactor (r : RangeRead) (res : Result) : Prop := + if r.reverse then BracketObservableRev r res else BracketObservable r res + +/-- **The complete branch's value.** In the canonical region `1 ≤ begin.offset ∧ +end.offset ≤ 1` the minimal sound range is the canonical range `[begin.key, end.key)` +(`minimal_canonicalRange_of_end_offset_le_one_general`), *not* the hull: at a +nonpositive end offset the hull's empty-aware glue moves `W`'s endpoint up to the +least reported key, dropping the genuinely necessary canonical keys that lie below +it. In the overshoot region at `begin.offset ≥ 1` the resolved end is past the data +(`minimal_topRange_of_end_offset_ge_two_general`), so the value is the unbounded top +range `[begin.key, ⊤)`. Elsewhere the complete value remains the hull assembly +`completeRange`. + +The remaining uncertified regions are two, both refuted as *sound* claims rather than +merely open: + +* the crossed selector order with `end.key = ⊥`, positive `begin.offset`, and an end + offset between two and the begin offset (so not `overshoot`, since `⊥` is below every + nonempty key): there the hull need not be minimal; +* the `begin.offset ≠ 1` cell at an overshooting-free end, where the resolved begin may + clamp to `⊤` on one side of an agreement and to a stored key on the other, so the hull + is *unsound* (`FormalConflictRange.Counterexample.cw_notSound`). That cell has no + single minimal shape either: the `cwMin` value (the bare successor of the greatest + reported key) at `end.offset = 2` fails once the end selector's slack runs the window + past the reported last, as `Counterexample.ns_cand_notSound` records, with every key + necessary (`ns_top_minimal`) and the true value the unbounded top (`ns_top_sound`). + +`conflictRangePartial` returns `none` on both, so `completeValue` is a totalization +whose hull fallback is not correct off the canonical and overshoot regions. -/ +def completeValue (r : RangeRead) (res : Result) : ConflictRange := + if 1 ≤ r.begin.offset ∧ r.end_.offset ≤ 1 then canonicalRange r + else if 1 ≤ r.begin.offset ∧ overshoot r res then topRange r + else completeRange r res + +/-- **The general algorithm's value, by reduction to the empty boundary reads.** The +result is dispatched on emptiness, truncation, and traversal: + +* an empty result uses the empty theory's value `emptyValue`; +* a forward truncated result is `[lowBound.begin, keyAfter kₙ)`, the begin selector's + empty contribution plus the reported keys; +* a reverse truncated result is `[kₙ, highBound.end_)`, the reported keys plus the end + selector's empty contribution, the begin selector dropping out; +* a complete result is `completeValue`, the canonical range in the canonical region and + the hull of the two boundary reads' minimal ranges `hull (lowBound r res) (highBound r res)` + elsewhere. + +The low and high contributions are the empty boundary reads' minimal ranges, each the +empty range when its read is trivially empty; the definition is computable. + +**This is a provisional totalization, not the object of record.** The +certified algorithm is `conflictRangePartial`; `conflictRangeVal` is the +every-input `ConflictRange`-valued wrapper, and its branches off the certified +region (the truncated dispatch and the `completeValue` hull fallback) are *not* +sound in general — the kernel-checked `FormalConflictRange.Counterexample` +(`cw_notSound`, and the truncated-branch violations) record this. Its correctness +theorems (`conflictRangeVal_spec`, `_spec_of_witness`, `_spec_complete`) are stated +relative to the open `EmptyTheoryComplete` and are consequently vacuous, not the +headline claim. Once `conflictRangePartial` is proved total it becomes the +`ConflictRange`-valued algorithm and this definition is subsumed by it; the naming +change is deferred until then. -/ +def conflictRangeVal (r : RangeRead) (res : Result) : ConflictRange := + if res.keys = [] then + emptyValue r + else if res.more = true then + if r.reverse = true then reverseTruncatedRange r res + else forwardTruncatedRange r res + else + completeValue r res + +/-! +## The complete branch: the assembled hull + +The complete branch is the section-7 reduction: the hull of the two boundary reads' +minimal ranges and the reported interval, necessary on the two boundary ranges +(`BoundaryNecessity`) and the derived interior, and sound from `EmptyInference` once the +bracket residue is supplied. -/ + +/-- The complete value is the hull of the lower contribution, the reported +interval, and the upper contribution, `hull (hull W M) U`. -/ +theorem completeRange_eq_hull (r : RangeRead) (res : Result) : + completeRange r res = + hull (hull (lowBound r res) (reportedRange r res)) (highBound r res) := rfl + +/-- The reported interval is a subrange of the complete value: the reported keys are +always covered, whichever way the boundary contributions land. -/ +theorem subrange_completeRange_reported (r : RangeRead) (res : Result) : + Subrange (reportedRange r res) (completeRange r res) := + Subrange.trans (subrange_hull_right _ _) (subrange_hull_left _ _) + +/-- Each boundary contribution is a subrange of the complete value. -/ +theorem subrange_completeRange_lowBound (r : RangeRead) (res : Result) : + Subrange (lowBound r res) (completeRange r res) := + Subrange.trans (subrange_hull_left _ _) (subrange_hull_left _ _) + +/-- Each boundary contribution is a subrange of the complete value. -/ +theorem subrange_completeRange_highBound (r : RangeRead) (res : Result) : + Subrange (highBound r res) (completeRange r res) := + subrange_hull_right _ _ + +/-- **Necessity of the complete value from the boundary `ψ`.** The first conjunct of +`BoundaryNecessity` covers `[R.begin, k₀)`, the reported interval `[k₀, keyAfter kₙ)` is +necessary unconditionally (`necessaryRange_reportedRange`), and the second conjunct +covers `[keyAfter kₙ, R.end)`. The three chain to exactly `[R.begin, R.end)`. -/ +theorem necessaryRange_completeRange_of_boundaryNecessity {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hψ : BoundaryNecessity r res) : + NecessaryRange r res (completeRange r res).begin (completeRange r res).end_ := + (hψ.1.trans (necessaryRange_reportedRange hread hne)).trans hψ.2 + +/-- **Minimality of the complete value from the boundary `ψ` and soundness.** Soundness +of the hull plus the necessary range derived above give minimality by +`minimal_iff_sound_and_necessaryRange`. The interior necessity is derived +(`necessaryRange_reportedRange`), so — beyond soundness — the only content is the empty +`ψ` at the two boundaries, stretched to the complete value's endpoints. -/ +theorem minimal_completeRange_of_boundaryNecessity {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hψ : BoundaryNecessity r res) + (hs : Sound r res (completeRange r res)) : + Minimal r res (completeRange r res) := + minimal_iff_sound_and_necessaryRange.mpr + ⟨hs, necessaryRange_completeRange_of_boundaryNecessity hread hne hψ⟩ + +/-! +## Boundary necessity, forward complete, from the begin/end key theorems + +Away from the joint-base construction, `BoundaryNecessity`'s two ranges follow directly +from the existing end-key necessity theorems plus convexity: the reported keys are +necessary (`necessary_of_mem_result`), and the selector keys fill the gaps. These supply +`BoundaryNecessity` in the non-crossed, positive-offset region without any database +graft. +-/ + +/-- **Lower boundary necessity, forward.** At a positive begin offset the begin key is +necessary (`necessary_begin_key`) and the least reported key is necessary; convexity +fills `[begin.key, k₀)`. -/ +theorem necessaryRange_low_fwd {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hoff : 1 ≤ r.begin.offset) {v : Key} (hkey : r.begin.key = (↑v : WithTop Key)) : + NecessaryRange r res r.begin.key ((leastReported r res : Key) : WithTop Key) := by + have hlen : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hv : Necessary r res v := necessary_begin_key hread hrev hlen hoff hkey + have hhead : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] + have hk0 : Necessary r res (leastReported r res) := by + rw [hhead]; exact necessary_of_mem_result hread (headD_mem hne) + intro k hlo hhi + rw [hkey] at hlo + exact hv.of_between hk0 (WithTop.coe_le_coe.mp hlo) (le_of_lt (WithTop.coe_lt_coe.mp hhi)) + +/-- **Upper boundary necessity, forward.** With an end offset at most one, the keys from +`keyAfter kₙ` up to `end_.key` are necessary (`necessary_last_to_end_key`). -/ +theorem necessaryRange_high_fwd {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 1) : + NecessaryRange r res ((keyAfter (greatestReported r res) : Key) : WithTop Key) + r.end_.key := by + have hgreat : greatestReported r res = res.keys.getLastD ([] : Key) := by + simp [greatestReported, hrev] + have hlastD : res.keys.getLast hne = res.keys.getLastD ([] : Key) := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + intro k hlo hhi cr hs + have hk0 : res.keys.getLastD ([] : Key) < k := by + by_contra hcon + have hle : k ≤ res.keys.getLastD ([] : Key) := le_of_not_gt hcon + have : (k : WithTop Key) < (↑(keyAfter (res.keys.getLastD ([] : Key))) : WithTop Key) := + WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k (res.keys.getLastD ([] : Key))).mpr hle) + rw [hgreat] at hlo + exact absurd hlo (not_le.mpr this) + exact necessary_last_to_end_key hread hrev hmore hne hend cr hs + (by rw [hlastD]; exact le_of_lt hk0) hhi + +/-! ### Hull endpoint identities -/ + +/-- The hull of two abutting intervals `[x, y)` and `[y, z)` is `[x, z)`. -/ +theorem hull_abut (x y z : WithTop Key) (hxy : x ≤ y) (hyz : y ≤ z) : + hull (⟨x, y⟩ : ConflictRange) ⟨y, z⟩ = ⟨x, z⟩ := by + unfold hull + by_cases h : (⟨x, y⟩ : ConflictRange).IsEmpty + · rw [ite_eq_left h] + have : y = x := le_antisymm (by simpa [ConflictRange.IsEmpty] using h) hxy + rw [this] + · rw [ite_eq_right h] + by_cases h2 : (⟨y, z⟩ : ConflictRange).IsEmpty + · rw [ite_eq_left h2] + have : z = y := le_antisymm (by simpa [ConflictRange.IsEmpty] using h2) hyz + rw [this] + · rw [ite_eq_right h2] + rw [min_eq_left hxy, max_eq_right hyz] + +/-- The hull of three abutting intervals `[B, a)`, `[a, b)`, `[b, c)` is `[B, c)`. -/ +theorem hull3 (B a b c : WithTop Key) (hBa : B ≤ a) (hab : a ≤ b) (hbc : b ≤ c) : + hull (hull (⟨B, a⟩ : ConflictRange) ⟨a, b⟩) ⟨b, c⟩ = ⟨B, c⟩ := by + rw [hull_abut B a b hBa hab, hull_abut B b c (le_trans hBa hab) hbc] + +/-- For a non-reversed read the least reported key is at or below the greatest. -/ +theorem least_le_greatest_fwd {r : RangeRead} {res : Result} (hrev : r.reverse = false) + (hpair : res.keys.Pairwise (· < ·)) (hne : res.keys ≠ []) : + leastReported r res ≤ greatestReported r res := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] + have hgreat : greatestReported r res = res.keys.getLastD ([] : Key) := by + simp [greatestReported, hrev] + rw [hleast, hgreat] + exact headD_le_of_pairwise hpair (getLastD_mem hne) + +/-! +## Hull soundness from the empty inference + +The bridge is a four-way conjunction. Three of its factors transfer from `EmptyInference` +by the shape of the assembled value: the two boundary emptinesses restrict agreement on +the complete value to each boundary contribution (`subrange_completeRange_lowBound` / +`_highBound`) and read off the empty inference's soundness there; the middle interval +restricts to `reportedRange`. The fourth, `bracket`, is the observability residue: it +asks that the resolved endpoints stay outside the reported span, which the other three +factors do not force. It is isolated as a hypothesis here. +-/ + +/-- The empty inference applies to every read: the empty database is always compatible +with the empty result, so every read is empty-realizable. -/ +theorem emptyInference_minimal (h : EmptyInference) (r : RangeRead) : + Minimal r { keys := [], more := false } (emptyValue r) := + h r ⟨emptyDB, compatible_emptyDB_emptyRes r⟩ + +/-- The lower boundary read's emptiness is agreement-invariant: agreement on the +complete value restricts to the lower contribution, and the empty inference says that +contribution is sound for the boundary pair, whose compatibility is exactly boundary +emptiness (`compatible_empty_iff_complete_nil`). -/ +theorem boundaryLo_empty_iff_of_agree (h : EmptyInference) (r : RangeRead) (res : Result) + {db₁ db₂ : DB} (hag : AgreeOn (completeRange r res) db₁ db₂) : + RangeRead.completeKeys db₁ (boundaryReadLo r (leastReported r res)) = [] ↔ + RangeRead.completeKeys db₂ (boundaryReadLo r (leastReported r res)) = [] := by + have hsnd := (emptyInference_minimal h (boundaryReadLo r (leastReported r res))).sound + have hag' : AgreeOn (lowBound r res) db₁ db₂ := + keys_eq_of_subrange_agree (subrange_completeRange_lowBound r res) hag + have hh := hsnd db₁ db₂ hag' + simpa only [compatible_empty_iff_complete_nil] using hh + +/-- The upper boundary read's emptiness is agreement-invariant, dually. -/ +theorem boundaryHi_empty_iff_of_agree (h : EmptyInference) (r : RangeRead) (res : Result) + {db₁ db₂ : DB} (hag : AgreeOn (completeRange r res) db₁ db₂) : + RangeRead.completeKeys db₁ (boundaryReadHi r (greatestReported r res)) = [] ↔ + RangeRead.completeKeys db₂ (boundaryReadHi r (greatestReported r res)) = [] := by + have hsnd := (emptyInference_minimal h (boundaryReadHi r (greatestReported r res))).sound + have hag' : AgreeOn (highBound r res) db₁ db₂ := + keys_eq_of_subrange_agree (subrange_completeRange_highBound r res) hag + have hh := hsnd db₁ db₂ hag' + simpa only [compatible_empty_iff_complete_nil] using hh + +/-- The middle interval equality is agreement-invariant: it is the `reportedRange`'s +stored keys, and agreement on the complete value restricts to that subrange. -/ +theorem midEq_iff_of_agree (r : RangeRead) (res : Result) (hrev : r.reverse = false) + {db₁ db₂ : DB} (hag : AgreeOn (completeRange r res) db₁ db₂) : + midEq res.keys db₁ ↔ midEq res.keys db₂ := by + have hag' : AgreeOn (reportedRange r res) db₁ db₂ := + keys_eq_of_subrange_agree (subrange_completeRange_reported r res) hag + have hle : leastReported r res = res.keys.headD ([] : Key) := by simp [leastReported, hrev] + have hge : greatestReported r res = res.keys.getLastD ([] : Key) := by + simp [greatestReported, hrev] + have hiff : ∀ db, midEq res.keys db ↔ (reportedRange r res).keys db = res.keys := by + intro db + simp only [midEq, reportedRange, hle, hge, ConflictRange.keys_eq_inRange] + rw [hiff db₁, hiff db₂, hag'] + +/-- **Hull soundness, with the bracket isolated.** The three tractable bridge factors +transfer by `EmptyInference` and subrange agreement; the fourth, `bracket`, is the +observability residue, taken as `BracketObservable`. -/ +theorem sound_completeRange_of_bracketTransfer {r : RangeRead} {res : Result} + (h : EmptyInference) (hrev : r.reverse = false) (hmore : res.more = false) + (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· < ·)) + (hlim : RangeRead.withinLimit r res.keys) (hbr : BracketObservable r res) : + Sound r res (completeRange r res) := by + obtain ⟨ks, m⟩ := res + simp only at hmore hL hpair hlim hbr ⊢ + subst hmore + intro db₁ db₂ hag + have hle : leastReported r { keys := ks, more := false } = ks.headD ([] : Key) := by + simp [leastReported, hrev] + have hge : greatestReported r { keys := ks, more := false } = ks.getLastD ([] : Key) := by + simp [greatestReported, hrev] + rw [compatible_complete_iff_bridge hrev hL hpair hlim, + compatible_complete_iff_bridge hrev hL hpair hlim] + unfold bridge + rw [← hle, ← hge] + refine ⟨fun ⟨h1, h2, h3, h4⟩ => ⟨?_, ?_, ?_, ?_⟩, + fun ⟨h1, h2, h3, h4⟩ => ⟨?_, ?_, ?_, ?_⟩⟩ + · exact (boundaryLo_empty_iff_of_agree h r _ hag).mp h1 + · exact (boundaryHi_empty_iff_of_agree h r _ hag).mp h2 + · exact (midEq_iff_of_agree r _ hrev hag).mp h3 + · exact (hbr db₁ db₂ hag).mp h4 + · exact (boundaryLo_empty_iff_of_agree h r _ hag).mpr h1 + · exact (boundaryHi_empty_iff_of_agree h r _ hag).mpr h2 + · exact (midEq_iff_of_agree r _ hrev hag).mpr h3 + · exact (hbr db₁ db₂ hag).mpr h4 + +/-- The middle interval equality at the reversed reported list is agreement-invariant +(`midEq` uses the reported interval, whose stored keys are fixed by agreement). -/ +theorem midEq_rev_iff_of_agree (r : RangeRead) (res : Result) (hrev : r.reverse = true) + {db₁ db₂ : DB} (hag : AgreeOn (completeRange r res) db₁ db₂) : + midEq res.keys.reverse db₁ ↔ midEq res.keys.reverse db₂ := by + have hag' : AgreeOn (reportedRange r res) db₁ db₂ := + keys_eq_of_subrange_agree (subrange_completeRange_reported r res) hag + have hle : leastReported r res = res.keys.getLastD ([] : Key) := by simp [leastReported, hrev] + have hge : greatestReported r res = res.keys.headD ([] : Key) := by + simp [greatestReported, hrev] + have hiff : ∀ db, midEq res.keys.reverse db ↔ + (reportedRange r res).keys db = res.keys.reverse := by + intro db + have h1 : res.keys.reverse.headD ([] : Key) = leastReported r res := by + rw [hle, List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse] + have h2 : res.keys.reverse.getLastD ([] : Key) = greatestReported r res := by + rw [hge, List.getLastD_eq_getLast?, List.headD_eq_head?, List.getLast?_reverse] + simp only [midEq, reportedRange, h1, h2, ConflictRange.keys_eq_inRange] + rw [hiff db₁, hiff db₂, hag'] + +/-- **Hull soundness, with the bracket isolated, reverse traversal.** Dual to +`sound_completeRange_of_bracketTransfer`: the reported list is descending, so the bridge +is stated for `res.keys.reverse`. -/ +theorem sound_completeRange_of_bracketTransfer_rev {r : RangeRead} {res : Result} + (h : EmptyInference) (hrev : r.reverse = true) (hmore : res.more = false) + (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· > ·)) + (hlim : RangeRead.withinLimit r res.keys) + (hbr : ∀ db₁ db₂ : DB, AgreeOn (completeRange r res) db₁ db₂ → + (bracket r res.keys.reverse db₁ ↔ bracket r res.keys.reverse db₂)) : + Sound r res (completeRange r res) := by + obtain ⟨ks, m⟩ := res + simp only at hmore hL hpair hlim hbr ⊢ + subst hmore + have hpairr : ks.reverse.Pairwise (· < ·) := by simpa using hpair.reverse + intro db₁ db₂ hag + have hle : leastReported r { keys := ks, more := false } = ks.getLastD ([] : Key) := by + simp [leastReported, hrev] + have hge : greatestReported r { keys := ks, more := false } = ks.headD ([] : Key) := by + simp [greatestReported, hrev] + rw [compatible_complete_iff_bridge_rev hrev hL hpair hlim, + compatible_complete_iff_bridge_rev hrev hL hpair hlim] + unfold bridge + have hr1 : ks.reverse.headD ([] : Key) = ks.getLastD ([] : Key) := by + rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse] + have hr2 : ks.reverse.getLastD ([] : Key) = ks.headD ([] : Key) := by + rw [List.getLastD_eq_getLast?, List.headD_eq_head?, List.getLast?_reverse] + rw [hr1, hr2, ← hle, ← hge] + refine ⟨fun ⟨h1, h2, h3, h4⟩ => ⟨?_, ?_, ?_, ?_⟩, + fun ⟨h1, h2, h3, h4⟩ => ⟨?_, ?_, ?_, ?_⟩⟩ + · exact (boundaryLo_empty_iff_of_agree h r _ hag).mp h1 + · exact (boundaryHi_empty_iff_of_agree h r _ hag).mp h2 + · exact (midEq_rev_iff_of_agree r _ hrev hag).mp h3 + · exact (hbr db₁ db₂ hag).mp h4 + · exact (boundaryLo_empty_iff_of_agree h r _ hag).mpr h1 + · exact (boundaryHi_empty_iff_of_agree h r _ hag).mpr h2 + · exact (midEq_rev_iff_of_agree r _ hrev hag).mpr h3 + · exact (hbr db₁ db₂ hag).mpr h4 + +/-- **Hull soundness from `Readable`.** In the forward complete case `Readable` supplies +the pairwise, limit, and nonemptiness facts, so `EmptyInference` and `BracketObservable` +alone give soundness. -/ +theorem sound_completeRange_of_emptyInference {r : RangeRead} {res : Result} + (h : EmptyInference) (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hL : res.keys ≠ []) + (hbr : BracketObservable r res) : + Sound r res (completeRange r res) := by + obtain ⟨db₀, hcomp⟩ := hread + have heq : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hpair : res.keys.Pairwise (· < ·) := by + rw [heq] + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + exact RangeRead.inRange_pairwise db₀ _ _ + exact sound_completeRange_of_bracketTransfer h hrev hmore hL hpair hcomp.1 hbr + +/-- **Hull soundness from `Readable`, reverse traversal.** -/ +theorem sound_completeRange_of_emptyInference_rev {r : RangeRead} {res : Result} + (h : EmptyInference) (hread : Readable r res) (hrev : r.reverse = true) + (hmore : res.more = false) (hL : res.keys ≠ []) + (hbr : BracketObservableRev r res) : + Sound r res (completeRange r res) := by + obtain ⟨db₀, hcomp⟩ := hread + have heq : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hpair : res.keys.Pairwise (· > ·) := by + rw [heq] + rw [RangeRead.completeKeys, ite_eq_left (by simp [hrev])] + exact List.Pairwise.reverse (RangeRead.inRange_pairwise db₀ _ _) + exact sound_completeRange_of_bracketTransfer_rev h hrev hmore hL hpair hcomp.1 hbr + +/-- **Minimality of the complete value, forward traversal, from the empty inference.** +Combines the derived hull soundness (`sound_completeRange_of_emptyInference`) with the +boundary `ψ` and the derived interior necessity. The only hypotheses beyond +`EmptyInference` are `BoundaryNecessity` and `BracketObservable`. -/ +theorem minimal_completeRange_of_factors {r : RangeRead} {res : Result} + (h : EmptyInference) (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hL : res.keys ≠ []) + (hψ : BoundaryNecessity r res) (hbr : BracketObservable r res) : + Minimal r res (completeRange r res) := + minimal_completeRange_of_boundaryNecessity hread hL hψ + (sound_completeRange_of_emptyInference h hread hrev hmore hL hbr) + +/-- **Minimality of the complete value, reverse traversal, from the empty inference.** -/ +theorem minimal_completeRange_of_factors_rev {r : RangeRead} {res : Result} + (h : EmptyInference) (hread : Readable r res) (hrev : r.reverse = true) + (hmore : res.more = false) (hL : res.keys ≠ []) + (hψ : BoundaryNecessity r res) (hbr : BracketObservableRev r res) : + Minimal r res (completeRange r res) := + minimal_completeRange_of_boundaryNecessity hread hL hψ + (sound_completeRange_of_emptyInference_rev h hread hrev hmore hL hbr) + +/-! +## The reduction: general necessity is reported necessity plus the empty ends + +`necessaryRange_completeRange_of_boundaryNecessity` derives the interior from +`Readable` and leaves only `BoundaryNecessity` as content. The point of this section is +that the general case factors *exactly* that way: the necessity of the complete value is +the necessity of the reported keys, the convex hull they span, and the two boundary empty +reads. The reported part is unconditional (`necessary_of_mem_result`), the interior is +convexity, and the only genuinely empty content is `BoundaryNecessity` — the empty theory +at the two boundary reads, stretched across the gaps to the reported keys. +-/ + +/-- **The reported interval is necessary from pointwise reported necessity.** This is +`necessaryRange_reportedRange` with the database witness abstracted away: any way of +knowing that each reported key is necessary — in practice `necessary_of_mem_result` — +fills the interior by convexity. -/ +theorem necessaryRange_reportedRange_of_reported {r : RangeRead} {res : Result} + (hmem : ∀ k ∈ res.keys, Necessary r res k) (hne : res.keys ≠ []) : + NecessaryRange r res (reportedRange r res).begin (reportedRange r res).end_ := by + intro k hlo hhi + simp only [reportedRange] at hlo hhi + have hle : leastReported r res ≤ k := WithTop.coe_le_coe.mp hlo + have hkg : k ≤ greatestReported r res := + (lt_keyAfter_iff k (greatestReported r res)).mp (WithTop.coe_lt_coe.mp hhi) + have hmem_lo : leastReported r res ∈ res.keys := by + unfold leastReported + split + · exact getLastD_mem hne + · exact headD_mem hne + have hmem_hi : greatestReported r res ∈ res.keys := by + unfold greatestReported + split + · exact headD_mem hne + · exact getLastD_mem hne + exact (hmem _ hmem_lo).of_between (hmem _ hmem_hi) hle hkg + +/-- **The reduction identity for complete necessity.** The complete value's necessary +range is the lower boundary `ψ`, then the reported interval (from pointwise reported +necessity, by convexity), then the upper boundary `ψ`. Nothing else enters. -/ +theorem necessaryRange_completeRange_of_reported {r : RangeRead} {res : Result} + (hmem : ∀ k ∈ res.keys, Necessary r res k) (hne : res.keys ≠ []) + (hψ : BoundaryNecessity r res) : + NecessaryRange r res (completeRange r res).begin (completeRange r res).end_ := + (hψ.1.trans (necessaryRange_reportedRange_of_reported hmem hne)).trans hψ.2 + +/-- **The complete branch, from reported necessity and the empty ends.** Soundness of +the assembled hull plus the reduction identity above give minimality. The reported keys +are supplied by `necessary_of_mem_result` in applications; this statement keeps them as +an explicit factor so the decomposition "reported + empty" is visible. -/ +theorem minimal_completeRange_of_reported {r : RangeRead} {res : Result} + (hmem : ∀ k ∈ res.keys, Necessary r res k) (hne : res.keys ≠ []) + (hψ : BoundaryNecessity r res) (hs : Sound r res (completeRange r res)) : + Minimal r res (completeRange r res) := + minimal_iff_sound_and_necessaryRange.mpr + ⟨hs, necessaryRange_completeRange_of_reported hmem hne hψ⟩ + +/-- **The complete branch, reduction form.** Reported necessity is now the unconditional +`necessary_of_mem_result`, so the only hypotheses beyond `Readable` are soundness of the +hull and the boundary `ψ`; the interior is derived. -/ +theorem minimal_completeRange_of_emptyEnds {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hψ : BoundaryNecessity r res) (hs : Sound r res (completeRange r res)) : + Minimal r res (completeRange r res) := + minimal_completeRange_of_reported (fun _ hk => necessary_of_mem_result hread hk) hne hψ hs + +/-! +### The boundary `ψ` from the empty joint bases + +`BoundaryNecessity` is the empty theory's `ψ` at the two boundary reads, transferred to +the full pair. The transfer is the factor-toggle joint-base construction; its two halves +are named here, forward and reversed, so that the boundary hypothesis can be discharged +from witnesses rather than assumed. +-/ + +/-- A result with `more = false` is exactly its key list paired with `false`. -/ +theorem res_eq_keys_more_false {res : Result} (hmore : res.more = false) : + res = { keys := res.keys, more := false } := by + cases res; simp_all + +/-- **Boundary necessity from the lower joint bases, forward.** The lower joint bases on +`[R.begin, k₀)` transfer the empty lower read's necessity to the full pair. -/ +theorem boundaryNecessity_low_of_jointBases {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = false) (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· < ·)) (hlim : RangeRead.withinLimit r res.keys) + (hlo : LowerJointBaseRange r res.keys (completeRange r res).begin + (reportedRange r res).begin) : + NecessaryRange r res (completeRange r res).begin (reportedRange r res).begin := + necessaryRange_of_lowerJointBaseRange hrev hL hpair hlim + (res_eq_keys_more_false hmore) hlo + +/-- **Boundary necessity from the upper joint bases, forward.** -/ +theorem boundaryNecessity_high_of_jointBases {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = false) (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· < ·)) (hlim : RangeRead.withinLimit r res.keys) + (hhi : UpperJointBaseRange r res.keys (reportedRange r res).end_ + (completeRange r res).end_) : + NecessaryRange r res (reportedRange r res).end_ (completeRange r res).end_ := + necessaryRange_of_upperJointBaseRange hrev hL hpair hlim + (res_eq_keys_more_false hmore) hhi + +/-- **Boundary necessity from the lower joint bases, reversed.** -/ +theorem boundaryNecessity_low_of_jointBasesRev {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hmore : res.more = false) (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· > ·)) (hlim : RangeRead.withinLimit r res.keys) + (hlo : LowerJointBaseRangeRev r res.keys (completeRange r res).begin + (reportedRange r res).begin) : + NecessaryRange r res (completeRange r res).begin (reportedRange r res).begin := + necessaryRange_of_lowerJointBaseRangeRev hrev hL hpair hlim + (res_eq_keys_more_false hmore) hlo + +/-- **Boundary necessity from the upper joint bases, reversed.** -/ +theorem boundaryNecessity_high_of_jointBasesRev {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hmore : res.more = false) (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· > ·)) (hlim : RangeRead.withinLimit r res.keys) + (hhi : UpperJointBaseRangeRev r res.keys (reportedRange r res).end_ + (completeRange r res).end_) : + NecessaryRange r res (reportedRange r res).end_ (completeRange r res).end_ := + necessaryRange_of_upperJointBaseRangeRev hrev hL hpair hlim + (res_eq_keys_more_false hmore) hhi + +/-- **The complete branch, from the empty joint bases.** This is the end-to-end +reduction statement: reported necessity is derived (`necessary_of_mem_result`), the +interior is convexity, the two boundary ranges come from the empty theory's joint-base +witnesses, and the only remaining hypothesis is hull soundness. -/ +theorem minimal_completeRange_of_jointBasesEmpty {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· < ·)) + (hlim : RangeRead.withinLimit r res.keys) + (hlo : LowerJointBaseRange r res.keys (completeRange r res).begin + (reportedRange r res).begin) + (hhi : UpperJointBaseRange r res.keys (reportedRange r res).end_ + (completeRange r res).end_) + (hs : Sound r res (completeRange r res)) : + Minimal r res (completeRange r res) := + minimal_completeRange_of_emptyEnds hread hL + ⟨boundaryNecessity_low_of_jointBases hrev hmore hL hpair hlim hlo, + boundaryNecessity_high_of_jointBases hrev hmore hL hpair hlim hhi⟩ hs + +/-- **The complete branch, from the empty joint bases, reverse traversal.** -/ +theorem minimal_completeRange_of_jointBasesEmptyRev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = false) + (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· > ·)) + (hlim : RangeRead.withinLimit r res.keys) + (hlo : LowerJointBaseRangeRev r res.keys (completeRange r res).begin + (reportedRange r res).begin) + (hhi : UpperJointBaseRangeRev r res.keys (reportedRange r res).end_ + (completeRange r res).end_) + (hs : Sound r res (completeRange r res)) : + Minimal r res (completeRange r res) := + minimal_completeRange_of_emptyEnds hread hL + ⟨boundaryNecessity_low_of_jointBasesRev hrev hmore hL hpair hlim hlo, + boundaryNecessity_high_of_jointBasesRev hrev hmore hL hpair hlim hhi⟩ hs + +/-- **The joint-base boundary hypothesis, selected by traversal.** The two boundary +ranges of `BoundaryNecessity`, obtained from the empty theory's flip witnesses, in +whichever traversal order the read uses. This is the exact input the complete branch +extracts from the empty theory beyond `EmptyInference`; the extraction itself is the +length-sensitive graft recorded in the notes. It is *false* — the endpoints are those of the +(unsound) hull, and `BoundaryNecessity` on them can demand keys that no sound range need +contain (`FormalConflictRange.Counterexample.cw_notBoundaryJointBases`). -/ +def BoundaryJointBases (r : RangeRead) (res : Result) : Prop := + if r.reverse then + LowerJointBaseRangeRev r res.keys (completeRange r res).begin + (reportedRange r res).begin ∧ + UpperJointBaseRangeRev r res.keys (reportedRange r res).end_ + (completeRange r res).end_ + else + LowerJointBaseRange r res.keys (completeRange r res).begin + (reportedRange r res).begin ∧ + UpperJointBaseRange r res.keys (reportedRange r res).end_ + (completeRange r res).end_ + +/-- **The complete branch, from `EmptyInference` and the joint bases.** Soundness of the +hull is derived from `EmptyInference` once the bracket factor is supplied; necessity is +the derived reported interval plus the boundary ranges from the joint bases. Compared +with `minimal_completeRange_of_factors`, the boundary `BoundaryNecessity` hypothesis is +replaced by the lower-level joint bases, so the reduction exhibits the witnesses rather +than assuming the conclusion. -/ +theorem minimal_completeRange_of_factors_joint {r : RangeRead} {res : Result} + (h : EmptyInference) (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· < ·)) + (hlim : RangeRead.withinLimit r res.keys) + (hjoint : BoundaryJointBases r res) (hbr : BracketObservable r res) : + Minimal r res (completeRange r res) := by + unfold BoundaryJointBases at hjoint + rw [ite_eq_right (by simp [hrev])] at hjoint + exact minimal_completeRange_of_jointBasesEmpty hread hrev hmore hL hpair hlim + hjoint.1 hjoint.2 + (sound_completeRange_of_bracketTransfer h hrev hmore hL hpair hlim hbr) + +/-- **The complete branch, from `EmptyInference` and the joint bases, reverse.** -/ +theorem minimal_completeRange_of_factors_joint_rev {r : RangeRead} {res : Result} + (h : EmptyInference) (hread : Readable r res) (hrev : r.reverse = true) + (hmore : res.more = false) (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· > ·)) + (hlim : RangeRead.withinLimit r res.keys) + (hjoint : BoundaryJointBases r res) (hbr : BracketObservableRev r res) : + Minimal r res (completeRange r res) := by + unfold BoundaryJointBases at hjoint + rw [ite_eq_left (by simp [hrev])] at hjoint + exact minimal_completeRange_of_jointBasesEmptyRev hread hrev hmore hL hpair hlim + hjoint.1 hjoint.2 + (sound_completeRange_of_bracketTransfer_rev h hrev hmore hL hpair hlim hbr) + +/-- **The complete branch, from `EmptyInferenceWitness` and the joint bases.** The +witness-exposing oracle supplies soundness (`EmptyInference` via +`emptyInference_of_witness`); the joint bases remain the one extraction hypothesis (false in +general: `FormalConflictRange.Counterexample.cw_notBoundaryJointBases`). This +is the sharpest form of the reduction available: the reported keys and their convex hull +are derived, and the boundary necessity is stated at the witness level rather than as the +finished `BoundaryNecessity`. -/ +theorem minimal_completeRange_of_witness_joint {r : RangeRead} {res : Result} + (h : EmptyInferenceWitness) (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· < ·)) + (hlim : RangeRead.withinLimit r res.keys) + (hjoint : BoundaryJointBases r res) (hbr : BracketObservable r res) : + Minimal r res (completeRange r res) := + minimal_completeRange_of_factors_joint (emptyInference_of_witness h) hread hrev hmore + hL hpair hlim hjoint hbr + +/-- **The complete branch, from `EmptyInferenceWitness` and the joint bases, reverse.** -/ +theorem minimal_completeRange_of_witness_joint_rev {r : RangeRead} {res : Result} + (h : EmptyInferenceWitness) (hread : Readable r res) (hrev : r.reverse = true) + (hmore : res.more = false) (hL : res.keys ≠ []) (hpair : res.keys.Pairwise (· > ·)) + (hlim : RangeRead.withinLimit r res.keys) + (hjoint : BoundaryJointBases r res) (hbr : BracketObservableRev r res) : + Minimal r res (completeRange r res) := + minimal_completeRange_of_factors_joint_rev (emptyInference_of_witness h) hread hrev hmore + hL hpair hlim hjoint hbr + +/-- **The joint bases reach `BoundaryNecessity`, forward.** The witness-level boundary +content packages into the finished two-range hypothesis used by the abstract correctness +theorem. -/ +theorem boundaryNecessity_of_jointBases {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = false) (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· < ·)) (hlim : RangeRead.withinLimit r res.keys) + (hjoint : BoundaryJointBases r res) : BoundaryNecessity r res := by + unfold BoundaryJointBases at hjoint + rw [ite_eq_right (by simp [hrev])] at hjoint + exact ⟨boundaryNecessity_low_of_jointBases hrev hmore hL hpair hlim hjoint.1, + boundaryNecessity_high_of_jointBases hrev hmore hL hpair hlim hjoint.2⟩ + +/-- **The joint bases reach `BoundaryNecessity`, reverse.** -/ +theorem boundaryNecessity_of_jointBases_rev {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hmore : res.more = false) (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· > ·)) (hlim : RangeRead.withinLimit r res.keys) + (hjoint : BoundaryJointBases r res) : BoundaryNecessity r res := by + unfold BoundaryJointBases at hjoint + rw [ite_eq_left (by simp [hrev])] at hjoint + exact ⟨boundaryNecessity_low_of_jointBasesRev hrev hmore hL hpair hlim hjoint.1, + boundaryNecessity_high_of_jointBasesRev hrev hmore hL hpair hlim hjoint.2⟩ + +/-! +## The truncated branches, from the existing certificates + +For a forward truncated result the upper end is the bare successor of the greatest +reported key; for a reverse truncated result the lower end is the bare least +reported key and the begin selector drops out. At offset one, when the corresponding +boundary read is not trivially empty, these are exactly the certified +`minimal_truncatedRange_of_offset_one` and `minimal_truncatedRangeRev_of_offset_one`. +-/ + +/-- At a positive begin offset, the lower boundary read (whose end is offset one) +has `emptyValue` the canonical range `[begin.key, k₀)`: it is not trivially empty by +hypothesis, and its end offset is one, so `emptyValue` takes the canonical branch. -/ +theorem lowBound_eq_canonical_of_pos {r : RangeRead} {res : Result} + (_hbegin : 1 ≤ r.begin.offset) + (hempty : ¬ AlwaysEmpty (boundaryReadLo r (leastReported r res))) : + lowBound r res = + ⟨r.begin.key, (leastReported r res : WithTop Key)⟩ := by + unfold lowBound emptyValue + rw [ite_eq_right hempty, + ite_eq_right (show ¬ (boundaryReadLo r (leastReported r res)).begin.offset ≤ 0 by + simp only [boundaryReadLo]; omega), + ite_eq_left (show (boundaryReadLo r (leastReported r res)).end_.offset ≤ 1 + from le_of_eq rfl)] + rfl + +/-- At an end offset at most one, the upper boundary read (whose begin is offset one) +has `emptyValue` the canonical range `[keyAfter kₙ, end.key)`. -/ +theorem highBound_eq_canonical_of_end_le_one {r : RangeRead} {res : Result} + (hend : r.end_.offset ≤ 1) + (hempty : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) : + highBound r res = + ⟨(keyAfter (greatestReported r res) : WithTop Key), r.end_.key⟩ := by + unfold highBound emptyValue + rw [ite_eq_right hempty, + ite_eq_right (show ¬ (boundaryReadHi r (greatestReported r res)).begin.offset ≤ 0 by + simp only [boundaryReadHi, selAt_offset]; norm_num), + ite_eq_left (show (boundaryReadHi r (greatestReported r res)).end_.offset ≤ 1 from hend)] + rfl + +/-- **`BoundaryNecessity` in the canonical region.** When the boundary contributions +have their canonical endpoints (`lowBound = [begin.key, k₀)` and +`highBound = [keyAfter kₙ, end.key)`) and they abut the reported interval, the complete +value is exactly `[begin.key, end.key)` and `BoundaryNecessity` follows from the two +end-key necessity theorems. No joint base is needed here. -/ +theorem boundaryNecessity_canonical {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hpair : res.keys.Pairwise (· < ·)) + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) + (hWe : ¬ AlwaysEmpty (boundaryReadLo r (leastReported r res))) + (hUe : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) + {v : Key} (hkey : r.begin.key = (↑v : WithTop Key)) + (hordB : r.begin.key ≤ (leastReported r res : WithTop Key)) + (hordK : (keyAfter (greatestReported r res) : WithTop Key) ≤ r.end_.key) : + BoundaryNecessity r res := by + have hW := lowBound_eq_canonical_of_pos hbegin hWe + have hU := highBound_eq_canonical_of_end_le_one hend hUe + have hmid : (↑(leastReported r res) : WithTop Key) ≤ + (↑(keyAfter (greatestReported r res)) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt + (least_le_greatest_fwd hrev hpair hne) (keyAfter_self_lt _))) + have hc : completeRange r res = ⟨r.begin.key, r.end_.key⟩ := by + unfold completeRange reportedRange + rw [hW, hU] + exact hull3 r.begin.key (leastReported r res : WithTop Key) + (keyAfter (greatestReported r res) : WithTop Key) r.end_.key hordB hmid hordK + refine ⟨?_, ?_⟩ + · rw [hc]; simp only [reportedRange] + exact necessaryRange_low_fwd hread hrev hne hbegin hkey + · rw [hc]; simp only [reportedRange] + exact necessaryRange_high_fwd hread hrev hmore hne hend + +/-- For a reversed read the least reported key is at or below the greatest. -/ +theorem least_le_greatest_rev {r : RangeRead} {res : Result} (hrev : r.reverse = true) + (hpair : res.keys.Pairwise (· > ·)) (hne : res.keys ≠ []) : + leastReported r res ≤ greatestReported r res := by + have hleast : leastReported r res = res.keys.getLastD ([] : Key) := by simp [leastReported, hrev] + have hgreat : greatestReported r res = res.keys.headD ([] : Key) := by + simp [greatestReported, hrev] + rw [hleast, hgreat] + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlastD] + have hp : res.keys.Pairwise (· ≥ ·) := hpair.imp (fun hab => le_of_lt hab) + exact hp.rel_getLast (headD_mem hne) + +/-- **Lower boundary necessity, reverse.** At a positive begin offset and a complete +result, the begin key is necessary (`necessary_begin_key_rev`) and the least reported +key is necessary; convexity fills `[begin.key, k₀)`. -/ +theorem necessaryRange_low_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = false) + (hlen : 0 < res.keys.length) (hoff : 1 ≤ r.begin.offset) + {v : Key} (hkey : r.begin.key = (↑v : WithTop Key)) : + NecessaryRange r res r.begin.key ((leastReported r res : Key) : WithTop Key) := by + have hv : Necessary r res v := necessary_begin_key_rev hread hrev hmore hlen hoff hkey + have hget : leastReported r res = res.keys.getLastD ([] : Key) := by simp [leastReported, hrev] + have hle : leastReported r res ∈ res.keys := by + rw [hget]; exact getLastD_mem (List.ne_nil_of_length_pos hlen) + have hk0 : Necessary r res (leastReported r res) := necessary_of_mem_result hread hle + intro k hlo hhi + rw [hkey] at hlo + exact hv.of_between hk0 (WithTop.coe_le_coe.mp hlo) (le_of_lt (WithTop.coe_lt_coe.mp hhi)) + +/-- **Upper boundary necessity, reverse.** With an end offset at most one, the keys from +`keyAfter kₙ` up to `end_.key` are necessary (`necessary_head_to_end_key`). -/ +theorem necessaryRange_high_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (_hmore : res.more = false) + (hlen : 0 < res.keys.length) (hend : r.end_.offset ≤ 1) : + NecessaryRange r res ((keyAfter (greatestReported r res) : Key) : WithTop Key) r.end_.key := by + have hhead : greatestReported r res = res.keys.headD ([] : Key) := by + simp [greatestReported, hrev] + have hne : res.keys ≠ [] := List.ne_nil_of_length_pos hlen + intro k hlo hhi cr hs + have hk0 : res.keys.head hne ≤ k := by + rw [hhead] at hlo + have h : res.keys.headD ([] : Key) ≤ k := by + by_contra hcon + have hle : k ≤ res.keys.headD ([] : Key) := le_of_not_ge hcon + have : (k : WithTop Key) < (↑(keyAfter (res.keys.headD ([] : Key))) : WithTop Key) := + WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k (res.keys.headD ([] : Key))).mpr hle) + exact absurd hlo (not_le.mpr this) + rw [List.headD_eq_head?, List.head?_eq_some_head hne] at h + exact h + exact necessary_head_to_end_key hread hrev hne hend cr hs hk0 hhi + +/-- **`BoundaryNecessity` in the canonical region, reverse traversal.** Dual to +`boundaryNecessity_canonical`. -/ +theorem boundaryNecessity_canonical_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = false) + (hne : res.keys ≠ []) (hpair : res.keys.Pairwise (· > ·)) + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) + (hWe : ¬ AlwaysEmpty (boundaryReadLo r (leastReported r res))) + (hUe : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) + {v : Key} (hkey : r.begin.key = (↑v : WithTop Key)) + (hordB : r.begin.key ≤ (leastReported r res : WithTop Key)) + (hordK : (keyAfter (greatestReported r res) : WithTop Key) ≤ r.end_.key) : + BoundaryNecessity r res := by + have hW := lowBound_eq_canonical_of_pos hbegin hWe + have hU := highBound_eq_canonical_of_end_le_one hend hUe + have hmid : (↑(leastReported r res) : WithTop Key) ≤ + (↑(keyAfter (greatestReported r res)) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt + (least_le_greatest_rev hrev hpair hne) (keyAfter_self_lt _))) + have hc : completeRange r res = ⟨r.begin.key, r.end_.key⟩ := by + unfold completeRange reportedRange + rw [hW, hU] + exact hull3 r.begin.key (leastReported r res : WithTop Key) + (keyAfter (greatestReported r res) : WithTop Key) r.end_.key hordB hmid hordK + refine ⟨?_, ?_⟩ + · rw [hc]; simp only [reportedRange] + exact necessaryRange_low_rev hread hrev hmore (List.length_pos_of_ne_nil hne) hbegin hkey + · rw [hc]; simp only [reportedRange] + exact necessaryRange_high_rev hread hrev hmore (List.length_pos_of_ne_nil hne) hend + +/-- **The complete value is the canonical range in the canonical region.** When both +boundary contributions have canonical endpoints and abut the reported interval, the +three-piece hull collapses to `[begin.key, end.key)`. -/ +theorem completeRange_eq_canonical {r : RangeRead} {res : Result} + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) + (hWe : ¬ AlwaysEmpty (boundaryReadLo r (leastReported r res))) + (hUe : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) + (hordB : r.begin.key ≤ (leastReported r res : WithTop Key)) + (hmid : (leastReported r res : WithTop Key) ≤ (keyAfter (greatestReported r res) : WithTop Key)) + (hordK : (keyAfter (greatestReported r res) : WithTop Key) ≤ r.end_.key) : + completeRange r res = canonicalRange r := by + have hW := lowBound_eq_canonical_of_pos hbegin hWe + have hU := highBound_eq_canonical_of_end_le_one hend hUe + unfold completeRange canonicalRange reportedRange + rw [hW, hU] + exact hull3 r.begin.key (leastReported r res : WithTop Key) + (keyAfter (greatestReported r res) : WithTop Key) r.end_.key hordB hmid hordK + +/-- **The complete branch in the canonical region needs no boundary hypothesis.** At +`begin.offset = 1`, `end.offset ≤ 1`, and canonical endpoints, the complete value is the +canonical range and its minimality is the existing +`minimal_canonicalRange_of_end_offset_le_one` — neither `BoundaryNecessity` nor +`BracketFactor` is required. -/ +theorem minimal_completeRange_canonical {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hpair : res.keys.Pairwise (· < ·)) + (hbegin1 : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) + (hWe : ¬ AlwaysEmpty (boundaryReadLo r (leastReported r res))) + (hUe : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) + (hordB : r.begin.key ≤ (leastReported r res : WithTop Key)) + (hordK : (keyAfter (greatestReported r res) : WithTop Key) ≤ r.end_.key) : + Minimal r res (completeRange r res) := by + have hmid : (leastReported r res : WithTop Key) ≤ + (keyAfter (greatestReported r res) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt + (least_le_greatest_fwd hrev hpair hne) (keyAfter_self_lt _))) + have hc := completeRange_eq_canonical (r := r) (res := res) + (by omega) hend hWe hUe hordB hmid hordK + rw [hc] + exact minimal_canonicalRange_of_end_offset_le_one hread hmore hne hbegin1 hend + +/-- **Forward truncated value equals the certified truncated range.** With +`reverse = false` and the lower boundary read not trivially empty, the forward truncated +value is `[begin.key, keyAfter (greatest reported))`. -/ +theorem forwardTruncatedRange_eq_truncatedRange {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hempty : ¬ AlwaysEmpty (boundaryReadLo r (leastReported r res))) : + forwardTruncatedRange r res = + truncatedRange r (res.keys.getLastD ([] : Key)) := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + have hgreat : greatestReported r res = res.keys.getLastD ([] : Key) := by + simp [greatestReported, hrev] + rw [forwardTruncatedRange, hgreat, truncatedRange, lowBound_eq_canonical_of_pos hbegin hempty] + +/-- **Forward truncated, offset one.** With `begin.offset = 1` and `end.offset = 1`, +and the lower boundary read not trivially empty, the forward truncated value is minimal: +it matches `minimal_truncatedRange_of_offset_one`. -/ +theorem minimal_forwardTruncatedRange_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) + (hempty : ¬ AlwaysEmpty (boundaryReadLo r (leastReported r res))) : + Minimal r res (forwardTruncatedRange r res) := by + rw [forwardTruncatedRange_eq_truncatedRange hrev (by omega) hempty] + exact minimal_truncatedRange_of_offset_one hread hmore hrev hbegin hend + +/-- The reverse truncated value equals the certified reversed truncated range, when +the upper boundary read is not trivially empty at an end offset at most one. -/ +theorem reverseTruncatedRange_eq_truncatedRangeRev {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hend : r.end_.offset ≤ 1) + (hempty : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) : + reverseTruncatedRange r res = + truncatedRangeRev r (res.keys.getLastD ([] : Key)) := by + have hleast : leastReported r res = res.keys.getLastD ([] : Key) := by + simp [leastReported, hrev] + rw [reverseTruncatedRange, hleast, truncatedRangeRev, + highBound_eq_canonical_of_end_le_one hend hempty] + +/-- **Reverse truncated, offset one.** With `begin.offset = 1` and `end.offset = 1`, +and the upper boundary read not trivially empty, the reverse truncated value is minimal: +it matches `minimal_truncatedRangeRev_of_offset_one`. -/ +theorem minimal_reverseTruncatedRange_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) + (hempty : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) : + Minimal r res (reverseTruncatedRange r res) := by + rw [reverseTruncatedRange_eq_truncatedRangeRev hrev (by omega) hempty] + exact minimal_truncatedRangeRev_of_offset_one hread hmore hrev hbegin hend + +/-! +## The truncated branches through the empty boundary value + +The forward/reverse truncated values are the lower/upper boundary contribution's +begin/end glued to the reported interval. The offset-one certificates above are the +same statement via `emptyValue`; the general statement is the same reduction as the +complete branch, with the far boundary read replaced by the reported interval. Those +reductions are the remaining obligations of the truncated branches and are stated in +the notes. +-/ + +/-! +## The empty branch + +The empty result is answered by `emptyValue`, reading off the empty theory's cases. +The offset-one underEnd case is certified below; the remaining offsets are the open +empty theory. +-/ + +/-- **The empty branch, `begin.offset = 1`, `end.offset ≤ 1`.** A non-trivially-empty +read with `begin.offset = 1` and `end.offset ≤ 1` whose result is empty has +`emptyValue r = canonicalRange r` minimal, by +`minimal_canonicalRange_of_underEnd_empty`. -/ +theorem minimal_emptyValue_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hbegin : r.begin.offset = 1) + (hend : r.end_.offset ≤ 1) (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res (emptyValue r) := by + have hval : emptyValue r = canonicalRange r := by + unfold emptyValue + rw [ite_eq_right hgap, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), ite_eq_left hend] + rw [hval] + exact minimal_canonicalRange_of_underEnd_empty hread hgap hbegin hend hmore hempty + +/-- **The empty branch, `begin.offset = 1`, `end.offset ≤ 1`, any emptiness.** With no +non-triviality hypothesis at all, `emptyValue r` is minimal for the empty result at +`begin.offset = 1`, `end.offset ≤ 1`: trivially empty reads use the empty range, the +rest the canonical range. This is the `EmptyInference` instance for this region. -/ +theorem minimal_emptyValue_underEnd (r : RangeRead) (hbegin : r.begin.offset = 1) + (hend : r.end_.offset ≤ 1) : + Minimal r { keys := [], more := false } (emptyValue r) := by + by_cases hAE : AlwaysEmpty r + · have : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + rw [this]; exact minimal_emptyRange_of_alwaysEmpty hAE + · have hv : emptyValue r = canonicalRange r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), ite_eq_left hend] + rw [hv] + exact minimal_canonicalRange_of_underEnd_empty + ⟨emptyDB, compatible_emptyDB_emptyRes r⟩ hAE hbegin hend rfl rfl + +/-! ### The empty inference at `begin.offset = 1`: reducing to forward reads + +Traversal is invisible to the empty inference, and `emptyValue` never mentions +`reverse`, so `EmptyInference` reduces to its non-reversed instance; and the +`begin.offset = 1` instance splits by the end offset into the three empty cases already +in the library. This pins the open empty-theory content to `begin.offset ≠ 1`. +-/ + +/-- `AlwaysEmpty` is invariant under flipping the traversal. -/ +theorem alwaysEmpty_reverse (r : RangeRead) : + AlwaysEmpty { r with reverse := !r.reverse } ↔ AlwaysEmpty r := by + unfold AlwaysEmpty + constructor <;> intro h db + · have hh := h db; rw [completeKeys_eq_nil_iff_inRange] at hh ⊢; exact hh + · have hh := h db; rw [completeKeys_eq_nil_iff_inRange] at hh ⊢; exact hh + +/-- `emptyValue` never mentions `reverse`, so it is invariant under flipping it. -/ +theorem emptyValue_reverse (r : RangeRead) : + emptyValue { r with reverse := !r.reverse } = emptyValue r := by + unfold emptyValue + by_cases h : AlwaysEmpty r + · rw [ite_eq_left ((alwaysEmpty_reverse r).mpr h), ite_eq_left h] + rfl + · rw [ite_eq_right (fun hh => h ((alwaysEmpty_reverse r).mp hh)), ite_eq_right h] + rfl + +/-- Compatibility with the empty result is invariant under flipping the traversal: +reversal only reverses the complete keys, which are empty on the same databases. -/ +theorem compatible_empty_flip {db : DB} {r : RangeRead} : + RangeRead.Compatible db r { keys := [], more := false } ↔ + RangeRead.Compatible db { r with reverse := !r.reverse } { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil] + cases hr : r.reverse <;> simp [RangeRead.completeKeys, hr] + +/-- Soundness for empty results is invariant under flipping the traversal. -/ +theorem sound_empty_flip {r : RangeRead} {cr : ConflictRange} : + Sound r { keys := [], more := false } cr ↔ + Sound { r with reverse := !r.reverse } { keys := [], more := false } cr := by + constructor <;> intro h d1 d2 hag + · rw [← compatible_empty_flip (db := d1), ← compatible_empty_flip (db := d2)]; exact h d1 d2 hag + · rw [compatible_empty_flip (db := d1), compatible_empty_flip (db := d2)]; exact h d1 d2 hag + +/-- Necessity for empty results is invariant under flipping the traversal. -/ +theorem necessary_empty_flip {r : RangeRead} {k : Key} : + Necessary r { keys := [], more := false } k ↔ + Necessary { r with reverse := !r.reverse } { keys := [], more := false } k := by + unfold Necessary + exact ⟨fun h cr hs => h cr (sound_empty_flip.mpr hs), + fun h cr hs => h cr (sound_empty_flip.mp hs)⟩ + +/-- Minimality for empty results is invariant under flipping the traversal. -/ +theorem minimal_empty_flip {r : RangeRead} {cr : ConflictRange} : + Minimal r { keys := [], more := false } cr ↔ + Minimal { r with reverse := !r.reverse } { keys := [], more := false } cr := by + rw [minimal_iff_sound_and_necessary, minimal_iff_sound_and_necessary] + exact ⟨fun h => ⟨sound_empty_flip.mp h.1, fun k hk => necessary_empty_flip.mp (h.2 k hk)⟩, + fun h => ⟨sound_empty_flip.mpr h.1, fun k hk => necessary_empty_flip.mpr (h.2 k hk)⟩⟩ + +/-- **`EmptyInference` reduces to its non-reversed instance.** -/ +theorem emptyInference_of_forward + (h : ∀ r' : RangeRead, r'.reverse = false → + Minimal r' { keys := [], more := false } (emptyValue r')) : + EmptyInference := by + intro r' _ + cases hr : r'.reverse + · exact h r' hr + · have hf := h { r' with reverse := false } rfl + rw [show ({ r' with reverse := false } : RangeRead) = + { r' with reverse := !r'.reverse } from by simp [hr]] at hf + rw [emptyValue_reverse, ← minimal_empty_flip] at hf + exact hf + +/-- **The empty inference at `begin.offset = 1`, cross-free overshoot.** -/ +theorem minimal_emptyValue_crossFree_fwd (r : RangeRead) (hbegin : r.begin.offset = 1) + (hend : 2 ≤ r.end_.offset) (hle : r.begin.key ≤ r.end_.key) (hrev : r.reverse = false) : + Minimal r { keys := [], more := false } (emptyValue r) := by + by_cases hAE : AlwaysEmpty r + · have : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + rw [this]; exact minimal_emptyRange_of_alwaysEmpty hAE + · have hv : emptyValue r = topRange r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), + ite_eq_right (by omega), ite_eq_left hle] + rw [hv] + exact minimal_topRange_of_end_offset_ge_two_empty + ⟨emptyDB, compatible_emptyDB_emptyRes r⟩ rfl hrev rfl hbegin hend hle + +/-- **The empty inference at `begin.offset = 1`, crossed overshoot.** The crossed case +holds for either traversal. -/ +theorem minimal_emptyValue_crossed (r : RangeRead) (hbegin : r.begin.offset = 1) + (hend : 2 ≤ r.end_.offset) (hcross : r.end_.key < r.begin.key) : + Minimal r { keys := [], more := false } (emptyValue r) := by + by_cases hAE : AlwaysEmpty r + · have : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + rw [this]; exact minimal_emptyRange_of_alwaysEmpty hAE + · have hv : emptyValue r = crossedRangeGeneral r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), + ite_eq_right (by omega), ite_eq_right (not_le.mpr hcross)] + rw [hv] + exact minimal_crossedRangeGeneral_of_empty rfl rfl (by omega) hend hcross hAE + +/-- **The empty inference at `begin.offset = 1`, non-reversed.** -/ +theorem emptyInference_beginOne_fwd (r : RangeRead) (hbegin : r.begin.offset = 1) + (hrev : r.reverse = false) : + Minimal r { keys := [], more := false } (emptyValue r) := by + by_cases hend : r.end_.offset ≤ 1 + · exact minimal_emptyValue_underEnd r hbegin hend + · have hend2 : 2 ≤ r.end_.offset := by omega + by_cases hle : r.begin.key ≤ r.end_.key + · exact minimal_emptyValue_crossFree_fwd r hbegin hend2 hle hrev + · exact minimal_emptyValue_crossed r hbegin hend2 (not_le.mp hle) + +/-- **The empty witness oracle at `begin.offset = 1`, non-reversed.** At this offset the +empty theory is complete on every end offset and traversal, so every key of `emptyValue` +has a flip witness. This is the witness-level companion of +`emptyInference_beginOne_fwd`; the witnesses are the ones built by the empty-result case +proofs. -/ +theorem emptyWitness_beginOne_fwd (r : RangeRead) (hbegin : r.begin.offset = 1) + (_hrev : r.reverse = false) + (hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + by_cases hend : r.end_.offset ≤ 1 + · have hv : emptyValue r = canonicalRange r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), ite_eq_left hend] + refine ⟨?_, ?_⟩ + · rw [hv] + exact (minimal_canonicalRange_of_underEnd_empty hread hAE hbegin hend rfl rfl).sound + · intro k hk + rw [hv] at hk + exact emptyWitness_underEnd hAE hbegin hend hk + · have hend2 : 2 ≤ r.end_.offset := by omega + by_cases hle : r.begin.key ≤ r.end_.key + · have hv : emptyValue r = topRange r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), + ite_eq_right (by omega), ite_eq_left hle] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_topRange_of_le_general (by omega) (by omega) hle rfl rfl + · intro k hk + rw [hv] at hk + exact emptyWitness_crossFree hread hbegin hend2 hle hk + · have hcross : r.end_.key < r.begin.key := not_le.mp hle + have hEB : r.end_.key ≤ r.begin.key := le_of_lt hcross + have hv : emptyValue r = crossedRangeGeneral r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), + ite_eq_right (by omega), ite_eq_right (not_le.mpr hcross)] + by_cases hgap : ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat) ≤ + intervalCard r.end_.key r.begin.key + · rw [crossedRangeGeneral, ite_eq_left hgap] at hv + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_top_end_of_crossed_general (by omega) (by omega) hEB rfl rfl + · intro k hk + rw [hv] at hk + rcases lt_or_ge (k : WithTop Key) r.begin.key with hkB | hkB + · exact emptyWitness_crossed_interval hbegin hend2 hAE (by simpa [hbegin] using hgap) + hk.1 hkB + · exact emptyWitness_crossed_beginLe hbegin hend2 hkB + · rw [crossedRangeGeneral, ite_eq_right hgap] at hv + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_top_begin_of_short_general (by omega) (by omega) hEB (not_le.mp hgap) rfl rfl + · intro k hk + rw [hv] at hk + exact emptyWitness_crossed_beginLe hbegin hend2 hk.1 + +/-- **The empty inference at `begin.offset = 1`, any traversal.** Combines the +non-reversed instance with traversal-invariance. -/ +theorem emptyInference_beginOne (r : RangeRead) (hbegin : r.begin.offset = 1) : + Minimal r { keys := [], more := false } (emptyValue r) := by + cases hrev : r.reverse + · exact emptyInference_beginOne_fwd r hbegin hrev + · have hf := emptyInference_beginOne_fwd { r with reverse := false } hbegin rfl + rw [show ({ r with reverse := false } : RangeRead) = + { r with reverse := !r.reverse } from by simp [hrev]] at hf + rw [emptyValue_reverse, ← minimal_empty_flip] at hf + exact hf + +/-- **The empty flip witness is traversal-invariant.** Reversal only reverses the +complete keys, which are empty on the same databases (`compatible_empty_flip`), so a +flip witness for one traversal is a flip witness for the other. -/ +theorem emptyWitness_flip {r : RangeRead} {k : Key} : + EmptyWitness { r with reverse := !r.reverse } k ↔ EmptyWitness r k := by + unfold EmptyWitness + constructor + · rintro ⟨db, hc, hn⟩ + exact ⟨db, (compatible_empty_flip (db := db) (r := r)).mpr hc, + fun h => hn ((compatible_empty_flip (db := toggle db k) (r := r)).mp h)⟩ + · rintro ⟨db, hc, hn⟩ + exact ⟨db, (compatible_empty_flip (db := db) (r := r)).mp hc, + fun h => hn ((compatible_empty_flip (db := toggle db k) (r := r)).mpr h)⟩ + +/-- **The empty witness oracle at `begin.offset = 1`, any traversal.** The witness-level +companion of `emptyInference_beginOne`: soundness and a flip witness at every key of +`emptyValue`. This is `EmptyInferenceWitness` restricted to `begin.offset = 1`. -/ +theorem emptyWitness_beginOne (r : RangeRead) (hbegin : r.begin.offset = 1) + (hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + cases hrev : r.reverse + · exact emptyWitness_beginOne_fwd r hbegin hrev hread + · have hread' : Readable { r with reverse := false } { keys := [], more := false } := by + obtain ⟨db, hc⟩ := hread + refine ⟨db, ?_⟩ + have := (compatible_empty_flip (db := db) (r := r)).mp hc + rwa [show ({ r with reverse := !r.reverse } : RangeRead) = { r with reverse := false } + from by simp [hrev]] at this + have hf := emptyWitness_beginOne_fwd { r with reverse := false } hbegin rfl hread' + rw [show ({ r with reverse := false } : RangeRead) = + { r with reverse := !r.reverse } from by simp [hrev]] at hf + refine ⟨?_, ?_⟩ + · simpa [emptyValue_reverse] using (sound_empty_flip (r := r)).mpr hf.1 + · intro k hk + rw [← emptyValue_reverse] at hk + exact emptyWitness_flip.mp (hf.2 k hk) + +/-- **The exact residual of the empty inference.** Everything at `begin.offset = 1` is +proved; only `begin.offset ≠ 1` remains. -/ +def EmptyInferenceOffOne : Prop := + ∀ r' : RangeRead, r'.begin.offset ≠ 1 → Readable r' { keys := [], more := false } → + Minimal r' { keys := [], more := false } (emptyValue r') + +/-- **The exact residual of the witness oracle.** Everything at `begin.offset = 1` is +proved in `emptyWitness_beginOne`; only `begin.offset ≠ 1` remains. This is the +witness-level analogue of `EmptyInferenceOffOne`, and the form the general reduction's +empty content reduces to. -/ +def EmptyInferenceWitnessOffOne : Prop := + ∀ r' : RangeRead, r'.begin.offset ≠ 1 → Readable r' { keys := [], more := false } → + Sound r' { keys := [], more := false } (emptyValue r') ∧ + ∀ k : Key, k ∈ emptyValue r' → EmptyWitness r' k + +/-- **The genuinely open residual of the witness oracle.** At `begin.offset = 1` +(`emptyWitness_beginOne`), throughout `begin.offset ≥ 1` with `end.offset ≤ 1` +(`emptyWitness_underEnd_gen`), throughout the overshoot region (`end.offset ≥ 2`: both +`begin.offset ≥ 1` by `emptyWitness_overshoot` and `begin.offset ≤ 0` by +`emptyWitness_nonpos_overshoot`), all of `begin.offset ≤ 0`, `end.offset = 1`, and the +cross-free `begin.offset ≤ 0`, `end.offset ≤ 0` case +(`emptyWitness_nonpos_end_nonpos_crossFree`), and the crossed nonpositive-end short gap +(`emptyWitness_nonpos_crossed_short`, criterion `cnt end.key ≤ 1 - end.offset`), the oracle +is proved. What remains is the nonpositive begin offset with `end.offset ≤ 0`, crossed +selectors (`end.key < begin.key`) and a long gap +(`end.offset - begin.offset ≤ intervalCard end.key begin.key`), where `emptyValue` is the +bottom range `[⊥, begin.key)` and the gap is long enough that both disjuncts of +`compatible_empty_iff_nonpos_crossed_long` can fire. This is the residual the whole empty +theory now reduces to. -/ +def EmptyInferenceWitnessResidual : Prop := + ∀ r' : RangeRead, r'.begin.offset ≤ 0 → r'.end_.offset ≤ 0 → r'.end_.key < r'.begin.key → + (((r'.end_.offset - r'.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r'.end_.key r'.begin.key → + Readable r' { keys := [], more := false } → + Sound r' { keys := [], more := false } (emptyValue r') ∧ + ∀ k : Key, k ∈ emptyValue r' → EmptyWitness r' k + +/-- **`EmptyInferenceWitnessOffOne` from its off-one residual.** -/ +theorem emptyInferenceWitness_of_offOne (h : EmptyInferenceWitnessOffOne) : + EmptyInferenceWitness := by + intro r' hr + by_cases hb : r'.begin.offset = 1 + · exact emptyWitness_beginOne r' hb hr + · exact h r' hb hr + + +/-- **`EmptyInferenceOffOne` from its witness residual.** The witness oracle implies the +abstract oracle (`emptyInference_of_witness`), so the witness residual at off-one offsets +supplies the abstract one. -/ +theorem emptyInferenceOffOne_of_witnessOffOne (h : EmptyInferenceWitnessOffOne) : + EmptyInferenceOffOne := by + intro r' hb hr + have hw := emptyInference_of_witness (emptyInferenceWitness_of_offOne h) + exact hw r' hr + +/-- **`EmptyInference` from its off-`begin.offset = 1` residual.** The +`begin.offset = 1` instance is `emptyInference_beginOne`. -/ +theorem emptyInference_of_offOne (h : EmptyInferenceOffOne) : EmptyInference := by + intro r' hr + by_cases hb : r'.begin.offset = 1 + · exact emptyInference_beginOne r' hb + · exact h r' hb hr + +/-! +## The general obligation and correctness +-/ + +/-- **The nonempty-branch obligation.** The empty branch is discharged by the empty +inference (`minimal_emptyValue_of_emptyInference`) and the complete branch by +`minimal_completeRange_of_factors`/`_rev`. Only the two truncated fields remain: they +are the direct minimality statements for their values, using the same boundary transfer +together with the reported interval. -/ +structure GeneralObligation (r : RangeRead) (res : Result) : Prop where + /-- A forward truncated result: `[w, keyAfter kₙ)` is minimal. -/ + fwdTrunc : res.more = true → r.reverse = false → res.keys ≠ [] → + Minimal r res (forwardTruncatedRange r res) + /-- A reverse truncated result: `[kₙ, u)` is minimal. -/ + revTrunc : res.more = true → r.reverse = true → res.keys ≠ [] → + Minimal r res (reverseTruncatedRange r res) + +/-- **The complete-branch obligation, in push-down form.** Beyond the empty theory, the +complete branch needs exactly two things, neither mentioning an offset: + +* `joint`, the generic graft: the empty boundary reads' flip witnesses extend to + full-compatible databases, i.e. `BoundaryJointBases`; +* `bracket`, the observability residue, itself an instance of the empty gap split. + +Everything else in the complete branch — reported-key necessity, convexity, the middle +interval, and the three derivable soundness factors — is derived. -/ +structure CompleteObligation (r : RangeRead) (res : Result) : Prop where + /-- The boundary reads' empty witnesses, as full-compatible joint bases. -/ + joint : BoundaryJointBases r res + /-- The bridge's fourth factor: agreement on the complete value fixes the bracket. -/ + bracket : BracketFactor r res + +/-- **The general algorithm's correctness, pushed down to the empty theory.** The +hypotheses are the witness-exposing empty oracle, the complete-branch obligation (a +generic graft plus the bracket factor), and the truncated obligation. No hypothesis +mentions an offset, `EmptyInference` as such, or the finished `BoundaryNecessity`: the +arbitrary offset handling lives entirely inside `EmptyInferenceWitness` and the two +obligations, while the general case is the offset-free assembly. -/ +theorem conflictRangeVal_spec_of_witness {r : RangeRead} {res : Result} + (hempty : EmptyInferenceWitness) (h : GeneralObligation r res) + (hc : CompleteObligation r res) (hread : Readable r res) : + Minimal r res (conflictRangeVal r res) := by + unfold conflictRangeVal + by_cases hkeys : res.keys = [] + · rw [ite_eq_left hkeys] + exact minimal_emptyValue_of_emptyInference (emptyInference_of_witness hempty) hread hkeys + · rw [ite_eq_right hkeys] + by_cases hmore : res.more = true + · rw [ite_eq_left hmore] + by_cases hrev : r.reverse = true + · rw [ite_eq_left hrev]; exact h.revTrunc hmore hrev hkeys + · rw [ite_eq_right hrev]; exact h.fwdTrunc hmore (by simpa using hrev) hkeys + · rw [ite_eq_right hmore] + have hm : res.more = false := by simpa using hmore + unfold completeValue + split + · next hcan => + exact minimal_canonicalRange_of_end_offset_le_one_general hread hm hkeys hcan.1 hcan.2 + · next _hcan => + split + · next hover => + cases hr : r.reverse + · exact minimal_topRange_of_end_offset_ge_two_general hread hm hr hover.2.2.1 hover.1 + hover.2.1 hover.2.2.2 + · exact minimal_topRange_of_end_offset_ge_two_rev_general hread hm hr hover.2.2.1 hover.1 + hover.2.1 hover.2.2.2 + · next _hover => + obtain ⟨db, hcomp⟩ := hread + have hlim : RangeRead.withinLimit r res.keys := hcomp.1 + cases hr : r.reverse + · have hp : res.keys.Pairwise (· < ·) := (RangeRead.compatible_sortedLT hcomp hr).pairwise + have hbr := hc.bracket + unfold BracketFactor at hbr + rw [ite_eq_right (by simp [hr])] at hbr + exact minimal_completeRange_of_witness_joint hempty ⟨db, hcomp⟩ hr hm hkeys hp hlim + hc.joint hbr + · have hp : res.keys.Pairwise (· > ·) := (RangeRead.compatible_sortedGT hcomp hr).pairwise + have hbr := hc.bracket + unfold BracketFactor at hbr + rw [ite_eq_left (by simp [hr])] at hbr + exact minimal_completeRange_of_witness_joint_rev hempty ⟨db, hcomp⟩ hr hm hkeys hp hlim + hc.joint hbr + +/-- The existence conjecture follows from the witness-exposing empty oracle and the two +nonempty obligations: the value witnesses a minimal sound range, with all offset handling +confined to the empty theory. -/ +theorem conjecture_exists_minimal_conflict_range_of_witness {r : RangeRead} {res : Result} + (hempty : EmptyInferenceWitness) (h : GeneralObligation r res) + (hc : CompleteObligation r res) (hread : Readable r res) : + ∃ cr : ConflictRange, Minimal r res cr := + ⟨conflictRangeVal r res, conflictRangeVal_spec_of_witness hempty h hc hread⟩ + +/-- **The general algorithm's correctness, from the empty inference, the boundary `ψ`, +the bracket factor, and the truncated obligation.** The empty branch is the empty +inference; the complete branch is `minimal_completeRange_of_factors`/`_rev`, with soundness +derived from `EmptyInference` and `BracketFactor`; the truncated branches are `h`'s +fields. The proof is a case split mirroring the definition. -/ +theorem conflictRangeVal_spec {r : RangeRead} {res : Result} + (hempty : EmptyInference) (h : GeneralObligation r res) + (hread : Readable r res) (hψ : BoundaryNecessity r res) + (hbr : BracketFactor r res) : + Minimal r res (conflictRangeVal r res) := by + unfold conflictRangeVal + by_cases hkeys : res.keys = [] + · rw [ite_eq_left hkeys]; exact minimal_emptyValue_of_emptyInference hempty hread hkeys + · rw [ite_eq_right hkeys] + by_cases hmore : res.more = true + · rw [ite_eq_left hmore] + by_cases hrev : r.reverse = true + · rw [ite_eq_left hrev]; exact h.revTrunc hmore hrev hkeys + · rw [ite_eq_right hrev]; exact h.fwdTrunc hmore (by simpa using hrev) hkeys + · rw [ite_eq_right hmore] + have hm : res.more = false := by simpa using hmore + unfold completeValue + split + · next hcan => + exact minimal_canonicalRange_of_end_offset_le_one_general hread hm hkeys hcan.1 hcan.2 + · next _hcan => + split + · next hover => + cases hr : r.reverse + · exact minimal_topRange_of_end_offset_ge_two_general hread hm hr hover.2.2.1 hover.1 + hover.2.1 hover.2.2.2 + · exact minimal_topRange_of_end_offset_ge_two_rev_general hread hm hr hover.2.2.1 hover.1 + hover.2.1 hover.2.2.2 + · next _hover => + unfold BracketFactor at hbr + split at hbr + · next hr => + exact minimal_completeRange_of_factors_rev hempty hread hr hm hkeys hψ hbr + · next hr => + exact minimal_completeRange_of_factors hempty hread (by simpa using hr) hm hkeys + hψ hbr + +/-- The existence conjecture follows from the empty inference, the boundary `ψ`, the +bracket factor, and the truncated obligation: the value witnesses a minimal sound +range. -/ +theorem conjecture_exists_minimal_conflict_range {r : RangeRead} {res : Result} + (hempty : EmptyInference) (h : GeneralObligation r res) + (hread : Readable r res) (hψ : BoundaryNecessity r res) + (hbr : BracketFactor r res) : + ∃ cr : ConflictRange, Minimal r res cr := + ⟨conflictRangeVal r res, conflictRangeVal_spec hempty h hread hψ hbr⟩ + +/-! +## The complete empty theory + +This section packages the entire residual of the reduction into one hypothesis, +`EmptyTheoryComplete`, and proves the general algorithm correct from it. The hypothesis +is deliberately stated at the granularity of the empty theory's outputs rather than as +the finished general theorem: `witness` is the empty-result theory in flip-witness form +(the strength the graft consumes), `joint` is the generic graft that extends those +witnesses across the reported interval, `bracket` is the observability gap split, and +`truncated` is the one-sided graft for truncated results. Each is an instance of the +empty theory's analysis at the boundary reads; none is an offset analysis of the general +read. With them the general case is the offset-free assembly. + +`EmptyTheoryComplete` implies the abstract `EmptyInference` (`EmptyTheoryComplete.emptyInference`) +and the conjecture (`exists_minimal_conflict_range_of_complete`). It is the exact form of +"the empty theory is finished": there is nothing else to prove. In fact it is *refuted*: +all three of `joint`, `bracket`, and `truncated` are false, independently +(`FormalConflictRange.Counterexample.cw_emptyTheoryComplete_false`, +`ce_emptyTheoryComplete_false`, `tr_emptyTheoryComplete_false`), so the structure is +uninhabited and the reduction is not a route to the headline. -/ + +/-- **A complete empty theory, in reduction form.** The empty-result theory together with +the generic graft, the bracket gap split, and the truncated one-sided graft. Every field +is an output of the empty theory's analysis at a boundary read; in particular no field +mentions an offset. -/ +structure EmptyTheoryComplete : Prop where + /-- The empty-result theory in flip-witness form: the strength the graft needs, which + an abstract `Minimal` does not expose. -/ + witness : EmptyInferenceWitness + /-- The generic graft: each boundary read's empty flip witness extends to a + full-compatible database. Supplies both end ranges of the complete branch. The + `Readable` guard is necessary: a bridge database exists only when the reported keys + are the complete keys of some database, so an unrealizable pair makes the conclusion + false rather than merely unproved. -/ + joint : ∀ (r : RangeRead) (res : Result), Readable r res → BoundaryJointBases r res + /-- The observability gap split: agreement on the complete value fixes the bracket. + The last bridge factor not derivable from the boundary emptinesses. -/ + bracket : ∀ (r : RangeRead) (res : Result), BracketFactor r res + /-- The truncated one-sided graft: the used boundary read's witness extends across the + reported prefix (forward) or suffix (reverse), with the far boundary dropped. Guarded + by `Readable` for the same reason as `joint`. -/ + truncated : ∀ (r : RangeRead) (res : Result), Readable r res → GeneralObligation r res + +/-- **The empty branch, from a complete empty theory.** -/ +theorem EmptyTheoryComplete.emptyInference (H : EmptyTheoryComplete) : EmptyInference := + emptyInference_of_witness H.witness + +/-- **The complete-branch obligation, from a complete empty theory.** The graft and the +bracket gap split of the empty theory give `CompleteObligation` for every realizable read. -/ +theorem EmptyTheoryComplete.completeObligation (H : EmptyTheoryComplete) (r : RangeRead) + (res : Result) (hread : Readable r res) : CompleteObligation r res := + ⟨H.joint r res hread, H.bracket r res⟩ + +/-- **`BoundaryNecessity` for a complete read, from a complete empty theory.** This +recovers the legacy finished hypothesis from the witness-level graft, tying the two +forms together. -/ +theorem EmptyTheoryComplete.boundaryNecessity (H : EmptyTheoryComplete) {r : RangeRead} + {res : Result} (hread : Readable r res) (hmore : res.more = false) + (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· < ·)) (hlim : RangeRead.withinLimit r res.keys) + (hrev : r.reverse = false) : BoundaryNecessity r res := + boundaryNecessity_of_jointBases hrev hmore hL hpair hlim (H.joint r res hread) + +/-- **`BoundaryNecessity` for a complete read, from a complete empty theory, reverse.** -/ +theorem EmptyTheoryComplete.boundaryNecessity_rev (H : EmptyTheoryComplete) {r : RangeRead} + {res : Result} (hread : Readable r res) (hmore : res.more = false) + (hL : res.keys ≠ []) + (hpair : res.keys.Pairwise (· > ·)) (hlim : RangeRead.withinLimit r res.keys) + (hrev : r.reverse = true) : BoundaryNecessity r res := + boundaryNecessity_of_jointBases_rev hrev hmore hL hpair hlim (H.joint r res hread) + +/-- **The general algorithm is correct from a complete empty theory.** The only +hypothesis beyond the empty theory is realizability; there is no offset, no +`EmptyInference` as a separate assumption, no `BoundaryNecessity`, and no bracket factor. +The general case is the offset-free assembly, with all offset handling confined to the +empty theory. -/ +theorem conflictRangeVal_spec_complete (H : EmptyTheoryComplete) {r : RangeRead} + {res : Result} (hread : Readable r res) : + Minimal r res (conflictRangeVal r res) := + conflictRangeVal_spec_of_witness H.witness (H.truncated r res hread) + (H.completeObligation r res hread) hread + +/-- **The existence conjecture from a complete empty theory.** -/ +theorem exists_minimal_conflict_range_of_complete (H : EmptyTheoryComplete) {r : RangeRead} + {res : Result} (hread : Readable r res) : + ∃ cr : ConflictRange, Minimal r res cr := + ⟨conflictRangeVal r res, conflictRangeVal_spec_complete H hread⟩ + + + +/-! +### Positive-offset empty witnesses + +The cross-free and crossed empty cases at all positive begin offsets are proved in +`FormalConflictRange.Empty`; these are the witness-level companions, exposing the +flip pairs those necessity proofs build. +-/ + +/-- **The cross-free empty witness at or above `B`, all positive begin offsets.** The +witness-level companion of `necessary_of_begin_le_of_le_general`. -/ +theorem emptyWitness_beginLe_of_le_general {r : RangeRead} + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) (hne : ¬ AlwaysEmpty r) + (hle : r.begin.key ≤ r.end_.key) {k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) : + EmptyWitness r k := by + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty hbegin hne + obtain ⟨b₀, hb₀⟩ : ∃ b₀ : Key, r.begin.key = (↑b₀ : WithTop Key) := by + cases h : r.begin.key with + | top => exact absurd h hBne + | coe b₀ => exact ⟨b₀, rfl⟩ + have hbk : b₀ ≤ k := by rw [hb₀] at hk; exact WithTop.coe_le_coe.mp hk + have hkb : ¬ (k : WithTop Key) < r.begin.key := by + rw [hb₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hbk) + set b : ℕ := (r.begin.offset - 1).toNat with hbdef + set e : ℕ := (r.end_.offset - 1).toNat with hedef + have he1 : 1 ≤ e := by omega + by_cases hEtop : r.end_.key = ⊤ + · obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid b₀ b k + let db₂ : DB := ⟨dbJ.keys, dbJ.sorted⟩ + have hkeys₂ : db₂.keys = dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by rw [hkeys₂]; exact hkJ + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx)] + have hcntE₂ : cnt db₂ r.end_.key = b := by + rw [hEtop, cnt_top, hkeys₂, hlenJ] + have hlen₂ : db₂.keys.length = b := hlenJ + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega))) + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [DB.insert_keys, List.orderedInsert_length, hlen₂, hcntB₁] at hh; omega + · rw [hEtop, cnt_top, DB.insert_keys, List.orderedInsert_length, hlen₂, hcntB₁] at hh + omega + refine ⟨db₂, hcomp₂, ?_⟩ + rw [toggle, dite_eq_right hk₂]; exact hnot₁ + · obtain ⟨e₀, he₀⟩ : ∃ e₀ : Key, r.end_.key = (↑e₀ : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h hEtop + | coe e₀ => exact ⟨e₀, rfl⟩ + have hbe₀ : b₀ ≤ e₀ := by + have hh := hle; rw [hb₀, he₀] at hh; exact WithTop.coe_le_coe.mp hh + rcases lt_or_ge b e with hlt_be | hge_be + · obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ b k + let db₂ : DB := ⟨dbJ.keys, dbJ.sorted⟩ + have hkeys₂ : db₂.keys = dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by rw [hkeys₂]; exact hkJ + have hlen₂ : db₂.keys.length = b := by rw [hkeys₂, hlenJ] + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge] + intro x hx + exact le_trans hbe₀ (hdbJ x hx) + have hcntE₂ : cnt db₂ r.end_.key = 0 := by + rw [he₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx)] + have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega))) + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [hlen₁, hcntB₁] at hh; omega + · rw [hcntB₁] at hh + by_cases hkE : r.end_.key ≤ (k : WithTop Key) + · rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) (by + rw [he₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr + (by rw [he₀] at hkE; exact WithTop.coe_le_coe.mp hkE))), hcntE₂] at hh + omega + · have hke₀ : k < e₀ := by + rw [he₀] at hkE; exact WithTop.coe_lt_coe.mp (lt_of_not_ge hkE) + rw [cnt_insert_lt db₂ k hk₂ (v := r.end_.key) (by + rw [he₀]; exact WithTop.coe_lt_coe.mpr hke₀), hcntE₂] at hh + omega + refine ⟨db₂, hcomp₂, ?_⟩ + rw [toggle, dite_eq_right hk₂]; exact hnot₁ + · have hed : e ≤ b := hge_be + have hroom := tsub_add_one_le_intervalCard_of_le_general hbegin hoE hle hne hed + by_cases hkE : r.end_.key ≤ (k : WithTop Key) + · have hke₀ : e₀ ≤ k := by rw [he₀] at hkE; exact WithTop.coe_le_coe.mp hkE + have hkEnlt : ¬ (k : WithTop Key) < r.end_.key := by + rw [he₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hke₀) + obtain ⟨dbA, hdbA, hlenA⟩ := + exists_db_keysIn (B := r.begin.key) (E := r.end_.key) (m := b - e + 1) + (by simpa only [← hbdef, ← hedef] using hroom) + have hAhi : ∀ x ∈ dbA.keys, x < e₀ := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).2 + have hAlo : ∀ x ∈ dbA.keys, b₀ ≤ x := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).1 + have hAk : k ∉ dbA.keys := fun h => absurd (hAhi k h) (not_lt.mpr hke₀) + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ (e - 1) k + have hcrossJ : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y := + fun x hx y hy => lt_of_lt_of_le (hAhi x hx) (hdbJ y hy) + let db₂ : DB := ⟨dbA.keys ++ dbJ.keys, + List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcrossJ⟩⟩ + have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by + rw [hkeys₂, List.mem_append]; rintro (h | h); exacts [hAk h, hkJ h] + have hlen₂ : db₂.keys.length = b := by + rw [hkeys₂, List.length_append, hlenA, hlenJ]; simp only [hbdef, hedef]; omega + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact hAlo x hx + · exact le_trans hbe₀ (hdbJ x hx) + have hcntE₂ : cnt db₂ r.end_.key = b - e + 1 := by + rw [he₀, cnt_coe, hkeys₂, List.countP_append, + countP_lt_eq_length_of_forall_lt hAhi, hlenA, + countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx), Nat.add_zero] + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = b - e + 1 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) hkEnlt, hcntE₂] + have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega))) + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [hlen₁, hcntB₁] at hh; omega + · rw [hcntE₁, hcntB₁, hbdef, hedef] at hh; omega + refine ⟨db₂, hcomp₂, ?_⟩ + rw [toggle, dite_eq_right hk₂]; exact hnot₁ + · have hke₀ : k < e₀ := by + rw [he₀] at hkE; exact WithTop.coe_lt_coe.mp (lt_of_not_ge hkE) + have hkElt : (k : WithTop Key) < r.end_.key := by + rw [he₀]; exact WithTop.coe_lt_coe.mpr hke₀ + obtain ⟨dbA, hdbA, hkA, hlenA⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := k) (m := b - e) + (by simpa only [Nat.cast_add, Nat.cast_one, hbdef, hedef] using hroom) + have hAhi : ∀ x ∈ dbA.keys, x < e₀ := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).2 + have hAlo : ∀ x ∈ dbA.keys, b₀ ≤ x := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).1 + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ e k + have hcrossJ : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y := + fun x hx y hy => lt_of_lt_of_le (hAhi x hx) (hdbJ y hy) + let db₂ : DB := ⟨dbA.keys ++ dbJ.keys, + List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcrossJ⟩⟩ + have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by + rw [hkeys₂, List.mem_append]; rintro (h | h); exacts [hkA h, hkJ h] + have hlen₂ : db₂.keys.length = b := by + rw [hkeys₂, List.length_append, hlenA, hlenJ]; simp only [hbdef, hedef]; omega + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact hAlo x hx + · exact le_trans hbe₀ (hdbJ x hx) + have hcntE₂ : cnt db₂ r.end_.key = b - e := by + rw [he₀, cnt_coe, hkeys₂, List.countP_append, + countP_lt_eq_length_of_forall_lt hAhi, hlenA, + countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx), Nat.add_zero] + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = b - e + 1 := by + rw [cnt_insert_lt db₂ k hk₂ (v := r.end_.key) hkElt, hcntE₂] + have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inr (by rw [hcntE₂, hcntB₂, hbdef, hedef]; omega))) + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [hlen₁, hcntB₁] at hh; omega + · rw [hcntE₁, hcntB₁, hbdef, hedef] at hh; omega + refine ⟨db₂, hcomp₂, ?_⟩ + rw [toggle, dite_eq_right hk₂]; exact hnot₁ + +/-- **The crossed empty witness at or above `B`, all positive begin offsets.** The +witness-level companion of `necessary_of_begin_le_general`. -/ +theorem emptyWitness_beginLe_general {r : RangeRead} + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) (hne : ¬ AlwaysEmpty r) + (hEB : r.end_.key ≤ r.begin.key) {k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) : + EmptyWitness r k := by + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty hbegin hne + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by + cases hkb : r.begin.key with + | top => exact absurd hkb hBne + | coe b => exact ⟨b, rfl⟩ + have hbk : b ≤ k := by rw [hb] at hk; exact WithTop.coe_le_coe.mp hk + rw [hb] at hEB + have heb : ¬ ((r.end_.offset - 1).toNat ≤ (r.begin.offset - 1).toNat) := by + intro hle + apply hne + intro db + rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hbegin (by omega)] + exact Or.inr (by + have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := + cnt_le_cnt_of_le db (by rw [hb]; exact hEB) + omega) + obtain ⟨db₂, hdb₂, hk₂, hlen₂⟩ := + exists_db_ge_avoid b ((r.begin.offset - 1).toNat) k + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb, cnt_coe, List.countP_eq_zero] + intro x hx; simp only [decide_eq_true_eq] + exact not_lt.mpr (hdb₂ x hx) + have hcntE₂ : cnt db₂ r.end_.key = 0 := by + rw [cnt, List.countP_eq_zero] + intro x hx; simp only [decide_eq_true_eq] + exact not_lt.mpr (le_trans hEB (WithTop.coe_le_coe.mpr (hdb₂ x hx))) + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key), hcntB₂] + rw [hb]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hbk) + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key), hcntE₂] + exact not_lt.mpr (le_trans hEB (WithTop.coe_le_coe.mpr hbk)) + have hlen₁ : (db₂.insert k hk₂).keys.length = (r.begin.offset - 1).toNat + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega))) + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [hlen₁, hcntB₁] at hh; omega + · rw [hcntB₁, hcntE₁] at hh + have := Nat.lt_of_not_le heb; omega + refine ⟨db₂, hcomp₂, ?_⟩ + rw [toggle, dite_eq_right hk₂]; exact hnot₁ + +/-- **The crossed empty witness inside `[E, B)`, all positive begin offsets.** The +witness-level companion of `necessary_of_mem_interval_general`. -/ +theorem emptyWitness_mem_interval_general {r : RangeRead} + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) (hne : ¬ AlwaysEmpty r) + (hEB : r.end_.key ≤ r.begin.key) + (hgap : ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : EmptyWitness r k := by + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty hbegin hne + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by + cases hkb : r.begin.key with + | top => exact absurd hkb hBne + | coe b => exact ⟨b, rfl⟩ + have hkb : k < b := by rw [hb] at hkB; exact WithTop.coe_lt_coe.mp hkB + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (↑e : WithTop Key) := by + cases hke : r.end_.key with + | top => rw [hke] at hkE; exact absurd hkE (not_le.mpr (WithTop.coe_lt_top k)) + | coe e => exact ⟨e, rfl⟩ + have hke : e ≤ k := by rw [he] at hkE; exact WithTop.coe_le_coe.mp hkE + have heb : e ≤ b := le_trans hke (le_of_lt hkb) + have hEB' : r.end_.key ≤ r.begin.key := hEB + rw [hb] at hEB + obtain ⟨g, hg⟩ : ∃ g : ℕ, g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := + ⟨_, rfl⟩ + have hgpos : 1 ≤ g := by + have hlt : (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat := + Nat.lt_of_not_le (by + intro hle + apply hne + intro db + rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hbegin (by omega)] + exact Or.inr (by + have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := + cnt_le_cnt_of_le db hEB' + omega)) + rw [hg]; exact Nat.sub_pos_of_lt hlt + have hcardA : (((g - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + rw [Nat.sub_add_cancel hgpos, hg]; exact hgap + obtain ⟨dbA, hdbA, hkA, hlenA⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := g - 1) hcardA + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid b ((r.begin.offset - 1).toNat + 1) k + have hAe : ∀ x ∈ dbA.keys, e ≤ x := fun x hx => by + have h := (hdbA x hx).1; rw [he] at h; exact WithTop.coe_le_coe.mp h + have hAb : ∀ x ∈ dbA.keys, x < b := fun x hx => by + have h := (hdbA x hx).2; rw [hb] at h; exact WithTop.coe_lt_coe.mp h + have hcross : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y := + fun x hx y hy => lt_of_lt_of_le (hAb x hx) (hdbJ y hy) + let db₂ : DB := ⟨dbA.keys ++ dbJ.keys, + List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcross⟩⟩ + have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by + rw [hkeys₂, List.mem_append]; rintro (h | h); exacts [hkA h, hkJ h] + have hlen₂ : db₂.keys.length = g + (r.begin.offset - 1).toNat := by + rw [hkeys₂, List.length_append, hlenA, hlenJ] + have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg + omega + have hcntB₂ : cnt db₂ r.begin.key = g - 1 := by + rw [hb, cnt_coe, hkeys₂, List.countP_append] + rw [countP_lt_eq_length_of_forall_lt hAb, hlenA, + countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx), Nat.add_zero] + have hcntE₂ : cnt db₂ r.end_.key = 0 := by + rw [he, cnt_coe, hkeys₂, List.countP_append] + rw [countP_lt_eq_zero_of_forall_ge (l := dbA.keys) hAe, + countP_lt_eq_zero_of_forall_ge (l := dbJ.keys) + (fun x hx => le_trans heb (hdbJ x hx)), Nat.add_zero] + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = g := by + rw [cnt_insert_lt db₂ k hk₂ (v := r.begin.key) (by rw [hb]; exact WithTop.coe_lt_coe.mpr hkb), + hcntB₂] + omega + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = 0 := by + have hke' : ¬ (k : WithTop Key) < r.end_.key := by + rw [he]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hke) + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) hke', hcntE₂] + have hcomp₁ : RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)).mpr + (Or.inr (by + rw [hcntE₁, hcntB₁] + have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg + omega))) + have hnot₂ : ¬ RangeRead.Compatible db₂ r { keys := [], more := false } := by + intro hc + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)] at hc + have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg + rcases hc with hc | hc + · rw [hlen₂, hcntB₂] at hc; omega + · rw [hcntE₂, hcntB₂] at hc; omega + refine ⟨db₂.insert k hk₂, hcomp₁, ?_⟩ + have hkeys : (toggle (db₂.insert k hk₂) k).keys = db₂.keys := by + rw [toggle, dite_eq_left (DB.mem_insert_self _ _ _)] + exact DB.erase_insert_keys _ k hk₂ + intro hc + exact hnot₂ ((RangeRead.compatible_congr + (RangeRead.completeKeys_congr hkeys r)).mp hc) + +/-! +### The empty witness oracle for positive begin offsets, overshooting end + +With `begin.offset ≥ 1` and `end.offset ≥ 2`, the empty value is the top range +(cross-free) or the crossed candidate; the witnesses extracted above supply every key, +so `EmptyInferenceWitnessOffOne` holds throughout this region. +-/ + +/-- **The empty witness oracle, `begin.offset ≥ 1`, `end.offset ≥ 2`, non-reversed.** +The two candidate branches of `emptyValue` (`topRange` and `crossedRangeGeneral`) are +covered by `emptyWitness_beginLe_of_le_general`, `emptyWitness_beginLe_general`, and +`emptyWitness_mem_interval_general`. -/ +theorem emptyWitness_overshoot_fwd (r : RangeRead) (hbegin : 1 ≤ r.begin.offset) + (hend : 2 ≤ r.end_.offset) (_hrev : r.reverse = false) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + by_cases hle : r.begin.key ≤ r.end_.key + · have hv : emptyValue r = topRange r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), + ite_eq_right (by omega), ite_eq_left hle] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_topRange_of_le_general (by omega) (by omega) hle rfl rfl + · intro k hk + rw [hv] at hk + exact emptyWitness_beginLe_of_le_general hbegin hend hAE hle hk.1 + · have hcross : r.end_.key < r.begin.key := not_le.mp hle + have hEB : r.end_.key ≤ r.begin.key := le_of_lt hcross + have hv : emptyValue r = crossedRangeGeneral r := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right (by omega : ¬ r.begin.offset ≤ 0), + ite_eq_right (by omega), ite_eq_right (not_le.mpr hcross)] + by_cases hgap : ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat) ≤ + intervalCard r.end_.key r.begin.key + · rw [crossedRangeGeneral, ite_eq_left hgap] at hv + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_top_end_of_crossed_general (by omega) (by omega) hEB rfl rfl + · intro k hk + rw [hv] at hk + rcases lt_or_ge (k : WithTop Key) r.begin.key with hkB | hkB + · exact emptyWitness_mem_interval_general hbegin hend hAE hEB + (by simpa using hgap) hk.1 hkB + · exact emptyWitness_beginLe_general hbegin hend hAE hEB hkB + · rw [crossedRangeGeneral, ite_eq_right hgap] at hv + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_top_begin_of_short_general (by omega) (by omega) hEB + (not_le.mp hgap) rfl rfl + · intro k hk + rw [hv] at hk + exact emptyWitness_beginLe_general hbegin hend hAE hEB hk.1 + +/-- **The empty witness oracle, `begin.offset ≥ 1`, `end.offset ≥ 2`, any traversal.** +Reversed is reduced to non-reversed by traversal invariance. -/ +theorem emptyWitness_overshoot (r : RangeRead) (hbegin : 1 ≤ r.begin.offset) + (hend : 2 ≤ r.end_.offset) + (hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + cases hrev : r.reverse + · exact emptyWitness_overshoot_fwd r hbegin hend hrev hread + · have hread' : Readable { r with reverse := false } { keys := [], more := false } := by + obtain ⟨db, hc⟩ := hread + refine ⟨db, ?_⟩ + have := (compatible_empty_flip (db := db) (r := r)).mp hc + rwa [show ({ r with reverse := !r.reverse } : RangeRead) = { r with reverse := false } + from by simp [hrev]] at this + have hf := emptyWitness_overshoot_fwd { r with reverse := false } hbegin hend rfl hread' + rw [show ({ r with reverse := false } : RangeRead) = + { r with reverse := !r.reverse } from by simp [hrev]] at hf + refine ⟨?_, ?_⟩ + · simpa [emptyValue_reverse] using (sound_empty_flip (r := r)).mpr hf.1 + · intro k hk + rw [← emptyValue_reverse] at hk + exact emptyWitness_flip.mp (hf.2 k hk) + +/-- **The empty witness oracle, nonpositive begin and overshooting end, any selector +order.** With `begin.offset ≤ 0` and `end.offset ≥ 2`, `emptyValue` is the full range +`[⊥, ⊤)` (unless the read is trivially empty). Soundness is +`sound_fullRange_of_nonpos_overshoot_all`; the flip witness at any key is the singleton +`{k}`, incompatible for every `k` by `not_compatible_singleton_overshoot_all`. This is the +crossed half of weaselbot's item 2, now discharged: `emptyValue` no longer returns the +unsound crossed bottom range here. -/ +theorem emptyWitness_nonpos_overshoot (r : RangeRead) (hbegin : r.begin.offset ≤ 0) + (hend : 2 ≤ r.end_.offset) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hv : emptyValue r = ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_left hbegin, ite_eq_left hend] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_fullRange_of_nonpos_overshoot_all rfl rfl + · intro k _hk + exact ⟨emptyDB, compatible_emptyDB_emptyRes r, by + have htoggle : toggle emptyDB k = singleton k := by + unfold toggle + rw [dite_eq_right (by simp [emptyDB])] + rfl + rw [htoggle] + exact not_compatible_singleton_overshoot_all hbegin hend⟩ + +/-- **The empty witness oracle, nonpositive begin and `end.offset = 1`, cross-free.** +With `begin.offset ≤ 0`, `end.offset = 1` and `begin.key ≤ end.key`, `emptyValue` is the +bottom range `[⊥, end.key)`, and every key below `end.key` has the singleton flip witness: +the empty database is compatible (`compatible_emptyDB_emptyRes`) and toggling `k` gives +`{k}`, whose complete keys are nonempty (`not_compatible_singleton_self`). This is the +witness-exposing companion of `minimal_bottomRange_of_nonpos_end_one`, and it is the part +of issue #19's item 1 that the single-key witness still separates. -/ +theorem emptyWitness_nonpos_end_one_crossFree (r : RangeRead) (hbegin : r.begin.offset ≤ 0) + (hend : r.end_.offset = 1) (hle : r.begin.key ≤ r.end_.key) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hv : emptyValue r = ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_left hbegin, ite_eq_right (by omega), ite_eq_left hle] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_bottomRange_of_nonpos_underEnd_le hbegin (le_of_eq hend) hle rfl rfl + · intro k hk + rw [hv] at hk + have htoggle : toggle emptyDB k = singleton k := by + unfold toggle + rw [dite_eq_right (by simp [emptyDB])] + rfl + exact ⟨emptyDB, compatible_emptyDB_emptyRes r, by + rw [htoggle]; exact not_compatible_singleton_self hbegin hend hk.2⟩ + +/-- **The empty witness oracle, nonpositive begin, `end.offset = 1`, crossed short gap.** +With `begin.offset ≤ 0`, `end.offset = 1`, crossed selectors (`end.key < begin.key`) and a +short gap, `emptyValue` is the bottom range `[⊥, end.key)`, and every key below `end.key` +has the singleton flip witness. Soundness is +`sound_bottomRange_of_nonpos_end_one_short`; necessity is the same +`necessary_of_nonpos_end_one` as the cross-free case. This closes weaselbot's second +counterexample (the always-clamping begin anchor) to the extent the short gap reaches. -/ +theorem emptyWitness_nonpos_end_one_crossedShort (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hcross : r.end_.key < r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hg : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) := by + rw [hend]; exact hgap + have hv : emptyValue r = ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_left hbegin, ite_eq_right (by omega : ¬ 2 ≤ r.end_.offset), + ite_eq_right (not_le.mpr hcross), ite_eq_left hg] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_bottomRange_of_nonpos_end_one_short hbegin hend (le_of_lt hcross) hgap rfl rfl + · intro k hk + rw [hv] at hk + have htoggle : toggle emptyDB k = singleton k := by + unfold toggle + rw [dite_eq_right (by simp [emptyDB])] + rfl + exact ⟨emptyDB, compatible_emptyDB_emptyRes r, by + rw [htoggle]; exact not_compatible_singleton_self hbegin hend hk.2⟩ + +theorem compat_regionP {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hBE : r.begin.key ≤ r.end_.key) + (hsmall : (1 - r.end_.offset).toNat < (1 - r.begin.offset).toNat ∨ + intervalCard ((([] : Key)) : WithTop Key) r.begin.key < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) : + (RangeRead.Compatible db r { keys := [], more := false } ↔ + cnt db r.end_.key ≤ (1 - r.end_.offset).toNat) := by + rw [compatible_empty_iff_nonpos_counts (db := db) hbegin hend] + set b : ℕ := (1 - r.begin.offset).toNat with hb + set e : ℕ := (1 - r.end_.offset).toNat with he + have hmono : cnt db r.begin.key ≤ cnt db r.end_.key := cnt_le_cnt_of_le db hBE + have hsmall' : e < b ∨ (cnt db r.begin.key : ℤ) < b := by + rcases hsmall with h | h + · exact Or.inl h + · refine Or.inr ?_ + have h1 := cnt_le_intervalCard_nil db r.begin.key + have h2 : (cnt db r.begin.key : ℕ∞) < (b : ℕ∞) := lt_of_le_of_lt h1 h + exact_mod_cast h2 + constructor + · intro h + rcases hsmall' with hs | hs + · by_cases hcb : (b : ℤ) ≤ (cnt db r.begin.key : ℤ) + · rw [max_eq_left (by omega)] at h + have : (cnt db r.begin.key : ℤ) ≤ cnt db r.end_.key := by exact_mod_cast hmono + omega + · rw [max_eq_right (by omega)] at h; omega + · rw [max_eq_right (by omega)] at h; omega + · intro h + have : (cnt db r.end_.key : ℤ) - e ≤ 0 := by omega + exact le_trans this (le_max_right _ _) + +theorem compat_of_cnt {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + {cB cE : ℕ} (hcB : cnt db r.begin.key = cB) (hcE : cnt db r.end_.key = cE) : + (RangeRead.Compatible db r { keys := [], more := false } ↔ + (cE : ℤ) - ((1 - r.end_.offset).toNat : ℤ) ≤ + max ((cB : ℤ) - ((1 - r.begin.offset).toNat : ℤ)) 0) := by + rw [compatible_empty_iff_nonpos_counts (db := db) hbegin hend, hcB, hcE] + +theorem wI_gap {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hBE : r.begin.key ≤ r.end_.key) + (hne : ¬ AlwaysEmpty r) (hbe : (1 - r.begin.offset).toNat ≤ (1 - r.end_.offset).toNat) + (hCB : (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard ((([] : Key)) : WithTop Key) r.begin.key) + {k : Key} (hkB : r.begin.key ≤ (k : WithTop Key)) (hkE : (k : WithTop Key) < r.end_.key) : + EmptyWitness r k := by + set b : ℕ := (1 - r.begin.offset).toNat with hb + set e : ℕ := (1 - r.end_.offset).toNat with he + have hgap := tsub_add_one_le_intervalCard_of_not_alwaysEmpty_nonpos (r := r) hbegin hend + (by rw [← hb, ← he]; exact hbe) hne + obtain ⟨db, hk, hcB, hcE⟩ := exists_db_below_gap_avoid_gap hBE hkB hkE + (clo := b) (chi := e) (by rw [hb, he]; exact hbe) (by rw [hb]; exact hCB) + (by rw [he, hb]; exact hgap) + have hkBn : ¬ (k : WithTop Key) < r.begin.key := not_lt.mpr hkB + have htoggle : toggle db k = db.insert k hk := by + unfold toggle; rw [dite_eq_right hk] + have hbase : RangeRead.Compatible db r { keys := [], more := false } := by + rw [compat_of_cnt (r := r) hbegin hend hcB hcE] + rw [show ((1 - r.end_.offset).toNat : ℤ) = e from by rw [he], + show ((1 - r.begin.offset).toNat : ℤ) = b from by rw [hb]] + have : (e : ℤ) - e ≤ max ((b : ℤ) - b) 0 := by ring_nf; simp + exact this + refine ⟨db, hbase, ?_⟩ + rw [htoggle] + have hcB' : cnt (db.insert k hk) r.begin.key = b := by + rw [cnt_insert_not_lt db k hk hkBn, hcB] + have hcE' : cnt (db.insert k hk) r.end_.key = e + 1 := by + rw [cnt_insert_lt db k hk hkE, hcE] + intro hcomp + rw [compat_of_cnt (r := r) hbegin hend hcB' hcE'] at hcomp + rw [show ((1 - r.end_.offset).toNat : ℤ) = e from by rw [he], + show ((1 - r.begin.offset).toNat : ℤ) = b from by rw [hb]] at hcomp + simp only [sub_self, max_eq_right le_rfl] at hcomp; omega + +theorem wI_below {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hBE : r.begin.key ≤ r.end_.key) + (hne : ¬ AlwaysEmpty r) (hbe : (1 - r.begin.offset).toNat ≤ (1 - r.end_.offset).toNat) + (hCB : (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard ((([] : Key)) : WithTop Key) r.begin.key) + {k : Key} (hkB : (k : WithTop Key) < r.begin.key) : + EmptyWitness r k := by + set b : ℕ := (1 - r.begin.offset).toNat with hb + set e : ℕ := (1 - r.end_.offset).toNat with he + have hbe' : b ≤ e := hbe + have hgap := tsub_add_one_le_intervalCard_of_not_alwaysEmpty_nonpos (r := r) hbegin hend + hbe' hne + have hgap' : (((e - (b - 1) : ℕ)) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + have : e - (b - 1) = e - b + 1 := by omega + rw [this]; exact hgap + have hlo' : (((b - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) r.begin.key := by + have : b - 1 + 1 = b := by omega + rw [this]; exact hCB + have hcl : b - 1 ≤ e := by omega + obtain ⟨db, hk, hcB, hcE⟩ := exists_db_below_gap_avoid_below hBE hkB + (clo := b - 1) (chi := e) hcl hlo' hgap' + have htoggle : toggle db k = db.insert k hk := by + unfold toggle; rw [dite_eq_right hk] + have hbase : RangeRead.Compatible db r { keys := [], more := false } := by + rw [compat_of_cnt (r := r) hbegin hend hcB hcE] + rw [show ((1 - r.end_.offset).toNat : ℤ) = e from by rw [he], + show ((1 - r.begin.offset).toNat : ℤ) = b from by rw [hb]] + have hbb : ((b - 1 : ℕ) : ℤ) - b = -1 := by omega + rw [hbb] + have : (e : ℤ) - e ≤ max (-1) 0 := by simp + exact this + refine ⟨db, hbase, ?_⟩ + rw [htoggle] + have hcB' : cnt (db.insert k hk) r.begin.key = b := by + rw [cnt_insert_lt db k hk hkB, hcB]; omega + have hcE' : cnt (db.insert k hk) r.end_.key = e + 1 := by + rw [cnt_insert_lt db k hk (lt_of_lt_of_le hkB hBE), hcE] + intro hcomp + rw [compat_of_cnt (r := r) hbegin hend hcB' hcE'] at hcomp + rw [show ((1 - r.end_.offset).toNat : ℤ) = e from by rw [he], + show ((1 - r.begin.offset).toNat : ℤ) = b from by rw [hb]] at hcomp + simp only [sub_self, max_eq_right le_rfl] at hcomp; omega + +/-- Region P witness. -/ +theorem wP {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hBE : r.begin.key ≤ r.end_.key) + (hne : ¬ AlwaysEmpty r) + (hsmall : (1 - r.end_.offset).toNat < (1 - r.begin.offset).toNat ∨ + intervalCard ((([] : Key)) : WithTop Key) r.begin.key < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) + {k : Key} (hkE : (k : WithTop Key) < r.end_.key) : + EmptyWitness r k := by + set e : ℕ := (1 - r.end_.offset).toNat with he + have hroom := not_alwaysEmpty_intervalCard_bot (r := r) hbegin hend hne + have hroom' : (((e + 1 : ℕ)) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) r.end_.key := by + rw [← he] at hroom; exact hroom + obtain ⟨db, hdb, hk, hlen⟩ := + exists_db_keysIn_avoid (B := ((([] : Key)) : WithTop Key)) (E := r.end_.key) (k := k) + (m := e) hroom' + have hcE : cnt db r.end_.key = e := by + rw [cnt_eq_length_of_forall_lt db (fun x hx => (hdb x hx).2), hlen] + have htoggle : toggle db k = db.insert k hk := by + unfold toggle; rw [dite_eq_right hk] + have hbase : RangeRead.Compatible db r { keys := [], more := false } := by + rw [compat_regionP (r := r) hbegin hend hBE hsmall] + rw [hcE] + refine ⟨db, hbase, ?_⟩ + rw [htoggle] + have hcE' : cnt (db.insert k hk) r.end_.key = e + 1 := by + rw [cnt_insert_lt db k hk hkE, hcE] + intro hcomp + rw [compat_regionP (r := r) hbegin hend hBE hsmall, hcE'] at hcomp + omega + +/-- **The crossed long-gap empty witness, `end.key ≤ k < begin.key`.** The flip pair is +the coupled-count construction: `db₀` stores one key below `end.key` together with +`1 - begin.offset - 1` keys of `[end.key, begin.key)` avoiding `k`, and finally `k`. Its +counts are `cnt end.key = 1` and `cnt begin.key = 1 - begin.offset`, so the empty result +is compatible on the first disjunct; erasing `k` drops the begin count by one, leaving +`1 - begin.offset - 1` in the gap and `cnt end.key = 1`, so both disjuncts fail. -/ +theorem emptyWitness_nonpos_end_one_crossedLong_interval (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hcross : r.end_.key < r.begin.key) + (hgap : (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + (hne : ¬ AlwaysEmpty r) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + EmptyWitness r k := by + set N : ℕ := (1 - r.begin.offset).toNat with hN + have hNpos : 1 ≤ N := by omega + have hEbot : r.end_.key ≠ ((([] : Key)) : WithTop Key) := + fun hbot => hne (alwaysEmpty_of_end_key_eq_bot hbegin hend (le_of_lt hcross) hbot) + have hpE : ((([] : Key)) : WithTop Key) < r.end_.key := by + have hle : ((([] : Key)) : WithTop Key) ≤ r.end_.key := by + cases r.end_.key with + | top => exact le_top + | coe e => exact WithTop.coe_le_coe.mpr (nil_le_key e) + exact lt_of_le_of_ne hle (Ne.symm hEbot) + have hcard : (((N - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + rw [Nat.sub_add_cancel hNpos]; rw [hN]; exact hgap + obtain ⟨dbA, hdbA, hkA, hlenA⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := N - 1) hcard + have hdbAge : ∀ x ∈ dbA.keys, r.end_.key ≤ (x : WithTop Key) := by + intro x hx + have h := hdbA x hx + exact h.1 + have hdbAlt : ∀ x ∈ dbA.keys, (x : WithTop Key) < r.begin.key := by + intro x hx + have h := hdbA x hx + exact h.2 + have hcntA_E : cnt dbA r.end_.key = 0 := + cnt_eq_zero_of_forall_ge dbA hdbAge + have hcntA_B : cnt dbA r.begin.key = N - 1 := by + rw [cnt_eq_length_of_forall_lt dbA hdbAlt, hlenA] + have hp_notin : ([] : Key) ∉ dbA.keys := by + intro hp_mem + exact absurd hpE (not_lt.mpr (hdbAge ([] : Key) hp_mem)) + have hk_notin_insert : k ∉ (dbA.insert ([] : Key) hp_notin).keys := by + rw [DB.mem_insert_iff hp_notin] + rintro (rfl | hk_A) + · exact absurd hpE (not_lt.mpr hkE) + · exact hkA hk_A + let db₀ : DB := (dbA.insert ([] : Key) hp_notin).insert k hk_notin_insert + have hcntE₁ : cnt (dbA.insert ([] : Key) hp_notin) r.end_.key = 1 := by + rw [cnt_insert_lt dbA ([] : Key) hp_notin hpE, hcntA_E, Nat.zero_add] + have hcntB₁ : cnt (dbA.insert ([] : Key) hp_notin) r.begin.key = N := by + rw [cnt_insert_lt dbA ([] : Key) hp_notin (lt_of_lt_of_le hpE (le_of_lt hcross)), + hcntA_B, Nat.sub_add_cancel hNpos] + have hcntE₀ : cnt db₀ r.end_.key = 1 := by + dsimp only [db₀] + rw [cnt_insert_not_lt _ k hk_notin_insert (v := r.end_.key) (not_lt.mpr hkE), hcntE₁] + have hcntB₀ : cnt db₀ r.begin.key = N + 1 := by + dsimp only [db₀] + rw [cnt_insert_lt _ k hk_notin_insert hkB, hcntB₁] + have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := + (compatible_empty_iff_nonpos_end_one_crossed' (db := db₀) hbegin hend + (le_of_lt hcross)).mpr (Or.inl (by rw [hcntB₀, hcntE₀]; omega)) + refine ⟨db₀, hcomp₀, ?_⟩ + have htoggle : toggle db₀ k = db₀.erase k := by + unfold toggle + rw [dite_eq_left (DB.mem_insert_self _ _ _)] + rw [htoggle] + intro hc + have hmemk : k ∈ db₀.keys := DB.mem_insert_self _ _ _ + have hcntE₂ : cnt (db₀.erase k) r.end_.key = 1 := by + rw [cnt_erase_not_lt db₀ hmemk (not_lt.mpr hkE), hcntE₀] + have hcntB₂ : cnt (db₀.erase k) r.begin.key = N := by + rw [cnt_erase_lt db₀ hmemk hkB, hcntB₀] + omega + rw [compatible_empty_iff_nonpos_end_one_crossed' (db := db₀.erase k) hbegin hend + (le_of_lt hcross)] at hc + rcases hc with hc | hc + · rw [hcntB₂, hcntE₂] at hc; omega + · rw [hcntE₂] at hc; omega + +/-- **The crossed long-gap empty witness, `k < end.key`.** Here the flip pair is even +simpler: `db₀` stores `1 - begin.offset - 1` keys of `[end.key, begin.key)` and no key +below `end.key`, so its begin count is `1 - begin.offset - 1` and its end count is `0`. +The empty result is compatible on the second disjunct (`cnt end.key = 0`); inserting `k` +below `end.key` lifts `cnt end.key` to `1` and `cnt begin.key` to `1 - begin.offset`, +leaving the first disjunct too short, so the result becomes incompatible. -/ +theorem emptyWitness_nonpos_end_one_crossedLong_below (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hcross : r.end_.key < r.begin.key) + (hgap : (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + {k : Key} (hkE : (k : WithTop Key) < r.end_.key) : + EmptyWitness r k := by + set N : ℕ := (1 - r.begin.offset).toNat with hN + have hNpos : 1 ≤ N := by omega + have hcard : (((N - 1 : ℕ)) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := + le_trans (by exact_mod_cast Nat.sub_le N 1) hgap + obtain ⟨dbA, hdbA, hlenA⟩ := + exists_db_keysIn (B := r.end_.key) (E := r.begin.key) (m := N - 1) hcard + have hdbAge : ∀ x ∈ dbA.keys, r.end_.key ≤ (x : WithTop Key) := by + intro x hx + have h := hdbA x hx + exact h.1 + have hdbAlt : ∀ x ∈ dbA.keys, (x : WithTop Key) < r.begin.key := by + intro x hx + have h := hdbA x hx + exact h.2 + have hcntA_E : cnt dbA r.end_.key = 0 := + cnt_eq_zero_of_forall_ge dbA hdbAge + have hcntA_B : cnt dbA r.begin.key = N - 1 := by + rw [cnt_eq_length_of_forall_lt dbA hdbAlt, hlenA] + have hk_notin : k ∉ dbA.keys := by + intro hk + exact absurd hkE (not_lt.mpr (hdbAge k hk)) + refine ⟨dbA, ?_, ?_⟩ + · rw [compatible_empty_iff_nonpos_end_one_crossed' (db := dbA) hbegin hend + (le_of_lt hcross)] + exact Or.inr hcntA_E + · have htoggle : toggle dbA k = dbA.insert k hk_notin := by + unfold toggle + rw [dite_eq_right hk_notin] + rw [htoggle] + intro hc + rw [compatible_empty_iff_nonpos_end_one_crossed' (db := dbA.insert k hk_notin) hbegin + hend (le_of_lt hcross)] at hc + have hcntE₁ : cnt (dbA.insert k hk_notin) r.end_.key = 1 := by + rw [cnt_insert_lt dbA k hk_notin hkE, hcntA_E, Nat.zero_add] + have hcntB₁ : cnt (dbA.insert k hk_notin) r.begin.key = N := by + rw [cnt_insert_lt dbA k hk_notin (lt_of_lt_of_le hkE (le_of_lt hcross)), hcntA_B, + Nat.sub_add_cancel hNpos] + rcases hc with hc | hc + · rw [hcntB₁, hcntE₁] at hc; omega + · rw [hcntE₁] at hc; omega + +/-- **The empty witness oracle, nonpositive begin, `end.offset = 1`, crossed long gap.** +With `begin.offset ≤ 0`, `end.offset = 1`, crossed selectors (`end.key < begin.key`) and a +gap long enough to lift the begin anchor (`1 - begin.offset ≤ intervalCard end.key +begin.key`), `emptyValue` is the bottom range `[⊥, begin.key)`. Soundness is +`sound_bottomRange_of_nonpos_end_one_crossed`; every key below `begin.key` has a flip +witness, split on whether it lies at or above `end.key` (the coupled two-key construction) +or below it (the empty-second-disjunct construction). This closes the crossed long gap, +the last `end.offset = 1` corner. -/ +theorem emptyWitness_nonpos_end_nonpos_crossFree (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hBE : r.begin.key ≤ r.end_.key) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hv : emptyValue r = ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_left hbegin, ite_eq_right (by omega : ¬ 2 ≤ r.end_.offset), + ite_eq_left hBE] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_bottomRange_of_nonpos_underEnd_le (r := r) hbegin (by omega) hBE rfl rfl + · intro k hk + rw [hv] at hk + -- region split + by_cases hreg : (1 - r.end_.offset).toNat < (1 - r.begin.offset).toNat ∨ + intervalCard ((([] : Key)) : WithTop Key) r.begin.key < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) + · exact wP (r := r) hbegin hend hBE hAE hreg hk.2 + · have hbe : (1 - r.begin.offset).toNat ≤ (1 - r.end_.offset).toNat := + le_of_not_gt (fun hh => hreg (Or.inl hh)) + have hCB : (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard ((([] : Key)) : WithTop Key) r.begin.key := + le_of_not_gt (fun hh => hreg (Or.inr hh)) + rcases lt_or_ge (k : WithTop Key) r.begin.key with hkB | hkB + · exact wI_below (r := r) hbegin hend hBE hAE hbe hCB hkB + · exact wI_gap (r := r) hbegin hend hBE hAE hbe hCB hkB hk.2 + +theorem emptyWitness_nonpos_end_one_crossedLong (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hcross : r.end_.key < r.begin.key) + (hgap : (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hg : ¬ intervalCard r.end_.key r.begin.key < + (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) := by + rw [hend]; exact not_lt.mpr hgap + have hv : emptyValue r = ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_left hbegin, ite_eq_right (by omega : ¬ 2 ≤ r.end_.offset), + ite_eq_right (not_le.mpr hcross), ite_eq_right hg, max_eq_left (le_of_lt hcross)] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_bottomRange_of_nonpos_end_one_crossed hbegin hend (le_of_lt hcross) rfl rfl + · intro k hk + rw [hv] at hk + rcases lt_or_ge (k : WithTop Key) r.end_.key with hkE | hkE + · exact emptyWitness_nonpos_end_one_crossedLong_below r hbegin hend hcross hgap hkE + · exact emptyWitness_nonpos_end_one_crossedLong_interval r hbegin hend hcross hgap hAE + hkE hk.2 + +/-- **The crossed short-gap room, key at or above `end.key`.** The short-gap criterion is +`cnt end.key ≤ e`, so no `AlwaysEmpty` read can have fewer than `e + 1` keys below +`end.key`: the `[⊥, end.key)` room the short-gap witness spends. -/ +theorem not_alwaysEmpty_intervalCard_bot_end {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hne : ¬ AlwaysEmpty r) : + (((1 - r.end_.offset).toNat + 1 : ℕ) : ℕ∞) ≤ + intervalCard ((([] : Key)) : WithTop Key) r.end_.key := by + have hnot : ¬ intervalCard ((([] : Key)) : WithTop Key) r.end_.key ≤ + (((1 - r.end_.offset).toNat : ℕ) : ℕ∞) := by + intro h + apply hne + intro db + rw [← compatible_empty_iff_complete_nil] + rw [compatible_empty_iff_nonpos_endle (db := db) hbegin (by omega)] + have hcE : cnt db r.end_.key ≤ (1 - r.end_.offset).toNat := by + have hle := cnt_le_intervalCard_nil db r.end_.key + exact Nat.cast_le.mp (le_trans hle h) + have : (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ 0 := by omega + exact le_trans this (le_max_right _ _) + rw [not_le] at hnot + rw [show (((1 - r.end_.offset).toNat + 1 : ℕ) : ℕ∞) = + (((1 - r.end_.offset).toNat : ℕ) : ℕ∞) + 1 by push_cast; ring] + exact ENat.add_one_le_iff (by simp) |>.mpr hnot + +/-- **The crossed nonpositive-end short-gap empty witness.** With `begin.offset ≤ 0`, +`end.offset ≤ 0`, crossed selectors and a short gap, `emptyValue` is `[⊥, end.key)` +(unless trivially empty). Soundness is `sound_bottomRange_of_nonpos_crossed_short`; +every key below `end.key` has the singleton flip witness: the empty database is +compatible, and toggling `k` gives the singleton `{k}`, incompatible by +`not_compatible_singleton_self`. -/ +theorem emptyWitness_nonpos_crossed_short (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key < r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞)) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hv : emptyValue r = ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_left hbegin, ite_eq_right (by omega : ¬ 2 ≤ r.end_.offset), + ite_eq_right (not_le.mpr hcross), ite_eq_left hgap] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_bottomRange_of_nonpos_crossed_short hbegin hend (le_of_lt hcross) hgap rfl rfl + · intro k hk + rw [hv] at hk + set e : ℕ := (1 - r.end_.offset).toNat with he + have hroom := not_alwaysEmpty_intervalCard_bot_end (r := r) hbegin hend hAE + obtain ⟨db, hdb, hknot, hlen⟩ := + exists_db_keysIn_avoid (B := ((([] : Key)) : WithTop Key)) (E := r.end_.key) + (k := k) (m := e) (by rw [← he] at hroom; exact hroom) + have hcE : cnt db r.end_.key = e := by + rw [cnt_eq_length_of_forall_lt db (fun x hx => (hdb x hx).2), hlen] + have htoggle : toggle db k = db.insert k hknot := by + unfold toggle; rw [dite_eq_right hknot] + refine ⟨db, ?_, ?_⟩ + · rw [compatible_empty_iff_nonpos_crossed_short (db := db) hbegin hend + (le_of_lt hcross) hgap, hcE] + · rw [htoggle] + have hcE' : cnt (db.insert k hknot) r.end_.key = e + 1 := by + rw [cnt_insert_lt db k hknot hk.2, hcE] + intro hcomp + rw [compatible_empty_iff_nonpos_crossed_short (db := db.insert k hknot) hbegin hend + (le_of_lt hcross) hgap, hcE'] at hcomp + omega + +/-- **The crossed nonpositive-end long-gap empty witness, `k < end.key`.** The flip pair +stores `1 - end.offset` keys below `end.key` avoiding `k`; the empty result is compatible +on the short disjunct `cnt end.key ≤ 1 - end.offset`, and inserting `k` lifts the end count +to `1 - end.offset + 1`, killing the short disjunct while the gap (unchanged) stays below +`1 - begin.offset - (1 - end.offset)`, which is positive off the trivially empty region. -/ +theorem emptyWitness_nonpos_crossed_long_below (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key ≤ r.begin.key) (hne : ¬ AlwaysEmpty r) + {k : Key} (hkE : (k : WithTop Key) < r.end_.key) : + EmptyWitness r k := by + set e : ℕ := (1 - r.end_.offset).toNat with he + set b : ℕ := (1 - r.begin.offset).toNat with hb + have hbgt : 1 ≤ b - e := by + have : e < b := by + by_contra hle + exact hne (alwaysEmpty_of_slack_le_slack hbegin hend hcross (le_of_not_gt hle)) + omega + have hroom := not_alwaysEmpty_intervalCard_bot_end (r := r) hbegin hend hne + obtain ⟨db, hdb, hknot, hlen⟩ := + exists_db_keysIn_avoid (B := ((([] : Key)) : WithTop Key)) (E := r.end_.key) + (k := k) (m := e) (by rw [← he] at hroom; exact hroom) + have hlt : ∀ x ∈ db.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hdb x hx).2 + have hcE : cnt db r.end_.key = e := by + rw [cnt_eq_length_of_forall_lt db hlt, hlen] + have hcB : cnt db r.begin.key = e := by + rw [cnt_eq_length_of_forall_lt db (fun x hx => lt_of_lt_of_le (hlt x hx) hcross), hlen] + refine ⟨db, ?_, ?_⟩ + · rw [compatible_empty_iff_nonpos_crossed_long (db := db) hbegin hend hcross] + exact Or.inl (by rw [hcE, he]) + · have htoggle : toggle db k = db.insert k hknot := by + unfold toggle; rw [dite_eq_right hknot] + rw [htoggle] + intro hcomp + rw [compatible_empty_iff_nonpos_crossed_long (db := db.insert k hknot) hbegin hend + hcross] at hcomp + have hcE' : cnt (db.insert k hknot) r.end_.key = e + 1 := by + rw [cnt_insert_lt db k hknot hkE, hcE] + have hcB' : cnt (db.insert k hknot) r.begin.key = e + 1 := by + rw [cnt_insert_lt db k hknot (lt_of_lt_of_le hkE hcross), hcB] + rcases hcomp with h | ⟨-, hg'⟩ + · rw [hcE', he] at h; omega + · have hlen2 : (between (db.insert k hknot) r.end_.key r.begin.key).length = + e + 1 - (e + 1) := by + rw [← cnt_sub_eq_between_length hcross, hcB', hcE'] + rw [hlen2, ← hb, ← he] at hg' + omega + +/-- **The crossed nonpositive-end long-gap empty witness, `end.key ≤ k < begin.key`.** +The flip pair is the coupled construction: `db₀` stores `1 - end.offset + 1` keys below +`end.key` and `1 - begin.offset - (1 - end.offset) - 1` keys of `[end.key, begin.key)` +avoiding `k`, and finally `k`. Its counts are `cnt begin.key = 1 - begin.offset + 1` and +`cnt end.key = 1 - end.offset + 1`, so the empty result is compatible on the long disjunct; +erasing `k` drops the begin count by one, leaving the gap one short of +`1 - begin.offset - (1 - end.offset)`, so both disjuncts fail. -/ +theorem emptyWitness_nonpos_crossed_long_interval (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key < r.begin.key) + (hgap : (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + (hne : ¬ AlwaysEmpty r) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) (hkB : (k : WithTop Key) < r.begin.key) : + EmptyWitness r k := by + set e : ℕ := (1 - r.end_.offset).toNat with he + set b : ℕ := (1 - r.begin.offset).toNat with hb + have hbgt : 1 ≤ b - e := by + have : e < b := by + by_contra hle + exact hne (alwaysEmpty_of_slack_le_slack hbegin hend (le_of_lt hcross) (le_of_not_gt hle)) + omega + have hgap' : ((b - e : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + have h := offset_sub_toNat r.begin.offset r.end_.offset hbegin hend + rw [← hb, ← he] at h + rw [← h]; exact hgap + have hroom := not_alwaysEmpty_intervalCard_bot_end (r := r) hbegin hend hne + have hroom' : (((e + 1 : ℕ)) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) r.end_.key := by + rw [← he] at hroom; exact hroom + obtain ⟨dbLo, hLo, hlenLo⟩ := + exists_db_keysIn (B := ((([] : Key)) : WithTop Key)) (E := r.end_.key) (m := e + 1) hroom' + obtain ⟨dbHi, hHi, hkHi, hlenHi⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) + (m := b - e - 1) (by + have hbeq : b - e = (b - e - 1) + 1 := by omega + rw [hbeq] at hgap' + rwa [Nat.cast_add, Nat.cast_one] at hgap') + have hkLo : k ∉ dbLo.keys := fun hk => absurd hkE (not_le.mpr (hLo k hk).2) + have hcross' : ∀ x ∈ dbLo.keys, ∀ y ∈ dbHi.keys, x < y := fun x hx y hy => + WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hLo x hx).2 (hHi y hy).1) + let dbPre : DB := ⟨dbLo.keys ++ dbHi.keys, + List.sortedLT_append.mpr ⟨dbLo.sorted, dbHi.sorted, hcross'⟩⟩ + have hkeys : dbPre.keys = dbLo.keys ++ dbHi.keys := rfl + have hkPre : k ∉ dbPre.keys := by + rw [hkeys, List.mem_append]; rintro (h | h) + exacts [hkLo h, hkHi h] + let db₀ : DB := dbPre.insert k hkPre + have hcntPre_E : cnt dbPre r.end_.key = e + 1 := by + have hsplit : cnt dbPre r.end_.key = cnt dbLo r.end_.key + cnt dbHi r.end_.key := by + unfold cnt; rw [hkeys, List.countP_append] + rw [hsplit, cnt_eq_length_of_forall_lt dbLo (fun x hx => (hLo x hx).2), + cnt_eq_zero_of_forall_ge dbHi (fun x hx => (hHi x hx).1), Nat.add_zero, hlenLo] + have hcntPre_B : cnt dbPre r.begin.key = e + 1 + (b - e - 1) := by + have hsplit : cnt dbPre r.begin.key = cnt dbLo r.begin.key + cnt dbHi r.begin.key := by + unfold cnt; rw [hkeys, List.countP_append] + rw [hsplit, + cnt_eq_length_of_forall_lt dbLo (fun x hx => lt_of_lt_of_le (hLo x hx).2 (le_of_lt hcross)), + cnt_eq_length_of_forall_lt dbHi (fun x hx => (hHi x hx).2), hlenLo, hlenHi] + have hcntE : cnt db₀ r.end_.key = e + 1 := by + dsimp only [db₀] + rw [cnt_insert_not_lt dbPre k hkPre (not_lt.mpr hkE), hcntPre_E] + have hcntB : cnt db₀ r.begin.key = b + 1 := by + dsimp only [db₀] + rw [cnt_insert_lt dbPre k hkPre hkB, hcntPre_B]; omega + have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := by + rw [compatible_empty_iff_nonpos_crossed_long (db := db₀) hbegin hend (le_of_lt hcross)] + refine Or.inr ⟨by rw [hcntB, hb]; exact Nat.le_succ b, ?_⟩ + have hlen : (between db₀ r.end_.key r.begin.key).length = + cnt db₀ r.begin.key - cnt db₀ r.end_.key := by + have := cnt_eq_add_between db₀ (le_of_lt hcross); omega + rw [hlen, hcntB, hcntE, hb, he]; omega + refine ⟨db₀, hcomp₀, ?_⟩ + have htoggle : toggle db₀ k = db₀.erase k := by + unfold toggle; rw [dite_eq_left (DB.mem_insert_self dbPre k hkPre)] + rw [htoggle] + intro hc + have hmemk : k ∈ db₀.keys := DB.mem_insert_self dbPre k hkPre + have hE2 : cnt (db₀.erase k) r.end_.key = e + 1 := by + rw [cnt_erase_not_lt db₀ hmemk (not_lt.mpr hkE), hcntE] + have hB2 : cnt (db₀.erase k) r.begin.key = b := by + rw [cnt_erase_lt db₀ hmemk hkB, hcntB]; omega + rw [compatible_empty_iff_nonpos_crossed_long (db := db₀.erase k) hbegin hend + (le_of_lt hcross)] at hc + rcases hc with hce | ⟨-, hgap2⟩ + · rw [hE2] at hce; omega + · have hlen2 : (between (db₀.erase k) r.end_.key r.begin.key).length = + b - (e + 1) := by + rw [← cnt_sub_eq_between_length (le_of_lt hcross), hB2, hE2] + rw [hlen2, hb, he] at hgap2 + omega + +/-- **The crossed nonpositive-end long-gap empty witness.** With `begin.offset ≤ 0`, +`end.offset ≤ 0`, crossed selectors (`end.key < begin.key`) and a long gap, `emptyValue` +is the bottom range `[⊥, begin.key)` (unless trivially empty). Soundness is +`sound_bottomRange_of_nonpos_end_one_crossed`; every key below `begin.key` has a flip +witness, split on whether it lies below `end.key` (`emptyWitness_nonpos_crossed_long_below`) +or in `[end.key, begin.key)` (`emptyWitness_nonpos_crossed_long_interval`). This is the +exact residual case `EmptyInferenceWitnessResidual`. -/ +theorem emptyWitness_nonpos_crossed_long (r : RangeRead) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key < r.begin.key) + (hgap : (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + (_hread : Readable r { keys := [], more := false }) : + Sound r { keys := [], more := false } (emptyValue r) ∧ + ∀ k : Key, k ∈ emptyValue r → EmptyWitness r k := by + by_cases hAE : AlwaysEmpty r + · have hv : emptyValue r = emptyRange r := by simp [emptyValue, hAE] + refine ⟨?_, ?_⟩ + · rw [hv]; exact sound_emptyRange_of_alwaysEmpty hAE + · intro k hk + rw [hv] at hk + exact absurd hk (ConflictRange.not_mem_of_isEmpty (by + simp [ConflictRange.IsEmpty, emptyRange]) k) + · have hg : ¬ intervalCard r.end_.key r.begin.key < + (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) := not_lt.mpr hgap + have hv : emptyValue r = ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_left hbegin, ite_eq_right (by omega : ¬ 2 ≤ r.end_.offset), + ite_eq_right (not_le.mpr hcross), ite_eq_right hg, max_eq_left (le_of_lt hcross)] + refine ⟨?_, ?_⟩ + · rw [hv] + exact sound_bottomRange_of_nonpos_crossed_long hbegin hend (le_of_lt hcross) rfl rfl + · intro k hk + rw [hv] at hk + rcases lt_or_ge (k : WithTop Key) r.end_.key with hkE | hkE + · exact emptyWitness_nonpos_crossed_long_below r hbegin hend (le_of_lt hcross) hAE hkE + · exact emptyWitness_nonpos_crossed_long_interval r hbegin hend hcross hgap hAE hkE hk.2 + +/-- **`EmptyInferenceWitnessOffOne` from the nonpositive-begin residual.** The +off-one region splits into the nonpositive begin offsets: the overshooting end +(`end.offset ≥ 2`, `emptyWitness_nonpos_overshoot`); then with `end.offset ≤ 1` the +cross-free `end.offset = 1` case (`emptyWitness_nonpos_end_one_crossFree`), the crossed +`end.offset = 1` short gap (`emptyWitness_nonpos_end_one_crossedShort`), the crossed +`end.offset = 1` long gap (`emptyWitness_nonpos_end_one_crossedLong`), the cross-free +`end.offset ≤ 0` case (`emptyWitness_nonpos_end_nonpos_crossFree`), or the crossed +`end.offset ≤ 0` residual; and the `1 ≤ begin.offset` region splits into `end.offset ≤ 1` +(`emptyWitness_underEnd_gen`) and `end.offset ≥ 2` (`emptyWitness_overshoot`). -/ +theorem emptyInferenceWitnessOffOne_of_residual (h : EmptyInferenceWitnessResidual) : + EmptyInferenceWitnessOffOne := by + intro r' hb hr + rcases le_or_gt r'.begin.offset 0 with hb0 | hbpos + · rcases lt_or_ge r'.end_.offset 2 with he | he + · -- `end.offset ≤ 1` + by_cases h1 : r'.end_.offset = 1 + · rcases lt_or_ge r'.end_.key r'.begin.key with hcross | hle + · by_cases hg : intervalCard r'.end_.key r'.begin.key < + (((1 - r'.begin.offset).toNat : ℕ) : ℕ∞) + · exact emptyWitness_nonpos_end_one_crossedShort r' hb0 h1 hcross hg hr + · exact emptyWitness_nonpos_end_one_crossedLong r' hb0 h1 hcross (not_lt.mp hg) hr + · exact emptyWitness_nonpos_end_one_crossFree r' hb0 h1 hle hr + · -- `end.offset ≤ 0` + rcases lt_or_ge r'.end_.key r'.begin.key with hcross | hle + · by_cases hg : (((r'.end_.offset - r'.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r'.end_.key r'.begin.key + · exact h r' hb0 (by omega) hcross hg hr + · exact emptyWitness_nonpos_crossed_short r' hb0 (by omega) hcross (not_le.mp hg) hr + · exact emptyWitness_nonpos_end_nonpos_crossFree r' hb0 (by omega) hle hr + · exact emptyWitness_nonpos_overshoot r' hb0 he hr + · have hb1 : 1 ≤ r'.begin.offset := by omega + rcases lt_or_ge r'.end_.offset 2 with he | he + · exact emptyWitness_underEnd_gen r' hb1 (by omega) hr + · exact emptyWitness_overshoot r' hb1 he hr + +/-- **`EmptyInferenceWitness` from the residual.** The whole empty theory reduces to +`EmptyInferenceWitnessResidual`: the crossed nonpositive-begin, nonpositive-end long gap. -/ +theorem emptyInferenceWitness_of_residual (h : EmptyInferenceWitnessResidual) : + EmptyInferenceWitness := + emptyInferenceWitness_of_offOne (emptyInferenceWitnessOffOne_of_residual h) + +/-- **The witness oracle, unconditionally.** The last residual +`EmptyInferenceWitnessResidual` is discharged by the nonpositive-end crossed case analysis: +short gaps by `emptyWitness_nonpos_crossed_short`, long gaps by +`emptyWitness_nonpos_crossed_long`. -/ +theorem emptyInferenceWitness : EmptyInferenceWitness := + emptyInferenceWitness_of_residual fun r' hb he hcross hgap hr => + emptyWitness_nonpos_crossed_long r' hb he hcross hgap hr + +/-- **The abstract empty inference, unconditionally.** -/ +theorem emptyInference : EmptyInference := + emptyInference_of_witness emptyInferenceWitness + +/-- **The partial algorithm is minimal wherever it is defined.** `conflictRangePartial` +returns `some` only on certified cases, each discharged by a named theorem: an empty +result by the (unconditional) empty inference `emptyInference`; the offset-one +complete/truncated cases by the offset-one certificates; an overshooting end by the +unbounded-top theorems for a complete result and the truncated/top-of-last theorems +otherwise; and every nonempty complete result at `begin.offset ≥ 1` by +`minimal_canonicalRange_of_end_offset_le_one_general` for `underEnd` and the +`minimal_topRange_of_end_offset_ge_two_general` pair for an overshoot, with the truncated +*non-reversed* result by `minimal_truncatedRange_of_end_offset_ge_two_general` at an +overshoot and `minimal_truncatedRange_of_end_offset_one` at `end.offset = 1`. The +`none` branches are unreachable from a `some` value, so only their guards matter. This +statement lives after `emptyInference` because its empty branch consumes that theorem; +nothing in the definition depends on the ordering. -/ +theorem conflictRangePartial_spec {r : RangeRead} {res : Result} {cr : ConflictRange} + (hread : Readable r res) (h : conflictRangePartial r res = some cr) : + Minimal r res cr := by + unfold conflictRangePartial at h + by_cases hkeys : res.keys = [] + · rw [dite_eq_left hkeys, Option.some.injEq] at h + subst h + exact minimal_emptyValue_of_emptyInference emptyInference hread hkeys + · rw [dite_eq_right hkeys] at h + by_cases hb : beginOne r + · rw [dite_eq_left hb] at h + by_cases he : endOne r + · rw [dite_eq_left he, Option.some.injEq] at h + subst h + by_cases hmore : res.more = true + · by_cases hrev : r.reverse = true + · exact conflictRangeOffsetOne_spec_truncated_rev hread hmore hrev hb he + · exact conflictRangeOffsetOne_spec_truncated hread hmore (by simpa using hrev) hb he + · exact conflictRangeOffsetOne_spec hread (by simpa using hmore) hb he + · rw [dite_eq_right he] at h + by_cases hover : overshoot r res + · rw [dite_eq_left hover] at h + obtain ⟨hend2, hne, hhi⟩ := hover + by_cases hmore : res.more = false + · rw [dite_eq_left hmore, Option.some.injEq] at h + subst h + cases hrev : r.reverse with + | false => + exact minimal_topRange_of_end_offset_ge_two hread hmore hrev hne hb hend2 hhi + | true => + exact minimal_topRange_of_end_offset_ge_two_rev hread hmore hrev hne hb hend2 hhi + · rw [dite_eq_right hmore] at h + by_cases hrev : r.reverse = false + · rw [dite_eq_left hrev, Option.some.injEq] at h + subst h + exact minimal_truncatedRange_of_end_offset_ge_two hread (by simpa using hmore) + hrev hb hend2 hhi + · rw [dite_eq_right hrev, Option.some.injEq] at h + subst h + exact minimal_topOfLast_of_end_offset_ge_two_rev hread (by simpa using hmore) + (by simpa using hrev) hb hend2 hhi + · rw [dite_eq_right hover] at h + by_cases hunder : underEnd r + · rw [dite_eq_left hunder] at h + by_cases hm : res.more = false + · rw [dite_eq_left hm, Option.some.injEq] at h + subst h + exact minimal_canonicalRange_of_end_offset_le_one_general hread hm hkeys hb.ge hunder + · rw [dite_eq_right hm] at h + by_cases hrev : r.reverse = false + · rw [dite_eq_left hrev, Option.some.injEq] at h + subst h + exact minimal_canonicalRange_of_truncated_end_offset_nonpos hread + (by simpa using hm) hrev hb.ge (by + have hne1 : r.end_.offset ≠ 1 := fun hcon => he hcon + omega) + · rw [dite_eq_right hrev, Option.some.injEq] at h + subst h + exact minimal_truncatedRangeRev_of_end_offset_nonpos hread (by simpa using hm) + (by simpa using hrev) hb (by + have hne1 : r.end_.offset ≠ 1 := fun hcon => he hcon + omega) + · rw [dite_eq_right hunder] at h + exact absurd h (by simp) + · rw [dite_eq_right hb] at h + by_cases hge : 1 ≤ r.begin.offset + · rw [dite_eq_left hge] at h + by_cases he0 : r.end_.offset ≤ 0 + · rw [dite_eq_left he0] at h + by_cases hm : res.more = false + · rw [dite_eq_left hm, Option.some.injEq] at h + subst h + exact minimal_canonicalRange_of_end_offset_le_one_general hread hm hkeys hge + (by omega) + · rw [dite_eq_right hm] at h + by_cases hrev : r.reverse = false + · rw [dite_eq_left hrev, Option.some.injEq] at h + subst h + exact minimal_canonicalRange_of_truncated_end_offset_nonpos hread + (by simpa using hm) hrev hge he0 + · rw [dite_eq_right hrev, Option.some.injEq] at h + subst h + exact minimal_canonicalRange_of_end_offset_le_one_rev_more hread (by simpa using hm) + (by simpa using hrev) hkeys + (by have hne1 : r.begin.offset ≠ 1 := + fun hcon => hb (by simpa [beginOne] using hcon) + omega) + (by omega) + · rw [dite_eq_right he0] at h + by_cases hunder : underEnd r + · rw [dite_eq_left hunder] at h + by_cases hm : res.more = false + · rw [dite_eq_left hm, Option.some.injEq] at h + subst h + exact minimal_canonicalRange_of_end_offset_le_one_general hread hm hkeys hge hunder + · rw [dite_eq_right hm] at h + by_cases hrev : r.reverse = false + · rw [dite_eq_left hrev, Option.some.injEq] at h + subst h + exact minimal_truncatedRange_of_end_offset_one hread (by simpa using hm) hrev hge + (by omega) + · rw [dite_eq_right hrev, Option.some.injEq] at h + subst h + exact minimal_canonicalRange_of_end_offset_le_one_rev_more hread (by simpa using hm) + (by simpa using hrev) hkeys + (by have hne1 : r.begin.offset ≠ 1 := + fun hcon => hb (by simpa [beginOne] using hcon) + omega) + hunder + · rw [dite_eq_right hunder] at h + by_cases hover : overshoot r res + · rw [dite_eq_left hover] at h + obtain ⟨hend2, hne, hhi⟩ := hover + by_cases hm : res.more = false + · rw [dite_eq_left hm, Option.some.injEq] at h + subst h + cases hrev : r.reverse with + | false => + exact minimal_topRange_of_end_offset_ge_two_general hread hm hrev hne + hge hend2 hhi + | true => + exact minimal_topRange_of_end_offset_ge_two_rev_general hread hm hrev hne + hge hend2 hhi + · rw [dite_eq_right hm] at h + by_cases hrev : r.reverse = false + · rw [dite_eq_left hrev, Option.some.injEq] at h + subst h + exact minimal_truncatedRange_of_end_offset_ge_two_general hread + (by simpa using hm) hrev hge hend2 hhi + · rw [dite_eq_right hrev, Option.some.injEq] at h + subst h + exact minimal_topRange_of_end_offset_ge_two_rev_more hread (by simpa using hm) + (by simpa using hrev) + (by have hne1 : r.begin.offset ≠ 1 := + fun hcon => hb (by simpa [beginOne] using hcon) + omega) + hend2 hhi + · rw [dite_eq_right hover] at h + exact absurd h (by simp) + · rw [dite_eq_right hge] at h + by_cases hnp : nonposOvershootComplete r res + · rw [dite_eq_left hnp, Option.some.injEq] at h + subst h + obtain ⟨hend2, hm, hne, hhi, hloD⟩ := hnp + cases hrev : r.reverse with + | false => + exact minimal_fullRange_of_nonpos_overshoot_nonempty hread hrev hm hne + (by omega) hend2 hhi (by + have hloD' : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rwa [← hhead]) + | true => + exact minimal_fullRange_of_nonpos_overshoot_nonempty_rev hread hrev hm hne + (by omega) hend2 hhi (by + have hloD' : r.begin.key ≤ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rwa [← hlast]) + · rw [dite_eq_right hnp] at h + by_cases hnu : nonposUnderEndComplete r res + · rw [dite_eq_left hnu, Option.some.injEq] at h + subst h + obtain ⟨hend1, hm, hne, hloD⟩ := hnu + cases hrev : r.reverse with + | false => + exact minimal_bottomRange_of_nonpos_underEnd_nonempty hread hrev hm hne + (by omega) hend1 (by + have hloD' : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rwa [← hhead]) + | true => + exact minimal_bottomRange_of_nonpos_underEnd_nonempty_rev hread hrev hm hne + (by omega) hend1 (by + have hloD' : r.begin.key ≤ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rwa [← hlast]) + · rw [dite_eq_right hnu] at h + by_cases hft : nonposFwdTruncPinned r res + · rw [dite_eq_left hft, Option.some.injEq] at h + subst h + obtain ⟨hm, hrev, hloD, hend, hhi⟩ := hft + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hkeys := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hkeys]; rfl + rw [hlast] + exact minimal_truncatedRangeBottom_of_nonpos hread hrev hm hkeys (by omega) (by + have hloD' : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hhead : res.keys.headD ([] : Key) = res.keys.head hkeys := by + rw [List.headD_eq_head?, List.head?_eq_some_head hkeys]; rfl + rwa [← hhead]) hend hhi + · rw [dite_eq_right hft] at h + by_cases hrt : nonposRevTruncOvershoot r res + · rw [dite_eq_left hrt, Option.some.injEq] at h + subst h + obtain ⟨hm, hrevr, hloD, hend2, hhi⟩ := hrt + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hkeys := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hkeys]; rfl + rw [hlast] + exact minimal_topOfLast_of_end_offset_ge_two_rev_nonpos hread hm hrevr hkeys + (by omega) hend2 (by + have hloD' : r.begin.key ≤ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrevr] using hloD + rwa [← hlast]) hhi + · rw [dite_eq_right hrt] at h + by_cases hrt1 : nonposRevTruncEndOne r res + · rw [dite_eq_left hrt1, Option.some.injEq] at h + subst h + obtain ⟨hm, hrevr, hloD, hend1⟩ := hrt1 + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hkeys := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hkeys]; rfl + rw [hlast] + exact minimal_truncatedRangeRev_of_end_offset_one_nonpos hread hm hrevr hkeys + (by omega) hend1 (by + have hloD' : r.begin.key ≤ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrevr] using hloD + rwa [← hlast]) + · rw [dite_eq_right hrt1] at h + by_cases hzc : zeroCrossOvershootComplete r res + · rw [dite_eq_left hzc, Option.some.injEq] at h + subst h + obtain ⟨hbeg0, hm, hrevf, hov, hcrossL⟩ := hzc + obtain ⟨hend2, hne, hhi⟩ := hov + have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < + r.begin.key := by + simpa [leastReported, hrevf] using hcrossL + have hhead : res.keys.headD ([] : Key) = res.keys.head hkeys := by + rw [List.headD_eq_head?, List.head?_eq_some_head hkeys]; rfl + rw [hhead] at hcrossD + rw [hhead] + exact minimal_topOfHead_of_zero_begin_crossed_overshoot hread hrevf hm + hkeys hbeg0 hcrossD hend2 hhi + · rw [dite_eq_right hzc] at h + by_cases hze : zeroCrossEndOne r res + · rw [dite_eq_left hze, Option.some.injEq] at h + subst h + obtain ⟨hbeg0, hm, hrevf, hend1, hne', hcrossL, hkeyle⟩ := hze + have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < + r.begin.key := by + simpa [leastReported, hrevf] using hcrossL + have hhead : res.keys.headD ([] : Key) = res.keys.head hne' := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne']; rfl + rw [hhead] at hcrossD + rw [hhead] + exact minimal_headToEnd_of_zero_begin_crossed_endOne hread hrevf hm hne' + hbeg0 hcrossD hend1 hkeyle + · rw [dite_eq_right hze] at h + by_cases hzel : zeroCrossEndOneLong r res + · rw [dite_eq_left hzel, Option.some.injEq] at h + subst h + obtain ⟨hbeg0, hm, hrevf, hend1, hne', hcrossL, hlongD⟩ := hzel + have hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < + r.begin.key := by + simpa [leastReported, hrevf] using hcrossL + have hhead : res.keys.headD ([] : Key) = res.keys.head hne' := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne']; rfl + rw [hhead] at hcrossD + rw [hhead] + exact minimal_headToBeginKey_of_zero_begin_crossed_endOne_long hread + hrevf hm hne' hbeg0 hcrossD hend1 hlongD + · rw [dite_eq_right hzel] at h + exact absurd h (by simp) + +/-! +## The desired outcome: a total minimal algorithm + +The stated objective is a *total* conflict-range algorithm returning a *minimal* sound +range on every realizable read/result pair. This section makes the objective precise and +locates it relative to the existence headline. As a *function*, the objective is not +stronger than the headline: given the headline, `Classical.choice` turns the pointwise +existentials into a total `ConflictRange`-valued function, and `conflictRangeTotal` below +refines the algorithm of record `conflictRangePartial` — it agrees with it on every +certified `some` value, and on the uncertified `none` branches returns the chosen minimal +range. What would be strictly stronger is a *computable* total algorithm, i.e. branch +completeness of `conflictRangePartial`, which is blocked on exactly the regions where no +offset-formula candidate exists (`cwMin` versus the unbounded top). +-/ + +/-- The desired outcome, as a proposition: a total `ConflictRange`-valued algorithm that +returns a minimal sound range on every realizable read/result pair. Non-realizable inputs +carry no requirement; being total, the algorithm still returns a range there. -/ +def TotalMinimalAlgorithm : Prop := + ∃ algo : RangeRead → Result → ConflictRange, + ∀ r res, Readable r res → Minimal r res (algo r res) + +/-- **The desired outcome is exactly the existence headline.** A total minimal algorithm +exists iff every realizable pair admits a minimal sound range. The forward direction is +immediate; the reverse is `Classical.choice`, which needs no extra structure because the +minimal key set is unique (`Minimal.toSet_eq`). So "minimal and total" is not a stronger +obligation than existence at the level of functions. -/ +theorem totalMinimalAlgorithm_iff : + TotalMinimalAlgorithm ↔ + ∀ r res, Readable r res → ∃ cr : ConflictRange, Minimal r res cr := by + constructor + · rintro ⟨algo, h⟩ r res hread + exact ⟨algo r res, h r res hread⟩ + · intro h + classical + refine ⟨fun r res => + if hh : ∃ cr : ConflictRange, Minimal r res cr then Classical.choose hh + else canonicalRange r, ?_⟩ + intro r res hread + have hh : ∃ cr : ConflictRange, Minimal r res cr := h r res hread + dsimp only + rw [dite_eq_left hh] + exact Classical.choose_spec hh + +/-- **A totalization of the algorithm of record.** Given the headline, `conflictRangePartial` +extends to a total algorithm that is minimal on every realizable pair: it keeps the certified +value where `conflictRangePartial` returns `some`, and on the uncertified `none` branches +returns the (classically chosen) minimal range. Making it *computable* — filling the `none` +branches with explicit candidates — is the remaining branch-completeness work. -/ +noncomputable def conflictRangeTotal + (_h : ∀ r res, Readable r res → ∃ cr : ConflictRange, Minimal r res cr) : + RangeRead → Result → ConflictRange := by + classical + exact fun r res => + match conflictRangePartial r res with + | some cr => cr + | none => + if hh : ∃ cr : ConflictRange, Minimal r res cr then Classical.choose hh + else canonicalRange r + +/-- `conflictRangeTotal` is minimal wherever the pair is realizable. -/ +theorem conflictRangeTotal_spec + (h : ∀ r res, Readable r res → ∃ cr : ConflictRange, Minimal r res cr) + {r : RangeRead} {res : Result} (hread : Readable r res) : + Minimal r res (conflictRangeTotal h r res) := by + classical + unfold conflictRangeTotal + cases hpart : conflictRangePartial r res with + | some cr => exact conflictRangePartial_spec hread hpart + | none => + have hh : ∃ cr : ConflictRange, Minimal r res cr := h r res hread + dsimp only + rw [dite_eq_left hh] + exact Classical.choose_spec hh + +/-- `conflictRangeTotal` agrees with the algorithm of record on every certified `some` +value. -/ +theorem conflictRangeTotal_eq_partial + (h : ∀ r res, Readable r res → ∃ cr : ConflictRange, Minimal r res cr) + {r : RangeRead} {res : Result} {cr : ConflictRange} + (hpart : conflictRangePartial r res = some cr) : + conflictRangeTotal h r res = cr := by + unfold conflictRangeTotal + rw [hpart] + +/-- **A complete empty theory from the witness oracle.** The `witness` field is +supplied by `emptyInferenceWitness`; the other three fields remain the graft and +gap-split obligations. The graft fields are guarded by `Readable`. -/ +theorem emptyTheoryComplete_of_witness + (hjoint : ∀ (r : RangeRead) (res : Result), Readable r res → BoundaryJointBases r res) + (hbracket : ∀ (r : RangeRead) (res : Result), BracketFactor r res) + (htrunc : ∀ (r : RangeRead) (res : Result), Readable r res → GeneralObligation r res) : + EmptyTheoryComplete := + ⟨emptyInferenceWitness, hjoint, hbracket, htrunc⟩ + +/-- **A complete empty theory from the (now vacuous) residual.** Kept for compatibility: +`EmptyInferenceWitnessResidual` is inhabited by the empty theory, so the residual may be +supplied trivially. -/ +theorem emptyTheoryComplete_of_residual (h : EmptyInferenceWitnessResidual) + (hjoint : ∀ (r : RangeRead) (res : Result), Readable r res → BoundaryJointBases r res) + (hbracket : ∀ (r : RangeRead) (res : Result), BracketFactor r res) + (htrunc : ∀ (r : RangeRead) (res : Result), Readable r res → GeneralObligation r res) : + EmptyTheoryComplete := + ⟨emptyInferenceWitness_of_residual h, hjoint, hbracket, htrunc⟩ + +end FormalConflictRange + + + diff --git a/FormalConflictRange/Basic.lean b/FormalConflictRange/Basic.lean new file mode 100644 index 0000000..7b467b6 --- /dev/null +++ b/FormalConflictRange/Basic.lean @@ -0,0 +1,1688 @@ +/- +Copyright (c) 2026 FormalConflictRange contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: FormalConflictRange contributors +-/ +import Mathlib + +/-! +# Keys and databases + +This file defines the basic data model: `Byte`, `Key` (lexicographically +ordered byte strings) and `DB` (a sorted, duplicate-free list of keys). + +## Design note: keys and `WithTop` + +`Key` is ordered lexicographically, so it has a least element, the empty key, +and no greatest element. Wherever a key-shaped value may need to name a +position outside the stored keys of an arbitrary database — a selector's +anchor, a resolved selector target, an endpoint of a conflict range — the +library uses `WithTop Key`, whose `⊥` is the empty key and whose `⊤` is a +genuine sentinel above every key. The bottom needs no sentinel of its own: the +empty key is the least key, so "the start of the key space" is expressed by the +empty key itself, which keeps acting as an ordinary key throughout the +development (indeed, as `FormalConflictRange.Sound` shows, the empty key can +itself be necessary). The top is different: no fixed key lies above every +stored key of an arbitrary database, so only `⊤` can serve as an unbounded +upper end or anchor a selector past the end of the database — which is what +makes a database-independent "read all keys" read (`RangeRead.readAll`) +expressible. +-/ + +namespace FormalConflictRange + +/-- A byte is an 8-bit value, represented as an element of `Fin 256`. -/ +abbrev Byte := Fin 256 + +/-- A key is a list of bytes, ordered lexicographically. -/ +abbrev Key := List Byte + +/-- A database is a sorted, duplicate-free list of keys. +The strict sortedness condition subsumes the no-duplicates condition. -/ +structure DB where + keys : List Key + sorted : keys.SortedLT + +namespace DB + +/-- A database contains no duplicate keys. -/ +theorem nodup (db : DB) : db.keys.Nodup := + db.sorted.pairwise.nodup + +/-- A `List.Pairwise (· ≤ ·)` list with no duplicates is `List.Pairwise (· < ·)`. -/ +theorem pairwise_lt_of_le_of_nodup {l : List Key} (h : l.Pairwise (· ≤ ·)) (hnd : l.Nodup) : + l.Pairwise (· < ·) := by + rw [List.pairwise_iff_getElem] at h ⊢ + intro i j hi hj hij + have hle := h i j hi hj hij + have hne : l[i] ≠ l[j] := fun heq => (Nat.ne_of_lt hij) ((hnd.getElem_inj_iff).mp heq) + exact lt_of_le_of_ne hle hne + +/-- Remove a key from a database, preserving sortedness. -/ +def erase (db : DB) (k : Key) : DB where + keys := db.keys.erase k + sorted := List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.erase k) + +/-- Insert a key not already stored into a database at its sorted position, +preserving sortedness. -/ +def insert (db : DB) (k : Key) (hk : k ∉ db.keys) : DB where + keys := db.keys.orderedInsert (· ≤ ·) k + sorted := List.sortedLT_iff_pairwise.mpr <| + pairwise_lt_of_le_of_nodup + ((db.sorted.pairwise.imp fun h => le_of_lt h).orderedInsert k) + ((List.perm_orderedInsert (· ≤ ·) k db.keys).nodup_iff.mpr + (List.nodup_cons.mpr ⟨hk, db.nodup⟩)) + +/-- The keys of an erased database are the erased keys of the database. -/ +@[simp] theorem erase_keys (db : DB) (k : Key) : (db.erase k).keys = db.keys.erase k := rfl + +/-- The keys of an inserted database are the ordered insert of the keys. -/ +@[simp] theorem insert_keys (db : DB) (k : Key) (hk : k ∉ db.keys) : + (db.insert k hk).keys = db.keys.orderedInsert (· ≤ ·) k := rfl + +/-- A key is stored in an erased database exactly when it is stored in the +original and differs from the erased key. -/ +theorem mem_erase_iff {db : DB} {k x : Key} : x ∈ (db.erase k).keys ↔ x ≠ k ∧ x ∈ db.keys := by + rw [erase_keys] + exact List.Nodup.mem_erase_iff db.nodup + +/-- The erased key is no longer stored. -/ +theorem not_mem_erase_self (db : DB) (k : Key) : k ∉ (db.erase k).keys := by + rw [erase_keys] + exact List.Nodup.not_mem_erase db.nodup + +/-- A key is stored in an inserted database exactly when it is the inserted key +or was already stored. -/ +theorem mem_insert_iff {db : DB} {k x : Key} (hk : k ∉ db.keys) : + x ∈ (db.insert k hk).keys ↔ x = k ∨ x ∈ db.keys := by + rw [insert_keys] + exact List.mem_orderedInsert (· ≤ ·) + +/-- The inserted key is stored. -/ +theorem mem_insert_self (db : DB) (k : Key) (hk : k ∉ db.keys) : + k ∈ (db.insert k hk).keys := + (mem_insert_iff hk).mpr (Or.inl rfl) + +/-- Erasing the key just inserted restores the database's keys. This is +`List.erase_orderedInsert` for `Key`; it is stated here because the `List.erase` +`BEq` instance is not definitionally the decidable-equality one, so the library +lemma does not apply directly. -/ +theorem erase_insert_keys (db : DB) (k : Key) (hk : k ∉ db.keys) : + ((db.insert k hk).erase k).keys = db.keys := by + rw [erase_keys, insert_keys] + induction db.keys with + | nil => simp + | cons a t ih => + rw [List.orderedInsert_cons] + by_cases h : k ≤ a + · rw [ite_eq_left h, List.erase_cons_head] + · rw [ite_eq_right h] + have h' : ¬(a == k) := by + intro hk'; rw [beq_iff_eq] at hk'; exact h (le_of_eq hk'.symm) + rw [List.erase_cons_tail h', ih] + +/-- Erase every stored key in the half-open interval `[a, b)`, preserving +sortedness. This is the interval analogue of `erase`, removing a whole interval +of keys at once rather than a single key. -/ +def eraseInterval (db : DB) (a b : WithTop Key) : DB where + keys := db.keys.filter + (fun (x : Key) => decide (¬ (a ≤ (x : WithTop Key) ∧ (x : WithTop Key) < b))) + sorted := List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + +/-- The keys of an interval-erased database are the interval-filtered keys. -/ +@[simp] theorem eraseInterval_keys (db : DB) (a b : WithTop Key) : + (db.eraseInterval a b).keys = db.keys.filter + (fun (x : Key) => decide (¬ (a ≤ (x : WithTop Key) ∧ (x : WithTop Key) < b))) := rfl + +/-- A key is stored in an interval-erased database exactly when it is stored in +the original and does not lie in the erased interval. -/ +theorem mem_eraseInterval_iff {db : DB} {a b : WithTop Key} {x : Key} : + x ∈ (db.eraseInterval a b).keys ↔ + x ∈ db.keys ∧ ¬(a ≤ (x : WithTop Key) ∧ (x : WithTop Key) < b) := by + rw [eraseInterval_keys, List.mem_filter] + simp only [decide_eq_true_eq] + +end DB + +/-- In a strictly increasing list, exactly the keys at indices below `n` are +strictly below the `n`-th key. Hence the number of keys strictly below that key +is `n`. -/ +theorem countP_lt_eq_of_getElem {l : List Key} (hs : l.SortedLT) {x : Key} {n : Nat} + (hn : n < l.length) (hnx : l[n] = x) : + l.countP (fun k => decide (k < x)) = n := by + have hdecomp : l = l.take n ++ l[n] :: l.drop (n + 1) := by + have h1 := List.take_append_drop (n + 1) l + rw [List.take_succ_eq_append_getElem hn, List.append_assoc, List.singleton_append] at h1 + exact h1.symm + rw [hdecomp, List.countP_append] + rw [List.countP_cons_of_neg] + · have htake : (l.take n).countP (fun k => decide (k < x)) = (l.take n).length := by + rw [List.countP_eq_length] + intro k hk + rw [List.mem_take_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + rw [decide_eq_true_eq, ← hnx, hs.getElem_lt_getElem_iff] + omega + rw [htake, List.length_take, Nat.min_eq_left (le_of_lt hn)] + have hdrop : (l.drop (n + 1)).countP (fun k => decide (k < x)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [List.mem_drop_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + simp only [decide_eq_true_eq] + rw [← hnx] + exact not_lt.mpr ((hs.getElem_le_getElem_iff).mpr (by omega)) + rw [hdrop, Nat.add_zero] + · rw [hnx]; simp + +/-- A list containing a key its predicate accepts has a positive count. -/ +theorem countP_pos_of_mem {l : List Key} {p : Key → Bool} {a : Key} (h : a ∈ l) + (hp : p a = true) : 0 < l.countP p := by + by_contra h0 + have hz : l.countP p = 0 := by omega + exact (List.countP_eq_zero.mp hz) a h hp + +/-- A key below `x` at index `i` of a strictly increasing list forces more than +`i` keys below `x`. -/ +theorem lt_countP_of_getElem_lt {l : List Key} (hs : l.SortedLT) {x : Key} {i : Nat} + (hi : i < l.length) (hlt : l[i] < x) : i < l.countP (fun k => decide (k < x)) := by + have h1 : (l.take (i + 1)).countP (fun k => decide (k < x)) = (l.take (i + 1)).length := by + rw [List.countP_eq_length] + intro k hk + rw [List.mem_take_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt ((hs.getElem_le_getElem_iff).mpr (by omega)) hlt + have h3 : (l.take (i + 1)).countP (fun k => decide (k < x)) ≤ + l.countP (fun k => decide (k < x)) := + List.Sublist.countP_le (List.take_prefix (i + 1) l).sublist + rw [h1, List.length_take, Nat.min_eq_left (by omega)] at h3 + omega + +/-- In a strictly increasing list, the key at an index below the count of keys +below `x` is itself below `x`. -/ +theorem getElem_lt_of_lt_countP {l : List Key} (hs : l.SortedLT) {x : Key} {i : Nat} + (hi : i < l.length) (hic : i < l.countP (fun k => decide (k < x))) : l[i] < x := by + by_contra h + have hle : x ≤ l[i] := le_of_not_gt h + have hzero : (l.drop i).countP (fun k => decide (k < x)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [List.mem_drop_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + simp only [decide_eq_true_eq] + exact not_lt.mpr (le_trans hle ((hs.getElem_le_getElem_iff).mpr (by omega))) + have hsplit : l.countP (fun k => decide (k < x)) = + (l.take i).countP (fun k => decide (k < x)) + + (l.drop i).countP (fun k => decide (k < x)) := by + conv_lhs => rw [← List.take_append_drop i l] + rw [List.countP_append] + rw [hzero, Nat.add_zero] at hsplit + have hle' : (l.take i).countP (fun k => decide (k < x)) ≤ (l.take i).length := + List.countP_le_length + rw [List.length_take, Nat.min_eq_left (le_of_lt hi)] at hle' + omega + +/-- Erasing the `n`-th key does not change the count of keys strictly below it. -/ +theorem eraseIdx_countP {l : List Key} (hs : l.SortedLT) {x : Key} {n : Nat} + (hn : n < l.length) (hnx : l[n] = x) : + (l.eraseIdx n).countP (fun k => decide (k < x)) = n := by + rw [List.eraseIdx_eq_take_drop_succ, List.countP_append] + have htake : (l.take n).countP (fun k => decide (k < x)) = (l.take n).length := by + rw [List.countP_eq_length] + intro k hk + rw [List.mem_take_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + rw [decide_eq_true_eq, ← hnx, hs.getElem_lt_getElem_iff] + omega + have hdrop : (l.drop (n + 1)).countP (fun k => decide (k < x)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [List.mem_drop_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + simp only [decide_eq_true_eq] + rw [← hnx] + exact not_lt.mpr ((hs.getElem_le_getElem_iff).mpr (by omega)) + rw [htake, List.length_take, Nat.min_eq_left (le_of_lt hn), hdrop, Nat.add_zero] + +/-- Erasing the key at index `n` brings the successor of that key to index `n`. -/ +theorem eraseIdx_getElem_succ {l : List Key} (n : Nat) (hn : n + 1 < l.length) : + (l.eraseIdx n)[n]'(by rw [List.length_eraseIdx]; split <;> omega) = l[n + 1] := by + rw [List.getElem_eraseIdx_of_ge (by rw [List.length_eraseIdx]; split <;> omega) (le_refl n)] + +/-- Insert at rank `m`, decomposed into a prefix, the element, and a suffix. -/ +theorem insertIdx_eq_take_drop : ∀ (l : List Key) (i : Nat) (k : Key), i ≤ l.length → + l.insertIdx i k = l.take i ++ k :: l.drop i := by + intro l + induction l with + | nil => intro i k hi; simp only [List.length_nil, nonpos_iff_eq_zero] at hi; subst hi; rfl + | cons a t ih => + intro i k hi + cases i with + | zero => rfl + | succ i => + change a :: t.insertIdx i k = a :: (t.take i ++ k :: t.drop i) + rw [ih i k (by simpa using hi)] + +/-- Filtering is invariant under inserting a key the predicate rejects. -/ +theorem filter_insertIdx_of_not {l : List Key} (p : Key → Bool) (m : Nat) (k : Key) + (hk : p k = false) : (l.insertIdx m k).filter p = l.filter p := by + by_cases hm : m ≤ l.length + · rw [insertIdx_eq_take_drop l m k hm, List.filter_append, + List.filter_cons_of_neg (by simpa using hk)] + rw [← List.filter_append, List.take_append_drop m l] + · rw [List.insertIdx_of_length_lt (by omega)] + +/-- In a strictly increasing list not containing `v`, the prefix of keys below `v` +is taken by `takeWhile (¬ v ≤ ·)`. -/ +theorem takeWhile_not_le_eq_take {l : List Key} (hs : l.SortedLT) {v : Key} (hv : v ∉ l) : + l.takeWhile (fun b => ¬ v ≤ b) = + l.take (l.countP (fun k => decide (k < v))) := by + induction l with + | nil => rfl + | cons a t ih => + have ht : t.SortedLT := List.sortedLT_iff_pairwise.mpr (List.pairwise_cons.mp hs.pairwise).2 + have hvt : v ∉ t := fun h => hv (List.mem_cons_of_mem a h) + by_cases hva : v ≤ a + · rw [List.takeWhile_cons_of_neg (by simp [hva])] + have hcount : (a :: t).countP (fun k => decide (k < v)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [List.mem_cons] at hx + rcases hx with rfl | hx + · simp [hva] + · have hax : a ≤ x := le_of_lt ((List.pairwise_cons.mp hs.pairwise).1 x hx) + simp [not_lt.mpr (le_trans hva hax)] + rw [hcount]; rfl + · have hlt : a < v := lt_of_not_ge hva + rw [List.takeWhile_cons_of_pos (by simp [hva]), ih ht hvt] + have hcount : (a :: t).countP (fun k => decide (k < v)) = + t.countP (fun k => decide (k < v)) + 1 := by + rw [List.countP_cons]; simp [hlt] + rw [hcount, List.take_succ_cons] + +/-- In a strictly increasing list not containing `v`, the suffix of keys at or +above `v` is taken by `dropWhile (¬ v ≤ ·)`. -/ +theorem dropWhile_not_le_eq_drop {l : List Key} (hs : l.SortedLT) {v : Key} (hv : v ∉ l) : + l.dropWhile (fun b => ¬ v ≤ b) = + l.drop (l.countP (fun k => decide (k < v))) := by + induction l with + | nil => rfl + | cons a t ih => + have ht : t.SortedLT := List.sortedLT_iff_pairwise.mpr (List.pairwise_cons.mp hs.pairwise).2 + have hvt : v ∉ t := fun h => hv (List.mem_cons_of_mem a h) + by_cases hva : v ≤ a + · rw [List.dropWhile_cons_of_neg (by simp [hva])] + have hcount : (a :: t).countP (fun k => decide (k < v)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [List.mem_cons] at hx + rcases hx with rfl | hx + · simp [hva] + · have hax : a ≤ x := le_of_lt ((List.pairwise_cons.mp hs.pairwise).1 x hx) + simp [not_lt.mpr (le_trans hva hax)] + rw [hcount] + rfl + · have hlt : a < v := lt_of_not_ge hva + rw [List.dropWhile_cons_of_pos (by simp [hva]), ih ht hvt] + have hcount : (a :: t).countP (fun k => decide (k < v)) = + t.countP (fun k => decide (k < v)) + 1 := by + rw [List.countP_cons]; simp [hlt] + rw [hcount, List.drop_succ_cons] + +/-- Inserting an absent key into a sorted list is an `insertIdx` at the count of +keys strictly below it. -/ +theorem orderedInsert_eq_insertIdx {l : List Key} (hs : l.SortedLT) {v : Key} + (hv : v ∉ l) : + l.orderedInsert (· ≤ ·) v = + l.insertIdx (l.countP (fun k => decide (k < v))) v := by + rw [List.orderedInsert_eq_take_drop, takeWhile_not_le_eq_take hs hv, + dropWhile_not_le_eq_drop hs hv] + exact (insertIdx_eq_take_drop l (l.countP (fun k => decide (k < v))) v + List.countP_le_length).symm + +/-- A key's rank equals the number of stored keys strictly below it. -/ +theorem getElem_countP_lt {l : List Key} (hs : l.SortedLT) {x : Key} (hx : x ∈ l) : + l[l.countP (fun k => decide (k < x))]'(by + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hx + rw [countP_lt_eq_of_getElem hs hi hix] + exact hi) = x := by + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hx + have hcount := countP_lt_eq_of_getElem hs hi hix + subst hcount + exact hix + +/-- Inserting a key the predicate rejects preserves the count of accepted keys. -/ +theorem countP_insertIdx {l : List Key} (p : Key → Bool) (i : Nat) (v : Key) + (hv : p v = false) : (l.insertIdx i v).countP p = l.countP p := by + rw [List.countP_eq_length_filter, List.countP_eq_length_filter, + filter_insertIdx_of_not p i v hv] + +/-- Inserting a key strictly between the insertion rank and `v` increments the +count of keys strictly below `v`, where `v` may be past every key. -/ +theorem countP_insertIdx_of_lt {l : List Key} (hs : l.SortedLT) {x : Key} {v : WithTop Key} + (hxv : (x : WithTop Key) < v) : + (l.insertIdx (l.countP (fun k => decide (k < x))) x).countP + (fun (k : Key) => decide ((↑k : WithTop Key) < v)) = + l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < v)) + 1 := by + set c := l.countP (fun k => decide (k < x)) with hc + have hclen : c ≤ l.length := by rw [hc]; exact List.countP_le_length + rw [insertIdx_eq_take_drop l c x hclen, List.countP_append, + List.countP_cons_of_pos (by rw [decide_eq_true_eq]; exact hxv)] + have htake : (l.take c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < v)) = c := by + have h1 : (l.take c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < v)) = + (l.take c).length := by + rw [List.countP_eq_length] + intro k hk + rw [List.mem_take_iff_getElem] at hk + obtain ⟨i, hi, rfl⟩ := hk + rw [decide_eq_true_eq] + exact lt_trans (WithTop.coe_lt_coe.mpr + (getElem_lt_of_lt_countP hs (by omega) (by rw [← hc]; omega))) hxv + rw [h1, List.length_take, Nat.min_eq_left hclen] + have hsplit : l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < v)) = + (l.take c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < v)) + + (l.drop c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < v)) := by + conv_lhs => rw [← List.take_append_drop c l] + rw [List.countP_append] + rw [htake] at hsplit + omega + +/-- The count of keys strictly below a coerced key `↑v` equals the count of keys +strictly below `v`. -/ +theorem countP_lt_coe {l : List Key} (v : Key) : + l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < (↑v : WithTop Key))) = + l.countP (fun k => decide (k < v)) := by + simp [WithTop.coe_lt_coe] + +/-- No key lies strictly below the empty key, so nothing is counted below it. -/ +theorem countP_lt_nil {l : List Key} : + l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < (([] : Key) : WithTop Key))) = 0 := by + rw [List.countP_eq_zero] + intro k _ + simp only [decide_eq_true_eq, WithTop.coe_lt_coe] + exact List.not_lt_nil k + +/-- Every key lies strictly below `⊤`, so all of `l` is counted below it. -/ +theorem countP_lt_top {l : List Key} : + l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < (⊤ : WithTop Key))) = l.length := by + rw [List.countP_eq_length] + intro k _ + rw [decide_eq_true_eq] + exact WithTop.coe_lt_top k + +/-! +## The least key above a key + +`keyAfter k = k ++ [0]` is the immediate successor of `k` in the lexicographic +order on keys: it lies strictly above `k`, and every key strictly below it is at +most `k`. This is what lets a range end just past a reported key while still +naming a genuine half-open upper bound. +-/ + +/-- A key strictly above `k`: append a zero byte. By `lt_keyAfter_iff` it is the +least such key. -/ +def keyAfter (k : Key) : Key := k ++ [0] + +/-- `keyAfter` commutes with `cons`, by definition. -/ +theorem keyAfter_cons (a : Byte) (t : Key) : keyAfter (a :: t) = a :: keyAfter t := rfl + +/-- A key lies strictly below its successor. -/ +theorem keyAfter_self_lt (l : Key) : l < keyAfter l := by + rw [keyAfter] + simpa using List.Lex.append_left (· < ·) + (List.Lex.nil (a := (0 : Byte)) (l := ([] : Key))) l + +/-- Lexicographic `≤` on keys, in the `LinearOrder` instance this library uses, +splits on the head byte. The core `List.cons_le_cons_iff` names a different `LE` +instance and so does not apply directly. -/ +private theorem cons_le_cons_iff {c a : Byte} {u t : Key} : + (c :: u ≤ a :: t) ↔ c < a ∨ c = a ∧ u ≤ t := by + rw [le_iff_lt_or_eq, List.cons_lt_cons_iff] + constructor + · rintro (h | h) + · rcases h with hca | ⟨hca, hut⟩ + · exact Or.inl hca + · exact Or.inr ⟨hca, le_of_lt hut⟩ + · rw [List.cons.injEq] at h + obtain ⟨rfl, rfl⟩ := h + exact Or.inr ⟨rfl, le_refl u⟩ + · rintro (hca | ⟨rfl, hut⟩) + · exact Or.inl (Or.inl hca) + · rw [le_iff_lt_or_eq] at hut + rcases hut with hut | hut + · exact Or.inl (Or.inr ⟨rfl, hut⟩) + · exact Or.inr (by rw [hut]) + +/-- A key is strictly below `keyAfter l` exactly when it is at or below `l`. -/ +theorem lt_keyAfter_iff (k l : Key) : k < keyAfter l ↔ k ≤ l := by + constructor + · intro h + induction l generalizing k with + | nil => + cases k with + | nil => exact le_refl [] + | cons c u => + simp only [keyAfter, List.nil_append] at h + rw [List.cons_lt_cons_iff] at h + rcases h with hc | ⟨_, h⟩ + · exact absurd hc (not_lt.mpr (by simp)) + · exact absurd h (List.not_lt_nil u) + | cons a t ih => + cases k with + | nil => exact (le_iff_lt_or_eq).mpr (Or.inl (List.nil_lt_cons a t)) + | cons c u => + rw [keyAfter_cons, List.cons_lt_cons_iff] at h + rcases h with hca | ⟨hca, hut⟩ + · exact cons_le_cons_iff.mpr (Or.inl hca) + · exact cons_le_cons_iff.mpr (Or.inr ⟨hca, ih u hut⟩) + · intro h + rcases (le_iff_lt_or_eq).mp h with hlt | heq + · exact List.lt_trans hlt (keyAfter_self_lt l) + · rw [heq]; exact keyAfter_self_lt l + +/-- `keyAfter l` is at most any `WithTop Key` bound lying strictly above `l`. -/ +theorem keyAfter_le_of_lt {l : Key} {E : WithTop Key} (h : (l : WithTop Key) < E) : + (keyAfter l : WithTop Key) ≤ E := by + cases E with + | top => exact le_top + | coe e => + refine WithTop.coe_le_coe.mpr ?_ + by_contra hcon + have hel : e < keyAfter l := not_le.mp hcon + exact absurd (WithTop.coe_lt_coe.mp h) (not_lt.mpr ((lt_keyAfter_iff e l).mp hel)) + +/-- In a strictly increasing list, the key at an index below the count of keys +below `w` is itself below `w` in `WithTop Key`. -/ +theorem getElem_lt_of_lt_countP_top {l : List Key} (hs : l.SortedLT) {w : WithTop Key} + {i : Nat} (hi : i < l.length) + (hic : i < l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < w))) : + (l[i] : WithTop Key) < w := by + by_contra h + have hle : w ≤ (l[i] : WithTop Key) := le_of_not_gt h + have hzero : (l.drop i).countP (fun (k : Key) => decide ((↑k : WithTop Key) < w)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [List.mem_drop_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + simp only [decide_eq_true_eq] + exact not_lt.mpr (le_trans hle + (WithTop.coe_le_coe.mpr ((hs.getElem_le_getElem_iff).mpr (by omega)))) + have hsplit : l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < w)) = + (l.take i).countP (fun (k : Key) => decide ((↑k : WithTop Key) < w)) + + (l.drop i).countP (fun (k : Key) => decide ((↑k : WithTop Key) < w)) := by + conv_lhs => rw [← List.take_append_drop i l] + rw [List.countP_append] + rw [hzero, Nat.add_zero] at hsplit + have hle' : (l.take i).countP (fun (k : Key) => decide ((↑k : WithTop Key) < w)) ≤ + (l.take i).length := List.countP_le_length + rw [List.length_take, Nat.min_eq_left (le_of_lt hi)] at hle' + omega + +/-! +## The cardinality of a key interval + +`intervalCard b e` is the number of keys in the half-open interval `[b, e)` as an +extended natural. The key space is discrete but unbounded above, so the count is +`⊤` unless the interval is either empty or a finite run of the immediate successors +of its lower end: `[b, b ++ [0]⁰)` is empty, and `[b, b ++ [0]ⁿ)` contains exactly +the `n` keys `b ++ [0]⁰, …, b ++ [0]ⁿ⁻¹`. Every other interval is infinite. + +`[b, e)` is empty when `e ≤ b` and, since `⊤` is above every key, `[b, ⊤)` is +infinite for any genuine `b` while `[⊤, e)` is empty. The successor chain is exactly +the notation the truncated conflict ranges use: `keyAfter b = b ++ [0]`, so +`[b, keyAfterⁿ b)` has `n` keys. +-/ + +/-- The number of keys in the half-open interval `[b, e)`, as an extended natural. +It is `0` when the interval is empty (`e ≤ b`), the length `n` when `e` is `b` +followed by `n` zero bytes, and `⊤` otherwise. -/ +def intervalCard (b e : WithTop Key) : ℕ∞ := + match b, e with + | ⊤, _ => 0 + | _, ⊤ => ⊤ + | (b : Key), (e : Key) => + if e ≤ b then 0 + else if b <+: e ∧ (e.drop b.length).all (fun c => c == (0 : Byte)) then + (e.length - b.length : ℕ) + else ⊤ + +/-- No key lies at or above `⊤`. -/ +theorem intervalCard_top_left (e : WithTop Key) : intervalCard ⊤ e = 0 := by + cases e <;> simp only [intervalCard] + +/-- The interval from a genuine key to `⊤` is infinite. -/ +theorem intervalCard_top_right {b : WithTop Key} (hb : b ≠ ⊤) : + intervalCard b ⊤ = ⊤ := by + cases b with + | top => exact absurd rfl hb + | coe b => simp only [intervalCard] + +/-- For genuine keys, `intervalCard b ⊤` is `⊤`: spelled with both ends coerced to +`WithTop Key`, this is the `intervalCard_top_right` shape the guard uses. -/ +theorem intervalCard_coe_top (b : Key) : + intervalCard (↑b : WithTop Key) (⊤ : WithTop Key) = ⊤ := + intervalCard_top_right (by simp) + +/-- An empty interval has cardinality zero. -/ +theorem intervalCard_eq_zero_of_le {b e : WithTop Key} (h : e ≤ b) : + intervalCard b e = 0 := by + cases b with + | top => exact intervalCard_top_left e + | coe b => + cases e with + | top => exact absurd h (not_le.mpr (WithTop.coe_lt_top b)) + | coe e => + simp only [intervalCard] + rw [ite_eq_left (WithTop.coe_le_coe.mp h)] + +/-- The interval from `b` to `b` followed by `n` zero bytes has exactly `n` keys. -/ +theorem intervalCard_succChain (b : Key) (n : ℕ) : + intervalCard (↑b) (↑(b ++ List.replicate n (0 : Byte))) = (n : ℕ) := by + induction n with + | zero => simpa using intervalCard_eq_zero_of_le (le_refl (↑b : WithTop Key)) + | succ k ih => + rw [List.replicate_succ] + simp only [intervalCard] + have hb_lt : b < b ++ (0 : Byte) :: List.replicate k 0 := by + simpa using List.append_left_lt (l₁ := b) (l₂ := ([] : Key)) + (l₃ := (0 : Byte) :: List.replicate k 0) (List.nil_lt_cons 0 _) + have hlt : ¬ (b ++ (0 : Byte) :: List.replicate k 0) ≤ b := + fun h => absurd (lt_of_lt_of_le hb_lt h) (lt_irrefl b) + rw [ite_eq_right hlt] + have hpre : b <+: b ++ (0 : Byte) :: List.replicate k 0 := List.prefix_append b _ + have hdrop : (b ++ (0 : Byte) :: List.replicate k 0).drop b.length = + (0 : Byte) :: List.replicate k 0 := List.drop_left + have hall : ((b ++ (0 : Byte) :: List.replicate k 0).drop b.length).all + (fun c => c == (0 : Byte)) = true := by + rw [hdrop]; simp + rw [ite_eq_left ⟨hpre, hall⟩] + have hlen : (b ++ (0 : Byte) :: List.replicate k 0).length = b.length + (k + 1) := by + simp [List.length_append, Nat.add_assoc, Nat.add_comm] + rw [hlen, Nat.add_sub_cancel_left] + +/-- For genuine keys, a finite cardinality exactly characterizes the successor +chains: `[b, e)` has `n` keys precisely when `e ≤ b` (and `n = 0`) or `e` is `b` +followed by `n` zero bytes. This says the finite intervals are exactly the +iterated `keyAfter` chains. -/ +theorem intervalCard_eq_coe_iff {b e : Key} {n : ℕ} : + intervalCard (↑b) (↑e) = (n : ℕ) ↔ + (n = 0 ∧ e ≤ b) ∨ e = b ++ List.replicate n (0 : Byte) := by + constructor + · intro h + simp only [intervalCard] at h + by_cases hle : e ≤ b + · rw [ite_eq_left hle] at h + exact Or.inl ⟨(WithTop.coe_eq_coe.mp h).symm, hle⟩ + · rw [ite_eq_right hle] at h + by_cases hc : b <+: e ∧ (e.drop b.length).all (fun c => c == (0 : Byte)) + · rw [ite_eq_left hc] at h + right + have heq : e = b ++ (e.drop b.length) := (List.prefix_iff_eq_append.mp hc.1).symm + have hall : e.drop b.length = List.replicate (e.drop b.length).length (0 : Byte) := + List.eq_replicate_length.mpr fun x hx => + eq_of_beq ((List.all_eq_true.mp hc.2) x hx) + rw [heq, hall] + have hnd : n = (e.drop b.length).length := by + rw [← WithTop.coe_eq_coe.mp h, List.length_drop] + rw [hnd] + · rw [ite_eq_right hc] at h + exact absurd h (by simp) + · rintro (⟨rfl, hle⟩ | heq) + · exact intervalCard_eq_zero_of_le (WithTop.coe_le_coe.mpr hle) + · subst heq + exact intervalCard_succChain b n + +/-- A genuine-coerced interval has cardinality zero exactly when it is empty. -/ +theorem intervalCard_eq_zero_iff {b e : WithTop Key} : + intervalCard b e = 0 ↔ e ≤ b := by + constructor + · intro h + cases b with + | top => exact le_top + | coe b => + cases e with + | top => rw [intervalCard_top_right (by simp)] at h; exact absurd h (by simp) + | coe e => + by_contra hle + rcases (intervalCard_eq_coe_iff (b := b) (e := e) (n := 0)).mp h with + ⟨_, hle'⟩ | heq + · exact hle (WithTop.coe_le_coe.mpr hle') + · rw [List.replicate_zero, List.append_nil] at heq + exact hle (WithTop.coe_le_coe.mpr heq.le) + · exact intervalCard_eq_zero_of_le + +/-- A genuine-coerced interval is infinite exactly when `e` is neither at or below +`b` nor a run of zero bytes appended to `b`. -/ +theorem intervalCard_eq_top_iff {b e : Key} : + intervalCard (↑b) (↑e) = ⊤ ↔ + ¬ e ≤ b ∧ ¬ (b <+: e ∧ (e.drop b.length).all (fun c => c == (0 : Byte))) := by + constructor + · intro h + simp only [intervalCard] at h + by_cases hle : e ≤ b + · rw [ite_eq_left hle] at h; exact absurd h (by simp) + · rw [ite_eq_right hle] at h + by_cases hc : b <+: e ∧ (e.drop b.length).all (fun c => c == (0 : Byte)) + · rw [ite_eq_left hc] at h; exact absurd h (by simp) + · rw [ite_eq_right hc] at h; exact ⟨hle, hc⟩ + · rintro ⟨hle, hc⟩ + simp only [intervalCard] + rw [ite_eq_right hle, ite_eq_right hc] + +/-! +### `intervalCard` counts the keys in the interval + +The three computational cases of `intervalCard` are exactly the cardinalities of the +set `keysIn b e` of genuine keys lying in `[b, e)`. The finite case is the heart of +the argument: the keys of `[b, b ++ [0]ⁿ)` are precisely the iterated successors +`b, keyAfter b, …, keyAfterⁿ⁻¹ b` (`eq_chain_of_lt_chain`), a set of `n` elements. +When the interval is nonempty but its upper end is not such a chain, an injection +`n ↦ b ++ [0]ⁿ` into the interval makes it infinite (`keysIn_coe_coe_infinite`). +-/ + +/-- The iterated successor chain `b, keyAfter b, keyAfter² b, …`. -/ +def chain (b : Key) (n : ℕ) : Key := b ++ List.replicate n (0 : Byte) + +/-- The chain starts at `b`. -/ +theorem chain_zero (b : Key) : chain b 0 = b := by simp [chain] + +/-- Each chain element is the successor of the previous one. -/ +theorem chain_succ (b : Key) (n : ℕ) : chain b (n + 1) = keyAfter (chain b n) := by + simp only [chain, keyAfter, List.replicate_succ'] + rw [← List.append_assoc] + +/-- The chain is injective in its index. -/ +theorem chain_injective (b : Key) : Function.Injective (chain b) := by + intro i j h + have h' : List.replicate i (0 : Byte) = List.replicate j (0 : Byte) := + List.append_cancel_left h + simpa using congrArg List.length h' + +/-- Every chain element is at or above its base. -/ +theorem chain_le (b : Key) (n : ℕ) : b ≤ chain b n := by + rcases n with _ | m + · simp [chain] + · rw [chain] + have h := List.append_left_lt (l₁ := b) (l₂ := ([] : Key)) + (l₃ := List.replicate (m + 1) (0 : Byte)) (by + rw [List.replicate_succ]; exact List.nil_lt_cons _ _) + simpa using le_of_lt h + +/-- The chain is strictly increasing in its index. -/ +theorem chain_lt_mono {b : Key} {i n : ℕ} (h : i < n) : chain b i < chain b n := by + have hle : i ≤ n := le_of_lt h + have hsplit : chain b n = chain b i ++ List.replicate (n - i) (0 : Byte) := by + simp only [chain] + rw [List.append_assoc, List.replicate_append_replicate, Nat.add_sub_of_le hle] + rw [hsplit] + have hlt : ([] : Key) < List.replicate (n - i) (0 : Byte) := by + obtain ⟨m, hm⟩ : ∃ m, n - i = m + 1 := ⟨n - i - 1, by omega⟩ + rw [hm, List.replicate_succ] + exact List.nil_lt_cons _ _ + have h := List.append_left_lt (l₁ := chain b i) (l₂ := ([] : Key)) + (l₃ := List.replicate (n - i) (0 : Byte)) hlt + simpa using h + +/-- Nothing lies strictly between `b` and its successor `keyAfter b`. -/ +theorem eq_of_le_of_lt_keyAfter {b k : Key} (h₁ : b ≤ k) (h₂ : k < keyAfter b) : k = b := + le_antisymm ((lt_keyAfter_iff k b).mp h₂) h₁ + +/-- A key in `(b, chain b n]` is some `chain b m` with `m ≤ n`: the chain absorbs +every key up to its last element. -/ +theorem eq_chain_of_le_chain {b e : Key} (hb : b < e) : + ∀ n, e ≤ chain b n → ∃ m ≤ n, e = chain b m := by + intro n + induction n with + | zero => + intro h + rw [chain_zero] at h + exact absurd (lt_of_lt_of_le hb h) (lt_irrefl b) + | succ k ih => + intro h + rw [chain_succ] at h + rcases le_or_gt e (chain b k) with hle | hgt + · obtain ⟨m, hm, rfl⟩ := ih hle + exact ⟨m, Nat.le_succ_of_le hm, rfl⟩ + · have heq : e = keyAfter (chain b k) := by + rcases lt_or_eq_of_le h with hlt | heq + · exact absurd (eq_of_le_of_lt_keyAfter (le_of_lt hgt) hlt) (ne_of_gt hgt) + · exact heq + exact ⟨k + 1, le_refl _, by rw [chain_succ]; exact heq⟩ + +/-- A key strictly below `chain b n` and above `b` is some `chain b m` with `m < n`. -/ +theorem eq_chain_of_lt_chain {b e : Key} (hb : b < e) : + ∀ n, e < chain b n → ∃ m < n, e = chain b m := by + intro n h + obtain ⟨m, hm, hmk⟩ := eq_chain_of_le_chain hb n (le_of_lt h) + refine ⟨m, lt_of_le_of_ne hm ?_, hmk⟩ + intro hmn + subst hmn + exact absurd hmk (ne_of_lt h) + +/-- If `e` is above `b` and is not any chain element, every chain element is below it. -/ +theorem chain_lt_of_not_chain {b e : Key} (hb : b < e) (hne : ∀ n, e ≠ chain b n) : + ∀ n, chain b n < e := by + intro n + by_contra h + have hle : e ≤ chain b n := le_of_not_gt h + obtain ⟨m, _, hm⟩ := eq_chain_of_le_chain hb n hle + exact hne m hm + +/-- The set of genuine keys lying in the half-open interval `[b, e)`. -/ +def keysIn (b e : WithTop Key) : Set Key := + {k : Key | b ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < e} + +/-- The first `n` elements of the chain from `b`. -/ +def chainSet (b : Key) (n : ℕ) : Set Key := + {k : Key | ∃ i, i < n ∧ k = chain b i} + +/-- In the coerced case, `[b, e)` is the set of keys between `b` and `e`. -/ +theorem keysIn_coe_coe (b e : Key) : + keysIn (↑b : WithTop Key) (↑e : WithTop Key) = {k : Key | b ≤ k ∧ k < e} := by + ext k; simp only [keysIn, Set.mem_ofPred_eq, WithTop.coe_le_coe, WithTop.coe_lt_coe] + +/-- `[⊤, e)` is empty: no key lies at or above `⊤`. -/ +theorem keysIn_top_left (e : WithTop Key) : keysIn ⊤ e = ∅ := by + ext k + simp only [keysIn, Set.mem_ofPred_eq, Set.mem_empty_iff_false, iff_false, not_and] + intro h + exact absurd h (not_le.mpr (WithTop.coe_lt_top k)) + +/-- `[b, ⊤)` is the set of keys at or above `b`. -/ +theorem keysIn_coe_top (b : Key) : + keysIn (↑b : WithTop Key) (⊤ : WithTop Key) = {k : Key | b ≤ k} := by + ext k + simp only [keysIn, Set.mem_ofPred_eq, WithTop.coe_le_coe, WithTop.coe_lt_top, and_true] + +/-- The interval up to a chain element is exactly the chain's first `n` elements. -/ +theorem keysIn_chain (b : Key) (n : ℕ) : + keysIn (↑b) (↑(chain b n)) = chainSet b n := by + ext k + simp only [keysIn, chainSet, Set.mem_ofPred_eq, WithTop.coe_le_coe, WithTop.coe_lt_coe] + constructor + · rintro ⟨hbk, hke⟩ + by_cases hbk' : b = k + · subst hbk' + refine ⟨0, ?_, (chain_zero b).symm⟩ + rcases n with _ | m + · rw [chain_zero] at hke; exact absurd hke (lt_irrefl b) + · exact Nat.succ_pos m + · have hb_lt_k : b < k := lt_of_le_of_ne hbk hbk' + obtain ⟨m, hm, hmk⟩ := eq_chain_of_lt_chain hb_lt_k n hke + exact ⟨m, hm, hmk⟩ + · rintro ⟨i, hi, rfl⟩ + exact ⟨chain_le b i, chain_lt_mono hi⟩ + +/-- A chain of `n` keys has exactly `n` elements. -/ +theorem chainSet_encard (b : Key) (n : ℕ) : (chainSet b n).encard = (n : ℕ∞) := by + have hset : chainSet b n = chain b '' {i : ℕ | i < n} := by + ext k + simp only [chainSet, Set.mem_ofPred_eq, Set.mem_image] + constructor + · rintro ⟨i, hi, rfl⟩; exact ⟨i, hi, rfl⟩ + · rintro ⟨i, hi, rfl⟩; exact ⟨i, hi, rfl⟩ + rw [hset, (chain_injective b).encard_image, Set.Nat.encard_range] + +/-- `[b, ⊤)` is infinite: the chain from `b` injects into it. -/ +theorem keysIn_coe_top_infinite (b : Key) : (keysIn (↑b : WithTop Key) ⊤).Infinite := by + rw [keysIn_coe_top] + exact Set.infinite_of_injective_forall_mem (chain_injective b) (fun n => chain_le b n) + +/-- `[b, e)` is infinite when `e` is above `b` but not a chain element: the chain +from `b` injects into it, staying below `e`. -/ +theorem keysIn_coe_coe_infinite {b e : Key} (hb : b < e) (hne : ∀ n, e ≠ chain b n) : + (keysIn (↑b : WithTop Key) (↑e : WithTop Key)).Infinite := by + rw [keysIn_coe_coe] + exact Set.infinite_of_injective_forall_mem (chain_injective b) + (fun n => ⟨chain_le b n, chain_lt_of_not_chain hb hne n⟩) + +/-- A prefix of zero bytes is exactly a chain endpoint: if `b` is a prefix of `e` +whose remainder is all zeros, then `e` is `chain b n` for `n` the length excess. -/ +theorem eq_chain_of_prefix_zeros {b e : Key} (hpre : b <+: e) + (hall : (e.drop b.length).all (· == (0 : Byte)) = true) : + e = chain b (e.length - b.length) := by + have hdrop : e.drop b.length = List.replicate (e.length - b.length) (0 : Byte) := by + have h : e.drop b.length = + List.replicate (e.drop b.length).length (0 : Byte) := + List.eq_replicate_length.mpr fun x hx => + eq_of_beq ((List.all_eq_true.mp hall) x hx) + rwa [List.length_drop] at h + have heq : e = b ++ (e.drop b.length) := (List.prefix_iff_eq_append.mp hpre).symm + rw [heq, hdrop] + simp [chain] + +/-- A byte list is a run of zero bytes exactly when all its entries are zero. -/ +theorem exists_eq_replicate_zero_iff (l : List Byte) : + (∃ m, l = List.replicate m (0 : Byte)) ↔ l.all (· == (0 : Byte)) = true := by + constructor + · rintro ⟨m, rfl⟩; simp + · intro h + exact ⟨l.length, List.eq_replicate_length.mpr fun b hb => + eq_of_beq ((List.all_eq_true.mp h) b hb)⟩ + +/-- **Interpretation.** `intervalCard b e` is the cardinality of the set of genuine +keys lying in the half-open interval `[b, e)`. -/ +theorem intervalCard_eq_encard_keysIn (b e : WithTop Key) : + intervalCard b e = (keysIn b e).encard := by + cases b with + | top => rw [intervalCard_top_left, keysIn_top_left, Set.encard_empty] + | coe b => + cases e with + | top => rw [intervalCard_top_right (by simp), (keysIn_coe_top_infinite b).encard_eq] + | coe e => + by_cases hle : e ≤ b + · simp only [intervalCard] + rw [ite_eq_left hle] + have hempty : keysIn (↑b : WithTop Key) (↑e : WithTop Key) = ∅ := by + ext k + simp only [keysIn, Set.mem_ofPred_eq, Set.mem_empty_iff_false, iff_false, + not_and, WithTop.coe_le_coe, WithTop.coe_lt_coe] + intro hbk + exact not_lt.mpr (le_trans hle hbk) + rw [hempty, Set.encard_empty] + · simp only [intervalCard] + rw [ite_eq_right hle] + by_cases hc : b <+: e ∧ (e.drop b.length).all (fun c => c == (0 : Byte)) = true + · rw [ite_eq_left hc] + have hchain := eq_chain_of_prefix_zeros hc.1 hc.2 + have hset : keysIn (↑b : WithTop Key) (↑e : WithTop Key) = + chainSet b (e.length - b.length) := by + rw [hchain] + have hlen : (chain b (e.length - b.length)).length - b.length = + e.length - b.length := by + simp [chain, List.length_append] + rw [hlen] + exact keysIn_chain b _ + rw [hset, chainSet_encard] + · rw [ite_eq_right hc] + have hne : ∀ n, e ≠ chain b n := by + intro n hn + apply hc + exact ⟨by rw [hn, chain]; exact List.prefix_append b _, by + rw [hn, chain, List.drop_left]; simp⟩ + have hb : b < e := lt_of_not_ge hle + rw [(keysIn_coe_coe_infinite hb hne).encard_eq] + +/-- **Additivity over a subrange.** When `m` lies in `[b, e]`, the keys of +`[b, e)` are exactly those of `[b, m)` together with those of `[m, e)`, and the +two subintervals are disjoint, so their cardinalities add: +`intervalCard b e = intervalCard b m + intervalCard m e`. The hypothesis that `m` +sits between `b` and `e` is essential: for `m` above `e` the interval `[b, m)` +already contains keys at or above `e`, which `[b, e)` excludes. -/ +theorem intervalCard_add {b m e : WithTop Key} (hb : b ≤ m) (hm : m ≤ e) : + intervalCard b e = intervalCard b m + intervalCard m e := by + rw [intervalCard_eq_encard_keysIn, intervalCard_eq_encard_keysIn, + intervalCard_eq_encard_keysIn] + have hdisj : Disjoint (keysIn b m) (keysIn m e) := by + rw [Set.disjoint_left] + rintro k ⟨-, hk2⟩ ⟨hmk, -⟩ + exact absurd hk2 (not_lt.mpr hmk) + have hunion : keysIn b e = keysIn b m ∪ keysIn m e := by + ext k + simp only [keysIn, Set.mem_ofPred_eq, Set.mem_union] + constructor + · rintro ⟨hbk, hke⟩ + by_cases hkm : (↑k : WithTop Key) < m + · exact Or.inl ⟨hbk, hkm⟩ + · exact Or.inr ⟨le_of_not_gt hkm, hke⟩ + · rintro (⟨hbk, hkm⟩ | ⟨hmk, hke⟩) + · exact ⟨hbk, lt_of_lt_of_le hkm hm⟩ + · exact ⟨le_trans hb hmk, hke⟩ + rw [hunion, Set.encard_union_eq hdisj] + +/-! +## Counting and indexing keys + +These are the generic sorted-list tools shared by the selector-conflict-range +arguments: counting the keys of a sorted list that lie strictly below a bound, +and bounding the key at an index by the successor chain of the count. They +mention only `List Key` and `Key`, so they live here beside the `chain` API. +-/ + +/-- A list whose keys all lie at or above `k₀` has nothing counted strictly +below `k₀`. -/ +theorem countP_lt_eq_zero_of_forall_ge {l : List Key} {k₀ : Key} (h : ∀ x ∈ l, k₀ ≤ x) : + l.countP (fun k => decide (k < k₀)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simpa using not_lt.mpr (h x hx) + +/-- A list whose keys all lie strictly below `k₀` has all of them counted +strictly below `k₀`. -/ +theorem countP_lt_eq_length_of_forall_lt {l : List Key} {k₀ : Key} (h : ∀ x ∈ l, x < k₀) : + l.countP (fun k => decide (k < k₀)) = l.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact h x hx + +/-- In a strictly increasing list, every index at or past the count of keys +strictly below `k₀` holds a key at or above `k₀`. -/ +theorem le_getElem_of_countP_le {l : List Key} (hs : l.SortedLT) {k₀ : Key} {i : Nat} + (hci : l.countP (fun k => decide (k < k₀)) ≤ i) (hi : i < l.length) : k₀ ≤ l[i] := by + by_contra hcon + exact absurd (lt_countP_of_getElem_lt hs hi (not_le.mp hcon)) (not_lt.mpr hci) + +/-- In a strictly increasing list, the `j`-fold successor chain from the key at +`i` stays at or below the key at `i + j`. -/ +theorem chain_getElem_le {l : List Key} (hs : l.SortedLT) {i j : Nat} (hi : i < l.length) + (hij : i + j < l.length) : chain l[i] j ≤ l[i + j] := by + induction j with + | zero => simp [chain_zero] + | succ j ih => + have hij' : i + j < l.length := by omega + have hle : chain l[i] j ≤ l[i + j] := ih hij' + have hstep : l[i + j] < l[i + (j + 1)] := + (hs.getElem_lt_getElem_iff).mpr (by omega) + have hkey : (keyAfter (chain l[i] j) : WithTop Key) ≤ (↑(l[i + (j + 1)]) : WithTop Key) := + keyAfter_le_of_lt (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hle hstep)) + rw [chain_succ] + exact WithTop.coe_le_coe.mp hkey + +/-- In a strictly increasing list, the key `m` chain steps past the count of keys +strictly below `k₀` is at or above `chain k₀ m`. -/ +theorem chain_le_getElem_countP {l : List Key} (hs : l.SortedLT) {k₀ : Key} {m : Nat} + (hm : l.countP (fun k => decide (k < k₀)) + m < l.length) : + chain k₀ m ≤ l[l.countP (fun k => decide (k < k₀)) + m] := by + induction m with + | zero => + have hm' : l.countP (fun k => decide (k < k₀)) < l.length := by omega + have h := le_getElem_of_countP_le hs (le_refl _) hm' + simpa [chain_zero] using h + | succ m ih => + have hm' : l.countP (fun k => decide (k < k₀)) + m < l.length := by omega + have hle : chain k₀ m ≤ l[l.countP (fun k => decide (k < k₀)) + m] := ih hm' + have hstep : l[l.countP (fun k => decide (k < k₀)) + m] < + l[l.countP (fun k => decide (k < k₀)) + (m + 1)] := + (hs.getElem_lt_getElem_iff).mpr (by omega) + have hkey : (keyAfter (chain k₀ m) : WithTop Key) ≤ + (↑(l[l.countP (fun k => decide (k < k₀)) + (m + 1)]) : WithTop Key) := + keyAfter_le_of_lt (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hle hstep)) + rw [chain_succ] + exact WithTop.coe_le_coe.mp hkey + +/-- The successor chain is monotone in its index. -/ +theorem chain_le_chain {b : Key} {i j : ℕ} (h : i ≤ j) : chain b i ≤ chain b j := by + rcases eq_or_lt_of_le h with rfl | hlt + · exact le_refl _ + · exact le_of_lt (chain_lt_mono hlt) + +/-- The empty key is at or below every key. -/ +theorem nil_le_key (k : Key) : ([] : Key) ≤ k := by + rcases k with - | ⟨a, t⟩ + · exact le_refl _ + · exact le_of_lt (List.nil_lt_cons a t) + +/-! +### Sortedness of the successor-chain lists + +The successor chain in index order is strictly increasing, and appending a key +above the whole chain preserves that. These build the witness databases of the +footprint specification. +-/ + +/-- The successor chain from a base, in index order, is strictly increasing. -/ +theorem sortedLT_map_chain (b : Key) (n : ℕ) : ((List.range n).map (chain b)).SortedLT := by + rw [List.sortedLT_iff_pairwise, List.pairwise_iff_getElem] + intro i j hi hj hij + simp only [List.getElem_map, List.getElem_range] + exact chain_lt_mono hij + +/-- The successors of a base, in index order, are strictly increasing. -/ +theorem sortedLT_map_chain_succ (b : Key) (n : ℕ) : + ((List.range n).map (fun i => chain b (i + 1))).SortedLT := by + rw [List.sortedLT_iff_pairwise, List.pairwise_iff_getElem] + intro i j hi hj hij + simp only [List.getElem_map, List.getElem_range] + exact chain_lt_mono (by omega) + +/-- A one-key list is strictly increasing. -/ +theorem sortedLT_singleton (k : Key) : [k].SortedLT := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + +/-- A key followed by its own successors is strictly increasing. -/ +theorem sortedLT_cons_map_chain_succ (b : Key) (n : ℕ) : + (b :: (List.range n).map (fun i => chain b (i + 1))).SortedLT := by + rw [List.sortedLT_cons] + refine ⟨?_, sortedLT_map_chain_succ b n⟩ + intro x hx + rw [List.mem_map] at hx + obtain ⟨i, -, rfl⟩ := hx + have h : chain b 0 < chain b (i + 1) := chain_lt_mono (Nat.succ_pos i) + simpa [chain_zero] using h + +/-- The successors of `k₀` followed by a key strictly above them all is strictly +increasing. -/ +theorem sortedLT_map_chain_append {k₀ k : Key} {m : ℕ} (h : ∀ i < m, chain k₀ i < k) : + ((List.range m).map (chain k₀) ++ [k]).SortedLT := by + rw [List.sortedLT_append] + refine ⟨sortedLT_map_chain k₀ m, sortedLT_singleton k, ?_⟩ + intro a ha b hb + rw [List.mem_map] at ha + obtain ⟨i, hi, rfl⟩ := ha + rw [List.mem_singleton] at hb + subst hb + exact h i (List.mem_range.mp hi) + +/-- The successors of `k₀` followed by a key at or above `k₀` has nothing +counted strictly below `k₀`. -/ +theorem countP_lt_map_chain_append {k₀ k : Key} {m : ℕ} (h : k₀ ≤ k) : + ((List.range m).map (chain k₀) ++ [k]).countP (fun x => decide (x < k₀)) = 0 := + countP_lt_eq_zero_of_forall_ge (fun x hx => by + rw [List.mem_append, List.mem_singleton] at hx + rcases hx with hx | rfl + · rw [List.mem_map] at hx + obtain ⟨i, -, rfl⟩ := hx + exact chain_le k₀ i + · exact h) + +/-- A key below `k₀` followed by successors that all stay below `k₀` has all of +its keys counted strictly below `k₀`. -/ +theorem countP_lt_cons_map_chain_succ {k k₀ : Key} {n : ℕ} + (hk : k < k₀) (h : ∀ i < n, chain k (i + 1) < k₀) : + (k :: (List.range n).map (fun i => chain k (i + 1))).countP + (fun x => decide (x < k₀)) = + (k :: (List.range n).map (fun i => chain k (i + 1))).length := + countP_lt_eq_length_of_forall_lt (fun x hx => by + rw [List.mem_cons, List.mem_map] at hx + rcases hx with rfl | ⟨i, hi, rfl⟩ + · exact hk + · exact h i (List.mem_range.mp hi)) + +/-- Delete every stored key in `[a, b)` and insert the successor chain +`chain a 0, …, chain a (n-1)` in its place, preserving sortedness. This is the +interval analogue of `insert`: it replaces a whole interval of keys by a chosen +run of successors rather than toggling a single key. The keys below `a` and at or +above `b` are kept verbatim, and `hn` certifies that the inserted chain stays +strictly inside `[a, b)`. -/ +def DB.insertInterval (db : DB) (a b : Key) (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) : DB where + keys := (db.keys.filter (fun x => decide (x < a)) ++ + (List.range n).map (chain a)) ++ + db.keys.filter (fun x => decide (b ≤ x)) + sorted := by + have hP_sorted : (db.keys.filter (fun x => decide (x < a))).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hrest_sorted : ((List.range n).map (chain a)).SortedLT := sortedLT_map_chain a n + have hQ_sorted : (db.keys.filter (fun x => decide (b ≤ x))).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hcross : ∀ x ∈ db.keys.filter (fun x => decide (x < a)), + ∀ y ∈ (List.range n).map (chain a), x < y := by + intro x hx y hy + have hxa : x < a := of_decide_eq_true (List.mem_filter.mp hx).2 + rw [List.mem_map] at hy + obtain ⟨i, -, rfl⟩ := hy + exact lt_of_lt_of_le hxa (chain_le a i) + have hPrest_sorted : ((db.keys.filter (fun x => decide (x < a)) ++ + (List.range n).map (chain a))).SortedLT := + List.sortedLT_append.mpr ⟨hP_sorted, hrest_sorted, hcross⟩ + have hcross2 : ∀ x ∈ db.keys.filter (fun x => decide (x < a)) ++ + (List.range n).map (chain a), + ∀ y ∈ db.keys.filter (fun x => decide (b ≤ x)), x < y := by + intro x hx y hy + have hyb : b ≤ y := of_decide_eq_true (List.mem_filter.mp hy).2 + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact lt_of_lt_of_le + (lt_of_lt_of_le (of_decide_eq_true (List.mem_filter.mp hx).2) hab) hyb + · rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact lt_of_lt_of_le (hn i (List.mem_range.mp hi)) hyb + exact List.sortedLT_append.mpr ⟨hPrest_sorted, hQ_sorted, hcross2⟩ + +/-- The keys of an interval-inserted database: the keys below `a`, the inserted +successor chain, then the keys at or above `b`. -/ +@[simp] theorem DB.insertInterval_keys (db : DB) (a b : Key) (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) : + (db.insertInterval a b hab n hn).keys = + (db.keys.filter (fun x => decide (x < a)) ++ (List.range n).map (chain a)) ++ + db.keys.filter (fun x => decide (b ≤ x)) := rfl + +/-- Inserting the successor chain of `[a, b)` leaves the count of keys strictly +below `a` unchanged: those keys are exactly the stored keys below `a`, and each +precedes the whole chain. This is the interval analogue of `countP_insertIdx` +below `a`. -/ +theorem countP_lt_insertInterval_of_lt {db : DB} {a b x : Key} (hab : a ≤ b) (hxa : x ≤ a) + (n : ℕ) (hn : ∀ i < n, chain a i < b) : + (db.insertInterval a b hab n hn).keys.countP (fun k => decide (k < x)) = + db.keys.countP (fun k => decide (k < x)) := by + rw [DB.insertInterval_keys, List.countP_append, List.countP_append] + have hP : (db.keys.filter (fun k => decide (k < a))).countP (fun k => decide (k < x)) = + db.keys.countP (fun k => decide (k < x)) := by + rw [List.countP_eq_length_filter, List.filter_filter, List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + by_cases hkx : k < x + · have hka : k < a := lt_of_lt_of_le hkx hxa + simp [hkx, hka] + · simp [hkx] + have hrest : ((List.range n).map (chain a)).countP (fun k => decide (k < x)) = 0 := + countP_lt_eq_zero_of_forall_ge fun k hk => by + rw [List.mem_map] at hk + obtain ⟨i, -, rfl⟩ := hk + exact le_trans hxa (chain_le a i) + have hQ : (db.keys.filter (fun k => decide (b ≤ k))).countP (fun k => decide (k < x)) = 0 := + countP_lt_eq_zero_of_forall_ge fun k hk => + le_trans (le_trans hxa hab) (of_decide_eq_true (List.mem_filter.mp hk).2) + rw [hP, hrest, hQ] + omega + +/-- Inserting the successor chain of `[a, b)` makes the count of keys strictly +below `b` exactly the stored keys below `a` plus the `n` chain elements: the +erased `[a, b)` keys and the chain are replaced one-for-the-other, and the stored +keys at or above `b` are not counted. This is the interval analogue of +`countP_insertIdx` at the new key. -/ +theorem countP_lt_insertInterval_self {db : DB} {a b : Key} (hab : a ≤ b) {n : ℕ} + (hn : ∀ i < n, chain a i < b) : + (db.insertInterval a b hab n hn).keys.countP (fun k => decide (k < b)) = + (db.keys.filter (fun k => decide (k < a))).length + n := by + rw [DB.insertInterval_keys, List.countP_append, List.countP_append] + have hP : (db.keys.filter (fun k => decide (k < a))).countP (fun k => decide (k < b)) = + (db.keys.filter (fun k => decide (k < a))).length := + countP_lt_eq_length_of_forall_lt fun k hk => + lt_of_lt_of_le (of_decide_eq_true (List.mem_filter.mp hk).2) hab + have hrest : ((List.range n).map (chain a)).countP (fun k => decide (k < b)) = n := by + rw [countP_lt_eq_length_of_forall_lt (fun k hk => ?_), List.length_map, List.length_range] + rw [List.mem_map] at hk + obtain ⟨i, hi, rfl⟩ := hk + exact hn i (List.mem_range.mp hi) + have hQ : (db.keys.filter (fun k => decide (b ≤ k))).countP (fun k => decide (k < b)) = 0 := + countP_lt_eq_zero_of_forall_ge fun k hk => + of_decide_eq_true (List.mem_filter.mp hk).2 + rw [hP, hrest, hQ] + omega + +/-- Erasing the interval `[a, b)` leaves the count of keys strictly below `a` +unchanged: erased keys all lie at or above `a`, so none is counted. -/ +theorem countP_lt_eraseInterval_of_le {db : DB} {a b : Key} {x : Key} (hxa : x ≤ a) : + (db.eraseInterval (↑a) (↑b)).keys.countP (fun k => decide (k < x)) = + db.keys.countP (fun k => decide (k < x)) := by + rw [DB.eraseInterval_keys, List.countP_eq_length_filter, List.countP_eq_length_filter, + List.filter_filter] + congr 1 + apply List.filter_congr + intro k _ + by_cases hkx : k < x + · have hq : decide (¬ (a ≤ (k : WithTop Key) ∧ (k : WithTop Key) < b)) = true := by + rw [decide_eq_true_eq] + rintro ⟨hak, -⟩ + exact absurd (lt_of_lt_of_le hkx hxa) (not_lt.mpr (WithTop.coe_le_coe.mp hak)) + rw [hq] + simp [hkx] + · simp [hkx] + +/-- Erasing `[a, b)` with `a ≤ b` leaves the count of keys strictly below `b` +at exactly the stored keys below `a`: every erased key lies in `[a, b)`, and the +stored keys at or above `b` are not counted. This is the erase counterpart of +`countP_lt_insertInterval_self`. -/ +theorem countP_lt_eraseInterval_self {db : DB} {a b : Key} (hab : a ≤ b) : + (db.eraseInterval (↑a) (↑b)).keys.countP (fun k => decide (k < b)) = + (db.keys.filter (fun k => decide (k < a))).length := by + simp only [DB.eraseInterval_keys, List.countP_eq_length_filter, List.filter_filter] + congr 1 + apply List.filter_congr + intro k _ + by_cases hka : k < a + · have hkb : k < b := lt_of_lt_of_le hka hab + have hna : ¬ ((a : WithTop Key) ≤ (k : WithTop Key)) := + fun h => not_le.mpr hka (WithTop.coe_le_coe.mp h) + simp [hka, hkb, hna] + · have hak : (a : WithTop Key) ≤ (k : WithTop Key) := by + rw [WithTop.coe_le_coe]; exact le_of_not_gt hka + by_cases hkb : k < b + · have hkb' : (k : WithTop Key) < (b : WithTop Key) := WithTop.coe_lt_coe.mpr hkb + simp [hka, hkb, hak, hkb'] + · simp [hka, hkb] + +/-- Inserting the chain of `[a, b)` and then counting below `b` leaves exactly +the stored keys below `a` plus the chain length: the inserted `n` elements are +the only keys of the rebuilt database in `[a, b)`. This is the count form of the +selector-anchor shift under an interval replacement. -/ +theorem countP_lt_insertInterval_shift {db : DB} {a b : Key} (hab : a ≤ b) {n : ℕ} + (hn : ∀ i < n, chain a i < b) : + (db.insertInterval a b hab n hn).keys.countP (fun k => decide (k < b)) = + (db.eraseInterval (↑a) (↑b)).keys.countP (fun k => decide (k < b)) + n := by + rw [countP_lt_insertInterval_self hab hn, countP_lt_eraseInterval_self hab] + +/-- The `i`-th key of an interval-inserted database, for `i < n`, is exactly the +chain element `chain a i`: it sits at index `i` past the stored keys below `a`. +This pinpoints the inserted chain, the multi-key analogue of a single-key +insertion placing the new key at its rank. -/ +theorem getElem?_insertInterval_chain {db : DB} {a b : Key} (hab : a ≤ b) {n i : ℕ} + (hn : ∀ j < n, chain a j < b) (hi : i < n) : + (db.insertInterval a b hab n hn).keys[ + (db.keys.filter (fun k => decide (k < a))).length + i]? = some (chain a i) := by + rw [DB.insertInterval_keys] + have hlen : ((db.keys.filter (fun k => decide (k < a))) ++ + (List.range n).map (chain a)).length = + (db.keys.filter (fun k => decide (k < a))).length + n := by + rw [List.length_append, List.length_map, List.length_range] + rw [List.getElem?_append_left (by rw [hlen]; omega), + List.getElem?_append_right (by omega), Nat.add_sub_cancel_left, + List.getElem?_map, List.getElem?_range hi] + rfl + +/-- Single-key erasure is interval erasure over `[k, keyAfter k)`: the only key +the interval can contain is `k` itself. This makes `DB.erase` a special case of +`DB.eraseInterval`. -/ +theorem DB.erase_eq_eraseInterval (db : DB) (k : Key) : + db.erase k = db.eraseInterval (↑k) (↑(keyAfter k)) := by + unfold DB.erase DB.eraseInterval + congr 1 + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.erase k) + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · intro x + rw [List.Nodup.mem_erase_iff db.nodup] + simp only [List.mem_filter, decide_eq_true_eq, WithTop.coe_le_coe, WithTop.coe_lt_coe] + constructor + · rintro ⟨hne, hx⟩ + exact ⟨hx, fun hcon => hne (le_antisymm ((lt_keyAfter_iff x k).mp hcon.2) hcon.1)⟩ + · rintro ⟨hx, hcon⟩ + refine ⟨?_, hx⟩ + intro heq + rw [heq] at hcon + exact hcon ⟨le_refl k, keyAfter_self_lt k⟩ + +/-- Single-key insertion is the interval insertion of the one-element chain at +`[k, keyAfter k)`: it erases nothing but `k` (which is unstored) and inserts `k`. +This makes `DB.insert` a special case of `DB.insertInterval`. -/ +theorem DB.insert_eq_insertInterval (db : DB) (k : Key) (hk : k ∉ db.keys) : + db.insert k hk = + db.insertInterval k (keyAfter k) (le_of_lt (keyAfter_self_lt k)) 1 + (by intro i _; interval_cases i; simp only [chain, List.replicate_zero, List.append_nil]; + exact keyAfter_self_lt k) := by + have hkeyAfter_le : ∀ c z : Key, keyAfter c ≤ z ↔ c < z := by + intro c z + constructor + · intro h; exact lt_of_lt_of_le (keyAfter_self_lt c) h + · intro h + by_contra hcon + exact absurd ((lt_keyAfter_iff z c).mp (not_le.mp hcon)) (not_le.mpr h) + have hordered : db.keys.orderedInsert (· ≤ ·) k = + (db.keys.filter (fun x => decide (x < k)) ++ [k]) ++ + db.keys.filter (fun x => decide (keyAfter k ≤ x)) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr <| + DB.pairwise_lt_of_le_of_nodup + ((db.sorted.pairwise.imp fun h => le_of_lt h).orderedInsert k) + ((List.perm_orderedInsert (· ≤ ·) k db.keys).nodup_iff.mpr + (List.nodup_cons.mpr ⟨hk, db.nodup⟩)) + · refine List.sortedLT_append.mpr ⟨?_, List.sortedLT_iff_pairwise.mpr + (db.sorted.pairwise.filter _), ?_⟩ + · refine List.sortedLT_append.mpr ⟨List.sortedLT_iff_pairwise.mpr + (db.sorted.pairwise.filter _), sortedLT_singleton k, ?_⟩ + intro a ha b hb + rw [List.mem_filter] at ha + rw [List.mem_singleton] at hb + rw [hb] + exact (of_decide_eq_true ha.2) + · intro a ha b hb + rw [List.mem_append] at ha + rw [List.mem_filter] at hb + rcases ha with ha | ha + · rw [List.mem_filter] at ha + exact lt_of_lt_of_le (of_decide_eq_true ha.2) + (le_of_lt ((hkeyAfter_le k b).mp (of_decide_eq_true hb.2))) + · rw [List.mem_singleton] at ha + rw [ha] + exact (hkeyAfter_le k b).mp (of_decide_eq_true hb.2) + · intro x + rw [List.mem_orderedInsert (· ≤ ·)] + constructor + · rintro (rfl | hx) + · exact List.mem_append.mpr + (Or.inl (List.mem_append.mpr (Or.inr (List.mem_singleton_self x)))) + · rcases lt_trichotomy x k with hlt | heq | hgt + · exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl + (List.mem_filter.mpr ⟨hx, by rw [decide_eq_true_eq]; exact hlt⟩)))) + · exact absurd (heq ▸ hx) hk + · exact List.mem_append.mpr (Or.inr (List.mem_filter.mpr + ⟨hx, by rw [decide_eq_true_eq]; exact (hkeyAfter_le k x).mpr hgt⟩)) + · intro hxx + rw [List.mem_append, List.mem_append, List.mem_singleton] at hxx + rcases hxx with (hx | rfl) | hx + · exact Or.inr (List.mem_filter.mp hx).1 + · exact Or.inl rfl + · exact Or.inr (List.mem_filter.mp hx).1 + unfold DB.insert DB.insertInterval + congr 1 + rw [hordered] + simp [chain] + +/-! +## How many keys fit below a key + +`intervalCard ⊥ k` bounds the number of stored keys a database can place strictly +below a genuine key `k`, and every number up to it is attainable. The bound is +`intervalCard_eq_encard_keysIn` at the lower end: the stored keys below `k` are a +subset of `keysIn ⊥ k`, whose extended cardinality is `intervalCard ⊥ k`. For the +converse, the successor chain `⊥, [0], [0,0], …` enumerates `[⊥, k)` — it lies +below `k` exactly up to index `intervalCard ⊥ k` (`chain_nil_lt_iff`) — so storing +its first `c` elements realizes any `c` below the bound. This is the same-database +tool for reasoning about selector counts: a selector's key alone does not bound how +many keys can lie below it, and `intervalCard ⊥ k` pins that count exactly. +-/ + +/-- The chain from `b` is strictly increasing in its index, so a larger index means +a larger key. -/ +theorem chain_lt_iff_lt (b : Key) (i n : ℕ) : chain b i < chain b n ↔ i < n := by + constructor + · intro h + by_contra hcon + exact absurd h (not_lt.mpr (chain_le_chain (b := b) (by omega))) + · exact chain_lt_mono + +/-- Every chain element from `b` is strictly below `e` whenever `intervalCard b e` +is infinite. -/ +theorem chain_lt_of_intervalCard_eq_top {b e : Key} + (h : intervalCard (↑b : WithTop Key) (↑e : WithTop Key) = ⊤) (i : ℕ) : + chain b i < e := by + have hbe : b < e := by + by_contra hle + have h0 : intervalCard (↑b : WithTop Key) (↑e : WithTop Key) = 0 := + intervalCard_eq_zero_of_le (WithTop.coe_le_coe.mpr (le_of_not_gt hle)) + rw [h0] at h; exact absurd h (by simp) + have hne : ∀ m, e ≠ chain b m := by + intro m hm + have : intervalCard (↑b : WithTop Key) (↑(chain b m)) = (m : ℕ∞) := by + rw [chain]; exact intervalCard_succChain b m + rw [hm, this] at h + exact absurd h (by simp) + exact chain_lt_of_not_chain hbe hne i + +/-- The chain from `b` lies below `e` exactly up to `intervalCard b e`: the +interval `[b, e)` is enumerated by the successor chain. This is the counting bound +at the heart of the same-database analysis, `chain_nil_lt_iff` being the case +`b = ⊥`. -/ +theorem chain_lt_iff (b e : Key) (i : ℕ) : + chain b i < e ↔ + (i : ℕ∞) < intervalCard (↑b : WithTop Key) (↑e : WithTop Key) := by + by_cases htop : intervalCard (↑b : WithTop Key) (↑e : WithTop Key) = ⊤ + · rw [htop] + exact ⟨fun _ => WithTop.coe_lt_top i, fun _ => chain_lt_of_intervalCard_eq_top htop i⟩ + · obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp htop + have hcoeeq : intervalCard (↑b : WithTop Key) (↑e : WithTop Key) = (n : ℕ∞) := hn.symm + rw [hcoeeq, ENat.natCast_lt_natCast] + have hiff := (intervalCard_eq_coe_iff (b := b) (e := e) (n := n)).mp hcoeeq + rcases hiff with ⟨rfl, hle⟩ | hk + · refine iff_of_false (fun hc => ?_) (by simp) + exact absurd (lt_of_lt_of_le hc hle) (not_lt.mpr (chain_le b i)) + · rw [hk]; exact chain_lt_iff_lt b i n + +/-- The successor chain from the empty key is the run of `n` zero bytes. -/ +theorem chain_nil_eq_replicate (n : ℕ) : + chain ([] : Key) n = List.replicate n (0 : Byte) := by + simp [chain] + +/-- The chain from the empty key lies below `k` exactly up to `intervalCard ⊥ k`: +the interval `[⊥, k)` is enumerated by the successor chain. -/ +theorem chain_nil_lt_iff (k : Key) (i : ℕ) : + chain ([] : Key) i < k ↔ + (i : ℕ∞) < intervalCard (↑([] : Key) : WithTop Key) (↑k : WithTop Key) := by + simpa only [List.nil_append] using chain_lt_iff ([] : Key) k i + +/-- `countP (· < k) = c` says the filtered list has `c` elements. -/ +theorem countP_lt_iff_length_filter (l : List Key) (k : Key) (c : ℕ) : + l.countP (fun x => decide (x < k)) = c ↔ + (l.filter (fun x => decide (x < k))).length = c := by + rw [List.countP_eq_length_filter] + +/-- The possible counts of stored keys strictly below a genuine key `k` are exactly +the natural numbers up to `intervalCard ⊥ k`: a database can store any number of +the keys in `[⊥, k)`, and no database can store more than that interval holds. +`intervalCard ⊥ k` therefore characterizes the possible values of +`countP (· < k)` over all databases. -/ +theorem exists_countP_lt_eq_iff (k : Key) (c : ℕ) : + (∃ db : DB, db.keys.countP (fun x => decide (x < k)) = c) ↔ + (c : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑k : WithTop Key) := by + constructor + · rintro ⟨db, hdb⟩ + have hlen : (db.keys.filter (fun x => decide (x < k))).length = c := + (countP_lt_iff_length_filter db.keys k c).mp hdb + have hnodup : (db.keys.filter (fun x => decide (x < k))).Nodup := db.nodup.filter _ + have hset : ((db.keys.filter (fun x => decide (x < k))).toFinset : Set Key) ⊆ + keysIn (↑([] : Key) : WithTop Key) (↑k : WithTop Key) := by + intro x hx + simp only [Finset.mem_coe, List.mem_toFinset, List.mem_filter] at hx + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key x), WithTop.coe_lt_coe.mpr (by simpa using hx.2)⟩ + have h1 := Set.encard_le_encard hset + rw [Set.encard_coe_eq_coe_finsetCard, List.toFinset_card_of_nodup hnodup, hlen] at h1 + rw [intervalCard_eq_encard_keysIn] + exact h1 + · intro hc + refine ⟨⟨(List.range c).map (chain ([] : Key)), sortedLT_map_chain _ _⟩, ?_⟩ + rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range] + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + rw [chain_nil_lt_iff] + exact lt_of_lt_of_le (by exact_mod_cast List.mem_range.mp hi) hc + +/-- For a list, the set of keys below `ke` splits at `kb ≤ ke` into the keys below +`kb` and those in `[kb, ke)`. -/ +theorem filter_lt_toFinset_union (l : List Key) {kb ke : Key} (h : kb ≤ ke) : + (l.filter (fun x => decide (x < ke))).toFinset = + (l.filter (fun x => decide (x < kb))).toFinset ∪ + (l.filter (fun x => decide (kb ≤ x ∧ x < ke))).toFinset := by + ext x + simp only [List.mem_toFinset, List.mem_filter, decide_eq_true_eq, Finset.mem_union] + constructor + · rintro ⟨hx, hxe⟩ + by_cases hxb : x < kb + · exact Or.inl ⟨hx, hxb⟩ + · exact Or.inr ⟨hx, le_of_not_gt hxb, hxe⟩ + · rintro (⟨hx, hxb⟩ | ⟨hx, hxb, hxe⟩) + · exact ⟨hx, lt_of_lt_of_le hxb h⟩ + · exact ⟨hx, hxe⟩ + +/-- The two pieces of the preceding split are disjoint. -/ +theorem disjoint_filters (l : List Key) (kb ke : Key) : + Disjoint (l.filter (fun x => decide (x < kb))).toFinset + (l.filter (fun x => decide (kb ≤ x ∧ x < ke))).toFinset := by + rw [Finset.disjoint_left] + intro x hx1 hx2 + simp only [List.mem_toFinset, List.mem_filter, decide_eq_true_eq] at hx1 hx2 + exact absurd hx1.2 (not_lt.mpr hx2.2.1) + +/-- Counting keys below `ke` splits at `kb ≤ ke` into those below `kb` and those in +`[kb, ke)`: the counts of a database in the two adjacent intervals add. -/ +theorem countP_lt_eq_add_filter (l : List Key) (hnd : l.Nodup) {kb ke : Key} (h : kb ≤ ke) : + l.countP (fun x => decide (x < ke)) = + l.countP (fun x => decide (x < kb)) + + (l.filter (fun x => decide (kb ≤ x ∧ x < ke))).length := by + simp only [List.countP_eq_length_filter] + rw [← List.toFinset_card_of_nodup (hnd.filter _), + ← List.toFinset_card_of_nodup (hnd.filter _), + ← List.toFinset_card_of_nodup (hnd.filter _), + filter_lt_toFinset_union l h, + Finset.card_union_of_disjoint (disjoint_filters l kb ke)] + +/-- For a key, membership in the coerced interval `[kb, ke)` is exactly +`kb ≤ x ∧ x < ke`. -/ +theorem mem_keysIn_coe {kb ke x : Key} : + x ∈ keysIn (↑kb : WithTop Key) (↑ke : WithTop Key) ↔ kb ≤ x ∧ x < ke := by + simp [keysIn] + +/-- A database's keys in `[kb, ke)` form a subset of `keysIn kb ke`. -/ +theorem filter_toFinset_subset_keysIn (db : DB) (kb ke : Key) : + ((db.keys.filter (fun x => decide (kb ≤ x ∧ x < ke))).toFinset : Set Key) ⊆ + keysIn (↑kb : WithTop Key) (↑ke : WithTop Key) := by + intro x hx + simp only [Finset.mem_coe, List.mem_toFinset, List.mem_filter, decide_eq_true_eq] at hx + exact mem_keysIn_coe.mpr hx.2 + +/-- The concatenation of the chain from `⊥` below `kb` with the chain from `kb` is +strictly increasing, provided the first chain stays below `kb`. -/ +theorem sortedLT_append_map_chain {c d : ℕ} {kb : Key} + (h1 : ∀ i < c, chain ([] : Key) i < kb) : + ((List.range c).map (chain ([] : Key)) ++ (List.range d).map (chain kb)).SortedLT := by + rw [List.sortedLT_append] + refine ⟨sortedLT_map_chain _ _, sortedLT_map_chain _ _, ?_⟩ + intro a ha b hb + rw [List.mem_map] at ha hb + obtain ⟨i, hi, rfl⟩ := ha + obtain ⟨j, -, rfl⟩ := hb + exact lt_of_lt_of_le (h1 i (List.mem_range.mp hi)) (chain_le kb j) + +/-- The same-database joint count lemma: for genuine `kb ≤ ke`, a database can +realize `c` stored keys below `kb` and `c + d` below `ke` exactly when `c` fits in +`[⊥, kb)` and `d` fits in `[kb, ke)`. The gap `d` is bounded by +`intervalCard kb ke`, so the two counts are coupled in one database — the fact the +separate footprints of `kb` and `ke` cannot see. -/ +theorem exists_countP_lt_pair_iff (kb ke : Key) (hkbke : kb ≤ ke) (c d : ℕ) : + (∃ db : DB, db.keys.countP (fun x => decide (x < kb)) = c ∧ + db.keys.countP (fun x => decide (x < ke)) = c + d) ↔ + (c : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑kb : WithTop Key) ∧ + (d : ℕ∞) ≤ intervalCard (↑kb : WithTop Key) (↑ke : WithTop Key) := by + constructor + · rintro ⟨db, hc, hcd⟩ + refine ⟨(exists_countP_lt_eq_iff kb c).mp ⟨db, hc⟩, ?_⟩ + have hsplit := countP_lt_eq_add_filter db.keys db.nodup hkbke + have hd : (db.keys.filter (fun x => decide (kb ≤ x ∧ x < ke))).length = d := by + rw [hc] at hsplit; omega + have hnodup : (db.keys.filter (fun x => decide (kb ≤ x ∧ x < ke))).Nodup := + db.nodup.filter _ + have h1 := Set.encard_le_encard (filter_toFinset_subset_keysIn db kb ke) + rw [Set.encard_coe_eq_coe_finsetCard, List.toFinset_card_of_nodup hnodup, hd] at h1 + rw [intervalCard_eq_encard_keysIn] + exact h1 + · rintro ⟨hc, hd⟩ + have hchainc : ∀ i < c, chain ([] : Key) i < kb := fun i hi => + (chain_nil_lt_iff kb i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hc) + have hkind : ∀ i < d, chain kb i < ke := fun i hi => + (chain_lt_iff kb ke i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hd) + let db : DB := ⟨(List.range c).map (chain ([] : Key)) ++ (List.range d).map (chain kb), + sortedLT_append_map_chain hchainc⟩ + refine ⟨db, ?_, ?_⟩ + · rw [List.countP_append] + have h1 : ((List.range c).map (chain ([] : Key))).countP + (fun x => decide (x < kb)) = c := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range] + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchainc i (List.mem_range.mp hi) + have h2 : ((List.range d).map (chain kb)).countP + (fun x => decide (x < kb)) = 0 := by + rw [countP_lt_eq_zero_of_forall_ge] + intro x hx + rw [List.mem_map] at hx + obtain ⟨i, _, rfl⟩ := hx + exact chain_le kb i + rw [h1, h2, Nat.add_zero] + · rw [List.countP_append] + have h1 : ((List.range c).map (chain ([] : Key))).countP + (fun x => decide (x < ke)) = c := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range] + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact lt_of_lt_of_le (hchainc i (List.mem_range.mp hi)) hkbke + have h2 : ((List.range d).map (chain kb)).countP + (fun x => decide (x < ke)) = d := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range] + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hkind i (List.mem_range.mp hi) + rw [h1, h2] + +/-- Interval perturbation: a database can be rebuilt to agree with a given one +both below `a` and at or above `b` — keeping exactly its stored keys outside +`[a, b)` — while realizing any gap `d` up to `intervalCard a b` in the count of +stored keys below `b`. Only the keys in `[a, b)` are changed, and the realization +is a successor chain from `a`, so the keys below `a` and the keys at or above `b` +are untouched. This is the batch companion of the single-key `erase`/`insert` +perturbations: varying a whole interval by `d` rather than one key, which is what +an agreement on a range disjoint from `[a, b)` permits. The witness is +`DB.insertInterval a b d`, the interval analogue of `DB.insert`. + +When `[a, b)` is infinite (`intervalCard a b = ⊤`), `d` is unbounded, so the count +of stored keys below `b` can be shifted arbitrarily far while agreement on +`[⊥, a)` and on `[b, ⊤)` is preserved. -/ +theorem exists_db_perturb_interval {db : DB} {a b : Key} (hab : a ≤ b) (d : ℕ) + (hd : (d : ℕ∞) ≤ intervalCard (↑a : WithTop Key) (↑b : WithTop Key)) : + ∃ db' : DB, + db'.keys.filter (fun x => decide (x < a)) = db.keys.filter (fun x => decide (x < a)) ∧ + db'.keys.filter (fun x => decide (b ≤ x)) = db.keys.filter (fun x => decide (b ≤ x)) ∧ + db'.keys.countP (fun x => decide (x < b)) = + (db.keys.filter (fun x => decide (x < a))).length + d := by + have hchain : ∀ i < d, chain a i < b := fun i hi => + (chain_lt_iff a b i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hd) + let P : List Key := db.keys.filter (fun x => decide (x < a)) + let rest : List Key := (List.range d).map (chain a) + let Q : List Key := db.keys.filter (fun x => decide (b ≤ x)) + have hkey : (db.insertInterval a b hab d hchain).keys = (P ++ rest) ++ Q := rfl + refine ⟨db.insertInterval a b hab d hchain, ?_, ?_, ?_⟩ + · rw [hkey, List.filter_append, List.filter_append] + have hP' : P.filter (fun x => decide (x < a)) = P := + List.filter_eq_self.mpr fun x hx => (List.mem_filter.mp hx).2 + have hrest' : rest.filter (fun x => decide (x < a)) = [] := + List.filter_eq_nil_iff.mpr fun x hx hcon => by + rw [List.mem_map] at hx + obtain ⟨i, -, rfl⟩ := hx + rw [decide_eq_true_eq] at hcon + exact absurd hcon (not_lt.mpr (chain_le a i)) + have hQ' : Q.filter (fun x => decide (x < a)) = [] := + List.filter_eq_nil_iff.mpr fun x hx hcon => by + rw [decide_eq_true_eq] at hcon + exact absurd hcon (not_lt.mpr (le_trans hab (of_decide_eq_true (List.mem_filter.mp hx).2))) + rw [hP', hrest', hQ', List.append_nil, List.append_nil] + · rw [hkey, List.filter_append, List.filter_append] + have hQ' : Q.filter (fun x => decide (b ≤ x)) = Q := + List.filter_eq_self.mpr fun x hx => (List.mem_filter.mp hx).2 + have hP' : P.filter (fun x => decide (b ≤ x)) = [] := + List.filter_eq_nil_iff.mpr fun x hx hcon => by + rw [decide_eq_true_eq] at hcon + exact absurd (le_trans hab hcon) + (not_le.mpr (of_decide_eq_true (List.mem_filter.mp hx).2)) + have hrest' : rest.filter (fun x => decide (b ≤ x)) = [] := + List.filter_eq_nil_iff.mpr fun x hx hcon => by + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + rw [decide_eq_true_eq] at hcon + exact absurd hcon (not_le.mpr (hchain i (List.mem_range.mp hi))) + rw [hP', hrest', hQ', List.nil_append, List.nil_append] + · rw [hkey, List.countP_append, List.countP_append] + have hPcount : P.countP (fun x => decide (x < b)) = P.length := + countP_lt_eq_length_of_forall_lt fun x hx => + lt_of_lt_of_le (of_decide_eq_true (List.mem_filter.mp hx).2) hab + have hrestcount : rest.countP (fun x => decide (x < b)) = d := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, + List.length_range] + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchain i (List.mem_range.mp hi) + have hQcount : Q.countP (fun x => decide (x < b)) = 0 := + countP_lt_eq_zero_of_forall_ge fun x hx => + of_decide_eq_true (List.mem_filter.mp hx).2 + rw [hPcount, hrestcount, hQcount, Nat.add_zero] + +/-- Interval-emptying perturbation: a database can be rebuilt to agree with a +given one both below `a` and at or above `b` while storing nothing in `[a, b)`. +The keys outside `[a, b)` are kept verbatim, so this removes exactly the stored +keys in `[a, b)`; it is the `d = 0` companion of `exists_db_perturb_interval`, +whose rebuilt database agrees on the same two ranges but may fill `[a, b)` with a +successor chain. The witness is `DB.eraseInterval a b`, the interval analogue of +`DB.erase`, so no ordering hypothesis on `a` and `b` is needed. -/ +theorem exists_db_empty_interval {db : DB} {a b : Key} : + ∃ db' : DB, + db'.keys.filter (fun x => decide (x < a)) = db.keys.filter (fun x => decide (x < a)) ∧ + db'.keys.filter (fun x => decide (b ≤ x)) = db.keys.filter (fun x => decide (b ≤ x)) ∧ + db'.keys.filter (fun x => decide (a ≤ x ∧ x < b)) = [] := by + refine ⟨db.eraseInterval (↑a) (↑b), ?_, ?_, ?_⟩ + · rw [DB.eraseInterval_keys, List.filter_filter] + apply List.filter_congr + intro x _ + by_cases h : x < a <;> simp [h] + · rw [DB.eraseInterval_keys, List.filter_filter] + apply List.filter_congr + intro x _ + by_cases h : b ≤ x <;> simp [h] + · rw [DB.eraseInterval_keys, List.filter_filter] + rw [List.filter_eq_nil_iff] + intro x _ hcon + simp only [Bool.and_eq_true, decide_eq_true_eq] at hcon + exact hcon.2 ⟨WithTop.coe_le_coe.mpr hcon.1.1, WithTop.coe_lt_coe.mpr hcon.1.2⟩ + +end FormalConflictRange diff --git a/FormalConflictRange/ClosedForm.lean b/FormalConflictRange/ClosedForm.lean new file mode 100644 index 0000000..78c97e3 --- /dev/null +++ b/FormalConflictRange/ClosedForm.lean @@ -0,0 +1,366 @@ +/- +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.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 diff --git a/FormalConflictRange/ClosedFormAll.lean b/FormalConflictRange/ClosedFormAll.lean new file mode 100644 index 0000000..8f5b463 --- /dev/null +++ b/FormalConflictRange/ClosedFormAll.lean @@ -0,0 +1,1304 @@ +/- +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.CrossedTruncatedGeneral +import FormalConflictRange.CrossedRevGeneral +import FormalConflictRange.CrossedResidual +import FormalConflictRange.CrossedResidualCond +import FormalConflictRange.Residual +import FormalConflictRange.CrossFreeResidual +import FormalConflictRange.ClosedFormTotal +import FormalConflictRange.NonposClosedForm +import FormalConflictRange.NonposRevClosedForm +import FormalConflictRange.NegCrossClosedForm +import FormalConflictRange.NegCrossComplete +import FormalConflictRange.NegCrossBottom +import FormalConflictRange.NegCrossClampNonpos +import FormalConflictRange.NegCrossClampLongNonpos +import FormalConflictRange.NegCrossCompleteResidual +import FormalConflictRange.NegCrossCompleteHigh +import FormalConflictRange.NegCrossHighClamp +import FormalConflictRange.NegCrossResidual +import FormalConflictRange.NegCrossLongNonpos +import FormalConflictRange.NegCrossTruncNonclamp +import FormalConflictRange.NegCrossTruncClamp +import FormalConflictRange.NegCrossTruncRevTop +import FormalConflictRange.NegCrossTruncRevFinite +import FormalConflictRange.NegCrossTruncRevBeginTop +import FormalConflictRange.NegCrossTruncRevBeginTopGeTwo +import FormalConflictRange.NegCrossTruncRevBelowEnd +import FormalConflictRange.NegCrossTruncRevNonpos +import FormalConflictRange.NegCrossTruncRevCrossedSmall +import FormalConflictRange.NegCrossTruncRevCrossedLarge +import FormalConflictRange.NegCrossTruncRevCrossedNonpos +import FormalConflictRange.NegCrossTruncRevResidual +import FormalConflictRange.NegCrossTruncRevResidualLargeTop +import FormalConflictRange.NegCrossTruncRevResidualLargeSentinel +import FormalConflictRange.NegCrossTruncRevResidualFinite +import FormalConflictRange.NegCrossTruncRevResidualFiniteOver +import FormalConflictRange.ZeroCrossLong +import FormalConflictRange.ZeroCrossTrunc +import FormalConflictRange.ComputableAlgorithm + +/-! +# A total result-aware closed form + +This module assembles the compact closed forms proved in the preceding modules into a +single total rule, `conflictRangeClosedForm`. At positive begin offsets it covers + +* complete results in either traversal: the cross-free rule `closedFormCompleteCrossFree` + (forward) and its flip `closedFormCompleteCrossFreeRev` (reversed), and the crossed + rule `completeCrossedGeneral`; +* forward truncated results: the cross-free rule `closedFormTruncatedGeneral` and the + crossed rule `truncatedForwardCrossedGeneral`. + +Beyond begin offset one, the crossed reversed truncated finite branch +`t + (begin.offset - 1) = (end.offset - 1)` is inlined as the compact rule +`[end_.key, keyAfter (head reported))`, and the complementary crossed residual uses +`closedFormCrossedResidual`: it is `[begin.key, ⊤)` when the capacity condition +`crossedResidualCond` fails (`minimal_crossedResidual_not_cond`) and `[end_.key, ⊤)` +when it holds (`minimal_crossedResidual_cond`). The cross-free residual at +`begin.offset ≥ 2` with a reported key at or above `end.key` is +`closedFormCrossFreeResidual`, which is the compact truncated range on the finite +branch and the top range otherwise. No nonempty positive-offset cell falls back to the +exact executable algorithm. + +At nonpositive begin offsets it uses the cross-free complete rule +`closedFormCompleteNonposCrossFree`, the forward truncated rules +`closedFormNonposFwdTrunc` (`end.offset ≥ 1`) and `closedFormNonposFwdTruncNonpos` +(`end.offset ≤ 0`), the reversed truncated rule `closedFormNonposRevTrunc`, and the +crossed complete rules at begin offset zero `closedFormZeroCrossOvershoot`, +`closedFormZeroCrossEndOne`, and `closedFormZeroCrossEndOneLong`, and the crossed +complete overshoot at negative begin offsets `closedFormNegCrossOvershoot` (full range +when the reported keys below `begin.key` fit the begin slack, the top range from the +reported head otherwise). The residual nonpositive crossed cells delegate to the exact +executable `simpleConflictRange`. + +The resulting definition is total and minimal on every `Readable` pair +(`minimal_conflictRangeClosedForm_total`), and denotes the same key set as both +`conflictRangeGeneral` and the executable algorithm. +-/ + +open FormalConflictRange + +set_option maxHeartbeats 1200000 + +namespace FormalConflictRange + +/-- The reversed-traversal cross-free complete-result closed form, obtained by flipping +to the forward rule. -/ +noncomputable def closedFormCompleteCrossFreeRev (r : RangeRead) (res : Result) : + ConflictRange := + closedFormCompleteCrossFree { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- The reversed cross-free complete rule is minimal at every positive begin offset. -/ +theorem minimal_closedForm_of_complete_crossFree_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : + Minimal r res (closedFormCompleteCrossFreeRev r res) := by + 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' (closedFormCompleteCrossFree r' res') := + minimal_closedForm_of_complete_crossFree hread' rfl rfl hne' hbegin hcross + have hflip := (minimal_flip (r := r) (res := res) + (cr := closedFormCompleteCrossFree r' res') (b := true) hrev hmore) + exact hflip.mp hmin' + +/-- The reversed cross-free complete rule denotes the general conflict range. -/ +theorem closedFormCompleteCrossFreeRev_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : + (closedFormCompleteCrossFreeRev r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_closedForm_of_complete_crossFree_rev hread hmore hrev hne hbegin hcross).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The same identification with the executable algorithm. -/ +theorem closedFormCompleteCrossFreeRev_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : + (closedFormCompleteCrossFreeRev r res).toSet = (simpleConflictRange r res).toSet := + (minimal_closedForm_of_complete_crossFree_rev hread hmore hrev hne hbegin hcross).toSet_eq + (simpleConflictRange_minimal hread) + +/-- The crossed reversed truncated residual: when the capacity condition fails, the +minimal range is `[begin.key, ⊤)`; when it holds, the minimal range is +`[end_.key, ⊤)`. -/ +noncomputable def closedFormCrossedResidual (r : RangeRead) (res : Result) : ConflictRange := by + classical + exact if h : res.keys = [] then emptyValue r + else if crossedResidualCond r res h then ⟨r.end_.key, ⊤⟩ + else ⟨r.begin.key, ⊤⟩ + +/-- The crossed nonpositive-begin complete overshoot closed form at begin offset zero. -/ +noncomputable def closedFormZeroCrossOvershoot (r : RangeRead) (res : Result) : ConflictRange := + topOfLast (leastReported r res) + +/-- The crossed nonpositive-begin complete under-end closed form at begin offset zero, +`end.offset = 1` and `begin.key ≤ end_.key`. -/ +noncomputable def closedFormZeroCrossEndOne (r : RangeRead) (res : Result) : ConflictRange := + ⟨((leastReported r res : Key) : WithTop Key), r.end_.key⟩ + +/-- The crossed nonpositive-begin complete long under-end closed form at begin offset zero, +`end.offset = 1` and `end_.key < begin.key`. -/ +noncomputable def closedFormZeroCrossEndOneLong (r : RangeRead) (res : Result) : ConflictRange := + ⟨((leastReported r res : Key) : WithTop Key), r.begin.key⟩ + +/-- The crossed nonpositive-begin complete under-end closed form at begin offset zero and +`end.offset ≤ 0`. -/ +noncomputable def closedFormZeroCrossEndNonpos (r : RangeRead) (res : Result) : ConflictRange := + ⟨((leastReported r res : Key) : WithTop Key), r.end_.key⟩ + +/-- The crossed complete begin-zero under-end closed form at `end.offset ≥ 2` and +`begin.key ≤ end.key`, via the offset-one anchor at the reported head. -/ +noncomputable def closedFormZeroCrossUnderEnd (r : RangeRead) (res : Result) : ConflictRange := + closedFormOffsetOneTotal (zeroBeginAnchor r res) res + +/-- **The crossed complete begin-zero under-end rule with `end.offset ≥ 2` and +`begin.key ≤ end.key` is minimal.** The crossed read and its offset-one anchor have +the same compatible databases, so the offset-one closed form answers it. -/ +theorem minimal_closedForm_of_zeroCross_underEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossUnderEnd r res) : + Minimal r res (closedFormZeroCrossUnderEnd r res) := by + obtain ⟨hbegin, hmore, hrev, hend2, hne, hcrossD, hBE⟩ := h + have hleast : leastReported r res = res.keys.head hne := by + simp only [leastReported, hrev] + 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 + rw [← hleast]; exact hcrossD + let r' := zeroBeginAnchor r res + have hiff := compatible_zero_crossed_iff_anchor hread hrev hmore hne hbegin hcross hBE + (by omega) + have hread' : Readable r' res := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (hiff db).mp hcomp⟩ + have hmin' : Minimal r' res (closedFormOffsetOneTotal r' res) := + minimal_closedForm_of_offset_one_total hread' hne (by simp [r', zeroBeginAnchor]) + exact minimal_of_compatible_iff hiff hmin' + +/-- **The crossed complete begin-zero long-cell rule with `begin.key = ⊤` is minimal.** -/ +theorem minimal_closedForm_of_zeroCross_longTop {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossLongTop r res) : + Minimal r res (closedFormZeroCrossLong r res) := by + obtain ⟨hbegin, hmore, hrev, hend, hne, htop⟩ := h + exact minimal_closedForm_of_zero_cross_long_top hread hrev hmore hne hbegin htop hend + +/-- **The crossed complete begin-zero long-cell rule at finite `begin.key` is +minimal.** -/ +theorem minimal_closedForm_of_zeroCross_longFinite {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossLongFinite r res) : + Minimal r res (closedFormZeroCrossLong r res) := by + obtain ⟨hbegin, hmore, hrev, hend, hne, hB, hlong, hleast⟩ := h + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast' : leastReported r res = res.keys.head hne := by + simp only [leastReported, hrev] + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rw [← hleast']; exact hleast + exact minimal_closedForm_of_zero_cross_long_finite hread hrev hmore hne hbegin hcross + hend hlong hB + +/-- **The crossed nonpositive-begin complete overshoot rule is minimal.** -/ +theorem minimal_closedForm_of_zeroCross_overshoot {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossOvershootComplete r res) : + Minimal r res (closedFormZeroCrossOvershoot r res) := by + obtain ⟨hbegin, hmore, hrev, hov, hcrossD⟩ := h + obtain ⟨hend, hne, hhi⟩ := hov + have hleast : leastReported r res = res.keys.head hne := by + simp only [leastReported, hrev] + 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 + rw [← hleast]; exact hcrossD + unfold closedFormZeroCrossOvershoot + rw [hleast] + exact minimal_topOfHead_of_zero_begin_crossed_overshoot hread hrev hmore hne hbegin hcross + hend hhi + +/-- **The crossed nonpositive-begin complete under-end rule at `end.offset = 1` is +minimal.** -/ +theorem minimal_closedForm_of_zeroCross_endOne {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossEndOne r res) : + Minimal r res (closedFormZeroCrossEndOne r res) := by + obtain ⟨hbegin, hmore, hrev, hend, hne, hcrossD, hkeyle⟩ := h + have hleast : leastReported r res = res.keys.head hne := by + simp only [leastReported, hrev] + 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 + rw [← hleast]; exact hcrossD + unfold closedFormZeroCrossEndOne + rw [hleast] + exact minimal_headToEnd_of_zero_begin_crossed_endOne hread hrev hmore hne hbegin hcross + hend hkeyle + +/-- **The crossed nonpositive-begin complete long under-end rule at `end.offset = 1` is +minimal.** -/ +theorem minimal_closedForm_of_zeroCross_endOne_long {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossEndOneLong r res) : + Minimal r res (closedFormZeroCrossEndOneLong r res) := by + obtain ⟨hbegin, hmore, hrev, hend, hne, hcrossD, hlong⟩ := h + have hleast : leastReported r res = res.keys.head hne := by + simp only [leastReported, hrev] + 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 + rw [← hleast]; exact hcrossD + unfold closedFormZeroCrossEndOneLong + rw [hleast] + exact minimal_headToBeginKey_of_zero_begin_crossed_endOne_long hread hrev hmore hne hbegin + hcross hend hlong + +/-- **The crossed nonpositive-begin complete under-end rule with `end.offset ≤ 0` is +minimal.** -/ +theorem minimal_closedForm_of_zeroCross_endNonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossEndNonpos r res) : + Minimal r res (closedFormZeroCrossEndNonpos r res) := by + obtain ⟨hbegin, hmore, hrev, hend, hne, hcrossD⟩ := h + have hleast : leastReported r res = res.keys.head hne := by + simp only [leastReported, hrev] + 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 + rw [← hleast]; exact hcrossD + unfold closedFormZeroCrossEndNonpos + rw [hleast] + exact minimal_headToEnd_of_zero_begin_crossed_endNonpos hread hrev hmore hne hbegin hcross hend + +/-- The crossed complete begin-zero closed form in a single dispatch, used for the +reversed reduction. -/ +noncomputable def closedFormZeroCrossComplete (r : RangeRead) (res : Result) : + ConflictRange := + if r.end_.offset ≤ 0 then closedFormZeroCrossEndNonpos r res + else if r.end_.offset = 1 then + if r.end_.key < r.begin.key then closedFormZeroCrossEndOneLong r res + else closedFormZeroCrossEndOne r res + else if r.begin.key = (⊤ : WithTop Key) then closedFormZeroCrossLong r res + else if r.end_.key < r.begin.key then closedFormZeroCrossLong r res + else closedFormZeroCrossUnderEnd r res + +/-- **The crossed complete begin-zero forward rule is minimal.** Every crossed +complete forward result at begin offset zero falls into one of the end-offset / +selector-order cells closed above. -/ +theorem minimal_closedForm_of_zeroCross_complete {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hbegin : r.begin.offset = 0) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : + Minimal r res (closedFormZeroCrossComplete r res) := by + unfold closedFormZeroCrossComplete + by_cases hE0 : r.end_.offset ≤ 0 + · rw [ite_eq_left hE0] + exact minimal_closedForm_of_zeroCross_endNonpos hread + ⟨hbegin, hmore, hrev, hE0, hne, hcross⟩ + · rw [ite_eq_right hE0] + by_cases hE1 : r.end_.offset = 1 + · rw [ite_eq_left hE1] + by_cases hlong : r.end_.key < r.begin.key + · rw [ite_eq_left hlong] + exact minimal_closedForm_of_zeroCross_endOne_long hread + ⟨hbegin, hmore, hrev, hE1, hne, hcross, hlong⟩ + · rw [ite_eq_right hlong] + exact minimal_closedForm_of_zeroCross_endOne hread + ⟨hbegin, hmore, hrev, hE1, hne, hcross, not_lt.mp hlong⟩ + · rw [ite_eq_right hE1] + have hE2 : 2 ≤ r.end_.offset := by omega + by_cases htop : r.begin.key = (⊤ : WithTop Key) + · rw [ite_eq_left htop] + exact minimal_closedForm_of_zeroCross_longTop hread + ⟨hbegin, hmore, hrev, hE2, hne, htop⟩ + · rw [ite_eq_right htop] + by_cases hlong : r.end_.key < r.begin.key + · rw [ite_eq_left hlong] + exact minimal_closedForm_of_zeroCross_longFinite hread + ⟨hbegin, hmore, hrev, hE2, hne, htop, hlong, hcross⟩ + · rw [ite_eq_right hlong] + exact minimal_closedForm_of_zeroCross_underEnd hread + ⟨hbegin, hmore, hrev, hE2, hne, hcross, not_lt.mp hlong⟩ + +/-- The reversed-traversal crossed complete begin-zero closed form, obtained by +flipping to the forward rule. -/ +noncomputable def closedFormZeroCrossCompleteRev (r : RangeRead) (res : Result) : + ConflictRange := + closedFormZeroCrossComplete { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- **The reversed crossed complete begin-zero rule is minimal.** -/ +theorem minimal_closedForm_of_zeroCross_complete_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hbegin : r.begin.offset = 0) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hcross : ((leastReported r res : Key) : WithTop Key) < r.begin.key) : + Minimal r res (closedFormZeroCrossCompleteRev r res) := by + 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 hleast : leastReported r' res' = leastReported r res := by + have h1 : leastReported r' res' = res'.keys.headD ([] : Key) := by + simp [leastReported, r'] + have h2 : leastReported r res = res.keys.getLastD ([] : Key) := by + simp [leastReported, hrev] + rw [h1, h2] + simp only [res'] + rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse] + have hcross' : ((leastReported r' res' : Key) : WithTop Key) < r'.begin.key := by + rw [hleast] + exact hcross + have hmin' : Minimal r' res' (closedFormZeroCrossComplete r' res') := + minimal_closedForm_of_zeroCross_complete hread' rfl hbegin rfl hne' hcross' + have hflip := (minimal_flip (r := r) (res := res) + (cr := closedFormZeroCrossComplete r' res') (b := true) hrev hmore) + exact hflip.mp hmin' + +/-- Minimality is invariant under a change of the result that preserves the +compatible databases. -/ +theorem minimal_congr_of_compatible_res {r : RangeRead} {res res' : Result} + {cr : ConflictRange} + (h : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r res') : + Minimal r res cr ↔ Minimal r res' cr := by + have hS : ∀ cr, Sound r res cr ↔ Sound r res' cr := by + intro cr + unfold Sound + constructor <;> intro hs db₁ db₂ hagree + · exact (h db₁).symm.trans ((hs db₁ db₂ hagree).trans (h db₂)) + · exact (h db₁).trans ((hs db₁ db₂ hagree).trans (h db₂).symm) + have hN : ∀ k, Necessary r res k ↔ Necessary r res' k := by + intro k + unfold Necessary + exact ⟨fun hk cr hs => hk cr ((hS cr).mpr hs), + fun hk cr hs => hk cr ((hS cr).mp hs)⟩ + rw [minimal_iff_sound_and_necessary, minimal_iff_sound_and_necessary] + constructor + · rintro ⟨hs, hnec⟩ + exact ⟨(hS cr).mp hs, fun k hk => (hN k).mp (hnec k hk)⟩ + · rintro ⟨hs, hnec⟩ + exact ⟨(hS cr).mpr hs, fun k hk => (hN k).mpr (hnec k hk)⟩ + +/-- A nonempty *truncated reversed* result at begin offset zero that is crossed +(the reported last lies strictly below `begin.key`). On this cell the reported keys +are already the whole window: the reversed complete keys all lie at or above the +reported last, so the `≥ last` filter of the truncated bridge is the identity. The +compatible databases are therefore the same as those of the complete result. -/ +abbrev zeroCrossTruncRevCrossed (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = true ∧ res.more = true ∧ res.keys ≠ [] ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key + +/-- The begin-zero crossed reversed truncated closed form, by dropping the `more` +flag and using the crossed complete begin-zero rule. -/ +noncomputable def closedFormZeroCrossTruncRevCrossed (r : RangeRead) (res : Result) : + ConflictRange := + closedFormZeroCrossCompleteRev r { keys := res.keys, more := false } + +/-- Under the begin-zero crossed reversed truncated hypotheses, the truncated and +complete results have the same compatible databases: the reported keys contain the +reported last, so the reversed truncated bridge forces the whole window to be +reported. -/ +theorem compatible_zeroCrossTruncRevCrossed_iff_complete {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 0) (hmore : res.more = true) + (hne : res.keys ≠ []) + (hcross : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) : + ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db r { keys := res.keys, more := false } := by + intro db + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hB : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hcross + constructor + · intro hcomp + obtain ⟨hlim, heq⟩ := + (compatible_iff_truncated_keys_rev hrev hmore hne).mp hcomp + have hlastmem_res : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + have hlastmem_filter : res.keys.getLast hne ∈ + (RangeRead.completeKeys db r).filter + (fun k : Key => decide (res.keys.getLast hne ≤ k)) := by + rw [heq]; exact hlastmem_res + have hlastmem_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db r := + List.mem_of_mem_filter hlastmem_filter + obtain ⟨hlastdb, hreslo, -⟩ := RangeRead.mem_completeKeys.mp hlastmem_ck + have hres_ge : ((res.keys.getLast hne : Key) : WithTop Key) ≤ resolve db r.begin := + resolve_zero_begin_le_of_mem_below hbegin hlastdb hB + (res.keys.getLast hne) hlastdb hB + have hres_eq : resolve db r.begin = ((res.keys.getLast hne : Key) : WithTop Key) := + le_antisymm hreslo hres_ge + have hfilter_id : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (res.keys.getLast hne ≤ k)) = + RangeRead.completeKeys db r := by + apply List.filter_eq_self.mpr + intro k hk + rw [decide_eq_true_eq] + have hkge := (RangeRead.mem_completeKeys.mp hk).2.1 + rw [hres_eq] at hkge + exact WithTop.coe_le_coe.mp hkge + have hck_eq : res.keys = RangeRead.completeKeys db r := by + rw [hfilter_id] at heq; exact heq.symm + exact ⟨hlim, Or.inr ⟨rfl, hck_eq⟩⟩ + · intro hcomp + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp at hm + · refine ⟨hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + exact heq ▸ List.prefix_refl (res.keys) + +/-- **The crossed begin-zero reversed truncated closed form is minimal.** The +truncated result has the same compatible databases as its complete version, so the +crossed complete begin-zero rule answers it. -/ +theorem minimal_closedForm_of_zeroCross_trunc_revCrossed {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncRevCrossed r res) : + Minimal r res (closedFormZeroCrossTruncRevCrossed r res) := by + obtain ⟨hbegin, hrev, hmore, hne, hcrossD⟩ := h + have hlastD : leastReported r res = res.keys.getLastD ([] : Key) := by + simp [leastReported, hrev] + have hcrossLast : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hcrossD + have hcrossD' : ((leastReported r { keys := res.keys, more := false } : Key) : WithTop Key) < + r.begin.key := by + have hleast' : leastReported r { keys := res.keys, more := false } = + res.keys.getLastD ([] : Key) := by simp [leastReported, hrev] + rw [hleast']; exact hcrossLast + have hread' : Readable r { keys := res.keys, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_zeroCrossTruncRevCrossed_iff_complete hrev hbegin hmore hne + hcrossLast db).mp hcomp⟩ + have hmin' : Minimal r { keys := res.keys, more := false } + (closedFormZeroCrossCompleteRev r { keys := res.keys, more := false }) := + minimal_closedForm_of_zeroCross_complete_rev hread' hrev hbegin rfl hne hcrossD' + exact (minimal_congr_of_compatible_res + (compatible_zeroCrossTruncRevCrossed_iff_complete hrev hbegin hmore hne hcrossLast)).mpr + hmin' + +/-- **The total result-aware closed form at every positive begin offset.** Empty +results use the empty-result value `emptyValue`; every nonempty cell uses a compact +closed form. -/ +noncomputable def conflictRangeClosedForm (r : RangeRead) (res : Result) : ConflictRange := + if res.keys = [] then emptyValue r + else if nonposCompleteCrossFree r res then closedFormCompleteNonposCrossFree r res + else if nonposFwdTrunc r res then closedFormNonposFwdTrunc r res + else if nonposFwdTruncNonpos r res then closedFormNonposFwdTruncNonpos r res + else if nonposRevTrunc r res then closedFormNonposRevTrunc r res + else if zeroCrossOvershootComplete r res then closedFormZeroCrossOvershoot r res + else if zeroCrossEndOne r res then closedFormZeroCrossEndOne r res + else if zeroCrossEndOneLong r res then closedFormZeroCrossEndOneLong r res + else if zeroCrossEndNonpos r res then closedFormZeroCrossEndNonpos r res + else if zeroCrossUnderEnd r res then closedFormZeroCrossUnderEnd r res + else if zeroCrossLongTop r res then closedFormZeroCrossLong r res + else if zeroCrossLongFinite r res then closedFormZeroCrossLong r res + else if zeroCrossCompleteRev r res then closedFormZeroCrossCompleteRev r res + else if negCrossOvershoot r res then closedFormNegCrossOvershoot r res + else if negCrossOvershootRev r res then closedFormNegCrossOvershootRev r res + else if negCrossLowFwd r res then closedFormNegCrossLowFwd r res + else if negCrossLowFwdRev r res then closedFormNegCrossLowFwdRev r res + else if negCrossEndOne r res then closedFormNegCrossEndOne r res + else if negCrossEndOneRev r res then closedFormNegCrossEndOneRev r res + else if negCrossEndOneLong r res then closedFormNegCrossEndOneLong r res + else if negCrossEndOneLongRev r res then closedFormNegCrossEndOneLongRev r res + else if negCrossClampEndOne r res then closedFormNegCrossClampEndOne r res + else if negCrossClampEndOneRev r res then closedFormNegCrossClampEndOneRev r res + else if negCrossHeadBottom r res then closedFormNegCrossHeadBottom r res + else if negCrossHeadBottomRev r res then closedFormNegCrossHeadBottomRev r res + else if negCrossClampNonposInfinite r res then closedFormNegCrossClampNonposInfinite r res + else if negCrossClampNonposInfiniteRev r res then closedFormNegCrossClampNonposInfiniteRev r res + else if r.begin.offset ≤ 0 then + if zeroCrossTruncRevCrossed r res then closedFormZeroCrossTruncRevCrossed r res + else if zeroCrossTruncReadAllEnd r res then closedFormZeroCrossTrunc r res + else if zeroCrossTruncBelowEnd r res then closedFormZeroCrossTrunc r res + else if zeroCrossTruncEndGeHead r res then closedFormZeroCrossTrunc r res + else if zeroCrossTruncLowEndSmall r res then closedFormZeroCrossTrunc r res + else if zeroCrossTruncLowEndBig r res then closedFormZeroCrossTruncLowEnd r res + else if zeroCrossTruncNonposEnd r res then closedFormZeroCrossTruncNonposEnd r res + else if negCrossClampNonposLastGe r res then closedFormNegCrossClampNonposLastGe r res + else if negCrossClampNonposLastGeRev r res then closedFormNegCrossClampNonposLastGeRev r res + else if negCrossClampNonposBottom r res then closedFormNegCrossClampNonposBottom r res + else if negCrossClampNonposBottomRev r res then closedFormNegCrossClampNonposBottomRev r res + else if negCrossNonclampNonpos r res then closedFormNegCrossNonclampNonpos r res + else if negCrossNonclampNonposRev r res then closedFormNegCrossNonclampNonposRev r res + else if negCrossNonclampLongNonpos r res then closedFormNegCrossNonclampLongNonpos r res + else if negCrossNonclampLongNonposRev r res then closedFormNegCrossNonclampLongNonposRev r res + else if negCrossClampLongNonpos r res then closedFormNegCrossClampLongNonpos r res + else if negCrossClampLongNonposRev r res then closedFormNegCrossClampLongNonposRev r res + else if negCrossCompleteHighSmall r res then closedFormNegCrossCompleteHigh r res + else if negCrossCompleteHighClampFinite r res then closedFormNegCrossCompleteHigh r res + else if negCrossCompleteHighRev r res then closedFormNegCrossCompleteHighRev r res + else if negCrossCompleteResidual r res then closedFormNegCrossCompleteResidual r res + else if negCrossCompleteResidualRev r res then closedFormNegCrossCompleteResidualRev r res + else if negCrossTruncNonclampBelowEnd r res then closedFormNegCrossTruncNonclampBelowEnd r res + else if negCrossTruncNonclampEndGeHead r res then closedFormNegCrossTruncNonclampTop r res + else if negCrossTruncNonclampEndOne r res then closedFormNegCrossTruncNonclampEndOne r res + else if negCrossTruncNonclampTop r res then closedFormNegCrossTruncNonclampTop r res + else if negCrossTruncNonclampLowEndSmall r res then closedFormNegCrossTruncNonclampTop r res + else if negCrossTruncNonclampLowEndBig r res then closedFormNegCrossTruncNonclampLowEnd r res + else if negCrossTruncClampGapLow r res then closedFormNegCrossTruncClampGapLow r res + else if negCrossTruncClampGapBotMid r res then closedFormNegCrossTruncClampGapBotMid r res + else if negCrossTruncClampGapHighBot r res then closedFormNegCrossTruncClampGapHighBot r res + else if negCrossTruncClampGapHigh r res then closedFormNegCrossTruncClampGapHigh r res + else if negCrossTruncClampNoGap r res then closedFormNegCrossTruncClampNoGap r res + else if negCrossTruncRevBelowEnd r res then closedFormNegCrossTruncRevBelowEnd r res + else if negCrossTruncRevNonposOvershoot r res then + closedFormNegCrossTruncRevNonposOvershoot r res + else if negCrossTruncRevNonposUnder r res then closedFormNegCrossTruncRevNonposUnder r res + else if negCrossTruncRevCrossedSmall r res then + closedFormNegCrossTruncRevCrossedSmall r res + else if negCrossTruncRevCrossedLarge r res then + closedFormNegCrossTruncRevCrossedLarge r res + else if negCrossTruncRevUnder r res then closedFormNegCrossTruncRevUnder r res + else if negCrossTruncRevBeginTop r res then closedFormNegCrossTruncRevBeginTop r res + else if negCrossTruncRevBeginTopGeTwo r res then + closedFormNegCrossTruncRevBeginTopGeTwo r res + else if negCrossTruncRevTop r res then closedFormNegCrossTruncRevTop r res + else if negCrossTruncRevCrossedNonposSmall r res then + closedFormNegCrossTruncRevCrossedNonposSmall r res + else if negCrossTruncRevCrossedNonposLarge r res then + closedFormNegCrossTruncRevCrossedNonposLarge r res + else if negCrossTruncRevResidualRoom r res then + closedFormNegCrossTruncRevResidualRoom r res + else if negCrossTruncRevResidualTopCrossedSmallGap r res then + closedFormNegCrossTruncRevResidualTopCrossedSmallGap r res + else if negCrossTruncRevResidualTopCrossed r res then + closedFormNegCrossTruncRevResidualTopCrossed r res + else if negCrossTruncRevResidualTopLargeCrossed r res then + closedFormNegCrossTruncRevResidualTopLargeCrossed r res + else if negCrossTruncRevResidualTopLargeSentinel r res then + closedFormNegCrossTruncRevResidualTopLargeSentinel r res + else if negCrossTruncRevResidualFiniteUnderCrossed r res then + closedFormNegCrossTruncRevResidualFiniteUnder r res + else if negCrossTruncRevResidualFiniteUnderNotCrossed r res then + closedFormNegCrossTruncRevResidualFiniteUnder r res + else if negCrossTruncRevResidualFiniteOver r res then + closedFormNegCrossTruncRevResidualFiniteOver r res + else simpleConflictRange r res + else if r.begin.offset = 1 then closedFormOffsetOneTotal r res + else if res.more = false then + if r.end_.key < r.begin.key then completeCrossedGeneral r res + else if r.reverse = true then closedFormCompleteCrossFreeRev r res + else closedFormCompleteCrossFree r res + else + if r.reverse = true then + 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 r.end_.key < r.begin.key ∧ + res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat then + ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ + else if r.end_.key < r.begin.key then closedFormCrossedResidual r res + else closedFormCrossFreeResidual r res + else if r.end_.key < r.begin.key then truncatedForwardCrossedGeneral r res + else closedFormTruncatedGeneral r res + +/-- **The total closed form is minimal for every readable result at every positive begin +offset.** -/ +theorem minimal_conflictRangeClosedForm {r : RangeRead} {res : Result} + (hread : Readable r res) (hbegin : 1 ≤ r.begin.offset) : + Minimal r res (conflictRangeClosedForm r res) := by + classical + unfold conflictRangeClosedForm + 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 hnp : nonposCompleteCrossFree r res + · exact absurd hnp.2.2.1 (by omega) + · rw [ite_eq_right hnp] + by_cases hft : nonposFwdTrunc r res + · exact absurd hft.2.2.1 (by omega) + · rw [ite_eq_right hft] + by_cases hftn : nonposFwdTruncNonpos r res + · exact absurd hftn.2.2.1 (by omega) + · rw [ite_eq_right hftn] + by_cases hrt : nonposRevTrunc r res + · exact absurd hrt.2.2.1 (by omega) + · rw [ite_eq_right hrt] + by_cases hzc : zeroCrossOvershootComplete r res + · exact absurd hzc.1 (by omega) + · rw [ite_eq_right hzc] + by_cases hze : zeroCrossEndOne r res + · exact absurd hze.1 (by omega) + · rw [ite_eq_right hze] + by_cases hzel : zeroCrossEndOneLong r res + · exact absurd hzel.1 (by omega) + · rw [ite_eq_right hzel] + have hzn_false : ¬ zeroCrossEndNonpos r res := by + intro hzn + exact absurd hzn.1 (by omega) + rw [ite_eq_right hzn_false] + have hzu_false : ¬ zeroCrossUnderEnd r res := by + intro hzu + exact absurd hzu.1 (by omega) + rw [ite_eq_right hzu_false] + have hzl_false : ¬ zeroCrossLongTop r res := by + intro hzl + exact absurd hzl.1 (by omega) + rw [ite_eq_right hzl_false] + have hzlf_false : ¬ zeroCrossLongFinite r res := by + intro hzlf + exact absurd hzlf.1 (by omega) + rw [ite_eq_right hzlf_false] + have hzcr_false : ¬ zeroCrossCompleteRev r res := by + intro hzcr + exact absurd hzcr.1 (by omega) + rw [ite_eq_right hzcr_false] + have hnc_false : ¬ negCrossOvershoot r res := by + intro hnc + exact absurd hnc.2.1 (by omega) + rw [ite_eq_right hnc_false] + have hncr_false : ¬ negCrossOvershootRev r res := by + intro hncr + exact absurd hncr.2.1 (by omega) + rw [ite_eq_right hncr_false] + have hncl_false : ¬ negCrossLowFwd r res := by + intro hncl + exact absurd hncl.2.1 (by omega) + rw [ite_eq_right hncl_false] + have hnclr_false : ¬ negCrossLowFwdRev r res := by + intro hnclr + exact absurd hnclr.2.1 (by omega) + rw [ite_eq_right hnclr_false] + have hnce_false : ¬ negCrossEndOne r res := by + intro hnce + exact absurd hnce.2.1 (by omega) + rw [ite_eq_right hnce_false] + have hncer_false : ¬ negCrossEndOneRev r res := by + intro hncer + exact absurd hncer.2.1 (by omega) + rw [ite_eq_right hncer_false] + have hncel_false : ¬ negCrossEndOneLong r res := by + intro h + exact absurd h.2.1 (by omega) + rw [ite_eq_right hncel_false] + have hncelr_false : ¬ negCrossEndOneLongRev r res := by + intro h + exact absurd h.2.1 (by omega) + rw [ite_eq_right hncelr_false] + have hncc_false : ¬ negCrossClampEndOne r res := by + intro hncc + exact absurd hncc.2.1 (by omega) + rw [ite_eq_right hncc_false] + have hnccr_false : ¬ negCrossClampEndOneRev r res := by + intro hnccr + exact absurd hnccr.2.1 (by omega) + rw [ite_eq_right hnccr_false] + have hnb_false : ¬ negCrossHeadBottom r res := by + intro h + exact absurd h.2.1 (by omega) + rw [ite_eq_right hnb_false] + have hnbr_false : ¬ negCrossHeadBottomRev r res := by + intro h + exact absurd h.2.1 (by omega) + rw [ite_eq_right hnbr_false] + have hnci_false : ¬ negCrossClampNonposInfinite r res := by + intro h + exact absurd h.2.1 (by omega) + rw [ite_eq_right hnci_false] + have hncir_false : ¬ negCrossClampNonposInfiniteRev r res := by + intro h + exact absurd h.2.1 (by omega) + rw [ite_eq_right hncir_false] + have hnonpos : ¬ (r.begin.offset ≤ 0) := by omega + rw [ite_eq_right hnonpos] + by_cases hone : r.begin.offset = 1 + · rw [ite_eq_left hone] + exact minimal_closedForm_of_offset_one_total hread hempty hone + · rw [ite_eq_right hone] + by_cases hmore : res.more = false + · rw [ite_eq_left hmore] + by_cases hcross : r.end_.key < r.begin.key + · rw [ite_eq_left hcross] + exact minimal_completeCrossedGeneral hread hmore hempty hbegin hcross + · rw [ite_eq_right hcross] + have hcf : r.begin.key ≤ r.end_.key := not_lt.mp hcross + by_cases hrev : r.reverse = true + · rw [ite_eq_left hrev] + exact minimal_closedForm_of_complete_crossFree_rev hread hmore hrev hempty hbegin hcf + · rw [ite_eq_right hrev] + have hrevf : r.reverse = false := by + cases hr : r.reverse <;> simp_all + exact minimal_closedForm_of_complete_crossFree hread hmore hrevf hempty hbegin hcf + · rw [ite_eq_right hmore] + have hm : res.more = true := by + cases hm : res.more <;> simp_all + by_cases hrev : r.reverse = true + · rw [ite_eq_left hrev] + by_cases hle : r.end_.offset ≤ 1 + · rw [ite_eq_left hle] + exact minimal_canonicalRange_of_end_offset_le_one_rev_more hread hm hrev hempty + (by omega) hle + · rw [ite_eq_right hle] + 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 hm hrev + (by omega) hend2 hhi + · rw [ite_eq_right hhi] + by_cases hfin : r.end_.key < r.begin.key ∧ + res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat + · rw [ite_eq_left hfin] + exact minimal_truncatedRevCrossed_finite_general hread hm hrev hbegin hfin.1 + (by omega) hempty hfin.2 + · rw [ite_eq_right hfin] + by_cases hcross : r.end_.key < r.begin.key + · rw [ite_eq_left hcross] + unfold closedFormCrossedResidual + rw [dite_eq_right hempty] + obtain ⟨db, hcomp⟩ := hread + have hread' : Readable r res := ⟨db, hcomp⟩ + have hle := crossed_rev_res_length_add_le_end (db := db) hcomp hrev + (by omega) hcross (by omega) hempty + have hne_eq : res.keys.length + (r.begin.offset - 1).toNat ≠ + (r.end_.offset - 1).toNat := by + intro heq + exact hfin ⟨hcross, heq⟩ + have hlt : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat := by omega + by_cases hcond : crossedResidualCond r res hempty + · rw [ite_eq_left hcond] + exact minimal_crossedResidual_cond hread' hm hrev (by omega) hcross + (by omega) hempty hlt hcond + · rw [ite_eq_right hcond] + exact minimal_crossedResidual_not_cond hread' hm hrev (by omega) hcross + (by omega) hempty hlt hcond + · rw [ite_eq_right hcross] + 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 hcf : r.begin.key ≤ r.end_.key := not_lt.mp hcross + exact minimal_closedForm_of_crossfree_residual hread hm hrev + (by omega) hcf hend2 hempty htpos + · rw [ite_eq_right hrev] + have hrevf : r.reverse = false := by + cases hr : r.reverse <;> simp_all + by_cases hcross : r.end_.key < r.begin.key + · rw [ite_eq_left hcross] + have hend := crossed_end_offset_ge_two_truncated hread hm hbegin hcross + exact minimal_truncatedForwardCrossedGeneral hread hm hrevf hempty hbegin hcross hend + · rw [ite_eq_right hcross] + have hcf : r.begin.key ≤ r.end_.key := not_lt.mp hcross + exact minimal_closedForm_of_truncated_general hread hm hrevf hbegin hcf + +/-- **The closed form is minimal on the cross-free nonpositive-begin complete cell.** -/ +theorem minimal_conflictRangeClosedForm_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposCompleteCrossFree r res) : + Minimal r res (conflictRangeClosedForm r res) := by + classical + unfold conflictRangeClosedForm + have hne : res.keys ≠ [] := h.2.1 + rw [ite_eq_right hne, ite_eq_left h] + exact minimal_closedForm_of_complete_nonpos_crossFree hread h + +/-- On the cross-free nonpositive-begin complete cell, the closed form denotes the +general conflict range. -/ +theorem conflictRangeClosedForm_nonpos_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposCompleteCrossFree r res) : + (conflictRangeClosedForm r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_conflictRangeClosedForm_nonpos hread h).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- On the cross-free nonpositive-begin complete cell, the closed form denotes the +executable algorithm. -/ +theorem conflictRangeClosedForm_nonpos_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposCompleteCrossFree r res) : + (conflictRangeClosedForm r res).toSet = (simpleConflictRange r res).toSet := + (minimal_conflictRangeClosedForm_nonpos hread h).toSet_eq + (simpleConflictRange_minimal hread) + +/-- **The closed form is minimal on the cross-free nonpositive-begin forward truncated +cell.** -/ +theorem minimal_conflictRangeClosedForm_nonpos_trunc {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposFwdTrunc r res) : + Minimal r res (conflictRangeClosedForm r res) := by + classical + unfold conflictRangeClosedForm + have hne : res.keys ≠ [] := by + obtain ⟨db, hcomp⟩ := hread + exact RangeRead.compatible_ne_nil_of_more hcomp h.1 + have hnp : ¬ nonposCompleteCrossFree r res := by + intro hc + unfold nonposCompleteCrossFree at hc + rw [h.1] at hc + exact Bool.noConfusion hc.1 + rw [ite_eq_right hne, ite_eq_right hnp, ite_eq_left h] + exact minimal_closedForm_of_nonpos_fwdTrunc hread h + +/-- On the cross-free nonpositive-begin forward truncated cell, the closed form denotes +the general conflict range. -/ +theorem conflictRangeClosedForm_nonpos_trunc_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposFwdTrunc r res) : + (conflictRangeClosedForm r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_conflictRangeClosedForm_nonpos_trunc hread h).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- On the cross-free nonpositive-begin forward truncated cell, the closed form denotes +the executable algorithm. -/ +theorem conflictRangeClosedForm_nonpos_trunc_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposFwdTrunc r res) : + (conflictRangeClosedForm r res).toSet = (simpleConflictRange r res).toSet := + (minimal_conflictRangeClosedForm_nonpos_trunc hread h).toSet_eq + (simpleConflictRange_minimal hread) + +/-- **The closed form is minimal on the cross-free nonpositive-begin forward truncated +nonpositive-end cell.** -/ +theorem minimal_conflictRangeClosedForm_nonpos_trunc_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposFwdTruncNonpos r res) : + Minimal r res (conflictRangeClosedForm r res) := by + classical + unfold conflictRangeClosedForm + have hne : res.keys ≠ [] := by + obtain ⟨db, hcomp⟩ := hread + exact RangeRead.compatible_ne_nil_of_more hcomp h.1 + have hnp : ¬ nonposCompleteCrossFree r res := by + intro hc + unfold nonposCompleteCrossFree at hc + rw [h.1] at hc + exact Bool.noConfusion hc.1 + have hft : ¬ nonposFwdTrunc r res := by + intro hf + have hge : 1 ≤ r.end_.offset := hf.2.2.2.2 + have hle : r.end_.offset ≤ 0 := h.2.2.2.2 + omega + rw [ite_eq_right hne, ite_eq_right hnp, ite_eq_right hft, ite_eq_left h] + exact minimal_closedForm_of_nonpos_fwdTruncNonpos hread h + +/-- On the cross-free nonpositive-begin forward truncated nonpositive-end cell, the +closed form denotes the general conflict range. -/ +theorem conflictRangeClosedForm_nonpos_trunc_nonpos_toSet_eq_general {r : RangeRead} + {res : Result} (hread : Readable r res) (h : nonposFwdTruncNonpos r res) : + (conflictRangeClosedForm r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_conflictRangeClosedForm_nonpos_trunc_nonpos hread h).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- On the cross-free nonpositive-begin forward truncated nonpositive-end cell, the +closed form denotes the executable algorithm. -/ +theorem conflictRangeClosedForm_nonpos_trunc_nonpos_toSet_eq_simple {r : RangeRead} + {res : Result} (hread : Readable r res) (h : nonposFwdTruncNonpos r res) : + (conflictRangeClosedForm r res).toSet = (simpleConflictRange r res).toSet := + (minimal_conflictRangeClosedForm_nonpos_trunc_nonpos hread h).toSet_eq + (simpleConflictRange_minimal hread) + +/-- **The closed form is minimal on the cross-free nonpositive-begin reversed truncated +cell.** -/ +theorem minimal_conflictRangeClosedForm_nonpos_revtrunc {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposRevTrunc r res) : + Minimal r res (conflictRangeClosedForm r res) := by + classical + unfold conflictRangeClosedForm + have hne : res.keys ≠ [] := by + obtain ⟨db, hcomp⟩ := hread + exact RangeRead.compatible_ne_nil_of_more hcomp h.1 + have hnp : ¬ nonposCompleteCrossFree r res := by + intro hc + have hmc : res.more = false := hc.1 + rw [h.1] at hmc + exact Bool.noConfusion hmc + have hft : ¬ nonposFwdTrunc r res := by + intro hf + have hrevf : r.reverse = false := hf.2.1 + rw [h.2.1] at hrevf + exact Bool.noConfusion hrevf + have hftn : ¬ nonposFwdTruncNonpos r res := by + intro hf + have hrevf : r.reverse = false := hf.2.1 + rw [h.2.1] at hrevf + exact Bool.noConfusion hrevf + rw [ite_eq_right hne, ite_eq_right hnp, ite_eq_right hft, ite_eq_right hftn, + ite_eq_left h] + exact minimal_closedForm_of_nonpos_revTrunc hread h + +/-- On the cross-free nonpositive-begin reversed truncated cell, the closed form denotes +the general conflict range. -/ +theorem conflictRangeClosedForm_nonpos_revtrunc_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposRevTrunc r res) : + (conflictRangeClosedForm r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_conflictRangeClosedForm_nonpos_revtrunc hread h).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- On the cross-free nonpositive-begin reversed truncated cell, the closed form denotes +the executable algorithm. -/ +theorem conflictRangeClosedForm_nonpos_revtrunc_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposRevTrunc r res) : + (conflictRangeClosedForm r res).toSet = (simpleConflictRange r res).toSet := + (minimal_conflictRangeClosedForm_nonpos_revtrunc hread h).toSet_eq + (simpleConflictRange_minimal hread) + +/-- The total closed form denotes the general conflict range for every readable result +at every positive begin offset. -/ +theorem conflictRangeClosedForm_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hbegin : 1 ≤ r.begin.offset) : + (conflictRangeClosedForm r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_conflictRangeClosedForm hread hbegin).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The total closed form denotes the executable algorithm for every readable result at +every positive begin offset. -/ +theorem conflictRangeClosedForm_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (hbegin : 1 ≤ r.begin.offset) : + (conflictRangeClosedForm r res).toSet = (simpleConflictRange r res).toSet := + (minimal_conflictRangeClosedForm hread hbegin).toSet_eq + (simpleConflictRange_minimal hread) + +/-- **The total closed form is minimal for every readable result, at every begin +offset.** Positive begin offsets use the compact closed forms, and the nonpositive +cells use the cross-free and crossed closed forms where they exist; the residual +nonpositive crossed cells delegate to the exact executable `simpleConflictRange`. -/ +theorem minimal_conflictRangeClosedForm_total {r : RangeRead} {res : Result} + (hread : Readable r res) : + Minimal r res (conflictRangeClosedForm r res) := by + classical + by_cases hpos : 1 ≤ r.begin.offset + · exact minimal_conflictRangeClosedForm hread hpos + · unfold conflictRangeClosedForm + 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 hnp : nonposCompleteCrossFree r res + · rw [ite_eq_left hnp] + exact minimal_closedForm_of_complete_nonpos_crossFree hread hnp + · rw [ite_eq_right hnp] + by_cases hft : nonposFwdTrunc r res + · rw [ite_eq_left hft] + exact minimal_closedForm_of_nonpos_fwdTrunc hread hft + · rw [ite_eq_right hft] + by_cases hftn : nonposFwdTruncNonpos r res + · rw [ite_eq_left hftn] + exact minimal_closedForm_of_nonpos_fwdTruncNonpos hread hftn + · rw [ite_eq_right hftn] + by_cases hrt : nonposRevTrunc r res + · rw [ite_eq_left hrt] + exact minimal_closedForm_of_nonpos_revTrunc hread hrt + · rw [ite_eq_right hrt] + by_cases hzc : zeroCrossOvershootComplete r res + · rw [ite_eq_left hzc] + exact minimal_closedForm_of_zeroCross_overshoot hread hzc + · rw [ite_eq_right hzc] + by_cases hze : zeroCrossEndOne r res + · rw [ite_eq_left hze] + exact minimal_closedForm_of_zeroCross_endOne hread hze + · rw [ite_eq_right hze] + by_cases hzel : zeroCrossEndOneLong r res + · rw [ite_eq_left hzel] + exact minimal_closedForm_of_zeroCross_endOne_long hread hzel + · rw [ite_eq_right hzel] + by_cases hzn : zeroCrossEndNonpos r res + · rw [ite_eq_left hzn] + exact minimal_closedForm_of_zeroCross_endNonpos hread hzn + · rw [ite_eq_right hzn] + by_cases hzu : zeroCrossUnderEnd r res + · rw [ite_eq_left hzu] + exact minimal_closedForm_of_zeroCross_underEnd hread hzu + · rw [ite_eq_right hzu] + by_cases hzl : zeroCrossLongTop r res + · rw [ite_eq_left hzl] + exact minimal_closedForm_of_zeroCross_longTop hread hzl + · rw [ite_eq_right hzl] + by_cases hzlf : zeroCrossLongFinite r res + · rw [ite_eq_left hzlf] + exact minimal_closedForm_of_zeroCross_longFinite hread hzlf + · rw [ite_eq_right hzlf] + by_cases hzcr : zeroCrossCompleteRev r res + · rw [ite_eq_left hzcr] + exact minimal_closedForm_of_zeroCross_complete_rev hread hzcr.2.2.1 + hzcr.1 hzcr.2.1 hzcr.2.2.2.1 hzcr.2.2.2.2 + · rw [ite_eq_right hzcr] + by_cases hnc : negCrossOvershoot r res + · rw [ite_eq_left hnc] + have hcross' : ((res.keys.head hnc.2.2.2.1 : Key) : WithTop Key) < + r.begin.key := by + have h := hnc.2.2.2.2.2.2 + rwa [headD_eq_head_of_ne hnc.2.2.2.1] at h + exact minimal_closedForm_of_negCross_overshoot hread hnc.1 hnc.2.1 + hnc.2.2.1 hnc.2.2.2.1 hnc.2.2.2.2.1 hnc.2.2.2.2.2.1 hcross' + · rw [ite_eq_right hnc] + by_cases hncr : negCrossOvershootRev r res + · rw [ite_eq_left hncr] + exact minimal_closedForm_of_negCross_overshoot_rev hread hncr + · rw [ite_eq_right hncr] + by_cases hncl : negCrossLowFwd r res + · rw [ite_eq_left hncl] + exact minimal_closedForm_of_negCross_lowFwd hread hncl + · rw [ite_eq_right hncl] + by_cases hnclr : negCrossLowFwdRev r res + · rw [ite_eq_left hnclr] + exact minimal_closedForm_of_negCross_lowFwd_rev hread hnclr + · rw [ite_eq_right hnclr] + by_cases hnce : negCrossEndOne r res + · rw [ite_eq_left hnce] + exact minimal_closedForm_of_negCross_endOne hread hnce + · rw [ite_eq_right hnce] + by_cases hncer : negCrossEndOneRev r res + · rw [ite_eq_left hncer] + exact minimal_closedForm_of_negCross_endOne_rev hread hncer + · rw [ite_eq_right hncer] + by_cases hncel : negCrossEndOneLong r res + · rw [ite_eq_left hncel] + exact minimal_closedForm_of_negCross_endOne_long hread hncel + · rw [ite_eq_right hncel] + by_cases hncelr : negCrossEndOneLongRev r res + · rw [ite_eq_left hncelr] + exact minimal_closedForm_of_negCross_endOne_long_rev hread hncelr + · rw [ite_eq_right hncelr] + by_cases hncc : negCrossClampEndOne r res + · rw [ite_eq_left hncc] + exact minimal_closedForm_of_negCross_clampEndOne hread hncc + · rw [ite_eq_right hncc] + by_cases hnccr : negCrossClampEndOneRev r res + · rw [ite_eq_left hnccr] + exact minimal_closedForm_of_negCross_clampEndOne_rev hread hnccr + · rw [ite_eq_right hnccr] + by_cases hnb : negCrossHeadBottom r res + · rw [ite_eq_left hnb] + exact minimal_closedForm_of_negCross_headBottom hread hnb + · rw [ite_eq_right hnb] + by_cases hnbr : negCrossHeadBottomRev r res + · rw [ite_eq_left hnbr] + exact minimal_closedForm_of_negCross_headBottom_rev hread hnbr + · rw [ite_eq_right hnbr] + by_cases hnci : negCrossClampNonposInfinite r res + · rw [ite_eq_left hnci] + exact minimal_closedForm_of_negCross_clampNonpos_infinite hread hnci + · rw [ite_eq_right hnci] + by_cases hncir : negCrossClampNonposInfiniteRev r res + · rw [ite_eq_left hncir] + exact minimal_closedForm_of_negCross_clampNonpos_infinite_rev hread hncir + · rw [ite_eq_right hncir] + rw [ite_eq_left (by omega : r.begin.offset ≤ 0)] + by_cases hzcrv : zeroCrossTruncRevCrossed r res + · rw [ite_eq_left hzcrv] + exact minimal_closedForm_of_zeroCross_trunc_revCrossed hread hzcrv + · rw [ite_eq_right hzcrv] + by_cases hzct : zeroCrossTruncReadAllEnd r res + · rw [ite_eq_left hzct] + exact minimal_closedForm_of_zeroCross_trunc_readAllEnd hread hzct + · rw [ite_eq_right hzct] + by_cases hzcb : zeroCrossTruncBelowEnd r res + · rw [ite_eq_left hzcb] + exact minimal_closedForm_of_zeroCross_trunc_belowEnd hread hzcb + · rw [ite_eq_right hzcb] + by_cases hzce : zeroCrossTruncEndGeHead r res + · rw [ite_eq_left hzce] + exact minimal_closedForm_of_zeroCross_trunc_endGeHead hread hzce + · rw [ite_eq_right hzce] + by_cases hzcl : zeroCrossTruncLowEndSmall r res + · rw [ite_eq_left hzcl] + exact minimal_closedForm_of_zeroCross_trunc_lowEndSmall hread hzcl + · rw [ite_eq_right hzcl] + by_cases hzcbg : zeroCrossTruncLowEndBig r res + · rw [ite_eq_left hzcbg] + exact minimal_closedForm_of_zeroCross_trunc_lowEndBig hread hzcbg + · rw [ite_eq_right hzcbg] + by_cases hzcn : zeroCrossTruncNonposEnd r res + · rw [ite_eq_left hzcn] + exact minimal_closedForm_of_zeroCross_trunc_nonposEnd hread hzcn + · rw [ite_eq_right hzcn] + by_cases hnlg : negCrossClampNonposLastGe r res + · rw [ite_eq_left hnlg] + exact + minimal_closedForm_of_negCross_clampNonpos_lastGe hread hnlg + · rw [ite_eq_right hnlg] + by_cases hnlgrev : negCrossClampNonposLastGeRev r res + · rw [ite_eq_left hnlgrev] + exact + minimal_closedForm_of_negCross_clampNonpos_lastGe_rev hread hnlgrev + · rw [ite_eq_right hnlgrev] + by_cases hncbn : negCrossClampNonposBottom r res + · rw [ite_eq_left hncbn] + exact + minimal_closedForm_of_negCross_clampNonpos_bottom hread hncbn + · rw [ite_eq_right hncbn] + by_cases hncbnr : negCrossClampNonposBottomRev r res + · rw [ite_eq_left hncbnr] + exact + minimal_closedForm_of_negCross_clampNonpos_bottom_rev hread hncbnr + · rw [ite_eq_right hncbnr] + by_cases hnc : negCrossNonclampNonpos r res + · rw [ite_eq_left hnc] + exact + minimal_closedForm_of_negCross_nonclamp_nonpos hread hnc + · rw [ite_eq_right hnc] + by_cases hncr : negCrossNonclampNonposRev r res + · rw [ite_eq_left hncr] + exact + minimal_closedForm_of_negCross_nonclamp_nonpos_rev hread hncr + · rw [ite_eq_right hncr] + by_cases hncl : negCrossNonclampLongNonpos r res + · rw [ite_eq_left hncl] + exact + minimal_closedForm_of_negCross_nonclamp_long_nonpos hread hncl + · rw [ite_eq_right hncl] + by_cases hnclr : negCrossNonclampLongNonposRev r res + · rw [ite_eq_left hnclr] + exact + minimal_closedForm_of_negCross_nonclamp_long_nonpos_rev hread hnclr + · rw [ite_eq_right hnclr] + by_cases hnclc : negCrossClampLongNonpos r res + · rw [ite_eq_left hnclc] + exact + minimal_closedForm_of_negCross_clamp_long_nonpos hread hnclc + · rw [ite_eq_right hnclc] + by_cases hnclcr : negCrossClampLongNonposRev r res + · rw [ite_eq_left hnclcr] + exact + minimal_closedForm_of_negCross_clamp_long_nonpos_rev hread hnclcr + · rw [ite_eq_right hnclcr] + by_cases hhigh : negCrossCompleteHighSmall r res + · rw [ite_eq_left hhigh] + exact + minimal_closedForm_of_negCrossCompleteHigh_small hread hhigh + · rw [ite_eq_right hhigh] + by_cases hclampfin : negCrossCompleteHighClampFinite r res + · rw [ite_eq_left hclampfin] + exact + minimal_closedForm_of_negCrossCompleteHigh_clampFinite hread hclampfin + · rw [ite_eq_right hclampfin] + by_cases hhighrev : negCrossCompleteHighRev r res + · rw [ite_eq_left hhighrev] + exact + minimal_closedForm_of_negCrossCompleteHigh_rev hread hhighrev + · rw [ite_eq_right hhighrev] + by_cases hncres : negCrossCompleteResidual r res + · rw [ite_eq_left hncres] + exact minimal_closedForm_of_negCross_complete_residual hread hncres + · rw [ite_eq_right hncres] + by_cases hncresr : negCrossCompleteResidualRev r res + · rw [ite_eq_left hncresr] + exact minimal_closedForm_of_negCross_complete_residual_rev hread hncresr + · rw [ite_eq_right hncresr] + by_cases hnb : negCrossTruncNonclampBelowEnd r res + · rw [ite_eq_left hnb] + exact minimal_closedForm_of_negCross_trunc_nonclamp_below hread hnb + · rw [ite_eq_right hnb] + by_cases heg : negCrossTruncNonclampEndGeHead r res + · rw [ite_eq_left heg] + exact minimal_closedForm_of_negCross_trunc_nonclamp_endGeHead hread heg + · rw [ite_eq_right heg] + by_cases hne1 : negCrossTruncNonclampEndOne r res + · rw [ite_eq_left hne1] + exact minimal_closedForm_of_negCross_trunc_nonclamp_endOne hread hne1 + · rw [ite_eq_right hne1] + by_cases hnt : negCrossTruncNonclampTop r res + · rw [ite_eq_left hnt] + exact minimal_closedForm_of_negCross_trunc_nonclamp_top hread hnt + · rw [ite_eq_right hnt] + by_cases hlows : negCrossTruncNonclampLowEndSmall r res + · rw [ite_eq_left hlows] + exact minimal_closedForm_of_negCross_trunc_nonclamp_lowEndSmall hread hlows + · rw [ite_eq_right hlows] + by_cases hlowb : negCrossTruncNonclampLowEndBig r res + · rw [ite_eq_left hlowb] + exact minimal_closedForm_of_negCross_trunc_nonclamp_lowEndBig hread hlowb + · rw [ite_eq_right hlowb] + by_cases hgaplow : negCrossTruncClampGapLow r res + · rw [ite_eq_left hgaplow] + exact minimal_closedForm_of_negCross_trunc_clamp_gapLow hread hgaplow + · rw [ite_eq_right hgaplow] + by_cases hgapbotmid : negCrossTruncClampGapBotMid r res + · rw [ite_eq_left hgapbotmid] + exact minimal_closedForm_of_negCross_trunc_clamp_gapBotMid hread hgapbotmid + · rw [ite_eq_right hgapbotmid] + by_cases hgaphigh : negCrossTruncClampGapHighBot r res + · rw [ite_eq_left hgaphigh] + exact minimal_closedForm_of_negCross_trunc_clamp_gapHighBot hread hgaphigh + · rw [ite_eq_right hgaphigh] + by_cases hgaphighgen : negCrossTruncClampGapHigh r res + · rw [ite_eq_left hgaphighgen] + exact minimal_closedForm_of_negCross_trunc_clamp_gapHigh hread hgaphighgen + · rw [ite_eq_right hgaphighgen] + by_cases hclampgen : negCrossTruncClampNoGap r res + · rw [ite_eq_left hclampgen] + exact minimal_closedForm_of_negCross_trunc_clamp_noGap hread hclampgen + · rw [ite_eq_right hclampgen] + by_cases hrevbelowend : negCrossTruncRevBelowEnd r res + · rw [ite_eq_left hrevbelowend] + exact minimal_closedForm_of_negCross_trunc_rev_belowEnd hread hrevbelowend + · rw [ite_eq_right hrevbelowend] + by_cases hrevnonposover : negCrossTruncRevNonposOvershoot r res + · rw [ite_eq_left hrevnonposover] + exact minimal_closedForm_of_negCross_trunc_rev_nonposOvershoot hread hrevnonposover + · rw [ite_eq_right hrevnonposover] + by_cases hrevnonposunder : negCrossTruncRevNonposUnder r res + · rw [ite_eq_left hrevnonposunder] + exact minimal_closedForm_of_negCross_trunc_rev_nonposUnder hread hrevnonposunder + · rw [ite_eq_right hrevnonposunder] + by_cases hrevcrosssmall : negCrossTruncRevCrossedSmall r res + · rw [ite_eq_left hrevcrosssmall] + exact minimal_closedForm_of_negCross_trunc_rev_crossedSmall hread hrevcrosssmall + · rw [ite_eq_right hrevcrosssmall] + by_cases hrevcrosslarge : negCrossTruncRevCrossedLarge r res + · rw [ite_eq_left hrevcrosslarge] + exact minimal_closedForm_of_negCross_trunc_rev_crossedLarge hread hrevcrosslarge + · rw [ite_eq_right hrevcrosslarge] + by_cases hrevunder : negCrossTruncRevUnder r res + · rw [ite_eq_left hrevunder] + exact minimal_closedForm_of_negCross_trunc_rev_under hread hrevunder + · rw [ite_eq_right hrevunder] + by_cases hrevbegtop : negCrossTruncRevBeginTop r res + · rw [ite_eq_left hrevbegtop] + exact minimal_closedForm_of_negCross_trunc_rev_beginTop hread hrevbegtop + · rw [ite_eq_right hrevbegtop] + by_cases hrevbegtoptwo : negCrossTruncRevBeginTopGeTwo r res + · rw [ite_eq_left hrevbegtoptwo] + exact minimal_closedForm_of_negCross_trunc_rev_beginTop_ge_two hread hrevbegtoptwo + · rw [ite_eq_right hrevbegtoptwo] + by_cases hrevtop : negCrossTruncRevTop r res + · rw [ite_eq_left hrevtop] + exact minimal_closedForm_of_negCross_trunc_rev_top hread hrevtop + · rw [ite_eq_right hrevtop] + by_cases hrevcrossnonpossmall : negCrossTruncRevCrossedNonposSmall r res + · rw [ite_eq_left hrevcrossnonpossmall] + exact minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_small hread hrevcrossnonpossmall + · rw [ite_eq_right hrevcrossnonpossmall] + by_cases hrevcrossnonposlarge : negCrossTruncRevCrossedNonposLarge r res + · rw [ite_eq_left hrevcrossnonposlarge] + exact minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_large hread hrevcrossnonposlarge + · rw [ite_eq_right hrevcrossnonposlarge] + by_cases hrevresroom : negCrossTruncRevResidualRoom r res + · rw [ite_eq_left hrevresroom] + exact minimal_closedForm_of_negCross_trunc_rev_residual_room hread hrevresroom + · rw [ite_eq_right hrevresroom] + by_cases hrevrestopsmall : negCrossTruncRevResidualTopCrossedSmallGap r res + · rw [ite_eq_left hrevrestopsmall] + exact minimal_closedForm_of_negCross_trunc_rev_residual_top_crossed_smallGap hread hrevrestopsmall + · rw [ite_eq_right hrevrestopsmall] + by_cases hrevrestopcrossed : negCrossTruncRevResidualTopCrossed r res + · rw [ite_eq_left hrevrestopcrossed] + exact minimal_closedForm_of_negCross_trunc_rev_residual_top_crossed hread hrevrestopcrossed + · rw [ite_eq_right hrevrestopcrossed] + by_cases hrevrestoplarge : negCrossTruncRevResidualTopLargeCrossed r res + · rw [ite_eq_left hrevrestoplarge] + exact minimal_closedForm_of_negCross_trunc_rev_residual_top_large_crossed hread hrevrestoplarge + · rw [ite_eq_right hrevrestoplarge] + by_cases hrevrestopsentinel : negCrossTruncRevResidualTopLargeSentinel r res + · rw [ite_eq_left hrevrestopsentinel] + exact minimal_closedForm_of_negCross_trunc_rev_residual_top_large_sentinel hread hrevrestopsentinel + · rw [ite_eq_right hrevrestopsentinel] + by_cases hrevfinunder : negCrossTruncRevResidualFiniteUnderCrossed r res + · rw [ite_eq_left hrevfinunder] + exact minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_crossed hread hrevfinunder + · rw [ite_eq_right hrevfinunder] + by_cases hrevfinundernc : negCrossTruncRevResidualFiniteUnderNotCrossed r res + · rw [ite_eq_left hrevfinundernc] + exact minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_notCrossed hread hrevfinundernc + · rw [ite_eq_right hrevfinundernc] + by_cases hrevfinover : negCrossTruncRevResidualFiniteOver r res + · rw [ite_eq_left hrevfinover] + exact minimal_closedForm_of_negCross_trunc_rev_residualFinite_over hread hrevfinover + · rw [ite_eq_right hrevfinover] + exact simpleConflictRange_minimal hread + +/-- The total closed form denotes the general conflict range for every readable +result. -/ +theorem conflictRangeClosedForm_toSet_eq_general_total {r : RangeRead} {res : Result} + (hread : Readable r res) : + (conflictRangeClosedForm r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_conflictRangeClosedForm_total hread).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The total closed form denotes the executable algorithm for every readable result. -/ +theorem conflictRangeClosedForm_toSet_eq_simple_total {r : RangeRead} {res : Result} + (hread : Readable r res) : + (conflictRangeClosedForm r res).toSet = (simpleConflictRange r res).toSet := + (minimal_conflictRangeClosedForm_total hread).toSet_eq + (simpleConflictRange_minimal hread) + +end FormalConflictRange diff --git a/FormalConflictRange/ClosedFormTotal.lean b/FormalConflictRange/ClosedFormTotal.lean new file mode 100644 index 0000000..fa50bdc --- /dev/null +++ b/FormalConflictRange/ClosedFormTotal.lean @@ -0,0 +1,108 @@ +/- +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.CrossedRev + +/-! +# The total closed form at begin offset one + +This module assembles the complete, forward-truncated and reversed-truncated rules +into a single result-aware closed form that is minimal for *every* readable result at +`begin.offset = 1`, crossed or cross-free, in either traversal order. + +The cross-free region is delegated to `closedFormOffsetOneUniversal`; on a crossed +selector pair `end_.key < begin_.key` it uses the crossed complete rule +`completeClosed`, the crossed forward-truncated rule `truncatedForwardClosed`, or the +corrected reversed-truncated rule `closedFormOffsetOneTruncatedRevFullC`. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- On a crossed readable truncated result, `end.offset ≥ 2`. A nonpositive end +overshoot would make the complete window empty, contradicting a nonempty truncated +result. -/ +theorem crossed_end_offset_ge_two_truncated {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) : + 2 ≤ r.end_.offset := by + by_contra h + have hend : r.end_.offset ≤ 1 := by omega + obtain ⟨db₀, hcomp₀⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hemp : RangeRead.completeKeys db₀ r = [] := + alwaysEmpty_of_begin_gt_end hbegin hend hcross db₀ + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + rw [hemp] at hpre + exact hne (List.eq_nil_of_prefix_nil hpre) + +/-- **The total closed form at begin offset one.** Cross-free reads use the compact +universal rule; crossed reads use the traversal-aware crossed complete and truncated +rules. -/ +noncomputable def closedFormOffsetOneTotal (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.key < r.begin.key then + if res.more = true then + if r.reverse = true then closedFormOffsetOneTruncatedRevFullC r res + else truncatedForwardClosed r res + else completeClosed r res + else closedFormOffsetOneUniversal r res + +/-- **The total closed form is minimal for every readable result at begin offset +one.** No crossed or cross-free cell is left open. -/ +theorem minimal_closedForm_of_offset_one_total {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) : + Minimal r res (closedFormOffsetOneTotal r res) := by + unfold closedFormOffsetOneTotal + by_cases hcross : r.end_.key < r.begin.key + · rw [if_pos hcross] + by_cases hmore : res.more = true + · rw [if_pos hmore] + have hend := crossed_end_offset_ge_two_truncated hread hmore (by omega) hcross + by_cases hrev : r.reverse = true + · rw [if_pos hrev] + exact minimal_closedForm_of_offset_one_truncated_rev_fullC_crossed hread hmore + hrev hbegin hcross hend + · rw [if_neg hrev] + have hrevf : r.reverse = false := by + cases hr : r.reverse <;> simp_all + exact minimal_truncatedForwardClosed_of_crossed hread hmore hrevf hne hbegin + hcross hend + · rw [if_neg hmore] + have hfalse : res.more = false := by + cases hm : res.more <;> simp_all + exact minimal_completeClosed hread hfalse hne hbegin + · rw [if_neg hcross] + have hcf : r.begin.key ≤ r.end_.key := not_lt.mp hcross + exact minimal_closedForm_of_offset_one_universal hread hne hbegin hcf + +/-- The total closed form denotes the general conflict range for every readable +result at begin offset one. -/ +theorem closedFormOffsetOneTotal_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) : + (closedFormOffsetOneTotal r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_closedForm_of_offset_one_total hread hne hbegin).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The total closed form denotes the same key set as the executable algorithm. -/ +theorem closedFormOffsetOneTotal_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) : + (closedFormOffsetOneTotal r res).toSet = (simpleConflictRange r res).toSet := + (minimal_closedForm_of_offset_one_total hread hne hbegin).toSet_eq + (simpleConflictRange_minimal hread) + +/-- On the cross-free region the total rule agrees with the compact universal rule. -/ +theorem closedFormOffsetOneTotal_eq_universal {r : RangeRead} {res : Result} + (hcross : r.begin.key ≤ r.end_.key) : + closedFormOffsetOneTotal r res = closedFormOffsetOneUniversal r res := by + unfold closedFormOffsetOneTotal + rw [if_neg (not_lt.mpr hcross)] + +end FormalConflictRange diff --git a/FormalConflictRange/ComputableAlgorithm.lean b/FormalConflictRange/ComputableAlgorithm.lean new file mode 100644 index 0000000..80cc38c --- /dev/null +++ b/FormalConflictRange/ComputableAlgorithm.lean @@ -0,0 +1,72 @@ +/- +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.ComputableHull +import FormalConflictRange.ComputableSearch + +/-! +# A simple computable conflict-range algorithm + +This file assembles the computable pieces into one algorithm: + +* `activeComputableFast` decides cell activity by finite count feasibility, using + the lazy hitting-set search of `ComputableSearch.lean`; +* `cellRepsC`/`activeRepsOfC` enumerate the endpoint cells with executable + `Finset` operations; +* `hullOfActiveRepsC` extracts the least active key and greatest active upper + boundary with `Finset.min`/`Finset.max`. + +The result, `simpleConflictRange`, is a total `ConflictRange`-valued `def` (not +a classical existence statement) and is proved minimal on every readable pair. +Both activity and hull extraction are executable; the eager `choices` oracle is +kept only as a proof-facing reference. +-/ + +namespace FormalConflictRange + +/-- The simple computable conflict-range algorithm: enumerate the endpoint +cells, decide activity with the lazy oracle `activeComputableFast`, and take the +hull. -/ +def simpleConflictRange (r : RangeRead) (res : Result) : ConflictRange := + computableCellwise r res (activeComputableFast r res) + +/-- The simple algorithm agrees with the cellwise oracle algorithm. -/ +theorem simpleConflictRange_eq_cellwise (r : RangeRead) (res : Result) : + simpleConflictRange r res = conflictRangeCellwise r res (activeComputableFast r res) := + computableCellwise_eq_cellwise r res (activeComputableFast r res) + +/-- On a readable pair, the simple algorithm agrees with the cellwise algorithm +driven by the eager `choices` oracle. -/ +theorem simpleConflictRange_eq_eager_cellwise {r : RangeRead} {res : Result} + (hread : Readable r res) : + simpleConflictRange r res = + conflictRangeCellwise r res (activeComputable r res) := by + rw [simpleConflictRange_eq_cellwise] + have hfun : activeComputableFast r res = activeComputable r res := + funext (activeComputableFast_eq hread) + rw [hfun] + +/-- The simple algorithm computes the general hull on every readable pair. -/ +theorem simpleConflictRange_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) : + simpleConflictRange r res = conflictRangeGeneral r res := by + rw [simpleConflictRange_eq_cellwise] + exact conflictRangeCellwise_eq_general (activeComputableFast_spec hread) + +/-- The simple algorithm is minimal on every readable pair. -/ +theorem simpleConflictRange_minimal {r : RangeRead} {res : Result} + (hread : Readable r res) : + Minimal r res (simpleConflictRange r res) := by + rw [simpleConflictRange_eq_general hread] + exact conflictRangeGeneral_minimal hread + +/-- The simple algorithm computes exactly the necessary-key set. -/ +theorem simpleConflictRange_toSet_eq_necessarySet {r : RangeRead} {res : Result} + (hread : Readable r res) : + (simpleConflictRange r res).toSet = necessarySet r res := by + rw [simpleConflictRange_eq_general hread] + exact necessarySet_eq_flipHull (characterizesDNF_characterizingDNF hread) + +end FormalConflictRange diff --git a/FormalConflictRange/ComputableHull.lean b/FormalConflictRange/ComputableHull.lean new file mode 100644 index 0000000..43963d8 --- /dev/null +++ b/FormalConflictRange/ComputableHull.lean @@ -0,0 +1,197 @@ +/- +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.General + +/-! +# A computable cell hull + +The general algorithm is stated with `Finset.min'`/`Finset.max'`, which are +noncomputable because they take an explicit nonemptiness proof. This file +replaces those choices by the executable `Finset.min`/`Finset.max` operations +(with `⊤`/`⊥` as the empty-set defaults) and proves that the resulting cell +representatives and hull agree with the classical definitions. + +The endpoint cells themselves are already computable: `cellEndpoints` is a +`def`, and only the choice of a representative inside a cell used `min'`/`max'`. +Once those are replaced, the cellwise algorithm with `activeComputable` is an +executable `def`. +-/ + +namespace FormalConflictRange + +/-- Computable least endpoint strictly above `e`, with `⊤` for the empty case. -/ +def nextEndpointC (E : Finset (WithTop Key)) (e : WithTop Key) : WithTop Key := + match (E.filter fun x => e < x).min with + | none => ⊤ + | some x => x + +theorem nextEndpointC_eq (E : Finset (WithTop Key)) (e : WithTop Key) : + nextEndpointC E e = nextEndpoint E e := by + unfold nextEndpointC nextEndpoint + by_cases h : (E.filter fun x => e < x).Nonempty + · rw [dite_eq_left h] + have hmin : (E.filter fun x => e < x).min = + ((Finset.min' _ h : WithTop Key) : WithTop (WithTop Key)) := + (Finset.coe_min' h).symm + rw [hmin] + · have hempty : E.filter (fun x => e < x) = ∅ := + Finset.not_nonempty_iff_eq_empty.mp h + rw [dite_eq_right h, hempty] + simp + +/-- Computable greatest endpoint at or below `k`, with `⊥` as a default. The +`hBot` argument is kept for definitional compatibility with `cellLo`. -/ +def cellLoC (E : Finset (WithTop Key)) + (_hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : WithTop Key := + match (E.filter fun e => e ≤ (k : WithTop Key)).max with + | some e => e + | none => (([] : Key) : WithTop Key) + +theorem cellLoC_eq (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : + cellLoC E hBot k = cellLo E hBot k := by + unfold cellLoC cellLo + have hne : (E.filter fun e => e ≤ (k : WithTop Key)).Nonempty := by + refine ⟨(([] : Key) : WithTop Key), ?_⟩ + rw [Finset.mem_filter] + exact ⟨hBot, by simp [nil_le_key]⟩ + have hmax : (E.filter fun e => e ≤ (k : WithTop Key)).max = + ((Finset.max' _ hne : WithTop Key) : WithBot (WithTop Key)) := + (Finset.coe_max' hne).symm + rw [hmax] + +/-- Computable version of the next endpoint after the cell's lower endpoint. -/ +def cellHiC (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : WithTop Key := + nextEndpointC E (cellLoC E hBot k) + +theorem cellHiC_eq (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : + cellHiC E hBot k = cellHi E hBot k := by + unfold cellHiC cellHi + rw [cellLoC_eq, nextEndpointC_eq] + +/-- Computable representative of `k`'s cell. -/ +def cellRepC (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : Key × WithTop Key := + let lo := cellLoC E hBot k + match lo with + | some l => + if (k : WithTop Key) = lo then (k, keyAfter k) + else (keyAfter l, nextEndpointC E lo) + | none => (k, keyAfter k) + +theorem cellRepC_eq (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : + cellRepC E hBot k = cellRep E hBot k := by + unfold cellRepC cellRep + rw [cellLoC_eq] + cases hlo : cellLo E hBot k with + | top => rfl + | coe l => + simp only + by_cases hk : (k : WithTop Key) = (↑l : WithTop Key) + · simp [hk] + · simp [hk, nextEndpointC_eq] + +/-- Computable open-cell representative following a finite endpoint. -/ +def openRepC (E : Finset (WithTop Key)) (e : WithTop Key) : Key × WithTop Key := + match e with + | some l => (keyAfter l, nextEndpointC E e) + | none => (([] : Key), (([] : Key) : WithTop Key)) + +theorem openRepC_eq (E : Finset (WithTop Key)) (e : WithTop Key) : + openRepC E e = openRep E e := by + unfold openRepC openRep + cases e with + | top => rfl + | coe l => rw [nextEndpointC_eq] + +/-- Computable finite set of cell representatives. -/ +def cellRepsC (E : Finset (WithTop Key)) + (_hBot : (([] : Key) : WithTop Key) ∈ E) (_hTop : ⊤ ∈ E) : + Finset (Key × WithTop Key) := + let endpoints := E.filter fun e => e ≠ ⊤ + ((endpoints.image singletonRep) ∪ (endpoints.image (openRepC E))).filter + fun p => (p.1 : WithTop Key) < p.2 + +theorem cellRepsC_eq (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (hTop : ⊤ ∈ E) : + cellRepsC E hBot hTop = cellReps E hBot hTop := by + unfold cellRepsC cellReps + have hop : openRepC E = openRep E := funext (openRepC_eq E) + rw [hop] + + +/-! ## Computable representatives and hull -/ + +/-- Computable active representatives, filtered by a Boolean activity oracle. -/ +def activeRepsOfC (D : ConstraintDNF) (active : Key → Bool) : + Finset (Key × WithTop Key) := + (cellRepsC (cellEndpoints D) (cellEndpoints_bot D) (cellEndpoints_top D)).filter + (fun p => active p.1) + +theorem activeRepsOfC_eq (D : ConstraintDNF) (active : Key → Bool) : + activeRepsOfC D active = activeRepsOf D active := by + unfold activeRepsOfC activeRepsOf + rw [cellRepsC_eq] + +/-- Computable least key of a finite set of active representatives. -/ +def activeBeginC (A : Finset (Key × WithTop Key)) : Key := + match (A.image fun p => p.1).min with + | none => [] + | some k => k + +/-- Computable greatest upper boundary of a finite set of active representatives. -/ +def activeEndC (A : Finset (Key × WithTop Key)) : WithTop Key := + match (A.image fun p => p.2).max with + | none => (([] : Key) : WithTop Key) + | some e => e + +theorem activeBeginC_eq {A : Finset (Key × WithTop Key)} (h : A.Nonempty) : + activeBeginC A = activeBegin A h := by + unfold activeBeginC activeBegin + have hne : (A.image fun p => p.1).Nonempty := h.image _ + have hmin : (A.image fun p => p.1).min = + ((Finset.min' _ hne : Key) : WithTop Key) := + (Finset.coe_min' hne).symm + rw [hmin] + +theorem activeEndC_eq {A : Finset (Key × WithTop Key)} (h : A.Nonempty) : + activeEndC A = activeEnd A h := by + unfold activeEndC activeEnd + have hne : (A.image fun p => p.2).Nonempty := h.image _ + have hmax : (A.image fun p => p.2).max = + ((Finset.max' _ hne : WithTop Key) : WithBot (WithTop Key)) := + (Finset.coe_max' hne).symm + rw [hmax] + +/-- Computable hull of a finite set of active representatives. -/ +def hullOfActiveRepsC (A : Finset (Key × WithTop Key)) : ConflictRange := + ⟨(activeBeginC A : WithTop Key), activeEndC A⟩ + +theorem hullOfActiveRepsC_eq (A : Finset (Key × WithTop Key)) : + hullOfActiveRepsC A = hullOfActiveReps A := by + unfold hullOfActiveRepsC hullOfActiveReps + by_cases h : A.Nonempty + · rw [dite_eq_left h, activeBeginC_eq h, activeEndC_eq h] + · have hA : A = ∅ := Finset.not_nonempty_iff_eq_empty.mp h + rw [dite_eq_right h, hA] + simp [activeBeginC, activeEndC] + +/-- The cellwise algorithm with a Boolean oracle, using only computable hull +extraction. -/ +def computableCellwise (r : RangeRead) (res : Result) (active : Key → Bool) : + ConflictRange := + hullOfActiveRepsC (activeRepsOfC (characterizingDNF r res) active) + +theorem computableCellwise_eq_cellwise (r : RangeRead) (res : Result) + (active : Key → Bool) : + computableCellwise r res active = conflictRangeCellwise r res active := by + unfold computableCellwise conflictRangeCellwise + rw [activeRepsOfC_eq, hullOfActiveRepsC_eq] + +end FormalConflictRange diff --git a/FormalConflictRange/ComputableSearch.lean b/FormalConflictRange/ComputableSearch.lean new file mode 100644 index 0000000..4bbef49 --- /dev/null +++ b/FormalConflictRange/ComputableSearch.lean @@ -0,0 +1,320 @@ +/- +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.FiniteCountDecider + +/-! +# A lazy activity oracle + +The exact oracle `activeComputable` decides activity by enumerating, for every DNF +alternative, one violated constraint, and then testing the resulting finite count +system. That product is correct but needlessly large: for the characterizing DNF +the alternatives share almost all of their constraints. + +This file replaces the eager product by a lazy search for a *hitting set* of +constraints: one constraint from every alternative, but without repeating a +constraint that has already been selected. A partial selection is tested for +count feasibility before it is extended; infeasible prefixes are discarded, since +adding more violated constraints can only make the count system harder. +-/ + +namespace FormalConflictRange + +/-! ## Hitting sets versus choices -/ + +/-- A list of constraints hits an alternative when one of the alternative's +constraints occurs in the list. -/ +def Hits (B : List Constraint) (S : ConstraintSystem) : Prop := + ∃ c ∈ S, c ∈ B + +/-- A list of constraints hits every alternative of a DNF. -/ +def HitsAll (B : List Constraint) (D : ConstraintDNF) : Prop := + ∀ S ∈ D, Hits B S + +/-- Every `ChoosesAll` selection is a hitting set. -/ +theorem hitsAll_of_choosesAll {bad : List Constraint} {D : ConstraintDNF} + (h : ChoosesAll bad D) : HitsAll bad D := by + induction D generalizing bad with + | nil => intro S hS; simp at hS + | cons S Ss ih => + cases bad with + | nil => simp [ChoosesAll] at h + | cons c cs => + rcases h with ⟨hcS, htail⟩ + intro S' hS' + rcases List.mem_cons.mp hS' with rfl | hS' + · exact ⟨c, hcS, by simp⟩ + · obtain ⟨c', hc'S', hc'bad⟩ := ih htail S' hS' + exact ⟨c', hc'S', by simp [hc'bad]⟩ + +/-- A hitting set contains a `ChoosesAll` selection: choose one hit from each +alternative, in order. -/ +theorem exists_choices_subset {D : ConstraintDNF} {B : List Constraint} + (h : HitsAll B D) : + ∃ bad : List Constraint, bad ∈ choices D ∧ ∀ c ∈ bad, c ∈ B := by + induction D with + | nil => exact ⟨[], by simp [choices], by simp⟩ + | cons S Ss ih => + obtain ⟨c, hcS, hcB⟩ := h S (by simp) + obtain ⟨bad, hbad, hsub⟩ := ih (fun S' hS' => h S' (by simp [hS'])) + refine ⟨c :: bad, ?_, ?_⟩ + · simp only [choices, List.mem_flatMap, List.mem_map] + exact ⟨c, hcS, bad, hbad, rfl⟩ + · intro d hd + rcases List.mem_cons.mp hd with rfl | hd + · exact hcB + · exact hsub d hd + +/-! ## Monotonicity of count feasibility -/ + +/-- Adding violated constraints cannot make a count system feasible. -/ +theorem CountFeasible.mono {bounds : List Key} {k : Key} {S : ConstraintSystem} + {bad bad' : List Constraint} (hsub : ∀ c ∈ bad, c ∈ bad') + (h : CountFeasible bounds k S bad') : CountFeasible bounds k S bad := by + obtain ⟨counts, topCount, hfit, hS, hbad⟩ := h + exact ⟨counts, topCount, hfit, hS, fun c hc => hbad c (hsub c hc)⟩ + +/-! ## The lazy search -/ + +/-- Lazy search for a feasible hitting set. The accumulator `acc` already hits +the alternatives processed so far; a new constraint is added only when the +current alternative is not yet hit, and a prefix is abandoned as soon as its +count system is infeasible. -/ +def searchBad (bounds : List Key) (k : Key) (S : ConstraintSystem) : + ConstraintDNF → List Constraint → Bool + | [], acc => countFeasibleBool bounds k S acc + | S' :: Ss, acc => + if S'.any (fun c => decide (c ∈ acc)) then + searchBad bounds k S Ss acc + else + S'.any fun c => + countFeasibleBool bounds k S (c :: acc) && + searchBad bounds k S Ss (c :: acc) + +/-- The search finds a feasible hitting set extending the accumulator. -/ +theorem searchBad_spec (bounds : List Key) (k : Key) (S : ConstraintSystem) + (D : ConstraintDNF) (acc : List Constraint) + (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds) + (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) + (hcoverS : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hcoverD : ∀ S' ∈ D, ∀ c ∈ S', + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hcoverAcc : ∀ c ∈ acc, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) : + searchBad bounds k S D acc = true ↔ + ∃ B : List Constraint, (∀ c ∈ acc, c ∈ B) ∧ HitsAll B D ∧ + CountFeasible bounds k S B := by + induction D generalizing acc with + | nil => + constructor + · intro h + exact ⟨acc, fun _ hc => hc, by intro S' hS'; simp at hS', + (countFeasibleBool_spec bounds k S acc hnoBot hle hcoverS hcoverAcc hk hka).mp h⟩ + · rintro ⟨B, haccB, -, hB⟩ + exact (countFeasibleBool_spec bounds k S acc hnoBot hle hcoverS hcoverAcc hk hka).mpr + (CountFeasible.mono haccB hB) + | cons S' Ss ih => + by_cases hcover : ∃ c ∈ S', c ∈ acc + · have hany : S'.any (fun c => decide (c ∈ acc)) = true := by + rw [List.any_eq_true] + obtain ⟨c, hcS', hcacc⟩ := hcover + exact ⟨c, hcS', by simp [hcacc]⟩ + rw [searchBad, ite_eq_left hany] + obtain ⟨c, hcS', hcacc⟩ := hcover + have hcoverSs : ∀ S'' ∈ Ss, ∀ c ∈ S'', + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := + fun S'' hS'' c hc => hcoverD S'' (by simp [hS'']) c hc + have ih' := ih acc hcoverSs hcoverAcc + constructor + · intro hsearch + obtain ⟨B, haccB, hhits, hfeas⟩ := ih'.mp hsearch + exact ⟨B, haccB, fun S'' hS'' => by + rcases List.mem_cons.mp hS'' with hS | hS'' + · subst S'' + exact ⟨c, hcS', haccB c hcacc⟩ + · exact hhits S'' hS'', hfeas⟩ + · rintro ⟨B, haccB, hhits, hfeas⟩ + exact ih'.mpr ⟨B, haccB, + fun S'' hS'' => hhits S'' (List.mem_cons_of_mem _ hS''), hfeas⟩ + · have hany : S'.any (fun c => decide (c ∈ acc)) = false := by + rw [List.any_eq_false] + intro c hcS' hcacc + exact hcover ⟨c, hcS', by simpa using hcacc⟩ + rw [searchBad, ite_eq_right (by simp [hany])] + constructor + · intro hsearch + rw [List.any_eq_true] at hsearch + obtain ⟨c, hcS', hboth⟩ := hsearch + rw [Bool.and_eq_true] at hboth + obtain ⟨hcfeas, htail⟩ := hboth + have hcoverAcc' : ∀ d ∈ c :: acc, + EndpointCovered d.lo bounds ∧ EndpointCovered d.hi bounds := by + intro d hd + rcases List.mem_cons.mp hd with hdc | hd + · rw [hdc] + exact hcoverD S' (by simp) c hcS' + · exact hcoverAcc d hd + have hfeasAcc : CountFeasible bounds k S (c :: acc) := + (countFeasibleBool_spec bounds k S (c :: acc) hnoBot hle hcoverS + hcoverAcc' hk hka).mp hcfeas + have hcoverSs : ∀ S'' ∈ Ss, ∀ d ∈ S'', + EndpointCovered d.lo bounds ∧ EndpointCovered d.hi bounds := + fun S'' hS'' d hd => hcoverD S'' (by simp [hS'']) d hd + obtain ⟨B, haccB, hhits, hfeas⟩ := + (ih (c :: acc) hcoverSs hcoverAcc').mp htail + exact ⟨B, fun d hd => haccB d (List.mem_cons_of_mem _ hd), + fun S'' hS'' => by + rcases List.mem_cons.mp hS'' with hS | hS'' + · subst S'' + exact ⟨c, hcS', haccB c (by simp)⟩ + · exact hhits S'' hS'', hfeas⟩ + · rintro ⟨B, haccB, hhits, hfeas⟩ + obtain ⟨c, hcS', hcB⟩ := hhits S' (by simp) + have hcacc : c ∉ acc := by + intro hcacc + exact hcover ⟨c, hcS', by simpa using hcacc⟩ + have hsub : ∀ d ∈ c :: acc, d ∈ B := by + intro d hd + rcases List.mem_cons.mp hd with hdc | hd + · rw [hdc] + exact hcB + · exact haccB d hd + have hcoverAcc' : ∀ d ∈ c :: acc, + EndpointCovered d.lo bounds ∧ EndpointCovered d.hi bounds := by + intro d hd + rcases List.mem_cons.mp hd with hdc | hd + · rw [hdc] + exact hcoverD S' (by simp) c hcS' + · exact hcoverAcc d hd + have hcfeas : countFeasibleBool bounds k S (c :: acc) = true := + (countFeasibleBool_spec bounds k S (c :: acc) hnoBot hle hcoverS + hcoverAcc' hk hka).mpr (CountFeasible.mono hsub hfeas) + have hcoverSs : ∀ S'' ∈ Ss, ∀ d ∈ S'', + EndpointCovered d.lo bounds ∧ EndpointCovered d.hi bounds := + fun S'' hS'' d hd => hcoverD S'' (by simp [hS'']) d hd + have htail : searchBad bounds k S Ss (c :: acc) = true := + (ih (c :: acc) hcoverSs hcoverAcc').mpr + ⟨B, fun d hd => by + rcases List.mem_cons.mp hd with hdc | hd + · rw [hdc] + exact hcB + · exact haccB d hd, + fun S'' hS'' => hhits S'' (List.mem_cons_of_mem _ hS''), hfeas⟩ + rw [List.any_eq_true] + exact ⟨c, hcS', by rw [Bool.and_eq_true]; exact ⟨hcfeas, htail⟩⟩ + +/-- On the canonical bounds, the lazy search is exactly the eager `choices` +enumeration. -/ +theorem searchBad_choices (bounds : List Key) (k : Key) (S : ConstraintSystem) + (D : ConstraintDNF) + (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds) + (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) + (hcoverS : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hcoverD : ∀ S' ∈ D, ∀ c ∈ S', + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) : + searchBad bounds k S D [] = true ↔ + ∃ bad : List Constraint, bad ∈ choices D ∧ + countFeasibleBool bounds k S bad = true := by + constructor + · intro hsearch + obtain ⟨B, -, hhits, hfeas⟩ := + (searchBad_spec bounds k S D [] hnoBot hle hk hka hcoverS hcoverD (by simp)).mp hsearch + obtain ⟨bad, hbad, hsub⟩ := exists_choices_subset hhits + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem (mem_choices.mp hbad) hc + exact hcoverD S' hS' c hcS' + exact ⟨bad, hbad, + (countFeasibleBool_spec bounds k S bad hnoBot hle hcoverS hcoverBad hk hka).mpr + (CountFeasible.mono hsub hfeas)⟩ + · rintro ⟨bad, hbad, hfeas⟩ + have hchooses : ChoosesAll bad D := mem_choices.mp hbad + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc + exact hcoverD S' hS' c hcS' + exact (searchBad_spec bounds k S D [] hnoBot hle hk hka hcoverS hcoverD (by simp)).mpr + ⟨bad, by simp, hitsAll_of_choosesAll hchooses, + (countFeasibleBool_spec bounds k S bad hnoBot hle hcoverS hcoverBad hk hka).mp hfeas⟩ + +/-! ## The lazy oracle and the simple algorithm -/ + +/-- The lazy exact activity oracle. It has the same decision procedure as +`activeComputable`, but searches for a feasible hitting set instead of +enumerating the full product of one constraint per alternative. -/ +def activeComputableFast (r : RangeRead) (res : Result) (k : Key) : Bool := + let D := characterizingDNF r res + let bounds := activeBounds D k + withinLimitBool r res.keys && + D.any fun S => searchBad bounds k S D [] + +/-- The lazy oracle is exact on every readable pair. -/ +theorem activeComputableFast_spec {r : RangeRead} {res : Result} + (hread : Readable r res) : + ActiveDecider r res (activeComputableFast r res) := by + intro k + let D := characterizingDNF r res + let bounds := activeBounds D k + have hnoBot : ([] : Key) ∉ bounds := not_mem_activeBounds_bot D k + have hle : BoundsLe ([] : Key) bounds := activeBounds_boundsLe D k + have hcoverD : ∀ S' ∈ D, ∀ c ∈ S', + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro S' hS' c hc + exact dnfEndpointCovered_activeBounds hS' hc + have hk : k = ([] : Key) ∨ k ∈ bounds := by + by_cases hkbot : k = ([] : Key) + · exact Or.inl hkbot + · exact Or.inr (by + rw [mem_activeBounds] + exact Or.inr (Or.inr ⟨hkbot, rfl⟩)) + have hka : keyAfter k ∈ bounds := by + rw [mem_activeBounds] + exact Or.inl rfl + rw [activeComputableFast] + rw [Bool.and_eq_true, withinLimitBool_eq, List.any_eq_true] + constructor + · rintro ⟨hw, S, hS, hsearch⟩ + obtain ⟨bad, hbad, hfeas⟩ := + (searchBad_choices bounds k S D hnoBot hle hk hka (hcoverD S hS) hcoverD).mp hsearch + have hchooses : ChoosesAll bad D := mem_choices.mp hbad + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc + exact hcoverD S' hS' c hcS' + have hcount : CountFeasible bounds k S bad := + (countFeasibleBool_spec bounds k S bad hnoBot hle (hcoverD S hS) hcoverBad hk hka).mp + hfeas + exact (flipWitness_iff_exists_counts_canonical (D := D) + (characterizesDNF_characterizingDNF hread)).mpr + ⟨hw, S, hS, bad, hchooses, hcount⟩ + · intro hw + rcases (flipWitness_iff_exists_counts_canonical (D := D) + (characterizesDNF_characterizingDNF hread)).mp hw with + ⟨hwithin, S, hS, bad, hchooses, hcount⟩ + refine ⟨hwithin, ⟨S, hS, ?_⟩⟩ + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc + exact hcoverD S' hS' c hcS' + have hfeas : countFeasibleBool bounds k S bad = true := + (countFeasibleBool_spec bounds k S bad hnoBot hle (hcoverD S hS) hcoverBad hk hka).mpr + hcount + exact (searchBad_choices bounds k S D hnoBot hle hk hka (hcoverD S hS) hcoverD).mpr + ⟨bad, mem_choices.mpr hchooses, hfeas⟩ + +/-- On a readable pair, the lazy oracle computes exactly the eager oracle's +answer. -/ +theorem activeComputableFast_eq {r : RangeRead} {res : Result} + (hread : Readable r res) (k : Key) : + activeComputableFast r res k = activeComputable r res k := by + apply Bool.eq_iff_iff.mpr + rw [activeComputableFast_spec hread k, activeComputable_spec hread k] + +end FormalConflictRange diff --git a/FormalConflictRange/ConflictRange.lean b/FormalConflictRange/ConflictRange.lean new file mode 100644 index 0000000..f880875 --- /dev/null +++ b/FormalConflictRange/ConflictRange.lean @@ -0,0 +1,388 @@ +/- +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.RangeRead + +/-! +# Conflict ranges + +A `ConflictRange` is a half-open interval of keys `[begin, end_)`, independent +of any database. Both ends are `WithTop Key`: the lower end `⊥` is the empty +key, an ordinary key below every other, so an unbounded lower end needs no +sentinel, while the upper end `⊤` denotes an unbounded range past every key. A +key lies in the range when `begin ≤ k < end_`. + +## Why both ends are `WithTop` + +Both ends share one type, so a conflict range is uniformly an interval in +`WithTop Key`, matching `RangeRead.inRange` and resolved selector targets; in +particular the keys read by `RangeRead.readAll` fill the range `[⊥, ⊤)`. The +bottom does no work of its own: `begin = ⊥` is exactly the empty key, which is +how "unbounded below" is spelled and how a range genuinely begins at a key — +as the necessary-key analysis of `FormalConflictRange.Sound` shows, the empty +key itself can be necessary. The useless corners are symmetric: `end_ = ⊥` and +`begin = ⊤` both denote nothing but the empty range, since no key lies strictly +below the empty key or at or above `⊤`. Membership remains a property of +genuine keys, as the necessary-key analysis requires: a necessary key must be +contained in every sound range, and only genuine keys can be. +-/ + +namespace FormalConflictRange + +/-- A conflict range is a half-open interval of keys `[begin, end_)`, whose +lower end may be the empty key (the bottom of `WithTop Key`) for an unbounded +range and whose upper end may be `⊤` for an unbounded range. -/ +structure ConflictRange where + begin : WithTop Key + end_ : WithTop Key + +namespace ConflictRange + +/-- A key falls in a conflict range when `begin ≤ k < end_`. -/ +def mem (cr : ConflictRange) (k : Key) : Prop := + cr.begin ≤ (k : WithTop Key) ∧ (k : WithTop Key) < cr.end_ + +instance : Membership Key ConflictRange := ⟨mem⟩ + +/-- The set of keys lying in a conflict range. -/ +def toSet (cr : ConflictRange) : Set Key := {k | k ∈ cr} + +/-- Membership in a conflict range is membership in its set of keys. -/ +@[simp] theorem mem_toSet {cr : ConflictRange} {k : Key} : k ∈ cr.toSet ↔ k ∈ cr := Iff.rfl + +/-- A nonempty range is determined by its set of keys. The lower end is the +least key in the set, and the upper end is the first key excluded above it (or +`⊤` when no key is excluded). -/ +theorem eq_of_toSet_eq_of_nonempty {cr cr' : ConflictRange} + (h : cr.toSet = cr'.toSet) (hne : ∃ k : Key, k ∈ cr) : cr = cr' := by + obtain ⟨k, hk⟩ := hne + have hk'_set : k ∈ cr'.toSet := by + rw [← h] + exact mem_toSet.mpr hk + have hk' : k ∈ cr' := mem_toSet.mp hk'_set + have hb : cr.begin ≤ cr'.begin := by + by_contra hnot + have hlt : cr'.begin < cr.begin := lt_of_not_ge hnot + cases hb' : cr'.begin with + | top => + rw [hb'] at hlt + exact absurd hlt (not_lt.mpr (le_top)) + | coe c => + have hc_le_k : (c : WithTop Key) ≤ (k : WithTop Key) := by + have hkbegin := hk'.1 + rw [hb'] at hkbegin + exact hkbegin + have hc' : c ∈ cr' := by + constructor + · rw [hb'] + · exact lt_of_le_of_lt hc_le_k hk'.2 + have hc'_set : c ∈ cr'.toSet := mem_toSet.mpr hc' + have hltc : (c : WithTop Key) < cr.begin := by + rw [hb'] at hlt + exact hlt + have hc_not : ¬ c ∈ cr := by + intro hmem + exact absurd hmem.1 (not_le.mpr hltc) + have hc_set : c ∈ cr.toSet := by + rw [h] + exact hc'_set + exact hc_not (mem_toSet.mp hc_set) + have hb' : cr'.begin ≤ cr.begin := by + by_contra hnot + have hlt : cr.begin < cr'.begin := lt_of_not_ge hnot + cases hb : cr.begin with + | top => + rw [hb] at hlt + exact absurd hlt (not_lt.mpr (le_top)) + | coe c => + have hc_le_k : (c : WithTop Key) ≤ (k : WithTop Key) := by + have hkbegin := hk.1 + rw [hb] at hkbegin + exact hkbegin + have hc : c ∈ cr := by + constructor + · rw [hb] + · exact lt_of_le_of_lt hc_le_k hk.2 + have hc_set : c ∈ cr.toSet := mem_toSet.mpr hc + have hltc : (c : WithTop Key) < cr'.begin := by + rw [hb] at hlt + exact hlt + have hc_not : ¬ c ∈ cr' := by + intro hmem + exact absurd hmem.1 (not_le.mpr hltc) + have hc'_set : c ∈ cr'.toSet := by + rw [← h] + exact hc_set + exact hc_not (mem_toSet.mp hc'_set) + have he : cr.end_ ≤ cr'.end_ := by + by_contra hnot + have hlt : cr'.end_ < cr.end_ := lt_of_not_ge hnot + cases he' : cr'.end_ with + | top => + rw [he'] at hlt + exact absurd hlt (not_lt.mpr (le_top)) + | coe c => + have hk_lt_c : (k : WithTop Key) < (c : WithTop Key) := by + have hkend := hk'.2 + rw [he'] at hkend + exact hkend + have hc : c ∈ cr := by + constructor + · exact le_trans hk.1 (le_of_lt hk_lt_c) + · rw [he'] at hlt + exact hlt + have hc_set : c ∈ cr.toSet := mem_toSet.mpr hc + have hc_not : ¬ c ∈ cr' := by + intro hmem + have hltc := hmem.2 + rw [he'] at hltc + exact absurd hltc (lt_irrefl _) + have hc'_set : c ∈ cr'.toSet := by + rw [← h] + exact hc_set + exact hc_not (mem_toSet.mp hc'_set) + have he' : cr'.end_ ≤ cr.end_ := by + by_contra hnot + have hlt : cr.end_ < cr'.end_ := lt_of_not_ge hnot + cases he : cr.end_ with + | top => + rw [he] at hlt + exact absurd hlt (not_lt.mpr (le_top)) + | coe c => + have hk_lt_c : (k : WithTop Key) < (c : WithTop Key) := by + have hkend := hk.2 + rw [he] at hkend + exact hkend + have hc' : c ∈ cr' := by + constructor + · exact le_trans hk'.1 (le_of_lt hk_lt_c) + · rw [he] at hlt + exact hlt + have hc'_set : c ∈ cr'.toSet := mem_toSet.mpr hc' + have hc_not : ¬ c ∈ cr := by + intro hmem + have hltc := hmem.2 + rw [he] at hltc + exact absurd hltc (lt_irrefl _) + have hc_set : c ∈ cr.toSet := by + rw [h] + exact hc'_set + exact hc_not (mem_toSet.mp hc_set) + cases cr with + | mk b e => + cases cr' with + | mk b' e' => + have hb_eq : b = b' := le_antisymm hb hb' + have he_eq : e = e' := le_antisymm he he' + rw [hb_eq, he_eq] + +/-- A conflict range is empty when its end is at or below its begin. -/ +def IsEmpty (cr : ConflictRange) : Prop := cr.end_ ≤ cr.begin + +/-- The keys of `db` lying in the conflict range, ascending. -/ +def keys (cr : ConflictRange) (db : DB) : List Key := + db.keys.filter + (fun (k : Key) => decide (cr.begin ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < cr.end_)) + +/-- Filtering a database by a conflict range is filtering by its endpoints. -/ +theorem keys_eq_inRange (cr : ConflictRange) (db : DB) : + cr.keys db = RangeRead.inRange db.keys cr.begin cr.end_ := rfl + +/-- A key is in the range's keys exactly when it is stored and in the range. -/ +theorem mem_keys_iff {cr : ConflictRange} {db : DB} {k : Key} : + k ∈ cr.keys db ↔ k ∈ db.keys ∧ k ∈ cr := by + change k ∈ db.keys.filter + (fun (k : Key) => + decide (cr.begin ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < cr.end_)) ↔ + k ∈ db.keys ∧ cr.begin ≤ (k : WithTop Key) ∧ (k : WithTop Key) < cr.end_ + rw [List.mem_filter, decide_eq_true_eq] + +/-- An empty conflict range contains no keys. -/ +theorem not_mem_of_isEmpty {cr : ConflictRange} (h : cr.IsEmpty) (k : Key) : k ∉ cr := by + intro hk + exact absurd hk.2 (not_lt.mpr (le_trans h hk.1)) + +/-- The keys of an empty conflict range are empty. -/ +theorem keys_eq_nil_of_isEmpty {cr : ConflictRange} (h : cr.IsEmpty) (db : DB) : + cr.keys db = [] := by + rw [List.eq_nil_iff_forall_not_mem] + intro k hk + rw [mem_keys_iff] at hk + exact not_mem_of_isEmpty h k hk.2 + +/-- The range's keys are a sublist of the database's keys. -/ +theorem keys_sublist (cr : ConflictRange) (db : DB) : (cr.keys db).Sublist db.keys := + List.filter_sublist + +/-- The range's keys are duplicate-free. -/ +theorem keys_nodup (cr : ConflictRange) (db : DB) : (cr.keys db).Nodup := + (keys_sublist cr db).nodup db.sorted.pairwise.nodup + +/-- The range's keys are strictly increasing. -/ +theorem keys_pairwise (cr : ConflictRange) (db : DB) : + List.Pairwise (· < ·) (cr.keys db) := + db.sorted.pairwise.sublist (keys_sublist cr db) + +/-- A key in the range lies between the endpoints. -/ +theorem mem_iff {cr : ConflictRange} {k : Key} : + k ∈ cr ↔ cr.begin ≤ k ∧ (k : WithTop Key) < cr.end_ := + Iff.rfl + +/-- A conflict range contains every key between any two keys it contains. -/ +theorem mem_of_between {cr : ConflictRange} {a b k : Key} + (ha : a ∈ cr) (hb : b ∈ cr) (hak : a ≤ k) (hkb : k ≤ b) : k ∈ cr := + ⟨le_trans ha.1 (by exact_mod_cast hak), lt_of_le_of_lt (by exact_mod_cast hkb) hb.2⟩ + +/-- The intersection of two conflict ranges, as a conflict range: the interval +from the larger lower end to the smaller upper end. -/ +def inter (cr₁ cr₂ : ConflictRange) : ConflictRange := + ⟨max cr₁.begin cr₂.begin, min cr₁.end_ cr₂.end_⟩ + +/-- A key lies in an intersection of conflict ranges exactly when it lies in both. +This is the only property of `inter` the soundness analysis needs: intersecting +ranges intersects their stored keys. -/ +theorem mem_inter_iff {cr₁ cr₂ : ConflictRange} {k : Key} : + k ∈ inter cr₁ cr₂ ↔ k ∈ cr₁ ∧ k ∈ cr₂ := by + change (max cr₁.begin cr₂.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < min cr₁.end_ cr₂.end_) ↔ + ((cr₁.begin ≤ (k : WithTop Key) ∧ (k : WithTop Key) < cr₁.end_) ∧ + (cr₂.begin ≤ (k : WithTop Key) ∧ (k : WithTop Key) < cr₂.end_)) + rw [max_le_iff, lt_min_iff] + tauto + +/-- Erasing an interval disjoint from a range leaves the range's stored keys +unchanged. The interval `[a, b)` is disjoint from `cr` when it lies entirely +below `cr.begin` (`cr.begin ≤ a` is excluded) or entirely at or above `cr.end_`. +This is the interval analogue of `keys_erase_of_not_mem`. -/ +theorem keys_eraseInterval_of_disjoint (cr : ConflictRange) (db : DB) {a b : Key} + (hdisj : ∀ x, a ≤ x → x < b → x ∉ cr) : + cr.keys (db.eraseInterval (↑a) (↑b)) = cr.keys db := by + unfold keys + rw [DB.eraseInterval_keys, List.filter_filter] + apply List.filter_congr + intro x _ + by_cases hp : cr.begin ≤ (x : WithTop Key) ∧ (x : WithTop Key) < cr.end_ + · have hq : ¬ (a ≤ x ∧ x < b) := fun h => hdisj x h.1 h.2 hp + simp [hp, hq] + · simp [hp] + +/-- Inserting an interval disjoint from a range leaves the range's stored keys +unchanged. This is the interval analogue of `keys_insert_of_not_mem`: the keys +below `a` and at or above `b` are kept, and the inserted successor chain lies in +`[a, b)` and so outside `cr`. -/ +theorem keys_insertInterval_of_disjoint (cr : ConflictRange) (db : DB) {a b : Key} + (hab : a ≤ b) (n : ℕ) (hn : ∀ i < n, chain a i < b) + (hdisj : ∀ x, a ≤ x → x < b → x ∉ cr) : + cr.keys (db.insertInterval a b hab n hn) = cr.keys db := by + unfold keys + rw [DB.insertInterval_keys, List.filter_append, List.filter_append] + have hrest : List.filter (fun (k : Key) => decide (cr.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < cr.end_)) (List.map (chain a) (List.range n)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hcon + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + rw [decide_eq_true_eq] at hcon + exact hdisj (chain a i) (chain_le a i) (hn i (List.mem_range.mp hi)) hcon + rw [hrest, List.append_nil] + apply List.SortedLT.eq_of_mem_iff + · rw [List.sortedLT_append] + refine ⟨?_, ?_, ?_⟩ + · exact List.sortedLT_iff_pairwise.mpr ((db.sorted.pairwise.filter _).filter _) + · exact List.sortedLT_iff_pairwise.mpr ((db.sorted.pairwise.filter _).filter _) + · intro x hx y hy + rw [List.mem_filter] at hx hy + exact lt_of_lt_of_le + (of_decide_eq_true (List.mem_filter.mp hx.1).2) + (le_trans hab (of_decide_eq_true (List.mem_filter.mp hy.1).2)) + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · intro x + constructor + · intro hx + simp only [List.mem_append, List.mem_filter, decide_eq_true_eq] at hx ⊢ + rcases hx with ⟨⟨hx, hp⟩, hr⟩ | ⟨⟨hx, hq⟩, hr⟩ + · exact ⟨hx, hr⟩ + · exact ⟨hx, hr⟩ + · intro hx + simp only [List.mem_append, List.mem_filter, decide_eq_true_eq] at hx ⊢ + obtain ⟨hx, hr⟩ := hx + have hpq : x < a ∨ b ≤ x := by + rcases lt_or_ge x a with h | h + · exact Or.inl h + · exact Or.inr (le_of_not_gt fun hxb => hdisj x h hxb hr) + rcases hpq with h | h + · exact Or.inl ⟨⟨hx, h⟩, hr⟩ + · exact Or.inr ⟨⟨hx, h⟩, hr⟩ + +/-- Erasing a key outside a range leaves the range's stored keys unchanged. This +is the `[k, keyAfter k)` case of `keys_eraseInterval_of_disjoint`, via +`DB.erase_eq_eraseInterval`. -/ +theorem keys_erase_of_not_mem (cr : ConflictRange) (db : DB) {k : Key} + (hk : k ∉ cr) : cr.keys (db.erase k) = cr.keys db := by + rw [DB.erase_eq_eraseInterval] + exact keys_eraseInterval_of_disjoint cr db fun x hkx hxk => by + have hx : x = k := le_antisymm ((lt_keyAfter_iff x k).mp hxk) hkx + rw [hx]; exact hk + +/-- Inserting a key outside a range leaves the range's stored keys unchanged. This +is the one-element-chain case of `keys_insertInterval_of_disjoint`, via +`DB.insert_eq_insertInterval`. -/ +theorem keys_insert_of_not_mem (cr : ConflictRange) (db : DB) {k : Key} + (hdb : k ∉ db.keys) (hk : k ∉ cr) : cr.keys (db.insert k hdb) = cr.keys db := by + rw [DB.insert_eq_insertInterval db k hdb] + exact keys_insertInterval_of_disjoint cr db (le_of_lt (keyAfter_self_lt k)) 1 + (by intro i _; interval_cases i + simp only [chain, List.replicate_zero, List.append_nil] + exact keyAfter_self_lt k) + fun x hkx hxk => by + have hx : x = k := le_antisymm ((lt_keyAfter_iff x k).mp hxk) hkx + rw [hx]; exact hk + +end ConflictRange + +/-- A conflict range is a subrange of another when every key lying in the first +also lies in the second. -/ +def Subrange (cr' cr : ConflictRange) : Prop := ∀ k, k ∈ cr' → k ∈ cr + +/-- A conflict range is a proper subrange when it is a subrange and the reverse +containment fails, i.e. the larger range contains a key the smaller does not. -/ +def ProperSubrange (cr' cr : ConflictRange) : Prop := + Subrange cr' cr ∧ ¬ Subrange cr cr' + +namespace Subrange + +theorem refl (cr : ConflictRange) : Subrange cr cr := fun _ hk => hk + +theorem trans {cr₁ cr₂ cr₃ : ConflictRange} (h₁ : Subrange cr₁ cr₂) + (h₂ : Subrange cr₂ cr₃) : Subrange cr₁ cr₃ := + fun k hk => h₂ k (h₁ k hk) + +end Subrange + +/-- A conflict range is a subrange of another exactly when its set of keys is a +subset of the other's. -/ +theorem subrange_iff_subset {cr' cr : ConflictRange} : + Subrange cr' cr ↔ cr'.toSet ⊆ cr.toSet := + Iff.rfl + +/-- A range whose endpoints lie inside another's is a subrange. -/ +theorem subrange_of_le {cr' cr : ConflictRange} (hb : cr.begin ≤ cr'.begin) + (he : cr'.end_ ≤ cr.end_) : Subrange cr' cr := + fun _ hk => ⟨le_trans hb hk.1, lt_of_lt_of_le hk.2 he⟩ + +/-- A key in the larger range but not the smaller refutes the reverse +containment, making the smaller a proper subrange. -/ +theorem not_subrange_of_witness {cr' cr : ConflictRange} (k : Key) + (hk : k ∈ cr) (hk' : k ∉ cr') : ¬ Subrange cr cr' := + fun h => hk' (h k hk) + +/-- A range contained by another, with a witness key missing from the smaller, +is a proper subrange. -/ +theorem properSubrange_of_le_of_witness {cr' cr : ConflictRange} + (hb : cr.begin ≤ cr'.begin) (he : cr'.end_ ≤ cr.end_) (k : Key) + (hk : k ∈ cr) (hk' : k ∉ cr') : ProperSubrange cr' cr := + ⟨subrange_of_le hb he, not_subrange_of_witness k hk hk'⟩ + +end FormalConflictRange diff --git a/FormalConflictRange/CountBridge.lean b/FormalConflictRange/CountBridge.lean new file mode 100644 index 0000000..ab62b35 --- /dev/null +++ b/FormalConflictRange/CountBridge.lean @@ -0,0 +1,617 @@ +/- +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.General +import FormalConflictRange.Prototype +import FormalConflictRange.Realization + +/-! +# From databases to finite interval counts + +This file connects the database-realization theorem of `Realization.lean` to +the difference-constraint systems of the prototype. A system is satisfiable by +a database exactly when a finite list of interval counts fits the interval +capacities and makes every constraint hold under the cumulative count +environment. The remaining arithmetic is therefore entirely on natural-number +interval counts, which is the shape an active-cell decider consumes. +-/ + +namespace FormalConflictRange + +def EndpointCovered (e : WithTop Key) (bounds : List Key) : Prop := + e = (([] : Key) : WithTop Key) ∨ e = ⊤ ∨ ∃ b ∈ bounds, e = (b : WithTop Key) + +/-- The count environment satisfies a constraint system when every difference +constraint holds under `evalCount`. -/ +def SystemHoldsWithCounts (S : ConstraintSystem) (bounds : List Key) + (counts : List ℕ) (topCount : ℕ) : Prop := + ∀ c ∈ S, evalCount bounds counts topCount c.hi - + evalCount bounds counts topCount c.lo ≤ c.bound + +/-- `CountsFit` implies that the endpoint chain is nondecreasing. -/ +theorem boundsLe_of_CountsFit {start : Key} {bounds : List Key} {counts : List ℕ} + (h : CountsFit start bounds counts) : BoundsLe start bounds := by + induction bounds generalizing start counts with + | nil => trivial + | cons b bs ih => + cases counts with + | nil => simp [CountsFit] at h + | cons d ds => + rcases h with ⟨hstartb, -, htail⟩ + exact ⟨hstartb, ih htail⟩ + +/-- The count environment agrees with the database on every covered endpoint. -/ +theorem evalCount_eq_cnt_of_covered {db : DB} {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} (h : CountsAt db 0 bounds counts) + (htop : cnt db ⊤ = counts.sum + topCount) (hnoBot : ([] : Key) ∉ bounds) + {e : WithTop Key} (he : EndpointCovered e bounds) : + evalCount bounds counts topCount e = cnt db e := by + rcases he with rfl | rfl | ⟨b, hb, rfl⟩ + · exact evalCount_bot + · exact evalCount_top htop + · exact evalCount_coe_of_mem h hb (by + intro hbBot + exact hnoBot (hbBot ▸ hb)) + +/-- **Satisfiability bridge.** A constraint system is satisfied by some database +exactly when its finite count relaxation is satisfiable: choose consecutive +interval counts that fit the interval capacities and make every constraint hold +under the cumulative count environment. + +This turns the existential over databases into a finite count problem. It is +the bridge used by an active-cell decider; the remaining arithmetic is entirely +on `ℕ`-valued interval counts. -/ +theorem exists_db_systemHoldsAt_iff_counts (S : ConstraintSystem) (bounds : List Key) + (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds) + (hcover : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) : + (∃ db : DB, SystemHoldsAt S db) ↔ + ∃ counts : List ℕ, ∃ topCount : ℕ, + CountsFit ([] : Key) bounds counts ∧ + SystemHoldsWithCounts S bounds counts topCount := by + constructor + · rintro ⟨db, hS⟩ + let counts : List ℕ := intervalCounts db ([] : Key) bounds + have hfit : CountsFit ([] : Key) bounds counts := + intervalCounts_fit db ([] : Key) bounds hle + have hcounts : CountsAt db 0 bounds counts := + countsAt_intervalCounts db ([] : Key) bounds hle 0 (by simp [cnt_nil]) + have hsum : counts.sum ≤ cnt db ⊤ := intervalCounts_sum_le_top db bounds hle + let topCount : ℕ := cnt db ⊤ - counts.sum + have htop : cnt db ⊤ = counts.sum + topCount := by + dsimp [topCount] + omega + refine ⟨counts, topCount, hfit, ?_⟩ + intro c hc + have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).2 + have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).1 + rw [hhi, hlo] + exact hS c hc + · rintro ⟨counts, topCount, hfit, hholds⟩ + obtain ⟨db, hcounts, htop⟩ := exists_db_realizing_counts bounds counts topCount hfit + refine ⟨db, ?_⟩ + intro c hc + have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).2 + have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).1 + change (cnt db c.hi : ℤ) - (cnt db c.lo : ℤ) ≤ c.bound + rw [← hhi, ← hlo] + exact hholds c hc + +/-! ## Toggling one key in the count environment -/ + +/-- The interval `[k, keyAfter k)` contains exactly `k` when it is stored and +nothing otherwise. -/ +theorem between_keyAfter_length (db : DB) (k : Key) : + (between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key)).length = + if k ∈ db.keys then 1 else 0 := by + set l := between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) with hl + have hnodup : l.Nodup := db.nodup.filter _ + have hsubset : l ⊆ [k] := by + intro x hx + simp only [hl, between, List.mem_filter, decide_eq_true_eq] at hx + have hxk : x = k := eq_of_le_of_lt_keyAfter (WithTop.coe_le_coe.mp hx.2.1) + (WithTop.coe_lt_coe.mp hx.2.2) + simp [hxk] + have hle : l.length ≤ 1 := by + have := hnodup.length_le_of_subset hsubset + simpa using this + by_cases hk : k ∈ db.keys + · have hmem : k ∈ l := by + simp [hl, between, hk, keyAfter_self_lt] + have hpos : 0 < l.length := List.length_pos_of_mem hmem + rw [ite_eq_left hk] + omega + · have hnil : l = [] := by + rw [hl, between, List.filter_eq_nil_iff] + intro x hx hcon + simp only [decide_eq_true_eq] at hcon + have hxmem : x ∈ db.keys := by simpa using hx + have hxk : x = k := eq_of_le_of_lt_keyAfter (WithTop.coe_le_coe.mp hcon.1) + (WithTop.coe_lt_coe.mp hcon.2) + exact hk (hxk ▸ hxmem) + simp [hnil, hk] + +/-- The difference of the counts at `k` and `keyAfter k` is the indicator of +`k` being stored. -/ +theorem cnt_keyAfter_sub_cnt (db : DB) (k : Key) : + (cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) - cnt db (k : WithTop Key) = + if k ∈ db.keys then 1 else 0 := by + have hle : (k : WithTop Key) ≤ ((keyAfter k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k)) + have hmono : cnt db (k : WithTop Key) ≤ cnt db ((keyAfter k : Key) : WithTop Key) := + cnt_le_cnt_of_le db hle + have hcnt := cnt_sub_eq_between_length (db := db) hle + have hlen := between_keyAfter_length db k + rw [← Nat.cast_sub hmono, hcnt, hlen] + by_cases hk : k ∈ db.keys <;> simp [hk] + +/-- The additive change in a count when the membership of `k` is toggled. The +variable `x` is the indicator that `k` is stored. -/ +def toggleShift (bounds : List Key) (counts : List ℕ) (topCount : ℕ) (k : Key) + (e : WithTop Key) : ℤ := + let x : ℤ := evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) + if (k : WithTop Key) < e then 1 - 2 * x else 0 + +/-- The count environment after toggling `k`. -/ +def evalCountToggled (bounds : List Key) (counts : List ℕ) (topCount : ℕ) (k : Key) + (e : WithTop Key) : ℤ := + evalCount bounds counts topCount e + + toggleShift bounds counts topCount k e + +/-- The toggled count environment agrees with the toggled database. -/ +theorem evalCountToggled_eq_cnt_toggle {db : DB} {bounds : List Key} + {counts : List ℕ} {topCount : ℕ} (hcounts : CountsAt db 0 bounds counts) + (htop : cnt db ⊤ = counts.sum + topCount) (hnoBot : ([] : Key) ∉ bounds) + {k : Key} (hk : k = ([] : Key) ∨ k ∈ bounds) + (hka : keyAfter k ∈ bounds) {e : WithTop Key} (he : EndpointCovered e bounds) : + evalCountToggled bounds counts topCount k e = cnt (toggle db k) e := by + have hka_bot : keyAfter k ≠ ([] : Key) := by + intro h + have : (keyAfter k).length = 0 := by simp [h] + simp [keyAfter] at this + have he_cnt := evalCount_eq_cnt_of_covered hcounts htop hnoBot he + have hka_cnt : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) = + cnt db ((keyAfter k : Key) : WithTop Key) := + evalCount_coe_of_mem hcounts hka hka_bot + have hk_cnt : evalCount bounds counts topCount (k : WithTop Key) = cnt db (k : WithTop Key) := by + rcases hk with rfl | hk + · exact evalCount_bot + · exact evalCount_coe_of_mem hcounts hk (by + intro h + exact hnoBot (h ▸ hk)) + have hx : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = + if k ∈ db.keys then 1 else 0 := by + rw [hka_cnt, hk_cnt] + exact cnt_keyAfter_sub_cnt db k + unfold evalCountToggled toggleShift + rw [he_cnt, hx] + unfold toggle + by_cases hmem : k ∈ db.keys + · rw [dite_eq_left hmem, cnt_erase_eq db k hmem e] + by_cases hlt : (k : WithTop Key) < e + · simp [hlt, hmem] + try ring + · simp [hlt] + try ring + · rw [dite_eq_right hmem, cnt_insert_eq db k hmem e] + by_cases hlt : (k : WithTop Key) < e + · simp [hlt, hmem] + try ring + · simp [hlt] + try ring + +/-- **One toggle witness in finite count form.** A database satisfying a system +`S` while violating every constraint in a chosen list `bad` after toggling `k` +exists exactly when the finite count relaxation has a solution that satisfies +`S` and makes every toggled constraint in `bad` fail. The list `bad` is the +finite choice of one violated constraint per DNF alternative. -/ +theorem exists_db_systemHoldsAt_and_violates_iff_counts + (S : ConstraintSystem) (bad : List Constraint) (bounds : List Key) + (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds) + (hcoverS : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hcoverBad : ∀ c ∈ bad, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + {k : Key} (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) : + (∃ db : DB, SystemHoldsAt S db ∧ ∀ c ∈ bad, ¬ c.HoldsAt (toggle db k)) ↔ + ∃ counts : List ℕ, ∃ topCount : ℕ, + CountsFit ([] : Key) bounds counts ∧ + SystemHoldsWithCounts S bounds counts topCount ∧ + ∀ c ∈ bad, c.bound < + evalCountToggled bounds counts topCount k c.hi - + evalCountToggled bounds counts topCount k c.lo := by + constructor + · rintro ⟨db, hS, hbad⟩ + let counts : List ℕ := intervalCounts db ([] : Key) bounds + have hfit : CountsFit ([] : Key) bounds counts := + intervalCounts_fit db ([] : Key) bounds hle + have hcounts : CountsAt db 0 bounds counts := + countsAt_intervalCounts db ([] : Key) bounds hle 0 (by simp [cnt_nil]) + have hsum : counts.sum ≤ cnt db ⊤ := intervalCounts_sum_le_top db bounds hle + let topCount : ℕ := cnt db ⊤ - counts.sum + have htop : cnt db ⊤ = counts.sum + topCount := by + dsimp [topCount] + omega + refine ⟨counts, topCount, hfit, ?_, ?_⟩ + · intro c hc + have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).2 + have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).1 + rw [hhi, hlo] + exact hS c hc + · intro c hc + have hhi := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka + (hcoverBad c hc).2 + have hlo := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka + (hcoverBad c hc).1 + have hnc := hbad c hc + change ¬ ((cnt (toggle db k) c.hi : ℤ) - + (cnt (toggle db k) c.lo : ℤ) ≤ c.bound) at hnc + rw [hhi, hlo] + omega + · rintro ⟨counts, topCount, hfit, hS, hbad⟩ + obtain ⟨db, hcounts, htop⟩ := exists_db_realizing_counts bounds counts topCount hfit + refine ⟨db, ?_, ?_⟩ + · intro c hc + have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).2 + have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).1 + change (cnt db c.hi : ℤ) - (cnt db c.lo : ℤ) ≤ c.bound + rw [← hhi, ← hlo] + exact hS c hc + · intro c hc + have hhi := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka + (hcoverBad c hc).2 + have hlo := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka + (hcoverBad c hc).1 + have hnc := hbad c hc + change ¬ ((cnt (toggle db k) c.hi : ℤ) - + (cnt (toggle db k) c.lo : ℤ) ≤ c.bound) + rw [← hhi, ← hlo] + omega + +/-- A finite choice of one violated constraint from each DNF alternative. -/ +def ViolatesAll : List Constraint → ConstraintDNF → DB → Prop + | [], [], _ => True + | c :: cs, S :: Ss, db => c ∈ S ∧ ¬ c.HoldsAt db ∧ ViolatesAll cs Ss db + | _, _, _ => False + +/-- A DNF fails exactly when one can choose a violated constraint from every +alternative. This is the finite disjunction used by the count encoding. -/ +theorem not_anyHolds_iff_exists_violating_choice {D : ConstraintDNF} {db : DB} : + ¬ AnyHolds D db ↔ ∃ bad : List Constraint, ViolatesAll bad D db := by + induction D with + | nil => + constructor + · intro _ + exact ⟨[], by trivial⟩ + · intro _ h + simp [AnyHolds] at h + | cons S Ss ih => + cases S with + | nil => + have hAny : AnyHolds ([] :: Ss) db := by + refine ⟨[], by simp, ?_⟩ + intro c hc + simp at hc + constructor + · intro h + exact absurd hAny h + · rintro ⟨bad, hbad⟩ + cases bad with + | nil => + unfold ViolatesAll at hbad + exact False.elim hbad + | cons b bs => + unfold ViolatesAll at hbad + exact False.elim (by simpa using hbad.1) + | cons c cs => + have hAny : AnyHolds ((c :: cs) :: Ss) db ↔ + SystemHoldsAt (c :: cs) db ∨ AnyHolds Ss db := by + simp [AnyHolds, exists_eq_or_imp] + constructor + · intro h + rw [hAny] at h + have hS : ¬ SystemHoldsAt (c :: cs) db := fun hh => h (Or.inl hh) + have hTail : ¬ AnyHolds Ss db := fun hh => h (Or.inr hh) + unfold SystemHoldsAt at hS + push Not at hS + rcases hS with ⟨c', hc', hnc'⟩ + obtain ⟨bad, hbad⟩ := ih.mp hTail + refine ⟨c' :: bad, ?_⟩ + unfold ViolatesAll + exact ⟨hc', hnc', hbad⟩ + · rintro ⟨bad, hbad⟩ + rw [hAny] + intro h + cases bad with + | nil => + unfold ViolatesAll at hbad + exact False.elim hbad + | cons b bs => + unfold ViolatesAll at hbad + rcases hbad with ⟨hb, hnb, htail⟩ + rcases h with hS | hTail + · exact hnb (hS b hb) + · exact (ih.mpr ⟨bs, htail⟩) hTail + +/-- A finite choice of one constraint from each DNF alternative, independent of +a database. -/ +def ChoosesAll : List Constraint → ConstraintDNF → Prop + | [], [] => True + | c :: cs, S :: Ss => c ∈ S ∧ ChoosesAll cs Ss + | _, _ => False + +/-- The database-dependent violation predicate is a choice of constraints plus +their failure. -/ +theorem violatesAll_iff_choosesAll {bad : List Constraint} {D : ConstraintDNF} {db : DB} : + ViolatesAll bad D db ↔ ChoosesAll bad D ∧ ∀ c ∈ bad, ¬ c.HoldsAt db := by + induction bad generalizing D with + | nil => + cases D with + | nil => simp [ViolatesAll, ChoosesAll] + | cons S Ss => simp [ViolatesAll, ChoosesAll] + | cons c cs ih => + cases D with + | nil => simp [ViolatesAll, ChoosesAll] + | cons S Ss => + simp only [ViolatesAll, ChoosesAll, List.forall_mem_cons, ih, and_assoc] + tauto + +/-- Every chosen constraint lies in one of the DNF alternatives. -/ +theorem choosesAll_mem {bad : List Constraint} {D : ConstraintDNF} (h : ChoosesAll bad D) + {c : Constraint} (hc : c ∈ bad) : ∃ S ∈ D, c ∈ S := by + induction bad generalizing D with + | nil => simp at hc + | cons c' cs ih => + cases D with + | nil => simp [ChoosesAll] at h + | cons S Ss => + rcases h with ⟨hc', htail⟩ + rcases List.mem_cons.mp hc with rfl | hcs + · exact ⟨S, by simp, hc'⟩ + · obtain ⟨S', hS', hcS'⟩ := ih htail hcs + exact ⟨S', by simp [hS'], hcS'⟩ + +/-! ## Canonical finite endpoint lists for the count description -/ + +/-- The finite, nonempty part of a `WithTop Key` endpoint, as an option. -/ +def finiteEndpointOption : WithTop Key → Option Key + | some k => if k = ([] : Key) then none else some k + | none => none + +theorem finiteEndpointOption_eq_some {e : WithTop Key} {k : Key} : + finiteEndpointOption e = some k ↔ e = (k : WithTop Key) ∧ k ≠ ([] : Key) := by + cases e with + | top => simp [finiteEndpointOption] + | coe a => + by_cases ha : a = ([] : Key) + · simp [finiteEndpointOption, ha] + · simp only [finiteEndpointOption, ha, ↓reduceIte] + constructor + · intro h + refine ⟨h, ?_⟩ + intro hk + exact ha (Option.some.inj h ▸ hk) + · rintro ⟨h, _⟩ + exact h + +theorem finiteEndpointOption_injective_on {a a' : WithTop Key} {b : Key} + (ha : b ∈ finiteEndpointOption a) (ha' : b ∈ finiteEndpointOption a') : a = a' := by + cases a with + | top => simp [finiteEndpointOption] at ha + | coe ka => + cases a' with + | top => simp [finiteEndpointOption] at ha' + | coe ka' => + by_cases hka : ka = ([] : Key) + · simp [finiteEndpointOption, hka] at ha + · by_cases hka' : ka' = ([] : Key) + · simp [finiteEndpointOption, hka'] at ha' + · simp only [finiteEndpointOption, hka, hka', ↓reduceIte] at ha ha' + simp only [Option.mem_some] at ha ha' + rw [ha, ha'] + +/-- All finite, nonempty endpoints occurring in a DNF. -/ +def finiteKeyEndpoints (D : ConstraintDNF) : Finset Key := + (cellEndpoints D).filterMap finiteEndpointOption + (by + intro a a' b ha ha' + exact finiteEndpointOption_injective_on ha ha') + +theorem mem_finiteKeyEndpoints {D : ConstraintDNF} {k : Key} : + k ∈ finiteKeyEndpoints D ↔ + (k : WithTop Key) ∈ cellEndpoints D ∧ k ≠ ([] : Key) := by + rw [finiteKeyEndpoints, Finset.mem_filterMap] + constructor + · rintro ⟨a, ha, hfa⟩ + rw [finiteEndpointOption_eq_some] at hfa + exact ⟨by simpa [hfa.1] using ha, hfa.2⟩ + · rintro ⟨ha, hk⟩ + exact ⟨(k : WithTop Key), ha, (finiteEndpointOption_eq_some).mpr ⟨rfl, hk⟩⟩ + +/-- The canonical finite endpoint list for the active-cell count description: all +finite nonempty DNF endpoints, together with `k` (when nonempty) and `keyAfter k`, +sorted increasingly. -/ +def activeBounds (D : ConstraintDNF) (k : Key) : List Key := + let s := insert (keyAfter k) (finiteKeyEndpoints D) + (if k = ([] : Key) then s else insert k s).sort (· ≤ ·) + +theorem keyAfter_ne_nil (k : Key) : keyAfter k ≠ ([] : Key) := by + intro h + have : (keyAfter k).length = 0 := by simp [h] + simp [keyAfter] at this + +theorem mem_activeBounds {D : ConstraintDNF} {k e : Key} : + e ∈ activeBounds D k ↔ + e = keyAfter k ∨ e ∈ finiteKeyEndpoints D ∨ + (k ≠ ([] : Key) ∧ e = k) := by + unfold activeBounds + by_cases hk : k = ([] : Key) + · simp [hk, Finset.mem_sort, or_comm] + · simp [hk, Finset.mem_sort, or_left_comm, or_comm] + +theorem not_mem_activeBounds_bot (D : ConstraintDNF) (k : Key) : + ([] : Key) ∉ activeBounds D k := by + rw [mem_activeBounds] + rintro (h | h | ⟨hk, hk'⟩) + · exact keyAfter_ne_nil k h.symm + · exact (mem_finiteKeyEndpoints.mp h).2 rfl + · exact hk hk'.symm + +/-- A sorted list whose elements are all above `start` has nondecreasing bounds. -/ +theorem boundsLe_of_sortedLE {start : Key} {l : List Key} + (hs : l.SortedLE) (h : ∀ b ∈ l, start ≤ b) : BoundsLe start l := by + induction l generalizing start with + | nil => trivial + | cons a t ih => + change start ≤ a ∧ BoundsLe a t + constructor + · exact h a (by simp) + · apply ih (start := a) + · exact (List.sortedLE_cons.mp hs).2 + · intro b hb + exact (List.sortedLE_cons.mp hs).1 b hb + +theorem activeBounds_boundsLe (D : ConstraintDNF) (k : Key) : + BoundsLe ([] : Key) (activeBounds D k) := by + apply boundsLe_of_sortedLE + · unfold activeBounds + by_cases hk : k = ([] : Key) + · simpa [hk] using + (Finset.sortedLT_sort (insert (keyAfter k) (finiteKeyEndpoints D))).sortedLE + · simpa [hk] using + (Finset.sortedLT_sort (insert k (insert (keyAfter k) (finiteKeyEndpoints D)))).sortedLE + · intro b hb + exact nil_le_key b + +theorem endpointCovered_activeBounds {D : ConstraintDNF} {k : Key} {e : WithTop Key} + (he : e ∈ cellEndpoints D) : EndpointCovered e (activeBounds D k) := by + unfold EndpointCovered + cases e with + | top => exact Or.inr (Or.inl rfl) + | coe a => + by_cases ha : a = ([] : Key) + · exact Or.inl (by simp [ha]) + · refine Or.inr (Or.inr ⟨a, ?_, rfl⟩) + rw [mem_activeBounds] + exact Or.inr (Or.inl ((mem_finiteKeyEndpoints).mpr ⟨by simpa using he, ha⟩)) + +theorem dnfEndpointCovered_activeBounds {D : ConstraintDNF} {k : Key} + {S : ConstraintSystem} {c : Constraint} (hS : S ∈ D) (hc : c ∈ S) : + EndpointCovered c.lo (activeBounds D k) ∧ + EndpointCovered c.hi (activeBounds D k) := + ⟨endpointCovered_activeBounds + (dnfEndpoints_subset_cellEndpoints D (mem_dnfEndpoints_lo hS hc)), + endpointCovered_activeBounds + (dnfEndpoints_subset_cellEndpoints D (mem_dnfEndpoints_hi hS hc))⟩ + +/-- **Flip witnesses as finite count disjunctions.** Under a characterizing DNF, +a one-key toggle witness exists exactly when the limit is respected and there is +an alternative `S` of the DNF, a finite choice `bad` of one constraint from every +alternative, and a finite interval-count assignment that satisfies `S` while +making every constraint in `bad` fail after toggling `k`. All quantifiers here +are finite except the count feasibility, which is the remaining decision +problem. -/ +theorem flipWitness_iff_exists_counts + {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) {k : Key} + (bounds : List Key) (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds) + (hcoverD : ∀ S ∈ D, ∀ c ∈ S, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) : + FlipWitness r res k ↔ + RangeRead.withinLimit r res.keys ∧ + ∃ S ∈ D, ∃ bad : List Constraint, ChoosesAll bad D ∧ + ∃ counts : List ℕ, ∃ topCount : ℕ, + CountsFit ([] : Key) bounds counts ∧ + SystemHoldsWithCounts S bounds counts topCount ∧ + ∀ c ∈ bad, c.bound < + evalCountToggled bounds counts topCount k c.hi - + evalCountToggled bounds counts topCount k c.lo := by + rw [flipWitness_iff_exists_not_anyHolds hchar] + constructor + · rintro ⟨db, hcomp, hnot⟩ + rcases (hchar db).mp hcomp with ⟨hwithin, hany⟩ + obtain ⟨S, hS, hSholds⟩ := hany + obtain ⟨bad, hbad⟩ := (not_anyHolds_iff_exists_violating_choice).mp hnot + rcases (violatesAll_iff_choosesAll).mp hbad with ⟨hchooses, hviol⟩ + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc + exact hcoverD S' hS' c hcS' + obtain ⟨counts, topCount, hfit, hScounts, hbadcounts⟩ := + (exists_db_systemHoldsAt_and_violates_iff_counts S bad bounds hnoBot hle + (hcoverD S hS) hcoverBad hk hka).mp ⟨db, hSholds, hviol⟩ + exact ⟨hwithin, S, hS, bad, hchooses, counts, topCount, hfit, hScounts, hbadcounts⟩ + · rintro ⟨hwithin, S, hS, bad, hchooses, counts, topCount, hfit, hScounts, hbadcounts⟩ + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc + exact hcoverD S' hS' c hcS' + obtain ⟨db, hSholds, hviol⟩ := + (exists_db_systemHoldsAt_and_violates_iff_counts S bad bounds hnoBot hle + (hcoverD S hS) hcoverBad hk hka).mpr + ⟨counts, topCount, hfit, hScounts, hbadcounts⟩ + have hnot : ¬ AnyHolds D (toggle db k) := + (not_anyHolds_iff_exists_violating_choice).mpr + ⟨bad, (violatesAll_iff_choosesAll).mpr ⟨hchooses, hviol⟩⟩ + exact ⟨db, (hchar db).mpr ⟨hwithin, ⟨S, hS, hSholds⟩⟩, hnot⟩ + +/-- **Canonical finite-count form of `FlipWitness`.** The endpoint list is computed +from the DNF itself, so no coverage hypothesis remains. -/ +theorem flipWitness_iff_exists_counts_canonical + {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) {k : Key} : + FlipWitness r res k ↔ + RangeRead.withinLimit r res.keys ∧ + ∃ S ∈ D, ∃ bad : List Constraint, ChoosesAll bad D ∧ + ∃ counts : List ℕ, ∃ topCount : ℕ, + CountsFit ([] : Key) (activeBounds D k) counts ∧ + SystemHoldsWithCounts S (activeBounds D k) counts topCount ∧ + ∀ c ∈ bad, c.bound < + evalCountToggled (activeBounds D k) counts topCount k c.hi - + evalCountToggled (activeBounds D k) counts topCount k c.lo := + flipWitness_iff_exists_counts (D := D) hchar (bounds := activeBounds D k) + (not_mem_activeBounds_bot D k) (activeBounds_boundsLe D k) + (fun S hS c hc => dnfEndpointCovered_activeBounds hS hc) + (by + by_cases hk : k = ([] : Key) + · exact Or.inl hk + · exact Or.inr (by + rw [mem_activeBounds] + exact Or.inr (Or.inr ⟨hk, rfl⟩))) + (by + rw [mem_activeBounds] + exact Or.inl rfl) + +/-! ## A total activity oracle and the unconditional cellwise algorithm -/ + +/-- The total classical activity oracle: decide `FlipWitness` by excluded middle. +This closes the algebraic interface. Making it *computable* is exactly the finite +count-feasibility decision problem isolated by `flipWitness_iff_exists_counts`. -/ +noncomputable def activeClassical (r : RangeRead) (res : Result) : Key → Bool := + fun k => @decide (FlipWitness r res k) (Classical.propDecidable _) + +/-- The classical oracle is exact. -/ +theorem activeClassical_spec (r : RangeRead) (res : Result) : + ActiveDecider r res (activeClassical r res) := by + intro k + simp [activeClassical] + +/-- With the classical total oracle, the cellwise algorithm is minimal on every +readable pair. This is the unconditional form of `conflictRangeCellwise_minimal`; +the only remaining *computability* obligation is the count-feasibility decider. -/ +theorem conflictRangeCellwise_minimal_classical {r : RangeRead} {res : Result} + (hread : Readable r res) : + Minimal r res (conflictRangeCellwise r res (activeClassical r res)) := + conflictRangeCellwise_minimal (activeClassical_spec r res) hread + +/-- The unconditional classical algorithm computes exactly the necessary set. -/ +theorem conflictRangeCellwise_toSet_eq_necessarySet_classical {r : RangeRead} {res : Result} + (hread : Readable r res) : + (conflictRangeCellwise r res (activeClassical r res)).toSet = necessarySet r res := + conflictRangeCellwise_toSet_eq_necessarySet (activeClassical_spec r res) hread + +end FormalConflictRange diff --git a/FormalConflictRange/CrossFreeResidual.lean b/FormalConflictRange/CrossFreeResidual.lean new file mode 100644 index 0000000..72113f2 --- /dev/null +++ b/FormalConflictRange/CrossFreeResidual.lean @@ -0,0 +1,1498 @@ +/- +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.Residual +import FormalConflictRange.CrossedResidualCond + +/-! +# The cross-free reversed truncated residual + +This module closes the last cross-free reversed truncated cell: `begin.offset ≥ 2`, +`end.offset ≥ 2`, `begin.key ≤ end_.key`, and a nonempty truncated result with a +reported key at or above `end_.key`. + +Writing `b = begin.offset - 1`, `e = end.offset - 1`, `t` for the number of reported +keys at or above `end_.key`, and `g = intervalCard begin.key end_.key`, the minimal +range is + +* `[begin.key, keyAfter head)` when `t + max 0 (b - g) ≥ e` (the finite branch); +* `[begin.key, ⊤)` when `t + max 0 (b - g) < e` (the top branch). + +The proof mirrors the complete-result residual but works with the reversed truncated +window. The counting core is the reversed counterpart of +`completeKeys_count_ge_eq_general`; the top-branch witness prepends `b` chain keys to +the reversed reported keys. +-/ + +open FormalConflictRange + +set_option maxHeartbeats 1200000 + +namespace FormalConflictRange + +/-! ### The reversed complete-window count -/ + +/-- The reversed complete-keys count at or above `end_.key` has the same value as the +forward count: reversal is a permutation and does not see the traversal flag. -/ +theorem completeKeys_count_ge_eq_general_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + min ((r.end_.offset - 1).toNat) + (db.keys.length - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + - ((r.begin.offset - 1).toNat - + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)))) := by + have h := completeKeys_count_ge_eq_general (db := db) (r := {r with reverse := false}) + (by simp) hbegin hcross hend + have hck : RangeRead.completeKeys db {r with reverse := false} = + (RangeRead.completeKeys db r).reverse := by + simp [RangeRead.completeKeys, hrev] + rw [hck] at h + rw [List.Perm.countP_eq _ (List.reverse_perm _)] at h + exact h + +/-- The reversed window count in terms of the begin/end counts and the over/under +offsets. -/ +private theorem completeKeys_count_ge_eq_min_rev {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + residualT r res ≤ + min (residualE r) (db.keys.length - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + - (residualB r - + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)))) := by + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hle : residualT r res ≤ + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := + List.Sublist.countP_le hpre.sublist + have hcount := completeKeys_count_ge_eq_general_rev (db := db) hrev hbegin hcross hend + rw [hcount] at hle + simpa [residualT, residualB, residualE] using hle + +/-! ### The branch inequalities -/ + +/-- The deficit `max 0 (b - g)` plus the reported over-count never exceeds the end +overshoot. This is the reversed truncation counterpart of +`residual_dt_le_e`. -/ +theorem residualD_add_T_le_E {r : RangeRead} {res : Result} {g : ℕ∞} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (htpos : 0 < residualT r res) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = g) : + residualD r g + residualT r res ≤ residualE r := by + obtain ⟨db, hcomp⟩ := hread + have hbegin1 : 1 ≤ r.begin.offset := by omega + have hend1 : 1 ≤ r.end_.offset := by omega + have hle := completeKeys_count_ge_eq_min_rev (db := db) hcomp hmore hrev hbegin1 hcross hend1 + set p := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set A := db.keys.length - cE with hA + set b := residualB r with hb + set e := residualE r with he + set N := cE - p with hN + have hNle : ((N : ℕ) : ℕ∞) ≤ g := by + have h := cnt_sub_le_intervalCard (db := db) (w₁ := r.begin.key) (w₂ := r.end_.key) hcross + rw [hcard] at h + simpa [N, p, cE, cnt, hp, hcE, hN] using h + have hTle : residualT r res ≤ min e A - (b - N) := by + simpa [residualT, residualE, residualB, A, b, e, N, p, cE, hp, hcE, hA, hb, he, hN] + using hle + by_cases hg : g = ⊤ + · have hD : residualD r g = 0 := by simp [residualD, hg] + rw [hD, Nat.zero_add] + exact le_trans hTle (le_trans (Nat.sub_le _ _) (Nat.min_le_left _ _)) + · obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hg + have hD : residualD r g = b - n := by + rw [← hn]; exact residualD_coe r n + rw [hD] + have hNn : N ≤ n := by + have h := hNle + rw [← hn] at h + exact_mod_cast h + have hbNe : b - N ≤ e := by + have h1 : 1 ≤ min e A - (b - N) := le_trans htpos hTle + omega + have hmain : (b - n) + (min e A - (b - N)) ≤ e := by + by_cases hbn : b ≤ n + · have hz : b - n = 0 := by omega + rw [hz, Nat.zero_add] + exact le_trans (Nat.sub_le _ _) (Nat.min_le_left _ _) + · have hnb : n < b := lt_of_not_ge hbn + omega + exact le_trans (Nat.add_le_add_left hTle _) hmain + +/-- The deficit plus the reported over-count never exceeds the number of stored keys +at or above `end_.key`. -/ +theorem residualD_add_T_le_count_ge {r : RangeRead} {res : Result} {g : ℕ∞} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (htpos : 0 < residualT r res) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = g) + (db : DB) (hcomp : RangeRead.Compatible db r res) : + residualD r g + residualT r res ≤ + db.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + have hbegin1 : 1 ≤ r.begin.offset := by omega + have hend1 : 1 ≤ r.end_.offset := by omega + have hle := completeKeys_count_ge_eq_min_rev (db := db) hcomp hmore hrev hbegin1 hcross hend1 + set p := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set A := db.keys.length - cE with hA + set b := residualB r with hb + set e := residualE r with he + set N := cE - p with hN + have hNle : ((N : ℕ) : ℕ∞) ≤ g := by + have h := cnt_sub_le_intervalCard (db := db) (w₁ := r.begin.key) (w₂ := r.end_.key) hcross + rw [hcard] at h + simpa [N, p, cE, cnt, hp, hcE, hN] using h + have hTle : residualT r res ≤ min e A - (b - N) := by + simpa [residualT, residualE, residualB, A, b, e, N, p, cE, hp, hcE, hA, hb, he, hN] + using hle + have hAeq : A = db.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + have hlen := List.length_eq_countP_add_countP + (p := fun k : Key => decide ((k : WithTop Key) < r.end_.key)) (l := db.keys) + have hnot : db.keys.countP + (fun k : Key => ¬ decide ((k : WithTop Key) < r.end_.key)) = + db.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + apply List.countP_congr + intro k _ + simp [decide_eq_true_eq] + rw [hA, hcE, ← hnot] + omega + by_cases hg : g = ⊤ + · have hD : residualD r g = 0 := by simp [residualD, hg] + rw [hD, Nat.zero_add, ← hAeq] + exact le_trans hTle (le_trans (Nat.sub_le _ _) (Nat.min_le_right _ _)) + · obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hg + have hD : residualD r g = b - n := by + rw [← hn]; exact residualD_coe r n + rw [hD] + have hNn : N ≤ n := by + have h := hNle + rw [← hn] at h + exact_mod_cast h + have hbNe : b - N ≤ e := by + have h1 : 1 ≤ min e A - (b - N) := le_trans htpos hTle + omega + have hmain : (b - n) + (min e A - (b - N)) ≤ A := by + by_cases hbn : b ≤ n + · have hz : b - n = 0 := by omega + rw [hz, Nat.zero_add] + exact le_trans (Nat.sub_le _ _) (Nat.min_le_right _ _) + · have hnb : n < b := lt_of_not_ge hbn + omega + rw [← hAeq] + exact le_trans (Nat.add_le_add_left hTle _) hmain + + + +/-- Cross-free reversed complete keys: the reversed block of keys at or above +`begin.key`, first `g + e` of them, with the first `b` dropped. -/ +theorem completeKeys_crossfree_rev_eq {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hend : 1 ≤ r.end_.offset) (hcross : r.begin.key ≤ r.end_.key) : + RangeRead.completeKeys db r = + (((above db r.begin.key).take + ((between db r.begin.key r.end_.key).length + + (r.end_.offset - 1).toNat)).drop (r.begin.offset - 1).toNat).reverse := by + set B : WithTop Key := r.begin.key with hB + set E : WithTop Key := r.end_.key with hE + set b : Nat := (r.begin.offset - 1).toNat with hb + set e : Nat := (r.end_.offset - 1).toNat with he + set g : Nat := (between db B E).length with hg + rw [completeKeys_rev_eq_reverse db r hrev] + rw [completeKeys_eq_filter_ge_drop_take (db := db) (r := { r with reverse := false }) + (by simp) hbegin hend] + simp only [show ({ r with reverse := false } : RangeRead).end_.offset = r.end_.offset from rfl, + show ({ r with reverse := false } : RangeRead).begin.offset = r.begin.offset from rfl] + rw [show db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = above db B + from by rw [hB]; rfl] + rw [← hB, ← hE, ← hb, ← he] + have hp : cnt db E = cnt db B + g := by + rw [hg, hE, hB]; exact cnt_eq_add_between db hcross + have hlenB : (above db B).length = db.keys.length - cnt db B := by + have h := cnt_add_above_length db B + omega + have hcalc : min (cnt db E + e) db.keys.length - (cnt db B + b) = + min (g + e) (above db B).length - b := by + rw [hp, hlenB]; omega + simp only [cnt] at hcalc ⊢ + rw [hcalc] + congr 1 + exact take_sub_drop_gen (above db B) (g + e) b + +/-- Filtering a sorted list at or below the `m`-th element gives its first `m` +elements. -/ +theorem filter_le_eq_take_of_window' {A : List Key} (hs : A.SortedLT) + {b m : Nat} (hbm : b < m) (hm : m ≤ A.length) {H : Key} + (hlast : A[m - 1]? = some H) : + A.filter (fun k : Key => decide (k ≤ H)) = A.take m := by + have hmpos : 0 < m := Nat.lt_of_le_of_lt (Nat.zero_le b) hbm + obtain ⟨hm1lt, hval⟩ := List.getElem?_eq_some_iff.mp hlast + have h := filter_le_eq_take_succ_of_getElem (l := A) hs (n := m - 1) hm1lt hval + simpa [Nat.sub_add_cancel hmpos] using h + +/-- **Cross-free reversed window as a filter.** If `res` is a nonempty prefix of +the reversed complete keys, then dropping `(begin.offset - 1).toNat` from the +stored keys at or above `begin.key` and at or below the reported head gives +exactly the complete window (as a list). -/ +theorem completeKeys_crossfree_rev_eq_filter {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hend : 1 ≤ r.end_.offset) (hcross : r.begin.key ≤ r.end_.key) + (hne : res.keys ≠ []) : + RangeRead.completeKeys db r = + (((above db r.begin.key).filter + (fun k : Key => decide (k ≤ res.keys.head hne))).drop + (r.begin.offset - 1).toNat).reverse := by + set B : WithTop Key := r.begin.key with hB + set E : WithTop Key := r.end_.key with hE + set b : Nat := (r.begin.offset - 1).toNat with hb + set e : Nat := (r.end_.offset - 1).toNat with he + set g : Nat := (between db B E).length with hg + set A : List Key := above db B with hAdef + set M : Nat := min (g + e) A.length with hM + set W : List Key := (A.take (g + e)).drop b with hW + have hformula : RangeRead.completeKeys db r = W.reverse := by + rw [completeKeys_crossfree_rev_eq (db := db) (r := r) hrev hbegin hend hcross] + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hckne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne + have hWne : W ≠ [] := by + intro h; apply hckne; rw [hformula, h]; rfl + have hWlen : W.length = M - b := by + rw [hW, List.length_drop, List.length_take, hM] + have hbm : b < M := by + have hpos : 0 < W.length := List.length_pos_of_ne_nil hWne + omega + have hMle : M ≤ A.length := by rw [hM]; exact Nat.min_le_right _ _ + have hW' : W = (A.drop b).take (M - b) := by + rw [hW, hM] + exact (take_sub_drop_gen A (g + e) b).symm + have hWlast : W.getLast hWne = res.keys.head hne := by + have hWrevne : W.reverse ≠ [] := by rwa [List.reverse_ne_nil_iff] + have hhead? : res.keys.head? = W.reverse.head? := by + have hprehead : res.keys.head? = (RangeRead.completeKeys db r).head? := by + obtain ⟨t, ht⟩ := hpre + rw [← ht, List.head?_append, List.head?_eq_some_head hne, Option.some_or] + rw [hprehead, hformula] + have hhead : res.keys.head hne = W.reverse.head hWrevne := by + rw [List.head_eq_iff_head?_eq_some hne, hhead?, List.head?_eq_some_head hWrevne] + rw [hhead] + exact (List.head_reverse hWrevne).symm + have hlastD : W.getLastD [] = res.keys.head hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hWne] + simp [hWlast] + have hbt : b + (M - b) ≤ A.length := by omega + have hm1lt : M - 1 < A.length := by omega + have hget := getLast_take_drop (A := A) (b := b) (t := M - b) (res := W) + (by omega) hW' hbt + have hget' : W.getLastD [] = A[M - 1]'hm1lt := by + simpa [show b + (M - b) - 1 = M - 1 by omega] using hget + have hidx : A[M - 1]'hm1lt = res.keys.head hne := hget'.symm.trans hlastD + have hsome : A[M - 1]? = some (res.keys.head hne) := by + rw [List.getElem?_eq_getElem hm1lt] + exact congrArg some hidx + have hsA : A.SortedLT := by + rw [hAdef] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hfilter : A.filter (fun k : Key => decide (k ≤ res.keys.head hne)) = A.take M := + filter_le_eq_take_of_window' hsA hbm hMle hsome + rw [hformula, hW', hfilter] + rw [show (A.drop b).take (M - b) = (A.take M).drop b by + have h := take_sub_drop_gen A M b + rwa [Nat.min_eq_left hMle] at h] + +/-- Exact count of complete keys at or above `end_.key` for a reversed cross-free read. -/ +theorem completeKeys_count_ge_eq_crossfree_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + min ((r.end_.offset - 1).toNat) + (db.keys.length - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + - ((r.begin.offset - 1).toNat - + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)))) := by + have h := completeKeys_count_ge_eq_general (db := db) + (r := { r with reverse := false }) (by simp) hbegin hcross hend + rw [completeKeys_rev_eq_reverse db r hrev] + simpa using h + +/-- The keys of a truncated range are the stored keys at or above the begin key and +at or below the last reported key. -/ +theorem truncatedRange_keys_eq_above_filter_le (db : DB) (r : RangeRead) (H : Key) : + (truncatedRange r H).keys db = + (above db r.begin.key).filter (fun k : Key => decide (k ≤ H)) := by + unfold truncatedRange above + rw [ConflictRange.keys_eq_inRange] + unfold RangeRead.inRange + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hb : r.begin.key ≤ (k : WithTop Key) + · by_cases hh : (k : WithTop Key) < ((keyAfter H : Key) : WithTop Key) + · have hle : k ≤ H := (lt_keyAfter_iff k H).mp (WithTop.coe_lt_coe.mp hh) + simp [hb, hle, WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hle (keyAfter_self_lt H))] + · have hnot : ¬ k ≤ H := by + intro hle + exact hh (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hle (keyAfter_self_lt H))) + simp [hb, hh, hnot] + · simp [hb] + +/-- Agreement on the truncated range `[begin.key, keyAfter H)` fixes the stored keys +at or above the begin key and at or below `H`. -/ +theorem agreeOn_truncatedRange_filter_le {r : RangeRead} {H : Key} {d₁ d₂ : DB} + (hag : AgreeOn (truncatedRange r H) d₁ d₂) : + (above d₁ r.begin.key).filter (fun k : Key => decide (k ≤ H)) = + (above d₂ r.begin.key).filter (fun k : Key => decide (k ≤ H)) := by + have h := hag + rw [AgreeOn, truncatedRange_keys_eq_above_filter_le, + truncatedRange_keys_eq_above_filter_le] at h + exact h + +/-- Agreement on the stored keys at or above `B` and at or below `H` fixes the number +of stored keys in `[B, E)` whenever `E ≤ H`. -/ +theorem between_length_eq_of_filter_le_eq {d₁ d₂ : DB} {B E : WithTop Key} {H : Key} + (hEH : E ≤ (H : WithTop Key)) + (hF : (above d₁ B).filter (fun k : Key => decide (k ≤ H)) = + (above d₂ B).filter (fun k : Key => decide (k ≤ H))) : + (between d₁ B E).length = (between d₂ B E).length := by + have hlt (d : DB) : + (above d B).filter (fun k : Key => decide ((k : WithTop Key) < E)) = + ((above d B).filter (fun k : Key => decide (k ≤ H))).filter + (fun k : Key => decide ((k : WithTop Key) < E)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : (k : WithTop Key) < E + · have hkH : k ≤ H := WithTop.coe_le_coe.mp (le_trans (le_of_lt hk) hEH) + simp [hk, hkH] + · simp [hk] + rw [between_eq_filter_above, between_eq_filter_above, hlt d₁, hlt d₂, hF] + +/-- The exact count of complete keys at or above `end_.key`, in terms of the stored keys +at or above `end_.key` and the stored keys in `[begin.key, end_.key)`. -/ +theorem completeKeys_count_ge_eq_min_sub_between {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + min ((r.end_.offset - 1).toNat) (above db r.end_.key).length - + ((r.begin.offset - 1).toNat - (between db r.begin.key r.end_.key).length) := by + set B : WithTop Key := r.begin.key with hB + set E : WithTop Key := r.end_.key with hE + set pB := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < B)) with hpB + set cE := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set g := (between db B E).length with hg + have hcnt := completeKeys_count_ge_eq_crossfree_rev (db := db) (r := r) hrev hbegin + (by simpa [hB, hE] using hcross) hend + have hg_eq : g = cE - pB := by + have h := cnt_eq_add_between (db := db) (w₁ := B) (w₂ := E) (by simpa [hB, hE] using hcross) + rw [← hg] at h + simp only [cnt] at h + omega + have haE : (above db E).length = db.keys.length - cE := by + have h := cnt_add_above_length db E + simp only [cnt] at h + omega + rw [hcnt] + rw [show db.keys.length - cE = (above db E).length from haE.symm] + rw [← hg_eq] + +/-- The keys at or above `B` split into those in `[B, E)` and those at or above `E`. -/ +theorem above_length_eq_between_add_above (db : DB) {B E : WithTop Key} (hBE : B ≤ E) : + (above db B).length = (between db B E).length + (above db E).length := by + have hsplit := List.length_eq_length_filter_add (l := above db B) + (f := fun k : Key => decide ((k : WithTop Key) < E)) + rw [hsplit, between_le_eq_filter_above] + have hnot : (above db B).filter + (fun k : Key => !decide ((k : WithTop Key) < E)) = above db E := by + rw [above_eq_filter, above_eq_filter, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : (k : WithTop Key) < E + · simp [hk] + · have hke : E ≤ (k : WithTop Key) := le_of_not_gt hk + have hkb : B ≤ (k : WithTop Key) := le_trans hBE hke + simp [hk, hke, hkb] + rw [hnot] + +/-- Number of reported keys at or above the end key. -/ +def crossFreeResidualT (r : RangeRead) (res : Result) : ℕ := + (res.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).length + +/-- Slack between the begin offset and the capacity of the begin--end gap. -/ +def crossFreeSlack (r : RangeRead) : ℕ := + ((((r.begin.offset - 1).toNat : ℕ∞) - intervalCard r.begin.key r.end_.key)).toNat + +/-- In the finite branch, a compatible database has at least `end.offset - 1` stored +keys at or above `end_.key`, and the stored keys at or above the begin key and at or +below the reported head are exactly the `g + e` keys consumed by the window. -/ +theorem finite_consequences {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hend : 1 ≤ r.end_.offset) (hcross : r.begin.key ≤ r.end_.key) + (hne : res.keys ≠ []) + (hHE : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hfin : crossFreeResidualT r res + crossFreeSlack r ≥ (r.end_.offset - 1).toNat) : + (r.end_.offset - 1).toNat ≤ (above db r.end_.key).length ∧ + ((above db r.begin.key).filter + (fun k : Key => decide (k ≤ res.keys.head hne))).length = + (between db r.begin.key r.end_.key).length + (r.end_.offset - 1).toNat := by + set B : WithTop Key := r.begin.key with hB + set E : WithTop Key := r.end_.key with hE + set H : Key := res.keys.head hne with hH + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set g := (between db B E).length with hg + set aE := (above db E).length with haE + set T := crossFreeResidualT r res with hT + set S := crossFreeSlack r with hS + have hcE : r.begin.key ≤ r.end_.key := hcross + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hTcount : T = res.keys.countP (fun k : Key => decide (E ≤ (k : WithTop Key))) := by + rw [hT, crossFreeResidualT, List.countP_eq_length_filter] + have hTle : T ≤ (RangeRead.completeKeys db r).countP + (fun k : Key => decide (E ≤ (k : WithTop Key))) := by + rw [hTcount] + exact List.Sublist.countP_le hpre.sublist + have hcount := completeKeys_count_ge_eq_min_sub_between (db := db) (r := r) + hrev hbegin (by simpa [hB, hE] using hcross) hend + have hCeq : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (E ≤ (k : WithTop Key))) = min e aE - (b - g) := by + simpa [B, E, b, e, g, aE] using hcount + have hCKlen : (RangeRead.completeKeys db r).length = min (g + e) (above db B).length - b := by + rw [completeKeys_crossfree_rev_eq (db := db) (r := r) hrev hbegin hend + (by simpa [hB, hE] using hcross)] + rw [List.length_reverse, List.length_drop, List.length_take] + have hAlen : (above db B).length = g + aE := by + rw [hg, haE, hB, hE] + exact above_length_eq_between_add_above db hcross + have hCKpos : 0 < (RangeRead.completeKeys db r).length := + List.length_pos_of_ne_nil (hpre.ne_nil hne) + have hbg_le : b - g ≤ min e aE := by + rw [hCKlen, hAlen] at hCKpos + omega + have hSle : S ≤ b - g := by + rw [hS, crossFreeSlack] + have hgcard : ((g : ℕ) : ℕ∞) ≤ intervalCard B E := by + rw [hg, hB, hE] + exact between_length_le_intervalCard (db := db) hcross + have hle : ((b : ℕ∞) - intervalCard B E) ≤ ((b : ℕ∞) - (g : ℕ∞)) := + tsub_le_tsub_left hgcard (b : ℕ∞) + have hto := ENat.toNat_le_toNat hle (by simp) + rw [ENat.toNat_sub (by simp : ((g : ℕ∞)) ≠ ⊤), ENat.toNat_coe, ENat.toNat_coe] at hto + simpa [hB, hE, hb] using hto + have hfin' : e ≤ T + (b - g) := + le_trans hfin (Nat.add_le_add_left hSle T) + have hTleC : T ≤ min e aE - (b - g) := by + rw [← hCeq]; exact hTle + have hTg_le : T + (b - g) ≤ min e aE := by omega + have he_le_aE : e ≤ aE := by + have h1 : e ≤ T + (b - g) := hfin' + exact le_trans h1 (le_trans hTg_le (Nat.min_le_right _ _)) + have hmin : min e aE = e := Nat.min_eq_left he_le_aE + have hCeq' : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (E ≤ (k : WithTop Key))) = e - (b - g) := by + rw [hCeq, hmin] + have hfilter := completeKeys_crossfree_rev_eq_filter (db := db) (r := r) (res := res) + hcomp hrev hbegin hend (by simpa [hB, hE] using hcross) hne + have hFlen : ((above db B).filter (fun k : Key => decide (k ≤ H))).length = + b + (RangeRead.completeKeys db r).length := by + have h := congrArg List.length hfilter + rw [List.length_reverse, List.length_drop, ← hb] at h + have hb_le : b ≤ ((above db B).filter (fun k : Key => decide (k ≤ H))).length := by + by_contra hlt + have hlt' : ((above db B).filter (fun k : Key => decide (k ≤ H))).length < b := + lt_of_not_ge hlt + have hdrop_nil : ((above db B).filter (fun k : Key => decide (k ≤ H))).drop b = [] := + List.drop_eq_nil_iff.mpr (le_of_lt hlt') + have hck_nil : RangeRead.completeKeys db r = [] := by + rw [hfilter, hdrop_nil]; rfl + exact hpre.ne_nil hne hck_nil + have hsub : ((above db B).filter (fun k : Key => decide (k ≤ H))).length - b = + (RangeRead.completeKeys db r).length := h.symm + have hadd := Nat.eq_add_of_sub_eq hb_le hsub + simpa [Nat.add_comm] using hadd + have hge : g + e ≤ (above db B).length := by + rw [hAlen]; omega + have hminA : min (g + e) (above db B).length = g + e := Nat.min_eq_left hge + have hCKlen' : (RangeRead.completeKeys db r).length = g + e - b := by + rw [hCKlen, hminA] + have hb_le_ge : b ≤ g + e := by omega + have hFlen' : ((above db B).filter (fun k : Key => decide (k ≤ H))).length = g + e := by + rw [hFlen, hCKlen'] + omega + constructor + · simpa [E] using he_le_aE + · simpa [B, E, H, g, e] using hFlen' + + + +/-! ### Branch bookkeeping and the finite-branch soundness -/ + +/-- The filter-length form and the `countP` form of the reported over-count agree. -/ +theorem crossFreeResidualT_eq_residualT (r : RangeRead) (res : Result) : + crossFreeResidualT r res = residualT r res := by + rw [crossFreeResidualT, residualT, List.countP_eq_length_filter] + +/-- The capacity slack is the residual deficit. -/ +theorem crossFreeSlack_eq_residualD (r : RangeRead) : + crossFreeSlack r = residualD r (intervalCard r.begin.key r.end_.key) := by + unfold crossFreeSlack residualD + by_cases hg : intervalCard r.begin.key r.end_.key = (⊤ : ℕ∞) + · simp [hg] + · obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hg + rw [← hn, if_neg (by simp : (↑n : ℕ∞) ≠ ⊤), ENat.toNat_sub (by simp), + ENat.toNat_natCast, ENat.toNat_natCast] + rfl + +/-- In a reversed sorted list a reported key at or above `end_.key` bounds the head. -/ +theorem head_ge_end_of_residualT_pos {r : RangeRead} {res : Result} + (hsort : res.keys.SortedGT) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) : + r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) := by + rw [residualT] at htpos + obtain ⟨k, hk, hkge⟩ := List.countP_pos_iff.mp htpos + rw [decide_eq_true_eq] at hkge + have hp : res.keys.Pairwise (fun a b => a ≥ b) := hsort.pairwise.imp le_of_lt + exact le_trans hkge (WithTop.coe_le_coe.mpr (hp.rel_head hk)) + +/-- The keys at or above any selector are strictly increasing. -/ +theorem above_sortedLT (db : DB) (B : WithTop Key) : (above db B).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + +/-- When the reversed window ends at the sentinel, the complete keys are the reversal +of the top range's stored keys dropped by `(begin.offset - 1).toNat`. -/ +theorem completeKeys_topRange_drop_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hE : resolve db r.end_ = (⊤ : WithTop Key)) : + RangeRead.completeKeys db r = + (((topRange r).keys db).drop (r.begin.offset - 1).toNat).reverse := by + rw [completeKeys_rev_eq_reverse db r hrev, + completeKeys_eq_topRange_keys_drop (db := db) (r := { r with reverse := false }) + (by simp) hbegin (by simpa using hE)] + simp [topRange] + +/-- **Finite-branch soundness for the cross-free reversed truncated residual.** On +the equality branch `d + t = e`, agreement on `[begin.key, keyAfter head)` fixes the +complete keys of every compatible database, so compatibility transfers. -/ +theorem sound_truncatedRange_crossfree_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hcross : r.begin.key ≤ r.end_.key) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) + (hfin : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res = + residualE r) : + Sound r res (truncatedRange r (res.keys.head hne)) := by + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hread.choose_spec hrev + have hHE : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) := + head_ge_end_of_residualT_pos hsort hne htpos + have hfin' : crossFreeResidualT r res + crossFreeSlack r ≥ residualE r := by + rw [crossFreeResidualT_eq_residualT, crossFreeSlack_eq_residualD] + omega + have step : ∀ db db', AgreeOn (truncatedRange r (res.keys.head hne)) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag hcomp + have hF := agreeOn_truncatedRange_filter_le (r := r) (H := res.keys.head hne) hag + obtain ⟨_hele, hlenF⟩ := + finite_consequences (db := db) hcomp hmore hrev hbegin hend hcross hne hHE hfin' + have hgb : (between db' r.begin.key r.end_.key).length = + (between db r.begin.key r.end_.key).length := + between_length_eq_of_filter_le_eq hHE hF.symm + have hlen' : ((above db' r.begin.key).filter + (fun k : Key => decide (k ≤ res.keys.head hne))).length = + (between db' r.begin.key r.end_.key).length + (r.end_.offset - 1).toNat := by + rw [← hF, hlenF, hgb] + have htake : (above db' r.begin.key).take + ((between db' r.begin.key r.end_.key).length + (r.end_.offset - 1).toNat) = + (above db' r.begin.key).filter + (fun k : Key => decide (k ≤ res.keys.head hne)) := + take_eq_filter_le_of_length (above_sortedLT db' r.begin.key) hlen' + have hck_eq : RangeRead.completeKeys db' r = RangeRead.completeKeys db r := by + rw [completeKeys_crossfree_rev_eq (db := db') hrev hbegin hend hcross, + htake, ← hF, + completeKeys_crossfree_rev_eq_filter (db := db) hcomp hrev hbegin hend hcross hne] + rw [compatible_iff_truncated_keys_rev (db := db) hrev hmore hne] at hcomp + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + exact ⟨hcomp.1, by rw [hck_eq]; exact hcomp.2⟩ + intro d1 d2 hag + exact ⟨step d1 d2 hag, step d2 d1 hag.symm⟩ + + +end FormalConflictRange + +namespace FormalConflictRange + +/-! ### Counting the prepended chain -/ + +/-- The first `b` natural numbers filtered by `i < n` number `min b n`. -/ +theorem filter_range_lt_length (b n : ℕ) : + ((List.range b).filter (fun i => decide (i < n))).length = min b n := by + induction b with + | zero => simp + | succ k ih => + rw [List.range_succ, List.filter_append, List.length_append, ih] + by_cases h : k < n + · rw [show ([k].filter (fun i => decide (i < n))).length = 1 by simp [h], + Nat.min_eq_left (by omega), Nat.min_eq_left (by omega)] + · have hnk : n ≤ k := by omega + rw [show ([k].filter (fun i => decide (i < n))).length = 0 by simp [h], + Nat.min_eq_right hnk, Nat.min_eq_right (by omega)] + omega + +/-- The chain from `v` lies below `E` exactly up to a finite `intervalCard`. -/ +theorem chain_filter_lt_length_coe {v E : Key} {n : ℕ} (b : ℕ) + (hcard : intervalCard (↑v : WithTop Key) (↑E : WithTop Key) = (n : ℕ∞)) : + (((List.range b).map (chain v)).filter (fun k => decide (k < E))).length = + min b n := by + rw [← List.countP_eq_length_filter] + rw [List.countP_map] + have hpred : ∀ i : ℕ, decide (chain v i < E) = decide (i < n) := by + intro i + rw [decide_eq_decide] + rw [chain_lt_iff v E i, hcard, ENat.natCast_lt_natCast] + rw [show List.countP ((fun k : Key => decide (k < E)) ∘ chain v) (List.range b) = + List.countP (fun i => decide (i < n)) (List.range b) from + List.countP_congr (fun i _ => by simp only [Function.comp_apply]; rw [hpred i])] + rw [List.countP_eq_length_filter] + exact filter_range_lt_length b n + +/-- The chain from `v` is entirely below `E` when the interval is infinite. -/ +theorem chain_filter_lt_length_top {v E : Key} (b : ℕ) + (hcard : intervalCard (↑v : WithTop Key) (↑E : WithTop Key) = ⊤) : + (((List.range b).map (chain v)).filter (fun k => decide (k < E))).length = b := by + rw [← List.countP_eq_length_filter, List.countP_map] + have hpred : ∀ i : ℕ, decide (chain v i < E) = true := by + intro i + rw [decide_eq_true_eq] + exact chain_lt_of_intervalCard_eq_top hcard i + rw [show List.countP ((fun k : Key => decide (k < E)) ∘ chain v) (List.range b) = + List.countP (fun _ : ℕ => true) (List.range b) from + List.countP_congr (fun i _ => by simp only [Function.comp_apply]; rw [hpred i]; simp)] + simp + +/-- The chain's filtered count against an `ℕ∞` gap is `min b (g.toNat)` in the finite +case and `b` in the infinite case; together with the deficit it covers `b`. -/ +theorem chain_filter_lt_length_add_residualD {v E : Key} (b : ℕ) (g : ℕ∞) + (hcard : intervalCard (↑v : WithTop Key) (↑E : WithTop Key) = g) : + (((List.range b).map (chain v)).filter (fun k => decide (k < E))).length + + (if g = ⊤ then 0 else b - g.toNat) = b := by + by_cases hg : g = ⊤ + · rw [hg] at hcard + rw [if_pos hg, chain_filter_lt_length_top b hcard] + omega + · obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hg + have hcard' : intervalCard (↑v : WithTop Key) (↑E : WithTop Key) = (n : ℕ∞) := by + rw [hn]; exact hcard + rw [if_neg hg, ← hn, ENat.toNat_natCast, chain_filter_lt_length_coe b hcard'] + omega + +/-! ### Begin-key necessity -/ + +/-- The counted complement of `residualT`: reported keys strictly below `end_.key`. -/ +theorem countP_lt_eq_length_sub_residualT (r : RangeRead) (res : Result) : + res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + res.keys.length - residualT r res := by + have hlen := List.length_eq_countP_add_countP + (p := fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) (l := res.keys) + have hnot : res.keys.countP + (fun k : Key => ¬ decide (r.end_.key ≤ (k : WithTop Key))) = + res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + apply List.countP_congr + intro k _ + simp [decide_eq_true_eq] + rw [hnot] at hlen + rw [residualT] + omega + +/-- A positive reported over-count forces a genuine end selector key. -/ +theorem end_key_ne_top_of_residualT_pos {r : RangeRead} {res : Result} + (htpos : 0 < residualT r res) : r.end_.key ≠ (⊤ : WithTop Key) := by + intro h + rw [residualT, h] at htpos + simp at htpos + +/-- **Begin-key necessity for the cross-free reversed truncated residual.** The +begin selector key is necessary in the residual cell, where some reported key lies at +or above `end_.key`. The witness prepends the `b` successor-chain keys from `v` to the +reversed reported keys; the count inequality `d + t ≤ e` makes its end selector +overshoot, and erasing `v` shortens the complete window by exactly one. -/ +theorem necessary_begin_key_crossfree_residual {r : RangeRead} {res : Result} {v : Key} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = true) + (hlen : 0 < res.keys.length) (hbegin : 2 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (htpos : 0 < residualT r res) + (hkey : r.begin.key = (↑v : WithTop Key)) : + Necessary r res v := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := List.ne_nil_of_length_pos hlen + set b : ℕ := (r.begin.offset - 1).toNat with hb + have hbpos : 1 ≤ b := by rw [hb]; omega + have hresrev_sort : res.keys.reverse.SortedLT := + (RangeRead.compatible_sortedGT hcomp hrev).reverse + have hlast_le : ∀ y ∈ res.keys.reverse, res.keys.getLast hne ≤ y := by + intro y hy + have hy' : y ∈ res.keys := List.mem_reverse.mp hy + have hge : res.keys.Pairwise (fun a b => a ≥ b) := + (RangeRead.compatible_pairwise hcomp).2 hrev |>.imp (fun h => le_of_lt h) + exact hge.rel_getLast hy' + have hresolve_coe : ∃ k : Key, resolve db₀ r.begin = (↑k : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.begin) with h | h | h + · exact ⟨[], h⟩ + · exfalso + have hnil : RangeRead.completeKeys db₀ r = [] := by + rw [RangeRead.completeKeys, ite_eq_left (by simp [hrev])] + simp only [RangeRead.inRange, h] + rw [List.reverse_eq_nil_iff, List.filter_eq_nil_iff] + intro x _ hx + exact absurd (of_decide_eq_true hx).1 (not_le.mpr (WithTop.coe_lt_top x)) + exact List.IsPrefix.ne_nil (RangeRead.compatible_keys_prefix hcomp) hne hnil + · obtain ⟨k, _, hk⟩ := h; exact ⟨k, hk⟩ + obtain ⟨k, hk⟩ := hresolve_coe + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.getLast_mem hne) + have hk_le_last : k ≤ res.keys.getLast hne := by + have h := (RangeRead.mem_completeKeys.mp hlast_ck).2.1 + rw [hk] at h + exact WithTop.coe_le_coe.mp h + have hchain_b_le_k : chain v b ≤ k := by + have h := chain_le_of_resolve (s := r.begin) (k₀ := v) (k := k) hkey (by omega) hk + rwa [show (r.begin.offset - 1).toNat = b from hb] at h + have hchain_lt_last : ∀ i < b, chain v i < res.keys.getLast hne := + fun i _ => lt_of_lt_of_le (chain_lt_mono (b := v) (by omega)) + (le_trans hchain_b_le_k hk_le_last) + set Fp : List Key := (List.range b).map (chain v) with hFp + have hFplen : Fp.length = b := by rw [hFp, List.length_map, List.length_range] + have hFpsort : Fp.SortedLT := by rw [hFp]; exact sortedLT_map_chain v b + have hFp_lt_last : ∀ x ∈ Fp, x < res.keys.getLast hne := by + intro x hx + rw [hFp, List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchain_lt_last i (List.mem_range.mp hi) + have hFp_ge_v : ∀ x ∈ Fp, v ≤ x := by + intro x hx + rw [hFp, List.mem_map] at hx + obtain ⟨i, _, rfl⟩ := hx + exact chain_le v i + have hFp_v : v ∈ Fp := by + rw [hFp, List.mem_map]; exact ⟨0, List.mem_range.mpr (by omega), chain_zero v⟩ + set D : List Key := Fp ++ res.keys.reverse with hD + have hDs : D.SortedLT := by + rw [hD] + refine List.sortedLT_append.mpr ⟨hFpsort, hresrev_sort, ?_⟩ + intro a ha c hc + exact lt_of_lt_of_le (hFp_lt_last a ha) (hlast_le c hc) + let dbw : DB := ⟨D, hDs⟩ + have hD_ge_v : ∀ x ∈ D, v ≤ x := by + intro x hx + rw [hD, List.mem_append] at hx + rcases hx with hx | hx + · exact hFp_ge_v x hx + · exact le_trans (le_of_lt (hFp_lt_last _ hFp_v)) (hlast_le x hx) + have htopw : (topRange r).keys dbw = D := by + unfold topRange ConflictRange.keys + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨by rw [hkey]; exact WithTop.coe_le_coe.mpr (hD_ge_v x hx), WithTop.coe_lt_top x⟩ + -- The end selector overshoots, driven by the count inequality. + obtain ⟨E, hE⟩ := WithTop.ne_top_iff_exists.mp (end_key_ne_top_of_residualT_pos htpos) + set g : ℕ∞ := intervalCard r.begin.key r.end_.key with hg + have hcard' : intervalCard (↑v : WithTop Key) (↑E : WithTop Key) = g := by + rw [hg, ← hkey, hE] + have hFp_cnt : Fp.countP (fun x : Key => decide (x < E)) + residualD r g = b := by + rw [hFp, List.countP_eq_length_filter] + exact chain_filter_lt_length_add_residualD (v := v) (E := E) b g hcard' + have hcntD : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + Fp.countP (fun x : Key => decide (x < E)) + + res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [hD, List.countP_append, List.countP_reverse] + congr 1 + apply List.countP_congr + intro x _ + rw [← hE] + simp [WithTop.coe_lt_coe] + have hanchor : dbw.keys.length ≤ (r.end_.anchor dbw).toNat := by + have hDlen : dbw.keys.length = b + res.keys.length := by + rw [show dbw.keys = D from rfl, hD, List.length_append, hFplen, List.length_reverse] + have hTle : residualT r res ≤ res.keys.length := by + rw [residualT, List.countP_eq_length_filter] + exact List.length_filter_le _ _ + have hdt := residualD_add_T_le_E (r := r) (res := res) (g := g) + ⟨db₀, hcomp⟩ hmore hrev hne htpos hbegin hcross hend hg + have hEe : residualE r = (r.end_.offset - 1).toNat := rfl + have hres_cnt := countP_lt_eq_length_sub_residualT r res + rw [anchor_toNat_eq (s := r.end_) (by omega), hDlen, hcntD, hres_cnt] + have hdt' : residualD r g + residualT r res ≤ (r.end_.offset - 1).toNat := by + simpa [hEe] using hdt + omega + have hEw : resolve dbw r.end_ = (⊤ : WithTop Key) := + resolve_eq_top (anchor_nonneg_of_one_le (db := dbw) (s := r.end_) (by omega)) hanchor + have hckw : RangeRead.completeKeys dbw r = res.keys := by + rw [completeKeys_topRange_drop_rev (db := dbw) hrev (by omega) hEw, htopw, hD, + List.drop_left' hFplen, List.reverse_reverse] + have hcompw : RangeRead.Compatible dbw r res := + ⟨hcomp.1, Or.inl ⟨hmore, by rw [hckw], hne⟩⟩ + have herase_top : (topRange r).keys (dbw.erase v) = D.erase v := by + unfold topRange ConflictRange.keys + rw [DB.erase_keys] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨by rw [hkey]; exact WithTop.coe_le_coe.mpr (hD_ge_v x (List.erase_subset hx)), + WithTop.coe_lt_top x⟩ + have hDerase : D.erase v = Fp.erase v ++ res.keys.reverse := by + rw [hD, List.erase_append_left res.keys.reverse hFp_v] + have hvD : v ∈ D := by rw [hD]; exact List.mem_append_left _ hFp_v + have hErase_anchor : (dbw.erase v).keys.length ≤ ((r.end_.anchor (dbw.erase v))).toNat := by + have hlen_erase : (dbw.erase v).keys.length = D.length - 1 := by + rw [show (dbw.erase v).keys = D.erase v from rfl, List.length_erase_of_mem hvD] + have hcnt_erase : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) - 1 ≤ + (D.erase v).countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [List.countP_erase] + split <;> omega + have hanchorD : D.length ≤ + D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + have h := hanchor + rw [show dbw.keys = D from rfl, anchor_toNat_eq (s := r.end_) (by omega)] at h + exact h + rw [anchor_toNat_eq (s := r.end_) (by omega), hlen_erase, + show (dbw.erase v).keys = D.erase v from rfl] + omega + have hEerase : resolve (dbw.erase v) r.end_ = (⊤ : WithTop Key) := + resolve_eq_top (anchor_nonneg_of_one_le (db := dbw.erase v) (s := r.end_) (by omega)) + hErase_anchor + have hnot : ¬ RangeRead.Compatible (dbw.erase v) r res := by + intro hc + have hpre := RangeRead.compatible_keys_prefix hc + have hck_erase : RangeRead.completeKeys (dbw.erase v) r = res.keys.dropLast := by + rw [completeKeys_topRange_drop_rev (db := dbw.erase v) hrev (by omega) hEerase, + herase_top, hDerase, + show (r.begin.offset - 1).toNat = ((r.begin.offset - 1).toNat - 1) + 1 by omega, + ← List.drop_drop] + rw [show ((r.begin.offset - 1).toNat - 1) = (Fp.erase v).length by + rw [List.length_erase_of_mem hFp_v, hFplen, hb]] + rw [List.drop_left' rfl, List.drop_reverse, List.reverse_reverse, ← List.dropLast_eq_take] + rw [hck_erase] at hpre + have hle := hpre.length_le + rw [List.length_dropLast] at hle + omega + intro cr hs + by_contra hvcr + exact hnot ((hs dbw (dbw.erase v) + (ConflictRange.keys_erase_of_not_mem cr dbw hvcr).symm).mp hcompw) + +/-! ### Finite-branch minimality -/ + +/-- **Finite-branch minimality for the cross-free reversed truncated residual.** On +the equality branch `d + t = e`, the truncated range `[begin.key, keyAfter head)` is +minimal: it is sound, the begin key and the reported head are necessary, and convexity +covers the whole interval. -/ +theorem minimal_truncatedRange_crossfree_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) + (hfin : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res = + residualE r) : + Minimal r res (truncatedRange r (res.keys.head hne)) := by + have hsound := sound_truncatedRange_crossfree_rev hread hmore hrev (by omega) (by omega) + hcross hne htpos hfin + refine minimal_iff_sound_and_necessaryRange.mpr ⟨hsound, ?_⟩ + intro k hklo hkhi + have hHmem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hH_nec : Necessary r res (res.keys.head hne) := necessary_of_mem_result hread hHmem + obtain ⟨E, hE⟩ := WithTop.ne_top_iff_exists.mp (end_key_ne_top_of_residualT_pos htpos) + have hcrossE : r.begin.key ≤ (↑E : WithTop Key) := by rw [hE]; exact hcross + have hbne : r.begin.key ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hcrossE + exact absurd hcrossE (not_le.mpr (WithTop.coe_lt_top E)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hbne + have hv' : r.begin.key = (↑v : WithTop Key) := hv.symm + have hv_nec : Necessary r res v := necessary_begin_key_crossfree_residual hread hrev hmore + (List.length_pos_of_ne_nil hne) hbegin hcross hend htpos hv' + have hkH : k ≤ res.keys.head hne := by + have h := WithTop.coe_lt_coe.mp hkhi + exact (lt_keyAfter_iff k (res.keys.head hne)).mp h + have hklo' : r.begin.key ≤ (↑k : WithTop Key) := hklo + have hvk : v ≤ k := by + rw [hv'] at hklo' + exact WithTop.coe_le_coe.mp hklo' + exact Necessary.of_between hv_nec hH_nec hvk hkH + +/-! ### The capacity bound and the top-branch witness -/ + +/-- In a compatible cross-free reversed truncated read, the begin offset shift `b` is +bounded by the interval cardinality up to the least reported key. -/ +theorem crossfree_rev_b_le_capBL {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (_hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (_hcross : r.begin.key ≤ r.end_.key) + (_hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) : + (((r.begin.offset - 1).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.begin.key (((res.keys.getLast hne : Key)) : WithTop Key) := by + set B : WithTop Key := r.begin.key with hB + set L : Key := res.keys.getLast hne with hL + set b : ℕ := (r.begin.offset - 1).toNat with hb + have hbpos : 1 ≤ b := by rw [hb]; omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hLck : L ∈ RangeRead.completeKeys db r := hpre.subset hLmem + have hmem := RangeRead.mem_completeKeys.mp hLck + have hres_le0 : resolve db r.begin ≤ (↑L : WithTop Key) := hmem.2.1 + have hLlt : (↑L : WithTop Key) < resolve db r.end_ := hmem.2.2 + set p : ℕ := db.keys.countP (fun x : Key => decide ((x : WithTop Key) < B)) with hp + have hanc : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) (by omega), hp, hb] + have hres_ne_top : resolve db r.begin ≠ (⊤ : WithTop Key) := + ne_of_lt (lt_of_lt_of_le (lt_of_le_of_lt hres_le0 hLlt) le_top) + have hlt : (r.begin.anchor db).toNat < db.keys.length := by + by_contra hge + exact hres_ne_top (resolve_eq_top (anchor_nonneg_of_one_le (by omega)) (not_lt.mp hge)) + have hgetres : resolve db r.begin = (↑(db.keys[(r.begin.anchor db).toNat]'hlt) : WithTop Key) := + resolve_eq_getElem (anchor_nonneg_of_one_le (by omega)) hlt + have hBne : B ≠ (⊤ : WithTop Key) := by + intro htop + have hp_top : p = db.keys.length := by + rw [hp, htop, countP_lt_top] + have hh : p + b < db.keys.length := by + rw [← hanc]; exact hlt + omega + obtain ⟨b₀, hb₀⟩ := WithTop.ne_top_iff_exists.mp hBne + have hcntB : db.keys.countP (fun x : Key => decide (x < b₀)) = + db.keys.countP (fun x : Key => decide ((x : WithTop Key) < B)) := by + apply List.countP_congr + intro x _ + simp only [decide_eq_true_eq, ← hb₀, WithTop.coe_lt_coe] + have hci : db.keys.countP (fun x : Key => decide (x < b₀)) ≤ + (r.begin.anchor db).toNat := by + rw [hcntB, ← hp, hanc] + omega + have hres_ge_B : B ≤ resolve db r.begin := by + rw [hgetres, ← hb₀] + exact WithTop.coe_le_coe.mpr (le_getElem_of_countP_le db.sorted hci hlt) + have hkey_le : db.keys[(r.begin.anchor db).toNat]'hlt ≤ L := by + have h := hres_le0 + rw [hgetres] at h + exact WithTop.coe_le_coe.mp h + have hBLe : B ≤ (↑L : WithTop Key) := le_trans hres_ge_B hres_le0 + have hcntL_ge : (r.begin.anchor db).toNat ≤ + db.keys.countP (fun x : Key => decide (x < L)) := by + have hall : ∀ x ∈ db.keys.take (r.begin.anchor db).toNat, + decide (x < L) = true := by + intro x hx + rw [List.mem_take_iff_getElem] at hx + obtain ⟨i, hi, rfl⟩ := hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (db.sorted.getElem_lt_getElem_iff.mpr (by omega)) hkey_le + have htake : (db.keys.take (r.begin.anchor db).toNat).countP + (fun x : Key => decide (x < L)) = (r.begin.anchor db).toNat := by + rw [List.countP_eq_length.mpr hall, List.length_take, Nat.min_eq_left (by omega)] + calc (r.begin.anchor db).toNat = + (db.keys.take (r.begin.anchor db).toNat).countP (fun x : Key => decide (x < L)) := + htake.symm + _ ≤ db.keys.countP (fun x : Key => decide (x < L)) := + List.Sublist.countP_le (List.take_prefix _ db.keys).sublist + have hcntL : db.keys.countP + (fun x : Key => decide ((x : WithTop Key) < (↑L : WithTop Key))) = + db.keys.countP (fun x : Key => decide (x < L)) := by + apply List.countP_congr + intro x _ + simp only [decide_eq_true_eq, WithTop.coe_lt_coe] + have hcnt := cnt_eq_add_between (db := db) (w₁ := B) (w₂ := (↑L : WithTop Key)) hBLe + simp only [cnt] at hcnt + have hcnt' : db.keys.countP (fun x : Key => decide (x < L)) = + p + (between db B (↑L : WithTop Key)).length := by + rw [← hcntL, hcnt, ← hp] + have hbetween_ge : b ≤ (between db B (↑L : WithTop Key)).length := by + have h := hcntL_ge + rw [hcnt', hanc] at h + omega + have hcast : ((b : ℕ) : ℕ∞) ≤ ((between db B (↑L : WithTop Key)).length : ℕ∞) := by + exact_mod_cast hbetween_ge + exact le_trans hcast (between_length_le_intervalCard (db := db) hBLe) + +/-! ### Above-head necessity in the top branch -/ + +/-- **Insertion core for the cross-free reversed residual.** Given a database whose keys +are a pad `P` followed by the reversed reported keys `R`, inserting a key `k` above the +reported head appends it to the complete window; if the window was already the reversed +reported keys, the inserted window starts with `k`, contradicting the reported head. -/ +private theorem necessary_above_head_of_pad {r : RangeRead} {res : Result} + {db₀ dbfull : DB} {P R : List Key} {H k : Key} + (hcomp₀ : RangeRead.Compatible db₀ r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (hH : H = res.keys.head hne) + (hR : R = res.keys.reverse) + (hdb : dbfull.keys = P ++ R) + (hPlen : P.length = (r.begin.offset - 1).toNat) + (hBmem : ∀ x ∈ P ++ R, r.begin.key ≤ (x : WithTop Key)) + (hPk : ∀ x ∈ P, x < k) (hRk : ∀ y ∈ R, y < k) + (hkH : H < k) (hkB : r.begin.key ≤ (k : WithTop Key)) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hg : (r.begin.offset - 1).toNat + R.length < + (between dbfull r.begin.key r.end_.key).length + (r.end_.offset - 1).toNat) : + Necessary r res k := by + set b : ℕ := (r.begin.offset - 1).toNat with hb + set e : ℕ := (r.end_.offset - 1).toNat with he + have hk_not : k ∉ dbfull.keys := by + rw [hdb] + intro hk + rcases List.mem_append.mp hk with hk | hk + · exact absurd (hPk k hk) (lt_irrefl k) + · exact absurd (hRk k hk) (lt_irrefl k) + have hall_lt : ∀ x ∈ dbfull.keys, x < k := by + rw [hdb] + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hPk x hx + · exact hRk x hx + have hins : (dbfull.insert k hk_not).keys = dbfull.keys ++ [k] := by + rw [DB.insert_keys] + exact orderedInsert_eq_append_of_forall_lt (l := dbfull.keys) (k := k) hall_lt + have haboveB : above dbfull r.begin.key = P ++ R := by + rw [above_eq_filter, hdb, List.filter_eq_self] + intro x hx + exact decide_eq_true (hBmem x hx) + have hBmem_ins : ∀ x ∈ (P ++ R) ++ [k], r.begin.key ≤ (x : WithTop Key) := by + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hBmem x hx + · rw [List.mem_singleton] at hx + subst x + exact hkB + have hbetween_ins : (between (dbfull.insert k hk_not) r.begin.key r.end_.key).length = + (between dbfull r.begin.key r.end_.key).length := by + have hfilter : List.filter (fun x : Key => decide + (r.begin.key ≤ (x : WithTop Key) ∧ (x : WithTop Key) < r.end_.key)) [k] = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [List.mem_singleton] at hx + subst x + rw [decide_eq_true_eq] + intro h + exact absurd h.2 (not_lt.mpr hkE) + unfold between + rw [hins, hdb, List.filter_append, hfilter, List.append_nil] + have haboveB_ins : above (dbfull.insert k hk_not) r.begin.key = (P ++ R) ++ [k] := by + rw [above_eq_filter, hins, hdb, List.filter_eq_self] + intro x hx + exact decide_eq_true (hBmem_ins x hx) + have hckFull : RangeRead.completeKeys dbfull r = res.keys := by + rw [completeKeys_crossfree_rev_eq (db := dbfull) (r := r) hrev hbegin hend hcross, + haboveB] + have hge : (P ++ R).length ≤ + (between dbfull r.begin.key r.end_.key).length + e := by + rw [List.length_append, hPlen, hb, he] + omega + rw [List.take_of_length_le hge, List.drop_left' hPlen, hR, List.reverse_reverse] + have hckIns : RangeRead.completeKeys (dbfull.insert k hk_not) r = [k] ++ res.keys := by + rw [completeKeys_crossfree_rev_eq (db := dbfull.insert k hk_not) (r := r) + hrev hbegin hend hcross, haboveB_ins, hbetween_ins] + have hge : ((P ++ R) ++ [k]).length ≤ + (between dbfull r.begin.key r.end_.key).length + e := by + rw [List.length_append, List.length_singleton, List.length_append, hPlen, hb, he] + omega + rw [List.take_of_length_le hge, List.append_assoc, List.drop_left' hPlen, + List.reverse_append, hR, List.reverse_reverse, List.reverse_singleton] + have hcompFull : RangeRead.Compatible dbfull r res := + ⟨hcomp₀.1, Or.inl ⟨hmore, by rw [hckFull], hne⟩⟩ + refine necessary_of_toggle ⟨dbfull, hcompFull, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not] + intro hcomp' + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hpre' : res.keys <+: [k] ++ res.keys := by + rcases hcomp'.2 with ⟨_, hp, _⟩ | ⟨hm, _⟩ + · rw [hckIns] at hp + exact hp + · rw [hmore] at hm + simp at hm + have h0 := hpre'.getElem (i := 0) htpos + have hlhs : res.keys[0] = H := by + simpa [hH] using (List.getElem_zero htpos) + have hrhs : ([k] ++ res.keys)[0] = k := by + simp [List.getElem_cons_zero] + rw [hlhs, hrhs] at h0 + exact absurd hkH (not_lt_of_ge (le_of_eq h0.symm)) + +/-- **Top-branch above-head necessity for the cross-free reversed residual.** Every key +strictly above the reported head is necessary in the top branch `d + t < e`. The witness +pads the reversed reported keys with `b` keys below the reported last, chosen so that the +complete window is exactly the reported keys; inserting a key above the head then makes +it the new head of the reversed window. -/ +theorem necessary_above_head_crossfree_residual {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) + (htop : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res < + residualE r) + {k : Key} (hkH : res.keys.head hne < k) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set B : WithTop Key := r.begin.key with hB + set E : WithTop Key := r.end_.key with hE + set e : ℕ := (r.end_.offset - 1).toNat with he + set b : ℕ := (r.begin.offset - 1).toNat with hb + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set R : List Key := res.keys.reverse with hR + have hbegin1 : 1 ≤ r.begin.offset := by omega + have hhend1 : 1 ≤ r.end_.offset := by omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne + have hLck : L ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hLmem + have hBL : B ≤ (↑L : WithTop Key) := + le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) hbegin1) + (RangeRead.mem_completeKeys.mp hLck).2.1 + have hbcard : ((b : ℕ) : ℕ∞) ≤ intervalCard B (↑L) := by + simpa [B, L, b] using crossfree_rev_b_le_capBL (db := db₀) (r := r) (res := res) + hcomp₀ hrev hbegin (by simpa [B, E] using hcross) hhend1 hne + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLleH : L ≤ H := by + have h := (hsort.pairwise.imp le_of_lt).rel_getLast hHmem + simpa [hL, hH] using h + have hHE : E ≤ ((H : Key) : WithTop Key) := by + simpa [E, H] using head_ge_end_of_residualT_pos hsort hne htpos + have hRsort : R.SortedLT := by rw [hR]; exact hsort.reverse + have hRmem : ∀ y ∈ R, L ≤ y := by + intro y hy + have hyres : y ∈ res.keys := by rw [hR] at hy; exact List.mem_reverse.mp hy + exact (hsort.pairwise.imp le_of_lt).rel_getLast hyres + have hRleH : ∀ y ∈ R, y ≤ H := by + intro y hy + have hyres : y ∈ res.keys := by rw [hR] at hy; exact List.mem_reverse.mp hy + simpa [hH] using (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hyres + have hTle : residualT r res ≤ res.keys.length := by + rw [residualT, List.countP_eq_length_filter] + exact List.length_filter_le _ _ + have hTe : residualT r res < e := by + have h : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res < e := by + simpa [he, residualE] using htop + omega + have hRcnt : (R.filter (fun y : Key => decide ((y : WithTop Key) < E))).length = + res.keys.length - residualT r res := by + rw [← List.countP_eq_length_filter, hR, List.countP_reverse] + simpa [E] using countP_lt_eq_length_sub_residualT r res + have hkB : B ≤ (k : WithTop Key) := + le_trans hBL (le_trans (WithTop.coe_le_coe.mpr hLleH) + (WithTop.coe_le_coe.mpr (le_of_lt hkH))) + have hkE : r.end_.key ≤ (k : WithTop Key) := by + refine le_trans ?_ (WithTop.coe_le_coe.mpr (le_of_lt hkH)) + simpa [E] using hHE + by_cases hLE : L ≤ E + · obtain ⟨pad, hpadIn, hpadlen⟩ := + exists_db_keysIn (B := B) (E := (↑L)) (m := b) hbcard + set P : List Key := pad.keys with hPdef + have hPsort : P.SortedLT := pad.sorted + have hPmem : ∀ x ∈ P, B ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < (↑L : WithTop Key) := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx + have hPlen : P.length = b := hpadlen + have hPleL : ∀ x ∈ P, x < L := fun x hx => + (WithTop.coe_lt_coe (a := L) (b := x)).mp (hPmem x hx).2 + have hPR : ∀ x ∈ P, ∀ y ∈ R, x < y := + fun x hx y hy => lt_of_lt_of_le (hPleL x hx) (hRmem y hy) + have hfullSort : (P ++ R).SortedLT := + List.sortedLT_append.mpr ⟨hPsort, hRsort, hPR⟩ + set dbfull : DB := ⟨P ++ R, hfullSort⟩ with hdbfull + have hdb : dbfull.keys = P ++ R := rfl + have hPall : ∀ x ∈ P, decide (B ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < E) = true := by + intro x hx + rw [decide_eq_true_eq] + exact ⟨(hPmem x hx).1, lt_of_lt_of_le (hPmem x hx).2 hLE⟩ + have hPfil : P.filter (fun x : Key => decide + (B ≤ (x : WithTop Key) ∧ (x : WithTop Key) < E)) = P := + (List.filter_eq_self).mpr hPall + have hRfil : R.filter (fun y : Key => decide + (B ≤ (y : WithTop Key) ∧ (y : WithTop Key) < E)) = + R.filter (fun y : Key => decide ((y : WithTop Key) < E)) := by + apply List.filter_congr + intro y hy + have hBy : B ≤ (y : WithTop Key) := + le_trans hBL (WithTop.coe_le_coe.mpr (hRmem y hy)) + by_cases h : (y : WithTop Key) < E <;> simp [h, hBy] + have hbetween : (between dbfull B E).length = + b + (res.keys.length - residualT r res) := by + unfold between + rw [show dbfull.keys = P ++ R from hdb, List.filter_append, hPfil, hRfil, + List.length_append, hRcnt, hPlen] + have hBmem : ∀ x ∈ P ++ R, B ≤ (x : WithTop Key) := by + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact (hPmem x hx).1 + · exact le_trans hBL (WithTop.coe_le_coe.mpr (hRmem x hx)) + have hPk : ∀ x ∈ P, x < k := fun x hx => + lt_of_lt_of_le (hPleL x hx) (le_trans hLleH (le_of_lt hkH)) + have hRk : ∀ y ∈ R, y < k := fun y hy => + lt_of_le_of_lt (hRleH y hy) hkH + have hg : b + R.length < (between dbfull B E).length + e := by + rw [hbetween, hR, List.length_reverse] + omega + exact necessary_above_head_of_pad hcomp₀ hmore hrev hbegin1 hcross hhend1 hne + hH hR hdb hPlen hBmem hPk hRk hkH hkB hkE hg + · have hEL : E < (↑L : WithTop Key) := lt_of_not_ge hLE + set c : ℕ∞ := intervalCard B E with hc + set p : ℕ := if c = ⊤ then b else min b c.toNat with hp + have hp_le_c : ((p : ℕ) : ℕ∞) ≤ c := by + rw [hp] + by_cases hc' : c = ⊤ + · rw [if_pos hc']; simp [hc'] + · rw [if_neg hc'] + obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hc' + rw [← hn, ENat.toNat_natCast] + exact_mod_cast Nat.min_le_right b n + have hDp_le : (((b - p : ℕ)) : ℕ∞) ≤ intervalCard E (↑L) := by + rw [hp] + by_cases hc' : c = ⊤ + · rw [if_pos hc']; simp + · rw [if_neg hc'] + obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hc' + have hp_eq : min b c.toNat = min b n := by rw [← hn, ENat.toNat_natCast] + rw [hp_eq] + have hbL : ((b : ℕ) : ℕ∞) ≤ intervalCard B (↑L) := by + simpa [B, L, b] using hbcard + have hadd := intervalCard_add (b := B) (m := E) (e := (↑L)) + (by simpa [B, E] using hcross) (le_of_lt hEL) + rw [hadd, ← hc, ← hn] at hbL + by_cases hcEL : intervalCard E (↑L) = ⊤ + · rw [hcEL] + exact le_top + · obtain ⟨m, hm⟩ := ENat.ne_top_iff_exists.mp hcEL + rw [← hm] at hbL + rw [← hm] + have hbm : b ≤ n + m := by + rw [← Nat.cast_add] at hbL + exact ENat.coe_le_coe.mp hbL + exact_mod_cast (by omega : b - min b n ≤ m) + obtain ⟨pad1, hpad1In, hpad1len⟩ := + exists_db_keysIn (B := B) (E := E) (m := p) hp_le_c + obtain ⟨pad2, hpad2In, hpad2len⟩ := + exists_db_keysIn (B := E) (E := (↑L)) (m := b - p) hDp_le + set P1 : List Key := pad1.keys with hP1def + set P2 : List Key := pad2.keys with hP2def + set P : List Key := P1 ++ P2 with hPdef + have hP1sort : P1.SortedLT := pad1.sorted + have hP2sort : P2.SortedLT := pad2.sorted + have hP1mem : ∀ x ∈ P1, B ≤ (x : WithTop Key) ∧ (x : WithTop Key) < E := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpad1In x hx + have hP2mem : ∀ x ∈ P2, E ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < (↑L : WithTop Key) := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpad2In x hx + have hP1len : P1.length = p := hpad1len + have hP2len : P2.length = b - p := hpad2len + have hpleb : p ≤ b := by + rw [hp] + by_cases hc' : c = ⊤ + · simp [hp, hc'] + · rw [if_neg hc']; exact Nat.min_le_left b _ + have hPlen : P.length = b := by + rw [hPdef, List.length_append, hP1len, hP2len] + omega + have hP12 : ∀ x ∈ P1, ∀ y ∈ P2, x < y := by + intro x hx y hy + exact (WithTop.coe_lt_coe (a := y) (b := x)).mp + (lt_of_lt_of_le (hP1mem x hx).2 (hP2mem y hy).1) + have hPsort : P.SortedLT := by + rw [hPdef] + exact List.sortedLT_append.mpr ⟨hP1sort, hP2sort, hP12⟩ + have hPleL : ∀ x ∈ P, x < L := by + intro x hx + rw [hPdef, List.mem_append] at hx + rcases hx with hx | hx + · exact (WithTop.coe_lt_coe (a := L) (b := x)).mp + (lt_of_lt_of_le (hP1mem x hx).2 (le_of_lt hEL)) + · exact (WithTop.coe_lt_coe (a := L) (b := x)).mp (hP2mem x hx).2 + have hPmemB : ∀ x ∈ P, B ≤ (x : WithTop Key) := by + intro x hx + rw [hPdef, List.mem_append] at hx + rcases hx with hx | hx + · exact (hP1mem x hx).1 + · exact le_trans (by simpa [B, E] using hcross) (hP2mem x hx).1 + have hPR : ∀ x ∈ P, ∀ y ∈ R, x < y := + fun x hx y hy => lt_of_lt_of_le (hPleL x hx) (hRmem y hy) + have hfullSort : (P ++ R).SortedLT := + List.sortedLT_append.mpr ⟨hPsort, hRsort, hPR⟩ + set dbfull : DB := ⟨P ++ R, hfullSort⟩ with hdbfull + have hdb : dbfull.keys = P ++ R := rfl + have hP1all : ∀ x ∈ P1, decide (B ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < E) = true := by + intro x hx + rw [decide_eq_true_eq] + exact hP1mem x hx + have hP1fil : P1.filter (fun x : Key => decide + (B ≤ (x : WithTop Key) ∧ (x : WithTop Key) < E)) = P1 := + (List.filter_eq_self).mpr hP1all + have hP2nil : P2.filter (fun x : Key => decide + (B ≤ (x : WithTop Key) ∧ (x : WithTop Key) < E)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro h + exact absurd h.2 (not_lt.mpr (hP2mem x hx).1) + have hRfil : R.filter (fun y : Key => decide + (B ≤ (y : WithTop Key) ∧ (y : WithTop Key) < E)) = + R.filter (fun y : Key => decide ((y : WithTop Key) < E)) := by + apply List.filter_congr + intro y hy + have hBy : B ≤ (y : WithTop Key) := + le_trans hBL (WithTop.coe_le_coe.mpr (hRmem y hy)) + by_cases h : (y : WithTop Key) < E <;> simp [h, hBy] + have hbetween : (between dbfull B E).length = + p + (res.keys.length - residualT r res) := by + unfold between + rw [show dbfull.keys = P ++ R from hdb, hPdef, List.append_assoc, + List.filter_append, List.filter_append, hP1fil, hP2nil, List.nil_append, + hRfil, List.length_append, hRcnt, hP1len] + have hBmem : ∀ x ∈ P ++ R, B ≤ (x : WithTop Key) := by + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hPmemB x hx + · exact le_trans hBL (WithTop.coe_le_coe.mpr (hRmem x hx)) + have hPk : ∀ x ∈ P, x < k := fun x hx => + lt_of_lt_of_le (hPleL x hx) (le_trans hLleH (le_of_lt hkH)) + have hRk : ∀ y ∈ R, y < k := fun y hy => + lt_of_le_of_lt (hRleH y hy) hkH + have hbT_lt : b + residualT r res < p + e := by + have htop' : residualD r c + residualT r res < e := by + simpa [c, e, B, E, residualE] using htop + rw [hp] + by_cases hc' : c = ⊤ + · rw [if_pos hc'] + rw [residualD, if_pos hc'] at htop' + omega + · rw [if_neg hc'] + obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hc' + have hD : residualD r c = b - n := by + rw [residualD, if_neg hc', ← hn, ENat.toNat_natCast, residualB, ← hb] + rw [hD] at htop' + rw [← hn, ENat.toNat_natCast] + omega + have hg : b + R.length < (between dbfull B E).length + e := by + rw [hbetween, hR, List.length_reverse] + omega + exact necessary_above_head_of_pad hcomp₀ hmore hrev hbegin1 hcross hhend1 hne + hH hR hdb hPlen hBmem hPk hRk hkH hkB hkE hg + +/-- **Top-branch minimality for the cross-free reversed truncated residual.** In the top +branch `d + t < e` the top range `[begin.key, ⊤)` is minimal: it is sound, the begin key +is necessary, the reported head is necessary, and everything above the head is necessary +by `necessary_above_head_crossfree_residual`; convexity covers the rest. -/ +theorem minimal_topRange_crossfree_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) + (htop : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res < + residualE r) : + Minimal r res (topRange r) := by + have hbegin1 : 1 ≤ r.begin.offset := by omega + have hhend1 : 1 ≤ r.end_.offset := by omega + have hsound : Sound r res (topRange r) := by + rw [topRange] + exact sound_topRange_of_one_le_le hbegin1 hhend1 hcross + refine minimal_iff_sound_and_necessaryRange.mpr ⟨hsound, ?_⟩ + intro k hklo hkhi + change r.begin.key ≤ (k : WithTop Key) at hklo + change (k : WithTop Key) < (⊤ : WithTop Key) at hkhi + have hHmem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hH_nec : Necessary r res (res.keys.head hne) := necessary_of_mem_result hread hHmem + by_cases hkH : k ≤ res.keys.head hne + · have hbne : r.begin.key ≠ (⊤ : WithTop Key) := by + intro htopb + rw [htopb] at hklo + exact absurd hklo (not_le.mpr (WithTop.coe_lt_top k)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hbne + have hv' : r.begin.key = (↑v : WithTop Key) := hv.symm + have hv_nec : Necessary r res v := necessary_begin_key_crossfree_residual hread hrev + hmore (List.length_pos_of_ne_nil hne) hbegin hcross hend htpos hv' + have hvk : v ≤ k := by + have h := hklo + rw [hv'] at h + exact WithTop.coe_le_coe.mp h + exact Necessary.of_between hv_nec hH_nec hvk hkH + · have hkH' : res.keys.head hne < k := lt_of_not_ge hkH + exact necessary_above_head_crossfree_residual hread hmore hrev hbegin hcross hend hne + htpos htop hkH' + +/-- **The cross-free reversed truncated residual closed form.** It is the truncated range +`[begin.key, keyAfter head)` in the finite branch `d + t = e`, and the top range +`[begin.key, ⊤)` in the top branch. -/ +noncomputable def closedFormCrossFreeResidual (r : RangeRead) (res : Result) : ConflictRange := + if residualD r (intervalCard r.begin.key r.end_.key) + residualT r res = residualE r then + truncatedRange r (res.keys.headD ([] : Key)) + else topRange r + +/-- The head of a nonempty result is its `headD`. -/ +theorem headD_eq_head_of_ne_nil {l : List Key} (hne : l ≠ []) : + l.headD ([] : Key) = l.head hne := by + cases l with + | nil => exact absurd rfl hne + | cons a t => rfl + +/-- **The cross-free reversed truncated residual closed form is minimal.** -/ +theorem minimal_closedForm_of_crossfree_residual {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) : + Minimal r res (closedFormCrossFreeResidual r res) := by + unfold closedFormCrossFreeResidual + by_cases hfin : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res = + residualE r + · rw [ite_eq_left hfin, headD_eq_head_of_ne_nil hne] + exact minimal_truncatedRange_crossfree_rev hread hmore hrev hbegin hcross hend hne htpos hfin + · rw [ite_eq_right hfin] + have hle := residualD_add_T_le_E (g := intervalCard r.begin.key r.end_.key) + hread hmore hrev hne htpos hbegin hcross hend rfl + have hlt : residualD r (intervalCard r.begin.key r.end_.key) + residualT r res < + residualE r := lt_of_le_of_ne hle hfin + exact minimal_topRange_crossfree_rev hread hmore hrev hbegin hcross hend hne htpos hlt + +/-- The cross-free reversed truncated residual closed form denotes the general conflict +range. -/ +theorem closedFormCrossFreeResidual_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) : + (closedFormCrossFreeResidual r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_closedForm_of_crossfree_residual hread hmore hrev hbegin hcross hend hne htpos).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The cross-free reversed truncated residual closed form denotes the executable +algorithm. -/ +theorem closedFormCrossFreeResidual_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (htpos : 0 < residualT r res) : + (closedFormCrossFreeResidual r res).toSet = (simpleConflictRange r res).toSet := + (minimal_closedForm_of_crossfree_residual hread hmore hrev hbegin hcross hend hne htpos).toSet_eq + (simpleConflictRange_minimal hread) + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedClosedForm.lean b/FormalConflictRange/CrossedClosedForm.lean new file mode 100644 index 0000000..19aa001 --- /dev/null +++ b/FormalConflictRange/CrossedClosedForm.lean @@ -0,0 +1,893 @@ +/- +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.Empty +import FormalConflictRange.ClosedForm + +/-! +# Crossed selectors at begin offset one + +This module closes the crossed cell `end_.key < begin_.key` of the compact closed form +at `begin.offset = 1`. The complete window starts at the first stored key at or above +`begin_.key`, while the end resolution counts stored keys from `end_.key`; the two +selector keys therefore cut the database at different points. + +The main results are: + +* `minimal_topRange_of_crossed_large_room` and + `minimal_topRange_of_crossed_small_room`: the top branch `t < e`, split by whether the + gap `[end_.key, begin_.key)` has room for the overshoot `e - t`; +* `minimal_truncatedRange_of_finite_crossed` (from `FormalConflictRange.Sound`): the + finite branch `t = e`; +* `minimal_completeForward_of_crossed`: the unified crossed forward-complete rule, + implemented by `completeForwardClosed`. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- In a readable crossed complete result at begin offset one, every reported key lies +at or above `begin.key`. -/ +theorem crossed_mem_result_ge_begin {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hbegin : r.begin.offset = 1) : + ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + intro k hk + have hk' : k ∈ RangeRead.completeKeys db₀ r := by rw [← hres]; exact hk + exact le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) (by omega)) + (RangeRead.mem_completeKeys.mp hk').2.1 + +/-- In a crossed complete result at begin offset one, the reported keys at or above +`end_.key` are all reported keys, so the count equals the length. -/ +theorem crossed_complete_t_eq_length {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) : + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + res.keys.length := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have h := completeKeys_count_ge_eq_length_of_crossed (db := db₀) (r := r) + hrev hbegin hcross hend + rw [← hres] at h + exact h + +/-- In a readable crossed complete result at begin offset one with positive end offset, +the number of reported keys at or above `end_.key` is at most `end_.offset - 1`. -/ +theorem crossed_complete_t_le_e {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (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 htlen := crossed_complete_t_eq_length hread hmore hrev hbegin hcross (by omega) + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hlen := completeKeys_length_of_offset_one (db := db₀) (r := r) hrev hbegin hend + rw [← hres] at hlen + rw [htlen] + have hcEp : db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) ≤ + db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_trans hk hcross + calc res.keys.length + = min (db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db₀.keys.length + - db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := hlen + _ ≤ (db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) + - db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := + Nat.sub_le_sub_right (Nat.min_le_left _ _) _ + _ ≤ (r.end_.offset - 1).toNat := by omega + +end FormalConflictRange + +namespace FormalConflictRange + +/-- When there is enough room below `begin.key` (in the sense that the actual gap count +plus the reported length is below `end.offset - 1`), a crossed complete result is +determined exactly by the stored keys at or above `begin.key`. -/ +theorem completeKeys_eq_iff_filter_ge_begin_of_room {db : DB} {r : RangeRead} + {res : Result} (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hend : 1 ≤ r.end_.offset) (hcross : r.end_.key ≤ r.begin.key) + (hne : res.keys ≠ []) + (hroom : res.keys.length + + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + < (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = res.keys ↔ + db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + res.keys := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set e := (r.end_.offset - 1).toNat with he + set t := res.keys.length with ht + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + set q := l.length - p with hq + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hcEp : cE ≤ p := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hcross + have hp_le_n : p ≤ l.length := by rw [hp]; exact List.countP_le_length + have hroom' : t + (p - cE) < e := by simpa [hp, hcE, he, ht] using hroom + have htpos : 0 < t := List.length_pos_of_ne_nil hne + have hA : A = l.drop p := by + have hdrop : l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) = + l.drop p := by + rw [hp] + exact filter_not_lt_eq_drop_countP hls r.begin.key + rw [hAdef, ← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hAlen : A.length = q := by rw [hA, List.length_drop, hq] + have hsplit : l.length = cE + (p - cE) + q := by + rw [hq] + omega + have hlen : RangeRead.completeKeys db r = + A.take (min (cE + e) l.length - p) := by + rw [completeKeys_eq_filter_take_of_offset_one hrev hbegin hend] + constructor + · intro hck + have hcklen : (RangeRead.completeKeys db r).length = t := by rw [hck, ht] + have hLt : min (cE + e) l.length - p = t := by + rw [hlen, List.length_take] at hcklen + have hAq : A.length = q := hAlen + omega + -- Suppose q > t; then both arguments of min exceed p + t. + have hqle : q ≤ t := by + by_contra hqt + have hq_gt : t < q := lt_of_not_ge hqt + have h1 : p + t < cE + e := by omega + have h2 : p + t < l.length := by omega + have hmin : p + t < min (cE + e) l.length := lt_min h1 h2 + omega + have hqge : t ≤ q := by + have := hLt + have hle : min (cE + e) l.length - p ≤ l.length - p := + Nat.sub_le_sub_right (Nat.min_le_right _ _) _ + omega + have hqt : q = t := le_antisymm hqle hqge + -- hence `take` keeps all of `A` + have hLge : A.length ≤ min (cE + e) l.length - p := by + rw [hAlen, hqt] + omega + have htake : A.take (min (cE + e) l.length - p) = A := + List.take_of_length_le hLge + exact htake.symm.trans (hlen.symm.trans hck) + · intro hfil + have hAres : A = res.keys := by rw [hAdef]; exact hfil + have hqt : q = t := by rw [← hAlen, hAres, ht] + have hL : min (cE + e) l.length - p = t := by + have hn : l.length = p + t := by omega + have h1 : l.length < cE + e := by omega + rw [Nat.min_eq_right (le_of_lt h1), hn] + omega + rw [hlen, hL, hfil, ht, List.take_length] + +end FormalConflictRange + +namespace FormalConflictRange + +/-- Move the end selector's key up to the begin key, keeping its offset. In the crossed +complete case with little room below the begin key, this read has the same compatible +databases as the original. -/ +def endKeyToBegin (r : RangeRead) : RangeRead := + { r with end_ := { r.end_ with key := r.begin.key } } + +theorem compatible_crossed_iff_endKeyToBegin_of_room {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.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hroom : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) : ℕ) : ℕ∞)) : + ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (endKeyToBegin r) res := by + have htlen := crossed_complete_t_eq_length hread hmore hrev hbegin hcross (by omega) + have htle := crossed_complete_t_le_e hread hmore hrev hbegin hcross (by omega) + have hresne := hne + intro db + have hbase : ∀ (r' : RangeRead), + r'.reverse = false ∧ r'.begin.offset = 1 ∧ 1 ≤ r'.end_.offset ∧ + r'.end_.key ≤ r'.begin.key → + (res.keys.length + + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r'.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r'.end_.key))) + < (r'.end_.offset - 1).toNat) → + (RangeRead.Compatible db r' res ↔ + (RangeRead.withinLimit r' res.keys ∧ + db.keys.filter (fun k : Key => decide (r'.begin.key ≤ (k : WithTop Key))) = + res.keys)) := by + intro r' hprops hroom' + obtain ⟨hrev', hbegin', hend', hcross'⟩ := hprops + constructor + · rintro ⟨hlim, hdisj | ⟨_, heq⟩⟩ + · simp [hmore] at hdisj + · exact ⟨hlim, (completeKeys_eq_iff_filter_ge_begin_of_room + (db := db) (r := r') (res := res) hrev' hbegin' hend' hcross' hresne hroom').mp + heq.symm⟩ + · rintro ⟨hlim, hfil⟩ + refine ⟨hlim, Or.inr ⟨hmore, ?_⟩⟩ + exact ((completeKeys_eq_iff_filter_ge_begin_of_room + (db := db) (r := r') (res := res) hrev' hbegin' hend' hcross' hresne hroom').mpr + hfil).symm + have hgap_le : ((db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key := by + simpa [cnt] using cnt_sub_le_intervalCard (db := db) (w₁ := r.end_.key) (w₂ := r.begin.key) (le_of_lt hcross) + -- gap plus reported length stays below the end offset budget + rw [htlen] at hroom + have hgap_lt : (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) < + (r.end_.offset - 1).toNat - res.keys.length := by + have h1 : ((db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) : ℕ) : ℕ∞) < + (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) := + lt_of_le_of_lt hgap_le hroom + exact_mod_cast h1 + have hroom_db : res.keys.length + + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + < (r.end_.offset - 1).toNat := by omega + have hbase_r := hbase r ⟨hrev, hbegin, by omega, le_of_lt hcross⟩ hroom_db + -- the modified read + have hbase_r' := hbase (endKeyToBegin r) + ⟨by simp [endKeyToBegin, hrev], by simp [endKeyToBegin, hbegin], + by simp [endKeyToBegin]; omega, by simp [endKeyToBegin]⟩ + (by + have htlt : res.keys.length < (r.end_.offset - 1).toNat := by omega + change res.keys.length + + (List.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) db.keys - + List.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) db.keys) < + (r.end_.offset - 1).toNat + rw [Nat.sub_self, Nat.add_zero] + exact htlt) + rw [hbase_r, hbase_r'] + rfl + +end FormalConflictRange + +namespace FormalConflictRange + +/-- **Crossed complete top branch with little room below the begin key.** If the interval +between the two selector keys holds fewer keys than the overshoot `e - t`, then no key in +that gap can be necessary, and the minimal range is `[begin.key, ⊤)`. Moving the end key +up to the begin key makes the read cross-free with the same compatible databases. -/ +theorem minimal_topRange_of_crossed_small_room {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.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hroom : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) : ℕ) : ℕ∞)) : + Minimal r res ⟨r.begin.key, ⊤⟩ := by + let r' := endKeyToBegin r + have hiff := compatible_crossed_iff_endKeyToBegin_of_room hread hmore hne hrev hbegin + hcross hend hroom + have hall_ge : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := + crossed_mem_result_ge_begin hread hmore hbegin + have htlen := crossed_complete_t_eq_length hread hmore hrev hbegin hcross (by omega) + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r' res := ⟨db₀, (hiff db₀).mp hcomp₀⟩ + have ht' : res.keys.countP + (fun k : Key => decide (r'.end_.key ≤ (k : WithTop Key))) < + (r'.end_.offset - 1).toNat := by + have hcnt : res.keys.countP + (fun k : Key => decide (r'.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + simpa [r', endKeyToBegin] using hall_ge k hk + rw [hcnt, ← htlen] + simpa [r', endKeyToBegin] using ht + have hmin' : Minimal r' res (topRange r') := + minimal_topRange_of_top_branch hread' hmore (by simp [r', endKeyToBegin, hrev]) + hne (by simp [r', endKeyToBegin, hbegin]) (by simp [r', endKeyToBegin]) hend ht' + have hmin'' : Minimal r' res ⟨r.begin.key, ⊤⟩ := by + simpa [r', endKeyToBegin, topRange] using hmin' + exact minimal_of_compatible_iff hiff hmin'' + +end FormalConflictRange + +namespace FormalConflictRange + +/-- In the crossed complete top branch, every key at or above `begin.key` is necessary. -/ +theorem necessary_of_begin_le_crossed_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + {x : Key} (hx : r.begin.key ≤ (x : WithTop Key)) : + Necessary r res x := by + have hread₀ := hread + obtain ⟨db₀, hcomp₀⟩ := hread + have hall_ge : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := + crossed_mem_result_ge_begin hread₀ hmore hbegin + by_cases hxmem : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hcomp₀⟩ hxmem + · have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + let d₀ : DB := ⟨res.keys, hsort⟩ + have hfil₀ : res.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact hall_ge k hk + have hp₀ : res.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + have hle := hall_ge k hk + rw [decide_eq_true_eq] + exact not_lt.mpr hle + have hcE₀ : res.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + have hle := le_trans (le_of_lt hcross) (hall_ge k hk) + rw [decide_eq_true_eq] + exact not_lt.mpr hle + have hroom₀ : res.keys.length + + (res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + < (r.end_.offset - 1).toNat := by + rw [hp₀, hcE₀] + have htlen := crossed_complete_t_eq_length hread₀ hmore hrev hbegin hcross (by omega) + simpa [htlen] using ht + have hcompD₀ : RangeRead.Compatible d₀ r res := by + have hkey := (completeKeys_eq_iff_filter_ge_begin_of_room + (db := d₀) (r := r) (res := res) hrev hbegin (by omega) (le_of_lt hcross) + hne hroom₀).mpr hfil₀ + refine ⟨hcomp₀.1, Or.inr ⟨hmore, hkey.symm⟩⟩ + intro cr hs + by_contra hxcr + have hknot : x ∉ d₀.keys := hxmem + let d₁ : DB := d₀.insert x hknot + have hall₁ : ∀ k ∈ d₁.keys, r.begin.key ≤ (k : WithTop Key) := by + intro k hk + rw [show d₁ = d₀.insert x hknot from rfl, DB.mem_insert_iff hknot] at hk + rcases hk with rfl | hk + · exact hx + · exact hall_ge k hk + have hfil₁ : d₁.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = d₁.keys := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact hall₁ k hk + have hp₁ : d₁.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + have hle := hall₁ k hk + rw [decide_eq_true_eq] + exact not_lt.mpr hle + have hcE₁ : d₁.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + have hle := le_trans (le_of_lt hcross) (hall₁ k hk) + rw [decide_eq_true_eq] + exact not_lt.mpr hle + have hroom₁ : res.keys.length + + (d₁.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + d₁.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + < (r.end_.offset - 1).toNat := by + rw [hp₁, hcE₁] + have htlen := crossed_complete_t_eq_length hread₀ hmore hrev hbegin hcross (by omega) + simpa [htlen] using ht + have hcomplen : d₁.keys.length = res.keys.length + 1 := by + rw [show d₁ = d₀.insert x hknot from rfl, DB.insert_keys, List.orderedInsert_length] + have hfilne : d₁.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) ≠ res.keys := by + intro heq + have hlen : (d₁.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).length = + res.keys.length + 1 := by rw [hfil₁, hcomplen] + rw [heq] at hlen + omega + -- compatibility transfer along the sound range + have hagree : AgreeOn cr d₀ d₁ := + (ConflictRange.keys_insert_of_not_mem cr d₀ hknot hxcr).symm + have hcompD₁ : RangeRead.Compatible d₁ r res := (hs d₀ d₁ hagree).mp hcompD₀ + have hkey₁ : d₁.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = res.keys := by + rcases hcompD₁.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact (completeKeys_eq_iff_filter_ge_begin_of_room + (db := d₁) (r := r) (res := res) hrev hbegin (by omega) (le_of_lt hcross) + hne hroom₁).mp heq.symm + exact hfilne hkey₁ + +end FormalConflictRange + +namespace FormalConflictRange + +/-- **Necessity of every key in `[end_.key, begin_.key)` in the crossed top branch.** With +enough room in the gap, a key `k` with `E ≤ k < B` can be toggled: a database of `s - 1` +gap keys, the reported keys, and one extra key above the reported last has complete +window of length `t + 1`, while inserting `k` restores the reported keys. -/ +theorem necessary_of_mem_interval_crossed_top {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hroom : (((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + set t : ℕ := res.keys.length with htdef + set eo : ℕ := (r.end_.offset - 1).toNat with heodef + have hte : t < eo := by + have hcount := crossed_complete_t_eq_length hread hmore hrev hbegin hcross (by omega) + rw [hcount] at ht + simpa [t, eo, htdef, heodef] using ht + set s : ℕ := eo - t with hsdef + have hs : 0 < s := by omega + have hsub : s - 1 + 1 = s := Nat.sub_add_cancel (by omega : 1 ≤ s) + have htlen := crossed_complete_t_eq_length hread hmore hrev hbegin hcross (by omega) + have hcard : (((s - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + rw [hsub] + have hroom' : ((eo - t : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + simpa [eo, t, htdef, heodef, htlen] using hroom + exact hroom' + obtain ⟨dbH, hHrange, hkH, hHlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := s - 1) hcard + have hHgeE : ∀ x ∈ dbH.keys, r.end_.key ≤ (x : WithTop Key) := + fun x hx => (hHrange x hx).1 + have hHltB : ∀ x ∈ dbH.keys, (x : WithTop Key) < r.begin.key := + fun x hx => (hHrange x hx).2 + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := + crossed_mem_result_ge_begin hread hmore hbegin + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + obtain ⟨C, hCge, hCnotin⟩ := + exists_le_not_mem (res.keys ++ dbH.keys) (res.keys.getLast hne) + have hCnotres : C ∉ res.keys := fun h => hCnotin (List.mem_append_left _ h) + have hCnotH : C ∉ dbH.keys := fun h => hCnotin (List.mem_append_right _ h) + have hCgtRes : ∀ a ∈ res.keys, a < C := by + intro a ha + have hle : a ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast ha + exact lt_of_le_of_ne (le_trans hle hCge) (fun h => hCnotres (h ▸ ha)) + have hlast_ge_B : r.begin.key ≤ (res.keys.getLast hne : WithTop Key) := + hall_ge _ (List.getLast_mem hne) + have hHltC : ∀ a ∈ dbH.keys, a < C := by + intro a ha + have h1 : (a : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le (hHltB a ha) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + exact WithTop.coe_lt_coe.mp h1 + have hHres_lt : ∀ a ∈ dbH.keys, ∀ b ∈ res.keys, a < b := + fun a ha b hb => + WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltB a ha) (hall_ge b hb)) + have hHres_sorted : (dbH.keys ++ res.keys).SortedLT := + List.sortedLT_append.mpr ⟨dbH.sorted, hsort, hHres_lt⟩ + have hsingle : ([C] : List Key).SortedLT := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + have hcrossList : ∀ a ∈ (dbH.keys ++ res.keys), ∀ b ∈ ([C] : List Key), a < b := by + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + rcases List.mem_append.mp ha with haH | haR + · exact hHltC a haH + · exact hCgtRes a haR + have hlst_sorted : (dbH.keys ++ res.keys ++ [C]).SortedLT := + List.sortedLT_append.mpr ⟨hHres_sorted, hsingle, hcrossList⟩ + let db₂ : DB := ⟨dbH.keys ++ res.keys ++ [C], hlst_sorted⟩ + have hdb₂ : db₂.keys = dbH.keys ++ res.keys ++ [C] := rfl + -- the filter at or above `begin.key` + have hfil₂ : db₂.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = res.keys ++ [C] := by + rw [hdb₂, List.filter_append, List.filter_append] + have hHf : dbH.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_le.mpr (hHltB x hx) + have hresf : res.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hall_ge x hx + have hCf : ([C] : List Key).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = [C] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge) + rw [hHf, hresf, hCf] + simp + -- counts for `db₂` + have hcE₂ : cnt db₂ r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hdb₂] at hx + rcases List.mem_append.mp hx with hx' | hxC + · rcases List.mem_append.mp hx' with hxH | hxR + · exact hHgeE x hxH + · exact le_trans (le_of_lt hcross) (hall_ge x hxR) + · rw [List.mem_singleton] at hxC + subst hxC + exact le_trans (le_of_lt hcross) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + have hp₂ : cnt db₂ r.begin.key = s - 1 := by + show (db₂.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key))) = s - 1 + rw [hdb₂, List.countP_append, List.countP_append] + have hHc : dbH.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbH.keys.length := + cnt_eq_length_of_forall_lt dbH hHltB + have hresc : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hall_ge x hx) + have hCc : ([C] : List Key).countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact not_lt.mpr (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hHc, hresc, hCc, hHlen] + omega + have hn₂ : db₂.keys.length = eo := by + rw [hdb₂, List.length_append, List.length_append, hHlen] + have htres : res.keys.length = t := rfl + rw [htres, List.length_singleton] + omega + have hk_db₂ : k ∉ db₂.keys := by + rw [hdb₂] + intro hmem + rcases List.mem_append.mp hmem with hmem' | hmemC + · rcases List.mem_append.mp hmem' with hmemH | hmemR + · exact hkH hmemH + · exact absurd (hall_ge k hmemR) (not_le.mpr hkB) + · rw [List.mem_singleton] at hmemC + have hkC : k = C := hmemC + have h1 : (k : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le hkB (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hkC] at h1 + exact (lt_irrefl (C : WithTop Key)) h1 + -- counts for the inserted database + have hcE₁ : cnt (db₂.insert k hk_db₂) r.end_.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk_db₂ (v := r.end_.key) (not_lt.mpr hkE)] + exact hcE₂ + have hp₁ : cnt (db₂.insert k hk_db₂) r.begin.key = s := by + rw [cnt_insert_lt db₂ k hk_db₂ (v := r.begin.key) hkB, hp₂] + omega + have hn₁ : (db₂.insert k hk_db₂).keys.length = eo + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hn₂] + have hfil₁ : (db₂.insert k hk_db₂).keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = res.keys ++ [C] := by + have hsingle_filter : List.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) (k :: db₂.keys) = + db₂.keys.filter (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) := by + rw [List.filter_cons] + have hkf : decide (r.begin.key ≤ (k : WithTop Key)) = false := by + rw [decide_eq_false_iff_not] + exact not_le.mpr hkB + rw [hkf] + rfl + have hperm : List.Perm ((List.orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))) + (db₂.keys.filter (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))) := by + have h := (List.perm_orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) + rwa [hsingle_filter] at h + have hs₁ : ((List.orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))).SortedLT := + List.sortedLT_iff_pairwise.mpr ((db₂.insert k hk_db₂).sorted.pairwise.filter _) + have hs₂ : (db₂.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))).SortedLT := + List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + rw [DB.insert_keys] + exact (List.SortedLT.eq_of_mem_iff hs₁ hs₂ (fun x => hperm.mem_iff)).trans hfil₂ + -- compatibility of the inserted database + have hck₁ : RangeRead.completeKeys (db₂.insert k hk_db₂) r = res.keys := by + have htres : res.keys.length = t := rfl + have hidx : min (cnt (db₂.insert k hk_db₂) r.end_.key + eo) + (db₂.insert k hk_db₂).keys.length - + cnt (db₂.insert k hk_db₂) r.begin.key = t := by + rw [hcE₁, hn₁, hp₁] + omega + have htake : (res.keys ++ [C]).take t = res.keys := by + rw [List.take_append_of_le_length (le_of_eq htres.symm)] + exact List.take_length + have hidx' : min (List.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + (db₂.insert k hk_db₂).keys + (r.end_.offset - 1).toNat) + (db₂.insert k hk_db₂).keys.length - + List.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) + (db₂.insert k hk_db₂).keys = t := by + simpa [cnt, eo] using hidx + rw [completeKeys_eq_filter_take_of_offset_one hrev hbegin (by omega), hfil₁, hidx', htake] + have hcomp₁ : RangeRead.Compatible (db₂.insert k hk_db₂) r res := + ⟨hcomp₀.1, Or.inr ⟨hmore, hck₁.symm⟩⟩ + -- `db₂` itself is not compatible + have hlenComplete₂ : (RangeRead.completeKeys db₂ r).length = t + 1 := by + rw [completeKeys_length_of_offset_one hrev hbegin (by omega)] + change min (cnt db₂ r.end_.key + eo) db₂.keys.length - cnt db₂ r.begin.key = t + 1 + rw [hcE₂, hn₂, hp₂] + omega + have hnot₂ : ¬ RangeRead.Compatible db₂ r res := by + intro hc + have ht_eq : res.keys.length = t := rfl + rcases hc.2 with ⟨hm, _, _⟩ | ⟨hm, heq⟩ + · rw [hmore] at hm + exact Bool.false_ne_true hm + · rw [← heq] at hlenComplete₂ + rw [ht_eq] at hlenComplete₂ + omega + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₂ (db₂.insert k hk_db₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk_db₂ hkcr).symm + exact hnot₂ ((hs db₂ (db₂.insert k hk_db₂) hagree).mpr hcomp₁) + +end FormalConflictRange + +namespace FormalConflictRange + +/-- **Crossed complete top branch with enough room below the begin key.** If the gap +`[end_.key, begin_.key)` holds at least the overshoot `e - t` keys, the minimal range is +`[end_.key, ⊤)`. Soundness is general; keys in the gap are necessary by +`necessary_of_mem_interval_crossed_top`, and keys at or above `begin.key` by +`necessary_of_begin_le_crossed_top`. -/ +theorem minimal_topRange_of_crossed_large_room {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hroom : (((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) : + Minimal r res ⟨r.end_.key, ⊤⟩ := by + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨sound_topRange_of_lo_le (by omega) (by omega) (le_of_lt hcross) (le_refl _), ?_⟩ + intro k hkE _ + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_of_mem_interval_crossed_top hread hmore hrev hne hbegin hcross hend + ht hroom hkE hkB + · exact necessary_of_begin_le_crossed_top hread hmore hrev hne hbegin hcross hend ht + (not_lt.mp hkB) + +end FormalConflictRange + +namespace FormalConflictRange + +/-- A readable crossed complete result with begin offset one has end offset at least +two: if `end_.offset ≤ 1`, `alwaysEmpty_of_begin_gt_end` makes the complete keys empty. -/ +theorem crossed_end_offset_ge_two {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) : + 2 ≤ r.end_.offset := by + by_contra h + have hend : r.end_.offset ≤ 1 := by omega + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hemp := alwaysEmpty_of_begin_gt_end (r := r) hbegin hend hcross db₀ + rw [← hres] at hemp + exact hne hemp + +end FormalConflictRange + +namespace FormalConflictRange + +/-- The number of reported keys at or above `end_.key`. -/ +def reportedAtEnd (r : RangeRead) (res : Result) : ℕ := + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + +/-- The overshoot `e = (end_.offset - 1).toNat`. -/ +def endOvershoot (r : RangeRead) : ℕ := (r.end_.offset - 1).toNat + +/-- The remaining overshoot `s = e - t` in the crossed complete top branch. -/ +def gapOvershoot (r : RangeRead) (res : Result) : ℕ := + endOvershoot r - reportedAtEnd r res + +/-- A unified forward complete rule at begin offset one, valid on crossed selectors: +the lower endpoint moves down to `end_.key` exactly when the gap has room for the +overshoot `e - t`; the upper endpoint is `keyAfter (last reported)` in the finite branch +`t = e` and `⊤` otherwise. -/ +def completeForwardClosed (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 1 then canonicalRange r + else if reportedAtEnd r res = endOvershoot r then + ⟨if r.end_.key < r.begin.key ∧ + (gapOvershoot r res : ℕ∞) ≤ intervalCard r.end_.key r.begin.key + then r.end_.key else r.begin.key, + (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ + else + ⟨if r.end_.key < r.begin.key ∧ + (gapOvershoot r res : ℕ∞) ≤ intervalCard r.end_.key r.begin.key + then r.end_.key else r.begin.key, + ⊤⟩ + +/-- Crossed complete forward minimality for the unified rule. -/ +theorem minimal_completeForward_of_crossed {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.end_.key < r.begin.key) : + Minimal r res (completeForwardClosed r res) := by + have hend2 : 2 ≤ r.end_.offset := + crossed_end_offset_ge_two hread hmore hne (by omega) hcross + have htle : reportedAtEnd r res ≤ endOvershoot r := by + have h := crossed_complete_t_le_e hread hmore hrev hbegin hcross (by omega) + simpa [reportedAtEnd, endOvershoot] using h + by_cases hte : reportedAtEnd r res = endOvershoot r + · have hmin : Minimal r res + ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := + minimal_truncatedRange_of_finite_crossed hread hmore hne hrev hbegin hcross + hend2 (by simpa [reportedAtEnd, endOvershoot] using hte) + have hs0 : gapOvershoot r res = 0 := by + simp [gapOvershoot, hte] + rw [completeForwardClosed, if_neg (by omega : ¬ r.end_.offset ≤ 1), if_pos hte] + rw [if_pos (by refine ⟨hcross, ?_⟩; rw [hs0]; simp)] + exact hmin + · have htlt : reportedAtEnd r res < endOvershoot r := lt_of_le_of_ne htle hte + have htlt' : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat := by + simpa [reportedAtEnd, endOvershoot] using htlt + by_cases hroom : (gapOvershoot r res : ℕ∞) ≤ intervalCard r.end_.key r.begin.key + · have hroom' : ((((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) : ℕ) : ℕ∞)) ≤ + intervalCard r.end_.key r.begin.key := by + simpa [gapOvershoot, endOvershoot, reportedAtEnd] using hroom + have hmin : Minimal r res ⟨r.end_.key, ⊤⟩ := + minimal_topRange_of_crossed_large_room hread hmore hrev hne hbegin hcross hend2 + htlt' hroom' + rw [completeForwardClosed, if_neg (by omega : ¬ r.end_.offset ≤ 1), if_neg hte] + rw [if_pos (by refine ⟨hcross, ?_⟩; exact hroom)] + exact hmin + · have hroom' : intervalCard r.end_.key r.begin.key < + ((((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) : ℕ) : ℕ∞)) := by + simpa [gapOvershoot, endOvershoot, reportedAtEnd] using lt_of_not_ge hroom + have hmin : Minimal r res ⟨r.begin.key, ⊤⟩ := + minimal_topRange_of_crossed_small_room hread hmore hne hrev hbegin hcross hend2 + htlt' hroom' + rw [completeForwardClosed, if_neg (by omega : ¬ r.end_.offset ≤ 1), if_neg hte] + rw [if_neg (by intro h; exact hroom h.2)] + exact hmin + +/-- The reversed-traversal analogue of `completeForwardClosed`: the upper endpoint is +`keyAfter (head reported)` in the finite branch, and the lower endpoint is chosen by the +same crossed-room test. -/ +def completeReversedClosed (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 1 then canonicalRange r + else if reportedAtEnd r res = endOvershoot r then + ⟨if r.end_.key < r.begin.key ∧ + (gapOvershoot r res : ℕ∞) ≤ intervalCard 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 ∧ + (gapOvershoot r res : ℕ∞) ≤ intervalCard r.end_.key r.begin.key + then r.end_.key else r.begin.key, + ⊤⟩ + +/-- Crossed complete reversed minimality, by flipping the traversal to the forward +crossed theorem. -/ +theorem minimal_completeReversed_of_crossed {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.end_.key < r.begin.key) : + Minimal r res (completeReversedClosed r res) := by + 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' (completeForwardClosed r' res') := + minimal_completeForward_of_crossed hread' rfl hne' rfl hbegin hcross + have hflip := (minimal_flip (r := r) (res := res) + (cr := completeForwardClosed r' res') (b := true) hrev hmore) + have hformula : completeForwardClosed r' res' = completeReversedClosed r res := by + unfold completeForwardClosed completeReversedClosed + dsimp only [r', res'] + simp only [endOvershoot, gapOvershoot, reportedAtEnd, List.countP_reverse, + getLastD_reverse] + rfl + have hflip' : Minimal r res (completeForwardClosed r' res') := hflip.mp hmin' + rw [hformula] at hflip' + exact hflip' + +end FormalConflictRange + +namespace FormalConflictRange + +/-- On cross-free selectors the unified forward rule is the existing compact +`closedFormOffsetOne`. -/ +theorem completeForwardClosed_eq_closedFormOffsetOne {r : RangeRead} {res : Result} + (hcross : r.begin.key ≤ r.end_.key) : + completeForwardClosed r res = closedFormOffsetOne r res := by + unfold completeForwardClosed closedFormOffsetOne + by_cases h : r.end_.offset ≤ 1 + · rw [if_pos h, if_pos h] + · rw [if_neg h, if_neg h] + have hnc : ¬ r.end_.key < r.begin.key := not_lt.mpr hcross + simp only [hnc, false_and, if_false] + rfl + +/-- On cross-free selectors the unified reversed rule is the existing compact +`closedFormOffsetOneRev`. -/ +theorem completeReversedClosed_eq_closedFormOffsetOneRev {r : RangeRead} {res : Result} + (hcross : r.begin.key ≤ r.end_.key) : + completeReversedClosed r res = closedFormOffsetOneRev r res := by + unfold completeReversedClosed closedFormOffsetOneRev + by_cases h : r.end_.offset ≤ 1 + · rw [if_pos h, if_pos h] + · rw [if_neg h, if_neg h] + have hnc : ¬ r.end_.key < r.begin.key := not_lt.mpr hcross + simp only [hnc, false_and, if_false] + rfl + +/-- The complete closed-form rule at begin offset one, dispatching on traversal order. -/ +def completeClosed (r : RangeRead) (res : Result) : ConflictRange := + if r.reverse then completeReversedClosed r res else completeForwardClosed r res + +/-- **Total complete closed form at begin offset one.** The rule is minimal for every +readable complete result, crossed or cross-free, in either traversal order. -/ +theorem minimal_completeClosed {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) : + Minimal r res (completeClosed r res) := by + unfold completeClosed + by_cases hrev : r.reverse = true + · rw [if_pos hrev] + by_cases hcross : r.end_.key < r.begin.key + · exact minimal_completeReversed_of_crossed hread hmore hne hrev hbegin hcross + · have hcf : r.begin.key ≤ r.end_.key := not_lt.mp hcross + rw [completeReversedClosed_eq_closedFormOffsetOneRev hcf] + exact minimal_closedForm_of_offset_one_rev hread hmore hne hrev hbegin hcf + · rw [if_neg hrev] + have hrevf : r.reverse = false := by + cases hr : r.reverse <;> simp_all + by_cases hcross : r.end_.key < r.begin.key + · exact minimal_completeForward_of_crossed hread hmore hne hrevf hbegin hcross + · have hcf : r.begin.key ≤ r.end_.key := not_lt.mp hcross + rw [completeForwardClosed_eq_closedFormOffsetOne hcf] + exact minimal_closedForm_of_offset_one hread hmore hne hrevf hbegin hcf + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedGeneral.lean b/FormalConflictRange/CrossedGeneral.lean new file mode 100644 index 0000000..dc70b87 --- /dev/null +++ b/FormalConflictRange/CrossedGeneral.lean @@ -0,0 +1,1622 @@ +/- +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.CrossedTruncated + +/-! +# Crossed selectors at positive begin offsets + +This module extends the crossed-selector closed forms from `begin.offset = 1` to every +positive begin offset. In the finite complete-result branch the finite condition reads +`t + (begin.offset - 1) = end.offset - 1`, where `t` is the number of reported keys at or +above `end_.key`; it forces the selector gap to be empty. The top branch leaves the +overshoot `e - t - (begin.offset - 1)` to be absorbed by the gap. + +The main ingredients here are the gap-emptiness lemma, the exact finite window +`completeKeys = (filter (begin.key ≤ · ∧ · ≤ last)).drop (begin.offset - 1)`, and the +soundness/necessity results built from them. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- **Finite branch emptiness of the selector gap.** If the number of reported keys at +or above `end_.key` plus `begin.offset - 1` equals `end.offset - 1`, then no stored key +lies in `[end_.key, begin_.key)`. -/ +theorem crossed_gap_empty_of_finite {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (hres : res.keys = RangeRead.completeKeys db r) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + ∀ k ∈ db.keys, ¬ (r.end_.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by + set l := db.keys with hl + have hcounts := crossed_counts_of_t_add_b_eq_e (db := db) (r := r) (res := res) + hrev hbegin hcross hend hne hres ht + have hcEp : l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by + simpa [hl] using hcounts.1 + have hpart := countP_lt_add_between (l := l) (E := r.end_.key) (B := r.begin.key) + (le_of_lt hcross) + have hzero : l.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) = 0 := by omega + intro k hk hkb + exact (List.countP_eq_zero.mp hzero) k hk (by rw [decide_eq_true_eq]; exact hkb) + +/-- If no list element lies in `[E, B)`, then filtering at or above `E` agrees with +filtering at or above `B`. -/ +theorem filter_le_eq_filter_of_no_between {l : List Key} {E B : WithTop Key} + (hEB : E ≤ B) (hno : ∀ k ∈ l, ¬ (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B)) : + l.filter (fun k : Key => decide (E ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (B ≤ (k : WithTop Key))) := by + apply List.filter_congr + intro k hk + by_cases hEk : E ≤ (k : WithTop Key) + · have hBk : B ≤ (k : WithTop Key) := by + by_contra h + exact hno k hk ⟨hEk, not_le.mp h⟩ + simp [hEk, hBk] + · have hBk : ¬ B ≤ (k : WithTop Key) := fun h => hEk (le_trans hEB h) + simp [hEk, hBk] + +/-- **The finite crossed complete window.** In the finite branch at any positive begin +offset, the complete keys are the stored keys in `[begin_.key, last]` after dropping the +first `begin.offset - 1` of them. -/ +theorem completeKeys_eq_filter_drop_of_crossed_finite {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (hres : res.keys = RangeRead.completeKeys db r) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne))).drop + (r.begin.offset - 1).toNat := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set t := res.keys.length with htlen + have hcounts := crossed_counts_of_t_add_b_eq_e (db := db) (r := r) (res := res) + hrev hbegin hcross hend hne hres ht + have hcEp : l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = p := by + simpa [hl, hp] using hcounts.1 + have hAele : p + e ≤ l.length := by simpa [hl, hp, he] using hcounts.2 + have hcountlen : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [hres] + exact completeKeys_count_ge_eq_length_of_crossed_general (db := db) hrev hbegin hcross hend + have htb : t + b = e := by + rw [show t = res.keys.length from rfl] + omega + have htpos : 0 < t := List.length_pos_of_ne_nil hne + have hlist := completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend + rw [← hl, ← hp, ← hb, ← he] at hlist + have hmin : min (l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + e) + l.length = p + e := by + rw [hcEp] + exact Nat.min_eq_left hAele + have hlist' : RangeRead.completeKeys db r = (l.drop (p + b)).take t := by + rw [hlist, hmin] + congr 1 + omega + set W := (l.drop (p + b)).take t with hWdef + have hW : res.keys = W := by rw [hres, hlist'] + have hWne : W ≠ [] := by rw [← hW]; exact hne + have hWlen : W.length = t := by + rw [hWdef, List.length_take] + have : t ≤ (l.drop (p + b)).length := by rw [List.length_drop]; omega + omega + have hlast_eq : res.keys.getLast hne = l[p + e - 1] := by + rw [List.getLast_congr hne hWne hW, List.getLast_eq_getElem] + rw [List.getElem_take, List.getElem_drop] + congr 1 + omega + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hAeq : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = l.drop p := by + rw [hp] + have hdrop := filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hAs : (l.drop p).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hepos : 0 < e := by omega + have hidx : e - 1 < (l.drop p).length := by rw [List.length_drop]; omega + have hlastA : (l.drop p)[e - 1]'hidx = res.keys.getLast hne := by + rw [List.getElem_drop] + calc l[p + (e - 1)] = l[p + e - 1] := by congr 1; omega + _ = res.keys.getLast hne := hlast_eq.symm + have hAle : (l.drop p).filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + (l.drop p).take e := by + have h := filter_le_eq_take_succ_of_getElem hAs (x := res.keys.getLast hne) (n := e - 1) + (by rw [List.length_drop]; omega) hlastA + simpa [Nat.sub_add_cancel hepos] using h + have hfil : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne)) = + (l.drop p).take e := by + have hsplit : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne)) = + (l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + exact (Bool.decide_and (r.begin.key ≤ (k : WithTop Key)) + (k ≤ res.keys.getLast hne)).trans (Bool.and_comm _ _) + rw [hsplit, hAeq, hAle] + rw [hlist', hfil] + rw [List.drop_take, List.drop_drop] + rw [hWdef] + congr 1 + omega + +/-- A conjunction filter is a nested filter. -/ +theorem filter_and_eq_filter_filter {l : List Key} (p q : Key → Prop) + [DecidablePred p] [DecidablePred q] : + l.filter (fun k : Key => decide (p k ∧ q k)) = + (l.filter (fun k : Key => decide (p k))).filter (fun k : Key => decide (q k)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + exact (Bool.decide_and (p k) (q k)).trans (Bool.and_comm _ _) + +/-- **The finite crossed window from gap emptiness alone.** If the selector gap is +empty and exactly `end.offset - 1` stored keys lie in `[end_.key, last]`, then the +complete keys are the stored keys in `[begin_.key, last]` with the first +`begin.offset - 1` removed. -/ +theorem completeKeys_eq_filter_drop_of_no_gap {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hno : ∀ k ∈ db.keys, ¬ (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) + (hlen : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + k ≤ last))).length = (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ last))).drop (r.begin.offset - 1).toNat := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hEeq : l.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) := + filter_le_eq_filter_of_no_between (le_of_lt hcross) (by simpa [hl] using hno) + have hEnotlt : l.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (¬ (k : WithTop Key) < r.end_.key)) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hBnotlt : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (¬ (k : WithTop Key) < r.begin.key)) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hdropE := filter_not_lt_eq_drop_countP hls r.end_.key + have hdropB := filter_not_lt_eq_drop_countP hls r.begin.key + have hcEp : cE = p := by + have hlenEq := congrArg List.length hEeq + rw [hEnotlt, hdropE, List.length_drop, hBnotlt, hdropB, List.length_drop] at hlenEq + have hcE_le : cE ≤ l.length := by rw [hcE]; exact List.countP_le_length + have hp_le : p ≤ l.length := by rw [hp]; exact List.countP_le_length + omega + have hAeq : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = l.drop p := by + rw [hBnotlt, hdropB, ← hp] + have hAs : (l.drop p).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hlenEf : ((l.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = e := by + rw [← filter_and_eq_filter_filter (p := fun k : Key => r.end_.key ≤ (k : WithTop Key)) + (q := fun k : Key => k ≤ last)] + simpa [hl, he] using hlen + have hlenBf : ((l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = e := by + rw [← hEeq]; exact hlenEf + have hlenBf' : ((l.drop p).filter (fun k : Key => decide (k ≤ last))).length = e := by + rw [← hAeq]; exact hlenBf + have hAle : (l.drop p).filter (fun k : Key => decide (k ≤ last)) = (l.drop p).take e := by + rw [filter_le_eq_take_countP hAs last] + congr 1 + rw [List.countP_eq_length_filter] + exact hlenBf' + have hp_le : p ≤ l.length := by rw [hp]; exact List.countP_le_length + have hpe_le : p + e ≤ l.length := by + have he_le : e ≤ (l.drop p).length := by + have hlen_take : (List.take e (l.drop p)).length = e := by + rw [← hAle]; exact hlenBf' + rw [List.length_take] at hlen_take + omega + rw [List.length_drop] at he_le + omega + have hmain := completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend + rw [← hl, ← hp, ← hcE, ← hb, ← he] at hmain + have hmin : min (cE + e) l.length = p + e := by + rw [hcEp] + exact Nat.min_eq_left hpe_le + have hck : RangeRead.completeKeys db r = (l.drop (p + b)).take (e - b) := by + rw [hmain, hmin] + congr 1 + omega + have hfil : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ last)) = (l.drop p).take e := by + rw [filter_and_eq_filter_filter (p := fun k : Key => r.begin.key ≤ (k : WithTop Key)) + (q := fun k : Key => k ≤ last)] + rw [hAeq] + exact hAle + rw [hck, hfil, List.drop_take, List.drop_drop] + +/-- Conjunction filters also agree across an empty selector gap. -/ +theorem filter_le_le_eq_filter_of_no_between {l : List Key} {E B : WithTop Key} {last : Key} + (hEB : E ≤ B) (hno : ∀ k ∈ l, ¬ (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B)) : + l.filter (fun k : Key => decide (E ≤ (k : WithTop Key) ∧ k ≤ last)) = + l.filter (fun k : Key => decide (B ≤ (k : WithTop Key) ∧ k ≤ last)) := by + apply List.filter_congr + intro k hk + by_cases hEk : E ≤ (k : WithTop Key) + · have hBk : B ≤ (k : WithTop Key) := by + by_contra h + exact hno k hk ⟨hEk, not_le.mp h⟩ + simp [hEk, hBk] + · have hBk : ¬ B ≤ (k : WithTop Key) := fun h => hEk (le_trans hEB h) + simp [hEk, hBk] + +/-- A crossed complete result's reported keys lie at or above `begin_.key`, for any +positive begin offset. -/ +theorem crossed_mem_result_ge_begin_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hbegin : 1 ≤ r.begin.offset) : + ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + intro k hk + have hk' : k ∈ RangeRead.completeKeys db₀ r := by rw [← hres]; exact hk + exact le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) hbegin) + (RangeRead.mem_completeKeys.mp hk').2.1 + +/-- The keys of `[lo, keyAfter last)` are the stored keys in `[lo, last]`. -/ +theorem conflictRange_keys_keyAfter_eq_filter {db : DB} {lo : WithTop Key} {last : Key} : + (⟨lo, (keyAfter last : Key)⟩ : ConflictRange).keys db = + (db.keys.filter (fun k : Key => decide (lo ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + unfold ConflictRange.keys + rw [show db.keys.filter (fun (k : Key) => decide (lo ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + db.keys.filter (fun (k : Key) => + decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key)) && + decide (lo ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter] + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + +/-- **Finite-branch soundness for crossed complete results at any positive begin +offset.** Agreement on `[end_.key, keyAfter (last reported))` fixes the complete keys: +the finite condition makes the selector gap empty, and the finite window is the stored +keys in `[begin_.key, last]` with the first `begin.offset - 1` removed. -/ +theorem sound_truncatedRange_of_crossed_finite_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.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + Sound r res ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + let cr : ConflictRange := ⟨r.end_.key, (keyAfter (res.keys.getLast hne) : WithTop Key)⟩ + change Sound r res cr + have hall_ge : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := + crossed_mem_result_ge_begin_general hread hmore hbegin + have hlast_ge_B : r.begin.key ≤ (res.keys.getLast hne : WithTop Key) := + hall_ge _ (List.getLast_mem hne) + have htb : res.keys.length + (r.begin.offset - 1).toNat = + (r.end_.offset - 1).toNat := by + have hcount : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact le_trans (le_of_lt hcross) (hall_ge k hk) + omega + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have step : ∀ {dA dB : DB}, RangeRead.Compatible dA r res → AgreeOn cr dA dB → + RangeRead.Compatible dB r res := by + intro dA dB hcA hag + have hresA : res.keys = RangeRead.completeKeys dA r := by + rcases hcA.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hgapA := crossed_gap_empty_of_finite (db := dA) hrev hbegin hcross + (by omega) hne hresA ht + have hwinA := completeKeys_eq_filter_drop_of_crossed_finite (db := dA) hrev hbegin + hcross (by omega) hne hresA ht + have hfilEeqA := filter_le_le_eq_filter_of_no_between (l := dA.keys) + (last := res.keys.getLast hne) (le_of_lt hcross) hgapA + have hlenBA : (dA.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne))).length = (r.end_.offset - 1).toNat := by + have hlenck : (RangeRead.completeKeys dA r).length = res.keys.length := by + rw [← hresA] + rw [hwinA, List.length_drop] at hlenck + omega + have hlenA : (dA.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne))).length = (r.end_.offset - 1).toNat := by + rw [hfilEeqA]; exact hlenBA + have hcrA : cr.keys dA = (dA.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + simpa [cr] using conflictRange_keys_keyAfter_eq_filter (db := dA) + (lo := r.end_.key) (last := res.keys.getLast hne) + have hcrA_E : cr.keys dA = dA.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne)) := by + rw [hcrA, ← filter_and_eq_filter_filter + (p := fun k : Key => r.end_.key ≤ (k : WithTop Key)) + (q := fun k : Key => k ≤ res.keys.getLast hne)] + have hcrlenA : (cr.keys dA).length = (r.end_.offset - 1).toNat := by + rw [hcrA_E, hlenA] + have hgapB : ∀ k ∈ dB.keys, ¬ (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + intro k hkB hkb + have hkAfter : (k : WithTop Key) < + ((keyAfter (res.keys.getLast hne) : Key) : WithTop Key) := + lt_of_lt_of_le (lt_of_lt_of_le hkb.2 hlast_ge_B) + (le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt _))) + have hkcrB : k ∈ cr.keys dB := by + rw [ConflictRange.mem_keys_iff] + refine ⟨hkB, ?_⟩ + change r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter (res.keys.getLast hne) : Key) : WithTop Key) + exact ⟨hkb.1, hkAfter⟩ + have hkcrA : k ∈ cr.keys dA := by + rw [hag]; exact hkcrB + have hkA : k ∈ dA.keys := (ConflictRange.mem_keys_iff.mp hkcrA).1 + exact hgapA k hkA ⟨hkb.1, hkb.2⟩ + have hcrB : cr.keys dB = (dB.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + simpa [cr] using conflictRange_keys_keyAfter_eq_filter (db := dB) + (lo := r.end_.key) (last := res.keys.getLast hne) + have hcrlenB : (cr.keys dB).length = (r.end_.offset - 1).toNat := by + rw [← hag]; exact hcrlenA + have hcrB_E : cr.keys dB = dB.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne)) := by + rw [hcrB, ← filter_and_eq_filter_filter + (p := fun k : Key => r.end_.key ≤ (k : WithTop Key)) + (q := fun k : Key => k ≤ res.keys.getLast hne)] + have hlenB : (dB.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne))).length = (r.end_.offset - 1).toNat := by + rw [← hcrB_E]; exact hcrlenB + have hwinB := completeKeys_eq_filter_drop_of_no_gap (db := dB) hrev hbegin + hcross (by omega) hgapB hlenB + have hfilEeqB := filter_le_le_eq_filter_of_no_between (l := dB.keys) + (last := res.keys.getLast hne) (le_of_lt hcross) hgapB + have hcrA_B : cr.keys dA = (dA.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [hcrA, filter_le_eq_filter_of_no_between (l := dA.keys) (le_of_lt hcross) hgapA] + have hcrB_B : cr.keys dB = (dB.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [hcrB, filter_le_eq_filter_of_no_between (l := dB.keys) (le_of_lt hcross) hgapB] + have hfB : (dA.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + (dB.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [← hcrA_B, ← hcrB_B]; exact hag + have hconj : dA.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne)) = + dB.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne)) := by + rw [filter_and_eq_filter_filter + (p := fun k : Key => r.begin.key ≤ (k : WithTop Key)) + (q := fun k : Key => k ≤ res.keys.getLast hne), + filter_and_eq_filter_filter + (p := fun k : Key => r.begin.key ≤ (k : WithTop Key)) + (q := fun k : Key => k ≤ res.keys.getLast hne)] + exact hfB + refine ⟨hcA.1, Or.inr ⟨hmore, ?_⟩⟩ + calc res.keys = RangeRead.completeKeys dA r := hresA + _ = (dA.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne))).drop (r.begin.offset - 1).toNat := hwinA + _ = (dB.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne))).drop (r.begin.offset - 1).toNat := by rw [hconj] + _ = RangeRead.completeKeys dB r := hwinB.symm + intro db₁ db₂ hagree + exact ⟨fun hc₁ => step hc₁ hagree, fun hc₂ => step hc₂ hagree.symm⟩ + +/-- **The end selector's key is necessary in the finite crossed branch, at any +positive begin offset.** In that branch the selector gap is empty, so `end_.key` +itself is unstored. Inserting it into a database that a sound range omits it from +leaves the range's keys unchanged but shifts the finite window down by one reported +key, so the result is no longer complete. -/ +theorem necessary_end_key_of_crossed_finite_general {r : RangeRead} {res : Result} {v : Key} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) + (hE : r.end_.key = (↑v : WithTop Key)) : + Necessary r res v := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hgap : ∀ k ∈ db₀.keys, ¬ (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := + crossed_gap_empty_of_finite (db := db₀) hrev hbegin hcross (by omega) hne hres ht + have hv0 : v ∉ db₀.keys := by + intro hmem + exact hgap v hmem ⟨le_of_eq hE, hE ▸ hcross⟩ + intro cr hs + by_contra hvcr + -- Inserting the (unstored) end key changes the finite window. + let db₁ : DB := db₀.insert v hv0 + have hagree : AgreeOn cr db₀ db₁ := + (ConflictRange.keys_insert_of_not_mem cr db₀ hv0 hvcr).symm + have hcomp₁ : RangeRead.Compatible db₁ r res := (hs db₀ db₁ hagree).mp hcomp₀ + -- The complete keys of `db₁` drop the last reported key. + set l := db₀.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set t := res.keys.length with htlen + have hcounts := crossed_counts_of_t_add_b_eq_e (db := db₀) hrev hbegin hcross + (by omega) hne hres ht + have hcEp : l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = p := by + simpa [hl, hp] using hcounts.1 + have hAele : p + e ≤ l.length := by simpa [hl, hp, he] using hcounts.2 + have hcountlen : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [hres] + exact completeKeys_count_ge_eq_length_of_crossed_general (db := db₀) hrev hbegin + hcross (by omega) + have htb : t + b = e := by + rw [show t = res.keys.length from rfl] + omega + have htpos : 0 < t := List.length_pos_of_ne_nil hne + have hwin₀ : RangeRead.completeKeys db₀ r = (l.drop (p + b)).take t := by + have h := completeKeys_eq_drop_take_general (db := db₀) (r := r) hrev hbegin (by omega) + rw [← hl, ← hp, ← hb, ← he] at h + rw [h, show min (l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + e) + l.length = p + e from by rw [hcEp]; exact Nat.min_eq_left hAele] + congr 1 + omega + have hresW : res.keys = (l.drop (p + b)).take t := by rw [hres, hwin₀] + -- The rank of `v` in `l` is exactly `p`. + have hcount_v : l.countP (fun k : Key => decide (k < v)) = p := by + rw [← countP_lt_coe (l := l) v, ← hE] + exact hcEp + have hkeys₁ : db₁.keys = l.insertIdx p v := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx (by rw [hl]; exact db₀.sorted) hv0, + hcount_v] + have hcE₁ : db₁.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = p := by + rw [hkeys₁, + countP_insertIdx (p := fun k : Key => decide ((k : WithTop Key) < r.end_.key)) p v + (by rw [decide_eq_false_iff_not, hE]; exact not_lt.mpr le_rfl)] + exact hcEp + have hcB₁ : db₁.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + p + 1 := by + rw [hkeys₁, ← hcount_v, + countP_insertIdx_of_lt (l := l) (hs := by rw [hl]; exact db₀.sorted) (x := v) + (v := r.begin.key) (by rw [← hE]; exact hcross)] + have : l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = p := hp + omega + have hp_le : p ≤ l.length := by rw [← hcount_v]; exact List.countP_le_length + have hlen₁ : db₁.keys.length = l.length + 1 := by + rw [hkeys₁, insertIdx_eq_take_drop l p v hp_le, + List.length_append, List.length_cons, List.length_drop, List.length_take, + Nat.min_eq_left hp_le] + omega + have hdrop : (l.insertIdx p v).drop (p + 1 + b) = l.drop (p + b) := by + rw [insertIdx_eq_take_drop l p v hp_le, List.drop_append, + List.drop_eq_nil_of_le (by rw [List.length_take]; omega), List.length_take, + Nat.min_eq_left hp_le] + have hsub : p + 1 + b - p = b + 1 := by omega + rw [hsub, List.drop_succ_cons, List.drop_drop] + rfl + have hck₁ : RangeRead.completeKeys db₁ r = res.keys.take (t - 1) := by + have h := completeKeys_eq_drop_take_general (db := db₁) (r := r) hrev hbegin (by omega) + rw [h, hcB₁, hcE₁, hlen₁, hkeys₁, hdrop, Nat.min_eq_left (by omega)] + have hidx : p + e - (p + 1 + b) = t - 1 := by omega + rw [hidx, hresW, List.take_take, Nat.min_eq_left (by omega)] + have htake_ne : res.keys.take (t - 1) ≠ res.keys := by + intro heq + have hlen := congrArg List.length heq + rw [List.length_take, htlen] at hlen + omega + have hnotcomp : ¬ RangeRead.Compatible db₁ r res := by + intro hc + have heq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hc.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + exact htake_ne (by rw [← hck₁, ← heq]) + exact hnotcomp hcomp₁ + +/-- **Finite crossed complete-branch minimality at any positive begin offset.** +When `t + (begin.offset - 1) = end.offset - 1` the minimal range is +`[end_.key, keyAfter (last reported))`. Soundness is +`sound_truncatedRange_of_crossed_finite_general`; every key in the range lies between +the necessary end key and the necessary last reported key. -/ +theorem minimal_truncatedRange_of_crossed_finite_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.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + Minimal r res ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨sound_truncatedRange_of_crossed_finite_general hread hmore hne hrev hbegin + hcross hend ht, ?_⟩ + intro k hkE hklt + have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hcross + exact absurd hcross (not_lt.mpr le_top) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hEne + have hEnec : Necessary r res v := + necessary_end_key_of_crossed_finite_general hread hmore hne hrev hbegin hcross hend ht + hv.symm + have hlastnec : Necessary r res (res.keys.getLastD ([] : Key)) := by + rw [hlast] + exact necessary_of_mem_result hread (List.getLast_mem hne) + have hklast : k ≤ res.keys.getLastD ([] : Key) := + (lt_keyAfter_iff k (res.keys.getLastD ([] : Key))).mp (WithTop.coe_lt_coe.mp hklt) + exact hEnec.of_between hlastnec (WithTop.coe_le_coe.mp (hv.symm ▸ hkE)) hklast + +/-- A key strictly inside the gap is needed to compute the complete keys; the room +bound makes the whole window a prefix of the stored keys at or above `begin.key`. +This is the general-offset form of `completeKeys_eq_iff_filter_ge_begin_of_room`. +The extra `begin.offset - 1` reflects that the window starts that many keys into +the `[begin.key, ⊤)` group. -/ +theorem completeKeys_eq_iff_filter_drop_of_room_general {db : DB} {r : RangeRead} + {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hend : 1 ≤ r.end_.offset) (hEB : r.end_.key ≤ r.begin.key) + (hne : res.keys ≠ []) + (hroom : res.keys.length + (r.begin.offset - 1).toNat + + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + < (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = res.keys ↔ + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).drop + (r.begin.offset - 1).toNat = res.keys := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hroom' : res.keys.length + b + (p - cE) < e := by + simpa [← hl, ← hp, ← hcE, ← hb, ← he] using hroom + have hcEp : cE ≤ p := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hEB + have hp_le : p ≤ l.length := by rw [hp]; exact List.countP_le_length + have hlen : (RangeRead.completeKeys db r).length = + min (cE + e) l.length - (p + b) := by + have h := completeKeys_length_general (db := db) (r := r) hrev hbegin hend + simpa [← hl, ← hp, ← hcE, ← hb, ← he] using h + have hA : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + l.drop p := by + have hnot : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + rw [hnot, filter_not_lt_eq_drop_countP hls r.begin.key, ← hp] + have hdrop_drop : l.drop (p + b) = (l.drop p).drop b := by + rw [List.drop_drop] + constructor + · intro hck + have hLres : res.keys.length = min (cE + e) l.length - (p + b) := by + rw [hck] at hlen + exact hlen + have hpos : 0 < min (cE + e) l.length - (p + b) := by + rw [← hLres] + exact List.length_pos_of_ne_nil hne + have hpb_le : p + b ≤ min (cE + e) l.length := by omega + have hmin : min (cE + e) l.length = l.length := by + rcases le_total (cE + e) l.length with hle | hle + · exfalso + have hMeq : min (cE + e) l.length = cE + e := Nat.min_eq_left hle + have hlen' : res.keys.length = cE + e - (p + b) := by + rw [hMeq] at hLres; exact hLres + have hsum : res.keys.length + (p + b) = cE + e := by omega + have harith : res.keys.length + b + (p - cE) = e := by omega + omega + · exact Nat.min_eq_right hle + have hfull : RangeRead.completeKeys db r = l.drop (p + b) := by + rw [completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend] + rw [← hl, ← hp, ← hcE, ← hb, ← he, hmin, ← List.length_drop] + exact List.take_length + rw [hA, ← hdrop_drop] + rw [← hfull] + exact hck + · intro hfil + have hA_drop : (l.drop p).drop b = res.keys := by + rw [← hA] + exact hfil + have hq_eq : (l.drop p).length = l.length - p := List.length_drop + have hb_le_q : b ≤ (l.drop p).length := by + by_contra hlt + have hemp : (l.drop p).drop b = [] := List.drop_eq_nil_of_le (by omega) + rw [hemp] at hA_drop + exact hne hA_drop.symm + have hreslen : res.keys.length = (l.drop p).length - b := by + rw [← hA_drop, List.length_drop] + have hq_eq' : (l.drop p).length = l.length - p := List.length_drop + have hlt_len : l.length < cE + e := by + have hsum : res.keys.length + b + (p - cE) < e := hroom' + have hlenq : res.keys.length + b = l.length - p := by omega + omega + have hmin : min (cE + e) l.length = l.length := Nat.min_eq_right (le_of_lt hlt_len) + have hfull : RangeRead.completeKeys db r = l.drop (p + b) := by + rw [completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend] + rw [← hl, ← hp, ← hcE, ← hb, ← he, hmin, ← List.length_drop] + exact List.take_length + rw [hfull, hdrop_drop, hA_drop] + +/-- **Small-room crossed compatibility at any positive begin offset.** If the gap +`[end_.key, begin_.key)` has fewer than the remaining overshoot +`e - t - (begin.offset - 1)` keys, then the end key may be moved up to the begin key +without changing compatibility. This is the general-offset form of +`compatible_crossed_iff_endKeyToBegin_of_room`. -/ +theorem compatible_crossed_iff_endKeyToBegin_of_room_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.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (htb : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat) + (hroom : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞)) : + ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (endKeyToBegin r) res := by + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have htb' : res.keys.length + b < e := by simpa [hb, he] using htb + intro db + have hgap_le : ((db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key := by + simpa [cnt] using cnt_sub_le_intervalCard db (le_of_lt hcross) + have hgap_lt : (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) < + e - res.keys.length - b := by + have h1 : ((db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) :Nat) : ℕ∞) < + (((e - res.keys.length - b : ℕ)) : ℕ∞) := by + simpa [hb, he] using lt_of_le_of_lt hgap_le hroom + exact_mod_cast h1 + have hroom_db : res.keys.length + b + + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) < e := by + omega + have hA : db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) := rfl + have hbase_r : RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).drop b = + res.keys := by + constructor + · rintro ⟨hlim, hdisj | ⟨_, heq⟩⟩ + · simp [hmore] at hdisj + · exact ⟨hlim, (completeKeys_eq_iff_filter_drop_of_room_general + (db := db) (r := r) (res := res) hrev hbegin (by omega) (le_of_lt hcross) + hne hroom_db).mp heq.symm⟩ + · rintro ⟨hlim, hfil⟩ + refine ⟨hlim, Or.inr ⟨hmore, ?_⟩⟩ + exact ((completeKeys_eq_iff_filter_drop_of_room_general + (db := db) (r := r) (res := res) hrev hbegin (by omega) (le_of_lt hcross) + hne hroom_db).mpr hfil).symm + have hbase_r' : RangeRead.Compatible db (endKeyToBegin r) res ↔ + RangeRead.withinLimit r res.keys ∧ + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).drop b = + res.keys := by + have hroom_db' : res.keys.length + b + + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < + (endKeyToBegin r).begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < + (endKeyToBegin r).end_.key))) < e := by + have h1 : (endKeyToBegin r).begin.key = r.begin.key := rfl + have h2 : (endKeyToBegin r).end_.key = r.begin.key := rfl + rw [h1, h2] + have : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := + Nat.sub_self _ + rw [this, Nat.add_zero] + exact htb' + constructor + · rintro ⟨hlim, hdisj | ⟨_, heq⟩⟩ + · simp [hmore] at hdisj + · refine ⟨?_, (completeKeys_eq_iff_filter_drop_of_room_general + (db := db) (r := endKeyToBegin r) (res := res) (by simpa [endKeyToBegin] using hrev) + (by simpa [endKeyToBegin] using hbegin) (by simpa [endKeyToBegin] using (by omega : 1 ≤ r.end_.offset)) + (by simp [endKeyToBegin]) hne hroom_db').mp heq.symm⟩ + simpa [endKeyToBegin, RangeRead.withinLimit] using hlim + · rintro ⟨hlim, hfil⟩ + refine ⟨?_, Or.inr ⟨hmore, ?_⟩⟩ + · simpa [endKeyToBegin, RangeRead.withinLimit] using hlim + · exact ((completeKeys_eq_iff_filter_drop_of_room_general + (db := db) (r := endKeyToBegin r) (res := res) (by simpa [endKeyToBegin] using hrev) + (by simpa [endKeyToBegin] using hbegin) (by simpa [endKeyToBegin] using (by omega : 1 ≤ r.end_.offset)) + (by simp [endKeyToBegin]) hne hroom_db').mpr hfil).symm + rw [hbase_r, hbase_r'] + +/-- **Crossed complete top branch with little room below the begin key, at any +positive begin offset.** If the gap `[end_.key, begin_.key)` holds fewer than the +remaining overshoot `e - t - (begin.offset - 1)` keys, the minimal range is +`[begin.key, ⊤)`: moving the end key up to the begin key gives a key-equal read +with the same compatible databases, and the key-equal read's top branch is +`minimal_closedForm_of_key_eq_complete_general`. -/ +theorem minimal_topRange_of_crossed_small_room_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.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (htb : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat) + (hroom : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞)) : + Minimal r res ⟨r.begin.key, ⊤⟩ := by + let r' := endKeyToBegin r + have hiff := compatible_crossed_iff_endKeyToBegin_of_room_general hread hmore hne hrev + hbegin hcross hend htb hroom + have hall_ge : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := + crossed_mem_result_ge_begin_general hread hmore hbegin + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r' res := ⟨db₀, (hiff db₀).mp hcomp₀⟩ + have hcount : res.keys.countP + (fun k : Key => decide (r'.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + simpa [r', endKeyToBegin] using hall_ge k hk + have htne : res.keys.countP + (fun k : Key => decide (r'.end_.key ≤ (k : WithTop Key))) + + (r'.begin.offset - 1).toNat ≠ (r'.end_.offset - 1).toNat := by + rw [hcount] + simpa [r', endKeyToBegin] using (ne_of_lt htb) + have hmin' : Minimal r' res (closedFormKeyEqComplete r' res) := + minimal_closedForm_of_key_eq_complete_general hread' hmore + (by simpa [r', endKeyToBegin] using hrev) hne + (by simpa [r', endKeyToBegin] using hbegin) (by simp [r', endKeyToBegin]) + (by simpa [r', endKeyToBegin] using hend) + have hform : closedFormKeyEqComplete r' res = ⟨r.begin.key, ⊤⟩ := by + unfold closedFormKeyEqComplete + rw [if_neg htne] + simp [topRange, r', endKeyToBegin] + rw [hform] at hmin' + exact minimal_of_compatible_iff hiff hmin' + + +/-- For a complete crossed result at positive begin offset, the `begin.offset - 1` +stored keys just above `begin.key` and before the first reported key exist, are +sorted, lie at or above `begin.key`, and lie strictly below every reported key. -/ +theorem crossed_pad_general {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hres : res.keys = RangeRead.completeKeys db r) : + ∃ P : List Key, P.length = (r.begin.offset - 1).toNat ∧ P.SortedLT ∧ + (∀ x ∈ P, r.begin.key ≤ (x : WithTop Key)) ∧ + (∀ x ∈ P, ∀ y ∈ res.keys, x < y) ∧ + (P ++ res.keys).SortedLT := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hA : A = l.drop p := by + rw [hAdef] + have hnot : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + rw [hnot, filter_not_lt_eq_drop_countP hls r.begin.key, ← hp] + have hAs : A.SortedLT := by + rw [hA] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hAge : ∀ x ∈ A, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + rw [hAdef] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + -- The complete window is a truncated tail of `A.drop b`. + set K := min (l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) l.length - (p + b) with hK + have hck : RangeRead.completeKeys db r = (A.drop b).take K := by + rw [completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend] + rw [← hl, ← hp, ← hb, hA, List.drop_drop, hK] + have hresK : res.keys = (A.drop b).take K := by rw [hres, hck] + have htake_ne : (A.drop b).take K ≠ [] := by rw [← hresK]; exact hne + have hdrop_ne : A.drop b ≠ [] := fun hnil => htake_ne (by rw [hnil]; simp) + have hb_lt_A : b < A.length := by + have := List.length_pos_of_ne_nil hdrop_ne + rw [List.length_drop] at this + omega + have hKpos : 0 < K := by + have := List.length_pos_of_ne_nil htake_ne + rw [List.length_take] at this + omega + let P := A.take b + have hPdef : P = A.take b := rfl + have hPlen : P.length = b := by + rw [hPdef, List.length_take, Nat.min_eq_left (le_of_lt hb_lt_A)] + have hPs : P.SortedLT := by + rw [hPdef] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.take (i := b) + (List.sortedLT_iff_pairwise.mp hAs)) + have hPge : ∀ x ∈ P, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + exact hAge x (List.take_subset _ _ (by rw [← hPdef]; exact hx)) + have happend : A = P ++ A.drop b := by rw [hPdef, List.take_append_drop] + have hcross : ∀ x ∈ P, ∀ y ∈ A.drop b, x < y := by + have := List.sortedLT_append.mp (by rw [← happend]; exact hAs) + exact this.2.2 + have hP_lt_res : ∀ x ∈ P, ∀ y ∈ res.keys, x < y := by + intro x hx y hy + have hy' : y ∈ A.drop b := by + rw [hresK] at hy + exact List.mem_of_mem_take hy + exact hcross x hx y hy' + have hdropS : (A.drop b).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := b) + (List.sortedLT_iff_pairwise.mp hAs)) + have hresS : res.keys.SortedLT := by + rw [hresK] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.take (i := K) + (List.sortedLT_iff_pairwise.mp hdropS)) + refine ⟨P, ?_, hPs, hPge, hP_lt_res, ?_⟩ + · rw [hPlen, hb] + · exact List.sortedLT_append.mpr ⟨hPs, hresS, hP_lt_res⟩ + + +/-- Generic complete-window computation for a database of the form `P ++ res` with +`P` padding of length `b`, all at or above the begin key and below the reported keys. -/ +theorem completeKeys_pad_eq_general {r : RangeRead} {res : Result} {P : List Key} + (hPs : P.SortedLT) (hresS : res.keys.SortedLT) + (hPlen : P.length = (r.begin.offset - 1).toNat) + (hPge : ∀ x ∈ P, r.begin.key ≤ (x : WithTop Key)) + (hresge : ∀ y ∈ res.keys, r.begin.key ≤ (y : WithTop Key)) + (hPlt : ∀ x ∈ P, ∀ y ∈ res.keys, x < y) + (hsort : (P ++ res.keys).SortedLT) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (ht : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) : + RangeRead.completeKeys (⟨P ++ res.keys, hsort⟩ : DB) r = res.keys := by + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + let db : DB := ⟨P ++ res.keys, hsort⟩ + have hkeys : db.keys = P ++ res.keys := rfl + have hp0 : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + change cnt db r.begin.key = 0 + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeys] at hx + rcases List.mem_append.mp hx with hxP | hxR + · exact hPge x hxP + · exact hresge x hxR + have hcE0 : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by + change cnt db r.end_.key = 0 + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeys] at hx + rcases List.mem_append.mp hx with hxP | hxR + · exact le_trans (le_of_lt hcross) (hPge x hxP) + · exact le_trans (le_of_lt hcross) (hresge x hxR) + have hlen : db.keys.length = b + res.keys.length := by + rw [hkeys, List.length_append, hPlen] + have hmin : min (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + e) + db.keys.length = b + res.keys.length := by + rw [hcE0, Nat.zero_add, hlen] + exact Nat.min_eq_right (le_of_lt (by omega)) + rw [completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend] + rw [hp0, Nat.zero_add] + rw [hcE0, Nat.zero_add, hlen, Nat.min_eq_right (le_of_lt (by omega))] + have hsub : b + res.keys.length - b = res.keys.length := by omega + rw [hsub, hkeys, ← hb, ← hPlen, List.drop_append_of_le_length le_rfl] + simp + + +theorem completeKeys_length_pad_general {r : RangeRead} {res : Result} {Q : List Key} + (hQs : Q.SortedLT) (hsort : (Q ++ res.keys).SortedLT) + (hQge : ∀ x ∈ Q, r.begin.key ≤ (x : WithTop Key)) + (hresge : ∀ y ∈ res.keys, r.begin.key ≤ (y : WithTop Key)) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys (⟨Q ++ res.keys, hsort⟩ : DB) r).length = + min ((r.end_.offset - 1).toNat) (Q.length + res.keys.length) - + (r.begin.offset - 1).toNat := by + let db : DB := ⟨Q ++ res.keys, hsort⟩ + have hkeys : db.keys = Q ++ res.keys := rfl + have hp0 : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + change cnt db r.begin.key = 0 + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeys] at hx + rcases List.mem_append.mp hx with hxQ | hxR + · exact hQge x hxQ + · exact hresge x hxR + have hcE0 : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by + change cnt db r.end_.key = 0 + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeys] at hx + rcases List.mem_append.mp hx with hxQ | hxR + · exact le_trans (le_of_lt hcross) (hQge x hxQ) + · exact le_trans (le_of_lt hcross) (hresge x hxR) + have hlen : db.keys.length = Q.length + res.keys.length := by + rw [hkeys, List.length_append] + rw [completeKeys_length_general (db := db) (r := r) hrev hbegin hend] + rw [hp0, hcE0, Nat.zero_add, Nat.zero_add, hlen] + + +theorem necessary_of_begin_le_crossed_top_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) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) + {x : Key} (hx : r.begin.key ≤ (x : WithTop Key)) : Necessary r res x := by + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hall_ge : ∀ y ∈ res.keys, r.begin.key ≤ (y : WithTop Key) := + crossed_mem_result_ge_begin_general ⟨db₀, hcomp₀⟩ hmore hbegin + by_cases hxmem : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hcomp₀⟩ hxmem + obtain ⟨P, hPlen, hPs, hPge, hPlt, hPsort⟩ := + crossed_pad_general (db := db₀) hrev hbegin hcross (by omega) hne hres + have hPlen' : P.length = b := by simpa [hb] using hPlen + have hPge' : ∀ y ∈ P, r.begin.key ≤ (y : WithTop Key) := by + intro y hy; simpa using hPge y hy + have hhall : ∀ y ∈ res.keys, r.begin.key ≤ (y : WithTop Key) := hall_ge + by_cases hxP : x ∈ P + · -- `x` is already one of the padding keys: erase it to obtain the base. + let dT : DB := ⟨P ++ res.keys, hPsort⟩ + have hcompT : RangeRead.Compatible dT r res := by + refine ⟨hcomp₀.1, Or.inr ⟨hmore, ?_⟩⟩ + exact (completeKeys_pad_eq_general (r := r) (res := res) (P := P) hPs hsort + hPlen' hPge' hhall hPlt hPsort hrev hbegin hcross (by omega) (by omega)).symm + let dB : DB := dT.erase x + have hdBkeys : dB.keys = P.erase x ++ res.keys := by + simp only [dB, dT, DB.erase_keys] + rw [List.erase_append, if_pos hxP] + have hQlen : (P.erase x).length = b - 1 := by + rw [List.length_erase_of_mem hxP, hPlen'] + have hQs : (P.erase x).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.erase x (List.sortedLT_iff_pairwise.mp hPs)) + have hQge : ∀ y ∈ P.erase x, r.begin.key ≤ (y : WithTop Key) := + fun y hy => hPge' y (List.mem_of_mem_erase hy) + have hQsort : (P.erase x ++ res.keys).SortedLT := by + rw [← hdBkeys] + exact dB.sorted + have hdB : dB = ⟨P.erase x ++ res.keys, hQsort⟩ := by + change dT.erase x = ⟨P.erase x ++ res.keys, hQsort⟩ + have hk : (dT.erase x).keys = P.erase x ++ res.keys := hdBkeys + generalize hd : dT.erase x = d at hk ⊢ + cases d with + | mk ks hs => + simp only [DB.mk.injEq] at hk ⊢ + exact hk + have hlenB : (RangeRead.completeKeys dB r).length = res.keys.length - 1 := by + have h := completeKeys_length_pad_general (r := r) (res := res) (Q := P.erase x) + hQs hQsort hQge hhall hrev hbegin hcross (by omega) + rw [hdB, h, hQlen, ← he, ← hb] + have hlt : b - 1 + res.keys.length < e := by omega + rw [Nat.min_eq_right (le_of_lt hlt)] + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hbpos : 0 < b := by + rw [← hPlen'] + exact List.length_pos_of_ne_nil (fun h => by simp [h] at hxP) + omega + have hnotB : ¬ RangeRead.Compatible dB r res := by + intro hc + have heq : res.keys = RangeRead.completeKeys dB r := by + rcases hc.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hlen := congrArg List.length heq + rw [hlenB] at hlen + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + omega + intro cr hs + by_contra hxcr + have hagree : AgreeOn cr dT dB := + (ConflictRange.keys_erase_of_not_mem cr dT hxcr).symm + exact hnotB ((hs dT dB hagree).mp hcompT) + · -- `x` is not padding: use `P ++ res` as base and insert `x`. + let dB : DB := ⟨P ++ res.keys, hPsort⟩ + have hxdb : x ∉ dB.keys := by + simp only [dB, List.mem_append] + rintro (hxp | hxr) + · exact hxP hxp + · exact hxmem hxr + have hcompB : RangeRead.Compatible dB r res := by + refine ⟨hcomp₀.1, Or.inr ⟨hmore, ?_⟩⟩ + exact (completeKeys_pad_eq_general (r := r) (res := res) (P := P) hPs hsort + hPlen' hPge' hhall hPlt hPsort hrev hbegin hcross (by omega) (by omega)).symm + let dT : DB := dB.insert x hxdb + have hxT : ∀ y ∈ dT.keys, r.begin.key ≤ (y : WithTop Key) := by + intro y hy + rw [show dT = dB.insert x hxdb from rfl, DB.mem_insert_iff hxdb] at hy + rcases hy with rfl | hy + · exact hx + · rw [show dB = ⟨P ++ res.keys, hPsort⟩ from rfl, List.mem_append] at hy + rcases hy with hP | hR + · exact hPge' y hP + · exact hhall y hR + have hpT : dT.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + change cnt dT r.begin.key = 0 + apply cnt_eq_zero_of_forall_ge + exact fun y hy => hxT y hy + have hcET : dT.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by + change cnt dT r.end_.key = 0 + apply cnt_eq_zero_of_forall_ge + intro y hy + exact le_trans (le_of_lt hcross) (hxT y hy) + have hlenT : (RangeRead.completeKeys dT r).length = res.keys.length + 1 := by + have h := completeKeys_length_general (db := dT) (r := r) hrev hbegin (by omega) + rw [h, hpT, hcET, Nat.zero_add, Nat.zero_add] + have hlen : dT.keys.length = P.length + res.keys.length + 1 := by + rw [show dT = dB.insert x hxdb from rfl, DB.insert_keys, List.orderedInsert_length] + simp only [dB, List.length_append, hPlen'] + rw [hlen, hPlen', ← hb, ← he] + have hlt : b + res.keys.length + 1 ≤ e := by omega + rw [Nat.min_eq_right hlt] + omega + have hnotT : ¬ RangeRead.Compatible dT r res := by + intro hc + have heq : res.keys = RangeRead.completeKeys dT r := by + rcases hc.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hlen := congrArg List.length heq + rw [hlenT] at hlen + omega + intro cr hs + by_contra hxcr + have hagree : AgreeOn cr dB dT := + (ConflictRange.keys_insert_of_not_mem cr dB hxdb hxcr).symm + exact hnotT ((hs dB dT hagree).mp hcompB) + + + +theorem orderedInsert_append_of_forall_lt {l r : List Key} {k : Key} + (h : ∀ x ∈ r, k < x) : + (l ++ r).orderedInsert (· ≤ ·) k = l.orderedInsert (· ≤ ·) k ++ r := by + induction l with + | nil => + have hr : r.orderedInsert (· ≤ ·) k = k :: r := by + induction r with + | nil => simp + | cons a t ih => + have hka : k ≤ a := le_of_lt (h a (by simp)) + rw [List.orderedInsert_cons, ite_eq_left hka] + simp [hr] + | cons a l ih => + by_cases hka : k ≤ a + · rw [List.cons_append, List.orderedInsert_cons, ite_eq_left hka, + List.orderedInsert_cons, ite_eq_left hka] + simp + · rw [List.cons_append, List.orderedInsert_cons, ite_eq_right hka, + List.orderedInsert_cons, ite_eq_right hka, ih] + simp + +/-- **Necessity of every key in `[end_.key, begin_.key)` in the crossed top branch at +any positive begin offset.** -/ +theorem necessary_of_mem_interval_crossed_top_general {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) + (hroom : (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + set t : ℕ := res.keys.length with htdef + set eo : ℕ := (r.end_.offset - 1).toNat with heodef + set b : ℕ := (r.begin.offset - 1).toNat with hbdef + set s : ℕ := eo - t - b with hsdef + have htb : t + b < eo := by simpa [t, eo, b, htdef, heodef, hbdef] using ht + have hs : 0 < s := by omega + have hsub : s - 1 + 1 = s := Nat.sub_add_cancel (by omega : 1 ≤ s) + have hroom' : ((s : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + simpa [s, eo, t, b, htdef, heodef, hbdef] using hroom + have hcard : (((s - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + rw [hsub]; exact hroom' + obtain ⟨dbH, hHrange, hkH, hHlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := s - 1) hcard + have hHgeE : ∀ x ∈ dbH.keys, r.end_.key ≤ (x : WithTop Key) := + fun x hx => (hHrange x hx).1 + have hHltB : ∀ x ∈ dbH.keys, (x : WithTop Key) < r.begin.key := + fun x hx => (hHrange x hx).2 + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := + crossed_mem_result_ge_begin_general ⟨db₀, hcomp₀⟩ hmore hbegin + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + obtain ⟨P, hPlen₀, hPs, hPge, hPlt, hPsort⟩ := + crossed_pad_general (db := db₀) hrev hbegin hcross (by omega) hne hres + have hPlen : P.length = b := by simpa [b, hbdef] using hPlen₀ + obtain ⟨C, hCge, hCnotin⟩ := + exists_le_not_mem (res.keys ++ dbH.keys) (res.keys.getLast hne) + have hCnotres : C ∉ res.keys := fun h => hCnotin (List.mem_append_left _ h) + have hCnotH : C ∉ dbH.keys := fun h => hCnotin (List.mem_append_right _ h) + have hCgtRes : ∀ a ∈ res.keys, a < C := by + intro a ha + have hle : a ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast ha + exact lt_of_le_of_ne (le_trans hle hCge) (fun h => hCnotres (h ▸ ha)) + have hlast_ge_B : r.begin.key ≤ (res.keys.getLast hne : WithTop Key) := + hall_ge _ (List.getLast_mem hne) + have hHltC : ∀ a ∈ dbH.keys, a < C := by + intro a ha + have h1 : (a : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le (hHltB a ha) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + exact WithTop.coe_lt_coe.mp h1 + have hHltP : ∀ a ∈ dbH.keys, ∀ p ∈ P, a < p := by + intro a ha p hp + have h1 : (a : WithTop Key) < (p : WithTop Key) := + lt_of_lt_of_le (hHltB a ha) (hPge p hp) + exact WithTop.coe_lt_coe.mp h1 + have hPltC : ∀ p ∈ P, p < C := by + intro p hp + exact lt_of_lt_of_le (hPlt p hp (res.keys.getLast hne) (List.getLast_mem hne)) hCge + have hHres_lt : ∀ a ∈ dbH.keys, ∀ c ∈ res.keys, a < c := + fun a ha c hc => + WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltB a ha) (hall_ge c hc)) + have hdbH_P : (dbH.keys ++ P).SortedLT := + List.sortedLT_append.mpr ⟨dbH.sorted, hPs, hHltP⟩ + have hdbH_P_res : (dbH.keys ++ P ++ res.keys).SortedLT := + List.sortedLT_append.mpr ⟨hdbH_P, hsort, by + intro a ha c hc + rcases List.mem_append.mp ha with haH | haP + · exact hHres_lt a haH c hc + · exact hPlt a haP c hc⟩ + have hsingle : ([C] : List Key).SortedLT := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + have hcrossList : ∀ a ∈ (dbH.keys ++ P ++ res.keys), ∀ b ∈ ([C] : List Key), a < b := by + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + rcases List.mem_append.mp ha with ha' | haR + · rcases List.mem_append.mp ha' with haH | haP + · exact hHltC a haH + · exact hPltC a haP + · exact hCgtRes a haR + have hlst_sorted : (dbH.keys ++ P ++ res.keys ++ [C]).SortedLT := + List.sortedLT_append.mpr ⟨hdbH_P_res, hsingle, hcrossList⟩ + let db₂ : DB := ⟨dbH.keys ++ P ++ res.keys ++ [C], hlst_sorted⟩ + have hkeys₂ : db₂.keys = dbH.keys ++ (P ++ (res.keys ++ [C])) := by + simp [db₂, List.append_assoc] + -- counts for `db₂` + have hcE₂ : cnt db₂ r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeys₂] at hx + rcases List.mem_append.mp hx with hxH | hx' + · exact hHgeE x hxH + rcases List.mem_append.mp hx' with hxP | hx'' + · exact le_trans (le_of_lt hcross) (hPge x hxP) + rcases List.mem_append.mp hx'' with hxR | hxC + · exact le_trans (le_of_lt hcross) (hall_ge x hxR) + · rw [List.mem_singleton] at hxC + subst hxC + exact le_trans (le_of_lt hcross) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + have hp₂ : cnt db₂ r.begin.key = s - 1 := by + show db₂.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = s - 1 + rw [hkeys₂, List.countP_append, List.countP_append, List.countP_append] + have hHc : dbH.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbH.keys.length := + cnt_eq_length_of_forall_lt dbH hHltB + have hPc : P.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hPge x hx) + have hresc : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hall_ge x hx) + have hCc : ([C] : List Key).countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + rw [List.mem_singleton] at hx + subst hx + exact not_lt.mpr (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hHc, hPc, hresc, hCc, hHlen] + omega + have hn₂ : db₂.keys.length = eo := by + rw [hkeys₂, List.length_append, List.length_append, List.length_append, hPlen, hHlen] + have htres : res.keys.length = t := rfl + rw [htres, List.length_singleton] + omega + have hk_db₂ : k ∉ db₂.keys := by + rw [hkeys₂] + intro hmem + rcases List.mem_append.mp hmem with hmemH | hmem' + · exact hkH hmemH + rcases List.mem_append.mp hmem' with hmemP | hmem'' + · exact absurd (hPge k hmemP) (not_le.mpr hkB) + rcases List.mem_append.mp hmem'' with hmemR | hmemC + · exact absurd (hall_ge k hmemR) (not_le.mpr hkB) + · rw [List.mem_singleton] at hmemC + have h1 : (k : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le hkB (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hmemC] at h1 + exact (lt_irrefl (C : WithTop Key)) h1 + -- counts for the inserted database + have hcE₁ : cnt (db₂.insert k hk_db₂) r.end_.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk_db₂ (v := r.end_.key) (not_lt.mpr hkE)] + exact hcE₂ + have hp₁ : cnt (db₂.insert k hk_db₂) r.begin.key = s := by + rw [cnt_insert_lt db₂ k hk_db₂ (v := r.begin.key) hkB, hp₂] + omega + have hn₁ : (db₂.insert k hk_db₂).keys.length = eo + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hn₂] + let dbHi : DB := dbH.insert k hkH + have hdbHi_len : dbHi.keys.length = s := by + rw [show dbHi = dbH.insert k hkH from rfl, DB.insert_keys, List.orderedInsert_length, hHlen] + omega + have hkeys_ins : (db₂.insert k hk_db₂).keys = dbHi.keys ++ P ++ res.keys ++ [C] := by + rw [DB.insert_keys, hkeys₂] + rw [show dbH.keys ++ (P ++ (res.keys ++ [C])) = + dbH.keys ++ (P ++ res.keys ++ [C]) by simp [List.append_assoc]] + rw [orderedInsert_append_of_forall_lt] + · simp [dbHi, DB.insert_keys, List.append_assoc] + · intro x hx + rcases List.mem_append.mp hx with hxPR | hxC + · rcases List.mem_append.mp hxPR with hxP | hxR + · exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le hkB (hPge x hxP)) + · exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le hkB (hall_ge x hxR)) + · rw [List.mem_singleton] at hxC + rw [hxC] + have h1 : (k : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le hkB (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + exact WithTop.coe_lt_coe.mp h1 + have hdrop : (dbHi.keys ++ P ++ res.keys ++ [C]).drop (s + b) = res.keys ++ [C] := by + rw [show dbHi.keys ++ P ++ res.keys ++ [C] = + dbHi.keys ++ (P ++ (res.keys ++ [C])) by simp [List.append_assoc]] + rw [List.drop_append] + rw [show s + b - dbHi.keys.length = b by rw [hdbHi_len]; omega] + rw [List.drop_eq_nil_of_le (by rw [hdbHi_len]; omega), List.nil_append] + rw [List.drop_append_of_le_length (by omega)] + rw [show P.drop b = [] by rw [← hPlen, List.drop_length], List.nil_append] + -- compatibility of the inserted database + have hck₁ : RangeRead.completeKeys (db₂.insert k hk_db₂) r = res.keys := by + have htres : res.keys.length = t := rfl + have hp₁' : (db₂.insert k hk_db₂).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = s := by + simpa [cnt] using hp₁ + have hcE₁' : (db₂.insert k hk_db₂).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + simpa [cnt] using hcE₁ + have hidx : eo - (s + b) = t := by omega + have htake : (res.keys ++ [C]).take t = res.keys := by + rw [List.take_append_of_le_length (by rw [htres])] + exact List.take_length + rw [completeKeys_eq_drop_take_general (db := db₂.insert k hk_db₂) (r := r) hrev + hbegin (by omega)] + rw [hp₁', hcE₁', Nat.zero_add, hkeys_ins] + have hlenList : (dbHi.keys ++ P ++ res.keys ++ [C]).length = eo + 1 := by + rw [List.length_append, List.length_append, List.length_append, hdbHi_len, hPlen, + List.length_singleton] + have htres : res.keys.length = t := rfl + rw [htres] + omega + rw [hlenList] + rw [Nat.min_eq_left (by omega : eo ≤ eo + 1)] + rw [hdrop, hidx, htake] + have hcomp₁ : RangeRead.Compatible (db₂.insert k hk_db₂) r res := + ⟨hcomp₀.1, Or.inr ⟨hmore, hck₁.symm⟩⟩ + -- `db₂` itself is not compatible + have hlen₂ : (RangeRead.completeKeys db₂ r).length = t + 1 := by + rw [completeKeys_length_general (db := db₂) (r := r) hrev hbegin (by omega)] + have hp₂' : db₂.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = s - 1 := by + simpa [cnt] using hp₂ + have hcE₂' : db₂.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + simpa [cnt] using hcE₂ + rw [hp₂', hcE₂', Nat.zero_add, hn₂] + have hidx : min eo eo - (s - 1 + b) = t + 1 := by omega + simpa [heodef, hbdef] using hidx + have hnot₂ : ¬ RangeRead.Compatible db₂ r res := by + intro hc + have ht_eq : res.keys.length = t := rfl + rcases hc.2 with ⟨hm, _, _⟩ | ⟨hm, heq⟩ + · rw [hmore] at hm + exact Bool.false_ne_true hm + · rw [← heq] at hlen₂ + rw [ht_eq] at hlen₂ + omega + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₂ (db₂.insert k hk_db₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk_db₂ hkcr).symm + exact hnot₂ ((hs db₂ (db₂.insert k hk_db₂) hagree).mpr hcomp₁) + +end FormalConflictRange + +namespace FormalConflictRange + +/-- **Crossed complete top branch with enough room below the begin key, at any positive +begin offset.** If the gap `[end_.key, begin_.key)` holds at least the overshoot +`e - t - (begin.offset - 1)` keys, the minimal range is `[end_.key, ⊤)`. -/ +theorem minimal_topRange_of_crossed_large_room_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) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) + (hroom : (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key) : + Minimal r res ⟨r.end_.key, ⊤⟩ := by + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨sound_topRange_of_lo_le (by omega) (by omega) (le_of_lt hcross) (le_refl _), ?_⟩ + intro k hkE _ + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_of_mem_interval_crossed_top_general hread hmore hrev hne hbegin hcross + hend ht hroom hkE hkB + · exact necessary_of_begin_le_crossed_top_general hread hmore hrev hne hbegin hcross + hend ht (not_lt.mp hkB) + + + +/-- In a readable crossed complete result with positive end offset at any positive +begin offset, the number of reported keys at or above `end_.key`, plus +`begin.offset - 1`, is at most `end.offset - 1`. -/ +theorem crossed_complete_t_le_e_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) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) : + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat ≤ (r.end_.offset - 1).toNat := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hcount : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [hres] + exact completeKeys_count_ge_eq_length_of_crossed_general (db := db₀) hrev hbegin hcross hend + have hlen := completeKeys_length_general (db := db₀) (r := r) hrev hbegin hend + rw [← hres] at hlen + set p := db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hcEp : cE ≤ p := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk (le_of_lt hcross) + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + rw [hcount] + omega + + + +/-- The remaining overshoot `s = e - t - (begin.offset - 1)` in the crossed complete +top branch. -/ +def crossedOvershootGeneral (r : RangeRead) (res : Result) : ℕ := + endOvershoot r - reportedAtEnd r res - (r.begin.offset - 1).toNat + +/-- The unified forward complete rule at any positive begin offset on crossed selectors. +The lower endpoint is `end_.key` when the gap has room for the remaining overshoot +`e - t - (begin.offset - 1)`, and `begin.key` otherwise. The upper endpoint is +`keyAfter (last reported)` in the finite branch `t + (begin.offset - 1) = e`, and `⊤` +otherwise. -/ +def completeForwardCrossedGeneral (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 1 then canonicalRange r + else if reportedAtEnd r res + (r.begin.offset - 1).toNat = endOvershoot r 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, ⊤⟩ + +/-- **Crossed complete forward minimality at any positive begin offset.** -/ +theorem minimal_completeForwardCrossedGeneral {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.end_.key < r.begin.key) : + Minimal r res (completeForwardCrossedGeneral r res) := by + have hend2 : 2 ≤ r.end_.offset := + crossed_end_offset_ge_two hread hmore hne hbegin hcross + have htle : reportedAtEnd r res + (r.begin.offset - 1).toNat ≤ endOvershoot r := by + have h := crossed_complete_t_le_e_general hread hmore hrev hne hbegin hcross (by omega) + simpa [reportedAtEnd, endOvershoot] using h + have hcount : reportedAtEnd r res = res.keys.length := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + rw [reportedAtEnd, hres] + exact completeKeys_count_ge_eq_length_of_crossed_general (db := db₀) hrev hbegin hcross + (by omega) + by_cases hte : reportedAtEnd r res + (r.begin.offset - 1).toNat = endOvershoot r + · have hmin : Minimal r res + ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := + minimal_truncatedRange_of_crossed_finite_general hread hmore hne hrev hbegin hcross + hend2 (by simpa [reportedAtEnd, endOvershoot] using hte) + rw [completeForwardCrossedGeneral, if_neg (by omega : ¬ r.end_.offset ≤ 1), if_pos hte] + exact hmin + · have htlt : reportedAtEnd r res + (r.begin.offset - 1).toNat < endOvershoot r := + lt_of_le_of_ne htle hte + have htb : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat := by + rw [← hcount] + simpa [endOvershoot] using htlt + by_cases hroom : (crossedOvershootGeneral r res : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key + · have hroom' : (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + simpa [crossedOvershootGeneral, endOvershoot, hcount] using hroom + have hmin : Minimal r res ⟨r.end_.key, ⊤⟩ := + minimal_topRange_of_crossed_large_room_general hread hmore hrev hne hbegin hcross + hend2 htb hroom' + rw [completeForwardCrossedGeneral, if_neg (by omega : ¬ r.end_.offset ≤ 1), + if_neg hte, if_pos hroom] + exact hmin + · have hroom' : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) := by + simpa [crossedOvershootGeneral, endOvershoot, hcount] using lt_of_not_ge hroom + have hmin : Minimal r res ⟨r.begin.key, ⊤⟩ := + minimal_topRange_of_crossed_small_room_general hread hmore hne hrev hbegin hcross + hend2 htb hroom' + rw [completeForwardCrossedGeneral, if_neg (by omega : ¬ r.end_.offset ≤ 1), + if_neg hte, if_neg hroom] + exact hmin + +end FormalConflictRange + +namespace FormalConflictRange + +/-- The reversed-traversal analogue of `completeForwardCrossedGeneral`. -/ +def completeReversedCrossedGeneral (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 1 then canonicalRange r + else if reportedAtEnd r res + (r.begin.offset - 1).toNat = endOvershoot r then + ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ + else + ⟨if (crossedOvershootGeneral r res : ℕ∞) ≤ intervalCard r.end_.key r.begin.key + then r.end_.key else r.begin.key, ⊤⟩ + +/-- **Crossed complete reversed minimality at any positive begin offset**, by flipping +to the forward theorem. -/ +theorem minimal_completeReversedCrossedGeneral {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) : + Minimal r res (completeReversedCrossedGeneral r res) := by + 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 hbegin' : 1 ≤ r'.begin.offset := by simpa [r'] using hbegin + have hmin' : Minimal r' res' (completeForwardCrossedGeneral r' res') := + minimal_completeForwardCrossedGeneral hread' rfl hne' rfl hbegin' hcross + have hflip := (minimal_flip (r := r) (res := res) + (cr := completeForwardCrossedGeneral r' res') (b := true) hrev hmore) + have hformula : completeForwardCrossedGeneral r' res' = completeReversedCrossedGeneral r res := by + unfold completeForwardCrossedGeneral completeReversedCrossedGeneral + dsimp only [r', res'] + simp only [endOvershoot, crossedOvershootGeneral, reportedAtEnd, List.countP_reverse, + getLastD_reverse] + rfl + have hflip' : Minimal r res (completeForwardCrossedGeneral r' res') := hflip.mp hmin' + rw [hformula] at hflip' + exact hflip' + +/-- The forward crossed complete general rule denotes the general conflict range. -/ +theorem completeForwardCrossedGeneral_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) + (hcross : r.end_.key < r.begin.key) : + (completeForwardCrossedGeneral r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_completeForwardCrossedGeneral hread hmore hne hrev hbegin hcross).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The same identification with the executable algorithm. -/ +theorem completeForwardCrossedGeneral_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) + (hcross : r.end_.key < r.begin.key) : + (completeForwardCrossedGeneral r res).toSet = (simpleConflictRange r res).toSet := + (minimal_completeForwardCrossedGeneral hread hmore hne hrev hbegin hcross).toSet_eq + (simpleConflictRange_minimal hread) + +/-- The reversed crossed complete general rule denotes the general conflict range. -/ +theorem completeReversedCrossedGeneral_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) : + (completeReversedCrossedGeneral r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_completeReversedCrossedGeneral hread hmore hne hrev hbegin hcross).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The same identification with the executable algorithm. -/ +theorem completeReversedCrossedGeneral_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) : + (completeReversedCrossedGeneral r res).toSet = (simpleConflictRange r res).toSet := + (minimal_completeReversedCrossedGeneral hread hmore hne hrev hbegin hcross).toSet_eq + (simpleConflictRange_minimal hread) + +/-- The result-aware crossed complete rule at any positive begin offset: dispatch on the +traversal order. -/ +noncomputable def completeCrossedGeneral (r : RangeRead) (res : Result) : ConflictRange := + if r.reverse then completeReversedCrossedGeneral r res + else completeForwardCrossedGeneral r res + +/-- **Total crossed complete closed form at every positive begin offset.** -/ +theorem minimal_completeCrossedGeneral {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) : + Minimal r res (completeCrossedGeneral r res) := by + unfold completeCrossedGeneral + by_cases hrev : r.reverse = true + · rw [ite_eq_left hrev] + exact minimal_completeReversedCrossedGeneral hread hmore hne hrev hbegin hcross + · rw [ite_eq_right hrev] + have hrevf : r.reverse = false := by + cases hr : r.reverse <;> simp_all + exact minimal_completeForwardCrossedGeneral hread hmore hne hrevf hbegin hcross + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedResidual.lean b/FormalConflictRange/CrossedResidual.lean new file mode 100644 index 0000000..8a77ce5 --- /dev/null +++ b/FormalConflictRange/CrossedResidual.lean @@ -0,0 +1,920 @@ +/- +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.CrossedRevGeneral + +/-! +# The crossed reversed truncated residual + +This module closes the last crossed reversed truncated cell: `end_.key < begin_.key`, +`begin.offset ≥ 2`, `end.offset ≥ 2`, a nonempty truncated result with a reported key +at or above `end_.key`, and `#reported + (begin.offset - 1) < end.offset - 1`. The +minimal range always has upper endpoint `⊤`; its lower endpoint is `end_.key` when the +key lattice has enough room (a capacity condition on `intervalCard`), and `begin_.key` +otherwise. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- In a compatible crossed reversed truncated read, the reported length plus the +begin offset shift is at most the end offset shift. -/ +theorem crossed_rev_res_length_add_le_end {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) : + res.keys.length + (r.begin.offset - 1).toNat ≤ (r.end_.offset - 1).toNat := by + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set g : Nat := (between db E r.begin.key).length with hg + set lE : List Key := above db E with hlE + have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by + simpa [E, e, b, lE, g] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have htle : res.keys.length ≤ min e lE.length - (g + b) := by + rw [← hlen] + exact hpre.length_le + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlt : g + b < min e lE.length := by omega + have hle : res.keys.length + (g + b) ≤ min e lE.length := by omega + have h1 : min e lE.length ≤ e := Nat.min_le_left _ _ + omega + +/-- The `b = begin.offset - 1` keys at or above `begin.key` that are dropped by the +crossed reversed window all lie below the least reported key, so the interval from +`begin.key` to that key contains at least `b` keys. -/ +theorem crossed_rev_b_le_capBL {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) : + (((r.begin.offset - 1).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.begin.key (((res.keys.getLast hne : Key)) : WithTop Key) := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set g : Nat := (between db E B).length with hg + set lE : List Key := above db E with hlE + set lB : List Key := above db B with hlB + set L : Key := res.keys.getLast hne with hL + have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) + have hdrop : lB = lE.drop g := by + rw [hlB, hlE, hg] + exact above_eq_drop_between hEB + have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by + simpa [E, e, b, lE, g] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have htle : res.keys.length ≤ min e lE.length - (g + b) := by + rw [← hlen] + exact hpre.length_le + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlt : g + b < min e lE.length := by omega + have hgle : g ≤ lE.length := by omega + have hlBlen : lB.length = lE.length - g := by rw [hdrop, List.length_drop] + have hblt : b < lB.length := by omega + -- the window starts at the `b`-th key at or above `begin.key` + have hb_le_e : b ≤ e := by omega + have hge_le : g + b ≤ e := by omega + have hW : (lE.take e).drop (g + b) = (lB.drop b).take (e - (g + b)) := by + rw [List.drop_take, ← List.drop_drop, ← hdrop] + have hlenW : ((lB.drop b).take (e - (g + b))).length = + min e lE.length - (g + b) := by + rw [← hW, List.length_drop, List.length_take] + have hWne : (lB.drop b).take (e - (g + b)) ≠ [] := by + intro hnil + rw [hnil] at hlenW + simp at hlenW + omega + have hdropne : lB.drop b ≠ [] := by + intro hnil + exact hWne (by rw [hnil]; simp) + have hsorte : lE.SortedLT := by + rw [hlE]; exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hlBsort : lB.SortedLT := by + rw [hlB]; exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hWpos : 0 < e - (g + b) := by + have : 0 < ((lB.drop b).take (e - (g + b))).length := List.length_pos_of_ne_nil hWne + rw [List.length_take] at this + omega + have hsublE : ((lE.take e).drop (g + b)).Sublist lE := + (List.drop_sublist _ _).trans (List.take_sublist _ _) + have hsortW : ((lB.drop b).take (e - (g + b))).SortedLT := by + rw [← hW] + exact (List.Pairwise.sublist hsublE hsorte.pairwise).sortedLT + have hW0 : ((lB.drop b).take (e - (g + b)))[0]'(List.length_pos_of_ne_nil hWne) = + lB[b]'hblt := by + rw [List.getElem_take] + rw [List.getElem_drop] + simp + have hLmemR : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hCK : RangeRead.completeKeys db r = + ((lB.drop b).take (e - (g + b))).reverse := by + rw [hformula, hW] + have hLmemW : L ∈ (lB.drop b).take (e - (g + b)) := by + rw [← List.mem_reverse, ← hCK] + exact hpre.subset hLmemR + have hhead_le_L : lB[b]'hblt ≤ L := by + obtain ⟨j, hj, hjeq⟩ := List.mem_iff_getElem.mp hLmemW + have h0len : 0 < ((lB.drop b).take (e - (g + b))).length := + List.length_pos_of_ne_nil hWne + have hle : ((lB.drop b).take (e - (g + b)))[0]'h0len ≤ + ((lB.drop b).take (e - (g + b)))[j]'hj := by + by_cases hj0 : j = 0 + · subst hj0; rfl + · exact le_of_lt ((hsortW.getElem_lt_getElem_iff).mpr (Nat.pos_of_ne_zero hj0)) + rw [hW0] at hle + rw [← hjeq] + exact hle + have hpad : ∀ k ∈ lB.take b, k < L := by + intro k hk + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hk + have htake_len : (lB.take b).length = b := by + rw [List.length_take, Nat.min_eq_left (le_of_lt hblt)] + have hib : i < b := by omega + have hilt : i < lB.length := lt_trans hib hblt + have hltake : (lB.take b)[i] = lB[i] := List.getElem_take + have hklt : lB[i] < lB[b] := + (List.SortedLT.getElem_lt_getElem_iff (l := lB) hlBsort).mpr hib + rw [← hik, hltake] + exact lt_of_lt_of_le hklt hhead_le_L + have hbetween : between db B (((L : Key)) : WithTop Key) = + lB.filter (fun k : Key => decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) := by + rw [between_eq_filter_above, ← hlB] + have hfilter_len : b ≤ (lB.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key)))).length := by + have hall : (lB.take b).filter (fun k : Key => + decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) = lB.take b := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact WithTop.coe_lt_coe.mpr (hpad k hk) + have hsplit : lB.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) = + (lB.take b).filter (fun k : Key => + decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) ++ + (lB.drop b).filter (fun k : Key => + decide (((k : Key) : WithTop Key) < (((L : Key)) : WithTop Key))) := by + rw [← List.filter_append, List.take_append_drop] + have htake_len : (lB.take b).length = b := by + rw [List.length_take, Nat.min_eq_left (le_of_lt hblt)] + rw [hsplit, hall, List.length_append] + omega + have hBL : B ≤ (((L : Key)) : WithTop Key) := + crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L hLmemR + have hcard := between_length_le_intervalCard (db := db) (E := B) + (B := (((L : Key)) : WithTop Key)) hBL + have hble : ((b : ℕ) : ℕ∞) ≤ + (((between db B (((L : Key)) : WithTop Key)).length : ℕ) : ℕ∞) := by + rw [hbetween] + exact_mod_cast hfilter_len + have hLB : lB = above db B := hlB + calc ((b : ℕ) : ℕ∞) ≤ + (((between db B (((L : Key)) : WithTop Key)).length : ℕ) : ℕ∞) := hble + _ ≤ intervalCard B (((L : Key)) : WithTop Key) := hcard + _ = intervalCard r.begin.key (((res.keys.getLast hne : Key)) : WithTop Key) := by + simp [B, L] + +/-- The crossed residual capacity condition: the end overshoot `e - t` does not +exceed the number of keys in `[end_.key, last reported)`. When it holds the minimal +range starts at `end_.key`, otherwise at `begin_.key`. -/ +def crossedResidualCond (r : RangeRead) (res : Result) (hne : res.keys ≠ []) : Prop := + (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + +/-- When the keys at or above `end_.key` fit inside the end overshoot, the complete +crossed reversed window is the reversed keys at or above `begin.key`, with the begin +shift dropped. In particular the gap count no longer appears. -/ +theorem crossed_rev_window_eq_above_drop {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hle : (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = + ((above db r.begin.key).drop (r.begin.offset - 1).toNat).reverse := by + set E : WithTop Key := r.end_.key with hE + set b : Nat := (r.begin.offset - 1).toNat with hb + set e : Nat := (r.end_.offset - 1).toNat with he + set g : Nat := (between db E r.begin.key).length with hg + have hEB : E ≤ r.begin.key := le_of_lt (by simpa [E] using hcross) + have hformula : RangeRead.completeKeys db r = + (((above db E).take e).drop (g + b)).reverse := by + simpa [E, e, b, g] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have htake : (above db E).take e = above db E := + List.take_of_length_le (by simpa [E, e] using hle) + rw [hformula, htake, above_eq_drop_between hEB, List.drop_drop] + +/-- Under the failure of the capacity condition, every compatible crossed reversed +truncated database has fewer than `end.offset - 1` keys at or above `end_.key`. The +end overshoot is too short to fit the reported keys plus the required room below the +least reported key. -/ +theorem crossed_residual_above_lt_end_of_not_cond {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : + (above db r.end_.key).length < (r.end_.offset - 1).toNat := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set g : Nat := (between db E B).length with hg + set A : List Key := above db E with hA + set L : Key := res.keys.getLast hne with hL + set W : List Key := (A.take e).drop (g + b) with hW + set t : Nat := res.keys.length with ht + set k : Nat := W.length - t with hk + have hformula : RangeRead.completeKeys db r = W.reverse := by + rw [hW] + simpa [E, e, b, g, hA] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hWlen : W.length = min e A.length - (g + b) := by + rw [hW, List.length_drop, List.length_take] + have hlen_le : t ≤ W.length := by + rw [ht] + have h := hpre.length_le + rwa [hformula, List.length_reverse] at h + have htpos : 0 < t := by rw [ht]; exact List.length_pos_of_ne_nil hne + have hkpos : k < W.length := by rw [hk]; omega + have hres_eq : res.keys = (W.drop k).reverse := by + obtain ⟨suf, hsuf⟩ := hpre + have hck : W.reverse = res.keys ++ suf := hformula.symm.trans hsuf.symm + have htake : (W.reverse).take t = res.keys := by + rw [hck, ht, List.take_left] + rw [← htake, List.take_reverse, hk] + have hWne : W ≠ [] := by + intro hnil + rw [hnil] at hkpos + simp at hkpos + have hgetA : A[g + b + k] = L := by + have hltW : k < W.length := hkpos + have hltAtake : g + b + k < (A.take e).length := by + rw [hW] at hltW + rw [List.length_drop] at hltW + omega + have hltA : g + b + k < A.length := lt_of_lt_of_le hltAtake (List.take_sublist e A).length_le + have hWk : W[k]'hltW = A[g + b + k]'hltA := by + simp only [hW, List.getElem_drop, List.getElem_take] + have hLget : res.keys.getLast hne = W[k]'hltW := by + have hdropne : W.drop k ≠ [] := by + intro h; rw [List.drop_eq_nil_iff] at h; omega + have h1 : res.keys.getLast? = some (res.keys.getLast hne) := + List.getLast?_eq_some_getLast hne + have h2 : res.keys.getLast? = some (W[k]'hltW) := by + rw [hres_eq, List.getLast?_reverse, List.head?_eq_some_head hdropne, + List.head_drop hdropne] + exact Option.some.inj (h1.symm.trans h2) + rw [hL, hLget, hWk] + have hsA : A.SortedLT := by + rw [hA] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hcount : A.countP (fun x : Key => decide (x < L)) = g + b + k := by + have hltA : g + b + k < A.length := by + have hltAtake : g + b + k < (A.take e).length := by + have hltW : k < W.length := hkpos + rw [hW] at hltW + rw [List.length_drop] at hltW + omega + exact lt_of_lt_of_le hltAtake (List.take_sublist e A).length_le + exact countP_lt_eq_of_getElem hsA hltA hgetA + have hbetween_len : (between db E ((L : Key) : WithTop Key)).length = g + b + k := by + have hfil : between db E ((L : Key) : WithTop Key) = + A.filter (fun x : Key => decide (x < L)) := by + rw [between_eq_filter_above, ← hA] + apply List.filter_congr + intro x _ + rw [decide_eq_decide] + exact WithTop.coe_lt_coe + rw [hfil, ← List.countP_eq_length_filter] + exact hcount + have hEL : E ≤ ((L : Key) : WithTop Key) := by + have hBL : B ≤ ((L : Key) : WithTop Key) := + crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by + rw [hL]; exact List.getLast_mem hne) + exact le_trans (le_of_lt (by simpa [E, B] using hcross)) hBL + have hcard : (((g + b + k : ℕ)) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by + have := between_length_le_intervalCard (db := db) hEL + rw [hbetween_len] at this + exact this + by_contra hge + have heA : e ≤ A.length := le_of_not_gt hge + have hWlen' : W.length = e - (g + b) := by + rw [hWlen, Nat.min_eq_left heA] + have hkW : k = (e - (g + b)) - t := by rw [hk, hWlen'] + have htle' : t ≤ e - (g + b) := by rw [← hWlen']; exact hlen_le + have hsum : g + b + k = e - t := by omega + apply hnot + rw [crossedResidualCond] + have hcast : (((e - t : ℕ)) : ℕ∞) = (((g + b + k : ℕ)) : ℕ∞) := by rw [hsum] + rw [hcast] + simpa [E, L] using hcard + +/-- Under the failure of the capacity condition, the stored keys at or above the +reported last are exactly the reversed reported keys. -/ +theorem crossed_residual_above_last_eq_rev {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : + above db (((res.keys.getLast hne : Key)) : WithTop Key) = res.keys.reverse := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set L : Key := res.keys.getLast hne with hL + set t : Nat := res.keys.length with ht + set A : List Key := above db B with hA + have hle : (above db E).length ≤ e := + le_of_lt (crossed_residual_above_lt_end_of_not_cond hcomp hrev hbegin hcross hend hne hnot) + have hwin : RangeRead.completeKeys db r = ((above db B).drop b).reverse := + crossed_rev_window_eq_above_drop (db := db) hrev hbegin hcross hend hle + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + obtain ⟨suf, hsuf⟩ := hpre + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hBL : B ≤ ((L : Key) : WithTop Key) := + crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by + rw [hL]; exact List.getLast_mem hne) + have hWdecomp : (above db B).drop b = suf.reverse ++ res.keys.reverse := by + have hr := congrArg List.reverse hsuf + rw [List.reverse_append] at hr + rw [hwin, List.reverse_reverse] at hr + exact hr.symm + have hAdecomp : A = A.take b ++ (suf.reverse ++ res.keys.reverse) := by + calc A = A.take b ++ A.drop b := (List.take_append_drop b A).symm + _ = A.take b ++ (above db B).drop b := by rw [hA] + _ = A.take b ++ (suf.reverse ++ res.keys.reverse) := by rw [hWdecomp] + have hAsorted : A.SortedLT := by + rw [hA] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hpair := hAsorted.pairwise + rw [hAdecomp, List.pairwise_append] at hpair + obtain ⟨_, hSuffix, hcrosslt⟩ := hpair + obtain ⟨_, _, hcrossSuf⟩ := List.pairwise_append.mp hSuffix + have hLmem : L ∈ res.keys.reverse := by + rw [List.mem_reverse] + rw [hL] + exact List.getLast_mem hne + have haboveL_eq : above db ((L : Key) : WithTop Key) = + A.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ (k : WithTop Key))) := by + rw [hA] + unfold above + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) + · have hBk : B ≤ (k : WithTop Key) := le_trans hBL hLk + simp [hLk, hBk] + · simp [hLk] + have hfilter : A.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ + (k : WithTop Key))) = res.keys.reverse := by + rw [hAdecomp, List.filter_append, List.filter_append] + have hPnil : (A.take b).filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ + (k : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hge + have hLmem' : L ∈ suf.reverse ++ res.keys.reverse := + List.mem_append.mpr (Or.inr hLmem) + have hlt : x < L := hcrosslt x hx L hLmem' + exact absurd (of_decide_eq_true hge) (not_le.mpr (WithTop.coe_lt_coe.mpr hlt)) + have hSnil : suf.reverse.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ + (k : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hge + have hlt : x < L := hcrossSuf x hx L hLmem + exact absurd (of_decide_eq_true hge) (not_le.mpr (WithTop.coe_lt_coe.mpr hlt)) + have hRself : res.keys.reverse.filter (fun k : Key => decide (((L : Key) : WithTop Key) ≤ + (k : WithTop Key))) = res.keys.reverse := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have hxmem : x ∈ res.keys := (List.mem_reverse.mp hx) + exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hxmem) + rw [hPnil, hSnil, hRself, List.nil_append, List.nil_append] + exact haboveL_eq.trans hfilter + +/-- Under the failure of the capacity condition, the stored keys at or above +`begin.key` are bounded by the key-lattice gap below the reported last plus the +number of reported keys. -/ +theorem crossed_residual_above_begin_le_card_add {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : + (((above db r.begin.key).length : ℕ) : ℕ∞) ≤ + intervalCard r.begin.key (((res.keys.getLast hne : Key)) : WithTop Key) + + (res.keys.length : ℕ∞) := by + set B : WithTop Key := r.begin.key with hB + set L : Key := res.keys.getLast hne with hL + set t : Nat := res.keys.length with ht + set A : List Key := above db B with hA + have hBL : B ≤ ((L : Key) : WithTop Key) := + crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by + rw [hL]; exact List.getLast_mem hne) + have haboveL : above db ((L : Key) : WithTop Key) = res.keys.reverse := + crossed_residual_above_last_eq_rev hcomp hrev hbegin hcross hend hne hnot + have hpart : A.length = (between db B ((L : Key) : WithTop Key)).length + t := by + have h := length_above_eq_between_add_above (db := db) (E := B) + (L := ((L : Key) : WithTop Key)) hBL + rw [haboveL, List.length_reverse, ← ht] at h + simpa [A] using h + have hbetween_le : (((between db B ((L : Key) : WithTop Key)).length : ℕ) : ℕ∞) ≤ + intervalCard B ((L : Key) : WithTop Key) := + between_length_le_intervalCard (db := db) hBL + have hcast : ((A.length : ℕ) : ℕ∞) ≤ + intervalCard B ((L : Key) : WithTop Key) + (t : ℕ∞) := by + rw [hpart, Nat.cast_add] + exact add_le_add_left hbetween_le (t : ℕ∞) + simpa [A, B, L, t] using hcast + +/-- If a database agrees with a compatible database on `[begin.key, ⊤)` in the crossed +reversed truncated residual under the failure of the capacity condition, it has fewer +than `end.offset - 1` keys at or above `end_.key`. This is exactly the room needed for +the crossed reversed window to ignore the gap. -/ +theorem crossed_residual_above_end_lt_of_agree {db db' : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) + (hagree : above db' r.begin.key = above db r.begin.key) : + (above db' r.end_.key).length < (r.end_.offset - 1).toNat := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set t : Nat := res.keys.length with ht + set L : Key := res.keys.getLast hne with hL + have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) + have hBL : B ≤ ((L : Key) : WithTop Key) := + crossed_rev_res_ge_begin_general (db := db) (r := r) (res := res) hcomp hbegin L (by + rw [hL]; exact List.getLast_mem hne) + have hA' : (above db' B).length = (above db B).length := by rw [hagree] + have hAle : (((above db' B).length : ℕ) : ℕ∞) ≤ + intervalCard B ((L : Key) : WithTop Key) + (t : ℕ∞) := by + rw [hA'] + exact crossed_residual_above_begin_le_card_add hcomp hrev hbegin hcross hend hne hnot + have hpart : (above db' E).length = (between db' E B).length + (above db' B).length := + length_above_eq_between_add_above (db := db') hEB + have hbetween_le : (((between db' E B).length : ℕ) : ℕ∞) ≤ intervalCard E B := + between_length_le_intervalCard (db := db') hEB + have hsumcast : ((above db' E).length : ℕ∞) ≤ + intervalCard E B + intervalCard B ((L : Key) : WithTop Key) + (t : ℕ∞) := by + rw [hpart, Nat.cast_add] + have h := add_le_add hbetween_le hAle + simpa [add_assoc] using h + have hadd : intervalCard E B + intervalCard B ((L : Key) : WithTop Key) = + intervalCard E ((L : Key) : WithTop Key) := + (intervalCard_add hEB hBL).symm + rw [hadd] at hsumcast + have hnotC : ¬ ((((e - t : ℕ)) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key)) := by + rw [crossedResidualCond] at hnot + simpa [E, L, e, t] using hnot + have hnot' : intervalCard E ((L : Key) : WithTop Key) < ((e - t : ℕ) : ℕ∞) := + not_le.mp hnotC + have htle : t ≤ e := by + have h := crossed_rev_res_length_add_le_end hcomp hrev hbegin hcross hend hne + omega + have hlt : ((above db' E).length : ℕ∞) < (e : ℕ∞) := by + calc ((above db' E).length : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := + hsumcast + _ < ((e - t : ℕ) : ℕ∞) + (t : ℕ∞) := + WithTop.add_lt_add_right (z := (t : ℕ∞)) (ENat.coe_ne_top t) hnot' + _ = (e : ℕ∞) := by rw [← Nat.cast_add]; congr 1; omega + have := ENat.natCast_lt_natCast.mp hlt + simpa [E, e] using this + +/-- Soundness of the crossed reversed truncated residual's `begin.key` top branch: +under the failure of the capacity condition, agreement on `[begin.key, ⊤)` fixes the +complete keys, because both databases have fewer than `end.offset - 1` keys at or above +`end_.key` and the window then collapses to the keys at or above `begin.key`. -/ +theorem sound_crossedResidual_top_begin {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hnot : ¬ crossedResidualCond r res hne) : + Sound r res ⟨r.begin.key, ⊤⟩ := by + set B : WithTop Key := r.begin.key with hB + set b : Nat := (r.begin.offset - 1).toNat with hb + intro db₁ db₂ hagree + have habove : above db₁ B = above db₂ B := by + have h := hagree + rw [AgreeOn] at h + simpa [ConflictRange.keys, above, B] using h + have hkeys (db : DB) (hc : RangeRead.Compatible db r res) : + RangeRead.completeKeys db r = ((above db B).drop b).reverse := by + have hlt : (above db r.end_.key).length < (r.end_.offset - 1).toNat := + crossed_residual_above_lt_end_of_not_cond hc hrev hbegin hcross hend hne hnot + simpa [B, b] using + crossed_rev_window_eq_above_drop (db := db) hrev hbegin hcross hend (le_of_lt hlt) + constructor + · intro hc₁ + have hlt₂ : (above db₂ r.end_.key).length < (r.end_.offset - 1).toNat := + crossed_residual_above_end_lt_of_agree hc₁ hrev hbegin hcross hend hne hnot habove.symm + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [hkeys db₁ hc₁] + have h2 : RangeRead.completeKeys db₂ r = ((above db₂ B).drop b).reverse := by + simpa [B, b] using + crossed_rev_window_eq_above_drop (db := db₂) hrev hbegin hcross hend (le_of_lt hlt₂) + rw [h2, habove] + simpa [RangeRead.Compatible, hck] using hc₁ + · intro hc₂ + have hlt₁ : (above db₁ r.end_.key).length < (r.end_.offset - 1).toNat := + crossed_residual_above_end_lt_of_agree hc₂ hrev hbegin hcross hend hne hnot habove + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [hkeys db₂ hc₂] + have h1 : RangeRead.completeKeys db₁ r = ((above db₁ B).drop b).reverse := by + simpa [B, b] using + crossed_rev_window_eq_above_drop (db := db₁) hrev hbegin hcross hend (le_of_lt hlt₁) + rw [h1, habove] + simpa [RangeRead.Compatible, hck] using hc₂ + +/-- In a compatible crossed reversed truncated read, more than `begin.offset - 1` +stored keys lie at or above `begin.key`. -/ +theorem crossed_rev_above_begin_length_gt_shift {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) : + (r.begin.offset - 1).toNat < (above db r.begin.key).length := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set g : Nat := (between db E B).length with hg + set A : List Key := above db E with hA + have hformula : RangeRead.completeKeys db r = ((A.take e).drop (g + b)).reverse := by + simpa [E, B, e, b, g, A] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hlen : (RangeRead.completeKeys db r).length = min e A.length - (g + b) := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have htle : res.keys.length ≤ min e A.length - (g + b) := by + rw [← hlen]; exact hpre.length_le + have hmin : g + b < min e A.length := by omega + have hAle : min e A.length ≤ A.length := Nat.min_le_right _ _ + have hApart : A.length = g + (above db B).length := by + have h := length_above_eq_between_add_above (db := db) (E := E) (L := B) + (le_of_lt (by simpa [E, B] using hcross)) + simpa [A, g] using h + omega + +/-- In the crossed reversed truncated residual, every key in +`[begin.key, last reported)` is necessary. The witness database has exactly +`begin.offset - 1` stored keys in `[begin.key, last reported)`, one of them the target +key `k`, followed by the reversed reported keys; erasing `k` shifts the window by one +and drops the last reported key. -/ +theorem crossed_residual_necessary_below_last_not_cond {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hres : res.keys.length + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) + {k : Key} (hBk : r.begin.key ≤ (k : WithTop Key)) + (hkL : ((k : Key) : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set L : Key := res.keys.getLast hne with hL + have hbegin1 : 1 ≤ r.begin.offset := by omega + have hres' : res.keys.length + b < e := by omega + have hbpos : 0 < b := by omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx + rw [hL] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hBltL : B < ((L : Key) : WithTop Key) := lt_of_le_of_lt hBk hkL + have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) + have hbcard : (((b : ℕ)) : ℕ∞) ≤ intervalCard B ((L : Key) : WithTop Key) := by + simpa [B, L, b] using + crossed_rev_b_le_capBL (db := db₀) (r := r) (res := res) hcomp₀ hrev hbegin1 hcross + hend hne + have hsub : b - 1 + 1 = b := Nat.sub_add_cancel (Nat.succ_le_of_lt hbpos) + have hcast : (((b - 1 : ℕ)) : ℕ∞) + 1 = ((b : ℕ) : ℕ∞) := by + exact_mod_cast hsub + have hcard' : (((b - 1 : ℕ)) : ℕ∞) + 1 ≤ intervalCard B ((L : Key) : WithTop Key) := by + rw [hcast] + exact hbcard + obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ := + exists_db_keysIn_avoid (B := B) (E := ((L : Key) : WithTop Key)) (k := k) (m := b - 1) + hcard' + set P : List Key := (pad.insert k hkpad).keys with hP + have hPmem : ∀ x ∈ P, + B ≤ (x : WithTop Key) ∧ ((x : Key) : WithTop Key) < ((L : Key) : WithTop Key) := by + intro x hx + have hx' : x ∈ List.orderedInsert (fun x1 x2 => x1 ≤ x2) k pad.keys := by + rw [hP, DB.insert_keys] at hx + exact hx + rw [List.mem_orderedInsert] at hx' + rcases hx' with rfl | hx' + · exact ⟨hBk, hkL⟩ + · simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx' + have hPsort : P.SortedLT := by + rw [hP] + exact (pad.insert k hkpad).sorted + have hPlen : P.length = b := by + rw [hP, DB.insert_keys, List.orderedInsert_length, hpadlen, hsub] + have hkP : k ∈ P := by + rw [hP, DB.insert_keys] + rw [List.mem_orderedInsert] + exact Or.inl rfl + set R : List Key := res.keys.reverse with hR + have hRsort : R.SortedLT := by + rw [hR] + exact hsort.reverse + have hcrossPR : ∀ x ∈ P, ∀ y ∈ R, x < y := by + intro x hx y hy + have hxL : x < L := WithTop.coe_lt_coe.mp (hPmem x hx).2 + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + exact lt_of_lt_of_le hxL (hLle y hyres) + have hfullSort : (P ++ R).SortedLT := + List.sortedLT_append.mpr ⟨hPsort, hRsort, hcrossPR⟩ + set dbfull : DB := ⟨P ++ R, hfullSort⟩ with hdbfull + have hfullKeys : dbfull.keys = P ++ R := rfl + have hRmem : ∀ y ∈ R, L ≤ y := by + intro y hy + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + exact hLle y hyres + have haboveFullB : above dbfull B = P ++ R := by + rw [above_eq_filter, hfullKeys, List.filter_eq_self] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact decide_eq_true (hPmem x hx).1 + · exact decide_eq_true (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx))) + have haboveFullE : above dbfull E = P ++ R := by + rw [above_eq_filter, hfullKeys, List.filter_eq_self] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact decide_eq_true (le_trans hEB (hPmem x hx).1) + · exact decide_eq_true + (le_trans hEB (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx)))) + have hfullElen : (above dbfull E).length ≤ e := by + rw [haboveFullE, List.length_append, hPlen, hR, List.length_reverse] + omega + have hckFull : RangeRead.completeKeys dbfull r = res.keys := by + rw [crossed_rev_window_eq_above_drop (db := dbfull) hrev hbegin1 hcross hend hfullElen, + haboveFullB, List.drop_append_of_le_length (le_of_eq hPlen.symm), + List.drop_eq_nil_of_le (le_of_eq hPlen), List.nil_append, hR, List.reverse_reverse] + have hcompFull : RangeRead.Compatible dbfull r res := by + refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + simpa [hckFull] using List.prefix_refl res.keys + refine necessary_of_toggle ⟨dbfull, hcompFull, ?_⟩ + have hkFull : k ∈ dbfull.keys := by + rw [hfullKeys] + exact List.mem_append.mpr (Or.inl hkP) + rw [toggle_eq_erase_of_mem hkFull] + intro hcompA + have hka : k ∉ R := by + intro hkR + have hkres : k ∈ res.keys := by + rw [hR] at hkR + exact List.mem_reverse.mp hkR + exact absurd (hLle k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkL)) + have hAkeys : (dbfull.erase k).keys = P.erase k ++ R := by + rw [DB.erase_keys, hfullKeys, List.erase_append_left R hkP] + have hPlen' : (P.erase k).length = b - 1 := by + rw [List.length_erase_of_mem hkP, hPlen] + have haboveAB : above (dbfull.erase k) B = P.erase k ++ R := by + rw [above_eq_filter, hAkeys, List.filter_eq_self] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact decide_eq_true (hPmem x (List.mem_of_mem_erase hx)).1 + · exact decide_eq_true (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx))) + have haboveAE : above (dbfull.erase k) E = P.erase k ++ R := by + rw [above_eq_filter, hAkeys, List.filter_eq_self] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact decide_eq_true (le_trans hEB (hPmem x (List.mem_of_mem_erase hx)).1) + · exact decide_eq_true + (le_trans hEB (le_trans (le_of_lt hBltL) (WithTop.coe_le_coe.mpr (hRmem x hx)))) + have hAelen : (above (dbfull.erase k) E).length ≤ e := by + rw [haboveAE, List.length_append, hPlen', hR, List.length_reverse] + omega + have hckA : RangeRead.completeKeys (dbfull.erase k) r = res.keys.dropLast := by + rw [crossed_rev_window_eq_above_drop (db := dbfull.erase k) hrev hbegin1 hcross hend + hAelen, haboveAB, List.drop_append] + have hdropP : (P.erase k).drop b = [] := + List.drop_eq_nil_of_le (by rw [hPlen']; omega) + rw [hdropP, List.nil_append, hPlen'] + have hb1 : b - (b - 1) = 1 := by omega + rw [hb1, hR, List.drop_reverse, List.reverse_reverse, List.dropLast_eq_take] + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + rcases hcompA.2 with ⟨_, hpre, _⟩ | ⟨hm, _⟩ + · rw [hckA] at hpre + have hle := hpre.length_le + rw [List.length_dropLast] at hle + omega + · rw [hmore] at hm + simp at hm + +/-- Inserting a key above every element of a sorted list appends it. -/ +theorem orderedInsert_eq_append_of_forall_lt {l : List Key} {k : Key} + (h : ∀ x ∈ l, x < k) : + List.orderedInsert (fun x1 x2 => x1 ≤ x2) k l = l ++ [k] := by + induction l with + | nil => simp + | cons a t ih => + rw [List.orderedInsert_cons] + have hat : a < k := h a (by simp) + rw [if_neg (not_le.mpr hat), ih (fun x hx => h x (by simp [hx]))] + rfl + +/-- Under the failure of the capacity condition, every key above the reported head is +necessary: inserting it appends it to the complete window, whose first element would +then have to be the reported head. -/ +theorem crossed_residual_necessary_above_head_not_cond {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hnot : ¬ crossedResidualCond r res hne) + {k : Key} (hkH : res.keys.head hne < k) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set H : Key := res.keys.head hne with hH + set A : List Key := above db₀ B with hA + have hlt₀ : (above db₀ E).length < e := + crossed_residual_above_lt_end_of_not_cond hcomp₀ hrev hbegin hcross hend hne hnot + have hwin₀ : RangeRead.completeKeys db₀ r = (A.drop b).reverse := by + simpa [A, B, b, E, e] using + crossed_rev_window_eq_above_drop (db := db₀) hrev hbegin hcross hend (le_of_lt hlt₀) + have hpre₀ : res.keys <+: (A.drop b).reverse := by + rw [← hwin₀] + exact RangeRead.compatible_keys_prefix hcomp₀ + have hpre₀len : res.keys.length ≤ (A.drop b).reverse.length := + List.IsPrefix.length_le hpre₀ + obtain ⟨suf, hsuf⟩ := hpre₀ + have hAdrop : A.drop b = suf.reverse ++ res.keys.reverse := by + have h := congrArg List.reverse hsuf + rw [List.reverse_append, List.reverse_reverse] at h + exact h.symm + have hAsort : A.SortedLT := by + rw [hA] + exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hAdrop_ne : A.drop b ≠ [] := by + intro hnil + have h : res.keys ++ suf = ([] : List Key) := by rw [hsuf, hnil]; simp + have hlen := congrArg List.length h + rw [List.length_append, List.length_nil] at hlen + omega + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLeH : ∀ y ∈ res.keys, y ≤ H := by + intro y hy + have := (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hy + simpa [hH] using this + have hdrop_le_H : ∀ y ∈ A.drop b, y ≤ H := by + intro y hy + rw [hAdrop, List.mem_append] at hy + have hRne : res.keys.reverse ≠ [] := by + intro h + rw [List.reverse_eq_nil_iff] at h + exact hne h + rcases hy with hy | hy + · have hsufSort : (suf.reverse ++ res.keys.reverse).SortedLT := by + rw [← hAdrop] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop hAsort.pairwise) + have hcrossS : ∀ a ∈ suf.reverse, ∀ z ∈ res.keys.reverse, a < z := + (List.sortedLT_append.mp hsufSort).2.2 + have hylt : y < res.keys.reverse.head hRne := hcrossS y hy _ (List.head_mem hRne) + have hhead : res.keys.reverse.head hRne = res.keys.getLast hne := by + rw [List.head_reverse] + have hLH : res.keys.reverse.head hRne ≤ H := by + rw [hhead] + exact hLeH _ (List.getLast_mem hne) + exact le_trans (le_of_lt hylt) hLH + · exact hLeH y (List.mem_reverse.mp hy) + have hAall : ∀ x ∈ A, x ≤ H := by + intro x hx + have hAeq : A = A.take b ++ A.drop b := (List.take_append_drop b A).symm + rw [hAeq, List.mem_append] at hx + rcases hx with hx | hx + · have hcrossT : ∀ a ∈ A.take b, ∀ z ∈ A.drop b, a < z := by + have hs := List.sortedLT_append.mp (by rw [← hAeq]; exact hAsort) + exact hs.2.2 + exact le_trans (le_of_lt (hcrossT x hx _ (List.head_mem hAdrop_ne))) + (hdrop_le_H _ (List.head_mem hAdrop_ne)) + · exact hdrop_le_H x hx + have hBH : B ≤ ((H : Key) : WithTop Key) := by + simpa [B, H] using + crossed_rev_res_ge_begin_general (db := db₀) (r := r) (res := res) hcomp₀ hbegin H + (by rw [hH]; exact List.head_mem hne) + have hHltk : (H : WithTop Key) < (k : WithTop Key) := WithTop.coe_lt_coe.mpr hkH + have hEk : E ≤ (k : WithTop Key) := + le_trans (le_of_lt (by simpa [E, B] using hcross)) (le_trans hBH (le_of_lt hHltk)) + have hk_not : k ∉ db₀.keys := by + intro hkdb + have hkA : k ∈ A := by + rw [hA, mem_above] + exact ⟨hkdb, le_trans hBH (le_of_lt hHltk)⟩ + exact absurd hkH (not_lt_of_ge (hAall k hkA)) + have hdb_all : ∀ x ∈ db₀.keys, x < k := by + intro x hx + by_cases hxB : B ≤ (x : WithTop Key) + · have hxA : x ∈ A := by + rw [hA, mem_above] + exact ⟨hx, hxB⟩ + exact lt_of_le_of_lt (hAall x hxA) hkH + · have hlt : (x : WithTop Key) < B := lt_of_not_ge hxB + exact WithTop.coe_lt_coe.mp + (lt_trans hlt (lt_of_le_of_lt hBH hHltk)) + set dbfull' : DB := db₀.insert k hk_not with hdbfull' + have hins : dbfull'.keys = db₀.keys ++ [k] := by + rw [hdbfull', DB.insert_keys] + exact orderedInsert_eq_append_of_forall_lt hdb_all + have habove'B : above dbfull' B = A ++ [k] := by + rw [above_eq_filter, hins] + simp [above_eq_filter, hA, decide_eq_true (le_trans hBH (le_of_lt hHltk))] + have habove'E : above dbfull' E = above db₀ E ++ [k] := by + rw [above_eq_filter, hins] + simp [above_eq_filter, decide_eq_true hEk] + have hAb : b < A.length := by + simpa [A, B, b] using + crossed_rev_above_begin_length_gt_shift (db := db₀) (r := r) (res := res) hcomp₀ hrev + hbegin hcross hend hne + have hle₁ : (above dbfull' E).length ≤ e := by + rw [habove'E, List.length_append, List.length_singleton] + omega + have hck₁ : RangeRead.completeKeys dbfull' r = [k] ++ (res.keys ++ suf) := by + rw [crossed_rev_window_eq_above_drop (db := dbfull') hrev hbegin hcross hend hle₁, + habove'B, List.drop_append_of_le_length (le_of_lt hAb), List.reverse_append] + simp only [List.reverse_singleton] + rw [← hsuf] + refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not, ← hdbfull'] + intro hcomp' + have hpre' : res.keys <+: [k] ++ (res.keys ++ suf) := by + rcases hcomp'.2 with ⟨_, hp, _⟩ | ⟨hm, _⟩ + · rwa [hck₁] at hp + · rw [hmore] at hm + simp at hm + have h0 := hpre'.getElem (i := 0) htpos + have hlhs : res.keys[0] = H := by + simpa [hH] using (List.getElem_zero htpos) + have hrhs : ([k] ++ (res.keys ++ suf))[0] = k := by + simp [List.getElem_cons_zero] + rw [hlhs, hrhs] at h0 + exact absurd hkH (not_lt_of_ge (le_of_eq h0.symm)) + +/-- Under the failure of the capacity condition, the crossed reversed truncated residual +has minimal range `[begin.key, ⊤)`. -/ +theorem minimal_crossedResidual_not_cond {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hres : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat) + (hnot : ¬ crossedResidualCond r res hne) : + Minimal r res ⟨r.begin.key, ⊤⟩ := by + have hbegin1 : 1 ≤ r.begin.offset := by omega + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_crossedResidual_top_begin hrev hbegin1 hcross hend hne hnot + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hkB, _⟩ := hk + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + by_cases hkL : (k : WithTop Key) < ((L : Key) : WithTop Key) + · exact crossed_residual_necessary_below_last_not_cond hread hmore hrev hbegin hcross + hend hne hres hkB hkL + · have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hkL + by_cases hkHle : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) + · exact necessary_of_between_result hread + (by rw [hL]; exact List.getLast_mem hne) + (by rw [hH]; exact List.head_mem hne) + (WithTop.coe_le_coe.mp hLk) (WithTop.coe_le_coe.mp hkHle) + · have hHk : ((H : Key) : WithTop Key) < (k : WithTop Key) := lt_of_not_ge hkHle + exact crossed_residual_necessary_above_head_not_cond hread hmore hrev hbegin1 + hcross hend hne hnot (WithTop.coe_lt_coe.mp hHk) + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedResidualCond.lean b/FormalConflictRange/CrossedResidualCond.lean new file mode 100644 index 0000000..fb548a6 --- /dev/null +++ b/FormalConflictRange/CrossedResidualCond.lean @@ -0,0 +1,590 @@ +/- +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.CrossedResidual + +/-! +# The crossed reversed truncated residual under the capacity condition + +This module closes the remaining branch of the crossed reversed truncated residual: +when `crossedResidualCond` holds, the minimal range is `[end_.key, ⊤)`. The two new +necessity constructions are the gap keys `[end_.key, begin_.key)`, which need a +capacity-filling database with an extra key above the reported head, and the keys above +the reported head, which need a pad of `begin.offset - 1` keys below the reported last. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- Choice of the number of gap keys used in the capacity-branch witness. -/ +private theorem exists_gap_choice {D b : ℕ} {capGap capBL : ℕ∞} + (hbD : b < D) (hgap1 : (1 : ℕ∞) ≤ capGap) + (hbBL : (b : ℕ∞) ≤ capBL) + (hcond : (D : ℕ∞) ≤ capGap + capBL) : + ∃ g : ℕ, 1 ≤ g ∧ g ≤ D - b ∧ (g : ℕ∞) ≤ capGap ∧ + ((D - g : ℕ) : ℕ∞) ≤ capBL := by + by_cases hG : capGap = ⊤ + · refine ⟨D - b, by omega, le_refl _, hG ▸ le_top, ?_⟩ + have hsub : D - (D - b) = b := by omega + rw [hsub] + exact hbBL + · obtain ⟨nG, rfl⟩ := ENat.ne_top_iff_exists.mp hG + have hnG1 : 1 ≤ nG := by exact_mod_cast hgap1 + by_cases hGL : nG ≤ D - b + · refine ⟨nG, hnG1, hGL, le_refl _, ?_⟩ + by_cases hB : capBL = ⊤ + · rw [hB] + exact le_top + · obtain ⟨nBL, rfl⟩ := ENat.ne_top_iff_exists.mp hB + have hcast : (D : ℕ∞) ≤ ((nG + nBL : ℕ) : ℕ∞) := by + simpa [Nat.cast_add] using hcond + have hDle : D ≤ nG + nBL := Nat.cast_le.mp hcast + have hle : D - nG ≤ nBL := by omega + exact_mod_cast hle + · refine ⟨D - b, by omega, le_refl _, ?_, ?_⟩ + · have hle : D - b ≤ nG := by omega + exact_mod_cast hle + · have hsub : D - (D - b) = b := by omega + rw [hsub] + exact hbBL + +/-- Under the residual, every key above the reported head is necessary. A pad of +`begin.offset - 1` keys in `[begin.key, last reported)` followed by the reversed +reported keys is compatible; inserting a key above the head appends it to the window, +so the reported head is no longer first. -/ +theorem necessary_crossedResidual_above_head_residual {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hres : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat) + {k : Key} (hkH : res.keys.head hne < k) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set R : List Key := res.keys.reverse with hR + have hbegin1 : 1 ≤ r.begin.offset := by omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne + have hBL : B ≤ ((L : Key) : WithTop Key) := by + simpa [B, L] using + crossed_rev_res_ge_begin_general (db := db₀) (r := r) (res := res) hcomp₀ + hbegin1 L hLmem + have hbcard : ((b : ℕ) : ℕ∞) ≤ intervalCard B ((L : Key) : WithTop Key) := by + simpa [B, L, b] using + crossed_rev_b_le_capBL (db := db₀) (r := r) (res := res) hcomp₀ hrev hbegin1 + hcross hend hne + obtain ⟨pad, hpadIn, hpadlen⟩ := + exists_db_keysIn (B := B) (E := ((L : Key) : WithTop Key)) (m := b) hbcard + set P : List Key := pad.keys with hP + have hPsort : P.SortedLT := pad.sorted + have hPmem : ∀ x ∈ P, B ≤ (x : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((L : Key) : WithTop Key) := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx + have hPlen : P.length = b := hpadlen + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLleH : L ≤ H := by + have h := (hsort.pairwise.imp le_of_lt).rel_getLast hHmem + simpa [hL, hH] using h + have hRsort : R.SortedLT := by + rw [hR] + exact hsort.reverse + have hRmem : ∀ y ∈ R, L ≤ y := by + intro y hy + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + exact (hsort.pairwise.imp le_of_lt).rel_getLast hyres + have hRleH : ∀ y ∈ R, y ≤ H := by + intro y hy + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + simpa [hH] using (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hyres + have hPR : ∀ x ∈ P, ∀ y ∈ R, x < y := by + intro x hx y hy + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mp (hPmem x hx).2) (hRmem y hy) + have hfullSort : (P ++ R).SortedLT := + List.sortedLT_append.mpr ⟨hPsort, hRsort, hPR⟩ + set dbfull : DB := ⟨P ++ R, hfullSort⟩ with hdbfull + have hfullKeys : dbfull.keys = P ++ R := rfl + have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) + have haboveB : above dbfull B = P ++ R := by + rw [above_eq_filter, hfullKeys, List.filter_eq_self] + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact decide_eq_true (hPmem x hx).1 + · exact decide_eq_true (le_trans hBL (WithTop.coe_le_coe.mpr (hRmem x hx))) + have haboveE : above dbfull E = P ++ R := by + rw [above_eq_filter, hfullKeys, List.filter_eq_self] + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact decide_eq_true (le_trans hEB (hPmem x hx).1) + · exact decide_eq_true + (le_trans hEB (le_trans hBL (WithTop.coe_le_coe.mpr (hRmem x hx)))) + have hckFull : RangeRead.completeKeys dbfull r = res.keys := by + have hle : (above dbfull E).length ≤ e := by + rw [haboveE, List.length_append, hPlen, hR, List.length_reverse] + omega + rw [crossed_rev_window_eq_above_drop (db := dbfull) hrev hbegin1 hcross hend hle, + haboveB, List.drop_append_of_le_length (le_of_eq hPlen.symm), + List.drop_eq_nil_of_le (le_of_eq hPlen), List.nil_append, hR, + List.reverse_reverse] + have hcompFull : RangeRead.Compatible dbfull r res := by + refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + simp [hckFull] + have hk_not : k ∉ dbfull.keys := by + rw [hfullKeys] + intro hk + rcases List.mem_append.mp hk with hk | hk + · have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := + le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hkH)) + exact absurd (hPmem k hk).2 (not_lt.mpr hLk) + · exact absurd (hRleH k hk) (not_le.mpr hkH) + have hins : (dbfull.insert k hk_not).keys = dbfull.keys ++ [k] := by + rw [DB.insert_keys] + exact orderedInsert_eq_append_of_forall_lt (l := dbfull.keys) (k := k) (by + intro x hx + rw [hfullKeys] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_lt_of_le (lt_of_lt_of_le (WithTop.coe_lt_coe.mp (hPmem x hx).2) + hLleH) (le_of_lt hkH) + · exact lt_of_le_of_lt (hRleH x hx) hkH) + have hfiltE : List.filter (fun x : Key => decide (E ≤ (x : WithTop Key))) + (P ++ R) = P ++ R := by + have h := haboveE + rw [above_eq_filter, hfullKeys] at h + exact h + have hfiltEk : List.filter (fun x : Key => decide (E ≤ (x : WithTop Key))) + [k] = [k] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst x + exact decide_eq_true (le_trans hEB (le_trans hBL + (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hkH))))) + have haboveIns : above (dbfull.insert k hk_not) E = (P ++ R) ++ [k] := by + rw [above_eq_filter, hins, hfullKeys, List.filter_append, hfiltE, hfiltEk] + have hckIns : RangeRead.completeKeys (dbfull.insert k hk_not) r = [k] ++ res.keys := by + have hle : (above (dbfull.insert k hk_not) E).length ≤ e := by + rw [haboveIns, List.length_append, List.length_append, hPlen, hR, + List.length_reverse, List.length_singleton] + omega + rw [crossed_rev_window_eq_above_drop (db := dbfull.insert k hk_not) hrev hbegin1 + hcross hend hle] + have hfiltB : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) + (P ++ R) = P ++ R := by + have h := haboveB + rw [above_eq_filter, hfullKeys] at h + exact h + have hfiltBk : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) + [k] = [k] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst x + exact decide_eq_true (le_trans hBL + (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hkH)))) + have haboveB' : above (dbfull.insert k hk_not) B = (P ++ R) ++ [k] := by + rw [above_eq_filter, hins, hfullKeys, List.filter_append, hfiltB, hfiltBk] + rw [haboveB', List.append_assoc, List.drop_append_of_le_length (le_of_eq hPlen.symm), + List.drop_eq_nil_of_le (le_of_eq hPlen), List.nil_append, List.reverse_append, + hR, List.reverse_reverse, List.reverse_singleton] + refine necessary_of_toggle ⟨dbfull, hcompFull, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not] + intro hcomp' + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hpre' : res.keys <+: [k] ++ res.keys := by + rcases hcomp'.2 with ⟨_, hp, _⟩ | ⟨hm, _⟩ + · rw [hckIns] at hp + exact hp + · rw [hmore] at hm + simp at hm + have h0 := hpre'.getElem (i := 0) htpos + have hlhs : res.keys[0] = H := by + simpa [hH] using (List.getElem_zero htpos) + have hrhs : ([k] ++ res.keys)[0] = k := by + simp [List.getElem_cons_zero] + rw [hlhs, hrhs] at h0 + exact absurd hkH (not_lt_of_ge (le_of_eq h0.symm)) + +/-- Under the capacity condition, every key in `[end_.key, begin_.key)` is necessary. +The witness has `g` keys in the gap (including the target), `D - g` pad keys in +`[begin_.key, last reported)`, the reversed reported keys, and one key above the reported +head. Erasing the target lengthens the window by one, so the key above the head enters +the complete keys and breaks compatibility. -/ +theorem necessary_crossedResidual_gap_cond {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hres : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat) + (hcond : crossedResidualCond r res hne) + {k : Key} (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set t : Nat := res.keys.length with ht + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set D : Nat := e - t with hD + have hbegin1 : 1 ≤ r.begin.offset := by omega + have hres' : t + b < e := by simpa [ht, hb, he] using hres + have hbD : b < D := by rw [hD]; omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne + have hEB : E ≤ B := le_of_lt (by simpa [E, B] using hcross) + have hBL : B ≤ ((L : Key) : WithTop Key) := by + simpa [B, L] using + crossed_rev_res_ge_begin_general (db := db₀) (r := r) (res := res) hcomp₀ + hbegin1 L hLmem + have hbcard : ((b : ℕ) : ℕ∞) ≤ intervalCard B ((L : Key) : WithTop Key) := by + simpa [B, L, b] using + crossed_rev_b_le_capBL (db := db₀) (r := r) (res := res) hcomp₀ hrev hbegin1 + hcross hend hne + have hgap1 : (1 : ℕ∞) ≤ intervalCard E B := by + simpa [E, B] using + one_le_intervalCard_of_le_lt (B := E) (E := B) (k := k) + (by simpa [E] using hEk) (by simpa [B] using hkB) + have hcondEL : ((D : ℕ) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by + rw [crossedResidualCond] at hcond + simpa [E, L, e, t, ht, he, hL, hD] using hcond + have hcond' : ((D : ℕ) : ℕ∞) ≤ + intervalCard E B + intervalCard B ((L : Key) : WithTop Key) := by + rw [← intervalCard_add hEB hBL] + exact hcondEL + obtain ⟨g, hg1, hgDb, hgG, hgP⟩ := exists_gap_choice hbD hgap1 hbcard hcond' + set p : Nat := D - g with hp + have hgp : g + p = D := by rw [hp]; omega + have hpb : b ≤ p := by + rw [hp] + omega + obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ := + exists_db_keysIn_avoid (B := E) (E := B) (k := k) (m := g - 1) (by + have hsub : g - 1 + 1 = g := Nat.sub_add_cancel hg1 + have h : (((g - 1 + 1 : ℕ)) : ℕ∞) ≤ intervalCard E B := by + rw [hsub] + exact hgG + simpa only [Nat.cast_add, Nat.cast_one] using h) + set G : List Key := (pad.insert k hkpad).keys with hGdef + have hGmem : ∀ x ∈ G, E ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B := by + intro x hx + have hx' : x ∈ List.orderedInsert (fun x1 x2 => x1 ≤ x2) k pad.keys := by + rw [hGdef, DB.insert_keys] at hx + exact hx + rw [List.mem_orderedInsert] at hx' + rcases hx' with rfl | hx' + · exact ⟨by simpa [E] using hEk, by simpa [B] using hkB⟩ + · simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx' + have hGsort : G.SortedLT := by + rw [hGdef] + exact (pad.insert k hkpad).sorted + have hGlen : G.length = g := by + rw [hGdef, DB.insert_keys, List.orderedInsert_length, hpadlen] + omega + obtain ⟨padL, hpadLIn, hpadLlen⟩ := + exists_db_keysIn (B := B) (E := ((L : Key) : WithTop Key)) (m := p) (by + rw [hp] + exact hgP) + set P : List Key := padL.keys with hPdef + have hPmem : ∀ x ∈ P, B ≤ (x : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((L : Key) : WithTop Key) := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpadLIn x hx + have hPsort : P.SortedLT := padL.sorted + have hPlen : P.length = p := hpadLlen + set Q : Key := keyAfter H with hQ + have hHQ : H < Q := by rw [hQ]; exact keyAfter_self_lt H + have hQsort : ([Q] : List Key).SortedLT := sortedLT_singleton Q + set R : List Key := res.keys.reverse with hR + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLleH : L ≤ H := by + have h := (hsort.pairwise.imp le_of_lt).rel_getLast hHmem + simpa [hL, hH] using h + have hRsort : R.SortedLT := by + rw [hR] + exact hsort.reverse + have hRmem : ∀ y ∈ R, L ≤ y := by + intro y hy + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + exact (hsort.pairwise.imp le_of_lt).rel_getLast hyres + have hRleH : ∀ y ∈ R, y ≤ H := by + intro y hy + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + simpa [hH] using (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hyres + have hGP : ∀ x ∈ G, ∀ y ∈ P, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hGmem x hx).2 (hPmem y hy).1) + have hGPR : ∀ x ∈ G ++ P, ∀ y ∈ R, x < y := by + intro x hx y hy + rcases List.mem_append.mp hx with hx | hx + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hGmem x hx).2 + (le_trans hBL (WithTop.coe_le_coe.mpr (hRmem y hy)))) + · exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hPmem x hx).2 (WithTop.coe_le_coe.mpr (hRmem y hy))) + set T : List Key := (G ++ P) ++ R with hT + have hTsort : T.SortedLT := by + rw [hT] + exact List.sortedLT_append.mpr ⟨ + List.sortedLT_append.mpr ⟨hGsort, hPsort, hGP⟩, hRsort, hGPR⟩ + have hTQ : ∀ x ∈ T, x < Q := by + intro x hx + rw [hT] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · have hxH : (x : WithTop Key) ≤ (H : WithTop Key) := + le_trans (le_trans (le_of_lt (hGmem x hx).2) hBL) + (WithTop.coe_le_coe.mpr hLleH) + exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt hxH (WithTop.coe_lt_coe.mpr hHQ)) + · have hxH : (x : WithTop Key) ≤ (H : WithTop Key) := + le_trans (le_of_lt (hPmem x hx).2) (WithTop.coe_le_coe.mpr hLleH) + exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt hxH (WithTop.coe_lt_coe.mpr hHQ)) + · exact lt_of_le_of_lt (hRleH x hx) hHQ + have hfullSort : (T ++ [Q]).SortedLT := + List.sortedLT_append.mpr ⟨hTsort, hQsort, by + intro x hx y hy + rw [List.mem_singleton] at hy + subst y + exact hTQ x hx⟩ + set dbfull : DB := ⟨T ++ [Q], hfullSort⟩ with hdbfull + have hfullKeys : dbfull.keys = T ++ [Q] := rfl + have haboveE : above dbfull E = T ++ [Q] := by + rw [above_eq_filter, hfullKeys, List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · rw [hT] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x hx).1 + · exact le_trans hEB (hPmem x hx).1 + · exact le_trans hEB (le_trans hBL (WithTop.coe_le_coe.mpr (hRmem x hx))) + · rw [List.mem_singleton] at hx + subst x + exact le_trans hEB (le_trans hBL + (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hHQ)))) + have haboveB : above dbfull B = (P ++ R) ++ [Q] := by + rw [above_eq_filter, hfullKeys, hT] + rw [List.filter_append, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) G = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hge + exact absurd (hGmem x hx).2 (not_lt.mpr (of_decide_eq_true hge)) + have hPself : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) P = P := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hPmem x hx).1 + have hRself : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hBL.trans (WithTop.coe_le_coe.mpr (hRmem x hx))) + have hQself : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) [Q] = [Q] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst x + exact decide_eq_true (le_trans hBL + (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hHQ)))) + rw [hGnil, hPself, hRself, hQself] + simp + have haboveElen : (above dbfull E).length = g + p + t + 1 := by + rw [haboveE, hT, List.length_append, List.length_append, List.length_append, + hGlen, hPlen, hR, List.length_reverse, List.length_singleton] + have haboveBlen : (above dbfull B).length = p + t + 1 := by + rw [haboveB, List.length_append, List.length_append, hPlen, hR, + List.length_reverse, List.length_singleton] + have hbetweenLen : (between dbfull E B).length = g := by + have h := length_above_eq_between_add_above (db := dbfull) (E := E) (L := B) hEB + rw [haboveElen, haboveBlen] at h + omega + have htle : t ≤ e := by omega + have hTlen : T.length = e := by + rw [hT, List.length_append, List.length_append, hGlen, hPlen, hR, + List.length_reverse] + omega + have htake : (T ++ [Q]).take e = T := by + rw [← hTlen] + exact List.take_left + have hGPdrop : (G ++ P).drop (g + b) = P.drop b := by + rw [← List.drop_drop] + rw [List.drop_append_of_le_length (le_of_eq hGlen.symm)] + rw [List.drop_eq_nil_of_le (le_of_eq hGlen), List.nil_append] + have hdrop : T.drop (g + b) = P.drop b ++ R := by + rw [hT] + rw [List.drop_append_of_le_length (by + rw [List.length_append, hGlen, hPlen] + omega)] + rw [hGPdrop] + have hckFull : RangeRead.completeKeys dbfull r = + res.keys ++ (P.drop b).reverse := by + rw [completeKeys_crossed_rev_eq_general (db := dbfull) hrev hbegin1 hcross hend, + haboveE, hbetweenLen, htake, hdrop, List.reverse_append, hR, + List.reverse_reverse] + have hcompFull : RangeRead.Compatible dbfull r res := by + refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hckFull] + exact List.prefix_append res.keys (P.drop b).reverse + have hkG : k ∈ G := by + rw [hGdef, DB.insert_keys] + rw [List.mem_orderedInsert] + exact Or.inl rfl + have hkGP : k ∈ G ++ P := List.mem_append.mpr (Or.inl hkG) + have hkT : k ∈ T := by + rw [hT] + exact List.mem_append.mpr (Or.inl hkGP) + have hkeysErase : (dbfull.erase k).keys = ((G.erase k ++ P) ++ R) ++ [Q] := by + rw [DB.erase_keys, hfullKeys, hT] + rw [List.erase_append_left (l₁ := (G ++ P) ++ R) [Q] + (List.mem_append.mpr (Or.inl hkGP))] + rw [List.erase_append_left (l₁ := G ++ P) R hkGP] + rw [List.erase_append_left (l₁ := G) P hkG] + have haboveEraseE : above (dbfull.erase k) E = ((G.erase k ++ P) ++ R) ++ [Q] := by + rw [above_eq_filter, hkeysErase, List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x (List.mem_of_mem_erase hx)).1 + · exact le_trans hEB (hPmem x hx).1 + · exact le_trans hEB (le_trans hBL (WithTop.coe_le_coe.mpr (hRmem x hx))) + · rw [List.mem_singleton] at hx + subst x + exact le_trans hEB (le_trans hBL + (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hHQ)))) + have haboveEraseB : above (dbfull.erase k) B = (P ++ R) ++ [Q] := by + rw [above_eq_filter, hkeysErase] + rw [List.filter_append, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) + (G.erase k) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hge + exact absurd (hGmem x (List.mem_of_mem_erase hx)).2 + (not_lt.mpr (of_decide_eq_true hge)) + have hPself : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) P = P := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hPmem x hx).1 + have hRself : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hBL.trans (WithTop.coe_le_coe.mpr (hRmem x hx))) + have hQself : List.filter (fun x : Key => decide (B ≤ (x : WithTop Key))) [Q] = [Q] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst x + exact decide_eq_true (le_trans hBL + (le_trans (WithTop.coe_le_coe.mpr hLleH) (le_of_lt (WithTop.coe_lt_coe.mpr hHQ)))) + rw [hGnil, hPself, hRself, hQself] + simp + have hGeraseLen : (G.erase k).length = g - 1 := by + rw [List.length_erase_of_mem hkG, hGlen] + have hEraseElen : (above (dbfull.erase k) E).length = (g - 1) + p + t + 1 := by + rw [haboveEraseE, List.length_append, List.length_append, List.length_append, + hGeraseLen, hPlen, hR, List.length_reverse, List.length_singleton] + have hEraseBlen : (above (dbfull.erase k) B).length = p + t + 1 := by + rw [haboveEraseB, List.length_append, List.length_append, hPlen, hR, + List.length_reverse, List.length_singleton] + have hbetweenEraseLen : (between (dbfull.erase k) E B).length = g - 1 := by + have h := length_above_eq_between_add_above (db := dbfull.erase k) (E := E) + (L := B) hEB + rw [hEraseElen, hEraseBlen] at h + omega + have hEraseElenE : (above (dbfull.erase k) E).length = e := by + rw [hEraseElen] + omega + have htakeErase : (above (dbfull.erase k) E).take e = above (dbfull.erase k) E := by + rw [← hEraseElenE] + exact List.take_length + have hGPdropErase : (G.erase k ++ P).drop (g - 1 + b) = P.drop b := by + rw [← List.drop_drop] + rw [List.drop_append_of_le_length (le_of_eq hGeraseLen.symm)] + rw [List.drop_eq_nil_of_le (le_of_eq hGeraseLen), List.nil_append] + have hdropErase : (above (dbfull.erase k) E).drop (g - 1 + b) = + P.drop b ++ R ++ [Q] := by + rw [haboveEraseE, List.append_assoc] + rw [List.drop_append_of_le_length (by + rw [List.length_append, hGeraseLen, hPlen] + omega)] + rw [hGPdropErase, List.append_assoc] + have hckErase : RangeRead.completeKeys (dbfull.erase k) r = + [Q] ++ res.keys ++ (P.drop b).reverse := by + rw [completeKeys_crossed_rev_eq_general (db := dbfull.erase k) hrev hbegin1 hcross + hend, hbetweenEraseLen, htakeErase, hdropErase, + List.reverse_append, List.reverse_append, hR, List.reverse_reverse, + List.reverse_singleton, List.append_assoc] + refine necessary_of_toggle ⟨dbfull, hcompFull, ?_⟩ + have hkFull : k ∈ dbfull.keys := by + rw [hfullKeys] + exact List.mem_append.mpr (Or.inl hkT) + rw [toggle_eq_erase_of_mem hkFull] + intro hcomp' + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hpre' := RangeRead.compatible_keys_prefix hcomp' + have hpre'' : res.keys <+: [Q] ++ res.keys ++ (P.drop b).reverse := by + simpa [hckErase] using hpre' + have h0 := hpre''.getElem (i := 0) htpos + have hlhs : res.keys[0] = H := by + simpa [hH] using (List.getElem_zero htpos) + have hrhs : ([Q] ++ res.keys ++ (P.drop b).reverse)[0] = Q := by + simp [List.getElem_cons_zero] + rw [hlhs, hrhs] at h0 + exact (ne_of_lt hHQ) h0 + +/-- Under the capacity condition, the crossed reversed truncated residual has minimal +range `[end_.key, ⊤)`. -/ +theorem minimal_crossedResidual_cond {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hres : res.keys.length + (r.begin.offset - 1).toNat < + (r.end_.offset - 1).toNat) + (hcond : crossedResidualCond r res hne) : + Minimal r res ⟨r.end_.key, ⊤⟩ := by + have hbegin1 : 1 ≤ r.begin.offset := by omega + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · simpa using sound_truncatedRevCrossed_top_general (r := r) (res := res) + hrev hbegin1 hcross hend + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hEk, _⟩ := hk + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_crossedResidual_gap_cond hread hmore hrev hbegin hcross hend hne + hres hcond hEk hkB + · have hBk : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hkB + by_cases hkL : (k : WithTop Key) < ((L : Key) : WithTop Key) + · exact crossed_residual_necessary_below_last_not_cond hread hmore hrev hbegin + hcross hend hne hres hBk hkL + · have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hkL + by_cases hkHle : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) + · exact necessary_of_between_result hread + (by rw [hL]; exact List.getLast_mem hne) + (by rw [hH]; exact List.head_mem hne) + (WithTop.coe_le_coe.mp hLk) (WithTop.coe_le_coe.mp hkHle) + · have hHk : ((H : Key) : WithTop Key) < (k : WithTop Key) := lt_of_not_ge hkHle + exact necessary_crossedResidual_above_head_residual hread hmore hrev hbegin + hcross hend hne hres (WithTop.coe_lt_coe.mp hHk) + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedRev.lean b/FormalConflictRange/CrossedRev.lean new file mode 100644 index 0000000..ccf3f20 --- /dev/null +++ b/FormalConflictRange/CrossedRev.lean @@ -0,0 +1,1478 @@ +/- +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.CrossedTruncated + +/-! +# Crossed reversed truncated reads at begin offset one + +This module closes the last cell of the offset-one closed form: a truncated result +whose traversal is reversed and whose selector keys are crossed, `end_.key < +begin_.key`. + +Because the traversal is reversed, the reported keys are the largest keys of the +complete window. Writing `E = end_.key`, `B = begin_.key`, `e = (end_.offset - +1).toNat`, and `t = res.keys.length`, all reported keys lie at or above `B > E`, so +`t` is also the number of reported keys at or above `E`. The complete window, +relative to the stored keys at or above `E`, is the block obtained by dropping the +gap `[E, B)` and taking the first `e` keys. + +A compatible result is a prefix of the reversed window. Compatibility forces the +gap to be empty when `t = e`; then the window is exactly the reported keys and the +minimal range is `[E, keyAfter (head reported))`. When `t < e` the resolved end +outruns the reported block, and the minimal range is the unbounded `[E, ⊤)`. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +theorem completeKeys_rev_eq_reverse (db : DB) (r : RangeRead) (hrev : r.reverse = true) : + RangeRead.completeKeys db r = + (RangeRead.completeKeys db { r with reverse := false }).reverse := by + simp [RangeRead.completeKeys, hrev] + +/-- A compatible crossed reversed result has all reported keys at or above `end_.key`. -/ +theorem crossed_rev_res_ge_end {db : DB} {r : RangeRead} {res : Result} + (hc : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + ∀ k ∈ res.keys, r.end_.key ≤ (k : WithTop Key) := by + intro k hk + have hmem := (RangeRead.compatible_keys_prefix hc).subset hk + rw [completeKeys_rev_eq_reverse db r hrev] at hmem + have hmem' := List.mem_reverse.mp hmem + rw [completeKeys_eq_filter_take_of_offset_one (db := db) + (r := { r with reverse := false }) (by simp) hbegin hend] at hmem' + have hfil := List.mem_of_mem_take hmem' + have hge := (List.mem_filter.mp hfil).2 + rw [decide_eq_true_eq] at hge + exact le_trans (le_of_lt hcross) hge + +theorem crossed_rev_count_ge_eq_length {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hread : Readable r res) : + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + res.keys.length := by + obtain ⟨db, hc⟩ := hread + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact crossed_rev_res_ge_end hc hrev hbegin hcross hend k hk + +/-- The keys at or above `B` are the keys at or above `E` with the gap `[E, B)` dropped. -/ +theorem above_eq_drop_between {db : DB} {E B : WithTop Key} (hEB : E ≤ B) : + above db B = (above db E).drop (between db E B).length := by + rw [above_B_eq_filter hEB, between_eq_filter_above] + have hs : (above db E).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hL : (above db E).filter (fun k : Key => decide (B ≤ (k : WithTop Key))) = + (above db E).filter (fun k : Key => decide (¬ ((k : WithTop Key) < B))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + rw [hL, filter_not_lt_eq_drop_countP hs B, List.countP_eq_length_filter] + +private theorem take_sub_drop {α : Type*} (l : List α) (e g : Nat) : + (l.drop g).take (min e l.length - g) = (l.take e).drop g := by + rw [List.drop_take, List.take_eq_take_iff] + simp only [List.length_drop] + omega + +/-- The complete keys of a crossed reversed read are the reversed block of the +keys at or above `end_.key` from which the gap is dropped. -/ +theorem completeKeys_crossed_rev_eq {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = + (((above db r.end_.key).take ((r.end_.offset - 1).toNat)).drop + (between db r.end_.key r.begin.key).length).reverse := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + have hEB : E ≤ B := le_of_lt hcross + have hfwd : RangeRead.completeKeys db r = + ((above db B).take (min (cnt db E + e) db.keys.length - cnt db B)).reverse := by + rw [completeKeys_rev_eq_reverse db r hrev] + rw [completeKeys_eq_filter_take_of_offset_one (db := db) + (r := { r with reverse := false }) (by simp) hbegin hend] + have hfilB : db.keys.filter (fun k : Key => + decide (({ r with reverse := false } : RangeRead).begin.key ≤ + (k : WithTop Key))) = above db B := rfl + rw [hfilB] + rfl + have hcalc : min (cnt db E + e) db.keys.length - cnt db B = + min e (above db E).length - (between db E B).length := by + rw [← cnt_add_above_length db E, cnt_eq_add_between db hEB, + Nat.add_min_add_left, Nat.add_sub_add_left] + rw [hfwd, hcalc, above_eq_drop_between hEB, take_sub_drop] + +/-- Agreement on the keys at or above `end_.key` fixes the complete keys of a +crossed reversed read. -/ +theorem completeKeys_eq_of_agree_above_crossed_rev {db₁ db₂ : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (habove : above db₁ r.end_.key = above db₂ r.end_.key) : + RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + have hEB : E ≤ B := le_of_lt hcross + have hfwd (db : DB) : + RangeRead.completeKeys db r = + ((above db B).take + (min (cnt db E + e) db.keys.length - cnt db B)).reverse := by + rw [completeKeys_rev_eq_reverse db r hrev] + rw [completeKeys_eq_filter_take_of_offset_one (db := db) + (r := { r with reverse := false }) (by simp) hbegin hend] + have hfilB : db.keys.filter (fun k : Key => + decide (({ r with reverse := false } : RangeRead).begin.key ≤ + (k : WithTop Key))) = above db B := rfl + rw [hfilB] + rfl + have hU : above db₁ E = above db₂ E := habove + have hB_eq : above db₁ B = above db₂ B := by + rw [above_B_eq_filter (db := db₁) hEB, above_B_eq_filter (db := db₂) hEB, hU] + have hg_eq : (between db₁ E B).length = (between db₂ E B).length := by + rw [between_eq_filter_above (db := db₁) E B, + between_eq_filter_above (db := db₂) E B, hU] + have hUlen : (above db₁ E).length = (above db₂ E).length := by rw [hU] + have hcalc (db : DB) : + min (cnt db E + e) db.keys.length - cnt db B = + min e (above db E).length - (between db E B).length := by + rw [← cnt_add_above_length db E, cnt_eq_add_between db hEB, + Nat.add_min_add_left, Nat.add_sub_add_left] + have hlen_eq : + min (cnt db₁ E + e) db₁.keys.length - cnt db₁ B = + min (cnt db₂ E + e) db₂.keys.length - cnt db₂ B := by + rw [hcalc db₁, hcalc db₂, hUlen, hg_eq] + rw [hfwd db₁, hfwd db₂, hB_eq, hlen_eq] + +/-- The stored keys of `[lo, keyAfter last)` are the keys at or above `lo` that are +at most `last`. -/ +private theorem keys_lo_keyAfter_eq_filter_le {db : DB} {lo : WithTop Key} {last : Key} : + (⟨lo, (keyAfter last : Key)⟩ : ConflictRange).keys db = + (db.keys.filter (fun k : Key => decide (lo ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + unfold ConflictRange.keys + rw [show db.keys.filter (fun (k : Key) => decide (lo ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + db.keys.filter (fun (k : Key) => + decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key)) && + decide (lo ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter] + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + +/-- In the finite branch the candidate range contains exactly the reported keys, +reversed. -/ +theorem crossed_rev_finite_keys_eq {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length = (r.end_.offset - 1).toNat) : + (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : ConflictRange).keys db = + res.keys.reverse := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set lE : List Key := above db E with hlE + set g : Nat := (between db E B).length with hg + have hEB : E ≤ B := le_of_lt hcross + have hformula : RangeRead.completeKeys db r = ((lE.take e).drop g).reverse := by + simpa [E, B, e, lE, g] using + completeKeys_crossed_rev_eq (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hlen_ck : (RangeRead.completeKeys db r).length = min e lE.length - g := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have hepos : 0 < e := by + have h := List.length_pos_of_ne_nil hne + omega + have htle : e ≤ min e lE.length - g := by + have h := hpre.length_le + rw [hlen_ck, ht] at h + exact h + have hg0 : g = 0 := by omega + have hmin : min e lE.length = e := by omega + have hckeq : RangeRead.completeKeys db r = (lE.take e).reverse := by + rw [hformula, hg0, List.drop_zero] + have hlenck_e : (RangeRead.completeKeys db r).length = e := by + rw [hckeq, List.length_reverse, List.length_take, hmin] + have hreseq : res.keys = (lE.take e).reverse := by + have h := hpre.eq_of_length (by rw [ht, hlenck_e]) + rw [h] at * + exact hckeq + have htake_ne : lE.take e ≠ [] := by + intro hnil + rw [hnil, List.reverse_nil] at hreseq + exact hne hreseq + have hH : res.keys.head hne = (lE.take e).getLast htake_ne := by + apply (List.head_eq_iff_head?_eq_some hne).mpr + rw [hreseq, List.head?_reverse] + exact List.getLast?_eq_getLast_of_ne_nil htake_ne + have hsE : lE.SortedLT := by + rw [hlE] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have htake_sorted : (lE.take e).SortedLT := + List.sortedLT_iff_pairwise.mpr + (List.Pairwise.take (i := e) (List.sortedLT_iff_pairwise.mp hsE)) + have hle_take : ∀ x ∈ lE.take e, x ≤ (lE.take e).getLast htake_ne := + fun x hx => (htake_sorted.pairwise.imp le_of_lt).rel_getLast hx + have hsplit : lE = lE.take e ++ lE.drop e := (List.take_append_drop e lE).symm + have hcross_lt : ∀ x ∈ lE.take e, ∀ y ∈ lE.drop e, x < y := by + have hs' : (lE.take e ++ lE.drop e).SortedLT := by rw [← hsplit]; exact hsE + exact (List.sortedLT_append.mp hs').2.2 + have hfil_take : (lE.take e).filter (fun k : Key => decide (k ≤ res.keys.head hne)) = + lE.take e := by + rw [List.filter_eq_self] + intro x hx + rw [hH] + exact decide_eq_true (hle_take x hx) + have hfil_drop : (lE.drop e).filter (fun k : Key => decide (k ≤ res.keys.head hne)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [hH, decide_eq_true_eq] + exact not_le.mpr (hcross_lt _ (List.getLast_mem htake_ne) x hx) + rw [keys_lo_keyAfter_eq_filter_le] + change lE.filter (fun k : Key => decide (k ≤ res.keys.head hne)) = res.keys.reverse + conv_lhs => rw [hsplit] + rw [List.filter_append, hfil_take, hfil_drop, List.append_nil] + rw [hreseq, List.reverse_reverse] + +/-- A compatible crossed reversed result has all reported keys at or above `begin.key`. -/ +theorem crossed_rev_res_ge_begin {db : DB} {r : RangeRead} {res : Result} + (hc : RangeRead.Compatible db r res) (hbegin : r.begin.offset = 1) : + ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := by + intro k hk + have hmem := (RangeRead.compatible_keys_prefix hc).subset hk + exact le_trans (key_le_resolve_of_one_le (db := db) (s := r.begin) (by omega)) + (RangeRead.mem_completeKeys.mp hmem).2.1 + +/-- If the range `[E, keyAfter H)` contains exactly the reported keys reversed, then +the first `e` keys at or above `E` are those reported keys and the gap `[E, B)` is +empty. -/ +theorem take_above_eq_of_range_keys_eq {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length = (r.end_.offset - 1).toNat) + (hBR : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key)) + (hR : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : + ConflictRange).keys db = res.keys.reverse) : + (above db r.end_.key).take ((r.end_.offset - 1).toNat) = res.keys.reverse ∧ + between db r.end_.key r.begin.key = [] := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set H : Key := res.keys.head hne with hHdef + set lE : List Key := above db E with hlE + have hEB : E ≤ B := le_of_lt hcross + have hsE : lE.SortedLT := by + rw [hlE] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hRfil : lE.filter (fun k : Key => decide (k ≤ H)) = res.keys.reverse := by + have h := hR + rw [keys_lo_keyAfter_eq_filter_le] at h + simpa only [lE, above] using h + have htake_c : lE.take (lE.countP (fun k : Key => decide (k ≤ H))) = res.keys.reverse := by + rw [← filter_le_eq_take_countP hsE H, hRfil] + have hc_le : lE.countP (fun k : Key => decide (k ≤ H)) ≤ lE.length := + List.countP_le_length + have hc_eq : lE.countP (fun k : Key => decide (k ≤ H)) = e := by + have hlen := congrArg List.length htake_c + rw [List.length_take, List.length_reverse, ht] at hlen + omega + have htakeE : lE.take e = res.keys.reverse := by + rw [← hc_eq] + exact htake_c + have hbnil : between db E B = [] := by + rw [between_eq_filter_above] + change lE.filter (fun k : Key => decide ((k : WithTop Key) < B)) = [] + have hsplit : lE = res.keys.reverse ++ lE.drop e := by + rw [← htakeE] + exact (List.take_append_drop e lE).symm + rw [hsplit] + have hRnil : res.keys.reverse.filter + (fun k : Key => decide ((k : WithTop Key) < B)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hBR x (List.mem_reverse.mp hx)) + have htailnil : (lE.drop e).filter + (fun k : Key => decide ((k : WithTop Key) < B)) = [] := by + rw [List.filter_eq_nil_iff] + intro y hy + rw [decide_eq_true_eq] + have hs' : (res.keys.reverse ++ lE.drop e).SortedLT := by + rw [← hsplit]; exact hsE + have hcross := (List.sortedLT_append.mp hs').2.2 + have hrevne : res.keys.reverse ≠ [] := by + rwa [List.reverse_ne_nil_iff] + obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil _ hrevne + exact not_lt.mpr (le_of_lt (lt_of_le_of_lt (hBR x (List.mem_reverse.mp hx)) + (WithTop.coe_lt_coe.mpr (hcross x hx y hy)))) + rw [List.filter_append, hRnil, htailnil] + rfl + refine ⟨by simpa [E, e, lE] using htakeE, by simpa [E, B] using hbnil⟩ + +/-- If the finite candidate range contains exactly the reported keys reversed, then the +complete keys are exactly the reported keys. -/ +theorem completeKeys_eq_of_range_keys_eq_crossed_rev {db : DB} {r : RangeRead} + {res : Result} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length = (r.end_.offset - 1).toNat) + (hBR : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key)) + (hR : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : + ConflictRange).keys db = res.keys.reverse) : + RangeRead.completeKeys db r = res.keys := by + obtain ⟨htake, hbnil⟩ := take_above_eq_of_range_keys_eq hrev hbegin hcross hend hne ht hBR hR + rw [completeKeys_crossed_rev_eq (db := db) (r := r) hrev hbegin hcross hend] + rw [htake, hbnil] + simp + +/-- Soundness of the finite crossed reversed branch. -/ +theorem sound_truncatedRevCrossed_finite {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length = (r.end_.offset - 1).toNat) : + Sound r res ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ := by + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rw [hhead] + intro db₁ db₂ hagree + have hBR₁ := crossed_rev_res_ge_begin (db := db₁) (r := r) (res := res) + have hBR₂ := crossed_rev_res_ge_begin (db := db₂) (r := r) (res := res) + constructor + · intro hc₁ + have hBR := hBR₁ hc₁ hbegin + have hT₁ := crossed_rev_finite_keys_eq (db := db₁) hc₁ hmore hrev hbegin hcross + hend hne ht + have hT₂ : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : + ConflictRange).keys db₂ = res.keys.reverse := by + rw [← hagree]; exact hT₁ + have hck := completeKeys_eq_of_range_keys_eq_crossed_rev hrev hbegin hcross hend + hne ht hBR hT₂ + exact ⟨hc₁.1, Or.inl ⟨hmore, by rw [hck], hne⟩⟩ + · intro hc₂ + have hBR := hBR₂ hc₂ hbegin + have hT₂ := crossed_rev_finite_keys_eq (db := db₂) hc₂ hmore hrev hbegin hcross + hend hne ht + have hT₁ : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : + ConflictRange).keys db₁ = res.keys.reverse := by + rw [hagree]; exact hT₂ + have hck := completeKeys_eq_of_range_keys_eq_crossed_rev hrev hbegin hcross hend + hne ht hBR hT₁ + exact ⟨hc₂.1, Or.inl ⟨hmore, by rw [hck], hne⟩⟩ + +/-- Soundness of the top crossed reversed branch: agreement on `[end_.key, ⊤)` fixes +the complete keys. -/ +theorem sound_truncatedRevCrossed_top {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + Sound r res ⟨r.end_.key, ⊤⟩ := by + intro db₁ db₂ hagree + have habove : above db₁ r.end_.key = above db₂ r.end_.key := by + have h := hagree + rw [AgreeOn] at h + simpa [ConflictRange.keys, above] using h + have hck := completeKeys_eq_of_agree_above_crossed_rev (db₁ := db₁) (db₂ := db₂) + hrev hbegin hcross hend habove + constructor + · intro hc + simpa [RangeRead.Compatible, hck] using hc + · intro hc + simpa [RangeRead.Compatible, hck] using hc + + + +/-- In the finite reversed-truncated branch, inserting an unstored key `k` with +`end_.key ≤ k < last reported` makes the end selector resolve onto the reported head. +The head then leaves the complete keys, so the truncated result can no longer be +compatible. -/ +theorem crossed_rev_insert_between_incompatible {db : DB} {r : RangeRead} {res : Result} + {k : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + (hk0 : k ∉ db.keys) + (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + ¬ RangeRead.Compatible (db.insert k hk0) r res := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + let L : Key := res.keys.getLast hne + let H : Key := res.keys.head hne + let E : WithTop Key := r.end_.key + let e : Nat := (r.end_.offset - 1).toNat + let P : Key → Bool := fun k => decide (E ≤ (k : WithTop Key) ∧ + (k : WithTop Key) ≤ ((H : Key) : WithTop Key)) + let Q : Key → Bool := fun k => decide (E ≤ (k : WithTop Key)) + let BT : Key → Bool := fun k => decide (E ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter H : Key) : WithTop Key)) + have htlen : res.keys.length = (r.end_.offset - 1).toNat := by + have hcount := crossed_rev_count_ge_eq_length (r := r) (res := res) hrev hbegin + hcross (by omega) ⟨db, hcomp⟩ + rw [← hcount] + exact ht + have hT : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : + ConflictRange).keys db = res.keys.reverse := + crossed_rev_finite_keys_eq (db := db) hcomp hmore hrev hbegin hcross (by omega) hne htlen + have hT' : db.keys.filter BT = res.keys.reverse := by + simpa only [BT, L, H, E, ConflictRange.keys] using hT + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLH : L ≤ H := by + dsimp only [L, H] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hepos : 0 < e := by dsimp only [e]; omega + -- `H ≥ E`, from a reported key at or above `E`. + have htpos : 0 < res.keys.countP Q := by + dsimp only [Q] + rw [ht] + exact hepos + obtain ⟨k₀, hk₀mem, hk₀Q⟩ := List.countP_pos_iff.mp htpos + have hk₀E : E ≤ (k₀ : WithTop Key) := by + dsimp only [Q] at hk₀Q + exact of_decide_eq_true hk₀Q + have hk₀H : k₀ ≤ H := by + dsimp only [H] + exact (hsort.pairwise.imp le_of_lt).rel_head hk₀mem + have hHE : E ≤ ((H : Key) : WithTop Key) := le_trans hk₀E (WithTop.coe_le_coe.mpr hk₀H) + have hHmemC : H ∈ res.keys.reverse := by + dsimp only [H] + exact List.mem_reverse.mpr (List.head_mem hne) + have hHdb : H ∈ db.keys := by + have h := hHmemC + rw [← hT'] at h + exact List.mem_of_mem_filter h + -- The number of stored keys in `[E, H]` is `e`. + have hcountEH : db.keys.countP P = e := by + have h1 : db.keys.countP P = db.keys.countP (fun k => Q k && BT k) := by + apply List.countP_congr + intro k hk + dsimp only [P, Q, BT] + simp only [decide_eq_true_eq, Bool.and_eq_true] + constructor + · rintro ⟨hEk, hkH⟩ + refine ⟨hEk, hEk, ?_⟩ + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k H).mpr (WithTop.coe_le_coe.mp hkH)) + · rintro ⟨hEk, -, hkH⟩ + exact ⟨hEk, WithTop.coe_le_coe.mpr + ((lt_keyAfter_iff k H).mp (WithTop.coe_lt_coe.mp hkH))⟩ + have h2 : db.keys.countP (fun k => Q k && BT k) = e := by + rw [← List.countP_filter] + rw [hT', List.Perm.countP_eq _ (List.reverse_perm res.keys)] + dsimp only [Q, e] + exact ht + rw [h1, h2] + -- Split at `E` and `H` to compute the index of `H`. + have hEne : E ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hHE + exact absurd hHE (not_le.mpr (WithTop.coe_lt_top H)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hEne + have hvH : v ≤ H := by + rw [← hv] at hHE + exact WithTop.coe_le_coe.mp hHE + have hcountP_split : db.keys.countP P = + (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length + 1 := by + rw [List.countP_eq_length_filter] + have hlist : db.keys.filter P = + (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))) ++ [H] := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · apply List.sortedLT_append.mpr + refine ⟨List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _), + sortedLT_singleton H, ?_⟩ + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + exact (of_decide_eq_true (List.mem_filter.mp ha).2).2 + · intro x + dsimp only [P] + rw [← hv] + simp only [List.mem_filter, List.mem_append, List.mem_singleton, + decide_eq_true_eq, WithTop.coe_le_coe] + constructor + · rintro ⟨hx, hvx, hxH⟩ + by_cases hxH' : x = H + · exact Or.inr hxH' + · exact Or.inl ⟨hx, hvx, lt_of_le_of_ne hxH hxH'⟩ + · rintro (⟨hx, hvx, hxH⟩ | rfl) + · exact ⟨hx, hvx, le_of_lt hxH⟩ + · exact ⟨hHdb, hvH, le_refl H⟩ + rw [hlist, List.length_append, List.length_singleton] + have hfilter_len : (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length = e - 1 := by + omega + have hsplit := countP_lt_eq_add_filter db.keys db.nodup hvH + have hcE : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) = + db.keys.countP (fun k : Key => decide (k < v)) := by + rw [← hv] + exact countP_lt_coe v + have hcH : db.keys.countP (fun k : Key => decide (k < H)) = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by + have h := hsplit + rw [← hcE, hfilter_len] at h + omega + obtain ⟨j, hjlt, hj⟩ := List.mem_iff_getElem.mp hHdb + have hcj : db.keys.countP (fun k : Key => decide (k < H)) = j := + countP_lt_eq_of_getElem db.sorted hjlt hj + have hj_eq : j = db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by + rw [← hcj] + exact hcH + have hEanchor : (r.end_.anchor db).toNat = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega)] + -- Inserting `k` at or above `E` does not change the count below `E`, so the + -- anchor still targets the successor of `H`; the new key lies below `H`, which + -- shifts `H` into that target index. + have hinskeys : (db.insert k hk0).keys = + db.keys.insertIdx (db.keys.countP (fun x => decide (x < k))) k := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hk0] + have hcountnew : (db.insert k hk0).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [hinskeys] + exact countP_insertIdx _ _ _ (by + rw [decide_eq_false_iff_not] + exact not_lt.mpr hEk) + have hanchornew : (r.end_.anchor (db.insert k hk0)).toNat = + db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega), hcountnew] + have hJeq : (r.end_.anchor (db.insert k hk0)).toNat = j + 1 := by + rw [hanchornew] + have hb : db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e = + (db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e - 1) + 1 := by + omega + rw [hb, ← hj_eq] + have hpj : db.keys.countP (fun x : Key => decide (x < k)) ≤ j := by + rw [← hcj] + apply countP_le_countP_of_imp + intro x hx hlt + rw [decide_eq_true_eq] at hlt ⊢ + have hkH : k < H := lt_of_lt_of_le (WithTop.coe_lt_coe.mp hkL) hLH + exact lt_trans hlt hkH + have hnewlen : (r.end_.anchor (db.insert k hk0)).toNat < + (db.insert k hk0).keys.length := by + rw [hinskeys, hJeq, List.length_insertIdx_of_le_length List.countP_le_length] + omega + have hnewget? : (db.insert k hk0).keys[(r.end_.anchor (db.insert k hk0)).toNat]? = + some H := by + rw [hinskeys, hJeq, List.getElem?_insertIdx] + have hlt : ¬ j + 1 < db.keys.countP (fun x => decide (x < k)) := by omega + have heq : ¬ j + 1 = db.keys.countP (fun x => decide (x < k)) := by omega + rw [if_neg hlt, if_neg heq, Nat.add_sub_cancel, List.getElem?_eq_getElem hjlt, hj] + obtain ⟨_, hnewget⟩ := List.getElem?_eq_some_iff.mp hnewget? + have hHresolve : resolve (db.insert k hk0) r.end_ = ((H : Key) : WithTop Key) := by + rw [resolve_eq_getElem (anchor_nonneg_of_one_le (by omega)) hnewlen] + exact congrArg (fun x : Key => (x : WithTop Key)) hnewget + intro hcompNew + have hpre := RangeRead.compatible_keys_prefix hcompNew + have hHmem : H ∈ res.keys := by + dsimp only [H] + exact List.head_mem hne + have hHcomp : H ∈ RangeRead.completeKeys (db.insert k hk0) r := hpre.subset hHmem + have hlt := (RangeRead.mem_completeKeys.mp hHcomp).2.2 + rw [hHresolve] at hlt + exact (lt_irrefl ((H : Key) : WithTop Key)) hlt + +/-- In the finite crossed reversed branch no compatible database stores a key in +`[end_.key, last reported)`: the candidate range `[end_.key, keyAfter head)` contains +exactly the reported keys reversed, so any stored key below the reported last would be +one of those reported keys. -/ +theorem crossed_rev_no_key_between {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + {k : Key} (hkdb : k ∈ db.keys) + (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : k < res.keys.getLast hne) : False := by + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLle : ∀ x ∈ res.keys, res.keys.getLast hne ≤ x := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hLH : res.keys.getLast hne ≤ res.keys.head hne := + hLle _ (List.head_mem hne) + have hkH : k < res.keys.head hne := lt_of_lt_of_le hkL hLH + have hklt : (k : WithTop Key) < + ((keyAfter (res.keys.head hne) : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr (lt_trans hkH (keyAfter_self_lt _)) + have htlen : res.keys.length = (r.end_.offset - 1).toNat := by + have hcount := crossed_rev_count_ge_eq_length (r := r) (res := res) hrev hbegin + hcross (by omega) ⟨db, hcomp⟩ + rw [← hcount] + exact ht + have hT := crossed_rev_finite_keys_eq (db := db) hcomp hmore hrev hbegin hcross (by omega) hne htlen + have hmem : k ∈ (⟨r.end_.key, + (keyAfter (res.keys.head hne) : WithTop Key)⟩ : ConflictRange).keys db := by + rw [ConflictRange.mem_keys_iff] + exact ⟨hkdb, hEk, hklt⟩ + rw [hT] at hmem + have hkres : k ∈ res.keys := List.mem_reverse.mp hmem + exact absurd (hLle k hkres) (not_le.mpr hkL) + +/-- In the finite crossed reversed branch, every key in `[end_.key, last reported)` is +necessary. -/ +theorem necessary_finite_below_last_crossed_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + {k : Key} (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : ((k : Key) : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hk0 : k ∉ db₀.keys := by + intro hkdb + exact crossed_rev_no_key_between (db := db₀) hcomp₀ hmore hrev hbegin hcross + hend hne ht hkdb hEk (WithTop.coe_lt_coe.mp hkL) + have hagree : AgreeOn cr db₀ (db₀.insert k hk0) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hk0 hkcr).symm + have hcompNew : RangeRead.Compatible (db₀.insert k hk0) r res := + (hs db₀ _ hagree).mp hcomp₀ + exact crossed_rev_insert_between_incompatible (db := db₀) hcomp₀ hmore hrev hbegin + hcross hend hne ht hk0 hEk hkL hcompNew + +/-- Length partition of `above E` at `L`. -/ +theorem length_above_eq_between_add_above {db : DB} {E L : WithTop Key} + (hEL : E ≤ L) : + (above db E).length = (between db E L).length + (above db L).length := by + have h1 := cnt_eq_add_between db hEL + have h2 := cnt_add_above_length db E + have h3 := cnt_add_above_length db L + omega + +/-- Under the room condition, the complete crossed reversed window is all keys at or +above `begin.key`, reversed. -/ +theorem completeKeys_eq_above_begin_rev_of_room {db : DB} {r : RangeRead} + {res : Result} {L : Key} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hBL : r.begin.key ≤ (L : WithTop Key)) + (habove : above db (L : WithTop Key) = res.keys.reverse) + (hroom : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length < + (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = (above db r.begin.key).reverse := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + have hEB : E ≤ B := le_of_lt hcross + have hEL : E ≤ (L : WithTop Key) := le_trans hEB hBL + have hbet : between db E (L : WithTop Key) = + db.keys.filter (fun k : Key => decide (E ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (L : WithTop Key))) := rfl + have hlenE : (above db E).length = (between db E (L : WithTop Key)).length + + res.keys.length := by + rw [length_above_eq_between_add_above hEL, habove, List.length_reverse] + have hroom' : (between db E (L : WithTop Key)).length + res.keys.length < e := by + rw [hbet]; exact hroom + have htake : (above db E).take e = above db E := by + rw [List.take_eq_self_iff] + omega + rw [completeKeys_crossed_rev_eq (db := db) (r := r) hrev hbegin hcross hend, htake, + ← above_eq_drop_between hEB] + +/-- The filter at or above `L` of the complete crossed reversed window is `res`. -/ +theorem completeKeys_filter_ge_crossed_rev_of_room {db : DB} {r : RangeRead} + {res : Result} {L : Key} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hBL : r.begin.key ≤ (L : WithTop Key)) + (habove : above db (L : WithTop Key) = res.keys.reverse) + (hroom : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length < + (r.end_.offset - 1).toNat) : + (RangeRead.completeKeys db r).filter (fun k : Key => decide (L ≤ k)) = res.keys := by + rw [completeKeys_eq_above_begin_rev_of_room hrev hbegin hcross hend hBL habove hroom, + List.filter_reverse] + have hfilter : (above db r.begin.key).filter (fun k : Key => decide (L ≤ k)) = + above db (L : WithTop Key) := by + rw [above_eq_filter, above_eq_filter, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hL : L ≤ k + · have hB : r.begin.key ≤ (k : WithTop Key) := le_trans hBL (WithTop.coe_le_coe.mpr hL) + simp [hL, hB] + · simp [hL] + rw [hfilter, habove, List.reverse_reverse] + + + +/-- The complete keys of a crossed reversed read as a reversed prefix of the keys at +or above `begin.key`. -/ +theorem completeKeys_crossed_rev_eq_take_above_begin {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = + ((above db r.begin.key).take + (min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length - + cnt db r.begin.key)).reverse := by + set B : WithTop Key := r.begin.key with hB + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + rw [completeKeys_rev_eq_reverse db r hrev] + rw [completeKeys_eq_filter_take_of_offset_one (db := db) + (r := { r with reverse := false }) (by simp) hbegin hend] + have hfilB : db.keys.filter (fun k : Key => + decide (({ r with reverse := false } : RangeRead).begin.key ≤ + (k : WithTop Key))) = above db B := rfl + rw [hfilB] + rfl + +private theorem length_filter_lt_eq_sub {l : List Key} {L : Key} : + (l.filter (fun k : Key => !decide (L ≤ k))).length = + l.length - (l.filter (fun k : Key => decide (L ≤ k))).length := by + have h : (l.filter (fun k : Key => decide (L ≤ k))).length + + (l.filter (fun k : Key => !decide (L ≤ k))).length = l.length := by + have hp := List.filter_append_perm (fun k : Key => decide (L ≤ k)) l + have := hp.length_eq + simpa only [List.length_append] using this + omega + +/-- A compatible crossed reversed result with the room condition has its keys at or +above the reported last exactly the reported keys, reversed. -/ +theorem above_last_eq_reverse_of_compatible_crossed_rev {db : DB} {r : RangeRead} + {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + {L : Key} (hL : L = res.keys.getLast hne) + (hroom : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length < + (r.end_.offset - 1).toNat) : + above db (L : WithTop Key) = res.keys.reverse := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set P : List Key := above db B with hPdef + set g : Nat := (between db E B).length with hgdef + set h : Nat := (between db E (L : WithTop Key)).length with hhdef + set t : Nat := res.keys.length with htdef + set m : Nat := min (cnt db E + e) db.keys.length - cnt db B with hmdef + set b : Nat := ((P.take m).filter (fun k : Key => decide (k < L))).length with hbdef + have hEB : E ≤ B := le_of_lt hcross + have hBL : B ≤ (L : WithTop Key) := by + have := crossed_rev_res_ge_begin hcomp hbegin L (by rw [hL]; exact List.getLast_mem hne) + simpa only [B] using this + have hW : RangeRead.completeKeys db r = (P.take m).reverse := by + rw [completeKeys_crossed_rev_eq_take_above_begin (db := db) (r := r) hrev hbegin + hcross hend] + have hWf := ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp).2 + have hPf : (P.take m).filter (fun k : Key => decide (L ≤ k)) = res.keys.reverse := by + have h := hWf + rw [hW, List.filter_reverse, ← hL] at h + exact List.reverse_injective (by simpa only [List.reverse_reverse] using h) + have hcalc : m = min e (above db E).length - g := by + rw [hmdef, ← cnt_add_above_length db E, cnt_eq_add_between db hEB, + Nat.add_min_add_left, Nat.add_sub_add_left] + have hlenE : (above db E).length = g + P.length := by + have h1 := cnt_add_above_length db E + have h2 := cnt_add_above_length db B + have h3 := cnt_eq_add_between db hEB + dsimp only [g, P] + omega + have hm : m = min (e - g) P.length := by + rw [hcalc, hlenE, ← Nat.sub_min_sub_right e (g + P.length) g, Nat.add_sub_cancel_left] + have hm_le : m ≤ P.length := by rw [hm]; exact Nat.min_le_right _ _ + have htake_len : (P.take m).length = m := by + rw [List.length_take, Nat.min_eq_left hm_le] + have hbt : m = t + b := by + have hp := List.filter_append_perm (fun k : Key => decide (L ≤ k)) (P.take m) + have hlen := hp.length_eq + rw [List.length_append] at hlen + have h1 : (P.take m).filter (fun k : Key => !decide (L ≤ k)) = + (P.take m).filter (fun k : Key => decide (k < L)) := by + apply List.filter_congr + intro k _ + by_cases h : L ≤ k + · have hlt : ¬ k < L := not_lt.mpr h + simp [h, hlt] + · have hlt : k < L := not_le.mp h + simp [h, hlt] + rw [hPf, List.length_reverse, ← htdef, h1, ← hbdef, htake_len] at hlen + omega + have hgj : h = g + (between db B (L : WithTop Key)).length := by + have h1 := cnt_eq_add_between (db := db) hEB + have h2 := cnt_eq_add_between (db := db) hBL + have h3 := cnt_eq_add_between (db := db) (le_trans hEB hBL) + unfold h g + omega + have hgj' : h = (cnt db B - cnt db E) + (between db B (L : WithTop Key)).length := by + have hc := cnt_sub_eq_between_length (db := db) hEB + omega + have hb_le : b ≤ (between db B (L : WithTop Key)).length := by + have hsub : (P.take m).Sublist db.keys := by + rw [hPdef] + exact (List.take_sublist m (above db B)).trans List.filter_sublist + have hall : ∀ k ∈ P.take m, B ≤ (k : WithTop Key) := by + intro k hk + have hkP : k ∈ P := List.mem_of_mem_take hk + rw [hPdef] at hkP + exact of_decide_eq_true (List.mem_filter.mp hkP).2 + have hb_eq : ((P.take m).filter (fun k : Key => decide (k < L))).length = + ((P.take m).filter (fun k : Key => decide (B ≤ (k : WithTop Key) ∧ + (k : Key) < L))).length := by + congr 1 + apply List.filter_congr + intro k hk + rw [decide_eq_decide] + exact ⟨fun hkL => ⟨hall k hk, hkL⟩, fun hk => hk.2⟩ + rw [hbdef, hb_eq] + have hb2 := (List.Sublist.filter (fun k : Key => decide (B ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (L : WithTop Key))) hsub).length_le + simpa only [between, WithTop.coe_lt_coe] using hb2 + by_cases hPlen : P.length ≤ m + · have htakeP : P.take m = P := List.take_of_length_le hPlen + rw [htakeP] at hPf + have hfilter : P.filter (fun k : Key => decide (L ≤ k)) = above db (L : WithTop Key) := by + rw [hPdef, above_eq_filter, above_eq_filter, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hLk : L ≤ k + · have hBk : B ≤ (k : WithTop Key) := le_trans hBL (WithTop.coe_le_coe.mpr hLk) + simp [hLk, hBk] + · simp [hLk] + rwa [hfilter] at hPf + · have hPlt : m < P.length := lt_of_not_ge hPlen + have hle : e - g ≤ P.length := by + by_contra hcon + have hmin : min (e - g) P.length = P.length := Nat.min_eq_right (le_of_not_ge hcon) + rw [hm, hmin] at hPlt + omega + have hmE : m = e - g := by rw [hm, Nat.min_eq_left hle] + have hb_val : b = e - g - t := by omega + have hroom' : h + t < e := by simpa only [h, between] using hroom + have hhle : h ≤ e - t - 1 := by omega + have hble : b ≤ (between db B (L : WithTop Key)).length := hb_le + have hgf : g ≤ e - t - 1 := by omega + have hdf : (between db B (L : WithTop Key)).length ≤ e - t - 1 - g := by omega + have hb_f : b = e - t - 1 - g + 1 := by omega + omega + + + +/-- **Soundness of the crossed reversed truncated top branch starting at the reported +last key.** When the successor chain from `end_.key` reaches the reported last, the +room between `end_.key` and the last fits inside the end overshoot, so agreement on +`[last, ⊤)` determines all keys above `begin_.key` inside the complete window. -/ +theorem sound_truncatedRevCrossed_top_last {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + {E L : Key} {n : Nat} + (hE : r.end_.key = (E : WithTop Key)) (hL : L = res.keys.getLast hne) + (hn : n = (r.end_.offset - 1).toNat - res.keys.length - 1) + (hno : L ≤ keyAfterN n E) : + Sound r res ⟨(L : WithTop Key), ⊤⟩ := by + intro db₁ db₂ hagree + have hno' : res.keys.getLast hne ≤ keyAfterN n E := by rw [← hL]; exact hno + have step : ∀ db db', AgreeOn ⟨(L : WithTop Key), ⊤⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hBL : r.begin.key ≤ (L : WithTop Key) := by + have h := crossed_rev_res_ge_begin hcomp hbegin L + (by rw [hL]; exact List.getLast_mem hne) + simpa only [hL] using h + have hroom_of : ∀ (d : DB), + (d.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length < + (r.end_.offset - 1).toNat := by + intro d + have hle := length_filter_between_le_of_le_keyAfterN d.nodup + (E := E) (L := res.keys.getLast hne) (n := n) hno' + have hEq : (d.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (L : WithTop Key)))) = + (d.keys.filter (fun k : Key => decide (E ≤ k ∧ k < res.keys.getLast hne))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + rw [hE, hL] + simp + rw [hEq] + have hElt : E < res.keys.getLast hne := by + have h1 : (E : WithTop Key) < (L : WithTop Key) := by + rw [← hE] + exact lt_of_lt_of_le hcross hBL + exact WithTop.coe_lt_coe.mp (by simpa only [hL] using h1) + have hnpos : 0 < n := by + by_contra hcon + have hn0 : n = 0 := Nat.eq_zero_of_not_pos hcon + rw [hn0, keyAfterN_zero] at hno' + exact absurd hno' (not_le.mpr hElt) + have he : res.keys.length + 1 < (r.end_.offset - 1).toNat := by omega + omega + have hroom_db : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < (L : WithTop Key)))).length + + res.keys.length < (r.end_.offset - 1).toNat := hroom_of db + have hroom_db' : (db'.keys.filter (fun k : Key => decide (r.end_.key ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < (L : WithTop Key)))).length + + res.keys.length < (r.end_.offset - 1).toNat := hroom_of db' + have habove_db := above_last_eq_reverse_of_compatible_crossed_rev hcomp hmore hrev + hbegin hcross hend hne hL hroom_db + have hagmem : ∀ k : Key, (L : WithTop Key) ≤ (k : WithTop Key) → + (k ∈ db.keys ↔ k ∈ db'.keys) := by + intro k hk + exact (agreeOn_iff ⟨(L : WithTop Key), ⊤⟩ db db').mp hag' k + (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) + have habove_eq : above db (L : WithTop Key) = above db' (L : WithTop Key) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db'.sorted.pairwise.filter _) + · intro k + rw [mem_above, mem_above] + constructor + · rintro ⟨hk, hLk⟩ + exact ⟨(hagmem k hLk).mp hk, hLk⟩ + · rintro ⟨hk, hLk⟩ + exact ⟨(hagmem k hLk).mpr hk, hLk⟩ + have habove_db' : above db' (L : WithTop Key) = res.keys.reverse := by + rw [← habove_eq, habove_db] + have hPf := completeKeys_filter_ge_crossed_rev_of_room (db := db') hrev hbegin + hcross hend hBL habove_db' hroom_db' + exact (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr + ⟨hcomp.1, by simpa only [hL] using hPf⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + + + +theorem crossed_rev_pad_top_necessary {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + {E : Key} (hE : r.end_.key = (E : WithTop Key)) + (hEL : (E : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) + {n : Nat} + (hlt : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hroom : keyAfterN n E < res.keys.getLast hne) + {k : Key} (hEk : E ≤ k) (hkL : k < res.keys.getLast hne) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + set e : Nat := (r.end_.offset - 1).toNat with hedef + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx + rw [hLdef] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hHge : ∀ x ∈ res.keys, x ≤ H := by + intro x hx + rw [hHdef] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hLH : L ≤ H := hLle H (by rw [hHdef]; exact List.head_mem hne) + have hkL' : k < L := by rw [hLdef]; exact hkL + have hk_notin_res : k ∉ res.keys := by + intro hx + exact absurd (hLle k hx) (not_le.mpr hkL') + have hk_notin_rev : k ∉ res.keys.reverse := by + rw [List.mem_reverse]; exact hk_notin_res + have htlen : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq, hE] + exact le_trans (le_of_lt hEL) (WithTop.coe_le_coe.mpr (hLle x hx)) + have hn_e : e = n + res.keys.length + 1 := by + rw [hedef] + omega + -- the successor chain below `L` + set S : List Key := (List.range (n + 1)).map (chain E) with hSdef + have hS_len : S.length = n + 1 := by + rw [hSdef, List.length_map, List.length_range] + have hS_sorted : S.SortedLT := by + rw [hSdef]; exact sortedLT_map_chain E (n + 1) + have hS_nodup : S.Nodup := hS_sorted.pairwise.nodup + have hS_mem : ∀ x, x ∈ S ↔ ∃ i, i < n + 1 ∧ chain E i = x := by + intro x + rw [hSdef, List.mem_map] + constructor + · rintro ⟨i, hi, hix⟩; exact ⟨i, List.mem_range.mp hi, hix⟩ + · rintro ⟨i, hi, hix⟩; exact ⟨i, List.mem_range.mpr hi, hix⟩ + have hS_le : ∀ x ∈ S, x ≤ chain E n := by + intro x hx + obtain ⟨i, hi, rfl⟩ := (hS_mem x).mp hx + rcases lt_or_eq_of_le (by omega : i ≤ n) with h | h + · exact le_of_lt (chain_lt_mono h) + · rw [h] + have hS_lt_L : ∀ x ∈ S, x < L := by + intro x hx + have h := hS_le x hx + rw [chain_eq_keyAfterN E n] at h + exact lt_of_le_of_lt h hroom + have hS_ge_E : ∀ x ∈ S, E ≤ x := by + intro x hx + obtain ⟨i, hi, rfl⟩ := (hS_mem x).mp hx + exact chain_le E i + set P : List Key := (S.erase k).take n with hPdef + have hP_len : P.length = n := by + rw [hPdef, List.length_take] + have hge : n ≤ (S.erase k).length := by + by_cases hkS : k ∈ S + · rw [List.length_erase_of_mem hkS, hS_len]; omega + · rw [List.erase_of_not_mem hkS, hS_len]; omega + omega + have hP_sorted : P.SortedLT := by + rw [hPdef] + exact List.sortedLT_iff_pairwise.mpr + (List.Pairwise.take (i := n) (List.Pairwise.erase k (List.sortedLT_iff_pairwise.mp hS_sorted))) + have hP_sub : P ⊆ S := by + intro x hx + rw [hPdef] at hx + exact List.erase_subset ((List.take_sublist n (S.erase k)).subset hx) + have hP_lt_L : ∀ x ∈ P, x < L := fun x hx => hS_lt_L x (hP_sub hx) + have hP_ge_E : ∀ x ∈ P, E ≤ x := fun x hx => hS_ge_E x (hP_sub hx) + have hk_notin_P : k ∉ P := by + intro hx + have hx' : k ∈ S.erase k := (List.take_sublist n (S.erase k)).subset hx + exact (hS_nodup.mem_erase_iff.mp hx').1 rfl + set u : Key := keyAfter H with hudef + have hHlt_u : H < u := by rw [hudef]; exact keyAfter_self_lt H + have hres_lt_u : ∀ x ∈ res.keys.reverse, x < u := by + intro x hx + exact lt_of_le_of_lt (hHge x (List.mem_reverse.mp hx)) hHlt_u + have hk_notin_a : k ∉ P ++ res.keys.reverse ++ [u] := by + intro hx + rw [List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact hk_notin_P hx + · exact hk_notin_rev hx + · rw [List.mem_singleton] at hx + exact (ne_of_lt (lt_trans (lt_of_lt_of_le hkL' hLH) hHlt_u)) hx + set aList : List Key := P ++ res.keys.reverse ++ [u] with hadef + have hres_rev_sorted : res.keys.reverse.SortedLT := hsort.reverse + have hP_lt_res : ∀ x ∈ P, ∀ y ∈ res.keys.reverse, x < y := by + intro x hx y hy + exact lt_of_lt_of_le (hP_lt_L x hx) (hLle y (List.mem_reverse.mp hy)) + have ha_sorted : aList.SortedLT := by + rw [hadef] + refine List.sortedLT_append.mpr ⟨?_, ?_, ?_⟩ + · exact List.sortedLT_append.mpr ⟨hP_sorted, hres_rev_sorted, hP_lt_res⟩ + · exact List.sortedLT_iff_pairwise.mpr (by simp) + · intro x hx y hy + rw [List.mem_singleton] at hy + rw [hy] + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact lt_of_lt_of_le (hP_lt_L x hx) (le_trans hLH (le_of_lt hHlt_u)) + · exact hres_lt_u x hx + let db_a : DB := ⟨aList, ha_sorted⟩ + have hlen_a : aList.length = e := by + rw [hadef, List.length_append, List.length_append, hP_len, + List.length_reverse, List.length_singleton] + omega + have ha_ge_E : ∀ x ∈ aList, E ≤ x := by + intro x hx + rw [hadef, List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact hP_ge_E x hx + · exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hEL)) + (hLle x (List.mem_reverse.mp hx)) + · rw [List.mem_singleton] at hx + rw [hx] + exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hEL)) + (le_trans hLH (le_of_lt hHlt_u)) + have hcount_a : aList.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx hlt + rw [decide_eq_true_eq] at hlt + rw [hE] at hlt + exact absurd (WithTop.coe_le_coe.mpr (ha_ge_E x hx)) (not_le.mpr hlt) + have hanc_a : (r.end_.anchor db_a).toNat = e := by + rw [anchor_toNat_eq (db := db_a) (s := r.end_) (by omega), hcount_a] + omega + have hresend_a : resolve db_a r.end_ = (⊤ : WithTop Key) := + resolve_eq_top (anchor_nonneg_of_one_le (db := db_a) (s := r.end_) (by omega)) + (by rw [hlen_a, hanc_a]) + have hbegin_le_L : r.begin.key ≤ (↑L : WithTop Key) := by + rw [hLdef] + exact crossed_rev_res_ge_begin hcomp₀ hbegin L + (by rw [hLdef]; exact List.getLast_mem hne) + have hL_mem_a : L ∈ db_a.keys := by + change L ∈ aList + rw [hadef] + simp only [List.mem_append, List.mem_singleton] + exact Or.inl (Or.inr (List.mem_reverse.mpr + (by rw [hLdef]; exact List.getLast_mem hne))) + have hrb_a : resolve db_a r.begin ≤ (↑L : WithTop Key) := + (key_le_resolve_iff_of_offset_one (db := db_a) hbegin hL_mem_a).mp hbegin_le_L + have hck_a : RangeRead.completeKeys db_a r = + (RangeRead.inRange db_a.keys (resolve db_a r.begin) (⊤ : WithTop Key)).reverse := by + simp [RangeRead.completeKeys, hrev, hresend_a] + have hfilter_a : aList.filter (fun a : Key => decide (L ≤ a)) = + res.keys.reverse ++ [u] := by + rw [hadef, List.filter_append, List.filter_append] + have hPnil : P.filter (fun a : Key => decide (L ≤ a)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_le.mpr (hP_lt_L x hx) + have hRself : res.keys.reverse.filter (fun a : Key => decide (L ≤ a)) = + res.keys.reverse := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hLle x (List.mem_reverse.mp hx) + have hU : [u].filter (fun a : Key => decide (L ≤ a)) = [u] := by + simp only [List.filter_cons, List.filter_nil] + rw [decide_eq_true (le_of_lt (lt_of_le_of_lt hLH hHlt_u))] + simp + rw [hPnil, hRself, hU] + simp + have hnotcomp_a : ¬ RangeRead.Compatible db_a r res := by + intro hc + have hf := ((compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mp hc).2 + rw [hck_a, List.filter_reverse] at hf + unfold RangeRead.inRange at hf + rw [List.filter_filter] at hf + have hpred : ∀ a ∈ db_a.keys, + (decide (L ≤ a) && decide (resolve db_a r.begin ≤ (↑a : WithTop Key) ∧ + (↑a : WithTop Key) < (⊤ : WithTop Key))) = decide (L ≤ a) := by + intro a _ + by_cases hLa : L ≤ a + · have hres : resolve db_a r.begin ≤ (↑a : WithTop Key) := + le_trans hrb_a (WithTop.coe_le_coe.mpr hLa) + rw [decide_eq_true hLa, Bool.true_and, decide_eq_true] + exact ⟨hres, WithTop.coe_lt_top a⟩ + · rw [decide_eq_false hLa, Bool.false_and] + rw [List.filter_congr hpred] at hf + have hda : db_a.keys = aList := rfl + rw [hda, hfilter_a, List.reverse_append, List.reverse_singleton, + List.reverse_reverse, List.singleton_append] at hf + have hlen := congrArg List.length hf + simp only [List.length_cons] at hlen + omega + -- insert the target key + let db_b : DB := db_a.insert k hk_notin_a + have hb_keys : db_b.keys = aList.orderedInsert (· ≤ ·) k := rfl + have hb_mem : ∀ x, x ∈ db_b.keys ↔ x = k ∨ x ∈ aList := by + intro x + rw [hb_keys, List.mem_orderedInsert] + have hb_len : db_b.keys.length = e + 1 := by + rw [hb_keys] + have hp := List.Perm.length_eq (List.perm_orderedInsert (· ≤ ·) k aList) + simp only [List.length_cons] at hp + rw [hp, hlen_a] + have hb_ge_E : ∀ x ∈ db_b.keys, E ≤ x := by + intro x hx + rcases (hb_mem x).mp hx with rfl | hx + · exact hEk + · exact ha_ge_E x hx + have hcount_b : db_b.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx hlt + rw [decide_eq_true_eq] at hlt + rw [hE] at hlt + exact absurd (WithTop.coe_le_coe.mpr (hb_ge_E x hx)) (not_le.mpr hlt) + have hanc_b : (r.end_.anchor db_b).toNat = e := by + rw [anchor_toNat_eq (db := db_b) (s := r.end_) (by omega), hcount_b] + omega + have hresend_b : resolve db_b r.end_ = (↑u : WithTop Key) := by + have h0 := anchor_nonneg_of_one_le (db := db_b) (s := r.end_) (by omega) + have hidx : (r.end_.anchor db_b).toNat < db_b.keys.length := by + rw [hanc_b, hb_len]; omega + have hu_mem : u ∈ db_b.keys := by + rw [hb_mem]; exact Or.inr (by rw [hadef]; simp) + have hall_le_u : ∀ x ∈ db_b.keys, x ≤ u := by + intro x hx + rcases (hb_mem x).mp hx with rfl | hx + · exact le_of_lt (lt_trans (lt_of_lt_of_le hkL' hLH) hHlt_u) + · rw [hadef] at hx + rw [List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact le_of_lt (lt_of_lt_of_le (hP_lt_L x hx) + (le_trans hLH (le_of_lt hHlt_u))) + · exact le_of_lt (hres_lt_u x hx) + · rw [List.mem_singleton] at hx + rw [hx] + have hbne : db_b.keys ≠ [] := List.ne_nil_of_length_pos (by rw [hb_len]; omega) + have hlast : db_b.keys.getLast hbne = u := by + apply le_antisymm + · exact hall_le_u _ (List.getLast_mem hbne) + · exact (List.sortedLT_iff_pairwise.mp db_b.sorted).imp le_of_lt + |>.rel_getLast hu_mem + have hidx3 : db_b.keys.length - 1 = e := by rw [hb_len]; omega + have hgl := List.getLast_eq_getElem (l := db_b.keys) (h := hbne) + have hidx_e : e < db_b.keys.length := by rw [hb_len]; omega + have hkeyu : db_b.keys[e] = u := by + rw [List.getElem_eq_iff hidx_e, ← hidx3, + List.getElem?_eq_getElem (by omega), ← hgl, hlast] + have hkey_anchor : db_b.keys[(r.end_.anchor db_b).toNat] = u := by + rw [List.getElem_eq_iff hidx, hanc_b, List.getElem?_eq_getElem hidx_e, hkeyu] + rw [resolve_eq_getElem h0 hidx, hkey_anchor] + have hcomp_b : RangeRead.Compatible db_b r res := by + refine (compatible_iff_truncated_keys_rev (db := db_b) hrev hmore hne).mpr + ⟨hcomp₀.1, ?_⟩ + have hL_mem_res : L ∈ res.keys := by + rw [hLdef]; exact List.getLast_mem hne + have hL_mem_b : L ∈ db_b.keys := by + rw [hb_mem]; exact Or.inr (by rw [hadef]; simp [hL_mem_res]) + have hrb_b : resolve db_b r.begin ≤ (↑L : WithTop Key) := + (key_le_resolve_iff_of_offset_one (db := db_b) hbegin hL_mem_b).mp hbegin_le_L + have hck_b : RangeRead.completeKeys db_b r = + (RangeRead.inRange db_b.keys (resolve db_b r.begin) (↑u : WithTop Key)).reverse := by + simp [RangeRead.completeKeys, hrev, hresend_b] + rw [hck_b, List.filter_reverse] + unfold RangeRead.inRange + rw [List.filter_filter] + have hpred : ∀ a ∈ db_b.keys, + (decide (L ≤ a) && decide (resolve db_b r.begin ≤ (↑a : WithTop Key) ∧ + (↑a : WithTop Key) < (↑u : WithTop Key))) = decide (L ≤ a ∧ a < u) := by + intro a _ + by_cases hLa : L ≤ a + · have hres : resolve db_b r.begin ≤ (↑a : WithTop Key) := + le_trans hrb_b (WithTop.coe_le_coe.mpr hLa) + rw [decide_eq_true hLa, Bool.true_and, decide_eq_decide] + exact ⟨fun h => ⟨hLa, WithTop.coe_lt_coe.mp h.2⟩, + fun h => ⟨hres, WithTop.coe_lt_coe.mpr h.2⟩⟩ + · rw [decide_eq_false hLa] + simp [hLa] + rw [List.filter_congr hpred] + have hstep : db_b.keys.filter (fun a => decide (L ≤ a ∧ a < u)) = res.keys.reverse := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db_b.sorted.pairwise.filter _) + · exact hres_rev_sorted + · intro x + rw [List.mem_filter, List.mem_reverse] + constructor + · rintro ⟨hx, hpred⟩ + have hpred' := of_decide_eq_true hpred + rcases (hb_mem x).mp hx with rfl | hx + · exact absurd hpred'.1 (not_le.mpr hkL') + · rw [hadef] at hx + rw [List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact absurd hpred'.1 (not_le.mpr (hP_lt_L x hx)) + · exact List.mem_reverse.mp hx + · rw [List.mem_singleton] at hx + rw [hx] at hpred' + exact absurd hpred'.2 (lt_irrefl u) + · intro hx + refine ⟨?_, ?_⟩ + · rw [hb_mem]; exact Or.inr (by rw [hadef]; simp [hx]) + · exact decide_eq_true ⟨hLle x hx, hres_lt_u x (List.mem_reverse.mpr hx)⟩ + rw [hstep, List.reverse_reverse] + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db_a db_b := + (ConflictRange.keys_insert_of_not_mem cr db_a hk_notin_a hkcr).symm + exact hnotcomp_a ((hs db_a db_b hagree).mpr hcomp_b) + + + +/-- **Minimality of the crossed top branch beginning at `end_.key`.** -/ +theorem minimal_topRevCrossed_begin_end {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + {E L : Key} {n : Nat} + (hE : r.end_.key = (E : WithTop Key)) (hL : L = res.keys.getLast hne) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hroom : keyAfterN n E < L) : + Minimal r res ⟨(E : WithTop Key), ⊤⟩ := by + have hEL : (E : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [← hL] + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (keyAfterN_le_self E n) hroom) + have hroom' : keyAfterN n E < res.keys.getLast hne := by rw [← hL]; exact hroom + have hs : Sound r res ⟨(E : WithTop Key), ⊤⟩ := by + rw [← hE] + exact sound_truncatedRevCrossed_top hrev hbegin hcross (by omega) + refine (minimal_iff_sound_and_necessary).mpr ⟨hs, ?_⟩ + intro k hk + have hEk : E ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkL : k < L + · exact crossed_rev_pad_top_necessary hread hmore hrev hbegin hcross hend hne hE hEL hlt hn + hroom' hEk (by rw [← hL]; exact hkL) + · have hLk : L ≤ k := le_of_not_gt hkL + by_cases hkH : k ≤ res.keys.head hne + · exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne) + (List.head_mem hne) hLk hkH + · exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH) + +/-- **Minimality of the crossed top branch beginning at the reported last key.** -/ +theorem minimal_topRevCrossed_begin_last {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) + {E L : Key} {n : Nat} + (hE : r.end_.key = (E : WithTop Key)) + (hne : res.keys ≠ []) (hL : L = res.keys.getLast hne) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hcase : L ≤ E ∨ L ≤ keyAfterN n E) : + Minimal r res ⟨(L : WithTop Key), ⊤⟩ := by + have hcount : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := + crossed_rev_count_ge_eq_length hrev hbegin hcross (by omega) hread + have hn' : n = (r.end_.offset - 1).toNat - res.keys.length - 1 := by + rw [hn, hcount] + rcases hcase with hLE | hno + · have hLno : L ≤ keyAfterN n E := + le_trans hLE (keyAfterN_le_self E n) + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_truncatedRevCrossed_top_last hmore hrev hbegin hcross (by omega) hne hE hL hn' hLno, ?_⟩ + intro k hk + have hLk : L ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkH : k ≤ res.keys.head hne + · exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne) + (List.head_mem hne) hLk hkH + · exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH) + · refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_truncatedRevCrossed_top_last hmore hrev hbegin hcross (by omega) hne hE hL hn' hno, ?_⟩ + intro k hk + have hLk : L ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkH : k ≤ res.keys.head hne + · exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne) + (List.head_mem hne) hLk hkH + · exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH) + + + +/-- **Minimality of the crossed finite reversed truncated branch.** -/ +theorem minimal_truncatedRevCrossed_finite {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (ht : res.keys.length = (r.end_.offset - 1).toNat) : + Minimal r res ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ := by + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have ht' : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat := by + rw [crossed_rev_count_ge_eq_length hrev hbegin hcross (by omega) hread, ht] + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_truncatedRevCrossed_finite hmore hrev hbegin hcross (by omega) hne ht, ?_⟩ + intro k hk + rw [ConflictRange.mem_iff] at hk + have hkH : k ≤ res.keys.head hne := by + have h2 : (k : WithTop Key) < (keyAfter (res.keys.headD ([] : Key)) : WithTop Key) := hk.2 + rw [hhead] at h2 + exact (lt_keyAfter_iff k (res.keys.head hne)).mp (WithTop.coe_lt_coe.mp h2) + by_cases hkL : k < res.keys.getLast hne + · exact necessary_finite_below_last_crossed_rev hread hmore hrev hbegin hcross hend hne ht' + hk.1 (WithTop.coe_lt_coe.mpr hkL) + · have hLk : res.keys.getLast hne ≤ k := le_of_not_gt hkL + exact necessary_of_between_result hread (List.getLast_mem hne) (List.head_mem hne) + hLk hkH + + + +/-- **Minimality of the corrected reversed truncated rule on crossed reads.** -/ +theorem minimal_closedForm_of_offset_one_truncated_rev_fullC_crossed + {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + Minimal r res (closedFormOffsetOneTruncatedRevFullC r res) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hcount : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := + crossed_rev_count_ge_eq_length hrev hbegin hcross (by omega) hread' + have hlen_le_e : res.keys.length ≤ (r.end_.offset - 1).toNat := by + have hpre := (RangeRead.compatible_keys_prefix hcomp₀).length_le + rw [completeKeys_crossed_rev_eq hrev hbegin hcross (by omega)] at hpre + rw [List.length_reverse, List.length_drop] at hpre + have htake : (List.take (r.end_.offset - 1).toNat (above db₀ r.end_.key)).length ≤ + (r.end_.offset - 1).toNat := by + rw [List.length_take] + exact Nat.min_le_left _ _ + omega + have hcount_le : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + rw [hcount]; exact hlen_le_e + unfold closedFormOffsetOneTruncatedRevFullC + rw [if_neg (by omega : ¬ r.end_.offset ≤ 1)] + dsimp only + by_cases ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat + · rw [if_pos ht] + have htlen : res.keys.length = (r.end_.offset - 1).toNat := by + rw [← hcount]; exact ht + have hEL : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by + rw [hlast] + exact le_trans (le_of_lt hcross) + (crossed_rev_res_ge_begin hcomp₀ hbegin (res.keys.getLast hne) + (List.getLast_mem hne)) + have hmin_eq : min ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.end_.key = + r.end_.key := min_eq_right hEL + rw [hmin_eq] + exact minimal_truncatedRevCrossed_finite hread' hmore hrev hbegin hcross hend hne htlen + · rw [if_neg ht] + have hlt : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat := lt_of_le_of_ne hcount_le ht + cases hkey : r.end_.key with + | top => + rw [hkey] at hcross + exact absurd hcross (not_lt_of_ge le_top) + | coe E => + change Minimal r res (if keyAfterN ((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1) + E < res.keys.getLastD ([] : Key) then ⟨(E : WithTop Key), ⊤⟩ + else ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), ⊤⟩) + by_cases hroom : keyAfterN ((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1) + E < res.keys.getLastD ([] : Key) + · rw [if_pos hroom] + have hroom' : keyAfterN ((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1) + E < res.keys.getLast hne := by + rw [← hlast]; exact hroom + exact minimal_topRevCrossed_begin_end hread' hmore hrev hbegin hcross hend hne hkey + rfl (by rw [hkey]) hlt hroom' + · rw [if_neg hroom] + have hge : res.keys.getLastD ([] : Key) ≤ keyAfterN ((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1) + E := le_of_not_gt hroom + have hge' : res.keys.getLast hne ≤ keyAfterN ((r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1) + E := by + rw [← hlast]; exact hge + rw [hlast] + exact minimal_topRevCrossed_begin_last hread' hmore hrev hbegin hcross hend hkey + hne rfl (by rw [hkey]) hlt (Or.inr hge') + + + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedRevGeneral.lean b/FormalConflictRange/CrossedRevGeneral.lean new file mode 100644 index 0000000..3d5e8a1 --- /dev/null +++ b/FormalConflictRange/CrossedRevGeneral.lean @@ -0,0 +1,982 @@ +/- +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.CrossedRev +import FormalConflictRange.CrossedTruncatedGeneral + +/-! +# Crossed reversed truncated reads at every positive begin offset + +This module extends `CrossedRev` from begin offset one to every positive begin offset. +With `E = end_.key`, `B = begin_.key`, `e = (end_.offset - 1).toNat`, +`b = (begin.offset - 1).toNat`, and `g` the number of stored keys in `[E, B)`, the +complete window of a crossed reversed read is the reversed block +`((above db E).take e).drop (g + b)`. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- Taking `min e |l| - g` after dropping `g` is the same as dropping `g` after +taking `e`. -/ +theorem take_sub_drop_gen {α : Type*} (l : List α) (e g : Nat) : + (l.drop g).take (min e l.length - g) = (l.take e).drop g := by + rw [List.drop_take, List.take_eq_take_iff] + simp only [List.length_drop] + omega + +/-- The complete keys of a crossed reversed read at a positive begin offset are the +reversed block of the keys at or above `end_.key`, with the gap and the first +`begin.offset - 1` keys dropped. -/ +theorem completeKeys_crossed_rev_eq_general {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = + (((above db r.end_.key).take ((r.end_.offset - 1).toNat)).drop + ((between db r.end_.key r.begin.key).length + (r.begin.offset - 1).toNat)).reverse := by + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set g : Nat := (between db E B).length with hg + have hEB : E ≤ B := le_of_lt hcross + have hfwd : RangeRead.completeKeys db r = + (((above db B).drop b).take + (min (cnt db E + e) db.keys.length - (cnt db B + b))).reverse := by + rw [completeKeys_rev_eq_reverse db r hrev] + rw [completeKeys_eq_filter_ge_drop_take (db := db) + (r := { r with reverse := false }) (by simp) hbegin hend] + have hfilB : db.keys.filter (fun k : Key => + decide (({ r with reverse := false } : RangeRead).begin.key ≤ + (k : WithTop Key))) = above db B := rfl + rw [hfilB] + simp only [cnt, hE, hB, he, hb] + have hp : cnt db B = cnt db E + g := by + rw [hg, hB, hE] + simpa using cnt_eq_add_between (db := db) hEB + have hlenE : (above db E).length = db.keys.length - cnt db E := by + rw [← cnt_add_above_length db E] + omega + have hcalc : min (cnt db E + e) db.keys.length - (cnt db B + b) = + min e (above db E).length - (g + b) := by + rw [hp, hlenE] + omega + rw [hfwd, hcalc, above_eq_drop_between (db := db) hEB, List.drop_drop, + take_sub_drop_gen] + +/-- A compatible crossed reversed result has all reported keys at or above + `begin.key`, at every positive begin offset. -/ +theorem crossed_rev_res_ge_begin_general {db : DB} {r : RangeRead} {res : Result} + (hc : RangeRead.Compatible db r res) (hbegin : 1 ≤ r.begin.offset) : + ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := by + intro k hk + have hmem := (RangeRead.compatible_keys_prefix hc).subset hk + exact le_trans (key_le_resolve_of_one_le (db := db) (s := r.begin) hbegin) + (RangeRead.mem_completeKeys.mp hmem).2.1 + +/-- A compatible crossed reversed result at a positive begin offset has all reported + keys at or above `end_.key`. -/ +theorem crossed_rev_res_ge_end_general {db : DB} {r : RangeRead} {res : Result} + (hc : RangeRead.Compatible db r res) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + ∀ k ∈ res.keys, r.end_.key ≤ (k : WithTop Key) := by + intro k hk + have hge := crossed_rev_res_ge_begin_general hc hbegin k hk + exact le_trans (le_of_lt hcross) hge + +/-- In a crossed reversed result at a positive begin offset every reported key is at + or above `end_.key`, so the count at or above `end_.key` is the number of reported + keys. -/ +theorem crossed_rev_count_ge_eq_length_general {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hread : Readable r res) : + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + res.keys.length := by + obtain ⟨db, hc⟩ := hread + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact crossed_rev_res_ge_end_general hc hrev hbegin hcross hend k hk + +/-- Agreement on the keys at or above `end_.key` fixes the complete keys of a crossed + reversed read at a positive begin offset. -/ +theorem completeKeys_eq_of_agree_above_crossed_rev_general {db₁ db₂ : DB} + {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (habove : above db₁ r.end_.key = above db₂ r.end_.key) : + RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + have hB (db : DB) : above db r.begin.key = (above db E).drop (between db E r.begin.key).length := + above_eq_drop_between (le_of_lt hcross) + have hg_eq : (between db₁ E r.begin.key).length = + (between db₂ E r.begin.key).length := by + rw [between_eq_filter_above (db := db₁) E r.begin.key, + between_eq_filter_above (db := db₂) E r.begin.key, habove] + have hlen : (above db₁ E).length = (above db₂ E).length := by rw [habove] + rw [completeKeys_crossed_rev_eq_general (db := db₁) hrev hbegin hcross hend, + completeKeys_crossed_rev_eq_general (db := db₂) hrev hbegin hcross hend, + habove, hg_eq] + +/-- Soundness of the unbounded top crossed reversed branch at a positive begin offset: + agreement on `[end_.key, ⊤)` fixes the complete keys. -/ +theorem sound_truncatedRevCrossed_top_general {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + Sound r res ⟨r.end_.key, ⊤⟩ := by + intro db₁ db₂ hagree + have habove : above db₁ r.end_.key = above db₂ r.end_.key := by + have h := hagree + rw [AgreeOn] at h + simpa [ConflictRange.keys, above] using h + have hck := completeKeys_eq_of_agree_above_crossed_rev_general (db₁ := db₁) + (db₂ := db₂) hrev hbegin hcross hend habove + constructor + · intro hc + simpa [RangeRead.Compatible, hck] using hc + · intro hc + simpa [RangeRead.Compatible, hck] using hc + +/-- In the finite branch `t + (begin.offset - 1) = (end.offset - 1)` of a crossed +reversed truncated read, the complete keys are exactly the reported keys. -/ +theorem completeKeys_eq_res_of_crossed_rev_finite_general {db : DB} {r : RangeRead} + {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = res.keys := by + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db E with hlE + set g : Nat := (between db E r.begin.key).length with hg + have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by + simpa [E, e, b, lE, g] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have htle : res.keys.length ≤ min e lE.length - (g + b) := by + rw [← hlen]; exact hpre.length_le + have htle' : res.keys.length + (g + b) ≤ e := by + have h1 : min e lE.length ≤ e := Nat.min_le_left _ _ + have h2 : res.keys.length ≤ min e lE.length - (g + b) := htle + omega + have hlelen : res.keys.length + b ≤ lE.length := by + have h1 : min e lE.length ≤ lE.length := Nat.min_le_right _ _ + have h2 := htle + omega + have htb : res.keys.length + b = e := by simpa [e, b] using ht + have hg0 : g = 0 := by omega + have he_le : e ≤ lE.length := by rw [← htb]; exact hlelen + have hmin : min e lE.length = e := Nat.min_eq_left he_le + have hlen' : (RangeRead.completeKeys db r).length = res.keys.length := by + rw [hlen, hg0, hmin]; omega + exact (hpre.eq_of_length hlen'.symm).symm + +/-- If a sorted list has exactly `e` elements at or below `H`, its first `e` elements +are those elements. -/ +theorem take_eq_filter_le_of_length {l : List Key} (hs : l.SortedLT) {H : Key} {e : Nat} + (h : (l.filter (fun k : Key => decide (k ≤ H))).length = e) : + l.take e = l.filter (fun k : Key => decide (k ≤ H)) := by + rw [filter_le_eq_take_countP hs H] + have hc : l.countP (fun k : Key => decide (k ≤ H)) = e := by + rw [List.countP_eq_length_filter]; exact h + rw [hc] + +/-- In the finite branch, filtering the keys at or above `end_.key` at or below the +reported head gives the first `e` of them. -/ +theorem crossed_rev_finite_above_filter_eq_take_general {db : DB} {r : RangeRead} + {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + (above db r.end_.key).filter + (fun k : Key => decide (k ≤ res.keys.head hne)) = + (above db r.end_.key).take ((r.end_.offset - 1).toNat) := by + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db E with hlE + set g : Nat := (between db E r.begin.key).length with hg + set H : Key := res.keys.head hne with hHdef + have hck := completeKeys_eq_res_of_crossed_rev_finite_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by + simpa [E, e, b, lE, g] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have htle : res.keys.length ≤ min e lE.length - (g + b) := by + rw [← hlen]; exact hpre.length_le + have hlelen : res.keys.length + b ≤ lE.length := by + have h1 : min e lE.length ≤ lE.length := Nat.min_le_right _ _ + have h2 := htle + omega + have htb : res.keys.length + b = e := by simpa [e, b] using ht + have hg0 : g = 0 := by + have h1 : min e lE.length ≤ e := Nat.min_le_left _ _ + omega + have he_le : e ≤ lE.length := by rw [← htb]; exact hlelen + have hmin : min e lE.length = e := Nat.min_eq_left he_le + have hwin : (lE.take e).drop b = res.keys.reverse := by + have h := congrArg List.reverse hck + rw [hformula, hg0, Nat.zero_add] at h + rw [List.reverse_reverse] at h + exact h + have hb_lt_e : b < e := by omega + have htake_len : (lE.take e).length = e := by rw [List.length_take, hmin] + have htakene : lE.take e ≠ [] := List.ne_nil_of_length_pos (by rw [htake_len]; omega) + have hdropne : (lE.take e).drop b ≠ [] := by + apply List.ne_nil_of_length_pos + rw [List.length_drop, htake_len] + omega + have hHlast : H = (lE.take e).getLast htakene := by + rw [hHdef] + have hrevne : res.keys.reverse ≠ [] := by rwa [List.reverse_ne_nil_iff] + have h1 : res.keys.head hne = (res.keys.reverse).getLast hrevne := + (List.getLast_reverse hrevne).symm + rw [h1] + exact (List.getLast_congr hrevne hdropne hwin.symm).trans (List.getLast_drop hdropne) + have hHidx : H = lE[e - 1]'(by omega) := by + rw [hHlast, List.getLast_take] + rw [List.getElem?_eq_getElem (by omega)] + simp + have hsE : lE.SortedLT := by + rw [hlE] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hfilter := filter_le_getElem_eq_take (A := lE) hsE (b := 0) (t := e) + (by omega) (by simpa using he_le) + have hfilter' : lE.filter (fun k : Key => decide (k ≤ H)) = lE.take e := by + rw [hHidx] + simpa [Nat.zero_add] using hfilter + simpa using hfilter' + +/-- In the finite branch the gap `[end_.key, begin_.key)` stores no key. -/ +theorem crossed_rev_finite_gap_eq_zero_general {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + (between db r.end_.key r.begin.key).length = 0 := by + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set g : Nat := (between db E r.begin.key).length with hg + set lE : List Key := above db E with hlE + have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by + simpa [E, e, b, lE, g] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have htb : res.keys.length + b = e := by simpa [e, b] using ht + have htle : res.keys.length ≤ min e lE.length - (g + b) := by + rw [← hlen]; exact hpre.length_le + have hmin : min e lE.length ≤ e := Nat.min_le_left _ _ + have hkeyle : res.keys.length ≤ e - (g + b) := + le_trans htle (Nat.sub_le_sub_right hmin (g + b)) + omega + +/-- Agreement on `[E, keyAfter H)` is agreement on the keys at or above `E` and at or +below `H`. -/ +theorem agreeOn_keys_eq_filter_le {E : WithTop Key} {H : Key} {d1 d2 : DB} + (hag : AgreeOn (⟨E, ((keyAfter H : Key) : WithTop Key)⟩ : ConflictRange) d1 d2) : + (above d1 E).filter (fun k : Key => decide (k ≤ H)) = + (above d2 E).filter (fun k : Key => decide (k ≤ H)) := by + have hmk (d : DB) : + (⟨E, ((keyAfter H : Key) : WithTop Key)⟩ : ConflictRange).keys d = + (above d E).filter (fun k : Key => decide (k ≤ H)) := by + rw [ConflictRange.keys_eq_inRange] + unfold RangeRead.inRange above + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hE : E ≤ (k : WithTop Key) <;> by_cases hk : k ≤ H <;> + simp [hE, hk, WithTop.coe_lt_coe, lt_keyAfter_iff] + have h := hag + rw [AgreeOn, hmk d1, hmk d2] at h + exact h + +/-- In the finite branch, agreement on `[end_.key, keyAfter (head reported))` fixes the +complete keys, hence soundness of the finite crossed reversed candidate. -/ +theorem sound_truncatedRevCrossed_finite_general {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + Sound r res ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ := by + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rw [hhead] + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set H : Key := res.keys.head hne with hHdef + have hcr : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : ConflictRange) = + ⟨E, ((keyAfter H : Key) : WithTop Key)⟩ := by simp [E, H] + rw [hcr] + have step : ∀ d1 d2 : DB, AgreeOn ⟨E, ((keyAfter H : Key) : WithTop Key)⟩ d1 d2 → + RangeRead.Compatible d1 r res → RangeRead.Compatible d2 r res := by + intro d1 d2 hag hc1 + have hck1 : RangeRead.completeKeys d1 r = res.keys := + completeKeys_eq_res_of_crossed_rev_finite_general (db := d1) (r := r) (res := res) + hc1 hmore hrev hbegin hcross hend hne ht + have hgap1 : (between d1 E r.begin.key).length = 0 := + crossed_rev_finite_gap_eq_zero_general (db := d1) (r := r) (res := res) + hc1 hmore hrev hbegin hcross hend hne ht + have hfil_eq := agreeOn_keys_eq_filter_le (E := E) (H := H) (d1 := d1) (d2 := d2) hag + have hfil1 : (above d1 E).filter (fun k : Key => decide (k ≤ H)) = + (above d1 E).take e := by + simpa [E, e, H] using crossed_rev_finite_above_filter_eq_take_general (db := d1) + (r := r) (res := res) hc1 hmore hrev hbegin hcross hend hne ht + have hformula1 : RangeRead.completeKeys d1 r = + (((above d1 E).take e).drop + ((between d1 E r.begin.key).length + b)).reverse := by + simpa [E, e, b] using + completeKeys_crossed_rev_eq_general (db := d1) (r := r) hrev hbegin hcross hend + have hlen1 : (RangeRead.completeKeys d1 r).length = + min e (above d1 E).length - + ((between d1 E r.begin.key).length + b) := by + rw [hformula1, List.length_reverse, List.length_drop, List.length_take] + have htle1 : res.keys.length ≤ min e (above d1 E).length - + ((between d1 E r.begin.key).length + b) := by + rw [← hlen1] + exact (RangeRead.compatible_keys_prefix hc1).length_le + have htb : res.keys.length + b = e := by simpa [e, b] using ht + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have he_le1 : e ≤ (above d1 E).length := by + have hmin := Nat.min_le_right e (above d1 E).length + omega + have htake_len1 : ((above d1 E).take e).length = e := by + rw [List.length_take, Nat.min_eq_left he_le1] + have hsE2 : (above d2 E).SortedLT := by + rw [above_eq_filter] + exact List.sortedLT_iff_pairwise.mpr (d2.sorted.pairwise.filter _) + have hfil2 : (above d2 E).filter (fun k : Key => decide (k ≤ H)) = + (above d2 E).take e := by + have hlen2 : ((above d2 E).filter (fun k : Key => decide (k ≤ H))).length = e := by + rw [← hfil_eq, hfil1, htake_len1] + exact (take_eq_filter_le_of_length hsE2 (H := H) (e := e) hlen2).symm + have htake_eq : (above d1 E).take e = (above d2 E).take e := by + rw [← hfil1, hfil_eq, hfil2] + have hBleH : r.begin.key ≤ (H : WithTop Key) := by + exact crossed_rev_res_ge_begin_general hc1 hbegin H (List.head_mem hne) + have hgap2 : (between d2 E r.begin.key).length = 0 := by + have hsub : between d2 E r.begin.key ⊆ between d1 E r.begin.key := by + intro k hk + rw [between_eq_filter_above] at hk ⊢ + rw [List.mem_filter] at hk ⊢ + obtain ⟨hkabove2, hklt⟩ := hk + have hkH : k ≤ H := by + have hkB : (k : WithTop Key) < r.begin.key := of_decide_eq_true hklt + exact WithTop.coe_le_coe.mp (le_trans (le_of_lt hkB) hBleH) + have hkfil2 : k ∈ (above d2 E).filter (fun k : Key => decide (k ≤ H)) := by + rw [List.mem_filter] + exact ⟨hkabove2, decide_eq_true hkH⟩ + rw [hfil2] at hkfil2 + have hktake1 : k ∈ (above d1 E).take e := by + rw [htake_eq] + exact hkfil2 + exact ⟨List.mem_of_mem_take hktake1, hklt⟩ + have hnil1 : between d1 E r.begin.key = [] := List.length_eq_zero_iff.mp hgap1 + rw [List.length_eq_zero_iff, List.eq_nil_iff_forall_not_mem] + intro k hk + have hk1 : k ∈ between d1 E r.begin.key := hsub hk + rw [hnil1] at hk1 + exact List.not_mem_nil hk1 + have hck2 : RangeRead.completeKeys d2 r = res.keys := by + have hformula2 : RangeRead.completeKeys d2 r = + (((above d2 E).take e).drop + ((between d2 E r.begin.key).length + b)).reverse := by + simpa [E, e, b] using + completeKeys_crossed_rev_eq_general (db := d2) (r := r) hrev hbegin hcross hend + rw [hformula2, hgap2, Nat.zero_add, ← hfil2, ← hfil_eq, hfil1] + have hf1 := hformula1 + rw [hgap1, Nat.zero_add] at hf1 + rw [← hf1, hck1] + rw [compatible_iff_truncated_keys_rev hrev hmore hne] + refine ⟨hc1.1, ?_⟩ + rw [hck2] + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact (RangeRead.compatible_sortedGT hc1 hrev).pairwise.imp le_of_lt |>.rel_getLast hk + intro d1 d2 hagree + exact ⟨step d1 d2 hagree, step d2 d1 hagree.symm⟩ + +/-- The keys at or above `E` are the database keys with everything below `E` +dropped. -/ +theorem above_eq_drop_cnt (db : DB) (E : WithTop Key) : + above db E = db.keys.drop (cnt db E) := by + rw [above_eq_filter, cnt] + have h := filter_not_lt_eq_drop_countP db.sorted E + convert h using 2 + funext k + rw [decide_eq_decide] + show (E ≤ ((k : Key) : WithTop Key)) ↔ ¬ (((k : Key) : WithTop Key) < E) + constructor + · intro h; exact not_lt.mpr h + · intro h; exact le_of_not_gt h + +/-- In the finite branch, the `b`-shifted first `e` keys at or above `end_.key` +are exactly the reported keys, reversed, and `e` is within the number of keys at +or above `end_.key`. -/ +theorem crossed_rev_finite_window_general {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + ((above db r.end_.key).take ((r.end_.offset - 1).toNat)).drop + ((r.begin.offset - 1).toNat) = res.keys.reverse ∧ + (r.end_.offset - 1).toNat ≤ (above db r.end_.key).length := by + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set g : Nat := (between db E r.begin.key).length with hg + set lE : List Key := above db E with hlE + have hck := completeKeys_eq_res_of_crossed_rev_finite_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + have hgap : g = 0 := by + simpa [g, E] using crossed_rev_finite_gap_eq_zero_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + have hformula : RangeRead.completeKeys db r = ((lE.take e).drop (g + b)).reverse := by + simpa [E, e, b, lE, g] using + completeKeys_crossed_rev_eq_general (db := db) (r := r) hrev hbegin hcross hend + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlen : (RangeRead.completeKeys db r).length = min e lE.length - (g + b) := by + rw [hformula, List.length_reverse, List.length_drop, List.length_take] + have htle : res.keys.length ≤ min e lE.length - (g + b) := by + rw [← hlen]; exact hpre.length_le + have htb : res.keys.length + b = e := by simpa [e, b] using ht + have hb_lt_e : b < e := by omega + have he_le : e ≤ lE.length := by + have hmin_le : min e lE.length ≤ e := Nat.min_le_left _ _ + omega + have hwin : (lE.take e).drop b = res.keys.reverse := by + have h := congrArg List.reverse hck + rw [hformula, List.reverse_reverse] at h + rw [hgap, Nat.zero_add] at h + exact h + exact ⟨by simpa [E, e, b, lE] using hwin, by simpa [E, e, lE] using he_le⟩ + +/-- In the finite branch, the first `e` keys at or above `end_.key` split into the +first `b` of them followed by the reversed reported keys. -/ +theorem crossed_rev_finite_take_split_general {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + (above db r.end_.key).take ((r.end_.offset - 1).toNat) = + (above db r.end_.key).take ((r.begin.offset - 1).toNat) ++ res.keys.reverse := by + obtain ⟨hwin, he_le⟩ := crossed_rev_finite_window_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + have hwin' : ((above db E).take e).drop b = res.keys.reverse := by + simpa [E, e, b] using hwin + have hb_le_e : b ≤ e := by + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have htb : res.keys.length + b = e := by simpa [e, b] using ht + omega + rw [← List.take_append_drop b ((above db E).take e), hwin'] + rw [List.take_take, Nat.min_eq_left hb_le_e] + +/-- In the finite branch, the reported head is the last of the first `e` keys at or +above `end_.key`. -/ +theorem crossed_rev_finite_head_lookup_general {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + ∃ (hidx : (r.end_.offset - 1).toNat - 1 < (above db r.end_.key).length), + (above db r.end_.key)[(r.end_.offset - 1).toNat - 1]'hidx = res.keys.head hne := by + obtain ⟨hwin, he_le⟩ := crossed_rev_finite_window_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db E with hlE + set H : Key := res.keys.head hne with hHdef + have hwin' : (lE.take e).drop b = res.keys.reverse := by + simpa [E, e, b, lE] using hwin + have he_le' : e ≤ lE.length := by simpa [E, e, lE] using he_le + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hb_lt_e : b < e := by + have htb : res.keys.length + b = e := by simpa [e, b] using ht + omega + have htake_len : (lE.take e).length = e := by rw [List.length_take, Nat.min_eq_left he_le'] + have htakene : lE.take e ≠ [] := List.ne_nil_of_length_pos (by rw [htake_len]; omega) + have hdropne : (lE.take e).drop b ≠ [] := by + apply List.ne_nil_of_length_pos + rw [List.length_drop, htake_len] + omega + have hHlast : H = (lE.take e).getLast htakene := by + rw [hHdef] + have hrevne : res.keys.reverse ≠ [] := by rwa [List.reverse_ne_nil_iff] + have h1 : res.keys.head hne = (res.keys.reverse).getLast hrevne := + (List.getLast_reverse hrevne).symm + rw [h1] + exact (List.getLast_congr hrevne hdropne hwin'.symm).trans (List.getLast_drop hdropne) + have hHidx : H = lE[e - 1]'(by omega) := by + rw [hHlast, List.getLast_take] + rw [List.getElem?_eq_getElem (by omega)] + simp + refine ⟨by simp only [E, e, lE] at he_le' ⊢; omega, ?_⟩ + simpa [E, e, lE, hHdef] using hHidx.symm + +/-- In the finite crossed reversed branch at a positive begin offset, inserting an +unstored key `k` with `end_.key ≤ k < head reported` makes the end selector resolve +onto the reported head, so the head leaves the complete keys and the truncated +result can no longer be compatible. -/ +theorem crossed_rev_insert_between_incompatible_general {db : DB} {r : RangeRead} + {res : Result} {k : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) + (hk0 : k ∉ db.keys) + (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + ¬ RangeRead.Compatible (db.insert k hk0) r res := by + let H : Key := res.keys.head hne + let E : WithTop Key := r.end_.key + let e : Nat := (r.end_.offset - 1).toNat + obtain ⟨hidx, hHlookup⟩ := crossed_rev_finite_head_lookup_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + have hHidx : H = (above db E)[e - 1]'hidx := by + simpa [H, E, e] using hHlookup.symm + have hdrop : above db E = db.keys.drop (cnt db E) := above_eq_drop_cnt db E + set j : Nat := cnt db E + (e - 1) with hjdef + have hjeq? : db.keys[j]? = some H := by + rw [hjdef, ← List.getElem?_drop (i := cnt db E) (j := e - 1) (xs := db.keys)] + rw [← hdrop, List.getElem?_eq_getElem hidx] + exact congrArg some (by simpa [E, e] using hHidx.symm) + obtain ⟨hjlt, hjeq⟩ := List.getElem?_eq_some_iff.mp hjeq? + have hcj : db.keys.countP (fun x : Key => decide (x < H)) = j := + countP_lt_eq_of_getElem db.sorted hjlt hjeq + have hHmem : H ∈ res.keys := by dsimp only [H]; exact List.head_mem hne + have hinskeys : (db.insert k hk0).keys = + db.keys.insertIdx (db.keys.countP (fun x => decide (x < k))) k := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hk0] + have hcountnew : (db.insert k hk0).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [hinskeys] + exact countP_insertIdx _ _ _ (by + rw [decide_eq_false_iff_not] + exact not_lt.mpr hEk) + have hanchornew : (r.end_.anchor (db.insert k hk0)).toNat = + db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e := by + rw [anchor_toNat_eq (s := r.end_) hend, hcountnew] + have hJeq : (r.end_.anchor (db.insert k hk0)).toNat = j + 1 := by + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have htb : res.keys.length + (r.begin.offset - 1).toNat = e := by simpa [e] using ht + have hepos : 0 < e := by omega + rw [hanchornew, hjdef] + have hcnt : db.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = cnt db E := by + rfl + rw [hcnt] + omega + have hpj : db.keys.countP (fun x : Key => decide (x < k)) ≤ j := by + rw [← hcj] + apply countP_le_countP_of_imp + intro x _ hlt + rw [decide_eq_true_eq] at hlt ⊢ + have hkH' : k < H := WithTop.coe_lt_coe.mp (by simpa [H] using hkH) + exact lt_trans hlt hkH' + have hnewlen : (r.end_.anchor (db.insert k hk0)).toNat < + (db.insert k hk0).keys.length := by + rw [hinskeys, hJeq, List.length_insertIdx_of_le_length List.countP_le_length] + omega + have hnewget? : (db.insert k hk0).keys[(r.end_.anchor (db.insert k hk0)).toNat]? = + some H := by + rw [hinskeys, hJeq, List.getElem?_insertIdx] + have hlt : ¬ j + 1 < db.keys.countP (fun x => decide (x < k)) := by omega + have heq : ¬ j + 1 = db.keys.countP (fun x => decide (x < k)) := by omega + rw [if_neg hlt, if_neg heq, Nat.add_sub_cancel, List.getElem?_eq_getElem hjlt, hjeq] + obtain ⟨_, hnewget⟩ := List.getElem?_eq_some_iff.mp hnewget? + have hHresolve : resolve (db.insert k hk0) r.end_ = ((H : Key) : WithTop Key) := by + rw [resolve_eq_getElem (anchor_nonneg_of_one_le hend) hnewlen] + exact congrArg (fun x : Key => (x : WithTop Key)) hnewget + intro hcompNew + have hpre := RangeRead.compatible_keys_prefix hcompNew + have hHcomp : H ∈ RangeRead.completeKeys (db.insert k hk0) r := hpre.subset hHmem + have hlt := (RangeRead.mem_completeKeys.mp hHcomp).2.2 + rw [hHresolve] at hlt + exact (lt_irrefl ((H : Key) : WithTop Key)) hlt + +/-- In the finite branch every key among the first `b` keys at or above `end_.key` +lies strictly below the reported last key. -/ +theorem crossed_rev_finite_pad_lt_last_general {db : DB} {r : RangeRead} {res : Result} + {k : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) + (hkP : k ∈ (above db r.end_.key).take ((r.begin.offset - 1).toNat)) : + k < res.keys.getLast hne := by + obtain ⟨hwin, he_le⟩ := crossed_rev_finite_window_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db E with hlE + have he_le' : e ≤ lE.length := by simpa [E, e, lE] using he_le + have hb_le_e : b ≤ e := by + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have htb : res.keys.length + b = e := by simpa [e, b] using ht + omega + have hsplit : lE.take e = lE.take b ++ res.keys.reverse := by + have h := crossed_rev_finite_take_split_general (db := db) (r := r) (res := res) + hcomp hmore hrev hbegin hcross hend hne ht + simpa [E, e, b, lE] using h + have hsortedE : (lE.take e).SortedLT := + List.sortedLT_iff_pairwise.mpr + (List.Pairwise.take (i := e) (List.sortedLT_iff_pairwise.mp (by + rw [hlE] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)))) + have hcrosslt : ∀ a ∈ lE.take b, ∀ c ∈ res.keys.reverse, a < c := by + rw [hsplit] at hsortedE + exact (List.sortedLT_append.mp hsortedE).2.2 + have hLmem : res.keys.getLast hne ∈ res.keys.reverse := + List.mem_reverse.mpr (List.getLast_mem hne) + exact hcrosslt k (by simpa [E, b, lE] using hkP) _ hLmem + +/-- In the finite branch the reported last key is the `b`-th key at or above +`end_.key`. -/ +theorem crossed_rev_finite_last_lookup_general {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + ∃ (hidx : (r.begin.offset - 1).toNat < (above db r.end_.key).length), + (above db r.end_.key)[(r.begin.offset - 1).toNat]'hidx = res.keys.getLast hne := by + obtain ⟨hwin, he_le⟩ := crossed_rev_finite_window_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db E with hlE + have he_le' : e ≤ lE.length := by simpa [E, e, lE] using he_le + have hb_le_e : b ≤ e := by + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have htb : res.keys.length + b = e := by simpa [e, b] using ht + omega + have hsplit : lE.take e = lE.take b ++ res.keys.reverse := by + have h := crossed_rev_finite_take_split_general (db := db) (r := r) (res := res) + hcomp hmore hrev hbegin hcross hend hne ht + simpa [E, e, b, lE] using h + have hP_len : (lE.take b).length = b := by + rw [List.length_take, Nat.min_eq_left (le_trans hb_le_e he_le')] + have hdecomp : lE = lE.take b ++ res.keys.reverse ++ lE.drop e := by + conv_lhs => rw [← List.take_append_drop e lE] + rw [hsplit, List.append_assoc] + have hb_lt : b < lE.length := by + rw [hdecomp, List.length_append, List.length_append, hP_len, List.length_reverse] + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + omega + refine ⟨by simp only [E, b, lE] at hb_lt ⊢; exact hb_lt, ?_⟩ + have hgoal? : lE[b]? = some (res.keys.getLast hne) := by + rw [hdecomp] + rw [List.getElem?_append] + rw [if_pos (by + rw [List.length_append, hP_len, List.length_reverse] + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + omega)] + rw [List.getElem?_append] + rw [if_neg (by rw [hP_len]; omega)] + have hidx : b - (lE.take b).length = 0 := by rw [hP_len]; omega + rw [hidx] + have hpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hrev? := List.getElem?_reverse (l := res.keys) (i := 0) (by simpa using hpos) + rw [hrev?] + rw [Nat.sub_zero] + rw [List.getElem?_eq_getElem (by omega)] + congr 1 + rw [List.getLast_eq_getElem] + obtain ⟨_, hval⟩ := List.getElem?_eq_some_iff.mp hgoal? + exact hval + +/-- Erasing `k` from the database removes it from the keys at or above `E` when it was +there. -/ +theorem above_erase_of_mem (db : DB) (E : WithTop Key) {k : Key} + (hk : k ∈ above db E) : above (db.erase k) E = (above db E).erase k := by + rw [above_eq_filter, above_eq_filter, DB.erase_keys, ← List.erase_filter] + +/-- Erasing `k` from `[E, B)` removes it from the erased database's gap. -/ +theorem between_erase_of_mem (db : DB) (E B : WithTop Key) (k : Key) : + between (db.erase k) E B = (between db E B).erase k := by + unfold between + rw [DB.erase_keys, ← List.erase_filter] + +/-- If a key `k` lies at or above `E` and strictly below the reported last `L`, it is +among the first `b` keys at or above `E` in the finite crossed reversed branch. -/ +theorem crossed_rev_finite_mem_take_of_lt_last_general {db : DB} {r : RangeRead} + {res : Result} {k : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) + (hkdb : k ∈ db.keys) (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : k < res.keys.getLast hne) : + k ∈ (above db r.end_.key).take ((r.begin.offset - 1).toNat) := by + set E : WithTop Key := r.end_.key with hE + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db E with hlE + obtain ⟨hbL, hLlookup⟩ := crossed_rev_finite_last_lookup_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + have hsE : lE.SortedLT := by + rw [hlE] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hkmem : k ∈ lE := by + rw [hlE, mem_above] + exact ⟨hkdb, by simpa [E] using hEk⟩ + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hkmem + have hLlookup' : lE[b]'hbL = res.keys.getLast hne := by + simpa [E, b, lE] using hLlookup + have hib : i < b := by + have hlt : lE[i] < lE[b] := by + rw [hik, hLlookup'] + exact hkL + exact (List.SortedLT.getElem_lt_getElem_iff (l := lE) hsE).mp hlt + rw [List.mem_take_iff_getElem] + exact ⟨i, Nat.lt_min.mpr ⟨by simpa [b] using hib, hi⟩, by simpa [E, lE] using hik⟩ + +/-- In the finite crossed reversed branch, erasing a reported key makes the result +incompatible. -/ +theorem crossed_rev_erase_reported_incompatible_general {db : DB} {r : RangeRead} + {res : Result} {k : Key} (hcomp : RangeRead.Compatible db r res) + (hk : k ∈ res.keys) : + ¬ RangeRead.Compatible (db.erase k) r res := by + intro hc + have hsub := RangeRead.compatible_keys_subset hc + exact absurd (hsub hk) (DB.not_mem_erase_self db k) + +/-- In the finite crossed reversed branch, erasing one of the `b` pad keys makes the +reported last key leave the complete keys, so the result becomes incompatible. -/ +theorem crossed_rev_finite_erase_pad_incompatible_general {db : DB} {r : RangeRead} + {res : Result} {k : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) + (hkP : k ∈ (above db r.end_.key).take ((r.begin.offset - 1).toNat)) : + ¬ RangeRead.Compatible (db.erase k) r res := by + set E : WithTop Key := r.end_.key with hE + set e : Nat := (r.end_.offset - 1).toNat with he + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db E with hlE + set L : Key := res.keys.getLast hne with hL + have hkP' : k ∈ lE.take b := by simpa [E, b, lE] using hkP + have hklE : k ∈ lE := List.take_subset b lE hkP' + have hklt : k < L := by + have h := crossed_rev_finite_pad_lt_last_general (db := db) (r := r) (res := res) + hcomp hmore hrev hbegin hcross hend hne ht hkP + simpa [L] using h + obtain ⟨hbL, hLlookup⟩ := crossed_rev_finite_last_lookup_general (db := db) (r := r) + (res := res) hcomp hmore hrev hbegin hcross hend hne ht + have hsE : lE.SortedLT := by + rw [hlE] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hsErase : (lE.erase k).SortedLT := + List.sortedLT_iff_pairwise.mpr (hsE.pairwise.erase k) + have hLlookup' : lE[b]'hbL = L := by simpa [E, b, lE, L] using hLlookup + have hcount : lE.countP (fun x : Key => decide (x < L)) = b := + countP_lt_eq_of_getElem hsE hbL hLlookup' + have hcountErase : (lE.erase k).countP (fun x : Key => decide (x < L)) = b - 1 := by + rw [List.countP_erase, hcount] + have hcond : k ∈ lE ∧ decide (k < L) = true := ⟨hklE, by simp [hklt]⟩ + rw [if_pos hcond] + have hLmemErase : L ∈ lE.erase k := by + have hLmem : L ∈ lE := hLlookup' ▸ List.getElem_mem hbL + exact (List.mem_erase_of_ne (ne_of_gt hklt)).mpr hLmem + have hbpos : 0 < b := by + obtain ⟨i, hi, _⟩ := List.mem_take_iff_getElem.mp hkP' + exact Nat.lt_of_le_of_lt (Nat.zero_le i) (Nat.lt_min.mp hi).1 + have hnotdrop : L ∉ (lE.erase k).drop b := by + intro hmem + obtain ⟨j, hj, hjeq⟩ := List.mem_drop_iff_getElem.mp hmem + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hLmemErase + have hci : (lE.erase k).countP (fun x : Key => decide (x < L)) = i := + countP_lt_eq_of_getElem hsErase hi hix + have hib : i < b := by omega + have hbj : b + j < (lE.erase k).length := by omega + have hlt : (lE.erase k)[i] < (lE.erase k)[b + j] := + (List.SortedLT.getElem_lt_getElem_iff (l := lE.erase k) hsErase + (hi := hi) (hj := hbj)).mpr (by omega) + rw [hix, hjeq] at hlt + exact lt_irrefl L hlt + have hgapdb : between db E r.begin.key = [] := by + have h := crossed_rev_finite_gap_eq_zero_general (db := db) (r := r) (res := res) + hcomp hmore hrev hbegin hcross hend hne ht + apply List.eq_nil_of_length_eq_zero + simpa [E] using h + have hgapnew : between (db.erase k) E r.begin.key = [] := by + rw [between_erase_of_mem, hgapdb] + rfl + have hformula : RangeRead.completeKeys (db.erase k) r = + (((above (db.erase k) E).take e).drop + ((between (db.erase k) E r.begin.key).length + b)).reverse := + completeKeys_crossed_rev_eq_general (db := db.erase k) (r := r) hrev hbegin hcross hend + rw [hgapnew] at hformula + rw [List.length_nil] at hformula + rw [Nat.zero_add] at hformula + have habove : above (db.erase k) E = lE.erase k := by + rw [← above_erase_of_mem db E hklE] + intro hcompNew + have hLcomp : L ∈ RangeRead.completeKeys (db.erase k) r := by + have hpre := RangeRead.compatible_keys_prefix hcompNew + exact hpre.subset (by rw [hL]; exact List.getLast_mem hne) + rw [hformula, List.mem_reverse, habove] at hLcomp + have hdropmem : L ∈ (lE.erase k).drop b := + ((List.take_sublist e (lE.erase k)).drop b).subset hLcomp + exact hnotdrop hdropmem + +/-- **Finite-branch minimality for crossed reversed truncated results at any positive +begin offset.** -/ +theorem minimal_truncatedRevCrossed_finite_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (ht : res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + Minimal r res ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ := by + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set E : WithTop Key := r.end_.key with hE + set b : Nat := (r.begin.offset - 1).toNat with hb + set lE : List Key := above db₀ E with hlE + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + refine (minimal_iff_sound_and_necessary).mpr + ⟨?_, ?_⟩ + · have h := sound_truncatedRevCrossed_finite_general (r := r) (res := res) + hmore hrev hbegin hcross hend hne ht + simpa [E, hhead] using h + · intro k hk + rw [ConflictRange.mem_iff] at hk + have hEk : E ≤ (k : WithTop Key) := hk.1 + have hkH : k ≤ H := by + have h2 : (k : WithTop Key) < (keyAfter (res.keys.headD ([] : Key)) : WithTop Key) := hk.2 + rw [hhead] at h2 + exact (lt_keyAfter_iff k (res.keys.head hne)).mp (WithTop.coe_lt_coe.mp h2) + by_cases hkL : k < L + · by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread' hkres + · by_cases hkdb : k ∈ db₀.keys + · have hkP : k ∈ lE.take b := by + have h := crossed_rev_finite_mem_take_of_lt_last_general (db := db₀) (r := r) + (res := res) hcomp₀ hmore hrev hbegin hcross hend hne ht hkdb + (by simpa [E] using hEk) (by simpa [L] using hkL) + simpa [E, b, lE] using h + intro cr hs + by_contra hkcr + have hins := crossed_rev_finite_erase_pad_incompatible_general (db := db₀) (r := r) + (res := res) hcomp₀ hmore hrev hbegin hcross hend hne ht + (by simpa [E, b, lE] using hkP) + have herase : AgreeOn cr db₀ (db₀.erase k) := + (ConflictRange.keys_erase_of_not_mem cr db₀ hkcr).symm + exact hins ((hs db₀ (db₀.erase k) herase).mp hcomp₀) + · intro cr hs + by_contra hkcr + have hkHlt : (k : WithTop Key) < ((H : Key) : WithTop Key) := by + have hkL' : (k : WithTop Key) < (L : WithTop Key) := WithTop.coe_lt_coe.mpr hkL + have hLH : (L : WithTop Key) ≤ (H : WithTop Key) := by + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, res.keys.getLast hne ≤ x := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + exact WithTop.coe_le_coe.mpr (by + rw [hL, hH] + exact hLle _ (List.head_mem hne)) + exact lt_of_lt_of_le hkL' hLH + have hagree : AgreeOn cr db₀ (db₀.insert k hkdb) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hkdb hkcr).symm + have hcompNew : RangeRead.Compatible (db₀.insert k hkdb) r res := + (hs db₀ _ hagree).mp hcomp₀ + exact crossed_rev_insert_between_incompatible_general (db := db₀) hcomp₀ hmore hrev + hbegin hcross hend hne ht hkdb (by simpa [E] using hEk) hkHlt hcompNew + · have hLk : L ≤ k := le_of_not_gt hkL + exact necessary_of_between_result hread' (by rw [hL]; exact List.getLast_mem hne) + (by rw [hH]; exact List.head_mem hne) hLk hkH + +/-- A closed form for crossed reversed truncated results at every positive begin offset. +The finite branch `t + (begin.offset - 1) = (end.offset - 1)` is compact; the remaining +top branch uses the exact executable algorithm as a fallback. -/ +noncomputable def closedFormCrossedRevGeneral (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.key < r.begin.key ∧ + res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat then + ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ + else + simpleConflictRange r res + +/-- The partial crossed reversed closed form is minimal on every readable truncated +result at every positive begin and end offset. -/ +theorem minimal_closedForm_of_crossedRevGeneral {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) : + Minimal r res (closedFormCrossedRevGeneral r res) := by + obtain ⟨db, hcomp⟩ := hread + have hread' : Readable r res := ⟨db, hcomp⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + unfold closedFormCrossedRevGeneral + by_cases hcond : r.end_.key < r.begin.key ∧ + res.keys.length + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat + · rw [ite_eq_left hcond] + exact minimal_truncatedRevCrossed_finite_general hread' hmore hrev hbegin hcond.1 hend + hne hcond.2 + · rw [ite_eq_right hcond] + exact simpleConflictRange_minimal hread' + +attribute [irreducible] closedFormCrossedRevGeneral + +/-- The partial crossed reversed closed form denotes the general conflict range. -/ +theorem closedFormCrossedRevGeneral_toSet_eq_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) : + (closedFormCrossedRevGeneral r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_closedForm_of_crossedRevGeneral hread hmore hrev hbegin hend).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The same identification with the executable algorithm. -/ +theorem closedFormCrossedRevGeneral_toSet_eq_simple {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) : + (closedFormCrossedRevGeneral r res).toSet = (simpleConflictRange r res).toSet := + (minimal_closedForm_of_crossedRevGeneral hread hmore hrev hbegin hend).toSet_eq + (simpleConflictRange_minimal hread) + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedTruncated.lean b/FormalConflictRange/CrossedTruncated.lean new file mode 100644 index 0000000..92f4a47 --- /dev/null +++ b/FormalConflictRange/CrossedTruncated.lean @@ -0,0 +1,757 @@ +/- +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.CrossedClosedForm + +/-! +# Crossed selectors at begin offset one: the truncated cells + +This module proves the minimal conflict range for a truncated, non-reversed result at +`begin.offset = 1` when the selector keys are crossed, `end_.key < begin_.key`. + +The complete-key window starts at the first stored key at or above `begin_.key`, while +the end resolution counts stored keys from `end_.key`. The gap `[end_.key, begin_.key)` +therefore contributes to the end count without contributing to the reported keys. +Writing `e = (end_.offset - 1).toNat`, `t = #reported keys`, and `h = intervalCard +end_.key begin_.key`, the minimal range is + +* `[end_.key, keyAfter (last reported))` when `e - t < h`; +* `[begin_.key, keyAfter (last reported))` otherwise. + +The lower endpoint moves down to `end_.key` exactly when the gap has room for the +remaining overshoot, mirroring the complete-result crossed rule. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- **B-branch soundness for the forward truncated crossed read.** When the gap +`[end_.key, begin_.key)` has at least the remaining overshoot `e - |res|` keys, the +truncated range `[begin.key, keyAfter (last reported))` is sound. The proof rewrites +the complete keys as a take of the stored keys at or above `begin.key`; the range +equality fixes the filtered take, and the room bound makes the take long enough not to +cut the reported prefix. -/ +theorem sound_truncatedRange_of_crossed_B {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞)) : + Sound r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + intro db₁ db₂ hagree + have key (db : DB) : RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (truncatedRange r (res.keys.getLast hne)).keys db = res.keys := by + constructor + · intro hc + refine ⟨hc.1, ?_⟩ + set l := db.keys with hl + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hA + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hc + have htake := completeKeys_eq_filter_take_of_offset_one (db := db) hrev hbegin + (by omega : 1 ≤ r.end_.offset) + have hpreA : res.keys <+: A := by + rw [hA] + refine List.IsPrefix.trans hpre ?_ + rw [htake] + exact List.take_prefix _ _ + have hAs : A.Pairwise (· < ·) := by + rw [hA] + exact db.sorted.pairwise.filter _ + have hfil := filter_le_getLast_eq_of_prefix hAs hpreA hne + rw [truncatedRange_keys_eq_filter_le] + exact hfil + · rintro ⟨hlim, hT⟩ + rw [compatible_iff_truncated_keys (db := db) hrev hmore hne] + refine ⟨hlim, ?_⟩ + set l := db.keys with hl + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set cB := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hcB + set e := (r.end_.offset - 1).toNat with he + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hA + set n := res.keys.length with hn + set q := A.countP (fun k : Key => decide (k ≤ res.keys.getLast hne)) with hq + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hAs : A.Pairwise (· < ·) := by + rw [hA] + exact hls.pairwise.filter _ + have hAsLT : A.SortedLT := List.sortedLT_iff_pairwise.mpr hAs + have hA_eq : A = l.drop cB := by + rw [hA, hcB] + have hdrop : l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) + = l.drop (l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key))) := + filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hfilT : A.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = res.keys := by + rw [← truncatedRange_keys_eq_filter_le (db := db) (r := r) (last := res.keys.getLast hne)] + exact hT + have hresA : res.keys = A.take q := by + rw [← hfilT, filter_le_eq_take_countP hAsLT (res.keys.getLast hne), hq] + have hqle : q ≤ A.length := by rw [hq]; exact List.countP_le_length + have hnq : n = q := by + rw [hn, hresA, List.length_take, Nat.min_eq_left hqle] + have hcnt : cB = cE + (between db r.end_.key r.begin.key).length := by + rw [hcB, hcE] + simpa [cnt, hl] using cnt_eq_add_between db (le_of_lt hcross) + have hn_le_e : n ≤ e := by + by_contra hlt + have hne : e < n := by omega + have hz : e - n = 0 := Nat.sub_eq_zero_of_le (le_of_lt hne) + have hle0 : intervalCard r.end_.key r.begin.key ≤ 0 := by simpa [hz] using hcard + have hzero : intervalCard r.end_.key r.begin.key = 0 := + le_antisymm hle0 zero_le + exact absurd (intervalCard_eq_zero_iff.mp hzero) (not_le.mpr hcross) + have hgn : (between db r.end_.key r.begin.key).length + n ≤ e := by + have hg : (((between db r.end_.key r.begin.key).length : ℕ) : ℕ∞) ≤ + (((e - n : ℕ)) : ℕ∞) := by + refine le_trans (between_length_le_intervalCard (db := db) (le_of_lt hcross)) ?_ + simpa [e, n, hn] using hcard + have hg' : (between db r.end_.key r.begin.key).length ≤ e - n := by + exact_mod_cast hg + omega + have hAn : n ≤ A.length := by + rw [hn, hresA, List.length_take] + exact Nat.min_le_right _ _ + have hcBl : cB ≤ l.length := by rw [hcB]; exact List.countP_le_length + have hlen : cB + n ≤ l.length := by + rw [hA_eq, List.length_drop] at hAn + omega + have hm_ge : n ≤ min (cE + e) l.length - cB := by + have h1 : cB + n ≤ cE + e := by rw [hcnt]; omega + have h3 : cB + n ≤ min (cE + e) l.length := le_min h1 hlen + omega + rw [completeKeys_eq_filter_take_of_offset_one (db := db) hrev hbegin + (by omega : 1 ≤ r.end_.offset)] + rw [← hA] + rw [filter_take_eq_take_min (p := fun k : Key => decide (k ≤ res.keys.getLast hne)) + (h := filter_le_eq_take_countP hAsLT (res.keys.getLast hne)) + (m := min (cE + e) l.length - cB)] + have hmin : min (min (cE + e) l.length - cB) q = n := by + rw [← hnq] + exact Nat.min_eq_right hm_ge + rw [hmin, hnq] + exact hresA.symm + rw [key db₁, key db₂, hagree] + + +private theorem keys_lo_keyAfter_eq_filter_le {db : DB} {lo : WithTop Key} {last : Key} : + (⟨lo, (keyAfter last : Key)⟩ : ConflictRange).keys db = + (db.keys.filter (fun k : Key => decide (lo ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + unfold ConflictRange.keys + rw [show db.keys.filter (fun (k : Key) => decide (lo ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + db.keys.filter (fun (k : Key) => + decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key)) && + decide (lo ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter] + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + +private theorem filter_ge_last_eq_filter_of_keys_eq {db : DB} {E B : WithTop Key} {last : Key} + (hEB : E ≤ B) (hBlast : B ≤ (last : WithTop Key)) : + (db.keys.filter (fun k : Key => decide (B ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + ((⟨E, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide (B ≤ (k : WithTop Key))) := by + rw [keys_lo_keyAfter_eq_filter_le] + simp only [List.filter_filter] + apply List.filter_congr + intro k _ + simp only [← Bool.decide_and] + rw [decide_eq_decide] + constructor + · rintro ⟨hl, hB⟩ + exact ⟨hB, hl, le_trans hEB hB⟩ + · rintro ⟨hB, hl, _⟩ + exact ⟨hl, hB⟩ + +private theorem between_eq_filter_of_keys_eq {db : DB} {E B : WithTop Key} {last : Key} + (hEB : E ≤ B) (hBlast : B ≤ (last : WithTop Key)) : + between db E B = + ((⟨E, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide ((k : WithTop Key) < B)) := by + rw [keys_lo_keyAfter_eq_filter_le] + simp only [List.filter_filter] + unfold between + rw [show db.keys.filter (fun k : Key => decide (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B)) = + db.keys.filter (fun k : Key => decide ((k : WithTop Key) < B) && decide (E ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + apply List.filter_congr + intro k _ + simp only [← Bool.decide_and] + rw [decide_eq_decide] + constructor + · rintro ⟨hkB, hE⟩ + have hklt : (k : WithTop Key) < (last : WithTop Key) := lt_of_lt_of_le hkB hBlast + exact ⟨hkB, le_of_lt (WithTop.coe_lt_coe.mp hklt), hE⟩ + · rintro ⟨hkB, _, hE⟩ + exact ⟨hkB, hE⟩ + +/-- Agreement on `[E, keyAfter last)` determines the complete keys filtered at or +below `last`, for a forward offset-one read with `E ≤ begin.key`. -/ +theorem completeKeys_filter_le_eq_of_agree_crossed {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hend : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) (hBlast : r.begin.key ≤ (last : WithTop Key)) + {db₁ db₂ : DB} + (hagree : (⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db₁ = + (⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db₂) : + (RangeRead.completeKeys db₁ r).filter (fun k : Key => decide (k ≤ last)) = + (RangeRead.completeKeys db₂ r).filter (fun k : Key => decide (k ≤ last)) := by + have hC (db : DB) : + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + ((⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) := + filter_ge_last_eq_filter_of_keys_eq (db := db) hEB hBlast + have hG (db : DB) : + between db r.end_.key r.begin.key = + ((⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := + between_eq_filter_of_keys_eq (db := db) hEB hBlast + have hCeq : (db₁.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + (db₂.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + rw [hC db₁, hC db₂, hagree] + have hGeq : between db₁ r.end_.key r.begin.key = between db₂ r.end_.key r.begin.key := by + rw [hG db₁, hG db₂, hagree] + let e := (r.end_.offset - 1).toNat + have hformula (db : DB) : + (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + (((db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).take + (min (e - (between db r.end_.key r.begin.key).length) + (((db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length))) := by + set l := db.keys with hl + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hA + set C := A.filter (fun k : Key => decide (k ≤ last)) with hCdef + set q := A.countP (fun k : Key => decide (k ≤ last)) with hq + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hAs : A.SortedLT := by + rw [hA] + exact List.sortedLT_iff_pairwise.mpr (hls.pairwise.filter _) + have hCtake : C = A.take q := by + rw [hCdef, hq] + exact filter_le_eq_take_countP hAs last + have hqC : C.length = q := by rw [hCtake, List.length_take, Nat.min_eq_left (by rw [hq]; exact List.countP_le_length)] + have hCAlen : C.length ≤ A.length := by rw [hCtake]; exact (List.take_sublist q A).length_le + have hcB : l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (between db r.end_.key r.begin.key).length := by + simpa [cnt, hl] using cnt_eq_add_between db hEB + have hM : min (l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + e) + l.length - l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + min (e - (between db r.end_.key r.begin.key).length) A.length := by + have hA_eq : A = l.drop (l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key))) := by + rw [hA] + have hdrop := filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hAlen : A.length = l.length - l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by + rw [hA_eq, List.length_drop] + rw [hcB, hAlen] + omega + have htake := completeKeys_eq_filter_take_of_offset_one (db := db) hrev hbegin hend + rw [htake] + rw [filter_take_eq_take_min (p := fun k : Key => decide (k ≤ last)) + (h := filter_le_eq_take_countP hAs last) + (m := min (l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + e) + l.length - l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)))] + rw [hM] + change A.take (min (min (e - (between db r.end_.key r.begin.key).length) A.length) q) = + C.take (min (e - (between db r.end_.key r.begin.key).length) C.length) + have hqA : q ≤ A.length := by rw [← hqC]; exact hCAlen + have hminA : min A.length q = q := Nat.min_eq_right hqA + rw [Nat.min_assoc, hminA] + rw [hqC, hCtake, List.take_take] + rw [Nat.min_assoc, Nat.min_self] + rw [hformula db₁, hformula db₂, hCeq, hGeq] + +/-- A compatible forward offset-one result's last key is at or above the begin key. -/ +theorem le_begin_key_of_compatible_crossed {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hend : 1 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hc : RangeRead.Compatible db r res) : + r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + have htake := completeKeys_eq_filter_take_of_offset_one (db := db) hrev hbegin hend + have hmem : res.keys.getLast hne ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hc).subset (List.getLast_mem hne) + rw [htake] at hmem + have := List.mem_of_mem_take hmem + exact decide_eq_true_eq.mp (List.mem_filter.mp this).2 + +/-- **E-branch soundness for the forward truncated crossed read.** When the gap +`[end_.key, begin_.key)` is too small to absorb the remaining overshoot, the wider +range `[end_.key, keyAfter (last reported))` is sound: agreement on it fixes the +reported prefix of the complete keys. -/ +theorem sound_truncatedRange_of_crossed_E {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + Sound r res ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + intro db₁ db₂ hagree + constructor + · intro hc₁ + have hBlast := le_begin_key_of_compatible_crossed (db := db₁) hrev hbegin (by omega) hne hc₁ + have hceq := completeKeys_filter_le_eq_of_agree_crossed (r := r) + (last := res.keys.getLast hne) hrev hbegin (by omega) (le_of_lt hcross) hBlast hagree + rw [compatible_iff_truncated_keys (db := db₁) hrev hmore hne] at hc₁ + rw [compatible_iff_truncated_keys (db := db₂) hrev hmore hne] + refine ⟨hc₁.1, ?_⟩ + rw [← hceq] + exact hc₁.2 + · intro hc₂ + have hBlast := le_begin_key_of_compatible_crossed (db := db₂) hrev hbegin (by omega) hne hc₂ + have hceq := completeKeys_filter_le_eq_of_agree_crossed (r := r) + (last := res.keys.getLast hne) hrev hbegin (by omega) (le_of_lt hcross) hBlast hagree + rw [compatible_iff_truncated_keys (db := db₂) hrev hmore hne] at hc₂ + rw [compatible_iff_truncated_keys (db := db₁) hrev hmore hne] + refine ⟨hc₂.1, ?_⟩ + rw [hceq] + exact hc₂.2 + + + +private theorem enat_succ_le_of_lt {s : ℕ} {h : ℕ∞} (hs : (s : ℕ∞) < h) : + ((s + 1 : ℕ) : ℕ∞) ≤ h := by + cases h with + | top => exact le_top + | coe n => + rw [ENat.coe_lt_coe] at hs + exact_mod_cast Nat.succ_le_of_lt hs + +/-- **Gap-key necessity for the forward truncated crossed read.** When the gap +`[end_.key, begin_.key)` is too small to absorb the remaining overshoot, every key in +the gap is necessary: a witness with `s` gap keys whose complete window is exactly the +reported prefix becomes incompatible once the target gap key is inserted, because the +window shrinks by one and drops the last reported key. -/ +theorem necessary_of_mem_gap_crossed_truncated {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hlt : (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) < + intervalCard r.end_.key r.begin.key) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + set t : ℕ := res.keys.length with htdef + set eo : ℕ := (r.end_.offset - 1).toNat with heodef + set s : ℕ := eo - t with hsdef + obtain ⟨db₀, hcomp₀⟩ := hread + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + -- The reported keys all lie at or above `begin.key`. + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + have htake := completeKeys_eq_filter_take_of_offset_one (db := db₀) hrev hbegin + (by omega : 1 ≤ r.end_.offset) + have hmem : x ∈ RangeRead.completeKeys db₀ r := hpre₀.subset hx + rw [htake] at hmem + exact decide_eq_true_eq.mp (List.mem_filter.mp (List.mem_of_mem_take hmem)).2 + have hlast_ge_B : r.begin.key ≤ (res.keys.getLast hne : WithTop Key) := + hall_ge _ (List.getLast_mem hne) + -- `t ≤ eo`, so `eo = s + t`. + have ht_le_eo : t ≤ eo := by + have hlen₀ := completeKeys_length_of_offset_one (db := db₀) hrev hbegin + (by omega : 1 ≤ r.end_.offset) + have htl : t ≤ (RangeRead.completeKeys db₀ r).length := by + rw [htdef]; exact hpre₀.length_le + rw [hlen₀] at htl + set a := db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with ha + set bb := db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hbb + set N := db₀.keys.length with hN + have hmono : a ≤ bb := by + rw [ha, hbb] + simpa [cnt] using cnt_le_cnt_of_le db₀ (le_of_lt hcross) + have hmin : min (a + eo) N ≤ a + eo := min_le_left _ _ + omega + have heo : eo = s + t := by + rw [hsdef] + exact (Nat.sub_add_cancel ht_le_eo).symm + have hcard : ((s + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := + enat_succ_le_of_lt (s := s) (h := intervalCard r.end_.key r.begin.key) (by + simpa [s, htdef, heodef] using hlt) + obtain ⟨dbH, hHrange, hkH, hHlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := s) hcard + have hHgeE : ∀ x ∈ dbH.keys, r.end_.key ≤ (x : WithTop Key) := + fun x hx => (hHrange x hx).1 + have hHltB : ∀ x ∈ dbH.keys, (x : WithTop Key) < r.begin.key := + fun x hx => (hHrange x hx).2 + obtain ⟨C, hCge, hCnotin⟩ := + exists_le_not_mem (res.keys ++ dbH.keys) (res.keys.getLast hne) + have hCnotres : C ∉ res.keys := fun h => hCnotin (List.mem_append_left _ h) + have hCnotH : C ∉ dbH.keys := fun h => hCnotin (List.mem_append_right _ h) + have hCgtRes : ∀ a ∈ res.keys, a < C := by + intro a ha + have hle : a ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast ha + exact lt_of_le_of_ne (le_trans hle hCge) (fun h => hCnotres (h ▸ ha)) + have hHltC : ∀ a ∈ dbH.keys, a < C := by + intro a ha + have h1 : (a : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le (hHltB a ha) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + exact WithTop.coe_lt_coe.mp h1 + have hHres_lt : ∀ a ∈ dbH.keys, ∀ b ∈ res.keys, a < b := + fun a ha b hb => + WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltB a ha) (hall_ge b hb)) + have hHres_sorted : (dbH.keys ++ res.keys).SortedLT := + List.sortedLT_append.mpr ⟨dbH.sorted, hsort, hHres_lt⟩ + have hsingle : ([C] : List Key).SortedLT := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + have hcrossList : ∀ a ∈ (dbH.keys ++ res.keys), ∀ b ∈ ([C] : List Key), a < b := by + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + rcases List.mem_append.mp ha with haH | haR + · exact hHltC a haH + · exact hCgtRes a haR + have hlst_sorted : (dbH.keys ++ res.keys ++ [C]).SortedLT := + List.sortedLT_append.mpr ⟨hHres_sorted, hsingle, hcrossList⟩ + let db₂ : DB := ⟨dbH.keys ++ res.keys ++ [C], hlst_sorted⟩ + have hdb₂ : db₂.keys = dbH.keys ++ res.keys ++ [C] := rfl + have hfil₂ : db₂.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = res.keys ++ [C] := by + rw [hdb₂, List.filter_append, List.filter_append] + have hHf : dbH.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_le.mpr (hHltB x hx) + have hresf : res.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hall_ge x hx + have hCf : ([C] : List Key).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = [C] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge) + rw [hHf, hresf, hCf] + simp + have hcE₂ : cnt db₂ r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hdb₂] at hx + rcases List.mem_append.mp hx with hx' | hxC + · rcases List.mem_append.mp hx' with hxH | hxR + · exact hHgeE x hxH + · exact le_trans (le_of_lt hcross) (hall_ge x hxR) + · rw [List.mem_singleton] at hxC + subst hxC + exact le_trans (le_of_lt hcross) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + have hcB₂ : cnt db₂ r.begin.key = s := by + show db₂.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = s + rw [hdb₂, List.countP_append, List.countP_append] + have hHc : dbH.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbH.keys.length := + cnt_eq_length_of_forall_lt dbH hHltB + have hresc : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hall_ge x hx) + have hCc : ([C] : List Key).countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact not_lt.mpr (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hHc, hresc, hCc, hHlen] + omega + have hn₂ : db₂.keys.length = s + t + 1 := by + rw [hdb₂, List.length_append, List.length_append, hHlen, htdef, List.length_singleton] + have hck₂ : RangeRead.completeKeys db₂ r = res.keys := by + rw [completeKeys_eq_filter_take_of_offset_one hrev hbegin (by omega : 1 ≤ r.end_.offset), + hfil₂] + have hidx : min (cnt db₂ r.end_.key + eo) db₂.keys.length - + cnt db₂ r.begin.key = t := by + rw [hcE₂, hcB₂, hn₂, heo] + omega + have hidx' : min (List.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + db₂.keys + (r.end_.offset - 1).toNat) db₂.keys.length - + List.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) db₂.keys = t := by + simpa [cnt, eo] using hidx + rw [hidx'] + have htake : (res.keys ++ [C]).take t = res.keys := by + rw [List.take_append_of_le_length (le_of_eq htdef)] + rw [htdef] + exact List.take_length + exact htake + have hcomp₂ : RangeRead.Compatible db₂ r res := by + refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck₂] + have hk_db₂ : k ∉ db₂.keys := by + rw [hdb₂] + intro hmem + rcases List.mem_append.mp hmem with hmem' | hmemC + · rcases List.mem_append.mp hmem' with hmemH | hmemR + · exact hkH hmemH + · exact absurd (hall_ge k hmemR) (not_le.mpr hkB) + · rw [List.mem_singleton] at hmemC + have hkC : k = C := hmemC + have h1 : (k : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le hkB (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hkC] at h1 + exact (lt_irrefl (C : WithTop Key)) h1 + have hcE₁ : cnt (db₂.insert k hk_db₂) r.end_.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk_db₂ (v := r.end_.key) (not_lt.mpr hkE)] + exact hcE₂ + have hcB₁ : cnt (db₂.insert k hk_db₂) r.begin.key = s + 1 := by + rw [cnt_insert_lt db₂ k hk_db₂ (v := r.begin.key) hkB, hcB₂] + have hn₁ : (db₂.insert k hk_db₂).keys.length = s + t + 2 := by + rw [DB.insert_keys, List.orderedInsert_length, hn₂] + have hfil₁ : (db₂.insert k hk_db₂).keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = res.keys ++ [C] := by + have hsingle_filter : List.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) (k :: db₂.keys) = + db₂.keys.filter (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) := by + rw [List.filter_cons] + have hkf : decide (r.begin.key ≤ (k : WithTop Key)) = false := by + rw [decide_eq_false_iff_not] + exact not_le.mpr hkB + rw [hkf] + rfl + have hperm : List.Perm ((List.orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))) + (db₂.keys.filter (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))) := by + have h := (List.perm_orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) + rwa [hsingle_filter] at h + have hs₁ : ((List.orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))).SortedLT := + List.sortedLT_iff_pairwise.mpr ((db₂.insert k hk_db₂).sorted.pairwise.filter _) + have hs₂ : (db₂.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))).SortedLT := + List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + rw [DB.insert_keys] + exact (List.SortedLT.eq_of_mem_iff hs₁ hs₂ (fun x => hperm.mem_iff)).trans hfil₂ + have hck₁ : RangeRead.completeKeys (db₂.insert k hk_db₂) r = res.keys.take (t - 1) := by + rw [completeKeys_eq_filter_take_of_offset_one hrev hbegin (by omega : 1 ≤ r.end_.offset), + hfil₁] + have hidx : min (cnt (db₂.insert k hk_db₂) r.end_.key + eo) + (db₂.insert k hk_db₂).keys.length - + cnt (db₂.insert k hk_db₂) r.begin.key = t - 1 := by + rw [hcE₁, hcB₁, hn₁, heo] + omega + have hidx' : min (List.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + (db₂.insert k hk_db₂).keys + (r.end_.offset - 1).toNat) + (db₂.insert k hk_db₂).keys.length - + List.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) + (db₂.insert k hk_db₂).keys = t - 1 := by + simpa [cnt, eo] using hidx + rw [hidx'] + rw [List.take_append_of_le_length (by omega : t - 1 ≤ res.keys.length)] + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk_db₂) r res := by + intro hc + rw [compatible_iff_truncated_keys (db := db₂.insert k hk_db₂) hrev hmore hne] at hc + rw [hck₁] at hc + have hf : List.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) + (res.keys.take (t - 1)) = res.keys.take (t - 1) := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_of_mem_take hx) + rw [hf] at hc + have heq : res.keys.take (t - 1) = res.keys := hc.2 + have hlen := congrArg List.length heq + rw [List.length_take, htdef] at hlen + have htpos : 0 < t := by rw [htdef]; exact List.length_pos_of_ne_nil hne + omega + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₂ (db₂.insert k hk_db₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk_db₂ hkcr).symm + exact hnot₁ ((hs db₂ (db₂.insert k hk_db₂) hagree).mp hcomp₂) + + +/-- A `WithTop Key` that is not `⊤` is a coerced genuine key. -/ +private theorem begin_key_exists_coe {r : RangeRead} + (h : r.begin.key ≠ (⊤ : WithTop Key)) : + ∃ v : Key, r.begin.key = (↑v : WithTop Key) := by + cases hk : r.begin.key with + | top => exact absurd hk h + | coe v => exact ⟨v, rfl⟩ + +/-- **Forward truncated crossed closed form, small-gap branch.** If the gap +`[end_.key, begin_.key)` holds fewer than the remaining overshoot `e - |res|` keys, +the minimal range is `[end_.key, keyAfter (last reported))`. -/ +theorem minimal_truncatedForward_of_crossed {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hsmall : (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) < + intervalCard r.end_.key r.begin.key) : + Minimal r res ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨?_, ?_⟩ + · have hs := sound_truncatedRange_of_crossed_E (r := r) (res := res) + hrev hmore hne hbegin hcross hend + rwa [hlast] at hs + · intro k hkE hklt + have hklast : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hklt) + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_of_mem_gap_crossed_truncated hread hmore hrev hne hbegin hcross hend + hsmall hkE hkB + · have hkBle : r.begin.key ≤ (k : WithTop Key) := not_lt.mp hkB + obtain ⟨db₀, hcomp₀⟩ := hread + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + have htake := completeKeys_eq_filter_take_of_offset_one (db := db₀) hrev hbegin + (by omega : 1 ≤ r.end_.offset) + have hmem : x ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hx + rw [htake] at hmem + exact decide_eq_true_eq.mp (List.mem_filter.mp (List.mem_of_mem_take hmem)).2 + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := by + obtain ⟨x, hx⟩ : ∃ x, x ∈ res.keys := by + cases h : res.keys with + | nil => exact absurd h hne + | cons a tl => exact ⟨a, by simp⟩ + have h1 : r.begin.key ≤ (x : WithTop Key) := hall_ge x hx + intro htop + rw [htop] at h1 + exact not_le.mpr (WithTop.coe_lt_top x) h1 + obtain ⟨v, hv⟩ := begin_key_exists_coe hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp₀⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hkBle) + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + exact hbegin_nec.of_between hlast_nec hvk hklast + + +/-- **Large-gap branch minimality for the forward truncated crossed read.** When the +gap `[end_.key, begin_.key)` holds at least the remaining overshoot, the smaller range +`[begin_.key, keyAfter (last reported))` is minimal. -/ +theorem minimal_truncatedRange_of_crossed_B {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hbig : intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞)) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨?_, ?_⟩ + · have hs := sound_truncatedRange_of_crossed_B (r := r) (res := res) + hrev hmore hne hbegin hcross hend hbig + rwa [hlast] at hs + · intro k hkB hklt + have hklast : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hklt) + obtain ⟨db₀, hcomp₀⟩ := hread + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + have htake := completeKeys_eq_filter_take_of_offset_one (db := db₀) hrev hbegin + (by omega : 1 ≤ r.end_.offset) + have hmem : x ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hx + rw [htake] at hmem + exact decide_eq_true_eq.mp (List.mem_filter.mp (List.mem_of_mem_take hmem)).2 + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := by + obtain ⟨x, hx⟩ : ∃ x, x ∈ res.keys := by + cases h : res.keys with + | nil => exact absurd h hne + | cons a tl => exact ⟨a, by simp⟩ + have h1 : r.begin.key ≤ (x : WithTop Key) := hall_ge x hx + intro htop + rw [htop] at h1 + exact not_le.mpr (WithTop.coe_lt_top x) h1 + obtain ⟨v, hv⟩ := begin_key_exists_coe hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp₀⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hkB) + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + exact hbegin_nec.of_between hlast_nec hvk hklast + +/-- The forward truncated crossed closed form: the small-gap branch keeps the end key, +the large-gap branch starts at the begin key. -/ +noncomputable def truncatedForwardClosed (r : RangeRead) (res : Result) : ConflictRange := + if intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) then + truncatedRange r (res.keys.getLastD ([] : Key)) + else + ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ + +/-- The forward truncated crossed closed form is minimal on the whole region. -/ +theorem minimal_truncatedForwardClosed_of_crossed {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) : + Minimal r res (truncatedForwardClosed r res) := by + unfold truncatedForwardClosed + by_cases hbig : intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) + · rw [if_pos hbig] + exact minimal_truncatedRange_of_crossed_B hread hmore hrev hne hbegin hcross hend hbig + · rw [if_neg hbig] + exact minimal_truncatedForward_of_crossed hread hmore hrev hne hbegin hcross hend + (not_le.mp hbig) + +/-- The forward truncated crossed closed form denotes the same key set as the general +conflict range. -/ +theorem truncatedForwardClosed_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.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + (truncatedForwardClosed r res).toSet = (conflictRangeGeneral r res).toSet := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + exact (minimal_truncatedForwardClosed_of_crossed hread' hmore hrev + (RangeRead.compatible_ne_nil_of_more hcomp₀ hmore) hbegin hcross hend).toSet_eq + (conflictRangeGeneral_minimal hread') + +/-- The same identification with the executable algorithm. -/ +theorem truncatedForwardClosed_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.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + (truncatedForwardClosed r res).toSet = (simpleConflictRange r res).toSet := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + exact (minimal_truncatedForwardClosed_of_crossed hread' hmore hrev + (RangeRead.compatible_ne_nil_of_more hcomp₀ hmore) hbegin hcross hend).toSet_eq + (simpleConflictRange_minimal hread') + +end FormalConflictRange diff --git a/FormalConflictRange/CrossedTruncatedGeneral.lean b/FormalConflictRange/CrossedTruncatedGeneral.lean new file mode 100644 index 0000000..f5f7678 --- /dev/null +++ b/FormalConflictRange/CrossedTruncatedGeneral.lean @@ -0,0 +1,1112 @@ +import FormalConflictRange.CrossedGeneral + +open FormalConflictRange + +namespace FormalConflictRange + +/-- Slicing a sorted list at `b` and taking `t` elements: the filter up to the last +taken element is exactly the first `b + t` elements. -/ +theorem filter_le_getElem_eq_take {A : List Key} (hAs : A.SortedLT) {b t : ℕ} + (ht : 0 < t) (hbt : b + t ≤ A.length) : + A.filter (fun k : Key => decide (k ≤ A[b + t - 1]'(by omega))) = A.take (b + t) := by + have hidx : b + t - 1 < A.length := by omega + have hgne : A.take (b + t) ≠ [] := + List.ne_nil_of_length_pos (by rw [List.length_take]; omega) + have hglast : (A.take (b + t)).getLast hgne = A[b + t - 1]'hidx := by + rw [List.getLast_eq_getElem, List.getElem_take] + simp only [List.length_take, Nat.min_eq_left hbt] + have h := filter_le_getLast_eq_of_prefix (α := Key) (g := A.take (b + t)) (W := A) + hAs.pairwise (List.take_prefix (b + t) A) hgne + rw [hglast] at h + exact h + +theorem completeKeys_eq_filter_ge_drop_take {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = + ((db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).drop + (r.begin.offset - 1).toNat).take + (min (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db.keys.length + - (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) + + (r.begin.offset - 1).toNat)) := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hAeq : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = l.drop p := by + rw [hp] + have hdrop := filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + rw [completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend, ← hl, ← hp, ← hb] + rw [hAeq, List.drop_drop] + +/-- The last element of `(A.drop b).take t` is `A[b+t-1]`. -/ +theorem getLast_take_drop {A : List Key} {b t : ℕ} {res : List Key} + (ht : 0 < t) (hres : res = (A.drop b).take t) (hbt : b + t ≤ A.length) : + res.getLastD [] = A[b + t - 1]'(by omega) := by + have hne : res ≠ [] := by + rw [hres] + apply List.ne_nil_of_length_pos + rw [List.length_take, Nat.min_eq_left] + · omega + · rw [List.length_drop]; omega + have hlastD : res.getLastD [] = res.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlastD] + have hne' : (A.drop b).take t ≠ [] := by rw [← hres]; exact hne + calc res.getLast hne + = ((A.drop b).take t).getLast hne' := List.getLast_congr hne hne' hres + _ = A[b + t - 1]'(by omega) := by + rw [List.getLast_eq_getElem, List.getElem_take] + simp only [List.length_take, List.length_drop, Nat.min_eq_left (by omega : t ≤ A.length - b)] + rw [List.getElem_drop] + congr 1 + omega + +end FormalConflictRange + +namespace FormalConflictRange + +/-- In the crossed forward truncated B-branch, agreement on the truncated range +forced the reported last to stay strictly below the resolved end. -/ +theorem crossed_truncated_hlr_of_drop_gap {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hcross : r.end_.key < r.begin.key) (hne : res.keys ≠ []) + (hgap : (between db r.end_.key r.begin.key).length + (r.begin.offset - 1).toNat + + res.keys.length ≤ (r.end_.offset - 1).toNat) + (hdrop : ((truncatedRange r (res.keys.getLast hne)).keys db).drop + (r.begin.offset - 1).toNat = res.keys) : + ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set t := res.keys.length with ht + set last := res.keys.getLast hne with hlast + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hA + set S := A.filter (fun k : Key => decide (k ≤ last)) with hS + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hAs : A.SortedLT := by + rw [hA] + exact List.sortedLT_iff_pairwise.mpr (hls.pairwise.filter _) + have htrunc : (truncatedRange r last).keys db = S := by + rw [truncatedRange_keys_eq_filter_le] + have hdrop' : S.drop b = res.keys := by + rw [← htrunc] + simpa [hb] using hdrop + have htpos : 0 < t := by rw [ht]; exact List.length_pos_of_ne_nil hne + have hSlen : S.length = b + t := by + have := congrArg List.length hdrop' + rw [List.length_drop, ← ht] at this + omega + have hStake : S = A.take (b + t) := by + rw [hS, filter_le_eq_take_countP hAs last] + congr 1 + rw [List.countP_eq_length_filter] + simpa [S] using hSlen + have hAbt : b + t ≤ A.length := by + rw [hStake, List.length_take] at hSlen + omega + have hresdrop : res.keys = (A.drop b).take t := by + rw [← hdrop', hStake, List.drop_take] + congr 1 + omega + have hlastD : res.keys.getLastD [] = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastA : last = A[b + t - 1]'(by omega) := by + rw [hlast, ← hlastD] + exact getLast_take_drop htpos hresdrop hAbt + set g := (between db r.end_.key r.begin.key).length with hgdef + have hcnt : p = cE + g := by + rw [hp, hcE, hgdef] + simpa [cnt, hl] using cnt_eq_add_between (db := db) (le_of_lt hcross) + have hA_eq : A = l.drop p := by + rw [hA, hp] + have hdrop := filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hjlt : p + (b + t - 1) < l.length := by + have := hAbt + rw [hA_eq, List.length_drop] at this + omega + have hlast_l : last = l[p + (b + t - 1)]'hjlt := by + rw [hlastA] + have hidx : b + t - 1 < A.length := by omega + have h1 : A[b + t - 1]'hidx = (l.drop p)[b + t - 1]'(by rw [List.length_drop]; omega) := + List.getElem_of_eq hA_eq hidx + rw [h1, List.getElem_drop] + have hgbt : g + (t + b) ≤ e := by + have h := hgap + omega + have hj_anchor : p + (b + t - 1) < (r.end_.anchor db).toNat := by + rw [anchor_toNat_eq (s := r.end_) hend, ← hl, ← hcE, hcnt] + have hble : b + t - 1 < t + b := by omega + omega + have hj_clamp : p + (b + t - 1) < clampIndex db r.end_ := by + unfold clampIndex + exact Nat.lt_min.mpr ⟨hj_anchor, hjlt⟩ + rw [hlast_l] + exact (getElem_lt_resolve_iff (db := db) (s := r.end_) + (j := p + (b + t - 1)) hjlt).mpr hj_clamp + +end FormalConflictRange + +namespace FormalConflictRange + +/-- A compatible crossed truncated result bounds the number of stored keys in the +selector gap: `gap + begin-offset-padding + reported-length ≤ end-overshoot`. -/ +theorem compatible_gap_bound {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hcross : r.end_.key < r.begin.key) + (hc : RangeRead.Compatible db r res) : + (between db r.end_.key r.begin.key).length + (r.begin.offset - 1).toNat + + res.keys.length ≤ (r.end_.offset - 1).toNat := by + have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hc + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set t := res.keys.length with ht + set g := (between db r.end_.key r.begin.key).length with hg + have hck := completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend + rw [← hl, ← hp, ← hcE, ← hb, ← he] at hck + have ht_le : t ≤ min (cE + e) l.length - (p + b) := by + have := hpre.length_le + rw [hck, List.length_take, ← ht] at this + exact le_trans this (Nat.min_le_left _ _) + have hcnt : p = cE + g := by + rw [hp, hcE, hg] + simpa [cnt, hl] using cnt_eq_add_between (db := db) (le_of_lt hcross) + have hsub : cE + e - (p + b) = e - g - b := by + rw [hcnt, add_assoc, Nat.add_sub_add_left, Nat.sub_sub] + have hminle : min (cE + e) l.length - (p + b) ≤ e - g - b := by + rw [← hsub] + exact Nat.sub_le_sub_right (Nat.min_le_left _ _) _ + have ht_le' : t ≤ e - g - b := le_trans ht_le hminle + have htpos : 0 < t := by rw [ht]; exact List.length_pos_of_ne_nil hne + have hgbe : g + b ≤ e := by + by_contra h + have hz : e - g - b = 0 := by omega + omega + have hfinal : g + b + t ≤ e := by + calc g + b + t = t + (g + b) := by omega + _ ≤ (e - (g + b)) + (g + b) := by + have h1 : t ≤ e - (g + b) := by rwa [Nat.sub_sub] at ht_le' + exact Nat.add_le_add_right h1 _ + _ = e := Nat.sub_add_cancel hgbe + simpa [hg, hb, ht, he] using hfinal + +end FormalConflictRange + +namespace FormalConflictRange + +/-- The hcard form of the crossed truncated height lemma, derived from the actual +gap-count form. -/ +theorem crossed_truncated_hlr_of_drop {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hcross : r.end_.key < r.begin.key) (hne : res.keys ≠ []) + (hcard : intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length - (r.begin.offset - 1).toNat : ℕ) : ℕ∞)) + (hdrop : ((truncatedRange r (res.keys.getLast hne)).keys db).drop + (r.begin.offset - 1).toNat = res.keys) : + ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := by + refine crossed_truncated_hlr_of_drop_gap hrev hbegin hend hcross hne ?_ hdrop + set g := (between db r.end_.key r.begin.key).length with hg + set t := res.keys.length with ht + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hgcard : ((g : ℕ) : ℕ∞) ≤ (((e - t - b : ℕ)) : ℕ∞) := by + refine le_trans ?_ (by simpa [e, t, b, g] using hcard) + simpa [g] using between_length_le_intervalCard (db := db) (le_of_lt hcross) + have hgle0 : g ≤ e - t - b := by exact_mod_cast hgcard + have hgle : g ≤ e - (t + b) := by rwa [Nat.sub_sub] at hgle0 + have htb : t + b ≤ e := by + by_contra h + have hzero : e - t - b = 0 := by omega + have hle0 : intervalCard r.end_.key r.begin.key ≤ 0 := by simpa [hzero] using hcard + have hzero2 : intervalCard r.end_.key r.begin.key = 0 := le_antisymm hle0 zero_le + exact absurd (intervalCard_eq_zero_iff.mp hzero2) (not_le.mpr hcross) + have hgbt : g + (t + b) ≤ e := by + calc g + (t + b) ≤ (e - (t + b)) + (t + b) := Nat.add_le_add_right hgle _ + _ = e := Nat.sub_add_cancel htb + omega + +end FormalConflictRange + + +open FormalConflictRange + +namespace FormalConflictRange + +/-- General crossed complete keys filtered at or below the last reported key. -/ +theorem completeKeys_filter_le_eq_crossed_general {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) (hBlast : r.begin.key ≤ (last : WithTop Key)) : + (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + (((db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).take + (min ((r.end_.offset - 1).toNat - (between db r.end_.key r.begin.key).length) + ((db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length)).drop + (r.begin.offset - 1).toNat := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + set C := A.filter (fun k : Key => decide (k ≤ last)) with hCdef + set q := A.countP (fun k : Key => decide (k ≤ last)) with hqdef + set gap := (between db r.end_.key r.begin.key).length with hgapdef + set K := min (cE + e) l.length - (p + b) with hK + set L := min (e - gap) q with hL + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hAs : A.SortedLT := by + rw [hAdef] + exact List.sortedLT_iff_pairwise.mpr (hls.pairwise.filter _) + have hA : A = l.drop p := by + rw [hAdef, hp] + have hdrop := filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hCtake : C = A.take q := by + rw [hCdef, hqdef] + exact filter_le_eq_take_countP hAs last + have hqA : q ≤ A.length := by rw [hqdef]; exact List.countP_le_length + have hqC : C.length = q := by + rw [hCtake, List.length_take] + exact Nat.min_eq_left hqA + have hp_le : p ≤ l.length := by rw [hp]; exact List.countP_le_length + have hcB : p = cE + gap := by + rw [hp, hcE, hgapdef] + simpa [cnt, hl] using cnt_eq_add_between (db := db) hEB + have hqN : q + p ≤ l.length := by + have := hqA + rw [hA, List.length_drop] at this + omega + -- filter of a drop of `A` + have hfilter_drop : (A.drop b).filter (fun k : Key => decide (k ≤ last)) = + (A.drop b).take (q - b) := by + have hsplit : A.filter (fun k : Key => decide (k ≤ last)) = + (A.take b).filter (fun k : Key => decide (k ≤ last)) ++ + (A.drop b).filter (fun k : Key => decide (k ≤ last)) := by + conv_lhs => rw [← List.take_append_drop b A] + rw [List.filter_append] + have htakeb : (A.take b).filter (fun k : Key => decide (k ≤ last)) = + A.take (min b q) := + filter_take_eq_take_min (p := fun k : Key => decide (k ≤ last)) hCtake b + have hCtake' : A.filter (fun k : Key => decide (k ≤ last)) = A.take q := hCtake + rw [hCtake', htakeb] at hsplit + have hsplit2 : A.take q = A.take (min b q) ++ (A.take q).drop (min b q) := by + conv_lhs => rw [← List.take_append_drop (min b q) (A.take q)] + rw [List.take_take, Nat.min_eq_left (Nat.min_le_right b q)] + rw [hsplit2] at hsplit + have hdrop : (A.drop b).filter (fun k : Key => decide (k ≤ last)) = + (A.take q).drop (min b q) := (List.append_cancel_left hsplit).symm + have htakeq : (A.take q).drop (min b q) = (A.drop b).take (q - b) := by + by_cases hbq : b ≤ q + · rw [Nat.min_eq_left hbq, List.drop_take] + · have hbq' : q ≤ b := by omega + rw [Nat.min_eq_right hbq', Nat.sub_eq_zero_of_le hbq', List.take_zero] + exact List.drop_eq_nil_of_le (by rw [List.length_take]; exact Nat.min_le_left _ _) + exact hdrop.trans htakeq + have htake_filter : ((A.drop b).take K).filter (fun k : Key => decide (k ≤ last)) = + (A.drop b).take (min K (q - b)) := + filter_take_eq_take_min (p := fun k : Key => decide (k ≤ last)) hfilter_drop K + have harith : min K (q - b) = L - b := by + rw [hK, hL] + omega + have hAtake : A.take L = C.take L := by + rw [hCtake, List.take_take] + congr 1 + exact (Nat.min_eq_left (Nat.min_le_right (e - gap) q)).symm + rw [completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend] + rw [← hl, ← hp, ← hcE, ← hb, ← he] + have hdropA : List.drop (p + b) l = A.drop b := by rw [hA, List.drop_drop] + rw [hdropA, show min (cE + e) l.length - (p + b) = K from rfl] + rw [htake_filter, harith] + rw [← List.drop_take, hAtake, hqC] + +end FormalConflictRange + + +namespace FormalConflictRange + +private theorem crossedTrunc_keys_lo_keyAfter_eq_filter_le {db : DB} {lo : WithTop Key} {last : Key} : + (⟨lo, (keyAfter last : Key)⟩ : ConflictRange).keys db = + (db.keys.filter (fun k : Key => decide (lo ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + unfold ConflictRange.keys + rw [show db.keys.filter (fun (k : Key) => decide (lo ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + db.keys.filter (fun (k : Key) => + decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key)) && + decide (lo ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter] + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + +private theorem crossedTrunc_filter_ge_last_eq_filter_of_keys_eq {db : DB} {E B : WithTop Key} {last : Key} + (hEB : E ≤ B) (hBlast : B ≤ (last : WithTop Key)) : + (db.keys.filter (fun k : Key => decide (B ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + ((⟨E, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide (B ≤ (k : WithTop Key))) := by + rw [crossedTrunc_keys_lo_keyAfter_eq_filter_le] + simp only [List.filter_filter] + apply List.filter_congr + intro k _ + simp only [← Bool.decide_and] + rw [decide_eq_decide] + constructor + · rintro ⟨hl, hB⟩ + exact ⟨hB, hl, le_trans hEB hB⟩ + · rintro ⟨hB, hl, _⟩ + exact ⟨hl, hB⟩ + +private theorem crossedTrunc_between_eq_filter_of_keys_eq {db : DB} {E B : WithTop Key} {last : Key} + (hEB : E ≤ B) (hBlast : B ≤ (last : WithTop Key)) : + between db E B = + ((⟨E, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide ((k : WithTop Key) < B)) := by + rw [crossedTrunc_keys_lo_keyAfter_eq_filter_le] + simp only [List.filter_filter] + unfold between + rw [show db.keys.filter (fun k : Key => decide (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B)) = + db.keys.filter (fun k : Key => decide ((k : WithTop Key) < B) && decide (E ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + apply List.filter_congr + intro k _ + simp only [← Bool.decide_and] + rw [decide_eq_decide] + constructor + · rintro ⟨hkB, hE⟩ + have hklt : (k : WithTop Key) < (last : WithTop Key) := lt_of_lt_of_le hkB hBlast + exact ⟨hkB, le_of_lt (WithTop.coe_lt_coe.mp hklt), hE⟩ + · rintro ⟨hkB, _, hE⟩ + exact ⟨hkB, hE⟩ + +/-- A compatible crossed truncated result reports only keys at or above `begin_.key`, +at any positive begin offset. -/ +theorem le_begin_key_of_compatible_crossed_general {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hne : res.keys ≠ []) + (hc : RangeRead.Compatible db r res) : + r.begin.key ≤ (res.keys.getLast hne : WithTop Key) := by + have hmem : res.keys.getLast hne ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hc).subset (List.getLast_mem hne) + exact le_trans (key_le_resolve_of_one_le (db := db) (s := r.begin) hbegin) + (RangeRead.mem_completeKeys.mp hmem).2.1 + +/-- Agreement on `[r.end_.key, keyAfter last)` fixes the complete keys filtered at or +below `last`, for any positive begin offset on a crossed read with `end_.key ≤ +begin_.key`. -/ +theorem completeKeys_filter_le_eq_of_agree_crossed_general {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) (hBlast : r.begin.key ≤ (last : WithTop Key)) + {db₁ db₂ : DB} + (hagree : (⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db₁ = + (⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db₂) : + (RangeRead.completeKeys db₁ r).filter (fun k : Key => decide (k ≤ last)) = + (RangeRead.completeKeys db₂ r).filter (fun k : Key => decide (k ≤ last)) := by + let C : DB → List Key := fun db => + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) + let gap : DB → ℕ := fun db => (between db r.end_.key r.begin.key).length + have hC (db : DB) : C db = + ((⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) := + crossedTrunc_filter_ge_last_eq_filter_of_keys_eq (db := db) hEB hBlast + have hG (db : DB) : between db r.end_.key r.begin.key = + ((⟨r.end_.key, (keyAfter last : Key)⟩ : ConflictRange).keys db).filter + (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := + crossedTrunc_between_eq_filter_of_keys_eq (db := db) hEB hBlast + have hCeq : C db₁ = C db₂ := by + rw [hC db₁, hC db₂, hagree] + have hGeq : gap db₁ = gap db₂ := by + unfold gap + rw [hG db₁, hG db₂, hagree] + have hformula (db : DB) : + (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + ((C db).take (min ((r.end_.offset - 1).toNat - gap db) (C db).length)).drop + (r.begin.offset - 1).toNat := by + simpa [C, gap] using completeKeys_filter_le_eq_crossed_general + (db := db) (r := r) (last := last) hrev hbegin hend hEB hBlast + calc + (RangeRead.completeKeys db₁ r).filter (fun k : Key => decide (k ≤ last)) + = ((C db₁).take (min ((r.end_.offset - 1).toNat - gap db₁) (C db₁).length)).drop + (r.begin.offset - 1).toNat := hformula db₁ + _ = ((C db₂).take (min ((r.end_.offset - 1).toNat - gap db₂) (C db₂).length)).drop + (r.begin.offset - 1).toNat := by rw [hCeq, hGeq] + _ = (RangeRead.completeKeys db₂ r).filter (fun k : Key => decide (k ≤ last)) := + (hformula db₂).symm + +/-- **E-branch soundness for the forward truncated crossed read at any positive begin +offset.** Agreement on `[end_.key, keyAfter (last reported))` fixes the reported prefix +of the complete keys. -/ +theorem sound_truncatedRange_of_crossed_E_general {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + Sound r res ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + intro db₁ db₂ hagree + constructor + · intro hc₁ + have hBlast := le_begin_key_of_compatible_crossed_general (db := db₁) hrev hbegin hne hc₁ + have hceq := completeKeys_filter_le_eq_of_agree_crossed_general (r := r) + (last := res.keys.getLast hne) hrev hbegin (by omega) (le_of_lt hcross) hBlast hagree + rw [compatible_iff_truncated_keys (db := db₁) hrev hmore hne] at hc₁ + rw [compatible_iff_truncated_keys (db := db₂) hrev hmore hne] + refine ⟨hc₁.1, ?_⟩ + rw [← hceq] + exact hc₁.2 + · intro hc₂ + have hBlast := le_begin_key_of_compatible_crossed_general (db := db₂) hrev hbegin hne hc₂ + have hceq := completeKeys_filter_le_eq_of_agree_crossed_general (r := r) + (last := res.keys.getLast hne) hrev hbegin (by omega) (le_of_lt hcross) hBlast hagree + rw [compatible_iff_truncated_keys (db := db₂) hrev hmore hne] at hc₂ + rw [compatible_iff_truncated_keys (db := db₁) hrev hmore hne] + refine ⟨hc₂.1, ?_⟩ + rw [hceq] + exact hc₂.2 + +end FormalConflictRange + +namespace FormalConflictRange + +/-- For a sorted key list, dropping `b` from the prefix at or below `last` is the +same as filtering the dropped tail at or below `last`. -/ +theorem filter_drop_eq_drop_filter_le {A : List Key} (hAs : A.SortedLT) (last : Key) (b : ℕ) : + (A.filter (fun k : Key => decide (k ≤ last))).drop b = + (A.drop b).filter (fun k : Key => decide (k ≤ last)) := by + set q := A.countP (fun k : Key => decide (k ≤ last)) with hq + have hqA : q ≤ A.length := by rw [hq]; exact List.countP_le_length + have hC : A.filter (fun k : Key => decide (k ≤ last)) = A.take q := by + rw [hq]; exact filter_le_eq_take_countP hAs last + have hdrop : (A.drop b).filter (fun k : Key => decide (k ≤ last)) = + (A.take q).drop (min b q) := by + have hsplit : A.filter (fun k : Key => decide (k ≤ last)) = + (A.take b).filter (fun k : Key => decide (k ≤ last)) ++ + (A.drop b).filter (fun k : Key => decide (k ≤ last)) := by + conv_lhs => rw [← List.take_append_drop b A] + rw [List.filter_append] + have htakeb : (A.take b).filter (fun k : Key => decide (k ≤ last)) = + A.take (min b q) := + filter_take_eq_take_min (p := fun k : Key => decide (k ≤ last)) hC b + rw [hC, htakeb] at hsplit + have hsplit2 : A.take q = A.take (min b q) ++ (A.take q).drop (min b q) := by + conv_lhs => rw [← List.take_append_drop (min b q) (A.take q)] + rw [List.take_take, Nat.min_eq_left (Nat.min_le_right b q)] + rw [hsplit2] at hsplit + exact (List.append_cancel_left hsplit).symm + rw [hC, hdrop] + by_cases hbq : b ≤ q + · rw [Nat.min_eq_left hbq, List.drop_take] + · have hbq' : q ≤ b := by omega + rw [Nat.min_eq_right hbq'] + rw [List.drop_eq_nil_of_le (by rw [List.length_take, Nat.min_eq_left hqA]; exact hbq'), + List.drop_eq_nil_of_le (by rw [List.length_take, Nat.min_eq_left hqA])] + +/-- Compatibility with a truncated crossed read is equivalent to the limit and the +begin-offset-dropped truncated range storing exactly the reported keys, provided the +last reported key is strictly below the resolved end. -/ +theorem compatible_iff_truncated_drop_of_hlr {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hmore : res.more = true) + (hne : res.keys ≠ []) + (hlr : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_) : + RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + ((truncatedRange r (res.keys.getLast hne)).keys db).drop + (r.begin.offset - 1).toNat = res.keys := by + rw [compatible_iff_truncated_keys (db := db) hrev hmore hne] + have hbridge := completeKeys_filter_le_drop (db := db) (r := r) + (last := res.keys.getLast hne) hrev hbegin hlr + constructor + · rintro ⟨hlim, hfil⟩ + exact ⟨hlim, hbridge.symm.trans hfil⟩ + · rintro ⟨hlim, hdrop⟩ + exact ⟨hlim, hbridge.trans hdrop⟩ + +/-- **B-branch soundness for the forward truncated crossed read, at any positive begin +offset.** When the gap `[end_.key, begin_.key)` has at least the remaining overshoot +`e - t - (begin.offset - 1)` keys, the truncated range `[begin.key, keyAfter (last +reported))` is sound. -/ +theorem sound_truncatedRange_of_crossed_B_general {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.end_.key < r.begin.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length - (r.begin.offset - 1).toNat : ℕ) : ℕ∞)) : + Sound r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + set b := (r.begin.offset - 1).toNat with hbdef + intro db₁ db₂ hagree + have keyF (db : DB) (hc : RangeRead.Compatible db r res) : + RangeRead.withinLimit r res.keys ∧ + ((truncatedRange r (res.keys.getLast hne)).keys db).drop b = res.keys := by + refine ⟨hc.1, ?_⟩ + set l := db.keys with hl + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hAs : A.SortedLT := by + rw [hAdef] + exact List.sortedLT_iff_pairwise.mpr (hls.pairwise.filter _) + have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hc + have hck : RangeRead.completeKeys db r = (A.drop b).take + (min (l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) l.length + - (l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) + b)) := by + simpa [A, b, hl] using completeKeys_eq_filter_ge_drop_take (db := db) (r := r) + hrev hbegin (by omega) + have hpreA : res.keys <+: A.drop b := by + rw [hck] at hpre + exact List.IsPrefix.trans hpre (List.take_prefix _ _) + have hAsDrop : (A.drop b).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := b) hAs.pairwise) + have hfil : (A.drop b).filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = res.keys := + filter_le_getLast_eq_of_prefix hAsDrop.pairwise hpreA hne + have hAeq : db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = A := by + rw [hAdef, hl] + rw [truncatedRange_keys_eq_filter_le, hAeq, filter_drop_eq_drop_filter_le hAs _ b] + exact hfil + have hlrOf (db : DB) + (hdrop : ((truncatedRange r (res.keys.getLast hne)).keys db).drop b = res.keys) : + ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + crossed_truncated_hlr_of_drop hrev hbegin (by omega) hcross hne (by simpa [b] using hcard) hdrop + constructor + · intro hc₁ + have hF₁ := keyF db₁ hc₁ + have hdrop₂ : ((truncatedRange r (res.keys.getLast hne)).keys db₂).drop b = res.keys := by + rw [← hagree]; exact hF₁.2 + exact (compatible_iff_truncated_drop_of_hlr (db := db₂) hrev hbegin hmore hne + (hlrOf db₂ hdrop₂)).mpr ⟨hc₁.1, hdrop₂⟩ + · intro hc₂ + have hF₂ := keyF db₂ hc₂ + have hdrop₁ : ((truncatedRange r (res.keys.getLast hne)).keys db₁).drop b = res.keys := by + rw [hagree]; exact hF₂.2 + exact (compatible_iff_truncated_drop_of_hlr (db := db₁) hrev hbegin hmore hne + (hlrOf db₁ hdrop₁)).mpr ⟨hc₂.1, hdrop₁⟩ + +end FormalConflictRange + +namespace FormalConflictRange + +/-- Extract the begin-offset padding `P` from a compatible truncated crossed read. -/ +theorem crossed_pad_truncated_general {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hc : RangeRead.Compatible db r res) : + ∃ P : List Key, P.length = (r.begin.offset - 1).toNat ∧ P.SortedLT ∧ + (∀ x ∈ P, r.begin.key ≤ (x : WithTop Key)) ∧ + (∀ x ∈ P, ∀ y ∈ res.keys, x < y) ∧ + (P ++ res.keys).SortedLT := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + set K := min (l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) l.length - (p + b) with hK + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hA : A = l.drop p := by + rw [hAdef] + have hnot : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + rw [hnot, filter_not_lt_eq_drop_countP hls r.begin.key, ← hp] + have hAs : A.SortedLT := by + rw [hA] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hAge : ∀ x ∈ A, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + rw [hAdef] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + have hck : RangeRead.completeKeys db r = (A.drop b).take K := by + rw [completeKeys_eq_drop_take_general (db := db) (r := r) hrev hbegin hend] + rw [← hl, ← hp, ← hb, hA, List.drop_drop, hK] + have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hc + have hpreA : res.keys <+: A.drop b := by + rw [hck] at hpre + exact List.IsPrefix.trans hpre (List.take_prefix _ _) + have hbA : b ≤ A.length := by + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have := hpreA.length_le + rw [List.length_drop] at this + omega + let P := A.take b + have hPdef : P = A.take b := rfl + have hPlen : P.length = b := by + rw [hPdef, List.length_take, Nat.min_eq_left hbA] + have hPs : P.SortedLT := by + rw [hPdef] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.take (i := b) + (List.sortedLT_iff_pairwise.mp hAs)) + have hPge : ∀ x ∈ P, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + exact hAge x (List.take_subset _ _ (by rw [← hPdef]; exact hx)) + have happend : A = P ++ A.drop b := by rw [hPdef, List.take_append_drop] + have hcrossA : ∀ x ∈ P, ∀ y ∈ A.drop b, x < y := by + have := List.sortedLT_append.mp (by rw [← happend]; exact hAs) + exact this.2.2 + have hP_lt_res : ∀ x ∈ P, ∀ y ∈ res.keys, x < y := by + intro x hx y hy + exact hcrossA x hx y (hpreA.subset hy) + have hresS : res.keys.SortedLT := RangeRead.compatible_sortedLT hc hrev + refine ⟨P, ?_, hPs, hPge, hP_lt_res, ?_⟩ + · rw [hPlen, hb] + · exact List.sortedLT_append.mpr ⟨hPs, hresS, hP_lt_res⟩ + +end FormalConflictRange + +namespace FormalConflictRange + +private theorem crossedTrunc_enat_succ_le_of_lt {s : ℕ} {h : ℕ∞} (hs : (s : ℕ∞) < h) : + ((s + 1 : ℕ) : ℕ∞) ≤ h := by + cases h with + | top => exact le_top + | coe n => + rw [ENat.coe_lt_coe] at hs + exact_mod_cast Nat.succ_le_of_lt hs + +/-- **Gap-key necessity for the forward truncated crossed read at any positive begin +offset.** When the gap is too small to absorb `e - t - b`, every key in +`[end_.key, begin_.key)` is necessary. -/ +theorem necessary_of_mem_gap_crossed_truncated_general {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hlt : (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) < intervalCard r.end_.key r.begin.key) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + set t : ℕ := res.keys.length with htdef + set eo : ℕ := (r.end_.offset - 1).toNat with heodef + set b : ℕ := (r.begin.offset - 1).toNat with hbdef + set s : ℕ := eo - t - b with hsdef + obtain ⟨db₀, hcomp₀⟩ := hread + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + have hmem : x ∈ RangeRead.completeKeys db₀ r := hpre₀.subset hx + exact le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) hbegin) + (RangeRead.mem_completeKeys.mp hmem).2.1 + have hlast_ge_B : r.begin.key ≤ (res.keys.getLast hne : WithTop Key) := + hall_ge _ (List.getLast_mem hne) + have hgap : (between db₀ r.end_.key r.begin.key).length + b + t ≤ eo := by + have := compatible_gap_bound (db := db₀) hrev hmore hne hbegin (by omega) hcross hcomp₀ + simpa [hbdef, htdef, heodef] using this + have hbt : b + t ≤ eo := by omega + have hse : eo = s + t + b := by rw [hsdef]; omega + have hcard : ((s + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := + crossedTrunc_enat_succ_le_of_lt (s := s) + (by simpa [s, htdef, heodef, hbdef] using hlt) + obtain ⟨dbH, hHrange, hkH, hHlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := s) hcard + have hHgeE : ∀ x ∈ dbH.keys, r.end_.key ≤ (x : WithTop Key) := + fun x hx => (hHrange x hx).1 + have hHltB : ∀ x ∈ dbH.keys, (x : WithTop Key) < r.begin.key := + fun x hx => (hHrange x hx).2 + obtain ⟨P, hPlen, hPs, hPge, hPlt, hPsortPR⟩ := + crossed_pad_truncated_general (db := db₀) hrev hbegin hcross (by omega) hne hcomp₀ + have hPlen' : P.length = b := by simpa [hbdef] using hPlen + have hPge' : ∀ x ∈ P, r.begin.key ≤ (x : WithTop Key) := by + intro x hx; simpa using hPge x hx + have hH_lt_P : ∀ x ∈ dbH.keys, ∀ y ∈ P, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltB x hx) (hPge' y hy)) + obtain ⟨C, hCge, hCnotin⟩ := + exists_le_not_mem (res.keys ++ dbH.keys) (res.keys.getLast hne) + have hCnotres : C ∉ res.keys := fun h => hCnotin (List.mem_append_left _ h) + have hCnotH : C ∉ dbH.keys := fun h => hCnotin (List.mem_append_right _ h) + have hCgtRes : ∀ a ∈ res.keys, a < C := by + intro a ha + have hle : a ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast ha + exact lt_of_le_of_ne (le_trans hle hCge) (fun h => hCnotres (h ▸ ha)) + have hHltC : ∀ a ∈ dbH.keys, a < C := by + intro a ha + have h1 : (a : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le (hHltB a ha) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + exact WithTop.coe_lt_coe.mp h1 + have hHP : (dbH.keys ++ P).SortedLT := + List.sortedLT_append.mpr ⟨dbH.sorted, hPs, hH_lt_P⟩ + have hHPres_lt : ∀ x ∈ (dbH.keys ++ P), ∀ y ∈ res.keys, x < y := by + intro x hx y hy + rcases List.mem_append.mp hx with hxH | hxP + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltB x hxH) (hall_ge y hy)) + · exact hPlt x hxP y hy + have hHPres : (dbH.keys ++ P ++ res.keys).SortedLT := + List.sortedLT_append.mpr ⟨hHP, hsort, hHPres_lt⟩ + have hsingle : ([C] : List Key).SortedLT := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + have hcrossList : ∀ a ∈ (dbH.keys ++ P ++ res.keys), ∀ y ∈ ([C] : List Key), a < y := by + intro a ha y hy + rw [List.mem_singleton] at hy + subst hy + rcases List.mem_append.mp ha with ha' | haR + · rcases List.mem_append.mp ha' with haH | haP + · exact hHltC a haH + · exact lt_of_lt_of_le (hPlt a haP _ (List.getLast_mem hne)) hCge + · exact hCgtRes a haR + have hsort₂ : (dbH.keys ++ P ++ res.keys ++ [C]).SortedLT := + List.sortedLT_append.mpr ⟨hHPres, hsingle, hcrossList⟩ + let db₂ : DB := ⟨dbH.keys ++ P ++ res.keys ++ [C], hsort₂⟩ + have hdb₂ : db₂.keys = dbH.keys ++ P ++ res.keys ++ [C] := rfl + have hfil₂ : db₂.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = P ++ res.keys ++ [C] := by + rw [hdb₂, List.filter_append, List.filter_append, List.filter_append] + have hHf : dbH.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_le.mpr (hHltB x hx) + have hPf : P.filter (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = P := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hPge' x hx + have hresf : res.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hall_ge x hx + have hCf : ([C] : List Key).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = [C] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge) + rw [hHf, hPf, hresf, hCf] + simp + have hcE₂ : cnt db₂ r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hdb₂] at hx + rcases List.mem_append.mp hx with hx' | hxC + · rcases List.mem_append.mp hx' with hx'' | hxR + · rcases List.mem_append.mp hx'' with hxH | hxP + · exact hHgeE x hxH + · exact le_trans (le_of_lt hcross) (hPge' x hxP) + · exact le_trans (le_of_lt hcross) (hall_ge x hxR) + · rw [List.mem_singleton] at hxC + subst hxC + exact le_trans (le_of_lt hcross) + (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + have hcB₂ : cnt db₂ r.begin.key = s := by + show db₂.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = s + rw [hdb₂, List.countP_append, List.countP_append, List.countP_append] + have hHc : dbH.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbH.keys.length := + cnt_eq_length_of_forall_lt dbH hHltB + have hPc : P.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hPge' x hx) + have hresc : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hall_ge x hx) + have hCc : ([C] : List Key).countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact not_lt.mpr (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hHc, hPc, hresc, hCc, hHlen] + simp + have hn₂ : db₂.keys.length = s + b + t + 1 := by + rw [hdb₂, List.length_append, List.length_append, List.length_append, + hHlen, hPlen', htdef, List.length_singleton] + have hdropP : (P ++ res.keys ++ [C]).drop b = res.keys ++ [C] := by + have hbP : b = P.length := hPlen'.symm + rw [hbP, List.append_assoc] + rw [List.drop_append_of_le_length le_rfl, List.drop_length] + simp + have hck₂ : RangeRead.completeKeys db₂ r = res.keys := by + rw [completeKeys_eq_filter_ge_drop_take (db := db₂) (r := r) hrev hbegin (by omega), + hfil₂, hdropP] + have hK : min (cnt db₂ r.end_.key + (r.end_.offset - 1).toNat) db₂.keys.length - + (cnt db₂ r.begin.key + b) = t := by + have hE : (r.end_.offset - 1).toNat = eo := heodef.symm + rw [hcE₂, hcB₂, hn₂, hE, hse] + rw [Nat.min_eq_left (by omega)] + omega + have hK' : min (db₂.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db₂.keys.length - + (db₂.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) + b) = t := by + simpa [cnt] using hK + rw [hK', List.take_append_of_le_length (le_of_eq htdef), htdef, List.take_length] + have hcomp₂ : RangeRead.Compatible db₂ r res := by + refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck₂] + have hk_db₂ : k ∉ db₂.keys := by + rw [hdb₂] + intro hmem + rcases List.mem_append.mp hmem with hmem' | hmemC + · rcases List.mem_append.mp hmem' with hmem'' | hmemR + · rcases List.mem_append.mp hmem'' with hmemH | hmemP + · exact hkH hmemH + · exact absurd (hPge' k hmemP) (not_le.mpr hkB) + · exact absurd (hall_ge k hmemR) (not_le.mpr hkB) + · rw [List.mem_singleton] at hmemC + have hkC : k = C := hmemC + have h1 : (k : WithTop Key) < (C : WithTop Key) := + lt_of_lt_of_le hkB (le_trans hlast_ge_B (WithTop.coe_le_coe.mpr hCge)) + rw [hkC] at h1 + exact (lt_irrefl (C : WithTop Key)) h1 + let db₁ : DB := db₂.insert k hk_db₂ + have hcE₁ : cnt db₁ r.end_.key = 0 := by + rw [show db₁ = db₂.insert k hk_db₂ from rfl, + cnt_insert_not_lt db₂ k hk_db₂ (v := r.end_.key) (not_lt.mpr hkE)] + exact hcE₂ + have hcB₁ : cnt db₁ r.begin.key = s + 1 := by + rw [show db₁ = db₂.insert k hk_db₂ from rfl, + cnt_insert_lt db₂ k hk_db₂ (v := r.begin.key) hkB, hcB₂] + have hn₁ : db₁.keys.length = s + b + t + 2 := by + rw [show db₁ = db₂.insert k hk_db₂ from rfl, DB.insert_keys, + List.orderedInsert_length, hn₂] + have hfil₁ : db₁.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) = P ++ res.keys ++ [C] := by + have hsingle_filter : List.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) (k :: db₂.keys) = + db₂.keys.filter (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) := by + rw [List.filter_cons] + have hkf : decide (r.begin.key ≤ (k : WithTop Key)) = false := by + rw [decide_eq_false_iff_not] + exact not_le.mpr hkB + rw [hkf] + rfl + have hperm : List.Perm ((List.orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))) + (db₂.keys.filter (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))) := by + have h := (List.perm_orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key))) + rwa [hsingle_filter] at h + have hs₁ : ((List.orderedInsert (· ≤ ·) k db₂.keys).filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))).SortedLT := + List.sortedLT_iff_pairwise.mpr ((db₂.insert k hk_db₂).sorted.pairwise.filter _) + have hs₂ : (db₂.keys.filter + (fun x : Key => decide (r.begin.key ≤ (x : WithTop Key)))).SortedLT := + List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + rw [show db₁ = db₂.insert k hk_db₂ from rfl, DB.insert_keys] + exact (List.SortedLT.eq_of_mem_iff hs₁ hs₂ (fun x => hperm.mem_iff)).trans hfil₂ + have hck₁ : RangeRead.completeKeys db₁ r = res.keys.take (t - 1) := by + rw [completeKeys_eq_filter_ge_drop_take (db := db₁) (r := r) hrev hbegin (by omega), + hfil₁, hdropP] + have hK : min (cnt db₁ r.end_.key + (r.end_.offset - 1).toNat) db₁.keys.length - + (cnt db₁ r.begin.key + b) = t - 1 := by + have hE : (r.end_.offset - 1).toNat = eo := heodef.symm + rw [hcE₁, hcB₁, hn₁, hE, hse] + rw [Nat.min_eq_left (by omega)] + omega + have hK' : min (db₁.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db₁.keys.length - + (db₁.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) + b) = t - 1 := by + simpa [cnt] using hK + rw [hK', List.take_append_of_le_length (by omega : t - 1 ≤ res.keys.length)] + have hnot₁ : ¬ RangeRead.Compatible db₁ r res := by + intro hc + rw [compatible_iff_truncated_keys (db := db₁) hrev hmore hne] at hc + rw [hck₁] at hc + have hf : List.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) + (res.keys.take (t - 1)) = res.keys.take (t - 1) := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_of_mem_take hx) + rw [hf] at hc + have heq : res.keys.take (t - 1) = res.keys := hc.2 + have hlen := congrArg List.length heq + rw [List.length_take, htdef] at hlen + have htpos : 0 < t := by rw [htdef]; exact List.length_pos_of_ne_nil hne + omega + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₂ db₁ := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk_db₂ hkcr).symm + exact hnot₁ ((hs db₂ db₁ hagree).mp hcomp₂) + +end FormalConflictRange + +namespace FormalConflictRange + +private theorem crossedTrunc_begin_key_exists_coe {r : RangeRead} + (h : r.begin.key ≠ (⊤ : WithTop Key)) : + ∃ v : Key, r.begin.key = (↑v : WithTop Key) := by + cases hk : r.begin.key with + | top => exact absurd hk h + | coe v => exact ⟨v, rfl⟩ + +/-- **Large-gap branch minimality for the forward truncated crossed read at any positive +begin offset.** -/ +theorem minimal_truncatedRange_of_crossed_B_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) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hbig : intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞)) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨?_, ?_⟩ + · have hs := sound_truncatedRange_of_crossed_B_general (r := r) (res := res) + hrev hmore hne hbegin hcross hend hbig + rwa [hlast] at hs + · intro k hkB hklt + have hklast : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hklt) + obtain ⟨db₀, hcomp₀⟩ := hread + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + have hmem : x ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hx + exact le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) hbegin) + (RangeRead.mem_completeKeys.mp hmem).2.1 + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := by + obtain ⟨x, hx⟩ : ∃ x, x ∈ res.keys := by + cases h : res.keys with + | nil => exact absurd h hne + | cons a tl => exact ⟨a, by simp⟩ + have h1 : r.begin.key ≤ (x : WithTop Key) := hall_ge x hx + intro htop + rw [htop] at h1 + exact not_le.mpr (WithTop.coe_lt_top x) h1 + obtain ⟨v, hv⟩ := crossedTrunc_begin_key_exists_coe hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp₀⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hkB) + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + exact hbegin_nec.of_between hlast_nec hvk hklast + +/-- **Small-gap branch minimality for the forward truncated crossed read at any positive +begin offset.** -/ +theorem minimal_truncatedForward_of_crossed_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) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hsmall : (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) < intervalCard r.end_.key r.begin.key) : + Minimal r res ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨?_, ?_⟩ + · have hs := sound_truncatedRange_of_crossed_E_general (r := r) (res := res) + hrev hmore hne hbegin hcross hend + rwa [hlast] at hs + · intro k hkE hklt + have hklast : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hklt) + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_of_mem_gap_crossed_truncated_general hread hmore hrev hne hbegin + hcross hend hsmall hkE hkB + · have hkBle : r.begin.key ≤ (k : WithTop Key) := not_lt.mp hkB + obtain ⟨db₀, hcomp₀⟩ := hread + have hall_ge : ∀ x ∈ res.keys, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + have hmem : x ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hx + exact le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) hbegin) + (RangeRead.mem_completeKeys.mp hmem).2.1 + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := by + obtain ⟨x, hx⟩ : ∃ x, x ∈ res.keys := by + cases h : res.keys with + | nil => exact absurd h hne + | cons a tl => exact ⟨a, by simp⟩ + have h1 : r.begin.key ≤ (x : WithTop Key) := hall_ge x hx + intro htop + rw [htop] at h1 + exact not_le.mpr (WithTop.coe_lt_top x) h1 + obtain ⟨v, hv⟩ := crossedTrunc_begin_key_exists_coe hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp₀⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hkBle) + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + exact hbegin_nec.of_between hlast_nec hvk hklast + +/-- The forward truncated crossed closed form at any positive begin offset. -/ +noncomputable def truncatedForwardCrossedGeneral (r : RangeRead) (res : Result) : ConflictRange := + if intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) then + truncatedRange r (res.keys.getLastD ([] : Key)) + else + ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ + +/-- The forward truncated crossed closed form is minimal on the whole region. -/ +theorem minimal_truncatedForwardCrossedGeneral {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) : + Minimal r res (truncatedForwardCrossedGeneral r res) := by + unfold truncatedForwardCrossedGeneral + by_cases hbig : intervalCard r.end_.key r.begin.key ≤ + (((r.end_.offset - 1).toNat - res.keys.length - + (r.begin.offset - 1).toNat : ℕ) : ℕ∞) + · rw [ite_eq_left hbig] + exact minimal_truncatedRange_of_crossed_B_general hread hmore hrev hne hbegin hcross hend hbig + · rw [ite_eq_right hbig] + exact minimal_truncatedForward_of_crossed_general hread hmore hrev hne hbegin hcross hend + (not_le.mp hbig) + +/-- The forward truncated crossed general rule denotes the general conflict range. -/ +theorem truncatedForwardCrossedGeneral_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) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) : + (truncatedForwardCrossedGeneral r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_truncatedForwardCrossedGeneral hread hmore hrev hne hbegin hcross hend).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The same identification with the executable algorithm. -/ +theorem truncatedForwardCrossedGeneral_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) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) : + (truncatedForwardCrossedGeneral r res).toSet = (simpleConflictRange r res).toSet := + (minimal_truncatedForwardCrossedGeneral hread hmore hrev hne hbegin hcross hend).toSet_eq + (simpleConflictRange_minimal hread) + +end FormalConflictRange diff --git a/FormalConflictRange/DiffFeasible.lean b/FormalConflictRange/DiffFeasible.lean new file mode 100644 index 0000000..b06cc01 --- /dev/null +++ b/FormalConflictRange/DiffFeasible.lean @@ -0,0 +1,344 @@ +/- +Copyright (c) 2026 FormalConflictRange contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: FormalConflictRange contributors +-/ + +import Mathlib + +/-! +# A computable decision procedure for difference constraints +-/ + +namespace FormalConflictRange + +/-- A difference constraint `x v - x u ≤ w`. -/ +structure DEdge where + u : ℕ + v : ℕ + w : ℤ + +namespace DEdge + +/-- The constraint holds at the assignment `x`. -/ +def Holds (x : ℕ → ℤ) (e : DEdge) : Prop := x e.v - x e.u ≤ e.w + +end DEdge + +/-- All constraints hold at `x`. -/ +def HoldsAll (x : ℕ → ℤ) (es : List DEdge) : Prop := ∀ e ∈ es, e.Holds x + +/-- All collected constant constraints `0 ≤ w` hold. -/ +def HoldsConsts (cs : List ℤ) : Prop := ∀ w ∈ cs, 0 ≤ w + +/-- A state is feasible when it has an assignment satisfying all constants and edges. -/ +def DiffFeasible (cs : List ℤ) (es : List DEdge) : Prop := + ∃ x : ℕ → ℤ, HoldsConsts cs ∧ HoldsAll x es + +/-- Eliminate variable `k` from a difference-constraint state. -/ +def eliminate (k : ℕ) (cs : List ℤ) (es : List DEdge) : List ℤ × List DEdge := + let selfs0 := es.filter (fun e => decide (e.u = e.v)) + let inc := es.filter (fun e => decide (e.u ≠ e.v ∧ e.v = k)) + let out := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u = k)) + let keep := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k)) + let comb := inc.flatMap fun a => out.map fun b => (⟨a.u, b.v, a.w + b.w⟩ : DEdge) + let all := keep ++ comb + let selfs := all.filter (fun e => decide (e.u = e.v)) + let rest := all.filter (fun e => decide (e.u ≠ e.v)) + (cs ++ selfs0.map (fun e => e.w) ++ selfs.map (fun e => e.w), rest) + +/-- Eliminate variables `0, …, n-1`, highest first. -/ +def elimAll : ℕ → List ℤ → List DEdge → List ℤ × List DEdge + | 0, cs, es => (cs, es) + | n + 1, cs, es => + let p := eliminate n cs es + elimAll n p.1 p.2 + +/-- The decision procedure: eliminate all variables, then check the constants. -/ +def diffFeasibleBool (n : ℕ) (cs : List ℤ) (es : List DEdge) : Bool := + let p := elimAll n cs es + p.1.all (fun w => decide (0 ≤ w)) + +/-- A finite separation lemma for integers. -/ +theorem exists_between_finset (L U : Finset ℤ) + (h : ∀ l ∈ L, ∀ u ∈ U, l ≤ u) : + ∃ x : ℤ, (∀ l ∈ L, l ≤ x) ∧ (∀ u ∈ U, x ≤ u) := by + by_cases hL : L.Nonempty + · by_cases hU : U.Nonempty + · refine ⟨L.max' hL, ?_, ?_⟩ + · intro l hl + exact Finset.le_max' L l hl + · intro u hu + exact h _ (Finset.max'_mem L hL) u hu + · refine ⟨L.max' hL, ?_, ?_⟩ + · intro l hl + exact Finset.le_max' L l hl + · intro u hu + exact absurd ⟨u, hu⟩ hU + · by_cases hU : U.Nonempty + · refine ⟨U.min' hU, ?_, ?_⟩ + · intro l hl + exact absurd ⟨l, hl⟩ hL + · intro u hu + exact Finset.min'_le U u hu + · refine ⟨0, ?_, ?_⟩ + · intro l hl + exact absurd ⟨l, hl⟩ hL + · intro u hu + exact absurd ⟨u, hu⟩ hU + +theorem self_holds_iff {x : ℕ → ℤ} {e : DEdge} (h : e.u = e.v) : + e.Holds x ↔ 0 ≤ e.w := by + simp [DEdge.Holds, h] + +/-- Eliminating one variable preserves feasibility. -/ +theorem diffFeasible_eliminate (k : ℕ) (cs : List ℤ) (es : List DEdge) : + DiffFeasible cs es ↔ DiffFeasible (eliminate k cs es).1 (eliminate k cs es).2 := by + classical + unfold DiffFeasible + constructor + · rintro ⟨x, hcs, hes⟩ + have hself0 : ∀ e ∈ es, e.u = e.v → 0 ≤ e.w := by + intro e he huv + have := hes e he + rwa [self_holds_iff huv] at this + have hkeep : HoldsAll x + (es.filter (fun e => decide (e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k))) := by + intro e he + simp only [List.mem_filter, decide_eq_true_eq] at he + exact hes e he.1 + have hcomb : HoldsAll x + ((es.filter (fun e => decide (e.u ≠ e.v ∧ e.v = k))).flatMap fun a => + (es.filter (fun e => decide (e.u ≠ e.v ∧ e.u = k))).map fun b => + (⟨a.u, b.v, a.w + b.w⟩ : DEdge)) := by + intro e he + rw [List.mem_flatMap] at he + obtain ⟨a, ha, he⟩ := he + rw [List.mem_map] at he + obtain ⟨b, hb, rfl⟩ := he + simp only [List.mem_filter, decide_eq_true_eq] at ha hb + have hae := hes a ha.1 + have hbe := hes b hb.1 + simp only [DEdge.Holds] at hae hbe ⊢ + rw [ha.2.2] at hae + rw [hb.2.2] at hbe + linarith + refine ⟨x, ?_, ?_⟩ + · intro w hw + simp only [eliminate, List.mem_append, List.mem_map, List.mem_filter, + decide_eq_true_eq] at hw + rcases hw with (hw | hw) | hw + · exact hcs w hw + · obtain ⟨e, he, rfl⟩ := hw + exact hself0 e he.1 he.2 + · obtain ⟨e, he, rfl⟩ := hw + rcases he.1 with hek | hec + · exact hself0 e hek.1 he.2 + · rw [List.mem_flatMap] at hec + obtain ⟨a, ha, hec⟩ := hec + rw [List.mem_map] at hec + obtain ⟨b, hb, rfl⟩ := hec + simp only [List.mem_filter, decide_eq_true_eq] at ha hb + have hae := hes a ha.1 + have hbe := hes b hb.1 + simp only [DEdge.Holds] at hae hbe ⊢ + rw [ha.2.2] at hae + rw [hb.2.2] at hbe + -- self means `a.u = b.v`, so the left side is zero + have hab : a.u = b.v := he.2 + have hzero : x b.v - x a.u = 0 := by rw [← hab]; ring + linarith + · intro e he + simp only [eliminate, List.mem_append, List.mem_filter, decide_eq_true_eq] at he + rcases he.1 with hek | hec + · exact hkeep e (by simpa only [List.mem_filter, decide_eq_true_eq] using hek) + · exact hcomb e hec + · rintro ⟨y, hcs, hes⟩ + let selfs0 := es.filter (fun e => decide (e.u = e.v)) + let inc := es.filter (fun e => decide (e.u ≠ e.v ∧ e.v = k)) + let out := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u = k)) + let keep := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k)) + let comb := inc.flatMap fun a => out.map fun b => (⟨a.u, b.v, a.w + b.w⟩ : DEdge) + let all := keep ++ comb + let selfs := all.filter (fun e => decide (e.u = e.v)) + have hcs' : HoldsConsts (cs ++ selfs0.map (fun e => e.w) ++ + selfs.map (fun e => e.w)) := by + simpa only [eliminate, selfs0, inc, out, keep, comb, all, selfs] using hcs + have hes' : HoldsAll y (all.filter (fun e => decide (e.u ≠ e.v))) := by + simpa only [eliminate, selfs0, inc, out, keep, comb, all, selfs] using hes + have hkeep : HoldsAll y keep := by + intro e he + have hek : e ∈ es ∧ e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k := by + simpa [keep] using he + apply hes' e + simp only [List.mem_filter, decide_eq_true_eq] + exact ⟨List.mem_append.mpr (Or.inl he), hek.2.1⟩ + have hcomb : HoldsAll y comb := by + intro e he + by_cases hself : e.u = e.v + · have hw : e.w ∈ cs ++ selfs0.map (fun e => e.w) ++ selfs.map (fun e => e.w) := by + simp only [List.mem_append, List.mem_map] + exact Or.inr ⟨e, by + change e ∈ all.filter (fun e => decide (e.u = e.v)) + simp only [List.mem_filter, decide_eq_true_eq] + exact ⟨List.mem_append.mpr (Or.inr he), hself⟩, rfl⟩ + have := hcs' e.w hw + simpa [DEdge.Holds, hself] using this + · apply hes' e + simp only [List.mem_filter, decide_eq_true_eq] + exact ⟨List.mem_append.mpr (Or.inr he), hself⟩ + let L : Finset ℤ := (out.map fun b => y b.v - b.w).toFinset + let U : Finset ℤ := (inc.map fun a => y a.u + a.w).toFinset + have hsep : ∀ l ∈ L, ∀ u ∈ U, l ≤ u := by + intro l hl u hu + rw [show L = (out.map fun b => y b.v - b.w).toFinset by rfl] at hl + rw [show U = (inc.map fun a => y a.u + a.w).toFinset by rfl] at hu + rw [List.mem_toFinset] at hl hu + obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hl + obtain ⟨a, ha, rfl⟩ := List.mem_map.mp hu + have hcombEdge : (⟨a.u, b.v, a.w + b.w⟩ : DEdge) ∈ comb := by + unfold comb + rw [List.mem_flatMap] + exact ⟨a, ha, List.mem_map.mpr ⟨b, hb, rfl⟩⟩ + have hh := hcomb _ hcombEdge + simp only [DEdge.Holds] at hh + linarith + obtain ⟨xk, hxkL, hxkU⟩ := exists_between_finset L U hsep + let x : ℕ → ℤ := Function.update y k xk + have hx_eq : ∀ i, i ≠ k → x i = y i := by + intro i hi + simp [x, Function.update_of_ne hi] + have hx_self : x k = xk := by simp [x] + refine ⟨x, ?_, ?_⟩ + · intro w hw + have hw' : w ∈ (cs ++ selfs0.map (fun e => e.w)) ++ selfs.map (fun e => e.w) := + List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl hw))) + exact hcs' w hw' + · intro e he + by_cases huv : e.u = e.v + · have hw : e.w ∈ cs ++ selfs0.map (fun e => e.w) ++ selfs.map (fun e => e.w) := by + simp only [List.mem_append, List.mem_map] + exact Or.inl (Or.inr ⟨e, by + simp only [selfs0, List.mem_filter, decide_eq_true_eq] + exact ⟨he, huv⟩, rfl⟩) + exact (self_holds_iff huv).mpr (hcs' e.w hw) + · by_cases huk : e.u = k + · have hmem : e ∈ out := by + simp only [out, List.mem_filter, decide_eq_true_eq] + exact ⟨he, huv, huk⟩ + have hlow : y e.v - e.w ≤ xk := hxkL _ (by + rw [show L = (out.map fun b => y b.v - b.w).toFinset by rfl] + exact List.mem_toFinset.mpr (List.mem_map.mpr ⟨e, hmem, rfl⟩)) + have hve : e.v ≠ k := by + intro h; exact huv (huk.trans h.symm) + simp only [DEdge.Holds] + rw [show e.u = k from huk, hx_self, hx_eq e.v hve] + linarith + · by_cases hvk : e.v = k + · have hmem : e ∈ inc := by + simp only [inc, List.mem_filter, decide_eq_true_eq] + exact ⟨he, huv, hvk⟩ + have hup : xk ≤ y e.u + e.w := hxkU _ (by + rw [show U = (inc.map fun a => y a.u + a.w).toFinset by rfl] + exact List.mem_toFinset.mpr (List.mem_map.mpr ⟨e, hmem, rfl⟩)) + have hue : e.u ≠ k := by + intro h; exact huv (h.trans hvk.symm) + simp only [DEdge.Holds] + rw [show e.v = k from hvk, hx_self, hx_eq e.u hue] + linarith + · have hmem : e ∈ keep := by + simp only [keep, List.mem_filter, decide_eq_true_eq] + exact ⟨he, huv, huk, hvk⟩ + have hh := hkeep e hmem + simpa only [DEdge.Holds, hx_eq e.u huk, hx_eq e.v hvk] using hh + +/-- Eliminating all variables preserves feasibility. -/ +theorem diffFeasible_elimAll (n : ℕ) (cs : List ℤ) (es : List DEdge) : + DiffFeasible cs es ↔ DiffFeasible (elimAll n cs es).1 (elimAll n cs es).2 := by + induction n generalizing cs es with + | zero => simp [elimAll] + | succ n ih => + rw [elimAll, diffFeasible_eliminate] + exact ih _ _ + +/-- After eliminating `0, …, n-1`, no nonself edge with endpoints below `n` +remains. -/ +theorem elimAll_snd_eq_nil_of_endpoints_lt {n : ℕ} {cs : List ℤ} {es : List DEdge} + (h : ∀ e ∈ es, e.u < n ∧ e.v < n) : (elimAll n cs es).2 = [] := by + induction n generalizing cs es with + | zero => + have hes : es = [] := by + rw [List.eq_nil_iff_forall_not_mem] + intro e he + have := h e he + omega + simp [elimAll, hes] + | succ n ih => + let p := eliminate n cs es + have hp : ∀ e ∈ p.2, e.u < n ∧ e.v < n := by + intro e he + dsimp [p] at he + simp only [eliminate, List.mem_filter, decide_eq_true_eq, List.mem_append] at he + rcases he with ⟨hek | hec, hne⟩ + · have hb := h e hek.1 + exact ⟨by omega, by omega⟩ + · rw [List.mem_flatMap] at hec + obtain ⟨a, ha, hec⟩ := hec + rw [List.mem_map] at hec + obtain ⟨b, hb, rfl⟩ := hec + simp only [List.mem_filter, decide_eq_true_eq] at ha hb + have hua := h a ha.1 + have hvb := h b hb.1 + change a.u < n ∧ b.v < n + exact ⟨by omega, by omega⟩ + have := ih (cs := p.1) (es := p.2) hp + simpa [elimAll, p] using this + +/-- The Boolean elimination procedure is correct for edge lists whose endpoints +all lie below `n`. -/ +theorem diffFeasibleBool_spec (n : ℕ) (cs : List ℤ) (es : List DEdge) + (h : ∀ e ∈ es, e.u < n ∧ e.v < n) : + diffFeasibleBool n cs es = true ↔ DiffFeasible cs es := by + have hnil := elimAll_snd_eq_nil_of_endpoints_lt (cs := cs) (es := es) h + have hconst : HoldsConsts (elimAll n cs es).1 ↔ + DiffFeasible (elimAll n cs es).1 (elimAll n cs es).2 := by + constructor + · intro hc + exact ⟨fun _ => 0, hc, by simp [HoldsAll, hnil]⟩ + · rintro ⟨x, hc, -⟩ + exact hc + rw [diffFeasibleBool, List.all_eq_true, diffFeasible_elimAll] + constructor + · intro hall + exact hconst.mp fun w hw => of_decide_eq_true (hall w hw) + · intro hf w hw + exact decide_eq_true_eq.mpr (hconst.mpr hf w hw) + +/-- A safe upper bound on all variable indices occurring in an edge list. -/ +def edgeBound : List DEdge → ℕ + | [] => 0 + | e :: es => max (max e.u e.v + 1) (edgeBound es) + +theorem edgeBound_spec (es : List DEdge) : + ∀ e ∈ es, e.u < edgeBound es ∧ e.v < edgeBound es := by + induction es with + | nil => intro e he; simp at he + | cons d ds ih => + intro e he + simp only [edgeBound] + rw [List.mem_cons] at he + rcases he with rfl | he + · omega + · have := ih e he + omega + +/-- Feasibility of an arbitrary edge list, using its own index bound. -/ +def diffFeasibleBoolAuto (es : List DEdge) : Bool := + diffFeasibleBool (edgeBound es) [] es + +theorem diffFeasibleBoolAuto_spec (es : List DEdge) : + diffFeasibleBoolAuto es = true ↔ DiffFeasible [] es := + diffFeasibleBool_spec (edgeBound es) [] es (edgeBound_spec es) + + +end FormalConflictRange diff --git a/FormalConflictRange/Empty.lean b/FormalConflictRange/Empty.lean new file mode 100644 index 0000000..c5fe9ee --- /dev/null +++ b/FormalConflictRange/Empty.lean @@ -0,0 +1,3204 @@ +/- +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.Sound + +/-! +# When is every result of a read empty? + +A read is *trivially empty* when no database admits a non-empty compatible +result. This file characterizes that property purely in terms of the read's two +selector keys and offsets, with no mention of any database. + +The bridge is `completeKeys_ne_nil_iff`: the complete keys of a read are nonempty +exactly when the begin selector's clamped anchor index is strictly below the end +selector's. Since a nonempty result always exists once the complete keys are +nonempty (`limit` is a positive natural, so a length-one prefix reporting `more` +is compatible), trivial emptiness is equivalent to + + `∀ db, completeKeys db r = []`, + +which is further equivalent to the arithmetic side condition `¬ countFeasible r` +developed below: `countFeasible` quantifies over the counts of stored keys below +the two selector keys and the database length, constrained only by the interval +cardinalities, and asserts that the clamped anchors can be ordered. + +The two halves are `countFeasible_of_exists_db` (a witness database yields +feasible counts) and `countFeasible_complete` (feasible counts are realized by a +database), glued together by `alwaysEmpty_iff_not_countFeasible`. +-/ + +namespace FormalConflictRange + +/-- The clamped anchor index of a selector against a database: the begin +selector of a read targets this index, clamped to the database length. It is the +`Nat`-valued counterpart of `Selector.anchor`, with the negative clamp to the +empty key and the past-the-end clamp to `⊤` made explicit by `min` and `toNat`. -/ +def clampIndex (db : DB) (s : Selector) : ℕ := + min (s.anchor db).toNat db.keys.length + +/-- A stored key at index `j` lies strictly below the resolution of `s` exactly +when `j` is below the clamped anchor index. -/ +theorem getElem_lt_resolve_iff {db : DB} {s : Selector} {j : ℕ} + (hj : j < db.keys.length) : + (↑(db.keys[j]) : WithTop Key) < resolve db s ↔ j < clampIndex db s := by + unfold clampIndex + by_cases hneg : s.anchor db < 0 + · have h0 : (s.anchor db).toNat = 0 := by omega + rw [resolve_eq_nil hneg, h0, Nat.zero_min] + exact iff_of_false + (fun hc => List.not_lt_nil (db.keys[j]) (WithTop.coe_lt_coe.mp hc)) (by omega) + · have h0 : 0 ≤ s.anchor db := by omega + rcases lt_or_ge (s.anchor db).toNat db.keys.length with hlt | hge + · rw [resolve_eq_getElem h0 hlt, WithTop.coe_lt_coe, db.sorted.getElem_lt_getElem_iff, + Nat.min_eq_left (le_of_lt hlt)] + · rw [resolve_eq_top h0 hge, Nat.min_eq_right hge] + exact iff_of_true (WithTop.coe_lt_top _) hj + +/-- The resolution of `s` lies at or below a stored key at index `j` exactly when +the clamped anchor index is at most `j`. -/ +theorem resolve_le_getElem_iff {db : DB} {s : Selector} {j : ℕ} + (hj : j < db.keys.length) : + resolve db s ≤ (↑(db.keys[j]) : WithTop Key) ↔ clampIndex db s ≤ j := by + unfold clampIndex + by_cases hneg : s.anchor db < 0 + · have h0 : (s.anchor db).toNat = 0 := by omega + rw [resolve_eq_nil hneg, h0, Nat.zero_min] + exact iff_of_true (WithTop.coe_le_coe.mpr (nil_le_key _)) (Nat.zero_le j) + · have h0 : 0 ≤ s.anchor db := by omega + rcases lt_or_ge (s.anchor db).toNat db.keys.length with hlt | hge + · rw [resolve_eq_getElem h0 hlt, WithTop.coe_le_coe, db.sorted.getElem_le_getElem_iff, + Nat.min_eq_left (le_of_lt hlt)] + · rw [resolve_eq_top h0 hge, Nat.min_eq_right hge] + exact iff_of_false (not_le.mpr (WithTop.coe_lt_top _)) (by omega) + +/-- The number of stored keys strictly below a `WithTop Key` bound. This is the +count a selector's anchor is built from: nothing below the empty key, the whole +database below `⊤`, and the `countP (· < k)` below a genuine key. -/ +def cnt (db : DB) (w : WithTop Key) : ℕ := + db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < w)) + +/-- Nothing is stored below the empty key. -/ +theorem cnt_nil (db : DB) : + cnt db (([] : Key) : WithTop Key) = 0 := countP_lt_nil + +/-- Everything is stored below `⊤`. -/ +theorem cnt_top (db : DB) : cnt db ⊤ = db.keys.length := countP_lt_top + +/-- A count is bounded by the database length. -/ +theorem cnt_le_length (db : DB) (w : WithTop Key) : cnt db w ≤ db.keys.length := + List.countP_le_length + +/-- The stored keys in the half-open `WithTop Key` interval `[w₁, w₂)`. -/ +def between (db : DB) (w₁ w₂ : WithTop Key) : List Key := + db.keys.filter (fun (k : Key) => decide (w₁ ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < w₂)) + +/-- The stored keys in `[w₁, w₂)`, as a set of genuine keys, lie in `keysIn w₁ w₂`. -/ +theorem between_toFinset_subset_keysIn (db : DB) (w₁ w₂ : WithTop Key) : + ((between db w₁ w₂).toFinset : Set Key) ⊆ keysIn w₁ w₂ := by + intro x hx + simp only [Finset.mem_coe, List.mem_toFinset, between, List.mem_filter, + decide_eq_true_eq] at hx + exact hx.2 + +/-- For a list, the keys below `w₂` split at `w₁ ≤ w₂` into the keys below `w₁` +and those in `[w₁, w₂)`. -/ +theorem filter_lt_toFinset_union_top (l : List Key) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) : + (l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < w₂))).toFinset = + (l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < w₁))).toFinset ∪ + (l.filter (fun (k : Key) => + decide (w₁ ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < w₂))).toFinset := by + ext x + simp only [List.mem_toFinset, List.mem_filter, decide_eq_true_eq, Finset.mem_union] + constructor + · rintro ⟨hx, hxe⟩ + by_cases hxb : (↑x : WithTop Key) < w₁ + · exact Or.inl ⟨hx, hxb⟩ + · exact Or.inr ⟨hx, le_of_not_gt hxb, hxe⟩ + · rintro (⟨hx, hxb⟩ | ⟨hx, hxb, hxe⟩) + · exact ⟨hx, lt_of_lt_of_le hxb h⟩ + · exact ⟨hx, hxe⟩ + +/-- The two pieces of the preceding split are disjoint. -/ +theorem disjoint_filters_top (l : List Key) (w₁ w₂ : WithTop Key) : + Disjoint (l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < w₁))).toFinset + (l.filter (fun (k : Key) => + decide (w₁ ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < w₂))).toFinset := by + rw [Finset.disjoint_left] + intro x hx1 hx2 + simp only [List.mem_toFinset, List.mem_filter, decide_eq_true_eq] at hx1 hx2 + exact absurd hx1.2 (not_lt.mpr hx2.2.1) + +/-- Counting below `w₂` splits at `w₁ ≤ w₂` into the keys below `w₁` and those in +`[w₁, w₂)`. -/ +theorem cnt_eq_add_between (db : DB) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) : + cnt db w₂ = cnt db w₁ + (between db w₁ w₂).length := by + simp only [cnt, between, List.countP_eq_length_filter] + rw [← List.toFinset_card_of_nodup (db.nodup.filter _), + ← List.toFinset_card_of_nodup (db.nodup.filter _), + ← List.toFinset_card_of_nodup (db.nodup.filter _), + filter_lt_toFinset_union_top db.keys h, + Finset.card_union_of_disjoint (disjoint_filters_top db.keys w₁ w₂)] + +/-- The number of stored keys below a `WithTop Key` bound is at most the +cardinality of the interval from the empty key. -/ +theorem cnt_le_intervalCard_nil (db : DB) (w : WithTop Key) : + (cnt db w : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) w := by + cases w with + | top => + rw [cnt_top, intervalCard_top_right (by simp)] + exact le_top + | coe ko => + have hnodup : (db.keys.filter (fun k => decide (k < ko))).Nodup := db.nodup.filter _ + have hset : ((db.keys.filter (fun k => decide (k < ko))).toFinset : Set Key) ⊆ + keysIn (↑([] : Key) : WithTop Key) (↑ko : WithTop Key) := by + intro x hx + simp only [Finset.mem_coe, List.mem_toFinset, List.mem_filter, decide_eq_true_eq] at hx + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key x), WithTop.coe_lt_coe.mpr hx.2⟩ + have h1 := Set.encard_le_encard hset + rw [Set.encard_coe_eq_coe_finsetCard, List.toFinset_card_of_nodup hnodup] at h1 + have hcnt : cnt db (↑ko : WithTop Key) = + (db.keys.filter (fun k => decide (k < ko))).length := by + rw [cnt, List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + simp [WithTop.coe_lt_coe] + rw [hcnt, intervalCard_eq_encard_keysIn] + exact h1 + +/-- The excess of the count below `w₂` over the count below `w₁ ≤ w₂` is the +number of stored keys in `[w₁, w₂)`, bounded by the interval's cardinality. -/ +theorem cnt_sub_le_intervalCard (db : DB) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) : + ((cnt db w₂ - cnt db w₁ : ℕ) : ℕ∞) ≤ intervalCard w₁ w₂ := by + have hnodup : (between db w₁ w₂).Nodup := by + unfold between + exact db.nodup.filter _ + have h1 : ((between db w₁ w₂).length : ℕ∞) ≤ (keysIn w₁ w₂).encard := by + calc ((between db w₁ w₂).length : ℕ∞) + = ((between db w₁ w₂).toFinset.card : ℕ∞) := by + rw [List.toFinset_card_of_nodup hnodup] + _ = ((between db w₁ w₂).toFinset : Set Key).encard := + (Set.encard_coe_eq_coe_finsetCard _).symm + _ ≤ (keysIn w₁ w₂).encard := + Set.encard_le_encard (between_toFinset_subset_keysIn db w₁ w₂) + have h2 := cnt_eq_add_between db h + have hlen : (between db w₁ w₂).length = cnt db w₂ - cnt db w₁ := by omega + rw [intervalCard_eq_encard_keysIn, ← hlen] + exact h1 + +/-- A count is monotone in the bound: a higher bound counts weakly more keys. -/ +theorem cnt_le_cnt_of_le (db : DB) {w₁ w₂ : WithTop Key} (h : w₁ ≤ w₂) : + cnt db w₁ ≤ cnt db w₂ := by + have := cnt_eq_add_between db h + omega + + +/-- Below a genuine key, a count is the `Key`-level `countP`. -/ +theorem cnt_coe (db : DB) (k : Key) : + cnt db (↑k : WithTop Key) = db.keys.countP (fun x => decide (x < k)) := + countP_lt_coe (l := db.keys) k + +/-- If every stored key is at or above `w`, nothing is counted below `w`. -/ +theorem cnt_eq_zero_of_forall_ge (db : DB) {w : WithTop Key} + (h : ∀ x ∈ db.keys, w ≤ (x : WithTop Key)) : cnt db w = 0 := by + unfold cnt + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (h x hx) + +/-- If every stored key is strictly below `w`, all of them are counted below `w`. -/ +theorem cnt_eq_length_of_forall_lt (db : DB) {w : WithTop Key} + (h : ∀ x ∈ db.keys, (x : WithTop Key) < w) : cnt db w = db.keys.length := by + unfold cnt + rw [List.countP_eq_length] + intro x hx + simp only [decide_eq_true_eq] + exact h x hx + +/-- The clamped anchor index, in terms of a prescribed count below the selector +key. -/ +theorem clampIndex_eq (db : DB) (β : WithTop Key) (o : ℤ) (c : ℕ) + (hc : cnt db β = c) : + clampIndex db ⟨β, o⟩ = min ((c : ℤ) + o - 1).toNat db.keys.length := by + unfold clampIndex + have h : Selector.anchor db (⟨β, o⟩ : Selector) = (cnt db β : ℤ) + o - 1 := by + simp only [Selector.anchor, cnt] + rw [h, hc] + +/-- A selector at offset one clamps to at most the count of stored keys below its +own key: its anchor is that count exactly, and the clamp to the database length +only lowers it. -/ +theorem clampIndex_le_cnt_of_offset_one (db : DB) (β : WithTop Key) : + clampIndex db (⟨β, 1⟩ : Selector) ≤ cnt db β := by + rw [clampIndex_eq db β 1 (cnt db β) rfl] + have h : (((cnt db β : ℤ) + 1 - 1).toNat) = cnt db β := by + rw [show ((cnt db β : ℤ) + 1 - 1) = (cnt db β : ℤ) by ring, Int.toNat_natCast] + rw [h] + exact Nat.min_le_left _ _ + +/-- The clamped anchor index of the begin selector at offset one: its anchor is +exactly the count of stored keys strictly below `begin.key`, clamped to the +database length. -/ +theorem clampIndex_begin_eq {db : DB} {r : RangeRead} (h : r.begin.offset = 1) : + clampIndex db r.begin = min (cnt db r.begin.key) db.keys.length := by + unfold clampIndex + have han : r.begin.anchor db = (cnt db r.begin.key : ℤ) := by + unfold Selector.anchor cnt; rw [h]; ring + rw [han, Int.toNat_natCast] + +/-- The clamped anchor index of the end selector: its anchor is the count of +stored keys strictly below `end_.key` plus `end_.offset - 1`, clamped to the +database length. -/ +theorem clampIndex_end_eq {db : DB} {r : RangeRead} : + clampIndex db r.end_ = + min (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) db.keys.length := by + unfold clampIndex + have han : r.end_.anchor db = (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by + unfold Selector.anchor cnt; ring + rw [han] + +/-- The keys of `[B, ↑e)` are contained in those of `[B, E)` whenever `↑e ≤ E`, so +the interval cardinality is monotone in the upper end. -/ +theorem intervalCard_le_intervalCard_of_end_le {B : WithTop Key} {e : Key} {E : WithTop Key} + (h : (↑e : WithTop Key) ≤ E) : + intervalCard B (↑e) ≤ intervalCard B E := by + rw [intervalCard_eq_encard_keysIn, intervalCard_eq_encard_keysIn] + exact Set.encard_le_encard fun k hk => ⟨hk.1, lt_of_lt_of_le hk.2 h⟩ + +/-- Every database length is realizable. -/ +theorem exists_db_length (n : ℕ) : ∃ db : DB, db.keys.length = n := + ⟨⟨(List.range n).map (chain ([] : Key)), sortedLT_map_chain _ _⟩, by simp⟩ + +/-- **Realizing `⊤` with a genuine end key.** A database of length `n` with +exactly `cE` stored keys below `e`, for any `cE` fitting below `e` and `cE ≤ n`. +The first `cE` successors of the empty key stay below `e`; the remainder are +successors of `e` itself, at or above it. -/ +theorem exists_db_top_coe {e : Key} {cE n : ℕ} + (hcE : (cE : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑e : WithTop Key)) + (hlen : cE ≤ n) : + ∃ db : DB, db.keys.length = n ∧ cnt db (⊤ : WithTop Key) = n ∧ cnt db (↑e) = cE := by + have hchainnil : ∀ i < cE, chain ([] : Key) i < e := fun i hi => + (chain_nil_lt_iff e i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hcE) + have hsorted : ((List.range cE).map (chain ([] : Key)) ++ + (List.range (n - cE)).map (chain e)).SortedLT := by + rw [List.sortedLT_append] + refine ⟨sortedLT_map_chain _ _, sortedLT_map_chain _ _, ?_⟩ + intro a ha b hb + rw [List.mem_map] at ha hb + obtain ⟨i, hi, rfl⟩ := ha + obtain ⟨j, -, rfl⟩ := hb + exact lt_of_lt_of_le (hchainnil i (List.mem_range.mp hi)) (chain_le e j) + refine ⟨⟨_, hsorted⟩, ?_, ?_, ?_⟩ + · rw [List.length_append, List.length_map, List.length_map, List.length_range, + List.length_range] + omega + · rw [cnt_top, List.length_append, List.length_map, List.length_map, List.length_range, + List.length_range] + omega + · rw [cnt_coe, List.countP_append] + have h1 : ((List.range cE).map (chain ([] : Key))).countP (fun x => decide (x < e)) = cE := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range] + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchainnil i (List.mem_range.mp hi) + have h2 : ((List.range (n - cE)).map (chain e)).countP (fun x => decide (x < e)) = 0 := by + rw [countP_lt_eq_zero_of_forall_ge] + intro x hx + rw [List.mem_map] at hx + obtain ⟨j, -, rfl⟩ := hx + exact chain_le e j + rw [h1, h2, Nat.add_zero] + +/-- **Realizing a genuine begin with `⊤`.** A database of length `n` with exactly +`cB` stored keys below `b`, for any `cB` fitting below `b` and `cB ≤ n`. -/ +theorem exists_db_coe_top {b : Key} {cB n : ℕ} + (hcB : (cB : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑b : WithTop Key)) + (hlen : cB ≤ n) : + ∃ db : DB, db.keys.length = n ∧ cnt db (↑b) = cB ∧ cnt db (⊤ : WithTop Key) = n := by + have hchainnil : ∀ i < cB, chain ([] : Key) i < b := fun i hi => + (chain_nil_lt_iff b i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hcB) + have hsorted : ((List.range cB).map (chain ([] : Key)) ++ + (List.range (n - cB)).map (chain b)).SortedLT := by + rw [List.sortedLT_append] + refine ⟨sortedLT_map_chain _ _, sortedLT_map_chain _ _, ?_⟩ + intro a ha b' hb' + rw [List.mem_map] at ha hb' + obtain ⟨i, hi, rfl⟩ := ha + obtain ⟨j, -, rfl⟩ := hb' + exact lt_of_lt_of_le (hchainnil i (List.mem_range.mp hi)) (chain_le b j) + refine ⟨⟨_, hsorted⟩, ?_, ?_, ?_⟩ + · rw [List.length_append, List.length_map, List.length_map, List.length_range, + List.length_range] + omega + · rw [cnt_coe, List.countP_append] + have h1 : ((List.range cB).map (chain ([] : Key))).countP (fun x => decide (x < b)) = cB := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => ?_), List.length_map, List.length_range] + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchainnil i (List.mem_range.mp hi) + have h2 : ((List.range (n - cB)).map (chain b)).countP (fun x => decide (x < b)) = 0 := by + rw [countP_lt_eq_zero_of_forall_ge] + intro x hx + rw [List.mem_map] at hx + obtain ⟨j, -, rfl⟩ := hx + exact chain_le b j + rw [h1, h2, Nat.add_zero] + · rw [cnt_top, List.length_append, List.length_map, List.length_map, List.length_range, + List.length_range] + omega + +/-- **Realizing a pair of counts.** For genuine keys `lo ≤ hi`, a database of any +prescribed length `n ≥ chi` can be built with exactly `clo` stored keys below +`lo` and `chi` below `hi`, provided `clo` keys fit below `lo` and `chi - clo` fit +in `[lo, hi)`. The witness is three successor chains: `clo` keys below `lo`, then +`chi - clo` keys in `[lo, hi)`, then `n - chi` keys at or above `hi`. -/ +theorem exists_db_counts_of_le {lo hi : Key} (h : lo ≤ hi) {clo chi n : ℕ} + (hcl : clo ≤ chi) + (hlo : (clo : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑lo : WithTop Key)) + (hgap : ((chi - clo : ℕ) : ℕ∞) ≤ intervalCard (↑lo : WithTop Key) (↑hi : WithTop Key)) + (hlen : chi ≤ n) : + ∃ db : DB, db.keys.length = n ∧ cnt db (↑lo : WithTop Key) = clo ∧ + cnt db (↑hi : WithTop Key) = chi := by + have hchainLo : ∀ i < clo, chain ([] : Key) i < lo := fun i hi => + (chain_nil_lt_iff lo i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hlo) + have hchainGap : ∀ j < chi - clo, chain lo j < hi := fun j hj => + (chain_lt_iff lo hi j).mpr (lt_of_lt_of_le (by exact_mod_cast hj) hgap) + let A : List Key := (List.range clo).map (chain ([] : Key)) + let B : List Key := (List.range (chi - clo)).map (chain lo) + let C : List Key := (List.range (n - chi)).map (chain hi) + have hA : A.SortedLT := sortedLT_map_chain _ _ + have hB : B.SortedLT := sortedLT_map_chain _ _ + have hC : C.SortedLT := sortedLT_map_chain _ _ + have hAB : ∀ a ∈ A, ∀ b ∈ B, a < b := by + intro a ha b hb + rw [List.mem_map] at ha hb + obtain ⟨i, hi, rfl⟩ := ha + obtain ⟨j, -, rfl⟩ := hb + exact lt_of_lt_of_le (hchainLo i (List.mem_range.mp hi)) (chain_le lo j) + have hAC : ∀ a ∈ A, ∀ c ∈ C, a < c := by + intro a ha c hc + rw [List.mem_map] at ha hc + obtain ⟨i, hiM, rfl⟩ := ha + obtain ⟨j, -, rfl⟩ := hc + exact lt_of_lt_of_le + (lt_of_lt_of_le (hchainLo i (List.mem_range.mp hiM)) h) (chain_le hi j) + have hBC : ∀ b ∈ B, ∀ c ∈ C, b < c := by + intro b hb c hc + rw [List.mem_map] at hb hc + obtain ⟨j, hj, rfl⟩ := hb + obtain ⟨k, -, rfl⟩ := hc + exact lt_of_lt_of_le (hchainGap j (List.mem_range.mp hj)) (chain_le hi k) + have hAB_sorted : (A ++ B).SortedLT := List.sortedLT_append.mpr ⟨hA, hB, hAB⟩ + have hABC : (A ++ B ++ C).SortedLT := + List.sortedLT_append.mpr ⟨hAB_sorted, hC, by + intro a ha c hc + rw [List.mem_append] at ha + rcases ha with ha | ha + · exact hAC a ha c hc + · exact hBC a ha c hc⟩ + refine ⟨⟨A ++ B ++ C, hABC⟩, ?_, ?_, ?_⟩ + · simp only [List.length_append, List.length_map, List.length_range, A, B, C] + omega + · have hcA : A.countP (fun x => decide (x < lo)) = clo := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => by + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchainLo i (List.mem_range.mp hi))] + simp [A] + have hcB : B.countP (fun x => decide (x < lo)) = 0 := + countP_lt_eq_zero_of_forall_ge (fun x hx => by + rw [List.mem_map] at hx + obtain ⟨j, -, rfl⟩ := hx + exact chain_le lo j) + have hcC : C.countP (fun x => decide (x < lo)) = 0 := + countP_lt_eq_zero_of_forall_ge (fun x hx => by + rw [List.mem_map] at hx + obtain ⟨k, -, rfl⟩ := hx + exact le_trans h (chain_le hi k)) + rw [cnt_coe] + change (A ++ B ++ C).countP (fun x => decide (x < lo)) = clo + rw [List.countP_append, List.countP_append, hcA, hcB, hcC] + omega + · have hcA : A.countP (fun x => decide (x < hi)) = clo := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => by + rw [List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact lt_of_lt_of_le (hchainLo i (List.mem_range.mp hi)) h)] + simp [A] + have hcB : B.countP (fun x => decide (x < hi)) = chi - clo := by + rw [countP_lt_eq_length_of_forall_lt (fun x hx => by + rw [List.mem_map] at hx + obtain ⟨j, hj, rfl⟩ := hx + exact hchainGap j (List.mem_range.mp hj))] + simp [B] + have hcC : C.countP (fun x => decide (x < hi)) = 0 := + countP_lt_eq_zero_of_forall_ge (fun x hx => by + rw [List.mem_map] at hx + obtain ⟨k, -, rfl⟩ := hx + exact chain_le hi k) + rw [cnt_coe] + change (A ++ B ++ C).countP (fun x => decide (x < hi)) = chi + rw [List.countP_append, List.countP_append, hcA, hcB, hcC] + omega + +/-- Cancelling a `+ 1`/`- 1` in `ℕ∞`. -/ +private theorem enat_add_one_tsub_one (m : ℕ) : ((m : ℕ∞) + 1) - 1 = (m : ℕ∞) := by + rw [← ENat.natCast_one, ← ENat.natCast_add, ← ENat.natCast_sub] + simp + +/-- From any `m + 1` elements of a set, `m` can be chosen avoiding a given member. +Take an `m + 1`-element subset; if it contains the member, dropping that member +leaves `m`, and if not, an `m`-subset of it avoids the member. -/ +private theorem exists_subset_avoid_card {S : Set Key} {k : Key} {m : ℕ} + (hcard : (m + 1 : ℕ∞) ≤ S.encard) : + ∃ t : Set Key, t ⊆ S ∧ k ∉ t ∧ t.encard = (m : ℕ∞) := by + obtain ⟨T, hTS, hTcard⟩ := Set.exists_subset_encard_eq hcard + by_cases hkT : k ∈ T + · refine ⟨T \ {k}, fun x hx => hTS hx.1, by simp, ?_⟩ + rw [Set.encard_sdiff_singleton_of_mem hkT, hTcard, enat_add_one_tsub_one] + · obtain ⟨t, htT, htcard⟩ := Set.exists_subset_encard_eq (k := (m : ℕ∞)) + (by rw [hTcard]; exact le_add_right le_rfl) + exact ⟨t, htT.trans hTS, fun hkt => hkT (htT hkt), htcard⟩ + +/-- A database of exactly `m` keys, all inside the half-open interval `keysIn B E`, +avoiding a chosen key `k` of that interval, exists whenever the interval holds at +least `m + 1` keys. The finite subset is listed in increasing order. -/ +theorem exists_db_keysIn_avoid {B E : WithTop Key} {k : Key} {m : ℕ} + (hcard : (m + 1 : ℕ∞) ≤ intervalCard B E) : + ∃ db : DB, (∀ x ∈ db.keys, x ∈ keysIn B E) ∧ k ∉ db.keys ∧ + db.keys.length = m := by + rw [intervalCard_eq_encard_keysIn] at hcard + obtain ⟨t, htS, hkt, htcard⟩ := exists_subset_avoid_card hcard + have hfin : t.Finite := Set.finite_of_encard_eq_coe htcard + let s : Finset Key := hfin.toFinset + have hmem : ∀ x, x ∈ s ↔ x ∈ t := fun x => hfin.mem_toFinset + refine ⟨⟨s.sort (· ≤ ·), Finset.sortedLT_sort s⟩, ?_, ?_, ?_⟩ + · intro x hx + exact htS ((hmem x).mp ((Finset.mem_sort (· ≤ ·)).mp hx)) + · intro hk' + exact hkt ((hmem k).mp ((Finset.mem_sort (· ≤ ·)).mp hk')) + · rw [Finset.length_sort] + have h1 : (s.card : ℕ∞) = (m : ℕ∞) := by + rw [← Set.encard_coe_eq_coe_finsetCard, hfin.coe_toFinset, htcard] + exact Nat.cast_injective h1 + +/-- A database of exactly `m` keys, all inside the half-open interval `keysIn B E`, +exists whenever the interval holds at least `m` keys. The finite subset is listed in +increasing order. -/ +theorem exists_db_keysIn {B E : WithTop Key} {m : ℕ} + (hcard : (m : ℕ∞) ≤ intervalCard B E) : + ∃ db : DB, (∀ x ∈ db.keys, x ∈ keysIn B E) ∧ db.keys.length = m := by + rw [intervalCard_eq_encard_keysIn] at hcard + obtain ⟨t, htS, htcard⟩ := Set.exists_subset_encard_eq hcard + have hfin : t.Finite := Set.finite_of_encard_eq_coe htcard + let s : Finset Key := hfin.toFinset + have hmem : ∀ x, x ∈ s ↔ x ∈ t := fun x => hfin.mem_toFinset + refine ⟨⟨s.sort (· ≤ ·), Finset.sortedLT_sort s⟩, ?_, ?_⟩ + · intro x hx + exact htS ((hmem x).mp ((Finset.mem_sort (· ≤ ·)).mp hx)) + · rw [Finset.length_sort] + have h1 : (s.card : ℕ∞) = (m : ℕ∞) := by + rw [← Set.encard_coe_eq_coe_finsetCard, hfin.coe_toFinset, htcard] + exact Nat.cast_injective h1 + +/-- The count-level feasibility system for a read: there are counts `cB, cE` of +stored keys below the two selector keys and a database length `n` satisfying the +order/interval constraints, the edge pins (`⊤` counts everything, the empty key +counts nothing) and the strict anchor comparison. -/ +def countFeasible (β ε : WithTop Key) (oB oE : ℤ) : Prop := + ∃ cB cE n : ℕ, + (β = ⊤ → cB = n) ∧ (ε = ⊤ → cE = n) ∧ + cB ≤ n ∧ cE ≤ n ∧ + (cB : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) β ∧ + (cE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε ∧ + ((cB - cE : ℕ) : ℕ∞) ≤ intervalCard ε β ∧ + ((cE - cB : ℕ) : ℕ∞) ≤ intervalCard β ε ∧ + min (((cB : ℤ) + oB - 1).toNat) n < min (((cE : ℤ) + oE - 1).toNat) n + +/-- **Soundness of the count system.** Every database whose begin anchor index +falls below its end anchor index yields counts satisfying `countFeasible`. -/ +theorem countFeasible_of_exists_db {β ε : WithTop Key} {oB oE : ℤ} + (h : ∃ db : DB, clampIndex db ⟨β, oB⟩ < clampIndex db ⟨ε, oE⟩) : + countFeasible β ε oB oE := by + obtain ⟨db, hlt⟩ := h + refine ⟨cnt db β, cnt db ε, db.keys.length, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro htop; rw [htop, cnt_top] + · intro htop; rw [htop, cnt_top] + · exact cnt_le_length db β + · exact cnt_le_length db ε + · exact cnt_le_intervalCard_nil db β + · exact cnt_le_intervalCard_nil db ε + · rcases le_total ε β with hle | hle + · exact cnt_sub_le_intervalCard db hle + · have hmono := cnt_le_cnt_of_le db hle + rw [Nat.sub_eq_zero_of_le hmono] + simp + · rcases le_total β ε with hle | hle + · exact cnt_sub_le_intervalCard db hle + · have hmono := cnt_le_cnt_of_le db hle + rw [Nat.sub_eq_zero_of_le hmono] + simp + · have h1 : Selector.anchor db (⟨β, oB⟩ : Selector) = (cnt db β : ℤ) + oB - 1 := by + simp only [Selector.anchor, cnt] + have h2 : Selector.anchor db (⟨ε, oE⟩ : Selector) = (cnt db ε : ℤ) + oE - 1 := by + simp only [Selector.anchor, cnt] + change min ((Selector.anchor db ⟨β, oB⟩).toNat) db.keys.length < + min ((Selector.anchor db ⟨ε, oE⟩).toNat) db.keys.length + rw [h1, h2] + exact hlt + +/-- **Completeness of the count system.** Any feasible counts are realized by a +database whose begin anchor index falls below its end anchor index. -/ +theorem countFeasible_complete {β ε : WithTop Key} {oB oE : ℤ} + (hf : countFeasible β ε oB oE) : + ∃ db : DB, clampIndex db ⟨β, oB⟩ < clampIndex db ⟨ε, oE⟩ := by + obtain ⟨cB, cE, n, hbt, hct, hBn, hEn, hBint, hEint, hgB, hgE, hmin⟩ := hf + cases β with + | top => + cases ε with + | top => + obtain ⟨db, hlen⟩ := exists_db_length n + have hB : cnt db (⊤ : WithTop Key) = cB := by + rw [cnt_top, hlen, ← hbt rfl] + have hE : cnt db (⊤ : WithTop Key) = cE := by + rw [cnt_top, hlen, ← hct rfl] + refine ⟨db, ?_⟩ + rw [clampIndex_eq db ⊤ oB cB hB, clampIndex_eq db ⊤ oE cE hE, hlen] + exact hmin + | coe e => + have hcE : (cE : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑e : WithTop Key) := hEint + obtain ⟨db, hlen, htop, hcoe⟩ := exists_db_top_coe (e := e) hcE hEn + have hB : cnt db (⊤ : WithTop Key) = cB := by rw [htop, ← hbt rfl] + refine ⟨db, ?_⟩ + rw [clampIndex_eq db ⊤ oB cB hB, clampIndex_eq db (↑e) oE cE hcoe, hlen] + exact hmin + | coe b => + cases ε with + | top => + have hcB : (cB : ℕ∞) ≤ intervalCard (↑([] : Key) : WithTop Key) (↑b : WithTop Key) := hBint + obtain ⟨db, hlen, hcoe, htop⟩ := exists_db_coe_top (b := b) hcB hBn + have hE : cnt db (⊤ : WithTop Key) = cE := by rw [htop, ← hct rfl] + refine ⟨db, ?_⟩ + rw [clampIndex_eq db (↑b) oB cB hcoe, clampIndex_eq db ⊤ oE cE hE, hlen] + exact hmin + | coe e => + rcases le_total b e with hbe | hbe + · have hcl : cB ≤ cE := by + by_contra hcon + have hzero : intervalCard (↑e : WithTop Key) (↑b : WithTop Key) = 0 := + intervalCard_eq_zero_of_le (WithTop.coe_le_coe.mpr hbe) + have hgg := hgB + rw [hzero] at hgg + have : cB - cE = 0 := by + by_contra hne + have hpos : 0 < cB - cE := Nat.pos_of_ne_zero hne + exact absurd hgg (not_le.mpr (by + exact_mod_cast hpos : (0 : ℕ∞) < (↑(cB - cE) : ℕ∞))) + omega + obtain ⟨db, hlen, hlo, hhi⟩ := exists_db_counts_of_le hbe hcl hBint hgE hEn + refine ⟨db, ?_⟩ + rw [clampIndex_eq db (↑b) oB cB hlo, clampIndex_eq db (↑e) oE cE hhi, hlen] + exact hmin + · have hcl : cE ≤ cB := by + by_contra hcon + have hzero : intervalCard (↑b : WithTop Key) (↑e : WithTop Key) = 0 := + intervalCard_eq_zero_of_le (WithTop.coe_le_coe.mpr hbe) + have hgg := hgE + rw [hzero] at hgg + have : cE - cB = 0 := by + by_contra hne + have hpos : 0 < cE - cB := Nat.pos_of_ne_zero hne + exact absurd hgg (not_le.mpr (by + exact_mod_cast hpos : (0 : ℕ∞) < (↑(cE - cB) : ℕ∞))) + omega + obtain ⟨db, hlen, hlo, hhi⟩ := exists_db_counts_of_le hbe hcl hEint hgB hBn + refine ⟨db, ?_⟩ + rw [clampIndex_eq db (↑b) oB cB hhi, clampIndex_eq db (↑e) oE cE hlo, hlen] + exact hmin + +/-- A read's complete keys are nonempty exactly when the begin selector's clamped +anchor index is strictly below the end selector's. -/ +theorem completeKeys_ne_nil_iff {db : DB} {r : RangeRead} : + RangeRead.completeKeys db r ≠ [] ↔ clampIndex db r.begin < clampIndex db r.end_ := by + constructor + · intro hne + obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne + obtain ⟨hkm, hkb, hke⟩ := RangeRead.mem_completeKeys.mp hk + obtain ⟨j, hj, rfl⟩ := List.mem_iff_getElem.mp hkm + have hbj : clampIndex db r.begin ≤ j := (resolve_le_getElem_iff hj).mp hkb + have hje : j < clampIndex db r.end_ := (getElem_lt_resolve_iff hj).mp hke + omega + · intro hlt + have hB : clampIndex db r.begin < db.keys.length := by + unfold clampIndex at hlt ⊢ + omega + have hk : db.keys[clampIndex db r.begin] ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr + ⟨List.getElem_mem hB, (resolve_le_getElem_iff hB).mpr (le_refl _), + (getElem_lt_resolve_iff hB).mpr hlt⟩ + intro hempty + rw [hempty] at hk + exact List.not_mem_nil hk + +/-- A read is trivially empty when every database yields no complete keys. This is +the database-free form of `NoNonemptyResult`: by `alwaysEmpty_iff_noNonemptyResult` +the two coincide because the limit is a positive natural. -/ +def AlwaysEmpty (r : RangeRead) : Prop := ∀ db : DB, RangeRead.completeKeys db r = [] + +/-- A read admits no non-empty compatible result: for every database and every +result compatible with the read, the result reports no keys. This is the property +the module docstring names "trivially empty", stated directly against +`RangeRead.Compatible` rather than through the complete keys. -/ +def NoNonemptyResult (r : RangeRead) : Prop := + ∀ (db : DB) (res : Result), RangeRead.Compatible db r res → res.keys = [] + +/-- `AlwaysEmpty` is exactly the faithful, compatibility-level form +`NoNonemptyResult`: because `limit` is a positive natural, a nonempty complete-keys +list always yields a compatible nonempty result — its one-element prefix reporting +`more` — so no database admits a nonempty compatible result precisely when every +database's complete keys are empty. -/ +theorem alwaysEmpty_iff_noNonemptyResult (r : RangeRead) : + AlwaysEmpty r ↔ NoNonemptyResult r := by + constructor + · intro h db res hc + unfold RangeRead.Compatible at hc + rw [h db] at hc + rcases hc.2 with ⟨_, hpre, hne⟩ | ⟨_, heq⟩ + · exact absurd (List.eq_nil_of_prefix_nil hpre) hne + · exact heq + · intro h db + by_contra hne + have htake : (RangeRead.completeKeys db r).take 1 ≠ [] := by + intro h0 + have hlen := congrArg List.length h0 + rw [List.length_take, List.length_nil] at hlen + have hpos := List.length_pos_of_ne_nil hne + omega + have hcomp : RangeRead.Compatible db r + { keys := (RangeRead.completeKeys db r).take 1, more := true } := by + refine ⟨?_, Or.inl ⟨rfl, List.take_prefix 1 _, htake⟩⟩ + unfold RangeRead.withinLimit + cases hlim : r.limit with + | none => trivial + | some n => + rw [List.length_take] + exact le_trans (Nat.min_le_left 1 _) (Nat.succ_le_of_lt n.pos) + exact htake (h db _ hcomp) + +/-- **The characterization.** A read is trivially empty exactly when the count +system is infeasible: no lengths and stored-key counts below the selector keys +can make the begin anchor fall strictly below the end anchor. This is a +decidable-in-principle condition on the read alone. -/ +theorem alwaysEmpty_iff_not_countFeasible (r : RangeRead) : + AlwaysEmpty r ↔ + ¬ countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset := by + constructor + · intro h hf + obtain ⟨db, hlt⟩ := countFeasible_complete hf + exact ((completeKeys_ne_nil_iff (db := db) (r := r)).mpr hlt) (h db) + · intro hf db + by_contra hne + exact hf (countFeasible_of_exists_db + ⟨db, (completeKeys_ne_nil_iff (db := db) (r := r)).mp hne⟩) + +/-- **Room below the end key.** A feasible count system with `begin.offset = 1` +and `end.offset ≤ 1` forces the count difference `cE - cB` to be at least `2 - oE`, +and the difference cap bounds it by the canonical interval `[B, E)`. The end anchor +must stay strictly above the begin anchor, and `oE ≤ 1` keeps that excess within +the `B`-to-`E` gap, so the begin count can never equal the end count. -/ +theorem two_sub_oE_le_intervalCard_of_countFeasible {B E : WithTop Key} {oE : ℤ} + (hoE : oE ≤ 1) (hfe : countFeasible B E 1 oE) : + ((2 - oE).toNat : ℕ∞) ≤ intervalCard B E := by + obtain ⟨cB, cE, n, -, -, -, -, -, -, hgB, hgE, hmin⟩ := hfe + have hA : (((cB : ℤ) + 1 - 1).toNat) = cB := by + rw [show (cB : ℤ) + 1 - 1 = (cB : ℤ) by ring, Int.toNat_natCast] + rw [hA] at hmin + have hmin1 : cB < (((cE : ℤ) + oE - 1).toNat) := by omega + have hInt : (cB : ℤ) < (cE : ℤ) + oE - 1 := by + by_cases h : (cE : ℤ) + oE - 1 ≤ 0 + · have hz : (((cE : ℤ) + oE - 1).toNat) = 0 := Int.toNat_of_nonpos h + rw [hz] at hmin1; omega + · have hz : ((((cE : ℤ) + oE - 1).toNat : ℕ) : ℤ) = (cE : ℤ) + oE - 1 := + Int.toNat_of_nonneg (le_of_not_ge h) + omega + have hcBcE : cB ≤ cE := by omega + have hle2 : (2 - oE).toNat ≤ cE - cB := by + have h2 : (((2 - oE).toNat : ℕ) : ℤ) = 2 - oE := Int.toNat_of_nonneg (by omega) + have h3 : ((cE - cB : ℕ) : ℤ) = (cE : ℤ) - cB := Nat.cast_sub hcBcE + omega + exact le_trans (by exact_mod_cast hle2) hgE + +/-- The canonical interval `[begin.key, end.key)` of a non-trivially-empty +begin-offset-one read whose end offset is at most one holds at least `2 - oE` keys. +The count system is feasible by `alwaysEmpty_iff_not_countFeasible`, and +`two_sub_oE_le_intervalCard_of_countFeasible` transfers the room to the canonical +interval. -/ +theorem two_sub_oE_le_intervalCard_canonical {r : RangeRead} + (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) (hne : ¬ AlwaysEmpty r) : + ((2 - r.end_.offset).toNat : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + have hfe : countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset := by + by_contra hc + exact hne ((alwaysEmpty_iff_not_countFeasible r).mpr hc) + rw [hbegin] at hfe + exact two_sub_oE_le_intervalCard_of_countFeasible hend hfe + +/-- **Room in `[B, E)` for the cross-free case.** A feasible count system with +`B ≤ E`, `1 ≤ oB`, `1 ≤ oE` and `e ≤ b` (where `b = (oB-1).toNat`, +`e = (oE-1).toNat`) has `b - e + 1 ≤ intervalCard B E`: the strict anchor comparison +forces the `[B, E)` count `c_E - c_B` to exceed `b - e`, so it reaches `b - e + 1`, +and the difference cap bounds it by the interval. This is the cross-free analogue of +`two_sub_oE_le_intervalCard_of_countFeasible`. -/ +theorem tsub_add_one_le_intervalCard_of_countFeasible {B E : WithTop Key} {oB oE : ℤ} + (hBE : B ≤ E) (hoB : 1 ≤ oB) (hoE : 1 ≤ oE) + (hed : (oE - 1).toNat ≤ (oB - 1).toNat) + (hfe : countFeasible B E oB oE) : + (((oB - 1).toNat - (oE - 1).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard B E := by + obtain ⟨cB, cE, n, -, -, -, -, -, -, hgB, hgE, hmin⟩ := hfe + have hb : (((cB : ℤ) + oB - 1).toNat) = cB + (oB - 1).toNat := by + rw [show ((cB : ℤ) + oB - 1) = (cB : ℤ) + (oB - 1) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + have he : (((cE : ℤ) + oE - 1).toNat) = cE + (oE - 1).toNat := by + rw [show ((cE : ℤ) + oE - 1) = (cE : ℤ) + (oE - 1) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + rw [hb, he] at hmin + have hsub : (cB - cE : ℕ) = 0 := by + have h := hgB + rw [intervalCard_eq_zero_of_le hBE] at h + have h0 : ((cB - cE : ℕ) : ℕ∞) = ((0 : ℕ) : ℕ∞) := by simpa using le_antisymm h (by simp) + exact Nat.cast_injective h0 + have hcBcE : cB ≤ cE := by omega + have hlt : cB + (oB - 1).toNat < cE + (oE - 1).toNat := by omega + have hle : (oB - 1).toNat - (oE - 1).toNat + 1 ≤ cE - cB := by omega + exact le_trans (by exact_mod_cast hle) hgE + +/-- The canonical interval `[begin.key, end.key)` of a non-trivially-empty +cross-free read with `begin.offset ≥ 1`, `end.offset ≥ 2` and `e ≤ b` holds at +least `b - e + 1` keys, where `b = (begin.offset-1).toNat`, `e = (end.offset-1).toNat`. +The count system is feasible by `alwaysEmpty_iff_not_countFeasible`, and +`tsub_add_one_le_intervalCard_of_countFeasible` transfers the room. -/ +theorem tsub_add_one_le_intervalCard_of_le_general {r : RangeRead} + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r) + (hed : (r.end_.offset - 1).toNat ≤ (r.begin.offset - 1).toNat) : + (((r.begin.offset - 1).toNat - (r.end_.offset - 1).toNat + 1 : ℕ) : ℕ∞) ≤ + intervalCard r.begin.key r.end_.key := by + have hfe : countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset := by + by_contra hc + exact hne ((alwaysEmpty_iff_not_countFeasible r).mpr hc) + exact tsub_add_one_le_intervalCard_of_countFeasible hle hbegin (by omega) hed hfe + +/-- **Room in `[B, E)` for the begin-positive `underEnd` case.** A feasible count +system with `B ≤ E`, `1 ≤ oB` and `oE ≤ 1` has +`(oB - 1).toNat + (1 - oE).toNat + 1 ≤ intervalCard B E`: the strict anchor comparison +forces the `[B, E)` count `cE - cB` to exceed `(oB - 1).toNat + (1 - oE).toNat`, and the +difference cap bounds it by the interval. This is the off-one analogue of +`two_sub_oE_le_intervalCard_of_countFeasible`. -/ +theorem add_add_one_le_intervalCard_of_countFeasible {B E : WithTop Key} {oB oE : ℤ} + (hBE : B ≤ E) (hoB : 1 ≤ oB) (hoE : oE ≤ 1) + (hfe : countFeasible B E oB oE) : + (((oB - 1).toNat + (1 - oE).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard B E := by + obtain ⟨cB, cE, n, -, -, -, -, -, -, hgB, hgE, hmin⟩ := hfe + have hb : (((cB : ℤ) + oB - 1).toNat) = cB + (oB - 1).toNat := by + rw [show ((cB : ℤ) + oB - 1) = (cB : ℤ) + (oB - 1) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + have he : (((cE : ℤ) + oE - 1).toNat) = cE - (1 - oE).toNat := by + have h1 : ((1 - oE).toNat : ℤ) = 1 - oE := Int.toNat_of_nonneg (by omega) + rw [show ((cE : ℤ) + oE - 1) = (cE : ℤ) - ((1 - oE).toNat : ℤ) by rw [h1]; ring, + Int.toNat_sub] + rw [hb, he] at hmin + have hsub : (cB - cE : ℕ) = 0 := by + have h := hgB + rw [intervalCard_eq_zero_of_le hBE] at h + have h0 : ((cB - cE : ℕ) : ℕ∞) = ((0 : ℕ) : ℕ∞) := by simpa using le_antisymm h (by simp) + exact Nat.cast_injective h0 + have hcBcE : cB ≤ cE := by omega + have hlt : cB + (oB - 1).toNat < cE - (1 - oE).toNat := by omega + have hle : (oB - 1).toNat + (1 - oE).toNat + 1 ≤ cE - cB := by omega + exact le_trans (by exact_mod_cast hle) hgE + +/-- The canonical interval `[begin.key, end.key)` of a non-trivially-empty read +with `begin.offset ≥ 1`, `end.offset ≤ 1` and `begin.key ≤ end.key` holds at least +`(begin.offset - 1).toNat + (1 - end.offset).toNat + 1` keys. The count system is +feasible by `alwaysEmpty_iff_not_countFeasible`, and +`add_add_one_le_intervalCard_of_countFeasible` transfers the room. -/ +theorem add_add_one_le_intervalCard_canonical {r : RangeRead} + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) + (hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r) : + (((r.begin.offset - 1).toNat + (1 - r.end_.offset).toNat + 1 : ℕ) : ℕ∞) ≤ + intervalCard r.begin.key r.end_.key := by + have hfe : countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset := by + by_contra hc + exact hne ((alwaysEmpty_iff_not_countFeasible r).mpr hc) + exact add_add_one_le_intervalCard_of_countFeasible hle hbegin hend hfe + +/-- **The empty-result `underEnd` case.** A realizable, non-trivially-empty read +with `begin.offset = 1` and `end_.offset ≤ 1` whose complete result is empty has the +canonical range `[begin.key, end.key)` as its minimal sound conflict range. Soundness +is the bound equivalence `compatible_empty_iff_length_le_any`; necessity takes a +database storing exactly `m = (1 - end_.offset).toNat` keys of the canonical interval +and omitting the chosen key, then inserts it to break the bound. The hypothesis +`¬ AlwaysEmpty r` is what supplies the room `m + 1 ≤ intervalCard begin.key end_.key`; +without it the canonical range may be nonempty while the empty range is already +minimal. -/ +theorem minimal_canonicalRange_of_underEnd_empty {r : RangeRead} {res : Result} + (hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hbegin : r.begin.offset = 1) + (hend : r.end_.offset ≤ 1) (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res (canonicalRange r) := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + set m : ℕ := (1 - r.end_.offset).toNat with hm + have hm1 : m + 1 = (2 - r.end_.offset).toNat := by + have h1 : (0 : ℤ) ≤ 1 - r.end_.offset := by omega + have h2 : (0 : ℤ) ≤ 2 - r.end_.offset := by omega + apply Nat.cast_injective (R := ℤ) + rw [Nat.cast_add, Nat.cast_one, hm, Int.toNat_of_nonneg h1, Int.toNat_of_nonneg h2] + omega + have hroom : (m + 1 : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + have h := two_sub_oE_le_intervalCard_canonical hbegin hend hgap + rwa [← hm1] at h + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hkey : ((canonicalRange r).keys db₁).length = + ((canonicalRange r).keys db₂).length := by + rw [show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree] + rw [compatible_empty_iff_length_le_any (db := db₁) hbegin hend, + compatible_empty_iff_length_le_any (db := db₂) hbegin hend, hkey] + · intro k hlo hhi + have hkcan : k ∈ canonicalRange r := ConflictRange.mem_iff.mpr ⟨hlo, hhi⟩ + obtain ⟨db₀, hdb₀range, hkndb, hlen⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) hroom + have hcan₀ : (canonicalRange r).keys db₀ = db₀.keys := + canonicalRange_keys_eq_self_of_forall fun x hx => hdb₀range x hx + have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := by + rw [compatible_empty_iff_length_le_any (db := db₀) hbegin hend, hcan₀, hlen] + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₀ (db₀.insert k hkndb) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hkndb hkcr).symm + have hcomp₁ : RangeRead.Compatible (db₀.insert k hkndb) r + { keys := [], more := false } := (hs db₀ _ hagree).mp hcomp₀ + have hcan₁ : (canonicalRange r).keys (db₀.insert k hkndb) = + (db₀.insert k hkndb).keys := by + refine canonicalRange_keys_eq_self_of_forall fun x hx => ?_ + rw [DB.mem_insert_iff hkndb] at hx + rcases hx with rfl | hx + · exact hkcan + · exact hdb₀range x hx + have hlen₁ : ((canonicalRange r).keys (db₀.insert k hkndb)).length = m + 1 := by + rw [hcan₁, DB.insert_keys, List.orderedInsert_length, hlen] + have hnot : ¬ RangeRead.Compatible (db₀.insert k hkndb) r + { keys := [], more := false } := by + rw [compatible_empty_iff_length_le_any (db := db₀.insert k hkndb) hbegin hend, hlen₁] + omega + exact hnot hcomp₁ + +/-- **A crossed read with `begin.offset ≥ 1` and `end.offset ≤ 1` is trivially +empty.** If `end.key < begin.key`, then `cend ≤ cbegin`, and with `oE ≤ 1 ≤ oB` +the end anchor is at most the begin anchor at every database, so the window is +always empty. -/ +theorem alwaysEmpty_of_begin_gt_end {r : RangeRead} (hbegin : 1 ≤ r.begin.offset) + (hend : r.end_.offset ≤ 1) (hcross : r.end_.key < r.begin.key) : AlwaysEmpty r := by + intro db + rw [← not_ne_iff, completeKeys_ne_nil_iff] + have hea : r.end_.anchor db ≤ r.begin.anchor db := by + have hc : (cnt db r.end_.key : ℤ) ≤ cnt db r.begin.key := by + exact_mod_cast cnt_le_cnt_of_le db (le_of_lt hcross) + have ho : r.end_.offset ≤ r.begin.offset := by omega + simp only [Selector.anchor, cnt] at hc ⊢ + omega + exact not_lt.mpr (min_le_min (Int.toNat_le_toNat hea) le_rfl) + +/-- **A non-trivially-empty read with `begin.offset ≥ 1` and `end.offset ≤ 1` is +cross-free.** The contrapositive of `alwaysEmpty_of_begin_gt_end`: if `end.key < begin.key` +the read is trivially empty, so a non-trivially-empty read has `begin.key ≤ end.key`. -/ +theorem begin_key_le_end_key_of_not_alwaysEmpty {r : RangeRead} + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) (hgap : ¬ AlwaysEmpty r) : + r.begin.key ≤ r.end_.key := by + by_contra h + exact hgap (alwaysEmpty_of_begin_gt_end hbegin hend (not_le.mp h)) + +/-- **The empty-result `underEnd` case at a general positive begin offset.** A +realizable, non-trivially-empty read with `1 ≤ begin.offset` and `end_.offset ≤ 1`, +`begin.key ≤ end.key`, whose complete result is empty, has the canonical range +`[begin.key, end.key)` as its minimal sound conflict range. Soundness is the bound +equivalence `compatible_empty_iff_length_sub_le_any`. Necessity takes a database of +exactly `b + k` keys of the canonical interval, where `b = (begin.offset - 1).toNat` +and `k = (1 - end.offset).toNat`, avoiding the chosen key, then inserts it: the +canonical count rises by one, so the complete keys, a left-and-right truncation of +the canonical keys, become nonempty. The hypothesis `¬ AlwaysEmpty r` supplies the +room `b + k + 1 ≤ intervalCard begin.key end.key`. -/ +theorem minimal_canonicalRange_of_underEnd_empty_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hbegin : 1 ≤ r.begin.offset) + (hend : r.end_.offset ≤ 1) (hle : r.begin.key ≤ r.end_.key) + (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res (canonicalRange r) := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + set b : ℕ := (r.begin.offset - 1).toNat with hb + set k : ℕ := (1 - r.end_.offset).toNat with hk + have hroom : (b + k + 1 : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + rw [hb, hk]; exact add_add_one_le_intervalCard_canonical hbegin hend hle hgap + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hkey : ((canonicalRange r).keys db₁).length = + ((canonicalRange r).keys db₂).length := by + rw [show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree] + rw [compatible_empty_iff_length_sub_le_any (db := db₁) hbegin hend, + compatible_empty_iff_length_sub_le_any (db := db₂) hbegin hend, hkey] + · intro m hlo hhi + have hmcan : m ∈ canonicalRange r := ConflictRange.mem_iff.mpr ⟨hlo, hhi⟩ + obtain ⟨db₀, hdb₀range, hmndb, hlen⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := m) (m := b + k) hroom + have hcan₀ : (canonicalRange r).keys db₀ = db₀.keys := + canonicalRange_keys_eq_self_of_forall fun x hx => hdb₀range x hx + have hcomp₀ : RangeRead.Compatible db₀ r { keys := [], more := false } := by + rw [compatible_empty_iff_length_sub_le_any (db := db₀) hbegin hend, hcan₀, hlen] + omega + intro cr hs + by_contra hmcr + have hagree : AgreeOn cr db₀ (db₀.insert m hmndb) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hmndb hmcr).symm + have hcomp₁ : RangeRead.Compatible (db₀.insert m hmndb) r + { keys := [], more := false } := (hs db₀ _ hagree).mp hcomp₀ + have hcan₁ : (canonicalRange r).keys (db₀.insert m hmndb) = + (db₀.insert m hmndb).keys := by + refine canonicalRange_keys_eq_self_of_forall fun x hx => ?_ + rw [DB.mem_insert_iff hmndb] at hx + rcases hx with rfl | hx + · exact hmcan + · exact hdb₀range x hx + have hlen₁ : ((canonicalRange r).keys (db₀.insert m hmndb)).length = b + k + 1 := by + rw [hcan₁, DB.insert_keys, List.orderedInsert_length, hlen] + have hnot : ¬ RangeRead.Compatible (db₀.insert m hmndb) r + { keys := [], more := false } := by + rw [compatible_empty_iff_length_sub_le_any (db := db₀.insert m hmndb) hbegin hend, + hlen₁] + omega + exact hnot hcomp₁ + +/-- The minimal canonical range in the empty-result `underEnd` case is unique up to +set equality. -/ +theorem unique_minimal_canonicalRange_of_underEnd_empty {r : RangeRead} {res : Result} + (hread : Readable r res) (hgap : ¬ AlwaysEmpty r) (hbegin : r.begin.offset = 1) + (hend : r.end_.offset ≤ 1) (hmore : res.more = false) (hempty : res.keys = []) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_canonicalRange_of_underEnd_empty hread hgap hbegin hend hmore hempty) + +/-- **The empty result at an overshooting end, cross-free.** With `begin.offset = 1`, +`end_.offset ≥ 2` and `begin.key ≤ end_.key`, an empty complete result and the top +range `[begin.key, ⊤)` are compatible with exactly the same databases: if any stored +key reached `begin.key`, the first such key would be `resolve begin`, and the +overshoot would put it strictly below `resolve end_`, so it would have been reported. +Hence an empty result holds exactly when the top range stores nothing. -/ +theorem completeKeys_eq_nil_iff_topRange_keys_eq_nil {db : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) : + RangeRead.completeKeys db r = [] ↔ (topRange r).keys db = [] := by + constructor + · intro hce + rw [List.eq_nil_iff_forall_not_mem] + intro x hx + rw [ConflictRange.mem_keys_iff] at hx + obtain ⟨hxdb, hxge, -⟩ := hx + have hclt : cnt db r.begin.key < db.keys.length := by + rw [cnt, List.countP_lt_length_iff] + exact ⟨x, hxdb, by rw [decide_eq_false_iff_not, not_lt]; exact hxge⟩ + have hnotlt : ¬ (clampIndex db r.begin < clampIndex db r.end_) := + fun h => (completeKeys_ne_nil_iff (db := db) (r := r)).mpr h hce + have hleC : clampIndex db r.end_ ≤ clampIndex db r.begin := not_lt.mp hnotlt + have htoNat : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) + = cnt db r.end_.key + (r.end_.offset - 1).toNat := by + rw [show ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) = + (cnt db r.end_.key : ℤ) + (r.end_.offset - 1) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + rw [clampIndex_begin_eq hbegin, Nat.min_eq_left (le_of_lt hclt), + clampIndex_end_eq, htoNat] at hleC + have hmono := cnt_le_cnt_of_le db hle + have ho1 : 1 ≤ (r.end_.offset - 1).toNat := by omega + have hA : cnt db r.end_.key + (r.end_.offset - 1).toNat ≤ cnt db r.begin.key := by + rcases le_total (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length with h | h + · rwa [Nat.min_eq_left h] at hleC + · rw [Nat.min_eq_right h] at hleC; omega + omega + · intro htr + have hcBn : cnt db r.begin.key = db.keys.length := by + rw [cnt, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + by_contra hlt + have : x ∈ (topRange r).keys db := + ConflictRange.mem_keys_iff.mpr ⟨hx, le_of_not_gt hlt, WithTop.coe_lt_top x⟩ + rw [htr] at this; exact List.not_mem_nil this + have hnotlt : ¬ (clampIndex db r.begin < clampIndex db r.end_) := by + rw [clampIndex_begin_eq hbegin, hcBn, Nat.min_self] + exact not_lt.mpr (by rw [clampIndex_end_eq]; exact Nat.min_le_right _ _) + by_contra hne + exact hnotlt ((completeKeys_ne_nil_iff (db := db) (r := r)).mp hne) + +/-- **The minimal top range for an empty complete result, cross-free.** With +`begin.offset = 1`, `end_.offset ≥ 2`, `begin.key ≤ end_.key` and a non-reversed +read, the unbounded top `[begin.key, ⊤)` is the minimal sound conflict range for the +empty complete result. Both soundness and necessity reduce to +`completeKeys_eq_nil_iff_topRange_keys_eq_nil`: compatibility is equivalent to the +top range storing nothing, so any two databases agreeing on the top range agree on +compatibility, and inserting a missing `k` into the top range makes it nonempty and +so breaks the empty result. -/ +theorem minimal_topRange_of_end_offset_ge_two_empty {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (_hrev : r.reverse = false) + (hempty : res.keys = []) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) : + Minimal r res (topRange r) := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + rw [compatible_empty_iff_complete_nil (db := db₁) (r := r), + compatible_empty_iff_complete_nil (db := db₂) (r := r), + completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₁) hbegin hend hle, + completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₂) hbegin hend hle, + show (topRange r).keys db₁ = (topRange r).keys db₂ from hagree] + · intro k hk cr hs + obtain ⟨hklo, _⟩ := ConflictRange.mem_iff.mp hk + by_contra hkcr + obtain ⟨db₀, hdb₀⟩ := hread + have h0nil : (topRange r).keys db₀ = [] := + (completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₀) hbegin hend hle).mp + ((compatible_empty_iff_complete_nil (db := db₀) (r := r)).mp hdb₀) + have hknot : k ∉ db₀.keys := by + intro hkmem + have : k ∈ (topRange r).keys db₀ := ConflictRange.mem_keys_iff.mpr ⟨hkmem, hk⟩ + rw [h0nil] at this; exact List.not_mem_nil this + have hagree : AgreeOn cr db₀ (db₀.insert k hknot) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hknot hkcr).symm + have hcomp₁ : RangeRead.Compatible (db₀.insert k hknot) r { keys := [], more := false } := + (hs db₀ _ hagree).mp hdb₀ + have h1ne : (topRange r).keys (db₀.insert k hknot) ≠ [] := by + intro hnil + have : k ∈ (topRange r).keys (db₀.insert k hknot) := + ConflictRange.mem_keys_iff.mpr ⟨DB.mem_insert_self _ _ _, hk⟩ + rw [hnil] at this; exact List.not_mem_nil this + exact h1ne ((completeKeys_eq_nil_iff_topRange_keys_eq_nil (db := db₀.insert k hknot) + hbegin hend hle).mp ((compatible_empty_iff_complete_nil + (db := db₀.insert k hknot) (r := r)).mp hcomp₁)) + +/-- The minimal top range for an empty complete result at an overshooting end is +unique up to set equality. -/ +theorem unique_minimal_topRange_of_end_offset_ge_two_empty {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hempty : res.keys = []) (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topRange_of_end_offset_ge_two_empty hread hmore hrev hempty hbegin hend hle) + +/-! +## A non-trivial always-empty read + +The `countFeasible` characterization is exact, so `AlwaysEmpty` catches reads that +no offset comparison alone can. The witness is `readPinned`: both selectors sit at +the empty key with the end offset ahead of the begin offset, yet the count below the +empty key is zero, so both clamped anchors land at `0` and the window is empty for +every database. +-/ + +/-- A read whose two selectors both sit at the empty key, with begin offset zero and +end offset one, is always empty: the count below the empty key is zero, so the begin +anchor is `-1` and resolves to the empty key, while the end anchor is `0` and +resolves to the first stored key (or `⊤` against the empty database). The interval +`[⊥, resolve end)` therefore holds no key: nothing is stored strictly below the +least stored key, even though the end offset is ahead of the begin offset. -/ +def readPinned : RangeRead where + begin := { key := (([] : Key) : WithTop Key), offset := 0 } + end_ := { key := (([] : Key) : WithTop Key), offset := 1 } + limit := none + reverse := false + +/-- `readPinned` is always empty: the begin anchor is `-1` (its count below the +empty key is zero) and the end anchor is `0`, so both clamped indices are `0`. -/ +theorem alwaysEmpty_readPinned : AlwaysEmpty readPinned := by + intro db + by_contra hne + have hlt := (completeKeys_ne_nil_iff (db := db) (r := readPinned)).mp hne + have hb : clampIndex db readPinned.begin = 0 := by + have h := clampIndex_eq db (([] : Key) : WithTop Key) readPinned.begin.offset 0 + (cnt_nil db) + have : readPinned.begin = ⟨(([] : Key) : WithTop Key), readPinned.begin.offset⟩ := rfl + rw [this, h] + norm_num [readPinned] + have he : clampIndex db readPinned.end_ = 0 := by + have h := clampIndex_eq db (([] : Key) : WithTop Key) readPinned.end_.offset 0 + (cnt_nil db) + have : readPinned.end_ = ⟨(([] : Key) : WithTop Key), readPinned.end_.offset⟩ := rfl + rw [this, h] + norm_num [readPinned] + rw [hb, he] at hlt + exact Nat.lt_irrefl 0 hlt + +/-- **Non-vacuity of the feasible side.** `readAll` can return a key: against the +one-key database `[[0]]` it returns that key. -/ +theorem not_alwaysEmpty_readAll : ¬ AlwaysEmpty RangeRead.readAll := by + intro h + have hsorted : ([[(0 : Byte)]] : List Key).SortedLT := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + have hk : RangeRead.completeKeys (⟨[[(0 : Byte)]], hsorted⟩ : DB) RangeRead.readAll = + [[(0 : Byte)]] := by + rw [RangeRead.completeKeys_readAll] + exact (by decide : ([[(0 : Byte)]] : List Key) ≠ []) (hk ▸ h _) + +/-- The count system is feasible for `readAll`. -/ +theorem countFeasible_readAll : + countFeasible RangeRead.readAll.begin.key RangeRead.readAll.end_.key + RangeRead.readAll.begin.offset RangeRead.readAll.end_.offset := by + refine countFeasible_of_exists_db ⟨⟨[[(0 : Byte)]], ?_⟩, ?_⟩ + · rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + · decide + +/-! +## Deciding the system: eliminating the database length + +`countFeasible` quantifies over `cB`, `cE` and `n`, and the conjuncts are all +difference constraints `x - y ≤ k` once `n` is handled. The only conjunct that +couples `n` to the anchors is `min a n < min b n`, and + + `min a n < min b n ↔ a < b ∧ a < n` + +isolates the length into the single side condition `a < n`. Since the remaining +`n`-constraints are `cB ≤ n`, `cE ≤ n` and the edge pins `c = n`, the length can be +chosen minimally as the maximum of the lower bounds whenever the anchor inequality +is satisfiable, so `n` is eliminable and `countFeasible` reduces to a bounded +condition on `cB` and `cE` alone. +-/ + +/-- The `min`-with-length conjunct is a pure anchor ordering plus a lower bound on +the length: `min a n < min b n` exactly when `a < b` and `a` is still below `n`. -/ +theorem min_min_lt_iff (a b n : ℕ) : min a n < min b n ↔ a < b ∧ a < n := by + omega + +/-- The `min`-with-length predicate is monotone in the length: if it holds at `n` it +holds at any larger length, since `a < n` persists. -/ +theorem min_min_lt_mono {a b : ℕ} {n m : ℕ} (hnm : n ≤ m) (h : min a n < min b n) : + min a m < min b m := by + rw [min_min_lt_iff] at h ⊢ + exact ⟨h.1, lt_of_lt_of_le h.2 hnm⟩ + +/-- The length can be clamped upward to at least `a + 1` once the `min`-conjunct +holds: the only role of the length is to exceed the begin anchor `a`. -/ +theorem min_min_lt_max_succ {a b n : ℕ} (h : min a n < min b n) : + min a (max n (a + 1)) < min b (max n (a + 1)) := + min_min_lt_mono (le_max_left _ _) h + +/-- The anchor `(c + o - 1).toNat` is below the length `n` exactly when `n` is +positive and the signed anchor is below `n`. This turns the length lower bound into +ordinary difference constraints on `c` and `n`. -/ +theorem toNat_anchor_lt_iff (c : ℕ) (o : ℤ) (n : ℕ) : + ((c : ℤ) + o - 1).toNat < n ↔ 0 < n ∧ (c : ℤ) + o - 1 < (n : ℤ) := by + rcases lt_or_ge 0 ((c : ℤ) + o - 1) with h | h + · rw [Int.toNat_lt (le_of_lt h)] + omega + · have hz : ((c : ℤ) + o - 1).toNat = 0 := Int.toNat_of_nonpos h + rw [hz] + omega + +/-- Two anchors are ordered below the length exactly when the end anchor is +positive, the begin anchor precedes it, the length is positive, and the begin +anchor is below the length. The clamped-`Nat` comparison collapses to a pure +integer condition: the clamp to the empty key is subsumed by `0 < end` and +`begin < end`. -/ +theorem clamp_lt_iff (cB cE n : ℕ) (oB oE : ℤ) : + min (((cB : ℤ) + oB - 1).toNat) n < min (((cE : ℤ) + oE - 1).toNat) n ↔ + 0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧ + 0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ) := by + rw [min_min_lt_iff] + have hb : ((cB : ℤ) + oB - 1).toNat < ((cE : ℤ) + oE - 1).toNat ↔ + 0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 := by + constructor + · intro h + exact ⟨by omega, Int.lt_of_toNat_lt h⟩ + · rintro ⟨h0, hlt⟩ + exact (Int.toNat_lt_toNat h0).mpr hlt + rw [hb, toNat_anchor_lt_iff] + tauto + +/-- **The empty result at a nonpositive begin offset.** With `begin.offset ≤ 0` the +begin anchor is `cB + oB - 1 ≤ cB - 1 < n`, so it never clamps at `⊤` and the clamped +comparison collapses to `cE + oE - 1 ≤ (cB + oB - 1).toNat`, with the sole extra +boundary being the empty database. Here `cB, cE` are the stored counts below the two +selector keys. This is the case where the canonical range is unsound, so the value +built from it below is a genuinely different candidate. -/ +theorem compatible_empty_iff_clamp {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + db.keys = [] ∨ + (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ + (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat : ℤ) := by + have hiff : RangeRead.Compatible db r { keys := [], more := false } ↔ + ¬ (clampIndex db r.begin < clampIndex db r.end_) := by + rw [compatible_empty_iff_complete_nil, ← completeKeys_ne_nil_iff]; simp only [not_ne_iff] + rw [hiff, clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl, + clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl] + have halt : ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) < (db.keys.length : ℤ) := by + have := cnt_le_length db r.begin.key + omega + have han : ((((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat)) ≤ db.keys.length := + Int.toNat_le.mpr (le_of_lt halt) + rw [Nat.min_eq_left han, not_lt] + constructor + · intro h + by_cases hn0 : db.keys.length = 0 + · exact Or.inl (by rw [List.eq_nil_iff_length_eq_zero]; exact hn0) + · refine Or.inr ?_ + -- Here the begin anchor `aNat < n`, so `min eNat n ≤ aNat` forces `eNat ≤ aNat`. + have haLt : (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) < db.keys.length := by + rcases lt_or_ge 0 ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) with h0 | h0 + · rw [Int.toNat_lt (le_of_lt h0)]; exact halt + · rw [Int.toNat_of_nonpos h0]; omega + have hEA : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤ + (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) := by + by_cases hEn : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤ db.keys.length + · rw [Nat.min_eq_left hEn] at h; exact h + · exfalso + rw [Nat.min_eq_right (le_of_lt (not_le.mp hEn))] at h + omega + have hEmax : ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) ≤ + ((((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat : ℕ) : ℤ) := by + rw [Int.toNat_eq_max]; omega + omega + · rintro (hnil | hle) + · rw [List.eq_nil_iff_length_eq_zero] at hnil + rw [hnil]; simp + · have : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤ + (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) := by + simpa using Int.toNat_le_toNat hle + exact le_trans (Nat.min_le_left _ _) this + +/-- The arithmetic core of `countFeasible`, with the clamped comparison replaced by +its pure integer form. This is the difference-constraint system on `cB`, `cE`, `n` +whose feasibility `countFeasible` decides. -/ +def countFeasibleInt (β ε : WithTop Key) (oB oE : ℤ) : Prop := + ∃ cB cE n : ℕ, + (β = ⊤ → cB = n) ∧ (ε = ⊤ → cE = n) ∧ + cB ≤ n ∧ cE ≤ n ∧ + (cB : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) β ∧ + (cE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε ∧ + ((cB - cE : ℕ) : ℕ∞) ≤ intervalCard ε β ∧ + ((cE - cB : ℕ) : ℕ∞) ≤ intervalCard β ε ∧ + 0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧ + 0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ) + +/-- `countFeasible` has the same solutions as its pure integer form: the clamped +comparison is equivalent to the integer conjuncts by `clamp_lt_iff`. -/ +theorem countFeasible_iff_int (β ε : WithTop Key) (oB oE : ℤ) : + countFeasible β ε oB oE ↔ countFeasibleInt β ε oB oE := by + unfold countFeasible countFeasibleInt + refine exists_congr fun cB => exists_congr fun cE => exists_congr fun n => ?_ + constructor + · rintro ⟨hbt, hct, h1, h2, h3, h4, h5, h6, h7⟩ + exact ⟨hbt, hct, h1, h2, h3, h4, h5, h6, (clamp_lt_iff cB cE n oB oE).mp h7⟩ + · rintro ⟨hbt, hct, h1, h2, h3, h4, h5, h6, h7⟩ + exact ⟨hbt, hct, h1, h2, h3, h4, h5, h6, (clamp_lt_iff cB cE n oB oE).mpr h7⟩ + +/-! +## A finite witness bound + +The witnesses to `countFeasible` can be taken bounded by the offsets alone: +`|oB| + |oE| + 3`, independently of the interval cardinalities. Intuitively the +counts only matter up to their order, so the least feasible triple is determined +by how far the two anchors can be pushed apart, which the offsets control; the +caps only forbid large values, never require them. + +The proof fixes the *smallest* `cE` forced by the end anchor (`2 - oE`) and the +room the difference needs (`oB - oE + 1`), then takes `cB` as the smallest value +allowed by the difference cap `intervalCard β ε`, and the length as the least +value exceeding both counts and the begin anchor. +-/ + +/-- A count `c` is at most an `ℕ∞` cap exactly when it is at most the cap's +`toNat` or the cap is `⊤`. -/ +theorem natCast_le_iff_toNat (c : ℕ) (κ : ℕ∞) : (c : ℕ∞) ≤ κ ↔ c ≤ κ.toNat ∨ κ = ⊤ := by + cases κ with + | top => simp + | coe k => simp + +/-- A truncated difference is at most an `ℕ∞` cap exactly when it is at most the +cap's `toNat` or the cap is `⊤`. -/ +theorem tsub_natCast_le_iff_toNat (a b : ℕ) (κ : ℕ∞) : + ((a - b : ℕ) : ℕ∞) ≤ κ ↔ a - b ≤ κ.toNat ∨ κ = ⊤ := by + cases κ with + | top => simp + | coe k => + simp only [ENat.toNat_natCast] + constructor + · intro h; exact Or.inl (ENat.natCast_le_natCast.mp h) + · rintro (h | h) + · exact_mod_cast h + · exact absurd h (by simp) + +/-- The offset datum `max (2 - oE) (oB - oE + 1)` as a natural number: the least +end count `cE` compatible with the end anchor and with room for the begin anchor. +Any feasible solution has `cE` at least this. -/ +def cELo (oB oE : ℤ) : ℕ := max ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat + +/-- `cELo` is a lower bound for any feasible `cE`. -/ +theorem cELo_le {oB oE : ℤ} {cB cE : ℕ} + (h1 : 0 < (cE : ℤ) + oE - 1) (h2 : (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1) : + cELo oB oE ≤ cE := by + unfold cELo + have ha : ((2 : ℤ) - oE).toNat ≤ cE := by omega + have hb : (oB - oE + 1).toNat ≤ cE := by omega + omega + +/-- `cELo` still satisfies the end-anchor positivity and can carry the begin +anchor at any `cB ≤ cELo + (oE - oB) - 1`; in particular it is within +`|oB| + |oE| + 3`. -/ +theorem cELo_anchor_le {oB oE : ℤ} : + (cELo oB oE : ℤ) + oE - 1 < (oB.natAbs + oE.natAbs + 3 : ℕ) := by + have ha : (((2 : ℤ) - oE).toNat : ℤ) + oE - 1 < (oB.natAbs : ℤ) + oE.natAbs + 3 := by + rcases lt_or_ge oE 2 with h | h + · have : (0 : ℤ) ≤ 2 - oE := by omega + rw [Int.toNat_of_nonneg this] + have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega + · have : ((2 : ℤ) - oE).toNat = 0 := Int.toNat_of_nonpos (by omega) + rw [this] + have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega + have hb : (((oB - oE + 1).toNat : ℤ)) + oE - 1 < (oB.natAbs : ℤ) + oE.natAbs + 3 := by + rcases lt_or_ge 0 (oB - oE + 1) with h | h + · rw [Int.toNat_of_nonneg (by omega)] + have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega + · have hz : ((oB - oE + 1).toNat : ℤ) = 0 := by + rw [Int.toNat_of_nonpos (by omega)]; simp + rw [hz] + have := Int.le_natAbs (a := oE); have := Int.le_natAbs (a := oB); omega + unfold cELo + rcases le_total ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat with h | h + · rw [max_eq_right h]; exact hb + · rw [max_eq_left h]; exact ha + +theorem cELo_spec {oB oE : ℤ} : + 0 < (cELo oB oE : ℤ) + oE - 1 ∧ cELo oB oE ≤ oB.natAbs + oE.natAbs + 3 := by + unfold cELo + constructor <;> omega + +/-- The end anchor at `cELo` sits strictly above the begin anchor, so `cELo` leaves +room for some `cB`: `oB - oE < cELo oB oE` in `ℤ`. -/ +theorem cELo_gt (oB oE : ℤ) : oB - oE < (cELo oB oE : ℤ) := by + unfold cELo + have h := le_max_right ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat + have hle : (((oB - oE + 1).toNat : ℕ) : ℤ) ≤ + (max ((2 : ℤ) - oE).toNat (oB - oE + 1).toNat : ℕ) := by exact_mod_cast h + rcases lt_or_ge 0 (oB - oE + 1) with hpos | hneg + · have : ((oB - oE + 1).toNat : ℤ) = oB - oE + 1 := Int.toNat_of_nonneg (by omega) + omega + · have : ((oB - oE + 1).toNat : ℤ) = 0 := by + rw [Int.toNat_of_nonpos (by omega)]; simp + omega + +/-- **Core witness reduction, no edge pins.** Given any feasible triple for the +count system, there is one with `cE` the offset datum `cELo`, `cB` the least value +allowed by the difference cap `intervalCard β ε`, and all three bounded by +`|oB| + |oE| + 3`. The caps only forbid large values, so shrinking to the least +compatible counts preserves feasibility. -/ +theorem exists_bounded_of_noPin {KB KE KEB KBE : ℕ∞} {oB oE : ℤ} + (h : ∃ cB cE n : ℕ, + (cB : ℕ∞) ≤ KB ∧ (cE : ℕ∞) ≤ KE ∧ + ((cB - cE : ℕ) : ℕ∞) ≤ KEB ∧ ((cE - cB : ℕ) : ℕ∞) ≤ KBE ∧ + cB ≤ n ∧ cE ≤ n ∧ + 0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧ + 0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ)) : + ∃ cB cE n : ℕ, + (cB : ℕ∞) ≤ KB ∧ (cE : ℕ∞) ≤ KE ∧ + ((cB - cE : ℕ) : ℕ∞) ≤ KEB ∧ ((cE - cB : ℕ) : ℕ∞) ≤ KBE ∧ + cB ≤ n ∧ cE ≤ n ∧ + 0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧ + 0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ) ∧ + cB ≤ oB.natAbs + oE.natAbs + 3 ∧ cE ≤ oB.natAbs + oE.natAbs + 3 ∧ + n ≤ oB.natAbs + oE.natAbs + 3 := by + obtain ⟨cB, cE, n, hKB, hKE, hKEB, hKBE, h1, h2, h3, h4, h5, h6⟩ := h + have hce_lo : cELo oB oE ≤ cE := cELo_le h3 h4 + have hspec := cELo_spec (oB := oB) (oE := oE) + have hd2 : cE - cB ≤ KBE.toNat ∨ KBE = ⊤ := (tsub_natCast_le_iff_toNat cE cB KBE).mp hKBE + have hcb_cap : cB ≤ KB.toNat ∨ KB = ⊤ := (natCast_le_iff_toNat cB KB).mp hKB + have hce_cap : cE ≤ KE.toNat ∨ KE = ⊤ := (natCast_le_iff_toNat cE KE).mp hKE + set e0 := cELo oB oE with he0 + set b0 : ℕ := if KBE = ⊤ then 0 else max 0 (e0 - KBE.toNat) with hb0 + have hb0_cB : b0 ≤ cB := by + by_cases hBE : KBE = ⊤ + · simp only [hb0, hBE, ↓reduceIte]; omega + · simp only [hb0, hBE, ↓reduceIte] + rcases hd2 with hd | hd + · omega + · exact absurd hd hBE + have hb0_e0 : b0 ≤ e0 := by + by_cases hBE : KBE = ⊤ <;> simp only [hb0, hBE, ↓reduceIte] <;> omega + have hb0_KB : (b0 : ℕ∞) ≤ KB := by + rw [natCast_le_iff_toNat] + by_cases hB : KB = ⊤ + · exact Or.inr hB + · left; rcases hcb_cap with h | h + · omega + · exact absurd h hB + have he0_KE : (e0 : ℕ∞) ≤ KE := by + rw [natCast_le_iff_toNat] + rcases hce_cap with h | h + · exact Or.inl (le_trans hce_lo h) + · exact Or.inr h + have hd1' : ((b0 - e0 : ℕ) : ℕ∞) ≤ KEB := by + have : b0 - e0 = 0 := Nat.sub_eq_zero_of_le hb0_e0 + rw [this]; exact bot_le + have hd2' : ((e0 - b0 : ℕ) : ℕ∞) ≤ KBE := by + rw [tsub_natCast_le_iff_toNat] + by_cases hBE : KBE = ⊤ + · exact Or.inr hBE + · left + have hb : b0 = max 0 (e0 - KBE.toNat) := by simp only [hb0, hBE, ↓reduceIte] + omega + have he0_ge : (0 : ℤ) < (e0 : ℤ) + oE - 1 := by rw [he0]; exact hspec.1 + have he0gt : oB - oE < (e0 : ℤ) := by rw [he0]; exact cELo_gt oB oE + have hord : (b0 : ℤ) + oB - 1 < (e0 : ℤ) + oE - 1 := by + by_cases hBE : KBE = ⊤ + · have hb : b0 = 0 := by simp only [hb0, hBE, ↓reduceIte] + rw [hb]; omega + · have hb : b0 = max 0 (e0 - KBE.toNat) := by simp only [hb0, hBE, ↓reduceIte] + rw [hb] + have hlt : oB - oE < (KBE.toNat : ℤ) := by + rcases hd2 with hd | hd + · omega + · exact absurd hd hBE + by_cases hle : e0 ≤ KBE.toNat + · rw [max_eq_left (by omega)]; omega + · rw [max_eq_right (by omega)]; omega + have he0_bound : e0 ≤ oB.natAbs + oE.natAbs + 3 := by rw [he0]; exact hspec.2 + have hb0_bound : b0 ≤ oB.natAbs + oE.natAbs + 3 := le_trans hb0_e0 he0_bound + have hBpos : 0 < oB.natAbs + oE.natAbs + 3 := by positivity + have hA : (e0 : ℤ) + oE - 1 < (oB.natAbs + oE.natAbs + 3 : ℕ) := by + rw [he0]; exact cELo_anchor_le + have hbeg : (b0 : ℤ) + oB - 1 < (oB.natAbs + oE.natAbs + 3 : ℕ) := + lt_trans hord hA + exact ⟨b0, e0, oB.natAbs + oE.natAbs + 3, hb0_KB, he0_KE, hd1', hd2', + hb0_bound, he0_bound, he0_ge, hord, hBpos, hbeg, + hb0_bound, he0_bound, le_refl _⟩ + +/-- The begin count of the canonical no-pin witness: the least value within the +difference cap `KBE` of the least end count `cELo`. -/ +def noPinB (KBE : ℕ∞) (oB oE : ℤ) : ℕ := + if KBE = ⊤ then 0 else max 0 (cELo oB oE - KBE.toNat) + +/-- The canonical no-pin length bound. -/ +def noPinN (oB oE : ℤ) : ℕ := oB.natAbs + oE.natAbs + 3 + +/-- **No-pin closed form.** For the genuine/genuine case the count system is +feasible exactly when its canonical candidate — end count `cELo`, begin count +`noPinB`, length `noPinN` — satisfies the quantifier-free constraints. The caps +only forbid large counts, so the least compatible counts witness feasibility. -/ +theorem exists_bounded_of_noPin_iff {KB KE KEB KBE : ℕ∞} {oB oE : ℤ} : + (∃ cB cE n : ℕ, + (cB : ℕ∞) ≤ KB ∧ (cE : ℕ∞) ≤ KE ∧ + ((cB - cE : ℕ) : ℕ∞) ≤ KEB ∧ ((cE - cB : ℕ) : ℕ∞) ≤ KBE ∧ + cB ≤ n ∧ cE ≤ n ∧ + 0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧ + 0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ)) ↔ + ((noPinB KBE oB oE : ℕ∞) ≤ KB ∧ (cELo oB oE : ℕ∞) ≤ KE ∧ + ((noPinB KBE oB oE - cELo oB oE : ℕ) : ℕ∞) ≤ KEB ∧ + ((cELo oB oE - noPinB KBE oB oE : ℕ) : ℕ∞) ≤ KBE ∧ + 0 < (cELo oB oE : ℤ) + oE - 1 ∧ + (noPinB KBE oB oE : ℤ) + oB - 1 < (cELo oB oE : ℤ) + oE - 1) := by + constructor + · intro h + obtain ⟨cB, cE, n, hKB, hKE, hKEB, hKBE, h1, h2, h3, h4, h5, h6⟩ := h + -- Re-run the construction, extracting the body for the canonical candidate. + have hce_lo : cELo oB oE ≤ cE := cELo_le h3 h4 + have hspec := cELo_spec (oB := oB) (oE := oE) + have hd2 : cE - cB ≤ KBE.toNat ∨ KBE = ⊤ := (tsub_natCast_le_iff_toNat cE cB KBE).mp hKBE + have hcb_cap : cB ≤ KB.toNat ∨ KB = ⊤ := (natCast_le_iff_toNat cB KB).mp hKB + have hce_cap : cE ≤ KE.toNat ∨ KE = ⊤ := (natCast_le_iff_toNat cE KE).mp hKE + have hb0_cB : noPinB KBE oB oE ≤ cB := by + unfold noPinB + by_cases hBE : KBE = ⊤ + · simp only [hBE, ↓reduceIte]; omega + · simp only [hBE, ↓reduceIte] + rcases hd2 with hd | hd + · omega + · exact absurd hd hBE + have hb0_e0 : noPinB KBE oB oE ≤ cELo oB oE := by + unfold noPinB + by_cases hBE : KBE = ⊤ <;> simp only [hBE, ↓reduceIte] <;> omega + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [natCast_le_iff_toNat] + by_cases hB : KB = ⊤ + · exact Or.inr hB + · left; rcases hcb_cap with hc | hc + · omega + · exact absurd hc hB + · rw [natCast_le_iff_toNat] + rcases hce_cap with hc | hc + · exact Or.inl (le_trans hce_lo hc) + · exact Or.inr hc + · rw [tsub_natCast_le_iff_toNat] + left + have : noPinB KBE oB oE - cELo oB oE = 0 := Nat.sub_eq_zero_of_le hb0_e0 + omega + · rw [tsub_natCast_le_iff_toNat] + by_cases hBE : KBE = ⊤ + · exact Or.inr hBE + · left + unfold noPinB + simp only [hBE, ↓reduceIte] + omega + · exact hspec.1 + · -- anchor ordering for the canonical candidate + have he0_ge := hspec.1 + have hord : (noPinB KBE oB oE : ℤ) + oB - 1 < (cELo oB oE : ℤ) + oE - 1 := by + have he0gt : oB - oE < (cELo oB oE : ℤ) := cELo_gt oB oE + unfold noPinB + by_cases hBE : KBE = ⊤ + · simp only [hBE, ↓reduceIte]; omega + · simp only [hBE, ↓reduceIte] + have hlt : oB - oE < (KBE.toNat : ℤ) := by + rcases hd2 with hd | hd + · omega + · exact absurd hd hBE + by_cases hle : cELo oB oE ≤ KBE.toNat + · rw [max_eq_left (by omega)]; omega + · rw [max_eq_right (by omega)]; omega + exact hord + · rintro ⟨hKB, hKE, hKEB, hKBE, hge, hord⟩ + refine ⟨noPinB KBE oB oE, cELo oB oE, noPinN oB oE, hKB, hKE, hKEB, hKBE, + ?_, ?_, hge, hord, ?_, ?_⟩ + · -- noPinB ≤ noPinN + unfold noPinB noPinN + by_cases hBE : KBE = ⊤ <;> simp only [hBE, ↓reduceIte] + · omega + · have := cELo_spec (oB := oB) (oE := oE); omega + · -- cELo ≤ noPinN + exact cELo_spec.2 + · -- 0 < noPinN + unfold noPinN; omega + · -- begin anchor below noPinN + have hA : (cELo oB oE : ℤ) + oE - 1 < (noPinN oB oE : ℕ) := by + unfold noPinN; exact cELo_anchor_le + have hbeg : (noPinB KBE oB oE : ℤ) + oB - 1 < (noPinN oB oE : ℕ) := + lt_trans hord hA + exact hbeg + +/-! +## The closed form by endpoint case + +`countFeasibleInt` unfolds by whether each endpoint is `⊤` or a genuine key. The +genuine/genuine case is `exists_bounded_of_noPin_iff`; the three cases with a `⊤` +endpoint each collapse to a simple offset condition, because `intervalCard ⊤ _ = 0` +pins the counts together and `intervalCard _ ⊤ = ⊤` drops the difference bounds. +-/ + +/-- The least end count when the begin endpoint is `⊤`: only the end-anchor +positivity `2 - oE ≤ cE` constrains it, since the begin count is then free. -/ +def cEminP (_oB oE : ℤ) : ℕ := ((2 : ℤ) - oE).toNat + +/-- `cEminP` satisfies the end-anchor positivity. -/ +theorem cEminP_ge (oB oE : ℤ) : (2 : ℤ) - oE ≤ (cEminP oB oE : ℤ) := by + unfold cEminP + rcases lt_or_ge oE 2 with h | h + · rw [Int.toNat_of_nonneg (by omega)] + · have : ((2 : ℤ) - oE).toNat = 0 := Int.toNat_of_nonpos (by omega) + rw [this]; omega + +/-- `β = ⊤`, `ε = ↑e`: only `cB = n` is pinned, so the system is feasible exactly +when `oB ≤ 0` (the begin anchor cannot exceed the length), `oB < oE` (the end anchor +gets ahead), and the least end count `cEminP` fits below the end cap. -/ +theorem top_coe_iff {e : Key} {oB oE : ℤ} : + countFeasibleInt (⊤ : WithTop Key) (↑e) oB oE ↔ + oB ≤ 0 ∧ oB < oE ∧ + (cEminP oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) (↑e) := by + unfold countFeasibleInt + constructor + · rintro ⟨cB, cE, n, hpin, -, h3, h4, -, h6, -, h8, hge, hord, h11, h12⟩ + have hcB : cB = n := hpin rfl + rw [hcB] at h8 h12 + have hKBE : ((cE - n : ℕ) : ℕ∞) ≤ (0 : ℕ∞) := by + simpa only [intervalCard_top_left] using h8 + have hcEn : cE ≤ n := by + by_contra hc + have hpos : 0 < cE - n := Nat.sub_pos_of_lt (lt_of_not_ge hc) + have : (0 : ℕ∞) < ((cE - n : ℕ) : ℕ∞) := by exact_mod_cast hpos + omega + have hcE : cEminP oB oE ≤ cE := by + unfold cEminP; rw [Int.toNat_le]; omega + exact ⟨by omega, by omega, le_trans (by exact_mod_cast hcE) h6⟩ + · rintro ⟨hoB, hoBoE, hKE⟩ + refine ⟨max (cEminP oB oE) 1, cEminP oB oE, max (cEminP oB oE) 1, ?_⟩ + have hge' : (0 : ℤ) < (cEminP oB oE : ℤ) + oE - 1 := by + have := cEminP_ge oB oE; omega + have hord' : ((max (cEminP oB oE) 1 : ℕ) : ℤ) + oB - 1 < + (cEminP oB oE : ℤ) + oE - 1 := by + by_cases hle : cEminP oB oE ≤ 1 + · have hmax : max (cEminP oB oE) 1 = 1 := max_eq_right hle + rw [hmax] + have hz : cEminP oB oE = 0 ∨ cEminP oB oE = 1 := by omega + rcases hz with h0 | h1 + · have : 2 ≤ oE := by + unfold cEminP at h0 + have := (Int.toNat_eq_zero.mp h0); omega + omega + · omega + · have hmax : max (cEminP oB oE) 1 = cEminP oB oE := max_eq_left (by omega) + rw [hmax]; omega + exact ⟨fun _ => rfl, fun h => absurd h (by simp), le_refl _, le_max_left _ _, + le_top, hKE, le_top, (by rw [Nat.sub_eq_zero_of_le (le_max_left _ _)]; exact bot_le), + hge', hord', lt_of_lt_of_le (by norm_num) (le_max_right _ _), by omega⟩ + +/-- `ε = ⊤`: always feasible, since the end count can be taken as large as the +database length and the begin count as `0`. -/ +theorem coe_top_iff {b : Key} {oB oE : ℤ} : + countFeasibleInt (↑b) (⊤ : WithTop Key) oB oE ↔ True := by + unfold countFeasibleInt + refine iff_true_intro ?_ + let N : ℕ := oB.natAbs + oE.natAbs + 3 + have hNpos : 0 < N := by unfold N; omega + have hNoB : oB ≤ (N : ℤ) := by unfold N; omega + have hNoE : (0 : ℤ) < (N : ℤ) + oE - 1 := by unfold N; omega + have hNord : (0 : ℤ) + oB - 1 < (N : ℤ) + oE - 1 := by unfold N; omega + exact ⟨0, N, N, fun h => absurd h (by simp), fun _ => rfl, Nat.zero_le _, le_refl _, + by simp, le_top, by simp, le_top, hNoE, hNord, hNpos, by omega⟩ + +/-- Both endpoints `⊤`: the counts are pinned equal to the length, so the system is +feasible exactly when the begin anchor can be pushed to `≤ 0` and below the end +anchor, i.e. `oB ≤ 0` and `oB < oE`. -/ +theorem top_top_iff {oB oE : ℤ} : + countFeasibleInt (⊤ : WithTop Key) (⊤ : WithTop Key) oB oE ↔ + oB ≤ 0 ∧ oB < oE := by + unfold countFeasibleInt + constructor + · rintro ⟨cB, cE, n, hpinB, hpinE, -, -, -, -, h7, h8, hge, hord, h11, h12⟩ + have hB : cB = n := hpinB rfl + have hE : cE = n := hpinE rfl + subst hB; subst hE + exact ⟨by omega, by omega⟩ + · rintro ⟨hoB, hoBoE⟩ + let N : ℕ := oE.natAbs + 2 + refine ⟨N, N, N, fun _ => rfl, fun _ => rfl, le_refl _, le_refl _, + le_top, le_top, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [Nat.sub_self]; exact bot_le + · rw [Nat.sub_self]; exact bot_le + · unfold N; omega + · unfold N; omega + · unfold N; omega + · unfold N; omega + +/-- The genuine/genuine body in the form `exists_bounded_of_noPin_iff` expects. -/ +theorem coe_coe_iff {b e : Key} {oB oE : ℤ} : + countFeasibleInt (↑b) (↑e) oB oE ↔ + (∃ cB cE n : ℕ, + (cB : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) (↑b) ∧ + (cE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) (↑e) ∧ + ((cB - cE : ℕ) : ℕ∞) ≤ intervalCard (↑e) (↑b) ∧ + ((cE - cB : ℕ) : ℕ∞) ≤ intervalCard (↑b) (↑e) ∧ + cB ≤ n ∧ cE ≤ n ∧ + 0 < (cE : ℤ) + oE - 1 ∧ (cB : ℤ) + oB - 1 < (cE : ℤ) + oE - 1 ∧ + 0 < n ∧ (cB : ℤ) + oB - 1 < (n : ℤ)) := by + unfold countFeasibleInt + refine exists_congr fun cB => exists_congr fun cE => exists_congr fun n => ?_ + constructor + · rintro ⟨-, -, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10⟩ + exact ⟨h3, h4, h5, h6, h1, h2, h7, h8, h9, h10⟩ + · rintro ⟨h3, h4, h5, h6, h1, h2, h7, h8, h9, h10⟩ + exact ⟨by simp, by simp, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10⟩ + +/-! +## Deciding feasibility + +Collecting the four endpoint cases into a single quantifier-free predicate +`closedForm` makes `countFeasibleInt` decidable: every case is a Boolean +combination of `intervalCard` comparisons and integer arithmetic on the read's +own data, with no search over counts. +-/ + +/-- The quantifier-free decision predicate: `countFeasibleInt` unfolded by the +endpoint case, with the genuine/genuine case evaluated at its canonical witness. +It is an `abbrev` so the decidable instance for its `if` guards unfolds. -/ +abbrev closedForm (β ε : WithTop Key) (oB oE : ℤ) : Prop := + if β = ⊤ then + if ε = ⊤ then oB ≤ 0 ∧ oB < oE + else oB ≤ 0 ∧ oB < oE ∧ + (cEminP oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε + else if ε = ⊤ then True + else + (noPinB (intervalCard β ε) oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) β ∧ + (cELo oB oE : ℕ∞) ≤ intervalCard (([] : Key) : WithTop Key) ε ∧ + ((noPinB (intervalCard β ε) oB oE - cELo oB oE : ℕ) : ℕ∞) ≤ intervalCard ε β ∧ + ((cELo oB oE - noPinB (intervalCard β ε) oB oE : ℕ) : ℕ∞) ≤ intervalCard β ε ∧ + 0 < (cELo oB oE : ℤ) + oE - 1 ∧ + (noPinB (intervalCard β ε) oB oE : ℤ) + oB - 1 < (cELo oB oE : ℤ) + oE - 1 + +/-- `closedForm` decides `countFeasibleInt`. The genuine/genuine case routes +through `coe_coe_iff` and `exists_bounded_of_noPin_iff`, which replace the +existential over counts by the canonical candidate. -/ +theorem countFeasibleInt_iff_closedForm (β ε : WithTop Key) (oB oE : ℤ) : + countFeasibleInt β ε oB oE ↔ closedForm β ε oB oE := by + unfold closedForm + by_cases hβ : β = ⊤ + · subst hβ + by_cases hε : ε = ⊤ + · subst hε; exact top_top_iff + · rw [ite_eq_left rfl, ite_eq_right hε] + cases ε with + | top => exact absurd rfl hε + | coe e => exact top_coe_iff + · cases β with + | top => exact absurd rfl hβ + | coe b => + by_cases hε : ε = ⊤ + · subst hε; rw [ite_eq_right hβ, ite_eq_left rfl] + exact coe_top_iff + · rw [ite_eq_right hβ, ite_eq_right hε] + cases ε with + | top => exact absurd rfl hε + | coe e => + rw [coe_coe_iff] + exact exists_bounded_of_noPin_iff + +/-- `countFeasibleInt` is decidable: `closedForm` is a quantifier-free Boolean +combination of interval cardinalities and integer comparisons. -/ +instance instDecidableCountFeasibleInt (β ε : WithTop Key) (oB oE : ℤ) : + Decidable (countFeasibleInt β ε oB oE) := + decidable_of_iff (closedForm β ε oB oE) (countFeasibleInt_iff_closedForm β ε oB oE).symm + +/-- `countFeasible` is decidable. -/ +instance instDecidableCountFeasible (β ε : WithTop Key) (oB oE : ℤ) : + Decidable (countFeasible β ε oB oE) := + decidable_of_iff (countFeasibleInt β ε oB oE) (countFeasible_iff_int β ε oB oE).symm + +/-- **A read is decidable as trivially empty.** The characterization +`alwaysEmpty_iff_not_countFeasible` and the decidability of `countFeasible` yield a +decision procedure for `AlwaysEmpty r` computable from the read alone. -/ +instance instDecidableAlwaysEmpty (r : RangeRead) : Decidable (AlwaysEmpty r) := + decidable_of_iff (¬ countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset) + (alwaysEmpty_iff_not_countFeasible r).symm + +/-! +## The empty range for an always-empty read + +`AlwaysEmpty r` is the exact, database-free characterization of reads that can +return no key, so it certifies the empty range for every such read: the empty +interval is sound and minimal whenever the read is trivially empty. +-/ + +/-- Under trivial emptiness, compatibility is exactly the empty complete result: +every database is compatible with `⟨[], false⟩`, and nothing else. -/ +theorem compatible_iff_nil_of_alwaysEmpty {db : DB} {r : RangeRead} {res : Result} + (h : AlwaysEmpty r) : + RangeRead.Compatible db r res ↔ (res.more = false ∧ res.keys = []) := by + constructor + · intro hc + rcases hc.2 with ⟨hm, hpre, hne⟩ | ⟨hm, heq⟩ + · rw [h db] at hpre + exact absurd (List.eq_nil_of_prefix_nil hpre) hne + · exact ⟨hm, by rw [heq, h db]⟩ + · rintro ⟨hm, hk⟩ + refine ⟨?_, Or.inr ⟨hm, ?_⟩⟩ + · rw [hk] + unfold RangeRead.withinLimit + split <;> simp + · rw [hk, h db] + +/-- The empty range is sound for an always-empty read: every database's +compatibility is the same empty-result condition, independent of the database. -/ +theorem sound_emptyRange_of_alwaysEmpty {r : RangeRead} {res : Result} + (h : AlwaysEmpty r) : Sound r res (emptyRange r) := by + intro db₁ db₂ _ + rw [compatible_iff_nil_of_alwaysEmpty (db := db₁) h, + compatible_iff_nil_of_alwaysEmpty (db := db₂) h] + +/-- The empty range is minimal for an always-empty read: it is sound, and no proper +subrange exists because a range with no keys admits no separating key. -/ +theorem minimal_emptyRange_of_alwaysEmpty {r : RangeRead} {res : Result} + (h : AlwaysEmpty r) : Minimal r res (emptyRange r) := by + refine ⟨sound_emptyRange_of_alwaysEmpty h, fun cr' hproper _ => ?_⟩ + exact hproper.2 fun k hk => (not_mem_emptyRange r k hk).elim + +/-- With trivial emptiness, the empty range is *the* minimal sound conflict range +up to set equality: it is minimal by `minimal_emptyRange_of_alwaysEmpty`, and every +minimal range denotes the same set by the unconditional `Minimal.toSet_eq`. -/ +theorem unique_minimal_emptyRange_of_alwaysEmpty {r : RangeRead} {res : Result} + (h : AlwaysEmpty r) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal (minimal_emptyRange_of_alwaysEmpty h) + +/-! +## The emptiness predicate `ψ` in full generality + +The count criterion `alwaysEmpty_iff_not_countFeasible` decides emptiness, but the +concrete predicates used to build conflict ranges — `Crossed`'s +`completeKeys_eq_nil_iff_counts` at `begin.offset = 1`, and its `above`/`between` +form — fix the begin offset. This section derives the predicate `ψ` for *all* +offsets, purely from the clamped-anchor characterization, so no offset hypothesis is +needed. + +Emptiness is exactly `clampIndex db r.begin ≥ clampIndex db r.end_`. Writing +`a = (c_B + oB - 1).toNat` and `e = (c_E + oE - 1).toNat` for the two signed +anchors, the clamped comparison is `¬ (min a n < min e n)`, and +`min_min_lt_iff` (`min a n < min e n ↔ a < e ∧ a < n`) turns its negation into the +two-disjunct predicate + + `ψ(db) = (n ≤ a) ∨ (e ≤ a)`. + +The first disjunct says the begin anchor reaches the database length; the second +says it already dominates the end anchor. At a positive offset, `toNat` splits as +`(c + o - 1).toNat = c + (o - 1).toNat`, giving the count form +`(n ≤ c_B + (oB-1).toNat) ∨ (c_E + (oE-1).toNat ≤ c_B + (oB-1).toNat)`, which at +`oB = 1` collapses to `(c_B = n) ∨ (c_E + (oE-1) ≤ c_B)` — the predicate of +`completeKeys_eq_nil_iff_counts`. So `ψ` unifies the offset-one, cross-free, +and crossed empty cases directly in terms of the read's own data. + +Rewriting `ψ` through `above` and `between` is in +`completeKeys_eq_nil_iff_above_between`; the crossed soundness and candidate it +yields for all positive offsets are `sound_top_end_of_crossed_general`, +`sound_top_begin_of_short_general`, and `crossedRangeGeneral`, below. + +With `b = (oB-1).toNat` and `g = (oE-1).toNat - (oB-1).toNat`, the crossed `ψ` +reads `(#stored ≥ B ≤ b) ∨ (#stored in [E,B) ≥ g)`. Both disjuncts are functions +of the keys at or above `E`, so `[E,⊤)` is sound; `[B,⊤)` is sound only in the +short gap `intervalCard E B < g`, where the second disjunct is unsatisfiable. The +long gap's necessity needs the slack `b`, not the offset-one empty database: +`necessary_of_begin_le_general` and `necessary_of_mem_interval_general` build the +witnesses, and `minimal_crossedRangeGeneral_of_empty` assembles them. + +The cross-free sibling `completeKeys_eq_nil_iff_above_between_le` rewrites the +*same* `ψ` through `[B,E)` instead of `[E,B)`: for `B ≤ E` the count difference +`c_E - c_B` is the number of stored keys in `[B,E)`, so the predicate becomes +`(above B).length ≤ b ∨ #(between B E) + e ≤ b`, both disjuncts functions of the +keys at or above `B`. Soundness of `[B,⊤)` is therefore direct +(`sound_topRange_of_le_general`), and necessity (`necessary_of_begin_le_of_le_general`) +uses the slack `b` on keys at or above `E` while spending `[B,E)` keys to flip the +second disjunct. The `¬ AlwaysEmpty` hypothesis is essential — unlike the offset-one +case, a positive `B` can be genuine while `ψ` is vacuous (`e ≤ b`), and then the +minimal range is `∅`. `minimal_topRange_of_le_empty` assembles the general +certificate; `minimal_topRange_of_end_offset_ge_two_empty` is its `b = 0` case. + +There is no order-reversing symmetry to transport the end-offset theory to the +begin side directly. `Key` is a finite-byte-string type with a least element and no +greatest, so an order reversal would have to send `[]` to a nonexistent greatest +key; byte complement reverses order only on fixed-length keys and preserves the +prefix order, so it fails to reverse `anchor`/index order across unequal lengths. +The extension is therefore direct, through the clamped comparison. +-/ + +/-- The clamped anchor index with the offset split off: at a positive offset the +signed anchor is the count below the selector key plus `offset - 1`, and `toNat` +distributes over the sum. -/ +theorem clampIndex_eq_min_add (db : DB) (β : WithTop Key) (o : ℤ) (h : 1 ≤ o) + (c : ℕ) (hc : cnt db β = c) : + clampIndex db (⟨β, o⟩ : Selector) = min (c + (o - 1).toNat) db.keys.length := by + rw [clampIndex_eq db β o c hc] + congr 1 + rw [show ((c : ℤ) + o - 1) = (c : ℤ) + (o - 1) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + +/-- A count is at most the database length, so `n ≤ cnt db β` forces equality. -/ +theorem cnt_eq_length_of_length_le (db : DB) (β : WithTop Key) + (h : db.keys.length ≤ cnt db β) : cnt db β = db.keys.length := + le_antisymm (cnt_le_length db β) h + +/-- **The emptiness predicate `ψ`, all offsets, raw form.** A complete result is +empty exactly when the begin anchor reaches the database length or dominates the end +anchor, in the raw `toNat` form. No hypothesis on either offset is needed: this is +just the negation of the clamped anchor comparison `min_min_lt_iff`. -/ +theorem completeKeys_eq_nil_iff_psi_raw {db : DB} {r : RangeRead} : + RangeRead.completeKeys db r = [] ↔ + db.keys.length ≤ (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) ∨ + (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) ≤ + (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) := by + have hkey : (RangeRead.completeKeys db r = []) ↔ + ¬ (clampIndex db r.begin < clampIndex db r.end_) := by + rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff] + rw [hkey, + clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl, + clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl, + min_min_lt_iff] + omega + +/-- **The emptiness predicate `ψ`, positive offsets.** With both offsets at least +one, `ψ` takes the count form `(n ≤ c_B + (oB-1)) ∨ (c_E + (oE-1) ≤ c_B + (oB-1))`, +where `c_B, c_E` are the stored-key counts below the two selector keys. This +generalizes `completeKeys_eq_nil_iff_counts` (the `oB = 1` case, where +`oB - 1 = 0`) and hence both the cross-free and crossed empty cases. -/ +theorem completeKeys_eq_nil_iff_psi {db : DB} {r : RangeRead} + (hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = [] ↔ + db.keys.length ≤ cnt db r.begin.key + (r.begin.offset - 1).toNat ∨ + cnt db r.end_.key + (r.end_.offset - 1).toNat ≤ + cnt db r.begin.key + (r.begin.offset - 1).toNat := by + have hkey : (RangeRead.completeKeys db r = []) ↔ + ¬ (clampIndex db r.begin < clampIndex db r.end_) := by + rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff] + rw [hkey, + clampIndex_eq_min_add db r.begin.key r.begin.offset hB (cnt db r.begin.key) rfl, + clampIndex_eq_min_add db r.end_.key r.end_.offset hE (cnt db r.end_.key) rfl, + min_min_lt_iff] + omega + +/-- At `begin.offset = 1` the general predicate recovers the existing count form +`completeKeys_eq_nil_iff_counts`: `(c_B = n) ∨ (c_E + (oE-1) ≤ c_B)`. -/ +theorem completeKeys_eq_nil_iff_psi_one_begin {db : DB} {r : RangeRead} + (hB : r.begin.offset = 1) (hE : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = [] ↔ + cnt db r.begin.key = db.keys.length ∨ + cnt db r.end_.key + (r.end_.offset - 1).toNat ≤ cnt db r.begin.key := by + rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) (by omega) hE, hB] + norm_num + constructor + · rintro (h | h) + · exact Or.inl (le_antisymm (cnt_le_length db r.begin.key) h) + · exact Or.inr h + · rintro (h | h) + · exact Or.inl (le_of_eq h.symm) + · exact Or.inr h + +/-- **`ψ` at offset one on both ends.** For `begin.offset = end.offset = 1`, a +complete result is empty exactly when no stored key reaches `begin.key` (`c_B = n`) +or the end count does not exceed the begin count (`c_E ≤ c_B`). -/ +theorem completeKeys_eq_nil_iff_psi_offset_one {db : DB} {r : RangeRead} + (hB : r.begin.offset = 1) (hE : r.end_.offset = 1) : + RangeRead.completeKeys db r = [] ↔ + cnt db r.begin.key = db.keys.length ∨ cnt db r.end_.key ≤ cnt db r.begin.key := by + rw [completeKeys_eq_nil_iff_psi_one_begin (db := db) (r := r) hB (by omega), hE] + norm_num + +/-- The stored keys at or above `E`. -/ +def above (db : DB) (E : WithTop Key) : List Key := + db.keys.filter (fun (k : Key) => decide (E ≤ (k : WithTop Key))) + +/-- A key is stored at or above `E` exactly when it is stored and at or above `E`. -/ +theorem mem_above {db : DB} {E : WithTop Key} {k : Key} : + k ∈ above db E ↔ k ∈ db.keys ∧ E ≤ (k : WithTop Key) := by + unfold above + rw [List.mem_filter, decide_eq_true_eq] + +/-- The stored keys of the top range `[E, ⊤)` are the stored keys at or above `E`. -/ +theorem keys_top_eq_above (db : DB) (E : WithTop Key) : + (ConflictRange.mk E ⊤).keys db = above db E := by + unfold above ConflictRange.keys + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact and_iff_left (WithTop.coe_lt_top k) + +/-- The top range's stored keys are a function of the keys at or above `E`. -/ +theorem above_eq_filter (db : DB) {E : WithTop Key} : + above db E = db.keys.filter (fun (k : Key) => decide (E ≤ (k : WithTop Key))) := rfl + +/-- For `E ≤ B`, the keys at or above `B` are the `B`-filter of the keys at or +above `E`. -/ +theorem above_B_eq_filter {db : DB} {E B : WithTop Key} (hEB : E ≤ B) : + above db B = (above db E).filter (fun (k : Key) => decide (B ≤ (k : WithTop Key))) := by + rw [above_eq_filter, above_eq_filter, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases h : B ≤ (k : WithTop Key) + · simp [h, le_trans hEB h] + · simp [h] + +/-- The keys in `[E, B)` are the `< B`-filter of the keys at or above `E`. -/ +theorem between_eq_filter_above (db : DB) (E B : WithTop Key) : + between db E B = (above db E).filter (fun (k : Key) => decide ((k : WithTop Key) < B)) := by + rw [above_eq_filter] + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases h : (k : WithTop Key) < B <;> by_cases h2 : E ≤ (k : WithTop Key) <;> simp [h, h2] + +/-- No stored key reaches `B` exactly when all of them lie strictly below it. -/ +theorem cnt_eq_length_iff_above_nil (db : DB) (B : WithTop Key) : + cnt db B = db.keys.length ↔ above db B = [] := by + rw [cnt, List.countP_eq_length, above, List.filter_eq_nil_iff] + constructor + · intro h a ha + simp only [decide_eq_true_eq] + exact not_le.mpr (of_decide_eq_true (h a ha)) + · intro h a ha + have hh := h a ha + simp only [decide_eq_true_eq] at hh ⊢ + exact lt_of_not_ge hh + +/-- For `E ≤ B`, the count difference `c_B - c_E` is the number of stored keys in +`[E, B)`. -/ +theorem cnt_sub_eq_between_length {db : DB} {E B : WithTop Key} (hEB : E ≤ B) : + cnt db B - cnt db E = (between db E B).length := by + have := cnt_eq_add_between db hEB + omega + +/-- For `E ≤ B`, the second disjunct `c_E + k ≤ c_B` is exactly "`[E, B)` stores at +least `k` keys". -/ +theorem cnt_add_le_iff_between {db : DB} {E B : WithTop Key} (hEB : E ≤ B) (k : ℕ) : + cnt db E + k ≤ cnt db B ↔ k ≤ (between db E B).length := by + rw [cnt_eq_add_between db hEB, Nat.add_le_add_iff_left] + +/-- The number of stored keys in `[E, B)` is bounded by the interval cardinality. -/ +theorem between_length_le_intervalCard {db : DB} {E B : WithTop Key} (hEB : E ≤ B) : + ((between db E B).length : ℕ∞) ≤ intervalCard E B := by + have h := cnt_sub_le_intervalCard db hEB + rwa [cnt_sub_eq_between_length hEB] at h + +/-- In the short-gap branch the second disjunct is never satisfied: fewer than `k` +keys can lie in `[E, B)`. -/ +theorem between_length_lt_of_intervalCard_lt {db : DB} {E B : WithTop Key} (hEB : E ≤ B) + {k : ℕ} (hgap : intervalCard E B < (k : ℕ∞)) : + (between db E B).length < k := by + have h := between_length_le_intervalCard (db := db) hEB + have hlt : ((between db E B).length : ℕ∞) < (k : ℕ∞) := lt_of_le_of_lt h hgap + exact_mod_cast hlt + +/-- The count below `B` plus the number of stored keys at or above `B` is the +database length: the two filters partition the stored keys. -/ +private theorem countP_lt_add_filter_ge_length (l : List Key) (B : WithTop Key) : + l.countP (fun k => decide (((k : Key) : WithTop Key) < B)) + + (l.filter (fun k => decide (B ≤ ((k : Key) : WithTop Key)))).length = + l.length := by + have hperm := List.filter_append_perm + (fun k : Key => decide (((k : Key) : WithTop Key) < B)) l + have hcnt : l.countP (fun k => decide (((k : Key) : WithTop Key) < B)) = + (l.filter (fun k => decide (((k : Key) : WithTop Key) < B))).length := + List.countP_eq_length_filter + have hge : l.filter (fun k => decide (B ≤ ((k : Key) : WithTop Key))) = + l.filter (fun k => !decide (((k : Key) : WithTop Key) < B)) := by + apply List.filter_congr + intro k _ + rw [show (!decide (((k : Key) : WithTop Key) < B)) = + decide (¬ (((k : Key) : WithTop Key) < B)) by rw [decide_not], + decide_eq_decide] + exact not_lt.symm + rw [hcnt, hge, ← List.length_append] + exact hperm.length_eq + +/-- The count below `B` plus the number of stored keys at or above `B` is the +database length: the two filters partition the stored keys. -/ +theorem cnt_add_above_length (db : DB) (B : WithTop Key) : + cnt db B + (above db B).length = db.keys.length := by + rw [above, cnt] + exact countP_lt_add_filter_ge_length db.keys B + +/-- **The `above`-form of the first `ψ` disjunct.** The begin anchor reaching the +database length, `n ≤ c_B + b`, is exactly "at most `b` stored keys lie at or above +`begin.key`". -/ +theorem length_le_cnt_add_iff_above_le (db : DB) (B : WithTop Key) (b : ℕ) : + db.keys.length ≤ cnt db B + b ↔ (above db B).length ≤ b := by + have h := cnt_add_above_length db B + omega + +/-- **The `between`-form of the second `ψ` disjunct, crossed case.** For +`E ≤ B`, the end anchor dominating the begin anchor, `c_E + e ≤ c_B + b`, is +exactly `e - b ≤ #(stored in [E, B))`, the count difference being the number of +stored keys in `[E, B)`. The natural-number subtraction handles `e < b` with no +positivity hypothesis on `e - b`. -/ +theorem cnt_add_le_iff_between_tsub {db : DB} {E B : WithTop Key} (hEB : E ≤ B) + (b e : ℕ) : + cnt db E + e ≤ cnt db B + b ↔ e - b ≤ (between db E B).length := by + have h := cnt_eq_add_between db hEB + omega + +/-- **The `between`-form of the second `ψ` disjunct, cross-free case.** For +`B ≤ E`, the end anchor dominating the begin anchor, `c_E + e ≤ c_B + b`, is +exactly `#(stored in [B, E)) + e ≤ b`, the count `c_E - c_B` being the number of +stored keys in `[B, E)`. -/ +theorem cnt_add_le_iff_between_le {db : DB} {B E : WithTop Key} (hBE : B ≤ E) + (b e : ℕ) : + cnt db E + e ≤ cnt db B + b ↔ (between db B E).length + e ≤ b := by + have h := cnt_eq_add_between db hBE + omega + +/-- **The general crossed emptiness predicate.** With both offsets at least one and +the selectors crossed (`end_.key ≤ begin_.key`), a complete result is empty exactly +when at most `(oB - 1).toNat` stored keys lie at or above `begin.key`, or the gap +`(oE - 1).toNat - (oB - 1).toNat` is at most the number of stored keys in +`[end_.key, begin_.key)`. This is the all-positive-offset extension of +`completeKeys_eq_nil_iff_above` (the `oB = 1`, so `(oB-1).toNat = 0` case) +and unifies the cross-free and crossed empty predicates. -/ +theorem completeKeys_eq_nil_iff_above_between {db : DB} {r : RangeRead} + (hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) : + RangeRead.completeKeys db r = [] ↔ + (above db r.begin.key).length ≤ (r.begin.offset - 1).toNat ∨ + (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat ≤ + (between db r.end_.key r.begin.key).length := by + rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hB hE, + length_le_cnt_add_iff_above_le, cnt_add_le_iff_between_tsub hEB] + +/-- The keys in `[B, E)` are the `< E`-filter of the keys at or above `B`. -/ +theorem between_le_eq_filter_above (db : DB) (B E : WithTop Key) : + between db B E = (above db B).filter (fun (k : Key) => decide ((k : WithTop Key) < E)) := by + rw [above_eq_filter] + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hB : B ≤ (k : WithTop Key) <;> by_cases hE : (k : WithTop Key) < E <;> simp [hB, hE] + +/-- **The cross-free `ψ` depends only on the keys at or above `B`.** With both +offsets positive and the selectors cross-free (`begin.key ≤ end_.key`), the second +disjunct of `ψ` is a function of `above db begin.key`, since `between begin.key +end_.key` is the `< end_.key`-filter of that list. -/ +theorem completeKeys_eq_nil_iff_le_of_agree {db₁ db₂ : DB} {r : RangeRead} + (hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) + (habove : above db₁ r.begin.key = above db₂ r.begin.key) : + (RangeRead.completeKeys db₁ r = [] ↔ RangeRead.completeKeys db₂ r = []) := by + have hbetween : between db₁ r.begin.key r.end_.key = + between db₂ r.begin.key r.end_.key := by + rw [between_le_eq_filter_above, between_le_eq_filter_above, habove] + rw [completeKeys_eq_nil_iff_psi (db := db₁) (r := r) hB hE, + completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hB hE, + length_le_cnt_add_iff_above_le, length_le_cnt_add_iff_above_le, habove, + cnt_add_le_iff_between_le hle, cnt_add_le_iff_between_le hle, hbetween] + +/-- **The cross-free emptiness predicate, general positive offsets.** With +`begin.key ≤ end_.key`, a complete result is empty exactly when at most +`(begin.offset - 1).toNat` stored keys lie at or above `begin.key`, or the +`[begin.key, end_.key)` count plus `(end_.offset - 1).toNat` is at most +`(begin.offset - 1).toNat`. This is the cross-free sibling +of `completeKeys_eq_nil_iff_above_between`, with `[begin.key, end_.key)` in place of +`[end_.key, begin.key)`. -/ +theorem completeKeys_eq_nil_iff_above_between_le {db : DB} {r : RangeRead} + (hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) : + RangeRead.completeKeys db r = [] ↔ + (above db r.begin.key).length ≤ (r.begin.offset - 1).toNat ∨ + (between db r.begin.key r.end_.key).length + (r.end_.offset - 1).toNat ≤ + (r.begin.offset - 1).toNat := by + rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hB hE, + length_le_cnt_add_iff_above_le, cnt_add_le_iff_between_le hle] + +/-- **Soundness of `[B, ⊤)` for the cross-free general predicate.** With the +selectors cross-free (`begin.key ≤ end_.key`), the predicate `ψ` of +`completeKeys_eq_nil_iff_above_between_le` is a function of the stored keys at or +above `begin.key`: `above begin.key` is exactly that set, and `between begin.key +end_.key` is its `< end_.key`-filter. Agreement on `[begin.key, ⊤)` therefore fixes +`ψ`, and with it whether the empty result is compatible, whatever the begin +offset. -/ +theorem sound_topRange_of_le_general {r : RangeRead} {res : Result} + (hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨r.begin.key, ⊤⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have h : above db₁ r.begin.key = above db₂ r.begin.key := by + rw [← keys_top_eq_above, hagree, keys_top_eq_above] + rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil] + exact completeKeys_eq_nil_iff_le_of_agree hB hE hle h + +/-- **Soundness of `[E, ⊤)` for the general crossed predicate.** With the selectors +crossed (`end_.key ≤ begin_.key`), the predicate `ψ` of +`completeKeys_eq_nil_iff_above_between` is a function of the stored keys at or above +`end_.key`: both `above begin.key` and `between end_.key begin.key` are filters of +`above end_.key` (`above_B_eq_filter`, `between_eq_filter_above`). Agreement on +`[end_.key, ⊤)` therefore fixes `ψ`, and with it whether the empty result is +compatible, whatever the begin offset. -/ +theorem sound_top_end_of_crossed_general {r : RangeRead} {res : Result} + (hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨r.end_.key, ⊤⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have h : above db₁ r.end_.key = above db₂ r.end_.key := by + rw [← keys_top_eq_above, hagree, keys_top_eq_above] + have hB' : above db₁ r.begin.key = above db₂ r.begin.key := by + rw [above_B_eq_filter hEB, above_B_eq_filter hEB, h] + have hbet : between db₁ r.end_.key r.begin.key = between db₂ r.end_.key r.begin.key := by + rw [between_eq_filter_above, between_eq_filter_above, h] + rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_above_between (db := db₁) hB hE hEB, + completeKeys_eq_nil_iff_above_between (db := db₂) hB hE hEB, hB', hbet] + +/-- **The general crossed candidate.** `[end_.key, ⊤)` in the long gap +(`(oE-1) - (oB-1) ≤ intervalCard end_.key begin_.key`) and `[begin_.key, ⊤)` in the +short gap. This extends `crossedRange` to all positive begin offsets: at +`begin.offset = 1` the gap `(oE-1) - 0` is `crossedRange`'s `(oE-1).toNat`, so the +two agree there (the bridge `crossedRangeGeneral_eq_crossedRange`, below). -/ +def crossedRangeGeneral (r : RangeRead) : ConflictRange := + if ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat) ≤ + intervalCard r.end_.key r.begin.key + then ⟨r.end_.key, ⊤⟩ else ⟨r.begin.key, ⊤⟩ + +/-- In the short gap the second disjunct of the general crossed `ψ` is never +satisfiable: the gap `(oE-1) - (oB-1)` exceeds the interval cardinality, hence the +number of stored keys in `[E, B)`. -/ +theorem between_length_lt_of_intervalCard_lt_tsub {db : DB} {E B : WithTop Key} (hEB : E ≤ B) + {b e : ℕ} (hgap : intervalCard E B < (e - b : ℕ)) : + (between db E B).length < e - b := by + have h := between_length_le_intervalCard (db := db) hEB + have hlt : ((between db E B).length : ℕ∞) < ((e - b : ℕ) : ℕ∞) := + lt_of_le_of_lt h (by exact_mod_cast hgap) + exact_mod_cast hlt + +/-- **Soundness of `[B, ⊤)` in the short gap, general offsets.** With +`intervalCard E B < (oE-1) - (oB-1)` the second disjunct of `ψ` is unsatisfiable, so +`ψ` reduces to `above B`'s length being at most `(oB-1).toNat`, a function of the +stored keys at or above `B`. This extends `sound_top_begin_of_short` to all +positive begin offsets. -/ +theorem sound_top_begin_of_short_general {r : RangeRead} {res : Result} + (hB : 1 ≤ r.begin.offset) (hE : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat)) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨r.begin.key, ⊤⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have h : above db₁ r.begin.key = above db₂ r.begin.key := by + rw [← keys_top_eq_above, hagree, keys_top_eq_above] + have red : ∀ db : DB, (RangeRead.completeKeys db r = [] ↔ + (above db r.begin.key).length ≤ (r.begin.offset - 1).toNat) := by + intro db + rw [completeKeys_eq_nil_iff_above_between (db := db) hB hE hEB] + have hlt := between_length_lt_of_intervalCard_lt_tsub (db := db) hEB hgap + exact ⟨fun h => h.elim id fun hc => absurd hc (not_le.mpr hlt), Or.inl⟩ + rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil, + red db₁, red db₂, h] + +/-! +## The crossed empty case + +The empty complete result at `begin.offset = 1`, `end_.offset ≥ 2`, with the +selectors *crossed* (`end_.key < begin.key`). The cross-free overshoot case is +`minimal_topRange_of_end_offset_ge_two_empty`; here the top range `[begin.key, ⊤)` +need not be sound, because emptiness can depend on the stored keys in +`[end_.key, begin.key)`. + +Writing `B = begin.key`, `E = end_.key`, `oE = end_.offset`, `n = |db.keys|`, +`cₖ = #{stored < k}`, the empty result is compatible with a database exactly when +`ψ(db) = (c_B = n) ∨ (c_E + (oE - 1) ≤ c_B)`; equivalently `ψ` is the disjunction +"no stored key `≥ B`" or "at least `oE - 1` stored keys in `[E, B)`". Both +disjuncts depend only on the stored keys at or above `E`, so `[E, ⊤)` is sound. +The minimal range is `[E, ⊤)` in the *long gap* (`oE - 1 ≤ intervalCard E B`) and +`[B, ⊤)` in the *short gap* (`intervalCard E B < oE - 1`), where the second +disjunct is unsatisfiable and `ψ` reduces to "no stored key `≥ B`". +-/ + +/-- **The emptiness predicate.** At `begin.offset = 1` and `end_.offset ≥ 1`, an +empty complete result is compatible with `db` exactly when either no stored key +reaches `begin.key` (`c_B = n`) or the count difference is at least +`end_.offset - 1`. This is the formal counterpart of the predicate `ψ` derived in +the notes, and it holds regardless of the relative order of the selector keys. -/ +theorem completeKeys_eq_nil_iff_counts {db : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = [] ↔ + cnt db r.begin.key = db.keys.length ∨ + cnt db r.end_.key + (r.end_.offset - 1).toNat ≤ cnt db r.begin.key := by + have htoNat : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) = + cnt db r.end_.key + (r.end_.offset - 1).toNat := by + have h1 : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + rw [show ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) = + (cnt db r.end_.key : ℤ) + (r.end_.offset - 1) by ring, + Int.toNat_add (Int.natCast_nonneg _) h1, Int.toNat_natCast] + have key : (RangeRead.completeKeys db r = []) ↔ + ¬ (clampIndex db r.begin < clampIndex db r.end_) := by + rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff] + rw [key, not_lt, clampIndex_begin_eq hbegin, clampIndex_end_eq, htoNat, + Nat.min_eq_left (cnt_le_length db r.begin.key)] + constructor + · intro h + rcases min_le_iff.mp h with h | h + · exact Or.inr h + · exact Or.inl (le_antisymm (cnt_le_length db r.begin.key) h) + · rintro (h | h) + · rw [← h]; exact Nat.min_le_right _ _ + · exact le_trans (Nat.min_le_left _ _) h + +/-- Inserting a key strictly below `v` increments the count of stored keys below +`v`. -/ +theorem cnt_insert_lt (db : DB) (x : Key) (hx : x ∉ db.keys) {v : WithTop Key} + (hxv : (x : WithTop Key) < v) : + cnt (db.insert x hx) v = cnt db v + 1 := by + unfold cnt + rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hx] + exact countP_insertIdx_of_lt db.sorted hxv + +/-- Inserting a key not strictly below `v` leaves the count of stored keys below +`v` unchanged. -/ +theorem cnt_insert_not_lt (db : DB) (x : Key) (hx : x ∉ db.keys) {v : WithTop Key} + (hxv : ¬ (x : WithTop Key) < v) : + cnt (db.insert x hx) v = cnt db v := by + unfold cnt + rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hx] + exact countP_insertIdx (l := db.keys) (fun k => decide ((k : WithTop Key) < v)) + (db.keys.countP (fun k => decide (k < x))) x (decide_eq_false_iff_not.mpr hxv) + +/-- For `E ≤ B`, the empty complete result at begin offset one is equivalent to: +no stored key reaches `B`, or the keys in `[E, B)` number at least `end_.offset - 1`. +The first disjunct is the `c_B = n` case, the second the count difference. -/ +theorem completeKeys_eq_nil_iff_above {db : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) : + RangeRead.completeKeys db r = [] ↔ + above db r.begin.key = [] ∨ + (r.end_.offset - 1).toNat ≤ (between db r.end_.key r.begin.key).length := by + rw [completeKeys_eq_nil_iff_counts hbegin hoE, cnt_eq_length_iff_above_nil, + cnt_add_le_iff_between hEB] + +/-- In the short gap the second disjunct is never satisfied, so an empty complete +result is exactly "no stored key reaches `B`". -/ +theorem completeKeys_eq_nil_iff_above_B {db : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < ((r.end_.offset - 1).toNat : ℕ∞)) : + RangeRead.completeKeys db r = [] ↔ above db r.begin.key = [] := by + rw [completeKeys_eq_nil_iff_above hbegin hoE hEB] + have hlt := between_length_lt_of_intervalCard_lt (db := db) hEB hgap + constructor + · rintro (h | h) + · exact h + · exact absurd h (not_le.mpr hlt) + · exact Or.inl + +/-- **Soundness of `[E, ⊤)` in the crossed case.** The empty result's compatibility +is the predicate `ψ`, which reads only the stored keys at or above `E`; agreement on +`[E, ⊤)` fixes exactly those. -/ +theorem sound_top_end_of_crossed {r : RangeRead} {res : Result} + (hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) (hmore : res.more = false) + (hempty : res.keys = []) : + Sound r res ⟨r.end_.key, ⊤⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have h : above db₁ r.end_.key = above db₂ r.end_.key := by + rw [← keys_top_eq_above, hagree, keys_top_eq_above] + have hB : above db₁ r.begin.key = above db₂ r.begin.key := by + rw [above_B_eq_filter hEB, above_B_eq_filter hEB, h] + have hbet : between db₁ r.end_.key r.begin.key = between db₂ r.end_.key r.begin.key := by + rw [between_eq_filter_above, between_eq_filter_above, h] + rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_above hbegin hoE hEB, completeKeys_eq_nil_iff_above hbegin hoE hEB, + hB, hbet] + +/-- **Soundness of `[B, ⊤)` in the short gap.** With `intervalCard E B < oE - 1` the +second disjunct of `ψ` is unsatisfiable, so `ψ` reduces to "no stored key reaches +`B`", which agreement on `[B, ⊤)` fixes. -/ +theorem sound_top_begin_of_short {r : RangeRead} {res : Result} + (hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset) + (hEB : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < ((r.end_.offset - 1).toNat : ℕ∞)) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨r.begin.key, ⊤⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have h : above db₁ r.begin.key = above db₂ r.begin.key := by + rw [← keys_top_eq_above, hagree, keys_top_eq_above] + rw [compatible_empty_iff_complete_nil, compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_above_B hbegin hoE hEB hgap, + completeKeys_eq_nil_iff_above_B hbegin hoE hEB hgap] + rw [h] + +/-- Any finite list of keys misses a key at or above `b`: the `|l| + 1` successor +keys `chain b 0, …, chain b |l|` are distinct and at or above `b`, so one of them +is unstored. -/ +theorem exists_le_not_mem (l : List Key) (b : Key) : ∃ x : Key, b ≤ x ∧ x ∉ l := by + by_contra h + push Not at h + have hsub : (Finset.range (l.length + 1)).image (chain b) ⊆ l.toFinset := by + intro x hx + rw [Finset.mem_image] at hx + obtain ⟨i, _, rfl⟩ := hx + exact List.mem_toFinset.mpr (h (chain b i) (chain_le b i)) + have hcard := Finset.card_le_card hsub + rw [Finset.card_image_of_injective _ (chain_injective b), Finset.card_range] at hcard + have := List.toFinset_card_le l + omega + +/-- A read whose begin selector is keyed at `⊤` with positive offset is trivially +empty: `resolve begin` is the sentinel for every database, so the window is +empty. -/ +theorem alwaysEmpty_of_begin_key_top {r : RangeRead} (hbegin : 1 ≤ r.begin.offset) + (htop : r.begin.key = (⊤ : WithTop Key)) : AlwaysEmpty r := by + intro db + have hres : resolve db r.begin = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top htop hbegin + have hin : RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = [] := by + unfold RangeRead.inRange + rw [hres, List.filter_eq_nil_iff] + intro k _ hk + exact absurd (of_decide_eq_true hk).1 (not_le.mpr (WithTop.coe_lt_top k)) + unfold RangeRead.completeKeys + rw [hin] + cases r.reverse <;> rfl + +/-- A non-trivially-empty read with positive begin offset has a genuine begin key. -/ +theorem begin_key_ne_top_of_not_alwaysEmpty {r : RangeRead} (hbegin : 1 ≤ r.begin.offset) + (h : ¬ AlwaysEmpty r) : r.begin.key ≠ (⊤ : WithTop Key) := + fun htop => h (alwaysEmpty_of_begin_key_top hbegin htop) + +/-- The empty database. -/ +def emptyDB : DB := ⟨[], List.sortedLT_nil⟩ + +/-- The empty complete result. -/ +def emptyRes : Result := { keys := [], more := false } + +/-- The empty database's complete keys are empty, so it is compatible with the +empty result. -/ +theorem compatible_emptyDB_emptyRes (r : RangeRead) : + RangeRead.Compatible emptyDB r emptyRes := + compatible_empty_iff_complete_nil.mpr (by simp [emptyDB, RangeRead.completeKeys, + RangeRead.inRange]) + +/-- **Necessity at or above `B`.** Every key `k ≥ B` lies in every sound range: the +empty database is compatible with the empty result (no key reaches `B`), while +inserting `k` makes a key reach `B` and keeps the `[E, B)` count below `oE - 1`, so +the two databases, which differ only in the omitted `k`, disagree on compatibility. +No realizability hypothesis is needed: the empty database is the witness directly. -/ +theorem necessary_of_begin_le_crossed {r : RangeRead} {res : Result} + (hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset) + (hmore : res.more = false) (hempty : res.keys = []) + {k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) : + Necessary r res k := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + intro cr hs + by_contra hkcr + have hk0 : k ∉ emptyDB.keys := by simp [emptyDB] + have hkB : ¬ (k : WithTop Key) < r.begin.key := not_lt.mpr hk + have hcntB : cnt (emptyDB.insert k hk0) r.begin.key = 0 := by + rw [cnt_insert_not_lt emptyDB k hk0 hkB] + simp [cnt, emptyDB] + have hlen : (emptyDB.insert k hk0).keys.length = 1 := by + rw [DB.insert_keys]; simp [emptyDB] + have hcomp1 : RangeRead.Compatible emptyDB r { keys := [], more := false } := + compatible_emptyDB_emptyRes r + have hagree : AgreeOn cr emptyDB (emptyDB.insert k hk0) := + (ConflictRange.keys_insert_of_not_mem cr emptyDB hk0 hkcr).symm + have hcomp2 : RangeRead.Compatible (emptyDB.insert k hk0) r + { keys := [], more := false } := (hs emptyDB _ hagree).mp hcomp1 + have hnot2 : ¬ RangeRead.Compatible (emptyDB.insert k hk0) r + { keys := [], more := false } := by + intro hc + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_counts hbegin (by omega)] at hc + have h1 : 1 ≤ (r.end_.offset - 1).toNat := by omega + rw [hcntB, hlen] at hc + omega + exact hnot2 hcomp2 + +/-- **Necessity inside `[E, B)`.** In the long-gap branch (`oE - 1 ≤ intervalCard E B`) +every genuine key `k` with `E ≤ k < B` is necessary. The witnesses are two +databases differing only in `k`: `db₂` holds `oE - 2` keys of `[E, B)` together with +a key `j ≥ B`, so `ψ(db₂)` fails on both disjuncts, while inserting `k` makes the +`[E, B)` count reach `oE - 1` and satisfies the second disjunct. -/ +theorem necessary_of_mem_interval_crossed {r : RangeRead} {res : Result} + (hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset) + (hmore : res.more = false) (hempty : res.keys = []) + (hne : ¬ AlwaysEmpty r) (_hEB : r.end_.key ≤ r.begin.key) + (hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key r.begin.key) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty (by omega) hne + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by + cases hkb : r.begin.key with + | top => exact absurd hkb hBne + | coe b => exact ⟨b, rfl⟩ + have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hkE + exact not_le.mpr (WithTop.coe_lt_top k) hkE + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (↑e : WithTop Key) := by + cases hke : r.end_.key with + | top => exact absurd hke hEne + | coe e => exact ⟨e, rfl⟩ + have hkltb : k < b := by rw [hb] at hkB; exact WithTop.coe_lt_coe.mp hkB + have hke' : e ≤ k := by rw [he] at hkE; exact WithTop.coe_le_coe.mp hkE + have hee : e ≤ b := le_trans hke' (le_of_lt hkltb) + intro cr hs + by_contra hkcr + -- A database of `oE - 2` keys of `[E, B)` avoiding `k`. + set m : ℕ := (r.end_.offset - 2).toNat with hm + have hm1 : m + 1 = (r.end_.offset - 1).toNat := by + rw [hm] + have h3 : (0 : ℤ) ≤ r.end_.offset - 2 := by omega + have h4 : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + have : (((r.end_.offset - 2).toNat + 1 : ℕ) : ℤ) = (r.end_.offset - 1).toNat := by + rw [Nat.cast_add, Nat.cast_one, Int.toNat_of_nonneg h3, Int.toNat_of_nonneg h4] + ring + exact_mod_cast this + have hcard : (m + 1 : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := + calc (m + 1 : ℕ∞) = ((m + 1 : ℕ) : ℕ∞) := rfl + _ = ((r.end_.offset - 1).toNat : ℕ∞) := by rw [hm1] + _ ≤ intervalCard r.end_.key r.begin.key := hgap + obtain ⟨dbK, hdbKrange, hkK, hlenK⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := m) hcard + have hdbKe : ∀ x ∈ dbK.keys, e ≤ x := by + intro x hx + have h := (hdbKrange x hx).1 + rw [he] at h + exact WithTop.coe_le_coe.mp h + have hdbKb : ∀ x ∈ dbK.keys, x < b := by + intro x hx + have h := (hdbKrange x hx).2 + rw [hb] at h + exact WithTop.coe_lt_coe.mp h + have hcntE : cnt dbK (↑e : WithTop Key) = 0 := by + rw [cnt_coe] + exact countP_lt_eq_zero_of_forall_ge hdbKe + have hcntB : cnt dbK (↑b : WithTop Key) = m := by + rw [cnt_coe, countP_lt_eq_length_of_forall_lt hdbKb, hlenK] + -- `j ≥ b`, not already stored. + obtain ⟨j, hjb, hjK⟩ := exists_le_not_mem dbK.keys b + -- `db₂ = dbK.insert j`. + have hjnotin : j ∉ dbK.keys := hjK + have hcntE₂ : cnt (dbK.insert j hjnotin) (↑e : WithTop Key) = 0 := by + rw [cnt_insert_not_lt dbK j hjnotin (v := (↑e : WithTop Key))] + · rw [hcntE] + · rw [WithTop.coe_lt_coe]; intro hlt + exact absurd hlt (not_lt.mpr (le_trans hee hjb)) + have hcntB₂ : cnt (dbK.insert j hjnotin) (↑b : WithTop Key) = m := by + rw [cnt_insert_not_lt dbK j hjnotin (v := (↑b : WithTop Key))] + · rw [hcntB] + · rw [WithTop.coe_lt_coe]; exact not_lt.mpr hjb + have hlen₂ : (dbK.insert j hjnotin).keys.length = m + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlenK] + -- `k` is absent from `db₂`. + have hk_db2 : k ∉ (dbK.insert j hjnotin).keys := by + rw [DB.mem_insert_iff hjnotin] + rintro (rfl | hk) + · exact absurd hkltb (not_lt.mpr hjb) + · exact hkK hk + -- `db₁ = db₂.insert k`. + have hcntB₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2) + (↑b : WithTop Key) = (r.end_.offset - 1).toNat := by + rw [cnt_insert_lt _ k hk_db2 (v := (↑b : WithTop Key)) (WithTop.coe_lt_coe.mpr hkltb), + hcntB₂] + exact hm1 + have hcntE₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2) + (↑e : WithTop Key) = 0 := by + rw [cnt_insert_not_lt _ k hk_db2 (v := (↑e : WithTop Key))] + · exact hcntE₂ + · rw [WithTop.coe_lt_coe]; exact not_lt.mpr hke' + -- `ψ(db₁)` holds by the second disjunct, so `db₁` is compatible. + have hψ₁ : cnt ((dbK.insert j hjnotin).insert k hk_db2) r.end_.key + + (r.end_.offset - 1).toNat ≤ + cnt ((dbK.insert j hjnotin).insert k hk_db2) r.begin.key := by + rw [hb, he, hcntE₁, hcntB₁, zero_add] + have hcomp₁ : RangeRead.Compatible ((dbK.insert j hjnotin).insert k hk_db2) r + { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_counts hbegin (by omega)).mpr (Or.inr hψ₁)) + -- `ψ(db₂)` fails, so `db₂` is incompatible. + have hnot₂ : ¬ RangeRead.Compatible (dbK.insert j hjnotin) r + { keys := [], more := false } := by + intro hc + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_counts hbegin (by omega)] at hc + have hcntE₂' : cnt (dbK.insert j hjnotin) r.end_.key = 0 := by rw [he]; exact hcntE₂ + have hcntB₂' : cnt (dbK.insert j hjnotin) r.begin.key = m := by rw [hb]; exact hcntB₂ + rcases hc with hc | hc + · rw [hcntB₂', hlen₂] at hc; omega + · rw [hcntE₂', hcntB₂'] at hc; omega + have hagree : AgreeOn cr (dbK.insert j hjnotin) ((dbK.insert j hjnotin).insert k hk_db2) := + (ConflictRange.keys_insert_of_not_mem cr (dbK.insert j hjnotin) hk_db2 hkcr).symm + exact hnot₂ ((hs (dbK.insert j hjnotin) _ hagree).mpr hcomp₁) + +/-- A genuine key `b` has infinitely many keys at or above it, so the interval +`[b, ⊤)` realizes any finite count. -/ +theorem natCast_le_intervalCard_coe_top (b : Key) (m : ℕ) : + (m : ℕ∞) ≤ intervalCard (↑b : WithTop Key) (⊤ : WithTop Key) := by + rw [intervalCard_top_right (b := (↑b : WithTop Key)) WithTop.coe_ne_top] + exact le_top + +/-- A database of `m` keys, all at or above `b`, avoiding the key `k`. Built from +`exists_db_keysIn_avoid` on the half-infinite interval `[b, ⊤)`. -/ +theorem exists_db_ge_avoid (b : Key) (m : ℕ) (k : Key) : + ∃ db : DB, (∀ x ∈ db.keys, b ≤ x) ∧ k ∉ db.keys ∧ db.keys.length = m := by + obtain ⟨db, hdb, hk, hlen⟩ := + exists_db_keysIn_avoid (B := (↑b : WithTop Key)) (E := ⊤) (k := k) (m := m) + (natCast_le_intervalCard_coe_top b (m + 1)) + refine ⟨db, fun x hx => ?_, hk, hlen⟩ + have h := hdb x hx + rw [keysIn_coe_top] at h + exact h + +/-- **Necessity at or above `B`, general positive offsets.** Every key +`k ≥ B` lies in every sound range. The witnesses are two databases differing only +in `k`: the base stores `b = (begin.offset-1).toNat` keys at or above `B` and +satisfies `ψ` on the first disjunct (`#stored ≥ B = b`), while inserting `k` pushes +that count to `b + 1` and leaves the `[E, B)` count at `0`, so both disjuncts fail +(`b + 1 > b` and `e > b`). The offset-one empty-database witness is the case +`b = 0`. -/ +theorem necessary_of_begin_le_general {r : RangeRead} {res : Result} + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) + (hmore : res.more = false) (hempty : res.keys = []) + (hne : ¬ AlwaysEmpty r) (hEB : r.end_.key ≤ r.begin.key) + {k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) : + Necessary r res k := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty hbegin hne + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by + cases hkb : r.begin.key with + | top => exact absurd hkb hBne + | coe b => exact ⟨b, rfl⟩ + have hbk : b ≤ k := by rw [hb] at hk; exact WithTop.coe_le_coe.mp hk + rw [hb] at hEB + -- `¬ AlwaysEmpty` forces `(end.offset-1).toNat > (begin.offset-1).toNat`, else + -- the second disjunct is vacuous. + have heb : ¬ ((r.end_.offset - 1).toNat ≤ (r.begin.offset - 1).toNat) := by + intro hle + apply hne + intro db + rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hbegin (by omega)] + exact Or.inr (by + have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := + cnt_le_cnt_of_le db (by rw [hb]; exact hEB) + omega) + refine Necessary.of_readable ⟨emptyDB, compatible_emptyDB_emptyRes r⟩ ?_ + intro db₀ _ cr hs + by_contra hkcr + -- Base: `(begin.offset-1).toNat` keys at or above `B`, avoiding `k`. + obtain ⟨db₂, hdb₂, hk₂, hlen₂⟩ := + exists_db_ge_avoid b ((r.begin.offset - 1).toNat) k + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb, cnt_coe, List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hdb₂ x hx) + have hcntE₂ : cnt db₂ r.end_.key = 0 := by + rw [cnt, List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (le_trans hEB (WithTop.coe_le_coe.mpr (hdb₂ x hx))) + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key)] + · exact hcntB₂ + · rw [hb]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hbk) + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key)] + · exact hcntE₂ + · exact not_lt.mpr (le_trans hEB (WithTop.coe_le_coe.mpr hbk)) + have hlen₁ : (db₂.insert k hk₂).keys.length = (r.begin.offset - 1).toNat + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + -- `ψ(db₂)` holds on the first disjunct; `ψ(db₁)` fails both. + have hψ₂ : RangeRead.completeKeys db₂ r = [] := + (completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega)) + have hbig₁ : ¬ ((db₂.insert k hk₂).keys.length ≤ + cnt (db₂.insert k hk₂) r.begin.key + (r.begin.offset - 1).toNat) := by + rw [hlen₁, hcntB₁]; omega + have hnotψ₁ : ¬ (cnt (db₂.insert k hk₂) r.end_.key + (r.end_.offset - 1).toNat ≤ + cnt (db₂.insert k hk₂) r.begin.key + (r.begin.offset - 1).toNat) := by + rw [hcntB₁, hcntE₁] + have := Nat.lt_of_not_le heb + omega + have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr hψ₂ + have hcomp₁ : RangeRead.Compatible (db₂.insert k hk₂) r + { keys := [], more := false } := (hs db₂ _ hagree).mp hcomp₂ + have hψ₁ := (completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) + hbegin (by omega)).mp ((compatible_empty_iff_complete_nil + (db := db₂.insert k hk₂)).mp hcomp₁) + exact hψ₁.elim hbig₁ hnotψ₁ + +/-- **Necessity inside `[E, B)`, general positive offsets.** In the long-gap branch +(`g := (oE-1) - (oB-1) ≤ intervalCard E B`) every genuine key `k` with `E ≤ k < B` is +necessary. The base database stores `g - 1` keys of `[E, B) ∖ {k}` together with +`b + 1` keys at or above `B` (where `b = (oB-1).toNat`), so it fails both disjuncts +of `ψ` (`f = g - 1 < g` and `#stored ≥ B = b + 1 > b`); inserting `k` brings `f` to +`g` and flips `ψ` true. The two databases differ only in `k`, so they agree on any +range omitting it. -/ +theorem necessary_of_mem_interval_general {r : RangeRead} {res : Result} + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) + (hmore : res.more = false) (hempty : res.keys = []) + (hne : ¬ AlwaysEmpty r) (hEB : r.end_.key ≤ r.begin.key) + (hgap : ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty hbegin hne + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by + cases hkb : r.begin.key with + | top => exact absurd hkb hBne + | coe b => exact ⟨b, rfl⟩ + have hkb : k < b := by rw [hb] at hkB; exact WithTop.coe_lt_coe.mp hkB + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (↑e : WithTop Key) := by + cases hke : r.end_.key with + | top => rw [hke] at hkE; exact absurd hkE (not_le.mpr (WithTop.coe_lt_top k)) + | coe e => exact ⟨e, rfl⟩ + have hke : e ≤ k := by rw [he] at hkE; exact WithTop.coe_le_coe.mp hkE + have heb : e ≤ b := le_trans hke (le_of_lt hkb) + have hEB' : r.end_.key ≤ r.begin.key := hEB + rw [hb] at hEB + -- `g := (oE-1) - (oB-1) ≥ 1`. + obtain ⟨g, hg⟩ : ∃ g : ℕ, g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := + ⟨_, rfl⟩ + have hgpos : 1 ≤ g := by + have hlt : (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat := + Nat.lt_of_not_le (by + intro hle + apply hne + intro db + rw [completeKeys_eq_nil_iff_psi (db := db) (r := r) hbegin (by omega)] + exact Or.inr (by + have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := + cnt_le_cnt_of_le db hEB' + omega)) + rw [hg]; exact Nat.sub_pos_of_lt hlt + -- Room for `g - 1` interval keys: `g ≤ intervalCard E B`. + have hcardA : (((g - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + rw [Nat.sub_add_cancel hgpos, hg]; exact hgap + obtain ⟨dbA, hdbA, hkA, hlenA⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) (m := g - 1) + hcardA + -- `b + 1` keys at or above `B`, avoiding `k`. + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid b ((r.begin.offset - 1).toNat + 1) k + -- Combine: every `A`-key is below `B`, every `J`-key at or above it. + have hAe : ∀ x ∈ dbA.keys, e ≤ x := fun x hx => by + have h := (hdbA x hx).1 + rw [he] at h + exact WithTop.coe_le_coe.mp h + have hAb : ∀ x ∈ dbA.keys, x < b := fun x hx => by + have h := (hdbA x hx).2 + rw [hb] at h + exact WithTop.coe_lt_coe.mp h + have hcross : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y := + fun x hx y hy => lt_of_lt_of_le (hAb x hx) (hdbJ y hy) + let ks : List Key := dbA.keys ++ dbJ.keys + have hsorted : ks.SortedLT := List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcross⟩ + let db₂ : DB := ⟨ks, hsorted⟩ + have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by + rw [hkeys₂, List.mem_append] + rintro (h | h) + · exact hkA h + · exact hkJ h + have hlen₂ : db₂.keys.length = g + (r.begin.offset - 1).toNat := by + rw [hkeys₂, List.length_append, hlenA, hlenJ] + have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg + omega + have hcntB₂ : cnt db₂ r.begin.key = g - 1 := by + rw [hb, cnt_coe, hkeys₂, List.countP_append] + have h1 : dbA.keys.countP (fun x => decide (x < b)) = (g - 1) := by + rw [countP_lt_eq_length_of_forall_lt hAb, hlenA] + have h2 : dbJ.keys.countP (fun x => decide (x < b)) = 0 := + countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx) + rw [h1, h2, Nat.add_zero] + have hcntE₂ : cnt db₂ r.end_.key = 0 := by + rw [he, cnt_coe, hkeys₂, List.countP_append] + rw [countP_lt_eq_zero_of_forall_ge (l := dbA.keys) hAe, + countP_lt_eq_zero_of_forall_ge (l := dbJ.keys) + (fun x hx => le_trans heb (hdbJ x hx)), Nat.add_zero] + -- `ψ(db₂)` fails both disjuncts. + have hbig₂ : ¬ (db₂.keys.length ≤ cnt db₂ r.begin.key + (r.begin.offset - 1).toNat) := by + rw [hlen₂, hcntB₂] + have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg + omega + have hnotψ₂ : ¬ (cnt db₂ r.end_.key + (r.end_.offset - 1).toNat ≤ + cnt db₂ r.begin.key + (r.begin.offset - 1).toNat) := by + rw [hcntE₂, hcntB₂] + have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg + omega + -- `db₁ = db₂.insert k`. + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = g := by + rw [cnt_insert_lt db₂ k hk₂ (v := r.begin.key) (by rw [hb]; exact WithTop.coe_lt_coe.mpr hkb), + hcntB₂] + omega + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) + (by rw [he]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hke)), + hcntE₂] + have hlen₁ : (db₂.insert k hk₂).keys.length = db₂.keys.length + 1 := by + rw [DB.insert_keys, List.orderedInsert_length] + -- `ψ(db₁)` holds on the second disjunct. + have hψ₁ : (cnt (db₂.insert k hk₂) r.end_.key + (r.end_.offset - 1).toNat ≤ + cnt (db₂.insert k hk₂) r.begin.key + (r.begin.offset - 1).toNat) := by + rw [hcntE₁, hcntB₁] + have hgg : g = (r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat := hg + omega + -- `db₂.insert k` is compatible (`ψ` true on the second disjunct); toggling `k` + -- out yields the incompatible `db₂`, so any sound range must contain `k`. + have hcomp₀ : RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)).mpr + (Or.inr hψ₁)) + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm + have hcomp₁ : RangeRead.Compatible db₂ r { keys := [], more := false } := + (hs db₂ (db₂.insert k hk₂) hagree).mpr hcomp₀ + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)] at hcomp₁ + exact hcomp₁.elim hbig₂ hnotψ₂ + +/-- **The candidate range for the crossed empty case.** `[E, ⊤)` in the long gap +(`oE - 1 ≤ intervalCard E B`) and `[B, ⊤)` in the short gap. -/ +def crossedRange (r : RangeRead) : ConflictRange := + if ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key r.begin.key + then ⟨r.end_.key, ⊤⟩ else ⟨r.begin.key, ⊤⟩ + +/-- At `begin.offset = 1` the all-positive-offset `crossedRangeGeneral` agrees with +`crossedRange`. -/ +theorem crossedRangeGeneral_eq_crossedRange {r : RangeRead} (hB : r.begin.offset = 1) : + crossedRangeGeneral r = crossedRange r := by + unfold crossedRangeGeneral crossedRange + rw [hB] + norm_num + +/-- **Minimality of the crossed candidate.** In the crossed empty case the range +`crossedRange r` is the minimal sound conflict range: `[E, ⊤)` in the long gap, +where both disjuncts of `ψ` are functions of the stored keys at or above `E`, and +`[B, ⊤)` in the short gap, where `ψ` collapses to "no stored key reaches `B`". -/ +theorem minimal_crossedRange_of_empty {r : RangeRead} {res : Result} + (hmore : res.more = false) (hempty : res.keys = []) + (hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset) + (hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) : + Minimal r res (crossedRange r) := by + have hEB : r.end_.key ≤ r.begin.key := le_of_lt hcross + unfold crossedRange + by_cases hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ intervalCard r.end_.key r.begin.key + · rw [ite_eq_left hgap] + refine (minimal_iff_sound_and_necessaryRange).mpr + ⟨sound_top_end_of_crossed hbegin (by omega) hEB hmore hempty, ?_⟩ + intro k hkE _ + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_of_mem_interval_crossed hbegin hoE hmore hempty hne hEB hgap hkE hkB + · exact necessary_of_begin_le_crossed hbegin hoE hmore hempty (le_of_not_gt hkB) + · rw [ite_eq_right hgap] + refine (minimal_iff_sound_and_necessaryRange).mpr + ⟨sound_top_begin_of_short hbegin (by omega) hEB (not_le.mp hgap) hmore hempty, ?_⟩ + intro k hkB _ + exact necessary_of_begin_le_crossed hbegin hoE hmore hempty hkB + +/-- The minimal crossed range is unique up to set equality. -/ +theorem unique_minimal_crossedRange_of_empty {r : RangeRead} {res : Result} + (hmore : res.more = false) (hempty : res.keys = []) + (hbegin : r.begin.offset = 1) (hoE : 2 ≤ r.end_.offset) + (hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_crossedRange_of_empty hmore hempty hbegin hoE hcross hne) + +/-- **Minimality of the crossed candidate, all positive offsets.** The +all-offset extension of `minimal_crossedRange_of_empty`: in the crossed empty case +with `begin.offset ≥ 1` and `end.offset ≥ 2`, the range `crossedRangeGeneral r` is +the minimal sound conflict range — `[E, ⊤)` in the long gap +(`(oE-1) - (oB-1) ≤ intervalCard E B`) and `[B, ⊤)` in the short gap. Soundness is +`sound_top_end_of_crossed_general`/`sound_top_begin_of_short_general`; necessity of +a key `≥ E` splits at `B`, using `necessary_of_mem_interval_general` inside `[E, B)` +and `necessary_of_begin_le_general` at or above it. -/ +theorem minimal_crossedRangeGeneral_of_empty {r : RangeRead} {res : Result} + (hmore : res.more = false) (hempty : res.keys = []) + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) + (hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) : + Minimal r res (crossedRangeGeneral r) := by + have hEB : r.end_.key ≤ r.begin.key := le_of_lt hcross + unfold crossedRangeGeneral + by_cases hgap : ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat) ≤ + intervalCard r.end_.key r.begin.key + · rw [ite_eq_left hgap] + refine (minimal_iff_sound_and_necessaryRange).mpr + ⟨sound_top_end_of_crossed_general hbegin (by omega) hEB hmore hempty, ?_⟩ + intro k hkE _ + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_of_mem_interval_general hbegin hoE hmore hempty hne hEB hgap hkE hkB + · exact necessary_of_begin_le_general hbegin hoE hmore hempty hne hEB (le_of_not_gt hkB) + · rw [ite_eq_right hgap] + refine (minimal_iff_sound_and_necessaryRange).mpr + ⟨sound_top_begin_of_short_general hbegin (by omega) hEB (not_le.mp hgap) hmore hempty, ?_⟩ + intro k hkB _ + exact necessary_of_begin_le_general hbegin hoE hmore hempty hne hEB hkB + +/-- The minimal general crossed range is unique up to set equality. -/ +theorem unique_minimal_crossedRangeGeneral_of_empty {r : RangeRead} {res : Result} + (hmore : res.more = false) (hempty : res.keys = []) + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) + (hcross : r.end_.key < r.begin.key) (hne : ¬ AlwaysEmpty r) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_crossedRangeGeneral_of_empty hmore hempty hbegin hoE hcross hne) + +/-- **Necessity at or above `B`, cross-free, general positive offsets.** Every +genuine key `k ≥ begin.key` lies in every sound range, when `begin.key ≤ end.key`, +`begin.offset ≥ 1`, `end.offset ≥ 2` and the read is not trivially empty. The +witnesses differ only in `k`: they store some keys in `[B, E)` and the slack +`b = (oB-1).toNat` worth of keys at or above `E`, and inserting `k` flips exactly +one disjunct of the cross-free predicate +`(above B).length ≤ b ∨ #(between B E) + e ≤ b`. -/ +theorem necessary_of_begin_le_of_le_general {r : RangeRead} {res : Result} + (hbegin : 1 ≤ r.begin.offset) (hoE : 2 ≤ r.end_.offset) + (hmore : res.more = false) (hempty : res.keys = []) + (hne : ¬ AlwaysEmpty r) (hle : r.begin.key ≤ r.end_.key) + {k : Key} (hk : r.begin.key ≤ (k : WithTop Key)) : + Necessary r res k := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hempty hmore; subst hempty; subst hmore; rfl + subst hres + have hBne : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_not_alwaysEmpty hbegin hne + obtain ⟨b₀, hb₀⟩ : ∃ b₀ : Key, r.begin.key = (↑b₀ : WithTop Key) := by + cases h : r.begin.key with + | top => exact absurd h hBne + | coe b₀ => exact ⟨b₀, rfl⟩ + have hbk : b₀ ≤ k := by rw [hb₀] at hk; exact WithTop.coe_le_coe.mp hk + have hkb : ¬ (k : WithTop Key) < r.begin.key := by + rw [hb₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hbk) + set b : ℕ := (r.begin.offset - 1).toNat with hbdef + set e : ℕ := (r.end_.offset - 1).toNat with hedef + have he1 : 1 ≤ e := by omega + intro cr hs + by_contra hkcr + by_cases hEtop : r.end_.key = ⊤ + · -- `E = ⊤`: `ψ` reads `#(stored ≥ B) ≤ b`. + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid b₀ b k + let db₂ : DB := ⟨dbJ.keys, dbJ.sorted⟩ + have hkeys₂ : db₂.keys = dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by rw [hkeys₂]; exact hkJ + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx)] + have hcntE₂ : cnt db₂ r.end_.key = b := by + rw [hEtop, cnt_top, hkeys₂, hlenJ] + have hlen₂ : db₂.keys.length = b := hlenJ + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega))) + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [DB.insert_keys, List.orderedInsert_length, hlen₂, hcntB₁] at hh; omega + · rw [hEtop, cnt_top, DB.insert_keys, List.orderedInsert_length, hlen₂, hcntB₁] at hh + omega + have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm + exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂) + · -- `E = ↑e₀` genuine. + obtain ⟨e₀, he₀⟩ : ∃ e₀ : Key, r.end_.key = (↑e₀ : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h hEtop + | coe e₀ => exact ⟨e₀, rfl⟩ + have hbe₀ : b₀ ≤ e₀ := by + have hh := hle; rw [hb₀, he₀] at hh; exact WithTop.coe_le_coe.mp hh + rcases lt_or_ge b e with hlt_be | hge_be + · -- `b < e`: the second disjunct is always false, so `ψ` is `#(stored ≥ B) ≤ b`. + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ b k + let db₂ : DB := ⟨dbJ.keys, dbJ.sorted⟩ + have hkeys₂ : db₂.keys = dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by rw [hkeys₂]; exact hkJ + have hlen₂ : db₂.keys.length = b := by rw [hkeys₂, hlenJ] + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge] + intro x hx + exact le_trans hbe₀ (hdbJ x hx) + have hcntE₂ : cnt db₂ r.end_.key = 0 := by + rw [he₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx)] + have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega))) + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [hlen₁, hcntB₁] at hh; omega + · rw [hcntB₁] at hh + by_cases hkE : r.end_.key ≤ (k : WithTop Key) + · rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) (by + rw [he₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr + (by rw [he₀] at hkE; exact WithTop.coe_le_coe.mp hkE))), hcntE₂] at hh + omega + · have hke₀ : k < e₀ := by + rw [he₀] at hkE; exact WithTop.coe_lt_coe.mp (lt_of_not_ge hkE) + rw [cnt_insert_lt db₂ k hk₂ (v := r.end_.key) (by + rw [he₀]; exact WithTop.coe_lt_coe.mpr hke₀), hcntE₂] at hh + omega + have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm + exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂) + · -- `e ≤ b`. + have hed : e ≤ b := hge_be + have hroom := tsub_add_one_le_intervalCard_of_le_general hbegin hoE hle hne hed + by_cases hkE : r.end_.key ≤ (k : WithTop Key) + · -- `k ≥ E`: `b - e + 1` keys of `[B, E)` and `e - 1` keys `≥ E`. + have hke₀ : e₀ ≤ k := by rw [he₀] at hkE; exact WithTop.coe_le_coe.mp hkE + have hkEnlt : ¬ (k : WithTop Key) < r.end_.key := by + rw [he₀]; exact not_lt.mpr (WithTop.coe_le_coe.mpr hke₀) + obtain ⟨dbA, hdbA, hlenA⟩ := + exists_db_keysIn (B := r.begin.key) (E := r.end_.key) (m := b - e + 1) + (by simpa only [← hbdef, ← hedef] using hroom) + have hAhi : ∀ x ∈ dbA.keys, x < e₀ := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).2 + have hAlo : ∀ x ∈ dbA.keys, b₀ ≤ x := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).1 + have hAk : k ∉ dbA.keys := fun h => absurd (hAhi k h) (not_lt.mpr hke₀) + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ (e - 1) k + have hcrossJ : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y := + fun x hx y hy => lt_of_lt_of_le (hAhi x hx) (hdbJ y hy) + let db₂ : DB := ⟨dbA.keys ++ dbJ.keys, + List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcrossJ⟩⟩ + have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by + rw [hkeys₂, List.mem_append]; rintro (h | h); exacts [hAk h, hkJ h] + have hlen₂ : db₂.keys.length = b := by + rw [hkeys₂, List.length_append, hlenA, hlenJ]; simp only [hbdef, hedef]; omega + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact hAlo x hx + · exact le_trans hbe₀ (hdbJ x hx) + have hcntE₂ : cnt db₂ r.end_.key = b - e + 1 := by + rw [he₀, cnt_coe, hkeys₂, List.countP_append, + countP_lt_eq_length_of_forall_lt hAhi, hlenA, + countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx), Nat.add_zero] + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = b - e + 1 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.end_.key) hkEnlt, hcntE₂] + have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inl (by rw [hlen₂, hcntB₂]; omega))) + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [hlen₁, hcntB₁] at hh; omega + · rw [hcntE₁, hcntB₁, hbdef, hedef] at hh; omega + have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm + exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂) + · -- `k ∈ [B, E)`: `b - e` keys of `[B, E) \ {k}` and `e` keys `≥ E`. + have hke₀ : k < e₀ := by + rw [he₀] at hkE; exact WithTop.coe_lt_coe.mp (lt_of_not_ge hkE) + have hkElt : (k : WithTop Key) < r.end_.key := by + rw [he₀]; exact WithTop.coe_lt_coe.mpr hke₀ + obtain ⟨dbA, hdbA, hkA, hlenA⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := k) (m := b - e) + (by simpa only [Nat.cast_add, Nat.cast_one, hbdef, hedef] using hroom) + have hAhi : ∀ x ∈ dbA.keys, x < e₀ := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).2 + have hAlo : ∀ x ∈ dbA.keys, b₀ ≤ x := fun x hx => by + have h := hdbA x hx; rw [hb₀, he₀] at h; exact (mem_keysIn_coe.mp h).1 + obtain ⟨dbJ, hdbJ, hkJ, hlenJ⟩ := exists_db_ge_avoid e₀ e k + have hcrossJ : ∀ x ∈ dbA.keys, ∀ y ∈ dbJ.keys, x < y := + fun x hx y hy => lt_of_lt_of_le (hAhi x hx) (hdbJ y hy) + let db₂ : DB := ⟨dbA.keys ++ dbJ.keys, + List.sortedLT_append.mpr ⟨dbA.sorted, dbJ.sorted, hcrossJ⟩⟩ + have hkeys₂ : db₂.keys = dbA.keys ++ dbJ.keys := rfl + have hk₂ : k ∉ db₂.keys := by + rw [hkeys₂, List.mem_append]; rintro (h | h); exacts [hkA h, hkJ h] + have hlen₂ : db₂.keys.length = b := by + rw [hkeys₂, List.length_append, hlenA, hlenJ]; simp only [hbdef, hedef]; omega + have hcntB₂ : cnt db₂ r.begin.key = 0 := by + rw [hb₀, cnt_coe, hkeys₂, countP_lt_eq_zero_of_forall_ge] + intro x hx + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact hAlo x hx + · exact le_trans hbe₀ (hdbJ x hx) + have hcntE₂ : cnt db₂ r.end_.key = b - e := by + rw [he₀, cnt_coe, hkeys₂, List.countP_append, + countP_lt_eq_length_of_forall_lt hAhi, hlenA, + countP_lt_eq_zero_of_forall_ge (fun x hx => hdbJ x hx), Nat.add_zero] + have hcntB₁ : cnt (db₂.insert k hk₂) r.begin.key = 0 := by + rw [cnt_insert_not_lt db₂ k hk₂ (v := r.begin.key) hkb, hcntB₂] + have hcntE₁ : cnt (db₂.insert k hk₂) r.end_.key = b - e + 1 := by + rw [cnt_insert_lt db₂ k hk₂ (v := r.end_.key) hkElt, hcntE₂] + have hlen₁ : (db₂.insert k hk₂).keys.length = b + 1 := by + rw [DB.insert_keys, List.orderedInsert_length, hlen₂] + have hcomp₂ : RangeRead.Compatible db₂ r { keys := [], more := false } := + compatible_empty_iff_complete_nil.mpr + ((completeKeys_eq_nil_iff_psi (db := db₂) (r := r) hbegin (by omega)).mpr + (Or.inr (by rw [hcntE₂, hcntB₂, hbdef, hedef]; omega))) + have hnot₁ : ¬ RangeRead.Compatible (db₂.insert k hk₂) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_nil_iff_psi (db := db₂.insert k hk₂) (r := r) hbegin (by omega)] + rintro (hh | hh) + · rw [hlen₁, hcntB₁] at hh; omega + · rw [hcntE₁, hcntB₁, hbdef, hedef] at hh; omega + have hagree : AgreeOn cr db₂ (db₂.insert k hk₂) := + (ConflictRange.keys_insert_of_not_mem cr db₂ hk₂ hkcr).symm + exact hnot₁ ((hs db₂ _ hagree).mp hcomp₂) + +/-- **The cross-free empty result at all positive begin offsets.** With +`begin.offset ≥ 1`, `end.offset ≥ 2`, `begin.key ≤ end.key` and a non-trivially-empty +read, the top range `[begin.key, ⊤)` is the minimal sound conflict range for the +empty complete result. Soundness is `sound_topRange_of_le_general`; necessity is +`necessary_of_begin_le_of_le_general`. At `begin.offset = 1` this is +`minimal_topRange_of_end_offset_ge_two_empty`. -/ +theorem minimal_topRange_of_le_empty {r : RangeRead} {res : Result} + (_hread : Readable r res) (hmore : res.more = false) (_hrev : r.reverse = false) + (hempty : res.keys = []) (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r) : + Minimal r res (topRange r) := by + refine (minimal_iff_sound_and_necessaryRange).mpr + ⟨sound_topRange_of_le_general hbegin (by omega) hle hmore hempty, ?_⟩ + intro k hlo _ + exact necessary_of_begin_le_of_le_general hbegin hend hmore hempty hne hle hlo + +/-- The minimal top range for an empty complete result at all positive begin +offsets is unique up to set equality. -/ +theorem unique_minimal_topRange_of_le_empty {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hempty : res.keys = []) (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) (hne : ¬ AlwaysEmpty r) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topRange_of_le_empty hread hmore hrev hempty hbegin hend hle hne) + +end FormalConflictRange diff --git a/FormalConflictRange/FiniteCountDecider.lean b/FormalConflictRange/FiniteCountDecider.lean new file mode 100644 index 0000000..814c3c0 --- /dev/null +++ b/FormalConflictRange/FiniteCountDecider.lean @@ -0,0 +1,902 @@ +/- +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.CountBridge +import FormalConflictRange.DiffFeasible + +/-! +# Deciding the finite count relaxation + +We translate the finite count feasibility of `CountBridge.lean` into integer +difference constraints on cumulative counts and decide the latter by variable +elimination (`DiffFeasible.lean`). +-/ + +namespace FormalConflictRange + +/-- Prefix sum of the first `i` interval counts. -/ +def pref (counts : List ℕ) (i : ℕ) : ℕ := (counts.take i).sum + +/-- Cumulative counts extracted from a count assignment, with the last variable +standing for the total count including the unbounded final interval. -/ +def countsToX (n : ℕ) (counts : List ℕ) (topCount : ℕ) (i : ℕ) : ℤ := + if i = n + 1 then (counts.sum + topCount : ℕ) else (pref counts i : ℕ) + +/-- The cumulative lookup in `countAtWith` is the prefix sum. -/ +theorem countAtWith_eq_pref (base : ℕ) {bounds : List Key} {counts : List ℕ} + (hlen : counts.length = bounds.length) {k : Key} (hk : k ∈ bounds) : + countAtWith base bounds counts k = + base + pref counts (bounds.idxOf k + 1) := by + induction bounds generalizing base counts with + | nil => simp at hk + | cons b bs ih => + cases counts with + | nil => simp at hlen + | cons d ds => + have hlen' : ds.length = bs.length := by simpa using hlen + by_cases hkb : k = b + · subst k + simp [countAtWith, pref, List.idxOf_cons_self] + · have hkbs : k ∈ bs := by + rcases List.mem_cons.mp hk with rfl | hkbs + · exact absurd rfl hkb + · exact hkbs + rw [List.idxOf_cons_ne bs (fun h => hkb h.symm)] + simp only [countAtWith, if_neg hkb] + rw [ih (base := base + d) hlen' hkbs] + simp [pref, List.take_succ_cons, List.sum_cons] + omega + + +/-- The variable index of an endpoint in the cumulative count vector. -/ +def endpointIndex (bounds : List Key) : WithTop Key → ℕ + | some k => if k = ([] : Key) then 0 else bounds.idxOf k + 1 + | none => bounds.length + 1 + +/-- Capacity and nonnegativity edges for consecutive interval counts. -/ +def capEdgesAux (start : Key) (bounds : List Key) (i : ℕ) : List DEdge := + match bounds with + | [] => [] + | b :: bs => + [⟨i + 1, i, 0⟩] ++ + (if h : intervalCard (start : WithTop Key) (b : WithTop Key) = ⊤ then [] + else [⟨i, i + 1, ((intervalCard (start : WithTop Key) (b : WithTop Key)).toNat : ℤ)⟩]) ++ + capEdgesAux b bs (i + 1) + +/-- The full capacity/order system, including the nonnegativity of the final +unbounded interval count. -/ +def capEdges (bounds : List Key) : List DEdge := + capEdgesAux ([] : Key) bounds 0 ++ + [⟨bounds.length + 1, bounds.length, 0⟩] + +/-- The coefficient of the toggle shift on an endpoint. -/ +def toggleCoeff (stored : Bool) (k : Key) (e : WithTop Key) : ℤ := + if (k : WithTop Key) < e then (if stored then (-1 : ℤ) else 1) else 0 + +/-- An edge for a constraint `lo hi bound`. -/ +def constraintEdge (bounds : List Key) (lo hi : WithTop Key) (w : ℤ) : DEdge := + ⟨endpointIndex bounds lo, endpointIndex bounds hi, w⟩ + +/-- The edges encoding a whole constraint system. -/ +def constraintEdges (bounds : List Key) (S : ConstraintSystem) : List DEdge := + S.map fun c => constraintEdge bounds c.lo c.hi c.bound + +/-- The edges encoding the toggled failures of a chosen list of constraints. -/ +def badEdges (bounds : List Key) (k : Key) (stored : Bool) + (bad : List Constraint) : List DEdge := + bad.map fun c => + constraintEdge bounds c.hi c.lo + (toggleCoeff stored k c.hi - toggleCoeff stored k c.lo - c.bound - 1) + +/-- The two edges forcing the toggle indicator to be `1` (stored) or `0` (absent). -/ +def branchEqEdges (bounds : List Key) (k : Key) (stored : Bool) : List DEdge := + let i := endpointIndex bounds (k : WithTop Key) + let j := endpointIndex bounds ((keyAfter k : Key) : WithTop Key) + if stored then [⟨i, j, 1⟩, ⟨j, i, -1⟩] else [⟨i, j, 0⟩, ⟨j, i, 0⟩] + +/-- The complete difference system for one branch of the finite count relaxation. -/ +def countDiffEdges (bounds : List Key) (k : Key) (stored : Bool) + (S : ConstraintSystem) (bad : List Constraint) : List DEdge := + capEdges bounds ++ constraintEdges bounds S ++ badEdges bounds k stored bad ++ + branchEqEdges bounds k stored + +/-- The finite-count feasibility predicate for one toggle-indicator branch. -/ +def CountFeasibleBranch (bounds : List Key) (k : Key) (stored : Bool) + (S : ConstraintSystem) (bad : List Constraint) : Prop := + ∃ counts : List ℕ, ∃ topCount : ℕ, + CountsFit ([] : Key) bounds counts ∧ + SystemHoldsWithCounts S bounds counts topCount ∧ + evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = (if stored then 1 else 0) ∧ + ∀ c ∈ bad, c.bound < + evalCountToggled bounds counts topCount k c.hi - + evalCountToggled bounds counts topCount k c.lo + +/-- `x` has prefix sums given by `counts`, starting at index `i`. -/ +def PrefixCum (x : ℕ → ℤ) (i : ℕ) (counts : List ℕ) : Prop := + ∀ k ≤ counts.length, x (i + k) - x i = (pref counts k : ℤ) + +theorem pref_succ (counts : List ℕ) {i : ℕ} (hi : i < counts.length) : + pref counts (i + 1) = pref counts i + counts[i] := by + unfold pref + rw [List.sum_take_succ counts i hi] + +theorem pref_zero (counts : List ℕ) : pref counts 0 = 0 := by simp [pref] + +theorem pref_cons_succ (d : ℕ) (ds : List ℕ) (k : ℕ) : + pref (d :: ds) (k + 1) = d + pref ds k := by + simp [pref, List.take_succ_cons, List.sum_cons] + +theorem prefixCum_cons_head {x : ℕ → ℤ} {i d} {ds : List ℕ} + (h : PrefixCum x i (d :: ds)) : + x (i + 1) - x i = (d : ℤ) := by + have := h 1 (by simp) + simpa [pref_cons_succ, pref_zero] using this + +theorem prefixCum_tail {x : ℕ → ℤ} {i d} {ds : List ℕ} + (h : PrefixCum x i (d :: ds)) : PrefixCum x (i + 1) ds := by + intro k hk + have h1 := h (k + 1) (by simp [Nat.succ_le_succ_iff.mpr hk]) + have h0 := h 1 (by simp) + have : x (i + (k + 1)) - x i - (x (i + 1) - x i) = + (pref (d :: ds) (k + 1) : ℤ) - (pref (d :: ds) 1 : ℤ) := by + rw [h1, h0] + rw [pref_cons_succ, pref_cons_succ, pref_zero] at this + have harg : i + (k + 1) = i + 1 + k := by omega + rw [harg] at this + have hpref : ((d + pref ds k : ℕ) : ℤ) - ((d + 0 : ℕ) : ℤ) = (pref ds k : ℤ) := by + push_cast + ring + linarith + +theorem prefixCum_countsToX (n : ℕ) (counts : List ℕ) (topCount : ℕ) + (hlen : counts.length = n) : PrefixCum (countsToX n counts topCount) 0 counts := by + intro k hk + have hk' : k ≤ n := by omega + have hne : k ≠ n + 1 := by omega + simp [countsToX, hne, pref_zero] + +theorem length_eq_of_countsFit {start : Key} {bounds : List Key} {counts : List ℕ} + (h : CountsFit start bounds counts) : counts.length = bounds.length := by + induction bounds generalizing start counts with + | nil => + cases counts with + | nil => rfl + | cons d ds => simp [CountsFit] at h + | cons b bs ih => + cases counts with + | nil => simp [CountsFit] at h + | cons d ds => simp [CountsFit] at h; exact congrArg Nat.succ (ih h.2.2) + +/-- Capacity edges hold when the prefix-cumulative assignment comes from a +capacity-fitting count list. -/ +theorem capEdgesAux_holds_of_countsFit {start : Key} {bounds : List Key} + {counts : List ℕ} {x : ℕ → ℤ} {i : ℕ} + (hfit : CountsFit start bounds counts) (hlen : counts.length = bounds.length) + (hx : PrefixCum x i counts) : + HoldsAll x (capEdgesAux start bounds i) := by + induction bounds generalizing start counts i with + | nil => + have : counts = [] := by + cases counts with + | nil => rfl + | cons d ds => simp at hlen + subst this + simp [capEdgesAux, HoldsAll] + | cons b bs ih => + cases counts with + | nil => simp [CountsFit] at hfit + | cons d ds => + rcases hfit with ⟨hstartb, hcap, hfitTail⟩ + have hlen' : ds.length = bs.length := by simpa using hlen + have hhead := prefixCum_cons_head hx + have htail := prefixCum_tail hx + have hnonneg : x i - x (i + 1) ≤ 0 := by linarith [hhead] + have hcapEdge : HoldsAll x + (if h : intervalCard (start : WithTop Key) (b : WithTop Key) = ⊤ then [] + else [⟨i, i + 1, ((intervalCard (start : WithTop Key) (b : WithTop Key)).toNat : ℤ)⟩]) := by + by_cases htop : intervalCard (start : WithTop Key) (b : WithTop Key) = ⊤ + · simp [htop, HoldsAll] + · intro e he + simp only [htop, ↓reduceDIte, List.mem_singleton] at he + subst he + simp only [DEdge.Holds] + have hcast : (d : ℕ∞) ≤ intervalCard (start : WithTop Key) (b : WithTop Key) := hcap + have hnat : d ≤ (intervalCard (start : WithTop Key) (b : WithTop Key)).toNat := by + have := ENat.natCast_toNat_eq_self.mpr htop + rw [← this] at hcast + exact_mod_cast hcast + rw [hhead] + exact_mod_cast hnat + intro e he + -- `capEdgesAux start (b :: bs) i` is `([nonneg] ++ caps) ++ tail` + rcases List.mem_append.mp he with hheadList | htailList + · rcases List.mem_append.mp hheadList with hnon | hcapMem + · simp only [List.mem_singleton] at hnon + rw [hnon] + simpa [DEdge.Holds] using hnonneg + · exact hcapEdge e hcapMem + · exact ih hfitTail hlen' htail e htailList + +/-- Conversely, capacity edges produce a capacity-fitting count list. -/ +theorem countsFit_of_capEdgesAux {start : Key} {bounds : List Key} + {counts : List ℕ} {x : ℕ → ℤ} {i : ℕ} + (hle : BoundsLe start bounds) (hlen : counts.length = bounds.length) + (hx : PrefixCum x i counts) (h : HoldsAll x (capEdgesAux start bounds i)) : + CountsFit start bounds counts := by + induction bounds generalizing start counts i with + | nil => + cases counts with + | nil => simp [CountsFit] + | cons d ds => simp at hlen + | cons b bs ih => + cases counts with + | nil => simp at hlen + | cons d ds => + rcases hle with ⟨hstartb, hleTail⟩ + have hlen' : ds.length = bs.length := by simpa using hlen + have hhead := prefixCum_cons_head hx + have htail := prefixCum_tail hx + have hnonnegEdge : (⟨i + 1, i, 0⟩ : DEdge) ∈ capEdgesAux start (b :: bs) i := by + simp [capEdgesAux] + have hnonneg := h _ hnonnegEdge + simp only [DEdge.Holds] at hnonneg + have hd_nonneg : (0 : ℤ) ≤ (d : ℤ) := by linarith [hhead] + have hcap : (d : ℕ∞) ≤ intervalCard (start : WithTop Key) (b : WithTop Key) := by + by_cases htop : intervalCard (start : WithTop Key) (b : WithTop Key) = ⊤ + · rw [htop]; exact le_top + · have hmem : (⟨i, i + 1, + ((intervalCard (start : WithTop Key) (b : WithTop Key)).toNat : ℤ)⟩ : DEdge) ∈ + capEdgesAux start (b :: bs) i := by + simp [capEdgesAux, htop] + have hcapInt := h _ hmem + simp only [DEdge.Holds] at hcapInt + rw [hhead] at hcapInt + have hdNat : d ≤ (intervalCard (start : WithTop Key) (b : WithTop Key)).toNat := by + exact_mod_cast hcapInt + have hcast := ENat.natCast_toNat_eq_self.mpr htop + rw [← hcast] + exact_mod_cast hdNat + refine ⟨hstartb, hcap, ?_⟩ + apply ih hleTail hlen' htail + intro e he + exact h e (List.mem_append.mpr (Or.inr he)) + + +/-- Evaluation of the count environment is the corresponding cumulative variable. -/ +theorem evalCount_eq_countsToX {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} (hlen : counts.length = bounds.length) + (hnoBot : ([] : Key) ∉ bounds) {e : WithTop Key} + (he : EndpointCovered e bounds) : + evalCount bounds counts topCount e = + countsToX bounds.length counts topCount (endpointIndex bounds e) := by + rcases he with rfl | rfl | ⟨k, hk, rfl⟩ + · -- bottom + simp [evalCount, countsToX, endpointIndex, pref] + · -- top + simp [evalCount, countsToX, endpointIndex] + · -- finite nonempty endpoint + by_cases hkbot : k = ([] : Key) + · simp [evalCount, countsToX, endpointIndex, hkbot, pref] + · have hidxlt : bounds.idxOf k < bounds.length := + List.idxOf_lt_length_iff.mpr hk + have hlen' : counts.length = bounds.length := hlen + have hlook := countAtWith_eq_pref (base := 0) hlen' hk + have hne : bounds.idxOf k ≠ bounds.length := by omega + have hnot : k ≠ ([] : Key) := hkbot + simp [evalCount, countsToX, endpointIndex, hkbot, hnot, hne, hlook] + + +/-- The toggled count environment is the untoggled one plus the toggle coefficient, +once the toggle indicator is pinned to the branch value. -/ +theorem evalCountToggled_eq_add_toggleCoeff {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} {k : Key} {stored : Bool} + (hxd : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = (if stored then 1 else 0)) + (e : WithTop Key) : + evalCountToggled bounds counts topCount k e = + evalCount bounds counts topCount e + toggleCoeff stored k e := by + unfold evalCountToggled toggleShift toggleCoeff + by_cases hlt : (k : WithTop Key) < e + · cases stored <;> simp [hlt, hxd] + · simp [hlt] + +/-- The constraint edges hold exactly when the count environment satisfies the +constraint system. -/ +theorem constraintEdges_holds_iff {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} {S : ConstraintSystem} + (hlen : counts.length = bounds.length) {x : ℕ → ℤ} + (hx : ∀ e : WithTop Key, EndpointCovered e bounds → x (endpointIndex bounds e) = + evalCount bounds counts topCount e) + (hcover : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) : + HoldsAll x (constraintEdges bounds S) ↔ SystemHoldsWithCounts S bounds counts topCount := by + unfold constraintEdges SystemHoldsWithCounts + constructor + · intro h c hc + have hlo := hcover c hc + have hedge := h (constraintEdge bounds c.lo c.hi c.bound) (List.mem_map.mpr ⟨c, hc, rfl⟩) + simp only [constraintEdge, DEdge.Holds] at hedge + rw [hx c.hi hlo.2, hx c.lo hlo.1] at hedge + exact hedge + · intro h e he + rw [List.mem_map] at he + obtain ⟨c, hc, rfl⟩ := he + have hlo := hcover c hc + simp only [constraintEdge, DEdge.Holds] + rw [hx c.hi hlo.2, hx c.lo hlo.1] + exact h c hc + +/-- The bad edges hold exactly when every chosen constraint fails after the +toggle. -/ +theorem badEdges_holds_iff {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} {k : Key} {stored : Bool} {bad : List Constraint} + {x : ℕ → ℤ} + (hxd : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = (if stored then 1 else 0)) + (hx : ∀ e : WithTop Key, EndpointCovered e bounds → x (endpointIndex bounds e) = + evalCount bounds counts topCount e) + (hcover : ∀ c ∈ bad, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) : + HoldsAll x (badEdges bounds k stored bad) ↔ + ∀ c ∈ bad, c.bound < + evalCountToggled bounds counts topCount k c.hi - + evalCountToggled bounds counts topCount k c.lo := by + unfold badEdges + constructor + · intro h c hc + have hlo := hcover c hc + have hedge := h (constraintEdge bounds c.hi c.lo + (toggleCoeff stored k c.hi - toggleCoeff stored k c.lo - c.bound - 1)) + (List.mem_map.mpr ⟨c, hc, rfl⟩) + simp only [constraintEdge, DEdge.Holds] at hedge + rw [hx c.hi hlo.2, hx c.lo hlo.1] at hedge + rw [evalCountToggled_eq_add_toggleCoeff hxd c.hi, + evalCountToggled_eq_add_toggleCoeff hxd c.lo] + omega + · intro h e he + rw [List.mem_map] at he + obtain ⟨c, hc, rfl⟩ := he + have hlo := hcover c hc + have hbad := h c hc + rw [evalCountToggled_eq_add_toggleCoeff hxd c.hi, + evalCountToggled_eq_add_toggleCoeff hxd c.lo] at hbad + simp only [constraintEdge, DEdge.Holds] + rw [hx c.hi hlo.2, hx c.lo hlo.1] + omega + + +/-- The interval counts read off from a cumulative assignment. -/ +def diffCounts (x : ℕ → ℤ) : ℕ → List ℕ + | 0 => [] + | n + 1 => diffCounts x n ++ [(x (n + 1) - x n).toNat] + +theorem diffCounts_length (x : ℕ → ℤ) (n : ℕ) : (diffCounts x n).length = n := by + induction n with + | zero => rfl + | succ n ih => simp [diffCounts, ih] + +theorem pref_append_singleton_le {l : List ℕ} {d k : ℕ} (hk : k ≤ l.length) : + pref (l ++ [d]) k = pref l k := by + unfold pref + rw [List.take_append_of_le_length hk] + +theorem pref_append_singleton_self (l : List ℕ) (d : ℕ) : + pref (l ++ [d]) (l.length + 1) = pref l l.length + d := by + unfold pref + have hlen : l.length + 1 = (l ++ [d]).length := by simp + rw [hlen, List.take_length] + simp + +theorem pref_length (l : List ℕ) : pref l l.length = l.sum := by + simp [pref] + +theorem pref_eq_sum_of_length_le {l : List ℕ} {n : ℕ} (h : l.length ≤ n) : + pref l n = l.sum := by + unfold pref + rw [List.take_of_length_le h] + +/-- Differences read off from `x` telescope back to `x`. -/ +theorem prefixCum_diffCounts {x : ℕ → ℤ} (h0 : x 0 = 0) (n : ℕ) + (hnonneg : ∀ i < n, 0 ≤ x (i + 1) - x i) : + PrefixCum x 0 (diffCounts x n) := by + induction n with + | zero => + intro k hk + have hk0 : k = 0 := by simpa [diffCounts] using hk + subst k + simp [PrefixCum, h0, pref] + | succ n ih => + intro k hk + have hk' : k ≤ n + 1 := by simpa [diffCounts_length] using hk + by_cases hkn : k ≤ n + · have hklen : k ≤ (diffCounts x n).length := by + simpa [diffCounts_length] using hkn + rw [diffCounts, pref_append_singleton_le hklen] + exact ih (fun i hi => hnonneg i (by omega)) k + (by simpa [diffCounts_length] using hkn) + · have hkeq : k = n + 1 := by omega + subst k + rw [diffCounts, show n + 1 = (diffCounts x n).length + 1 by + rw [diffCounts_length]] + rw [pref_append_singleton_self, pref_length, diffCounts_length] + have hprev := ih (fun i hi => hnonneg i (by omega)) n + (by simpa [diffCounts_length] using le_refl n) + have hprefsum : pref (diffCounts x n) n = (diffCounts x n).sum := + pref_eq_sum_of_length_le (by rw [diffCounts_length]) + have hprev' : x n - x 0 = ((diffCounts x n).sum : ℤ) := by + simpa only [Nat.zero_add, hprefsum] using hprev + have hd : ((x (n + 1) - x n).toNat : ℤ) = x (n + 1) - x n := by + rw [Int.toNat_of_nonneg (hnonneg n (by omega))] + have hmap : (List.map Nat.cast (diffCounts x n)).sum = + ((diffCounts x n).sum : ℤ) := + (map_list_sum Int.ofNatHom (diffCounts x n)).symm + push_cast + rw [hmap, hd] + simp only [Nat.zero_add] + linarith + +/-- The cumulative assignment reconstructed from its differences agrees with +`countsToX`. -/ +theorem eq_countsToX_of_prefixCum {x : ℕ → ℤ} {counts : List ℕ} {n topCount : ℕ} + (h0 : x 0 = 0) (hlen : counts.length = n) + (hpref : PrefixCum x 0 counts) + (htop : topCount = (x (n + 1) - x n).toNat) + (hnonneg : 0 ≤ x (n + 1) - x n) : + ∀ i ≤ n + 1, x i = countsToX n counts topCount i := by + intro i hi + by_cases hin : i = n + 1 + · subst i + simp only [countsToX, if_pos rfl] + have hsum : x n = (pref counts n : ℤ) := by + have := hpref n (by omega) + simpa [h0] using this + have hsum' : x n = (pref counts counts.length : ℤ) := by + simpa [← hlen] using hsum + have hsumsum : (counts.sum : ℤ) = x n := by + rw [pref_length] at hsum' + exact hsum'.symm + have hd : ((x (n + 1) - x n).toNat : ℤ) = x (n + 1) - x n := + Int.toNat_of_nonneg hnonneg + have hmap : (List.map Nat.cast counts).sum = (counts.sum : ℤ) := + (map_list_sum Int.ofNatHom counts).symm + rw [htop] + push_cast + rw [hmap, hsumsum, hd] + ring + · have hi' : i ≤ n := by omega + have hne : i ≠ n + 1 := hin + simp only [countsToX, if_neg hne] + have := hpref i (by omega) + simpa [h0] using this + + +/-- The branch equality edges say exactly that the toggle indicator is the chosen +branch value. -/ +theorem branchEqEdges_holds_iff {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} {k : Key} {stored : Bool} {x : ℕ → ℤ} + (hx : ∀ e : WithTop Key, EndpointCovered e bounds → x (endpointIndex bounds e) = + evalCount bounds counts topCount e) + (hk : k = ([] : Key) ∨ k ∈ bounds) + (hka : keyAfter k ∈ bounds) : + HoldsAll x (branchEqEdges bounds k stored) ↔ + evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = (if stored then 1 else 0) := by + have hcovK : EndpointCovered (k : WithTop Key) bounds := by + rcases hk with rfl | hk + · exact Or.inl rfl + · exact Or.inr (Or.inr ⟨k, hk, rfl⟩) + have hcovKa : EndpointCovered ((keyAfter k : Key) : WithTop Key) bounds := + Or.inr (Or.inr ⟨keyAfter k, hka, rfl⟩) + have hxi : x (endpointIndex bounds (k : WithTop Key)) = + evalCount bounds counts topCount (k : WithTop Key) := hx _ hcovK + have hxj : x (endpointIndex bounds ((keyAfter k : Key) : WithTop Key)) = + evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) := hx _ hcovKa + unfold branchEqEdges + cases stored + · change HoldsAll x + [⟨endpointIndex bounds (k : WithTop Key), + endpointIndex bounds ((keyAfter k : Key) : WithTop Key), 0⟩, + ⟨endpointIndex bounds ((keyAfter k : Key) : WithTop Key), + endpointIndex bounds (k : WithTop Key), 0⟩] ↔ + evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = 0 + constructor + · intro h + have h1 := h ⟨endpointIndex bounds (k : WithTop Key), + endpointIndex bounds ((keyAfter k : Key) : WithTop Key), 0⟩ (by simp) + have h2 := h ⟨endpointIndex bounds ((keyAfter k : Key) : WithTop Key), + endpointIndex bounds (k : WithTop Key), 0⟩ (by simp) + simp only [DEdge.Holds] at h1 h2 + rw [hxi, hxj] at h1 h2 + linarith + · intro h + intro e he + simp only [List.mem_cons, List.not_mem_nil, or_false] at he + rcases he with rfl | rfl + · simp only [DEdge.Holds] + rw [hxi, hxj] + linarith + · simp only [DEdge.Holds] + rw [hxi, hxj] + linarith + · change HoldsAll x + [⟨endpointIndex bounds (k : WithTop Key), + endpointIndex bounds ((keyAfter k : Key) : WithTop Key), 1⟩, + ⟨endpointIndex bounds ((keyAfter k : Key) : WithTop Key), + endpointIndex bounds (k : WithTop Key), -1⟩] ↔ + evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = 1 + constructor + · intro h + have h1 := h ⟨endpointIndex bounds (k : WithTop Key), + endpointIndex bounds ((keyAfter k : Key) : WithTop Key), 1⟩ (by simp) + have h2 := h ⟨endpointIndex bounds ((keyAfter k : Key) : WithTop Key), + endpointIndex bounds (k : WithTop Key), -1⟩ (by simp) + simp only [DEdge.Holds] at h1 h2 + rw [hxi, hxj] at h1 h2 + linarith + · intro h + intro e he + simp only [List.mem_cons, List.not_mem_nil, or_false] at he + rcases he with rfl | rfl + · simp only [DEdge.Holds] + rw [hxi, hxj] + linarith + · simp only [DEdge.Holds] + rw [hxi, hxj] + linarith + +/-- Every nonnegativity edge of the capacity system is present. -/ +theorem nonnegEdge_mem_capEdgesAux (start : Key) (bounds : List Key) (i j : ℕ) + (hj : j < bounds.length) : + (⟨i + j + 1, i + j, 0⟩ : DEdge) ∈ capEdgesAux start bounds i := by + induction bounds generalizing start i j with + | nil => simp at hj + | cons b bs ih => + cases j with + | zero => + simp [capEdgesAux] + | succ j' => + have hj' : j' < bs.length := by simpa using hj + have hmem := ih b (i + 1) j' hj' + simp only [capEdgesAux, List.mem_append] + right + have h1 : i + (j' + 1) = i + 1 + j' := by omega + have h2 : i + (j' + 1) + 1 = i + 1 + j' + 1 := by omega + simpa [h1, h2] using hmem + + +/-- One branch of the finite count relaxation is feasible exactly when its +difference system is. -/ +theorem countFeasibleBranch_iff_diffFeasible + (bounds : List Key) (k : Key) (stored : Bool) + (S : ConstraintSystem) (bad : List Constraint) + (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds) + (hcoverS : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hcoverBad : ∀ c ∈ bad, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) : + CountFeasibleBranch bounds k stored S bad ↔ + DiffFeasible [] (countDiffEdges bounds k stored S bad) := by + classical + constructor + · rintro ⟨counts, topCount, hfit, hS, hxd, hbad⟩ + have hlen : counts.length = bounds.length := length_eq_of_countsFit hfit + let x : ℕ → ℤ := countsToX bounds.length counts topCount + have hxEval : ∀ e : WithTop Key, EndpointCovered e bounds → + x (endpointIndex bounds e) = evalCount bounds counts topCount e := by + intro e he + exact (evalCount_eq_countsToX hlen hnoBot he).symm + have hpref : PrefixCum x 0 counts := + prefixCum_countsToX bounds.length counts topCount hlen + have hcapAux : HoldsAll x (capEdgesAux ([] : Key) bounds 0) := + capEdgesAux_holds_of_countsFit hfit hlen hpref + have htopEdge : HoldsAll x [⟨bounds.length + 1, bounds.length, 0⟩] := by + intro e he + simp only [List.mem_singleton] at he + rw [he] + simp only [DEdge.Holds] + have hxn : x bounds.length = (counts.sum : ℤ) := by + dsimp [x] + rw [countsToX, if_neg (by omega)] + rw [pref_eq_sum_of_length_le (l := counts) (n := bounds.length) (by omega)] + have hxt : x (bounds.length + 1) = ((counts.sum + topCount : ℕ) : ℤ) := by + dsimp [x] + rw [countsToX, if_pos rfl] + push_cast + ring + rw [hxn, hxt] + push_cast + omega + have hcap : HoldsAll x (capEdges bounds) := by + intro e he + rw [capEdges, List.mem_append] at he + rcases he with he | he + · exact hcapAux e he + · exact htopEdge e he + have hcon : HoldsAll x (constraintEdges bounds S) := + (constraintEdges_holds_iff hlen hxEval hcoverS).mpr hS + have hbadH : HoldsAll x (badEdges bounds k stored bad) := + (badEdges_holds_iff hxd hxEval hcoverBad).mpr hbad + have hbr : HoldsAll x (branchEqEdges bounds k stored) := + (branchEqEdges_holds_iff hxEval hk hka).mpr hxd + refine ⟨x, by simp [HoldsConsts], ?_⟩ + intro e he + simp only [countDiffEdges, List.mem_append] at he + rcases he with ((he | he) | he) | he + · exact hcap e he + · exact hcon e he + · exact hbadH e he + · exact hbr e he + · rintro ⟨x, -, hx⟩ + let y : ℕ → ℤ := fun i => x i - x 0 + have hy : HoldsAll y (countDiffEdges bounds k stored S bad) := by + intro e he + have hx' := hx e he + simp only [DEdge.Holds] at hx' ⊢ + dsimp [y] + linarith + have hcap : HoldsAll y (capEdges bounds) := by + intro e he + apply hy e + simp only [countDiffEdges, List.mem_append] + exact Or.inl (Or.inl (Or.inl he)) + have hcon : HoldsAll y (constraintEdges bounds S) := by + intro e he + apply hy e + simp only [countDiffEdges, List.mem_append] + exact Or.inl (Or.inl (Or.inr he)) + have hbadH : HoldsAll y (badEdges bounds k stored bad) := by + intro e he + apply hy e + simp only [countDiffEdges, List.mem_append] + exact Or.inl (Or.inr he) + have hbr : HoldsAll y (branchEqEdges bounds k stored) := by + intro e he + apply hy e + simp only [countDiffEdges, List.mem_append] + exact Or.inr he + have hcapAux : HoldsAll y (capEdgesAux ([] : Key) bounds 0) := by + intro e he + exact hcap e (List.mem_append.mpr (Or.inl he)) + have hnonneg : ∀ i < bounds.length, 0 ≤ y (i + 1) - y i := by + intro i hi + have hmem := nonnegEdge_mem_capEdgesAux ([] : Key) bounds 0 i hi + have hmem' : (⟨i + 1, i, 0⟩ : DEdge) ∈ capEdges bounds := + List.mem_append.mpr (Or.inl (by simpa using hmem)) + have := hcap _ hmem' + simpa [DEdge.Holds] using this + have htopNonneg : 0 ≤ y (bounds.length + 1) - y bounds.length := by + have hmem : (⟨bounds.length + 1, bounds.length, 0⟩ : DEdge) ∈ capEdges bounds := by + simp [capEdges] + have := hcap _ hmem + simpa [DEdge.Holds] using this + let counts : List ℕ := diffCounts y bounds.length + let topCount : ℕ := (y (bounds.length + 1) - y bounds.length).toNat + have hlen : counts.length = bounds.length := diffCounts_length y bounds.length + have h0 : y 0 = 0 := by simp [y] + have hpref : PrefixCum y 0 counts := + prefixCum_diffCounts h0 bounds.length hnonneg + have hfit : CountsFit ([] : Key) bounds counts := + countsFit_of_capEdgesAux hle hlen hpref hcapAux + have htopEq : topCount = (y (bounds.length + 1) - y bounds.length).toNat := rfl + have hyAgree : ∀ i ≤ bounds.length + 1, + y i = countsToX bounds.length counts topCount i := + eq_countsToX_of_prefixCum h0 hlen hpref htopEq htopNonneg + have hxEval : ∀ e : WithTop Key, EndpointCovered e bounds → + y (endpointIndex bounds e) = evalCount bounds counts topCount e := by + intro e he + have hidx : endpointIndex bounds e ≤ bounds.length + 1 := by + rcases he with rfl | rfl | ⟨a, ha, rfl⟩ + · simp [endpointIndex] + · simp [endpointIndex] + · have ha' : a ≠ ([] : Key) := by + intro hbot + exact hnoBot (hbot ▸ ha) + have hlt : bounds.idxOf a < bounds.length := List.idxOf_lt_length_iff.mpr ha + simp only [endpointIndex, ha', if_false] + omega + rw [hyAgree _ hidx] + exact (evalCount_eq_countsToX hlen hnoBot he).symm + have hS : SystemHoldsWithCounts S bounds counts topCount := + (constraintEdges_holds_iff hlen hxEval hcoverS).mp hcon + have hxd : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) - + evalCount bounds counts topCount (k : WithTop Key) = (if stored then 1 else 0) := + (branchEqEdges_holds_iff hxEval hk hka).mp hbr + have hbad : ∀ c ∈ bad, c.bound < + evalCountToggled bounds counts topCount k c.hi - + evalCountToggled bounds counts topCount k c.lo := + (badEdges_holds_iff hxd hxEval hcoverBad).mp hbadH + exact ⟨counts, topCount, hfit, hS, hxd, hbad⟩ + + +/-- The unbranched finite-count feasibility predicate appearing in +`flipWitness_iff_exists_counts_canonical`. -/ +def CountFeasible (bounds : List Key) (k : Key) + (S : ConstraintSystem) (bad : List Constraint) : Prop := + ∃ counts : List ℕ, ∃ topCount : ℕ, + CountsFit ([] : Key) bounds counts ∧ + SystemHoldsWithCounts S bounds counts topCount ∧ + ∀ c ∈ bad, c.bound < + evalCountToggled bounds counts topCount k c.hi - + evalCountToggled bounds counts topCount k c.lo + +/-- The unbranched count predicate is the disjunction of the two toggle-indicator +branches. -/ +theorem countFeasible_iff_branch_or + (bounds : List Key) (k : Key) (S : ConstraintSystem) (bad : List Constraint) + (hnoBot : ([] : Key) ∉ bounds) + (hcoverS : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hcoverBad : ∀ c ∈ bad, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) : + CountFeasible bounds k S bad ↔ + CountFeasibleBranch bounds k true S bad ∨ + CountFeasibleBranch bounds k false S bad := by + constructor + · rintro ⟨counts, topCount, hfit, hS, hbad⟩ + obtain ⟨db, hcountsAt, htop⟩ := exists_db_realizing_counts bounds counts topCount hfit + have hcovK : EndpointCovered (k : WithTop Key) bounds := by + rcases hk with rfl | hk + · exact Or.inl rfl + · exact Or.inr (Or.inr ⟨k, hk, rfl⟩) + have hcovKa : EndpointCovered ((keyAfter k : Key) : WithTop Key) bounds := + Or.inr (Or.inr ⟨keyAfter k, hka, rfl⟩) + have hkEval : evalCount bounds counts topCount (k : WithTop Key) = cnt db (k : WithTop Key) := + evalCount_eq_cnt_of_covered hcountsAt htop hnoBot hcovK + have hkaEval : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) = + cnt db ((keyAfter k : Key) : WithTop Key) := + evalCount_eq_cnt_of_covered hcountsAt htop hnoBot hcovKa + by_cases hmem : k ∈ db.keys + · refine Or.inl ⟨counts, topCount, hfit, hS, ?_, hbad⟩ + rw [hkEval, hkaEval] + simpa [hmem] using cnt_keyAfter_sub_cnt db k + · refine Or.inr ⟨counts, topCount, hfit, hS, ?_, hbad⟩ + rw [hkEval, hkaEval] + simpa [hmem] using cnt_keyAfter_sub_cnt db k + · rintro (⟨counts, topCount, hfit, hS, -, hbad⟩ | + ⟨counts, topCount, hfit, hS, -, hbad⟩) + · exact ⟨counts, topCount, hfit, hS, hbad⟩ + · exact ⟨counts, topCount, hfit, hS, hbad⟩ + +/-- Computable decision of the unbranched finite-count feasibility. -/ +def countFeasibleBool (bounds : List Key) (k : Key) + (S : ConstraintSystem) (bad : List Constraint) : Bool := + diffFeasibleBoolAuto (countDiffEdges bounds k false S bad) || + diffFeasibleBoolAuto (countDiffEdges bounds k true S bad) + +theorem countFeasibleBool_spec (bounds : List Key) (k : Key) + (S : ConstraintSystem) (bad : List Constraint) + (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds) + (hcoverS : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hcoverBad : ∀ c ∈ bad, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) + (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) : + countFeasibleBool bounds k S bad = true ↔ CountFeasible bounds k S bad := by + have hfalse := countFeasibleBranch_iff_diffFeasible bounds k false S bad + hnoBot hle hcoverS hcoverBad hk hka + have htrue := countFeasibleBranch_iff_diffFeasible bounds k true S bad + hnoBot hle hcoverS hcoverBad hk hka + rw [countFeasibleBool, Bool.or_eq_true, diffFeasibleBoolAuto_spec, + diffFeasibleBoolAuto_spec, ← hfalse, ← htrue] + rw [countFeasible_iff_branch_or bounds k S bad hnoBot hcoverS hcoverBad hk hka] + tauto + + +/-- All finite choices of one constraint from each alternative of a DNF. -/ +def choices : ConstraintDNF → List (List Constraint) + | [] => [[]] + | S :: Ss => S.flatMap fun c => (choices Ss).map fun bs => c :: bs + +theorem mem_choices {D : ConstraintDNF} {bad : List Constraint} : + bad ∈ choices D ↔ ChoosesAll bad D := by + induction D generalizing bad with + | nil => + cases bad <;> simp [choices, ChoosesAll] + | cons S Ss ih => + cases bad with + | nil => + simp [choices, ChoosesAll] + | cons c cs => + simp only [choices, List.mem_flatMap, List.mem_map, ChoosesAll, ih] + constructor + · rintro ⟨a, ha, bs, hbs, heq⟩ + cases heq + exact ⟨ha, hbs⟩ + · rintro ⟨ha, hcs⟩ + exact ⟨c, ha, cs, hcs, rfl⟩ + + +/-- Computable version of `RangeRead.withinLimit`. -/ +def withinLimitBool (r : RangeRead) (ks : List Key) : Bool := + match r.limit with + | none => true + | some n => decide (ks.length ≤ (n : ℕ)) + +theorem withinLimitBool_eq (r : RangeRead) (ks : List Key) : + withinLimitBool r ks = true ↔ RangeRead.withinLimit r ks := by + cases h : r.limit with + | none => simp [withinLimitBool, RangeRead.withinLimit, h] + | some n => simp [withinLimitBool, RangeRead.withinLimit, h] + +/-- The computable activity oracle. It enumerates the DNF alternatives and the +finite choices of one violated constraint per alternative, then invokes the +difference-constraint decider for the canonical endpoint list `activeBounds`. -/ +def activeComputable (r : RangeRead) (res : Result) (k : Key) : Bool := + let D := characterizingDNF r res + let bounds := activeBounds D k + withinLimitBool r res.keys && + D.any fun S => (choices D).any fun bad => countFeasibleBool bounds k S bad + +/-- The computable activity oracle is exact: it returns `true` exactly for the +one-key flip witnesses. This closes the algorithmic interface left open in +`General.lean`. -/ +theorem activeComputable_spec {r : RangeRead} {res : Result} + (hread : Readable r res) : + ActiveDecider r res (activeComputable r res) := by + intro k + let D := characterizingDNF r res + let bounds := activeBounds D k + have hnoBot : ([] : Key) ∉ bounds := not_mem_activeBounds_bot D k + have hle : BoundsLe ([] : Key) bounds := activeBounds_boundsLe D k + have hcoverD : ∀ S ∈ D, ∀ c ∈ S, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro S hS c hc + exact dnfEndpointCovered_activeBounds hS hc + have hk : k = ([] : Key) ∨ k ∈ bounds := by + by_cases hkbot : k = ([] : Key) + · exact Or.inl hkbot + · exact Or.inr (by + rw [mem_activeBounds] + exact Or.inr (Or.inr ⟨hkbot, rfl⟩)) + have hka : keyAfter k ∈ bounds := by + rw [mem_activeBounds] + exact Or.inl rfl + rw [activeComputable] + rw [Bool.and_eq_true, withinLimitBool_eq, List.any_eq_true] + constructor + · rintro ⟨hw, S, hS, hchoiceTrue⟩ + rw [List.any_eq_true] at hchoiceTrue + obtain ⟨bad, hbadMem, hfeas⟩ := hchoiceTrue + have hchooses : ChoosesAll bad D := mem_choices.mp hbadMem + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc + exact hcoverD S' hS' c hcS' + have hcount : CountFeasible bounds k S bad := + (countFeasibleBool_spec bounds k S bad hnoBot hle (hcoverD S hS) hcoverBad hk hka).mp + hfeas + exact (flipWitness_iff_exists_counts_canonical (D := D) + (characterizesDNF_characterizingDNF hread)).mpr + ⟨hw, S, hS, bad, hchooses, hcount⟩ + · intro hw + rcases (flipWitness_iff_exists_counts_canonical (D := D) + (characterizesDNF_characterizingDNF hread)).mp hw with + ⟨hwithin, S, hS, bad, hchooses, hcount⟩ + refine ⟨hwithin, ⟨S, hS, ?_⟩⟩ + rw [List.any_eq_true] + refine ⟨bad, mem_choices.mpr hchooses, ?_⟩ + have hcoverBad : ∀ c ∈ bad, + EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by + intro c hc + obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc + exact hcoverD S' hS' c hcS' + exact (countFeasibleBool_spec bounds k S bad hnoBot hle (hcoverD S hS) hcoverBad hk hka).mpr + hcount + + +/-- The cellwise algorithm with the computable oracle is minimal on every readable +pair. -/ +theorem conflictRangeCellwise_minimal_computable {r : RangeRead} {res : Result} + (hread : Readable r res) : + Minimal r res (conflictRangeCellwise r res (activeComputable r res)) := + conflictRangeCellwise_minimal (activeComputable_spec hread) hread + +/-- The computable-oracle cellwise algorithm computes exactly the necessary set. -/ +theorem conflictRangeCellwise_toSet_eq_necessarySet_computable + {r : RangeRead} {res : Result} (hread : Readable r res) : + (conflictRangeCellwise r res (activeComputable r res)).toSet = necessarySet r res := + conflictRangeCellwise_toSet_eq_necessarySet (activeComputable_spec hread) hread + + +end FormalConflictRange diff --git a/FormalConflictRange/General.lean b/FormalConflictRange/General.lean new file mode 100644 index 0000000..e57d935 --- /dev/null +++ b/FormalConflictRange/General.lean @@ -0,0 +1,1196 @@ +/- +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.Prototype + +/-! +# A general flip-witness proof of the headline + +This file develops the cell/flip-witness route to `exists_minimal_conflict_range`. +The key observation is that the finite decision procedure in the prototype is not +needed for the proof: the active cells can be defined directly by the existence of a +one-key toggle witness. The only combinatorial input is that toggling two keys in the +same endpoint cell has the same effect on every collected constraint. +-/ + +namespace FormalConflictRange + +open RangeRead + +/-! ## Endpoints and cells -/ + +/-- The endpoints occurring in a constraint. -/ +def Constraint.endpoints (c : Constraint) : List (WithTop Key) := [c.lo, c.hi] + +/-- All endpoints occurring in a disjunctive system. -/ +def dnfEndpoints (D : ConstraintDNF) : Finset (WithTop Key) := + (D.flatMap fun S => S.flatMap fun c => c.endpoints).toFinset + +/-- The endpoint set used for cells, with explicit bottom and top sentinels. -/ +def cellEndpoints (D : ConstraintDNF) : Finset (WithTop Key) := + insert (([] : Key) : WithTop Key) (insert ⊤ (dnfEndpoints D)) + +theorem mem_dnfEndpoints_lo {D : ConstraintDNF} {S : ConstraintSystem} {c : Constraint} + (hS : S ∈ D) (hc : c ∈ S) : c.lo ∈ dnfEndpoints D := by + rw [dnfEndpoints, List.mem_toFinset, List.mem_flatMap] + refine ⟨S, hS, ?_⟩ + rw [List.mem_flatMap] + exact ⟨c, hc, by simp [Constraint.endpoints]⟩ + +theorem mem_dnfEndpoints_hi {D : ConstraintDNF} {S : ConstraintSystem} {c : Constraint} + (hS : S ∈ D) (hc : c ∈ S) : c.hi ∈ dnfEndpoints D := by + rw [dnfEndpoints, List.mem_toFinset, List.mem_flatMap] + refine ⟨S, hS, ?_⟩ + rw [List.mem_flatMap] + exact ⟨c, hc, by simp [Constraint.endpoints]⟩ + +theorem dnfEndpoints_subset_cellEndpoints (D : ConstraintDNF) : + dnfEndpoints D ⊆ cellEndpoints D := by + intro e he + exact Finset.mem_insert_of_mem (Finset.mem_insert_of_mem he) + +/-- Two keys lie in the same cell when every endpoint separates them the same way. -/ +def SameCell (E : Finset (WithTop Key)) (k k' : Key) : Prop := + ∀ e ∈ E, ((k : WithTop Key) < e ↔ (k' : WithTop Key) < e) + +theorem SameCell.refl (E : Finset (WithTop Key)) (k : Key) : SameCell E k k := + fun _ _ => Iff.rfl + +theorem SameCell.symm {E : Finset (WithTop Key)} {k k' : Key} (h : SameCell E k k') : + SameCell E k' k := + fun e he => (h e he).symm + +/-! ## Moving one key inside a cell -/ + +/-- Replace a stored key `old` by an absent key `new`, preserving sortedness. -/ +def DB.move (db : DB) (old new : Key) (_hold : old ∈ db.keys) (hnew : new ∉ db.keys) : DB := + (db.erase old).insert new (by + rw [DB.mem_erase_iff] + rintro ⟨hne, hmem⟩ + exact hnew hmem) + +/-- The keys of a moved database are obtained by erasing the old key and inserting +the new one. -/ +@[simp] theorem DB.move_keys (db : DB) (old new : Key) (hold : old ∈ db.keys) + (hnew : new ∉ db.keys) : + (db.move old new hold hnew).keys = (db.erase old).keys.orderedInsert (· ≤ ·) new := rfl + +/-- Moving one key within a cell preserves every endpoint count. -/ +theorem cnt_move_eq_of_sameCell {E : Finset (WithTop Key)} {db : DB} {old new : Key} + (hsame : SameCell E old new) (hold : old ∈ db.keys) (hnew : new ∉ db.keys) + {e : WithTop Key} (he : e ∈ E) : + cnt (db.move old new hold hnew) e = cnt db e := by + have hnew' : new ∉ (db.erase old).keys := by + rw [DB.mem_erase_iff] + rintro ⟨hne, hmem⟩ + exact hnew hmem + have hins := cnt_insert_eq (db.erase old) new hnew' e + have herase := cnt_erase_eq db old hold e + have hlt := hsame e he + have hnat : (cnt (db.move old new hold hnew) e : ℤ) = (cnt db e : ℤ) := by + rw [DB.move, hins, herase] + by_cases h : (old : WithTop Key) < e + · have h' : (new : WithTop Key) < e := hlt.mp h + simp [h, h'] + · have h' : ¬ (new : WithTop Key) < e := fun hn => h (hlt.mpr hn) + simp [h, h'] + exact_mod_cast hnat + +/-- Erasing either of two stored keys in the same cell gives the same endpoint +counts. -/ +theorem cnt_erase_eq_of_sameCell {E : Finset (WithTop Key)} {db : DB} {k k' : Key} + (hsame : SameCell E k k') (hk : k ∈ db.keys) (hk' : k' ∈ db.keys) + {e : WithTop Key} (he : e ∈ E) : + cnt (db.erase k) e = cnt (db.erase k') e := by + have hkE := cnt_erase_eq db k hk e + have hk'E := cnt_erase_eq db k' hk' e + have hlt := hsame e he + have hnat : (cnt (db.erase k) e : ℤ) = (cnt (db.erase k') e : ℤ) := by + rw [hkE, hk'E] + by_cases h : (k : WithTop Key) < e + · have h' : (k' : WithTop Key) < e := hlt.mp h + simp [h, h'] + · have h' : ¬ (k' : WithTop Key) < e := fun hn => h (hlt.mpr hn) + simp [h, h'] + exact_mod_cast hnat + +/-- Inserting either of two absent keys in the same cell gives the same endpoint +counts. -/ +theorem cnt_insert_eq_of_sameCell {E : Finset (WithTop Key)} {db : DB} {k k' : Key} + (hsame : SameCell E k k') (hk : k ∉ db.keys) (hk' : k' ∉ db.keys) + {e : WithTop Key} (he : e ∈ E) : + cnt (db.insert k hk) e = cnt (db.insert k' hk') e := by + have hkE := cnt_insert_eq db k hk e + have hk'E := cnt_insert_eq db k' hk' e + have hlt := hsame e he + have hnat : (cnt (db.insert k hk) e : ℤ) = (cnt (db.insert k' hk') e : ℤ) := by + rw [hkE, hk'E] + by_cases h : (k : WithTop Key) < e + · have h' : (k' : WithTop Key) < e := hlt.mp h + simp [h, h'] + · have h' : ¬ (k' : WithTop Key) < e := fun hn => h (hlt.mpr hn) + simp [h, h'] + exact_mod_cast hnat + +/-- Two databases with the same endpoint counts satisfy the same constraints. -/ +theorem eval_eq_of_agree_endpoints {D : ConstraintDNF} {db₁ db₂ : DB} + (h : ∀ e ∈ dnfEndpoints D, cnt db₁ e = cnt db₂ e) : + ∀ S ∈ D, ∀ c ∈ S, c.eval db₁ = c.eval db₂ := by + intro S hS c hc + have hlo := h c.lo (mem_dnfEndpoints_lo hS hc) + have hhi := h c.hi (mem_dnfEndpoints_hi hS hc) + simp [Constraint.eval, hlo, hhi] + +/-- Equal endpoint counts give the same truth value for every alternative. -/ +theorem systemHoldsAt_congr_endpoints {D : ConstraintDNF} {db₁ db₂ : DB} + (h : ∀ e ∈ dnfEndpoints D, cnt db₁ e = cnt db₂ e) + {S : ConstraintSystem} (hS : S ∈ D) : + SystemHoldsAt S db₁ ↔ SystemHoldsAt S db₂ := by + have heval := eval_eq_of_agree_endpoints h S hS + constructor + · intro hh c hc + change c.eval db₂ ≤ c.bound + rw [← heval c hc] + exact hh c hc + · intro hh c hc + change c.eval db₁ ≤ c.bound + rw [heval c hc] + exact hh c hc + +/-- Equal endpoint counts give the same disjunction. -/ +theorem anyHolds_congr_endpoints {D : ConstraintDNF} {db₁ db₂ : DB} + (h : ∀ e ∈ dnfEndpoints D, cnt db₁ e = cnt db₂ e) : + AnyHolds D db₁ ↔ AnyHolds D db₂ := by + constructor + · rintro ⟨S, hS, hh⟩ + exact ⟨S, hS, (systemHoldsAt_congr_endpoints h hS).mp hh⟩ + · rintro ⟨S, hS, hh⟩ + exact ⟨S, hS, (systemHoldsAt_congr_endpoints h hS).mpr hh⟩ + +/-- A move inside a cell preserves the disjunction. -/ +theorem anyHolds_move_of_sameCell {D : ConstraintDNF} {E : Finset (WithTop Key)} + {db : DB} {old new : Key} + (hE : dnfEndpoints D ⊆ E) (hsame : SameCell E old new) + (hold : old ∈ db.keys) (hnew : new ∉ db.keys) : + AnyHolds D (db.move old new hold hnew) ↔ AnyHolds D db := by + apply anyHolds_congr_endpoints + intro e he + exact cnt_move_eq_of_sameCell hsame hold hnew (hE he) + +/-- Erasing either of two stored keys in the same cell preserves the disjunction. -/ +theorem anyHolds_erase_of_sameCell {D : ConstraintDNF} {E : Finset (WithTop Key)} + {db : DB} {k k' : Key} + (hE : dnfEndpoints D ⊆ E) (hsame : SameCell E k k') + (hk : k ∈ db.keys) (hk' : k' ∈ db.keys) : + AnyHolds D (db.erase k) ↔ AnyHolds D (db.erase k') := by + apply anyHolds_congr_endpoints + intro e he + exact cnt_erase_eq_of_sameCell hsame hk hk' (hE he) + +/-- Inserting either of two absent keys in the same cell preserves the disjunction. -/ +theorem anyHolds_insert_of_sameCell {D : ConstraintDNF} {E : Finset (WithTop Key)} + {db : DB} {k k' : Key} + (hE : dnfEndpoints D ⊆ E) (hsame : SameCell E k k') + (hk : k ∉ db.keys) (hk' : k' ∉ db.keys) : + AnyHolds D (db.insert k hk) ↔ AnyHolds D (db.insert k' hk') := by + apply anyHolds_congr_endpoints + intro e he + exact cnt_insert_eq_of_sameCell hsame hk hk' (hE he) + +/-! ## Flip witnesses -/ + +/-- A one-key toggle witness: a compatible database whose flip at `k` is not +compatible. -/ +def FlipWitness (r : RangeRead) (res : Result) (k : Key) : Prop := + ∃ db : DB, RangeRead.Compatible db r res ∧ + ¬ RangeRead.Compatible (toggle db k) r res + +/-- Flipping a stored key erases it. -/ +theorem toggle_eq_erase_of_mem {db : DB} {k : Key} (hk : k ∈ db.keys) : + toggle db k = db.erase k := by + simp [toggle, hk] + +/-- Flipping an absent key inserts it. -/ +theorem toggle_eq_insert_of_not_mem {db : DB} {k : Key} (hk : k ∉ db.keys) : + toggle db k = db.insert k hk := by + simp [toggle, hk] + +/-- Every reported key has a flip witness: in any compatible database it is +stored, and erasing it breaks compatibility. This exposes the witness that +`necessary_of_mem_result` constructs internally, so the cell machinery can use +reported keys as active representatives. -/ +theorem flipWitness_of_mem_result {r : RangeRead} {res : Result} + (hread : Readable r res) {k : Key} (hk : k ∈ res.keys) : + FlipWitness r res k := by + obtain ⟨db₀, hcomp⟩ := hread + have hkdb : k ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp hk + refine ⟨db₀, hcomp, ?_⟩ + rw [toggle_eq_erase_of_mem hkdb] + intro hcompErase + exact DB.not_mem_erase_self db₀ k + (RangeRead.compatible_keys_subset hcompErase hk) + + +/-- Under a characterizing DNF, a flip witness is exactly a compatible database +whose flip fails the disjunction. -/ +theorem flipWitness_iff_exists_not_anyHolds {D : ConstraintDNF} {r : RangeRead} + {res : Result} (hchar : CharacterizesDNF D r res) {k : Key} : + FlipWitness r res k ↔ + ∃ db : DB, RangeRead.Compatible db r res ∧ ¬ AnyHolds D (toggle db k) := by + constructor + · rintro ⟨db, hcomp, hnot⟩ + refine ⟨db, hcomp, fun hany => hnot ?_⟩ + exact (hchar (toggle db k)).mpr ⟨hcomp.1, hany⟩ + · rintro ⟨db, hcomp, hnot⟩ + refine ⟨db, hcomp, fun hcomp' => hnot ?_⟩ + exact ((hchar (toggle db k)).mp hcomp').2 + +/-- Erasing a moved stored key recovers the original erased database's counts. -/ +theorem cnt_erase_move_eq_of_sameCell {E : Finset (WithTop Key)} {db : DB} {k k' : Key} + (hsame : SameCell E k k') (hk : k ∈ db.keys) (hk' : k' ∉ db.keys) + {e : WithTop Key} (he : e ∈ E) : + cnt ((db.move k k' hk hk').erase k') e = cnt (db.erase k) e := by + have hk'_db' : k' ∈ (db.move k k' hk hk').keys := by + unfold DB.move + exact DB.mem_insert_self (db.erase k) k' _ + have h1 := cnt_erase_eq (db.move k k' hk hk') k' hk'_db' e + have h2 := cnt_erase_eq db k hk e + have h3 := cnt_move_eq_of_sameCell hsame hk hk' he + have hlt := hsame e he + have hnat : (cnt ((db.move k k' hk hk').erase k') e : ℤ) = (cnt (db.erase k) e : ℤ) := by + rw [h1, h2, h3] + by_cases h : (k : WithTop Key) < e + · have h' : (k' : WithTop Key) < e := hlt.mp h + simp [h, h'] + · have h' : ¬ (k' : WithTop Key) < e := fun hn => h (hlt.mpr hn) + simp [h, h'] + exact_mod_cast hnat + +/-- The key moved into the old key's place is absent from the moved database. -/ +theorem not_mem_move_of_mem_not_mem {db : DB} {k k' : Key} (hk : k ∉ db.keys) + (hk' : k' ∈ db.keys) : k' ∉ (db.move k' k hk' hk).keys := by + unfold DB.move + rw [DB.insert_keys, List.mem_orderedInsert] + rintro (heq | hmem) + · exact hk (heq ▸ hk') + · exact (DB.mem_erase_iff.mp hmem).1 rfl + +/-- Inserting an absent key after moving a stored key to its place gives the same +counts as inserting the originally absent key. -/ +theorem cnt_insert_move_eq_of_sameCell {E : Finset (WithTop Key)} {db : DB} {k k' : Key} + (hsame : SameCell E k k') (hk : k ∉ db.keys) (hk' : k' ∈ db.keys) + (hins : k' ∉ (db.move k' k hk' hk).keys) + {e : WithTop Key} (he : e ∈ E) : + cnt ((db.move k' k hk' hk).insert k' hins) e = cnt (db.insert k hk) e := by + have hne : k' ≠ k := fun heq => hk (heq ▸ hk') + have hk'_db' : k' ∉ (db.move k' k hk' hk).keys := hins + have h1 := cnt_insert_eq (db.move k' k hk' hk) k' hins e + have h2 := cnt_insert_eq db k hk e + have h3 := cnt_move_eq_of_sameCell hsame.symm hk' hk he + have hlt := hsame e he + have hnat : (cnt ((db.move k' k hk' hk).insert k' hins) e : ℤ) = + (cnt (db.insert k hk) e : ℤ) := by + rw [h1, h2, h3] + by_cases h : (k : WithTop Key) < e + · have h' : (k' : WithTop Key) < e := hlt.mp h + simp [h, h'] + · have h' : ¬ (k' : WithTop Key) < e := fun hn => h (hlt.mpr hn) + simp [h, h'] + exact_mod_cast hnat + +/-- The flip-witness property transfers to another key in the same cell. -/ +theorem flipWitness_of_sameCell_forward {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) {k k' : Key} + (hsame : SameCell (cellEndpoints D) k k') : + FlipWitness r res k → FlipWitness r res k' := by + intro hwit + by_cases hkk : k = k' + · simpa [hkk] using hwit + rw [flipWitness_iff_exists_not_anyHolds hchar] at hwit ⊢ + obtain ⟨db, hcomp, hnot⟩ := hwit + have hE : dnfEndpoints D ⊆ cellEndpoints D := dnfEndpoints_subset_cellEndpoints D + by_cases hkmem : k ∈ db.keys + · by_cases hk'mem : k' ∈ db.keys + · refine ⟨db, hcomp, ?_⟩ + rw [toggle_eq_erase_of_mem hkmem] at hnot + rw [toggle_eq_erase_of_mem hk'mem] + exact fun hany => hnot ((anyHolds_erase_of_sameCell hE hsame hkmem hk'mem).mpr hany) + · let db' := db.move k k' hkmem hk'mem + have hcomp' : RangeRead.Compatible db' r res := by + rw [hchar db'] + exact ⟨hcomp.1, + (anyHolds_move_of_sameCell hE hsame hkmem hk'mem).mpr ((hchar db).mp hcomp).2⟩ + refine ⟨db', hcomp', ?_⟩ + have hk'_db' : k' ∈ db'.keys := by + unfold db' DB.move + exact DB.mem_insert_self (db.erase k) k' _ + rw [toggle_eq_erase_of_mem hkmem] at hnot + rw [toggle_eq_erase_of_mem hk'_db'] + have hcnt : ∀ e ∈ dnfEndpoints D, cnt (db'.erase k') e = cnt (db.erase k) e := by + intro e he + exact cnt_erase_move_eq_of_sameCell hsame hkmem hk'mem (hE he) + exact fun hany => hnot ((anyHolds_congr_endpoints hcnt).mp hany) + · by_cases hk'mem : k' ∈ db.keys + · have hins : k' ∉ (db.move k' k hk'mem hkmem).keys := + not_mem_move_of_mem_not_mem hkmem hk'mem + let db' := db.move k' k hk'mem hkmem + have hcomp' : RangeRead.Compatible db' r res := by + rw [hchar db'] + exact ⟨hcomp.1, + (anyHolds_move_of_sameCell hE hsame.symm hk'mem hkmem).mpr + ((hchar db).mp hcomp).2⟩ + refine ⟨db', hcomp', ?_⟩ + rw [toggle_eq_insert_of_not_mem hkmem] at hnot + change ¬ AnyHolds D (toggle (db.move k' k hk'mem hkmem) k') + rw [toggle_eq_insert_of_not_mem hins] + have hcnt : ∀ e ∈ dnfEndpoints D, + cnt (db'.insert k' hins) e = cnt (db.insert k hkmem) e := by + intro e he + exact cnt_insert_move_eq_of_sameCell hsame hkmem hk'mem hins (hE he) + exact fun hany => hnot ((anyHolds_congr_endpoints hcnt).mp hany) + · refine ⟨db, hcomp, ?_⟩ + rw [toggle_eq_insert_of_not_mem hkmem] at hnot + rw [toggle_eq_insert_of_not_mem hk'mem] + exact fun hany => hnot ((anyHolds_insert_of_sameCell hE hsame hkmem hk'mem).mpr hany) + +/-- The flip-witness property is constant on a cell. -/ +theorem flipWitness_of_sameCell {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) {k k' : Key} + (hsame : SameCell (cellEndpoints D) k k') : + FlipWitness r res k ↔ FlipWitness r res k' := + ⟨flipWitness_of_sameCell_forward hchar hsame, + flipWitness_of_sameCell_forward hchar hsame.symm⟩ + +/-! ## The finite cell partition -/ + +/-- The least endpoint strictly above `e`, or `⊤` when there is none (which only +happens at `e = ⊤`). -/ +noncomputable def nextEndpoint (E : Finset (WithTop Key)) (e : WithTop Key) : WithTop Key := + if h : (E.filter fun x => e < x).Nonempty then Finset.min' _ h else ⊤ + +theorem nextEndpoint_mem (E : Finset (WithTop Key)) {e : WithTop Key} (he : e < ⊤) + (hTop : ⊤ ∈ E) : nextEndpoint E e ∈ E := by + unfold nextEndpoint + by_cases h : (E.filter fun x => e < x).Nonempty + · rw [dite_eq_left h] + exact (Finset.mem_filter.mp (Finset.min'_mem _ h)).1 + · exact absurd ⟨⊤, Finset.mem_filter.mpr ⟨hTop, he⟩⟩ h + +theorem lt_nextEndpoint (E : Finset (WithTop Key)) {e : WithTop Key} (he : e < ⊤) + (hTop : ⊤ ∈ E) : e < nextEndpoint E e := by + unfold nextEndpoint + by_cases h : (E.filter fun x => e < x).Nonempty + · rw [dite_eq_left h] + exact (Finset.mem_filter.mp (Finset.min'_mem _ h)).2 + · exact absurd ⟨⊤, Finset.mem_filter.mpr ⟨hTop, he⟩⟩ h + +theorem nextEndpoint_le (E : Finset (WithTop Key)) {e x : WithTop Key} + (hx : x ∈ E) (hex : e < x) : nextEndpoint E e ≤ x := by + unfold nextEndpoint + by_cases h : (E.filter fun x => e < x).Nonempty + · rw [dite_eq_left h] + exact Finset.min'_le _ _ (Finset.mem_filter.mpr ⟨hx, hex⟩) + · exact absurd ⟨x, Finset.mem_filter.mpr ⟨hx, hex⟩⟩ h + +/-- The greatest endpoint at or below `k`. -/ +noncomputable def cellLo (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : WithTop Key := + Finset.max' (E.filter fun e => e ≤ (k : WithTop Key)) (by + refine ⟨(([] : Key) : WithTop Key), ?_⟩ + rw [Finset.mem_filter] + exact ⟨hBot, by simp [nil_le_key]⟩) + +theorem cellLo_mem (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) + (k : Key) : cellLo E hBot k ∈ E := by + unfold cellLo + exact (Finset.mem_filter.mp (Finset.max'_mem (E.filter fun e => e ≤ (k : WithTop Key)) _)).1 + +theorem cellLo_le (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) + (k : Key) : cellLo E hBot k ≤ (k : WithTop Key) := by + unfold cellLo + exact (Finset.mem_filter.mp (Finset.max'_mem (E.filter fun e => e ≤ (k : WithTop Key)) _)).2 + +theorem le_cellLo (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) + {k : Key} {e : WithTop Key} (he : e ∈ E) (hek : e ≤ (k : WithTop Key)) : + e ≤ cellLo E hBot k := by + unfold cellLo + exact Finset.le_max' _ _ (Finset.mem_filter.mpr ⟨he, hek⟩) + +/-- The next endpoint after the cell's lower endpoint. -/ +noncomputable def cellHi (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : WithTop Key := + nextEndpoint E (cellLo E hBot k) + +/-- A representative of `k`'s cell: the endpoint itself for a singleton cell, +otherwise the successor of the cell's lower endpoint. The second component is the +upper boundary of the cell. -/ +noncomputable def cellRep (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : Key × WithTop Key := + let lo := cellLo E hBot k + match lo with + | some l => + if (k : WithTop Key) = lo then (k, keyAfter k) + else (keyAfter l, nextEndpoint E lo) + | none => (k, keyAfter k) + +/-- The singleton-cell representative of a finite endpoint. -/ +def singletonRep (e : WithTop Key) : Key × WithTop Key := + match e with + | some l => (l, keyAfter l) + | none => (([] : Key), (([] : Key) : WithTop Key)) + +/-- The open-cell representative following a finite endpoint. -/ +noncomputable def openRep (E : Finset (WithTop Key)) (e : WithTop Key) : + Key × WithTop Key := + match e with + | some l => (keyAfter l, nextEndpoint E e) + | none => (([] : Key), (([] : Key) : WithTop Key)) + +/-- The finite set of cell representatives, with their upper boundaries. -/ +noncomputable def cellReps (E : Finset (WithTop Key)) + (_hBot : (([] : Key) : WithTop Key) ∈ E) (_hTop : ⊤ ∈ E) : + Finset (Key × WithTop Key) := + let endpoints := E.filter fun e => e ≠ ⊤ + ((endpoints.image singletonRep) ∪ (endpoints.image (openRep E))).filter + fun p => (p.1 : WithTop Key) < p.2 + +theorem cellEndpoints_bot (D : ConstraintDNF) : + (([] : Key) : WithTop Key) ∈ cellEndpoints D := by + simp [cellEndpoints] + +theorem cellEndpoints_top (D : ConstraintDNF) : ⊤ ∈ cellEndpoints D := by + simp [cellEndpoints] + +theorem cellLo_lt_top (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : + cellLo E hBot k < (⊤ : WithTop Key) := + lt_of_le_of_lt (cellLo_le E hBot k) (WithTop.coe_lt_top k) + +theorem cellHi_gt (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (hTop : ⊤ ∈ E) (k : Key) : + (k : WithTop Key) < cellHi E hBot k := by + unfold cellHi + have hlo_lt_top : cellLo E hBot k < (⊤ : WithTop Key) := cellLo_lt_top E hBot k + have hlt : cellLo E hBot k < nextEndpoint E (cellLo E hBot k) := + lt_nextEndpoint E hlo_lt_top hTop + have hmem : nextEndpoint E (cellLo E hBot k) ∈ E := + nextEndpoint_mem E hlo_lt_top hTop + by_contra hnot + have hle : nextEndpoint E (cellLo E hBot k) ≤ (k : WithTop Key) := le_of_not_gt hnot + have hle_lo : nextEndpoint E (cellLo E hBot k) ≤ cellLo E hBot k := + le_cellLo E hBot hmem hle + exact (not_lt_of_ge hle_lo) hlt + +theorem cellRep_eq_singleton {E : Finset (WithTop Key)} + (hBot : (([] : Key) : WithTop Key) ∈ E) {k l : Key} + (hlo : cellLo E hBot k = (l : WithTop Key)) (hk : (k : WithTop Key) = (l : WithTop Key)) : + cellRep E hBot k = (k, (keyAfter k : WithTop Key)) := by + simp [cellRep, hlo, hk] + +/-- If a key is itself an endpoint, it is the lower endpoint of its cell, so the +cell representative is the singleton representative `(k, keyAfter k)`. -/ +theorem cellRep_of_mem_endpoints {E : Finset (WithTop Key)} + (hBot : (([] : Key) : WithTop Key) ∈ E) {k : Key} + (hk : (k : WithTop Key) ∈ E) : + cellRep E hBot k = (k, (keyAfter k : WithTop Key)) := by + have hlo : cellLo E hBot k = (k : WithTop Key) := + le_antisymm (cellLo_le E hBot k) (le_cellLo E hBot hk le_rfl) + exact cellRep_eq_singleton hBot hlo rfl + +theorem cellRep_eq_open {E : Finset (WithTop Key)} + (hBot : (([] : Key) : WithTop Key) ∈ E) {k l : Key} + (hlo : cellLo E hBot k = (l : WithTop Key)) (hk : (k : WithTop Key) ≠ (l : WithTop Key)) : + cellRep E hBot k = (keyAfter l, nextEndpoint E (l : WithTop Key)) := by + simp [cellRep, hlo, hk] + +theorem cellRep_fst_le (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : + (cellRep E hBot k).1 ≤ k := by + dsimp only [cellRep] + split + · split + · exact le_refl k + · rename_i l hlo hne + change cellLo E hBot k = (l : WithTop Key) at hlo + have hle : (l : WithTop Key) ≤ (k : WithTop Key) := by + rw [← hlo] + exact cellLo_le E hBot k + have hne' : (k : WithTop Key) ≠ (l : WithTop Key) := by + rw [hlo] at hne + exact hne + have hlt : l < k := by + exact lt_of_le_of_ne (WithTop.coe_le_coe.mp hle) (fun heq => hne' (by rw [heq])) + exact le_of_not_gt fun hklt => + (not_le_of_gt hlt) ((lt_keyAfter_iff k l).mp hklt) + · exact le_refl k + +theorem cellRep_snd_gt (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (hTop : ⊤ ∈ E) (k : Key) : + (k : WithTop Key) < (cellRep E hBot k).2 := by + dsimp only [cellRep] + split + · split + · exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt k) + · rename_i l hlo hne + change cellLo E hBot k = (l : WithTop Key) at hlo + have hlt : (k : WithTop Key) < nextEndpoint E (l : WithTop Key) := by + have h := cellHi_gt E hBot hTop k + unfold cellHi at h + exact hlo ▸ h + dsimp only + rw [hlo] + exact hlt + · exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt k) + +theorem cellRep_sameCell (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : + SameCell E k (cellRep E hBot k).1 := by + dsimp only [cellRep] + split + · rename_i l hlo + split + · rename_i hk + change cellLo E hBot k = (l : WithTop Key) at hlo + have hkl : k = l := WithTop.coe_inj.mp (by simpa [hlo] using hk) + subst k + exact SameCell.refl E l + · rename_i hne + change cellLo E hBot k = (l : WithTop Key) at hlo + change SameCell E k (keyAfter l) + intro e he + have hle : (l : WithTop Key) ≤ (k : WithTop Key) := by + rw [← hlo] + exact cellLo_le E hBot k + have hlk : l < k := by + have hne' : (k : WithTop Key) ≠ (l : WithTop Key) := by + rw [hlo] at hne + exact hne + exact lt_of_le_of_ne (WithTop.coe_le_coe.mp hle) (fun heq => hne' (by rw [heq])) + have hkey_le : (keyAfter l : WithTop Key) ≤ (k : WithTop Key) := + WithTop.coe_le_coe.mpr <| le_of_not_gt fun hklt => + (not_le_of_gt hlk) ((lt_keyAfter_iff k l).mp hklt) + constructor + · intro hke + exact lt_of_le_of_lt hkey_le hke + · intro hkeye + by_contra hke + have hek : (e : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hke + have hele : e ≤ cellLo E hBot k := le_cellLo E hBot he hek + have hle' : (l : WithTop Key) < e := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt l)) hkeye + rw [hlo] at hele + exact (not_lt_of_ge hele) hle' + · rename_i hnone + have hcontra : cellLo E hBot k < (⊤ : WithTop Key) := cellLo_lt_top E hBot k + rw [hnone] at hcontra + exact absurd hcontra (lt_irrefl ⊤) + +theorem cellRep_mem_cellReps (E : Finset (WithTop Key)) + (hBot : (([] : Key) : WithTop Key) ∈ E) (hTop : ⊤ ∈ E) (k : Key) : + cellRep E hBot k ∈ cellReps E hBot hTop := by + dsimp only [cellRep] + split + · split + · rename_i l hlo hk + change cellLo E hBot k = (l : WithTop Key) at hlo + have hmem : (l : WithTop Key) ∈ E := by rw [← hlo]; exact cellLo_mem E hBot k + have hkl : k = l := WithTop.coe_inj.mp (by simpa [hlo] using hk) + subst k + have hne : (l : WithTop Key) ≠ ⊤ := by simp + have hlt : (l : WithTop Key) < (keyAfter l : WithTop Key) := + WithTop.coe_lt_coe.mpr (keyAfter_self_lt l) + refine Finset.mem_filter.mpr ⟨?_, hlt⟩ + exact Finset.mem_union.mpr (Or.inl (Finset.mem_image.mpr + ⟨(l : WithTop Key), Finset.mem_filter.mpr ⟨hmem, hne⟩, by simp [singletonRep]⟩)) + · rename_i l hlo hne + change cellLo E hBot k = (l : WithTop Key) at hlo + have hmem : (l : WithTop Key) ∈ E := by rw [← hlo]; exact cellLo_mem E hBot k + have hne_top : (l : WithTop Key) ≠ ⊤ := by simp + have hle : (l : WithTop Key) ≤ (k : WithTop Key) := by + rw [← hlo] + exact cellLo_le E hBot k + have hlk : l < k := by + have hne' : (k : WithTop Key) ≠ (l : WithTop Key) := by + rw [hlo] at hne + exact hne + exact lt_of_le_of_ne (WithTop.coe_le_coe.mp hle) (fun heq => hne' (by rw [heq])) + have hkey_le : (keyAfter l : WithTop Key) ≤ (k : WithTop Key) := + WithTop.coe_le_coe.mpr <| le_of_not_gt fun hklt => + (not_le_of_gt hlk) ((lt_keyAfter_iff k l).mp hklt) + have hk_hi : (k : WithTop Key) < nextEndpoint E (l : WithTop Key) := by + have h := cellHi_gt E hBot hTop k + unfold cellHi at h + exact hlo ▸ h + have hlt : (keyAfter l : WithTop Key) < nextEndpoint E (l : WithTop Key) := + lt_of_le_of_lt hkey_le hk_hi + rw [hlo] + refine Finset.mem_filter.mpr ⟨?_, hlt⟩ + exact Finset.mem_union.mpr (Or.inr (Finset.mem_image.mpr + ⟨(l : WithTop Key), Finset.mem_filter.mpr ⟨hmem, hne_top⟩, by simp [openRep]⟩)) + · rename_i hnone + have hcontra : cellLo E hBot k < (⊤ : WithTop Key) := cellLo_lt_top E hBot k + rw [hnone] at hcontra + exact absurd hcontra (lt_irrefl ⊤) + +/-- Every representative in `cellReps` is either a singleton rep or an open rep. -/ +theorem mem_cellReps_cases {E : Finset (WithTop Key)} + (hBot : (([] : Key) : WithTop Key) ∈ E) (hTop : ⊤ ∈ E) + {p : Key × WithTop Key} (hp : p ∈ cellReps E hBot hTop) : + (∃ l : Key, (l : WithTop Key) ∈ E ∧ p = (l, (keyAfter l : WithTop Key))) ∨ + (∃ l : Key, (l : WithTop Key) ∈ E ∧ + p = (keyAfter l, nextEndpoint E (l : WithTop Key))) := by + unfold cellReps at hp + rw [Finset.mem_filter] at hp + obtain ⟨hunion, hlt⟩ := hp + rcases Finset.mem_union.mp hunion with hsingle | hopen + · obtain ⟨e, he, rfl⟩ := Finset.mem_image.mp hsingle + obtain ⟨heE, hne⟩ := Finset.mem_filter.mp he + cases e with + | coe l => + exact Or.inl ⟨l, by simpa using heE, by simp [singletonRep]⟩ + | top => + simp [singletonRep] at hlt + · obtain ⟨e, he, rfl⟩ := Finset.mem_image.mp hopen + obtain ⟨heE, hne⟩ := Finset.mem_filter.mp he + cases e with + | coe l => + exact Or.inr ⟨l, by simpa using heE, by simp [openRep]⟩ + | top => + simp [openRep] at hlt + +/-- A key lying in a representative's interval is in the same cell as the +representative. -/ +theorem sameCell_of_mem_cellReps {E : Finset (WithTop Key)} + (hBot : (([] : Key) : WithTop Key) ∈ E) (hTop : ⊤ ∈ E) + {p : Key × WithTop Key} (hp : p ∈ cellReps E hBot hTop) {k : Key} + (hlo : p.1 ≤ k) (hhi : (k : WithTop Key) < p.2) : + SameCell E k p.1 := by + rcases mem_cellReps_cases hBot hTop hp with ⟨l, hlE, rfl⟩ | ⟨l, hlE, rfl⟩ + · -- singleton rep: the half-open interval [l, keyAfter l) contains only l + change SameCell E k l + change l ≤ k at hlo + change (k : WithTop Key) < (keyAfter l : WithTop Key) at hhi + have hkl : k = l := by + have hk_lt : k < keyAfter l := WithTop.coe_lt_coe.mp hhi + exact le_antisymm ((lt_keyAfter_iff k l).mp hk_lt) hlo + subst k + exact SameCell.refl E l + · -- open rep: no endpoint lies strictly between `l` and the next endpoint + change SameCell E k (keyAfter l) + change keyAfter l ≤ k at hlo + change (k : WithTop Key) < nextEndpoint E (l : WithTop Key) at hhi + intro e he + constructor + · intro hke + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hlo) hke + · intro hkeye + have hlkey : (l : WithTop Key) < (keyAfter l : WithTop Key) := + WithTop.coe_lt_coe.mpr (keyAfter_self_lt l) + have hle : (l : WithTop Key) < e := lt_trans hlkey hkeye + by_contra hnot + have hle' : e ≤ (k : WithTop Key) := le_of_not_gt hnot + have hnext_le : nextEndpoint E (l : WithTop Key) ≤ e := + nextEndpoint_le E he hle + have he_lt : e < nextEndpoint E (l : WithTop Key) := + lt_of_le_of_lt hle' hhi + exact (not_lt_of_ge hnext_le) he_lt + +/-! ## Toggling a list of keys -/ + +/-- Toggle each key in a list, in order. -/ +def toggleList (db : DB) : List Key → DB + | [] => db + | k :: ks => toggleList (toggle db k) ks + +/-- Membership after a single toggle. -/ +theorem mem_toggle_keys {db : DB} {k x : Key} : + x ∈ (toggle db k).keys ↔ (x = k ∧ x ∉ db.keys) ∨ (x ≠ k ∧ x ∈ db.keys) := by + by_cases hx : x = k + · subst x + by_cases hk : k ∈ db.keys + · rw [toggle_eq_erase_of_mem hk] + have hnot' : k ∉ db.keys.erase k := db.nodup.not_mem_erase + simp [hnot', hk] + · rw [toggle_eq_insert_of_not_mem hk] + simp [hk] + · by_cases hk : k ∈ db.keys + · rw [toggle_eq_erase_of_mem hk, DB.mem_erase_iff] + simp [hx] + · rw [toggle_eq_insert_of_not_mem hk, DB.mem_insert_iff hk] + simp [hx] + +/-- Toggling a duplicate-free list once gives the expected membership: a key of +the list is present exactly when it was absent before, and every other key is +unchanged. -/ +theorem mem_toggleList_keys {db : DB} {ks : List Key} (hnd : ks.Nodup) (x : Key) : + x ∈ (toggleList db ks).keys ↔ + if x ∈ ks then x ∉ db.keys else x ∈ db.keys := by + induction ks generalizing db with + | nil => simp [toggleList] + | cons k ks ih => + rw [List.nodup_cons] at hnd + obtain ⟨hknot, hnd'⟩ := hnd + simp only [toggleList] + rw [ih hnd'] + by_cases hx : x = k + · subst x + have hxks : k ∉ ks := hknot + simp [List.mem_cons, hxks, mem_toggle_keys] + · have hxtoggle : x ∈ (toggle db k).keys ↔ x ∈ db.keys := by + rw [mem_toggle_keys] + simp [hx] + by_cases hxks : x ∈ ks + · simp [List.mem_cons, hx, hxks, hxtoggle] + · simp [List.mem_cons, hx, hxks, hxtoggle] + +/-- A list-toggle preserves sortedness. -/ +theorem toggleList_sorted (db : DB) (ks : List Key) : (toggleList db ks).keys.SortedLT := by + induction ks generalizing db with + | nil => exact db.sorted + | cons k ks ih => exact ih (toggle db k) + +/-! ## The active cells and their hull -/ + +/-- The representatives of the cells that admit a one-key flip witness. -/ +noncomputable def activeReps (r : RangeRead) (res : Result) (D : ConstraintDNF) : + Finset (Key × WithTop Key) := by + classical + exact (cellReps (cellEndpoints D) (cellEndpoints_bot D) (cellEndpoints_top D)).filter + (fun p => FlipWitness r res p.1) + +/-- The least key among the active cell representatives. -/ +noncomputable def activeBegin (A : Finset (Key × WithTop Key)) (h : A.Nonempty) : Key := + (A.image fun p => p.1).min' (h.image fun p => p.1) + +/-- The greatest upper boundary among the active cell representatives. -/ +noncomputable def activeEnd (A : Finset (Key × WithTop Key)) (h : A.Nonempty) : WithTop Key := + (A.image fun p => p.2).max' (h.image fun p => p.2) + +/-- The hull of the active cells, from the least active key to the greatest active +upper boundary. When there are no active cells this is the empty range. -/ +noncomputable def flipHull (r : RangeRead) (res : Result) (D : ConstraintDNF) : + ConflictRange := by + classical + let A := activeReps r res D + exact if h : A.Nonempty then + ⟨(activeBegin A h : WithTop Key), activeEnd A h⟩ + else + ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩ + +theorem activeReps_mem_iff {r : RangeRead} {res : Result} {D : ConstraintDNF} + {p : Key × WithTop Key} : + p ∈ activeReps r res D ↔ + p ∈ cellReps (cellEndpoints D) (cellEndpoints_bot D) (cellEndpoints_top D) ∧ + FlipWitness r res p.1 := by + classical + unfold activeReps + rw [Finset.mem_filter] + +theorem cellRep_mem_activeReps_iff {r : RangeRead} {res : Result} {D : ConstraintDNF} + (hchar : CharacterizesDNF D r res) {k : Key} : + cellRep (cellEndpoints D) (cellEndpoints_bot D) k ∈ activeReps r res D ↔ + FlipWitness r res k := by + rw [activeReps_mem_iff] + have hsame : SameCell (cellEndpoints D) k + (cellRep (cellEndpoints D) (cellEndpoints_bot D) k).1 := + cellRep_sameCell (cellEndpoints D) (cellEndpoints_bot D) k + have hiff := flipWitness_of_sameCell hchar hsame + constructor + · rintro ⟨_, hw⟩ + exact hiff.mpr hw + · intro hw + exact ⟨cellRep_mem_cellReps (cellEndpoints D) (cellEndpoints_bot D) + (cellEndpoints_top D) k, hiff.mp hw⟩ + +theorem activeBegin_le {A : Finset (Key × WithTop Key)} {h : A.Nonempty} + {p : Key × WithTop Key} (hp : p ∈ A) : activeBegin A h ≤ p.1 := by + unfold activeBegin + exact Finset.min'_le _ _ (Finset.mem_image.mpr ⟨p, hp, rfl⟩) + +theorem le_activeEnd {A : Finset (Key × WithTop Key)} {h : A.Nonempty} + {p : Key × WithTop Key} (hp : p ∈ A) : p.2 ≤ activeEnd A h := by + unfold activeEnd + exact Finset.le_max' _ _ (Finset.mem_image.mpr ⟨p, hp, rfl⟩) + +theorem flipHull_eq_active {r : RangeRead} {res : Result} {D : ConstraintDNF} + {h : (activeReps r res D).Nonempty} : + flipHull r res D = + ⟨(activeBegin (activeReps r res D) h : WithTop Key), + activeEnd (activeReps r res D) h⟩ := by + classical + unfold flipHull + rw [dite_eq_left h] + +theorem flipHull_eq_empty {r : RangeRead} {res : Result} {D : ConstraintDNF} + (h : ¬ (activeReps r res D).Nonempty) : + flipHull r res D = + ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩ := by + classical + unfold flipHull + rw [dite_eq_right h] + +set_option maxHeartbeats 1000000 in +-- Unfolding the hull's endpoint expressions is expensive; give this declaration room. +theorem active_cellRep_inside_hull {r : RangeRead} {res : Result} {D : ConstraintDNF} + {k : Key} (ha : cellRep (cellEndpoints D) (cellEndpoints_bot D) k ∈ + activeReps r res D) : + k ∈ flipHull r res D := by + classical + let A := activeReps r res D + have hne : A.Nonempty := ⟨_, ha⟩ + rw [flipHull_eq_active (h := hne)] + rw [ConflictRange.mem_iff] + constructor + · exact le_trans (WithTop.coe_le_coe.mpr (activeBegin_le ha)) + (WithTop.coe_le_coe.mpr (cellRep_fst_le (cellEndpoints D) + (cellEndpoints_bot D) k)) + · exact lt_of_lt_of_le (cellRep_snd_gt (cellEndpoints D) (cellEndpoints_bot D) + (cellEndpoints_top D) k) (le_activeEnd (h := hne) ha) + +theorem not_flipWitness_of_not_mem_hull {r : RangeRead} {res : Result} {D : ConstraintDNF} + (hchar : CharacterizesDNF D r res) {k : Key} (hk : k ∉ flipHull r res D) : + ¬ FlipWitness r res k := by + intro hw + have ha : cellRep (cellEndpoints D) (cellEndpoints_bot D) k ∈ activeReps r res D := + (cellRep_mem_activeReps_iff hchar).mpr hw + exact hk (active_cellRep_inside_hull ha) + +theorem compatible_toggle_of_not_mem_hull {r : RangeRead} {res : Result} {D : ConstraintDNF} + (hchar : CharacterizesDNF D r res) {db : DB} {k : Key} + (hcomp : RangeRead.Compatible db r res) (hk : k ∉ flipHull r res D) : + RangeRead.Compatible (toggle db k) r res := by + by_contra hnot + exact not_flipWitness_of_not_mem_hull hchar hk ⟨db, hcomp, hnot⟩ + +theorem compatible_toggleList_of_forall_not_mem_hull {r : RangeRead} {res : Result} + {D : ConstraintDNF} (hchar : CharacterizesDNF D r res) {db : DB} {ks : List Key} + (hcomp : RangeRead.Compatible db r res) + (hks : ∀ k ∈ ks, k ∉ flipHull r res D) : + RangeRead.Compatible (toggleList db ks) r res := by + induction ks generalizing db with + | nil => simpa [toggleList] using hcomp + | cons k ks ih => + simp only [toggleList] + exact ih (compatible_toggle_of_not_mem_hull hchar hcomp (hks k (by simp))) + (fun x hx => hks x (by simp [hx])) + +/-! ## The symmetric-difference toggle path -/ + +/-- The keys whose membership differs between two databases. -/ +def diffKeys (db₁ db₂ : DB) : List Key := + (db₁.keys.filter fun k => decide (k ∉ db₂.keys)) ++ + (db₂.keys.filter fun k => decide (k ∉ db₁.keys)) + +theorem mem_diffKeys {db₁ db₂ : DB} {k : Key} : + k ∈ diffKeys db₁ db₂ ↔ + (k ∈ db₁.keys ∧ k ∉ db₂.keys) ∨ (k ∉ db₁.keys ∧ k ∈ db₂.keys) := by + unfold diffKeys + rw [List.mem_append, List.mem_filter, List.mem_filter] + simp only [decide_eq_true_eq] + constructor + · rintro (⟨h1, h2⟩ | ⟨h1, h2⟩) + · exact Or.inl ⟨h1, h2⟩ + · exact Or.inr ⟨h2, h1⟩ + · rintro (⟨h1, h2⟩ | ⟨h1, h2⟩) + · exact Or.inl ⟨h1, h2⟩ + · exact Or.inr ⟨h2, h1⟩ + +theorem diffKeys_nodup (db₁ db₂ : DB) : (diffKeys db₁ db₂).Nodup := by + unfold diffKeys + rw [List.nodup_append] + refine ⟨db₁.nodup.filter _, db₂.nodup.filter _, ?_⟩ + intro a ha b hb heq + subst b + have ha₁ : a ∈ db₁.keys := (List.mem_filter.mp ha).1 + have hb₁ : a ∉ db₁.keys := of_decide_eq_true (List.mem_filter.mp hb).2 + exact hb₁ ha₁ + +theorem diffKeys_not_mem_of_agreeOn {cr : ConflictRange} {db₁ db₂ : DB} + (h : AgreeOn cr db₁ db₂) {k : Key} (hk : k ∈ cr) : + k ∉ diffKeys db₁ db₂ := by + rw [mem_diffKeys] + rintro (⟨h1, h2⟩ | ⟨h1, h2⟩) + · exact h2 (((agreeOn_iff cr db₁ db₂).mp h k hk).mp h1) + · exact h1 (((agreeOn_iff cr db₁ db₂).mp h k hk).mpr h2) + +theorem toggleList_diffKeys_keys (db₁ db₂ : DB) : + (toggleList db₁ (diffKeys db₁ db₂)).keys = db₂.keys := by + apply List.SortedLT.eq_of_mem_iff (toggleList_sorted db₁ (diffKeys db₁ db₂)) db₂.sorted + intro x + rw [mem_toggleList_keys (diffKeys_nodup db₁ db₂) x] + by_cases h1 : x ∈ db₁.keys <;> by_cases h2 : x ∈ db₂.keys + · have hx : x ∉ diffKeys db₁ db₂ := by + rw [mem_diffKeys]; tauto + simp [hx, h1, h2] + · have hx : x ∈ diffKeys db₁ db₂ := by + rw [mem_diffKeys]; exact Or.inl ⟨h1, h2⟩ + simp [hx, h1, h2] + · have hx : x ∈ diffKeys db₁ db₂ := by + rw [mem_diffKeys]; exact Or.inr ⟨h1, h2⟩ + simp [hx, h1, h2] + · have hx : x ∉ diffKeys db₁ db₂ := by + rw [mem_diffKeys]; tauto + simp [hx, h1, h2] + +/-! ## Soundness and necessity of the hull -/ + +theorem flipHull_sound {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) : Sound r res (flipHull r res D) := by + intro db₁ db₂ hagree + constructor + · intro hcomp₁ + let ks := diffKeys db₁ db₂ + have hks : ∀ k ∈ ks, k ∉ flipHull r res D := by + intro k hk hkcr + exact diffKeys_not_mem_of_agreeOn (cr := flipHull r res D) hagree hkcr hk + have hcomp_tl := compatible_toggleList_of_forall_not_mem_hull hchar hcomp₁ hks + have hkeys := toggleList_diffKeys_keys db₁ db₂ + exact (RangeRead.compatible_congr (RangeRead.completeKeys_congr hkeys r)).mp hcomp_tl + · intro hcomp₂ + let ks := diffKeys db₂ db₁ + have hks : ∀ k ∈ ks, k ∉ flipHull r res D := by + intro k hk hkcr + exact diffKeys_not_mem_of_agreeOn (cr := flipHull r res D) + (AgreeOn.symm hagree) hkcr hk + have hcomp_tl := compatible_toggleList_of_forall_not_mem_hull hchar hcomp₂ hks + have hkeys := toggleList_diffKeys_keys db₂ db₁ + exact (RangeRead.compatible_congr (RangeRead.completeKeys_congr hkeys r)).mp hcomp_tl + +set_option maxHeartbeats 1000000 in +-- Unfolding the hull's endpoint expressions is expensive; give this declaration room. +theorem necessary_of_mem_flipHull {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) {k : Key} (hk : k ∈ flipHull r res D) : + Necessary r res k := by + classical + let A := activeReps r res D + have hne : A.Nonempty := by + by_contra hA + rw [flipHull_eq_empty (h := hA)] at hk + rw [ConflictRange.mem_iff] at hk + exact (not_lt_of_ge hk.1) hk.2 + rw [flipHull_eq_active (h := hne)] at hk + have hmin_mem : activeBegin A hne ∈ A.image (fun p => p.1) := Finset.min'_mem _ _ + obtain ⟨p, hpA, hpkey⟩ := Finset.mem_image.mp hmin_mem + have hp_le : p.1 ≤ k := by + have h := hk.1 + rw [← hpkey] at h + exact WithTop.coe_le_coe.mp h + by_cases hcell : ∃ p ∈ A, p.1 ≤ k ∧ (k : WithTop Key) < p.2 + · obtain ⟨p, hpA, hlo, hhi⟩ := hcell + have hpcell : p ∈ cellReps (cellEndpoints D) (cellEndpoints_bot D) + (cellEndpoints_top D) := ((activeReps_mem_iff).mp hpA).1 + have hsame : SameCell (cellEndpoints D) k p.1 := + sameCell_of_mem_cellReps (cellEndpoints_bot D) (cellEndpoints_top D) hpcell hlo hhi + have hpw : FlipWitness r res p.1 := ((activeReps_mem_iff).mp hpA).2 + exact necessary_of_toggle ((flipWitness_of_sameCell hchar hsame).mpr hpw) + · have hmax_mem : activeEnd A hne ∈ A.image (fun p => p.2) := Finset.max'_mem _ _ + obtain ⟨q, hqA, hqend⟩ := Finset.mem_image.mp hmax_mem + have hqle : ¬ q.1 ≤ k := by + intro hqk + exact hcell ⟨q, hqA, hqk, by rw [hqend]; exact hk.2⟩ + have hkq : k < q.1 := lt_of_not_ge hqle + have hp_nec : Necessary r res p.1 := + necessary_of_toggle ((activeReps_mem_iff).mp hpA).2 + have hq_nec : Necessary r res q.1 := + necessary_of_toggle ((activeReps_mem_iff).mp hqA).2 + exact Necessary.of_between hp_nec hq_nec hp_le (le_of_lt hkq) + +theorem necessarySet_eq_flipHull {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) : + (flipHull r res D).toSet = necessarySet r res := by + ext k + constructor + · intro hk + exact necessary_of_mem_flipHull hchar hk + · intro hk + exact hk (flipHull r res D) (flipHull_sound hchar) + +theorem exists_minimal_of_characterizes {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) : + ∃ cr : ConflictRange, Minimal r res cr := + exists_minimal_of_necessarySet_representable + ⟨flipHull r res D, necessarySet_eq_flipHull hchar⟩ + +/-- The flip hull of a characterizing DNF is itself minimal, not merely some range +whose existence follows from the characterization. -/ +theorem minimal_flipHull {D : ConstraintDNF} {r : RangeRead} {res : Result} + (hchar : CharacterizesDNF D r res) : + Minimal r res (flipHull r res D) := + (minimal_iff_sound_and_necessary).mpr + ⟨flipHull_sound hchar, fun _ hk => necessary_of_mem_flipHull hchar hk⟩ + +/-- **The headline, unconditional on a readable read.** Every read/result pair +that is realized by some database has a minimal conflict range. The characterizing +DNF is `emptyConstraints` for the empty result and the precise collector otherwise; +the general flip-witness hull then represents the necessary keys. -/ +theorem exists_minimal_conflict_range_of_readable {r : RangeRead} {res : Result} + (hread : Readable r res) : ∃ cr : ConflictRange, Minimal r res cr := by + obtain ⟨db, hcomp⟩ := hread + by_cases hne : res.keys = [] + · have hmore : res.more = false := by + by_contra hm + have hmtrue : res.more = true := by + cases h : res.more + · exact absurd h hm + · rfl + exact (RangeRead.compatible_ne_nil_of_more hcomp hmtrue) hne + have hres : res = emptyRes := by + cases res with + | mk ks more => + simp at hne + subst ks + have hmore' : more = false := by simpa using hmore + subst more + rfl + have hchar : CharacterizesDNF (emptyConstraints r) r res := by + rw [hres] + exact characterizesDNF_emptyConstraints r + exact exists_minimal_of_characterizes hchar + · have hA : ascendingKeys r res ≠ [] := ascendingKeys_ne_nil hne + have hpair : (ascendingKeys r res).Pairwise (· < ·) := + pairwise_ascendingKeys ⟨db, hcomp⟩ hne + exact exists_minimal_of_characterizes + (characterizesDNF_collectOf (r := r) (res := res) hA hpair) + +/-! ## The general algorithm + +The algorithm is not a table of offset cases. It is the cellwise flip-witness hull +of a characterizing DNF: partition the key space at the DNF endpoints, mark a cell +active when a one-key toggle in that cell breaks the DNF, and take the hull of the +active cells. The characterizing DNF is `emptyConstraints` for the empty result and +the precise collector otherwise. + +The definition below is noncomputable only because `FlipWitness` is an existential +over databases. The cellwise shape is the complete algorithm statement; an +executable version replaces `FlipWitness` by a decision procedure for active cells. -/ + +/-- The characterizing DNF used by the general algorithm: the empty theory for an +empty result and the precise collector for a nonempty result. -/ +def characterizingDNF (r : RangeRead) (res : Result) : ConstraintDNF := + if res.keys = [] then emptyConstraints r + else collectOf r (ascendingKeys r res) res.more + +/-- The DNF produced by `characterizingDNF` characterizes every readable pair. -/ +theorem characterizesDNF_characterizingDNF {r : RangeRead} {res : Result} + (hread : Readable r res) : + CharacterizesDNF (characterizingDNF r res) r res := by + obtain ⟨db, hcomp⟩ := hread + unfold characterizingDNF + by_cases hne : res.keys = [] + · have hmore : res.more = false := by + by_contra hm + have hmtrue : res.more = true := by + cases h : res.more + · exact absurd h hm + · rfl + exact (RangeRead.compatible_ne_nil_of_more hcomp hmtrue) hne + have hres : res = emptyRes := by + cases res with + | mk ks more => + simp at hne + subst ks + have hmore' : more = false := by simpa using hmore + subst more + rfl + rw [ite_eq_left hne, hres] + exact characterizesDNF_emptyConstraints r + · have hA : ascendingKeys r res ≠ [] := ascendingKeys_ne_nil hne + have hpair : (ascendingKeys r res).Pairwise (· < ·) := + pairwise_ascendingKeys ⟨db, hcomp⟩ hne + rw [ite_eq_right hne] + exact characterizesDNF_collectOf (r := r) (res := res) hA hpair + +/-- **The general conflict-range algorithm.** Take the cellwise flip-witness hull +of the characterizing DNF. This is the offset-free specification: the endpoint +cells, their active statuses, and the hull are the whole algorithm. -/ +noncomputable def conflictRangeGeneral (r : RangeRead) (res : Result) : ConflictRange := + flipHull r res (characterizingDNF r res) + +/-- **The general algorithm is correct.** On every readable pair it returns a +minimal conflict range. The proof is the characterization lemma together with the +flip-hull minimality theorem; no offset case analysis is involved. -/ +theorem conflictRangeGeneral_minimal {r : RangeRead} {res : Result} + (hread : Readable r res) : + Minimal r res (conflictRangeGeneral r res) := + minimal_flipHull (characterizesDNF_characterizingDNF hread) + +/-- A Boolean activity oracle is correct when it accepts exactly the keys with a +one-key flip witness. This is the only interface the cellwise algorithm consumes; +constructing such an oracle is the remaining implementation task. -/ +def ActiveDecider (r : RangeRead) (res : Result) (active : Key → Bool) : Prop := + ∀ k, active k = true ↔ FlipWitness r res k + +/-- The cell representatives selected by a Boolean activity oracle. This is the +computational shape of `activeReps`: the only difference is that `FlipWitness` is +replaced by a decision procedure. -/ +noncomputable def activeRepsOf (D : ConstraintDNF) (active : Key → Bool) : + Finset (Key × WithTop Key) := + (cellReps (cellEndpoints D) (cellEndpoints_bot D) (cellEndpoints_top D)).filter + (fun p => active p.1) + +/-- The hull of a finite set of cell representatives. This is the endpoint +extraction used by both `flipHull` and the oracle-driven algorithm. -/ +noncomputable def hullOfActiveReps (A : Finset (Key × WithTop Key)) : ConflictRange := + if h : A.Nonempty then + ⟨(activeBegin A h : WithTop Key), activeEnd A h⟩ + else + ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩ + +/-- The cellwise algorithm with an abstract activity oracle. It is the executable +shape of `conflictRangeGeneral`: once `active r res` is a decision procedure for +`FlipWitness r res`, this definition computes the hull. -/ +noncomputable def conflictRangeCellwise (r : RangeRead) (res : Result) + (active : Key → Bool) : ConflictRange := + hullOfActiveReps (activeRepsOf (characterizingDNF r res) active) + +theorem activeRepsOf_eq_activeReps {D : ConstraintDNF} {r : RangeRead} {res : Result} + {active : Key → Bool} + (hactive : ActiveDecider r res active) : + activeRepsOf D active = activeReps r res D := by + ext p + simp only [activeRepsOf, activeReps, Finset.mem_filter] + exact ⟨fun h => ⟨h.1, (hactive p.1).mp h.2⟩, + fun h => ⟨h.1, (hactive p.1).mpr h.2⟩⟩ + +theorem hullOfActiveReps_eq_flipHull {D : ConstraintDNF} {r : RangeRead} {res : Result} + {active : Key → Bool} + (hactive : ActiveDecider r res active) : + hullOfActiveReps (activeRepsOf D active) = flipHull r res D := by + unfold hullOfActiveReps flipHull + rw [activeRepsOf_eq_activeReps (D := D) hactive] + +/-- The oracle-driven cellwise algorithm is the general algorithm, provided the +oracle decides exactly the one-key flip witnesses. -/ +theorem conflictRangeCellwise_eq_general {r : RangeRead} {res : Result} + {active : Key → Bool} + (hactive : ActiveDecider r res active) : + conflictRangeCellwise r res active = conflictRangeGeneral r res := by + unfold conflictRangeCellwise conflictRangeGeneral + exact hullOfActiveReps_eq_flipHull (D := characterizingDNF r res) hactive + +/-- **Correctness of the oracle-driven cellwise algorithm.** If the activity +oracle decides `FlipWitness` exactly, then the cellwise hull is minimal on every +readable pair. This isolates the only remaining implementation obligation: build +and prove such an oracle (for example, the region-count/DBM test). -/ +theorem conflictRangeCellwise_minimal {r : RangeRead} {res : Result} + {active : Key → Bool} + (hactive : ActiveDecider r res active) + (hread : Readable r res) : + Minimal r res (conflictRangeCellwise r res active) := by + rw [conflictRangeCellwise_eq_general hactive] + exact conflictRangeGeneral_minimal hread + +/-- The active-cell algorithm computes exactly the necessary set. This is the +general-principle correctness statement: the hull of the active cells is the set of +keys forced by every sound range. -/ +theorem conflictRangeCellwise_toSet_eq_necessarySet {r : RangeRead} {res : Result} + {active : Key → Bool} + (hactive : ActiveDecider r res active) + (hread : Readable r res) : + (conflictRangeCellwise r res active).toSet = necessarySet r res := by + rw [conflictRangeCellwise_eq_general hactive] + exact necessarySet_eq_flipHull (characterizesDNF_characterizingDNF hread) + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossBottom.lean b/FormalConflictRange/NegCrossBottom.lean new file mode 100644 index 0000000..fb9e589 --- /dev/null +++ b/FormalConflictRange/NegCrossBottom.lean @@ -0,0 +1,153 @@ +import FormalConflictRange.NegCrossComplete + +open FormalConflictRange + +namespace FormalConflictRange + +/-- The negative-begin complete bottom cell: forward, complete, nonempty, nonpositive +end, `begin.key ≤ end.key`, and reported head the empty key. -/ +abbrev negCrossHeadBottom (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + res.keys.headD ([] : Key) = ([] : Key) + +/-- The closed form for the negative-begin complete bottom cell: `[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossHeadBottom (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- Filtering keys below `E` is the same as filtering the half-open interval +`[⊥, E)`. -/ +theorem filter_between_bot_eq {l : List Key} {E : WithTop Key} : + l.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < E)) = + l.filter (fun k : Key => decide (((k : Key) : WithTop Key) < E)) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨coe_nil_le_key_withTop _, h⟩⟩ + +/-- **The negative-begin complete bottom cell is minimal.** The reported head is the +empty key, so the begin selector resolves to `⊥` in every compatible database; the +complete window is a prefix of the keys below `end_.key`, hence agreement on +`[⊥, end_.key)` determines compatibility. Every key in `[⊥, end_.key)` is necessary: +the reported keys and the gaps between them by convexity, and the keys above the +reported last by `necessary_last_to_end_key`. -/ +theorem minimal_closedForm_of_negCross_headBottom {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossHeadBottom r res) : + Minimal r res (closedFormNegCrossHeadBottom r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hheadD⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_empty : (res.keys.head hne : Key) = ([] : Key) := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne] at hheadD + simpa using hheadD + rw [show closedFormNegCrossHeadBottom r res = + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ by rfl] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have step : ∀ db₁ db₂, AgreeOn ⟨(([] : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + 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 : res.keys.head hne ∈ RangeRead.completeKeys db₁ r := by + rw [← hres_eq]; exact hhead_mem + have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) := by + have hle : resolve db₁ r.begin ≤ (([] : Key) : WithTop Key) := by + have h := (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + rwa [hhead_empty] at h + exact le_antisymm hle (coe_nil_le_key_withTop _) + have hfilt : db₁.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by + simpa [AgreeOn, ConflictRange.keys] using hagree + have hfiltE : db₁.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.end_.key)) = + db₂.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) := by + rw [← filter_between_bot_eq, ← filter_between_bot_eq] + exact hfilt + have hR : resolve db₂ r.end_ = resolve db₁ r.end_ := by + rw [resolve_eq_filter_lt_reverse (db := db₁) hend, + resolve_eq_filter_lt_reverse (db := db₂) hend, hfiltE] + have hfiltB : db₁.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + db₂.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := + filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := ([] : Key)) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt + have hfiltB0 : db₁.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = + db₂.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) := by + rw [← filter_between_bot_eq (E := r.begin.key), + ← filter_between_bot_eq (E := r.begin.key)] + exact hfiltB + have hL₂ : resolve db₂ r.begin = resolve db₁ r.begin := by + rw [resolve_eq_filter_lt_reverse (db := db₁) (s := r.begin) (le_of_lt hbegin), + resolve_eq_filter_lt_reverse (db := db₂) (s := r.begin) (le_of_lt hbegin), + hfiltB0] + have hL₂' : resolve db₂ r.begin = (([] : Key) : WithTop Key) := by rw [hL₂, hL₁] + have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hP : db₁.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) = + db₂.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) := + filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := ([] : Key)) (B := resolve db₁ r.end_) (E := r.end_.key) hres_le hfilt + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), hL₁, hL₂', hR] + exact hP + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_bot : ([] : Key) ≤ k := WithTop.coe_le_coe.mp hlo + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result ⟨db₀, hcomp₀⟩ hhead_mem (List.getLast_mem hne) + (by rw [hhead_empty]; exact hk_bot) hklast + · exact fun cr hs => necessary_last_to_end_key ⟨db₀, hcomp₀⟩ hrev hmore hne (by omega) + cr hs (le_of_lt (lt_of_not_ge hklast)) hhi + +/-- The reversed negative-begin complete bottom cell: the reported last is the empty +key. -/ +abbrev negCrossHeadBottomRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + res.keys.getLastD ([] : Key) = ([] : Key) + +/-- The closed form for the reversed negative-begin complete bottom cell. -/ +noncomputable def closedFormNegCrossHeadBottomRev (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **The reversed negative-begin complete bottom cell is minimal**, by flipping the +traversal. -/ +theorem minimal_closedForm_of_negCross_headBottom_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossHeadBottomRev r res) : + Minimal r res (closedFormNegCrossHeadBottomRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hlastD⟩ := hcell + 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 hcell' : negCrossHeadBottom {r with reverse := false} + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, ?_⟩ + rw [hheadD]; exact hlastD + have hread' : Readable {r with reverse := false} + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (r := r) (res := res) (b := true) hrev hmore).mpr hcomp⟩ + have hmin' : Minimal {r with reverse := false} + { keys := res.keys.reverse, more := false } + (closedFormNegCrossHeadBottomRev r res) := by + simpa [closedFormNegCrossHeadBottomRev, closedFormNegCrossHeadBottom] using + minimal_closedForm_of_negCross_headBottom hread' hcell' + exact (minimal_flip (r := r) (res := res) (cr := closedFormNegCrossHeadBottomRev r res) + (b := true) hrev hmore).mp hmin' + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossClampLongNonpos.lean b/FormalConflictRange/NegCrossClampLongNonpos.lean new file mode 100644 index 0000000..7c37e87 --- /dev/null +++ b/FormalConflictRange/NegCrossClampLongNonpos.lean @@ -0,0 +1,1474 @@ +/- +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.NegCrossComplete +import FormalConflictRange.NegCrossLongNonpos + +/-! +# The crossed negative-begin complete long clamp cell at a nonpositive end offset + +A forward complete result with `end_.key < begin.key`, a negative begin offset and +`end.offset ≤ 0`, where the begin selector clamps. Unlike the short case, the gap +`[end_.key, begin.key)` lies *above* the end key, so agreement below `end_.key` does not +fix the gap. A compatible database stores `P` gap keys; the begin anchor is `P - C` +(with `C = beginSlack - reportedBelowBegin - clampNonposD`), so compatibility forces +`P ≤ C`. The upper endpoint of the minimal range is `end_.key` when no compatible +database can fill the gap (`cap < C`, or `cap = C` with the reported head at the empty +key), and `begin.key` otherwise. +-/ + +namespace FormalConflictRange + +/-- The capacity slack `C = beginSlack - reportedBelowBegin - clampNonposD` of the +crossed negative-begin complete long clamp cell. -/ +def clampLongC (r : RangeRead) (res : Result) : ℕ := + beginSlack r - reportedBelowBegin r res - clampNonposD r + +/-- The capacity `[end_.key, begin.key)` of the gap above the end key. -/ +def clampLongCap (r : RangeRead) : ℕ∞ := + intervalCard r.end_.key r.begin.key + +/-- The upper endpoint of the crossed negative-begin complete long clamp cell. -/ +noncomputable def clampLongUpper (r : RangeRead) (res : Result) : WithTop Key := + if clampLongCap r < (clampLongC r res : ℕ∞) ∨ + (clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = []) + then r.end_.key else r.begin.key + +/-- The crossed negative-begin complete long clamp cell at `end.offset ≤ 0`. -/ +abbrev negCrossClampLongNonpos (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ clampNonposBottom r res ∧ + r.end_.key < r.begin.key + +/-- The closed form for the crossed negative-begin complete long clamp cell. -/ +noncomputable def closedFormNegCrossClampLongNonpos (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), clampLongUpper r res⟩ + +section Structural + +variable {r : RangeRead} {res : Result} + +theorem clampLong_headD_eq {hne : res.keys ≠ []} : + res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + +/-- Every reported key lies below `end_.key` in the crossed negative-begin complete +long clamp cell. -/ +theorem clampLong_reported_lt_end (hread : Readable r res) + (hrev : r.reverse = false) (hmore : res.more = false) (hend : r.end_.offset ≤ 0) : + ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + obtain ⟨db, hcomp⟩ := hread + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + intro k hk + have hklt := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hk)).2.2 + exact lt_of_lt_of_le hklt (resolve_le_key_of_offset_nonpos hend) + +theorem clampLong_reported_eq_length (hread : Readable r res) + (hcell : negCrossClampLongNonpos r res) : + reportedBelowBegin r res = res.keys.length := by + obtain ⟨hrev, -, hmore, -, hend, -, -, -, hlong⟩ := hcell + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_trans (clampLong_reported_lt_end hread hrev hmore hend k hk) hlong + +theorem clampLong_nat_bottom (hcell : negCrossClampLongNonpos r res) : + reportedBelowBegin r res + (clampNonposD r + 1) ≤ beginSlack r := by + obtain ⟨-, -, -, -, -, -, -, hbottom, hlong⟩ := hcell + have hcap0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le (le_of_lt hlong) + have h := hbottom + unfold clampNonposBottom at h + rw [hcap0] at h + have h' : ((reportedBelowBegin r res + (clampNonposD r + 1) : ℕ) : ℕ∞) ≤ + (beginSlack r : ℕ∞) := by + simpa [ENat.natCast_add, add_assoc] using h + exact ENat.natCast_le_natCast.mp h' + +theorem clampLong_C_pos (hcell : negCrossClampLongNonpos r res) : + 0 < clampLongC r res := by + have hnat := clampLong_nat_bottom hcell + unfold clampLongC + omega + +/-- The complete window of a compatible database is the half-open interval between the +resolutions. -/ +theorem clampLong_complete_eq_between {db : DB} (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = false) (hmore : res.more = false) : + res.keys = between db (resolve db r.begin) (resolve db r.end_) := by + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + rw [hres_eq, RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + unfold RangeRead.inRange between + rfl + +/-- The begin anchor in terms of the gap count, once the count below the begin +resolution is known. -/ +theorem clampLong_anchor_eq {db : DB} (hread : Readable r res) + (hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res) + {b : ℕ} (hb : cnt db (resolve db r.begin) = b) : + r.begin.anchor db = + (b : ℤ) + ((between db r.end_.key r.begin.key).length : ℤ) - + (clampLongC r res : ℤ) := by + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, -, -, -, hlong⟩ := hcell + have hresb := clampLong_complete_eq_between hcomp hrev hmore + have hle : resolve db r.begin ≤ resolve db r.end_ := by + obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne + have hk' : k ∈ between db (resolve db r.begin) (resolve db r.end_) := hresb ▸ hk + unfold between at hk' + rw [List.mem_filter] at hk' + rcases (of_decide_eq_true hk'.2) with ⟨h1, h2⟩ + exact le_of_lt (lt_of_le_of_lt h1 h2) + have ht : (between db (resolve db r.begin) (resolve db r.end_)).length = res.keys.length := by + rw [← hresb] + have hcntR : cnt db (resolve db r.end_) = b + res.keys.length := by + have h := cnt_eq_add_between db hle + rw [hb, ht] at h + omega + have hRle : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + have hhead : res.keys.head hne ∈ between db (resolve db r.begin) (resolve db r.end_) := + hresb ▸ List.head_mem hne + unfold between at hhead + rw [List.mem_filter] at hhead + rw [hR] at hhead + exact absurd (of_decide_eq_true hhead.2).2 + (not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne))) + have hd : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := + between_resolve_end_length hend hRne + have hcntE : cnt db r.end_.key = b + res.keys.length + (clampNonposD r + 1) := by + have h := cnt_eq_add_between db hRle + rw [hcntR, hd] at h + omega + have hEB : r.end_.key ≤ r.begin.key := le_of_lt hlong + have hcntB : cnt db r.begin.key = + b + res.keys.length + (clampNonposD r + 1) + + (between db r.end_.key r.begin.key).length := by + have h := cnt_eq_add_between db hEB + rw [hcntE] at h + omega + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have hcntB' : db.keys.countP + (fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) = + b + res.keys.length + (clampNonposD r + 1) + + (between db r.end_.key r.begin.key).length := by + simpa [cnt] using hcntB + rw [hcntB'] + have hrep : reportedBelowBegin r res = res.keys.length := + clampLong_reported_eq_length hread hcell_keep + have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + rw [← hrep] + exact clampLong_nat_bottom hcell_keep + have hC : (clampLongC r res : ℤ) = + (beginSlack r : ℤ) - (res.keys.length : ℤ) - (clampNonposD r : ℤ) := by + unfold clampLongC + rw [hrep] + omega + rw [hC] + push_cast + ring + +/-- In the long clamp cell the gap count equals the capacity slack for any compatible +database with a nonnegative begin anchor. Such a database is nonclamping, resolves the +begin selector onto the reported head, and its anchor is exactly the number of stored +keys below that head; the anchor identity then forces the gap count to `C`. -/ +theorem clampLong_P_eq_C_of_anchor_nonneg {db : DB} (hread : Readable r res) + (hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res) + (hanchor : 0 ≤ r.begin.anchor db) : + (between db r.end_.key r.begin.key).length = clampLongC r res := by + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, -, -, -, -, -⟩ := hcell + set head : Key := res.keys.head hne with hhead + have hL : resolve db r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_anchor_nonneg hcomp hrev hmore hne hanchor + have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + rw [hL]; exact WithTop.coe_ne_top + 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 hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor hltlen + have hidx : db.keys[(r.begin.anchor db).toNat] = head := by + have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) = + ((head : Key) : WithTop Key) := by rw [← hget, hL] + exact WithTop.coe_injective hcoe + have hrank : db.keys.countP (fun k => decide (k < head)) = (r.begin.anchor db).toNat := + countP_lt_eq_of_getElem db.sorted hltlen hidx + have hb : cnt db (resolve db r.begin) = (r.begin.anchor db).toNat := by + rw [hL] + unfold cnt + simpa using hrank + have hEq := clampLong_anchor_eq hread hcell_keep hcomp hb + have hEq' : (r.begin.anchor db : ℤ) = + ((r.begin.anchor db).toNat : ℤ) + + ((between db r.end_.key r.begin.key).length : ℤ) - + (clampLongC r res : ℤ) := hEq + rw [Int.toNat_of_nonneg hanchor] at hEq' + have hP : ((between db r.end_.key r.begin.key).length : ℤ) = (clampLongC r res : ℤ) := by + omega + exact_mod_cast hP + +/-- In the long clamp cell a compatible database clamps exactly when its gap count is +below the capacity slack `C`. -/ +theorem clampLong_anchor_neg_iff {db : DB} (hread : Readable r res) + (hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res) : + r.begin.anchor db < 0 ↔ (between db r.end_.key r.begin.key).length < clampLongC r res := by + constructor + · intro hneg + have hb : cnt db (resolve db r.begin) = 0 := by + rw [resolve_eq_nil hneg]; exact cnt_nil db + have hEq := clampLong_anchor_eq hread hcell hcomp hb + have hEq' : (r.begin.anchor db : ℤ) = + (0 : ℤ) + ((between db r.end_.key r.begin.key).length : ℤ) - + (clampLongC r res : ℤ) := hEq + have : ((between db r.end_.key r.begin.key).length : ℤ) < (clampLongC r res : ℤ) := by + omega + exact_mod_cast this + · intro hP + by_contra hcon + have hanchor : 0 ≤ r.begin.anchor db := le_of_not_gt hcon + have hEq := clampLong_P_eq_C_of_anchor_nonneg hread hcell hcomp hanchor + omega + +/-- **A clamping compatible database exists in the long clamp cell.** Take the reported +keys and the `d + 1` pads that any compatible database stores in +`[resolve end, end_.key)`; no gap keys are stored, so the begin anchor is `-C < 0`. -/ +theorem exists_clampLong_db {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) : + ∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by + have hread_keep : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2 + have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hbot + rw [hbot] at hlast_lt_R₀ + exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _)) + set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hLdef + have hLlen : L.length = clampNonposD r + 1 := by + rw [hLdef]; exact between_resolve_end_length hend hR_ne_nil + have hLne : L ≠ [] := by rw [← List.length_pos_iff, hLlen]; omega + have hLsorted : L.SortedLT := + List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + rw [hLdef] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hres_lt_L : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by + intro x hx y hy + have hxle : ((x : Key) : WithTop Key) ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (hlast_le x hx) + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (lt_of_le_of_lt hxle hlast_lt_R₀) (hLmem y hy).1) + have hAppSorted : (res.keys ++ L).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hLsorted, hres_lt_L⟩ + set db : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdb + have hrep : reportedBelowBegin r res = res.keys.length := + clampLong_reported_eq_length hread_keep ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, + hbottom, hlong⟩ + have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + rw [← hrep] + exact clampLong_nat_bottom ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ + have hslack : beginSlack r = res.keys.length + clampLongC r res + clampNonposD r := by + unfold clampLongC + rw [hrep] + omega + have hR_lt_E : resolve db₀ r.end_ < r.end_.key := + lt_of_le_of_lt (hLmem _ (List.head_mem hLne)).1 (hLmem _ (List.head_mem hLne)).2 + have hdcast : (clampNonposD r : ℤ) = -r.end_.offset := by + rw [clampNonposD_eq] + rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ -r.end_.offset by omega)] + have hslackcast : (beginSlack r : ℤ) = -r.begin.offset := by + unfold beginSlack + rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ 0 - r.begin.offset by omega)] + ring + have hcntE : cnt db r.end_.key = res.keys.length + (clampNonposD r + 1) := by + unfold cnt + rw [List.countP_eq_length_filter] + have hdbk : db.keys = res.keys ++ L := by rw [hdb] + rw [hdbk] + have hf : (res.keys ++ L).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = res.keys ++ L := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_trans hlast_lt_R₀ hR_lt_E) + · exact (hLmem x hx).2 + rw [hf, List.length_append, hLlen] + have hcntB : cnt db r.begin.key = res.keys.length + (clampNonposD r + 1) := by + unfold cnt + rw [List.countP_eq_length_filter] + have hdbk : db.keys = res.keys ++ L := by rw [hdb] + rw [hdbk] + have hf : (res.keys ++ L).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = res.keys ++ L := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · exact lt_trans (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_trans hlast_lt_R₀ hR_lt_E)) hlong + · exact lt_trans (hLmem x hx).2 hlong + rw [hf, List.length_append, hLlen] + have hanchorE : r.end_.anchor db = (res.keys.length : ℤ) := by + unfold Selector.anchor + have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.end_.key)) = + res.keys.length + (clampNonposD r + 1) := by simpa [cnt] using hcntE + rw [hc] + push_cast + rw [hdcast] + ring + have hanchorE_nonneg : 0 ≤ r.end_.anchor db := by rw [hanchorE]; exact Int.natCast_nonneg _ + have htoNatE : (r.end_.anchor db).toNat = res.keys.length := by + rw [hanchorE, Int.toNat_natCast] + have hltlenE : (r.end_.anchor db).toNat < db.keys.length := by + rw [htoNatE, hdb] + simp only [List.length_append] + omega + have hdbk : db.keys = res.keys ++ L := by rw [hdb] + have hRE : resolve db r.end_ = ((L.head hLne : Key) : WithTop Key) := by + rw [resolve_eq_getElem hanchorE_nonneg (by rw [htoNatE, hdbk]; simp only [List.length_append]; omega)] + simp only [htoNatE, hdbk] + rw [List.getElem_append_right (show res.keys.length ≤ res.keys.length by omega)] + simp [List.head_eq_getElem] + have hanchorB : r.begin.anchor db < 0 := by + unfold Selector.anchor + have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.length + (clampNonposD r + 1) := by simpa [cnt] using hcntB + rw [hc] + have hCpos : 0 < clampLongC r res := + clampLong_C_pos ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ + have hbo : (r.begin.offset : ℤ) = -((res.keys.length + clampLongC r res + + clampNonposD r : ℕ) : ℤ) := by + omega + rw [hbo] + push_cast + omega + have hLB : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorB + have hcomplete : res.keys = RangeRead.completeKeys db r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hLB, hRE, hdbk] + have hf : (res.keys ++ L).filter (fun k : Key => decide + ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : + WithTop Key))) = res.keys := by + rw [List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide + ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : + WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + refine ⟨coe_nil_le_key_withTop x, ?_⟩ + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (hres_lt_L x hx _ + (List.head_mem _))) le_rfl + have h2 : L.filter (fun k : Key => decide + ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : + WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq, not_and] + intro _ + exact not_lt.mpr (WithTop.coe_le_coe.mpr + ((hLsorted.pairwise.imp le_of_lt).rel_head hx)) + rw [h1, h2, List.append_nil] + rw [hf] + exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hcomplete⟩⟩, hanchorB⟩ + +/-- **A compatible database with prescribed below-head keys and gap keys.** Given a +sorted list `H` of keys below the reported head and a sorted list `G` of gap keys in +`[end_.key, begin.key)` of length exactly the capacity slack `C`, the concatenation +`H ++ reported ++ pads ++ G` (with the pads coming from any compatible database) is +compatible. Its begin anchor is `H.length`, so it resolves onto the reported head. -/ +theorem exists_long_compat_db {r : RangeRead} {res : Result} {db₀ : DB} + (hcomp₀ : RangeRead.Compatible db₀ r res) + (hcell : negCrossClampLongNonpos r res) + (H G : List Key) + (hHsorted : H.SortedLT) + (hHlt : ∀ x ∈ H, x < res.keys.headD ([] : Key)) + (hGsorted : G.SortedLT) + (hGlo : ∀ x ∈ G, r.end_.key ≤ ((x : Key) : WithTop Key)) + (hGhi : ∀ x ∈ G, ((x : Key) : WithTop Key) < r.begin.key) + (hGlen : G.length = clampLongC r res) : + ∃ db : DB, RangeRead.Compatible db r res ∧ + db.keys = H ++ res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key ++ G := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2 + have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hbot + rw [hbot] at hlast_lt_R₀ + exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _)) + have hR_le_E : resolve db₀ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hLdef + have hLlen : L.length = clampNonposD r + 1 := by + rw [hLdef]; exact between_resolve_end_length hend hR_ne_nil + have hLne : L ≠ [] := by rw [← List.length_pos_iff, hLlen]; omega + have hLsorted : L.SortedLT := + List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + rw [hLdef] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hheadD_eq : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hhead_le : ∀ x ∈ res.keys, res.keys.head hne ≤ x := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_head hx + have hhead_lt_R : ((res.keys.head hne : Key) : WithTop Key) < resolve db₀ r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hhead_le _ (List.getLast_mem hne))) + hlast_lt_R₀ + have hhead_lt_E : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hhead_lt_R hR_le_E + have hHlt' : ∀ x ∈ H, x < res.keys.head hne := by + intro x hx + have := hHlt x hx + rwa [hheadD_eq] at this + have hHltE : ∀ x ∈ H, ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + exact lt_trans (WithTop.coe_lt_coe.mpr (hHlt' x hx)) hhead_lt_E + have hres_lt_L : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (lt_of_le_of_lt + (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlast_lt_R₀) (hLmem y hy).1) + have hH_lt_res : ∀ x ∈ H, ∀ y ∈ res.keys, x < y := by + intro x hx y hy + exact lt_of_lt_of_le (hHlt' x hx) (hhead_le y hy) + have hHsorted_res : (H ++ res.keys).SortedLT := + List.sortedLT_append.mpr ⟨hHsorted, hsort, hH_lt_res⟩ + have hA_lt_L : ∀ x ∈ H ++ res.keys, ∀ y ∈ L, x < y := by + intro x hx y hy + rcases List.mem_append.mp hx with hx | hx + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (lt_trans (WithTop.coe_lt_coe.mpr (hHlt' x hx)) hhead_lt_R) (hLmem y hy).1) + · exact hres_lt_L x hx y hy + have hB_sorted : ((H ++ res.keys) ++ L).SortedLT := + List.sortedLT_append.mpr ⟨hHsorted_res, hLsorted, hA_lt_L⟩ + have hB_lt_G : ∀ x ∈ (H ++ res.keys) ++ L, ∀ y ∈ G, x < y := by + intro x hx y hy + have hyE := hGlo y hy + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltE x hx) hyE) + · exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlast_lt_R₀ (le_trans hR_le_E hyE))) + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hLmem x hx).2 hyE) + have hAppSorted : (((H ++ res.keys) ++ L) ++ G).SortedLT := + List.sortedLT_append.mpr ⟨hB_sorted, hGsorted, hB_lt_G⟩ + set B : List Key := (H ++ res.keys) ++ L with hBdef + set db : DB := ⟨B ++ G, hAppSorted⟩ with hdb + have hdbk : db.keys = B ++ G := by rw [hdb] + have hrep : reportedBelowBegin r res = res.keys.length := + clampLong_reported_eq_length ⟨db₀, hcomp₀⟩ + ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ + have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + rw [← hrep] + exact clampLong_nat_bottom ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ + have hslack : beginSlack r = res.keys.length + clampLongC r res + clampNonposD r := by + unfold clampLongC + rw [hrep] + omega + have hdcast : (clampNonposD r : ℤ) = -r.end_.offset := by + rw [clampNonposD_eq] + rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ -r.end_.offset by omega)] + have hslackcast : (beginSlack r : ℤ) = -r.begin.offset := by + unfold beginSlack + rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ 0 - r.begin.offset by omega)] + ring + have hBlength : B.length = H.length + res.keys.length + (clampNonposD r + 1) := by + rw [hBdef] + simp only [List.length_append] + rw [hLlen] + have hcntE : cnt db r.end_.key = B.length := by + unfold cnt + rw [List.countP_eq_length_filter, hdbk] + have hBf : B.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = B := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hBdef] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact hHltE x hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlast_lt_R₀ hR_le_E) + · exact (hLmem x hx).2 + have hGf : G.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hGlo x hx) + rw [List.filter_append, hBf, hGf, List.append_nil] + have hcntB : cnt db r.begin.key = B.length + G.length := by + unfold cnt + rw [List.countP_eq_length_filter, hdbk] + have hBf : B.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = B := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hBdef] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact lt_trans (hHltE x hx) hlong + · exact lt_trans (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlast_lt_R₀ hR_le_E)) hlong + · exact lt_trans (hLmem x hx).2 hlong + have hGf : G.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = G := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hGhi x hx + rw [List.filter_append, hBf, hGf, List.length_append] + have hanchorE : r.end_.anchor db = ((H.length + res.keys.length : ℕ) : ℤ) := by + unfold Selector.anchor + have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.end_.key)) = B.length := by + simpa [cnt] using hcntE + rw [hc, hBlength] + push_cast + rw [hdcast] + ring + have htoNatE : (r.end_.anchor db).toNat = H.length + res.keys.length := by + rw [hanchorE] + exact Int.toNat_natCast _ + have hanchorE_nonneg : 0 ≤ r.end_.anchor db := by rw [hanchorE]; exact Int.natCast_nonneg _ + have hltlenE : (r.end_.anchor db).toNat < db.keys.length := by + rw [htoNatE, hdbk, List.length_append, hBlength]; omega + have hgetE : db.keys[(r.end_.anchor db).toNat]? = some (L.head hLne) := by + rw [htoNatE, hdbk] + rw [List.getElem?_append_left (show H.length + res.keys.length < B.length by + rw [hBlength]; omega)] + rw [hBdef] + rw [List.getElem?_append_right (show (H ++ res.keys).length ≤ H.length + res.keys.length by + simp)] + have hsub : H.length + res.keys.length - (H ++ res.keys).length = 0 := by simp + rw [hsub] + simp [List.head_eq_getElem] + have hvalE : db.keys[(r.end_.anchor db).toNat]'(hltlenE) = L.head hLne := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hltlenE)] + exact hgetE + have hRE : resolve db r.end_ = ((L.head hLne : Key) : WithTop Key) := by + rw [resolve_eq_getElem hanchorE_nonneg hltlenE] + exact congrArg (fun z => ((z : Key) : WithTop Key)) hvalE + have hanchorB : r.begin.anchor db = ((H.length : ℕ) : ℤ) := by + unfold Selector.anchor + have hc : db.keys.countP (fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) = + B.length + G.length := by simpa [cnt] using hcntB + rw [hc] + have hbo : (r.begin.offset : ℤ) = -((res.keys.length + clampLongC r res + + clampNonposD r : ℕ) : ℤ) := by omega + rw [hbo, hBlength, hGlen] + push_cast + ring + have hltlenB : (r.begin.anchor db).toNat < db.keys.length := by + rw [hanchorB, Int.toNat_natCast, hdbk, List.length_append, hBlength]; omega + have hgetB : db.keys[(r.begin.anchor db).toNat]? = some (res.keys.head hne) := by + rw [hanchorB, Int.toNat_natCast, hdbk] + rw [List.getElem?_append_left (show H.length < B.length by rw [hBlength]; omega)] + rw [hBdef] + rw [List.getElem?_append_left (show H.length < (H ++ res.keys).length by + rw [List.length_append]; have := List.length_pos_of_ne_nil hne; omega)] + rw [List.getElem?_append_right (show H.length ≤ H.length by omega)] + rw [show H.length - H.length = 0 by omega] + rw [List.getElem?_eq_getElem (h := List.length_pos_iff.mpr hne)] + exact congrArg some (List.head_eq_getElem hne).symm + have hvalB : db.keys[(r.begin.anchor db).toNat]'(hltlenB) = res.keys.head hne := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hltlenB)] + exact hgetB + have hLB : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + rw [resolve_eq_getElem (by rw [hanchorB]; exact Int.natCast_nonneg _) hltlenB] + exact congrArg (fun z => ((z : Key) : WithTop Key)) hvalB + have hcomplete : res.keys = RangeRead.completeKeys db r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hLB, hRE, hdbk] + have hf : (B ++ G).filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = res.keys := by + rw [hBdef] + rw [List.filter_append, List.filter_append, List.filter_append] + have hHf : H.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro h + exact not_le.mpr (hHlt' x hx) (WithTop.coe_le_coe.mp h.1) + have hresf : res.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (hhead_le x hx), + WithTop.coe_lt_coe.mpr (hres_lt_L x hx _ (List.head_mem hLne))⟩ + have hLf : L.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_and.mpr (fun _ => not_lt.mpr (WithTop.coe_le_coe.mpr + ((hLsorted.pairwise.imp le_of_lt).rel_head hx))) + have hGf : G.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((L.head hLne : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_and.mpr (fun _ => not_lt.mpr + (le_trans (le_of_lt (hLmem _ (List.head_mem hLne)).2) (hGlo x hx))) + rw [hHf, hresf, hLf, hGf] + simp + rw [hf] + exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hcomplete⟩⟩, by rw [hdbk, hBdef]⟩ + +/-- A sorted database that stores the empty key has the empty key as its first key. -/ +theorem keys_getElem_zero_eq_nil_of_mem {db : DB} (h : ([] : Key) ∈ db.keys) : + db.keys[0]'(List.length_pos_of_mem h) = ([] : Key) := by + obtain ⟨i, hi, hidx⟩ := List.getElem_of_mem h + have hcount := countP_lt_eq_of_getElem db.sorted hi hidx + have hzero : db.keys.countP (fun k => decide (k < ([] : Key))) = 0 := by + rw [List.countP_eq_zero] + intro k _ + simp + rw [hzero] at hcount + have hi0 : i = 0 := hcount.symm + subst i + exact hidx + +/-- Under the short-gap condition every compatible database clamps the begin selector: +below the end key the short-gap condition rules out a full gap, and the empty-key +exception makes a nonclamping resolution land on the empty key anyway. -/ +theorem resolve_begin_eq_nil_of_clampLong_short {db : DB} (hread : Readable r res) + (hcell : negCrossClampLongNonpos r res) (hcomp : RangeRead.Compatible db r res) + (hbranch : clampLongCap r < (clampLongC r res : ℕ∞) ∨ + (clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = [])) : + resolve db r.begin = (([] : Key) : WithTop Key) := by + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + by_cases hclamp : r.begin.anchor db < 0 + · exact resolve_eq_nil hclamp + · have hnn : 0 ≤ r.begin.anchor db := le_of_not_gt hclamp + have hP := clampLong_P_eq_C_of_anchor_nonneg hread hcell_keep hcomp hnn + have hle : ((clampLongC r res : ℕ) : ℕ∞) ≤ clampLongCap r := by + rw [← hP] + simpa [clampLongCap] using + between_length_le_intervalCard (db := db) (E := r.end_.key) (B := r.begin.key) + (le_of_lt hlong) + rcases hbranch with hlt | ⟨heq, hhead⟩ + · exact absurd (lt_of_le_of_lt hle hlt) (lt_irrefl _) + · rw [resolve_begin_eq_head_of_anchor_nonneg hcomp hrev hmore hne hnn, + ← clampLong_headD_eq (hne := hne)] + exact congrArg (fun k : Key => ((k : Key) : WithTop Key)) hhead + +/-- When the begin selector clamps, the count below the end key is the reported count +plus the `d + 1` end pads. -/ +theorem clampLong_cnt_end_of_clamp {db : DB} (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hend : r.end_.offset ≤ 0) (hL : resolve db r.begin = (([] : Key) : WithTop Key)) : + cnt db r.end_.key = res.keys.length + (clampNonposD r + 1) := by + have hresb := clampLong_complete_eq_between hcomp hrev hmore + have ht : (between db (resolve db r.begin) (resolve db r.end_)).length = res.keys.length := by + rw [← hresb] + rw [hL] at ht + have hcntR : cnt db (resolve db r.end_) = res.keys.length := by + have h := cnt_eq_add_between db (w₁ := (([] : Key) : WithTop Key)) + (w₂ := resolve db r.end_) (coe_nil_le_key_withTop _) + rw [cnt_nil, Nat.zero_add, ht] at h + exact h + have hRle : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + have hhead : res.keys.head hne ∈ between db (resolve db r.begin) (resolve db r.end_) := + hresb ▸ List.head_mem hne + unfold between at hhead + rw [List.mem_filter] at hhead + rw [hL, hR] at hhead + exact absurd (of_decide_eq_true hhead.2).2 + (not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne))) + have hd : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := + between_resolve_end_length hend hRne + have h := cnt_eq_add_between db hRle + rw [hcntR, hd] at h + exact h + +/-- The begin anchor of any database in terms of its gap count, once the count below the +end key is known to be the reported count plus the end pads. -/ +theorem clampLong_anchor_eq_of_cnt {db : DB} (hread : Readable r res) + (hcell : negCrossClampLongNonpos r res) + (hcnt : cnt db r.end_.key = res.keys.length + (clampNonposD r + 1)) : + r.begin.anchor db = + ((between db r.end_.key r.begin.key).length : ℤ) - (clampLongC r res : ℤ) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + have hEB : r.end_.key ≤ r.begin.key := le_of_lt hlong + have hsplit := cnt_eq_add_between db hEB + have hcntB : db.keys.countP + (fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.length + (clampNonposD r + 1) + + (between db r.end_.key r.begin.key).length := by + have h := hsplit + rw [hcnt] at h + unfold cnt at h + exact h + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcntB] + have hrep : reportedBelowBegin r res = res.keys.length := + clampLong_reported_eq_length hread + ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ + have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + rw [← hrep] + exact clampLong_nat_bottom + ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ + have hC : (clampLongC r res : ℤ) = + (beginSlack r : ℤ) - (res.keys.length : ℤ) - (clampNonposD r : ℤ) := by + unfold clampLongC + rw [hrep] + omega + rw [hC] + push_cast + ring + +/-- **Soundness of `[⊥, end_.key)` in the crossed negative-begin complete long clamp +cell.** Under the short-gap condition every database with the same keys below +`end_.key` also clamps the begin selector: the gap can contribute at most `cap ≤ C` +keys, and in the equality case the reported head is the empty key, which the agreed +prefix stores below `end_.key`. The end selector is fixed by agreement below +`end_.key`, and the complete window below it is then determined. -/ +theorem sound_clampLong_nonpos_end {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) + (hbranch : clampLongCap r < (clampLongC r res : ℕ∞) ∨ + (clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = [])) : + Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + have hhead_eq : res.keys.headD ([] : Key) = res.keys.head hne := + clampLong_headD_eq (hne := hne) + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have step : ∀ db₁ db₂, AgreeOn (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : + ConflictRange) db₁ db₂ → RangeRead.Compatible db₁ r res → + RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hfilt : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) := + resolve_begin_eq_nil_of_clampLong_short hread hcell_keep hcomp₁ hbranch + have hcnt₁ : cnt db₁ r.end_.key = res.keys.length + (clampNonposD r + 1) := + clampLong_cnt_end_of_clamp hcomp₁ hrev hmore hne hend hL₁ + have hcnt₂ : cnt db₂ r.end_.key = res.keys.length + (clampNonposD r + 1) := by + rw [← hcnt₁] + unfold cnt + rw [List.countP_eq_length_filter, List.countP_eq_length_filter, hfilt] + have hanchor₂ : r.begin.anchor db₂ = + ((between db₂ r.end_.key r.begin.key).length : ℤ) - (clampLongC r res : ℤ) := + clampLong_anchor_eq_of_cnt hread hcell_keep hcnt₂ + have hPle : ((between db₂ r.end_.key r.begin.key).length : ℕ∞) ≤ clampLongCap r := + between_length_le_intervalCard (le_of_lt hlong) + have hL₂ : resolve db₂ r.begin = (([] : Key) : WithTop Key) := by + rcases hbranch with hlt | ⟨heq, hheadD⟩ + · have hlt' : ((between db₂ r.end_.key r.begin.key).length : ℕ∞) < + (clampLongC r res : ℕ∞) := lt_of_le_of_lt hPle hlt + have hlt'' : (between db₂ r.end_.key r.begin.key).length < clampLongC r res := + ENat.natCast_lt_natCast.mp hlt' + exact resolve_eq_nil (by rw [hanchor₂]; omega) + · have hPle' : (between db₂ r.end_.key r.begin.key).length ≤ clampLongC r res := by + have h := hPle + rw [heq] at h + exact ENat.natCast_le_natCast.mp h + have hAle : r.begin.anchor db₂ ≤ 0 := by rw [hanchor₂]; omega + have hhead_nil : res.keys.head hne = ([] : Key) := by + rw [← hhead_eq]; exact hheadD + have hltE : (([] : Key) : WithTop Key) < r.end_.key := by + rw [← hhead_nil] + exact clampLong_reported_lt_end hread hrev hmore hend _ hhead_mem + have hnil₁ : ([] : Key) ∈ db₁.keys := by + have hck : ([] : Key) ∈ RangeRead.completeKeys db₁ r := by + rw [← hres_eq, ← hhead_nil] + exact hhead_mem + revert hck + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + intro hck + exact (List.mem_filter.mp hck).1 + have hnil₂ : ([] : Key) ∈ db₂.keys := by + have hmem : ([] : Key) ∈ + db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [List.mem_filter] + refine ⟨hnil₁, ?_⟩ + rw [decide_eq_true_eq] + exact hltE + rw [hfilt] at hmem + exact (List.mem_filter.mp hmem).1 + rcases lt_or_eq_of_le hAle with hneg | hzero + · exact resolve_eq_nil hneg + · have hpos : 0 < db₂.keys.length := List.length_pos_of_mem hnil₂ + have hltlen : (r.begin.anchor db₂).toNat < db₂.keys.length := by + rw [hzero]; simpa using hpos + rw [resolve_eq_getElem (le_of_eq hzero.symm) hltlen] + have hz? : db₂.keys[(r.begin.anchor db₂).toNat]? = some ([] : Key) := by + rw [hzero, Int.toNat_zero] + rw [List.getElem?_eq_getElem (h := List.length_pos_of_mem hnil₂)] + exact congrArg some (keys_getElem_zero_eq_nil_of_mem hnil₂) + have hz : db₂.keys[(r.begin.anchor db₂).toNat]'(hltlen) = ([] : Key) := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hltlen)] + exact hz? + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hz + have hR : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.end_) + hend le_rfl hfilt + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_filter_of_end_nonpos (db := db₁) hrev hend, + completeKeys_eq_filter_of_end_nonpos (db := db₂) hrev hend, + hfilt, hL₁, hL₂, hR] + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Soundness of `[⊥, begin.key)` in the crossed negative-begin complete long clamp +cell.** Agreement below `begin.key` fixes both selector resolutions (both selector +keys are at or below it) and every complete window lies below `begin.key`. -/ +theorem sound_clampLong_nonpos_begin {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) (hmore : res.more = false) + (hlong : r.end_.key < r.begin.key) : + Sound r res (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) := by + have step : ∀ db₁ db₂, AgreeOn (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : + ConflictRange) db₁ db₂ → RangeRead.Compatible db₁ r res → + RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hfilt : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + have hL : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.begin.key) (s := r.begin) + (le_of_lt hbegin) le_rfl hfilt + have hR : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.begin.key) (s := r.end_) + hend (le_of_lt hlong) hfilt + have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hP : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + have h₁ : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + (db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key))).filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_ + · have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le + have hkB : (k : WithTop Key) < r.begin.key := lt_trans hkE hlong + simp [hk, hkB] + · simp [hk] + have h₂ : db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + (db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.begin.key))).filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_ + · have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le + have hkB : (k : WithTop Key) < r.begin.key := lt_trans hkE hlong + simp [hk, hkB] + · simp [hk] + rw [h₁, h₂, hfilt] + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), ← hL, ← hR] + exact hP + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- A database of `m` keys in `[B, E)` that contains a prescribed key `k ∈ [B, E)`, +whenever the interval has exactly `m` keys. -/ +theorem exists_db_keysIn_mem {B E : WithTop Key} {k : Key} {m : ℕ} + (hcard : (m : ℕ∞) = intervalCard B E) (hk : k ∈ keysIn B E) : + ∃ db : DB, (∀ x ∈ db.keys, x ∈ keysIn B E) ∧ k ∈ db.keys ∧ + db.keys.length = m := by + obtain ⟨db, hrange, hlen⟩ := exists_db_keysIn (B := B) (E := E) (m := m) (le_of_eq hcard) + have hkdb : k ∈ db.keys := by + by_contra hknot + have hsub : ((db.keys.toFinset ∪ {k} : Finset Key) : Set Key) ⊆ keysIn B E := by + intro x hx + simp only [Finset.mem_coe, Finset.mem_union, List.mem_toFinset, + Finset.mem_singleton] at hx + rcases hx with hx | rfl + · exact hrange x hx + · exact hk + have hcardle : ((db.keys.toFinset ∪ {k} : Finset Key).card : ℕ∞) ≤ intervalCard B E := by + rw [intervalCard_eq_encard_keysIn, ← Set.encard_coe_eq_coe_finsetCard] + exact Set.encard_le_encard hsub + have hcard' : (db.keys.toFinset ∪ {k} : Finset Key).card = m + 1 := by + rw [Finset.card_union_of_disjoint] + · rw [List.toFinset_card_of_nodup db.nodup, hlen]; simp + · rw [Finset.disjoint_singleton_right, List.mem_toFinset] + exact hknot + rw [hcard', ← hcard] at hcardle + exact absurd hcardle + (not_le.mpr (ENat.natCast_lt_natCast.mpr (Nat.lt_succ_self m))) + exact ⟨db, hrange, hkdb, hlen⟩ + +/-- A database whose keys are the reported keys followed by a nonempty tail, with begin +anchor one, is incompatible: the begin resolution moves to the second stored key, so the +reported head leaves the complete window. -/ +theorem clampLong_not_compat_of_anchor_one {db : DB} {Tail : List Key} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) + (hkeys : db.keys = res.keys ++ Tail) (hTail : Tail ≠ []) + (hanchor : r.begin.anchor db = 1) : + ¬ RangeRead.Compatible db r res := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + have hlenpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlen : 1 < db.keys.length := by + rw [hkeys, List.length_append] + have := List.length_pos_of_ne_nil hTail + omega + have h0 : db.keys[0]'(by omega) = res.keys.head hne := by + have h0? : db.keys[0]? = some (res.keys.head hne) := by + rw [hkeys, List.getElem?_append_left hlenpos, ← List.head?_eq_getElem?, + List.head?_eq_some_head hne] + have hlen0 : 0 < db.keys.length := by omega + rw [List.getElem?_eq_getElem hlen0] at h0? + exact Option.some.inj h0? + have hstep : db.keys[0]'(by omega) < db.keys[1]'(by omega) := + (db.sorted.getElem_lt_getElem_iff).mpr (by omega) + have hgt : ((res.keys.head hne : Key) : WithTop Key) < + ((db.keys[1] : Key) : WithTop Key) := by + rw [← h0] + exact WithTop.coe_lt_coe.mpr hstep + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + rw [hanchor]; simpa using hlen + have hget : db.keys[(r.begin.anchor db).toNat]? = some (db.keys[1]'(by omega)) := by + rw [hanchor] + norm_num + have hval : db.keys[(r.begin.anchor db).toNat]'(hltlen) = db.keys[1]'(by omega) := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hltlen)] + exact hget + have hres_begin : resolve db r.begin = ((db.keys[1] : Key) : WithTop Key) := by + rw [resolve_eq_getElem (by rw [hanchor]; norm_num) hltlen] + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval + have hhead_lt_E : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := + clampLong_reported_lt_end hread hrev hmore hend _ (List.head_mem hne) + intro hcomp + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hnot : res.keys.head hne ∉ RangeRead.completeKeys db r := by + rw [completeKeys_eq_filter_of_end_nonpos (db := db) hrev hend] + intro hmem + simp only [List.mem_filter, decide_eq_true_eq] at hmem + have hle : ((db.keys[1] : Key) : WithTop Key) ≤ + ((res.keys.head hne : Key) : WithTop Key) := by + rw [← hres_begin]; exact hmem.2.1 + exact absurd hle (not_le.mpr hgt) + exact hnot (by rw [← hres_eq]; exact List.head_mem hne) + +/-- A database whose least key is the empty key, with begin anchor zero, is incompatible +with a complete result whose head is not the empty key: the begin resolution moves to the +empty key, which enters the complete window. -/ +theorem clampLong_not_compat_of_anchor_zero_bottom {db : DB} {Tail : List Key} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) + (hheadne : res.keys.headD ([] : Key) ≠ ([] : Key)) + (hkeys : db.keys = ([] : Key) :: Tail) + (hanchor : r.begin.anchor db = 0) : + ¬ RangeRead.Compatible db r res := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + have hhead : res.keys.head hne ≠ ([] : Key) := by + rw [← clampLong_headD_eq (hne := hne)]; exact hheadne + have hbot_lt_head : ([] : Key) < res.keys.head hne := by + cases hh : res.keys.head hne with + | nil => exact absurd hh hhead + | cons a t => exact List.nil_lt_cons a t + have hnil_lt_E : (([] : Key) : WithTop Key) < r.end_.key := by + have hh := clampLong_reported_lt_end hread hrev hmore hend _ (List.head_mem hne) + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head)) hh + have hlen : 0 < db.keys.length := by rw [hkeys]; simp + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + rw [hanchor]; simpa using hlen + have hget : db.keys[(r.begin.anchor db).toNat]? = some ([] : Key) := by + rw [hanchor] + norm_num + rw [hkeys] + simp + have hval : db.keys[(r.begin.anchor db).toNat]'(hltlen) = ([] : Key) := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hltlen)] + exact hget + have hres_begin : resolve db r.begin = (([] : Key) : WithTop Key) := by + rw [resolve_eq_getElem (by rw [hanchor]) hltlen] + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval + intro hcomp + 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 : res.keys.head hne ∈ RangeRead.completeKeys db r := by + rw [← hres_eq]; exact List.head_mem hne + rw [completeKeys_eq_filter_of_end_nonpos (db := db) hrev hend] at hhead_ck + simp only [List.mem_filter, decide_eq_true_eq] at hhead_ck + have hhead_lt_resEnd : ((res.keys.head hne : Key) : WithTop Key) < resolve db r.end_ := + hhead_ck.2.2 + have hnil_lt_resEnd : (([] : Key) : WithTop Key) < resolve db r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head)) hhead_lt_resEnd + have hnil_ck : ([] : Key) ∈ RangeRead.completeKeys db r := by + rw [completeKeys_eq_filter_of_end_nonpos (db := db) hrev hend] + rw [List.mem_filter] + refine ⟨?_, ?_⟩ + · rw [List.mem_filter] + exact ⟨by rw [hkeys]; exact List.mem_cons_self .. + , by rw [decide_eq_true_eq]; exact hnil_lt_E⟩ + · rw [decide_eq_true_eq] + exact ⟨by rw [hres_begin], hnil_lt_resEnd⟩ + have hnil_not_res : ([] : Key) ∉ res.keys := by + intro h + have hle := head_le_of_sortedLT' (RangeRead.compatible_sortedLT hcomp hrev) hne h + exact absurd hle (not_le.mpr hbot_lt_head) + exact hnil_not_res (by rw [hres_eq]; exact hnil_ck) + +/-- **Every gap key is necessary when the gap is strictly larger than the slack.** Use +a compatible database storing exactly `C` gap keys and avoiding `k`, then insert `k`: +the begin anchor rises to one, so the reported head leaves the complete window. -/ +theorem clampLong_necessary_gap_of_cap_gt {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) + (hgt : (clampLongC r res : ℕ∞) < clampLongCap r) + (hkE : r.end_.key ≤ ((k : Key) : WithTop Key)) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + have hread_keep : Readable r res := hread + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hcard : ((clampLongC r res + 1 : ℕ) : ℕ∞) ≤ clampLongCap r := by + rw [Nat.cast_add, Nat.cast_one] + exact (ENat.add_one_le_iff (by simp)).mpr hgt + obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) + (m := clampLongC r res) hcard + have hJlo : ∀ x ∈ dbJ.keys, r.end_.key ≤ ((x : Key) : WithTop Key) := + fun x hx => (hJrange x hx).1 + have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => (hJrange x hx).2 + obtain ⟨dbA, hAcomp, hAkeys⟩ := + exists_long_compat_db hcomp₀ hcell_keep [] dbJ.keys List.sortedLT_nil + (by intro x hx; simp at hx) dbJ.sorted hJlo hJhi hJlen + have hres_lt_E := clampLong_reported_lt_end hread_keep hrev hmore hend + have hPads_lt : ∀ x ∈ between db₀ (resolve db₀ r.end_) r.end_.key, + ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + exact (of_decide_eq_true (List.mem_filter.mp hx).2).2 + have hkA : k ∉ dbA.keys := by + rw [hAkeys] + intro hk + simp only [List.nil_append, List.mem_append] at hk + rcases hk with (hk | hk) | hk + · exact absurd hkE (not_le.mpr (hres_lt_E k hk)) + · exact absurd hkE (not_le.mpr (hPads_lt k hk)) + · exact hkJ hk + let Lb : List Key := dbJ.keys.orderedInsert (· ≤ ·) k + have hall_lt : ∀ a ∈ res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key, a < k := by + intro a ha + have haE : ((a : Key) : WithTop Key) < r.end_.key := by + rcases List.mem_append.mp ha with ha | ha + · exact hres_lt_E a ha + · exact hPads_lt a ha + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le haE hkE) + have hBkeys : (toggle dbA k).keys = + res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key ++ Lb := by + rw [toggle_eq_insert_of_not_mem hkA, DB.insert_keys, hAkeys] + simp only [List.nil_append] + rw [orderedInsert_append_of_all_lt hall_lt] + have hLblo : ∀ x ∈ Lb, r.end_.key ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hkE + · exact hJlo x hx + have hLbhi : ∀ x ∈ Lb, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hkB + · exact hJhi x hx + have hLblen : Lb.length = clampLongC r res + 1 := by + simp [Lb, List.orderedInsert_length, hJlen] + have hpadlen : (between db₀ (resolve db₀ r.end_) r.end_.key).length = + clampNonposD r + 1 := by + 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 : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := by + rw [← hres_eq₀]; exact List.head_mem hne + have hhead_lt_resEnd : ((res.keys.head hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hhead_ck).2.2 + have hRne : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + rw [hR] at hhead_lt_resEnd + exact absurd hhead_lt_resEnd + (not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne))) + exact between_resolve_end_length hend hRne + have hcntB : (toggle dbA k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.length + (clampNonposD r + 1) + Lb.length := by + rw [hBkeys] + rw [List.countP_append, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong) + have h2 : (between db₀ (resolve db₀ r.end_) r.end_.key).countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + (between db₀ (resolve db₀ r.end_) r.end_.key).length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_trans (hPads_lt x hx) hlong + have h3 : Lb.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = Lb.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hLbhi x hx + rw [h1, h2, h3, hpadlen] + have hreplen : reportedBelowBegin r res = res.keys.length := + clampLong_reported_eq_length hread_keep hcell_keep + have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + rw [← hreplen]; exact clampLong_nat_bottom hcell_keep + have hanchor : r.begin.anchor (toggle dbA k) = 1 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcntB] + have hslack : beginSlack r = + res.keys.length + clampLongC r res + clampNonposD r := by + unfold clampLongC + rw [hreplen] + omega + rw [hLblen, hslack] + push_cast + ring + have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res := + clampLong_not_compat_of_anchor_one (Tail := between db₀ (resolve db₀ r.end_) r.end_.key ++ Lb) + hread_keep hcell_keep (by rw [hBkeys, List.append_assoc]) (by + intro h + have hlen := congrArg List.length h + rw [List.length_append, hLblen] at hlen + simp at hlen) hanchor + exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩ + +/-- **Every gap key is necessary when the gap exactly matches the slack and the reported +head is not the empty key.** Use a compatible database with one bottom key below the +head and all gap keys stored, then erase `k`: the begin anchor returns to zero, the +resolution lands on the bottom key, which enters the complete window although it is not +reported. -/ +theorem clampLong_necessary_gap_of_cap_eq {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) + (hcap : clampLongCap r = (clampLongC r res : ℕ∞)) + (hheadne : res.keys.headD ([] : Key) ≠ ([] : Key)) + (hkE : r.end_.key ≤ ((k : Key) : WithTop Key)) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + have hread_keep : Readable r res := hread + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hhead : res.keys.head hne ≠ ([] : Key) := by + rw [← clampLong_headD_eq (hne := hne)]; exact hheadne + have hbot_lt_head : ([] : Key) < res.keys.head hne := by + cases hh : res.keys.head hne with + | nil => exact absurd hh hhead + | cons a t => exact List.nil_lt_cons a t + have hk_interval : k ∈ keysIn r.end_.key r.begin.key := ⟨hkE, hkB⟩ + obtain ⟨dbJ, hJrange, hkJmem, hJlen⟩ := + exists_db_keysIn_mem (B := r.end_.key) (E := r.begin.key) (k := k) + (m := clampLongC r res) (by unfold clampLongCap at hcap; exact hcap.symm) hk_interval + have hJlo : ∀ x ∈ dbJ.keys, r.end_.key ≤ ((x : Key) : WithTop Key) := + fun x hx => (hJrange x hx).1 + have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => (hJrange x hx).2 + have hnil_lt_head : (([] : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr hbot_lt_head + obtain ⟨dbA, hAcomp, hAkeys⟩ := + exists_long_compat_db hcomp₀ hcell_keep [(([] : Key))] dbJ.keys + (sortedLT_singleton _) + (by + intro x hx + simp only [List.mem_singleton] at hx + subst x + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := + clampLong_headD_eq (hne := hne) + rw [hheadD] + exact hbot_lt_head) + dbJ.sorted hJlo hJhi hJlen + have hres_lt_E := clampLong_reported_lt_end hread_keep hrev hmore hend + have hPads_lt : ∀ x ∈ between db₀ (resolve db₀ r.end_) r.end_.key, + ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + exact (of_decide_eq_true (List.mem_filter.mp hx).2).2 + have hkA : k ∈ dbA.keys := by + rw [hAkeys] + simp only [List.singleton_append] + exact List.mem_append.mpr (Or.inr hkJmem) + have hk_res : k ∉ res.keys := fun h => absurd hkE (not_le.mpr (hres_lt_E k h)) + have hk_pads : k ∉ between db₀ (resolve db₀ r.end_) r.end_.key := + fun h => absurd hkE (not_le.mpr (hPads_lt k h)) + have hnil_ne_k : ([] : Key) ≠ k := by + intro h + have hlt : (([] : Key) : WithTop Key) < r.end_.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head)) + (hres_lt_E _ (List.head_mem hne)) + rw [h] at hlt + exact absurd hkE (not_le.mpr hlt) + have hnil_beq : ¬(([] : Key) == k) = true := by + rw [beq_iff_eq]; exact hnil_ne_k + have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res := by + have hBkeys : (toggle dbA k).keys = ([] : Key) :: + (res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key ++ dbJ.keys.erase k) := by + rw [toggle_eq_erase_of_mem hkA, DB.erase_keys, hAkeys] + simp only [List.cons_append, List.nil_append] + rw [List.erase_cons_tail hnil_beq] + congr 1 + rw [List.erase_append_right (l₁ := res.keys ++ between db₀ (resolve db₀ r.end_) r.end_.key) + (a := k) dbJ.keys] + intro h + rcases List.mem_append.mp h with h | h + · exact hk_res h + · exact hk_pads h + have hanchor : r.begin.anchor (toggle dbA k) = 0 := by + have hpadlen : (between db₀ (resolve db₀ r.end_) r.end_.key).length = + clampNonposD r + 1 := by + 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 : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := by + rw [← hres_eq₀]; exact List.head_mem hne + have hhead_lt_resEnd : ((res.keys.head hne : Key) : WithTop Key) < + resolve db₀ r.end_ := (RangeRead.mem_completeKeys.mp hhead_ck).2.2 + have hRne : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + rw [hR] at hhead_lt_resEnd + exact absurd hhead_lt_resEnd + (not_lt.mpr (coe_nil_le_key_withTop (res.keys.head hne))) + exact between_resolve_end_length hend hRne + have herase_len : (dbJ.keys.erase k).length = clampLongC r res - 1 := by + rw [List.length_erase_of_mem hkJmem, hJlen] + -- count below begin.key after erasure + have hcntB : (toggle dbA k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.length + (clampNonposD r + 1) + clampLongC r res := by + rw [hBkeys, List.countP_cons_of_pos] + · rw [List.countP_append, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong) + have h2 : (between db₀ (resolve db₀ r.end_) r.end_.key).countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + (between db₀ (resolve db₀ r.end_) r.end_.key).length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_trans (hPads_lt x hx) hlong + have h3 : (dbJ.keys.erase k).countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + (dbJ.keys.erase k).length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hJhi x (List.mem_of_mem_erase hx) + rw [h1, h2, h3, hpadlen, herase_len] + have hCpos : 0 < clampLongC r res := clampLong_C_pos hcell_keep + omega + · rw [decide_eq_true_eq] + have hlt : (([] : Key) : WithTop Key) < + ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + rw [clampLong_headD_eq (hne := hne)] + exact WithTop.coe_lt_coe.mpr hbot_lt_head + exact lt_trans hlt hcrossD + have hreplen : reportedBelowBegin r res = res.keys.length := + clampLong_reported_eq_length hread_keep hcell_keep + have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + rw [← hreplen]; exact clampLong_nat_bottom hcell_keep + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcntB] + have hslack : beginSlack r = + res.keys.length + clampLongC r res + clampNonposD r := by + unfold clampLongC + rw [hreplen] + have hle : res.keys.length + clampNonposD r ≤ beginSlack r := by omega + omega + rw [hslack] + omega + exact clampLong_not_compat_of_anchor_zero_bottom hread_keep hcell_keep hheadne + hBkeys hanchor + exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩ + +/-- **Every key below the end key is necessary in the crossed negative-begin complete +long clamp cell.** Below the reported head it is necessary because a clamping witness +stores nothing there; between reported keys it is convex; above the last reported key it +is fixed by the complete window. -/ +theorem necessary_below_end_clampLong {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) {k : Key} + (hkE : ((k : Key) : WithTop Key) < r.end_.key) : + Necessary r res k := by + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · obtain ⟨db, hcomp, hneg⟩ := exists_clampLong_db hread hcell_keep + exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hneg hkhead + · have hkhead_le : ((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + le_of_not_gt hkhead + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead_le) hklast + · have hkgt : res.keys.getLast hne < k := lt_of_not_ge hklast + exact fun cr hs => necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs + (le_of_lt hkgt) hkE + +/-- **Minimality of the crossed negative-begin complete long clamp cell.** -/ +theorem minimal_closedForm_of_negCross_clamp_long_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampLongNonpos r res) : + Minimal r res (closedFormNegCrossClampLongNonpos r res) := by + have hcell_keep : negCrossClampLongNonpos r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + by_cases hb : clampLongCap r < (clampLongC r res : ℕ∞) ∨ + (clampLongCap r = (clampLongC r res : ℕ∞) ∧ res.keys.headD ([] : Key) = []) + · have hcr : closedFormNegCrossClampLongNonpos r res = + (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + unfold closedFormNegCrossClampLongNonpos clampLongUpper + rw [ite_eq_left hb] + rw [hcr] + exact minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_clampLong_nonpos_end hread hcell_keep hb, + fun k _ hk => necessary_below_end_clampLong hread hcell_keep hk⟩ + · have hcr : closedFormNegCrossClampLongNonpos r res = + (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) := by + unfold closedFormNegCrossClampLongNonpos clampLongUpper + rw [ite_eq_right hb] + rw [hcr] + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_clampLong_nonpos_begin hrev hbegin hend hmore hlong, ?_⟩ + intro k _ hkB + by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key + · exact necessary_below_end_clampLong hread hcell_keep hkE + · have hkE_le : r.end_.key ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkE + have hle : (clampLongC r res : ℕ∞) ≤ clampLongCap r := by + by_contra h + exact hb (Or.inl (lt_of_not_ge h)) + rcases lt_or_eq_of_le hle with hlt | heq + · exact clampLong_necessary_gap_of_cap_gt hread hcell_keep hlt hkE_le hkB + · have hheadne : res.keys.headD ([] : Key) ≠ [] := by + intro h + exact hb (Or.inr ⟨heq.symm, h⟩) + exact clampLong_necessary_gap_of_cap_eq hread hcell_keep heq.symm hheadne hkE_le hkB + +end Structural + +/-- The reversed crossed negative-begin complete long clamp cell at `end.offset ≤ 0`. -/ +abbrev negCrossClampLongNonposRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ clampNonposBottom r res ∧ + r.end_.key < r.begin.key + +/-- The reversed closed form for the crossed negative-begin complete long clamp cell. -/ +noncomputable def closedFormNegCrossClampLongNonposRev (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossClampLongNonpos { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- **The reversed crossed negative-begin complete long clamp cell is minimal**, by +flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_clamp_long_nonpos_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampLongNonposRev r res) : + Minimal r res (closedFormNegCrossClampLongNonposRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hbottom, hlong⟩ := hcell + 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 hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hbottom' : clampNonposBottom { r with reverse := false } + { keys := res.keys.reverse, more := false } ↔ clampNonposBottom r res := by + unfold clampNonposBottom + rw [hcount] + rfl + have hcell' : negCrossClampLongNonpos { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, ?_, hlong⟩ + · rw [hheadD]; exact hcrossD + · rw [hcount]; exact hmq + · exact hbottom'.mpr hbottom + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_clamp_long_nonpos hread' hcell' + exact (minimal_flip (b := true) hrev hmore).mp hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossClampNonpos.lean b/FormalConflictRange/NegCrossClampNonpos.lean new file mode 100644 index 0000000..17f6e8b --- /dev/null +++ b/FormalConflictRange/NegCrossClampNonpos.lean @@ -0,0 +1,1224 @@ +/- +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.NegCrossBottom + +/-! +# The crossed negative-begin complete nonpositive-end clamp cell + +A forward complete result with a negative begin offset, `end.offset ≤ 0`, the begin key +at or below the end key but with an infinite capacity in `[begin.key, end.key)`, and +reported keys below `begin.key` within the begin slack. The begin selector clamps to the +empty key, so the minimal conflict range is `[⊥, end_.key)`. The reversed traversal is +the flip of this cell. +-/ + +namespace FormalConflictRange + +/-- A forward complete result with a negative begin offset, `end.offset ≤ 0`, the begin +key at or below the end key but with infinite capacity between them, and reported keys +below the begin key within the begin slack. -/ +abbrev negCrossClampNonposInfinite (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + intervalCard r.begin.key r.end_.key = ⊤ ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The closed form for the infinite-capacity negative-begin complete clamp cell: +`[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossClampNonposInfinite (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **Soundness of the infinite-capacity negative-begin complete clamp cell.** Agreement +on `[⊥, end_.key)` fixes all keys below the end key, hence both resolutions and the whole +complete window, because `begin.key ≤ end_.key`. -/ +theorem sound_clampNonposInfinite {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) (hmore : res.more = false) + (hB_le_E : r.begin.key ≤ r.end_.key) : + Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + have step : ∀ db₁ db₂, AgreeOn (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) + db₁ db₂ → RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hfiltE : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + have hL : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.begin) + (le_of_lt hbegin) hB_le_E hfiltE + have hR : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.end_) + hend le_rfl hfiltE + have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hP : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + have h₁ : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + (db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_ + · have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le + simp [hk, hkE] + · simp [hk] + have h₂ : db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + (db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_ + · have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le + simp [hk, hkE] + · simp [hk] + rw [h₁, h₂, hfiltE] + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), ← hL, ← hR] + exact hP + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + + +theorem intervalCard_keyAfter_eq_top_of_eq_top {B : Key} {E : WithTop Key} + (h : intervalCard (B : WithTop Key) E = ⊤) : + intervalCard ((keyAfter B : Key) : WithTop Key) E = ⊤ := by + have hBltE : (B : WithTop Key) < E := by + rcases lt_or_ge (B : WithTop Key) E with hlt | hge + · exact hlt + · rw [intervalCard_eq_zero_of_le hge] at h + exact absurd h (by simp) + have hle1 : (B : WithTop Key) ≤ ((keyAfter B : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt B)) + have hle2 : ((keyAfter B : Key) : WithTop Key) ≤ E := keyAfter_le_of_lt hBltE + have hadd := intervalCard_add hle1 hle2 + have hone : intervalCard (B : WithTop Key) ((keyAfter B : Key) : WithTop Key) = 1 := by + rw [keyAfter] + simpa using intervalCard_succChain B 1 + have hsum : (1 : ℕ∞) + intervalCard ((keyAfter B : Key) : WithTop Key) E = ⊤ := by + have := hadd.symm.trans h + simpa [hone] using this + rcases ENat.add_eq_top.mp hsum with h1 | h2 + · exact absurd h1 (by simp) + · exact h2 + +/-- The buffer size `d = -end.offset`. -/ +def clampNonposD (r : RangeRead) : ℕ := (0 - r.end_.offset).toNat + +theorem clampNonposD_eq (r : RangeRead) : clampNonposD r = (-r.end_.offset).toNat := by + unfold clampNonposD; rw [zero_sub] + +/-- Whether a clamping compatible database can exist in the `end.offset ≤ 0` crossed +complete cell. -/ +def clampNonposBottom (r : RangeRead) (res : Result) : Prop := + (reportedBelowBegin r res : ℕ∞) + + ((((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key) ≤ + (beginSlack r : ℕ∞) + +instance instDecidableClampNonposBottom (r : RangeRead) (res : Result) : + Decidable (clampNonposBottom r res) := by + unfold clampNonposBottom + infer_instance + +/-- The stored keys in `[resolve end, end.key)` number `d+1` at a nonpositive end +offset. -/ +theorem between_resolve_end_length {db : DB} {r : RangeRead} + (hend : r.end_.offset ≤ 0) + (hne : resolve db r.end_ ≠ (([] : Key) : WithTop Key)) : + (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := by + set d := clampNonposD r with hd + set L := db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) + with hL + have hLsorted : L.SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hdval : (d : ℤ) = -r.end_.offset := by + rw [hd, clampNonposD_eq] + rw [Int.toNat_of_nonneg (by omega)] + have hrev := resolve_eq_filter_lt_reverse (db := db) (s := r.end_) hend + rw [← hL, ← clampNonposD_eq, ← hd] at hrev + cases hsome : L.reverse[d]? with + | none => + rw [hsome] at hrev + simp at hrev + exact absurd hrev hne + | some qv => + rw [hsome] at hrev + simp at hrev + have hqv : resolve db r.end_ = ((qv : Key) : WithTop Key) := hrev + obtain ⟨hdlt, hget⟩ := List.getElem?_eq_some_iff.mp hsome + have hdlt' : d < L.length := by simpa using hdlt + have hrev2 : L.reverse[d]? = L[L.length - 1 - d]? := + List.getElem?_reverse (l := L) (i := d) hdlt' + rw [hsome] at hrev2 + obtain ⟨hilt, higet⟩ := List.getElem?_eq_some_iff.mp hrev2.symm + have hcount : L.countP (fun k : Key => decide (k < qv)) = L.length - 1 - d := + countP_lt_eq_of_getElem hLsorted hilt higet + -- length of the filter of keys at or above qv + have hfilt : L.filter (fun k : Key => decide (qv ≤ k)) = + L.filter (fun k : Key => decide (¬ (((k : Key) : WithTop Key) < ((qv : Key) : WithTop Key)))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => not_lt.mpr (WithTop.coe_le_coe.mpr h), + fun h => WithTop.coe_le_coe.mp (le_of_not_gt h)⟩ + have hdrop := filter_not_lt_eq_drop_countP (l := L) hLsorted ((qv : Key) : WithTop Key) + have hcnt' : L.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < ((qv : Key) : WithTop Key))) = + L.length - 1 - d := by + rw [← hcount] + apply List.countP_congr + intro k _ + rw [decide_eq_true_eq, decide_eq_true_eq] + exact WithTop.coe_lt_coe + have hlen : (L.filter (fun k : Key => decide (qv ≤ k))).length = d + 1 := by + rw [hfilt, hdrop, List.length_drop, hcnt'] + omega + have hbetween : between db (resolve db r.end_) r.end_.key = + L.filter (fun k : Key => decide (qv ≤ k)) := by + rw [hqv] + unfold between + rw [hL, List.filter_filter] + apply List.filter_congr + intro k _ + have h1 : decide (((qv : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) = + decide (qv ≤ k) := by + rw [decide_eq_decide] + exact WithTop.coe_le_coe + rw [Bool.decide_and] + rw [h1] + rw [hbetween, hlen] + + + +theorem exists_clamp_compat_nonpos_infinite {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) (hB_le_E : r.begin.key ≤ r.end_.key) + (hinf : intervalCard r.begin.key r.end_.key = ⊤) + (hcross : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) : + ∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = (↑b : WithTop Key) := by + cases h : r.begin.key with + | top => rw [h, intervalCard_top_left] at hinf; exact absurd hinf (by simp) + | coe b => exact ⟨b, rfl⟩ + rw [hb] at hinf + have hb_lt_E : (b : WithTop Key) < r.end_.key := by + rcases lt_or_ge (b : WithTop Key) r.end_.key with hlt | hge + · exact hlt + · rw [intervalCard_eq_zero_of_le hge] at hinf + exact absurd hinf (by simp) + by_cases hlastB : res.keys.getLast hne < b + · -- Option A: every reported key lies below `b`. + have hrepA : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [hb] + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (hlast_le x hx) hlastB) + have hcard : ((clampNonposD r : ℕ) : ℕ∞) ≤ + intervalCard ((keyAfter b : Key) : WithTop Key) r.end_.key := by + have hka := intervalCard_keyAfter_eq_top_of_eq_top (B := b) hinf + rw [hka]; exact le_top + obtain ⟨pad, hpadIn, hpadlen⟩ := + exists_db_keysIn (B := ((keyAfter b : Key) : WithTop Key)) (E := r.end_.key) + (m := clampNonposD r) hcard + set F : List Key := pad.keys with hF + have hFsorted : F.SortedLT := pad.sorted + have hFmem : ∀ x ∈ F, ((keyAfter b : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.end_.key := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx + have hFb : ∀ x ∈ F, b < x := by + intro x hx + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt b)) (hFmem x hx).1) + have hFlen : F.length = clampNonposD r := hpadlen + have hresb : ∀ x ∈ res.keys, x < b := + fun x hx => lt_of_le_of_lt (hlast_le x hx) hlastB + have hbF_sorted : (b :: F).SortedLT := by + rw [List.sortedLT_cons] + exact ⟨fun y hy => hFb y hy, hFsorted⟩ + have hcrossA : ∀ x ∈ res.keys, ∀ y ∈ b :: F, x < y := by + intro x hx y hy + rcases List.mem_cons.mp hy with hy | hy + · rw [hy]; exact hresb x hx + · exact lt_trans (hresb x hx) (hFb y hy) + have hAppSorted : (res.keys ++ (b :: F)).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hbF_sorted, hcrossA⟩ + set dbA : DB := ⟨res.keys ++ (b :: F), hAppSorted⟩ with hdbA + have hfiltE : dbA.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + res.keys ++ (b :: F) := by + rw [hdbA] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_trans (WithTop.coe_lt_coe.mpr hlastB) hb_lt_E) + · rcases List.mem_cons.mp hx with hx | hx + · rw [hx]; exact hb_lt_E + · exact (hFmem x hx).2 + have hrevlist : (res.keys ++ (b :: F)).reverse = + F.reverse ++ (b :: res.keys.reverse) := by + rw [List.reverse_append, List.reverse_cons, List.append_assoc] + rfl + have hgetd : (res.keys ++ (b :: F)).reverse[clampNonposD r]? = some b := by + rw [hrevlist, List.getElem?_append] + have hFl : F.reverse.length = clampNonposD r := by rw [List.length_reverse, hFlen] + rw [ite_eq_right (by omega : ¬ clampNonposD r < F.reverse.length)] + simp [hFl] + have hR_A : resolve dbA r.end_ = ((b : Key) : WithTop Key) := by + rw [resolve_eq_filter_lt_reverse (db := dbA) (s := r.end_) hend, hfiltE, + ← clampNonposD_eq, hgetd] + have hcntA : dbA.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + res.keys.length := by + rw [hb, hdbA] + rw [List.countP_append, List.countP_cons] + have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = + res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_lt_coe.mpr (hresb x hx) + have h2 : decide (((b : Key) : WithTop Key) < (↑b : WithTop Key)) = false := by simp + have h3 : F.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_lt (hFb x hx))) + rw [h1, h2, h3] + simp + have hanchorA : r.begin.anchor dbA < 0 := by + rw [anchor_eq_count_sub_slack dbA r (le_of_lt hbegin), hcntA] + have hle : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) := by + have := hclamp; rw [hrepA] at this; exact_mod_cast this + omega + have hL_A : resolve dbA r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorA + have hfilt_b : dbA.keys.filter (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = + res.keys := by + rw [hdbA] + rw [List.filter_append, List.filter_cons] + have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = + res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_lt_coe.mpr (hresb x hx) + have h2 : decide (((b : Key) : WithTop Key) < (↑b : WithTop Key)) = false := by simp + have h3 : F.filter (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_lt (hFb x hx))) + rw [h1, h2, h3] + simp + have hckA : RangeRead.completeKeys dbA r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hL_A, hR_A] + rw [filter_between_bot_eq] + exact hfilt_b + exact ⟨dbA, ⟨hlim, Or.inr ⟨hmore, hckA.symm⟩⟩, hanchorA⟩ + · -- Option B: the reported last lies at or above `b`. Use the keys of `db₀` above it. + push_neg at hlastB + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2 + have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hbot + rw [hbot] at hlast_lt_R₀ + exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _)) + obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩ + · exact absurd hn hR_ne_nil + · exact absurd ht (resolve_ne_top_of_offset_nonpos hend) + · exact ⟨q, hqeq⟩ + have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key) := by + rw [← hr₀]; exact hlast_lt_R₀ + have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by + rw [← hr₀] + exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil + set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL + have hLlen : L.length = clampNonposD r + 1 := by + rw [hL]; exact between_resolve_end_length hend hR_ne_nil + have hLsorted : L.SortedLT := by + rw [hL] + exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hL] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + have hresL : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlast_lt_R₀ (hLmem y hy).1)) + have hAppSorted : (res.keys ++ L).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hLsorted, hresL⟩ + set dbB : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdbB + have hresE : ∀ x ∈ res.keys, (x : WithTop Key) < r.end_.key := by + intro x hx + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_trans hlast_lt_r₀ hr₀_lt_E) + have hfiltEB : dbB.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + res.keys ++ L := by + rw [hdbB] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · exact hresE x hx + · exact (hLmem x hx).2 + have hbetweenB : dbB.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [hdbB] + rw [List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + intro hcon + exact absurd hcon.1 + (not_le.mpr (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + hlast_lt_r₀)) + have h2 : L.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [← hr₀] + exact hLmem x hx + rw [h1, h2] + simp + have hbetween₀ : db₀.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [hL, ← hr₀] + rfl + have hqspec : (db₀.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).reverse[ + (-r.end_.offset).toNat]? = some r₀ := + resolve_filter_reverse_eq_some_of_ne_nil hend (by simpa [hr₀] using hR_ne_nil) hr₀ + have hR_B : resolve dbB r.end_ = ((r₀ : Key) : WithTop Key) := by + refine resolve_eq_of_agree_between (db₁ := db₀) (db₂ := dbB) (s := r.end_) hend rfl + (le_of_lt hr₀_lt_E) ?_ hqspec (le_refl _) + rw [hbetween₀, hbetweenB] + have hcntB : dbB.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hb, hdbB] + rw [List.countP_append] + have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = + reportedBelowBegin r res := by + unfold reportedBelowBegin + rw [hb] + have h2 : L.countP (fun k : Key => decide ((k : WithTop Key) < (↑b : WithTop Key))) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + have hbx : (b : WithTop Key) ≤ ((x : Key) : WithTop Key) := by + refine le_trans (WithTop.coe_le_coe.mpr hlastB) ?_ + exact le_of_lt (lt_of_lt_of_le hlast_lt_R₀ (hLmem x hx).1) + exact not_lt.mpr hbx + rw [h1, h2, Nat.add_zero] + have hanchorB : r.begin.anchor dbB < 0 := by + rw [anchor_eq_count_sub_slack dbB r (le_of_lt hbegin), hcntB] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL_B : resolve dbB r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorB + have hfilt_r : dbB.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = res.keys := by + rw [hdbB] + rw [List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_lt_coe.mpr + (lt_of_le_of_lt (hlast_le x hx) (WithTop.coe_lt_coe.mp hlast_lt_r₀)) + have h2 : L.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + rw [← hr₀] + exact not_lt.mpr (hLmem x hx).1 + rw [h1, h2] + simp + have hckB : RangeRead.completeKeys dbB r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hL_B, hR_B] + rw [filter_between_bot_eq] + exact hfilt_r + exact ⟨dbB, ⟨hlim, Or.inr ⟨hmore, hckB.symm⟩⟩, hanchorB⟩ + + +/-- **The infinite-capacity negative-begin complete clamp cell is minimal.** -/ +theorem minimal_closedForm_of_negCross_clampNonpos_infinite {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampNonposInfinite r res) : + Minimal r res (closedFormNegCrossClampNonposInfinite r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hinf, hcrossD, hclamp⟩ := hcell + obtain ⟨db, hcomp, hanchor⟩ := + exists_clamp_compat_nonpos_infinite hread hrev hmore hne hbegin hend hB_le_E hinf hcrossD hclamp + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_clampNonposInfinite hrev hbegin hend hmore hB_le_E, ?_⟩ + intro k hkbot hkE + by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead + · push_neg at hkhead + by_cases hklast : ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast) + · push_neg at hklast + intro cr hs + exact necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs + (le_of_lt (WithTop.coe_lt_coe.mp hklast)) hkE +/-- A reversed complete result in the same cell. -/ +abbrev negCrossClampNonposInfiniteRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + intervalCard r.begin.key r.end_.key = ⊤ ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The closed form for the reversed cell: `[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossClampNonposInfiniteRev (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **The reversed infinite-capacity negative-begin complete clamp cell is minimal**, by +flipping the traversal and applying the forward rule. -/ +theorem minimal_closedForm_of_negCross_clampNonpos_infinite_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampNonposInfiniteRev r res) : + Minimal r res (closedFormNegCrossClampNonposInfiniteRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hinf, hcrossD, hclamp⟩ := hcell + have hne' : res.keys.reverse ≠ [] := by simpa using hne + have hlastD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by + rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse] + have hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hcell' : negCrossClampNonposInfinite { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, hinf, ?_, ?_⟩ + · rw [hlastD]; exact hcrossD + · rw [hcount]; exact hclamp + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_clampNonpos_infinite hread' hcell' + have hcr : closedFormNegCrossClampNonposInfinite { r with reverse := false } + { keys := res.keys.reverse, more := false } = + closedFormNegCrossClampNonposInfiniteRev r res := by + unfold closedFormNegCrossClampNonposInfinite closedFormNegCrossClampNonposInfiniteRev + rfl + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +/-- A forward complete result at a negative begin offset with `end.offset ≤ 0`, reported +keys reaching at or above `begin.key`, and reported keys below `begin.key` within the begin +slack. -/ +abbrev negCrossClampNonposLastGe (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The closed form for the cell: `[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossClampNonposLastGe (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +theorem exists_clamp_compat_nonpos_lastGe {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) : + ∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?] + simp [List.getLast?_eq_some_getLast hne] + have hlastB : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [← hlastD]; exact hB_le_last + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2 + have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hbot + rw [hbot] at hlast_lt_R₀ + exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _)) + obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩ + · exact absurd hn hR_ne_nil + · exact absurd ht (resolve_ne_top_of_offset_nonpos hend) + · exact ⟨q, hqeq⟩ + have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key) := by + rw [← hr₀]; exact hlast_lt_R₀ + have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by + rw [← hr₀] + exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil + set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL + have hLlen : L.length = clampNonposD r + 1 := by + rw [hL]; exact between_resolve_end_length hend hR_ne_nil + have hLsorted : L.SortedLT := by + rw [hL] + exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hL] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hresL : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlast_lt_r₀ (le_trans (le_of_eq hr₀.symm) (hLmem y hy).1))) + have hAppSorted : (res.keys ++ L).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hLsorted, hresL⟩ + set db : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdb + have hL_ge_r₀ : ∀ x ∈ L, ((r₀ : Key) : WithTop Key) ≤ (x : WithTop Key) := by + intro x hx + rw [← hr₀] + exact (hLmem x hx).1 + have hL_lt_E : ∀ x ∈ L, (x : WithTop Key) < r.end_.key := fun x hx => (hLmem x hx).2 + have hres_lt_r₀ : ∀ x ∈ res.keys, x < r₀ := by + intro x hx + exact WithTop.coe_lt_coe.mp + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlast_lt_r₀) + have hfilt : db.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [hdb] + rw [List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + intro hcon + exact absurd hcon.1 (not_le.mpr (WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx))) + have h2 : L.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨hL_ge_r₀ x hx, hL_lt_E x hx⟩ + rw [h1, h2] + simp + have hfilt₀ : db₀.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [hL, ← hr₀] + rfl + have hqspec : (db₀.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).reverse[ + (-r.end_.offset).toNat]? = some r₀ := + resolve_filter_reverse_eq_some_of_ne_nil hend (by simpa [hr₀] using hR_ne_nil) hr₀ + have hR : resolve db r.end_ = ((r₀ : Key) : WithTop Key) := by + refine resolve_eq_of_agree_between (db₁ := db₀) (db₂ := db) (s := r.end_) hend rfl + (le_of_lt hr₀_lt_E) ?_ hqspec (le_refl _) + rw [hfilt₀, hfilt] + have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hdb] + rw [List.countP_append] + have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have h2 : L.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr + (le_trans hlastB (le_trans (le_of_lt hlast_lt_r₀) + (le_trans (le_of_eq hr₀.symm) (hLmem x hx).1))) + rw [h1, h2, Nat.add_zero] + have hanchor : r.begin.anchor db < 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL_db : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + have hfilt_b : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = res.keys := by + rw [hdb] + rw [List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx) + have h2 : L.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + rw [← hr₀] + exact not_lt.mpr (hLmem x hx).1 + rw [h1, h2] + simp + have hck : RangeRead.completeKeys db r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hL_db, hR] + rw [filter_between_bot_eq] + exact hfilt_b + exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩, hanchor⟩ + +theorem minimal_closedForm_of_negCross_clampNonpos_lastGe {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampNonposLastGe r res) : + Minimal r res (closedFormNegCrossClampNonposLastGe r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hB_le_last, hclamp⟩ := hcell + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?] + simp [List.getLast?_eq_some_getLast hne] + obtain ⟨db, hcomp, hanchor⟩ := + exists_clamp_compat_nonpos_lastGe hread hrev hmore hne hbegin hend hB_le_last hclamp + have hB_le_E : r.begin.key ≤ r.end_.key := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast_lt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2 + have hres_le : resolve db₀ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hB_le_last' : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [← hlastD]; exact hB_le_last + exact le_trans hB_le_last' (le_trans (le_of_lt hlast_lt) hres_le) + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_clampNonposInfinite hrev hbegin hend hmore hB_le_E, ?_⟩ + intro k hkbot hkE + by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead + · push_neg at hkhead + by_cases hklast : ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast) + · push_neg at hklast + intro cr hs + exact necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs + (le_of_lt (WithTop.coe_lt_coe.mp hklast)) hkE + +/-- A reversed complete result at a negative begin offset with `end.offset ≤ 0`, reported +keys reaching at or above `begin.key` (in the reversed order, the reported head), and +reported keys below `begin.key` within the begin slack. -/ +abbrev negCrossClampNonposLastGeRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The closed form for the reversed cell: `[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossClampNonposLastGeRev (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **The reversed cell is minimal**, by flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_clampNonpos_lastGe_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampNonposLastGeRev r res) : + Minimal r res (closedFormNegCrossClampNonposLastGeRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hB_le_head, hclamp⟩ := hcell + have hne' : res.keys.reverse ≠ [] := by simpa using hne + have hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hcrossD' : ((res.keys.reverse.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by + rw [List.headD_eq_head?, List.head?_reverse] + simpa [List.getLastD_eq_getLast?] using hcrossD + have hB_le_last : r.begin.key ≤ + ((res.keys.reverse.getLastD ([] : Key) : Key) : WithTop Key) := by + rw [List.getLastD_eq_getLast?, List.getLast?_reverse] + simpa [List.headD_eq_head?] using hB_le_head + have hcell' : negCrossClampNonposLastGe { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, hcrossD', hB_le_last, ?_⟩ + rw [hcount]; exact hclamp + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_clampNonpos_lastGe hread' hcell' + have hcr : closedFormNegCrossClampNonposLastGe { r with reverse := false } + { keys := res.keys.reverse, more := false } = + closedFormNegCrossClampNonposLastGeRev r res := by + unfold closedFormNegCrossClampNonposLastGe closedFormNegCrossClampNonposLastGeRev + rfl + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +/-- Split a capacity bound `n ≤ A + C` into pads below and above a cut: `a` pads fit in +`C` and the remaining `b = n - C` fit in `A`. -/ +theorem exists_pad_split {n : ℕ} {A C : ℕ∞} + (h : (n : ℕ∞) ≤ A + C) : + ∃ a b : ℕ, a + b = n ∧ (a : ℕ∞) ≤ C ∧ (b : ℕ∞) ≤ A ∧ + (b : ℕ∞) = (n : ℕ∞) - C := by + by_cases hC : C = ⊤ + · refine ⟨n, 0, by omega, ?_, by simp, ?_⟩ + · rw [hC]; exact le_top + · rw [hC, ENat.sub_top]; simp + · have hCeq : ((C.toNat : ℕ) : ℕ∞) = C := ENat.natCast_toNat hC + by_cases hcn : n ≤ C.toNat + · refine ⟨n, 0, by omega, ?_, by simp, ?_⟩ + · rw [← hCeq]; exact_mod_cast hcn + · rw [← hCeq, ← ENat.natCast_sub, Nat.sub_eq_zero_of_le hcn] + · push_neg at hcn + have hble : ((n - C.toNat : ℕ) : ℕ∞) ≤ A := by + rw [← hCeq] at h + rw [ENat.natCast_sub, tsub_le_iff_left, add_comm] + exact h + refine ⟨C.toNat, n - C.toNat, by omega, ?_, hble, ?_⟩ + · rw [← hCeq]; exact le_rfl + · calc ((n - C.toNat : ℕ) : ℕ∞) = (n : ℕ∞) - ((C.toNat : ℕ) : ℕ∞) := + ENat.natCast_sub n C.toNat + _ = (n : ℕ∞) - C := by rw [hCeq] + +/-- A finite-capacity clamping database exists in the crossed negative-begin complete +nonpositive-end cell whenever the capacity bound `clampNonposBottom` holds. -/ +theorem exists_clamp_compat_nonpos_bottom {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (hB_le_E : r.begin.key ≤ r.end_.key) + (hbottom : clampNonposBottom r res) : + ∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact List.getLast_mem hne)).2.2 + have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hbot + rw [hbot] at hlast_lt_R₀ + exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _)) + obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩ + · exact absurd hn hR_ne_nil + · exact absurd ht (resolve_ne_top_of_offset_nonpos hend) + · exact ⟨q, hqeq⟩ + have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key) := by + rw [← hr₀]; exact hlast_lt_R₀ + have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by + rw [← hr₀] + exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil + set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL + have hLlen : L.length = clampNonposD r + 1 := by + rw [hL]; exact between_resolve_end_length hend hR_ne_nil + have hcap_r₀ : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard ((r₀ : Key) : WithTop Key) r.end_.key := by + have h := between_length_le_intervalCard (db := db₀) + (E := ((r₀ : Key) : WithTop Key)) (B := r.end_.key) (le_of_lt hr₀_lt_E) + have h' : ((between db₀ (resolve db₀ r.end_) r.end_.key).length : ℕ∞) ≤ + intervalCard ((r₀ : Key) : WithTop Key) r.end_.key := by + rw [hr₀]; exact h + rw [← hL, hLlen] at h' + exact h' + set last : Key := res.keys.getLast hne with hlastdef + set X : Key := keyAfter last with hXdef + have hlast_lt_X : last < X := keyAfter_self_lt last + have hX_le_r₀ : ((X : Key) : WithTop Key) ≤ ((r₀ : Key) : WithTop Key) := + keyAfter_le_of_lt hlast_lt_r₀ + have hX_le_E : ((X : Key) : WithTop Key) ≤ r.end_.key := + le_of_lt (lt_of_le_of_lt hX_le_r₀ hr₀_lt_E) + have hcapX : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard ((X : Key) : WithTop Key) r.end_.key := by + calc (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard ((r₀ : Key) : WithTop Key) r.end_.key := hcap_r₀ + _ ≤ intervalCard ((X : Key) : WithTop Key) r.end_.key := by + rw [intervalCard_add hX_le_r₀ (le_of_lt hr₀_lt_E)] + exact le_add_left le_rfl + have hres_lt_X : ∀ x ∈ res.keys, x < X := by + intro x hx + exact lt_of_le_of_lt (hlast_le x hx) hlast_lt_X + by_cases hBX : r.begin.key ≤ ((X : Key) : WithTop Key) + · -- Case 1: all pads can go at or above `begin.key`. + obtain ⟨pad, hpadIn, hpadlen⟩ := + exists_db_keysIn (B := ((X : Key) : WithTop Key)) (E := r.end_.key) + (m := clampNonposD r + 1) hcapX + set P : List Key := pad.keys with hP + have hPsorted : P.SortedLT := pad.sorted + have hPmem : ∀ x ∈ P, ((X : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.end_.key := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx + have hPlen : P.length = clampNonposD r + 1 := hpadlen + have hP_gt_last : ∀ x ∈ P, last < x := by + intro x hx + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) (hPmem x hx).1) + have hP_ge_B : ∀ x ∈ P, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + exact le_trans hBX (hPmem x hx).1 + have hcross : ∀ x ∈ res.keys, ∀ y ∈ P, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (hres_lt_X x hx)) (hPmem y hy).1) + have hAppSorted : (res.keys ++ P).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hPsorted, hcross⟩ + set db : DB := ⟨res.keys ++ P, hAppSorted⟩ with hdb + have hPne : P ≠ [] := by + rw [← List.length_pos_iff, hPlen]; omega + have hfiltE : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + res.keys ++ P := by + rw [hdb] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) hX_le_E) + · exact (hPmem x hx).2 + have hget : (res.keys ++ P).reverse[clampNonposD r]? = + some (P.head hPne) := by + rw [List.reverse_append, List.getElem?_append] + have hPlrev : P.reverse.length = clampNonposD r + 1 := by + rw [List.length_reverse, hPlen] + rw [ite_eq_left (by omega : clampNonposD r < P.reverse.length)] + rw [List.getElem?_reverse (l := P) (i := clampNonposD r) (by omega)] + have hidx : P.length - 1 - clampNonposD r = 0 := by omega + rw [hidx] + match P, hPne with + | [], h => exact absurd rfl h + | a :: t, _ => rfl + have hR : resolve db r.end_ = ((P.head hPne : Key) : WithTop Key) := by + rw [resolve_eq_filter_lt_reverse (db := db) (s := r.end_) hend, hfiltE, + ← clampNonposD_eq, hget] + have hhead_ge_X : ((X : Key) : WithTop Key) ≤ ((P.head hPne : Key) : WithTop Key) := + (hPmem (P.head hPne) (List.head_mem hPne)).1 + have hhead_le : ∀ x ∈ P, ((P.head hPne : Key) : WithTop Key) ≤ (x : WithTop Key) := by + intro x hx + exact WithTop.coe_le_coe.mpr ((hPsorted.pairwise.imp le_of_lt).rel_head hx) + have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hdb, List.countP_append] + have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have h2 : P.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hP_ge_B x hx) + rw [h1, h2, Nat.add_zero] + have hzero : ((((clampNonposD r + 1 : ℕ)) : ℕ∞) - + intervalCard r.begin.key r.end_.key) = 0 := by + rw [tsub_eq_zero_iff_le] + calc (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard ((X : Key) : WithTop Key) r.end_.key := hcapX + _ ≤ intervalCard r.begin.key r.end_.key := by + rw [intervalCard_add hBX hX_le_E] + exact le_add_left le_rfl + have hanchor : r.begin.anchor db < 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt] + have hq : (reportedBelowBegin r res : ℕ∞) ≤ (beginSlack r : ℕ∞) := by + have hb := hbottom + unfold clampNonposBottom at hb + rw [hzero, add_zero] at hb + exact hb + have hq' : reportedBelowBegin r res ≤ beginSlack r := by + exact_mod_cast hq + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hq' + omega + have hLdb : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + have hfilt_head : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((P.head hPne : Key) : WithTop Key))) = res.keys := by + rw [hdb, List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((P.head hPne : Key) : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) hhead_ge_X) + have h2 : P.filter (fun k : Key => decide ((k : WithTop Key) < + ((P.head hPne : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hhead_le x hx) + rw [h1, h2] + simp + have hck : RangeRead.completeKeys db r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hLdb, hR] + rw [filter_between_bot_eq] + exact hfilt_head + exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩, hanchor⟩ + · -- Case 2: pads split between `[X, begin.key)` and `[begin.key, end.key)`. + push_neg at hBX + have hXB : ((X : Key) : WithTop Key) < r.begin.key := hBX + have hadd : intervalCard ((X : Key) : WithTop Key) r.end_.key = + intervalCard ((X : Key) : WithTop Key) r.begin.key + + intervalCard r.begin.key r.end_.key := + intervalCard_add (le_of_lt hXB) hB_le_E + have hcap' : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard ((X : Key) : WithTop Key) r.begin.key + + intervalCard r.begin.key r.end_.key := by + rw [← hadd]; exact hcapX + obtain ⟨a, b, hab, haC, hbA, hbeq⟩ := exists_pad_split hcap' + obtain ⟨padQ, hpadQIn, hpadQlen⟩ := + exists_db_keysIn (B := r.begin.key) (E := r.end_.key) (m := a) haC + obtain ⟨padP, hpadPIn, hpadPlen⟩ := + exists_db_keysIn (B := ((X : Key) : WithTop Key)) (E := r.begin.key) (m := b) hbA + set Q : List Key := padQ.keys with hQ + set R : List Key := padP.keys with hRset + have hQsorted : Q.SortedLT := padQ.sorted + have hRsorted : R.SortedLT := padP.sorted + have hQmem : ∀ x ∈ Q, r.begin.key ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.end_.key := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpadQIn x hx + have hRmem : ∀ x ∈ R, ((X : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.begin.key := by + intro x hx + simpa only [keysIn, Set.mem_ofPred_eq] using hpadPIn x hx + have hQlen : Q.length = a := hpadQlen + have hRlen : R.length = b := hpadPlen + have hR_gt_last : ∀ x ∈ R, last < x := by + intro x hx + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) (hRmem x hx).1) + have hQ_gt_last : ∀ x ∈ Q, last < x := by + intro x hx + exact lt_trans hlast_lt_X + (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hXB (hQmem x hx).1)) + have hcross : ∀ x ∈ R, ∀ y ∈ Q, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hRmem x hx).2 (hQmem y hy).1) + set Lp : List Key := R ++ Q with hLp + have hLpSorted : Lp.SortedLT := + List.sortedLT_append.mpr ⟨hRsorted, hQsorted, hcross⟩ + have hLplen : Lp.length = clampNonposD r + 1 := by + rw [hLp, List.length_append, hQlen, hRlen] + omega + have hLpne : Lp ≠ [] := by + rw [← List.length_pos_iff, hLplen]; omega + have hLpmem : ∀ x ∈ Lp, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hLp] at hx + rcases List.mem_append.mp hx with hx | hx + · exact ⟨hR_gt_last x hx, lt_of_lt_of_le (hRmem x hx).2 hB_le_E⟩ + · exact ⟨hQ_gt_last x hx, (hQmem x hx).2⟩ + have hcross' : ∀ x ∈ res.keys, ∀ y ∈ Lp, x < y := by + intro x hx y hy + have hXy : ((X : Key) : WithTop Key) ≤ (y : WithTop Key) := by + rw [hXdef] + exact keyAfter_le_of_lt (WithTop.coe_lt_coe.mpr (hLpmem y hy).1) + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (hres_lt_X x hx)) hXy) + have hAppSorted : (res.keys ++ Lp).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hLpSorted, hcross'⟩ + set db : DB := ⟨res.keys ++ Lp, hAppSorted⟩ with hdb + have hfiltE : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + res.keys ++ Lp := by + rw [hdb] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hlast_lt_X) hX_le_E) + · exact (hLpmem x hx).2 + have hget : (res.keys ++ Lp).reverse[clampNonposD r]? = + some (Lp.head hLpne) := by + rw [List.reverse_append, List.getElem?_append] + have hLplrev : Lp.reverse.length = clampNonposD r + 1 := by + rw [List.length_reverse, hLplen] + rw [ite_eq_left (by omega : clampNonposD r < Lp.reverse.length)] + rw [List.getElem?_reverse (l := Lp) (i := clampNonposD r) (by omega)] + have hidx : Lp.length - 1 - clampNonposD r = 0 := by omega + rw [hidx] + match Lp, hLpne with + | [], h => exact absurd rfl h + | a :: t, _ => rfl + have hR : resolve db r.end_ = ((Lp.head hLpne : Key) : WithTop Key) := by + rw [resolve_eq_filter_lt_reverse (db := db) (s := r.end_) hend, hfiltE, + ← clampNonposD_eq, hget] + have hhead_ge_last : last < Lp.head hLpne := + (hLpmem (Lp.head hLpne) (List.head_mem hLpne)).1 + have hhead_le : ∀ x ∈ Lp, ((Lp.head hLpne : Key) : WithTop Key) ≤ (x : WithTop Key) := by + intro x hx + exact WithTop.coe_le_coe.mpr ((hLpSorted.pairwise.imp le_of_lt).rel_head hx) + have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res + b := by + rw [hdb, List.countP_append] + have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + rw [h1] + rw [hLp, List.countP_append] + have h2 : R.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = b := by + rw [← hRlen, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact (hRmem x hx).2 + have h3 : Q.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hQmem x hx).1 + rw [h2, h3, Nat.add_zero] + have hanchor : r.begin.anchor db < 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt] + have hq : ((reportedBelowBegin r res + b : ℕ) : ℕ∞) ≤ (beginSlack r : ℕ∞) := by + rw [Nat.cast_add, hbeq] + simpa only [clampNonposBottom] using hbottom + have hq' : reportedBelowBegin r res + b ≤ beginSlack r := by + exact_mod_cast hq + have hle : ((reportedBelowBegin r res + b : ℕ) : ℤ) ≤ (beginSlack r : ℤ) := by + exact_mod_cast hq' + rw [Nat.cast_add] at hle + omega + have hLdb : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + have hfilt_head : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((Lp.head hLpne : Key) : WithTop Key))) = res.keys := by + rw [hdb, List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((Lp.head hLpne : Key) : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (WithTop.coe_lt_coe.mpr hhead_ge_last) + have h2 : Lp.filter (fun k : Key => decide ((k : WithTop Key) < + ((Lp.head hLpne : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hhead_le x hx) + rw [h1, h2] + simp + have hck : RangeRead.completeKeys db r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hLdb, hR] + rw [filter_between_bot_eq] + exact hfilt_head + exact ⟨db, ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩, hanchor⟩ + +/-- The finite-capacity crossed negative-begin complete nonpositive-end clamp cell. -/ +abbrev negCrossClampNonposBottom (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + clampNonposBottom r res + +/-- The closed form for the finite-capacity clamp cell: `[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossClampNonposBottom (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **The finite-capacity negative-begin complete clamp cell is minimal.** -/ +theorem minimal_closedForm_of_negCross_clampNonpos_bottom {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampNonposBottom r res) : + Minimal r res (closedFormNegCrossClampNonposBottom r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcross, hbottom⟩ := hcell + obtain ⟨db, hcomp, hanchor⟩ := + exists_clamp_compat_nonpos_bottom hread hrev hmore hne hbegin hend hB_le_E hbottom + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_clampNonposInfinite hrev hbegin hend hmore hB_le_E, ?_⟩ + intro k hkbot hkE + by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead + · push_neg at hkhead + by_cases hklast : ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast) + · push_neg at hklast + intro cr hs + exact necessary_last_to_end_key hread hrev hmore hne (by omega) cr hs + (le_of_lt (WithTop.coe_lt_coe.mp hklast)) hkE + +/-- The reversed traversal of the finite-capacity clamp cell. -/ +abbrev negCrossClampNonposBottomRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + clampNonposBottom r res + +/-- The closed form for the reversed finite-capacity clamp cell. -/ +noncomputable def closedFormNegCrossClampNonposBottomRev (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **The reversed finite-capacity clamp cell is minimal**, by flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_clampNonpos_bottom_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampNonposBottomRev r res) : + Minimal r res (closedFormNegCrossClampNonposBottomRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcrossD, hbottom⟩ := hcell + have hne' : res.keys.reverse ≠ [] := by simpa using hne + have hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hcrossD' : ((res.keys.reverse.headD ([] : Key) : Key) : WithTop Key) < r.begin.key := by + rw [List.headD_eq_head?, List.head?_reverse] + simpa [List.getLastD_eq_getLast?] using hcrossD + have hbottom' : clampNonposBottom { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + unfold clampNonposBottom + rw [hcount] + exact hbottom + have hcell' : negCrossClampNonposBottom { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, hcrossD', hbottom'⟩ + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_clampNonpos_bottom hread' hcell' + have hcr : closedFormNegCrossClampNonposBottom { r with reverse := false } + { keys := res.keys.reverse, more := false } = + closedFormNegCrossClampNonposBottomRev r res := by + unfold closedFormNegCrossClampNonposBottom closedFormNegCrossClampNonposBottomRev + rfl + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossClosedForm.lean b/FormalConflictRange/NegCrossClosedForm.lean new file mode 100644 index 0000000..44c7604 --- /dev/null +++ b/FormalConflictRange/NegCrossClosedForm.lean @@ -0,0 +1,647 @@ +/- +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.CrossedGeneral + +/-! +# The crossed nonpositive-begin complete overshoot closed form + +A forward complete result at a negative begin offset whose `begin.key` lies strictly +above the reported head and whose end overshoots has a two-branch minimal sound +conflict range. Let `q = -begin.offset` be the begin slack and let `hmin` be the +number of reported keys strictly below `begin.key`. If `hmin ≤ q` the begin selector +clamps to the empty key, every key is forced, and the minimal range is `[⊥, ⊤)`. +Otherwise a compatible database resolves the begin selector exactly onto the reported +head (`hmin = q + 1`), the complete keys are the stored keys at or above the head, and +the minimal range is `[head, ⊤)`. + +The clamp branch is minimal because inserting any key below the head forces a new +complete key; the nonclamp branch is sound because agreement on `[head, ⊤)` fixes the +begin resolution and the end sentinel. +-/ + +namespace FormalConflictRange + +/-- The nonpositive begin slack, `-begin.offset` as a natural. -/ +def beginSlack (r : RangeRead) : ℕ := (0 - r.begin.offset).toNat + +/-- Reported keys strictly below `begin.key`. -/ +def reportedBelowBegin (r : RangeRead) (res : Result) : ℕ := + res.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) + +/-- The crossed nonpositive-begin complete overshoot region (forward traversal). -/ +abbrev negCrossOvershoot (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key + +/-- The closed form for the crossed complete overshoot at a negative begin offset. -/ +noncomputable def closedFormNegCrossOvershoot (r : RangeRead) (res : Result) : ConflictRange := + if reportedBelowBegin r res ≤ beginSlack r then + ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + else topOfLast (res.keys.headD ([] : Key)) + +theorem beginSlack_cast (r : RangeRead) (h : r.begin.offset ≤ 0) : + (beginSlack r : ℤ) = -r.begin.offset := by + unfold beginSlack + rw [Int.toNat_of_nonneg (by omega)] + ring + +theorem anchor_eq_count_sub_slack (db : DB) (r : RangeRead) (h : r.begin.offset ≤ 0) : + r.begin.anchor db = + (db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - + (beginSlack r : ℤ) - 1 := by + unfold Selector.anchor + rw [beginSlack_cast r h] + ring + +theorem result_sublist_db_of_compatible {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) : + List.Sublist res.keys db.keys := by + have hpre := RangeRead.compatible_keys_prefix hcomp + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + rfl + rw [hck] at hpre + exact hpre.sublist.trans List.filter_sublist + +theorem resolve_end_top_of_negCross_compat {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + resolve db r.end_ = (⊤ : WithTop Key) := by + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + exact resolve_end_eq_top_of_hhi hcomp hend hhi (res.keys.getLast hne) + (List.getLast_mem hne) (fun k hk => hmaxres k (by rw [hres_eq]; exact hk)) + +/-- The compatibility data used by both branches: when `hmin > q`, a compatible +database resolves its begin selector exactly onto the reported head. -/ +theorem resolve_begin_eq_head_of_negCross_compat {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + 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 + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + have hmin_le : reportedBelowBegin r res ≤ cB := List.Sublist.countP_le hsub + have hcB_ge : beginSlack r + 1 ≤ cB := + Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) + have hanchor_ge : 0 ≤ r.begin.anchor db := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge + omega + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : head ∈ res.keys := List.head_mem hne + have hhead_ck : head ∈ RangeRead.completeKeys db r := by + rw [← hres_eq]; exact 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_ge (le_of_not_gt h)) + have hresolve_get : resolve db r.begin = + ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) := + resolve_eq_getElem hanchor_ge hltlen + set k : Key := db.keys[(r.begin.anchor db).toNat] with hk + have hk_le_head : k ≤ head := WithTop.coe_le_coe.mp (by rw [← hresolve_get]; exact hle_head) + have hk_stored : k ∈ db.keys := List.getElem_mem hltlen + have hE := resolve_end_top_of_negCross_compat hcomp hrev hmore hne hend hhi + have hk_ck : k ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨hk_stored, by rw [hresolve_get], + by rw [hE]; exact WithTop.coe_lt_top k⟩ + have hk_ge_head : head ≤ k := by + have hmem : k ∈ res.keys := by rw [hres_eq]; exact hk_ck + exact (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hmem + have hk_eq : k = head := le_antisymm hk_le_head hk_ge_head + rw [← hk_eq] + exact hresolve_get + + +theorem negCross_compat_filter {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + db.keys.filter (fun k : Key => decide (res.keys.head hne ≤ k)) = res.keys := by + set head : Key := res.keys.head hne with hhead + have hE := resolve_end_top_of_negCross_compat hcomp hrev hmore hne hend hhi + have hL := resolve_begin_eq_head_of_negCross_compat hcomp hrev hmore hne hbegin hend hhi + hcross hnonclamp + have hck := completeKeys_eq_topOfLast_keys (db := db) (r := r) (last := head) hrev hE hL + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + rw [hres_eq, hck] + +theorem negCross_count_eq {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key))) + + reportedBelowBegin r res := by + set head : Key := res.keys.head hne with hhead + have hfil : db.keys.filter (fun k : Key => decide (head ≤ k)) = res.keys := + negCross_compat_filter hcomp hrev hmore hne hbegin hend hhi hcross hnonclamp + cases hB : r.begin.key with + | top => + have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.length := by rw [hB]; exact countP_lt_top + have hhmin : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin; rw [hB]; exact countP_lt_top + have he : db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) = + db.keys.countP (fun k : Key => decide (k < head)) := countP_lt_coe head + have hlen := List.length_eq_length_filter_add (l := db.keys) + (f := fun k : Key => decide (head ≤ k)) + have hf1 : (db.keys.filter (fun k : Key => decide (head ≤ k))).length = res.keys.length := by + rw [hfil] + have hf2 : (db.keys.filter (fun k : Key => !decide (head ≤ k))).length = + db.keys.countP (fun k : Key => decide (k < head)) := by + rw [List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + by_cases h : head ≤ k + · simp [h, not_lt.mpr h] + · simp [h, lt_of_not_ge h] + rw [hB] at hcB + rw [hcB, he, hhmin] + omega + | coe b => + have hcross_key : head < b := by + rw [hB] at hcross + exact WithTop.coe_lt_coe.mp hcross + have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.countP (fun k : Key => decide (k < b)) := by rw [hB]; exact countP_lt_coe b + have he : db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) = + db.keys.countP (fun k : Key => decide (k < head)) := countP_lt_coe head + have hhmin : reportedBelowBegin r res = res.keys.countP (fun k : Key => decide (k < b)) := by + unfold reportedBelowBegin; rw [hB]; exact countP_lt_coe b + have hsplit := countP_lt_eq_add_filter db.keys db.nodup (le_of_lt hcross_key) + have hfil2 : db.keys.filter (fun k : Key => decide (head ≤ k ∧ k < b)) = + res.keys.filter (fun k : Key => decide (k < b)) := by + rw [filter_and_eq_filter_filter (fun k : Key => head ≤ k) (fun k : Key => k < b), hfil] + have hlen : (db.keys.filter (fun k : Key => decide (head ≤ k ∧ k < b))).length = + res.keys.countP (fun k : Key => decide (k < b)) := by + rw [hfil2, List.countP_eq_length_filter] + rw [hB] at hcB + rw [hcB, he, hhmin] + omega + +/-- A general count identity: if the stored keys at or above `head` are exactly `R`, then +the number of stored keys below `B` splits as those below `head` plus those of `R` +below `B`. -/ +theorem count_eq_of_filter {l : List Key} {R : List Key} {head : Key} {B : WithTop Key} + (hnd : l.Nodup) (hheadB : ((head : Key) : WithTop Key) < B) + (hfil : l.filter (fun k : Key => decide (head ≤ k)) = R) : + l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) + + R.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) := by + cases B with + | top => + have hcB : l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = + l.length := countP_lt_top + have hhmin : R.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = + R.length := countP_lt_top + have he : l.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) = + l.countP (fun k : Key => decide (k < head)) := countP_lt_coe head + have hlen := List.length_eq_length_filter_add (l := l) + (f := fun k : Key => decide (head ≤ k)) + have hf1 : (l.filter (fun k : Key => decide (head ≤ k))).length = R.length := by + rw [hfil] + have hf2 : (l.filter (fun k : Key => !decide (head ≤ k))).length = + l.countP (fun k : Key => decide (k < head)) := by + rw [List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + by_cases h : head ≤ k + · simp [h, not_lt.mpr h] + · simp [h, lt_of_not_ge h] + rw [hcB, he, hhmin] + omega + | coe b => + have hcross_key : head < b := WithTop.coe_lt_coe.mp hheadB + have hcB : l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (↑b : WithTop Key))) = + l.countP (fun k : Key => decide (k < b)) := countP_lt_coe b + have he : l.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) = + l.countP (fun k : Key => decide (k < head)) := countP_lt_coe head + have hhmin : R.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (↑b : WithTop Key))) = + R.countP (fun k : Key => decide (k < b)) := countP_lt_coe b + have hsplit := countP_lt_eq_add_filter l hnd (le_of_lt hcross_key) + have hfil2 : l.filter (fun k : Key => decide (head ≤ k ∧ k < b)) = + R.filter (fun k : Key => decide (k < b)) := by + rw [filter_and_eq_filter_filter (fun k : Key => head ≤ k) (fun k : Key => k < b), hfil] + have hlen : (l.filter (fun k : Key => decide (head ≤ k ∧ k < b))).length = + R.countP (fun k : Key => decide (k < b)) := by + rw [hfil2, List.countP_eq_length_filter] + rw [hcB, he, hhmin] + omega + + +theorem negCross_hmin_eq {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + reportedBelowBegin r res = beginSlack r + 1 := by + set head : Key := res.keys.head hne with hhead + have hcount := negCross_count_eq hcomp hrev hmore hne hbegin hend hhi hcross hnonclamp + set cH : ℕ := db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) with hcH + set cB : ℕ := db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) with hcB + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + have hmin_le : reportedBelowBegin r res ≤ cB := List.Sublist.countP_le hsub + have hcB_ge : beginSlack r + 1 ≤ cB := + Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) + have hanchor_ge : 0 ≤ r.begin.anchor db := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge + omega + have hL := resolve_begin_eq_head_of_negCross_compat hcomp hrev hmore hne hbegin hend hhi + hcross hnonclamp + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hhead_db : head ∈ db.keys := RangeRead.compatible_keys_subset hcomp hhead_mem + have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro htop + rw [hL] at htop + exact absurd htop (WithTop.coe_ne_top (a := head)) + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + by_contra h + exact hnot_top (resolve_eq_top (db := db) hanchor_ge (le_of_not_gt h)) + have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor_ge hltlen + have hidx : db.keys[(r.begin.anchor db).toNat] = head := by + have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) = + ((head : Key) : WithTop Key) := by rw [← hget, hL] + exact WithTop.coe_injective hcoe + have hrank : db.keys.countP (fun k => decide (k < head)) = (r.begin.anchor db).toNat := + countP_lt_eq_of_getElem db.sorted hltlen hidx + have hcH_eq : cH = (r.begin.anchor db).toNat := by + rw [hcH, countP_lt_coe head] + exact hrank + 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 htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by + 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 hcH_add : cH + (beginSlack r + 1) = cB := by + rw [hcH_eq, htoNat] + exact Nat.sub_add_cancel hcB_ge + omega + + +/-- In the clamp region every key below the reported head is necessary: inserting it +into the database made of the reported keys either clamps the begin selector to the +empty key or lands it on the inserted key, and either way the complete keys gain a +key, so the result is no longer compatible. -/ +theorem necessary_below_head_of_negCross_clamp {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + {k : Key} (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hk_not : k ∉ res.keys := by + intro hkmem + exact hkcr (necessary_of_mem_result ⟨db₀, hcomp₀⟩ hkmem cr hs) + let db₁ : DB := ⟨res.keys, hsort⟩ + have hcB₁ : db₁.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have hanchor₁ : r.begin.anchor db₁ < 0 := by + rw [anchor_eq_count_sub_slack db₁ r (le_of_lt hbegin), hcB₁] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil (db := db₁) (s := r.begin) hanchor₁ + have hE₁ : resolve db₁ r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt (db := db₁) (fun x hx => hhi x hx) hend + have hfil₁ : db₁.keys.filter (fun x : Key => decide (([] : Key) ≤ x)) = res.keys := by + have hself : db₁.keys.filter (fun x : Key => decide (([] : Key) ≤ x)) = db₁.keys := by + rw [List.filter_eq_self] + intro x _; rw [decide_eq_true_eq]; exact nil_le_key x + rw [hself] + have hcomp₁ : RangeRead.Compatible db₁ r res := by + refine ⟨hcomp₀.1, Or.inr ⟨hmore, ?_⟩⟩ + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev]), hL₁, hE₁] + simpa [RangeRead.inRange] using hfil₁.symm + let db₂ : DB := db₁.insert k hk_not + have hagree : AgreeOn cr db₁ db₂ := + (ConflictRange.keys_insert_of_not_mem cr db₁ hk_not hkcr).symm + have hcomp₂ : RangeRead.Compatible db₂ r res := (hs db₁ db₂ hagree).mp hcomp₁ + have hck₂ : RangeRead.completeKeys db₂ r = res.keys := by + rcases hcomp₂.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq.symm + have hdb₂_keys : db₂.keys = k :: res.keys := by + unfold db₂ + rw [DB.insert_keys, show db₁.keys = res.keys from rfl] + cases hres : res.keys with + | nil => exact absurd hres hne + | cons a t => + have hhead_a : res.keys.head hne = a := by simp [hres] + have hka : k ≤ a := by + rw [hhead_a] at hk + exact le_of_lt (WithTop.coe_lt_coe.mp hk) + rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka] + have hkltB : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hk hcross + have hcB₂ : db₂.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res + 1 := by + rw [hdb₂_keys, List.countP_cons] + rw [decide_eq_true_eq.mpr hkltB] + simp [reportedBelowBegin] + have hanchor₂ : r.begin.anchor db₂ ≤ 0 := by + rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hcB₂] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL₂_le : resolve db₂ r.begin ≤ ((k : Key) : WithTop Key) := by + by_cases hneg : r.begin.anchor db₂ < 0 + · rw [resolve_eq_nil (db := db₂) (s := r.begin) hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + · have hzero : r.begin.anchor db₂ = 0 := by omega + have hltlen₂ : (r.begin.anchor db₂).toNat < db₂.keys.length := by + rw [hzero, hdb₂_keys] + simp + rw [resolve_eq_getElem (db := db₂) (s := r.begin) (by omega) hltlen₂] + have hval₂ : db₂.keys[(r.begin.anchor db₂).toNat]'hltlen₂ = k := by + simp [hzero, hdb₂_keys] + exact WithTop.coe_le_coe.mpr (le_of_eq hval₂) + have hall₂ : ∀ x ∈ db₂.keys, ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + rw [hdb₂_keys] at hx + rcases List.mem_cons.mp hx with rfl | hx + · exact lt_of_lt_of_le hk (le_of_lt (hhi _ (List.head_mem hne))) + · exact hhi x hx + have hE₂ : resolve db₂ r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hall₂ hend + have hk_ck₂ : k ∈ RangeRead.completeKeys db₂ r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by rw [hdb₂_keys]; exact List.mem_cons_self .., hL₂_le, ?_⟩ + rw [hE₂]; exact WithTop.coe_lt_top k + rw [hck₂] at hk_ck₂ + exact hk_not hk_ck₂ + +/-- **The clamp branch of the negative-offset crossed complete overshoot.** When the +reported keys below `begin.key` fit in the begin slack, every key is necessary, so the +full range is minimal. -/ +theorem minimal_fullRange_of_negCross_clamp {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + refine (minimal_iff_sound_and_necessary).mpr ⟨sound_fullRange r res, ?_⟩ + intro k _ + by_cases hkhead : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_below_head_of_negCross_clamp hread hrev hmore hne hbegin hend hhi + hcross hclamp hkhead + · have hhead_le : res.keys.head hne ≤ k := WithTop.coe_le_coe.mp (le_of_not_gt hkhead) + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + hhead_le hklast + · exact necessary_of_end_offset_ge_two hread hrev hmore hne hend hhi + (le_of_lt (lt_of_not_ge hklast)) + +/-- **Soundness of the top range from the reported head in the nonclamp branch.** +Agreement on `[head, ⊤)` fixes the stored keys at or above the head, hence the begin +resolution (`hmin = q + 1` forces the selector onto the head) and the end sentinel, so +the complete keys are unchanged. -/ +theorem sound_topOfLast_of_negCross_nonclamp {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + Sound r res (topOfLast (res.keys.head hne)) := by + intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (topOfLast (res.keys.head hne)) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + set head : Key := res.keys.head hne with hhead + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hhmin : reportedBelowBegin r res = beginSlack r + 1 := + negCross_hmin_eq hcomp_db hrev hmore hne hbegin hend hhi hcross hnonclamp + have hE := resolve_end_top_of_negCross_compat hcomp_db hrev hmore hne hend hhi + have hL := resolve_begin_eq_head_of_negCross_compat hcomp_db hrev hmore hne hbegin + hend hhi hcross hnonclamp + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp_db.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k => decide (head ≤ k)) := + completeKeys_eq_topOfLast_keys hrev hE hL + have hagree_keys : db.keys.filter (fun k => decide (head ≤ k)) = + db'.keys.filter (fun k => decide (head ≤ k)) := by + have h := hag' + rw [AgreeOn, topOfLast_keys, topOfLast_keys] at h + exact h + have hfil' : db'.keys.filter (fun k => decide (head ≤ k)) = res.keys := by + rw [← hagree_keys, ← hck, hres_eq] + have hall' : ∀ k ∈ db'.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hkh : head ≤ k + · have : k ∈ db'.keys.filter (fun k => decide (head ≤ k)) := + List.mem_filter.mpr ⟨hk, by rw [decide_eq_true_eq]; exact hkh⟩ + rw [hfil'] at this + exact hhi k this + · exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (lt_of_not_ge hkh)) + (le_of_lt (hhi head hhead_mem)) + have hE' : resolve db' r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hall' hend + have hhead_db' : head ∈ db'.keys := by + have : head ∈ db'.keys.filter (fun k => decide (head ≤ k)) := by + rw [hfil']; exact hhead_mem + exact List.mem_of_mem_filter this + have hcount' : db'.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db'.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) + + reportedBelowBegin r res := + count_eq_of_filter db'.nodup hcross hfil' + have hcB'_ge : beginSlack r + 1 ≤ db'.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hcount', hhmin]; omega + have hanchor'_ge : 0 ≤ r.begin.anchor db' := by + rw [anchor_eq_count_sub_slack db' r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ ((db'.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))) : ℤ) := by + exact_mod_cast hcB'_ge + omega + have htoNat' : (r.begin.anchor db').toNat = + db'.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)) - (beginSlack r + 1) := by + have hz : r.begin.anchor db' = ((db'.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ)) := by + rw [anchor_eq_count_sub_slack db' r (le_of_lt hbegin)]; push_cast; ring + have hz2 : ((db'.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ) = + ((db'.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)) - + (beginSlack r + 1) : ℕ) : ℤ) := by + rw [← Nat.cast_sub hcB'_ge] + rw [hz, hz2, Int.toNat_natCast] + have hidx_eq : (r.begin.anchor db').toNat = + db'.keys.countP (fun k => decide (k < head)) := by + have hcH'_eq : db'.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < ((head : Key) : WithTop Key))) = + db'.keys.countP (fun k => decide (k < head)) := countP_lt_coe head + rw [htoNat', hcount', hhmin, hcH'_eq] + omega + have hltlen' : (r.begin.anchor db').toNat < db'.keys.length := by + rw [hidx_eq] + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_db' + rw [countP_lt_eq_of_getElem db'.sorted hi hix] + exact hi + have hval : db'.keys[(r.begin.anchor db').toNat]'hltlen' = head := by + simpa [hidx_eq] using getElem_countP_lt db'.sorted hhead_db' + have hL' : resolve db' r.begin = ((head : Key) : WithTop Key) := by + rw [resolve_eq_getElem (db := db') (s := r.begin) hanchor'_ge hltlen'] + exact congrArg (fun x => ((x : Key) : WithTop Key)) hval + have hck' : RangeRead.completeKeys db' r = + db'.keys.filter (fun k => decide (head ≤ k)) := + completeKeys_eq_topOfLast_keys hrev hE' hL' + exact ⟨hcomp_db.1, Or.inr ⟨hmore, by rw [hck', hfil']⟩⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **The nonclamp branch of the negative-offset crossed complete overshoot.** The top +range from the reported head is minimal: soundness is above, and every key at or above +the head is necessary by convexity or by the overshoot necessity. -/ +theorem minimal_topOfLast_of_negCross_nonclamp {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + Minimal r res (topOfLast (res.keys.head hne)) := by + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_topOfLast_of_negCross_nonclamp hrev hmore hne hbegin hend hhi hcross hnonclamp, ?_⟩ + intro k hk + have hhead_le : res.keys.head hne ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + hhead_le hklast + · exact necessary_of_end_offset_ge_two hread hrev hmore hne hend hhi + (le_of_lt (lt_of_not_ge hklast)) + +theorem headD_eq_head_of_ne {l : List Key} (h : l ≠ []) : + l.headD ([] : Key) = l.head h := by + cases l with + | nil => exact absurd rfl h + | cons a t => rfl + +/-- **Closed form for the negative-offset crossed complete overshoot (forward).** -/ +theorem minimal_closedForm_of_negCross_overshoot {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hbegin : r.begin.offset < 0) + (hmore : res.more = false) (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : + Minimal r res (closedFormNegCrossOvershoot r res) := by + unfold closedFormNegCrossOvershoot + by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r + · rw [ite_eq_left hclamp] + exact minimal_fullRange_of_negCross_clamp hread hrev hmore hne hbegin hend hhi hcross hclamp + · rw [ite_eq_right hclamp] + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := + headD_eq_head_of_ne hne + rw [hheadD] + exact minimal_topOfLast_of_negCross_nonclamp hread hrev hmore hne hbegin hend hhi hcross + (lt_of_not_ge hclamp) + + +theorem getLastD_eq_getLast_of_ne {l : List Key} (h : l ≠ []) : + l.getLastD ([] : Key) = l.getLast h := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast h]; rfl + +/-- The crossed nonpositive-begin complete overshoot region (reversed traversal). -/ +abbrev negCrossOvershootRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key + +/-- The reversed closed form, expressed through the forward rule on the flipped read. -/ +noncomputable def closedFormNegCrossOvershootRev (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossOvershoot { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- **Closed form for the negative-offset crossed complete overshoot (reversed).** -/ +theorem minimal_closedForm_of_negCross_overshoot_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossOvershootRev r res) : + Minimal r res (closedFormNegCrossOvershootRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hhi, hcross⟩ := h + 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 hhi' : ∀ k ∈ res'.keys, ((k : Key) : WithTop Key) < r'.end_.key := by + intro k hk + exact hhi k (by simpa [res'] using hk) + have hhead' : res'.keys.head hne' = res.keys.getLast hne := by + simpa [res'] using List.head_reverse (l := res.keys) (by simpa [res'] using hne') + have hcross' : ((res'.keys.head hne' : Key) : WithTop Key) < r'.begin.key := by + rw [hhead'] + rw [show res.keys.getLastD ([] : Key) = res.keys.getLast hne from + getLastD_eq_getLast_of_ne hne] at hcross + simpa [r'] using hcross + have hmin' : Minimal r' res' (closedFormNegCrossOvershoot r' res') := + minimal_closedForm_of_negCross_overshoot hread' rfl hbegin rfl hne' hend hhi' hcross' + exact (minimal_flip (r := r) (res := res) (cr := closedFormNegCrossOvershoot r' res') + (b := true) hrev hmore).mp hmin' + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossComplete.lean b/FormalConflictRange/NegCrossComplete.lean new file mode 100644 index 0000000..5455858 --- /dev/null +++ b/FormalConflictRange/NegCrossComplete.lean @@ -0,0 +1,2487 @@ +/- +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.NonposClosedForm +import FormalConflictRange.NegCrossClosedForm + +/-! +# Crossed negative-begin complete cells + +At a negative begin offset that is *nonclamping* (`beginSlack r < reportedBelowBegin r res`), +a compatible complete forward database resolves its begin selector exactly onto the reported +head, independently of the end selector. This module records that resolution and the induced +count identity `#stored in [head, begin.key) = beginSlack r + 1`, and uses them to close the +`end.offset ≤ 0` cell: the minimal range is `[head, end_.key)`. +-/ + +namespace FormalConflictRange + +/-- Counting keys below a `WithTop` bound splits at a genuine key `a ≤ B` into those +below `a` and those in `[a, B)`. -/ +theorem countP_lt_eq_add_filter_withTop (l : List Key) (hnd : l.Nodup) {a : Key} + {B : WithTop Key} (haB : (a : WithTop Key) ≤ B) : + l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + l.countP (fun k : Key => decide (k < a)) + + (l.filter (fun k : Key => decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < B))).length := by + cases B with + | top => + have htop : l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = + l.length := countP_lt_top + have hfilter : l.filter (fun k : Key => decide ((a : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = + l.filter (fun k : Key => decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact and_iff_left (WithTop.coe_lt_top k) + have hlen := List.length_eq_length_filter_add (l := l) + (f := fun k : Key => decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key))) + have hcompl : (l.filter (fun k : Key => !decide ((a : WithTop Key) ≤ + ((k : Key) : WithTop Key)))).length = l.countP (fun k : Key => decide (k < a)) := by + rw [List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + rw [show (!decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key))) = + decide (¬ ((a : WithTop Key) ≤ ((k : Key) : WithTop Key))) by rw [decide_not]] + rw [decide_eq_decide] + rw [WithTop.coe_le_coe] + exact not_le + rw [htop, hfilter, ← hcompl] + omega + | coe ke => + have hab : a ≤ ke := WithTop.coe_le_coe.mp haB + have h := countP_lt_eq_add_filter l hnd hab + have hcoe : l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + ((ke : Key) : WithTop Key))) = l.countP (fun k : Key => decide (k < ke)) := + countP_lt_coe ke + have hf : (l.filter (fun k : Key => decide ((a : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((ke : Key) : WithTop Key)))).length = + (l.filter (fun x : Key => decide (a ≤ x ∧ x < ke))).length := by + congr 1 + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + simp only [WithTop.coe_le_coe, WithTop.coe_lt_coe] + rw [hcoe, h, hf] + +/-- **The begin resolution in the nonclamping negative-offset complete cell.** When the +reported keys below `begin.key` outnumber the begin slack, every compatible complete +forward database resolves its begin selector exactly onto the reported head, whatever +the end selector is. -/ +theorem resolve_begin_eq_head_of_negCross_compat' {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + 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 + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + have hmin_le : reportedBelowBegin r res ≤ cB := List.Sublist.countP_le hsub + have hcB_ge : beginSlack r + 1 ≤ cB := + Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) + have hanchor_ge : 0 ≤ r.begin.anchor db := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge + omega + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hhead_ck : head ∈ RangeRead.completeKeys db r := by + rw [← hres_eq]; exact hhead_mem + have hle_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hhead_ck).2.2 + 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_ge (le_of_not_gt h)) + have hresolve_get : resolve db r.begin = + ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) := + resolve_eq_getElem hanchor_ge hltlen + set k : Key := db.keys[(r.begin.anchor db).toNat] with hk + have hk_stored : k ∈ db.keys := List.getElem_mem hltlen + have hk_le_head : k ≤ head := WithTop.coe_le_coe.mp (by rw [← hresolve_get]; exact hle_head) + have hk_le : resolve db r.begin ≤ ((k : Key) : WithTop Key) := by rw [hresolve_get] + have hk_lt : ((k : Key) : WithTop Key) < resolve db r.end_ := by + calc ((k : Key) : WithTop Key) = resolve db r.begin := hresolve_get.symm + _ ≤ ((head : Key) : WithTop Key) := hle_head + _ < resolve db r.end_ := hhead_lt_res + have hk_ck : k ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨hk_stored, hk_le, hk_lt⟩ + have hk_mem_res : k ∈ res.keys := by rw [hres_eq]; exact hk_ck + have hk_ge_head : head ≤ k := + (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk_mem_res + have hk_eq : k = head := le_antisymm hk_le_head hk_ge_head + rw [← hk_eq] + exact hresolve_get + +/-- **The begin count identity in the nonclamping negative-offset complete cell.** Exactly +`beginSlack r + 1` stored keys lie in `[head, begin.key)`. -/ +theorem negCross_filter_begin_length {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + (db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + 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 + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + have hmin_le : reportedBelowBegin r res ≤ cB := List.Sublist.countP_le hsub + have hcB_ge : beginSlack r + 1 ≤ cB := + Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) + have hL := resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hanchor_ge : 0 ≤ r.begin.anchor db := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge + omega + have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro htop; rw [hL] at htop + exact absurd htop (WithTop.coe_ne_top (a := head)) + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + by_contra h + exact hnot_top (resolve_eq_top hanchor_ge (le_of_not_gt h)) + have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor_ge hltlen + have hidx : db.keys[(r.begin.anchor db).toNat] = head := by + have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) = + ((head : Key) : WithTop Key) := by rw [← hget, hL] + exact WithTop.coe_injective hcoe + have hrank : db.keys.countP (fun k => decide (k < head)) = (r.begin.anchor db).toNat := + countP_lt_eq_of_getElem db.sorted hltlen hidx + 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 htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by + 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 hcH_add : (db.keys.countP (fun k => decide (k < head))) + (beginSlack r + 1) = cB := by + rw [hrank, htoNat] + exact Nat.sub_add_cancel hcB_ge + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hsplit' : cB = (db.keys.countP (fun k => decide (k < head))) + + (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := by + rw [hcB]; exact hsplit + omega + +/-- **The begin resolution from the interval count.** If exactly `beginSlack r + 1` +stored keys lie in `[head, begin.key)` and the head itself is stored above the crossed +threshold, then the begin selector resolves exactly onto the head. Unlike +`resolve_begin_eq_head_of_negCross_compat'`, this needs no compatibility hypothesis, so +it transfers across databases agreeing on the interval. -/ +theorem resolve_begin_eq_head_of_filter_len {db : DB} {r : RangeRead} {head : Key} + (hbegin : r.begin.offset < 0) (hhead : head ∈ db.keys) + (hcross : ((head : Key) : WithTop Key) < r.begin.key) + (hlen : (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1) : + resolve db r.begin = ((head : Key) : WithTop Key) := by + set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) + with hcB + set cH := db.keys.countP (fun k : Key => decide (k < head)) with hcH + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + rw [hlen] at hsplit + have hsplit' : cB = cH + (beginSlack r + 1) := by + rw [← hcB, ← hcH] at hsplit + exact hsplit + have hanchor : r.begin.anchor db = (cH : ℤ) := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB, hsplit'] + push_cast + ring + have hanchor_nonneg : 0 ≤ r.begin.anchor db := by rw [hanchor]; exact Int.natCast_nonneg _ + set n := (r.begin.anchor db).toNat with hn + have htoNat : n = cH := by rw [hn, hanchor, Int.toNat_natCast] + have hlt : n < db.keys.length := by + rw [htoNat, hcH] + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead + rw [countP_lt_eq_of_getElem db.sorted hi hix] + exact hi + have hkey : db.keys[n] = head := by + simp only [htoNat, hcH] + exact getElem_countP_lt db.sorted (x := head) hhead + rw [show resolve db r.begin = ((db.keys[n] : Key) : WithTop Key) by + rw [resolve_eq_getElem hanchor_nonneg (hn ▸ hlt)]] + rw [hkey] + +/-- **The crossed threshold stays at or below the end key** in the nonclamping +negative-offset complete cell with a nonpositive end offset. Counting the reported keys +below `begin.key` from both the begin and the end resolution rules rules out +`end_.key < begin.key`. -/ +theorem negCross_begin_le_end {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hend : r.end_.offset ≤ 0) : + r.begin.key ≤ r.end_.key := by + by_contra hcon + have hEB : r.end_.key < r.begin.key := lt_of_not_ge hcon + set head : Key := res.keys.head hne with hhead + set q := beginSlack r with hq + set e := (-r.end_.offset).toNat with he + set cH := db.keys.countP (fun k : Key => decide (k < head)) with hcH + set cE := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) + with hcE + set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) + with hcB + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hhead_ck := RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem) + have hhead_stored : head ∈ db.keys := hhead_ck.1 + have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db r.end_ := hhead_ck.2.2 + have hL := resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hlenB := negCross_filter_begin_length hcomp hrev hmore hne hbegin hcross hnonclamp + -- The number of stored keys below `begin.key` splits at the head. + have hsplitB := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + rw [hlenB] at hsplitB + have hcB_eq : cB = cH + (q + 1) := by + rw [← hcB, ← hcH, ← hq] at hsplitB + exact hsplitB + -- The end resolution is the `e`-th key from the top below `end_.key`. + have he_cast : (e : ℤ) = -r.end_.offset := by + rw [he]; exact Int.toNat_of_nonneg (by omega) + have hanchorE : r.end_.anchor db = (cE : ℤ) - (e : ℤ) - 1 := by + unfold Selector.anchor + rw [← hcE] + omega + have hres_not_nil : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hnil + rw [hnil] at hhead_lt_res + exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _)) + have hanchorE_nonneg : 0 ≤ r.end_.anchor db := by + by_contra hneg + exact hres_not_nil (resolve_eq_nil (lt_of_not_ge hneg)) + have hres_not_top : resolve db r.end_ ≠ (⊤ : WithTop Key) := + resolve_ne_top_of_offset_nonpos hend + have hltE : (r.end_.anchor db).toNat < db.keys.length := by + by_contra hge + exact hres_not_top (resolve_eq_top hanchorE_nonneg (le_of_not_gt hge)) + have htoNatE : (r.end_.anchor db).toNat = cE - e - 1 := by + have h1 : e + 1 ≤ cE := by + by_contra hlt + have hec : cE ≤ e := by omega + have hanc : r.end_.anchor db < 0 := by + rw [hanchorE]; omega + exact hres_not_nil (resolve_eq_nil hanc) + have hz : r.end_.anchor db = ((cE - e - 1 : ℕ) : ℤ) := by + rw [hanchorE] + have hsub : cE - e - 1 = cE - (e + 1) := by omega + rw [hsub, Nat.cast_sub h1] + push_cast + ring + rw [hz, Int.toNat_natCast] + have hcountE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + resolve db r.end_)) = cE - e - 1 := by + rw [countP_lt_resolve, htoNatE] + have hltE' : cE - e - 1 < db.keys.length := by rw [← htoNatE]; exact hltE + exact Nat.min_eq_left (le_of_lt hltE') + -- The reported keys are exactly the stored keys in `[head, resolve end)`. + have hfilter_eq : db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys := by + rw [hres_eq] + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + rw [hL] + have hsplitE := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := resolve db r.end_) (le_of_lt hhead_lt_res) + rw [hfilter_eq, hcountE, ← hcH] at hsplitE + have hlenRes : res.keys.length = (cE - e - 1) - cH := by omega + -- Under `end_.key < begin.key` every reported key lies below `begin.key`. + have hall_lt_B : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.begin.key := by + intro k hk + have hk_lt_res : ((k : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hk)).2.2 + have hres_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + exact lt_trans (lt_of_lt_of_le hk_lt_res hres_le) hEB + have hreported : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length_filter] + congr 1 + apply List.filter_eq_self.mpr + intro k hk + simpa using hall_lt_B k hk + have hstrict : q < res.keys.length := by + rw [← hreported] + exact hnonclamp + have hcE_le : cE ≤ cB := by + rw [hcE, hcB] + exact countP_le_countP_of_imp (fun k _ hk => by + rw [decide_eq_true_eq] at hk ⊢ + exact lt_trans hk hEB) + omega + +/-- Restricting an agreement on `[a, E)` to the smaller interval `[a, B)` when `B ≤ E`. -/ +theorem filter_between_eq_of_filter_end_eq {db₁ db₂ : DB} {a : Key} {B E : WithTop Key} + (hBE : B ≤ E) + (h : db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E)) = + db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E))) : + db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) = + db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) := by + have h₁ : db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) = + (db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < B)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [← Bool.decide_and, decide_eq_decide] + constructor + · rintro ⟨h1, h2⟩; exact ⟨h2, h1, lt_of_lt_of_le h2 hBE⟩ + · rintro ⟨h2, h1, _⟩; exact ⟨h1, h2⟩ + have h₂ : db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) = + (db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < B)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [← Bool.decide_and, decide_eq_decide] + constructor + · rintro ⟨h1, h2⟩; exact ⟨h2, h1, lt_of_lt_of_le h2 hBE⟩ + · rintro ⟨h2, h1, _⟩; exact ⟨h1, h2⟩ + rw [h₁, h, ← h₂] + +/-- The crossed negative-begin complete low cell: forward, complete, nonempty, with a +nonpositive end offset, `begin.key` above the reported head, and a nonclamping negative +begin offset. -/ +abbrev negCrossLowFwd (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + beginSlack r < reportedBelowBegin r res + +/-- The closed form for the crossed negative-begin complete low cell: `[head, end_.key)`. -/ +noncomputable def closedFormNegCrossLowFwd (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ + +/-- **The crossed negative-begin complete low cell is minimal.** On the nonclamping +negative-offset region with `end_.offset ≤ 0`, the minimal conflict range is exactly the +reported head up to `end_.key`. The begin selector resolves onto the head, and the end +selector stays below `end_.key`, so agreement on `[head, end_.key)` fixes the whole +complete window; keys between the last reported key and `end_.key` are necessary by the +nonpositive-end rule. -/ +theorem minimal_closedForm_of_negCross_lowFwd {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossLowFwd r res) : + Minimal r res (closedFormNegCrossLowFwd r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp⟩ := hcell + 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 + rw [← hheadD]; exact hcrossD + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead + rw [show closedFormNegCrossLowFwd r res = + ⟨((head : Key) : WithTop Key), r.end_.key⟩ by + unfold closedFormNegCrossLowFwd; rw [hheadD']] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored : head ∈ db₁.keys := + RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne) + have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_compat' hcomp₁ hrev hmore hne hbegin hnonclamp + have hB_le_E : r.begin.key ≤ r.end_.key := + negCross_begin_le_end hcomp₁ hrev hmore hne hbegin hcross hnonclamp hend + have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := + negCross_filter_begin_length hcomp₁ hrev hmore hne hbegin hcross hnonclamp + -- Agreement on the larger window `[head, end_.key)`. + have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by + simpa [AgreeOn, ConflictRange.keys] using hagree + -- Agreement on the begin window `[head, begin.key)`. + have hfiltB := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt + -- The head is stored in `db₂`. + have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := + List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩ + have hhead_stored₂ : head ∈ db₂.keys := + List.mem_of_mem_filter (by rw [← hfiltB]; exact hhead_in₁) + have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by rw [← hfiltB]; exact hlen₁ + have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂ + -- The end resolution is fixed by agreement on `[head, end_.key)`. + have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db₁ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.2 + have hres_le : resolve db₁ r.end_ ≤ r.end_.key := + resolve_le_key_of_offset_nonpos hend + have hhead_lt_E : ((head : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hhead_lt_res hres_le + have hR : resolve db₂ r.end_ = resolve db₁ r.end_ := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₁) (s := r.end_) with hn | ht | ⟨q, hqdb, hqeq⟩ + · 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 hqne : q ≠ [] := by + intro hqnil + rw [hqnil] at hqeq + rw [hqeq] at hhead_lt_res + exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _)) + have hqspec : (db₁.keys.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < r.end_.key))).reverse[ + (-r.end_.offset).toNat]? = some q := + resolve_filter_reverse_eq_some_of_ne_nil hend hqne hqeq + have hqa : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := by + rw [← hqeq]; exact le_of_lt hhead_lt_res + have hR' : resolve db₂ r.end_ = ((q : Key) : WithTop Key) := + resolve_eq_of_agree_between (db₁ := db₁) (db₂ := db₂) (s := r.end_) hend rfl + (le_of_lt hhead_lt_E) hfilt hqspec hqa + rw [hR', hqeq] + -- The complete windows agree. + have hP : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) := + filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := resolve db₁ r.end_) (E := r.end_.key) hres_le hfilt + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), hL₁, hL₂, hR] + exact hP + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_le : head ≤ k := WithTop.coe_le_coe.mp hlo + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result ⟨db₀, hcomp₀⟩ hhead_mem (List.getLast_mem hne) + hk_le hklast + · exact fun cr hs => necessary_last_to_end_key ⟨db₀, hcomp₀⟩ hrev hmore hne (by omega) + cr hs (le_of_lt (lt_of_not_ge hklast)) hhi + +/-- The crossed negative-begin complete low cell in reversed traversal. -/ +abbrev negCrossLowFwdRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + beginSlack r < reportedBelowBegin r res + +/-- The reversed closed form for the crossed negative-begin complete low cell. -/ +noncomputable def closedFormNegCrossLowFwdRev (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ + +/-- **The reversed crossed negative-begin complete low cell is minimal**, by flipping the +traversal and applying the forward rule. -/ +theorem minimal_closedForm_of_negCross_lowFwd_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossLowFwdRev r res) : + Minimal r res (closedFormNegCrossLowFwdRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp⟩ := hcell + 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 hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hcell' : negCrossLowFwd { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_⟩ + · rw [hheadD]; exact hcrossD + · rw [hcount]; exact hnonclamp + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_lowFwd hread' hcell' + have hcr : closedFormNegCrossLowFwd { r with reverse := false } + { keys := res.keys.reverse, more := false } = closedFormNegCrossLowFwdRev r res := by + unfold closedFormNegCrossLowFwd closedFormNegCrossLowFwdRev + rw [hheadD] + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +/-- The crossed negative-begin complete cell at `end.offset = 1` with `begin.key ≤ end_.key`. -/ +abbrev negCrossEndOne (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset = 1 ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + beginSlack r < reportedBelowBegin r res ∧ r.begin.key ≤ r.end_.key + +/-- The closed form for the crossed negative-begin complete cell at `end.offset = 1`: +`[head, end_.key)`. -/ +noncomputable def closedFormNegCrossEndOne (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ + +/-- **The crossed negative-begin complete cell at `end.offset = 1` is minimal** when +`begin.key ≤ end_.key`. Agreement on `[head, end_.key)` fixes the begin resolution (through +the count identity on `[head, begin.key)`) and the complete window, which with end offset one +is exactly the stored keys in `[head, end_.key)`; keys above the reported last are necessary +by `necessary_last_to_end_key`. -/ +theorem minimal_closedForm_of_negCross_endOne {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossEndOne r res) : + Minimal r res (closedFormNegCrossEndOne r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hB_le_E⟩ := hcell + 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 + rw [← hheadD]; exact hcrossD + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead + rw [show closedFormNegCrossEndOne r res = + ⟨((head : Key) : WithTop Key), r.end_.key⟩ by + unfold closedFormNegCrossEndOne; rw [hheadD']] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored : head ∈ db₁.keys := + RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne) + have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db₁ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.2 + have hhead_lt_E : ((head : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_one hend hhead_stored hhead_lt_res + have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_compat' hcomp₁ hrev hmore hne hbegin hnonclamp + have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by + simpa [AgreeOn, ConflictRange.keys] using hagree + have hfiltB := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt + have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := + negCross_filter_begin_length hcomp₁ hrev hmore hne hbegin hcross hnonclamp + have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := + List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩ + have hhead_stored₂ : head ∈ db₂.keys := + List.mem_of_mem_filter (by rw [← hfiltB]; exact hhead_in₁) + have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by rw [← hfiltB]; exact hlen₁ + have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂ + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_filter_of_end_one hrev hend, + completeKeys_eq_filter_of_end_one (db := db₂) hrev hend, hL₁, hL₂, + List.filter_filter, List.filter_filter] + simpa only [Bool.decide_and] using hfilt + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_le : head ≤ k := WithTop.coe_le_coe.mp hlo + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result ⟨db₀, hcomp₀⟩ hhead_mem (List.getLast_mem hne) + hk_le hklast + · exact fun cr hs => necessary_last_to_end_key ⟨db₀, hcomp₀⟩ hrev hmore hne (by omega) + cr hs (le_of_lt (lt_of_not_ge hklast)) hhi + +/-- The reversed crossed negative-begin complete cell at `end.offset = 1` with +`begin.key ≤ end_.key`. -/ +abbrev negCrossEndOneRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset = 1 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + beginSlack r < reportedBelowBegin r res ∧ r.begin.key ≤ r.end_.key + +/-- The reversed closed form for the crossed negative-begin complete cell at +`end.offset = 1`: `[last reported, end_.key)`. -/ +noncomputable def closedFormNegCrossEndOneRev (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ + +/-- **The reversed crossed negative-begin complete cell at `end.offset = 1` is minimal**, +by flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_endOne_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossEndOneRev r res) : + Minimal r res (closedFormNegCrossEndOneRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hB_le_E⟩ := hcell + 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 hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hcell' : negCrossEndOne { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, hB_le_E⟩ + · rw [hheadD]; exact hcrossD + · rw [hcount]; exact hnonclamp + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_endOne hread' hcell' + have hcr : closedFormNegCrossEndOne { r with reverse := false } + { keys := res.keys.reverse, more := false } = closedFormNegCrossEndOneRev r res := by + unfold closedFormNegCrossEndOne closedFormNegCrossEndOneRev + rw [hheadD] + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +/-- Inserting a gap key strictly between the reported head and `begin.key` lifts the begin +resolution strictly above the head. This is the negative-offset analogue of +`resolve_zero_begin_le_of_mem_below` used for the crossed long cells: the head is the +`beginSlack r + 1`-st key below `begin.key`, so an extra key in the gap makes the resolved +index one past the head. -/ +theorem resolve_begin_insert_gt {db : DB} {r : RangeRead} {head k : Key} + (hbegin : r.begin.offset < 0) (hhead : head ∈ db.keys) + (hcross : ((head : Key) : WithTop Key) < r.begin.key) + (hknot : k ∉ db.keys) (hk_gt : head < k) + (hk_lt : ((k : Key) : WithTop Key) < r.begin.key) + (hlen : (db.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1) : + ((head : Key) : WithTop Key) < resolve (db.insert k hknot) r.begin := by + set db' := db.insert k hknot with hdb' + set cB := db.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) + with hcB + set cH := db.keys.countP (fun x : Key => decide (x < head)) with hcH + have hsplitB := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + rw [hlen] at hsplitB + have hcB_eq : cB = cH + (beginSlack r + 1) := by + rw [← hcB, ← hcH] at hsplitB + exact hsplitB + have hcB' : (db'.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < + r.begin.key))) = cB + 1 := by + rw [hdb'] + show (db.keys.orderedInsert (· ≤ ·) k).countP _ = cB + 1 + rw [List.Perm.countP_eq _ (List.perm_orderedInsert (· ≤ ·) k db.keys)] + rw [List.countP_cons] + simp only [decide_eq_true_eq, hk_lt, if_true] + rw [hcB] + have hcH' : (db'.keys.countP (fun x : Key => decide (x < head))) = cH := by + rw [hdb'] + show (db.keys.orderedInsert (· ≤ ·) k).countP _ = cH + rw [List.Perm.countP_eq _ (List.perm_orderedInsert (· ≤ ·) k db.keys)] + rw [List.countP_cons] + have hkf : (decide (k < head)) = false := by + rw [decide_eq_false_iff_not] + exact not_lt.mpr (le_of_lt hk_gt) + simp only [hkf, Bool.false_eq_true, if_false] + rw [hcH] + omega + have hanchor : r.begin.anchor db' = ((cH : ℤ) + 1) := by + rw [anchor_eq_count_sub_slack db' r (le_of_lt hbegin), hcB'] + have h : (cB : ℤ) = (cH : ℤ) + (beginSlack r : ℤ) + 1 := by + rw [hcB_eq]; push_cast; ring + push_cast + omega + have hanchor_nonneg : 0 ≤ r.begin.anchor db' := by rw [hanchor]; omega + have htoNat : (r.begin.anchor db').toNat = cH + 1 := by rw [hanchor]; simp + have hltlen : cH + 1 < db'.keys.length := by + have h1 : cH + 1 < cB + 1 := by omega + have h2 : cB + 1 ≤ db'.keys.length := by rw [← hcB']; exact List.countP_le_length + omega + have hres : resolve db' r.begin = (((db'.keys[cH + 1] : Key)) : WithTop Key) := by + rw [resolve_eq_getElem hanchor_nonneg (by rw [htoNat]; exact hltlen)] + simp only [htoNat] + have hhead' : head ∈ db'.keys := (DB.mem_insert_iff hknot).mpr (Or.inr hhead) + have hhead_get : db'.keys[cH] = head := by + have h := getElem_countP_lt db'.sorted (x := head) hhead' + simpa only [hcH'] using h + have hcH_lt : cH < db'.keys.length := by omega + have hmono : db'.keys[cH] < db'.keys[cH + 1] := by + rw [db'.sorted.getElem_lt_getElem_iff] + omega + rw [hres] + exact WithTop.coe_lt_coe.mpr (by simpa only [hhead_get] using hmono) + + +/-- The crossed negative-begin complete cell at `end.offset = 1` with `end_.key < begin.key`. -/ +abbrev negCrossEndOneLong (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset = 1 ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + beginSlack r < reportedBelowBegin r res ∧ r.end_.key < r.begin.key + +/-- The closed form for the crossed negative-begin complete long cell at `end.offset = 1`: +`[head, begin.key)`. -/ +noncomputable def closedFormNegCrossEndOneLong (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ + +/-- **The crossed negative-begin complete long cell at `end.offset = 1` is minimal.** The +complete window is the stored keys in `[head, end_.key)`, so agreement on `[head, begin.key)` +restricts to fix it. Keys between `end_.key` and `begin.key` are forced: one is never +stored in a compatible database (all `beginSlack r + 1` stored keys of `[head, begin.key)` +are already reported below `end_.key`), and inserting it lifts the begin resolution above +the reported head. -/ +theorem minimal_closedForm_of_negCross_endOne_long {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossEndOneLong r res) : + Minimal r res (closedFormNegCrossEndOneLong r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hlong⟩ := hcell + 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 + rw [← hheadD]; exact hcrossD + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead + rw [show closedFormNegCrossEndOneLong r res = + ⟨((head : Key) : WithTop Key), r.begin.key⟩ by + unfold closedFormNegCrossEndOneLong; rw [hheadD']] + have hreadable : Readable r res := ⟨db₀, hcomp₀⟩ + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored₀ : head ∈ db₀.keys := + RangeRead.compatible_keys_subset hcomp₀ (by rw [hhead]; exact List.head_mem hne) + have hL₀ : resolve db₀ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_compat' hcomp₀ hrev hmore hne hbegin hnonclamp + have hlen₀ : (db₀.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := + negCross_filter_begin_length hcomp₀ hrev hmore hne hbegin hcross hnonclamp + have hhead_lt_E₀ : ((head : Key) : WithTop Key) < r.end_.key := by + have hh := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hhead_mem)).2.2 + exact lt_end_key_of_lt_resolve_end_one hend hhead_stored₀ hh + have hall_reported_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + have hkst : k ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hk + have hklt := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hk)).2.2 + exact lt_end_key_of_lt_resolve_end_one hend hkst hklt + have hall_reported_lt_B : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.begin.key := + fun k hk => lt_trans (hall_reported_lt_E k hk) hlong + have hreported_eq : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact hall_reported_lt_B k hk + have hq_lt : beginSlack r < res.keys.length := by + rw [← hreported_eq]; exact hnonclamp + have hsub₀ : List.Sublist res.keys db₀.keys := result_sublist_db_of_compatible hcomp₀ hrev + have hlen_le : res.keys.length ≤ beginSlack r + 1 := by + have hcount_res : res.keys.countP (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.length := by + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hk + have hs : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hhead_le : head ≤ k := by + rw [← hik, hhead, List.head_eq_getElem hne] + exact hs.getElem_le_getElem_iff.mpr (Nat.zero_le i) + exact ⟨WithTop.coe_le_coe.mpr hhead_le, hall_reported_lt_B k hk⟩ + rw [← hcount_res] + calc res.keys.countP (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) + ≤ db₀.keys.countP (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) := + List.Sublist.countP_le hsub₀ + _ = (db₀.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length := + List.countP_eq_length_filter + _ = beginSlack r + 1 := hlen₀ + have hres_len : res.keys.length = beginSlack r + 1 := le_antisymm hlen_le (by omega) + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.begin.key⟩ db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored : head ∈ db₁.keys := + RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne) + have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_compat' hcomp₁ hrev hmore hne hbegin hnonclamp + have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by + simpa [AgreeOn, ConflictRange.keys] using hagree + have hfiltE : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := + filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := r.end_.key) (E := r.begin.key) (le_of_lt hlong) hfilt + have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := + negCross_filter_begin_length hcomp₁ hrev hmore hne hbegin hcross hnonclamp + have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => + decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := + List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩ + have hhead_stored₂ : head ∈ db₂.keys := + List.mem_of_mem_filter (by rw [← hfilt]; exact hhead_in₁) + have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by rw [← hfilt]; exact hlen₁ + have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂ + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_filter_of_end_one hrev hend, + completeKeys_eq_filter_of_end_one (db := db₂) hrev hend, hL₁, hL₂, + List.filter_filter, List.filter_filter] + simpa only [Bool.decide_and] using hfiltE + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_head : head ≤ k := WithTop.coe_le_coe.mp hlo + have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := hhi + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hreadable hhead_mem (List.getLast_mem hne) hk_head hklast + · have hk_gt_last : res.keys.getLast hne < k := lt_of_not_ge hklast + have hhead_le_last : head ≤ res.keys.getLast hne := by + have hs : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + rw [hhead, List.head_eq_getElem hne, List.getLast_eq_getElem hne] + exact hs.getElem_le_getElem_iff.mpr (by + have := List.length_pos_of_ne_nil hne + omega) + have hk_gt_head : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last + by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key + · exact fun cr hs => necessary_last_to_end_key hreadable hrev hmore hne (le_of_eq hend) + cr hs (le_of_lt hk_gt_last) hkE + · have hkE_le : r.end_.key ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkE + intro cr hs + by_contra hkcr + have hknot : k ∉ db₀.keys := by + intro hkdb + have hPk : (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) k = true := by + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr hk_head, hk_lt_B⟩ + have hkfil : k ∈ db₀.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) := + List.mem_filter.mpr ⟨hkdb, hPk⟩ + have hres_filter_self : res.keys.filter (fun x : Key => + decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hx + have hsrt : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hhead_le : head ≤ x := by + rw [← hix, hhead, List.head_eq_getElem hne] + exact hsrt.getElem_le_getElem_iff.mpr (Nat.zero_le i) + exact ⟨WithTop.coe_le_coe.mpr hhead_le, hall_reported_lt_B x hx⟩ + have hfilt_eq : db₀.keys.filter (fun x : Key => + decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun x : Key => + decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key)) := by + symm + apply List.Sublist.eq_of_length + · exact List.Sublist.filter _ hsub₀ + · rw [hlen₀, hres_filter_self, hres_len] + have hkres : k ∈ res.keys := by + have hmf : k ∈ res.keys.filter (fun x : Key => + decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key)) := by + rw [← hfilt_eq]; exact hkfil + exact List.mem_of_mem_filter hmf + exact absurd (hall_reported_lt_E k hkres) (not_lt.mpr hkE_le) + have hhead' : head ∈ (db₀.insert k hknot).keys := + (DB.mem_insert_iff hknot).mpr (Or.inr hhead_stored₀) + have hgt : ((head : Key) : WithTop Key) < resolve (db₀.insert k hknot) r.begin := + resolve_begin_insert_gt hbegin hhead_stored₀ hcross hknot hk_gt_head hk_lt_B hlen₀ + have hhead_notin : head ∉ RangeRead.completeKeys (db₀.insert k hknot) r := by + rw [completeKeys_eq_filter_of_end_one hrev hend] + intro hmem + simp only [List.mem_filter, decide_eq_true_eq] at hmem + exact absurd hmem.2 (not_le.mpr hgt) + have hcomp₁ : RangeRead.Compatible (db₀.insert k hknot) r res := + (hs db₀ _ (ConflictRange.keys_insert_of_not_mem cr db₀ hknot hkcr).symm).mp hcomp₀ + have hres_eq₁ : res.keys = RangeRead.completeKeys (db₀.insert k hknot) r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + exact hhead_notin (by rw [← hres_eq₁]; exact hhead_mem) + + +/-- The reversed crossed negative-begin complete long cell at `end.offset = 1`. -/ +abbrev negCrossEndOneLongRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset = 1 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + beginSlack r < reportedBelowBegin r res ∧ r.end_.key < r.begin.key + +/-- The reversed closed form for the crossed negative-begin complete long cell at +`end.offset = 1`: `[last reported, begin.key)`. -/ +noncomputable def closedFormNegCrossEndOneLongRev (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ + +/-- **The reversed crossed negative-begin complete long cell at `end.offset = 1` is +minimal**, by flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_endOne_long_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossEndOneLongRev r res) : + Minimal r res (closedFormNegCrossEndOneLongRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hlong⟩ := hcell + 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 hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hcell' : negCrossEndOneLong { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, hlong⟩ + · rw [hheadD]; exact hcrossD + · rw [hcount]; exact hnonclamp + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_endOne_long hread' hcell' + have hcr : closedFormNegCrossEndOneLong { r with reverse := false } + { keys := res.keys.reverse, more := false } = closedFormNegCrossEndOneLongRev r res := by + unfold closedFormNegCrossEndOneLong closedFormNegCrossEndOneLongRev + rw [hheadD] + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +/-- A key of a strictly increasing list is at or above the list's head. -/ +private theorem head_le_of_pairwise_negCross {l : List Key} (h : l.Pairwise (· < ·)) + (hne : l ≠ []) {a : Key} (ha : a ∈ l) : l.head hne ≤ a := + (h.imp le_of_lt).rel_head ha + +private theorem mem_inRange_negCross {db : DB} {lo hi : WithTop Key} {k : Key} : + k ∈ RangeRead.inRange db.keys lo hi ↔ + k ∈ db.keys ∧ lo ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < hi := by + unfold RangeRead.inRange + rw [List.mem_filter, decide_eq_true_eq] + +/-- Every stored key of `db` at or above every key of the window `[⊥, hi)` is at or +above `hi` itself: a stored key below `hi` would lie in the window, so the window's +least key sits below it. -/ +private theorem le_of_not_mem_inRange_nil_negCross {db : DB} {hi : WithTop Key} (m : Key) + (hne : RangeRead.inRange db.keys (([] : Key) : WithTop Key) hi ≠ []) + (hm : ∀ y ∈ RangeRead.inRange db.keys (([] : Key) : WithTop Key) hi, m ≤ y) + (x : Key) (hx : x ∈ db.keys) : m ≤ x := by + by_cases hxin : x ∈ RangeRead.inRange db.keys (([] : Key) : WithTop Key) hi + · exact hm x hxin + · have hmhi : ((m : Key) : WithTop Key) < hi := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hm _ (List.head_mem hne))) + (mem_inRange_negCross.mp (List.head_mem hne)).2.2 + by_contra hcon + exact hxin (mem_inRange_negCross.mpr ⟨hx, coe_nil_le_key_withTop x, + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (not_le.mp hcon)) (le_of_lt hmhi)⟩) + +/-- In a non-reversed read whose begin selector is clamped to the empty key in a +compatible database, every key strictly below the reported head is necessary. -/ +theorem necessary_below_head_of_anchor_nonpos_negCross {r : RangeRead} {res : Result} (db₀ : DB) + (hread : RangeRead.Compatible db₀ r res) (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hclamp : r.begin.anchor db₀ < 0) {k : Key} + (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + intro cr hs + by_contra hcr + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := RangeRead.compatible_keys_prefix hread + have hnil₀ : resolve db₀ r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hclamp + have hck₀ : RangeRead.completeKeys db₀ r = + RangeRead.inRange db₀.keys (([] : Key) : WithTop Key) (resolve db₀ r.end_) := by + simp only [RangeRead.completeKeys] + rw [hnil₀, hrev] + simp + have hwin₀ : RangeRead.inRange db₀.keys (([] : Key) : WithTop Key) (resolve db₀ r.end_) ≠ [] := by + rw [← hck₀] + exact hpre₀.ne_nil hne + have hheadwin : (RangeRead.inRange db₀.keys (([] : Key) : WithTop Key) + (resolve db₀ r.end_)).head hwin₀ = res.keys.head hne := by + have hhead?₀ : (RangeRead.inRange db₀.keys (([] : Key) : WithTop Key) + (resolve db₀ r.end_)).head? = res.keys.head? := by + obtain ⟨t, ht⟩ := hpre₀ + rw [← hck₀, ← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl, + Option.some_or] + exact (List.head_eq_iff_head?_eq_some hwin₀).mpr + (hhead?₀.trans ((List.head_eq_iff_head?_eq_some hne).mp rfl)) + have hheadle₀ : ∀ y ∈ RangeRead.inRange db₀.keys (([] : Key) : WithTop Key) + (resolve db₀ r.end_), res.keys.head hne ≤ y := by + intro y hy + rw [← hheadwin] + exact head_le_of_pairwise_negCross (RangeRead.inRange_pairwise db₀ _ _) hwin₀ hy + have hmin : ∀ x ∈ db₀.keys, res.keys.head hne ≤ x := + fun x hx => le_of_not_mem_inRange_nil_negCross (res.keys.head hne) hwin₀ hheadle₀ x hx + have hk0 : k ∉ db₀.keys := fun hxm => + absurd hk (not_lt.mpr (WithTop.coe_le_coe.mpr (hmin k hxm))) + have hkeys₁ : (db₀.insert k hk0).keys = k :: db₀.keys := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx db₀.sorted hk0] + have hc0 : db₀.keys.countP (fun k' => decide (k' < k)) = 0 := by + rw [List.countP_eq_zero] + intro k' hk' + rw [decide_eq_true_eq] + exact not_lt.mpr (le_of_lt (lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk) (hmin k' hk'))) + rw [hc0] + simp + have hagree : AgreeOn cr db₀ (db₀.insert k hk0) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hk0 hcr).symm + have hcompat₁ : RangeRead.Compatible (db₀.insert k hk0) r res := (hs db₀ _ hagree).mp hread + have hpre₁ : res.keys <+: RangeRead.completeKeys (db₀.insert k hk0) r := + RangeRead.compatible_keys_prefix hcompat₁ + have hcnt : (db₀.insert k hk0).keys.countP + (fun k' => decide (((k' : Key) : WithTop Key) < r.begin.key)) = + db₀.keys.countP (fun k' => decide (((k' : Key) : WithTop Key) < r.begin.key)) + + (if ((k : Key) : WithTop Key) < r.begin.key then 1 else 0) := by + rw [hkeys₁, List.countP_cons] + by_cases hkk : ((k : Key) : WithTop Key) < r.begin.key <;> simp [hkk] + have hanchor : Selector.anchor (db₀.insert k hk0) r.begin ≤ 0 := by + have hneg₀ : Selector.anchor db₀ r.begin ≤ -1 := by + have := hclamp + omega + unfold Selector.anchor at hneg₀ ⊢ + rw [hcnt] + push_cast + split <;> omega + have hbegin₁ : resolve (db₀.insert k hk0) r.begin ≤ ((k : Key) : WithTop Key) := by + rcases lt_or_eq_of_le hanchor with hneg | heq + · rw [resolve_eq_ite, ite_eq_left hneg] + exact coe_nil_le_key_withTop k + · have h0 : 0 ≤ Selector.anchor (db₀.insert k hk0) r.begin := le_of_eq heq.symm + have hidx : (Selector.anchor (db₀.insert k hk0) r.begin).toNat = 0 := by rw [heq]; rfl + have hget : (db₀.insert k hk0).keys[(Selector.anchor (db₀.insert k hk0) r.begin).toNat]? = + some k := by rw [hidx, hkeys₁]; rfl + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0)] + simp only [hget] + exact le_of_eq rfl + by_cases hle : resolve (db₀.insert k hk0) r.end_ ≤ ((k : Key) : WithTop Key) + · have hlt₁ : ((res.keys.head hne : Key) : WithTop Key) < + resolve (db₀.insert k hk0) r.end_ := + (RangeRead.mem_completeKeys.mp (hpre₁.subset (List.head_mem hne))).2.2 + exact absurd (lt_of_lt_of_le (lt_trans hk hlt₁) hle) (lt_irrefl (((k : Key) : WithTop Key))) + · have hck₁ : RangeRead.completeKeys (db₀.insert k hk0) r = + RangeRead.inRange (db₀.insert k hk0).keys + (resolve (db₀.insert k hk0) r.begin) (resolve (db₀.insert k hk0) r.end_) := by + simp only [RangeRead.completeKeys, hrev] + simp + have hpref : (RangeRead.completeKeys (db₀.insert k hk0) r).head? = res.keys.head? := by + obtain ⟨t, ht⟩ := hpre₁ + rw [← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl, Option.some_or] + have hhead?₁ : (RangeRead.completeKeys (db₀.insert k hk0) r).head? = some k := by + rw [hck₁] + simp only [RangeRead.inRange, List.head?_filter] + refine RangeRead.find?_eq_some_of_pairwise + (List.sortedLT_iff_pairwise.mp (db₀.insert k hk0).sorted) ?_ ?_ ?_ + · rw [hkeys₁] + simp + · rw [decide_eq_true_eq] + exact ⟨hbegin₁, lt_of_not_ge hle⟩ + · intro x hx hxa + rw [decide_eq_false_iff_not] + intro hcon + rw [hkeys₁] at hx + have hx0 : x ∈ db₀.keys := by + rcases List.mem_cons.mp hx with hx1 | hx2 + · subst hx1 + exact absurd hxa (lt_irrefl _) + · exact hx2 + exact absurd (lt_of_lt_of_le (lt_trans hxa (WithTop.coe_lt_coe.mp hk)) (hmin x hx0)) + (lt_irrefl x) + have hkeq : k = res.keys.head hne := Option.some.inj + ((hpref.symm.trans hhead?₁).symm.trans ((List.head_eq_iff_head?_eq_some hne).mp rfl)) + exact absurd hk (not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_eq hkeq.symm))) + +/-! +# The crossed negative-begin complete cell at end offset one inside the clamp + +This scratch file develops the missing closed form for the forward crossed +negative-begin complete result at `end.offset = 1` in the clamping regime +`reportedBelowBegin ≤ beginSlack`. +-/ + + +/-- The crossed negative-begin complete cell at `end.offset = 1` in the clamp +regime. -/ +abbrev negCrossClampEndOne (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset = 1 ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The slack beyond the reported keys below `begin.key`. -/ +def clampEndOneSlack (r : RangeRead) (res : Result) : ℕ := + beginSlack r + 1 - reportedBelowBegin r res + +/-- The capacity of the key gap `[end_.key, begin.key)`. -/ +def clampEndOneCap (r : RangeRead) : ℕ∞ := + intervalCard r.end_.key r.begin.key + +/-- The upper endpoint of the clamp end-one cell. -/ +noncomputable def clampEndOneUpper (r : RangeRead) (res : Result) : WithTop Key := + if ((clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r) ∧ + (((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r) ∨ + res.keys.headD ([] : Key) ≠ []) + then r.begin.key else r.end_.key + +/-- The closed form for the crossed negative-begin complete clamp end-one cell. -/ +noncomputable def closedFormNegCrossClampEndOne (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), clampEndOneUpper r res⟩ + +section Structural + +variable {r : RangeRead} {res : Result} + +theorem clampEndOne_headD_eq {hne : res.keys ≠ []} : + res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + +theorem clampEndOne_slack_pos (hcell : negCrossClampEndOne r res) : + 0 < clampEndOneSlack r res := by + obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell + unfold clampEndOneSlack + omega + +theorem clampEndOne_cap_nonneg (r : RangeRead) : (0 : ℕ∞) ≤ clampEndOneCap r := + zero_le + +/-- `U = begin.key` forces `end_.key ≤ begin.key`, because the gap capacity is zero +otherwise and the slack is positive. -/ +theorem clampEndOne_end_le_begin_of_upper_begin (hcell : negCrossClampEndOne r res) + (hupper : clampEndOneUpper r res = r.begin.key) : + r.end_.key ≤ r.begin.key := by + by_contra hcon + have hlt : r.begin.key < r.end_.key := lt_of_not_ge hcon + have hzero : clampEndOneCap r = 0 := by + unfold clampEndOneCap + exact intervalCard_eq_zero_of_le (le_of_lt hlt) + have hs := clampEndOne_slack_pos hcell + unfold clampEndOneUpper at hupper + by_cases hc : ((clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r) ∧ + (((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r) ∨ + res.keys.headD ([] : Key) ≠ []) + · rw [if_pos hc] at hupper + rw [hzero] at hc + exact absurd hc.1 (not_le.mpr (by exact_mod_cast hs)) + · rw [if_neg hc] at hupper + exact absurd hupper (ne_of_lt hlt).symm + +end Structural + + + +section CountBounds + +variable {db : DB} {r : RangeRead} {res : Result} + +theorem clampEndOne_reported_eq_length (hcell : negCrossClampEndOne r res) + (hEB : r.end_.key ≤ r.begin.key) + (hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hres_lt_E k hk) hEB + +theorem clampEndOne_count_le_of_between_bound {db : DB} {r : RangeRead} {res : Result} + (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = + res.keys) + (hm : reportedBelowBegin r res = res.keys.length) + (hEB : r.end_.key ≤ r.begin.key) : + db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.length + + (db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := by + by_cases hEtop : r.end_.key = (⊤ : WithTop Key) + · -- `end_.key = ⊤`: the split key is `⊤`; the interval `[⊤, B)` is empty. + have hEB' : (⊤ : WithTop Key) ≤ r.begin.key := by rw [← hEtop]; exact hEB + have hB : r.begin.key = (⊤ : WithTop Key) := le_antisymm le_top hEB' + rw [hB] + have hcnt : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = + db.keys.length := countP_lt_top + have hlen : res.keys.length = db.keys.length := by + conv_rhs => rw [← hcnt] + rw [← hfilter, hEtop, List.countP_eq_length_filter] + have hempty : db.keys.filter (fun k : Key => decide ((⊤ : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro k _; simp + rw [hcnt, hEtop, hempty] + simp [hlen] + · -- `end_.key = ↑E`: split at the genuine key `E`. + obtain ⟨E, hE⟩ : ∃ E : Key, r.end_.key = (↑E : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h hEtop + | coe E => exact ⟨E, rfl⟩ + have hEB' : ((E : Key) : WithTop Key) ≤ r.begin.key := by rw [← hE]; exact hEB + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := E) (B := r.begin.key) hEB' + have hcntE : db.keys.countP (fun k : Key => decide (k < E)) = res.keys.length := by + rw [← countP_lt_coe E, List.countP_eq_length_filter] + have h := congrArg List.length hfilter + simpa [hE] using h + rw [hcntE] at hsplit + simpa [hE] using hsplit + +theorem clampEndOne_count_le_slack_of_cap_lt (hcell : negCrossClampEndOne r res) + (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = + res.keys) + (hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcap : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞)) : + db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) ≤ + beginSlack r := by + by_cases hEB : r.end_.key ≤ r.begin.key + · have hm := clampEndOne_reported_eq_length hcell hEB hres_lt_E + have hcount := clampEndOne_count_le_of_between_bound (db := db) hfilter hm hEB + have hbetween : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length < + clampEndOneSlack r res := by + have hle : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length : ℕ∞) ≤ + clampEndOneCap r := by + unfold clampEndOneCap + exact between_length_le_intervalCard (db := db) hEB + exact_mod_cast lt_of_le_of_lt hle hcap + obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell + have hs : clampEndOneSlack r res = beginSlack r + 1 - reportedBelowBegin r res := rfl + omega + · -- `begin.key < end_.key`: no room above `end_.key`, and all reported keys are below it. + have hlt : r.begin.key < r.end_.key := lt_of_not_ge hEB + have h₁ : (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hkB : ((k : Key) : WithTop Key) < r.begin.key + · have hkE : ((k : Key) : WithTop Key) < r.end_.key := lt_trans hkB hlt + simp [hkB, hkE] + · simp [hkB] + have hBsubE : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [← h₁, hfilter] + obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell + have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [List.countP_eq_length_filter] + unfold reportedBelowBegin + rw [List.countP_eq_length_filter, hBsubE] + omega + +end CountBounds + + + +/-- A member of a strictly increasing list is at or above its head. -/ +theorem head_le_of_sortedLT' {l : List Key} (h : l.SortedLT) (hne : l ≠ []) {a : Key} + (ha : a ∈ l) : l.head hne ≤ a := + (List.sortedLT_iff_pairwise.mp h).imp le_of_lt |>.rel_head ha + +section ResolveNil + +variable {db : DB} {r : RangeRead} {res : Result} + +/-- Under `cap < slack`, any database whose keys below `end_.key` are exactly the reported +keys has a clamping begin selector. -/ +theorem clampEndOne_resolve_begin_nil_of_cap_lt (hcell : negCrossClampEndOne r res) + (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = + res.keys) + (hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcap : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞)) : + resolve db r.begin = (([] : Key) : WithTop Key) := by + have hcB := clampEndOne_count_le_slack_of_cap_lt (db := db) hcell hfilter hres_lt_E hcap + have hbegin : r.begin.offset < 0 := hcell.2.1 + apply resolve_eq_nil + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have : (db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) ≤ + (beginSlack r : ℤ) := by exact_mod_cast hcB + omega + +/-- Under `end_.key ≤ begin.key`, the number of stored keys below `begin.key` is at most +`slack + 1` when the stored keys below `end_.key` are the reported keys. -/ +theorem clampEndOne_count_le_succ_slack_of_cap_eq (hcell : negCrossClampEndOne r res) + (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = + res.keys) + (hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞)) : + db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) ≤ + beginSlack r + 1 := by + have hs : 0 < clampEndOneSlack r res := clampEndOne_slack_pos hcell + have hcap0 : clampEndOneCap r ≠ 0 := by rw [hcap]; exact ne_of_gt (by exact_mod_cast hs) + have hEB : r.end_.key ≤ r.begin.key := by + by_contra hcon + have hlt : r.begin.key < r.end_.key := lt_of_not_ge hcon + exact hcap0 (by unfold clampEndOneCap; exact intervalCard_eq_zero_of_le (le_of_lt hlt)) + have hm := clampEndOne_reported_eq_length hcell hEB hres_lt_E + have hcount := clampEndOne_count_le_of_between_bound (db := db) hfilter hm hEB + obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell + have hbetween : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length ≤ + clampEndOneSlack r res := by + have hle : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length : ℕ∞) ≤ + clampEndOneCap r := by + unfold clampEndOneCap + exact between_length_le_intervalCard (db := db) hEB + rw [hcap] at hle + exact_mod_cast hle + have hsdef : clampEndOneSlack r res = beginSlack r + 1 - reportedBelowBegin r res := rfl + omega + +/-- Under `cap = slack` with the empty key as the reported head, any database whose keys +below `end_.key` are exactly the reported keys has a clamping begin selector. -/ +theorem clampEndOne_resolve_begin_nil_of_cap_eq_nil (hcell : negCrossClampEndOne r res) + (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = + res.keys) + (hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞)) + (hnil : res.keys.headD ([] : Key) = ([] : Key)) : + resolve db r.begin = (([] : Key) : WithTop Key) := by + have hcB := clampEndOne_count_le_succ_slack_of_cap_eq (db := db) hcell hfilter hres_lt_E hcap + have hbegin : r.begin.offset < 0 := hcell.2.1 + have hne : res.keys ≠ [] := hcell.2.2.2.1 + have hanchor_le : r.begin.anchor db ≤ 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have : (db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) ≤ + (beginSlack r : ℤ) + 1 := by exact_mod_cast hcB + omega + rcases lt_or_eq_of_le hanchor_le with hneg | heq + · exact resolve_eq_nil (db := db) (s := r.begin) hneg + · have hzero : r.begin.anchor db = 0 := heq + have hmem_nil : ([] : Key) ∈ db.keys := by + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_nil : res.keys.head hne = ([] : Key) := by + rw [← clampEndOne_headD_eq (hne := hne)] + exact hnil + have hmem : res.keys.head hne ∈ db.keys := by + have : res.keys.head hne ∈ db.keys.filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) := by + rw [hfilter]; exact hhead_mem + exact (List.mem_filter.mp this).1 + rwa [hhead_nil] at hmem + have hlen : 0 < db.keys.length := List.length_pos_of_mem hmem_nil + have hdbne : db.keys ≠ [] := by + intro h; rw [h] at hlen; simp at hlen + have hhead_nil : db.keys.head hdbne = ([] : Key) := + le_antisymm (head_le_of_sortedLT' db.sorted hdbne hmem_nil) (nil_le_key _) + have h0 : db.keys[0]? = some ([] : Key) := by + rw [List.getElem?_eq_getElem hlen, ← List.head_eq_getElem hdbne, hhead_nil] + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr (le_of_eq hzero.symm))] + rw [show (r.begin.anchor db).toNat = 0 by rw [hzero]; rfl, h0] + +end ResolveNil + + + + +/-- Filtering below `lo` first, then below a larger `E`, agrees with filtering below `lo` +directly. -/ +theorem filter_lt_of_le_filter_lt {db : DB} {lo E : WithTop Key} (hE : lo ≤ E) : + db.keys.filter (fun k : Key => decide ((k : WithTop Key) < lo)) = + (db.keys.filter (fun k : Key => decide ((k : WithTop Key) < E))).filter + (fun k : Key => decide ((k : WithTop Key) < lo)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : (k : WithTop Key) < lo + · have hkE : (k : WithTop Key) < E := lt_of_lt_of_le hk hE + simp [hk, hkE] + · simp [hk] + +/-- **Agreement below a bound covering both selector keys fixes the complete keys at +`end.offset = 1`.** -/ +theorem completeKeys_eq_of_agree_cover {r : RangeRead} {db₁ db₂ : DB} {E : WithTop Key} + (hrev : r.reverse = false) (hend : r.end_.offset = 1) (hbegin : r.begin.offset ≤ 0) + (hB : r.begin.key ≤ E) (hE : r.end_.key ≤ E) + (hagree : AgreeOn ⟨(([] : Key) : WithTop Key), E⟩ db₁ db₂) : + RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + have hfE : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < E)) = + db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < E)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + have hf_end : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [filter_lt_of_le_filter_lt (db := db₁) hE, + filter_lt_of_le_filter_lt (db := db₂) hE, hfE] + have hres := resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := E) (s := r.begin) + hbegin hB hfE + rw [completeKeys_eq_filter_of_end_one hrev hend, + completeKeys_eq_filter_of_end_one (db := db₂) hrev hend, hf_end, hres] + + + +/-- **The `U = end_.key` long branch is compatible from its filter condition.** If the +stored keys below `end_.key` are exactly the reported keys and the capacity condition +fails (the gap is too short, or it is exactly `s` with an empty-key head), the begin +selector clamps to the empty key, so the complete window is exactly those stored keys. +-/ +theorem clampEndOne_compat_of_filter {r : RangeRead} {res : Result} {db : DB} + (hcell : negCrossClampEndOne r res) + (hlong : r.end_.key < r.begin.key) + (hcond : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞) ∨ + (clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) ∧ + res.keys.headD ([] : Key) = ([] : Key))) + (hlim : RangeRead.withinLimit r res.keys) + (hD : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = res.keys) : + RangeRead.Compatible db r res := by + have hcell_keep : negCrossClampEndOne r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + have hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + have hk' : k ∈ db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [hD]; exact hk + exact of_decide_eq_true (List.mem_filter.mp hk').2 + have hnil : resolve db r.begin = (([] : Key) : WithTop Key) := by + rcases hcond with hcap | ⟨hcap, hheadnil⟩ + · exact clampEndOne_resolve_begin_nil_of_cap_lt hcell_keep hD hres_lt_E hcap + · exact clampEndOne_resolve_begin_nil_of_cap_eq_nil hcell_keep hD hres_lt_E hcap hheadnil + have hself : (db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter + (fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) = + db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [hnil] + exact List.filter_eq_self.mpr (fun k _ => by + rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _) + refine ⟨hlim, Or.inr ⟨hmore, ?_⟩⟩ + rw [completeKeys_eq_filter_of_end_one hrev hend, hself, hD] + + + +/-- **The `U = end_.key` long branch has its filter condition.** A compatible database +with a nonempty complete result stores exactly the reported keys below `end_.key`: a +positive begin anchor would force the begin resolution to consume exactly the slack, which +the capacity condition rules out (or, when the head is the empty key, contradicts that key +being minimal). +-/ +theorem clampEndOne_filter_eq_of_compat {r : RangeRead} {res : Result} {db : DB} + (hcell : negCrossClampEndOne r res) + (hlong : r.end_.key < r.begin.key) + (hcond : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞) ∨ + (clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) ∧ + res.keys.headD ([] : Key) = ([] : Key))) + (hcomp : RangeRead.Compatible db r res) : + db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = res.keys := by + have hcell_keep : negCrossClampEndOne r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + set D : List Key := db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + with hDdef + have hDtake : D = db.keys.take (cnt db r.end_.key) := by + rw [hDdef, filter_lt_eq_take_countP db.sorted] + rfl + have hDlen : D.length = cnt db r.end_.key := by + rw [hDdef, cnt, List.countP_eq_length_filter] + have hDf : res.keys = D.filter + (fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) := by + rw [hres_eq, completeKeys_eq_filter_of_end_one hrev hend] + by_cases hbot : resolve db r.begin = (([] : Key) : WithTop Key) + · have hself : D.filter + (fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) = D := by + rw [hbot] + exact List.filter_eq_self.mpr (fun k _ => by + rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _) + rw [hself] at hDf + exact hDf.symm + · -- A genuine resolved begin key. + have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro htop + have hnil : D.filter + (fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) = [] := by + rw [htop, List.filter_eq_nil_iff] + intro k _; simp + rw [hnil] at hDf + exact hne hDf + have hex : ∃ k ∈ db.keys, resolve db r.begin = ((k : Key) : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with h | h | h + · exact absurd h hbot + · exact absurd h hnot_top + · exact h + obtain ⟨k, hkdb, hkres⟩ := hex + have hanchor_nonneg : 0 ≤ r.begin.anchor db := by + by_contra hcon + exact hbot (resolve_eq_nil (lt_of_not_ge hcon)) + have hget : db.keys[(r.begin.anchor db).toNat]? = some k := by + have hite := resolve_eq_ite db r.begin + rw [hite, ite_eq_right (not_lt.mpr hanchor_nonneg)] at hkres + cases h : db.keys[(r.begin.anchor db).toNat]? with + | none => rw [h] at hkres; exact absurd hkres (by simp) + | some k' => + rw [h] at hkres + have hk' : k' = k := WithTop.coe_inj.mp hkres + simpa [hk'] using h + have hlt : (r.begin.anchor db).toNat < db.keys.length := + List.getElem?_eq_some_iff.mp hget |>.1 + have hk_eq : db.keys[(r.begin.anchor db).toNat] = k := + List.getElem?_eq_some_iff.mp hget |>.2 + -- `k` is the least reported key and lies strictly below `end_.key`. + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hk_lt_E : ((k : Key) : WithTop Key) < r.end_.key := by + obtain ⟨x, hxmem⟩ := List.exists_mem_of_ne_nil _ hne + have hx' : x ∈ D.filter + (fun x : Key => decide (resolve db r.begin ≤ ((x : Key) : WithTop Key))) := by + rw [← hDf]; exact hxmem + have hxlt : ((x : Key) : WithTop Key) < r.end_.key := + of_decide_eq_true (List.mem_filter.mp (List.mem_filter.mp hx').1).2 + have hkx : ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by + rw [← hkres] + exact of_decide_eq_true (List.mem_filter.mp hx').2 + exact lt_of_le_of_lt hkx hxlt + have hres_lt_E : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + have hx' : x ∈ D.filter + (fun x : Key => decide (resolve db r.begin ≤ ((x : Key) : WithTop Key))) := by + rw [← hDf]; exact hx + exact of_decide_eq_true (List.mem_filter.mp (List.mem_filter.mp hx').1).2 + have hk_res : k ∈ res.keys := by + rw [hDf] + refine List.mem_filter.mpr ⟨?_, ?_⟩ + · rw [hDdef] + exact List.mem_filter.mpr ⟨hkdb, by rw [decide_eq_true_eq]; exact hk_lt_E⟩ + · rw [decide_eq_true_eq]; rw [hkres] + have hk_le_all : ∀ y ∈ res.keys, k ≤ y := by + intro y hy + have hy' : y ∈ D.filter + (fun x : Key => decide (resolve db r.begin ≤ ((x : Key) : WithTop Key))) := by + rw [← hDf]; exact hy + have hky := of_decide_eq_true (List.mem_filter.mp hy').2 + rw [hkres] at hky + exact WithTop.coe_le_coe.mp hky + have hk_head : res.keys.head hne = k := + le_antisymm (head_le_of_sortedLT' hsort hne hk_res) + (hk_le_all _ (List.head_mem hne)) + -- The arithmetic of the genuine branch: the gap above `end_.key` has length `s`. + have hm_eq : reportedBelowBegin r res = res.keys.length := + clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E + set cE : ℕ := cnt db r.end_.key with hcE + set cB : ℕ := cnt db r.begin.key with hcB + set g : ℕ := (between db r.end_.key r.begin.key).length with hg + set a : ℕ := (r.begin.anchor db).toNat with ha + have hDlen' : D.length = cE := by + rw [hDdef, hcE]; unfold cnt; rw [List.countP_eq_length_filter] + have hDsorted : D.SortedLT := by + rw [hDdef] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hcB_eq : cB = cE + g := by + rw [hcB, hcE, hg, cnt_eq_add_between db (le_of_lt hlong)] + have hca : db.keys.countP (fun x : Key => decide (x < k)) = a := + countP_lt_eq_of_getElem db.sorted hlt hk_eq + have hcaW : db.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) = a := by + rw [countP_lt_coe k] + exact hca + have hDa : D.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) = a := by + have hfilt : D.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) = + db.keys.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) := by + rw [hDdef, List.filter_filter] + apply List.filter_congr + intro x _ + by_cases hxk : ((x : Key) : WithTop Key) < ((k : Key) : WithTop Key) + · have hxE : ((x : Key) : WithTop Key) < r.end_.key := lt_trans hxk hk_lt_E + simp [hxk, hxE] + · simp [hxk] + rw [List.countP_eq_length_filter, hfilt, ← List.countP_eq_length_filter] + exact hcaW + have ha_cast : (a : ℤ) = (cB : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [ha] + rw [Int.toNat_of_nonneg hanchor_nonneg] + rw [hcB] + exact anchor_eq_count_sub_slack db r (le_of_lt hbegin) + have ha_lt_cE : a < cE := by + have hlt' : ((k : Key) : WithTop Key) < r.end_.key := hk_lt_E + cases hE : r.end_.key with + | top => + rw [hE] at hlong + exact absurd hlong (not_lt.mpr le_top) + | coe E => + rw [hcE, hE]; unfold cnt; rw [countP_lt_coe] + have hkE : k < E := WithTop.coe_lt_coe.mp (by rw [hE] at hlt'; exact hlt') + exact lt_countP_of_getElem_lt db.sorted hlt (by rw [hk_eq]; exact hkE) + have hdropk : D.filter + (fun x : Key => decide (((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key))) = D.drop a := by + have h := filter_not_lt_eq_drop_countP hDsorted ((k : Key) : WithTop Key) + rw [hDa] at h + rw [← h] + apply List.filter_congr + intro x _ + rw [decide_eq_decide] + exact not_lt.symm + have hres_drop : res.keys = D.drop a := by + have hDf_k : res.keys = D.filter + (fun x : Key => decide (((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key))) := by + rw [hDf] + apply List.filter_congr + intro x _ + rw [hkres, decide_eq_decide] + rw [hDf_k, hdropk] + have hm_len : reportedBelowBegin r res = cE - a := by + rw [hm_eq, hres_drop, List.length_drop, hDlen'] + have hm_add_a : reportedBelowBegin r res + a = cE := by + rw [hm_len] + exact Nat.sub_add_cancel (le_of_lt ha_lt_cE) + have hgs : clampEndOneSlack r res = g := by + unfold clampEndOneSlack + have h1 : ((reportedBelowBegin r res + a : ℕ) : ℤ) = (cE : ℤ) := by + exact_mod_cast hm_add_a + have h2 : (cB : ℤ) = (cE : ℤ) + (g : ℤ) := by exact_mod_cast hcB_eq + omega + rcases hcond with hcap | ⟨hcap, hheadnil⟩ + · have hgle : (g : ℕ∞) ≤ clampEndOneCap r := by + rw [hg]; unfold clampEndOneCap + exact between_length_le_intervalCard (db := db) (le_of_lt hlong) + rw [hgs] at hcap + exact absurd hcap (not_lt.mpr hgle) + · have hheadk : res.keys.headD ([] : Key) = k := by + have h0 : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + exact h0.trans hk_head + have hknil : k = [] := hheadk.symm.trans hheadnil + exfalso + exact hbot (by rw [hkres, hknil]) + + + +/-- If the upper endpoint of the clamp end-one cell is `end_.key`, the capacity +condition fails: either the gap is too short, or it is exactly the slack with an +empty-key head. -/ +theorem clampEndOne_neg_cond {r : RangeRead} {res : Result} + (hU : clampEndOneUpper r res ≠ r.begin.key) : + clampEndOneCap r < (clampEndOneSlack r res : ℕ∞) ∨ + (clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) ∧ + res.keys.headD ([] : Key) = ([] : Key)) := by + by_cases hle : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r + · by_cases hstrict : (clampEndOneSlack r res : ℕ∞) < clampEndOneCap r + · exfalso + apply hU + unfold clampEndOneUpper + rw [if_pos ⟨hle, Or.inl hstrict⟩] + · by_cases hhead : res.keys.headD ([] : Key) ≠ ([] : Key) + · exfalso + apply hU + unfold clampEndOneUpper + rw [if_pos ⟨hle, Or.inr hhead⟩] + · exact Or.inr ⟨le_antisymm (not_lt.mp hstrict) hle, not_not.mp hhead⟩ + · exact Or.inl (not_le.mp hle) + +/-- The clamp end-one cell's closed form is sound. -/ +theorem sound_clampEndOne {r : RangeRead} {res : Result} + (hcell : negCrossClampEndOne r res) : + Sound r res (closedFormNegCrossClampEndOne r res) := by + have hcell_keep : negCrossClampEndOne r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + have step : ∀ db₁ db₂, AgreeOn (closedFormNegCrossClampEndOne r res) db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + by_cases hU : clampEndOneUpper r res = r.begin.key + · have hEB := clampEndOne_end_le_begin_of_upper_begin hcell_keep hU + have hagree' : AgreeOn ⟨(([] : Key) : WithTop Key), r.begin.key⟩ db₁ db₂ := by + simpa [closedFormNegCrossClampEndOne, hU] using hagree + have hck := completeKeys_eq_of_agree_cover (r := r) (E := r.begin.key) + hrev hend (le_of_lt hbegin) (le_refl _) hEB hagree' + exact (RangeRead.compatible_congr hck).mp hcomp₁ + · have hU' : clampEndOneUpper r res = r.end_.key := by + unfold clampEndOneUpper at hU ⊢ + by_cases hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧ + ((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨ + res.keys.headD ([] : Key) ≠ ([] : Key)) + · rw [if_pos hP] at hU + exact absurd rfl hU + · rw [if_neg hP] at hU ⊢ + have hagree' : AgreeOn ⟨(([] : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ := by + simpa [closedFormNegCrossClampEndOne, hU'] using hagree + by_cases hshort : r.begin.key ≤ r.end_.key + · have hck := completeKeys_eq_of_agree_cover (r := r) (E := r.end_.key) + hrev hend (le_of_lt hbegin) hshort (le_refl _) hagree' + exact (RangeRead.compatible_congr hck).mp hcomp₁ + · have hlong : r.end_.key < r.begin.key := lt_of_not_ge hshort + have hcond := clampEndOne_neg_cond (r := r) (res := res) hU + have hD₁ := clampEndOne_filter_eq_of_compat hcell_keep hlong hcond hcomp₁ + have hD₂ : db₂.keys.filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = res.keys := by + have h := hagree' + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h.symm.trans hD₁ + exact clampEndOne_compat_of_filter hcell_keep hlong hcond hlim hD₂ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + + + +/-- In the clamp end-one cell, the reported keys are all strictly below `end_.key`. -/ +theorem clampEndOne_reported_lt_end {r : RangeRead} {res : Result} {db : DB} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hend : r.end_.offset = 1) : + ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + intro k hk + rw [hres_eq, completeKeys_eq_filter_of_end_one hrev hend] at hk + exact of_decide_eq_true (List.mem_filter.mp (List.mem_filter.mp hk).1).2 + +/-- The database consisting of exactly the reported keys has a clamping begin +selector and is compatible. -/ +theorem clampEndOne_res_db_compat {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) : + ∃ db₀ : DB, db₀.keys = res.keys ∧ + RangeRead.Compatible db₀ r res ∧ r.begin.anchor db₀ < 0 := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hall := clampEndOne_reported_lt_end hcomp hrev hmore hend + have hs : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hanchor : r.begin.anchor ⟨res.keys, hs⟩ < 0 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin)] + have hcount : res.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) + = reportedBelowBegin r res := rfl + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + rw [hcount]; omega + refine ⟨⟨res.keys, hs⟩, rfl, ?_, hanchor⟩ + have hD : res.keys.filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = res.keys := + List.filter_eq_self.mpr (fun k hk => by rw [decide_eq_true_eq]; exact hall k hk) + have hnil : resolve ⟨res.keys, hs⟩ r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil hanchor + refine ⟨hcomp.1, Or.inr ⟨hmore, ?_⟩⟩ + show res.keys = RangeRead.completeKeys ⟨res.keys, hs⟩ r + rw [completeKeys_eq_filter_of_end_one hrev hend] + show res.keys = (res.keys.filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))).filter + (fun k : Key => decide (resolve ⟨res.keys, hs⟩ r.begin ≤ ((k : Key) : WithTop Key))) + rw [hD, hnil] + exact (List.filter_eq_self.mpr (fun k _ => by + rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _)).symm + +/-- Every key strictly below `end_.key` is necessary in the clamp end-one cell. -/ +theorem clampEndOne_necessary_below_end {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) + {k : Key} (hk : ((k : Key) : WithTop Key) < r.end_.key) : Necessary r res k := by + have hcell_keep : negCrossClampEndOne r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + set head : Key := res.keys.head hne with hhead + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + by_cases hle : head ≤ k + · by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hread hhead_mem hlast_mem hle hklast + · intro cr hs + exact necessary_last_to_end_key hread hrev hmore hne (le_of_eq hend) cr hs + (le_of_lt (lt_of_not_ge hklast)) hk + · have hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := by + rw [← hhead] + exact WithTop.coe_lt_coe.mpr (not_le.mp hle) + obtain ⟨db₀, hkeys, hcomp₀, hanchor⟩ := clampEndOne_res_db_compat hread hcell_keep + exact necessary_below_head_of_anchor_nonpos_negCross db₀ hcomp₀ hrev hne hanchor + (by rw [← hhead]; exact WithTop.coe_lt_coe.mpr (not_le.mp hle)) + + + +/-- A database whose keys are the reported keys followed by a sorted list `L` of +`s = clampEndOneSlack` gap keys is compatible, with begin anchor `0`. -/ +theorem clampEndOne_gap_db {r : RangeRead} {res : Result} {L : List Key} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) + (hlong : r.end_.key < r.begin.key) + (hLsorted : L.SortedLT) + (hLlen : L.length = clampEndOneSlack r res) + (hLlo : ∀ x ∈ L, r.end_.key ≤ ((x : Key) : WithTop Key)) + (hLhi : ∀ x ∈ L, ((x : Key) : WithTop Key) < r.begin.key) : + ∃ dbA : DB, dbA.keys = res.keys ++ L ∧ RangeRead.Compatible dbA r res ∧ + r.begin.anchor dbA = 0 := by + have hcell_keep : negCrossClampEndOne r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend + have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hcross : ∀ a ∈ res.keys, ∀ b ∈ L, a < b := by + intro a ha b hb + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hres_lt_E a ha) (hLlo b hb)) + have hAppSorted : (res.keys ++ L).SortedLT := + List.sortedLT_append.mpr ⟨hres_sorted, hLsorted, hcross⟩ + have hm : reportedBelowBegin r res = res.keys.length := + clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E + have hD : (res.keys ++ L).filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys := by + rw [List.filter_append] + have h1 : res.keys.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys := + List.filter_eq_self.mpr (fun x hx => by rw [decide_eq_true_eq]; exact hres_lt_E x hx) + have h2 : L.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hLlo x hx) + rw [h1, h2, List.append_nil] + have hcount : (res.keys ++ L).countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.length + L.length := by + rw [List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong) + have h2 : L.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = L.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hLhi x hx + rw [h1, h2] + have hanchor : r.begin.anchor ⟨res.keys ++ L, hAppSorted⟩ = 0 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount, hLlen] + have hsdef : clampEndOneSlack r res = beginSlack r + 1 - reportedBelowBegin r res := rfl + rw [hsdef, hm] + have hlen_le : res.keys.length ≤ beginSlack r + 1 := by + rw [← hm]; exact le_trans hclamp (Nat.le_succ _) + have hsimp : res.keys.length + (beginSlack r + 1 - res.keys.length) = beginSlack r + 1 := by + omega + rw [hsimp] + push_cast + ring + have hresolve : resolve ⟨res.keys ++ L, hAppSorted⟩ r.begin = + ((res.keys.head hne : Key) : WithTop Key) := by + rw [resolve_eq_ite, ite_eq_right (by rw [hanchor]; exact not_lt.mpr (le_refl 0))] + rw [show (r.begin.anchor ⟨res.keys ++ L, hAppSorted⟩).toNat = 0 by rw [hanchor]; rfl] + have hget : (res.keys ++ L)[0]? = some (res.keys.head hne) := by + rw [← List.head?_eq_getElem?, List.head?_append, List.head?_eq_some_head hne] + rfl + rw [hget] + refine ⟨⟨res.keys ++ L, hAppSorted⟩, rfl, ?_, hanchor⟩ + refine ⟨hcomp.1, Or.inr ⟨hmore, ?_⟩⟩ + show res.keys = RangeRead.completeKeys ⟨res.keys ++ L, hAppSorted⟩ r + rw [completeKeys_eq_filter_of_end_one hrev hend] + show res.keys = ((res.keys ++ L).filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key))).filter + (fun x : Key => decide (resolve ⟨res.keys ++ L, hAppSorted⟩ r.begin ≤ + ((x : Key) : WithTop Key))) + rw [hD, hresolve] + exact (List.filter_eq_self.mpr (fun x hx => by + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hres_sorted hne hx))).symm + + + +/-- A database whose keys are the reported keys followed by `s+1` gap keys has +begin anchor `1` and is incompatible: the least reported key falls out of the +complete window. -/ +theorem clampEndOne_succ_gap_not_compat {r : RangeRead} {res : Result} {Lb : List Key} {dbB : DB} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) + (hlong : r.end_.key < r.begin.key) + (hkeys : dbB.keys = res.keys ++ Lb) + (hLblo : ∀ x ∈ Lb, r.end_.key ≤ ((x : Key) : WithTop Key)) + (hLbhi : ∀ x ∈ Lb, ((x : Key) : WithTop Key) < r.begin.key) + (hLblen : Lb.length = clampEndOneSlack r res + 1) : + ¬ RangeRead.Compatible dbB r res := by + have hcell_keep : negCrossClampEndOne r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend + have hm : reportedBelowBegin r res = res.keys.length := + clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E + have hD : dbB.keys.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys := by + rw [hkeys, List.filter_append] + have h1 : res.keys.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys := + List.filter_eq_self.mpr (fun x hx => by rw [decide_eq_true_eq]; exact hres_lt_E x hx) + have h2 : Lb.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hLblo x hx) + rw [h1, h2, List.append_nil] + have hcount : dbB.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.length + Lb.length := by + rw [hkeys, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong) + have h2 : Lb.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = Lb.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hLbhi x hx + rw [h1, h2] + have hanchor : r.begin.anchor dbB = 1 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount] + unfold clampEndOneSlack at hLblen + rw [hLblen, hm] + have hsimp : res.keys.length + (beginSlack r + 1 - res.keys.length + 1) = + beginSlack r + 2 := by omega + rw [hsimp] + push_cast + ring + have hlenpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have h1lt : (1 : Nat) < dbB.keys.length := by + rw [hkeys, List.length_append, hLblen] + omega + have h0 : dbB.keys[0] = res.keys.head hne := by + have h0? : dbB.keys[0]? = some (res.keys.head hne) := by + rw [hkeys, List.getElem?_append_left hlenpos, ← List.head?_eq_getElem?, + List.head?_eq_some_head hne] + have hlen0 : 0 < dbB.keys.length := by + rw [hkeys, List.length_append]; omega + rw [List.getElem?_eq_getElem hlen0] at h0? + exact Option.some.inj h0? + have h1gt : ((dbB.keys[0] : Key) : WithTop Key) < ((dbB.keys[1] : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr + ((dbB.sorted.getElem_lt_getElem_iff).mpr (by omega : (0 : Nat) < 1)) + have hresolve : resolve dbB r.begin = ((dbB.keys[1] : Key) : WithTop Key) := by + have hanchor_toNat : (r.begin.anchor dbB).toNat = 1 := by rw [hanchor]; rfl + rw [resolve_eq_ite, ite_eq_right (by rw [hanchor]; norm_num), + hanchor_toNat, List.getElem?_eq_getElem h1lt] + intro hcomp + have hres_eq : res.keys = RangeRead.completeKeys dbB r := by + rcases hcomp.2 with ⟨hmt, _, _⟩ | ⟨-, heq⟩ + · rw [hmore] at hmt; exact absurd hmt (by simp) + · exact heq + have hheadmem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hnot : res.keys.head hne ∉ RangeRead.completeKeys dbB r := by + rw [completeKeys_eq_filter_of_end_one hrev hend, hD, hresolve] + intro hmem + rw [List.mem_filter] at hmem + rw [decide_eq_true_eq] at hmem + have hlt : ((res.keys.head hne : Key) : WithTop Key) < ((dbB.keys[1] : Key) : WithTop Key) := by + rw [← h0]; exact h1gt + exact absurd hmem.2 (not_le.mpr hlt) + exact hnot (by rw [← hres_eq]; exact hheadmem) + + + +/-- A database with an extra bottom key below the reported head, followed by the +reported keys and `s` gap keys, is compatible with begin anchor `1`. -/ +theorem clampEndOne_gap_db_extra {r : RangeRead} {res : Result} {L : List Key} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) + (hlong : r.end_.key < r.begin.key) + (hheadne : res.keys.headD ([] : Key) ≠ ([] : Key)) + (hLsorted : L.SortedLT) + (hLlen : L.length = clampEndOneSlack r res) + (hLlo : ∀ x ∈ L, r.end_.key ≤ ((x : Key) : WithTop Key)) + (hLhi : ∀ x ∈ L, ((x : Key) : WithTop Key) < r.begin.key) : + ∃ dbA : DB, dbA.keys = (([] : Key) :: res.keys) ++ L ∧ + RangeRead.Compatible dbA r res ∧ r.begin.anchor dbA = 1 := by + have hcell_keep : negCrossClampEndOne r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend + have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hhead_nil : res.keys.head hne ≠ ([] : Key) := by + rw [← clampEndOne_headD_eq (hne := hne)]; exact hheadne + have hnil_lt_head : ([] : Key) < res.keys.head hne := by + cases h : res.keys.head hne with + | nil => exact absurd h hhead_nil + | cons a t => exact List.nil_lt_cons a t + have hnil_lt_res : ∀ x ∈ res.keys, ([] : Key) < x := by + intro x hx + exact lt_of_lt_of_le hnil_lt_head (head_le_of_sortedLT' hres_sorted hne hx) + have hnil_lt_E : (([] : Key) : WithTop Key) < r.end_.key := by + have hh := hres_lt_E _ (List.head_mem hne) + have : ((([] : Key)) : WithTop Key) ≤ ((res.keys.head hne : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt hnil_lt_head) + exact lt_of_le_of_lt this hh + have hPreSorted : (([] : Key) :: res.keys).SortedLT := by + rw [List.sortedLT_iff_pairwise] + exact List.pairwise_cons.mpr ⟨fun a ha => hnil_lt_res a ha, hres_sorted.pairwise⟩ + have hcross : ∀ a ∈ (([] : Key) :: res.keys), ∀ b ∈ L, a < b := by + intro a ha b hb + rcases List.mem_cons.mp ha with rfl | ha' + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hnil_lt_E (hLlo b hb)) + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hres_lt_E a ha') (hLlo b hb)) + have hAppSorted : ((([] : Key) :: res.keys) ++ L).SortedLT := + List.sortedLT_append.mpr ⟨hPreSorted, hLsorted, hcross⟩ + have hm : reportedBelowBegin r res = res.keys.length := + clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E + have hD : ((([] : Key) :: res.keys) ++ L).filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = + ([] : Key) :: res.keys := by + rw [List.filter_append] + have h1 : (([] : Key) :: res.keys).filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = + ([] : Key) :: res.keys := by + rw [List.filter_cons_of_pos (by rw [decide_eq_true_eq]; exact hnil_lt_E)] + rw [List.filter_eq_self.mpr (fun x hx => by + rw [decide_eq_true_eq]; exact hres_lt_E x hx)] + have h2 : L.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hLlo x hx) + rw [h1, h2, List.append_nil] + have hcount : ((([] : Key) :: res.keys) ++ L).countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + 1 + res.keys.length + L.length := by + rw [List.countP_append, List.countP_cons_of_pos] + · have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong) + have h2 : L.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = L.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hLhi x hx + rw [h1, h2] + omega + · rw [decide_eq_true_eq] + exact lt_of_lt_of_le hnil_lt_E (le_of_lt hlong) + have hanchor : r.begin.anchor ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ = 1 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount] + unfold clampEndOneSlack at hLlen + rw [hLlen, hm] + have hsimp : 1 + res.keys.length + (beginSlack r + 1 - res.keys.length) = + beginSlack r + 2 := by omega + rw [hsimp] + push_cast + ring + have hresolve : resolve ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ r.begin = + ((res.keys.head hne : Key) : WithTop Key) := by + have hanchor_toNat : + (r.begin.anchor ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩).toNat = 1 := by + rw [hanchor]; rfl + have hlen1 : (1 : Nat) < ((([] : Key) :: res.keys) ++ L).length := by + have hpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + rw [List.length_append, List.length_cons] + omega + have hget1 : ((([] : Key) :: res.keys) ++ L)[1] = res.keys.head hne := by + have h1? : ((([] : Key) :: res.keys) ++ L)[1]? = some (res.keys.head hne) := by + rw [List.getElem?_append_left (by simp [List.length_pos_of_ne_nil hne])] + rw [List.getElem?_cons_succ, List.getElem?_eq_getElem (List.length_pos_of_ne_nil hne)] + exact congrArg some (List.head_eq_getElem hne).symm + rw [List.getElem?_eq_getElem hlen1] at h1? + exact Option.some.inj h1? + rw [resolve_eq_ite, ite_eq_right (by rw [hanchor]; norm_num), hanchor_toNat, + List.getElem?_eq_getElem hlen1, hget1] + refine ⟨⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩, rfl, ?_, hanchor⟩ + refine ⟨hcomp.1, Or.inr ⟨hmore, ?_⟩⟩ + show res.keys = RangeRead.completeKeys ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ r + rw [completeKeys_eq_filter_of_end_one hrev hend] + show res.keys = (((([] : Key) :: res.keys) ++ L).filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key))).filter + (fun x : Key => decide (resolve ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ r.begin ≤ + ((x : Key) : WithTop Key))) + rw [hD, hresolve] + have hstep : ((([] : Key) :: res.keys).filter + (fun x : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key)))) = res.keys := by + rw [List.filter_cons_of_neg (by + rw [decide_eq_true_eq]; exact not_le.mpr (WithTop.coe_lt_coe.mpr hnil_lt_head))] + exact List.filter_eq_self.mpr (fun x hx => by + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hres_sorted hne hx)) + rw [hstep] + + + +/-- Inserting a key larger than every element of a list inserts it into the suffix +after that list, when appending another list. -/ +theorem orderedInsert_append_of_all_lt {l₁ l₂ : List Key} {k : Key} + (h : ∀ a ∈ l₁, a < k) : + (l₁ ++ l₂).orderedInsert (· ≤ ·) k = l₁ ++ l₂.orderedInsert (· ≤ ·) k := by + induction l₁ with + | nil => simp + | cons a t ih => + have ha : a < k := h a (by simp) + have ht : ∀ b ∈ t, b < k := fun b hb => h b (by simp [hb]) + rw [List.cons_append, List.orderedInsert_cons] + rw [ite_eq_right (not_le.mpr ha)] + rw [ih ht, List.cons_append] + +/-- Every gap key is necessary in the clamp end-one cell when the gap is strictly +larger than the slack. -/ +theorem clampEndOne_necessary_gap_of_cap_lt {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) + (hlt : (clampEndOneSlack r res : ℕ∞) < clampEndOneCap r) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + have hcell_keep : negCrossClampEndOne r res := hcell + have hread_keep : Readable r res := hread + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hlong : r.end_.key < r.begin.key := by + by_contra h + have hle : r.begin.key ≤ r.end_.key := le_of_not_gt h + have h0 : clampEndOneCap r = 0 := intervalCard_eq_zero_of_le hle + rw [h0] at hlt + exact not_lt.mpr (zero_le) hlt + have hcard : ((clampEndOneSlack r res + 1 : ℕ) : ℕ∞) ≤ clampEndOneCap r := by + rw [Nat.cast_add, Nat.cast_one] + exact (ENat.add_one_le_iff (by simp)).mpr hlt + obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) + (m := clampEndOneSlack r res) hcard + have hLlo : ∀ x ∈ dbJ.keys, r.end_.key ≤ ((x : Key) : WithTop Key) := + fun x hx => (hJrange x hx).1 + have hLhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => (hJrange x hx).2 + obtain ⟨dbA, hAkeys, hAcomp, hAanchor⟩ := + clampEndOne_gap_db hread_keep hcell_keep hlong dbJ.sorted hJlen hLlo hLhi + have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend + have hkres : k ∉ res.keys := by + intro hk + exact absurd hkE (not_le.mpr (hres_lt_E k hk)) + have hkA : k ∉ dbA.keys := by + rw [hAkeys] + intro hk + rcases List.mem_append.mp hk with hk' | hk' + · exact hkres hk' + · exact hkJ hk' + have hall_lt : ∀ a ∈ res.keys, a < k := by + intro a ha + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hres_lt_E a ha) hkE) + let Lb : List Key := dbJ.keys.orderedInsert (· ≤ ·) k + have hBkeys : (toggle dbA k).keys = res.keys ++ Lb := by + rw [toggle_eq_insert_of_not_mem hkA, DB.insert_keys, hAkeys, + orderedInsert_append_of_all_lt hall_lt] + have hLblo : ∀ x ∈ Lb, r.end_.key ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hkE + · exact hLlo x hx + have hLbhi : ∀ x ∈ Lb, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hkB + · exact hLhi x hx + have hLblen : Lb.length = clampEndOneSlack r res + 1 := by + simp [Lb, List.orderedInsert_length, hJlen] + have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res := + clampEndOne_succ_gap_not_compat hread_keep hcell_keep hlong hBkeys hLblo hLbhi hLblen + exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩ + + + +/-- Counting a key of a list that satisfies the predicate adds one after erasure. -/ +theorem countP_erase_add_one {l : List Key} {k : Key} {p : Key → Bool} + (hk : k ∈ l) (hp : p k = true) : + l.countP p = (l.erase k).countP p + 1 := by + have hpos : 0 < l.countP p := + List.countP_pos_iff.mpr ⟨k, hk, hp⟩ + have h := List.countP_erase p l k + rw [if_pos ⟨hk, hp⟩] at h + omega + +/-- A database with the extra bottom key and the full gap list, compatible with begin +anchor one, becomes incompatible after erasing a gap key: the resolution clamps back +to bottom and the extra bottom key enters the complete result. -/ +theorem clampEndOne_erase_extra_not_compat {r : RangeRead} {res : Result} {dbA : DB} + {k : Key} {L : List Key} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) + (hlong : r.end_.key < r.begin.key) + (hheadne : res.keys.headD ([] : Key) ≠ ([] : Key)) + (hAkeys : dbA.keys = (([] : Key) :: res.keys) ++ L) + (hLlo : ∀ x ∈ L, r.end_.key ≤ ((x : Key) : WithTop Key)) + (hLhi : ∀ x ∈ L, ((x : Key) : WithTop Key) < r.begin.key) + (hkL : k ∈ L) + (hAanchor : r.begin.anchor dbA = 1) : + ¬ RangeRead.Compatible (dbA.erase k) r res := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + obtain ⟨db0, hcomp0⟩ := hread + have hres_lt_E := clampEndOne_reported_lt_end hcomp0 hrev hmore hend + have hhead : res.keys.head hne ≠ ([] : Key) := by + rw [← clampEndOne_headD_eq (hne := hne)]; exact hheadne + have hbot_lt_head : ([] : Key) < res.keys.head hne := by + cases h : res.keys.head hne with + | nil => exact absurd h hhead + | cons a t => exact List.nil_lt_cons a t + have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + have hbot_lt_res : ∀ x ∈ res.keys, ([] : Key) < x := + fun x hx => lt_of_lt_of_le hbot_lt_head (head_le_of_sortedLT' hres_sorted hne hx) + have hbot_lt_E : (([] : Key) : WithTop Key) < r.end_.key := by + have hh := hres_lt_E _ (List.head_mem hne) + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head)) hh + have hk_ge_E : r.end_.key ≤ ((k : Key) : WithTop Key) := hLlo k hkL + have hk_lt_B := hLhi k hkL + have hkres : k ∉ res.keys := by + intro hk + exact absurd hk_ge_E (not_le.mpr (hres_lt_E k hk)) + have hkbot : k ≠ ([] : Key) := by + intro hbot + rw [hbot] at hk_ge_E + exact absurd hk_ge_E (not_le.mpr hbot_lt_E) + have hknotpre : k ∉ (([] : Key) :: res.keys) := by + intro hk + rcases List.mem_cons.mp hk with hk' | hk' + · exact hkbot hk' + · exact hkres hk' + have hkA : k ∈ dbA.keys := by rw [hAkeys]; exact List.mem_append_right _ hkL + have herase : (dbA.erase k).keys = (([] : Key) :: res.keys) ++ L.erase k := by + rw [DB.erase_keys, hAkeys, List.erase_append_right _ hknotpre] + have hfilterCons : ((([] : Key) :: res.keys)).filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = + ([] : Key) :: res.keys := by + rw [List.filter_cons_of_pos (by rw [decide_eq_true_eq]; exact hbot_lt_E)] + rw [List.filter_eq_self.mpr (fun x hx => by + rw [decide_eq_true_eq]; exact hres_lt_E x hx)] + have hfilterL : L.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hLlo x hx) + have hfilterEraseL : (L.erase k).filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hLlo x (List.erase_sublist.subset hx)) + have hfilterErase : (dbA.erase k).keys.filter + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = + ([] : Key) :: res.keys := by + rw [herase, List.filter_append, hfilterCons, hfilterEraseL, List.append_nil] + have hp : (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) k = true := by + rw [decide_eq_true_eq]; exact hk_lt_B + have hcountrel : dbA.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + (dbA.erase k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) + 1 := + countP_erase_add_one hkA hp + have hcountA : ((dbA.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key))) : ℤ) = + (beginSlack r : ℤ) + 2 := by + have h := anchor_eq_count_sub_slack dbA r (le_of_lt hbegin) + rw [hAanchor] at h + omega + have hanchorErase : r.begin.anchor (dbA.erase k) = 0 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin)] + have h2 : (((dbA.erase k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key))) : ℤ) = + (beginSlack r : ℤ) + 1 := by + have h3 : (((dbA.erase k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key))) : ℤ) + 1 = + (beginSlack r : ℤ) + 2 := by + rw [← hcountA] + exact_mod_cast hcountrel.symm + omega + omega + have hresolve : resolve (dbA.erase k) r.begin = (([] : Key) : WithTop Key) := by + have hlen : 0 < (dbA.erase k).keys.length := by + rw [herase]; exact List.length_pos_of_ne_nil (by simp) + have h0? : (dbA.erase k).keys[0]? = some (([] : Key) : Key) := by + rw [DB.erase_keys, hAkeys, List.erase_append_right _ hknotpre] + simp + rw [resolve_eq_ite, ite_eq_right (by rw [hanchorErase]; norm_num), + show (r.begin.anchor (dbA.erase k)).toNat = 0 by rw [hanchorErase]; rfl, h0?] + have hcomplete : RangeRead.completeKeys (dbA.erase k) r = ([] : Key) :: res.keys := by + rw [completeKeys_eq_filter_of_end_one hrev hend, hfilterErase, hresolve] + exact List.filter_eq_self.mpr (fun x _ => by + rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _) + have hne_cons : ([] : Key) :: res.keys ≠ res.keys := by + intro h + have hlen := congrArg List.length h + simp at hlen + intro hcomp + have hres_eq : res.keys = RangeRead.completeKeys (dbA.erase k) r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · rw [hmore] at hm + exact absurd hm (by simp) + · exact heq + rw [hcomplete] at hres_eq + exact hne_cons hres_eq.symm + + + +/-- Every gap key is necessary in the clamp end-one cell when the gap exactly matches +the slack and the reported head is a genuine key. -/ +theorem clampEndOne_necessary_gap_of_cap_eq {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) + (hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞)) + (hheadne : res.keys.headD ([] : Key) ≠ ([] : Key)) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + have hcell_keep : negCrossClampEndOne r res := hcell + have hread_keep : Readable r res := hread + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + have hs_pos := clampEndOne_slack_pos hcell_keep + have hcappos : (0 : ℕ∞) < clampEndOneCap r := by + rw [hcap]; exact_mod_cast hs_pos + have hlong : r.end_.key < r.begin.key := by + by_contra h + have hle : r.begin.key ≤ r.end_.key := le_of_not_gt h + have h0 : clampEndOneCap r = 0 := intervalCard_eq_zero_of_le hle + rw [h0] at hcappos + exact not_lt.mpr (zero_le) hcappos + have hcard : ((clampEndOneSlack r res - 1 + 1 : ℕ) : ℕ∞) ≤ clampEndOneCap r := by + rw [Nat.sub_add_cancel (by omega : 1 ≤ clampEndOneSlack r res), hcap] + obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) + (m := clampEndOneSlack r res - 1) hcard + let dbL : DB := dbJ.insert k hkJ + have hLkeys : dbL.keys = dbJ.keys.orderedInsert (· ≤ ·) k := rfl + have hLsorted : dbL.keys.SortedLT := dbL.sorted + have hLlen : dbL.keys.length = clampEndOneSlack r res := by + rw [hLkeys, List.orderedInsert_length, hJlen] + omega + have hLlo : ∀ x ∈ dbL.keys, r.end_.key ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [hLkeys, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hkE + · exact (hJrange x hx).1 + have hLhi : ∀ x ∈ dbL.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + rw [hLkeys, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hkB + · exact (hJrange x hx).2 + have hkL : k ∈ dbL.keys := by + rw [hLkeys, List.mem_orderedInsert] + exact Or.inl rfl + obtain ⟨dbA, hAkeys, hAcomp, hAanchor⟩ := + clampEndOne_gap_db_extra hread_keep hcell_keep hlong hheadne hLsorted hLlen hLlo hLhi + have hkA : k ∈ dbA.keys := by + rw [hAkeys]; exact List.mem_append_right _ hkL + have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res := by + rw [toggle_eq_erase_of_mem hkA] + exact clampEndOne_erase_extra_not_compat hread_keep hcell_keep hlong hheadne + hAkeys hLlo hLhi hkL hAanchor + exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩ + + + +/-- The closed form for the crossed negative-begin complete clamp end-one cell is +minimal. -/ +theorem minimal_closedForm_of_negCross_clampEndOne {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampEndOne r res) : + Minimal r res (closedFormNegCrossClampEndOne r res) := by + rw [minimal_iff_sound_and_necessaryRange] + refine ⟨sound_clampEndOne hcell, ?_⟩ + intro k hlo hhi + by_cases hU : clampEndOneUpper r res = r.begin.key + · have hkltB : (k : WithTop Key) < r.begin.key := by + simpa [closedFormNegCrossClampEndOne, hU] using hhi + by_cases hkE : (k : WithTop Key) < r.end_.key + · exact clampEndOne_necessary_below_end hread hcell hkE + · have hkE' : r.end_.key ≤ (k : WithTop Key) := le_of_not_gt hkE + have hEB : r.end_.key < r.begin.key := lt_of_le_of_lt hkE' hkltB + have hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧ + ((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨ + res.keys.headD ([] : Key) ≠ ([] : Key)) := by + unfold clampEndOneUpper at hU + by_cases hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧ + ((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨ + res.keys.headD ([] : Key) ≠ ([] : Key)) + · exact hP + · rw [if_neg hP] at hU + exact absurd hU (ne_of_lt hEB) + by_cases hstrict : (clampEndOneSlack r res : ℕ∞) < clampEndOneCap r + · exact clampEndOne_necessary_gap_of_cap_lt hread hcell hstrict hkE' hkltB + · have hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) := + le_antisymm (not_lt.mp hstrict) hP.1 + have hheadne : res.keys.headD ([] : Key) ≠ ([] : Key) := by + rcases hP.2 with hstrict' | hhead + · exact absurd hstrict' hstrict + · exact hhead + exact clampEndOne_necessary_gap_of_cap_eq hread hcell hcap hheadne hkE' hkltB + · have hU' : clampEndOneUpper r res = r.end_.key := by + unfold clampEndOneUpper at hU ⊢ + by_cases hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧ + ((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨ + res.keys.headD ([] : Key) ≠ ([] : Key)) + · rw [if_pos hP] at hU + exact absurd rfl hU + · rw [if_neg hP] at hU ⊢ + have hkE : (k : WithTop Key) < r.end_.key := by + simpa [closedFormNegCrossClampEndOne, hU'] using hhi + exact clampEndOne_necessary_below_end hread hcell hkE + + +/-- The reversed crossed negative-begin complete clamp cell at `end.offset = 1`. -/ +abbrev negCrossClampEndOneRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset = 1 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The reversed closed form for the crossed negative-begin complete clamp cell at +`end.offset = 1`. -/ +noncomputable def closedFormNegCrossClampEndOneRev (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossClampEndOne { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- **The reversed crossed negative-begin complete clamp cell at `end.offset = 1` is +minimal**, by flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_clampEndOne_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossClampEndOneRev r res) : + Minimal r res (closedFormNegCrossClampEndOneRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell + 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 hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hcell' : negCrossClampEndOne { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_⟩ + · rw [hheadD]; exact hcrossD + · rw [hcount]; exact hclamp + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_clampEndOne hread' hcell' + exact (minimal_flip (b := true) hrev hmore).mp hmin + + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossCompleteHigh.lean b/FormalConflictRange/NegCrossCompleteHigh.lean new file mode 100644 index 0000000..d822431 --- /dev/null +++ b/FormalConflictRange/NegCrossCompleteHigh.lean @@ -0,0 +1,2497 @@ +/- +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.NegCrossCompleteResidual + +/-! +# The crossed negative-begin complete high cell + +This module closes the remaining crossed negative-begin complete cells with +`2 ≤ end.offset` and `end.key < head`: the end selector key lies strictly below every +reported key. + +**The four-branch `max` rule is false.** An earlier version of this module advertised +the minimal range as `[⊥, max begin.key (keyAfter last))` in the clamping finite cell, +`[⊥, ⊤)` in the clamping low cell, and `[end.key, …)` in the nonclamping cells. The +clamping finite branch is genuinely capacity-dependent, not `max`-shaped. With `L` the +last reported key, `q = beginSlack` and `r = reportedBelowBegin`, put `C = q + 1 - r` +(so `C ≥ 1` in the clamping cell). The upper endpoint is `begin.key` when the interval +`[keyAfter L, begin.key)` holds `C` keys (or `C + 1` keys when `end.key = ⊥`), and +`keyAfter L` otherwise. The witness `res = [[5], [6]]`, `end.key = [1]`, +`begin.key = [6,0,0]`, `begin.offset = -3`, `end.offset = 3` is kernel-checked in +`Counterexample.lean` (`nch_M_not_minimal`): every compatible database then has +`begin.key = [6,0,0]` strictly above `keyAfter last = [6,0]`, but the extra key `[6,0]` +lies in no sound range, so the `max` rule over-collects it. + +The present module proves the clamp / low-offset branch `[⊥, ⊤)`. The witness database +is the reported list itself: clamping makes the begin selector resolve to `⊥`, and +`t < e` makes the end selector resolve past the end of that list. Flipping any key +outside the reported list therefore either becomes the new minimum (below the head) or +joins the reported window (above the last), so it is necessary. The whole range is thus +minimal, and soundness is the trivial soundness of the full range. +-/ + +namespace FormalConflictRange + +/-- The upper endpoint `M = max begin.key (keyAfter last)` of the crossed +negative-begin complete high cell. -/ +noncomputable def negCrossCompleteHighM (r : RangeRead) (res : Result) : WithTop Key := + max r.begin.key + ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) + +/-- The crossed negative-begin complete high cell. -/ +abbrev negCrossCompleteHigh (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) + +/-- The excess `D = (end.offset - 1).toNat - #reported` of the end anchor over the +reported length in the low branch of the crossed negative-begin complete high cell. -/ +def negCrossHighD (r : RangeRead) (res : Result) : ℕ := + (r.end_.offset - 1).toNat - res.keys.length + +/-- The number of keys available in the half-open gap `[end.key, head)` of the crossed +negative-begin complete high cell. -/ +noncomputable def negCrossHighGapCap (r : RangeRead) (res : Result) : ℕ∞ := + intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key) + +/-- In the nonclamping low branch the minimal range starts at `end.key` when the gap +`[end.key, head)` can hold the excess `D` and the begin key admits a key above it (or +when the gap strictly exceeds `D`). Otherwise it starts at the reported head. -/ +noncomputable def negCrossHighUsesEnd (r : RangeRead) (res : Result) : Prop := + ((negCrossHighD r res : ℕ∞) < negCrossHighGapCap r res) ∨ + ((negCrossHighD r res : ℕ∞) ≤ negCrossHighGapCap r res ∧ + r.begin.key < (⊤ : WithTop Key)) + +/-- The slack beyond the reported keys below `begin.key` in the crossed negative-begin +complete high cell. -/ +def clampHighC (r : RangeRead) (res : Result) : ℕ := + beginSlack r + 1 - reportedBelowBegin r res + +/-- The capacity of the key gap `[keyAfter last reported, begin.key)`. -/ +noncomputable def clampHighCap (r : RangeRead) (res : Result) : ℕ∞ := + intervalCard ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key + +/-- The required capacity in the clamping finite branch: `clampHighC`, plus one when +`end.key` is the empty key (there is then no key below it to anchor the begin +resolution, so one extra gap slot is needed). -/ +def clampHighThresh (r : RangeRead) (res : Result) : ℕ := + clampHighC r res + if r.end_.key = (([] : Key) : WithTop Key) then 1 else 0 + +/-- The upper endpoint of the clamping finite branch of the crossed negative-begin +complete high cell. -/ +noncomputable def clampHighU (r : RangeRead) (res : Result) : WithTop Key := + if (clampHighThresh r res : ℕ∞) ≤ clampHighCap r res then r.begin.key + else ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) + +/-- The closed form of the crossed negative-begin complete high cell. -/ +noncomputable def closedFormNegCrossCompleteHigh (r : RangeRead) (res : Result) : + ConflictRange := by + classical + exact + if reportedBelowBegin r res ≤ beginSlack r then + if res.keys.length = (r.end_.offset - 1).toNat then + ⟨(([] : Key) : WithTop Key), clampHighU r res⟩ + else ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + else + if res.keys.length = (r.end_.offset - 1).toNat then + ⟨r.end_.key, negCrossCompleteHighM r res⟩ + else if negCrossHighUsesEnd r res then ⟨r.end_.key, (⊤ : WithTop Key)⟩ + else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + +/-- The clamping finite branch of the crossed negative-begin complete high cell. -/ +abbrev negCrossCompleteHighClampFinite (r : RangeRead) (res : Result) : Prop := + negCrossCompleteHigh r res ∧ reportedBelowBegin r res ≤ beginSlack r ∧ + res.keys.length = (r.end_.offset - 1).toNat + +/-- The closed form evaluates to `[⊥, clampHighU r res)` in the clamping finite branch. -/ +theorem closedForm_clampFinite_eq {r : RangeRead} {res : Result} + (hcell : negCrossCompleteHighClampFinite r res) : + closedFormNegCrossCompleteHigh r res = + (⟨(([] : Key) : WithTop Key), clampHighU r res⟩ : ConflictRange) := by + obtain ⟨-, hclamp, hfinite⟩ := hcell + unfold closedFormNegCrossCompleteHigh + rw [ite_eq_left hclamp, ite_eq_left hfinite] + +section Structural + +variable {r : RangeRead} {res : Result} + +/-- Every reported key lies strictly above `end.key` in the high cell. -/ +theorem high_reported_gt_end (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + ∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) := by + intro k hk + have hhead_le : res.keys.head hne ≤ k := head_le_of_sortedLT' hsort hne hk + have hcoe : ((res.keys.headD ([] : Key) : Key) : WithTop Key) = + ((res.keys.head hne : Key) : WithTop Key) := by + rw [headD_eq_head_of_ne hne] + rw [hcoe] at hEH + exact lt_of_lt_of_le hEH (WithTop.coe_le_coe.mpr hhead_le) + +end Structural + +/-- In the clamping, low-offset branch the reported list itself is a compatible +database: the begin selector clamps to `⊥` and the end selector overshoots the finite +reported list. -/ +theorem high_witness_compatible_clamp_low {r : RangeRead} {res : Result} + {hsort : res.keys.SortedLT} + (hwl : RangeRead.withinLimit r res.keys) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) : + RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by + set db : DB := ⟨res.keys, hsort⟩ with hdb + have hdbkeys : db.keys = res.keys := rfl + have hgt : ∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) := + high_reported_gt_end hsort hne hEH + have hcE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by + rw [hdbkeys, List.countP_eq_zero] + intro k hk + rw [decide_eq_true_eq] + exact not_lt.mpr (le_of_lt (hgt k hk)) + have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have hanchorB : r.begin.anchor db < 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcB] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hlo : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorB + have hepos : 1 ≤ r.end_.offset := by + -- `end.offset ≥ 1` because the end offset is not nonpositive in this cell + by_contra h + have hle : r.end_.offset ≤ 0 := by omega + have hz : (r.end_.offset - 1).toNat = 0 := by omega + rw [hz] at hlow + exact Nat.not_lt_zero _ hlow + have hanchorE_toNat : (r.end_.anchor db).toNat = (r.end_.offset - 1).toNat := by + rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add] + have hEi0 : 0 ≤ r.end_.anchor db := by + by_contra hneg + have hlt : r.end_.anchor db < 0 := lt_of_not_ge hneg + have hz : (r.end_.anchor db).toNat = 0 := Int.toNat_of_nonpos (le_of_lt hlt) + rw [hanchorE_toNat] at hz + rw [hz] at hlow + exact Nat.not_lt_zero _ hlow + have hlen : db.keys.length ≤ (r.end_.anchor db).toNat := by + rw [hanchorE_toNat] + exact le_of_lt hlow + have hhi : resolve db r.end_ = (⊤ : WithTop Key) := resolve_eq_top hEi0 hlen + refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩ + rw [RangeRead.completeKeys, hrev] + simp only [Bool.false_eq_true, if_false] + rw [hlo, hhi] + exact (RangeRead.inRange_unbounded res.keys).symm + +/-- Count of keys strictly below a selector key after inserting a fresh key. -/ +theorem countP_orderedInsert_begin {l : List Key} {B : WithTop Key} (hs : l.SortedLT) + {k : Key} (hk : k ∉ l) : + (l.orderedInsert (· ≤ ·) k).countP + (fun a : Key => decide (((a : Key) : WithTop Key) < B)) = + l.countP (fun a : Key => decide (((a : Key) : WithTop Key) < B)) + + (if ((k : Key) : WithTop Key) < B then 1 else 0) := by + rw [orderedInsert_eq_insertIdx hs hk] + by_cases hkB : ((k : Key) : WithTop Key) < B + · rw [if_pos hkB] + exact countP_insertIdx_of_lt hs hkB + · rw [if_neg hkB] + exact countP_insertIdx (fun a : Key => decide (((a : Key) : WithTop Key) < B)) + (l.countP (fun a => decide (a < k))) k (by simp [hkB]) + +/-- Any key outside the reported list is necessary in the clamping, low-offset +branch: inserting it into the reported list either becomes the new minimum or joins the +window, so the result changes. -/ +theorem necessary_of_high_not_mem_clamp_low {r : RangeRead} {res : Result} + {hsort : res.keys.SortedLT} + (hwl : RangeRead.withinLimit r res.keys) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + {k : Key} (hk : k ∉ res.keys) : + Necessary r res k := by + have hcomp₀ := high_witness_compatible_clamp_low (hsort := hsort) hwl hrev hmore hne + hbegin hEH hclamp hlow + set db : DB := ⟨res.keys, hsort⟩ with hdb + have hkdb : k ∉ db.keys := by simpa [hdb] using hk + let db₁ : DB := db.insert k hkdb + have hdb₁keys : db₁.keys = res.keys.orderedInsert (· ≤ ·) k := rfl + refine necessary_of_toggle ⟨db, hcomp₀, ?_⟩ + rw [toggle_eq_insert_of_not_mem hkdb] + intro hcomp₁ + have hck : RangeRead.completeKeys db₁ r = res.keys := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq.symm + have hk₁ : k ∈ db₁.keys := DB.mem_insert_self db k hkdb + have hne₁ : db₁.keys ≠ [] := by + intro hnil + have hlen0 : db₁.keys.length = 0 := by rw [hnil]; rfl + rw [hdb₁keys, List.orderedInsert_length] at hlen0 + omega + -- The end resolution overshoots the finite inserted list. + have hepos : 1 ≤ r.end_.offset := by + have hle : 0 ≤ (r.end_.offset - 1).toNat := Nat.zero_le _ + omega + have hlen₁ : db₁.keys.length = res.keys.length + 1 := by + rw [hdb₁keys, List.orderedInsert_length] + have htoNat₁ : (r.end_.anchor db₁).toNat = + db₁.keys.countP (fun a : Key => decide (((a : Key) : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := anchor_toNat_eq (s := r.end_) hepos + have hElen : db₁.keys.length ≤ (r.end_.anchor db₁).toNat := by + rw [htoNat₁, hlen₁] + omega + have hEi0₁ : 0 ≤ r.end_.anchor db₁ := by + unfold Selector.anchor + have hc : (0 : ℤ) ≤ (db₁.keys.countP + (fun a : Key => decide (((a : Key) : WithTop Key) < r.end_.key)) : ℤ) := + Int.natCast_nonneg _ + have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + linarith + have hhi₁ : resolve db₁ r.end_ = (⊤ : WithTop Key) := resolve_eq_top hEi0₁ hElen + -- The begin resolution lands at or below the inserted key. + have hcB₁ : db₁.keys.countP + (fun a : Key => decide (((a : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res + + (if ((k : Key) : WithTop Key) < r.begin.key then 1 else 0) := by + rw [hdb₁keys] + simpa [reportedBelowBegin] using + (countP_orderedInsert_begin (B := r.begin.key) hsort (by simpa [hdb] using hk)) + have hanchorB₁ : r.begin.anchor db₁ ≤ 0 := by + rw [anchor_eq_count_sub_slack db₁ r (le_of_lt hbegin), hcB₁] + have hcl : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by + exact_mod_cast hclamp + split_ifs with hkB <;> push_cast <;> omega + have hlo₁ : resolve db₁ r.begin ≤ ((k : Key) : WithTop Key) := by + rcases lt_or_eq_of_le hanchorB₁ with hlt | heq + · rw [resolve_eq_nil hlt] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + · have h0 : 0 ≤ r.begin.anchor db₁ := le_of_eq heq.symm + have hpos : 0 < db₁.keys.length := by rw [hlen₁]; omega + have hltlen : (r.begin.anchor db₁).toNat < db₁.keys.length := by + rw [heq]; simpa using hpos + have hhead_le : db₁.keys.head hne₁ ≤ k := + head_le_of_sortedLT' db₁.sorted hne₁ hk₁ + have hzero : (r.begin.anchor db₁).toNat = 0 := by rw [heq]; rfl + have hresolve : resolve db₁ r.begin = + ((db₁.keys[(r.begin.anchor db₁).toNat] : Key) : WithTop Key) := + resolve_eq_getElem h0 hltlen + rw [hresolve] + have hget : db₁.keys[(r.begin.anchor db₁).toNat] = db₁.keys.head hne₁ := by + simp only [hzero] + exact List.getElem_zero _ + rw [hget] + exact WithTop.coe_le_coe.mpr hhead_le + have hkck : k ∈ RangeRead.completeKeys db₁ r := by + rw [RangeRead.mem_completeKeys] + exact ⟨hk₁, hlo₁, by rw [hhi₁]; exact WithTop.coe_lt_top k⟩ + rw [hck] at hkck + exact hk hkck + +/-- **The clamping, low-offset branch of the crossed negative-begin complete high cell +is the full range.** -/ +theorem minimal_fullRange_of_negCrossHigh_clamp_low {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hbegin : r.begin.offset < 0) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) : + Minimal r res (⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ : ConflictRange) := by + obtain ⟨dbw, hcompw⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcompw hrev + refine (minimal_iff_sound_and_necessary).mpr ⟨sound_fullRange r res, ?_⟩ + intro k _ + by_cases hk : k ∈ res.keys + · exact necessary_of_mem_result ⟨dbw, hcompw⟩ hk + · exact necessary_of_high_not_mem_clamp_low (hsort := hsort) hcompw.1 hrev hmore hne + hbegin hEH hclamp hlow hk + +/-- The clamping, low-offset branch of the crossed negative-begin complete high cell. -/ +abbrev negCrossCompleteHighClampLow (r : RangeRead) (res : Result) : Prop := + negCrossCompleteHigh r res ∧ reportedBelowBegin r res ≤ beginSlack r ∧ + res.keys.length < (r.end_.offset - 1).toNat + +/-- **The clamping, low-offset branch of the crossed negative-begin complete high cell +is minimal** for the total closed form. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh_clampLow {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampLow r res) : + Minimal r res (closedFormNegCrossCompleteHigh r res) := by + obtain ⟨⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩, hclamp, hlow⟩ := hcell + have hmin := minimal_fullRange_of_negCrossHigh_clamp_low hread hrev hbegin hmore hne + hcrossD hEH hclamp hlow + unfold closedFormNegCrossCompleteHigh + rw [ite_eq_left hclamp] + rw [ite_eq_right (ne_of_lt hlow)] + exact hmin + + +open scoped Classical + +variable {r : RangeRead} {res : Result} + +/-- A key of `db.keys` at or above `head` and below `begin.key` is counted. -/ +private theorem countP_between_head_begin_eq_filter_length (db : DB) (head : Key) + (B : WithTop Key) : + db.keys.countP (fun k : Key => + decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < B)) = + (db.keys.filter (fun k : Key => + decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < B))).length := + List.countP_eq_length_filter + +/-- In the crossed negative-begin complete high cell, the number of reported keys +below `begin.key` exceeds the slack by at most one. -/ +theorem high_reportedBelowBegin_le_succ {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (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 + have hlen := negCross_filter_begin_length hcomp hrev hmore hne hbegin hcross' hnon + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + -- The reported count below `begin.key` is at most the stored interval count. + have hfilter_res : res.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply List.filter_congr + intro k hk + have hheadle : ((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (head_le_of_sortedLT' + (RangeRead.compatible_sortedLT hcomp hrev) hne hk) + simp only [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hheadle, h⟩⟩ + have hcnt_res : res.keys.countP (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := by + rw [reportedBelowBegin, List.countP_eq_length_filter (p := fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)), + List.countP_eq_length_filter (p := fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)), hfilter_res] + have hle_count : res.keys.countP (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) ≤ + db.keys.countP (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := + List.Sublist.countP_le hsub + have hcnt_db : db.keys.countP (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by + rw [countP_between_head_begin_eq_filter_length] + exact hlen + omega + + +/-- In the crossed negative-begin complete high cell the reported list is no longer +than the end offset allows: `#reported ≤ (end.offset - 1).toNat`. The window is a +contiguous stored block above `end.key`, and the end anchor adds only `(end.offset - 1)` +to the count below `end.key`. -/ +theorem high_length_le_end {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + res.keys.length ≤ (r.end_.offset - 1).toNat := by + obtain ⟨db, hcomp⟩ := hread + set lo : WithTop Key := resolve db r.begin with hlo + set hi : WithTop Key := resolve db r.end_ with hhi + have hkeys : res.keys = RangeRead.inRange db.keys lo hi := by + have heq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, he⟩ + · simp [hmore] at hm + · exact he + exact heq.trans (completeKeys_eq_inRange_of_not_rev hrev) + have hgt : ∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) := + high_reported_gt_end (RangeRead.compatible_sortedLT hcomp hrev) hne hEH + have hbetween_eq : between db lo hi = res.keys := by + rw [hkeys] + rfl + have hheadmem : res.keys.headD ([] : Key) ∈ res.keys := headD_mem hne + have hhead_in : (res.keys.headD ([] : Key)) ∈ db.keys ∧ + lo ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < hi := by + have hmem : (res.keys.headD ([] : Key)) ∈ RangeRead.inRange db.keys lo hi := by + rw [← hkeys]; exact hheadmem + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hmem + have hEH' : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) := hEH + have hE_hi : r.end_.key < hi := lt_trans hEH' hhead_in.2.2 + have hlo_hi : lo ≤ hi := le_trans hhead_in.2.1 (le_of_lt hhead_in.2.2) + have hcnt_hi : cnt db hi = cnt db lo + res.keys.length := by + rw [cnt_eq_add_between db hlo_hi, hbetween_eq] + have hcnt_hi' : cnt db hi = + min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length := by + rw [hhi] + show db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + resolve db r.end_)) = min (cnt db r.end_.key + (r.end_.offset - 1).toNat) + db.keys.length + rw [countP_lt_resolve (db := db) (s := r.end_), + anchor_toNat_eq (s := r.end_) (by omega)] + rfl + have hcntE_le : cnt db r.end_.key ≤ cnt db lo := by + rcases le_total r.end_.key lo with hEl | hloE + · exact cnt_le_cnt_of_le db hEl + · have hb0 : between db lo r.end_.key = [] := by + rw [between, List.filter_eq_nil_iff] + intro k hkdb hcon + simp only [decide_eq_true_eq] at hcon + obtain ⟨hklo, hkE⟩ := hcon + have hkhi : ((k : Key) : WithTop Key) < hi := lt_trans hkE hE_hi + have hkres : k ∈ res.keys := by + rw [hkeys] + simp only [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] + exact ⟨hkdb, hklo, hkhi⟩ + exact absurd (hgt k hkres) (not_lt.mpr (le_of_lt hkE)) + have h := cnt_eq_add_between db hloE + rw [hb0, List.length_nil, Nat.add_zero] at h + exact le_of_eq h + rw [hcnt_hi] at hcnt_hi' + rcases Nat.le_total (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length with h | h + · rw [Nat.min_eq_left h] at hcnt_hi' + omega + · rw [Nat.min_eq_right h] at hcnt_hi' + omega + +/-- **A sufficient shape for minimality.** If agreement with the result is exactly +equality of the stored keys in a fixed interval `[E, U)`, then that interval is minimal. +Soundness is immediate because agreement on the interval is equality of those filtered +lists; necessity follows by flipping any key of the interval into a compatible database, +which perturbs the filtered list. -/ +theorem minimal_of_compatible_iff_filter {r : RangeRead} {res : Result} + {E U : WithTop Key} (hread : Readable r res) + (hchar : ∀ db : DB, RangeRead.Compatible db r res ↔ + db.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < U)) = res.keys) : + Minimal r res ⟨E, U⟩ := by + classical + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + have hf : db1.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < U)) = + db2.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < U)) := by + simpa only [AgreeOn, ConflictRange.keys] using hagree + rw [hchar db1, hchar db2, hf] + · intro k hk + obtain ⟨hkE, hkU⟩ := ConflictRange.mem_iff.mp hk + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · obtain ⟨db0, hcomp0⟩ := hread + have hknot : k ∉ db0.keys := by + intro hkdb + have hmem : k ∈ db0.keys.filter (fun k : Key => + decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < U)) := by + rw [List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkE, hkU⟩⟩ + rw [(hchar db0).mp hcomp0] at hmem + exact hkres hmem + refine necessary_of_toggle ⟨db0, hcomp0, ?_⟩ + intro hcomp1 + have hkf : k ∈ (toggle db0 k).keys.filter (fun k : Key => + decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < U)) := by + rw [toggle_eq_insert_of_not_mem hknot, List.mem_filter] + exact ⟨DB.mem_insert_self db0 k hknot, by rw [decide_eq_true_eq]; exact ⟨hkE, hkU⟩⟩ + rw [(hchar _).mp hcomp1] at hkf + exact hkres hkf + +/-- In the crossed negative-begin complete high cell with a finite end, the begin +resolution equals the reported head, and no stored keys lie between `end.key` and +the head. -/ +theorem high_cnt_head_eq_cnt_end {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : + cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) = cnt db r.end_.key := by + set H : Key := res.keys.headD ([] : Key) with hH + have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hhead_mem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hL' : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by rw [hL', ← hHD] + have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL] + rfl + have hhead_lt_hi : ((H : Key) : WithTop Key) < resolve db r.end_ := by + have hmem : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [← hwin]; exact hhead_mem + have hcond := of_decide_eq_true (List.mem_filter.mp hmem).2 + exact hcond.2 + have hcnt_hi : cnt db (resolve db r.end_) = + cnt db (((H : Key) : WithTop Key)) + res.keys.length := by + rw [cnt_eq_add_between db (le_of_lt hhead_lt_hi), ← hwin] + have hcnt_hi_le : cnt db (resolve db r.end_) ≤ + cnt db r.end_.key + (r.end_.offset - 1).toNat := by + have h : cnt db (resolve db r.end_) = + min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length := by + rw [show cnt db (resolve db r.end_) = db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl, + countP_lt_resolve (db := db) (s := r.end_), + anchor_toNat_eq (s := r.end_) (by omega)] + rfl + rw [h]; exact Nat.min_le_left _ _ + rw [hcnt_hi, hfinite] at hcnt_hi_le + have hcntE_le : cnt db r.end_.key ≤ cnt db (((H : Key) : WithTop Key)) := + cnt_le_cnt_of_le db (le_of_lt hEH) + omega + +/-- In the crossed negative-begin complete high cell with a finite end, no stored key +lies in `[end.key, head)`. -/ +theorem high_between_end_head_eq_nil {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : + between db r.end_.key (((res.keys.headD ([] : Key) : Key) : WithTop Key)) = [] := by + have hcnt := high_cnt_head_eq_cnt_end hcomp hrev hmore hne hbegin hend hEH hnonclamp hfinite + have hsplit := cnt_eq_add_between db (le_of_lt hEH) + rw [hcnt, between] at hsplit + have hlen : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key)))).length = 0 := by + omega + exact List.length_eq_zero_iff.mp hlen + +/-- In the crossed negative-begin complete high cell the reported-below-begin count is +exactly the begin slack plus one. -/ +theorem high_reportedBelowBegin_eq_slack_succ {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + reportedBelowBegin r res = beginSlack r + 1 := by + have hle := high_reportedBelowBegin_le_succ hread hrev hmore hne hbegin hcrossD + omega + +/-- In the crossed negative-begin complete high cell with a finite end and a begin key +above `keyAfter last`, no stored key lies in `[keyAfter last, begin.key)`. -/ +theorem high_between_keyAfter_begin_eq_nil {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hKleB : ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) ≤ r.begin.key) : + between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) r.begin.key = [] := by + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + have hHhead : H = res.keys.head hne := headD_eq_head_of_ne hne + have hLlast : L = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + rw [← hHhead]; exact hcrossD + -- The reported-below count is the slack successor. + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ ⟨db, hcomp⟩ hrev hmore hne hbegin hcrossD hnonclamp + -- Length of `[H, begin.key)`. + have hlenHB : (between db ((H : Key) : WithTop Key) r.begin.key).length = beginSlack r + 1 := by + rw [hHhead] + exact negCross_filter_begin_length hcomp hrev hmore hne hbegin hcross hnonclamp + -- `res.keys` below `begin.key` are exactly the `res.keys` at or below the last. + have hcount_res : (res.keys.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key))).length = beginSlack r + 1 := by + rw [← List.countP_eq_length_filter, ← hr]; rfl + -- Every reported key is at or below the last. + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hall_le_last : ∀ k ∈ res.keys, k ≤ L := by + intro k hk; rw [hLlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hHleL : H ≤ L := by + rw [hHhead, hLlast] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hH_le_K : ((H : Key) : WithTop Key) ≤ ((K : Key) : WithTop Key) := by + rw [WithTop.coe_le_coe]; exact le_trans hHleL (le_of_lt (keyAfter_self_lt L)) + -- All reported keys below `begin.key` are stored in `[H, K)`. + have hsub : ∀ k ∈ res.keys.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)), k ∈ between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) := by + intro k hk + rw [List.mem_filter] at hk + have hkres := hk.1 + have hklt : ((k : Key) : WithTop Key) < r.begin.key := by + rw [decide_eq_true_eq] at hk; exact hk.2 + have hkH : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by + rw [hHhead]; exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hkres) + have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by + rw [WithTop.coe_lt_coe]; exact lt_of_le_of_lt (hall_le_last k hkres) (keyAfter_self_lt L) + exact List.mem_filter.mpr ⟨RangeRead.compatible_keys_subset hcomp hkres, + by rw [decide_eq_true_eq]; exact ⟨hkH, hkK⟩⟩ + -- Hence `[H, K)` holds at least `beginSlack + 1` stored keys. + have hlenHK_ge : beginSlack r + 1 ≤ (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).length := by + have hfin : (res.keys.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key))).toFinset ⊆ + (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).toFinset := by + intro k hk + rw [List.mem_toFinset] at hk ⊢ + exact hsub k hk + have hcard := Finset.card_le_card hfin + rw [List.toFinset_card_of_nodup (hsort.nodup.filter _)] at hcard + simp only [between] at hcard + rw [List.toFinset_card_of_nodup (db.nodup.filter _)] at hcard + rwa [hcount_res] at hcard + -- Counts: `K ≤ begin.key`, so `[K, begin.key)` adds nothing. + have hcnt : cnt db r.begin.key = cnt db ((H : Key) : WithTop Key) + + (between db ((H : Key) : WithTop Key) r.begin.key).length := + cnt_eq_add_between db (le_of_lt hcrossD) + have hcntHB : cnt db r.begin.key = cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) := by + rw [hcnt, hlenHB] + have hcntHK : cnt db ((K : Key) : WithTop Key) = cnt db ((H : Key) : WithTop Key) + + (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).length := + cnt_eq_add_between db hH_le_K + have hK_le_ck : cnt db ((K : Key) : WithTop Key) ≤ cnt db r.begin.key := + cnt_le_cnt_of_le db hKleB + have hlenKB : (between db ((K : Key) : WithTop Key) r.begin.key).length = 0 := by + have hsplit := cnt_eq_add_between db hKleB + rw [hcntHB] at hsplit + have hle : cnt db ((K : Key) : WithTop Key) ≤ + cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) := by rw [← hcntHB]; exact hK_le_ck + have hge : cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) ≤ + cnt db ((K : Key) : WithTop Key) := by + rw [hcntHK]; exact Nat.add_le_add_left hlenHK_ge _ + have heq : cnt db ((K : Key) : WithTop Key) = + cnt db ((H : Key) : WithTop Key) + (beginSlack r + 1) := le_antisymm hle hge + rw [heq] at hsplit + omega + exact List.length_eq_zero_iff.mp (by simpa [hH, hL, hK] using hlenKB) + + +/-- Forward direction: in the crossed negative-begin complete high cell with a finite +end and a nonclamping begin, the stored keys in `[end.key, M)` are exactly the +reported keys, where `M = max begin.key (keyAfter last)`. -/ +theorem high_nonclamp_finite_filter_eq {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : + db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < negCrossCompleteHighM r res)) = res.keys := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set M : WithTop Key := negCrossCompleteHighM r res with hM + have hMdef : M = max r.begin.key ((K : Key) : WithTop Key) := by + rw [hM] + unfold negCrossCompleteHighM + rw [← hL, ← hK] + have hHhead : H = res.keys.head hne := headD_eq_head_of_ne hne + have hLlast : L = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + rw [← hHhead]; exact hcrossD + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hLresolve : resolve db r.begin = ((H : Key) : WithTop Key) := by + rw [hHhead] + exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hLresolve] + rfl + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hLmem : L ∈ res.keys := by rw [hLlast]; exact List.getLast_mem hne + have hLlt : ((L : Key) : WithTop Key) < resolve db r.end_ := by + have hmem : L ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [← hwin]; exact hLmem + exact (of_decide_eq_true (List.mem_filter.mp hmem).2).2 + have hKleR : ((K : Key) : WithTop Key) ≤ resolve db r.end_ := keyAfter_le_of_lt hLlt + have hnilEH : between db r.end_.key ((H : Key) : WithTop Key) = [] := + high_between_end_head_eq_nil hcomp hrev hmore hne hbegin hend hEH hnonclamp hfinite + refine List.Subset.antisymm_of_sortedLT ?_ ?_ + (List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)) hsort + · intro k hk + rw [List.mem_filter] at hk + obtain ⟨hkdb, hkcond⟩ := hk + rw [decide_eq_true_eq] at hkcond + obtain ⟨hkE, hkM⟩ := hkcond + by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) + · have hmem : k ∈ between db r.end_.key ((H : Key) : WithTop Key) := by + rw [between, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkE, hkH⟩⟩ + rw [hnilEH] at hmem + exact absurd hmem (by simp) + · have hHle : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH + have hkltR : ((k : Key) : WithTop Key) < resolve db r.end_ := by + by_contra hnot + have hRk : resolve db r.end_ ≤ ((k : Key) : WithTop Key) := le_of_not_gt hnot + have hkK : ((K : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_trans hKleR hRk + have hkB : ((k : Key) : WithTop Key) < r.begin.key := by + rw [hMdef] at hkM + rcases le_total r.begin.key ((K : Key) : WithTop Key) with hBK | hBK + · rw [max_eq_right hBK] at hkM + exact absurd hkM (not_lt.mpr hkK) + · rw [max_eq_left hBK] at hkM + exact hkM + have hnilKB : between db ((K : Key) : WithTop Key) r.begin.key = [] := + high_between_keyAfter_begin_eq_nil hcomp hrev hmore hne hbegin hend hEH hcrossD + hnonclamp (le_trans hkK (le_of_lt hkB)) + have hmem : k ∈ between db ((K : Key) : WithTop Key) r.begin.key := by + rw [between, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkK, hkB⟩⟩ + rw [hnilKB] at hmem + exact absurd hmem (by simp) + have hmem : k ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [between, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hHle, hkltR⟩⟩ + rw [hwin]; exact hmem + · intro k hk + rw [List.mem_filter] + have hkdb : k ∈ db.keys := RangeRead.compatible_keys_subset hcomp hk + have hkE : r.end_.key < ((k : Key) : WithTop Key) := + high_reported_gt_end hsort hne hEH k hk + have hkL : k ≤ L := by + rw [hLlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hkK : k < K := by + rw [hK]; exact lt_of_le_of_lt hkL (keyAfter_self_lt L) + have hkM : ((k : Key) : WithTop Key) < M := by + rw [hMdef] + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hkK) (le_max_right _ _) + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨le_of_lt hkE, hkM⟩⟩ + + +/-- If `p` implies `q` pointwise, then `p`-filtering equals `q`-filtering followed +by `p`-filtering. -/ +theorem filter_eq_filter_filter_of_imp {α : Type*} {l : List α} {p q : α → Bool} + (h : ∀ a, p a = true → q a = true) : + l.filter p = (l.filter q).filter p := by + rw [List.filter_filter] + apply List.filter_congr + intro a _ + cases hp : p a with + | false => simp [hp] + | true => simp [hp, h a hp] + +/-- Backward direction: in the crossed negative-begin complete high cell with a finite +end and a nonclamping begin, equality of the stored keys in `[end.key, M)` with the +reported keys forces compatibility. -/ +theorem high_nonclamp_finite_compatible_of_filter {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) + (hfilter : db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < negCrossCompleteHighM r res)) = res.keys) : + RangeRead.Compatible db r res := by + classical + obtain ⟨db0, hcomp0⟩ := hread + have hlim : RangeRead.withinLimit r res.keys := hcomp0.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set M : WithTop Key := negCrossCompleteHighM r res with hM + have hMdef : M = max r.begin.key ((K : Key) : WithTop Key) := by + rw [hM]; unfold negCrossCompleteHighM; rw [← hL, ← hK] + have hHhead : H = res.keys.head hne := headD_eq_head_of_ne hne + have hLlast : L = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + rw [← hHhead]; exact hcrossD + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ ⟨db0, hcomp0⟩ hrev hmore hne hbegin hcrossD hnonclamp + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hHmem_db : H ∈ db.keys := by + have : H ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < M)) := by rw [hfilter]; exact hHmem + exact (List.mem_filter.mp this).1 + have hHleM : ((H : Key) : WithTop Key) ≤ M := by + rw [hMdef]; exact le_trans (le_of_lt hcrossD) (le_max_left _ _) + -- No stored key lies in `[end.key, H)`. + have hnilEH : between db r.end_.key ((H : Key) : WithTop Key) = [] := by + rw [between, List.filter_eq_nil_iff] + intro k hk hdec + have hcond : r.end_.key ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key) := of_decide_eq_true hdec + have hkP : k ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < M)) := by + rw [List.mem_filter] + exact ⟨hk, by rw [decide_eq_true_eq]; exact ⟨hcond.1, lt_of_lt_of_le hcond.2 hHleM⟩⟩ + rw [hfilter] at hkP + have hmin : H ≤ k := by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hkP + exact absurd (WithTop.coe_lt_coe.mp hcond.2) (not_lt.mpr hmin) + have hcntHE : cnt db ((H : Key) : WithTop Key) = cnt db r.end_.key := by + have h := cnt_eq_add_between db (le_of_lt hEH) + rw [hnilEH] at h + simpa using h + -- The stored keys in `[H, M)` are the reported keys. + have hkeysHM : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M)) = res.keys := by + have hstep : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M)) = + (db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < M))).filter + (fun k : Key => decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < M)) := by + apply filter_eq_filter_filter_of_imp + intro k hk + rw [decide_eq_true_eq] at hk ⊢ + exact ⟨le_trans (le_of_lt hEH) hk.1, hk.2⟩ + rw [hstep, hfilter] + apply List.filter_eq_self.mpr + intro k hk + have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hk) + have hkL : k ≤ L := by + rw [hLlast]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hkM : ((k : Key) : WithTop Key) < M := by + rw [hMdef] + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkL (keyAfter_self_lt L))) + (le_max_right _ _) + rw [decide_eq_true_eq]; exact ⟨hHk, hkM⟩ + -- The stored keys in `[H, begin.key)` are exactly the reported keys there. + have hkeysHB : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by + have hstep : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M))).filter + (fun k : Key => decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + apply filter_eq_filter_filter_of_imp + intro k hk + rw [decide_eq_true_eq] at hk ⊢ + exact ⟨hk.1, lt_of_lt_of_le hk.2 (by rw [hMdef]; exact le_max_left _ _)⟩ + rw [hstep, hkeysHM] + have hlenFilter : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + rw [hkeysHB] + have hresEq : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply List.filter_congr + intro k hk + have hHk : H ≤ k := by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hk + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr hHk, h⟩⟩ + rw [hresEq, ← List.countP_eq_length_filter] + exact hr + have hhhead : res.keys.head hne ∈ db.keys := by rw [← hHhead]; exact hHmem_db + have hresB : resolve db r.begin = ((H : Key) : WithTop Key) := by + have hlenFilter' : (db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + simpa only [hHhead] using hlenFilter + rw [hHhead] + exact resolve_begin_eq_head_of_filter_len hbegin hhhead hcross hlenFilter' + have hdropH : db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key))) = db.keys.drop (cnt db r.end_.key) := by + rw [filter_not_lt_eq_drop_countP db.sorted ((H : Key) : WithTop Key)] + change db.keys.drop (cnt db ((H : Key) : WithTop Key)) = db.keys.drop (cnt db r.end_.key) + rw [hcntHE] + -- The first `#reported` stored keys from `H` are the reported keys. + have hfilterM : (db.keys.drop (cnt db r.end_.key)).filter (fun k : Key => + decide (((k : Key) : WithTop Key) < M)) = res.keys := by + rw [← hdropH] + have hstep : (db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key)))).filter (fun k : Key => + decide (((k : Key) : WithTop Key) < M)) = + db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < M)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) + · have hnot : ¬ (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := not_le.mpr hkH + simp [hkH, hnot] + · have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH + simp [hkH, hHk] + rw [hstep, hkeysHM] + have htake : (db.keys.drop (cnt db r.end_.key)).take (res.keys.length) = res.keys := by + have hsorted : (db.keys.drop (cnt db r.end_.key)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := cnt db r.end_.key) db.sorted.pairwise) + have hcount : (db.keys.drop (cnt db r.end_.key)).countP (fun k : Key => + decide (((k : Key) : WithTop Key) < M)) = res.keys.length := by + rw [List.countP_eq_length_filter, hfilterM] + have htake0 := filter_lt_eq_take_countP hsorted M + rw [hfilterM, hcount] at htake0 + exact htake0.symm + -- The drop has at least `#reported` keys, so the end anchor stays in range. + have hresSubDrop : res.keys ⊆ db.keys.drop (cnt db r.end_.key) := by + intro k hk + have hkdb : k ∈ db.keys := by + have : k ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < M)) := by rw [hfilter]; exact hk + exact (List.mem_filter.mp this).1 + have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (by rw [hHhead]; exact head_le_of_sortedLT' hsort hne hk) + rw [← hdropH, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact not_lt.mpr hHk⟩ + have hlen_ge : res.keys.length ≤ (db.keys.drop (cnt db r.end_.key)).length := by + have hfin : res.keys.toFinset ⊆ (db.keys.drop (cnt db r.end_.key)).toFinset := by + intro k hk; rw [List.mem_toFinset] at hk ⊢; exact hresSubDrop hk + have hcard := Finset.card_le_card hfin + rw [List.toFinset_card_of_nodup hsort.nodup, + List.toFinset_card_of_nodup (List.Nodup.sublist (List.drop_sublist _ _) db.nodup)] at hcard + exact hcard + have hce_le : cnt db r.end_.key + res.keys.length ≤ db.keys.length := by + rw [List.length_drop] at hlen_ge + omega + have hanchorE_toNat : (r.end_.anchor db).toNat = + cnt db r.end_.key + (r.end_.offset - 1).toNat := by + rw [anchor_toNat_eq (s := r.end_) (by omega)] + rfl + have hcntR : db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) = + cnt db r.end_.key + res.keys.length := by + rw [countP_lt_resolve (db := db) (s := r.end_), hanchorE_toNat, hfinite] + exact Nat.min_eq_left (by omega) + have hHR : ((H : Key) : WithTop Key) ≤ resolve db r.end_ := by + by_contra h + have hle : resolve db r.end_ ≤ ((H : Key) : WithTop Key) := le_of_not_ge h + have hmono := cnt_le_cnt_of_le db hle + change db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + resolve db r.end_)) ≤ cnt db ((H : Key) : WithTop Key) at hmono + rw [hcntR, hcntHE] at hmono + have hpos : 0 < res.keys.length := List.length_pos_iff.mpr hne + omega + -- Split the database at `cnt end.key`; the tail starts at the reported head. + have hER : r.end_.key ≤ resolve db r.end_ := le_trans (le_of_lt hEH) hHR + have htakeE : db.keys.take (cnt db r.end_.key) = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) := + (filter_lt_eq_take_countP db.sorted r.end_.key).symm + have hcountTake : (db.keys.take (cnt db r.end_.key)).countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) = cnt db r.end_.key := by + rw [htakeE] + have hlen : (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))).countP + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = + (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))).length := + List.countP_eq_length.mpr (by + intro k hk + rw [List.mem_filter] at hk + exact decide_eq_true (lt_of_lt_of_le (of_decide_eq_true hk.2) hER)) + rw [hlen, ← List.countP_eq_length_filter] + rfl + have hcountR : (db.keys.drop (cnt db r.end_.key)).countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys.length := by + have hsplit := List.countP_append (p := fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) + (l₁ := db.keys.take (cnt db r.end_.key)) (l₂ := db.keys.drop (cnt db r.end_.key)) + rw [List.take_append_drop] at hsplit + rw [hcountTake, hcntR] at hsplit + omega + have hwindow : between db ((H : Key) : WithTop Key) (resolve db r.end_) = res.keys := by + have hsorted : (db.keys.drop (cnt db r.end_.key)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := cnt db r.end_.key) db.sorted.pairwise) + have hstep : between db ((H : Key) : WithTop Key) (resolve db r.end_) = + (db.keys.drop (cnt db r.end_.key)).filter (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + unfold between + rw [← hdropH, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) + · have hnot : ¬ (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := not_le.mpr hkH + simp [hkH, hnot] + · have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH + simp [hkH, hHk] + rw [hstep, filter_lt_eq_take_countP hsorted (resolve db r.end_), hcountR, htake] + have hck : RangeRead.completeKeys db r = res.keys := by + rw [completeKeys_eq_inRange_of_not_rev hrev, hresB, RangeRead.inRange] + exact hwindow + exact ⟨hlim, Or.inr ⟨hmore, hck.symm⟩⟩ + + +/-- Nonclamping finite high cell: compatibility is exactly the `[end.key, M)` filter +equality. -/ +theorem high_nonclamp_finite_compatible_iff_filter {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : + RangeRead.Compatible db r res ↔ + db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < negCrossCompleteHighM r res)) = res.keys := + ⟨fun hcomp => high_nonclamp_finite_filter_eq hcomp hrev hmore hne hbegin hend hEH hcrossD + hnonclamp hfinite, + high_nonclamp_finite_compatible_of_filter hread hrev hmore hne hbegin hend hEH hcrossD + hnonclamp hfinite⟩ + +/-- Nonclamping finite crossed negative-begin complete high cell: the minimal range is +`[end.key, max begin.key (keyAfter last))`. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh_nonclamp_finite {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : + Minimal r res ⟨r.end_.key, negCrossCompleteHighM r res⟩ := + minimal_of_compatible_iff_filter hread (fun db => + high_nonclamp_finite_compatible_iff_filter hread hrev hmore hne hbegin hend hEH hcrossD + hnonclamp hfinite) + + +/-- The nonclamping finite branch of the crossed negative-begin complete high cell. -/ +abbrev negCrossCompleteHighNonclampFinite (r : RangeRead) (res : Result) : Prop := + negCrossCompleteHigh r res ∧ beginSlack r < reportedBelowBegin r res ∧ + res.keys.length = (r.end_.offset - 1).toNat + +/-- The nonclamping low-offset branch of the crossed negative-begin complete high +cell. -/ +abbrev negCrossCompleteHighNonclampLow (r : RangeRead) (res : Result) : Prop := + negCrossCompleteHigh r res ∧ beginSlack r < reportedBelowBegin r res ∧ + res.keys.length < (r.end_.offset - 1).toNat + +/-- **The nonclamping finite branch of the crossed negative-begin complete high cell is +minimal** for the total closed form. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh_nonclampFinite {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighNonclampFinite r res) : + Minimal r res (closedFormNegCrossCompleteHigh r res) := by + obtain ⟨⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩, hnonclamp, hfinite⟩ := hcell + have hmin := minimal_closedForm_of_negCrossCompleteHigh_nonclamp_finite hread hrev hmore hne + hbegin hend2 hEH hcrossD hnonclamp hfinite + unfold closedFormNegCrossCompleteHigh + rw [ite_eq_right (not_le.mpr hnonclamp)] + rw [ite_eq_left hfinite] + exact hmin + + +/-- Filtering at or above a bound and below `⊤` is the `above` list. -/ +theorem filter_le_top_eq_above_aux (db : DB) (E : WithTop Key) : + db.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = above db E := by + rw [above_eq_filter] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact and_iff_left (WithTop.coe_lt_top k) + +/-- The end resolution overshoots in the nonclamping low branch with no capacity. -/ +theorem high_nonclamp_low_resolveEnd_top_of_compat {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hno : ¬ (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + resolve db r.end_ = (⊤ : WithTop Key) := by + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set D : ℕ := e - t with hD + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by + rw [hH, hheadD] + exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL] + rfl + -- `m = #stored in [E, H)`. + have hEHcoe : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEHcoe + set m : ℕ := (between db E ((H : Key) : WithTop Key)).length with hm + -- count identity at the end resolution + have hanchor : (r.end_.anchor db).toNat = cnt db E + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega), hE, he] + rfl + have hcnt_end : cnt db (resolve db r.end_) = min (cnt db E + e) db.keys.length := by + rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl, + countP_lt_resolve (db := db) (s := r.end_), hanchor] + have hcntH_split : cnt db ((H : Key) : WithTop Key) = cnt db E + m := by + rw [hm]; exact cnt_eq_add_between db hEB + have hHmem_between : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [← hwin]; rw [hH]; exact headD_mem hne + have hH_le_res : ((H : Key) : WithTop Key) ≤ resolve db r.end_ := + le_of_lt (of_decide_eq_true (List.mem_filter.mp hHmem_between).2).2 + have hcnt_win : cnt db (resolve db r.end_) = + cnt db ((H : Key) : WithTop Key) + t := by + rw [cnt_eq_add_between db hH_le_res, ← hwin, ← ht] + -- `m ≤ intervalCard E H < D`, hence `m + t < e`. + have hmcard : ((m : ℕ) : ℕ∞) ≤ intervalCard E ((H : Key) : WithTop Key) := by + rw [hm]; exact between_length_le_intervalCard hEB + have hcardD : intervalCard E ((H : Key) : WithTop Key) < ((D : ℕ) : ℕ∞) := by + rw [hE, hH, hD, he, ht] at hno ⊢ + exact lt_of_not_ge hno + have hmD : m < D := by + have : ((m : ℕ) : ℕ∞) < ((D : ℕ) : ℕ∞) := lt_of_le_of_lt hmcard hcardD + exact_mod_cast this + have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow) + have hmte : m + t < e := by omega + -- Now compute the count of the end resolution and show it is `⊤`. + have hcnt_lower : cnt db (resolve db r.end_) = cnt db E + m + t := by + rw [hcnt_win, hcntH_split] + by_contra hnot + have hlt : (r.end_.anchor db).toNat < db.keys.length := by + by_contra hge + exact hnot (resolve_eq_top (by omega) (le_of_not_gt hge)) + have hmin : min (cnt db E + e) db.keys.length = cnt db E + e := + Nat.min_eq_left (by rw [← hanchor]; exact le_of_lt hlt) + rw [hcnt_end, hmin] at hcnt_lower + omega + +/-- **No-cap nonclamp branch: compatibility is exactly agreement of the keys at or +above the reported head with the result.** -/ +theorem high_nonclamp_low_aboveH_iff {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hno : ¬ (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + RangeRead.Compatible db r res ↔ above db ((res.keys.headD ([] : Key) : Key) : WithTop Key) = res.keys := by + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set D : ℕ := e - t with hD + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hEHcoe : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEHcoe + have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow) + constructor + · intro hcomp + have htop := high_nonclamp_low_resolveEnd_top_of_compat hcomp hrev hmore hne hbegin hend + hEH hnonclamp hlow hno + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by + rw [hH, hheadD] + exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL]; rfl + rw [hwin, htop] + exact (filter_le_top_eq_above_aux db ((H : Key) : WithTop Key)).symm + · intro habove + -- forward construction of compatibility + have hwl : RangeRead.withinLimit r res.keys := by + obtain ⟨db0, hcomp0⟩ := hread + exact RangeRead.compatible_withinLimit hcomp0 + have hHmem_db : H ∈ db.keys := by + have : H ∈ above db ((H : Key) : WithTop Key) := by rw [habove]; exact hHmem + exact (mem_above.mp this).1 + have hcross : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD + -- filter `[H, begin.key)` has length `q + 1`. + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ hread hrev hmore hne hbegin hcrossD hnonclamp + have haboveH : above db ((H : Key) : WithTop Key) = res.keys := habove + have hfilterHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + have h1 : db.keys.filter (fun k : Key => + decide (((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = + (above db ((H : Key) : WithTop Key)).filter (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [above_eq_filter, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) <;> + simp [hk] + rw [h1, haboveH] + rw [reportedBelowBegin, List.countP_eq_length_filter] at hr + exact hr + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by + exact resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem_db hcross hfilterHB + -- the end resolution is `⊤` + have hanchor : (r.end_.anchor db).toNat = cnt db E + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega), hE, he] + rfl + have hcnt_above : (above db E).length = (between db E ((H : Key) : WithTop Key)).length + + (above db ((H : Key) : WithTop Key)).length := by + have h1 := cnt_add_above_length db E + have h2 := cnt_eq_add_between db hEB + have h3 := cnt_add_above_length db ((H : Key) : WithTop Key) + omega + have haboveHlen : (above db ((H : Key) : WithTop Key)).length = t := by + rw [haboveH, ht] + have hlenE : (above db E).length = (between db E ((H : Key) : WithTop Key)).length + t := by + rw [hcnt_above, haboveHlen] + have hmcard : ((between db E ((H : Key) : WithTop Key)).length : ℕ∞) ≤ + intervalCard E ((H : Key) : WithTop Key) := between_length_le_intervalCard hEB + have hcardD : intervalCard E ((H : Key) : WithTop Key) < ((D : ℕ) : ℕ∞) := by + rw [hE, hH, hD, he, ht] at hno ⊢ + exact lt_of_not_ge hno + have hmD : (between db E ((H : Key) : WithTop Key)).length < D := by + have : ((between db E ((H : Key) : WithTop Key)).length : ℕ∞) < ((D : ℕ) : ℕ∞) := + lt_of_le_of_lt hmcard hcardD + exact_mod_cast this + have hlen_lt : db.keys.length < cnt db E + e := by + have hlen : db.keys.length = cnt db E + (above db E).length := + (cnt_add_above_length db E).symm + rw [hlen, hlenE] + omega + have htop : resolve db r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top (by omega) + rw [hanchor]; omega + refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩ + rw [RangeRead.completeKeys, hrev] + simp only [Bool.false_eq_true, if_false] + rw [hL, htop] + -- between H top = above H = res.keys + show res.keys = List.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) db.keys + exact haboveH.symm.trans (filter_le_top_eq_above_aux db ((H : Key) : WithTop Key)).symm + +/-- **The no-cap nonclamping low branch is `[head, ⊤)`.** -/ +theorem minimal_nonclampLow_noCap {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hno : ¬ (((r.end_.offset - 1).toNat - res.keys.length : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + Minimal r res ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), ⊤⟩ := by + refine minimal_of_compatible_iff_filter hread (fun db => ?_) + rw [high_nonclamp_low_aboveH_iff hread hrev hmore hne hbegin hend hEH hcrossD hnonclamp hlow hno, + filter_le_top_eq_above_aux] + +/-- A genuine key strictly above the empty key is nonempty. -/ +theorem ne_nil_of_bot_lt {E : WithTop Key} {H : Key} (h : E < (H : WithTop Key)) : + H ≠ [] := by + intro hH + rw [hH] at h + exact (not_lt.mpr (coe_nil_le_key_withTop E)) h + +/-- **Soundness of `[end_.key, ⊤)` in the nonclamping low branch of the crossed +negative-begin complete high cell.** Agreement on the keys at or above `end_.key` +fixes the end resolution (positive offset) and, through the reported head and the +interval `[end_.key, begin.key)`, the begin resolution (negative offset). The complete +window then starts at the reported head, which lies at or above `end_.key`, so it too is +fixed. -/ +theorem sound_topRange_endKey_nonclamp_low {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hend : 2 ≤ r.end_.offset) : + Sound r res ⟨r.end_.key, ⊤⟩ := by + set H : Key := res.keys.headD ([] : Key) with hH + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hEH' : r.end_.key < ((H : Key) : WithTop Key) := by rw [hH]; exact hEH + have hE_le_H : r.end_.key ≤ ((H : Key) : WithTop Key) := le_of_lt hEH' + have hcrossE : r.end_.key < r.begin.key := lt_trans hEH' hcrossD + have step : ∀ db1 db2 : DB, AgreeOn ⟨r.end_.key, ⊤⟩ db1 db2 → + RangeRead.Compatible db1 r res → RangeRead.Compatible db2 r res := by + intro db1 db2 hagree hcomp1 + have habove : above db1 r.end_.key = above db2 r.end_.key := by + have h := hagree + rw [AgreeOn, keys_top_eq_above, keys_top_eq_above] at h + exact h + have hag : ∀ k : Key, r.end_.key ≤ (k : WithTop Key) → + (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk + have h := congrArg (fun l : List Key => k ∈ l) habove + rw [mem_above, mem_above] at h + exact ⟨fun hk1 => (h.mp ⟨hk1, hk⟩).1, fun hk2 => (h.mpr ⟨hk2, hk⟩).1⟩ + have hRend : resolve db1 r.end_ = resolve db2 r.end_ := + resolve_eq_of_agree_ge (db₁ := db1) (db₂ := db2) (s := r.end_) (by omega) hag + have hHne : H ≠ [] := ne_nil_of_bot_lt + (lt_of_le_of_lt (coe_nil_le_key_withTop r.end_.key) hEH') + have hRbeg1 : resolve db1 r.begin = ((H : Key) : WithTop Key) := by + rw [hH, hheadD] + exact resolve_begin_eq_head_of_negCross_compat' hcomp1 hrev hmore hne hbegin hnonclamp + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (e : WithTop Key) := by + cases h : r.end_.key with + | top => rw [h] at hEH; exact absurd hEH (not_lt.mpr le_top) + | coe e => exact ⟨e, rfl⟩ + have hbetween_agree : between db1 r.end_.key r.begin.key = + between db2 r.end_.key r.begin.key := by + rw [between_eq_filter_above, between_eq_filter_above, habove] + have hq := resolve_filter_reverse_eq_some_of_ne_nil (db := db1) (s := r.begin) + (le_of_lt hbegin) hHne hRbeg1 + have hRbeg2 : resolve db2 r.begin = ((H : Key) : WithTop Key) := by + refine resolve_eq_of_agree_between (db₁ := db1) (db₂ := db2) (s := r.begin) + (le_of_lt hbegin) (a := e) (E := r.begin.key) (q := H) rfl ?_ ?_ hq ?_ + · rw [← he]; exact le_of_lt hcrossE + · simpa [he, between] using hbetween_agree + · rw [← he]; exact le_of_lt hEH' + have hck : RangeRead.completeKeys db1 r = RangeRead.completeKeys db2 r := by + have h1 : RangeRead.completeKeys db1 r = + between db1 ((H : Key) : WithTop Key) (resolve db1 r.end_) := by + rw [completeKeys_eq_inRange_of_not_rev hrev, hRbeg1]; rfl + have h2 : RangeRead.completeKeys db2 r = + between db2 ((H : Key) : WithTop Key) (resolve db2 r.end_) := by + rw [completeKeys_eq_inRange_of_not_rev hrev, hRbeg2]; rfl + rw [h1, h2, hRend, between_eq_filter_above, between_eq_filter_above, + above_B_eq_filter (db := db1) hE_le_H, above_B_eq_filter (db := db2) hE_le_H, + habove] + exact (RangeRead.compatible_congr hck).mp hcomp1 + intro db1 db2 hagree + exact ⟨step db1 db2 hagree, step db2 db1 hagree.symm⟩ + +/-- The stored keys of the top range starting at `E` are the stored keys at or above `E`. -/ +theorem between_top_eq_above (db : DB) (E : WithTop Key) : + between db E (⊤ : WithTop Key) = above db E := by + unfold between above + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact and_iff_left (WithTop.coe_lt_top k) + +/-- In the nonclamping low branch of the crossed negative-begin complete high cell, the +reported list itself is a compatible database. The begin selector resolves onto the +reported head and the end selector overshoots the finite list. -/ +theorem high_nonclamp_low_base_compatible {r : RangeRead} {res : Result} + {hsort : res.keys.SortedLT} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) : + RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by + set H : Key := res.keys.headD ([] : Key) with hH + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hcross : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD + have hwl : RangeRead.withinLimit r res.keys := by + obtain ⟨db0, hcomp0⟩ := hread + exact RangeRead.compatible_withinLimit hcomp0 + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ hread hrev hmore hne hbegin hcrossD hnonclamp + have hfilter : (res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + have h2 : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply List.filter_congr + intro k hk + have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk) + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hHk, h⟩⟩ + rw [reportedBelowBegin, List.countP_eq_length_filter] at hr + rw [h2] + exact hr + have hL : resolve (⟨res.keys, hsort⟩ : DB) r.begin = ((H : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem hcross hfilter + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + have hcE : (⟨res.keys, hsort⟩ : DB).keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [decide_eq_true_eq] + exact not_lt.mpr (le_of_lt (high_reported_gt_end hsort hne hEH k hk)) + have hepos : 1 ≤ r.end_.offset := by omega + have hDpos : 0 < e - t := by rw [he, ht]; omega + have hanchor : (r.end_.anchor (⟨res.keys, hsort⟩ : DB)).toNat = e := by + rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add, he] + have htop : resolve (⟨res.keys, hsort⟩ : DB) r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top + · unfold Selector.anchor + rw [hcE] + have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + simpa using this + · rw [hanchor] + show res.keys.length ≤ e + rw [← ht] + omega + have hbetween : between (⟨res.keys, hsort⟩ : DB) ((H : Key) : WithTop Key) + (⊤ : WithTop Key) = res.keys := by + rw [between_top_eq_above] + unfold above + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr (by + rw [hH, hheadD] + exact head_le_of_sortedLT' hsort hne hk) + refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩ + rw [RangeRead.completeKeys, hrev] + simp only [Bool.false_eq_true, if_false] + rw [hL, htop] + exact hbetween.symm + +/-- Keys at or above the reported head are necessary in the nonclamping low branch of the +crossed negative-begin complete high cell. -/ +theorem necessary_above_head_nonclamp_low {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hkH : ((res.keys.headD ([] : Key) : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) + (hknot : k ∉ res.keys) : + Necessary r res k := by + have hread0 : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread0 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set H : Key := res.keys.headD ([] : Key) with hH + have hEH' : r.end_.key < ((H : Key) : WithTop Key) := by rw [hH]; exact hEH + have hbase : RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := + high_nonclamp_low_base_compatible (hsort := hsort) hread hrev hmore hne hbegin hEH hcrossD + hnonclamp hlow + have hknot0 : k ∉ (⟨res.keys, hsort⟩ : DB).keys := hknot + refine necessary_of_toggle ⟨⟨res.keys, hsort⟩, hbase, ?_⟩ + rw [toggle_eq_insert_of_not_mem hknot0] + intro hcompIns + have hRb := resolve_begin_eq_head_of_negCross_compat' hcompIns hrev hmore hne hbegin hnonclamp + set dbIns : DB := (⟨res.keys, hsort⟩ : DB).insert k hknot0 with hdbIns + have hcE : dbIns.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by + rw [hdbIns, DB.insert_keys, List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact not_lt.mpr (le_trans (le_of_lt hEH') hkH) + · exact not_lt.mpr (le_of_lt (high_reported_gt_end hsort hne hEH x hx)) + have hepos : 1 ≤ r.end_.offset := by omega + have hanchor : (r.end_.anchor dbIns).toNat = (r.end_.offset - 1).toNat := by + rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add] + have hlen : dbIns.keys.length = res.keys.length + 1 := by + rw [hdbIns, DB.insert_keys, List.orderedInsert_length] + have htop : resolve dbIns r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top + · unfold Selector.anchor + rw [hcE] + have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + simpa using this + · rw [hanchor, hlen]; omega + have hcomplete : res.keys = RangeRead.completeKeys dbIns r := by + rcases hcompIns.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hheadHeq : ((res.keys.head hne : Key) : WithTop Key) = ((H : Key) : WithTop Key) := by + rw [hH, hheadD] + have hbetween : RangeRead.completeKeys dbIns r = above dbIns ((H : Key) : WithTop Key) := by + rw [completeKeys_eq_inRange_of_not_rev hrev, hRb, htop, hheadHeq] + exact between_top_eq_above dbIns ((H : Key) : WithTop Key) + have hkabove : k ∈ above dbIns ((H : Key) : WithTop Key) := by + rw [mem_above] + exact ⟨by rw [hdbIns, DB.insert_keys]; exact (List.mem_orderedInsert (· ≤ ·)).mpr (Or.inl rfl), + hkH⟩ + rw [← hbetween, ← hcomplete] at hkabove + exact hknot hkabove + +theorem high_nonclamp_low_pad_compatible {r : RangeRead} {res : Result} {pad : List Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hpad : ∀ x ∈ pad, r.end_.key ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hps : pad.SortedLT) + (hlen : pad.length ≤ negCrossHighD r res) : + ∃ hs : (pad ++ res.keys).SortedLT, + RangeRead.Compatible ⟨pad ++ res.keys, hs⟩ r res := by + have hread0 : Readable r res := hread + obtain ⟨db0, hcomp0⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + have hwl : RangeRead.withinLimit r res.keys := + RangeRead.compatible_withinLimit hcomp0 + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set D : ℕ := e - t with hD + have hDdef : negCrossHighD r res = D := by + unfold negCrossHighD; rw [← he, ← ht, ← hD] + have hhlen : pad.length ≤ D := hDdef ▸ hlen + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hcross : ∀ x ∈ pad, ∀ y ∈ res.keys, x < y := by + intro x hx y hy + have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by + rw [hH]; exact (hpad x hx).2 + have hyH : ((H : Key) : WithTop Key) ≤ ((y : Key) : WithTop Key) := by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hy) + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hxH hyH) + refine ⟨List.sortedLT_append.mpr ⟨hps, hsort, hcross⟩, ?_⟩ + set db : DB := ⟨pad ++ res.keys, List.sortedLT_append.mpr ⟨hps, hsort, hcross⟩⟩ with hdb + have hkeys : db.keys = pad ++ res.keys := rfl + -- all keys are at or above `E` + have hge : ∀ x ∈ db.keys, E ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [hkeys, List.mem_append] at hx + rcases hx with hx | hx + · rw [hE]; exact (hpad x hx).1 + · rw [hE] + exact le_of_lt (lt_of_lt_of_le hEH' (by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx))) + have hcntE : db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq, ← hE] + exact not_lt.mpr (hge x hx) + have hepos : 1 ≤ r.end_.offset := by omega + have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow) + have hlenDB : db.keys.length = pad.length + t := by rw [hkeys, List.length_append, ht] + -- the end selector overshoots + have hanchorE : (r.end_.anchor db).toNat = e := by + rw [anchor_toNat_eq (s := r.end_) hepos, hcntE, Nat.zero_add, he] + have htop : resolve db r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top + · unfold Selector.anchor + rw [hcntE] + have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + simpa using this + · rw [hanchorE, hlenDB] + omega + -- the begin selector resolves onto `H` + have hHmem : H ∈ db.keys := by + rw [hkeys, List.mem_append] + exact Or.inr (by rw [hH, hheadD]; exact List.head_mem hne) + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ hread0 hrev hmore hne hbegin hcrossD hnonclamp + have hfilter : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + rw [hkeys, List.filter_append] + have hnil : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.1 (not_le.mpr (by rw [hH]; exact (hpad x hx).2)) + rw [hnil, List.nil_append] + have hcongr : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply List.filter_congr + intro k hk + have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk) + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hHk, h⟩⟩ + rw [hcongr] + rw [reportedBelowBegin, List.countP_eq_length_filter] at hr + exact hr + have hRbeg : resolve db r.begin = ((H : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem + (by rw [hH]; exact hcrossD) hfilter + -- the complete window is the reported list + have hcomplete : RangeRead.completeKeys db r = res.keys := by + rw [RangeRead.completeKeys, hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hRbeg, htop] + unfold RangeRead.inRange + rw [filter_le_top_eq_above_aux] + unfold above + rw [hkeys, List.filter_append] + have hnil : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_le.mpr (by rw [hH]; exact (hpad x hx).2) + rw [hnil, List.nil_append] + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk) + exact ⟨hwl, Or.inr ⟨hmore, hcomplete.symm⟩⟩ + +/-- In the nonclamping low branch, every compatible database stores at most +`D = (end.offset - 1).toNat - #reported` keys in the gap `[end.key, reported head)`. -/ +theorem high_nonclamp_low_pad_count_le {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) : + (between db r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key)).length ≤ + negCrossHighD r res := by + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set D : ℕ := e - t with hD + have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD] + rw [hDdef] + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by + rw [hH, hheadD] + exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL] + rfl + have hEHcoe : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEHcoe + set m : ℕ := (between db E ((H : Key) : WithTop Key)).length with hm + have hanchor : (r.end_.anchor db).toNat = cnt db E + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega), hE, he] + rfl + have hcnt_end : cnt db (resolve db r.end_) = min (cnt db E + e) db.keys.length := by + rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl, + countP_lt_resolve (db := db) (s := r.end_), hanchor] + have hcntH_split : cnt db ((H : Key) : WithTop Key) = cnt db E + m := by + rw [hm]; exact cnt_eq_add_between db hEB + have hHmem_between : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [← hwin]; rw [hH]; exact headD_mem hne + have hH_le_res : ((H : Key) : WithTop Key) ≤ resolve db r.end_ := + le_of_lt (of_decide_eq_true (List.mem_filter.mp hHmem_between).2).2 + have hcnt_win : cnt db (resolve db r.end_) = + cnt db ((H : Key) : WithTop Key) + t := by + rw [cnt_eq_add_between db hH_le_res, ← hwin, ← ht] + have hcnt_lower : cnt db (resolve db r.end_) = cnt db E + m + t := by + rw [hcnt_win, hcntH_split] + have hmte : m + t ≤ e := by + rw [hcnt_lower, hcnt_end] at * + have := Nat.min_le_left (cnt db E + e) db.keys.length + omega + have hmD : m ≤ D := by rw [hD]; omega + exact hmD + +/-- Gap keys `[end.key, head)` are necessary in the nonclamping low branch when the gap +strictly exceeds the excess `D`: inserting the key into a maximal pad makes one key too +many. -/ +theorem necessary_gap_nonclamp_low_lt_cap {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hkE : r.end_.key ≤ ((k : Key) : WithTop Key)) + (hkH : ((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hknot : k ∉ res.keys) + (hlt : ((negCrossHighD r res : ℕ) : ℕ∞) < negCrossHighGapCap r res) : + Necessary r res k := by + have hcap : (((negCrossHighD r res + 1 : ℕ) : ℕ∞)) ≤ negCrossHighGapCap r res := by + have h := (ENat.add_one_le_iff (by simp)).mpr hlt + simpa [Nat.cast_add, Nat.cast_one] using h + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + have hcap' : (((negCrossHighD r res + 1 : ℕ) : ℕ∞)) ≤ intervalCard E ((H : Key) : WithTop Key) := by + simpa [negCrossHighGapCap, hE, hH] using hcap + obtain ⟨dbPad, hdbPadIn, hkpad, hpadlen⟩ := + exists_db_keysIn_avoid (B := E) (E := ((H : Key) : WithTop Key)) (k := k) + (m := negCrossHighD r res) hcap' + set pad : List Key := dbPad.keys with hpaddef + have hpadIn : ∀ x ∈ pad, E ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by + intro x hx + simpa only [pad, keysIn, Set.mem_ofPred_eq] using hdbPadIn x hx + have hps : pad.SortedLT := dbPad.sorted + have hplen : pad.length = negCrossHighD r res := hpadlen + obtain ⟨hs0, hcomp0⟩ := high_nonclamp_low_pad_compatible (pad := pad) + hread hrev hmore hne hbegin hEH hcrossD hnonclamp hlow hpadIn hps (le_of_eq hplen) + set db0 : DB := ⟨pad ++ res.keys, hs0⟩ with hdb0 + have hknot0 : k ∉ db0.keys := by + rw [hdb0] + simp only + rw [List.mem_append] + push_neg + exact ⟨hkpad, hknot⟩ + refine necessary_of_toggle ⟨db0, hcomp0, ?_⟩ + rw [toggle_eq_insert_of_not_mem hknot0] + intro hcompIns + set db1 : DB := db0.insert k hknot0 with hdb1 + have hle := high_nonclamp_low_pad_count_le hcompIns hrev hmore hne hbegin (by omega) + hEH hnonclamp hlow + -- reduce to the two count values + have hcntH1 : cnt db1 ((H : Key) : WithTop Key) = cnt db0 ((H : Key) : WithTop Key) + 1 := by + rw [hdb1] + unfold cnt + rw [DB.insert_keys] + have := countP_orderedInsert_begin (l := db0.keys) db0.sorted hknot0 + (B := ((H : Key) : WithTop Key)) + have hkH' : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by rw [hH]; exact hkH + simpa [hkH'] using this + have hcntE1 : cnt db1 E = cnt db0 E := by + rw [hdb1] + unfold cnt + rw [DB.insert_keys] + have := countP_orderedInsert_begin (l := db0.keys) db0.sorted hknot0 (B := E) + have hkE' : ¬ ((k : Key) : WithTop Key) < E := not_lt.mpr (by rw [hE]; exact hkE) + simpa [hkE'] using this + have hbetween1 : (between db1 E ((H : Key) : WithTop Key)).length = + (between db0 E ((H : Key) : WithTop Key)).length + 1 := by + have hEB : E ≤ ((H : Key) : WithTop Key) := by rw [hE, hH]; exact le_of_lt hEH + have h1 := cnt_eq_add_between (db := db1) hEB + have h0 := cnt_eq_add_between (db := db0) hEB + rw [hcntH1, hcntE1] at h1 + omega + have hbetween0 : (between db0 E ((H : Key) : WithTop Key)).length = negCrossHighD r res := by + rw [hdb0] + unfold between + rw [List.filter_append] + have hpadf : pad.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = pad := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hpadIn x hx + have hresf : res.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.2 (not_lt.mpr (by + rw [hH] + exact WithTop.coe_le_coe.mpr (by + rw [headD_eq_head_of_ne hne] + exact head_le_of_sortedLT' + (RangeRead.compatible_sortedLT hcomp0 hrev) hne hx))) + rw [hpadf, hresf, List.append_nil] + exact hplen + rw [hbetween0] at hbetween1 + have hle' : (between db1 E ((H : Key) : WithTop Key)).length ≤ negCrossHighD r res := by + simpa only [← hE, ← hH] using hle + omega + +/-- **A compatible database in the nonclamping low branch, with a tail key.** Building on +the maximal pad inside `[end.key, head)`, appending the reported keys and one key `y` +above both the reported last and `begin.key` keeps the result compatible: the end +selector resolves onto `y`, so the complete window is still exactly the reported keys. -/ +theorem high_nonclamp_low_tail_compatible {r : RangeRead} {res : Result} + {pad : List Key} {y : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hpad : ∀ x ∈ pad, r.end_.key ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hps : pad.SortedLT) + (hlen : pad.length = negCrossHighD r res) + (hys : ∀ x ∈ res.keys, x < y) + (hyB : r.begin.key < ((y : Key) : WithTop Key)) : + ∃ hs : (pad ++ res.keys ++ [y]).SortedLT, + RangeRead.Compatible ⟨pad ++ res.keys ++ [y], hs⟩ r res := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread0 : Readable r res := ⟨db₀, hcomp₀⟩ + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hwl : RangeRead.withinLimit r res.keys := RangeRead.compatible_withinLimit hcomp₀ + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set D : ℕ := e - t with hD + have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD] + have hlenD : pad.length = D := by rw [hlen, hDdef] + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hcrossH : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD + have hHy : H < y := hys H hHmem + have hcrosspadres : ∀ x ∈ pad, ∀ z ∈ res.keys, x < z := by + intro x hx z hz + have hxH : x < H := WithTop.coe_lt_coe.mp (hpad x hx).2 + have hHz : ((H : Key) : WithTop Key) ≤ ((z : Key) : WithTop Key) := by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hz) + exact lt_of_lt_of_le hxH (WithTop.coe_le_coe.mp hHz) + have hcrossPR_y : ∀ x ∈ pad ++ res.keys, x < y := by + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_trans (WithTop.coe_lt_coe.mp (hpad x hx).2) hHy + · exact hys x hx + have hs : (pad ++ res.keys ++ [y]).SortedLT := by + rw [List.sortedLT_append] + refine ⟨?_, sortedLT_singleton y, ?_⟩ + · rw [List.sortedLT_append] + exact ⟨hps, hsort, hcrosspadres⟩ + · intro x hx z hz + rw [List.mem_singleton] at hz + subst hz + exact hcrossPR_y x hx + let db : DB := ⟨pad ++ res.keys ++ [y], hs⟩ + have hkeys : db.keys = pad ++ res.keys ++ [y] := rfl + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ hread0 hrev hmore hne hbegin hcrossD hnonclamp + -- begin resolution via the filter length + have hHmem_db : H ∈ db.keys := by + rw [hkeys] + exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hHmem))) + have hfilterHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + rw [hkeys, List.append_assoc, List.filter_append, List.filter_append] + have hpadf : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.1 (not_le.mpr (by rw [hH]; exact (hpad x hx).2)) + have hresf : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply List.filter_congr + intro k hk + have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk) + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hHk, h⟩⟩ + have hyf : [y].filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.2 (not_lt.mpr (le_of_lt hyB)) + rw [hpadf, hresf, hyf] + simp only [List.nil_append, List.append_nil] + rw [reportedBelowBegin, List.countP_eq_length_filter] at hr + exact hr + have hRbeg : resolve db r.begin = ((H : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmem_db hcrossH hfilterHB + -- end resolution: all keys are at or above `E` + have hge : ∀ x ∈ db.keys, E ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [hkeys, List.mem_append] at hx + rcases hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact (hpad x hx).1 + · have hHx : ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx) + exact le_of_lt (lt_of_lt_of_le hEH' hHx) + · rw [List.mem_singleton] at hx + subst hx + exact le_of_lt (lt_trans hEH' (WithTop.coe_lt_coe.mpr hHy)) + have hcntE : db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq, ← hE] + exact not_lt.mpr (hge x hx) + have hepos : 1 ≤ r.end_.offset := by omega + have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow) + have hlenDB : db.keys.length = D + t + 1 := by + rw [hkeys] + simp only [List.length_append, List.length_singleton] + rw [hlenD, ← ht] + have hanchorE : (r.end_.anchor db).toNat = e := by + rw [anchor_toNat_eq (s := r.end_) hepos, hcntE, Nat.zero_add, he] + have hgetE : db.keys[e]? = some y := by + rw [hkeys, List.append_assoc] + rw [List.getElem?_append_right (by rw [hlenD]; omega)] + have heD : e - pad.length = t := by rw [hlenD]; omega + rw [heD] + rw [List.getElem?_append_right (by rw [ht])] + simp [ht] + have hRend : resolve db r.end_ = ((y : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db := by + have hepos' : 0 < e := by omega + omega + rw [resolve, ite_eq_right (not_lt.mpr h0), hanchorE, hgetE] + -- complete window + have hcomplete : res.keys = RangeRead.completeKeys db r := by + rw [RangeRead.completeKeys, hrev] + simp only [Bool.false_eq_true, ite_false] + rw [RangeRead.inRange, hRbeg, hRend] + rw [hkeys, List.filter_append, List.filter_append] + have hpadf : pad.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((y : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.1 (not_le.mpr (by rw [hH]; exact (hpad x hx).2)) + have hresf : res.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((y : Key) : WithTop Key))) = + res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx), + WithTop.coe_lt_coe.mpr (hys x hx)⟩ + have hyf : [y].filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((y : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.2 (lt_irrefl _) + rw [hpadf, hyf] + simp only [List.nil_append, List.append_nil] + rw [hresf] + exact ⟨hs, hwl, Or.inr ⟨hmore, hcomplete⟩⟩ + +/-- In the nonclamping low branch with a full gap and a finite begin key, gap keys are +necessary: a database filling the gap and carrying a tail key above the reported last is +compatible, but erasing the gap key makes the end selector overshoot and exposes the +tail key in the complete window. -/ +theorem necessary_gap_nonclamp_low_eq_cap {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hkE : r.end_.key ≤ ((k : Key) : WithTop Key)) + (hkH : ((k : Key) : WithTop Key) < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hknot : k ∉ res.keys) + (hbeginTop : r.begin.key < (⊤ : WithTop Key)) + (hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread0 : Readable r res := ⟨db₀, hcomp₀⟩ + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set D : ℕ := e - t with hD + have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD] + have hDpos : 1 ≤ D := by rw [hD]; omega + have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow) + have hcapD : ((D : ℕ) : ℕ∞) = intervalCard E ((H : Key) : WithTop Key) := by + have h := hcap + rw [hDdef] at h + simpa [negCrossHighGapCap, hE, hH] using h + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hHleL : H ≤ res.keys.getLastD ([] : Key) := by + rw [hH] + exact le_getLastD_of_pairwise (List.sortedLT_iff_pairwise.mp hsort) (headD_mem hne) + -- a pad of `D - 1` keys in `[end.key, head)` avoiding `k` + obtain ⟨padkDb, hpadkIn, hkpadk, hpadklen⟩ := + exists_db_keysIn_avoid (B := E) (E := ((H : Key) : WithTop Key)) (k := k) (m := D - 1) + (by + have hsub : D - 1 + 1 = D := Nat.sub_add_cancel hDpos + have hcast : (((D - 1 : ℕ)) : ℕ∞) + 1 = (D : ℕ∞) := by + have : (((D - 1 : ℕ)) : ℕ∞) + 1 = (((D - 1 + 1 : ℕ)) : ℕ∞) := by + rw [Nat.cast_add, Nat.cast_one] + rw [this, hsub] + rw [hcast] + exact le_of_eq hcapD) + set padk : List Key := padkDb.keys with hpadkdef + have hpadkIn' : ∀ x ∈ padk, E ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by + intro x hx + simpa only [padk, keysIn, Set.mem_ofPred_eq] using hpadkIn x hx + have hpadks : padk.SortedLT := padkDb.sorted + have hpadklen' : padk.length = D - 1 := hpadklen + -- insert `k` into the pad + set padDb : DB := (⟨padk, hpadks⟩ : DB).insert k hkpadk with hpadDb + have hpadDbkeys : padDb.keys = padk.orderedInsert (· ≤ ·) k := rfl + set pad : List Key := padDb.keys with hpaddef + have hps : pad.SortedLT := padDb.sorted + have hpadIn : ∀ x ∈ pad, E ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := by + intro x hx + rw [hpadDbkeys, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact ⟨by rw [hE]; exact hkE, by rw [hH]; exact hkH⟩ + · exact hpadkIn' x hx + have hpadlen : pad.length = D := by + rw [hpadDbkeys, List.orderedInsert_length, hpadklen'] + omega + have hkpad : k ∈ pad := by + rw [hpadDbkeys, List.mem_orderedInsert] + exact Or.inl rfl + -- a tail key above the reported last and the begin key + set L : Key := res.keys.getLastD ([] : Key) with hL + have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne + have hLle : ∀ x ∈ res.keys, x ≤ L := by + intro x hx + rw [hL] + exact le_getLastD_of_pairwise (List.sortedLT_iff_pairwise.mp hsort) hx + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = ((b : Key) : WithTop Key) := by + cases hbk : r.begin.key with + | top => rw [hbk] at hbeginTop; exact absurd hbeginTop (lt_irrefl _) + | coe b => exact ⟨b, rfl⟩ + set m : Key := if L ≤ b then b else L with hm + have hLm : L ≤ m := by + rw [hm] + by_cases h : L ≤ b + · rw [if_pos h]; exact h + · rw [if_neg h] + have hbm : b ≤ m := by + rw [hm] + by_cases h : L ≤ b + · rw [if_pos h] + · rw [if_neg h]; exact le_of_lt (lt_of_not_ge h) + set y : Key := keyAfter m with hy + have hLy : L < y := lt_of_le_of_lt hLm (by rw [hy]; exact keyAfter_self_lt m) + have hby : b < y := lt_of_le_of_lt hbm (by rw [hy]; exact keyAfter_self_lt m) + have hys : ∀ x ∈ res.keys, x < y := fun x hx => lt_of_le_of_lt (hLle x hx) hLy + have hyB : r.begin.key < ((y : Key) : WithTop Key) := by + rw [hb]; exact WithTop.coe_lt_coe.mpr hby + have hk_ne_y : k ≠ y := by + intro hky + subst hky + have h1 : y < H := WithTop.coe_lt_coe.mp hkH + exact absurd (lt_trans (lt_of_lt_of_le h1 hHleL) hLy) (lt_irrefl y) + -- the compatible witness with the full pad and the tail + obtain ⟨hs0, hcomp0⟩ := + high_nonclamp_low_tail_compatible (pad := pad) (y := y) + hread0 hrev hmore hne hbegin hEH hcrossD hnonclamp hlow hpadIn hps hpadlen hys hyB + set db0 : DB := ⟨pad ++ res.keys ++ [y], hs0⟩ with hdb0 + have hk0 : k ∈ db0.keys := by + rw [hdb0] + exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl hkpad))) + refine necessary_of_toggle ⟨db0, hcomp0, ?_⟩ + rw [toggle_eq_erase_of_mem hk0] + -- the erased database's keys + have hks0 : db0.keys = pad ++ res.keys ++ [y] := rfl + have hkrest : k ∉ res.keys ++ [y] := by + rw [List.mem_append, List.mem_singleton] + push_neg + exact ⟨hknot, hk_ne_y⟩ + have hpadErase : pad.erase k = padk := by + have h := DB.erase_insert_keys (⟨padk, hpadks⟩ : DB) k hkpadk + rw [← hpadDb, DB.erase_keys, ← hpaddef] at h + simpa using h + have herase0 : (pad ++ res.keys ++ [y]).erase k = padk ++ res.keys ++ [y] := by + rw [List.erase_append_left (l₁ := pad ++ res.keys) [y] + (List.mem_append.mpr (Or.inl hkpad))] + rw [List.erase_append_left (l₁ := pad) res.keys hkpad] + rw [hpadErase] + have hkeysErase : (db0.erase k).keys = padk ++ res.keys ++ [y] := by + rw [DB.erase_keys, hks0] + exact herase0 + set dbe : DB := db0.erase k with hdbe + have hHmemE : H ∈ dbe.keys := by + rw [hkeysErase] + exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hHmem))) + have hcrossH : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ hread0 hrev hmore hne hbegin hcrossD hnonclamp + have hfilterHB : (dbe.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + rw [hkeysErase, List.filter_append, List.filter_append] + have hpadf : padk.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.1 (not_le.mpr (hpadkIn' x hx).2) + have hresf : res.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.filter (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) := by + apply List.filter_congr + intro x hx + have hHx : ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx) + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hHx, h⟩⟩ + have hyf : [y].filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon.2 (not_lt.mpr (le_of_lt hyB)) + rw [hpadf, hresf, hyf] + simp only [List.nil_append, List.append_nil] + rw [reportedBelowBegin, List.countP_eq_length_filter] at hr + exact hr + have hRbegE : resolve dbe r.begin = ((H : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len (head := H) hbegin hHmemE hcrossH hfilterHB + have hgeE : ∀ x ∈ dbe.keys, E ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [hkeysErase, List.mem_append] at hx + rcases hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact (hpadkIn' x hx).1 + · exact le_of_lt (high_reported_gt_end hsort hne hEH x hx) + · rw [List.mem_singleton] at hx + subst hx + exact le_of_lt (lt_trans hEH (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hHleL hLy))) + have hcntEE : dbe.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq, ← hE] + exact not_lt.mpr (hgeE x hx) + have hepos : 1 ≤ r.end_.offset := by omega + have hanchorE : (r.end_.anchor dbe).toNat = e := by + rw [anchor_toNat_eq (s := r.end_) hepos, hcntEE, Nat.zero_add, he] + have hlenE : dbe.keys.length = e := by + rw [hkeysErase, List.length_append, List.length_append, hpadklen'] + simp only [List.length_singleton] + omega + have htopE : resolve dbe r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top + · unfold Selector.anchor + rw [hcntEE] + have : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + simpa using this + · rw [hanchorE, hlenE] + have hbetweenE : between dbe ((H : Key) : WithTop Key) (⊤ : WithTop Key) = + res.keys ++ [y] := by + rw [between_top_eq_above] + unfold above + rw [hkeysErase, List.filter_append, List.filter_append] + have hpadf : padk.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hcon + exact absurd hcon (not_le.mpr (hpadkIn' x hx).2) + have hresf : res.keys.filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hH, hheadD] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx) + have hyf : [y].filter (fun x : Key => decide (((H : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key))) = [y] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr (le_trans hHleL (le_of_lt hLy)) + rw [hpadf, hresf, hyf] + simp + have hckE : RangeRead.completeKeys dbe r = res.keys ++ [y] := by + rw [completeKeys_eq_inRange_of_not_rev hrev, hRbegE, htopE] + simpa [RangeRead.inRange, between] using hbetweenE + intro hcompE + have hcontr : res.keys = res.keys ++ [y] := by + rcases hcompE.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · rw [hckE] at heq + exact heq + have hlen := congrArg List.length hcontr + rw [List.length_append, List.length_singleton] at hlen + omega + +/-- **The `begin.key = ⊤`, full-gap nonclamp low branch: compatibility is exactly +agreement of the keys at or above the reported head with the result.** Here the begin +selector, whose key is `⊤`, lands exactly on the reported head, so the reported keys are +the top `t` stored keys; the end selector always overshoots because the gap can hold at +most `D` keys. -/ +theorem high_nonclamp_low_aboveH_iff_top {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hbeginTop : r.begin.key = (⊤ : WithTop Key)) + (hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res) : + RangeRead.Compatible db r res ↔ + above db ((res.keys.headD ([] : Key) : Key) : WithTop Key) = res.keys := by + set H : Key := res.keys.headD ([] : Key) with hH + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set D : ℕ := e - t with hD + have hDdef : negCrossHighD r res = D := by unfold negCrossHighD; rw [← he, ← ht, ← hD] + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hEB : E ≤ ((H : Key) : WithTop Key) := le_of_lt hEH' + have hcrossH : ((H : Key) : WithTop Key) < r.begin.key := by rw [hH]; exact hcrossD + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hrb : reportedBelowBegin r res = t := by + unfold reportedBelowBegin; rw [hbeginTop, ht]; exact countP_lt_top + have hr : reportedBelowBegin r res = beginSlack r + 1 := + high_reportedBelowBegin_eq_slack_succ hread hrev hmore hne hbegin hcrossH hnonclamp + have hq : beginSlack r + 1 = t := by omega + have hcapD : ((D : ℕ) : ℕ∞) = intervalCard E ((H : Key) : WithTop Key) := by + have h := hcap + rw [hDdef] at h + simpa [negCrossHighGapCap, hE, hH] using h + have hDt : D + t = e := by rw [hD]; exact Nat.sub_add_cancel (le_of_lt hlow) + constructor + · intro hcomp + -- the begin resolution lands on the head, so the top `t` stored keys are reported + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by + rw [hH, hheadD] + exact resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + have htlen : t ≤ db.keys.length := by rw [ht]; exact hsub.length_le + have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.length := by + rw [hbeginTop]; exact countP_lt_top + have hanchor_cast : r.begin.anchor db = + (db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - + (beginSlack r : ℤ) - 1 := anchor_eq_count_sub_slack db r (le_of_lt hbegin) + have hcB_ge : beginSlack r + 1 ≤ db.keys.length := by omega + have hanchor_ge : 0 ≤ r.begin.anchor db := by + rw [hanchor_cast, hcB] + have : (beginSlack r : ℤ) + 1 ≤ (db.keys.length : ℤ) := by exact_mod_cast hcB_ge + omega + have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro htop; rw [hL] at htop + exact absurd htop (WithTop.coe_ne_top (a := H)) + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + by_contra h + exact hnot_top (resolve_eq_top hanchor_ge (le_of_not_gt h)) + have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor_ge hltlen + have hidx : db.keys[(r.begin.anchor db).toNat] = H := by + have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) = + ((H : Key) : WithTop Key) := by rw [← hget, hL] + exact WithTop.coe_injective hcoe + have hrank : db.keys.countP (fun k : Key => decide (k < H)) = (r.begin.anchor db).toNat := + countP_lt_eq_of_getElem db.sorted hltlen hidx + have htoNat : (r.begin.anchor db).toNat = db.keys.length - t := by + have hz : r.begin.anchor db = ((db.keys.length : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by + rw [hanchor_cast, hcB]; push_cast; ring + have hz2 : ((db.keys.length : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) = + ((db.keys.length - (beginSlack r + 1) : ℕ) : ℤ) := by + rw [← Nat.cast_sub hcB_ge] + rw [hz, hz2, Int.toNat_natCast, hq] + have hcnt : cnt db ((H : Key) : WithTop Key) = db.keys.length - t := by + unfold cnt + rw [show (fun k : Key => decide ((↑k : WithTop Key) < ((H : Key) : WithTop Key))) = + (fun k : Key => decide (k < H)) from by + funext k; rw [decide_eq_decide]; exact WithTop.coe_lt_coe] + rw [htoNat] at hrank + exact hrank + have haboveLen : (above db ((H : Key) : WithTop Key)).length = t := by + have h := cnt_add_above_length db ((H : Key) : WithTop Key) + rw [hcnt] at h + omega + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL]; rfl + have hfilterPrefix : res.keys = (above db ((H : Key) : WithTop Key)).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + rw [hwin, between_eq_filter_above] + have hfilterLen : ((above db ((H : Key) : WithTop Key)).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_))).length = t := by + rw [← hfilterPrefix, ht] + have hfilterAll : (above db ((H : Key) : WithTop Key)).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = + above db ((H : Key) : WithTop Key) := + List.Sublist.eq_of_length List.filter_sublist (by rw [hfilterLen, haboveLen]) + exact hfilterAll.symm.trans hfilterPrefix.symm + · intro habove + have hsort : res.keys.SortedLT := by + obtain ⟨db0, hcomp0⟩ := hread + exact RangeRead.compatible_sortedLT hcomp0 hrev + have hwl : RangeRead.withinLimit r res.keys := by + obtain ⟨db0, hcomp0⟩ := hread + exact RangeRead.compatible_withinLimit hcomp0 + -- the begin selector resolves onto the head + have hHabove : H ∈ above db ((H : Key) : WithTop Key) := by rw [habove]; exact hHmem + have hHdb : H ∈ db.keys := (mem_above.mp hHabove).1 + have hfilterHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + have h1 : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + above db ((H : Key) : WithTop Key) := by + rw [above_eq_filter, hbeginTop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact and_iff_left (WithTop.coe_lt_top k) + rw [h1, habove, ← ht, hq] + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len (head := H) hbegin hHdb hcrossH hfilterHB + -- the end selector always overshoots + have hbetweenLen : (between db E ((H : Key) : WithTop Key)).length ≤ D := by + have hcard : (((between db E ((H : Key) : WithTop Key)).length : ℕ) : ℕ∞) ≤ + intervalCard E ((H : Key) : WithTop Key) := between_length_le_intervalCard hEB + have : (((between db E ((H : Key) : WithTop Key)).length : ℕ) : ℕ∞) ≤ (D : ℕ∞) := by + rw [hcapD]; exact hcard + exact_mod_cast this + have hlenEnd : db.keys.length ≤ cnt db E + e := by + have h1 : cnt db ((H : Key) : WithTop Key) = cnt db E + + (between db E ((H : Key) : WithTop Key)).length := cnt_eq_add_between db hEB + have h2 : cnt db ((H : Key) : WithTop Key) + + (above db ((H : Key) : WithTop Key)).length = db.keys.length := + cnt_add_above_length db ((H : Key) : WithTop Key) + have h3 : (above db ((H : Key) : WithTop Key)).length = t := by rw [habove, ht] + rw [h3] at h2 + omega + have htop : resolve db r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top + · unfold Selector.anchor + have h1 : (0 : ℤ) ≤ r.end_.offset - 1 := by omega + have h2 : (0 : ℤ) ≤ (db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ) := by + exact_mod_cast Nat.zero_le _ + linarith + · rw [anchor_toNat_eq (s := r.end_) (by omega), ← he] + exact hlenEnd + refine ⟨hwl, Or.inr ⟨hmore, ?_⟩⟩ + rw [RangeRead.completeKeys, hrev] + simp only [Bool.false_eq_true, if_false] + rw [hL, htop] + show res.keys = between db ((H : Key) : WithTop Key) (⊤ : WithTop Key) + rw [between_top_eq_above] + exact habove.symm + +/-- Every key at or above `end.key` is necessary in the nonclamping low branch of the +crossed negative-begin complete high cell when `end.key` is the chosen lower endpoint. -/ +theorem necessary_topRange_endKey_nonclamp_low {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (huses : negCrossHighUsesEnd r res) + (hkE : r.end_.key ≤ ((k : Key) : WithTop Key)) : + Necessary r res k := by + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hkH : ((k : Key) : WithTop Key) < + ((res.keys.headD ([] : Key) : Key) : WithTop Key) + · by_cases hlt : ((negCrossHighD r res : ℕ) : ℕ∞) < negCrossHighGapCap r res + · exact necessary_gap_nonclamp_low_lt_cap hread hrev hmore hne hbegin hEH hcrossD + hnonclamp hlow hkE hkH hkres hlt + · have hsecond : (((negCrossHighD r res : ℕ) : ℕ∞) ≤ negCrossHighGapCap r res) ∧ + r.begin.key < (⊤ : WithTop Key) := by + rcases huses with h | h + · exact absurd h hlt + · exact h + have hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res := + le_antisymm hsecond.1 (le_of_not_gt hlt) + exact necessary_gap_nonclamp_low_eq_cap hread hrev hmore hne hbegin hEH hcrossD + hnonclamp hlow hkE hkH hkres hsecond.2 hcap + · have hkHle : ((res.keys.headD ([] : Key) : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) := le_of_not_gt hkH + exact necessary_above_head_nonclamp_low hread hrev hmore hne hbegin hEH hcrossD + hnonclamp hlow hkHle hkres + +/-- **The `[end.key, ⊤)` branch of the nonclamping low crossed negative-begin complete +high cell is minimal** when the gap can absorb the excess and the begin key admits a key +above it. -/ +theorem minimal_nonclampLow_usesEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (huses : negCrossHighUsesEnd r res) : + Minimal r res ⟨r.end_.key, (⊤ : WithTop Key)⟩ := by + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_topRange_endKey_nonclamp_low hread hrev hmore hne hbegin hEH hcrossD + hnonclamp hend + · intro k hk + exact necessary_topRange_endKey_nonclamp_low hread hrev hmore hne hbegin hend hEH + hcrossD hnonclamp hlow huses (ConflictRange.mem_iff.mp hk).1 + +/-- **The `[head, ⊤)` branch of the nonclamping low crossed negative-begin complete high +cell is minimal** when the gap is too small or the begin key is `⊤`. -/ +theorem minimal_nonclampLow_notUsesEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) + (hlow : res.keys.length < (r.end_.offset - 1).toNat) + (hnot : ¬ negCrossHighUsesEnd r res) : + Minimal r res ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + by_cases hle : ((negCrossHighD r res : ℕ) : ℕ∞) ≤ negCrossHighGapCap r res + · have htopB : ¬ (r.begin.key < (⊤ : WithTop Key)) := by + intro hB + exact hnot (Or.inr ⟨hle, hB⟩) + have hBtop : r.begin.key = (⊤ : WithTop Key) := le_antisymm le_top (le_of_not_gt htopB) + have hcap : ((negCrossHighD r res : ℕ) : ℕ∞) = negCrossHighGapCap r res := by + rcases lt_or_ge ((negCrossHighD r res : ℕ) : ℕ∞) (negCrossHighGapCap r res) with hlt | hge + · exact absurd (Or.inl hlt) hnot + · exact le_antisymm hle hge + refine minimal_of_compatible_iff_filter hread (fun db => ?_) + rw [high_nonclamp_low_aboveH_iff_top hread hrev hmore hne hbegin hend hEH hcrossD + hnonclamp hlow hBtop hcap, filter_le_top_eq_above_aux] + · exact minimal_nonclampLow_noCap hread hrev hmore hne hbegin hend hEH hcrossD + hnonclamp hlow hle + +/-- **The nonclamping low-offset branch of the crossed negative-begin complete high cell +is minimal** for the total closed form. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh_nonclampLow {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighNonclampLow r res) : + Minimal r res (closedFormNegCrossCompleteHigh r res) := by + obtain ⟨⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩, hnonclamp, hlow⟩ := hcell + unfold closedFormNegCrossCompleteHigh + rw [ite_eq_right (not_le.mpr hnonclamp)] + rw [ite_eq_right (ne_of_lt hlow)] + by_cases huses : negCrossHighUsesEnd r res + · rw [ite_eq_left huses] + exact minimal_nonclampLow_usesEnd hread hrev hmore hne hbegin hend2 hEH hcrossD + hnonclamp hlow huses + · rw [ite_eq_right huses] + exact minimal_nonclampLow_notUsesEnd hread hrev hmore hne hbegin hend2 hEH hcrossD + hnonclamp hlow huses + +/-- A small closed-form region of the crossed negative-begin complete high cell: the +clamping low branch, the nonclamping finite branch and the nonclamping low branch. -/ +abbrev negCrossCompleteHighSmall (r : RangeRead) (res : Result) : Prop := + negCrossCompleteHighClampLow r res ∨ negCrossCompleteHighNonclampFinite r res ∨ + negCrossCompleteHighNonclampLow r res + +/-- **The small closed-form region of the crossed negative-begin complete high cell is +minimal** for the total closed form. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh_small {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighSmall r res) : + Minimal r res (closedFormNegCrossCompleteHigh r res) := by + rcases hcell with h | h | h + · exact minimal_closedForm_of_negCrossCompleteHigh_clampLow hread h + · exact minimal_closedForm_of_negCrossCompleteHigh_nonclampFinite hread h + · exact minimal_closedForm_of_negCrossCompleteHigh_nonclampLow hread h + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossCompleteResidual.lean b/FormalConflictRange/NegCrossCompleteResidual.lean new file mode 100644 index 0000000..71e5480 --- /dev/null +++ b/FormalConflictRange/NegCrossCompleteResidual.lean @@ -0,0 +1,475 @@ +/- +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.NegCrossComplete +import FormalConflictRange.NegCrossClosedForm +import FormalConflictRange.NonposRevClosedForm + +/-! +# The crossed negative-begin complete non-overshoot residual + +This module closes the remaining crossed negative-begin complete cells: those with +`2 ≤ end.offset`, `begin.key` strictly above the reported head, and at least one +reported key at or above `end.key`. + +The key observation is that the end selector can be shifted to the immediate successor +of the last reported key without changing any compatible database. If `L` is the last +reported key and `t` is the number of reported keys at or above `end.key`, then the +original end anchor is `cnt end.key + end.offset - 1`, while the anchor of +`⟨keyAfter L, end.offset - t⟩` is `cnt (keyAfter L) + end.offset - t - 1`. Because the +half-open gap `[end.key, keyAfter L)` contains only reported keys in every compatible +database, `cnt (keyAfter L) = cnt end.key + t`, so the two anchors agree. The reduced +read has all reported keys strictly below its end key; it is therefore a complete +overshoot (`2 ≤ end.offset - t`) or an offset-one cell. Minimality transfers between +the two reads directly, since their compatible database sets coincide. +-/ + +namespace FormalConflictRange + +/-- The number of reported keys at or above the end selector key. -/ +def reportedAtOrAboveEnd (r : RangeRead) (res : Result) : ℕ := + res.keys.countP (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key))) + +/-- The read whose end selector is shifted to the successor of the last reported key. +The offset is reduced by the number of reported keys at or above `end.key`, so the end +anchor (and hence every resolution) is unchanged on compatible databases. -/ +noncomputable def completeResidualReduced (r : RangeRead) (res : Result) : RangeRead := + { r with + end_ := + { key := ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) + offset := r.end_.offset - (reportedAtOrAboveEnd r res : ℤ) } } + +/-- **The gap `[end.key, keyAfter L)` contains exactly the reported keys at or above +`end.key`.** The result keys are the stored keys of the half-open window, `L` is the +last reported key, and `end.key` lies between the window's lower bound and `L`. Any +stored key strictly above `L` but below the window's upper bound would be reported and +contradict `L` being last. -/ +theorem between_end_keyAfter_length_eq_reportedAtOrAboveEnd + {db : DB} {res : Result} {lo hi : WithTop Key} {E : WithTop Key} + (hne : res.keys ≠ []) + (hkeys : res.keys = RangeRead.inRange db.keys lo hi) + (hloE : lo ≤ E) + (_hEL : E ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (_hLhi : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < hi) : + (between db E ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)).length = + res.keys.countP (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key))) := by + set L : Key := res.keys.getLastD ([] : Key) with hL + have hLget : L = res.keys.getLast hne := by + rw [hL] + exact getLastD_eq_getLast_of_ne hne + have hLmem : L ∈ res.keys := by rw [hLget]; exact List.getLast_mem hne + have hpair : res.keys.Pairwise (· < ·) := hkeys.symm ▸ RangeRead.inRange_pairwise db lo hi + have hmax : ∀ x ∈ res.keys, x ≤ L := fun x hx => le_getLastD_of_pairwise hpair hx + have hLin : L ∈ RangeRead.inRange db.keys lo hi := hkeys.symm ▸ hLmem + have hLin' : L ∈ db.keys ∧ lo ≤ (L : WithTop Key) ∧ (L : WithTop Key) < hi := by + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hLin + have hLstored : L ∈ db.keys := hLin'.1 + -- No stored key lies strictly between `L` and the window's upper bound. + have hno_above : ∀ x ∈ db.keys, L < x → hi ≤ (x : WithTop Key) := by + intro x hxdb hxL + by_contra hxhi + have hxhi' : (x : WithTop Key) < hi := lt_of_not_ge hxhi + have hxin : x ∈ RangeRead.inRange db.keys lo hi := by + simp only [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] + exact ⟨hxdb, le_trans hLin'.2.1 (WithTop.coe_le_coe.mpr (le_of_lt hxL)), hxhi'⟩ + have hxmem : x ∈ res.keys := hkeys.symm ▸ hxin + exact absurd (hmax x hxmem) (not_le.mpr hxL) + -- The two filters agree: both select the stored keys in `[E, L]`. + have hflt : (db.keys.filter (fun k : Key => + decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((keyAfter L : Key) : WithTop Key)))) = + (db.keys.filter (fun k : Key => + decide (E ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < hi))) := by + apply List.filter_congr + intro x hxdb + rw [decide_eq_decide] + constructor + · rintro ⟨hEx, hxka⟩ + refine ⟨hEx, lt_of_le_of_lt ?_ hLin'.2.2⟩ + exact WithTop.coe_le_coe.mpr ((lt_keyAfter_iff x L).mp (WithTop.coe_lt_coe.mp hxka)) + · rintro ⟨hEx, hxhi⟩ + refine ⟨hEx, ?_⟩ + by_contra hcon + have hxge : ((keyAfter L : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + le_of_not_gt hcon + have hxL : L < x := + lt_of_lt_of_le (keyAfter_self_lt L) (WithTop.coe_le_coe.mp hxge) + exact absurd hxhi (not_lt.mpr (hno_above x hxdb hxL)) + -- Convert to the reported-key count. + rw [show (between db E ((keyAfter L : Key) : WithTop Key)) = + db.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((keyAfter L : Key) : WithTop Key))) by + rfl] + rw [hflt] + -- The reported keys at or above `E` are exactly the stored keys in `[E, hi)`. + have hreported : res.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key))) = + RangeRead.inRange db.keys E hi := by + rw [hkeys] + unfold RangeRead.inRange + rw [List.filter_filter] + apply List.filter_congr + intro x _ + by_cases hE : E ≤ ((x : Key) : WithTop Key) + · by_cases hhi : ((x : Key) : WithTop Key) < hi + · have hlo : lo ≤ ((x : Key) : WithTop Key) := le_trans hloE hE + simp [hE, hhi, hlo] + · simp [hE, hhi] + · simp [hE] + have hlen : (db.keys.filter (fun k : Key => decide (E ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < hi))) = + RangeRead.inRange db.keys E hi := rfl + rw [hlen, ← hreported, List.countP_eq_length_filter] + +/-- The count identity behind the reduction: the number of stored keys below the +successor of the last reported key is the count below `end.key` plus the number of +reported keys at or above `end.key`. -/ +theorem cnt_keyAfter_eq_cnt_end_add + {db : DB} {r : RangeRead} {res : Result} {lo hi : WithTop Key} + (hne : res.keys ≠ []) + (hkeys : res.keys = RangeRead.inRange db.keys lo hi) + (hloE : lo ≤ r.end_.key) + (hEL : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hLhi : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < hi) : + cnt db ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) = + cnt db r.end_.key + reportedAtOrAboveEnd r res := by + have hEL' : r.end_.key ≤ ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) := + le_trans hEL (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt _))) + rw [cnt_eq_add_between db hEL', + between_end_keyAfter_length_eq_reportedAtOrAboveEnd hne hkeys hloE hEL hLhi] + rfl + +/-- The shifted end selector has the same anchor as the original on any compatible +complete result. -/ +theorem reduced_end_anchor_eq + {db : DB} {r : RangeRead} {res : Result} {lo hi : WithTop Key} + (hne : res.keys ≠ []) + (hkeys : res.keys = RangeRead.inRange db.keys lo hi) + (hloE : lo ≤ r.end_.key) + (hEL : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hLhi : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < hi) : + (completeResidualReduced r res).end_.anchor db = r.end_.anchor db := by + have hcnt := cnt_keyAfter_eq_cnt_end_add hne hkeys hloE hEL hLhi + change (cnt db ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) : ℤ) + + (r.end_.offset - (reportedAtOrAboveEnd r res : ℤ)) - 1 = + (cnt db r.end_.key : ℤ) + r.end_.offset - 1 + rw [hcnt] + push_cast + ring + +/-- The complete keys of a forward read are the stored keys in the resolved window. -/ +theorem completeKeys_eq_inRange_of_not_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = false) : + RangeRead.completeKeys db r = + RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + unfold RangeRead.completeKeys + rw [ite_eq_right (by simp [hrev])] + +/-- **The reduction preserves compatible databases.** Under the complete-result +hypotheses and the cell condition that the resolved begin is at or below `end.key` +(which follows from `head ≤ end.key`), shifting the end selector to +`⟨keyAfter (last reported), end.offset - t⟩` leaves the set of compatible databases +unchanged. -/ +theorem compatible_reduced_iff + {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hheadE : ((res.keys.headD ([] : Key) : Key) : WithTop Key) ≤ r.end_.key) + (hEL : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (completeResidualReduced r res) res := by + have hwithin : RangeRead.withinLimit r res.keys ↔ + RangeRead.withinLimit (completeResidualReduced r res) res.keys := by + simp [RangeRead.withinLimit, completeResidualReduced] + have hrev' : (completeResidualReduced r res).reverse = false := hrev + constructor + · intro h + have heq : res.keys = RangeRead.completeKeys db r := by + rcases h.2 with ⟨hm, -, -⟩ | ⟨-, he⟩ + · simp [hmore] at hm + · exact he + have hkeys : res.keys = RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := + heq.trans (completeKeys_eq_inRange_of_not_rev hrev) + have hloE : resolve db r.begin ≤ r.end_.key := by + have hheadmem : res.keys.headD ([] : Key) ∈ res.keys := headD_mem hne + have hheadin : res.keys.headD ([] : Key) ∈ + RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [← hkeys]; exact hheadmem + have hheadin' : (res.keys.headD ([] : Key)) ∈ db.keys ∧ + resolve db r.begin ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < resolve db r.end_ := by + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hheadin + exact le_trans hheadin'.2.1 hheadE + have hLhi : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < + resolve db r.end_ := by + have hLin : (res.keys.getLastD ([] : Key)) ∈ + RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [← hkeys]; exact getLastD_mem hne + have hLin' : (res.keys.getLastD ([] : Key)) ∈ db.keys ∧ + resolve db r.begin ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < resolve db r.end_ := by + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hLin + exact hLin'.2.2 + have hanc := reduced_end_anchor_eq (db := db) (r := r) (res := res) + (lo := resolve db r.begin) (hi := resolve db r.end_) hne hkeys hloE hEL hLhi + have hres_eq : resolve db (completeResidualReduced r res).end_ = resolve db r.end_ := by + unfold resolve + rw [hanc] + refine ⟨hwithin.mp h.1, Or.inr ⟨hmore, ?_⟩⟩ + rw [heq, completeKeys_eq_inRange_of_not_rev hrev, + completeKeys_eq_inRange_of_not_rev hrev', hres_eq] + simp only [completeResidualReduced] + · intro h + have heq : res.keys = RangeRead.completeKeys db (completeResidualReduced r res) := by + rcases h.2 with ⟨hm, -, -⟩ | ⟨-, he⟩ + · simp [hmore] at hm + · exact he + have hkeys : res.keys = RangeRead.inRange db.keys (resolve db r.begin) + (resolve db (completeResidualReduced r res).end_) := + heq.trans (completeKeys_eq_inRange_of_not_rev hrev') + have hloE : resolve db r.begin ≤ r.end_.key := by + have hheadmem : res.keys.headD ([] : Key) ∈ res.keys := headD_mem hne + have hheadin : res.keys.headD ([] : Key) ∈ + RangeRead.inRange db.keys (resolve db r.begin) + (resolve db (completeResidualReduced r res).end_) := by + rw [← hkeys]; exact hheadmem + have hheadin' : (res.keys.headD ([] : Key)) ∈ db.keys ∧ + resolve db r.begin ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < + resolve db (completeResidualReduced r res).end_ := by + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hheadin + exact le_trans hheadin'.2.1 hheadE + have hLhi : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < + resolve db (completeResidualReduced r res).end_ := by + have hLin : (res.keys.getLastD ([] : Key)) ∈ + RangeRead.inRange db.keys (resolve db r.begin) + (resolve db (completeResidualReduced r res).end_) := by + rw [← hkeys]; exact getLastD_mem hne + have hLin' : (res.keys.getLastD ([] : Key)) ∈ db.keys ∧ + resolve db r.begin ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < + resolve db (completeResidualReduced r res).end_ := by + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hLin + exact hLin'.2.2 + have hanc := reduced_end_anchor_eq (db := db) (r := r) (res := res) + (lo := resolve db r.begin) (hi := resolve db (completeResidualReduced r res).end_) + hne hkeys hloE hEL hLhi + have hres_eq : resolve db r.end_ = resolve db (completeResidualReduced r res).end_ := by + unfold resolve + rw [hanc] + refine ⟨hwithin.mpr h.1, Or.inr ⟨hmore, ?_⟩⟩ + rw [heq, completeKeys_eq_inRange_of_not_rev hrev', + completeKeys_eq_inRange_of_not_rev hrev, hres_eq] + simp only [completeResidualReduced] + +/-- The number of stored keys below a resolution is at most its clamped anchor index. -/ +theorem cnt_resolve_le_anchor_toNat {db : DB} {s : Selector} (h0 : 0 ≤ s.anchor db) : + cnt db (resolve db s) ≤ (s.anchor db).toNat := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := s) with hnil | htop | ⟨k, hkdb, hkeq⟩ + · rw [hnil, cnt_nil]; omega + · rw [htop, cnt_top] + have hlen : db.keys.length ≤ (s.anchor db).toNat := by + by_contra hcon + have hlt : (s.anchor db).toNat < db.keys.length := lt_of_not_ge hcon + rw [resolve_eq_getElem h0 hlt] at htop + exact absurd htop (by simp) + exact hlen + · rw [hkeq] + have hlt : (s.anchor db).toNat < db.keys.length := by + by_contra hcon + exact absurd (resolve_eq_top h0 (le_of_not_gt hcon) ▸ hkeq : (⊤ : WithTop Key) = ↑k) (by simp) + have hget : db.keys[(s.anchor db).toNat] = k := by + have := resolve_eq_getElem (db := db) (s := s) h0 hlt + rw [hkeq] at this + exact WithTop.coe_inj.mp this.symm + rw [cnt_coe, countP_lt_eq_of_getElem db.sorted hlt hget] + +/-- **The reduced end offset is positive.** The last reported key lies strictly below +the resolved end, so the successor of the last reported key is at most the resolved +end; hence the number of stored keys below the successor is at most the end anchor, +which is `cnt end.key + end.offset - 1`. The count identity then gives +`end.offset - t ≥ 1`. -/ +theorem endOffset_sub_reportedAtOrAboveEnd_ge_one + {db : DB} {r : RangeRead} {res : Result} {lo hi : WithTop Key} + (hne : res.keys ≠ []) + (hkeys : res.keys = RangeRead.inRange db.keys lo hi) + (hloE : lo ≤ r.end_.key) + (hEL : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hLhi : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < hi) + (hhi : hi = resolve db r.end_) + (heo : 1 ≤ r.end_.offset) : + 1 ≤ r.end_.offset - (reportedAtOrAboveEnd r res : ℤ) := by + have hcnt := cnt_keyAfter_eq_cnt_end_add hne hkeys hloE hEL hLhi + have hkeyle : ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) ≤ + resolve db r.end_ := by + rw [← hhi] + exact keyAfter_le_of_lt hLhi + have hle1 : cnt db ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) ≤ + cnt db (resolve db r.end_) := cnt_le_cnt_of_le db hkeyle + have h0 : 0 ≤ r.end_.anchor db := by + unfold Selector.anchor + have : (0 : ℤ) ≤ (db.keys.countP (fun k : Key => + decide ((↑k : WithTop Key) < r.end_.key)) : ℤ) := Int.natCast_nonneg _ + omega + have hle2 : cnt db (resolve db r.end_) ≤ (r.end_.anchor db).toNat := + cnt_resolve_le_anchor_toNat h0 + have hanc : (r.end_.anchor db).toNat = + cnt db r.end_.key + (r.end_.offset - 1).toNat := + anchor_toNat_eq (s := r.end_) heo + have hcntle : reportedAtOrAboveEnd r res ≤ (r.end_.offset - 1).toNat := by + rw [hcnt] at hle1 + omega + have hcast : ((reportedAtOrAboveEnd r res : ℕ) : ℤ) ≤ r.end_.offset - 1 := by + have hd0 : 0 ≤ r.end_.offset - 1 := by omega + have h2 : ((r.end_.offset - 1).toNat : ℤ) = r.end_.offset - 1 := + Int.toNat_of_nonneg hd0 + exact le_trans (by exact_mod_cast hcntle) (le_of_eq h2) + omega + +/-- The forward crossed negative-begin complete residual non-overshoot cell: at least one +reported key is at or above `end.key`, but the reported head is at or below `end.key` and +the reported last is at or above it. -/ +abbrev negCrossCompleteResidual (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) ≤ r.end_.key ∧ + r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + +/-- The closed form for the residual: shift the end selector to the successor of the last +reported key, then read off the overshoot or offset-one rule of the shifted read. -/ +noncomputable def closedFormNegCrossCompleteResidual (r : RangeRead) (res : Result) : + ConflictRange := + let r' := completeResidualReduced r res + if 2 ≤ r'.end_.offset then closedFormNegCrossOvershoot r' res + else if reportedBelowBegin r' res ≤ beginSlack r' then closedFormNegCrossClampEndOne r' res + else if r.begin.key ≤ r'.end_.key then closedFormNegCrossEndOne r' res + else closedFormNegCrossEndOneLong r' res + +/-- The last reported key lies strictly below the resolved end of any compatible complete +forward database, and every reported key is at or below it. -/ +theorem complete_residual_last_facts {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hcomp : RangeRead.Compatible db r res) : + res.keys = RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < resolve db r.end_ ∧ + List.Pairwise (· < ·) res.keys := by + have heq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, he⟩ + · simp [hmore] at hm + · exact he + have hkeys : res.keys = RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := + heq.trans (completeKeys_eq_inRange_of_not_rev hrev) + refine ⟨hkeys, ?_, ?_⟩ + · have hLin : (res.keys.getLastD ([] : Key)) ∈ + RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [← hkeys]; exact getLastD_mem hne + have hLin' : (res.keys.getLastD ([] : Key)) ∈ db.keys ∧ + resolve db r.begin ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < resolve db r.end_ := by + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hLin + exact hLin'.2.2 + · exact hkeys.symm ▸ RangeRead.inRange_pairwise db _ _ + +/-- **The crossed negative-begin complete residual non-overshoot cell is minimal.** -/ +theorem minimal_closedForm_of_negCross_complete_residual {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteResidual r res) : + Minimal r res (closedFormNegCrossCompleteResidual r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hheadE, hEL⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + obtain ⟨hkeys₀, hlast_lt, hpair₀⟩ := complete_residual_last_facts hrev hmore hne hcomp₀ + have hheadmem : res.keys.headD ([] : Key) ∈ res.keys := headD_mem hne + have hheadin : res.keys.headD ([] : Key) ∈ + RangeRead.inRange db₀.keys (resolve db₀ r.begin) (resolve db₀ r.end_) := by + rw [← hkeys₀]; exact hheadmem + have hheadin' : (res.keys.headD ([] : Key)) ∈ db₀.keys ∧ + resolve db₀ r.begin ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < resolve db₀ r.end_ := by + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using hheadin + have hloE₀ : resolve db₀ r.begin ≤ r.end_.key := le_trans hheadin'.2.1 hheadE + have hpos : 1 ≤ r.end_.offset - (reportedAtOrAboveEnd r res : ℤ) := + endOffset_sub_reportedAtOrAboveEnd_ge_one hne hkeys₀ hloE₀ hEL hlast_lt rfl (by omega) + have hcompat : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (completeResidualReduced r res) res := + fun db => compatible_reduced_iff hrev hmore hne hheadE hEL + have hread' : Readable (completeResidualReduced r res) res := ⟨db₀, (hcompat db₀).mp hcomp₀⟩ + have hrev' : (completeResidualReduced r res).reverse = false := hrev + have hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < + (completeResidualReduced r res).end_.key := by + intro k hk + have hkle : k ≤ res.keys.getLastD ([] : Key) := le_getLastD_of_pairwise hpair₀ hk + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt _)) + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + rw [← headD_eq_head_of_ne hne]; exact hcrossD + rw [minimal_congr_of_compatible_iff hcompat] + unfold closedFormNegCrossCompleteResidual + have hred : (completeResidualReduced r res).end_.offset = + r.end_.offset - (reportedAtOrAboveEnd r res : ℤ) := rfl + by_cases hbig : 2 ≤ (completeResidualReduced r res).end_.offset + · rw [ite_eq_left hbig] + exact minimal_closedForm_of_negCross_overshoot hread' hrev' hbegin hmore hne hbig hhi hcross + · rw [ite_eq_right hbig] + have hoff1 : (completeResidualReduced r res).end_.offset = 1 := by omega + by_cases hclamp : reportedBelowBegin (completeResidualReduced r res) res ≤ + beginSlack (completeResidualReduced r res) + · rw [ite_eq_left hclamp] + exact minimal_closedForm_of_negCross_clampEndOne hread' + ⟨hrev', hbegin, hmore, hne, hoff1, hcrossD, hclamp⟩ + · rw [ite_eq_right hclamp] + have hnc : beginSlack (completeResidualReduced r res) < + reportedBelowBegin (completeResidualReduced r res) res := not_le.mp hclamp + by_cases hB : r.begin.key ≤ (completeResidualReduced r res).end_.key + · rw [ite_eq_left hB] + exact minimal_closedForm_of_negCross_endOne hread' + ⟨hrev', hbegin, hmore, hne, hoff1, hcrossD, hnc, hB⟩ + · rw [ite_eq_right hB] + exact minimal_closedForm_of_negCross_endOne_long hread' + ⟨hrev', hbegin, hmore, hne, hoff1, hcrossD, hnc, not_le.mp hB⟩ + +/-- The reversed crossed negative-begin complete residual non-overshoot cell. -/ +abbrev negCrossCompleteResidualRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ≤ r.end_.key ∧ + r.end_.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) + +/-- The reversed closed form, expressed through the forward rule on the flipped read. -/ +noncomputable def closedFormNegCrossCompleteResidualRev (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossCompleteResidual { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- **The reversed crossed negative-begin complete residual non-overshoot cell is +minimal**, by flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_complete_residual_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteResidualRev r res) : + Minimal r res (closedFormNegCrossCompleteResidualRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hheadE, hEL⟩ := hcell + 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 hpair : List.Pairwise (· < ·) res.keys.reverse := by + obtain ⟨db, hcompd⟩ := hread + simpa using ((RangeRead.compatible_pairwise hcompd).2 hrev).reverse + have hcell' : negCrossCompleteResidual { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend2, ?_, ?_, ?_⟩ + · rw [hheadD]; exact hcrossD + · rw [hheadD]; exact hheadE + · rw [hlastD]; exact hEL + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_complete_residual hread' hcell' + exact (minimal_flip (b := true) hrev hmore).mp (by + change Minimal { r with reverse := false } { keys := res.keys.reverse, more := false } + (closedFormNegCrossCompleteResidual { r with reverse := false } + { keys := res.keys.reverse, more := false }) + exact hmin) + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossHighClamp.lean b/FormalConflictRange/NegCrossHighClamp.lean new file mode 100644 index 0000000..d8c172c --- /dev/null +++ b/FormalConflictRange/NegCrossHighClamp.lean @@ -0,0 +1,2330 @@ +/- +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.NegCrossCompleteHigh + +/-! +# The clamping finite crossed negative-begin complete high cell + +This module closes the last crossed negative-begin complete cell: `begin.offset < 0`, +`end.offset ≥ 2`, `end.key < head`, `reportedBelowBegin ≤ beginSlack` and +`#reported = (end.offset - 1).toNat`. + +Writing `r = reportedBelowBegin`, `q = beginSlack`, `t = #reported`, +`C = q + 1 - r` and `K = keyAfter (last reported)`, the minimal range is + +* `[⊥, begin.key)` when `[K, begin.key)` holds at least `C` keys (at least `C + 1` + when `end.key` is the empty key), +* `[⊥, K)` otherwise. + +The proof separates the two possible begin resolutions (`⊥` and the reported head). +In the second case the database stores exactly `C` extra keys above the reported last +before `begin.key`; in the first case it stores at most `C - 1` such keys. This gives a +complete description of compatibility by the keys below `begin.key`, which yields +soundness. Necessity inside `[K, begin.key)` is shown by inserting one more gap key +(or, in the tight-capacity case, by removing one together with a key below `end.key`). +-/ + +namespace FormalConflictRange + +open scoped Classical + +variable {db db₁ db₂ : DB} {r : RangeRead} {res : Result} + +/-- In the clamping finite crossed negative-begin complete high cell, the begin +resolution is either the empty key or the reported head. -/ +theorem clamp_begin_resolve_bot_or_head + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) : + resolve db r.begin = (([] : Key) : WithTop Key) ∨ + resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + set H : Key := res.keys.headD ([] : Key) with hH + have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = RangeRead.inRange db.keys (resolve db r.begin) + (resolve db r.end_) := by + rw [hres, completeKeys_eq_inRange_of_not_rev hrev] + have hHin : H ∈ db.keys ∧ resolve db r.begin ≤ ((H : Key) : WithTop Key) ∧ + ((H : Key) : WithTop Key) < resolve db r.end_ := by + have h := hwin ▸ hHmem + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using h + by_cases hneg : r.begin.anchor db < 0 + · left; exact resolve_eq_nil hneg + · have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hneg + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with hnil | htop | ⟨k, hkdb, hresk⟩ + · left; exact hnil + · rw [htop] at hHin + exact absurd hHin.2.1 (not_le.mpr (WithTop.coe_lt_top H)) + · right + have hle : ((k : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := by + rw [← hresk]; exact hHin.2.1 + have hkH : k ≤ H := WithTop.coe_le_coe.mp hle + have hkwin : k ∈ RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [RangeRead.inRange, List.mem_filter] + refine ⟨hkdb, ?_⟩ + rw [decide_eq_true_eq] + refine ⟨?_, ?_⟩ + · exact le_of_eq hresk + · exact lt_of_le_of_lt hle hHin.2.2 + have hkres : k ∈ res.keys := by rw [hwin]; exact hkwin + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hHle : H ≤ k := by + rw [hH, hHD] + exact head_le_of_sortedLT' hsort hne hkres + have hkH' : k ≤ H := WithTop.coe_le_coe.mp hle + have hkH : k = H := le_antisymm hkH' hHle + rw [hresk, hkH] + +/-- In the crossed negative-begin complete high cell with a finite end, if the begin +resolution is the reported head then the count below the head equals the count below +the end key. -/ +theorem high_cnt_head_eq_cnt_end_of_resolve + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : + cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) = cnt db r.end_.key := by + set H : Key := res.keys.headD ([] : Key) with hH + have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hhead_mem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hL : resolve db r.begin = ((H : Key) : WithTop Key) := by simpa [hH] using hhead + have hwin : res.keys = between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev, hL] + rfl + have hhead_lt_hi : ((H : Key) : WithTop Key) < resolve db r.end_ := by + have hmem : H ∈ between db ((H : Key) : WithTop Key) (resolve db r.end_) := by + rw [← hwin]; exact hhead_mem + have hcond := of_decide_eq_true (List.mem_filter.mp hmem).2 + exact hcond.2 + have hcnt_hi : cnt db (resolve db r.end_) = + cnt db (((H : Key) : WithTop Key)) + res.keys.length := by + rw [cnt_eq_add_between db (le_of_lt hhead_lt_hi), ← hwin] + have hcnt_hi_le : cnt db (resolve db r.end_) ≤ + cnt db r.end_.key + (r.end_.offset - 1).toNat := by + have h : cnt db (resolve db r.end_) = + min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length := by + rw [show cnt db (resolve db r.end_) = db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl, + countP_lt_resolve (db := db) (s := r.end_), + anchor_toNat_eq (s := r.end_) (by omega)] + rfl + rw [h]; exact Nat.min_le_left _ _ + rw [hcnt_hi, hfinite] at hcnt_hi_le + have hcntE_le : cnt db r.end_.key ≤ cnt db (((H : Key) : WithTop Key)) := + cnt_le_cnt_of_le db (le_of_lt hEH) + omega + +/-- The begin-resolution alternatives together with the window description give that +no stored key lies in `[end.key, head)` in the clamping finite high cell. -/ +theorem clamp_between_end_head_eq_nil + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) : + between db r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key) = [] := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + have hHD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = RangeRead.inRange db.keys (resolve db r.begin) + (resolve db r.end_) := by + rw [hres, completeKeys_eq_inRange_of_not_rev hrev] + rcases clamp_begin_resolve_bot_or_head hcomp hrev hmore hne hbegin hcrossD with + hbot | hhead + · -- The begin resolution is `⊥`; the window starts at `⊥` and contains nothing + -- below the reported head. + rw [between, List.filter_eq_nil_iff] + intro x hxdb hdec + have hxEH : r.end_.key ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec + have hHin : H ∈ db.keys ∧ resolve db r.begin ≤ ((H : Key) : WithTop Key) ∧ + ((H : Key) : WithTop Key) < resolve db r.end_ := by + have h := hwin ▸ hHmem + simpa [RangeRead.inRange, List.mem_filter, decide_eq_true_eq] using h + have hxwin : x ∈ RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [RangeRead.inRange, List.mem_filter] + refine ⟨hxdb, ?_⟩ + rw [decide_eq_true_eq] + refine ⟨?_, lt_trans hxEH.2 hHin.2.2⟩ + rw [hbot] + exact WithTop.coe_le_coe.mpr (nil_le_key x) + have hxres : x ∈ res.keys := by rw [hwin]; exact hxwin + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hHle : H ≤ x := by + rw [hH, hHD] + exact head_le_of_sortedLT' hsort hne hxres + exact absurd (WithTop.coe_lt_coe.mp hxEH.2) (not_lt.mpr hHle) + · -- The begin resolution is the reported head. Count below the head equals the + -- count below the end key. + have hcntHE : cnt db (((H : Key) : WithTop Key)) = cnt db r.end_.key := + high_cnt_head_eq_cnt_end_of_resolve hcomp hrev hmore hne hend hEH hhead hfinite + rw [between, List.filter_eq_nil_iff] + intro x hxdb hdec + have hxEH : r.end_.key ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec + have hsplit := cnt_eq_add_between db (le_of_lt hEH) + rw [hcntHE] at hsplit + simp only [between] at hsplit + have hlen : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key)))).length = 0 := by + omega + have hfilter_nil := List.length_eq_zero_iff.mp hlen + have hxmem : x ∈ db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) := + List.mem_filter.mpr ⟨hxdb, hdec⟩ + rw [hfilter_nil] at hxmem + exact absurd hxmem (by simp) + +/-- The slack `C = beginSlack + 1 - reportedBelowBegin` is positive in the clamping +cell. -/ +theorem clampHigh_C_pos (hclamp : reportedBelowBegin r res ≤ beginSlack r) : + 0 < clampHighC r res := by + unfold clampHighC + omega + +/-- If the capacity is below the threshold, then it is at most `C`, and either it is +strictly below `C` or the end key is the empty key. -/ +theorem clampHigh_thresh_gt_facts + (hth : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + clampHighCap r res ≤ (clampHighC r res : ℕ∞) ∧ + (clampHighCap r res < (clampHighC r res : ℕ∞) ∨ + r.end_.key = (([] : Key) : WithTop Key)) := by + have hlt : clampHighCap r res < + ((clampHighC r res + (if r.end_.key = (([] : Key) : WithTop Key) then 1 else 0) : ℕ) : ℕ∞) := + lt_of_not_ge hth + by_cases hE : r.end_.key = (([] : Key) : WithTop Key) + · have hlt' : clampHighCap r res < (clampHighC r res : ℕ∞) + 1 := by + simpa [clampHighThresh, hE, Nat.cast_add, Nat.cast_one] using hlt + have hcapne : clampHighCap r res ≠ (⊤ : ℕ∞) := by + intro htop + rw [htop] at hlt' + exact not_top_lt hlt' + exact ⟨(ENat.lt_add_one_iff (m := clampHighCap r res) + (n := (clampHighC r res : ℕ∞)) (by simp)).mp hlt', Or.inr hE⟩ + · have hlt' : clampHighCap r res < (clampHighC r res : ℕ∞) := by + simpa [clampHighThresh, hE] using hlt + exact ⟨le_of_lt hlt', Or.inl hlt'⟩ + +/-- Every reported key lies strictly below `keyAfter (last reported)`. -/ +theorem clampHigh_reported_lt_keyAfter {r : RangeRead} {res : Result} + (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) : + ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < + ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) := by + intro k hk + have hklast : k ≤ res.keys.getLastD ([] : Key) := by + rw [getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hklast (keyAfter_self_lt _)) + +/-- In the crossed negative-begin complete high cell, the reported keys are exactly the +stored keys in `[head, keyAfter last)`. -/ +theorem clamp_keys_HK_eq {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) : + between db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) = res.keys := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set a : WithTop Key := resolve db r.begin with ha + set z : WithTop Key := resolve db r.end_ with hz + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db a z := by + rw [hres, completeKeys_eq_inRange_of_not_rev hrev]; rfl + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne + have haH : a ≤ ((H : Key) : WithTop Key) := by + have h := hwin ▸ hHmem + exact (of_decide_eq_true (List.mem_filter.mp h).2).1 + have hLz : ((L : Key) : WithTop Key) < z := by + have h := hwin ▸ hLmem + exact (of_decide_eq_true (List.mem_filter.mp h).2).2 + have hKz : ((K : Key) : WithTop Key) ≤ z := by + rw [hK]; exact keyAfter_le_of_lt hLz + refine List.Subset.antisymm_of_sortedLT ?_ ?_ + (List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)) hsort + · intro k hk + rw [between, List.mem_filter] at hk + obtain ⟨hkdb, hkcond⟩ := hk + rw [decide_eq_true_eq] at hkcond + obtain ⟨hkH, hkK⟩ := hkcond + have hkz : ((k : Key) : WithTop Key) < z := lt_of_lt_of_le hkK hKz + have hmem : k ∈ between db a z := by + rw [between, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨le_trans haH hkH, hkz⟩⟩ + rw [hwin]; exact hmem + · intro k hk + rw [between, List.mem_filter] + have hkdb : k ∈ db.keys := RangeRead.compatible_keys_subset hcomp hk + have hHle : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (by + rw [hH, headD_eq_head_of_ne hne] + exact head_le_of_sortedLT' hsort hne hk) + have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := + clampHigh_reported_lt_keyAfter (r := r) hsort hne k hk + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hHle, hkK⟩⟩ + +/-- No stored key lies in `[keyAfter last, resolve end)` in the crossed negative-begin +complete high cell. -/ +theorem clamp_between_K_resolve_end_eq_nil {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) : + between db + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) + (resolve db r.end_) = [] := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set a : WithTop Key := resolve db r.begin with ha + set z : WithTop Key := resolve db r.end_ with hz + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db a z := by + rw [hres, completeKeys_eq_inRange_of_not_rev hrev]; rfl + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have haH : a ≤ ((H : Key) : WithTop Key) := by + have h := hwin ▸ hHmem + exact (of_decide_eq_true (List.mem_filter.mp h).2).1 + have hall_le_L : ∀ k ∈ res.keys, k ≤ L := by + intro k hk + rw [hL, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hHL : H ≤ L := by + rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + rw [between, List.filter_eq_nil_iff] + intro x hxdb hdec + have hxcond : ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < z := of_decide_eq_true hdec + have hKx : K ≤ x := WithTop.coe_le_coe.mp hxcond.1 + have hHx : H ≤ x := le_trans hHL (le_trans (by rw [hK]; exact le_of_lt (keyAfter_self_lt L)) hKx) + have hmem : x ∈ between db a z := by + rw [between, List.mem_filter] + refine ⟨hxdb, ?_⟩ + rw [decide_eq_true_eq] + exact ⟨le_trans haH (WithTop.coe_le_coe.mpr hHx), hxcond.2⟩ + rw [← hwin] at hmem + have hxL : x ≤ L := hall_le_L x hmem + exact absurd (le_trans hKx hxL) (not_le.mpr (by rw [hK]; exact keyAfter_self_lt L)) + +/-- The number of stored keys below `begin.key` in the clamping finite crossed high +cell is `#reported + #gap`, splitting at `keyAfter last`, provided `keyAfter last` lies +below `begin.key`. This is the counting core of the `U = keyAfter last` branch. -/ +theorem clamp_cnt_begin_split {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hcrossD : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hKleB : ((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key) ≤ r.begin.key) : + cnt db r.begin.key = cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) + + res.keys.length + + (between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) + r.begin.key).length := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + have hHK : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := + clamp_keys_HK_eq hcomp hrev hmore hne + have hHL : H ≤ L := by + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hHKle : ((H : Key) : WithTop Key) ≤ ((K : Key) : WithTop Key) := by + rw [WithTop.coe_le_coe, hK] + exact le_trans hHL (le_of_lt (keyAfter_self_lt L)) + have hHleB : ((H : Key) : WithTop Key) ≤ r.begin.key := by + rw [hH]; exact le_of_lt hcrossD + have hKleB' : ((K : Key) : WithTop Key) ≤ r.begin.key := by rw [hK]; exact hKleB + have hsplit1 := cnt_eq_add_between db hHKle + have hsplit2 := cnt_eq_add_between db hKleB' + rw [hsplit2, hsplit1, hHK] + +/-- If the begin resolution is the reported head, the begin count below `begin.key` +equals the count below the head plus the slack plus one. -/ +theorem clamp_anchor_eq_cnt_head_of_resolve {db : DB} {r : RangeRead} {res : Result} + (hbegin : r.begin.offset < 0) (hne : res.keys ≠ []) + (hHne : res.keys.headD ([] : Key) ≠ []) + (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + cnt db r.begin.key = cnt db (((res.keys.headD ([] : Key) : Key) : WithTop Key)) + + beginSlack r + 1 := by + set H : Key := res.keys.headD ([] : Key) with hH + have hHne' : H ≠ [] := by rw [hH]; exact hHne + have hanc := anchor_eq_count_sub_slack db r (le_of_lt hbegin) + have hHidx : r.begin.anchor db = (cnt db ((H : Key) : WithTop Key) : ℤ) := by + by_cases hneg : r.begin.anchor db < 0 + · rw [resolve_eq_nil hneg] at hhead + have hHeq : H = [] := WithTop.coe_injective hhead.symm + exact absurd hHeq hHne' + · have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hneg + by_cases hltlen : (r.begin.anchor db).toNat < db.keys.length + · have hget := resolve_eq_getElem (db := db) (s := r.begin) h0 hltlen + rw [hhead] at hget + have hidx : db.keys[(r.begin.anchor db).toNat] = H := WithTop.coe_injective hget.symm + have hcnt := countP_lt_eq_of_getElem db.sorted hltlen hidx + have hcoe : cnt db ((H : Key) : WithTop Key) = + db.keys.countP (fun k : Key => decide (k < H)) := countP_lt_coe H + rw [hcoe, hcnt, Int.toNat_of_nonneg h0] + · have htop := resolve_eq_top h0 (le_of_not_gt hltlen) + rw [hhead] at htop + exact absurd htop (WithTop.coe_ne_top (a := H)) + have hEq : (cnt db r.begin.key : ℤ) = + (cnt db ((H : Key) : WithTop Key) : ℤ) + (beginSlack r : ℤ) + 1 := by + rw [hHidx] at hanc + simp only [cnt] at hanc ⊢ + linarith + have hgoal : ((cnt db r.begin.key : ℕ) : ℤ) = + ((cnt db ((H : Key) : WithTop Key) + beginSlack r + 1 : ℕ) : ℤ) := by + push_cast + linarith + exact_mod_cast hgoal + +/-- In the `U = keyAfter last` branch with `end.key ≠ ⊥`, the begin resolution is not +the reported head: that would require at least `clampHighC` gap keys, hence capacity at +least the threshold. -/ +theorem clampHigh_begin_resolve_ne_head {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hcomp : RangeRead.Compatible db r res) + (hcell : negCrossCompleteHighClampFinite r res) + (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + (hEbot : r.end_.key ≠ (([] : Key) : WithTop Key)) : + resolve db r.begin ≠ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + intro hhead + have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 + have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + have hHne : res.keys.headD ([] : Key) ≠ [] := by + intro h + have hlt : r.end_.key < (([] : Key) : WithTop Key) := by rw [← h]; exact hEH + exact absurd hlt (not_lt.mpr (coe_nil_le_key_withTop r.end_.key)) + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hcntB : cnt db r.begin.key = + cnt db ((H : Key) : WithTop Key) + beginSlack r + 1 := + clamp_anchor_eq_cnt_head_of_resolve (r := r) (res := res) hbegin hne hHne + (by rw [← hH]; exact hhead) + have hHK : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := + clamp_keys_HK_eq hcomp hrev hmore hne + have hHleB : ((H : Key) : WithTop Key) ≤ r.begin.key := by + rw [hH]; exact le_of_lt hcrossD + -- Split on the relative position of `begin.key`. + by_cases hBK : r.begin.key ≤ ((K : Key) : WithTop Key) + · -- `B ≤ K`: the stored keys `[H, B)` are the reported keys below `B`, count `rb` + have hsplit := cnt_eq_add_between db hHleB + have hfilter : between db ((H : Key) : WithTop Key) r.begin.key = + res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + calc between db ((H : Key) : WithTop Key) r.begin.key + = (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [between, between, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hlt : ((k : Key) : WithTop Key) < r.begin.key + · have hleK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := + lt_of_lt_of_le hlt hBK + simp [hlt, hleK] + · simp [hlt] + _ = _ := by rw [hHK] + have hlen : (between db ((H : Key) : WithTop Key) r.begin.key).length = + reportedBelowBegin r res := by + rw [hfilter, reportedBelowBegin, ← List.countP_eq_length_filter] + have hcntEq : cnt db r.begin.key = cnt db ((H : Key) : WithTop Key) + + reportedBelowBegin r res := by + rw [hsplit, hlen] + have hrbeq : reportedBelowBegin r res = beginSlack r + 1 := by omega + have hC0 : clampHighC r res = 0 := by unfold clampHighC; omega + have hCpos := clampHigh_C_pos (r := r) (res := res) hclamp + omega + · -- `K < B`: `t + gap = q + 1`, so `cap ≥ C` + push_neg at hBK + have hKleB : ((K : Key) : WithTop Key) ≤ r.begin.key := le_of_lt hBK + have hsplit := clamp_cnt_begin_split hcomp hrev hmore hne hcrossD (by rw [← hK]; exact hKleB) + have hrb : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by + simpa [hK, hL] using clampHigh_reported_lt_keyAfter (r := r) hsort hne k hk + exact lt_trans hkK hBK + have hgap : (between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) + r.begin.key).length = clampHighC r res := by + have hcntB' : cnt db r.begin.key = + cnt db (((res.keys.headD ([] : Key)) : WithTop Key)) + beginSlack r + 1 := by + simpa [hH] using hcntB + have hgapEq : res.keys.length + + (between db (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) + r.begin.key).length = beginSlack r + 1 := by + have h := hsplit + rw [hcntB'] at h + omega + unfold clampHighC + rw [hrb] + omega + have hle := between_length_le_intervalCard (db := db) (E := + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key))) + (B := r.begin.key) hKleB + rw [hgap] at hle + -- `thresh = C` since `E ≠ ⊥` + have hth : clampHighThresh r res = clampHighC r res := by + unfold clampHighThresh + rw [if_neg hEbot] + exact Nat.add_zero _ + apply hnotcap + rw [hth] + exact hle + +/-- In the `U = keyAfter last` branch (`¬ thresh ≤ cap`), every compatible database has +exactly the reported keys below `keyAfter last`. -/ +theorem clampHigh_K_filter_of_compatible {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hcomp : RangeRead.Compatible db r res) + (hcell : negCrossCompleteHighClampFinite r res) + (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys := by + classical + have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 + have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set E : WithTop Key := r.end_.key with hE + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db (resolve db r.begin) (resolve db r.end_) := by + rw [hres_eq, completeKeys_eq_inRange_of_not_rev hrev]; rfl + have hHK : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := + clamp_keys_HK_eq hcomp hrev hmore hne + have hKz : between db ((K : Key) : WithTop Key) (resolve db r.end_) = [] := + clamp_between_K_resolve_end_eq_nil hcomp hrev hmore hne + have hEHn : between db E ((H : Key) : WithTop Key) = [] := by + rw [hE, hH] + exact clamp_between_end_head_eq_nil hcomp hrev hmore hne hbegin hend hEH hcrossD hfinite + -- `H ≤ L` and `H < K`. + have hHL : H ≤ L := by + rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hHz : ((H : Key) : WithTop Key) < resolve db r.end_ := by + have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne + have hLz : ((L : Key) : WithTop Key) < resolve db r.end_ := by + have h := hwin ▸ hLmem + exact (of_decide_eq_true (List.mem_filter.mp h).2).2 + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hHL) hLz + refine eq_of_forall_mem_iff_of_pairwise + (db.sorted.pairwise.filter _) hsort.pairwise ?_ + intro k + constructor + · -- stored keys below `K` are reported + intro hx + rw [List.mem_filter] at hx + obtain ⟨hxdb, hxdec⟩ := hx + rw [decide_eq_true_eq] at hxdec + by_cases hxH : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · have : k ∈ between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) := by + rw [between, List.mem_filter] + exact ⟨hxdb, by rw [decide_eq_true_eq]; exact ⟨hxH, hxdec⟩⟩ + rw [hHK] at this; exact this + · -- `k < H`: ruled out + have hxltH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) := lt_of_not_ge hxH + by_cases hEbot : E = (([] : Key) : WithTop Key) + · have hxmem : k ∈ between db E ((H : Key) : WithTop Key) := by + rw [between, List.mem_filter] + refine ⟨hxdb, ?_⟩ + rw [decide_eq_true_eq] + exact ⟨by rw [hEbot]; exact coe_nil_le_key_withTop _, hxltH⟩ + rw [hEHn] at hxmem + exact absurd hxmem (by simp) + · -- `E ≠ ⊥`: show the begin resolution is `⊥`, so `k` is in the window + have hbot : resolve db r.begin = (([] : Key) : WithTop Key) := by + rcases clamp_begin_resolve_bot_or_head hcomp hrev hmore hne hbegin + (by rw [← hH]; exact hcrossD) with hab | hah + · exact hab + · exact absurd hah + (clampHigh_begin_resolve_ne_head hread hcomp hcell hnotcap (by rw [← hE]; exact hEbot)) + -- with `a = ⊥`, `k` lies in the window + have hxwin : k ∈ between db (resolve db r.begin) (resolve db r.end_) := by + rw [between, List.mem_filter] + refine ⟨hxdb, ?_⟩ + rw [decide_eq_true_eq] + exact ⟨by rw [hbot]; exact coe_nil_le_key_withTop _, lt_trans hxltH hHz⟩ + rw [← hwin] at hxwin + have hHle : H ≤ k := by + rw [hH, headD_eq_head_of_ne hne] + exact head_le_of_sortedLT' hsort hne hxwin + exact absurd (WithTop.coe_le_coe.mpr hHle) (not_le.mpr hxltH) + · -- reported keys are stored below `K` + intro hk + rw [List.mem_filter] + exact ⟨RangeRead.compatible_keys_subset hcomp hk, + by rw [decide_eq_true_eq] + exact clampHigh_reported_lt_keyAfter (r := r) hsort hne k hk⟩ + +/-- Filtering at `⊥` is the same as dropping the lower-bound conjunct. -/ +theorem clampHigh_filter_bot_eq (db : DB) (U : WithTop Key) : + db.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < U)) = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < U)) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨coe_nil_le_key_withTop _, h⟩⟩ + +/-- In the `U = keyAfter last` branch, no stored key lies below the reported head. -/ +theorem clampHigh_no_below_head_of_filter {db : DB} {res : Result} {H K : Key} + (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) + (hH : H = res.keys.headD ([] : Key)) + (hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key)) + (hfilter : db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys) : + db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < ((H : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hxdb hdec + have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec + have hxK : ((x : Key) : WithTop Key) < ((K : Key) : WithTop Key) := lt_trans hxH hHltK + have hxmem : x ∈ db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) := by + rw [List.mem_filter] + exact ⟨hxdb, by rw [decide_eq_true_eq]; exact hxK⟩ + rw [hfilter] at hxmem + have hHle : ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (by + rw [hH, headD_eq_head_of_ne hne] + exact head_le_of_sortedLT' hsort hne hxmem) + exact absurd hxH (not_lt.mpr hHle) + +/-- In the `U = keyAfter last` branch, the stored keys in `[head, keyAfter last)` are +exactly the reported keys. -/ +theorem clampHigh_between_head_keyAfter_of_filter {db : DB} {res : Result} {H K : Key} + (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) + (hH : H = res.keys.headD ([] : Key)) + (hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key)) + (hfilter : db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys) : + between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := by + refine eq_of_forall_mem_iff_of_pairwise (db.sorted.pairwise.filter _) hsort.pairwise ?_ + intro x + constructor + · intro hx + rw [between, List.mem_filter] at hx + obtain ⟨hxdb, hxdec⟩ := hx + rw [decide_eq_true_eq] at hxdec + rw [← hfilter, List.mem_filter] + exact ⟨hxdb, by rw [decide_eq_true_eq]; exact hxdec.2⟩ + · intro hx + rw [← hfilter, List.mem_filter] at hx + obtain ⟨hxdb, hxdec⟩ := hx + rw [decide_eq_true_eq] at hxdec + have hxres : x ∈ res.keys := by + rw [← hfilter, List.mem_filter] + exact ⟨hxdb, by rw [decide_eq_true_eq]; exact hxdec⟩ + rw [between, List.mem_filter] + refine ⟨hxdb, ?_⟩ + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (by + rw [hH, headD_eq_head_of_ne hne] + exact head_le_of_sortedLT' hsort hne hxres), hxdec⟩ + +/-- In the `U = keyAfter last` branch, nothing is stored below the reported head. -/ +theorem clampHigh_cnt_head_eq_zero_of_filter {db : DB} {res : Result} {H K : Key} + (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) + (hH : H = res.keys.headD ([] : Key)) + (hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key)) + (hfilter : db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys) : + cnt db ((H : Key) : WithTop Key) = 0 := by + have hno := clampHigh_no_below_head_of_filter hsort hne hH hHltK hfilter + rw [cnt, List.countP_eq_length_filter, hno] + rfl + +/-- In the `U = keyAfter last` branch, the stored keys below `begin.key` number at most +`beginSlack + 1`. -/ +theorem clampHigh_cnt_begin_le_of_filter {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys) : + cnt db r.begin.key ≤ beginSlack r + 1 := by + classical + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 + have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 + obtain ⟨db0, hcomp0⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + have hfilter' : db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys := by + simpa [hK, hL] using hfilter + have hHL : H ≤ L := by + rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by + rw [hK] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hHL) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt L)) + have hbetweenHK := clampHigh_between_head_keyAfter_of_filter hsort hne hH hHltK hfilter' + have hcntH := clampHigh_cnt_head_eq_zero_of_filter hsort hne hH hHltK hfilter' + by_cases hBK : r.begin.key ≤ ((K : Key) : WithTop Key) + · -- `begin.key ≤ keyAfter last`: every reported key below `begin.key` is stored there. + have hHB : between db ((H : Key) : WithTop Key) r.begin.key = + res.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [show between db ((H : Key) : WithTop Key) r.begin.key = + (between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key)).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) from by + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hkB : ((k : Key) : WithTop Key) < r.begin.key + · have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := + lt_of_lt_of_le hkB hBK + simp [hkB, hkK] + · simp [hkB]] + rw [hbetweenHK] + have hlen : (between db ((H : Key) : WithTop Key) r.begin.key).length = + reportedBelowBegin r res := by + rw [hHB, reportedBelowBegin, ← List.countP_eq_length_filter] + have hcntB : cnt db r.begin.key = reportedBelowBegin r res := by + have hsplit := cnt_eq_add_between db (le_of_lt hcrossD) + rw [hsplit, hcntH, hlen]; omega + omega + · -- `keyAfter last < begin.key`: the gap keys plus the reported keys. + push_neg at hBK + have hKleB : ((K : Key) : WithTop Key) ≤ r.begin.key := le_of_lt hBK + have hcntK : cnt db ((K : Key) : WithTop Key) = res.keys.length := by + rw [cnt_eq_add_between db (le_of_lt hHltK), hcntH, hbetweenHK] + exact Nat.zero_add _ + have hcntB : cnt db r.begin.key = res.keys.length + + (between db ((K : Key) : WithTop Key) r.begin.key).length := by + rw [cnt_eq_add_between db hKleB, hcntK] + have hr : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + have hkK : ((k : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by + simpa [hK, hL] using clampHigh_reported_lt_keyAfter (r := r) hsort hne k hk + exact lt_trans hkK hBK + have hcapC : (between db ((K : Key) : WithTop Key) r.begin.key).length ≤ + clampHighC r res := by + have hle := between_length_le_intervalCard (db := db) + (E := ((K : Key) : WithTop Key)) (B := r.begin.key) hKleB + have hfacts := clampHigh_thresh_gt_facts (r := r) (res := res) hnotcap + have hcap : ((between db ((K : Key) : WithTop Key) r.begin.key).length : ℕ∞) ≤ + (clampHighC r res : ℕ∞) := le_trans hle hfacts.1 + exact_mod_cast hcap + have hC : clampHighC r res = beginSlack r + 1 - res.keys.length := by + unfold clampHighC + rw [hr] + have htle : res.keys.length ≤ beginSlack r + 1 := by + have := hclamp + rw [hr] at this + omega + have hbnd : res.keys.length + + (between db ((K : Key) : WithTop Key) r.begin.key).length ≤ beginSlack r + 1 := by + calc res.keys.length + (between db ((K : Key) : WithTop Key) r.begin.key).length + ≤ res.keys.length + clampHighC r res := Nat.add_le_add_left hcapC _ + _ = beginSlack r + 1 := by rw [hC]; omega + omega + +/-- In the `U = keyAfter last` branch, the filter condition characterises +compatibility. -/ +theorem clampHigh_K_compatible_of_keys {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + (hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys) : + RangeRead.Compatible db r res := by + classical + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hclamp : reportedBelowBegin r res ≤ beginSlack r := hcell.2.1 + have hfinite : res.keys.length = (r.end_.offset - 1).toNat := hcell.2.2 + obtain ⟨db0, hcomp0⟩ := hread + have hlim : RangeRead.withinLimit r res.keys := hcomp0.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + have hfilter' : db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = res.keys := by + simpa [hK, hL] using hfilter + have hHL : H ≤ L := by + rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hHltK : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by + rw [hK] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hHL) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt L)) + have hbetweenHK := clampHigh_between_head_keyAfter_of_filter hsort hne hH hHltK hfilter' + have hcntH := clampHigh_cnt_head_eq_zero_of_filter hsort hne hH hHltK hfilter' + have hcntE : cnt db E = 0 := by + have hle : cnt db E ≤ cnt db ((H : Key) : WithTop Key) := + cnt_le_cnt_of_le db (le_of_lt hEH) + rw [hcntH] at hle; omega + -- the stored keys below `keyAfter last` are the reported keys + have h1 := filter_lt_eq_take_countP db.sorted ((K : Key) : WithTop Key) + have htakeEq : db.keys.take (db.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key)))) = res.keys := by + rw [← h1]; exact hfilter' + have hcntK' : db.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) = t := by + have hlen := congrArg List.length htakeEq + rw [List.length_take] at hlen + have hle : db.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < ((K : Key) : WithTop Key))) ≤ db.keys.length := + List.countP_le_length + rw [ht]; omega + have hcntK : cnt db ((K : Key) : WithTop Key) = t := by + simpa [cnt] using hcntK' + have htake : db.keys.take t = res.keys := by + rw [← hcntK']; exact htakeEq + have ht_le : t ≤ db.keys.length := by + have h := congrArg List.length htake + rw [List.length_take] at h + rw [ht] at h ⊢ + omega + -- the end resolution sits exactly past the first `t` stored keys + have hanchorNat : (r.end_.anchor db).toNat = t := by + rw [anchor_toNat_eq (s := r.end_) (by omega)] + have hce : db.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.end_.key)) = 0 := by + simpa [cnt, hE] using hcntE + rw [hce, Nat.zero_add, ← hfinite, ht] + have htpos : 0 < t := by + rw [ht]; exact List.length_pos_of_ne_nil hne + have h0end : 0 ≤ r.end_.anchor db := by + by_contra h + have hlt : r.end_.anchor db < 0 := lt_of_not_ge h + rw [Int.toNat_of_nonpos (le_of_lt hlt)] at hanchorNat + omega + have hcntz : cnt db (resolve db r.end_) = t := by + rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl, + countP_lt_resolve (db := db) (s := r.end_), hanchorNat] + exact Nat.min_eq_left ht_le + have hcntz' : db.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < resolve db r.end_)) = t := by + simpa [cnt] using hcntz + have hfilterZ : db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys := by + rw [filter_lt_eq_take_countP db.sorted (resolve db r.end_), hcntz', htake] + -- the begin resolution is at most the reported head + have hHdb : H ∈ db.keys := by + have hHres : H ∈ res.keys := by rw [hH]; exact headD_mem hne + rw [← hfilter', List.mem_filter] at hHres + exact hHres.1 + have hH0 : db.keys[0]'(List.length_pos_of_mem hHdb) = H := by + obtain ⟨j, hj, hjH⟩ := List.mem_iff_getElem.mp hHdb + have hcntHkey : db.keys.countP (fun k : Key => decide (k < H)) = 0 := by + rw [← countP_lt_coe H]; exact hcntH + have hcnt_eq_j : db.keys.countP (fun k : Key => decide (k < H)) = j := + countP_lt_eq_of_getElem db.sorted hj hjH + have hj0 : j = 0 := by rw [← hcnt_eq_j]; exact hcntHkey + subst hj0 + simpa using hjH + have hcntBle := clampHigh_cnt_begin_le_of_filter ⟨db0, hcomp0⟩ hcell hnotcap hfilter + have hanchor_le : r.begin.anchor db ≤ 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + have hc : ((db.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) ≤ (beginSlack r : ℤ) + 1 := by + have := hcntBle + simp only [cnt] at this + exact_mod_cast this + linarith + have haH : resolve db r.begin ≤ ((H : Key) : WithTop Key) := by + by_cases hneg : r.begin.anchor db < 0 + · rw [resolve_eq_nil hneg]; exact coe_nil_le_key_withTop _ + · have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hneg + have hzero : r.begin.anchor db = 0 := le_antisymm hanchor_le h0 + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + rw [hzero, Int.toNat_zero]; exact List.length_pos_of_mem hHdb + have hidx : (r.begin.anchor db).toNat = 0 := by rw [hzero, Int.toNat_zero] + have hget : db.keys[(r.begin.anchor db).toNat]'hltlen = + db.keys[0]'(List.length_pos_of_mem hHdb) := by + congr 1 + rw [resolve_eq_getElem h0 hltlen, hget, hH0] + have hcnta : cnt db (resolve db r.begin) = 0 := by + have hle : cnt db (resolve db r.begin) ≤ cnt db ((H : Key) : WithTop Key) := + cnt_le_cnt_of_le db haH + rw [hcntH] at hle; omega + -- the complete window is the reported list + have hwin : between db (resolve db r.begin) (resolve db r.end_) = res.keys := by + refine eq_of_forall_mem_iff_of_pairwise (db.sorted.pairwise.filter _) hsort.pairwise ?_ + intro k + constructor + · intro hk + rw [between, List.mem_filter] at hk + obtain ⟨hkdb, hdec⟩ := hk + rw [decide_eq_true_eq] at hdec + rw [← hfilterZ, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact hdec.2⟩ + · intro hk + have hkdb : k ∈ db.keys := by + have : k ∈ db.keys.take t := by rw [htake]; exact hk + exact List.mem_of_mem_take this + have hka : resolve db r.begin ≤ ((k : Key) : WithTop Key) := by + refine le_trans haH ?_ + exact WithTop.coe_le_coe.mpr (by + rw [hH, headD_eq_head_of_ne hne] + exact head_le_of_sortedLT' hsort hne hk) + have hkz : ((k : Key) : WithTop Key) < resolve db r.end_ := by + have hmem : k ∈ db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < resolve db r.end_)) := by + rw [hfilterZ]; exact hk + rw [List.mem_filter] at hmem + exact of_decide_eq_true hmem.2 + rw [between, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hka, hkz⟩⟩ + have hck : res.keys = RangeRead.completeKeys db r := by + rw [completeKeys_eq_inRange_of_not_rev hrev] + exact hwin.symm + exact ⟨hlim, Or.inr ⟨hmore, hck⟩⟩ + +/-- The `U = keyAfter last` branch: compatibility is exactly the filter below +`keyAfter last`. -/ +theorem clampHigh_K_compatible_iff_filter {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + RangeRead.Compatible db r res ↔ + db.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys := by + have hiff : ∀ db : DB, RangeRead.Compatible db r res ↔ + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)))) = res.keys := + fun db => ⟨fun hcomp => clampHigh_K_filter_of_compatible hread hcomp hcell hnotcap, + fun hf => clampHigh_K_compatible_of_keys hread hcell hnotcap hf⟩ + rw [hiff db, ← clampHigh_filter_bot_eq db + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key))] + +/-- **The `U = keyAfter last` branch of the clamping finite crossed high cell is +minimal.** -/ +theorem minimal_clampHigh_K {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hnotcap : ¬ ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + Minimal r res ⟨(([] : Key) : WithTop Key), + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key))⟩ := + minimal_of_compatible_iff_filter hread (fun db => + clampHigh_K_compatible_iff_filter hread hcell hnotcap) + +/-- A database splits into its keys below a threshold and those at or above it. -/ +theorem keys_eq_filter_lt_append_filter_not_lt (db : DB) (B : WithTop Key) : + db.keys = db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) ++ + db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < B)) := by + have h1 := filter_lt_eq_take_countP db.sorted B + have h2 := filter_not_lt_eq_drop_countP db.sorted B + rw [h1, h2] + exact (List.take_append_drop _ _).symm + +/-- The capacity condition of the `U = begin.key` branch forces `keyAfter last` to lie +strictly below `begin.key`. -/ +theorem clampHigh_K_lt_begin {r : RangeRead} {res : Result} + (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) < r.begin.key := by + by_contra hcon + push_neg at hcon + have hzero : clampHighCap r res = 0 := by + unfold clampHighCap + exact intervalCard_eq_zero_of_le hcon + have hpos : 0 < clampHighThresh r res := by + have := clampHigh_C_pos (r := r) (res := res) hcell.2.1 + unfold clampHighThresh + omega + rw [hzero] at hcap + exact absurd hcap (not_le.mpr (ENat.natCast_lt_natCast.mpr hpos)) + +/-- Every reported key lies strictly below `begin.key` once the capacity condition of +the `U = begin.key` branch holds. -/ +theorem clampHigh_reported_lt_begin {r : RangeRead} {res : Result} + (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) + (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.begin.key := by + intro k hk + have hK := clampHigh_K_lt_begin (r := r) (res := res) hcell hcap + have hkL : k ≤ res.keys.getLastD ([] : Key) := by + rw [getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hkK : ((k : Key) : WithTop Key) < + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) := + WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkL (keyAfter_self_lt _)) + exact lt_trans hkK hK + +section BeginKeyBranch + +open scoped Classical + +variable {db : DB} {r : RangeRead} {res : Result} + +/-- In the `U = begin.key` branch, a compatible database whose begin selector is +clamped stores nothing below the reported head. -/ +theorem clampHigh_B_bot_pad_eq_nil + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbot : resolve db r.begin = (([] : Key) : WithTop Key)) : + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((res.keys.headD ([] : Key) : Key) : WithTop Key))) = [] := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + set z : WithTop Key := resolve db r.end_ with hz + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db (([] : Key) : WithTop Key) z := by + rw [hres, completeKeys_eq_inRange_of_not_rev hrev, hbot]; rfl + have hHz : ((H : Key) : WithTop Key) < z := by + have h := hwin ▸ hHmem + exact (of_decide_eq_true (List.mem_filter.mp h).2).2 + rw [List.filter_eq_nil_iff] + intro x hxdb hdec + have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hdec + have hxz : ((x : Key) : WithTop Key) < z := lt_trans hxH hHz + have hxmem : x ∈ between db (([] : Key) : WithTop Key) z := by + rw [between, List.mem_filter] + exact ⟨hxdb, by rw [decide_eq_true_eq]; exact ⟨coe_nil_le_key_withTop _, hxz⟩⟩ + rw [← hwin] at hxmem + have hxH' : ((x : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := by + rw [← hheadD]; exact hxH + exact absurd (WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hxmem)) (not_le.mpr hxH') + +/-- In the `U = begin.key` branch, if the begin selector resolves onto the reported +head, then the keys below that head are exactly the stored keys below `end.key`, and in +particular all lie below `end.key`. -/ +theorem clampHigh_B_head_pad_lt_end + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hend : 1 ≤ r.end_.offset) + (hfinite : res.keys.length = (r.end_.offset - 1).toNat) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + ∀ x ∈ db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((res.keys.headD ([] : Key) : Key) : WithTop Key))), + ((x : Key) : WithTop Key) < r.end_.key := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + set z : WithTop Key := resolve db r.end_ with hz + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + set cE : ℕ := cnt db r.end_.key with hcE + set cH : ℕ := cnt db ((H : Key) : WithTop Key) with hcH + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hwin : res.keys = between db ((H : Key) : WithTop Key) z := by + rw [hres, completeKeys_eq_inRange_of_not_rev hrev, hhead]; rfl + have hHz : ((H : Key) : WithTop Key) < z := by + have h := hwin ▸ hHmem + exact (of_decide_eq_true (List.mem_filter.mp h).2).2 + have hcntz : cnt db z = cH + t := by + have h := cnt_eq_add_between db (le_of_lt hHz) + rw [← hcH, ← hwin, ← ht] at h + exact h + have hane : (r.end_.anchor db).toNat = cE + t := by + rw [anchor_toNat_eq (s := r.end_) hend] + change cnt db r.end_.key + (r.end_.offset - 1).toNat = cE + t + have het : (r.end_.offset - 1).toNat = t := hfinite.symm.trans ht.symm + rw [← hcE, het] + have hmin : min (cE + t) db.keys.length = cH + t := by + have h := countP_lt_resolve (db := db) (s := r.end_) + rw [← hz, hane] at h + rw [show db.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < z)) = cnt db z from rfl, hcntz] at h + exact h.symm + have hcEle : cE ≤ cH := by + have := cnt_le_cnt_of_le db (le_of_lt hEH) + simpa [hcE, hcH] using this + have hceq : cE = cH := by + by_contra hcontra + have hlt : cE < cH := lt_of_le_of_ne hcEle hcontra + have hle := Nat.min_le_left (cE + t) db.keys.length + omega + intro x hx + rw [List.mem_filter] at hx + have hxH : ((x : Key) : WithTop Key) < ((H : Key) : WithTop Key) := of_decide_eq_true hx.2 + by_contra hcon + push_neg at hcon + have hcntsplit := cnt_eq_add_between db (le_of_lt hEH) + rw [← hcH, ← hcE, hceq] at hcntsplit + have hlen0 : (between db r.end_.key ((H : Key) : WithTop Key)).length = 0 := by + omega + have hnil : between db r.end_.key ((H : Key) : WithTop Key) = [] := + List.eq_nil_of_length_eq_zero hlen0 + have hmem : x ∈ between db r.end_.key ((H : Key) : WithTop Key) := by + rw [between, List.mem_filter] + exact ⟨hx.1, by rw [decide_eq_true_eq]; exact ⟨hcon, hxH⟩⟩ + rw [hnil] at hmem + exact absurd hmem (by simp) + +/-- In the `U = begin.key` branch, if the begin selector resolves to the empty key, +then at most `beginSlack + 1` stored keys lie below `begin.key`. -/ +theorem clampHigh_B_bot_cnt_le {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hbot : resolve db r.begin = (([] : Key) : WithTop Key)) : + cnt db r.begin.key ≤ beginSlack r + 1 := by + by_cases hneg : r.begin.anchor db < 0 + · have hanchor : r.begin.anchor db = + (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + rfl + rw [hanchor] at hneg + omega + · push_neg at hneg + have hlt : (r.begin.anchor db).toNat < db.keys.length := by + by_contra hcon + push_neg at hcon + rw [resolve_eq_top hneg hcon] at hbot + exact absurd hbot (by simp) + have hget : db.keys[(r.begin.anchor db).toNat] = ([] : Key) := by + have := resolve_eq_getElem hneg hlt + rw [hbot] at this + exact (WithTop.coe_inj.mp this).symm + have hidx : (r.begin.anchor db).toNat = 0 := by + have h := countP_lt_eq_of_getElem db.sorted hlt hget + have hz : db.keys.countP (fun k : Key => decide (k < ([] : Key))) = 0 := by + rw [List.countP_eq_zero] + intro k _ + simp [List.not_lt_nil] + rw [← h, hz] + have hle : r.begin.anchor db ≤ 0 := Int.toNat_eq_zero.mp hidx + have hanchor : r.begin.anchor db = + (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + rfl + rw [hanchor] at hle + omega + +end BeginKeyBranch + +section BeginKeyDecomp + +open scoped Classical + +variable {db : DB} {r : RangeRead} {res : Result} + +/-- When the begin selector resolves onto the reported head in the crossed +negative-begin complete high cell, the number of stored keys below `begin.key` is the +number below the head plus `beginSlack + 1`. -/ +theorem clampHigh_B_head_cnt_begin + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hEH : r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key)) + (hhead : resolve db r.begin = ((res.keys.headD ([] : Key) : Key) : WithTop Key)) : + cnt db r.begin.key = + cnt db ((res.keys.headD ([] : Key) : Key) : WithTop Key) + beginSlack r + 1 := by + classical + set H : Key := res.keys.headD ([] : Key) with hH + set cH : ℕ := cnt db ((H : Key) : WithTop Key) with hcH + set cB : ℕ := cnt db r.begin.key with hcB + set q : ℕ := beginSlack r with hq + have hHne : H ≠ [] := by + intro hnil + rw [hnil] at hEH + exact absurd hEH (not_lt.mpr (coe_nil_le_key_withTop r.end_.key)) + have hanchor : r.begin.anchor db = (cB : ℤ) - (q : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcB, hq] + rfl + have hnonneg : 0 ≤ r.begin.anchor db := by + by_contra hneg + rw [resolve_eq_nil (lt_of_not_ge hneg)] at hhead + exact hHne (WithTop.coe_inj.mp hhead.symm) + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + have hcalc : (r.begin.anchor db).toNat = cB - q - 1 := by + rw [hanchor] + omega + rw [hcalc] + exact lt_of_lt_of_le (by omega) (cnt_le_length db r.begin.key) + have hget : db.keys[(r.begin.anchor db).toNat] = H := by + have := resolve_eq_getElem hnonneg hltlen + rw [hhead] at this + exact WithTop.coe_inj.mp this.symm + have hidx : (r.begin.anchor db).toNat = cH := by + have := countP_lt_eq_of_getElem db.sorted hltlen hget + simpa [cnt, hcH] using this.symm + have hcast : (r.begin.anchor db) = (cH : ℤ) := by + conv_lhs => rw [← Int.toNat_of_nonneg hnonneg] + rw [hidx] + rw [hcast] at hanchor + have : (cH : ℤ) = (cB : ℤ) - (q : ℤ) - 1 := hanchor + omega + +/-- **Decomposition of a compatible database in the `U = begin.key` branch.** The +stored keys split as a (possibly empty) pad below `end.key`, the reported keys, a gap in +`[keyAfter last, begin.key)`, and keys at or above `begin.key`. The gap has length +`C` when the begin selector resolves onto the reported head, and the pad is empty with a +shorter gap when it clamps. -/ +theorem clampHigh_B_decomp_of_compatible + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + (hcomp : RangeRead.Compatible db r res) : + ∃ pad gap rest : List Key, + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + pad ++ res.keys ++ gap ∧ + db.keys = pad ++ res.keys ++ gap ++ rest ∧ + (∀ x ∈ pad, ((x : Key) : WithTop Key) < r.end_.key) ∧ + (∀ x ∈ gap, (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) ∧ + (∀ x ∈ rest, r.begin.key ≤ ((x : Key) : WithTop Key)) ∧ + (gap.length = clampHighC r res ∨ + (gap.length < clampHighC r res ∧ pad = [])) := by + classical + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hclamp := hcell.2.1 + have hfinite := hcell.2.2 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set q : ℕ := beginSlack r with hq + set t : ℕ := res.keys.length with ht + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne + have hHL : H ≤ L := by + rw [hH, hL, hheadD, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hHKle : ((H : Key) : WithTop Key) ≤ ((K : Key) : WithTop Key) := by + rw [hK] + exact WithTop.coe_le_coe.mpr (le_trans hHL (le_of_lt (keyAfter_self_lt L))) + have hKLB : ((K : Key) : WithTop Key) < r.begin.key := by + have := clampHigh_K_lt_begin (r := r) (res := res) hcell hcap + simpa [hK, hL] using this + -- the reported block `[head, keyAfter last)` + have hHK_eq : between db ((H : Key) : WithTop Key) ((K : Key) : WithTop Key) = res.keys := by + simpa [hH, hK, hL] using clamp_keys_HK_eq (db := db) (r := r) (res := res) hcomp hrev hmore hne + -- filter below `keyAfter last` + have hfK : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((K : Key) : WithTop Key))) = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key))) ++ res.keys := by + rw [filter_lt_eq_filter_lt_append_between db.sorted hHKle] + simp only [between] at hHK_eq + rw [← hHK_eq] + -- filter below `begin.key` + have hfB : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key))) ++ res.keys ++ between db ((K : Key) : WithTop Key) r.begin.key := by + rw [filter_lt_eq_filter_lt_append_between db.sorted (le_of_lt hKLB), hfK] + rfl + -- split the database into below and at-or-above `begin.key` + have hsplit := keys_eq_filter_lt_append_filter_not_lt db r.begin.key + rw [hfB] at hsplit + refine ⟨db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key))), + between db ((K : Key) : WithTop Key) r.begin.key, + db.keys.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < r.begin.key)), + hfB, hsplit, ?_, ?_, ?_, ?_⟩ + · -- pad keys lie below `end.key` + have hresol := clamp_begin_resolve_bot_or_head (db := db) (r := r) (res := res) + hcomp hrev hmore hne hbegin hcrossD + rcases hresol with hbot | hhead + · have hnil := clampHigh_B_bot_pad_eq_nil (db := db) (r := r) (res := res) + hcomp hrev hmore hne hbot + have hpadnil : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key))) = [] := by simpa [hH] using hnil + intro x hx + rw [hpadnil] at hx + exact absurd hx (by simp) + · have hp := clampHigh_B_head_pad_lt_end (db := db) (r := r) (res := res) + hcomp hrev hmore hne (by omega) hfinite hEH hhead + intro x hx + exact hp x (by simpa [hH] using hx) + · -- gap keys lie in `[keyAfter last, begin.key)` + intro x hx + rw [between, List.mem_filter] at hx + exact of_decide_eq_true hx.2 + · -- remaining keys lie at or above `begin.key` + intro x hx + rw [List.mem_filter] at hx + exact le_of_not_gt (of_decide_eq_true hx.2) + · -- length condition on the gap + have hresol := clamp_begin_resolve_bot_or_head (db := db) (r := r) (res := res) + hcomp hrev hmore hne hbegin hcrossD + have hr : reportedBelowBegin r res = t := by + rw [reportedBelowBegin, ht, List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap k hk + have hcBsplit : cnt db r.begin.key = + (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key)))).length + t + + (between db ((K : Key) : WithTop Key) r.begin.key).length := by + rw [show cnt db r.begin.key = (db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key))).length from by + simp [cnt, List.countP_eq_length_filter], hfB] + simp only [List.length_append, ht] + rcases hresol with hbot | hhead + · -- clamped: the pad is empty and the gap has at most `C` keys + have hnil := clampHigh_B_bot_pad_eq_nil (db := db) (r := r) (res := res) + hcomp hrev hmore hne hbot + have hpadnil : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key))) = [] := by simpa [hH] using hnil + have hcntle := clampHigh_B_bot_cnt_le (db := db) (r := r) (res := res) + hcomp hrev hmore hne hbegin hbot + have hpadlen : (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key)))).length = 0 := by rw [hpadnil]; rfl + rw [hpadlen] at hcBsplit + simp only [Nat.zero_add] at hcBsplit + have hC : clampHighC r res = q + 1 - t := by + unfold clampHighC + rw [hq, hr] + have hgaple : (between db ((K : Key) : WithTop Key) r.begin.key).length ≤ + clampHighC r res := by + rw [hC]; omega + rcases lt_or_eq_of_le hgaple with hlt | heq + · exact Or.inr ⟨hlt, hpadnil⟩ + · exact Or.inl heq + · -- resolved onto the head: the gap has length exactly `C` + have hcnt := clampHigh_B_head_cnt_begin (db := db) (r := r) (res := res) + hcomp hrev hmore hne hbegin hEH hhead + have hcH : cnt db ((res.keys.headD ([] : Key) : Key) : WithTop Key) = + (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + ((H : Key) : WithTop Key)))).length := by + have : ((res.keys.headD ([] : Key) : Key) : WithTop Key) = ((H : Key) : WithTop Key) := by + rw [hH] + rw [this] + simp [cnt, List.countP_eq_length_filter] + rw [hcH] at hcnt + left + have hC : clampHighC r res = q + 1 - t := by + unfold clampHighC + rw [hq, hr] + rw [hC] + rw [hcBsplit] at hcnt + simp only [List.length_append, ht] at hcnt + omega + +end BeginKeyDecomp + +section ShapeTools + +/-- A list all of whose entries lie below `B` is fixed by filtering below `B`. -/ +theorem filter_lt_eq_self_of_forall {l : List Key} {B : WithTop Key} + (h : ∀ x ∈ l, ((x : Key) : WithTop Key) < B) : + l.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = l := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact h k hk + +/-- A list none of whose entries lie below `B` is emptied by filtering below `B`. -/ +theorem filter_lt_eq_nil_of_forall {l : List Key} {B : WithTop Key} + (h : ∀ x ∈ l, ¬ ((x : Key) : WithTop Key) < B) : + l.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk + rw [decide_eq_true_eq] + exact h k hk + +end ShapeTools + +section BetweenTools + +/-- A list all of whose entries lie in `[a, z)` is fixed by filtering to that range. -/ +theorem filter_between_eq_self {l : List Key} {a z : WithTop Key} + (h : ∀ x ∈ l, a ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < z) : + l.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < z)) = l := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact h k hk + +/-- A list none of whose entries lie in `[a, z)` is emptied by filtering to that range. -/ +theorem filter_between_eq_nil {l : List Key} {a z : WithTop Key} + (h : ∀ x ∈ l, ¬(a ≤ ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < z)) : + l.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < z)) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk + rw [decide_eq_true_eq] + exact h k hk + +end BetweenTools + +section BeginKeyConverse + +open scoped Classical +variable {db : DB} {r : RangeRead} {res : Result} + +/-- The `U = begin.key` branch, converse direction: a database whose keys below +`begin.key` are a pad, the reported keys and a gap of the prescribed size is compatible. -/ +theorem clampHigh_B_compatible_of_shape + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + {pad gap rest : List Key} + (hkeys : db.keys = pad ++ res.keys ++ gap ++ rest) + (hpad : ∀ x ∈ pad, ((x : Key) : WithTop Key) < r.end_.key) + (hgap : ∀ x ∈ gap, (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) + (hrest : ∀ x ∈ rest, r.begin.key ≤ ((x : Key) : WithTop Key)) + (hgapcond : gap.length = clampHighC r res ∨ + (gap.length < clampHighC r res ∧ pad = [])) + (hwl : RangeRead.withinLimit r res.keys) : + RangeRead.Compatible db r res := by + classical + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hfinite := hcell.2.2 + have hclamp := hcell.2.1 + obtain ⟨db0, hcomp0⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set B : WithTop Key := r.begin.key with hB + set E : WithTop Key := r.end_.key with hE + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne + have hres_ge_H : ∀ k ∈ res.keys, ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := by + intro k hk + rw [hH, headD_eq_head_of_ne hne] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hk) + have hres_le_L : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := by + intro k hk + rw [hL, getLastD_eq_getLast_of_ne hne] + exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hk) + have hHL : ((H : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := hres_ge_H L hLmem + have hEH' : E < ((H : Key) : WithTop Key) := by rw [hE, hH]; exact hEH + have hLK : ((L : Key) : WithTop Key) < ((K : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr (keyAfter_self_lt L) + have hKB : ((K : Key) : WithTop Key) < B := by + rw [hB] + have := clampHigh_K_lt_begin (r := r) (res := res) hcell hcap + simpa [hK, hL] using this + have hreported : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < B := by + rw [hB] + exact clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap + have hEHb : E < B := lt_trans hEH' hcrossD + have hEK : E < ((K : Key) : WithTop Key) := lt_trans (lt_of_lt_of_le hEH' hHL) hLK + have hres_sub : ∀ k ∈ res.keys, k ∈ db.keys := by + intro k hk + rw [hkeys] + exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hk))))) + -- filters below `E` and `B` + have hfiltE : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < E)) = pad := by + rw [hkeys, List.filter_append, List.filter_append, List.filter_append] + rw [filter_lt_eq_self_of_forall (B := E) (by intro x hx; exact hpad x hx)] + rw [filter_lt_eq_nil_of_forall (B := E) (by + intro x hx; exact not_lt.mpr (le_of_lt (lt_of_lt_of_le hEH' (hres_ge_H x hx))))] + rw [filter_lt_eq_nil_of_forall (B := E) (by + intro x hx; exact not_lt.mpr (le_of_lt (lt_of_lt_of_le hEK (hgap x hx).1)))] + rw [filter_lt_eq_nil_of_forall (B := E) (by + intro x hx; exact not_lt.mpr (le_of_lt (lt_of_lt_of_le hEHb (hrest x hx))))] + simp + have hcntE : cnt db E = pad.length := by + rw [show cnt db E = (db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < E))).length from by + simp only [cnt, List.countP_eq_length_filter]] + rw [hfiltE] + have hfiltB : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + pad ++ res.keys ++ gap := by + rw [hkeys, List.filter_append, List.filter_append, List.filter_append] + rw [filter_lt_eq_self_of_forall (B := B) (by intro x hx; exact lt_trans (hpad x hx) hEHb)] + rw [filter_lt_eq_self_of_forall (B := B) (by intro x hx; exact hreported x hx)] + rw [filter_lt_eq_self_of_forall (B := B) (by intro x hx; exact (hgap x hx).2)] + rw [filter_lt_eq_nil_of_forall (B := B) (by + intro x hx; exact not_lt.mpr (hrest x hx))] + simp + have hcntB : cnt db B = pad.length + t + gap.length := by + rw [show cnt db B = (db.keys.filter (fun k : Key => decide + (((k : Key) : WithTop Key) < B))).length from by + simp only [cnt, List.countP_eq_length_filter]] + rw [hfiltB, List.length_append, List.length_append, ht] + have hr : reportedBelowBegin r res = t := by + rw [reportedBelowBegin, ht, List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact by rw [← hB]; exact hreported k hk + have hCval : clampHighC r res = q + 1 - t := by + unfold clampHighC + rw [hq, hr] + -- the end anchor, in natural numbers + have hane : (r.end_.anchor db).toNat = pad.length + t := by + rw [anchor_toNat_eq (s := r.end_) (by omega)] + have hcountE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + r.end_.key)) = cnt db E := by rw [hE]; rfl + rw [hcountE, hcntE] + have het : (r.end_.offset - 1).toNat = t := by omega + rw [het] + -- the begin anchor + have hanchorB : r.begin.anchor db = (cnt db B : ℤ) - (q : ℤ) - 1 := by + rw [hB, anchor_eq_count_sub_slack db r (le_of_lt hbegin), hq] + rfl + -- split on the two admissible shapes of the gap + rcases hgapcond with hgEq | ⟨hgLt, hpadnil⟩ + · -- `gap.length = C`, the begin selector resolves onto the reported head + have hgle : gap.length = q + 1 - t := by rw [← hCval]; exact hgEq + have hCpos := clampHigh_C_pos (r := r) (res := res) hclamp + have hCvalpos : 0 < q + 1 - t := by rw [← hCval]; exact hCpos + have hgapne : gap ≠ [] := by + intro hnil; rw [hnil, List.length_nil] at hgle; omega + -- the filter `[head, begin.key)` has `q + 1` entries + have hfiltHB : (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B))).length = q + 1 := by + have heq : db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) = res.keys ++ gap := by + rw [hkeys, List.filter_append, List.filter_append, List.filter_append] + rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) (z := B) (by + intro x hx; exact fun hcon => absurd hcon.1 (not_le.mpr (lt_trans (hpad x hx) hEH')))] + rw [filter_between_eq_self (a := ((H : Key) : WithTop Key)) (z := B) (by + intro x hx; exact ⟨hres_ge_H x hx, hreported x hx⟩)] + rw [filter_between_eq_self (a := ((H : Key) : WithTop Key)) (z := B) (by + intro x hx; exact ⟨le_trans (le_trans hHL (le_of_lt hLK)) (hgap x hx).1, (hgap x hx).2⟩)] + rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) (z := B) (by + intro x hx hcon; exact absurd hcon.2 (not_lt.mpr (hrest x hx)))] + simp + rw [heq, List.length_append, hgle] + omega + have haresolve : resolve db r.begin = ((H : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len (db := db) (r := r) (head := H) hbegin + (hres_sub H hHmem) hcrossD hfiltHB + -- the end selector resolves to the first gap key + have hEidx : (r.end_.anchor db).toNat = pad.length + t := hane + have hlen_lt : pad.length + t < db.keys.length := by + rw [hkeys, List.length_append, List.length_append, List.length_append] + have : 0 < gap.length := List.length_pos_of_ne_nil hgapne + omega + have hnonnegE : 0 ≤ r.end_.anchor db := by + unfold Selector.anchor + omega + have hz : resolve db r.end_ = + ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key) := by + rw [resolve_eq_getElem hnonnegE (by rw [hane]; exact hlen_lt)] + have hidx : db.keys[(r.end_.anchor db).toNat] = db.keys[pad.length + t] := by + have h1 : db.keys[(r.end_.anchor db).toNat]? = + some (db.keys[(r.end_.anchor db).toNat]) := + List.getElem?_eq_getElem _ + have h2 : db.keys[pad.length + t]? = some (db.keys[pad.length + t]) := + List.getElem?_eq_getElem hlen_lt + have h3 : db.keys[(r.end_.anchor db).toNat]? = db.keys[pad.length + t]? := by + rw [hane] + rw [h1, h2] at h3 + exact Option.some.inj h3 + rw [hidx] + congr 1 + have hge : db.keys[pad.length + t] = gap[0]'(List.length_pos_of_ne_nil hgapne) := by + have hsome : db.keys[pad.length + t]? = gap[0]? := by + rw [hkeys, List.append_assoc] + rw [List.getElem?_append_right (l₁ := pad ++ res.keys) (l₂ := gap ++ rest) (by + rw [List.length_append, ← ht])] + rw [show (pad.length + t) - (pad ++ res.keys).length = 0 from by + rw [List.length_append, ← ht]; omega] + rw [List.getElem?_append_left (List.length_pos_of_ne_nil hgapne)] + have hzgap : gap[0]? = some (gap[0]'(List.length_pos_of_ne_nil hgapne)) := + List.getElem?_eq_getElem (List.length_pos_of_ne_nil hgapne) + have hs : some (db.keys[pad.length + t]) = + some (gap[0]'(List.length_pos_of_ne_nil hgapne)) := by + rw [← List.getElem?_eq_getElem hlen_lt, hsome, hzgap] + exact Option.some.inj hs + exact hge + -- the gap block is sorted, so its head is its least element + have hsub : gap.Sublist db.keys := by + rw [hkeys] + exact (List.sublist_append_left gap rest).trans + (by simpa [List.append_assoc] using + (List.sublist_append_right (pad ++ res.keys) (gap ++ rest))) + have hgapSorted : gap.SortedLT := + List.sortedLT_iff_pairwise.mpr + (List.Pairwise.sublist hsub (List.sortedLT_iff_pairwise.mp db.sorted)) + -- the range `[head, first gap key)` is exactly the reported keys + have hbetween : between db (resolve db r.begin) (resolve db r.end_) = res.keys := by + rw [haresolve, hz, between, hkeys, List.filter_append, List.filter_append, List.filter_append] + rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) + (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by + intro x hx; exact fun hcon => absurd hcon.1 (not_le.mpr (lt_trans (hpad x hx) hEH')))] + rw [filter_between_eq_self (a := ((H : Key) : WithTop Key)) + (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by + intro x hx + refine ⟨hres_ge_H x hx, ?_⟩ + exact lt_of_lt_of_le (lt_of_le_of_lt (hres_le_L x hx) hLK) + (hgap _ (List.getElem_mem _)).1)] + rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) + (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by + intro x hx + have hhead : ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) := by + rw [← List.head_eq_getElem hgapne] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hgapSorted hgapne hx) + exact fun hcon => absurd hcon.2 (not_lt.mpr hhead))] + rw [filter_between_eq_nil (a := ((H : Key) : WithTop Key)) + (z := ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key)) (by + intro x hx hcon + have hzB : ((gap[0]'(List.length_pos_of_ne_nil hgapne) : Key) : WithTop Key) < B := + (hgap _ (List.getElem_mem _)).2 + exact absurd hcon.2 + (not_lt.mpr (le_of_lt (lt_of_lt_of_le hzB (hrest x hx)))))] + simp + have hck : res.keys = RangeRead.completeKeys db r := by + rw [completeKeys_eq_inRange_of_not_rev hrev] + exact hbetween.symm + exact ⟨hwl, Or.inr ⟨hmore, hck⟩⟩ + · -- `gap.length < C` and the pad is empty + have hpadl : pad.length = 0 := by rw [hpadnil]; rfl + have hgle : gap.length < q + 1 - t := by rw [← hCval]; exact hgLt + -- the begin selector clamps to `⊥` + have hanchor_neg : r.begin.anchor db < 0 := by + rw [hanchorB, hcntB, hpadl] + have : (t + gap.length : ℤ) - (q : ℤ) - 1 < 0 := by + have : t + gap.length < q + 1 := by omega + omega + simpa using this + have haresolve : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor_neg + -- the end selector resolves to the first stored key at or after `begin.key` (or `⊤`) + have hEidx : (r.end_.anchor db).toNat = t := by + rw [hane, hpadl]; omega + -- keys below `begin.key` are exactly the reported keys + have hfiltBpad : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + res.keys ++ gap := by + rw [hfiltB, hpadnil]; simp + -- `between` is the take of the count below the end resolution + have hcntz : cnt db (resolve db r.end_) = t := by + rw [show cnt db (resolve db r.end_) = db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) = t from by + rw [countP_lt_resolve (db := db) (s := r.end_), hEidx] + exact Nat.min_eq_left (by + have : t ≤ db.keys.length := by + rw [hkeys, List.length_append, List.length_append, List.length_append, hpadl] + omega + exact this)] + have hbetween : between db (resolve db r.begin) (resolve db r.end_) = res.keys := by + rw [haresolve] + have hbot : between db (([] : Key) : WithTop Key) (resolve db r.end_) = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + unfold between + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨coe_nil_le_key_withTop _, h⟩⟩ + rw [hbot, filter_lt_eq_take_countP db.sorted] + have hcnt' : db.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < resolve db r.end_)) = t := by + rw [countP_lt_resolve (db := db) (s := r.end_), hEidx] + exact Nat.min_eq_left (by + have : t ≤ db.keys.length := by + rw [hkeys, List.length_append, List.length_append, List.length_append, hpadl] + omega + exact this) + rw [hcnt'] + have htake : db.keys.take t = res.keys := by + rw [hkeys, hpadnil] + simp only [List.nil_append] + rw [List.append_assoc] + exact List.take_left' ht.symm + exact htake + have hck : res.keys = RangeRead.completeKeys db r := by + rw [completeKeys_eq_inRange_of_not_rev hrev] + exact hbetween.symm + exact ⟨hwl, Or.inr ⟨hmore, hck⟩⟩ + +end BeginKeyConverse + +section BeginKeySound + +open scoped Classical + +variable {db : DB} {r : RangeRead} {res : Result} + +/-- Soundness of the `U = begin.key` branch: agreement below `begin.key` determines +compatibility. -/ +theorem clampHigh_B_sound + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + Sound r res (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : ConflictRange) := by + have step : ∀ dba dbc : DB, AgreeOn (⟨(([] : Key) : WithTop Key), r.begin.key⟩ : + ConflictRange) dba dbc → RangeRead.Compatible dba r res → + RangeRead.Compatible dbc r res := by + intro dba dbc hagree hcomp + have hfilt : dba.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = dbc.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + obtain ⟨pad, gap, rest₁, hfB, hkeys, hpad, hgap, _, hgapcond⟩ := + clampHigh_B_decomp_of_compatible hread hcell hcap hcomp + set rest₂ : List Key := dbc.keys.filter (fun k : Key => decide + (¬ ((k : Key) : WithTop Key) < r.begin.key)) with hrest₂ + have hkeys₂ : dbc.keys = pad ++ res.keys ++ gap ++ rest₂ := by + have hsplit := keys_eq_filter_lt_append_filter_not_lt dbc r.begin.key + rw [← hfilt, hfB] at hsplit + rw [hsplit, hrest₂] + have hrest₂' : ∀ x ∈ rest₂, r.begin.key ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [hrest₂, List.mem_filter] at hx + exact le_of_not_gt (of_decide_eq_true hx.2) + exact clampHigh_B_compatible_of_shape hread hcell hcap hkeys₂ hpad hgap hrest₂' + hgapcond hcomp.1 + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +end BeginKeySound + +section BeginKeyNecessary + +open scoped Classical + +variable {db : DB} {r : RangeRead} {res : Result} + +/-- In the `U = begin.key` branch the reported keys themselves form a compatible +database: the begin selector clamps to `⊥` and the end selector overshoots. -/ +theorem clampHigh_B_keys_compatible {hsort : res.keys.SortedLT} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) : + RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hclamp := hcell.2.1 + have hfinite := hcell.2.2 + have hwl : RangeRead.withinLimit r res.keys := by + obtain ⟨db0, hcomp0⟩ := hread + exact RangeRead.compatible_withinLimit hcomp0 + set db : DB := ⟨res.keys, hsort⟩ with hdb + have hdbkeys : db.keys = res.keys := rfl + have hgt : ∀ k ∈ res.keys, r.end_.key < ((k : Key) : WithTop Key) := + high_reported_gt_end hsort hne hEH + have hcE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + r.end_.key)) = 0 := by + rw [hdbkeys] + rw [List.countP_eq_zero] + intro k hk + rw [decide_eq_true_eq] + exact not_lt.mpr (le_of_lt (hgt k hk)) + have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = reportedBelowBegin r res := rfl + have hneg : r.begin.anchor db < 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcB] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hbot : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hneg + have hepos : 1 ≤ r.end_.offset := by omega + have hanchorE : (r.end_.anchor db).toNat = (r.end_.offset - 1).toNat := by + rw [anchor_toNat_eq (s := r.end_) hepos, hcE, Nat.zero_add] + have hEtop : resolve db r.end_ = (⊤ : WithTop Key) := by + have hnonneg : 0 ≤ r.end_.anchor db := by + unfold Selector.anchor + omega + refine resolve_eq_top hnonneg ?_ + rw [hanchorE, ← hfinite, hdbkeys] + have hck : res.keys = RangeRead.completeKeys db r := by + rw [completeKeys_eq_inRange_of_not_rev hrev] + unfold RangeRead.inRange + rw [hbot, hEtop] + have htrue : db.keys.filter (fun k : Key => decide ((([] : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = + db.keys := by + rw [List.filter_eq_self] + intro k _ + rw [decide_eq_true_eq] + exact ⟨coe_nil_le_key_withTop _, WithTop.coe_lt_top k⟩ + rw [htrue, hdbkeys] + exact ⟨hwl, Or.inr ⟨hmore, hck⟩⟩ + +end BeginKeyNecessary + +section BeginKeyNecessaryTools + +open scoped Classical + +variable {db : DB} {r : RangeRead} {res : Result} + +/-- The threshold splits as `C` away from the bottom and `C + 1` at the bottom. -/ +theorem clampHigh_thresh_of_ne_bot + (hE : r.end_.key ≠ (([] : Key) : WithTop Key)) : + clampHighThresh r res = clampHighC r res := by + unfold clampHighThresh + rw [if_neg hE] + exact Nat.add_zero _ + +theorem clampHigh_thresh_of_bot + (hE : r.end_.key = (([] : Key) : WithTop Key)) : + clampHighThresh r res = clampHighC r res + 1 := by + unfold clampHighThresh + rw [if_pos hE] + +/-- Away from the gap, a key above the last reported key is at or above +`keyAfter (last reported)`. -/ +theorem clampHigh_B_gap_of_not_le_last {k : Key} + (hkL : ¬ ((k : Key) : WithTop Key) ≤ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ ((k : Key) : WithTop Key) := by + exact le_of_not_gt (fun h => hkL (WithTop.coe_le_coe.mpr + ((lt_keyAfter_iff (k := k) (l := res.keys.getLastD ([] : Key))).mp + (WithTop.coe_lt_coe.mp h)))) + +/-- The begin anchor of the reported-keys database is negative in the clamping branch. -/ +theorem clampHigh_B_keys_anchor_neg {hsort : res.keys.SortedLT} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) : + r.begin.anchor (⟨res.keys, hsort⟩ : DB) < 0 := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hclamp := hcell.2.1 + have hcB : (⟨res.keys, hsort⟩ : DB).keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := rfl + rw [anchor_eq_count_sub_slack (⟨res.keys, hsort⟩ : DB) r (le_of_lt hbegin), hcB] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + +end BeginKeyNecessaryTools + +section BeginKeyGapNecessary + +open scoped Classical + +variable {db : DB} {r : RangeRead} {res : Result} + +/-- **Every gap key is necessary when `end.key` is the empty key.** Build a +compatible database with the reported keys and exactly `C` gap keys avoiding `k` +(possible because the threshold is `C + 1`), then insert `k`: the gap grows to `C + 1`, +which no compatible database admits. -/ +theorem clampHigh_B_necessary_gap_bot {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + (hEbot : r.end_.key = (([] : Key) : WithTop Key)) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) + (hkK : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ + ((k : Key) : WithTop Key)) + (hknot : k ∉ res.keys) : + Necessary r res k := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hclamp := hcell.2.1 + have hfinite := hcell.2.2 + obtain ⟨db0, hcomp0⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + have hwl := RangeRead.compatible_withinLimit hcomp0 + have hread_keep : Readable r res := ⟨db0, hcomp0⟩ + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set B : WithTop Key := r.begin.key with hB + set C : ℕ := clampHighC r res with hC + set t : ℕ := res.keys.length with ht + have hth : clampHighThresh r res = C + 1 := by + rw [clampHigh_thresh_of_bot (r := r) (res := res) hEbot, hC] + have hcap' : ((C + 1 : ℕ) : ℕ∞) ≤ clampHighCap r res := by + rw [← hth]; exact hcap + have hcapK : ((C + 1 : ℕ) : ℕ∞) ≤ + intervalCard ((K : Key) : WithTop Key) B := by + simpa [clampHighCap, hK, hL, hB] using hcap' + obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ := + exists_db_keysIn_avoid (B := ((K : Key) : WithTop Key)) (E := B) (k := k) (m := C) hcapK + have hJlo : ∀ x ∈ dbJ.keys, ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + fun x hx => (hJrange x hx).1 + have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < B := + fun x hx => (hJrange x hx).2 + have hcross : ∀ a ∈ res.keys, ∀ b ∈ dbJ.keys, a < b := by + intro a ha b hb + have haL : a ≤ L := by + rw [hL, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_getLast ha + have haK : a < K := by + rw [hK] + exact lt_of_le_of_lt haL (keyAfter_self_lt L) + have hKb : ((K : Key) : WithTop Key) ≤ ((b : Key) : WithTop Key) := hJlo b hb + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr haK) hKb) + have hsortedA : (res.keys ++ dbJ.keys).SortedLT := + List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hcross⟩ + let dbA : DB := ⟨res.keys ++ dbJ.keys, hsortedA⟩ + have hcompA : RangeRead.Compatible dbA r res := by + refine clampHigh_B_compatible_of_shape (db := dbA) hread_keep hcell hcap + (pad := []) (gap := dbJ.keys) (rest := []) (hwl := hwl) ?_ ?_ ?_ ?_ ?_ + · simp [dbA] + · intro x hx; exact absurd hx (by simp) + · intro x hx; exact ⟨hJlo x hx, hJhi x hx⟩ + · intro x hx; exact absurd hx (by simp) + · left + rw [hJlen, hC] + have hkA : k ∉ dbA.keys := by simp [dbA, hknot, hkJ] + let db1 : DB := dbA.insert k hkA + have hkeys1 : db1.keys = (res.keys ++ dbJ.keys).orderedInsert (· ≤ ·) k := by + simp [db1, dbA] + have hall1 : ∀ x ∈ db1.keys, ((x : Key) : WithTop Key) < B := by + intro x hx + rw [hkeys1, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hkB + · rw [List.mem_append] at hx + rcases hx with hx | hx + · exact by simpa [hB] using clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx + · exact hJhi x hx + have hcnt1 : db1.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + t + (C + 1) := by + rw [List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact hall1 x hx)] + rw [hkeys1, List.orderedInsert_length, List.length_append, hJlen] + simp only [ht] + omega + have hnot : ¬ RangeRead.Compatible db1 r res := by + intro hcomp1 + obtain ⟨pad', gap1, rest', -, hkeys', hpad', hgap', hrest', hgapcond'⟩ := + clampHigh_B_decomp_of_compatible hread_keep hcell hcap hcomp1 + have hpadnil : pad' = [] := by + by_contra hne' + obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil pad' hne' + have hlt := hpad' x hx + rw [hEbot] at hlt + exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key x))) + have hpadlen : pad'.length = 0 := by rw [hpadnil]; rfl + have hEB : r.end_.key < B := lt_trans hEH hcrossD + have hcount' : db1.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + pad'.length + t + gap1.length := by + rw [hkeys', List.countP_append, List.countP_append, List.countP_append] + have hp : pad'.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + pad'.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq] + exact lt_trans (hpad' x hx) hEB) + have hr : res.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + res.keys.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq] + exact by + simpa [hB] using + clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx) + have hg : gap1.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = + gap1.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact (hgap' x hx).2) + have hs : rest'.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) = 0 := + List.countP_eq_zero.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact not_lt.mpr (hrest' x hx)) + rw [hp, hr, hg, hs, ht] + simp + have hcnt_eq : pad'.length + t + gap1.length = t + (C + 1) := by + rw [← hcount', hcnt1] + rw [hpadlen, Nat.zero_add] at hcnt_eq + have hgap1 : gap1.length = C + 1 := by omega + rcases hgapcond' with hgC | ⟨hgLt, -⟩ + · omega + · omega + exact necessary_of_toggle ⟨dbA, hcompA, by rwa [toggle_eq_insert_of_not_mem hkA]⟩ + +end BeginKeyGapNecessary + +/-- **Every gap key is necessary when `end.key` is not the empty key.** Build a +compatible database with one pad key below `end.key` and exactly `C` gap keys +containing `k` (possible because the threshold is `C`), so erasing `k` leaves a +nonempty pad with a too-short gap, which no compatible database admits. -/ +theorem clampHigh_B_necessary_gap_ne_bot {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + (hE : r.end_.key ≠ (([] : Key) : WithTop Key)) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) + (hkK : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ + ((k : Key) : WithTop Key)) + (hknot : k ∉ res.keys) : + Necessary r res k := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + have hclamp := hcell.2.1 + obtain ⟨db0, hcomp0⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + have hwl := RangeRead.compatible_withinLimit hcomp0 + have hread_keep : Readable r res := ⟨db0, hcomp0⟩ + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + set K : Key := keyAfter L with hK + set B : WithTop Key := r.begin.key with hB + set C : ℕ := clampHighC r res with hC + set t : ℕ := res.keys.length with ht + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hHL : H ≤ L := by + rw [hH, hL, headD_eq_head_of_ne hne, getLastD_eq_getLast_of_ne hne] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hHL' : ((H : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := WithTop.coe_le_coe.mpr hHL + have hLK : ((L : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by + rw [hK]; exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt L) + have hE_lt_K : r.end_.key < ((K : Key) : WithTop Key) := + lt_of_lt_of_le hEH (le_trans hHL' (le_of_lt hLK)) + have hres_ge_H : ∀ x ∈ res.keys, ((H : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by + intro x hx + rw [hH, headD_eq_head_of_ne hne] + exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hsort hne hx) + have hres_lt_K : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < ((K : Key) : WithTop Key) := by + intro x hx + have hxL : ((x : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) := by + rw [hL, getLastD_eq_getLast_of_ne hne] + exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx) + exact lt_of_le_of_lt hxL hLK + have hnilE : ((([] : Key) : WithTop Key)) < r.end_.key := + lt_of_le_of_ne (coe_nil_le_key_withTop _) (Ne.symm hE) + have hnil_lt_K : ((([] : Key) : WithTop Key)) < ((K : Key) : WithTop Key) := + lt_trans hnilE hE_lt_K + have hth : clampHighThresh r res = C := by + rw [clampHigh_thresh_of_ne_bot (r := r) (res := res) hE, hC] + have hcap' : (C : ℕ∞) ≤ clampHighCap r res := by rw [← hth]; exact hcap + have hcapK : (C : ℕ∞) ≤ intervalCard ((K : Key) : WithTop Key) B := by + simpa [clampHighCap, hK, hL, hB] using hcap' + have hCpos : 0 < C := clampHigh_C_pos (r := r) (res := res) hclamp + have hcapsub : ((C - 1 + 1 : ℕ) : ℕ∞) ≤ intervalCard ((K : Key) : WithTop Key) B := by + have hsub : C - 1 + 1 = C := by omega + rw [hsub]; exact hcapK + obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ := + exists_db_keysIn_avoid (B := ((K : Key) : WithTop Key)) (E := B) (k := k) (m := C - 1) hcapsub + have hJlo : ∀ x ∈ dbJ.keys, ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + fun x hx => (hJrange x hx).1 + have hJhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < B := + fun x hx => (hJrange x hx).2 + set dbG : DB := dbJ.insert k hkJ with hdbG + have hGkeys : dbG.keys = dbJ.keys.orderedInsert (· ≤ ·) k := DB.insert_keys dbJ k hkJ + have hGrange : ∀ x ∈ dbG.keys, ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B := by + intro x hx + rw [hGkeys, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact ⟨hkK, hkB⟩ + · exact ⟨hJlo x hx, hJhi x hx⟩ + have hGlen : dbG.keys.length = C := by + rw [hGkeys, List.orderedInsert_length, hJlen] + omega + have hkG : k ∈ dbG.keys := by + rw [hGkeys]; exact (List.mem_orderedInsert (· ≤ ·)).mpr (Or.inl rfl) + have hnil_lt_res : ∀ b ∈ res.keys, ([] : Key) < b := by + intro b hb + have hlt : ((([] : Key) : WithTop Key)) < ((b : Key) : WithTop Key) := + lt_of_lt_of_le (lt_trans hnilE hEH) (hres_ge_H b hb) + exact WithTop.coe_lt_coe.mp hlt + have hnil_lt_gap : ∀ b ∈ dbG.keys, ([] : Key) < b := by + intro b hb + have hKb : ((K : Key) : WithTop Key) ≤ ((b : Key) : WithTop Key) := (hGrange b hb).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hnil_lt_K hKb) + have hcross_res_gap : ∀ a ∈ res.keys, ∀ b ∈ dbG.keys, a < b := by + intro a ha b hb + have haK : ((a : Key) : WithTop Key) < ((K : Key) : WithTop Key) := hres_lt_K a ha + have hKb : ((K : Key) : WithTop Key) ≤ ((b : Key) : WithTop Key) := (hGrange b hb).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le haK hKb) + have hsing : ([[]] : List Key).SortedLT := + List.sortedLT_cons.mpr ⟨by intro b hb; exact absurd hb (by simp), List.sortedLT_nil⟩ + have hcross_nil : ∀ a ∈ ([[]] : List Key), ∀ b ∈ res.keys ++ dbG.keys, a < b := by + intro a ha b hb + simp only [List.mem_singleton] at ha + subst ha + rw [List.mem_append] at hb + rcases hb with hb | hb + · exact hnil_lt_res b hb + · exact hnil_lt_gap b hb + have hinner : (res.keys ++ dbG.keys).SortedLT := + List.sortedLT_append.mpr ⟨hsort, dbG.sorted, hcross_res_gap⟩ + have hsortedA : ([[]] ++ res.keys ++ dbG.keys).SortedLT := by + rw [List.append_assoc] + exact List.sortedLT_append.mpr ⟨hsing, hinner, hcross_nil⟩ + let dbA : DB := ⟨[[]] ++ res.keys ++ dbG.keys, hsortedA⟩ + have hkeysA : dbA.keys = [[]] ++ res.keys ++ dbG.keys := rfl + have hcompA : RangeRead.Compatible dbA r res := by + refine clampHigh_B_compatible_of_shape (db := dbA) hread_keep hcell hcap + (pad := [[]]) (gap := dbG.keys) (rest := []) (hwl := hwl) ?_ ?_ ?_ ?_ ?_ + · simp [hkeysA] + · intro x hx + rw [List.mem_singleton] at hx + subst hx + exact hnilE + · intro x hx; exact hGrange x hx + · intro x hx; exact absurd hx (by simp) + · left; exact hGlen + have hkA : k ∈ dbA.keys := by + rw [hkeysA] + exact List.mem_append_right ([[]] ++ res.keys) hkG + have hk_nil : k ≠ ([] : Key) := by + intro h + have hlt := hnil_lt_gap k hkG + rw [h] at hlt + exact absurd hlt (lt_irrefl _) + have hk_not_padres : k ∉ ([[]] : List Key) ++ res.keys := by + rw [List.mem_append] + rintro (h | h) + · rw [List.mem_singleton] at h; exact hk_nil h + · exact hknot h + have hdb1keys : (dbA.erase k).keys = [[]] ++ res.keys ++ dbG.keys.erase k := by + rw [DB.erase_keys] + change (([[]] ++ res.keys ++ dbG.keys).erase k) = [[]] ++ res.keys ++ dbG.keys.erase k + rw [List.erase_append_right dbG.keys hk_not_padres] + have hall1 : ∀ x ∈ (dbA.erase k).keys, ((x : Key) : WithTop Key) < B := by + intro x hx + rw [hdb1keys, List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · rw [List.mem_singleton] at hx + subst hx + exact lt_trans hnilE (lt_trans hEH hcrossD) + · exact by + simpa [hB] using + clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx + · exact (hGrange x (List.mem_of_mem_erase hx)).2 + have hlenA : dbA.keys.length = 1 + t + C := by + rw [hkeysA, List.length_append, List.length_append, hGlen, ht] + simp + have hcnt1 : (dbA.erase k).keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = + t + C := by + rw [List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact hall1 x hx), + DB.erase_keys, List.length_erase_of_mem hkA, hlenA] + omega + have hnot : ¬ RangeRead.Compatible (dbA.erase k) r res := by + intro hcomp1 + obtain ⟨pad', gap1, rest', -, hkeys', hpad', hgap', hrest', hgapcond'⟩ := + clampHigh_B_decomp_of_compatible hread_keep hcell hcap hcomp1 + have hcount' : (dbA.erase k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = + pad'.length + t + gap1.length := by + rw [hkeys', List.countP_append, List.countP_append, List.countP_append] + have hp : pad'.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = + pad'.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq] + exact lt_trans (lt_trans (hpad' x hx) hEH) hcrossD) + have hr : res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = + res.keys.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq] + exact by + simpa [hB] using + clampHigh_reported_lt_begin (r := r) (res := res) hsort hne hcell hcap x hx) + have hg : gap1.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = + gap1.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact (hgap' x hx).2) + have hs : rest'.countP (fun x : Key => decide (((x : Key) : WithTop Key) < B)) = 0 := + List.countP_eq_zero.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact not_lt.mpr (hrest' x hx)) + rw [hp, hr, hg, hs, ht] + simp + have hpadgap : pad'.length + gap1.length = C := by + have h := hcount'.symm.trans hcnt1 + omega + have hcntK : (dbA.erase k).keys.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = C - 1 := by + rw [hdb1keys, List.countP_append, List.countP_append] + have hnil : ([[]] : List Key).countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [List.mem_singleton] at hx + subst hx + rw [decide_eq_true_eq] + exact fun hcon => absurd hcon.1 (not_le.mpr hnil_lt_K) + have hres : res.keys.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact fun hcon => absurd hcon.1 (not_le.mpr (hres_lt_K x hx)) + have hG : (dbG.keys.erase k).countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = C - 1 := by + rw [List.countP_erase] + have hall : dbG.keys.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = dbG.keys.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact hGrange x hx) + rw [hall, hGlen] + rw [if_pos ⟨hkG, by rw [decide_eq_true_eq]; exact ⟨hkK, hkB⟩⟩] + rw [hnil, hres, hG] + omega + have hcnt_decomp : (dbA.erase k).keys.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = gap1.length := by + rw [hkeys', List.countP_append, List.countP_append, List.countP_append] + have hp : pad'.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact fun hcon => absurd hcon.1 (not_le.mpr (lt_trans (hpad' x hx) hE_lt_K)) + have hr : res.keys.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact fun hcon => absurd hcon.1 (not_le.mpr (hres_lt_K x hx)) + have hg : gap1.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = gap1.length := + List.countP_eq_length.mpr (by + intro x hx; rw [decide_eq_true_eq]; exact hgap' x hx) + have hs : rest'.countP (fun x : Key => decide + (((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < B)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact fun hcon => absurd hcon.2 (not_lt.mpr (hrest' x hx)) + rw [hp, hr, hg, hs] + simp + have hgap1 : gap1.length = C - 1 := hcnt_decomp.symm.trans hcntK + rcases hgapcond' with hgC | ⟨hgLt, hpadnil⟩ + · omega + · rw [hpadnil] at hpadgap + simp at hpadgap + omega + exact necessary_of_toggle ⟨dbA, hcompA, by + rw [toggle_eq_erase_of_mem hkA] + exact hnot⟩ + +/-- **Every key below `begin.key` is necessary in the `U = begin.key` branch.** Split +by position relative to the reported head and last keys: below the head a clamping +witness makes the key necessary, between them it is reported (so necessary), and +strictly above the last reported key it lies in the gap (handled by the two toggle +constructions, according to whether `end.key` is the empty key). -/ +theorem clampHigh_B_necessary {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) + {k : Key} (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hEH⟩ := hcell.1 + obtain ⟨db0, hcomp0⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev + have hread_keep : Readable r res := ⟨db0, hcomp0⟩ + set H : Key := res.keys.headD ([] : Key) with hH + set L : Key := res.keys.getLastD ([] : Key) with hL + have hHmem : H ∈ res.keys := by rw [hH]; exact headD_mem hne + have hLmem : L ∈ res.keys := by rw [hL]; exact getLastD_mem hne + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread_keep hkres + · by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) + · refine necessary_below_head_of_anchor_nonpos_negCross (⟨res.keys, hsort⟩ : DB) + (clampHigh_B_keys_compatible (hsort := hsort) hread_keep hcell) hrev hne + (clampHigh_B_keys_anchor_neg (hsort := hsort) hread_keep hcell) ?_ + rw [← headD_eq_head_of_ne hne, ← hH] + exact hkH + · have hHk : ((H : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkH + by_cases hkL : ((k : Key) : WithTop Key) ≤ ((L : Key) : WithTop Key) + · exact necessary_of_between_result hread_keep hHmem hLmem + (WithTop.coe_le_coe.mp hHk) (WithTop.coe_le_coe.mp hkL) + · have hkK : (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) ≤ + ((k : Key) : WithTop Key) := + clampHigh_B_gap_of_not_le_last (k := k) (by rw [← hL]; exact hkL) + by_cases hE : r.end_.key = (([] : Key) : WithTop Key) + · exact clampHigh_B_necessary_gap_bot hread_keep hcell hcap hE hkB hkK hkres + · exact clampHigh_B_necessary_gap_ne_bot hread_keep hcell hcap hE hkB hkK hkres + +/-- **The `U = begin.key` branch of the clamping finite crossed high cell is +minimal.** -/ +theorem minimal_clampHigh_B {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) + (hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res)) : + Minimal r res ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := + (minimal_iff_sound_and_necessary).mpr ⟨clampHigh_B_sound hread hcell hcap, + fun _ hk => clampHigh_B_necessary hread hcell hcap (ConflictRange.mem_iff.mp hk).2⟩ + +/-- **The clamping finite branch of the crossed negative-begin complete high cell is +closed**: the closed-form offset rule is minimal there. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh_clampFinite {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighClampFinite r res) : + Minimal r res (closedFormNegCrossCompleteHigh r res) := by + rw [closedForm_clampFinite_eq hcell] + by_cases hcap : ((clampHighThresh r res : ℕ∞) ≤ clampHighCap r res) + · have hu : clampHighU r res = r.begin.key := by + unfold clampHighU; rw [ite_eq_left hcap] + rw [hu] + exact minimal_clampHigh_B hread hcell hcap + · have hu : clampHighU r res = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key) : WithTop Key)) := by + unfold clampHighU; rw [ite_eq_right hcap] + rw [hu] + exact minimal_clampHigh_K hread hcell hcap + +/-- **The crossed negative-begin complete high cell is closed in every branch**: the +clamping low and finite branches and the non-clamping low and finite branches exhaust +the cell. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHigh r res) : + Minimal r res (closedFormNegCrossCompleteHigh r res) := by + have hhigh : negCrossCompleteHigh r res := hcell + obtain ⟨hrev, hbegin, hmore, hne, hend2, hcrossD, hEH⟩ := hcell + have htle := high_length_le_end hread hrev hmore hne hend2 hEH + by_cases hclamp : reportedBelowBegin r res ≤ beginSlack r + · by_cases hfin : res.keys.length = (r.end_.offset - 1).toNat + · exact minimal_closedForm_of_negCrossCompleteHigh_clampFinite hread + ⟨hhigh, hclamp, hfin⟩ + · have hlow : res.keys.length < (r.end_.offset - 1).toNat := by omega + exact minimal_closedForm_of_negCrossCompleteHigh_clampLow hread + ⟨hhigh, hclamp, hlow⟩ + · have hnc : beginSlack r < reportedBelowBegin r res := not_le.mp hclamp + by_cases hfin : res.keys.length = (r.end_.offset - 1).toNat + · exact minimal_closedForm_of_negCrossCompleteHigh_nonclampFinite hread + ⟨hhigh, hnc, hfin⟩ + · have hlow : res.keys.length < (r.end_.offset - 1).toNat := by omega + exact minimal_closedForm_of_negCrossCompleteHigh_nonclampLow hread + ⟨hhigh, hnc, hlow⟩ + +/-- The reversed traversal of the crossed negative-begin complete high cell: flipping +the read gives the forward high cell. -/ +abbrev negCrossCompleteHighRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ res.more = false ∧ res.keys ≠ [] ∧ + negCrossCompleteHigh { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- The reversed crossed negative-begin complete high closed form. -/ +noncomputable def closedFormNegCrossCompleteHighRev (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossCompleteHigh { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- **The reversed crossed negative-begin complete high cell is minimal**, by flipping +the traversal and applying the forward rule. -/ +theorem minimal_closedForm_of_negCrossCompleteHigh_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossCompleteHighRev r res) : + Minimal r res (closedFormNegCrossCompleteHighRev r res) := by + obtain ⟨hrev, hmore, hne, hhigh⟩ := hcell + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCrossCompleteHigh hread' hhigh + exact (minimal_flip (b := true) hrev hmore).mp hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossLongNonpos.lean b/FormalConflictRange/NegCrossLongNonpos.lean new file mode 100644 index 0000000..4af6834 --- /dev/null +++ b/FormalConflictRange/NegCrossLongNonpos.lean @@ -0,0 +1,388 @@ +/- +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.NegCrossResidual + +/-! +# The crossed negative-begin complete long cell at a nonpositive end offset + +A forward complete result with a negative begin offset, `end.offset ≤ 0`, the end key +*below* the begin key (`end_.key < begin_.key`), reported keys below the begin key, and no +feasible clamping database. Every compatible database is nonclamping and resolves the +begin selector onto the reported head; the end selector stays at or below `end_.key`, so +agreement on `[head, begin_.key)` fixes the whole complete window. Every key from the head +up to `begin_.key` is necessary: the reported keys by convexity, keys below `end_.key` by +`necessary_last_to_end_key`, and keys in `[end_.key, begin_.key)` because inserting one +lifts the begin resolution above the reported head. + +The minimal conflict range is therefore `[head, begin_.key)`. +-/ + +namespace FormalConflictRange + +/-- **The capacity bound from a negative begin anchor, in the long cell.** In the crossed +complete cell with `end_.key < begin_.key` and `end.offset ≤ 0`, a clamping compatible +database forces `clampNonposBottom`. The interval `[begin.key, end.key)` contributes +nothing, so the stored keys split into the reported keys below `resolve end` and the +`d + 1` pad keys in `[resolve end, end.key)`, all below `begin.key`. -/ +theorem clampNonposBottom_of_anchor_neg_long {r : RangeRead} {res : Result} {db : DB} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hlong : r.end_.key < r.begin.key) + (hanchor : r.begin.anchor db < 0) : + clampNonposBottom r res := by + have hL : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + have hres : res.keys = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · rw [heq, RangeRead.completeKeys, if_neg (by simp [hrev])] + unfold RangeRead.inRange + rw [hL] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + constructor + · intro h; exact h.2 + · intro h; exact ⟨coe_nil_le_key_withTop _, h⟩ + have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + apply hne + rw [hres, hR] + rw [List.filter_eq_nil_iff] + intro k _ + simp only [decide_eq_true_eq] + exact not_lt.mpr (coe_nil_le_key_withTop _) + have hRE : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hlen : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := + between_resolve_end_length hend hRne + have htR : res.keys.length = cnt db (resolve db r.end_) := by + rw [hres, cnt, List.countP_eq_length_filter] + have hrep : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + rw [hres] at hk + have hk' : ((k : Key) : WithTop Key) < resolve db r.end_ := + of_decide_eq_true (List.mem_filter.mp hk).2 + exact lt_trans hk' (lt_of_le_of_lt hRE hlong) + have hcB : cnt db r.begin.key ≤ beginSlack r := by + have h := hanchor + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] at h + unfold cnt + omega + have hRB : resolve db r.end_ ≤ r.begin.key := le_trans hRE (le_of_lt hlong) + have hEB : r.end_.key ≤ r.begin.key := le_of_lt hlong + have h1 : cnt db r.end_.key = + cnt db (resolve db r.end_) + (between db (resolve db r.end_) r.end_.key).length := + cnt_eq_add_between db hRE + have h2 : cnt db r.begin.key = + cnt db r.end_.key + (between db r.end_.key r.begin.key).length := + cnt_eq_add_between db hEB + have hbd : (clampNonposD r + 1 : ℕ) ≤ beginSlack r - reportedBelowBegin r res := by + rw [hrep] + have h3 : res.keys.length + (clampNonposD r + 1) ≤ cnt db r.begin.key := by + rw [← htR, hlen] at h1 + omega + omega + have hcap0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le (le_of_lt hlong) + unfold clampNonposBottom + rw [hrep, hcap0] + have hnat : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + have := hbd + 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 + +/-- The nonclamping crossed negative-begin complete long cell at a nonpositive end +offset: `end_.key < begin.key`. -/ +abbrev negCrossNonclampLongNonpos (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res ∧ + r.end_.key < r.begin.key + +/-- The closed form for the nonclamping crossed negative-begin complete long cell at a +nonpositive end offset: `[head, begin.key)`. -/ +noncomputable def closedFormNegCrossNonclampLongNonpos (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ + +/-- **The nonclamping crossed negative-begin complete long cell at a nonpositive end +offset is minimal.** Failure of the capacity bound makes every compatible database +nonclamping. The complete window is `[head, resolve end)`, so agreement on +`[head, begin.key)` fixes it. Every key from the head up to `begin.key` is necessary: +reported keys by convexity, keys below `end_.key` by `necessary_last_to_end_key`, and keys +in `[end_.key, begin.key)` because the capacity failure leaves `[end_.key, begin.key)` +empty in any compatible database, while inserting one lifts the begin resolution above +the reported head. -/ +theorem minimal_closedForm_of_negCross_nonclamp_long_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossNonclampLongNonpos r res) : + Minimal r res (closedFormNegCrossNonclampLongNonpos r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hnotbottom, hlong⟩ := hcell + 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 + rw [← hheadD]; exact hcrossD + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead + rw [show closedFormNegCrossNonclampLongNonpos r res = + ⟨((head : Key) : WithTop Key), r.begin.key⟩ by + unfold closedFormNegCrossNonclampLongNonpos; rw [hheadD']] + have hnonclamp : ∀ db, RangeRead.Compatible db r res → 0 ≤ r.begin.anchor db := by + intro db hcomp + by_contra hneg + push_neg at hneg + exact hnotbottom + (clampNonposBottom_of_anchor_neg_long hcomp hrev hmore hne hbegin hend hlong hneg) + have hread₀ : Readable r res := ⟨db₀, hcomp₀⟩ + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored₀ : head ∈ db₀.keys := + RangeRead.compatible_keys_subset hcomp₀ (by rw [hhead]; exact List.head_mem hne) + have hhead_lt_res₀ : ((head : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hhead_mem)).2.2 + have hhead_le_res₀ : ((head : Key) : WithTop Key) ≤ resolve db₀ r.end_ := le_of_lt hhead_lt_res₀ + have hres_le₀ : resolve db₀ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hhead_lt_E₀ : ((head : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hhead_lt_res₀ hres_le₀ + have hRne₀ : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + rw [hR] at hhead_lt_res₀ + exact absurd hhead_lt_res₀ (not_lt.mpr (coe_nil_le_key_withTop (head : Key))) + have hL₀ : resolve db₀ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_anchor_nonneg hcomp₀ hrev hmore hne (hnonclamp db₀ hcomp₀) + have hres_filters : res.keys = between db₀ (head : WithTop Key) (resolve db₀ r.end_) := by + rw [hres_eq₀] + unfold RangeRead.completeKeys RangeRead.inRange between + rw [ite_eq_right (by simp [hrev]), hL₀] + have hlen_end : (between db₀ (head : WithTop Key) r.end_.key).length = + res.keys.length + (clampNonposD r + 1) := by + have hL1 := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key)) (w₂ := r.end_.key) + (le_of_lt hhead_lt_E₀) + have hL2 := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key)) + (w₂ := resolve db₀ r.end_) hhead_le_res₀ + have hL3 := cnt_eq_add_between db₀ (w₁ := resolve db₀ r.end_) (w₂ := r.end_.key) hres_le₀ + have hL4 : (between db₀ (head : WithTop Key) (resolve db₀ r.end_)).length = + res.keys.length := by rw [← hres_filters] + have hp : (between db₀ (resolve db₀ r.end_) r.end_.key).length = clampNonposD r + 1 := + between_resolve_end_length hend hRne₀ + omega + have hlen_begin : (between db₀ (head : WithTop Key) r.begin.key).length = beginSlack r + 1 := + filter_begin_length_of_anchor_nonneg hcomp₀ hrev hmore hne hbegin hcross + (hnonclamp db₀ hcomp₀) + have hall_reported_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + have hklt := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hk)).2.2 + exact lt_of_lt_of_le hklt hres_le₀ + have hreported_eq : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_trans (hall_reported_lt_E k hk) hlong + have hnat : beginSlack r < res.keys.length + (clampNonposD r + 1) := by + have hcap0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le (le_of_lt hlong) + have hnotle : ¬ ((reportedBelowBegin r res : ℕ∞) + + (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ (beginSlack r : ℕ∞)) := by + intro hle + apply hnotbottom + unfold clampNonposBottom + rw [hcap0, tsub_zero] + exact hle + have hltE : (beginSlack r : ℕ∞) < (reportedBelowBegin r res : ℕ∞) + + (((clampNonposD r + 1 : ℕ)) : ℕ∞) := not_le.mp hnotle + rw [hreported_eq, ← ENat.natCast_add] at hltE + exact ENat.natCast_lt_natCast.mp hltE + have hP0 : (between db₀ r.end_.key r.begin.key).length = 0 := by + have hb := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key)) (w₂ := r.begin.key) + (le_trans (le_of_lt hhead_lt_E₀) (le_of_lt hlong)) + have he := cnt_eq_add_between db₀ (w₁ := (head : WithTop Key)) (w₂ := r.end_.key) + (le_of_lt hhead_lt_E₀) + have hbe := cnt_eq_add_between db₀ (w₁ := r.end_.key) (w₂ := r.begin.key) (le_of_lt hlong) + rw [hlen_begin] at hb + rw [hlen_end] at he + omega + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.begin.key⟩ db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored : head ∈ db₁.keys := + RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne) + have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_anchor_nonneg hcomp₁ hrev hmore hne (hnonclamp db₁ hcomp₁) + have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by + simpa [AgreeOn, ConflictRange.keys] using hagree + have hfiltE := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := r.end_.key) (E := r.begin.key) (le_of_lt hlong) hfilt + have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := + filter_begin_length_of_anchor_nonneg hcomp₁ hrev hmore hne hbegin hcross + (hnonclamp db₁ hcomp₁) + have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := + List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩ + have hhead_stored₂ : head ∈ db₂.keys := + List.mem_of_mem_filter (by rw [← hfilt]; exact hhead_in₁) + have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by rw [← hfilt]; exact hlen₁ + have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂ + have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db₁ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.2 + have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hhead_lt_E : ((head : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hhead_lt_res hres_le + have hR : resolve db₂ r.end_ = resolve db₁ r.end_ := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₁) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩ + · 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 hqne : q ≠ [] := by + intro hqnil + rw [hqnil] at hqeq + rw [hqeq] at hhead_lt_res + exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _)) + have hqspec : (db₁.keys.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < r.end_.key))).reverse[ + (-r.end_.offset).toNat]? = some q := + resolve_filter_reverse_eq_some_of_ne_nil hend hqne hqeq + have hqa : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := by + rw [← hqeq]; exact le_of_lt hhead_lt_res + have hR' : resolve db₂ r.end_ = ((q : Key) : WithTop Key) := + resolve_eq_of_agree_between (db₁ := db₁) (db₂ := db₂) (s := r.end_) hend rfl + (le_of_lt hhead_lt_E) hfiltE hqspec hqa + rw [hR', hqeq] + have hP : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) := + filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := resolve db₁ r.end_) (E := r.end_.key) hres_le hfiltE + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), hL₁, hL₂, hR] + exact hP + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_head : head ≤ k := WithTop.coe_le_coe.mp hlo + have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := hhi + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hread₀ hhead_mem (List.getLast_mem hne) hk_head hklast + · have hk_gt_last : res.keys.getLast hne < k := lt_of_not_ge hklast + have hhead_le_last : head ≤ res.keys.getLast hne := by + have hsrt : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + rw [hhead, List.head_eq_getElem hne, List.getLast_eq_getElem hne] + exact hsrt.getElem_le_getElem_iff.mpr (by + have := List.length_pos_of_ne_nil hne + omega) + have hk_gt_head : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last + by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key + · exact fun cr hs => necessary_last_to_end_key hread₀ hrev hmore hne (by omega) + cr hs (le_of_lt hk_gt_last) hkE + · have hkE_le : r.end_.key ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkE + intro cr hs + by_contra hkcr + have hknot : k ∉ db₀.keys := by + intro hkdb + have hkmem : k ∈ between db₀ r.end_.key r.begin.key := by + unfold between + exact List.mem_filter.mpr ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hkE_le, hk_lt_B⟩⟩ + have hpos : 0 < (between db₀ r.end_.key r.begin.key).length := + List.length_pos_of_mem hkmem + omega + have hgt : ((head : Key) : WithTop Key) < resolve (db₀.insert k hknot) r.begin := + resolve_begin_insert_gt hbegin hhead_stored₀ hcross hknot hk_gt_head hk_lt_B hlen_begin + have hhead_notin : head ∉ RangeRead.completeKeys (db₀.insert k hknot) r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + intro hmem + simp only [List.mem_filter, decide_eq_true_eq] at hmem + exact absurd hmem.2.1 (not_le.mpr hgt) + have hcomp₁ : RangeRead.Compatible (db₀.insert k hknot) r res := + (hs db₀ _ (ConflictRange.keys_insert_of_not_mem cr db₀ hknot hkcr).symm).mp hcomp₀ + have hres_eq₁ : res.keys = RangeRead.completeKeys (db₀.insert k hknot) r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + exact hhead_notin (by rw [← hres_eq₁]; exact hhead_mem) + +/-- The reversed nonclamping crossed negative-begin complete long cell at a nonpositive +end offset. -/ +abbrev negCrossNonclampLongNonposRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res ∧ + r.end_.key < r.begin.key + +/-- The reversed closed form for the nonclamping crossed negative-begin complete long +cell at a nonpositive end offset: `[last reported, begin.key)`. -/ +noncomputable def closedFormNegCrossNonclampLongNonposRev (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ + +/-- **The reversed nonclamping crossed negative-begin complete long cell at a nonpositive +end offset is minimal**, by flipping the traversal. -/ +theorem minimal_closedForm_of_negCross_nonclamp_long_nonpos_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossNonclampLongNonposRev r res) : + Minimal r res (closedFormNegCrossNonclampLongNonposRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hmq, hnotbottom, hlong⟩ := hcell + 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 hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hbottom' : clampNonposBottom { r with reverse := false } + { keys := res.keys.reverse, more := false } ↔ clampNonposBottom r res := by + unfold clampNonposBottom + rw [hcount] + rfl + have hcell' : negCrossNonclampLongNonpos { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, ?_, hlong⟩ + · rw [hheadD]; exact hcrossD + · rw [hcount]; exact hmq + · intro h + exact hnotbottom (hbottom'.mp h) + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_nonclamp_long_nonpos hread' hcell' + have hcr : closedFormNegCrossNonclampLongNonpos { r with reverse := false } + { keys := res.keys.reverse, more := false } = + closedFormNegCrossNonclampLongNonposRev r res := by + unfold closedFormNegCrossNonclampLongNonpos closedFormNegCrossNonclampLongNonposRev + rw [hheadD] + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossResidual.lean b/FormalConflictRange/NegCrossResidual.lean new file mode 100644 index 0000000..92f7b6c --- /dev/null +++ b/FormalConflictRange/NegCrossResidual.lean @@ -0,0 +1,432 @@ +/- +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.NegCrossClampNonpos + +/-! +# The crossed negative-begin complete nonpositive-end nonclamping cell + +A forward complete result with a negative begin offset and `end.offset ≤ 0`, where the +begin key is at or below the end key but the reported keys below the begin key exhaust the +begin slack, so no compatible database clamps. The begin selector then resolves onto the +reported head and the minimal conflict range is `[head, end_.key)`. + +The key new ingredient is the converse of the clamping capacity bound: any clamping +compatible database forces `clampNonposBottom`. Its contrapositive says that below that +bound every compatible database is nonclamping, which is what makes `[head, end_.key)` +sound. +-/ + +namespace FormalConflictRange + +/-- In the crossed negative-begin complete cell with `begin.key ≤ end.key`, a clamping +compatible database forces the capacity bound `clampNonposBottom`. -/ +theorem clampNonposBottom_of_anchor_neg {r : RangeRead} {res : Result} {db : DB} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hB_le_E : r.begin.key ≤ r.end_.key) + (hanchor : r.begin.anchor db < 0) : + clampNonposBottom r res := by + have hL : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + have hres : res.keys = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · rw [heq, RangeRead.completeKeys, if_neg (by simp [hrev])] + unfold RangeRead.inRange + rw [hL] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + constructor + · intro h; exact h.2 + · intro h; exact ⟨coe_nil_le_key_withTop _, h⟩ + have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + apply hne + rw [hres, hR] + rw [List.filter_eq_nil_iff] + intro k _ + simp only [decide_eq_true_eq] + exact not_lt.mpr (coe_nil_le_key_withTop _) + have hRE : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hlen : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := + between_resolve_end_length hend hRne + have hcntE : cnt db r.end_.key = + cnt db (resolve db r.end_) + (clampNonposD r + 1) := by + rw [cnt_eq_add_between db hRE, hlen] + have hcntEB : cnt db r.end_.key = + cnt db r.begin.key + (between db r.begin.key r.end_.key).length := + cnt_eq_add_between db hB_le_E + have hcntBq : cnt db r.begin.key ≤ beginSlack r := by + have h := hanchor + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] at h + unfold cnt + omega + -- Split the reported count below `begin.key` by whether `resolve end ≤ begin.key`. + have hm_eq : reportedBelowBegin r res = + (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_ ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := by + unfold reportedBelowBegin + rw [hres, List.countP_eq_length_filter, List.filter_filter] + congr 1 + apply List.filter_congr + intro k _ + simp [Bool.and_comm] + have hmq : reportedBelowBegin r res ≤ beginSlack r := by + by_cases hRB : resolve db r.end_ ≤ r.begin.key + · have hcntB : cnt db r.begin.key = + cnt db (resolve db r.end_) + (between db (resolve db r.end_) r.begin.key).length := + cnt_eq_add_between db hRB + have hmZ : reportedBelowBegin r res = cnt db (resolve db r.end_) := by + rw [hm_eq, cnt, List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.1, fun h => ⟨h, lt_of_lt_of_le h hRB⟩⟩ + rw [hmZ] + omega + · have hBR : r.begin.key < resolve db r.end_ := lt_of_not_ge hRB + have hmY : reportedBelowBegin r res = cnt db r.begin.key := by + rw [hm_eq, cnt, List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨lt_of_lt_of_le h (le_of_lt hBR), h⟩⟩ + rw [hmY] + exact hcntBq + have hdnat : clampNonposD r + 1 ≤ + (between db r.begin.key r.end_.key).length + + (beginSlack r - reportedBelowBegin r res) := by + by_cases hRB : resolve db r.end_ ≤ r.begin.key + · have hcntB : cnt db r.begin.key = + cnt db (resolve db r.end_) + (between db (resolve db r.end_) r.begin.key).length := + cnt_eq_add_between db hRB + have hmZ : reportedBelowBegin r res = cnt db (resolve db r.end_) := by + rw [hm_eq, cnt, List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.1, fun h => ⟨h, lt_of_lt_of_le h hRB⟩⟩ + rw [hmZ] + omega + · have hBR : r.begin.key < resolve db r.end_ := lt_of_not_ge hRB + have hmY : reportedBelowBegin r res = cnt db r.begin.key := by + rw [hm_eq, cnt, List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨lt_of_lt_of_le h (le_of_lt hBR), h⟩⟩ + have hYZ : cnt db r.begin.key ≤ cnt db (resolve db r.end_) := + cnt_le_cnt_of_le db (le_of_lt hBR) + rw [hmY] + omega + -- Convert the natural inequality to the `ℕ∞` capacity bound. + have hcast : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + ((between db r.begin.key r.end_.key).length : ℕ∞) + + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := by + exact_mod_cast hdnat + have hg' : ((between db r.begin.key r.end_.key).length : ℕ∞) ≤ + intervalCard r.begin.key r.end_.key := + between_length_le_intervalCard hB_le_E + have hmain : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard r.begin.key r.end_.key + + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := + le_trans hcast (add_le_add hg' le_rfl) + have htsub : (((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key ≤ + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := by + rw [tsub_le_iff_left] + exact hmain + unfold clampNonposBottom + calc (reportedBelowBegin r res : ℕ∞) + + ((((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key) + ≤ (reportedBelowBegin r res : ℕ∞) + + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := + add_le_add le_rfl htsub + _ = (beginSlack r : ℕ∞) := by + rw [← ENat.natCast_add] + congr 1 + omega + +/-- **Nonclamping resolution onto the head.** A compatible forward complete database whose +begin anchor is nonnegative resolves the begin selector exactly onto the reported head, +without the `beginSlack < reportedBelowBegin` hypothesis of the offset-free form. -/ +theorem resolve_begin_eq_head_of_anchor_nonneg {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hanchor : 0 ≤ r.begin.anchor db) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + set head : Key := res.keys.head hne with hhead + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hhead_ck : head ∈ RangeRead.completeKeys db r := by + rw [← hres_eq]; exact hhead_mem + have hle_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hhead_ck).2.2 + 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 hresolve_get : resolve db r.begin = + ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) := + resolve_eq_getElem hanchor hltlen + set k : Key := db.keys[(r.begin.anchor db).toNat] with hk + have hk_stored : k ∈ db.keys := List.getElem_mem hltlen + have hk_le_head : k ≤ head := WithTop.coe_le_coe.mp (by rw [← hresolve_get]; exact hle_head) + have hk_le : resolve db r.begin ≤ ((k : Key) : WithTop Key) := by rw [hresolve_get] + have hk_lt : ((k : Key) : WithTop Key) < resolve db r.end_ := by + calc ((k : Key) : WithTop Key) = resolve db r.begin := hresolve_get.symm + _ ≤ ((head : Key) : WithTop Key) := hle_head + _ < resolve db r.end_ := hhead_lt_res + have hk_ck : k ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨hk_stored, hk_le, hk_lt⟩ + have hk_mem_res : k ∈ res.keys := by rw [hres_eq]; exact hk_ck + have hk_ge_head : head ≤ k := + (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk_mem_res + have hk_eq : k = head := le_antisymm hk_le_head hk_ge_head + rw [← hk_eq] + exact hresolve_get + +/-- **The begin count identity from a nonnegative anchor.** With a nonnegative begin +anchor exactly `beginSlack r + 1` stored keys lie in `[head, begin.key)`, replacing the +`beginSlack < reportedBelowBegin` hypothesis of `negCross_filter_begin_length`. -/ +theorem filter_begin_length_of_anchor_nonneg {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hanchor : 0 ≤ r.begin.anchor db) : + (db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + 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 + have hcB_ge : beginSlack r + 1 ≤ cB := by + have h : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB] at hanchor + omega + exact_mod_cast h + have hL := resolve_begin_eq_head_of_anchor_nonneg hcomp hrev hmore hne hanchor + have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro htop; rw [hL] at htop + exact absurd htop (WithTop.coe_ne_top (a := 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 hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor hltlen + have hidx : db.keys[(r.begin.anchor db).toNat] = head := by + have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) = + ((head : Key) : WithTop Key) := by rw [← hget, hL] + exact WithTop.coe_injective hcoe + have hrank : db.keys.countP (fun k => decide (k < head)) = (r.begin.anchor db).toNat := + countP_lt_eq_of_getElem db.sorted hltlen hidx + 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 htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by + 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 hcH_add : (db.keys.countP (fun k => decide (k < head))) + (beginSlack r + 1) = cB := by + rw [hrank, htoNat] + exact Nat.sub_add_cancel hcB_ge + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hsplit' : cB = (db.keys.countP (fun k => decide (k < head))) + + (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := by + rw [hcB]; exact hsplit + omega + +/-- A forward complete crossed negative-begin result with `end.offset ≤ 0`, `begin.key ≤ +end.key`, reported keys below the begin key exhausting the begin slack, and no feasible +clamping database. -/ +abbrev negCrossNonclampNonpos (r : RangeRead) (res : Result) : Prop := + r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res + +/-- The closed form for the nonclamping negative-begin complete nonpositive-end cell: +`[head, end_.key)`. -/ +noncomputable def closedFormNegCrossNonclampNonpos (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ + +/-- **The nonclamping negative-begin complete nonpositive-end cell is minimal.** The +capacity bound fails, so every compatible database is nonclamping and resolves the begin +selector onto the reported head; agreement on `[head, end_.key)` fixes the whole complete +window, and every key up to `end_.key` is necessary. -/ +theorem minimal_closedForm_of_negCross_nonclamp_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossNonclampNonpos r res) : + Minimal r res (closedFormNegCrossNonclampNonpos r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcrossD, hmq, hnotbottom⟩ := hcell + 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 + rw [← hheadD]; exact hcrossD + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead + rw [show closedFormNegCrossNonclampNonpos r res = + ⟨((head : Key) : WithTop Key), r.end_.key⟩ by + unfold closedFormNegCrossNonclampNonpos; rw [hheadD']] + have hnonclamp : ∀ db, RangeRead.Compatible db r res → 0 ≤ r.begin.anchor db := by + intro db hcomp + by_contra hneg + push_neg at hneg + exact hnotbottom + (clampNonposBottom_of_anchor_neg hcomp hrev hmore hne hbegin hend hB_le_E hneg) + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hanchor₁ := hnonclamp db₁ hcomp₁ + have hhead_stored : head ∈ db₁.keys := + RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne) + have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_anchor_nonneg hcomp₁ hrev hmore hne hanchor₁ + have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := + filter_begin_length_of_anchor_nonneg hcomp₁ hrev hmore hne hbegin hcross hanchor₁ + have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by + simpa [AgreeOn, ConflictRange.keys] using hagree + have hfiltB := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt + have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := + List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩ + have hhead_stored₂ : head ∈ db₂.keys := + List.mem_of_mem_filter (by rw [← hfiltB]; exact hhead_in₁) + have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by rw [← hfiltB]; exact hlen₁ + have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂ + have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db₁ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.2 + have hres_le : resolve db₁ r.end_ ≤ r.end_.key := + resolve_le_key_of_offset_nonpos hend + have hhead_lt_E : ((head : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hhead_lt_res hres_le + have hR : resolve db₂ r.end_ = resolve db₁ r.end_ := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₁) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩ + · 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 hqne : q ≠ [] := by + intro hqnil + rw [hqnil] at hqeq + rw [hqeq] at hhead_lt_res + exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _)) + have hqspec : (db₁.keys.filter (fun k : Key => + decide (((k : Key) : WithTop Key) < r.end_.key))).reverse[ + (-r.end_.offset).toNat]? = some q := + resolve_filter_reverse_eq_some_of_ne_nil hend hqne hqeq + have hqa : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := by + rw [← hqeq]; exact le_of_lt hhead_lt_res + have hR' : resolve db₂ r.end_ = ((q : Key) : WithTop Key) := + resolve_eq_of_agree_between (db₁ := db₁) (db₂ := db₂) (s := r.end_) hend rfl + (le_of_lt hhead_lt_E) hfilt hqspec hqa + rw [hR', hqeq] + have hP : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) = + db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) := + filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂) + (a := head) (B := resolve db₁ r.end_) (E := r.end_.key) hres_le hfilt + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), hL₁, hL₂, hR] + exact hP + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_le : head ≤ k := WithTop.coe_le_coe.mp hlo + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result ⟨db₀, hcomp₀⟩ hhead_mem (List.getLast_mem hne) + hk_le hklast + · exact fun cr hs => necessary_last_to_end_key ⟨db₀, hcomp₀⟩ hrev hmore hne (by omega) + cr hs (le_of_lt (lt_of_not_ge hklast)) hhi + +/-- The reversed traversal of the nonclamping negative-begin complete nonpositive-end +cell. -/ +abbrev negCrossNonclampNonposRev (r : RangeRead) (res : Result) : Prop := + r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 0 ∧ r.begin.key ≤ r.end_.key ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ ¬ clampNonposBottom r res + +/-- The reversed closed form for the nonclamping negative-begin complete nonpositive-end +cell: `[lastReported, end_.key)`. -/ +noncomputable def closedFormNegCrossNonclampNonposRev (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.end_.key⟩ + +/-- **The reversed nonclamping negative-begin complete nonpositive-end cell is minimal**, +by flipping the traversal and applying the forward rule. -/ +theorem minimal_closedForm_of_negCross_nonclamp_nonpos_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossNonclampNonposRev r res) : + Minimal r res (closedFormNegCrossNonclampNonposRev r res) := by + obtain ⟨hrev, hbegin, hmore, hne, hend, hB_le_E, hcrossD, hmq, hnotbottom⟩ := hcell + 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 hcount : reportedBelowBegin { r with reverse := false } + { keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by + unfold reportedBelowBegin + exact List.Perm.countP_eq _ (List.reverse_perm res.keys) + have hbottom' : clampNonposBottom { r with reverse := false } + { keys := res.keys.reverse, more := false } ↔ clampNonposBottom r res := by + unfold clampNonposBottom + rw [hcount] + rfl + have hcell' : negCrossNonclampNonpos { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + refine ⟨rfl, hbegin, rfl, hne', hend, hB_le_E, ?_, ?_, ?_⟩ + · rw [hheadD]; exact hcrossD + · rw [hcount]; exact hmq + · intro h + exact hnotbottom (hbottom'.mp h) + have hread' : Readable { r with reverse := false } + { keys := res.keys.reverse, more := false } := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩ + have hmin := minimal_closedForm_of_negCross_nonclamp_nonpos hread' hcell' + have hcr : closedFormNegCrossNonclampNonpos { r with reverse := false } + { keys := res.keys.reverse, more := false } = + closedFormNegCrossNonclampNonposRev r res := by + unfold closedFormNegCrossNonclampNonpos closedFormNegCrossNonclampNonposRev + rw [hheadD] + rw [hcr] at hmin + exact (minimal_flip (b := true) hrev hmore).mp hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncAnchor.lean b/FormalConflictRange/NegCrossTruncAnchor.lean new file mode 100644 index 0000000..f732d29 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncAnchor.lean @@ -0,0 +1,185 @@ +/- +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.NegCrossLongNonpos + +/-! +# The truncated negative-begin anchor bound + +The complete negative-begin cells show that a negative begin anchor forces the capacity +bound `clampNonposBottom` (`clampNonposBottom_of_anchor_neg`, +`clampNonposBottom_of_anchor_neg_long`). This module proves the truncated analogue: for a +forward result reporting `more`, a negative begin offset and a nonpositive end offset, a +negative begin anchor still forces `clampNonposBottom`. + +The only difference from the complete case is that the reported keys are a prefix of the +complete window rather than the whole window. The reported keys below `begin.key` are +therefore a *sub*-count of the complete keys, which is enough: the complete window has +`clampNonposD + 1` keys in `[resolve end, end.key)`, so the count below `begin.key` still +forces the capacity inequality. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- In a compatible forward truncated nonpositive-end read, the reported keys below +`begin.key` are no more numerous than the complete window, i.e. the stored keys below +`resolve end`. -/ +theorem reportedBelowBegin_le_cnt_resolve_end {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) : + reportedBelowBegin r res ≤ cnt db (resolve db r.end_) := by + have htlen : reportedBelowBegin r res ≤ res.keys.length := + List.countP_le_length + have hpre := RangeRead.compatible_keys_prefix hcomp + have hlenle : res.keys.length ≤ (RangeRead.completeKeys db r).length := + hpre.length_le + have hck : (RangeRead.completeKeys db r).length ≤ cnt db (resolve db r.end_) := by + rw [RangeRead.completeKeys, if_neg (by simp [hrev]), RangeRead.inRange] + calc (db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_))).length + = db.keys.countP + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) := by + rw [List.countP_eq_length_filter] + _ ≤ db.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact hk.2 + _ = cnt db (resolve db r.end_) := by rw [cnt] + omega + +/-- If the begin key lies at or below the end resolution in a nonpositive-end read, the +capacity of `[begin.key, end.key)` already contains the `clampNonposD + 1` stored keys in +`[resolve end, end.key)`. Hence the clamping capacity test `clampNonposBottom` holds +whenever the reported keys below the begin key fit in the begin slack. -/ +theorem clampNonposBottom_of_le_resolve_end {r : RangeRead} {res : Result} {db : DB} + (hend : r.end_.offset ≤ 0) + (hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key)) + (hB_le_R : r.begin.key ≤ resolve db r.end_) + (hmq : reportedBelowBegin r res ≤ beginSlack r) : + clampNonposBottom r res := by + have hRE : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hlen : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := + between_resolve_end_length hend hRne + have hbetween_le : ((between db (resolve db r.end_) r.end_.key).length : ℕ∞) ≤ + intervalCard (resolve db r.end_) r.end_.key := + between_length_le_intervalCard hRE + have hmono : intervalCard (resolve db r.end_) r.end_.key ≤ + intervalCard r.begin.key r.end_.key := by + rw [intervalCard_eq_encard_keysIn, intervalCard_eq_encard_keysIn] + apply Set.encard_le_encard + intro k hk + exact ⟨le_trans hB_le_R hk.1, hk.2⟩ + have hcap : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + rw [← hlen] + exact le_trans hbetween_le hmono + have hsub : (((clampNonposD r + 1 : ℕ)) : ℕ∞) - + intervalCard r.begin.key r.end_.key = 0 := by + rw [tsub_eq_zero_iff_le] + exact hcap + unfold clampNonposBottom + rw [hsub, add_zero] + exact Nat.cast_le.mpr hmq + +/-- **The truncated negative-begin anchor bound.** In a compatible forward truncated +result with a negative begin offset, a nonpositive end offset, and reported keys below the +begin key within the begin slack, a negative begin anchor forces `clampNonposBottom`. -/ +theorem clampNonposBottom_of_anchor_neg_trunc {r : RangeRead} {res : Result} {db : DB} + (hcomp : RangeRead.Compatible db r res) + (hrev : r.reverse = false) (_hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hmq : reportedBelowBegin r res ≤ beginSlack r) + (hanchor : r.begin.anchor db < 0) : + clampNonposBottom r res := by + have hL : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + rw [RangeRead.completeKeys, if_neg (by simp [hrev])] + unfold RangeRead.inRange + rw [hL] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨coe_nil_le_key_withTop _, h⟩⟩ + have hRne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hR + have hpre := RangeRead.compatible_keys_prefix (db := db) (r := r) (res := res) hcomp + rw [hck, hR] at hpre + have hempty : db.keys.filter + (fun k : Key => decide (((k : Key) : WithTop Key) < (([] : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro k _ + simp only [decide_eq_true_eq, not_lt] + exact coe_nil_le_key_withTop _ + rw [hempty] at hpre + exact hne (List.prefix_nil.mp hpre) + have hRE : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hlen : (between db (resolve db r.end_) r.end_.key).length = clampNonposD r + 1 := + between_resolve_end_length hend hRne + have htR : reportedBelowBegin r res ≤ cnt db (resolve db r.end_) := + reportedBelowBegin_le_cnt_resolve_end hcomp hrev + have hcB : cnt db r.begin.key ≤ beginSlack r := by + have h := hanchor + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] at h + unfold cnt + omega + have hcntE : cnt db r.end_.key = + cnt db (resolve db r.end_) + (clampNonposD r + 1) := by + rw [cnt_eq_add_between db hRE, hlen] + by_cases hB_le_E : r.begin.key ≤ r.end_.key + · have hcntEB : cnt db r.end_.key = + cnt db r.begin.key + (between db r.begin.key r.end_.key).length := + cnt_eq_add_between db hB_le_E + have hdnat : clampNonposD r + 1 ≤ + (between db r.begin.key r.end_.key).length + + (beginSlack r - reportedBelowBegin r res) := by + omega + have hcast : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + ((between db r.begin.key r.end_.key).length : ℕ∞) + + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := by + exact_mod_cast hdnat + have hg' : ((between db r.begin.key r.end_.key).length : ℕ∞) ≤ + intervalCard r.begin.key r.end_.key := + between_length_le_intervalCard hB_le_E + have hmain : (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard r.begin.key r.end_.key + + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := + le_trans hcast (add_le_add hg' le_rfl) + have htsub : (((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key ≤ + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := by + rw [tsub_le_iff_left] + exact hmain + unfold clampNonposBottom + calc (reportedBelowBegin r res : ℕ∞) + + ((((clampNonposD r + 1 : ℕ)) : ℕ∞) - intervalCard r.begin.key r.end_.key) + ≤ (reportedBelowBegin r res : ℕ∞) + + ((beginSlack r - reportedBelowBegin r res : ℕ) : ℕ∞) := + add_le_add le_rfl htsub + _ = (beginSlack r : ℕ∞) := by + rw [← ENat.natCast_add] + congr 1 + omega + · have hEB : r.end_.key < r.begin.key := lt_of_not_ge hB_le_E + have hEBle : r.end_.key ≤ r.begin.key := le_of_lt hEB + have hcntB : cnt db r.begin.key = + cnt db r.end_.key + (between db r.end_.key r.begin.key).length := + cnt_eq_add_between db hEBle + have hnat : reportedBelowBegin r res + (clampNonposD r + 1) ≤ beginSlack r := by + omega + have hcap0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le hEBle + unfold clampNonposBottom + rw [hcap0] + have hcast : (((reportedBelowBegin r res + (clampNonposD r + 1) : ℕ)) : ℕ∞) ≤ + (beginSlack r : ℕ∞) := + Nat.cast_le.mpr hnat + simpa [ENat.natCast_add, add_comm, add_left_comm, add_assoc] using hcast + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncBottomGap.lean b/FormalConflictRange/NegCrossTruncBottomGap.lean new file mode 100644 index 0000000..c5c4f78 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncBottomGap.lean @@ -0,0 +1,538 @@ +/- +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.NegCrossTruncClampLastGe +import FormalConflictRange.NegCrossClampNonpos +import FormalConflictRange.NegCrossClosedForm + +/-! +# The crossed negative-begin truncated bottom-gap cell + +A forward truncated result with all reported keys strictly below `begin.key`, a negative +begin offset, a nonpositive end offset, a clamping begin selector, `begin.key ≤ end_.key`, +and enough gap capacity above `begin.key` for the end overshoot. In this cell the begin +selector clamps to the empty key and the minimal sound range is `[⊥, end_.key)`. + +The cell is the complement of the crossed short cell in the region where the reported +keys do not reach the begin key. Its closed form is proved by a case analysis on the +target key `k < end_.key`: + +* reported keys are necessary by `necessary_of_mem_result`; +* keys between the reported head and last by `necessary_of_between_result`; +* keys below the reported head by a toggle witness: padding the reported keys with + `clampNonposD + 1` gap keys above `begin.key` gives a compatible database, and + inserting a key below the head adds it to the truncated window; +* keys between the reported last and `end_.key` by an erase witness: a database formed + from the reported keys, the target, and the gap pads resolves its end selector onto the + target, and erasing the target drops the end resolution onto the reported last, which + removes the last reported key from the truncated window. +-/ + +namespace FormalConflictRange + +/-- A forward truncated, crossed, negative-begin read with a nonpositive end offset that +clamps, whose reported keys all lie below `begin.key`, and whose gap above `begin.key` +has room for the end overshoot. -/ +def negCrossTruncBottomGap (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧ + r.begin.key ≤ r.end_.key ∧ r.end_.key ≠ (⊤ : WithTop Key) ∧ + ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key + +/-- The gap-capacity condition forces `clampNonposBottom` in the bottom-gap cell. -/ +theorem clampNonposBottom_of_negCross_trunc_bottomGap {r : RangeRead} {res : Result} + (hcell : negCrossTruncBottomGap r res) : clampNonposBottom r res := by + obtain ⟨-, -, -, -, -, -, hclamp, -, -, hcap⟩ := hcell + unfold clampNonposBottom + have hzero : (((clampNonposD r + 1 : ℕ) : ℕ∞) - intervalCard r.begin.key r.end_.key) = 0 := + tsub_eq_zero_of_le hcap + rw [hzero, add_zero] + exact Nat.cast_le.mpr hclamp + +/-- The reported last key is the last of the result, and it lies below `begin.key`. -/ +theorem negCrossTruncBottomGap_last {r : RangeRead} {res : Result} + (hne : res.keys ≠ []) (hcell : negCrossTruncBottomGap r res) : + res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + +/-- Padding the reported keys with `clampNonposD + 1` keys that lie below `end_.key` and +above the reported last key — with at most one of them below `begin.key` — gives a +compatible database. The begin selector clamps (or resolves to the reported head), and +the end selector resolves to the first padding key, so the truncated filter is exactly +the reported keys. -/ +theorem exists_padDb_compatible {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hBE : r.begin.key ≤ r.end_.key) (hEne : r.end_.key ≠ (⊤ : WithTop Key)) + (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (dbF : DB) (k : Key) + (hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x) + (hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key) + (hFge : ∀ x ∈ dbF.keys, x = k ∨ r.begin.key ≤ (x : WithTop Key)) + (hFlen : dbF.keys.length = clampNonposD r + 1) : + ∃ dbw : DB, dbw.keys = res.keys ++ dbF.keys ∧ RangeRead.Compatible dbw r res ∧ + r.end_.anchor dbw = (res.keys.length : ℤ) ∧ + dbw.keys.length = res.keys.length + (clampNonposD r + 1) := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hlastE : ((last : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hlastB' hBE + have hFne : dbF.keys ≠ [] := by + intro h; rw [h] at hFlen; simp at hFlen + -- count of padding keys below `begin.key` is at most one + have hFcnt_le : dbF.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) ≤ 1 := by + rw [List.countP_eq_length_filter] + set l := dbF.keys.filter (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) with hl + have hnodup : l.Nodup := hl ▸ (dbF.sorted.pairwise.nodup.filter _) + have hall : ∀ x ∈ l, x = k := by + intro x hx + rw [hl] at hx + obtain ⟨hxmem, hxlt⟩ := List.mem_filter.mp hx + have hxlt' : (x : WithTop Key) < r.begin.key := of_decide_eq_true hxlt + rcases hFge x hxmem with hk | hge + · exact hk + · exact absurd (lt_of_lt_of_le hxlt' hge) (lt_irrefl (x : WithTop Key)) + by_contra h + have h2 : 1 < l.length := by omega + have heq : l[0] = l[1] := by + rw [hall l[0] (List.getElem_mem (by omega)), hall l[1] (List.getElem_mem (by omega))] + exact (by omega : (0 : ℕ) ≠ 1) ((hnodup.getElem_inj).mp heq) + -- sorted concatenation + set D : List Key := res.keys ++ dbF.keys with hD + have hDsort : D.SortedLT := by + rw [hD] + exact List.sortedLT_append.mpr ⟨hsort, dbF.sorted, fun a ha b hb => + lt_of_le_of_lt (hle_last a ha) (hFlast b hb)⟩ + let dbw : DB := ⟨D, hDsort⟩ + have hdbw_keys : dbw.keys = D := rfl + -- the begin anchor is nonpositive + have hcount_le : dbw.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) ≤ + reportedBelowBegin r res + 1 := by + rw [hdbw_keys, hD, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + rw [h1] + exact Nat.add_le_add_left hFcnt_le _ + have hanchor_le : r.begin.anchor dbw ≤ 0 := by + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin)] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + have hle' : (dbw.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) : ℤ) ≤ + (reportedBelowBegin r res : ℤ) + 1 := by exact_mod_cast hcount_le + omega + -- the begin resolution is the empty key or the reported head + set H : Key := if r.begin.anchor dbw < 0 then ([] : Key) else res.keys.head hne with hH + have hHle : H ≤ res.keys.head hne := by + by_cases hneg : r.begin.anchor dbw < 0 + · rw [hH]; simp only [hneg, ↓reduceIte]; exact nil_le_key _ + · rw [hH]; simp only [hneg, ↓reduceIte] + exact le_rfl + have hrb : resolve dbw r.begin = ((H : Key) : WithTop Key) := by + by_cases hneg : r.begin.anchor dbw < 0 + · rw [resolve_eq_nil hneg] + rw [hH]; simp only [hneg, ↓reduceIte] + · have hzero : r.begin.anchor dbw = 0 := by omega + have h0 : 0 ≤ r.begin.anchor dbw := by omega + have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by + rw [hzero, hdbw_keys, hD, List.length_append, hFlen] + simp + rw [resolve_eq_getElem h0 hlt] + have hz : (r.begin.anchor dbw).toNat = 0 := by rw [hzero]; rfl + have hget : dbw.keys[(r.begin.anchor dbw).toNat] = res.keys.head hne := by + simp only [hzero, hdbw_keys, hD] + have hpos : 0 < res.keys.length := List.length_pos_iff.mpr hne + rw [List.getElem_append_left (i := (0 : Int).toNat) (h := by simpa using hpos)] + exact (List.head_eq_getElem hne).symm + rw [hget] + rw [hH]; simp only [hneg, ↓reduceIte] + -- every stored key is below the end key + have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hD] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE + · exact hFend x hx + have hDlen : D.length = res.keys.length + (clampNonposD r + 1) := by + rw [hD, List.length_append, hFlen] + have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + D.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hDlt x hx + have hd_int : ((clampNonposD r : ℕ) : ℤ) = -r.end_.offset := by + rw [clampNonposD_eq] + exact Int.toNat_of_nonneg (by omega) + have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by + cases hE : r.end_.key with + | top => exact absurd hE hEne + | coe e => + have hce : D.countP (fun x => decide (x < e)) = D.length := by + rw [← countP_lt_coe (l := D) e, ← hE] + exact hcount_end + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hDlen] + push_cast + omega + have ht_lt : res.keys.length < D.length := by rw [hDlen]; omega + have hDget? : dbw.keys[res.keys.length]? = some (dbF.keys.head hFne) := by + change D[res.keys.length]? = some (dbF.keys.head hFne) + rw [hD, List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] + have hre : resolve dbw r.end_ = ((dbF.keys.head hFne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)] + rw [hanchor_end, Int.toNat_natCast, hDget?] + have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [hre] + exact WithTop.coe_lt_coe.mpr (hFlast _ (List.head_mem hFne)) + -- the truncated filter is the reported keys + have hck : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ last)) = res.keys := by + rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF] + rw [hdbw_keys, hD, List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + (((H : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨le_trans (WithTop.coe_le_coe.mpr (hHle.trans (by + exact (hsort.pairwise.imp le_of_lt).rel_head hx))) le_rfl, hle_last x hx⟩ + have hF_fil : dbF.keys.filter (fun x : Key => decide + (((H : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr (hFlast x hx) + rw [hres_fil, hF_fil, List.append_nil] + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨hcomp₀.1, hck⟩ + exact ⟨dbw, by rw [hdbw_keys, hD], hcompw, hanchor_end, by rw [hdbw_keys, hDlen]⟩ + +/-- A key strictly below the reported head has a flip witness in the bottom-gap cell: +the padded database is compatible, and inserting the key adds it to the truncated window +because the begin selector resolves to the empty key or to the inserted key itself. -/ +theorem exists_witness_below_head_bottomGap {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncBottomGap r res) + (hne : res.keys ≠ []) + (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (toggle dbw k) r res := by + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hBE, hEne, hcap⟩ := hcell + have hread' := hread + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hkHead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := hk + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hcross + have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlast_lt_B + have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hkHead hhead_lt_B + have hk_not_res : k ∉ res.keys := by + intro hx + exact absurd hkHead (not_lt.mpr + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) + obtain ⟨dbF, hFmem, hFlen⟩ := + exists_db_keysIn (B := r.begin.key) (E := r.end_.key) (m := clampNonposD r + 1) hcap + have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B (hFmem x hx).1) + have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2 + have hFge : ∀ x ∈ dbF.keys, x = k ∨ r.begin.key ≤ (x : WithTop Key) := + fun x hx => Or.inr (hFmem x hx).1 + obtain ⟨dbw, hkeyw, hcompw, -, hlenw⟩ := + exists_padDb_compatible hread' hrev hmore hne hbegin hend hclamp hBE hEne hcross dbF k + hFlast hFend hFge hFlen + -- every stored key is strictly above `k` + have hk_lt_all : ∀ x ∈ dbw.keys, k < x := by + intro x hx + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hkHead + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hk_lt_B (hFmem x hx).1) + have hk_not_dbw : k ∉ dbw.keys := fun hx => absurd (hk_lt_all k hx) (lt_irrefl k) + refine ⟨dbw, hcompw, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not_dbw] + intro hcomp' + have hins_keys : (dbw.insert k hk_not_dbw).keys = k :: dbw.keys := by + rw [DB.insert_keys] + cases hkw : dbw.keys with + | nil => simp + | cons a t => + have hka : k ≤ a := le_of_lt (hk_lt_all a (by rw [hkw]; exact List.mem_cons_self ..)) + rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka] + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + -- the begin anchor of the inserted database is still nonpositive + have hrescnt : dbw.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [hkeyw, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B + have h2 : dbF.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hFmem x hx).1 + rw [h1, h2, Nat.add_zero] + have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length + 1 := by + rw [hins_keys, List.countP_cons, decide_eq_true_eq.mpr hk_lt_B, if_pos rfl, hrescnt] + have hreported : reportedBelowBegin r res = res.keys.length := by + rw [reportedBelowBegin, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B + have hanchor_ins : r.begin.anchor (dbw.insert k hk_not_dbw) ≤ 0 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins] + have hle : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) := by + rw [hreported] at hclamp + exact_mod_cast hclamp + push_cast + omega + have hk_ck : k ∈ RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by rw [hins_keys]; exact List.mem_cons_self .., ?_, ?_⟩ + · by_cases hneg : r.begin.anchor (dbw.insert k hk_not_dbw) < 0 + · rw [resolve_eq_nil hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + · have hzero : r.begin.anchor (dbw.insert k hk_not_dbw) = 0 := by omega + have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by omega + have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat < + (dbw.insert k hk_not_dbw).keys.length := by + rw [hzero, hins_keys, List.length_cons]; simp + rw [resolve_eq_getElem h0 hlt] + have hget : (dbw.insert k hk_not_dbw).keys[(r.begin.anchor + (dbw.insert k hk_not_dbw)).toNat] = k := by + simp only [hzero, hins_keys] + rfl + rw [hget] + · have hpre := RangeRead.compatible_keys_prefix hcomp' + have hhead_mem : res.keys.head hne ∈ + RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := + hpre.subset (List.head_mem hne) + exact lt_trans hkHead (RangeRead.mem_completeKeys.mp hhead_mem).2.2 + have hk_le_last : k ≤ res.keys.getLast hne := + le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkHead)) hhead_le_last + have hk_filt : k ∈ (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [List.mem_filter] + exact ⟨hk_ck, by rw [decide_eq_true_eq]; exact hk_le_last⟩ + rw [hfilt'] at hk_filt + exact hk_not_res hk_filt + +/-- A key strictly above the reported last and strictly below the end key has an erase +witness in the bottom-gap cell: the target key sits among `clampNonposD + 1` padding keys +in `[begin.key, end_.key)`, so the end selector resolves into the padding; erasing the +target lowers the end resolution to the reported last, which drops the last reported key +from the truncated window. -/ +theorem exists_witness_erase_bottomGap {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncBottomGap r res) + (hne : res.keys ≠ []) + (hkgt : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) + (hkE : (k : WithTop Key) < r.end_.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hBE, hEne, hcap⟩ := hcell + have hread' := hread + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_k : res.keys.getLast hne < k := WithTop.coe_lt_coe.mp hkgt + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hcross + have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hlast_lt_B hBE + have hk_not_res : k ∉ res.keys := by + intro hx + exact absurd hlast_lt_k (not_lt.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) + -- pad keys in `[begin.key, end_.key)` avoiding `k` + obtain ⟨dbB, hBmem, hBk, hBlen⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := r.end_.key) (k := k) + (m := clampNonposD r) hcap + set raw : List Key := k :: dbB.keys with hraw + have hrawNodup : raw.Nodup := by + rw [hraw] + exact List.nodup_cons.mpr ⟨hBk, dbB.sorted.pairwise.nodup⟩ + set F : List Key := raw.insertionSort (· ≤ ·) with hF + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp hrawNodup) + let dbF : DB := ⟨F, hFsorted⟩ + have hFlen : dbF.keys.length = clampNonposD r + 1 := by + change F.length = clampNonposD r + 1 + rw [hF, List.length_insertionSort, hraw, List.length_cons, hBlen] + have hFmem' : ∀ x ∈ dbF.keys, x = k ∨ + (r.begin.key ≤ (x : WithTop Key) ∧ (x : WithTop Key) < r.end_.key) := by + intro x hx + have hx' := hFperm.mem_iff.mpr hx + rw [hraw] at hx' + rcases List.mem_cons.mp hx' with rfl | hxB + · exact Or.inl rfl + · exact Or.inr ⟨(hBmem x hxB).1, (hBmem x hxB).2⟩ + have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by + intro x hx + rcases hFmem' x hx with rfl | ⟨hxB, -⟩ + · exact hlast_lt_k + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B hxB) + have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := by + intro x hx + rcases hFmem' x hx with rfl | ⟨-, hxE⟩ + · exact hkE + · exact hxE + have hFge : ∀ x ∈ dbF.keys, x = k ∨ r.begin.key ≤ (x : WithTop Key) := by + intro x hx + rcases hFmem' x hx with rfl | ⟨hxB, -⟩ + · exact Or.inl rfl + · exact Or.inr hxB + obtain ⟨dbw, hkeyw, hcompw, hanchw, hlenw⟩ := + exists_padDb_compatible hread' hrev hmore hne hbegin hend hclamp hBE hEne hcross dbF k + hFlast hFend hFge hFlen + have hk_in_F : k ∈ dbF.keys := hFperm.mem_iff.mp (by rw [hraw]; exact List.mem_cons_self ..) + have hk_in_dbw : k ∈ dbw.keys := by rw [hkeyw]; exact List.mem_append.mpr (Or.inr hk_in_F) + refine ⟨dbw, hcompw, hk_in_dbw, ?_⟩ + intro hcomp' + have herase_keys : (dbw.erase k).keys = res.keys ++ dbF.keys.erase k := by + rw [DB.erase_keys, hkeyw, List.erase_append_right _ hk_not_res] + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + -- the erased end resolution is the reported last + have hcount_erase : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + res.keys.length + clampNonposD r := by + rw [herase_keys, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_E + have hFall : dbF.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = dbF.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hFend x hx + have hktrue : (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) k = true := by + rw [decide_eq_true_eq]; exact hkE + have h2 : (dbF.keys.erase k).countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = clampNonposD r := by + rw [List.countP_erase, if_pos ⟨hk_in_F, hktrue⟩, hFall, hFlen] + omega + rw [h1, h2] + have hd_int : ((clampNonposD r : ℕ) : ℤ) = -r.end_.offset := by + rw [clampNonposD_eq] + exact Int.toNat_of_nonneg (by omega) + have hanchor_erase : r.end_.anchor (dbw.erase k) = (res.keys.length : ℤ) - 1 := by + cases hE : r.end_.key with + | top => exact absurd hE hEne + | coe e => + rw [anchor_of_coe (s := r.end_) (v := e) hE] + have hce : (dbw.erase k).keys.countP (fun x => decide (x < e)) = + res.keys.length + clampNonposD r := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_erase + rw [hce] + push_cast + omega + have ht_pos : 0 < res.keys.length := List.length_pos_iff.mpr hne + have hresolve_end : resolve (dbw.erase k) r.end_ = + ((res.keys.getLast hne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanchor_erase]; omega + have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by + rw [herase_keys, List.length_append, List.length_erase, if_pos hk_in_F, hFlen] + omega + have hlt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by + rw [hanchor_erase, hlenErase] + omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_erase] + have : ((res.keys.length : ℤ) - 1).toNat = res.keys.length - 1 := by omega + exact this + have hget : (dbw.erase k).keys[(r.end_.anchor (dbw.erase k)).toNat] = + res.keys.getLast hne := by + simp only [hidx, herase_keys] + have hlen : res.keys.length - 1 < res.keys.length := by omega + rw [List.getElem_append_left (i := res.keys.length - 1) (h := hlen)] + exact (List.getLast_eq_getElem hne).symm + rw [hget] + have hlast_not_ck : res.keys.getLast hne ∉ + RangeRead.completeKeys (dbw.erase k) r := by + intro hmem + have hlt := (RangeRead.mem_completeKeys.mp hmem).2.2 + rw [hresolve_end] at hlt + exact lt_irrefl _ hlt + have hlast_in_filt : res.keys.getLast hne ∈ + (RangeRead.completeKeys (dbw.erase k) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [hfilt'] + exact List.getLast_mem hne + rw [List.mem_filter] at hlast_in_filt + exact hlast_not_ck hlast_in_filt.1 + +/-- The closed form of the bottom-gap cell: `[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossTruncBottomGap (r : RangeRead) (_res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **The crossed negative-begin truncated bottom-gap cell is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_bottomGap {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncBottomGap r res) : + Minimal r res (closedFormNegCrossTruncBottomGap r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hBE, hEne, hcap⟩ := hcell + have hcell' : negCrossTruncBottomGap r res := + ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hBE, hEne, hcap⟩ + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomEnd_of_clamp_nonpos_prefix hrev hbegin hend hBE, ?_⟩ + intro k _ hkE + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hkhead : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_of_toggle + (exists_witness_below_head_bottomGap hread hcell' hne hkhead) + · push_neg at hkhead + by_cases hklast : ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast) + · push_neg at hklast + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_erase_bottomGap hread hcell' hne hklast hkE + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_erase_of_mem hkdbw] + exact hnot⟩ + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncClamp.lean b/FormalConflictRange/NegCrossTruncClamp.lean new file mode 100644 index 0000000..102f2b9 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncClamp.lean @@ -0,0 +1,2789 @@ +/- +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.NegCrossTruncNonclamp +import FormalConflictRange.NegCrossClampLongNonpos + +/-! +# Crossed negative-begin truncated clamping cells + +This module begins closing the forward, crossed, truncated cells where the reported +keys below `begin.key` fit inside the begin slack (`reportedBelowBegin ≤ beginSlack`). +Unlike the nonclamping cells, the begin selector may clamp to the empty key, so the +minimal sound range starts at `⊥`. + +The first subcell treated here is the *no-gap* case `begin.key = keyAfter last`: the +minimal sound range is `[⊥, begin.key)`, and every key below `begin.key` is necessary. +The proof splits keys below the reported head (a toggle witness adapted from the +complete clamping cell) from the reported and intermediate keys (convexity). +-/ + +open FormalConflictRange + +set_option maxHeartbeats 1200000 + +namespace FormalConflictRange + +/-- The forward, crossed, truncated, clamping negative-begin cell in which the begin key +is exactly the successor of the reported last and every reported key lies below the end +key. -/ +abbrev negCrossTruncClampNoGapTop (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The closed form for the no-gap clamping cell: `[⊥, begin.key)`. -/ +noncomputable def closedFormNegCrossTruncClampNoGapTop (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ + +/-- **Every key below the reported head of a clamping truncated cell is necessary.** +The reported list alone is a compatible database: its end-filter count is bounded by the +count in any originally compatible database. Inserting a key below the head lands in the +`≤ last` filter of the complete keys. No relation between the end key and the reported +last is needed. -/ +theorem necessary_below_head_of_negCross_trunc_clamp {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : 1 ≤ r.end_.offset) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + {k : Key} (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hk_not : k ∉ res.keys := by + intro hkmem + exact hkcr (necessary_of_mem_result ⟨db₀, hcomp₀⟩ hkmem cr hs) + let db₁ : DB := ⟨res.keys, hsort⟩ + have hcB₁ : db₁.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have hanchor₁ : r.begin.anchor db₁ < 0 := by + rw [anchor_eq_count_sub_slack db₁ r (le_of_lt hbegin), hcB₁] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil (db := db₁) (s := r.begin) hanchor₁ + have hlast_ck₀ : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset (List.getLast_mem hne) + have hlast_lt_res₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck₀).2.2 + have hcnt₀ := filter_length_le_offset_of_resolve_end_gt (db := db₀) hend hlast_lt_res₀ + have hsub : res.keys.Sublist db₀.keys := result_sublist_db_of_compatible hcomp₀ hrev + have hsub' : ((res.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne))).Sublist + ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne))) := + (hsub.filter _).filter _ + have hself : (res.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = + res.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_filter.mp hx).1 + have hlast_lt_res₁ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₁ r.end_ := by + apply resolve_end_gt_of_filter_length_le (db := db₁) hend + change ((res.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne))).length ≤ _ + rw [hself] + rw [hself] at hsub' + exact le_trans hsub'.length_le hcnt₀ + have hck₁ : RangeRead.completeKeys db₁ r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + rw [hL₁] + rw [show db₁.keys = res.keys from rfl] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + refine ⟨WithTop.coe_le_coe.mpr (nil_le_key x), ?_⟩ + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) + hlast_lt_res₁ + have hcomp₁ : RangeRead.Compatible db₁ r res := by + refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + simpa [hck₁] using (List.prefix_refl (res.keys)) + let db₂ : DB := db₁.insert k hk_not + have hagree : AgreeOn cr db₁ db₂ := + (ConflictRange.keys_insert_of_not_mem cr db₁ hk_not hkcr).symm + have hcomp₂ : RangeRead.Compatible db₂ r res := (hs db₁ db₂ hagree).mp hcomp₁ + obtain ⟨-, heq₂⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp₂ + have hdb₂_keys : db₂.keys = k :: res.keys := by + unfold db₂ + rw [DB.insert_keys, show db₁.keys = res.keys from rfl] + cases hres : res.keys with + | nil => exact absurd hres hne + | cons a t => + have hhead_a : res.keys.head hne = a := by simp [hres] + have hka : k ≤ a := by + rw [hhead_a] at hk + exact le_of_lt (WithTop.coe_lt_coe.mp hk) + rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka] + have hkltB : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hk hcross + have hcB₂ : db₂.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res + 1 := by + rw [hdb₂_keys, List.countP_cons] + rw [decide_eq_true_eq.mpr hkltB] + simp [reportedBelowBegin] + have hanchor₂ : r.begin.anchor db₂ ≤ 0 := by + rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hcB₂] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL₂_le : resolve db₂ r.begin ≤ ((k : Key) : WithTop Key) := by + by_cases hneg : r.begin.anchor db₂ < 0 + · rw [resolve_eq_nil (db := db₂) (s := r.begin) hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + · have hzero : r.begin.anchor db₂ = 0 := by omega + have hltlen₂ : (r.begin.anchor db₂).toNat < db₂.keys.length := by + rw [hzero, hdb₂_keys] + simp + rw [resolve_eq_getElem (db := db₂) (s := r.begin) (by omega) hltlen₂] + have hval₂ : db₂.keys[(r.begin.anchor db₂).toNat]'hltlen₂ = k := by + simp [hzero, hdb₂_keys] + exact WithTop.coe_le_coe.mpr (le_of_eq hval₂) + have hk_lt_res₂ : ((k : Key) : WithTop Key) < resolve db₂ r.end_ := by + have hhead_ck₂ : res.keys.head hne ∈ RangeRead.completeKeys db₂ r := + (RangeRead.compatible_keys_prefix hcomp₂).subset (List.head_mem hne) + exact lt_trans hk (RangeRead.mem_completeKeys.mp hhead_ck₂).2.2 + have hk_ck₂ : k ∈ RangeRead.completeKeys db₂ r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by rw [hdb₂_keys]; exact List.mem_cons_self .., hL₂_le, hk_lt_res₂⟩ + have hkK : k < res.keys.head hne := WithTop.coe_lt_coe.mp hk + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hk_le_last : k ≤ res.keys.getLast hne := + le_trans (le_of_lt hkK) hhead_le_last + have hk_filter₂ : k ∈ (RangeRead.completeKeys db₂ r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [List.mem_filter] + exact ⟨hk_ck₂, by rw [decide_eq_true_eq]; exact hk_le_last⟩ + rw [heq₂] at hk_filter₂ + exact hk_not hk_filter₂ + +/-- **Every key below the reported head is necessary when the reported last reaches +`begin.key`, for an arbitrary positive-end key.** Filter a compatible database down to +the keys at or above the reported head. Every remaining key below `begin.key` is at or +below the reported last, hence is reported. The filtered database therefore clamps its +begin selector, and filtering cannot increase the end-selector count, so it remains +compatible. Inserting a key below the head then adds it to the truncated window. -/ +theorem necessary_below_head_of_negCross_trunc_clamp_lastGe + {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : 1 ≤ r.end_.offset) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hBlast : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) + {k : Key} (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + have hres_filter : (RangeRead.completeKeys db₀ r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys := + ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp₀).2 + have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := + hpre.subset (List.head_mem hne) + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + hpre.subset (List.getLast_mem hne) + have hrb_head : resolve db₀ r.begin ≤ ((res.keys.head hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + have hlast_lt_re₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hmem_res_of_mem {x : Key} (hxdb : x ∈ db₀.keys) + (hxhead : ((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key)) + (hxlast : x ≤ res.keys.getLast hne) : x ∈ res.keys := by + have hxck : x ∈ RangeRead.completeKeys db₀ r := by + rw [RangeRead.mem_completeKeys] + exact ⟨hxdb, le_trans hrb_head hxhead, + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hxlast) hlast_lt_re₀⟩ + have hxfilt : x ∈ (RangeRead.completeKeys db₀ r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [List.mem_filter] + exact ⟨hxck, decide_eq_true_eq.mpr hxlast⟩ + rwa [hres_filter] at hxfilt + let db₁ : DB := ⟨db₀.keys.filter + (fun x : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key))), + List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)⟩ + have hmem_db₁_of_res {x : Key} (hx : x ∈ res.keys) : x ∈ db₁.keys := by + change x ∈ db₀.keys.filter + (fun x : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key))) + rw [List.mem_filter] + exact ⟨(RangeRead.mem_completeKeys.mp (hpre.subset hx)).1, + decide_eq_true_eq.mpr (WithTop.coe_le_coe.mpr + ((hsort.pairwise.imp le_of_lt).rel_head hx))⟩ + have hk_not : k ∉ db₁.keys := by + intro hkmem + have hkhead : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := by + have := (List.mem_filter.mp hkmem).2 + exact of_decide_eq_true this + exact (not_le_of_gt hk) hkhead + have hcB₁ : db₁.keys.countP + (fun x : Key => decide (((x : 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 eq_of_forall_mem_iff_of_pairwise + · exact db₁.sorted.pairwise.filter _ + · exact hsort.pairwise.filter _ + · intro x + simp only [db₁, List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hxdb, hxhead⟩, hxB⟩ + refine ⟨hmem_res_of_mem hxdb hxhead ?_, hxB⟩ + exact WithTop.coe_le_coe.mp (le_trans (le_of_lt hxB) hBlast) + · rintro ⟨hxres, hxB⟩ + exact ⟨⟨(RangeRead.mem_completeKeys.mp (hpre.subset hxres)).1, + WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hxres)⟩, hxB⟩ + have hanchor₁ : r.begin.anchor db₁ < 0 := by + rw [anchor_eq_count_sub_slack db₁ r (le_of_lt hbegin), hcB₁] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil (db := db₁) (s := r.begin) hanchor₁ + have hcnt₀ := filter_length_le_offset_of_resolve_end_gt (db := db₀) hend hlast_lt_re₀ + have hsub : ((db₁.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne))).Sublist + ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne))) := by + exact List.Sublist.filter (fun x : Key => decide (x ≤ res.keys.getLast hne)) + (List.Sublist.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) + (show db₁.keys.Sublist db₀.keys from List.filter_sublist)) + have hlast_lt_res₁ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₁ r.end_ := + resolve_end_gt_of_filter_length_le (db := db₁) hend (le_trans hsub.length_le hcnt₀) + have hfilter₁ : (RangeRead.completeKeys db₁ r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys := by + rw [completeKeys_filter_le_eq hrev hlast_lt_res₁, hL₁] + apply eq_of_forall_mem_iff_of_pairwise + · exact db₁.sorted.pairwise.filter _ + · exact hsort.pairwise + · intro x + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hxdb₁, -, hxlast⟩ + have hxparts : x ∈ db₀.keys ∧ + ((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key) := by + simpa only [db₁, List.mem_filter, decide_eq_true_eq] using hxdb₁ + exact hmem_res_of_mem hxparts.1 hxparts.2 hxlast + · intro hxres + exact ⟨hmem_db₁_of_res hxres, WithTop.coe_le_coe.mpr (nil_le_key x), + (hsort.pairwise.imp le_of_lt).rel_getLast hxres⟩ + have hcomp₁ : RangeRead.Compatible db₁ r res := + (compatible_iff_truncated_keys hrev hmore hne).mpr ⟨hcomp₀.1, hfilter₁⟩ + let db₂ : DB := db₁.insert k hk_not + have hagree : AgreeOn cr db₁ db₂ := + (ConflictRange.keys_insert_of_not_mem cr db₁ hk_not hkcr).symm + have hcomp₂ : RangeRead.Compatible db₂ r res := (hs db₁ db₂ hagree).mp hcomp₁ + obtain ⟨-, heq₂⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp₂ + have hperm : db₂.keys.Perm (k :: db₁.keys) := by + unfold db₂ + rw [DB.insert_keys] + exact List.perm_orderedInsert (· ≤ ·) k db₁.keys + have hcB₂ : db₂.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res + 1 := by + rw [hperm.countP_eq, List.countP_cons, decide_eq_true_eq.mpr (lt_trans hk hcross), hcB₁] + simp [reportedBelowBegin] + have hanchor₂ : r.begin.anchor db₂ ≤ 0 := by + rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hcB₂] + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hkmem₂ : k ∈ db₂.keys := DB.mem_insert_self db₁ k hk_not + have hdb₂_keys : db₂.keys = k :: db₁.keys := by + unfold db₂ + rw [DB.insert_keys] + cases hkeys : db₁.keys with + | nil => simp + | cons a t => + have ha : a ∈ db₁.keys := by rw [hkeys]; exact List.mem_cons_self .. + have hHa : ((res.keys.head hne : Key) : WithTop Key) ≤ (a : WithTop Key) := by + have := (List.mem_filter.mp ha).2 + exact of_decide_eq_true this + have hka : k ≤ a := le_of_lt (lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk) + (WithTop.coe_le_coe.mp hHa)) + rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka] + have hL₂_le : resolve db₂ r.begin ≤ ((k : Key) : WithTop Key) := by + by_cases hneg : r.begin.anchor db₂ < 0 + · rw [resolve_eq_nil (db := db₂) (s := r.begin) hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + · have hzero : r.begin.anchor db₂ = 0 := by omega + have hltlen₂ : (r.begin.anchor db₂).toNat < db₂.keys.length := by + rw [hzero, hdb₂_keys] + simp + rw [resolve_eq_getElem (db := db₂) (s := r.begin) (by omega) hltlen₂] + have hval₂ : db₂.keys[(r.begin.anchor db₂).toNat]'hltlen₂ = k := by + simp [hzero, hdb₂_keys] + exact WithTop.coe_le_coe.mpr (le_of_eq hval₂) + have hk_lt_res₂ : ((k : Key) : WithTop Key) < resolve db₂ r.end_ := by + have hhead_ck₂ : res.keys.head hne ∈ RangeRead.completeKeys db₂ r := + (RangeRead.compatible_keys_prefix hcomp₂).subset (List.head_mem hne) + exact lt_trans hk (RangeRead.mem_completeKeys.mp hhead_ck₂).2.2 + have hk_ck₂ : k ∈ RangeRead.completeKeys db₂ r := by + rw [RangeRead.mem_completeKeys] + exact ⟨hkmem₂, hL₂_le, hk_lt_res₂⟩ + have hk_le_last : k ≤ res.keys.getLast hne := + le_trans (le_of_lt (WithTop.coe_lt_coe.mp hk)) + ((hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)) + have hk_filter₂ : k ∈ (RangeRead.completeKeys db₂ r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [List.mem_filter] + exact ⟨hk_ck₂, decide_eq_true_eq.mpr hk_le_last⟩ + rw [heq₂] at hk_filter₂ + have hkres : k ∈ res.keys := hk_filter₂ + have hkhead : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := + WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hkres) + exact (not_le_of_gt hk) hkhead + +/-- **Compatibility transfers across agreement on the no-gap clamping range +`[⊥, begin.key)`.** Agreement fixes every stored key below `begin.key`, hence the +begin resolution (whose offset is negative) and the truncated complete keys, because +the end selector is `⊤` and the reported last lies below `begin.key`. -/ +theorem compatible_of_agree_negCross_trunc_clamp_noGap_top {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) + (hgap : r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncClampNoGapTop r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + rw [hgap, hlastD] + exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt last) + have hcr : closedFormNegCrossTruncClampNoGapTop r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hmem := (agreeOn_iff (closedFormNegCrossTruncClampNoGapTop r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key → + (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk + refine hmem k ?_ + rw [hcr] + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩ + have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩ + have hbeg : resolve db1 r.begin = resolve db2 r.begin := by + rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin), + resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF] + have hE1 : resolve db1 r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top hendkey hendoff + have hE2 : resolve db2 r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top hendkey hendoff + have hfil : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = + (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + exact (db1.sorted.pairwise.filter _).filter _ + · unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + exact (db2.sorted.pairwise.filter _).filter _ + · intro k + rw [List.mem_filter, List.mem_filter, RangeRead.mem_completeKeys, + RangeRead.mem_completeKeys, hbeg, hE1, hE2] + constructor + · rintro ⟨⟨hdb, hlo, hhi⟩, hle⟩ + have hle' : k ≤ last := of_decide_eq_true hle + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle') hlastB + exact ⟨⟨(hmk k hkB).mp hdb, hlo, hhi⟩, hle⟩ + · rintro ⟨⟨hdb, hlo, hhi⟩, hle⟩ + have hle' : k ≤ last := of_decide_eq_true hle + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle') hlastB + exact ⟨⟨(hmk k hkB).mpr hdb, hlo, hhi⟩, hle⟩ + have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + rw [compatible_iff_truncated_keys hrev hmore hne] + refine ⟨by simpa using hc1.1, ?_⟩ + rw [← hlast, ← hfil, hres1] + +/-- **Closed form for the forward crossed negative-begin truncated clamping no-gap +cell.** The minimal sound range is `[⊥, begin.key)`. -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_noGap_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampNoGapTop r res) : + Minimal r res (closedFormNegCrossTruncClampNoGapTop r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hendkey, hendoff, hcrossD, hgap, hclamp⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hcrossD + exact hcrossD + have hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k _ + rw [hendkey] + exact WithTop.coe_lt_top k + have hcr : closedFormNegCrossTruncClampNoGapTop r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hsound : Sound r res (closedFormNegCrossTruncClampNoGapTop r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp_noGap_top hrev hmore hne hbegin + hendkey hendoff hgap h hagree, + fun h => compatible_of_agree_negCross_trunc_clamp_noGap_top hrev hmore hne hbegin + hendkey hendoff hgap h hagree.symm⟩ + rw [hcr] + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k _ hkend + have hkend' : k < keyAfter last := by + rw [hgap, hlastD] at hkend + exact WithTop.coe_lt_coe.mp hkend + have hkle : k ≤ last := (lt_keyAfter_iff k last).mp hkend' + by_cases hlt : k < head + · exact necessary_below_head_of_negCross_trunc_clamp hread' hrev hmore hne hbegin hendoff + hcross hclamp (WithTop.coe_lt_coe.mpr hlt) + · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) (le_of_not_gt hlt) hkle + +/-- The forward, crossed, truncated, clamping negative-begin cell in which the begin key +is exactly the successor of the reported last and every reported key lies below the end +key. -/ +abbrev negCrossTruncClampBelowEnd (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + +/-- The closed form for the below-end clamping cell, by dropping the end bound. -/ +noncomputable def closedFormNegCrossTruncClampBelowEnd (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossTruncClampNoGapTop r res + +/-- **Closed form for the forward crossed negative-begin truncated clamping below-end +cell.** Every reported key lies below `end_.key`, so the end selector can be dropped to +`⊤` and the no-gap rule applies. -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_below {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampBelowEnd r res) : + Minimal r res (closedFormNegCrossTruncClampBelowEnd r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hend, hcrossD, hgap, hclamp, hbelow⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hcompiff : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + fun db => compatible_iff_end_top_of_below hrev hmore hne hend hbelow + have hcellTop : negCrossTruncClampNoGapTop + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + ⟨hmore, hrev, hbegin, hne, rfl, by simpa using hend, hcrossD, + by simpa using hgap, hclamp⟩ + have hread' : Readable { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + ⟨db, (hcompiff db).mp hcomp⟩ + have hmin := minimal_closedForm_of_negCross_trunc_clamp_noGap_top hread' hcellTop + unfold closedFormNegCrossTruncClampBelowEnd + exact (minimal_congr_of_compatible_iff (r := r) + (r' := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) + (res := res) (cr := closedFormNegCrossTruncClampNoGapTop r res) hcompiff).mpr hmin + +/-- **In the no-gap clamping cell every compatible database has a clamping begin +anchor.** The reported keys are exactly the stored keys in the truncated window, so the +count of stored keys below `begin.key` is the reported length plus the count below the +begin resolution; the anchor equation then forces the resolution to clamp. -/ +theorem anchor_neg_of_negCross_trunc_clamp_noGap {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hgap : r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) : + r.begin.anchor db < 0 := by + set last : Key := res.keys.getLast hne with hlast + set head : Key := res.keys.head hne with hhead + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hgap' : r.begin.key = (((keyAfter last : Key)) : WithTop Key) := by + rw [hgap, hlastD] + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + rw [hgap'] + exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt last) + have hheadB : ((head : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne))) hlastB + have hcount_rep : reportedBelowBegin r res = res.keys.length := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB + have hlen_le : res.keys.length ≤ beginSlack r := by + rw [← hcount_rep]; exact hclamp + have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp + have hck_ne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne + have hres_eq : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db) hrev hmore hne).mp hcomp).2 + rwa [← hlast] at h + have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := by + have hlast_ck : last ∈ RangeRead.completeKeys db r := + hpre.subset (by rw [hlast]; exact List.getLast_mem hne) + exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + by_contra hnot + have hanchor : 0 ≤ r.begin.anchor db := le_of_not_gt hnot + have hhead_ck : head ∈ RangeRead.completeKeys db r := hpre.subset (List.head_mem hne) + have hrb_le_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + have hrb_ne_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hrb_le_head + exact absurd hrb_le_head (not_le.mpr (WithTop.coe_lt_top head)) + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + by_contra h + exact hrb_ne_top (resolve_eq_top hanchor (le_of_not_gt h)) + have hrb_get : resolve db r.begin = + (((db.keys[(r.begin.anchor db).toNat] : Key)) : WithTop Key) := + resolve_eq_getElem hanchor hltlen + have hrbkey_le_head : db.keys[(r.begin.anchor db).toNat] ≤ head := + WithTop.coe_le_coe.mp (by rw [← hrb_get]; exact hrb_le_head) + have hhead_lt_re : ((head : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hhead_ck).2.2 + have hrb_lt_re : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) < + resolve db r.end_ := by + rw [← hrb_get] + exact lt_of_le_of_lt hrb_le_head hhead_lt_re + have hrb_ck : db.keys[(r.begin.anchor db).toNat] ∈ RangeRead.completeKeys db r := by + rw [RangeRead.mem_completeKeys] + exact ⟨List.getElem_mem hltlen, by rw [← hrb_get], hrb_lt_re⟩ + have hck_sorted : (RangeRead.completeKeys db r).SortedLT := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hhead_ck_eq : (RangeRead.completeKeys db r).head hck_ne = head := by + refine (List.head_eq_iff_head?_eq_some hck_ne).mpr ?_ + obtain ⟨t, ht⟩ := hpre + rw [← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl, + Option.some_or] + have hhead_le_rbkey : head ≤ db.keys[(r.begin.anchor db).toNat] := by + have h := head_le_of_sortedLT' hck_sorted hck_ne hrb_ck + rwa [hhead_ck_eq] at h + have hrbeq : db.keys[(r.begin.anchor db).toNat] = head := + le_antisymm hrbkey_le_head hhead_le_rbkey + have hrb_head : resolve db r.begin = ((head : Key) : WithTop Key) := by + rw [hrb_get, hrbeq] + have hcnt_rb : cnt db (resolve db r.begin) = (r.begin.anchor db).toNat := by + rw [cnt, countP_lt_resolve, Nat.min_eq_left (le_of_lt hltlen)] + have hbetween : between db ((head : Key) : WithTop Key) r.begin.key = res.keys := by + unfold between + rw [hgap', ← hres_eq] + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hrb_head, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hh : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · by_cases hl : k ≤ last + · have hkre : ((k : Key) : WithTop Key) < resolve db r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hl) hlast_lt_re + have hklt : ((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hl) + simp [hh, hl, hklt, hkre] + · have hklt : ¬ ((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key) := + fun h => hl ((lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h)) + simp [hh, hl, hklt] + · simp [hh] + have hle_rb : resolve db r.begin ≤ r.begin.key := by + rw [hrb_head]; exact le_of_lt hheadB + have hcnt_split : cnt db r.begin.key = (r.begin.anchor db).toNat + res.keys.length := by + rw [cnt_eq_add_between db hle_rb, hcnt_rb, hrb_head, hbetween] + have hanchor_eq : (r.begin.anchor db : ℤ) = + (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + rfl + have hcnt_splitz : (cnt db r.begin.key : ℤ) = + ((r.begin.anchor db).toNat : ℤ) + (res.keys.length : ℤ) := by + rw [hcnt_split]; push_cast; ring + have htoNat : ((r.begin.anchor db).toNat : ℤ) = (r.begin.anchor db : ℤ) := + Int.toNat_of_nonneg hanchor + have hcontra : (res.keys.length : ℤ) = (beginSlack r : ℤ) + 1 := by + have h := hanchor_eq + rw [hcnt_splitz, htoNat] at h + omega + have h3 : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hlen_le + omega + +/-- **The resolved end overshoots the reported last on both sides of an agreement below +`begin.key`.** If `last` lies below `begin.key` and two databases agree on every stored +key below `begin.key`, then the end filter `[end_.key, last]` has the same length in +both, so one end resolution overshooting `last` forces the other to as well. -/ +theorem lt_resolve_end_of_agree_below {db1 db2 : DB} {r : RangeRead} {last : Key} + (hend : 1 ≤ r.end_.offset) + (hlastB : ((last : Key) : WithTop Key) < r.begin.key) + (hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key → + (k ∈ db1.keys ↔ k ∈ db2.keys)) + (h1 : ((last : Key) : WithTop Key) < resolve db1 r.end_) : + ((last : Key) : WithTop Key) < resolve db2 r.end_ := by + have hM : (db1.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + (db2.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact (db1.sorted.pairwise.filter _).filter _ + · exact (db2.sorted.pairwise.filter _).filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hdb, hE⟩, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨⟨(hmk k hkB).mp hdb, hE⟩, hle⟩ + · rintro ⟨⟨hdb, hE⟩, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨⟨(hmk k hkB).mpr hdb, hE⟩, hle⟩ + have hc1 := filter_length_le_offset_of_resolve_end_gt (db := db1) hend h1 + have hc2 : ((db2.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [← hM] + exact hc1 + exact resolve_end_gt_of_filter_length_le (db := db2) hend hc2 + +/-- **Compatibility transfers across agreement on the clamping range +`[⊥, begin.key)` for an arbitrary end key.** Agreement fixes every stored key below +`begin.key`, hence the begin resolution (negative offset) and both end-filter lengths; +the truncated complete keys below the reported last then coincide. The only geometric +input is `last < begin.key`, which holds both in the no-gap case and in a genuine gap. -/ +theorem compatible_of_agree_negCross_trunc_clamp {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hendoff : 1 ≤ r.end_.offset) + (hlastB : (((res.keys.getLast hne : Key)) : WithTop Key) < r.begin.key) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncClampNoGapTop r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set last : Key := res.keys.getLast hne with hlast + have hcr : closedFormNegCrossTruncClampNoGapTop r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hmem := (agreeOn_iff (closedFormNegCrossTruncClampNoGapTop r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key → + (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk + refine hmem k ?_ + rw [hcr] + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩ + have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩ + have hbeg : resolve db1 r.begin = resolve db2 r.begin := by + rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin), + resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF] + have hpre : res.keys <+: RangeRead.completeKeys db1 r := by + rcases hc1.2 with ⟨-, hpre, -⟩ | ⟨hm, -⟩ + · exact hpre + · rw [hmore] at hm + exact absurd hm (by simp) + have hlast_ck : last ∈ RangeRead.completeKeys db1 r := by + refine hpre.subset ?_ + rw [hlast] + exact List.getLast_mem hne + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + lt_resolve_end_of_agree_below (db1 := db1) (db2 := db2) (r := r) (last := last) + hendoff hlastB hmk hlt1 + have hfil : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = + (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) := by + rw [completeKeys_filter_le_eq (db := db1) (r := r) hrev hlt1, + completeKeys_filter_le_eq (db := db2) (r := r) hrev hlt2, hbeg] + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hdb, hlo, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨(hmk k hkB).mp hdb, hlo, hle⟩ + · rintro ⟨hdb, hlo, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨(hmk k hkB).mpr hdb, hlo, hle⟩ + have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + rw [compatible_iff_truncated_keys hrev hmore hne] + refine ⟨by simpa using hc1.1, ?_⟩ + rw [← hlast, ← hfil, hres1] + +/-- The forward, crossed, truncated, clamping negative-begin cell in which the begin key +is exactly the successor of the reported last, with an arbitrary end key. -/ +abbrev negCrossTruncClampNoGap (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ∧ + reportedBelowBegin r res ≤ beginSlack r + +/-- The closed form for the arbitrary-end no-gap clamping cell: `[⊥, begin.key)`. -/ +noncomputable def closedFormNegCrossTruncClampNoGap (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ + +/-- **Closed form for the forward crossed negative-begin truncated clamping no-gap +cell.** The minimal sound range is `[⊥, begin.key)`, for every end key. -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_noGap {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampNoGap r res) : + Minimal r res (closedFormNegCrossTruncClampNoGap r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hcrossD + exact hcrossD + have hcr : closedFormNegCrossTruncClampNoGap r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + rw [hgap, hlastD] + exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt last) + have hsound : Sound r res (closedFormNegCrossTruncClampNoGap r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin + hendoff hlastB h hagree, + fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin + hendoff hlastB h hagree.symm⟩ + rw [hcr] + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k _ hkend + have hkend' : k < keyAfter last := by + rw [hgap, hlastD] at hkend + exact WithTop.coe_lt_coe.mp hkend + have hkle : k ≤ last := (lt_keyAfter_iff k last).mp hkend' + by_cases hlt : k < head + · exact necessary_below_head_of_anchor_nonpos_negCross db₀ hcomp₀ hrev hne + (anchor_neg_of_negCross_trunc_clamp_noGap hcomp₀ hrev hmore hne hbegin hgap hclamp) + (WithTop.coe_lt_coe.mpr hlt) + · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) (le_of_not_gt hlt) hkle + +/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap and +insufficient gap capacity. Writing `q = beginSlack`, `t = #reported` and +`C = intervalCard last begin.key`, the capacity is insufficient when `C < q + 1 - t`. +The minimal range then collapses to `[⊥, keyAfter last)`: no gap key is necessary, and +compatibility is the same as for the read whose begin key is moved down to the immediate +successor of the reported last. -/ +abbrev negCrossTruncClampGapLow (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ + intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key ≤ + ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) + +/-- The read with the begin key moved down to the immediate successor of the reported +last, used to reduce the insufficient-capacity gap cell to the no-gap cell. -/ +def noGapRead (r : RangeRead) (res : Result) : RangeRead := + { r with begin := { r.begin with + key := (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) } } + +/-- The closed form for the insufficient-capacity gap cell: `[⊥, keyAfter last)`. -/ +noncomputable def closedFormNegCrossTruncClampGapLow (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossTruncClampNoGap (noGapRead r res) res + +/-- **Every compatible database in the insufficient-capacity gap cell clamps its begin +selector.** Compatibility forces the stored keys at or below the reported last to be +exactly the reported keys; the gap above them has fewer than `q + 1 - t` stored keys, so +the count below `begin.key` is at most `q`, making the begin anchor negative. -/ +theorem anchor_neg_of_negCross_trunc_clamp_gapLow {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hcell : negCrossTruncClampGapLow r res) : + r.begin.anchor db < 0 := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hlow⟩ := hcell + set last : Key := res.keys.getLast hne with hlast + set head : Key := res.keys.head hne with hhead + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set g : ℕ := (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length + with hg + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastD' : res.keys.getLast?.getD ([] : Key) = last := by + rw [List.getLast?_eq_some_getLast hne]; rfl + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hres_subl : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hres_eq : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db) hrev hmore hne).mp hcomp).2 + rwa [← hlast] at h + have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp (hres_subl.subset hlast_mem)).2.2 + have hheadB : ((head : Key) : WithTop Key) < r.begin.key := by + have hle : head ≤ last := hmem_le_last head (by rw [hhead]; exact List.head_mem hne) + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + have hrb_le_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp + (hres_subl.subset (by rw [hhead]; exact List.head_mem hne))).2.1 + have hrb_le_B : resolve db r.begin ≤ r.begin.key := + lt_of_le_of_lt hrb_le_head hheadB |> le_of_lt + -- The stored keys in `[resolve begin, begin.key)` are the reported keys plus the + -- stored gap keys in `[keyAfter last, begin.key)`. + have hbetween_eq : between db (resolve db r.begin) r.begin.key = + res.keys ++ between db (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + apply eq_of_forall_mem_iff_of_pairwise + · simp only [between] + exact db.sorted.pairwise.filter _ + · rw [← List.sortedLT_iff_pairwise, List.sortedLT_append] + refine ⟨hsort, List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _), ?_⟩ + intro a ha b hb + simp only [between, List.mem_filter, decide_eq_true_eq] at hb + have hale : a ≤ last := hmem_le_last a ha + exact lt_of_le_of_lt hale + (lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hb.2.1)) + · intro k + simp only [between, List.mem_filter, List.mem_append, decide_eq_true_eq] + constructor + · rintro ⟨hkdb, hlo, hhi⟩ + by_cases hkle : k ≤ last + · left + have hkre : ((k : Key) : WithTop Key) < resolve db r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re + have hkck : k ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨hkdb, hlo, hkre⟩ + have : k ∈ (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) := by + rw [List.mem_filter]; exact ⟨hkck, by simpa using hkle⟩ + rwa [hres_eq] at this + · right + refine ⟨hkdb, ?_, hhi⟩ + exact le_of_not_gt (fun h => hkle + ((lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h))) + · rintro (hkres | ⟨hkdb, hlo, hhi⟩) + · refine ⟨result_sublist_db_of_compatible hcomp hrev |>.subset hkres, ?_, ?_⟩ + · exact (RangeRead.mem_completeKeys.mp (hres_subl.subset hkres)).2.1 + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hkres)) hlastB + · refine ⟨hkdb, ?_, hhi⟩ + exact le_trans hrb_le_head (le_trans (WithTop.coe_le_coe.mpr (hmem_le_last head + (by rw [hhead]; exact List.head_mem hne))) + (le_trans (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt last))) hlo)) + have hdec1 : cnt db r.begin.key = + cnt db (resolve db r.begin) + (between db (resolve db r.begin) r.begin.key).length := + cnt_eq_add_between db hrb_le_B + have hIlen : (between db (resolve db r.begin) r.begin.key).length = t + g := by + rw [hbetween_eq, List.length_append, ht, hg] + -- If the anchor were nonnegative, the decomposition of `cnt begin.key` would force + -- `t + g = q + 1`, contradicting the insufficient-capacity bound. + by_contra hnot + have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hnot + have hrb_ne_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hrb_le_head + exact absurd hrb_le_head (not_le.mpr (WithTop.coe_lt_top head)) + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + by_contra hle + exact hrb_ne_top (resolve_eq_top h0 (le_of_not_gt hle)) + have hcntres : cnt db (resolve db r.begin) = (r.begin.anchor db).toNat := by + rw [cnt, countP_lt_resolve, Nat.min_eq_left (le_of_lt hltlen)] + have hcnt_eq : cnt db r.begin.key = (r.begin.anchor db).toNat + t + g := by + rw [hdec1, hcntres, hIlen] + omega + have hanc : (r.begin.anchor db : ℤ) = + (cnt db r.begin.key : ℤ) - (q : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + rfl + have htoNat : ((r.begin.anchor db).toNat : ℤ) = (r.begin.anchor db : ℤ) := + Int.toNat_of_nonneg h0 + have htg : t + g = q + 1 := by + have hcntz : (cnt db r.begin.key : ℤ) = + ((r.begin.anchor db).toNat : ℤ) + (t : ℤ) + (g : ℤ) := by + rw [hcnt_eq]; push_cast; ring + have h := hanc + rw [hcntz, htoNat] at h + omega + have hgs : g = q + 1 - t := by omega + have hle_tq : t ≤ q := by + have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + have hrep : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB + omega + have hg_le_card : (g : ℕ∞) ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) + r.begin.key := + between_length_le_intervalCard (db := db) (le_of_lt hgap') + have hcard_one : intervalCard ((last : Key) : WithTop Key) + (((keyAfter last : Key)) : WithTop Key) = 1 := by + have := intervalCard_succChain last 1 + simpa [keyAfter, List.replicate_one] using this + have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key = + intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := + intervalCard_add (b := ((last : Key) : WithTop Key)) + (m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key) + (le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap') + have hg_succ_le : (g : ℕ∞) + 1 ≤ ((q + 1 - t : ℕ) : ℕ∞) := by + calc (g : ℕ∞) + 1 + ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 := + add_le_add_left hg_le_card 1 + _ = intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [hcard_one, add_comm] + _ = intervalCard ((last : Key) : WithTop Key) r.begin.key := hcard_add.symm + _ ≤ ((q + 1 - t : ℕ) : ℕ∞) := by simpa [hq, hlastD'] using hlow + have hg_lt : (g : ℕ∞) < ((q + 1 - t : ℕ) : ℕ∞) := + (ENat.add_one_le_iff (by simp)).mp hg_succ_le + have hcontra : (g : ℕ∞) < (g : ℕ∞) := by + rw [← hgs] at hg_lt + exact hg_lt + exact absurd hcontra (lt_irrefl _) + +/-- **Compatibility is unchanged by lowering the begin key to the successor of the +reported last in the insufficient-capacity gap cell.** The begin resolution clamps to +the empty key on both sides: for `r` by `anchor_neg_of_negCross_trunc_clamp_gapLow`, +and for the lowered read by `anchor_neg_of_negCross_trunc_clamp_noGap`, which also +pins the count below `keyAfter last` to the reported length. -/ +theorem compatible_iff_noGapRead_of_gapLow {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapLow r res) (db : DB) : + RangeRead.Compatible db r res ↔ RangeRead.Compatible db (noGapRead r res) res := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hlow⟩ := hcell + set last : Key := res.keys.getLast hne with hlast + set head : Key := res.keys.head hne with hhead + set t : ℕ := res.keys.length with ht + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastD' : res.keys.getLast?.getD ([] : Key) = last := by + rw [List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hheadD' : res.keys.head?.getD ([] : Key) = head := by + rw [List.head?_eq_some_head hne]; rfl + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hreported_r : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB + have htq : t ≤ beginSlack r := by rw [← hreported_r]; exact hclamp + have hbetweenLen : (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length + = (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := rfl + constructor + · -- Forward: the original read clamps, hence so does the lowered read. + intro hcomp + have hanc_r : r.begin.anchor db < 0 := + anchor_neg_of_negCross_trunc_clamp_gapLow hcomp ⟨hmore, hrev, hbegin, hne, hendoff, + hcrossD, hgap, hclamp, hlow⟩ + have hres_r : resolve db r.begin = ((↑([] : Key) : WithTop Key)) := resolve_eq_nil hanc_r + have hcnt_le : cnt db (((keyAfter last : Key)) : WithTop Key) ≤ cnt db r.begin.key := by + unfold cnt + apply countP_le_countP_of_imp + intro k hk hlt + simp only [decide_eq_true_eq] at hlt ⊢ + exact lt_trans hlt hgap' + have hanc_ng : (noGapRead r res).begin.anchor db < 0 := by + have h_eq_ng : (noGapRead r res).begin.anchor db = + (cnt db (((keyAfter last : Key)) : WithTop Key) : ℤ) - (beginSlack r : ℤ) - 1 := by + have hs : beginSlack (noGapRead r res) = beginSlack r := rfl + rw [anchor_eq_count_sub_slack db (noGapRead r res) (le_of_lt hbegin), hs] + simp [cnt, noGapRead, hlastD, hlastD'] + have h_eq_r : r.begin.anchor db = + (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + simp [cnt] + rw [h_eq_ng] + have hlt : (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 < 0 := by + rw [← h_eq_r]; exact hanc_r + have : (cnt db (((keyAfter last : Key)) : WithTop Key) : ℤ) ≤ (cnt db r.begin.key : ℤ) := by + exact_mod_cast hcnt_le + omega + have hres_ng : resolve db (noGapRead r res).begin = + ((↑([] : Key) : WithTop Key)) := resolve_eq_nil hanc_ng + have hck_eq : RangeRead.completeKeys db (noGapRead r res) = RangeRead.completeKeys db r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_r, hres_ng] + rfl + rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne] + rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne] at hcomp + exact ⟨by change RangeRead.withinLimit r res.keys; exact hcomp.1, by rw [hck_eq]; exact hcomp.2⟩ + · -- Reverse: the lowered read clamps, which forces the count below `begin.key` to fit. + intro hcompNG + have hcrossNG : ((leastReported (noGapRead r res) res : Key) : WithTop Key) < + (noGapRead r res).begin.key := by + have hle : leastReported (noGapRead r res) res = head := by + unfold leastReported + rw [show (noGapRead r res).reverse = false from hrev] + simp [hheadD'] + rw [hle, show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne))) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) + have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + rw [show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk)) + have hclampNG : reportedBelowBegin (noGapRead r res) res ≤ beginSlack (noGapRead r res) := by + rw [hreported_ng] + have hs : beginSlack (noGapRead r res) = beginSlack r := rfl + rw [hs]; exact htq + have hcellNG : negCrossTruncClampNoGap (noGapRead r res) res := + ⟨hmore, hrev, hbegin, hne, hendoff, hcrossNG, rfl, hclampNG⟩ + have hancNG : (noGapRead r res).begin.anchor db < 0 := + anchor_neg_of_negCross_trunc_clamp_noGap hcompNG hrev hmore hne hbegin rfl hclampNG + have hres_ng : resolve db (noGapRead r res).begin = + ((↑([] : Key) : WithTop Key)) := resolve_eq_nil hancNG + have hres_eqNG : (RangeRead.completeKeys db (noGapRead r res)).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev + hmore hne).mp hcompNG).2 + rwa [← hlast] at h + have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := by + have hlast_mem : last ∈ res.keys := List.getLast_mem hne + have hmem : last ∈ (RangeRead.completeKeys db (noGapRead r res)).filter + (fun k : Key => decide (k ≤ last)) := by rw [hres_eqNG]; exact hlast_mem + exact (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hmem).1).2.2 + have hck_ng : RangeRead.completeKeys db (noGapRead r res) = + db.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [show (noGapRead r res).reverse = false from hrev, ite_eq_right (by simp), hres_ng] + simp [nil_le_key, noGapRead] + have hpred : ∀ k ∈ db.keys, + (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key)) ↔ + ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := by + intro k _ + constructor + · intro hk + have hkle : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hk) + exact ⟨lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re, hkle⟩ + · rintro ⟨hkre, hkle⟩ + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle) + have hcnt_keyAfter : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by + rw [cnt, List.countP_eq_length_filter] + have hl : db.keys.filter + (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter last : Key)) : WithTop Key))) = + (RangeRead.completeKeys db (noGapRead r res)).filter + (fun k : Key => decide (k ≤ last)) := by + rw [hck_ng, List.filter_filter] + apply List.filter_congr + intro k hk + by_cases h1 : (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key)) + · have h2 := (hpred k hk).mp h1 + simp [h1, h2.1, h2.2] + · have h2 : ¬ ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := + fun h => h1 ((hpred k hk).mpr h) + by_cases hA : (((k : Key) : WithTop Key) < resolve db r.end_) + · have hB : ¬ k ≤ last := fun hB => h2 ⟨hA, hB⟩ + simp [h1, hA, hB] + · simp [h1, hA] + rw [hl, hres_eqNG, ht] + have hcnt_split : cnt db r.begin.key = t + + (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := by + rw [cnt_eq_add_between db (le_of_lt hgap'), hcnt_keyAfter] + have hcard_one : intervalCard ((last : Key) : WithTop Key) + (((keyAfter last : Key)) : WithTop Key) = 1 := by + have := intervalCard_succChain last 1 + simpa [keyAfter, List.replicate_one] using this + have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key = + intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := + intervalCard_add (b := ((last : Key) : WithTop Key)) + (m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key) + (le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap') + have hg_succ_le : (((between db (((keyAfter last : Key)) : WithTop Key) + r.begin.key).length : ℕ) : ℕ∞) + 1 ≤ ((beginSlack r + 1 - t : ℕ) : ℕ∞) := by + calc (((between db (((keyAfter last : Key)) : WithTop Key) + r.begin.key).length : ℕ) : ℕ∞) + 1 + ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 := + add_le_add_left (between_length_le_intervalCard (db := db) (le_of_lt hgap')) 1 + _ = intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [hcard_one, add_comm] + _ = intervalCard ((last : Key) : WithTop Key) r.begin.key := hcard_add.symm + _ ≤ ((beginSlack r + 1 - t : ℕ) : ℕ∞) := by simpa [hlastD'] using hlow + have hg_lt : (((between db (((keyAfter last : Key)) : WithTop Key) + r.begin.key).length : ℕ) : ℕ∞) < ((beginSlack r + 1 - t : ℕ) : ℕ∞) := + (ENat.add_one_le_iff (by simp)).mp hg_succ_le + have htg_le : t + (between db (((keyAfter last : Key)) : WithTop Key) + r.begin.key).length ≤ beginSlack r := by + have hgl : (between db (((keyAfter last : Key)) : WithTop Key) + r.begin.key).length < beginSlack r + 1 - t := ENat.coe_lt_coe.mp hg_lt + omega + have hanc_r : r.begin.anchor db < 0 := by + have h_eq_r : r.begin.anchor db = + (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + simp [cnt] + rw [h_eq_r, hcnt_split] + have : ((t + (between db (((keyAfter last : Key)) : WithTop Key) + r.begin.key).length : ℕ) : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast htg_le + omega + have hres_r : resolve db r.begin = ((↑([] : Key) : WithTop Key)) := resolve_eq_nil hanc_r + have hck_eq : RangeRead.completeKeys db (noGapRead r res) = RangeRead.completeKeys db r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_r, hres_ng] + rfl + rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne] + rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne] at hcompNG + exact ⟨hcompNG.1, by rw [← hck_eq]; exact hcompNG.2⟩ + +/-- The lowered read satisfies the no-gap clamping cell whenever the original read is in +the insufficient-capacity gap cell. -/ +theorem negCrossTruncClampNoGap_noGapRead {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapLow r res) : + negCrossTruncClampNoGap (noGapRead r res) res := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hlow⟩ := hcell + set last : Key := res.keys.getLast hne with hlast + set head : Key := res.keys.head hne with hhead + set t : ℕ := res.keys.length with ht + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hheadD' : res.keys.head?.getD ([] : Key) = head := by + rw [List.head?_eq_some_head hne]; rfl + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hreported_r : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) + (lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap') + have htq : t ≤ beginSlack r := by rw [← hreported_r]; exact hclamp + have hcrossNG : ((leastReported (noGapRead r res) res : Key) : WithTop Key) < + (noGapRead r res).begin.key := by + have hle : leastReported (noGapRead r res) res = head := by + unfold leastReported + rw [show (noGapRead r res).reverse = false from hrev] + simp [hheadD'] + rw [hle, show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne))) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) + have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + rw [show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk)) + have hclampNG : reportedBelowBegin (noGapRead r res) res ≤ beginSlack (noGapRead r res) := by + rw [hreported_ng] + have hs : beginSlack (noGapRead r res) = beginSlack r := rfl + rw [hs]; exact htq + exact ⟨hmore, hrev, hbegin, hne, hendoff, hcrossNG, rfl, hclampNG⟩ + +/-- **Minimality of the insufficient-capacity gap closed form.** Compatibility is the +same as for the lowered no-gap read, so the no-gap closed form `[⊥, keyAfter last)` is +minimal here as well. -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_gapLow {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapLow r res) : + Minimal r res (closedFormNegCrossTruncClampGapLow r res) := by + have hiff : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (noGapRead r res) res := + fun db => compatible_iff_noGapRead_of_gapLow hread hcell db + have hreadNG : Readable (noGapRead r res) res := by + obtain ⟨db₀, hcomp₀⟩ := hread + exact ⟨db₀, (hiff db₀).mp hcomp₀⟩ + have hcellNG : negCrossTruncClampNoGap (noGapRead r res) res := + negCrossTruncClampNoGap_noGapRead hread hcell + exact (minimal_congr_of_compatible_iff hiff).mpr + (minimal_closedForm_of_negCross_trunc_clamp_noGap hreadNG hcellNG) + + +/-- For a truncated-compatible database whose complete window starts at the empty key, +the number of stored keys below `keyAfter last` is exactly the reported length. -/ +private theorem cnt_keyAfter_getLast_eq_length_of_window {db : DB} {r : RangeRead} {res : Result} + (hne : res.keys ≠ []) + (hwin : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_))) + (hfil : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = res.keys) : + cnt db (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) = res.keys.length := by + set last : Key := res.keys.getLast hne with hlast + have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne + have hmem : last ∈ (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) := by + rw [hfil]; exact hlast_mem + have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hmem).1).2.2 + have hpred : ∀ k ∈ db.keys, + (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key)) ↔ + ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := by + intro k _ + constructor + · intro hk + have hkle : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hk) + exact ⟨lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re, hkle⟩ + · rintro ⟨hkre, hkle⟩ + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle) + rw [cnt, List.countP_eq_length_filter] + have hl : db.keys.filter + (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter last : Key)) : WithTop Key))) = + (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) := by + rw [hwin, List.filter_filter] + apply List.filter_congr + intro k hk + by_cases h1 : (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key)) + · have h2 := (hpred k hk).mp h1 + simp [h1, h2.1, h2.2] + · have h2 : ¬ ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := + fun h => h1 ((hpred k hk).mpr h) + by_cases hA : (((k : Key) : WithTop Key) < resolve db r.end_) + · have hB : ¬ k ≤ last := fun hB => h2 ⟨hA, hB⟩ + simp [h1, hA, hB] + · simp [h1, hA] + rw [hl, hfil] + +/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap, the +reported head equal to the empty key, and gap capacity at most one more than the slack. +Then no key of the genuine gap is necessary and the minimal range is +`[⊥, keyAfter (last reported))`. -/ +abbrev negCrossTruncClampGapBotMid (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ + res.keys.headD ([] : Key) = ([] : Key) ∧ + intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key ≤ + (((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞) + +/-- The closed form for the empty-head middle gap cell: `[⊥, keyAfter last)`. -/ +noncomputable def closedFormNegCrossTruncClampGapBotMid (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossTruncClampNoGap (noGapRead r res) res + +/-- **Compatibility is unchanged by lowering the begin key in the empty-head middle gap +cell.** The reported head is the empty key, which is the least key, so both reads resolve +their begin selector to `⊥`; the capacity bound keeps the number of stored keys below +`begin.key` at most `q + 1`, so the original read cannot resolve above `⊥` either. -/ +theorem compatible_iff_noGapRead_of_gapBotMid {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapBotMid r res) (db : DB) : + RangeRead.Compatible db r res ↔ RangeRead.Compatible db (noGapRead r res) res := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hcap⟩ := hcell + set last : Key := res.keys.getLast hne with hlast + set head : Key := res.keys.head hne with hhead + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set g : ℕ := (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length + with hg + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastD' : res.keys.getLast?.getD ([] : Key) = last := by + rw [List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hheadD' : res.keys.head?.getD ([] : Key) = head := by + rw [List.head?_eq_some_head hne]; rfl + have hhead_bot : head = ([] : Key) := by rw [← hheadD]; exact hheadbot + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hreported_r : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB + have htq : t ≤ q := by + rw [← hreported_r, hq]; exact hclamp + constructor + · -- Forward: the original read resolves its begin to `⊥` because its head is `⊥`. + intro hcomp + have hfil : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db) hrev hmore hne).mp hcomp).2 + rwa [← hlast] at h + have hhead_mem : res.keys.head hne ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.head_mem hne) + have hres_bot : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := by + have hle := (RangeRead.mem_completeKeys.mp hhead_mem).2.1 + rw [← hhead] at hle + rw [hhead_bot] at hle + exact le_antisymm hle (coe_nil_le_key_withTop _) + have hwin : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), hres_bot] + simp [nil_le_key] + have hcntKA : cnt db (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) = + res.keys.length := + cnt_keyAfter_getLast_eq_length_of_window hne hwin hfil + have hcntKA' : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by + simpa [hlast, ht] using hcntKA + have hanchor_ng : (noGapRead r res).begin.anchor db < 0 := by + have h_eq : (noGapRead r res).begin.anchor db = + (cnt db (((keyAfter last : Key)) : WithTop Key) : ℤ) - (beginSlack r : ℤ) - 1 := by + have hs : beginSlack (noGapRead r res) = beginSlack r := rfl + rw [anchor_eq_count_sub_slack db (noGapRead r res) (le_of_lt hbegin), hs] + simp [cnt, noGapRead, hlastD, hlastD'] + rw [h_eq, hcntKA'] + have h1 : (t : ℤ) ≤ (q : ℤ) := by exact_mod_cast htq + rw [← hq]; omega + have hres_ng : resolve db (noGapRead r res).begin = ((↑([] : Key)) : WithTop Key) := + resolve_eq_nil hanchor_ng + have hck_eq_ng : RangeRead.completeKeys db (noGapRead r res) = + RangeRead.completeKeys db r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_ng, hres_bot] + simp [noGapRead] + rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne] + rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne] at hcomp + exact ⟨by change RangeRead.withinLimit r res.keys; exact hcomp.1, by rw [hck_eq_ng]; exact hcomp.2⟩ + · -- Reverse: the lowered read resolves to `⊥`; the capacity bound keeps `r`'s anchor + -- nonpositive, and `⊥` is stored, so `r` resolves to `⊥` too. + intro hcompNG + have hcrossNG : ((leastReported (noGapRead r res) res : Key) : WithTop Key) < + (noGapRead r res).begin.key := by + have hle : leastReported (noGapRead r res) res = head := by + unfold leastReported + rw [show (noGapRead r res).reverse = false from hrev] + simp [hheadD'] + rw [hle, show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne))) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) + have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + rw [show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk)) + have hclampNG : reportedBelowBegin (noGapRead r res) res ≤ beginSlack (noGapRead r res) := by + rw [hreported_ng] + have hs : beginSlack (noGapRead r res) = beginSlack r := rfl + rw [hs]; exact htq + have hancNG : (noGapRead r res).begin.anchor db < 0 := + anchor_neg_of_negCross_trunc_clamp_noGap hcompNG hrev hmore hne hbegin rfl hclampNG + have hres_ng : resolve db (noGapRead r res).begin = + ((↑([] : Key)) : WithTop Key) := resolve_eq_nil hancNG + have hfilNG : (RangeRead.completeKeys db (noGapRead r res)).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev + hmore hne).mp hcompNG).2 + rwa [← hlast] at h + have hwinNG : RangeRead.completeKeys db (noGapRead r res) = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [show (noGapRead r res).reverse = false from hrev, ite_eq_right (by simp), hres_ng] + simp [noGapRead, nil_le_key] + have hcntKA : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by + have h := cnt_keyAfter_getLast_eq_length_of_window hne hwinNG hfilNG + simpa [hlast, ht] using h + have hcnt : cnt db r.begin.key = t + g := by + rw [cnt_eq_add_between db (le_of_lt hgap'), hcntKA] + -- Capacity bound: the gap fits at most `q + 1 - t` stored keys. + have hcard_one : intervalCard ((last : Key) : WithTop Key) + (((keyAfter last : Key)) : WithTop Key) = 1 := by + have := intervalCard_succChain last 1 + simpa [keyAfter, List.replicate_one] using this + have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key = + intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := + intervalCard_add (b := ((last : Key) : WithTop Key)) + (m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key) + (le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap') + have hg_le : (g : ℕ∞) ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) + r.begin.key := + between_length_le_intervalCard (db := db) (le_of_lt hgap') + have hcap' : (((g + 1 : ℕ) : ℕ∞)) ≤ (((q + 1 - t) + 1 : ℕ) : ℕ∞) := by + calc (((g + 1 : ℕ) : ℕ∞)) + = ((g : ℕ∞) + 1) := by rw [Nat.cast_add, Nat.cast_one] + _ ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 := + add_le_add_left hg_le 1 + _ = intervalCard ((last : Key) : WithTop Key) r.begin.key := by + rw [hcard_add, hcard_one] + exact add_comm _ _ + _ ≤ (((q + 1 - t) + 1 : ℕ) : ℕ∞) := by simpa [hlastD', hq, ht] using hcap + have hg1 : g + 1 ≤ (q + 1 - t) + 1 := ENat.coe_le_coe.mp hcap' + have htg : t + g ≤ q + 1 := by omega + have hanchor : r.begin.anchor db = + (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]; simp [cnt] + have hanchor_le : r.begin.anchor db ≤ 0 := by + rw [hanchor, hcnt] + have h1 : ((t + g : ℕ) : ℤ) ≤ (q : ℤ) + 1 := by exact_mod_cast htg + have hq' : (q : ℤ) = (beginSlack r : ℤ) := by rw [hq] + rw [hq'] at h1 + omega + have hbot_mem : ([] : Key) ∈ db.keys := by + have hsub := RangeRead.compatible_keys_subset (db := db) (r := noGapRead r res) hcompNG + exact hsub (by rw [← hhead_bot]; exact List.head_mem hne) + have hres_r : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := by + rcases lt_or_eq_of_le hanchor_le with hlt | heq + · exact resolve_eq_nil hlt + · have hpos : 0 ≤ r.begin.anchor db := le_of_eq heq.symm + have hlen_pos : 0 < db.keys.length := List.length_pos_of_mem hbot_mem + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + rw [heq]; simpa using hlen_pos + rw [resolve_eq_getElem hpos hltlen] + simp only [heq, Int.toNat_zero] + rw [keys_getElem_zero_eq_nil_of_mem hbot_mem] + have hck_eq : RangeRead.completeKeys db (noGapRead r res) = + RangeRead.completeKeys db r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_ng, hres_r] + simp [noGapRead] + rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne] + rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne] at hcompNG + exact ⟨by change RangeRead.withinLimit r res.keys; exact hcompNG.1, by rw [← hck_eq]; exact hcompNG.2⟩ + +/-- The lowered read satisfies the no-gap clamping cell whenever the original read is in +the empty-head middle gap cell. -/ +theorem negCrossTruncClampNoGap_noGapRead_of_gapBotMid {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapBotMid r res) : + negCrossTruncClampNoGap (noGapRead r res) res := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hcap⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hreported_r : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) + (lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap') + have htq : t ≤ beginSlack r := by rw [← hreported_r]; exact hclamp + have hleast : leastReported (noGapRead r res) res = ([] : Key) := by + unfold leastReported + rw [show (noGapRead r res).reverse = false from hrev] + simpa [List.headD] using hheadbot + refine ⟨hmore, hrev, hbegin, hne, hendoff, ?_, rfl, ?_⟩ + · rw [hleast] + change ((↑([] : Key) : WithTop Key)) < + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) + rw [hlastD] + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (nil_le_key last) + (keyAfter_self_lt last)) + · have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + rw [show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk)) + rw [hreported_ng] + have hs : beginSlack (noGapRead r res) = beginSlack r := rfl + rw [hs]; exact htq + +/-- **Minimality of the empty-head middle gap closed form.** Compatibility is the same +as for the lowered no-gap read, so the no-gap closed form `[⊥, keyAfter last)` is minimal +here as well. -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_gapBotMid {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapBotMid r res) : + Minimal r res (closedFormNegCrossTruncClampGapBotMid r res) := by + have hiff : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (noGapRead r res) res := + fun db => compatible_iff_noGapRead_of_gapBotMid hread hcell db + have hreadNG : Readable (noGapRead r res) res := by + obtain ⟨db₀, hcomp₀⟩ := hread + exact ⟨db₀, (hiff db₀).mp hcomp₀⟩ + have hcellNG : negCrossTruncClampNoGap (noGapRead r res) res := + negCrossTruncClampNoGap_noGapRead_of_gapBotMid hread hcell + exact (minimal_congr_of_compatible_iff hiff).mpr + (minimal_closedForm_of_negCross_trunc_clamp_noGap hreadNG hcellNG) + +/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap, the +reported head equal to the empty key, and gap capacity strictly above the middle bound. +The minimal range is `[⊥, begin.key)`. -/ +abbrev negCrossTruncClampGapHighBot (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ + res.keys.headD ([] : Key) = ([] : Key) ∧ + (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) < r.end_.key ∧ + (((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞) < + intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key + +/-- The closed form for the empty-head high-capacity gap cell: `[⊥, begin.key)`. -/ +noncomputable def closedFormNegCrossTruncClampGapHighBot (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ + +/-- **Compatibility transfers across agreement on the high-capacity gap range +`[⊥, begin.key)`.** The reported keys all lie below `begin.key`, the end selector lies +above the reported last, and every compatible database has begin resolution `⊥`, so the +truncated complete keys depend only on the stored keys below `begin.key`. -/ +theorem compatible_of_agree_negCross_trunc_clamp_gapHighBot {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hheadbot : res.keys.headD ([] : Key) = ([] : Key)) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncClampGapHighBot r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := 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 hhead_bot : res.keys.head hne = ([] : Key) := by + rw [← hheadD]; exact hheadbot + have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + rw [hlastD] at hgap + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt hgap) + have hcr : closedFormNegCrossTruncClampGapHighBot r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hmem := (agreeOn_iff (closedFormNegCrossTruncClampGapHighBot r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key → + (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk + refine hmem k ?_ + rw [hcr] + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩ + have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩ + have hbeg : resolve db1 r.begin = resolve db2 r.begin := by + rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin), + resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF] + have hbot_res : ([] : Key) ∈ res.keys := by + rw [← hhead_bot]; exact List.head_mem hne + have hbeg1 : resolve db1 r.begin = ((↑([] : Key)) : WithTop Key) := by + have hmem_ck := (RangeRead.compatible_keys_prefix hc1).subset hbot_res + have hle := (RangeRead.mem_completeKeys.mp hmem_ck).2.1 + exact le_antisymm hle (coe_nil_le_key_withTop _) + have hbeg2 : resolve db2 r.begin = ((↑([] : Key)) : WithTop Key) := by + rw [← hbeg]; exact hbeg1 + have hE1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + lt_resolve_end_of_lt_end_key hendoff hlastE' + have hE2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + lt_resolve_end_of_lt_end_key hendoff hlastE' + have hflt : db1.keys.filter (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide (k ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hdb, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨(hmk k hkB).mp hdb, hle⟩ + · rintro ⟨hdb, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨(hmk k hkB).mpr hdb, hle⟩ + have hck1 : RangeRead.completeKeys db1 r = + db1.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db1 r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), hbeg1] + simp [nil_le_key] + have hck2 : RangeRead.completeKeys db2 r = + db2.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db2 r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), hbeg2] + simp [nil_le_key] + have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hfil1 : db1.keys.filter (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck1, List.filter_filter] at hres1 + have hsame : db1.keys.filter (fun k : Key => decide (k ≤ last) && decide + (((k : Key) : WithTop Key) < resolve db1 r.end_)) = + db1.keys.filter (fun k : Key => decide (k ≤ last)) := by + apply List.filter_congr + intro k _ + by_cases hle : k ≤ last + · have hlt := lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE1 + simp [hle, hlt] + · simp [hle] + rw [hsame] at hres1 + exact hres1 + have hfil2 : db2.keys.filter (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [← hflt, hfil1] + rw [compatible_iff_truncated_keys (db := db2) hrev hmore hne] + refine ⟨by simpa using hc1.1, ?_⟩ + rw [hck2, List.filter_filter] + have hsame2 : db2.keys.filter (fun k : Key => decide (k ≤ last) && decide + (((k : Key) : WithTop Key) < resolve db2 r.end_)) = + db2.keys.filter (fun k : Key => decide (k ≤ last)) := by + apply List.filter_congr + intro k _ + by_cases hle : k ≤ last + · have hlt := lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE2 + simp [hle, hlt] + · simp [hle] + rw [hsame2, hfil2] + +/-- A database formed by the reported keys followed by `T` gap keys in +`(keyAfter last, begin.key)` is compatible with the high-capacity gap cell. Its begin +anchor is `0`, it resolves the begin to the empty key, and its end resolution lies above +the reported last. -/ +theorem compatible_of_append_gapKeys_highBot {r : RangeRead} {res : Result} {db : DB} + {J : List Key} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hlim : r.withinLimit res.keys) + (htq : res.keys.length ≤ beginSlack r) + (hheadbot : res.keys.headD ([] : Key) = ([] : Key)) + (hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hsort : res.keys.SortedLT) (hJ : J.SortedLT) + (hJrange : ∀ x ∈ J, (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) + (hJlen : J.length = beginSlack r + 1 - res.keys.length) + (hkeys : db.keys = res.keys ++ J) : + RangeRead.Compatible db r res := by + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hlastD : res.keys.getLastD ([] : Key) = last := 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 hhead_bot : res.keys.head hne = ([] : Key) := by + rw [← hheadD]; exact hheadbot + have hJlen' : J.length = T := by omega + have hTpos : 1 ≤ T := by + rw [hT]; omega + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + have hxle : ((x : Key) : WithTop Key) ≤ ((last : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (hlast_le x hx) + exact lt_of_le_of_lt hxle (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt (by rw [← hlastD]; exact hgap))) + have hJgt_last : ∀ x ∈ J, last < x := by + intro x hx + have := (hJrange x hx).1 + rw [hlastD] at this + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) this) + have hJlt_B : ∀ x ∈ J, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact (hJrange x hx).2 + have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy) + have hcntB : cnt db r.begin.key = t + T := by + rw [cnt, List.countP_eq_length_filter] + have hf : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = db.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hkeys] at hx + rcases List.mem_append.mp hx with hx | hx + · exact hres_lt_B x hx + · exact hJlt_B x hx + rw [hf, hkeys, List.length_append, ht, hJlen'] + have hanchorB : r.begin.anchor db = 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + change ((cnt db r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 0 + rw [hcntB, ← hq] + change ((t + T : ℕ) : ℤ) - (q : ℤ) - 1 = 0 + rw [hT] + omega + have hbot_mem : ([] : Key) ∈ db.keys := by + rw [hkeys, ← hhead_bot] + exact List.mem_append_left _ (List.head_mem hne) + have hltlenB : (r.begin.anchor db).toNat < db.keys.length := by + rw [hanchorB]; exact List.length_pos_of_mem hbot_mem + have hz? : db.keys[(r.begin.anchor db).toNat]? = some ([] : Key) := by + rw [hanchorB, Int.toNat_zero] + rw [List.getElem?_eq_getElem (h := List.length_pos_of_mem hbot_mem)] + exact congrArg some (keys_getElem_zero_eq_nil_of_mem hbot_mem) + have hz : db.keys[(r.begin.anchor db).toNat]'(hltlenB) = ([] : Key) := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hltlenB)] + exact hz? + have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hresB : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := by + rw [resolve_eq_getElem (by omega) hltlenB] + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hz + have hemptyM : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = 0 := by + rw [List.length_eq_zero_iff, List.eq_nil_iff_forall_not_mem] + intro x hx + rw [List.mem_filter, List.mem_filter] at hx + obtain ⟨⟨hxdb, hxE⟩, hxle⟩ := hx + rw [hkeys] at hxdb + rcases List.mem_append.mp hxdb with hx | hx + · have hxB : ((x : Key) : WithTop Key) < r.end_.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlastE' le_rfl) + exact absurd (of_decide_eq_true hxE) (not_le.mpr hxB) + · exact absurd (of_decide_eq_true hxle) (not_le.mpr (hJgt_last x hx)) + have hresE : ((last : Key) : WithTop Key) < resolve db r.end_ := + resolve_end_gt_of_filter_length_le hendoff (by rw [hemptyM]; omega) + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), hresB] + simp [nil_le_key] + have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + rw [hck, hkeys, List.filter_filter, List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide (k ≤ last) && decide + (((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys := by + rw [List.filter_eq_self] + intro k hk + rw [Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq] + exact ⟨hlast_le k hk, lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le k hk)) hresE⟩ + have h2 : J.filter (fun k : Key => decide (k ≤ last) && decide + (((k : Key) : WithTop Key) < resolve db r.end_)) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk hcon + rw [Bool.and_eq_true] at hcon + exact absurd (of_decide_eq_true hcon.1) (not_le.mpr (hJgt_last k hk)) + rw [h1, h2, List.append_nil] + rw [compatible_iff_truncated_keys (db := db) hrev hmore hne] + exact ⟨hlim, hfil⟩ + +/-- **Gap keys are necessary in the empty-head high-capacity gap cell.** A key between +the reported last and `begin.key` is necessary: a witness database with exactly +`beginSlack + 1 - #reported` gap keys resolves the begin to `⊥`, while inserting the +target raises the begin anchor to `1`, so the empty key drops out of the truncated +complete keys and the reported result is no longer compatible. -/ +theorem necessary_gap_highBot {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncClampGapHighBot r res) + (hkgt : res.keys.getLastD ([] : Key) < k) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hlastE, + hcap⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : r.withinLimit res.keys := hcomp₀.1 + set last : Key := res.keys.getLast hne with hlast + set q : ℕ := beginSlack r with hq + set t : ℕ := res.keys.length with ht + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by + rw [hlastD] at hlastE; exact hlastE + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hkgt' : last < k := by rw [← hlastD]; exact hkgt + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hreported : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB + have htq : t ≤ q := by rw [← hreported, hq]; exact hclamp + have hTpos : 1 ≤ q + 1 - t := by omega + -- Capacity: one more than the number of chosen gap keys fits in `(keyAfter last, B)`. + have hcard_one : intervalCard ((last : Key) : WithTop Key) + (((keyAfter last : Key)) : WithTop Key) = 1 := by + have := intervalCard_succChain last 1 + simpa [keyAfter, List.replicate_one] using this + have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key = + intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := + intervalCard_add (b := ((last : Key) : WithTop Key)) + (m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key) + (le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap') + have hD : (((q + 1 - t : ℕ) : ℕ∞) + 1) ≤ + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [hlastD] at hcap + have hC : intervalCard ((last : Key) : WithTop Key) r.begin.key = + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 := by + rw [hcard_add, hcard_one, add_comm] + rw [hC] at hcap + have hcast : (((q + 1 - t : ℕ) + 1 : ℕ) : ℕ∞) = ((q + 1 - t : ℕ) : ℕ∞) + 1 := by + push_cast; ring + rw [hcast] at hcap + by_cases htop : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = ⊤ + · rw [htop]; exact le_top + · exact (ENat.lt_add_one_iff + (n := intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key) htop).mp hcap + -- Choose the gap keys avoiding the target. + obtain ⟨dbJ, hJmem, hJavoid, hJlen⟩ := exists_db_keysIn_avoid + (B := (((keyAfter last : Key)) : WithTop Key)) (E := r.begin.key) (k := k) + (m := q + 1 - t) hD + have hJrange : ∀ x ∈ dbJ.keys, + (((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => hJmem x hx + have hJgt_last : ∀ x ∈ dbJ.keys, last < x := by + intro x hx + have h1 := (hJrange x hx).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1) + have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ dbJ.keys, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy) + have hsorted : (res.keys ++ dbJ.keys).SortedLT := + List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hres_lt_J⟩ + let db : DB := ⟨res.keys ++ dbJ.keys, hsorted⟩ + have hkeys : db.keys = res.keys ++ dbJ.keys := rfl + have hJrange' : ∀ x ∈ dbJ.keys, + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + have h := hJrange x hx + rw [← hlastD] at h + exact h + have hJlen' : dbJ.keys.length = beginSlack r + 1 - res.keys.length := by + rw [hJlen, ← hq, ← ht] + have hcomp : RangeRead.Compatible db r res := + compatible_of_append_gapKeys_highBot hrev hmore hne hbegin hendoff hlim htq hheadbot + hlastE hgap hsort dbJ.sorted hJrange' hJlen' hkeys + have hknot : k ∉ db.keys := by + rw [hkeys] + intro hk + rcases List.mem_append.mp hk with hk | hk + · exact absurd (hlast_le k hk) (not_le.mpr hkgt') + · exact hJavoid hk + refine necessary_of_toggle ⟨db, hcomp, ?_⟩ + intro hcompk + rw [toggle_eq_insert_of_not_mem hknot] at hcompk + -- The inserted database has begin anchor `1` and resolves the begin above `⊥`. + have hcntDb : cnt db r.begin.key = res.keys.length + dbJ.keys.length := by + rw [cnt, List.countP_eq_length_filter] + have hf : db.keys.filter (fun x : Key => decide (((x : Key) : WithTop Key) < + r.begin.key)) = db.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hkeys] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB + · exact (hJrange x hx).2 + rw [hf, hkeys, List.length_append] + have hcntT : cnt db r.begin.key = q + 1 := by + rw [hcntDb, hJlen, ← ht] + omega + have hcnt' : cnt (db.insert k hknot) r.begin.key = q + 2 := by + rw [cnt_insert_lt db k hknot hkB, hcntT] + have hanchor' : r.begin.anchor (db.insert k hknot) = 1 := by + rw [anchor_eq_count_sub_slack (db.insert k hknot) r (le_of_lt hbegin)] + change ((cnt (db.insert k hknot) r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 1 + rw [hcnt', ← hq] + omega + have hbot_res : ([] : Key) ∈ res.keys := by + rw [← hheadbot]; exact headD_mem hne + have hbot_mem : ([] : Key) ∈ db.keys := by + rw [hkeys]; exact List.mem_append_left _ hbot_res + let db' : DB := db.insert k hknot + have hbot_mem' : ([] : Key) ∈ db'.keys := + (DB.mem_insert_iff hknot).mpr (Or.inr hbot_mem) + have hlen_db : 2 ≤ db.keys.length := by + rw [hkeys, List.length_append] + have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne + omega + have hlen1 : 1 < db'.keys.length := by + change 1 < (db.insert k hknot).keys.length + rw [DB.insert_keys, List.orderedInsert_length]; omega + have hg0 : db'.keys[0] = ([] : Key) := + keys_getElem_zero_eq_nil_of_mem hbot_mem' + have h01 : db'.keys[0] < db'.keys[1] := + (db'.sorted.getElem_lt_getElem_iff).mpr (by omega : (0 : Nat) < 1) + have hgt0 : (([] : Key) : WithTop Key) < ((db'.keys[1] : Key) : WithTop Key) := by + rw [← hg0]; exact WithTop.coe_lt_coe.mpr h01 + have hanchor_nat : (r.begin.anchor db').toNat = 1 := by + change (r.begin.anchor (db.insert k hknot)).toNat = 1 + rw [hanchor']; rfl + have hanc' : r.begin.anchor db' = 1 := by + change r.begin.anchor (db.insert k hknot) = 1 + exact hanchor' + have hresB' : resolve db' r.begin = ((db'.keys[1] : Key) : WithTop Key) := by + have h0' : 0 ≤ r.begin.anchor db' := by rw [hanc']; norm_num + have hlt : (r.begin.anchor db').toNat < db'.keys.length := by + rw [hanchor_nat]; exact hlen1 + have hget := resolve_eq_getElem (db := db') (s := r.begin) h0' hlt + rw [hget] + have hidx : db'.keys[(r.begin.anchor db').toNat] = db'.keys[1] := by + rw [getElem_congr_idx (h' := hanchor_nat)] + rw [hidx] + have hnotbot : ([] : Key) ∉ RangeRead.completeKeys db' r := by + rw [RangeRead.mem_completeKeys] + rintro ⟨_, hle, _⟩ + rw [hresB'] at hle + exact (not_le.mpr hgt0) hle + have hcompk' : RangeRead.Compatible db' r res := hcompk + have hfilter := + ((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcompk').2 + rw [← hlast] at hfilter + have hbot_in : ([] : Key) ∈ (RangeRead.completeKeys db' r).filter + (fun x : Key => decide (x ≤ last)) := by + rw [hfilter]; exact hbot_res + exact hnotbot (List.mem_filter.mp hbot_in).1 + +/-- **Minimality of the empty-head high-capacity gap closed form.** The minimal range +is `[⊥, begin.key)`: soundness is transfer across agreement on that range, the reported +span is necessary by convexity, and every gap key is necessary by +`necessary_gap_highBot`. -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_gapHighBot {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapHighBot r res) : + Minimal r res (closedFormNegCrossTruncClampGapHighBot r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hlastE, + hcap⟩ := hcell + have hcr : closedFormNegCrossTruncClampGapHighBot r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hsound : Sound r res (closedFormNegCrossTruncClampGapHighBot r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp_gapHighBot hrev hmore hne + hbegin hendoff hheadbot hgap hlastE h hagree, + fun h => compatible_of_agree_negCross_trunc_clamp_gapHighBot hrev hmore hne + hbegin hendoff hheadbot hgap hlastE h hagree.symm⟩ + rw [hcr] at hsound ⊢ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k _ hkend + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hbot_res : ([] : Key) ∈ res.keys := by + rw [← hheadbot]; exact headD_mem hne + by_cases hkle : k ≤ res.keys.getLastD ([] : Key) + · have hkle' : k ≤ last := by rw [hlastD] at hkle; exact hkle + exact necessary_of_between_result hread hbot_res + (by rw [hlast]; exact List.getLast_mem hne) (nil_le_key k) hkle' + · have hkgt : res.keys.getLastD ([] : Key) < k := lt_of_not_ge hkle + exact necessary_gap_highBot hread ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, + hclamp, hheadbot, hlastE, hcap⟩ hkgt hkend + +/-- A database formed by the reported keys followed by `T` gap keys in +`(keyAfter last, begin.key)` is compatible with the general high-capacity gap cell. Its +begin anchor is `0`, it resolves the begin to the reported head, and its end resolution +lies above the reported last. -/ +theorem compatible_of_append_gapKeys_high {r : RangeRead} {res : Result} {db : DB} + {J : List Key} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hlim : r.withinLimit res.keys) + (htq : res.keys.length ≤ beginSlack r) + (hcnt : (res.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).length ≤ + (r.end_.offset - 1).toNat) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hsort : res.keys.SortedLT) (hJ : J.SortedLT) + (hJrange : ∀ x ∈ J, (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) + (hJlen : J.length = beginSlack r + 1 - res.keys.length) + (hkeys : db.keys = res.keys ++ J) : + RangeRead.Compatible db r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hJlen' : J.length = T := by omega + have hTpos : 1 ≤ T := by rw [hT]; omega + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + have hxle : ((x : Key) : WithTop Key) ≤ ((last : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (hlast_le x hx) + exact lt_of_le_of_lt hxle (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt (by rw [← hlastD]; exact hgap))) + have hJgt_last : ∀ x ∈ J, last < x := by + intro x hx + have := (hJrange x hx).1 + rw [hlastD] at this + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) this) + have hJlt_B : ∀ x ∈ J, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact (hJrange x hx).2 + have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy) + have hcntB : cnt db r.begin.key = t + T := by + rw [cnt, List.countP_eq_length_filter] + have hf : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = db.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hkeys] at hx + rcases List.mem_append.mp hx with hx | hx + · exact hres_lt_B x hx + · exact hJlt_B x hx + rw [hf, hkeys, List.length_append, ht, hJlen'] + have hanchorB : r.begin.anchor db = 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + change ((cnt db r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 0 + rw [hcntB, ← hq] + change ((t + T : ℕ) : ℤ) - (q : ℤ) - 1 = 0 + rw [hT] + omega + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hz? : db.keys[(r.begin.anchor db).toNat]? = some head := by + rw [hanchorB, Int.toNat_zero, hkeys] + rw [List.getElem?_append_left (by rw [← ht]; exact List.length_pos_of_ne_nil hne)] + rw [List.getElem?_eq_getElem (h := List.length_pos_of_ne_nil hne)] + exact congrArg some (by rw [hhead]; exact (List.head_eq_getElem hne).symm) + have hltlenB : (r.begin.anchor db).toNat < db.keys.length := by + rw [hanchorB, Int.toNat_zero] + rw [hkeys, List.length_append] + exact Nat.lt_of_lt_of_le (List.length_pos_of_ne_nil hne) (Nat.le_add_right _ _) + have hz : db.keys[(r.begin.anchor db).toNat]'(hltlenB) = head := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hltlenB)] + exact hz? + have hresB : resolve db r.begin = ((head : Key) : WithTop Key) := by + rw [resolve_eq_getElem (by omega) hltlenB] + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hz + have hM : ((db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))) = + res.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + rw [hkeys, List.filter_append, List.filter_append] + have h1 : (res.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + res.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hlast_le x (List.mem_filter.mp hx).1 + have h2 : (J.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hcon + exact (not_le.mpr (hJgt_last x (List.mem_filter.mp hx).1)) + (of_decide_eq_true hcon) + rw [h1, h2, List.append_nil] + have hresE : ((last : Key) : WithTop Key) < resolve db r.end_ := + resolve_end_gt_of_filter_length_le hendoff (by rw [hM]; exact hcnt) + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (head ≤ k) && decide + (((k : Key) : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), hresB] + simp [WithTop.coe_le_coe] + have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + rw [hck, hkeys, List.filter_filter, List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) && + decide (((k : Key) : WithTop Key) < resolve db r.end_))) = res.keys := by + rw [List.filter_eq_self] + intro k hk + rw [Bool.and_eq_true, Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq, + decide_eq_true_eq] + exact ⟨hlast_le k hk, hhead_le k hk, + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le k hk)) hresE⟩ + have h2 : J.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) && + decide (((k : Key) : WithTop Key) < resolve db r.end_))) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk hcon + rw [Bool.and_eq_true] at hcon + exact absurd (of_decide_eq_true hcon.1) (not_le.mpr (hJgt_last k hk)) + rw [h1, h2, List.append_nil] + rw [compatible_iff_truncated_keys (db := db) hrev hmore hne] + exact ⟨hlim, hfil⟩ + +/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap, a +nonempty reported head, and gap capacity strictly above the low bound (`T < C`). The +minimal range is `[⊥, begin.key)`. -/ +abbrev negCrossTruncClampGapHigh (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ + res.keys.headD ([] : Key) ≠ ([] : Key) ∧ + (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) < r.end_.key ∧ + ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) < + intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key + +/-- The closed form for the nonempty-head high-capacity gap cell: `[⊥, begin.key)`. -/ +noncomputable def closedFormNegCrossTruncClampGapHigh (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ + +/-- A database whose keys are the empty key, the reported keys, and `T` gap keys has +begin anchor `1`, resolves the begin to the reported head, and is compatible with the +nonempty-head high-capacity gap cell. -/ +theorem compatible_of_nil_cons_gapKeys_high {r : RangeRead} {res : Result} {db : DB} + {J : List Key} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hlim : r.withinLimit res.keys) + (htq : res.keys.length ≤ beginSlack r) + (hheadne : res.keys.headD ([] : Key) ≠ ([] : Key)) + (hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hsort : res.keys.SortedLT) (hJ : J.SortedLT) + (hJrange : ∀ x ∈ J, (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) + (hJlen : J.length = beginSlack r + 1 - res.keys.length) + (hkeys : db.keys = ([] : Key) :: (res.keys ++ J)) : + RangeRead.Compatible db r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne + have hJlen' : J.length = T := by omega + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + have hxle : ((x : Key) : WithTop Key) ≤ ((last : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (hlast_le x hx) + exact lt_of_le_of_lt hxle (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt (by rw [← hlastD]; exact hgap))) + have hJgt_last : ∀ x ∈ J, last < x := by + intro x hx + have := (hJrange x hx).1 + rw [hlastD] at this + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) this) + have hJlt_B : ∀ x ∈ J, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact (hJrange x hx).2 + have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy) + have hnil_lt_B : (([] : Key) : WithTop Key) < r.begin.key := by + have hlt : (([] : Key) : WithTop Key) < ((head : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr (lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne')) + exact lt_trans hlt (hres_lt_B head (List.head_mem hne)) + have hcntB : cnt db r.begin.key = 1 + t + T := by + rw [cnt, List.countP_eq_length_filter] + have hf : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = db.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hkeys] at hx + rcases List.mem_cons.mp hx with rfl | hx + · exact hnil_lt_B + · rcases List.mem_append.mp hx with hx | hx + · exact hres_lt_B x hx + · exact hJlt_B x hx + rw [hf, hkeys, List.length_cons, List.length_append, ht, hJlen'] + omega + have hanchorB : r.begin.anchor db = 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + change ((cnt db r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 1 + rw [hcntB, ← hq] + have : ((1 + t + T : ℕ) : ℤ) - (q : ℤ) - 1 = 1 := by rw [hT]; omega + exact this + have hlt1 : 1 < db.keys.length := by + rw [hkeys, List.length_cons, List.length_append] + have := List.length_pos_of_ne_nil hne + omega + have hz? : db.keys[1]? = some head := by + rw [hkeys, List.getElem?_cons_succ] + rw [List.getElem?_append_left (by rw [← ht]; exact List.length_pos_of_ne_nil hne)] + rw [List.getElem?_eq_getElem (h := List.length_pos_of_ne_nil hne)] + exact congrArg some (by rw [hhead]; exact (List.head_eq_getElem hne).symm) + have hz : db.keys[1]'(hlt1) = head := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hlt1)] + exact hz? + have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hresB : resolve db r.begin = ((head : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db := by rw [hanchorB]; norm_num + have hlt : (r.begin.anchor db).toNat < db.keys.length := by + rw [hanchorB]; simpa using hlt1 + have hval : db.keys[(r.begin.anchor db).toNat]'(hlt) = head := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hlt)] + have hidx : (r.begin.anchor db).toNat = 1 := by rw [hanchorB]; rfl + rw [hidx] + exact hz? + rw [resolve_eq_getElem h0 hlt] + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval + have hemptyM : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = 0 := by + rw [List.length_eq_zero_iff, List.eq_nil_iff_forall_not_mem] + intro x hx + rw [List.mem_filter, List.mem_filter] at hx + obtain ⟨⟨hxdb, hxE⟩, hxle⟩ := hx + rw [hkeys] at hxdb + rcases List.mem_cons.mp hxdb with rfl | hxdb + · have hnil_lt_last : (([] : Key) : WithTop Key) < ((last : Key) : WithTop Key) := + lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne'))) + (WithTop.coe_le_coe.mpr (hlast_le head (List.head_mem hne))) + have hx0 : (([] : Key) : WithTop Key) < r.end_.key := lt_trans hnil_lt_last hlastE' + exact absurd (of_decide_eq_true hxE) (not_le.mpr hx0) + · rcases List.mem_append.mp hxdb with hx | hx + · have hxB : ((x : Key) : WithTop Key) < r.end_.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlastE' le_rfl) + exact absurd (of_decide_eq_true hxE) (not_le.mpr hxB) + · exact absurd (of_decide_eq_true hxle) (not_le.mpr (hJgt_last x hx)) + have hresE : ((last : Key) : WithTop Key) < resolve db r.end_ := + resolve_end_gt_of_filter_length_le hendoff (by rw [hemptyM]; omega) + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (head ≤ k) && decide + (((k : Key) : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), hresB] + simp [WithTop.coe_le_coe] + have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + rw [hck, hkeys, List.filter_filter, List.filter_cons, List.filter_append] + have hnil_pred : ¬ (decide ((([] : Key)) ≤ last) && (decide (head ≤ (([] : Key))) && + decide ((([] : Key) : WithTop Key) < resolve db r.end_))) = true := by + intro hcon + rw [Bool.and_eq_true, Bool.and_eq_true] at hcon + exact (not_le.mpr (lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne'))) + (of_decide_eq_true hcon.2.1) + rw [if_neg hnil_pred] + have h1 : res.keys.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) && + decide (((k : Key) : WithTop Key) < resolve db r.end_))) = res.keys := by + rw [List.filter_eq_self] + intro k hk + rw [Bool.and_eq_true, Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq, + decide_eq_true_eq] + exact ⟨hlast_le k hk, hhead_le k hk, + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le k hk)) hresE⟩ + have h2 : J.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) && + decide (((k : Key) : WithTop Key) < resolve db r.end_))) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk hcon + rw [Bool.and_eq_true] at hcon + exact absurd (of_decide_eq_true hcon.1) (not_le.mpr (hJgt_last k hk)) + rw [h1, h2, List.append_nil] + rw [compatible_iff_truncated_keys (db := db) hrev hmore hne] + exact ⟨hlim, hfil⟩ + +/-- **Compatibility transfers across agreement on the nonempty-head high-capacity gap +range `[⊥, begin.key)`.** Agreement on the stored keys below `begin.key` fixes the begin +resolution and every reported key, and the end resolution lies above the reported last. -/ +theorem compatible_of_agree_negCross_trunc_clamp_gapHigh {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncClampGapHigh r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + rw [hlastD] at hgap + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt hgap) + have hcr : closedFormNegCrossTruncClampGapHigh r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hmem := (agreeOn_iff (closedFormNegCrossTruncClampGapHigh r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key → + (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk + refine hmem k ?_ + rw [hcr] + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩ + have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩ + have hbeg : resolve db1 r.begin = resolve db2 r.begin := by + rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin), + resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF] + have hE1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + lt_resolve_end_of_lt_end_key hendoff hlastE' + have hE2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + lt_resolve_end_of_lt_end_key hendoff hlastE' + have hred1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide (resolve db1 r.begin ≤ (k : WithTop Key)) && + decide (k ≤ last)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hle : k ≤ last + · have hlt : ((k : Key) : WithTop Key) < resolve db1 r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE1 + simp [hle, hlt] + · simp [hle] + have hred2 : (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide (resolve db2 r.begin ≤ (k : WithTop Key)) && + decide (k ≤ last)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hle : k ≤ last + · have hlt : ((k : Key) : WithTop Key) < resolve db2 r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE2 + simp [hle, hlt] + · simp [hle] + have heq : db1.keys.filter (fun k : Key => decide (resolve db1 r.begin ≤ (k : WithTop Key)) && + decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide (resolve db2 r.begin ≤ (k : WithTop Key)) && + decide (k ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, Bool.and_eq_true, decide_eq_true_eq] + constructor + · rintro ⟨hdb, hrb, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨(hmk k hkB).mp hdb, by rw [← hbeg]; exact hrb, hle⟩ + · rintro ⟨hdb, hrb, hle⟩ + have hkB : ((k : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB + exact ⟨(hmk k hkB).mpr hdb, by rw [hbeg]; exact hrb, hle⟩ + have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + rw [compatible_iff_truncated_keys (db := db2) hrev hmore hne] + refine ⟨by simpa using hc1.1, ?_⟩ + rw [hred2, ← heq, ← hred1, hres1] + +/-- **Gap keys are necessary in the nonempty-head high-capacity gap cell.** A key between +the reported last and `begin.key` is necessary: a witness database with the empty key, the +reported keys and `T` gap keys including the target resolves the begin to the reported +head, while erasing the target lowers the begin anchor so the empty key enters the +truncated complete keys. -/ +theorem necessary_gap_high {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncClampGapHigh r res) + (hkgt : res.keys.getLastD ([] : Key) < k) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadne, hlastE, + hcap⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : r.withinLimit res.keys := hcomp₀.1 + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne + have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by + rw [hlastD] at hlastE; exact hlastE + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hkgt' : last < k := by rw [← hlastD]; exact hkgt + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hreported : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB + have htq : t ≤ q := by rw [← hreported, hq]; exact hclamp + have hTpos : 1 ≤ q + 1 - t := by omega + have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB + have hnil_lt_head : ([] : Key) < head := + lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne') + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hnil_lt_mem : ∀ x ∈ res.keys, ([] : Key) < x := by + intro x hx + exact lt_of_lt_of_le hnil_lt_head (hhead_le x hx) + have hnil_lt_B : (([] : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr hnil_lt_head) + (hres_lt_B head (List.head_mem hne)) + have hnil_lt_lastK : ([] : Key) < last := + lt_of_lt_of_le hnil_lt_head (hlast_le head (List.head_mem hne)) + have hnil_lt_last : (([] : Key) : WithTop Key) < ((last : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr hnil_lt_lastK + -- capacity: `T ≤ intervalCard (keyAfter last) begin.key` + have hcard_one : intervalCard ((last : Key) : WithTop Key) + (((keyAfter last : Key)) : WithTop Key) = 1 := by + have := intervalCard_succChain last 1 + simpa [keyAfter, List.replicate_one] using this + have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key = + intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := + intervalCard_add (b := ((last : Key) : WithTop Key)) + (m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key) + (le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap') + have hD : ((T : ℕ) : ℕ∞) ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [hlastD] at hcap + have hC : intervalCard ((last : Key) : WithTop Key) r.begin.key = + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 := by + rw [hcard_add, hcard_one, add_comm] + rw [hC] at hcap + by_cases htop : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = ⊤ + · rw [htop]; exact le_top + · exact (ENat.lt_add_one_iff + (n := intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key) htop).mp hcap + -- choose `T - 1` gap keys avoiding the target + have hcard' : (((T - 1) + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [Nat.sub_add_cancel hTpos]; exact hD + obtain ⟨dbJ, hJmem, hJavoid, hJlen⟩ := exists_db_keysIn_avoid + (B := (((keyAfter last : Key)) : WithTop Key)) (E := r.begin.key) (k := k) + (m := T - 1) hcard' + have hJ'range : ∀ x ∈ dbJ.keys, + (((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => hJmem x hx + have hJ'gt_last : ∀ x ∈ dbJ.keys, last < x := by + intro x hx + have h1 := (hJ'range x hx).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1) + have hres_lt_J' : ∀ x ∈ res.keys, ∀ y ∈ dbJ.keys, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJ'gt_last y hy) + -- the target is above the last and in the gap + have hk_le_keyAfter : keyAfter last ≤ k := by + by_contra hcon + rw [not_le] at hcon + exact absurd ((lt_keyAfter_iff k last).mp hcon) (not_le.mpr hkgt') + have hk_not_res : k ∉ res.keys := by + intro hk + exact absurd (hlast_le k hk) (not_le.mpr hkgt') + have hk_ne_nil : k ≠ ([] : Key) := by + intro h + have hnil_lt_k : ([] : Key) < k := + lt_of_le_of_lt (nil_le_key last) hkgt' + rw [h] at hnil_lt_k + exact (lt_irrefl ([] : Key)) hnil_lt_k + have hk_not_dbJ : k ∉ dbJ.keys := hJavoid + let J : List Key := dbJ.keys.orderedInsert (· ≤ ·) k + have hJlen' : J.length = T := by + unfold J; rw [List.orderedInsert_length, hJlen]; omega + have hJnodup : J.Nodup := by + unfold J + exact (List.perm_orderedInsert (· ≤ ·) k dbJ.keys).nodup_iff.mpr + (List.nodup_cons.mpr ⟨hk_not_dbJ, dbJ.nodup⟩) + have hJle : J.Pairwise (· ≤ ·) := by + unfold J + exact (dbJ.sorted.pairwise.imp le_of_lt).orderedInsert k + have hJsorted : J.SortedLT := + List.sortedLT_iff_pairwise.mpr (DB.pairwise_lt_of_le_of_nodup hJle hJnodup) + have hJrange : ∀ x ∈ J, + (((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + unfold J at hx + rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact ⟨WithTop.coe_le_coe.mpr hk_le_keyAfter, hkB⟩ + · exact hJ'range x hx + have hJgt_last : ∀ x ∈ J, last < x := by + intro x hx + have h1 := (hJrange x hx).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1) + have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy) + -- sorted database with the empty key, the reported keys and `T - 1` gap keys + have hsorted0 : (([] : Key) :: (res.keys ++ dbJ.keys)).SortedLT := by + rw [List.sortedLT_cons] + refine ⟨?_, ?_⟩ + · intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hnil_lt_mem x hx + · exact lt_trans hnil_lt_lastK (hJ'gt_last x hx) + · exact List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hres_lt_J'⟩ + let db0 : DB := ⟨([] : Key) :: (res.keys ++ dbJ.keys), hsorted0⟩ + -- sorted database with the empty key, the reported keys and `T` gap keys + have hsorted1 : (([] : Key) :: (res.keys ++ J)).SortedLT := by + rw [List.sortedLT_cons] + refine ⟨?_, ?_⟩ + · intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hnil_lt_mem x hx + · exact lt_trans hnil_lt_lastK (hJgt_last x hx) + · exact List.sortedLT_append.mpr ⟨hsort, hJsorted, hres_lt_J⟩ + let db1 : DB := ⟨([] : Key) :: (res.keys ++ J), hsorted1⟩ + have hcomp1 : RangeRead.Compatible db1 r res := + compatible_of_nil_cons_gapKeys_high hrev hmore hne hbegin hendoff hlim htq hheadne + hlastE hgap hsort hJsorted + (by intro x hx; rw [hlastD]; exact hJrange x hx) + (by rw [hJlen']) rfl + -- the database without the target is incompatible: the empty key enters the window + have hnotcomp0 : ¬ RangeRead.Compatible db0 r res := by + intro hc0 + have hfil0 := ((compatible_iff_truncated_keys (db := db0) hrev hmore hne).mp hc0).2 + rw [← hlast] at hfil0 + have hcnt0 : cnt db0 r.begin.key = 1 + t + (T - 1) := by + rw [cnt, List.countP_eq_length_filter] + have hf : db0.keys.filter (fun x : Key => decide (((x : Key) : WithTop Key) < + r.begin.key)) = db0.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl] at hx + rcases List.mem_cons.mp hx with rfl | hx + · exact hnil_lt_B + · rcases List.mem_append.mp hx with hx | hx + · exact hres_lt_B x hx + · exact (hJ'range x hx).2 + rw [hf, show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl] + rw [List.length_cons, List.length_append, ht, hJlen] + omega + have hanchor0 : r.begin.anchor db0 = 0 := by + rw [anchor_eq_count_sub_slack db0 r (le_of_lt hbegin)] + change ((cnt db0 r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 0 + rw [hcnt0, ← hq] + have : ((1 + t + (T - 1) : ℕ) : ℤ) - (q : ℤ) - 1 = 0 := by rw [hT]; omega + exact this + have hbot_mem : ([] : Key) ∈ db0.keys := by + rw [show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl] + exact List.mem_cons_self .. + have hresB0 : resolve db0 r.begin = ((↑([] : Key)) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db0 := by rw [hanchor0] + have hlt : (r.begin.anchor db0).toNat < db0.keys.length := by + rw [hanchor0]; exact List.length_pos_of_mem hbot_mem + have hval : db0.keys[(r.begin.anchor db0).toNat]'(hlt) = ([] : Key) := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hlt)] + have hidx : (r.begin.anchor db0).toNat = 0 := by rw [hanchor0]; rfl + rw [hidx, List.getElem?_eq_getElem (h := List.length_pos_of_mem hbot_mem)] + exact congrArg some (keys_getElem_zero_eq_nil_of_mem hbot_mem) + rw [resolve_eq_getElem h0 hlt] + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval + have hresE0 : ((last : Key) : WithTop Key) < resolve db0 r.end_ := + lt_resolve_end_of_lt_end_key (db := db0) hendoff hlastE' + have hnil_ck : ([] : Key) ∈ RangeRead.completeKeys db0 r := by + rw [RangeRead.mem_completeKeys] + refine ⟨hbot_mem, ?_, ?_⟩ + · rw [hresB0] + · exact lt_trans hnil_lt_last hresE0 + have hnil_fil : ([] : Key) ∈ (RangeRead.completeKeys db0 r).filter + (fun x : Key => decide (x ≤ last)) := by + rw [List.mem_filter] + exact ⟨hnil_ck, by rw [decide_eq_true_eq]; exact nil_le_key last⟩ + rw [hfil0] at hnil_fil + have hnil_not_mem : ([] : Key) ∉ res.keys := by + intro hmem + exact (not_le.mpr hnil_lt_head) (hhead_le [] hmem) + exact hnil_not_mem hnil_fil + -- the two databases differ only at the target + have hagree : AgreeOn cr db1 db0 := by + unfold AgreeOn ConflictRange.keys + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db0.sorted.pairwise.filter _) + · intro x + simp only [List.mem_filter, decide_eq_true_eq] + have hdbiff : x ∈ db1.keys ↔ x = k ∨ x ∈ db0.keys := by + show x ∈ ([] : Key) :: (res.keys ++ J) ↔ + x = k ∨ x ∈ ([] : Key) :: (res.keys ++ dbJ.keys) + unfold J + simp only [List.mem_cons, List.mem_append, List.mem_orderedInsert] + tauto + constructor + · rintro ⟨hdb, hcr⟩ + rcases (hdbiff.mp hdb) with rfl | hdb0 + · exact absurd hcr hkcr + · exact ⟨hdb0, hcr⟩ + · rintro ⟨hdb, hcr⟩ + exact ⟨hdbiff.mpr (Or.inr hdb), hcr⟩ + exact hnotcomp0 ((hs db1 db0 hagree).mp hcomp1) + +/-- **Minimality of the nonempty-head high-capacity gap closed form.** The minimal range +is `[⊥, begin.key)`: soundness transfers across agreement on that range, the reported span +is necessary by convexity, and every key below the reported head or in the gap +`[last, begin.key)` is necessary. -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_gapHigh {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapHigh r res) : + Minimal r res (closedFormNegCrossTruncClampGapHigh r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadne, hlastE, + hcap⟩ := hcell + have hcr : closedFormNegCrossTruncClampGapHigh r res = + (⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl + have hsound : Sound r res (closedFormNegCrossTruncClampGapHigh r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp_gapHigh hrev hmore hne + hbegin hendoff hgap hlastE h hagree, + fun h => compatible_of_agree_negCross_trunc_clamp_gapHigh hrev hmore hne + hbegin hendoff hgap hlastE h hagree.symm⟩ + rw [hcr] at hsound ⊢ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k _ hkend + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne + have hhi : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + have hsort : res.keys.SortedLT := + RangeRead.compatible_sortedLT hread.choose_spec hrev + have hlastGet : res.keys.getLastD ([] : Key) = res.keys.getLast hne := + hlastD.trans hlast + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) + (by rw [← hlastGet]; exact hlastE) + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hcd := hcrossD + simp only [leastReported, hrev] at hcd + rw [hheadD] at hcd + rw [hhead] at hcd + exact hcd + by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_below_head_of_negCross_trunc_clamp hread hrev hmore hne hbegin + hendoff hcross hclamp hkhead + · have hkhead' : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + le_of_not_gt hkhead + by_cases hkle : k ≤ res.keys.getLastD ([] : Key) + · have hkle' : k ≤ last := by rw [hlastD] at hkle; exact hkle + exact necessary_of_between_result hread (a := head) (b := last) + (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead') hkle' + · have hkgt : res.keys.getLastD ([] : Key) < k := lt_of_not_ge hkle + exact necessary_gap_high hread ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, + hclamp, hheadne, hlastE, hcap⟩ hkgt hkend + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncClampAny.lean b/FormalConflictRange/NegCrossTruncClampAny.lean new file mode 100644 index 0000000..d962847 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncClampAny.lean @@ -0,0 +1,1054 @@ +/- +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.NegCrossTruncClamp + +/-! +# Arbitrary-end high-capacity clamping gaps + +This module extends the positive-end clamping certificates to end keys inside the reported +window. When the gap above the reported last has room for the target in addition to a full +begin slack of other keys, the reported keys and those other gap keys form a compatible +database. Inserting the target lifts the begin resolution strictly above the reported +head, so the target is necessary regardless of the end key. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1200000 + +/-- Split the half-open interval above a key at its immediate successor. -/ +theorem intervalCard_last_eq_keyAfter_add_one {last : Key} {B : WithTop Key} + (hgap : (((keyAfter last : Key)) : WithTop Key) < B) : + intervalCard ((last : Key) : WithTop Key) B = + intervalCard (((keyAfter last : Key)) : WithTop Key) B + 1 := by + have hcard_one : intervalCard ((last : Key) : WithTop Key) + (((keyAfter last : Key)) : WithTop Key) = 1 := by + have h := intervalCard_succChain last 1 + simpa [keyAfter, List.replicate_one] using h + rw [intervalCard_add (b := ((last : Key) : WithTop Key)) + (m := (((keyAfter last : Key)) : WithTop Key)) (e := B) + (le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap), + hcard_one, add_comm] + +/-- **Insertion necessity in a high-capacity gap, at an arbitrary positive end key.** +Write `T = beginSlack + 1 - #reported`. If the interval +`[keyAfter last, begin.key)` has room for `T` keys avoiding the target, then the reported +keys together with those `T` keys are compatible. Inserting the target moves the negative +begin selector past the reported head, which makes the result incompatible. -/ +theorem necessary_gap_insert_of_capacity {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hcrossD : ((leastReported r res : Key) : WithTop Key) < r.begin.key) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hcap : (((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) + r.begin.key) + (hkgt : res.keys.getLastD ([] : Key) < k) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : r.withinLimit res.keys := hcomp₀.1 + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hkgt' : last < k := by rw [← hlastD]; exact hkgt + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB + have hreported : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hres_lt_B x hx + have htq : t ≤ q := by rw [← hreported, hq]; exact hclamp + have hTpos : 1 ≤ T := by rw [hT]; omega + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hcd := hcrossD + simp only [leastReported, hrev] at hcd + rwa [hheadD] at hcd + have hcnt : (res.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).length ≤ + (r.end_.offset - 1).toNat := by + simpa only [List.countP_eq_length_filter] using + reported_count_ge_le_end_offset hread' hmore hne hendoff + have hcap' : (((T + 1 : ℕ)) : ℕ∞) ≤ + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [hlastD] at hcap + simpa only [hT, hq, ht] using hcap + obtain ⟨dbJ, hJmem, hJavoid, hJlen⟩ := exists_db_keysIn_avoid + (B := (((keyAfter last : Key)) : WithTop Key)) (E := r.begin.key) (k := k) + (m := T) hcap' + have hJgt_last : ∀ x ∈ dbJ.keys, last < x := by + intro x hx + have h1 := (hJmem x hx).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1) + have hJlt_B : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => (hJmem x hx).2 + have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ dbJ.keys, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy) + have hsorted : (res.keys ++ dbJ.keys).SortedLT := + List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hres_lt_J⟩ + let dbw : DB := ⟨res.keys ++ dbJ.keys, hsorted⟩ + have hcompw : RangeRead.Compatible dbw r res := + compatible_of_append_gapKeys_high hrev hmore hne hbegin hendoff hlim htq hcnt hgap + hsort dbJ.sorted + (by intro x hx; rw [hlastD]; exact hJmem x hx) + (by rw [hJlen, hT, hq, ht]) rfl + have hk_le_keyAfter : keyAfter last ≤ k := by + by_contra hcon + rw [not_le] at hcon + exact absurd ((lt_keyAfter_iff k last).mp hcon) (not_le.mpr hkgt') + have hknot_res : k ∉ res.keys := by + intro hk + exact absurd (hlast_le k hk) (not_le.mpr hkgt') + have hknot : k ∉ dbw.keys := by + intro hk + change k ∈ res.keys ++ dbJ.keys at hk + rcases List.mem_append.mp hk with hk | hk + · exact hknot_res hk + · exact hJavoid hk + have hhead_mem : head ∈ dbw.keys := by + change head ∈ res.keys ++ dbJ.keys + exact List.mem_append_left _ (by rw [hhead]; exact List.head_mem hne) + have hk_gt_head : head < k := lt_of_le_of_lt (hhead_le last (by + rw [hlast]; exact List.getLast_mem hne)) hkgt' + have hlen : (dbw.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + have hf : dbw.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) = + dbw.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + change x ∈ res.keys ++ dbJ.keys at hx + rcases List.mem_append.mp hx with hx | hx + · exact ⟨WithTop.coe_le_coe.mpr (hhead_le x hx), hres_lt_B x hx⟩ + · exact ⟨WithTop.coe_le_coe.mpr (le_trans (hhead_le last (by + rw [hlast]; exact List.getLast_mem hne)) (le_of_lt (hJgt_last x hx))), + hJlt_B x hx⟩ + rw [hf] + change (res.keys ++ dbJ.keys).length = beginSlack r + 1 + rw [List.length_append, hJlen, ← ht, ← hq] + omega + apply necessary_of_toggle + refine ⟨dbw, hcompw, ?_⟩ + rw [toggle_eq_insert_of_not_mem hknot] + intro hcins + have hhead_ck : head ∈ RangeRead.completeKeys (dbw.insert k hknot) r := + (RangeRead.compatible_keys_prefix hcins).subset (by rw [hhead]; exact List.head_mem hne) + have hle := (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + exact (not_lt.mpr hle) (resolve_begin_insert_gt hbegin hhead_mem hcross hknot + hk_gt_head hkB hlen) + +/-- **A saturated empty-key end window excludes padding below the reported head.** +If `end_.key = ⊥` and `(end_.offset - 1).toNat` is exactly the number of reported keys, +then a compatible forward truncated read has no stored key strictly below its reported +head. Such a key and all reported keys would lie at or below the reported last, exceeding +the end-count bound forced by `last < resolve end_`. -/ +theorem not_lt_head_of_mem_of_compatible_saturated {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) (hendoff : 1 ≤ r.end_.offset) + (hE : r.end_.key = (([] : Key) : WithTop Key)) + (hsat : (r.end_.offset - 1).toNat = res.keys.length) + {x : Key} (hxdb : x ∈ db.keys) (hxhead : x < res.keys.head hne) : False := by + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hhead_le : ∀ y ∈ res.keys, head ≤ y := by + intro y hy + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hy + have hlast_le : ∀ y ∈ res.keys, y ≤ last := by + intro y hy + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hy + have hxhead' : x < head := by rwa [hhead] + have hxnot : x ∉ res.keys := by + intro hx + exact (not_lt.mpr (hhead_le x hx)) hxhead' + have hxlast : x ≤ last := by + exact le_of_lt (lt_of_lt_of_le hxhead' (hhead_le last (by + rw [hlast]; exact List.getLast_mem hne))) + have hlast_ck : last ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset (by + rw [hlast]; exact List.getLast_mem hne) + have hlastE : ((last : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hcnt := filter_length_le_offset_of_resolve_end_gt (db := db) hendoff hlastE + have hfilterE : (db.keys.filter + (fun y : Key => decide (r.end_.key ≤ (y : WithTop Key)))).filter + (fun y : Key => decide (y ≤ last)) = db.keys.filter (fun y : Key => decide (y ≤ last)) := by + have hinner : db.keys.filter + (fun y : Key => decide (r.end_.key ≤ (y : WithTop Key))) = db.keys := by + rw [List.filter_eq_self] + intro y hy + rw [decide_eq_true_eq, hE] + exact coe_nil_le_key_withTop _ + rw [hinner] + rw [hfilterE, hsat] at hcnt + have hnodup : (x :: res.keys).Nodup := List.nodup_cons.mpr ⟨hxnot, hsort.nodup⟩ + have hsubset : (x :: res.keys) ⊆ db.keys.filter (fun y : Key => decide (y ≤ last)) := by + intro y hy + rw [List.mem_filter, decide_eq_true_eq] + rcases List.mem_cons.mp hy with rfl | hy + · exact ⟨hxdb, hxlast⟩ + · exact ⟨RangeRead.compatible_keys_subset hcomp hy, hlast_le y hy⟩ + have hlen := hnodup.length_le_of_subset hsubset + rw [List.length_cons] at hlen + omega + +/-- If no stored key lies below the reported head, compatibility forces the stored keys +below `keyAfter last` to be exactly the reported list. -/ +theorem cnt_keyAfter_getLast_eq_length_of_no_lt_head {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hno : ∀ x ∈ db.keys, ¬ x < res.keys.head hne) : + cnt db (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) = res.keys.length := by + have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.head_mem hne) + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.getLast_mem hne) + have hrb := (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + have hlastE := (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hfil := ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2 + rw [cnt, List.countP_eq_length_filter] + have hl : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) = + (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db.sorted.pairwise.filter _ + · have hcp := RangeRead.completeKeys_pairwise (db := db) (r := r) + rw [hrev] at hcp + exact hcp.sublist List.filter_sublist + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hkdb, hkK⟩ + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hkK) + have hkhead : res.keys.head hne ≤ k := le_of_not_gt (hno k hkdb) + have hkrb : resolve db r.begin ≤ ((k : Key) : WithTop Key) := + le_trans hrb (WithTop.coe_le_coe.mpr hkhead) + have hkre : ((k : Key) : WithTop Key) < resolve db r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlastE + exact ⟨RangeRead.mem_completeKeys.mpr ⟨hkdb, hkrb, hkre⟩, hkle⟩ + · rintro ⟨hkck, hkle⟩ + exact ⟨(RangeRead.mem_completeKeys.mp hkck).1, WithTop.coe_lt_coe.mpr + ((lt_keyAfter_iff k (res.keys.getLast hne)).mpr hkle)⟩ + rw [hl, hfil] + +/-- A nonpositive begin anchor resolves at or below the least stored key. -/ +theorem resolve_begin_le_of_anchor_nonpos_of_no_lt_head {db : DB} {r : RangeRead} {head : Key} + (hhead : head ∈ db.keys) (hno : ∀ x ∈ db.keys, ¬ x < head) + (hanchor : r.begin.anchor db ≤ 0) : + resolve db r.begin ≤ ((head : Key) : WithTop Key) := by + rcases lt_or_eq_of_le hanchor with hneg | heq + · rw [resolve_eq_nil hneg] + exact coe_nil_le_key_withTop _ + · have h0 : 0 ≤ r.begin.anchor db := le_of_eq heq.symm + have hlt : (r.begin.anchor db).toNat < db.keys.length := by + rw [heq] + simpa using List.length_pos_of_mem hhead + rw [resolve_eq_getElem h0 hlt] + have hmin : ∀ x ∈ db.keys, head ≤ x := fun x hx => le_of_not_gt (hno x hx) + have hget : db.keys[(r.begin.anchor db).toNat] = head := by + have hidx : (r.begin.anchor db).toNat = 0 := by rw [heq]; rfl + have hcount : db.keys.countP (fun x : Key => decide (x < head)) = 0 := + countP_lt_eq_zero_of_forall_ge hmin + simpa only [hidx, hcount] using getElem_countP_lt db.sorted hhead + rw [hget] + +/-- When there are no stored keys below `head`, two forward reads whose begin resolutions +are at or below `head` have the same complete window. -/ +theorem completeKeys_noGapRead_eq_of_no_lt_head {db : DB} {r : RangeRead} {res : Result} + {head : Key} (hrev : r.reverse = false) (hno : ∀ x ∈ db.keys, ¬ x < head) + (hbr : resolve db r.begin ≤ ((head : Key) : WithTop Key)) + (hbng : resolve db (noGapRead r res).begin ≤ ((head : Key) : WithTop Key)) : + RangeRead.completeKeys db r = RangeRead.completeKeys db (noGapRead r res) := by + unfold RangeRead.completeKeys RangeRead.inRange + simp only [noGapRead, List.getLastD_eq_getLast?] at hbng ⊢ + simp only [hrev, Bool.false_eq_true, ite_false] + apply List.filter_congr + intro k hk + have hhead : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_not_gt (hno k hk)) + have h1 := le_trans hbr hhead + have h2 := le_trans hbng hhead + simp [h1, h2] + +/-- A database consisting of the empty key, the reported keys, and `T` gap keys is +compatible whenever its begin selector resolves to the reported head and its reported +end-filter count fits. Unlike `compatible_of_nil_cons_gapKeys_high`, the end key is +arbitrary; the explicit count hypothesis replaces `last < end.key`. -/ +theorem compatible_of_nil_cons_gapKeys_high_of_end_count {r : RangeRead} {res : Result} + {db : DB} {J : List Key} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hlim : r.withinLimit res.keys) + (htq : res.keys.length ≤ beginSlack r) + (hheadne : res.keys.headD ([] : Key) ≠ ([] : Key)) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hsort : res.keys.SortedLT) (hJ : J.SortedLT) + (hJrange : ∀ x ∈ J, (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤ + ((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key) + (hJlen : J.length = beginSlack r + 1 - res.keys.length) + (hkeys : db.keys = ([] : Key) :: (res.keys ++ J)) + (hcnt : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne))).length ≤ + (r.end_.offset - 1).toNat) : + RangeRead.Compatible db r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne + have hJlen' : J.length = T := by omega + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hJgt_last : ∀ x ∈ J, last < x := by + intro x hx + have h := (hJrange x hx).1 + rw [hlastD] at h + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h) + have hJlt_B : ∀ x ∈ J, ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => (hJrange x hx).2 + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + exact lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) + (by rw [← hlastD]; exact hgap) + have hcross : ((head : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hhead_le last (by + rw [hlast]; exact List.getLast_mem hne))) hlastB + have hnil_lt_head : ([] : Key) < head := + lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne') + have hhead_mem : head ∈ db.keys := by + rw [hkeys] + exact List.mem_cons_of_mem _ (List.mem_append_left _ (by + rw [hhead]; exact List.head_mem hne)) + have hfilterB : db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) = + res.keys ++ J := by + rw [hkeys, List.filter_cons] + have hnil : ¬ (((head : Key) : WithTop Key) ≤ ((([] : Key) : WithTop Key)) ∧ + ((([] : Key) : WithTop Key)) < r.begin.key) := by + intro h + exact (not_le.mpr (WithTop.coe_lt_coe.mpr hnil_lt_head)) h.1 + rw [if_neg (by simpa only [decide_eq_true_eq] using hnil)] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · exact ⟨WithTop.coe_le_coe.mpr (hhead_le x hx), + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB⟩ + · exact ⟨WithTop.coe_le_coe.mpr (le_trans (hhead_le last (by + rw [hlast]; exact List.getLast_mem hne)) (le_of_lt (hJgt_last x hx))), + hJlt_B x hx⟩ + have hlenB : (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length = + beginSlack r + 1 := by + rw [hfilterB, List.length_append, hJlen, ← ht, ← hq] + omega + have hresB : resolve db r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_filter_len hbegin hhead_mem hcross hlenB + have hresE : ((last : Key) : WithTop Key) < resolve db r.end_ := by + apply resolve_end_gt_of_filter_length_le (db := db) hendoff + exact hcnt + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (head ≤ k) && decide + (((k : Key) : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, ite_eq_right (by simp), hresB] + simp [WithTop.coe_le_coe] + have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + res.keys := by + rw [hck, hkeys, List.filter_filter, List.filter_cons, List.filter_append] + have hnil_pred : ¬ (decide ((([] : Key)) ≤ last) && (decide (head ≤ (([] : Key))) && + decide ((([] : Key) : WithTop Key) < resolve db r.end_))) = true := by + intro hcon + rw [Bool.and_eq_true, Bool.and_eq_true] at hcon + exact (not_le.mpr hnil_lt_head) (of_decide_eq_true hcon.2.1) + rw [if_neg hnil_pred] + have h1 : res.keys.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) && + decide (((k : Key) : WithTop Key) < resolve db r.end_))) = res.keys := by + rw [List.filter_eq_self] + intro k hk + rw [Bool.and_eq_true, Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq, + decide_eq_true_eq] + exact ⟨hlast_le k hk, hhead_le k hk, + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le k hk)) hresE⟩ + have h2 : J.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) && + decide (((k : Key) : WithTop Key) < resolve db r.end_))) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk hcon + rw [Bool.and_eq_true] at hcon + exact absurd (of_decide_eq_true hcon.1) (not_le.mpr (hJgt_last k hk)) + rw [h1, h2, List.append_nil] + rw [compatible_iff_truncated_keys (db := db) hrev hmore hne] + exact ⟨hlim, hfil⟩ + +/-- The empty key can be padded to the reported keys and gap keys without exceeding the +end-count bound, unless the end selector is the saturated empty-key window. Away from +`end_.key = ⊥`, the empty key is invisible to the end filter. At `end_.key = ⊥`, an +unsaturated window has room for it in addition to all the reported keys. -/ +theorem end_count_nil_cons_gapKeys_of_unsaturated {r : RangeRead} {res : Result} + {db : DB} {J : List Key} (hread : Readable r res) + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hendoff : 1 ≤ r.end_.offset) + (hunsat : r.end_.key ≠ (([] : Key) : WithTop Key) ∨ + (r.end_.offset - 1).toNat ≠ res.keys.length) + (hkeys : db.keys = ([] : Key) :: (res.keys ++ J)) + (hJgt : ∀ x ∈ J, res.keys.getLast hne < x) : + ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne))).length ≤ + (r.end_.offset - 1).toNat := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set last : Key := res.keys.getLast hne with hlast + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hbase : (res.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).length ≤ + (r.end_.offset - 1).toNat := by + simpa only [List.countP_eq_length_filter] using + reported_count_ge_le_end_offset ⟨db₀, hcomp₀⟩ hmore hne hendoff + have hlast_filter : db.keys.filter (fun k : Key => decide (k ≤ last)) = + ([] : Key) :: res.keys := by + rw [hkeys, List.filter_cons, List.filter_append] + have hnil : decide (([] : Key) ≤ last) = true := by simp [nil_le_key] + rw [if_pos hnil] + have h1 : res.keys.filter (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hlast_le x hx) + have h2 : J.filter (fun k : Key => decide (k ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hcon + exact (not_le.mpr (by rw [hlast]; exact hJgt x hx)) (of_decide_eq_true hcon) + rw [h1, h2, List.append_nil] + rw [List.filter_comm] + rw [show db.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + ([] : Key) :: res.keys from by rwa [← hlast]] + by_cases hE : r.end_.key = (([] : Key) : WithTop Key) + · have hresE : res.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq, hE] + exact coe_nil_le_key_withTop _ + have hnilE : decide (r.end_.key ≤ ((([] : Key) : WithTop Key))) = true := by + rw [decide_eq_true_eq, hE] + rw [List.filter_cons, if_pos hnilE, hresE, List.length_cons] + have hbase' : res.keys.length ≤ (r.end_.offset - 1).toNat := by + rwa [hresE] at hbase + have hne_count := hunsat.resolve_left (not_not.mpr hE) + omega + · have hnilE : decide (r.end_.key ≤ ((([] : Key) : WithTop Key))) = false := by + rw [decide_eq_false_iff_not] + intro hle + exact hE (le_antisymm hle (coe_nil_le_key_withTop _)) + rw [List.filter_cons, hnilE] + simpa only [Bool.false_eq_true, ↓reduceIte] using hbase + +/-- A nonempty-head clamping gap with room for the full begin slack and an unsaturated +end window. The arbitrary-end padded witness applies to this whole region. -/ +abbrev negCrossTruncClampGapPadded (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ + res.keys.headD ([] : Key) ≠ ([] : Key) ∧ + ((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) + r.begin.key ∧ + (r.end_.key ≠ (([] : Key) : WithTop Key) ∨ + (r.end_.offset - 1).toNat ≠ res.keys.length) + +/-- **Gap keys are necessary at an arbitrary positive end key when the end window is +unsaturated.** The witness stores the empty key, the reported keys, and `T` gap keys +including the target. Its begin selector resolves to the reported head. Erasing the +target lowers the begin anchor to zero, so the empty key enters the truncated window. -/ +theorem necessary_gap_padded {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncClampGapPadded r res) + (hkgt : res.keys.getLastD ([] : Key) < k) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadne, hcap, + hunsat⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : r.withinLimit res.keys := hcomp₀.1 + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hkgt' : last < k := by rw [← hlastD]; exact hkgt + have hlast_le : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hreported : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB + have htq : t ≤ q := by rw [← hreported, hq]; exact hclamp + have hTpos : 1 ≤ q + 1 - t := by omega + have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB + have hnil_lt_head : ([] : Key) < head := + lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne') + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hnil_lt_mem : ∀ x ∈ res.keys, ([] : Key) < x := by + intro x hx + exact lt_of_lt_of_le hnil_lt_head (hhead_le x hx) + have hnil_lt_B : (([] : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr hnil_lt_head) + (hres_lt_B head (List.head_mem hne)) + have hnil_lt_lastK : ([] : Key) < last := + lt_of_lt_of_le hnil_lt_head (hlast_le head (List.head_mem hne)) + have hnil_lt_last : (([] : Key) : WithTop Key) < ((last : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr hnil_lt_lastK + have hD : ((T : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [hlastD] at hcap + simpa only [hT, hq, ht] using hcap + have hcard' : (((T - 1) + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [Nat.sub_add_cancel hTpos] + exact hD + obtain ⟨dbJ, hJmem, hJavoid, hJlen⟩ := exists_db_keysIn_avoid + (B := (((keyAfter last : Key)) : WithTop Key)) (E := r.begin.key) (k := k) + (m := T - 1) hcard' + have hJ'range : ∀ x ∈ dbJ.keys, + (((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key := + fun x hx => hJmem x hx + have hJ'gt_last : ∀ x ∈ dbJ.keys, last < x := by + intro x hx + have h1 := (hJ'range x hx).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1) + have hres_lt_J' : ∀ x ∈ res.keys, ∀ y ∈ dbJ.keys, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJ'gt_last y hy) + have hk_le_keyAfter : keyAfter last ≤ k := by + by_contra hcon + rw [not_le] at hcon + exact absurd ((lt_keyAfter_iff k last).mp hcon) (not_le.mpr hkgt') + have hk_not_res : k ∉ res.keys := by + intro hk + exact absurd (hlast_le k hk) (not_le.mpr hkgt') + have hk_ne_nil : k ≠ ([] : Key) := by + intro h + have hnil_lt_k : ([] : Key) < k := lt_of_le_of_lt (nil_le_key last) hkgt' + rw [h] at hnil_lt_k + exact (lt_irrefl ([] : Key)) hnil_lt_k + have hk_not_dbJ : k ∉ dbJ.keys := hJavoid + let J : List Key := dbJ.keys.orderedInsert (· ≤ ·) k + have hJlen' : J.length = T := by + unfold J + rw [List.orderedInsert_length, hJlen] + omega + have hJnodup : J.Nodup := by + unfold J + exact (List.perm_orderedInsert (· ≤ ·) k dbJ.keys).nodup_iff.mpr + (List.nodup_cons.mpr ⟨hk_not_dbJ, dbJ.nodup⟩) + have hJle : J.Pairwise (· ≤ ·) := by + unfold J + exact (dbJ.sorted.pairwise.imp le_of_lt).orderedInsert k + have hJsorted : J.SortedLT := + List.sortedLT_iff_pairwise.mpr (DB.pairwise_lt_of_le_of_nodup hJle hJnodup) + have hJrange : ∀ x ∈ J, + (((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + unfold J at hx + rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact ⟨WithTop.coe_le_coe.mpr hk_le_keyAfter, hkB⟩ + · exact hJ'range x hx + have hJgt_last : ∀ x ∈ J, last < x := by + intro x hx + have h1 := (hJrange x hx).1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1) + have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by + intro x hx y hy + exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy) + have hsorted0 : (([] : Key) :: (res.keys ++ dbJ.keys)).SortedLT := by + rw [List.sortedLT_cons] + refine ⟨?_, ?_⟩ + · intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hnil_lt_mem x hx + · exact lt_trans hnil_lt_lastK (hJ'gt_last x hx) + · exact List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hres_lt_J'⟩ + let db0 : DB := ⟨([] : Key) :: (res.keys ++ dbJ.keys), hsorted0⟩ + have hsorted1 : (([] : Key) :: (res.keys ++ J)).SortedLT := by + rw [List.sortedLT_cons] + refine ⟨?_, ?_⟩ + · intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hnil_lt_mem x hx + · exact lt_trans hnil_lt_lastK (hJgt_last x hx) + · exact List.sortedLT_append.mpr ⟨hsort, hJsorted, hres_lt_J⟩ + let db1 : DB := ⟨([] : Key) :: (res.keys ++ J), hsorted1⟩ + have hcnt1 : ((db1.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne))).length ≤ + (r.end_.offset - 1).toNat := + end_count_nil_cons_gapKeys_of_unsaturated (db := db1) (J := J) ⟨db₀, hcomp₀⟩ + hrev hmore hne hendoff hunsat rfl (by + intro x hx + rw [← hlast] + exact hJgt_last x hx) + have hcomp1 : RangeRead.Compatible db1 r res := + compatible_of_nil_cons_gapKeys_high_of_end_count hrev hmore hne hbegin hendoff hlim htq + hheadne hgap hsort hJsorted + (by intro x hx; rw [hlastD]; exact hJrange x hx) + (by rw [hJlen']) rfl hcnt1 + have hnotcomp0 : ¬ RangeRead.Compatible db0 r res := by + intro hc0 + have hfil0 := ((compatible_iff_truncated_keys (db := db0) hrev hmore hne).mp hc0).2 + rw [← hlast] at hfil0 + have hcnt0 : cnt db0 r.begin.key = 1 + t + (T - 1) := by + rw [cnt, List.countP_eq_length_filter] + have hf : db0.keys.filter (fun x : Key => decide (((x : Key) : WithTop Key) < + r.begin.key)) = db0.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl] at hx + rcases List.mem_cons.mp hx with rfl | hx + · exact hnil_lt_B + · rcases List.mem_append.mp hx with hx | hx + · exact hres_lt_B x hx + · exact (hJ'range x hx).2 + rw [hf, show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl] + rw [List.length_cons, List.length_append, ht, hJlen] + omega + have hanchor0 : r.begin.anchor db0 = 0 := by + rw [anchor_eq_count_sub_slack db0 r (le_of_lt hbegin)] + change ((cnt db0 r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 0 + rw [hcnt0, ← hq] + have : ((1 + t + (T - 1) : ℕ) : ℤ) - (q : ℤ) - 1 = 0 := by rw [hT]; omega + exact this + have hbot_mem : ([] : Key) ∈ db0.keys := by + rw [show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl] + exact List.mem_cons_self .. + have hresB0 : resolve db0 r.begin = ((↑([] : Key)) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db0 := by rw [hanchor0] + have hlt : (r.begin.anchor db0).toNat < db0.keys.length := by + rw [hanchor0] + exact List.length_pos_of_mem hbot_mem + have hval : db0.keys[(r.begin.anchor db0).toNat]'(hlt) = ([] : Key) := by + apply Option.some.inj + rw [← List.getElem?_eq_getElem (h := hlt)] + have hidx : (r.begin.anchor db0).toNat = 0 := by rw [hanchor0]; rfl + rw [hidx, List.getElem?_eq_getElem (h := List.length_pos_of_mem hbot_mem)] + exact congrArg some (keys_getElem_zero_eq_nil_of_mem hbot_mem) + rw [resolve_eq_getElem h0 hlt] + exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval + have hlast_ck0 : last ∈ RangeRead.completeKeys db0 r := + (RangeRead.compatible_keys_prefix hc0).subset (by + rw [hlast] + exact List.getLast_mem hne) + have hresE0 : ((last : Key) : WithTop Key) < resolve db0 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck0).2.2 + have hnil_ck : ([] : Key) ∈ RangeRead.completeKeys db0 r := by + rw [RangeRead.mem_completeKeys] + refine ⟨hbot_mem, ?_, ?_⟩ + · rw [hresB0] + · exact lt_trans hnil_lt_last hresE0 + have hnil_fil : ([] : Key) ∈ (RangeRead.completeKeys db0 r).filter + (fun x : Key => decide (x ≤ last)) := by + rw [List.mem_filter] + exact ⟨hnil_ck, by rw [decide_eq_true_eq]; exact nil_le_key last⟩ + rw [hfil0] at hnil_fil + have hnil_not_mem : ([] : Key) ∉ res.keys := by + intro hmem + exact (not_le.mpr hnil_lt_head) (hhead_le [] hmem) + exact hnil_not_mem hnil_fil + have hagree : AgreeOn cr db1 db0 := by + unfold AgreeOn ConflictRange.keys + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db0.sorted.pairwise.filter _) + · intro x + simp only [List.mem_filter, decide_eq_true_eq] + have hdbiff : x ∈ db1.keys ↔ x = k ∨ x ∈ db0.keys := by + show x ∈ ([] : Key) :: (res.keys ++ J) ↔ + x = k ∨ x ∈ ([] : Key) :: (res.keys ++ dbJ.keys) + unfold J + simp only [List.mem_cons, List.mem_append, List.mem_orderedInsert] + tauto + constructor + · rintro ⟨hdb, hcr⟩ + rcases (hdbiff.mp hdb) with rfl | hdb0 + · exact absurd hcr hkcr + · exact ⟨hdb0, hcr⟩ + · rintro ⟨hdb, hcr⟩ + exact ⟨hdbiff.mpr (Or.inr hdb), hcr⟩ + exact hnotcomp0 ((hs db1 db0 hagree).mp hcomp1) + +/-- **Minimality of `[⊥, begin.key)` for an unsaturated padded gap.** Soundness is the +arbitrary-end clamping argument. Keys below the reported head use the reported-list +witness, reported-span keys use convexity, and gap keys use `necessary_gap_padded`. -/ +theorem minimal_beginRange_of_gap_padded {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapPadded r res) : + Minimal r res (closedFormNegCrossTruncClampNoGapTop r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadne, hcap, + hunsat⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + exact lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hcd := hcrossD + simp only [leastReported, hrev] at hcd + rwa [hheadD] at hcd + have hsound : Sound r res (closedFormNegCrossTruncClampNoGapTop r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin + hendoff (by rwa [← hlast]) h hagree, + fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin + hendoff (by rwa [← hlast]) h hagree.symm⟩ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k _ hkend + by_cases hkhead : ((k : Key) : WithTop Key) < ((head : Key) : WithTop Key) + · exact necessary_below_head_of_negCross_trunc_clamp hread' hrev hmore hne hbegin + hendoff hcross hclamp hkhead + · by_cases hkle : k ≤ last + · exact necessary_of_between_result hread' (a := head) (b := last) + (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) + (WithTop.coe_le_coe.mp (le_of_not_gt hkhead)) hkle + · apply necessary_gap_padded hread' + ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadne, hcap, hunsat⟩ + · rw [hlastD] + exact lt_of_not_ge hkle + · exact hkend + +/-- A genuine clamping gap at exact nonempty-head capacity, with a saturated empty-key +end window. Writing `T = beginSlack + 1 - #reported`, the gap above `keyAfter last` has +exactly `T` available keys, and the end window has exactly one position per reported key. -/ +abbrev negCrossTruncClampGapExactSaturated (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res ≤ beginSlack r ∧ + 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 : ℕ) : ℕ∞) + +/-- Compatibility is unchanged by lowering the begin key in the saturated exact-capacity +cell. Saturation excludes stored keys below the reported head. The exact capacity then +keeps the original begin resolution at or below that head, while the lowered read clamps; +both reads therefore have the same complete window. -/ +theorem compatible_iff_noGapRead_of_gapExactSaturated {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapExactSaturated r res) (db : DB) : + RangeRead.Compatible db r res ↔ RangeRead.Compatible db (noGapRead r res) res := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hE, hsat, hcap⟩ := hcell + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + set q : ℕ := beginSlack r with hq + set T : ℕ := q + 1 - t with hT + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastD' : res.keys.getLast?.getD ([] : Key) = last := by + rw [List.getLast?_eq_some_getLast hne]; rfl + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hreported : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB + have htq : t ≤ q := by rw [← hreported, hq]; exact hclamp + have hcap' : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = + ((T : ℕ) : ℕ∞) := by + rw [hlastD] at hcap + simpa only [hT, hq, ht] using hcap + constructor + · intro hcomp + have hno : ∀ x ∈ db.keys, ¬ x < head := by + intro x hx hlt + exact not_lt_head_of_mem_of_compatible_saturated hcomp hrev hmore hne hendoff hE + hsat hx (by simpa only [hhead] using hlt) + have hcntK : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by + have h := cnt_keyAfter_getLast_eq_length_of_no_lt_head hcomp hrev hmore hne + (by simpa [hhead] using hno) + simpa [hlast, ht] using h + have hanchorNG : (noGapRead r res).begin.anchor db < 0 := by + have h_eq : (noGapRead r res).begin.anchor db = + (cnt db (((keyAfter last : Key)) : WithTop Key) : ℤ) - + (beginSlack r : ℤ) - 1 := by + have hs : beginSlack (noGapRead r res) = beginSlack r := rfl + rw [anchor_eq_count_sub_slack db (noGapRead r res) (le_of_lt hbegin), hs] + simp [cnt, noGapRead, hlastD, hlastD'] + rw [h_eq, hcntK] + have h1 : (t : ℤ) ≤ (q : ℤ) := by exact_mod_cast htq + rw [← hq] + omega + have hbng : resolve db (noGapRead r res).begin ≤ ((head : Key) : WithTop Key) := by + rw [resolve_eq_nil hanchorNG] + exact coe_nil_le_key_withTop _ + have hbr : resolve db r.begin ≤ ((head : Key) : WithTop Key) := by + have hhead_ck : head ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset (by rw [hhead]; exact List.head_mem hne) + exact (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + have hck := completeKeys_noGapRead_eq_of_no_lt_head hrev hno hbr hbng + rw [compatible_iff_truncated_keys (r := noGapRead r res) hrev hmore hne] + exact ⟨by change RangeRead.withinLimit r res.keys; exact hcomp.1, + by rw [← hck]; exact ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2⟩ + · intro hcompNG + have hno : ∀ x ∈ db.keys, ¬ x < head := by + intro x hx hlt + exact not_lt_head_of_mem_of_compatible_saturated (r := noGapRead r res) hcompNG hrev + hmore hne hendoff (by simpa [noGapRead] using hE) (by simpa [noGapRead] using hsat) + hx (by simpa only [hhead] using hlt) + have hcntK : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by + have h := cnt_keyAfter_getLast_eq_length_of_no_lt_head (r := noGapRead r res) hcompNG + hrev hmore hne (by simpa [hhead] using hno) + simpa [hlast, ht] using h + have hbng : resolve db (noGapRead r res).begin ≤ ((head : Key) : WithTop Key) := by + have hhead_ck : head ∈ RangeRead.completeKeys db (noGapRead r res) := + (RangeRead.compatible_keys_prefix hcompNG).subset (by rw [hhead]; exact List.head_mem hne) + exact (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + set g : ℕ := (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length + with hg + have hcntB : cnt db r.begin.key = t + g := by + rw [cnt_eq_add_between db (le_of_lt hgap'), hcntK, ← hg] + have hgT : g ≤ T := by + have hgA : (g : ℕ∞) ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) + r.begin.key := by + rw [hg] + exact between_length_le_intervalCard (db := db) (le_of_lt hgap') + exact ENat.coe_le_coe.mp (hgA.trans hcap'.le) + have htg : t + g ≤ q + 1 := by rw [hT] at hgT; omega + have hanchor : r.begin.anchor db ≤ 0 := by + have h_eq : r.begin.anchor db = + (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)] + simp [cnt] + rw [h_eq, hcntB] + have h1 : ((t + g : ℕ) : ℤ) ≤ (q : ℤ) + 1 := by exact_mod_cast htg + rw [← hq] + omega + have hheadDB : head ∈ db.keys := + RangeRead.compatible_keys_subset hcompNG (by rw [hhead]; exact List.head_mem hne) + have hbr := resolve_begin_le_of_anchor_nonpos_of_no_lt_head hheadDB hno hanchor + have hck := completeKeys_noGapRead_eq_of_no_lt_head hrev hno hbr hbng + rw [compatible_iff_truncated_keys hrev hmore hne] + rw [compatible_iff_truncated_keys (r := noGapRead r res) hrev hmore hne] at hcompNG + exact ⟨by change RangeRead.withinLimit r res.keys; exact hcompNG.1, + by rw [hck]; exact hcompNG.2⟩ + +/-- The lowered read satisfies the arbitrary-end no-gap clamping cell. -/ +theorem negCrossTruncClampNoGap_noGapRead_of_gapExactSaturated {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampGapExactSaturated r res) : + negCrossTruncClampNoGap (noGapRead r res) res := by + obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hE, hsat, hcap⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set t : ℕ := res.keys.length with ht + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hheadD' : res.keys.head?.getD ([] : Key) = head := by + rw [List.head?_eq_some_head hne]; rfl + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := + lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by + intro k hk + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hreported : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB + have htq : t ≤ beginSlack r := by rw [← hreported]; exact hclamp + have hcrossNG : ((leastReported (noGapRead r res) res : Key) : WithTop Key) < + (noGapRead r res).begin.key := by + have hle : leastReported (noGapRead r res) res = head := by + unfold leastReported + rw [show (noGapRead r res).reverse = false from hrev] + simp [hheadD'] + rw [hle, show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne))) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) + have hreportedNG : reportedBelowBegin (noGapRead r res) res = t := by + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + rw [show (noGapRead r res).begin.key = + (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD] + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk)) + have hclampNG : reportedBelowBegin (noGapRead r res) res ≤ + beginSlack (noGapRead r res) := by + rw [hreportedNG] + exact htq + exact ⟨hmore, hrev, hbegin, hne, hendoff, hcrossNG, rfl, hclampNG⟩ + +/-- **The saturated exact-capacity branch has the shorter range `[⊥, keyAfter last)`.** -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_gapExactSaturated + {r : RangeRead} {res : Result} (hread : Readable r res) + (hcell : negCrossTruncClampGapExactSaturated r res) : + Minimal r res (closedFormNegCrossTruncClampGapLow r res) := by + have hiff : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (noGapRead r res) res := + fun db => compatible_iff_noGapRead_of_gapExactSaturated hread hcell db + have hreadNG : Readable (noGapRead r res) res := by + obtain ⟨db₀, hcomp₀⟩ := hread + exact ⟨db₀, (hiff db₀).mp hcomp₀⟩ + have hcellNG : negCrossTruncClampNoGap (noGapRead r res) res := + negCrossTruncClampNoGap_noGapRead_of_gapExactSaturated hread hcell + exact (minimal_congr_of_compatible_iff hiff).mpr + (minimal_closedForm_of_negCross_trunc_clamp_noGap hreadNG hcellNG) + +/-- **Minimality of `[⊥, begin.key)` under the insertion-capacity bound.** Soundness is +the arbitrary-end clamping argument. Keys below the reported head use the reported-list +toggle witness, keys in the reported span are necessary by convexity, and gap keys use +`necessary_gap_insert_of_capacity`. -/ +theorem minimal_beginRange_of_gap_insert_capacity {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset) + (hcrossD : ((leastReported r res : Key) : WithTop Key) < r.begin.key) + (hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hcap : (((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) + r.begin.key) : + Minimal r res (closedFormNegCrossTruncClampNoGapTop r res) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by + have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hgap + exact lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap' + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hcd := hcrossD + simp only [leastReported, hrev] at hcd + rwa [hheadD] at hcd + have hsound : Sound r res (closedFormNegCrossTruncClampNoGapTop r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin + hendoff (by rwa [← hlast]) h hagree, + fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin + hendoff (by rwa [← hlast]) h hagree.symm⟩ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k _ hkend + by_cases hkhead : ((k : Key) : WithTop Key) < ((head : Key) : WithTop Key) + · exact necessary_below_head_of_negCross_trunc_clamp hread' hrev hmore hne hbegin + hendoff hcross hclamp hkhead + · by_cases hkle : k ≤ last + · exact necessary_of_between_result hread' (a := head) (b := last) + (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) + (WithTop.coe_le_coe.mp (le_of_not_gt hkhead)) hkle + · apply necessary_gap_insert_of_capacity hread' hmore hrev hne hbegin hendoff hcrossD + hgap hclamp hcap + · rw [hlastD] + exact lt_of_not_ge hkle + · exact hkend + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncClampLastGe.lean b/FormalConflictRange/NegCrossTruncClampLastGe.lean new file mode 100644 index 0000000..a01532b --- /dev/null +++ b/FormalConflictRange/NegCrossTruncClampLastGe.lean @@ -0,0 +1,609 @@ +/- +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.NegCrossClampNonpos +import FormalConflictRange.ZeroCrossTrunc + +/-! +# Crossed negative-begin truncated clamping nonpositive-end cell + +This module closes the forward, crossed, truncated, clamping cell at a negative begin +offset with a nonpositive end offset, in the subcase where the reported last reaches +`begin.key`. The minimal sound range is `[⊥, end_.key)`. + +The two ingredients are the complete-result clamp certificate of +`NegCrossClampNonpos.lean`, generalized here from complete to truncated results, and a +tail witness: storing the reported keys together with `1 - end.offset` fillers above the +reported last makes the end selector resolve onto the first filler, so erasing any filler +drops the end resolution to the reported last and breaks the truncated read-off. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- A forward truncated, crossed, negative-begin read with a nonpositive end offset that +clamps and whose reported last reaches `begin.key`. -/ +def negCrossTruncClampLastGe (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧ + r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + +/-- In the clamped cell, a compatible database exists with a negative begin anchor. This +is the truncated analogue of `exists_clamp_compat_nonpos_lastGe`; the constructed database +has `completeKeys` exactly equal to the reported keys, hence is compatible with either +`more` flag. -/ +theorem exists_clamp_compat_nonpos_lastGe_prefix {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) : + ∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?] + simp [List.getLast?_eq_some_getLast hne] + have hlastB : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [← hlastD]; exact hB_le_last + have hlast_ck₀ : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset (List.getLast_mem hne) + have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck₀).2.2 + have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro hbot + rw [hbot] at hlast_lt_R₀ + exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _)) + obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩ + · exact absurd hn hR_ne_nil + · exact absurd ht (resolve_ne_top_of_offset_nonpos hend) + · exact ⟨q, hqeq⟩ + have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key) := by + rw [← hr₀]; exact hlast_lt_R₀ + have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by + rw [← hr₀] + exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil + set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL + have hLlen : L.length = clampNonposD r + 1 := by + rw [hL]; exact between_resolve_end_length hend hR_ne_nil + have hLsorted : L.SortedLT := by + rw [hL] + exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hL] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hresL : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) + (lt_of_lt_of_le hlast_lt_r₀ (le_trans (le_of_eq hr₀.symm) (hLmem y hy).1))) + have hAppSorted : (res.keys ++ L).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hLsorted, hresL⟩ + set db : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdb + have hL_ge_r₀ : ∀ x ∈ L, ((r₀ : Key) : WithTop Key) ≤ (x : WithTop Key) := by + intro x hx + rw [← hr₀] + exact (hLmem x hx).1 + have hL_lt_E : ∀ x ∈ L, (x : WithTop Key) < r.end_.key := fun x hx => (hLmem x hx).2 + have hres_lt_r₀ : ∀ x ∈ res.keys, x < r₀ := by + intro x hx + exact WithTop.coe_lt_coe.mp + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlast_lt_r₀) + have hfilt : db.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [hdb] + rw [List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + intro hcon + exact absurd hcon.1 (not_le.mpr (WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx))) + have h2 : L.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨hL_ge_r₀ x hx, hL_lt_E x hx⟩ + rw [h1, h2] + simp + have hfilt₀ : db₀.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by + rw [hL, ← hr₀] + rfl + have hqspec : (db₀.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).reverse[ + (-r.end_.offset).toNat]? = some r₀ := + resolve_filter_reverse_eq_some_of_ne_nil hend (by simpa [hr₀] using hR_ne_nil) hr₀ + have hR : resolve db r.end_ = ((r₀ : Key) : WithTop Key) := by + refine resolve_eq_of_agree_between (db₁ := db₀) (db₂ := db) (s := r.end_) hend rfl + (le_of_lt hr₀_lt_E) ?_ hqspec (le_refl _) + rw [hfilt₀, hfilt] + have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hdb] + rw [List.countP_append] + have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have h2 : L.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr + (le_trans hlastB (le_trans (le_of_lt hlast_lt_r₀) + (le_trans (le_of_eq hr₀.symm) (hLmem x hx).1))) + rw [h1, h2, Nat.add_zero] + have hanchor : r.begin.anchor db < 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hL_db : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + have hfilt_b : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = res.keys := by + rw [hdb] + rw [List.filter_append] + have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx) + have h2 : L.filter (fun k : Key => decide ((k : WithTop Key) < + ((r₀ : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [decide_eq_true_eq] + rw [← hr₀] + exact not_lt.mpr (hLmem x hx).1 + rw [h1, h2] + simp + have hck : RangeRead.completeKeys db r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hL_db, hR] + rw [filter_between_bot_eq] + exact hfilt_b + refine ⟨db, ⟨hlim, ?_⟩, hanchor⟩ + rcases hcomp₀.2 with ⟨hmore, -, -⟩ | ⟨hmore, -⟩ + · exact Or.inl ⟨hmore, by simpa [hck] using (List.prefix_refl res.keys), hne⟩ + · exact Or.inr ⟨hmore, hck.symm⟩ + +/-- **Soundness of `[⊥, end_.key)` for truncated results.** Agreement on every key below +`end_.key` fixes both resolutions and the complete key list, so compatibility transfers +for either `more` flag. -/ +theorem sound_bottomEnd_of_clamp_nonpos_prefix {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) (hB_le_E : r.begin.key ≤ r.end_.key) : + Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + have step : ∀ db₁ db₂, AgreeOn (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) + db₁ db₂ → RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1 + have hpre : res.keys <+: RangeRead.completeKeys db₁ r := + RangeRead.compatible_keys_prefix hcomp₁ + have hfiltE : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + have hL : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.begin) + (le_of_lt hbegin) hB_le_E hfiltE + have hR : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.end_) + hend le_rfl hfiltE + have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hP : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + have h₁ : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + (db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_ + · have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le + simp [hk, hkE] + · simp [hk] + have h₂ : db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + (db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_ + · have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le + simp [hk, hkE] + · simp [hk] + rw [h₁, h₂, hfiltE] + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), ← hL, ← hR] + exact hP + rcases hcomp₁.2 with ⟨hmore, -, hne⟩ | ⟨hmore, heq⟩ + · exact ⟨hlim, Or.inl ⟨hmore, by rw [← hck]; exact hpre, hne⟩⟩ + · exact ⟨hlim, Or.inr ⟨hmore, by rw [← hck]; exact heq⟩⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- Above the reported last, a stored filler is necessary in the clamped cell: take the +reported keys together with `1 - end.offset` fillers, the first of which is the target. +Erasing the target drops the end resolution onto the reported last, so the truncated +read-off loses its last key. -/ +theorem exists_witness_negCross_trunc_clamp_above_last {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB : r.begin.key ≤ ((last : Key) : WithTop Key) := by + rw [← hlastD]; exact hB_le_last + have hlast_lt_k : ((last : Key) : WithTop Key) < ((k : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr hkgt + have hlast_ck : last ∈ 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 : ((last : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res + have hfill := filler_count_nonpos_more_nonpos_end hcomp₀ hrev hmore hne + (le_of_lt hbegin) hend + set m := (1 - r.end_.offset).toNat with hm + have hm1 : 1 ≤ m := by rw [hm]; omega + set G : List Key := db₀.keys.filter + (fun x : Key => decide (last < x ∧ (x : WithTop Key) < r.end_.key)) with hG + have hGlen : m ≤ G.length := hfill + have hGnodup : G.Nodup := by + rw [hG]; exact db₀.sorted.pairwise.nodup.filter _ + have hGmem : ∀ x ∈ G, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hG] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + set raw : List Key := (k :: G.erase k).take m with hraw + have hrawNodup : raw.Nodup := by + rw [hraw] + refine (List.take_sublist m (k :: G.erase k)).nodup ?_ + exact List.nodup_cons.mpr ⟨hGnodup.not_mem_erase, hGnodup.erase k⟩ + have hrawLen : raw.length = m := by + rw [hraw, List.length_take] + apply Nat.min_eq_left + by_cases hkG : k ∈ G + · rw [List.length_cons, List.length_erase_of_mem hkG]; omega + · rw [List.length_cons, List.erase_of_not_mem hkG]; omega + have hrawSub : ∀ x ∈ raw, x = k ∨ x ∈ G := by + intro x hx + rw [hraw] at hx + rcases List.mem_cons.mp (List.mem_of_mem_take hx) with rfl | hx' + · exact Or.inl rfl + · exact Or.inr (List.mem_of_mem_erase hx') + have hrawk : k ∈ raw := by + rw [hraw, List.mem_take_iff_getElem] + refine ⟨0, ?_, rfl⟩ + rw [List.length_cons] + omega + have hrawGt : ∀ x ∈ raw, last < x := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkgt + · exact (hGmem x hx').1 + have hrawLt : ∀ x ∈ raw, (x : WithTop Key) < r.end_.key := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkE + · exact (hGmem x hx').2 + have hrawGe : ∀ x ∈ raw, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + exact le_trans hlastB (WithTop.coe_le_coe.mpr (le_of_lt (hrawGt x hx))) + set F : List Key := raw.insertionSort (· ≤ ·) with hF + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp hrawNodup) + have hFlen : F.length = m := by rw [hF, List.length_insertionSort, hrawLen] + have hFk : k ∈ F := hFperm.mem_iff.mp hrawk + have hFmem : ∀ x ∈ F, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + exact ⟨hrawGt x (hFperm.mem_iff.mpr hx), hrawLt x (hFperm.mem_iff.mpr hx)⟩ + have hFge : ∀ x ∈ F, r.begin.key ≤ (x : WithTop Key) := fun x hx => + hrawGe x (hFperm.mem_iff.mpr hx) + have hFne : F ≠ [] := by + intro h; rw [h] at hFlen; simp at hFlen; omega + set D : List Key := res.keys ++ F with hD + have hDsort : D.SortedLT := by + rw [hD] + refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩ + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hFmem b hb).1 + let dbw : DB := ⟨D, hDsort⟩ + have hdbw_keys : dbw.keys = D := rfl + -- the begin selector clamps + have hcount : dbw.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hdbw_keys, hD, List.countP_append] + have h1 : res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have h2 : F.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hFge x hx) + rw [h1, h2, Nat.add_zero] + have hanchor : r.begin.anchor dbw < 0 := by + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcount] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hrb : resolve dbw r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor + -- every stored key is below the end key + have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hD] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE + · exact (hFmem x hx).2 + have hlenD : D.length = res.keys.length + m := by + rw [hD, List.length_append, hFlen] + have hm_int : (m : Int) = 1 - r.end_.offset := by + rw [hm]; exact Int.toNat_of_nonneg (by omega) + have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + D.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hDlt x hx + have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD] + push_cast + rw [hm_int] + ring + | coe e => + have hce : D.countP (fun x : Key => decide (x < e)) = D.length := by + rw [← countP_lt_coe (l := D) e, ← hE] + exact hcount_end + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD] + push_cast + rw [hm_int] + ring + have ht_lt : res.keys.length < D.length := by rw [hlenD]; omega + have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by + change D[res.keys.length]? = some (F.head hFne) + rw [hD, List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] + have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)] + rw [hanchor_end, Int.toNat_natCast, hDget?] + have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [hre] + exact WithTop.coe_lt_coe.mpr (hFmem (F.head hFne) (List.head_mem hFne)).1 + -- complete keys of the witness are the reported keys + have hckw : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ last)) = res.keys := by + rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF] + rw [hdbw_keys, hD, List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + ((([] : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨coe_nil_le_key_withTop _, hle_last x hx⟩ + have hF_fil : F.filter (fun x : Key => decide + ((([] : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr (hFmem x hx).1 + rw [hres_fil, hF_fil, List.append_nil] + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨hcomp₀.1, hckw⟩ + have hkdbw : k ∈ dbw.keys := by + rw [hdbw_keys, hD] + exact List.mem_append.mpr (Or.inr hFk) + -- the erased database + have hk_not_mem_res : k ∉ res.keys := by + intro hk + exact absurd (hle_last k hk) (not_le.mpr hkgt) + have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by + rw [DB.erase_keys, hdbw_keys, hD] + exact List.erase_append_right F hk_not_mem_res + have hcount' : (dbw.erase k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hD', List.countP_append] + have h1 : res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have h2 : (F.erase k).countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hFge x (List.mem_of_mem_erase hx)) + rw [h1, h2, Nat.add_zero] + have hanchor' : r.begin.anchor (dbw.erase k) < 0 := by + rw [anchor_eq_count_sub_slack (dbw.erase k) r (le_of_lt hbegin), hcount'] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hrb' : resolve (dbw.erase k) r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil hanchor' + have hD'lt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by + intro x hx + exact hDlt x (by + rw [DB.erase_keys, hdbw_keys] at hx + exact List.mem_of_mem_erase hx) + have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by + rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen] + omega + have hcount_end' : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hD'lt x hx + have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + | coe e => + have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) = + (dbw.erase k).keys.length := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_end' + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne + have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by + rw [Nat.cast_sub ht1]; norm_num + have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_end', hEqInt, Int.toNat_natCast] + have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by + rw [hD', List.getElem?_append] + rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)] + rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)] + congr 1 + rw [hlast, List.getLast_eq_getElem hne] + have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)] + rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?] + have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hrb', hre'] + have hpred : ∀ x ∈ (dbw.erase k).keys, + decide (((↑([] : Key) : WithTop Key)) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((↑last : Key) : WithTop Key)) = + decide (x < last) := by + intro x _ + rw [decide_eq_decide] + exact ⟨fun h => WithTop.coe_lt_coe.mp h.2, + fun h => ⟨coe_nil_le_key_withTop _, WithTop.coe_lt_coe.mpr h⟩⟩ + rw [List.filter_congr hpred, hD', List.filter_append] + conv_lhs => rw [← List.dropLast_append_getLast hne, hlast] + rw [List.filter_append] + have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have h := hsort.pairwise.rel_dropLast_getLast hx + simpa [hlast] using h + rw [hfil] + have hFnil : (F.erase k).filter (fun x : Key => decide (x < last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not] + exact not_lt.mpr (le_of_lt (hFmem x (List.mem_of_mem_erase hx)).1) + rw [hFnil, List.append_nil] + simp + have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + intro h + have heq := h.2 + rw [hckE] at heq + have hfil : res.keys.dropLast.filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [← hlast] + exact hle_last x (List.mem_of_mem_dropLast hx) + rw [hfil] at heq + have hlen := congrArg List.length heq + rw [List.length_dropLast] at hlen + omega + exact ⟨dbw, hcompw, hkdbw, hnot⟩ + +/-- Above the reported last and below the end key, a key is necessary in the clamped cell. -/ +theorem necessary_negCross_trunc_clamp_above_last {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key) : + Necessary r res k := by + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_negCross_trunc_clamp_above_last hread hrev hmore hne hbegin hend hclamp + hB_le_last hkgt hkE + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_erase_of_mem hkdbw] + exact hnot⟩ + +/-- The closed form of the cell: `[⊥, end_.key)`. -/ +noncomputable def closedFormNegCrossTruncClampLastGe (r : RangeRead) (res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- **The clamped nonpositive-end cell with the reported last reaching `begin.key` is +minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_lastGe {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncClampLastGe r res) : + Minimal r res (closedFormNegCrossTruncClampLastGe r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hclamp, hB_le_last⟩ := hcell + 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 hB_le_last' : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [← hlastD]; exact hB_le_last + 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_E : r.begin.key ≤ r.end_.key := le_trans hB_le_last' (le_of_lt hlastE) + obtain ⟨db, hcomp, hanchor⟩ := + exists_clamp_compat_nonpos_lastGe_prefix ⟨db₀, hcomp₀⟩ hrev hne hbegin hend hB_le_last hclamp + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomEnd_of_clamp_nonpos_prefix hrev hbegin hend hB_le_E, ?_⟩ + intro k hkbot hkE + by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead + · push_neg at hkhead + by_cases hklast : ((k : Key) : WithTop Key) ≤ ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread' (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast) + · push_neg at hklast + exact necessary_negCross_trunc_clamp_above_last hread' hrev hmore hne hbegin hend + hclamp hB_le_last (WithTop.coe_lt_coe.mp hklast) hkE + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncClampLong.lean b/FormalConflictRange/NegCrossTruncClampLong.lean new file mode 100644 index 0000000..9ce4f40 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncClampLong.lean @@ -0,0 +1,187 @@ +/- +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.NegCrossClampLongNonpos +import FormalConflictRange.NonposRevClosedForm + +/-! +# The crossed negative-begin truncated long clamp cell at a nonpositive end offset + +A forward truncated result with `end_.key < begin.key`, a negative begin offset, +`end.offset ≤ 0`, a clamping begin selector, and at least one reported key at or above +`end_.key`. In that situation every stored key in the complete window lies below +`resolve end ≤ end_.key ≤ last`, so the truncated result is compatible exactly when the +corresponding complete result is. The minimal sound range is therefore the same as in +the complete long clamp cell: `[⊥, clampLongUpper r res]`. + +The two ingredient lemmas are generic. The first says that when the end selector cuts at +or below the reported last, truncation is invisible: compatibility of the truncated +result is compatibility of the complete result. The second transfers minimality across a +change of the result when the compatible databases are the same. +-/ + +namespace FormalConflictRange + +/-- Minimality is invariant under a change of the result that preserves compatibility. -/ +theorem minimal_congr_of_compatible_iff_result {r : RangeRead} {res res' : Result} + {cr : ConflictRange} + (h : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r res') : + Minimal r res cr ↔ Minimal r res' cr := by + have hS : ∀ cr, Sound r res cr ↔ Sound r res' cr := by + intro cr + unfold Sound + constructor + · intro hs db₁ db₂ hagree + exact (h db₁).symm.trans ((hs db₁ db₂ hagree).trans (h db₂)) + · intro hs db₁ db₂ hagree + exact (h db₁).trans ((hs db₁ db₂ hagree).trans (h db₂).symm) + unfold Minimal + refine ⟨fun ⟨hs, hn⟩ => ⟨(hS cr).mp hs, ?_⟩, fun ⟨hs, hn⟩ => ⟨(hS cr).mpr hs, ?_⟩⟩ + · intro cr' hsub hs' + exact hn cr' hsub ((hS cr').mpr hs') + · intro cr' hsub hs' + exact hn cr' hsub ((hS cr').mp hs') + +/-- When the end selector has a nonpositive offset and its key is at or below the +reported last, the truncated result is compatible exactly when the complete result is: +every stored key of the complete window lies below `resolve end ≤ end_.key ≤ last`. -/ +theorem compatible_truncated_iff_complete_of_end_le_last {db : DB} {r : RangeRead} + {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0) + (hlast : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db r { res with more := false } := by + have hlast' : r.end_.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + rwa [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] at hlast + constructor + · intro hc + obtain ⟨hlim, hfil⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hc + have hck : RangeRead.completeKeys db r = res.keys := by + have hself : (RangeRead.completeKeys db r).filter + (fun (k : Key) => decide (k ≤ res.keys.getLast hne)) = + RangeRead.completeKeys db r := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have hxlt : ((x : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hx).2.2 + have h1 : ((x : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hxlt (resolve_le_key_of_offset_nonpos hend) + have h2 : ((x : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) := + le_of_lt (lt_of_lt_of_le h1 hlast') + exact WithTop.coe_le_coe.mp h2 + rw [hself] at hfil + exact hfil + exact ⟨hlim, Or.inr ⟨rfl, hck.symm⟩⟩ + · intro hc + have hlim : RangeRead.withinLimit r res.keys := hc.1 + have heq : res.keys = RangeRead.completeKeys db r := by + rcases hc.2 with ⟨hm, _⟩ | ⟨-, heq⟩ + · exact absurd hm (by simp) + · exact heq + have hfil : (RangeRead.completeKeys db r).filter + (fun (k : Key) => decide (k ≤ res.keys.getLast hne)) = res.keys := by + rw [← heq] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + exact (compatible_iff_truncated_keys hrev hmore hne).mpr ⟨hlim, hfil⟩ + +/-- The crossed negative-begin truncated long clamp cell at `end.offset ≤ 0`: a +clamping begin selector, `end_.key < begin.key`, and at least one reported key at or above +`end_.key`. -/ +def negCrossTruncClampLongNonpos (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧ + clampNonposBottom r res ∧ r.end_.key < r.begin.key ∧ + r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + +/-- **The crossed negative-begin truncated long clamp cell is minimal with the complete +long clamp closed form `[⊥, clampLongUpper]`.** -/ +theorem minimal_closedForm_of_negCross_trunc_clamp_long_nonpos {r : RangeRead} + {res : Result} (hread : Readable r res) (hcell : negCrossTruncClampLongNonpos r res) : + Minimal r res (closedFormNegCrossClampLongNonpos r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hmq, hbottom, hlong, hlastE⟩ := hcell + let res' : Result := { res with more := false } + have hcompat : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db r res' := + fun db => compatible_truncated_iff_complete_of_end_le_last hread hrev hmore hne hend hlastE + have hread' : Readable r res' := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (hcompat db).mp hcomp⟩ + have hcell' : negCrossClampLongNonpos r res' := by + refine ⟨hrev, hbegin, rfl, hne, hend, ?_, hmq, hbottom, hlong⟩ + simpa [res'] using hcrossD + have hmin := minimal_closedForm_of_negCross_clamp_long_nonpos hread' hcell' + have hcf : closedFormNegCrossClampLongNonpos r res' = + closedFormNegCrossClampLongNonpos r res := by + unfold closedFormNegCrossClampLongNonpos clampLongUpper clampLongCap clampLongC + simp only [res'] + rfl + refine (minimal_congr_of_compatible_iff_result (r := r) (res := res) (res' := res') + (cr := closedFormNegCrossClampLongNonpos r res) hcompat).mpr ?_ + rwa [← hcf] + +/-- The crossed negative-begin truncated short cell at `end.offset ≤ 0`: `end_.key < +begin.key` and at least one reported key at or above `end_.key`. Truncation is invisible +here, because `resolve end ≤ end_.key ≤ last`. Both capacity regimes are included. -/ +def negCrossTruncShortNonpos (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧ + r.end_.key < r.begin.key ∧ + r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + +/-- The closed form of the crossed negative-begin truncated short cell: the complete long +clamp rule when the begin selector clamps, and the nonclamping full-window rule +`[head, begin.key)` otherwise. -/ +noncomputable def closedFormNegCrossTruncShortNonpos (r : RangeRead) (res : Result) : + ConflictRange := + if clampNonposBottom r res then + ⟨(([] : Key) : WithTop Key), clampLongUpper r res⟩ + else ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ + +/-- **The crossed negative-begin truncated short cell is minimal.** Truncation is +invisible (`compatible_truncated_iff_complete_of_end_le_last`), so both capacity regimes +transfer from the complete crossed nonpositive-end theorems. -/ +theorem minimal_closedForm_of_negCross_trunc_short_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncShortNonpos r res) : + Minimal r res (closedFormNegCrossTruncShortNonpos r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hmq, hlong, hlastE⟩ := hcell + by_cases hbottom : clampNonposBottom r res + · rw [closedFormNegCrossTruncShortNonpos, ite_eq_left hbottom] + exact minimal_closedForm_of_negCross_trunc_clamp_long_nonpos hread + ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hmq, hbottom, hlong, hlastE⟩ + · rw [closedFormNegCrossTruncShortNonpos, ite_eq_right hbottom] + let res' : Result := { res with more := false } + have hcompat : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db r res' := + fun db => compatible_truncated_iff_complete_of_end_le_last hread hrev hmore hne hend hlastE + have hread' : Readable r res' := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (hcompat db).mp hcomp⟩ + have hcell' : negCrossNonclampLongNonpos r res' := by + refine ⟨hrev, hbegin, rfl, hne, hend, ?_, ?_, ?_, hlong⟩ + · exact hcrossD + · exact hmq + · exact hbottom + have hmin := minimal_closedForm_of_negCross_nonclamp_long_nonpos hread' hcell' + have hcf : closedFormNegCrossNonclampLongNonpos r res' = + (⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ : + ConflictRange) := by + unfold closedFormNegCrossNonclampLongNonpos + rfl + rw [hcf] at hmin + exact (minimal_congr_of_compatible_iff_result (r := r) (res := res) (res' := res') + (cr := ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩) + hcompat).mpr hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncNonclamp.lean b/FormalConflictRange/NegCrossTruncNonclamp.lean new file mode 100644 index 0000000..43c72f5 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncNonclamp.lean @@ -0,0 +1,1749 @@ +/- +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.NegCrossComplete +import FormalConflictRange.ZeroCrossTrunc +import FormalConflictRange.NonposRevClosedForm + +/-! +# Crossed negative-begin truncated nonclamping unbounded-end cell + +This module closes the forward, crossed, truncated, nonclamping cell at a negative +begin offset with an unbounded end selector (`end_.key = ⊤`, `1 ≤ end_.offset`). In +this cell the begin anchor is exactly one past the reported keys below `begin.key`, so +every compatible database resolves the begin selector onto the reported head, and the +minimal sound conflict range is `[head, max begin.key (keyAfter last))`. + +The key arithmetic is `count_lt_begin_eq_of_negCross_trunc_nonclamp`: the stored keys +below `begin.key` split at the head with exactly `reportedBelowBegin` keys in +`[head, begin.key)`. Soundness then follows because agreement on that range fixes both +the gap count and the truncated complete keys, and the gap keys `(last, begin.key)` are +necessary by a toggle witness on the reported keys. +-/ + +open FormalConflictRange + +set_option maxHeartbeats 1200000 + +namespace FormalConflictRange + +/-- In the nonclamping truncated crossed negative-begin cell, a compatible database +resolves its begin selector onto the reported head. -/ +theorem resolve_begin_eq_head_of_negCross_trunc_nonclamp {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (_hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : beginSlack r < reportedBelowBegin r res) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + 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 + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + 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 hhead_db : head ∈ db.keys := + (RangeRead.mem_completeKeys.mp hhead_ck).1 + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross + -- Count identity: stored keys below `begin.key` split at the head. + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross') + 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 ≤ + (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := 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] at h + exact h + have hcB_ge : beginSlack r + 1 ≤ cB := by + have hN : beginSlack r + 1 ≤ + (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := + Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) + rw [hcB, hsplit] + omega + have hanchor_ge : 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 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_ge (le_of_not_gt h)) + have hresolve_get : resolve db r.begin = + ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) := + resolve_eq_getElem hanchor_ge hltlen + -- The head's rank is below the anchor index, so the resolved key is at least the head. + have hcH_le : db.keys.countP (fun k : Key => decide (k < head)) ≤ (r.begin.anchor db).toNat := 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 htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by + 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] + rw [htoNat] + have hsplit' : cB = db.keys.countP (fun k : Key => decide (k < head)) + + (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := by + rw [hcB]; exact hsplit + omega + have hhead_le_resolve : ((head : Key) : WithTop Key) ≤ resolve db r.begin := by + rw [hresolve_get] + exact WithTop.coe_le_coe.mpr (le_getElem_of_countP_le db.sorted hcH_le hltlen) + exact le_antisymm hle_head hhead_le_resolve + +/-- The forward, crossed, truncated, nonclamping, unbounded-end negative-begin cell. -/ +abbrev negCrossTruncNonclampTop (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ + r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res = beginSlack r + 1 + +/-- The closed form for the cell: from the reported head up to the larger of +`begin.key` and the successor of the reported last. -/ +noncomputable def closedFormNegCrossTruncNonclampTop (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), + max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩ + +/-- The reported keys themselves are compatible with the cell's read. -/ +theorem compatible_self_of_negCross_trunc_nonclamp_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonclampTop r res) : + RangeRead.Compatible ⟨res.keys, RangeRead.compatible_sortedLT + (Classical.choose_spec hread) hcell.2.1⟩ r res := by + classical + obtain ⟨hmore, hrev, hbegin, hendkey, hendoff, hcrossD, hnonclamp⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set head : Key := res.keys.head hne with hhead + let db : DB := ⟨res.keys, hsort⟩ + have hdb : db.keys = res.keys := rfl + have hheadD : res.keys.headD ([] : Key) = head := 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 [leastReported, hrev] using hcrossD + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + rw [← hheadD]; exact hcrossD' + have hcB : res.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have hanchor : r.begin.anchor db = 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hdb, hcB, hnonclamp] + push_cast; ring + have hanchor_nat : (r.begin.anchor db).toNat = 0 := by rw [hanchor]; rfl + have hL : resolve db r.begin = ((head : Key) : WithTop Key) := by + have hlt : (r.begin.anchor db).toNat < db.keys.length := by + rw [hanchor_nat, hdb]; exact List.length_pos_of_ne_nil hne + have hget := resolve_eq_getElem (db := db) (s := r.begin) + (by rw [hanchor]) hlt + rw [hget] + have hidx : db.keys[(r.begin.anchor db).toNat] = head := by + rw [getElem_congr_idx (h' := hanchor_nat)] + simpa [hhead] using + (List.getElem_zero_eq_head (l := res.keys) + (h := List.length_pos_of_ne_nil hne)) + rw [hidx] + have hck : RangeRead.completeKeys db r = res.keys := by + rw [completeKeys_eq_filter_ge_of_end_top (db := db) hrev hendkey hendoff hL, hdb] + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hk) + exact ⟨RangeRead.compatible_withinLimit hcomp₀, Or.inl ⟨hmore, by rw [hck], hne⟩⟩ + +/-- In the nonclamping truncated crossed negative-begin cell, the number of stored +keys below `begin.key` splits at the reported head with exactly `reportedBelowBegin` +keys in `[head, begin.key)`. -/ +theorem count_lt_begin_eq_of_negCross_trunc_nonclamp {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) : + db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.countP (fun k : Key => decide (k < res.keys.head hne)) + + reportedBelowBegin r res := by + 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 + have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega + have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev + 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 hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross') + 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 ≤ + (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := 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] at h + exact h + have hcB_ge : beginSlack r + 1 ≤ cB := by + have hN : beginSlack r + 1 ≤ + (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := + Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le) + rw [hcB, hsplit] + omega + have hanchor_ge : 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 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_ge (le_of_not_gt h)) + have hL := resolve_begin_eq_head_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin + hcross hnonclamp + have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor_ge hltlen + have hval : db.keys[(r.begin.anchor db).toNat] = head := + WithTop.coe_inj.mp (hget.symm.trans hL) + have hcountP : 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] + rw [hcB] + omega + +/-- **Compatibility transfers across agreement on the nonclamping truncated crossed +negative-begin range `[head, max begin.key (keyAfter last))`.** Agreement on the range +fixes the reported head and the stored keys in `[head, begin.key)`, hence the begin +resolution after the nonclamp count identity, and fixes the truncated complete keys. -/ +theorem compatible_of_agree_negCross_trunc_nonclamp_top {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncNonclampTop r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), upper⟩ := by + unfold closedFormNegCrossTruncNonclampTop + rw [hheadD, hlastD, hupper] + have hB_upper : r.begin.key ≤ upper := le_max_left _ _ + have hlast_upper : ((last : Key) : WithTop Key) < upper := + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampTop r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp + have hE1 : resolve db1 r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top hendkey hendoff + have hE2 : resolve db2 r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top hendkey hendoff + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_upper : ((head : Key) : WithTop Key) < upper := + lt_of_lt_of_le hcross hB_upper + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + -- Count identity for `db1`, and the equal gap counts. + have hcount1 := count_lt_begin_eq_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross + hnonclamp_eq + have hcount1' : db1.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db1.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by + simpa [hhead] using hcount1 + have hsplit1 := countP_lt_eq_add_filter_withTop db1.keys db1.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hgaplen1 : (db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by + rw [hcount1'] at hsplit1 + omega + have hgap_eq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge, hlt⟩ + · rintro ⟨hk2, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge, hlt⟩ + have hgaplen2 : (db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by + rw [← hgap_eq, hgaplen1] + have hsplit2 := countP_lt_eq_add_filter_withTop db2.keys db2.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hcount2 : db2.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by + rw [hsplit2, hgaplen2] + have cB2_ge : beginSlack r + 1 ≤ db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hcount2, ← hnonclamp_eq]; omega + have hanchor2 : 0 ≤ r.begin.anchor db2 := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))) : ℤ) := by + exact_mod_cast cB2_ge + omega + have htoNat2 : (r.begin.anchor db2).toNat = db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - + (beginSlack r + 1) := by + have hz : r.begin.anchor db2 = ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ)) := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)]; push_cast; ring + have hz2 : ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ) = + ((db2.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)) - + (beginSlack r + 1) : ℕ) : ℤ) := by + rw [← Nat.cast_sub cB2_ge] + rw [hz, hz2, Int.toNat_natCast] + have hidx_eq2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k => decide (k < head)) := by + rw [htoNat2, hcount2, hnonclamp_eq] + omega + have hltlen2 : (r.begin.anchor db2).toNat < db2.keys.length := by + rw [hidx_eq2] + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_db2 + rw [countP_lt_eq_of_getElem db2.sorted hi hix] + exact hi + have hval2 : db2.keys[(r.begin.anchor db2).toNat]'hltlen2 = head := by + simpa [hidx_eq2] using getElem_countP_lt db2.sorted hhead_db2 + have hL2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := by + rw [resolve_eq_getElem (db := db2) (s := r.begin) hanchor2 hltlen2] + exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval2 + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := by + rw [hE1]; exact WithTop.coe_lt_top last + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by + rw [hE2]; exact WithTop.coe_lt_top last + have hfilter1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hfilter2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 + have hres1fil : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hfeq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge, hle⟩ + · rintro ⟨hk2, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge, hle⟩ + have hres2fil : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hfilter2, ← hfeq, ← hfilter1, hres1fil] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, by simpa [hlast] using hres2fil⟩ + +/-- Inserting a key strictly above every element of a list appends it. -/ +theorem orderedInsert_append_of_forall_lt' {l : List Key} {k : Key} + (h : ∀ x ∈ l, x < k) : l.orderedInsert (· ≤ ·) k = l ++ [k] := by + induction l with + | nil => simp + | cons a t ih => + have ha : ¬ k ≤ a := fun hka => absurd hka (not_le.mpr (h a (List.mem_cons_self ..))) + rw [List.orderedInsert_cons, ite_eq_right ha, + ih (fun x hx => h x (List.mem_cons_of_mem a hx))] + simp + +/-- **Gap keys are necessary.** A key strictly between the reported last and +`begin.key` is necessary: inserting it into the database of reported keys moves the +begin anchor from `0` to `1`, so the reported head is no longer the resolved begin. -/ +theorem necessary_negCross_trunc_gap {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + (hkgt : res.keys.getLast hne < k) (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + let db : DB := ⟨res.keys, hsort⟩ + have hdb : db.keys = res.keys := 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 hcell : negCrossTruncNonclampTop r res := + ⟨hmore, hrev, hbegin, hendkey, hendoff, by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD]; exact hcross, hnonclamp_eq⟩ + have hcomp : RangeRead.Compatible db r res := + compatible_self_of_negCross_trunc_nonclamp_top ⟨db₀, hcomp₀⟩ hcell + have hle_last : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hk_notmem : k ∉ db.keys := by + rw [hdb] + exact fun hk => absurd (hle_last k hk) (not_le.mpr hkgt) + refine necessary_of_toggle ⟨db, hcomp, ?_⟩ + intro hcompk + have htoggle : toggle db k = db.insert k hk_notmem := by + unfold toggle + rw [dite_eq_right hk_notmem] + rw [htoggle] at hcompk + set db' : DB := db.insert k hk_notmem with hdb' + have hkeys' : db'.keys = res.keys ++ [k] := by + rw [hdb', DB.insert_keys] + exact orderedInsert_append_of_forall_lt' + (fun x hx => lt_of_le_of_lt (hle_last x hx) hkgt) + have hcount' : db'.keys.countP (fun x : Key => decide + (((x : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res + 1 := by + rw [hkeys', List.countP_append] + have hkdec : decide (((k : Key) : WithTop Key) < r.begin.key) = true := by + rw [decide_eq_true_eq]; exact hkB + rw [List.countP_singleton, hkdec, ite_eq_left rfl] + have hres_count : res.keys.countP (fun x : Key => decide + (((x : Key) : WithTop Key) < r.begin.key)) = reportedBelowBegin r res := rfl + rw [hres_count] + have hanchor' : r.begin.anchor db' = 1 := by + rw [anchor_eq_count_sub_slack db' r (le_of_lt hbegin), hcount', hnonclamp_eq] + push_cast; ring + have hL' : resolve db' r.begin = ((res.keys.head hne : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_trunc_nonclamp hcompk hrev hmore hne hbegin hcross + (by rw [hnonclamp_eq]; omega) + -- But the count identity for the compatible `db'` would force one stored key + -- below the reported head, while `db'.keys = res.keys ++ [k]` has none. + have hcount_lem := count_lt_begin_eq_of_negCross_trunc_nonclamp hcompk hrev hmore hne hbegin + hcross hnonclamp_eq + have hzero_count : db'.keys.countP (fun x : Key => decide (x < res.keys.head hne)) = 0 := by + apply countP_lt_eq_zero_of_forall_ge + intro x hx + rw [hkeys'] at hx + rcases List.mem_append.mp hx with hx | hx + · exact (hsort.pairwise.imp le_of_lt).rel_head hx + · simp only [List.mem_singleton] at hx + subst hx + exact le_trans ((hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)) + (le_of_lt hkgt) + rw [hcount', hzero_count] at hcount_lem + omega + +/-- **Gap keys are necessary for any end selector.** A key strictly between the +reported last and `begin.key` is necessary: toggling it changes the number of stored keys +below `begin.key` by one while leaving the number below the reported head unchanged, so +the nonclamp count identity cannot hold on the toggled database. -/ +theorem necessary_negCross_trunc_gap_general {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + (hkgt : res.keys.getLast hne < k) (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort := RangeRead.compatible_sortedLT hcomp₀ hrev + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hhead_lt_k : res.keys.head hne < k := lt_of_le_of_lt hhead_le_last hkgt + have hhead_not_lt : ¬ ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) := by + rw [not_lt] + exact WithTop.coe_le_coe.mpr (le_of_lt hhead_lt_k) + refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ + intro hcompk + have hiden₀ := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp₀ hrev hmore hne hbegin + hcross hnonclamp_eq + rw [← countP_lt_coe] at hiden₀ + change cnt db₀ r.begin.key = cnt db₀ (res.keys.head hne) + reportedBelowBegin r res at hiden₀ + have hiden₁ := count_lt_begin_eq_of_negCross_trunc_nonclamp hcompk hrev hmore hne hbegin + hcross hnonclamp_eq + rw [← countP_lt_coe] at hiden₁ + change cnt (toggle db₀ k) r.begin.key = + cnt (toggle db₀ k) (res.keys.head hne) + reportedBelowBegin r res at hiden₁ + by_cases hkmem : k ∈ db₀.keys + · have htoggle : toggle db₀ k = db₀.erase k := toggle_eq_erase_of_mem hkmem + rw [htoggle] at hiden₁ + rw [cnt_erase_lt db₀ hkmem hkB, + cnt_erase_not_lt db₀ hkmem hhead_not_lt] at hiden₁ + have hpos : 1 ≤ cnt db₀ r.begin.key := by + rw [hiden₀] + have hq : 0 < beginSlack r := by + unfold beginSlack + rw [← Int.pos_iff_toNat_pos] + omega + omega + omega + · have htoggle : toggle db₀ k = db₀.insert k hkmem := toggle_eq_insert_of_not_mem hkmem + rw [htoggle] at hiden₁ + rw [cnt_insert_lt db₀ k hkmem hkB, + cnt_insert_not_lt db₀ k hkmem hhead_not_lt] at hiden₁ + omega + +/-- **Closed form for the forward crossed negative-begin truncated nonclamping +unbounded-end cell.** The minimal sound range is +`[head, max begin.key (keyAfter last))`. -/ +theorem minimal_closedForm_of_negCross_trunc_nonclamp_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonclampTop r res) : + Minimal r res (closedFormNegCrossTruncNonclampTop r res) := by + obtain ⟨hmore, hrev, hbegin, hendkey, hendoff, hcrossD, hnonclamp_eq⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hcrossD + exact hcrossD + let upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) + have hcr : closedFormNegCrossTruncNonclampTop r res = + ⟨((head : Key) : WithTop Key), upper⟩ := by + unfold closedFormNegCrossTruncNonclampTop + rw [hheadD, hlastD] + have hsound : Sound r res (closedFormNegCrossTruncNonclampTop r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_top hrev hmore hne hbegin + hendkey hendoff hcross hnonclamp_eq h hagree, + fun h => compatible_of_agree_negCross_trunc_nonclamp_top hrev hmore hne hbegin + hendkey hendoff hcross hnonclamp_eq h hagree.symm⟩ + rw [hcr] at hsound ⊢ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k hkbeg hkend + by_cases hkle : k ≤ last + · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) (WithTop.coe_le_coe.mp hkbeg) hkle + · have hkgt : res.keys.getLast hne < k := by + simpa [hlast] using lt_of_not_ge hkle + have hkltB : (k : WithTop Key) < r.begin.key := by + have hkmax : (k : WithTop Key) < + max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend + rcases lt_max_iff.mp hkmax with h | h + · exact h + · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) + exact absurd hklast hkle + exact necessary_negCross_trunc_gap hread' hrev hmore hne hbegin hendkey hendoff + hcross hnonclamp_eq hkgt hkltB + +/-- **A key below `end_.key` lies below the end resolution at any positive end +offset.** With offset at least one the end anchor is at or past the count of stored keys +below `end_.key`, so the resolution is at or past every key below `end_.key`. -/ +theorem lt_resolve_end_of_lt_end_key {db : DB} {r : RangeRead} + (hend : 1 ≤ r.end_.offset) {k : Key} (hk : (k : WithTop Key) < r.end_.key) : + (k : WithTop Key) < resolve db r.end_ := by + set c := db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) with hc + have hanc : r.end_.anchor db = (c : ℤ) + r.end_.offset - 1 := by + unfold Selector.anchor; rw [hc] + have hanchor_nonneg : 0 ≤ r.end_.anchor db := by + rw [hanc]; have : (0 : ℤ) ≤ c := Int.natCast_nonneg _; omega + have hcnt_le : db.keys.countP (fun x : Key => decide (x < keyAfter k)) ≤ c := by + rw [hc] + apply List.countP_mono_left + intro x _ hxd + rw [decide_eq_true_eq] at hxd ⊢ + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr ((lt_keyAfter_iff x k).mp hxd)) hk + have hanchor_ge : (db.keys.countP (fun x : Key => decide (x < keyAfter k)) : ℤ) ≤ + r.end_.anchor db := by + rw [hanc] + have hcle : (db.keys.countP (fun x : Key => decide (x < keyAfter k)) : ℤ) ≤ (c : ℤ) := by + exact_mod_cast hcnt_le + have hoff : (1 : ℤ) ≤ r.end_.offset := by exact_mod_cast hend + omega + rcases lt_or_ge (r.end_.anchor db).toNat db.keys.length with hlt | hge + · have hres := resolve_eq_getElem (db := db) (s := r.end_) hanchor_nonneg hlt + rw [hres] + have hcount_le_anchor : db.keys.countP (fun x : Key => decide (x < keyAfter k)) ≤ + (r.end_.anchor db).toNat := by + omega + have hle : keyAfter k ≤ db.keys[(r.end_.anchor db).toNat] := + le_getElem_of_countP_le db.sorted hcount_le_anchor hlt + exact WithTop.coe_lt_coe.mpr (lt_of_lt_of_le (keyAfter_self_lt k) hle) + · have htop := resolve_eq_top (db := db) (s := r.end_) hanchor_nonneg hge + rw [htop]; exact WithTop.coe_lt_top k + +/-- **Dropping the end bound at `end_.offset = 1` when every reported key lies below +`end_.key`.** At offset one the end selector resolves to the first stored key at or +above `end_.key`, so every stored key at or below the reported last lies below the +resolution; replacing `end_.key` by `⊤` therefore leaves compatibility unchanged. -/ +theorem compatible_iff_end_top_of_offset_one {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hend : r.end_.offset = 1) + (hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := by + set r' : RangeRead := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } with hr' + have hlast_lt : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := + hbelow _ (List.getLast_mem hne) + have hlt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + lt_resolve_end_of_end_one hend hlast_lt + have hend' : r'.end_.offset = 1 := by rw [hr']; exact hend + have htop : resolve db r'.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top (db := db) (s := r'.end_) + (by rw [hr']) (by rw [hend']) + have hlt' : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r'.end_ := by + rw [htop]; exact WithTop.coe_lt_top _ + have hrev' : r'.reverse = false := by rw [hr']; exact hrev + have hfil : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + (RangeRead.completeKeys db r').filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [completeKeys_filter_le_eq (db := db) (r := r) hrev hlt, + completeKeys_filter_le_eq (db := db) (r := r') hrev' hlt'] + rw [compatible_iff_truncated_keys (r := r) hrev hmore hne, + compatible_iff_truncated_keys (r := r') hrev' hmore hne, hfil] + constructor <;> rintro ⟨h, w⟩ <;> exact ⟨h, w⟩ + +/-- The forward, crossed, truncated, nonclamping negative-begin cell with end offset +one and every reported key below `end_.key`. -/ +abbrev negCrossTruncNonclampEndOne (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ r.end_.offset = 1 ∧ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res = beginSlack r + 1 + +/-- The closed form for the end-one cell, obtained by replacing the end key with `⊤`. -/ +noncomputable def closedFormNegCrossTruncNonclampEndOne (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossTruncNonclampTop + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res + +/-- **Closed form for the forward crossed negative-begin truncated nonclamping +end-offset-one cell.** Every reported key lies below `end_.key`, so the end selector +can be dropped to `⊤` and the unbounded-end rule applies. -/ +theorem minimal_closedForm_of_negCross_trunc_nonclamp_endOne {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonclampEndOne r res) : + Minimal r res (closedFormNegCrossTruncNonclampEndOne r res) := by + obtain ⟨hmore, hrev, hbegin, hend, hbelow, hcrossD, hnonclamp_eq⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hcompiff : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + fun db => compatible_iff_end_top_of_offset_one hrev hmore hne hend hbelow + have hcellTop : negCrossTruncNonclampTop + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + ⟨hmore, hrev, hbegin, rfl, by simpa using (le_of_eq hend.symm), hcrossD, hnonclamp_eq⟩ + have hread' : Readable { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + ⟨db, (hcompiff db).mp hcomp⟩ + have hmin := minimal_closedForm_of_negCross_trunc_nonclamp_top hread' hcellTop + unfold closedFormNegCrossTruncNonclampEndOne + exact (minimal_congr_of_compatible_iff (r := r) + (r' := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) + (res := res) (cr := closedFormNegCrossTruncNonclampTop + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res) hcompiff).mpr hmin + +/-- **Dropping the end bound when every reported key lies below `end_.key`.** -/ +theorem completeKeys_filter_le_eq_of_below_end {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hend : 1 ≤ r.end_.offset) + (hbelow : (last : WithTop Key) < r.end_.key) : + (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + (RangeRead.completeKeys db + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }).filter + (fun k : Key => decide (k ≤ last)) := by + have hlt : (last : WithTop Key) < resolve db r.end_ := + lt_resolve_end_of_lt_end_key hend hbelow + have htop : resolve db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }.end_ = + (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top (db := db) (s := _) rfl (by simpa using hend) + have hlt' : (last : WithTop Key) < + resolve db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }.end_ := by + rw [htop]; exact WithTop.coe_lt_top last + rw [completeKeys_filter_le_eq (db := db) (r := r) hrev hlt, + completeKeys_filter_le_eq (db := db) + (r := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) hrev hlt'] + +/-- **Dropping the end bound whenever every reported key is below `end_.key`.** -/ +theorem compatible_iff_end_top_of_below {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hend : 1 ≤ r.end_.offset) + (hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := by + have hlast_lt : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := + hbelow _ (List.getLast_mem hne) + have hfil := completeKeys_filter_le_eq_of_below_end (db := db) (r := r) + (last := res.keys.getLast hne) hrev hend hlast_lt + rw [compatible_iff_truncated_keys (r := r) hrev hmore hne, + compatible_iff_truncated_keys + (r := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) hrev hmore hne, hfil] + constructor <;> rintro ⟨h, w⟩ <;> exact ⟨h, w⟩ + +/-- The forward, crossed, truncated, nonclamping negative-begin cell with positive end +offset and every reported key below `end_.key`. -/ +abbrev negCrossTruncNonclampBelowEnd (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 1 ≤ r.end_.offset ∧ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res = beginSlack r + 1 + +/-- The closed form for the below-end cell, obtained by replacing the end key with `⊤`. -/ +noncomputable def closedFormNegCrossTruncNonclampBelowEnd (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossTruncNonclampTop + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res + +/-- **Closed form for the forward crossed negative-begin truncated nonclamping +below-end cell.** Every reported key lies below `end_.key`, so the end selector can be +dropped to `⊤` and the unbounded-end rule applies. -/ +theorem minimal_closedForm_of_negCross_trunc_nonclamp_below {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonclampBelowEnd r res) : + Minimal r res (closedFormNegCrossTruncNonclampBelowEnd r res) := by + obtain ⟨hmore, hrev, hbegin, hend, hbelow, hcrossD, hnonclamp_eq⟩ := hcell + obtain ⟨db, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hcompiff : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + fun db => compatible_iff_end_top_of_below hrev hmore hne hend hbelow + have hcellTop : negCrossTruncNonclampTop + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + ⟨hmore, hrev, hbegin, rfl, by simpa using hend, hcrossD, hnonclamp_eq⟩ + have hread' : Readable { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := + ⟨db, (hcompiff db).mp hcomp⟩ + have hmin := minimal_closedForm_of_negCross_trunc_nonclamp_top hread' hcellTop + unfold closedFormNegCrossTruncNonclampBelowEnd + exact (minimal_congr_of_compatible_iff (r := r) + (r' := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } }) + (res := res) (cr := closedFormNegCrossTruncNonclampTop + { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res) hcompiff).mpr hmin + +/-- **Compatibility transfers across agreement on the nonclamping truncated crossed +negative-begin range when the end key is at or above the reported head.** Agreement on +`[head, max begin.key (keyAfter last))` fixes the begin resolution through the nonclamp +count identity, and the end filter count fixes the end resolution above `last`. -/ +theorem compatible_of_agree_negCross_trunc_nonclamp_endGeHead {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hge : 1 ≤ r.end_.offset) + (hheadE : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncNonclampTop r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hcr : closedFormNegCrossTruncNonclampTop r res = ⟨((head : Key) : WithTop Key), upper⟩ := by + unfold closedFormNegCrossTruncNonclampTop + rw [hheadD, hlastD, hupper] + have hB_upper : r.begin.key ≤ upper := le_max_left _ _ + have hlast_upper : ((last : Key) : WithTop Key) < upper := + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampTop r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_upper : ((head : Key) : WithTop Key) < upper := + lt_of_lt_of_le hcross hB_upper + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + -- Count identity for `db1`, and the equal gap counts. + have hcount1 := count_lt_begin_eq_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross + hnonclamp_eq + have hcount1' : db1.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db1.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by + simpa [hhead] using hcount1 + have hsplit1 := countP_lt_eq_add_filter_withTop db1.keys db1.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hgaplen1 : (db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by + rw [hcount1'] at hsplit1 + omega + have hgap_eq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge', hlt⟩ + exact ⟨(hmk k hge' (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge', hlt⟩ + · rintro ⟨hk2, hge', hlt⟩ + exact ⟨(hmk k hge' (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge', hlt⟩ + have hgaplen2 : (db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by + rw [← hgap_eq, hgaplen1] + have hsplit2 := countP_lt_eq_add_filter_withTop db2.keys db2.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hcount2 : db2.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by + rw [hsplit2, hgaplen2] + have cB2_ge : beginSlack r + 1 ≤ db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hcount2, ← hnonclamp_eq]; omega + have hanchor2 : 0 ≤ r.begin.anchor db2 := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))) : ℤ) := by + exact_mod_cast cB2_ge + omega + have htoNat2 : (r.begin.anchor db2).toNat = db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - + (beginSlack r + 1) := by + have hz : r.begin.anchor db2 = ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ)) := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)]; push_cast; ring + have hz2 : ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ) = + ((db2.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)) - + (beginSlack r + 1) : ℕ) : ℤ) := by + rw [← Nat.cast_sub cB2_ge] + rw [hz, hz2, Int.toNat_natCast] + have hidx_eq2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k => decide (k < head)) := by + rw [htoNat2, hcount2, hnonclamp_eq] + omega + have hltlen2 : (r.begin.anchor db2).toNat < db2.keys.length := by + rw [hidx_eq2] + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_db2 + rw [countP_lt_eq_of_getElem db2.sorted hi hix] + exact hi + have hval2 : db2.keys[(r.begin.anchor db2).toNat]'hltlen2 = head := by + simpa [hidx_eq2] using getElem_countP_lt db2.sorted hhead_db2 + have hL2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := by + rw [resolve_eq_getElem (db := db2) (s := r.begin) hanchor2 hltlen2] + exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval2 + -- The end resolution is above `last` for both databases, by the end filter count. + have hheadE' : ((head : Key) : WithTop Key) ≤ r.end_.key := by rw [hhead]; exact hheadE + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hfiltereq : (db1.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + (db2.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _) + · exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk1, hEk⟩, hkle⟩ + exact ⟨⟨(hmk k (le_trans hheadE' hEk) + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mp hk1, hEk⟩, hkle⟩ + · rintro ⟨⟨hk2, hEk⟩, hkle⟩ + exact ⟨⟨(hmk k (le_trans hheadE' hEk) + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mpr hk2, hEk⟩, hkle⟩ + have hcnt1 : ((db1.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := + filter_length_le_offset_of_resolve_end_gt (db := db1) hge hlt1 + have hcnt2 : ((db2.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [← hfiltereq] + exact hcnt1 + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + resolve_end_gt_of_filter_length_le (db := db2) hge hcnt2 + have hfilter1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hfilter2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 + have hres1fil : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hfeq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge', hle⟩ + exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge', hle⟩ + · rintro ⟨hk2, hge', hle⟩ + exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge', hle⟩ + have hres2fil : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hfilter2, ← hfeq, ← hfilter1, hres1fil] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, by simpa [hlast] using hres2fil⟩ + +/-- The forward, crossed, truncated, nonclamping negative-begin cell with positive end +offset and the end key at or above the reported head. -/ +abbrev negCrossTruncNonclampEndGeHead (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) ≤ r.end_.key ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res = beginSlack r + 1 + +/-- **Closed form for the forward crossed negative-begin truncated nonclamping +end-at-or-above-head cell.** The minimal sound range is again +`[head, max begin.key (keyAfter last))`. -/ +theorem minimal_closedForm_of_negCross_trunc_nonclamp_endGeHead {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonclampEndGeHead r res) : + Minimal r res (closedFormNegCrossTruncNonclampTop r res) := by + obtain ⟨hmore, hrev, hbegin, hge, hheadED, hcrossD, hnonclamp_eq⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hcrossD + exact hcrossD + have hheadE : ((head : Key) : WithTop Key) ≤ r.end_.key := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hheadED + exact hheadED + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) + have hcr : closedFormNegCrossTruncNonclampTop r res = + ⟨((head : Key) : WithTop Key), upper⟩ := by + unfold closedFormNegCrossTruncNonclampTop + rw [hheadD, hlastD] + have hsound : Sound r res (closedFormNegCrossTruncNonclampTop r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_endGeHead hrev hmore hne hbegin + hge hheadE hcross hnonclamp_eq h hagree, + fun h => compatible_of_agree_negCross_trunc_nonclamp_endGeHead hrev hmore hne hbegin + hge hheadE hcross hnonclamp_eq h hagree.symm⟩ + rw [hcr] at hsound ⊢ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k hkbeg hkend + by_cases hkle : k ≤ last + · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) (WithTop.coe_le_coe.mp hkbeg) hkle + · have hkgt : res.keys.getLast hne < k := by + simpa [hlast] using lt_of_not_ge hkle + have hkltB : (k : WithTop Key) < r.begin.key := by + have hkmax : (k : WithTop Key) < + max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend + rcases lt_max_iff.mp hkmax with h | h + · exact h + · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) + exact absurd hklast hkle + exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross + hnonclamp_eq hkgt hkltB + +/-- **Begin resolution and reported window from agreement in the nonclamping crossed +negative-begin truncated cell.** Agreement on `[head, max begin.key (keyAfter last))` +forces the nonclamp count identity on the second database, hence the same begin +resolution `head`, and fixes the stored keys in `[head, last]`. -/ +theorem begin_eq_head_and_window_of_agree_negCross_trunc_nonclamp {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hmk : ∀ k : Key, + ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < + max r.begin.key (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) → + (k ∈ db1.keys ↔ k ∈ db2.keys)) : + resolve db2 r.begin = ((res.keys.head hne : Key) : WithTop Key) ∧ + db2.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + (k : Key) ≤ res.keys.getLast hne)) = res.keys := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hB_upper : r.begin.key ≤ upper := le_max_left _ _ + have hmk' : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + exact hmk k (by simpa [hhead] using hk1) (by simpa [hhead, hlast, hupper] using hk2) + have hnonclamp : beginSlack r < reportedBelowBegin r res := by rw [hnonclamp_eq]; omega + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross hnonclamp + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_upper : ((head : Key) : WithTop Key) < upper := + lt_of_lt_of_le hcross hB_upper + have hhead_db2 : head ∈ db2.keys := (hmk' head le_rfl hhead_upper).mp hhead_db1 + have hcount1 := count_lt_begin_eq_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross + hnonclamp_eq + have hcount1' : db1.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db1.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by + simpa [hhead] using hcount1 + have hsplit1 := countP_lt_eq_add_filter_withTop db1.keys db1.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hgaplen1 : (db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by + rw [hcount1'] at hsplit1 + omega + have hgap_eq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge', hlt⟩ + exact ⟨(hmk' k hge' (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge', hlt⟩ + · rintro ⟨hk2, hge', hlt⟩ + exact ⟨(hmk' k hge' (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge', hlt⟩ + have hgaplen2 : (db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = reportedBelowBegin r res := by + rw [← hgap_eq, hgaplen1] + have hsplit2 := countP_lt_eq_add_filter_withTop db2.keys db2.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hcount2 : db2.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db2.keys.countP (fun k : Key => decide (k < head)) + reportedBelowBegin r res := by + rw [hsplit2, hgaplen2] + have cB2_ge : beginSlack r + 1 ≤ db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hcount2, ← hnonclamp_eq]; omega + have hanchor2 : 0 ≤ r.begin.anchor db2 := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] + have : (beginSlack r : ℤ) + 1 ≤ ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))) : ℤ) := by + exact_mod_cast cB2_ge + omega + have htoNat2 : (r.begin.anchor db2).toNat = db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) - + (beginSlack r + 1) := by + have hz : r.begin.anchor db2 = ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ)) := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)]; push_cast; ring + have hz2 : ((db2.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) - + ((beginSlack r + 1 : ℕ) : ℤ) = + ((db2.keys.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)) - + (beginSlack r + 1) : ℕ) : ℤ) := by + rw [← Nat.cast_sub cB2_ge] + rw [hz, hz2, Int.toNat_natCast] + have hidx_eq2 : (r.begin.anchor db2).toNat = db2.keys.countP (fun k => decide (k < head)) := by + rw [htoNat2, hcount2, hnonclamp_eq] + omega + have hltlen2 : (r.begin.anchor db2).toNat < db2.keys.length := by + rw [hidx_eq2] + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_db2 + rw [countP_lt_eq_of_getElem db2.sorted hi hix] + exact hi + have hval2 : db2.keys[(r.begin.anchor db2).toNat]'hltlen2 = head := by + simpa [hidx_eq2] using getElem_countP_lt db2.sorted hhead_db2 + have hL2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := by + rw [resolve_eq_getElem (db := db2) (s := r.begin) hanchor2 hltlen2] + exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval2 + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hres1fil : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hfilter1 := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hpart1 : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + rw [← hfilter1]; exact hres1fil + have hpart2 : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + have heq : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db2.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk2, hHk, hklast⟩ + exact ⟨(hmk' k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) + (le_max_right _ _)))).mpr hk2, hHk, hklast⟩ + · rintro ⟨hk1, hHk, hklast⟩ + exact ⟨(hmk' k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) + (le_max_right _ _)))).mp hk1, hHk, hklast⟩ + rw [heq, hpart1] + exact ⟨hL2, hpart2⟩ + +/-- The closed form for the low-end (saturated gap) crossed negative-begin truncated +nonclamping cell: from `end.key` instead of the reported head. -/ +noncomputable def closedFormNegCrossTruncNonclampLowEnd (r : RangeRead) (res : Result) : + ConflictRange := + ⟨r.end_.key, + max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩ + +/-- **Compatibility transfers across agreement on the low-end crossed negative-begin +truncated nonclamping range when the gap absorbs the reported keys.** -/ +theorem compatible_of_agree_negCross_trunc_nonclamp_lowEndSmall {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hge : 2 ≤ r.end_.offset) + (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + (hsmall : intervalCard r.end_.key (((res.keys.head hne : Key)) : WithTop Key) + + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞)) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncNonclampTop r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcr : closedFormNegCrossTruncNonclampTop r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormNegCrossTruncNonclampTop + rw [hheadD, hlastD] + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := + lt_of_lt_of_le hcross (le_max_left _ _) + have hlast_upper : ((last : Key) : WithTop Key) < upper := + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampTop r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + obtain ⟨hL2, hpart2⟩ := begin_eq_head_and_window_of_agree_negCross_trunc_nonclamp + hrev hmore hne hbegin hcross hnonclamp_eq hc1 (fun k hk1 hk2 => hmk k hk1 (by simpa [hupper] using hk2)) + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead + have hsmall' : intervalCard r.end_.key ((head : Key) : WithTop Key) + + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + simpa [hhead] using hsmall + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + have hsplit := filter_end_le_split_between (db := db2) (E := r.end_.key) + (head := head) (last := last) (le_of_lt hEhead') hhead_le_last + have hbetween : (((between db2 r.end_.key ((head : Key) : WithTop Key)).length : ℕ∞)) ≤ + intervalCard r.end_.key ((head : Key) : WithTop Key) := + between_length_le_intervalCard (db := db2) (le_of_lt hEhead') + have hsum : ((((between db2 r.end_.key ((head : Key) : WithTop Key)).length + + res.keys.length : ℕ)) : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + rw [Nat.cast_add] + exact le_trans (add_le_add hbetween le_rfl) hsmall' + have hpart2' : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + simpa [hhead, hlast] using hpart2 + have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + have hlen : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = + (between db2 r.end_.key ((head : Key) : WithTop Key)).length + res.keys.length := by + rw [hsplit, hpart2', List.length_append] + rw [hlen] + exact_mod_cast hsum + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2 + have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, by rw [hck2, hpart2']⟩ + +/-- **Compatibility transfers across agreement on the saturated low-end crossed +negative-begin truncated nonclamping range.** -/ +theorem compatible_of_agree_negCross_trunc_nonclamp_lowEndBig {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hge : 2 ≤ r.end_.offset) + (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncNonclampLowEnd r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcr : closedFormNegCrossTruncNonclampLowEnd r res = + ⟨r.end_.key, max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormNegCrossTruncNonclampLowEnd + rw [hlastD] + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := + lt_of_lt_of_le hcross (le_max_left _ _) + have hlast_upper : ((last : Key) : WithTop Key) < upper := + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead + have hE_upper : r.end_.key < upper := lt_trans hEhead' hhead_upper + have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampLowEnd r res) db1 db2).mp hagree + have hmkE : ∀ k : Key, r.end_.key ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + exact hmkE k (le_trans (le_of_lt hEhead') hk1) hk2 + obtain ⟨hL2, hpart2⟩ := begin_eq_head_and_window_of_agree_negCross_trunc_nonclamp + hrev hmore hne hbegin hcross hnonclamp_eq hc1 (fun k hk1 hk2 => hmk k hk1 (by simpa [hupper] using hk2)) + have hpart2' : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + simpa [hhead, hlast] using hpart2 + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hcnt1 : ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := + filter_length_le_offset_of_resolve_end_gt (db := db1) (by omega) hlt1 + have hcnt_eq : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))) = + ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _) + · exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk2, hEk⟩, hklast⟩ + exact ⟨⟨(hmkE k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2, + hEk⟩, hklast⟩ + · rintro ⟨⟨hk1, hEk⟩, hklast⟩ + exact ⟨⟨(hmkE k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1, + hEk⟩, hklast⟩ + have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [hcnt_eq]; exact hcnt1 + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2 + have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hL2 hlt2 + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, by rw [hck2, hpart2']⟩ + +/-- **Gap keys below the reported head are necessary in the saturated low-end cell.** +A key `k` with `end.key ≤ k < head` is necessary: building a witness with `n` gap keys in +`[end.key, head)` excluding `k` puts the begin anchor exactly at the reported head and +saturates the end offset. Toggling `k` on then overflows the end offset. -/ +theorem necessary_negCross_trunc_between_end_head {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp_eq : reportedBelowBegin r res = beginSlack r + 1) + (hge : 2 ≤ r.end_.offset) + (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞)) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set e1 : ℕ := (r.end_.offset - 1).toNat with he1 + set rl : ℕ := res.keys.length with hrl + set n : ℕ := e1 - rl with hn + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by rw [hhead]; exact hcross + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead + have hkH' : (k : WithTop Key) < ((head : Key) : WithTop Key) := by rw [hhead]; exact hkH + have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hres_pair : res.keys.Pairwise (· ≤ ·) := hres_sorted.pairwise.imp le_of_lt + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + have := hres_pair.rel_head hx + simpa [hhead] using this + have hle_last : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + have := hres_pair.rel_getLast hx + simpa [hlast] using this + have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hhead_le_last : head ≤ last := hhead_le last hlast_mem + -- The reported length is bounded by the end offset. + have hlast_ck0 : last ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hlast_mem + have hlt0 : ((last : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck0).2.2 + have hcnt0 : ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ last))).length ≤ e1 := by + simpa [he1] using filter_length_le_offset_of_resolve_end_gt (db := db₀) (by omega) hlt0 + have hres_sub : res.keys ⊆ + ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ last))) := by + intro x hx + have hxdb : x ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hx + have hxE : r.end_.key ≤ (x : WithTop Key) := + le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)) + have hxl : x ≤ last := hle_last x hx + rw [List.mem_filter, List.mem_filter] + exact ⟨⟨hxdb, by simpa using hxE⟩, by simpa using hxl⟩ + have hrle : rl ≤ e1 := by + have := hres_sorted.nodup.length_le_of_subset hres_sub + exact le_trans (by simpa [hrl] using this) hcnt0 + have hbig' : (e1 : ℕ∞) < intervalCard r.end_.key ((head : Key) : WithTop Key) + + (rl : ℕ∞) := by + simpa [he1, hrl, hhead] using hbig + have hcard : ((n + 1 : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((head : Key) : WithTop Key) := by + by_cases htop : intervalCard r.end_.key ((head : Key) : WithTop Key) = (⊤ : ℕ∞) + · rw [htop]; exact le_top + · obtain ⟨G, hG⟩ := ENat.ne_top_iff_exists.mp htop + have hlt : e1 < G + rl := by + have hb := hbig' + rw [← hG] at hb + rw [← ENat.coe_add] at hb + exact ENat.coe_lt_coe.mp hb + have hGle : n + 1 ≤ G := by + rw [hn]; omega + rw [← hG] + exact ENat.coe_le_coe.mpr hGle + obtain ⟨dbK, hKrange, hkK, hKlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := ((head : Key) : WithTop Key)) + (k := k) (m := n) hcard + have hK_ge : ∀ x ∈ dbK.keys, r.end_.key ≤ (x : WithTop Key) := + fun x hx => (hKrange x hx).1 + have hK_lt : ∀ x ∈ dbK.keys, x < head := + fun x hx => WithTop.coe_lt_coe.mp (hKrange x hx).2 + -- The witness database. + have hsort_append : (dbK.keys ++ res.keys).SortedLT := by + refine List.sortedLT_append.mpr ⟨dbK.sorted, hres_sorted, ?_⟩ + intro a ha b hb + exact lt_of_lt_of_le (hK_lt a ha) (hhead_le b hb) + let dbw : DB := ⟨dbK.keys ++ res.keys, hsort_append⟩ + have hdbw : dbw.keys = dbK.keys ++ res.keys := rfl + have hk_notmem : k ∉ dbw.keys := by + rw [hdbw, List.mem_append] + rintro (hkK' | hkres) + · exact hkK hkK' + · exact absurd (hhead_le k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkH')) + have hhead_dbw : head ∈ dbw.keys := by + rw [hdbw]; exact List.mem_append.mpr (Or.inr hhead_mem) + -- The begin selector resolves onto the reported head because the anchor is `n`. + have hcntB_dbw : cnt dbw r.begin.key = n + reportedBelowBegin r res := by + unfold cnt + rw [hdbw, List.countP_append] + have hKcount : dbK.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + dbK.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_trans (WithTop.coe_lt_coe.mpr (hK_lt x hx)) hcross' + rw [hKcount, hKlen] + rfl + have hdbw_len : dbw.keys.length = n + rl := by + rw [hdbw, List.length_append, hKlen, hrl] + have hanchorB : r.begin.anchor dbw = (n : ℤ) := by + have hcountP : dbw.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + n + reportedBelowBegin r res := by + simpa only [cnt] using hcntB_dbw + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountP, hnonclamp_eq] + push_cast; ring + have hanchorB_nonneg : 0 ≤ r.begin.anchor dbw := by + rw [hanchorB]; exact Int.natCast_nonneg n + have hltlenB : (r.begin.anchor dbw).toNat < dbw.keys.length := by + rw [hanchorB, Int.toNat_natCast, hdbw_len] + have hpos : 0 < rl := by rw [hrl]; exact List.length_pos_of_ne_nil hne + omega + have hltlenB_n : n < dbw.keys.length := by + rw [hanchorB, Int.toNat_natCast] at hltlenB + exact hltlenB + have hres_eq_head : res.keys = head :: res.keys.tail := by + rw [hhead]; exact (List.cons_head_tail hne).symm + have hsplitw : dbw.keys = dbK.keys ++ head :: res.keys.tail := by + rw [hdbw, hres_eq_head] + rfl + have hval_n : dbw.keys[n]'hltlenB_n = head := by + have := List.getElem_of_append (l := dbw.keys) (l₁ := dbK.keys) (a := head) + (l₂ := res.keys.tail) hsplitw hKlen + simpa using this + have hidx : (r.begin.anchor dbw).toNat = n := by + rw [hanchorB, Int.toNat_natCast] + have hresb : resolve dbw r.begin = ((head : Key) : WithTop Key) := by + rw [resolve_eq_getElem (db := dbw) (s := r.begin) hanchorB_nonneg hltlenB] + simp only [hidx] + exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval_n + have hmem_all_E : ∀ x ∈ dbw.keys, r.end_.key ≤ (x : WithTop Key) := by + intro x hx + rw [hdbw, List.mem_append] at hx + rcases hx with hx | hx + · exact hK_ge x hx + · exact le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)) + have hcntE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hmem_all_E x hx) + have hdbw_len_e1 : dbw.keys.length = e1 := by + rw [hdbw_len, hn] + exact Nat.sub_add_cancel hrle + have hanchorE : (r.end_.anchor dbw).toNat = e1 := by + rw [anchor_toNat_eq (db := dbw) (s := r.end_) (by omega), hcntE, ← he1] + omega + have hrestop : resolve dbw r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top + · exact anchor_nonneg_of_one_le (db := dbw) (s := r.end_) (by omega) + · rw [hanchorE, hdbw_len_e1] + have hck : RangeRead.completeKeys dbw r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hresb, hrestop] + have hpred : (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < (⊤ : WithTop Key))) = + (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) := by + funext x; simp + rw [hpred, hdbw, List.filter_append] + have hleft : dbK.keys.filter + (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_le.mpr (WithTop.coe_lt_coe.mpr (hK_lt x hx)) + have hright : res.keys.filter + (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = res.keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr (hhead_le x hx) + rw [hleft, hright, List.nil_append] + have hfilter_le : res.keys.filter (fun x : Key => decide (x ≤ last)) = res.keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact hle_last x hx + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hcomp₀.1, by rw [hck, hfilter_le]⟩ + -- Toggling the target on overflows the end offset. + have hdb' : toggle dbw k = dbw.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem + have hkeys_toggle : (toggle dbw k).keys = dbw.keys.orderedInsert (· ≤ ·) k := by + rw [hdb', DB.insert_keys] + have hlen_toggle : (toggle dbw k).keys.length = e1 + 1 := by + rw [hkeys_toggle, List.orderedInsert_length, hdbw_len_e1] + have hmem_all : ∀ x ∈ (toggle dbw k).keys, + r.end_.key ≤ (x : WithTop Key) ∧ x ≤ last := by + intro x hx + rw [hkeys_toggle, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact ⟨hkE, le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkH')) hhead_le_last⟩ + · rw [hdbw, List.mem_append] at hx + rcases hx with hx | hx + · exact ⟨hK_ge x hx, le_trans (le_of_lt (hK_lt x hx)) hhead_le_last⟩ + · exact ⟨le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)), + hle_last x hx⟩ + have hcount_toggle : + ((toggle dbw k).keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ last)) = (toggle dbw k).keys := by + have h1 : (toggle dbw k).keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) = (toggle dbw k).keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact (hmem_all x hx).1 + rw [h1] + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact (hmem_all x hx).2 + have hnot : ¬ RangeRead.Compatible (toggle dbw k) r res := by + intro hc + have hlast_ck : last ∈ RangeRead.completeKeys (toggle dbw k) r := + (RangeRead.compatible_keys_prefix hc).subset hlast_mem + have hlt : ((last : Key) : WithTop Key) < resolve (toggle dbw k) r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hcnt := filter_length_le_offset_of_resolve_end_gt (db := toggle dbw k) (by omega) hlt + rw [hcount_toggle, hlen_toggle, ← he1] at hcnt + omega + exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩ + +/-- The forward, crossed, truncated, nonclamping negative-begin low-end cell with +positive end offset and the end key strictly below the reported head, in the branch +where the gap to the head absorbs the reported keys. -/ +abbrev negCrossTruncNonclampLowEndSmall (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 2 ≤ r.end_.offset ∧ + r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res = beginSlack r + 1 ∧ + intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) + + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) + +/-- The forward, crossed, truncated, nonclamping negative-begin low-end cell with +positive end offset and the end key strictly below the reported head, in the saturated +branch. -/ +abbrev negCrossTruncNonclampLowEndBig (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ 2 ≤ r.end_.offset ∧ + r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + reportedBelowBegin r res = beginSlack r + 1 ∧ + (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < + intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) + + (res.keys.length : ℕ∞) + +/-- **Closed form for the low-end absorbing branch.** The minimal range is +`[head, max begin.key (keyAfter last))`. -/ +theorem minimal_closedForm_of_negCross_trunc_nonclamp_lowEndSmall {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonclampLowEndSmall r res) : + Minimal r res (closedFormNegCrossTruncNonclampTop r res) := by + obtain ⟨hmore, hrev, hbegin, hge, hEheadD, hcrossD, hnonclamp_eq, hsmallD⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hcrossD + exact hcrossD + have hEhead : r.end_.key < ((head : Key) : WithTop Key) := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hEheadD + exact hEheadD + have hsmall : intervalCard r.end_.key ((head : Key) : WithTop Key) + + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hsmallD + exact hsmallD + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) + have hcr : closedFormNegCrossTruncNonclampTop r res = + ⟨((head : Key) : WithTop Key), upper⟩ := by + unfold closedFormNegCrossTruncNonclampTop + rw [hheadD, hlastD] + have hsound : Sound r res (closedFormNegCrossTruncNonclampTop r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndSmall hrev hmore hne hbegin + hge hEhead hcross hnonclamp_eq hsmall h hagree, + fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndSmall hrev hmore hne hbegin + hge hEhead hcross hnonclamp_eq hsmall h hagree.symm⟩ + rw [hcr] at hsound ⊢ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k hkbeg hkend + by_cases hkle : k ≤ last + · exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne) + (by rw [hlast]; exact List.getLast_mem hne) (WithTop.coe_le_coe.mp hkbeg) hkle + · have hkgt : res.keys.getLast hne < k := by + simpa [hlast] using lt_of_not_ge hkle + have hkltB : (k : WithTop Key) < r.begin.key := by + have hkmax : (k : WithTop Key) < + max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend + rcases lt_max_iff.mp hkmax with h | h + · exact h + · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) + exact absurd hklast hkle + exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross + hnonclamp_eq hkgt hkltB + +/-- **Closed form for the saturated low-end branch.** The minimal range is +`[end.key, max begin.key (keyAfter last))`. -/ +theorem minimal_closedForm_of_negCross_trunc_nonclamp_lowEndBig {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonclampLowEndBig r res) : + Minimal r res (closedFormNegCrossTruncNonclampLowEnd r res) := by + obtain ⟨hmore, hrev, hbegin, hge, hEheadD, hcrossD, hnonclamp_eq, hbigD⟩ := hcell + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hcrossD + exact hcrossD + have hEhead : r.end_.key < ((head : Key) : WithTop Key) := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hEheadD + exact hEheadD + have hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < + intervalCard r.end_.key ((head : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by + have hle : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hle, hheadD] at hbigD + exact hbigD + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) + have hcr : closedFormNegCrossTruncNonclampLowEnd r res = + ⟨r.end_.key, upper⟩ := by + unfold closedFormNegCrossTruncNonclampLowEnd + rw [hlastD] + have hsound : Sound r res (closedFormNegCrossTruncNonclampLowEnd r res) := by + intro db1 db2 hagree + exact ⟨fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndBig hrev hmore hne hbegin + hge hEhead hcross hnonclamp_eq h hagree, + fun h => compatible_of_agree_negCross_trunc_nonclamp_lowEndBig hrev hmore hne hbegin + hge hEhead hcross hnonclamp_eq h hagree.symm⟩ + rw [hcr] at hsound ⊢ + refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩ + intro k hkbeg hkend + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · by_cases hkhead : k < head + · exact necessary_negCross_trunc_between_end_head hread' hrev hmore hne hbegin hcross + hnonclamp_eq hge hEhead hbig hkbeg (WithTop.coe_lt_coe.mpr hkhead) + · exact Necessary.of_between hhead_nec hlast_nec (le_of_not_gt hkhead) hkle + · have hkgt : res.keys.getLast hne < k := by + simpa [hlast] using lt_of_not_ge hkle + have hkltB : (k : WithTop Key) < r.begin.key := by + have hkmax : (k : WithTop Key) < + max r.begin.key (((keyAfter last : Key)) : WithTop Key) := hkend + rcases lt_max_iff.mp hkmax with h | h + · exact h + · have hklast : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h) + exact absurd hklast hkle + exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross + hnonclamp_eq hkgt hkltB + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncNonclampNonpos.lean b/FormalConflictRange/NegCrossTruncNonclampNonpos.lean new file mode 100644 index 0000000..6ebc8b3 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncNonclampNonpos.lean @@ -0,0 +1,930 @@ +/- +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.NegCrossTruncNonclamp + +/-! +# Crossed negative-begin truncated nonclamping nonpositive-end cell + +This module closes the forward, crossed, truncated, nonclamping cell at a negative +begin offset with a nonpositive end offset. In this cell the begin selector resolves +onto the reported head, there are no *unreported* stored keys between the reported last +and `begin.key`, and the minimal sound conflict range is +`[head, max begin.key end.key)`. When `end.key < begin.key` no compatible database +exists, so the maximal branch reduces to `[head, end.key)`. + +The proof adapts the begin-zero nonpositive-end argument of `ZeroCrossTrunc.lean`. The +begin-zero fact "no stored key lies between the reported head and `begin.key`" is +replaced by the count identity +`count_lt_begin_eq_of_negCross_trunc_nonclamp`, which says the stored keys in that gap +are exactly the reported ones, and by an anchor computation locating the begin +resolution at the reported head. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- In the compatible nonclamping crossed truncated negative-begin cell, the stored +keys in `[head, begin.key)` are exactly the reported keys there. Equivalently, no +unreported stored key lies above the reported last and below `begin.key`. -/ +theorem filter_between_head_begin_eq_of_negCross_trunc_nonclamp + {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) : + db.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) = + res.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := by + set head : Key := res.keys.head hne with hhead + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross + -- The count of stored keys below `begin.key` splits at the head. + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross') + have hsplit' : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.countP (fun k : Key => decide (k < head)) + + db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hsplit] + rw [← List.countP_eq_length_filter] + -- The count identity from the nonclamping cell. + have hcount := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin + hcross' hnonclamp + rw [← hhead] at hcount + -- The reported count in the gap. + 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 + -- Equal counts and a sublist give equal filters. + have hlen_eq : (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = + (res.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := by + have h1 : (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = + db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [List.countP_eq_length_filter] + have h2 : (res.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = + res.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [List.countP_eq_length_filter] + rw [h1, h2, hres_count] + omega + have hsub : List.Sublist (res.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))) + (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))) := + List.Sublist.filter _ (result_sublist_db_of_compatible hcomp hrev) + exact (hsub.eq_of_length hlen_eq.symm).symm + +/-- In the compatible nonclamping crossed truncated negative-begin cell, no stored key +lies strictly above the reported last and strictly below `begin.key`. -/ +theorem not_stored_between_last_begin_of_negCross_trunc_nonclamp + {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) + {x : Key} (hx : x ∈ db.keys) (hxlast : res.keys.getLast hne < x) + (hxB : (x : WithTop Key) < r.begin.key) : False := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt + |>.rel_getLast (List.head_mem hne) + have hxhead : ((head : Key) : WithTop Key) ≤ (x : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hhead_le_last hxlast)) + have heq := filter_between_head_begin_eq_of_negCross_trunc_nonclamp hcomp hrev hmore + hne hbegin hcross hnonclamp + have hxdb : x ∈ db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := by + rw [List.mem_filter] + exact ⟨hx, by rw [decide_eq_true_eq]; exact ⟨hxhead, hxB⟩⟩ + rw [heq] at hxdb + have hxres : x ∈ res.keys := (List.mem_filter.mp hxdb).1 + have hxle : x ≤ last := + ((RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt).rel_getLast hxres + exact absurd hxle (not_le.mpr hxlast) + +/-- In the compatible nonclamping crossed truncated negative-begin cell with a +nonpositive end offset, the end key lies at or above `begin.key`. Otherwise the +complete window `[head, resolve end)` would hold at most `beginSlack` keys, while the +reported prefix has at least `beginSlack + 1`. -/ +theorem begin_le_end_of_negCross_trunc_nonclamp_nonpos + {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) + (hend : r.end_.offset ≤ 0) : + r.begin.key ≤ r.end_.key := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + by_contra hcon + have hEB : r.end_.key < r.begin.key := lt_of_not_ge hcon + -- The reported prefix is nonempty, so the complete window is nonempty. + have hck_ne : RangeRead.completeKeys db r ≠ [] := by + intro hnil + have : res.keys = [] := by + have hpre' := hpre + rw [hnil] at hpre' + exact List.eq_nil_of_prefix_nil hpre' + exact hne this + obtain ⟨k, hkck⟩ := List.exists_mem_of_ne_nil _ hck_ne + obtain ⟨-, hkb, hke⟩ := RangeRead.mem_completeKeys.mp hkck + have hlt_res : resolve db r.begin < resolve db r.end_ := lt_of_le_of_lt hkb hke + -- The begin resolution is the reported head. + have hrb : resolve db r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin hcross' + (by omega) + have hcnt_head : cnt db (resolve db r.begin) = + db.keys.countP (fun k : Key => decide (k < head)) := by + rw [hrb] + simp only [cnt, WithTop.coe_lt_coe] + -- The end resolution is a genuine stored key; name it and its rank. + have hne_top : resolve db r.end_ ≠ (⊤ : WithTop Key) := + resolve_ne_top_of_offset_nonpos hend + obtain ⟨q, hq⟩ := WithTop.ne_top_iff_exists.mp hne_top + have hanchor_nonneg : 0 ≤ r.end_.anchor db := by + by_contra hneg + have hnil : resolve db r.end_ = (([] : Key) : WithTop Key) := + resolve_eq_nil (lt_of_not_ge hneg) + rw [hnil] at hq + have hqnil : q = [] := WithTop.coe_inj.mp hq + rw [hrb, hnil] at hlt_res + exact absurd hlt_res (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key head))) + have hltlen : (r.end_.anchor db).toNat < db.keys.length := by + by_contra hle + exact hne_top (resolve_eq_top hanchor_nonneg (le_of_not_gt hle)) + have hresget : resolve db r.end_ = ((db.keys[(r.end_.anchor db).toNat] : Key) : WithTop Key) := + resolve_eq_getElem hanchor_nonneg hltlen + have hqval : db.keys[(r.end_.anchor db).toNat] = q := by + apply WithTop.coe_inj.mp + rw [← hresget, hq] + have hcnt_q : cnt db (resolve db r.end_) = (r.end_.anchor db).toNat := by + rw [hresget] + simp only [cnt, WithTop.coe_lt_coe, hqval] + exact countP_lt_eq_of_getElem db.sorted hltlen hqval + -- Bound the complete window length by the begin slack. + have hlen : (RangeRead.completeKeys db r).length = + cnt db (resolve db r.end_) - cnt db (resolve db r.begin) := by + have hb : RangeRead.completeKeys db r = + between db (resolve db r.begin) (resolve db r.end_) := by + unfold RangeRead.completeKeys RangeRead.inRange between + rw [ite_eq_right (by simp [hrev])] + rw [hb] + exact (cnt_sub_eq_between_length (le_of_lt hlt_res)).symm + -- Count below `begin.key` and below `end.key`. + set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) + with hcB + set cE := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) + with hcE + set cH := db.keys.countP (fun k : Key => decide (k < head)) with hcH + have hcount : cB = cH + reportedBelowBegin r res := by + have h := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin hcross' + hnonclamp + rw [← hhead] at h + exact h + have hcE_le : cE ≤ cB := by + apply countP_le_countP_of_imp + intro k _ + rw [decide_eq_true_eq, decide_eq_true_eq] + exact fun h => lt_trans h hEB + -- The end anchor in terms of the end count. + cases hE : r.end_.key with + | top => + rw [hE] at hEB + exact absurd hEB (not_lt.mpr (le_top)) + | coe e => + have hcE' : cE = db.keys.countP (fun k : Key => decide (k < e)) := by + rw [hcE, hE, countP_lt_coe] + have hance : r.end_.anchor db = (cE : ℤ) - ((-r.end_.offset).toNat : ℤ) - 1 := by + rw [anchor_of_coe (s := r.end_) hE, ← hcE'] + have hoff : ((-r.end_.offset).toNat : ℤ) = -r.end_.offset := + Int.toNat_of_nonneg (by omega) + rw [hoff] + ring + -- From `cE ≤ cB = cH + (beginSlack + 1)` and the end anchor bound. + have hreport : reportedBelowBegin r res = beginSlack r + 1 := hnonclamp + have hqle : (r.end_.anchor db).toNat ≤ cE - (-r.end_.offset).toNat - 1 := by + rw [hance] + have : 0 ≤ (cE : ℤ) - ((-r.end_.offset).toNat : ℤ) - 1 := by + have := hanchor_nonneg; rw [hance] at this; exact this + omega + have hlenle : (RangeRead.completeKeys db r).length ≤ beginSlack r := by + rw [hlen, hcnt_q, hcnt_head] + have hcE_bound : cE ≤ cH + beginSlack r + 1 := by + have h1 : cE ≤ cH + reportedBelowBegin r res := le_trans hcE_le (le_of_eq hcount) + rw [hreport] at h1 + omega + have hnat : cE - (-r.end_.offset).toNat - 1 ≤ cH + beginSlack r := by omega + omega + have ht_ge : beginSlack r + 1 ≤ (RangeRead.completeKeys db r).length := by + have := hpre.length_le + have hrep : beginSlack r + 1 ≤ res.keys.length := by + rw [← hreport] + unfold reportedBelowBegin + exact List.countP_le_length + omega + omega + +/-- Given agreement on the gap `[head, begin.key)`, the begin selector of a compatible +nonclamping database resolves onto the reported head in every database that also stores +the head. Both counts below `begin.key` split at the head, and the gap length is exactly +`beginSlack + 1`, so the anchor is the rank of the head. -/ +theorem resolve_begin_eq_head_of_agree_negCross_trunc_nonclamp_nonpos + {db₁ db₂ : DB} {r : RangeRead} {res : Result} + (hcomp₁ : RangeRead.Compatible db₁ r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) + (hagree : db₁.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) = + db₂.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key))) + (hhead_mem₂ : res.keys.head hne ∈ db₂.keys) : + resolve db₂ r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + set head : Key := res.keys.head hne with hhead + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := hcross + -- Split the stored keys below `begin.key` at the head in both databases. + have hsplit₁ := countP_lt_eq_add_filter_withTop db₁.keys db₁.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross') + have hsplit₂ := countP_lt_eq_add_filter_withTop db₂.keys db₂.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross') + have hsplit₁' : db₁.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db₁.keys.countP (fun k : Key => decide (k < head)) + + db₁.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hsplit₁, ← List.countP_eq_length_filter] + have hsplit₂' : db₂.keys.countP (fun k : Key => decide + (((k : Key) : WithTop Key) < r.begin.key)) = + db₂.keys.countP (fun k : Key => decide (k < head)) + + db₂.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hsplit₂, ← List.countP_eq_length_filter] + have hcount₁ := count_lt_begin_eq_of_negCross_trunc_nonclamp hcomp₁ hrev hmore hne hbegin + hcross' hnonclamp + rw [← hhead] at hcount₁ + -- The gap length is `beginSlack + 1`. + have hgap₁ : db₁.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by + omega + -- Agreement preserves the gap count. + have hgap₂ : db₂.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by + rw [List.countP_eq_length_filter, ← hagree, ← List.countP_eq_length_filter] + exact hgap₁ + -- The anchor is the rank of the head. + have hanchor₂ : r.begin.anchor db₂ = + (db₂.keys.countP (fun k : Key => decide (k < head)) : ℤ) := by + rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hsplit₂', hgap₂] + push_cast + ring + have hlt : db₂.keys.countP (fun k : Key => decide (k < head)) < db₂.keys.length := by + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_mem₂ + rw [countP_lt_eq_of_getElem db₂.sorted hi hix] + exact hi + have hnat : (r.begin.anchor db₂).toNat = + db₂.keys.countP (fun k : Key => decide (k < head)) := by + rw [hanchor₂, Int.toNat_natCast] + have h0 : 0 ≤ r.begin.anchor db₂ := by + rw [hanchor₂]; exact Int.natCast_nonneg _ + have hlt' : (r.begin.anchor db₂).toNat < db₂.keys.length := by + rw [hnat]; exact hlt + have hget : db₂.keys[(r.begin.anchor db₂).toNat] = head := by + rw [getElem_congr_idx (h' := hnat)] + exact getElem_countP_lt db₂.sorted hhead_mem₂ + rw [resolve_eq_getElem h0 hlt'] + exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hget + +/-- The closed form for the crossed negative-begin truncated nonclamping cell with a +nonpositive end offset: the reported head up to the larger of `begin.key` and +`end_.key`. -/ +noncomputable def closedFormNegCrossTruncNonclampNonpos (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), + max r.begin.key r.end_.key⟩ + +/-- **Compatibility transfers across agreement on the nonpositive-end crossed +truncated negative-begin nonclamping range.** -/ +theorem compatible_of_agree_negCross_trunc_nonclamp_nonpos + {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) + (hend : r.end_.offset ≤ 0) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormNegCrossTruncNonclampNonpos r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_negCross_trunc_nonclamp hc1 hrev hmore hne hbegin hcross + (by omega) + have hcr : closedFormNegCrossTruncNonclampNonpos r res = + ⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by + unfold closedFormNegCrossTruncNonclampNonpos + rw [hheadD] + set upper : WithTop Key := max r.begin.key r.end_.key with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + have hlast_upper : ((last : Key) : WithTop Key) < upper := by + have hle := resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend + exact lt_of_lt_of_le (lt_of_lt_of_le hlt1 hle) (by rw [hupper]; exact le_max_right _ _) + have hmem := (agreeOn_iff (closedFormNegCrossTruncNonclampNonpos r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + -- Agreement on the gap `[head, begin.key)` forces the second begin resolution. + have hB_upper : r.begin.key ≤ upper := by rw [hupper]; exact le_max_left _ _ + have hgap : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge, hlt⟩ + · rintro ⟨hk2, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge, hlt⟩ + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_agree_negCross_trunc_nonclamp_nonpos hc1 hrev hmore hne hbegin + hcross hnonclamp hgap hhead_db2 + -- The end resolution transfers through the agreement filter below `end_.key`. + have hne_top : resolve db1 r.end_ ≠ (⊤ : WithTop Key) := + resolve_ne_top_of_offset_nonpos hend + obtain ⟨q, hq⟩ := WithTop.ne_top_iff_exists.mp hne_top + have hlast_q : last < q := by + have h := hlt1 + rw [← hq] at h + exact WithTop.coe_lt_coe.mp h + have hqne : q ≠ ([] : Key) := by + intro hnil + have h : last < ([] : Key) := by rwa [hnil] at hlast_q + exact absurd h (List.not_lt_nil last) + have hq_le_E : ((q : Key) : WithTop Key) ≤ r.end_.key := by + rw [hq] + exact resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend + have hhead_q : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hhead_le_last hlast_q)) + have hEnd_upper : r.end_.key ≤ upper := by + rw [hupper]; exact le_max_right _ _ + have hagreeEnd : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mp hk1, hge, hlt⟩ + · rintro ⟨hk2, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mpr hk2, hge, hlt⟩ + have hqget := resolve_filter_reverse_eq_some_of_ne_nil (db := db1) (s := r.end_) hend hqne hq.symm + have hresEnd2 : resolve db2 r.end_ = ((q : Key) : WithTop Key) := + resolve_eq_of_agree_between (db₁ := db1) (db₂ := db2) (s := r.end_) hend + (E := r.end_.key) rfl (le_trans hhead_q hq_le_E) hagreeEnd hqget hhead_q + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by + rw [hresEnd2] + exact WithTop.coe_lt_coe.mpr hlast_q + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hck1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hck2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + have heq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge, hle⟩ + · rintro ⟨hk2, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge, hle⟩ + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck2, ← heq, ← hck1, hres1len] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- When the database is the reported keys followed by fillers at or above +`begin.key`, the negative begin anchor lands on the reported head. -/ +theorem resolve_begin_eq_head_of_append_ge_begin {db : DB} {r : RangeRead} {res : Result} + {F : List Key} (hdb : db.keys = res.keys ++ F) + (hF : ∀ x ∈ F, r.begin.key ≤ (x : WithTop Key)) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + have hFzero : F.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hF x hx) + have hcnt : db.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hdb, List.countP_append, hFzero, Nat.add_zero] + rfl + have hanchor : r.begin.anchor db = 0 := by + rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt, hnonclamp] + push_cast + ring + have htoNat : (r.begin.anchor db).toNat = 0 := by rw [hanchor]; rfl + have hpos : 0 < db.keys.length := by + rw [hdb, List.length_append] + exact Nat.lt_of_lt_of_le (List.length_pos_of_ne_nil hne) (Nat.le_add_right _ _) + have hlt : (r.begin.anchor db).toNat < db.keys.length := by rw [htoNat]; exact hpos + have hopt0 : db.keys[0]? = some (res.keys.head hne) := by + rw [hdb, List.getElem?_append] + rw [if_pos (List.length_pos_of_ne_nil hne)] + rw [← List.head?_eq_getElem?] + exact List.head?_eq_some_head hne + have hopt : db.keys[(r.begin.anchor db).toNat]? = some (res.keys.head hne) := by + rw [htoNat] + exact hopt0 + have hval : db.keys[(r.begin.anchor db).toNat] = res.keys.head hne := + Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans hopt) + rw [resolve_eq_getElem (db := db) (s := r.begin) (by rw [hanchor]) hlt] + exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hval + +/-- **Above-begin keys are necessary in the nonpositive-end crossed truncated +nonclamping cell.** A witness stores the reported keys together with +`(1 - end.offset)` fillers above the reported last; erasing any filler lowers the end +anchor by one, so the end resolution drops from above the last reported key to the last +reported key itself. -/ +theorem exists_witness_negCross_trunc_nonclamp_nonposEnd {r : RangeRead} {res : Result} + {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) + (hend : r.end_.offset ≤ 0) + (hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key) + (hkge : r.begin.key ≤ (k : WithTop Key)) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + obtain ⟨db, hcomp⟩ := hread + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hlast_ck : last ∈ 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 : ((last : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res + have hfill := filler_count_nonpos_more_nonpos_end hcomp hrev hmore hne + (le_of_lt hbegin) hend + set m := (1 - r.end_.offset).toNat with hm + have hm1 : 1 ≤ m := by rw [hm]; omega + set G : List Key := db.keys.filter + (fun x : Key => decide (last < x ∧ (x : WithTop Key) < r.end_.key)) with hG + have hGlen : m ≤ G.length := hfill + have hGnodup : G.Nodup := by + rw [hG]; exact db.sorted.pairwise.nodup.filter _ + have hGmem : ∀ x ∈ G, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hG] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + have hGdb : ∀ x ∈ G, x ∈ db.keys := by + intro x hx + rw [hG] at hx + exact (List.mem_filter.mp hx).1 + -- Nonclamping places every stored key above the reported last at or above `begin.key`. + have hGge : ∀ x ∈ G, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + by_contra hcon + exact not_stored_between_last_begin_of_negCross_trunc_nonclamp hcomp hrev hmore hne hbegin + hcross hnonclamp (hGdb x hx) (hGmem x hx).1 (lt_of_not_ge hcon) + set raw : List Key := (k :: G.erase k).take m with hraw + have hrawNodup : raw.Nodup := by + rw [hraw] + refine (List.take_sublist m (k :: G.erase k)).nodup ?_ + exact List.nodup_cons.mpr ⟨hGnodup.not_mem_erase, hGnodup.erase k⟩ + have hrawLen : raw.length = m := by + rw [hraw, List.length_take] + apply Nat.min_eq_left + by_cases hkG : k ∈ G + · rw [List.length_cons, List.length_erase_of_mem hkG]; omega + · rw [List.length_cons, List.erase_of_not_mem hkG]; omega + have hrawSub : ∀ x ∈ raw, x = k ∨ x ∈ G := by + intro x hx + rw [hraw] at hx + rcases List.mem_cons.mp (List.mem_of_mem_take hx) with rfl | hx' + · exact Or.inl rfl + · exact Or.inr (List.mem_of_mem_erase hx') + have hrawk : k ∈ raw := by + rw [hraw, List.mem_take_iff_getElem] + refine ⟨0, ?_, rfl⟩ + rw [List.length_cons] + omega + have hrawGt : ∀ x ∈ raw, last < x := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkgt + · exact (hGmem x hx').1 + have hrawLt : ∀ x ∈ raw, (x : WithTop Key) < r.end_.key := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkE + · exact (hGmem x hx').2 + have hrawGe : ∀ x ∈ raw, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkge + · exact hGge x hx' + set F : List Key := raw.insertionSort (· ≤ ·) with hF + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp hrawNodup) + have hFlen : F.length = m := by rw [hF, List.length_insertionSort, hrawLen] + have hFk : k ∈ F := hFperm.mem_iff.mp hrawk + have hFmem : ∀ x ∈ F, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + exact ⟨hrawGt x (hFperm.mem_iff.mpr hx), hrawLt x (hFperm.mem_iff.mpr hx)⟩ + have hFge : ∀ x ∈ F, r.begin.key ≤ (x : WithTop Key) := fun x hx => + hrawGe x (hFperm.mem_iff.mpr hx) + have hFne : F ≠ [] := by + intro h; rw [h] at hFlen; simp at hFlen; omega + set D : List Key := res.keys ++ F with hD + have hDsort : D.SortedLT := by + rw [hD] + refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩ + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hFmem b hb).1 + let dbw : DB := ⟨D, hDsort⟩ + have hdbw_keys : dbw.keys = D := rfl + -- The begin selector still resolves to the reported head. + have hrb : resolve dbw r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_append_ge_begin hdbw_keys hFge hne hbegin hnonclamp + -- every stored key is below the end key + have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hD] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE + · exact (hFmem x hx).2 + have hlenD : D.length = res.keys.length + m := by + rw [hD, List.length_append, hFlen] + have hm_int : (m : Int) = 1 - r.end_.offset := by + rw [hm]; exact Int.toNat_of_nonneg (by omega) + have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + D.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hDlt x hx + have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD] + push_cast + rw [hm_int] + ring + | coe e => + have hce : D.countP (fun x : Key => decide (x < e)) = D.length := by + rw [← countP_lt_coe (l := D) e, ← hE] + exact hcount_end + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD] + push_cast + rw [hm_int] + ring + have ht_lt : res.keys.length < D.length := by rw [hlenD]; omega + have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by + change D[res.keys.length]? = some (F.head hFne) + rw [hD, List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] + have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)] + rw [hanchor_end, Int.toNat_natCast, hDget?] + have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [hre] + exact WithTop.coe_lt_coe.mpr (hFmem (F.head hFne) (List.head_mem hFne)).1 + -- complete keys of the witness are the reported keys + have hckw : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ last)) = res.keys := by + rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF] + rw [hdbw_keys, hD, List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx), hle_last x hx⟩ + have hF_fil : F.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr (hFmem x hx).1 + rw [hres_fil, hF_fil, List.append_nil] + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨hcomp.1, hckw⟩ + have hkdbw : k ∈ dbw.keys := by + rw [hdbw_keys, hD] + exact List.mem_append.mpr (Or.inr hFk) + -- now the erased database + have hk_not_mem_res : k ∉ res.keys := by + intro hk + exact absurd (hle_last k hk) (not_le.mpr hkgt) + have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by + rw [DB.erase_keys, hdbw_keys, hD] + exact List.erase_append_right F hk_not_mem_res + have hF'ge : ∀ x ∈ F.erase k, r.begin.key ≤ (x : WithTop Key) := + fun x hx => hFge x (List.mem_of_mem_erase hx) + have hrb' : resolve (dbw.erase k) r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_append_ge_begin hD' hF'ge hne hbegin hnonclamp + have hD'lt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by + intro x hx + exact hDlt x (by + rw [DB.erase_keys, hdbw_keys] at hx + exact List.mem_of_mem_erase hx) + have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by + rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen] + omega + have hcount_end' : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hD'lt x hx + have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + | coe e => + have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) = + (dbw.erase k).keys.length := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_end' + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne + have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by + rw [Nat.cast_sub ht1]; norm_num + have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_end', hEqInt, Int.toNat_natCast] + have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by + rw [hD', List.getElem?_append] + rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)] + rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)] + congr 1 + rw [hlast, List.getLast_eq_getElem hne] + have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)] + rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?] + have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hrb', hre'] + have hpred : ∀ x ∈ (dbw.erase k).keys, + decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((↑last : Key) : WithTop Key)) = + decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ x < last) := by + intro x _ + rw [decide_eq_decide] + exact and_congr_right fun _ => WithTop.coe_lt_coe + rw [List.filter_congr hpred, hD', List.filter_append] + have hhead_le_all : ∀ x ∈ res.keys, ((head : Key) : WithTop Key) ≤ (x : WithTop Key) := by + intro x hx + exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx) + rw [show res.keys.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) = + res.keys.filter (fun x : Key => decide (x < last)) from by + apply List.filter_congr + intro x hx + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hhead_le_all x hx, h⟩⟩] + conv_lhs => rw [← List.dropLast_append_getLast hne, hlast] + rw [List.filter_append] + have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have h := hsort.pairwise.rel_dropLast_getLast hx + simpa [hlast] using h + rw [hfil] + have hFnil : (F.erase k).filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + intro _ + exact not_lt.mpr (le_of_lt (hFmem x (List.mem_of_mem_erase hx)).1) + rw [hFnil, List.append_nil] + simp + have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + intro h + have heq := h.2 + rw [hckE] at heq + have hfil : res.keys.dropLast.filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [← hlast] + exact hle_last x (List.mem_of_mem_dropLast hx) + rw [hfil] at heq + have hlen := congrArg List.length heq + rw [List.length_dropLast] at hlen + omega + exact ⟨dbw, hcompw, hkdbw, hnot⟩ + +/-- A key at or above `begin.key` and below `end.key` is necessary in the crossed +negative-begin truncated nonclamping cell with a nonpositive end offset. -/ +theorem necessary_negCross_trunc_above_begin_nonposEnd {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hnonclamp : reportedBelowBegin r res = beginSlack r + 1) + (hend : r.end_.offset ≤ 0) + (hkgt : res.keys.getLast hne < k) + (hkE : (k : WithTop Key) < r.end_.key) + (hkge : r.begin.key ≤ (k : WithTop Key)) : + Necessary r res k := by + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_negCross_trunc_nonclamp_nonposEnd hread hrev hmore hne hbegin hcross + hnonclamp hend hkgt hkE hkge + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_erase_of_mem hkdbw] + exact hnot⟩ + +/-- The crossed negative-begin truncated nonclamping cell with a nonpositive end +offset. -/ +def negCrossTruncNonclampNonposEnd (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 + 1 + +/-- **The crossed negative-begin truncated nonclamping nonpositive-end closed form is +minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_nonclamp_nonposEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncNonclampNonposEnd r res) : + Minimal r res (closedFormNegCrossTruncNonclampNonpos r res) := by + obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hnonclamp⟩ := h + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hcross : ((head : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, hheadD] at hcrossD + exact hcrossD + have hcr : closedFormNegCrossTruncNonclampNonpos r res = + ⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by + unfold closedFormNegCrossTruncNonclampNonpos + rw [hheadD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_negCross_trunc_nonclamp_nonpos hrev hmore hne hbegin hcross + hnonclamp hend hc1 hagree + · intro hc2 + exact compatible_of_agree_negCross_trunc_nonclamp_nonpos hrev hmore hne hbegin hcross + hnonclamp hend hc2 (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle + · have hkgt : last < k := lt_of_not_ge hkle + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_negCross_trunc_gap_general hread' hrev hmore hne hbegin hcross + hnonclamp (by rw [← hlast]; exact hkgt) hkB + · have hkge : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hkB + by_cases hleE : r.begin.key ≤ r.end_.key + · have hmax : max r.begin.key r.end_.key = r.end_.key := max_eq_right hleE + have hkE : (k : WithTop Key) < r.end_.key := by rw [← hmax]; exact hkhi + exact necessary_negCross_trunc_above_begin_nonposEnd hread' hrev hmore hne hbegin + hcross hnonclamp hend (by rw [← hlast]; exact hkgt) hkE hkge + · exfalso + have hltE : r.end_.key < r.begin.key := lt_of_not_ge hleE + have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hltE) + rw [hmax] at hkhi + exact absurd hkhi (not_lt.mpr hkge) + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncNonposGap.lean b/FormalConflictRange/NegCrossTruncNonposGap.lean new file mode 100644 index 0000000..0475394 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncNonposGap.lean @@ -0,0 +1,406 @@ +/- +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.NegCrossTruncBottomGap +import FormalConflictRange.NegCrossTruncClampLong +import FormalConflictRange.NegCrossTruncNonclampNonpos +import FormalConflictRange.NegCrossTruncAnchor + +/-! +# The crossed negative-begin truncated nonpositive-end gap cell + +This module closes the last forward truncated residual cell: a crossed forward read with +a negative begin offset, a nonpositive end offset, and reported keys strictly below both +selectors. The cell splits on whether the gap above `begin.key` has enough capacity for +the end overshoot, but the closed form is uniform in the two capacity regimes. + +Writing `U = max begin.key end_.key`, the minimal sound range is `[⊥, U)` when +`clampNonposBottom` holds, and `[head, U)` otherwise (where `head` is the reported head). + +The soundness half is complete. + +* Soundness of `[⊥, U)` is `sound_bottomLong_of_nonpos`: agreement below `U` fixes both + selector resolutions, and the complete window lies strictly below `end_.key ≤ U`. +* In the non-bottom branch every compatible database resolves its begin selector onto the + reported head. The gap count in `[head, begin.key)` is forced to `beginSlack + 1` by + `gap_count_eq_beginSlack_of_resolve_head`, and `resolve_begin_eq_head_of_agree_gap` + transports the resolution across agreement on that gap; this yields soundness of + `[head, U)` (`sound_headLong_of_nonbottom`). + +The necessity half is still open; see `notes/main.tex`. It splits into toggle witnesses +for keys in `(last, end_.key)` (erase) and in `[end_.key, U)` (insert), with the filler +budget determined by `clampNonposBottom`. The closed form `closedFormNegCrossTruncNonposGap` +is recorded here so the dispatch can be wired once the necessity lemmas land. +-/ + +namespace FormalConflictRange + +/-- The crossed negative-begin truncated nonpositive-end gap cell. -/ +def negCrossTruncNonposGap (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 ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key ∧ + r.end_.key < (⊤ : WithTop Key) ∧ + (r.end_.key < r.begin.key ∨ + intervalCard r.begin.key r.end_.key < (((clampNonposD r + 1 : ℕ)) : ℕ∞)) + +/-- The closed form of the crossed negative-begin truncated nonpositive-end gap cell. -/ +noncomputable def closedFormNegCrossTruncNonposGap (r : RangeRead) (res : Result) : + ConflictRange := by + classical + exact if clampNonposBottom r res then + ⟨(([] : Key) : WithTop Key), max r.begin.key r.end_.key⟩ + else + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), + max r.begin.key r.end_.key⟩ + +/-- **Soundness of `[⊥, max begin.key end.key)` at nonpositive selector offsets.** The +complete window lies strictly below `end_.key`, so agreement on every key below the +maximum fixes both selector resolutions and the whole window. -/ +theorem sound_bottomLong_of_nonpos {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) : + Sound r res (⟨(([] : Key) : WithTop Key), max r.begin.key r.end_.key⟩ : ConflictRange) := by + intro db₁ db₂ hagree + set U : WithTop Key := max r.begin.key r.end_.key with hU + have hf : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < U)) = + db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < U)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + have hB : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := U) (s := r.begin) + hbegin (le_max_left _ _) hf + have hE : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := U) (s := r.end_) + hend (le_max_right _ _) hf + have hresEnd : resolve db₁ r.end_ ≤ r.end_.key := + resolve_le_key_of_offset_nonpos hend + have hpred : ∀ db : DB, + db.keys.filter (fun k : Key => + decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) = + (db.keys.filter (fun k : Key => decide ((k : WithTop Key) < U))).filter + (fun k : Key => + decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_)) := by + intro db + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hlt : (k : WithTop Key) < U + · simp [hlt] + · have hp : ¬ (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_) := by + rintro ⟨-, hkk⟩ + exact hlt (lt_of_lt_of_le hkk (le_trans hresEnd (le_max_right _ _))) + simp [hlt, hp] + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [← hB, ← hE] + rw [hpred db₁, hpred db₂, hf] + unfold RangeRead.Compatible + rw [hck] + +/-- When the begin selector resolves onto a stored key `head` at a negative begin +offset, the gap count in `[head, begin.key)` is exactly `beginSlack + 1`. -/ +theorem gap_count_eq_beginSlack_of_resolve_head {db : DB} {r : RangeRead} {head : Key} + (hbegin : r.begin.offset < 0) + (hcross : ((head : Key) : WithTop Key) < r.begin.key) + (hanc : 0 ≤ r.begin.anchor db) + (hres : resolve db r.begin = ((head : Key) : WithTop Key)) : + db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by + classical + have hne_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + rw [hres]; exact (WithTop.coe_lt_top head).ne + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + by_contra h + rw [resolve_eq_top hanc (by omega)] at hne_top + exact hne_top rfl + have hget : db.keys[(r.begin.anchor db).toNat] = head := by + rw [resolve_eq_getElem hanc hltlen] at hres + exact WithTop.coe_inj.mp hres + have hrank : db.keys.countP (fun k : Key => decide (k < head)) = (r.begin.anchor db).toNat := + countP_lt_eq_of_getElem db.sorted hltlen hget + have hanchor : r.begin.anchor db = (db.keys.countP (fun k : Key => decide (k < head)) : ℤ) := by + have hconv : ((r.begin.anchor db).toNat : ℤ) = r.begin.anchor db := Int.toNat_of_nonneg hanc + rw [← hrank] at hconv + exact hconv.symm + have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hsplit' : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db.keys.countP (fun k : Key => decide (k < head)) + + (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length := by + rw [hsplit, ← List.countP_eq_length_filter] + have hkey := anchor_eq_count_sub_slack db r (le_of_lt hbegin) + rw [hanchor] at hkey + have hcast : ((db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) = + (db.keys.countP (fun k : Key => decide (k < head)) : ℤ) + + ((db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length : ℤ) := by + rw [hsplit']; push_cast; ring + have hG : (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length = beginSlack r + 1 := by + have h1 : ((db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℕ) : ℤ) = + (db.keys.countP (fun k : Key => decide (k < head)) : ℤ) + (beginSlack r : ℤ) + 1 := by + linarith [hkey] + have : ((db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))).length : ℤ) = (beginSlack r : ℤ) + 1 := by + linarith [hcast, h1] + omega + rw [List.countP_eq_length_filter] + exact hG + +/-- Given agreement on the gap `[head, begin.key)` and a first database whose begin +selector resolves onto the reported head, the second database (which stores the head) +resolves onto the head too. The gap count is forced to `beginSlack + 1` by the anchor +identity, so the anchor is always the rank of the head. -/ +theorem resolve_begin_eq_head_of_agree_gap + {db₁ db₂ : DB} {r : RangeRead} {res : Result} {head : Key} + (_hrev : r.reverse = false) (_hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((head : Key) : WithTop Key) < r.begin.key) + (hanc₁ : 0 ≤ r.begin.anchor db₁) + (hres₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key)) + (hagree : db₁.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = + db₂.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))) + (hhead_mem₂ : head ∈ db₂.keys) : + resolve db₂ r.begin = ((head : Key) : WithTop Key) := by + classical + have hsplit₁ := countP_lt_eq_add_filter_withTop db₁.keys db₁.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hsplit₂ := countP_lt_eq_add_filter_withTop db₂.keys db₂.nodup + (a := head) (B := r.begin.key) (le_of_lt hcross) + have hsplit₁' : db₁.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db₁.keys.countP (fun k : Key => decide (k < head)) + + db₁.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hsplit₁, ← List.countP_eq_length_filter] + have hsplit₂' : db₂.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + db₂.keys.countP (fun k : Key => decide (k < head)) + + db₂.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [hsplit₂, ← List.countP_eq_length_filter] + have hgap₁ : db₁.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := + gap_count_eq_beginSlack_of_resolve_head hbegin hcross hanc₁ hres₁ + have hgap₂ : db₂.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := by + rw [List.countP_eq_length_filter, ← hagree, ← List.countP_eq_length_filter] + exact hgap₁ + have hanchor₂ : r.begin.anchor db₂ = + (db₂.keys.countP (fun k : Key => decide (k < head)) : ℤ) := by + rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hsplit₂', hgap₂] + push_cast + ring + have hlt : db₂.keys.countP (fun k : Key => decide (k < head)) < db₂.keys.length := by + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead_mem₂ + rw [countP_lt_eq_of_getElem db₂.sorted hi hix] + exact hi + have hnat : (r.begin.anchor db₂).toNat = + db₂.keys.countP (fun k : Key => decide (k < head)) := by + rw [hanchor₂, Int.toNat_natCast] + have h0 : 0 ≤ r.begin.anchor db₂ := by + rw [hanchor₂]; exact Int.natCast_nonneg _ + have hlt' : (r.begin.anchor db₂).toNat < db₂.keys.length := by + rw [hnat]; exact hlt + have hget : db₂.keys[(r.begin.anchor db₂).toNat] = head := by + rw [getElem_congr_idx (h' := hnat)] + exact getElem_countP_lt db₂.sorted hhead_mem₂ + rw [resolve_eq_getElem h0 hlt'] + exact congrArg (fun x : Key => ((x : Key) : WithTop Key)) hget + +/-- **Compatibility transfers across agreement on the crossed negative-begin truncated +nonpositive-end gap range in the non-bottom branch.** Here the range is +`[head, max begin.key end.key)`. -/ +theorem compatible_of_agree_negCross_trunc_nonposGap_nonbottom + {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hnbot : ¬ clampNonposBottom r res) + (hend : r.end_.offset ≤ 0) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (⟨((res.keys.head hne : Key) : WithTop Key), + max r.begin.key r.end_.key⟩ : ConflictRange) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + -- The begin anchor is nonnegative, hence the begin selector resolves onto the head. + have hanc1 : 0 ≤ r.begin.anchor db1 := by + by_contra h + exact hnbot (clampNonposBottom_of_anchor_neg_trunc hc1 hrev hmore hne hbegin hend hclamp + (by omega)) + have hpre := RangeRead.compatible_keys_prefix hc1 + have hckne : RangeRead.completeKeys db1 r ≠ [] := hpre.ne_nil hne + obtain ⟨a, _ha_mem, ha_res, ha_head⟩ := + RangeRead.completeKeys_head_resolve hrev hckne hanc1 + have hhead_eq_a : res.keys.head hne = a := by + rw [← ha_head] + exact hpre.head hne + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead, hhead_eq_a]; exact ha_res + set upper : WithTop Key := max r.begin.key r.end_.key with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + hpre.subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + have hlast_upper : ((last : Key) : WithTop Key) < upper := by + have hle := resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend + exact lt_of_lt_of_le (lt_of_lt_of_le hlt1 hle) (by rw [hupper]; exact le_max_right _ _) + have hmem := (agreeOn_iff (⟨((head : Key) : WithTop Key), upper⟩ : ConflictRange) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + have hB_upper : r.begin.key ≤ upper := by rw [hupper]; exact le_max_left _ _ + have hgap : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mp hk1, hge, hlt⟩ + · rintro ⟨hk2, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hB_upper)).mpr hk2, hge, hlt⟩ + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_head_of_agree_gap hrev hne hbegin hcross' hanc1 hres1 hgap hhead_db2 + have hne_top : resolve db1 r.end_ ≠ (⊤ : WithTop Key) := + resolve_ne_top_of_offset_nonpos hend + obtain ⟨q, hq⟩ := WithTop.ne_top_iff_exists.mp hne_top + have hlast_q : last < q := by + have h := hlt1 + rw [← hq] at h + exact WithTop.coe_lt_coe.mp h + have hqne : q ≠ ([] : Key) := by + intro hnil + have h : last < ([] : Key) := by rwa [hnil] at hlast_q + exact absurd h (List.not_lt_nil last) + have hq_le_E : ((q : Key) : WithTop Key) ≤ r.end_.key := by + rw [hq] + exact resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend + have hhead_q : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hhead_le_last hlast_q)) + have hEnd_upper : r.end_.key ≤ upper := by + rw [hupper]; exact le_max_right _ _ + have hagreeEnd : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mp hk1, hge, hlt⟩ + · rintro ⟨hk2, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mpr hk2, hge, hlt⟩ + have hqget := resolve_filter_reverse_eq_some_of_ne_nil (db := db1) (s := r.end_) hend hqne hq.symm + have hresEnd2 : resolve db2 r.end_ = ((q : Key) : WithTop Key) := + resolve_eq_of_agree_between (db₁ := db1) (db₂ := db2) (s := r.end_) hend + (E := r.end_.key) rfl (le_trans hhead_q hq_le_E) hagreeEnd hqget hhead_q + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by + rw [hresEnd2] + exact WithTop.coe_lt_coe.mpr hlast_q + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hck1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hck2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + have heq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge, hle⟩ + · rintro ⟨hk2, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge, hle⟩ + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck2, ← heq, ← hck1, hres1len] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- **Soundness of `[head, max begin.key end.key)` in the non-bottom branch.** -/ +theorem sound_headLong_of_nonbottom {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hnbot : ¬ clampNonposBottom r res) + (hend : r.end_.offset ≤ 0) : + Sound r res (⟨((res.keys.head hne : Key) : WithTop Key), + max r.begin.key r.end_.key⟩ : ConflictRange) := by + intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_negCross_trunc_nonposGap_nonbottom hrev hmore hne hbegin hcross + hclamp hnbot hend hc1 hagree + · intro hc2 + exact compatible_of_agree_negCross_trunc_nonposGap_nonbottom hrev hmore hne hbegin hcross + hclamp hnbot hend hc2 (AgreeOn.symm hagree) + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncNonposGapA2a.lean b/FormalConflictRange/NegCrossTruncNonposGapA2a.lean new file mode 100644 index 0000000..39dd090 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncNonposGapA2a.lean @@ -0,0 +1,761 @@ +/- +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.NegCrossTruncNonposGap + +/-! +# The crossed truncated nonpositive-end gap cell at `end_.key < begin.key` + +This module closes the non-clamping (`¬ clampNonposBottom`) half of the crossed +negative-begin truncated nonpositive-end gap cell in the sub-region where the end key +lies strictly below the begin key. The minimal sound range is `[head, begin.key)`. + +The key structural fact is that readability plus `¬ clampNonposBottom` forces the begin +slack to equal the reported count plus the end overshoot, +`beginSlack r = res.keys.length + clampNonposD r`. Consequently every compatible +database supplies exactly `clampNonposD r + 1` filler keys in `(last, end_.key)`, and +the closed form is proved by the usual erase (for keys below `end_.key`) and insert +(for keys between `end_.key` and `begin.key`) witnesses. +-/ + +namespace FormalConflictRange + +/-- The crossed negative-begin truncated nonpositive-end gap cell in the sub-region +`end_.key < begin.key` with a non-clamping begin selector. -/ +def negCrossTruncNonposGapA2aNonbottom (r : RangeRead) (res : Result) : Prop := + negCrossTruncNonposGap r res ∧ r.end_.key < r.begin.key ∧ ¬ clampNonposBottom r res + +/-- The closed form of the crossed nonpositive-end gap cell in the non-clamping +`end_.key < begin.key` sub-region: `[head, begin.key)`. -/ +noncomputable def closedFormNegCrossTruncNonposGapA2aNonbottom + (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩ + +/-- All reported keys lie strictly below the begin key once the reported last does. -/ +theorem reportedBelowBegin_eq_length_of_last_lt {r : RangeRead} {res : Result} + (hne : res.keys ≠ []) (hsort : res.keys.SortedLT) + (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) : + reportedBelowBegin r res = res.keys.length := by + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + have hxle : x ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast hx + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hxle) (by rwa [hlastD] at hlastB) + +/-- In the non-clamping gap cell every compatible database resolves its begin selector +onto the reported head. -/ +theorem resolve_begin_eq_head_of_nonbottom {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hnbot : ¬ clampNonposBottom r res) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + have hanc : 0 ≤ r.begin.anchor db := by + by_contra h + exact hnbot (clampNonposBottom_of_anchor_neg_trunc hcomp hrev hmore hne hbegin hend + hclamp (by omega)) + have hpre := RangeRead.compatible_keys_prefix hcomp + have hckne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne + obtain ⟨a, _ha_mem, ha_res, ha_head⟩ := + RangeRead.completeKeys_head_resolve hrev hckne hanc + have hhead_eq_a : res.keys.head hne = a := by + rw [← ha_head] + exact hpre.head hne + rw [hhead_eq_a] + exact ha_res + +/-- In the `end_.key < begin.key` non-clamping sub-region, readability forces the begin +slack to equal the reported count plus the end overshoot. -/ +theorem beginSlack_eq_of_A2a_nonbottom {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hnbot : ¬ clampNonposBottom r res) + (hEB : r.end_.key < r.begin.key) + (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hlastE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key) : + beginSlack r = res.keys.length + clampNonposD r := by + classical + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne + have hhead_le_last : head ≤ last := hle_last head hhead_mem + have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB + have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by rwa [hlastD] at hlastE + have hheadB : ((head : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlastB' + have hres : resolve db r.begin = ((head : Key) : WithTop Key) := by + have := resolve_begin_eq_head_of_nonbottom hcomp hrev hmore hne hbegin hend hclamp hnbot + simpa [hhead] using this + have hanc : 0 ≤ r.begin.anchor db := by + by_contra h + exact hnbot (clampNonposBottom_of_anchor_neg_trunc hcomp hrev hmore hne hbegin hend + hclamp (by omega)) + have hgap : db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := + gap_count_eq_beginSlack_of_resolve_head hbegin hheadB hanc hres + -- stored keys in `[head, last]` are exactly the reported keys + have hresEnd_gt : ((last : Key) : WithTop Key) < resolve db r.end_ := by + have hlast_ck : last ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset hlast_mem + exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hfilterComp : (RangeRead.completeKeys db r).filter + (fun k => decide (k ≤ last)) = res.keys := + ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2 + have hfilterBegin : (RangeRead.completeKeys db r).filter + (fun k => decide (k ≤ last)) = + db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin hrev hres hresEnd_gt + have hlen_head_last : (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last))).length + = res.keys.length := by + rw [← hfilterBegin, hfilterComp] + have hfilter_ka : db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + by_cases hk : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · simp only [hk, true_and] + exact WithTop.coe_lt_coe.trans (lt_keyAfter_iff k last) + · simp only [hk, false_and] + have hlen_ka : (between db ((head : Key) : WithTop Key) + (((keyAfter last : Key) : WithTop Key))).length = res.keys.length := by + unfold between + rw [hfilter_ka, hlen_head_last] + have hcntB : cnt db r.begin.key = + cnt db ((head : Key) : WithTop Key) + + db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [cnt_eq_add_between db (le_of_lt hheadB), between, List.countP_eq_length_filter] + have hcntKa : cnt db (((keyAfter last : Key) : WithTop Key)) = + cnt db ((head : Key) : WithTop Key) + + (between db ((head : Key) : WithTop Key) + (((keyAfter last : Key) : WithTop Key))).length := + cnt_eq_add_between db (WithTop.coe_le_coe.mpr + (le_trans hhead_le_last (le_of_lt (keyAfter_self_lt last)))) + have hcntKB : cnt db r.begin.key = + cnt db (((keyAfter last : Key) : WithTop Key)) + + (between db (((keyAfter last : Key) : WithTop Key)) r.begin.key).length := + cnt_eq_add_between db (keyAfter_le_of_lt hlastB') + have hresEnd_ne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro h + rw [h] at hresEnd_gt + exact absurd hresEnd_gt + (not_lt.mpr (coe_nil_le_key_withTop ((last : Key) : WithTop Key))) + have hbetween_len : (between db (resolve db r.end_) r.end_.key).length + = clampNonposD r + 1 := + between_resolve_end_length hend hresEnd_ne + have hka_le_resEnd : ((keyAfter last : Key) : WithTop Key) ≤ resolve db r.end_ := + keyAfter_le_of_lt hresEnd_gt + have hG : clampNonposD r + 1 ≤ + (between db (((keyAfter last : Key) : WithTop Key)) r.begin.key).length := by + rw [← hbetween_len] + apply List.Sublist.length_le + show List.Sublist + (db.keys.filter (fun k : Key => decide + (resolve db r.end_ ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.end_.key))) + (db.keys.filter (fun k : Key => decide + (((keyAfter last : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key))) + apply filter_sublist_filter_of_imp + intro k hk + rw [decide_eq_true_eq] at hk ⊢ + exact ⟨le_trans hka_le_resEnd hk.1, lt_trans hk.2 hEB⟩ + have hkey : db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = res.keys.length + + (between db (((keyAfter last : Key) : WithTop Key)) r.begin.key).length := by + rw [hcntKa, hlen_ka] at hcntKB + omega + have hlow : res.keys.length + clampNonposD r ≤ beginSlack r := by + omega + have hc0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le (le_of_lt hEB) + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + have hnot : ¬ (res.keys.length + (clampNonposD r + 1) ≤ beginSlack r) := by + intro h + apply hnbot + unfold clampNonposBottom + rw [hc0, hrep, tsub_zero] + exact_mod_cast h + have hup : beginSlack r ≤ res.keys.length + clampNonposD r := by omega + omega + +/-- Readability supplies enough room in `(last, end_.key)` for the erase and insert +witnesses: the end-resolution identity puts `clampNonposD + 1` stored keys there. -/ +theorem intervalCard_keyAfter_getLast_ge {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hne : res.keys ≠ []) + (hend : r.end_.offset ≤ 0) : + (((clampNonposD r + 1 : ℕ)) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key := by + classical + set last : Key := res.keys.getLast hne with hlast + have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne + have hresEnd_gt : ((last : Key) : WithTop Key) < resolve db r.end_ := by + have hlast_ck : last ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset hlast_mem + exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hresEnd_ne : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by + intro h + rw [h] at hresEnd_gt + exact absurd hresEnd_gt + (not_lt.mpr (coe_nil_le_key_withTop ((last : Key) : WithTop Key))) + have hbetween_len : (between db (resolve db r.end_) r.end_.key).length + = clampNonposD r + 1 := + between_resolve_end_length hend hresEnd_ne + have hka_le_resEnd : ((keyAfter last : Key) : WithTop Key) ≤ resolve db r.end_ := + keyAfter_le_of_lt hresEnd_gt + have hresEnd_le_E : resolve db r.end_ ≤ r.end_.key := + resolve_le_key_of_offset_nonpos (db := db) (s := r.end_) hend + have hkaE : ((keyAfter last : Key) : WithTop Key) ≤ r.end_.key := + le_trans hka_le_resEnd hresEnd_le_E + have hcard := between_length_le_intervalCard (db := db) + (E := ((keyAfter last : Key) : WithTop Key)) (B := r.end_.key) hkaE + have hlen_le : (between db (resolve db r.end_) r.end_.key).length ≤ + (between db (((keyAfter last : Key) : WithTop Key)) r.end_.key).length := by + apply List.Sublist.length_le + show List.Sublist + (db.keys.filter (fun k : Key => decide + (resolve db r.end_ ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.end_.key))) + (db.keys.filter (fun k : Key => decide + (((keyAfter last : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.end_.key))) + apply filter_sublist_filter_of_imp + intro k hk + rw [decide_eq_true_eq] at hk ⊢ + exact ⟨le_trans hka_le_resEnd hk.1, hk.2⟩ + calc (((clampNonposD r + 1 : ℕ)) : ℕ∞) + = ((between db (resolve db r.end_) r.end_.key).length : ℕ∞) := by rw [hbetween_len] + _ ≤ ((between db (((keyAfter last : Key) : WithTop Key)) r.end_.key).length : ℕ∞) := + Nat.cast_le.mpr hlen_le + _ ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key := hcard + +/-- The element at the seam of a split list is the head of the right part. -/ +theorem getElem_append_length {as bs : List Key} (hbs : bs ≠ []) : + (as ++ bs)[as.length]'(by + rw [List.length_append]; have := List.length_pos_iff.mpr hbs; omega) = + bs.head hbs := by + rw [List.getElem_append_right (as := as) (bs := bs) (i := as.length) (by omega) + (h₂ := by + rw [List.length_append]; have := List.length_pos_iff.mpr hbs; omega)] + simp only [Nat.sub_self] + exact (List.head_eq_getElem_zero hbs).symm + +/-- Padding the reported keys with filler keys in `(last, end_.key)` (all of them below +`end_.key` and hence below `begin.key`) gives a compatible database whose begin selector +resolves to the reported head and whose end selector resolves to the first filler. -/ +theorem compatible_fillers_A2a {r : RangeRead} {res : Result} {dbF : DB} + (hsort : res.keys.SortedLT) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) + (hlim : RangeRead.withinLimit r res.keys) + (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hslack : beginSlack r = res.keys.length + clampNonposD r) + (hEB : r.end_.key < r.begin.key) + (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x) + (hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key) + (hFlen : dbF.keys.length = clampNonposD r + 1) : + ∃ dbw : DB, dbw.keys = res.keys ++ dbF.keys ∧ RangeRead.Compatible dbw r res ∧ + r.begin.anchor dbw = 0 ∧ r.end_.anchor dbw = (res.keys.length : ℤ) := by + classical + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by rw [← hlastD]; exact hlastB + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by + intro h + rw [h] at hEB + exact absurd hEB (not_lt.mpr le_top) + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = ((e : Key) : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h hEne + | coe e => exact ⟨e, rfl⟩ + have hDlen : dbF.keys.length = clampNonposD r + 1 := hFlen + have hFne : dbF.keys ≠ [] := by intro h; rw [h] at hDlen; simp at hDlen + -- sorted concatenation + set D : List Key := res.keys ++ dbF.keys with hD + have hDsort : D.SortedLT := by + rw [hD] + exact List.sortedLT_append.mpr ⟨hsort, dbF.sorted, fun a ha b hb => + lt_of_le_of_lt (hle_last a ha) (hFlast b hb)⟩ + let dbw : DB := ⟨D, hDsort⟩ + have hkeyw : dbw.keys = res.keys ++ dbF.keys := rfl + have hnodup : dbw.keys.Nodup := dbw.nodup + -- every filler lies below `begin.key` + have hFltB : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.begin.key := fun x hx => + lt_of_lt_of_le (hFend x hx) (le_of_lt hEB) + -- begin anchor is zero + have hanchorB : r.begin.anchor dbw = 0 := by + have hc : dbw.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastB' + · exact hFltB x hx + have hc' : ((dbw.keys.length : ℕ) : ℤ) = + res.keys.length + clampNonposD r + 1 := by + rw [hkeyw, List.length_append, hDlen]; push_cast; ring + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hc] + have hlen : (dbw.keys.length : ℤ) = res.keys.length + clampNonposD r + 1 := by + exact_mod_cast hc' + have hslack' : (beginSlack r : ℤ) = (res.keys.length : ℤ) + clampNonposD r := by + rw [hslack]; push_cast; ring + rw [hlen, hslack']; ring + have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by + have hhead_mem : dbF.keys.head hFne ∈ dbF.keys := List.head_mem hFne + exact lt_trans (WithTop.coe_lt_coe.mpr (hFlast _ hhead_mem)) (hFend _ hhead_mem) + -- end anchor is the reported count + have hcountE : dbw.keys.countP (fun k : Key => decide (k < e)) = + res.keys.length + clampNonposD r + 1 := by + have hcl : dbw.keys.countP (fun k : Key => decide (k < e)) = dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [hkeyw] at hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · apply WithTop.coe_lt_coe.mp + rw [← he] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E + · apply WithTop.coe_lt_coe.mp + simpa [he] using hFend x hx + rw [hcl, hkeyw, List.length_append, hDlen] + omega + have hanchorE : r.end_.anchor dbw = (res.keys.length : ℤ) := by + rw [anchor_of_coe (s := r.end_) (v := e) he, hcountE] + have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by + rw [clampNonposD_eq] + have hnonneg : 0 ≤ -r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnonneg]; ring + rw [hoff]; push_cast; ring + refine ⟨dbw, hkeyw, ?_, hanchorB, hanchorE⟩ + -- compatibility: the complete keys are exactly the reported keys + have hhead_eq : dbw.keys[0]'(by rw [hkeyw, List.length_append]; omega) = + res.keys.head hne := by + have hleft := List.getElem_append_left (as := res.keys) (bs := dbF.keys) + (i := 0) (h := by simp [List.length_pos_iff.mpr hne]) + (h' := by simp [List.length_pos_iff.mpr hne]) + exact hleft.trans (List.head_eq_getElem_zero hne).symm + have hfill_head_gt : ∀ x ∈ dbF.keys, x ≥ dbF.keys.head hFne := fun x hx => + (dbF.sorted.pairwise.imp le_of_lt).rel_head hx + have hresolve_end : resolve dbw r.end_ = ((dbF.keys.head hFne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor dbw := by rw [hanchorE]; exact_mod_cast Nat.zero_le _ + have hlt : (r.end_.anchor dbw).toNat < dbw.keys.length := by + rw [hanchorE, hkeyw, List.length_append, hDlen] + omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.end_.anchor dbw).toNat = res.keys.length := by + rw [hanchorE]; simp + simp only [hidx] + have hget : dbw.keys[res.keys.length]'(by rw [hkeyw, List.length_append, hDlen]; omega) = + dbF.keys.head hFne := by + have h1 : dbw.keys = res.keys ++ dbF.keys := hkeyw + rw [List.getElem_of_eq h1] + exact getElem_append_length hFne + exact congrArg (fun x : Key => (x : WithTop Key)) hget + have hresolve_begin : resolve dbw r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor dbw := by rw [hanchorB] + have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by + rw [hanchorB, hkeyw, List.length_append, hDlen] + simp only [Int.toNat_zero] + omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor dbw).toNat = 0 := by rw [hanchorB]; rfl + simp only [hidx] + exact congrArg (fun x : Key => (x : WithTop Key)) hhead_eq + have hckmem : ∀ k : Key, k ∈ RangeRead.completeKeys dbw r ↔ k ∈ res.keys := by + intro k + rw [RangeRead.mem_completeKeys, hresolve_begin, hresolve_end] + constructor + · rintro ⟨hk, -, hlt⟩ + rw [hkeyw] at hk + rcases List.mem_append.mp hk with hk | hk + · exact hk + · exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (hfill_head_gt k hk))) + · intro hk + refine ⟨?_, ?_, ?_⟩ + · rw [hkeyw]; exact List.mem_append_left _ hk + · exact WithTop.coe_le_coe.mpr + ((hsort.pairwise.imp le_of_lt).rel_head hk) + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk)) + (WithTop.coe_lt_coe.mpr (hFlast _ (List.head_mem hFne))) + have hck : RangeRead.completeKeys dbw r = res.keys := by + apply List.SortedLT.eq_of_mem_iff + · rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])] + exact List.sortedLT_iff_pairwise.mpr (RangeRead.inRange_pairwise dbw _ _) + · exact hsort + · exact hckmem + refine ⟨hlim, ?_⟩ + left + refine ⟨hmore, ?_, hne⟩ + rw [hck] + +/-- Inserting a key above every element of a list appends it at the end. -/ +theorem orderedInsert_of_forall_lt {l : List Key} {k : Key} (h : ∀ x ∈ l, x < k) : + l.orderedInsert (· ≤ ·) k = l ++ [k] := by + induction l with + | nil => simp + | cons a t ih => + rw [List.orderedInsert_of_not_le t (r := (· ≤ ·)) + (not_le.mpr (h a (List.mem_cons_self ..)))] + have := ih (fun x hx => h x (List.mem_cons_of_mem _ hx)) + simp only [this, List.cons_append] + +/-- A key strictly between the reported last and `end_.key` has an erase witness in the +A2a non-clamping subcell: the target sits among `clampNonposD + 1` padding keys in +`(last, end_.key)`, so the end selector resolves into the padding; erasing the target +lowers the end resolution to the reported last, which drops the last reported key from +the truncated window. -/ +theorem exists_witness_erase_A2a {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aNonbottom r res) + (hne : res.keys ≠ []) + (hkgt : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) + (hkE : (k : WithTop Key) < r.end_.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + obtain ⟨hcell0, hEB, hnbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_k : res.keys.getLast hne < k := WithTop.coe_lt_coe.mp hkgt + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hslack : beginSlack r = res.keys.length + clampNonposD r := + beginSlack_eq_of_A2a_nonbottom hcomp₀ hrev hmore hne hbegin hend hclamp hnbot hEB + hlastB hlastE + have hk_not_res : k ∉ res.keys := by + intro hx + exact absurd hlast_lt_k (not_lt.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) + -- pad keys in `(last, end_.key)` avoiding `k` + have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key := + intervalCard_keyAfter_getLast_ge hcomp₀ hne hend + obtain ⟨dbB, hBmem, hBk, hBlen⟩ := + exists_db_keysIn_avoid (B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) + (E := r.end_.key) (k := k) (m := clampNonposD r) hcard + have hBgt : ∀ x ∈ dbB.keys, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne))) + (hBmem x hx).1) + have hBElt : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hBmem x hx).2 + -- assemble `k` with the padding and sort + set raw : List Key := k :: dbB.keys with hraw + have hrawNodup : raw.Nodup := by + rw [hraw] + exact List.nodup_cons.mpr ⟨hBk, dbB.sorted.pairwise.nodup⟩ + set F : List Key := raw.insertionSort (· ≤ ·) with hF + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp hrawNodup) + let dbF : DB := ⟨F, hFsorted⟩ + have hFlen : dbF.keys.length = clampNonposD r + 1 := by + change F.length = clampNonposD r + 1 + rw [hF, List.length_insertionSort, hraw, List.length_cons, hBlen] + have hFlt : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by + intro x hx + have hx' := hFperm.mem_iff.mpr hx + rw [hraw] at hx' + rcases List.mem_cons.mp hx' with rfl | hxB + · exact hlast_lt_k + · exact hBgt x hxB + have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := by + intro x hx + have hx' := hFperm.mem_iff.mpr hx + rw [hraw] at hx' + rcases List.mem_cons.mp hx' with rfl | hxB + · exact hkE + · exact hBElt x hxB + obtain ⟨dbw, hkeyw, hcompw, hanchB, hanchE⟩ := + compatible_fillers_A2a hsort hrev hmore hne hlim hbegin hend hslack hEB hlastB + hFlt hFend hFlen + have hk_in_F : k ∈ dbF.keys := + hFperm.mem_iff.mp (by rw [hraw]; exact List.mem_cons_self ..) + have hk_in_dbw : k ∈ dbw.keys := by + rw [hkeyw]; exact List.mem_append.mpr (Or.inr hk_in_F) + refine ⟨dbw, hcompw, hk_in_dbw, ?_⟩ + intro hcomp' + have herase_keys : (dbw.erase k).keys = res.keys ++ dbF.keys.erase k := by + rw [DB.erase_keys, hkeyw, List.erase_append_right _ hk_not_res] + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + have hcount_erase : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + res.keys.length + clampNonposD r := by + rw [herase_keys, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_E + have hFall : dbF.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = dbF.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hFend x hx + have hktrue : (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) k = true := by + rw [decide_eq_true_eq]; exact hkE + have h2 : (dbF.keys.erase k).countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = clampNonposD r := by + rw [List.countP_erase, if_pos ⟨hk_in_F, hktrue⟩, hFall, hFlen] + omega + rw [h1, h2] + have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by + rw [clampNonposD_eq] + have hnonneg : 0 ≤ -r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnonneg]; ring + have hanchor_erase : r.end_.anchor (dbw.erase k) = (res.keys.length : ℤ) - 1 := by + cases hE : r.end_.key with + | top => exact absurd hE (ne_of_lt hEne) + | coe e => + rw [anchor_of_coe (s := r.end_) (v := e) hE] + have hce : (dbw.erase k).keys.countP (fun x => decide (x < e)) = + res.keys.length + clampNonposD r := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_erase + rw [hce, hoff] + push_cast + ring + have ht_pos : 0 < res.keys.length := List.length_pos_iff.mpr hne + have hresolve_end : resolve (dbw.erase k) r.end_ = + ((res.keys.getLast hne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanchor_erase]; omega + have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by + rw [herase_keys, List.length_append, List.length_erase, if_pos hk_in_F, hFlen] + omega + have hlt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by + rw [hanchor_erase, hlenErase] + omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_erase] + have : ((res.keys.length : ℤ) - 1).toNat = res.keys.length - 1 := by omega + exact this + have hget : (dbw.erase k).keys[(r.end_.anchor (dbw.erase k)).toNat] = + res.keys.getLast hne := by + simp only [hidx, herase_keys] + have hlen : res.keys.length - 1 < res.keys.length := by omega + rw [List.getElem_append_left (i := res.keys.length - 1) (h := hlen)] + exact (List.getLast_eq_getElem hne).symm + rw [hget] + have hlast_not_ck : res.keys.getLast hne ∉ + RangeRead.completeKeys (dbw.erase k) r := by + intro hmem + have hlt := (RangeRead.mem_completeKeys.mp hmem).2.2 + rw [hresolve_end] at hlt + exact lt_irrefl _ hlt + have hlast_in_filt : res.keys.getLast hne ∈ + (RangeRead.completeKeys (dbw.erase k) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [hfilt'] + exact List.getLast_mem hne + rw [List.mem_filter] at hlast_in_filt + exact hlast_not_ck hlast_in_filt.1 + +/-- A key in `[end_.key, begin.key)` has an insert witness in the A2a non-clamping +subcell: padding with `clampNonposD + 1` keys in `(last, end_.key)` makes the begin +selector resolve to the reported head with anchor zero; inserting the target raises the +begin anchor to one, so the head drops out of the complete window. -/ +theorem exists_witness_insert_A2a {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aNonbottom r res) + (hne : res.keys ≠ []) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (toggle dbw k) r res := by + obtain ⟨hcell0, hEB, hnbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hslack : beginSlack r = res.keys.length + clampNonposD r := + beginSlack_eq_of_A2a_nonbottom hcomp₀ hrev hmore hne hbegin hend hclamp hnbot hEB + hlastB hlastE + have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key := + intervalCard_keyAfter_getLast_ge hcomp₀ hne hend + obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn + (B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) (E := r.end_.key) + (m := clampNonposD r + 1) hcard + have hFlt : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne))) + (hFmem x hx).1) + have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2 + obtain ⟨dbw, hkeyw, hcompw, hanchB, hanchE⟩ := + compatible_fillers_A2a hsort hrev hmore hne hlim hbegin hend hslack hEB hlastB + hFlt hFend hFlen + have hdbw_lt_k : ∀ x ∈ dbw.keys, x < k := by + intro x hx + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) + (WithTop.coe_lt_coe.mpr (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E hkE)))) + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hFend x hx) hkE) + have hk_not_dbw : k ∉ dbw.keys := fun hx => absurd (hdbw_lt_k k hx) (lt_irrefl k) + refine ⟨dbw, hcompw, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not_dbw] + have hins_keys : (dbw.insert k hk_not_dbw).keys = dbw.keys ++ [k] := by + rw [DB.insert_keys] + exact orderedInsert_of_forall_lt hdbw_lt_k + intro hcomp' + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + have hlen_dbw : 2 ≤ dbw.keys.length := by + rw [hkeyw, List.length_append, hFlen] + have : 1 ≤ res.keys.length := List.length_pos_iff.mpr hne + omega + have hhead0 : dbw.keys[0]'(by omega) = res.keys.head hne := by + have h1 : dbw.keys = res.keys ++ dbF.keys := hkeyw + rw [List.getElem_of_eq h1] + have hpos : 0 < res.keys.length := List.length_pos_iff.mpr hne + rw [List.getElem_append_left (i := 0) (h := hpos) + (h' := by rw [List.length_append]; omega)] + exact (List.head_eq_getElem_zero hne).symm + have hhead_lt : ((res.keys.head hne : Key) : WithTop Key) < + ((dbw.keys[1] : Key) : WithTop Key) := by + rw [← hhead0] + have hlt := dbw.sorted.getElem_lt_getElem_of_lt (i := 0) (j := 1) + (hi := by omega) (hj := by omega) (by omega) + exact WithTop.coe_lt_coe.mpr hlt + have hcount_before : dbw.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + res.keys.length + clampNonposD r + 1 := by + rw [hkeyw, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B + have h2 : dbF.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbF.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hFend x hx) (le_of_lt hEB) + rw [h1, h2, hFlen] + omega + have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + res.keys.length + clampNonposD r + 2 := by + rw [hins_keys, List.countP_append, hcount_before, List.countP_singleton, + decide_eq_true_eq.mpr hkB] + norm_num + have hanchor_ins : r.begin.anchor (dbw.insert k hk_not_dbw) = 1 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins] + have hslack' : (beginSlack r : ℤ) = (res.keys.length : ℤ) + clampNonposD r := by + rw [hslack]; push_cast; ring + rw [hslack']; push_cast; ring + have hresolve_ins : resolve (dbw.insert k hk_not_dbw) r.begin = + ((dbw.keys[1] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by rw [hanchor_ins]; norm_num + have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat < + (dbw.insert k hk_not_dbw).keys.length := by + rw [hanchor_ins, hins_keys, List.length_append, List.length_singleton] + exact Nat.lt_succ_of_lt (by omega) + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat = 1 := by + rw [hanchor_ins]; rfl + simp only [hidx, hins_keys] + rw [List.getElem_append_left (i := 1) (h := by omega)] + have hhead_in : res.keys.head hne ∈ + (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [hfilt'] + exact List.head_mem hne + rw [List.mem_filter] at hhead_in + have hge := (RangeRead.mem_completeKeys.mp hhead_in.1).2.1 + rw [hresolve_ins] at hge + exact absurd hhead_lt (not_lt.mpr hge) + +/-- **The `end_.key < begin.key` non-clamping subcell of the crossed truncated +nonpositive-end gap cell is minimal**, with closed form `[head, begin.key)`. -/ +theorem minimal_closedForm_of_negCross_trunc_nonposGap_A2a_nonbottom + {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aNonbottom r res) : + Minimal r res (closedFormNegCrossTruncNonposGapA2aNonbottom r res) := by + have hcellKeep : negCrossTruncNonposGapA2aNonbottom r res := hcell + obtain ⟨hcell0, hEB, hnbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hEB) + have hcross' : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.head hne := by + simp [leastReported, hrev, List.head?_eq_some_head hne] + rwa [hle] at hcross + have hclosed : closedFormNegCrossTruncNonposGapA2aNonbottom r res = + ⟨((res.keys.head hne : Key) : WithTop Key), r.begin.key⟩ := by + unfold closedFormNegCrossTruncNonposGapA2aNonbottom + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rw [hclosed] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have hs := sound_headLong_of_nonbottom hrev hmore hne hbegin hcross' hclamp hnbot hend + rwa [hmax] at hs + · intro k hkhead hkB + have hkhead' : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := hkhead + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hklast : (k : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hkhead') (WithTop.coe_le_coe.mp hklast) + · have hlastk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) := + not_le.mp hklast + by_cases hkE : (k : WithTop Key) < r.end_.key + · obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_erase_A2a hread hcellKeep hne hlastk hkE + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩ + · have hEk : r.end_.key ≤ (k : WithTop Key) := not_lt.mp hkE + obtain ⟨dbw, hcompw, hnot⟩ := + exists_witness_insert_A2a hread hcellKeep hne hEk hkB + exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩ + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncNonposGapA2aBottom.lean b/FormalConflictRange/NegCrossTruncNonposGapA2aBottom.lean new file mode 100644 index 0000000..3aec41c --- /dev/null +++ b/FormalConflictRange/NegCrossTruncNonposGapA2aBottom.lean @@ -0,0 +1,1778 @@ +/- +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.NegCrossTruncNonposGapA2a + +/-! +# The crossed truncated nonpositive-end gap cell at `end_.key < begin.key`, clamping half + +This module closes the clamping (`clampNonposBottom`) half of the crossed +negative-begin truncated nonpositive-end gap cell in the sub-region where the end key +lies strictly below the begin key. +-/ + +namespace FormalConflictRange + +/-- The crossed negative-begin truncated nonpositive-end gap cell in the sub-region +`end_.key < begin.key` with a clamping begin selector. -/ +def negCrossTruncNonposGapA2aBottom (r : RangeRead) (res : Result) : Prop := + negCrossTruncNonposGap r res ∧ r.end_.key < r.begin.key ∧ clampNonposBottom r res + +/-- The gap-capacity condition selecting the `[⊥, begin.key)` branch in the clamping +sub-region. -/ +def negCrossTruncNonposGapA2aBottomHigh (r : RangeRead) (res : Result) : Prop := + negCrossTruncNonposGapA2aBottom r res ∧ + ((beginSlack r + 1 + (if res.keys.headD ([] : Key) = [] then 1 else 0) : ℕ) : ℕ∞) ≤ + (res.keys.length : ℕ∞) + + intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) + r.begin.key + +instance instDecidableA2aBottomHigh (r : RangeRead) (res : Result) : + Decidable (negCrossTruncNonposGapA2aBottomHigh r res) := by + unfold negCrossTruncNonposGapA2aBottomHigh negCrossTruncNonposGapA2aBottom + negCrossTruncNonposGap clampNonposBottom + infer_instance + +/-- The closed form of the crossed nonpositive-end gap cell in the clamping +`end_.key < begin.key` sub-region. -/ +noncomputable def closedFormNegCrossTruncNonposGapA2aBottom + (r : RangeRead) (res : Result) : ConflictRange := by + classical + exact if negCrossTruncNonposGapA2aBottomHigh r res then + ⟨(([] : Key) : WithTop Key), r.begin.key⟩ + else + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + +/-- Padding the reported keys with `clampNonposD + 1` fillers in `(last, end_.key)` gives a +compatible database with a clamping begin selector, provided the begin slack absorbs the +whole stored list. -/ +theorem compatible_clamp_fillers_A2a {r : RangeRead} {res : Result} {dbF : DB} + (hsort : res.keys.SortedLT) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) + (hlim : RangeRead.withinLimit r res.keys) + (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hEB : r.end_.key < r.begin.key) + (hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x) + (hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key) + (hFlen : dbF.keys.length = clampNonposD r + 1) + (hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r) : + ∃ dbw : DB, dbw.keys = res.keys ++ dbF.keys ∧ RangeRead.Compatible dbw r res ∧ + r.begin.anchor dbw < 0 ∧ r.end_.anchor dbw = (res.keys.length : ℤ) := by + classical + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hEne : r.end_.key ≠ (⊤ : WithTop Key) := by + intro h + rw [h] at hEB + exact absurd hEB (not_lt.mpr le_top) + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = ((e : Key) : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h hEne + | coe e => exact ⟨e, rfl⟩ + have hDlen : dbF.keys.length = clampNonposD r + 1 := hFlen + have hFne : dbF.keys ≠ [] := by intro h; rw [h] at hDlen; simp at hDlen + have hFlast' : ∀ x ∈ dbF.keys, last < x := fun x hx => by + rw [hlast]; exact hFlast x hx + -- the reported last and every filler lie below `begin.key` + have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by + obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil _ hFne + exact lt_trans (WithTop.coe_lt_coe.mpr (hFlast' x hx)) (hFend x hx) + have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := + lt_of_lt_of_le hlast_lt_E (le_of_lt hEB) + have hFltB : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.begin.key := fun x hx => + lt_of_lt_of_le (hFend x hx) (le_of_lt hEB) + set D : List Key := res.keys ++ dbF.keys with hD + have hDsort : D.SortedLT := by + rw [hD] + exact List.sortedLT_append.mpr ⟨hsort, dbF.sorted, fun a ha b hb => + lt_of_le_of_lt (hle_last a ha) (hFlast' b hb)⟩ + let dbw : DB := ⟨D, hDsort⟩ + have hkeyw : dbw.keys = res.keys ++ dbF.keys := rfl + have hfill_head_gt : ∀ x ∈ dbF.keys, dbF.keys.head hFne ≤ x := fun x hx => + (dbF.sorted.pairwise.imp le_of_lt).rel_head hx + have hcountAll : dbw.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < + r.begin.key)) = dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastB' + · exact hFltB x hx + have hlen : dbw.keys.length = res.keys.length + (clampNonposD r + 1) := by + rw [hkeyw, List.length_append, hDlen] + have hanchorB : r.begin.anchor dbw < 0 := by + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountAll, hlen] + have hcast : (res.keys.length + (clampNonposD r + 1) : ℤ) ≤ (beginSlack r : ℤ) := by + exact_mod_cast hcount + omega + have hcountE : dbw.keys.countP (fun k : Key => decide (k < e)) = + res.keys.length + clampNonposD r + 1 := by + have hcl : dbw.keys.countP (fun k : Key => decide (k < e)) = dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · apply WithTop.coe_lt_coe.mp + rw [← he] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E + · apply WithTop.coe_lt_coe.mp + simpa [he] using hFend x hx + rw [hcl, hkeyw, List.length_append, hDlen] + omega + have hanchorE : r.end_.anchor dbw = (res.keys.length : ℤ) := by + rw [anchor_of_coe (s := r.end_) (v := e) he, hcountE] + have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by + rw [clampNonposD_eq] + have hnonneg : 0 ≤ -r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnonneg]; ring + rw [hoff]; push_cast; ring + have hresolve_begin : resolve dbw r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil hanchorB + have hresolve_end : resolve dbw r.end_ = ((dbF.keys.head hFne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor dbw := by rw [hanchorE]; exact_mod_cast Nat.zero_le _ + have hlt : (r.end_.anchor dbw).toNat < dbw.keys.length := by + rw [hanchorE, hkeyw, List.length_append, hDlen] + omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.end_.anchor dbw).toNat = res.keys.length := by + rw [hanchorE]; simp + simp only [hidx] + have hget : dbw.keys[res.keys.length]'(by rw [hkeyw, List.length_append, hDlen]; omega) = + dbF.keys.head hFne := by + have h1 : dbw.keys = res.keys ++ dbF.keys := hkeyw + rw [List.getElem_of_eq h1] + exact getElem_append_length hFne + exact congrArg (fun x : Key => (x : WithTop Key)) hget + have hckmem : ∀ k : Key, k ∈ RangeRead.completeKeys dbw r ↔ k ∈ res.keys := by + intro k + rw [RangeRead.mem_completeKeys, hresolve_begin, hresolve_end] + constructor + · rintro ⟨hk, -, hlt⟩ + rw [hkeyw] at hk + rcases List.mem_append.mp hk with hk | hk + · exact hk + · exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (hfill_head_gt k hk))) + · intro hk + refine ⟨?_, ?_, ?_⟩ + · rw [hkeyw]; exact List.mem_append_left _ hk + · exact WithTop.coe_le_coe.mpr (nil_le_key k) + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk)) + (WithTop.coe_lt_coe.mpr (hFlast' _ (List.head_mem hFne))) + have hck : RangeRead.completeKeys dbw r = res.keys := by + apply List.SortedLT.eq_of_mem_iff + · rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])] + exact List.sortedLT_iff_pairwise.mpr (RangeRead.inRange_pairwise dbw _ _) + · exact hsort + · exact hckmem + refine ⟨dbw, hkeyw, ?_, hanchorB, hanchorE⟩ + rw [compatible_iff_truncated_keys hrev hmore hne] + refine ⟨hlim, ?_⟩ + rw [hck] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hle_last x hx + +/-- A clamping compatible database exists in the crossed nonpositive-end gap cell. -/ +theorem exists_clamp_compatible_A2aBottom {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) : + ∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by + obtain ⟨hcell0, hEB, hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key := + intervalCard_keyAfter_getLast_ge hcomp₀ hne hend + obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn + (B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) (E := r.end_.key) + (m := clampNonposD r + 1) hcard + have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne))) + (hFmem x hx).1) + have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2 + have hc0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le (le_of_lt hEB) + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + have h := hbot + unfold clampNonposBottom at h + rw [hc0, hrep, tsub_zero] at h + exact_mod_cast h + obtain ⟨dbw, hkeyw, hcompw, hanchor, -⟩ := + compatible_clamp_fillers_A2a hsort hrev hmore hne hlim hbegin hend hEB hFlast hFend hFlen + hcount + exact ⟨dbw, hcompw, hanchor⟩ + +/-- A key strictly below the reported head has an insert witness in the clamping cell: +inserting it into a clamping compatible database adds it to the complete window and hence to +the truncated read-off. -/ +theorem exists_witness_below_head_A2aBottom {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) + (hne : res.keys ≠ []) + (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (toggle dbw k) r res := by + obtain ⟨hcell0, hEB, hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, -, -, -⟩ := hcell0 + have hread' := hread + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlast_lt_B + have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hk hhead_lt_B + have hk_not_res : k ∉ res.keys := by + intro hx + exact absurd hk (not_lt.mpr + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) + have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key := + intervalCard_keyAfter_getLast_ge hcomp₀ hne hend + obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn + (B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) (E := r.end_.key) + (m := clampNonposD r + 1) hcard + have hFlast : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne))) + (hFmem x hx).1) + have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2 + have hc0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le (le_of_lt hEB) + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + have h := hbot + unfold clampNonposBottom at h + rw [hc0, hrep, tsub_zero] at h + exact_mod_cast h + obtain ⟨dbw, hkeyw, hcompw, hanchorB, hanchorE⟩ := + compatible_clamp_fillers_A2a hsort hrev hmore hne hlim hbegin hend hEB hFlast hFend hFlen + hcount + have hk_lt_all : ∀ x ∈ dbw.keys, k < x := by + intro x hx + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hk + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) + · have hk_last : k < res.keys.getLast hne := + lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk) hhead_le_last + exact lt_trans hk_last (hFlast x hx) + have hk_not_dbw : k ∉ dbw.keys := fun hx => absurd (hk_lt_all k hx) (lt_irrefl k) + refine ⟨dbw, hcompw, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not_dbw] + intro hcomp' + have hins_keys : (dbw.insert k hk_not_dbw).keys = k :: dbw.keys := by + rw [DB.insert_keys] + cases hkw : dbw.keys with + | nil => simp + | cons a t => + have hka : k ≤ a := le_of_lt (hk_lt_all a (by rw [hkw]; exact List.mem_cons_self ..)) + rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka] + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + have hrescnt : dbw.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + res.keys.length + (clampNonposD r + 1) := by + rw [hkeyw, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B + have h2 : dbF.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbF.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_lt_of_le (hFend x hx) (le_of_lt hEB) + rw [h1, h2, hFlen] + have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + res.keys.length + (clampNonposD r + 1) + 1 := by + rw [hins_keys, List.countP_cons, decide_eq_true_eq.mpr hk_lt_B, if_pos rfl, hrescnt] + have hanchor_ins : r.begin.anchor (dbw.insert k hk_not_dbw) ≤ 0 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins] + have hcast : (res.keys.length + (clampNonposD r + 1) : ℤ) ≤ (beginSlack r : ℤ) := by + exact_mod_cast hcount + push_cast + omega + have hk_ck : k ∈ RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by rw [hins_keys]; exact List.mem_cons_self .., ?_, ?_⟩ + · by_cases hneg : r.begin.anchor (dbw.insert k hk_not_dbw) < 0 + · rw [resolve_eq_nil hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + · have hzero : r.begin.anchor (dbw.insert k hk_not_dbw) = 0 := by omega + have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by omega + have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat < + (dbw.insert k hk_not_dbw).keys.length := by + rw [hzero, hins_keys, List.length_cons]; simp + rw [resolve_eq_getElem h0 hlt] + have hget : (dbw.insert k hk_not_dbw).keys[(r.begin.anchor + (dbw.insert k hk_not_dbw)).toNat] = k := by + simp only [hzero, hins_keys] + rfl + rw [hget] + · have hpre := RangeRead.compatible_keys_prefix hcomp' + have hhead_mem : res.keys.head hne ∈ + RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := + hpre.subset (List.head_mem hne) + exact lt_trans hk (RangeRead.mem_completeKeys.mp hhead_mem).2.2 + have hk_le_last : k ≤ res.keys.getLast hne := + le_trans (le_of_lt (WithTop.coe_lt_coe.mp hk)) hhead_le_last + have hk_filt : k ∈ (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [List.mem_filter] + exact ⟨hk_ck, by rw [decide_eq_true_eq]; exact hk_le_last⟩ + rw [hfilt'] at hk_filt + exact hk_not_res hk_filt + +/-- A key strictly between the reported last and `end_.key` has an erase witness in the +clamping cell: it sits among the `clampNonposD + 1` fillers, so erasing it lowers the end +resolution onto the reported last and drops the last reported key from the window. -/ +theorem exists_witness_last_end_A2aBottom {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) + (hne : res.keys ≠ []) + (hkgt : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) + (hkE : (k : WithTop Key) < r.end_.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + obtain ⟨hcell0, hEB, hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, -, hend, hclamp, hlastB, hlastE, hEne, -⟩ := hcell0 + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_k : res.keys.getLast hne < k := WithTop.coe_lt_coe.mp hkgt + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hk_not_res : k ∉ res.keys := by + intro hx + exact absurd hlast_lt_k (not_lt.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) + have hcard : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key := + intervalCard_keyAfter_getLast_ge hcomp₀ hne hend + obtain ⟨dbB, hBmem, hBk, hBlen⟩ := + exists_db_keysIn_avoid (B := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) + (E := r.end_.key) (k := k) (m := clampNonposD r) hcard + have hBgt : ∀ x ∈ dbB.keys, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt (res.keys.getLast hne))) + (hBmem x hx).1) + have hBElt : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hBmem x hx).2 + set raw : List Key := k :: dbB.keys with hraw + have hrawNodup : raw.Nodup := by + rw [hraw] + exact List.nodup_cons.mpr ⟨hBk, dbB.sorted.pairwise.nodup⟩ + set F : List Key := raw.insertionSort (· ≤ ·) with hF + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp hrawNodup) + let dbF : DB := ⟨F, hFsorted⟩ + have hFlen : dbF.keys.length = clampNonposD r + 1 := by + change F.length = clampNonposD r + 1 + rw [hF, List.length_insertionSort, hraw, List.length_cons, hBlen] + have hFlt : ∀ x ∈ dbF.keys, res.keys.getLast hne < x := by + intro x hx + have hx' := hFperm.mem_iff.mpr hx + rw [hraw] at hx' + rcases List.mem_cons.mp hx' with rfl | hxB + · exact hlast_lt_k + · exact hBgt x hxB + have hFend : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := by + intro x hx + have hx' := hFperm.mem_iff.mpr hx + rw [hraw] at hx' + rcases List.mem_cons.mp hx' with rfl | hxB + · exact hkE + · exact hBElt x hxB + have hc0 : intervalCard r.begin.key r.end_.key = 0 := + intervalCard_eq_zero_of_le (le_of_lt hEB) + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + have h := hbot + unfold clampNonposBottom at h + rw [hc0, hrep, tsub_zero] at h + exact_mod_cast h + obtain ⟨dbw, hkeyw, hcompw, hanchorB, hanchorE⟩ := + compatible_clamp_fillers_A2a hsort hrev hmore hne hlim hbegin hend hEB hFlt hFend hFlen + hcount + have hk_in_F : k ∈ dbF.keys := + hFperm.mem_iff.mp (by rw [hraw]; exact List.mem_cons_self ..) + have hk_in_dbw : k ∈ dbw.keys := by + rw [hkeyw]; exact List.mem_append.mpr (Or.inr hk_in_F) + refine ⟨dbw, hcompw, hk_in_dbw, ?_⟩ + intro hcomp' + have herase_keys : (dbw.erase k).keys = res.keys ++ dbF.keys.erase k := by + rw [DB.erase_keys, hkeyw, List.erase_append_right _ hk_not_res] + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + have hcount_erase : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + res.keys.length + clampNonposD r := by + rw [herase_keys, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_E + have hFall : dbF.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = dbF.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hFend x hx + have hktrue : (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) k = true := by + rw [decide_eq_true_eq]; exact hkE + have h2 : (dbF.keys.erase k).countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = clampNonposD r := by + rw [List.countP_erase, if_pos ⟨hk_in_F, hktrue⟩, hFall, hFlen] + omega + rw [h1, h2] + have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by + rw [clampNonposD_eq] + have hnonneg : 0 ≤ -r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnonneg]; ring + have hanchor_erase : r.end_.anchor (dbw.erase k) = (res.keys.length : ℤ) - 1 := by + cases hE : r.end_.key with + | top => exact absurd hE (ne_of_lt hEne) + | coe e => + rw [anchor_of_coe (s := r.end_) (v := e) hE] + have hce : (dbw.erase k).keys.countP (fun x => decide (x < e)) = + res.keys.length + clampNonposD r := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_erase + rw [hce, hoff] + push_cast + ring + have ht_pos : 0 < res.keys.length := List.length_pos_iff.mpr hne + have hresolve_end : resolve (dbw.erase k) r.end_ = + ((res.keys.getLast hne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanchor_erase]; omega + have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by + rw [herase_keys, List.length_append, List.length_erase, if_pos hk_in_F, hFlen] + omega + have hlt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by + rw [hanchor_erase, hlenErase] + omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_erase] + have : ((res.keys.length : ℤ) - 1).toNat = res.keys.length - 1 := by omega + exact this + have hget : (dbw.erase k).keys[(r.end_.anchor (dbw.erase k)).toNat] = + res.keys.getLast hne := by + simp only [hidx, herase_keys] + have hlen : res.keys.length - 1 < res.keys.length := by omega + rw [List.getElem_append_left (i := res.keys.length - 1) (h := hlen)] + exact (List.getLast_eq_getElem hne).symm + rw [hget] + have hanchor_eraseB : r.begin.anchor (dbw.erase k) < 0 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin)] + have hcountB : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + (dbw.erase k).keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [herase_keys] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr + ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlast_lt_B + · exact lt_of_lt_of_le (hFend x (List.mem_of_mem_erase hx)) (le_of_lt hEB) + have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by + rw [herase_keys, List.length_append, List.length_erase, if_pos hk_in_F, hFlen] + omega + rw [hcountB, hlenErase] + have hcast : (res.keys.length + clampNonposD r : ℤ) ≤ (beginSlack r : ℤ) := by + have := hcount; omega + omega + have hresolve_begin_erase : resolve (dbw.erase k) r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil hanchor_eraseB + have hlast_not_ck : res.keys.getLast hne ∉ RangeRead.completeKeys (dbw.erase k) r := by + rw [RangeRead.mem_completeKeys, hresolve_begin_erase, hresolve_end] + rintro ⟨-, -, hlt⟩ + exact lt_irrefl _ hlt + have hlast_in_filt : res.keys.getLast hne ∈ + (RangeRead.completeKeys (dbw.erase k) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [hfilt'] + exact List.getLast_mem hne + exact hlast_not_ck (List.mem_of_mem_filter hlast_in_filt) + +/-- Split `n` stored keys between a lower interval of capacity `cap1` and an upper +interval of capacity `cap2`, keeping at least `D + 1` below. -/ +theorem exists_split_below {n D : ℕ} {cap1 cap2 : ℕ∞} + (hD : D + 1 ≤ n) (hcap1 : (D + 1 : ℕ∞) ≤ cap1) + (h : (n : ℕ∞) ≤ cap1 + cap2) : + ∃ fb fa : ℕ, fb + fa = n ∧ D + 1 ≤ fb ∧ (fb : ℕ∞) ≤ cap1 ∧ (fa : ℕ∞) ≤ cap2 := by + by_cases hC1 : cap1 = ⊤ + · refine ⟨n, 0, by omega, hD, by rw [hC1]; exact le_top, bot_le⟩ + by_cases hC2 : cap2 = ⊤ + · refine ⟨D + 1, n - (D + 1), by omega, by omega, hcap1, ?_⟩ + rw [hC2]; exact le_top + obtain ⟨b, hb⟩ : ∃ b : ℕ, cap1 = (b : ℕ∞) := ⟨cap1.toNat, (ENat.natCast_toNat hC1).symm⟩ + obtain ⟨c, hc⟩ : ∃ c : ℕ, cap2 = (c : ℕ∞) := ⟨cap2.toNat, (ENat.natCast_toNat hC2).symm⟩ + rw [hb] at hcap1 + have hcap1n : D + 1 ≤ b := by exact_mod_cast hcap1 + rw [hb, hc, ← Nat.cast_add] at h + have hn : n ≤ b + c := by exact_mod_cast h + have hbc : n - c ≤ b := by rw [Nat.sub_le_iff_le_add]; omega + refine ⟨max (D + 1) (n - c), n - max (D + 1) (n - c), ?_, ?_, ?_, ?_⟩ + · omega + · omega + · rw [hb]; exact_mod_cast max_le hcap1n hbc + · rw [hc]; exact_mod_cast (show n - max (D + 1) (n - c) ≤ c by omega) + +/-- Split `n` stored keys between a lower interval of capacity `cap1` and an upper +interval of capacity `cap2`, keeping at least `D + 1` below and room for one avoided key +above. -/ +theorem exists_split_below_avoid {n D : ℕ} {cap1 cap2 : ℕ∞} + (hD : D + 1 ≤ n) (hcap1 : (D + 1 : ℕ∞) ≤ cap1) (hcap2 : (1 : ℕ∞) ≤ cap2) + (h : ((n + 1 : ℕ) : ℕ∞) ≤ cap1 + cap2) : + ∃ fb fa : ℕ, fb + fa = n ∧ D + 1 ≤ fb ∧ (fb : ℕ∞) ≤ cap1 ∧ + ((fa + 1 : ℕ) : ℕ∞) ≤ cap2 := by + by_cases hC1 : cap1 = ⊤ + · refine ⟨n, 0, by omega, hD, by rw [hC1]; exact le_top, by simpa using hcap2⟩ + by_cases hC2 : cap2 = ⊤ + · refine ⟨D + 1, n - (D + 1), by omega, by omega, hcap1, ?_⟩ + rw [hC2]; exact le_top + obtain ⟨b, hb⟩ : ∃ b : ℕ, cap1 = (b : ℕ∞) := ⟨cap1.toNat, (ENat.natCast_toNat hC1).symm⟩ + obtain ⟨c, hc⟩ : ∃ c : ℕ, cap2 = (c : ℕ∞) := ⟨cap2.toNat, (ENat.natCast_toNat hC2).symm⟩ + rw [hb] at hcap1 + rw [hc] at hcap2 + have hcap1n : D + 1 ≤ b := by exact_mod_cast hcap1 + have hcap2pos : 1 ≤ c := by exact_mod_cast hcap2 + rw [hb, hc, ← Nat.cast_add] at h + have hn : n + 1 ≤ b + c := by exact_mod_cast h + have hbc : n + 1 - c ≤ b := by rw [Nat.sub_le_iff_le_add]; omega + refine ⟨max (D + 1) (n + 1 - c), n - max (D + 1) (n + 1 - c), ?_, ?_, ?_, ?_⟩ + · omega + · omega + · rw [hb]; exact_mod_cast max_le hcap1n hbc + · rw [hc]; exact_mod_cast (show n - max (D + 1) (n + 1 - c) + 1 ≤ c by omega) + +/-- Split `n` stored keys between a lower interval of capacity `cap1` and an upper +interval of capacity `cap2`, keeping at least `D + 1` below and at least one above. -/ +theorem exists_split_below_pos {n D : ℕ} {cap1 cap2 : ℕ∞} + (hD : D + 2 ≤ n) (hcap1 : ((D + 1 : ℕ) : ℕ∞) ≤ cap1) (hcap2 : (1 : ℕ∞) ≤ cap2) + (h : (n : ℕ∞) ≤ cap1 + cap2) : + ∃ fb fa : ℕ, fb + fa = n ∧ D + 1 ≤ fb ∧ (fb : ℕ∞) ≤ cap1 ∧ 1 ≤ fa ∧ + (fa : ℕ∞) ≤ cap2 := by + by_cases hC1 : cap1 = ⊤ + · refine ⟨n - 1, 1, by omega, by omega, by rw [hC1]; exact le_top, by omega, ?_⟩ + simpa using hcap2 + by_cases hC2 : cap2 = ⊤ + · refine ⟨D + 1, n - (D + 1), by omega, by omega, hcap1, by omega, by rw [hC2]; exact le_top⟩ + obtain ⟨b, hb⟩ : ∃ b : ℕ, cap1 = (b : ℕ∞) := ⟨cap1.toNat, (ENat.natCast_toNat hC1).symm⟩ + obtain ⟨c, hc⟩ : ∃ c : ℕ, cap2 = (c : ℕ∞) := ⟨cap2.toNat, (ENat.natCast_toNat hC2).symm⟩ + rw [hb] at hcap1 + rw [hc] at hcap2 + have hcap1n : D + 1 ≤ b := by exact_mod_cast hcap1 + have hcap2pos : 1 ≤ c := by exact_mod_cast hcap2 + rw [hb, hc, ← Nat.cast_add] at h + have hn : n ≤ b + c := by exact_mod_cast h + refine ⟨max (D + 1) (n - c), n - max (D + 1) (n - c), ?_, ?_, ?_, ?_, ?_⟩ + · omega + · omega + · rw [hb]; exact_mod_cast (max_le hcap1n (by omega)) + · omega + · rw [hc]; exact_mod_cast (show n - max (D + 1) (n - c) ≤ c by omega) + +/-- A key in `[end_.key, begin.key)` has an erase witness in the clamping subcell when +the reported head is nonempty. The witness stores the empty key below the reported head, +so erasing the target drops the begin anchor from one to zero, and the empty key enters +the truncated window. -/ +theorem exists_witness_gap_ne_head_A2aBottom {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) + (hhi : negCrossTruncNonposGapA2aBottomHigh r res) + (hne : res.keys ≠ []) (hhead : res.keys.head hne ≠ []) + (hkE : r.end_.key ≤ (k : WithTop Key)) (hkB : (k : WithTop Key) < r.begin.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + obtain ⟨hcell0, hEB, hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, -⟩ := hcell0 + obtain ⟨-, hcond⟩ := hhi + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + set last : Key := res.keys.getLast hne with hlastdef + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlastdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hld : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [hlastD] + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hle_last : ∀ a ∈ res.keys, a ≤ last := by + intro a ha + have := (hsort.pairwise.imp le_of_lt).rel_getLast ha + rwa [← hlastdef] at this + have hlast_lt_B : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB + have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by rwa [hlastD] at hlastE + have hleast : leastReported r res = res.keys.head hne := by + simp [leastReported, hrev, List.head?_eq_some_head hne] + have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + rwa [hleast] at hcross + have hnil_head : ([] : Key) < res.keys.head hne := + lt_of_le_of_ne (nil_le_key _) (fun h => hhead h.symm) + have hnil_lt_head : ((([] : Key) : WithTop Key)) < ((res.keys.head hne : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr hnil_head + have hnil_lt_B : ((([] : Key) : WithTop Key)) < r.begin.key := lt_trans hnil_lt_head hhead_lt_B + have hnil_lt_E : ((([] : Key) : WithTop Key)) < r.end_.key := + lt_trans hnil_lt_head + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last _ (List.head_mem hne))) hlast_lt_E) + -- capacity condition, with `if` reduced by `hhead` + have hcond' : ((beginSlack r + 1 : ℕ) : ℕ∞) ≤ (res.keys.length : ℕ∞) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + have := hcond + rw [hheadD, if_neg hhead, Nat.add_zero, hlastD] at this + exact this + -- `t + (D+1) ≤ q` + have hBE0 : intervalCard r.begin.key r.end_.key = 0 := intervalCard_eq_zero_of_le (le_of_lt hEB) + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + have h := hbot + unfold clampNonposBottom at h + rw [hBE0, hrep, tsub_zero] at h + exact_mod_cast h + set t : ℕ := res.keys.length with ht + set Dv : ℕ := clampNonposD r with hDv + set q : ℕ := beginSlack r with hq + set Fcap : ℕ∞ := intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key with hFcap + set Gcap : ℕ∞ := intervalCard r.end_.key r.begin.key with hGcap + have hD2 : Dv + 2 ≤ q + 1 - t := by omega + have hFcapge : ((Dv + 1 : ℕ) : ℕ∞) ≤ Fcap := by + have := intervalCard_keyAfter_getLast_ge hcomp₀ hne hend + rw [← hDv, ← hlastdef] at this + exact this + have hGcapge : (1 : ℕ∞) ≤ Gcap := one_le_intervalCard_of_le_lt hkE hkB + have hcadd : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = Fcap + Gcap := by + have hkaE : ((keyAfter last : Key) : WithTop Key) ≤ r.end_.key := keyAfter_le_of_lt hlast_lt_E + have h := intervalCard_add (b := ((keyAfter last : Key) : WithTop Key)) (m := r.end_.key) + (e := r.begin.key) hkaE (le_of_lt hEB) + rw [← hFcap, ← hGcap] at h + exact h + have hn_le : ((q + 1 - t : ℕ) : ℕ∞) ≤ Fcap + Gcap := by + have hq1 : t ≤ q + 1 := by omega + have hmain : (q + 1 : ℕ∞) ≤ (t : ℕ∞) + (Fcap + Gcap) := by + have := hcond' + rwa [ht, hq, hcadd] at this + have hnsub : ((q + 1 - t : ℕ) : ℕ∞) = (q + 1 : ℕ∞) - (t : ℕ∞) := + ENat.natCast_sub (q + 1) t + rw [hnsub, tsub_le_iff_left] + simpa [add_comm] using hmain + -- split the required extra keys between fillers and gap keys + obtain ⟨fb, fa, hfb_fa, hfbD, hfbF, hfa1, hfaG⟩ := + exists_split_below_pos (n := q + 1 - t) (D := Dv) (cap1 := Fcap) (cap2 := Gcap) + hD2 hFcapge hGcapge hn_le + -- filler keys in `(last, end_.key)` + obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn + (B := ((keyAfter last : Key) : WithTop Key)) (E := r.end_.key) (m := fb) hfbF + have hFlt_last : ∀ x ∈ dbF.keys, last < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (hFmem x hx).1) + have hFlt_E : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2 + -- gap keys in `[end_.key, begin.key)` including `k` + have hfacap : (((fa - 1 + 1 : ℕ)) : ℕ∞) ≤ Gcap := by + rw [Nat.sub_add_cancel hfa1]; exact hfaG + obtain ⟨dbB, hBmem, hBk, hBlen⟩ := exists_db_keysIn_avoid + (B := r.end_.key) (E := r.begin.key) (k := k) (m := fa - 1) hfacap + have hGge_E : ∀ x ∈ dbB.keys, r.end_.key ≤ (x : WithTop Key) := fun x hx => (hBmem x hx).1 + have hGlt_B : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.begin.key := fun x hx => (hBmem x hx).2 + let dbG : DB := dbB.insert k hBk + have hGlen : dbG.keys.length = fa := by + rw [DB.insert_keys, List.orderedInsert_length, hBlen]; omega + have hGmem : ∀ x ∈ dbG.keys, r.end_.key ≤ (x : WithTop Key) ∧ (x : WithTop Key) < r.begin.key := by + intro x hx + rw [DB.mem_insert_iff hBk] at hx + rcases hx with rfl | hx + · exact ⟨hkE, hkB⟩ + · exact ⟨hGge_E x hx, hGlt_B x hx⟩ + have hkG : k ∈ dbG.keys := by rw [DB.mem_insert_iff hBk]; exact Or.inl rfl + have hGlast : ∀ b ∈ dbG.keys, last < b := fun b hb => + WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E (hGmem b hb).1) + -- assemble the witness database + have hres_lt_F : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys, a < b := by + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hFlt_last b hb) + have hres_lt_G : ∀ a ∈ res.keys, ∀ b ∈ dbG.keys, a < b := by + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hGlast b hb) + have hres_lt_FG : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys ++ dbG.keys, a < b := by + intro a ha b hb + rcases List.mem_append.mp hb with hb | hb + · exact hres_lt_F a ha b hb + · exact hres_lt_G a ha b hb + have hF_lt_G : ∀ a ∈ dbF.keys, ∀ b ∈ dbG.keys, a < b := by + intro a ha b hb + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hFlt_E a ha) (hGmem b hb).1) + have hFG_sorted : (dbF.keys ++ dbG.keys).SortedLT := + List.sortedLT_append.mpr ⟨dbF.sorted, dbG.sorted, hF_lt_G⟩ + have hX_sorted : (res.keys ++ (dbF.keys ++ dbG.keys)).SortedLT := + List.sortedLT_append.mpr ⟨hsort, hFG_sorted, hres_lt_FG⟩ + have hnilX : ∀ b ∈ res.keys ++ (dbF.keys ++ dbG.keys), ([] : Key) < b := by + intro b hb + rcases List.mem_append.mp hb with hb | hb + · exact lt_of_lt_of_le hnil_head + (hsort.pairwise.imp le_of_lt |>.rel_head hb) + · rcases List.mem_append.mp hb with hb | hb + · exact lt_trans hnil_head (lt_of_le_of_lt (hle_last _ (List.head_mem hne)) (hFlt_last b hb)) + · exact lt_trans hnil_head (lt_of_le_of_lt (hle_last _ (List.head_mem hne)) (hGlast b hb)) + have hD_sorted : (([] : Key) :: (res.keys ++ (dbF.keys ++ dbG.keys))).SortedLT := + List.sortedLT_cons.mpr ⟨hnilX, hX_sorted⟩ + let dbw : DB := ⟨([] : Key) :: (res.keys ++ (dbF.keys ++ dbG.keys)), hD_sorted⟩ + have hDkeys : dbw.keys = ([] : Key) :: (res.keys ++ (dbF.keys ++ dbG.keys)) := rfl + have hDlen : dbw.keys.length = q + 2 := by + rw [hDkeys, List.length_cons, List.length_append, List.length_append, hFlen, hGlen, ← ht] + omega + have hcountAll : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [hDkeys] at hx + simp only [List.mem_cons] at hx + rcases hx with rfl | hx + · exact hnil_lt_B + · rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B + · rcases List.mem_append.mp hx with hx | hx + · exact lt_of_lt_of_le (hFlt_E x hx) (le_of_lt hEB) + · exact (hGmem x hx).2 + have hanchorB : r.begin.anchor dbw = 1 := by + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountAll, hDlen, ← hq] + push_cast; ring + have hresolveB : resolve dbw r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor dbw := by rw [hanchorB]; norm_num + have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by rw [hanchorB, hDlen]; norm_num + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor dbw).toNat = 1 := by rw [hanchorB]; rfl + simp only [hidx] + have hget : dbw.keys[1]'(by rw [hDlen]; norm_num) = res.keys.head hne := by + rw [List.getElem_of_eq hDkeys] + rw [List.getElem_cons_succ] + rw [List.getElem_append_left (i := 0) (h := List.length_pos_iff.mpr hne)] + exact (List.head_eq_getElem_zero hne).symm + exact congrArg (fun x : Key => (x : WithTop Key)) hget + -- the end resolution lies above the reported last + have hcountE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + 1 + t + fb := by + rw [hDkeys, List.countP_cons_of_pos (by rw [decide_eq_true_eq]; exact hnil_lt_E), + List.countP_append, List.countP_append] + have h1 : res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = t := by + rw [ht, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E + have h2 : dbF.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = fb := by + rw [← hFlen, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hFlt_E x hx + have h3 : dbG.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hGmem x hx).1 + rw [h1, h2, h3]; omega + have hanchorE : r.end_.anchor dbw = (t + fb : ℤ) - Dv := by + unfold Selector.anchor + rw [hcountE] + have hoff : r.end_.offset = -(Dv : ℤ) := by + rw [hDv, clampNonposD_eq] + have hnn : 0 ≤ -r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnn]; ring + rw [hoff]; push_cast; ring + have hanchorE_nonneg : 0 ≤ r.end_.anchor dbw := by + rw [hanchorE] + have : (Dv : ℤ) ≤ t + fb := by + have : Dv ≤ fb := by omega + omega + omega + have hidxE : (r.end_.anchor dbw).toNat = t + fb - Dv := by + rw [hanchorE] + have : ((t + fb : ℤ) - Dv).toNat = t + fb - Dv := by omega + exact this + have hidxE_lt : (r.end_.anchor dbw).toNat < dbw.keys.length := by + rw [hidxE, hDlen] + have : Dv ≤ fb := by omega + omega + have hlast_eq_D : dbw.keys[t]'(by rw [hDlen]; omega) = last := by + rw [List.getElem_of_eq hDkeys, List.getElem_cons] + rw [dif_neg (by have : 0 < t := List.length_pos_iff.mpr hne; omega)] + rw [List.getElem_append_left (i := t - 1) (h := by + have : 0 < t := List.length_pos_iff.mpr hne + omega)] + rw [hlastdef, List.getLast_eq_getElem hne] + have hresolveE_gt_last : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [resolve_eq_getElem hanchorE_nonneg hidxE_lt] + rw [← hlast_eq_D] + have hlt : t < (r.end_.anchor dbw).toNat := by + rw [hidxE] + have : Dv ≤ fb := by omega + omega + exact WithTop.coe_lt_coe.mpr + (dbw.sorted.getElem_lt_getElem_of_lt (i := t) (j := (r.end_.anchor dbw).toNat) + (hi := by rw [hDlen]; omega) (hj := hidxE_lt) hlt) + -- the stored keys in `[head, last]` are the reported keys + have hfilt_between : dbw.keys.filter (fun x : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) ≤ ((last : Key) : WithTop Key))) = res.keys := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (dbw.sorted.pairwise.filter _) + · exact hsort + · intro x + rw [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hx, hge, hle⟩ + rw [hDkeys] at hx + simp only [List.mem_cons] at hx + rcases hx with rfl | hx + · exact absurd hge (not_le.mpr hnil_lt_head) + · rcases List.mem_append.mp hx with hx | hx + · exact hx + · rcases List.mem_append.mp hx with hx | hx + · exact absurd hle (not_le.mpr (WithTop.coe_lt_coe.mpr (hFlt_last x hx))) + · exact absurd hle (not_le.mpr (lt_of_lt_of_le hlast_lt_E (hGmem x hx).1)) + · intro hx + refine ⟨?_, ?_, WithTop.coe_le_coe.mpr (hle_last x hx)⟩ + · rw [hDkeys]; exact List.mem_cons_of_mem _ (List.mem_append_left _ hx) + · exact WithTop.coe_le_coe.mpr (hsort.pairwise.imp le_of_lt |>.rel_head hx) + -- compatibility + have hfilt_eq : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) = res.keys := by + apply List.SortedLT.eq_of_mem_iff + · rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])] + exact List.sortedLT_iff_pairwise.mpr + ((RangeRead.inRange_pairwise dbw _ _).filter _) + · exact hsort + · intro x + rw [List.mem_filter, RangeRead.mem_completeKeys, hresolveB, hlastD, decide_eq_true_eq] + constructor + · rintro ⟨⟨hx, hge, -⟩, hle⟩ + have hb : x ∈ dbw.keys.filter (fun y : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (y : WithTop Key) ∧ + (y : WithTop Key) ≤ ((last : Key) : WithTop Key))) := by + rw [List.mem_filter, decide_eq_true_eq] + exact ⟨hx, hge, WithTop.coe_le_coe.mpr hle⟩ + rwa [hfilt_between] at hb + · intro hxres + have hb := hfilt_between ▸ hxres + rw [List.mem_filter, decide_eq_true_eq] at hb + obtain ⟨hx, hge, hleW⟩ := hb + exact ⟨⟨hx, hge, lt_of_le_of_lt hleW hresolveE_gt_last⟩, WithTop.coe_le_coe.mp hleW⟩ + refine ⟨dbw, ?_, ?_, ?_⟩ + · rw [compatible_iff_truncated_keys hrev hmore hne] + refine ⟨hlim, ?_⟩ + rw [← hld] + exact hfilt_eq + · rw [hDkeys]; exact List.mem_cons_of_mem _ (List.mem_append_right _ (List.mem_append_right _ hkG)) + · intro hcomp' + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + have hknil : k ≠ ([] : Key) := + ne_of_gt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hnil_lt_E hkE)) + have hk_not_res : k ∉ res.keys := by + intro hk + exact absurd hkE (not_le.mpr + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk)) hlast_lt_E)) + have hk_not_F : k ∉ dbF.keys := by + intro hk + exact absurd (hFlt_E k hk) (not_lt.mpr hkE) + have herase_keys : (dbw.erase k).keys = + ([] : Key) :: (res.keys ++ (dbF.keys ++ dbB.keys)) := by + rw [DB.erase_keys, hDkeys] + rw [List.erase_cons_tail (by simp only [beq_iff_eq]; exact fun h => hknil h.symm)] + congr 1 + rw [List.erase_append_right _ hk_not_res] + congr 1 + rw [List.erase_append_right _ hk_not_F] + congr 1 + have h := DB.erase_insert_keys dbB k hBk + simpa [dbG] using h + have hlenErase : (dbw.erase k).keys.length = q + 1 := by + rw [herase_keys, List.length_cons, List.length_append, List.length_append, hFlen, + hBlen, ← ht] + omega + have hcountB' : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = q + 1 := by + rw [← hlenErase, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [herase_keys] at hx + simp only [List.mem_cons] at hx + rcases hx with rfl | hx + · exact hnil_lt_B + · rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B + · rcases List.mem_append.mp hx with hx | hx + · exact lt_of_lt_of_le (hFlt_E x hx) (le_of_lt hEB) + · exact hGlt_B x hx + have hanchorB' : r.begin.anchor (dbw.erase k) = 0 := by + rw [anchor_eq_count_sub_slack (dbw.erase k) r (le_of_lt hbegin), hcountB', ← hq] + push_cast; ring + have hresolveB' : resolve (dbw.erase k) r.begin = (([] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor (dbw.erase k) := by rw [hanchorB'] + have hlt : (r.begin.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by + rw [hanchorB', hlenErase]; omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor (dbw.erase k)).toNat = 0 := by rw [hanchorB']; rfl + simp only [hidx, herase_keys, List.getElem_cons_zero] + have hfiltEnd : (dbw.erase k).keys.filter + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + dbw.keys.filter (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [DB.erase_keys, ← List.erase_filter] + exact List.erase_eq_self_iff.mpr (by + intro hk + rw [List.mem_filter] at hk + exact absurd (of_decide_eq_true hk.2) (not_lt.mpr hkE)) + have hresolveE' : resolve (dbw.erase k) r.end_ = resolve dbw r.end_ := + resolve_eq_of_keys_below (db₁ := dbw.erase k) (db₂ := dbw) (E := r.end_.key) + (s := r.end_) hend (le_refl _) hfiltEnd + have hnil_ck : ([] : Key) ∈ RangeRead.completeKeys (dbw.erase k) r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by rw [herase_keys]; exact List.mem_cons_self .., ?_, ?_⟩ + · rw [hresolveB'] + · rw [hresolveE'] + exact lt_of_le_of_lt (coe_nil_le_key_withTop last) hresolveE_gt_last + have hnil_filt : ([] : Key) ∈ (RangeRead.completeKeys (dbw.erase k) r).filter + (fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) := by + rw [List.mem_filter] + refine ⟨hnil_ck, ?_⟩ + rw [decide_eq_true_eq, hlastD] + exact nil_le_key last + rw [hld] at hnil_filt + rw [hfilt'] at hnil_filt + have hnil_notin : ([] : Key) ∉ res.keys := by + intro hk + have hle : res.keys.head hne ≤ [] := (hsort.pairwise.imp le_of_lt).rel_head hk + exact absurd (lt_of_lt_of_le hnil_head hle) (lt_irrefl _) + exact hnil_notin hnil_filt + +/-- A key in `[end_.key, begin.key)` has an insert witness in the clamping subcell when +the reported head is the empty key. The witness stores exactly `beginSlack` keys below the +begin key, so the begin resolution is the empty key; inserting the target raises the begin +anchor from zero to one, and the empty key leaves the truncated window. -/ +theorem exists_witness_gap_eq_head_A2aBottom {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) + (hhi : negCrossTruncNonposGapA2aBottomHigh r res) + (hne : res.keys ≠ []) (hhead : res.keys.head hne = []) + (hkE : r.end_.key ≤ (k : WithTop Key)) (hkB : (k : WithTop Key) < r.begin.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (toggle dbw k) r res := by + obtain ⟨hcell0, hEB, hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, -⟩ := hcell0 + obtain ⟨-, hcond⟩ := hhi + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + set last : Key := res.keys.getLast hne with hlastdef + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlastdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hld : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [hlastD] + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hle_last : ∀ a ∈ res.keys, a ≤ last := by + intro a ha + have := (hsort.pairwise.imp le_of_lt).rel_getLast ha + rwa [← hlastdef] at this + have hlast_lt_B : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB + have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by rwa [hlastD] at hlastE + have hnil_lt_E : ((([] : Key) : WithTop Key)) < r.end_.key := by + have hle : res.keys.head hne ≤ last := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + rw [hhead] at hle + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_lt_E + -- capacity condition, with `if` reduced by `hhead` + have hcond' : (((beginSlack r + 1 + 1 : ℕ)) : ℕ∞) ≤ (res.keys.length : ℕ∞) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + have := hcond + rw [hheadD, hhead, hlastD] at this + simpa using this + -- `t + (D+1) ≤ q` + have hBE0 : intervalCard r.begin.key r.end_.key = 0 := intervalCard_eq_zero_of_le (le_of_lt hEB) + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + have hcount : res.keys.length + (clampNonposD r + 1) ≤ beginSlack r := by + have h := hbot + unfold clampNonposBottom at h + rw [hBE0, hrep, tsub_zero] at h + exact_mod_cast h + set t : ℕ := res.keys.length with ht + set Dv : ℕ := clampNonposD r with hDv + set q : ℕ := beginSlack r with hq + set Fcap : ℕ∞ := intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key with hFcap + set Gcap : ℕ∞ := intervalCard r.end_.key r.begin.key with hGcap + have hD2 : Dv + 1 ≤ q + 1 - t := by omega + have hFcapge : ((Dv + 1 : ℕ) : ℕ∞) ≤ Fcap := by + have := intervalCard_keyAfter_getLast_ge hcomp₀ hne hend + rw [← hDv, ← hlastdef] at this + exact this + have hGcapge : (1 : ℕ∞) ≤ Gcap := one_le_intervalCard_of_le_lt hkE hkB + have hcadd : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = Fcap + Gcap := by + have hkaE : ((keyAfter last : Key) : WithTop Key) ≤ r.end_.key := keyAfter_le_of_lt hlast_lt_E + have h := intervalCard_add (b := ((keyAfter last : Key) : WithTop Key)) (m := r.end_.key) + (e := r.begin.key) hkaE (le_of_lt hEB) + rw [← hFcap, ← hGcap] at h + exact h + have hmain : ((q + 2 : ℕ) : ℕ∞) ≤ (t : ℕ∞) + (Fcap + Gcap) := by + have := hcond' + rw [hcadd] at this + have hcast : ((q + 1 + 1 : ℕ) : ℕ∞) = ((q + 2 : ℕ) : ℕ∞) := rfl + rw [hcast] at this + exact this + have hn_le : ((q + 1 - t + 1 : ℕ) : ℕ∞) ≤ Fcap + Gcap := by + have hsub : ((q + 1 - t + 1 : ℕ) : ℕ∞) = ((q + 2 : ℕ) : ℕ∞) - (t : ℕ∞) := by + rw [show q + 1 - t + 1 = q + 2 - t by omega] + exact ENat.natCast_sub (q + 2) t + rw [hsub, tsub_le_iff_left] + simpa using hmain + -- split the extra keys between fillers and gap keys, with room for the avoided target + obtain ⟨fb, fa, hfb_fa, hfbD, hfbF, hfaG⟩ := + exists_split_below_avoid (n := q + 1 - t) (D := Dv) (cap1 := Fcap) (cap2 := Gcap) + hD2 hFcapge hGcapge hn_le + -- filler keys in `(last, end_.key)` + obtain ⟨dbF, hFmem, hFlen⟩ := exists_db_keysIn + (B := ((keyAfter last : Key) : WithTop Key)) (E := r.end_.key) (m := fb) hfbF + have hFlt_last : ∀ x ∈ dbF.keys, last < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (hFmem x hx).1) + have hFlt_E : ∀ x ∈ dbF.keys, (x : WithTop Key) < r.end_.key := fun x hx => (hFmem x hx).2 + -- gap keys in `[end_.key, begin.key)` avoiding the target + obtain ⟨dbB, hBmem, hBk, hBlen⟩ := exists_db_keysIn_avoid + (B := r.end_.key) (E := r.begin.key) (k := k) (m := fa) hfaG + have hGge_E : ∀ x ∈ dbB.keys, r.end_.key ≤ (x : WithTop Key) := fun x hx => (hBmem x hx).1 + have hGlt_B : ∀ x ∈ dbB.keys, (x : WithTop Key) < r.begin.key := fun x hx => (hBmem x hx).2 + -- assemble the witness database + have hres_lt_F : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys, a < b := by + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hFlt_last b hb) + have hres_lt_G : ∀ a ∈ res.keys, ∀ b ∈ dbB.keys, a < b := by + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) + (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E (hGge_E b hb))) + have hres_lt_FG : ∀ a ∈ res.keys, ∀ b ∈ dbF.keys ++ dbB.keys, a < b := by + intro a ha b hb + rcases List.mem_append.mp hb with hb | hb + · exact hres_lt_F a ha b hb + · exact hres_lt_G a ha b hb + have hF_lt_G : ∀ a ∈ dbF.keys, ∀ b ∈ dbB.keys, a < b := by + intro a ha b hb + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hFlt_E a ha) (hGge_E b hb)) + have hFG_sorted : (dbF.keys ++ dbB.keys).SortedLT := + List.sortedLT_append.mpr ⟨dbF.sorted, dbB.sorted, hF_lt_G⟩ + have hX_sorted : (res.keys ++ dbF.keys ++ dbB.keys).SortedLT := by + rw [List.append_assoc] + exact List.sortedLT_append.mpr ⟨hsort, hFG_sorted, hres_lt_FG⟩ + let dbw : DB := ⟨res.keys ++ dbF.keys ++ dbB.keys, hX_sorted⟩ + have hDkeys : dbw.keys = res.keys ++ dbF.keys ++ dbB.keys := rfl + have hDlen : dbw.keys.length = q + 1 := by + rw [hDkeys, List.length_append, List.length_append, hFlen, hBlen, ← ht] + omega + have hcountAll : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [hDkeys] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B + · exact lt_of_lt_of_le (hFlt_E x hx) (le_of_lt hEB) + · exact hGlt_B x hx + have hanchorB : r.begin.anchor dbw = 0 := by + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountAll, hDlen, ← hq] + push_cast; ring + have hresolveB : resolve dbw r.begin = (([] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor dbw := by rw [hanchorB] + have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by + rw [hanchorB, hDlen]; omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor dbw).toNat = 0 := by rw [hanchorB]; rfl + simp only [hidx] + have hget : dbw.keys[0]'(by rw [hDlen]; omega) = ([] : Key) := by + rw [List.getElem_of_eq hDkeys] + rw [List.getElem_append_left (i := 0) (h := by + rw [List.length_append]; omega)] + rw [List.getElem_append_left (i := 0) (h := List.length_pos_iff.mpr hne)] + exact (List.head_eq_getElem_zero hne).symm.trans hhead + exact congrArg (fun x : Key => (x : WithTop Key)) hget + -- the end resolution lies above the reported last + have hcountE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + t + fb := by + rw [hDkeys, List.countP_append, List.countP_append] + have h1 : res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = t := by + rw [ht, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E + have h2 : dbF.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = fb := by + rw [← hFlen, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hFlt_E x hx + have h3 : dbB.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hGge_E x hx) + rw [h1, h2, h3]; omega + have hanchorE : r.end_.anchor dbw = (t + fb : ℤ) - Dv - 1 := by + unfold Selector.anchor + rw [hcountE] + have hoff : r.end_.offset = -(Dv : ℤ) := by + rw [hDv, clampNonposD_eq] + have hnn : 0 ≤ -r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnn]; ring + rw [hoff]; push_cast; ring + have hanchorE_nonneg : 0 ≤ r.end_.anchor dbw := by + rw [hanchorE] + have : Dv + 1 ≤ fb := hfbD + omega + have hidxE : (r.end_.anchor dbw).toNat = t + fb - Dv - 1 := by + rw [hanchorE] + have : ((t + fb : ℤ) - Dv - 1).toNat = t + fb - Dv - 1 := by omega + exact this + have hidxE_lt : (r.end_.anchor dbw).toNat < dbw.keys.length := by + rw [hidxE, hDlen] + have : Dv + 1 ≤ fb := hfbD + omega + have ht_pos : 0 < t := by rw [ht]; exact List.length_pos_iff.mpr hne + have hlast_eq_D : dbw.keys[t - 1]'(by rw [hDlen]; omega) = last := by + rw [List.getElem_of_eq hDkeys] + rw [List.getElem_append_left (i := t - 1) (h := by + rw [List.length_append]; omega)] + rw [List.getElem_append_left (i := t - 1) (h := by omega)] + rw [hlastdef, List.getLast_eq_getElem hne] + have hresolveE_gt_last : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [resolve_eq_getElem hanchorE_nonneg hidxE_lt] + rw [← hlast_eq_D] + have hlt : t - 1 < (r.end_.anchor dbw).toNat := by + rw [hidxE] + have : Dv + 1 ≤ fb := hfbD + omega + exact WithTop.coe_lt_coe.mpr + (dbw.sorted.getElem_lt_getElem_of_lt (i := t - 1) (j := (r.end_.anchor dbw).toNat) + (hi := by rw [hDlen]; omega) (hj := hidxE_lt) hlt) + -- the stored keys below the end resolution are the reported keys + have hfilt_eq : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) = res.keys := by + apply List.SortedLT.eq_of_mem_iff + · rw [RangeRead.completeKeys, ite_eq_right (by simp [show r.reverse = false from hrev])] + exact List.sortedLT_iff_pairwise.mpr + ((RangeRead.inRange_pairwise dbw _ _).filter _) + · exact hsort + · intro x + rw [List.mem_filter, RangeRead.mem_completeKeys, hresolveB, hlastD, decide_eq_true_eq] + constructor + · rintro ⟨⟨hx, -, -⟩, hle⟩ + rw [hDkeys] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact hx + · exact absurd hle (not_le.mpr (hFlt_last x hx)) + · exact absurd hle (not_le.mpr + (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_E (hGge_E x hx)))) + · intro hx + refine ⟨⟨?_, ?_, ?_⟩, hle_last x hx⟩ + · rw [hDkeys]; exact List.mem_append_left _ (List.mem_append_left _ hx) + · exact coe_nil_le_key_withTop (x : WithTop Key) + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hresolveE_gt_last + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + refine ⟨hlim, ?_⟩ + rw [← hld] + exact hfilt_eq + -- the target is not among the stored keys + have hk_not_res : k ∉ res.keys := by + intro hk + exact absurd hkE (not_le.mpr + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last k hk)) hlast_lt_E)) + have hk_not_F : k ∉ dbF.keys := by + intro hk + exact absurd (hFlt_E k hk) (not_lt.mpr hkE) + have hk_not_dbw : k ∉ dbw.keys := by + rw [hDkeys] + intro hk + rcases List.mem_append.mp hk with hk | hk + · rcases List.mem_append.mp hk with hk | hk + · exact hk_not_res hk + · exact hk_not_F hk + · exact hBk hk + refine ⟨dbw, hcompw, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not_dbw] + intro hcomp' + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + -- the inserted database has one more key below the begin key + have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = q + 2 := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx dbw.sorted hk_not_dbw] + rw [countP_insertIdx_of_lt (l := dbw.keys) (x := k) (v := r.begin.key) dbw.sorted hkB, + hcountAll, hDlen] + have hanchorB_ins : r.begin.anchor (dbw.insert k hk_not_dbw) = 1 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins, ← hq] + push_cast; ring + -- the empty key leaves the begin resolution + have hnil_mem : ([] : Key) ∈ dbw.keys := by + rw [hDkeys] + exact List.mem_append_left _ (List.mem_append_left _ ((hhead ▸ List.head_mem hne : _))) + have hins_len : (dbw.insert k hk_not_dbw).keys.length = q + 2 := by + rw [DB.insert_keys, List.orderedInsert_length, hDlen] + have hnil_lt_one : ((([] : Key) : WithTop Key)) < + (((dbw.insert k hk_not_dbw).keys[1]'(by rw [hins_len]; omega) : Key) : WithTop Key) := by + have hlt := (dbw.insert k hk_not_dbw).sorted.getElem_lt_getElem_of_lt (i := 0) (j := 1) + (hi := by rw [hins_len]; omega) (hj := by rw [hins_len]; omega) + (by omega) + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (nil_le_key _)) + (WithTop.coe_lt_coe.mpr hlt) + have hresolveB_ins : ((([] : Key) : WithTop Key)) < + resolve (dbw.insert k hk_not_dbw) r.begin := by + have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by rw [hanchorB_ins]; norm_num + have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat < + (dbw.insert k hk_not_dbw).keys.length := by + rw [hanchorB_ins, hins_len]; omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat = 1 := by + rw [hanchorB_ins]; rfl + simp only [hidx] + exact hnil_lt_one + -- the empty key is reported, so it must survive the truncated filter + have hnil_res : ([] : Key) ∈ res.keys := hhead ▸ List.head_mem hne + have hnil_filt : ([] : Key) ∈ (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter + (fun x : Key => decide (x ≤ res.keys.getLastD ([] : Key))) := by + rw [hld, hfilt']; exact hnil_res + rw [List.mem_filter] at hnil_filt + have hge := (RangeRead.mem_completeKeys.mp hnil_filt.1).2.1 + exact absurd hresolveB_ins (not_lt.mpr hge) + +/-- In the crossed nonpositive-end gap cell a clamping begin selector leaves the stored +keys below the end key equal to the reported keys together with the stored keys in +`(last, end_.key)`. -/ +theorem cnt_end_eq_of_clamp_A2aBottom {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hend : r.end_.offset ≤ 0) (hL : resolve db r.begin = (([] : Key) : WithTop Key)) : + cnt db r.end_.key = res.keys.length + + (between db (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.end_.key).length := by + classical + set last : Key := res.keys.getLast hne with hlast + have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne + have hcmpEnd : resolve db r.end_ ≤ r.end_.key := + resolve_le_key_of_offset_nonpos (db := db) (s := r.end_) hend + have hwin : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) := by + rw [completeKeys_eq_filter_of_end_nonpos hrev hend, hL, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases h1 : (((k : Key) : WithTop Key) < resolve db r.end_) + · have h2 : (((k : Key) : WithTop Key) < r.end_.key) := lt_of_lt_of_le h1 hcmpEnd + have h3 : ((([] : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := + coe_nil_le_key_withTop _ + simp [h1, h2, h3] + · simp [h1] + have hfil : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hlast] + exact ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2 + have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := by + have hmem : last ∈ (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) := by + rw [hfil]; exact hlast_mem + exact (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hmem).1).2.2 + have hka_le_re : (((keyAfter last : Key)) : WithTop Key) ≤ resolve db r.end_ := + keyAfter_le_of_lt hlast_lt_re + have hcntKa : cnt db (((keyAfter last : Key)) : WithTop Key) = res.keys.length := by + rw [cnt, List.countP_eq_length_filter] + have hl : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < + (((keyAfter last : Key)) : WithTop Key))) = + (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) := by + rw [hwin, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases h1 : (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key)) + · have hkle : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h1) + have h2 : (((k : Key) : WithTop Key) < resolve db r.end_) := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re + simp [h1, h2, hkle] + · have h2 : ¬ ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := by + rintro ⟨hkre, hkle⟩ + exact h1 (WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle)) + by_cases hA : (((k : Key) : WithTop Key) < resolve db r.end_) + · have hB : ¬ k ≤ last := fun hB => h2 ⟨hA, hB⟩ + simp [h1, hA, hB] + · simp [h1, hA] + rw [hl, hfil] + have hkaE : (((keyAfter last : Key)) : WithTop Key) ≤ r.end_.key := + keyAfter_le_of_lt (lt_of_lt_of_le hlast_lt_re hcmpEnd) + have hsplit := cnt_eq_add_between db hkaE + rw [hcntKa] at hsplit + exact hsplit + +/-- **Gap bound at a nonnegative begin anchor.** In the crossed nonpositive-end gap +cell, a compatible database with nonnegative begin anchor resolves the begin selector +onto the reported head, and the slack is bounded by the reported count plus the capacity +of the gap above the reported last. -/ +theorem gap_slack_bound_A2aBottom {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hanc : 0 ≤ r.begin.anchor db) : + ((beginSlack r + 1 : ℕ) : ℕ∞) ≤ (res.keys.length : ℕ∞) + + intervalCard (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) r.begin.key := by + classical + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB' : ((last : Key) : WithTop Key) < r.begin.key := by rwa [hlastD] at hlastB + have hpre := RangeRead.compatible_keys_prefix hcomp + have hckne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne + obtain ⟨a, _ha_mem, ha_res, ha_head⟩ := + RangeRead.completeKeys_head_resolve hrev hckne hanc + have hhead_eq_a : res.keys.head hne = a := by + rw [← ha_head]; exact hpre.head hne + have hres : resolve db r.begin = ((head : Key) : WithTop Key) := by + rw [hhead, hhead_eq_a]; exact ha_res + have hhead_le_last : head ≤ last := by + rw [hhead, hlast] + exact (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_getLast + (List.head_mem hne) + have hheadB : ((head : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlastB' + -- the gap count is the slack + have hgap : db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = beginSlack r + 1 := + gap_count_eq_beginSlack_of_resolve_head hbegin hheadB hanc hres + have hresEnd_gt : ((last : Key) : WithTop Key) < resolve db r.end_ := by + have hlast_ck : last ∈ RangeRead.completeKeys db r := hpre.subset (List.getLast_mem hne) + exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hfilterComp : (RangeRead.completeKeys db r).filter + (fun k => decide (k ≤ last)) = res.keys := + ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp).2 + have hfilterBegin : (RangeRead.completeKeys db r).filter + (fun k => decide (k ≤ last)) = + db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin hrev hres hresEnd_gt + have hlen_head_last : (db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last))).length + = res.keys.length := by + rw [← hfilterBegin, hfilterComp] + have hfilter_ka : db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + db.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + by_cases hk : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · simp only [hk, true_and] + exact WithTop.coe_lt_coe.trans (lt_keyAfter_iff k last) + · simp only [hk, false_and] + have hlen_ka : (between db ((head : Key) : WithTop Key) + (((keyAfter last : Key) : WithTop Key))).length = res.keys.length := by + unfold between + rw [hfilter_ka, hlen_head_last] + have hcntB : cnt db r.begin.key = + cnt db ((head : Key) : WithTop Key) + + db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) := by + rw [cnt_eq_add_between db (le_of_lt hheadB), between, List.countP_eq_length_filter] + have hcntKa : cnt db (((keyAfter last : Key)) : WithTop Key) = + cnt db ((head : Key) : WithTop Key) + + (between db ((head : Key) : WithTop Key) + (((keyAfter last : Key)) : WithTop Key)).length := + cnt_eq_add_between db (WithTop.coe_le_coe.mpr + (le_trans hhead_le_last (le_of_lt (keyAfter_self_lt last)))) + have hcntKB : cnt db r.begin.key = + cnt db (((keyAfter last : Key)) : WithTop Key) + + (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := + cnt_eq_add_between db (keyAfter_le_of_lt hlastB') + have hkey : db.keys.countP (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key)) = res.keys.length + + (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := by + rw [hcntKa, hlen_ka] at hcntKB + omega + have hkeyNat : beginSlack r + 1 = + res.keys.length + + (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := by + rw [← hgap, hkey] + have hlen_le : (((between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := + between_length_le_intervalCard (keyAfter_le_of_lt hlastB') + calc ((beginSlack r + 1 : ℕ) : ℕ∞) + = ((res.keys.length + + (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length : ℕ) : ℕ∞) := by + rw [hkeyNat] + _ = (res.keys.length : ℕ∞) + + (((between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length : ℕ) : ℕ∞) := by + rw [Nat.cast_add] + _ ≤ (res.keys.length : ℕ∞) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := + add_le_add (le_refl (res.keys.length : ℕ∞)) hlen_le + +/-- **Every compatible database clamps in the low branch.** In the crossed +nonpositive-end gap cell with a clamping begin selector, if the gap-capacity condition +fails then every compatible database resolves its begin selector to `⊥`. A nonnegative +anchor would force the gap count up to `beginSlack + 1`, hence the empty reported head, +which resolves to `⊥` anyway. -/ +theorem resolve_begin_eq_nil_of_low_A2aBottom {r : RangeRead} {res : Result} {db : DB} + (hcell : negCrossTruncNonposGapA2aBottom r res) + (hlow : ¬ negCrossTruncNonposGapA2aBottomHigh r res) + (hcomp : RangeRead.Compatible db r res) : + resolve db r.begin = (([] : Key) : WithTop Key) := by + classical + have hcellKeep := hcell + obtain ⟨hcell0, hEB, _hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hlastD' : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hnotRaw := hlow + unfold negCrossTruncNonposGapA2aBottomHigh at hnotRaw + have hnot' : ¬ (((beginSlack r + 1 + + (if res.keys.headD ([] : Key) = [] then 1 else 0) : ℕ) : ℕ∞) + ≤ (res.keys.length : ℕ∞) + + intervalCard (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) + r.begin.key) := by + intro h; exact hnotRaw ⟨hcellKeep, h⟩ + rw [hlastD'] at hnot' + by_cases hanc : 0 ≤ r.begin.anchor db + · have hpre := RangeRead.compatible_keys_prefix hcomp + have hckne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne + obtain ⟨a, _ha_mem, ha_res, ha_head⟩ := + RangeRead.completeKeys_head_resolve hrev hckne hanc + have hhead_eq_a : res.keys.head hne = a := by + rw [← ha_head]; exact hpre.head hne + have hres : resolve db r.begin = ((head : Key) : WithTop Key) := by + rw [hhead, hhead_eq_a]; exact ha_res + have hbound := gap_slack_bound_A2aBottom hcomp hrev hmore hne hbegin hlastB hanc + have hhead_nil : head = [] := by + by_contra hne_head + have hheadD_ne : res.keys.headD ([] : Key) ≠ [] := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne] + exact hne_head + rw [if_neg hheadD_ne] at hnot' + exact hnot' (by simpa using hbound) + rw [hres, hhead_nil] + · exact resolve_eq_nil (not_le.mp hanc) + +/-- **Soundness of `[⊥, end_.key)` in the low branch.** If the gap-capacity condition +fails, every compatible database resolves its begin selector to `⊥`; both selectors are +then fixed by agreement below `end_.key`, so the truncated range `[⊥, end_.key)` is +sound. -/ +theorem sound_bottomEnd_A2aBottom {r : RangeRead} {res : Result} + (hcell : negCrossTruncNonposGapA2aBottom r res) + (hlow : ¬ negCrossTruncNonposGapA2aBottomHigh r res) : + Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + classical + have hcellKeep := hcell + obtain ⟨hcell0, hEB, _hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set δ : ℕ := (if res.keys.headD ([] : Key) = [] then 1 else 0) with hδdef + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hlow' : ¬ (((beginSlack r + 1 + δ : ℕ) : ℕ∞) + ≤ (res.keys.length : ℕ∞) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key) := by + have h := hlow + unfold negCrossTruncNonposGapA2aBottomHigh at h + rw [← hδdef] at h + rw [hlastD] at h + intro hineq + exact h ⟨hcellKeep, hineq⟩ + have hδle : δ ≤ 1 := by rw [hδdef]; split <;> omega + have step : ∀ db1 db2, AgreeOn (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) + db1 db2 → RangeRead.Compatible db1 r res → RangeRead.Compatible db2 r res := by + intro db1 db2 hagree hc1 + have hfiltE : db1.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db2.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + have h := hagree + rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + exact h + have hL1 : resolve db1 r.begin = (([] : Key) : WithTop Key) := + resolve_begin_eq_nil_of_low_A2aBottom hcellKeep hlow hc1 + have hcntE : cnt db2 r.end_.key = cnt db1 r.end_.key := by + rw [cnt, cnt, List.countP_eq_length_filter, List.countP_eq_length_filter] + exact (congrArg List.length hfiltE).symm + have hcnt1 : cnt db1 r.end_.key = res.keys.length + + (between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length := + cnt_end_eq_of_clamp_A2aBottom hc1 hrev hmore hne hend hL1 + have hb1 : (((between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length : ℕ) : ℕ∞) + ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key := + between_length_le_intervalCard (keyAfter_le_of_lt hlast_lt_E) + have hsplit2 := cnt_eq_add_between db2 (le_of_lt hEB) + have hb2 : (((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) + ≤ intervalCard r.end_.key r.begin.key := + between_length_le_intervalCard (le_of_lt hEB) + have hinterval : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = + intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key + + intervalCard r.end_.key r.begin.key := + intervalCard_add (keyAfter_le_of_lt hlast_lt_E) (le_of_lt hEB) + have hcnt2_bound : (cnt db2 r.begin.key : ℕ∞) ≤ + (res.keys.length : ℕ∞) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + calc (cnt db2 r.begin.key : ℕ∞) + = (cnt db2 r.end_.key : ℕ∞) + + (((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by + rw [hsplit2]; push_cast; ring + _ = (cnt db1 r.end_.key : ℕ∞) + + (((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by rw [hcntE] + _ = ((res.keys.length + + (between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length : ℕ) : ℕ∞) + + (((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by rw [hcnt1] + _ = (res.keys.length : ℕ∞) + + (((between db1 (((keyAfter last : Key)) : WithTop Key) r.end_.key).length : ℕ) : ℕ∞) + + (((between db2 r.end_.key r.begin.key).length : ℕ) : ℕ∞) := by + push_cast; ring + _ ≤ (res.keys.length : ℕ∞) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.end_.key + + intervalCard r.end_.key r.begin.key := + add_le_add (add_le_add (le_refl _) hb1) hb2 + _ = (res.keys.length : ℕ∞) + + intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by + rw [hinterval]; ring + have hcnt_lt : (cnt db2 r.begin.key : ℕ∞) < + ((beginSlack r + 1 + δ : ℕ) : ℕ∞) := lt_of_le_of_lt hcnt2_bound (not_le.mp hlow') + have hcnt_le : cnt db2 r.begin.key ≤ beginSlack r + δ := by + have := ENat.natCast_lt_natCast.mp hcnt_lt + omega + have hanchor_le : r.begin.anchor db2 ≤ 0 := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] + have h1 : (cnt db2 r.begin.key : ℤ) ≤ (beginSlack r : ℤ) + (δ : ℤ) := by + exact_mod_cast hcnt_le + have h2 : (δ : ℤ) ≤ 1 := by exact_mod_cast hδle + simp only [cnt] at h1 ⊢ + omega + have hL2 : resolve db2 r.begin = (([] : Key) : WithTop Key) := by + rcases lt_or_eq_of_le hanchor_le with hneg | heq + · exact resolve_eq_nil hneg + · have hcnt_eq : cnt db2 r.begin.key = beginSlack r + 1 := by + rw [anchor_eq_count_sub_slack db2 r (le_of_lt hbegin)] at heq + have h3 : (cnt db2 r.begin.key : ℤ) = (beginSlack r : ℤ) + 1 := by + simp only [cnt] at heq ⊢ + omega + exact_mod_cast h3 + have hδ1 : δ = 1 := by omega + have hheadD : res.keys.headD ([] : Key) = [] := by + rw [hδdef] at hδ1 + by_contra h + rw [if_neg h] at hδ1 + omega + have hhead_nil : head = [] := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne] at hheadD + rw [← hhead] at hheadD + exact hheadD + have hnil_mem1 : ([] : Key) ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (hhead_nil ▸ List.head_mem hne) + have hnil_lt_E : ((([] : Key)) : WithTop Key) < r.end_.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (nil_le_key last)) hlast_lt_E + have hnil_mem2 : ([] : Key) ∈ db2.keys := by + have hmem1 : ([] : Key) ∈ db1.keys.filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [List.mem_filter]; exact ⟨hnil_mem1, by simpa using hnil_lt_E⟩ + rw [hfiltE] at hmem1 + exact (List.mem_filter.mp hmem1).1 + have hpos : 0 ≤ r.begin.anchor db2 := le_of_eq heq.symm + have hlt : (r.begin.anchor db2).toNat < db2.keys.length := by + rw [heq]; simp only [Int.toNat_zero] + exact List.length_pos_of_mem hnil_mem2 + rw [resolve_eq_getElem hpos hlt] + have hidx : (r.begin.anchor db2).toNat = 0 := by rw [heq]; rfl + simp only [hidx] + simpa using keys_getElem_zero_eq_nil_of_mem hnil_mem2 + have hR : resolve db1 r.end_ = resolve db2 r.end_ := + resolve_eq_of_keys_below (db₁ := db1) (db₂ := db2) (E := r.end_.key) (s := r.end_) + hend le_rfl hfiltE + have hck : RangeRead.completeKeys db1 r = RangeRead.completeKeys db2 r := by + rw [completeKeys_eq_filter_of_end_nonpos hrev hend, + completeKeys_eq_filter_of_end_nonpos (db := db2) hrev hend, hL1, hL2, hR, hfiltE] + rcases hc1.2 with ⟨hmore', hpre, hne'⟩ | ⟨hmore', heq'⟩ + · exact ⟨hc1.1, Or.inl ⟨hmore', by rw [← hck]; exact hpre, hne'⟩⟩ + · exact ⟨hc1.1, Or.inr ⟨hmore', by rw [← hck]; exact heq'⟩⟩ + intro db1 db2 hagree + exact ⟨step db1 db2 hagree, step db2 db1 hagree.symm⟩ + +/-- **Minimality of the clamping crossed nonpositive-end gap closed form.** On the +capacity-high branch the range is `[⊥, begin.key)` and on the low branch `[⊥, end_.key)`; +both are sound and every key below the respective upper endpoint is necessary. -/ +theorem minimal_closedForm_of_negCross_trunc_nonposGap_A2aBottom + {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonposGapA2aBottom r res) : + Minimal r res (closedFormNegCrossTruncNonposGapA2aBottom r res) := by + classical + have hcellKeep : negCrossTruncNonposGapA2aBottom r res := hcell + obtain ⟨hcell0, hEB, hbot⟩ := hcell + obtain ⟨hmore, hrev, hbegin, hne, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + have hle_begin : r.begin.offset ≤ 0 := le_of_lt hbegin + -- necessity below the end key, for both branches + have nec_below_end : ∀ k : Key, ((k : Key) : WithTop Key) < r.end_.key → + Necessary r res k := by + intro k hkE + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hkhead : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · obtain ⟨dbw, hcompw, hnot⟩ := + exists_witness_below_head_A2aBottom hread hcellKeep hne hkhead + exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩ + · have hheadk : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := + not_lt.mp hkhead + by_cases hklast : ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hheadk) (WithTop.coe_le_coe.mp hklast) + · have hlastk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) := + not_le.mp hklast + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_last_end_A2aBottom hread hcellKeep hne hlastk hkE + exact necessary_of_toggle ⟨dbw, hcompw, by rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩ + by_cases hhi : negCrossTruncNonposGapA2aBottomHigh r res + · have hclosed : closedFormNegCrossTruncNonposGapA2aBottom r res = + ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by + unfold closedFormNegCrossTruncNonposGapA2aBottom; rw [if_pos hhi] + rw [hclosed] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · have hs := sound_bottomLong_of_nonpos (r := r) (res := res) hle_begin hend + have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hEB) + rwa [hmax] at hs + · intro k _ hkB + by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key + · exact nec_below_end k hkE + · have hEk : r.end_.key ≤ (k : WithTop Key) := not_lt.mp hkE + by_cases hhead : res.keys.head hne = [] + · obtain ⟨dbw, hcompw, hnot⟩ := + exists_witness_gap_eq_head_A2aBottom hread hcellKeep hhi hne hhead hEk hkB + exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩ + · obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_gap_ne_head_A2aBottom hread hcellKeep hhi hne hhead hEk hkB + exact necessary_of_toggle ⟨dbw, hcompw, + by rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩ + · have hclosed : closedFormNegCrossTruncNonposGapA2aBottom r res = + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + unfold closedFormNegCrossTruncNonposGapA2aBottom; rw [if_neg hhi] + rw [hclosed] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_bottomEnd_A2aBottom hcellKeep hhi, ?_⟩ + intro k _ hkE + exact nec_below_end k hkE + +/-! ## `clampLongUpper` is not the gap-cell upper endpoint + +The complete-cell endpoint `clampLongUpper` compares the capacity of +`[end_.key, begin.key)` with the slack `C`. In the crossed nonpositive-end gap cell the +interval `(last, end_.key)` also contributes necessity capacity, so `clampLongUpper` can +select `end_.key` while the high-capacity condition holds and the minimal range is +`[⊥, begin.key)`. The witness below is kernel-checked: `beginSlack = 2`, +`clampNonposD = 0`, one reported key (the empty key), `clampLongCap = clampLongC = 1`, +the high condition true, and yet `clampLongUpper` picks `end_.key`. This is why +`compactForwardTruncatedNonpos` dispatches the crossed clamping branch on whether the +reported last lies below `end_.key`. -/ + +/-- The empty key. -/ +def a2bNil : Key := [] +/-- The key `[0]`. -/ +def a2bZero : Key := [(0 : Fin 256)] +/-- The key `[0,0,0]`, the end selector key. -/ +def a2bThree : Key := [(0 : Fin 256), (0 : Fin 256), (0 : Fin 256)] +/-- The key `[0,0,0,0]`, the begin selector key. -/ +def a2bFour : Key := [(0 : Fin 256), (0 : Fin 256), (0 : Fin 256), (0 : Fin 256)] + +/-- The witness database `{[], [0], [0,0,0]}`. -/ +def a2bDB : DB := ⟨[a2bNil, a2bZero, a2bThree], by decide⟩ + +/-- The witness read: `begin = ⟨[0,0,0,0], -2⟩`, `end = ⟨[0,0,0], 0⟩`. -/ +def a2bRead : RangeRead := + { begin := ⟨(a2bFour : Key), -2⟩, end_ := ⟨(a2bThree : Key), 0⟩, + limit := none, reverse := false } + +/-- The witness result: reports the empty key, truncated. -/ +def a2bRes : Result := { keys := [a2bNil], more := true } + +/-- The witness database is compatible with the witness read and result. -/ +theorem a2b_compatible : RangeRead.Compatible a2bDB a2bRead a2bRes := + ⟨(by unfold RangeRead.withinLimit; simp [a2bRead]), + Or.inl ⟨rfl, by decide, by decide⟩⟩ + +/-- The witness pair is readable. -/ +theorem a2b_readable : Readable a2bRead a2bRes := ⟨a2bDB, a2b_compatible⟩ + +/-- The witness lies in the crossed nonpositive-end gap clamping cell. -/ +theorem a2b_cell : negCrossTruncNonposGapA2aBottom a2bRead a2bRes := by + unfold negCrossTruncNonposGapA2aBottom negCrossTruncNonposGap + decide + +/-- The witness satisfies the high-capacity condition, so its minimal range is +`[⊥, begin.key)`. -/ +theorem a2b_high : negCrossTruncNonposGapA2aBottomHigh a2bRead a2bRes := by + decide + +/-- The complete-cell endpoint `clampLongUpper` selects the end key on the witness. -/ +theorem a2b_clampLongUpper : + clampLongUpper a2bRead a2bRes = ((a2bThree : Key) : WithTop Key) := by + unfold clampLongUpper + rw [if_pos] + · rfl + · decide + +/-- The gap-cell closed form is `[⊥, begin.key)` on the witness. -/ +theorem a2b_closedForm : + closedFormNegCrossTruncNonposGapA2aBottom a2bRead a2bRes = + ⟨(([] : Key) : WithTop Key), ((a2bFour : Key) : WithTop Key)⟩ := by + unfold closedFormNegCrossTruncNonposGapA2aBottom + rw [if_pos a2b_high] + rfl + +/-- The old clamped branch `[⊥, clampLongUpper]` is not the gap-cell closed form on the +witness. -/ +theorem a2b_clampLongUpper_not_gapUpper : + (⟨(([] : Key) : WithTop Key), clampLongUpper a2bRead a2bRes⟩ : ConflictRange) ≠ + closedFormNegCrossTruncNonposGapA2aBottom a2bRead a2bRes := by + rw [a2b_clampLongUpper, a2b_closedForm] + intro h + exact absurd (congrArg ConflictRange.end_ h) (by decide) + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncNonposGapCrossFree.lean b/FormalConflictRange/NegCrossTruncNonposGapCrossFree.lean new file mode 100644 index 0000000..e11c9cd --- /dev/null +++ b/FormalConflictRange/NegCrossTruncNonposGapCrossFree.lean @@ -0,0 +1,1309 @@ +/- +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.NegCrossTruncNonposGapA2aBottom + +/-! +# The cross-free nonpositive-end gap cell + +The crossed nonpositive-end gap cell was closed in `NegCrossTruncNonposGapA2a*`. The +remaining subcell, where `begin.key ≤ end_.key`, has the same candidate closed form +`closedFormNegCrossTruncNonposGap`: `[⊥, end_.key)` when the begin selector can clamp and +`[head, end_.key)` otherwise. + +This module records the structural facts that distinguish it from the crossed cell. + +* Soundness of both branches is already available from the crossed module: + `sound_bottomLong_of_nonpos` fixes `[⊥, max begin.key end.key) = [⊥, end_.key)`, and + `sound_headLong_of_nonbottom` fixes `[head, max begin.key end.key) = [head, end_.key)` + in the non-clamping branch. Neither proof uses crossing. +* The non-clamping branch really is nonempty on the cross-free cell, so the `[head, + end_.key)` branch cannot be discarded as vacuous. +* Crucially, `[head, end_.key)` is **not** sound outside the non-clamping branch: a + compatible database can have a negative begin anchor even when `clampNonposBottom` + holds, because a truncated result (`more = true`) may still report the entire complete + window. The E2 witness below is kernel-checked. This invalidates the earlier guess + that the cross-free cell is always `[⊥, end_.key)`; the `clampNonposBottom` split is + essential. +-/ + +namespace FormalConflictRange + +/-- The cross-free part of the crossed negative-begin truncated nonpositive-end gap +cell. -/ +def negCrossTruncNonposGapCrossFree (r : RangeRead) (res : Result) : Prop := + negCrossTruncNonposGap r res ∧ r.begin.key ≤ r.end_.key + +/-- The cross-free gap closed form is the same three-branch rule as the crossed one; on +cross-free reads `max begin.key end.key = end_.key`. -/ +noncomputable def closedFormNegCrossTruncNonposGapCrossFree (r : RangeRead) (res : Result) : + ConflictRange := + closedFormNegCrossTruncNonposGap r res + +/-- **Soundness of `[⊥, end_.key)` on the cross-free gap cell.** -/ +theorem sound_bottomEnd_crossFree {r : RangeRead} {res : Result} + (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hB_le_E : r.begin.key ≤ r.end_.key) : + Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + have hs := sound_bottomLong_of_nonpos (r := r) (res := res) (le_of_lt hbegin) hend + rwa [max_eq_right hB_le_E] at hs + +/-- **Soundness of `[head, end_.key)` in the non-clamping branch.** -/ +theorem sound_headEnd_crossFree {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset < 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hnbot : ¬ clampNonposBottom r res) + (hend : r.end_.offset ≤ 0) (hB_le_E : r.begin.key ≤ r.end_.key) : + Sound r res (⟨((res.keys.head hne : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + have hs := sound_headLong_of_nonbottom hrev hmore hne hbegin hcross hclamp hnbot hend + rwa [max_eq_right hB_le_E] at hs + +/-! ## The E2 witness: `[head, end_.key)` is not sound on the clamping branch + +Read `begin = ⟨[5], -2⟩`, `end = ⟨[5,0,0], -2⟩`, reporting a single key `[0]` with +`more = true`. The database `[[0],[1],[5],[5,0]]` is compatible with a negative begin +anchor (it reports the full window), while inserting the empty key makes it incompatible. +The two databases agree on `[[0], [5,0,0))`, so `[head, end_.key)` is unsound there. This +is exactly why the clamping branch must keep the lower endpoint at `⊥`. +-/ + +/-- The empty key. -/ +def xgNil : Key := [] +/-- The key `[0]`. -/ +def xgZero : Key := [(0 : Fin 256)] +/-- The key `[1]`. -/ +def xgOne : Key := [(1 : Fin 256)] +/-- The key `[5]`, the begin selector key. -/ +def xgFive : Key := [(5 : Fin 256)] +/-- The key `[5,0]`. -/ +def xgFiveZero : Key := [(5 : Fin 256), (0 : Fin 256)] +/-- The key `[5,0,0]`, the end selector key. -/ +def xgFiveZeroZero : Key := [(5 : Fin 256), (0 : Fin 256), (0 : Fin 256)] + +/-- The E2 read. -/ +def xgRead : RangeRead := + { begin := ⟨(xgFive : Key), -2⟩, end_ := ⟨(xgFiveZeroZero : Key), -2⟩, + limit := none, reverse := false } + +/-- The E2 result: one reported key, truncated. -/ +def xgRes : Result := { keys := [xgZero], more := true } + +/-- Bottom-anchored E2 database, reporting the whole window. -/ +def xgDB3 : DB := ⟨[xgZero, xgOne, xgFive, xgFiveZero], by decide⟩ + +/-- The E2 database with the empty key inserted; incompatible. -/ +def xgDB4 : DB := ⟨[xgNil, xgZero, xgOne, xgFive, xgFiveZero], by decide⟩ + +/-- The range `[[0], [5,0,0))`. -/ +def xgHeadRange : ConflictRange := ⟨(xgZero : Key), (xgFiveZeroZero : Key)⟩ + +/-- The range `[⊥, [5,0,0))`. -/ +def xgBottomRange : ConflictRange := ⟨(([] : Key) : WithTop Key), (xgFiveZeroZero : Key)⟩ + +theorem xg_compatible : RangeRead.Compatible xgDB3 xgRead xgRes := by + unfold RangeRead.Compatible RangeRead.withinLimit + refine ⟨trivial, Or.inl ⟨rfl, ?_, by decide⟩⟩ + decide + +theorem xg_incompatible : ¬ RangeRead.Compatible xgDB4 xgRead xgRes := by + unfold RangeRead.Compatible RangeRead.withinLimit + rintro ⟨-, h | h⟩ + · obtain ⟨-, hpre, -⟩ := h + revert hpre + decide + · obtain ⟨hm, -⟩ := h + revert hm + decide + +theorem xg_agree : AgreeOn xgHeadRange xgDB3 xgDB4 := by + unfold AgreeOn ConflictRange.keys xgHeadRange + decide + +/-- **`[head, end_.key)` is not sound on the E2 cell.** -/ +theorem xg_headRange_not_sound : ¬ Sound xgRead xgRes xgHeadRange := by + intro hs + have h := (hs xgDB3 xgDB4 xg_agree).mp xg_compatible + exact xg_incompatible h + +/-- The E2 pair lies in the cross-free nonpositive-end gap cell. -/ +theorem xg_cell : negCrossTruncNonposGapCrossFree xgRead xgRes := by + refine ⟨?_, by decide⟩ + unfold negCrossTruncNonposGap + refine ⟨rfl, rfl, by decide, by decide, by decide, by decide, by decide, by decide, + by decide, by decide, ?_⟩ + right + decide + +/-- The begin selector clamps in the E2 cell. -/ +theorem xg_clamp : clampNonposBottom xgRead xgRes := by + decide + +/-- `[⊥, end_.key)` is sound on the E2 cell. -/ +theorem xg_bottomRange_sound : Sound xgRead xgRes xgBottomRange := by + unfold xgBottomRange + exact sound_bottomEnd_crossFree (by decide) (by decide) (by decide) + +/-! ## A non-clamping cross-free witness + +The same selectors but reporting two keys `[0], [1]` gives `clampNonposBottom = false`, so +the non-clamping branch of the cross-free cell is genuinely inhabited. Its database +`[[0],[1],[2],[5],[5,0]]` has a nonnegative begin anchor and reports the whole window. +-/ + +/-- The key `[2]`. -/ +def xgTwo : Key := [(2 : Fin 256)] + +/-- The non-clamping result. -/ +def xgResTwo : Result := { keys := [xgZero, xgOne], more := true } + +/-- The non-clamping database. -/ +def xgDBNC : DB := ⟨[xgZero, xgOne, xgTwo, xgFive, xgFiveZero], by decide⟩ + +theorem xg_nonclamp_compatible : RangeRead.Compatible xgDBNC xgRead xgResTwo := by + unfold RangeRead.Compatible RangeRead.withinLimit + refine ⟨trivial, Or.inl ⟨rfl, ?_, by decide⟩⟩ + decide + +/-- The non-clamping read/result still lies in the cross-free gap cell. -/ +theorem xg_nonclamp_cell : negCrossTruncNonposGapCrossFree xgRead xgResTwo := by + refine ⟨?_, by decide⟩ + unfold negCrossTruncNonposGap + refine ⟨rfl, rfl, by decide, by decide, by decide, by decide, by decide, by decide, + by decide, by decide, ?_⟩ + right + decide + +/-- The begin selector does not clamp for the two-key result. -/ +theorem xg_nonclamp : ¬ clampNonposBottom xgRead xgResTwo := by + decide + +/-- `[head, end_.key)` is sound in the non-clamping cross-free branch. -/ +theorem xg_nonclamp_headRange_sound : + Sound xgRead xgResTwo (⟨(xgZero : Key), (xgFiveZeroZero : Key)⟩ : ConflictRange) := by + exact sound_headEnd_crossFree rfl rfl (by decide) (by decide) (by decide) (by decide) + xg_nonclamp (by decide) (by decide) + +end FormalConflictRange + +namespace FormalConflictRange + +/-- Extended-natural arithmetic: if `D + 1 ≤ x + g` with `g ≤ D` finite, then +`D + 1 - g ≤ x`. -/ +theorem enat_sub_cast_le_of_add_cast_le {x : ℕ∞} {g D : ℕ} + (h : ((D + 1 : ℕ) : ℕ∞) ≤ x + (g : ℕ∞)) (hg : g ≤ D) : + ((D + 1 - g : ℕ) : ℕ∞) ≤ x := by + have hD2 : ((D + 1 : ℕ) : ℕ∞) = (g : ℕ∞) + ((D + 1 - g : ℕ) : ℕ∞) := by + have h1 : D + 1 = g + (D + 1 - g) := by omega + conv_lhs => rw [h1] + exact Nat.cast_add .. + rw [hD2, add_comm (g : ℕ∞) ((D + 1 - g : ℕ) : ℕ∞)] at h + exact (ENat.add_le_add_iff_right (by simp : (g : ℕ∞) ≠ ⊤)).mp h + +/-- In the cross-free low-capacity gap cell, the key space from the reported last to +`begin.key` holds at least `clampNonposD + 1 - g` keys, where `g = intervalCard begin +end`. This is the room needed to build the padding database. -/ +theorem intervalCard_begin_key_ge_of_crossFree {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hcell : negCrossTruncNonposGapCrossFree r res) + (hne : res.keys ≠ []) : + ((clampNonposD r + 1 - (intervalCard r.begin.key r.end_.key).toNat : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key := by + obtain ⟨hcell0, hB_le_E⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + have hcap' : intervalCard r.begin.key r.end_.key < + ((clampNonposD r + 1 : ℕ) : ℕ∞) := by + rcases hcap with hcrosslt | hlt + · exact absurd hcrosslt (not_lt.mpr hB_le_E) + · exact hlt + have hlt_top : intervalCard r.begin.key r.end_.key < (⊤ : ℕ∞) := + lt_of_lt_of_le hcap' (by simp) + have hne_top : intervalCard r.begin.key r.end_.key ≠ (⊤ : ℕ∞) := ne_of_lt hlt_top + have hg : intervalCard r.begin.key r.end_.key = + ((intervalCard r.begin.key r.end_.key).toNat : ℕ∞) := + (ENat.natCast_toNat hne_top).symm + have hg_le : (intervalCard r.begin.key r.end_.key).toNat ≤ clampNonposD r := by + have h := hcap' + rw [hg] at h + have h' : (intervalCard r.begin.key r.end_.key).toNat < clampNonposD r + 1 := by + exact_mod_cast h + omega + have hlastD_eq : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD_eq]; exact hlastB + have hka_le_B : ((keyAfter (res.keys.getLast hne) : Key) : WithTop Key) ≤ r.begin.key := + keyAfter_le_of_lt hlast_lt_B + have hkeyAdd : intervalCard (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) + r.end_.key = + intervalCard (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key + + intervalCard r.begin.key r.end_.key := + intervalCard_add hka_le_B hB_le_E + have htot : ((clampNonposD r + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key + + intervalCard r.begin.key r.end_.key := by + have h := intervalCard_keyAfter_getLast_ge (db := db) (r := r) (res := res) + hcomp hne hend + rw [hkeyAdd] at h + exact h + rw [hg] at htot + exact enat_sub_cast_le_of_add_cast_le htot hg_le + +end FormalConflictRange + +namespace FormalConflictRange + +open scoped Classical + +/-- Padding the reported keys with `f = D + 1 - g` keys in `(last, begin.key)` and the +`g` keys of `[begin.key, end_.key)` gives a compatible database: the end selector +resolves exactly at index `#reported`, and the begin anchor is nonpositive. Here +`g = intervalCard begin end` and `D = clampNonposD`. -/ +theorem exists_padDb_crossFree {r : RangeRead} {res : Result} {dbF1 dbF2 : DB} {g : ℕ} + (hsort : res.keys.SortedLT) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hlim : RangeRead.withinLimit r res.keys) + (hbegin : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hB_le_E : r.begin.key ≤ r.end_.key) + (hEne : r.end_.key < (⊤ : WithTop Key)) + (hlastB : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hg_le : g ≤ clampNonposD r) + (hF1last : ∀ x ∈ dbF1.keys, res.keys.getLast hne < x) + (hF1B : ∀ x ∈ dbF1.keys, (x : WithTop Key) < r.begin.key) + (hF1len : dbF1.keys.length = clampNonposD r + 1 - g) + (hF2B : ∀ x ∈ dbF2.keys, r.begin.key ≤ (x : WithTop Key)) + (hF2E : ∀ x ∈ dbF2.keys, (x : WithTop Key) < r.end_.key) + (hF2len : dbF2.keys.length = g) + (hanc0 : res.keys.length + (clampNonposD r + 1 - g) ≤ beginSlack r + 1) : + ∃ dbw : DB, dbw.keys = res.keys ++ dbF1.keys ++ dbF2.keys ∧ + RangeRead.Compatible dbw r res ∧ r.end_.anchor dbw = (res.keys.length : ℤ) ∧ + r.begin.anchor dbw ≤ 0 := by + classical + set last : Key := res.keys.getLast hne with hlast + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_B : ((last : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hlast_lt_E : ((last : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hlast_lt_B hB_le_E + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hF1ne : dbF1.keys ≠ [] := by + intro h + rw [h] at hF1len + simp only [List.length_nil] at hF1len + omega + -- sorted concatenation + set F : List Key := dbF1.keys ++ dbF2.keys with hF + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_append] + refine ⟨dbF1.sorted, dbF2.sorted, ?_⟩ + intro a ha b hb + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hF1B a ha) (hF2B b hb)) + have hFsorted' : (res.keys ++ F).SortedLT := by + rw [List.sortedLT_append] + refine ⟨hsort, hFsorted, ?_⟩ + intro a ha b hb + rw [hF] at hb + rcases List.mem_append.mp hb with hb | hb + · exact lt_of_le_of_lt (hle_last a ha) (hF1last b hb) + · exact lt_of_le_of_lt (hle_last a ha) + (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B (hF2B b hb))) + let dbw : DB := ⟨res.keys ++ F, hFsorted'⟩ + have hkeyw : dbw.keys = res.keys ++ dbF1.keys ++ dbF2.keys := by + change res.keys ++ F = _; rw [hF, List.append_assoc] + -- all stored keys lie below the end key + have hall_lt_E : ∀ x ∈ dbw.keys, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E + · exact lt_of_lt_of_le (hF1B x hx) hB_le_E + · exact hF2E x hx + have hlen : dbw.keys.length = res.keys.length + (clampNonposD r + 1) := by + rw [hkeyw, List.length_append, List.length_append, hF1len, hF2len] + omega + have hcntE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hall_lt_E x hx + have hDint : ((clampNonposD r : ℕ) : ℤ) = -r.end_.offset := by + rw [clampNonposD_eq] + exact Int.toNat_of_nonneg (by omega) + have hanchorE : r.end_.anchor dbw = (res.keys.length : ℤ) := by + cases hE : r.end_.key with + | top => exact absurd hE (ne_of_lt hEne) + | coe e => + have hce : dbw.keys.countP (fun x : Key => decide (x < e)) = dbw.keys.length := by + rw [← countP_lt_coe (l := dbw.keys) e, ← hE] + exact hcntE + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlen] + push_cast + omega + -- the count below the begin key is `t + f` + have hcntB : dbw.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + res.keys.length + (clampNonposD r + 1 - g) := by + rw [hkeyw, List.countP_append, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B + have h2 : dbF1.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbF1.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hF1B x hx + have h3 : dbF2.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq, not_lt] + exact hF2B x hx + rw [h1, h2, h3, hF1len] + omega + have hanchorB : r.begin.anchor dbw ≤ 0 := by + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcntB] + have h := hanc0 + push_cast + omega + -- the end resolution sits at the first filler, above the reported last + have hFne : F ≠ [] := by + rw [hF] + exact List.append_ne_nil_of_left_ne_nil hF1ne _ + have hDget? : (res.keys ++ F)[res.keys.length]? = some (F.head hFne) := by + rw [List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hF, List.length_append]; omega), + List.head_eq_getElem hFne] + have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by + have hanchorE0 : 0 ≤ r.end_.anchor dbw := by rw [hanchorE]; exact Int.natCast_nonneg _ + have hltlen : (r.end_.anchor dbw).toNat < dbw.keys.length := by + rw [hanchorE, Int.toNat_natCast, hkeyw, List.length_append, List.length_append, + hF1len, hF2len] + omega + have htoNat : (r.end_.anchor dbw).toNat = res.keys.length := by rw [hanchorE]; simp + have hsome : dbw.keys[(r.end_.anchor dbw).toNat]? = some (F.head hFne) := by + rw [htoNat] + exact hDget? + have hget : dbw.keys[(r.end_.anchor dbw).toNat] = F.head hFne := + (List.getElem?_eq_some_iff.mp hsome).2 + rw [resolve_eq_getElem hanchorE0 hltlen, hget] + have hFgt : ∀ x ∈ F, last < x := by + intro x hx + have hx' : x ∈ dbF1.keys ++ dbF2.keys := hx + rcases List.mem_append.mp hx' with hx' | hx' + · exact hF1last x hx' + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B (hF2B x hx')) + have hlast_lt_Fhead : last < F.head hFne := hFgt _ (List.head_mem hFne) + have hFhead_le : ∀ x, x ∈ F → F.head hFne ≤ x := + fun x hx => (hFsorted.pairwise.imp le_of_lt).rel_head hx + have hFhead_le_F1 : ∀ x ∈ dbF1.keys, F.head hFne ≤ x := + fun x hx => hFhead_le x (List.mem_append.mpr (Or.inl hx)) + have hFhead_le_F2 : ∀ x ∈ dbF2.keys, F.head hFne ≤ x := + fun x hx => hFhead_le x (List.mem_append.mpr (Or.inr hx)) + -- begin resolution is at most the reported head + have hb_le_head : resolve dbw r.begin ≤ ((res.keys.head hne : Key) : WithTop Key) := by + by_cases hneg : r.begin.anchor dbw < 0 + · rw [resolve_eq_nil hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key _) + · have hzero : r.begin.anchor dbw = 0 := by omega + have h0 : 0 ≤ r.begin.anchor dbw := by omega + have hlt : (r.begin.anchor dbw).toNat < dbw.keys.length := by + rw [hzero, hkeyw, List.length_append, List.length_append, hF1len, hF2len] + simp + omega + have htoNat : (r.begin.anchor dbw).toNat = 0 := by rw [hzero]; simp + have hget : dbw.keys[(r.begin.anchor dbw).toNat] = res.keys.head hne := by + have hsome : dbw.keys[(r.begin.anchor dbw).toNat]? = some (res.keys.head hne) := by + rw [htoNat] + rw [hkeyw, List.append_assoc] + have hpos : 0 < res.keys.length := List.length_pos_iff.mpr hne + rw [List.getElem?_append_left hpos, ← List.head?_eq_getElem?] + exact List.head?_eq_some_head hne + exact (List.getElem?_eq_some_iff.mp hsome).2 + rw [resolve_eq_getElem h0 hlt, hget] + -- the complete keys are exactly the reported keys + have hck : RangeRead.completeKeys dbw r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [if_neg (by simp [hrev])] + rw [hkeyw, List.filter_append, List.filter_append] + have hres : res.keys.filter (fun k : Key => decide + (resolve dbw r.begin ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < resolve dbw r.end_)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + refine ⟨le_trans hb_le_head + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx)), ?_⟩ + rw [hre] + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (hle_last x hx) hlast_lt_Fhead) + have hF1nil : dbF1.keys.filter (fun k : Key => decide + (resolve dbw r.begin ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < resolve dbw r.end_)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq, hre] + intro hcon + exact not_lt.mpr (WithTop.coe_le_coe.mpr (hFhead_le_F1 x hx)) hcon.2 + have hF2nil : dbF2.keys.filter (fun k : Key => decide + (resolve dbw r.begin ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < resolve dbw r.end_)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq, hre] + intro hcon + exact not_lt.mpr (WithTop.coe_le_coe.mpr (hFhead_le_F2 x hx)) hcon.2 + rw [hres, hF1nil, hF2nil, List.append_nil, List.append_nil] + refine ⟨dbw, hkeyw, ?_, hanchorE, hanchorB⟩ + exact ⟨hlim, Or.inl ⟨hmore, by rw [hck], hne⟩⟩ + + +open scoped Classical + +/-- Padding a compatible database with a list `F` of keys strictly above the reported +last and strictly below the end key, whose length is `clampNonposD + 1`, and where the +end selector anchors exactly at the reported count, makes any single padding key +necessary: erasing it drops the end resolution onto the reported last, which the result +does not report. -/ +theorem erase_incompatible_of_pad + {r : RangeRead} {res : Result} {dbw : DB} {F : List Key} {k : Key} + (hsort : res.keys.SortedLT) + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hkeyw : dbw.keys = res.keys ++ F) + (hkF : k ∈ F) + (hFgt : ∀ x ∈ F, res.keys.getLast hne < x) + (hreslt : ∀ x ∈ res.keys, (x : WithTop Key) < r.end_.key) + (hFlt : ∀ x ∈ F, (x : WithTop Key) < r.end_.key) + (hFlen : F.length = clampNonposD r + 1) + (hend : r.end_.offset ≤ 0) + (hEne : r.end_.key < (⊤ : WithTop Key)) : + ¬ RangeRead.Compatible (dbw.erase k) r res := by + classical + have hle_last : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hk_not_res : k ∉ res.keys := by + intro hx + exact absurd (hFgt k hkF) (not_lt.mpr (hle_last k hx)) + have herase_keys : (dbw.erase k).keys = res.keys ++ F.erase k := by + rw [DB.erase_keys, hkeyw, List.erase_append_right F hk_not_res] + have hEraseLt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [herase_keys] at hx + rcases List.mem_append.mp hx with hx | hx + · exact hreslt x hx + · exact hFlt x (List.mem_of_mem_erase hx) + have hcntErase : cnt (dbw.erase k) r.end_.key = (dbw.erase k).keys.length := + cnt_eq_length_of_forall_lt _ hEraseLt + have hlenErase : (dbw.erase k).keys.length = res.keys.length + clampNonposD r := by + rw [herase_keys, List.length_append, List.length_erase, if_pos hkF, hFlen] + omega + have hoff : r.end_.offset = -(clampNonposD r : ℤ) := by + rw [clampNonposD_eq] + have hnonneg : 0 ≤ -r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnonneg]; ring + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = (↑e : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h (ne_of_lt hEne) + | coe e => exact ⟨e, rfl⟩ + have hanchorErase : r.end_.anchor (dbw.erase k) = (res.keys.length : ℤ) - 1 := by + rw [anchor_of_coe (s := r.end_) (v := e) he] + have hce : (dbw.erase k).keys.countP (fun x => decide (x < e)) = + res.keys.length + clampNonposD r := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← he] + exact hcntErase.trans hlenErase + rw [hce, hoff]; push_cast; ring + have ht_pos : 0 < res.keys.length := List.length_pos_iff.mpr hne + have hresolve_end : resolve (dbw.erase k) r.end_ = + ((res.keys.getLast hne : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanchorErase]; omega + have hlt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by + rw [hanchorErase, hlenErase]; omega + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchorErase] + have : ((res.keys.length : ℤ) - 1).toNat = res.keys.length - 1 := by omega + exact this + have hget : (dbw.erase k).keys[(r.end_.anchor (dbw.erase k)).toNat] = + res.keys.getLast hne := by + simp only [hidx, herase_keys] + have hlen : res.keys.length - 1 < res.keys.length := by omega + rw [List.getElem_append_left (i := res.keys.length - 1) (h := hlen)] + exact (List.getLast_eq_getElem hne).symm + rw [hget] + intro hcomp' + have hfilt := ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp').2 + have hlast_not_ck : res.keys.getLast hne ∉ RangeRead.completeKeys (dbw.erase k) r := by + rw [RangeRead.mem_completeKeys, hresolve_end] + rintro ⟨-, -, hlt⟩ + exact lt_irrefl _ hlt + have hlast_in_filt : res.keys.getLast hne ∈ + (RangeRead.completeKeys (dbw.erase k) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [hfilt]; exact List.getLast_mem hne + exact hlast_not_ck (List.mem_of_mem_filter hlast_in_filt) + +open scoped Classical + +/-- A database of `f` keys in `(last, begin.key)`. -/ +theorem exists_dbF1_crossFree {r : RangeRead} {res : Result} {f : ℕ} {b : Key} + (hne : res.keys ≠ []) + (hb : r.begin.key = ((b : Key) : WithTop Key)) + (hroom : (f : ℕ∞) ≤ intervalCard + (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key) : + ∃ dbF1 : DB, (∀ x ∈ dbF1.keys, res.keys.getLast hne < x) ∧ + (∀ x ∈ dbF1.keys, (x : WithTop Key) < r.begin.key) ∧ dbF1.keys.length = f := by + obtain ⟨dbF1, hmem, hlen⟩ := exists_db_keysIn + (B := (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key)) + (E := r.begin.key) (m := f) hroom + refine ⟨dbF1, ?_, ?_, hlen⟩ + · intro x hx + have h := hmem x hx + rw [hb] at h + rw [mem_keysIn_coe] at h + exact lt_of_lt_of_le (keyAfter_self_lt _) h.1 + · intro x hx + have h := hmem x hx + rw [hb] at h + rw [mem_keysIn_coe] at h + rw [hb]; exact WithTop.coe_lt_coe.mpr h.2 + +/-- A database of `g` keys in `[begin.key, end_.key)`. -/ +theorem exists_dbF2_crossFree {r : RangeRead} {res : Result} {g : ℕ} {b e : Key} + (hb : r.begin.key = ((b : Key) : WithTop Key)) + (he : r.end_.key = ((e : Key) : WithTop Key)) + (hcap : (g : ℕ∞) ≤ intervalCard r.begin.key r.end_.key) : + ∃ dbF2 : DB, (∀ x ∈ dbF2.keys, r.begin.key ≤ (x : WithTop Key)) ∧ + (∀ x ∈ dbF2.keys, (x : WithTop Key) < r.end_.key) ∧ dbF2.keys.length = g := by + obtain ⟨dbF2, hmem, hlen⟩ := exists_db_keysIn + (B := r.begin.key) (E := r.end_.key) (m := g) hcap + refine ⟨dbF2, ?_, ?_, hlen⟩ + · intro x hx + have h := hmem x hx + rw [hb, he] at h + rw [mem_keysIn_coe] at h + rw [hb]; exact WithTop.coe_le_coe.mpr h.1 + · intro x hx + have h := hmem x hx + rw [hb, he] at h + rw [mem_keysIn_coe] at h + rw [he]; exact WithTop.coe_lt_coe.mpr h.2 + +/-- A database of `f - 1` keys in `(last, begin.key)` avoiding a chosen key. -/ +theorem exists_dbF1_avoid_crossFree {r : RangeRead} {res : Result} {f : ℕ} {k b : Key} + (hne : res.keys ≠ []) + (hb : r.begin.key = ((b : Key) : WithTop Key)) + (hf : 1 ≤ f) + (hroom : (f : ℕ∞) ≤ intervalCard + (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key) : + ∃ dbB : DB, (∀ x ∈ dbB.keys, res.keys.getLast hne < x) ∧ + (∀ x ∈ dbB.keys, (x : WithTop Key) < r.begin.key) ∧ + dbB.keys.length = f - 1 ∧ k ∉ dbB.keys := by + have hcard : (((f - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard + (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key := by + rwa [Nat.sub_add_cancel hf] + obtain ⟨dbB, hmem, hBk, hlen⟩ := exists_db_keysIn_avoid + (B := (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key)) + (E := r.begin.key) (k := k) (m := f - 1) hcard + refine ⟨dbB, ?_, ?_, hlen, hBk⟩ + · intro x hx + have h := hmem x hx + rw [hb] at h + rw [mem_keysIn_coe] at h + exact lt_of_lt_of_le (keyAfter_self_lt _) h.1 + · intro x hx + have h := hmem x hx + rw [hb] at h + rw [mem_keysIn_coe] at h + rw [hb]; exact WithTop.coe_lt_coe.mpr h.2 + +/-- A database of `f` keys in `(last, begin.key)` containing a chosen key `k`. -/ +theorem exists_dbF1_mem_crossFree {r : RangeRead} {res : Result} {f : ℕ} {k b : Key} + (hne : res.keys ≠ []) + (hb : r.begin.key = ((b : Key) : WithTop Key)) + (hf : 1 ≤ f) + (hroom : (f : ℕ∞) ≤ intervalCard + (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key) + (hk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + ∃ dbF1 : DB, (∀ x ∈ dbF1.keys, res.keys.getLast hne < x) ∧ + (∀ x ∈ dbF1.keys, (x : WithTop Key) < r.begin.key) ∧ + dbF1.keys.length = f ∧ k ∈ dbF1.keys := by + obtain ⟨dbB, hBgt, hBB, hBlen, hBk⟩ := + exists_dbF1_avoid_crossFree (r := r) (res := res) (f := f) (k := k) (b := b) hne hb hf hroom + let dbF1 : DB := dbB.insert k hBk + have hkey : dbF1.keys = dbB.keys.orderedInsert (· ≤ ·) k := DB.insert_keys dbB k hBk + refine ⟨dbF1, ?_, ?_, ?_, ?_⟩ + · intro x hx + rw [hkey] at hx + rcases (List.mem_orderedInsert (· ≤ ·)).mp hx with rfl | hx + · exact WithTop.coe_lt_coe.mp hk + · exact hBgt x hx + · intro x hx + rw [hkey] at hx + rcases (List.mem_orderedInsert (· ≤ ·)).mp hx with rfl | hx + · exact hkB + · exact hBB x hx + · rw [DB.insert_keys, List.orderedInsert_length, hBlen] + omega + · exact DB.mem_insert_self dbB k hBk + +/-- A database of `g - 1` keys in `[begin.key, end_.key)` avoiding a chosen key. -/ +theorem exists_dbF2_avoid_crossFree {r : RangeRead} {res : Result} {g : ℕ} {k b e : Key} + (hb : r.begin.key = ((b : Key) : WithTop Key)) + (he : r.end_.key = ((e : Key) : WithTop Key)) + (hg : 1 ≤ g) + (hcap : (g : ℕ∞) ≤ intervalCard r.begin.key r.end_.key) : + ∃ dbB : DB, (∀ x ∈ dbB.keys, r.begin.key ≤ (x : WithTop Key)) ∧ + (∀ x ∈ dbB.keys, (x : WithTop Key) < r.end_.key) ∧ + dbB.keys.length = g - 1 ∧ k ∉ dbB.keys := by + have hcard : (((g - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + rwa [Nat.sub_add_cancel hg] + obtain ⟨dbB, hmem, hBk, hlen⟩ := exists_db_keysIn_avoid + (B := r.begin.key) (E := r.end_.key) (k := k) (m := g - 1) hcard + refine ⟨dbB, ?_, ?_, hlen, hBk⟩ + · intro x hx + have h := hmem x hx + rw [hb, he] at h + rw [mem_keysIn_coe] at h + rw [hb]; exact WithTop.coe_le_coe.mpr h.1 + · intro x hx + have h := hmem x hx + rw [hb, he] at h + rw [mem_keysIn_coe] at h + rw [he]; exact WithTop.coe_lt_coe.mpr h.2 + +/-- A database of `g` keys in `[begin.key, end_.key)` containing a chosen key `k`. -/ +theorem exists_dbF2_mem_crossFree {r : RangeRead} {res : Result} {g : ℕ} {k b e : Key} + (hb : r.begin.key = ((b : Key) : WithTop Key)) + (he : r.end_.key = ((e : Key) : WithTop Key)) + (hg : 1 ≤ g) + (hcap : (g : ℕ∞) ≤ intervalCard r.begin.key r.end_.key) + (hkB : r.begin.key ≤ (k : WithTop Key)) + (hkE : (k : WithTop Key) < r.end_.key) : + ∃ dbF2 : DB, (∀ x ∈ dbF2.keys, r.begin.key ≤ (x : WithTop Key)) ∧ + (∀ x ∈ dbF2.keys, (x : WithTop Key) < r.end_.key) ∧ + dbF2.keys.length = g ∧ k ∈ dbF2.keys := by + obtain ⟨dbB, hBB, hBE, hBlen, hBk⟩ := + exists_dbF2_avoid_crossFree (r := r) (res := res) (g := g) (k := k) (b := b) (e := e) + hb he hg hcap + let dbF2 : DB := dbB.insert k hBk + have hkey : dbF2.keys = dbB.keys.orderedInsert (· ≤ ·) k := DB.insert_keys dbB k hBk + refine ⟨dbF2, ?_, ?_, ?_, ?_⟩ + · intro x hx + rw [hkey] at hx + rcases (List.mem_orderedInsert (· ≤ ·)).mp hx with rfl | hx + · exact hkB + · exact hBB x hx + · intro x hx + rw [hkey] at hx + rcases (List.mem_orderedInsert (· ≤ ·)).mp hx with rfl | hx + · exact hkE + · exact hBE x hx + · rw [DB.insert_keys, List.orderedInsert_length, hBlen] + omega + · exact DB.mem_insert_self dbB k hBk + +end FormalConflictRange + +namespace FormalConflictRange + +open scoped Classical + +/-- In the cross-free low-capacity gap cell the interval capacity is finite and at most +`clampNonposD`. -/ +theorem crossFree_g_le {r : RangeRead} {res : Result} + (hcell : negCrossTruncNonposGapCrossFree r res) : + (intervalCard r.begin.key r.end_.key).toNat ≤ clampNonposD r := by + obtain ⟨hcell0, hB_le_E⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcross, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + have hcap' : intervalCard r.begin.key r.end_.key < + ((clampNonposD r + 1 : ℕ) : ℕ∞) := by + rcases hcap with hcrosslt | hlt + · exact absurd hcrosslt (not_lt.mpr hB_le_E) + · exact hlt + have hlt_top : intervalCard r.begin.key r.end_.key < (⊤ : ℕ∞) := + lt_of_lt_of_le hcap' (by simp) + have hne_top : intervalCard r.begin.key r.end_.key ≠ (⊤ : ℕ∞) := ne_of_lt hlt_top + have hg : intervalCard r.begin.key r.end_.key = + ((intervalCard r.begin.key r.end_.key).toNat : ℕ∞) := + (ENat.natCast_toNat hne_top).symm + have h := hcap' + rw [hg] at h + have h' : (intervalCard r.begin.key r.end_.key).toNat < clampNonposD r + 1 := by + exact_mod_cast h + omega + +/-- In the cross-free low-capacity gap cell readability forces the padding inequality +`t + (D + 1 - g) ≤ beginSlack + 1`, where `t` is the reported count, `D` the end +overshoot and `g` the capacity of `[begin.key, end_.key)`. -/ +theorem crossFree_pad_hanc0 {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonposGapCrossFree r res) + (hne : res.keys ≠ []) : + res.keys.length + (clampNonposD r + 1 - (intervalCard r.begin.key r.end_.key).toNat) ≤ + beginSlack r + 1 := by + classical + have hcellKeep : negCrossTruncNonposGapCrossFree r res := hcell + obtain ⟨hcell0, hB_le_E⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcrossL, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hg_le := crossFree_g_le hcellKeep + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + by_cases hbot : clampNonposBottom r res + · -- clamping: `clampNonposBottom` gives `t + f ≤ beginSlack` + have h := hbot + unfold clampNonposBottom at h + rw [hrep] at h + -- the subtraction `(D+1) - intervalCard` is the natural `D+1-g` + have htop : intervalCard r.begin.key r.end_.key < (⊤ : ℕ∞) := by + have hcap' : intervalCard r.begin.key r.end_.key < + ((clampNonposD r + 1 : ℕ) : ℕ∞) := by + rcases hcap with hcrosslt | hlt + · exact absurd hcrosslt (not_lt.mpr hB_le_E) + · exact hlt + exact lt_of_lt_of_le hcap' (by simp) + have hg : intervalCard r.begin.key r.end_.key = + ((intervalCard r.begin.key r.end_.key).toNat : ℕ∞) := + (ENat.natCast_toNat (ne_of_lt htop)).symm + rw [hg] at h + -- `((D+1:ℕ):ℕ∞) - (g:ℕ∞) = ((D+1-g:ℕ):ℕ∞)` + have hsub : (((clampNonposD r + 1 : ℕ) : ℕ∞) - + ((intervalCard r.begin.key r.end_.key).toNat : ℕ∞)) = + ((clampNonposD r + 1 - (intervalCard r.begin.key r.end_.key).toNat : ℕ) : ℕ∞) := + (ENat.natCast_sub (clampNonposD r + 1) + (intervalCard r.begin.key r.end_.key).toNat).symm + rw [hsub] at h + have hnat : res.keys.length + + (clampNonposD r + 1 - (intervalCard r.begin.key r.end_.key).toNat) ≤ beginSlack r := by + exact_mod_cast h + omega + · -- non-clamping: count stored keys above the reported last + have hhead : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.head hne := by + simp [leastReported, hrev, List.head?_eq_some_head hne] + rwa [hle] at hcrossL + have hanc : 0 ≤ r.begin.anchor db₀ := by + by_contra h + exact hbot (clampNonposBottom_of_anchor_neg_trunc hcomp₀ hrev hmore hne hbegin hend + hclamp (by omega)) + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast hhead_mem + have hheadB : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := hhead + have hresBegin : resolve db₀ r.begin = ((res.keys.head hne : Key) : WithTop Key) := + resolve_begin_eq_head_of_nonbottom hcomp₀ hrev hmore hne hbegin hend hclamp hbot + have hgap := gap_count_eq_beginSlack_of_resolve_head (db := db₀) hbegin hhead hanc hresBegin + -- `last` is complete and lies below the end resolution + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hlast_mem + have hresEnd_gt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hfilterComp : (RangeRead.completeKeys db₀ r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = res.keys := + ((compatible_iff_truncated_keys hrev hmore hne).mp hcomp₀).2 + -- stored keys in `[head, last]` are exactly the reported ones + have hfilter := completeKeys_filter_le_of_resolve_begin (db := db₀) hrev hresBegin hresEnd_gt + have hHL : db₀.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : Key) ≤ res.keys.getLast hne)) = res.keys := by + rw [← hfilter, hfilterComp] + -- the same filter with `< keyAfter last` + have hHKa : (between db₀ ((res.keys.head hne : Key) : WithTop Key) + (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)).length = res.keys.length := by + unfold between + have hcongr : db₀.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + ((k : Key) : WithTop Key) < ((keyAfter (res.keys.getLast hne) : Key) : WithTop Key))) = + db₀.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : Key) ≤ res.keys.getLast hne)) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + by_cases hk : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) + · simp only [hk, true_and] + exact WithTop.coe_lt_coe.trans (lt_keyAfter_iff k (res.keys.getLast hne)) + · simp only [hk, false_and] + rw [hcongr, hHL] + -- counts split at `keyAfter last` and at `begin.key` + have hcntB : (cnt db₀ r.begin.key) = (cnt db₀ ((res.keys.head hne : Key) : WithTop Key)) + + (between db₀ ((res.keys.head hne : Key) : WithTop Key) r.begin.key).length := + cnt_eq_add_between db₀ (le_of_lt hheadB) + have hgapLen : (between db₀ ((res.keys.head hne : Key) : WithTop Key) r.begin.key).length = + beginSlack r + 1 := by + rw [← hgap] + unfold between + rw [List.countP_eq_length_filter] + have hcntKa : (cnt db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) = + (cnt db₀ ((res.keys.head hne : Key) : WithTop Key)) + res.keys.length := by + have := cnt_eq_add_between db₀ (w₁ := ((res.keys.head hne : Key) : WithTop Key)) + (w₂ := (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) (by + exact WithTop.coe_le_coe.mpr (le_trans hhead_le_last (le_of_lt (keyAfter_self_lt _)))) + rw [this, hHKa] + have hka_le_B : (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) ≤ r.begin.key := by + apply keyAfter_le_of_lt + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rwa [hlastD] at hlastB + have hcntB2 : (cnt db₀ r.begin.key) = + (cnt db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) + + (between db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) + r.begin.key).length := + cnt_eq_add_between db₀ hka_le_B + -- the end resolution sits above the reported last + have hresEnd_ne : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by + intro h + rw [h] at hresEnd_gt + exact absurd hresEnd_gt + (not_lt.mpr (coe_nil_le_key_withTop ((res.keys.getLast hne : Key) : WithTop Key))) + have hbetween_len : (between db₀ (resolve db₀ r.end_) r.end_.key).length = + clampNonposD r + 1 := between_resolve_end_length hend hresEnd_ne + have hka_le_resEnd : (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) ≤ + resolve db₀ r.end_ := keyAfter_le_of_lt hresEnd_gt + -- `[resolve end, end)` sits inside `[keyAfter last, end)` + have hsub : (between db₀ (resolve db₀ r.end_) r.end_.key).length ≤ + (between db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) + r.begin.key).length + + (between db₀ r.begin.key r.end_.key).length := by + -- count approach + have hcountEnd : (cnt db₀ r.end_.key) = + (cnt db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) + + (between db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) + r.end_.key).length := + cnt_eq_add_between db₀ (le_trans hka_le_B hB_le_E) + have hcountEnd2 : (cnt db₀ r.end_.key) = + (cnt db₀ (resolve db₀ r.end_)) + + (between db₀ (resolve db₀ r.end_) r.end_.key).length := + cnt_eq_add_between db₀ (resolve_le_key_of_offset_nonpos hend) + have hsplit : (between db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) + r.end_.key).length = + (between db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) + r.begin.key).length + + (between db₀ r.begin.key r.end_.key).length := by + have h1 := cnt_eq_add_between db₀ hka_le_B + have h2 := cnt_eq_add_between db₀ hB_le_E + have h3 := cnt_eq_add_between db₀ (le_trans hka_le_B hB_le_E) + omega + have hmono : (cnt db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key)) ≤ + (cnt db₀ (resolve db₀ r.end_)) := cnt_le_cnt_of_le db₀ hka_le_resEnd + rw [hsplit] at hcountEnd + omega + -- combine the counts + have hG : (between db₀ (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) + r.begin.key).length = beginSlack r + 1 - res.keys.length := by + have h1 := hcntB + rw [hgapLen] at h1 + rw [hcntKa] at hcntB2 + omega + have hHcard : (between db₀ r.begin.key r.end_.key).length ≤ + (intervalCard r.begin.key r.end_.key).toNat := by + have h := between_length_le_intervalCard (db := db₀) hB_le_E + have htop : intervalCard r.begin.key r.end_.key < (⊤ : ℕ∞) := by + have hcap' : intervalCard r.begin.key r.end_.key < + ((clampNonposD r + 1 : ℕ) : ℕ∞) := by + rcases hcap with hcrosslt | hlt + · exact absurd hcrosslt (not_lt.mpr hB_le_E) + · exact hlt + exact lt_of_lt_of_le hcap' (by simp) + have hg : ((intervalCard r.begin.key r.end_.key).toNat : ℕ∞) = + intervalCard r.begin.key r.end_.key := ENat.natCast_toNat (ne_of_lt htop) + rw [← hg] at h + exact_mod_cast h + rw [hbetween_len] at hsub + omega + + + +open scoped Classical + +/-- In the cross-free low-capacity gap cell, a key strictly between the reported last and +`end_.key` has an erase witness. -/ +theorem exists_witness_last_end_crossFree {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapCrossFree r res) + (hne : res.keys ≠ []) + (hkgt : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) + (hkE : (k : WithTop Key) < r.end_.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + classical + have hreadKeep : Readable r res := hread + have hcellKeep : negCrossTruncNonposGapCrossFree r res := hcell + obtain ⟨hcell0, hB_le_E⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcrossL, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = ((b : Key) : WithTop Key) := by + cases h : r.begin.key with + | top => + rw [h] at hB_le_E + exact absurd (lt_of_le_of_lt hB_le_E hEne) (lt_irrefl _) + | coe b => exact ⟨b, rfl⟩ + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = ((e : Key) : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h (ne_of_lt hEne) + | coe e => exact ⟨e, rfl⟩ + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hlast_lt_E : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by + rw [← hlastD]; exact hlastE + have hle_last : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + set g : ℕ := (intervalCard r.begin.key r.end_.key).toNat with hgdef + have hg_le : g ≤ clampNonposD r := crossFree_g_le hcellKeep + set f : ℕ := clampNonposD r + 1 - g with hfdef + have hf : 1 ≤ f := by rw [hfdef]; omega + have hroom := intervalCard_begin_key_ge_of_crossFree hcomp₀ hcellKeep hne + have hroom' : (f : ℕ∞) ≤ intervalCard + (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key := by + rw [hfdef]; exact hroom + have hfin : intervalCard r.begin.key r.end_.key < (⊤ : ℕ∞) := by + have hcap' : intervalCard r.begin.key r.end_.key < + ((clampNonposD r + 1 : ℕ) : ℕ∞) := by + rcases hcap with hcrosslt | hlt + · exact absurd hcrosslt (not_lt.mpr hB_le_E) + · exact hlt + exact lt_of_lt_of_le hcap' (by simp) + have hcap2 : (g : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + rw [hgdef, ENat.natCast_toNat (ne_of_lt hfin)] + have hanc0 := crossFree_pad_hanc0 hreadKeep hcellKeep hne + have hanc0' : res.keys.length + f ≤ beginSlack r + 1 := by + rw [hfdef, hgdef]; exact hanc0 + -- combine the two padding lists + have finish : ∀ (dbF1 dbF2 : DB) (hkIn : k ∈ dbF1.keys ++ dbF2.keys), + (∀ x ∈ dbF1.keys, res.keys.getLast hne < x) → + (∀ x ∈ dbF1.keys, (x : WithTop Key) < r.begin.key) → + dbF1.keys.length = f → + (∀ x ∈ dbF2.keys, r.begin.key ≤ (x : WithTop Key)) → + (∀ x ∈ dbF2.keys, (x : WithTop Key) < r.end_.key) → + dbF2.keys.length = g → + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + intro dbF1 dbF2 hkIn hF1gt hF1B hF1len hF2B hF2E hF2len + have hF1len' : dbF1.keys.length = clampNonposD r + 1 - g := by + rw [hF1len, hfdef] + obtain ⟨dbw, hkeyw, hcompw, hanchorE, hanchorB⟩ := + exists_padDb_crossFree hsort hrev hmore hne hlim hbegin hend hB_le_E hEne + hlastB hg_le hF1gt hF1B hF1len' hF2B hF2E hF2len hanc0' + have hFgt : ∀ x ∈ dbF1.keys ++ dbF2.keys, res.keys.getLast hne < x := by + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact hF1gt x hx + · exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le hlast_lt_B (hF2B x hx)) + have hFlt : ∀ x ∈ dbF1.keys ++ dbF2.keys, (x : WithTop Key) < r.end_.key := by + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_lt_of_le (hF1B x hx) hB_le_E + · exact hF2E x hx + have hreslt : ∀ x ∈ res.keys, (x : WithTop Key) < r.end_.key := fun x hx => + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_E + have hFlen : (dbF1.keys ++ dbF2.keys).length = clampNonposD r + 1 := by + rw [List.length_append, hF1len, hF2len, hfdef] + omega + have hkeyw' : dbw.keys = res.keys ++ (dbF1.keys ++ dbF2.keys) := by + rw [hkeyw, List.append_assoc] + have herase := erase_incompatible_of_pad hsort hrev hmore hne hkeyw' hkIn hFgt hreslt + hFlt hFlen hend hEne + refine ⟨dbw, hcompw, ?_, herase⟩ + rw [hkeyw'] + exact List.mem_append.mpr (Or.inr hkIn) + by_cases hkB : (k : WithTop Key) < r.begin.key + · -- k belongs to the first filler + obtain ⟨dbF1, hF1gt, hF1B, hF1len, hkF1⟩ := + exists_dbF1_mem_crossFree (r := r) (res := res) (f := f) (k := k) (b := b) + hne hb hf hroom' hkgt hkB + obtain ⟨dbF2, hF2B, hF2E, hF2len⟩ := + exists_dbF2_crossFree (r := r) (res := res) (g := g) (b := b) (e := e) hb he hcap2 + exact finish dbF1 dbF2 (List.mem_append.mpr (Or.inl hkF1)) hF1gt hF1B hF1len + hF2B hF2E hF2len + · -- k belongs to the second filler + have hBk : r.begin.key ≤ (k : WithTop Key) := not_lt.mp hkB + have hg1 : 1 ≤ g := by + have hge : (1 : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + rw [intervalCard_eq_encard_keysIn, Set.one_le_encard_iff_nonempty] + have hbk : b ≤ k := by + rw [hb] at hBk; exact WithTop.coe_le_coe.mp hBk + have hke : k < e := by + rw [he] at hkE; exact WithTop.coe_lt_coe.mp hkE + exact ⟨k, by rw [hb, he]; exact mem_keysIn_coe.mpr ⟨hbk, hke⟩⟩ + have hcast : (1 : ℕ∞) ≤ ((intervalCard r.begin.key r.end_.key).toNat : ℕ∞) := by + rwa [ENat.natCast_toNat (ne_of_lt hfin)] + have hnat : 1 ≤ (intervalCard r.begin.key r.end_.key).toNat := by exact_mod_cast hcast + rw [hgdef]; exact hnat + obtain ⟨dbF1, hF1gt, hF1B, hF1len⟩ := + exists_dbF1_crossFree (r := r) (res := res) (f := f) (b := b) hne hb hroom' + obtain ⟨dbF2, hF2B, hF2E, hF2len, hkF2⟩ := + exists_dbF2_mem_crossFree (r := r) (res := res) (g := g) (k := k) (b := b) (e := e) + hb he hg1 hcap2 hBk hkE + exact finish dbF1 dbF2 (List.mem_append.mpr (Or.inr hkF2)) hF1gt hF1B hF1len + hF2B hF2E hF2len + +/-- In the clamping branch of the cross-free low-capacity gap cell, a key strictly below +the reported head has an insertion witness. -/ +theorem exists_witness_below_head_crossFree {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hcell : negCrossTruncNonposGapCrossFree r res) + (hbot : clampNonposBottom r res) + (hne : res.keys ≠ []) + (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (toggle dbw k) r res := by + classical + have hcellKeep : negCrossTruncNonposGapCrossFree r res := hcell + obtain ⟨hcell0, hB_le_E⟩ := hcell + obtain ⟨hmore, hrev, hbegin, -, hcrossL, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := hcell0 + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1 + obtain ⟨b, hb⟩ : ∃ b : Key, r.begin.key = ((b : Key) : WithTop Key) := by + cases h : r.begin.key with + | top => + rw [h] at hB_le_E + exact absurd (lt_of_le_of_lt hB_le_E hEne) (lt_irrefl _) + | coe b => exact ⟨b, rfl⟩ + obtain ⟨e, he⟩ : ∃ e : Key, r.end_.key = ((e : Key) : WithTop Key) := by + cases h : r.end_.key with + | top => exact absurd h (ne_of_lt hEne) + | coe e => exact ⟨e, rfl⟩ + have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hle_last : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD]; exact hlastB + have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) hlast_lt_B + have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hk hhead_lt_B + have hk_not_res : k ∉ res.keys := by + intro hx + exact absurd hk (not_lt.mpr + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) + have hrep : reportedBelowBegin r res = res.keys.length := + reportedBelowBegin_eq_length_of_last_lt hne hsort hlastB + set g : ℕ := (intervalCard r.begin.key r.end_.key).toNat with hgdef + have hg_le : g ≤ clampNonposD r := crossFree_g_le hcellKeep + set f : ℕ := clampNonposD r + 1 - g with hfdef + have hf : 1 ≤ f := by rw [hfdef]; omega + have hroom := intervalCard_begin_key_ge_of_crossFree hcomp₀ hcellKeep hne + have hroom' : (f : ℕ∞) ≤ intervalCard + (((keyAfter (res.keys.getLast hne)) : Key) : WithTop Key) r.begin.key := by + rw [hfdef]; exact hroom + have hfin : intervalCard r.begin.key r.end_.key < (⊤ : ℕ∞) := by + have hcap' : intervalCard r.begin.key r.end_.key < + ((clampNonposD r + 1 : ℕ) : ℕ∞) := by + rcases hcap with hcrosslt | hlt + · exact absurd hcrosslt (not_lt.mpr hB_le_E) + · exact hlt + exact lt_of_lt_of_le hcap' (by simp) + have hcap2 : (g : ℕ∞) ≤ intervalCard r.begin.key r.end_.key := by + rw [hgdef, ENat.natCast_toNat (ne_of_lt hfin)] + -- clamping gives `t + f ≤ beginSlack` + have hcount : res.keys.length + f ≤ beginSlack r := by + have h := hbot + unfold clampNonposBottom at h + rw [hrep] at h + have hsub : (((clampNonposD r + 1 : ℕ) : ℕ∞) - (g : ℕ∞)) = (f : ℕ∞) := by + rw [hfdef] + exact (ENat.natCast_sub (clampNonposD r + 1) g).symm + -- replace the cast capacity by `g` + have hgcast : ((intervalCard r.begin.key r.end_.key).toNat : ℕ∞) = + intervalCard r.begin.key r.end_.key := ENat.natCast_toNat (ne_of_lt hfin) + rw [← hgcast, ← hgdef] at h + rw [hsub] at h + exact_mod_cast h + obtain ⟨dbF1, hF1gt, hF1B, hF1len⟩ := + exists_dbF1_crossFree (r := r) (res := res) (f := f) (b := b) hne hb hroom' + obtain ⟨dbF2, hF2B, hF2E, hF2len⟩ := + exists_dbF2_crossFree (r := r) (res := res) (g := g) (b := b) (e := e) hb he hcap2 + have hF1len' : dbF1.keys.length = clampNonposD r + 1 - g := by + rw [hF1len, hfdef] + have hanc0 : res.keys.length + (clampNonposD r + 1 - g) ≤ beginSlack r + 1 := by + rw [hfdef] at hcount; omega + obtain ⟨dbw, hkeyw, hcompw, hanchorE, hanchorB⟩ := + exists_padDb_crossFree hsort hrev hmore hne hlim hbegin hend hB_le_E hEne + hlastB hg_le hF1gt hF1B hF1len' hF2B hF2E hF2len hanc0 + -- the toggled database inserts `k` at the front + have hk_lt_all : ∀ x ∈ dbw.keys, k < x := by + intro x hx + rw [hkeyw] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hk + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx))) + · exact lt_trans (lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk) hhead_le_last) + (hF1gt x hx) + · exact lt_trans (lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk) hhead_le_last) + (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B (hF2B x hx) : + ((res.keys.getLast hne : Key) : WithTop Key) < ((x : Key) : WithTop Key))) + have hk_not_dbw : k ∉ dbw.keys := fun hx => absurd (hk_lt_all k hx) (lt_irrefl k) + refine ⟨dbw, hcompw, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_not_dbw] + intro hcomp' + have hdbw_ne : dbw.keys ≠ [] := by + rw [hkeyw, List.append_assoc]; exact List.append_ne_nil_of_left_ne_nil hne _ + have hins_keys : (dbw.insert k hk_not_dbw).keys = k :: dbw.keys := by + rw [DB.insert_keys] + cases hkw : dbw.keys with + | nil => exact absurd hkw hdbw_ne + | cons a t => + have hka : k ≤ a := le_of_lt (hk_lt_all a (by rw [hkw]; exact List.mem_cons_self ..)) + rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka] + obtain ⟨-, hfilt'⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp' + -- count below begin of the padded database + have hrescnt : dbw.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + res.keys.length + f := by + rw [hkeyw, List.countP_append, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlast_lt_B + have h2 : dbF1.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = dbF1.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hF1B x hx + have h3 : dbF2.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq, not_lt] + exact hF2B x hx + rw [h1, h2, h3, hF1len]; omega + have hcount_ins : (dbw.insert k hk_not_dbw).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = + res.keys.length + f + 1 := by + rw [hins_keys, List.countP_cons, decide_eq_true_eq.mpr hk_lt_B, if_pos rfl, hrescnt] + have hanchor_ins : r.begin.anchor (dbw.insert k hk_not_dbw) ≤ 0 := by + rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount_ins] + have hcast : (res.keys.length + f : ℤ) ≤ (beginSlack r : ℤ) := by + exact_mod_cast hcount + push_cast; omega + have hk_ck : k ∈ RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by rw [hins_keys]; exact List.mem_cons_self .., ?_, ?_⟩ + · by_cases hneg : r.begin.anchor (dbw.insert k hk_not_dbw) < 0 + · rw [resolve_eq_nil hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + · have hzero : r.begin.anchor (dbw.insert k hk_not_dbw) = 0 := by omega + have h0 : 0 ≤ r.begin.anchor (dbw.insert k hk_not_dbw) := by omega + have hlt : (r.begin.anchor (dbw.insert k hk_not_dbw)).toNat < + (dbw.insert k hk_not_dbw).keys.length := by + rw [hzero, hins_keys, List.length_cons]; simp + rw [resolve_eq_getElem h0 hlt] + have hget : (dbw.insert k hk_not_dbw).keys[(r.begin.anchor + (dbw.insert k hk_not_dbw)).toNat] = k := by + simp only [hzero, hins_keys] + rfl + rw [hget] + · have hpre := RangeRead.compatible_keys_prefix hcomp' + have hhead_mem : res.keys.head hne ∈ + RangeRead.completeKeys (dbw.insert k hk_not_dbw) r := + hpre.subset (List.head_mem hne) + exact lt_trans hk (RangeRead.mem_completeKeys.mp hhead_mem).2.2 + have hk_le_last : k ≤ res.keys.getLast hne := + le_trans (le_of_lt (WithTop.coe_lt_coe.mp hk)) hhead_le_last + have hk_filt : k ∈ (RangeRead.completeKeys (dbw.insert k hk_not_dbw) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) := by + rw [List.mem_filter] + exact ⟨hk_ck, by rw [decide_eq_true_eq]; exact hk_le_last⟩ + rw [hfilt'] at hk_filt + exact hk_not_res hk_filt + + +/-- The default head of a list is its head. -/ +theorem list_headD_eq_head {l : List Key} (h : l ≠ []) : l.headD ([] : Key) = l.head h := by + revert h + cases l with + | nil => intro h; exact absurd rfl h + | cons a t => intro h; rfl + +/-- **Minimality of the cross-free nonpositive-end gap closed form.** Both branches of +`closedFormNegCrossTruncNonposGapCrossFree` are sound, and every key strictly below +`end_.key` is necessary: reported keys by their membership, keys between the reported +head and last by convexity, keys above the last by the erasing padding witness, and (in +the clamping branch) keys below the head by the front insertion witness. -/ +theorem minimal_closedForm_of_negCross_trunc_nonposGap_crossFree + {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncNonposGapCrossFree r res) : + Minimal r res (closedFormNegCrossTruncNonposGapCrossFree r res) := by + classical + have hcellKeep : negCrossTruncNonposGapCrossFree r res := hcell + obtain ⟨hcell0, hB_le_E⟩ := hcell + obtain ⟨hmore, hrev, hbegin, hne, hcrossL, hend, hclamp, hlastB, hlastE, hEne, hcap⟩ := + hcell0 + have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hle : leastReported r res = res.keys.head hne := by + simp [leastReported, hrev, List.head?_eq_some_head hne] + rwa [hle] at hcrossL + by_cases hbot : clampNonposBottom r res + · have hclosed : closedFormNegCrossTruncNonposGapCrossFree r res = + (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + unfold closedFormNegCrossTruncNonposGapCrossFree closedFormNegCrossTruncNonposGap + rw [if_pos hbot, max_eq_right hB_le_E] + rw [hclosed] + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomEnd_crossFree hbegin hend hB_le_E, ?_⟩ + intro k _ hkE + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hkhead : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · obtain ⟨dbw, hcompw, hnot⟩ := + exists_witness_below_head_crossFree hread hcellKeep hbot hne hkhead + exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩ + · have hheadk : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := + not_lt.mp hkhead + by_cases hklast : ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hheadk) (WithTop.coe_le_coe.mp hklast) + · have hlastk : ((res.keys.getLast hne : Key) : WithTop Key) < + (k : WithTop Key) := not_le.mp hklast + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_last_end_crossFree hread hcellKeep hne hlastk hkE + exact necessary_of_toggle ⟨dbw, hcompw, + by rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩ + · have hclosed : closedFormNegCrossTruncNonposGapCrossFree r res = + (⟨((res.keys.head hne : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := + list_headD_eq_head hne + unfold closedFormNegCrossTruncNonposGapCrossFree closedFormNegCrossTruncNonposGap + rw [if_neg hbot, max_eq_right hB_le_E] + exact congrArg (fun k : WithTop Key => (⟨k, r.end_.key⟩ : ConflictRange)) + (congrArg (fun k : Key => ((k : Key) : WithTop Key)) hheadD) + rw [hclosed] + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_headEnd_crossFree hrev hmore hne hbegin hhead_lt_B hclamp hbot hend hB_le_E, ?_⟩ + intro k hklo hkE + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hklast : ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hklo) (WithTop.coe_le_coe.mp hklast) + · have hlastk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) := + not_le.mp hklast + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_last_end_crossFree hread hcellKeep hne hlastk hkE + exact necessary_of_toggle ⟨dbw, hcompw, + by rw [toggle_eq_erase_of_mem hkdbw]; exact hnot⟩ + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevBeginTop.lean b/FormalConflictRange/NegCrossTruncRevBeginTop.lean new file mode 100644 index 0000000..f4f5406 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevBeginTop.lean @@ -0,0 +1,333 @@ +/- +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.NegCrossTruncRevFinite + +/-! +# Reversed crossed negative-begin truncated cells with a sentinel begin key + +This module closes the reversed crossed negative-begin truncated cell whose begin +selector targets the sentinel `⊤` and whose end offset is at most one. The minimal +range is `[last reported, ⊤)`, the top range at the reported last key. + +Soundness reuses the reduction to the read whose begin is the reported last at offset +one: the begin-anchor count bound keeps the resolved begin at or below the reported last +under agreement on `[last, ⊤)`, so the complete keys are fixed. + +Necessity is uniform in the key. If a sound range omitted a key `k ≥ last`, then its +end must be finite: a sound range ending at `⊤` contains `last` (a reported key), hence +starts at or below `last ≤ k`, so it cannot omit `k`. With a finite end, inserting +fresh keys above both the range's end and `last` (there are arbitrarily many such keys) +raises the begin anchor past `last` without moving `last` in the database, so the +reported keys stop being a prefix of the complete keys. +-/ + +namespace FormalConflictRange + +/-- Agreement on `[a, ⊤)` restricts to agreement on `[a, b)` for any `b`. -/ +theorem agreeOn_top_narrow {db₁ db₂ : DB} {a b : WithTop Key} + (h : AgreeOn ⟨a, (⊤ : WithTop Key)⟩ db₁ db₂) : AgreeOn ⟨a, b⟩ db₁ db₂ := by + have hp : ∀ k : Key, decide (a ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < (⊤ : WithTop Key)) = + decide (a ≤ ((k : Key) : WithTop Key)) := by + intro k + rw [decide_eq_decide] + exact ⟨fun h => h.1, fun h => ⟨h, WithTop.coe_lt_top k⟩⟩ + have h' : db₁.keys.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key))) = + db₂.keys.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key))) := by + unfold AgreeOn ConflictRange.keys at h + simpa only [hp] using h + have hstep : ∀ l : List Key, + l.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < b)) = + (l.filter (fun k : Key => decide (a ≤ ((k : Key) : WithTop Key)))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < b)) := by + intro l + rw [List.filter_filter] + apply List.filter_congr + intro k _ + simp only [Bool.decide_and, Bool.and_comm] + unfold AgreeOn ConflictRange.keys + rw [hstep db₁.keys, hstep db₂.keys, h'] + +/-- **Soundness of the top range at the reported last in the sentinel-begin reversed +truncated cell.** Agreement on `[last, ⊤)` fixes the begin resolution at or below +`last`, hence fixes the complete keys up to their prefix. -/ +theorem sound_topOfLast_of_negCross_trunc_rev_beginTop {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 1) + (hB : r.begin.key = (⊤ : WithTop Key)) : + Sound r res (topOfLast (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hcross : ((L : Key) : WithTop Key) < r.begin.key := by + rw [hB]; exact WithTop.coe_lt_top L + obtain ⟨db₀, hcomp₀⟩ := hread + let r' : RangeRead := + { r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } } + have hr'_rev : r'.reverse = true := by simp [r', hrev] + have hr'_end : r'.end_ = r.end_ := rfl + have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) := + rfl + have hr'_off : r'.begin.offset = 1 := rfl + have hreduce : ∀ (db : DB), RangeRead.Compatible db r res → + RangeRead.Compatible db r' res := by + intro db hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = + res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hres' : resolve db r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb + have hLck_filter : (RangeRead.completeKeys db r').filter (fun k => decide (L ≤ k)) = + RangeRead.completeKeys db r' := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hk' := (RangeRead.mem_completeKeys.mp hk).2.1 + exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk') + have hck' : RangeRead.completeKeys db r' = res.keys := by + rw [← hLck_filter] + exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) (r₂ := r') + hrev hr'_rev hr'_end hrb (le_of_eq hres')).symm.trans hfil + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck'] + have hread' : Readable r' res := ⟨db₀, hreduce db₀ hcomp₀⟩ + have hgetLastD : res.keys.getLastD ([] : Key) = L := by + rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hmin' : Minimal r' res (truncatedRangeRev r L) := by + rcases lt_or_eq_of_le hend with hlt | heq + · have hle : r.end_.offset ≤ 0 := by omega + have hm := minimal_truncatedRangeRev_of_end_offset_nonpos (r := r') hread' hmore + hr'_rev hr'_off (by simpa [r'] using hle) + rwa [hgetLastD] at hm + · have hm := minimal_truncatedRangeRev_of_offset_one (r := r') hread' hmore + hr'_rev hr'_off heq + rwa [hgetLastD] at hm + have hS' : Sound r' res (truncatedRangeRev r L) := hmin'.sound + have step : ∀ db db', AgreeOn (topOfLast L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hcomp' : RangeRead.Compatible db r' res := hreduce db hcomp + have hag'' : AgreeOn (truncatedRangeRev r L) db db' := agreeOn_top_narrow hag' + have hcomp'' : RangeRead.Compatible db' r' res := (hS' db db' hag'').mp hcomp' + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = + res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hbound : ((between db ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := + between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + -- the agreement, restricted to the stored keys at or above `L` + have hfilEq : db'.keys.filter (fun k => decide (L ≤ k)) = + db.keys.filter (fun k => decide (L ≤ k)) := by + have h := hag' + unfold AgreeOn at h + rw [topOfLast_keys, topOfLast_keys] at h + exact h.symm + have hbetween_eq : between db' ((L : Key) : WithTop Key) r.begin.key = + between db ((L : Key) : WithTop Key) r.begin.key := by + rw [hB] + unfold between + simpa [WithTop.coe_le_coe] using hfilEq + have hbound' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [hbetween_eq]; exact hbound + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLdb' : L ∈ db'.keys := by + have hmem : L ∈ db'.keys.filter (fun k => decide (L ≤ k)) := by + rw [hfilEq] + exact List.mem_filter.mpr ⟨hLdb, by simp⟩ + exact (List.mem_filter.mp hmem).1 + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb' hcross hbound' + have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb' + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp''.1, ?_⟩ + have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) hrev hrb' + have h2' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r') hr'_rev + (le_of_eq hres'_db') + have h2 := (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp + hcomp'' |>.2 + rw [h1, ← h2'] + exact h2 + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Necessity of every key at or above the reported last in the sentinel-begin +reversed truncated cell.** A sound range omitting such a key must end below `⊤`; +inserting fresh keys above its end and above the reported last then breaks the reported +prefix by lifting the begin resolution alone. -/ +theorem necessary_of_ge_last_of_negCross_trunc_rev_beginTop {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (_hbeg : r.begin.offset < 0) + (hB : r.begin.key = (⊤ : WithTop Key)) {k : Key} + (hkL : ((res.keys.getLast hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) : + Necessary r res k := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + intro cr hs + by_contra hkcr + have hread' := hread + obtain ⟨db₀, hcomp₀⟩ := hread + -- a sound range ending at `⊤` contains `last`, hence cannot omit `k ≥ last` + have hLcr : L ∈ cr := necessary_of_mem_result hread' hLmem cr hs + have hL_lt_end : ((L : Key) : WithTop Key) < cr.end_ := (ConflictRange.mem_iff.mp hLcr).2 + have hcr_end_lt_top : cr.end_ < (⊤ : WithTop Key) := by + by_contra h + have htop : cr.end_ = (⊤ : WithTop Key) := le_antisymm le_top (le_of_not_gt h) + have hk_lt_begin : ((k : Key) : WithTop Key) < cr.begin := by + have hnot : ¬ (cr.begin ≤ ((k : Key) : WithTop Key)) := by + intro hb + exact hkcr (ConflictRange.mem_iff.mpr ⟨hb, by rw [htop]; exact WithTop.coe_lt_top k⟩) + exact lt_of_not_ge hnot + have hbegin_le_L : cr.begin ≤ ((L : Key) : WithTop Key) := (ConflictRange.mem_iff.mp hLcr).1 + exact (not_lt_of_ge hkL) (lt_of_lt_of_le hk_lt_begin hbegin_le_L) + -- a key above every stored key + have hexU : ∃ U : Key, ∀ x ∈ db₀.keys, x < U := by + have hlist : ∀ l : List Key, ∃ U : Key, ∀ x ∈ l, x < U := by + intro l + induction l with + | nil => exact ⟨[], by simp⟩ + | cons a t ih => + obtain ⟨U, hU⟩ := ih + refine ⟨max (keyAfter a) U, ?_⟩ + intro x hx + rcases List.mem_cons.mp hx with hxa | hx + · rw [hxa]; exact lt_of_lt_of_le (keyAfter_self_lt a) (le_max_left _ _) + · exact lt_of_lt_of_le (hU x hx) (le_max_right _ _) + exact hlist db₀.keys + obtain ⟨U, hU⟩ := hexU + -- the insertion threshold, above the range end, above `last`, and above all stored keys + set B : WithTop Key := + max (max cr.end_ (((keyAfter L : Key)) : WithTop Key)) ((U : Key) : WithTop Key) with hBdef + have hB_lt_top : B < (⊤ : WithTop Key) := by + rw [hBdef] + rw [max_lt_iff, max_lt_iff] + exact ⟨⟨hcr_end_lt_top, WithTop.coe_lt_top _⟩, WithTop.coe_lt_top U⟩ + obtain ⟨b, hb⟩ : ∃ b : Key, B = ((b : Key) : WithTop Key) := by + cases hBv : B with + | top => rw [hBv] at hB_lt_top; exact absurd le_top (not_le.mpr hB_lt_top) + | coe b => exact ⟨b, rfl⟩ + -- the number of keys to insert + set m : ℕ := (1 - r.begin.offset).toNat with hmdef + have hm_cast : (m : Int) = 1 - r.begin.offset := by + rw [hmdef, Int.toNat_of_nonneg]; omega + have hmpos : 1 ≤ m := by + rw [hmdef] + have : 0 ≤ 1 - r.begin.offset := by omega + omega + obtain ⟨dbJ, hJmem, hJlen⟩ := exists_db_keysIn (B := B) (E := (⊤ : WithTop Key)) + (m := m) (by rw [hb, intervalCard_coe_top]; exact le_top) + have hJge : ∀ x ∈ dbJ.keys, B ≤ ((x : Key) : WithTop Key) := fun x hx => (hJmem x hx).1 + have hJgtL : ∀ x ∈ dbJ.keys, L < x := by + intro x hx + have h1 : (((keyAfter L : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + le_trans (le_trans (le_max_right _ _) (le_max_left _ _)) (hJge x hx) + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt L)) h1) + have hJgt_db : ∀ x ∈ db₀.keys, ∀ y ∈ dbJ.keys, x < y := by + intro x hx y hy + have hxU : x < U := hU x hx + have hUle : ((U : Key) : WithTop Key) ≤ B := le_max_right _ _ + have : ((U : Key) : WithTop Key) ≤ ((y : Key) : WithTop Key) := le_trans hUle (hJge y hy) + exact lt_of_lt_of_le hxU (WithTop.coe_le_coe.mp this) + have hsorted : (db₀.keys ++ dbJ.keys).SortedLT := + List.sortedLT_append.mpr ⟨db₀.sorted, dbJ.sorted, hJgt_db⟩ + let db₂ : DB := ⟨db₀.keys ++ dbJ.keys, hsorted⟩ + have hdb₂_keys : db₂.keys = db₀.keys ++ dbJ.keys := rfl + have hJnotin_cr : ∀ y ∈ dbJ.keys, y ∉ cr := by + intro y hy hycr + have hyge : cr.end_ ≤ ((y : Key) : WithTop Key) := + le_trans (le_trans (le_max_left _ _) (le_max_left _ _)) (hJge y hy) + exact absurd (ConflictRange.mem_iff.mp hycr).2 (not_lt.mpr hyge) + have hagree : AgreeOn cr db₀ db₂ := by + unfold AgreeOn ConflictRange.keys + rw [hdb₂_keys, List.filter_append] + have hnil : dbJ.keys.filter (fun k : Key => decide (cr.begin ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < cr.end_)) = [] := by + apply List.filter_eq_nil_iff.mpr + intro y hy + have hnot : ¬ (cr.begin ≤ ((y : Key) : WithTop Key) ∧ + ((y : Key) : WithTop Key) < cr.end_) := by + simpa only [ConflictRange.mem_iff] using hJnotin_cr y hy + simpa only [decide_eq_true_eq] using hnot + rw [hnil, List.append_nil] + -- the inserted database is incompatible: the begin resolves above `last` + have hnotcomp : ¬ RangeRead.Compatible db₂ r res := by + intro hcomp₂ + have hfil₂ : (RangeRead.completeKeys db₂ r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db₂) hrev hmore hne).mp hcomp₂ |>.2 + have hLck : L ∈ RangeRead.completeKeys db₂ r := by + have : L ∈ (RangeRead.completeKeys db₂ r).filter (fun k => decide (L ≤ k)) := by + rw [hfil₂]; exact hLmem + exact (List.mem_filter.mp this).1 + have hrb₂ : resolve db₂ r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLck).2.1 + have hcnt : cnt db₂ r.begin.key = db₀.keys.length + m := by + rw [hB] + unfold cnt + rw [List.countP_eq_length.mpr (by intro x _; simp)] + rw [hdb₂_keys, List.length_append, hJlen] + have hanchor : r.begin.anchor db₂ = (db₀.keys.length : Int) := by + change (cnt db₂ r.begin.key : Int) + r.begin.offset - 1 = (db₀.keys.length : Int) + rw [hcnt] + push_cast [hm_cast] + ring + have hlen₂ : db₂.keys.length = db₀.keys.length + m := by + rw [hdb₂_keys, List.length_append, hJlen] + have hanchor_nonneg : 0 ≤ r.begin.anchor db₂ := by rw [hanchor]; exact Int.natCast_nonneg _ + have hlt : (r.begin.anchor db₂).toNat < db₂.keys.length := by + rw [hanchor, hlen₂] + simp only [Int.toNat_natCast] + omega + have hresolve : resolve db₂ r.begin = + ((db₂.keys[db₀.keys.length] : Key) : WithTop Key) := by + rw [resolve_eq_getElem hanchor_nonneg hlt] + have hidx : (r.begin.anchor db₂).toNat = db₀.keys.length := by + rw [hanchor]; simp + simp only [hidx] + have hget_gt : L < db₂.keys[db₀.keys.length] := by + have hpos : 0 < dbJ.keys.length := by rw [hJlen]; omega + have hsplit : db₂.keys[db₀.keys.length] = dbJ.keys[0] := by + simp only [hdb₂_keys] + simp + rw [hsplit] + exact hJgtL (dbJ.keys[0]'hpos) (List.getElem_mem hpos) + have hnotle : ¬ (resolve db₂ r.begin ≤ ((L : Key) : WithTop Key)) := by + rw [hresolve] + exact not_le.mpr (WithTop.coe_lt_coe.mpr hget_gt) + exact hnotle hrb₂ + exact hnotcomp ((hs db₀ db₂ hagree).mp hcomp₀) + +/-- **The reversed crossed negative-begin truncated cell with a sentinel begin key.** -/ +abbrev negCrossTruncRevBeginTop (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + r.end_.offset ≤ 1 ∧ r.begin.key = (⊤ : WithTop Key) ∧ r.end_.key < (⊤ : WithTop Key) + +/-- The closed form of the sentinel-begin reversed truncated cell: `[last, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncRevBeginTop (_r : RangeRead) (res : Result) : + ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The sentinel-begin reversed truncated closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_beginTop {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevBeginTop r res) : + Minimal r res (closedFormNegCrossTruncRevBeginTop r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hend, hB, _hE⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + unfold closedFormNegCrossTruncRevBeginTop + rw [hlast] + refine minimal_iff_sound_and_necessary.mpr + ⟨sound_topOfLast_of_negCross_trunc_rev_beginTop hread hmore hrev hne hend hB, ?_⟩ + intro k hk + exact necessary_of_ge_last_of_negCross_trunc_rev_beginTop hread hmore hrev hne hbeg hB hk.1 + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevBeginTopGeTwo.lean b/FormalConflictRange/NegCrossTruncRevBeginTopGeTwo.lean new file mode 100644 index 0000000..cd40899 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevBeginTopGeTwo.lean @@ -0,0 +1,163 @@ +/- +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.NegCrossTruncRevBeginTop + +/-! +# Sentinel-begin reversed truncated cells at end offset at least two + +This module extends the sentinel-begin reversed truncated cell +(`r.begin.key = ⊤`) from end offsets at most one to every end offset at least two, +under the extra hypothesis that every reported key lies strictly below `r.end_.key`. + +* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`; +* `begin.key = ⊤`; +* `2 ≤ end_.offset`, with every reported key strictly below `end_.key`. + +The minimal range is again `[last reported, ⊤)`, the top range at the reported last key. + +Soundness is the sentinel-begin argument with the overshoot: the reported keys are a +prefix of an unbounded window, so agreement on `[last, ⊤)` fixes the stored keys at or +above `last` and keeps the begin resolution at or below it. Necessity is the +offset-agnostic `necessary_of_ge_last_of_negCross_trunc_rev_beginTop`: every key at or +above the reported last is necessary. +-/ + +namespace FormalConflictRange + +/-- **Soundness of the top range at the reported last for a sentinel begin and an +end offset of at least two.** The overshoot hypothesis makes the reduced read's +window unbounded, and the sentinel begin keeps the resolved begin at or below the +reported last under agreement on `[last, ⊤)`. -/ +theorem sound_topOfLast_of_negCross_trunc_rev_beginTop_ge_two {r : RangeRead} + {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) + (hB : r.begin.key = (⊤ : WithTop Key)) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Sound r res (topOfLast (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hcross : ((L : Key) : WithTop Key) < r.begin.key := by + rw [hB]; exact WithTop.coe_lt_top L + obtain ⟨db₀, hcomp₀⟩ := hread + let r' : RangeRead := + { r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } } + have hr'_rev : r'.reverse = true := by simp [r', hrev] + have hr'_end : r'.end_ = r.end_ := rfl + have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) := + rfl + have hr'_off : r'.begin.offset = 1 := rfl + have hreduce : ∀ (db : DB), RangeRead.Compatible db r res → + RangeRead.Compatible db r' res := by + intro db hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = + res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hres' : resolve db r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb + have hLck_filter : (RangeRead.completeKeys db r').filter (fun k => decide (L ≤ k)) = + RangeRead.completeKeys db r' := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hk' := (RangeRead.mem_completeKeys.mp hk).2.1 + exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk') + have hck' : RangeRead.completeKeys db r' = res.keys := by + rw [← hLck_filter] + exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) (r₂ := r') + hrev hr'_rev hr'_end hrb (le_of_eq hres')).symm.trans hfil + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck'] + have hread' : Readable r' res := ⟨db₀, hreduce db₀ hcomp₀⟩ + have hgetLastD : res.keys.getLastD ([] : Key) = L := by + rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hmin' : Minimal r' res (topOfLast L) := by + have hm := minimal_topOfLast_of_end_offset_ge_two_rev (r := r') hread' hmore + hr'_rev hr'_off hend (by simpa [r'] using hhi) + rwa [hgetLastD] at hm + have hS' : Sound r' res (topOfLast L) := hmin'.sound + have step : ∀ db db', AgreeOn (topOfLast L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hcomp' : RangeRead.Compatible db r' res := hreduce db hcomp + have hcomp'' : RangeRead.Compatible db' r' res := (hS' db db' hag').mp hcomp' + have hbound : ((between db ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := + between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + -- the agreement, restricted to the stored keys at or above `L` + have hfilEq : db'.keys.filter (fun k => decide (L ≤ k)) = + db.keys.filter (fun k => decide (L ≤ k)) := by + have h := hag' + unfold AgreeOn at h + rw [topOfLast_keys, topOfLast_keys] at h + exact h.symm + have hbetween_eq : between db' ((L : Key) : WithTop Key) r.begin.key = + between db ((L : Key) : WithTop Key) r.begin.key := by + rw [hB] + unfold between + simpa [WithTop.coe_le_coe] using hfilEq + have hbound' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [hbetween_eq]; exact hbound + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLdb' : L ∈ db'.keys := by + have hmem : L ∈ db'.keys.filter (fun k => decide (L ≤ k)) := by + rw [hfilEq] + exact List.mem_filter.mpr ⟨hLdb, by simp⟩ + exact (List.mem_filter.mp hmem).1 + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb' hcross hbound' + have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb' + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp''.1, ?_⟩ + have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) hrev hrb' + have h2' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r') hr'_rev + (le_of_eq hres'_db') + have h2 := (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp + hcomp'' |>.2 + rw [h1, ← h2'] + exact h2 + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- The sentinel-begin reversed truncated cell with end offset at least two and every +reported key below the end key. -/ +abbrev negCrossTruncRevBeginTopGeTwo (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + r.begin.key = (⊤ : WithTop Key) ∧ 2 ≤ r.end_.offset ∧ + r.end_.key < (⊤ : WithTop Key) ∧ + ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key + +/-- The closed form of the sentinel-begin reversed truncated overshoot cell: +`[last reported, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncRevBeginTopGeTwo (_r : RangeRead) + (res : Result) : ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The sentinel-begin reversed truncated overshoot closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_beginTop_ge_two {r : RangeRead} + {res : Result} (hread : Readable r res) (h : negCrossTruncRevBeginTopGeTwo r res) : + Minimal r res (closedFormNegCrossTruncRevBeginTopGeTwo r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hB, hend, _hEtop, hhi⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + unfold closedFormNegCrossTruncRevBeginTopGeTwo + rw [hlast] + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_topOfLast_of_negCross_trunc_rev_beginTop_ge_two hread hmore hrev hne hend hB + hhi, ?_⟩ + intro k hk + exact necessary_of_ge_last_of_negCross_trunc_rev_beginTop hread hmore hrev hne hbeg hB + (k := k) (ConflictRange.mem_iff.mp hk).1 + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevBelowEnd.lean b/FormalConflictRange/NegCrossTruncRevBelowEnd.lean new file mode 100644 index 0000000..171e40c --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevBelowEnd.lean @@ -0,0 +1,179 @@ +/- +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.NegCrossTruncRevBeginTop + +/-! +# Reversed truncated cells with every reported key below the end key + +This module closes the reversed negative-begin truncated cell at end offset at least +two whose reported keys all lie strictly below `end_.key`, for an arbitrary begin key +(not only the sentinel `⊤`). + +* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`; +* `((res.keys.getLastD []) : WithTop Key) < begin.key` (the reported last is below the + begin key); +* `2 ≤ end_.offset`; +* every reported key is strictly below `end_.key`. + +The minimal range is `[last reported, ⊤)`, the top range at the reported last key. + +The key observation is that at end offset at least two, the end resolution is the +sentinel `⊤` in every compatible database. Consequently compatibility is unchanged by +replacing `end_.key` with `⊤`, and the closed form transfers from the sentinel-end cell +`negCrossTruncRevTop`. +-/ + +namespace FormalConflictRange + +/-- Compatibility depends on the read only through its complete keys and its limit. -/ +private theorem compatible_congr_of_completeKeys {db : DB} {r r' : RangeRead} {res : Result} + (hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r') + (hlim : r.limit = r'.limit) : + RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := by + unfold RangeRead.Compatible RangeRead.withinLimit + rw [hck, hlim] + +/-- The last element of a strictly increasing list is at or above every element. -/ +private theorem pairwise_le_getLast' {l : List Key} (h : l.Pairwise (· < ·)) {a : Key} + (ha : a ∈ l) {hne : l ≠ []} : a ≤ l.getLast hne := + (h.imp le_of_lt).rel_getLast ha + +/-- For a reversed read, the reported head is the window's last element. -/ +private theorem head?_eq_getLast?_of_prefix_rev' {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hpre : res.keys <+: RangeRead.completeKeys db r) + (hne : res.keys ≠ []) : + res.keys.head? = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast? := by + have hck : RangeRead.completeKeys db r = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by + simp [RangeRead.completeKeys, hrev] + obtain ⟨t, ht⟩ := hpre + rw [← ht] at hck + calc res.keys.head? = (res.keys ++ t).head? := (List.head?_append_of_ne_nil _ hne).symm + _ = (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse.head? := by + rw [hck] + _ = (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast? := + List.head?_reverse + +/-- In a reversed read, every complete key is at or below the reported head. -/ +private theorem le_head_of_mem_completeKeys_rev' {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hpre : res.keys <+: RangeRead.completeKeys db r) + (hne : res.keys ≠ []) {k : Key} (hk : k ∈ RangeRead.completeKeys db r) : + k ≤ res.keys.head hne := by + have hck : RangeRead.completeKeys db r = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by + simp [RangeRead.completeKeys, hrev] + have hWne : RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) ≠ [] := + fun h => hpre.ne_nil hne (hck.trans (List.reverse_eq_nil_iff.mpr h)) + rw [hck, List.mem_reverse] at hk + have h1 : k ≤ (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast + hWne := pairwise_le_getLast' (RangeRead.inRange_pairwise db _ _) hk + have h2 : res.keys.head hne = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast hWne := by + refine (List.head_eq_iff_head?_eq_some hne).mpr ?_ + rw [head?_eq_getLast?_of_prefix_rev' hrev hpre hne, + List.getLast?_eq_getLast_of_ne_nil hWne] + rw [h2] + exact h1 + +/-- **At end offset at least two with every reported key below `end_.key`, the end +resolution is the sentinel.** A compatible window whose reported keys all lie below +`end_.key` cannot contain a stored key at or above `end_.key`: such a key would be the +window's largest element and hence the reported head, contradicting the hypothesis. -/ +theorem resolve_end_eq_top_of_below_compat {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) + (hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + resolve db r.end_ = (⊤ : WithTop Key) := by + refine resolve_end_eq_top_of_hhi hcomp hend hbelow (res.keys.head hne) + (List.head_mem hne) (fun k hk => ?_) + exact le_head_of_mem_completeKeys_rev' hrev (RangeRead.compatible_keys_prefix hcomp) hne hk + +/-- **Replacing the end key by the sentinel preserves compatibility.** At end offset at +least two with every reported key below `end_.key`, every compatible database resolves +the end selector to `⊤`, so the complete keys — and hence compatibility — are unchanged +by moving the end key to `⊤`. Conversely, a database compatible with the sentinel-end +read cannot store a key at or above `end_.key`: the reported keys are the largest stored +keys at or above the resolved begin, and the reported head is below `end_.key`. -/ +theorem compatible_iff_topEnd_of_below {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : 2 ≤ r.end_.offset) + (hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res := by + let r' : RangeRead := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } + have hrev' : r'.reverse = true := by simp [r', hrev] + constructor + · intro hcomp + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_below_compat hcomp hrev hne hend hbelow + have hre' : resolve db r'.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top (db := db) (s := r'.end_) rfl (by simp [r']; omega) + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp only [RangeRead.completeKeys, RangeRead.inRange, hrev, hrev', hre, hre'] + rfl + exact (compatible_congr_of_completeKeys hck (rfl : r.limit = r'.limit)).mp hcomp + · intro hcomp' + have hpre' : res.keys <+: RangeRead.completeKeys db r' := + RangeRead.compatible_keys_prefix hcomp' + have hre' : resolve db r'.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top (db := db) (s := r'.end_) rfl (by simp [r']; omega) + have hHmem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hrbH : resolve db r'.begin ≤ ((res.keys.head hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (hpre'.subset hHmem)).2.1 + have hHE : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := hbelow _ hHmem + have hall : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_contra hge + have hge' : r.end_.key ≤ ((k : Key) : WithTop Key) := not_lt.mp hge + have hkrb : resolve db r'.begin ≤ ((k : Key) : WithTop Key) := + le_trans hrbH (le_trans (le_of_lt hHE) hge') + have hkck : k ∈ RangeRead.completeKeys db r' := + RangeRead.mem_completeKeys.mpr ⟨hk, hkrb, by rw [hre']; exact WithTop.coe_lt_top k⟩ + have hmax := le_head_of_mem_completeKeys_rev' hrev' hpre' hne hkck + exact absurd (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hmax) hHE) (not_lt.mpr hge') + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hall hend + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp only [RangeRead.completeKeys, RangeRead.inRange, hrev, hrev', hre, hre'] + rfl + exact (compatible_congr_of_completeKeys hck (rfl : r.limit = r'.limit)).mpr hcomp' + +/-- The reversed negative-begin truncated cell at end offset at least two with every +reported key below the end key. -/ +abbrev negCrossTruncRevBelowEnd (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + 2 ≤ r.end_.offset ∧ + ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key + +/-- The closed form of the reversed truncated below-end cell: `[last reported, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncRevBelowEnd (_r : RangeRead) (res : Result) : + ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The reversed truncated below-end closed form is minimal.** The compatibility +reduction to the sentinel-end read transfers minimality from +`minimal_closedForm_of_negCross_trunc_rev_top`. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_belowEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevBelowEnd r res) : + Minimal r res (closedFormNegCrossTruncRevBelowEnd r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hend, hbelow⟩ := h + let r' : RangeRead := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } + have hcompiff : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := + fun db => compatible_iff_topEnd_of_below (db := db) hrev hne hend hbelow + have hread' : Readable r' res := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (hcompiff db).mp hcomp⟩ + have hcell' : negCrossTruncRevTop r' res := by + refine ⟨hmore, by simp [r', hrev], by simp [r', hbeg], hne, ?_, rfl⟩ + simpa [r'] using hcrossD + have hmin' : Minimal r' res (closedFormNegCrossTruncRevBelowEnd r res) := + minimal_closedForm_of_negCross_trunc_rev_top hread' hcell' + exact (minimal_congr_of_compatible_iff (r := r) (r' := r') (res := res) + (cr := closedFormNegCrossTruncRevBelowEnd r res) hcompiff).mpr hmin' + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevCrossedLarge.lean b/FormalConflictRange/NegCrossTruncRevCrossedLarge.lean new file mode 100644 index 0000000..85fa4fa --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevCrossedLarge.lean @@ -0,0 +1,479 @@ +/- +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.NegCrossTruncRevCrossedSmall + +/-! +# Reversed crossed negative-begin truncated cells with a large crossed gap + +This module closes the large-capacity companion of the small-gap cell. The setting is + +* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`; +* the crossed selectors `end_.key < begin.key`, with the reported last `L < begin.key`; +* `end_.offset = 1`; +* `beginSlack + 2 ≤ intervalCard end_.key begin.key + |res.keys|`. + +The minimal range is `[L, begin.key)`. + +The soundness direction is the same count identity as the small-gap cell: agreement on +`[L, begin.key)` fixes the stored keys in `[L, end_.key)` (the reported keys) and the +stored keys in `[end_.key, begin.key)`, so the unresolved begin anchor keeps the same +position relative to `L`. Necessity is new: every key in `[end_.key, begin.key)` must +be in every sound range. The capacity condition supplies enough room in the gap for a +witness database that stores `beginSlack + 2 - |res.keys|` keys there, which pushes the +begin resolution past `L` and breaks the reported prefix. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1200000 + +/-- **The inner double filter of the reversed complete keys is the stored keys in +`[L, end_.key)`.** At end offset one, a stored key lies below the end resolution exactly +when it lies below `end_.key`. -/ +theorem filter_ge_filter_lt_resolve_end_eq_between {db : DB} {r : RangeRead} + (hend : r.end_.offset = 1) (L : Key) : + (db.keys.filter (fun k : Key => decide (L ≤ k))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = + between db ((L : Key) : WithTop Key) r.end_.key := by + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k hk + have hL : decide (L ≤ k) = decide (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := by + rw [decide_eq_decide] + exact WithTop.coe_le_coe.symm + rw [hL, Bool.and_comm] + conv_rhs => rw [Bool.decide_and] + congr 1 + rw [decide_eq_decide] + constructor + · intro h + exact lt_end_key_of_lt_resolve_end_one hend hk h + · intro h + exact lt_of_lt_of_le h (key_le_resolve_of_one_le (db := db) (by omega)) + +/-- **The reported keys of a compatible reversed truncated result are the stored keys +in `[last, end_.key)` read backwards.** -/ +theorem res_keys_eq_reverse_between_of_truncated_rev {db : DB} {r : RangeRead} + {res : Result} (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset = 1) : + res.keys = (between db ((res.keys.getLast hne : Key) : WithTop Key) r.end_.key).reverse := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfil : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hLck : L ∈ RangeRead.completeKeys db r := (List.mem_filter.mp hLfil).1 + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLck).2.1 + have hid := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) (last := L) hrev hrb + have hinner := filter_ge_filter_lt_resolve_end_eq_between (db := db) (r := r) (L := L) hend + rw [hfil] at hid + rw [hid, hinner] + +/-- **Soundness of the begin-key range in the large-gap reversed crossed cell.** +Agreement on `[last, begin.key)` preserves compatibility. -/ +theorem sound_beginKeyRange_of_negCross_crossedLarge {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1) : + Sound r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross + have cle : L < r.end_.key := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + have hck := hpre.subset hLmem + exact lt_end_key_of_lt_resolve_end_one hend (RangeRead.mem_completeKeys.mp hck).1 + (RangeRead.mem_completeKeys.mp hck).2.2 + have step : ∀ db db', AgreeOn ⟨((L : Key) : WithTop Key), r.begin.key⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hbetweenEq : between db ((L : Key) : WithTop Key) r.begin.key = + between db' ((L : Key) : WithTop Key) r.begin.key := by + have h := hag' + unfold AgreeOn ConflictRange.keys at h + unfold between + exact h + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLdb' : L ∈ db'.keys := by + have hmem : L ∈ between db ((L : Key) : WithTop Key) r.begin.key := by + unfold between + exact List.mem_filter.mpr ⟨hLdb, by simp [hLcross]⟩ + rw [hbetweenEq] at hmem + exact (List.mem_filter.mp hmem).1 + have hbound := between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + have hbound' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [← hbetweenEq]; exact hbound + have hres' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb' hLcross hbound' + have hbetweenE : between db ((L : Key) : WithTop Key) r.end_.key = + between db' ((L : Key) : WithTop Key) r.end_.key := + between_eq_of_agree (db := db) (db' := db') (last := ((L : Key) : WithTop Key)) + (beginKey := r.end_.key) (endKey := r.begin.key) hag' (le_of_lt cle) (le_of_lt hcrossE) + have hresBetween := res_keys_eq_reverse_between_of_truncated_rev (db := db) (r := r) + (res := res) hcomp hmore hrev hne hend + have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) (last := L) hrev hres' + have h2 := filter_ge_filter_lt_resolve_end_eq_between (db := db') (r := r) (L := L) hend + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + refine ⟨by simpa [RangeRead.withinLimit] using hcomp.1, ?_⟩ + rw [← hLdef] + rw [h1, h2, ← hbetweenE] + rw [hLdef] + rw [← hresBetween] + intro db₁ db₂ hag + exact ⟨step db₁ db₂ hag, step db₂ db₁ hag.symm⟩ + +/-- **The reported length is at most `beginSlack + 1`.** The reported keys are the +stored keys in `[last, end_.key)`, which is contained in `[last, begin.key)`, and the +begin anchor bound caps that interval at `beginSlack + 1`. -/ +theorem res_length_le_beginSlack_succ_of_negCross_trunc_rev {r : RangeRead} {res : Result} + {db : DB} (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1) : + res.keys.length ≤ beginSlack r + 1 := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLck : L ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset hLmem + have hLltE : ((L : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_one hend (RangeRead.mem_completeKeys.mp hLck).1 + (RangeRead.mem_completeKeys.mp hLck).2.2 + have ht_eq : res.keys.length = + (between db ((L : Key) : WithTop Key) r.end_.key).length := by + rw [res_keys_eq_reverse_between_of_truncated_rev (db := db) (r := r) (res := res) + hcomp hmore hrev hne hend, List.length_reverse] + have hsplit := between_length_add (db := db) (L := ((L : Key) : WithTop Key)) + (E := r.end_.key) (B := r.begin.key) (le_of_lt hLltE) (le_of_lt hcrossE) + have hlen_le : (between db ((L : Key) : WithTop Key) r.end_.key).length ≤ + (between db ((L : Key) : WithTop Key) r.begin.key).length := by + rw [hsplit]; omega + have hbound := between_add_offset_le_one (db := db) hcomp hmore hrev hne + (by rw [← hLdef]; exact hcross) + rw [← hLdef] at hbound + have hs : (beginSlack r : ℤ) = -r.begin.offset := by + unfold beginSlack + rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ 0 - r.begin.offset by omega)] + ring + have h1 : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) + 1 := by + have h2 : (res.keys.length : ℤ) = + ((between db ((L : Key) : WithTop Key) r.end_.key).length : ℤ) := by + exact_mod_cast ht_eq + have h3 : ((between db ((L : Key) : WithTop Key) r.end_.key).length : ℤ) ≤ + ((between db ((L : Key) : WithTop Key) r.begin.key).length : ℤ) := by + exact_mod_cast hlen_le + omega + exact_mod_cast h1 + +/-- Inserting a key above every element of a prefix leaves the prefix untouched. -/ +theorem orderedInsert_append_of_forall_lt_left {l r : List Key} {k : Key} + (h : ∀ x ∈ l, x < k) : + (l ++ r).orderedInsert (· ≤ ·) k = l ++ r.orderedInsert (· ≤ ·) k := by + induction l with + | nil => simp + | cons a l ih => + have hka : a < k := h a (by simp) + rw [List.cons_append, List.orderedInsert_cons, ite_eq_right (not_le.mpr hka)] + rw [ih (fun x hx => h x (by simp [hx]))] + rfl + +/-- In a strictly increasing list, the least key (which is the first entry) is any +element that is at most every element and itself appears in the list. -/ +theorem getElem_zero_eq_of_forall_le {l : List Key} (hs : l.SortedLT) {L : Key} + (hmin : ∀ x ∈ l, L ≤ x) (hL : L ∈ l) (h0 : 0 < l.length) : + l[(0 : ℕ)] = L := by + obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hL + have h1 : l[(0 : ℕ)] ≤ l[j] := + hs.getElem_le_getElem_iff.mpr (Nat.zero_le j) + rw [hjx] at h1 + have h2 : L ≤ l[(0 : ℕ)] := hmin _ (List.getElem_mem h0) + exact le_antisymm h1 h2 + +/-- **Gap-key necessity in the large-gap reversed crossed truncated cell.** The witness +database stores the reported keys in ascending order together with `beginSlack + 1 - t` +gap keys in `[end_.key, begin_.key)`, so its begin anchor is exactly zero and it resolves +onto the reported last. Inserting the target raises the anchor to one: the reported last +leaves the window and the reported keys can no longer be a prefix of the complete keys. +The capacity condition supplies the `beginSlack + 2 - t` distinct gap keys (the witness +keys plus the target) needed. -/ +theorem necessary_of_mem_gap_of_negCross_trunc_rev_crossedLarge {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1) + (hlarge : (beginSlack r : ℕ∞) + 2 ≤ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞)) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set L : Key := res.keys.getLast hne with hLdef + set t : ℕ := res.keys.length with htdef + set s : ℕ := beginSlack r with hsdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hreported_lt_E : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + have hxck : x ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hx + exact lt_end_key_of_lt_resolve_end_one hend (RangeRead.mem_completeKeys.mp hxck).1 + (RangeRead.mem_completeKeys.mp hxck).2.2 + have hLltE : ((L : Key) : WithTop Key) < r.end_.key := hreported_lt_E L hLmem + have ht_le : t ≤ s + 1 := by + rw [htdef, hsdef] + exact res_length_le_beginSlack_succ_of_negCross_trunc_rev hcomp₀ hmore hrev hne + hbeg hcross hcrossE hend + -- enough room in the crossed gap for the witness keys and the target + have hcard : (((s + 1 - t) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + cases hcap : intervalCard r.end_.key r.begin.key with + | top => exact le_top + | coe n => + have hnat : s + 2 ≤ n + t := by + have h' : ((s + 2 : ℕ) : ℕ∞) ≤ ((n + t : ℕ) : ℕ∞) := by + rw [hcap] at hlarge + simpa [ENat.natCast_add] using hlarge + exact_mod_cast h' + have hn : (s + 1 - t) + 1 ≤ n := by omega + exact_mod_cast hn + obtain ⟨dbG, hGrange, hkG, hGlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) + (m := s + 1 - t) hcard + set G : List Key := dbG.keys with hGdef + have hGmem : ∀ x ∈ G, r.end_.key ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.begin.key := by + intro x hx + simpa only [G, keysIn, Set.mem_ofPred_eq] using hGrange x (by simpa [hGdef] using hx) + have hGlen' : G.length = s + 1 - t := by rw [hGdef]; exact hGlen + -- assemble the ascending witness database + have hresSort : res.keys.reverse.SortedLT := hsort.reverse + have hGsorted : G.SortedLT := by rw [hGdef]; exact dbG.sorted + have hcrossG : ∀ x ∈ res.keys.reverse, ∀ y ∈ G, x < y := by + intro x hx y hy + have hx' : x ∈ res.keys := List.mem_reverse.mp hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_E x hx') (hGmem y hy).1) + have hkeysSorted : (res.keys.reverse ++ G).SortedLT := + List.sortedLT_append.mpr ⟨hresSort, hGsorted, hcrossG⟩ + let db_a : DB := ⟨res.keys.reverse ++ G, hkeysSorted⟩ + have hdb_a : db_a.keys = res.keys.reverse ++ G := rfl + have hcntB : db_a.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = t + G.length := by + rw [hdb_a, List.countP_append, List.countP_reverse] + have h1 : res.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = t := by + rw [htdef, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq] + exact lt_trans (hreported_lt_E x hx) hcrossE + have h2 : G.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = G.length := by + rw [List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact (hGmem x hx).2 + rw [h1, h2] + have hanchor : r.begin.anchor db_a = 0 := by + rw [anchor_eq_count_sub_slack db_a r (le_of_lt hbeg), hcntB, hGlen'] + have hts : t + (s + 1 - t) = s + 1 := Nat.add_sub_of_le ht_le + rw [hts]; push_cast; ring + have h0lt : (0 : ℕ) < db_a.keys.length := by + rw [hdb_a, List.length_append, List.length_reverse] + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + omega + have hmin_a : ∀ x ∈ db_a.keys, L ≤ x := by + intro x hx + rw [hdb_a] at hx + rcases List.mem_append.mp hx with hxr | hxG + · exact hLle x (List.mem_reverse.mp hxr) + · exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hLltE (hGmem x hxG).1)) + have hLmem_a : L ∈ db_a.keys := by + rw [hdb_a]; exact List.mem_append_left _ (List.mem_reverse.mpr hLmem) + have hget0 : db_a.keys[(0 : ℕ)] = L := + getElem_zero_eq_of_forall_le hkeysSorted hmin_a hLmem_a h0lt + have hresBegin : resolve db_a r.begin = ((L : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db_a := by rw [hanchor] + have hlt : (r.begin.anchor db_a).toNat < db_a.keys.length := by + rw [hanchor]; simpa using h0lt + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanchor]; rfl + simpa [hidx] using hget0 + have hresEnd_ge : r.end_.key ≤ resolve db_a r.end_ := + key_le_resolve_of_one_le (db := db_a) (s := r.end_) (by omega) + have hfilter : db_a.keys.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by + rw [hdb_a, List.filter_append] + have h1 : (res.keys.reverse).filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (hLle x (List.mem_reverse.mp hx)), + lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx)) hresEnd_ge⟩ + have h2 : G.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db_a r.end_)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hP + have hxmem : x ∈ db_a.keys := by rw [hdb_a]; exact List.mem_append_right _ hx + have hrele : resolve db_a r.end_ ≤ (x : WithTop Key) := + (key_le_resolve_iff_of_offset_one (db := db_a) (s := r.end_) hend hxmem).mp + (hGmem x hx).1 + exact not_lt.mpr hrele hP.2 + rw [h1, h2, List.append_nil] + have hcomplete : RangeRead.completeKeys db_a r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev] + simp only [ite_true] + rw [hresBegin, hfilter, List.reverse_reverse] + have hcompat_a : RangeRead.Compatible db_a r res := + ⟨hcomp₀.1, Or.inl ⟨hmore, by rw [hcomplete], hne⟩⟩ + have hk_a : k ∉ db_a.keys := by + rw [hdb_a] + intro hmem + rcases List.mem_append.mp hmem with hx | hx + · exact absurd (hreported_lt_E k (List.mem_reverse.mp hx)) (not_lt.mpr hkE) + · exact hkG (by simpa [hGdef] using hx) + -- insert the target: the first two sorted keys are the reported last and something above + let db_b : DB := db_a.insert k hk_a + have hins : (res.keys.reverse ++ G).orderedInsert (· ≤ ·) k = + res.keys.reverse ++ G.orderedInsert (· ≤ ·) k := by + refine orderedInsert_append_of_forall_lt_left ?_ + intro x hx + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx)) hkE) + have hb_keys : db_b.keys = res.keys.reverse ++ G.orderedInsert (· ≤ ·) k := by + rw [show db_b.keys = db_a.keys.orderedInsert (· ≤ ·) k from rfl, hdb_a, hins] + have hb_len : db_b.keys.length = s + 2 := by + rw [hb_keys, List.length_append, List.length_reverse, List.orderedInsert_length, + hGlen', htdef, hsdef] + omega + have hb_all : ∀ x ∈ db_b.keys, + decide (((x : Key) : WithTop Key) < r.begin.key) = true := by + intro x hx + rw [decide_eq_true_eq] + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hx' | hx' + · exact lt_trans (hreported_lt_E x (List.mem_reverse.mp hx')) hcrossE + · rw [List.mem_orderedInsert] at hx' + rcases hx' with rfl | hxG + · exact hkB + · exact (hGmem x hxG).2 + have hb_cntB : db_b.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = s + 2 := by + rw [← hb_len, List.countP_eq_length] + exact hb_all + have hanchor_b : r.begin.anchor db_b = 1 := by + rw [anchor_eq_count_sub_slack db_b r (le_of_lt hbeg), hb_cntB]; push_cast; ring + have hget0_b : db_b.keys[(0 : ℕ)] = L := by + have hmin_b : ∀ x ∈ db_b.keys, L ≤ x := by + intro x hx + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hxr | hxG + · exact hLle x (List.mem_reverse.mp hxr) + · rw [List.mem_orderedInsert] at hxG + rcases hxG with rfl | hxG + · exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hLltE hkE)) + · exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hLltE (hGmem x hxG).1)) + have hLmem_b : L ∈ db_b.keys := by + rw [hb_keys]; exact List.mem_append_left _ (List.mem_reverse.mpr hLmem) + have h0lt_b : 0 < db_b.keys.length := by rw [hb_len]; omega + exact getElem_zero_eq_of_forall_le db_b.sorted hmin_b hLmem_b h0lt_b + have h1lt : (1 : ℕ) < db_b.keys.length := by + rw [hb_len]; omega + have hresBegin_b : resolve db_b r.begin = ((db_b.keys[(1 : ℕ)] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db_b := by rw [hanchor_b]; norm_num + have hlt : (r.begin.anchor db_b).toNat < db_b.keys.length := by + rw [hanchor_b]; simpa using h1lt + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor db_b).toNat = 1 := by rw [hanchor_b]; rfl + simp [hidx] + have hLlt : L < db_b.keys[(1 : ℕ)] := by + have h01 : (0 : ℕ) < db_b.keys.length := by omega + have h := (List.SortedLT.getElem_lt_getElem_iff db_b.sorted (hi := h01) + (hj := h1lt)).mpr (show (0 : ℕ) < 1 by omega) + rwa [hget0_b] at h + have hLnot_ck : L ∉ RangeRead.completeKeys db_b r := by + intro hmem + have hle := (RangeRead.mem_completeKeys.mp hmem).2.1 + rw [hresBegin_b] at hle + exact absurd hle (not_le.mpr (WithTop.coe_lt_coe.mpr hLlt)) + have hnotCompat_b : ¬ RangeRead.Compatible db_b r res := by + intro hc + rcases hc.2 with ⟨_, hpre, _⟩ | ⟨hm, _⟩ + · exact hLnot_ck (hpre.subset hLmem) + · rw [hmore] at hm; exact absurd hm (by simp) + refine necessary_of_toggle ⟨db_a, hcompat_a, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_a] + exact hnotCompat_b + +/-- **Minimality of `[last observed, begin.key)` in the large-gap reversed crossed +truncated cell.** Soundness is the count identity; necessity splits the range at +`end_.key`: below it the reported keys and the above-head rule cover every key, and at or +above it the gap witness applies. -/ +theorem minimal_beginKeyRange_of_negCross_crossedLarge {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1) + (hlarge : (beginSlack r : ℕ∞) + 2 ≤ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞)) : + Minimal r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_beginKeyRange_of_negCross_crossedLarge hread hmore hrev hne hcross hcrossE hend + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hkL, hkB⟩ := hk + by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key + · by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result hread hLmem (List.head_mem hne) + (WithTop.coe_le_coe.mp hkL) hkhead + · intro cr hs + exact necessary_head_to_end_key hread hrev hne (le_of_eq hend) cr hs + (le_of_lt (lt_of_not_ge hkhead)) hkE + · exact necessary_of_mem_gap_of_negCross_trunc_rev_crossedLarge hread hmore hrev hne hbeg + hcross hcrossE hend hlarge (not_lt.mp hkE) hkB + +/-- The reversed crossed negative-begin truncated large-gap cell. -/ +abbrev negCrossTruncRevCrossedLarge (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.key < r.begin.key ∧ r.end_.offset = 1 ∧ + (beginSlack r : ℕ∞) + 2 ≤ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + +/-- The closed form of the reversed crossed negative-begin truncated large-gap cell: +`[last reported, begin.key)`. -/ +noncomputable def closedFormNegCrossTruncRevCrossedLarge (_r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), _r.begin.key⟩ + +/-- **The reversed crossed negative-begin truncated large-gap closed form is +minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_crossedLarge {r : RangeRead} + {res : Result} (hread : Readable r res) (h : negCrossTruncRevCrossedLarge r res) : + Minimal r res (closedFormNegCrossTruncRevCrossedLarge r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hlarge⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlast]; exact hcrossD + unfold closedFormNegCrossTruncRevCrossedLarge + rw [hlast] + exact minimal_beginKeyRange_of_negCross_crossedLarge hread hmore hrev hne hbeg hcross + hcrossE hend hlarge + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevCrossedNonpos.lean b/FormalConflictRange/NegCrossTruncRevCrossedNonpos.lean new file mode 100644 index 0000000..a6c6c5d --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevCrossedNonpos.lean @@ -0,0 +1,970 @@ +/- +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.NegCrossTruncRevCrossedLarge + +/-! +# Reversed crossed negative-begin truncated cells at a nonpositive end offset + +This module closes the last reversed truncated crossed cell: the end selector has +nonpositive offset, so the window ends at a stored key strictly below `end_.key`. + +The setting is + +* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`; +* the crossed selectors `end_.key < begin.key`; +* `end_.offset ≤ 0`. + +Writing `L` for the reported last, `t = |res.keys|`, `p = (1 - end_.offset).toNat` and +`cap = intervalCard end_.key begin.key`, the minimal range is `[L, end_.key)` when +`cap + t + p ≤ beginSlack + 1` and `[L, begin.key)` otherwise. The pad count `p` is the +number of stored keys in `[resolve end_, end_.key)`, which the count identity +`between_last_end_key_length_eq_of_end_nonpos` adds to the reported keys. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1200000 + +/-- **The reported keys are the stored keys in `[last, resolve end_)` read backwards.** +Unlike the end-offset-one version, this does not simplify the resolved end to `end_.key`; +it is the count of that window that the nonpositive-end count identity needs. -/ +theorem res_keys_eq_reverse_between_resolve_end {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) : + res.keys = (between db ((res.keys.getLast hne : Key) : WithTop Key) + (resolve db r.end_)).reverse := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfil : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hLck : L ∈ RangeRead.completeKeys db r := (List.mem_filter.mp hLfil).1 + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLck).2.1 + have hid := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) (last := L) hrev hrb + rw [hfil] at hid + rw [hid] + congr 1 + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + conv_lhs => rw [← Bool.decide_and] + rw [decide_eq_decide] + constructor + · rintro ⟨hk, hL⟩ + exact ⟨WithTop.coe_le_coe.mpr hL, hk⟩ + · rintro ⟨hL, hk⟩ + exact ⟨hk, WithTop.coe_le_coe.mp hL⟩ + +/-- **The stored keys in `[last, resolve end_)` number the reported keys.** Immediate +from the reversed-window identity. -/ +theorem between_last_resolve_end_length {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) : + (between db ((res.keys.getLast hne : Key) : WithTop Key) (resolve db r.end_)).length = + res.keys.length := by + have h := res_keys_eq_reverse_between_resolve_end (db := db) hcomp hmore hrev hne + have h2 := congrArg List.length h + rw [List.length_reverse] at h2 + exact h2.symm + +/-- **The pad count is at most the end count.** A nonempty truncated result at a +nonpositive end offset has its end anchor at a nonnegative index, so the +`(1 - end_.offset)` padding keys fit below `end_.key`. -/ +theorem pad_le_cnt_end_of_end_nonpos {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0) : + (1 - r.end_.offset).toNat ≤ cnt db r.end_.key := by + set p : ℕ := (1 - r.end_.offset).toNat with hpdef + have hpcast : (p : Int) = 1 - r.end_.offset := by + rw [hpdef, Int.toNat_of_nonneg (by omega)] + set c : ℕ := cnt db r.end_.key with hcdef + have hc_eq : (db.keys.countP + (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))) = c := by + rw [hcdef]; rfl + have hanc : r.end_.anchor db = (c : Int) - (p : Int) := by + unfold Selector.anchor + rw [hc_eq, hpcast] + ring + have hnonnil : between db ((res.keys.getLast hne : Key) : WithTop Key) + (resolve db r.end_) ≠ [] := by + intro hnil + have h := res_keys_eq_reverse_between_resolve_end (db := db) hcomp hmore hrev hne + rw [hnil] at h + exact hne (by simpa using h.symm) + have hanchor_nonneg : 0 ≤ r.end_.anchor db := by + by_contra hneg + have := resolve_eq_nil (db := db) (s := r.end_) (lt_of_not_ge hneg) + rw [this] at hnonnil + apply hnonnil + unfold between + rw [List.filter_eq_nil_iff] + intro k _ + rw [decide_eq_true_eq, not_and] + intro _ + exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k)) + have : (0 : Int) ≤ (c : Int) - (p : Int) := by rwa [← hanc] + omega + +/-- **The count below the end resolution at a nonpositive end offset.** With +`c = cnt db end_.key` stored keys below `end_.key`, the end anchor is `c - p` where +`p = (1 - end_.offset).toNat`, and the resolved index is at least `0` because a +nonempty truncated result forces the resolution above the empty key. Hence +`cnt db (resolve end_) = c - p`. -/ +theorem cnt_resolve_end_eq_of_end_nonpos {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0) : + cnt db (resolve db r.end_) = cnt db r.end_.key - (1 - r.end_.offset).toNat := by + set p : ℕ := (1 - r.end_.offset).toNat with hpdef + have hpcast : (p : Int) = 1 - r.end_.offset := by + rw [hpdef, Int.toNat_of_nonneg (by omega)] + set c : ℕ := cnt db r.end_.key with hcdef + have hc_eq : (db.keys.countP + (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))) = c := by + rw [hcdef]; rfl + have hanc : r.end_.anchor db = (c : Int) - (p : Int) := by + unfold Selector.anchor + rw [hc_eq, hpcast] + ring + -- The resolution is not the empty key: a nonempty reported window lies strictly + -- above it. + have hnonnil : between db ((res.keys.getLast hne : Key) : WithTop Key) + (resolve db r.end_) ≠ [] := by + intro hnil + have h := res_keys_eq_reverse_between_resolve_end (db := db) hcomp hmore hrev hne + rw [hnil] at h + exact hne (by simpa using h.symm) + have hanchor_nonneg : 0 ≤ r.end_.anchor db := by + by_contra hneg + have := resolve_eq_nil (db := db) (s := r.end_) (lt_of_not_ge hneg) + rw [this] at hnonnil + apply hnonnil + unfold between + rw [List.filter_eq_nil_iff] + intro k _ + rw [decide_eq_true_eq, not_and] + intro _ + exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k)) + have hcge : p ≤ c := by + have : (0 : Int) ≤ (c : Int) - (p : Int) := by rwa [← hanc] + omega + have hmin : cnt db (resolve db r.end_) = min (r.end_.anchor db).toNat db.keys.length := + countP_lt_resolve + have hclen : c ≤ db.keys.length := by rw [hcdef]; exact cnt_le_length db r.end_.key + have htoNat : (r.end_.anchor db).toNat = c - p := by + rw [hanc]; omega + rw [hmin, htoNat, Nat.min_eq_left (by omega)] + +/-- **The nonpositive-end count identity.** For a compatible reversed truncated result +with `end_.offset ≤ 0`, the stored keys in `[last, end_.key)` are exactly the reported +keys together with the `(1 - end_.offset)` stored keys in `[resolve end_, end_.key)`. -/ +theorem between_last_end_key_length_eq_of_end_nonpos {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0) : + (between db ((res.keys.getLast hne : Key) : WithTop Key) r.end_.key).length = + res.keys.length + (1 - r.end_.offset).toNat := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLck : L ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset hLmem + have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hLck).2.2 + have hLleRes : ((L : Key) : WithTop Key) ≤ resolve db r.end_ := le_of_lt hLres + have hresEnd_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hLleEnd : ((L : Key) : WithTop Key) ≤ r.end_.key := le_trans hLleRes hresEnd_le + have hresLen := between_last_resolve_end_length (db := db) hcomp hmore hrev hne + have hcntRes := cnt_resolve_end_eq_of_end_nonpos (db := db) hcomp hmore hrev hne hend + have hsplit := cnt_sub_eq_between_length (db := db) (E := ((L : Key) : WithTop Key)) + (B := r.end_.key) hLleEnd + have hsplitRes := cnt_sub_eq_between_length (db := db) (E := ((L : Key) : WithTop Key)) + (B := resolve db r.end_) hLleRes + rw [hresLen] at hsplitRes + have hmono : cnt db ((L : Key) : WithTop Key) ≤ cnt db (resolve db r.end_) := + cnt_le_cnt_of_le db hLleRes + have hp_le := pad_le_cnt_end_of_end_nonpos (db := db) hcomp hmore hrev hne hend + omega + +/-- **The stored keys in `[last, resolve end_)` are the first `t` keys of +`[last, end_.key)`.** When the end resolution lies at or below `end_.key`, filtering +`[last, end_.key)` at the resolution is a prefix of the sorted interval, and its length +is the number of reported keys. -/ +theorem between_resolve_eq_take_of_end_le {db : DB} {r : RangeRead} {L : Key} {t : ℕ} + (hresEnd_le : resolve db r.end_ ≤ r.end_.key) + (hlen : (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length = t) : + between db ((L : Key) : WithTop Key) (resolve db r.end_) = + (between db ((L : Key) : WithTop Key) r.end_.key).take t := by + set V : List Key := between db ((L : Key) : WithTop Key) r.end_.key with hV + have hVsorted : V.SortedLT := by + rw [hV]; unfold between + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hfilter : V.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) = + between db ((L : Key) : WithTop Key) (resolve db r.end_) := by + rw [hV]; unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hres : ((k : WithTop Key) < resolve db r.end_) + · have hE : ((k : WithTop Key) < r.end_.key) := lt_of_lt_of_le hres hresEnd_le + simp [hres, hE] + · simp [hres, Bool.decide_and] + rw [← hfilter, filter_lt_eq_take_countP hVsorted] + congr 1 + rw [List.countP_eq_length_filter, hfilter, hlen] + +/-- **Necessity of the reversed truncated range at a nonpositive end offset.** Every key +in `[last reported, end_.key)` is necessary: reported keys by +`necessary_of_mem_result`, keys up to the head by convexity, and keys from the head to +`end_.key` by `necessary_head_to_end_key`. -/ +theorem necessary_of_mem_truncatedRangeRev_of_end_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : r.end_.offset ≤ 0) {k : Key} + (hkL : ((res.keys.getLast hne : Key) : WithTop Key) ≤ (k : WithTop Key)) + (hkE : (k : WithTop Key) < r.end_.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + (List.head_mem hne) (WithTop.coe_le_coe.mp hkL) hkhead + · have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact fun cr hs => + necessary_head_to_end_key ⟨db₀, hcomp₀⟩ hrev hne (by omega) cr hs hheadk hkE + +/-- **Reduction to the read whose begin selector is the reported last at offset one.** A +compatible database for the crossed negative-begin truncated read is compatible with the +reduced read: both see exactly the reported keys at or above the reported last. -/ +theorem compatible_reduce_begin_last {r : RangeRead} {res : Result} {L : Key} + (hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ []) + (hLmem : L ∈ res.keys) (hLgetLast : L = res.keys.getLast hne) : + ∀ db, RangeRead.Compatible db r res → + RangeRead.Compatible db + { r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } res := by + intro db hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by + have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + rw [← hLgetLast] at h + exact h + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hres' : resolve db ({ r with begin := + ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } : RangeRead).begin = + ((L : Key) : WithTop Key) := + resolve_offset_one_key_of_mem hLdb + have hLck_filter : (RangeRead.completeKeys db + ({ r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } : + RangeRead)).filter (fun k => decide (L ≤ k)) = + RangeRead.completeKeys db + ({ r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } : + RangeRead) := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hk' := (RangeRead.mem_completeKeys.mp hk).2.1 + exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk') + have hck' : RangeRead.completeKeys db + ({ r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } : + RangeRead) = res.keys := by + rw [← hLck_filter] + exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) + (r₂ := { r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : + Selector) }) hrev (by simpa using hrev) (by simp) hrb (le_of_eq hres')).symm.trans hfil + refine ⟨by simpa [RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck'] + +/-- **Small-capacity soundness.** Under `cap + t + p ≤ beginSlack + 1`, agreement on +`[last, end_.key)` keeps the begin resolution at or below the reported last, so +compatibility transfers. -/ +theorem sound_truncatedRangeRev_of_negCross_crossedNonpos_small {r : RangeRead} + {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0) + (hcap : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + + ((1 - r.end_.offset).toNat : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1) : + Sound r res (truncatedRangeRev r (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross + have hLgetLast : L = res.keys.getLast hne := hLdef.symm + obtain ⟨db₀, hcomp₀⟩ := hread + let r' : RangeRead := + { r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } } + have hr'_rev : r'.reverse = true := by simp [r', hrev] + have hr'_end : r'.end_ = r.end_ := rfl + have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) := rfl + have hr'_off : r'.begin.offset = 1 := rfl + have hreduce : ∀ db : DB, RangeRead.Compatible db r res → RangeRead.Compatible db r' res := + compatible_reduce_begin_last hrev hmore hne hLmem hLgetLast + have hread' : Readable r' res := ⟨db₀, hreduce db₀ hcomp₀⟩ + have hgetLastD : res.keys.getLastD ([] : Key) = L := by + rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hmin' : Minimal r' res (truncatedRangeRev r L) := by + have hm := minimal_truncatedRangeRev_of_end_offset_nonpos (r := r') hread' hmore + hr'_rev hr'_off hend + rwa [hgetLastD] at hm + have hS' : Sound r' res (truncatedRangeRev r L) := hmin'.sound + -- `truncatedRangeRev r' L` is definitionally `truncatedRangeRev r L`. + have step : ∀ db db', AgreeOn (truncatedRangeRev r L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hcomp' : RangeRead.Compatible db r' res := hreduce db hcomp + have hcomp'' : RangeRead.Compatible db' r' res := (hS' db db' hag').mp hcomp' + have hbetweenEq' : between db ((L : Key) : WithTop Key) r.end_.key = + between db' ((L : Key) : WithTop Key) r.end_.key := by + have h := hag' + unfold AgreeOn ConflictRange.keys at h + unfold between + exact h + have hbetweenLen : (between db ((L : Key) : WithTop Key) r.end_.key).length = + res.keys.length + (1 - r.end_.offset).toNat := + between_last_end_key_length_eq_of_end_nonpos (db := db) hcomp hmore hrev hne hend + have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp + ((RangeRead.compatible_keys_prefix hcomp).subset hLmem)).2.2 + have hresEnd_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hLE : ((L : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le hLres hresEnd_le + have hcapNat : (between db' r.end_.key r.begin.key).length + res.keys.length + + (1 - r.end_.offset).toNat ≤ beginSlack r + 1 := by + have hb : ((between db' r.end_.key r.begin.key).length : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key := + between_length_le_intervalCard (le_of_lt hcrossE) + have hsum : (((between db' r.end_.key r.begin.key).length + res.keys.length + + (1 - r.end_.offset).toNat : ℕ) : ℕ∞) ≤ ((beginSlack r + 1 : ℕ) : ℕ∞) := by + rw [Nat.cast_add, Nat.cast_add] + calc ((between db' r.end_.key r.begin.key).length : ℕ∞) + + (res.keys.length : ℕ∞) + ((1 - r.end_.offset).toNat : ℕ∞) + ≤ intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + + ((1 - r.end_.offset).toNat : ℕ∞) := by gcongr + _ ≤ (beginSlack r : ℕ∞) + 1 := hcap + _ = ((beginSlack r + 1 : ℕ) : ℕ∞) := by rw [Nat.cast_add, Nat.cast_one] + exact_mod_cast hsum + have hsplit := between_length_add (db := db') (L := ((L : Key) : WithTop Key)) + (E := r.end_.key) (B := r.begin.key) (le_of_lt hLE) (le_of_lt hcrossE) + have hboundLen : (between db' ((L : Key) : WithTop Key) r.begin.key).length ≤ + beginSlack r + 1 := by + rw [hsplit, ← hbetweenEq', hbetweenLen]; omega + have hboundInt : + ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + have hs : (beginSlack r : Int) = -r.begin.offset := beginSlack_cast r (le_of_lt hbeg) + have hble : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤ + (beginSlack r : Int) + 1 := by exact_mod_cast hboundLen + linarith + have hLdb' : L ∈ db'.keys := RangeRead.compatible_keys_subset hcomp'' hLmem + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb' hLcross hboundInt + have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb' + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp''.1, ?_⟩ + have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) hrev hrb' + have h2' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r') hr'_rev + (le_of_eq hres'_db') + have h2 := (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp + hcomp'' |>.2 + rw [h1, ← h2'] + exact h2 + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Rewriting `between` as a double filter at a genuine key.** -/ +theorem between_eq_filter_filter (db : DB) (L : Key) (B : WithTop Key) : + between db ((L : Key) : WithTop Key) B = + (db.keys.filter (fun k : Key => decide (L ≤ k))).filter + (fun k => decide (((k : Key) : WithTop Key) < B)) := by + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hL : L ≤ k + · have hL' : ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr hL + by_cases hB : ((k : Key) : WithTop Key) < B + · simp [hL, hL', hB] + · simp [hB, hL'] + · have hL' : ¬ ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + fun h => hL (WithTop.coe_le_coe.mp h) + simp [hL, hL'] + +/-- **Large-capacity soundness.** Agreement on `[last, begin.key)` preserves +compatibility; the count identity locates the end resolution at a fixed relative position +inside that interval, so both databases resolve to the same reported window. -/ +theorem sound_beginKeyRange_of_negCross_crossedNonpos {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0) : + Sound r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross + have step : ∀ db db', AgreeOn ⟨((L : Key) : WithTop Key), r.begin.key⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hbetweenEq : between db ((L : Key) : WithTop Key) r.begin.key = + between db' ((L : Key) : WithTop Key) r.begin.key := by + have h := hag' + unfold AgreeOn ConflictRange.keys at h + unfold between + exact h + have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp + ((RangeRead.compatible_keys_prefix hcomp).subset hLmem)).2.2 + have hresEnd_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + have hLE : ((L : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le hLres hresEnd_le + have hVeq : between db ((L : Key) : WithTop Key) r.end_.key = + between db' ((L : Key) : WithTop Key) r.end_.key := + between_eq_of_agree (db := db) (db' := db') (last := ((L : Key) : WithTop Key)) + (beginKey := r.end_.key) (endKey := r.begin.key) hag' (le_of_lt hLE) (le_of_lt hcrossE) + have hbound := between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + have hbound' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [← hbetweenEq]; exact hbound + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLdb' : L ∈ db'.keys := by + have hmem : L ∈ between db ((L : Key) : WithTop Key) r.begin.key := by + unfold between + exact List.mem_filter.mpr ⟨hLdb, by simp [hLcross]⟩ + rw [hbetweenEq] at hmem + exact (List.mem_filter.mp hmem).1 + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb' hLcross hbound' + set p : ℕ := (1 - r.end_.offset).toNat with hpdef + have hpcast : (p : Int) = 1 - r.end_.offset := by + rw [hpdef, Int.toNat_of_nonneg (by omega)] + have hVlen : (between db ((L : Key) : WithTop Key) r.end_.key).length = + res.keys.length + p := + between_last_end_key_length_eq_of_end_nonpos (db := db) hcomp hmore hrev hne hend + have hVlen' : (between db' ((L : Key) : WithTop Key) r.end_.key).length = + res.keys.length + p := by rw [← hVeq]; exact hVlen + have hcntEqB : cnt db' r.end_.key = cnt db' ((L : Key) : WithTop Key) + + (between db' ((L : Key) : WithTop Key) r.end_.key).length := + cnt_eq_add_between db' (le_of_lt hLE) + have hanc : r.end_.anchor db' = + (cnt db' ((L : Key) : WithTop Key) : Int) + res.keys.length := by + change (cnt db' r.end_.key : Int) + r.end_.offset - 1 = _ + rw [hcntEqB, hVlen'] + push_cast + rw [hpcast]; ring + have hp_pos : 0 < p := by + rw [hpdef] + have : (0 : Int) < 1 - r.end_.offset := by omega + omega + have hlen_ge : cnt db' ((L : Key) : WithTop Key) + res.keys.length < db'.keys.length := by + have h := cnt_eq_add_between db' (le_of_lt hLE) + rw [hVlen'] at h + have := cnt_le_length db' r.end_.key + omega + have hanc_nonneg : 0 ≤ r.end_.anchor db' := by rw [hanc]; positivity + have hanc_toNat : (r.end_.anchor db').toNat = + cnt db' ((L : Key) : WithTop Key) + res.keys.length := by + rw [hanc]; omega + have hanc_len : (r.end_.anchor db').toNat < db'.keys.length := by + rw [hanc_toNat]; exact hlen_ge + have hcntRes : cnt db' (resolve db' r.end_) = (r.end_.anchor db').toNat := by + change db'.keys.countP + (fun k => decide (((k : Key) : WithTop Key) < resolve db' r.end_)) = _ + rw [countP_lt_resolve, Nat.min_eq_left (le_of_lt hanc_len)] + have hLleRes : ((L : Key) : WithTop Key) ≤ resolve db' r.end_ := by + have hres := resolve_eq_getElem (db := db') (s := r.end_) hanc_nonneg hanc_len + rw [hres] + refine WithTop.coe_le_coe.mpr (le_getElem_of_countP_le db'.sorted ?_ hanc_len) + rw [← cnt_coe db' L, hanc_toNat]; omega + have hresLen' : (between db' ((L : Key) : WithTop Key) (resolve db' r.end_)).length = + res.keys.length := by + rw [← cnt_sub_eq_between_length (db := db') hLleRes, hcntRes, hanc_toNat, + Nat.add_sub_cancel_left] + have htakeDb : between db ((L : Key) : WithTop Key) (resolve db r.end_) = + (between db ((L : Key) : WithTop Key) r.end_.key).take res.keys.length := + between_resolve_eq_take_of_end_le (db := db) (r := r) (L := L) (t := res.keys.length) + hresEnd_le (between_last_resolve_end_length (db := db) hcomp hmore hrev hne) + have htakeDb' : between db' ((L : Key) : WithTop Key) (resolve db' r.end_) = + (between db' ((L : Key) : WithTop Key) r.end_.key).take res.keys.length := + between_resolve_eq_take_of_end_le (db := db') (r := r) (L := L) (t := res.keys.length) + (resolve_le_key_of_offset_nonpos hend) hresLen' + have hbetween_res_eq : between db ((L : Key) : WithTop Key) (resolve db r.end_) = + between db' ((L : Key) : WithTop Key) (resolve db' r.end_) := by + rw [htakeDb, htakeDb', hVeq] + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + refine ⟨by simpa [RangeRead.withinLimit] using hcomp.1, ?_⟩ + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb hLcross hbound + have hfilDb : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hidDb := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) (last := L) hrev hrb + have hidDb' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) (last := L) hrev hrb' + have hbridgeDb : ((db.keys.filter (fun k : Key => decide (L ≤ k))).filter + (fun k => decide (((k : Key) : WithTop Key) < resolve db r.end_))) = + between db ((L : Key) : WithTop Key) (resolve db r.end_) := + (between_eq_filter_filter db L (resolve db r.end_)).symm + have hbridgeDb' : ((db'.keys.filter (fun k : Key => decide (L ≤ k))).filter + (fun k => decide (((k : Key) : WithTop Key) < resolve db' r.end_))) = + between db' ((L : Key) : WithTop Key) (resolve db' r.end_) := + (between_eq_filter_filter db' L (resolve db' r.end_)).symm + rw [hidDb', hbridgeDb', ← hbetween_res_eq, ← hbridgeDb, ← hidDb, hfilDb] + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Gap-key necessity in the large-capacity nonpositive-end crossed cell.** The +witness database stores the reported keys, the `p` pad keys that any compatible database +stores in `[resolve end, end_.key)`, and `beginSlack + 1 - t - p` gap keys in +`[end_.key, begin_.key)`. Its begin anchor is zero and it resolves onto the reported +last. Inserting the target raises the anchor to one, so the reported last leaves the +window and the reported keys can no longer be a prefix of the complete keys. -/ +theorem necessary_of_mem_gap_of_negCross_crossedNonpos_large {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0) + (hlarge : (beginSlack r : ℕ∞) + 2 ≤ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + + ((1 - r.end_.offset).toNat : ℕ∞)) + {k : Key} (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set L : Key := res.keys.getLast hne with hLdef + set t : ℕ := res.keys.length with htdef + set s : ℕ := beginSlack r with hsdef + set p : ℕ := (1 - r.end_.offset).toNat with hpdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + have hreported_lt_R : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < resolve db₀ r.end_ := by + intro x hx + exact (RangeRead.mem_completeKeys.mp (hpre₀.subset hx)).2.2 + have hreported_lt_E : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + exact lt_of_lt_of_le (hreported_lt_R x hx) + (resolve_le_key_of_offset_nonpos (db := db₀) (s := r.end_) hend) + have hLltE : ((L : Key) : WithTop Key) < r.end_.key := hreported_lt_E L hLmem + have hEcross : r.end_.key ≤ r.begin.key := le_of_lt hcrossE + have hbound : ((between db₀ ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := + between_add_offset_le_one (db := db₀) hcomp₀ hmore hrev hne hcross + have hbLen_le : (between db₀ ((L : Key) : WithTop Key) r.begin.key).length ≤ s + 1 := by + have hs : (beginSlack r : Int) = -r.begin.offset := beginSlack_cast r (le_of_lt hbeg) + have hb2 : ((between db₀ ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤ + (beginSlack r : Int) + 1 := by linarith + have : ((between db₀ ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤ + (s : Int) + 1 := by rw [hsdef]; exact hb2 + exact_mod_cast this + have hVlen : (between db₀ ((L : Key) : WithTop Key) r.end_.key).length = t + p := by + rw [htdef, hpdef] + exact between_last_end_key_length_eq_of_end_nonpos (db := db₀) hcomp₀ hmore hrev hne hend + have hsplit := between_length_add (db := db₀) (L := ((L : Key) : WithTop Key)) + (E := r.end_.key) (B := r.begin.key) (le_of_lt hLltE) hEcross + have ht_le : t + p ≤ s + 1 := by omega + -- enough room in the crossed gap for the witness keys and the target + have hcard : (((s + 1 - t - p) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by + cases hcap : intervalCard r.end_.key r.begin.key with + | top => exact le_top + | coe n => + have hnat : s + 2 ≤ n + t + p := by + have h' : ((s + 2 : ℕ) : ℕ∞) ≤ ((n + t + p : ℕ) : ℕ∞) := by + rw [hcap] at hlarge + simpa [ENat.natCast_add] using hlarge + exact_mod_cast h' + have hn : (s + 1 - t - p) + 1 ≤ n := by omega + exact_mod_cast hn + -- the pad keys taken from `db₀` + have hLres : ((L : Key) : WithTop Key) < resolve db₀ r.end_ := hreported_lt_R L hLmem + have hresEnd_le : resolve db₀ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend + set Pad : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hPaddef + have hPadLen : Pad.length = p := by + rw [hPaddef] + have hsplitP := between_length_add (db := db₀) (L := ((L : Key) : WithTop Key)) + (E := resolve db₀ r.end_) (B := r.end_.key) (le_of_lt hLres) hresEnd_le + have hresLen : (between db₀ ((L : Key) : WithTop Key) (resolve db₀ r.end_)).length = t := by + rw [htdef] + exact between_last_resolve_end_length (db := db₀) hcomp₀ hmore hrev hne + rw [hVlen, hresLen] at hsplitP + omega + have hPadPos : 0 < Pad.length := by rw [hPadLen, hpdef]; omega + have hPadSorted : Pad.SortedLT := by + rw [hPaddef]; unfold between + exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hPadMem : ∀ x ∈ Pad, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hPaddef] at hx + unfold between at hx + rw [List.mem_filter] at hx + exact of_decide_eq_true hx.2 + set P0 : Key := Pad[(0 : ℕ)]'hPadPos with hP0def + have hP0mem : P0 ∈ Pad := List.getElem_mem hPadPos + have hP0min : ∀ x ∈ Pad, P0 ≤ x := by + intro x hx + obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hx + have hle : Pad[(0 : ℕ)]'hPadPos ≤ Pad[j]'hj := + hPadSorted.getElem_le_getElem_iff.mpr (Nat.zero_le j) + rw [hP0def] + rwa [hjx] at hle + obtain ⟨dbG, hGrange, hkG, hGlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k) + (m := s + 1 - t - p) hcard + set G : List Key := dbG.keys with hGdef + have hGmem : ∀ x ∈ G, r.end_.key ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.begin.key := by + intro x hx + simpa only [G, keysIn, Set.mem_ofPred_eq] using hGrange x (by simpa [hGdef] using hx) + have hGlen' : G.length = s + 1 - t - p := by rw [hGdef]; exact hGlen + -- assemble the ascending witness database + have hresSort : res.keys.reverse.SortedLT := hsort.reverse + have hGsorted : G.SortedLT := by rw [hGdef]; exact dbG.sorted + have hcrossRP : ∀ x ∈ res.keys.reverse, ∀ y ∈ Pad, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_R x (List.mem_reverse.mp hx)) + (hPadMem y hy).1) + have hcrossPG : ∀ x ∈ Pad, ∀ y ∈ G, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hPadMem x hx).2 (hGmem y hy).1) + have hcrossRG : ∀ x ∈ res.keys.reverse, ∀ y ∈ G, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx)) + (hGmem y hy).1) + have hRPsorted : (res.keys.reverse ++ Pad).SortedLT := + List.sortedLT_append.mpr ⟨hresSort, hPadSorted, hcrossRP⟩ + have hcrossAllG : ∀ x ∈ (res.keys.reverse ++ Pad), ∀ y ∈ G, x < y := by + intro x hx y hy + rcases List.mem_append.mp hx with hx' | hx' + · exact hcrossRG x hx' y hy + · exact hcrossPG x hx' y hy + have hkeysSorted : (res.keys.reverse ++ Pad ++ G).SortedLT := + List.sortedLT_append.mpr ⟨hRPsorted, hGsorted, hcrossAllG⟩ + let db_a : DB := ⟨res.keys.reverse ++ Pad ++ G, hkeysSorted⟩ + have hdb_a : db_a.keys = res.keys.reverse ++ Pad ++ G := rfl + have hlen_a : db_a.keys.length = s + 1 := by + rw [hdb_a, List.length_append, List.length_append, List.length_reverse, hGlen'] + omega + have hcntB : db_a.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = t + p + G.length := by + rw [hdb_a, List.countP_append, List.countP_append, List.countP_reverse] + have h1 : res.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = t := by + rw [htdef, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq] + exact lt_trans (hreported_lt_E x hx) hcrossE + have h2 : Pad.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = p := by + rw [← hPadLen, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq] + exact lt_trans (hPadMem x hx).2 hcrossE + have h3 : G.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = G.length := by + rw [List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact (hGmem x hx).2 + rw [h1, h2, h3] + have hanchor : r.begin.anchor db_a = 0 := by + rw [anchor_eq_count_sub_slack db_a r (le_of_lt hbeg), hcntB, hGlen', ← hsdef] + have hts : t + p + (s + 1 - t - p) = s + 1 := by omega + rw [hts]; push_cast; ring + have h0lt : (0 : ℕ) < db_a.keys.length := by rw [hlen_a]; omega + have hmin_a : ∀ x ∈ db_a.keys, L ≤ x := by + intro x hx + rw [hdb_a] at hx + rcases List.mem_append.mp hx with hx' | hxG + · rcases List.mem_append.mp hx' with hxr | hxP + · exact hLle x (List.mem_reverse.mp hxr) + · exact le_of_lt (WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hreported_lt_R L hLmem) (hPadMem x hxP).1)) + · exact le_of_lt (WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hreported_lt_E L hLmem) (hGmem x hxG).1)) + have hLmem_a : L ∈ db_a.keys := by + rw [hdb_a] + exact List.mem_append_left _ (List.mem_append_left _ (List.mem_reverse.mpr hLmem)) + have hget0 : db_a.keys[(0 : ℕ)] = L := + getElem_zero_eq_of_forall_le hkeysSorted hmin_a hLmem_a h0lt + have hresBegin : resolve db_a r.begin = ((L : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db_a := by rw [hanchor] + have hlt : (r.begin.anchor db_a).toNat < db_a.keys.length := by + rw [hanchor]; simpa using h0lt + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanchor]; rfl + simp [hidx, hget0] + have hcntEnd : cnt db_a r.end_.key = t + p := by + unfold cnt + rw [hdb_a, List.countP_append, List.countP_append, List.countP_reverse] + have h1 : res.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.end_.key)) = t := by + rw [htdef, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact hreported_lt_E x hx + have h2 : Pad.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.end_.key)) = p := by + rw [← hPadLen, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact (hPadMem x hx).2 + have h3 : G.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq, not_lt] + exact (hGmem x hx).1 + rw [h1, h2, h3] + omega + have hancEnd : r.end_.anchor db_a = (t : Int) := by + change (cnt db_a r.end_.key : Int) + r.end_.offset - 1 = _ + rw [hcntEnd] + have hpcast : (p : Int) = 1 - r.end_.offset := by + rw [hpdef, Int.toNat_of_nonneg (by omega)] + push_cast; rw [hpcast]; ring + have hresEnd : resolve db_a r.end_ = ((P0 : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db_a := by rw [hancEnd]; positivity + have hlt : (r.end_.anchor db_a).toNat < db_a.keys.length := by + rw [hancEnd, hlen_a]; omega + have hlen1 : res.keys.reverse.length = t := by rw [List.length_reverse, htdef] + have hidx : (r.end_.anchor db_a).toNat = t := by rw [hancEnd]; simp + have hget : db_a.keys[(r.end_.anchor db_a).toNat]'hlt = P0 := by + simp only [hdb_a] at hlt ⊢ + simp only [hidx, ← hlen1] + simp only [List.append_assoc] + rw [List.getElem_append_right (as := res.keys.reverse) (bs := Pad ++ G) + (i := res.keys.reverse.length) (by simp)] + simp only [Nat.sub_self] + rw [List.getElem_append_left (i := 0) (as := Pad) (bs := G) hPadPos] + rw [resolve_eq_getElem h0 hlt, hget] + have hfilter : db_a.keys.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by + rw [hdb_a, List.filter_append, List.filter_append, hresEnd] + have h1 : (res.keys.reverse).filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((P0 : Key) : WithTop Key))) = res.keys.reverse := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (hLle x (List.mem_reverse.mp hx)), + lt_of_lt_of_le (hreported_lt_R x (List.mem_reverse.mp hx)) (hPadMem P0 hP0mem).1⟩ + have h2 : Pad.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((P0 : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hP + exact absurd hP.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (hP0min x hx))) + have h3 : G.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((P0 : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hP + have hle : ((P0 : Key) : WithTop Key) ≤ (x : WithTop Key) := + le_trans (hPadMem P0 hP0mem).2.le (hGmem x hx).1 + exact absurd hP.2 (not_lt.mpr hle) + rw [h1, h2, h3] + simp + have hcomplete : RangeRead.completeKeys db_a r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev] + simp only [ite_true] + rw [hresBegin, hfilter, List.reverse_reverse] + have hcompat_a : RangeRead.Compatible db_a r res := + ⟨hcomp₀.1, Or.inl ⟨hmore, by rw [hcomplete], hne⟩⟩ + have hk_a : k ∉ db_a.keys := by + rw [hdb_a] + intro hmem + rcases List.mem_append.mp hmem with hx | hx + · rcases List.mem_append.mp hx with hxr | hxP + · exact absurd (hreported_lt_E k (List.mem_reverse.mp hxr)) (not_lt.mpr hkE) + · exact absurd (hPadMem k hxP).2 (not_lt.mpr hkE) + · exact hkG (by simpa [hGdef] using hx) + -- insert the target: the first two sorted keys are the reported last and something above + let db_b : DB := db_a.insert k hk_a + have hins : (res.keys.reverse ++ Pad ++ G).orderedInsert (· ≤ ·) k = + res.keys.reverse ++ Pad ++ G.orderedInsert (· ≤ ·) k := by + refine orderedInsert_append_of_forall_lt_left ?_ + intro x hx + rcases List.mem_append.mp hx with hx' | hxP + · exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx')) hkE) + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hPadMem x hxP).2 hkE) + have hb_keys : db_b.keys = res.keys.reverse ++ Pad ++ G.orderedInsert (· ≤ ·) k := by + rw [show db_b.keys = db_a.keys.orderedInsert (· ≤ ·) k from rfl, hdb_a, hins] + have hb_len : db_b.keys.length = s + 2 := by + rw [hb_keys, List.length_append, List.length_append, List.length_reverse, + List.orderedInsert_length, hGlen', htdef, hsdef] + omega + have hb_all : ∀ x ∈ db_b.keys, + decide (((x : Key) : WithTop Key) < r.begin.key) = true := by + intro x hx + rw [decide_eq_true_eq] + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hx' | hx' + · rcases List.mem_append.mp hx' with hxr | hxP + · exact lt_trans (hreported_lt_E x (List.mem_reverse.mp hxr)) hcrossE + · exact lt_trans (hPadMem x hxP).2 hcrossE + · rw [List.mem_orderedInsert] at hx' + rcases hx' with rfl | hxG + · exact hkB + · exact (hGmem x hxG).2 + have hb_cntB : db_b.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = s + 2 := by + rw [← hb_len, List.countP_eq_length] + exact hb_all + have hanchor_b : r.begin.anchor db_b = 1 := by + rw [anchor_eq_count_sub_slack db_b r (le_of_lt hbeg), hb_cntB, ← hsdef] + push_cast; ring + have hget0_b : db_b.keys[(0 : ℕ)] = L := by + have hmin_b : ∀ x ∈ db_b.keys, L ≤ x := by + intro x hx + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hx' | hx' + · rcases List.mem_append.mp hx' with hxr | hxP + · exact hLle x (List.mem_reverse.mp hxr) + · exact le_of_lt (WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hreported_lt_R L hLmem) (hPadMem x hxP).1)) + · rw [List.mem_orderedInsert] at hx' + rcases hx' with rfl | hxG + · exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_E L hLmem) hkE)) + · exact le_of_lt (WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hreported_lt_E L hLmem) (hGmem x hxG).1)) + have hLmem_b : L ∈ db_b.keys := by + rw [hb_keys] + exact List.mem_append_left _ (List.mem_append_left _ (List.mem_reverse.mpr hLmem)) + have h0lt_b : 0 < db_b.keys.length := by rw [hb_len]; omega + exact getElem_zero_eq_of_forall_le db_b.sorted hmin_b hLmem_b h0lt_b + have h1lt : (1 : ℕ) < db_b.keys.length := by rw [hb_len]; omega + have hresBegin_b : resolve db_b r.begin = ((db_b.keys[(1 : ℕ)] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db_b := by rw [hanchor_b]; norm_num + have hlt : (r.begin.anchor db_b).toNat < db_b.keys.length := by + rw [hanchor_b]; simpa using h1lt + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor db_b).toNat = 1 := by rw [hanchor_b]; rfl + simp [hidx] + have hLlt : L < db_b.keys[(1 : ℕ)] := by + have h01 : (0 : ℕ) < db_b.keys.length := by omega + have h := (List.SortedLT.getElem_lt_getElem_iff db_b.sorted (hi := h01) + (hj := h1lt)).mpr (show (0 : ℕ) < 1 by omega) + rwa [hget0_b] at h + have hLnot_ck : L ∉ RangeRead.completeKeys db_b r := by + intro hmem + have hle := (RangeRead.mem_completeKeys.mp hmem).2.1 + rw [hresBegin_b] at hle + exact absurd hle (not_le.mpr (WithTop.coe_lt_coe.mpr hLlt)) + have hnotCompat_b : ¬ RangeRead.Compatible db_b r res := by + intro hc + rcases hc.2 with ⟨_, hpre, _⟩ | ⟨hm, _⟩ + · exact hLnot_ck (hpre.subset hLmem) + · rw [hmore] at hm; exact absurd hm (by simp) + refine necessary_of_toggle ⟨db_a, hcompat_a, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_a] + exact hnotCompat_b + +/-- **Minimality of the reversed truncated range in the small-capacity nonpositive-end +crossed cell.** Soundness is `sound_truncatedRangeRev_of_negCross_crossedNonpos_small`; +necessity of `[last, end_.key)` is `necessary_of_mem_truncatedRangeRev_of_end_nonpos`. -/ +theorem minimal_truncatedRangeRev_of_negCross_crossedNonpos_small {r : RangeRead} + {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0) + (hcap : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + + ((1 - r.end_.offset).toNat : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1) : + Minimal r res (truncatedRangeRev r (res.keys.getLast hne)) := by + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_truncatedRangeRev_of_negCross_crossedNonpos_small hread hmore hrev hbeg hne + hcross hcrossE hend hcap + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hkL, hkE⟩ := hk + exact necessary_of_mem_truncatedRangeRev_of_end_nonpos hread hrev hne hend hkL hkE + +/-- **Minimality of `[last, begin_.key)` in the large-capacity nonpositive-end crossed +cell.** Soundness is `sound_beginKeyRange_of_negCross_crossedNonpos`; necessity below +`end_.key` is the truncated-range necessity, and at or above it the gap witness applies. -/ +theorem minimal_beginKeyRange_of_negCross_crossedNonpos_large {r : RangeRead} + {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0) + (hlarge : (beginSlack r : ℕ∞) + 2 ≤ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + + ((1 - r.end_.offset).toNat : ℕ∞)) : + Minimal r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_beginKeyRange_of_negCross_crossedNonpos hmore hrev hne hcross hcrossE hend + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hkL, hkB⟩ := hk + by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key + · exact necessary_of_mem_truncatedRangeRev_of_end_nonpos hread hrev hne hend hkL hkE + · exact necessary_of_mem_gap_of_negCross_crossedNonpos_large hread hmore hrev hne hbeg + hcross hcrossE hend hlarge (not_lt.mp hkE) hkB + +/-- The reversed crossed negative-begin truncated small-capacity cell at a nonpositive +end offset. -/ +abbrev negCrossTruncRevCrossedNonposSmall (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.key < r.begin.key ∧ r.end_.offset ≤ 0 ∧ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + + ((1 - r.end_.offset).toNat : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1 + +/-- The closed form of the small-capacity nonpositive-end crossed cell: +`[last reported, end_.key)`. -/ +noncomputable def closedFormNegCrossTruncRevCrossedNonposSmall (_r : RangeRead) + (res : Result) : ConflictRange := + truncatedRangeRev _r (res.keys.getLastD ([] : Key)) + +/-- **The small-capacity nonpositive-end crossed closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_small {r : RangeRead} + {res : Result} (hread : Readable r res) + (h : negCrossTruncRevCrossedNonposSmall r res) : + Minimal r res (closedFormNegCrossTruncRevCrossedNonposSmall r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hcap⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlast]; exact hcrossD + unfold closedFormNegCrossTruncRevCrossedNonposSmall + rw [hlast] + exact minimal_truncatedRangeRev_of_negCross_crossedNonpos_small hread hmore hrev hbeg hne + hcross hcrossE hend hcap + +/-- The reversed crossed negative-begin truncated large-capacity cell at a nonpositive +end offset. -/ +abbrev negCrossTruncRevCrossedNonposLarge (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.key < r.begin.key ∧ r.end_.offset ≤ 0 ∧ + (beginSlack r : ℕ∞) + 2 ≤ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) + + ((1 - r.end_.offset).toNat : ℕ∞) + +/-- The closed form of the large-capacity nonpositive-end crossed cell: +`[last reported, begin_.key)`. -/ +noncomputable def closedFormNegCrossTruncRevCrossedNonposLarge (_r : RangeRead) + (res : Result) : ConflictRange := + ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), _r.begin.key⟩ + +/-- **The large-capacity nonpositive-end crossed closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_large {r : RangeRead} + {res : Result} (hread : Readable r res) + (h : negCrossTruncRevCrossedNonposLarge r res) : + Minimal r res (closedFormNegCrossTruncRevCrossedNonposLarge r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hlarge⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlast]; exact hcrossD + unfold closedFormNegCrossTruncRevCrossedNonposLarge + rw [hlast] + exact minimal_beginKeyRange_of_negCross_crossedNonpos_large hread hmore hrev hbeg hne + hcross hcrossE hend hlarge + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevCrossedSmall.lean b/FormalConflictRange/NegCrossTruncRevCrossedSmall.lean new file mode 100644 index 0000000..9478c9f --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevCrossedSmall.lean @@ -0,0 +1,252 @@ +/- +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.NegCrossTruncRevNonpos + +/-! +# Reversed crossed negative-begin truncated cells with a small crossed gap + +This module closes the reversed crossed negative-begin truncated cell at end offset one +whose capacity is small: + +* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`; +* the crossed selectors `end_.key < begin.key`, with every reported key at or above the + reported last and below `end_.key`; +* `end_.offset = 1`; +* `intervalCard end_.key begin.key + |res.keys| ≤ beginSlack + 1`. + +The minimal range is `[last reported, end_.key)`, the reversed truncated range. + +The reduction to the begin selector at the reported last is the same as in the +under-end cell. The only new ingredient is soundness: agreement on `[last, end_.key)` +fixes the stored keys there (exactly the `|res.keys|` reported keys), while the stored +keys in `[end_.key, begin.key)` number at most the interval cardinality. The capacity +bound therefore keeps the total count in `[last, begin.key)` at most `beginSlack + 1`, +so the begin resolution still lies at or below the reported last. + +Necessity is identical to the under-end cell: reported keys are necessary by +`necessary_of_mem_result`, keys between the reported last and head by +`necessary_of_between_result`, and keys from the head to `end_.key` by +`necessary_head_to_end_key`. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1200000 + +/-- **The length of `between` splits at an intermediate bound.** For `L ≤ E ≤ B` the +stored keys in `[L, B)` are those in `[L, E)` followed by those in `[E, B)`. -/ +theorem between_length_add {db : DB} {L E B : WithTop Key} (hLE : L ≤ E) (hEB : E ≤ B) : + (between db L B).length = (between db L E).length + (between db E B).length := by + have h1 := cnt_sub_eq_between_length (db := db) hLE + have h2 := cnt_sub_eq_between_length (db := db) hEB + have h3 := cnt_sub_eq_between_length (db := db) (le_trans hLE hEB) + have hmono : cnt db L ≤ cnt db E := cnt_le_cnt_of_le db hLE + have hmono' : cnt db E ≤ cnt db B := cnt_le_cnt_of_le db hEB + omega + +/-- **The reversed crossed negative-begin truncated small-gap cell.** -/ +theorem minimal_truncatedRangeRev_of_negCross_crossedSmall {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (_hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hcrossE : r.end_.key < r.begin.key) + (hend : r.end_.offset = 1) + (hcap : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) ≤ + (beginSlack r : ℕ∞) + 1) : + Minimal r res (truncatedRangeRev r (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross + obtain ⟨db₀, hcomp₀⟩ := hread + have hreadAll : Readable r res := ⟨db₀, hcomp₀⟩ + let r' : RangeRead := + { r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } } + have hr'_rev : r'.reverse = true := by simp [r', hrev] + have hr'_end : r'.end_ = r.end_ := rfl + have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) := + rfl + have hr'_off : r'.begin.offset = 1 := rfl + -- The reduced read sees exactly the reported keys. + have hreduce : ∀ (db : DB), RangeRead.Compatible db r res → + RangeRead.Compatible db r' res := by + intro db hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = + res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hres' : resolve db r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb + have hLck_filter : (RangeRead.completeKeys db r').filter (fun k => decide (L ≤ k)) = + RangeRead.completeKeys db r' := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hk' := (RangeRead.mem_completeKeys.mp hk).2.1 + exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk') + have hck' : RangeRead.completeKeys db r' = res.keys := by + rw [← hLck_filter] + exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) (r₂ := r') + hrev hr'_rev hr'_end hrb (le_of_eq hres')).symm.trans hfil + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck'] + have hread' : Readable r' res := ⟨db₀, hreduce db₀ hcomp₀⟩ + have hgetLastD : res.keys.getLastD ([] : Key) = L := by + rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hmin' : Minimal r' res (truncatedRangeRev r L) := by + have hm := minimal_truncatedRangeRev_of_offset_one (r := r') hread' hmore + hr'_rev hr'_off hend + rwa [hgetLastD] at hm + have hS' : Sound r' res (truncatedRangeRev r L) := hmin'.sound + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · -- **Soundness.** Agreement on `[last, end_.key)` plus the capacity bound keeps the + -- begin resolution at or below the reported last. + have step : ∀ db db', AgreeOn (truncatedRangeRev r L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hcomp' : RangeRead.Compatible db r' res := hreduce db hcomp + have hcomp'' : RangeRead.Compatible db' r' res := (hS' db db' hag').mp hcomp' + have hbetweenEq' : between db ((L : Key) : WithTop Key) r.end_.key = + between db' ((L : Key) : WithTop Key) r.end_.key := by + have h := hag' + unfold AgreeOn ConflictRange.keys at h + unfold between + exact h + -- The reported last is a stored key lying below `end_.key`. + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hLc : L ∈ RangeRead.completeKeys db r := hpre.subset hLmem + have hLinfo := RangeRead.mem_completeKeys.mp hLc + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := hLinfo.2.1 + have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ := hLinfo.2.2 + have hLE : ((L : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_one hend hLinfo.1 hLres + have hEre : r.end_.key ≤ resolve db r.end_ := key_le_resolve_of_one_le (by omega) + -- The stored keys in `[last, end_.key)` are exactly the reported ones. + have hbetweenFil : between db ((L : Key) : WithTop Key) r.end_.key = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).filter + (fun k => decide (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key))) := by + unfold between RangeRead.inRange + rw [List.filter_filter] + apply List.filter_congr + intro k hk + rw [← Bool.decide_and, decide_eq_decide] + constructor + · rintro ⟨hLk, hkE⟩ + exact ⟨hLk, le_trans hrb hLk, lt_of_lt_of_le hkE hEre⟩ + · rintro ⟨hLk, _, hkre⟩ + exact ⟨hLk, lt_end_key_of_lt_resolve_end_one hend hk hkre⟩ + have hbetweenLen : (between db ((L : Key) : WithTop Key) r.end_.key).length = + res.keys.length := by + have hfilW : (RangeRead.completeKeys db r).filter + (fun k => decide (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key))) = + res.keys := by + have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + rw [← hLdef] at h + simpa only [WithTop.coe_le_coe] using h + have hlen := congrArg List.length hfilW + have hck : RangeRead.completeKeys db r = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by + unfold RangeRead.completeKeys + simp [hrev] + rw [hck, List.filter_reverse, List.length_reverse] at hlen + rw [hbetweenFil] + exact hlen + have hbetweenLen' : (between db' ((L : Key) : WithTop Key) r.end_.key).length = + res.keys.length := by + rw [← hbetweenEq']; exact hbetweenLen + -- The extra stored keys above `end_.key` are bounded by the interval cardinality. + have hcapNat : (between db' r.end_.key r.begin.key).length + res.keys.length ≤ + beginSlack r + 1 := by + have hb : ((between db' r.end_.key r.begin.key).length : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key := + between_length_le_intervalCard (le_of_lt hcrossE) + have hsum : (((between db' r.end_.key r.begin.key).length + res.keys.length : ℕ) : + ℕ∞) ≤ ((beginSlack r + 1 : ℕ) : ℕ∞) := by + rw [Nat.cast_add] + calc ((between db' r.end_.key r.begin.key).length : ℕ∞) + + (res.keys.length : ℕ∞) + ≤ intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) := by + gcongr + _ ≤ (beginSlack r : ℕ∞) + 1 := hcap + _ = ((beginSlack r + 1 : ℕ) : ℕ∞) := by rw [Nat.cast_add, Nat.cast_one] + exact_mod_cast hsum + have hsplit := between_length_add (db := db') (L := ((L : Key) : WithTop Key)) + (E := r.end_.key) (B := r.begin.key) (le_of_lt hLE) (le_of_lt hcrossE) + have hboundLen : (between db' ((L : Key) : WithTop Key) r.begin.key).length ≤ + beginSlack r + 1 := by + rw [hsplit, hbetweenLen']; omega + have hboundInt : + ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + have hs : (beginSlack r : Int) = -r.begin.offset := by + unfold beginSlack + rw [Int.toNat_of_nonneg (show (0 : Int) ≤ 0 - r.begin.offset by omega)] + ring + have hble : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤ + (beginSlack r : Int) + 1 := by exact_mod_cast hboundLen + linarith + have hLdb' : L ∈ db'.keys := RangeRead.compatible_keys_subset hcomp'' hLmem + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb' hLcross hboundInt + have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb' + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp''.1, ?_⟩ + have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) hrev hrb' + have h2' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r') hr'_rev + (le_of_eq hres'_db') + have h2 := (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp + hcomp'' |>.2 + rw [h1, ← h2'] + exact h2 + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · -- **Necessity** is the under-end argument; it does not use `begin.key ≤ end_.key`. + intro k hk cr hs + have hkL : L ≤ k := WithTop.coe_le_coe.mp hk.1 + have hkend : ((k : Key) : WithTop Key) < r.end_.key := hk.2 + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hreadAll hkres cr hs + · by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result hreadAll hLmem (List.head_mem hne) hkL hkhead cr hs + · have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact necessary_head_to_end_key hreadAll hrev hne (le_of_eq hend) cr hs hheadk hkend + +/-- The reversed crossed negative-begin truncated small-gap cell. -/ +abbrev negCrossTruncRevCrossedSmall (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.key < r.begin.key ∧ r.end_.offset = 1 ∧ + intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) ≤ + (beginSlack r : ℕ∞) + 1 + +/-- The closed form of the reversed crossed negative-begin truncated small-gap cell: +`[last reported, end_.key)`. -/ +noncomputable def closedFormNegCrossTruncRevCrossedSmall (_r : RangeRead) (res : Result) : + ConflictRange := + truncatedRangeRev _r (res.keys.getLastD ([] : Key)) + +/-- **The reversed crossed negative-begin truncated small-gap closed form is +minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_crossedSmall {r : RangeRead} + {res : Result} (hread : Readable r res) (h : negCrossTruncRevCrossedSmall r res) : + Minimal r res (closedFormNegCrossTruncRevCrossedSmall r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hcap⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlast]; exact hcrossD + unfold closedFormNegCrossTruncRevCrossedSmall + rw [hlast] + exact minimal_truncatedRangeRev_of_negCross_crossedSmall hread hmore hrev hbeg hne hcross + hcrossE hend hcap + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevFinite.lean b/FormalConflictRange/NegCrossTruncRevFinite.lean new file mode 100644 index 0000000..756a439 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevFinite.lean @@ -0,0 +1,263 @@ +/- +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.NegCrossTruncRevTop + +/-! +# Reversed crossed negative-begin truncated cells with an under-end + +This module closes the reversed crossed negative-begin truncated cell whose end key +lies at or above `begin.key` and whose end offset is at most one. The minimal range is +`[last reported, end_.key)`, the reversed truncated range. + +The proof reduces the read to the offset-one begin keyed at the reported last, which +sees exactly the same stored keys at or above that last key. The begin-anchor count +bound `between_add_offset_le_one` shows that agreement on `[last, end_.key)` keeps the +begin resolution at or below the reported last, so the reduction is sound. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1200000 + +/-- **The general filter identity.** If the resolved begin lies at or below `last`, +filtering the reversed complete keys at or above `last` is the reversal of the stored +keys at or above `last` and below the resolved end. -/ +theorem completeKeys_filter_ge_eq_resolve_end {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = true) + (hrb : resolve db r.begin ≤ ((last : Key) : WithTop Key)) : + (RangeRead.completeKeys db r).filter (fun k => decide (last ≤ k)) = + ((db.keys.filter (fun k => decide (last ≤ k))).filter + (fun k => decide (((k : Key) : WithTop Key) < resolve db r.end_))).reverse := by + unfold RangeRead.completeKeys RangeRead.inRange + simp only [hrev, ite_true] + rw [List.filter_reverse, List.reverse_inj] + rw [List.filter_filter, List.filter_filter] + apply List.filter_congr + intro k _hk + conv_lhs => rw [← Bool.decide_and] + conv_rhs => rw [← Bool.decide_and] + apply Bool.decide_congr + constructor + · rintro ⟨hl, _hb, he⟩ + exact ⟨he, hl⟩ + · rintro ⟨he, hl⟩ + exact ⟨hl, le_trans hrb (WithTop.coe_le_coe.mpr hl), he⟩ + +/-- **The begin resolution stays at or below `last` from the anchor bound.** If `last` +is stored, lies strictly below `begin.key`, and the stored keys in `[last, begin.key)` +plus the begin offset minus one is at most zero, then the begin selector resolves at or +below `last`. -/ +theorem resolve_begin_le_of_between_bound {db : DB} {r : RangeRead} {last : Key} + (hlast_mem : last ∈ db.keys) + (hcross : ((last : Key) : WithTop Key) < r.begin.key) + (hbound : ((between db ((last : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0) : + resolve db r.begin ≤ ((last : Key) : WithTop Key) := by + obtain ⟨j, hjlt, hjeq⟩ := List.mem_iff_getElem.mp hlast_mem + have hcnt_last : cnt db ((last : Key) : WithTop Key) = j := by + rw [cnt_coe] + exact countP_lt_eq_of_getElem db.sorted hjlt hjeq + have hLleBegin : ((last : Key) : WithTop Key) ≤ r.begin.key := le_of_lt hcross + have hcnt_begin : cnt db r.begin.key = + j + (between db ((last : Key) : WithTop Key) r.begin.key).length := by + rw [cnt_eq_add_between db hLleBegin, hcnt_last] + have hanchor : r.begin.anchor db = (j : Int) + + ((between db ((last : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 := by + change (cnt db r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hcnt_begin]; push_cast; ring + have hanchor_le : r.begin.anchor db ≤ (j : Int) := by + rw [hanchor]; linarith + have htoNat : (r.begin.anchor db).toNat ≤ j := by + by_cases h : 0 ≤ r.begin.anchor db + · exact (Int.toNat_le).mpr hanchor_le + · have hz : (r.begin.anchor db).toNat = 0 := by omega + rw [hz]; omega + have hclamp : clampIndex db r.begin ≤ j := by + unfold clampIndex + exact le_trans (Nat.min_le_left _ _) htoNat + have hle := (resolve_le_getElem_iff (db := db) (s := r.begin) hjlt).mpr hclamp + rwa [hjeq] at hle + +/-- **Agreement on a range fixes the stored keys in any contained key interval.** If two +databases agree on `[last, endKey)` and `beginKey ≤ endKey`, they store the same keys in +`[last, beginKey)`. -/ +theorem between_eq_of_agree {db db' : DB} {last beginKey endKey : WithTop Key} + (hagree : AgreeOn ⟨last, endKey⟩ db db') + (_hlast : last ≤ beginKey) (hbegin : beginKey ≤ endKey) : + between db last beginKey = between db' last beginKey := by + have hcr : db.keys.filter (fun k : Key => decide (last ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < endKey)) = + db'.keys.filter (fun k : Key => decide (last ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < endKey)) := by + have h := hagree + unfold AgreeOn ConflictRange.keys at h + exact h + have h1 : db.keys.filter (fun k : Key => decide (last ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < beginKey)) = + (db.keys.filter (fun k : Key => decide (last ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < endKey))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < beginKey)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _hk + conv_rhs => rw [← Bool.decide_and] + apply Bool.decide_congr + constructor + · rintro ⟨hl, hb⟩ + exact ⟨hb, hl, lt_of_lt_of_le hb hbegin⟩ + · rintro ⟨hb, hl, _he⟩ + exact ⟨hl, hb⟩ + have h2 : db'.keys.filter (fun k : Key => decide (last ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < beginKey)) = + (db'.keys.filter (fun k : Key => decide (last ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < endKey))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < beginKey)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _hk + conv_rhs => rw [← Bool.decide_and] + apply Bool.decide_congr + constructor + · rintro ⟨hl, hb⟩ + exact ⟨hb, hl, lt_of_lt_of_le hb hbegin⟩ + · rintro ⟨hb, hl, _he⟩ + exact ⟨hl, hb⟩ + unfold between + rw [h1, h2, hcr] + +/-- **The reversed crossed negative-begin truncated under-end cell.** -/ +theorem minimal_truncatedRangeRev_of_negCross_underEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (_hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hunder : r.begin.key ≤ r.end_.key) (hend : r.end_.offset ≤ 1) : + Minimal r res (truncatedRangeRev r (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross + obtain ⟨db₀, hcomp₀⟩ := hread + have hreadAll : Readable r res := ⟨db₀, hcomp₀⟩ + let r' : RangeRead := + { r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } } + have hr'_rev : r'.reverse = true := by simp [r', hrev] + have hr'_end : r'.end_ = r.end_ := rfl + have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) := + rfl + have hr'_off : r'.begin.offset = 1 := rfl + -- The reduced read sees exactly the reported keys. + have hreduce : ∀ (db : DB), RangeRead.Compatible db r res → + RangeRead.Compatible db r' res := by + intro db hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = + res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hres' : resolve db r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb + have hLck_filter : (RangeRead.completeKeys db r').filter (fun k => decide (L ≤ k)) = + RangeRead.completeKeys db r' := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hk' := (RangeRead.mem_completeKeys.mp hk).2.1 + exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk') + have hck' : RangeRead.completeKeys db r' = res.keys := by + rw [← hLck_filter] + exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) (r₂ := r') + hrev hr'_rev hr'_end hrb (le_of_eq hres')).symm.trans hfil + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck'] + have hread' : Readable r' res := ⟨db₀, hreduce db₀ hcomp₀⟩ + have hgetLastD : res.keys.getLastD ([] : Key) = L := by + rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hmin' : Minimal r' res (truncatedRangeRev r L) := by + rcases lt_or_eq_of_le hend with hlt | heq + · have hle : r.end_.offset ≤ 0 := by omega + have hm := minimal_truncatedRangeRev_of_end_offset_nonpos (r := r') hread' hmore + hr'_rev hr'_off (by simpa [r'] using hle) + rwa [hgetLastD] at hm + · have hm := minimal_truncatedRangeRev_of_offset_one (r := r') hread' hmore + hr'_rev hr'_off heq + rwa [hgetLastD] at hm + have hS' : Sound r' res (truncatedRangeRev r L) := hmin'.sound + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · have step : ∀ db db', AgreeOn (truncatedRangeRev r L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hcomp' : RangeRead.Compatible db r' res := hreduce db hcomp + have hcomp'' : RangeRead.Compatible db' r' res := (hS' db db' hag').mp hcomp' + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = + res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hbound : ((between db ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := + between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + have hagree' : AgreeOn ⟨((L : Key) : WithTop Key), r.end_.key⟩ db db' := by + simpa [truncatedRangeRev] using hag' + have hbetween_eq : between db ((L : Key) : WithTop Key) r.begin.key = + between db' ((L : Key) : WithTop Key) r.begin.key := + between_eq_of_agree (db := db) (db' := db') hagree' (le_of_lt hLcross) hunder + have hbound' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [← hbetween_eq]; exact hbound + have hLdb' : L ∈ db'.keys := RangeRead.compatible_keys_subset hcomp'' hLmem + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_between_bound hLdb' hLcross hbound' + have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb' + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp''.1, ?_⟩ + have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) hrev hrb' + have h2' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r') hr'_rev + (le_of_eq hres'_db') + have h2 := (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp + hcomp'' |>.2 + rw [h1, ← h2'] + exact h2 + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hk cr hs + have hkL : L ≤ k := WithTop.coe_le_coe.mp hk.1 + have hkend : ((k : Key) : WithTop Key) < r.end_.key := hk.2 + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hreadAll hkres cr hs + · by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result hreadAll hLmem (List.head_mem hne) hkL hkhead cr hs + · have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact necessary_head_to_end_key hreadAll hrev hne hend cr hs hheadk hkend + +/-- The reversed crossed negative-begin truncated under-end cell. -/ +abbrev negCrossTruncRevUnder (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key ≤ r.end_.key ∧ r.end_.offset ≤ 1 + +/-- The closed form of the reversed crossed negative-begin truncated under-end cell. -/ +noncomputable def closedFormNegCrossTruncRevUnder (_r : RangeRead) (res : Result) : + ConflictRange := + truncatedRangeRev _r (res.keys.getLastD ([] : Key)) + +/-- **The reversed crossed negative-begin truncated under-end closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_under {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevUnder r res) : + Minimal r res (closedFormNegCrossTruncRevUnder r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hunder, hend⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlast]; exact hcrossD + unfold closedFormNegCrossTruncRevUnder + rw [hlast] + exact minimal_truncatedRangeRev_of_negCross_underEnd hread hmore hrev hbeg hne hcross + hunder hend + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevNonpos.lean b/FormalConflictRange/NegCrossTruncRevNonpos.lean new file mode 100644 index 0000000..cbc58d3 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevNonpos.lean @@ -0,0 +1,78 @@ +/- +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.NegCrossTruncRevBeginTop + +/-! +# Reversed crossed negative-begin truncated cells in the nonpositive-begin regime + +This module wires two already-total reversed truncated rules into the closed-form +dispatcher. Both concern a reversed truncated result at a nonpositive begin offset +whose begin key lies at or below the least reported key, so the lower cut never rises +above the reported block. + +* If the end overshoots (`2 ≤ end_.offset`) and every reported key is below `end_.key`, + the resolved end is the sentinel in every compatible database and the minimal range + is `[last, ⊤)`. +* If the end has offset one, the cut is exactly at `end_.key` and the minimal range is + the reversed truncated range `[last, end_.key)`. + +The minimality proofs are the corresponding total theorems of `Sound.lean`. +-/ + +namespace FormalConflictRange + +/-- The nonpositive-begin reversed truncated overshoot cell: the begin key is at or +below the least reported key, the end overshoots, and every reported key lies below +`end_.key`. -/ +abbrev negCrossTruncRevNonposOvershoot (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset ≤ 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key + +/-- The closed form of the nonpositive-begin reversed truncated overshoot cell. -/ +noncomputable def closedFormNegCrossTruncRevNonposOvershoot (_r : RangeRead) + (res : Result) : ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The nonpositive-begin reversed truncated overshoot closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_nonposOvershoot {r : RangeRead} + {res : Result} (hread : Readable r res) (h : negCrossTruncRevNonposOvershoot r res) : + Minimal r res (closedFormNegCrossTruncRevNonposOvershoot r res) := by + obtain ⟨hmore, hrev, hle, hne, hend, hlo, hhi⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + unfold closedFormNegCrossTruncRevNonposOvershoot + rw [hlast] + exact minimal_topOfLast_of_end_offset_ge_two_rev_nonpos hread hmore hrev hne hle hend + (by rw [hlast] at hlo; exact hlo) hhi + +/-- The nonpositive-begin reversed truncated under-end cell at end offset one: the +begin key is at or below the least reported key and the end has offset one. -/ +abbrev negCrossTruncRevNonposUnder (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset ≤ 0 ∧ res.keys ≠ [] ∧ + r.end_.offset = 1 ∧ + r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + +/-- The closed form of the nonpositive-begin reversed truncated under-end cell. -/ +noncomputable def closedFormNegCrossTruncRevNonposUnder (r : RangeRead) (res : Result) : + ConflictRange := + truncatedRangeRev r (res.keys.getLastD ([] : Key)) + +/-- **The nonpositive-begin reversed truncated under-end closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_nonposUnder {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevNonposUnder r res) : + Minimal r res (closedFormNegCrossTruncRevNonposUnder r res) := by + obtain ⟨hmore, hrev, hle, hne, hend, hlo⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + unfold closedFormNegCrossTruncRevNonposUnder + rw [hlast] + exact minimal_truncatedRangeRev_of_end_offset_one_nonpos hread hmore hrev hne hle hend + (by rw [hlast] at hlo; exact hlo) + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevResidual.lean b/FormalConflictRange/NegCrossTruncRevResidual.lean new file mode 100644 index 0000000..026ffb4 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevResidual.lean @@ -0,0 +1,1118 @@ +/- +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.NegCrossTruncRevCrossedNonpos +import FormalConflictRange.NonposRevClosedForm + +/-! +# The reversed crossed negative-begin truncated residual + +This module closes the last reversed crossed negative-begin truncated cell: at end +offset at least two, at least one reported key lies at or above `end_.key` (the head +is at or above `end_.key`). The minimal range is + +* `[min last end_.key, max begin.key (keyAfter head))` on the finite branch + `#reported ≥ end_.key = end_.offset - 1`; +* `[min last end_.key, ⊤)` on the top branch `#reported ≥ end_.key < end_.offset - 1`. + +The key generic fact is the count bound `completeKeys_count_ge_le_end_offset`: the number +of complete keys at or above `end_.key` is at most `end_.offset - 1`, for *every* read. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1200000 + +/-! ### Reduction to the offset-one read at the reported last -/ + +/-- Compatibility always descends to the read whose begin selector is keyed at the +reported last with offset one: that read sees exactly the reported keys. -/ +theorem compatible_reduce_of_compatible {r : RangeRead} {res : Result} {db : DB} {L : Key} + (hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ []) + (hL : L = res.keys.getLast hne) + (hcomp : RangeRead.Compatible db r res) : + RangeRead.Compatible db { r with begin := ⟨(L : WithTop Key), 1⟩ } res := by + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by + rw [hL] + exact (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hres' : resolve db ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead).begin = + ((L : Key) : WithTop Key) := resolve_offset_one_key_of_mem hLdb + have hLck_filter : (RangeRead.completeKeys db + ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead)).filter + (fun k => decide (L ≤ k)) = + RangeRead.completeKeys db ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead) := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hk' := (RangeRead.mem_completeKeys.mp hk).2.1 + exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk') + have hck' : RangeRead.completeKeys db ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead) + = res.keys := by + rw [← hLck_filter] + exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) + (r₂ := { r with begin := ⟨(L : WithTop Key), 1⟩ }) hrev (by simpa using hrev) rfl hrb + (le_of_eq hres')).symm.trans hfil + refine ⟨by simpa [RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck'] + +/-- Compatibility ascends from the offset-one read keyed at the reported last whenever the +original begin resolution lies at or below that last key. -/ +theorem compatible_of_compatible_reduce {r : RangeRead} {res : Result} {db : DB} {L : Key} + (hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ []) + (hL : L = res.keys.getLast hne) + (hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key)) + (hcomp' : RangeRead.Compatible db { r with begin := ⟨(L : WithTop Key), 1⟩ } res) : + RangeRead.Compatible db r res := by + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hpre' : res.keys <+: RangeRead.completeKeys db + ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead) := + RangeRead.compatible_keys_prefix hcomp' + have hLdb : L ∈ db.keys := + RangeRead.compatible_keys_subset hcomp' hLmem + have hres' : resolve db ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead).begin = + ((L : Key) : WithTop Key) := resolve_offset_one_key_of_mem hLdb + have hfil' : (RangeRead.completeKeys db + ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead)).filter + (fun k => decide (L ≤ k)) = res.keys := by + have := (compatible_iff_truncated_keys_rev (db := db) (by simpa using hrev) hmore hne).mp + hcomp' |>.2 + simpa [hL] using this + have hLck_filter : (RangeRead.completeKeys db + ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead)).filter + (fun k => decide (L ≤ k)) = + RangeRead.completeKeys db ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead) := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hk' := (RangeRead.mem_completeKeys.mp hk).2.1 + exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk') + have hck : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by + rw [completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) + (r₂ := { r with begin := ⟨(L : WithTop Key), 1⟩ }) hrev (by simpa using hrev) rfl hrb + (le_of_eq hres')] + exact hfil' + refine (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mpr + ⟨by simpa [RangeRead.withinLimit] using hcomp'.1, ?_⟩ + simpa [hL] using hck + +/-- If the original begin key is already at or below the reported last, its nonpositive +offset forces the begin resolution below that key. The offset-one reduction therefore +has exactly the same compatible databases, not merely a superset of them. -/ +theorem compatible_iff_reduce_of_begin_key_le {r : RangeRead} {res : Result} {L : Key} + (hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ []) + (hL : L = res.keys.getLast hne) (hbeg : r.begin.offset ≤ 0) + (hBL : r.begin.key ≤ ((L : Key) : WithTop Key)) (db : DB) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with begin := ⟨(L : WithTop Key), 1⟩ } res := by + constructor + · exact compatible_reduce_of_compatible hrev hmore hne hL + · intro hcomp' + apply compatible_of_compatible_reduce hrev hmore hne hL _ hcomp' + exact le_trans (resolve_le_key_of_offset_nonpos (db := db) (s := r.begin) hbeg) hBL + +/-! ### The residual cell and its closed form -/ + +/-- The last reversed crossed negative-begin truncated residual: at end offset at least +two, at least one reported key lies at or above `end_.key`. -/ +abbrev negCrossTruncRevResidual (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + ∃ k ∈ res.keys, r.end_.key ≤ ((k : Key) : WithTop Key) + +/-- The closed form of the residual: `[min last end_.key, max begin.key (keyAfter head))` +on the finite branch, and `[min last end_.key, ⊤)` otherwise. -/ +noncomputable def closedFormNegCrossTruncRevResidual (r : RangeRead) (res : Result) : + ConflictRange := + let L : WithTop Key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + let H : Key := res.keys.headD ([] : Key) + let c := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + let e := (r.end_.offset - 1).toNat + let a := min L r.end_.key + if c = e then ⟨a, max r.begin.key ((keyAfter H : Key) : WithTop Key)⟩ + else ⟨a, ⊤⟩ + +/-- The residual closed form's begin is at or below the reported last. -/ +theorem closedFormNegCrossTruncRevResidual_begin_le_last {r : RangeRead} {res : Result} + {L : Key} (hL : L = res.keys.getLastD ([] : Key)) : + (closedFormNegCrossTruncRevResidual r res).begin ≤ ((L : Key) : WithTop Key) := by + unfold closedFormNegCrossTruncRevResidual + dsimp only + rw [← hL] + split <;> exact min_le_left _ _ + +/-- The residual closed form's end is at or above `begin.key`. -/ +theorem closedFormNegCrossTruncRevResidual_end_ge_begin {r : RangeRead} {res : Result} + (hmore : res.more = true) (hne : res.keys ≠ []) : + r.begin.key ≤ (closedFormNegCrossTruncRevResidual r res).end_ := by + unfold closedFormNegCrossTruncRevResidual + dsimp only + split + · exact le_max_left _ _ + · exact le_top + +/-- **The `[end_.key, resolve end_)` length at a genuine end key.** At end offset at +least two, if the end selector does not resolve to the sentinel, then `resolve end` is +the `(end_.offset - 1)`-th stored key at or above `end_.key`, so `[end_.key, resolve +end_)` contains exactly `(end_.offset - 1).toNat` stored keys. -/ +theorem length_between_end_resolve_eq_of_ne_top {db : DB} {r : RangeRead} + (hend : 2 ≤ r.end_.offset) + (hne_top : resolve db r.end_ ≠ (⊤ : WithTop Key)) : + (between db r.end_.key (resolve db r.end_)).length = (r.end_.offset - 1).toNat := by + set E : WithTop Key := r.end_.key with hE + set e : ℕ := (r.end_.offset - 1).toNat with he + have hoff : 1 ≤ r.end_.offset := by omega + set Q : List Key := db.keys.filter (fun k : Key => decide (E ≤ (k : WithTop Key))) with hQ + have hQs : Q.SortedLT := by + rw [hQ] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hres := resolve_eq_filter_getElem? (db := db) (s := r.end_) hoff + have hres' : resolve db r.end_ = (match Q[e]? with + | some k => ((k : Key) : WithTop Key) | none => (⊤ : WithTop Key)) := by + have h := hres + rw [← hE, ← he, ← hQ] at h + exact h + have hsome : Q[e]? ≠ none := by + intro hnone + apply hne_top + rw [hres'] + simp [hnone] + obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.mp hsome + obtain ⟨hek, hkeq⟩ := List.getElem?_eq_some_iff.mp hk + have hresolve : resolve db r.end_ = ((Q[e] : Key) : WithTop Key) := by + rw [hres'] + rw [show Q[e]? = some (Q[e]) from by rw [hk, hkeq]] + have htake : between db E (resolve db r.end_) = Q.take e := by + rw [hresolve] + have hbetween_split : between db E ((Q[e] : Key) : WithTop Key) = + (db.keys.filter (fun k : Key => decide (E ≤ (k : WithTop Key)))).filter + (fun k : Key => decide ((k : WithTop Key) < ((Q[e] : Key) : WithTop Key))) := by + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro x _ + rw [Bool.decide_and, Bool.and_comm] + rw [hbetween_split, ← hQ] + rw [filter_lt_eq_take_countP hQs] + congr 1 + rw [countP_lt_coe] + exact countP_lt_eq_of_getElem hQs hek rfl + rw [htake, List.length_take, Nat.min_eq_left (le_of_lt hek)] + +/-- **A sub-sentinel count forces the sentinel end.** If the stored keys at or above +`end_.key` number fewer than `end_.offset - 1`, a genuine end resolution is impossible: +it would require exactly that many stored keys in `[end_.key, resolve end_)`, a sublist +of `[end_.key, ⊤)`. -/ +theorem resolve_end_eq_top_of_between_top_lt {db : DB} {r : RangeRead} + (hend : 2 ≤ r.end_.offset) + (htop : (between db r.end_.key (⊤ : WithTop Key)).length < + (r.end_.offset - 1).toNat) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hne_top + have hlenE := length_between_end_resolve_eq_of_ne_top (db := db) (r := r) hend hne_top + have hsub : List.Sublist (between db r.end_.key (resolve db r.end_)) + (between db r.end_.key (⊤ : WithTop Key)) := by + unfold between + apply filter_sublist_filter_of_imp + intro k hk + exact decide_eq_true ⟨(of_decide_eq_true hk).1, WithTop.coe_lt_top k⟩ + have hle := hsub.length_le + omega + +/-- **Counting above the end key across the gap.** If the stored keys at or above the +reported last number `t`, then the stored keys at or above `end_.key` are the `t` +reported keys together with the gap `[end_.key, last)`. -/ +theorem between_top_length_eq_add {db : DB} {E L : WithTop Key} {t : ℕ} + (hEL : E ≤ L) (hbetw : (between db L (⊤ : WithTop Key)).length = t) : + (between db E (⊤ : WithTop Key)).length = + (between db E L).length + t := by + have h1 : cnt db (⊤ : WithTop Key) = + cnt db L + (between db L (⊤ : WithTop Key)).length := + cnt_eq_add_between db le_top + have h2 : cnt db L = cnt db E + (between db E L).length := + cnt_eq_add_between db hEL + have h3 : cnt db (⊤ : WithTop Key) = + cnt db E + (between db E (⊤ : WithTop Key)).length := + cnt_eq_add_between db le_top + rw [hbetw] at h1 + omega + +/-- **The small-gap end count.** In the crossed top cell, a compatible database whose +reported keys are the minimal stored keys at or above the reported last cannot have a +genuine end resolution when the gap `[end_.key, last)` plus the reported keys are fewer +than `end_.offset - 1`. -/ +theorem resolve_end_eq_top_of_small_gap {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hcross : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) + (hsmall : intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞) < ((r.end_.offset - 1).toNat : ℕ∞)) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hne_top + set L : Key := res.keys.getLast hne with hLdef + set E : WithTop Key := r.end_.key with hE + set R : WithTop Key := resolve db r.end_ with hR + set b : ℕ := (between db E ((L : Key) : WithTop Key)).length with hb + set t : ℕ := res.keys.length with ht + set e : ℕ := (r.end_.offset - 1).toNat with he + have hEL : E ≤ ((L : Key) : WithTop Key) := le_of_lt (by simpa [E, L] using hcross) + have hLR : ((L : Key) : WithTop Key) < R := by + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLck : L ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset hLmem + have := (RangeRead.mem_completeKeys.mp hLck).2.2 + simpa [R] using this + have hbetween := res_keys_eq_reverse_between_resolve_end (db := db) (r := r) (res := res) + hcomp hmore hrev hne + have hlenLR : (between db ((L : Key) : WithTop Key) R).length = t := by + rw [← hLdef, ← hR] at hbetween + have h := congrArg List.length hbetween + rw [List.length_reverse] at h + rw [ht] + exact h.symm + have hsplit : (between db E R).length = b + t := by + have hRle : ((L : Key) : WithTop Key) ≤ R := le_of_lt hLR + have h1 : cnt db R = cnt db ((L : Key) : WithTop Key) + + (between db ((L : Key) : WithTop Key) R).length := cnt_eq_add_between db hRle + have h2 : cnt db ((L : Key) : WithTop Key) = cnt db E + b := by + rw [hb]; exact cnt_eq_add_between db hEL + have h3 : cnt db R = cnt db E + (between db E R).length := by + exact cnt_eq_add_between db (le_of_lt (lt_of_le_of_lt hEL hLR)) + rw [hlenLR] at h1 + omega + have hlenE : (between db E R).length = e := by + have := length_between_end_resolve_eq_of_ne_top (db := db) (r := r) hend hne_top + simpa [E, R, he] using this + have hcap : ((b : ℕ) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by + rw [hb]; exact between_length_le_intervalCard (db := db) hEL + have hsmall' : intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) < (e : ℕ∞) := by + simpa [E, L, t, e] using hsmall + have hlt : ((b + t : ℕ) : ℕ∞) < (e : ℕ∞) := by + rw [ENat.natCast_add] + exact lt_of_le_of_lt (add_le_add hcap le_rfl) hsmall' + have hltnat : b + t < e := ENat.natCast_lt_natCast.mp hlt + omega + +/-- **Filtering `between` at an intermediate bound.** For `L ≤ E`, the stored keys in +`[E, B)` are exactly those in `[L, B)` that lie at or above `E`. -/ +theorem between_filter_ge_eq {db : DB} {L E B : WithTop Key} (hLE : L ≤ E) : + (between db L B).filter (fun k : Key => decide (E ≤ (k : WithTop Key))) = + between db E B := by + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro x _ + by_cases hE : E ≤ (x : WithTop Key) + · have hLx : L ≤ (x : WithTop Key) := le_trans hLE hE + simp [hE, hLx] + · simp [hE] + +/-- **The count identity at a genuine end resolution.** For a compatible truncated +reversed result, `[end_.key, resolve end_)` contains exactly the reported keys at or +above `end_.key`, provided the reported last lies at or below `end_.key`. -/ +theorem length_between_end_resolve_eq_countP {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hLE : ((res.keys.getLast hne : Key) : WithTop Key) ≤ r.end_.key) : + (between db r.end_.key (resolve db r.end_)).length = + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + set L : Key := res.keys.getLast hne with hL + set E : WithTop Key := r.end_.key with hE + have hLE' : ((L : Key) : WithTop Key) ≤ E := hLE + have hbetween : between db ((L : Key) : WithTop Key) (resolve db r.end_) = res.keys.reverse := by + have h := res_keys_eq_reverse_between_resolve_end (db := db) (r := r) (res := res) + hcomp hmore hrev hne + rw [← hL] at h + rw [h, List.reverse_reverse] + have hfil := between_filter_ge_eq (db := db) (L := ((L : Key) : WithTop Key)) (E := E) + (B := resolve db r.end_) hLE' + rw [← hfil, hbetween, List.filter_reverse, List.length_reverse, List.countP_eq_length_filter] + +/-- **Every stored key below the end resolution lies at or below the reported head.** +In a compatible reversed truncated result, every stored key in `[end_.key, resolve +end_)` lies at or below the reported head `res.keys.head`: keys at or above the +reported last are themselves reported (the reported keys are the stored keys in +`[last, resolve end_)` read backwards), and keys below the reported last are below the +head. -/ +theorem between_end_resolve_le_head {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) : + ∀ k ∈ db.keys, r.end_.key ≤ ((k : Key) : WithTop Key) → + ((k : Key) : WithTop Key) < resolve db r.end_ → + ((k : Key) : WithTop Key) ≤ ((res.keys.head hne : Key) : WithTop Key) := by + intro k hkdb hEk hkR + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set W : List Key := between db ((L : Key) : WithTop Key) (resolve db r.end_) with hWdef + have hWres : res.keys = W.reverse := by + rw [hWdef] + exact res_keys_eq_reverse_between_resolve_end hcomp hmore hrev hne + have hWne : W ≠ [] := by + intro h + apply hne + rw [hWres, h] + simp + have hWs : W.SortedLT := by + rw [hWdef] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hHs : H = W.getLast hWne := by + rw [hH] + simp [hWres] + have hLs : L = W.head hWne := by + rw [hL] + simp [hWres] + have hWL : ∀ x ∈ W, x ≤ H := by + intro x hx + rw [hHs] + exact (hWs.pairwise.imp (fun h => le_of_lt h)).rel_getLast hx + have hLH : ((L : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := by + rw [hLs, hHs] + exact WithTop.coe_le_coe.mpr + ((hWs.pairwise.imp (fun h => le_of_lt h)).rel_getLast (List.head_mem hWne)) + by_cases hkL : ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · have hkW : k ∈ W := by + rw [hWdef] + exact List.mem_filter.mpr ⟨hkdb, by + rw [decide_eq_true_eq] + exact ⟨hkL, hkR⟩⟩ + exact WithTop.coe_le_coe.mpr (hWL k hkW) + · have hkLt : ((k : Key) : WithTop Key) < ((L : Key) : WithTop Key) := lt_of_not_ge hkL + exact le_trans (le_of_lt hkLt) hLH + +/-- **Every stored key lies at or below the reported head when the end is the sentinel.** +With a sentinel end key the reported keys are the stored keys in `[last, ⊤)` read +backwards, so keys at or above the reported last are reported and keys below it are below +the head. -/ +theorem db_keys_le_head_of_compat_sentinel {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) : + ∀ k ∈ db.keys, ((k : Key) : WithTop Key) ≤ ((res.keys.head hne : Key) : WithTop Key) := by + intro k hkdb + have hresolve : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top (db := db) (s := r.end_) hendkey hendoff + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set W : List Key := between db ((L : Key) : WithTop Key) (resolve db r.end_) with hWdef + have hWres : res.keys = W.reverse := by + rw [hWdef] + exact res_keys_eq_reverse_between_resolve_end hcomp hmore hrev hne + have hWne : W ≠ [] := by + intro h + apply hne + rw [hWres, h] + simp + have hWs : W.SortedLT := by + rw [hWdef] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hHs : H = W.getLast hWne := by + rw [hH] + simp [hWres] + have hLs : L = W.head hWne := by + rw [hL] + simp [hWres] + have hWL : ∀ x ∈ W, x ≤ H := by + intro x hx + rw [hHs] + exact (hWs.pairwise.imp (fun h => le_of_lt h)).rel_getLast hx + have hLH : ((L : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := by + rw [hLs, hHs] + exact WithTop.coe_le_coe.mpr + ((hWs.pairwise.imp (fun h => le_of_lt h)).rel_getLast (List.head_mem hWne)) + by_cases hkL : ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · have hkW : k ∈ W := by + rw [hWdef] + exact List.mem_filter.mpr ⟨hkdb, by + rw [decide_eq_true_eq] + exact ⟨hkL, by rw [hresolve]; exact WithTop.coe_lt_top k⟩⟩ + exact WithTop.coe_le_coe.mpr (hWL k hkW) + · exact le_trans (le_of_lt (lt_of_not_ge hkL)) hLH + +/-- **Splitting a counting interval at the reported head.** If every stored key in +`[E, B)` lies at or below `H`, and `H` is itself stored in `[E, B)`, then `[E, B)` is +`[E, H)` together with the single key `H`. -/ +theorem between_eq_between_head_append {db : DB} {E B : WithTop Key} {H : Key} + (hEH : E ≤ ((H : Key) : WithTop Key)) (hHB : ((H : Key) : WithTop Key) < B) + (hHdb : H ∈ db.keys) + (hmax : ∀ k ∈ db.keys, E ≤ ((k : Key) : WithTop Key) → + ((k : Key) : WithTop Key) < B → ((k : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key)) : + between db E B = between db E ((H : Key) : WithTop Key) ++ [H] := by + apply List.SortedLT.eq_of_mem_iff + · unfold between + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · rw [List.sortedLT_iff_pairwise, List.pairwise_append] + refine ⟨?_, ?_, ?_⟩ + · rw [← List.sortedLT_iff_pairwise] + unfold between + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · exact List.pairwise_singleton _ _ + · intro x hx y hy + rw [List.mem_singleton] at hy + subst hy + unfold between at hx + have hx' := of_decide_eq_true (List.mem_filter.mp hx).2 + exact WithTop.coe_lt_coe.mp hx'.2 + · intro k + simp only [between, List.mem_filter, List.mem_append, List.mem_singleton, + decide_eq_true_eq] + constructor + · rintro ⟨hkdb, hEk, hkB⟩ + by_cases hkH : ((k : Key) : WithTop Key) < ((H : Key) : WithTop Key) + · exact Or.inl ⟨hkdb, hEk, hkH⟩ + · have hkHle : ((k : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := + hmax k hkdb hEk hkB + have hkHeq : k = H := WithTop.coe_inj.mp (le_antisymm hkHle (le_of_not_gt hkH)) + exact Or.inr hkHeq + · rintro (⟨hkdb, hEk, hkH⟩ | hkHeq) + · exact ⟨hkdb, hEk, lt_trans hkH hHB⟩ + · subst hkHeq + exact ⟨hHdb, hEH, hHB⟩ + +/-- **At a top-branch count, every compatible database resolves the end to the +sentinel.** If the reported keys at or above `end_.key` number strictly fewer than +`end_.offset - 1`, then a compatible database cannot resolve the end to a genuine key: +the stored keys in `[end_.key, resolve end_)` would number exactly `end_.offset - 1`, +but the count identity says they number the reported keys at or above `end_.key`. -/ +theorem resolve_end_eq_top_of_count_lt {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hLE : ((res.keys.getLast hne : Key) : WithTop Key) ≤ r.end_.key) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hne_top + have hlen_c := length_between_end_resolve_eq_countP (db := db) (r := r) (res := res) + hcomp hmore hrev hne hLE + have hlen_e := length_between_end_resolve_eq_of_ne_top (db := db) (r := r) + hend hne_top + omega + +/-- **The room criterion: a genuine end resolution needs one more key than the gap +holds.** At a genuine end resolution the stored keys in `[end_.key, resolve end_)` +number `end_.offset - 1`; all of them lie at or below the reported head `H`, and `H` +itself is one of them, so `[end_.key, H)` holds `end_.offset - 2` of them. Hence if the +gap `[end_.key, H)` can hold at most `end_.offset - 3` keys, the end must resolve to the +sentinel. -/ +theorem resolve_end_eq_top_of_room {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hres : ∃ k ∈ res.keys, r.end_.key ≤ ((k : Key) : WithTop Key)) + (hroom : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 2 ≤ + (r.end_.offset - 1).toNat) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hne_top + set H : Key := res.keys.head hne with hH + set L : Key := res.keys.getLast hne with hL + set W : List Key := between db ((L : Key) : WithTop Key) (resolve db r.end_) with hWdef + have hWres : res.keys = W.reverse := by + rw [hWdef] + exact res_keys_eq_reverse_between_resolve_end hcomp hmore hrev hne + have hWne : W ≠ [] := by + intro h + apply hne + rw [hWres, h] + simp + have hWs : W.SortedLT := by + rw [hWdef] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hHs : H = W.getLast hWne := by + rw [hH] + simp [hWres] + have hWL : ∀ x ∈ W, x ≤ H := by + intro x hx + rw [hHs] + exact (hWs.pairwise.imp (fun h => le_of_lt h)).rel_getLast hx + have hHmem : H ∈ res.keys := by + rw [hH]; exact List.head_mem hne + have hHdb : H ∈ db.keys := RangeRead.compatible_keys_subset hcomp hHmem + have hHW : H ∈ W := by + have h := hHmem + rw [hWres] at h + exact List.mem_reverse.mp h + have hHlt : ((H : Key) : WithTop Key) < resolve db r.end_ := by + have hf := List.mem_filter.mp hHW + exact (of_decide_eq_true hf.2).2 + obtain ⟨k₀, hk₀mem, hk₀E⟩ := hres + have hk₀W : k₀ ∈ W := by + have h := hk₀mem + rw [hWres] at h + exact List.mem_reverse.mp h + have hEH : r.end_.key ≤ ((H : Key) : WithTop Key) := + le_trans hk₀E (WithTop.coe_le_coe.mpr (hWL k₀ hk₀W)) + have hmax : ∀ k ∈ db.keys, r.end_.key ≤ ((k : Key) : WithTop Key) → + ((k : Key) : WithTop Key) < resolve db r.end_ → + ((k : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := + between_end_resolve_le_head hcomp hmore hrev hne + have hsplit := between_eq_between_head_append (db := db) (E := r.end_.key) + (B := resolve db r.end_) (H := H) hEH hHlt hHdb hmax + have he := length_between_end_resolve_eq_of_ne_top (db := db) (r := r) hend hne_top + have hlenFull : (between db r.end_.key (resolve db r.end_)).length = + (between db r.end_.key ((H : Key) : WithTop Key)).length + 1 := by + rw [hsplit, List.length_append] + simp + have he1 : 1 ≤ (r.end_.offset - 1).toNat := by omega + have hsmall : (between db r.end_.key ((H : Key) : WithTop Key)).length = + (r.end_.offset - 1).toNat - 1 := by omega + have hcard : ((between db r.end_.key ((H : Key) : WithTop Key)).length : ℕ∞) ≤ + intervalCard r.end_.key ((H : Key) : WithTop Key) := + between_length_le_intervalCard (db := db) hEH + have hle1 : (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((H : Key) : WithTop Key) := by + rw [← hsmall]; exact hcard + have hroom' : ((2 : ℕ) : ℕ∞) + intervalCard r.end_.key ((H : Key) : WithTop Key) ≤ + ((r.end_.offset - 1).toNat : ℕ∞) := by + simpa [add_comm] using hroom + have h3 : ((2 : ℕ) : ℕ∞) + (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) ≤ + ((r.end_.offset - 1).toNat : ℕ∞) := + le_trans (add_le_add_right hle1 2) hroom' + have h4 : (((r.end_.offset - 1).toNat + 1 : ℕ) : ℕ∞) ≤ + ((r.end_.offset - 1).toNat : ℕ∞) := by + rw [← ENat.natCast_add] at h3 + have h : 2 + ((r.end_.offset - 1).toNat - 1) = (r.end_.offset - 1).toNat + 1 := by + omega + rwa [h] at h3 + have : (r.end_.offset - 1).toNat + 1 ≤ (r.end_.offset - 1).toNat := + ENat.natCast_le_natCast.mp h4 + omega + +/-- A counting contradiction: an interval of cardinality `a` cannot hold `e` keys when +`a + 2 ≤ e`. -/ +private theorem not_room_of_length_ge {a : ℕ∞} {e : ℕ} + (hcard : (e : ℕ∞) ≤ a) (hroom : a + 2 ≤ (e : ℕ∞)) : False := by + have he_top : (e : ℕ∞) ≠ ⊤ := ENat.natCast_ne_top e + have ha_top : a ≠ ⊤ := by + intro h + rw [h] at hroom + simp at hroom + have h1 : ENat.toNat (a + 2) ≤ ENat.toNat (e : ℕ∞) := ENat.toNat_le_toNat hroom he_top + rw [ENat.toNat_add ha_top (by norm_num : (2 : ℕ∞) ≠ ⊤)] at h1 + rw [ENat.toNat_natCast] at h1 + rw [show ENat.toNat (2 : ℕ∞) = 2 from ENat.toNat_ofNat 2] at h1 + have h2 : e ≤ ENat.toNat a := ENat.toNat_le_toNat hcard ha_top + omega + +/-- **The room criterion with all stored keys below the reported head.** If every stored +key lies at or below the reported head, the room condition still forces the end selector +to the sentinel: a genuine resolution would place `end_.offset - 1` stored keys in +`[end_.key, resolve end_)`, and they either all lie below `H` (so the interval holds +`e` keys) or include `H` as their maximum (so the interval below `H` holds `e - 1` +keys). This is the converse needed to transfer compatibility from the sentinel-end +read. -/ +theorem resolve_end_eq_top_of_room_of_all_le_head {db : DB} {r : RangeRead} + {res : Result} + (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hres : ∃ k ∈ res.keys, r.end_.key ≤ ((k : Key) : WithTop Key)) + (hresdb : ∀ k ∈ res.keys, k ∈ db.keys) + (hall : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) ≤ + ((res.keys.head hne : Key) : WithTop Key)) + (hroom : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 2 ≤ + (r.end_.offset - 1).toNat) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hne_top + set H : Key := res.keys.head hne with hH + set E : WithTop Key := r.end_.key with hE + set K : WithTop Key := resolve db r.end_ with hK + obtain ⟨k₀, hk₀mem, hk₀E⟩ := hres + have hk₀H : ((k₀ : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := + hall k₀ (hresdb k₀ hk₀mem) + have hEH : E ≤ ((H : Key) : WithTop Key) := le_trans hk₀E hk₀H + have hHdb : H ∈ db.keys := hresdb H (by rw [hH]; exact List.head_mem hne) + have he := length_between_end_resolve_eq_of_ne_top (db := db) (r := r) hend hne_top + have he' : (between db E K).length = (r.end_.offset - 1).toNat := by + simpa [hE, hK] using he + have he1 : 1 ≤ (r.end_.offset - 1).toNat := by omega + by_cases hHK : ((H : Key) : WithTop Key) < K + · have hmax : ∀ k ∈ db.keys, E ≤ ((k : Key) : WithTop Key) → + ((k : Key) : WithTop Key) < K → + ((k : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := + fun k hk _ _ => hall k hk + have hsplit := between_eq_between_head_append (db := db) (E := E) (B := K) (H := H) + hEH hHK hHdb hmax + have hlenFull : (between db E K).length = + (between db E ((H : Key) : WithTop Key)).length + 1 := by + rw [hsplit, List.length_append] + simp + have hsmall : (between db E ((H : Key) : WithTop Key)).length = + (r.end_.offset - 1).toNat - 1 := by omega + have hcard : ((between db E ((H : Key) : WithTop Key)).length : ℕ∞) ≤ + intervalCard E ((H : Key) : WithTop Key) := + between_length_le_intervalCard (db := db) hEH + -- the same ENat contradiction as in `resolve_end_eq_top_of_room` + have hroom' : ((2 : ℕ) : ℕ∞) + intervalCard E ((H : Key) : WithTop Key) ≤ + ((r.end_.offset - 1).toNat : ℕ∞) := by + simpa [add_comm] using hroom + have hle1 : (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) ≤ + intervalCard E ((H : Key) : WithTop Key) := by + rw [← hsmall]; exact hcard + have h3 : ((2 : ℕ) : ℕ∞) + (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) ≤ + ((r.end_.offset - 1).toNat : ℕ∞) := + le_trans (add_le_add_right hle1 2) hroom' + have h4 : (((r.end_.offset - 1).toNat + 1 : ℕ) : ℕ∞) ≤ + ((r.end_.offset - 1).toNat : ℕ∞) := by + rw [← ENat.natCast_add] at h3 + have h : 2 + ((r.end_.offset - 1).toNat - 1) = + (r.end_.offset - 1).toNat + 1 := by omega + rwa [h] at h3 + have : (r.end_.offset - 1).toNat + 1 ≤ (r.end_.offset - 1).toNat := + ENat.natCast_le_natCast.mp h4 + omega + · have hKleH : K ≤ ((H : Key) : WithTop Key) := le_of_not_gt hHK + have hsub : List.Sublist (between db E K) (between db E ((H : Key) : WithTop Key)) := by + unfold between + apply filter_sublist_filter_of_imp + intro k hk + simp only [decide_eq_true_eq] at hk ⊢ + exact ⟨hk.1, lt_of_lt_of_le hk.2 hKleH⟩ + have hcardNat : (between db E K).length ≤ + (between db E ((H : Key) : WithTop Key)).length := hsub.length_le + have hcard2 : ((between db E K).length : ℕ∞) ≤ + intervalCard E ((H : Key) : WithTop Key) := + le_trans (by exact_mod_cast hcardNat) + (between_length_le_intervalCard (db := db) hEH) + have hcard : ((r.end_.offset - 1).toNat : ℕ∞) ≤ + intervalCard E ((H : Key) : WithTop Key) := by + rw [← he'] + exact hcard2 + exact not_room_of_length_ge hcard hroom + +/-- Compatibility depends on the read only through its complete keys and its limit. -/ +theorem compatible_congr_completeKeys {db : DB} {r r' : RangeRead} {res : Result} + (hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r') + (hlim : r.limit = r'.limit) : + RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := by + unfold RangeRead.Compatible RangeRead.withinLimit + rw [hck, hlim] + +/-- **Replacing the end key by the sentinel in the small-gap crossed top cell preserves +compatibility.** The gap between `end_.key` and the reported last is too small to house +the hidden keys a genuine end resolution would need, so every compatible database +resolves the end selector to `⊤`. -/ +theorem compatible_iff_topEnd_of_small_gap {db : DB} {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hcross : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) + (hsmall : intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞) < ((r.end_.offset - 1).toNat : ℕ∞)) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with end_ := { key := (⊤ : WithTop Key), offset := 1 } } res := by + let r' : RangeRead := { r with end_ := { key := (⊤ : WithTop Key), offset := 1 } } + have hrev' : r'.reverse = true := by simp [r', hrev] + have hlim' : r'.limit = r.limit := rfl + have hresEnd' : resolve db r'.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top (s := r'.end_) rfl le_rfl + constructor + · intro hcomp + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_small_gap hcomp hmore hrev hne hend hcross hsmall + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp [RangeRead.completeKeys, RangeRead.inRange, hrev, hre, hresEnd', r'] + exact (compatible_congr_completeKeys hck hlim').mp hcomp + · intro hcomp' + set L : Key := res.keys.getLast hne with hLdef + set E : WithTop Key := r.end_.key with hE + set e : ℕ := (r.end_.offset - 1).toNat with he + have hEL : E ≤ ((L : Key) : WithTop Key) := le_of_lt (by simpa [E, L] using hcross) + have hbetw := res_keys_eq_reverse_between_resolve_end (db := db) (r := r') (res := res) + hcomp' hmore hrev' hne + have hbetw' : (between db ((L : Key) : WithTop Key) (⊤ : WithTop Key)).length = + res.keys.length := by + rw [← hLdef, hresEnd'] at hbetw + have h := congrArg List.length hbetw + rw [List.length_reverse] at h + exact h.symm + have htop_len : (between db E (⊤ : WithTop Key)).length = + (between db E ((L : Key) : WithTop Key)).length + res.keys.length := + between_top_length_eq_add hEL hbetw' + have hcap : (((between db E ((L : Key) : WithTop Key)).length : ℕ) : ℕ∞) ≤ + intervalCard E ((L : Key) : WithTop Key) := + between_length_le_intervalCard (db := db) hEL + have hsmall' : intervalCard E ((L : Key) : WithTop Key) + + (res.keys.length : ℕ∞) < (e : ℕ∞) := by + simpa [E, L, e] using hsmall + have hlt : (((between db E ((L : Key) : WithTop Key)).length + res.keys.length : ℕ) : ℕ∞) < + (e : ℕ∞) := by + rw [ENat.natCast_add] + exact lt_of_le_of_lt (add_le_add hcap le_rfl) hsmall' + have hltnat : (between db E ((L : Key) : WithTop Key)).length + res.keys.length < e := + ENat.natCast_lt_natCast.mp hlt + have htop : (between db r.end_.key (⊤ : WithTop Key)).length < + (r.end_.offset - 1).toNat := by + rw [show r.end_.key = E from hE.symm, htop_len] + simpa [he] using hltnat + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_between_top_lt hend htop + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp [RangeRead.completeKeys, RangeRead.inRange, hrev, hre, hresEnd', r'] + exact (compatible_congr_completeKeys hck hlim').mpr hcomp' + +/-- **Replacing the end key by the sentinel in the top branch preserves compatibility.** +On the top branch (`#reported ≥ end_.key < end_.offset - 1`) with the reported last at or +below `end_.key`, every compatible database resolves the end to `⊤`, so moving the end key +to `⊤` leaves the complete keys unchanged. Conversely, a database compatible with the +sentinel-end read stores exactly the reported keys at or above `end_.key`, so the end +selector must already resolve to the sentinel. -/ +theorem compatible_iff_topEnd_of_residual_top {db : DB} {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hLE : ((res.keys.getLast hne : Key) : WithTop Key) ≤ r.end_.key) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) : + RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with end_ := { key := (⊤ : WithTop Key), offset := 1 } } res := by + let r' : RangeRead := { r with end_ := { key := (⊤ : WithTop Key), offset := 1 } } + have hrev' : r'.reverse = true := by simp [r', hrev] + have hlim' : r'.limit = r.limit := rfl + have hresEnd' : resolve db r'.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top (db := db) (s := r'.end_) rfl (by simp [r']) + constructor + · intro hcomp + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_count_lt hcomp hmore hrev hne hend hLE hlt + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp [RangeRead.completeKeys, RangeRead.inRange, hrev, hrev', hre, hresEnd', r'] + exact (compatible_congr_completeKeys hck hlim').mp hcomp + · intro hcomp' + by_cases hretop : resolve db r.end_ = (⊤ : WithTop Key) + · have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp [RangeRead.completeKeys, RangeRead.inRange, hrev, hrev', hretop, hresEnd', r'] + exact (compatible_congr_completeKeys hck hlim').mpr hcomp' + · exfalso + set L : Key := res.keys.getLast hne with hL + have hLE' : ((L : Key) : WithTop Key) ≤ r.end_.key := hLE + have hbetween' : between db ((L : Key) : WithTop Key) (⊤ : WithTop Key) = + res.keys.reverse := by + have h := res_keys_eq_reverse_between_resolve_end (db := db) (r := r') (res := res) + hcomp' hmore hrev' hne + rw [hresEnd'] at h + rw [h, List.reverse_reverse] + have htoplen : (between db r.end_.key (⊤ : WithTop Key)).length = + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + have hfil := between_filter_ge_eq (db := db) (L := ((L : Key) : WithTop Key)) + (E := r.end_.key) (B := (⊤ : WithTop Key)) hLE' + rw [← hfil, hbetween', List.filter_reverse, List.length_reverse, + List.countP_eq_length_filter] + have hlen_e := length_between_end_resolve_eq_of_ne_top (db := db) (r := r) hend hretop + have hmono : (between db r.end_.key (resolve db r.end_)).length ≤ + (between db r.end_.key (⊤ : WithTop Key)).length := by + unfold between + have hsub : List.Sublist + (db.keys.filter (fun k : Key => + decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_))) + (db.keys.filter (fun k : Key => + decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (⊤ : WithTop Key)))) := by + apply filter_sublist_filter_of_imp + intro k hk + exact decide_eq_true ⟨(of_decide_eq_true hk).1, WithTop.coe_lt_top k⟩ + exact hsub.length_le + omega + +/-- **Soundness transfer from the offset-one reduction.** If the residual closed form is +sound for the read whose begin is keyed at the reported last with offset one, then it is +sound for the original read. -/ +theorem sound_of_sound_reduce {r : RangeRead} {res : Result} {L : Key} + (hmore : res.more = true) (hrev : r.reverse = true) (hbeg : r.begin.offset < 0) + (hne : res.keys ≠ []) (hL : L = res.keys.getLast hne) : + Sound { r with begin := ⟨(L : WithTop Key), 1⟩ } res + (closedFormNegCrossTruncRevResidual r res) → + Sound r res (closedFormNegCrossTruncRevResidual r res) := by + intro hS db1 db2 hagree + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hcr_beg : (closedFormNegCrossTruncRevResidual r res).begin ≤ ((L : Key) : WithTop Key) := by + have := closedFormNegCrossTruncRevResidual_begin_le_last (r := r) (res := res) (L := L) + (by rw [hL, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl) + exact this + have hcr_end : r.begin.key ≤ (closedFormNegCrossTruncRevResidual r res).end_ := + closedFormNegCrossTruncRevResidual_end_ge_begin hmore hne + have step : ∀ da db_, AgreeOn (closedFormNegCrossTruncRevResidual r res) da db_ → + RangeRead.Compatible da r res → RangeRead.Compatible db_ r res := by + intro da db_ hag hcomp + have hcomp' : RangeRead.Compatible da + ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead) res := + compatible_reduce_of_compatible hrev hmore hne hL hcomp + have hcomp2' : RangeRead.Compatible db_ + ({ r with begin := ⟨(L : WithTop Key), 1⟩ } : RangeRead) res := + (hS da db_ hag).mp hcomp' + have hLdb2 : L ∈ db_.keys := RangeRead.compatible_keys_subset hcomp2' hLmem + have hrb2 : resolve db_ r.begin ≤ ((L : Key) : WithTop Key) := by + by_cases hle : r.begin.key ≤ ((L : Key) : WithTop Key) + · exact le_trans (resolve_le_key_of_offset_nonpos (db := db_) (s := r.begin) (by omega)) hle + · have hcross : ((L : Key) : WithTop Key) < r.begin.key := lt_of_not_ge hle + have hbound : ((between da ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + simpa [← hL] using between_add_offset_le_one (db := da) hcomp hmore hrev hne + (by rw [← hL]; exact hcross) + have hagree' : AgreeOn ⟨((L : Key) : WithTop Key), + (closedFormNegCrossTruncRevResidual r res).end_⟩ da db_ := + agreeOn_of_subrange + (subrange_of_le (cr' := ⟨((L : Key) : WithTop Key), + (closedFormNegCrossTruncRevResidual r res).end_⟩) + (cr := closedFormNegCrossTruncRevResidual r res) + hcr_beg (le_refl (closedFormNegCrossTruncRevResidual r res).end_)) hag + have hbetween_eq : between da ((L : Key) : WithTop Key) r.begin.key = + between db_ ((L : Key) : WithTop Key) r.begin.key := + between_eq_of_agree (db := da) (db' := db_) hagree' (le_of_lt hcross) hcr_end + have hbound2 : ((between db_ ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [← hbetween_eq]; exact hbound + exact resolve_begin_le_of_between_bound hLdb2 hcross hbound2 + exact compatible_of_compatible_reduce hrev hmore hne hL hrb2 hcomp2' + constructor + · intro h1; exact step db1 db2 hagree h1 + · intro h2; exact step db2 db1 hagree.symm h2 + +/-- **The residual is cross-free at the begin selector when `begin.key ≤ last`.** In +that case the residual hypotheses are exactly the cross-free nonpositive reversed +truncated cell, already closed by the offset-one reduction. The only genuinely crossed +part of the residual is therefore `last < begin.key`. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residual_begin_le + {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevResidual r res) + (hBLE : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : + Minimal r res (closedFormNonposRevTrunc r res) := by + obtain ⟨hmore, hrev, hbeg, hne, _, _⟩ := h + refine minimal_closedForm_of_nonpos_revTrunc hread ⟨hmore, hrev, by omega, ?_⟩ + unfold leastReported + rw [if_pos hrev] + exact hBLE + +/-! ### A bound on the reported keys below the begin selector -/ + +/-- **The reported keys below the begin selector fit in the stored gap below it.** In a +compatible truncated reversed result, the reported keys strictly below `begin.key` are the +part of `[last, begin.key)` consisting of reported keys; every such key is stored, and the +stored keys in `[last, begin.key)` number at least them. -/ +theorem reportedBelowBegin_le_between {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (_hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) : + reportedBelowBegin r res ≤ + (between db ((res.keys.getLast hne : Key) : WithTop Key) r.begin.key).length := by + set L : Key := res.keys.getLast hne with hLdef + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLdef] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + set p : Key → Bool := fun k => decide (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < r.begin.key) with hp + have h1 : reportedBelowBegin r res = (res.keys.filter p).length := by + unfold reportedBelowBegin + rw [List.countP_eq_length_filter] + apply congrArg List.length + apply List.filter_congr + intro k hk + have hLk : L ≤ k := hLle k hk + by_cases hlt : ((k : Key) : WithTop Key) < r.begin.key + · simp [hp, hlt, hLk] + · simp [hp, hlt] + rw [h1] + have h2 : (res.keys.filter p).length ≤ ((RangeRead.completeKeys db r).filter p).length := by + apply List.Sublist.length_le + exact (RangeRead.compatible_keys_prefix hcomp).sublist.filter p + have h3 : ((RangeRead.completeKeys db r).filter p).length = + ((RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).filter p).length := by + exact ((RangeRead.completeKeys_perm_inRange db r).filter p).length_eq + have h4 : ((RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).filter p).length ≤ + (db.keys.filter p).length := by + unfold RangeRead.inRange + rw [List.filter_comm] + exact List.filter_sublist.length_le + have h5 : (db.keys.filter p).length = + (between db ((L : Key) : WithTop Key) r.begin.key).length := rfl + omega + +/-! ### The top subcell -/ + +/-- The top subcell of the residual: the reported last is at or below `end_.key` and the +top-branch count inequality `#reported ≥ end_.key < end_.offset - 1` holds. The minimal +range is `[last reported, ⊤)`. -/ +abbrev negCrossTruncRevResidualTopCrossed (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ≤ r.end_.key ∧ + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat + +/-- The closed form of the top crossed residual subcell: `[last reported, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncRevResidualTopCrossed (_r : RangeRead) + (res : Result) : ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The top crossed residual closed form is minimal.** In this subcell every +compatible database resolves the end selector to the sentinel, so compatibility is +unchanged by replacing the end key with `⊤`; minimality transfers from the sentinel-end +cell. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residual_top_crossed + {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevResidualTopCrossed r res) : + Minimal r res (closedFormNegCrossTruncRevResidualTopCrossed r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hend, hLE, hlt⟩ := h + set L : Key := res.keys.getLast hne with hLdef + have hlastD : res.keys.getLastD ([] : Key) = L := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] + exact hLdef.symm + have hLE' : ((L : Key) : WithTop Key) ≤ r.end_.key := by + rw [hlastD] at hLE + exact hLE + let r' : RangeRead := { r with end_ := { key := (⊤ : WithTop Key), offset := 1 } } + have hrev' : r'.reverse = true := by simp [r', hrev] + have heq : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := by + intro db + exact compatible_iff_topEnd_of_residual_top (db := db) hmore hrev hne hend hLE' hlt + have hread' : Readable r' res := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (heq db).mp hcomp⟩ + have hmin'_raw := minimal_topOfLast_of_rev_negTrunc_top_nocross (r := r') hread' hmore hrev' + hne hbeg (by simp [r']) (by simp [r']) + have hmin' : Minimal r' res (topOfLast L) := by + rw [hLdef]; exact hmin'_raw + have hmin : Minimal r res (topOfLast L) := + (minimal_congr_of_compatible_iff heq).mpr hmin' + unfold closedFormNegCrossTruncRevResidualTopCrossed + rw [hlastD] + exact hmin + +/-! ### The small-gap crossed top subcell -/ + +/-- The small-gap crossed top subcell of the residual: the end key lies strictly below +the reported last, and the gap `[end_.key, last)` together with the reported keys holds +fewer than `end_.offset - 1` keys. Then no compatible database can resolve the end to a +genuine key, and the minimal range is `[last, ⊤)`. -/ +abbrev negCrossTruncRevResidualTopCrossedSmallGap (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + r.end_.key < ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + intervalCard r.end_.key ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + + (res.keys.length : ℕ∞) < ((r.end_.offset - 1).toNat : ℕ∞) + +/-- The closed form of the small-gap crossed top subcell: `[last reported, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncRevResidualTopCrossedSmallGap (_r : RangeRead) + (res : Result) : ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The small-gap crossed top subcell closed form is minimal.** Every compatible +database resolves the end to `⊤`, so compatibility is unchanged by replacing the end key +with `⊤`; minimality transfers from the sentinel-end cell. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residual_top_crossed_smallGap + {r : RangeRead} {res : Result} + (hread : Readable r res) + (h : negCrossTruncRevResidualTopCrossedSmallGap r res) : + Minimal r res (closedFormNegCrossTruncRevResidualTopCrossedSmallGap r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hend, hcrossD, hsmallD⟩ := h + set L : Key := res.keys.getLast hne with hLdef + have hlastD : res.keys.getLastD ([] : Key) = L := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] + exact hLdef.symm + have hcross : r.end_.key < ((L : Key) : WithTop Key) := by + rw [hlastD] at hcrossD + exact hcrossD + have hsmall : intervalCard r.end_.key ((L : Key) : WithTop Key) + + (res.keys.length : ℕ∞) < ((r.end_.offset - 1).toNat : ℕ∞) := by + rw [hlastD] at hsmallD + exact hsmallD + let r' : RangeRead := { r with end_ := { key := (⊤ : WithTop Key), offset := 1 } } + have hrev' : r'.reverse = true := by simp [r', hrev] + have heq : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := by + intro db + exact compatible_iff_topEnd_of_small_gap (db := db) hmore hrev hne hend hcross hsmall + have hread' : Readable r' res := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (heq db).mp hcomp⟩ + have hmin'_raw := minimal_topOfLast_of_rev_negTrunc_top_nocross (r := r') hread' hmore hrev' + hne hbeg (by simp [r']) (by simp [r']) + have hmin' : Minimal r' res (topOfLast L) := by + rw [hLdef]; exact hmin'_raw + have hmin : Minimal r res (topOfLast L) := + (minimal_congr_of_compatible_iff heq).mpr hmin' + unfold closedFormNegCrossTruncRevResidualTopCrossedSmallGap + rw [hlastD] + exact hmin + +/-! ### The room subcell -/ + +/-- The room subcell of the residual: some reported key lies at or above `end_.key`, and +the gap `[end_.key, reported head)` has room for at most `end_.offset - 3` keys. The +minimal range is `[last reported, ⊤)`. Unlike the top crossed subcell, this can hold even +when the reported last is at or above `end_.key`: the head-based room bound then forces +the end resolution to the sentinel. -/ +abbrev negCrossTruncRevResidualRoom (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + (∃ k ∈ res.keys, r.end_.key ≤ ((k : Key) : WithTop Key)) ∧ + intervalCard r.end_.key ((res.keys.headD ([] : Key) : Key) : WithTop Key) + 2 ≤ + (r.end_.offset - 1).toNat + +/-- The closed form of the room subcell: `[last reported, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncRevResidualRoom (_r : RangeRead) (res : Result) : + ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The room subcell closed form is minimal.** Every compatible database resolves the +end selector to the sentinel by the room criterion, so compatibility is unchanged by +replacing the end key with `⊤`; minimality transfers from the sentinel-end cell. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residual_room + {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevResidualRoom r res) : + Minimal r res (closedFormNegCrossTruncRevResidualRoom r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hend, hres, hroomD⟩ := h + set L : Key := res.keys.getLast hne with hLdef + have hlastD : res.keys.getLastD ([] : Key) = L := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] + exact hLdef.symm + have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?_getD, List.head?_eq_some_head hne] + rfl + have hroom : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 2 ≤ + (r.end_.offset - 1).toNat := by + rw [hheadD] at hroomD + exact hroomD + let r' : RangeRead := { r with end_ := { key := (⊤ : WithTop Key), offset := 1 } } + have hrev' : r'.reverse = true := by simp [r', hrev] + have hresEnd' : ∀ db, resolve db r'.end_ = (⊤ : WithTop Key) := fun db => + resolve_eq_top_of_key_eq_top (s := r'.end_) rfl le_rfl + have heq : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := by + intro db + constructor + · intro hcomp + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_room hcomp hmore hrev hne hend hres hroom + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp [RangeRead.completeKeys, RangeRead.inRange, hrev, hre, hresEnd' db, r'] + exact (compatible_congr_completeKeys hck rfl).mp hcomp + · intro hcomp' + have hhall : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) ≤ + ((res.keys.head hne : Key) : WithTop Key) := + db_keys_le_head_of_compat_sentinel hcomp' hmore hrev' hne (by simp [r']) (by simp [r']) + have hresdb : ∀ k ∈ res.keys, k ∈ db.keys := fun k hk => + RangeRead.compatible_keys_subset hcomp' hk + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_room_of_all_le_head hne hend hres hresdb hhall hroom + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + simp [RangeRead.completeKeys, RangeRead.inRange, hrev, hre, hresEnd' db, r'] + exact (compatible_congr_completeKeys hck rfl).mpr hcomp' + have hread' : Readable r' res := by + obtain ⟨db, hcomp⟩ := hread + exact ⟨db, (heq db).mp hcomp⟩ + have hmin'_raw := minimal_topOfLast_of_rev_negTrunc_top_nocross (r := r') hread' hmore hrev' + hne hbeg (by simp [r']) (by simp [r']) + have hmin' : Minimal r' res (topOfLast L) := by + rw [hLdef]; exact hmin'_raw + have hmin : Minimal r res (topOfLast L) := + (minimal_congr_of_compatible_iff heq).mpr hmin' + unfold closedFormNegCrossTruncRevResidualRoom + rw [hlastD] + exact hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevResidualFinite.lean b/FormalConflictRange/NegCrossTruncRevResidualFinite.lean new file mode 100644 index 0000000..27c4e50 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevResidualFinite.lean @@ -0,0 +1,671 @@ +/- +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.NegCrossTruncRevResidualLargeSentinel +import FormalConflictRange.NegCrossTruncRevFinite + +/-! +# The finite branch of the reversed crossed negative-begin truncated residual + +This module closes the *under* part of the finite branch of the last reversed crossed +negative-begin truncated residual. The setting is + +* `res.more = true`, `r.reverse = true`, `r.begin.offset < 0`, `res.keys ≠ []`; +* `2 ≤ r.end_.offset`; +* the reported last `L` lies at or below `end_.key` (`last ≤ end_.key`); +* the finite count `#reported ≥ end_.key = end_.offset - 1`; +* the crossed selectors `L < begin.key`. + +Writing `H` for the reported head, `L` for the reported last and `cond` for the +capacity condition +`(reportedBelowBegin - 1) + intervalCard (keyAfter H) begin.key ≤ beginSlack`, the +minimal range is `[L, if cond then keyAfter H else begin.key)`. + +The finite branch makes the end resolution the least stored key strictly above the +reported head. Hence compatibility with `r` is the same as compatibility with the read +whose end selector is `(keyAfter H, 1)`, and minimality transfers from the offset-one +crossed cells. + +The complementary *over* part (`end_.key < last`) is not covered here. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1600000 + +/-- The finite under cell of the reversed crossed negative-begin truncated residual: +the finite branch on the region `last ≤ end_.key < begin.key`. -/ +abbrev negCrossTruncRevResidualFiniteUnder (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ≤ r.end_.key ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat + +/-- The capacity condition of the finite under cell: after accounting for the reported +keys below the begin selector, the gap `[keyAfter head, begin.key)` still fits in the +begin slack. -/ +noncomputable def finiteUnderCond (r : RangeRead) (res : Result) : Prop := + ((reportedBelowBegin r res - 1 : ℕ) : ℕ∞) + + intervalCard (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + r.begin.key ≤ + (beginSlack r : ℕ∞) + +/-- The closed form of the finite under cell: +`[last reported, if cond then keyAfter head else begin.key)`. -/ +noncomputable def closedFormNegCrossTruncRevResidualFiniteUnder + (r : RangeRead) (res : Result) : ConflictRange := by + classical + let L : WithTop Key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + let U : WithTop Key := + if finiteUnderCond r res then + (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + else r.begin.key + exact ⟨L, U⟩ + +/-! ### The end resolution is the least stored key above the reported head -/ + +/-- **Under compatibility the end resolution is the least stored key strictly above the +reported head.** The reported keys are the stored keys in `[last, resolve end_)` read +backwards, so the head is stored, the resolution lies strictly above it, and no stored +key lies between them. -/ +theorem resolve_end_eq_resolve_keyAfter_head {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) : + resolve db r.end_ = + resolve db (selAt (((keyAfter (res.keys.head hne)) : Key) : WithTop Key)) := by + set H : Key := res.keys.head hne with hHdef + set K : Key := keyAfter H with hKdef + set L : Key := res.keys.getLast hne with hLdef + set R : WithTop Key := resolve db r.end_ with hRdef + have hHK : H < K := by rw [hKdef]; exact keyAfter_self_lt H + have hHKcoe : ((H : Key) : WithTop Key) < ((K : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr hHK + have hbetween : between db ((L : Key) : WithTop Key) R = res.keys.reverse := by + have h := res_keys_eq_reverse_between_resolve_end hcomp hmore hrev hne + have h' : between db ((res.keys.getLast hne : Key) : WithTop Key) (resolve db r.end_) = + res.keys.reverse := by + have h2 := congrArg List.reverse h + rw [List.reverse_reverse] at h2 + exact h2.symm + simpa [hLdef, hRdef] using h' + have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne + have hHbetween : H ∈ between db ((L : Key) : WithTop Key) R := by + rw [hbetween]; exact List.mem_reverse.mpr hHmem + have hLH : ((L : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := + (of_decide_eq_true (List.mem_filter.mp hHbetween).2).1 + have hHR : ((H : Key) : WithTop Key) < R := + (of_decide_eq_true (List.mem_filter.mp hHbetween).2).2 + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hhead_le : ∀ x ∈ res.keys, x ≤ H := by + intro x hx + rw [hHdef] + exact (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hx + -- Every stored key at or above `keyAfter H` is at or above the resolution. + have hclaim : ∀ k ∈ db.keys, ((K : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) → + R ≤ ((k : Key) : WithTop Key) := by + intro k hk hKk + by_contra hkR + push Not at hkR + have hLk : ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + le_trans hLH (le_trans (le_of_lt hHKcoe) hKk) + have hkbetween : k ∈ between db ((L : Key) : WithTop Key) R := by + unfold between + exact List.mem_filter.mpr ⟨hk, by + rw [decide_eq_true_eq] + exact ⟨hLk, hkR⟩⟩ + have hkres : k ∈ res.keys := by + rw [hbetween] at hkbetween + exact List.mem_reverse.mp hkbetween + have hkH : k ≤ H := hhead_le k hkres + exact absurd (le_trans hKk (WithTop.coe_le_coe.mpr hkH)) (not_le.mpr hHKcoe) + refine le_antisymm ?_ ?_ + · -- `R ≤ resolve (selAt K)` + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) + (s := selAt (((K : Key) : WithTop Key))) with hnil | htop | ⟨x, hxmem, hxeq⟩ + · -- `resolve (selAt K) = ↑nil`, impossible since `↑K ≤ resolve` + have hKle : ((K : Key) : WithTop Key) ≤ (([] : Key) : WithTop Key) := by + rw [← hnil]; exact coe_le_resolve_selAt db K + have hKnil : K = [] := by + rw [WithTop.coe_le_coe] at hKle + exact le_antisymm hKle (nil_le_key K) + rw [hKnil] at hHK + exact absurd hHK (not_lt.mpr (nil_le_key H)) + · rw [htop]; exact le_top + · have hKx : ((K : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by + rw [← hxeq]; exact coe_le_resolve_selAt db K + rw [hxeq] + exact hclaim x hxmem hKx + · -- `resolve (selAt K) ≤ R` + rcases eq_or_ne R (⊤ : WithTop Key) with hRtop | hRne + · rw [hRtop]; exact le_top + · rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with + hnil | htop | ⟨r, hrmem, hRr⟩ + · rw [← hRdef] at hnil + rw [hnil] at hHR + exact absurd hHR (by simp) + · rw [← hRdef] at htop + exact absurd htop hRne + · rw [← hRdef] at hRr + have hKr : ((K : Key) : WithTop Key) ≤ ((r : Key) : WithTop Key) := by + rw [hRr] at hHR + exact keyAfter_le_of_lt hHR + rw [hRr] + exact resolve_selAt_le_of_mem hrmem (WithTop.coe_le_coe.mp hKr) + +/-- **A finite resolution of an offset-one selector keyed at a genuine key is a stored +key.** -/ +theorem mem_keys_of_resolve_selAt_eq_coe {db : DB} {b v : Key} + (h : resolve db (selAt (↑b : WithTop Key)) = (↑v : WithTop Key)) : + v ∈ db.keys := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := selAt (↑b : WithTop Key)) with + hnil | htop | ⟨x, hxmem, hxeq⟩ + · have hv : v = [] := WithTop.coe_eq_coe.mp (h.symm.trans hnil) + have hnonneg := anchor_selAt_coe_nonneg db b + have hempty : ([] : Key) ∈ db.keys := by + rw [resolve_eq_ite] at hnil + rw [ite_eq_right (not_lt.mpr hnonneg)] at hnil + cases hk : db.keys[(selAt (↑b : WithTop Key)).anchor db |>.toNat]? with + | none => rw [hk] at hnil; simp at hnil + | some k => + rw [hk] at hnil + have hknil : k = [] := WithTop.coe_eq_coe.mp hnil + rw [← hknil] + exact List.mem_of_getElem? hk + rw [hv] + exact hempty + · rw [htop] at h + exact absurd h (by simp) + · have hvx : v = x := WithTop.coe_eq_coe.mp (h.symm.trans hxeq) + rw [hvx] + exact hxmem + +/-- **The finite under cell reduces to the read with end selector `(keyAfter head, 1)`.** +Compatibility is unchanged by replacing the end selector with the immediate successor of +the reported head at offset one. -/ +theorem compatible_iff_finiteUnder_reduce {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hLE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ≤ r.end_.key) + (hcount : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db + { r with end_ := selAt (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) } + res := by + intro db + have hH : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + have hL : res.keys.getLastD ([] : Key) = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne + set r' : RangeRead := + { r with end_ := selAt (((keyAfter (res.keys.head hne) : Key)) : WithTop Key) } + with hr'def + have hr'end : r'.end_ = selAt (((keyAfter (res.keys.head hne) : Key)) : WithTop Key) := rfl + have hr'lim : r'.limit = r.limit := rfl + have hr'rev : r'.reverse = r.reverse := rfl + have hr'target : r' = + { r with end_ := selAt (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) } := by + rw [hr'def, hH] + -- forward direction + have hfwd : RangeRead.Compatible db r res → RangeRead.Compatible db r' res := by + intro hcomp + have hres := resolve_end_eq_resolve_keyAfter_head (db := db) hcomp hmore hrev hne + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + unfold RangeRead.completeKeys + rw [hr'rev, hrev] + simp only + rw [hres, hr'end] + exact (compatible_congr_completeKeys hck hr'lim).mp hcomp + -- backward direction + have hbwd : RangeRead.Compatible db r' res → RangeRead.Compatible db r res := by + intro hcomp' + have hres' := resolve_end_eq_resolve_keyAfter_head (db := db) (r := r') hcomp' hmore + (by rw [hr'rev, hrev]) hne + -- `resolve (r'.end_) = least stored key above H` + have hR' : resolve db r'.end_ = + resolve db (selAt (((keyAfter (res.keys.head hne)) : Key) : WithTop Key)) := hres' + -- the reported keys are the stored keys in `[L, resolve r'.end_)` read backwards + have hbetween' : between db ((res.keys.getLast hne : Key) : WithTop Key) + (resolve db r'.end_) = res.keys.reverse := by + have h := res_keys_eq_reverse_between_resolve_end hcomp' hmore + (by rw [hr'rev, hrev]) hne + have h2 := congrArg List.reverse h + rw [List.reverse_reverse] at h2 + exact h2.symm + -- We show that `resolve db r.end_` also lands on the least stored key above `H`. + -- Write `R' := resolve db r'.end_`. + set R' : WithTop Key := resolve db r'.end_ with hR'def + set H : Key := res.keys.head hne with hHdef + set L : Key := res.keys.getLast hne with hLdef + have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne + have hHbetween : H ∈ between db ((L : Key) : WithTop Key) R' := by + rw [hbetween'] + exact List.mem_reverse.mpr hHmem + have hLH : ((L : Key) : WithTop Key) ≤ ((H : Key) : WithTop Key) := + (of_decide_eq_true (List.mem_filter.mp hHbetween).2).1 + have hHR' : ((H : Key) : WithTop Key) < R' := + (of_decide_eq_true (List.mem_filter.mp hHbetween).2).2 + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp' (by rw [hr'rev, hrev]) + have hhead_le : ∀ x ∈ res.keys, x ≤ H := by + intro x hx + rw [hHdef] + exact (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hx + -- stored keys in `[L, R')` are exactly the reported keys + have hstoredLe : ∀ k ∈ db.keys, ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) → + ((k : Key) : WithTop Key) < R' → k ∈ res.keys := by + intro k hk hLk hkR + have hkb : k ∈ between db ((L : Key) : WithTop Key) R' := by + unfold between + exact List.mem_filter.mpr ⟨hk, by rw [decide_eq_true_eq]; exact ⟨hLk, hkR⟩⟩ + rw [hbetween'] at hkb + exact List.mem_reverse.mp hkb + -- `end_.key ≤ head`: if `head < end_.key`, no reported key is at or above it + have hEH : r.end_.key ≤ ((H : Key) : WithTop Key) := by + by_contra hcon + push Not at hcon + have hzero : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = 0 := by + apply List.countP_eq_zero.mpr + intro k hk + simpa using decide_eq_false + (not_le.mpr (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hhead_le k hk)) hcon)) + rw [hcount] at hzero + have : 1 ≤ (r.end_.offset - 1).toNat := by omega + omega + -- the first `e` stored keys at or above `end_.key` are the reported ones + have hbetweenE : between db r.end_.key R' = res.keys.reverse.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + have hLE' : ((L : Key) : WithTop Key) ≤ r.end_.key := by + rw [← hL] + exact hLE + have hfil := between_filter_ge_eq (db := db) (L := ((L : Key) : WithTop Key)) + (E := r.end_.key) (B := R') hLE' + rw [← hfil, hbetween'] + have hlenE : (between db r.end_.key R').length = + (r.end_.offset - 1).toNat := by + rw [hbetweenE, ← List.countP_eq_length_filter, List.countP_reverse, hcount] + -- the stored keys at or above `end_.key` split as the reported ones plus those + -- at or above `R'` + have hsplit : db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + between db r.end_.key R' ++ + db.keys.filter (fun k : Key => decide (R' ≤ (k : WithTop Key))) := by + have hdecomp : db.keys = + db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < R')) ++ + db.keys.filter (fun k : Key => + decide (¬ ((k : Key) : WithTop Key) < R')) := by + have h1 := filter_lt_eq_take_countP db.sorted R' + have h2 := filter_not_lt_eq_drop_countP db.sorted R' + rw [h1, h2] + exact (List.take_append_drop _ _).symm + have hEB : r.end_.key ≤ R' := le_trans hEH (le_of_lt hHR') + rw [hdecomp, List.filter_append, List.filter_append] + have hK1nil : (db.keys.filter + (fun k : Key => decide (((k : Key) : WithTop Key) < R'))).filter + (fun k : Key => decide (R' ≤ ((k : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk + have hklt : ((k : Key) : WithTop Key) < R' := + of_decide_eq_true (List.mem_filter.mp hk).2 + simpa using decide_eq_false (not_le.mpr hklt) + rw [hK1nil, List.nil_append] + congr 1 + · rw [List.filter_filter] + unfold between + apply List.filter_congr + intro k _ + by_cases hE : r.end_.key ≤ ((k : Key) : WithTop Key) <;> + by_cases hB : ((k : Key) : WithTop Key) < R' <;> simp [hE, hB] + · apply List.filter_congr + intro k hk + have hklt : ¬ ((k : Key) : WithTop Key) < R' := + of_decide_eq_true (List.mem_filter.mp hk).2 + have hkge : R' ≤ ((k : Key) : WithTop Key) := not_lt.mp hklt + have hE : r.end_.key ≤ ((k : Key) : WithTop Key) := le_trans hEB hkge + simp [hE, hkge] + have hresolve : resolve db r.end_ = R' := by + rw [resolve_eq_filter_getElem? (db := db) (s := r.end_) (by omega)] + rw [hsplit] + have hlen : (between db r.end_.key R').length = (r.end_.offset - 1).toNat := hlenE + rw [List.getElem?_append_right (le_of_eq hlen)] + rw [show (r.end_.offset - 1).toNat - (between db r.end_.key R').length = 0 by omega] + cases hR'eq : R' with + | top => + have hnil : db.keys.filter + (fun k : Key => decide ((⊤ : WithTop Key) ≤ ((k : Key) : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro k _ + simp + rw [hnil, List.getElem?_nil] + | coe r => + have hrmem : r ∈ db.keys := by + apply mem_keys_of_resolve_selAt_eq_coe + (b := keyAfter (res.keys.head hne)) + rw [← hr'end] + exact hR'def.symm.trans hR'eq + have hfilt : (db.keys.filter + (fun k : Key => decide ((↑r : WithTop Key) ≤ ((k : Key) : WithTop Key))))[0]? = + some r := by + have hsel : resolve db (selAt (↑r : WithTop Key)) = (↑r : WithTop Key) := + resolve_selAt_self hrmem + rw [resolve_eq_filter_getElem? (db := db) (s := selAt (↑r : WithTop Key)) + (by norm_num : (1 : Int) ≤ (selAt (↑r : WithTop Key)).offset)] at hsel + have hidx : ((selAt (↑r : WithTop Key)).offset - 1).toNat = 0 := by + simp [selAt] + rw [hidx, selAt_key] at hsel + cases hq : (db.keys.filter + (fun k : Key => decide ((↑r : WithTop Key) ≤ ((k : Key) : WithTop Key))))[0]? with + | none => rw [hq] at hsel; simp at hsel + | some k => rw [hq] at hsel; simpa using hsel + rw [hfilt] + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + unfold RangeRead.completeKeys + rw [hr'rev, hrev] + simp only + rw [hresolve, hr'end] + exact (compatible_congr_completeKeys hck hr'lim).mpr hcomp' + constructor + · intro h + rw [← hr'target] + exact hfwd h + · intro h + rw [← hr'target] at h + exact hbwd h + +/-- **Elementary `ℕ∞` arithmetic for the finite under cell.** For `t ≥ 1`, subtracting one +from `t` on the left of a sum costs exactly one unit of slack. -/ +theorem enat_natCast_sub_one_add_le_iff {I : ℕ∞} {q t : ℕ} (ht : 1 ≤ t) : + (((t - 1 : ℕ) : ℕ∞) + I ≤ (q : ℕ∞)) ↔ (I + (t : ℕ∞) ≤ (q : ℕ∞) + 1) := by + have hts : ((t : ℕ) : ℕ∞) = ((t - 1 : ℕ) : ℕ∞) + 1 := by + rw [← ENat.natCast_one, ← ENat.natCast_add] + congr 1 + omega + rw [hts] + rw [show I + (((t - 1 : ℕ) : ℕ∞) + 1) = (((t - 1 : ℕ) : ℕ∞) + I) + 1 by + rw [add_comm I]; ac_rfl] + set m : ℕ∞ := ((t - 1 : ℕ) : ℕ∞) + I + by_cases hm : m = ⊤ + · rw [hm]; simp + · rw [ENat.add_one_le_iff hm, ENat.lt_add_one_iff (ENat.natCast_ne_top q)] + +/-! ### A general bound on the reported keys below the begin selector + +At a negative begin offset the begin anchor sits either `beginSlack + 1` places below +`begin.key` (when it is nonnegative) or before the start of the database, so the stored +keys in `[resolve begin, begin.key)` occupy at most `beginSlack + 1` slots. The reported +keys below `begin.key` are a subcollection of those slots. -/ + +/-- At a negative begin offset the stored keys in `[resolve begin, begin.key)` fit in +`beginSlack + 1` slots. -/ +theorem between_resolve_begin_length_le_beginSlack_succ {db : DB} {r : RangeRead} + (hbegin : r.begin.offset < 0) : + (between db (resolve db r.begin) r.begin.key).length ≤ beginSlack r + 1 := by + set d : ℕ := (between db (resolve db r.begin) r.begin.key).length with hd + have hoff : r.begin.offset ≤ 0 := le_of_lt hbegin + have hEB : resolve db r.begin ≤ r.begin.key := resolve_le_key_of_offset_nonpos hoff + have hdec : cnt db r.begin.key = cnt db (resolve db r.begin) + d := + cnt_eq_add_between db hEB + have hcntEmin : cnt db (resolve db r.begin) = + min (r.begin.anchor db).toNat db.keys.length := by + rw [cnt, countP_lt_resolve] + have hcntB : (cnt db r.begin.key : ℤ) = r.begin.anchor db + (beginSlack r : ℤ) + 1 := by + have hanc := anchor_eq_count_sub_slack db r hoff + have hc : ((List.countP (fun k : Key => + decide (((k : Key) : WithTop Key) < r.begin.key)) db.keys : ℕ) : ℤ) = + (cnt db r.begin.key : ℤ) := rfl + rw [hc] at hanc + linarith + rcases lt_or_ge (r.begin.anchor db) 0 with hneg | hpos + · have hres : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hneg + rw [hres, cnt_nil, Nat.zero_add] at hdec + have hcntd : (cnt db r.begin.key : ℤ) = (d : ℤ) := by rw [hdec] + have hd_int : (d : ℤ) ≤ (beginSlack r : ℤ) := by linarith + have hd_nat : d ≤ beginSlack r := by exact_mod_cast hd_int + omega + · have hcast : ((r.begin.anchor db).toNat : ℤ) = r.begin.anchor db := + Int.toNat_of_nonneg hpos + have hcnat : cnt db r.begin.key = (r.begin.anchor db).toNat + beginSlack r + 1 := by + have h : (cnt db r.begin.key : ℤ) = + ((r.begin.anchor db).toNat : ℤ) + (beginSlack r : ℤ) + 1 := by + rw [hcast]; linarith + exact_mod_cast h + have hlt : (r.begin.anchor db).toNat < db.keys.length := by + have hle : cnt db r.begin.key ≤ db.keys.length := cnt_le_length db _ + omega + rw [hcntEmin, Nat.min_eq_left (le_of_lt hlt)] at hdec + omega + +/-- The reported keys below the begin selector are stored in `[resolve begin, begin.key)`. +-/ +theorem reportedBelowBegin_le_between_resolve_begin {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (_hbegin : r.begin.offset < 0) : + reportedBelowBegin r res ≤ (between db (resolve db r.begin) r.begin.key).length := by + have hstep1 := List.Sublist.countP_le + (p := fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) + (RangeRead.compatible_keys_prefix hcomp).sublist + have hstep2 : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := + (RangeRead.completeKeys_perm_inRange db r).countP_eq _ + have hstep3 : (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) ≤ + (between db (resolve db r.begin) r.begin.key).length := by + have hin : RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter (fun k : Key => + decide (resolve db r.begin ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < resolve db r.end_)) := rfl + rw [hin, List.countP_filter] + unfold between + rw [← List.countP_eq_length_filter] + apply countP_le_countP_of_imp + intro k _ hk + rw [Bool.and_eq_true] at hk + rw [decide_eq_true_eq] + exact ⟨(of_decide_eq_true hk.2).1, of_decide_eq_true hk.1⟩ + calc reportedBelowBegin r res + = res.keys.countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := rfl + _ ≤ (RangeRead.completeKeys db r).countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := hstep1 + _ = (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).countP + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := hstep2 + _ ≤ (between db (resolve db r.begin) r.begin.key).length := hstep3 + +/-- **The reported-below-begin bound.** At a negative begin offset, the reported keys +below `begin.key` number at most `beginSlack + 1`. -/ +theorem reportedBelowBegin_le_beginSlack_succ {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hbegin : r.begin.offset < 0) : + reportedBelowBegin r res ≤ beginSlack r + 1 := + le_trans (reportedBelowBegin_le_between_resolve_begin hcomp hbegin) + (between_resolve_begin_length_le_beginSlack_succ hbegin) + +/-- Under the crossed finite under cell every reported key lies strictly below +`begin.key`, so all of them are counted by `reportedBelowBegin`. -/ +theorem reportedBelowBegin_eq_length_of_crossed {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hcross : (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) < r.begin.key) : + reportedBelowBegin r res = res.keys.length := by + obtain ⟨db, hcomp⟩ := hread + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hH : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_of_ne hne + rw [hH] at hcross + have hHK : res.keys.head hne < keyAfter (res.keys.head hne) := keyAfter_self_lt _ + unfold reportedBelowBegin + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + have hkH : k ≤ res.keys.head hne := + (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hk + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkH) + (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hHK) (le_of_lt hcross)) + +/-- The finite under capacity condition is the small-gap condition of the reduced +offset-one read, on the crossed region. -/ +theorem finiteUnderCond_iff_crossedSmall {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hcross : (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) < r.begin.key) : + finiteUnderCond r res ↔ + intervalCard (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + r.begin.key + (res.keys.length : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1 := by + have hrep := reportedBelowBegin_eq_length_of_crossed hread hrev hne hcross + have ht : 1 ≤ res.keys.length := List.length_pos_iff.mpr hne + unfold finiteUnderCond + rw [hrep] + exact enat_natCast_sub_one_add_le_iff ht + +/-- When the finite under capacity condition fails on the crossed region, the large-gap +condition of the reduced offset-one read holds. -/ +theorem crossedLarge_of_not_finiteUnderCond {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hcross : (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) < r.begin.key) + (hnot : ¬ finiteUnderCond r res) : + (beginSlack r : ℕ∞) + 2 ≤ + intervalCard (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + r.begin.key + (res.keys.length : ℕ∞) := by + rw [finiteUnderCond_iff_crossedSmall hread hrev hne hcross] at hnot + have hgt : (beginSlack r : ℕ∞) + 1 < + intervalCard (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + r.begin.key + (res.keys.length : ℕ∞) := not_le.mp hnot + have hone : ((beginSlack r : ℕ∞) + 1) ≠ ⊤ := + WithTop.add_ne_top.mpr ⟨ENat.natCast_ne_top _, ENat.one_ne_top⟩ + have := (ENat.add_one_le_iff hone).mpr hgt + simpa [show ((beginSlack r : ℕ∞) + 1) + 1 = (beginSlack r : ℕ∞) + 2 by + rw [add_assoc]; norm_num] using this + +/-- The crossed finite under cell of the reversed crossed negative-begin truncated +residual: the finite under cell with the crossed selectors `keyAfter head < begin.key`. -/ +abbrev negCrossTruncRevResidualFiniteUnderCrossed (r : RangeRead) (res : Result) : Prop := + negCrossTruncRevResidualFiniteUnder r res ∧ + (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) < r.begin.key + +/-- **The crossed finite under cell of the residual is minimal.** The crossed case makes +the end resolution the least stored key above the reported head, so the cell has the +same compatible databases as the offset-one read with end selector +`(keyAfter head, 1)`, and minimality transfers from the small- or large-gap offset-one +cells. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_crossed + {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncRevResidualFiniteUnderCrossed r res) : + Minimal r res (closedFormNegCrossTruncRevResidualFiniteUnder r res) := by + obtain ⟨hbase, hcrossK⟩ := hcell + obtain ⟨hmore, hrev, hbeg, hne, hend, hLE, hcrossL, hcount⟩ := hbase + let r' : RangeRead := + { r with end_ := selAt (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) } + have hcompat : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := + fun db => compatible_iff_finiteUnder_reduce hmore hrev hne hend hLE hcount db + have hread' : Readable r' res := by + obtain ⟨db, hdb⟩ := hread + exact ⟨db, (hcompat db).mp hdb⟩ + have hr'end : r'.end_ = + selAt (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) := rfl + have hr'key : r'.end_.key = + (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) := rfl + have hr'off : r'.end_.offset = 1 := rfl + have hr'beg : r'.begin = r.begin := rfl + have hr'begoff : r'.begin.offset < 0 := hbeg + have hr'rev : r'.reverse = true := hrev + have hr'crossK : r'.end_.key < r'.begin.key := by + rw [hr'key, hr'beg]; exact hcrossK + have hr'crossL : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r'.begin.key := by + rw [hr'beg]; exact hcrossL + by_cases hcond : finiteUnderCond r res + · -- small-gap branch + have hcap : intervalCard r'.end_.key r'.begin.key + (res.keys.length : ℕ∞) ≤ + (beginSlack r : ℕ∞) + 1 := by + rw [hr'key, hr'beg] + exact (finiteUnderCond_iff_crossedSmall hread hrev hne hcrossK).mp hcond + have hcell' : negCrossTruncRevCrossedSmall r' res := by + refine ⟨hmore, hr'rev, hr'begoff, hne, hr'crossL, hr'crossK, hr'off, hcap⟩ + have hmin' : Minimal r' res (truncatedRangeRev r' (res.keys.getLastD ([] : Key))) := + minimal_closedForm_of_negCross_trunc_rev_crossedSmall hread' hcell' + have hcr : closedFormNegCrossTruncRevResidualFiniteUnder r res = + truncatedRangeRev r' (res.keys.getLastD ([] : Key)) := by + simp [closedFormNegCrossTruncRevResidualFiniteUnder, truncatedRangeRev, hcond, + hr'end] + rw [hcr] + exact (minimal_congr_of_compatible_iff hcompat).mpr hmin' + · -- large-gap branch + have hcap : (beginSlack r : ℕ∞) + 2 ≤ + intervalCard r'.end_.key r'.begin.key + (res.keys.length : ℕ∞) := by + rw [hr'key, hr'beg] + exact crossedLarge_of_not_finiteUnderCond hread hrev hne hcrossK hcond + have hcell' : negCrossTruncRevCrossedLarge r' res := by + refine ⟨hmore, hr'rev, hr'begoff, hne, hr'crossL, hr'crossK, hr'off, hcap⟩ + have hmin' : Minimal r' res (closedFormNegCrossTruncRevCrossedLarge r' res) := + minimal_closedForm_of_negCross_trunc_rev_crossedLarge hread' hcell' + have hcr : closedFormNegCrossTruncRevResidualFiniteUnder r res = + closedFormNegCrossTruncRevCrossedLarge r' res := by + simp [closedFormNegCrossTruncRevResidualFiniteUnder, + closedFormNegCrossTruncRevCrossedLarge, hcond, hr'beg] + rw [hcr] + exact (minimal_congr_of_compatible_iff hcompat).mpr hmin' + +/-- On the complementary, non-crossed part of the finite under cell, the capacity +condition holds automatically: the interval `[keyAfter head, begin.key)` is empty, and the +reported keys below `begin.key` fit in `beginSlack + 1` slots. -/ +theorem finiteUnderCond_of_not_crossed {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncRevResidualFiniteUnder r res) + (hnotcross : ¬ (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) < + r.begin.key) : + finiteUnderCond r res := by + obtain ⟨db, hcomp⟩ := hread + obtain ⟨hmore, hrev, hbeg, hne, hend, hLE, hcrossL, hcount⟩ := hcell + have hbound := reportedBelowBegin_le_beginSlack_succ hcomp hbeg + have hcard : intervalCard (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + r.begin.key = 0 := + intervalCard_eq_zero_of_le (le_of_not_gt hnotcross) + unfold finiteUnderCond + rw [hcard, add_zero, ENat.natCast_le_natCast] + omega + +/-- The non-crossed finite under cell of the reversed crossed negative-begin truncated +residual: the finite under cell with `begin.key ≤ keyAfter head`. -/ +abbrev negCrossTruncRevResidualFiniteUnderNotCrossed (r : RangeRead) (res : Result) : Prop := + negCrossTruncRevResidualFiniteUnder r res ∧ + ¬ (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) < r.begin.key + +/-- **The non-crossed finite under cell of the residual is minimal.** Here the interval +`[keyAfter head, begin.key)` is empty, so the capacity condition holds and the closed +form is `[last reported, keyAfter head)`. The reduced offset-one read has its begin key +at or below its end key, so minimality transfers from the reversed under-end cell. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_notCrossed + {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncRevResidualFiniteUnderNotCrossed r res) : + Minimal r res (closedFormNegCrossTruncRevResidualFiniteUnder r res) := by + obtain ⟨hbase, hnotcross⟩ := hcell + have hbase0 : negCrossTruncRevResidualFiniteUnder r res := hbase + obtain ⟨hmore, hrev, hbeg, hne, hend, hLE, hcrossL, hcount⟩ := hbase + let r' : RangeRead := + { r with end_ := selAt (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) } + have hcompat : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res := + fun db => compatible_iff_finiteUnder_reduce hmore hrev hne hend hLE hcount db + have hread' : Readable r' res := by + obtain ⟨db, hdb⟩ := hread + exact ⟨db, (hcompat db).mp hdb⟩ + have hr'end : r'.end_ = + selAt (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) := rfl + have hr'key : r'.end_.key = + (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) := rfl + have hr'off : r'.end_.offset = 1 := rfl + have hr'beg : r'.begin = r.begin := rfl + have hr'rev : r'.reverse = true := hrev + have hr'crossL : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r'.begin.key := by + rw [hr'beg]; exact hcrossL + have hr'under : r'.begin.key ≤ r'.end_.key := by + rw [hr'key]; exact le_of_not_gt hnotcross + have hcond : finiteUnderCond r res := finiteUnderCond_of_not_crossed hread hbase0 hnotcross + have hcell' : negCrossTruncRevUnder r' res := + ⟨hmore, hr'rev, hbeg, hne, hr'crossL, hr'under, by rw [hr'off]⟩ + have hmin' : Minimal r' res (closedFormNegCrossTruncRevUnder r' res) := + minimal_closedForm_of_negCross_trunc_rev_under hread' hcell' + have hcr : closedFormNegCrossTruncRevResidualFiniteUnder r res = + closedFormNegCrossTruncRevUnder r' res := by + simp [closedFormNegCrossTruncRevResidualFiniteUnder, closedFormNegCrossTruncRevUnder, + truncatedRangeRev, hcond, hr'end] + rw [hcr] + exact (minimal_congr_of_compatible_iff hcompat).mpr hmin' + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevResidualFiniteOver.lean b/FormalConflictRange/NegCrossTruncRevResidualFiniteOver.lean new file mode 100644 index 0000000..d6819ea --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevResidualFiniteOver.lean @@ -0,0 +1,709 @@ +/- +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.NegCrossTruncRevResidualFinite + +/-! +# The finite *over* cell of the reversed crossed negative-begin truncated residual + +This module starts closing the last reversed crossed negative-begin truncated cell of +the total closed form: the finite branch of the residual on which the reported last lies +strictly above `end_.key`. + +The setting is + +* `res.more = true`, `r.reverse = true`, `r.begin.offset < 0`, `res.keys ≠ []`; +* `2 ≤ r.end_.offset`; +* `r.end_.key < last` (the reported last `L` is above the end key); +* `last < begin.key` (the begin selector lies above the reported last); +* the finite count `|res.keys| = end_.offset - 1`. + +Since all reported keys lie above `end_.key`, the finite count says exactly that the +stored keys in `[end_.key, resolve end_)` are the reported ones. The first structural +fact is that no compatible database stores a key in `[end_.key, last)`. + +The main reduction is that compatibility with `r` is the same as compatibility with the +read whose end key is `last` (same offset), *provided* the database stores nothing in +`[end_.key, last)`. The reduced read lies in the already-closed finite under cell, so +minimality transfers. When `begin.key ≤ head`, the capacity condition `finiteUnderCond` +holds automatically, so the upper endpoint is `keyAfter head`; when the head lies below +`begin.key`, the remaining gap keys are supplied by a toggle witness. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1600000 + +/-- A subset of a duplicate-free list is no longer than it. -/ +theorem length_le_of_nodup_subset {l₁ l₂ : List Key} (h : l₁.Nodup) (hsub : l₁ ⊆ l₂) : + l₁.length ≤ l₂.length := by + obtain ⟨l, hperm, hsub'⟩ := List.subperm_iff.mp (h.subperm hsub) + calc l₁.length ≤ l.length := hsub'.length_le + _ = l₂.length := hperm.length_eq + +/-- **No stored key lies in `[end_.key, last)` in the finite over cell.** The end +resolution window `[end_.key, resolve end_)` is exactly as long as the reported list and +already contains every reported key, so it cannot also contain a key strictly below the +reported last. -/ +theorem between_end_key_last_eq_nil {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (ht : res.keys.length = (r.end_.offset - 1).toNat) : + between db r.end_.key ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) = [] := by + classical + have hLlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := + getLastD_eq_getLast_of_ne hne + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := List.getLast_mem hne + have hEL' : r.end_.key < ((L : Key) : WithTop Key) := by rw [← hLlast]; exact hEL + have hELE : r.end_.key ≤ ((L : Key) : WithTop Key) := le_of_lt hEL' + set e : ℕ := (r.end_.offset - 1).toNat with hedef + have ht' : res.keys.length = e := by rw [ht, hedef] + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := + fun x hx => (hsort.pairwise.imp (fun h => le_of_lt h)).rel_getLast hx + have hLR : ((L : Key) : WithTop Key) < resolve db r.end_ := by + have hck : L ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset hLmem + exact (RangeRead.mem_completeKeys.mp hck).2.2 + have hLRle : ((L : Key) : WithTop Key) ≤ resolve db r.end_ := le_of_lt hLR + have hoff : 1 ≤ r.end_.offset := by omega + have hnsub : res.keys.Nodup := + List.Nodup.sublist (RangeRead.compatible_keys_prefix hcomp).sublist (RangeRead.completeKeys_nodup db r) + rcases eq_or_ne (resolve db r.end_) (⊤ : WithTop Key) with hRtop | hRne + · -- the end resolution is the sentinel + have hresend := resolve_eq_filter_getElem? (db := db) (s := r.end_) hoff + have hgetnone : (db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))))[e]? = none := by + cases hq : (db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))))[e]? with + | none => rfl + | some k => + rw [hq] at hresend + exact absurd hresend (by simp [hRtop]) + have hQle : (db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).length ≤ e := + List.getElem?_eq_none_iff.mp hgetnone + -- every reported key is stored at or above `end_.key` + have hsubQ : res.keys ⊆ db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + intro k hk + exact List.mem_filter.mpr ⟨RangeRead.compatible_keys_subset hcomp hk, + by rw [decide_eq_true_eq]; exact le_trans hELE (WithTop.coe_le_coe.mpr (hLle k hk))⟩ + have htQ : res.keys.length ≤ (db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).length := + length_le_of_nodup_subset hnsub hsubQ + -- so `[end_.key, last)` is empty: any key there would enlarge the above-list + rw [List.eq_nil_iff_forall_not_mem] + intro x hx + rw [between_eq_filter_above] at hx + obtain ⟨hxA, hxL⟩ := List.mem_filter.mp hx + rw [hLlast] at hxL + have hxL' : ((x : Key) : WithTop Key) < ((L : Key) : WithTop Key) := + of_decide_eq_true hxL + have hxnotres : x ∉ res.keys := fun hres => + absurd (WithTop.coe_le_coe.mpr (hLle x hres)) (not_le.mpr hxL') + have hnsub2 : (x :: res.keys).Nodup := by + rw [List.nodup_cons]; exact ⟨hxnotres, hnsub⟩ + have hsub2 : (x :: res.keys) ⊆ db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + intro y hy + rcases List.mem_cons.mp hy with rfl | hy + · exact hxA + · exact hsubQ hy + have hlen : (x :: res.keys).length ≤ (db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).length := + length_le_of_nodup_subset hnsub2 hsub2 + simp only [List.length_cons, ht'] at hlen + omega + · -- a genuine end resolution + have hlenER : (between db r.end_.key (resolve db r.end_)).length = e := + length_between_end_resolve_eq_of_ne_top hend hRne + have hLRrev : res.keys = (between db ((L : Key) : WithTop Key) + (resolve db r.end_)).reverse := + res_keys_eq_reverse_between_resolve_end hcomp hmore hrev hne + have hlenLR : (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length = + res.keys.length := by + have h := congrArg List.length hLRrev + rw [List.length_reverse] at h + exact h.symm + have h1 := cnt_eq_add_between (db := db) hELE + have h2 := cnt_eq_add_between (db := db) hLRle + have h3 := cnt_eq_add_between (db := db) (le_trans hELE hLRle) + rw [ht'] at hlenLR + have : (between db r.end_.key ((L : Key) : WithTop Key)).length = 0 := by + omega + rw [hLlast] + exact List.eq_nil_of_length_eq_zero this + +/-- When the interval `[E, L)` stores no key, the lists of keys at or above `E` and at +or above `L` coincide. -/ +theorem above_eq_of_between_eq_nil {db : DB} {E L : WithTop Key} (hE : E ≤ L) + (h : between db E L = []) : above db E = above db L := by + rw [above_B_eq_filter hE] + exact (List.filter_eq_self.mpr fun x hx => by + rw [decide_eq_true_eq] + by_contra hlt + push Not at hlt + have hmem : x ∈ between db E L := by + rw [between_eq_filter_above] + exact List.mem_filter.mpr ⟨hx, by rw [decide_eq_true_eq]; exact hlt⟩ + rw [h] at hmem + exact absurd hmem List.not_mem_nil).symm + +/-- **The finite over cell reduces to the read whose end key is the reported last.** +Provided no key is stored in `[end_.key, last)`, replacing the end key by the reported +last at the same offset leaves the complete keys — hence compatibility — unchanged. -/ +theorem compatible_iff_over_reduce {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (ht : res.keys.length = (r.end_.offset - 1).toNat) : + ∀ db, RangeRead.Compatible db r res ↔ + (between db r.end_.key ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) = []) ∧ + RangeRead.Compatible db + { r with end_ := ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), + r.end_.offset⟩ } res := by + intro db + set Lk : Key := res.keys.getLastD ([] : Key) with hLk + have hLlast : Lk = res.keys.getLast hne := getLastD_eq_getLast_of_ne hne + have hELE : r.end_.key ≤ ((Lk : Key) : WithTop Key) := le_of_lt hEL + have hoff : 1 ≤ r.end_.offset := by omega + let r' : RangeRead := + { r with end_ := ⟨((Lk : Key) : WithTop Key), r.end_.offset⟩ } + have hr'end : r'.end_ = ⟨((Lk : Key) : WithTop Key), r.end_.offset⟩ := rfl + have hr'beg : r'.begin = r.begin := rfl + have hr'rev : r'.reverse = r.reverse := rfl + have hr'lim : r'.limit = r.limit := rfl + have hresolve : ∀ (_ : between db r.end_.key ((Lk : Key) : WithTop Key) = []), + resolve db r.end_ = resolve db r'.end_ := by + intro hnil + have habove : above db r.end_.key = above db ((Lk : Key) : WithTop Key) := + above_eq_of_between_eq_nil hELE hnil + rw [resolve_eq_filter_getElem? (db := db) (s := r.end_) hoff, + resolve_eq_filter_getElem? (db := db) (s := r'.end_) hoff, hr'end] + simp only [above] at habove + rw [habove] + have hck : ∀ (_ : between db r.end_.key ((Lk : Key) : WithTop Key) = []), + RangeRead.completeKeys db r = RangeRead.completeKeys db r' := by + intro hnil + have hres := hresolve hnil + unfold RangeRead.completeKeys + rw [hr'rev, hrev] + simp only [Bool.not_true, ite_false, ite_true] + rw [hres] + constructor + · intro hcomp + have hnil := between_end_key_last_eq_nil hcomp hmore hrev hne hend hEL ht + exact ⟨hnil, (compatible_congr_completeKeys (hck hnil) hr'lim).mp hcomp⟩ + · rintro ⟨hnil, hcomp'⟩ + exact (compatible_congr_completeKeys (hck hnil) hr'lim).mpr hcomp' + +/-- A key absent from every compatible database is necessary: a sound range omitting it +would see a compatible database and the same database with the key inserted as agreeing, +though only the former is compatible. -/ +theorem necessary_of_forall_not_mem {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) + (habs : ∀ db, RangeRead.Compatible db r res → k ∉ db.keys) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hkdb : k ∉ db₀.keys := habs db₀ hcomp₀ + have hagree : AgreeOn cr db₀ (db₀.insert k hkdb) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hkdb hkcr).symm + have hcomp₁ : RangeRead.Compatible (db₀.insert k hkdb) r res := + (hs db₀ (db₀.insert k hkdb) hagree).mp hcomp₀ + exact habs (db₀.insert k hkdb) hcomp₁ (DB.mem_insert_self db₀ k hkdb) + +/-- If one database stores no key in `[E, L)` and two databases agree on `[E, U)` with +`L ≤ U`, then neither does the other. -/ +theorem between_eq_nil_of_agreeOn {db₁ db₂ : DB} {E L U : WithTop Key} + (hE : E ≤ L) (hLU : L ≤ U) (hnil : between db₁ E L = []) + (hagree : AgreeOn ⟨E, U⟩ db₁ db₂) : + between db₂ E L = [] := by + rw [List.eq_nil_iff_forall_not_mem] + intro k hk + rw [between_eq_filter_above] at hk + obtain ⟨hkA, hkL⟩ := List.mem_filter.mp hk + have hkE : E ≤ ((k : Key) : WithTop Key) := (mem_above.mp hkA).2 + have hkL' : ((k : Key) : WithTop Key) < L := of_decide_eq_true hkL + have hkdb₂ : k ∈ db₂.keys := (mem_above.mp hkA).1 + have hkcr₂ : k ∈ (⟨E, U⟩ : ConflictRange).keys db₂ := + ConflictRange.mem_keys_iff.mpr ⟨hkdb₂, hkE, lt_of_lt_of_le hkL' hLU⟩ + rw [← hagree] at hkcr₂ + obtain ⟨hkdb₁, _hkE', hkU'⟩ := ConflictRange.mem_keys_iff.mp hkcr₂ + have hkbetween : k ∈ between db₁ E L := by + rw [between_eq_filter_above] + exact List.mem_filter.mpr ⟨mem_above.mpr ⟨hkdb₁, hkE⟩, + by rw [decide_eq_true_eq]; exact hkL'⟩ + rw [hnil] at hkbetween + exact absurd hkbetween List.not_mem_nil + +/-- The finite over cell of the reversed crossed negative-begin truncated residual: +the finite branch with the reported last strictly above `end_.key`, with the reported last +below `begin.key`. -/ +abbrev negCrossTruncRevResidualFiniteOver (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + r.end_.key < ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat + +/-- The closed form of the finite over cell: the reduced under-cell rule with its lower +endpoint lifted to `end_.key`. -/ +noncomputable def closedFormNegCrossTruncRevResidualFiniteOver + (r : RangeRead) (res : Result) : ConflictRange := by + classical + let U : WithTop Key := + if finiteUnderCond r res then + (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + else r.begin.key + exact ⟨r.end_.key, U⟩ + +/-- **Gap-key necessity in the finite over cell.** The witness database stores the +reported keys in ascending order together with `beginSlack + 1 - t` gap keys in +`[keyAfter head, begin.key)` avoiding the target, so its begin anchor is exactly zero, +resolving onto the reported last, while its end resolution is the first gap key and +excludes them. Inserting the target raises the begin anchor to one: the reported last +leaves the window and the reported keys can no longer be a prefix of the complete keys. +The capacity hypothesis is the negation of `finiteUnderCond`. -/ +theorem necessary_of_mem_gap_of_negCross_trunc_rev_residualFiniteOver + {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) + (hheadB : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key) + (hcount : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + (hcond : ¬ finiteUnderCond r res) + {k : Key} + (hkH : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < (k : WithTop Key)) + (hkB : ((k : Key) : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + set t : ℕ := res.keys.length with htdef + set e : ℕ := (r.end_.offset - 1).toNat with hedef + have hlastD : res.keys.getLastD ([] : Key) = L := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] + exact hLdef.symm + have hheadD : res.keys.headD ([] : Key) = H := by + rw [List.headD_eq_head?_getD, List.head?_eq_some_head hne] + exact hHdef.symm + have hheadOpt : res.keys.head?.getD ([] : Key) = H := by + rw [List.head?_eq_some_head hne] + exact hHdef.symm + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hHge : ∀ x ∈ res.keys, x ≤ H := by + intro x hx; rw [hHdef]; exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hEL' : r.end_.key < ((L : Key) : WithTop Key) := by rw [← hlastD]; exact hEL + have hheadB' : ((H : Key) : WithTop Key) < r.begin.key := by rw [← hheadD]; exact hheadB + have hkH' : ((H : Key) : WithTop Key) < (k : WithTop Key) := by rw [← hheadD]; exact hkH + have hHk : H < k := WithTop.coe_lt_coe.mp hkH' + have hE_le_reported : ∀ x ∈ res.keys, r.end_.key ≤ ((x : Key) : WithTop Key) := by + intro x hx + exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hLle x hx)) + have hreported_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hHge x hx)) hheadB' + have hcount_t : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + t := by + rw [htdef, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact hE_le_reported x hx + have ht_e : t = e := by rw [← hcount_t, hcount, hedef] + have hrepB : reportedBelowBegin r res = t := by + unfold reportedBelowBegin + rw [htdef, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact hreported_lt_B x hx + have ht_le : t ≤ beginSlack r + 1 := by + have h := reportedBelowBegin_le_beginSlack_succ hcomp₀ hbeg + rwa [hrepB] at h + -- the negation of the capacity condition gives one more gap slot than witness keys + have hcond_ineq : (beginSlack r : ℕ∞) < + ((t - 1 : ℕ) : ℕ∞) + + intervalCard (((keyAfter H : Key)) : WithTop Key) r.begin.key := by + have h := not_le.mp (by simpa [finiteUnderCond] using hcond) + rwa [hrepB, hheadOpt] at h + have hcard : (((beginSlack r + 1 - t) + 1 : ℕ) : ℕ∞) ≤ + intervalCard (((keyAfter H : Key)) : WithTop Key) r.begin.key := by + cases hcap : intervalCard (((keyAfter H : Key)) : WithTop Key) r.begin.key with + | top => exact le_top + | coe n => + have h' : (beginSlack r : ℕ∞) < ((t - 1 : ℕ) : ℕ∞) + ((n : ℕ) : ℕ∞) := by + rw [hcap] at hcond_ineq; exact hcond_ineq + rw [← ENat.natCast_add] at h' + have hnat : beginSlack r < (t - 1) + n := by exact_mod_cast h' + have hn : beginSlack r + 1 - t + 1 ≤ n := by omega + exact_mod_cast hn + obtain ⟨dbG, hGrange, hkG, hGlen⟩ := + exists_db_keysIn_avoid (B := (((keyAfter H : Key)) : WithTop Key)) + (E := r.begin.key) (k := k) (m := beginSlack r + 1 - t) hcard + set G : List Key := dbG.keys with hGdef + have hGmem : ∀ x ∈ G, (((keyAfter H : Key)) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < r.begin.key := by + intro x hx + simpa only [G, keysIn, Set.mem_ofPred_eq] using hGrange x (by simpa [hGdef] using hx) + have hGlen' : G.length = beginSlack r + 1 - t := by rw [hGdef]; exact hGlen + have hH_lt_G : ∀ x ∈ G, ((H : Key) : WithTop Key) < (x : WithTop Key) := by + intro x hx + exact lt_of_lt_of_le (by exact_mod_cast keyAfter_self_lt H) (hGmem x hx).1 + -- assemble the ascending witness database + have hresSort : res.keys.reverse.SortedLT := hsort.reverse + have hGsorted : G.SortedLT := by rw [hGdef]; exact dbG.sorted + have hcrossG : ∀ x ∈ res.keys.reverse, ∀ y ∈ G, x < y := by + intro x hx y hy + have hx' : x ∈ res.keys := List.mem_reverse.mp hx + exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hHge x hx')) (hH_lt_G y hy)) + have hkeysSorted : (res.keys.reverse ++ G).SortedLT := + List.sortedLT_append.mpr ⟨hresSort, hGsorted, hcrossG⟩ + let db_a : DB := ⟨res.keys.reverse ++ G, hkeysSorted⟩ + have hdb_a : db_a.keys = res.keys.reverse ++ G := rfl + have hcntB : db_a.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = t + G.length := by + rw [hdb_a, List.countP_append, List.countP_reverse] + have h1 : res.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = t := by + rw [htdef, List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact hreported_lt_B x hx + have h2 : G.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = G.length := by + rw [List.countP_eq_length] + intro x hx; rw [decide_eq_true_eq]; exact (hGmem x hx).2 + rw [h1, h2] + have hanchor : r.begin.anchor db_a = 0 := by + rw [anchor_eq_count_sub_slack db_a r (le_of_lt hbeg), hcntB, hGlen'] + have hts : t + (beginSlack r + 1 - t) = beginSlack r + 1 := Nat.add_sub_of_le ht_le + rw [hts]; push_cast; ring + have h0lt : (0 : ℕ) < db_a.keys.length := by + rw [hdb_a, List.length_append, List.length_reverse] + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + omega + have hmin_a : ∀ x ∈ db_a.keys, L ≤ x := by + intro x hx + rw [hdb_a] at hx + rcases List.mem_append.mp hx with hxr | hxG + · exact hLle x (List.mem_reverse.mp hxr) + · exact le_trans (hLle H hHmem) (le_of_lt (WithTop.coe_lt_coe.mp (hH_lt_G x hxG))) + have hLmem_a : L ∈ db_a.keys := by + rw [hdb_a]; exact List.mem_append_left _ (List.mem_reverse.mpr hLmem) + have hget0 : db_a.keys[(0 : ℕ)] = L := + getElem_zero_eq_of_forall_le hkeysSorted hmin_a hLmem_a h0lt + have hresBegin : resolve db_a r.begin = ((L : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db_a := by rw [hanchor] + have hlt : (r.begin.anchor db_a).toNat < db_a.keys.length := by + rw [hanchor]; simpa using h0lt + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanchor]; rfl + simpa [hidx] using hget0 + -- the end filter is the whole ascending list + have hEfilt : db_a.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) = + res.keys.reverse ++ G := by + rw [hdb_a] + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hxr | hxG + · exact hE_le_reported x (List.mem_reverse.mp hxr) + · exact le_of_lt (lt_of_lt_of_le hEL' (WithTop.coe_le_coe.mpr + (le_trans (hLle H hHmem) (le_of_lt (WithTop.coe_lt_coe.mp (hH_lt_G x hxG)))))) + have hresEnd : resolve db_a r.end_ = + (match G[0]? with + | some x => ((x : Key) : WithTop Key) + | none => (⊤ : WithTop Key)) := by + rw [resolve_eq_filter_getElem? (db := db_a) (s := r.end_) (by omega)] + rw [hEfilt] + have hidx : (r.end_.offset - 1).toNat = t := by omega + rw [hidx] + have h1 : (res.keys.reverse ++ G)[t]? = G[0]? := by + rw [List.getElem?_append_right (by rw [List.length_reverse, htdef])] + rw [htdef, List.length_reverse, Nat.sub_self] + rw [h1] + rfl + -- every reported key is below the end resolution, no gap key is + have hrep_lt_res : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < resolve db_a r.end_ := by + intro x hx + rw [hresEnd] + cases hG : G with + | nil => exact WithTop.coe_lt_top _ + | cons g gs => + simp only [List.getElem?_cons_zero] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hHge x hx)) (hH_lt_G g (by rw [hG]; exact List.mem_cons_self)) + have hG_not_lt_res : ∀ y ∈ G, ¬ ((y : Key) : WithTop Key) < resolve db_a r.end_ := by + intro y hy + rw [hresEnd] + cases hG : G with + | nil => rw [hG] at hy; exact absurd hy List.not_mem_nil + | cons g gs => + simp only [List.getElem?_cons_zero] + have hgle : g ≤ y := by + have hpair : (g :: gs).Pairwise (· < ·) := by + rw [← List.sortedLT_iff_pairwise]; rw [← hG]; exact hGsorted + have := (hpair.imp le_of_lt).rel_head (by rw [hG] at hy; exact hy) + simpa using this + exact not_lt.mpr (WithTop.coe_le_coe.mpr hgle) + have hfilter : db_a.keys.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by + rw [hdb_a, List.filter_append] + have h1 : (res.keys.reverse).filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (hLle x (List.mem_reverse.mp hx)), + hrep_lt_res x (List.mem_reverse.mp hx)⟩ + have h2 : G.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db_a r.end_)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + intro hP + exact hG_not_lt_res x hx hP.2 + rw [h1, h2, List.append_nil] + have hcomplete : RangeRead.completeKeys db_a r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev] + simp only [ite_true] + rw [hresBegin, hfilter, List.reverse_reverse] + have hcompat_a : RangeRead.Compatible db_a r res := + ⟨hcomp₀.1, Or.inl ⟨hmore, by rw [hcomplete], hne⟩⟩ + have hk_a : k ∉ db_a.keys := by + rw [hdb_a] + intro hmem + rcases List.mem_append.mp hmem with hx | hx + · exact absurd (WithTop.coe_le_coe.mpr (hHge k (List.mem_reverse.mp hx))) (not_le.mpr hkH') + · exact hkG (by simpa [hGdef] using hx) + -- insert the target: it lands above the reported keys and at the gap bottom + let db_b : DB := db_a.insert k hk_a + have hins : (res.keys.reverse ++ G).orderedInsert (· ≤ ·) k = + res.keys.reverse ++ G.orderedInsert (· ≤ ·) k := by + refine orderedInsert_append_of_forall_lt_left ?_ + intro x hx + exact WithTop.coe_lt_coe.mp + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hHge x (List.mem_reverse.mp hx))) hkH') + have hb_keys : db_b.keys = res.keys.reverse ++ G.orderedInsert (· ≤ ·) k := by + rw [show db_b.keys = db_a.keys.orderedInsert (· ≤ ·) k from rfl, hdb_a, hins] + have hb_all : ∀ x ∈ db_b.keys, + decide (((x : Key) : WithTop Key) < r.begin.key) = true := by + intro x hx + rw [decide_eq_true_eq] + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hx' | hx' + · exact hreported_lt_B x (List.mem_reverse.mp hx') + · rw [List.mem_orderedInsert] at hx' + rcases hx' with rfl | hxG + · exact hkB + · exact (hGmem x hxG).2 + have hb_len : db_b.keys.length = t + (beginSlack r + 1 - t) + 1 := by + rw [hb_keys, List.length_append, List.length_reverse, List.orderedInsert_length, + hGlen', htdef] + omega + have hb_cntB : db_b.keys.countP (fun x : Key => + decide (((x : Key) : WithTop Key) < r.begin.key)) = t + (beginSlack r + 1 - t) + 1 := by + rw [← hb_len, List.countP_eq_length] + exact hb_all + have hanchor_b : r.begin.anchor db_b = 1 := by + rw [anchor_eq_count_sub_slack db_b r (le_of_lt hbeg), hb_cntB] + have hts : t + (beginSlack r + 1 - t) = beginSlack r + 1 := Nat.add_sub_of_le ht_le + rw [hts]; push_cast; ring + have hb_min : ∀ x ∈ db_b.keys, L ≤ x := by + intro x hx + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hxr | hxG + · exact hLle x (List.mem_reverse.mp hxr) + · rw [List.mem_orderedInsert] at hxG + rcases hxG with rfl | hxG + · exact le_trans (hLle H hHmem) (le_of_lt (WithTop.coe_lt_coe.mp hkH')) + · exact le_trans (hLle H hHmem) (le_of_lt (WithTop.coe_lt_coe.mp (hH_lt_G x hxG))) + have hLmem_b : L ∈ db_b.keys := by + rw [hb_keys]; exact List.mem_append_left _ (List.mem_reverse.mpr hLmem) + have h0lt_b : 0 < db_b.keys.length := by rw [hb_len]; omega + have hget0_b : db_b.keys[(0 : ℕ)] = L := + getElem_zero_eq_of_forall_le db_b.sorted hb_min hLmem_b h0lt_b + have h1lt : (1 : ℕ) < db_b.keys.length := by rw [hb_len]; omega + have hresBegin_b : resolve db_b r.begin = ((db_b.keys[(1 : ℕ)] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db_b := by rw [hanchor_b]; norm_num + have hlt : (r.begin.anchor db_b).toNat < db_b.keys.length := by + rw [hanchor_b]; simpa using h1lt + rw [resolve_eq_getElem h0 hlt] + have hidx : (r.begin.anchor db_b).toNat = 1 := by rw [hanchor_b]; rfl + simp [hidx] + have hLlt : L < db_b.keys[(1 : ℕ)] := by + have h01 : (0 : ℕ) < db_b.keys.length := by omega + have h := (List.SortedLT.getElem_lt_getElem_iff db_b.sorted (hi := h01) + (hj := h1lt)).mpr (show (0 : ℕ) < 1 by omega) + rwa [hget0_b] at h + have hLnot_ck : L ∉ RangeRead.completeKeys db_b r := by + intro hmem + obtain ⟨_, hck, _⟩ := RangeRead.mem_completeKeys.mp hmem + rw [hresBegin_b] at hck + exact absurd hck (not_le.mpr (WithTop.coe_lt_coe.mpr hLlt)) + have hnotCompat_b : ¬ RangeRead.Compatible db_b r res := by + intro hc + rcases hc.2 with ⟨_, hpre, _⟩ | ⟨hm, _⟩ + · exact hLnot_ck (hpre.subset hLmem) + · rw [hmore] at hm; exact absurd hm (by simp) + refine necessary_of_toggle ⟨db_a, hcompat_a, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_a] + exact hnotCompat_b + +/-- **The finite over cell of the reversed residual is minimal.** The closed form is +`[end_.key, U)` with `U` the under-cell upper endpoint. Keys in `[end_.key, last)` are +absent from every compatible database, keys in `[last, head]` are reported (hence +necessary), and keys in `(head, U)` are necessary by the gap-toggle witness when `U` is +`begin.key`; the alternative `U = keyAfter head` leaves no key in that gap. Soundness +transfers from the reduced finite-under read, using that agreement on `[end_.key, U)` +also fixes the absence in `[end_.key, last)`. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residualFinite_over + {r : RangeRead} {res : Result} + (hread : Readable r res) (hcell : negCrossTruncRevResidualFiniteOver r res) : + Minimal r res (closedFormNegCrossTruncRevResidualFiniteOver r res) := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + obtain ⟨hmore, hrev, hbeg, hne, hend, hEL, hLB, hcount⟩ := hcell + set L : Key := res.keys.getLastD ([] : Key) with hLdef + set H : Key := res.keys.headD ([] : Key) with hHdef + have hLg : L = res.keys.getLast hne := by + rw [hLdef, getLastD_eq_getLast_of_ne hne] + have hHg : H = res.keys.head hne := by + rw [hHdef, headD_eq_head_of_ne hne] + have hLmem : L ∈ res.keys := by rw [hLg]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hHg]; exact List.head_mem hne + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLg]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hHge : ∀ x ∈ res.keys, x ≤ H := by + intro x hx; rw [hHg]; exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hEL' : r.end_.key < ((L : Key) : WithTop Key) := hEL + have hLB' : ((L : Key) : WithTop Key) < r.begin.key := hLB + -- the finite count is the length, because every reported key is at or above `L` + have ht : res.keys.length = (r.end_.offset - 1).toNat := by + rw [← hcount] + exact (List.countP_eq_length.mpr (fun x hx => by + rw [decide_eq_true_eq] + exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hLle x hx)))).symm + let U : WithTop Key := + if finiteUnderCond r res then + (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) + else r.begin.key + let r' : RangeRead := + { r with end_ := ⟨((L : Key) : WithTop Key), r.end_.offset⟩ } + have hcompat : ∀ db, RangeRead.Compatible db r res ↔ + between db r.end_.key ((L : Key) : WithTop Key) = [] ∧ + RangeRead.Compatible db r' res := by + intro db + simpa [r', hLdef] using + compatible_iff_over_reduce (r := r) (res := res) hmore hrev hne hend hEL ht db + have hread' : Readable r' res := + ⟨db₀, ((hcompat db₀).mp hcomp₀).2⟩ + -- the reduced read lies in the finite under cell + have hr'beg : r'.begin = r.begin := rfl + have hr'rev : r'.reverse = true := hrev + have hr'endoff : r'.end_.offset = r.end_.offset := rfl + have hr'endkey : r'.end_.key = ((L : Key) : WithTop Key) := by + change ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) = ((L : Key) : WithTop Key) + exact congrArg (fun x : Key => (x : WithTop Key)) hLdef.symm + have hcountL : res.keys.countP (fun k : Key => + decide (((L : Key) : WithTop Key) ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat := by + rw [← ht] + exact List.countP_eq_length.mpr (fun x hx => by + rw [decide_eq_true_eq]; exact WithTop.coe_le_coe.mpr (hLle x hx)) + have hbase : negCrossTruncRevResidualFiniteUnder r' res := by + refine ⟨hmore, hr'rev, ?_, hne, ?_, ?_, ?_, ?_⟩ + · rw [hr'beg]; exact hbeg + · rw [hr'endoff]; exact hend + · rw [hr'endkey] + · rw [hr'beg]; rw [← hLdef]; exact hLB + · rw [hr'endoff, hr'endkey]; exact hcountL + have hcrUnder : closedFormNegCrossTruncRevResidualFiniteUnder r' res = + (⟨((L : Key) : WithTop Key), U⟩ : ConflictRange) := by + have hcondEq : finiteUnderCond r' res = finiteUnderCond r res := rfl + simp only [closedFormNegCrossTruncRevResidualFiniteUnder, U, hcondEq, hLdef] + rfl + have hcrOver : closedFormNegCrossTruncRevResidualFiniteOver r res = + (⟨r.end_.key, U⟩ : ConflictRange) := by + simp only [closedFormNegCrossTruncRevResidualFiniteOver, U] + have hLU : ((L : Key) : WithTop Key) ≤ U := by + simp only [U] + split + · exact le_of_lt (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hLle H hHmem)) + (by exact_mod_cast keyAfter_self_lt H)) + · exact le_of_lt hLB' + have hmain : Sound r' res (⟨((L : Key) : WithTop Key), U⟩ : ConflictRange) → + Minimal r res (closedFormNegCrossTruncRevResidualFiniteOver r res) := by + intro hsnd' + rw [hcrOver] + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + rw [hcompat db₁, hcompat db₂] + constructor + · rintro ⟨hnil₁, hcomp₁⟩ + exact ⟨between_eq_nil_of_agreeOn (le_of_lt hEL') hLU hnil₁ hagree, + (hsnd' db₁ db₂ (agreeOn_of_subrange + (subrange_of_le (le_of_lt hEL') (le_refl U)) hagree)).mp hcomp₁⟩ + · rintro ⟨hnil₂, hcomp₂⟩ + exact ⟨between_eq_nil_of_agreeOn (le_of_lt hEL') hLU hnil₂ (AgreeOn.symm hagree), + (hsnd' db₁ db₂ (agreeOn_of_subrange + (subrange_of_le (le_of_lt hEL') (le_refl U)) hagree)).mpr hcomp₂⟩ + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hkE, hkU⟩ := hk + by_cases hkL : ((k : Key) : WithTop Key) < ((L : Key) : WithTop Key) + · refine necessary_of_forall_not_mem ⟨db₀, hcomp₀⟩ ?_ + intro db hcomp hkdb + obtain ⟨hnil, _⟩ := (hcompat db).mp hcomp + have hkbet : k ∈ between db r.end_.key ((L : Key) : WithTop Key) := by + rw [between_eq_filter_above] + exact List.mem_filter.mpr ⟨mem_above.mpr ⟨hkdb, hkE⟩, + by rw [decide_eq_true_eq]; exact hkL⟩ + rw [hnil] at hkbet + exact absurd hkbet List.not_mem_nil + · push Not at hkL + by_cases hkH : k ≤ H + · exact Necessary.of_between (necessary_of_mem_result ⟨db₀, hcomp₀⟩ hLmem) + (necessary_of_mem_result ⟨db₀, hcomp₀⟩ hHmem) (WithTop.coe_le_coe.mp hkL) hkH + · push Not at hkH + have hnotcond : ¬ finiteUnderCond r res := by + intro hcond + have hUeq : U = (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) := by + simp only [U, hcond, ite_true] + rw [hUeq] at hkU + have hHk' : res.keys.headD ([] : Key) < k := by rw [← hHdef]; exact hkH + have hkK : k < keyAfter (res.keys.headD ([] : Key)) := WithTop.coe_lt_coe.mp hkU + exact absurd hHk' (not_lt.mpr ((lt_keyAfter_iff k _).mp hkK)) + have hkUI : ((k : Key) : WithTop Key) < r.begin.key := by + simp only [U, hnotcond, ite_false] at hkU + exact hkU + exact necessary_of_mem_gap_of_negCross_trunc_rev_residualFiniteOver + ⟨db₀, hcomp₀⟩ hmore hrev hne hbeg hend (by rw [← hLdef]; exact hEL) + (by rw [← hHdef]; exact lt_trans (WithTop.coe_lt_coe.mpr hkH) hkUI) + hcount hnotcond + (WithTop.coe_lt_coe.mpr hkH) hkUI + by_cases hcross : (((keyAfter (res.keys.headD ([] : Key)) : Key)) : WithTop Key) < + r'.begin.key + · refine hmain ?_ + rw [← hcrUnder] + exact (minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_crossed hread' + ⟨hbase, hcross⟩).sound + · refine hmain ?_ + rw [← hcrUnder] + exact (minimal_closedForm_of_negCross_trunc_rev_residualFinite_under_notCrossed hread' + ⟨hbase, hcross⟩).sound + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevResidualLargeSentinel.lean b/FormalConflictRange/NegCrossTruncRevResidualLargeSentinel.lean new file mode 100644 index 0000000..dca5e07 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevResidualLargeSentinel.lean @@ -0,0 +1,1134 @@ +/- +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.NegCrossTruncRevResidualLargeTop + +/-! +# The sentinel-begin large-gap top cell of the reversed truncated residual + +This module closes the sentinel-begin companion of +`NegCrossTruncRevResidualLargeTop.lean`: the reversed crossed negative-begin +truncated residual at end offset at least two, with the end key strictly below +the reported last and the large-gap condition, but with `begin.key = ⊤`. + +Writing `E := r.end_.key`, `L` for the reported last, `t := #reported`, +`e := (r.end_.offset - 1).toNat` and `card := intervalCard E L`, the minimal +range is + +* `[L, ⊤)` when `e = card + t` **and** `t + r.begin.offset = 1`; +* `[E, ⊤)` otherwise. + +In the first (tight) case compatibility forces `resolve end = ⊤` and +`resolve begin = L`, so the cell has the same compatible databases as the +sentinel-end read; in the second case the gap keys below `L` are necessary and +the range must start at `E`. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 3200000 + +/-- The first optional entry of a nonempty list is its head. -/ +theorem getElem?_zero_eq_head {l : List Key} (hne : l ≠ []) : + l[0]? = some (l.head hne) := by + cases l with + | nil => exact absurd rfl hne + | cons a t => simp + +/-! ### The cell and its closed form -/ + +/-- The sentinel-begin large-gap top subcell of the reversed truncated residual. -/ +abbrev negCrossTruncRevResidualTopLargeSentinel (r : RangeRead) (res : Result) : Prop := + negCrossTruncRevResidualTopLarge r res ∧ r.begin.key = (⊤ : WithTop Key) + +/-- The tightness condition of the sentinel-begin large-gap top cell. -/ +noncomputable def sentinelLargeTight (r : RangeRead) (res : Result) : Prop := + let L : WithTop Key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + ((r.end_.offset - 1).toNat : ℕ∞) = intervalCard r.end_.key L + (res.keys.length : ℕ∞) ∧ + (res.keys.length : Int) + r.begin.offset = 1 + +/-- The closed form of the sentinel-begin large-gap top cell. -/ +noncomputable def closedFormNegCrossTruncRevResidualTopLargeSentinel + (r : RangeRead) (res : Result) : ConflictRange := by + classical + let L : WithTop Key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + exact if sentinelLargeTight r res then ⟨L, ⊤⟩ else ⟨r.end_.key, ⊤⟩ + +/-! ### Converting the tight condition to natural numbers -/ + +/-- The tight `ℕ∞` identity, read in `ℕ`. -/ +theorem tight_nat {r : RangeRead} {res : Result} {L : Key} + (htight : ((r.end_.offset - 1).toNat : ℕ∞) = + intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞)) : + (r.end_.offset - 1).toNat = + (intervalCard r.end_.key ((L : Key) : WithTop Key)).toNat + res.keys.length := by + have hcardtop : intervalCard r.end_.key ((L : Key) : WithTop Key) ≠ ⊤ := by + intro h + rw [h] at htight + have htop : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) = ⊤ := by + rw [htight]; simp + exact ENat.natCast_ne_top _ htop + have h := congrArg ENat.toNat htight + rw [ENat.toNat_natCast, ENat.toNat_add hcardtop (ENat.natCast_ne_top _), + ENat.toNat_natCast] at h + exact h + +/-! ### The tight counting core -/ + +/-- **The end resolution is the sentinel in the tight compatible cell.** + +Every compatible database in the tight cell resolves its end selector to `⊤`. +If the end resolution were a genuine key, the stored keys in `[L, resolve end)` +would be exactly the reported keys, so the stored keys in `[E, L)` would be the +whole interval `card = e - t`; the remaining stored keys above `resolve end` +would then shift the begin anchor, because `begin.key = ⊤`, past the reported +last, contradicting that the reported last lies in the complete keys. -/ +theorem resolve_end_eq_top_of_tight_forward {db : DB} {r : RangeRead} {res : Result} + {L : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hL : res.keys.getLast hne = L) (hbtop : r.begin.key = (⊤ : WithTop Key)) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((L : Key) : WithTop Key)) + (hoff : (res.keys.length : Int) + r.begin.offset = 1) : + resolve db r.end_ = (⊤ : WithTop Key) := by + classical + have htpos : 0 < res.keys.length := List.length_pos_iff.mpr hne + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by + have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + simpa [hL] using h + have hLmem : L ∈ res.keys := hL ▸ List.getLast_mem hne + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLck : L ∈ RangeRead.completeKeys db r := by + have h := hLmem + rw [← hfil] at h + exact (List.mem_filter.mp h).1 + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLck).2.1 + obtain ⟨j, hjlt, hjeq⟩ := List.mem_iff_getElem.mp hLdb + have hcntL : cnt db ((L : Key) : WithTop Key) = j := by + rw [cnt_coe] + exact countP_lt_eq_of_getElem db.sorted hjlt hjeq + by_contra hne_top + have hbetween_eq : between db ((L : Key) : WithTop Key) (resolve db r.end_) = + res.keys.reverse := by + have h := res_keys_eq_reverse_between_resolve_end (db := db) (r := r) hcomp hmore hrev hne + rw [hL] at h + simpa [List.reverse_reverse] using (congrArg List.reverse h).symm + have hlenL : (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length = + res.keys.length := by + rw [hbetween_eq, List.length_reverse] + have hlenE : (between db r.end_.key (resolve db r.end_)).length = + (r.end_.offset - 1).toNat := + length_between_end_resolve_eq_of_ne_top (db := db) (r := r) hend hne_top + have hLre : ((L : Key) : WithTop Key) < resolve db r.end_ := by + have hpos : 0 < (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length := by + rw [hlenL]; exact htpos + obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ (List.length_pos_iff.mp hpos) + have hk' := List.mem_filter.mp hk + rcases (of_decide_eq_true hk'.2) with ⟨hLk, hkre⟩ + exact lt_of_le_of_lt hLk hkre + have hEre : r.end_.key ≤ resolve db r.end_ := + le_of_lt (lt_trans hEL hLre) + have hcnt1 : cnt db (resolve db r.end_) = + cnt db ((L : Key) : WithTop Key) + + (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length := + cnt_eq_add_between db (le_of_lt hLre) + have hcnt2 : cnt db ((L : Key) : WithTop Key) = + cnt db r.end_.key + (between db r.end_.key ((L : Key) : WithTop Key)).length := + cnt_eq_add_between db (le_of_lt hEL) + have hcnt3 : cnt db (resolve db r.end_) = + cnt db r.end_.key + (between db r.end_.key (resolve db r.end_)).length := + cnt_eq_add_between db hEre + have hcntTop1 : cnt db (⊤ : WithTop Key) = + cnt db (resolve db r.end_) + + (between db (resolve db r.end_) (⊤ : WithTop Key)).length := + cnt_eq_add_between db le_top + have hanchor : r.begin.anchor db = (cnt db (⊤ : WithTop Key) : Int) + r.begin.offset - 1 := by + change (cnt db r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hbtop] + have hanchor_eq : r.begin.anchor db = + (cnt db ((L : Key) : WithTop Key) : Int) + + ((between db (resolve db r.end_) (⊤ : WithTop Key)).length : Int) := by + rw [hanchor] + have hN : cnt db (⊤ : WithTop Key) = + cnt db ((L : Key) : WithTop Key) + res.keys.length + + (between db (resolve db r.end_) (⊤ : WithTop Key)).length := by + have := hcntTop1 + rw [hcnt1, hlenL] at this + exact this + rw [hN] + have hoff' : r.begin.offset = 1 - (res.keys.length : Int) := by linarith + rw [hoff'] + push_cast + ring + have hanchor_nonneg : 0 ≤ r.begin.anchor db := by + rw [hanchor_eq] + have h1 : (0 : Int) ≤ (cnt db ((L : Key) : WithTop Key) : Int) := by + exact_mod_cast Nat.zero_le _ + have h2 : (0 : Int) ≤ ((between db (resolve db r.end_) (⊤ : WithTop Key)).length : Int) := by + exact_mod_cast Nat.zero_le _ + linarith + have hanchor_le : r.begin.anchor db ≤ (cnt db ((L : Key) : WithTop Key) : Int) := by + have hj := (resolve_le_getElem_iff (db := db) (s := r.begin) hjlt).mp (by rw [hjeq]; exact hrb) + have htoNat : (r.begin.anchor db).toNat ≤ j := by + unfold clampIndex at hj + by_cases h : (r.begin.anchor db).toNat ≤ db.keys.length + · rwa [Nat.min_eq_left h] at hj + · rw [Nat.min_eq_right (by omega)] at hj; omega + have hanchor_le_j : r.begin.anchor db ≤ (j : Int) := by + have h1 : r.begin.anchor db ≤ ((r.begin.anchor db).toNat : Int) := by + rw [Int.toNat_of_nonneg hanchor_nonneg] + have h2 : ((r.begin.anchor db).toNat : Int) ≤ (j : Int) := by exact_mod_cast htoNat + linarith + rwa [hcntL] + have hzpos : 0 < (between db (resolve db r.end_) (⊤ : WithTop Key)).length := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hnil | htop | ⟨q, hq, hqeq⟩ + · rw [hnil] + have hbw : between db (([] : Key) : WithTop Key) (⊤ : WithTop Key) = db.keys := by + unfold between + rw [List.filter_eq_self] + intro k _ + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), WithTop.coe_lt_top k⟩ + rw [hbw] + obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil _ hne + have hxdb : x ∈ db.keys := RangeRead.compatible_keys_subset hcomp (by simpa using hx) + rw [List.length_pos_iff] + exact fun hnil' => by rw [hnil'] at hxdb; exact absurd hxdb (by simp) + · exact absurd htop hne_top + · rw [hqeq] + have hmem : q ∈ between db ((q : Key) : WithTop Key) (⊤ : WithTop Key) := by + unfold between + rw [List.mem_filter] + exact ⟨hq, decide_eq_true ⟨le_refl _, WithTop.coe_lt_top q⟩⟩ + rw [List.length_pos_iff] + exact fun hnil' => by rw [hnil'] at hmem; exact absurd hmem (by simp) + rw [hanchor_eq] at hanchor_le + have hz0 : (between db (resolve db r.end_) (⊤ : WithTop Key)).length = 0 := by + have : ((between db (resolve db r.end_) (⊤ : WithTop Key)).length : Int) ≤ 0 := by linarith + omega + omega + +/-! ### The end resolution from agreement above the reported last -/ + +/-- **The end resolution is the sentinel in the tight cell, from keys at or above the +reported last.** If every stored key at or above the reported last is one of the +reported keys then, under tightness, the end selector overshoots: the whole gap +`[end_.key, last)` holds at most `card` keys, so the keys at or above `end_.key` number +at most `card + #reported = end_.offset - 1`, exactly the capacity of the end anchor. -/ +theorem resolve_end_eq_top_of_above {db : DB} {r : RangeRead} {res : Result} {L : Key} + (habove : above db ((L : Key) : WithTop Key) = res.keys.reverse) + (_hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((L : Key) : WithTop Key)) + (htight : ((r.end_.offset - 1).toNat : ℕ∞) = + intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞)) : + resolve db r.end_ = (⊤ : WithTop Key) := by + have hnat := tight_nat htight + have hcardtop : intervalCard r.end_.key ((L : Key) : WithTop Key) ≠ ⊤ := by + intro h + rw [h] at htight + have htop : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) = ⊤ := by + rw [htight]; simp + exact ENat.natCast_ne_top _ htop + -- `between L ⊤` is `above L` + have hbetweenTop : between db ((L : Key) : WithTop Key) (⊤ : WithTop Key) = + above db ((L : Key) : WithTop Key) := by + unfold between above + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact and_iff_left (WithTop.coe_lt_top k) + -- the total key count + have hcnt : cnt db (⊤ : WithTop Key) = + cnt db r.end_.key + (between db r.end_.key ((L : Key) : WithTop Key)).length + + res.keys.length := by + have h1 := cnt_eq_add_between db (le_of_lt hEL : + r.end_.key ≤ ((L : Key) : WithTop Key)) + have h2 := cnt_eq_add_between (db := db) + (w₁ := ((L : Key) : WithTop Key)) (w₂ := (⊤ : WithTop Key)) le_top + rw [hbetweenTop, habove, List.length_reverse] at h2 + rw [h1] at h2 + omega + -- the gap holds at most `card` keys + have hgaple : (between db r.end_.key ((L : Key) : WithTop Key)).length ≤ + (intervalCard r.end_.key ((L : Key) : WithTop Key)).toNat := by + have h := between_length_le_intervalCard (db := db) (E := r.end_.key) + (B := ((L : Key) : WithTop Key)) (le_of_lt hEL) + rw [← ENat.natCast_toNat hcardtop] at h + exact ENat.natCast_le_natCast.mp h + -- the anchor of the end selector + have hoff : ((r.end_.offset - 1).toNat : Int) = r.end_.offset - 1 := + Int.toNat_of_nonneg (by omega) + have hanchor : r.end_.anchor db = + ((cnt db r.end_.key + (r.end_.offset - 1).toNat : ℕ) : Int) := by + unfold Selector.anchor cnt + rw [Nat.cast_add, hoff] + ring + have hanchor_nonneg : 0 ≤ r.end_.anchor db := by + rw [hanchor]; exact_mod_cast Nat.zero_le _ + have hanchor_toNat : (r.end_.anchor db).toNat = + cnt db r.end_.key + (r.end_.offset - 1).toNat := by + rw [hanchor, Int.toNat_natCast] + refine resolve_eq_top hanchor_nonneg ?_ + rw [hanchor_toNat, ← cnt_top db, hcnt] + omega + +/-! ### Recovering agreement above the reported last -/ + +/-- **A compatible database sees exactly the reported keys above the last.** When the +end selector resolves to the sentinel, the complete keys above the reported last are all +stored keys above it, and compatibility identifies them with the reported keys. -/ +theorem above_eq_reverse_of_compat_end_top {db : DB} {r : RangeRead} {res : Result} {L : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hL : res.keys.getLast hne = L) + (hre : resolve db r.end_ = (⊤ : WithTop Key)) : + above db ((L : Key) : WithTop Key) = res.keys.reverse := by + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by + have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + simpa [hL] using h + have hLmem : L ∈ res.keys := hL ▸ List.getLast_mem hne + have hLck : L ∈ RangeRead.completeKeys db r := by + have h := hLmem + rw [← hfil] at h + exact (List.mem_filter.mp h).1 + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLck).2.1 + have h := completeKeys_filter_ge_eq_of_resolve_le (db := db) (r := r) (last := L) + hrev hre hrb + rw [hfil] at h + have hfilUp : db.keys.filter (fun k : Key => decide (L ≤ k)) = + above db ((L : Key) : WithTop Key) := by + rw [above_eq_filter] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact WithTop.coe_le_coe.symm + rw [hfilUp] at h + have h2 : res.keys.reverse = above db ((L : Key) : WithTop Key) := by + rw [h, List.reverse_reverse] + exact h2.symm + +/-! ### Reducing the tight cell to the sentinel-end read -/ + +/-- **In the tight cell the end selector is equivalent to the sentinel.** Replacing the +end selector by `⟨⊤, 1⟩` does not change the compatible databases: forward, `t + off = 1` +forces the end resolution to the sentinel; backward, agreement with the reported keys +above the last plus tightness does the same. -/ +theorem compatible_iff_sentinelEnd_of_tight {r : RangeRead} {res : Result} {L : Key} + (hmore : res.more = true) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hL : res.keys.getLast hne = L) (hbtop : r.begin.key = (⊤ : WithTop Key)) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((L : Key) : WithTop Key)) + (hoff : (res.keys.length : Int) + r.begin.offset = 1) + (htight : ((r.end_.offset - 1).toNat : ℕ∞) = + intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞)) : + ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db { r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } res := by + intro db + have htopEnd : resolve db ({ r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } : RangeRead).end_ = + (⊤ : WithTop Key) := resolve_end_eq_top_of_key_top rfl (by norm_num) + have hck : resolve db r.end_ = (⊤ : WithTop Key) → + RangeRead.completeKeys db r = + RangeRead.completeKeys db { r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } := by + intro hre + unfold RangeRead.completeKeys RangeRead.inRange + rw [hre, htopEnd] + constructor + · intro hc + have hre := resolve_end_eq_top_of_tight_forward hc hmore hrev hne hL hbtop hend hEL hoff + have hckeq := hck hre + unfold RangeRead.Compatible at hc ⊢ + rw [hckeq] at hc + exact hc + · intro hc + have habove := above_eq_reverse_of_compat_end_top hc hmore hrev hne hL htopEnd + have hre := resolve_end_eq_top_of_above (db := db) habove hend hEL htight + have hckeq := hck hre + unfold RangeRead.Compatible at hc ⊢ + rw [← hckeq] at hc + exact hc + +/-- **The tight sentinel-begin large-gap top cell is minimal with range `[last, ⊤)`.** -/ +theorem minimal_topOfLast_of_sentinel_large_tight {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hbtop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) + (hoff : (res.keys.length : Int) + r.begin.offset = 1) + (htight : ((r.end_.offset - 1).toNat : ℕ∞) = + intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞)) : + Minimal r res (topOfLast (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne + set r' : RangeRead := { r with end_ := ⟨(⊤ : WithTop Key), 1⟩ } with hr' + have hiff := compatible_iff_sentinelEnd_of_tight (r := r) (res := res) (L := L) + hmore hrev hne rfl hbtop hend hEL hoff htight + have hread' : Readable r' res := by + obtain ⟨db, hc⟩ := hread + exact ⟨db, (hiff db).mp hc⟩ + have hbeg' : r'.begin.offset < 0 := hbeg + have hmin' : Minimal r' res (topOfLast (res.keys.getLast hne)) := + minimal_topOfLast_of_rev_negTrunc_top_nocross hread' hmore hrev hne hbeg' rfl (by norm_num) + exact (minimal_congr_of_compatible_iff (r := r) (r' := r') hiff).mpr hmin' + +/-! ### The offset bound in the sentinel-begin cell -/ + +/-- **The reported length and begin offset cannot both be large.** In a compatible +sentinel-begin truncated result the begin anchor `N + offset - 1` lands at or below the +reported last, and only `N - index(last)` keys are at or above that last, so +`#reported + offset ≤ 1`. -/ +theorem sentinel_begin_offset_bound {r : RangeRead} {res : Result} {db : DB} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hbtop : r.begin.key = (⊤ : WithTop Key)) : + (res.keys.length : Int) + r.begin.offset ≤ 1 := by + classical + set L : Key := res.keys.getLast hne with hL + set t : Nat := res.keys.length with ht + set N : Nat := db.keys.length with hN + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by + have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + simpa [hL] using h + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hLck : L ∈ RangeRead.completeKeys db r := by + have h := hLmem + rw [← hfil] at h + exact (List.mem_filter.mp h).1 + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLck).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + obtain ⟨j, hjlt, hjeq⟩ := List.mem_iff_getElem.mp hLdb + have hcntL : cnt db ((L : Key) : WithTop Key) = j := by + rw [cnt_coe] + exact countP_lt_eq_of_getElem db.sorted hjlt hjeq + -- the reported keys fit inside the database + have htN : t ≤ N := by + have hpre := RangeRead.compatible_keys_prefix hcomp + refine le_trans hpre.length_le ?_ + rw [(RangeRead.completeKeys_perm_inRange db r).length_eq] + exact List.length_filter_le _ _ + -- the number of stored keys at or above the last + have haboveLen : (above db ((L : Key) : WithTop Key)).length = N - j := by + have hbT : between db ((L : Key) : WithTop Key) (⊤ : WithTop Key) = + above db ((L : Key) : WithTop Key) := by + unfold between above + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact and_iff_left (WithTop.coe_lt_top k) + have h := cnt_eq_add_between (db := db) + (w₁ := ((L : Key) : WithTop Key)) (w₂ := (⊤ : WithTop Key)) le_top + rw [hbT, cnt_top, ← hN, hcntL] at h + omega + -- the reported keys are a subset of the keys at or above the last + have hsubAbove : res.keys ⊆ above db ((L : Key) : WithTop Key) := by + intro k hk + rw [mem_above] + refine ⟨RangeRead.compatible_keys_subset hcomp hk, ?_⟩ + have hsort := RangeRead.compatible_sortedGT hcomp hrev + have hLk : L ≤ k := (hsort.pairwise.imp le_of_lt).rel_getLast hk + exact WithTop.coe_le_coe.mpr hLk + have htNj : t ≤ N - j := by + have hnd : res.keys.Nodup := by + rw [← hfil] + exact (RangeRead.completeKeys_nodup db r).filter _ + have := (List.subperm_of_subset (l₁ := res.keys) + (l₂ := above db ((L : Key) : WithTop Key)) hnd hsubAbove).length_le + rw [haboveLen] at this + exact this + have hanchor : r.begin.anchor db = (N : Int) + r.begin.offset - 1 := by + change (cnt db r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hbtop, cnt_top] + by_cases hneg : r.begin.anchor db < 0 + · -- the anchor is negative: `offset < 1 - N` + have : (t : Int) + r.begin.offset ≤ 1 := by + have htN' : (t : Int) ≤ (N : Int) := by exact_mod_cast htN + have hoff : (r.begin.offset : Int) < 1 - N := by + rw [hanchor] at hneg; linarith + linarith + exact this + · -- the anchor is nonnegative: it lands at or below `last` + have hnonneg : 0 ≤ r.begin.anchor db := le_of_not_gt hneg + have hclamp : clampIndex db r.begin ≤ j := + (resolve_le_getElem_iff (db := db) (s := r.begin) hjlt).mp (by rw [hjeq]; exact hrb) + have htoNat_le : (r.begin.anchor db).toNat ≤ j := by + unfold clampIndex at hclamp + by_cases h : (r.begin.anchor db).toNat ≤ N + · rwa [Nat.min_eq_left h] at hclamp + · rw [Nat.min_eq_right (by omega)] at hclamp; omega + have hanchor_le_j : r.begin.anchor db ≤ (j : Int) := by + have h1 : r.begin.anchor db = ((r.begin.anchor db).toNat : Int) := + (Int.toNat_of_nonneg hnonneg).symm + rw [h1] + exact_mod_cast htoNat_le + have hoff : (r.begin.offset : Int) ≤ (j : Int) - N + 1 := by + rw [hanchor] at hanchor_le_j; linarith + have htNj' : (t : Int) ≤ (N : Int) - (j : Int) := by + have h1 : (t : Int) ≤ ((N - j : Nat) : Int) := by exact_mod_cast htNj + have h2 : ((N - j : Nat) : Int) = (N : Int) - (j : Int) := by + rw [Nat.cast_sub (le_of_lt hjlt)] + rwa [h2] at h1 + linarith + +/-! ### Gap-key necessity in regime A -/ + +/-- **Gap-key necessity in the sentinel-begin large-gap top cell, regime A.** + +When `#reported + begin.offset = 1` and the gap is not tight, the witness stores `D = e - t` +gap keys in `[end_.key, last)` avoiding the target, then the reversed reported keys. Its +begin anchor is exactly `D`, so it resolves onto the reported last, and its end resolves to +the sentinel. Inserting the target raises the end resolution onto the reported head, +which leaves the complete keys and breaks the reported prefix. -/ +theorem necessary_gap_of_sentinel_large_A {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hbtop : r.begin.key = (⊤ : WithTop Key)) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) + (ht : res.keys.length < (r.end_.offset - 1).toNat) + (hstrict : ((r.end_.offset - 1).toNat : ℕ∞) < + intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞)) + (hoff : (res.keys.length : Int) + r.begin.offset = 1) + {k : Key} (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + Necessary r res k := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + set E : WithTop Key := r.end_.key with hE + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set e : Nat := (r.end_.offset - 1).toNat with he + set t : Nat := res.keys.length with ht' + set D : Nat := e - t with hD + have htn : 0 < t := List.length_pos_of_ne_nil hne + have ht_lt : t < e := by simpa [t, e] using ht + have hDt : D + t = e := by omega + have hD1 : 1 ≤ D := by omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne + have hEL' : E < ((L : Key) : WithTop Key) := by simpa [E, L] using hEL + have hkL' : ((k : Key) : WithTop Key) < ((L : Key) : WithTop Key) := hkL + -- room for `D + 1` gap keys avoiding the target + have hD_lt_card : (D : ℕ∞) < intervalCard E ((L : Key) : WithTop Key) := by + have hstrict' : (e : ℕ∞) < intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := by + simpa [E, L, e, t] using hstrict + have hcast : ((D : ℕ∞) + (t : ℕ∞)) = (e : ℕ∞) := by + rw [← Nat.cast_add, hDt] + rw [← hcast] at hstrict' + exact (ENat.add_lt_add_iff_right (ENat.natCast_ne_top t)).mp hstrict' + have hroom : (((D + 1 : ℕ) : ℕ∞)) ≤ intervalCard E ((L : Key) : WithTop Key) := by + have := (ENat.add_one_le_iff (m := (D : ℕ∞)) + (n := intervalCard E ((L : Key) : WithTop Key)) (ENat.natCast_ne_top D)).mpr hD_lt_card + simpa using this + obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ := + exists_db_keysIn_avoid (B := E) (E := ((L : Key) : WithTop Key)) (k := k) (m := D) hroom + set G : List Key := pad.keys with hGdef + have hGmem : ∀ x ∈ G, E ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((L : Key) : WithTop Key) := by + intro x hx + simpa only [G, keysIn, Set.mem_ofPred_eq] using hpadIn x (by simpa [hGdef] using hx) + have hGsort : G.SortedLT := by rw [hGdef]; exact pad.sorted + have hGlen : G.length = D := by rw [hGdef]; exact hpadlen + have hkG : k ∉ G := by rw [hGdef]; exact hkpad + -- the reported keys, ascending + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + set R : List Key := res.keys.reverse with hR + have hRsort : R.SortedLT := by rw [hR]; exact hsort.reverse + have hRmem : ∀ y ∈ R, L ≤ y := by + intro y hy + rw [hR] at hy + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_reverse.mp hy) + have hRleH : ∀ y ∈ R, y ≤ H := by + intro y hy + rw [hR] at hy + exact (hsort.pairwise.imp le_of_lt).rel_head (List.mem_reverse.mp hy) + have hLleH : L ≤ H := hRmem H (by rw [hR, List.mem_reverse]; exact hHmem) + have hGR : ∀ x ∈ G, ∀ y ∈ R, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr (hRmem y hy))) + set T : List Key := G ++ R with hT + have hTsort : T.SortedLT := by + rw [hT] + exact List.sortedLT_append.mpr ⟨hGsort, hRsort, hGR⟩ + let db_a : DB := ⟨T, hTsort⟩ + have hfullKeys : db_a.keys = T := rfl + have hTlen : T.length = e := by + rw [hT, List.length_append, hGlen, hR, List.length_reverse, ← ht'] + omega + have hlen_a : db_a.keys.length = e := by rw [hfullKeys, hTlen] + have hcnt0 : cnt db_a E = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hfullKeys, hT] at hx + rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x hx).1 + · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) + have hanchorEnd : (r.end_.anchor db_a).toNat = e := by + have hanc : r.end_.anchor db_a = r.end_.offset - 1 := by + unfold Selector.anchor + rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) db_a.keys = 0 from by + rw [← hE]; exact hcnt0] + ring + rw [hanc] + have hre_a : resolve db_a r.end_ = (⊤ : WithTop Key) := by + refine resolve_eq_top (by + change 0 ≤ (cnt db_a r.end_.key : Int) + r.end_.offset - 1 + rw [show cnt db_a r.end_.key = 0 from by simpa [hE] using hcnt0] + push_cast; omega) ?_ + rw [hanchorEnd, hlen_a] + have hoff' : r.begin.offset = 1 - (t : Int) := by linarith + have hanchorBegin : r.begin.anchor db_a = (D : Int) := by + change (cnt db_a r.begin.key : Int) + r.begin.offset - 1 = (D : Int) + have hecast : (e : Int) = (D : Int) + (t : Int) := by exact_mod_cast hDt.symm + rw [hbtop, cnt_top, hlen_a, hecast, hoff'] + ring + have hDltN : D < db_a.keys.length := by rw [hlen_a]; omega + have hgetD : db_a.keys[D] = L := by + have h1 : db_a.keys[D]? = some L := by + rw [hfullKeys, hT] + rw [List.getElem?_append_right (show G.length ≤ D by rw [hGlen]), hGlen, Nat.sub_self] + rw [hR, ← List.head?_eq_getElem?, List.head?_reverse, List.getLast?_eq_some_getLast hne, hL] + rw [List.getElem?_eq_getElem hDltN] at h1 + exact Option.some.inj h1 + have hrb_a : resolve db_a r.begin = ((L : Key) : WithTop Key) := by + have h0 : 0 ≤ r.begin.anchor db_a := by rw [hanchorBegin]; exact_mod_cast Nat.zero_le _ + have hlt : (r.begin.anchor db_a).toNat < db_a.keys.length := by + rw [hanchorBegin]; exact_mod_cast hDltN + rw [resolve_eq_getElem h0 hlt] + rw [WithTop.coe_eq_coe] + have hidx : (r.begin.anchor db_a).toNat = D := by rw [hanchorBegin]; rfl + have h1 : db_a.keys[(r.begin.anchor db_a).toNat]? = some L := by + have h2 : db_a.keys[(r.begin.anchor db_a).toNat]? = db_a.keys[D]? := by rw [hidx] + rw [h2, List.getElem?_eq_getElem hDltN] + exact congrArg some hgetD + exact Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans h1) + have hfilter_a : db_a.keys.filter (fun x : Key => decide (L ≤ x)) = R := by + rw [hfullKeys, hT, List.filter_append] + have hGnil : List.filter (fun x : Key => decide (L ≤ x)) G = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hlx + exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx)) + (not_le.mpr (hGmem x hx).2) + have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hRmem x hx) + rw [hGnil, hRself, List.nil_append] + have hcomp_a : RangeRead.Compatible db_a r res := by + refine (compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mpr + ⟨hcomp₀.1, ?_⟩ + rw [completeKeys_filter_ge_eq_resolve_end (db := db_a) (r := r) (last := L) hrev + (le_of_eq hrb_a), hfilter_a, hR, hre_a] + simp + have hk_a : k ∉ db_a.keys := by + rw [hfullKeys, hT] + intro hmem + rcases List.mem_append.mp hmem with hx | hx + · exact hkG hx + · exact absurd (WithTop.coe_le_coe.mpr (hRmem k hx)) (not_le.mpr hkL') + -- insert the target + let db_b : DB := db_a.insert k hk_a + have hkR : ∀ x ∈ R, k < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hkL' (WithTop.coe_le_coe.mpr (hRmem x hx))) + have hins : T.orderedInsert (· ≤ ·) k = G.orderedInsert (· ≤ ·) k ++ R := by + rw [hT] + exact orderedInsert_append_of_forall_lt hkR + have hb_keys : db_b.keys = G.orderedInsert (· ≤ ·) k ++ R := by + rw [show db_b.keys = db_a.keys.orderedInsert (· ≤ ·) k from rfl, hfullKeys, hins] + have hb_len : db_b.keys.length = e + 1 := by + rw [hb_keys, List.length_append, List.orderedInsert_length, hGlen, hR, + List.length_reverse, ← ht'] + omega + have hb_cnt0 : cnt db_b E = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hx | hx + · rw [List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact hEk + · exact (hGmem x hx).1 + · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) + have hancEnd_b : (r.end_.anchor db_b).toNat = e := by + have hanc : r.end_.anchor db_b = r.end_.offset - 1 := by + unfold Selector.anchor + rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) db_b.keys = 0 from by + rw [← hE]; exact hb_cnt0] + ring + rw [hanc] + have hre_b : resolve db_b r.end_ = ((H : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db_b := by + change 0 ≤ (cnt db_b r.end_.key : Int) + r.end_.offset - 1 + rw [show cnt db_b r.end_.key = 0 from by simpa [hE] using hb_cnt0] + push_cast; omega + have hlt : (r.end_.anchor db_b).toNat < db_b.keys.length := by + rw [hancEnd_b, hb_len]; omega + rw [resolve_eq_getElem h0 hlt, WithTop.coe_eq_coe] + have hidx : (r.end_.anchor db_b).toNat = e := hancEnd_b + have hG'len : (G.orderedInsert (· ≤ ·) k).length = D + 1 := by + rw [List.orderedInsert_length, hGlen] + have he_ge : (G.orderedInsert (· ≤ ·) k).length ≤ e := by rw [hG'len]; omega + have h1 : db_b.keys[(r.end_.anchor db_b).toNat]? = some H := by + have h2 : db_b.keys[(r.end_.anchor db_b).toNat]? = db_b.keys[e]? := by rw [hidx] + rw [h2, hb_keys, List.getElem?_append_right he_ge] + have hsub : e - (G.orderedInsert (· ≤ ·) k).length = t - 1 := by + rw [hG'len, ← hDt]; omega + rw [hsub, hR] + rw [List.getElem?_reverse (by rw [← ht']; omega)] + rw [show res.keys.length - 1 - (t - 1) = 0 by rw [← ht']; omega] + have hh := getElem?_zero_eq_head (l := res.keys) hne + rwa [← hH] at hh + exact Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans h1) + have hHmem_b : H ∈ db_b.keys := by + rw [hb_keys] + exact List.mem_append_right _ (by rw [hR, List.mem_reverse]; exact hHmem) + refine necessary_of_toggle ⟨db_a, hcomp_a, ?_⟩ + rw [toggle_eq_insert_of_not_mem hk_a] + intro hcomp' + have hfil := (compatible_iff_truncated_keys_rev (db := db_b) hrev hmore hne).mp hcomp' |>.2 + have hHck : H ∈ RangeRead.completeKeys db_b r := by + have h := hHmem + rw [← hfil] at h + exact (List.mem_filter.mp h).1 + exact absurd (RangeRead.mem_completeKeys.mp hHck).2.2 (by rw [hre_b]; exact lt_irrefl _) + +/-! ### Gap-key necessity in regime B -/ + +/-- **Gap-key necessity in the sentinel-begin large-gap top cell, regime B.** + +When `#reported + begin.offset ≤ 0`, the witness stores `D = e - t` gap keys in +`[end_.key, last)` including the target, then the reversed reported keys, then a key +above the reported head. With the target present the end selector resolves onto the +last witness key, so the keys above the reported last are exactly the reported ones. +Erasing the target lowers the database length by one; the end anchor now overshoots and +the witness key above the head enters the complete window, breaking the reported +prefix. -/ +theorem necessary_gap_of_sentinel_large_B {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hbtop : r.begin.key = (⊤ : WithTop Key)) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) + (htg : res.keys.length < (r.end_.offset - 1).toNat) + (hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞)) + (hoff : (res.keys.length : Int) + r.begin.offset ≤ 0) + {k : Key} (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + Necessary r res k := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + set E : WithTop Key := r.end_.key with hE + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set e : Nat := (r.end_.offset - 1).toNat with he + set t : Nat := res.keys.length with ht' + set D : Nat := e - t with hD + have htn : 0 < t := List.length_pos_of_ne_nil hne + have ht_lt : t < e := by simpa [t, e] using htg + have hDt : D + t = e := by omega + have hD1 : 1 ≤ D := by omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne + have hEL' : E < ((L : Key) : WithTop Key) := by simpa [E, L] using hEL + have hkL' : ((k : Key) : WithTop Key) < ((L : Key) : WithTop Key) := hkL + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + set R : List Key := res.keys.reverse with hR + have hRsort : R.SortedLT := by rw [hR]; exact hsort.reverse + have hRmem : ∀ y ∈ R, L ≤ y := by + intro y hy + rw [hR] at hy + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_reverse.mp hy) + have hRleH : ∀ y ∈ R, y ≤ H := by + intro y hy + rw [hR] at hy + exact (hsort.pairwise.imp le_of_lt).rel_head (List.mem_reverse.mp hy) + have hLleH : L ≤ H := hRmem H (by rw [hR, List.mem_reverse]; exact hHmem) + -- room for `D` gap keys, one of them the target + have hcapD : (D : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by + have hgap' : (e : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := by + simpa [E, L, e, t] using hgap + have hcast : ((D : ℕ∞) + (t : ℕ∞)) = (e : ℕ∞) := by + rw [← Nat.cast_add, hDt] + rw [← hcast] at hgap' + exact (ENat.add_le_add_iff_right (ENat.natCast_ne_top t)).mp hgap' + have hroom : (((D - 1) + 1 : ℕ) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by + rw [Nat.sub_add_cancel hD1]; exact hcapD + obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ := + exists_db_keysIn_avoid (B := E) (E := ((L : Key) : WithTop Key)) (k := k) + (m := D - 1) hroom + set Gp : List Key := pad.keys with hGpdef + have hGpmem : ∀ x ∈ Gp, E ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((L : Key) : WithTop Key) := by + intro x hx + simpa only [Gp, keysIn, Set.mem_ofPred_eq] using hpadIn x (by simpa [hGpdef] using hx) + have hGpsort : Gp.SortedLT := by rw [hGpdef]; exact pad.sorted + have hGplen : Gp.length = D - 1 := by rw [hGpdef]; exact hpadlen + have hkGp : k ∉ Gp := by rw [hGpdef]; exact hkpad + -- insert the target into the gap keys + let dbG : DB := pad.insert k hkGp + set G : List Key := dbG.keys with hGdef + have hGsort : G.SortedLT := by rw [hGdef]; exact dbG.sorted + have hGins : G = Gp.orderedInsert (· ≤ ·) k := by + rw [hGdef]; exact DB.insert_keys pad k hkGp + have hGlen : G.length = D := by + rw [hGins, List.orderedInsert_length, hGplen]; omega + have hGmem : ∀ x ∈ G, E ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((L : Key) : WithTop Key) := by + intro x hx + rw [hGins, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact ⟨hEk, hkL'⟩ + · exact hGpmem x hx + have hkG : k ∈ G := by + rw [hGins]; exact (List.mem_orderedInsert (· ≤ ·)).mpr (Or.inl rfl) + -- a key above the reported head + set Q : Key := keyAfter H with hQ + have hHQ : H < Q := by rw [hQ]; exact keyAfter_self_lt H + have hRQ : ∀ y ∈ R, y < Q := fun y hy => lt_of_le_of_lt (hRleH y hy) hHQ + have hGQ : ∀ x ∈ G, x < Q := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hGmem x hx).2 + (WithTop.coe_le_coe.mpr (le_trans hLleH (le_of_lt hHQ)))) + have hGR : ∀ x ∈ G, ∀ y ∈ R, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr (hRmem y hy))) + -- assemble the witness database + set T : List Key := G ++ R ++ [Q] with hT + have hGRsort : (G ++ R).SortedLT := + List.sortedLT_append.mpr ⟨hGsort, hRsort, hGR⟩ + have hGRQ : ∀ x ∈ G ++ R, ∀ y ∈ [Q], x < y := by + intro x hx y hy + rw [List.mem_singleton] at hy + subst hy + rcases List.mem_append.mp hx with hx | hx + · exact hGQ x hx + · exact hRQ x hx + have hTsort : T.SortedLT := by + rw [hT] + exact List.sortedLT_append.mpr ⟨hGRsort, by simp [List.sortedLT_iff_pairwise], hGRQ⟩ + let db_a : DB := ⟨T, hTsort⟩ + have hfullKeys : db_a.keys = T := rfl + have hTlen : T.length = D + t + 1 := by + rw [hT] + simp [List.length_append, hGlen, hR, ht'] + omega + have hlen_a : db_a.keys.length = D + t + 1 := by rw [hfullKeys, hTlen] + have hcnt0 : cnt db_a E = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hfullKeys, hT] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x hx).1 + · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) + · rw [List.mem_singleton] at hx + subst hx + exact le_trans (le_of_lt hEL') + (WithTop.coe_le_coe.mpr (le_trans hLleH (le_of_lt hHQ))) + have hanchorEnd : (r.end_.anchor db_a).toNat = e := by + have hanc : r.end_.anchor db_a = r.end_.offset - 1 := by + unfold Selector.anchor + rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) + db_a.keys = 0 from by rw [← hE]; exact hcnt0] + ring + rw [hanc] + have hQidx : db_a.keys[e]? = some Q := by + rw [hfullKeys, hT] + rw [List.getElem?_append_right (show (G ++ R).length ≤ e by + rw [List.length_append, hGlen, hR, List.length_reverse]; omega)] + rw [List.length_append, hGlen, hR, List.length_reverse] + rw [show e - (D + res.keys.length) = 0 by omega] + rfl + have hre_a : resolve db_a r.end_ = ((Q : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db_a := by + change 0 ≤ (cnt db_a r.end_.key : Int) + r.end_.offset - 1 + rw [show cnt db_a r.end_.key = 0 from by simpa [hE] using hcnt0] + push_cast; omega + have hlt : (r.end_.anchor db_a).toNat < db_a.keys.length := by + rw [hanchorEnd, hlen_a]; omega + rw [resolve_eq_getElem h0 hlt] + rw [WithTop.coe_eq_coe] + have hidx : (r.end_.anchor db_a).toNat = e := hanchorEnd + have h1 : db_a.keys[(r.end_.anchor db_a).toNat]? = some Q := by + have h2 : db_a.keys[(r.end_.anchor db_a).toNat]? = db_a.keys[e]? := by rw [hidx] + rw [h2]; exact hQidx + exact Option.some.inj ((List.getElem?_eq_getElem hlt).symm.trans h1) + -- the reported last sits at index `D` + have hLidx : db_a.keys[D]? = some L := by + rw [hfullKeys, hT, List.append_assoc] + rw [List.getElem?_append_right (show G.length ≤ D by rw [hGlen])] + rw [hGlen, Nat.sub_self] + rw [List.getElem?_append_left (by rw [hR, List.length_reverse, ← ht']; omega)] + rw [hR, ← List.head?_eq_getElem?, List.head?_reverse, List.getLast?_eq_some_getLast hne, hL] + have hrb_a : resolve db_a r.begin ≤ ((L : Key) : WithTop Key) := by + have hDltN : D < db_a.keys.length := by rw [hlen_a]; omega + have hclamp : clampIndex db_a r.begin ≤ D := by + unfold clampIndex + have hanchor_le : r.begin.anchor db_a ≤ (D : Int) := by + change (cnt db_a r.begin.key : Int) + r.begin.offset - 1 ≤ (D : Int) + rw [hbtop, cnt_top, hlen_a] + have hcast : ((D + t + 1 : Nat) : Int) = (D : Int) + (t : Int) + 1 := by push_cast; ring + rw [hcast]; linarith + have htoNat : (r.begin.anchor db_a).toNat ≤ D := (Int.toNat_le).mpr hanchor_le + exact le_trans (Nat.min_le_left _ _) htoNat + have hget : db_a.keys[D] = L := by + rw [List.getElem?_eq_getElem hDltN] at hLidx + exact Option.some.inj hLidx + rw [← hget] + exact (resolve_le_getElem_iff (db := db_a) (s := r.begin) hDltN).mpr hclamp + have hfilter_a : db_a.keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by + rw [hfullKeys, hT, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide (L ≤ x)) G = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hlx + exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx)) + (not_le.mpr (hGmem x hx).2) + have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hRmem x hx) + have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by + simp [decide_eq_true (le_trans hLleH (le_of_lt hHQ))] + rw [hGnil, hRself, hQself] + simp + have hinner_a : (R ++ [Q]).filter + (fun x : Key => decide ((x : WithTop Key) < resolve db_a r.end_)) = R := by + rw [hre_a, List.filter_append] + have hRself : List.filter (fun x : Key => + decide ((x : WithTop Key) < ((Q : Key) : WithTop Key))) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (WithTop.coe_lt_coe.mpr (hRQ x hx)) + have hQnil : List.filter (fun x : Key => + decide ((x : WithTop Key) < ((Q : Key) : WithTop Key))) [Q] = [] := by + simp + rw [hRself, hQnil, List.append_nil] + have hcomp_a : RangeRead.Compatible db_a r res := by + refine (compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mpr + ⟨hcomp₀.1, ?_⟩ + rw [completeKeys_filter_ge_eq_resolve_end (db := db_a) (r := r) (last := L) hrev hrb_a, + hfilter_a, hinner_a, hR, List.reverse_reverse] + -- erase the target + have hk_a : k ∈ db_a.keys := by + rw [hfullKeys, hT] + exact List.mem_append_left _ (List.mem_append_left _ hkG) + let db_b : DB := db_a.erase k + have hb_keys : db_b.keys = G.erase k ++ R ++ [Q] := by + change (db_a.keys.erase k) = _ + rw [hfullKeys, hT, List.append_assoc, List.erase_append_left (R ++ [Q]) hkG, + List.append_assoc] + have hb_len : db_b.keys.length = D + t := by + change (db_a.keys.erase k).length = D + t + rw [List.length_erase_of_mem hk_a, hlen_a] + omega + have hb_cnt0 : cnt db_b E = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hb_keys] at hx + rcases List.mem_append.mp hx with hx | hx + · rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x (List.mem_of_mem_erase hx)).1 + · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) + · rw [List.mem_singleton] at hx + subst hx + exact le_trans (le_of_lt hEL') + (WithTop.coe_le_coe.mpr (le_trans hLleH (le_of_lt hHQ))) + have hanchorEnd_b : (r.end_.anchor db_b).toNat = e := by + have hanc : r.end_.anchor db_b = r.end_.offset - 1 := by + unfold Selector.anchor + rw [show List.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) + db_b.keys = 0 from by rw [← hE]; exact hb_cnt0] + ring + rw [hanc] + have hre_b : resolve db_b r.end_ = (⊤ : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db_b := by + change 0 ≤ (cnt db_b r.end_.key : Int) + r.end_.offset - 1 + rw [show cnt db_b r.end_.key = 0 from by simpa [hE] using hb_cnt0] + push_cast; omega + refine resolve_eq_top h0 ?_ + rw [hanchorEnd_b, hb_len] + exact le_of_eq hDt + have hEraseLen : (G.erase k).length = D - 1 := by + rw [List.length_erase_of_mem hkG, hGlen] + have hLidx_b : db_b.keys[D - 1]? = some L := by + rw [hb_keys, List.append_assoc] + rw [List.getElem?_append_right (show (G.erase k).length ≤ D - 1 by rw [hEraseLen])] + rw [hEraseLen, Nat.sub_self] + rw [List.getElem?_append_left (by rw [hR, List.length_reverse, ← ht']; omega)] + rw [hR, ← List.head?_eq_getElem?, List.head?_reverse, List.getLast?_eq_some_getLast hne, hL] + have hrb_b : resolve db_b r.begin ≤ ((L : Key) : WithTop Key) := by + have hjlt : D - 1 < db_b.keys.length := by rw [hb_len]; omega + have hget : db_b.keys[D - 1] = L := by + rw [List.getElem?_eq_getElem hjlt] at hLidx_b + exact Option.some.inj hLidx_b + rw [← hget] + apply (resolve_le_getElem_iff (db := db_b) (s := r.begin) hjlt).mpr + unfold clampIndex + have hanchor_le : r.begin.anchor db_b ≤ ((D - 1 : Nat) : Int) := by + change (cnt db_b r.begin.key : Int) + r.begin.offset - 1 ≤ ((D - 1 : Nat) : Int) + rw [hbtop, cnt_top, hb_len] + have hcast : ((D + t : Nat) : Int) = (D : Int) + (t : Int) := by push_cast; ring + rw [hcast] + have hcast2 : ((D - 1 : Nat) : Int) = (D : Int) - 1 := by + rw [Nat.cast_sub hD1]; norm_num + rw [hcast2] + linarith + have htoNat : (r.begin.anchor db_b).toNat ≤ D - 1 := (Int.toNat_le).mpr hanchor_le + exact le_trans (Nat.min_le_left _ _) htoNat + have hfilter_b : (RangeRead.completeKeys db_b r).filter + (fun k => decide (L ≤ k)) = Q :: res.keys := by + rw [completeKeys_filter_ge_eq_resolve_end (db := db_b) (r := r) (last := L) hrev hrb_b, + hre_b] + have hbF : db_b.keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by + rw [hb_keys, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide (L ≤ x)) (G.erase k) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hlx + exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx)) + (not_le.mpr (hGmem x (List.mem_of_mem_erase hx)).2) + have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hRmem x hx) + have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by + simp [decide_eq_true (le_trans hLleH (le_of_lt hHQ))] + rw [hGnil, hRself, hQself] + simp + rw [hbF] + have hinner : (R ++ [Q]).filter + (fun x : Key => decide ((x : WithTop Key) < (⊤ : WithTop Key))) = R ++ [Q] := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (WithTop.coe_lt_top x) + rw [hinner, List.reverse_append, List.reverse_singleton, hR] + simp + refine necessary_of_toggle ⟨db_a, hcomp_a, ?_⟩ + rw [toggle_eq_erase_of_mem hk_a] + intro hcomp' + have hfil := (compatible_iff_truncated_keys_rev (db := db_b) hrev hmore hne).mp hcomp' |>.2 + rw [hfilter_b] at hfil + have hlen_eq := congrArg List.length hfil + simp at hlen_eq + +/-! ### Assembling the sentinel-begin cell -/ + +/-- **The sentinel-begin large-gap top cell closed form is minimal.** When the cell is +tight the range is `[last, ⊤)`, with the compatible databases reduced to the sentinel-end +read. Otherwise it is `[end_.key, ⊤)`: soundness is +`sound_topRange_endKey_of_residual_large`; necessity splits `[end_.key, ⊤)` at the +reported last and head, using the gap-key witnesses of regime A or B (decided by the +offset bound `sentinel_begin_offset_bound`), convexity for reported keys, and the +above-head witness. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residual_top_large_sentinel + {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevResidualTopLargeSentinel r res) : + Minimal r res (closedFormNegCrossTruncRevResidualTopLargeSentinel r res) := by + obtain ⟨hlarge, hbtop⟩ := h + obtain ⟨hmore, hrev, hbeg, hne, hend, hELD, htopD, hgapD⟩ := hlarge + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + have hlastD : res.keys.getLastD ([] : Key) = L := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] + simp [hLdef] + have hEL : r.end_.key < ((L : Key) : WithTop Key) := by + rw [hlastD] at hELD + exact hELD + have hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by + rw [hlastD] at hgapD + exact hgapD + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx + rw [hLdef] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne + have hcountP : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + exact decide_eq_true (le_of_lt (lt_of_lt_of_le hEL (WithTop.coe_le_coe.mpr (hLle x hx)))) + have ht : res.keys.length < (r.end_.offset - 1).toNat := by + rw [hcountP] at htopD + exact htopD + by_cases htight : sentinelLargeTight r res + · -- tight branch: the range is `[last, ⊤)` + have htight' : ((r.end_.offset - 1).toNat : ℕ∞) = + intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) ∧ + (res.keys.length : Int) + r.begin.offset = 1 := by + simpa only [sentinelLargeTight, hlastD] using htight + have hmin := minimal_topOfLast_of_sentinel_large_tight hread' hmore hrev hbeg hne + hbtop hend (by simpa [hLdef] using hEL) htight'.2 htight'.1 + have hcf : closedFormNegCrossTruncRevResidualTopLargeSentinel r res = + topOfLast (res.keys.getLast hne) := by + unfold closedFormNegCrossTruncRevResidualTopLargeSentinel topOfLast + dsimp only + rw [if_pos htight] + simp [List.getLast?_eq_some_getLast hne] + rw [hcf] + exact hmin + · -- non-tight branch: the range is `[end_.key, ⊤)` + have hmin : Minimal r res ⟨r.end_.key, ⊤⟩ := by + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_topRange_endKey_of_residual_large hmore hrev hbeg hne hend hEL + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hEk, _⟩ := hk + by_cases hkL : (k : WithTop Key) < ((L : Key) : WithTop Key) + · -- a gap key below the reported last + have hbound : (res.keys.length : Int) + r.begin.offset ≤ 1 := + sentinel_begin_offset_bound hcomp₀ hmore hrev hne hbtop + rcases lt_or_eq_of_le hbound with hlt1 | heq + · -- regime B: both lengths cannot be large + have hB : (res.keys.length : Int) + r.begin.offset ≤ 0 := by omega + exact necessary_gap_of_sentinel_large_B hread' hmore hrev hbeg hne hbtop hend + (by simpa [hLdef] using hEL) ht hgap hB hEk hkL + · -- regime A: the gap is strict + have hstrict : ((r.end_.offset - 1).toNat : ℕ∞) < + intervalCard r.end_.key ((L : Key) : WithTop Key) + + (res.keys.length : ℕ∞) := by + have hnt' : ¬ (((r.end_.offset - 1).toNat : ℕ∞) = + intervalCard r.end_.key ((L : Key) : WithTop Key) + + (res.keys.length : ℕ∞) ∧ + (res.keys.length : Int) + r.begin.offset = 1) := by + intro hc + exact htight (by simpa only [sentinelLargeTight, hlastD] using hc) + have hle : ((r.end_.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key ((L : Key) : WithTop Key) + + (res.keys.length : ℕ∞) := hgap + exact lt_of_le_of_ne hle (fun he => hnt' ⟨he, heq⟩) + exact necessary_gap_of_sentinel_large_A hread' hmore hrev hbeg hne hbtop hend + (by simpa [hLdef] using hEL) ht hstrict heq hEk hkL + · -- a key at or above the reported last + have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hkL + have hkKey : L ≤ k := WithTop.coe_le_coe.mp hLk + by_cases hkH : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) + · exact necessary_of_between_result hread' hLmem hHmem hkKey + (WithTop.coe_le_coe.mp hkH) + · have hHk : H < k := WithTop.coe_lt_coe.mp (lt_of_not_ge hkH) + exact necessary_above_head_of_residual_top_large hread' hmore hrev hbeg hne + hend (by simpa [hLdef] using hEL) ht + (by rw [hbtop]; exact WithTop.coe_lt_top L) + (by simpa [hHdef] using hHk) + have hcf : closedFormNegCrossTruncRevResidualTopLargeSentinel r res = + ⟨r.end_.key, ⊤⟩ := by + unfold closedFormNegCrossTruncRevResidualTopLargeSentinel + dsimp only + rw [if_neg htight] + rw [hcf] + exact hmin + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevResidualLargeTop.lean b/FormalConflictRange/NegCrossTruncRevResidualLargeTop.lean new file mode 100644 index 0000000..7467bcf --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevResidualLargeTop.lean @@ -0,0 +1,846 @@ +/- +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.NegCrossTruncRevResidual + +/-! +# The large-gap crossed top cell of the reversed truncated residual + +This module closes the last *top-branch* subcell of the reversed crossed +negative-begin truncated residual. The setting is + +* `res.more = true`, `r.reverse = true`, `r.begin.offset < 0`, `res.keys ≠ []`; +* `2 ≤ r.end_.offset`; +* the end key lies strictly below the reported last (`r.end_.key < last`); +* the top branch holds (`#reported ≥ end_.key < end_.offset - 1`); +* the crossed gap is *large*: the interval `[end_.key, last)` has room for the + `end_.offset - 1 - #reported` keys that a genuine end resolution needs. + +In this cell the minimal range is `[end_.key, ⊤)`. The reported last cannot be the +begin of the range: a database can store extra gap keys below it, while inserting a key +at `end_.key` changes which stored key the end selector lands on and breaks the reported +prefix. So the range must start at or below `end_.key`. + +The soundness proof shows agreement on `[end_.key, ⊤)` fixes both resolutions: the end +selector agrees by `resolve_eq_of_agree_ge`, and the begin selector stays at or below the +reported last on both databases, so `completeKeys_filter_ge_eq_resolve_end` finishes. +-/ + +namespace FormalConflictRange + +set_option maxHeartbeats 1600000 + +/-- The large-gap crossed top subcell of the reversed truncated residual. -/ +abbrev negCrossTruncRevResidualTopLarge (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + 2 ≤ r.end_.offset ∧ + r.end_.key < ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) ∧ + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat ∧ + ((r.end_.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) + + (res.keys.length : ℕ∞) + +/-! ### Soundness of `[end_.key, ⊤)` -/ + +/-- **Soundness of the top range ending at the end key in the large-gap cell.** +Agreement on `[end_.key, ⊤)` fixes the end resolution and keeps the begin resolution at +or below the reported last, so the truncated complete keys are determined. -/ +theorem sound_topRange_endKey_of_residual_large {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) + (hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) : + Sound r res ⟨r.end_.key, ⊤⟩ := by + intro db₁ db₂ hagree + -- agreement at or above `end_.key` + have habove : above db₁ r.end_.key = above db₂ r.end_.key := by + have h := hagree + rw [AgreeOn] at h + rw [keys_top_eq_above, keys_top_eq_above] at h + exact h + -- agreement at or above the reported last (a sub-filter of `above end_.key`) + have haboveL : above db₁ ((res.keys.getLast hne : Key) : WithTop Key) = + above db₂ ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [above_B_eq_filter (db := db₁) (E := r.end_.key) + (B := ((res.keys.getLast hne : Key) : WithTop Key)) (le_of_lt hEL), + above_B_eq_filter (db := db₂) (E := r.end_.key) + (B := ((res.keys.getLast hne : Key) : WithTop Key)) (le_of_lt hEL), + habove] + -- the end selector agrees + have hresend : resolve db₁ r.end_ = resolve db₂ r.end_ := by + refine resolve_eq_of_agree_ge (db₁ := db₁) (db₂ := db₂) (s := r.end_) (by omega) ?_ + intro k hk + have h1 : k ∈ above db₁ r.end_.key ↔ k ∈ above db₂ r.end_.key := by rw [habove] + rw [mem_above, mem_above] at h1 + exact ⟨fun h => (h1.mp ⟨h, hk⟩).1, fun h => (h1.mpr ⟨h, hk⟩).1⟩ + -- a single forward step, used in both directions + have step : ∀ (db db' : DB), RangeRead.Compatible db r res → + resolve db r.end_ = resolve db' r.end_ → + above db ((res.keys.getLast hne : Key) : WithTop Key) = + above db' ((res.keys.getLast hne : Key) : WithTop Key) → + RangeRead.Compatible db' r res := by + intro db db' hc hrend habL + obtain ⟨hlim, hfil⟩ := (compatible_iff_truncated_keys_rev hrev hmore hne).mp hc + -- agreement on `[last, ⊤)` propagated to the key-level filter + have hl : db.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k)) = + db'.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k)) := by + have h := habL + simp only [above] at h + rw [show (fun k : Key => + decide (((res.keys.getLast hne : Key) : WithTop Key) ≤ (k : WithTop Key))) = + (fun k : Key => decide (res.keys.getLast hne ≤ k)) from by + funext k; simp [WithTop.coe_le_coe]] at h + exact h + have hLmem : (res.keys.getLast hne) ∈ db.keys := + RangeRead.compatible_keys_subset hc (List.getLast_mem hne) + have hrb : resolve db r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + by_cases hB : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key + · exact resolve_begin_le_of_between_bound hLmem hB + (between_add_offset_le_one hc hmore hrev hne hB) + · exact le_trans (resolve_le_key_of_offset_nonpos (db := db) (s := r.begin) (by omega)) + (le_of_not_gt hB) + have hLmem' : (res.keys.getLast hne) ∈ db'.keys := by + have h1 : (res.keys.getLast hne) ∈ above db ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [mem_above]; exact ⟨hLmem, le_refl _⟩ + rw [habL] at h1 + rw [mem_above] at h1 + exact h1.1 + have hrb' : resolve db' r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + by_cases hB : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key + · have hbeq : between db ((res.keys.getLast hne : Key) : WithTop Key) r.begin.key = + between db' ((res.keys.getLast hne : Key) : WithTop Key) r.begin.key := by + apply between_eq_of_agree + · unfold AgreeOn + rw [keys_top_eq_above, keys_top_eq_above] + exact habL + · exact le_of_lt hB + · exact le_top + have hbound := between_add_offset_le_one hc hmore hrev hne hB + rw [hbeq] at hbound + exact resolve_begin_le_of_between_bound hLmem' hB hbound + · exact le_trans (resolve_le_key_of_offset_nonpos (db := db') (s := r.begin) (by omega)) + (le_of_not_gt hB) + have hw := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) + (last := res.keys.getLast hne) hrev hrb + have hw' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) + (last := res.keys.getLast hne) hrev hrb' + have hinner : (db.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) = + (db'.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db' r.end_)) := by + rw [hl, hrend] + rw [compatible_iff_truncated_keys_rev hrev hmore hne] + refine ⟨hlim, ?_⟩ + rw [hw', ← hinner, ← hw] + exact hfil + constructor + · intro hc + exact step db₁ db₂ hc hresend haboveL + · intro hc + exact step db₂ db₁ hc hresend.symm haboveL.symm + +/-! ### Above-head necessity -/ + +/-- **Every key above the reported head is necessary in the large-gap crossed top +cell.** The database of reported keys (ascending) is compatible; inserting a key above +the reported head makes it the head of the reversed complete window, breaking the +reported prefix. -/ +theorem necessary_above_head_of_residual_top_large {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (_hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) + (ht : res.keys.length < (r.end_.offset - 1).toNat) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + {k : Key} (hkH : res.keys.head hne < k) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set H : Key := res.keys.head hne with hHdef + set L : Key := res.keys.getLast hne with hLdef + have hbound : (reportedBelowBegin r res : Int) + r.begin.offset - 1 ≤ 0 := by + have h1 := reportedBelowBegin_le_between (db := db₀) hcomp₀ hmore hrev hne + have h2 := between_add_offset_le_one (db := db₀) hcomp₀ hmore hrev hne hcross + linarith + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hHge : ∀ x ∈ res.keys, x ≤ H := by + intro x hx; rw [hHdef]; exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hk_notin : k ∉ res.keys := by + intro hx + have hle := hHge k hx + have hleH : k ≤ res.keys.head hne := by rw [← hHdef]; exact hle + exact absurd hleH (not_le.mpr hkH) + have ha_sorted : res.keys.reverse.SortedLT := hsort.reverse + let db_a : DB := ⟨res.keys.reverse, ha_sorted⟩ + have ha_keys : db_a.keys = res.keys.reverse := rfl + have hcnt0 : cnt db_a r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [ha_keys] at hx + exact le_trans (le_of_lt hEL) + (WithTop.coe_le_coe.mpr (hLle x (List.mem_reverse.mp hx))) + have haresolve : resolve db_a r.begin ≤ ((L : Key) : WithTop Key) := by + have hanchor : r.begin.anchor db_a = (reportedBelowBegin r res : Int) + r.begin.offset - 1 := by + have hcnt : cnt db_a r.begin.key = reportedBelowBegin r res := by + unfold cnt reportedBelowBegin + rw [ha_keys, List.countP_reverse] + change (cnt db_a r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hcnt] + have hle : r.begin.anchor db_a ≤ 0 := by rw [hanchor]; linarith + by_cases hneg : r.begin.anchor db_a < 0 + · rw [resolve_eq_nil hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key L) + · have hanc0 : r.begin.anchor db_a = 0 := le_antisymm hle (le_of_not_gt hneg) + have hlen : 0 < db_a.keys.length := by + rw [ha_keys, List.length_reverse] + exact List.length_pos_of_ne_nil hne + have h0lt : (r.begin.anchor db_a).toNat < db_a.keys.length := by + rw [hanc0]; simpa using hlen + have hget0 : db_a.keys[(r.begin.anchor db_a).toNat] = L := by + have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanc0]; rfl + have h0 : db_a.keys[(r.begin.anchor db_a).toNat]? = some L := by + rw [hidx, ha_keys, ← List.head?_eq_getElem?] + rw [List.head?_reverse, List.getLast?_eq_some_getLast hne] + have h1 := List.getElem?_eq_getElem h0lt + rw [h0] at h1 + exact (Option.some.inj h1).symm + rw [resolve_eq_getElem (le_of_not_gt hneg) h0lt, hget0] + have hre_a : resolve db_a r.end_ = (⊤ : WithTop Key) := by + have hanc : (r.end_.anchor db_a).toNat = (r.end_.offset - 1).toNat := by + rw [anchor_toNat_eq (db := db_a) (s := r.end_) (by omega)] + simp only [cnt] at hcnt0 + rw [hcnt0, Nat.zero_add] + refine resolve_eq_top (by + change 0 ≤ (cnt db_a r.end_.key : Int) + r.end_.offset - 1 + rw [hcnt0]; push_cast; omega) ?_ + rw [hanc, ha_keys, List.length_reverse] + exact le_of_lt ht + have hck_a : RangeRead.completeKeys db_a r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, hre_a] + simp only [ite_true] + have hpred : db_a.keys.filter (fun x => decide + (resolve db_a r.begin ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < (⊤ : WithTop Key))) = db_a.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have hx' : L ≤ x := by + rw [ha_keys] at hx + exact hLle x (List.mem_reverse.mp hx) + exact ⟨le_trans haresolve (WithTop.coe_le_coe.mpr hx'), WithTop.coe_lt_top x⟩ + rw [hpred, ha_keys, List.reverse_reverse] + have hacompat : RangeRead.Compatible db_a r res := by + refine (compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mpr ⟨hcomp₀.1, ?_⟩ + rw [hck_a, List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hLle x hx + have hk_notin_a : k ∉ db_a.keys := by + rw [ha_keys, List.mem_reverse] + exact hk_notin + -- insert the target key above the head + let db_b : DB := db_a.insert k hk_notin_a + have hb_keys : db_b.keys = res.keys.reverse ++ [k] := by + rw [DB.insert_keys] + exact orderedInsert_append_of_forall_lt' (fun x hx => by + have hx' : x ∈ res.keys := by + have hx'' : x ∈ res.keys.reverse := by simpa [db_a] using hx + exact List.mem_reverse.mp hx'' + exact lt_of_le_of_lt (hHge x hx') hkH) + have hre_b : resolve db_b r.end_ = (⊤ : WithTop Key) := by + have hcnt_b0 : cnt db_b r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hb_keys, List.mem_append, List.mem_singleton] at hx + rcases hx with hx | rfl + · have hx' : x ∈ res.keys := List.mem_reverse.mp hx + exact le_trans (le_of_lt hEL) (WithTop.coe_le_coe.mpr (hLle x hx')) + · exact le_of_lt (lt_trans hEL (lt_of_le_of_lt + (WithTop.coe_le_coe.mpr (hLle H (List.head_mem hne))) + (WithTop.coe_lt_coe.mpr hkH))) + have hanc : (r.end_.anchor db_b).toNat = (r.end_.offset - 1).toNat := by + rw [anchor_toNat_eq (db := db_b) (s := r.end_) (by omega)] + simp only [cnt] at hcnt_b0 + rw [hcnt_b0, Nat.zero_add] + refine resolve_eq_top (by + change 0 ≤ (cnt db_b r.end_.key : Int) + r.end_.offset - 1 + rw [hcnt_b0]; push_cast; omega) ?_ + rw [hanc, hb_keys, List.length_append, List.length_reverse, List.length_singleton] + omega + have hrb_b : resolve db_b r.begin ≤ ((k : Key) : WithTop Key) := by + have hlenpos : 0 < res.keys.reverse.length := by + rw [List.length_reverse] + exact List.length_pos_of_ne_nil hne + have hkb : res.keys.reverse.length < db_b.keys.length := by + rw [hb_keys, List.length_append]; simp + have hkget : db_b.keys[res.keys.reverse.length] = k := by + have hkget? : db_b.keys[res.keys.reverse.length]? = some k := by + rw [hb_keys] + simp + have h1 := List.getElem?_eq_getElem hkb + rw [hkget?] at h1 + exact (Option.some.inj h1).symm + have hclamp : clampIndex db_b r.begin ≤ res.keys.reverse.length := by + unfold clampIndex + have htoNat : (r.begin.anchor db_b).toNat ≤ res.keys.reverse.length := by + by_cases h : 0 ≤ r.begin.anchor db_b + · have hcnt_b : cnt db_b r.begin.key ≤ reportedBelowBegin r res + 1 := by + unfold cnt reportedBelowBegin + rw [hb_keys, List.countP_append] + have h1 : res.keys.reverse.countP + (fun x => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.countP (fun x => decide (((x : Key) : WithTop Key) < r.begin.key)) := + List.countP_reverse + rw [h1] + have h2 : ([k].countP (fun x => decide (((x : Key) : WithTop Key) < r.begin.key))) ≤ 1 := by + rw [List.countP_singleton] + split <;> omega + omega + have hanchor_b : r.begin.anchor db_b ≤ ((res.keys.reverse.length : ℕ) : Int) := by + change (cnt db_b r.begin.key : Int) + r.begin.offset - 1 ≤ _ + have h1 : (cnt db_b r.begin.key : Int) ≤ (reportedBelowBegin r res : Int) + 1 := by + exact_mod_cast hcnt_b + have hlen1 : (1 : Int) ≤ ((res.keys.reverse.length : ℕ) : Int) := by + have hp : 0 < res.keys.reverse.length := hlenpos + omega + linarith + exact (Int.toNat_le).mpr hanchor_b + · have hz : (r.begin.anchor db_b).toNat = 0 := by omega + rw [hz]; omega + exact le_trans (Nat.min_le_left _ _) htoNat + have hle := (resolve_le_getElem_iff (db := db_b) (s := r.begin) hkb).mpr hclamp + rwa [hkget] at hle + have hkb_mem : k ∈ RangeRead.completeKeys db_b r := by + rw [RangeRead.mem_completeKeys] + exact ⟨DB.mem_insert_self db_a k hk_notin_a, hrb_b, by rw [hre_b]; exact WithTop.coe_lt_top k⟩ + have hck_pair : (RangeRead.completeKeys db_b r).Pairwise (· > ·) := by + have h := RangeRead.completeKeys_pairwise (db := db_b) (r := r) + rw [hrev] at h + exact h + have hhead_max : ∀ x ∈ RangeRead.completeKeys db_b r, x ≤ k := by + intro x hx + have hxdb := (RangeRead.mem_completeKeys.mp hx).1 + rw [hb_keys, List.mem_append, List.mem_singleton] at hxdb + rcases hxdb with hx' | rfl + · have hx'' : x ∈ res.keys := by + have hxx : x ∈ res.keys.reverse := by simpa [db_a] using hx' + exact List.mem_reverse.mp hxx + exact le_of_lt (lt_of_le_of_lt (hHge x hx'') hkH) + · exact le_rfl + have hhead_eq : (RangeRead.completeKeys db_b r).head (List.ne_nil_of_mem hkb_mem) = k := by + apply le_antisymm (hhead_max _ (List.head_mem _)) + exact (hck_pair.imp le_of_lt).rel_head hkb_mem + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db_a db_b := + (ConflictRange.keys_insert_of_not_mem cr db_a hk_notin_a hkcr).symm + have hcomp_b : RangeRead.Compatible db_b r res := (hs db_a db_b hagree).mp hacompat + have hpre := RangeRead.compatible_keys_prefix hcomp_b + have hhead := hpre.head hne + have hHk : H = k := by + rw [hHdef] + exact hhead.trans hhead_eq + have hHklt : H < k := by + rw [hHdef]; exact hkH + exact absurd hHk (ne_of_lt hHklt) + +/-! ### Gap-key necessity -/ + +/-- **Every gap key is necessary in the large-gap crossed top cell.** A witness stores +`end_.offset - 1 - #reported` extra keys in `[end_.key, last)` including the target, the +reversed reported keys, and one key above the reported head and at or above the begin +key. The end selector resolves onto that top key. Erasing the target shortens the +window by one, pushes the end selector past the end, and lets the top key enter the +reported filter, so the truncated result changes. -/ +theorem necessary_gap_of_residual_top_large_finite {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (_hbeg : r.begin.offset < 0) (hne : res.keys ≠ []) + (hend : 2 ≤ r.end_.offset) + (hEL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key)) + (ht : res.keys.length < (r.end_.offset - 1).toNat) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hBfin : r.begin.key ≠ ⊤) + (hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.getLast hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞)) + {k : Key} (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set E : WithTop Key := r.end_.key with hE + set B : WithTop Key := r.begin.key with hB + set e : Nat := (r.end_.offset - 1).toNat with he + set t : Nat := res.keys.length with ht' + set L : Key := res.keys.getLast hne with hL + set H : Key := res.keys.head hne with hH + set D : Nat := e - t with hD + have ht_lt : t < e := by simpa [t, e] using ht + have hD1 : 1 ≤ D := by omega + have hDt : D + t = e := by omega + have hLmem : L ∈ res.keys := by rw [hL]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne + have hEL' : E < ((L : Key) : WithTop Key) := by simpa [E, L] using hEL + have hLB : ((L : Key) : WithTop Key) ≤ B := + le_of_lt (by simpa [B, L] using hcross) + have hbound : (reportedBelowBegin r res : Int) + r.begin.offset - 1 ≤ 0 := by + have h1 := reportedBelowBegin_le_between (db := db₀) hcomp₀ hmore hrev hne + have h2 := between_add_offset_le_one (db := db₀) hcomp₀ hmore hrev hne hcross + linarith + have hcap : ((D : Nat) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by + have hgap' : ((e : ℕ∞)) ≤ intervalCard E ((L : Key) : WithTop Key) + (t : ℕ∞) := by + simpa [E, L, e, t] using hgap + have hcast : ((D : ℕ∞) + (t : ℕ∞)) = (e : ℕ∞) := by + rw [← Nat.cast_add, hDt] + rw [← hcast] at hgap' + exact (ENat.add_le_add_iff_right (ENat.natCast_ne_top t)).mp hgap' + -- the `D` gap keys in `[E, last)`, one of them the target + obtain ⟨pad, hpadIn, hkpad, hpadlen⟩ := + exists_db_keysIn_avoid (B := E) (E := ((L : Key) : WithTop Key)) (k := k) (m := D - 1) (by + have hsub : D - 1 + 1 = D := Nat.sub_add_cancel hD1 + have h : (((D - 1 + 1 : ℕ)) : ℕ∞) ≤ intervalCard E ((L : Key) : WithTop Key) := by + rw [hsub] + exact hcap + simpa only [Nat.cast_add, Nat.cast_one] using h) + set G : List Key := (pad.insert k hkpad).keys with hGdef + have hGmem : ∀ x ∈ G, E ≤ (x : WithTop Key) ∧ (x : WithTop Key) < ((L : Key) : WithTop Key) := by + intro x hx + have hx' : x ∈ List.orderedInsert (fun x1 x2 => x1 ≤ x2) k pad.keys := by + rw [hGdef, DB.insert_keys] at hx + exact hx + rw [List.mem_orderedInsert] at hx' + rcases hx' with rfl | hx' + · exact ⟨hEk, hkL⟩ + · simpa only [keysIn, Set.mem_ofPred_eq] using hpadIn x hx' + have hGsort : G.SortedLT := by + rw [hGdef] + exact (pad.insert k hkpad).sorted + have hGlen : G.length = D := by + rw [hGdef, DB.insert_keys, List.orderedInsert_length, hpadlen] + omega + have hkG : k ∈ G := by + rw [hGdef, DB.insert_keys, List.mem_orderedInsert] + exact Or.inl rfl + -- a key above the head and at or above the begin key + obtain ⟨b, hb⟩ := WithTop.ne_top_iff_exists.mp hBfin + have hbB : ((b : Key) : WithTop Key) = B := hb.trans hB.symm + set Q : Key := keyAfter (max H b) with hQ + have hHQ : H < Q := by + rw [hQ] + exact lt_of_le_of_lt (le_max_left _ _) (keyAfter_self_lt _) + have hbQ : b < Q := by + rw [hQ] + exact lt_of_le_of_lt (le_max_right _ _) (keyAfter_self_lt _) + have hBQ : B < ((Q : Key) : WithTop Key) := by + rw [← hbB] + exact WithTop.coe_lt_coe.mpr hbQ + set R : List Key := res.keys.reverse with hR + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLleH : L ≤ H := by + have h := (hsort.pairwise.imp le_of_lt).rel_getLast hHmem + simpa [hL, hH] using h + have hRsort : R.SortedLT := by + rw [hR] + exact hsort.reverse + have hRmem : ∀ y ∈ R, L ≤ y := by + intro y hy + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + exact (hsort.pairwise.imp le_of_lt).rel_getLast hyres + have hRleH : ∀ y ∈ R, y ≤ H := by + intro y hy + have hyres : y ∈ res.keys := by + rw [hR] at hy + exact List.mem_reverse.mp hy + simpa [hH] using (hsort.pairwise.imp (fun h => le_of_lt h)).rel_head hyres + have hGR : ∀ x ∈ G, ∀ y ∈ R, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr (hRmem y hy))) + set T : List Key := G ++ R with hT + have hTsort : T.SortedLT := by + rw [hT] + exact List.sortedLT_append.mpr ⟨hGsort, hRsort, hGR⟩ + have hTQ : ∀ x ∈ T, x < Q := by + intro x hx + rw [hT] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_trans (WithTop.coe_lt_coe.mp + (lt_of_lt_of_le (hGmem x hx).2 (WithTop.coe_le_coe.mpr hLleH))) hHQ + · exact lt_of_le_of_lt (hRleH x hx) hHQ + have hQsort : ([Q] : List Key).SortedLT := sortedLT_singleton Q + have hfullSort : (T ++ [Q]).SortedLT := + List.sortedLT_append.mpr ⟨hTsort, hQsort, by + intro x hx y hy + rw [List.mem_singleton] at hy + subst y + exact hTQ x hx⟩ + set dbfull : DB := ⟨T ++ [Q], hfullSort⟩ with hdbfull + have hfullKeys : dbfull.keys = T ++ [Q] := rfl + have hTlen : T.length = e := by + rw [hT, List.length_append, hGlen, hR, List.length_reverse, ← ht'] + omega + have haboveE : above dbfull E = T ++ [Q] := by + rw [above_eq_filter, hfullKeys, List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rcases List.mem_append.mp hx with hx | hx + · rw [hT] at hx + rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x hx).1 + · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) + · rw [List.mem_singleton] at hx + subst x + exact le_trans (le_of_lt hEL') (le_trans (WithTop.coe_le_coe.mpr hLleH) + (le_of_lt (WithTop.coe_lt_coe.mpr hHQ))) + have hcnt0 : cnt dbfull E = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hfullKeys, List.mem_append, List.mem_singleton] at hx + rcases hx with hx | rfl + · rw [hT] at hx + rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x hx).1 + · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) + · exact le_trans (le_of_lt hEL') (le_trans (WithTop.coe_le_coe.mpr hLleH) + (le_of_lt (WithTop.coe_lt_coe.mpr hHQ))) + have hancEnd : (r.end_.anchor dbfull).toNat = e := by + rw [anchor_toNat_eq (db := dbfull) (s := r.end_) (by omega)] + have h0 : dbfull.keys.countP + (fun (x : Key) => decide ((↑x : WithTop Key) < r.end_.key)) = 0 := by + simpa [cnt, hE] using hcnt0 + rw [h0, Nat.zero_add, he] + have hq : dbfull.keys[e]? = some Q := by + rw [hfullKeys, List.getElem?_append_right (l₁ := T) (l₂ := [Q]) (i := e) + (le_of_eq hTlen)] + rw [show e - T.length = 0 from by rw [hTlen]; omega] + simp + have hre : resolve dbfull r.end_ = ((Q : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor dbfull := by + change 0 ≤ (cnt dbfull r.end_.key : Int) + r.end_.offset - 1 + rw [show cnt dbfull r.end_.key = 0 from by simpa only [hE] using hcnt0] + push_cast + omega + rw [resolve, ite_eq_right (not_lt.mpr h0)] + rw [show dbfull.keys[(r.end_.anchor dbfull).toNat]? = some Q from by + rw [hancEnd]; exact hq] + -- the begin resolution stays at or below the reported last + have hcntB : cnt dbfull B = D + reportedBelowBegin r res := by + unfold cnt reportedBelowBegin + rw [hfullKeys, hT, List.countP_append, List.countP_append] + have hG : G.countP (fun x : Key => decide ((x : WithTop Key) < B)) = D := by + have h : G.countP (fun x : Key => decide ((x : WithTop Key) < B)) = G.length := + (List.countP_eq_length (l := G) + (p := fun x : Key => decide ((x : WithTop Key) < B))).mpr (by + intro x hx + exact decide_eq_true (lt_of_lt_of_le (hGmem x hx).2 hLB)) + rw [h, hGlen] + have hRc : R.countP (fun x : Key => decide ((x : WithTop Key) < B)) = + res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) := by + rw [hR, List.countP_reverse] + have hQc : ([Q].countP (fun x : Key => decide ((x : WithTop Key) < B))) = 0 := by + rw [List.countP_singleton] + have hnot : ¬ ((Q : Key) : WithTop Key) < B := not_lt.mpr (le_of_lt hBQ) + simp [hnot] + rw [hG, hRc, hQc] + omega + have hanchorB : r.begin.anchor dbfull = + (D : Int) + (reportedBelowBegin r res : Int) + r.begin.offset - 1 := by + change (cnt dbfull r.begin.key : Int) + r.begin.offset - 1 = _ + rw [show cnt dbfull r.begin.key = D + reportedBelowBegin r res from by + simpa only [hB] using hcntB] + push_cast + ring + have hanchorB_le : r.begin.anchor dbfull ≤ (D : Int) := by + rw [hanchorB] + linarith + have hbetween : between dbfull ((L : Key) : WithTop Key) B = + R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by + unfold between + rw [hfullKeys, hT, List.append_assoc, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) G = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hconj + exact absurd (of_decide_eq_true hconj).1 (not_le.mpr (hGmem x hx).2) + have hRself : List.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) R = + R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by + apply List.filter_congr + intro x hx + apply Bool.decide_congr + exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr (hRmem x hx), h⟩⟩ + have hQnil : List.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) [Q] = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hconj + rw [List.mem_singleton] at hx + subst x + exact absurd (of_decide_eq_true hconj).2 (not_lt.mpr (le_of_lt hBQ)) + rw [hGnil, hRself, hQnil] + simp + have hbetweenLen : (between dbfull ((L : Key) : WithTop Key) B).length = + reportedBelowBegin r res := by + rw [hbetween, ← List.countP_eq_length_filter, hR, List.countP_reverse] + rfl + have hLmemR : L ∈ R := by + rw [hR] + exact List.mem_reverse.mpr hLmem + have hLmemFull : L ∈ dbfull.keys := by + rw [hfullKeys, hT] + exact List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hLmemR))) + have hrb : resolve dbfull r.begin ≤ ((L : Key) : WithTop Key) := by + refine resolve_begin_le_of_between_bound (db := dbfull) (r := r) (last := L) + hLmemFull ?_ ?_ + · simpa [L] using hcross + · have h := hbound + rw [show between dbfull ((L : Key) : WithTop Key) r.begin.key = + between dbfull ((L : Key) : WithTop Key) B from by rw [hB]] + rw [hbetweenLen] + exact h + have hcompFull : RangeRead.Compatible dbfull r res := by + refine (compatible_iff_truncated_keys_rev (db := dbfull) hrev hmore hne).mpr + ⟨hcomp₀.1, ?_⟩ + rw [completeKeys_filter_ge_eq_resolve_end (db := dbfull) (r := r) (last := L) hrev hrb] + have hfiltL : dbfull.keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by + rw [hfullKeys, hT, List.append_assoc, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide (L ≤ x)) G = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hlx + exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx)) + (not_le.mpr (hGmem x hx).2) + have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hRmem x hx) + have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst x + exact decide_eq_true (le_trans hLleH (le_of_lt hHQ)) + rw [hGnil, hRself, hQself] + simp + rw [hfiltL, List.filter_append, hre] + have hRself : List.filter (fun y : Key => decide ((y : WithTop Key) < ((Q : Key) : WithTop Key))) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (hRleH x hx) hHQ)) + have hQnil : List.filter (fun y : Key => decide ((y : WithTop Key) < ((Q : Key) : WithTop Key))) [Q] = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hlt + rw [List.mem_singleton] at hx + subst x + exact absurd (of_decide_eq_true hlt) (not_lt.mpr le_rfl) + rw [hRself, hQnil, List.append_nil, hR, List.reverse_reverse] + -- the erased database resolves the end past the end and reports the top key too + have hkeysErase : (dbfull.erase k).keys = G.erase k ++ (R ++ [Q]) := by + rw [DB.erase_keys, hfullKeys, hT, List.append_assoc, + List.erase_append_left (l₁ := G) (l₂ := R ++ [Q]) hkG] + have hcnt0Erase : cnt (dbfull.erase k) E = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeysErase] at hx + rcases List.mem_append.mp hx with hx | hx + · exact (hGmem x (List.mem_of_mem_erase hx)).1 + · rcases List.mem_append.mp hx with hx | hx + · exact le_trans (le_of_lt hEL') (WithTop.coe_le_coe.mpr (hRmem x hx)) + · rw [List.mem_singleton] at hx + subst x + exact le_trans (le_of_lt hEL') (le_trans (WithTop.coe_le_coe.mpr hLleH) + (le_of_lt (WithTop.coe_lt_coe.mpr hHQ))) + have hlenErase : (dbfull.erase k).keys.length = e := by + rw [hkeysErase, List.length_append, List.length_append, List.length_erase_of_mem hkG, + hGlen, hR, List.length_reverse, ← ht', List.length_singleton] + omega + have hancEndErase : (r.end_.anchor (dbfull.erase k)).toNat = e := by + rw [anchor_toNat_eq (db := dbfull.erase k) (s := r.end_) (by omega)] + have h0 : (dbfull.erase k).keys.countP + (fun (x : Key) => decide ((↑x : WithTop Key) < r.end_.key)) = 0 := by + simpa [cnt, hE] using hcnt0Erase + rw [h0, Nat.zero_add, he] + have hreErase : resolve (dbfull.erase k) r.end_ = (⊤ : WithTop Key) := by + refine resolve_eq_top (by + change 0 ≤ (cnt (dbfull.erase k) r.end_.key : Int) + r.end_.offset - 1 + rw [show cnt (dbfull.erase k) r.end_.key = 0 from by + simpa only [hE] using hcnt0Erase] + push_cast + omega) ?_ + rw [hancEndErase, hlenErase] + have hbetweenErase : between (dbfull.erase k) ((L : Key) : WithTop Key) B = + R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by + unfold between + rw [hkeysErase, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) + (G.erase k) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hconj + exact absurd (of_decide_eq_true hconj).1 + (not_le.mpr (hGmem x (List.mem_of_mem_erase hx)).2) + have hRself : List.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) R = + R.filter (fun y : Key => decide ((y : WithTop Key) < B)) := by + apply List.filter_congr + intro x hx + apply Bool.decide_congr + exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr (hRmem x hx), h⟩⟩ + have hQnil : List.filter (fun x : Key => decide + (((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : WithTop Key) < B)) [Q] = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hconj + rw [List.mem_singleton] at hx + subst x + exact absurd (of_decide_eq_true hconj).2 (not_lt.mpr (le_of_lt hBQ)) + rw [hGnil, hRself, hQnil] + simp + have hbetweenLenErase : (between (dbfull.erase k) ((L : Key) : WithTop Key) B).length = + reportedBelowBegin r res := by + rw [hbetweenErase, ← List.countP_eq_length_filter, hR, List.countP_reverse] + rfl + have hLmemErase : L ∈ (dbfull.erase k).keys := by + rw [hkeysErase] + exact List.mem_append.mpr (Or.inr (List.mem_append.mpr (Or.inl hLmemR))) + have hrbErase : resolve (dbfull.erase k) r.begin ≤ ((L : Key) : WithTop Key) := by + refine resolve_begin_le_of_between_bound (db := dbfull.erase k) (r := r) (last := L) + hLmemErase ?_ ?_ + · simpa [L] using hcross + · have h := hbound + rw [show between (dbfull.erase k) ((L : Key) : WithTop Key) r.begin.key = + between (dbfull.erase k) ((L : Key) : WithTop Key) B from by rw [hB]] + rw [hbetweenLenErase] + exact h + have hfilterErase : (dbfull.erase k).keys.filter (fun x : Key => decide (L ≤ x)) = R ++ [Q] := by + rw [hkeysErase, List.filter_append, List.filter_append] + have hGnil : List.filter (fun x : Key => decide (L ≤ x)) (G.erase k) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx hlx + exact absurd (WithTop.coe_le_coe.mpr (of_decide_eq_true hlx)) + (not_le.mpr (hGmem x (List.mem_of_mem_erase hx)).2) + have hRself : List.filter (fun x : Key => decide (L ≤ x)) R = R := by + rw [List.filter_eq_self] + intro x hx + exact decide_eq_true (hRmem x hx) + have hQself : List.filter (fun x : Key => decide (L ≤ x)) [Q] = [Q] := by + rw [List.filter_eq_self] + intro x hx + rw [List.mem_singleton] at hx + subst x + exact decide_eq_true (le_trans hLleH (le_of_lt hHQ)) + rw [hGnil, hRself, hQself] + simp + have hckErase : (RangeRead.completeKeys (dbfull.erase k) r).filter + (fun x : Key => decide (L ≤ x)) = Q :: res.keys := by + rw [completeKeys_filter_ge_eq_resolve_end (db := dbfull.erase k) (r := r) (last := L) + hrev hrbErase] + rw [hfilterErase, hreErase] + have hid : (R ++ [Q]).filter + (fun x : Key => decide ((x : WithTop Key) < (⊤ : WithTop Key))) = R ++ [Q] := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_lt_top x + rw [hid, List.reverse_append, hR, List.reverse_reverse, List.reverse_singleton] + rfl + refine necessary_of_toggle ⟨dbfull, hcompFull, ?_⟩ + have hTmem : k ∈ T := by + rw [hT] + exact List.mem_append.mpr (Or.inl hkG) + have hkFull : k ∈ dbfull.keys := by + rw [hfullKeys] + exact List.mem_append.mpr (Or.inl hTmem) + rw [toggle_eq_erase_of_mem hkFull] + intro hcomp' + have hfil := (compatible_iff_truncated_keys_rev (db := dbfull.erase k) hrev hmore hne).mp + hcomp' |>.2 + rw [hckErase] at hfil + have hlen := congrArg List.length hfil + simp at hlen + +/-! ### The routed finite crossed subcell -/ + +/-- The routed subcell: the large-gap crossed top cell together with a finite crossed +begin selector (`last < begin.key ≠ ⊤`). The cross-free part is already covered by the +nonpositive reversed truncated rule, and the sentinel begin key is a separate +obstruction. -/ +abbrev negCrossTruncRevResidualTopLargeCrossed (r : RangeRead) (res : Result) : Prop := + negCrossTruncRevResidualTopLarge r res ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key ≠ ⊤ + +/-- The closed form of the routed large-gap crossed top subcell: `[end_.key, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncRevResidualTopLargeCrossed (r : RangeRead) + (_res : Result) : ConflictRange := + ⟨r.end_.key, ⊤⟩ + +/-- **The large-gap crossed top subcell closed form is minimal.** Soundness is +`sound_topRange_endKey_of_residual_large`. Necessity splits the range `[end_.key, ⊤)` +at the reported last and head: gap keys use the witness of +`necessary_gap_of_residual_top_large_finite`, reported keys use convexity, and keys +above the head use `necessary_above_head_of_residual_top_large`. -/ +theorem minimal_closedForm_of_negCross_trunc_rev_residual_top_large_crossed + {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevResidualTopLargeCrossed r res) : + Minimal r res (closedFormNegCrossTruncRevResidualTopLargeCrossed r res) := by + obtain ⟨hlarge, hcrossD, hBfin⟩ := h + obtain ⟨hmore, hrev, hbeg, hne, hend, hELD, htopD, hgapD⟩ := hlarge + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + have hlastD : res.keys.getLastD ([] : Key) = L := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne] + exact hLdef.symm + have hEL : r.end_.key < ((L : Key) : WithTop Key) := by + rw [hlastD] at hELD + exact hELD + have hcross : ((L : Key) : WithTop Key) < r.begin.key := by + rw [hlastD] at hcrossD + exact hcrossD + have hgap : ((r.end_.offset - 1).toNat : ℕ∞) ≤ + intervalCard r.end_.key ((L : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by + rw [hlastD] at hgapD + exact hgapD + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx + rw [hLdef] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne + have hcountP : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + exact decide_eq_true (le_of_lt (lt_of_lt_of_le hEL (WithTop.coe_le_coe.mpr (hLle x hx)))) + have ht : res.keys.length < (r.end_.offset - 1).toNat := by + rw [hcountP] at htopD + exact htopD + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · simpa [closedFormNegCrossTruncRevResidualTopLargeCrossed] using + sound_topRange_endKey_of_residual_large (r := r) (res := res) hmore hrev hbeg hne + hend (by simpa [hLdef] using hEL) + · intro k hk + rw [ConflictRange.mem_iff] at hk + obtain ⟨hEk, _⟩ := hk + by_cases hkL : (k : WithTop Key) < ((L : Key) : WithTop Key) + · exact necessary_gap_of_residual_top_large_finite hread' hmore hrev hbeg hne hend + (by simpa [hLdef] using hEL) ht (by simpa [hLdef] using hcross) hBfin + (by simpa [hLdef] using hgap) hEk hkL + · have hLk : ((L : Key) : WithTop Key) ≤ (k : WithTop Key) := le_of_not_gt hkL + have hkKey : L ≤ k := WithTop.coe_le_coe.mp hLk + by_cases hkH : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) + · exact necessary_of_between_result hread' hLmem hHmem hkKey + (WithTop.coe_le_coe.mp hkH) + · have hHk : H < k := WithTop.coe_lt_coe.mp (lt_of_not_ge hkH) + exact necessary_above_head_of_residual_top_large hread' hmore hrev hbeg hne hend + (by simpa [hLdef] using hEL) ht (by simpa [hLdef] using hcross) + (by simpa [hHdef] using hHk) + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncRevTop.lean b/FormalConflictRange/NegCrossTruncRevTop.lean new file mode 100644 index 0000000..51ebad9 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncRevTop.lean @@ -0,0 +1,777 @@ +/- +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.NegCrossTruncClamp + +/-! +# Reversed negative-begin truncated, sentinel-end cell + +This module closes the reversed traversal analogue of the forward crossed +negative-begin truncated cells, in the first and simplest subcell: the end selector's +key is the sentinel `⊤` and its offset is at least one, so the end resolves to `⊤` in +every database. The minimal sound conflict range is then `[last, ⊤)`, where `last` is +the smallest reported key (the last entry of the descending reversed result). + +The proof does not need the clamping/nonclamping split. The key observation is that +agreement on `[last, ⊤)` fixes the whole set of stored keys at or above `last`, and at +a negative begin offset the count of stored keys below `last` cancels in the begin +anchor, so the resolved begin stays at or below `last` on both sides. Hence the +complete keys are `res.keys` followed by keys below `last`, which keeps `res.keys` a +prefix. + +Above the reported head, necessity uses the database made of the reported keys in +ascending order: it is compatible, and inserting a fresh key above the head puts it at +the front of the (reversed) complete keys, so the reported keys are no longer a prefix. +-/ + +open FormalConflictRange + +set_option maxHeartbeats 1200000 + +namespace FormalConflictRange + +/-- If the end selector's key is the sentinel `⊤` and its offset is at least one, the +end selector resolves to the sentinel in every database. -/ +theorem resolve_end_eq_top_of_key_top {db : DB} {r : RangeRead} + (hkey : r.end_.key = (⊤ : WithTop Key)) (hoff : 1 ≤ r.end_.offset) : + resolve db r.end_ = (⊤ : WithTop Key) := by + have hcnt : db.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) = + db.keys.length := by + rw [hkey] + exact List.countP_eq_length.mpr fun k _ => by simp + have hann : r.end_.anchor db = (db.keys.length : Int) + r.end_.offset - 1 := by + unfold Selector.anchor + rw [hcnt] + refine resolve_eq_top ?_ ?_ + · rw [hann]; omega + · have htoNat : (r.end_.anchor db).toNat = + db.keys.length + (r.end_.offset - 1).toNat := by + rw [hann, show ((db.keys.length : Int) + r.end_.offset - 1) = + ((db.keys.length : Int) + (r.end_.offset - 1)) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + rw [htoNat] + omega + +/-- When the resolved end is the sentinel and the resolved begin lies at or below +`last`, filtering the reversed complete keys at or above `last` is the reversal of the +stored keys at or above `last`. -/ +theorem completeKeys_filter_ge_eq_of_resolve_le {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = true) (hre : resolve db r.end_ = (⊤ : WithTop Key)) + (hrb : resolve db r.begin ≤ ((last : Key) : WithTop Key)) : + (RangeRead.completeKeys db r).filter (fun k => decide (last ≤ k)) = + (db.keys.filter (fun k => decide (last ≤ k))).reverse := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, hre] + simp only [ite_true] + rw [List.filter_reverse, List.reverse_inj, List.filter_filter] + apply List.filter_congr + intro k _hk + by_cases hLk : last ≤ k + · have hres : decide (resolve db r.begin ≤ ((k : Key) : WithTop Key)) = true := by + rw [decide_eq_true_eq] + exact le_trans hrb (WithTop.coe_le_coe.mpr hLk) + simp [hLk, hres] + · simp [hLk] + +/-- **The begin anchor bound.** In a compatible crossed reversed truncated result the +number of stored keys in `[last, begin.key)`, plus the begin offset minus one, is at +most zero. This is the cancellation that makes agreement on `[last, ⊤)` sound: adding +or removing keys below `last` shifts the anchor index and the position of `last` in +lockstep. -/ +theorem between_add_offset_le_one {r : RangeRead} {res : Result} {db : DB} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) : + ((between db ((res.keys.getLast hne : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hLcomplete : L ∈ RangeRead.completeKeys db r := (List.mem_filter.mp hLfilt).1 + have hLmem' := RangeRead.mem_completeKeys.mp hLcomplete + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := hLmem'.2.1 + have hLdb : L ∈ db.keys := hLmem'.1 + obtain ⟨j, hjlt, hjeq⟩ := List.mem_iff_getElem.mp hLdb + have hclamp : clampIndex db r.begin ≤ j := + (resolve_le_getElem_iff (db := db) (s := r.begin) hjlt).mp (by rw [hjeq]; exact hrb) + have htoNat : (r.begin.anchor db).toNat ≤ j := by + have h1 : min (r.begin.anchor db).toNat db.keys.length ≤ j := hclamp + by_cases h : (r.begin.anchor db).toNat ≤ db.keys.length + · rwa [Nat.min_eq_left h] at h1 + · rw [Nat.min_eq_right (by omega)] at h1 + omega + have hanchor_le : r.begin.anchor db ≤ (j : Int) := by + have h1 : r.begin.anchor db ≤ ((r.begin.anchor db).toNat : Int) := by + by_cases h : 0 ≤ r.begin.anchor db + · rw [Int.toNat_of_nonneg h] + · omega + have h2 : ((r.begin.anchor db).toNat : Int) ≤ (j : Int) := by exact_mod_cast htoNat + linarith + have hcntL : cnt db ((L : Key) : WithTop Key) = j := by + rw [cnt_coe] + exact countP_lt_eq_of_getElem db.sorted hjlt hjeq + have hLleBegin : ((L : Key) : WithTop Key) ≤ r.begin.key := le_of_lt hcross + have hcnt_begin : cnt db r.begin.key = j + (between db ((L : Key) : WithTop Key) r.begin.key).length := by + rw [cnt_eq_add_between db hLleBegin, hcntL] + have hanchor : r.begin.anchor db = (j : Int) + + ((between db ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 := by + change (cnt db r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hcnt_begin]; push_cast; ring + have hmain := hanchor_le + rw [hanchor] at hmain + linarith + +/-- **The reported-below-begin offset bound.** With a sentinel end, the number of +reported keys below `begin.key` plus the begin offset minus one is at most zero. -/ +theorem reportedBelowBegin_add_offset_le_one {r : RangeRead} {res : Result} {db : DB} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) : + (reportedBelowBegin r res : Int) + r.begin.offset - 1 ≤ 0 := by + set L : Key := res.keys.getLast hne with hLdef + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have htop : db.keys.filter (fun k => decide (L ≤ k)) = res.keys.reverse := by + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_top hendkey hendoff + have h1 := completeKeys_filter_ge_eq_of_resolve_le (db := db) (r := r) (last := L) hrev hre hrb + rw [hfil] at h1 + have h2 : res.keys.reverse = db.keys.filter (fun k => decide (L ≤ k)) := by + have hrev1 := congrArg List.reverse h1 + simpa using hrev1 + exact h2.symm + have hbetween : (between db ((L : Key) : WithTop Key) r.begin.key).length + = reportedBelowBegin r res := by + have hbeq : between db ((L : Key) : WithTop Key) r.begin.key = + (db.keys.filter (fun k => decide (L ≤ k))).filter + (fun k => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [Bool.decide_and, Bool.and_comm] + by_cases h : L ≤ k + · simp [h, WithTop.coe_le_coe.mpr h] + · have h' : ¬ (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := by + simpa [WithTop.coe_le_coe] using h + simp [h, h'] + rw [hbeq, htop, ← List.countP_eq_length_filter, List.countP_reverse, reportedBelowBegin] + have hmain := between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + rw [hbetween] at hmain + exact hmain + +/-- **The begin resolution stays at or below the reported last under agreement on +`[last, ⊤)`.** This is the generic counting step used by both sentinel-end soundness +branches; it does not involve the end selector. -/ +theorem resolve_begin_le_of_agree_topOfLast {r : RangeRead} {res : Result} {db db' : DB} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hag : AgreeOn (topOfLast (res.keys.getLast hne)) db db') : + resolve db' r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hfilEq : db'.keys.filter (fun k => decide (L ≤ k)) = + db.keys.filter (fun k => decide (L ≤ k)) := by + have h := hag + unfold AgreeOn at h + rw [topOfLast_keys, topOfLast_keys] at h + exact h.symm + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLdb' : L ∈ db'.keys := by + have hmem : L ∈ db'.keys.filter (fun k => decide (L ≤ k)) := by + rw [hfilEq] + exact List.mem_filter.mpr ⟨hLdb, by simp⟩ + exact (List.mem_filter.mp hmem).1 + obtain ⟨j', hj'lt, hj'eq⟩ := List.mem_iff_getElem.mp hLdb' + have hcntL' : cnt db' ((L : Key) : WithTop Key) = j' := by + rw [cnt_coe] + exact countP_lt_eq_of_getElem db'.sorted hj'lt hj'eq + have hbeq : ∀ d : DB, between d ((L : Key) : WithTop Key) r.begin.key = + (d.keys.filter (fun k : Key => decide (L ≤ k))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + intro d + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [Bool.decide_and, Bool.and_comm] + by_cases h : L ≤ k + · simp [h, WithTop.coe_le_coe.mpr h] + · have h' : ¬ (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := by + simpa [WithTop.coe_le_coe] using h + simp [h, h'] + have hbetween : between db' ((L : Key) : WithTop Key) r.begin.key = + between db ((L : Key) : WithTop Key) r.begin.key := by + rw [hbeq db', hbeq db, hfilEq] + have hΔ : ((between db ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := + between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + have hΔ' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [hbetween]; exact hΔ + have hanchor' : r.begin.anchor db' = (j' : Int) + + ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 := by + have hcnt : cnt db' r.begin.key = + j' + (between db' ((L : Key) : WithTop Key) r.begin.key).length := by + rw [cnt_eq_add_between db' (le_of_lt hLcross), hcntL'] + change (cnt db' r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hcnt]; push_cast; ring + have hanchor_le' : r.begin.anchor db' ≤ (j' : Int) := by + rw [hanchor']; linarith + have htoNat' : (r.begin.anchor db').toNat ≤ j' := by + by_cases h : 0 ≤ r.begin.anchor db' + · exact (Int.toNat_le).mpr hanchor_le' + · have hz : (r.begin.anchor db').toNat = 0 := by omega + rw [hz]; omega + have hclamp' : clampIndex db' r.begin ≤ j' := by + unfold clampIndex + exact le_trans (Nat.min_le_left _ _) htoNat' + have hle := (resolve_le_getElem_iff (db := db') (s := r.begin) hj'lt).mpr hclamp' + rwa [hj'eq] at hle + +/-- **Two reversed reads with the same end selector and begin resolutions at or below +`last` see the same stored keys at or above `last`.** -/ +theorem completeKeys_filter_ge_eq_of_both_le {db : DB} {r₁ r₂ : RangeRead} {last : Key} + (hrev₁ : r₁.reverse = true) (hrev₂ : r₂.reverse = true) + (hend : r₁.end_ = r₂.end_) + (h₁ : resolve db r₁.begin ≤ ((last : Key) : WithTop Key)) + (h₂ : resolve db r₂.begin ≤ ((last : Key) : WithTop Key)) : + (RangeRead.completeKeys db r₁).filter (fun k => decide (last ≤ k)) = + (RangeRead.completeKeys db r₂).filter (fun k => decide (last ≤ k)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev₁, hrev₂] + simp only [ite_true] + rw [hend] + rw [List.filter_reverse, List.filter_reverse, List.reverse_inj, + List.filter_filter, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hlk : last ≤ k + · have hlk' : ((last : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr hlk + have hb₁ : resolve db r₁.begin ≤ ((k : Key) : WithTop Key) := le_trans h₁ hlk' + have hb₂ : resolve db r₂.begin ≤ ((k : Key) : WithTop Key) := le_trans h₂ hlk' + simp [hlk, hb₁, hb₂] + · simp [hlk] + +/-- **Soundness of the top-of-last range in the reversed negative-begin truncated +sentinel-end cell.** Agreement on `[last, ⊤)` fixes the stored keys at or above `last`, +and the resolved begin stays at or below `last` on both sides because the count of +stored keys in `[last, begin.key)` is shared. -/ +theorem sound_topOfLast_of_rev_negTrunc_top {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (_hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) : + Sound r res (topOfLast (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hLcross : ((L : Key) : WithTop Key) < r.begin.key := hcross + have step : ∀ db db', AgreeOn (topOfLast L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_top hendkey hendoff + have hre' : resolve db' r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_top hendkey hendoff + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := by + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + exact (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hfilEq : db'.keys.filter (fun k => decide (L ≤ k)) = + db.keys.filter (fun k => decide (L ≤ k)) := by + have h := hag' + unfold AgreeOn at h + rw [topOfLast_keys, topOfLast_keys] at h + exact h.symm + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLdb' : L ∈ db'.keys := by + have hmem : L ∈ db'.keys.filter (fun k => decide (L ≤ k)) := by + rw [hfilEq] + exact List.mem_filter.mpr ⟨hLdb, by simp⟩ + exact (List.mem_filter.mp hmem).1 + obtain ⟨j', hj'lt, hj'eq⟩ := List.mem_iff_getElem.mp hLdb' + have hcntL' : cnt db' ((L : Key) : WithTop Key) = j' := by + rw [cnt_coe] + exact countP_lt_eq_of_getElem db'.sorted hj'lt hj'eq + have hbeq : ∀ d : DB, between d ((L : Key) : WithTop Key) r.begin.key = + (d.keys.filter (fun k : Key => decide (L ≤ k))).filter + (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by + intro d + unfold between + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [Bool.decide_and, Bool.and_comm] + by_cases h : L ≤ k + · simp [h, WithTop.coe_le_coe.mpr h] + · have h' : ¬ (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := by + simpa [WithTop.coe_le_coe] using h + simp [h, h'] + have hbetween : between db' ((L : Key) : WithTop Key) r.begin.key = + between db ((L : Key) : WithTop Key) r.begin.key := by + rw [hbeq db', hbeq db, hfilEq] + have hΔ : ((between db ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := + between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross + have hΔ' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 ≤ 0 := by + rw [hbetween]; exact hΔ + have hanchor' : r.begin.anchor db' = (j' : Int) + + ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) + + r.begin.offset - 1 := by + have hcnt : cnt db' r.begin.key = + j' + (between db' ((L : Key) : WithTop Key) r.begin.key).length := by + rw [cnt_eq_add_between db' (le_of_lt hLcross), hcntL'] + change (cnt db' r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hcnt]; push_cast; ring + have hanchor_le' : r.begin.anchor db' ≤ (j' : Int) := by + rw [hanchor']; linarith + have htoNat' : (r.begin.anchor db').toNat ≤ j' := by + by_cases h : 0 ≤ r.begin.anchor db' + · exact (Int.toNat_le).mpr hanchor_le' + · have hz : (r.begin.anchor db').toNat = 0 := by omega + rw [hz]; omega + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := by + have hclamp' : clampIndex db' r.begin ≤ j' := by + unfold clampIndex + exact le_trans (Nat.min_le_left _ _) htoNat' + have hle := (resolve_le_getElem_iff (db := db') (s := r.begin) hj'lt).mpr hclamp' + rwa [hj'eq] at hle + have hdb'filt := completeKeys_filter_ge_eq_of_resolve_le (db := db') (r := r) (last := L) + hrev hre' hrb' + have hdbfilt := completeKeys_filter_ge_eq_of_resolve_le (db := db) (r := r) (last := L) + hrev hre hrb + refine (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr ⟨hcomp.1, ?_⟩ + rw [hdb'filt, hfilEq, ← hdbfilt, hfil] + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Above-head necessity in the reversed negative-begin truncated sentinel-end +cell.** The database made of the reported keys in ascending order is compatible; +inserting a fresh key above the reported head puts it at the front of the reversed +complete keys, so the reported keys are no longer a prefix. -/ +theorem necessary_above_head_of_rev_negTrunc_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (_hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) + {k : Key} (hkH : res.keys.head hne < k) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set H : Key := res.keys.head hne with hHdef + set L : Key := res.keys.getLast hne with hLdef + have hoff : (reportedBelowBegin r res : Int) + r.begin.offset - 1 ≤ 0 := + reportedBelowBegin_add_offset_le_one hcomp₀ hmore hrev hne hcross hendkey hendoff + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hHge : ∀ x ∈ res.keys, x ≤ H := by + intro x hx; rw [hHdef]; exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hk_notin : k ∉ res.keys := by + intro hx + have hle := hHge k hx + have hleH : k ≤ res.keys.head hne := by rw [← hHdef]; exact hle + exact absurd hleH (not_le.mpr hkH) + -- the witness database of reported keys in ascending order + have ha_sorted : res.keys.reverse.SortedLT := hsort.reverse + let db_a : DB := ⟨res.keys.reverse, ha_sorted⟩ + have ha_keys : db_a.keys = res.keys.reverse := rfl + have haresolve : resolve db_a r.begin ≤ ((L : Key) : WithTop Key) := by + have hanchor : r.begin.anchor db_a = (reportedBelowBegin r res : Int) + r.begin.offset - 1 := by + have hcnt : cnt db_a r.begin.key = reportedBelowBegin r res := by + unfold cnt reportedBelowBegin + rw [ha_keys, List.countP_reverse] + change (cnt db_a r.begin.key : Int) + r.begin.offset - 1 = _ + rw [hcnt] + have hle : r.begin.anchor db_a ≤ 0 := by rw [hanchor]; linarith + by_cases hneg : r.begin.anchor db_a < 0 + · rw [resolve_eq_nil hneg] + exact WithTop.coe_le_coe.mpr (nil_le_key L) + · have hanc0 : r.begin.anchor db_a = 0 := le_antisymm hle (le_of_not_gt hneg) + have hlen : 0 < db_a.keys.length := by + rw [ha_keys, List.length_reverse] + exact List.length_pos_of_ne_nil hne + have h0lt : (r.begin.anchor db_a).toNat < db_a.keys.length := by + rw [hanc0]; simpa using hlen + have hget0 : db_a.keys[(r.begin.anchor db_a).toNat] = L := by + have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanc0]; rfl + have h0 : db_a.keys[(r.begin.anchor db_a).toNat]? = some L := by + rw [hidx, ha_keys, ← List.head?_eq_getElem?] + rw [List.head?_reverse, List.getLast?_eq_some_getLast hne] + have h1 := List.getElem?_eq_getElem h0lt + rw [h0] at h1 + exact (Option.some.inj h1).symm + rw [resolve_eq_getElem (le_of_not_gt hneg) h0lt, hget0] + have hre : resolve db_a r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_top hendkey hendoff + have hck_a : RangeRead.completeKeys db_a r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev, hre] + simp only [ite_true] + have hpred : db_a.keys.filter (fun x => decide + (resolve db_a r.begin ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < (⊤ : WithTop Key))) = db_a.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have hx' : L ≤ x := by + rw [ha_keys] at hx + exact hLle x (List.mem_reverse.mp hx) + exact ⟨le_trans haresolve (WithTop.coe_le_coe.mpr hx'), WithTop.coe_lt_top x⟩ + rw [hpred, ha_keys, List.reverse_reverse] + have hacompat : RangeRead.Compatible db_a r res := by + refine (compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mpr ⟨hcomp₀.1, ?_⟩ + rw [hck_a, List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hLle x hx + have hk_notin_a : k ∉ db_a.keys := by + rw [ha_keys, List.mem_reverse] + exact hk_notin + -- insert the target key above the head + let db_b : DB := db_a.insert k hk_notin_a + have hb_keys : db_b.keys = res.keys.reverse ++ [k] := by + rw [DB.insert_keys] + exact orderedInsert_append_of_forall_lt' (fun x hx => by + have hx' : x ∈ res.keys := by + have hx'' : x ∈ res.keys.reverse := by simpa [db_a] using hx + exact List.mem_reverse.mp hx'' + exact lt_of_le_of_lt (hHge x hx') hkH) + have hcnt_b : cnt db_b r.begin.key ≤ reportedBelowBegin r res + 1 := by + unfold cnt reportedBelowBegin + rw [hb_keys, List.countP_append] + have h1 : res.keys.reverse.countP + (fun x => decide (((x : Key) : WithTop Key) < r.begin.key)) = + res.keys.countP (fun x => decide (((x : Key) : WithTop Key) < r.begin.key)) := + List.countP_reverse + rw [h1] + have h2 : ([k].countP (fun x => decide (((x : Key) : WithTop Key) < r.begin.key))) ≤ 1 := by + rw [List.countP_singleton] + split <;> omega + omega + have hanchor_b : r.begin.anchor db_b ≤ ((res.keys.reverse.length : ℕ) : Int) := by + change (cnt db_b r.begin.key : Int) + r.begin.offset - 1 ≤ _ + have h1 : (cnt db_b r.begin.key : Int) ≤ (reportedBelowBegin r res : Int) + 1 := by + exact_mod_cast hcnt_b + have hlen1 : (1 : Int) ≤ ((res.keys.reverse.length : ℕ) : Int) := by + have hp : 0 < res.keys.reverse.length := by + rw [List.length_reverse] + exact List.length_pos_of_ne_nil hne + omega + linarith + have hrb_b : resolve db_b r.begin ≤ ((k : Key) : WithTop Key) := by + have hlenpos : 0 < res.keys.reverse.length := by + rw [List.length_reverse] + exact List.length_pos_of_ne_nil hne + have hkb : res.keys.reverse.length < db_b.keys.length := by + rw [hb_keys, List.length_append]; simp + have hkget : db_b.keys[res.keys.reverse.length] = k := by + have hkget? : db_b.keys[res.keys.reverse.length]? = some k := by + rw [hb_keys] + simp + have h1 := List.getElem?_eq_getElem hkb + rw [hkget?] at h1 + exact (Option.some.inj h1).symm + have hclamp : clampIndex db_b r.begin ≤ res.keys.reverse.length := by + unfold clampIndex + have htoNat : (r.begin.anchor db_b).toNat ≤ res.keys.reverse.length := by + by_cases h : 0 ≤ r.begin.anchor db_b + · exact (Int.toNat_le).mpr hanchor_b + · have hz : (r.begin.anchor db_b).toNat = 0 := by omega + rw [hz]; omega + exact le_trans (Nat.min_le_left _ _) htoNat + have hle := (resolve_le_getElem_iff (db := db_b) (s := r.begin) hkb).mpr hclamp + rwa [hkget] at hle + have hkb_mem : k ∈ RangeRead.completeKeys db_b r := by + rw [RangeRead.mem_completeKeys] + have hre_b : resolve db_b r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_top hendkey hendoff + exact ⟨DB.mem_insert_self db_a k hk_notin_a, hrb_b, by rw [hre_b]; exact WithTop.coe_lt_top k⟩ + have hck_pair : (RangeRead.completeKeys db_b r).Pairwise (· > ·) := by + have h := RangeRead.completeKeys_pairwise (db := db_b) (r := r) + rw [hrev] at h + exact h + have hhead_max : ∀ x ∈ RangeRead.completeKeys db_b r, x ≤ k := by + intro x hx + have hxdb := (RangeRead.mem_completeKeys.mp hx).1 + rw [hb_keys, List.mem_append, List.mem_singleton] at hxdb + rcases hxdb with hx' | rfl + · have hx'' : x ∈ res.keys := by + have hxx : x ∈ res.keys.reverse := by simpa [db_a] using hx' + exact List.mem_reverse.mp hxx + exact le_of_lt (lt_of_le_of_lt (hHge x hx'') hkH) + · exact le_rfl + have hhead_eq : (RangeRead.completeKeys db_b r).head (List.ne_nil_of_mem hkb_mem) = k := by + apply le_antisymm (hhead_max _ (List.head_mem _)) + exact (hck_pair.imp le_of_lt).rel_head hkb_mem + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db_a db_b := + (ConflictRange.keys_insert_of_not_mem cr db_a hk_notin_a hkcr).symm + have hcomp_b : RangeRead.Compatible db_b r res := (hs db_a db_b hagree).mp hacompat + have hpre := RangeRead.compatible_keys_prefix hcomp_b + have hhead := hpre.head hne + have hHk : H = k := by + rw [hHdef] + exact hhead.trans hhead_eq + have hHklt : H < k := by + rw [hHdef]; exact hkH + exact absurd hHk (ne_of_lt hHklt) + +/-- Every key at or above the least reported key is necessary in the reversed +negative-begin truncated sentinel-end cell. -/ +theorem necessary_of_ge_last_of_rev_negTrunc_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) + {k : Key} (hk : ((res.keys.getLast hne : Key) : WithTop Key) ≤ (k : WithTop Key)) : + Necessary r res k := by + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne + have hLk : L ≤ k := WithTop.coe_le_coe.mp hk + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hkH : k ≤ H + · exact necessary_of_between_result hread hLmem hHmem hLk hkH + · exact necessary_above_head_of_rev_negTrunc_top hread hmore hrev hne hbeg hcross + hendkey hendoff (by rw [← hHdef]; exact lt_of_not_ge hkH) + +/-- **Minimality of `[last, ⊤)` in the reversed negative-begin truncated sentinel-end +cell.** -/ +theorem minimal_topOfLast_of_rev_negTrunc_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) : + Minimal r res (topOfLast (res.keys.getLast hne)) := by + refine minimal_iff_sound_and_necessary.mpr + ⟨sound_topOfLast_of_rev_negTrunc_top hmore hrev hne hbeg hcross hendkey hendoff, ?_⟩ + intro k hk + have hk' := ConflictRange.mem_iff.mp hk + exact necessary_of_ge_last_of_rev_negTrunc_top hread hmore hrev hne hbeg hcross + hendkey hendoff hk'.1 + +/-- **Soundness of `[last, ⊤)` when the sentinel end has a nonpositive offset.** The +reduced read with begin set to offset one on `last` has the same stored keys at or above +`last`, so its already-total nonpositive-end reversed rule transfers soundness. -/ +theorem sound_topOfLast_of_rev_negTrunc_top_endNonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : r.end_.offset ≤ 0) : + Sound r res (topOfLast (res.keys.getLast hne)) := by + set L : Key := res.keys.getLast hne with hLdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + let r' : RangeRead := + { r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } } + have hr'_rev : r'.reverse = true := by simp [r', hrev] + have hr'_end : r'.end_ = r.end_ := rfl + have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) := rfl + obtain ⟨db₀, hcomp₀⟩ := hread + have hcomp₀' : RangeRead.Compatible db₀ r' res := by + have hfil₀ : (RangeRead.completeKeys db₀ r).filter (fun k => decide (L ≤ k)) = res.keys := + ((compatible_iff_truncated_keys_rev (db := db₀) hrev hmore hne).mp hcomp₀).2 + have hLfilt₀ : L ∈ (RangeRead.completeKeys db₀ r).filter (fun k => decide (L ≤ k)) := by + rw [hfil₀]; exact hLmem + have hrb₀ : resolve db₀ r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt₀).1).2.1 + have hLdb₀ : L ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hLmem + have hres'₀ : resolve db₀ r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb₀ + have hfeq := completeKeys_filter_ge_eq_of_both_le (db := db₀) (r₁ := r) (r₂ := r') + hrev hr'_rev hr'_end hrb₀ (le_of_eq hres'₀) + rw [compatible_iff_truncated_keys_rev (db := db₀) hr'_rev hmore hne] + exact ⟨by simpa [r', RangeRead.withinLimit] using hcomp₀.1, by rw [← hfeq]; exact hfil₀⟩ + have hread' : Readable r' res := ⟨db₀, hcomp₀'⟩ + have hgetLast : res.keys.getLastD ([] : Key) = L := by + rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hS' : Sound r' res (topOfLast L) := by + have hmin' := minimal_truncatedRangeRev_of_end_offset_nonpos (r := r') hread' hmore + hr'_rev (by rfl : r'.begin.offset = 1) hendoff + rw [hgetLast] at hmin' + have hcr : truncatedRangeRev r' L = topOfLast L := by + unfold truncatedRangeRev topOfLast + rw [hr'_end, hendkey] + rw [hcr] at hmin' + exact hmin'.sound + have step : ∀ db db', AgreeOn (topOfLast L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by + rw [hfil]; exact hLmem + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1 + have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem + have hLdb' : L ∈ db'.keys := by + have hfilEq : db'.keys.filter (fun k => decide (L ≤ k)) = + db.keys.filter (fun k => decide (L ≤ k)) := by + have h := hag' + unfold AgreeOn at h + rw [topOfLast_keys, topOfLast_keys] at h + exact h.symm + have hmem : L ∈ db'.keys.filter (fun k => decide (L ≤ k)) := by + rw [hfilEq]; exact List.mem_filter.mpr ⟨hLdb, by simp⟩ + exact (List.mem_filter.mp hmem).1 + have hres'_db : resolve db r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb + have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by + rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb' + have hfeq_db := completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r) (r₂ := r') + hrev hr'_rev hr'_end hrb (le_of_eq hres'_db) + have hcomp'_db : RangeRead.Compatible db r' res := by + rw [compatible_iff_truncated_keys_rev (db := db) hr'_rev hmore hne] + exact ⟨by simpa [r', RangeRead.withinLimit] using hcomp.1, by rw [← hfeq_db]; exact hfil⟩ + have hcomp'_db' : RangeRead.Compatible db' r' res := (hS' db db' hag').mp hcomp'_db + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + resolve_begin_le_of_agree_topOfLast hcomp hmore hrev hne hcross hag' + have hfil' : (RangeRead.completeKeys db' r').filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp hcomp'_db' |>.2 + have hfeq_db' := completeKeys_filter_ge_eq_of_both_le (db := db') (r₁ := r) (r₂ := r') + hrev hr'_rev hr'_end hrb' (le_of_eq hres'_db') + rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne] + exact ⟨by simpa [r', RangeRead.withinLimit] using hcomp'_db'.1, by rw [hfeq_db']; exact hfil'⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Above-head necessity at a sentinel end for any end offset.** The general +`necessary_of_end_key_top_rev` already covers both signs of the end offset. -/ +theorem necessary_of_ge_last_of_rev_negTrunc_top_any {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hendkey : r.end_.key = (⊤ : WithTop Key)) + {k : Key} (hk : ((res.keys.getLast hne : Key) : WithTop Key) ≤ (k : WithTop Key)) : + Necessary r res k := by + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne + have hHmem : H ∈ res.keys := by rw [hHdef]; exact List.head_mem hne + have hLk : L ≤ k := WithTop.coe_le_coe.mp hk + by_cases hkres : k ∈ res.keys + · exact necessary_of_mem_result hread hkres + · by_cases hkH : k ≤ H + · exact necessary_of_between_result hread hLmem hHmem hLk hkH + · exact necessary_of_end_key_top_rev hread hrev hne hendkey + (le_of_lt (lt_of_not_ge hkH)) + +/-- **Minimality of `[last, ⊤)` at a sentinel end for any end offset.** -/ +theorem minimal_topOfLast_of_rev_negTrunc_top_any {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key) + (hendkey : r.end_.key = (⊤ : WithTop Key)) : + Minimal r res (topOfLast (res.keys.getLast hne)) := by + refine minimal_iff_sound_and_necessary.mpr ⟨?_, ?_⟩ + · rcases lt_or_ge r.end_.offset 1 with hoff | hoff + · exact sound_topOfLast_of_rev_negTrunc_top_endNonpos hread hmore hrev hne hcross + hendkey (by omega) + · exact sound_topOfLast_of_rev_negTrunc_top hmore hrev hne hbeg hcross hendkey hoff + · intro k hk + exact necessary_of_ge_last_of_rev_negTrunc_top_any hread hrev hne hendkey + (ConflictRange.mem_iff.mp hk).1 + +/-- **Soundness of `[last, ⊤)` with a sentinel end and no cross hypothesis.** When the +begin key lies at or below the reported last, the begin resolution is at or below the +last because the begin offset is nonpositive; otherwise the counting argument of +`sound_topOfLast_of_rev_negTrunc_top` applies. -/ +theorem sound_topOfLast_of_rev_negTrunc_top_nocross {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) : + Sound r res (topOfLast (res.keys.getLast hne)) := by + by_cases hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key + · exact sound_topOfLast_of_rev_negTrunc_top hmore hrev hne hbeg hcross hendkey hendoff + · set L : Key := res.keys.getLast hne with hLdef + have hkeyle : r.begin.key ≤ ((L : Key) : WithTop Key) := le_of_not_gt hcross + have step : ∀ db db', AgreeOn (topOfLast L) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := + (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2 + have hre : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_top hendkey hendoff + have hre' : resolve db' r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_top hendkey hendoff + have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + le_trans (resolve_le_key_of_offset_nonpos (le_of_lt hbeg)) hkeyle + have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) := + le_trans (resolve_le_key_of_offset_nonpos (le_of_lt hbeg)) hkeyle + have hfilEq : db'.keys.filter (fun k => decide (L ≤ k)) = + db.keys.filter (fun k => decide (L ≤ k)) := by + have h := hag' + unfold AgreeOn at h + rw [topOfLast_keys, topOfLast_keys] at h + exact h.symm + have hdb'filt := completeKeys_filter_ge_eq_of_resolve_le (db := db') (r := r) + (last := L) hrev hre' hrb' + have hdbfilt := completeKeys_filter_ge_eq_of_resolve_le (db := db) (r := r) + (last := L) hrev hre hrb + refine (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr + ⟨hcomp.1, ?_⟩ + rw [hdb'filt, hfilEq, ← hdbfilt, hfil] + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Minimality of `[last, ⊤)` at a sentinel end with no cross hypothesis.** Only the +begin key relative to the reported last changes; necessity is independent of it. -/ +theorem minimal_topOfLast_of_rev_negTrunc_top_nocross {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbeg : r.begin.offset < 0) + (hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset) : + Minimal r res (topOfLast (res.keys.getLast hne)) := by + refine minimal_iff_sound_and_necessary.mpr + ⟨sound_topOfLast_of_rev_negTrunc_top_nocross hmore hrev hne hbeg hendkey hendoff, ?_⟩ + intro k hk + exact necessary_of_ge_last_of_rev_negTrunc_top_any hread hrev hne hendkey + (ConflictRange.mem_iff.mp hk).1 + +/-- The reversed, crossed, negative-begin, truncated cell with sentinel end key. -/ +abbrev negCrossTruncRevTop (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.key = (⊤ : WithTop Key) + +/-- The closed form of the reversed negative-begin truncated sentinel-end cell. -/ +noncomputable def closedFormNegCrossTruncRevTop (_r : RangeRead) (res : Result) : + ConflictRange := + topOfLast (res.keys.getLastD ([] : Key)) + +/-- **The reversed negative-begin truncated sentinel-end closed form is minimal.** -/ +theorem minimal_closedForm_of_negCross_trunc_rev_top {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncRevTop r res) : + Minimal r res (closedFormNegCrossTruncRevTop r res) := by + obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hendkey⟩ := h + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlast]; exact hcrossD + unfold closedFormNegCrossTruncRevTop + rw [hlast] + exact minimal_topOfLast_of_rev_negTrunc_top_any hread hmore hrev hne hbeg hcross hendkey + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncStrictTop.lean b/FormalConflictRange/NegCrossTruncStrictTop.lean new file mode 100644 index 0000000..1ac5c02 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncStrictTop.lean @@ -0,0 +1,524 @@ +/- +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.NegCrossTruncClampLastGe +import FormalConflictRange.Empty + +/-! +# The crossed negative-begin truncated cell with the end sentinel + +This module closes a cell left open by the compact residual: a forward, crossed, +truncated read with a negative begin offset, a nonpositive end offset, the reported +keys below `begin.key`, and the end key at the sentinel `⊤`. The end key being the +sentinel means the end selector resolves to an ordinary stored key (since its offset is +nonpositive) and the whole key space above `begin.key` is available for padding. + +The minimal sound range is `[⊥, ⊤)`: every genuine key is necessary. Below the reported +head, inserting the key prepends it to the complete window; at or above `begin.key`, a +witness database stores the reported keys together with `1 - end.offset` pad keys above +`begin.key`, so erasing the target shortens the complete window and breaks the reported +prefix. Convexity between the reported last and `begin.key` fills the gap. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- A forward truncated, crossed, negative-begin read with a nonpositive end offset, a +clamped begin selector, the reported keys below `begin.key`, and the end key at the +sentinel. -/ +def negCrossTruncStrictTop (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧ + r.end_.key = (⊤ : WithTop Key) ∧ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧ + r.begin.key < r.end_.key + +/-- Storing the reported keys followed by pad keys at or above `begin.key` gives a +compatible database whose begin selector clamps and whose end selector lands on the +first pad key. -/ +theorem compat_of_pad_top {r : RangeRead} {res : Result} {F : List Key} {m : ℕ} + (hread : Readable r res) (hne : res.keys ≠ []) (hrev : r.reverse = false) + (hmore : res.more = true) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (htop : r.end_.key = (⊤ : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hallB : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key) + (hFsorted : F.SortedLT) (hFlen : F.length = m) + (hm : m = (1 - r.end_.offset).toNat) + (hFne : F ≠ []) (hFge : ∀ x ∈ F, r.begin.key ≤ ((x : Key) : WithTop Key)) + (hFlast : ∀ x ∈ F, res.keys.getLast hne < x) : + ∃ dbw : DB, dbw.keys = res.keys ++ F ∧ RangeRead.Compatible dbw r res ∧ + resolve dbw r.begin = (([] : Key) : WithTop Key) ∧ + resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hle_last : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hDsort : (res.keys ++ F).SortedLT := by + refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩ + intro a ha b hb + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hallB a ha) (hFge b hb)) + let dbw : DB := ⟨res.keys ++ F, hDsort⟩ + have hdbw_keys : dbw.keys = res.keys ++ F := rfl + have hcountB : dbw.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hdbw_keys, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have h2 : F.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hFge x hx) + rw [h1, h2, Nat.add_zero] + have hanchorB : r.begin.anchor dbw < 0 := by + rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcountB] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hrb : resolve dbw r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchorB + have hlenD : (res.keys ++ F).length = res.keys.length + m := by + rw [List.length_append, hFlen] + have hm_int : (m : Int) = 1 - r.end_.offset := by + rw [hm]; exact Int.toNat_of_nonneg (by omega) + have hanchorE : r.end_.anchor dbw = (res.keys.length : Int) := by + rw [anchor_of_top (s := r.end_) htop, hdbw_keys, hlenD] + push_cast + rw [hm_int]; ring + have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by + change (res.keys ++ F)[res.keys.length]? = some (F.head hFne) + rw [List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] + have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchorE]; omega)] + rw [hanchorE, Int.toNat_natCast, hDget?] + have hltF : ((res.keys.getLast hne : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [hre] + exact WithTop.coe_lt_coe.mpr (hFlast (F.head hFne) (List.head_mem hFne)) + have hckw : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys := by + rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev (H := ([] : Key)) hrb hltF] + rw [hdbw_keys, List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x ≤ res.keys.getLast hne)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨coe_nil_le_key_withTop _, hle_last x hx⟩ + have hF_fil : F.filter (fun x : Key => decide + (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x ≤ res.keys.getLast hne)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr (hFlast x hx) + rw [hres_fil, hF_fil, List.append_nil] + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨hcomp₀.1, hckw⟩ + exact ⟨dbw, hdbw_keys, hcompw, hrb, hre⟩ + +/-- Below the reported head, inserting the key into the padded witness prepends it to +the complete window, breaking the truncated prefix. -/ +theorem exists_witness_below_head_of_negCross_trunc_strict_top {r : RangeRead} {res : Result} + {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (htop : r.end_.key = (⊤ : WithTop Key)) + (hBltTop : r.begin.key < (⊤ : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hallB : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key) + (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + ∃ dbw : DB, ∃ hkdb : k ∉ dbw.keys, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hhead_le : ∀ x ∈ res.keys, res.keys.head hne ≤ x := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_head hx + have hk_head : k < res.keys.head hne := WithTop.coe_lt_coe.mp hk + set m : ℕ := (1 - r.end_.offset).toNat with hm + have hm1 : 1 ≤ m := by rw [hm]; omega + have hcard : (m : ℕ∞) ≤ intervalCard r.begin.key (⊤ : WithTop Key) := by + rw [intervalCard_top_right (ne_of_lt hBltTop)] + exact le_top + obtain ⟨dbf, hdbfIn, hdbfLen⟩ := + exists_db_keysIn (B := r.begin.key) (E := (⊤ : WithTop Key)) (m := m) hcard + set F : List Key := dbf.keys with hF + have hFsorted : F.SortedLT := dbf.sorted + have hFlen : F.length = m := hdbfLen + have hFne : F ≠ [] := by + intro h; rw [h] at hFlen; simp at hFlen; omega + have hFge : ∀ x ∈ F, r.begin.key ≤ ((x : Key) : WithTop Key) := + fun x hx => (hdbfIn x hx).1 + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := + hallB _ (List.getLast_mem hne) + have hFlast : ∀ x ∈ F, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B (hFge x hx)) + obtain ⟨dbw, hdbw_keys, hcompw, _hrb, _hre⟩ := + compat_of_pad_top ⟨db₀, hcomp₀⟩ hne hrev hmore hbegin hend htop hclamp hallB hFsorted hFlen + hm hFne hFge hFlast + have hk_not_res : k ∉ res.keys := fun hx => absurd (hhead_le k hx) (not_le.mpr hk_head) + have hhead_lt_B : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := + hallB _ (List.head_mem hne) + have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hk hhead_lt_B + have hk_not_F : k ∉ F := fun hx => absurd (hFge k hx) (not_le.mpr hk_lt_B) + have hkdb : k ∉ dbw.keys := by + rw [hdbw_keys] + simp [hk_not_res, hk_not_F] + have hins_keys : (dbw.insert k hkdb).keys = k :: (res.keys ++ F) := by + rw [DB.insert_keys, hdbw_keys] + cases hres : res.keys with + | nil => exact absurd hres hne + | cons a t => + have hka : k ≤ a := le_of_lt (WithTop.coe_lt_coe.mp (by simpa [hres] using hk)) + rw [List.cons_append] + rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) (l := t ++ F) hka] + have hcountB' : (dbw.insert k hkdb).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res + 1 := by + rw [hins_keys, List.countP_cons, List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + have h2 : F.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hFge x hx) + simp [h1, h2, hk_lt_B] + have hanchorB' : r.begin.anchor (dbw.insert k hkdb) ≤ 0 := by + rw [anchor_eq_count_sub_slack (dbw.insert k hkdb) r (le_of_lt hbegin), hcountB'] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hlen' : (dbw.insert k hkdb).keys.length = res.keys.length + m + 1 := by + rw [hins_keys, List.length_cons, List.length_append, hFlen] + have hm_int : (m : Int) = 1 - r.end_.offset := by + rw [hm]; exact Int.toNat_of_nonneg (by omega) + have hanchorE' : r.end_.anchor (dbw.insert k hkdb) = (res.keys.length : Int) + 1 := by + rw [anchor_of_top (s := r.end_) htop, hlen'] + push_cast + rw [hm_int]; ring + have hD'get? : (dbw.insert k hkdb).keys[res.keys.length + 1]? = some (F.head hFne) := by + rw [hins_keys, List.getElem?_cons_succ] + rw [List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] + have hre' : resolve (dbw.insert k hkdb) r.end_ = + ((F.head hFne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchorE']; omega)] + have hnat : ((res.keys.length : Int) + 1).toNat = res.keys.length + 1 := by + rw [← Nat.cast_one, ← Nat.cast_add, Int.toNat_natCast] + rw [hanchorE', hnat, hD'get?] + have hltF' : ((res.keys.getLast hne : Key) : WithTop Key) < + resolve (dbw.insert k hkdb) r.end_ := by + rw [hre'] + exact WithTop.coe_lt_coe.mpr (hFlast (F.head hFne) (List.head_mem hFne)) + have hck' : (RangeRead.completeKeys (dbw.insert k hkdb) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = k :: res.keys := by + by_cases hneg : r.begin.anchor (dbw.insert k hkdb) < 0 + · have hrb' : resolve (dbw.insert k hkdb) r.begin = (([] : Key) : WithTop Key) := + resolve_eq_nil hneg + rw [completeKeys_filter_le_of_resolve_begin (db := dbw.insert k hkdb) hrev + (H := ([] : Key)) hrb' hltF'] + rw [hins_keys, List.filter_cons_of_pos] + · rw [List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x ≤ res.keys.getLast hne)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨coe_nil_le_key_withTop _, (hsort.pairwise.imp le_of_lt).rel_getLast hx⟩ + have hF_fil : F.filter (fun x : Key => decide + (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x ≤ res.keys.getLast hne)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr (hFlast x hx) + rw [hres_fil, hF_fil, List.append_nil] + · rw [decide_eq_true_eq] + exact ⟨coe_nil_le_key_withTop _, le_of_lt (lt_of_lt_of_le hk_head hhead_le_last)⟩ + · have hzero : r.begin.anchor (dbw.insert k hkdb) = 0 := by omega + have hltlen : (r.begin.anchor (dbw.insert k hkdb)).toNat < + (dbw.insert k hkdb).keys.length := by + rw [hzero, hins_keys]; simp + have hins_ord : dbw.keys.orderedInsert (fun x1 x2 : Key => x1 ≤ x2) k = + k :: dbw.keys := by + rw [← DB.insert_keys (hk := hkdb)] + rw [hins_keys, hdbw_keys] + have hrb' : resolve (dbw.insert k hkdb) r.begin = ((k : Key) : WithTop Key) := by + rw [resolve_eq_ite, hzero] + simp [hins_ord] + rw [completeKeys_filter_le_of_resolve_begin (db := dbw.insert k hkdb) hrev + (H := k) hrb' hltF'] + rw [hins_keys, List.filter_cons_of_pos] + · rw [List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + (((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x ≤ res.keys.getLast hne)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (le_trans (le_of_lt hk_head) (hhead_le x hx)), + (hsort.pairwise.imp le_of_lt).rel_getLast hx⟩ + have hF_fil : F.filter (fun x : Key => decide + (((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x ≤ res.keys.getLast hne)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr (hFlast x hx) + rw [hres_fil, hF_fil, List.append_nil] + · rw [decide_eq_true_eq] + exact ⟨le_rfl, le_of_lt (lt_of_lt_of_le hk_head hhead_le_last)⟩ + have hnot : ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by + intro hc + have heq := ((compatible_iff_truncated_keys hrev hmore hne).mp hc).2 + rw [hck'] at heq + have hlen := congrArg List.length heq + rw [List.length_cons] at hlen + omega + exact ⟨dbw, hkdb, hcompw, hnot⟩ + +/-- At or above `begin.key`, erasing the target from the padded witness drops the end +resolution onto the reported last, so the complete window loses its last key. -/ +theorem exists_witness_above_begin_of_negCross_trunc_strict_top {r : RangeRead} {res : Result} + {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0) + (hend : r.end_.offset ≤ 0) + (htop : r.end_.key = (⊤ : WithTop Key)) + (hBltTop : r.begin.key < (⊤ : WithTop Key)) + (hclamp : reportedBelowBegin r res ≤ beginSlack r) + (hallB : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key) + (hkB : r.begin.key ≤ ((k : Key) : WithTop Key)) : + ∃ dbw : DB, k ∈ dbw.keys ∧ RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hle_last : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + set m : ℕ := (1 - r.end_.offset).toNat with hm + have hm1 : 1 ≤ m := by rw [hm]; omega + have hcard : ((m - 1 + 1 : ℕ) : ℕ∞) ≤ intervalCard r.begin.key (⊤ : WithTop Key) := by + rw [Nat.sub_add_cancel hm1, intervalCard_top_right (ne_of_lt hBltTop)] + exact le_top + obtain ⟨dba, hdbaIn, hknot, hdbaLen⟩ := + exists_db_keysIn_avoid (B := r.begin.key) (E := (⊤ : WithTop Key)) (k := k) (m := m - 1) + hcard + have hrawNodup : (k :: dba.keys).Nodup := List.nodup_cons.mpr ⟨hknot, dba.nodup⟩ + set F : List Key := (k :: dba.keys).insertionSort (· ≤ ·) with hF + have hFperm : (k :: dba.keys).Perm F := (List.perm_insertionSort (· ≤ ·) _).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) (k :: dba.keys)) + (hFperm.nodup_iff.mp hrawNodup) + have hFlen : F.length = m := by + rw [hF, List.length_insertionSort, List.length_cons, hdbaLen]; omega + have hFk : k ∈ F := hFperm.mem_iff.mp (List.mem_cons_self ..) + have hFge : ∀ x ∈ F, r.begin.key ≤ ((x : Key) : WithTop Key) := by + intro x hx + have hx' := hFperm.mem_iff.mpr hx + rcases List.mem_cons.mp hx' with rfl | hx'' + · exact hkB + · exact (hdbaIn x hx'').1 + have hlast_lt_B : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := + hallB _ (List.getLast_mem hne) + have hFlast : ∀ x ∈ F, res.keys.getLast hne < x := by + intro x hx + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B (hFge x hx)) + have hFne : F ≠ [] := by + intro h; rw [h] at hFlen; simp at hFlen; omega + obtain ⟨dbw, hdbw_keys, hcompw, _hrb, _hre⟩ := + compat_of_pad_top ⟨db₀, hcomp₀⟩ hne hrev hmore hbegin hend htop hclamp hallB hFsorted hFlen + hm hFne hFge hFlast + have hlast_lt_k : res.keys.getLast hne < k := by + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlast_lt_B hkB) + have hk_not_res : k ∉ res.keys := fun hx => absurd (hle_last k hx) (not_le.mpr hlast_lt_k) + have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by + rw [DB.erase_keys, hdbw_keys] + exact List.erase_append_right F hk_not_res + have hF'ge : ∀ x ∈ F.erase k, r.begin.key ≤ ((x : Key) : WithTop Key) := + fun x hx => hFge x (List.mem_of_mem_erase hx) + have hF'zero : (F.erase k).countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + simp only [decide_eq_true_eq] + exact not_lt.mpr (hF'ge x hx) + have hcnt' : (dbw.erase k).keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := by + rw [hD', List.countP_append] + have h1 : res.keys.countP + (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = + reportedBelowBegin r res := rfl + rw [h1, hF'zero, Nat.add_zero] + have hanchor' : r.begin.anchor (dbw.erase k) < 0 := by + rw [anchor_eq_count_sub_slack (dbw.erase k) r (le_of_lt hbegin), hcnt'] + have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp + omega + have hrb' : resolve (dbw.erase k) r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor' + have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by + rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen] + omega + have hm_int : (m : Int) = 1 - r.end_.offset := by + rw [hm]; exact Int.toNat_of_nonneg (by omega) + have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by + rw [anchor_of_top (s := r.end_) htop, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int]; ring + have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne + have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by + rw [Nat.cast_sub ht1]; norm_num + have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_end', hEqInt, Int.toNat_natCast] + have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = + some (res.keys.getLast hne) := by + rw [hD', List.getElem?_append] + rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)] + rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)] + congr 1 + rw [List.getLast_eq_getElem hne] + have hre' : resolve (dbw.erase k) r.end_ = ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)] + rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?] + have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hrb', hre'] + have hpred : ∀ x ∈ (dbw.erase k).keys, + decide (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < (((res.keys.getLast hne) : Key) : WithTop Key)) = + decide (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x < res.keys.getLast hne) := by + intro x _ + rw [decide_eq_decide] + exact and_congr_right fun _ => WithTop.coe_lt_coe + rw [List.filter_congr hpred, hD', List.filter_append] + conv_lhs => arg 1; arg 2; rw [← List.dropLast_append_getLast hne] + rw [List.filter_append] + have hfil : res.keys.dropLast.filter (fun x : Key => decide + (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x < res.keys.getLast hne)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨coe_nil_le_key_withTop _, by + have h := hsort.pairwise.rel_dropLast_getLast hx + simpa using h⟩ + rw [hfil] + have hFnil : (F.erase k).filter (fun x : Key => decide + (((([] : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + x < res.keys.getLast hne)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_lt.mpr (le_of_lt (hFlast x (List.mem_of_mem_erase hx))) + rw [hFnil, List.append_nil] + simp + have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + intro h + have heq := h.2 + rw [hckE] at heq + have hfil : res.keys.dropLast.filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact le_of_lt (hsort.pairwise.rel_dropLast_getLast hx) + rw [hfil] at heq + have hlen := congrArg List.length heq + rw [List.length_dropLast] at hlen + omega + exact ⟨dbw, by rw [hdbw_keys]; exact List.mem_append.mpr (Or.inr hFk), hcompw, hnot⟩ + +/-- The sentinel-end strict cell is minimal with the full range `[⊥, ⊤)`. -/ +theorem minimal_fullRange_of_negCross_trunc_strict_top {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncStrictTop r res) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + obtain ⟨hmore, hrev, hbegin, hne, _hcrossD, hend, hclamp, htop, hlastD, hBltE⟩ := h + have hreadP : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlastD' : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hlastB : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by + rw [← hlastD']; exact hlastD + have hBltTop : r.begin.key < (⊤ : WithTop Key) := by + rw [← htop]; exact hBltE + have hallB : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by + intro x hx + exact lt_of_le_of_lt + (WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) hlastB + have hbExists : ∃ b : Key, r.begin.key = ((b : Key) : WithTop Key) := by + obtain ⟨b, hb⟩ := WithTop.ne_top_iff_exists.mp (ne_of_lt hBltTop) + exact ⟨b, hb.symm⟩ + obtain ⟨b, hb⟩ := hbExists + refine (minimal_iff_sound_and_necessary).mpr ⟨sound_fullRange r res, ?_⟩ + intro k _ + by_cases hkhead : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · obtain ⟨dbw, hkdb, hcomp, hnot⟩ := + exists_witness_below_head_of_negCross_trunc_strict_top hreadP hrev hmore hne hbegin hend + htop hBltTop hclamp hallB hkhead + exact necessary_of_toggle ⟨dbw, hcomp, by + rw [toggle_eq_insert_of_not_mem hkdb]; exact hnot⟩ + · have hhead_le : res.keys.head hne ≤ k := + WithTop.coe_le_coe.mp (le_of_not_gt hkhead) + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hreadP (List.head_mem hne) (List.getLast_mem hne) + hhead_le hklast + · have hlast_lt_k : res.keys.getLast hne < k := lt_of_not_ge hklast + by_cases hkB : ((k : Key) : WithTop Key) < r.begin.key + · have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result hreadP (List.getLast_mem hne) + have hB_nec : Necessary r res b := by + obtain ⟨dbw, hkdb, hcomp, hnot⟩ := + exists_witness_above_begin_of_negCross_trunc_strict_top hreadP hrev hmore hne hbegin + hend htop hBltTop hclamp hallB (by rw [hb]) + exact necessary_of_toggle ⟨dbw, hcomp, by + rw [toggle_eq_erase_of_mem hkdb]; exact hnot⟩ + have hk_le_b : k ≤ b := by + rw [hb] at hkB + exact le_of_lt (WithTop.coe_lt_coe.mp hkB) + exact Necessary.of_between hlast_nec hB_nec (le_of_lt hlast_lt_k) hk_le_b + · have hkge : r.begin.key ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkB + obtain ⟨dbw, hkdb, hcomp, hnot⟩ := + exists_witness_above_begin_of_negCross_trunc_strict_top hreadP hrev hmore hne hbegin + hend htop hBltTop hclamp hallB hkge + exact necessary_of_toggle ⟨dbw, hcomp, by + rw [toggle_eq_erase_of_mem hkdb]; exact hnot⟩ + +/-- The compact closed form for the sentinel-end strict cell. -/ +noncomputable def closedFormNegCrossTruncStrictTop (r : RangeRead) (_res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + +/-- The compact sentinel-end strict closed form is minimal. -/ +theorem minimal_closedForm_of_negCross_trunc_strict_top {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncStrictTop r res) : + Minimal r res (closedFormNegCrossTruncStrictTop r res) := + minimal_fullRange_of_negCross_trunc_strict_top hread h + +end FormalConflictRange diff --git a/FormalConflictRange/NegCrossTruncTopTop.lean b/FormalConflictRange/NegCrossTruncTopTop.lean new file mode 100644 index 0000000..7f91514 --- /dev/null +++ b/FormalConflictRange/NegCrossTruncTopTop.lean @@ -0,0 +1,1038 @@ +/- +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.NegCrossTruncStrictTop + +/-! +# The crossed negative-begin truncated cell with both selectors at the sentinel + +This module closes the last forward-truncated cell whose begin and end keys are both +the sentinel `⊤`: a forward read with a negative begin offset and a nonpositive end +offset. Both selectors count every stored key, so the complete window is a contiguous +block of the stored keys near the top. + +Writing `q = beginSlack r`, `p = 1 - end.offset` and `t = res.keys.length`, readability +forces `t + p ≤ q + 1`. In the equality branch `t + p = q + 1` the reported keys fill +the complete window exactly, and every key at or above the reported head is necessary: +the minimal range is `[head, ⊤)`. In the strict branch `t + p < q + 1` the reported +keys are a strict prefix, and every genuine key is necessary: the minimal range is +`[⊥, ⊤)`. +-/ + +open FormalConflictRange + +namespace FormalConflictRange + +/-- A forward truncated read with a negative begin offset, a nonpositive end offset, and +both selector keys at the sentinel `⊤`. -/ +def negCrossTruncTopTop (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧ + r.begin.key = (⊤ : WithTop Key) ∧ r.end_.key = (⊤ : WithTop Key) ∧ + r.end_.offset ≤ 0 + +/-- The closed form for the both-sentinel truncated cell: `[head, ⊤)` when the reported +keys fill the complete window (`t + p = q + 1`), and the full range otherwise. -/ +noncomputable def closedFormNegCrossTruncTopTop (r : RangeRead) (res : Result) : + ConflictRange := + if res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1 then + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + else ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + +namespace NegCrossTruncTopTop + +variable {r : RangeRead} {res : Result} + +theorem hmore (h : negCrossTruncTopTop r res) : res.more = true := h.1 +theorem hrev (h : negCrossTruncTopTop r res) : r.reverse = false := h.2.1 +theorem hbegin (h : negCrossTruncTopTop r res) : r.begin.offset < 0 := h.2.2.1 +theorem hne (h : negCrossTruncTopTop r res) : res.keys ≠ [] := h.2.2.2.1 +theorem hBtop (h : negCrossTruncTopTop r res) : r.begin.key = (⊤ : WithTop Key) := + h.2.2.2.2.1 +theorem hEtop (h : negCrossTruncTopTop r res) : r.end_.key = (⊤ : WithTop Key) := + h.2.2.2.2.2.1 +theorem hend (h : negCrossTruncTopTop r res) : r.end_.offset ≤ 0 := h.2.2.2.2.2.2 + +theorem beginSlack_pos_of_neg {r : RangeRead} (hbo : r.begin.offset < 0) : + 0 < beginSlack r := by + unfold beginSlack + have hnn : 0 ≤ 0 - r.begin.offset := by omega + have hcast : (((0 - r.begin.offset).toNat : ℕ) : ℤ) = 0 - r.begin.offset := + Int.toNat_of_nonneg hnn + omega + +theorem endPad_pos_of_nonpos {r : RangeRead} (hend : r.end_.offset ≤ 0) : + 0 < (1 - r.end_.offset).toNat := by + have hnn : 0 ≤ 1 - r.end_.offset := by omega + have hcast : (((1 - r.end_.offset).toNat : ℕ) : ℤ) = 1 - r.end_.offset := + Int.toNat_of_nonneg hnn + omega + +theorem beginSlack_cast_of_neg {r : RangeRead} (hbo : r.begin.offset < 0) : + (beginSlack r : ℤ) = -r.begin.offset := by + have hnn : 0 ≤ 0 - r.begin.offset := by omega + unfold beginSlack + rw [Int.toNat_of_nonneg hnn] + ring + +theorem endPad_cast_of_nonpos {r : RangeRead} (hend : r.end_.offset ≤ 0) : + ((1 - r.end_.offset).toNat : ℤ) = 1 - r.end_.offset := by + have hnn : 0 ≤ 1 - r.end_.offset := by omega + rw [Int.toNat_of_nonneg hnn] + +theorem beginSlack_pos (h : negCrossTruncTopTop r res) : 0 < beginSlack r := + beginSlack_pos_of_neg h.2.2.1 + +theorem endPad_pos (h : negCrossTruncTopTop r res) : 0 < (1 - r.end_.offset).toNat := + endPad_pos_of_nonpos h.2.2.2.2.2.2 + +theorem beginSlack_cast (h : negCrossTruncTopTop r res) : + (beginSlack r : ℤ) = -r.begin.offset := + beginSlack_cast_of_neg h.2.2.1 + +theorem endPad_cast (h : negCrossTruncTopTop r res) : + ((1 - r.end_.offset).toNat : ℤ) = 1 - r.end_.offset := + endPad_cast_of_nonpos h.2.2.2.2.2.2 + +/-- The empty window never reports a truncated nonempty prefix. -/ +theorem not_compatible_of_resolve_end_nil {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hE : resolve db r.end_ = (([] : Key) : WithTop Key)) : + ¬ RangeRead.Compatible db r res := by + intro hc + have hpre := RangeRead.compatible_keys_prefix hc + have hneW : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne + obtain ⟨a, ha⟩ : ∃ a, a ∈ RangeRead.completeKeys db r := by + cases hW : RangeRead.completeKeys db r with + | nil => exact absurd hW hneW + | cons a t => exact ⟨a, by simp [hW]⟩ + have hlt := (RangeRead.mem_completeKeys.mp ha).2.2 + rw [hE, WithTop.coe_lt_coe] at hlt + exact absurd hlt (List.not_lt_nil a) + +/-- The begin anchor with the begin key at the sentinel is `N - q - 1`. -/ +theorem anchor_begin_top {db : DB} {r : RangeRead} + (hBtop : r.begin.key = (⊤ : WithTop Key)) (hbo : r.begin.offset < 0) : + r.begin.anchor db = (db.keys.length : ℤ) - (beginSlack r : ℤ) - 1 := by + rw [anchor_of_top (db := db) hBtop, beginSlack_cast_of_neg hbo] + ring + +/-- The end anchor with the end key at the sentinel is `N - p`. -/ +theorem anchor_end_top {db : DB} {r : RangeRead} + (hEtop : r.end_.key = (⊤ : WithTop Key)) (hend : r.end_.offset ≤ 0) : + r.end_.anchor db = (db.keys.length : ℤ) - ((1 - r.end_.offset).toNat : ℤ) := by + rw [anchor_of_top (db := db) hEtop, endPad_cast_of_nonpos hend] + ring + +/-- The begin anchor of the both-sentinel read is `N - q - 1`. -/ +theorem anchor_begin_eq (h : negCrossTruncTopTop r res) (db : DB) : + r.begin.anchor db = (db.keys.length : ℤ) - (beginSlack r : ℤ) - 1 := + anchor_begin_top h.2.2.2.2.1 h.2.2.1 + +/-- The end anchor of the both-sentinel read is `N - p`. -/ +theorem anchor_end_eq (h : negCrossTruncTopTop r res) (db : DB) : + r.end_.anchor db = (db.keys.length : ℤ) - ((1 - r.end_.offset).toNat : ℤ) := + anchor_end_top h.2.2.2.2.2.1 h.2.2.2.2.2.2 + +/-- Under `N ≥ p`, the end resolution is the stored key at index `N - p`. Under +`N ≥ q + 1`, the begin resolution is the stored key at index `N - q - 1`. This is the +drop-and-take form of the complete window. -/ +theorem completeKeys_eq_drop_take {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hBtop : r.begin.key = (⊤ : WithTop Key)) + (hEtop : r.end_.key = (⊤ : WithTop Key)) + (hbo : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) + (hNp : (1 - r.end_.offset).toNat ≤ db.keys.length) + (hNq : beginSlack r < db.keys.length) : + RangeRead.completeKeys db r = + (db.keys.drop (db.keys.length - beginSlack r - 1)).take + (beginSlack r + 1 - (1 - r.end_.offset).toNat) := by + classical + set l := db.keys with hl + set q := beginSlack r with hq + set p := (1 - r.end_.offset).toNat with hp + have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos_of_nonpos hend + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hqN : q < l.length := hNq + have hpN : p ≤ l.length := hNp + have hanB : (r.begin.anchor db).toNat = l.length - q - 1 := by + rw [anchor_begin_top hBtop hbo, hq, hl]; omega + have hanE : (r.end_.anchor db).toNat = l.length - p := by + rw [anchor_end_top hEtop hend, hp, hl]; omega + have hanBnn : 0 ≤ r.begin.anchor db := by + rw [anchor_begin_top hBtop hbo, ← hq, ← hl] + have hcast : (beginSlack r : ℤ) < (db.keys.length : ℤ) := by exact_mod_cast hNq + omega + have hsorted : (l.drop (l.length - q - 1)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := l.length - q - 1) hls.pairwise) + -- The count of dropped keys below the end resolution simplifies to `q + 1 - p`. + have hcnt : (l.drop (l.length - q - 1)).countP + (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) = + q + 1 - p := by + rw [countP_drop_eq hls, countP_lt_resolve, hanE] + have hlen : db.keys.length = l.length := by rw [hl] + rw [hlen] + omega + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + show l.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) = _ + have hsplit : l.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) = + (l.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key)))).filter + (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + simp only [Bool.decide_and] + exact Bool.and_comm _ _ + rw [hsplit, filter_resolve_le_eq_drop, hanB, + filter_lt_eq_take_countP hsorted (resolve db r.end_), hcnt] + +/-- When the database has exactly `beginSlack` keys, the begin clamps to the bottom and +the complete window is the first `beginSlack - p` keys. -/ +theorem completeKeys_eq_take_of_size_beginSlack {db : DB} {r : RangeRead} {res : Result} + (h : negCrossTruncTopTop r res) (hN : db.keys.length = beginSlack r) + (hpq : (1 - r.end_.offset).toNat ≤ beginSlack r) : + RangeRead.completeKeys db r = + db.keys.take (beginSlack r - (1 - r.end_.offset).toNat) := by + classical + set q := beginSlack r with hq + set p := (1 - r.end_.offset).toNat with hp + have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h + have hltqp : p ≤ q := hpq + have hanB : r.begin.anchor db < 0 := by + rw [anchor_begin_eq h db, hN, hq]; omega + have hrb : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil hanB + have hanE : r.end_.anchor db = (q : ℤ) - (p : ℤ) := by + rw [anchor_end_eq h db, hN, hq, hp] + have hanE0 : 0 ≤ r.end_.anchor db := by rw [hanE]; omega + have hanE_toNat : (r.end_.anchor db).toNat = q - p := by + rw [hanE] + rw [show (q : ℤ) - (p : ℤ) = ((q - p : ℕ) : ℤ) by push_cast; omega] + exact Int.toNat_natCast _ + have hanE_lt : (r.end_.anchor db).toNat < db.keys.length := by + rw [hanE_toNat, hN, hq]; omega + have hre : resolve db r.end_ = (↑db.keys[(r.end_.anchor db).toNat] : WithTop Key) := + resolve_eq_getElem hanE0 hanE_lt + rw [completeKeys_eq_of_resolve_nil_coe (db := db) + (w := db.keys[(r.end_.anchor db).toNat]) h.2.1 h.2.2.2.2.2.2 hrb hre] + rw [filter_lt_eq_take_countP db.sorted] + have hcnt : db.keys.countP + (fun k : Key => decide ((↑k : WithTop Key) < + (↑db.keys[(r.end_.anchor db).toNat] : WithTop Key))) = q - p := by + rw [countP_lt_coe] + have := countP_lt_eq_of_getElem db.sorted + (x := db.keys[(r.end_.anchor db).toNat]) hanE_lt rfl + rw [this, hanE_toNat] + rw [hcnt] + +/-- The complete window of a forward both-sentinel read has at most `q + 1 - p` keys, +whatever the database. -/ +theorem completeKeys_length_le_topTop {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hBtop : r.begin.key = (⊤ : WithTop Key)) + (hEtop : r.end_.key = (⊤ : WithTop Key)) + (hbo : r.begin.offset < 0) (hend : r.end_.offset ≤ 0) : + (RangeRead.completeKeys db r).length ≤ + beginSlack r + 1 - (1 - r.end_.offset).toNat := by + classical + set N := db.keys.length with hN + set q := beginSlack r with hq + set p := (1 - r.end_.offset).toNat with hp + by_cases hpN : p ≤ N + · by_cases hqN : q < N + · have hdt := completeKeys_eq_drop_take hrev hBtop hEtop hbo hend + (by rw [← hp, ← hN]; exact hpN) (by rw [← hq, ← hN]; exact hqN) + rw [hdt, List.length_take, List.length_drop] + omega + · push_neg at hqN + -- `N ≤ q`: the begin clamps to the bottom + have hanB : r.begin.anchor db < 0 := by + rw [anchor_begin_top hBtop hbo] + have : (N : ℤ) ≤ q := by exact_mod_cast hqN + omega + have hresB : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil hanB + have hlen : (RangeRead.completeKeys db r).length = + db.keys.countP (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) from rfl, hresB] + rw [show db.keys.filter (fun k : Key => decide (((↑([] : Key)) : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) = + db.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) from by + apply List.filter_congr + intro k _ + have h : decide (((↑([] : Key)) : WithTop Key) ≤ (↑k : WithTop Key)) = true := by + rw [decide_eq_true_eq]; exact WithTop.coe_le_coe.mpr (nil_le_key k) + rw [Bool.decide_and, h, Bool.true_and]] + rw [← List.countP_eq_length_filter] + rw [hlen, countP_lt_resolve, anchor_end_top hEtop hend, ← hN, ← hp] + omega + · push_neg at hpN + -- `p > N`: the end resolves below the bottom, so the window is empty + have hanE : r.end_.anchor db < 0 := by + rw [anchor_end_top hEtop hend] + have : (N : ℤ) < p := by exact_mod_cast hpN + omega + have hresE : resolve db r.end_ = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil hanE + have hempty : RangeRead.completeKeys db r = [] := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) from rfl, hresE] + rw [List.filter_eq_nil_iff] + intro k _ + simp only [decide_eq_true_eq, not_and] + intro _ + exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k)) + rw [hempty] + simp + +/-- The complete window is empty when the end anchor is negative. -/ +theorem completeKeys_eq_nil_of_anchor_end_neg {db : DB} {r : RangeRead} + (h : r.end_.anchor db < 0) : RangeRead.completeKeys db r = [] := by + have hres : resolve db r.end_ = ((↑([] : Key)) : WithTop Key) := resolve_eq_nil h + have hempty : RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = [] := by + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) from rfl, hres] + rw [List.filter_eq_nil_iff] + intro k _ + simp only [decide_eq_true_eq, not_and] + intro _ + exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k)) + rw [RangeRead.completeKeys] + by_cases hrev : r.reverse = true + · rw [if_pos hrev]; exact List.reverse_eq_nil_iff.mpr hempty + · rw [if_neg hrev]; exact hempty + +/-- Readability forces the both-sentinel counts to fit: `t + p ≤ q + 1`. -/ +theorem readable_counts_topTop (h : negCrossTruncTopTop r res) (hread : Readable r res) : + res.keys.length + (1 - r.end_.offset).toNat ≤ beginSlack r + 1 := by + obtain ⟨db, hcomp⟩ := hread + have hpre := RangeRead.compatible_keys_prefix hcomp + have hlen := hpre.length_le + have hbound := completeKeys_length_le_topTop (db := db) + h.2.1 h.2.2.2.2.1 h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 + have ht1 : 1 ≤ res.keys.length := + List.length_pos_of_ne_nil (RangeRead.compatible_ne_nil_of_more hcomp h.1) + omega + +/-- In the equality branch, compatibility forces the stored keys at or above the +reported head to be the reported keys followed by exactly `p` keys. -/ +theorem exists_filter_head_decomp_of_compatible {db : DB} (h : negCrossTruncTopTop r res) + (hne : res.keys ≠ []) (hcomp : RangeRead.Compatible db r res) + (hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) : + ∃ G : List Key, db.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) = + res.keys ++ G ∧ G.length = (1 - r.end_.offset).toNat := by + classical + set t := res.keys.length with ht + set p := (1 - r.end_.offset).toNat with hp + have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp h.2.1 + have hpre := RangeRead.compatible_keys_prefix hcomp + have hlen := hpre.length_le + have hbound := completeKeys_length_le_topTop (db := db) + h.2.1 h.2.2.2.2.1 h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 + rw [← hp] at hbound + have hcklen : (RangeRead.completeKeys db r).length = t := by + have hq : beginSlack r + 1 - p = t := by omega + have h1 : (RangeRead.completeKeys db r).length ≤ t := by omega + exact le_antisymm h1 (by rw [ht]; exact hlen) + have hckeq : res.keys = RangeRead.completeKeys db r := + hpre.eq_of_length (by rw [← ht]; exact hcklen.symm) + set N := db.keys.length with hN + have hanEnd : r.end_.anchor db = (N : ℤ) - (p : ℤ) := by + rw [anchor_end_top h.2.2.2.2.2.1 h.2.2.2.2.2.2, ← hN, ← hp] + have hanE_nn : 0 ≤ r.end_.anchor db := by + by_contra hneg + have hnil := completeKeys_eq_nil_of_anchor_end_neg (db := db) (lt_of_not_ge hneg) + rw [← hckeq] at hnil + exact hne hnil + have hanE_toNat : (r.end_.anchor db).toNat = N - p := by + rw [hanEnd] + have hpN : (p : ℤ) ≤ (N : ℤ) := by omega + rw [Int.toNat_of_nonneg (by omega)] + omega + have hanE_lt : (r.end_.anchor db).toNat < N := by rw [hanE_toNat]; omega + have hhead_mem : res.keys.head hne ∈ RangeRead.completeKeys db r := by + rw [← hckeq]; exact List.head_mem hne + have hresB_le : resolve db r.begin ≤ ((res.keys.head hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hhead_mem).2.1 + have hhead_lt_resE : ((res.keys.head hne : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hhead_mem).2.2 + have hhead_min : ∀ k ∈ res.keys, res.keys.head hne ≤ k := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_head hk + refine ⟨db.keys.drop (N - p), ?_, ?_⟩ + · have hTake : db.keys.take (N - p) = db.keys.filter + (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by + have hcnt : db.keys.countP + (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) = N - p := by + rw [countP_lt_resolve, hanE_toNat, Nat.min_eq_left (by rw [← hN]; omega)] + rw [← hcnt, filter_lt_eq_take_countP db.sorted] + have hFL : (db.keys.take (N - p)).filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) = res.keys := by + rw [hTake, List.filter_filter] + have hcongr : db.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key)) && + decide ((↑k : WithTop Key) < resolve db r.end_)) = + db.keys.filter (fun k : Key => decide + (resolve db r.begin ≤ (↑k : WithTop Key)) && + decide ((↑k : WithTop Key) < resolve db r.end_)) := by + apply List.filter_congr + intro k hkdb + by_cases hh : ((res.keys.head hne : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · have hb : resolve db r.begin ≤ ((k : Key) : WithTop Key) := le_trans hresB_le hh + by_cases hl : ((k : Key) : WithTop Key) < resolve db r.end_ <;> + simp [hh, hb, hl] + · by_cases hl : ((k : Key) : WithTop Key) < resolve db r.end_ + · have hb : ¬ (resolve db r.begin ≤ ((k : Key) : WithTop Key)) := by + intro hb + have hkck : k ∈ RangeRead.completeKeys db r := by + rw [RangeRead.completeKeys, if_neg (by simp [h.2.1])] + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter (fun x : Key => decide (resolve db r.begin ≤ (↑x : WithTop Key) ∧ + (↑x : WithTop Key) < resolve db r.end_)) from rfl, List.mem_filter] + exact ⟨hkdb, by rw [decide_eq_true_eq]; exact ⟨hb, hl⟩⟩ + exact hh (WithTop.coe_le_coe.mpr (hhead_min k (by rw [hckeq]; exact hkck))) + simp [hh, hb, hl] + · simp [hh, hl] + have hR : db.keys.filter (fun k : Key => decide + (resolve db r.begin ≤ (↑k : WithTop Key)) && + decide ((↑k : WithTop Key) < resolve db r.end_)) = res.keys := by + rw [hckeq, RangeRead.completeKeys, if_neg (by simp [h.2.1]), RangeRead.inRange] + simp only [Bool.decide_and] + exact hcongr.trans hR + have hFD : (db.keys.drop (N - p)).filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) = + db.keys.drop (N - p) := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + have hdrop : db.keys.drop (N - p) = db.keys.filter (fun k : Key => decide + (resolve db r.end_ ≤ (↑k : WithTop Key))) := by + rw [filter_resolve_le_eq_drop (db := db) (s := r.end_), hanE_toNat] + rw [hdrop] at hk + exact le_trans (le_of_lt hhead_lt_resE) + (of_decide_eq_true (List.mem_filter.mp hk).2) + conv_lhs => rw [← List.take_append_drop (N - p) db.keys] + rw [List.filter_append, hFL, hFD] + · rw [List.length_drop]; omega + +/-- In the equality branch, the stored keys at or above the reported head being the +reported keys followed by `p` keys makes the database compatible. -/ +theorem compatible_of_filter_head_decomp {db : DB} (h : negCrossTruncTopTop r res) + (hne : res.keys ≠ []) {G : List Key} + (hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) + (hF : db.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) = + res.keys ++ G) + (hGlen : G.length = (1 - r.end_.offset).toNat) + (hlim : RangeRead.withinLimit r res.keys) : + RangeRead.Compatible db r res := by + classical + set t := res.keys.length with ht + set p := (1 - r.end_.offset).toNat with hp + have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h + set c := db.keys.countP + (fun k : Key => decide ((↑k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key))) with hc + have hdropF : db.keys.drop c = res.keys ++ G := by + rw [← filter_not_lt_eq_drop_countP db.sorted] + rw [← hF] + apply List.filter_congr + intro k _ + rw [decide_eq_decide, not_lt, WithTop.coe_le_coe] + have hclen : (res.keys ++ G).length = db.keys.length - c := by + rw [← hdropF, List.length_drop] + set N := db.keys.length with hN + have hNc : N = c + t + p := by + rw [hN] + have : (res.keys ++ G).length = t + p := by rw [List.length_append, ht, hGlen] + omega + have hpN : p ≤ N := by omega + have hqN : beginSlack r < N := by omega + have hck : RangeRead.completeKeys db r = res.keys := by + rw [completeKeys_eq_drop_take (r := r) (db := db) h.2.1 h.2.2.2.2.1 + h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 hpN hqN] + have hidx : N - beginSlack r - 1 = c := by omega + have htake : beginSlack r + 1 - p = t := by omega + rw [hidx, htake, hdropF, List.take_append_of_le_length (by omega), ht, List.take_length] + exact ⟨hlim, Or.inl ⟨h.1, hck ▸ List.prefix_refl _, hne⟩⟩ + +/-- **Soundness in the equality branch.** Agreement on `[head, ⊤)` fixes the stored +keys at or above the reported head, hence compatibility by the decomposition above. -/ +theorem sound_top_head_of_branchH (h : negCrossTruncTopTop r res) (hne : res.keys ≠ []) + (hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) : + Sound r res ⟨((res.keys.head hne : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + classical + intro db₁ db₂ hag + have hagree : ∀ k : Key, ((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) → + (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + intro k hk + exact (agreeOn_iff _ _ _).mp hag k ⟨hk, WithTop.coe_lt_top k⟩ + have hfilter : db₁.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) = + db₂.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + · intro k + rw [List.mem_filter, List.mem_filter] + constructor + · intro hk1 + exact ⟨(hagree k (of_decide_eq_true hk1.2)).mp hk1.1, hk1.2⟩ + · intro hk2 + exact ⟨(hagree k (of_decide_eq_true hk2.2)).mpr hk2.1, hk2.2⟩ + constructor + · intro hcomp + obtain ⟨G, hF, hGlen⟩ := exists_filter_head_decomp_of_compatible h hne hcomp hH + exact compatible_of_filter_head_decomp h hne hH (hfilter ▸ hF) hGlen hcomp.1 + · intro hcomp + obtain ⟨G, hF, hGlen⟩ := exists_filter_head_decomp_of_compatible h hne hcomp hH + exact compatible_of_filter_head_decomp h hne hH (hfilter ▸ hF) hGlen hcomp.1 + +/-- **Removal witness above the reported last in the equality branch.** Store the reported +keys followed by `p` fresh keys above the reported last, one of them the target. The +begin anchor is `0`, so the window is exactly the reported keys. Erasing the target +lowers the begin anchor to `-1` (clamping to `⊥`) and pulls the end resolution down onto +the reported last, so the window loses its last key. -/ +theorem exists_witness_above_last_branchH {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) + (hkgt : res.keys.getLast (hne h) < k) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + have hne := hne h + have hrev := hrev h + have hmore := hmore h + set t := res.keys.length with ht + set p := (1 - r.end_.offset).toNat with hp + have ht1 : 1 ≤ t := by rw [ht]; exact List.length_pos_of_ne_nil hne + have hp1 : 0 < p := by rw [hp]; exact endPad_pos h + set last := res.keys.getLast hne with hlast + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hcard : ((p - 1 : ℕ) : ℕ∞) + 1 ≤ + intervalCard ((keyAfter last : Key) : WithTop Key) (⊤ : WithTop Key) := by + rw [intervalCard_top_right (by simp)] + exact le_top + obtain ⟨dba, hdbaIn, hknot, hdbaLen⟩ := + exists_db_keysIn_avoid (B := ((keyAfter last : Key) : WithTop Key)) + (E := (⊤ : WithTop Key)) (k := k) (m := p - 1) hcard + set G : List Key := (dba.insert k hknot).keys with hG + have hGlen : G.length = p := by + rw [hG, DB.insert_keys, List.orderedInsert_length, hdbaLen] + omega + have hGmem : k ∈ G := by + rw [hG]; exact DB.mem_insert_self dba k hknot + have hGgt : ∀ x ∈ G, last < x := by + intro x hx + rw [hG, DB.insert_keys] at hx + rcases (List.mem_orderedInsert (r := (· ≤ ·))).mp hx with rfl | hx' + · exact hkgt + · have hxge : ((keyAfter last : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + (hdbaIn x hx').1 + exact lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hxge) + have hDsort : (res.keys ++ G).SortedLT := by + refine List.sortedLT_append.mpr ⟨hsort, (dba.insert k hknot).sorted, ?_⟩ + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hGgt b hb) + let dbw : DB := ⟨res.keys ++ G, hDsort⟩ + have hdbw_keys : dbw.keys = res.keys ++ G := rfl + have hN : dbw.keys.length = t + p := by + rw [hdbw_keys, List.length_append, hGlen, ← ht] + have hq : beginSlack r = t + p - 1 := by omega + have hck : RangeRead.completeKeys dbw r = res.keys := by + rw [completeKeys_eq_drop_take (db := dbw) hrev h.2.2.2.2.1 h.2.2.2.2.2.1 + h.2.2.1 h.2.2.2.2.2.2 (by rw [hN]; omega) (by rw [hq, hN]; omega)] + have hidx : dbw.keys.length - beginSlack r - 1 = 0 := by rw [hN, hq]; omega + have htake : beginSlack r + 1 - (1 - r.end_.offset).toNat = t := by + rw [← hp, hq]; omega + rw [hidx, htake, hdbw_keys, List.drop_zero, + List.take_append_of_le_length (by rw [← ht]), ht, List.take_length] + have hcompw : RangeRead.Compatible dbw r res := by + refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩ + rw [hck] + have hkdbw : k ∈ dbw.keys := by + rw [hdbw_keys]; exact List.mem_append.mpr (Or.inr hGmem) + have hk_not_res : k ∉ res.keys := fun hx => absurd (hle_last k hx) (not_le.mpr hkgt) + have hkeys' : (dbw.erase k).keys = res.keys ++ G.erase k := by + rw [DB.erase_keys, hdbw_keys, List.erase_append, if_neg hk_not_res] + have hN' : (dbw.erase k).keys.length = t + p - 1 := by + rw [hkeys', List.length_append, List.length_erase_of_mem hGmem, hGlen, ← ht] + omega + have hNq' : (dbw.erase k).keys.length = beginSlack r := by + rw [hN', hq] + have hanB' : r.begin.anchor (dbw.erase k) < 0 := by + rw [anchor_begin_eq h (dbw.erase k), hNq'] + push_cast + omega + have hrb' : resolve (dbw.erase k) r.begin = ((↑([] : Key)) : WithTop Key) := + resolve_eq_nil hanB' + have hanE' : r.end_.anchor (dbw.erase k) = (t : ℤ) - 1 := by + rw [anchor_end_eq h (dbw.erase k), hN', ← hp] + omega + have hanE'_toNat : (r.end_.anchor (dbw.erase k)).toNat = t - 1 := by + rw [hanE'] + rw [show (t : ℤ) - 1 = ((t - 1 : ℕ) : ℤ) by + rw [Nat.cast_sub ht1]; push_cast; ring] + exact Int.toNat_natCast _ + have hanE'_lt : (r.end_.anchor (dbw.erase k)).toNat < (dbw.erase k).keys.length := by + rw [hanE'_toNat, hN']; omega + have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor (dbw.erase k) := by rw [hanE']; omega + have hidx : (r.end_.anchor (dbw.erase k)).toNat = t - 1 := hanE'_toNat + have hlastIdx : res.keys[t - 1]? = some last := by + have hlen : t - 1 < res.keys.length := by rw [← ht]; omega + rw [List.getElem?_eq_getElem hlen, hlast, List.getLast_eq_getElem hne] + rw [resolve, ite_eq_right (not_lt.mpr h0), hkeys', List.getElem?_append, + if_pos (by rw [hidx, ← ht]; omega), hidx, hlastIdx] + have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by + rw [completeKeys_eq_of_resolve_nil_coe (db := dbw.erase k) (w := last) hrev + h.2.2.2.2.2.2 hrb' hre', hkeys', List.filter_append] + have hres_fil : res.keys.filter + (fun x : Key => decide ((↑x : WithTop Key) < (↑last : WithTop Key))) = + res.keys.dropLast := by + have hpred : ∀ x ∈ res.keys, + decide ((↑x : WithTop Key) < (↑last : WithTop Key)) = decide (x < last) := by + intro x _; rw [decide_eq_decide]; exact WithTop.coe_lt_coe + rw [List.filter_congr hpred] + conv_lhs => rw [← List.dropLast_append_getLast hne, hlast] + rw [List.filter_append] + have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = + res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have h := hsort.pairwise.rel_dropLast_getLast hx + simpa [hlast] using h + rw [hfil] + simp + have hG_fil : (G.erase k).filter + (fun x : Key => decide ((↑x : WithTop Key) < (↑last : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, WithTop.coe_lt_coe] + exact not_lt.mpr (le_of_lt (hGgt x (List.mem_of_mem_erase hx))) + rw [hres_fil, hG_fil, List.append_nil] + have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by + intro hc + have hpre := RangeRead.compatible_keys_prefix hc + rw [hckE] at hpre + have hlen := hpre.length_le + rw [List.length_dropLast, ← ht] at hlen + omega + exact ⟨dbw, hcompw, hkdbw, hnot⟩ + +/-- Above the reported last, in the equality branch, every key is necessary. -/ +theorem necessary_above_last_branchH {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) + (hkgt : res.keys.getLast (hne h) < k) : + Necessary r res k := by + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_above_last_branchH hread h hH hkgt + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_erase_of_mem hkdbw] + exact hnot⟩ + +/-- **The equality branch is minimal with range `[head, ⊤)`.** -/ +theorem minimal_top_head_of_branchH {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1) : + Minimal r res ⟨((res.keys.head (hne h) : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + classical + have hne := hne h + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_top_head_of_branchH h hne hH, ?_⟩ + intro k hklo _ + by_cases hklast : ((k : Key) : WithTop Key) ≤ ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp hklo) (WithTop.coe_le_coe.mp hklast) + · push_neg at hklast + exact necessary_above_last_branchH hread h hH (WithTop.coe_lt_coe.mp hklast) + +/-- Inserting a key above every element of the first list distributes over append. -/ +theorem orderedInsert_append_of_forall_lt {l₁ l₂ : List Key} {k : Key} + (h : ∀ x ∈ l₁, x < k) : + (l₁ ++ l₂).orderedInsert (· ≤ ·) k = l₁ ++ l₂.orderedInsert (· ≤ ·) k := by + induction l₁ with + | nil => rfl + | cons a t ih => + rw [List.cons_append, List.orderedInsert_cons, + if_neg (not_le.mpr (h a (List.mem_cons_self ..))), + ih (fun x hx => h x (List.mem_cons_of_mem a hx))] + exact (List.cons_append).symm + +/-- **Removal witness below the reported head in the strict branch.** Store the reported +keys followed by `q - t` fresh keys above the reported last, so the begin clamps and the +window is exactly the reported keys (the end resolves above the last). Inserting +`k < head` lifts the begin anchor to `0`, so the window starts with `k` and the reported +truncated prefix breaks. -/ +theorem exists_witness_below_head_branchF {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1) + (hk : ((k : Key) : WithTop Key) < ((res.keys.head (hne h) : Key) : WithTop Key)) : + ∃ dbw : DB, ∃ hkdb : k ∉ dbw.keys, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + have hne := hne h + have hrev := hrev h + have hmore := hmore h + set t := res.keys.length with ht + set q := beginSlack r with hq + set p := (1 - r.end_.offset).toNat with hp + have ht1 : 1 ≤ t := by rw [ht]; exact List.length_pos_of_ne_nil hne + have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h + have htp : t + p ≤ q := by omega + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set last := res.keys.getLast hne with hlast + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_le_last : res.keys.head hne ≤ last := hle_last _ (List.head_mem hne) + have hhead_le : ∀ x ∈ res.keys, res.keys.head hne ≤ x := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_head hx + have hcard : (((q - t) + 1 : ℕ) : ℕ∞) ≤ + intervalCard ((keyAfter last : Key) : WithTop Key) (⊤ : WithTop Key) := by + rw [intervalCard_top_right (by simp)] + exact le_top + obtain ⟨dba, hdbaIn, hknot, hdbaLen⟩ := + exists_db_keysIn_avoid (B := ((keyAfter last : Key) : WithTop Key)) + (E := (⊤ : WithTop Key)) (k := k) (m := q - t) hcard + set G : List Key := dba.keys with hG + have hGlen : G.length = q - t := hdbaLen + have hGgt : ∀ x ∈ G, last < x := by + intro x hx + rw [hG] at hx + have hxge : ((keyAfter last : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + (hdbaIn x hx).1 + exact lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hxge) + have hDsort : (res.keys ++ G).SortedLT := by + refine List.sortedLT_append.mpr ⟨hsort, ?_, ?_⟩ + · simpa [hG] using dba.sorted + · intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hGgt b hb) + let dbw : DB := ⟨res.keys ++ G, hDsort⟩ + have hdbw_keys : dbw.keys = res.keys ++ G := rfl + have hN : dbw.keys.length = q := by + rw [hdbw_keys, List.length_append, hGlen, ← ht] + omega + have hpq : p ≤ q := by omega + have hck : RangeRead.completeKeys dbw r = dbw.keys.take (q - p) := by + have h0 := completeKeys_eq_take_of_size_beginSlack (r := r) (res := res) (db := dbw) h + (by rw [hN, hq]) (by simpa [← hq, ← hp] using hpq) + simpa [← hq, ← hp] using h0 + have hqp : t ≤ q - p := by omega + have htk : List.take (q - p) (res.keys ++ G) = + res.keys ++ List.take (q - p - t) G := by + rw [List.take_append, List.take_of_length_le (by rw [← ht]; omega), ← ht] + have hck' : RangeRead.completeKeys dbw r = res.keys ++ List.take (q - p - t) G := by + rw [hck, hdbw_keys, htk] + have hpre : res.keys <+: RangeRead.completeKeys dbw r := by + rw [hck']; exact List.prefix_append _ _ + have hcompw : RangeRead.Compatible dbw r res := + ⟨hcomp₀.1, Or.inl ⟨hmore, hpre, hne⟩⟩ + have hk_head : k < res.keys.head hne := WithTop.coe_lt_coe.mp hk + have hk_lt_last : k < last := lt_of_lt_of_le hk_head hhead_le_last + have hk_not_res : k ∉ res.keys := fun hx => absurd (hhead_le k hx) (not_le.mpr hk_head) + have hk_not_G : k ∉ G := fun hx => absurd (hGgt k hx) (not_lt.mpr (le_of_lt hk_lt_last)) + have hkdb : k ∉ dbw.keys := by + rw [hdbw_keys]; simp [hk_not_res, hk_not_G] + have hins_keys : (dbw.insert k hkdb).keys = k :: (res.keys ++ G) := by + rw [DB.insert_keys, hdbw_keys] + cases hres : res.keys with + | nil => exact absurd hres hne + | cons a tl => + have hka : k ≤ a := le_of_lt (WithTop.coe_lt_coe.mp (by simpa [hres] using hk)) + rw [List.cons_append] + exact List.orderedInsert_cons_of_le (· ≤ ·) (tl ++ G) hka + have hN' : (dbw.insert k hkdb).keys.length = q + 1 := by + rw [hins_keys, List.length_cons, List.length_append, hGlen, ← ht] + omega + have hNp' : p ≤ (dbw.insert k hkdb).keys.length := by rw [hN']; omega + have hNq' : beginSlack r < (dbw.insert k hkdb).keys.length := by rw [hN', hq]; omega + have hckins : RangeRead.completeKeys (dbw.insert k hkdb) r = + List.take (q + 1 - p) (k :: (res.keys ++ G)) := by + have h0 := completeKeys_eq_drop_take (db := dbw.insert k hkdb) hrev h.2.2.2.2.1 + h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 hNp' hNq' + rw [h0] + have hidx : (dbw.insert k hkdb).keys.length - beginSlack r - 1 = 0 := by + rw [hN', hq]; omega + have htake : beginSlack r + 1 - p = q + 1 - p := by rw [hq] + rw [hidx, htake, List.drop_zero, hins_keys] + have hk_le_last : k ≤ last := le_of_lt hk_lt_last + have hfilins : (RangeRead.completeKeys (dbw.insert k hkdb) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = k :: res.keys := by + rw [hckins, ← hlast] + rw [show q + 1 - p = (q - p) + 1 by omega, List.take_succ_cons, htk] + rw [List.filter_cons_of_pos (by rw [decide_eq_true_eq]; exact hk_le_last), + List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide (x ≤ last)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hle_last x hx + have hG_fil : (List.take (q - p - t) G).filter + (fun x : Key => decide (x ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not] + exact not_le.mpr (hGgt x (List.mem_of_mem_take hx)) + rw [hres_fil, hG_fil, List.append_nil] + have hnot : ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by + intro hc + have heq := ((compatible_iff_truncated_keys hrev hmore hne).mp hc).2 + rw [hfilins] at heq + have hlen := congrArg List.length heq + rw [List.length_cons] at hlen + omega + exact ⟨dbw, hkdb, hcompw, hnot⟩ + +/-- Below the reported head, in the strict branch, every key is necessary. -/ +theorem necessary_below_head_branchF {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1) + (hk : ((k : Key) : WithTop Key) < ((res.keys.head (hne h) : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨dbw, hkdb, hcompw, hnot⟩ := exists_witness_below_head_branchF hread h hF hk + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_insert_of_not_mem hkdb] + exact hnot⟩ + +/-- **Removal witness above the reported last in the strict branch.** Store the reported +keys followed by `q + 1 - t` fresh keys above the reported last; the begin anchor is `0` +and the window starts at the reported head, so the result is compatible. Inserting +`k > last` lifts the begin anchor to `1`, so the window starts above the head and the +reported truncated prefix breaks. -/ +theorem exists_witness_above_last_branchF {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1) + (hkgt : res.keys.getLast (hne h) < k) : + ∃ dbw : DB, ∃ hkdb : k ∉ dbw.keys, RangeRead.Compatible dbw r res ∧ + ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by + classical + obtain ⟨db₀, hcomp₀⟩ := hread + have hne := hne h + have hrev := hrev h + have hmore := hmore h + set t := res.keys.length with ht + set q := beginSlack r with hq + set p := (1 - r.end_.offset).toNat with hp + have ht1 : 1 ≤ t := by rw [ht]; exact List.length_pos_of_ne_nil hne + have hp1 : 1 ≤ p := by rw [hp]; exact endPad_pos h + have htp : t + p ≤ q := by omega + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + set last := res.keys.getLast hne with hlast + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hcard : (((q + 1 - t) + 1 : ℕ) : ℕ∞) ≤ + intervalCard ((keyAfter last : Key) : WithTop Key) (⊤ : WithTop Key) := by + rw [intervalCard_top_right (by simp)] + exact le_top + obtain ⟨dba, hdbaIn, hknot, hdbaLen⟩ := + exists_db_keysIn_avoid (B := ((keyAfter last : Key) : WithTop Key)) + (E := (⊤ : WithTop Key)) (k := k) (m := q + 1 - t) hcard + set G : List Key := dba.keys with hG + have hGlen : G.length = q + 1 - t := hdbaLen + have hGgt : ∀ x ∈ G, last < x := by + intro x hx + rw [hG] at hx + have hxge : ((keyAfter last : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + (hdbaIn x hx).1 + exact lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hxge) + have hDsort : (res.keys ++ G).SortedLT := by + refine List.sortedLT_append.mpr ⟨hsort, ?_, ?_⟩ + · simpa [hG] using dba.sorted + · intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hGgt b hb) + let dbw : DB := ⟨res.keys ++ G, hDsort⟩ + have hdbw_keys : dbw.keys = res.keys ++ G := rfl + have hN : dbw.keys.length = q + 1 := by + rw [hdbw_keys, List.length_append, hGlen, ← ht] + omega + have hNp : p ≤ dbw.keys.length := by rw [hN]; omega + have hNq : beginSlack r < dbw.keys.length := by rw [hN, hq]; omega + have hck : RangeRead.completeKeys dbw r = res.keys ++ List.take (q + 1 - p - t) G := by + have h0 := completeKeys_eq_drop_take (db := dbw) hrev h.2.2.2.2.1 h.2.2.2.2.2.1 + h.2.2.1 h.2.2.2.2.2.2 hNp hNq + rw [h0] + have hidx : dbw.keys.length - beginSlack r - 1 = 0 := by rw [hN, hq]; omega + have htake : beginSlack r + 1 - p = q + 1 - p := by rw [hq] + rw [hidx, htake, List.drop_zero, hdbw_keys, List.take_append, + List.take_of_length_le (by rw [← ht]; omega), ← ht] + have hpre : res.keys <+: RangeRead.completeKeys dbw r := by + rw [hck]; exact List.prefix_append _ _ + have hcompw : RangeRead.Compatible dbw r res := + ⟨hcomp₀.1, Or.inl ⟨hmore, hpre, hne⟩⟩ + have hk_not_res : k ∉ res.keys := fun hx => absurd (hle_last k hx) (not_le.mpr hkgt) + have hk_not_G : k ∉ G := by rw [hG]; exact hknot + have hkdb : k ∉ dbw.keys := by + rw [hdbw_keys]; simp [hk_not_res, hk_not_G] + have hforall : ∀ x ∈ res.keys, x < k := fun x hx => lt_of_le_of_lt (hle_last x hx) hkgt + have hins_keys : (dbw.insert k hkdb).keys = + res.keys ++ G.orderedInsert (· ≤ ·) k := by + rw [DB.insert_keys, hdbw_keys, orderedInsert_append_of_forall_lt hforall] + have hN' : (dbw.insert k hkdb).keys.length = q + 2 := by + rw [hins_keys, List.length_append, List.orderedInsert_length, hGlen, ← ht] + omega + have hNp' : p ≤ (dbw.insert k hkdb).keys.length := by rw [hN']; omega + have hNq' : beginSlack r < (dbw.insert k hkdb).keys.length := by rw [hN', hq]; omega + have hGins_gt : ∀ x ∈ G.orderedInsert (· ≤ ·) k, last < x := by + intro x hx + rcases (List.mem_orderedInsert (r := (· ≤ ·))).mp hx with rfl | hx + · exact hkgt + · exact hGgt x hx + have hckins : RangeRead.completeKeys (dbw.insert k hkdb) r = + List.take (q + 1 - p) (res.keys.tail ++ G.orderedInsert (· ≤ ·) k) := by + have h0 := completeKeys_eq_drop_take (db := dbw.insert k hkdb) hrev h.2.2.2.2.1 + h.2.2.2.2.2.1 h.2.2.1 h.2.2.2.2.2.2 hNp' hNq' + rw [h0] + have hidx : (dbw.insert k hkdb).keys.length - beginSlack r - 1 = 1 := by + rw [hN', hq]; omega + have htake : beginSlack r + 1 - p = q + 1 - p := by rw [hq] + rw [hidx, htake, hins_keys, List.drop_one, List.tail_append_of_ne_nil hne] + have htakeins : List.take (q + 1 - p) (res.keys.tail ++ G.orderedInsert (· ≤ ·) k) = + res.keys.tail ++ List.take (q + 1 - p - res.keys.tail.length) + (G.orderedInsert (· ≤ ·) k) := by + rw [List.take_append, List.take_of_length_le (by rw [List.length_tail, ← ht]; omega)] + have hresfil : res.keys.tail.filter (fun x : Key => decide (x ≤ last)) = res.keys.tail := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hle_last x (List.mem_of_mem_tail hx) + have hGfil : (List.take (q + 1 - p - res.keys.tail.length) + (G.orderedInsert (· ≤ ·) k)).filter (fun x : Key => decide (x ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not] + exact not_le.mpr (hGins_gt x (List.mem_of_mem_take hx)) + have hfilins : (RangeRead.completeKeys (dbw.insert k hkdb) r).filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.tail := by + rw [hckins, ← hlast, htakeins, List.filter_append, hresfil, hGfil, List.append_nil] + have hnot : ¬ RangeRead.Compatible (dbw.insert k hkdb) r res := by + intro hc + have heq := ((compatible_iff_truncated_keys hrev hmore hne).mp hc).2 + rw [hfilins] at heq + have hlen := congrArg List.length heq + rw [List.length_tail] at hlen + omega + exact ⟨dbw, hkdb, hcompw, hnot⟩ + +/-- Above the reported last, in the strict branch, every key is necessary. -/ +theorem necessary_above_last_branchF {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1) + (hkgt : res.keys.getLast (hne h) < k) : + Necessary r res k := by + obtain ⟨dbw, hkdb, hcompw, hnot⟩ := exists_witness_above_last_branchF hread h hF hkgt + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_insert_of_not_mem hkdb] + exact hnot⟩ + +/-- **The strict branch is minimal with the full range `[⊥, ⊤)`.** -/ +theorem minimal_full_of_branchF {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncTopTop r res) + (hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + classical + have hne := hne h + refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_fullRange r res, ?_⟩ + intro k _ _ + by_cases hkhead : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · exact necessary_below_head_branchF hread h hF hkhead + · have hhead_le : res.keys.head hne ≤ k := + WithTop.coe_le_coe.mp (le_of_not_gt hkhead) + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + hhead_le hklast + · have hlast_lt_k : res.keys.getLast hne < k := lt_of_not_ge hklast + exact necessary_above_last_branchF hread h hF hlast_lt_k + +end NegCrossTruncTopTop + +/-- The equality branch of the both-sentinel truncated cell: the reported keys fill the +complete window exactly (`t + p = q + 1`). -/ +def negCrossTruncTopTopEq (r : RangeRead) (res : Result) : Prop := + negCrossTruncTopTop r res ∧ + res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1 + +/-- The closed form of the equality branch: the reported keys are the complete window, so +the minimal range is `[head, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncTopTopEq (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + +/-- The default head of a nonempty list is its head. -/ +theorem headD_eq_head {l : List Key} (h : l ≠ []) : l.headD ([] : Key) = l.head h := by + cases l with + | nil => exact absurd rfl h + | cons a t => rfl + +/-- The equality branch is incompatible with `begin.key ≤ ↑k` for a genuine key. -/ +theorem not_negCross_trunc_topTop_eq_of_begin_le_coe {r : RangeRead} {res : Result} {k : Key} + (h : r.begin.key ≤ ((k : Key) : WithTop Key)) : ¬ negCrossTruncTopTopEq r res := by + rintro ⟨htop, -⟩ + have hB : r.begin.key = (⊤ : WithTop Key) := NegCrossTruncTopTop.hBtop htop + rw [hB] at h + exact absurd h (not_le.mpr (WithTop.coe_lt_top k)) + +/-- The equality branch is incompatible with a positive end offset. -/ +theorem not_negCross_trunc_topTop_eq_of_one_le_end {r : RangeRead} {res : Result} + (h : 1 ≤ r.end_.offset) : ¬ negCrossTruncTopTopEq r res := by + rintro ⟨htop, -⟩ + have hend : r.end_.offset ≤ 0 := NegCrossTruncTopTop.hend htop + omega + +/-- The equality branch of the both-sentinel truncated cell is minimal with range +`[head, ⊤)`. -/ +theorem minimal_closedForm_of_negCross_trunc_topTop_eq {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncTopTopEq r res) : + Minimal r res (closedFormNegCrossTruncTopTopEq r res) := by + obtain ⟨htop, hH⟩ := h + have hne : res.keys ≠ [] := NegCrossTruncTopTop.hne htop + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head hne + rw [closedFormNegCrossTruncTopTopEq, hhead] + exact NegCrossTruncTopTop.minimal_top_head_of_branchH hread htop hH + +/-- The strict branch of the both-sentinel truncated cell: the reported keys leave room in +the complete window (`t + p < q + 1`). -/ +def negCrossTruncTopTopStrict (r : RangeRead) (res : Result) : Prop := + negCrossTruncTopTop r res ∧ + res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1 + +/-- The closed form of the strict branch: the full range `[⊥, ⊤)`. -/ +noncomputable def closedFormNegCrossTruncTopTopStrict (r : RangeRead) (_res : Result) : + ConflictRange := + ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + +/-- The strict branch of the both-sentinel truncated cell is minimal with the full range +`[⊥, ⊤)`. -/ +theorem minimal_closedForm_of_negCross_trunc_topTop_strict {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncTopTopStrict r res) : + Minimal r res (closedFormNegCrossTruncTopTopStrict r res) := by + obtain ⟨htop, hF⟩ := h + exact NegCrossTruncTopTop.minimal_full_of_branchF hread htop hF + +/-- **The whole both-sentinel truncated cell is minimal with the compact closed form.** It +is `[head, ⊤)` when the reported keys fill the complete window, and the full range +otherwise. -/ +theorem minimal_closedForm_of_negCross_trunc_topTop {r : RangeRead} {res : Result} + (hread : Readable r res) (h : negCrossTruncTopTop r res) : + Minimal r res (closedFormNegCrossTruncTopTop r res) := by + by_cases hH : res.keys.length + (1 - r.end_.offset).toNat = beginSlack r + 1 + · rw [closedFormNegCrossTruncTopTop, if_pos hH] + exact minimal_closedForm_of_negCross_trunc_topTop_eq hread ⟨h, hH⟩ + · have hle := NegCrossTruncTopTop.readable_counts_topTop h hread + have hF : res.keys.length + (1 - r.end_.offset).toNat < beginSlack r + 1 := by omega + rw [closedFormNegCrossTruncTopTop, if_neg hH] + exact NegCrossTruncTopTop.minimal_full_of_branchF hread h hF + +end FormalConflictRange diff --git a/FormalConflictRange/Nonpos.lean b/FormalConflictRange/Nonpos.lean new file mode 100644 index 0000000..a3a978a --- /dev/null +++ b/FormalConflictRange/Nonpos.lean @@ -0,0 +1,1021 @@ +/- +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.Empty +import FormalConflictRange.Reduction + +/-! +# The empty theory at a nonpositive begin offset + +`emptyValue`'s canonical branch is unsound when `begin.offset ≤ 0` (`Empty.lean`, +`compatible_empty_iff_clamp`): the resolved begin depends on stored keys strictly +below `begin.key`, outside the canonical range, and may reach the empty key. This +file begins the replacement theory in the difference-constraint language. + +The emptiness predicate at `begin.offset ≤ 0` is a single scalar inequality in the +counts below the two selector keys (`compatible_empty_iff_nonpos`); at `end.offset ≤ 1` +the empty-database disjunct is subsumed +(`compatible_empty_iff_nonpos_endle`). Soundness of the candidate bottom range +`[⊥, max begin.key end.key)` then follows because agreement on it fixes both counts +(`sound_bottomRange_of_nonpos_underEnd`), with `[⊥, end.key)` the cross-free special +case (`sound_bottomRange_of_nonpos_underEnd_le`). + +The candidates are minimal where the empty theory can separate keys. At `end.offset = 1` +the empty database is a universal flip witness: toggling any key below `end.key` yields +the singleton, whose complete keys are nonempty, so `[⊥, end.key)` is minimal +(`minimal_bottomRange_of_nonpos_end_one`). Cross-free with `end.offset ≥ 2` the empty +result is compatible exactly with the empty database, so every key is necessary and +`[⊥, ⊤)` is minimal (`minimal_fullRange_of_nonpos_overshoot`). +-/ + +namespace FormalConflictRange + +open RangeRead + +/-- **The nonpositive-begin empty criterion, in pure `max` form.** With `begin.offset ≤ 0` +the begin anchor `a = cnt begin.key + begin.offset - 1` is always below the length, so +the clamped comparison collapses to `cnt end.key + end.offset - 1 ≤ max a 0`, with the +empty database as the only extra boundary. -/ +theorem compatible_empty_iff_nonpos {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + db.keys = [] ∨ + (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ + max (((cnt db r.begin.key : ℤ) + r.begin.offset - 1)) 0 := by + rw [compatible_empty_iff_clamp hbegin] + constructor + · rintro (hnil | hle) + · exact Or.inl hnil + · refine Or.inr ?_ + have htoNat : ((((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat : ℤ)) = + max ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) 0 := by + rw [Int.toNat_eq_max] + linarith [htoNat.symm ▸ hle] + · rintro (hnil | hle) + · exact Or.inl hnil + · refine Or.inr ?_ + have htoNat : ((((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat : ℤ)) = + max ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) 0 := by + rw [Int.toNat_eq_max] + linarith [htoNat.symm ▸ hle] + +/-- **The nonpositive-begin empty criterion at end offset at most one.** When +`end.offset ≤ 1` the end anchor is `cnt end.key + end.offset - 1 ≤ cnt end.key`, so the +empty-database disjunct of `compatible_empty_iff_nonpos` is subsumed: an empty database +gives `end.offset - 1 ≤ 0 ≤ max … 0`. Emptiness is therefore the single inequality +`cnt end.key + end.offset - 1 ≤ max (cnt begin.key + begin.offset - 1) 0`. -/ +theorem compatible_empty_iff_nonpos_endle {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ + max (((cnt db r.begin.key : ℤ) + r.begin.offset - 1)) 0 := by + rw [compatible_empty_iff_nonpos hbegin] + refine ⟨fun h => h.elim (fun hnil => ?_) id, fun h => Or.inr h⟩ + have hE : cnt db r.end_.key = 0 := by + unfold cnt + rw [hnil] + simp + have : (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ 0 := by omega + exact le_trans this (le_max_right _ _) + +/-- **The cross-free nonpositive-end criterion, in slack form.** With `begin.offset ≤ 0` +and `end.offset ≤ 0`, the empty result is compatible exactly when +`cnt end.key - (1-end.offset) ≤ max (cnt begin.key - (1-begin.offset)) 0`. -/ +theorem compatible_empty_iff_nonpos_counts {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + (cnt db r.end_.key : ℤ) - ((1 - r.end_.offset).toNat : ℤ) ≤ + max ((cnt db r.begin.key : ℤ) - ((1 - r.begin.offset).toNat : ℤ)) 0 := by + rw [compatible_empty_iff_nonpos_endle (db := db) hbegin (by omega)] + have hb : ((1 - r.begin.offset).toNat : ℤ) = 1 - r.begin.offset := + Int.toNat_of_nonneg (by omega) + have he : ((1 - r.end_.offset).toNat : ℤ) = 1 - r.end_.offset := + Int.toNat_of_nonneg (by omega) + rw [show (cnt db r.end_.key : ℤ) + r.end_.offset - 1 = + (cnt db r.end_.key : ℤ) - ((1 - r.end_.offset).toNat : ℤ) by rw [he]; ring, + show (cnt db r.begin.key : ℤ) + r.begin.offset - 1 = + (cnt db r.begin.key : ℤ) - ((1 - r.begin.offset).toNat : ℤ) by rw [hb]; ring] + +/-- **The crossed short-gap arithmetic.** If `b ≤ 0` and `s < 1 - b`, then no +`cE` satisfies `cE ≤ max (cE + s + b - 1) 0` except `cE = 0`: the max's first argument is +strictly below `cE`, so it can only be the `0` branch, forcing `cE ≤ 0`. -/ +theorem le_max_self_add_offset_iff (cE s : ℕ) (b : ℤ) (hb : b ≤ 0) + (hst : s < (1 - b).toNat) : + ((cE : ℤ) ≤ max ((cE : ℤ) + (s : ℤ) + b - 1) 0) ↔ cE = 0 := by + have hsz : (s : ℤ) < 1 - b := by + have h : (s : ℤ) < (((1 - b).toNat : ℕ) : ℤ) := by exact_mod_cast hst + rwa [Int.toNat_of_nonneg (by omega : (0 : ℤ) ≤ 1 - b)] at h + have hlt : (cE : ℤ) + (s : ℤ) + b - 1 < (cE : ℤ) := by omega + constructor + · intro h + by_contra hne + have hpos : (0 : ℤ) < (cE : ℤ) := by + have : 0 < cE := Nat.pos_of_ne_zero hne + exact_mod_cast this + by_cases h0 : 0 ≤ (cE : ℤ) + (s : ℤ) + b - 1 + · rw [max_eq_left h0] at h; omega + · rw [max_eq_right (le_of_lt (not_le.mp h0))] at h; omega + · intro h + rw [h] + simp only [Nat.cast_zero, zero_add] + exact le_max_right _ _ + +/-- **The crossed `end.offset = 1` arithmetic, both gaps.** With `b ≤ 0`, the criterion +`cE ≤ max (cE + s + b - 1) 0` is exactly "the gap `s` reaches `1 - b`, or `cE = 0`": if +`cE > 0` the max's `0` branch cannot witness the inequality, so the first argument must be +nonnegative and at least `cE`, i.e. `s ≥ 1 - b`. -/ +theorem le_max_self_add_offset_iff' (cE s : ℕ) (b : ℤ) (hb : b ≤ 0) : + ((cE : ℤ) ≤ max ((cE : ℤ) + (s : ℤ) + b - 1) 0) ↔ + ((1 - b).toNat ≤ s ∨ cE = 0) := by + have hbz : ((1 - b).toNat : ℤ) = 1 - b := Int.toNat_of_nonneg (by omega) + constructor + · intro h + by_cases hc : cE = 0 + · exact Or.inr hc + · refine Or.inl ?_ + have hpos : (0 : ℤ) < (cE : ℤ) := by + have : 0 < cE := Nat.pos_of_ne_zero hc + exact_mod_cast this + have h0 : 0 ≤ (cE : ℤ) + (s : ℤ) + b - 1 := by + by_contra hneg + rw [max_eq_right (le_of_lt (not_le.mp hneg))] at h; omega + rw [max_eq_left h0] at h + have hs_ge : (((1 - b).toNat : ℤ)) ≤ (s : ℤ) := by rw [hbz]; omega + exact_mod_cast hs_ge + · rintro (hs | rfl) + · have hs_ge : (1 : ℤ) - b ≤ (s : ℤ) := by + have : ((1 - b).toNat : ℤ) ≤ (s : ℤ) := by exact_mod_cast hs + rwa [hbz] at this + have hge : (cE : ℤ) ≤ (cE : ℤ) + (s : ℤ) + b - 1 := by omega + exact le_trans hge (le_max_left _ _) + · simp only [Nat.cast_zero, zero_add] + exact le_max_right _ _ + +/-- **The crossed, `end.offset = 1` emptiness criterion, both gaps.** With +`begin.offset ≤ 0`, `end.offset = 1` and crossed selectors, the empty result is compatible +exactly when the stored keys in `[end.key, begin.key)` reach `1 - begin.offset`, or no +stored key lies below `end.key`. Both quantities are functions of the keys below +`begin.key`, so `[⊥, begin.key)` is sound; the short gap is +`compatible_empty_iff_nonpos_end_one_short`. -/ +theorem compatible_empty_iff_nonpos_end_one_crossed {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hEB : r.end_.key ≤ r.begin.key) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + ((1 - r.begin.offset).toNat ≤ (between db r.end_.key r.begin.key).length ∨ + cnt db r.end_.key = 0) := by + rw [compatible_empty_iff_nonpos_endle (db := db) hbegin (le_of_eq hend), hend] + set cE := cnt db r.end_.key with hcE + set s := (between db r.end_.key r.begin.key).length with hs + have hsplit : cnt db r.begin.key = cE + s := by + rw [hcE, hs]; exact cnt_eq_add_between db hEB + rw [hsplit, Nat.cast_add] + have := le_max_self_add_offset_iff' cE s r.begin.offset hbegin + norm_num at this ⊢ + exact this + +/-- **The crossed, `end.offset = 1` emptiness criterion, count form.** Rewriting +`compatible_empty_iff_nonpos_end_one_crossed` through `cnt begin.key - cnt end.key = #keys +in [end.key, begin.key)` expresses the criterion purely in the two counts below the +selector keys, which agreement on `[⊥, begin.key)` fixes. -/ +theorem compatible_empty_iff_nonpos_end_one_crossed' {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hEB : r.end_.key ≤ r.begin.key) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + ((1 - r.begin.offset).toNat ≤ cnt db r.begin.key - cnt db r.end_.key ∨ + cnt db r.end_.key = 0) := by + rw [compatible_empty_iff_nonpos_end_one_crossed (db := db) hbegin hend hEB] + have hlen : (between db r.end_.key r.begin.key).length = + cnt db r.begin.key - cnt db r.end_.key := by + have h := cnt_eq_add_between db hEB; omega + rw [hlen] + +/-- **The crossed, `end.offset = 1` emptiness criterion, short gap.** With +`begin.offset ≤ 0`, `end.offset = 1`, crossed selectors and a short gap +`intervalCard end.key begin.key < 1 - begin.offset`, the begin anchor can never be +lifted to zero, so the empty result is compatible exactly when no stored key lies below +`end.key`. This is the exact minimality criterion in the crossed short gap: the +coupled counts `cE` and `s` collapse to `cE = 0` because `[end.key, begin.key)` is too +short to supply the `1 - begin.offset` keys the second disjunct needs. -/ +theorem compatible_empty_iff_nonpos_end_one_short {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hEB : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) : + RangeRead.Compatible db r { keys := [], more := false } ↔ cnt db r.end_.key = 0 := by + rw [compatible_empty_iff_nonpos_end_one_crossed (db := db) hbegin hend hEB] + have hst : (between db r.end_.key r.begin.key).length < + (1 - r.begin.offset).toNat := by + have h1 := between_length_le_intervalCard (db := db) hEB + have h2 : ((between db r.end_.key r.begin.key).length : ℕ∞) < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞) := lt_of_le_of_lt h1 hgap + exact_mod_cast h2 + exact ⟨fun h => h.elim (fun hc => absurd hc (not_le.mpr hst)) id, Or.inr⟩ + +/-- **Offset arithmetic for the nonpositive-end region.** With both offsets at most +zero, `(end.offset - begin.offset).toNat` is the difference of the slack counts +`(1 - begin.offset).toNat` and `(1 - end.offset).toNat`. -/ +theorem offset_sub_toNat (oB oE : ℤ) (hB : oB ≤ 0) (hE : oE ≤ 0) : + (oE - oB).toNat = (1 - oB).toNat - (1 - oE).toNat := by + have hb : ((1 - oB).toNat : ℤ) = 1 - oB := Int.toNat_of_nonneg (by omega) + have he : ((1 - oE).toNat : ℤ) = 1 - oE := Int.toNat_of_nonneg (by omega) + have hd : (oE - oB) = ((1 - oB).toNat : ℤ) - ((1 - oE).toNat : ℤ) := by omega + have h1 : (((1 - oB).toNat : ℤ)).toNat = (1 - oB).toNat := Int.toNat_natCast _ + have h2 : (((1 - oE).toNat : ℤ)).toNat = (1 - oE).toNat := Int.toNat_natCast _ + rw [hd, Int.toNat_sub'' (by positivity) (by positivity), h1, h2] + +/-- **Integer arithmetic of the crossed nonpositive-end short gap.** If `cE ≤ cB` and +the gap `cB - cE` is below `b - e`, the inequality `cE - e ≤ max (cB - b) 0` is exactly +`cE ≤ e`: a positive left side would force `cB - b ≥ cE - e`, contradicting the short +gap. -/ +theorem cross_short_arith (cB cE b e : ℕ) (hmono : cE ≤ cB) (hle : cB - cE < b - e) : + ((cE : ℤ) - (e : ℤ) ≤ max ((cB : ℤ) - (b : ℤ)) 0 ↔ cE ≤ e) := by + by_cases hbc : b ≤ cB + · have hmax : (0 : ℤ) ≤ (cB : ℤ) - (b : ℤ) := by omega + rw [max_eq_left hmax] + constructor + · intro h + by_contra hce + omega + · intro hce; omega + · have hmax : (cB : ℤ) - (b : ℤ) < 0 := by omega + rw [max_eq_right (le_of_lt hmax)] + constructor + · intro h; omega + · intro hce; omega + +/-- **Integer arithmetic of the crossed nonpositive-end long gap.** For crossed counts +(`cE ≤ cB`), the inequality `cE - e ≤ max (cB - b) 0` is exactly "the end count fits +below `e`, or the begin count reaches `b` and the gap `b - e` fits in `cB - cE`". -/ +theorem cross_long_arith (cB cE b e : ℕ) (hmono : cE ≤ cB) : + ((cE : ℤ) - (e : ℤ) ≤ max ((cB : ℤ) - (b : ℤ)) 0 ↔ + (cE ≤ e ∨ (b ≤ cB ∧ b - e ≤ cB - cE))) := by + by_cases hce : cE ≤ e + · constructor + · intro _; exact Or.inl hce + · intro _; omega + · push Not at hce + by_cases hbc : b ≤ cB + · by_cases heb : e ≤ b + · rw [max_eq_left (by omega : (0 : ℤ) ≤ (cB : ℤ) - (b : ℤ))] + constructor + · intro h; exact Or.inr ⟨hbc, by omega⟩ + · intro h + have hsub1 : ((b - e : ℕ) : ℤ) = (b : ℤ) - (e : ℤ) := Nat.cast_sub heb + have hsub2 : ((cB - cE : ℕ) : ℤ) = (cB : ℤ) - (cE : ℤ) := Nat.cast_sub hmono + omega + · push Not at heb + have hsub0 : b - e = 0 := Nat.sub_eq_zero_of_le (le_of_lt heb) + rw [max_eq_left (by omega : (0 : ℤ) ≤ (cB : ℤ) - (b : ℤ)), hsub0] + constructor + · intro _; exact Or.inr ⟨hbc, Nat.zero_le _⟩ + · intro _; omega + · push Not at hbc + have hmax0 : max ((cB : ℤ) - (b : ℤ)) 0 = 0 := max_eq_right (by omega) + rw [hmax0] + constructor + · intro h; exact absurd h (by omega) + · rintro (h | ⟨hb, -⟩) + · exact absurd h (not_le.mpr hce) + · exact absurd hb (not_le.mpr hbc) + +/-- **The crossed nonpositive-end short-gap criterion.** With `begin.offset ≤ 0`, +`end.offset ≤ 0`, crossed selectors (`end.key ≤ begin.key`) and a gap too short to +lift the begin anchor (`intervalCard end.key begin.key < end.offset - begin.offset`), +the empty result is compatible exactly when `cnt end.key ≤ 1 - end.offset`. The +second disjunct of `compatible_empty_iff_nonpos_counts` can never fire, so the +criterion collapses to the end-count bound. -/ +theorem compatible_empty_iff_nonpos_crossed_short {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞)) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + cnt db r.end_.key ≤ (1 - r.end_.offset).toNat := by + rw [compatible_empty_iff_nonpos_counts (db := db) hbegin hend] + have hlen : (between db r.end_.key r.begin.key).length = + cnt db r.begin.key - cnt db r.end_.key := by + have := cnt_eq_add_between db hcross; omega + have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := cnt_le_cnt_of_le db hcross + have hle : cnt db r.begin.key - cnt db r.end_.key < + (1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat := by + have hcard := between_length_le_intervalCard (db := db) hcross + rw [hlen] at hcard + have hoff : (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) = + ((((1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat : ℕ)) : ℕ∞) := by + rw [offset_sub_toNat r.begin.offset r.end_.offset hbegin hend] + rw [hoff] at hgap + exact_mod_cast lt_of_le_of_lt hcard hgap + exact cross_short_arith _ _ _ _ hmono hle + +/-- **The crossed nonpositive-end long-gap criterion.** With `begin.offset ≤ 0`, +`end.offset ≤ 0` and crossed selectors, the empty result is compatible exactly when +`cnt end.key ≤ 1 - end.offset`, or `cnt begin.key ≥ 1 - begin.offset` and the gap +`1 - begin.offset - (1 - end.offset)` is at most the stored keys in `[end.key, begin.key)`. -/ +theorem compatible_empty_iff_nonpos_crossed_long {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key ≤ r.begin.key) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + (cnt db r.end_.key ≤ (1 - r.end_.offset).toNat ∨ + ((1 - r.begin.offset).toNat ≤ cnt db r.begin.key ∧ + (1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat ≤ + (between db r.end_.key r.begin.key).length)) := by + rw [compatible_empty_iff_nonpos_counts (db := db) hbegin hend] + have hlen : (between db r.end_.key r.begin.key).length = + cnt db r.begin.key - cnt db r.end_.key := by + have := cnt_eq_add_between db hcross; omega + have hmono : cnt db r.end_.key ≤ cnt db r.begin.key := cnt_le_cnt_of_le db hcross + rw [hlen] + exact cross_long_arith _ _ _ _ hmono + +/-- The full range `[⊥, ⊤)` stores exactly the whole database. -/ +theorem keys_bottom_top_eq (db : DB) : + (⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ : ConflictRange).keys db = db.keys := by + unfold ConflictRange.keys + rw [List.filter_eq_self] + intro k _ + refine decide_eq_true ?_ + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), WithTop.coe_lt_top k⟩ + +/-- **The nonpositive-begin empty criterion at end offset at least two, cross-free.** +With `begin.offset ≤ 0`, `end.offset ≥ 2` and `begin.key ≤ end.key`, the second disjunct +of `compatible_empty_iff_nonpos` is unsatisfiable: the end anchor is `cE + oE - 1 ≥ cB + 1` +while the begin side `max (cB + oB - 1) 0 ≤ cB`. Emptiness is therefore exactly the empty +database. -/ +theorem compatible_empty_iff_nonpos_overshoot {db : DB} {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) + (hle : r.begin.key ≤ r.end_.key) : + RangeRead.Compatible db r { keys := [], more := false } ↔ db.keys = [] := by + rw [compatible_empty_iff_nonpos hbegin] + refine ⟨fun h => h.elim id (fun hcon => absurd hcon ?_), fun h => Or.inl h⟩ + intro hcon + have hcB_le : cnt db r.begin.key ≤ cnt db r.end_.key := cnt_le_cnt_of_le db hle + have hmax : max ((cnt db r.begin.key : ℤ) + r.begin.offset - 1) 0 ≤ + (cnt db r.begin.key : ℤ) := + max_le (by omega) (by positivity) + have he : (cnt db r.begin.key : ℤ) + 1 ≤ (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by + omega + linarith + +/-- Below the empty key bound, a range's stored keys are exactly the stored keys +strictly below `w`; its cardinality is therefore `cnt db w`. -/ +theorem keys_bottom_eq {db : DB} {w : WithTop Key} : + (⟨(([] : Key) : WithTop Key), w⟩ : ConflictRange).keys db = + db.keys.filter (fun (k : Key) => decide ((k : WithTop Key) < w)) := by + unfold ConflictRange.keys + apply List.filter_congr + intro k _ + by_cases h : (k : WithTop Key) < w + · simp [h, nil_le_key] + · simp [h] + +/-- The count below `w` is the cardinality of the bottom range to `w`. -/ +theorem cnt_eq_keys_bottom (db : DB) (w : WithTop Key) : + cnt db w = ((⟨(([] : Key) : WithTop Key), w⟩ : ConflictRange).keys db).length := by + rw [keys_bottom_eq, cnt, List.countP_eq_length_filter] + +/-- Agreement on the bottom range `[⊥, E)` fixes the count below `E`. -/ +theorem cnt_eq_of_agree_bottom {db₁ db₂ : DB} {E : WithTop Key} + (h : AgreeOn (⟨(([] : Key) : WithTop Key), E⟩ : ConflictRange) db₁ db₂) : + cnt db₁ E = cnt db₂ E := by + rw [cnt_eq_keys_bottom, cnt_eq_keys_bottom, h] + +/-- Agreement on the bottom range `[⊥, E)` fixes the count below `B` for any `B ≤ E`, +since the keys below `B` are the keys below `E` that are below `B`. -/ +theorem cnt_eq_of_agree_bottom_le {db₁ db₂ : DB} {B E : WithTop Key} (hBE : B ≤ E) + (h : AgreeOn (⟨(([] : Key) : WithTop Key), E⟩ : ConflictRange) db₁ db₂) : + cnt db₁ B = cnt db₂ B := by + have hsub : ∀ db : DB, + (⟨(([] : Key) : WithTop Key), B⟩ : ConflictRange).keys db = + ((⟨(([] : Key) : WithTop Key), E⟩ : ConflictRange).keys db).filter + (fun (k : Key) => decide ((k : WithTop Key) < B)) := by + intro db + rw [keys_bottom_eq, keys_bottom_eq, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hkB : (k : WithTop Key) < B + · have hkE : (k : WithTop Key) < E := lt_of_lt_of_le hkB hBE + simp [hkB, hkE] + · simp [hkB] + have h₁ := cnt_eq_keys_bottom db₁ B + have h₂ := cnt_eq_keys_bottom db₂ B + rw [h₁, h₂, hsub db₁, hsub db₂, h] + +/-- **Soundness of the bottom range `[⊥, begin.key)` in the crossed long gap.** Agreement +on `[⊥, begin.key)` fixes both counts `cnt begin.key` and `cnt end.key`, hence the count +criterion of `compatible_empty_iff_nonpos_end_one_crossed'`, whatever the gap. -/ +theorem sound_bottomRange_of_nonpos_end_one_crossed {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hEB : r.end_.key ≤ r.begin.key) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have hB : cnt db₁ r.begin.key = cnt db₂ r.begin.key := cnt_eq_of_agree_bottom hagree + have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := + cnt_eq_of_agree_bottom_le hEB hagree + rw [compatible_empty_iff_nonpos_end_one_crossed' (db := db₁) hbegin hend hEB, + compatible_empty_iff_nonpos_end_one_crossed' (db := db₂) hbegin hend hEB, hB, hE] + +/-- **Soundness of the bottom range `[⊥, end.key)` in the crossed short gap.** Agreement +on `[⊥, end.key)` fixes `cnt end.key`, and the short-gap criterion +(`compatible_empty_iff_nonpos_end_one_short`) says compatibility is exactly `cnt end.key = +0`. -/ +theorem sound_bottomRange_of_nonpos_end_one_short {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hEB : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := cnt_eq_of_agree_bottom hagree + rw [compatible_empty_iff_nonpos_end_one_short (db := db₁) hbegin hend hEB hgap, + compatible_empty_iff_nonpos_end_one_short (db := db₂) hbegin hend hEB hgap, hE] + +/-- **Soundness of the bottom range at a nonpositive begin offset, end offset at most +one.** With `begin.offset ≤ 0` and `end.offset ≤ 1` (cross-free or crossed), a database is +compatible with the empty result exactly when `cnt end.key + end.offset - 1 ≤ +max (cnt begin.key + begin.offset - 1) 0` (`compatible_empty_iff_nonpos_endle`). When +`end.offset ≤ 0` and the selectors are crossed the same criterion is expressed by +`compatible_empty_iff_nonpos_crossed_long`/`_short`, both functions of `cnt end.key` and +`cnt begin.key`. All are fixed by the stored keys below `max begin.key end.key`, so +agreement on `⟨⊥, max begin.key end.key⟩` gives soundness. -/ +theorem sound_bottomRange_of_nonpos_underEnd {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := + cnt_eq_of_agree_bottom_le (le_max_right _ _) (E := max r.begin.key r.end_.key) hagree + have hB : cnt db₁ r.begin.key = cnt db₂ r.begin.key := + cnt_eq_of_agree_bottom_le (le_max_left _ _) (E := max r.begin.key r.end_.key) hagree + by_cases hE0 : r.end_.offset ≤ 0 + · by_cases hcross : r.end_.key ≤ r.begin.key + · by_cases hgap : (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key r.begin.key + · have hb' : (between db₁ r.end_.key r.begin.key).length = + (between db₂ r.end_.key r.begin.key).length := by + rw [← cnt_sub_eq_between_length hcross, ← cnt_sub_eq_between_length hcross, hB, hE] + rw [compatible_empty_iff_nonpos_crossed_long (db := db₁) hbegin hE0 hcross, + compatible_empty_iff_nonpos_crossed_long (db := db₂) hbegin hE0 hcross, + hB, hE, hb'] + · have hshort := not_le.mp hgap + rw [compatible_empty_iff_nonpos_crossed_short (db := db₁) hbegin hE0 hcross hshort, + compatible_empty_iff_nonpos_crossed_short (db := db₂) hbegin hE0 hcross hshort, + hE] + · rw [compatible_empty_iff_nonpos_endle (db := db₁) hbegin hend, + compatible_empty_iff_nonpos_endle (db := db₂) hbegin hend, hE, hB] + · rw [compatible_empty_iff_nonpos_endle (db := db₁) hbegin hend, + compatible_empty_iff_nonpos_endle (db := db₂) hbegin hend, hE, hB] + +/-- **Soundness of the bottom range at a nonpositive begin offset, end offset at +most one, cross-free.** The cross-free special case of +`sound_bottomRange_of_nonpos_underEnd`, with candidate `[⊥, end.key)`. -/ +theorem sound_bottomRange_of_nonpos_underEnd_le {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) (hle : r.begin.key ≤ r.end_.key) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + have h : (⟨(([] : Key) : WithTop Key), max r.begin.key r.end_.key⟩ : ConflictRange) = + ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by rw [max_eq_right hle] + rw [← h] + exact sound_bottomRange_of_nonpos_underEnd hbegin hend hmore hempty + +/-- **Soundness of the crossed nonpositive-end bottom range `[⊥, end.key)`.** With +`begin.offset ≤ 0`, `end.offset ≤ 0`, crossed selectors and a short gap, compatibility is +exactly `cnt end.key ≤ 1 - end.offset`, fixed by agreement on `[⊥, end.key)` +(`compatible_empty_iff_nonpos_crossed_short`). -/ +theorem sound_bottomRange_of_nonpos_crossed_short {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + (((r.end_.offset - r.begin.offset).toNat : ℕ) : ℕ∞)) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := cnt_eq_of_agree_bottom hagree + rw [compatible_empty_iff_nonpos_crossed_short (db := db₁) hbegin hend hcross hgap, + compatible_empty_iff_nonpos_crossed_short (db := db₂) hbegin hend hcross hgap, hE] + +/-- **Soundness of the crossed nonpositive-end bottom range `[⊥, begin.key)`.** With +`begin.offset ≤ 0`, `end.offset ≤ 0` and crossed selectors, compatibility is the long-gap +criterion `compatible_empty_iff_nonpos_crossed_long`, a function of `cnt end.key` and +`cnt begin.key` (via the gap length), both fixed by agreement on `[⊥, begin.key)`. -/ +theorem sound_bottomRange_of_nonpos_crossed_long {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key ≤ r.begin.key) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), r.begin.key⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + have hE : cnt db₁ r.end_.key = cnt db₂ r.end_.key := + cnt_eq_of_agree_bottom_le hcross hagree + have hB : cnt db₁ r.begin.key = cnt db₂ r.begin.key := cnt_eq_of_agree_bottom hagree + have hlen : (between db₁ r.end_.key r.begin.key).length = + (between db₂ r.end_.key r.begin.key).length := by + rw [← cnt_sub_eq_between_length hcross, ← cnt_sub_eq_between_length hcross, hB, hE] + rw [compatible_empty_iff_nonpos_crossed_long (db := db₁) hbegin hend hcross, + compatible_empty_iff_nonpos_crossed_long (db := db₂) hbegin hend hcross, hB, hE, hlen] + +/-- The database holding exactly the key `k`. -/ +def singleton (k : Key) : DB where + keys := [k] + sorted := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + +@[simp] theorem singleton_keys (k : Key) : (singleton k).keys = [k] := rfl + +/-- The anchor of a selector against the singleton `{k}`. -/ +theorem anchor_singleton (k : Key) (s : Selector) : + Selector.anchor (singleton k) s = + (if (k : WithTop Key) < s.key then (1 : ℤ) else 0) + s.offset - 1 := by + unfold Selector.anchor + rw [singleton_keys, List.countP_cons, List.countP_nil] + by_cases h : (k : WithTop Key) < s.key <;> simp [h] + +/-- **`end.key = ⊥` makes the crossed read trivially empty at a nonpositive begin.** If +`end.key` is the empty key then `cnt db end.key = 0` for every database, so the emptiness +criterion `s ≥ 1 - begin.offset ∨ cnt end.key = 0` holds for every database: the read is +`AlwaysEmpty`. This is why a key below `end.key` exists in every non-trivially-empty +crossed long-gap read, the hypothesis the gap witness needs. -/ +theorem alwaysEmpty_of_end_key_eq_bot {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hEB : r.end_.key ≤ r.begin.key) + (hbot : r.end_.key = ((([] : Key)) : WithTop Key)) : + AlwaysEmpty r := by + intro db + rw [← compatible_empty_iff_complete_nil] + exact (compatible_empty_iff_nonpos_end_one_crossed (db := db) hbegin hend hEB).mpr + (Or.inr (by rw [hbot]; simp [cnt])) + +/-- **`AlwaysEmpty` when the whole bottom range fits below the end anchor.** With +`begin.offset ≤ 0`, `end.offset ≤ 0` and `intervalCard ⊥ end.key ≤ 1 - end.offset`, every +database has `cnt end.key ≤ 1 - end.offset`, so the end anchor `cnt end.key + end.offset - 1` +is at most zero and the emptiness inequality holds against `max … 0`. This is the +unconditional room fact behind the cross-free nonpositive-end residual: whenever the read is +*not* always empty, the bottom range `[⊥, end.key)` must be strictly longer than +`1 - end.offset`. -/ +theorem alwaysEmpty_of_intervalCard_bot_le {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcard : intervalCard ((([] : Key)) : WithTop Key) r.end_.key ≤ + (((1 - r.end_.offset).toNat : ℕ) : ℕ∞)) : + AlwaysEmpty r := by + intro db + rw [← compatible_empty_iff_complete_nil] + rw [compatible_empty_iff_nonpos_endle (db := db) hbegin (by omega)] + have hcE : cnt db r.end_.key ≤ (1 - r.end_.offset).toNat := by + have h := cnt_le_intervalCard_nil db r.end_.key + exact Nat.cast_le.mp (le_trans h hcard) + have : (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ 0 := by omega + exact le_trans this (le_max_right _ _) + +/-- Contrapositive of `alwaysEmpty_of_intervalCard_bot_le`: a non-trivially-empty +cross-free read with both offsets nonpositive has `1 - end.offset + 1 ≤ intervalCard ⊥ +end.key`. This is the room in `[⊥, end.key)` the cross-free witness spends. -/ +theorem not_alwaysEmpty_intervalCard_bot {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) (hne : ¬ AlwaysEmpty r) : + (((1 - r.end_.offset).toNat + 1 : ℕ) : ℕ∞) ≤ + intervalCard ((([] : Key)) : WithTop Key) r.end_.key := by + have hnot : ¬ intervalCard ((([] : Key)) : WithTop Key) r.end_.key ≤ + (((1 - r.end_.offset).toNat : ℕ) : ℕ∞) := + fun h => hne (alwaysEmpty_of_intervalCard_bot_le hbegin hend h) + rw [not_le] at hnot + rw [show (((1 - r.end_.offset).toNat + 1 : ℕ) : ℕ∞) = + (((1 - r.end_.offset).toNat : ℕ) : ℕ∞) + 1 by push_cast; ring] + exact ENat.add_one_le_iff (by simp) |>.mpr hnot + +/-- **`AlwaysEmpty` when the begin slack is at most the end slack, crossed.** With both +offsets nonpositive, crossed selectors and `(1-begin.offset) ≤ (1-end.offset)`, the empty +result is compatible with every database: if `cnt end.key` fits below `1-end.offset` the +end clamps, and otherwise the crossed monotonicity `cnt end.key ≤ cnt begin.key` plus +`1-begin.offset ≤ 1-end.offset` satisfies the long-gap disjunct. Consequently a +non-trivially-empty crossed read must have `1-begin.offset > 1-end.offset`. -/ +theorem alwaysEmpty_of_slack_le_slack {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hcross : r.end_.key ≤ r.begin.key) + (hed : (1 - r.begin.offset).toNat ≤ (1 - r.end_.offset).toNat) : + AlwaysEmpty r := by + intro db + rw [← compatible_empty_iff_complete_nil] + rw [compatible_empty_iff_nonpos_crossed_long (db := db) hbegin hend hcross] + by_cases hce : cnt db r.end_.key ≤ (1 - r.end_.offset).toNat + · exact Or.inl hce + · refine Or.inr ⟨?_, ?_⟩ + · have hmono := cnt_le_cnt_of_le db hcross + omega + · have hsub : (1 - r.begin.offset).toNat - (1 - r.end_.offset).toNat = 0 := + Nat.sub_eq_zero_of_le hed + rw [hsub]; exact Nat.zero_le _ + +/-- **Gap room for the cross-free nonpositive-end residual.** With `B ≤ E`, `oB ≤ 0`, +`oE ≤ 0` and `b ≤ e` (where `b = (1-oB).toNat`, `e = (1-oE).toNat`), a non-trivially-empty +read has `e - b + 1 ≤ intervalCard B E`: the strict anchor comparison forces the `[B, E)` +count `cE - cB` past `e - b`, and the difference cap bounds it by the interval. This is the +room the cross-free witness spends in `[B, E)`. -/ +theorem tsub_add_one_le_intervalCard_of_countFeasible_nonpos {B E : WithTop Key} {oB oE : ℤ} + (hoB : oB ≤ 0) (hoE : oE ≤ 0) + (hed : (1 - oB).toNat ≤ (1 - oE).toNat) + (hfe : countFeasible B E oB oE) : + (((1 - oE).toNat - (1 - oB).toNat + 1 : ℕ) : ℕ∞) ≤ intervalCard B E := by + obtain ⟨cB, cE, n, -, -, hcBn, -, -, -, hgB, hgE, hmin⟩ := hfe + set b : ℕ := (1 - oB).toNat with hb + set e : ℕ := (1 - oE).toNat with he + have hb1 : ((1 - oB).toNat : ℤ) = 1 - oB := Int.toNat_of_nonneg (by omega) + have he1 : ((1 - oE).toNat : ℤ) = 1 - oE := Int.toNat_of_nonneg (by omega) + have hbToNat : (((cB : ℤ) + oB - 1).toNat) = cB - b := by + rw [show (cB : ℤ) + oB - 1 = (cB : ℤ) - (b : ℤ) by rw [hb, hb1]; ring, Int.toNat_sub] + have heToNat : (((cE : ℤ) + oE - 1).toNat) = cE - e := by + rw [show (cE : ℤ) + oE - 1 = (cE : ℤ) - (e : ℤ) by rw [he, he1]; ring, Int.toNat_sub] + rw [hbToNat, heToNat] at hmin + have hle1 : cB - b ≤ n := le_trans (Nat.sub_le _ _) hcBn + have h1 : min (cB - b) n = cB - b := Nat.min_eq_left hle1 + have hlt : cB - b < cE - e := by + have h2 : cB - b < min (cE - e) n := by rw [← h1]; exact hmin + exact lt_of_lt_of_le h2 (Nat.min_le_left _ _) + have hbge : (b : ℤ) ≤ (e : ℤ) := by rw [hb, hb1, he, he1]; omega + have hnat : e - b + 1 ≤ cE - cB := by + have hcast : ((e - b + 1 : ℕ) : ℤ) ≤ ((cE - cB : ℕ) : ℤ) := by omega + exact_mod_cast hcast + exact le_trans (by exact_mod_cast hnat) hgE + +/-- The read-level form of `tsub_add_one_le_intervalCard_of_countFeasible_nonpos`: a +non-trivially-empty cross-free read with both offsets nonpositive and `1-oB ≤ 1-oE` has the +gap room `1-oE - (1-oB) + 1 ≤ intervalCard begin.key end.key`. Feasibility is +`alwaysEmpty_iff_not_countFeasible`. -/ +theorem tsub_add_one_le_intervalCard_of_not_alwaysEmpty_nonpos {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hed : (1 - r.begin.offset).toNat ≤ (1 - r.end_.offset).toNat) + (hne : ¬ AlwaysEmpty r) : + (((1 - r.end_.offset).toNat - (1 - r.begin.offset).toNat + 1 : ℕ) : ℕ∞) ≤ + intervalCard r.begin.key r.end_.key := by + have hfe : countFeasible r.begin.key r.end_.key r.begin.offset r.end_.offset := by + by_contra hc + exact hne ((alwaysEmpty_iff_not_countFeasible r).mpr hc) + exact tsub_add_one_le_intervalCard_of_countFeasible_nonpos hbegin hend hed hfe + +/-- Erasing a stored key strictly below `w` lowers the count below `w` by one. -/ +theorem cnt_erase_lt (db : DB) {k : Key} (hk : k ∈ db.keys) {w : WithTop Key} + (hkw : (k : WithTop Key) < w) : cnt (db.erase k) w = cnt db w - 1 := by + unfold cnt + rw [DB.erase_keys, List.countP_erase, + ite_eq_left ⟨hk, by simpa using hkw⟩] + +/-- Erasing a stored key not strictly below `w` leaves the count below `w` unchanged. -/ +theorem cnt_erase_not_lt (db : DB) {k : Key} (_hk : k ∈ db.keys) {w : WithTop Key} + (hkw : ¬ (k : WithTop Key) < w) : cnt (db.erase k) w = cnt db w := by + unfold cnt + rw [DB.erase_keys, List.countP_erase, + ite_eq_right (by rintro ⟨-, hp⟩; exact hkw (of_decide_eq_true hp)), Nat.sub_zero] + +/-- The count below `w` in the singleton `{k}`. -/ +theorem cnt_singleton (k : Key) (w : WithTop Key) : + cnt (singleton k) w = if (k : WithTop Key) < w then 1 else 0 := by + unfold cnt + rw [singleton_keys, List.countP_cons, List.countP_nil] + by_cases h : (k : WithTop Key) < w <;> simp [h] + +/-- A genuine key in `[B, E)` certifies the interval is nonempty. -/ +theorem one_le_intervalCard_of_le_lt {B E : WithTop Key} {k : Key} + (hkB : B ≤ (k : WithTop Key)) (hkE : (k : WithTop Key) < E) : + (1 : ℕ∞) ≤ intervalCard B E := by + rw [intervalCard_eq_encard_keysIn, Order.one_le_iff_ne_zero, Set.encard_ne_zero] + exact ⟨k, by simp only [keysIn, Set.mem_ofPred_eq]; exact ⟨hkB, hkE⟩⟩ + +/-- **Realizing a count pair avoiding a key below `B`.** For `B ≤ E` and a key `k < B`, a +database with `clo` keys in `[⊥, B)`, `chi - clo` keys in `[B, E)`, and no `k`, exists +whenever `clo + 1` keys fit in `[⊥, B)` (so one can be spared for `k`) and `chi - clo` fit +in `[B, E)`. Built by concatenating the two intervals. -/ +theorem exists_db_below_gap_avoid_below {B E : WithTop Key} (hBE : B ≤ E) {k : Key} + (hkB : (k : WithTop Key) < B) {clo chi : ℕ} (hcl : clo ≤ chi) + (hlo : ((clo + 1 : ℕ) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) B) + (hgap : (((chi - clo : ℕ)) : ℕ∞) ≤ intervalCard B E) : + ∃ db : DB, k ∉ db.keys ∧ cnt db B = clo ∧ cnt db E = chi := by + obtain ⟨dbA, hA, hkA, hlenA⟩ := + exists_db_keysIn_avoid (B := ((([] : Key)) : WithTop Key)) (E := B) (k := k) + (m := clo) hlo + obtain ⟨dbC, hC, hlenC⟩ := exists_db_keysIn (B := B) (E := E) (m := chi - clo) hgap + have hAlo : ∀ x ∈ dbA.keys, ((([] : Key)) : WithTop Key) ≤ (x : WithTop Key) := + fun x hx => (hA x hx).1 + have hAhi : ∀ x ∈ dbA.keys, (x : WithTop Key) < B := fun x hx => (hA x hx).2 + have hClo : ∀ x ∈ dbC.keys, B ≤ (x : WithTop Key) := fun x hx => (hC x hx).1 + have hChi : ∀ x ∈ dbC.keys, (x : WithTop Key) < E := fun x hx => (hC x hx).2 + have hcross : ∀ x ∈ dbA.keys, ∀ y ∈ dbC.keys, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hAhi x hx) (hClo y hy)) + let db : DB := ⟨dbA.keys ++ dbC.keys, List.sortedLT_append.mpr ⟨dbA.sorted, dbC.sorted, hcross⟩⟩ + have hkeys : db.keys = dbA.keys ++ dbC.keys := rfl + have hk : k ∉ db.keys := by + rw [hkeys, List.mem_append]; rintro (h | h) + · exact hkA h + · exact absurd hkB (not_lt.mpr (hClo k h)) + have hcntA_B : cnt dbA B = clo := by rw [cnt_eq_length_of_forall_lt dbA hAhi, hlenA] + have hcntC_B : cnt dbC B = 0 := cnt_eq_zero_of_forall_ge dbC hClo + have hcntA_E : cnt dbA E = clo := by + rw [cnt_eq_length_of_forall_lt dbA (fun x hx => lt_of_lt_of_le (hAhi x hx) hBE), hlenA] + have hcntC_E : cnt dbC E = chi - clo := by rw [cnt_eq_length_of_forall_lt dbC hChi, hlenC] + refine ⟨db, hk, ?_, ?_⟩ + · have : cnt db B = cnt dbA B + cnt dbC B := by unfold cnt; rw [hkeys, List.countP_append] + rw [this, hcntA_B, hcntC_B, Nat.add_zero] + · have : cnt db E = cnt dbA E + cnt dbC E := by unfold cnt; rw [hkeys, List.countP_append] + rw [this, hcntA_E, hcntC_E]; omega + +/-- **Realizing a count pair avoiding a key in `[B, E)`.** For `B ≤ E` and a key +`B ≤ k < E`, a database with `clo` keys in `[⊥, B)`, `chi - clo` keys in `[B, E)`, and no +`k`, exists whenever `clo` fit in `[⊥, B)` and `chi - clo + 1` fit in `[B, E)`. -/ +theorem exists_db_below_gap_avoid_gap {B E : WithTop Key} (hBE : B ≤ E) {k : Key} + (hkB : B ≤ (k : WithTop Key)) (_hkE : (k : WithTop Key) < E) {clo chi : ℕ} + (hcl : clo ≤ chi) + (hlo : ((clo : ℕ) : ℕ∞) ≤ intervalCard ((([] : Key)) : WithTop Key) B) + (hgap : (((chi - clo + 1 : ℕ)) : ℕ∞) ≤ intervalCard B E) : + ∃ db : DB, k ∉ db.keys ∧ cnt db B = clo ∧ cnt db E = chi := by + obtain ⟨dbA, hA, hlenA⟩ := + exists_db_keysIn (B := ((([] : Key)) : WithTop Key)) (E := B) (m := clo) hlo + obtain ⟨dbC, hC, hkC, hlenC⟩ := + exists_db_keysIn_avoid (B := B) (E := E) (k := k) (m := chi - clo) hgap + have hAlo : ∀ x ∈ dbA.keys, ((([] : Key)) : WithTop Key) ≤ (x : WithTop Key) := + fun x hx => (hA x hx).1 + have hAhi : ∀ x ∈ dbA.keys, (x : WithTop Key) < B := fun x hx => (hA x hx).2 + have hClo : ∀ x ∈ dbC.keys, B ≤ (x : WithTop Key) := fun x hx => (hC x hx).1 + have hChi : ∀ x ∈ dbC.keys, (x : WithTop Key) < E := fun x hx => (hC x hx).2 + have hcross : ∀ x ∈ dbA.keys, ∀ y ∈ dbC.keys, x < y := by + intro x hx y hy + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hAhi x hx) (hClo y hy)) + let db : DB := ⟨dbA.keys ++ dbC.keys, List.sortedLT_append.mpr ⟨dbA.sorted, dbC.sorted, hcross⟩⟩ + have hkeys : db.keys = dbA.keys ++ dbC.keys := rfl + have hk : k ∉ db.keys := by + rw [hkeys, List.mem_append]; rintro (h | h) + · exact absurd hkB (not_le.mpr (hAhi k h)) + · exact hkC h + have hcntA_B : cnt dbA B = clo := by rw [cnt_eq_length_of_forall_lt dbA hAhi, hlenA] + have hcntC_B : cnt dbC B = 0 := cnt_eq_zero_of_forall_ge dbC hClo + have hcntA_E : cnt dbA E = clo := by + rw [cnt_eq_length_of_forall_lt dbA (fun x hx => lt_of_lt_of_le (hAhi x hx) hBE), hlenA] + have hcntC_E : cnt dbC E = chi - clo := by rw [cnt_eq_length_of_forall_lt dbC hChi, hlenC] + refine ⟨db, hk, ?_, ?_⟩ + · have : cnt db B = cnt dbA B + cnt dbC B := by unfold cnt; rw [hkeys, List.countP_append] + rw [this, hcntA_B, hcntC_B, Nat.add_zero] + · have : cnt db E = cnt dbA E + cnt dbC E := by unfold cnt; rw [hkeys, List.countP_append] + rw [this, hcntA_E, hcntC_E]; omega + +/-- **Soundness of the full range at a nonpositive begin offset, end offset at least +two, cross-free.** With `begin.offset ≤ 0`, `end.offset ≥ 2` and `begin.key ≤ end.key`, +compatibility with the empty result is exactly emptiness of the database +(`compatible_empty_iff_nonpos_overshoot`), a property of the whole database and hence +fixed by agreement on `[⊥, ⊤)`. -/ +theorem sound_fullRange_of_nonpos_overshoot {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (hle : r.begin.key ≤ r.end_.key) + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + intro db₁ db₂ hagree + rw [compatible_empty_iff_nonpos_overshoot (db := db₁) hbegin hend hle, + compatible_empty_iff_nonpos_overshoot (db := db₂) hbegin hend hle] + have halign : db₁.keys = db₂.keys := by + have h := hagree + simp only [AgreeOn] at h + rw [keys_bottom_top_eq db₁, keys_bottom_top_eq db₂] at h + exact h + rw [halign] + +/-- At `end.offset ≥ 2`, a nonpositive begin and cross-free selectors, no singleton is +compatible with the empty result: every singleton database is nonempty. -/ +theorem not_compatible_singleton_overshoot {r : RangeRead} {k : Key} + (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) (hle : r.begin.key ≤ r.end_.key) : + ¬ RangeRead.Compatible (singleton k) r { keys := [], more := false } := by + rw [compatible_empty_iff_nonpos_overshoot (db := singleton k) hbegin hend hle] + simp [singleton] + +/-- At `end.offset ≥ 2` and a nonpositive begin, no singleton is compatible with the empty +result, whatever the order of the selector keys: in `{k}` the end anchor is at least one, so +the resolved end is past `k`, while the begin anchor is at most `1 + begin.offset - 1 ≤ 0`, +so the resolved begin is at or below `k`; `k` therefore lies in the complete keys and the +result is nonempty. This is the crossed counterpart of +`not_compatible_singleton_overshoot` and the key fact behind the full-range minimality at a +nonpositive begin offset with an overshooting end. -/ +theorem not_compatible_singleton_overshoot_all {r : RangeRead} {k : Key} + (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) : + ¬ RangeRead.Compatible (singleton k) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil] + intro h + have hmem : k ∈ RangeRead.completeKeys (singleton k) r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by simp [singleton], ?_, ?_⟩ + · -- `resolve begin ≤ k`: the begin anchor is at most zero. + rw [resolve, anchor_singleton] + by_cases hB : (k : WithTop Key) < r.begin.key + · rw [ite_eq_left hB] + by_cases han : (1 : ℤ) + r.begin.offset - 1 < 0 + · rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) + · rw [ite_eq_right han] + have hz : (1 : ℤ) + r.begin.offset - 1 = 0 := by omega + rw [hz]; simp + · rw [ite_eq_right hB] + have han : (0 : ℤ) + r.begin.offset - 1 < 0 := by omega + rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) + · -- `k < resolve end`: the end anchor is at least one, so `resolve` is past `k`. + have hpos : (1 : ℤ) ≤ r.end_.anchor (singleton k) := by + rw [anchor_singleton]; split <;> omega + have h0 : 0 ≤ r.end_.anchor (singleton k) := by omega + have hge : (singleton k).keys.length ≤ (r.end_.anchor (singleton k)).toNat := by + have hle : (1 : ℤ) ≤ ((r.end_.anchor (singleton k)).toNat : ℤ) := by + rw [Int.toNat_of_nonneg h0]; exact hpos + rw [singleton_keys] + exact_mod_cast hle + rw [resolve_eq_top h0 hge] + exact WithTop.coe_lt_top k + rw [h] at hmem + exact List.not_mem_nil hmem + +/-- **Necessity of every key, nonpositive begin, overshoot, any selector order.** With +`begin.offset ≤ 0` and `end.offset ≥ 2`, every key is necessary whatever the order of the +selector keys: the empty database is compatible, and toggling any key into it yields the +singleton `{k}`, incompatible by `not_compatible_singleton_overshoot_all`. This is the +crossed counterpart of `necessary_of_nonpos_overshoot`: weaselbot's singleton `{k}` is +incompatible for every `k`, so the full range is minimal in the crossed case too. -/ +theorem necessary_of_nonpos_overshoot_all {r : RangeRead} (hbegin : r.begin.offset ≤ 0) + (hend : 2 ≤ r.end_.offset) (k : Key) : + Necessary r { keys := [], more := false } k := by + refine necessary_of_toggle ⟨emptyDB, compatible_emptyDB_emptyRes r, ?_⟩ + have htoggle : toggle emptyDB k = singleton k := by + unfold toggle + rw [dite_eq_right (by simp [emptyDB])] + rfl + rw [htoggle] + exact not_compatible_singleton_overshoot_all hbegin hend + +/-- **Soundness of the full range at a nonpositive begin offset, end offset at least +two, any selector order.** Agreement on `[⊥, ⊤)` fixes the entire database, hence whether +the empty result is compatible, for any order of the selector keys. -/ +theorem sound_fullRange_of_nonpos_overshoot_all {r : RangeRead} {res : Result} + (hmore : res.more = false) (hempty : res.keys = []) : + Sound r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + exact sound_of_keys_eq (fun db => keys_bottom_top_eq db) + +/-- **Necessity of the full range's keys, nonpositive begin, overshoot, any order.** Every +genuine key lies in `[⊥, ⊤)` and is necessary by `necessary_of_nonpos_overshoot_all`. -/ +theorem necessaryRange_fullRange_of_nonpos_overshoot {r : RangeRead} + (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) : + NecessaryRange r { keys := [], more := false } + (([] : Key) : WithTop Key) (⊤ : WithTop Key) := + fun k _ _ => necessary_of_nonpos_overshoot_all hbegin hend k + +/-- **The nonpositive-begin overshoot case, any selector order.** A read with +`begin.offset ≤ 0`, `end.offset ≥ 2` and an empty complete result has `[⊥, ⊤)` as its +minimal sound conflict range, cross-free or crossed. This closes the crossed half of the +overshoot region that weaselbot flagged (his item 2). -/ +theorem minimal_fullRange_of_nonpos_overshoot_all {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) + (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_fullRange_of_nonpos_overshoot_all rfl rfl, + necessaryRange_fullRange_of_nonpos_overshoot hbegin hend⟩ + +/-- **The nonpositive-begin overshoot case, cross-free.** A read with `begin.offset ≤ 0`, +`end.offset ≥ 2`, `begin.key ≤ end.key` and an empty complete result has `[⊥, ⊤)` as its +minimal sound conflict range. -/ +theorem minimal_fullRange_of_nonpos_overshoot {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) + (_hle : r.begin.key ≤ r.end_.key) + (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := + minimal_fullRange_of_nonpos_overshoot_all hbegin hend hmore hempty + +/-! ## The empty-database witness + +At `end.offset = 1` the empty database is compatible with the empty result, and +inserting any key `k` below `end.key` makes a nonempty result: the singleton `{k}` +resolves its end past itself and its begin at or below itself. So the factor-toggle +lemma (`Reduction.necessary_of_toggle`) makes every such `k` necessary. -/ + +/-- At `end.offset = 1`, a key `k` below `end.key` lies in the complete keys of the +singleton `{k}`: the end anchor is `1`, resolving past the only key to `⊤`, while the +begin anchor is at most `0`, resolving at or below `k`. -/ +theorem mem_completeKeys_singleton_self {r : RangeRead} {k : Key} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hk : (k : WithTop Key) < r.end_.key) : + k ∈ RangeRead.completeKeys (singleton k) r := by + rw [RangeRead.mem_completeKeys] + refine ⟨by simp [singleton], ?_, ?_⟩ + · -- `resolve begin ≤ k` + rw [resolve, anchor_singleton] + by_cases hB : (k : WithTop Key) < r.begin.key + · rw [ite_eq_left hB] + by_cases han : (1 : ℤ) + r.begin.offset - 1 < 0 + · rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) + · rw [ite_eq_right han] + have hz : (1 : ℤ) + r.begin.offset - 1 = 0 := by omega + rw [hz]; simp + · rw [ite_eq_right hB] + have han : (0 : ℤ) + r.begin.offset - 1 < 0 := by omega + rw [ite_eq_left han]; exact WithTop.coe_le_coe.mpr (nil_le_key k) + · -- `k < resolve end` + rw [resolve, anchor_singleton] + rw [ite_eq_left hk] + have hge : ¬ ((1 : ℤ) + r.end_.offset - 1 < 0) := by omega + rw [ite_eq_right hge] + have hidx : ((1 : ℤ) + r.end_.offset - 1).toNat = 1 := by omega + rw [hidx]; simp + +/-- At `end.offset = 1`, the singleton `{k}` of a key below `end.key` is incompatible +with the empty result: its complete keys are nonempty. -/ +theorem not_compatible_singleton_self {r : RangeRead} {k : Key} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hk : (k : WithTop Key) < r.end_.key) : + ¬ RangeRead.Compatible (singleton k) r { keys := [], more := false } := by + rw [compatible_empty_iff_complete_nil] + intro h + have := mem_completeKeys_singleton_self (r := r) hbegin hend hk + rw [h] at this + exact List.not_mem_nil this + +/-- **Necessity below `end.key` at `end.offset = 1`, nonpositive begin.** Every stored +key `k` with `k < end.key` is necessary for the empty result: the empty database is +compatible, and toggling `k` into it yields the singleton `{k}`, whose complete keys +are nonempty. -/ +theorem necessary_of_nonpos_end_one {r : RangeRead} {k : Key} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hk : (k : WithTop Key) < r.end_.key) : + Necessary r { keys := [], more := false } k := by + refine necessary_of_toggle ⟨emptyDB, compatible_emptyDB_emptyRes r, ?_⟩ + have htoggle : toggle emptyDB k = singleton k := by + unfold toggle + rw [dite_eq_right (by simp [emptyDB])] + rfl + rw [htoggle] + exact not_compatible_singleton_self hbegin hend hk + +/-- **The nonpositive-begin `underEnd` case at `end.offset = 1`, cross-free.** A read +with `begin.offset ≤ 0`, `end.offset = 1`, `begin.key ≤ end.key` and an empty complete +result has `⟨⊥, end.key⟩` as its minimal sound conflict range. Soundness is +`sound_bottomRange_of_nonpos_underEnd` (both counts fixed by the stored keys below +`end.key`); necessity is `necessary_of_nonpos_end_one`, since the empty database and +the singleton witnesses separate every key of the range. -/ +theorem minimal_bottomRange_of_nonpos_end_one {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) (hle : r.begin.key ≤ r.end_.key) + (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomRange_of_nonpos_underEnd_le hbegin (le_of_eq hend) hle rfl rfl, ?_⟩ + intro k _ hk + exact necessary_of_nonpos_end_one hbegin hend hk + +/-- **The nonpositive-begin `end.offset = 1` crossed short-gap case.** A read with +`begin.offset ≤ 0`, `end.offset = 1`, crossed selectors (`end.key ≤ begin.key`) and a gap +too short to lift the begin anchor (`intervalCard end.key begin.key < 1 - begin.offset`) +has `⟨⊥, end.key⟩` as its minimal sound conflict range. Soundness is +`sound_bottomRange_of_nonpos_end_one_short`; necessity is `necessary_of_nonpos_end_one`, +the same below-`end.key` witnesses as the cross-free case. Weaselbot's second +counterexample is the `end.key = [0]`, `begin.key = [0,0]`, `begin.offset = -2` instance: +the gap is `1 < 3`, and indeed `[⊥, [0])` is minimal. -/ +theorem minimal_bottomRange_of_nonpos_end_one_short {r : RangeRead} {res : Result} + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hEB : r.end_.key ≤ r.begin.key) + (hgap : intervalCard r.end_.key r.begin.key < + (((1 - r.begin.offset).toNat : ℕ) : ℕ∞)) + (hmore : res.more = false) (hempty : res.keys = []) : + Minimal r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + have hres : res = { keys := [], more := false } := by + cases res with + | mk keys more => simp only at hmore hempty; subst hempty; subst hmore; rfl + subst hres + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomRange_of_nonpos_end_one_short hbegin hend hEB hgap rfl rfl, ?_⟩ + intro k _ hk + exact necessary_of_nonpos_end_one hbegin hend hk + +end FormalConflictRange diff --git a/FormalConflictRange/NonposClosedForm.lean b/FormalConflictRange/NonposClosedForm.lean new file mode 100644 index 0000000..82f3d94 --- /dev/null +++ b/FormalConflictRange/NonposClosedForm.lean @@ -0,0 +1,1437 @@ +/- +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.Residual +import FormalConflictRange.Nonpos + +/-! +# The cross-free nonpositive-begin complete closed form + +At a nonpositive begin offset the resolved begin can clamp below `begin.key`, so the +empty key can be forced into every sound range. This module closes the cross-free +complete-result cell there: `begin.offset ≤ 0`, `begin.key ≤` the reported head, +`begin.key ≤ end_.key`, and a nonempty complete result. Writing `t` for the number of +reported keys at or above `end_.key` and `e = end_.offset - 1`, the minimal range is + +* `[⊥, end_.key)` when `end_.offset ≤ 1`; +* `[⊥, keyAfter (last reported))` when `t = e` (the finite branch); +* `[⊥, ⊤)` when `t < e` (the top branch). + +The finite branch fixes the complete keys below `keyAfter (last reported)`; the top +branch forces the end selector to resolve to `⊤`, and inserting a key above the reported +last then keeps that sentinel and adds the key to the complete window. +-/ + +open FormalConflictRange + +set_option maxHeartbeats 1200000 + +namespace FormalConflictRange + +/-- The empty key is at or below every `WithTop Key` bound. -/ +theorem coe_nil_le_key_withTop (t : WithTop Key) : ((([] : Key) : WithTop Key)) ≤ t := by + cases t with + | top => exact le_top + | coe k => exact WithTop.coe_le_coe.mpr (nil_le_key k) + +/-- At a nonpositive offset, a resolution that is not the empty key lies strictly +below the selector's own key. -/ +theorem resolve_lt_key_of_offset_nonpos_of_ne_nil {db : DB} {s : Selector} + (hoff : s.offset ≤ 0) (hne : resolve db s ≠ (([] : Key) : WithTop Key)) : + resolve db s < s.key := by + rcases lt_or_ge (s.anchor db) 0 with hneg | hpos + · rw [resolve_eq_nil hneg] at hne + exact absurd rfl hne + · have hcnt : (s.anchor db).toNat < + db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) := + (Int.toNat_lt hpos).mpr (by simp only [Selector.anchor]; omega) + have hlt : (s.anchor db).toNat < db.keys.length := + lt_of_lt_of_le hcnt (List.countP_le_length) + rw [resolve_eq_getElem hpos hlt] + exact getElem_lt_of_lt_countP_top db.sorted hlt hcnt + +/-- In a complete nonempty result at a nonpositive begin offset with `begin.key` at +or below the reported head, the begin selector clamps to the empty key. -/ +theorem resolve_eq_nil_of_complete_begin_le_head {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) : + resolve db r.begin = (([] : Key) : WithTop Key) := by + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + set head : Key := res.keys.head hne with hhead + have hhead_ck : head ∈ RangeRead.completeKeys db r := by + rw [← hres_eq]; exact List.head_mem hne + have hhead_lo : resolve db r.begin ≤ (head : WithTop Key) := + (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + have hhead_hi : (head : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hhead_ck).2.2 + by_contra hne0 + have hlt : resolve db r.begin < r.begin.key := + resolve_lt_key_of_offset_nonpos_of_ne_nil hbegin hne0 + have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by + intro ht + rw [ht] at hhead_lo + exact absurd hhead_lo (not_le.mpr (WithTop.coe_lt_top head)) + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with h | h | ⟨k, hkdb, hkres⟩ + · exact hne0 h + · exact hnot_top h + · have hk_le : (k : WithTop Key) ≤ (head : WithTop Key) := by + rw [← hkres]; exact hhead_lo + have hk_lt_end : (k : WithTop Key) < resolve db r.end_ := lt_of_le_of_lt hk_le hhead_hi + have hk_ck : k ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨hkdb, by rw [hkres], hk_lt_end⟩ + have hk_res : k ∈ res.keys := by rw [hres_eq]; exact hk_ck + have hhead_le_k : head ≤ k := + (RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk_res + have hhead_le_res : (head : WithTop Key) ≤ resolve db r.begin := by + rw [hkres]; exact WithTop.coe_le_coe.mpr hhead_le_k + have hkeq : resolve db r.begin = (head : WithTop Key) := + le_antisymm hhead_lo hhead_le_res + rw [hkeq] at hlt + exact absurd hlo (not_le.mpr hlt) + +/-- For a compatible *complete* result, the complete keys are exactly the stored keys +in `[resolve begin, last reported]`. -/ +theorem completeKeys_eq_filter_le_last {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) : + RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + k ≤ res.keys.getLast hne)) := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast : res.keys.getLast hne ∈ RangeRead.completeKeys db r := by + rw [← hres]; exact List.getLast_mem hne + have hlt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hlast).2.2 + have hf := completeKeys_filter_le_eq (db := db) (r := r) + (last := res.keys.getLast hne) hrev hlt + have hpair : (RangeRead.completeKeys db r).Pairwise (· < ·) := by + have h := RangeRead.completeKeys_pairwise (db := db) (r := r) + rw [hrev] at h + exact h + have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp + have hfiltr : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = RangeRead.completeKeys db r := by + have h := filter_le_getLast_eq_of_prefix hpair hpre hne + exact h.trans hres + rw [← hf, hfiltr] + +/-- The complete keys at or above `end_.key` are the stored keys at or above +`end_.key` and at or below the reported last, in a complete cross-free nonpositive +read. -/ +theorem completeKeys_filter_ge_eq_db_filter {db : DB} {r : RangeRead} {res : Result} + {last : Key} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hlast : last = res.keys.getLast hne) + (hbegin : r.begin.offset ≤ 0) (hstart : resolve db r.begin ≤ r.end_.key) : + (RangeRead.completeKeys db r).filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + have hmain := completeKeys_eq_filter_le_last (db := db) (r := r) (res := res) + hcomp hrev hmore hne + rw [hmain, ← hlast] + rw [List.filter_filter, List.filter_filter] + apply List.filter_congr + intro k _ + rw [← Bool.decide_and, ← Bool.decide_and, decide_eq_decide] + constructor + · rintro ⟨hE, hs, hl⟩ + exact ⟨hl, hE⟩ + · rintro ⟨hl, hE⟩ + exact ⟨hE, le_trans hstart hE, hl⟩ + +/-- The number of reported keys at or above `end_.key` is the length of the stored +keys in `[end_.key, last]`, in a complete cross-free nonpositive read. -/ +theorem residualT_eq_filter_length {db : DB} {r : RangeRead} {res : Result} + {last : Key} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hlast : last = res.keys.getLast hne) + (hbegin : r.begin.offset ≤ 0) (hstart : resolve db r.begin ≤ r.end_.key) : + residualT r res = + ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hc := completeKeys_filter_ge_eq_db_filter (db := db) (r := r) (res := res) + (last := last) hcomp hrev hmore hne hlast hbegin hstart + unfold residualT + rw [List.countP_eq_length_filter, hres, hc] + +/-- The reported over-count never exceeds the end offset, in a complete cross-free +nonpositive read with `end_.offset ≥ 2`. -/ +theorem residualT_le_residualE_nonpos {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) (hstart : resolve db r.begin ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) : + residualT r res ≤ residualE r := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast : res.keys.getLast hne ∈ RangeRead.completeKeys db r := by + rw [← hres]; exact List.getLast_mem hne + have hlt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hlast).2.2 + have h := filter_length_le_offset_of_resolve_end_gt (db := db) (r := r) + (last := res.keys.getLast hne) (by omega) hlt + rw [residualT_eq_filter_length hcomp hrev hmore hne rfl hbegin hstart] + simpa [residualE] using h + +/-- A stronger Boolean filter is strictly shorter when it misses a member of the +weaker filter. -/ +theorem length_filter_lt_of_mem_not {L : List Key} {p q : Key → Bool} + (himp : ∀ a, p a = true → q a = true) {x : Key} + (hxq : x ∈ L.filter q) (hxp : x ∉ L.filter p) : + (L.filter p).length < (L.filter q).length := by + have hsub := filter_sublist_filter_of_imp (l := L) himp + rcases lt_or_eq_of_le hsub.length_le with h | h + · exact h + · exfalso + have heq := hsub.eq_of_length h + rw [← heq] at hxq + exact hxp hxq + +end FormalConflictRange + +/-- **Finite-branch completeness from agreement (nonpositive cross-free complete).** +If a database agrees on the bottom truncated range with a compatible finite-branch +database, then it is itself complete. -/ +theorem completeKeys_eq_res_of_agree_nonpos_complete {db₁ db₂ : DB} {r : RangeRead} + {res : Result} {last : Key} + (hcomp : RangeRead.Compatible db₁ r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hstart : resolve db₁ r.begin ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hlast : last = res.keys.getLast hne) + (hte : residualT r res = residualE r) + (hT : (truncatedRangeBottom last).keys db₁ = (truncatedRangeBottom last).keys db₂) : + RangeRead.completeKeys db₂ r = res.keys := by + have hres : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have he1 : 0 < residualE r := by unfold residualE; omega + have htpos : 0 < residualT r res := by rw [hte]; exact he1 + have hElast : r.end_.key ≤ (last : WithTop Key) := by + rw [hlast] + exact last_ge_end_of_count_pos hsort hne htpos + -- Agreement on `[⊥, keyAfter last)` is equality of the keys at or below `last`. + have hbE : db₁.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter last : WithTop Key))) = + db₂.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter last : WithTop Key))) := by + have h : (truncatedRangeBottom last).keys db₁ = + (truncatedRangeBottom last).keys db₂ := hT + unfold truncatedRangeBottom at h + rwa [keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + have hb : db₁.keys.filter (fun k : Key => decide (k ≤ last)) = + db₂.keys.filter (fun k : Key => decide (k ≤ last)) := by + have hc : ∀ db : DB, db.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter last : WithTop Key))) = + db.keys.filter (fun k : Key => decide (k ≤ last)) := by + intro db + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + rwa [hc db₁, hc db₂] at hbE + have hhead_le_last : res.keys.head hne ≤ last := by + rw [hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hkey_ka : r.begin.key ≤ (keyAfter last : WithTop Key) := + le_trans (le_trans hlo (WithTop.coe_le_coe.mpr hhead_le_last)) + (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt last))) + have hbegineq : resolve db₂ r.begin = resolve db₁ r.begin := by + symm + exact resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) hbegin hkey_ka hbE + -- The reference count is `e`. + have hM₁len : ((db₁.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = residualE r := by + have h := residualT_eq_filter_length (db := db₁) (r := r) (res := res) + (last := last) hcomp hrev hmore hne hlast hbegin hstart + rw [← h] + exact hte + have hM₂ : ((db₂.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))) = + ((db₁.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db₂.sorted.pairwise.sublist (List.filter_sublist.trans List.filter_sublist) + · exact db₁.sorted.pairwise.sublist (List.filter_sublist.trans List.filter_sublist) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk₂, hEk⟩, hkl⟩ + have hmem : k ∈ db₁.keys.filter (fun k : Key => decide (k ≤ last)) := by + rw [hb] + exact List.mem_filter.mpr ⟨hk₂, decide_eq_true hkl⟩ + exact ⟨⟨(List.mem_filter.mp hmem).1, hEk⟩, hkl⟩ + · rintro ⟨⟨hk₁, hEk⟩, hkl⟩ + have hmem : k ∈ db₂.keys.filter (fun k : Key => decide (k ≤ last)) := by + rw [← hb] + exact List.mem_filter.mpr ⟨hk₁, decide_eq_true hkl⟩ + exact ⟨⟨(List.mem_filter.mp hmem).1, hEk⟩, hkl⟩ + have hM₂len : ((db₂.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = residualE r := by + rw [hM₂, hM₁len] + have hlt₂ : (last : WithTop Key) < resolve db₂ r.end_ := + resolve_end_gt_of_filter_length_le (db := db₂) (r := r) (last := last) + (by omega) (le_of_eq hM₂len) + -- The complete keys of `db₂` are the stored keys in `[start, last]`. + have hck₂ : RangeRead.completeKeys db₂ r = + db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + k ≤ last)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev] + simp only [if_false] + rw [hbegineq] + apply List.filter_congr + intro k hk + rw [decide_eq_decide] + constructor + · rintro ⟨hs, hkt⟩ + refine ⟨hs, ?_⟩ + by_contra hnk + have hklast : last < k := lt_of_not_ge hnk + have hEk : r.end_.key ≤ (k : WithTop Key) := + le_trans hElast (WithTop.coe_le_coe.mpr (le_of_lt hklast)) + have hkL : k ∈ (db₂.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ k)) := by + rw [List.mem_filter, List.mem_filter] + exact ⟨⟨hk, decide_eq_true hEk⟩, decide_eq_true le_rfl⟩ + have hknot : k ∉ (db₂.keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ last)) := by + rw [List.mem_filter, List.mem_filter] + rintro ⟨⟨-, -⟩, hle⟩ + rw [decide_eq_true_eq] at hle + exact absurd hle (not_le.mpr hklast) + have himp : ∀ a : Key, decide (a ≤ last) = true → decide (a ≤ k) = true := by + intro a ha + rw [decide_eq_true_eq] at ha ⊢ + exact le_trans ha (le_of_lt hklast) + have hstrict := length_filter_lt_of_mem_not + (L := db₂.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))) + (p := fun x : Key => decide (x ≤ last)) (q := fun x : Key => decide (x ≤ k)) + himp hkL hknot + have hbound := filter_length_le_offset_of_resolve_end_gt (db := db₂) (r := r) + (last := k) (by omega) hkt + unfold residualE at hM₂len + omega + · rintro ⟨hs, hkl⟩ + exact ⟨hs, lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkl) hlt₂⟩ + -- Agreement on `[start, last]` is exactly the reported keys. + have hfilter_eq : db₂.keys.filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ k ≤ last)) = + db₁.keys.filter + (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ k ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db₂.sorted.pairwise.sublist List.filter_sublist + · exact db₁.sorted.pairwise.sublist List.filter_sublist + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk₂, hs, hl⟩ + have hmem : k ∈ db₁.keys.filter (fun k : Key => decide (k ≤ last)) := by + rw [hb] + exact List.mem_filter.mpr ⟨hk₂, decide_eq_true hl⟩ + exact ⟨(List.mem_filter.mp hmem).1, hs, hl⟩ + · rintro ⟨hk₁, hs, hl⟩ + have hmem : k ∈ db₂.keys.filter (fun k : Key => decide (k ≤ last)) := by + rw [← hb] + exact List.mem_filter.mpr ⟨hk₁, decide_eq_true hl⟩ + exact ⟨(List.mem_filter.mp hmem).1, hs, hl⟩ + have hck₁ : RangeRead.completeKeys db₁ r = + db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + k ≤ last)) := by + have h := completeKeys_eq_filter_le_last (db := db₁) (r := r) (res := res) + hcomp hrev hmore hne + rwa [← hlast] at h + rw [hck₂, hfilter_eq, ← hck₁, ← hres] + +/-- **A finite end resolution fills the reported count.** In a complete cross-free +nonpositive read with `end.offset ≥ 2`, if the end selector resolves to a genuine key, +then the number of reported keys at or above `end_.key` is exactly `end.offset - 1`. -/ +theorem residualT_eq_residualE_of_resolve_end_ne_top {db : DB} {r : RangeRead} + {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) (hstart : resolve db r.begin ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (htop : resolve db r.end_ ≠ (⊤ : WithTop Key)) : + residualT r res = residualE r := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + set Q := db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hQ + set e := (r.end_.offset - 1).toNat with he + have hQs : Q.SortedLT := by + rw [hQ] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hend1 : 1 ≤ r.end_.offset := by omega + have hQres := resolve_eq_filter_getElem? (db := db) (s := r.end_) hend1 + have heQ : e < Q.length := by + by_contra hge + rw [List.getElem?_eq_none (le_of_not_gt hge)] at hQres + exact htop hQres + have htopq : resolve db r.end_ = ((Q[e] : Key) : WithTop Key) := by + rw [hQres, List.getElem?_eq_getElem heQ] + have hloE : resolve db r.begin ≤ r.end_.key := hstart + -- The reported keys at or above `end_.key` are the first `e` stored keys there. + have hck : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + Q.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + rw [hQ] + apply eq_of_forall_mem_iff_of_pairwise + · exact db.sorted.pairwise.sublist (List.filter_sublist.trans List.filter_sublist) + · exact db.sorted.pairwise.sublist (List.filter_sublist.trans List.filter_sublist) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk, hlo, hhi⟩, hE⟩ + exact ⟨⟨hk, hE⟩, hhi⟩ + · rintro ⟨⟨hk, hE⟩, hhi⟩ + exact ⟨⟨hk, le_trans hloE hE, hhi⟩, hE⟩ + have htake : Q.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) = + Q.take e := by + rw [htopq, filter_lt_eq_take_countP hQs ((Q[e] : Key) : WithTop Key)] + congr 1 + rw [show Q.countP (fun k : Key => decide ((k : WithTop Key) < ((Q[e] : Key) : WithTop Key))) = + Q.countP (fun k : Key => decide (k < Q[e])) from by + apply List.countP_congr + intro k _ + rw [decide_eq_true_eq, decide_eq_true_eq, WithTop.coe_lt_coe]] + exact countP_lt_eq_of_getElem hQs heQ rfl + unfold residualT residualE + rw [hres, List.countP_eq_length_filter, hck, htake, List.length_take, + Nat.min_eq_left (le_of_lt heQ)] + +/-- **The top branch forces an unbounded end.** In a complete cross-free nonpositive +read with `end.offset ≥ 2`, if the reported count at or above `end_.key` is below +`end.offset - 1`, then the end selector resolves to `⊤`. -/ +theorem resolve_end_eq_top_of_residualT_lt_residualE {db : DB} {r : RangeRead} + {res : Result} (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) (hstart : resolve db r.begin ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (htlt : residualT r res < residualE r) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra htop + have h := residualT_eq_residualE_of_resolve_end_ne_top hcomp hrev hmore hne hbegin + hstart hend htop + omega + +/-- **Finite-branch soundness (nonpositive cross-free complete).** Agreement on the bottom +truncated range fixes the complete keys, so a complete result stays complete. -/ +theorem sound_truncatedRangeBottom_of_nonpos_complete {r : RangeRead} {res : Result} {last : Key} + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hend : 2 ≤ r.end_.offset) (hlast : last = res.keys.getLast hne) + (hte : residualT r res = residualE r) : + Sound r res (truncatedRangeBottom last) := by + intro db₁ db₂ hagree + have hiff : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ res.keys = RangeRead.completeKeys db r := by + intro db + constructor + · rintro ⟨hlim, hm | ⟨-, heq⟩⟩ + · simp [hmore] at hm + · exact ⟨hlim, heq⟩ + · rintro ⟨hlim, hck⟩ + exact ⟨hlim, Or.inr ⟨hmore, hck⟩⟩ + rw [hiff db₁, hiff db₂] + constructor + · rintro ⟨hlim, hck₁⟩ + have hcomp₁ : RangeRead.Compatible db₁ r res := (hiff db₁).mpr ⟨hlim, hck₁⟩ + have hstart₁ : resolve db₁ r.begin ≤ r.end_.key := by + rw [resolve_eq_nil_of_complete_begin_le_head hcomp₁ hrev hmore hne hbegin hlo] + exact coe_nil_le_key_withTop r.end_.key + exact ⟨hlim, (completeKeys_eq_res_of_agree_nonpos_complete hcomp₁ hrev hmore hne hbegin + hlo hstart₁ hend hlast hte hagree).symm⟩ + · rintro ⟨hlim, hck₂⟩ + have hcomp₂ : RangeRead.Compatible db₂ r res := (hiff db₂).mpr ⟨hlim, hck₂⟩ + have hstart₂ : resolve db₂ r.begin ≤ r.end_.key := by + rw [resolve_eq_nil_of_complete_begin_le_head hcomp₂ hrev hmore hne hbegin hlo] + exact coe_nil_le_key_withTop r.end_.key + exact ⟨hlim, (completeKeys_eq_res_of_agree_nonpos_complete (db₁ := db₂) (db₂ := db₁) hcomp₂ + hrev hmore hne hbegin hlo hstart₂ hend hlast hte hagree.symm).symm⟩ + +/-- **Finite-branch minimality (nonpositive cross-free complete).** When `t = e`, the +minimal range is `[⊥, keyAfter last)`. -/ +theorem minimal_truncatedRangeBottom_of_nonpos_complete_finite {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hend : 2 ≤ r.end_.offset) + (hte : residualT r res = residualE r) : + Minimal r res (truncatedRangeBottom (res.keys.getLast hne)) := by + obtain ⟨db₀, hcomp₀⟩ := hread + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_truncatedRangeBottom_of_nonpos_complete hrev hmore hne hbegin hlo hend rfl hte, ?_⟩ + intro k _ hk + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk) + exact (necessary_nil_of_begin_offset_nonpos ⟨db₀, hcomp₀⟩ hrev hne hbegin hlo).of_between + (necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne)) (nil_le_key k) hkle + +/-- **Top-branch necessity above the reported last (nonpositive cross-free complete).** +Every key above the reported last is necessary: inserting it keeps the unbounded end and +adds it to the complete window. -/ +theorem necessary_above_last_of_nonpos_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hBhead : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hend : 2 ≤ r.end_.offset) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) + {x : Key} (hlo : res.keys.getLast hne ≤ x) : + Necessary r res x := by + obtain ⟨db₀, hcomp⟩ := hread + intro cr hs + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hcomp⟩ hx cr hs + · by_contra hcr + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hstart₀ : resolve db₀ r.begin ≤ r.end_.key := by + rw [resolve_eq_nil_of_complete_begin_le_head hcomp hrev hmore hne hbegin hBhead] + exact coe_nil_le_key_withTop r.end_.key + have hE₀ : resolve db₀ r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_residualT_lt_residualE hcomp hrev hmore hne hbegin hstart₀ hend htlt + have hElast : r.end_.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + last_ge_end_of_count_pos hsort hne htpos + have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hbegin_last : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + le_trans hBhead (WithTop.coe_le_coe.mpr hhead_le_last) + have hx0 : x ∉ db₀.keys := by + intro hxk + have hge : resolve db₀ r.begin ≤ ((x : Key) : WithTop Key) := + le_trans (resolve_le_key_of_offset_nonpos hbegin) + (le_trans hbegin_last (WithTop.coe_le_coe.mpr hlo)) + have hmem : x ∈ RangeRead.completeKeys db₀ r := by + rw [RangeRead.mem_completeKeys] + exact ⟨hxk, hge, by rw [hE₀]; exact WithTop.coe_lt_top x⟩ + rw [← hres] at hmem + exact hx hmem + have hall : ∀ k ∈ db₀.keys, ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by + intro k hk + by_cases hkb : resolve db₀ r.begin ≤ ((k : Key) : WithTop Key) + · have hmem : k ∈ RangeRead.completeKeys db₀ r := by + rw [RangeRead.mem_completeKeys] + exact ⟨hk, hkb, by rw [hE₀]; exact WithTop.coe_lt_top k⟩ + have hkres : k ∈ res.keys := by rw [← hres] at hmem; exact hmem + exact le_trans (WithTop.coe_le_coe.mpr (hmaxres k hkres)) (WithTop.coe_le_coe.mpr hlo) + · have hklt : ((k : Key) : WithTop Key) < resolve db₀ r.begin := lt_of_not_ge hkb + have hk_lt_last : ((k : Key) : WithTop Key) < + ((res.keys.getLast hne : Key) : WithTop Key) := + lt_of_lt_of_le (lt_of_lt_of_le hklt (resolve_le_key_of_offset_nonpos hbegin)) + hbegin_last + exact le_trans (WithTop.coe_le_coe.mpr (le_of_lt (WithTop.coe_lt_coe.mp hk_lt_last))) + (WithTop.coe_le_coe.mpr hlo) + have hL_lt : db₀.keys.length < + db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + set Q := db₀.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hQ + have hQlelast : ∀ k ∈ Q, k ≤ res.keys.getLast hne := by + intro k hk + rw [hQ] at hk + obtain ⟨hkdb, hkE⟩ := (List.mem_filter.mp hk) + rw [decide_eq_true_eq] at hkE + have hmem : k ∈ RangeRead.completeKeys db₀ r := by + rw [RangeRead.mem_completeKeys] + exact ⟨hkdb, le_trans hstart₀ hkE, + by rw [hE₀]; exact WithTop.coe_lt_top k⟩ + have hkres : k ∈ res.keys := by rw [← hres] at hmem; exact hmem + exact hmaxres k hkres + have hfilter : Q.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = Q := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact hQlelast k hk + have hTQ : residualT r res = Q.length := by + have h := residualT_eq_filter_length (db := db₀) (r := r) (res := res) + (last := res.keys.getLast hne) hcomp hrev hmore hne rfl hbegin hstart₀ + rw [h, hfilter] + have hQlen : Q.length = db₀.keys.length - + db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + have hsum := countP_ge_add_countP_lt (l := db₀.keys) r.end_.key + rw [hQ, ← List.countP_eq_length_filter] + omega + have htlt' : Q.length < (r.end_.offset - 1).toNat := by + rw [← hTQ]; exact htlt + unfold residualE at htlt' + rw [hQlen] at htlt' + omega + have hEins := resolve_end_eq_top_of_insert_top hx0 hL_lt hend + exact hx (mem_result_of_insert hcomp hs hcr hmore hne hx0 hall hEins) + +/-- **Top-branch minimality (nonpositive cross-free complete).** When `t < e`, the +minimal range is the full range `[⊥, ⊤)`. -/ +theorem minimal_fullRange_of_nonpos_complete_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hend : 2 ≤ r.end_.offset) + (htlt : residualT r res < residualE r) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + obtain ⟨db₀, hcomp₀⟩ := hread + by_cases htpos : 0 < residualT r res + · refine minimal_iff_sound_and_necessaryRange.mpr ⟨sound_fullRange r res, ?_⟩ + intro k _ _ + by_cases hkle : k ≤ res.keys.getLast hne + · exact (necessary_nil_of_begin_offset_nonpos ⟨db₀, hcomp₀⟩ hrev hne hbegin hlo).of_between + (necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne)) (nil_le_key k) hkle + · exact necessary_above_last_of_nonpos_top ⟨db₀, hcomp₀⟩ hrev hmore hne hbegin hlo hend + htpos htlt (le_of_lt (lt_of_not_ge hkle)) + · have hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_contra hge + have hpos : 0 < residualT r res := by + unfold residualT + rw [List.countP_pos_iff] + exact ⟨k, hk, by rw [decide_eq_true_eq]; exact le_of_not_gt hge⟩ + omega + exact minimal_fullRange_of_nonpos_overshoot_nonempty ⟨db₀, hcomp₀⟩ hrev hmore hne hbegin + hend hhi hlo + +/-- The cross-free nonpositive-begin complete closed form. -/ +noncomputable def completeNonposCrossFree (r : RangeRead) (res : Result) : ConflictRange := + if res.keys = [] then ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + else if r.end_.offset ≤ 1 then ⟨(([] : Key) : WithTop Key), r.end_.key⟩ + else if residualT r res = residualE r then + truncatedRangeBottom (res.keys.getLastD ([] : Key)) + else ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ + +/-- **The cross-free nonpositive-begin complete closed form.** For `begin.offset ≤ 0`, +`begin.key ≤ head`, and a complete nonempty result, the minimal range is `[⊥, end_.key)` +when `end_.offset ≤ 1`, `[⊥, keyAfter last)` in the finite branch `t = e`, and `[⊥, ⊤)` +otherwise. -/ +theorem minimal_completeNonposCrossFree {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) : + Minimal r res (completeNonposCrossFree r res) := by + unfold completeNonposCrossFree + rw [if_neg hne] + by_cases hend1 : r.end_.offset ≤ 1 + · rw [if_pos hend1] + exact minimal_bottomRange_of_nonpos_underEnd_nonempty hread hrev hmore hne hbegin hend1 hlo + · rw [if_neg hend1] + have hend : 2 ≤ r.end_.offset := by omega + by_cases hte : residualT r res = residualE r + · rw [if_pos hte] + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + exact minimal_truncatedRangeBottom_of_nonpos_complete_finite hread hrev hmore hne hbegin + hlo hend hte + · rw [if_neg hte] + obtain ⟨db₀, hcomp₀⟩ := hread + have hstart₀ : resolve db₀ r.begin ≤ r.end_.key := by + rw [resolve_eq_nil_of_complete_begin_le_head hcomp₀ hrev hmore hne hbegin hlo] + exact coe_nil_le_key_withTop r.end_.key + have htle : residualT r res ≤ residualE r := + residualT_le_residualE_nonpos hcomp₀ hrev hmore hne hbegin hstart₀ hend + have htlt : residualT r res < residualE r := lt_of_le_of_ne htle hte + exact minimal_fullRange_of_nonpos_complete_top ⟨db₀, hcomp₀⟩ hrev hmore hne hbegin hlo + hend htlt + +/-- The reversed-traversal cross-free nonpositive-begin complete closed form. -/ +noncomputable def completeNonposCrossFreeRev (r : RangeRead) (res : Result) : ConflictRange := + completeNonposCrossFree { r with reverse := false } + { keys := res.keys.reverse, more := false } + +/-- The reversed cross-free nonpositive-begin complete rule is minimal. -/ +theorem minimal_completeNonposCrossFree_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) : + Minimal r res (completeNonposCrossFreeRev r res) := by + 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 hlo' : r'.begin.key ≤ ((res'.keys.head hne' : Key) : WithTop Key) := by + simpa [r', res'] using hlo + have hmin' : Minimal r' res' (completeNonposCrossFree r' res') := + minimal_completeNonposCrossFree hread' rfl rfl hne' hbegin hlo' + have hflip := (minimal_flip (r := r) (res := res) + (cr := completeNonposCrossFree r' res') (b := true) hrev hmore) + exact hflip.mp hmin' + +/-- A nonempty *complete* result at a nonpositive begin offset, cross-free, with the begin +key at or below the least reported key. This is the cross-free nonpositive-begin complete +cell, in either traversal. -/ +abbrev nonposCompleteCrossFree (r : RangeRead) (res : Result) : Prop := + res.more = false ∧ res.keys ≠ [] ∧ r.begin.offset ≤ 0 ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) + +/-- The cross-free nonpositive-begin complete closed form, in either traversal. -/ +noncomputable def closedFormCompleteNonposCrossFree (r : RangeRead) (res : Result) : + ConflictRange := + if r.reverse = true then completeNonposCrossFreeRev r res + else completeNonposCrossFree r res + +/-- **The cross-free nonpositive-begin complete closed form is minimal.** -/ +theorem minimal_closedForm_of_complete_nonpos_crossFree {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposCompleteCrossFree r res) : + Minimal r res (closedFormCompleteNonposCrossFree r res) := by + obtain ⟨hmore, hne, hbegin, hloD⟩ := h + unfold closedFormCompleteNonposCrossFree + cases hrev : r.reverse with + | false => + rw [ite_eq_right (by simp [hrev])] + have hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key) := by + have hloD' : r.begin.key ≤ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rwa [← hhead] + exact minimal_completeNonposCrossFree hread hrev hmore hne hbegin hlo + | true => + rw [ite_eq_left (by simp [hrev])] + have hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + have hloD' : r.begin.key ≤ + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rwa [← hlast] + exact minimal_completeNonposCrossFree_rev hread hrev hmore hne hbegin hlo +namespace FormalConflictRange + +/-- Agreement on keys at or below `last` fixes the `end_.key`-filtered keys at or +below `last`. -/ +theorem filter_end_le_swap (db : DB) (r : RangeRead) (last : Key) : + (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + (db.keys.filter (fun k : Key => decide (k ≤ last))).filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + rw [List.filter_filter, List.filter_filter] + apply List.filter_congr + intro k _ + exact Bool.and_comm _ _ + +/-- Agreement on keys at or below `last` transfers the end-resolution cut at `last`. -/ +theorem lt_resolve_end_iff_of_agree_bottom {db₁ db₂ : DB} {r : RangeRead} {last : Key} + (hend : 1 ≤ r.end_.offset) + (hb : db₁.keys.filter (fun k : Key => decide (k ≤ last)) = + db₂.keys.filter (fun k : Key => decide (k ≤ last))) : + ((last : Key) : WithTop Key) < resolve db₁ r.end_ ↔ + ((last : Key) : WithTop Key) < resolve db₂ r.end_ := by + have hQ : (db₁.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + (db₂.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + rw [filter_end_le_swap, filter_end_le_swap, hb] + constructor + · intro h1 + have hc1 := filter_length_le_offset_of_resolve_end_gt (db := db₁) hend h1 + have hc2 : ((db₂.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [← hQ] + exact hc1 + exact resolve_end_gt_of_filter_length_le (db := db₂) hend hc2 + · intro h2 + have hc2 := filter_length_le_offset_of_resolve_end_gt (db := db₂) hend h2 + have hc1 : ((db₁.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [hQ] + exact hc2 + exact resolve_end_gt_of_filter_length_le (db := db₁) hend hc1 + +/-- **Soundness of `[⊥, keyAfter last)` for a nonpositive-begin truncated forward +result, with no pinning hypothesis.** Agreement fixes the stored keys at or below the +reported last and the begin resolution. If the end key lies at or below the reported +last, the same agreement fixes the end resolution cut at the reported last; otherwise +the reported last lies below the end key and `end_.key ≤ resolve end_` fixes the cut +for every database. -/ +theorem sound_truncatedRangeBottom_of_nonpos_more {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset ≤ 0) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) + (hge : 1 ≤ r.end_.offset) : + Sound r res (truncatedRangeBottom (res.keys.getLast hne)) := by + intro db₁ db₂ hagree + have hbE : db₁.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter (res.keys.getLast hne) : WithTop Key))) = + db₂.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter (res.keys.getLast hne) : WithTop Key))) := by + have h : (truncatedRangeBottom (res.keys.getLast hne)).keys db₁ = + (truncatedRangeBottom (res.keys.getLast hne)).keys db₂ := hagree + unfold truncatedRangeBottom at h + rwa [keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + have hb : db₁.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + db₂.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + have hc : ∀ db : DB, db.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter (res.keys.getLast hne) : WithTop Key))) = + db.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + intro db + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + rwa [hc db₁, hc db₂] at hbE + have hkey_ka : r.begin.key ≤ (keyAfter (res.keys.getLast hne) : WithTop Key) := + le_trans hlo (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt (res.keys.getLast hne)))) + have hres : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below hbegin hkey_ka hbE + have hA : ∀ db : DB, db.keys.filter + (fun k : Key => + decide (resolve db r.begin ≤ (k : WithTop Key) ∧ k ≤ res.keys.getLast hne)) = + (db.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne))).filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key))) := by + intro db + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [Bool.decide_and] + have hck_of_lt : + ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₁ r.end_ → + ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₂ r.end_ → + (RangeRead.completeKeys db₁ r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + (RangeRead.completeKeys db₂ r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + intro hlt₁ hlt₂ + rw [completeKeys_filter_le_eq (db := db₁) hrev hlt₁, + completeKeys_filter_le_eq (db := db₂) hrev hlt₂, + hA db₁, hA db₂, hres, hb] + rw [compatible_iff_truncated_keys hrev hmore hne, + compatible_iff_truncated_keys (db := db₂) hrev hmore hne] + by_cases hE : r.end_.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) + · -- Non-overshoot: the end key lies at or below the reported last. + constructor + · rintro ⟨hlim, hck₁⟩ + have hmem : res.keys.getLast hne ∈ (RangeRead.completeKeys db₁ r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [hck₁] + exact List.getLast_mem hne + have hlt₁ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₁ r.end_ := + (RangeRead.mem_completeKeys.mp (List.mem_of_mem_filter hmem)).2.2 + have hlt₂ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₂ r.end_ := + (lt_resolve_end_iff_of_agree_bottom (db₁ := db₁) (db₂ := db₂) hge hb).mp hlt₁ + exact ⟨hlim, hck_of_lt hlt₁ hlt₂ ▸ hck₁⟩ + · rintro ⟨hlim, hck₂⟩ + have hmem : res.keys.getLast hne ∈ (RangeRead.completeKeys db₂ r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [hck₂] + exact List.getLast_mem hne + have hlt₂ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₂ r.end_ := + (RangeRead.mem_completeKeys.mp (List.mem_of_mem_filter hmem)).2.2 + have hlt₁ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₁ r.end_ := + (lt_resolve_end_iff_of_agree_bottom (db₁ := db₂) (db₂ := db₁) hge hb.symm).mp hlt₂ + exact ⟨hlim, (hck_of_lt hlt₁ hlt₂).symm ▸ hck₂⟩ + · -- The reported last lies below the end key, so every database cuts above it. + have hlast_lt : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := + lt_of_not_ge hE + have hlt : ∀ db : DB, ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + fun db => lt_of_lt_of_le hlast_lt (key_le_resolve_of_one_le (db := db) (s := r.end_) hge) + constructor + · rintro ⟨hlim, hck₁⟩ + exact ⟨hlim, hck_of_lt (hlt db₁) (hlt db₂) ▸ hck₁⟩ + · rintro ⟨hlim, hck₂⟩ + exact ⟨hlim, (hck_of_lt (hlt db₁) (hlt db₂)).symm ▸ hck₂⟩ + +/-- **The nonpositive-begin truncated forward case is minimal with no pinning.** -/ +theorem minimal_truncatedRangeBottom_of_nonpos_more {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hge : 1 ≤ r.end_.offset) : + Minimal r res (truncatedRangeBottom (res.keys.getLast hne)) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hhead_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hlo' : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + le_trans hlo (WithTop.coe_le_coe.mpr hhead_last) + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_truncatedRangeBottom_of_nonpos_more hrev hbegin hmore hne hlo' hge, ?_⟩ + intro k _ hk + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk) + exact (necessary_nil_of_begin_offset_nonpos ⟨db₀, hcomp₀⟩ hrev hne hbegin hlo).of_between + (necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne)) (nil_le_key k) hkle + +/-- A nonempty *truncated forward* result at a nonpositive begin offset, cross-free, +with `1 ≤ end_.offset`. This removes the pinning hypothesis from +`nonposFwdTruncPinned`: the minimal range is still `[⊥, keyAfter last)`. -/ +abbrev nonposFwdTrunc (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset ≤ 0 ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) ∧ + 1 ≤ r.end_.offset + +/-- The cross-free nonpositive-begin forward truncated closed form. -/ +noncomputable def closedFormNonposFwdTrunc (r : RangeRead) (res : Result) : ConflictRange := + truncatedRangeBottom (res.keys.getLastD ([] : Key)) + +/-- **The cross-free nonpositive-begin forward truncated closed form is minimal.** -/ +theorem minimal_closedForm_of_nonpos_fwdTrunc {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposFwdTrunc r res) : + Minimal r res (closedFormNonposFwdTrunc r res) := by + obtain ⟨hmore, hrev, hbegin, hloD, hge⟩ := h + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + unfold closedFormNonposFwdTrunc + rw [hlast] + have hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key) := by + have hloD' : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rwa [← hhead] + exact minimal_truncatedRangeBottom_of_nonpos_more hread' hrev hmore hne hbegin hlo hge + + +set_option maxHeartbeats 2400000 + +/-- Splitting a count below `b` at a threshold `a < b`. -/ +theorem countP_lt_split {l : List Key} {a b : WithTop Key} (hab : a < b) : + l.countP (fun (k : Key) => decide ((k : WithTop Key) < b)) = + l.countP (fun (k : Key) => decide ((k : WithTop Key) ≤ a)) + + l.countP (fun (k : Key) => decide (a < (k : WithTop Key)) && + decide ((k : WithTop Key) < b)) := by + induction l with + | nil => rfl + | cons x rest ih => + simp only [List.countP_cons] + rw [ih] + by_cases hb : ((x : WithTop Key) < b) + · by_cases hle : ((x : WithTop Key) ≤ a) + · have hnb : ¬ (a < (x : WithTop Key)) := not_lt.mpr hle + simp [hb, hle, hnb] + omega + · have hgt : a < (x : WithTop Key) := lt_of_not_ge hle + simp [hb, hle, hgt] + omega + · have hle : ¬ ((x : WithTop Key) ≤ a) := fun h => hb (lt_of_le_of_lt h hab) + simp [hb, hle] + +/-- In the compatible nonpositive-begin, nonpositive-end forward truncated case, +there are at least `(1 - end_.offset)` stored keys strictly between the reported last +and `end_.key`. -/ +theorem filler_count_nonpos_more_nonpos_end {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) : + (1 - r.end_.offset).toNat ≤ + (db.keys.filter (fun (k : Key) => decide (res.keys.getLast hne < k ∧ + ((k : WithTop Key) < r.end_.key)))).length := by + set last := res.keys.getLast hne with hlast + set o := r.end_.offset with ho + set c := db.keys.countP (fun (k : Key) => decide ((k : WithTop Key) < r.end_.key)) + with hc + set d := db.keys.countP + (fun (k : Key) => decide ((k : WithTop Key) ≤ (↑last : WithTop Key))) with hd + set n := (db.keys.filter (fun (k : Key) => decide (last < k ∧ + ((k : WithTop Key) < r.end_.key)))).length with hn + have hlastmem : last ∈ db.keys := + (RangeRead.mem_completeKeys.mp ((RangeRead.compatible_keys_prefix hcomp).subset + (List.getLast_mem hne))).1 + have hlast_res : ((last : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp ((RangeRead.compatible_keys_prefix hcomp).subset + (List.getLast_mem hne))).2.2 + have hlastE : ((last : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_nonpos hend hlastmem hlast_res + have hsplit : c = d + n := by + rw [hc, hd, hn] + have hs := countP_lt_split (l := db.keys) (a := (↑last : WithTop Key)) + (b := r.end_.key) hlastE + have hn' : (db.keys.filter (fun (k : Key) => decide (last < k ∧ + ((k : WithTop Key) < r.end_.key)))).length = + db.keys.countP (fun (k : Key) => decide ((↑last : WithTop Key) < + (k : WithTop Key)) && decide ((k : WithTop Key) < r.end_.key)) := by + rw [List.countP_eq_length_filter] + congr 1 + apply List.filter_congr + intro k _ + rw [Bool.decide_and] + rw [show decide (last < k) = + decide ((↑last : WithTop Key) < (↑k : WithTop Key)) from + decide_eq_decide.mpr (WithTop.coe_lt_coe.symm)] + rw [hn', ← hs] + have hanchor_nonneg : 0 ≤ r.end_.anchor db := by + by_contra hneg + have hnil : resolve db r.end_ = (↑([] : Key) : WithTop Key) := + resolve_eq_nil (lt_of_not_ge hneg) + rw [hnil] at hlast_res + exact absurd hlast_res (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key last))) + have hanchor_eq : r.end_.anchor db = (c : Int) + o - 1 := by + rw [hc, ho] + cases hE : r.end_.key with + | top => rw [anchor_of_top (s := r.end_) hE, countP_lt_top] + | coe e => rw [anchor_of_coe (s := r.end_) hE, countP_lt_coe] + have hd_le : d ≤ db.keys.countP + (fun (k : Key) => decide ((k : WithTop Key) < resolve db r.end_)) := by + rw [hd] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_le_of_lt hk hlast_res + have hres_count : db.keys.countP + (fun (k : Key) => decide ((k : WithTop Key) < resolve db r.end_)) = + min (r.end_.anchor db).toNat db.keys.length := countP_lt_resolve + have hd_le_min : d ≤ min ((c : Int) + o - 1).toNat db.keys.length := by + rw [hres_count, hanchor_eq] at hd_le + exact hd_le + have hanchor_nonneg' : 0 ≤ (c : Int) + o - 1 := by + rw [← hanchor_eq]; exact hanchor_nonneg + have hnat : (((c : Int) + o - 1).toNat : Int) = (c : Int) + o - 1 := + Int.toNat_of_nonneg hanchor_nonneg' + have hd_lt : (d : Int) < (c : Int) + o := by + have h1 : d ≤ ((c : Int) + o - 1).toNat := + le_trans hd_le_min (Nat.min_le_left _ _) + have h1' : (d : Int) ≤ (c : Int) + o - 1 := by + rw [← hnat] + exact_mod_cast h1 + omega + have hc_eq : (c : Int) = (d : Int) + (n : Int) := by + rw [hsplit]; push_cast; ring + have hn_gt : (n : Int) > -(o : Int) := by omega + have hni : (((1 - o).toNat : ℕ) : Int) ≤ (n : Int) := by + have htoNat : (((1 - o).toNat : ℕ) : Int) = (1 : Int) - o := by + rw [Int.toNat_of_nonneg]; omega + rw [htoNat] + omega + exact_mod_cast hni + +/-- At a nonpositive end offset, if the begin selector resolves to `⊥` and the end +selector to a genuine key `w`, the complete keys are exactly the stored keys below +`w`. -/ +theorem completeKeys_eq_of_resolve_nil_coe {db : DB} {r : RangeRead} {w : Key} + (hrev : r.reverse = false) (hend : r.end_.offset ≤ 0) + (hb : resolve db r.begin = ((↑([] : Key) : WithTop Key))) + (he : resolve db r.end_ = (↑w : WithTop Key)) : + RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < (↑w : WithTop Key))) := by + have hwle : (↑w : WithTop Key) ≤ r.end_.key := by + rw [← he]; exact resolve_le_key_of_offset_nonpos hend + rw [completeKeys_eq_filter_of_end_nonpos hrev hend, hb, he, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases h : ((↑k : WithTop Key) < (↑w : WithTop Key)) + · have hlt : ((↑k : WithTop Key)) < r.end_.key := lt_of_lt_of_le h hwle + have hbot : ((↑([] : Key) : WithTop Key)) ≤ (↑k : WithTop Key) := + WithTop.coe_le_coe.mpr (nil_le_key k) + simp [h, hlt, hbot] + · simp [h] + +/-- **The witness for a key above the reported last, in the nonpositive +forward-truncated nonpositive-end case.** We build a database consisting of the +reported keys followed by exactly `m = (1 - end_.offset)` filler keys in +`(last, end_.key)`, one of which is the target key `k`. The begin selector resolves to +`⊥`, the end selector resolves just above the reported last, so the complete keys are +exactly the reported keys; erasing the target lowers the end resolution to the reported +last and drops the last reported key, breaking compatibility. -/ +theorem exists_witness_nonpos_more_nonpos_end {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + {k : Key} (hkgt : res.keys.getLast hne < k) (hklt : (k : WithTop Key) < r.end_.key) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + 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 hfill := filler_count_nonpos_more_nonpos_end hcomp hrev hmore hne hbegin hend + set last := res.keys.getLast hne with hlast + set m := (1 - r.end_.offset).toNat with hm + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hm1 : 1 ≤ m := by rw [hm]; omega + set G : List Key := db.keys.filter + (fun x : Key => decide (last < x ∧ (x : WithTop Key) < r.end_.key)) with hG + have hGlen : m ≤ G.length := hfill + have hGnodup : G.Nodup := by + rw [hG]; exact db.sorted.pairwise.nodup.filter _ + have hGmem : ∀ x ∈ G, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hG] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + -- the raw filler list starts with `k` and then the reservoir without `k` + set raw : List Key := (k :: G.erase k).take m with hraw + have hrawNodup : raw.Nodup := by + rw [hraw] + refine (List.take_sublist m (k :: G.erase k)).nodup ?_ + exact List.nodup_cons.mpr ⟨hGnodup.not_mem_erase, hGnodup.erase k⟩ + have hrawLen : raw.length = m := by + rw [hraw, List.length_take] + apply Nat.min_eq_left + by_cases hkG : k ∈ G + · rw [List.length_cons, List.length_erase_of_mem hkG]; omega + · rw [List.length_cons, List.erase_of_not_mem hkG]; omega + have hrawSub : ∀ x ∈ raw, x = k ∨ x ∈ G := by + intro x hx + rw [hraw] at hx + rcases List.mem_cons.mp (List.mem_of_mem_take hx) with rfl | hx' + · exact Or.inl rfl + · exact Or.inr (List.mem_of_mem_erase hx') + have hrawk : k ∈ raw := by + rw [hraw, List.mem_take_iff_getElem] + refine ⟨0, ?_, rfl⟩ + rw [List.length_cons] + omega + have hrawGt : ∀ x ∈ raw, last < x := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkgt + · exact (hGmem x hx').1 + have hrawLt : ∀ x ∈ raw, (x : WithTop Key) < r.end_.key := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hklt + · exact (hGmem x hx').2 + -- sort the filler + set F : List Key := raw.insertionSort (· ≤ ·) with hF + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp hrawNodup) + have hFlen : F.length = m := by rw [hF, List.length_insertionSort, hrawLen] + have hFk : k ∈ F := hFperm.mem_iff.mp hrawk + have hFmem : ∀ x ∈ F, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + exact ⟨hrawGt x (hFperm.mem_iff.mpr hx), hrawLt x (hFperm.mem_iff.mpr hx)⟩ + have hFne : F ≠ [] := by + intro h; rw [h] at hFlen; simp at hFlen; omega + set D : List Key := res.keys ++ F with hD + have hDsort : D.SortedLT := by + rw [hD] + refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩ + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hFmem b hb).1 + let dbw : DB := ⟨D, hDsort⟩ + have hdbw_keys : dbw.keys = D := rfl + have hlastFhead : last < F.head hFne := (hFmem (F.head hFne) (List.head_mem hFne)).1 + -- the begin selector resolves to `⊥` + have hbegin_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hlo + exact WithTop.not_top_le_coe (res.keys.head hne) hlo + obtain ⟨bk, hbk⟩ := WithTop.ne_top_iff_exists.mp hbegin_ne_top + have hbk_le_head : bk ≤ res.keys.head hne := by + have h := hlo + rw [← hbk] at h + exact WithTop.coe_le_coe.mp h + have hDmem : ∀ x ∈ D, bk ≤ x := by + intro x hx + rw [hD] at hx + rcases List.mem_append.mp hx with hx | hx + · exact le_trans hbk_le_head ((hsort.pairwise.imp le_of_lt).rel_head hx) + · exact le_trans (le_trans hbk_le_head + ((hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne))) (le_of_lt (hFmem x hx).1) + have hcount_begin : D.countP (fun x : Key => decide (x < bk)) = 0 := + countP_lt_eq_zero_of_forall_ge hDmem + have hrb : resolve dbw r.begin = ((↑([] : Key) : WithTop Key)) := by + refine resolve_eq_nil ?_ + rw [anchor_of_coe (s := r.begin) (v := bk) hbk.symm, hcount_begin] + omega + -- every stored key is below `end_.key` + have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hD] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE + · exact (hFmem x hx).2 + have hlenD : D.length = res.keys.length + m := by + rw [hD, List.length_append, hFlen] + have hm_int : (m : Int) = 1 - r.end_.offset := by + rw [hm]; exact Int.toNat_of_nonneg (by omega) + have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + D.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hDlt x hx + have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD] + push_cast + rw [hm_int] + ring + | coe e => + have hce : D.countP (fun x : Key => decide (x < e)) = D.length := by + rw [← countP_lt_coe (l := D) e, ← hE] + exact hcount_end + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD] + push_cast + rw [hm_int] + ring + have ht_lt : res.keys.length < D.length := by rw [hlenD]; omega + have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by + change D[res.keys.length]? = some (F.head hFne) + rw [hD, List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] + have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)] + rw [hanchor_end, Int.toNat_natCast, hDget?] + -- complete keys of the witness are the reported keys + have hckw : RangeRead.completeKeys dbw r = res.keys := by + rw [completeKeys_eq_of_resolve_nil_coe (db := dbw) hrev hend hrb hre] + have hpred : ∀ x ∈ dbw.keys, + decide ((↑x : WithTop Key) < ((↑(F.head hFne) : Key) : WithTop Key)) = + decide (x < F.head hFne) := by + intro x _; rw [decide_eq_decide, WithTop.coe_lt_coe] + rw [List.filter_congr hpred, hdbw_keys, hD, List.filter_append] + have hres_self : res.keys.filter (fun x : Key => decide (x < F.head hFne)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (hle_last x hx) hlastFhead + have hF_nil : F.filter (fun x : Key => decide (x < F.head hFne)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr ((hFsorted.pairwise.imp le_of_lt).rel_head hx) + rw [hres_self, hF_nil, List.append_nil] + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + refine ⟨hcomp.1, ?_⟩ + rw [hckw] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [← hlast] + exact hle_last x hx + have hkdbw : k ∈ dbw.keys := by + rw [hdbw_keys, hD] + exact List.mem_append.mpr (Or.inr hFk) + -- now the erased database + have hk_not_mem_res : k ∉ res.keys := by + intro hk + exact absurd (hle_last k hk) (not_le.mpr hkgt) + have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by + rw [DB.erase_keys, hdbw_keys, hD] + exact List.erase_append_right F hk_not_mem_res + have hD'mem : ∀ x ∈ (dbw.erase k).keys, bk ≤ x := by + intro x hx + exact hDmem x (by + rw [DB.erase_keys, hdbw_keys] at hx + exact List.mem_of_mem_erase hx) + have hrb' : resolve (dbw.erase k) r.begin = ((↑([] : Key) : WithTop Key)) := by + refine resolve_eq_nil ?_ + rw [anchor_of_coe (s := r.begin) (v := bk) hbk.symm, + countP_lt_eq_zero_of_forall_ge hD'mem] + omega + have hD'lt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by + intro x hx + exact hDlt x (by + rw [DB.erase_keys, hdbw_keys] at hx + exact List.mem_of_mem_erase hx) + have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by + rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen] + omega + have hcount_end' : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hD'lt x hx + have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + | coe e => + have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) = + (dbw.erase k).keys.length := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_end' + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne + have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by + rw [Nat.cast_sub ht1]; norm_num + have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_end', hEqInt, Int.toNat_natCast] + have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by + rw [hD', List.getElem?_append] + rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)] + rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)] + congr 1 + rw [hlast, List.getLast_eq_getElem hne] + have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)] + rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?] + have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by + rw [completeKeys_eq_of_resolve_nil_coe (db := dbw.erase k) hrev hend hrb' hre'] + have hpred : ∀ x ∈ (dbw.erase k).keys, + decide ((↑x : WithTop Key) < ((↑last : Key) : WithTop Key)) = + decide (x < last) := by + intro x _; rw [decide_eq_decide, WithTop.coe_lt_coe] + rw [List.filter_congr hpred, hD', List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by + have hcnt : res.keys.countP + (fun x : Key => decide ((↑x : WithTop Key) < ((↑last : Key) : WithTop Key))) = + res.keys.length - 1 := by + rw [countP_lt_coe (l := res.keys) last] + refine countP_lt_eq_of_getElem hsort (by omega) ?_ + simp only [hlast, List.getLast_eq_getElem hne] + have hf : res.keys.filter (fun x : Key => decide (x < last)) = + res.keys.filter (fun x : Key => + decide ((↑x : WithTop Key) < ((↑last : Key) : WithTop Key))) := by + apply List.filter_congr + intro x _ + rw [decide_eq_decide, WithTop.coe_lt_coe] + rw [hf, filter_lt_eq_take_countP hsort ((↑last : Key) : WithTop Key), hcnt, + ← List.dropLast_eq_take] + have hF_fil : (F.erase k).filter (fun x : Key => decide (x < last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (le_of_lt (hFmem x (List.mem_of_mem_erase hx)).1) + rw [hres_fil, hF_fil, List.append_nil] + have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + intro h + have heq := h.2 + rw [hckE] at heq + have hfil : res.keys.dropLast.filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [← hlast] + exact hle_last x (List.mem_of_mem_dropLast hx) + rw [hfil] at heq + have hlen := congrArg List.length heq + rw [List.length_dropLast] at hlen + omega + exact ⟨dbw, hcompw, hkdbw, hnot⟩ + +/-- Every key strictly above the reported last (and below `end_.key`) is necessary in +the nonpositive forward-truncated nonpositive-end case. -/ +theorem necessary_above_last_of_nonpos_more_nonpos_end {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) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + {x : Key} (hxgt : res.keys.getLast hne < x) (hxlt : (x : WithTop Key) < r.end_.key) : + Necessary r res x := by + obtain ⟨db₀, hcomp⟩ := hread + intro cr hs + by_contra hxcr + obtain ⟨dbw, hcompw, -, hnot⟩ := + exists_witness_nonpos_more_nonpos_end hcomp hrev hmore hne hbegin hend hlo hxgt hxlt + exact hnot ((hs dbw (dbw.erase x) + (ConflictRange.keys_erase_of_not_mem cr dbw hxcr).symm).mp hcompw) + +/-- **The nonpositive forward-truncated nonpositive-end cell is `[⊥, end_.key)`.** +Soundness is `sound_bottomRange_of_nonpos_underEnd_nonempty`; keys at or below the +reported last are convex between the (necessary) empty key and the reported keys, and +keys above the reported last are `necessary_above_last_of_nonpos_more_nonpos_end`. -/ +theorem minimal_bottomRange_of_nonpos_more_nonpos_end {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) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) : + Minimal r res ⟨((↑([] : Key) : WithTop Key)), r.end_.key⟩ := by + obtain ⟨db₀, hcomp⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp⟩ + have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.head_mem hne) + obtain ⟨hhead_mem, -, hhead_res⟩ := RangeRead.mem_completeKeys.mp hhead_ck + have hheadE : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_nonpos hend hhead_mem hhead_res + have hle : r.begin.key ≤ r.end_.key := le_trans hlo (le_of_lt hheadE) + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomRange_of_nonpos_underEnd_nonempty hrev hbegin (by omega) hle, ?_⟩ + intro k _ hk + by_cases hk_last : k ≤ res.keys.getLast hne + · by_cases hk_head : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · exact (necessary_nil_of_begin_offset_nonpos hread' hrev hne hbegin hlo).of_between + (necessary_of_mem_result hread' (List.head_mem hne)) (nil_le_key k) + (le_of_lt (WithTop.coe_lt_coe.mp hk_head)) + · exact necessary_of_between_result hread' (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp (not_lt.mp hk_head)) hk_last + · exact necessary_above_last_of_nonpos_more_nonpos_end hread' hmore hrev hne hbegin hend hlo + (not_le.mp hk_last) hk + + +/-- A nonempty *truncated forward* result at a nonpositive begin offset, cross-free, +with `end_.offset ≤ 0`. In this cell the minimal range is `[⊥, end_.key)`. -/ +abbrev nonposFwdTruncNonpos (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = false ∧ r.begin.offset ≤ 0 ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) ∧ + r.end_.offset ≤ 0 + +/-- The cross-free nonpositive-begin forward truncated nonpositive-end closed form. -/ +noncomputable def closedFormNonposFwdTruncNonpos (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((↑([] : Key) : WithTop Key)), r.end_.key⟩ + +/-- **The cross-free nonpositive-begin forward truncated nonpositive-end closed form is +minimal.** -/ +theorem minimal_closedForm_of_nonpos_fwdTruncNonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposFwdTruncNonpos r res) : + Minimal r res (closedFormNonposFwdTruncNonpos r res) := by + obtain ⟨hmore, hrev, hbegin, hloD, hend⟩ := h + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key) := by + have hloD' : r.begin.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + rwa [← hhead] + exact minimal_bottomRange_of_nonpos_more_nonpos_end hread' hmore hrev hne hbegin hend hlo + +end FormalConflictRange diff --git a/FormalConflictRange/NonposRevClosedForm.lean b/FormalConflictRange/NonposRevClosedForm.lean new file mode 100644 index 0000000..046eac2 --- /dev/null +++ b/FormalConflictRange/NonposRevClosedForm.lean @@ -0,0 +1,172 @@ +/- +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.NonposClosedForm +import FormalConflictRange.ClosedFormTotal + +/-! +# Cross-free nonpositive-begin reversed truncated closed form + +At a nonpositive begin offset the begin selector resolves at or below `begin.key`, so +when `begin.key` lies at or below the least reported key every reported key is at or +above the resolution. For a reversed truncated result the compatible databases are +then exactly those whose stored keys at or above the reported last agree, and that +filter is unchanged by replacing the begin selector with an offset-one selector keyed +at the reported last (`completeKeys_filter_ge_eq_nonposRevReduced`). This reduces the +whole cross-free nonpositive reversed truncated region — every end offset, overshoot or +not — to the already-total begin-offset-one rule `closedFormOffsetOneTotal`. Soundness +and minimality transfer because the two reads have the same compatible databases. +-/ + +open FormalConflictRange + +set_option maxHeartbeats 1200000 + +namespace FormalConflictRange + +/-- The reduced read used to close cross-free nonpositive reversed truncated cells: +its begin selector sits at offset one on the reported last key. -/ +noncomputable def nonposRevReduced (r : RangeRead) (res : Result) : RangeRead := + { r with begin := { key := ((res.keys.getLastD ([] : Key) : Key) : WithTop Key), offset := 1 } } + +/-- At offset one keyed at the reported last, a stored key lies at or above the +resolution exactly when it lies at or above the reported last. -/ +theorem last_le_resolve_reduced_iff {db : DB} {last : Key} {k : Key} (hk : k ∈ db.keys) : + last ≤ k ↔ resolve db ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) ≤ + ((k : Key) : WithTop Key) := by + simpa using key_le_resolve_iff_of_offset_one (db := db) + (s := ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector)) rfl hk + +/-- The stored keys at or above the reported last are unchanged by replacing a +nonpositive cross-free begin selector with the offset-one selector keyed at the last. -/ +theorem completeKeys_filter_ge_eq_nonposRevReduced {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : + (RangeRead.completeKeys db r).filter + (fun k : Key => decide (res.keys.getLastD ([] : Key) ≤ k)) = + (RangeRead.completeKeys db (nonposRevReduced r res)).filter + (fun k : Key => decide (res.keys.getLastD ([] : Key) ≤ k)) := by + set last := res.keys.getLastD ([] : Key) + have hlo' : r.begin.key ≤ ((last : Key) : WithTop Key) := hlo + have hred_rev : (nonposRevReduced r res).reverse = true := by + simp [nonposRevReduced, hrev] + have hred_beg : (nonposRevReduced r res).begin = + ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) := rfl + have hred_end : (nonposRevReduced r res).end_ = r.end_ := rfl + rw [RangeRead.completeKeys, RangeRead.completeKeys, hrev, hred_rev] + simp only [ite_true] + rw [List.filter_reverse, List.filter_reverse, List.reverse_inj] + unfold RangeRead.inRange + rw [List.filter_filter, List.filter_filter] + apply List.filter_congr + intro k hk + have hkey := last_le_resolve_reduced_iff (db := db) (last := last) (k := k) hk + have hstart : resolve db r.begin ≤ ((last : Key) : WithTop Key) := + le_trans (resolve_le_key_of_offset_nonpos hbegin) hlo' + rw [hred_beg, hred_end] + by_cases hlk : ((last : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) + · have hred : resolve db r.begin ≤ ((k : Key) : WithTop Key) ↔ + resolve db ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) ≤ + ((k : Key) : WithTop Key) := by + constructor + · intro _; exact hkey.mp (WithTop.coe_le_coe.mp hlk) + · intro _; exact le_trans hstart hlk + have hconj : (resolve db r.begin ≤ ((k : Key) : WithTop Key) ∧ + ((k : Key) : WithTop Key) < resolve db r.end_) ↔ + (resolve db ({ key := ((last : Key) : WithTop Key), offset := 1 } : Selector) ≤ + ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db r.end_) := by + constructor <;> rintro ⟨h1, h2⟩ + · exact ⟨hred.mp h1, h2⟩ + · exact ⟨hred.mpr h1, h2⟩ + rw [decide_eq_true (WithTop.coe_le_coe.mp hlk), Bool.true_and, Bool.true_and, + decide_eq_decide.mpr hconj] + · rw [show decide (last ≤ k) = false + from decide_eq_false_iff_not.mpr (fun h => hlk (WithTop.coe_le_coe.mpr h)), + Bool.false_and, Bool.false_and] + +/-- Compatibility is invariant under the reversed nonpositive reduction. -/ +theorem compatible_iff_nonposRevReduced {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)) : + RangeRead.Compatible db r res ↔ RangeRead.Compatible db (nonposRevReduced r res) res := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hred_rev : (nonposRevReduced r res).reverse = true := by + simp [nonposRevReduced, hrev] + rw [compatible_iff_truncated_keys_rev hrev hmore hne, + compatible_iff_truncated_keys_rev (db := db) (r := nonposRevReduced r res) hred_rev hmore hne] + have hf := completeKeys_filter_ge_eq_nonposRevReduced (db := db) (r := r) (res := res) + hrev hbegin hlo + rw [hlast] at hf + have hlim : (nonposRevReduced r res).withinLimit res.keys ↔ r.withinLimit res.keys := by + simp [nonposRevReduced, RangeRead.withinLimit] + rw [hf] + constructor + · rintro ⟨h1, h2⟩ + exact ⟨hlim.mpr h1, h2⟩ + · rintro ⟨h1, h2⟩ + exact ⟨hlim.mp h1, h2⟩ + +/-- Range soundness is invariant under a change of read that preserves compatibility. -/ +theorem sound_congr_of_compatible_iff {r r' : RangeRead} {res : Result} {cr : ConflictRange} + (h : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res) : + Sound r res cr ↔ Sound r' res cr := by + unfold Sound + constructor <;> intro hs db₁ db₂ hagree + · exact (h db₁).symm.trans ((hs db₁ db₂ hagree).trans (h db₂)) + · exact (h db₁).trans ((hs db₁ db₂ hagree).trans (h db₂).symm) + +/-- Minimality is invariant under a change of read that preserves compatibility. -/ +theorem minimal_congr_of_compatible_iff {r r' : RangeRead} {res : Result} {cr : ConflictRange} + (h : ∀ db, RangeRead.Compatible db r res ↔ RangeRead.Compatible db r' res) : + Minimal r res cr ↔ Minimal r' res cr := by + have hS : ∀ cr, Sound r res cr ↔ Sound r' res cr := + fun cr => sound_congr_of_compatible_iff (cr := cr) h + have hN : ∀ k, Necessary r res k ↔ Necessary r' res k := by + intro k + unfold Necessary + exact ⟨fun hk cr hs => hk cr ((hS cr).mpr hs), + fun hk cr hs => hk cr ((hS cr).mp hs)⟩ + rw [minimal_iff_sound_and_necessary, minimal_iff_sound_and_necessary] + constructor + · rintro ⟨hs, hnec⟩ + exact ⟨(hS cr).mp hs, fun k hk => (hN k).mp (hnec k hk)⟩ + · rintro ⟨hs, hnec⟩ + exact ⟨(hS cr).mpr hs, fun k hk => (hN k).mpr (hnec k hk)⟩ + +/-- The cross-free nonpositive-begin reversed truncated closed form, via the +offset-one reduction. -/ +noncomputable def closedFormNonposRevTrunc (r : RangeRead) (res : Result) : ConflictRange := + closedFormOffsetOneTotal (nonposRevReduced r res) res + +/-- A nonempty *truncated reversed* result at a nonpositive begin offset, cross-free, +with the begin key at or below the least reported key. Every end offset is allowed. -/ +abbrev nonposRevTrunc (r : RangeRead) (res : Result) : Prop := + res.more = true ∧ r.reverse = true ∧ r.begin.offset ≤ 0 ∧ + r.begin.key ≤ ((leastReported r res : Key) : WithTop Key) + +/-- **The cross-free nonpositive-begin reversed truncated closed form is minimal.** -/ +theorem minimal_closedForm_of_nonpos_revTrunc {r : RangeRead} {res : Result} + (hread : Readable r res) (h : nonposRevTrunc r res) : + Minimal r res (closedFormNonposRevTrunc r res) := by + obtain ⟨hmore, hrev, hbegin, hloD⟩ := h + obtain ⟨db, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hlo : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by + simpa [leastReported, hrev] using hloD + have hcompiff : ∀ db, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (nonposRevReduced r res) res := + fun db => compatible_iff_nonposRevReduced hrev hmore hne hbegin hlo + have hread' : Readable (nonposRevReduced r res) res := ⟨db, (hcompiff db).mp hcomp⟩ + have hmin' : Minimal (nonposRevReduced r res) res + (closedFormOffsetOneTotal (nonposRevReduced r res) res) := + minimal_closedForm_of_offset_one_total hread' hne rfl + unfold closedFormNonposRevTrunc + exact (minimal_congr_of_compatible_iff (r := r) (r' := nonposRevReduced r res) + (res := res) (cr := closedFormOffsetOneTotal (nonposRevReduced r res) res) hcompiff).mpr hmin' + +end FormalConflictRange diff --git a/FormalConflictRange/Prototype.lean b/FormalConflictRange/Prototype.lean new file mode 100644 index 0000000..895252d --- /dev/null +++ b/FormalConflictRange/Prototype.lean @@ -0,0 +1,3185 @@ +/- +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.Algorithm + +/-! +# Conflict-range constraints: a prototype + +This file prototypes collecting the rank/difference information a transaction's +reads and results expose, and reading a candidate minimal set of conflict ranges +back off it. It is built on top of the existing development (`cnt`, `resolve`, +`toggle`, `bridge`, `emptyValue`, `completeRange`); no existing definition is +changed. + +## The rank function + +For a database `db` and `w : WithTop Key`, let `ρ db w = cnt db w`, the number of +stored keys strictly below `w`. The key identity (`Empty.lean`, +`cnt_eq_add_between`) is that for `w₁ ≤ w₂` + +`ρ w₂ − ρ w₁ = # { stored keys in [w₁, w₂) }`. + +A difference `ρ hi − ρ lo` is therefore *fixed* by agreement on any conflict +range containing `[lo, hi)`; this is the `supp`ort idea. + +## Difference constraints + +A `Constraint` is one inequality `ρ hi − ρ lo ≤ bound`. A `ConstraintSystem` is a +list of them: the information one read (or a whole transaction) exposes. It is a +conjunction of linear inequalities in the rank variables, i.e. a difference +constraint system. Disjunctions enter only through the empty result's `ψ`; the +existing empty theory resolves those by a read-level branch, so the prototype +carries a `ConstraintDNF`, a list of conjunctive systems paired with `AnyHolds`, one +alternative per branch. `ConstraintDNF` must not be handed to `hullSupports`, which +conjoins its argument. + +## What is proved + +* `constraint_eval_toggle` — a flip at `k` changes `ρ hi − ρ lo` by `±1` exactly + on the support and by `0` off it. +* `system_holdsAt_of_agree` — databases agreeing on a range covering every + support satisfy the system alike; this is soundness as support containment. +* `system_holdsAt_of_supports_hull`, `readOff_sound` — support containment gives + soundness at the `hullSupports` read-off. +* `cap_holdsAt`, `shortGap_not_le` — finite capacities are tautologies, and the + computed short-gap selector makes the second empty disjunct vacuous. +* `necessary_of_tight_support` — a key in the support of a *tight* constraint + whose flip violates the system is necessary, given `Characterizes`. +* `minimal_of_characterizing_system` / `minimal_of_tight_supports` — the direct + assembly: a characterizing system whose read-off `hullSupports` has every key + witnessed by a violated flip is minimal, with no boundary/interior/boundary + decomposition. `unique_minimal_of_characterizing_system` is the up-to-set form. +* `CharacterizesDNF`, `necessary_of_tight_alt`, `minimal_of_characterizing_DNF` — + the disjunctive assembly for the actual collector: necessity breaks *every* + alternative, and `characterizesDNF_collectOf` discharges the characterization from + `preciseCharacterizes`. +* `sound_of_live_supports`, `minimal_of_live_supports_DNF` — the soundness cover over + the alternatives live at each *compatible* database, not over globally feasible + alternatives. The latter (`sound_of_feasible_supports`, kept for the record) is + false on the empty read, because the `nilDb` alternative is feasible via the empty + database with full support (`emptyConstraints_cover_false_on_readPinned`). +* `Observable`, `sound_of_observable`, `observable_of_sound`, `minimal_of_observable_DNF` + — the *exact* soundness criterion. Support containment (feasible or live) is only + sufficient: the crossed always-empty read `crossRead` has empty minimal range yet no + alternative with empty support (`crossRead_support_cover_false`), because a change of + branch compensates. Soundness is observability of the disjunction on `R`. +* `characterizesDNF_emptyConstraints`, `minimal_emptyValue_via_observable` — the + validation: the DNF assembly *reproduces* the empty theory, deriving `emptyValue`'s + minimality from observability plus the witness oracle rather than the dedicated empty + proofs. +* `Observable.mono`, `observable_conjDNFs`, `observable_append_of_support`, + `observable_collectOf_of_boundary`, `observable_collectOf_of_emptyValue` — observability + is a congruence for the collector's construction, so the general soundness obligation + reduces to the required boundary reads' *proven* empty theory plus support containment + of the bracket/core middle. +* `hullRanges`, `collectReadOff`, `sound_collectReadOff` — the concrete computable + read-off (hull of the required boundary `emptyValue`s and the bracket/core supports) + and its **unconditional** soundness, for any realizable read/result with nonempty + ascending reported keys. It validates on the old counterexample `ceRead`, whose + `completeRange` was unsound. +* `sound_congr_toSet`, `minimal_congr_toSet`, `minimal_collectReadOff_of_subrange` — + soundness/minimality depend on a range only through its key set, so the read-off is + minimal once it is a subrange of a known-minimal range. The sole residual is the + containment `read-off ⊆ minimal`, i.e. that the hull does not overshoot (the per-key + flip witnesses in hull form). +* `hull_subrange_of`, `subrange_hullRanges_of`, `subrange_collectReadOff_iff` — the hull + is the least upper bound (`hull_subrange_of`), so the containment residual splits into + per-constituent containments: each boundary `emptyValue` and bracket/core support must + lie in the minimal range. +* `minimal_collectReadOff_of_necessary`, `minimal_collectReadOff_of_necessaryRange` — the + necessity route, the form the main development's per-key/interval necessity theorems + target: since soundness is unconditional, minimality follows from the read-off's keys + (equivalently endpoints, by convexity) being necessary. +* `minimal_inter_of_sound_of_minimal` — meet closure: intersecting a sound range with a + known-minimal one is minimal. This repairs an over-collecting candidate without proving + it contained in the minimal range; `minimal_inter_collectReadOff_of_minimal` is the + wrapper for the read-off, and `minimal_inter_collectReadOff_truncated_fwd`/`_rev` (+ the + overshoot variant) apply it to the truncated branch, where the read-off alone overshoots + (`Counterexample.ct_collectReadOff_not_minimal`). +* `reported_keys_bounds`, `subrange_collectReadOff_canonical`, + `collectReadOff_toSet_eq_canonical`, `minimal_collectReadOff_canonical` — the + canonical-region containment, proved. For a complete nonempty realizable pair in + `1 ≤ begin.offset ∧ end_.offset ≤ 1`, every reported key lies in `[begin.key, end.key)`, + so each boundary `emptyValue` and bracket/core support is contained in the canonical + range; the read-off is contained in it and, being sound, shares its key set. Thus + `collectReadOff` is minimal there, extending end-to-end minimality beyond `ceRead`. +* `subrange_collectReadOff_topRange`, `minimal_collectReadOff_topRange` — the + overshoot-region containment, proved. For a complete nonempty realizable pair in + `1 ≤ begin.offset ∧ 2 ≤ end.offset` with every reported key below `end.key`, the + algorithm's value is the unbounded top `[begin.key, ⊤)` (both traversals), and the + read-off is contained in it: the bracket/core supports and the lower boundary all + start at or above `begin.key`, while the upper boundary read's contribution does too + because its begin selector `⟨keyAfter kₙ, 1⟩` is keyed above the reported last key. + This is the region outside the canonical block, the other half of `completeValue`'s + non-hull answers. +* `collectReadOff_toSet_eq_topRange` — the overshoot counterpart of + `collectReadOff_toSet_eq_canonical`: the read-off and the top range share their key set. +* `minimal_collectReadOff_complete_general` — packages the two non-hull complete regions + (canonical and overshoot) behind one disjunction; the crossed selector order is the only + uncertified complete region. +* `collectOf_holds_of_compatible` — the precise general collector's forward half: a + compatible database satisfies the collected DNF, for every traversal, truncation, + and offset pair. +* `preciseCharacterizes` — precise collection, fully proved: `collectOf`, read at the + ascending reported keys, characterizes compatibility (with the limit factored out). + The reverse half is `precise_reverse`, whose core step `midEq_of_coreConstraints` + recovers the middle from storedness plus the total. +* `intervalCard_headD_getLastD_eq_capSum` — sorted endpoints telescope. + +The read-off is `hullSupports`, a computable fold over the supports, and the +`#eval` demo at the end exercises it. +-/ + +namespace FormalConflictRange + +open RangeRead + +/-! ## Count changes under a single-key flip -/ + +/-- Erasing a stored key `k` lowers every count below a bound `w > k` by one and +leaves the others unchanged, as an integer identity. -/ +theorem cnt_erase_eq (db : DB) (k : Key) (hmem : k ∈ db.keys) (w : WithTop Key) : + (cnt (db.erase k) w : ℤ) = + (cnt db w : ℤ) - (if (k : WithTop Key) < w then 1 else 0) := by + unfold cnt + rw [DB.erase_keys] + have h := List.countP_erase (fun y : Key => decide ((y : WithTop Key) < w)) db.keys k + by_cases hw : (k : WithTop Key) < w + · rw [ite_eq_left hw] + have hc : (if k ∈ db.keys ∧ decide ((k : WithTop Key) < w) = true then (1:ℕ) else 0) = 1 := by + simp [hmem, hw] + rw [hc] at h + have h1 : 1 ≤ db.keys.countP (fun y : Key => decide ((y : WithTop Key) < w)) := + countP_pos_of_mem hmem (by simp [hw]) + rw [h]; push_cast [Nat.cast_sub h1]; ring + · rw [ite_eq_right hw] + have hc : (if k ∈ db.keys ∧ decide ((k : WithTop Key) < w) = true then (1:ℕ) else 0) = 0 := by + simp [hw] + rw [hc] at h + rw [h]; simp + +/-- Inserting an absent key `k` raises every count below a bound `w > k` by one +and leaves the others unchanged, as an integer identity. -/ +theorem cnt_insert_eq (db : DB) (k : Key) (hk : k ∉ db.keys) (w : WithTop Key) : + (cnt (db.insert k hk) w : ℤ) = + (cnt db w : ℤ) + (if (k : WithTop Key) < w then 1 else 0) := by + unfold cnt + rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hk] + by_cases hw : (k : WithTop Key) < w + · rw [ite_eq_left hw] + rw [countP_insertIdx_of_lt db.sorted (by simpa using hw)] + push_cast; ring + · rw [ite_eq_right hw] + rw [countP_insertIdx (fun y : Key => decide ((y : WithTop Key) < w)) _ k (by simp [hw])] + simp + +/-! ## The constraint system -/ + +/-- One difference constraint on the rank function: `ρ hi − ρ lo ≤ bound`. The +orientation is raw; `supp` takes the interval between the endpoints, which is +what the bound's sign is relative to. -/ +structure Constraint where + lo : WithTop Key + hi : WithTop Key + bound : ℤ + deriving DecidableEq, Repr + +/-- The interval a constraint constrains: `[min lo hi, max lo hi)`. Membership +here flips the difference `ρ hi − ρ lo`, so this is exactly the interval a sound +range must contain to see the constraint. -/ +def Constraint.supp (c : Constraint) : ConflictRange := + ⟨min c.lo c.hi, max c.lo c.hi⟩ + +/-- The value of the constraint at a database: `ρ hi − ρ lo`. -/ +def Constraint.eval (c : Constraint) (db : DB) : ℤ := + (cnt db c.hi : ℤ) - (cnt db c.lo : ℤ) + +/-- A constraint holds at a database when its value is within its bound. -/ +def Constraint.HoldsAt (c : Constraint) (db : DB) : Prop := c.eval db ≤ c.bound + +/-- `ρ hi − ρ lo ≤ n`. -/ +def Constraint.le (lo hi : WithTop Key) (n : ℤ) : Constraint := ⟨lo, hi, n⟩ + +/-- `ρ hi − ρ lo ≥ n`, stated as `ρ lo − ρ hi ≤ -n`. -/ +def Constraint.ge (lo hi : WithTop Key) (n : ℤ) : Constraint := ⟨hi, lo, -n⟩ + +/-- A finite-range capacity constraint `ρ hi − ρ lo ≤ n`, for `n` the (finite) +cardinality of `[lo, hi)`. The finiteness is the hypothesis `hfin`, so this is +never confused with an infinite range: an infinite interval contributes no cap. +The cap is a tautology at every database (`Constraint.cap_holdsAt`), but it is the +*tightness threshold* the empty theory's branch uses: when `n` is below the gap +`e − b`, the corresponding disjunct is unsatisfiable for every database. -/ +def Constraint.cap (lo hi : WithTop Key) (n : ℕ) : Constraint := + ⟨lo, hi, (n : ℤ)⟩ + +/-- The capacity constraint holds at every database: no database stores more keys +in `[lo, hi)` than the finite cardinality `n` of the interval. When `hi ≤ lo` the +interval is empty, `n = 0`, and the difference is nonpositive. -/ +theorem Constraint.cap_holdsAt (db : DB) {lo hi : WithTop Key} {n : ℕ} + (hfin : intervalCard lo hi = (n : ℕ∞)) : + (Constraint.cap lo hi n).HoldsAt db := by + rcases le_total lo hi with hle | hle + · have h := cnt_sub_le_intervalCard db hle + rw [hfin] at h + have hd : cnt db hi - cnt db lo ≤ n := by exact_mod_cast h + change ((cnt db hi : ℤ) - (cnt db lo : ℤ)) ≤ (n : ℤ) + rw [← Nat.cast_sub (cnt_le_cnt_of_le db hle)] + exact_mod_cast hd + · have hzero : intervalCard lo hi = 0 := intervalCard_eq_zero_of_le hle + rw [hzero] at hfin + have hn : n = 0 := by simpa using hfin.symm + subst hn + have hmono : cnt db hi ≤ cnt db lo := cnt_le_cnt_of_le db hle + change ((cnt db hi : ℤ) - (cnt db lo : ℤ)) ≤ (0 : ℤ) + omega + +/-- A family of constraints: what a read or a transaction exposes in difference +form. -/ +abbrev ConstraintSystem := List Constraint + +/-- A system holds at a database when every constraint in it does; a conjunction, +i.e. one difference constraint system. -/ +def SystemHoldsAt (S : ConstraintSystem) (db : DB) : Prop := ∀ c ∈ S, c.HoldsAt db + +/-- A system is satisfiable when some database satisfies it. -/ +def Feasible (S : ConstraintSystem) : Prop := ∃ db : DB, SystemHoldsAt S db + +/-- A database violates a system when it fails some constraint. -/ +def SystemViolated (S : ConstraintSystem) (db : DB) : Prop := ∃ c ∈ S, ¬ c.HoldsAt db + +/-- A disjunction of conjunctive difference systems, in disjunctive normal form: +each element of the list is one alternative's conjunction. Unlike `ConstraintSystem` +this is *not* a conjunction, so it must never be fed to `hullSupports`, which would +conjoin the alternatives. It is the carrier for a read whose observation is a +disjunction, such as an empty result's `ψ`. -/ +abbrev ConstraintDNF := List ConstraintSystem + +/-- The DNF holds at a database when some alternative's conjunction does. This is a +union of the alternatives' feasible sets, not their intersection. -/ +def AnyHolds (D : ConstraintDNF) (db : DB) : Prop := ∃ S ∈ D, SystemHoldsAt S db + +/-- A DNF is satisfiable when some database satisfies some alternative. -/ +def FeasibleDNF (D : ConstraintDNF) : Prop := ∃ db : DB, AnyHolds D db + +/-- **A flip off the support leaves the rank difference unchanged.** If `k` is +outside `[min lo hi, max lo hi)`, both endpoints' counts move together, so +`ρ hi − ρ lo` is fixed. This is the complement of `constraint_eval_toggle`. -/ +theorem constraint_eval_toggle_eq_of_not_mem {db : DB} {k : Key} {c : Constraint} + (hk : k ∉ c.supp) : c.eval (toggle db k) = c.eval db := by + rw [Constraint.supp, ConflictRange.mem_iff, not_and_or] at hk + simp only [Constraint.eval] + unfold toggle + by_cases hmem : k ∈ db.keys + · rw [dite_eq_left hmem, cnt_erase_eq db k hmem c.hi, cnt_erase_eq db k hmem c.lo] + rcases hk with hmin | hmax + · -- `k < min lo hi`: both endpoints lie above `k`. + have hltmin := not_le.mp hmin + have hhi : (k : WithTop Key) < c.hi := lt_of_lt_of_le hltmin (min_le_right _ _) + have hlo : (k : WithTop Key) < c.lo := lt_of_lt_of_le hltmin (min_le_left _ _) + rw [ite_eq_left hhi, ite_eq_left hlo]; ring + · -- `max lo hi ≤ k`: both endpoints lie at or below `k`. + have hlemax := not_lt.mp hmax + have hhi : ¬ ((k : WithTop Key) < c.hi) := not_lt.mpr (le_trans (le_max_right _ _) hlemax) + have hlo : ¬ ((k : WithTop Key) < c.lo) := not_lt.mpr (le_trans (le_max_left _ _) hlemax) + rw [ite_eq_right hhi, ite_eq_right hlo]; ring + · rw [dite_eq_right hmem, cnt_insert_eq db k hmem c.hi, cnt_insert_eq db k hmem c.lo] + rcases hk with hmin | hmax + · have hltmin := not_le.mp hmin + have hhi : (k : WithTop Key) < c.hi := lt_of_lt_of_le hltmin (min_le_right _ _) + have hlo : (k : WithTop Key) < c.lo := lt_of_lt_of_le hltmin (min_le_left _ _) + rw [ite_eq_left hhi, ite_eq_left hlo]; ring + · have hlemax := not_lt.mp hmax + have hhi : ¬ ((k : WithTop Key) < c.hi) := not_lt.mpr (le_trans (le_max_right _ _) hlemax) + have hlo : ¬ ((k : WithTop Key) < c.lo) := not_lt.mpr (le_trans (le_max_left _ _) hlemax) + rw [ite_eq_right hhi, ite_eq_right hlo]; ring + +/-- **A flip changes a rank difference exactly on the support.** Flipping the +membership of `k` in `db` changes `ρ hi − ρ lo` by `±1` when `k` lies in the open +support interval `[min lo hi, max lo hi)` and by `0` otherwise. -/ +theorem constraint_eval_toggle {db : DB} {k : Key} {c : Constraint} (hk : k ∈ c.supp) : + (c.eval (toggle db k) = c.eval db + 1) ∨ (c.eval (toggle db k) = c.eval db - 1) := by + rw [Constraint.supp, ConflictRange.mem_iff] at hk + obtain ⟨hk1, hk2⟩ := hk + simp only [Constraint.eval] + unfold toggle + by_cases hmem : k ∈ db.keys + · rw [dite_eq_left hmem, cnt_erase_eq db k hmem c.hi, cnt_erase_eq db k hmem c.lo] + rcases le_total c.lo c.hi with hle | hle + · have hki : (k : WithTop Key) < c.hi := by rwa [max_eq_right hle] at hk2 + have hkj : c.lo ≤ (k : WithTop Key) := by rwa [min_eq_left hle] at hk1 + rw [ite_eq_left hki, ite_eq_right (not_lt.mpr hkj)]; right; ring + · have hki : (k : WithTop Key) < c.lo := by rwa [max_eq_left hle] at hk2 + have hkj : c.hi ≤ (k : WithTop Key) := by rwa [min_eq_right hle] at hk1 + rw [ite_eq_right (not_lt.mpr hkj), ite_eq_left hki]; left; ring + · rw [dite_eq_right hmem, cnt_insert_eq db k hmem c.hi, cnt_insert_eq db k hmem c.lo] + rcases le_total c.lo c.hi with hle | hle + · have hki : (k : WithTop Key) < c.hi := by rwa [max_eq_right hle] at hk2 + have hkj : c.lo ≤ (k : WithTop Key) := by rwa [min_eq_left hle] at hk1 + rw [ite_eq_left hki, ite_eq_right (not_lt.mpr hkj)]; left; ring + · have hki : (k : WithTop Key) < c.lo := by rwa [max_eq_left hle] at hk2 + have hkj : c.hi ≤ (k : WithTop Key) := by rwa [min_eq_right hle] at hk1 + rw [ite_eq_right (not_lt.mpr hkj), ite_eq_left hki]; right; ring + +/-! ## Support containment gives soundness -/ + +/-- **A constraint whose support lies inside a range is seen by that range.** +Two databases agreeing on `R` evaluate the constraint equally when `supp c ⊆ R`. -/ +theorem constraint_eval_eq_of_agree {R : ConflictRange} {db₁ db₂ : DB} + (h : AgreeOn R db₁ db₂) {c : Constraint} (hsub : Subrange c.supp R) : + c.eval db₁ = c.eval db₂ := by + have hkeys : c.supp.keys db₁ = c.supp.keys db₂ := keys_eq_of_subrange_agree hsub h + rcases le_total c.lo c.hi with hle | hle + · -- `lo ≤ hi`: `ρ hi − ρ lo` counts exactly the support's stored keys. + have hsupp : ∀ db : DB, c.supp.keys db = between db c.lo c.hi := by + intro db + unfold ConflictRange.keys between Constraint.supp + rw [min_eq_left hle, max_eq_right hle] + have hb : ∀ db : DB, (cnt db c.lo : ℤ) + + ((c.supp.keys db).length : ℤ) = (cnt db c.hi : ℤ) := by + intro db + rw [hsupp db, cnt_eq_add_between db hle] + push_cast; ring + have hb₁ := hb db₁ + have hb₂ := hb db₂ + rw [hkeys] at hb₁ + simp only [Constraint.eval] + omega + · -- `hi ≤ lo`: the roles reverse, and the same count argument applies to `-`. + have hsupp : ∀ db : DB, c.supp.keys db = between db c.hi c.lo := by + intro db + unfold ConflictRange.keys between Constraint.supp + rw [min_eq_right hle, max_eq_left hle] + have hb : ∀ db : DB, (cnt db c.hi : ℤ) + + ((c.supp.keys db).length : ℤ) = (cnt db c.lo : ℤ) := by + intro db + rw [hsupp db, cnt_eq_add_between db hle] + push_cast; ring + have hb₁ := hb db₁ + have hb₂ := hb db₂ + rw [hkeys] at hb₁ + simp only [Constraint.eval] + omega + +/-- **Support containment gives soundness.** Two databases agreeing on a range +`R` that contains every support in `S` satisfy `S` alike. -/ +theorem system_holdsAt_of_agree {S : ConstraintSystem} {R : ConflictRange} {db₁ db₂ : DB} + (h : AgreeOn R db₁ db₂) (hsub : ∀ c ∈ S, Subrange c.supp R) : + (SystemHoldsAt S db₁ ↔ SystemHoldsAt S db₂) := by + constructor <;> intro hh c hc + · change c.eval db₂ ≤ c.bound + rw [← constraint_eval_eq_of_agree h (hsub c hc)] + exact hh c hc + · change c.eval db₁ ≤ c.bound + rw [constraint_eval_eq_of_agree h (hsub c hc)] + exact hh c hc + +/-- The hull of the supports of a system: a candidate sound conflict range. The +reported interval, when present, is added as an explicit component by the caller. +Structural recursion keeps the induction principle available; the hull operation +is associative and commutative, so the order does not matter. -/ +def hullSupports : ConstraintSystem → Option ConflictRange + | [] => none + | c :: S => some (match hullSupports S with + | none => c.supp + | some R => hull R c.supp) + +/-- The hull of the supports of every system in a transaction. -/ +def transactionRanges (reads : List ConstraintSystem) : Option ConflictRange := + reads.foldl (fun acc S => some (match acc with + | none => hullSupports S |>.getD ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩ + | some R => hull R (hullSupports S |>.getD + ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩))) none + +/-! ## Soundness at the read-off range -/ + +/-- `hullSupports` is `none` only for the empty system. -/ +theorem hullSupports_eq_none_iff {S : ConstraintSystem} : + hullSupports S = none ↔ S = [] := by + constructor + · intro h + cases S with + | nil => rfl + | cons d S => simp only [hullSupports, Option.some_ne_none] at h + · rintro rfl; rfl + +/-- Every support of a system is contained in the hull of its supports, so the +read-off range is sound for the system. -/ +theorem subrange_supp_hullSupports {S : ConstraintSystem} {R : ConflictRange} + (hR : hullSupports S = some R) : ∀ c ∈ S, Subrange c.supp R := by + induction S generalizing R with + | nil => intro c hc; exact absurd hc (by simp) + | cons d S ih => + intro c hc + simp only [hullSupports] at hR + rcases htail : hullSupports S with _ | R' + · -- the tail is empty, so `d` is the only constraint + rw [htail] at hR + have hReq : d.supp = R := Option.some.inj hR + rw [← hReq] + rcases List.mem_cons.mp hc with rfl | hc' + · exact Subrange.refl _ + · rw [hullSupports_eq_none_iff.mp htail] at hc' + exact absurd hc' (by simp) + · rw [htail] at hR + have hReq : hull R' d.supp = R := Option.some.inj hR + rcases List.mem_cons.mp hc with rfl | hc' + · rw [← hReq]; exact subrange_hull_right _ _ + · rw [← hReq] + exact Subrange.trans (ih htail c hc') (subrange_hull_left _ _) + +/-- **The read-off range is sound.** If `hullSupports S = some R`, then any two +databases agreeing on `R` satisfy `S` alike, because every support is inside `R`. +This is `Sound` at the level of the constraint system. -/ +theorem system_holdsAt_of_supports_hull {S : ConstraintSystem} {R : ConflictRange} + (hR : hullSupports S = some R) {db₁ db₂ : DB} (h : AgreeOn R db₁ db₂) : + (SystemHoldsAt S db₁ ↔ SystemHoldsAt S db₂) := + system_holdsAt_of_agree h (subrange_supp_hullSupports hR) + +/-! ## Finite-capacity constraints and the branch selector -/ + +/-- The finite-capacity constraint of an interval, if the interval is finite. An +infinite range contributes nothing. -/ +def Constraint.capOf (lo hi : WithTop Key) : Option Constraint := + match intervalCard lo hi with + | ⊤ => none + | (n : ℕ) => some (Constraint.cap lo hi n) + +/-- The capacity constraint of an interval, as a (possibly empty) system. -/ +def capOfSystem (lo hi : WithTop Key) : ConstraintSystem := + (Constraint.capOf lo hi).toList + +/-- The **short-gap selector**: the second disjunct `f ≥ g` of the empty predicate +is unsatisfiable for every database exactly when the interval `[lo, hi)` holds +fewer than `g` keys. Computable from the read alone, with no database. -/ +def shortGap (g : ℕ) (lo hi : WithTop Key) : Bool := + match intervalCard lo hi with + | ⊤ => false + | (n : ℕ) => decide (n < g) + +/-- **The computed short-gap selector makes the second disjunct vacuous.** If +`shortGap g lo hi` is `true`, then *no* database stores `g` or more keys in +`[lo, hi)`. `shortGap` is the universal (read-only) statement `intervalCard < g`; +each individual database's gap is subordinate to it. -/ +theorem shortGap_not_le (db : DB) {g : ℕ} {lo hi : WithTop Key} (hfin : lo ≤ hi) + (h : shortGap g lo hi = true) : + ¬ (g ≤ (between db lo hi).length) := by + unfold shortGap at h + split at h + · exact absurd h (by simp) + · rename_i n hcard + rw [decide_eq_true_eq] at h + have hcap := between_length_le_intervalCard (db := db) hfin + rw [hcard] at hcap + have hcap' : (between db lo hi).length ≤ n := by + rw [← ENat.natCast_le_natCast]; exact hcap + omega + +/-! ## Collecting the constraints of a read -/ + +/-- The constraints a complete nonempty result exposes, in difference form. `B` +and `E` are the selector keys, `oB`/`oE` their offsets, `L` the reported keys. + +The binding directions are chosen from the bridge analysis of the main notes, +displayed in the single normal form `ρ hi − ρ lo ≤ n` (`ge` is `le` with the +endpoints swapped and the bound negated): + +* each reported key `k` is stored: `ρ (keyAfter k) − ρ k ≥ 1`, i.e. the equality + `ρ (keyAfter k) − ρ k = 1` (the `≤ 1` is free, the interval `[k, keyAfter k)` + having cardinality one, so the `≥` is the binding half); +* the reported interval has exactly `|L|` keys: `ρ (keyAfter kₙ) − ρ k₀ = |L|`; +* each adjacent pair is contiguous: the equality `ρ k_{i+1} − ρ k_i = 1` (given that + `k_i` is stored, its `≤ 1` half forbids any intermediate key); +* the window is bracketed: `ρ begin.key − ρ k₀ ≤ 1 − oB` and + `ρ kₙ − ρ end.key ≤ oE − 2` (the strict inequality `kₙ < resolve end` is `≤ oE−2`); +* each adjacent reported-key gap carries its finite capacity. -/ +def constraintsOf (r : RangeRead) (L : List Key) : ConstraintSystem := + match L with + | [] => [] + | k₀ :: _ => + let kₙ := L.getLastD k₀ + (L.map (fun (k : Key) => + Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1)) ++ + [Constraint.le (k₀ : WithTop Key) + ((keyAfter kₙ : Key) : WithTop Key) (L.length : ℤ), + Constraint.ge (k₀ : WithTop Key) + ((keyAfter kₙ : Key) : WithTop Key) (L.length : ℤ)] ++ + (L.zip (L.drop 1)).flatMap (fun p : Key × Key => + [Constraint.le (p.1 : WithTop Key) (p.2 : WithTop Key) 1, + Constraint.ge (p.1 : WithTop Key) (p.2 : WithTop Key) 1]) ++ + (L.zip (L.drop 1)).flatMap (fun p : Key × Key => + capOfSystem (p.1 : WithTop Key) (p.2 : WithTop Key)) ++ + [Constraint.ge r.begin.key k₀ (r.begin.offset - 1)] ++ + [Constraint.le r.end_.key kₙ (r.end_.offset - 2)] + +/-- The constraints a truncated non-reversed result exposes: a begin-side prefix +of `constraintsOf`, with the far end dropped. -/ +def truncatedConstraints (r : RangeRead) (L : List Key) : ConstraintSystem := + match L with + | [] => [] + | k₀ :: _ => + (L.map (fun (k : Key) => + Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1)) ++ + (L.zip (L.drop 1)).flatMap (fun p : Key × Key => + [Constraint.le (p.1 : WithTop Key) (p.2 : WithTop Key) 1, + Constraint.ge (p.1 : WithTop Key) (p.2 : WithTop Key) 1]) ++ + [Constraint.ge r.begin.key k₀ (r.begin.offset - 1)] + +/-! ## Constraints collected for an empty read + +For an empty complete result the observation is the disjunction `ψ`, read off +`resolve` (via `clampIndex`). The complete keys are empty exactly when the begin +clamped anchor is not below the end clamped anchor, and for arbitrary offsets the +negation of that strict comparison is the four-way disjunction + +* `ρ(E) − ρ(B) ≤ oB − oE`, the end anchor not above the begin anchor: the resolved + window closes because `resolve end ≤ resolve begin`; +* `ρ(⊤) − ρ(B) ≤ oB − 1`, the begin anchor running past the end: the window is + empty because `resolve begin = ⊤` (no key to point at); +* `ρ(B) ≤ 1 − oB ∧ ρ(E) ≤ 1 − oE`, both anchors negative: each selector clamps to + the empty key, so the begin anchor cannot lie below the end anchor either; +* `ρ(⊤) ≤ 0`, the empty database: the window is empty vacuously. + +The first two are the positive-offset disjuncts the empty theory already named; +the last two are the genuinely nonpositive-offset (and empty-database) cases that +`resolve`'s clamp introduces. At positive offsets the last two are unsatisfiable +and the disjunction collapses to the classical pair. + +The four disjuncts are recorded as alternatives of a `ConstraintDNF`, so an empty +read can be handled in the same difference language as a nonempty one. Because +emptiness is a disjunction, the carrier is a *list of alternatives*, not a +conjunction: the read is empty iff at least one alternative holds, and the minimal +range is the hull of the support of whichever alternative is live. -/ + +/-- The first empty disjunct: `resolve end ≤ resolve begin`, i.e. +`ρ(E) − ρ(B) ≤ oB − oE`. -/ +def emptyConstraintEndLe (r : RangeRead) : Constraint := + Constraint.le r.begin.key r.end_.key (r.begin.offset - r.end_.offset) + +/-- The second empty disjunct: `resolve begin = ⊤`, i.e. `ρ(⊤) − ρ(B) ≤ oB − 1`. -/ +def emptyConstraintBeginTop (r : RangeRead) : Constraint := + ⟨r.begin.key, ⊤, r.begin.offset - 1⟩ + +/-- The third empty disjunct's begin half: the begin anchor is negative, i.e. the +count below `begin.key` is at most `1 − oB`. -/ +def emptyConstraintBeginNonpos (r : RangeRead) : Constraint := + Constraint.le (([] : Key) : WithTop Key) r.begin.key (1 - r.begin.offset) + +/-- The third empty disjunct's end half: the end anchor is negative, i.e. the count +below `end_.key` is at most `1 − oE`. -/ +def emptyConstraintEndNonpos (r : RangeRead) : Constraint := + Constraint.le (([] : Key) : WithTop Key) r.end_.key (1 - r.end_.offset) + +/-- The fourth empty disjunct: the database is empty, i.e. `ρ(⊤) ≤ 0`. -/ +def emptyConstraintNilDb (_r : RangeRead) : Constraint := + Constraint.le (([] : Key) : WithTop Key) ⊤ 0 + +/-- The four-alternative system of an empty read, the first two a single constraint +each and the third a two-constraint conjunction. It is *not* a conjunction: the read +is empty when any member holds, i.e. under `AnyHolds`. The last two alternatives are +unsatisfiable at positive offsets, so there it reduces to the classical two. -/ +def emptyConstraints (r : RangeRead) : ConstraintDNF := + [[emptyConstraintEndLe r], + [emptyConstraintBeginTop r], + [emptyConstraintBeginNonpos r, emptyConstraintEndNonpos r], + [emptyConstraintNilDb r]] + +/-- `AnyHolds` of the four-alternative empty DNF is the explicit disjunction of its +alternatives. -/ +theorem anyHolds_emptyConstraints (r : RangeRead) (db : DB) : + AnyHolds (emptyConstraints r) db ↔ + (emptyConstraintEndLe r).HoldsAt db ∨ (emptyConstraintBeginTop r).HoldsAt db ∨ + ((emptyConstraintBeginNonpos r).HoldsAt db ∧ (emptyConstraintEndNonpos r).HoldsAt db) ∨ + (emptyConstraintNilDb r).HoldsAt db := by + simp [AnyHolds, SystemHoldsAt, emptyConstraints] + +/-- **Arithmetic core: the negation of the clamped anchor comparison is a four-way +disjunction.** For integers `a, b` and a natural `n`, `min a.toNat n` is not below +`min b.toNat n` exactly when `b ≤ a`, or the right clamp `n` is already at most `a`, +or both `a` and `b` are nonpositive (both clamp to the empty key), or `n` is zero. -/ +theorem not_lt_min_toNat_dnf (a b : ℤ) (n : ℕ) : + ¬ (min a.toNat n < min b.toNat n) ↔ + (b ≤ a) ∨ ((n : ℤ) ≤ a) ∨ (a ≤ 0 ∧ b ≤ 0) ∨ n = 0 := by + have hA : (a.toNat : ℤ) = max a 0 := Int.toNat_eq_max a + have hB : (b.toNat : ℤ) = max b 0 := Int.toNat_eq_max b + constructor + · intro h + have hle : min b.toNat n ≤ min a.toNat n := not_lt.mp h + by_cases ha : a < 0 + · by_cases hn0 : n = 0 + · exact Or.inr (Or.inr (Or.inr hn0)) + · have hA0 : a.toNat = 0 := by omega + have hminA : min a.toNat n = 0 := by rw [hA0, Nat.zero_min] + have hminB : min b.toNat n = 0 := by omega + have hb0 : b ≤ 0 := by + by_contra hc + have hbpos : 0 < b := lt_of_not_ge hc + have hBpos : 0 < b.toNat := by omega + have : 0 < min b.toNat n := Nat.lt_min.mpr (by omega) + omega + exact Or.inr (Or.inr (Or.inl ⟨le_of_lt ha, hb0⟩)) + · push Not at ha + by_cases hna : (n : ℤ) ≤ a + · exact Or.inr (Or.inl hna) + · push Not at hna + have haN : a.toNat < n := by omega + have hminA : min a.toNat n = a.toNat := Nat.min_eq_left (le_of_lt haN) + have hba : b ≤ a := by + by_contra hc + have hbgt : a < b := lt_of_not_ge hc + have hbpos : 0 < b.toNat := by omega + have hbBn : a.toNat < b.toNat := by omega + have : a.toNat < min b.toNat n := Nat.lt_min.mpr ⟨hbBn, haN⟩ + omega + exact Or.inl hba + · rintro (hba | hna | ⟨ha, hb⟩ | hn0) + · exact not_lt.mpr (min_le_min (Int.toNat_le_toNat hba) le_rfl) + · have hminA : min a.toNat n = n := by rw [Nat.min_eq_right]; omega + have : min b.toNat n ≤ n := Nat.min_le_right _ _ + omega + · have hA0 : a.toNat = 0 := by omega + have hB0 : b.toNat = 0 := by omega + rw [hA0, hB0]; simp + · subst hn0; simp + +/-- Arithmetic: a clamp below a stored index bounds the unclamped anchor above by it. -/ +theorem le_of_min_toNat_le {a : ℤ} {j n : ℕ} (hj : j < n) (h : min a.toNat n ≤ j) : + a ≤ (j : ℤ) := by + have hres' : (min a.toNat n : ℤ) ≤ (j : ℤ) := by exact_mod_cast h + have hminle : (min a.toNat n : ℤ) ≤ (a.toNat : ℤ) := by + exact_mod_cast Nat.min_le_left a.toNat n + have hmax := Int.toNat_eq_max a + omega + +/-- Arithmetic: an index strictly below a clamp is strictly below the unclamped anchor. -/ +theorem lt_of_lt_min_toNat {a : ℤ} {j n : ℕ} (h : j < min a.toNat n) : (j : ℤ) < a := by + have h1 : j < a.toNat := lt_of_lt_of_le h (Nat.min_le_left _ _) + have h2 : (j : ℤ) < (a.toNat : ℤ) := by exact_mod_cast h1 + have h3 := Int.toNat_eq_max a + omega + +/-- **The empty disjunction, read off `resolve`, unconditionally.** A complete result +is empty exactly when one of the four empty alternatives holds. The two extra +alternatives are the clamped cases at nonpositive offsets (both anchors negative) +and the empty database; at positive offsets they are unsatisfiable and this reduces +to the classical disjunction. -/ +theorem completeKeys_eq_nil_iff_emptyConstraints {db : DB} {r : RangeRead} : + RangeRead.completeKeys db r = [] ↔ AnyHolds (emptyConstraints r) db := by + have hAc : clampIndex db r.begin = + min (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) db.keys.length := + clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl + have hEc : clampIndex db r.end_ = + min (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) db.keys.length := + clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl + have key : (RangeRead.completeKeys db r = []) ↔ + ¬ (clampIndex db r.begin < clampIndex db r.end_) := by + rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff] + rw [key, hAc, hEc, not_lt_min_toNat_dnf, anyHolds_emptyConstraints] + have hEnd : (emptyConstraintEndLe r).HoldsAt db ↔ + (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ + (cnt db r.begin.key : ℤ) + r.begin.offset - 1 := by + simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintEndLe, Constraint.le] + omega + have hBeginTop : (emptyConstraintBeginTop r).HoldsAt db ↔ + (db.keys.length : ℤ) ≤ (cnt db r.begin.key : ℤ) + r.begin.offset - 1 := by + simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintBeginTop, cnt_top] + omega + have hBeginNonpos : (emptyConstraintBeginNonpos r).HoldsAt db ↔ + (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤ 0 := by + simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintBeginNonpos, Constraint.le, + cnt_nil] + omega + have hEndNonpos : (emptyConstraintEndNonpos r).HoldsAt db ↔ + (cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ 0 := by + simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintEndNonpos, Constraint.le, + cnt_nil] + omega + have hNilDb : (emptyConstraintNilDb r).HoldsAt db ↔ db.keys.length = 0 := by + simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintNilDb, Constraint.le, + cnt_top, cnt_nil] + omega + simp only [hEnd, hBeginTop, hBeginNonpos, hEndNonpos, hNilDb] + +/-! ## The general collector + +The three shapes above (`constraintsOf` for a complete result, `truncatedConstraints` +for a one-sided one, `emptyConstraints` for the empty result) are not yet the +precise collector for a *general* read/result pair. The precise statement is the +main development's general bridge `compatible_iff_bridge_general`: for the reported +keys `A` in ascending order, compatibility factors as the limit and +`bridgeOf r A more`, whose conjuncts are + +* the boundary read on the reported side being empty (`boundaryReadLo r k₀` for a + non-reversed read, `boundaryReadHi r kₙ` for a reversed one), dropped on a + truncated result's free side; +* `midEq A`, the interval equality `[k₀, keyAfter kₙ).keys(db) = A`; +* `bracket r A`, the two bracketing inequalities. + +Each conjunct is a rank statement, so the collector is a *choice* of which +conjuncts to emit. The middle equality is exactly the pair of total-count +constraints of `constraintsOf`; the boundary read's emptiness is the empty read's +`emptyConstraints` DNF; the bracket is the two bracket constraints. Composing them +gives a `ConstraintDNF` whose alternatives are the ways the boundary DNF can be +satisfied. This section builds that collector and proves *both* halves: a compatible +database satisfies the collected constraints, and conversely the constraints pin the +complete keys down, so the collector is exact. + +The middle-equality constraints are `constraintsOf`'s total pair; to avoid +duplicating the storedness/contiguity/capacity rows, the general collector is +defined by selecting the boundary DNF, prepending the bracket, and appending the +middle pair. -/ + +/-- The middle-equality system of a reported ascending list `A`: the two halves of +`ρ(keyAfter kₙ) − ρ(k₀) = |A|`, normalized to `≤ |A|` and `≤ -|A|`. This is the +sub-system of `constraintsOf A` that encodes `midEq A`, isolated so the general +collector can attach the boundary DNF without re-emitting the per-key rows. -/ +def midConstraints (A : List Key) : ConstraintSystem := + match A with + | [] => [] + | k₀ :: _ => + let kₙ := A.getLastD k₀ + [Constraint.le (k₀ : WithTop Key) ((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ), + Constraint.ge (k₀ : WithTop Key) ((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ)] + +/-- The traversal-independent **core** of the collected constraints: each reported +key is stored (`ρ(keyAfter k) − ρ k ≥ 1`, its `≤ 1` half being free), and the total +is `|A|` over `[k₀, keyAfter kₙ)`, both halves. Storedness plus the total already pin +the middle down to `A` (there are at least `|A|` stored keys, and exactly `|A|`), so +contiguity is redundant for exactness; the adjacent gap capacities are tautologies. +This is the content `midEq A` exposes, and the left conjunct of the reverse +obligation. -/ +def coreConstraints (A : List Key) : ConstraintSystem := + match A with + | [] => [] + | k₀ :: _ => + let kₙ := A.getLastD k₀ + (A.map (fun (k : Key) => + Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1)) ++ + [Constraint.le (k₀ : WithTop Key) + ((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ), + Constraint.ge (k₀ : WithTop Key) + ((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ)] + +/-- **Each reported key is stored.** Under `midEq A`, every `k ∈ A` satisfies +`ρ(keyAfter k) − ρ k ≥ 1`: the interval `[k, keyAfter k)` contains `k` itself, so +it stores at least one key. -/ +theorem coreStored_holds {db : DB} {A : List Key} (_hA : A ≠ []) (hmid : midEq A db) + {k : Key} (hk : k ∈ A) : + (Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1).HoldsAt db := by + have hkstore : k ∈ db.keys := + (inRange_mem_iff.mp (by rw [hmid]; exact hk)).1 + have hle : (k : WithTop Key) ≤ ((keyAfter k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k)) + have hmem : k ∈ between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) := by + rw [between, List.mem_filter] + exact ⟨hkstore, by simp [keyAfter_self_lt]⟩ + have hpos : 0 < (between db (k : WithTop Key) + ((keyAfter k : Key) : WithTop Key)).length := List.length_pos_of_mem hmem + have hcnt := cnt_eq_add_between db hle + change (cnt db (k : WithTop Key) : ℤ) - + (cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) ≤ -1 + have : (cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) = + cnt db (k : WithTop Key) + + ((between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key)).length : ℤ) := by + rw [hcnt]; push_cast; ring + omega + +/-- The bracket system of a read against `A`: the two bracketing inequalities +`ρ(begin.key) − ρ(k₀) ≤ 1 − o_B` and `ρ(kₙ) − ρ(end.key) ≤ o_E − 2`. Empty when `A` +is empty (no reported key to bracket). -/ +def bracketConstraints (r : RangeRead) (A : List Key) : ConstraintSystem := + match A with + | [] => [] + | k₀ :: _ => + let kₙ := A.getLastD k₀ + [Constraint.ge r.begin.key k₀ (r.begin.offset - 1), + Constraint.le r.end_.key kₙ (r.end_.offset - 2)] + +/-- The boundary read on the reported side: lower when not reversed, upper when +reversed. This is the read whose emptiness the truncated bridge asserts. -/ +def reportedBoundaryRead (r : RangeRead) (A : List Key) : RangeRead := + if r.reverse then boundaryReadHi r (A.getLastD ([] : Key)) + else boundaryReadLo r (A.headD ([] : Key)) + +/-- The boundary reads the bridge requires: both ends for a complete result, only +the reported side for a truncated one. This is the traversal/truncation dispatch of +`bridgeOf`, stated as the list of reads whose emptiness is a conjunct. -/ +def boundaryReads (r : RangeRead) (A : List Key) (more : Bool) : List RangeRead := + if more then [reportedBoundaryRead r A] + else [boundaryReadLo r (A.headD ([] : Key)), boundaryReadHi r (A.getLastD ([] : Key))] + +/-- Conjunction of two constraint DNFs: the alternatives are the pairs of one +alternative from each, concatenated. The empty conjunction is the one-element DNF +`[[]]`. -/ +def conjDNF (D₁ D₂ : ConstraintDNF) : ConstraintDNF := + D₁.flatMap (fun S₁ => D₂.map (fun S₂ => S₁ ++ S₂)) + +/-- Conjunction of a list of constraint DNFs, left to right. -/ +def conjDNFs : List ConstraintDNF → ConstraintDNF + | [] => [[]] + | D :: Ds => conjDNF D (conjDNFs Ds) + +/-- The precise constraint DNF collected from a general read and result, given the +reported keys `A` in ascending order and the truncation flag `more`. It conjoins the +empty DNF of every boundary read the bridge requires (both ends for a complete +result, the reported side only for a truncated one) with the bracket and core +systems. Each conjunction is one alternative of the resulting DNF, so the +alternatives enumerate the ways the boundary disjunctions can be resolved. -/ +def collectOf (r : RangeRead) (A : List Key) (more : Bool) : ConstraintDNF := + (conjDNFs ((boundaryReads r A more).map emptyConstraints)).map + (fun S => bracketConstraints r A ++ coreConstraints A ++ S) + +/-- Membership rewrites the boundary-read list: a complete result requires both +boundary reads, a truncated one only the reported side. -/ +theorem mem_boundaryReads {r : RangeRead} {A : List Key} {more : Bool} {s : RangeRead} : + s ∈ boundaryReads r A more ↔ + (if more then s = reportedBoundaryRead r A + else s = boundaryReadLo r (A.headD ([] : Key)) ∨ + s = boundaryReadHi r (A.getLastD ([] : Key))) := by + cases more <;> simp [boundaryReads] + +/-- `bridgeOf` as an explicit `if`, so rewriting on `more` and `reverse` reduces it. +The imported definition uses `match`, which is the same function. -/ +theorem bridgeOf_eq (r : RangeRead) (A : List Key) (more : Bool) (db : DB) : + bridgeOf r A more db ↔ + (if more then (if r.reverse then bridgeRevTrunc r A db else bridgeFwdTrunc r A db) + else bridge r A db) := by + cases more <;> cases hr : r.reverse <;> unfold bridgeOf <;> simp [hr] + +/-- The middle equality is a conjunct of every general bridge. -/ +theorem midEq_of_bridgeOf {db : DB} {r : RangeRead} {A : List Key} {more : Bool} + (h : bridgeOf r A more db) : midEq A db := by + rw [bridgeOf_eq] at h + by_cases hm : more + · by_cases hr : r.reverse + · simp only [hm, hr, bridgeRevTrunc] at h; exact h.2.1 + · simp only [hm, hr, bridgeFwdTrunc] at h; exact h.2.1 + · simp only [hm, bridge] at h; exact h.2.2.1 + +/-- The bracketing is a conjunct of every general bridge. -/ +theorem bracket_of_bridgeOf {db : DB} {r : RangeRead} {A : List Key} {more : Bool} + (h : bridgeOf r A more db) : bracket r A db := by + rw [bridgeOf_eq] at h + by_cases hm : more + · by_cases hr : r.reverse + · simp only [hm, hr, bridgeRevTrunc] at h; exact h.2.2 + · simp only [hm, hr, bridgeFwdTrunc] at h; exact h.2.2 + · simp only [hm, bridge] at h; exact h.2.2.2 + +/-- Every boundary read the collector requires is empty at a bridge-satisfying +database: the lower boundary read for a complete result, and the reported side for +a truncated one. -/ +theorem boundaryReads_empty_of_bridgeOf {db : DB} {r : RangeRead} {A : List Key} {more : Bool} + (h : bridgeOf r A more db) : + ∀ s ∈ boundaryReads r A more, completeKeys db s = [] := by + rw [bridgeOf_eq] at h + intro s hs + by_cases hmore : more + · -- truncated: only the reported side + rw [boundaryReads, ite_eq_left hmore, List.mem_singleton] at hs + subst hs + by_cases hr : r.reverse + · simp only [hmore, hr, bridgeRevTrunc, reportedBoundaryRead] at h ⊢ + exact h.1 + · simp only [hmore, hr, Bool.false_eq_true, bridgeFwdTrunc, reportedBoundaryRead] at h ⊢ + exact h.1 + · -- complete: both ends + rw [boundaryReads, ite_eq_right hmore] at hs + simp only [List.mem_cons, List.not_mem_nil, or_false] at hs + simp only [hmore, bridge] at h + rcases hs with rfl | rfl + · simpa only [List.headD_cons] using h.1 + · simpa only [List.getLastD_cons] using h.2.1 + +/-- The least reported key of a `midEq` interval is stored. -/ +theorem midEq_head_mem {db : DB} {A : List Key} (hA : A ≠ []) (hmid : midEq A db) : + A.headD ([] : Key) ∈ db.keys := + (inRange_mem_iff.mp (by rw [hmid]; exact headD_mem hA)).1 + +/-- The greatest reported key of a `midEq` interval is stored. -/ +theorem midEq_getLast_mem {db : DB} {A : List Key} (hA : A ≠ []) (hmid : midEq A db) : + A.getLastD ([] : Key) ∈ db.keys := + (inRange_mem_iff.mp (by rw [hmid]; exact getLastD_mem hA)).1 + +/-- **The lower bracket as a rank inequality.** If `resolve begin ≤ k₀` and `k₀` is +the least key of a `midEq` interval, then `ρ begin.key + (o_B − 1) ≤ ρ k₀`, i.e. the +constraint `Constraint.ge begin.key k₀ (o_B − 1)` holds. This is the counting form of +`resolve begin ≤ k₀` used by the general collector, and it is total in the offset: +`clampIndex`'s `min` with the database length only lowers the anchor, and +`Int.self_le_toNat` recovers the clamp at the bottom. -/ +theorem cnt_begin_add_offset_sub_one_le_of_resolve_le {db : DB} {r : RangeRead} {A : List Key} + (hA : A ≠ []) (hmid : midEq A db) + (hb : resolve db r.begin ≤ (↑(A.headD ([] : Key)) : WithTop Key)) : + (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤ + (cnt db (A.headD ([] : Key)) : ℤ) := by + set k₀ := A.headD ([] : Key) with hk₀ + have hk₀mem : k₀ ∈ db.keys := midEq_head_mem hA hmid + obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hk₀mem + have hcnt : db.keys.countP (fun x => decide (x < k₀)) = j := + countP_lt_eq_of_getElem db.sorted hj hjk + have hres : clampIndex db r.begin ≤ j := + (resolve_le_getElem_iff (db := db) (s := r.begin) hj).mp (by simpa [hjk] using hb) + have hge : (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤ (j : ℤ) := by + rw [clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl] at hres + exact le_of_min_toNat_le hj hres + have hk₀cnt : (cnt db k₀ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt + rw [hk₀cnt] + exact hge + +/-- **The upper bracket as a rank inequality.** If `kₙ < resolve end` and `kₙ` is the +greatest key of a `midEq` interval, then `ρ kₙ − ρ end.key ≤ o_E − 2`, i.e. the +constraint `Constraint.le end.key kₙ (o_E − 2)` holds. Total in the offset, by the +same `min`-lowering and `Int.self_le_toNat` argument as the lower bracket. -/ +theorem cnt_sub_end_le_of_lt_resolve {db : DB} {r : RangeRead} {A : List Key} + (hA : A ≠ []) (hmid : midEq A db) + (hb : (↑(A.getLastD ([] : Key)) : WithTop Key) < resolve db r.end_) : + (cnt db (A.getLastD ([] : Key)) : ℤ) - (cnt db r.end_.key : ℤ) ≤ + r.end_.offset - 2 := by + set kₙ := A.getLastD ([] : Key) with hkₙ + have hkₙmem : kₙ ∈ db.keys := midEq_getLast_mem hA hmid + obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hkₙmem + have hcnt : db.keys.countP (fun x => decide (x < kₙ)) = j := + countP_lt_eq_of_getElem db.sorted hj hjk + have hres : j < clampIndex db r.end_ := + (getElem_lt_resolve_iff (db := db) (s := r.end_) hj).mp (by simpa [hjk] using hb) + have hlt : (j : ℤ) < (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by + rw [clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl] at hres + exact lt_of_lt_min_toNat hres + have hkₙcnt : (cnt db kₙ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt + rw [hkₙcnt] + omega + + +/-- **The middle system holds.** If `midEq A db`, the two total-count constraints of +`midConstraints A` hold: `ρ(keyAfter kₙ) − ρ(k₀) = |A|`, stated as its two `≤` +halves. -/ +theorem midConstraints_holds {db : DB} {A : List Key} (hA : A ≠ []) + (hpair : A.Pairwise (· < ·)) (hmid : midEq A db) : + SystemHoldsAt (midConstraints A) db := by + obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA + have hpair' : (k₀ :: t).Pairwise (· < ·) := hpair + have hle : k₀ ≤ t.getLastD k₀ := by + simpa only [List.headD_cons, List.getLastD_cons] using + headD_le_of_pairwise hpair' (getLastD_mem (by simp)) + have hleW : (↑k₀ : WithTop Key) ≤ (↑(keyAfter (t.getLastD k₀)) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hle (keyAfter_self_lt _))) + have hcount : (cnt db (keyAfter (t.getLastD k₀)) : ℤ) - (cnt db k₀ : ℤ) = + ((k₀ :: t).length : ℤ) := by + have hbetween : between db (↑k₀ : WithTop Key) (↑(keyAfter (t.getLastD k₀)) : WithTop Key) = + inRange db.keys (↑k₀ : WithTop Key) (↑(keyAfter (t.getLastD k₀)) : WithTop Key) := rfl + have hmid' : inRange db.keys (↑k₀ : WithTop Key) + (↑(keyAfter (t.getLastD k₀)) : WithTop Key) = k₀ :: t := by + have := hmid + simp only [midEq, List.headD_cons, List.getLastD_cons] at this + exact this + have hcnt := cnt_eq_add_between db hleW + rw [hbetween, hmid'] at hcnt + have : (cnt db (↑(keyAfter (t.getLastD k₀)) : WithTop Key) : ℤ) = + cnt db k₀ + ((k₀ :: t).length : ℤ) := by + rw [hcnt]; push_cast; ring + omega + intro c hc + simp only [midConstraints, List.getLastD_cons, List.mem_cons, + List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · change (cnt db (↑(keyAfter (t.getLastD k₀)) : WithTop Key) : ℤ) - + (cnt db (↑k₀ : WithTop Key) : ℤ) ≤ ((k₀ :: t).length : ℤ) + omega + · change (cnt db (↑k₀ : WithTop Key) : ℤ) - + (cnt db (↑(keyAfter (t.getLastD k₀)) : WithTop Key) : ℤ) ≤ -(((k₀ :: t).length : ℤ)) + omega + +/-- **The core system holds under `midEq`.** Every constraint of `coreConstraints A` +holds at a database satisfying `midEq A`: storedness for each reported key +(`coreStored_holds`) and the total pair (`midConstraints_holds`). -/ +theorem coreConstraints_holds {db : DB} {A : List Key} (hA : A ≠ []) + (hpair : A.Pairwise (· < ·)) (hmid : midEq A db) : + SystemHoldsAt (coreConstraints A) db := by + obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA + have hmidS := midConstraints_holds (A := k₀ :: t) (by simp) hpair hmid + intro c hc + rw [coreConstraints] at hc + rcases List.mem_append.mp hc with hc | hc + · rcases List.mem_map.mp hc with ⟨k, hk, rfl⟩ + exact coreStored_holds (by simp) hmid hk + · exact hmidS c hc + +/-- **A storedness constraint forces its key stored.** If +`ρ(keyAfter k) − ρ k ≥ 1` holds, then `k ∈ db.keys`: the only key in +`[k, keyAfter k)` is `k` itself (`eq_of_le_of_lt_keyAfter`), and the interval stores +at least one. -/ +theorem mem_of_coreStored {db : DB} {k : Key} + (h : (Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1).HoldsAt db) : + k ∈ db.keys := by + have hle : (k : WithTop Key) ≤ ((keyAfter k : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k)) + have hcnt := cnt_eq_add_between db hle + have hge : cnt db (k : WithTop Key) + 1 ≤ + cnt db ((keyAfter k : Key) : WithTop Key) := by + change (cnt db (k : WithTop Key) : ℤ) - + (cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) ≤ -1 at h + omega + have hpos : 0 < (between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key)).length := by + omega + obtain ⟨y, hy⟩ := List.exists_mem_of_ne_nil _ + (List.ne_nil_of_length_pos hpos) + simp only [between, List.mem_filter, decide_eq_true_eq] at hy + have hyk : y = k := eq_of_le_of_lt_keyAfter (WithTop.coe_le_coe.mp hy.2.1) + (WithTop.coe_lt_coe.mp hy.2.2) + rw [hyk] at hy + exact hy.1 + +/-- **The core recovers the middle equality.** If `coreConstraints A` holds at `db` +for an ascending `A`, then `midEq A db`. Storedness forces every reported key stored; +the total caps the middle interval's stored count at `|A|`; since `A` itself lies in +that interval, the two ascending duplicate-free lists of equal length coincide. This +is the injectivity step of precise collection, and it is unconditional (the +boundary-read converse is what remains of the reverse). -/ +theorem midEq_of_coreConstraints {db : DB} {A : List Key} (hA : A ≠ []) + (hpair : A.Pairwise (· < ·)) (h : SystemHoldsAt (coreConstraints A) db) : + midEq A db := by + obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA + have hpair' : (k₀ :: t).Pairwise (· < ·) := hpair + have hk₀le_kn : k₀ ≤ (k₀ :: t).getLastD k₀ := + headD_le_of_pairwise hpair' (getLastD_mem (by simp)) + have hleW : (k₀ : WithTop Key) ≤ + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr + (le_of_lt (lt_of_le_of_lt hk₀le_kn (keyAfter_self_lt _))) + -- storedness: every reported key is stored + have hstored : ∀ k ∈ (k₀ :: t), k ∈ db.keys := by + intro k hk + apply mem_of_coreStored + apply h + rw [coreConstraints] + exact List.mem_append.mpr (Or.inl (List.mem_map.mpr ⟨k, hk, rfl⟩)) + -- the total constraint: the middle interval stores exactly |A| keys + have htotal_le : (cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) - + (cnt db (k₀ : WithTop Key) : ℤ) ≤ (((k₀ :: t).length : ℕ) : ℤ) := by + have hc := h (Constraint.le (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) + (((k₀ :: t).length : ℕ) : ℤ)) + (by rw [coreConstraints]; simp) + simpa only [Constraint.HoldsAt, Constraint.eval, Constraint.le] using hc + have htotal_ge : ((k₀ :: t).length : ℤ) ≤ + (cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) - + (cnt db (k₀ : WithTop Key) : ℤ) := by + have hc := h (Constraint.ge (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) + (((k₀ :: t).length : ℕ) : ℤ)) + (by rw [coreConstraints]; simp) + change (cnt db (k₀ : WithTop Key) : ℤ) - + (cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) ≤ + -(((k₀ :: t).length : ℕ) : ℤ) at hc + omega + -- the interval's stored length is |A| + have hlen : (inRange db.keys (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)).length = + (k₀ :: t).length := by + have hcnt := cnt_eq_add_between db hleW + rw [show between db (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) = + inRange db.keys (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) from rfl] at hcnt + have : (cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) = + cnt db (k₀ : WithTop Key) + + ((inRange db.keys (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)).length : ℤ) := by + rw [hcnt]; push_cast; ring + omega + -- A lies in the interval + have hsub : (k₀ :: t) ⊆ inRange db.keys (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) := by + intro k hk + rw [inRange_mem_iff] + refine ⟨hstored k hk, WithTop.coe_le_coe.mpr (headD_le_of_pairwise hpair' hk), ?_⟩ + have hkle : k ≤ (k₀ :: t).getLastD k₀ := le_getLastD_of_pairwise hpair' hk + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkle (keyAfter_self_lt _)) + -- equal ascending duplicate-free lists + have hperm : List.Perm (k₀ :: t) (inRange db.keys (k₀ : WithTop Key) + ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)) := + (List.subperm_of_subset hpair'.nodup hsub).perm_of_length_le (by rw [hlen]) + exact eq_of_mem_iff_pairwise (RangeRead.inRange_pairwise db _ _) hpair' + (fun x => ⟨fun hx => hperm.symm.subset hx, fun hx => hsub hx⟩) + +/-- **The lower bracket constraint recovers the resolved begin.** Conversely to +`cnt_begin_add_offset_sub_one_le_of_resolve_le`: if `ρ begin.key + (o_B−1) ≤ ρ k₀` and +`k₀` is the least key of a `midEq` interval, then `resolve begin ≤ k₀`. Total in the +offset, since `min` only lowers the clamp. -/ +theorem resolve_le_of_cnt_begin_add_offset_sub_one_le {db : DB} {r : RangeRead} + {A : List Key} (hA : A ≠ []) (hmid : midEq A db) + (hcon : (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤ + (cnt db (A.headD ([] : Key)) : ℤ)) : + resolve db r.begin ≤ (↑(A.headD ([] : Key)) : WithTop Key) := by + set k₀ := A.headD ([] : Key) with hk₀ + have hk₀mem : k₀ ∈ db.keys := midEq_head_mem hA hmid + obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hk₀mem + have hcnt : db.keys.countP (fun x => decide (x < k₀)) = j := + countP_lt_eq_of_getElem db.sorted hj hjk + have hk₀cnt : (cnt db k₀ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt + rw [hk₀cnt] at hcon + set a : ℤ := (cnt db r.begin.key : ℤ) + r.begin.offset - 1 with ha + have hclamp : clampIndex db r.begin ≤ j := by + rw [clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl, ← ha] + calc min a.toNat db.keys.length ≤ a.toNat := Nat.min_le_left _ _ + _ ≤ j := by + have hmax : (a.toNat : ℤ) = max a 0 := Int.toNat_eq_max a + have : (a.toNat : ℤ) ≤ (j : ℤ) := by + rw [hmax]; exact max_le hcon (by positivity) + exact_mod_cast this + have := (resolve_le_getElem_iff (db := db) (s := r.begin) hj).mpr (by simpa [hjk] using hclamp) + rwa [hjk] at this + +/-- **The upper bracket constraint recovers the resolved end.** Conversely to +`cnt_sub_end_le_of_lt_resolve`: if `ρ kₙ − ρ end.key ≤ o_E−2` and `kₙ` is the greatest +key of a `midEq` interval, then `kₙ < resolve end`. Total in the offset. -/ +theorem lt_resolve_of_cnt_sub_end_le {db : DB} {r : RangeRead} + {A : List Key} (hA : A ≠ []) (hmid : midEq A db) + (hcon : (cnt db (A.getLastD ([] : Key)) : ℤ) - (cnt db r.end_.key : ℤ) ≤ + r.end_.offset - 2) : + (↑(A.getLastD ([] : Key)) : WithTop Key) < resolve db r.end_ := by + set kₙ := A.getLastD ([] : Key) with hkₙ + have hkₙmem : kₙ ∈ db.keys := midEq_getLast_mem hA hmid + obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hkₙmem + have hcnt : db.keys.countP (fun x => decide (x < kₙ)) = j := + countP_lt_eq_of_getElem db.sorted hj hjk + have hkₙcnt : (cnt db kₙ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt + rw [hkₙcnt] at hcon + have hclamp : j < clampIndex db r.end_ := by + rw [clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl] + refine Nat.lt_min.mpr ⟨?_, hj⟩ + have ha : (j : ℤ) < (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by omega + have hmax : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat : ℤ) = + max ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) 0 := Int.toNat_eq_max _ + have h1 : (j : ℤ) < + max ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) 0 := + lt_of_lt_of_le ha (Int.le_max_left _ _) + rw [← hmax] at h1 + exact_mod_cast h1 + have := (getElem_lt_resolve_iff (db := db) (s := r.end_) hj).mpr (by simpa [hjk] using hclamp) + rwa [hjk] at this + +/-- **A member of a conjunction of DNFs projects to an alternative of each.** If a +specific alternative `S` of `conjDNFs Ds` holds at `db`, then every `D ∈ Ds` has some +alternative holding at `db`. This is the membership form of `anyHolds_conjDNFs`, and +it is what lets the reverse direction read each boundary read's disjunction off the +collected alternative. -/ +theorem anyHolds_of_mem_conjDNFs {Ds : List ConstraintDNF} {S : ConstraintSystem} + {db : DB} (hS : S ∈ conjDNFs Ds) (hh : SystemHoldsAt S db) : + ∀ D ∈ Ds, AnyHolds D db := by + induction Ds generalizing S with + | nil => + rw [conjDNFs, List.mem_singleton] at hS + subst hS + intro D hD + exact absurd hD (by simp) + | cons D Ds ih => + rw [conjDNFs, conjDNF, List.mem_flatMap] at hS + obtain ⟨S₁, hS₁, hS₂⟩ := hS + rw [List.mem_map] at hS₂ + obtain ⟨S₂, hS₂E, rfl⟩ := hS₂ + have hS₁' : SystemHoldsAt S₁ db := + fun c hc => hh c (List.mem_append.mpr (Or.inl hc)) + have hS₂' : SystemHoldsAt S₂ db := + fun c hc => hh c (List.mem_append.mpr (Or.inr hc)) + intro E hE + rcases List.mem_cons.mp hE with rfl | hE + · exact ⟨S₁, hS₁, hS₁'⟩ + · exact ih hS₂E hS₂' E hE + +/-- **The bracket system recovers the bracketing.** Conversely to +`bracketConstraints_holds`: if the two bracketing constraints hold and `midEq A db`, +then `bracket r A db`, using the converse count lemmas. Total in the offsets. -/ +theorem bracket_of_bracketConstraints_holds {db : DB} {r : RangeRead} {A : List Key} + (hA : A ≠ []) (hmid : midEq A db) (h : SystemHoldsAt (bracketConstraints r A) db) : + bracket r A db := by + obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA + have hlo : (cnt db r.begin.key : ℤ) - + (cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) ≤ -(r.begin.offset - 1) := by + have hc := h (Constraint.ge r.begin.key ((k₀ :: t).headD ([] : Key)) + (r.begin.offset - 1)) + (by rw [bracketConstraints]; simp) + change (cnt db r.begin.key : ℤ) - + (cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) ≤ + -(r.begin.offset - 1) at hc + exact hc + have hhi : (cnt db (↑((k₀ :: t).getLastD k₀) : WithTop Key) : ℤ) - + (cnt db r.end_.key : ℤ) ≤ r.end_.offset - 2 := by + have hc := h (Constraint.le r.end_.key ((k₀ :: t).getLastD k₀) (r.end_.offset - 2)) + (by rw [bracketConstraints]; simp) + exact hc + have hge : (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤ + (cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) := by omega + exact ⟨resolve_le_of_cnt_begin_add_offset_sub_one_le (A := k₀ :: t) (by simp) hmid hge, + lt_resolve_of_cnt_sub_end_le (A := k₀ :: t) (by simp) hmid hhi⟩ + +/-- **`bridgeOf` from its three factor families.** If `midEq` and `bracket` hold and +every boundary read required by the dispatch is empty, then `bridgeOf` holds. This +packages the four traversal/truncation cases into one statement. -/ +theorem bridgeOf_of_factors {db : DB} {r : RangeRead} {A : List Key} {more : Bool} + (hmid : midEq A db) (hbrack : bracket r A db) + (hempty : ∀ s ∈ boundaryReads r A more, completeKeys db s = []) : + bridgeOf r A more db := by + rw [bridgeOf_eq] + by_cases hm : more + · by_cases hr : r.reverse + · have h := hempty (reportedBoundaryRead r A) (by + rw [boundaryReads, ite_eq_left hm]; exact List.mem_singleton.mpr rfl) + rw [reportedBoundaryRead, ite_eq_left hr] at h + simp only [hm, hr, ↓reduceIte] + exact ⟨h, hmid, hbrack⟩ + · have h := hempty (reportedBoundaryRead r A) (by + rw [boundaryReads, ite_eq_left hm]; exact List.mem_singleton.mpr rfl) + rw [reportedBoundaryRead, ite_eq_right (by simpa using hr)] at h + simp only [hm, hr, Bool.false_eq_true, ↓reduceIte] + exact ⟨h, hmid, hbrack⟩ + · have hlo := hempty (boundaryReadLo r (A.headD ([] : Key))) (by + rw [boundaryReads, ite_eq_right hm]; exact List.mem_cons_self) + have hhi := hempty (boundaryReadHi r (A.getLastD ([] : Key))) (by + rw [boundaryReads, ite_eq_right hm] + exact List.mem_cons_of_mem _ (List.mem_singleton.mpr rfl)) + simp only [hm, bridge] + exact ⟨hlo, hhi, hmid, hbrack⟩ + +/-- **The bracket system holds.** If `midEq A db` and `bracket r A db`, the two +bracketing constraints of `bracketConstraints r A` hold, for every offset pair: +the counting forms are total once the clamp is absorbed by `Int.self_le_toNat`. -/ +theorem bracketConstraints_holds {db : DB} {r : RangeRead} {A : List Key} + (hA : A ≠ []) (hmid : midEq A db) (hbrack : bracket r A db) : + SystemHoldsAt (bracketConstraints r A) db := by + obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA + have hb : resolve db r.begin ≤ (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) := hbrack.1 + have hb' : (↑((k₀ :: t).getLastD k₀) : WithTop Key) < resolve db r.end_ := hbrack.2 + intro c hc + simp only [bracketConstraints, List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · change (cnt db r.begin.key : ℤ) - + (cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) ≤ -(r.begin.offset - 1) + have h := cnt_begin_add_offset_sub_one_le_of_resolve_le (r := r) (A := k₀ :: t) + (by simp) hmid hb + omega + · have h := cnt_sub_end_le_of_lt_resolve (r := r) (A := k₀ :: t) (by simp) hmid hb' + change (cnt db (↑((k₀ :: t).getLastD k₀) : WithTop Key) : ℤ) - + (cnt db r.end_.key : ℤ) ≤ r.end_.offset - 2 + exact h + +/-- **The conjunction of two DNFs holds iff both do.** -/ +theorem anyHolds_conjDNF {D₁ D₂ : ConstraintDNF} {db : DB} : + AnyHolds (conjDNF D₁ D₂) db ↔ (AnyHolds D₁ db ∧ AnyHolds D₂ db) := by + simp only [AnyHolds, conjDNF, List.mem_flatMap, List.mem_map] + constructor + · rintro ⟨S, ⟨S₁, hS₁, S₂, hS₂, rfl⟩, hS⟩ + refine ⟨⟨S₁, hS₁, fun c hc => hS c (List.mem_append.mpr (Or.inl hc))⟩, + ⟨S₂, hS₂, fun c hc => hS c (List.mem_append.mpr (Or.inr hc))⟩⟩ + · rintro ⟨⟨S₁, hS₁, h₁⟩, ⟨S₂, hS₂, h₂⟩⟩ + refine ⟨S₁ ++ S₂, ⟨S₁, hS₁, S₂, hS₂, rfl⟩, ?_⟩ + intro c hc + rcases List.mem_append.mp hc with hc | hc + · exact h₁ c hc + · exact h₂ c hc + +/-- **The conjunction of a list of DNFs holds iff every one does.** This is the +`conjDNFs` counterpart of `anyHolds_conjDNF`: the empty conjunction `[[]]` holds +trivially, and the alternatives of the result are the tuples of alternatives. -/ +theorem anyHolds_conjDNFs {Ds : List ConstraintDNF} {db : DB} : + AnyHolds (conjDNFs Ds) db ↔ ∀ D ∈ Ds, AnyHolds D db := by + induction Ds with + | nil => simp [conjDNFs, AnyHolds, SystemHoldsAt] + | cons D Ds ih => + rw [conjDNFs, anyHolds_conjDNF, ih] + constructor + · rintro ⟨hD, hrest⟩ E hE + rcases List.mem_cons.mp hE with rfl | hE + · exact hD + · exact hrest E hE + · intro h + exact ⟨h D (by simp), fun E hE => h E (by simp [hE])⟩ + +/-- **The general collection is sound (forward half).** If `db` is compatible with +the result `⟨reportedKeys r A, more⟩`, then `db` satisfies `collectOf r A more`: there +is one alternative of the collected DNF that holds. This is the precise-collection +*forward* direction, the part that is mechanical counting from the bridge. It is the +`AnyHolds` analogue of `Characterizes`'s `→`, instantiated at the general +traversal/truncation dispatch. + +The proof unwinds `compatible_iff_bridge_general`, sends the middle equality to the +`midConstraints` pair and the bracketing to `bracketConstraints`, and turns each +required boundary read's emptiness into its `emptyConstraints` alternative via +`completeKeys_eq_nil_iff_emptyConstraints`. Both boundary reads carry the empty +theory's offset hypotheses. -/ +theorem collectOf_holds_of_compatible {db : DB} {r : RangeRead} {A : List Key} {more : Bool} + (hA : A ≠ []) (hpair : A.Pairwise (· < ·)) + (hlim : withinLimit r (reportedList r A)) + (hcomp : RangeRead.Compatible db r { keys := reportedList r A, more := more }) : + AnyHolds (collectOf r A more) db := by + have hbo := (compatible_iff_bridge_general (db := db) (r := r) (A := A) (more := more) + hA hpair hlim).mp hcomp |>.2 + have hmid := midEq_of_bridgeOf hbo + have hbrack := bracket_of_bridgeOf hbo + have hmidS := coreConstraints_holds hA hpair hmid + have hbrS := bracketConstraints_holds hA hmid hbrack + -- each required boundary read's emptiness gives its `emptyConstraints` alternative + have hbalts : ∀ s ∈ boundaryReads r A more, AnyHolds (emptyConstraints s) db := by + intro s hs + have hempty := boundaryReads_empty_of_bridgeOf hbo s hs + by_cases hm : more + · -- truncated: `s` is the reported side + rw [boundaryReads, ite_eq_left hm] at hs + simp only [List.mem_singleton] at hs + subst hs + by_cases hr : r.reverse + · rw [reportedBoundaryRead, ite_eq_left hr] at hempty ⊢ + exact (completeKeys_eq_nil_iff_emptyConstraints (db := db) + (r := boundaryReadHi r (A.getLastD ([] : Key)))).mp hempty + · rw [reportedBoundaryRead, ite_eq_right (by simpa using hr)] at hempty ⊢ + exact (completeKeys_eq_nil_iff_emptyConstraints (db := db) + (r := boundaryReadLo r (A.headD ([] : Key)))).mp hempty + · -- complete: both ends + rw [boundaryReads, ite_eq_right (by simpa using hm)] at hs + simp only [List.mem_cons, List.not_mem_nil, or_false] at hs + rcases hs with rfl | rfl + · exact (completeKeys_eq_nil_iff_emptyConstraints (db := db) + (r := boundaryReadLo r (A.headD ([] : Key)))).mp hempty + · exact (completeKeys_eq_nil_iff_emptyConstraints (db := db) + (r := boundaryReadHi r (A.getLastD ([] : Key)))).mp hempty + have hconj : AnyHolds (conjDNFs ((boundaryReads r A more).map emptyConstraints)) db := by + rw [anyHolds_conjDNFs] + intro D hD + rw [List.mem_map] at hD + obtain ⟨s, hs, rfl⟩ := hD + exact hbalts s hs + rw [collectOf] + simp only [AnyHolds, List.mem_map] at hconj ⊢ + obtain ⟨S, hS, hSh⟩ := hconj + refine ⟨bracketConstraints r A ++ coreConstraints A ++ S, ⟨S, hS, rfl⟩, ?_⟩ + intro c hc + rcases List.mem_append.mp hc with hc | hc + · rcases List.mem_append.mp hc with hc | hc + · exact hbrS c hc + · exact hmidS c hc + · exact hSh c hc + +/-- The reported keys of a result in ascending order: `res.keys` unchanged for a +forward read, reversed for a reversed one. This is the `A` the collector expects. -/ +def ascendingKeys (r : RangeRead) (res : Result) : List Key := + if r.reverse then res.keys.reverse else res.keys + +/-- `reportedList` recovers `res.keys` from its ascending order: the two +traversal-dependent flips cancel. -/ +theorem reportedList_ascendingKeys (r : RangeRead) (res : Result) : + reportedList r (ascendingKeys r res) = res.keys := by + unfold reportedList ascendingKeys + by_cases hr : r.reverse <;> simp [hr] + +/-- **Precise collection.** The collected DNF characterizes the read/result pair +exactly, together with the limit: a database is compatible iff the result respects +the limit and some alternative of `collectOf` at the ascending reported keys holds. +The limit appears because it is not a rank statement and is not collected. -/ +def PreciseCharacterizes (r : RangeRead) (res : Result) : Prop := + ∀ db : DB, + RangeRead.Compatible db r res ↔ + withinLimit r res.keys ∧ AnyHolds (collectOf r (ascendingKeys r res) res.more) db + +/-- The forward half of precise collection, stated on a result: compatibility +implies the collected DNF holds. This is `collectOf_holds_of_compatible` with `A` +recovered from `res`, and is unconditional. -/ +theorem precise_forward {db : DB} {r : RangeRead} {res : Result} + (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) + (hlim : withinLimit r res.keys) + (hcomp : RangeRead.Compatible db r res) : + AnyHolds (collectOf r (ascendingKeys r res) res.more) db := + collectOf_holds_of_compatible hA hpair + (by rw [reportedList_ascendingKeys]; exact hlim) + (by rw [reportedList_ascendingKeys]; exact hcomp) + +/-- **The reverse half of precise collection.** If a database satisfies the +collected DNF, respects the limit, and the reported keys are nonempty and ascending, +then it is compatible. The core recovers `midEq` (`midEq_of_coreConstraints`), the +bracket constraints recover the bracketing +(`bracket_of_bracketConstraints_holds`), each collected boundary disjunct gives its +read's emptiness, and `bridgeOf_of_factors` assembles the bridge that +`compatible_iff_bridge_general` turns into compatibility. Unlike `Characterizes`, +this direction is now proved, for every offset pair: the core's storedness plus total +pin the middle down, and the total empty DNF's extra clamp branches supply the +boundary emptiness. -/ +theorem precise_reverse {db : DB} {r : RangeRead} {res : Result} + (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) + (hlim : withinLimit r res.keys) + (hany : AnyHolds (collectOf r (ascendingKeys r res) res.more) db) : + RangeRead.Compatible db r res := by + set A := ascendingKeys r res with hAdef + -- decompose the alternative: bracket ++ core ++ boundary-disjuncts + rw [collectOf] at hany + simp only [AnyHolds, List.mem_map] at hany + obtain ⟨S, ⟨S₀, hS₀, rfl⟩, hSh⟩ := hany + have hbr : SystemHoldsAt (bracketConstraints r A) db := + fun c hc => hSh c (List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl hc)))) + have hcore : SystemHoldsAt (coreConstraints A) db := + fun c hc => hSh c (List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hc)))) + have hS' : SystemHoldsAt S₀ db := + fun c hc => hSh c (List.mem_append.mpr (Or.inr hc)) + have hmid := midEq_of_coreConstraints hA hpair hcore + have hbrack := bracket_of_bracketConstraints_holds hA hmid hbr + -- each required boundary read is empty + have hempty : ∀ s ∈ boundaryReads r A res.more, completeKeys db s = [] := by + intro s hs + have hD : emptyConstraints s ∈ (boundaryReads r A res.more).map emptyConstraints := + List.mem_map.mpr ⟨s, hs, rfl⟩ + have hany' := anyHolds_of_mem_conjDNFs hS₀ hS' (emptyConstraints s) hD + exact (completeKeys_eq_nil_iff_emptyConstraints (db := db) (r := s)).mpr hany' + have hbo : bridgeOf r A res.more db := bridgeOf_of_factors hmid hbrack hempty + have hres : res = { keys := reportedList r A, more := res.more } := by + rw [reportedList_ascendingKeys] + rw [hres] + exact (compatible_iff_bridge_general (db := db) (r := r) (A := A) (more := res.more) + hA hpair (by rw [reportedList_ascendingKeys]; exact hlim)).mpr + ⟨by rw [reportedList_ascendingKeys]; exact hlim, hbo⟩ + +/-- **Precise collection, assembled.** The forward and reverse halves together give +`PreciseCharacterizes`. Both are now proved, for every offset pair: the collected DNF, +read at the ascending reported keys, characterizes compatibility. -/ +theorem preciseCharacterizes {r : RangeRead} {res : Result} + (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) : + PreciseCharacterizes r res := by + intro db + constructor + · intro hcomp + exact ⟨compatible_withinLimit hcomp, + precise_forward hA hpair (compatible_withinLimit hcomp) hcomp⟩ + · rintro ⟨hlim, hany⟩ + exact precise_reverse hA hpair hlim hany + +/-! ## The shared witness satisfies the collected system + +The collection soundness we actually use is local: the system is a conjunction of +rank identities, and a *single* database that realizes the read's result satisfies +all of them at once. The prototype checks this computationally below; the +framework theorem `system_holdsAt_of_supports_hull` turns it into soundness of the +read-off range. -/ + +/-- **Constraint collection is sound at a shared witness.** Fix one database and +one constraint system satisfied there; the read-off range built from the system's +supports is sound: any database agreeing on it satisfies the same system. This is +the single-witness soundness the transaction needs, independent of how the +constraints were collected. -/ +theorem readOff_sound {S : ConstraintSystem} {db : DB} + (h : SystemHoldsAt S db) : + ∀ db', AgreeOn (hullSupports S |>.getD + ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩) db db' → + (SystemHoldsAt S db') := by + intro db' hagree + cases hR : hullSupports S with + | none => + -- no constraints: the system is empty and holds trivially + intro c hc + rw [hullSupports_eq_none_iff.mp hR] at hc + exact absurd hc (by simp) + | some R => + rw [hR, Option.getD_some] at hagree + exact (system_holdsAt_of_supports_hull hR hagree).mp h + +/-! ## Necessity: a tight support is necessary + +The constructive direction of necessity. If a collected system *characterizes* +the read's compatibility, and the flip at `k` violates a constraint whose support +contains `k`, then `k` lies in every sound range for the read: any sound range +omitting `k` would see the witness and its flip as agreeing, transferring the +compatibility that the flip destroys. -/ + +/-- A constraint system characterizes a read/result pair when satisfying the +system is exactly being compatible with the result. -/ +def Characterizes (S : ConstraintSystem) (r : RangeRead) (res : Result) : Prop := + ∀ db : DB, RangeRead.Compatible db r res ↔ SystemHoldsAt S db + +/-- **A key whose flip violates a tight constraint is necessary.** Given a +characterizing system, a witness `db` satisfying it, a constraint `c ∈ S`, and a +flip at `k` that violates `c`, the key `k` is `Necessary` for the read/result +pair. This is the constructive form of necessity: the active set is the union, +over the shared witness, of the supports of the constraints the witness makes +tight — and `k` lies in `c`'s support automatically, since a flip off the support +leaves the constraint unchanged (`constraint_eval_toggle_eq_of_not_mem`). -/ +theorem necessary_of_tight_support {r : RangeRead} {res : Result} + {S : ConstraintSystem} {db : DB} {k : Key} {c : Constraint} + (hchar : Characterizes S r res) + (hc : c ∈ S) (hdb : SystemHoldsAt S db) + (hviol : ¬ c.HoldsAt (toggle db k)) : + Necessary r res k := by + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db (toggle db k) := agreeOn_toggle_of_not_mem cr db hkcr + have hcomp : RangeRead.Compatible db r res := (hchar db).mpr hdb + have hcomp₁ : RangeRead.Compatible (toggle db k) r res := + (hs db (toggle db k) hagree).mp hcomp + exact hviol (((hchar (toggle db k)).mp hcomp₁) c hc) + +/-- **Tight-support minimality.** Let `S` characterize `(r, res)`, let the witness +`db` satisfy `S`, and let `R` be the hull of `S`'s supports. If every key of `R` is +*witnessed* by a constraint of `S` that the single-key flip at it violates, then `R` +is the minimal sound conflict range. This is the direct assembly: soundness is +support containment (`system_holdsAt_of_supports_hull` through the characterization), +and necessity is `necessary_of_tight_support` at the witnessing constraint. + +The keys the observations force are exactly the union, over the shared witness, of +the supports of the constraints the witness makes tight; a key in the support of a +slack constraint does nothing, and the theorem asks only for a violating constraint +per key, not that each support be tight throughout. The containment of the slack +supports (that they do not drag `R` past the witnessed keys) is folded into the +witness hypothesis, which is what a collector discharges case by case. -/ +theorem minimal_of_characterizing_system {r : RangeRead} {res : Result} + {S : ConstraintSystem} {db : DB} {R : ConflictRange} + (hchar : Characterizes S r res) (hdb : SystemHoldsAt S db) + (hR : hullSupports S = some R) + (hnec : ∀ k, k ∈ R → ∃ c ∈ S, ¬ c.HoldsAt (toggle db k)) : + Minimal r res R := by + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hsys : SystemHoldsAt S db₁ ↔ SystemHoldsAt S db₂ := + system_holdsAt_of_supports_hull hR hagree + rw [hchar db₁, hchar db₂, hsys] + · intro k hk + obtain ⟨c, hcS, hviol⟩ := hnec k hk + exact necessary_of_tight_support hchar hcS hdb hviol + +/-- **Tight-support minimality, support form.** A corollary of +`minimal_of_characterizing_system` phrased at the supports: if every key of every +support of `S` is witnessed by a violating flip, and the supports of `S` cover `R` +(every key of `R` lies in some support), then `R = hullSupports S` is minimal. -/ +theorem minimal_of_tight_supports {r : RangeRead} {res : Result} + {S : ConstraintSystem} {db : DB} {R : ConflictRange} + (hchar : Characterizes S r res) (hdb : SystemHoldsAt S db) + (hR : hullSupports S = some R) + (hwit : ∀ c ∈ S, ∀ k ∈ c.supp, ∃ c' ∈ S, ¬ c'.HoldsAt (toggle db k)) + (hcover : ∀ k, k ∈ R → ∃ c ∈ S, k ∈ c.supp) : + Minimal r res R := + minimal_of_characterizing_system hchar hdb hR fun k hk => + let ⟨c, hcS, hksupp⟩ := hcover k hk + hwit c hcS k hksupp + +/-- **The tight-support read-off is *the* minimal range, up to set equality.** The +uniqueness companion of `minimal_of_characterizing_system`: any other minimal sound +range denotes the same key set, by the unconditional `Minimal.toSet_eq`. -/ +theorem unique_minimal_of_characterizing_system {r : RangeRead} {res : Result} + {S : ConstraintSystem} {db : DB} {R : ConflictRange} + (hchar : Characterizes S r res) (hdb : SystemHoldsAt S db) + (hR : hullSupports S = some R) + (hnec : ∀ k, k ∈ R → ∃ c ∈ S, ¬ c.HoldsAt (toggle db k)) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_of_characterizing_system hchar hdb hR hnec) + +/-! ## The disjunctive collector: feasibility-gated read-off + +A general read's collected object is a `ConstraintDNF`, not a conjunction: an empty +result's `ψ` is a disjunction, and its alternatives become conjunctive alternatives +of the collector. The candidate read-off must therefore be formed over the +alternatives that *could be live* --- those some database satisfies --- rather than a +single live alternative. Hulling only the live alternative's support is unsound: for +a crossed empty read the live alternative `ρ(E) − ρ(B) ≤ oB − oE` has support +`[E, B)`, yet the minimal range is `[E, ⊤)`, because the *other* feasible +alternative `ρ(⊤) − ρ(B) ≤ oB − 1` (support `[B, ⊤)`) can be the live one at another +compatible database. Since compatibility is the disjunction over the feasible +alternatives, agreement on the hull of their supports fixes every feasible +alternative, hence the disjunction. + +Necessity is correspondingly disjunctive: a key is forced when the single-key flip +breaks *every* alternative (`¬ AnyHolds`), since the flip could otherwise switch to a +different feasible alternative. -/ + +/-- A DNF characterizes a read/result pair when compatibility is exactly the limit +together with the disjunction holding. This is the `ConstraintDNF` analogue of +`Characterizes`; the collector's `preciseCharacterizes` discharges it. -/ +def CharacterizesDNF (D : ConstraintDNF) (r : RangeRead) (res : Result) : Prop := + ∀ db : DB, RangeRead.Compatible db r res ↔ + withinLimit r res.keys ∧ AnyHolds D db + +/-- **Disjunctive necessity: a flip that breaks every alternative forces its key.** +Given a characterizing DNF and a compatible witness, if the single-key flip at `k` +makes `AnyHolds` fail, then `k` is necessary. The proof is the toggle argument once +more: any sound range omitting `k` sees the witness and its flip as agreeing, so +soundness would carry compatibility to the flip, contradicting the failed +disjunction. -/ +theorem necessary_of_tight_alt {r : RangeRead} {res : Result} {D : ConstraintDNF} + {db : DB} {k : Key} + (hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res) + (hviol : ¬ AnyHolds D (toggle db k)) : + Necessary r res k := by + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db (toggle db k) := agreeOn_toggle_of_not_mem cr db hkcr + have hcomp' : RangeRead.Compatible (toggle db k) r res := + (hs db (toggle db k) hagree).mp hcomp + exact hviol ((hchar (toggle db k)).mp hcomp').2 + +/-- **Disjunctive soundness from a feasible-support cover.** If every constraint of +every *feasible* alternative has its support inside `R`, then `R` is sound: a +compatible database has some live alternative, which is feasible, so agreement on +`R` fixes all of that alternative's constraints and hence keeps the disjunction +true. + +The hypothesis is *not* the one to target at the concrete collector: for +`emptyConstraints` the globally-feasible `nilDb` alternative has full support, making +this cover false on a trivially-empty read (see +`emptyConstraints_cover_false_on_readPinned`). Use `sound_of_live_supports` instead. -/ +theorem sound_of_feasible_supports {r : RangeRead} {res : Result} {D : ConstraintDNF} + {R : ConflictRange} (hchar : CharacterizesDNF D r res) + (hsub : ∀ S, S ∈ D → (∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp R) : + Sound r res R := by + intro db₁ db₂ hagree + rw [hchar db₁, hchar db₂] + refine ⟨fun h => ?_, fun h => ?_⟩ + · obtain ⟨hlim, S, hS, hSh⟩ := h + exact ⟨hlim, S, hS, fun c hc => by + change c.eval db₂ ≤ c.bound + rw [← constraint_eval_eq_of_agree hagree (hsub S hS ⟨db₁, hSh⟩ c hc)] + exact hSh c hc⟩ + · obtain ⟨hlim, S, hS, hSh⟩ := h + exact ⟨hlim, S, hS, fun c hc => by + change c.eval db₁ ≤ c.bound + rw [constraint_eval_eq_of_agree hagree (hsub S hS ⟨db₂, hSh⟩ c hc)] + exact hSh c hc⟩ + +/-- **Disjunctive tight-support minimality.** Given a characterizing DNF, a +compatible witness, soundness of `R`, and that every key of `R` has its flip break +the whole disjunction, `R` is minimal. Soundness and necessity are the two halves of +`minimal_iff_sound_and_necessary`. -/ +theorem minimal_of_characterizing_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF} + {db : DB} {R : ConflictRange} + (hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res) + (hsound : Sound r res R) + (hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) : + Minimal r res R := + (minimal_iff_sound_and_necessary).mpr + ⟨hsound, fun k hk => necessary_of_tight_alt hchar hcomp (hnec k hk)⟩ + +/-- **Disjunctive tight-support minimality, assembled.** The feasible-support cover +supplies soundness, so the only hypotheses are the characterization, a compatible +witness, the cover, and the flip-breaking necessity for `R`'s keys. -/ +theorem minimal_of_feasible_supports_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF} + {db : DB} {R : ConflictRange} + (hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res) + (hsub : ∀ S, S ∈ D → (∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp R) + (hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) : + Minimal r res R := + minimal_of_characterizing_DNF hchar hcomp (sound_of_feasible_supports hchar hsub) hnec + +/-- **Disjunctive soundness from a live-support cover.** The correct cover is not +over globally feasible alternatives but over the alternatives *live at each compatible +database*: for every compatible `db` there is an alternative holding at `db` whose +supports lie in `R`. Then `R` is sound — the transfer from `db₁` to an agreeing `db₂` +uses the alternative live at `db₁`, whose constraints `db₂` fixes. This avoids the +`nilDb` trap: the globally-feasible full-support alternative is never forced on a +compatible database that has another live disjunct. -/ +theorem sound_of_live_supports {r : RangeRead} {res : Result} {D : ConstraintDNF} + {R : ConflictRange} (hchar : CharacterizesDNF D r res) + (hsub : ∀ db, RangeRead.Compatible db r res → ∃ S ∈ D, + SystemHoldsAt S db ∧ ∀ c ∈ S, Subrange c.supp R) : + Sound r res R := by + intro db₁ db₂ hagree + rw [hchar db₁, hchar db₂] + refine ⟨fun h => ?_, fun h => ?_⟩ + · obtain ⟨S, hS, hSh, hsupp⟩ := hsub db₁ ((hchar db₁).mpr h) + exact ⟨h.1, S, hS, fun c hc => by + change c.eval db₂ ≤ c.bound + rw [← constraint_eval_eq_of_agree hagree (hsupp c hc)] + exact hSh c hc⟩ + · obtain ⟨S, hS, hSh, hsupp⟩ := hsub db₂ ((hchar db₂).mpr h) + exact ⟨h.1, S, hS, fun c hc => by + change c.eval db₁ ≤ c.bound + rw [constraint_eval_eq_of_agree hagree (hsupp c hc)] + exact hSh c hc⟩ + +/-- **Disjunctive tight-support minimality, live-support form.** The corrected +assembly: the live-support cover supplies soundness, the flip-breaking condition +supplies necessity. This is the DNF companion of `minimal_of_characterizing_system`, +and the form a concrete read-off must target. -/ +theorem minimal_of_live_supports_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF} + {db : DB} {R : ConflictRange} + (hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res) + (hsub : ∀ db', RangeRead.Compatible db' r res → ∃ S ∈ D, + SystemHoldsAt S db' ∧ ∀ c ∈ S, Subrange c.supp R) + (hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) : + Minimal r res R := + minimal_of_characterizing_DNF hchar hcomp (sound_of_live_supports hchar hsub) hnec + +/-- **The collector characterizes.** `preciseCharacterizes` reads exactly as +`CharacterizesDNF` at the collector, for nonempty ascending reported keys, with the +limit factored out. -/ +theorem characterizesDNF_collectOf {r : RangeRead} {res : Result} + (hA : ascendingKeys r res ≠ []) (hpair : (ascendingKeys r res).Pairwise (· < ·)) : + CharacterizesDNF (collectOf r (ascendingKeys r res) res.more) r res := + fun db => preciseCharacterizes hA hpair db + +/-- **The disjunctive read-off is *the* minimal range, up to set equality.** The +uniqueness companion of `minimal_of_characterizing_DNF`. -/ +theorem unique_minimal_of_characterizing_DNF {r : RangeRead} {res : Result} + {D : ConstraintDNF} {db : DB} {R : ConflictRange} + (hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res) + (hsound : Sound r res R) + (hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_of_characterizing_DNF hchar hcomp hsound hnec) + +/-! ## The `nilDb` alternative blocks the feasible-support cover + +The disjunctive assembly `sound_of_feasible_supports` asks that every constraint of +every *feasible* alternative have its support inside `R`. For the empty read's +`emptyConstraints`, the fourth alternative `emptyConstraintNilDb` is feasible — the +empty database satisfies `ρ(⊤) − ρ(⊥) ≤ 0` — and its single constraint has full +support `[⊥, ⊤)`. So the cover forces every key into `R`. But a trivially-empty read +has `emptyRange` as its minimal range, which contains no key. Hence the cover is +*false* for `emptyConstraints` on a trivially-empty read, and the feasible-support +form of the direct assembly cannot be instantiated there as it stands. + +The obstacle is structural: `Feasible` (`∃ db, SystemHoldsAt S db`) is *not* the right +weakening, because the empty database — which satisfies `nilDb` — is not compatible +with any nonempty read and pins down no key. The cover must hull the supports of the +alternatives that are live at a database *compatible with the read*, not merely at +some database. This is the read-level feasibility the plan already isolated for +`shortGap`; the `nilDb` alternative shows the two notions are not equivalent, and it +is exactly why the concrete read-off `R` cannot be `hullSupports` over all feasible +alternatives. -/ + +/-- **The empty-database alternative is feasible with full support.** Its one +constraint `ρ(⊤) − ρ(⊥) ≤ 0` holds at the empty database, and its support is +`[⊥, ⊤)`, so it contains every key. -/ +theorem emptyConstraintNilDb_feasible_and_supp (r : RangeRead) : + SystemHoldsAt [emptyConstraintNilDb r] emptyDB ∧ + ∀ k : Key, k ∈ (emptyConstraintNilDb r).supp := by + refine ⟨?_, fun k => ?_⟩ + · intro c hc + rw [List.mem_singleton] at hc + subst hc + simp only [emptyConstraintNilDb, Constraint.HoldsAt, Constraint.eval, Constraint.le, + emptyDB, cnt_top, cnt_nil] + norm_num + · simp only [emptyConstraintNilDb, Constraint.le, Constraint.supp] + rw [ConflictRange.mem_iff] + exact ⟨le_trans (min_le_left _ _) (WithTop.coe_le_coe.mpr (nil_le_key k)), + by rw [max_eq_right le_top]; exact WithTop.coe_lt_top k⟩ + +/-- **The feasible-support cover is false for `emptyConstraints` on a trivially-empty +read.** The `nilDb` alternative is feasible via the empty database but its support is +full, while `emptyRange` holds no key. -/ +theorem emptyConstraints_cover_false_on_readPinned : + ¬ (∀ S, S ∈ emptyConstraints readPinned → + (∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp (emptyRange readPinned)) := by + intro h + have hsupp := (emptyConstraintNilDb_feasible_and_supp readPinned).2 + have hmem : [emptyConstraintNilDb readPinned] ∈ emptyConstraints readPinned := by + simp [emptyConstraints] + have hfeas : ∃ d : DB, SystemHoldsAt [emptyConstraintNilDb readPinned] d := + ⟨emptyDB, (emptyConstraintNilDb_feasible_and_supp readPinned).1⟩ + exact not_mem_emptyRange readPinned ([] : Key) + (h _ hmem hfeas _ (List.mem_singleton.mpr rfl) ([] : Key) (hsupp ([] : Key))) + +/-- **The intended empty-read instantiation is blocked.** There is a realizable +trivially-empty read whose minimal range is the empty range, yet for which the +feasible-support cover `hsub` of `sound_of_feasible_supports` is false. So the +feasible-support form of the direct assembly cannot be discharged against +`emptyConstraints` without refining "feasible" to read-level feasibility. -/ +theorem emptyConstraints_feasible_cover_blocked : + ∃ r : RangeRead, Readable r emptyRes ∧ Minimal r emptyRes (emptyRange r) ∧ + ¬ (∀ S, S ∈ emptyConstraints r → + (∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp (emptyRange r)) := + ⟨readPinned, ⟨emptyDB, compatible_emptyDB_emptyRes readPinned⟩, + minimal_emptyRange_of_alwaysEmpty alwaysEmpty_readPinned, + emptyConstraints_cover_false_on_readPinned⟩ + +/-- **The live-support cover is *satisfied* at `readPinned`, by the nonpositive +alternative.** Every compatible database (there is only the empty one, and in fact any +database at all) makes the third alternative +`begin_nonpos ∧ end_nonpos` hold; both its constraints have empty support, which is +contained in the empty range. So the corrected cover is not vacuous — the same read +that refutes the feasible cover satisfies the live one. -/ +theorem emptyConstraints_live_cover_readPinned : + ∀ db, RangeRead.Compatible db readPinned emptyRes → + ∃ S ∈ emptyConstraints readPinned, SystemHoldsAt S db ∧ + ∀ c ∈ S, Subrange c.supp (emptyRange readPinned) := by + intro db _ + refine ⟨[emptyConstraintBeginNonpos readPinned, emptyConstraintEndNonpos readPinned], + by simp [emptyConstraints], ?_, ?_⟩ + · intro c hc + simp only [List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintBeginNonpos, + Constraint.le, readPinned, cnt_nil] + norm_num + · simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintEndNonpos, + Constraint.le, readPinned, cnt_nil] + norm_num + · intro c hc + simp only [List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl <;> + intro k hk <;> + · simp only [emptyConstraintBeginNonpos, emptyConstraintEndNonpos, Constraint.le, + Constraint.supp, readPinned, min_self, max_self, emptyRange] at hk ⊢ + exact absurd hk.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k))) + +/-! ## Support containment is only sufficient: the exact criterion is observability + +The live-support cover repairs the `nilDb` trap, but it is *still* too strong, and the +gap is not an artefact of one alternative. Soundness of a DNF is a statement about +*whether the disjunction holds*, not about where its supports lie: two databases can +disagree on the supports of every alternative while still agreeing on the truth value +of the disjunction, because a *change of branch* can compensate. Support containment is +a sufficient condition — `observable_of_support` — but not a necessary one. + +The clean example is an always-empty read with **crossed nonempty selectors**: +`begin = ⟨[1], 1⟩`, `end = ⟨[0], 1⟩`. Every database has `completeKeys = []` +(`alwaysEmpty_crossRead`), because the begin anchor `ρ([1])` is at least the end anchor +`ρ([0])`, so the read is trivially empty and its minimal range is `emptyRange`, which +holds no key. Yet `emptyConstraints` has no alternative with empty support: `endLe` +spans `[[0,0], [1])`, `beginTop` spans `[[1], ⊤)`, and the two nonpositive alternatives +each span up to `[0]` (`crossRead_no_alt_supports_empty`). Hence even the live-support +cover is *false* here (`crossRead_support_cover_false`), while the empty range is sound +because the disjunction is constantly true. + +The correct criterion is therefore not support containment but **observability**: +agreement on `R` fixes whether the disjunction holds. -/ + +/-- A DNF is **observable** on a range `R` when agreement on `R` fixes whether the +disjunction holds. This is the exact soundness content for a DNF, weaker than support +containment: a range can miss an alternative's support yet still see the disjunction, +because the missed information only ever moves a database between alternatives. -/ +def Observable (D : ConstraintDNF) (R : ConflictRange) : Prop := + ∀ db₁ db₂, AgreeOn R db₁ db₂ → (AnyHolds D db₁ ↔ AnyHolds D db₂) + +/-- **Observability gives soundness.** For a characterizing DNF, if agreement on `R` +fixes the disjunction then `R` is sound. This is the exact form; support containment is +the special case `Observable.of_support`. -/ +theorem sound_of_observable {r : RangeRead} {res : Result} {D : ConstraintDNF} + {R : ConflictRange} (hchar : CharacterizesDNF D r res) (hobs : Observable D R) : + Sound r res R := by + intro db₁ db₂ hagree + rw [hchar db₁, hchar db₂] + exact ⟨fun h => ⟨h.1, (hobs db₁ db₂ hagree).mp h.2⟩, + fun h => ⟨h.1, (hobs db₁ db₂ hagree).mpr h.2⟩⟩ + +/-- **Soundness gives observability**, given the limit is fixed. For a characterizing +DNF and a result within the limit, the range's soundness is exactly observability of +the disjunction. The limit side condition is unavoidable: `Sound` also transports the +limit, which the disjunction does not see. -/ +theorem observable_of_sound {r : RangeRead} {res : Result} {D : ConstraintDNF} + {R : ConflictRange} (hchar : CharacterizesDNF D r res) + (hwl : RangeRead.withinLimit r res.keys) (hs : Sound r res R) : + Observable D R := by + intro db₁ db₂ hagree + have h1 := hs db₁ db₂ hagree + rw [hchar db₁, hchar db₂] at h1 + constructor + · intro h; exact (h1.mp ⟨hwl, h⟩).2 + · intro h; exact (h1.mpr ⟨hwl, h⟩).2 + +/-- **Support containment is sufficient for observability.** If every alternative's +every support lies in `R`, agreement on `R` fixes each constraint and hence the +disjunction. The converse is false (`crossRead_support_cover_false`). -/ +theorem observable_of_support {D : ConstraintDNF} {R : ConflictRange} + (hsub : ∀ S ∈ D, ∀ c ∈ S, Subrange c.supp R) : Observable D R := by + intro db₁ db₂ hagree + constructor <;> rintro ⟨S, hS, hSh⟩ + · exact ⟨S, hS, fun c hc => by + change c.eval db₂ ≤ c.bound + rw [← constraint_eval_eq_of_agree hagree (hsub S hS c hc)] + exact hSh c hc⟩ + · exact ⟨S, hS, fun c hc => by + change c.eval db₁ ≤ c.bound + rw [constraint_eval_eq_of_agree hagree (hsub S hS c hc)] + exact hSh c hc⟩ + +/-- **Disjunctive tight-support minimality, observability form.** The exact assembly: +observability supplies soundness (via `sound_of_observable`), the flip-breaking +condition supplies necessity. A concrete read-off must establish observability of the +collector's DNF on `R`, not support containment. -/ +theorem minimal_of_observable_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF} + {db : DB} {R : ConflictRange} + (hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res) + (hobs : Observable D R) + (hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) : + Minimal r res R := + minimal_of_characterizing_DNF hchar hcomp (sound_of_observable hchar hobs) hnec + +/-! ### A crossed always-empty read refutes support containment + +`begin = ⟨[1], 1⟩`, `end = ⟨[0], 1⟩`: crossed nonempty selectors, trivially empty. -/ + +/-- The key `[0]`. -/ +def crossKey0 : Key := [(0 : Byte)] + +/-- The key `[1]`. -/ +def crossKey1 : Key := [(1 : Byte)] + +/-- The key `[0, 0]`, strictly between `[0]` and `[1]`. -/ +def crossKey00 : Key := [(0 : Byte), (0 : Byte)] + +/-- A crossed read: begin keyed at `[1]`, end keyed at `[0]`, both at offset one. -/ +def crossRead : RangeRead where + begin := ⟨(crossKey1 : WithTop Key), 1⟩ + end_ := ⟨(crossKey0 : WithTop Key), 1⟩ + limit := none + reverse := false + +/-- **The crossed read is trivially empty.** Its begin anchor `ρ([1])` is at least its +end anchor `ρ([0])`, so the resolved window is always empty. -/ +theorem alwaysEmpty_crossRead : AlwaysEmpty crossRead := by + intro db + rw [show RangeRead.completeKeys db crossRead = [] ↔ + ¬ (clampIndex db crossRead.begin < clampIndex db crossRead.end_) from by + rw [← completeKeys_ne_nil_iff]; simp] + intro hlt + rw [clampIndex_begin_eq (db := db) (r := crossRead) rfl, + clampIndex_end_eq (db := db) (r := crossRead)] at hlt + simp only [crossRead] at hlt + have hle : cnt db (↑crossKey0) ≤ cnt db (↑crossKey1) := + cnt_le_cnt_of_le db (by decide) + omega + +/-- The collector characterizes the crossed read's empty result. -/ +theorem crossRead_characterizes : + CharacterizesDNF (emptyConstraints crossRead) crossRead emptyRes := by + intro db + change RangeRead.Compatible db crossRead { keys := [], more := false } ↔ _ + rw [compatible_empty_iff_complete_nil] + refine ⟨fun h => ⟨?_, completeKeys_eq_nil_iff_emptyConstraints.mp h⟩, + fun h => completeKeys_eq_nil_iff_emptyConstraints.mpr h.2⟩ + unfold RangeRead.withinLimit; simp [crossRead] + +/-- **The empty range is observable on the crossed read**, because the collector is +constantly satisfied there. -/ +theorem crossRead_observable_emptyRange : + Observable (emptyConstraints crossRead) (emptyRange crossRead) := + observable_of_sound crossRead_characterizes trivial + (sound_emptyRange_of_alwaysEmpty alwaysEmpty_crossRead) + +/-- A range containing a key cannot be a subrange of the empty range. -/ +theorem not_subrange_emptyRange_of_mem {r : RangeRead} {cr : ConflictRange} {k : Key} + (hk : k ∈ cr) : ¬ Subrange cr (emptyRange r) := by + intro h + have hk' := h k hk + simp only [emptyRange, ConflictRange.mem_iff] at hk' + exact absurd hk'.2 (not_lt.mpr hk'.1) + +/-- **No alternative of `emptyConstraints crossRead` has all supports in the empty +range.** The four alternatives have supports `[[0,0],[1))`, `[[1],⊤)`, `[⊥,[0])`, and +`[⊥,⊤)` respectively, each nonempty. -/ +theorem crossRead_no_alt_supports_empty : + ∀ S ∈ emptyConstraints crossRead, ¬ (∀ c ∈ S, Subrange c.supp (emptyRange crossRead)) := by + intro S hS h + simp only [emptyConstraints, List.mem_cons, List.not_mem_nil, or_false] at hS + rcases hS with rfl | rfl | rfl | rfl + · exact not_subrange_emptyRange_of_mem (r := crossRead) + (cr := (emptyConstraintEndLe crossRead).supp) (k := crossKey00) (by + simp only [emptyConstraintEndLe, Constraint.le, Constraint.supp, ConflictRange.mem_iff, + crossRead, crossKey0, crossKey1, crossKey00] + constructor <;> decide) + (h _ (List.mem_singleton.mpr rfl)) + · exact not_subrange_emptyRange_of_mem (r := crossRead) + (cr := (emptyConstraintBeginTop crossRead).supp) (k := crossKey1) (by + simp only [emptyConstraintBeginTop, Constraint.supp, ConflictRange.mem_iff, + crossRead, crossKey0, crossKey1] + constructor <;> decide) + (h _ (List.mem_singleton.mpr rfl)) + · exact not_subrange_emptyRange_of_mem (r := crossRead) + (cr := (emptyConstraintBeginNonpos crossRead).supp) (k := ([] : Key)) (by + simp only [emptyConstraintBeginNonpos, Constraint.le, Constraint.supp, + ConflictRange.mem_iff, crossRead, crossKey0, crossKey1] + constructor <;> decide) + (h _ (by simp)) + · exact not_subrange_emptyRange_of_mem (r := crossRead) + (cr := (emptyConstraintNilDb crossRead).supp) (k := ([] : Key)) (by + simp only [emptyConstraintNilDb, Constraint.le, Constraint.supp, + ConflictRange.mem_iff] + exact ⟨by simp, WithTop.coe_lt_top _⟩) + (h _ (List.mem_singleton.mpr rfl)) + +/-- **The support cover (feasible or live) is false on the crossed read.** No +alternative of `emptyConstraints` has all its supports in the minimal range +`emptyRange`, yet that range is sound — so support containment is not necessary for +soundness. -/ +theorem crossRead_support_cover_false : + ¬ (∀ S ∈ emptyConstraints crossRead, ∀ c ∈ S, Subrange c.supp (emptyRange crossRead)) := + fun h => crossRead_no_alt_supports_empty [emptyConstraintEndLe crossRead] + (by simp [emptyConstraints]) (h _ (by simp [emptyConstraints])) + +/-! ### The observability assembly reproduces the empty theory + +With the exact criterion in hand, the direct assembly closes the empty-read instance +end-to-end: `emptyConstraints` characterizes the empty result, its `Observable` is +soundness of `emptyValue` (read off the *proven* witness oracle), and the keys of +`emptyValue` are necessary by their flip witnesses. The result is `emptyValue`'s +minimality, but reached through the DNF assembly rather than the dedicated empty-theory +route — the validation that the direct formulation is not merely a certificate rule. -/ + +/-- **`emptyConstraints` characterizes the empty result**, with the limit factored out: +an empty result is compatible exactly when the four-way disjunction holds +(`completeKeys_eq_nil_iff_emptyConstraints`). -/ +theorem characterizesDNF_emptyConstraints (r : RangeRead) : + CharacterizesDNF (emptyConstraints r) r emptyRes := by + intro db + change RangeRead.Compatible db r { keys := [], more := false } ↔ _ + rw [compatible_empty_iff_complete_nil] + refine ⟨fun h => ⟨?_, completeKeys_eq_nil_iff_emptyConstraints.mp h⟩, + fun h => completeKeys_eq_nil_iff_emptyConstraints.mpr h.2⟩ + unfold RangeRead.withinLimit + cases r.limit with + | none => trivial + | some n => exact Nat.zero_le _ + +/-- **Observability assembly, necessity in range form.** Like +`minimal_of_observable_DNF` but taking the range's keys as *necessary* directly, which +is the form the proven empty theory supplies. -/ +theorem minimal_of_observable_necessary_DNF {r : RangeRead} {res : Result} + {D : ConstraintDNF} {R : ConflictRange} + (hchar : CharacterizesDNF D r res) (hobs : Observable D R) + (hnec : ∀ k, k ∈ R → Necessary r res k) : + Minimal r res R := + (minimal_iff_sound_and_necessary).mpr ⟨sound_of_observable hchar hobs, hnec⟩ + +/-- **The empty read closes through the observability assembly.** For every realizable +empty read, `emptyValue` is the minimal sound range, derived from the DNF assembly +rather than the dedicated empty-theory proofs: `emptyConstraints` characterizes, the +witness oracle gives soundness and hence observability, and each key of `emptyValue` is +necessary by its flip witness. -/ +theorem minimal_emptyValue_via_observable {r : RangeRead} (hread : Readable r emptyRes) : + Minimal r emptyRes (emptyValue r) := by + have hchar := characterizesDNF_emptyConstraints r + obtain ⟨hsnd, hwit⟩ := emptyInferenceWitness r hread + have hwl : RangeRead.withinLimit r emptyRes.keys := by + unfold RangeRead.withinLimit + cases r.limit with + | none => trivial + | some n => exact Nat.zero_le _ + exact minimal_of_observable_necessary_DNF hchar + (observable_of_sound hchar hwl hsnd) + (fun _ hk => necessary_of_toggle (hwit _ hk)) + +/-! ### Composition: the collector's observability reduces to its parts + +Observability is a congruence for the collector's construction, so the general +soundness obligation can be pushed onto the boundary reads and the middle: + +* `Observable.mono` — enlarging `R` preserves observability; +* `observable_conjDNF` / `observable_conjDNFs` — a conjunction of observable DNFs is + observable (the disjunction is the conjunction of the alternatives' disjunctions); +* `observable_append_of_support` — prepending a fixed system whose supports lie in `R` + preserves observability; +* `observable_emptyConstraints_iff_sound` — a boundary read's DNF is observable exactly + when that read's empty theory is sound. + +Together these give `observable_collectOf_of_boundary`: `collectOf` is observable on `R` +as soon as every required boundary read's empty theory is sound on `R` and the +bracket/core supports lie in `R`. The boundary side is the *proven* empty theory, so the +residual soundness obligation for the general read is only the middle's support +containment. -/ + +/-- Observability is monotone in the range: a range containing an observable one sees +the disjunction too. -/ +theorem Observable.mono {D : ConstraintDNF} {R R' : ConflictRange} + (h : Observable D R) (hsub : Subrange R R') : Observable D R' := + fun db₁ db₂ hagree => h db₁ db₂ (keys_eq_of_subrange_agree hsub hagree) + +/-- A conjunction of observables is observable. -/ +theorem observable_conjDNF {D₁ D₂ : ConstraintDNF} {R : ConflictRange} + (h₁ : Observable D₁ R) (h₂ : Observable D₂ R) : + Observable (conjDNF D₁ D₂) R := by + intro db₁ db₂ hagree + rw [anyHolds_conjDNF, anyHolds_conjDNF, h₁ db₁ db₂ hagree, h₂ db₁ db₂ hagree] + +/-- A finite conjunction of observables is observable. -/ +theorem observable_conjDNFs {Ds : List ConstraintDNF} {R : ConflictRange} + (h : ∀ D ∈ Ds, Observable D R) : Observable (conjDNFs Ds) R := by + induction Ds with + | nil => intro db₁ db₂ _; simp [conjDNFs, AnyHolds, SystemHoldsAt] + | cons D Ds ih => + rw [conjDNFs] + exact observable_conjDNF (h D (by simp)) (ih fun D' hD' => h D' (by simp [hD'])) + +/-- Appending a fixed system to each alternative distributes over `AnyHolds`. -/ +theorem anyHolds_map_append {D : ConstraintDNF} {F : ConstraintSystem} {db : DB} : + AnyHolds (D.map (fun S => F ++ S)) db ↔ AnyHolds [F] db ∧ AnyHolds D db := by + constructor + · rintro ⟨S, hS, hSh⟩ + rw [List.mem_map] at hS + obtain ⟨S', hS', rfl⟩ := hS + exact ⟨⟨F, by simp, fun c hc => hSh c (by simp [hc])⟩, + ⟨S', hS', fun c hc => hSh c (by simp [hc])⟩⟩ + · rintro ⟨⟨F', hF', hF'h⟩, ⟨S', hS', hS'h⟩⟩ + have hFeq : F' = F := List.mem_singleton.mp hF' + rw [hFeq] at hF'h + exact ⟨F ++ S', List.mem_map.mpr ⟨S', hS', rfl⟩, fun c hc => by + rcases List.mem_append.mp hc with hc | hc + · exact hF'h c hc + · exact hS'h c hc⟩ + +/-- Prepending a fixed system whose supports lie in `R` preserves observability. -/ +theorem observable_append_of_support {D : ConstraintDNF} {F : ConstraintSystem} + {R : ConflictRange} (hsub : ∀ c ∈ F, Subrange c.supp R) + (h : Observable D R) : Observable (D.map (fun S => F ++ S)) R := by + have hF : Observable [F] R := + observable_of_support (fun S' hS' => (List.mem_singleton.mp hS').symm ▸ hsub) + intro db₁ db₂ hagree + rw [anyHolds_map_append, anyHolds_map_append, hF db₁ db₂ hagree, h db₁ db₂ hagree] + +/-- A boundary read's collected DNF is observable on `R` exactly when that read's empty +theory is sound on `R`. -/ +theorem observable_emptyConstraints_iff_sound {s : RangeRead} {R : ConflictRange} : + Observable (emptyConstraints s) R ↔ Sound s emptyRes R := by + refine ⟨sound_of_observable (characterizesDNF_emptyConstraints s), fun hs => ?_⟩ + have hwl : RangeRead.withinLimit s emptyRes.keys := by + unfold RangeRead.withinLimit + cases s.limit with + | none => trivial + | some n => exact Nat.zero_le _ + exact observable_of_sound (characterizesDNF_emptyConstraints s) hwl hs + +/-- **The collector's observability reduces to its parts.** If every required boundary +read's empty theory is sound on `R` and the bracket/core supports lie in `R`, then +`collectOf` is observable on `R`. This pushes the general soundness obligation onto the +*proven* empty theory plus support containment of the middle. -/ +theorem observable_collectOf_of_boundary {r : RangeRead} {A : List Key} {more : Bool} + {R : ConflictRange} + (hsnd : ∀ s ∈ boundaryReads r A more, Sound s emptyRes R) + (hsubF : ∀ c ∈ bracketConstraints r A ++ coreConstraints A, Subrange c.supp R) : + Observable (collectOf r A more) R := by + have hb : Observable (conjDNFs ((boundaryReads r A more).map emptyConstraints)) R := + observable_conjDNFs fun D hD => by + rw [List.mem_map] at hD + obtain ⟨s, hs, rfl⟩ := hD + exact (observable_emptyConstraints_iff_sound).mpr (hsnd s hs) + have hmap : collectOf r A more = + (conjDNFs ((boundaryReads r A more).map emptyConstraints)).map + (fun S => (bracketConstraints r A ++ coreConstraints A) ++ S) := rfl + rw [hmap] + exact observable_append_of_support hsubF hb + +/-- Soundness of an empty read is preserved by enlarging the range. -/ +theorem sound_emptyRes_mono {s : RangeRead} {cr cr' : ConflictRange} + (hsub : Subrange cr cr') (h : Sound s emptyRes cr) : Sound s emptyRes cr' := + Sound.mono (fun _ _ hagree => keys_eq_of_subrange_agree hsub hagree) h + +/-- **The collector's observability, from the proven empty theory.** The boundary +soundness is discharged by the *proven* `emptyValue` minimality: with every required +boundary read realizable, `R` containing each boundary `emptyValue`, and `R` containing +the bracket/core supports, `collectOf` is observable on `R`. The residual obligation for +a general read is thus only the middle's support containment and the boundary +readability — no boundary-theory content beyond `emptyInferenceWitness`. -/ +theorem observable_collectOf_of_emptyValue {r : RangeRead} {A : List Key} {more : Bool} + {R : ConflictRange} + (hread : ∀ s ∈ boundaryReads r A more, Readable s emptyRes) + (hsubE : ∀ s ∈ boundaryReads r A more, Subrange (emptyValue s) R) + (hsubF : ∀ c ∈ bracketConstraints r A ++ coreConstraints A, Subrange c.supp R) : + Observable (collectOf r A more) R := by + refine observable_collectOf_of_boundary (fun s hs => ?_) hsubF + obtain ⟨hsnd, _⟩ := emptyInferenceWitness s (hread s hs) + exact sound_emptyRes_mono (hsubE s hs) hsnd + +/-! ### The concrete computable read-off, and its unconditional soundness + +The composition above still takes the range `R` and the two support conditions as +hypotheses. The concrete read-off discharges them by construction: `collectReadOff` is +the hull of the required boundary reads' `emptyValue`s together with the supports of the +bracket/core middle. Its subrange lemmas are immediate, and the boundary reads of a +*realizable* pair are themselves realizable with the empty result (the shared compatible +database makes each boundary read empty), so the readability hypothesis is automatic. +The result, `sound_collectReadOff`, is unconditional in every theory hypothesis: for any +realizable read/result pair with nonempty ascending reported keys, the computed read-off +is sound. -/ + +/-- The hull of a list of conflict ranges. -/ +def hullRanges : List ConflictRange → ConflictRange + | [] => ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩ + | cr :: rest => hull cr (hullRanges rest) + +/-- Every range in the list is a subrange of the hull. -/ +theorem subrange_hullRanges {cr : ConflictRange} {l : List ConflictRange} + (hmem : cr ∈ l) : Subrange cr (hullRanges l) := by + induction l with + | nil => exact absurd hmem (by simp) + | cons cr' rest ih => + rw [List.mem_cons] at hmem + rw [hullRanges] + rcases hmem with rfl | hmem + · exact subrange_hull_left _ _ + · exact Subrange.trans (ih hmem) (subrange_hull_right _ _) + +/-- The supports of a system, as a list of conflict ranges. -/ +def systemSupports (S : ConstraintSystem) : List ConflictRange := S.map Constraint.supp + +/-- **The concrete read-off.** The hull of the required boundary reads' `emptyValue`s +and the supports of the bracket/core middle. This is the computable candidate sound +range the DNF assembly targets. -/ +def collectReadOff (r : RangeRead) (A : List Key) (more : Bool) : ConflictRange := + hullRanges (((boundaryReads r A more).map emptyValue) ++ + systemSupports (bracketConstraints r A ++ coreConstraints A)) + +/-- Each required boundary read's `emptyValue` is a subrange of the read-off. -/ +theorem subrange_emptyValue_collectReadOff {r : RangeRead} {A : List Key} {more : Bool} + {s : RangeRead} (hs : s ∈ boundaryReads r A more) : + Subrange (emptyValue s) (collectReadOff r A more) := by + apply subrange_hullRanges + rw [List.mem_append]; left + exact List.mem_map.mpr ⟨s, hs, rfl⟩ + +/-- Each bracket/core support is a subrange of the read-off. -/ +theorem subrange_supp_collectReadOff {r : RangeRead} {A : List Key} {more : Bool} + {c : Constraint} (hc : c ∈ bracketConstraints r A ++ coreConstraints A) : + Subrange c.supp (collectReadOff r A more) := by + apply subrange_hullRanges + rw [List.mem_append]; right + exact List.mem_map.mpr ⟨c, hc, rfl⟩ + +/-- `ascendingKeys` is invariant under replacing the result by its reported form. -/ +theorem ascendingKeys_reportedList (r : RangeRead) (A : List Key) (more : Bool) : + ascendingKeys r { keys := reportedList r A, more := more } = A := by + unfold ascendingKeys reportedList + by_cases hr : r.reverse <;> simp [hr] + +/-- The required boundary reads of a realizable pair are themselves realizable with the +empty result: the shared compatible database makes each boundary read empty. -/ +theorem readable_boundaryReads {r : RangeRead} {A : List Key} {more : Bool} + (hA : A ≠ []) (hpair : A.Pairwise (· < ·)) + (hread : Readable r { keys := reportedList r A, more := more }) : + ∀ s ∈ boundaryReads r A more, Readable s emptyRes := by + obtain ⟨db, hcomp⟩ := hread + have hlim : RangeRead.withinLimit r (reportedList r A) := hcomp.1 + have hbo := (compatible_iff_bridge_general (db := db) (r := r) (A := A) (more := more) + hA hpair hlim).mp hcomp |>.2 + intro s hs + refine ⟨db, ?_⟩ + rw [show emptyRes = ({ keys := [], more := false } : Result) from rfl, + compatible_empty_iff_complete_nil] + exact boundaryReads_empty_of_bridgeOf hbo s hs + +/-- **General soundness of the concrete read-off, fully proved.** For any realizable +read/result pair with nonempty ascending reported keys, the hull of the required +boundary reads' `emptyValue`s and the bracket/core supports is a sound conflict range. +No boundary-theory hypothesis remains: the boundary half is the proven +`emptyInferenceWitness`, and the middle supports lie in the read-off by construction. -/ +theorem sound_collectReadOff {r : RangeRead} {res : Result} + (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) + (hread : Readable r res) : + Sound r res (collectReadOff r (ascendingKeys r res) res.more) := by + apply sound_of_observable (characterizesDNF_collectOf (r := r) (res := res) hA hpair) + refine observable_collectOf_of_emptyValue (A := ascendingKeys r res) (more := res.more) + ?_ ?_ ?_ + · have h : Readable r + { keys := reportedList r (ascendingKeys r res), more := res.more } := by + rwa [reportedList_ascendingKeys] + exact readable_boundaryReads hA hpair h + · intro s hs + exact subrange_emptyValue_collectReadOff hs + · intro c hc + exact subrange_supp_collectReadOff hc + +/-! ### Minimality transfer and the residual + +Soundness and minimality depend on a range only through its key set, so a range with +the same `toSet` as a known-minimal one is minimal too. Hence the read-off is minimal as +soon as it is a subrange of an already-minimal range: soundness then forces the reverse +containment (`Minimal.subrange_of_sound`), pinning the two key sets equal. This is +`minimal_collectReadOff_of_subrange`, the cleanest route to the read-off's minimality +whenever the algorithm's value is known minimal. The one residual for the general case +is *not* a theory hypothesis: it is that the computed read-off be contained in the +minimal range, i.e. that the hull does not overshoot — the per-key flip witnesses in +hull form. -/ + +/-- Soundness depends on a range only through its key set. -/ +theorem sound_congr_toSet {r : RangeRead} {res : Result} {cr cr' : ConflictRange} + (h : cr.toSet = cr'.toSet) : Sound r res cr ↔ Sound r res cr' := by + have hkeys : ∀ db : DB, cr.keys db = cr'.keys db := by + intro db + apply eq_of_mem_iff_pairwise (ConflictRange.keys_pairwise cr db) + (ConflictRange.keys_pairwise cr' db) + intro k + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff, + ← ConflictRange.mem_toSet, ← ConflictRange.mem_toSet, h] + constructor <;> intro hs db₁ db₂ hagree + · refine hs db₁ db₂ ?_ + unfold AgreeOn at hagree ⊢ + rw [hkeys db₁, hkeys db₂]; exact hagree + · refine hs db₁ db₂ ?_ + unfold AgreeOn at hagree ⊢ + rw [← hkeys db₁, ← hkeys db₂]; exact hagree + +/-- Minimality depends on a range only through its key set. -/ +theorem minimal_congr_toSet {r : RangeRead} {res : Result} {cr cr' : ConflictRange} + (h : cr.toSet = cr'.toSet) : Minimal r res cr ↔ Minimal r res cr' := by + have hsound := sound_congr_toSet (r := r) (res := res) h + have hsubrange : ∀ a, Subrange a cr ↔ Subrange a cr' := by + intro a; rw [subrange_iff_subset, subrange_iff_subset, h] + have hsubrange' : ∀ a, Subrange cr a ↔ Subrange cr' a := by + intro a; rw [subrange_iff_subset, subrange_iff_subset, h] + have hproper : ∀ cr'', ProperSubrange cr'' cr ↔ ProperSubrange cr'' cr' := by + intro cr'' + unfold ProperSubrange + rw [hsubrange cr'', hsubrange' cr''] + constructor + · intro hm + exact ⟨hsound.mp hm.1, fun cr'' hsub => hm.2 cr'' ((hproper cr'').mpr hsub)⟩ + · intro hm + exact ⟨hsound.mpr hm.1, fun cr'' hsub => hm.2 cr'' ((hproper cr'').mp hsub)⟩ + +/-- **Minimality of the read-off from a known-minimal range.** If the read-off is a +subrange of an already-minimal range `m`, then it is minimal: it is sound +(`sound_collectReadOff`), so `Minimal.subrange_of_sound` gives `m ⊆ read-off`, and with +the assumed containment the two have equal key sets. The residual is only the +containment `read-off ⊆ m`, i.e. that the hull does not overshoot. -/ +theorem minimal_collectReadOff_of_subrange {r : RangeRead} {res : Result} + {m : ConflictRange} + (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) + (hread : Readable r res) (hm : Minimal r res m) + (hsub : Subrange (collectReadOff r (ascendingKeys r res) res.more) m) : + Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := by + have hsound := sound_collectReadOff hA hpair hread + have hle : Subrange m (collectReadOff r (ascendingKeys r res) res.more) := + hm.subrange_of_sound hsound + have htoSet : m.toSet = (collectReadOff r (ascendingKeys r res) res.more).toSet := + Set.Subset.antisymm (subrange_iff_subset.mp hle) (subrange_iff_subset.mp hsub) + exact (minimal_congr_toSet htoSet).mp hm + +/-- **Meet closure of minimality.** Intersecting a *sound* range with a known-minimal +one is minimal. The intersection is sound (`Sound.inter`), so `Minimal.subrange_of_sound` +forces the minimal range into it, pinning the two key sets equal. This is the closure +that repairs an over-collecting candidate: even when the read-off is not contained in the +minimal range (as in the truncated branch), its meet with the algorithm's proven-minimal +value is minimal. -/ +theorem minimal_inter_of_sound_of_minimal {r : RangeRead} {res : Result} + {R m : ConflictRange} (hR : Sound r res R) (hm : Minimal r res m) : + Minimal r res (ConflictRange.inter R m) := by + have hs : Sound r res (ConflictRange.inter R m) := hR.inter hm.sound + have hle : Subrange (ConflictRange.inter R m) m := + subrange_of_le (le_max_right _ _) (min_le_right _ _) + have hge : Subrange m (ConflictRange.inter R m) := hm.subrange_of_sound hs + have htoSet : (ConflictRange.inter R m).toSet = m.toSet := + Set.Subset.antisymm (subrange_iff_subset.mp hle) (subrange_iff_subset.mp hge) + exact (minimal_congr_toSet htoSet.symm).mp hm + +/-- **Minimality from necessity.** Soundness of the read-off is unconditional +(`sound_collectReadOff`), so it is minimal as soon as every key it contains is necessary. +This is the form the main development's necessity theorems target: they supply the +per-key flip witnesses, and the read-off's keys are exactly the hull's key set. -/ +theorem minimal_collectReadOff_of_necessary {r : RangeRead} {res : Result} + (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) + (hread : Readable r res) + (hnec : ∀ k ∈ collectReadOff r (ascendingKeys r res) res.more, Necessary r res k) : + Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := + (minimal_iff_sound_and_necessary).mpr + ⟨sound_collectReadOff hA hpair hread, hnec⟩ + +/-- **Minimality of the read-off from necessity of its endpoints, range form.** Since +necessity is convex (`Necessary.of_between`), it suffices that the read-off's two +endpoints bracket necessary keys — the `NecessaryRange` form the main development's +interval-perturbation theorems produce. -/ +theorem minimal_collectReadOff_of_necessaryRange {r : RangeRead} {res : Result} + (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) + (hread : Readable r res) + (hnec : NecessaryRange r res (collectReadOff r (ascendingKeys r res) res.more).begin + (collectReadOff r (ascendingKeys r res) res.more).end_) : + Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := + (minimal_iff_sound_and_necessaryRange).mpr + ⟨sound_collectReadOff hA hpair hread, hnec⟩ + +/-- A nonempty range's begin is at or above the begin of any subrange of it. -/ +theorem le_begin_of_subrange_of_not_isEmpty {cr m : ConflictRange} + (h : Subrange cr m) (hne : ¬ cr.IsEmpty) : m.begin ≤ cr.begin := by + cases hb : cr.begin with + | top => rw [ConflictRange.IsEmpty, hb] at hne; exact absurd le_top hne + | coe b => + have hlt : (cr.begin : WithTop Key) < cr.end_ := lt_of_not_ge hne + have hmem : b ∈ cr := ConflictRange.mem_iff.mpr ⟨by rw [hb], by rw [← hb]; exact hlt⟩ + have hthis := (h b hmem).1 + simpa only [hb] using hthis + +/-- A nonempty range's end is at or below the end of any subrange of it. -/ +theorem end_le_of_subrange_of_not_isEmpty {cr m : ConflictRange} + (h : Subrange cr m) (hne : ¬ cr.IsEmpty) : cr.end_ ≤ m.end_ := by + by_contra hlt + rw [not_le] at hlt + have hme : m.end_ ≠ ⊤ := fun htop => absurd (htop ▸ hlt) (not_lt.mpr le_top) + cases hm : m.end_ with + | top => exact absurd hm hme + | coe e => + cases hb : cr.begin with + | top => rw [ConflictRange.IsEmpty, hb] at hne; exact absurd le_top hne + | coe b => + have hcrlt : (cr.begin : WithTop Key) < cr.end_ := lt_of_not_ge hne + have hbm : b ∈ m := + h b (ConflictRange.mem_iff.mpr ⟨by rw [hb], by rw [← hb]; exact hcrlt⟩) + have hb_e : (b : WithTop Key) < (e : WithTop Key) := by + have := hbm.2; rwa [hm] at this + have hecr : e ∈ cr := by + refine ConflictRange.mem_iff.mpr ⟨?_, ?_⟩ + · rw [hb]; exact le_of_lt hb_e + · rw [hm] at hlt; exact hlt + have hem := (h e hecr).2 + rw [hm] at hem + exact absurd hem (lt_irrefl _) + +/-- **The hull is the least upper bound.** A range containing both factors is a +superrange of their hull. -/ +theorem hull_subrange_of {cr₁ cr₂ m : ConflictRange} + (h₁ : Subrange cr₁ m) (h₂ : Subrange cr₂ m) : Subrange (hull cr₁ cr₂) m := by + unfold hull + by_cases he₁ : cr₁.IsEmpty + · rw [ite_eq_left he₁]; exact h₂ + · rw [ite_eq_right he₁] + by_cases he₂ : cr₂.IsEmpty + · rw [ite_eq_left he₂]; exact h₁ + · rw [ite_eq_right he₂] + exact subrange_of_le + (le_min (le_begin_of_subrange_of_not_isEmpty h₁ he₁) + (le_begin_of_subrange_of_not_isEmpty h₂ he₂)) + (max_le (end_le_of_subrange_of_not_isEmpty h₁ he₁) + (end_le_of_subrange_of_not_isEmpty h₂ he₂)) + +/-- A list-hull is a subrange of `m` when every member is. -/ +theorem subrange_hullRanges_of {l : List ConflictRange} {m : ConflictRange} + (h : ∀ cr ∈ l, Subrange cr m) : Subrange (hullRanges l) m := by + induction l with + | nil => intro k hk; exact absurd hk (by simp [hullRanges, ConflictRange.mem_iff]) + | cons cr rest ih => + rw [hullRanges] + exact hull_subrange_of (h cr (by simp)) + (ih fun cr' hcr' => h cr' (List.mem_cons_of_mem _ hcr')) + +/-- **The residual reduced to its constituents.** The read-off is a subrange of `m` +exactly when every boundary `emptyValue` and every bracket/core support is. So the +single containment obligation of `minimal_collectReadOff_of_subrange` splits into +per-constituent containments, each a case of a read-level analysis. -/ +theorem subrange_collectReadOff_iff {r : RangeRead} {A : List Key} {more : Bool} + {m : ConflictRange} : + Subrange (collectReadOff r A more) m ↔ + (∀ s ∈ boundaryReads r A more, Subrange (emptyValue s) m) ∧ + (∀ c ∈ bracketConstraints r A ++ coreConstraints A, Subrange c.supp m) := by + unfold collectReadOff + constructor + · intro h + refine ⟨fun s hs => ?_, fun c hc => ?_⟩ + · exact Subrange.trans + (subrange_hullRanges (List.mem_append.mpr + (Or.inl (List.mem_map.mpr ⟨s, hs, rfl⟩)))) h + · exact Subrange.trans + (subrange_hullRanges (List.mem_append.mpr + (Or.inr (List.mem_map.mpr ⟨c, hc, rfl⟩)))) h + · rintro ⟨h₁, h₂⟩ + apply subrange_hullRanges_of + intro cr hcr + rw [List.mem_append] at hcr + rcases hcr with hcr | hcr + · rw [List.mem_map] at hcr + obtain ⟨s, hs, rfl⟩ := hcr + exact h₁ s hs + · rw [systemSupports, List.mem_map] at hcr + obtain ⟨c, hc, rfl⟩ := hcr + exact h₂ c hc + +/-! ### The canonical-region containment + +In the canonical region `1 ≤ begin.offset ∧ end_.offset ≤ 1`, a complete nonempty +realizable result has every reported key inside the canonical range `[begin.key, end.key)`: +each reported key is stored (`completeKeys ⊆ db.keys`) and lies between the resolutions +(`resolve begin ≤ k < resolve end_`), the begin offset pushes `begin.key` below every +reported key (`key_le_resolve_of_one_le`), and the end offset at most one keeps every +stored key below `resolve end_` below `end.key` (`stored_lt_selector_key_of_lt_resolve`). +From this the constituents' containments follow directly, so the read-off is contained in +the canonical range and, being sound, shares its key set. -/ + +/-- **Every stored key below `resolve s` is below `s.key`,** when `s.offset ≤ 1`. A stored +key at or above `s.key` sits at index `≥ count (· < s.key)`, while the resolution's index +is that count shifted up by the (nonpositive) offset. -/ +theorem stored_lt_selector_key_of_lt_resolve {db : DB} {s : Selector} (hoff : s.offset ≤ 1) + {k : Key} (hk : k ∈ db.keys) (hlt : (k : WithTop Key) < resolve db s) : + (k : WithTop Key) < s.key := by + by_contra hnot + have hkge : s.key ≤ (k : WithTop Key) := le_of_not_gt hnot + set c : ℕ := db.keys.countP (fun x => decide ((x : Key) < s.key)) with hc + have hanc : s.anchor db = (c : ℤ) + s.offset - 1 := by + unfold Selector.anchor; rw [← hc] + obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hk + have hcj : c ≤ j := by + have hmono : c ≤ db.keys.countP (fun x => decide (x < k)) := by + rw [hc] + apply List.countP_mono_left + intro x _ hx + rw [decide_eq_true_eq] at hx ⊢ + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hx hkge) + have hj' : db.keys.countP (fun x => decide (x < k)) = j := + countP_lt_eq_of_getElem db.sorted hj hjk + omega + rcases lt_or_ge (s.anchor db) 0 with hneg | hpos + · rw [resolve_eq_nil hneg] at hlt + exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k))) + · rcases lt_or_ge (s.anchor db).toNat db.keys.length with hlti | hge + · rw [resolve_eq_getElem hpos hlti] at hlt + have hklt : k < db.keys[(s.anchor db).toNat] := WithTop.coe_lt_coe.mp hlt + have hjlt : j < (s.anchor db).toNat := by + rw [← hjk] at hklt + exact (db.sorted.getElem_lt_getElem_iff).mp hklt + have hle : (s.anchor db).toNat ≤ c := by + rw [Int.toNat_le, hanc]; omega + omega + · have hclen : db.keys.length ≤ c := by + have : (s.anchor db).toNat ≤ c := by rw [Int.toNat_le, hanc]; omega + omega + omega + +/-- Membership of the reported keys in the result, through `ascendingKeys`. -/ +theorem mem_res_keys_of_mem_ascendingKeys {r : RangeRead} {res : Result} {k : Key} + (hk : k ∈ ascendingKeys r res) : k ∈ res.keys := by + rw [← reportedList_ascendingKeys (r := r) (res := res)] + unfold reportedList + by_cases hr : r.reverse + · simp only [hr, ↓reduceIte]; exact List.mem_reverse.mpr hk + · simp only [hr, Bool.false_eq_true, ↓reduceIte]; exact hk + +/-- **Every reported key lies in the canonical range**, in the canonical region with a +realizable result. (Completeness is not needed: a truncated result's keys are still a +prefix of the complete keys.) -/ +theorem reported_keys_bounds {r : RangeRead} {res : Result} + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) + (hread : Readable r res) {k : Key} (hk : k ∈ res.keys) : + r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key := by + obtain ⟨db₀, hcomp⟩ := hread + have hpre := RangeRead.compatible_keys_prefix hcomp + have hck : k ∈ RangeRead.completeKeys db₀ r := hpre.subset hk + obtain ⟨hkmem, hlo, hhi⟩ := RangeRead.mem_completeKeys.mp hck + exact ⟨le_trans (key_le_resolve_of_one_le hbegin) hlo, + stored_lt_selector_key_of_lt_resolve hend hkmem hhi⟩ + +/-- A constraint whose lower and upper ends lie in `[B, E)` has its support inside +`[B, E)`. Both orders are allowed, since `supp` takes `min`/`max`. -/ +theorem subrange_supp_of_four {c : Constraint} {B E : WithTop Key} + (h1 : B ≤ c.lo) (h2 : c.hi ≤ E) (h3 : B ≤ c.hi) (h4 : c.lo ≤ E) : + Subrange c.supp ⟨B, E⟩ := by + rw [Constraint.supp] + exact subrange_of_le (le_min h1 h3) (max_le h4 h2) + +/-- The defaulted last of a nonempty cons is its undefaulted last. -/ +theorem getLastD_cons_default {a : Key} {l : List Key} : + (a :: l).getLastD a = (a :: l).getLastD ([] : Key) := by + rw [List.getLastD_cons, List.getLastD_cons] + +/-- Every bracket/core constraint support is contained in the canonical range, in the +canonical region with a complete realizable result. -/ +theorem supps_subrange_canonical {r : RangeRead} {res : Result} + (_hmore : res.more = false) (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) + (hread : Readable r res) (hne : res.keys ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) {c : Constraint} + (hc : c ∈ bracketConstraints r (ascendingKeys r res) ++ coreConstraints (ascendingKeys r res)) : + Subrange c.supp (canonicalRange r) := by + have hb := fun {k : Key} (hk : k ∈ res.keys) => reported_keys_bounds hbegin hend hread hk + have hmem := fun {k : Key} (hk : k ∈ ascendingKeys r res) => mem_res_keys_of_mem_ascendingKeys hk + have hAne : ascendingKeys r res ≠ [] := by + unfold ascendingKeys; by_cases hr : r.reverse <;> simp [hr, hne] + obtain ⟨k₀, rest, hAeq⟩ := List.exists_cons_of_ne_nil hAne + set kₙ : Key := (k₀ :: rest).getLastD k₀ with hkₙdef + have hk0res : k₀ ∈ res.keys := hmem (by rw [hAeq]; exact List.mem_cons_self ..) + have hkn_mem : kₙ ∈ k₀ :: rest := by + rw [hkₙdef, List.getLastD_cons] + exact List.getLastD_mem_cons + have hknres : kₙ ∈ res.keys := hmem (by rw [hAeq]; exact hkn_mem) + have hb0 := hb hk0res + have hbn := hb hknres + have hk0le : (k₀ : WithTop Key) ≤ (kₙ : WithTop Key) := + WithTop.coe_le_coe.mpr (headD_le_of_pairwise (by rw [← hAeq]; exact hpair) hkn_mem) + have hbe : r.begin.key ≤ r.end_.key := by + obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne + exact le_of_lt (lt_of_le_of_lt (hb hk).1 (hb hk).2) + have hkn' : (kₙ : WithTop Key) < r.end_.key := hbn.2 + have hk0' : (k₀ : WithTop Key) < r.end_.key := hb0.2 + have hbeg0 : r.begin.key ≤ (k₀ : WithTop Key) := hb0.1 + have hbegn : r.begin.key ≤ (kₙ : WithTop Key) := hbn.1 + have e2 : r.begin.key ≤ ((keyAfter kₙ : Key) : WithTop Key) := + le_trans hbegn (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt kₙ))) + rw [hAeq] at hc + rw [List.mem_append] at hc + rcases hc with hc | hc + · -- bracket constraints + simp only [bracketConstraints, List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · exact subrange_supp_of_four hbeg0 hbe le_rfl (le_of_lt hk0') + · exact subrange_supp_of_four hbe (le_of_lt hkn') hbegn le_rfl + · -- core constraints + simp only [coreConstraints, List.mem_append, List.mem_map, List.mem_cons, + List.not_mem_nil, or_false] at hc + rcases hc with hc | hc | hc + · obtain ⟨k, hkA, rfl⟩ := hc + have hkA' : k ∈ (k₀ :: rest) := List.mem_cons.mpr hkA + have hbk := hb (hmem (by rw [hAeq]; exact hkA')) + exact subrange_supp_of_four + (le_trans hbk.1 (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k)))) + (le_of_lt hbk.2) hbk.1 (keyAfter_le_of_lt hbk.2) + · rw [hc]; exact subrange_supp_of_four hbeg0 (keyAfter_le_of_lt hkn') e2 (le_of_lt hk0') + · rw [hc]; exact subrange_supp_of_four e2 (le_of_lt hk0') hbeg0 (keyAfter_le_of_lt hkn') + +/-- `emptyValue` of the lower boundary read lies in the canonical range. -/ +theorem emptyValue_boundaryReadLo_subrange {r : RangeRead} {k₀ : Key} + (hbegin : 1 ≤ r.begin.offset) (hbeg0 : r.begin.key ≤ (k₀ : WithTop Key)) + (hlt : (k₀ : WithTop Key) < r.end_.key) : + Subrange (emptyValue (boundaryReadLo r k₀)) (canonicalRange r) := by + have hbeginL : ¬ (boundaryReadLo r k₀).begin.offset ≤ 0 := by + simp only [boundaryReadLo]; omega + have hendL : (boundaryReadLo r k₀).end_.offset ≤ 1 := le_of_eq rfl + by_cases hAE : AlwaysEmpty (boundaryReadLo r k₀) + · have hv : emptyValue (boundaryReadLo r k₀) = emptyRange (boundaryReadLo r k₀) := by + unfold emptyValue; rw [ite_eq_left hAE] + rw [hv] + exact subrange_of_le le_rfl (le_trans hbeg0 (le_of_lt hlt)) + · have hv : emptyValue (boundaryReadLo r k₀) = canonicalRange (boundaryReadLo r k₀) := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right hbeginL, ite_eq_left hendL] + rw [hv] + exact subrange_of_le le_rfl (le_of_lt hlt) + +/-- `emptyValue` of the upper boundary read lies in the canonical range. -/ +theorem emptyValue_boundaryReadHi_subrange {r : RangeRead} {kₙ : Key} + (hend : r.end_.offset ≤ 1) (hbegn : r.begin.key ≤ (kₙ : WithTop Key)) + (hlt : ((keyAfter kₙ : Key) : WithTop Key) ≤ r.end_.key) : + Subrange (emptyValue (boundaryReadHi r kₙ)) (canonicalRange r) := by + have hbeginH : ¬ (boundaryReadHi r kₙ).begin.offset ≤ 0 := by + simp only [boundaryReadHi, selAt_offset]; norm_num + have hendH : (boundaryReadHi r kₙ).end_.offset ≤ 1 := hend + have hbeg : r.begin.key ≤ ((keyAfter kₙ : Key) : WithTop Key) := + le_trans hbegn (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt kₙ))) + by_cases hAE : AlwaysEmpty (boundaryReadHi r kₙ) + · have hv : emptyValue (boundaryReadHi r kₙ) = emptyRange (boundaryReadHi r kₙ) := by + unfold emptyValue; rw [ite_eq_left hAE] + rw [hv] + exact subrange_of_le hbeg hlt + · have hv : emptyValue (boundaryReadHi r kₙ) = canonicalRange (boundaryReadHi r kₙ) := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right hbeginH, ite_eq_left hendH] + rw [hv] + exact subrange_of_le hbeg le_rfl + +/-- A realizable nonempty result's ascending keys are strictly increasing. -/ +theorem pairwise_ascendingKeys {r : RangeRead} {res : Result} + (hread : Readable r res) (_hne : res.keys ≠ []) : + (ascendingKeys r res).Pairwise (· < ·) := by + obtain ⟨db, hcomp⟩ := hread + unfold ascendingKeys + by_cases hr : r.reverse + · simp only [hr, ↓reduceIte] + exact ((RangeRead.compatible_pairwise hcomp).2 hr).reverse + · simp only [hr, Bool.false_eq_true, ↓reduceIte] + exact (RangeRead.compatible_pairwise hcomp).1 (by simpa using hr) + +/-- The ascending keys of a nonempty result are nonempty. -/ +theorem ascendingKeys_ne_nil {r : RangeRead} {res : Result} (hne : res.keys ≠ []) : + ascendingKeys r res ≠ [] := by + unfold ascendingKeys; by_cases hr : r.reverse <;> simp [hr, hne] + +/-- **The read-off is contained in the canonical range** in the canonical region, for a +complete nonempty realizable result. -/ +theorem subrange_collectReadOff_canonical {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) : + Subrange (collectReadOff r (ascendingKeys r res) res.more) (canonicalRange r) := by + have hb := fun {k : Key} (hk : k ∈ res.keys) => reported_keys_bounds hbegin hend hread hk + have hmem := fun {k : Key} (hk : k ∈ ascendingKeys r res) => mem_res_keys_of_mem_ascendingKeys hk + have hAne : ascendingKeys r res ≠ [] := ascendingKeys_ne_nil hne + have hpair : (ascendingKeys r res).Pairwise (· < ·) := pairwise_ascendingKeys hread hne + rw [subrange_collectReadOff_iff] + refine ⟨?_, ?_⟩ + · intro s hs + have hhead_b := hb (hmem (headD_mem hAne)) + have hlast_b := hb (hmem (getLastD_mem hAne)) + rw [hmore] at hs + unfold boundaryReads at hs + simp only [Bool.false_eq_true, ↓reduceIte, List.mem_cons, List.not_mem_nil, or_false] at hs + rcases hs with rfl | rfl + · exact emptyValue_boundaryReadLo_subrange hbegin hhead_b.1 hhead_b.2 + · exact emptyValue_boundaryReadHi_subrange hend hlast_b.1 (keyAfter_le_of_lt hlast_b.2) + · intro c hc + exact supps_subrange_canonical hmore hbegin hend hread hne hpair hc + +/-- **The read-off computes the canonical range's key set** in the canonical region, for +a complete nonempty realizable result: it is sound and contained in the canonical range, +so the canonical range's minimality pins the two to the same key set. -/ +theorem collectReadOff_toSet_eq_canonical {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) : + (collectReadOff r (ascendingKeys r res) res.more).toSet = (canonicalRange r).toSet := by + have hsub := subrange_collectReadOff_canonical hread hmore hne hbegin hend + have hmin := minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend + have hle : Subrange (canonicalRange r) + (collectReadOff r (ascendingKeys r res) res.more) := + hmin.subrange_of_sound + (sound_collectReadOff (ascendingKeys_ne_nil hne) (pairwise_ascendingKeys hread hne) hread) + exact Set.Subset.antisymm (subrange_iff_subset.mp hsub) (subrange_iff_subset.mp hle) + +/-- **Minimality of the concrete read-off in the canonical region.** For a complete +nonempty realizable read/result pair with `1 ≤ begin.offset` and `end_.offset ≤ 1`, the +computable hull `collectReadOff` is a minimal sound conflict range. This extends +end-to-end minimality beyond the counterexample `ceRead`. -/ +theorem minimal_collectReadOff_canonical {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) : + Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := + minimal_collectReadOff_of_subrange (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread + (minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend) + (subrange_collectReadOff_canonical hread hmore hne hbegin hend) + +/-! ### The overshoot-region containment + +In the overshoot region `1 ≤ begin.offset ∧ 2 ≤ end.offset` with every reported key +below `end.key`, a complete nonempty realizable result has the unbounded top +`[begin.key, ⊤)` as its minimal range (`minimal_topRange_of_end_offset_ge_two_general` +and its reversal twin), so the containment residual is that the read-off lie inside +the top range. The begin side is forced by the reported-key bounds, and the upper +boundary read's contribution stays above `begin.key` because its begin selector +`⟨keyAfter kₙ, 1⟩` is keyed strictly above the reported last key. This is the region +the canonical block does not reach: the other half of `completeValue`'s non-hull +answers. -/ + +/-- `emptyValue` is the empty range on a trivially empty read. -/ +theorem emptyValue_eq_emptyRange_of_alwaysEmpty {s : RangeRead} (h : AlwaysEmpty s) : + emptyValue s = emptyRange s := by + unfold emptyValue; rw [ite_eq_left h] + +/-- `emptyValue` is the top range away from `AlwaysEmpty` when the begin offset is +positive, the end offset is at least two, and the selector keys are ordered. -/ +theorem emptyValue_eq_topRange_of {s : RangeRead} + (hAE : ¬ AlwaysEmpty s) (hb : ¬ s.begin.offset ≤ 0) (he : ¬ s.end_.offset ≤ 1) + (hk : s.begin.key ≤ s.end_.key) : emptyValue s = topRange s := by + unfold emptyValue + rw [ite_eq_right hAE, ite_eq_right hb, ite_eq_right he, ite_eq_left hk] + +/-- The lower end of the upper boundary read's `emptyValue` stays at or above +`begin.key`: its begin selector is keyed at `keyAfter kₙ`, which lies at or above the +begin key. -/ +theorem begin_key_le_emptyValue_boundaryReadHi_begin {r : RangeRead} {kₙ : Key} + (_hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hbk : r.begin.key ≤ (kₙ : WithTop Key)) (hke : (kₙ : WithTop Key) < r.end_.key) : + r.begin.key ≤ (emptyValue (boundaryReadHi r kₙ)).begin := by + classical + have hka : r.begin.key ≤ ((keyAfter kₙ : Key) : WithTop Key) := + le_trans hbk (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt kₙ))) + by_cases hAE : AlwaysEmpty (boundaryReadHi r kₙ) + · rw [emptyValue_eq_emptyRange_of_alwaysEmpty hAE] + simp only [emptyRange, boundaryReadHi, selAt_key] + exact hka + · rw [emptyValue_eq_topRange_of hAE ?_ ?_ ?_] + · simp only [topRange, boundaryReadHi, selAt_key] + exact hka + · simp only [boundaryReadHi, selAt_offset]; norm_num + · simp only [boundaryReadHi]; omega + · simp only [boundaryReadHi, selAt_key]; exact keyAfter_le_of_lt hke + +/-- A constraint whose lower and upper ends both lie at or above `B` has its support +inside `[B, ⊤)`. -/ +theorem subrange_supp_topRange_of_bounds {c : Constraint} {B : WithTop Key} + (h1 : B ≤ c.lo) (h2 : B ≤ c.hi) : Subrange c.supp ⟨B, ⊤⟩ := by + rw [Constraint.supp] + exact subrange_of_le (le_min h1 h2) le_top + +/-- Every bracket/core support is contained in the top range `[begin.key, ⊤)`, in +the overshoot region with a complete realizable result. -/ +theorem supps_subrange_topRange {r : RangeRead} {k₀ : Key} {rest : List Key} + (hpair : (k₀ :: rest).Pairwise (· < ·)) + (hbnd : ∀ {k : Key}, k ∈ k₀ :: rest → + r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key) + {c : Constraint} + (hc : c ∈ bracketConstraints r (k₀ :: rest) ++ coreConstraints (k₀ :: rest)) : + Subrange c.supp (topRange r) := by + have hk0 : k₀ ∈ k₀ :: rest := List.mem_cons_self .. + have hkn : rest.getLastD k₀ ∈ k₀ :: rest := List.getLastD_mem_cons + have hb0 := hbnd hk0 + have hbn := hbnd hkn + have hk0le : k₀ ≤ rest.getLastD k₀ := by + simpa only [List.headD_cons] using headD_le_of_pairwise hpair hkn + rw [List.mem_append] at hc + rcases hc with hc | hc + · simp only [bracketConstraints, List.getLastD_cons, List.mem_cons, + List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · exact subrange_supp_topRange_of_bounds hb0.1 (le_refl _) + · exact subrange_supp_topRange_of_bounds (le_of_lt (lt_of_le_of_lt hbn.1 hbn.2)) hbn.1 + · simp only [coreConstraints, List.getLastD_cons, List.mem_append, List.mem_map, + List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with ⟨k, hkA, rfl⟩ | rfl | rfl + · have hbk := hbnd (List.mem_cons.mpr hkA) + exact subrange_supp_topRange_of_bounds + (le_trans hbk.1 (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k)))) hbk.1 + · exact subrange_supp_topRange_of_bounds hb0.1 + (le_trans hb0.1 (WithTop.coe_le_coe.mpr + (le_of_lt (lt_of_le_of_lt hk0le (keyAfter_self_lt (rest.getLastD k₀)))))) + · exact subrange_supp_topRange_of_bounds + (le_trans hb0.1 (WithTop.coe_le_coe.mpr + (le_of_lt (lt_of_le_of_lt hk0le (keyAfter_self_lt (rest.getLastD k₀)))))) + hb0.1 + +/-- **The read-off is contained in the top range** in the overshoot region, for a +complete nonempty realizable result. -/ +theorem subrange_collectReadOff_topRange {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) : + Subrange (collectReadOff r (ascendingKeys r res) res.more) (topRange r) := by + have hAne : ascendingKeys r res ≠ [] := ascendingKeys_ne_nil hne + obtain ⟨k₀, rest, hAeq⟩ := List.exists_cons_of_ne_nil hAne + have hpair : (k₀ :: rest).Pairwise (· < ·) := by + rw [← hAeq]; exact pairwise_ascendingKeys hread hne + have hbnd : ∀ {k : Key}, k ∈ k₀ :: rest → + r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key := by + intro k hk + have hkres : k ∈ res.keys := + mem_res_keys_of_mem_ascendingKeys (r := r) (res := res) (by rw [hAeq]; exact hk) + obtain ⟨db₀, hcomp₀⟩ := hread + have hck := (RangeRead.compatible_keys_prefix hcomp₀).subset hkres + exact ⟨le_trans (key_le_resolve_of_one_le hbegin) + (RangeRead.mem_completeKeys.mp hck).2.1, hhi k hkres⟩ + rw [hAeq] + rw [subrange_collectReadOff_iff] + refine ⟨?_, ?_⟩ + · intro s hs + rw [hmore] at hs + simp only [boundaryReads, Bool.false_eq_true, ↓reduceIte, List.mem_cons, + List.not_mem_nil, or_false] at hs + rcases hs with rfl | rfl + · rw [List.headD_cons] + exact Subrange.trans + (emptyValue_boundaryReadLo_subrange hbegin (hbnd (List.mem_cons_self ..)).1 + (hbnd (List.mem_cons_self ..)).2) (subrange_of_le le_rfl le_top) + · have hlast : (k₀ :: rest).getLastD ([] : Key) = rest.getLastD k₀ := + List.getLastD_cons + rw [hlast] + have hkn : rest.getLastD k₀ ∈ k₀ :: rest := List.getLastD_mem_cons + exact subrange_of_le + (begin_key_le_emptyValue_boundaryReadHi_begin hbegin hend + (hbnd hkn).1 (hbnd hkn).2) le_top + · intro c hc + exact supps_subrange_topRange hpair hbnd hc + +/-- **Minimality of the concrete read-off in the overshoot region.** For a complete +nonempty realizable pair with `1 ≤ begin.offset`, `2 ≤ end.offset`, and every reported +key below `end.key`, the computable hull `collectReadOff` is a minimal sound conflict +range. The algorithm's value is the top range, and the read-off is contained in it. -/ +theorem minimal_collectReadOff_topRange {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) : + Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := by + have htop : Minimal r res (topRange r) := by + cases hrev : r.reverse with + | false => + exact minimal_topRange_of_end_offset_ge_two_general hread hmore hrev hne hbegin hend hhi + | true => + exact minimal_topRange_of_end_offset_ge_two_rev_general hread hmore hrev hne hbegin hend hhi + exact minimal_collectReadOff_of_subrange (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread htop + (subrange_collectReadOff_topRange hread hmore hne hbegin hend hhi) + +/-- **The read-off computes the top range's key set** in the overshoot region, for a +complete nonempty realizable result: it is sound and contained in the top range, so the +top range's minimality (either traversal) pins the two to the same key set. This is the +overshoot counterpart of `collectReadOff_toSet_eq_canonical`. -/ +theorem collectReadOff_toSet_eq_topRange {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) : + (collectReadOff r (ascendingKeys r res) res.more).toSet = (topRange r).toSet := by + have hsub := subrange_collectReadOff_topRange hread hmore hne hbegin hend hhi + have hle : Subrange (topRange r) (collectReadOff r (ascendingKeys r res) res.more) := by + cases hrev : r.reverse with + | false => + exact (minimal_topRange_of_end_offset_ge_two_general hread hmore hrev hne hbegin hend hhi + ).subrange_of_sound (sound_collectReadOff (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread) + | true => + exact (minimal_topRange_of_end_offset_ge_two_rev_general hread hmore hrev hne hbegin hend hhi + ).subrange_of_sound (sound_collectReadOff (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread) + exact Set.Subset.antisymm (subrange_iff_subset.mp hsub) (subrange_iff_subset.mp hle) + +/-- **Complete-branch read-off minimality, `begin.offset ≥ 1`.** The read-off is minimal +on both non-hull regions of `completeValue`: the canonical region `end.offset ≤ 1` and +the overshoot region (`2 ≤ end.offset` with every reported key below `end.key`). This +packages the two region theorems behind one disjunctive hypothesis; the crossed selector +order remains the only uncertified complete region. -/ +theorem minimal_collectReadOff_complete_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) + (hcase : r.end_.offset ≤ 1 ∨ + (2 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)) : + Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := by + rcases hcase with h1 | ⟨h2, hhi⟩ + · exact minimal_collectReadOff_canonical hread hmore hne hbegin h1 + · exact minimal_collectReadOff_topRange hread hmore hne hbegin h2 hhi + +/-! ### The truncated branch through the meet + +The read-off over-collects on a truncated result (`Counterexample.ct_collectReadOff_not_minimal`), +but its meet with the algorithm's proven-minimal truncated value is still minimal +(`minimal_inter_of_sound_of_minimal`): the meet is sound, so it sits inside the minimal +range, and being a subrange of it (the minimal range is itself a subrange of the +truncated interval) it shares the minimal key set. This gives a certified range on the +truncated branch without the read-off's overshoot. -/ + +/-- The forward truncated value's lower end is the begin key for any positive begin +offset: the lower boundary read is not trivially empty there, so its empty value is +the canonical range on the begin selector. -/ +theorem lowBound_begin_eq_of_one_le {r : RangeRead} {res : Result} + (hbegin : 1 ≤ r.begin.offset) : + (lowBound r res).begin = r.begin.key := by + unfold lowBound emptyValue + set s := boundaryReadLo r (leastReported r res) with hs + have hb : ¬ s.begin.offset ≤ 0 := by + rw [show s.begin.offset = r.begin.offset from rfl]; omega + have he : s.end_.offset ≤ 1 := by rw [show s.end_.offset = 1 from rfl] + by_cases hAE : AlwaysEmpty s + · rw [ite_eq_left hAE]; simp only [emptyRange, hs, boundaryReadLo] + · rw [ite_eq_right hAE, ite_eq_right hb, ite_eq_left he] + simp only [canonicalRange, hs, boundaryReadLo] + +/-- **Forward truncated value equals the certified truncated range**, without a +boundary-emptiness side condition: at a positive begin offset the lower end is always +the begin key. -/ +theorem forwardTruncatedRange_eq_truncatedRange_general {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) : + forwardTruncatedRange r res = truncatedRange r (res.keys.getLastD ([] : Key)) := by + have hgreat : greatestReported r res = res.keys.getLastD ([] : Key) := by + simp [greatestReported, hrev] + rw [forwardTruncatedRange, hgreat, truncatedRange, lowBound_begin_eq_of_one_le hbegin] + +/-- **Forward truncated, offset one, unconditionally.** The forward truncated value is +minimal at offset one with no boundary side condition. -/ +theorem minimal_forwardTruncatedRange_of_offset_one_free {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + Minimal r res (forwardTruncatedRange r res) := by + rw [forwardTruncatedRange_eq_truncatedRange_general hrev (by omega)] + exact minimal_truncatedRange_of_offset_one hread hmore hrev hbegin hend + +/-- **Forward truncated, overshoot end, unconditionally.** With `end.offset ≥ 2` and every +reported key below `end.key`, the forward truncated value is minimal. -/ +theorem minimal_forwardTruncatedRange_of_end_offset_ge_two_free {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (forwardTruncatedRange r res) := by + rw [forwardTruncatedRange_eq_truncatedRange_general hrev (by omega)] + exact minimal_truncatedRange_of_end_offset_ge_two hread hmore hrev hbegin hend hhi + +/-- **The meet of the read-off with any known-minimal range is minimal.** This is the +general repair used to certify over-collecting read-offs without proving containment. -/ +theorem minimal_inter_collectReadOff_of_minimal {r : RangeRead} {res : Result} + {m : ConflictRange} (hA : ascendingKeys r res ≠ []) + (hpair : (ascendingKeys r res).Pairwise (· < ·)) + (hread : Readable r res) (hm : Minimal r res m) : + Minimal r res + (ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more) m) := + minimal_inter_of_sound_of_minimal (sound_collectReadOff hA hpair hread) hm + +/-- **Truncated forward, offset one, the meet.** At offset one with a truncated +non-reversed result the meet of the read-off with the forward truncated value is minimal, +though the read-off alone need not be. -/ +theorem minimal_inter_collectReadOff_truncated_fwd {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) (hne : res.keys ≠ []) : + Minimal r res + (ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more) + (forwardTruncatedRange r res)) := + minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread + (minimal_forwardTruncatedRange_of_offset_one_free hread hmore hrev hbegin hend) + +/-- **Truncated forward, overshoot end, the meet.** With `end.offset ≥ 2` and every +reported key below `end.key`, the meet of the read-off with the forward truncated value +is minimal. -/ +theorem minimal_inter_collectReadOff_truncated_fwd_overshoot {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) (hne : res.keys ≠ []) : + Minimal r res + (ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more) + (forwardTruncatedRange r res)) := + minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread + (minimal_forwardTruncatedRange_of_end_offset_ge_two_free hread hmore hrev hbegin hend hhi) + +/-- **Truncated reversed, the meet.** At offset one with a truncated reversed result +and a nonempty upper boundary read, the meet of the read-off with the reverse truncated +value is minimal. -/ +theorem minimal_inter_collectReadOff_truncated_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) + (hempty : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res))) + (hne : res.keys ≠ []) : + Minimal r res + (ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more) + (reverseTruncatedRange r res)) := + minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread + (minimal_reverseTruncatedRange_of_offset_one hread hmore hrev hbegin hend hempty) + +/-- **Truncated reversed, overshoot end, the meet.** With `end.offset ≥ 2` and every +reported key below `end.key`, the meet of the read-off with the top-from-last value is +minimal. -/ +theorem minimal_inter_collectReadOff_truncated_rev_overshoot {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) (hne : res.keys ≠ []) : + Minimal r res + (ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more) + (topOfLast (res.keys.getLastD ([] : Key)))) := + minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne) + (pairwise_ascendingKeys hread hne) hread + (minimal_topOfLast_of_end_offset_ge_two_rev hread hmore hrev hbegin hend hhi) + +/-! ## Sorted endpoints telescope + +If all the keys mentioned by a family of finite-range capacities are sorted, the +capacities of adjacent intervals add to the whole (`intervalCard_add`). So a +system of interval capacities over a common, sorted set of endpoints collapses to +the capacities of the consecutive gaps — the normalization that makes the caps +first-class. -/ + +/-- The sum of the capacities of the consecutive gaps of a sorted boundary list. -/ +def capSum : List (WithTop Key) → ℕ∞ + | [] => 0 + | [_] => 0 + | a :: b :: rest => intervalCard a b + capSum (b :: rest) + +/-- A member of a `≤`-sorted list is at most its last element. -/ +theorem le_getLastD_of_pairwise_le {M : List (WithTop Key)} (h : M.Pairwise (· ≤ ·)) + {x : WithTop Key} (hx : x ∈ M) (d : WithTop Key) : x ≤ M.getLastD d := by + have hne : M ≠ [] := by rintro rfl; exact absurd hx (by simp) + have hlast : x ≤ M.getLast hne := h.rel_getLast hx + have hD : M.getLastD d = M.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne, Option.getD_some] + rw [hD]; exact hlast + +/-- **Sorted capacities telescope.** For a `≤`-sorted list of boundary keys, the +capacity of the whole span is the sum of the adjacent capacities: +`intervalCard (head) (last) = Σ intervalCard mᵢ mᵢ₊₁`. Sorting the keys of any +finite-range constraint family therefore turns it into a chain of adjacent caps. -/ +theorem intervalCard_headD_getLastD_eq_capSum {M : List (WithTop Key)} + (h : M.Pairwise (· ≤ ·)) (d : WithTop Key) : + intervalCard (M.headD d) (M.getLastD d) = capSum M := by + induction M generalizing d with + | nil => rw [capSum]; exact intervalCard_eq_zero_of_le le_rfl + | cons a t ih => + cases t with + | nil => rw [capSum]; exact intervalCard_eq_zero_of_le le_rfl + | cons b rest => + have hab : a ≤ b := (List.pairwise_cons.mp h).1 b (by simp) + have ht : (b :: rest).Pairwise (· ≤ ·) := (List.pairwise_cons.mp h).2 + have hble : b ≤ (b :: rest).getLastD a := + le_getLastD_of_pairwise_le ht (by simp) a + change intervalCard a ((b :: rest).getLastD a) = capSum (a :: b :: rest) + rw [intervalCard_add hab hble] + have hih := ih ht a + simp only [List.headD_cons] at hih + rw [hih, capSum] + +/-! ## A computable demonstration + +A concrete transaction over a small key space. Each read is run against the +shared witness `db`, its constraints are collected, and the candidate minimal +ranges are read off by hulling supports. -/ + +/-- The small keys `[0]`, `[1]`, `[2]`. -/ +def demoKey (n : Byte) : Key := [n] + +/-- A shared witness database with keys `[0]`, `[1]`, `[2]`. -/ +def demoDB : DB where + keys := [demoKey 0, demoKey 1, demoKey 2] + sorted := by decide + +/-- A complete read from `[0]` to `⊤` at offset one: returns every stored key. -/ +def demoRead : RangeRead where + begin := ⟨(demoKey 0 : WithTop Key), 1⟩ + end_ := ⟨⊤, 1⟩ + limit := none + reverse := false + +/-- A second read, from just past `[0]` to `⊤`: returns `[1]`, `[2]`. -/ +def demoRead2 : RangeRead where + begin := ⟨(keyAfter (demoKey 0) : WithTop Key), 1⟩ + end_ := ⟨⊤, 1⟩ + limit := none + reverse := false + +/-- A human-readable endpoint: `⊥` for the empty key, `⊤` for the sentinel, and +the byte values of a genuine key otherwise. -/ +def showEnd : WithTop Key → String + | none => "⊤" + | some [] => "⊥" + | some k => "[" ++ String.intercalate "," (k.map (fun b => toString b.val)) ++ "]" + +/-- A human-readable conflict range. -/ +def showRange (cr : ConflictRange) : String × String := + (showEnd cr.begin, showEnd cr.end_) + +/-- A human-readable constraint. -/ +def showCon (c : Constraint) : String := + s!"ρ{showEnd c.hi} − ρ{showEnd c.lo} ≤ {c.bound}" + +/-- The collected system of the first read against the shared witness. -/ +def demoSys : ConstraintSystem := + constraintsOf demoRead (RangeRead.completeKeys demoDB demoRead) + +/-- The collected system of the second read against the same witness. -/ +def demoSys2 : ConstraintSystem := + constraintsOf demoRead2 (RangeRead.completeKeys demoDB demoRead2) + +-- The two reads' reported keys. +#eval RangeRead.completeKeys demoDB demoRead +#eval RangeRead.completeKeys demoDB demoRead2 +-- The constraints collected for the first read. +#eval demoSys.map showCon +-- The read-off range of the first read (hull of supports). +#eval (hullSupports demoSys).map showRange +-- The read-off range of the second read. +#eval (hullSupports demoSys2).map showRange +-- The whole transaction: one range covering all reads' supports. +#eval (transactionRanges [demoSys, demoSys2]).map showRange +-- Finite-capacity constraint and the sorted telescoping identity. +-- `[[0], [1])` is infinite (infinitely many keys between `[0]` and `[1]`), so it +-- contributes no finite cap; the successor chain `[0] < [0,0] < [0,0,0]` has +-- adjacent gaps of one key each. +#eval intervalCard ((demoKey 0 : Key) : WithTop Key) ((demoKey 1 : Key) : WithTop Key) +#eval capSum ([(demoKey 0 : Key), keyAfter (demoKey 0), keyAfter (keyAfter (demoKey 0))] : + List (WithTop Key)) +#eval intervalCard ((demoKey 0 : Key) : WithTop Key) + (((keyAfter (keyAfter (demoKey 0))) : Key) : WithTop Key) +-- The short-gap branch selector: `[0]` to `⊤` is infinite (long gap, `false`); +-- `[0]` to `[1]` is infinite too; a one-key successor gap with `g = 2` is short. +#eval shortGap 2 ((demoKey 0 : Key) : WithTop Key) (⊤ : WithTop Key) +#eval shortGap 2 ((demoKey 0 : Key) : WithTop Key) + (((keyAfter (demoKey 0)) : Key) : WithTop Key) +-- An empty read: the two alternatives read off `resolve`, for the read with +-- `begin = ⟨[0], 2⟩`, `end = ⟨[1], 1⟩`: `ρ([1]) − ρ([0]) ≤ 1` or `ρ(⊤) − ρ([0]) ≤ 1`. +def demoEmptyRead : RangeRead where + begin := ⟨(demoKey 0 : WithTop Key), 2⟩ + end_ := ⟨(demoKey 1 : WithTop Key), 1⟩ + limit := none + reverse := false +#eval (emptyConstraints demoEmptyRead).map (fun S => S.map showCon) + +-- The general collector. For the complete first read the collected DNF has two +-- alternatives (one per empty boundary read disjunct), each carrying the bracket +-- and middle systems; for the truncated branch it is the single bracket+middle +-- alternative. +#eval (collectOf demoRead (RangeRead.completeKeys demoDB demoRead) false).map + (fun S => S.map showCon) +#eval (collectOf demoRead (RangeRead.completeKeys demoDB demoRead) true).map + (fun S => S.map showCon) +-- The ascending reported keys the collector is stated on, recovered from a result. +#eval ascendingKeys demoRead { keys := RangeRead.completeKeys demoDB demoRead, more := false } + +end FormalConflictRange diff --git a/FormalConflictRange/RangeRead.lean b/FormalConflictRange/RangeRead.lean new file mode 100644 index 0000000..966c596 --- /dev/null +++ b/FormalConflictRange/RangeRead.lean @@ -0,0 +1,429 @@ +/- +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.Selector + +/-! +# Range reads and results + +A `RangeRead` requests the keys between a `begin` and `end` selector (ordered +lexicographically), optionally capped by a positive `limit` and optionally +reversed. A `Result` reports the keys produced along with a `more` flag +indicating whether the range was truncated. + +Resolved endpoints live in `WithTop Key`, where `⊤` is past-the-end. Because a +selector's key is itself a `WithTop Key`, the unbounded ends of the key space +are reachable by fixed selectors: `readAll` below is the read whose complete +result is every stored key, in either traversal order. +-/ + +namespace FormalConflictRange + +/-- A range read names `begin` and `end` selectors, an optional positive +`limit`, and whether to traverse in `reverse`. -/ +structure RangeRead where + begin : Selector + end_ : Selector + limit : Option ℕ+ + reverse : Bool + +/-- The outcome of a range read: the `keys` produced and whether `more` remain +beyond what was returned. -/ +structure Result where + keys : List Key + more : Bool + +namespace RangeRead + +/-- The keys of `ks` lying in the half-open interval `[lo, hi)`, ascending. +The bounds are `WithTop Key`, so `⊤` naturally means "past the end". -/ +def inRange (ks : List Key) (lo hi : WithTop Key) : List Key := + ks.filter (fun k => decide (lo ≤ (k : WithTop Key) ∧ (k : WithTop Key) < hi)) + +/-- The complete keys of a range read: every stored key in the half-open +interval `[resolve begin, resolve end_)`, reversed when `reverse` is set. The +`limit` is ignored, as the complete result is untruncated. -/ +def completeKeys (db : DB) (r : RangeRead) : List Key := + let ks := inRange db.keys (resolve db r.begin) (resolve db r.end_) + if r.reverse then ks.reverse else ks + +/-- The complete result of a range read: the untruncated keys, with `more` set +to `false`. -/ +def complete (db : DB) (r : RangeRead) : Result := + { keys := completeKeys db r, more := false } + +/-- The complete keys depend on a database only through its keys. -/ +theorem completeKeys_congr {db₁ db₂ : DB} (hk : db₁.keys = db₂.keys) (r : RangeRead) : + completeKeys db₁ r = completeKeys db₂ r := by + unfold completeKeys inRange + rw [resolve_congr hk, resolve_congr hk, hk] + +/-- The keys of a complete result are exactly the stored keys in the resolved +half-open interval. -/ +theorem mem_completeKeys {db : DB} {r : RangeRead} {k : Key} : + k ∈ completeKeys db r ↔ + k ∈ db.keys ∧ resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_ := by + unfold completeKeys inRange + by_cases h : r.reverse <;> simp [h, List.mem_filter] + +/-- The complete keys are a permutation of the ascending in-range keys. -/ +theorem completeKeys_perm_inRange (db : DB) (r : RangeRead) : + (completeKeys db r).Perm + (inRange db.keys (resolve db r.begin) (resolve db r.end_)) := by + unfold completeKeys + split + · exact List.reverse_perm _ + · exact List.Perm.refl _ + +/-- The complete keys are duplicate-free. -/ +theorem completeKeys_nodup (db : DB) (r : RangeRead) : (completeKeys db r).Nodup := by + unfold completeKeys + split + · rw [List.nodup_reverse] + exact List.Nodup.filter _ db.sorted.pairwise.nodup + · exact List.Nodup.filter _ db.sorted.pairwise.nodup + +/-- The ascending in-range keys are strictly increasing. -/ +theorem inRange_pairwise (db : DB) (lo hi : WithTop Key) : + List.Pairwise (· < ·) (inRange db.keys lo hi) := by + unfold inRange + exact db.sorted.pairwise.sublist + (List.filter_sublist + (p := fun (k : Key) => decide (lo ≤ (k : WithTop Key) ∧ (k : WithTop Key) < hi))) + +/-- The complete keys are strictly increasing when not reversed and strictly +decreasing when reversed. -/ +theorem completeKeys_pairwise {db : DB} {r : RangeRead} : + if r.reverse then List.Pairwise (· > ·) (completeKeys db r) + else List.Pairwise (· < ·) (completeKeys db r) := by + unfold completeKeys + split + · exact List.Pairwise.reverse (inRange_pairwise db _ _) + · exact inRange_pairwise db _ _ + +/-- The length bound imposed by an optional positive limit. -/ +def withinLimit (r : RangeRead) (ks : List Key) : Prop := + match r.limit with + | none => True + | some n => ks.length ≤ (n : ℕ) + +/-- A result is compatible with a range read when it respects the limit and is a +valid (possibly truncated) response: a nonempty prefix of the complete keys +reporting `more`, or exactly the complete result reporting no more. -/ +def Compatible (db : DB) (r : RangeRead) (res : Result) : Prop := + withinLimit r res.keys ∧ + ((res.more = true ∧ res.keys <+: completeKeys db r ∧ res.keys ≠ []) + ∨ (res.more = false ∧ res.keys = completeKeys db r)) + +/-- A compatible result respects the limit. -/ +theorem compatible_withinLimit {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) : withinLimit r res.keys := h.1 + +/-- A compatible result reporting `more` is nonempty: a truncated response must +report at least one key. -/ +theorem compatible_ne_nil_of_more {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) (hmore : res.more = true) : res.keys ≠ [] := by + rcases h.2 with ⟨_, _, hne⟩ | ⟨hm, _⟩ + · exact hne + · simp [hmore] at hm + +/-- The keys of a compatible result are a prefix of the complete keys. -/ +theorem compatible_keys_prefix {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) : res.keys <+: completeKeys db r := by + rcases h.2 with ⟨_, hpre, _⟩ | ⟨_, heq⟩ + · exact hpre + · exact heq ▸ List.prefix_refl _ + +/-- The keys of a compatible result are stored in the database. -/ +theorem compatible_keys_subset {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) : res.keys ⊆ db.keys := + fun _ hk => (mem_completeKeys.mp ((compatible_keys_prefix h).subset hk)).1 + +/-- If a predicate picks out the first element that is at least `a` in a strictly +increasing list containing `a`, it finds `a`. -/ +theorem find?_eq_some_of_pairwise {l : List Key} (hs : l.Pairwise (· < ·)) {a : Key} + (ha : a ∈ l) {p : Key → Bool} (hpa : p a) + (hpbefore : ∀ k ∈ l, k < a → p k = false) : l.find? p = some a := by + induction l with + | nil => exact absurd ha (by simp) + | cons k t ih => + rw [List.mem_cons] at ha + rcases ha with rfl | ha + · simp [hpa] + · have hkt : k < a := (List.pairwise_cons.mp hs).1 a ha + have hpk : p k = false := hpbefore k (by simp) hkt + rw [List.find?_cons_of_neg (by simp [hpk])] + exact ih (List.pairwise_cons.mp hs).2 ha fun x hx hxa => hpbefore x (by simp [hx]) hxa + +/-- In a non-reversed read, if `resolve begin` is the stored key `a` and `a` is in +the complete result, then `a` is the head of the complete keys. -/ +theorem completeKeys_head_eq {db : DB} {r : RangeRead} (hrev : r.reverse = false) + (hne : completeKeys db r ≠ []) {a : Key} + (ha : resolve db r.begin = (↑a : WithTop Key)) (hain : a ∈ completeKeys db r) : + (completeKeys db r).head hne = a := by + have hain' : a ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [completeKeys] at hain + simpa only [hrev, Bool.false_eq_true, ite_false] using hain + have hhead? : (completeKeys db r).head? = some a := by + rw [completeKeys, ite_eq_right (by simp [hrev])] + unfold inRange + rw [List.head?_filter] + refine find?_eq_some_of_pairwise db.sorted.pairwise ?_ ?_ ?_ + · exact (List.mem_filter.mp hain').1 + · rw [decide_eq_true_eq] + exact ⟨by rw [ha], (decide_eq_true_eq.mp (List.mem_filter.mp hain').2).2⟩ + · intro k _ hka + rw [decide_eq_false_iff_not] + intro h + rw [ha] at h + exact absurd h.1 (not_le.mpr (by exact_mod_cast hka)) + exact (List.head_eq_iff_head?_eq_some hne).mpr hhead? + +/-- In a non-reversed read with nonnegative begin anchor and nonempty complete +result, the head of the complete keys is a stored key that `resolve begin` equals. -/ +theorem completeKeys_head_resolve {db : DB} {r : RangeRead} (hrev : r.reverse = false) + (hne : completeKeys db r ≠ []) (hpos : 0 ≤ r.begin.anchor db) : + ∃ a, a ∈ db.keys ∧ resolve db r.begin = (↑a : WithTop Key) ∧ + (completeKeys db r).head hne = a := by + obtain ⟨k, hk⟩ : ∃ k, k ∈ completeKeys db r := by + cases h : completeKeys db r with + | nil => exact absurd h hne + | cons k t => exact ⟨k, by simp⟩ + have hk' := mem_completeKeys.mp hk + have hlt : r.begin.anchor db < (db.keys.length : Int) := by + by_contra h + have htop : resolve db r.begin = (⊤ : WithTop Key) := resolve_eq_top hpos (by omega) + have hle : (⊤ : WithTop Key) ≤ (↑k : WithTop Key) := by rw [← htop]; exact hk'.2.1 + exact absurd hle (not_le.mpr (WithTop.coe_lt_top k)) + refine ⟨db.keys[(r.begin.anchor db).toNat], List.getElem_mem _, ?_, ?_⟩ + · exact resolve_eq_getElem hpos (by omega) + · have hlt2 : (↑db.keys[(r.begin.anchor db).toNat] : WithTop Key) < resolve db r.end_ := by + rw [resolve_eq_getElem hpos (by omega)] at hk' + exact lt_of_le_of_lt hk'.2.1 hk'.2.2 + have hain : db.keys[(r.begin.anchor db).toNat] ∈ completeKeys db r := + mem_completeKeys.mpr + ⟨List.getElem_mem _, by rw [resolve_eq_getElem hpos (by omega)], hlt2⟩ + exact completeKeys_head_eq hrev hne (resolve_eq_getElem hpos (by omega)) hain + +/-- If a predicate picks out the first element below `a` in a strictly +decreasing list containing `a`, it finds `a`. -/ +theorem find?_eq_some_of_pairwise_gt {l : List Key} (hs : l.Pairwise (· > ·)) {a : Key} + (ha : a ∈ l) {p : Key → Bool} (hpa : p a) + (hpabove : ∀ k ∈ l, a < k → p k = false) : l.find? p = some a := by + induction l with + | nil => exact absurd ha (by simp) + | cons k t ih => + rw [List.mem_cons] at ha + rcases ha with rfl | ha + · simp [hpa] + · have hkt : a < k := (List.pairwise_cons.mp hs).1 a ha + have hpk : p k = false := hpabove k (by simp) hkt + rw [List.find?_cons_of_neg (by simp [hpk])] + exact ih (List.pairwise_cons.mp hs).2 ha fun x hx hxa => hpabove x (by simp [hx]) hxa + +/-- With nonempty complete keys the end anchor is at least one. -/ +theorem one_le_anchor_of_completeKeys_ne_nil {db : DB} {r : RangeRead} + (hne : completeKeys db r ≠ []) : 1 ≤ r.end_.anchor db := by + obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne + obtain ⟨hkm, hkB, hklt⟩ := mem_completeKeys.mp hk + have h0 : 0 ≤ r.end_.anchor db := by + by_contra h + rw [resolve_eq_nil (db := db) (s := r.end_) (by omega)] at hklt + exact absurd (WithTop.coe_lt_coe.mp hklt) (List.not_lt_nil k) + by_contra h1 + have hz : r.end_.anchor db = 0 := by omega + have hlt : (r.end_.anchor db).toNat < db.keys.length := by + rw [hz, Int.toNat_zero] + exact List.length_pos_of_mem hkm + rw [resolve_eq_getElem h0 hlt] at hklt + rw [WithTop.coe_lt_coe] at hklt + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hkm + subst hik + have hle : db.keys[(r.end_.anchor db).toNat]'hlt ≤ db.keys[i]'hi := + (db.sorted.getElem_le_getElem_iff).mpr (by rw [hz, Int.toNat_zero]; omega) + exact absurd hklt (not_lt.mpr hle) + +/-- With `end_.offset ≤ 1` the end anchor never exceeds the database length. -/ +theorem end_anchor_toNat_le {db : DB} {r : RangeRead} (hoff : r.end_.offset ≤ 1) : + (r.end_.anchor db).toNat ≤ db.keys.length := by + rw [Int.toNat_le] + have h3 := List.countP_le_length + (p := fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) (l := db.keys) + unfold Selector.anchor + omega + +/-- In a reversed read with `end_.offset ≤ 1`, the head of the complete keys is +the stored key just below the end anchor. -/ +theorem completeKeys_head?_rev {db : DB} {r : RangeRead} (hrev : r.reverse = true) + (hoff : r.end_.offset ≤ 1) (hne : completeKeys db r ≠ []) : + (completeKeys db r).head? = db.keys[(r.end_.anchor db - 1).toNat]? := by + have hA : 1 ≤ r.end_.anchor db := one_le_anchor_of_completeKeys_ne_nil hne + set m := (r.end_.anchor db).toNat with hm_def + have hmlt : m - 1 < db.keys.length := by + have h := end_anchor_toNat_le (db := db) (r := r) hoff + omega + have hkey_lt : ∀ (i : Nat) (hi : i < db.keys.length), + ((↑(db.keys[i]'hi) : WithTop Key) < resolve db r.end_) ↔ i < m := by + intro i hi + rcases lt_or_ge m db.keys.length with hlt | hge + · have hlt' : (r.end_.anchor db).toNat < db.keys.length := by rw [← hm_def]; exact hlt + rw [resolve_eq_getElem (db := db) (s := r.end_) (by omega) hlt', WithTop.coe_lt_coe] + rw [hm_def] + exact db.sorted.getElem_lt_getElem_iff + · have hm : m = db.keys.length := by omega + rw [resolve_eq_top (db := db) (s := r.end_) (by omega) (by omega)] + exact ⟨fun _ => by omega, fun _ => WithTop.coe_lt_top _⟩ + have hB : resolve db r.begin ≤ (↑(db.keys[m-1]'hmlt) : WithTop Key) := by + obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne + obtain ⟨hkm, hkB, hklt⟩ := mem_completeKeys.mp hk + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hkm + have hkB' : resolve db r.begin ≤ (↑(db.keys[i]'hi) : WithTop Key) := hik.symm ▸ hkB + have hklt' : (↑(db.keys[i]'hi) : WithTop Key) < resolve db r.end_ := hik.symm ▸ hklt + have him : i < m := (hkey_lt i hi).mp hklt' + have hile : i ≤ m - 1 := by omega + calc resolve db r.begin ≤ (↑(db.keys[i]'hi) : WithTop Key) := hkB' + _ ≤ (↑(db.keys[m-1]'hmlt) : WithTop Key) := by + rw [WithTop.coe_le_coe] + exact (db.sorted.getElem_le_getElem_iff).mpr hile + have hE : (↑(db.keys[m-1]'hmlt) : WithTop Key) < resolve db r.end_ := + (hkey_lt (m - 1) hmlt).mpr (by omega) + rw [completeKeys, ite_eq_left (by simp [hrev])] + unfold inRange + rw [← List.filter_reverse, List.head?_filter, toNat_sub_one hA, ← hm_def, + List.getElem?_eq_getElem hmlt] + refine find?_eq_some_of_pairwise_gt (List.Pairwise.reverse (R := (· > ·)) db.sorted.pairwise) + (List.mem_reverse.mpr (List.getElem_mem hmlt)) ?_ ?_ + · rw [decide_eq_true_eq] + exact ⟨hB, hE⟩ + · intro k hk hklt + rw [decide_eq_false_iff_not] + intro hcon + rw [List.mem_reverse] at hk + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hk + subst hik + have hgt : m - 1 < i := db.sorted.getElem_lt_getElem_iff.mp hklt + have hnlt : ¬ i < m := by omega + exact hnlt ((hkey_lt i hi).mp hcon.2) + +/-- A compatible result is ordered: ascending when not reversed, descending when +reversed. -/ +theorem compatible_pairwise {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) : + (r.reverse = false → List.Pairwise (· < ·) res.keys) ∧ + (r.reverse = true → List.Pairwise (· > ·) res.keys) := by + have hpre := (compatible_keys_prefix h).sublist + constructor + · intro hf + have hp : List.Pairwise (· < ·) (completeKeys db r) := by + simpa [hf] using completeKeys_pairwise (db := db) (r := r) + exact hp.sublist hpre + · intro ht + have hp : List.Pairwise (· > ·) (completeKeys db r) := by + simpa [ht] using completeKeys_pairwise (db := db) (r := r) + exact hp.sublist hpre + +/-- A compatible result is strictly ascending when not reversed. -/ +theorem compatible_sortedLT {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) (hf : r.reverse = false) : res.keys.SortedLT := + List.sortedLT_iff_pairwise.mpr ((compatible_pairwise h).1 hf) + +/-- A compatible result is strictly descending when reversed. -/ +theorem compatible_sortedGT {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) (ht : r.reverse = true) : res.keys.SortedGT := + List.sortedGT_iff_pairwise.mpr ((compatible_pairwise h).2 ht) + +/-- The keys of a compatible result are the complete keys or a proper prefix. -/ +theorem compatible_keys_eq_or_prefix {db : DB} {r : RangeRead} {res : Result} + (h : Compatible db r res) : + res.keys = completeKeys db r ∨ res.keys <+: completeKeys db r := by + rcases h.2 with ⟨_, hpre, _⟩ | ⟨_, heq⟩ + · exact Or.inr hpre + · exact Or.inl heq + +/-- The complete result is compatible exactly when it fits within the limit. -/ +theorem complete_compatible_iff {db : DB} {r : RangeRead} : + Compatible db r (complete db r) ↔ withinLimit r (completeKeys db r) := by + unfold Compatible complete + simp + +/-- A result longer than the limit is never compatible. -/ +theorem not_compatible_of_limit_lt {db : DB} {r : RangeRead} {res : Result} + {n : ℕ+} (h : r.limit = some n) (hlen : (n : ℕ) < res.keys.length) : + ¬ Compatible db r res := by + rintro ⟨hlim, _⟩ + simp [withinLimit, h] at hlim + omega + +/-- Compatibility depends on the database only through its complete keys. -/ +theorem compatible_congr {db₁ db₂ : DB} {r : RangeRead} {res : Result} + (h : completeKeys db₁ r = completeKeys db₂ r) : + (Compatible db₁ r res ↔ Compatible db₂ r res) := by + unfold Compatible + rw [h] + +/-- The read of every stored key: it begins at the empty key with offset zero, +so `resolve` yields the start of the key space, and ends at `⊤` with offset +one, so `resolve` yields `⊤` past every stored key. Neither endpoint could be +named by a fixed plain key — the empty key is the least key, and no key lies +above all stored keys of an arbitrary database — so this "read all keys" read +is expressible precisely because selector keys are `WithTop Key`. -/ +def readAll : RangeRead where + begin := { key := (([] : Key) : WithTop Key), offset := 0 } + end_ := { key := (⊤ : WithTop Key), offset := 1 } + limit := none + reverse := false + +/-- The begin selector of `readAll` anchors at its offset less one: the empty +key counts no stored keys below it, and the offset is zero. -/ +theorem anchor_readAll_begin (db : DB) : + readAll.begin.anchor db = readAll.begin.offset - 1 := + anchor_of_nil rfl + +/-- The begin selector of `readAll` resolves to the empty key. -/ +theorem resolve_readAll_begin (db : DB) : + resolve db readAll.begin = (↑([] : Key) : WithTop Key) := by + have ho : readAll.begin.offset = 0 := rfl + refine resolve_eq_nil ?_ + rw [anchor_readAll_begin, ho] + omega + +/-- The end selector of `readAll` anchors at the database's key count: `⊤` +counts every stored key below it, and the offset is one. -/ +theorem anchor_readAll_end (db : DB) : + readAll.end_.anchor db = (db.keys.length : Int) := by + rw [anchor_of_top (show readAll.end_.key = (⊤ : WithTop Key) from rfl), + show readAll.end_.offset = 1 from rfl] + omega + +/-- The end selector of `readAll` resolves to `⊤`, past every stored key. -/ +theorem resolve_readAll_end (db : DB) : resolve db readAll.end_ = (⊤ : WithTop Key) := by + refine resolve_eq_top ?_ ?_ + · rw [anchor_readAll_end]; omega + · rw [anchor_readAll_end, Int.toNat_natCast] + +/-- Every key lies in the half-open interval from the empty key to `⊤`. -/ +theorem inRange_unbounded (ks : List Key) : + inRange ks ((↑([] : Key) : Key) : WithTop Key) (⊤ : WithTop Key) = ks := by + unfold inRange + rw [List.filter_eq_self] + intro k _ + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), WithTop.coe_lt_top k⟩ + +/-- The complete keys of `readAll` are all of the database's keys, ascending. -/ +theorem completeKeys_readAll (db : DB) : completeKeys db readAll = db.keys := by + simp [completeKeys, resolve_readAll_begin, resolve_readAll_end, inRange_unbounded, + show readAll.reverse = false from rfl] + +/-- The complete keys of the reversed `readAll` are all of the database's keys, +descending. -/ +theorem completeKeys_readAll_reverse (db : DB) : + completeKeys db {readAll with reverse := true} = db.keys.reverse := by + simp [completeKeys, resolve_readAll_begin, resolve_readAll_end, inRange_unbounded] + +end RangeRead + +end FormalConflictRange diff --git a/FormalConflictRange/Realization.lean b/FormalConflictRange/Realization.lean new file mode 100644 index 0000000..7af142f --- /dev/null +++ b/FormalConflictRange/Realization.lean @@ -0,0 +1,410 @@ +/- +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.Empty + +/-! +# Realizing prescribed interval counts by a database + +This file is the database side of the count-to-`FlipWitness` bridge. A finite +set of endpoints partitions the key space into consecutive half-open intervals. +If every interval is asked to contain at most as many keys as its cardinality +allows, the interval-insertion construction of `Basic.lean` realizes all the +prescribed counts in one database. + +The main theorem, `exists_db_realizing_counts`, starts at the empty key `⊥` and +builds the database left to right. The last interval is unbounded above, so no +finiteness hypothesis is needed for it. This is exactly the shape consumed by a +characterizing DNF: its endpoints always include `⊥`, and the count at `⊥` is +zero. + +The proof is constructive (modulo the classical choice inherent in the existing +`DB.insertInterval` construction) and uses only the successor chain inside each +interval, so it does not enumerate databases. +-/ + +namespace FormalConflictRange + +namespace DB + +/-- Append a successor chain above `a` to a database whose keys all lie strictly +below `a`. This realizes the final unbounded interval `[a, ⊤)`. -/ +def appendAbove (db : DB) (a : Key) (n : ℕ) + (h : ∀ k ∈ db.keys, k < a) : DB where + keys := db.keys ++ List.map (chain a) (List.range n) + sorted := by + rw [List.sortedLT_append] + refine ⟨db.sorted, sortedLT_map_chain a n, ?_⟩ + intro x hx y hy + rw [List.mem_map] at hy + obtain ⟨i, -, rfl⟩ := hy + exact lt_of_lt_of_le (h x hx) (chain_le a i) + +@[simp] theorem appendAbove_keys (db : DB) (a : Key) (n : ℕ) + (h : ∀ k ∈ db.keys, k < a) : + (db.appendAbove a n h).keys = db.keys ++ List.map (chain a) (List.range n) := rfl + +/-- Appending an unbounded chain does not change any count at or below its +base. -/ +theorem cnt_appendAbove_of_le (db : DB) (a : Key) (n : ℕ) + (h : ∀ k ∈ db.keys, k < a) {w : WithTop Key} (hw : w ≤ (a : WithTop Key)) : + cnt (db.appendAbove a n h) w = cnt db w := by + unfold cnt + rw [appendAbove_keys, List.countP_append] + have hzero : (List.map (chain a) (List.range n)).countP + (fun (k : Key) => decide ((k : WithTop Key) < w)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [List.mem_map] at hx + obtain ⟨i, -, rfl⟩ := hx + simpa using not_lt.mpr (le_trans hw (WithTop.coe_le_coe.mpr (chain_le a i))) + rw [hzero, Nat.add_zero] + +/-- Appending an unbounded chain leaves every key below `a` untouched. -/ +theorem cnt_appendAbove_self (db : DB) (a : Key) (n : ℕ) + (h : ∀ k ∈ db.keys, k < a) : + cnt (db.appendAbove a n h) (a : WithTop Key) = cnt db (a : WithTop Key) := + cnt_appendAbove_of_le db a n h le_rfl + +/-- Appending an unbounded chain of `n` keys makes the total count exactly the +old length plus `n`. -/ +theorem cnt_top_appendAbove (db : DB) (a : Key) (n : ℕ) + (h : ∀ k ∈ db.keys, k < a) : + cnt (db.appendAbove a n h) ⊤ = db.keys.length + n := by + unfold cnt + rw [appendAbove_keys, List.countP_append, countP_lt_top, countP_lt_top, + List.length_map, List.length_range] + +/-- When all keys are already below the interval's lower endpoint, interval +insertion keeps them all and appends exactly the successor chain. -/ +theorem insertInterval_keys_eq_of_below (db : DB) {a b : Key} (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) (hbelow : ∀ k ∈ db.keys, k < a) : + (db.insertInterval a b hab n hn).keys = db.keys ++ List.map (chain a) (List.range n) := by + rw [DB.insertInterval_keys] + have hP : db.keys.filter (fun x => decide (x < a)) = db.keys := + List.filter_eq_self.mpr fun x hx => by simpa using hbelow x hx + have hQ : db.keys.filter (fun x => decide (b ≤ x)) = [] := + List.filter_eq_nil_iff.mpr fun x hx hcon => by + have hxb : b ≤ x := of_decide_eq_true hcon + exact absurd (hbelow x hx) (not_lt.mpr (le_trans hab hxb)) + rw [hP, hQ, List.append_nil] + +/-- The length after interval insertion is the old length plus the chain +length, when all old keys are below the interval. -/ +theorem insertInterval_length_of_below (db : DB) {a b : Key} (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) (hbelow : ∀ k ∈ db.keys, k < a) : + (db.insertInterval a b hab n hn).keys.length = db.keys.length + n := by + rw [insertInterval_keys_eq_of_below db hab n hn hbelow, List.length_append, + List.length_map, List.length_range] + +/-- After interval insertion, every key is below the upper endpoint. -/ +theorem insertInterval_keys_lt_of_below (db : DB) {a b : Key} (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) (hbelow : ∀ k ∈ db.keys, k < a) : + ∀ k ∈ (db.insertInterval a b hab n hn).keys, k < b := by + intro k hk + rw [insertInterval_keys_eq_of_below db hab n hn hbelow, List.mem_append] at hk + rcases hk with hk | hk + · exact lt_of_lt_of_le (hbelow k hk) hab + · rw [List.mem_map] at hk + obtain ⟨i, hi, rfl⟩ := hk + exact hn i (List.mem_range.mp hi) + +/-- Interval insertion does not change a count at or below the lower +endpoint. -/ +theorem cnt_insertInterval_of_le (db : DB) {a b : Key} (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) {w : Key} (hw : w ≤ a) : + cnt (db.insertInterval a b hab n hn) (w : WithTop Key) = cnt db (w : WithTop Key) := by + rw [cnt_coe, cnt_coe] + exact countP_lt_insertInterval_of_lt hab hw n hn + +/-- Interval insertion raises the count at the upper endpoint by exactly the +number of old keys below the lower endpoint plus the chain length. -/ +theorem cnt_insertInterval_self (db : DB) {a b : Key} (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) (hbelow : ∀ k ∈ db.keys, k < a) : + cnt (db.insertInterval a b hab n hn) (b : WithTop Key) = db.keys.length + n := by + rw [cnt_coe] + rw [countP_lt_insertInterval_self hab hn] + have hP : db.keys.filter (fun x => decide (x < a)) = db.keys := + List.filter_eq_self.mpr fun x hx => by simpa using hbelow x hx + rw [hP] + +end DB + +/-! ## Count specifications for consecutive intervals -/ + +/-- The prescribed counts fit the capacities of the consecutive intervals +starting at `start`. The endpoints need only be nondecreasing: an empty +interval is allowed and then its capacity forces the corresponding count to be +zero. -/ +def CountsFit : Key → List Key → List ℕ → Prop + | _, [], [] => True + | start, b :: bs, d :: ds => + start ≤ b ∧ (d : ℕ∞) ≤ intervalCard (start : WithTop Key) (b : WithTop Key) ∧ + CountsFit b bs ds + | _, _, _ => False + +/-- The counts at a list of endpoints, relative to a base count below the first +endpoint. -/ +def CountsAt (db : DB) (base : ℕ) : List Key → List ℕ → Prop + | [], [] => True + | b :: bs, d :: ds => + cnt db (b : WithTop Key) = base + d ∧ CountsAt db (base + d) bs ds + | _, _ => False + +/-- The endpoints form a nondecreasing chain starting at `start`. -/ +def BoundsLe : Key → List Key → Prop + | _, [] => True + | start, b :: bs => start ≤ b ∧ BoundsLe b bs + +/-- The interval counts actually realized by a database: each entry is the +number of keys in the corresponding half-open interval. -/ +def intervalCounts (db : DB) : Key → List Key → List ℕ + | _, [] => [] + | start, b :: bs => + (cnt db (b : WithTop Key) - cnt db (start : WithTop Key)) :: intervalCounts db b bs + +/-- The last endpoint of the chain, or the starting point for the empty +chain. -/ +def lastBound (start : Key) : List Key → WithTop Key + | [] => (start : WithTop Key) + | b :: bs => lastBound b bs + +/-- The realized interval counts telescope from the starting count to the count +at the last endpoint. -/ +theorem intervalCounts_sum (db : DB) (start : Key) (bounds : List Key) + (hle : BoundsLe start bounds) : + cnt db (lastBound start bounds) = + cnt db (start : WithTop Key) + (intervalCounts db start bounds).sum := by + induction bounds generalizing start with + | nil => simp [lastBound, intervalCounts] + | cons b bs ih => + rcases hle with ⟨hstartb, hleTail⟩ + have hmono : cnt db (start : WithTop Key) ≤ cnt db (b : WithTop Key) := + cnt_le_cnt_of_le db (WithTop.coe_le_coe.mpr hstartb) + have htail := ih b hleTail + simp only [intervalCounts, lastBound, List.sum_cons] + omega + +/-- The realized interval counts fit their capacities. -/ +theorem intervalCounts_fit (db : DB) (start : Key) (bounds : List Key) + (hle : BoundsLe start bounds) : CountsFit start bounds (intervalCounts db start bounds) := by + induction bounds generalizing start with + | nil => simp [intervalCounts, CountsFit] + | cons b bs ih => + rcases hle with ⟨hstartb, hleTail⟩ + refine ⟨hstartb, ?_, ?_⟩ + · exact cnt_sub_le_intervalCard db (WithTop.coe_le_coe.mpr hstartb) + · exact ih b hleTail + +/-- The realized interval counts above `⊥` sum to at most the whole database +length. -/ +theorem intervalCounts_sum_le_top (db : DB) (bounds : List Key) + (hle : BoundsLe ([] : Key) bounds) : + (intervalCounts db ([] : Key) bounds).sum ≤ cnt db ⊤ := by + have h := intervalCounts_sum db ([] : Key) bounds hle + have hlast : cnt db (lastBound ([] : Key) bounds) ≤ cnt db ⊤ := + cnt_le_cnt_of_le db le_top + rw [cnt_nil] at h + omega + +/-- If the base count is the database's count at `start`, the realized interval +counts give the database's counts at every later endpoint. -/ +theorem countsAt_intervalCounts (db : DB) (start : Key) (bounds : List Key) + (hle : BoundsLe start bounds) (base : ℕ) (hbase : base = cnt db (start : WithTop Key)) : + CountsAt db base bounds (intervalCounts db start bounds) := by + induction bounds generalizing start base with + | nil => simp [intervalCounts, CountsAt] + | cons b bs ih => + rcases hle with ⟨hstartb, hleTail⟩ + have hmono : cnt db (start : WithTop Key) ≤ cnt db (b : WithTop Key) := + cnt_le_cnt_of_le db (WithTop.coe_le_coe.mpr hstartb) + refine ⟨?_, ?_⟩ + · rw [hbase] + omega + · have hbase' : base + (cnt db (b : WithTop Key) - cnt db (start : WithTop Key)) = + cnt db (b : WithTop Key) := by + rw [hbase] + omega + rw [hbase'] + exact ih b hleTail (cnt db (b : WithTop Key)) rfl + +/-- Look up the cumulative count at a key in a list of endpoints. If the key is +not present, the sum of all counts is returned; the lemmas below only use the +lookup at present keys. -/ +def countAtWith (base : ℕ) : List Key → List ℕ → Key → ℕ + | [], _, _ => base + | _ :: _, [], _ => base + | b :: bs, d :: ds, k => + if k = b then base + d else countAtWith (base + d) bs ds k + +/-- The cumulative lookup returns the database count at every endpoint in the +list. -/ +theorem countAtWith_eq_of_CountsAt {db : DB} {base : ℕ} {bounds : List Key} + {counts : List ℕ} (h : CountsAt db base bounds counts) {k : Key} (hk : k ∈ bounds) : + countAtWith base bounds counts k = cnt db (k : WithTop Key) := by + induction bounds generalizing base counts with + | nil => simp at hk + | cons b bs ih => + cases counts with + | nil => simp [CountsAt] at h + | cons d ds => + rcases h with ⟨hb, htail⟩ + by_cases hkb : k = b + · subst k + simp [countAtWith, hb] + · have hkbs : k ∈ bs := by + rcases List.mem_cons.mp hk with rfl | hkbs + · exact absurd rfl hkb + · exact hkbs + simp only [countAtWith] + rw [ite_eq_right hkb] + exact ih htail hkbs + +/-- Evaluate a count environment on any endpoint. The empty key has count +zero; a genuine endpoint uses the interval lookup; and the top endpoint counts +the whole database. -/ +def evalCount (bounds : List Key) (counts : List ℕ) (topCount : ℕ) : + WithTop Key → ℤ + | some k => if k = ([] : Key) then 0 else (countAtWith 0 bounds counts k : ℤ) + | none => (counts.sum + topCount : ℤ) + +/-- The count environment agrees with the empty key. -/ +theorem evalCount_bot {db : DB} {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} : + evalCount bounds counts topCount (([] : Key) : WithTop Key) = + cnt db (([] : Key) : WithTop Key) := by + simp [evalCount, cnt_nil] + +/-- The count environment agrees with the top endpoint. -/ +theorem evalCount_top {db : DB} {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} (htop : cnt db ⊤ = counts.sum + topCount) : + evalCount bounds counts topCount ⊤ = cnt db ⊤ := by + simp [evalCount, htop] + +/-- The count environment agrees with every genuine endpoint in the list. -/ +theorem evalCount_coe_of_mem {db : DB} {bounds : List Key} {counts : List ℕ} + {topCount : ℕ} (h : CountsAt db 0 bounds counts) {k : Key} (hk : k ∈ bounds) + (hkbot : k ≠ ([] : Key)) : + evalCount bounds counts topCount (k : WithTop Key) = cnt db (k : WithTop Key) := by + have hlook := countAtWith_eq_of_CountsAt h hk + simp [evalCount, hkbot, hlook] + + +/-- A count list that is realized at a database's endpoints fits the interval +capacities. This is the converse of `exists_db_realizing_counts`. -/ +theorem countsFit_of_CountsAt {db : DB} {start : Key} {base : ℕ} + {bounds : List Key} {counts : List ℕ} (h : CountsAt db base bounds counts) + (hle : BoundsLe start bounds) (hbase : base = cnt db (start : WithTop Key)) : + CountsFit start bounds counts := by + induction bounds generalizing start base counts with + | nil => + cases counts with + | nil => trivial + | cons d ds => simp [CountsAt] at h + | cons b bs ih => + cases counts with + | nil => simp [CountsAt] at h + | cons d ds => + rcases hle with ⟨hstartb, hleTail⟩ + rcases h with ⟨hb, htail⟩ + have hmono : cnt db (start : WithTop Key) ≤ cnt db (b : WithTop Key) := + cnt_le_cnt_of_le db (WithTop.coe_le_coe.mpr hstartb) + refine ⟨hstartb, ?_, ?_⟩ + · have hd : d = cnt db (b : WithTop Key) - cnt db (start : WithTop Key) := by + rw [hbase] at hb + omega + rw [hd] + exact cnt_sub_le_intervalCard db (WithTop.coe_le_coe.mpr hstartb) + · have hbase' : base + d = cnt db (b : WithTop Key) := by omega + rw [hbase'] at htail + exact ih htail hleTail rfl + +/-- **Database realization of prescribed consecutive interval counts.** Starting +from any database whose keys all lie below `start`, one can realize any list of +counts that fits the capacities of the consecutive intervals, and then add an +arbitrary number of keys in the final unbounded interval `[last, ⊤)`. The +counts below `start` are left untouched. + +This is the constructive bridge from count arithmetic to databases: it uses one +successor chain per interval, never enumerating keys. -/ +theorem exists_db_extend_with_top (db : DB) (start : Key) + (hbelow : ∀ k ∈ db.keys, k < start) (bounds : List Key) (counts : List ℕ) + (topCount : ℕ) (hfits : CountsFit start bounds counts) : + ∃ db' : DB, + CountsAt db' db.keys.length bounds counts ∧ + cnt db' ⊤ = db.keys.length + counts.sum + topCount ∧ + (∀ w : WithTop Key, w ≤ (start : WithTop Key) → cnt db' w = cnt db w) := by + induction bounds generalizing db start counts with + | nil => + cases counts with + | nil => + refine ⟨db.appendAbove start topCount hbelow, trivial, ?_, ?_⟩ + · rw [DB.cnt_top_appendAbove] + simp + · intro w hw + exact DB.cnt_appendAbove_of_le db start topCount hbelow hw + | cons d ds => simp [CountsFit] at hfits + | cons b bs ih => + cases counts with + | nil => simp [CountsFit] at hfits + | cons d ds => + rcases hfits with ⟨hstartb, hcap, hfitsTail⟩ + have hn : ∀ i < d, chain start i < b := by + intro i hi + exact (chain_lt_iff start b i).mpr + (lt_of_lt_of_le (by exact_mod_cast hi) hcap) + let db₁ := db.insertInterval start b hstartb d hn + have hbelow₁ : ∀ k ∈ db₁.keys, k < b := + DB.insertInterval_keys_lt_of_below db hstartb d hn hbelow + obtain ⟨db₂, hcounts₂, htop₂, hlower₂⟩ := + ih db₁ b hbelow₁ ds hfitsTail + refine ⟨db₂, ?_, ?_, ?_⟩ + · constructor + · have hb₂ : cnt db₂ (b : WithTop Key) = cnt db₁ (b : WithTop Key) := + hlower₂ (b : WithTop Key) le_rfl + rw [hb₂, DB.cnt_insertInterval_self db hstartb d hn hbelow] + · rw [DB.insertInterval_length_of_below db hstartb d hn hbelow] at hcounts₂ + exact hcounts₂ + · rw [htop₂, DB.insertInterval_length_of_below db hstartb d hn hbelow, + List.sum_cons] + omega + · intro w hw + have hwb : w ≤ (b : WithTop Key) := + le_trans hw (WithTop.coe_le_coe.mpr hstartb) + rw [hlower₂ w hwb] + cases w with + | top => exact (WithTop.not_top_le_coe start hw).elim + | coe x => + exact DB.cnt_insertInterval_of_le db hstartb d hn + (WithTop.coe_le_coe.mp hw) + +/-- **Database realization from the empty key.** Any list of consecutive +counts that fits the capacities is realized by a database, with zero keys below +`⊥`, the prescribed cumulative counts at the finite endpoints, and an arbitrary +number of further keys above the last endpoint. -/ +theorem exists_db_realizing_counts (bounds : List Key) (counts : List ℕ) + (topCount : ℕ) (hfits : CountsFit ([] : Key) bounds counts) : + ∃ db : DB, + CountsAt db 0 bounds counts ∧ cnt db ⊤ = counts.sum + topCount := by + let empty : DB := ⟨[], List.sortedLT_nil⟩ + obtain ⟨db, hcounts, htop, -⟩ := + exists_db_extend_with_top empty ([] : Key) (by intro k hk; simp [empty] at hk) + bounds counts topCount hfits + refine ⟨db, ?_, ?_⟩ + · simpa [empty] using hcounts + · simpa [empty] using htop + +/-- **Realizability of a count list is exactly its capacity fit.** -/ +theorem exists_db_counts_iff (bounds : List Key) (counts : List ℕ) (topCount : ℕ) + (hle : BoundsLe ([] : Key) bounds) : + (∃ db : DB, CountsAt db 0 bounds counts ∧ cnt db ⊤ = counts.sum + topCount) ↔ + CountsFit ([] : Key) bounds counts := by + constructor + · rintro ⟨db, hcounts, -⟩ + exact countsFit_of_CountsAt hcounts hle (by simp [cnt_nil]) + · intro hfit + exact exists_db_realizing_counts bounds counts topCount hfit + +end FormalConflictRange diff --git a/FormalConflictRange/Reduction.lean b/FormalConflictRange/Reduction.lean new file mode 100644 index 0000000..8b3195b --- /dev/null +++ b/FormalConflictRange/Reduction.lean @@ -0,0 +1,1229 @@ +/- +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.Empty + +/-! +# Reducing a general result to the empty boundary reads + +The empty-result theory of `FormalConflictRange.Empty` characterizes a read whose +complete result is empty. This file reduces a *general* result to that theory, in +full generality of traversal and truncation. + +For an untruncated result, the complete keys of a read equal a nonempty ascending +list `L` exactly when the two *boundary reads* — the read truncated to `L`'s least +reported key, and the read starting just past `L`'s greatest reported key — are both +empty, the middle interval `[k₀, keyAfter kₙ)` holds exactly `L`, and the resolved +window is bracketed by `k₀` and `kₙ`. A *reversed* read returns `L` descending; the +same bridge is stated for its ascending reading `L.reverse`. + +For a *truncated* result only one end of the window is reported. A non-reversed read +returns an ascending prefix `L`: the upper boundary read is dropped, and the lower boundary +read, the middle equality, and the bracketing remain (`bridgeFwdTrunc`). A reversed +read returns a descending prefix, i.e. an ascending suffix `L.reverse`: the lower boundary +read is dropped and the upper boundary read remains (`bridgeRevTrunc`). The single +statement `compatible_iff_bridge_general` covers all four combinations via +`bridgeOf`. + +## The factors + +* `boundaryReadLo r k₀` keeps the full begin selector and ends at the offset-one + selector keyed at `k₀`. Its emptiness is `[resolve begin, k₀)` holding no stored + key. +* `boundaryReadHi r kₙ` begins at the offset-one selector keyed at `keyAfter kₙ` and + keeps the full end selector. Its emptiness is `[keyAfter kₙ, resolve end)` + holding no stored key. +* `midEq L db` is the interval equality `[k₀, keyAfter kₙ).keys(db) = L`. +* `bracket r L db` is the pair of inequalities `resolve begin ≤ k₀` and + `kₙ < resolve end`, which put `k₀` least and `kₙ` greatest. +-/ + +namespace FormalConflictRange + +open RangeRead + +/-! ## The offset-one selector keyed at a value -/ + +/-- The offset-one selector keyed at `w`: it resolves to the first stored key at +or above `w`, or to `⊤` when there is none. -/ +def selAt (w : WithTop Key) : Selector := ⟨w, 1⟩ + +@[simp] theorem selAt_key (w : WithTop Key) : (selAt w).key = w := rfl +@[simp] theorem selAt_offset (w : WithTop Key) : (selAt w).offset = 1 := rfl + +/-- The anchor of `⟨↑b, 1⟩` is the count of stored keys strictly below `b`. -/ +theorem anchor_selAt_coe_toNat (db : DB) (b : Key) : + ((selAt (↑b : WithTop Key)).anchor db).toNat = + db.keys.countP (fun x => decide (x < b)) := by + rw [Selector.anchor, selAt_key, selAt_offset, countP_lt_coe] + norm_num + +/-- The anchor of `⟨↑b, 1⟩` is nonnegative. -/ +theorem anchor_selAt_coe_nonneg (db : DB) (b : Key) : + 0 ≤ (selAt (↑b : WithTop Key)).anchor db := by + rw [Selector.anchor, selAt_key, selAt_offset, countP_lt_coe] + have := Int.natCast_nonneg (db.keys.countP (fun x => decide (x < b))) + omega + +/-- With room to the right, `⟨↑b, 1⟩` resolves to the stored key at the count of +keys strictly below `b`. -/ +theorem resolve_selAt_coe_getElem (db : DB) (b : Key) + (h : db.keys.countP (fun x => decide (x < b)) < db.keys.length) : + resolve db (selAt (↑b : WithTop Key)) = + (↑(db.keys[db.keys.countP (fun x => decide (x < b))]) : WithTop Key) := by + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr (anchor_selAt_coe_nonneg db b)), + show db.keys[((selAt (↑b : WithTop Key)).anchor db).toNat]? = + db.keys[db.keys.countP (fun x => decide (x < b))]? by rw [anchor_selAt_coe_toNat], + List.getElem?_eq_getElem h] + +/-- A selector keyed at a stored key with offset one resolves to that key. -/ +theorem resolve_selAt_self {db : DB} {k : Key} (hk : k ∈ db.keys) : + resolve db (selAt (↑k : WithTop Key)) = (↑k : WithTop Key) := by + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hk + have hlt : db.keys.countP (fun x => decide (x < k)) < db.keys.length := by + rw [countP_lt_eq_of_getElem db.sorted hi hik]; exact hi + rw [resolve_selAt_coe_getElem db k hlt, getElem_countP_lt db.sorted hk] + +/-- The offset-one selector keyed at `b` resolves at or above `b`. -/ +theorem coe_le_resolve_selAt (db : DB) (b : Key) : + (↑b : WithTop Key) ≤ resolve db (selAt (↑b : WithTop Key)) := by + by_cases h : db.keys.countP (fun x => decide (x < b)) < db.keys.length + · rw [resolve_selAt_coe_getElem db b h] + exact WithTop.coe_le_coe.mpr (le_getElem_of_countP_le db.sorted (le_refl _) h) + · rw [resolve_eq_top (db := db) (s := selAt (↑b : WithTop Key)) + (anchor_selAt_coe_nonneg db b) (by rw [anchor_selAt_coe_toNat]; omega)] + exact le_top + +/-- The resolution of `⟨↑b, 1⟩` is at most any stored key at or above `b`. -/ +theorem resolve_selAt_le_of_mem {db : DB} {b x : Key} (hx : x ∈ db.keys) (hbx : b ≤ x) : + resolve db (selAt (↑b : WithTop Key)) ≤ (↑x : WithTop Key) := by + obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hx + have hbx' : b ≤ db.keys[j] := by rw [hjx]; exact hbx + have hci : db.keys.countP (fun y => decide (y < b)) ≤ j := by + by_contra hcon + exact absurd (getElem_lt_of_lt_countP db.sorted hj (not_le.mp hcon)) (not_lt.mpr hbx') + have hcl : db.keys.countP (fun y => decide (y < b)) < db.keys.length := by omega + rw [resolve_selAt_coe_getElem db b hcl] + refine WithTop.coe_le_coe.mpr ?_ + calc (db.keys[db.keys.countP (fun y => decide (y < b))]'(by omega) : Key) + ≤ db.keys[j] := (db.sorted.getElem_le_getElem_iff).mpr hci + _ = x := hjx + +/-! ## The boundary reads -/ + +/-- The lower boundary read of the target least key `k₀`: the full begin selector, with +the end selector replaced by `⟨↑k₀, 1⟩`. -/ +def boundaryReadLo (r : RangeRead) (k₀ : Key) : RangeRead := + { r with end_ := selAt (↑k₀ : WithTop Key) } + +/-- The upper boundary read of the target greatest key `kₙ`: the begin selector replaced +by `⟨keyAfter kₙ, 1⟩`, keeping the full end selector. -/ +def boundaryReadHi (r : RangeRead) (kₙ : Key) : RangeRead := + { r with begin := selAt (keyAfter kₙ : WithTop Key) } + +/-! ## List helpers for the ascending reported keys -/ + +/-- Membership in `inRange` unfolded. -/ +theorem inRange_mem_iff {ks : List Key} {lo hi : WithTop Key} {x : Key} : + x ∈ inRange ks lo hi ↔ x ∈ ks ∧ lo ≤ (↑x : WithTop Key) ∧ (↑x : WithTop Key) < hi := by + unfold inRange + rw [List.mem_filter, decide_eq_true_eq] + +/-- Two strictly increasing key lists with the same members are equal. -/ +theorem eq_of_mem_iff_pairwise {l₁ l₂ : List Key} + (h₁ : l₁.Pairwise (· < ·)) (h₂ : l₂.Pairwise (· < ·)) + (h : ∀ x, x ∈ l₁ ↔ x ∈ l₂) : l₁ = l₂ := + List.Perm.eq_of_pairwise (fun _ _ _ _ hab hba => (lt_asymm hab hba).elim) h₁ h₂ + ((List.subperm_of_subset h₁.nodup (fun _ ha => (h _).mp ha)).antisymm + (List.subperm_of_subset h₂.nodup (fun _ ha => (h _).mpr ha))) + +/-- The least element of a nonempty ascending list is at or below every element. -/ +theorem headD_le_of_pairwise {L : List Key} {x : Key} (h : L.Pairwise (· < ·)) (hx : x ∈ L) : + L.headD ([] : Key) ≤ x := by + cases L with + | nil => exact absurd hx (by simp) + | cons a t => + rw [List.headD_cons] + rcases List.mem_cons.mp hx with hxx | hx + · rw [hxx] + · exact le_of_lt ((List.pairwise_cons.mp h).1 x hx) + +/-- The greatest element of a nonempty ascending list is at or above every element. -/ +theorem le_getLastD_of_pairwise {L : List Key} {x : Key} (h : L.Pairwise (· < ·)) (hx : x ∈ L) : + x ≤ L.getLastD ([] : Key) := by + have hne : L ≠ [] := by rintro rfl; exact absurd hx (by simp) + have hlast : x ≤ L.getLast hne := (h.imp (fun hab => le_of_lt hab)).rel_getLast hx + have : L.getLastD ([] : Key) = L.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne, Option.getD_some] + rw [this]; exact hlast + +/-- The least element of a nonempty list is a member. -/ +theorem headD_mem {L : List Key} (hne : L ≠ []) : L.headD ([] : Key) ∈ L := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne, Option.getD_some] + exact List.head_mem hne + +/-- The greatest element of a nonempty list is a member. -/ +theorem getLastD_mem {L : List Key} (hne : L ≠ []) : L.getLastD ([] : Key) ∈ L := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne, Option.getD_some] + exact List.getLast_mem hne + +/-! ## The bridge -/ + +/-- The middle predicate: the stored keys in `[k₀, keyAfter kₙ)` are exactly `L`. -/ +def midEq (L : List Key) (db : DB) : Prop := + inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) = L + +/-- The bracketing predicate: `k₀` is at or above `resolve begin` and `kₙ` strictly +below `resolve end`, so `k₀` is the least complete key and `kₙ` the greatest. -/ +def bracket (r : RangeRead) (L : List Key) (db : DB) : Prop := + resolve db r.begin ≤ (↑(L.headD ([] : Key)) : WithTop Key) ∧ + (↑(L.getLastD ([] : Key)) : WithTop Key) < resolve db r.end_ + +/-- The full bridge predicate: both boundary reads are empty, the middle interval is `L`, +and `L`'s endpoints bracket the resolved window. -/ +def bridge (r : RangeRead) (L : List Key) (db : DB) : Prop := + completeKeys db (boundaryReadLo r (L.headD ([] : Key))) = [] ∧ + completeKeys db (boundaryReadHi r (L.getLastD ([] : Key))) = [] ∧ + midEq L db ∧ bracket r L db + +/-- The bridge with its two end-emptiness conjuncts exchanged. Stating the swap once +keeps the upper-end factor-toggle arguments from re-elaborating the conjunction. -/ +theorem bridge_swap {r : RangeRead} {L : List Key} {db : DB} : + bridge r L db ↔ + (completeKeys db (boundaryReadHi r (L.getLastD ([] : Key))) = [] ∧ + completeKeys db (boundaryReadLo r (L.headD ([] : Key))) = [] ∧ + midEq L db ∧ bracket r L db) := by + unfold bridge + exact ⟨fun ⟨a, b, c, d⟩ => ⟨b, a, c, d⟩, fun ⟨a, b, c, d⟩ => ⟨b, a, c, d⟩⟩ + +/-- For a non-reversed read the complete keys are the in-range stored keys. -/ +theorem completeKeys_of_not_reverse {db : DB} {r : RangeRead} (hrev : r.reverse = false) : + completeKeys db r = inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + unfold completeKeys + rw [ite_eq_right (by simp [hrev])] + +/-- The lower boundary read's complete keys are the in-range stored keys ending at +`resolve ⟨↑k, 1⟩`. -/ +theorem completeKeys_boundaryReadLo {db : DB} {r : RangeRead} (hrev : r.reverse = false) (k : Key) : + completeKeys db (boundaryReadLo r k) = + inRange db.keys (resolve db r.begin) (resolve db (selAt (↑k : WithTop Key))) := by + unfold boundaryReadLo + rw [completeKeys_of_not_reverse (by simp [hrev])] + +/-- The upper boundary read's complete keys are the in-range stored keys beginning at +`resolve ⟨keyAfter k, 1⟩`. -/ +theorem completeKeys_boundaryReadHi {db : DB} {r : RangeRead} (hrev : r.reverse = false) (k : Key) : + completeKeys db (boundaryReadHi r k) = + inRange db.keys (resolve db (selAt (↑(keyAfter k) : WithTop Key))) (resolve db r.end_) := by + unfold boundaryReadHi + rw [completeKeys_of_not_reverse (by simp [hrev])] + +/-- A read is empty exactly when its in-range stored keys are empty, regardless of +traversal: reversal preserves emptiness. -/ +theorem completeKeys_eq_nil_iff_inRange {db : DB} (r : RangeRead) : + completeKeys db r = [] ↔ inRange db.keys (resolve db r.begin) (resolve db r.end_) = [] := by + unfold completeKeys + split <;> simp + +/-- **The bridge equivalence for the ascending reading.** For a nonempty ascending +list `L`, the in-range stored keys equal `L` exactly when both boundary reads are empty, +the middle interval `[k₀, keyAfter kₙ)` is `L`, and the endpoints bracket the resolved +window. This is the traversal-independent core: `completeKeys` is this statement read +forward or reversed. -/ +theorem inRange_eq_iff_bridge {db : DB} {r : RangeRead} {L : List Key} + (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) : + inRange db.keys (resolve db r.begin) (resolve db r.end_) = L ↔ bridge r L db := by + have hlo_mem : L.headD ([] : Key) ∈ L := headD_mem hL + have hhi_mem : L.getLastD ([] : Key) ∈ L := getLastD_mem hL + have hLo_le_Hi : (↑(L.headD ([] : Key)) : WithTop Key) ≤ + (↑(L.getLastD ([] : Key)) : WithTop Key) := + WithTop.coe_le_coe.mpr (headD_le_of_pairwise hpair hhi_mem) + constructor + · intro hck + have hspec : ∀ {y : Key}, y ∈ L → y ∈ db.keys ∧ + resolve db r.begin ≤ (↑y : WithTop Key) ∧ + (↑y : WithTop Key) < resolve db r.end_ := by + intro y hy + have hy' : y ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [hck]; exact hy + exact inRange_mem_iff.mp hy' + have hbrack : bracket r L db := + ⟨(hspec hlo_mem).2.1, (hspec hhi_mem).2.2⟩ + refine ⟨?_, ?_, ?_, hbrack⟩ + · rw [completeKeys_eq_nil_iff_inRange, List.eq_nil_iff_forall_not_mem] + intro x hx + obtain ⟨hxdb, hlo, hhi⟩ := inRange_mem_iff.mp hx + simp only [boundaryReadLo] at hlo hhi + rw [resolve_selAt_self (hspec hlo_mem).1] at hhi + have hxL : x ∈ L := by + rw [← hck] + refine inRange_mem_iff.mpr ⟨hxdb, hlo, ?_⟩ + exact lt_of_le_of_lt (le_trans (le_of_lt hhi) hLo_le_Hi) (hspec hhi_mem).2.2 + exact absurd (headD_le_of_pairwise hpair hxL) (not_le.mpr (WithTop.coe_lt_coe.mp hhi)) + · rw [completeKeys_eq_nil_iff_inRange, List.eq_nil_iff_forall_not_mem] + intro x hx + obtain ⟨hxdb, hlo, hhi⟩ := inRange_mem_iff.mp hx + have hxge : (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) ≤ (↑x : WithTop Key) := + le_trans (coe_le_resolve_selAt db (keyAfter (L.getLastD ([] : Key)))) hlo + have hxgt : L.getLastD ([] : Key) < x := + WithTop.coe_lt_coe.mp (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt _)) hxge) + have hlo' : resolve db r.begin ≤ (↑x : WithTop Key) := + le_trans (hspec hlo_mem).2.1 + (WithTop.coe_le_coe.mpr (le_trans (headD_le_of_pairwise hpair hhi_mem) (le_of_lt hxgt))) + have hxL : x ∈ L := by + rw [← hck] + exact inRange_mem_iff.mpr ⟨hxdb, hlo', hhi⟩ + exact absurd (le_getLastD_of_pairwise hpair hxL) (not_le.mpr hxgt) + · unfold midEq + refine eq_of_mem_iff_pairwise (RangeRead.inRange_pairwise db _ _) hpair ?_ + intro x + rw [inRange_mem_iff] + constructor + · rintro ⟨hxdb, hlo, hhi⟩ + rw [← hck] + exact inRange_mem_iff.mpr ⟨hxdb, le_trans hbrack.1 hlo, + lt_of_lt_of_le hhi (keyAfter_le_of_lt hbrack.2)⟩ + · intro hx + have hxsp := hspec hx + exact ⟨hxsp.1, WithTop.coe_le_coe.mpr (headD_le_of_pairwise hpair hx), + lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_getLastD_of_pairwise hpair hx)) + (WithTop.coe_lt_coe.mpr (keyAfter_self_lt _))⟩ + · rintro ⟨hlow, hhigh, hmid, hbrack⟩ + rw [completeKeys_eq_nil_iff_inRange] at hlow + rw [completeKeys_eq_nil_iff_inRange] at hhigh + simp only [boundaryReadLo] at hlow + simp only [boundaryReadHi] at hhigh + have hLo_stored : L.headD ([] : Key) ∈ db.keys := by + have : L.headD ([] : Key) ∈ inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) := by rw [hmid]; exact hlo_mem + exact (inRange_mem_iff.mp this).1 + have hHi_stored : L.getLastD ([] : Key) ∈ db.keys := by + have : L.getLastD ([] : Key) ∈ inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) := by rw [hmid]; exact hhi_mem + exact (inRange_mem_iff.mp this).1 + refine eq_of_mem_iff_pairwise (RangeRead.inRange_pairwise db _ _) hpair ?_ + intro x + rw [inRange_mem_iff] + constructor + · intro hx + obtain ⟨hxdb, hxlo, hxhi⟩ := hx + by_contra hxL + by_cases hlt : x < L.headD ([] : Key) + · have hmem : x ∈ inRange db.keys (resolve db r.begin) + (resolve db (selAt (↑(L.headD ([] : Key))))) := by + rw [inRange_mem_iff, resolve_selAt_self hLo_stored] + exact ⟨hxdb, hxlo, WithTop.coe_lt_coe.mpr hlt⟩ + rw [hlow] at hmem; exact List.not_mem_nil hmem + · have hge : L.headD ([] : Key) ≤ x := le_of_not_gt hlt + by_cases hge2 : (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) ≤ (↑x : WithTop Key) + · have hmem : x ∈ inRange db.keys + (resolve db (selAt (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key))) + (resolve db r.end_) := by + rw [inRange_mem_iff] + exact ⟨hxdb, resolve_selAt_le_of_mem hxdb (WithTop.coe_le_coe.mp hge2), hxhi⟩ + rw [hhigh] at hmem; exact List.not_mem_nil hmem + · have hxmid : x ∈ inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) := by + rw [inRange_mem_iff] + exact ⟨hxdb, WithTop.coe_le_coe.mpr hge, lt_of_not_ge hge2⟩ + rw [hmid] at hxmid + exact hxL hxmid + · intro hx + have hxmid : x ∈ inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) := by rw [hmid]; exact hx + obtain ⟨hxdb, hxlo, hxhi⟩ := inRange_mem_iff.mp hxmid + exact ⟨hxdb, le_trans hbrack.1 hxlo, + lt_of_lt_of_le hxhi (keyAfter_le_of_lt hbrack.2)⟩ + +/-- **A non-reversed read's complete keys are its in-range stored keys.** -/ +theorem completeKeys_eq_inRange {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = false) : completeKeys db r = L ↔ + inRange db.keys (resolve db r.begin) (resolve db r.end_) = L := by + unfold completeKeys + rw [ite_eq_right (by simp [hrev])] + +/-- **A reversed read's complete keys are the reverse of its in-range stored keys.** +The `↔` is reversed because `L` names the returned, descending list. -/ +theorem completeKeys_eq_reverse_inRange {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = true) : completeKeys db r = L ↔ + inRange db.keys (resolve db r.begin) (resolve db r.end_) = L.reverse := by + unfold completeKeys + rw [ite_eq_left (by simp [hrev]), List.reverse_eq_iff] + +/-- **The bridge equivalence, non-reversed.** For a non-reversed read and a nonempty +ascending `L`, the complete keys equal `L` exactly when the bridge holds. -/ +theorem completeKeys_eq_iff_bridge {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = false) (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) : + completeKeys db r = L ↔ bridge r L db := by + rw [completeKeys_eq_inRange hrev] + exact inRange_eq_iff_bridge hL hpair + +/-- **The bridge equivalence, reversed.** For a reversed read the returned list `L` is +descending, so the bridge is stated for `L.reverse`, the ascending reading of the same +window. -/ +theorem completeKeys_eq_iff_bridge_rev {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = true) (hL : L ≠ []) (hpair : L.Pairwise (· > ·)) : + completeKeys db r = L ↔ bridge r L.reverse db := by + have hLr : L.reverse ≠ [] := by simpa using hL + have hpairr : L.reverse.Pairwise (· < ·) := by simpa using hpair.reverse + rw [completeKeys_eq_reverse_inRange hrev] + exact inRange_eq_iff_bridge hLr hpairr + +/-- **Compatibility of a complete result factors as its bridge.** For a non-reversed +read, a nonempty ascending list `L` within the limit, compatibility with the +complete result `⟨L, false⟩` is equivalent to the bridge predicate. -/ +theorem compatible_complete_iff_bridge {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = false) (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) + (hlim : withinLimit r L) : + RangeRead.Compatible db r { keys := L, more := false } ↔ bridge r L db := by + unfold RangeRead.Compatible + rw [show ({ keys := L, more := false } : Result).more = false from rfl, + show ({ keys := L, more := false } : Result).keys = L from rfl] + constructor + · rintro ⟨-, h | ⟨-, heq⟩⟩ + · exact absurd h (by simp) + · exact (completeKeys_eq_iff_bridge hrev hL hpair).mp heq.symm + · intro hb + exact ⟨hlim, Or.inr ⟨rfl, ((completeKeys_eq_iff_bridge hrev hL hpair).mpr hb).symm⟩⟩ + +/-- **Compatibility of a reversed complete result factors as its bridge.** The +reported list `L` is descending; the bridge is stated for `L.reverse`. -/ +theorem compatible_complete_iff_bridge_rev {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = true) (hL : L ≠ []) (hpair : L.Pairwise (· > ·)) + (hlim : withinLimit r L) : + RangeRead.Compatible db r { keys := L, more := false } ↔ bridge r L.reverse db := by + unfold RangeRead.Compatible + rw [show ({ keys := L, more := false } : Result).more = false from rfl, + show ({ keys := L, more := false } : Result).keys = L from rfl] + constructor + · rintro ⟨-, h | ⟨-, heq⟩⟩ + · exact absurd h (by simp) + · exact (completeKeys_eq_iff_bridge_rev hrev hL hpair).mp heq.symm + · intro hb + exact ⟨hlim, Or.inr ⟨rfl, ((completeKeys_eq_iff_bridge_rev hrev hL hpair).mpr hb).symm⟩⟩ + +/-! +## Truncated results + +A truncated result reports only one end of the window: a non-reversed read reports an +ascending prefix of the in-range keys, a reversed read reports a descending prefix +(an ascending suffix read backwards). The bridge accordingly drops the boundary read on +the *unreported* side — the upper end for a non-reversed read, the lower end for a +reversed one — and keeps the middle equality and the bracketing. +-/ + +/-- Splitting an interval at an intermediate bound: the in-range keys of `[a, c)` are +those of `[a, b)` followed by those of `[b, c)`. -/ +theorem inRange_append_range (db : DB) {a b c : WithTop Key} (hab : a ≤ b) (hbc : b ≤ c) : + inRange db.keys a c = inRange db.keys a b ++ inRange db.keys b c := by + apply eq_of_mem_iff_pairwise + · exact RangeRead.inRange_pairwise db a c + · refine (List.pairwise_append).mpr ⟨RangeRead.inRange_pairwise db a b, + RangeRead.inRange_pairwise db b c, ?_⟩ + intro x hx y hy + rw [inRange_mem_iff] at hx hy + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hx.2.2 hy.2.1) + · intro x + rw [inRange_mem_iff, List.mem_append, inRange_mem_iff, inRange_mem_iff] + constructor + · rintro ⟨hx, hax, hxc⟩ + rcases lt_or_ge (x : WithTop Key) b with h | h + · exact Or.inl ⟨hx, hax, h⟩ + · exact Or.inr ⟨hx, h, hxc⟩ + · rintro (⟨hx, hax, hxb⟩ | ⟨hx, hbx, hxc⟩) + · exact ⟨hx, hax, lt_of_lt_of_le hxb hbc⟩ + · exact ⟨hx, le_trans hab hbx, hxc⟩ + +/-- A list is a prefix of the reverse of `l` exactly when its reverse is a suffix of +`l`. -/ +theorem prefix_reverse_iff_suffix {l₁ l₂ : List Key} : + l₁ <+: l₂.reverse ↔ l₁.reverse <:+ l₂ := by + simpa using (List.reverse_suffix (l₁ := l₁) (l₂ := l₂.reverse)).symm + +/-- The forward truncated bridge: the lower boundary read is empty, the middle interval is +`L`, and `L` is bracketed in the window. The upper boundary read is dropped — the reported +keys are a prefix, so nothing above the last reported key is constrained. -/ +def bridgeFwdTrunc (r : RangeRead) (L : List Key) (db : DB) : Prop := + completeKeys db (boundaryReadLo r (L.headD ([] : Key))) = [] ∧ midEq L db ∧ bracket r L db + +/-- The reverse truncated bridge: the upper boundary read is empty, the middle interval is +`L`, and `L` is bracketed in the window. The lower boundary read is dropped — the reported +keys are a suffix, so nothing below the least reported key is constrained. -/ +def bridgeRevTrunc (r : RangeRead) (L : List Key) (db : DB) : Prop := + completeKeys db (boundaryReadHi r (L.getLastD ([] : Key))) = [] ∧ midEq L db ∧ bracket r L db + +/-- **Forward truncation is a prefix.** For a nonempty ascending `L`, `L` is a prefix +of the in-range keys exactly when the forward truncated bridge holds. -/ +theorem prefix_inRange_iff_bridgeFwdTrunc {db : DB} {r : RangeRead} {L : List Key} + (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) : + L <+: inRange db.keys (resolve db r.begin) (resolve db r.end_) ↔ + bridgeFwdTrunc r L db := by + have hlo_mem : L.headD ([] : Key) ∈ L := headD_mem hL + have hhi_mem : L.getLastD ([] : Key) ∈ L := getLastD_mem hL + have hW : (inRange db.keys (resolve db r.begin) (resolve db r.end_)).Pairwise (· < ·) := + RangeRead.inRange_pairwise db _ _ + have hlo_le_hi : L.headD ([] : Key) ≤ L.getLastD ([] : Key) := + headD_le_of_pairwise hpair hhi_mem + constructor + · intro hpre + obtain ⟨rest, hrest⟩ := hpre + have hW' : (L ++ rest).Pairwise (· < ·) := by rw [hrest]; exact hW + obtain ⟨-, -, hcross⟩ := List.pairwise_append.mp hW' + have hinL : ∀ {x : Key}, x ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) → + x ∈ L ∨ x ∈ rest := by + intro x hx; rw [← hrest] at hx; exact List.mem_append.mp hx + have hknW : L.getLastD ([] : Key) ∈ + inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [← hrest]; exact List.mem_append.mpr (Or.inl hhi_mem) + have hkoW : L.headD ([] : Key) ∈ + inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [← hrest]; exact List.mem_append.mpr (Or.inl hlo_mem) + have hknE : (↑(L.getLastD ([] : Key)) : WithTop Key) < resolve db r.end_ := + (inRange_mem_iff.mp hknW).2.2 + have hBko : resolve db r.begin ≤ (↑(L.headD ([] : Key)) : WithTop Key) := + (inRange_mem_iff.mp hkoW).2.1 + have hkostore : L.headD ([] : Key) ∈ db.keys := (inRange_mem_iff.mp hkoW).1 + have hlow : completeKeys db (boundaryReadLo r (L.headD ([] : Key))) = [] := by + rw [completeKeys_eq_nil_iff_inRange] + simp only [boundaryReadLo, resolve_selAt_self hkostore] + rw [List.eq_nil_iff_forall_not_mem] + intro x hx + obtain ⟨hxdb, hBx, hxko⟩ := inRange_mem_iff.mp hx + have hxltE : (↑x : WithTop Key) < resolve db r.end_ := + lt_of_lt_of_le hxko (le_trans (WithTop.coe_le_coe.mpr hlo_le_hi) (le_of_lt hknE)) + have hxW : x ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) := + inRange_mem_iff.mpr ⟨hxdb, hBx, hxltE⟩ + rcases hinL hxW with hxL | hxr + · exact absurd (headD_le_of_pairwise hpair hxL) (not_le.mpr (WithTop.coe_lt_coe.mp hxko)) + · exact absurd (hcross (L.headD ([] : Key)) hlo_mem x hxr) + (not_lt.mpr (le_of_lt (WithTop.coe_lt_coe.mp hxko))) + have hmid : midEq L db := by + unfold midEq + apply eq_of_mem_iff_pairwise (RangeRead.inRange_pairwise db _ _) hpair + intro x + rw [inRange_mem_iff] + constructor + · rintro ⟨hxdb, hkx, hxkA⟩ + have hxle : x ≤ L.getLastD ([] : Key) := + (lt_keyAfter_iff x (L.getLastD ([] : Key))).mp (WithTop.coe_lt_coe.mp hxkA) + have hxW : x ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) := + inRange_mem_iff.mpr ⟨hxdb, le_trans hBko hkx, + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hxle) hknE⟩ + rcases hinL hxW with hxL | hxr + · exact hxL + · exact absurd (hcross (L.getLastD ([] : Key)) hhi_mem x hxr) (not_lt.mpr hxle) + · intro hx + exact ⟨(inRange_mem_iff.mp (by rw [← hrest]; exact List.mem_append.mpr (Or.inl hx))).1, + WithTop.coe_le_coe.mpr (headD_le_of_pairwise hpair hx), + WithTop.coe_lt_coe.mpr + (lt_of_le_of_lt (le_getLastD_of_pairwise hpair hx) (keyAfter_self_lt _))⟩ + exact ⟨hlow, hmid, hBko, hknE⟩ + · rintro ⟨hlow, hmid, hbrack⟩ + rw [completeKeys_eq_nil_iff_inRange] at hlow + simp only [boundaryReadLo] at hlow + have hko_mid : L.headD ([] : Key) ∈ inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) := by + rw [hmid]; exact hlo_mem + have hkostore : L.headD ([] : Key) ∈ db.keys := (inRange_mem_iff.mp hko_mid).1 + rw [resolve_selAt_self hkostore] at hlow + have hko_ka : (↑(L.headD ([] : Key)) : WithTop Key) ≤ + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hlo_le_hi (keyAfter_self_lt _))) + have h1 : inRange db.keys (resolve db r.begin) (resolve db r.end_) = + inRange db.keys (resolve db r.begin) (↑(L.headD ([] : Key)) : WithTop Key) ++ + inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) (resolve db r.end_) := + inRange_append_range db hbrack.1 (le_trans hko_ka (keyAfter_le_of_lt hbrack.2)) + have h2 : inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) (resolve db r.end_) = + inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) ++ + inRange db.keys (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) + (resolve db r.end_) := + inRange_append_range db hko_ka (keyAfter_le_of_lt hbrack.2) + rw [h1, h2, hlow, List.nil_append, hmid] + exact List.prefix_append _ _ + +/-- **Reverse truncation is a suffix.** For a nonempty ascending `L`, `L` is a suffix +of the in-range keys exactly when the reverse truncated bridge holds. -/ +theorem suffix_inRange_iff_bridgeRevTrunc {db : DB} {r : RangeRead} {L : List Key} + (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) : + L <:+ inRange db.keys (resolve db r.begin) (resolve db r.end_) ↔ + bridgeRevTrunc r L db := by + have hlo_mem : L.headD ([] : Key) ∈ L := headD_mem hL + have hhi_mem : L.getLastD ([] : Key) ∈ L := getLastD_mem hL + have hW : (inRange db.keys (resolve db r.begin) (resolve db r.end_)).Pairwise (· < ·) := + RangeRead.inRange_pairwise db _ _ + have hlo_le_hi : L.headD ([] : Key) ≤ L.getLastD ([] : Key) := + headD_le_of_pairwise hpair hhi_mem + constructor + · intro hsuf + obtain ⟨rest, hrest⟩ := hsuf + have hW' : (rest ++ L).Pairwise (· < ·) := by rw [hrest]; exact hW + obtain ⟨-, -, hcross⟩ := List.pairwise_append.mp hW' + have hinL : ∀ {x : Key}, x ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) → + x ∈ rest ∨ x ∈ L := by + intro x hx; rw [← hrest] at hx; exact List.mem_append.mp hx + have hkoW : L.headD ([] : Key) ∈ + inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [← hrest]; exact List.mem_append.mpr (Or.inr hlo_mem) + have hknW : L.getLastD ([] : Key) ∈ + inRange db.keys (resolve db r.begin) (resolve db r.end_) := by + rw [← hrest]; exact List.mem_append.mpr (Or.inr hhi_mem) + have hBko : resolve db r.begin ≤ (↑(L.headD ([] : Key)) : WithTop Key) := + (inRange_mem_iff.mp hkoW).2.1 + have hknE : (↑(L.getLastD ([] : Key)) : WithTop Key) < resolve db r.end_ := + (inRange_mem_iff.mp hknW).2.2 + have hknstore : L.getLastD ([] : Key) ∈ db.keys := (inRange_mem_iff.mp hknW).1 + have hhigh : completeKeys db (boundaryReadHi r (L.getLastD ([] : Key))) = [] := by + rw [completeKeys_eq_nil_iff_inRange] + simp only [boundaryReadHi] + rw [List.eq_nil_iff_forall_not_mem] + intro x hx + obtain ⟨hxdb, hkaX, hxE⟩ := inRange_mem_iff.mp hx + have hxgt : L.getLastD ([] : Key) < x := + WithTop.coe_lt_coe.mp (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt _)) + (le_trans (coe_le_resolve_selAt db (keyAfter (L.getLastD ([] : Key)))) hkaX)) + have hxW : x ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) := + inRange_mem_iff.mpr ⟨hxdb, le_trans hBko + (WithTop.coe_le_coe.mpr (le_trans hlo_le_hi (le_of_lt hxgt))), hxE⟩ + rcases hinL hxW with hxr | hxL + · exact absurd (hcross x hxr (L.getLastD ([] : Key)) hhi_mem) (not_lt.mpr (le_of_lt hxgt)) + · exact absurd (le_getLastD_of_pairwise hpair hxL) (not_le.mpr hxgt) + have hmid : midEq L db := by + unfold midEq + apply eq_of_mem_iff_pairwise (RangeRead.inRange_pairwise db _ _) hpair + intro x + rw [inRange_mem_iff] + constructor + · rintro ⟨hxdb, hkx, hxkA⟩ + have hxle : x ≤ L.getLastD ([] : Key) := + (lt_keyAfter_iff x (L.getLastD ([] : Key))).mp (WithTop.coe_lt_coe.mp hxkA) + have hxW : x ∈ inRange db.keys (resolve db r.begin) (resolve db r.end_) := + inRange_mem_iff.mpr ⟨hxdb, le_trans hBko hkx, + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hxle) hknE⟩ + rcases hinL hxW with hxr | hxL + · exact absurd (WithTop.coe_lt_coe.mpr (hcross x hxr (L.headD ([] : Key)) hlo_mem)) + (not_lt.mpr hkx) + · exact hxL + · intro hx + exact ⟨(inRange_mem_iff.mp (by rw [← hrest]; exact List.mem_append.mpr (Or.inr hx))).1, + WithTop.coe_le_coe.mpr (headD_le_of_pairwise hpair hx), + WithTop.coe_lt_coe.mpr + (lt_of_le_of_lt (le_getLastD_of_pairwise hpair hx) (keyAfter_self_lt _))⟩ + exact ⟨hhigh, hmid, hBko, hknE⟩ + · rintro ⟨hhigh, hmid, hbrack⟩ + rw [completeKeys_eq_nil_iff_inRange] at hhigh + simp only [boundaryReadHi] at hhigh + have hEmpty : inRange db.keys (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) + (resolve db r.end_) = [] := by + rw [List.eq_nil_iff_forall_not_mem] + intro x hx + obtain ⟨hxdb, hkax, hxE⟩ := inRange_mem_iff.mp hx + have hxH : x ∈ inRange db.keys + (resolve db (selAt (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key))) + (resolve db r.end_) := + inRange_mem_iff.mpr ⟨hxdb, + resolve_selAt_le_of_mem hxdb (WithTop.coe_le_coe.mp hkax), hxE⟩ + rw [hhigh] at hxH + exact List.not_mem_nil hxH + have h1 : inRange db.keys (resolve db r.begin) (resolve db r.end_) = + inRange db.keys (resolve db r.begin) (↑(L.headD ([] : Key)) : WithTop Key) ++ + inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) (resolve db r.end_) := + inRange_append_range db hbrack.1 + (le_trans (WithTop.coe_le_coe.mpr hlo_le_hi) (le_of_lt hbrack.2)) + have h2 : inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) (resolve db r.end_) = + inRange db.keys (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) ++ + inRange db.keys (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) + (resolve db r.end_) := + inRange_append_range db + (WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hlo_le_hi (keyAfter_self_lt _)))) + (keyAfter_le_of_lt hbrack.2) + have hR : inRange db.keys (resolve db r.begin) (resolve db r.end_) = + inRange db.keys (resolve db r.begin) (↑(L.headD ([] : Key)) : WithTop Key) ++ L := by + rw [h1, h2, hEmpty, hmid, List.append_nil] + exact ⟨_, hR.symm⟩ + +/-- **Compatibility of a forward truncated result factors as its bridge.** A +non-reversed result reporting `more` has an ascending prefix `L`; compatibility is the +forward truncated bridge. -/ +theorem compatible_fwdTrunc_iff_bridgeFwdTrunc {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = false) (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) + (hlim : withinLimit r L) : + RangeRead.Compatible db r { keys := L, more := true } ↔ bridgeFwdTrunc r L db := by + unfold RangeRead.Compatible + rw [show ({ keys := L, more := true } : Result).more = true from rfl, + show ({ keys := L, more := true } : Result).keys = L from rfl, + completeKeys_of_not_reverse hrev] + constructor + · rintro ⟨-, h | ⟨hm, _⟩⟩ + · exact (prefix_inRange_iff_bridgeFwdTrunc hL hpair).mp h.2.1 + · exact absurd hm (by simp) + · intro hb + exact ⟨hlim, Or.inl ⟨rfl, (prefix_inRange_iff_bridgeFwdTrunc hL hpair).mpr hb, hL⟩⟩ + +/-- **Compatibility of a reverse truncated result factors as its bridge.** A reversed +result reporting `more` has a descending prefix `L`, i.e. `L.reverse` is an ascending +suffix; compatibility is the reverse truncated bridge for `L.reverse`. -/ +theorem compatible_revTrunc_iff_bridgeRevTrunc {db : DB} {r : RangeRead} {L : List Key} + (hrev : r.reverse = true) (hL : L ≠ []) (hpair : L.Pairwise (· > ·)) + (hlim : withinLimit r L) : + RangeRead.Compatible db r { keys := L, more := true } ↔ + bridgeRevTrunc r L.reverse db := by + have hLr : L.reverse ≠ [] := by simpa using hL + have hpairr : L.reverse.Pairwise (· < ·) := by simpa using hpair.reverse + have hck : completeKeys db r = + (inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by + unfold completeKeys + rw [ite_eq_left (by simp [hrev])] + unfold RangeRead.Compatible + rw [show ({ keys := L, more := true } : Result).more = true from rfl, + show ({ keys := L, more := true } : Result).keys = L from rfl, hck] + constructor + · rintro ⟨-, h | ⟨hm, _⟩⟩ + · have hM : L.reverse <:+ inRange db.keys (resolve db r.begin) (resolve db r.end_) := + prefix_reverse_iff_suffix.mp h.2.1 + exact (suffix_inRange_iff_bridgeRevTrunc hLr hpairr).mp hM + · exact absurd hm (by simp) + · intro hb + have hM : L.reverse <:+ inRange db.keys (resolve db r.begin) (resolve db r.end_) := + (suffix_inRange_iff_bridgeRevTrunc hLr hpairr).mpr hb + have hpre : L <+: (inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := + prefix_reverse_iff_suffix.mpr hM + exact ⟨hlim, Or.inl ⟨rfl, hpre, hL⟩⟩ + +/-- The reported keys in the order the read returns them, from the ascending list `A`: +reversed when the read traverses in reverse. -/ +def reportedList (r : RangeRead) (A : List Key) : List Key := + if r.reverse then A.reverse else A + +/-- The general bridge: the complete bridge for an untruncated result, and the +unreported-end bridge for a truncated one. -/ +def bridgeOf (r : RangeRead) (A : List Key) (more : Bool) (db : DB) : Prop := + match more with + | false => bridge r A db + | true => if r.reverse then bridgeRevTrunc r A db else bridgeFwdTrunc r A db + +/-- **The reduction in full generality.** For a nonempty ascending list `A` (the +reported keys in ascending order), compatibility of the general result — either +truncation flag, either traversal — factors as the limit and the general bridge. The +bridge's factors name the empty boundary read on the reported side (lower for a +non-reversed read, upper for a reversed one), the middle interval equality, and the +bracketing; a truncated result drops the boundary read on the unreported side. -/ +theorem compatible_iff_bridge_general {db : DB} {r : RangeRead} {A : List Key} {more : Bool} + (hA : A ≠ []) (hpair : A.Pairwise (· < ·)) + (hlim : withinLimit r (reportedList r A)) : + RangeRead.Compatible db r { keys := reportedList r A, more := more } ↔ + withinLimit r (reportedList r A) ∧ bridgeOf r A more db := by + have hp : A.reverse.Pairwise (· > ·) := by simpa using hpair.reverse + have hAr : A.reverse ≠ [] := by simpa using hA + unfold bridgeOf + cases more with + | false => + cases hr : r.reverse with + | false => + simp only [reportedList, hr, Bool.false_eq_true, ↓reduceIte] at hlim ⊢ + rw [compatible_complete_iff_bridge hr hA hpair hlim]; tauto + | true => + simp only [reportedList, hr, ↓reduceIte] at hlim ⊢ + rw [compatible_complete_iff_bridge_rev hr hAr hp hlim, List.reverse_reverse]; tauto + | true => + cases hr : r.reverse with + | false => + simp only [reportedList, hr, Bool.false_eq_true, ↓reduceIte] at hlim ⊢ + rw [compatible_fwdTrunc_iff_bridgeFwdTrunc hr hA hpair hlim]; tauto + | true => + simp only [reportedList, hr, ↓reduceIte] at hlim ⊢ + rw [compatible_revTrunc_iff_bridgeRevTrunc hr hAr hp hlim, List.reverse_reverse]; tauto + +/-! +## The factor-toggle lemma + +The necessity half of the reduction never cites an end theorem directly: it needs +only one full-compatible witness `db₀` and a single-key perturbation that breaks +the targeted factor. The generic statement below is the notes' Lemma 7.1: a sound +range can omit a key `k` only if flipping `k`'s membership in a compatible +database preserves compatibility. Since the flip is outside the range, soundness +forces compatibility on the flipped database; if that fails, `k` is necessary. +-/ + +/-- Flip the membership of `k` in `db`: erase it when present, insert it otherwise. -/ +def toggle (db : DB) (k : Key) : DB := + if hk : k ∈ db.keys then db.erase k else db.insert k hk + +/-- Flipping a key outside a range leaves the range's stored keys unchanged. -/ +theorem keys_toggle_of_not_mem (cr : ConflictRange) (db : DB) {k : Key} (hk : k ∉ cr) : + cr.keys (toggle db k) = cr.keys db := by + unfold toggle + by_cases hmem : k ∈ db.keys + · rw [dite_eq_left hmem]; exact ConflictRange.keys_erase_of_not_mem cr db hk + · rw [dite_eq_right hmem]; exact ConflictRange.keys_insert_of_not_mem cr db hmem hk + +/-- Flipping a key outside a range leaves the two databases agreeing on the range. -/ +theorem agreeOn_toggle_of_not_mem (cr : ConflictRange) (db : DB) {k : Key} (hk : k ∉ cr) : + AgreeOn cr db (toggle db k) := + (keys_toggle_of_not_mem cr db hk).symm + +/-- **Factor-toggle (Lemma 7.1).** If some database is compatible with `(r, res)` but +flipping the membership of `k` there breaks compatibility, then `k` is necessary: +any sound range omitting `k` would see both databases as agreeing, so soundness +would transfer the compatibility, contradicting the break. -/ +theorem necessary_of_toggle {r : RangeRead} {res : Result} {k : Key} + (h : ∃ db₀ : DB, RangeRead.Compatible db₀ r res ∧ + ¬ RangeRead.Compatible (toggle db₀ k) r res) : + Necessary r res k := by + obtain ⟨db₀, hc₀, hc₁⟩ := h + intro cr hs + by_contra hkcr + exact hc₁ ((hs db₀ (toggle db₀ k) (agreeOn_toggle_of_not_mem cr db₀ hkcr)).mp hc₀) + +/-- **The factor-toggle lemma.** Suppose compatibility factors as `φ₁ ∧ φ₂` on +databases, some database `db₀` satisfies both factors, and flipping the membership +of `k` in `db₀` breaks `φ₁`. Then no range omitting `k` is sound: such a range sees +`db₀` and its flip as agreeing, so soundness would carry `φ₁`. This is the notes' +Lemma 7.1, and it is the form the necessity constructions use — only the one +targeted factor must break. -/ +theorem not_sound_of_factor_toggle {r : RangeRead} {res : Result} {cr : ConflictRange} + {φ₁ φ₂ : DB → Prop} {db₀ : DB} {k : Key} + (hcomp : ∀ db, RangeRead.Compatible db r res ↔ (φ₁ db ∧ φ₂ db)) + (h₁ : φ₁ db₀) (h₂ : φ₂ db₀) (hnot : ¬ φ₁ (toggle db₀ k)) (hk : k ∉ cr) : + ¬ Sound r res cr := by + intro hs + have hc₀ : RangeRead.Compatible db₀ r res := (hcomp db₀).mpr ⟨h₁, h₂⟩ + have hc₁ : RangeRead.Compatible (toggle db₀ k) r res := + (hs db₀ (toggle db₀ k) (agreeOn_toggle_of_not_mem cr db₀ hk)).mp hc₀ + exact hnot ((hcomp (toggle db₀ k)).mp hc₁).1 + +/-- **Factor-toggle necessity.** Under the factorization of Lemma 7.1, a single +database whose flip breaks one factor makes `k` necessary. -/ +theorem necessary_of_factor_toggle {r : RangeRead} {res : Result} {k : Key} + {φ₁ φ₂ : DB → Prop} {db₀ : DB} + (hcomp : ∀ db, RangeRead.Compatible db r res ↔ (φ₁ db ∧ φ₂ db)) + (h₁ : φ₁ db₀) (h₂ : φ₂ db₀) (hnot : ¬ φ₁ (toggle db₀ k)) : + Necessary r res k := + fun cr hs => by + by_contra hkcr + exact not_sound_of_factor_toggle hcomp h₁ h₂ hnot hkcr hs + +/-! +## The joint base construction + +Section 7's necessity argument does not transfer soundness from the end pair; it +exhibits a single database `db₀` that is compatible with the *full* result and whose +flip at `k` breaks the targeted end factor. That object is named here: a *lower joint +base* for `k` is a full-compatible database whose flip makes the lower boundary read +nonempty, and similarly for the upper end. The transfer to `Necessary r res k` is +`necessary_of_factor_toggle`, with the bridge's four factors supplying the +factorization. +-/ + +/-- A **lower joint base** for `k`: a database compatible with the full complete +result `⟨L, false⟩` (via the bridge) whose flip at `k` makes the lower boundary read +nonempty, i.e. breaks the lower factor. This is the section-7 witness that turns the +empty lower certificate into `Necessary r res k`. -/ +def LowerJointBase (r : RangeRead) (L : List Key) (k : Key) : Prop := + ∃ db₀ : DB, bridge r L db₀ ∧ + completeKeys (toggle db₀ k) (boundaryReadLo r (L.headD ([] : Key))) ≠ [] + +/-- An **upper joint base** for `k`: a database compatible with the full complete +result `⟨L, false⟩` whose flip at `k` makes the upper boundary read nonempty. -/ +def UpperJointBase (r : RangeRead) (L : List Key) (k : Key) : Prop := + ∃ db₀ : DB, bridge r L db₀ ∧ + completeKeys (toggle db₀ k) (boundaryReadHi r (L.getLastD ([] : Key))) ≠ [] + +/-- **Lower joint base gives necessity.** A lower joint base for `k` makes `k` +necessary for the full pair, by factor-toggle against the full result's bridge. -/ +theorem necessary_of_lowerJointBase {r : RangeRead} {res : Result} {L : List Key} + {k : Key} (hrev : r.reverse = false) (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) + (hlim : withinLimit r L) (hres : res = { keys := L, more := false }) + (h : LowerJointBase r L k) : Necessary r res k := by + have hcomp : ∀ db, RangeRead.Compatible db r res ↔ + (completeKeys db (boundaryReadLo r (L.headD ([] : Key))) = [] ∧ + (completeKeys db (boundaryReadHi r (L.getLastD ([] : Key))) = [] ∧ + midEq L db ∧ bracket r L db)) := by + intro db + rw [hres, compatible_complete_iff_bridge hrev hL hpair hlim] + unfold bridge + tauto + obtain ⟨db₀, hb, hflip⟩ := h + exact necessary_of_factor_toggle hcomp hb.1 hb.2 hflip + +/-- **Upper joint base gives necessity.** An upper joint base for `k` makes `k` +necessary for the full pair, by factor-toggle with the upper end factor first. -/ +theorem necessary_of_upperJointBase {r : RangeRead} {res : Result} {L : List Key} + {k : Key} (hrev : r.reverse = false) (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) + (hlim : withinLimit r L) (hres : res = { keys := L, more := false }) + (h : UpperJointBase r L k) : Necessary r res k := by + have hcomp : ∀ db, RangeRead.Compatible db r res ↔ + (completeKeys db (boundaryReadHi r (L.getLastD ([] : Key))) = [] ∧ + (completeKeys db (boundaryReadLo r (L.headD ([] : Key))) = [] ∧ + midEq L db ∧ bracket r L db)) := by + intro db + rw [hres, compatible_complete_iff_bridge hrev hL hpair hlim] + unfold bridge + tauto + obtain ⟨db₀, hb, hflip⟩ := h + exact necessary_of_factor_toggle hcomp hb.2.1 ⟨hb.1, hb.2.2⟩ hflip + +/-- A lower joint base for every key of `[a, b)`. -/ +def LowerJointBaseRange (r : RangeRead) (L : List Key) (a b : WithTop Key) : Prop := + ∀ k : Key, a ≤ (↑k : WithTop Key) → (↑k : WithTop Key) < b → LowerJointBase r L k + +/-- An upper joint base for every key of `[a, b)`. -/ +def UpperJointBaseRange (r : RangeRead) (L : List Key) (a b : WithTop Key) : Prop := + ∀ k : Key, a ≤ (↑k : WithTop Key) → (↑k : WithTop Key) < b → UpperJointBase r L k + +/-- The lower joint bases on `[a, b)` make that whole interval necessary. -/ +theorem necessaryRange_of_lowerJointBaseRange {r : RangeRead} {res : Result} + {L : List Key} {a b : WithTop Key} (hrev : r.reverse = false) (hL : L ≠ []) + (hpair : L.Pairwise (· < ·)) (hlim : withinLimit r L) + (hres : res = { keys := L, more := false }) (h : LowerJointBaseRange r L a b) : + NecessaryRange r res a b := + fun k hklo hkhi => necessary_of_lowerJointBase hrev hL hpair hlim hres (h k hklo hkhi) + +/-- The upper joint bases on `[a, b)` make that whole interval necessary. -/ +theorem necessaryRange_of_upperJointBaseRange {r : RangeRead} {res : Result} + {L : List Key} {a b : WithTop Key} (hrev : r.reverse = false) (hL : L ≠ []) + (hpair : L.Pairwise (· < ·)) (hlim : withinLimit r L) + (hres : res = { keys := L, more := false }) (h : UpperJointBaseRange r L a b) : + NecessaryRange r res a b := + fun k hklo hkhi => necessary_of_upperJointBase hrev hL hpair hlim hres (h k hklo hkhi) + +/-! +## Joint bases for a reversed read + +For a reversed read the bridge is stated on the ascending reading `L.reverse`, so the +boundary reads attach to `L`'s last and head keys rather than its head and last. These +are the reversal-symmetric twins of `LowerJointBase` / `UpperJointBase`, and they let the +reduction consume the empty theory's witnesses in either traversal order. +-/ + +/-- A **lower joint base for a reversed read**: a database satisfying the ascending +bridge of the reversed reading `L.reverse` whose flip at `k` makes the lower boundary +read `[resolve begin, k₀)` nonempty. The endpoint used is the bridge's own least key +`L.reverse.headD`, which is `L`'s last key. -/ +def LowerJointBaseRev (r : RangeRead) (L : List Key) (k : Key) : Prop := + ∃ db₀ : DB, bridge r L.reverse db₀ ∧ + completeKeys (toggle db₀ k) (boundaryReadLo r (L.reverse.headD ([] : Key))) ≠ [] + +/-- An **upper joint base for a reversed read**: a database satisfying the ascending +bridge of `L.reverse` whose flip at `k` makes the upper boundary read nonempty. -/ +def UpperJointBaseRev (r : RangeRead) (L : List Key) (k : Key) : Prop := + ∃ db₀ : DB, bridge r L.reverse db₀ ∧ + completeKeys (toggle db₀ k) (boundaryReadHi r (L.reverse.getLastD ([] : Key))) ≠ [] + +/-- A lower joint base for a reversed read, for every key of `[a, b)`. -/ +def LowerJointBaseRangeRev (r : RangeRead) (L : List Key) (a b : WithTop Key) : Prop := + ∀ k : Key, a ≤ (↑k : WithTop Key) → (↑k : WithTop Key) < b → LowerJointBaseRev r L k + +/-- An upper joint base for a reversed read, for every key of `[a, b)`. -/ +def UpperJointBaseRangeRev (r : RangeRead) (L : List Key) (a b : WithTop Key) : Prop := + ∀ k : Key, a ≤ (↑k : WithTop Key) → (↑k : WithTop Key) < b → UpperJointBaseRev r L k + +/-- **Lower joint base gives necessity, reversed.** A lower joint base for `k` makes `k` +necessary for the full pair, by factor-toggle against the reversed bridge. -/ +theorem necessary_of_lowerJointBaseRev {r : RangeRead} {res : Result} {L : List Key} + {k : Key} (hrev : r.reverse = true) (hL : L ≠ []) (hpair : L.Pairwise (· > ·)) + (hlim : withinLimit r L) (hres : res = { keys := L, more := false }) + (h : LowerJointBaseRev r L k) : Necessary r res k := by + have hcomp : ∀ db, RangeRead.Compatible db r res ↔ + (completeKeys db (boundaryReadLo r (L.reverse.headD ([] : Key))) = [] ∧ + (completeKeys db (boundaryReadHi r (L.reverse.getLastD ([] : Key))) = [] ∧ + midEq L.reverse db ∧ bracket r L.reverse db)) := by + intro db + rw [hres, compatible_complete_iff_bridge_rev hrev hL hpair hlim] + rfl + obtain ⟨db₀, hb, hflip⟩ := h + exact necessary_of_factor_toggle hcomp hb.1 hb.2 hflip + +/-- **Upper joint base gives necessity, reversed.** -/ +theorem necessary_of_upperJointBaseRev {r : RangeRead} {res : Result} {L : List Key} + {k : Key} (hrev : r.reverse = true) (hL : L ≠ []) (hpair : L.Pairwise (· > ·)) + (hlim : withinLimit r L) (hres : res = { keys := L, more := false }) + (h : UpperJointBaseRev r L k) : Necessary r res k := by + have hcomp : ∀ db, RangeRead.Compatible db r res ↔ + (completeKeys db (boundaryReadHi r (L.reverse.getLastD ([] : Key))) = [] ∧ + (completeKeys db (boundaryReadLo r (L.reverse.headD ([] : Key))) = [] ∧ + midEq L.reverse db ∧ bracket r L.reverse db)) := by + intro db + rw [hres, compatible_complete_iff_bridge_rev hrev hL hpair hlim] + exact bridge_swap + obtain ⟨db₀, hb, hflip⟩ := h + exact necessary_of_factor_toggle hcomp hb.2.1 ⟨hb.1, hb.2.2⟩ hflip + +/-- The lower joint bases on `[a, b)` make that interval necessary, reversed. -/ +theorem necessaryRange_of_lowerJointBaseRangeRev {r : RangeRead} {res : Result} + {L : List Key} {a b : WithTop Key} (hrev : r.reverse = true) (hL : L ≠ []) + (hpair : L.Pairwise (· > ·)) (hlim : withinLimit r L) + (hres : res = { keys := L, more := false }) (h : LowerJointBaseRangeRev r L a b) : + NecessaryRange r res a b := + fun k hklo hkhi => necessary_of_lowerJointBaseRev hrev hL hpair hlim hres (h k hklo hkhi) + +/-- The upper joint bases on `[a, b)` make that interval necessary, reversed. -/ +theorem necessaryRange_of_upperJointBaseRangeRev {r : RangeRead} {res : Result} + {L : List Key} {a b : WithTop Key} (hrev : r.reverse = true) (hL : L ≠ []) + (hpair : L.Pairwise (· > ·)) (hlim : withinLimit r L) + (hres : res = { keys := L, more := false }) (h : UpperJointBaseRangeRev r L a b) : + NecessaryRange r res a b := + fun k hklo hkhi => necessary_of_upperJointBaseRev hrev hL hpair hlim hres (h k hklo hkhi) + +/-- **Soundness factors across a compatibility equivalence.** If compatibility is a +conjunction `φ₁ ∧ φ₂` and agreement on `cr` fixes each factor separately, then `cr` +is sound. This is the soundness analogue of the factor-toggle lemma: the reduction's +soundness proof is exactly this shape, with `φ_lo`, `φ_hi`, `φ_mid`, and the +bracketing as the factors. -/ +theorem Sound.of_factors {r : RangeRead} {res : Result} {cr : ConflictRange} + {φ₁ φ₂ : DB → Prop} + (hcomp : ∀ db, RangeRead.Compatible db r res ↔ (φ₁ db ∧ φ₂ db)) + (h₁ : ∀ db₁ db₂, AgreeOn cr db₁ db₂ → (φ₁ db₁ ↔ φ₁ db₂)) + (h₂ : ∀ db₁ db₂, AgreeOn cr db₁ db₂ → (φ₂ db₁ ↔ φ₂ db₂)) : + Sound r res cr := by + intro db₁ db₂ hagree + rw [hcomp db₁, hcomp db₂, h₁ db₁ db₂ hagree, h₂ db₁ db₂ hagree] + +/-- **Soundness from four factors.** The bridge is a four-way conjunction (two end +emptinesses, the middle equality, and the bracketing); this is `Sound.of_factors` +iterated over them. -/ +theorem Sound.of_four_factors {r : RangeRead} {res : Result} {cr : ConflictRange} + {φ₁ φ₂ φ₃ φ₄ : DB → Prop} + (hcomp : ∀ db, RangeRead.Compatible db r res ↔ (φ₁ db ∧ φ₂ db ∧ φ₃ db ∧ φ₄ db)) + (h₁ : ∀ db₁ db₂, AgreeOn cr db₁ db₂ → (φ₁ db₁ ↔ φ₁ db₂)) + (h₂ : ∀ db₁ db₂, AgreeOn cr db₁ db₂ → (φ₂ db₁ ↔ φ₂ db₂)) + (h₃ : ∀ db₁ db₂, AgreeOn cr db₁ db₂ → (φ₃ db₁ ↔ φ₃ db₂)) + (h₄ : ∀ db₁ db₂, AgreeOn cr db₁ db₂ → (φ₄ db₁ ↔ φ₄ db₂)) : + Sound r res cr := by + intro db₁ db₂ hagree + rw [hcomp db₁, hcomp db₂, h₁ db₁ db₂ hagree, h₂ db₁ db₂ hagree, h₃ db₁ db₂ hagree, + h₄ db₁ db₂ hagree] + +/-! +## Interior necessity +-/ + +/-- **Interior necessity.** If every reported key of a complete result is necessary, +then so is every key between the least and the greatest reported key: a sound range +is an interval, so it contains the convex hull of the reported keys. -/ +theorem necessaryRange_between_reported {r : RangeRead} {res : Result} {L : List Key} + (hL : L ≠ []) (hkeys : ∀ k ∈ L, Necessary r res k) : + NecessaryRange r res (↑(L.headD ([] : Key)) : WithTop Key) + (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key) := by + intro k hlo hhi + refine Necessary.of_between (hkeys _ (headD_mem hL)) (hkeys _ (getLastD_mem hL)) + (WithTop.coe_le_coe.mp hlo) ?_ + rw [WithTop.coe_lt_coe] at hhi + exact (lt_keyAfter_iff k (L.getLastD ([] : Key))).mp hhi + +/-- The reported keys of a complete result are necessary. This is the pointwise +input to the interior necessity above. -/ +theorem necessary_of_mem_complete {r : RangeRead} {res : Result} + (hread : Readable r res) : ∀ k ∈ res.keys, Necessary r res k := + fun _ hk => necessary_of_mem_result hread hk + +/-! +## The hull of two end ranges + +The assembled range is the hull of the two end ranges, not in general the canonical +range. Agreement on the hull restricts to each end range, and the necessary keys of +the pieces cover the hull. +-/ + +/-- `ConflictRange.IsEmpty` is decidable, so the hull can branch on it. -/ +instance (cr : ConflictRange) : Decidable cr.IsEmpty := by + unfold ConflictRange.IsEmpty; infer_instance + +/-- The hull of two conflict ranges: their interval hull `[min begin, max end_)`, +except that an empty component is dropped. An empty range denotes `∅` whatever +representative is used, so dropping it keeps the hull's *set* independent of that +representation — in particular a degenerate boundary contribution at the wrong +endpoint cannot extend the result. -/ +def hull (cr₁ cr₂ : ConflictRange) : ConflictRange := + if cr₁.IsEmpty then cr₂ + else if cr₂.IsEmpty then cr₁ + else ⟨min cr₁.begin cr₂.begin, max cr₁.end_ cr₂.end_⟩ + +/-- Each end range is a subrange of the hull. -/ +theorem subrange_hull_left (cr₁ cr₂ : ConflictRange) : Subrange cr₁ (hull cr₁ cr₂) := by + unfold hull + by_cases h₁ : cr₁.IsEmpty + · rw [ite_eq_left h₁] + intro k hk + exact (ConflictRange.not_mem_of_isEmpty h₁ k hk).elim + · rw [ite_eq_right h₁] + by_cases h₂ : cr₂.IsEmpty + · rw [ite_eq_left h₂]; exact Subrange.refl _ + · rw [ite_eq_right h₂]; exact subrange_of_le (min_le_left _ _) (le_max_left _ _) + +/-- Each end range is a subrange of the hull. -/ +theorem subrange_hull_right (cr₁ cr₂ : ConflictRange) : Subrange cr₂ (hull cr₁ cr₂) := by + unfold hull + by_cases h₁ : cr₁.IsEmpty + · rw [ite_eq_left h₁]; exact Subrange.refl _ + · rw [ite_eq_right h₁] + by_cases h₂ : cr₂.IsEmpty + · rw [ite_eq_left h₂] + intro k hk + exact (ConflictRange.not_mem_of_isEmpty h₂ k hk).elim + · rw [ite_eq_right h₂]; exact subrange_of_le (min_le_right _ _) (le_max_right _ _) + +/-- Agreement on a range restricts to agreement on any subrange. -/ +theorem keys_eq_of_subrange_agree {cr' cr : ConflictRange} {db₁ db₂ : DB} + (hsub : Subrange cr' cr) (h : AgreeOn cr db₁ db₂) : AgreeOn cr' db₁ db₂ := by + apply eq_of_mem_iff_pairwise (ConflictRange.keys_pairwise cr' db₁) + (ConflictRange.keys_pairwise cr' db₂) + intro x + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff] + constructor + · rintro ⟨hx₁, hxcr'⟩ + have : x ∈ cr.keys db₂ := by + rw [← h] + exact ConflictRange.mem_keys_iff.mpr ⟨hx₁, hsub x hxcr'⟩ + exact ⟨(ConflictRange.mem_keys_iff.mp this).1, hxcr'⟩ + · rintro ⟨hx₂, hxcr'⟩ + have : x ∈ cr.keys db₁ := by + rw [h] + exact ConflictRange.mem_keys_iff.mpr ⟨hx₂, hsub x hxcr'⟩ + exact ⟨(ConflictRange.mem_keys_iff.mp this).1, hxcr'⟩ + +/-- Agreement on the hull restricts to agreement on either factor. -/ +theorem agreeOn_of_hull_left {cr₁ cr₂ : ConflictRange} {db₁ db₂ : DB} + (h : AgreeOn (hull cr₁ cr₂) db₁ db₂) : AgreeOn cr₁ db₁ db₂ := + keys_eq_of_subrange_agree (subrange_hull_left cr₁ cr₂) h + +/-- Agreement on the hull restricts to agreement on either factor. -/ +theorem agreeOn_of_hull_right {cr₁ cr₂ : ConflictRange} {db₁ db₂ : DB} + (h : AgreeOn (hull cr₁ cr₂) db₁ db₂) : AgreeOn cr₂ db₁ db₂ := + keys_eq_of_subrange_agree (subrange_hull_right cr₁ cr₂) h + +/-- **Conditional minimality of the hull.** If the hull is sound and every key in it +is necessary, it is minimal. This is the assembly step: the reduction's remaining +obligations are exactly these two hypotheses (soundness transfers through the +bracketing/observability argument, necessity through the end theorems and the +interior convexity). -/ +theorem minimal_hull_of_sound_of_necessaryRange {r : RangeRead} {res : Result} + {cr : ConflictRange} (hs : Sound r res cr) + (hnec : NecessaryRange r res cr.begin cr.end_) : Minimal r res cr := + minimal_iff_sound_and_necessaryRange.mpr ⟨hs, hnec⟩ + +/-! +## Gluing necessary intervals across the hull + +The reduction's necessity proof establishes a necessary range for each boundary read and +one for the middle interval; these abut, so the hull's necessary range is their +chain. `NecessaryRange.trans` is the convexity step at interval granularity. -/ + +/-- Necessary ranges glue at a common endpoint: `[a, b)` and `[b, c)` give `[a, c)`. -/ +theorem NecessaryRange.trans {r : RangeRead} {res : Result} {a b c : WithTop Key} + (h₁ : NecessaryRange r res a b) (h₂ : NecessaryRange r res b c) : + NecessaryRange r res a c := by + intro k hak hkc + rcases lt_or_ge (k : WithTop Key) b with hlt | hge + · exact h₁ k hak hlt + · exact h₂ k hge hkc + +/-- A necessary range restricts to any sub-interval. -/ +theorem NecessaryRange.mono {r : RangeRead} {res : Result} {a b a' b' : WithTop Key} + (h : NecessaryRange r res a b) (ha : a ≤ a') (hb : b' ≤ b) : + NecessaryRange r res a' b' := + fun k hklo hkhi => h k (le_trans ha hklo) (lt_of_lt_of_le hkhi hb) + +/-- **Necessary range of the hull.** If the lower end range `cr₁` is necessary, the +middle interval `[cr₁.end_, cr₂.begin)` is necessary, the upper end range `cr₂` is +necessary, and the intervals are ordered (`cr₁.begin ≤ cr₂.begin` and +`cr₁.end_ ≤ cr₂.end_`), then the hull `[cr₁.begin, cr₂.end_)` is necessary. This is +exactly section 7's assembly: everything below the reported keys is the lower end, +the interior is convexity, and everything above is the upper end. -/ +theorem necessaryRange_hull_of_chain {r : RangeRead} {res : Result} + {cr₁ cr₂ : ConflictRange} + (h₁ : NecessaryRange r res cr₁.begin cr₁.end_) + (h₂ : NecessaryRange r res cr₁.end_ cr₂.begin) + (h₃ : NecessaryRange r res cr₂.begin cr₂.end_) + (hmin : cr₁.begin ≤ cr₂.begin) (hmax : cr₁.end_ ≤ cr₂.end_) : + NecessaryRange r res (hull cr₁ cr₂).begin (hull cr₁ cr₂).end_ := by + unfold hull + by_cases h₁e : cr₁.IsEmpty + · rw [ite_eq_left h₁e]; exact h₃ + · rw [ite_eq_right h₁e] + by_cases h₂e : cr₂.IsEmpty + · rw [ite_eq_left h₂e]; exact h₁ + · rw [ite_eq_right h₂e] + have h : NecessaryRange r res cr₁.begin cr₂.end_ := (h₁.trans h₂).trans h₃ + have hb : min cr₁.begin cr₂.begin = cr₁.begin := min_eq_left hmin + have he : max cr₁.end_ cr₂.end_ = cr₂.end_ := max_eq_right hmax + rw [hb, he]; exact h + +/-- **Minimality of the hull from the chained end ranges.** Soundness of the hull +plus the necessary chain of the previous theorem give minimality by +`minimal_iff_sound_and_necessaryRange`. -/ +theorem minimal_hull_of_chain {r : RangeRead} {res : Result} {cr₁ cr₂ : ConflictRange} + (hs : Sound r res (hull cr₁ cr₂)) + (h₁ : NecessaryRange r res cr₁.begin cr₁.end_) + (h₂ : NecessaryRange r res cr₁.end_ cr₂.begin) + (h₃ : NecessaryRange r res cr₂.begin cr₂.end_) + (hmin : cr₁.begin ≤ cr₂.begin) (hmax : cr₁.end_ ≤ cr₂.end_) : + Minimal r res (hull cr₁ cr₂) := + minimal_hull_of_sound_of_necessaryRange hs (necessaryRange_hull_of_chain h₁ h₂ h₃ hmin hmax) + +/-- **The reduction as a hypothesis boundary.** Minimality of the two end ranges +*for the full pair* `(r, res)`, together with a necessary middle interval and +soundness of the hull, gives minimality of the hull. The end minimalities supply +the first and third needed ranges via `minimal_iff_sound_and_necessaryRange`. This +is the precise form in which "the whole empty theory" can be assumed: what is +needed is not merely that the end pairs have minimal ranges, but that those minimal +ranges are minimal *at the full pair* — which is exactly what the factor-toggle +joint construction is for. -/ +theorem minimal_hull_of_minimal_ends {r : RangeRead} {res : Result} + {cr₁ cr₂ : ConflictRange} + (hlo : Minimal r res cr₁) (hhi : Minimal r res cr₂) + (hmid : NecessaryRange r res cr₁.end_ cr₂.begin) + (hmin : cr₁.begin ≤ cr₂.begin) (hmax : cr₁.end_ ≤ cr₂.end_) + (hs : Sound r res (hull cr₁ cr₂)) : + Minimal r res (hull cr₁ cr₂) := + minimal_hull_of_chain hs (minimal_iff_sound_and_necessaryRange.mp hlo).2 hmid + (minimal_iff_sound_and_necessaryRange.mp hhi).2 hmin hmax + +/-- **Necessity of the hull from the joint bases.** With the lower joint bases below +the reported keys, the interior necessary by convexity, and the upper joint bases +above, the hull's necessary range is assembled. This is section 7's necessity +assembly: the ends via factor-toggle joint witnesses, the middle free. -/ +theorem necessaryRange_hull_of_jointBases {r : RangeRead} {res : Result} {L : List Key} + {cr₁ cr₂ : ConflictRange} (hrev : r.reverse = false) (hL : L ≠ []) + (hpair : L.Pairwise (· < ·)) (hlim : withinLimit r L) + (hres : res = { keys := L, more := false }) + (hlo : LowerJointBaseRange r L cr₁.begin cr₁.end_) + (hmid : NecessaryRange r res cr₁.end_ cr₂.begin) + (hhi : UpperJointBaseRange r L cr₂.begin cr₂.end_) + (hmin : cr₁.begin ≤ cr₂.begin) (hmax : cr₁.end_ ≤ cr₂.end_) : + NecessaryRange r res (hull cr₁ cr₂).begin (hull cr₁ cr₂).end_ := + necessaryRange_hull_of_chain + (necessaryRange_of_lowerJointBaseRange hrev hL hpair hlim hres hlo) + hmid (necessaryRange_of_upperJointBaseRange hrev hL hpair hlim hres hhi) hmin hmax + +/-- **Minimality of the hull from the joint bases and hull soundness.** This is the +end-to-end shape of the section-7 necessity argument: the end ranges are established +by the joint bases (factor-toggle), the interior by convexity over the reported keys +(`necessaryRange_between_reported`), and the only remaining hypothesis is soundness +of the hull — the soundness half of the reduction. -/ +theorem minimal_hull_of_jointBases {r : RangeRead} {res : Result} {L : List Key} + {cr₁ cr₂ : ConflictRange} (hread : Readable r res) (hrev : r.reverse = false) + (hL : L ≠ []) (hpair : L.Pairwise (· < ·)) (hlim : withinLimit r L) + (hres : res = { keys := L, more := false }) + (hlo : LowerJointBaseRange r L cr₁.begin cr₁.end_) + (hhi : UpperJointBaseRange r L cr₂.begin cr₂.end_) + (hk₀ : cr₁.end_ = (↑(L.headD ([] : Key)) : WithTop Key)) + (hkₙ : cr₂.begin = (↑(keyAfter (L.getLastD ([] : Key))) : WithTop Key)) + (hmin : cr₁.begin ≤ cr₂.begin) (hmax : cr₁.end_ ≤ cr₂.end_) + (hs : Sound r res (hull cr₁ cr₂)) : + Minimal r res (hull cr₁ cr₂) := by + refine minimal_hull_of_chain hs + (necessaryRange_of_lowerJointBaseRange hrev hL hpair hlim hres hlo) + ?_ (necessaryRange_of_upperJointBaseRange hrev hL hpair hlim hres hhi) hmin hmax + rw [hk₀, hkₙ] + have hkeys : ∀ k ∈ L, Necessary r res k := by + intro k hk + have hkres : k ∈ res.keys := by rw [hres]; exact hk + exact necessary_of_mem_result hread hkres + exact necessaryRange_between_reported hL hkeys + +end FormalConflictRange diff --git a/FormalConflictRange/Residual.lean b/FormalConflictRange/Residual.lean new file mode 100644 index 0000000..54c3479 --- /dev/null +++ b/FormalConflictRange/Residual.lean @@ -0,0 +1,1437 @@ +/- +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.Empty +import FormalConflictRange.General +import FormalConflictRange.ComputableAlgorithm + +/-! +# The residual complete-result cell + +The complete-result closed form has one cross-free cell not given by the earlier +fixed offset rules: `begin.offset ≥ 2`, `end.offset ≥ 2`, `0 < t < e`, a reported key +at or above `end.key`, and `begin.key < end.key`. This file closes it with the +corrected capacity criterion. Writing `b = begin.offset - 1`, `e = end.offset - 1`, +`t` for the reported keys at or above `end.key`, and `g = intervalCard begin.key +end.key`, the minimal range is the truncated range when `(b - g) + t = e` and the top +range when `(b - g) + t < e`. + +The finite branch rests on the exact count identity +`completeKeys_count_ge_eq_general`: the number of complete keys at or above +`end.key` is `min e A_E - (b - N)`, where `N` is the number of stored keys in +`[begin.key, end.key)`. Since `N ≤ g`, the finite-branch identity forces `N = g` +and `e ≤ A_E`: every compatible database fills the whole selector gap. Then the +complete window is the first `e` stored keys at or above `end.key`, shifted past +`b - g` of them, and agreement on `[begin.key, keyAfter last)` fixes exactly that +window. + +The top branch is witnessed by a compatible database that prepends `b` keys below +the reported head to the reported keys, so its resolved end is `⊤` while its stored +count stays strictly below `c_E + e`. Every key above the reported last is then +necessary, since inserting it breaks compatibility. `residual_dt_le_e` proves the +two branches exhaustive, and `closedFormResidual` assembles the rule. +-/ + +namespace FormalConflictRange + +/-- The begin overshoot `b = begin.offset - 1`. -/ +def residualB (r : RangeRead) : ℕ := (r.begin.offset - 1).toNat + +/-- The end offset `e = end.offset - 1`. -/ +def residualE (r : RangeRead) : ℕ := (r.end_.offset - 1).toNat + +/-- The number `t` of reported keys at or above `end.key`. -/ +def residualT (r : RangeRead) (res : Result) : ℕ := + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + +/-- **The finite branch fills the selector gap.** In a compatible database of the +residual cell, if `(b - g) + t = e` with `0 < t < e` and `intervalCard begin.key +end.key = g`, then the database stores all `g` keys of the gap and at least `e` keys +at or above `end.key`, and the begin overshoot exceeds the gap (`g < b`). -/ +theorem residual_finite_counts {db : DB} {r : RangeRead} {res : Result} {g : ℕ} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) + (hdt : (residualB r - g) + residualT r res = residualE r) : + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = g ∧ + g < residualB r ∧ + residualE r ≤ db.keys.length - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := residualB r with hb + set e := residualE r with he + set t := residualT r res with ht + set N := cE - p with hN + set A := l.length - cE with hA + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hcount := completeKeys_count_ge_eq_general (db := db) (r := r) hrev hbegin hcross hend + rw [← hres] at hcount + have hbto : b = r.begin.offset.toNat - 1 := by + rw [hb, residualB]; exact toNat_sub_one hbegin + have heto : e = r.end_.offset.toNat - 1 := by + rw [he, residualE]; exact toNat_sub_one hend + have hcount' : t = min e A - (b - N) := by + rw [hbto, heto] + simpa [residualT, l, p, cE, t, N, A, ht, hl, hp, hcE, hN, hA] using hcount + have hNle : N ≤ g := by + have h := cnt_sub_le_intervalCard (db := db) (w₁ := r.begin.key) (w₂ := r.end_.key) hcross + rw [hcard] at h + have hcast := ENat.natCast_le_natCast.mp h + simpa [N, p, cE, cnt, hl, hp, hcE, hN] using hcast + have hbg : g < b := by + have : 0 < b - g := by omega + omega + have hbNpos : 0 < b - N := by omega + have hmin : min e A = e := by + have hminge : b - N ≤ min e A := by + by_contra hlt + have hzero : min e A - (b - N) = 0 := Nat.sub_eq_zero_of_le (le_of_not_ge hlt) + omega + have hbge : b - g ≤ b - N := Nat.sub_le_sub_left hNle b + omega + refine ⟨?_, hbg, by omega⟩ + omega + +/-! ### The finite-branch window and its list decompositions + +The complete window of a compatible database in the finite branch is the first `e` +stored keys at or above `end.key`, with the begin overshoot `b - g` dropped. To +compare two databases that agree on `[begin.key, keyAfter last)`, split the stored +keys of that range at `end.key` and split the keys at or above `end.key` at the +reported last. Both decompositions are proved by sortedness and membership. -/ + +private def rD (r : RangeRead) (last : Key) (db : DB) : List Key := + (truncatedRange r last).keys db + +private def rG (r : RangeRead) (db : DB) : List Key := + db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.end_.key)) + +private def rA (r : RangeRead) (last : Key) (db : DB) : List Key := + db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧ + (k : Key) ≤ last)) + +private def rW (r : RangeRead) (db : DB) : List Key := + db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + +private def rAbove (last : Key) (db : DB) : List Key := + db.keys.filter (fun k : Key => decide (last < k)) + +private theorem mem_rD {r : RangeRead} {last : Key} {db : DB} {k : Key} : + k ∈ rD r last db ↔ + k ∈ db.keys ∧ r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) ≤ ↑last := by + rw [rD, truncatedRange_keys_eq_filter_le, List.mem_filter, List.mem_filter, + decide_eq_true_eq, decide_eq_true_eq, WithTop.coe_le_coe] + tauto + +private theorem mem_rG {r : RangeRead} {db : DB} {k : Key} : + k ∈ rG r db ↔ + k ∈ db.keys ∧ r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key := by + simp [rG] + +private theorem mem_rA {r : RangeRead} {last : Key} {db : DB} {k : Key} : + k ∈ rA r last db ↔ + k ∈ db.keys ∧ r.end_.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) ≤ ↑last := by + simp [rA] + +private theorem mem_rW {r : RangeRead} {db : DB} {k : Key} : + k ∈ rW r db ↔ k ∈ db.keys ∧ r.end_.key ≤ (k : WithTop Key) := by + simp [rW] + +private theorem mem_rAbove {last : Key} {db : DB} {k : Key} : + k ∈ rAbove last db ↔ k ∈ db.keys ∧ ↑last < (k : WithTop Key) := by + simp [rAbove] + +private theorem sorted_rD {r : RangeRead} {last : Key} {db : DB} : + (rD r last db).SortedLT := by + rw [rD, truncatedRange_keys_eq_filter_le] + exact List.sortedLT_iff_pairwise.mpr ((db.sorted.pairwise.filter _).filter _) + +private theorem sorted_rG {r : RangeRead} {db : DB} : (rG r db).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + +private theorem sorted_rA {r : RangeRead} {last : Key} {db : DB} : + (rA r last db).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + +private theorem sorted_rW {r : RangeRead} {db : DB} : (rW r db).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + +private theorem sorted_rAbove {last : Key} {db : DB} : (rAbove last db).SortedLT := + List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + +private theorem rD_eq_rG_append_rA {r : RangeRead} {last : Key} {db : DB} + (hBE : r.begin.key ≤ r.end_.key) (hElast : r.end_.key ≤ (↑last : WithTop Key)) : + rD r last db = rG r db ++ rA r last db := by + refine List.SortedLT.eq_of_mem_iff (sorted_rD) ?_ ?_ + · rw [List.sortedLT_append] + refine ⟨sorted_rG, sorted_rA, ?_⟩ + intro x hx y hy + have hx' := (mem_rG.mp hx).2.2 + have hy' := (mem_rA.mp hy).2.1 + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hx' hy') + · intro k + rw [mem_rD, List.mem_append, mem_rG, mem_rA] + constructor + · rintro ⟨hmem, hB, hklast⟩ + by_cases hk : (k : WithTop Key) < r.end_.key + · exact Or.inl ⟨hmem, hB, hk⟩ + · exact Or.inr ⟨hmem, le_of_not_gt hk, hklast⟩ + · rintro (⟨hmem, hB, hk⟩ | ⟨hmem, hE, hklast⟩) + · exact ⟨hmem, hB, le_trans (le_of_lt hk) hElast⟩ + · exact ⟨hmem, le_trans hBE hE, hklast⟩ + +private theorem rW_eq_rA_append_rAbove {r : RangeRead} {last : Key} {db : DB} + (hElast : r.end_.key ≤ (↑last : WithTop Key)) : + rW r db = rA r last db ++ rAbove last db := by + refine List.SortedLT.eq_of_mem_iff (sorted_rW) ?_ ?_ + · rw [List.sortedLT_append] + refine ⟨sorted_rA, sorted_rAbove, ?_⟩ + intro x hx y hy + have hx' := (mem_rA.mp hx).2.2 + have hy' := (mem_rAbove.mp hy).2 + exact WithTop.coe_lt_coe.mp (lt_of_le_of_lt hx' hy') + · intro k + rw [mem_rW, List.mem_append, mem_rA, mem_rAbove] + constructor + · rintro ⟨hmem, hE⟩ + by_cases hk : (k : WithTop Key) ≤ ↑last + · exact Or.inl ⟨hmem, hE, hk⟩ + · exact Or.inr ⟨hmem, lt_of_not_ge hk⟩ + · rintro (⟨hmem, hE, -⟩ | ⟨hmem, hlt⟩) + · exact ⟨hmem, hE⟩ + · exact ⟨hmem, le_trans hElast (le_of_lt hlt)⟩ + +/-- **The finite-branch window.** In the finite branch the complete keys of any +compatible database are the first `e` stored keys at or above `end.key`, with the +begin overshoot `b - g` dropped. -/ +theorem residual_window {db : DB} {r : RangeRead} {res : Result} {g : ℕ} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) + (hdt : (residualB r - g) + residualT r res = residualE r) : + RangeRead.completeKeys db r = + ((rW r db).take (residualE r)).drop (residualB r - g) := by + obtain ⟨hN, hbg, hAle⟩ := + residual_finite_counts hcomp hmore hrev hbegin hcross hend hcard htpos htlt hdt + have hbN : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) ≤ + (r.begin.offset - 1).toNat := by + rw [show (r.begin.offset - 1).toNat = residualB r from rfl] + calc db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = g := hN + _ ≤ residualB r := le_of_lt hbg + have hAle' : (r.end_.offset - 1).toNat ≤ db.keys.length - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := hAle + have h := completeKeys_eq_above_take_drop (db := db) (r := r) hrev hbegin hcross hend hbN hAle' + simpa [rW, residualB, residualE, hN] using h + +/-- The reported last is at or above `end.key` whenever some reported key is. -/ +theorem last_ge_end_of_count_pos {r : RangeRead} {res : Result} + (hsort : res.keys.SortedLT) (hne : res.keys ≠ []) + (htpos : 0 < res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))) : + r.end_.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + obtain ⟨k, hk, hkge⟩ := List.countP_pos_iff.mp htpos + rw [decide_eq_true_eq] at hkge + exact le_trans hkge (WithTop.coe_le_coe.mpr + ((hsort.pairwise.imp le_of_lt).rel_getLast hk)) + +/-- The keys in `[begin.key, end.key)` number the difference of the counts below the +two endpoints. -/ +private theorem rG_length_eq_cnt {r : RangeRead} {db : DB} + (hBE : r.begin.key ≤ r.end_.key) : + (rG r db).length = cnt db r.end_.key - cnt db r.begin.key := by + have h := cnt_eq_add_between db hBE + have hr : rG r db = between db r.begin.key r.end_.key := rfl + rw [hr] + omega + +/-- The keys in `[end.key, last]` number `e` in the finite branch. The complete +window is the reported result; filtering it at or below the reported last removes +nothing, and the general drop identity turns that filter into the truncated range +with the begin overshoot removed. Splitting the truncated range at `end.key` leaves +`[end.key, last]`, whose length is `e`. -/ +private theorem rA_length_eq {db : DB} {r : RangeRead} {res : Result} {g : ℕ} {last : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (hlast : last = res.keys.getLast hne) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) + (hdt : (residualB r - g) + residualT r res = residualE r) : + (rA r last db).length = residualE r := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hlast_mem : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hmemck : last ∈ RangeRead.completeKeys db r := by + rw [← hres]; exact hlast_mem + have hlr : (last : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hmemck).2.2 + have hfilt : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [← hres, hlast] + exact filter_le_getLast_eq_of_prefix hsort.pairwise (List.prefix_refl _) hne + have hdrop := completeKeys_filter_le_drop (db := db) (r := r) (last := last) + hrev hbegin hlr + rw [hfilt] at hdrop + obtain ⟨hN, hbg, hAle⟩ := + residual_finite_counts hcomp hmore hrev hbegin hcross hend hcard htpos htlt hdt + have hElast : r.end_.key ≤ (↑last : WithTop Key) := by + rw [hlast] + exact last_ge_end_of_count_pos hsort hne htpos + have hGg : (rG r db).length = g := by + rw [rG_length_eq_cnt hcross] + simpa [cnt] using hN + have hDdrop : (rD r last db).drop (residualB r) = res.keys := by + rw [rD] + simpa [residualB] using hdrop.symm + have hD := rD_eq_rG_append_rA (r := r) (last := last) (db := db) hcross hElast + have hb_eq : residualB r = g + (residualB r - g) := by omega + have hA_drop : (rA r last db).drop (residualB r - g) = res.keys := by + have hstep : (rD r last db).drop (residualB r) = + ((rG r db) ++ (rA r last db)).drop (g + (residualB r - g)) := by + conv_lhs => + rw [hD] + rw [hb_eq] + rw [hstep, List.drop_append] at hDdrop + rw [List.drop_eq_nil_of_le (by omega : (rG r db).length ≤ g + (residualB r - g)), + List.nil_append] at hDdrop + have hsub : g + (residualB r - g) - (rG r db).length = residualB r - g := by + rw [hGg]; omega + rwa [hsub] at hDdrop + have hresmem : ∀ k ∈ res.keys, r.end_.key ≤ (k : WithTop Key) := by + intro k hk + have hkA : k ∈ rA r last db := by + apply List.mem_of_mem_drop + rw [hA_drop] + exact hk + exact (mem_rA.mp hkA).2.1 + have htlen : residualT r res = res.keys.length := by + rw [residualT] + exact List.countP_eq_length.mpr fun k hk => by + rw [decide_eq_true_eq]; exact hresmem k hk + have hlen := congrArg List.length hA_drop + rw [List.length_drop] at hlen + omega + +/-- `[begin.key, end.key)` is the part of `[begin.key, last]` below `end.key`, when +`end.key ≤ last`. -/ +private theorem rG_eq_rD_filter_lt {r : RangeRead} {last : Key} {db : DB} + (hElast : r.end_.key ≤ (↑last : WithTop Key)) : + rG r db = (rD r last db).filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + refine List.SortedLT.eq_of_mem_iff sorted_rG ?_ ?_ + · exact List.sortedLT_iff_pairwise.mpr (sorted_rD.pairwise.filter _) + · intro k + rw [mem_rG, List.mem_filter, mem_rD] + constructor + · rintro ⟨hmem, hB, hkE⟩ + exact ⟨⟨hmem, hB, le_trans (le_of_lt hkE) hElast⟩, + by rw [decide_eq_true_eq]; exact hkE⟩ + · rintro ⟨⟨hmem, hB, -⟩, hkE⟩ + exact ⟨hmem, hB, of_decide_eq_true hkE⟩ + +/-- `[end.key, last]` is the part of `[begin.key, last]` at or above `end.key`, when +`begin.key ≤ end.key`. -/ +private theorem rA_eq_rD_filter_ge {r : RangeRead} {last : Key} {db : DB} + (hBE : r.begin.key ≤ r.end_.key) : + rA r last db = (rD r last db).filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + refine List.SortedLT.eq_of_mem_iff sorted_rA ?_ ?_ + · exact List.sortedLT_iff_pairwise.mpr (sorted_rD.pairwise.filter _) + · intro k + rw [mem_rA, List.mem_filter, mem_rD] + constructor + · rintro ⟨hmem, hE, hklast⟩ + exact ⟨⟨hmem, le_trans hBE hE, hklast⟩, + by rw [decide_eq_true_eq]; exact hE⟩ + · rintro ⟨⟨hmem, -, hklast⟩, hE⟩ + exact ⟨hmem, of_decide_eq_true hE, hklast⟩ + +/-- **Finite-branch completeness from agreement.** If a database agrees on the +truncated range `[begin.key, keyAfter last)` with a finite-branch compatible +database, then it is itself complete: the shared gap `[begin.key, end.key)` fills +the begin overshoot, and the first `e` stored keys at or above `end.key` reach the +reported last. -/ +private theorem completeKeys_eq_of_truncated_eq_finite {db₁ db₂ : DB} {r : RangeRead} + {res : Result} {g : ℕ} {last : Key} + (hcomp : RangeRead.Compatible db₁ r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (hlast : last = res.keys.getLast hne) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) + (hdt : (residualB r - g) + residualT r res = residualE r) + (hT : (truncatedRange r last).keys db₂ = (truncatedRange r last).keys db₁) : + RangeRead.completeKeys db₂ r = res.keys := by + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + obtain ⟨hN₁, hbg, hAle₁⟩ := + residual_finite_counts hcomp hmore hrev hbegin hcross hend hcard htpos htlt hdt + have hElast : r.end_.key ≤ (↑last : WithTop Key) := by + rw [hlast] + exact last_ge_end_of_count_pos hsort hne htpos + have hres₁ : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + -- Agreement splits into the gap and the above-`end` parts. + have hT' : rD r last db₂ = rD r last db₁ := by simpa [rD] using hT + have hG₂ : rG r db₂ = rG r db₁ := by + rw [rG_eq_rD_filter_lt (db := db₂) hElast, + rG_eq_rD_filter_lt (db := db₁) hElast, hT'] + have hA₂ : rA r last db₂ = rA r last db₁ := by + rw [rA_eq_rD_filter_ge (db := db₂) hcross, + rA_eq_rD_filter_ge (db := db₁) hcross, hT'] + have hGlen₂ : (rG r db₂).length = g := by + rw [hG₂, rG_length_eq_cnt (db := db₁) hcross] + simpa [cnt] using hN₁ + have hAlen₁ : (rA r last db₁).length = residualE r := + rA_length_eq hcomp hmore hrev hne hbegin hcross hend hcard hlast htpos htlt hdt + have hAlen₂ : (rA r last db₂).length = residualE r := by rw [hA₂, hAlen₁] + -- The reference database's window. + have hW₁ := residual_window (db := db₁) hcomp hmore hrev hbegin hcross hend hcard + htpos htlt hdt + have hW₁dec := rW_eq_rA_append_rAbove (r := r) (last := last) (db := db₁) hElast + have htake₁ : (rW r db₁).take (residualE r) = rA r last db₁ := by + rw [hW₁dec, List.take_append_of_le_length (le_of_eq hAlen₁.symm), + List.take_of_length_le (le_of_eq hAlen₁)] + have hresA : (rA r last db₁).drop (residualB r - g) = res.keys := by + rw [← htake₁, ← hW₁, ← hres₁] + -- The shared decomposition supplies the count bounds for `db₂`. + have hgap₂ : db₂.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db₂.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = g := by + have h := rG_length_eq_cnt (db := db₂) hcross + rw [← hGlen₂] + simpa [cnt] using h.symm + have hbN₂ : db₂.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db₂.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) ≤ + (r.begin.offset - 1).toNat := by + rw [hgap₂] + exact le_of_lt hbg + have hAle₂ : (r.end_.offset - 1).toNat ≤ db₂.keys.length - + db₂.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + have h := cnt_add_above_length db₂ r.end_.key + have hsub : db₂.keys.length - + db₂.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + (above db₂ r.end_.key).length := by + have hcnt : db₂.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = cnt db₂ r.end_.key := rfl + omega + rw [hsub] + have hdec := rW_eq_rA_append_rAbove (r := r) (last := last) (db := db₂) hElast + have : above db₂ r.end_.key = rW r db₂ := rfl + rw [this, hdec, List.length_append] + have heq : (r.end_.offset - 1).toNat = residualE r := rfl + rw [heq, hAlen₂] + omega + -- The general window formula plus the shared gap/`end` split. + have hformula := completeKeys_eq_above_take_drop (db := db₂) (r := r) + hrev hbegin hcross hend hbN₂ hAle₂ + rw [hgap₂] at hformula + have hrW₂ : db₂.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + rW r db₂ := rfl + rw [hrW₂] at hformula + have hW₂dec := rW_eq_rA_append_rAbove (r := r) (last := last) (db := db₂) hElast + have htake₂ : (rW r db₂).take (residualE r) = rA r last db₂ := by + rw [hW₂dec, List.take_append_of_le_length (le_of_eq hAlen₂.symm), + List.take_of_length_le (le_of_eq hAlen₂)] + change RangeRead.completeKeys db₂ r = + ((rW r db₂).take (residualE r)).drop (residualB r - g) at hformula + rw [htake₂, hA₂, hresA] at hformula + exact hformula + +/-- **Finite-branch soundness in the residual cell.** Agreement on +`[begin.key, keyAfter last)` fixes compatibility for a complete result whose finite +branch is `(b - g) + t = e`. -/ +theorem sound_truncatedRange_of_residual_finite {r : RangeRead} {res : Result} {g : ℕ} + (hmore : res.more = false) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) + (hdt : (residualB r - g) + residualT r res = residualE r) : + Sound r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + have hne : res.keys ≠ [] := by + intro hnil + rw [residualT, hnil] at htpos + simp at htpos + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + intro db₁ db₂ hagree + have hcomp : ∀ db : DB, RangeRead.Compatible db r res → + RangeRead.withinLimit r res.keys ∧ RangeRead.completeKeys db r = res.keys := by + intro db hc + refine ⟨hc.1, ?_⟩ + rcases hc.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq.symm + constructor + · intro hc₁ + obtain ⟨hlim, hck₁⟩ := hcomp db₁ hc₁ + have hT : (truncatedRange r (res.keys.getLast hne)).keys db₂ = + (truncatedRange r (res.keys.getLast hne)).keys db₁ := hagree.symm + have hck₂ := completeKeys_eq_of_truncated_eq_finite + (db₁ := db₁) (db₂ := db₂) (g := g) + ⟨hlim, Or.inr ⟨hmore, hck₁.symm⟩⟩ hmore hrev hne hbegin hcross hend hcard rfl + htpos htlt hdt hT + exact ⟨hlim, Or.inr ⟨hmore, hck₂.symm⟩⟩ + · intro hc₂ + obtain ⟨hlim, hck₂⟩ := hcomp db₂ hc₂ + have hT : (truncatedRange r (res.keys.getLast hne)).keys db₁ = + (truncatedRange r (res.keys.getLast hne)).keys db₂ := hagree + have hck₁ := completeKeys_eq_of_truncated_eq_finite + (db₁ := db₂) (db₂ := db₁) (g := g) + ⟨hlim, Or.inr ⟨hmore, hck₂.symm⟩⟩ hmore hrev hne hbegin hcross hend hcard rfl + htpos htlt hdt hT + exact ⟨hlim, Or.inr ⟨hmore, hck₁.symm⟩⟩ + +/-- **Finite-branch minimality in the residual cell.** The truncated range +`[begin.key, keyAfter last)` is minimal for a complete result in the finite branch +`(b - g) + t = e`. -/ +theorem minimal_truncatedRange_of_residual_finite {r : RangeRead} {res : Result} {g : ℕ} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) + (hdt : (residualB r - g) + residualT r res = residualE r) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hne : res.keys ≠ [] := by + intro hnil + rw [residualT, hnil] at htpos + simp at htpos + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · rw [← hgetLast] + exact sound_truncatedRange_of_residual_finite hmore hrev hbegin hcross hend hcard + htpos htlt hdt + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + have hBtop : r.begin.key ≠ (⊤ : WithTop Key) := by + obtain ⟨m, _, hmge⟩ := List.countP_pos_iff.mp htpos + rw [decide_eq_true_eq] at hmge + intro htop + rw [htop] at hcross + exact absurd (le_trans hcross hmge) (not_le_of_gt (WithTop.coe_lt_top m)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hBtop + have hvB : r.begin.key = (↑v : WithTop Key) := hv.symm + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp₀⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hvB + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hvB ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + + +private theorem residual_between_begin_head {db₀ : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db₀ r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) : + (between db₀ r.begin.key ((res.keys.head hne : Key) : WithTop Key)).length = + (r.begin.offset - 1).toNat := by + set H : Key := res.keys.head hne with hH + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hneCK : RangeRead.completeKeys db₀ r ≠ [] := by rw [← hres]; exact hne + have hpos : 0 ≤ r.begin.anchor db₀ := anchor_nonneg_of_one_le hbegin + obtain ⟨a, ha_mem, ha_res, ha_head⟩ := + RangeRead.completeKeys_head_resolve (db := db₀) (r := r) hrev hneCK hpos + have hHhead? : res.keys.head? = some H := by + rw [hH, List.head?_eq_some_head hne] + have hahead? : (RangeRead.completeKeys db₀ r).head? = some a := by + rw [← ha_head, List.head?_eq_some_head hneCK] + have hsome : some H = some a := by + rw [← hHhead?, ← hahead?] + exact congrArg List.head? hres + have ha : a = H := (Option.some.inj hsome.symm) + have hresolveB : resolve db₀ r.begin = (↑H : WithTop Key) := by + rw [← ha, ha_res] + -- anchor < length + have hlt : (r.begin.anchor db₀).toNat < db₀.keys.length := by + by_contra h + have htop : resolve db₀ r.begin = (⊤ : WithTop Key) := + resolve_eq_top hpos (le_of_not_gt h) + rw [htop] at hresolveB + exact absurd hresolveB.symm (by simp) + have hget : db₀.keys[(r.begin.anchor db₀).toNat] = H := by + have h := resolve_eq_getElem (db := db₀) (s := r.begin) hpos hlt + rw [h] at hresolveB + exact WithTop.coe_eq_coe.mp hresolveB + have hcnt : cnt db₀ (↑H : WithTop Key) = (r.begin.anchor db₀).toNat := by + rw [cnt_coe] + exact countP_lt_eq_of_getElem db₀.sorted hlt hget + have hanchor : (r.begin.anchor db₀).toNat = + cnt db₀ r.begin.key + (r.begin.offset - 1).toNat := by + rw [anchor_toNat_eq hbegin] + rfl + have hBH : r.begin.key ≤ (↑H : WithTop Key) := by + have h := begin_key_le_head_of_one_le_offset (r := r) (res := res) ⟨db₀, hcomp⟩ hne hbegin + rwa [hH] + have hadd := cnt_eq_add_between db₀ hBH + omega + + +private theorem residual_topWitness_of_D {db₀ : DB} {r : RangeRead} {res : Result} {g n : ℕ} {H : Key} + (hcomp : RangeRead.Compatible db₀ r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hHle : ∀ k ∈ res.keys, (↑H : WithTop Key) ≤ (↑k : WithTop Key)) + (hBH : r.begin.key ≤ (↑H : WithTop Key)) + (D : List Key) + (hDlen : D.length = residualB r) + (hDsorted : D.SortedLT) + (hDB : ∀ k ∈ D, r.begin.key ≤ (↑k : WithTop Key)) + (hDH : ∀ k ∈ D, (↑k : WithTop Key) < (↑H : WithTop Key)) + (hDcntE : (D.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).length = n) + (hbn : residualB r - n = residualB r - g) + (htop : (residualB r - g) + residualT r res < residualE r) : + ∃ w : DB, RangeRead.Compatible w r res ∧ + (∀ k ∈ w.keys, ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key)) ∧ + resolve w r.end_ = (⊤ : WithTop Key) ∧ + w.keys.length < w.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + have hRsorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hwsorted : (D ++ res.keys).SortedLT := by + rw [List.sortedLT_append] + refine ⟨hDsorted, hRsorted, ?_⟩ + intro a ha b hb + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hDH a ha) (hHle b hb)) + let w : DB := { keys := D ++ res.keys, sorted := hwsorted } + have hwkeys : w.keys = D ++ res.keys := rfl + have hDBw : ∀ k ∈ w.keys, r.begin.key ≤ (↑k : WithTop Key) := by + intro k hk + rw [hwkeys] at hk + rcases List.mem_append.mp hk with hkD | hkR + · exact hDB k hkD + · exact le_trans hBH (hHle k hkR) + have htopKeys : (topRange r).keys w = w.keys := by + unfold topRange ConflictRange.keys + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact ⟨hDBw k hk, WithTop.coe_lt_top k⟩ + have hwlen : w.keys.length = residualB r + res.keys.length := by + rw [hwkeys, List.length_append, hDlen] + have hwcnt : w.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + n + res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [hwkeys, List.countP_append] + have hDcnt : D.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = n := by + rw [List.countP_eq_length_filter, hDcntE] + rw [hDcnt] + have hres_split : res.keys.length = + (res.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).length + + residualT r res := by + have h := List.length_eq_countP_add_countP (l := res.keys) + (p := fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + have hc : List.countP + (fun a : Key => decide ¬(decide (r.end_.key ≤ (a : WithTop Key))) = true) res.keys + = res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + apply List.countP_congr + intro k hk + simp only [decide_eq_true_eq] + exact not_le + rw [hc] at h + have h2 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + (res.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).length := + List.countP_eq_length_filter + rw [h2] at h + rw [residualT] + omega + have hlen_lt : w.keys.length < w.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + rw [hwlen, hwcnt, List.countP_eq_length_filter] + rw [hres_split] + have hee : residualE r = (r.end_.offset - 1).toNat := rfl + rw [← hee] + omega + have hEtop : resolve w r.end_ = (⊤ : WithTop Key) := by + refine resolve_eq_top (anchor_nonneg_of_one_le (by omega)) ?_ + rw [anchor_toNat_eq (s := r.end_) (by omega)] + omega + have hckW : RangeRead.completeKeys w r = res.keys := by + rw [completeKeys_eq_topRange_keys_drop (db := w) (r := r) hrev hbegin hEtop, htopKeys] + rw [hwkeys] + change List.drop (residualB r) (D ++ res.keys) = res.keys + rw [List.drop_append_of_le_length (i := residualB r) (le_of_eq hDlen.symm), + List.drop_eq_nil_of_le (le_of_eq hDlen), List.nil_append] + have hwcomp : RangeRead.Compatible w r res := by + refine ⟨hcomp.1, Or.inr ⟨hmore, hckW.symm⟩⟩ + have hwsub : ∀ k ∈ w.keys, ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) := by + intro k hk + rw [hwkeys] at hk + rcases List.mem_append.mp hk with hkD | hkR + · exact le_trans (le_of_lt (hDH k hkD)) + (hHle _ (List.getLast_mem hne)) + · exact WithTop.coe_le_coe.mpr ((hRsorted.pairwise.imp le_of_lt).rel_getLast hkR) + exact ⟨w, hwcomp, hwsub, hEtop, hlen_lt⟩ + + +private theorem residual_exists_topWitness {db₀ : DB} {r : RangeRead} {res : Result} {g : ℕ} + (hcomp : RangeRead.Compatible db₀ r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htop : (residualB r - g) + residualT r res < residualE r) : + ∃ w : DB, RangeRead.Compatible w r res ∧ + (∀ k ∈ w.keys, ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key)) ∧ + resolve w r.end_ = (⊤ : WithTop Key) ∧ + w.keys.length < w.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + set H : Key := res.keys.head hne with hH + have hHmem : H ∈ res.keys := by rw [hH]; exact List.head_mem hne + have hRsorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hHle : ∀ k ∈ res.keys, (↑H : WithTop Key) ≤ (↑k : WithTop Key) := by + intro k hk + rw [hH] + exact WithTop.coe_le_coe.mpr ((hRsorted.pairwise.imp le_of_lt).rel_head hk) + have hBH : r.begin.key ≤ (↑H : WithTop Key) := by + have h := begin_key_le_head_of_one_le_offset (r := r) (res := res) ⟨db₀, hcomp⟩ hne hbegin + rwa [hH] + have hB_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := by + intro htop' + have hle : (⊤ : WithTop Key) ≤ (↑H : WithTop Key) := by rwa [htop'] at hBH + exact absurd hle (not_le.mpr (WithTop.coe_lt_top H)) + obtain ⟨v, hvB⟩ := WithTop.ne_top_iff_exists.mp hB_ne_top + have hvB' : r.begin.key = (↑v : WithTop Key) := hvB.symm + have hE_ne_top : r.end_.key ≠ (⊤ : WithTop Key) := by + intro htop' + rw [htop', intervalCard_top_right hB_ne_top] at hcard + exact absurd hcard (by simp) + obtain ⟨e, heE'⟩ := WithTop.ne_top_iff_exists.mp hE_ne_top + have heE : r.end_.key = (↑e : WithTop Key) := heE'.symm + have hcard' : intervalCard (↑v : WithTop Key) (↑e : WithTop Key) = (g : ℕ∞) := by + simpa [hvB', heE] using hcard + have hve : (↑v : WithTop Key) ≤ (↑e : WithTop Key) := by + rw [← hvB', ← heE]; exact hcross + have hBH' : (↑v : WithTop Key) ≤ (↑H : WithTop Key) := by rwa [hvB'] at hBH + have hlenBH : (between db₀ (↑v) (↑H)).length = residualB r := by + have h := residual_between_begin_head (db₀ := db₀) (r := r) (res := res) + hcomp hmore hrev hne hbegin + have hh : ((res.keys.head hne : Key) : WithTop Key) = (↑H : WithTop Key) := by rw [hH] + rw [hvB', hh] at h + simpa [residualB] using h + have hleBE : (between db₀ (↑v) (↑e)).length ≤ g := by + have h := between_length_le_intervalCard (db := db₀) hve + rw [hcard'] at h + exact_mod_cast h + by_cases hEH : r.end_.key ≤ (↑H : WithTop Key) + · -- Case `end.key ≤ H`: fill `min b g` chain keys below `end.key`, + -- then take the remaining `b - min b g` database keys in `[end.key, H)`. + have hEH' : (↑e : WithTop Key) ≤ (↑H : WithTop Key) := by rwa [heE] at hEH + have hsplit : (between db₀ (↑v) (↑H)).length = + (between db₀ (↑v) (↑e)).length + (between db₀ (↑e) (↑H)).length := by + have h1 := cnt_eq_add_between db₀ hve + have h2 := cnt_eq_add_between db₀ hBH' + have h3 := cnt_eq_add_between db₀ hEH' + omega + have hleBE_b : (between db₀ (↑v) (↑e)).length ≤ residualB r := by omega + have hleBE_n : (between db₀ (↑v) (↑e)).length ≤ min (residualB r) g := + le_min hleBE_b hleBE + have hd_le : residualB r - min (residualB r) g ≤ + (between db₀ (↑e) (↑H)).length := by omega + set n : ℕ := min (residualB r) g with hn + set D : List Key := ((List.range n).map (chain v)) ++ + (between db₀ (↑e) (↑H)).take (residualB r - n) with hD + have hDlen : D.length = residualB r := by + simp only [hD, List.length_append, List.length_map, List.length_range, + List.length_take] + omega + have hbtsorted : (between db₀ (↑e) (↑H)).SortedLT := + List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hD2sorted : ((between db₀ (↑e) (↑H)).take (residualB r - n)).SortedLT := + List.sortedLT_iff_pairwise.mpr + (List.Pairwise.take (R := (· < ·)) (List.sortedLT_iff_pairwise.mp hbtsorted)) + have hDsorted : D.SortedLT := by + rw [hD, List.sortedLT_append] + refine ⟨sortedLT_map_chain v n, hD2sorted, ?_⟩ + intro a ha b hb + rw [List.mem_map] at ha + obtain ⟨i, hi, rfl⟩ := ha + have hi_lt : i < g := lt_of_lt_of_le (List.mem_range.mp hi) (min_le_right _ _) + have hae : chain v i < e := by + rw [chain_lt_iff] + rw [hcard'] + exact_mod_cast hi_lt + have hbmem := List.mem_of_mem_take hb + rw [between, List.mem_filter] at hbmem + exact lt_of_lt_of_le hae (WithTop.coe_le_coe.mp (of_decide_eq_true hbmem.2).1) + have hDB : ∀ k ∈ D, r.begin.key ≤ (↑k : WithTop Key) := by + intro k hk + rw [hD, List.mem_append] at hk + rcases hk with hk1 | hk2 + · rw [List.mem_map] at hk1 + obtain ⟨i, hi, rfl⟩ := hk1 + rw [hvB'] + exact WithTop.coe_le_coe.mpr (chain_le v i) + · have hbmem := List.mem_of_mem_take hk2 + rw [between, List.mem_filter] at hbmem + rw [hvB'] + exact le_trans hve (of_decide_eq_true hbmem.2).1 + have hDH : ∀ k ∈ D, (↑k : WithTop Key) < (↑H : WithTop Key) := by + intro k hk + rw [hD, List.mem_append] at hk + rcases hk with hk1 | hk2 + · rw [List.mem_map] at hk1 + obtain ⟨i, hi, rfl⟩ := hk1 + have hi_lt : i < g := lt_of_lt_of_le (List.mem_range.mp hi) (min_le_right _ _) + have hae : chain v i < e := by + rw [chain_lt_iff] + rw [hcard'] + exact_mod_cast hi_lt + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hae) hEH' + · have hbmem := List.mem_of_mem_take hk2 + rw [between, List.mem_filter] at hbmem + exact (of_decide_eq_true hbmem.2).2 + have hDcntE : (D.filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).length = n := by + rw [hD, List.filter_append] + have h1 : (List.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + ((List.range n).map (chain v))) = (List.range n).map (chain v) := by + rw [List.filter_eq_self] + intro k hk + rw [List.mem_map] at hk + obtain ⟨i, hi, rfl⟩ := hk + rw [decide_eq_true_eq, heE] + have hi_lt : i < g := lt_of_lt_of_le (List.mem_range.mp hi) (min_le_right _ _) + have hae : chain v i < e := by + rw [chain_lt_iff] + rw [hcard'] + exact_mod_cast hi_lt + exact WithTop.coe_lt_coe.mpr hae + have h2 : (List.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + ((between db₀ (↑e) (↑H)).take (residualB r - n))) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk + rw [decide_eq_true_eq] + have hbmem := List.mem_of_mem_take hk + rw [between, List.mem_filter] at hbmem + have hEk : (↑e : WithTop Key) ≤ (↑k : WithTop Key) := (of_decide_eq_true hbmem.2).1 + intro hklt + rw [heE] at hklt + exact absurd hklt (not_lt.mpr hEk) + rw [h1, h2, List.length_append, List.length_map, List.length_range, List.length_nil] + omega + have hbn : residualB r - n = residualB r - g := by + simp only [hn] + by_cases hbg : residualB r ≤ g + · rw [min_eq_left hbg] + have hz : residualB r - g = 0 := by omega + rw [Nat.sub_self, hz] + · rw [min_eq_right (le_of_not_ge hbg)] + exact residual_topWitness_of_D (g := g) (n := n) (H := H) hcomp hmore hrev hne hbegin hend + hHle hBH D hDlen hDsorted hDB hDH hDcntE hbn htop + · -- Case `H < end.key`: there are only `b` keys in `[B, H)`, and + -- `b ≤ g`, so use them all. + have hHE : (↑H : WithTop Key) < r.end_.key := lt_of_not_ge hEH + have hHE' : (↑H : WithTop Key) < (↑e : WithTop Key) := by rwa [heE] at hHE + have hb_le_g : residualB r ≤ g := by + have h1 := cnt_eq_add_between db₀ hBH' + have h2 := cnt_eq_add_between db₀ hve + have h3 := cnt_le_cnt_of_le db₀ (le_of_lt hHE') + omega + set n : ℕ := residualB r with hn + set D : List Key := between db₀ (↑v) (↑H) with hD + have hDlen : D.length = residualB r := by rw [hD]; exact hlenBH + have hD2sorted : D.SortedLT := + List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hDB : ∀ k ∈ D, r.begin.key ≤ (↑k : WithTop Key) := by + intro k hk + rw [hD, between, List.mem_filter] at hk + rw [hvB'] + exact (of_decide_eq_true hk.2).1 + have hDH : ∀ k ∈ D, (↑k : WithTop Key) < (↑H : WithTop Key) := by + intro k hk + rw [hD, between, List.mem_filter] at hk + exact (of_decide_eq_true hk.2).2 + have hDcntE : (D.filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).length = n := by + rw [show D.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = D from by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq, heE] + exact lt_trans (hDH k hk) hHE'] + exact hDlen + have hbn : residualB r - n = residualB r - g := by + simp only [hn, Nat.sub_self] + omega + exact residual_topWitness_of_D (g := g) (n := n) (H := H) hcomp hmore hrev hne hbegin hend + hHle hBH D hDlen hD2sorted hDB hDH hDcntE hbn htop + + +private theorem residual_necessary_above_last_top {db₀ : DB} {r : RangeRead} + {res : Result} {g : ℕ} + (hread : Readable r res) (hcomp : RangeRead.Compatible db₀ r res) + (hmore : res.more = false) (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htop : (residualB r - g) + residualT r res < residualE r) + {x : Key} (hlo : res.keys.getLast hne ≤ x) : + Necessary r res x := by + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result hread hx + · intro cr hs + by_contra hcr + obtain ⟨w, hwcomp, hwsub, hwtop, hwlt⟩ := + residual_exists_topWitness (db₀ := db₀) (r := r) (res := res) (g := g) + hcomp hmore hrev hne hbegin hcross hend hcard htop + have hx0 : x ∉ w.keys := by + intro hxw + have hxle : x ≤ res.keys.getLast hne := + WithTop.coe_le_coe.mp (hwsub x hxw) + have hxeq : x = res.keys.getLast hne := le_antisymm hxle hlo + exact hx (hxeq ▸ List.getLast_mem hne) + have hsub : ∀ k ∈ w.keys, ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + fun k hk => le_trans (hwsub k hk) (WithTop.coe_le_coe.mpr hlo) + have hEins : resolve (w.insert x hx0) r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_insert_top hx0 hwlt hend + exact hx (mem_result_of_insert (db₀ := w) hwcomp hs hcr hmore hne hx0 hsub hEins) + +/-- **Top-branch minimality in the residual cell.** When `(b - g) + t < e`, the +unbounded top range `[begin.key, ⊤)` is the minimal sound conflict range. Soundness is +`sound_topRange_of_one_le_le`; keys up to the reported last are necessary by the begin +key, the reported last, and convexity, while keys above it are +`necessary_above_last_of_residual_top`. -/ +theorem minimal_topRange_of_residual_top {r : RangeRead} {res : Result} {g : ℕ} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) + (htop : (residualB r - g) + residualT r res < residualE r) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp₀⟩ := hread + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_topRange_of_one_le_le (by omega) (by omega) hcross + · intro k hk cr hs + obtain ⟨hBk, -⟩ := hk + by_cases hklast : k ≤ res.keys.getLast hne + · have hBtop : r.begin.key ≠ (⊤ : WithTop Key) := by + obtain ⟨m, _, hmge⟩ := List.countP_pos_iff.mp htpos + rw [decide_eq_true_eq] at hmge + intro htop' + rw [htop'] at hcross + exact absurd (le_trans hcross hmge) (not_le_of_gt (WithTop.coe_lt_top m)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hBtop + have hvB : r.begin.key = (↑v : WithTop Key) := hv.symm + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp₀⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hvB + have hvk : v ≤ k := by + change r.begin.key ≤ (↑k : WithTop Key) at hBk + exact WithTop.coe_le_coe.mp (by rwa [hvB] at hBk) + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + exact (hbegin_nec.of_between hlast_nec hvk hklast) cr hs + · have hklast' : res.keys.getLast hne < k := lt_of_not_ge hklast + exact residual_necessary_above_last_top ⟨db₀, hcomp₀⟩ hcomp₀ hmore hrev hne hbegin + hcross hend hcard htop hklast'.le cr hs + + +/-- **The capacity criterion never overruns the end offset.** In the residual +complete-result cell the two branches are exhaustive: `(b - g) + t ≤ e`. The proof +reads the exact count identity `completeKeys_count_ge_eq_general`: at most `g` stored +keys lie in `[begin.key, end.key)`, and a positive count of reported keys at or above +`end.key` makes the top window long enough. -/ +theorem residual_dt_le_e {db : DB} {r : RangeRead} {res : Result} {g : ℕ} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) : + (residualB r - g) + residualT r res ≤ residualE r := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hEq := completeKeys_count_ge_eq_general (db := db) (r := r) hrev hbegin hcross hend + have ht : residualT r res = (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + rw [residualT, hres] + rw [ht, hEq] + have hNle : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) ≤ g := by + have h := cnt_sub_le_intervalCard db hcross + rw [hcard] at h + exact_mod_cast h + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set A := db.keys.length - db.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hA + set N := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hN + have htpos' : 0 < min e A - (b - N) := by + rw [residualT, hres] at htpos + rw [hEq] at htpos + simpa [b, e, A, N] using htpos + have hmain : (b - g) + (min e A - (b - N)) ≤ e := by + by_cases hbg : b ≤ g + · have hz : b - g = 0 := by omega + rw [hz, Nat.zero_add] + exact le_trans (Nat.sub_le _ _) (min_le_left _ _) + · have hgb : g < b := lt_of_not_ge hbg + have hNb : N ≤ g := hNle + omega + simpa [residualB, residualE, b, e, A, N] using hmain + +/-- **The residual complete-result closed form.** It is the truncated range +`[begin.key, keyAfter (last reported))` in the finite branch `(b - g) + t = e`, and +the unbounded top range `[begin.key, ⊤)` in the complementary branch. -/ +noncomputable def closedFormResidual (r : RangeRead) (res : Result) (g : ℕ) : + ConflictRange := + if (residualB r - g) + residualT r res = residualE r then + truncatedRange r (res.keys.getLastD ([] : Key)) + else topRange r + +/-- **The residual closed form is minimal.** The dispatch is exhaustive by +`residual_dt_le_e`; the finite branch is `minimal_truncatedRange_of_residual_finite` +and the strict branch is `minimal_topRange_of_residual_top`. -/ +theorem minimal_closedForm_of_residual {r : RangeRead} {res : Result} {g : ℕ} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) : + Minimal r res (closedFormResidual r res g) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hdtle := residual_dt_le_e (db := db₀) hcomp₀ hmore hrev hbegin hcross + (by omega) hcard htpos + unfold closedFormResidual + by_cases hdt : (residualB r - g) + residualT r res = residualE r + · rw [ite_eq_left hdt] + exact minimal_truncatedRange_of_residual_finite ⟨db₀, hcomp₀⟩ hmore hrev hbegin hcross + (by omega) hcard htpos htlt hdt + · rw [ite_eq_right hdt] + have htop : (residualB r - g) + residualT r res < residualE r := + lt_of_le_of_ne hdtle hdt + exact minimal_topRange_of_residual_top ⟨db₀, hcomp₀⟩ hmore hrev hne hbegin hcross hend + hcard htpos htop + +/-- The residual closed form denotes the same key set as the general conflict range. -/ +theorem closedFormResidual_toSet_eq_general {r : RangeRead} {res : Result} {g : ℕ} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) : + (closedFormResidual r res g).toSet = (conflictRangeGeneral r res).toSet := + (minimal_closedForm_of_residual hread hmore hrev hne hbegin hcross hend hcard + htpos htlt).toSet_eq (conflictRangeGeneral_minimal hread) + +/-- The residual closed form denotes the same key set as the executable algorithm. -/ +theorem closedFormResidual_toSet_eq_simple {r : RangeRead} {res : Result} {g : ℕ} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = (g : ℕ∞)) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) : + (closedFormResidual r res g).toSet = (simpleConflictRange r res).toSet := + (minimal_closedForm_of_residual hread hmore hrev hne hbegin hcross hend hcard + htpos htlt).toSet_eq (simpleConflictRange_minimal hread) + +/-! ## The infinite-gap case + +When `intervalCard begin.key end.key = ⊤`, there are arbitrarily many chain keys +between the selector keys, so the witness for the top branch can use `b` chain keys +without any finite capacity hypothesis. The finite branch `d + t = e` is impossible +there (it would require the finite database to fill an infinite gap), so the top +branch is the only outcome. This removes the last finiteness restriction from the +residual criterion. -/ + +private theorem residual_exists_topWitness_infinite {db₀ : DB} {r : RangeRead} + {res : Result} + (hcomp : RangeRead.Compatible db₀ r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hinf : intervalCard r.begin.key r.end_.key = ⊤) + (htpos : 0 < residualT r res) + (htop : residualT r res < residualE r) : + ∃ w : DB, RangeRead.Compatible w r res ∧ + (∀ k ∈ w.keys, ((k : Key) : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key)) ∧ + resolve w r.end_ = (⊤ : WithTop Key) ∧ + w.keys.length < w.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + set H : Key := res.keys.head hne with hH + have hRsorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hHle : ∀ k ∈ res.keys, (↑H : WithTop Key) ≤ (↑k : WithTop Key) := by + intro k hk + rw [hH] + exact WithTop.coe_le_coe.mpr ((hRsorted.pairwise.imp le_of_lt).rel_head hk) + have hBH : r.begin.key ≤ (↑H : WithTop Key) := by + have h := begin_key_le_head_of_one_le_offset (r := r) (res := res) ⟨db₀, hcomp⟩ hne hbegin + rwa [hH] + have hB_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := by + intro htop' + have hle : (⊤ : WithTop Key) ≤ (↑H : WithTop Key) := by rwa [htop'] at hBH + exact absurd hle (not_le.mpr (WithTop.coe_lt_top H)) + obtain ⟨v, hvB⟩ := WithTop.ne_top_iff_exists.mp hB_ne_top + have hvB' : r.begin.key = (↑v : WithTop Key) := hvB.symm + have hE_ne_top : r.end_.key ≠ (⊤ : WithTop Key) := by + intro htop' + simp [residualT, htop'] at htpos + obtain ⟨e, heE'⟩ := WithTop.ne_top_iff_exists.mp hE_ne_top + have heE : r.end_.key = (↑e : WithTop Key) := heE'.symm + have hinf' : intervalCard (↑v : WithTop Key) (↑e : WithTop Key) = ⊤ := by + simpa [hvB', heE] using hinf + have hve : (↑v : WithTop Key) ≤ (↑e : WithTop Key) := by + rw [← hvB', ← heE]; exact hcross + have hBH' : (↑v : WithTop Key) ≤ (↑H : WithTop Key) := by rwa [hvB'] at hBH + have hlenBH : (between db₀ (↑v) (↑H)).length = residualB r := by + have h := residual_between_begin_head (db₀ := db₀) (r := r) (res := res) + hcomp hmore hrev hne hbegin + have hh : ((res.keys.head hne : Key) : WithTop Key) = (↑H : WithTop Key) := by rw [hH] + rw [hvB', hh] at h + simpa [residualB] using h + by_cases hEH : r.end_.key ≤ (↑H : WithTop Key) + · have hEH' : (↑e : WithTop Key) ≤ (↑H : WithTop Key) := by rwa [heE] at hEH + set n : ℕ := residualB r with hn + set D : List Key := (List.range n).map (chain v) with hD + have hDlen : D.length = residualB r := by simp [hD, hn] + have hDsorted : D.SortedLT := by + rw [hD]; exact sortedLT_map_chain v n + have hDB : ∀ k ∈ D, r.begin.key ≤ (↑k : WithTop Key) := by + intro k hk + rw [hD, List.mem_map] at hk + obtain ⟨i, hi, rfl⟩ := hk + rw [hvB'] + exact WithTop.coe_le_coe.mpr (chain_le v i) + have hDH : ∀ k ∈ D, (↑k : WithTop Key) < (↑H : WithTop Key) := by + intro k hk + rw [hD, List.mem_map] at hk + obtain ⟨i, hi, rfl⟩ := hk + exact lt_of_lt_of_le + (WithTop.coe_lt_coe.mpr (chain_lt_of_intervalCard_eq_top hinf' i)) hEH' + have hDcntE : (D.filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).length = n := by + rw [hD] + have hfilter : (List.filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + ((List.range n).map (chain v))) = (List.range n).map (chain v) := by + rw [List.filter_eq_self] + intro k hk + rw [List.mem_map] at hk + obtain ⟨i, hi, rfl⟩ := hk + rw [decide_eq_true_eq, heE] + exact WithTop.coe_lt_coe.mpr (chain_lt_of_intervalCard_eq_top hinf' i) + rw [hfilter, List.length_map, List.length_range] + exact residual_topWitness_of_D (g := residualB r) (n := n) (H := H) + hcomp hmore hrev hne hbegin hend hHle hBH D hDlen hDsorted hDB hDH hDcntE + (by simp [hn]) (by simpa [hn] using htop) + · have hHE : (↑H : WithTop Key) < r.end_.key := lt_of_not_ge hEH + have hHE' : (↑H : WithTop Key) < (↑e : WithTop Key) := by rwa [heE] at hHE + set n : ℕ := residualB r with hn + set D : List Key := between db₀ (↑v) (↑H) with hD + have hDlen : D.length = residualB r := by rw [hD]; exact hlenBH + have hDsorted : D.SortedLT := by + rw [hD]; exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hDB : ∀ k ∈ D, r.begin.key ≤ (↑k : WithTop Key) := by + intro k hk + rw [hD, between, List.mem_filter] at hk + rw [hvB'] + exact (of_decide_eq_true hk.2).1 + have hDH : ∀ k ∈ D, (↑k : WithTop Key) < (↑H : WithTop Key) := by + intro k hk + rw [hD, between, List.mem_filter] at hk + exact (of_decide_eq_true hk.2).2 + have hDcntE : (D.filter + (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).length = n := by + rw [show D.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = D from by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq, heE] + exact lt_trans (hDH k hk) hHE'] + exact hDlen + exact residual_topWitness_of_D (g := residualB r) (n := n) (H := H) + hcomp hmore hrev hne hbegin hend hHle hBH D hDlen hDsorted hDB hDH hDcntE + (by simp [hn]) (by simpa [hn] using htop) + +private theorem residual_necessary_above_last_top_infinite {db₀ : DB} {r : RangeRead} + {res : Result} + (hread : Readable r res) (hcomp : RangeRead.Compatible db₀ r res) + (hmore : res.more = false) (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hinf : intervalCard r.begin.key r.end_.key = ⊤) + (htpos : 0 < residualT r res) + (htop : residualT r res < residualE r) + {x : Key} (hlo : res.keys.getLast hne ≤ x) : + Necessary r res x := by + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result hread hx + · intro cr hs + by_contra hcr + obtain ⟨w, hwcomp, hwsub, hwtop, hwlt⟩ := + residual_exists_topWitness_infinite (db₀ := db₀) (r := r) (res := res) + hcomp hmore hrev hne hbegin hcross hend hinf htpos htop + have hx0 : x ∉ w.keys := by + intro hxw + have hxle : x ≤ res.keys.getLast hne := + WithTop.coe_le_coe.mp (hwsub x hxw) + have hxeq : x = res.keys.getLast hne := le_antisymm hxle hlo + exact hx (hxeq ▸ List.getLast_mem hne) + have hsub : ∀ k ∈ w.keys, ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + fun k hk => le_trans (hwsub k hk) (WithTop.coe_le_coe.mpr hlo) + have hEins : resolve (w.insert x hx0) r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_insert_top hx0 hwlt hend + exact hx (mem_result_of_insert (db₀ := w) hwcomp hs hcr hmore hne hx0 hsub hEins) + +/-- **Top-branch minimality with an infinite selector gap.** When +`intervalCard begin.key end.key = ⊤`, the finite branch is impossible and the +unbounded top range is minimal as soon as `t < e`. -/ +theorem minimal_topRange_of_residual_top_infinite {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hinf : intervalCard r.begin.key r.end_.key = ⊤) + (htpos : 0 < residualT r res) + (htlt : residualT r res < residualE r) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp₀⟩ := hread + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_topRange_of_one_le_le (by omega) (by omega) hcross + · intro k hk cr hs + obtain ⟨hBk, -⟩ := hk + by_cases hklast : k ≤ res.keys.getLast hne + · have hBtop : r.begin.key ≠ (⊤ : WithTop Key) := by + obtain ⟨m, _, hmge⟩ := List.countP_pos_iff.mp htpos + rw [decide_eq_true_eq] at hmge + intro htop' + rw [htop'] at hcross + exact absurd (le_trans hcross hmge) (not_le_of_gt (WithTop.coe_lt_top m)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hBtop + have hvB : r.begin.key = (↑v : WithTop Key) := hv.symm + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp₀⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hvB + have hvk : v ≤ k := by + change r.begin.key ≤ (↑k : WithTop Key) at hBk + exact WithTop.coe_le_coe.mp (by rwa [hvB] at hBk) + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + exact (hbegin_nec.of_between hlast_nec hvk hklast) cr hs + · have hklast' : res.keys.getLast hne < k := lt_of_not_ge hklast + exact residual_necessary_above_last_top_infinite ⟨db₀, hcomp₀⟩ hcomp₀ hmore hrev + hne hbegin hcross hend hinf htpos htlt hklast'.le cr hs + +/-- The capacity deficit `d = max 0 (b - g)`, with `g : ℕ∞`; an infinite gap leaves +nothing to fill. -/ +def residualD (r : RangeRead) (g : ℕ∞) : ℕ := + if g = ⊤ then 0 else residualB r - g.toNat + +/-- For a finite gap the deficit is ordinary truncated subtraction. -/ +theorem residualD_coe (r : RangeRead) (n : ℕ) : + residualD r (n : ℕ∞) = residualB r - n := by + unfold residualD + rw [ite_eq_right (by simp)] + rfl + +/-- **The residual complete-result closed form with an infinite gap parameter.** It +is the truncated range in the finite branch `d + t = e` and the top range otherwise, +where `d = max 0 (b - g)` and `g` is the full interval cardinality, possibly `⊤`. -/ +noncomputable def closedFormResidualGeneral (r : RangeRead) (res : Result) (g : ℕ∞) : + ConflictRange := + if residualD r g + residualT r res = residualE r then + truncatedRange r (res.keys.getLastD ([] : Key)) + else topRange r + +/-- **The infinite-gap residual closed form is minimal.** The finite branch is +impossible, and `minimal_topRange_of_residual_top_infinite` gives the top range. -/ +theorem minimal_closedForm_of_residual_general {r : RangeRead} {res : Result} + {g : ℕ∞} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = g) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) : + Minimal r res (closedFormResidualGeneral r res g) := by + by_cases hg : g = ⊤ + · have hinf : intervalCard r.begin.key r.end_.key = ⊤ := by rw [hcard, hg] + have hne' : residualD r g + residualT r res ≠ residualE r := by + simp [residualD, hg] + omega + unfold closedFormResidualGeneral + rw [ite_eq_right hne'] + exact minimal_topRange_of_residual_top_infinite hread hmore hrev hne hbegin hcross + hend hinf htpos htlt + · obtain ⟨n, rfl⟩ := ENat.ne_top_iff_exists.mp hg + have hcardn : intervalCard r.begin.key r.end_.key = (n : ℕ∞) := hcard + unfold closedFormResidualGeneral + rw [residualD_coe] + by_cases hdt : (residualB r - n) + residualT r res = residualE r + · rw [ite_eq_left hdt] + exact minimal_truncatedRange_of_residual_finite hread hmore hrev hbegin hcross + (by omega) hcardn htpos htlt hdt + · rw [ite_eq_right hdt] + have hle := residual_dt_le_e (db := Classical.choose hread) (Classical.choose_spec hread) + hmore hrev hbegin hcross (by omega) hcardn htpos + have htop : (residualB r - n) + residualT r res < residualE r := + lt_of_le_of_ne hle hdt + exact minimal_topRange_of_residual_top hread hmore hrev hne hbegin hcross hend + hcardn htpos htop + +/-- The general residual closed form denotes the general conflict range. -/ +theorem closedFormResidualGeneral_toSet_eq_general {r : RangeRead} {res : Result} + {g : ℕ∞} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = g) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) : + (closedFormResidualGeneral r res g).toSet = (conflictRangeGeneral r res).toSet := + (minimal_closedForm_of_residual_general hread hmore hrev hne hbegin hcross hend hcard + htpos htlt).toSet_eq (conflictRangeGeneral_minimal hread) + +/-- The general residual closed form denotes the executable algorithm. -/ +theorem closedFormResidualGeneral_toSet_eq_simple {r : RangeRead} {res : Result} + {g : ℕ∞} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (hcard : intervalCard r.begin.key r.end_.key = g) + (htpos : 0 < residualT r res) (htlt : residualT r res < residualE r) : + (closedFormResidualGeneral r res g).toSet = (simpleConflictRange r res).toSet := + (minimal_closedForm_of_residual_general hread hmore hrev hne hbegin hcross hend hcard + htpos htlt).toSet_eq (simpleConflictRange_minimal hread) + +/-- **The total cross-free complete-result closed form.** On a complete result with +`begin.offset ≥ 1` and `begin.key ≤ end_.key`, the automatic three-branch rule covers +`end.offset ≤ 1`, the finite branch `t = e`, and the below-`end_.key` branch; the +equal-selector rule covers `begin.key = end_.key`; and +`closedFormResidualGeneral` covers the remaining strict cell `begin.key < end_.key`, +`0 < t < e`. The gap parameter is the full interval cardinality, so the infinite-gap +case is included. -/ +noncomputable def closedFormCompleteCrossFree (r : RangeRead) (res : Result) : + ConflictRange := + if r.end_.offset ≤ 1 then + closedFormOffsetOne r res + else if residualT r res = residualE r ∨ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) then + closedFormOffsetOne r res + else if r.begin.key = r.end_.key then + closedFormKeyEqComplete r res + else + closedFormResidualGeneral r res (intervalCard r.begin.key r.end_.key) + +/-- **The total cross-free complete-result rule is minimal at every positive begin +offset.** -/ +theorem minimal_closedForm_of_complete_crossFree {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) : + Minimal r res (closedFormCompleteCrossFree r res) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, -, -⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + unfold closedFormCompleteCrossFree + by_cases hend1 : r.end_.offset ≤ 1 + · rw [ite_eq_left hend1] + exact minimal_closedForm_of_complete_general ⟨db₀, hcomp₀⟩ hmore hne hrev hbegin hcross + (Or.inl hend1) + · rw [ite_eq_right hend1] + by_cases hcase : residualT r res = residualE r ∨ + (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + · rw [ite_eq_left hcase] + exact minimal_closedForm_of_complete_general ⟨db₀, hcomp₀⟩ hmore hne hrev hbegin hcross + (Or.inr hcase) + · rw [ite_eq_right hcase] + have htne : residualT r res ≠ residualE r := fun h => hcase (Or.inl h) + have hbelow : ¬ (∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) := + fun h => hcase (Or.inr h) + have ht0 : residualT r res ≠ 0 := by + intro h0 + apply hbelow + intro k hk + have hk' := List.countP_eq_zero.mp h0 k hk + rw [decide_eq_true_eq] at hk' + exact not_le.mp hk' + have htpos : 0 < residualT r res := Nat.pos_of_ne_zero ht0 + have htle' := completeKeys_count_ge_le_general (db := db₀) hrev hbegin hcross + (by omega : 1 ≤ r.end_.offset) + have htle : residualT r res ≤ residualE r := by + simpa [residualT, residualE, hres] using htle' + have htlt : residualT r res < residualE r := lt_of_le_of_ne htle htne + have hend2 : 2 ≤ r.end_.offset := by omega + by_cases hkey : r.begin.key = r.end_.key + · rw [ite_eq_left hkey] + exact minimal_closedForm_of_key_eq_complete_general ⟨db₀, hcomp₀⟩ hmore hrev hne hbegin + hkey hend2 + · rw [ite_eq_right hkey] + have hltkey : r.begin.key < r.end_.key := lt_of_le_of_ne hcross hkey + exact minimal_closedForm_of_residual_general + (g := intervalCard r.begin.key r.end_.key) ⟨db₀, hcomp₀⟩ hmore hrev hne hbegin + hcross hend2 rfl htpos htlt + +/-- The total cross-free complete rule denotes the general conflict range. -/ +theorem closedFormCompleteCrossFree_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) (hcross : r.begin.key ≤ r.end_.key) : + (closedFormCompleteCrossFree r res).toSet = (conflictRangeGeneral r res).toSet := + (minimal_closedForm_of_complete_crossFree hread hmore hrev hne hbegin hcross).toSet_eq + (conflictRangeGeneral_minimal hread) + +/-- The total cross-free complete rule denotes the executable algorithm. -/ +theorem closedFormCompleteCrossFree_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) (hcross : r.begin.key ≤ r.end_.key) : + (closedFormCompleteCrossFree r res).toSet = (simpleConflictRange r res).toSet := + (minimal_closedForm_of_complete_crossFree hread hmore hrev hne hbegin hcross).toSet_eq + (simpleConflictRange_minimal hread) + +end FormalConflictRange diff --git a/FormalConflictRange/Selector.lean b/FormalConflictRange/Selector.lean new file mode 100644 index 0000000..24c26b5 --- /dev/null +++ b/FormalConflictRange/Selector.lean @@ -0,0 +1,360 @@ +/- +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.Basic + +/-! +# Selectors and resolution + +A `Selector` names a key together with a signed offset. Resolving a selector +against a database targets the stored key at index +`countP (· < selector.key) + selector.offset - 1`: the last stored key strictly +below `selector.key` when the offset is zero, advancing one key per unit of +offset, so offset `1` is the first key at or above `selector.key`. Indices below +zero resolve to the empty key and indices past the end to `⊤`. + +Resolution returns a `WithTop Key`, where `⊤` is the sentinel "no such key". +This makes the past-the-end case explicit in the type, so no concrete sentinel +key and no well-formedness side condition need to be threaded through the API. + +The selector's `key` is a `WithTop Key` rather than a plain `Key`: it anchors +the offset in the key space by counting the stored keys strictly below it. The +bottom of `WithTop Key` is the empty key, which counts no keys, and the top +`⊤` counts every stored key, so a selector keyed at `⊤` anchors past the end +of the database whatever its size. This is what makes a database-independent +"read all keys" expressible: `key := ⊤, offset := 1` resolves to `⊤` for every +database, and `key := ↑[], offset := 0` resolves to the empty key, so the read +with those two selectors and no limit returns every stored key (see +`RangeRead.readAll`). No plain key could anchor past every stored key, since +`Key` has no greatest element, and only the empty key — the least key — anchors +at the very start, which is why the bottom needs no sentinel of its own. +Resolution still clamps negative anchors to the empty key, so a selector never +fails to resolve. +-/ + +namespace FormalConflictRange + +/-- A selector names a key position in `WithTop Key`, with an integer offset. +The empty key anchors at the start of the key space, and `⊤` anchors past every +stored key. -/ +structure Selector where + key : WithTop Key + offset : Int + +/-- The signed index a selector targets: the number of stored keys strictly +below `selector.key` in `WithTop Key` — none for the empty key, all of them for +`⊤` — less one, shifted by `selector.offset`. Offset zero names the last stored +key strictly below `selector.key`, which is index `-1` when there is none; +offset `1` names the first stored key at or above it; offset `-1` names the key +before that. -/ +def Selector.anchor (db : DB) (s : Selector) : Int := + (db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) + s.offset - 1 + +/-- Resolve a selector against `db`: target the anchor key, clamping negative +indices to the empty key and indices past the end to the sentinel `⊤`. -/ +def resolve (db : DB) (s : Selector) : WithTop Key := + if s.anchor db < 0 then ([] : Key) + else match db.keys[(s.anchor db).toNat]? with + | some k => k + | none => ⊤ + +/-- A selector targeting a negative index resolves to the empty key. -/ +theorem resolve_eq_nil {db : DB} {s : Selector} (h : s.anchor db < 0) : + resolve db s = ↑([] : Key) := by + rw [resolve, ite_eq_left h] + +/-- A selector targeting an in-range index resolves to the stored key there. -/ +theorem resolve_eq_getElem {db : DB} {s : Selector} + (h0 : 0 ≤ s.anchor db) (h : (s.anchor db).toNat < db.keys.length) : + resolve db s = ↑(db.keys[(s.anchor db).toNat]) := by + rw [resolve, ite_eq_right (not_lt.mpr h0), List.getElem?_eq_getElem h] + +/-- A selector targeting an index at or past the end resolves to `⊤`. -/ +theorem resolve_eq_top {db : DB} {s : Selector} + (h0 : 0 ≤ s.anchor db) (h : db.keys.length ≤ (s.anchor db).toNat) : + resolve db s = (⊤ : WithTop Key) := by + rw [resolve, ite_eq_right (not_lt.mpr h0), List.getElem?_eq_none h] + +/-- Every resolution is the empty key, the sentinel `⊤`, or a stored key. -/ +theorem resolve_eq_nil_or_eq_top_or_mem {db : DB} {s : Selector} : + resolve db s = ↑([] : Key) ∨ resolve db s = (⊤ : WithTop Key) ∨ + ∃ k ∈ db.keys, resolve db s = ↑k := by + rw [resolve] + split + · exact Or.inl rfl + · rename_i h + cases hlt : db.keys[(s.anchor db).toNat]? with + | none => exact Or.inr (Or.inl rfl) + | some k => exact Or.inr (Or.inr ⟨k, List.mem_of_getElem? hlt, rfl⟩) + +/-- Resolution depends on a database only through its keys. -/ +theorem resolve_congr {db₁ db₂ : DB} {s : Selector} (hk : db₁.keys = db₂.keys) : + resolve db₁ s = resolve db₂ s := by + unfold resolve Selector.anchor + rw [hk] + +/-- Outside the negative clamp, `resolve` is a `getElem?`. -/ +theorem resolve_eq_ite (db : DB) (s : Selector) : + resolve db s = (if s.anchor db < 0 then (↑([] : Key) : WithTop Key) + else match db.keys[(s.anchor db).toNat]? with + | some k => (↑k : WithTop Key) + | none => ⊤) := by + unfold resolve + split <;> rfl + +/-- A selector keyed at a coerced key `↑v` anchors at the count of stored keys +strictly below `v`. -/ +theorem anchor_of_coe {db : DB} {s : Selector} {v : Key} (h : s.key = (↑v : WithTop Key)) : + s.anchor db = (db.keys.countP (fun k => decide (k < v)) : Int) + s.offset - 1 := by + unfold Selector.anchor + rw [h, countP_lt_coe] + +/-- A selector keyed at the empty key anchors at its offset less one: the +empty key counts no stored keys below it. -/ +theorem anchor_of_nil {db : DB} {s : Selector} (h : s.key = (([] : Key) : WithTop Key)) : + s.anchor db = s.offset - 1 := by + unfold Selector.anchor + rw [h, countP_lt_nil] + omega + +/-- A selector keyed at `⊤` anchors past every stored key: all of them lie +strictly below `⊤`. -/ +theorem anchor_of_top {db : DB} {s : Selector} (h : s.key = (⊤ : WithTop Key)) : + s.anchor db = (db.keys.length : Int) + s.offset - 1 := by + unfold Selector.anchor + rw [h, countP_lt_top] + +/-- The anchor is nonnegative when the offset is at least one. -/ +theorem anchor_nonneg_of_one_le {db : DB} {s : Selector} (h : 1 ≤ s.offset) : + 0 ≤ s.anchor db := by + unfold Selector.anchor + have : (0 : Int) ≤ + (db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) := + Int.natCast_nonneg _ + omega + +/-- The anchor's natural-number value splits as the count of keys strictly below +the selector key plus the nonnegative offset shift. -/ +theorem anchor_toNat_eq {db : DB} {s : Selector} (h : 1 ≤ s.offset) : + (s.anchor db).toNat = + db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) + + (s.offset - 1).toNat := by + unfold Selector.anchor + rw [show ((db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) + + s.offset - 1) = + ((db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) + + (s.offset - 1)) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + +/-- Inserting the successor chain of `[a, b)` when the selector is keyed at `a` +leaves the selector's anchor index unchanged: the chain starts at `a` and so +never lies strictly below the selector key. This is the interval analogue of the +anchor stability under a single-key insertion at the selector key. The resolution +itself may still change, since the chain replaces the erased keys at and above +`a`. -/ +theorem anchor_insertInterval_below {db : DB} {s : Selector} {a b : Key} + (hkey : s.key = (↑a : WithTop Key)) (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) : + s.anchor (db.insertInterval a b hab n hn) = s.anchor db := by + unfold Selector.anchor + rw [hkey, countP_lt_coe, countP_lt_coe, + countP_lt_insertInterval_of_lt hab (le_refl a) n hn] + +/-- Erasing the interval `[a, b)` when the selector is keyed at `a` leaves the +selector's anchor index unchanged: erased keys all lie at or above `a`. This is +the interval analogue of the anchor stability under a single-key erasure at the +selector key. The resolution itself may still change, since keys at and above `a` +are removed. -/ +theorem anchor_eraseInterval_below {db : DB} {s : Selector} {a b : Key} + (hkey : s.key = (↑a : WithTop Key)) : + s.anchor (db.eraseInterval (↑a) (↑b)) = s.anchor db := by + unfold Selector.anchor + rw [hkey, countP_lt_coe, countP_lt_coe, + countP_lt_eraseInterval_of_le (x := a) (le_refl a)] + +/-- When the selector is keyed at the upper end `b` of the replaced interval, the +interval insertion shifts its anchor by the chain length `n` less the number of +stored keys erased from `[a, b)`: the chain elements replace exactly the erased +keys below `b`. This is the anchor form of the interval perturbation's count +shift, the multi-key analogue of the single-key insertion's `+1`. -/ +theorem anchor_insertInterval_self {db : DB} {s : Selector} {a b : Key} + (hkey : s.key = (↑b : WithTop Key)) (hab : a ≤ b) (n : ℕ) + (hn : ∀ i < n, chain a i < b) : + s.anchor (db.insertInterval a b hab n hn) = + s.anchor db + n - (db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by + have hsplit : db.keys.countP (fun k => decide (k < b)) = + (db.keys.filter (fun k => decide (k < a))).length + + (db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by + rw [countP_lt_eq_add_filter db.keys db.nodup hab, List.countP_eq_length_filter] + unfold Selector.anchor + rw [hkey, countP_lt_coe, countP_lt_coe, countP_lt_insertInterval_self hab hn, hsplit] + push_cast + ring + +/-- When the selector is keyed at the upper end `b` of the erased interval, the +interval erasure lowers its anchor by the number of erased keys in `[a, b)`: +that is exactly the multi-key analogue of the single-key erasure's `-1`. -/ +theorem anchor_eraseInterval_self {db : DB} {s : Selector} {a b : Key} + (hkey : s.key = (↑b : WithTop Key)) (hab : a ≤ b) : + s.anchor (db.eraseInterval (↑a) (↑b)) = + s.anchor db - (db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by + have hsplit : db.keys.countP (fun k => decide (k < b)) = + (db.keys.filter (fun k => decide (k < a))).length + + (db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by + rw [countP_lt_eq_add_filter db.keys db.nodup hab, List.countP_eq_length_filter] + unfold Selector.anchor + rw [hkey, countP_lt_coe, countP_lt_coe, countP_lt_eraseInterval_self hab, hsplit] + push_cast + ring + +/-- For a positive integer, `toNat` commutes with subtracting one. -/ +theorem toNat_sub_one {z : ℤ} (h : 1 ≤ z) : (z - 1).toNat = z.toNat - 1 := by + rw [← Int.toNat_of_nonneg (show (0 : ℤ) ≤ z by omega)] + exact Int.toNat_sub z.toNat 1 + +/-- For an integer at least two, `toNat` commutes with subtracting two. -/ +theorem toNat_sub_two {z : ℤ} (h : 2 ≤ z) : (z - 2).toNat = z.toNat - 2 := by + have hz : (↑z.toNat : ℤ) = z := Int.toNat_of_nonneg (by omega) + conv_lhs => rw [← hz] + exact Int.toNat_sub z.toNat 2 + +/-- When a nonnegative anchor resolves to a stored key, the anchor's index holds +that key. -/ +theorem getElem?_anchor_eq_some {db : DB} {s : Selector} {a : Key} + (h0 : 0 ≤ s.anchor db) (h : resolve db s = (↑a : WithTop Key)) : + db.keys[(s.anchor db).toNat]? = some a := by + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0)] at h + cases hg : db.keys[(s.anchor db).toNat]? with + | none => rw [hg] at h; exact absurd h (by simp) + | some k => rw [hg] at h; rw [WithTop.coe_inj.mp h] + +/-- A selector keyed at `⊤` with a positive offset resolves to `⊤`: it targets +past every stored key, whatever the database. -/ +theorem resolve_eq_top_of_key_eq_top {db : DB} {s : Selector} + (h : s.key = (⊤ : WithTop Key)) (hoff : 1 ≤ s.offset) : + resolve db s = (⊤ : WithTop Key) := by + have h0 : 0 ≤ s.anchor db := by rw [anchor_of_top h]; omega + refine resolve_eq_top h0 ?_ + rw [anchor_toNat_eq hoff, h, countP_lt_top] + have hpos' : 0 ≤ (s.offset - 1).toNat := Nat.zero_le _ + omega + +/-- A selector keyed at the empty key with offset zero resolves to the empty +key: it targets the start of the key space. -/ +theorem resolve_eq_nil_of_key_eq_nil {db : DB} {s : Selector} + (h : s.key = (([] : Key) : WithTop Key)) (hoff : s.offset = 0) : + resolve db s = (↑([] : Key) : WithTop Key) := by + refine resolve_eq_nil ?_ + rw [anchor_of_nil h, hoff] + omega + +/-- Inserting the chain of `[a, b)` lets a selector keyed at `a` land on any +chosen chain element: with offset `1 + i` its anchor is the stored-key count +below `a` plus `i`, which the insertion fills with `chain a i`. This is the +resolve-level content of the interval perturbation, the multi-key analogue of a +single-key insertion placing the targeted key. -/ +theorem resolve_insertInterval_chain {db : DB} {s : Selector} {a b : Key} + (hkey : s.key = (↑a : WithTop Key)) (hab : a ≤ b) {n i : ℕ} + (hn : ∀ j < n, chain a j < b) (hoff : s.offset = (i : ℤ) + 1) (hi : i < n) : + resolve (db.insertInterval a b hab n hn) s = (↑(chain a i) : WithTop Key) := by + have hanc : s.anchor (db.insertInterval a b hab n hn) = + (((db.keys.filter (fun k => decide (k < a))).length + i : ℕ) : ℤ) := by + unfold Selector.anchor + rw [hkey, countP_lt_coe, countP_lt_insertInterval_of_lt hab (le_refl a) n hn, hoff, + List.countP_eq_length_filter] + push_cast + ring + have htoNat : (s.anchor (db.insertInterval a b hab n hn)).toNat = + (db.keys.filter (fun k => decide (k < a))).length + i := by + rw [hanc]; exact Int.toNat_natCast _ + have h0 : 0 ≤ s.anchor (db.insertInterval a b hab n hn) := by rw [hanc]; positivity + have hlt : (s.anchor (db.insertInterval a b hab n hn)).toNat < + (db.insertInterval a b hab n hn).keys.length := by + rw [htoNat, DB.insertInterval_keys, List.length_append, List.length_append, List.length_map, + List.length_range] + have hfb : (db.keys.filter (fun k => decide (k < b))).length ≤ db.keys.length := by + simpa using List.length_filter_le (fun k => decide (k < b)) db.keys + have hfa : (db.keys.filter (fun k => decide (k < a))).length ≤ db.keys.length := by + simpa using List.length_filter_le (fun k => decide (k < a)) db.keys + omega + rw [resolve_eq_getElem h0 hlt] + apply WithTop.coe_inj.mpr + have h2 : (db.insertInterval a b hab n hn).keys[ + (s.anchor (db.insertInterval a b hab n hn)).toNat]? = some (chain a i) := by + rw [htoNat] + exact getElem?_insertInterval_chain hab hn hi + rw [List.getElem?_eq_getElem hlt] at h2 + exact Option.some.inj h2 + +/-! +## Bounding a resolution by the selector's key + +These bounds pin the value `resolve` can produce between the count of stored keys +below the selector key and the successor chain. They are the database-relative +half of the footprint specification and are stated here beside `resolve`. +-/ + +/-- A selector keyed at a genuine `k₀` with positive offset can only resolve to +keys at or above `chain k₀ (offset - 1)`: the anchor puts `off - 1` chain steps +between the count of keys below `k₀` and the resolved key. -/ +theorem chain_le_of_resolve {s : Selector} {k₀ k : Key} + (hk : s.key = (↑k₀ : WithTop Key)) (hoff : 1 ≤ s.offset) {db : DB} + (h : resolve db s = (↑k : WithTop Key)) : + chain k₀ (s.offset - 1).toNat ≤ k := by + have h0 : 0 ≤ s.anchor db := by + rw [anchor_of_coe hk] + have := Int.natCast_nonneg (db.keys.countP (fun x => decide (x < k₀))) + omega + have hsome : db.keys[(s.anchor db).toNat]? = some k := getElem?_anchor_eq_some h0 h + obtain ⟨hidx, hget⟩ := List.getElem?_eq_some_iff.mp hsome + have ht : (s.anchor db).toNat = + db.keys.countP (fun x => decide (x < k₀)) + (s.offset - 1).toNat := by + rw [anchor_toNat_eq hoff, hk, countP_lt_coe] + have hidx' : db.keys.countP (fun x => decide (x < k₀)) + (s.offset - 1).toNat < + db.keys.length := by omega + have hstep := chain_le_getElem_countP db.sorted hidx' + have hsome' : db.keys[db.keys.countP (fun x => decide (x < k₀)) + + (s.offset - 1).toNat]? = some k := by + rw [← ht] + exact hsome + exact hstep.trans_eq (List.getElem?_eq_some_iff.mp hsome').2 + +/-- A selector keyed at a genuine `k₀` with a nonpositive offset can only +resolve to the empty key or to a key whose `-offset`-fold successor chain stays +strictly below `k₀`: the resolved key sits `-offset` indices below the count of +keys strictly below `k₀`, and a strictly increasing list has a distinct key +between consecutive chain elements. -/ +theorem eq_nil_or_chain_lt_of_resolve {s : Selector} {k₀ k : Key} + (hk : s.key = (↑k₀ : WithTop Key)) (hoff : s.offset ≤ 0) {db : DB} + (h : resolve db s = (↑k : WithTop Key)) : + k = [] ∨ chain k (-s.offset).toNat < k₀ := by + by_cases hneg : s.anchor db < 0 + · refine Or.inl ?_ + exact WithTop.coe_inj.mp (h.symm.trans (resolve_eq_nil hneg)) + · have h0 : 0 ≤ s.anchor db := by omega + have hsome : db.keys[(s.anchor db).toNat]? = some k := getElem?_anchor_eq_some h0 h + obtain ⟨hidx, hget⟩ := List.getElem?_eq_some_iff.mp hsome + have hCint : (db.keys.countP (fun x => decide (x < k₀)) : ℤ) = + ((s.anchor db).toNat : ℤ) + ((-s.offset).toNat : ℤ) + 1 := by + have hanc : (s.anchor db : ℤ) = + (db.keys.countP (fun x => decide (x < k₀)) : ℤ) + s.offset - 1 := anchor_of_coe hk + have hnat : ((s.anchor db).toNat : ℤ) = s.anchor db := Int.toNat_of_nonneg h0 + have hn : (((-s.offset).toNat : ℤ)) = -s.offset := Int.toNat_of_nonneg (by omega) + omega + have hC : db.keys.countP (fun x => decide (x < k₀)) = + (s.anchor db).toNat + (-s.offset).toNat + 1 := by omega + have hindex : (s.anchor db).toNat + (-s.offset).toNat < db.keys.length := by + have := List.countP_le_length (p := fun x : Key => decide (x < k₀)) (l := db.keys) + omega + have hle := chain_getElem_le db.sorted (i := (s.anchor db).toNat) + (j := (-s.offset).toNat) hidx hindex + have hltC : (s.anchor db).toNat + (-s.offset).toNat < + db.keys.countP (fun x => decide (x < k₀)) := by omega + have hlt : db.keys[(s.anchor db).toNat + (-s.offset).toNat] < k₀ := + getElem_lt_of_lt_countP db.sorted hindex hltC + rw [hget] at hle + exact Or.inr (lt_of_le_of_lt hle hlt) + +end FormalConflictRange diff --git a/FormalConflictRange/SimpleClosedForm.lean b/FormalConflictRange/SimpleClosedForm.lean new file mode 100644 index 0000000..60ce1e0 --- /dev/null +++ b/FormalConflictRange/SimpleClosedForm.lean @@ -0,0 +1,3208 @@ +/- +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 [closedFormCompleteCrossFree, topRange, hend, hcross, ht, hoff] + 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, if_neg (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, if_neg 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_neg 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_neg 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 +/-- **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 diff --git a/FormalConflictRange/Sound.lean b/FormalConflictRange/Sound.lean new file mode 100644 index 0000000..80f9f20 --- /dev/null +++ b/FormalConflictRange/Sound.lean @@ -0,0 +1,13103 @@ +/- +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.ConflictRange + +/-! +# Sound conflict ranges + +Reading a range returns a `Result`, which tells us something about the database: +in particular, which answer sets are still possible. A conflict range `cr` is +*sound* when all of that information is already determined by the keys stored +inside `cr`. Equivalently: any two databases that agree on `cr` agree on whether +the result is compatible with the read, so no inference drawn from the result can +be invalidated by a database matching the range. + +A conflict-range inference never sees the database contents: it sees only the +read `r` and the returned result `res`. The necessity rules therefore take the +compatibility hypothesis as `Readable r res` — some database is compatible with +the pair — and each proof obtains one witness database to reason with. + +Soundness is monotone under enlargement: a conflict range that contains (and so +agreement on which implies agreement on) a sound range is itself sound. Dually, +minimal ranges are the smallest sound ranges. +-/ + +namespace FormalConflictRange + +/-- Two databases agree on a conflict range when they store the same keys in it. -/ +def AgreeOn (cr : ConflictRange) (db₁ db₂ : DB) : Prop := + cr.keys db₁ = cr.keys db₂ + +namespace AgreeOn + +theorem refl (cr : ConflictRange) (db : DB) : AgreeOn cr db db := rfl + +theorem symm {cr : ConflictRange} {db₁ db₂ : DB} (h : AgreeOn cr db₁ db₂) : + AgreeOn cr db₂ db₁ := Eq.symm h + +theorem trans {cr : ConflictRange} {db₁ db₂ db₃ : DB} + (h₁ : AgreeOn cr db₁ db₂) (h₂ : AgreeOn cr db₂ db₃) : AgreeOn cr db₁ db₃ := + Eq.trans h₁ h₂ + +/-- Databases agree on a range after erasing an interval disjoint from it. This +is the interval analogue of `erase_of_not_mem`. -/ +theorem eraseInterval_of_disjoint {cr : ConflictRange} (db : DB) {a b : Key} + (hdisj : ∀ x, a ≤ x → x < b → x ∉ cr) : + AgreeOn cr db (db.eraseInterval (↑a) (↑b)) := + (ConflictRange.keys_eraseInterval_of_disjoint cr db hdisj).symm + +/-- Databases agree on a range after replacing an interval disjoint from it by a +successor chain. This is the interval analogue of the insert case of +`erase_of_not_mem`/`keys_insert_of_not_mem`. -/ +theorem insertInterval_of_disjoint {cr : ConflictRange} (db : DB) {a b : Key} + (hab : a ≤ b) (n : ℕ) (hn : ∀ i < n, chain a i < b) + (hdisj : ∀ x, a ≤ x → x < b → x ∉ cr) : + AgreeOn cr db (db.insertInterval a b hab n hn) := + (ConflictRange.keys_insertInterval_of_disjoint cr db hab n hn hdisj).symm + +/-- Databases agree on a range after erasing a key outside that range. This is the +`[k, keyAfter k)` case of `eraseInterval_of_disjoint`. -/ +theorem erase_of_not_mem {cr : ConflictRange} (db : DB) {k : Key} (hk : k ∉ cr) : + AgreeOn cr db (db.erase k) := + (ConflictRange.keys_erase_of_not_mem cr db hk).symm + +/-- Interval perturbation, packaged for the soundness analysis: a database can be +rebuilt to agree with a given one on any range disjoint from `[a, b)` while +realizing any gap `d` up to `intervalCard a b` in the count of stored keys below +`b`. This is the `AgreeOn` form of `exists_db_perturb_interval`, the batch +companion of the single-key `erase`/`insert` perturbations: the new database +keeps exactly the stored keys outside `[a, b)`, so it agrees on any range avoiding +the interval, while the count below `b` can be shifted by `d`. -/ +theorem exists_perturb_interval_of_disjoint {db : DB} {a b : Key} (hab : a ≤ b) (d : ℕ) + (hd : (d : ℕ∞) ≤ intervalCard (↑a : WithTop Key) (↑b : WithTop Key)) + {cr : ConflictRange} (hdisj : ∀ x, a ≤ x → x < b → x ∉ cr) : + ∃ db' : DB, AgreeOn cr db db' ∧ + db'.keys.countP (fun x => decide (x < b)) = + (db.keys.filter (fun x => decide (x < a))).length + d := by + obtain ⟨db', hlo, hhi, hcount⟩ := exists_db_perturb_interval (db := db) hab d hd + refine ⟨db', ?_, hcount⟩ + refine List.SortedLT.eq_of_mem_iff + (List.sortedLT_iff_pairwise.mpr (ConflictRange.keys_pairwise cr db)) + (List.sortedLT_iff_pairwise.mpr (ConflictRange.keys_pairwise cr db')) ?_ + intro x + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff] + have hxab : ∀ {y : Key}, y ∈ cr → y < a ∨ b ≤ y := by + intro y hycr + rcases lt_or_ge y a with h | h + · exact Or.inl h + · exact Or.inr (le_of_not_gt fun hyb => hdisj y h hyb hycr) + constructor + · rintro ⟨hx, hxcr⟩ + rcases hxab hxcr with h | h + · have : x ∈ db.keys.filter (fun x => decide (x < a)) := + List.mem_filter.mpr ⟨hx, by rw [decide_eq_true_eq]; exact h⟩ + rw [← hlo] at this + exact ⟨(List.mem_filter.mp this).1, hxcr⟩ + · have : x ∈ db.keys.filter (fun x => decide (b ≤ x)) := + List.mem_filter.mpr ⟨hx, by rw [decide_eq_true_eq]; exact h⟩ + rw [← hhi] at this + exact ⟨(List.mem_filter.mp this).1, hxcr⟩ + · rintro ⟨hx, hxcr⟩ + rcases hxab hxcr with h | h + · have : x ∈ db'.keys.filter (fun x => decide (x < a)) := + List.mem_filter.mpr ⟨hx, by rw [decide_eq_true_eq]; exact h⟩ + rw [hlo] at this + exact ⟨(List.mem_filter.mp this).1, hxcr⟩ + · have : x ∈ db'.keys.filter (fun x => decide (b ≤ x)) := + List.mem_filter.mpr ⟨hx, by rw [decide_eq_true_eq]; exact h⟩ + rw [hhi] at this + exact ⟨(List.mem_filter.mp this).1, hxcr⟩ + +/-- Selector control by interval perturbation, packaged for the soundness +analysis: a database can be rebuilt to agree with a given one on any range +disjoint from `[a, b)` while driving a selector keyed at `a` with offset `1 + j` +to resolve to the chain element `chain a j`, provided `[a, b)` is long enough. +This is the caller-facing form of `resolve_insertInterval_chain`: where the +single-key perturbations nudge the resolved key by one stored key, this moves it +to any chosen position in `[a, b)`. -/ +theorem exists_db_resolve_chain_of_disjoint {db : DB} {a b : Key} (hab : a ≤ b) + {s : Selector} (hkey : s.key = (↑a : WithTop Key)) {j : ℕ} + (hoff : s.offset = (j : ℤ) + 1) + (hj : ((j + 1 : ℕ) : ℕ∞) ≤ intervalCard (↑a : WithTop Key) (↑b : WithTop Key)) + {cr : ConflictRange} (hdisj : ∀ x, a ≤ x → x < b → x ∉ cr) : + ∃ db' : DB, AgreeOn cr db db' ∧ resolve db' s = (↑(chain a j) : WithTop Key) := by + have hn : ∀ i < j + 1, chain a i < b := fun i hi => + (chain_lt_iff a b i).mpr (lt_of_lt_of_le (by exact_mod_cast hi) hj) + exact ⟨db.insertInterval a b hab (j + 1) hn, + AgreeOn.insertInterval_of_disjoint db hab (j + 1) hn hdisj, + resolve_insertInterval_chain hkey hab hn hoff (by omega)⟩ + +end AgreeOn + +/-- A conflict range is sound for a read and result when every pair of databases +that agree on the range also agree on whether the result is compatible with the +read. -/ +def Sound (r : RangeRead) (res : Result) (cr : ConflictRange) : Prop := + ∀ db₁ db₂, AgreeOn cr db₁ db₂ → (RangeRead.Compatible db₁ r res ↔ + RangeRead.Compatible db₂ r res) + +/-- If a result is compatible with one database and the range is sound, then the +result is compatible with any database that agrees with it on the range. -/ +theorem Compatible.of_agreeOn {db₁ db₂ : DB} {r : RangeRead} {res : Result} + {cr : ConflictRange} (hread : RangeRead.Compatible db₁ r res) + (hsound : Sound r res cr) (hagree : AgreeOn cr db₁ db₂) : + RangeRead.Compatible db₂ r res := + (hsound db₁ db₂ hagree).mp hread + +/-- Agreement on a conflict range is pointwise agreement of membership on its keys. +This is the form in which the soundness filter is closed under intersections of key +sets, independent of their interval representation. -/ +theorem agreeOn_iff (cr : ConflictRange) (db₁ db₂ : DB) : + AgreeOn cr db₁ db₂ ↔ ∀ k, k ∈ cr → (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + constructor + · intro h k hkcr + rw [AgreeOn] at h + constructor + · intro hk1 + have hmem : k ∈ cr.keys db₁ := ConflictRange.mem_keys_iff.mpr ⟨hk1, hkcr⟩ + rw [h] at hmem + exact (ConflictRange.mem_keys_iff.mp hmem).1 + · intro hk2 + have hmem : k ∈ cr.keys db₂ := ConflictRange.mem_keys_iff.mpr ⟨hk2, hkcr⟩ + rw [← h] at hmem + exact (ConflictRange.mem_keys_iff.mp hmem).1 + · intro h + refine List.SortedLT.eq_of_mem_iff + (List.sortedLT_iff_pairwise.mpr (ConflictRange.keys_pairwise cr db₁)) + (List.sortedLT_iff_pairwise.mpr (ConflictRange.keys_pairwise cr db₂)) ?_ + intro k + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff] + exact ⟨fun hk => ⟨(h k hk.2).mp hk.1, hk.2⟩, fun hk => ⟨(h k hk.2).mpr hk.1, hk.2⟩⟩ + +/-- Soundness stated for an arbitrary set of keys rather than an interval: agreement +on `S` fixes compatibility. `sound_iff_soundSet` shows the range predicate is the +interval-represented case. This form is what makes the filter closed under set +intersections, which is not available for intervals. -/ +def SoundSet (r : RangeRead) (res : Result) (S : Set Key) : Prop := + ∀ db₁ db₂, (∀ k, k ∈ S → (k ∈ db₁.keys ↔ k ∈ db₂.keys)) → + (RangeRead.Compatible db₁ r res ↔ RangeRead.Compatible db₂ r res) + +/-- Range soundness is set-level soundness of its key set. -/ +theorem sound_iff_soundSet {r : RangeRead} {res : Result} {cr : ConflictRange} : + Sound r res cr ↔ SoundSet r res cr.toSet := by + constructor + · intro h db₁ db₂ hagree + exact h db₁ db₂ ((agreeOn_iff cr db₁ db₂).mpr hagree) + · intro h db₁ db₂ hagree + exact h db₁ db₂ ((agreeOn_iff cr db₁ db₂).mp hagree) + +/-- A key is necessary for a read and result when it lies in every sound conflict +range. These are exactly the keys any correct conflict range must track. -/ +def Necessary (r : RangeRead) (res : Result) (k : Key) : Prop := + ∀ cr, Sound r res cr → k ∈ cr + +/-- The set of keys necessary for a read and result: the intersection of every sound +range. Agreement on exactly this set is what the soundness filter determines, so it is +the natural non-interval form of the minimal range. -/ +def necessarySet (r : RangeRead) (res : Result) : Set Key := {k | Necessary r res k} + +/-- A read/result pair is realizable: some database is compatible with it. A +conflict-range inference sees only the read and the returned result, never the +database contents, so this — rather than a particular database — is the +hypothesis the necessity rules are stated with. -/ +def Readable (r : RangeRead) (res : Result) : Prop := + ∃ db, RangeRead.Compatible db r res + +/-- Necessity may be established from any one witness database; the conclusion +does not mention it, so an existential suffices. -/ +theorem Necessary.of_readable {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) + (h : ∀ db, RangeRead.Compatible db r res → Necessary r res k) : + Necessary r res k := + hread.elim fun db hdb => h db hdb + +/-- The necessary keys are convex: any key between two necessary keys is itself +necessary, since every sound range is an interval. -/ +theorem Necessary.of_between {r : RangeRead} {res : Result} {a b k : Key} + (ha : Necessary r res a) (hb : Necessary r res b) (hak : a ≤ k) (hkb : k ≤ b) : + Necessary r res k := + fun _ hs => ConflictRange.mem_of_between (ha _ hs) (hb _ hs) hak hkb + +/-- **The necessary set is order-convex.** Any key lying between two necessary keys is +itself necessary (`Necessary.of_between`), so the set of necessary keys is an interval of +genuine keys. This is the structural half of representability: the remaining question is +whether that interval's endpoints are attained at genuine keys (rather than at non-key +limits such as the cut above the chain `⊥, [0], [0,0], …`). -/ +theorem necessarySet_ordConnected (r : RangeRead) (res : Result) : + (necessarySet r res).OrdConnected := + Set.OrdConnected.mk fun _ ha _ hb _ hk => Necessary.of_between ha hb hk.1 hk.2 + +/-- Every key of the half-open interval `[a, b)` is necessary. This is the +interval form of `Necessary`: instead of one key at a time, it asks that a whole +interval be tracked, which is what an interval perturbation establishes at once. +For `a = cr.begin` and `b = cr.end_` it says `cr` is contained in every sound +range, i.e. every key of `cr` is necessary. -/ +def NecessaryRange (r : RangeRead) (res : Result) (a b : WithTop Key) : Prop := + ∀ k : Key, a ≤ (k : WithTop Key) → (k : WithTop Key) < b → Necessary r res k + +/-- A key that a compatible result reports is necessary: erasing it yields a +database that still agrees on any range avoiding it, but whose complete keys no +longer admit the result. An inference sees only `(r, res)`, so any one witness +database suffices. -/ +theorem necessary_of_mem_result {r : RangeRead} {res : Result} + (hread : Readable r res) {k : Key} (hk : k ∈ res.keys) : + Necessary r res k := by + obtain ⟨db₀, hread⟩ := hread + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₀ (db₀.erase k) := AgreeOn.erase_of_not_mem db₀ hkcr + have hcomp : RangeRead.Compatible (db₀.erase k) r res := (hs db₀ _ hagree).mp hread + exact DB.not_mem_erase_self db₀ k + (RangeRead.compatible_keys_subset hcomp hk) + +/-- Every key a compatible result reports lies in every sound conflict range. +An inference sees only `(r, res)`, so any one witness database suffices. -/ +theorem mem_of_sound_of_mem_result {r : RangeRead} {res : Result} + (hread : Readable r res) {cr : ConflictRange} (hs : Sound r res cr) : + ∀ k ∈ res.keys, k ∈ cr := + fun _ hk => necessary_of_mem_result hread hk cr hs + +/-- The set of reported keys is contained in every sound conflict range. -/ +theorem result_keys_subset_sound {db₀ : DB} {r : RangeRead} {res : Result} + (hread : RangeRead.Compatible db₀ r res) {cr : ConflictRange} (hs : Sound r res cr) : + res.keys ⊆ cr.keys db₀ := by + intro k hk + rw [ConflictRange.mem_keys_iff] + exact ⟨RangeRead.compatible_keys_subset hread hk, + mem_of_sound_of_mem_result ⟨db₀, hread⟩ hs k hk⟩ + +/-- Any key between two reported keys is necessary: every sound range is an +interval, so it contains everything spanning the reported keys. An inference +sees only `(r, res)`, so any one witness database suffices. -/ +theorem necessary_of_between_result {r : RangeRead} {res : Result} + (hread : Readable r res) {a b : Key} (ha : a ∈ res.keys) + (hb : b ∈ res.keys) {k : Key} (hak : a ≤ k) (hkb : k ≤ b) : Necessary r res k := + (necessary_of_mem_result hread ha).of_between + (necessary_of_mem_result hread hb) hak hkb + +/-- In a non-reversed read with `begin.offset ≥ 1` and a nonempty result, the +selector's key is necessary: any sound range must contain it. Perturbing the +database by toggling that key (without changing anything in a range that omits +it) shifts `resolve begin` off the result's head, so the range could not be +sound. An inference sees only `(r, res)`, so any one witness database +suffices. -/ +theorem necessary_begin_key {r : RangeRead} {res : Result} {v : Key} + (hread : Readable r res) (hrev : r.reverse = false) + (hlen : 0 < res.keys.length) (hoff : 1 ≤ r.begin.offset) + (hkey : r.begin.key = (↑v : WithTop Key)) : + Necessary r res v := by + obtain ⟨db₀, hread⟩ := hread + intro cr hs + by_contra hcr + have hresne : res.keys ≠ [] := List.ne_nil_of_length_pos hlen + have hne : RangeRead.completeKeys db₀ r ≠ [] := + (RangeRead.compatible_keys_prefix hread).ne_nil hresne + have hpos : 0 ≤ r.begin.anchor db₀ := anchor_nonneg_of_one_le hoff + obtain ⟨a, _ha_mem, ha_res, ha_head⟩ := + RangeRead.completeKeys_head_resolve hrev hne hpos + have hreshead : res.keys.head hresne = a := by + rw [← ha_head] + exact (RangeRead.compatible_keys_prefix hread).head hresne + set c := db₀.keys.countP (fun k => decide (k < v)) with hc_def + set t := (r.begin.offset - 1).toNat with ht_def + set j := c + t with hj_def + have hcount_toNat : (r.begin.anchor db₀).toNat = j := by + rw [anchor_toNat_eq hoff, hkey, countP_lt_coe, ← hc_def, ← ht_def, ← hj_def] + have hget := getElem?_anchor_eq_some hpos ha_res + rw [hcount_toNat, List.getElem?_eq_some_iff] at hget + have hjlt : j < db₀.keys.length := hget.1 + have ha_eq : a = db₀.keys[j] := hget.2.symm + have hcle : c ≤ j := by rw [hj_def]; exact Nat.le_add_right _ _ + have hclt : c < db₀.keys.length := lt_of_le_of_lt hcle hjlt + have key_neq (db₁ : DB) (hanc : (r.begin.anchor db₁).toNat = j) + (hneq : db₁.keys[j]? ≠ some a) : resolve db₁ r.begin ≠ (↑a : WithTop Key) := by + intro h + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr (anchor_nonneg_of_one_le hoff)), hanc] at h + cases hg : db₁.keys[j]? with + | none => rw [hg] at h; exact absurd h (by simp) + | some k => + rw [hg] at h + exact hneq (by rw [hg, WithTop.coe_inj.mp h]) + have contra (db₁ : DB) (hagree : AgreeOn cr db₀ db₁) + (hanc : (r.begin.anchor db₁).toNat = j) (hneq : db₁.keys[j]? ≠ some a) : False := by + have compat₁ : RangeRead.Compatible db₁ r res := (hs db₀ db₁ hagree).mp hread + have hpre₁ := RangeRead.compatible_keys_prefix compat₁ + have hne₁ : RangeRead.completeKeys db₁ r ≠ [] := hpre₁.ne_nil hresne + obtain ⟨a', _, ha'_res, ha'_head⟩ := + RangeRead.completeKeys_head_resolve hrev hne₁ (anchor_nonneg_of_one_le hoff) + have hhead₁ : (RangeRead.completeKeys db₁ r).head hne₁ = a := by + rw [← hreshead] + exact (hpre₁.head hresne).symm + have hresolve : resolve db₁ r.begin = (↑a : WithTop Key) := by + rw [ha'_res, ha'_head.symm.trans hhead₁] + exact key_neq db₁ hanc hneq hresolve + by_cases hvm : v ∈ db₀.keys + · have hcv : db₀.keys[c] = v := getElem_countP_lt db₀.sorted hvm + let db₁ : DB := db₀.erase v + have hkeys₁ : db₁.keys = db₀.keys.eraseIdx c := by + change db₀.keys.erase v = db₀.keys.eraseIdx c + rw [← hcv] + exact List.Nodup.erase_getElem db₀.nodup c hclt + have hagree : AgreeOn cr db₀ db₁ := + (ConflictRange.keys_erase_of_not_mem cr db₀ hcr).symm + have hcount' : db₁.keys.countP (fun k => decide (k < v)) = c := by + rw [hkeys₁]; exact eraseIdx_countP db₀.sorted hclt hcv + have hanc' : (r.begin.anchor db₁).toNat = j := by + rw [anchor_toNat_eq hoff, hkey, countP_lt_coe, hcount', hj_def] + refine contra db₁ hagree hanc' ?_ + intro hk + rw [hkeys₁, List.getElem?_eraseIdx_of_ge hcle] at hk + cases hg : db₀.keys[j+1]? with + | none => rw [hg] at hk; exact absurd hk (by simp) + | some k => + rw [hg, Option.some.injEq] at hk + have hge := List.getElem?_eq_some_iff.mp hg + have heq : db₀.keys[j+1]'(hge.1) = db₀.keys[j] := by rw [hge.2, hk, ha_eq] + exact absurd ((db₀.nodup.getElem_inj_iff).mp heq) (by omega) + · let db₁ : DB := db₀.insert v hvm + have hkeys₁ : db₁.keys = db₀.keys.insertIdx c v := by + change db₀.keys.orderedInsert (· ≤ ·) v = _ + exact orderedInsert_eq_insertIdx db₀.sorted hvm + have hagree : AgreeOn cr db₀ db₁ := + (ConflictRange.keys_insert_of_not_mem cr db₀ hvm hcr).symm + have hcount' : db₁.keys.countP (fun k => decide (k < v)) = c := by + rw [hkeys₁]; exact countP_insertIdx _ c v (by simp) + have hanc' : (r.begin.anchor db₁).toNat = j := by + rw [anchor_toNat_eq hoff, hkey, countP_lt_coe, hcount', hj_def] + refine contra db₁ hagree hanc' ?_ + rcases lt_or_eq_of_le hcle with hjc | hjc + · intro hk + rw [hkeys₁, List.getElem?_insertIdx_of_gt hjc] at hk + cases hg : db₀.keys[j-1]? with + | none => rw [hg] at hk; exact absurd hk (by simp) + | some k => + rw [hg, Option.some.injEq] at hk + have hge := List.getElem?_eq_some_iff.mp hg + have : db₀.keys[j-1] < db₀.keys[j] := + db₀.sorted.getElem_lt_getElem_of_lt (by omega) + rw [hge.2, hk, ha_eq] at this + exact absurd this (lt_irrefl _) + · intro hk + rw [hkeys₁, hjc, List.getElem?_insertIdx_self] at hk + rw [ite_eq_left (le_of_lt hjlt), Option.some.injEq] at hk + have ha_ne : a ≠ v := by + intro hav + exact hvm (hav ▸ (ha_eq ▸ List.getElem_mem hjlt)) + exact ha_ne hk.symm + +/-- In a reversed read with `end_.offset ≤ 1` (so `resolve end_` is the last +stored key strictly below `end_.key` at offset zero, or the first key at or +above it at offset one) and a nonempty result, the range +`[res.keys[0], end_.key)` is necessary: every sound conflict range +contains every key of it. Erasing a stored key of the range drops +`resolve end_` past the result's head, and inserting an unstored one splits +the reversed prefix, so no database agreeing on a sound range that omits it +can stay compatible. An inference sees only `(r, res)`, so any one witness +database suffices. -/ +theorem necessary_head_to_end_key {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hoff : r.end_.offset ≤ 1) + (cr : ConflictRange) (hs : Sound r res cr) + {x : Key} (hlo : res.keys.head hne ≤ x) (hhi : (x : WithTop Key) < r.end_.key) : + x ∈ cr := by + obtain ⟨db₀, hread⟩ := hread + rcases eq_or_ne x (res.keys.head hne) with hx | hxa + · exact necessary_of_mem_result ⟨db₀, hread⟩ (by rw [hx]; exact List.head_mem hne) cr hs + · have hax : res.keys.head hne < x := lt_of_le_of_ne hlo (Ne.symm hxa) + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hread + have hne₀ : RangeRead.completeKeys db₀ r ≠ [] := hpre.ne_nil hne + have hA₀ : 1 ≤ r.end_.anchor db₀ := RangeRead.one_le_anchor_of_completeKeys_ne_nil hne₀ + set m := (r.end_.anchor db₀).toNat with hm_def + have hmlt : m - 1 < db₀.keys.length := by + have h := RangeRead.end_anchor_toNat_le (db := db₀) (r := r) hoff + omega + -- The heads of all compatible complete-key lists agree with the result's head. + have head?_prefix : ∀ (db : DB), res.keys <+: RangeRead.completeKeys db r → + (RangeRead.completeKeys db r).head? = res.keys.head? := by + intro db hpre' + obtain ⟨t, ht⟩ := hpre' + rw [← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl, + Option.some_or] + have hhead?₀ : (RangeRead.completeKeys db₀ r).head? = some (db₀.keys[m-1]'hmlt) := by + rw [RangeRead.completeKeys_head?_rev hrev hoff hne₀] + rw [show (r.end_.anchor db₀ - 1).toNat = m - 1 by + rw [toNat_sub_one hA₀, ← hm_def]] + exact List.getElem?_eq_getElem hmlt + have hreshead? : res.keys.head? = some (db₀.keys[m-1]'hmlt) := + (head?_prefix db₀ hpre).symm.trans hhead?₀ + have hhead₀ : res.keys.head hne = db₀.keys[m-1]'hmlt := + (List.head_eq_iff_head?_eq_some hne).mpr hreshead? + have hlt : db₀.keys[m-1]'hmlt < x := by rw [← hhead₀]; exact hax + -- Any database compatible with the same result stores `x`'s predecessor at + -- the index `end_.anchor - 1` derived from its own key count. + have key?_eq : ∀ (db : DB) (hne' : RangeRead.completeKeys db r ≠ []) {j : Nat}, + j = (r.end_.anchor db - 1).toNat → res.keys <+: RangeRead.completeKeys db r → + db.keys[j]? = db₀.keys[m-1]? := by + intro db hne' j hj hpre' + calc db.keys[j]? = (RangeRead.completeKeys db r).head? := by + rw [RangeRead.completeKeys_head?_rev hrev hoff hne', hj] + _ = res.keys.head? := head?_prefix db hpre' + _ = some (db₀.keys[m-1]'hmlt) := hreshead? + _ = db₀.keys[m-1]? := (List.getElem?_eq_getElem hmlt).symm + by_contra hcr + by_cases hxm : x ∈ db₀.keys + · -- `x` is stored: erasing it drops the anchor by one, pulling the + -- predecessor of `x` into the index that `x` itself used to occupy. + set c := db₀.keys.countP (fun k => decide (k < x)) with hc_def + have hcval : db₀.keys[c]? = some x := by + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hxm + have hce : c = i := by rw [hc_def, countP_lt_eq_of_getElem db₀.sorted hi hix] + rw [hce, List.getElem?_eq_getElem hi, hix] + obtain ⟨hclt, hc_eq⟩ := List.getElem?_eq_some_iff.mp hcval + have hclen : c ≤ db₀.keys.length := by rw [hc_def]; exact List.countP_le_length + have hjc : m ≤ c := by + have h1 : m - 1 < c := by + rw [hc_def] + exact lt_countP_of_getElem_lt db₀.sorted hmlt hlt + omega + let db₁ : DB := db₀.erase x + have hkeys₁ : db₁.keys = db₀.keys.eraseIdx c := by + change db₀.keys.erase x = db₀.keys.eraseIdx c + rw [← hc_eq] + exact List.Nodup.erase_getElem db₀.nodup c hclt + have hagree : AgreeOn cr db₀ db₁ := + (ConflictRange.keys_erase_of_not_mem cr db₀ hcr).symm + have compat₁ : RangeRead.Compatible db₁ r res := (hs db₀ db₁ hagree).mp hread + have hne₁ : RangeRead.completeKeys db₁ r ≠ [] := + (RangeRead.compatible_keys_prefix compat₁).ne_nil hne + have hJ1le : 1 ≤ db₀.keys.countP + (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) := + countP_pos_of_mem hxm (by rw [decide_eq_true_eq]; exact hhi) + have hJ₁ : db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + db₀.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) - 1 := by + change (db₀.keys.erase x).countP + (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + db₀.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) - 1 + rw [List.countP_erase, ite_eq_left ⟨hxm, by rw [decide_eq_true_eq]; exact hhi⟩] + have hanc₁ : r.end_.anchor db₁ = r.end_.anchor db₀ - 1 := by + unfold Selector.anchor + rw [hJ₁, Nat.cast_sub hJ1le] + ring + have hanc2 : (2 : ℤ) ≤ r.end_.anchor db₀ := by + have h1 := RangeRead.one_le_anchor_of_completeKeys_ne_nil (db := db₁) (r := r) hne₁ + rw [hanc₁] at h1 + omega + have hk₁ : (r.end_.anchor db₁ - 1).toNat = m - 2 := by + have h2 : r.end_.anchor db₁ - 1 = r.end_.anchor db₀ - 2 := by rw [hanc₁]; ring + rw [h2, toNat_sub_two hanc2, hm_def] + have hbad : db₁.keys[(r.end_.anchor db₁ - 1).toNat]? = db₀.keys[m-1]? := + key?_eq db₁ hne₁ rfl (RangeRead.compatible_keys_prefix compat₁) + have hbad' : (db₀.keys.eraseIdx c)[m-2]? = db₀.keys[m-1]? := by + rw [hk₁, hkeys₁] at hbad + exact hbad + have hb2 : db₀.keys[m-2]? = db₀.keys[m-1]? := by + rw [List.getElem?_eraseIdx_of_lt (show m - 2 < c by omega)] at hbad' + exact hbad' + exact absurd + ((db₀.nodup.getElem?_inj (show m - 2 < db₀.keys.length by omega)).mp hb2) + (by omega) + · -- `x` is unstored: inserting it at its rank raises the anchor by one, so + -- the index that used to hold `x`'s predecessor now holds a larger key. + set c := db₀.keys.countP (fun k => decide (k < x)) with hc_def + have hclen : c ≤ db₀.keys.length := by rw [hc_def]; exact List.countP_le_length + have hjc : m ≤ c := by + have h1 : m - 1 < c := by + rw [hc_def] + exact lt_countP_of_getElem_lt db₀.sorted hmlt hlt + omega + let db₁ : DB := db₀.insert x hxm + have hkeys₁ : db₁.keys = db₀.keys.insertIdx c x := by + change db₀.keys.orderedInsert (· ≤ ·) x = _ + exact orderedInsert_eq_insertIdx db₀.sorted hxm + have hagree : AgreeOn cr db₀ db₁ := + (ConflictRange.keys_insert_of_not_mem cr db₀ hxm hcr).symm + have compat₁ : RangeRead.Compatible db₁ r res := (hs db₀ db₁ hagree).mp hread + have hne₁ : RangeRead.completeKeys db₁ r ≠ [] := + (RangeRead.compatible_keys_prefix compat₁).ne_nil hne + have hJ₁ : db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + db₀.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) + 1 := by + rw [hkeys₁] + exact countP_insertIdx_of_lt db₀.sorted hhi + have hanc₁ : r.end_.anchor db₁ = r.end_.anchor db₀ + 1 := by + unfold Selector.anchor + rw [hJ₁, Nat.cast_add, Nat.cast_one] + ring + have hk₁ : (r.end_.anchor db₁ - 1).toNat = m := by + have h2 : r.end_.anchor db₁ - 1 = r.end_.anchor db₀ := by rw [hanc₁]; ring + rw [h2, hm_def] + have hbad : db₁.keys[(r.end_.anchor db₁ - 1).toNat]? = db₀.keys[m-1]? := + key?_eq db₁ hne₁ rfl (RangeRead.compatible_keys_prefix compat₁) + have hbad' : (db₀.keys.insertIdx c x)[m]? = db₀.keys[m-1]? := by + rw [hk₁, hkeys₁] at hbad + exact hbad + rcases lt_or_eq_of_le hjc with hltmc | heq + · -- `x` sits strictly after index `m`, so index `m` shifted down onto its + -- own predecessor: impossible in a duplicate-free list. + have hstep : db₀.keys[m]? = db₀.keys[m-1]? := by + rw [List.getElem?_insertIdx_of_lt hltmc] at hbad' + exact hbad' + exact absurd + ((db₀.nodup.getElem?_inj (show m < db₀.keys.length by omega)).mp hstep) + (by omega) + · -- `x` sits exactly at index `m`, so it would be the result's head, which + -- the hypothesis `hxa` excludes. + have hsome : (db₀.keys.insertIdx c x)[m]? = some x := by + rw [heq, List.getElem?_insertIdx_self, ite_eq_left hclen] + have hxval : db₀.keys[m-1]'hmlt = x := by + refine Option.some.inj ?_ + rw [← List.getElem?_eq_getElem hmlt, ← hbad'] + exact hsome + exact absurd (hhead₀.trans hxval) (Ne.symm hxa) + +/-- Reversal is invisible to compatibility once the result is complete: flipping +the traversal order and reversing the reported keys gives an equally compatible +read, since `more = false` forces the keys to be the complete keys and reversal +commutes with `completeKeys`. -/ +theorem compatible_flip {db : DB} {r : RangeRead} {res : Result} {b : Bool} + (hrev : r.reverse = b) (hmore : res.more = false) : + (RangeRead.Compatible db {r with reverse := !b} + {keys := res.keys.reverse, more := false} ↔ RangeRead.Compatible db r res) := by + have hck : RangeRead.completeKeys db {r with reverse := !b} = + (RangeRead.completeKeys db r).reverse := by + cases b <;> simp [RangeRead.completeKeys, hrev] + rw [RangeRead.Compatible, RangeRead.Compatible, hmore, hck] + simp only [RangeRead.withinLimit, List.length_reverse, Bool.false_eq_true, false_and, + true_and, false_or, List.reverse_inj] + +/-- Soundness is invariant under flipping the traversal order and reversing the +reported keys: each database's compatibility is preserved by `compatible_flip`. -/ +theorem sound_flip {r : RangeRead} {res : Result} {cr : ConflictRange} {b : Bool} + (hrev : r.reverse = b) (hmore : res.more = false) : + (Sound {r with reverse := !b} {keys := res.keys.reverse, more := false} cr ↔ + Sound r res cr) := by + constructor + · intro h db₁ db₂ hagree + rw [← compatible_flip (db := db₁) hrev hmore, ← compatible_flip (db := db₂) hrev hmore] + exact h db₁ db₂ hagree + · intro h db₁ db₂ hagree + rw [compatible_flip (db := db₁) hrev hmore, compatible_flip (db := db₂) hrev hmore] + exact h db₁ db₂ hagree + +/-- **Necessity is invariant under flipping the traversal order of a complete read.** +The sound ranges of `r` and of its flip are the same (`sound_flip`), so a key lies in +all of them for one exactly when it does for the other. Completeness (`more = false`) is +what lets the flip preserve soundness; for a truncated result the flipped result is a +suffix rather than a prefix, so this reduction does not apply. -/ +theorem necessary_flip {r : RangeRead} {res : Result} {k : Key} {b : Bool} + (hrev : r.reverse = b) (hmore : res.more = false) : + (Necessary {r with reverse := !b} {keys := res.keys.reverse, more := false} k ↔ + Necessary r res k) := + ⟨fun h cr hs => h cr ((sound_flip hrev hmore).mpr hs), + fun h cr hs => h cr ((sound_flip hrev hmore).mp hs)⟩ + +/-- In a non-reversed read that is complete (`more = false`), the keys from the +result's last key up to `end_.key` are necessary: since the result reports every +in-range key, perturbing a key in `[res.keys.getLast, end_.key)` shifts +`resolve end_` and changes the complete keys, so no sound range can omit it. +An inference sees only `(r, res)`, so any one witness database suffices. -/ +theorem necessary_last_to_end_key {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hoff : r.end_.offset ≤ 1) + (cr : ConflictRange) (hs : Sound r res cr) + {x : Key} (hlo : res.keys.getLast hne ≤ x) (hhi : (x : WithTop Key) < r.end_.key) : + x ∈ cr := by + obtain ⟨db₀, hread⟩ := hread + exact necessary_head_to_end_key (r := {r with reverse := true}) + (res := {keys := res.keys.reverse, more := false}) + ⟨db₀, (compatible_flip (db := db₀) (b := false) hrev hmore).mpr hread⟩ (by simp) + (by simpa using hne) hoff cr ((sound_flip (b := false) hrev hmore).mpr hs) + (by simpa using hlo) (by simpa using hhi) + +/-- In a reversed read that is complete (`more = false`) and has `begin.offset ≥ 1`, +the selector's key is necessary: any sound range must contain it. Completeness +ensures the result reaches the begin end of the range, so perturbing `begin.key` +shifts `resolve begin` and changes the reported keys. An inference sees only +`(r, res)`, so any one witness database suffices. -/ +theorem necessary_begin_key_rev {r : RangeRead} {res : Result} {v : Key} + (hread : Readable r res) (hrev : r.reverse = true) + (hmore : res.more = false) (hlen : 0 < res.keys.length) + (hoff : 1 ≤ r.begin.offset) (hkey : r.begin.key = (↑v : WithTop Key)) : + Necessary r res v := by + obtain ⟨db₀, hread⟩ := hread + have hbase : Necessary {r with reverse := false} {keys := res.keys.reverse, more := false} + v := + necessary_begin_key (r := {r with reverse := false}) + (res := {keys := res.keys.reverse, more := false}) + ⟨db₀, (compatible_flip (db := db₀) (b := true) hrev hmore).mpr hread⟩ rfl + (by simpa using hlen) hoff hkey + exact fun cr hs => hbase cr ((sound_flip (b := true) hrev hmore).mpr hs) + +/-- In a non-reversed read with `begin.offset ≤ 0` and a nonempty result whose head +is at or above `begin.key`, the empty key is necessary: every sound conflict range +must contain it. A database compatible with the result cannot store the empty key, +since being least it would head the window, which begins at or below `begin.key`; +inserting it into a database a range omits it from keeps agreement, so that range +would have to be unsound. An inference sees only `(r, res)`, so any one witness +database suffices. -/ +theorem necessary_nil_of_begin_offset_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hoff : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res ([] : Key) := by + obtain ⟨db₀, hread⟩ := hread + -- A database compatible with `res` cannot store the empty key: the window starts + -- at or below `begin.key`, and the empty key is least, so it would head it. + have hkey : ∀ db : DB, RangeRead.Compatible db r res → res.keys.head hne ≠ [] → + ([] : Key) ∉ db.keys := by + intro db hdb hnea h0 + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hdb + have hne_ck : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne + have hhe : resolve db r.begin ≤ (↑(res.keys.head hne) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (hpre.subset (List.head_mem hne))).2.1 + have hlt_hi : resolve db r.begin < resolve db r.end_ := by + obtain ⟨a, ha⟩ := List.exists_mem_of_ne_nil _ hne_ck + exact lt_of_le_of_lt (RangeRead.mem_completeKeys.mp ha).2.1 + (RangeRead.mem_completeKeys.mp ha).2.2 + -- A stored key `a` that `resolve begin` lands on heads the window: it is + -- minimal there, and the window starts at it. + have hhead_eq : ∀ a : Key, resolve db r.begin = (↑a : WithTop Key) → a ∈ db.keys → + res.keys.head hne = a := by + intro a ha hmem + have halt : (↑a : WithTop Key) < resolve db r.end_ := by rw [← ha]; exact hlt_hi + rw [hpre.head hne] + exact RangeRead.completeKeys_head_eq hrev hne_ck ha + (RangeRead.mem_completeKeys.mpr ⟨hmem, by rw [ha], halt⟩) + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with + hn | ht | ⟨k, hkdb, hkres⟩ + · exact hnea (hhead_eq ([] : Key) hn h0) + · rw [ht] at hhe + exact absurd hhe (not_le.mpr (WithTop.coe_lt_top (res.keys.head hne))) + · by_cases hneg : r.begin.anchor db < 0 + · have hk_nil : k = ([] : Key) := WithTop.coe_inj.mp + (hkres.symm.trans (resolve_eq_nil (db := db) (s := r.begin) hneg)) + exact hnea ((hhead_eq k hkres hkdb).trans hk_nil) + · -- A nonnegative anchor holds the key below `begin.key`'s count of stored + -- keys below it, hence below `begin.key` itself. + have hpos : 0 ≤ r.begin.anchor db := not_lt.mp hneg + obtain ⟨hnlt, hidx⟩ := List.getElem?_eq_some_iff.mp + (getElem?_anchor_eq_some hpos hkres) + have hlt2 : (r.begin.anchor db).toNat < + db.keys.countP (fun (x : Key) => decide ((↑x : WithTop Key) < r.begin.key)) := by + have hcast : ((r.begin.anchor db).toNat : ℤ) = r.begin.anchor db := + Int.toNat_of_nonneg hpos + have hltI : ((r.begin.anchor db).toNat : ℤ) < + ((db.keys.countP + (fun (x : Key) => decide ((↑x : WithTop Key) < r.begin.key)) : ℤ)) := by + rw [hcast] + unfold Selector.anchor + omega + exact_mod_cast hltI + have hlt_k : ((k : WithTop Key) < r.begin.key) := by + rw [← hidx] + exact getElem_lt_of_lt_countP_top db.sorted hnlt hlt2 + have hhead : res.keys.head hne = k := hhead_eq k hkres hkdb + rw [hhead] at hlo + exact absurd (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlt_k hlo)) (lt_irrefl k) + intro cr hs + by_contra hcr + rcases eq_or_ne ([] : Key) (res.keys.head hne) with hx | hxa + · exact hcr (necessary_of_mem_result ⟨db₀, hread⟩ (by rw [hx]; exact List.head_mem hne) + cr hs) + · by_cases h0 : ([] : Key) ∈ db₀.keys + · exact hkey db₀ hread (Ne.symm hxa) h0 + · -- Inserting the omitted empty key preserves agreement on the range while + -- making the database incompatible, so the range was not sound. + have hagree : AgreeOn cr db₀ (db₀.insert ([] : Key) h0) := + (ConflictRange.keys_insert_of_not_mem cr db₀ h0 hcr).symm + exact hkey (db₀.insert ([] : Key) h0) ((hs db₀ _ hagree).mp hread) (Ne.symm hxa) + (DB.mem_insert_self db₀ ([] : Key) h0) + +/-- In a reversed complete read (`more = false`) with `begin.offset ≤ 0` and a +nonempty result whose last key is at or above `begin.key`, the empty key is +necessary: every sound conflict range must contain it. Reversal makes the +result's last key its least, so flipping back to a non-reversed read reduces this +to `necessary_nil_of_begin_offset_nonpos`. An inference sees only `(r, res)`, +so any one witness database suffices. -/ +theorem necessary_nil_of_begin_offset_nonpos_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hoff : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) : + Necessary r res ([] : Key) := by + obtain ⟨db₀, hread⟩ := hread + have hbase : Necessary {r with reverse := false} {keys := res.keys.reverse, more := false} + ([] : Key) := + necessary_nil_of_begin_offset_nonpos (r := {r with reverse := false}) + (res := {keys := res.keys.reverse, more := false}) + ⟨db₀, (compatible_flip (db := db₀) (b := true) hrev hmore).mpr hread⟩ rfl + (by simpa using hne) hoff + (by simpa [List.head_reverse] using hlo) + exact fun cr hs => hbase cr ((sound_flip (b := true) hrev hmore).mpr hs) + +/-- **The lower endpoint is attained at the empty key once the empty key is necessary.** +The empty key is the least key, so if it is necessary it is the least necessary key. This is +the trivial half of endpoint attainment for the nonpositive-begin regions, where +`necessary_nil_of_begin_offset_nonpos` and its reversed twin supply the hypothesis. -/ +theorem exists_least_necessary_of_nil {r : RangeRead} {res : Result} + (h : Necessary r res ([] : Key)) : + ∃ A : Key, Necessary r res A ∧ ∀ k : Key, Necessary r res k → A ≤ k := + ⟨[], h, fun k _ => nil_le_key k⟩ + +/-- **Lower endpoint attained for the nonpositive-begin cross-free forward region.** With +`begin.offset ≤ 0` and `begin.key` at or below the reported head, the empty key is necessary +(`necessary_nil_of_begin_offset_nonpos`) and, being least, is the least necessary key. This +is the nonpositive-offset analogue of `exists_least_necessary_of_one_le_le`. -/ +theorem exists_least_necessary_of_begin_offset_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hoff : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) : + ∃ A : Key, Necessary r res A ∧ ∀ k : Key, Necessary r res k → A ≤ k := + exists_least_necessary_of_nil (necessary_nil_of_begin_offset_nonpos hread hrev hne hoff hlo) + +/-- **Lower endpoint attained for the nonpositive-begin cross-free reversed region.** The +reversal-symmetric twin of `exists_least_necessary_of_begin_offset_nonpos`, with the result's +last key in place of its head and `necessary_nil_of_begin_offset_nonpos_rev` supplying the +necessary empty key. -/ +theorem exists_least_necessary_of_begin_offset_nonpos_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = false) + (hne : res.keys ≠ []) (hoff : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) : + ∃ A : Key, Necessary r res A ∧ ∀ k : Key, Necessary r res k → A ≤ k := + exists_least_necessary_of_nil + (necessary_nil_of_begin_offset_nonpos_rev hread hrev hmore hne hoff hlo) + + +/-- A key lies in the window `inRange db.keys lo hi` exactly when it is stored and +lies in `[lo, hi)`. -/ +private theorem mem_inRange {db : DB} {lo hi : WithTop Key} {k : Key} : + k ∈ RangeRead.inRange db.keys lo hi ↔ + k ∈ db.keys ∧ lo ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < hi := by + unfold RangeRead.inRange + rw [List.mem_filter, decide_eq_true_eq] + +/-- Two strictly increasing key lists listing the same keys are equal. -/ +theorem eq_of_forall_mem_iff_of_pairwise {l₁ l₂ : List Key} + (h₁ : l₁.Pairwise (· < ·)) (h₂ : l₂.Pairwise (· < ·)) + (h : ∀ k, k ∈ l₁ ↔ k ∈ l₂) : l₁ = l₂ := + List.Perm.eq_of_pairwise (fun _ _ _ _ hab hba => (lt_asymm hab hba).elim) h₁ h₂ + ((List.subperm_of_subset h₁.nodup (fun _ ha => (h _).mp ha)).antisymm + (List.subperm_of_subset h₂.nodup (fun _ ha => (h _).mpr ha))) + +/-- Under the library's existing hypotheses for the empty key, every key strictly +below the reported head is necessary: `of_between` chains the empty-key theorem with +the reported head's own necessity. -/ +theorem necessary_below_head_of_begin_offset_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hoff : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) {k : Key} + (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := + (necessary_nil_of_begin_offset_nonpos hread hrev hne hoff hlo).of_between + (necessary_of_mem_result hread (List.head_mem hne)) + (nil_le_key k) + (le_of_lt (WithTop.coe_lt_coe.mp hk)) + +/-- The last element of a strictly increasing list is at or above every element. -/ +private theorem pairwise_le_getLast {l : List Key} (h : l.Pairwise (· < ·)) {a : Key} + (ha : a ∈ l) {hne : l ≠ []} : a ≤ l.getLast hne := + (h.imp le_of_lt).rel_getLast ha + +/-- For a reversed read, the reported keys head the reversed window, so the reported +head question reduces to the window's last element. -/ +private theorem head?_eq_getLast?_of_prefix_rev {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hpre : res.keys <+: RangeRead.completeKeys db r) + (hne : res.keys ≠ []) : + res.keys.head? = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast? := by + have hck : RangeRead.completeKeys db r = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by + simp [RangeRead.completeKeys, hrev] + obtain ⟨t, ht⟩ := hpre + rw [← ht] at hck + calc res.keys.head? = (res.keys ++ t).head? := (List.head?_append_of_ne_nil _ hne).symm + _ = (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse.head? := by + rw [hck] + _ = (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast? := + List.head?_reverse + +/-- In a reversed read, every complete key is at or below the reported head. -/ +private theorem le_head_of_mem_completeKeys_rev {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hpre : res.keys <+: RangeRead.completeKeys db r) + (hne : res.keys ≠ []) {k : Key} (hk : k ∈ RangeRead.completeKeys db r) : + k ≤ res.keys.head hne := by + have hck : RangeRead.completeKeys db r = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by + simp [RangeRead.completeKeys, hrev] + have hWne : RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) ≠ [] := + fun h => hpre.ne_nil hne (hck.trans (List.reverse_eq_nil_iff.mpr h)) + rw [hck, List.mem_reverse] at hk + have h1 : k ≤ (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast + hWne := pairwise_le_getLast (RangeRead.inRange_pairwise db _ _) hk + have h2 : res.keys.head hne = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).getLast hWne := by + refine (List.head_eq_iff_head?_eq_some hne).mpr ?_ + rw [head?_eq_getLast?_of_prefix_rev hrev hpre hne, + List.getLast?_eq_getLast_of_ne_nil hWne] + rw [h2] + exact h1 + +/-- If every stored key of `db` is at or below `x` and the read's complete keys are +nonempty, then `resolve begin` is at or below `x`. -/ +private theorem resolve_begin_le {db : DB} {r : RangeRead} {x : Key} + (h : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key)) + (hne : RangeRead.completeKeys db r ≠ []) : + resolve db r.begin ≤ ((x : Key) : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with hn | ht | ⟨k, hkdb, hkres⟩ + · rw [hn] + exact WithTop.coe_le_coe.mpr (nil_le_key x) + · exfalso + refine hne ?_ + refine List.eq_nil_iff_forall_not_mem.mpr fun k hk => ?_ + obtain ⟨_, h1, _⟩ := RangeRead.mem_completeKeys.mp hk + rw [ht] at h1 + exact absurd h1 (not_le.mpr (WithTop.coe_lt_top k)) + · rw [hkres] + exact h k hkdb + +/-- A nonempty complete-keys list forces the begin selector's key to be genuine, +not `⊤`: a `⊤`-keyed begin resolves to the sentinel, which empties the window. -/ +private theorem begin_key_ne_top_of_completeKeys_ne_nil {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hne : RangeRead.completeKeys db r ≠ []) + (hoff : 1 ≤ r.begin.offset) : r.begin.key ≠ (⊤ : WithTop Key) := by + intro htop + have hresolve : resolve db r.begin = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top htop hoff + have hnil : RangeRead.completeKeys db r = [] := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + unfold RangeRead.inRange + rw [hresolve, List.filter_eq_nil_iff] + intro k _ hk + exact absurd (of_decide_eq_true hk).1 (not_le.mpr (WithTop.coe_lt_top k)) + exact hne hnil + +/-- A `WithTop Key` that is not `⊤` is a coerced genuine key. -/ +private theorem exists_begin_key_of_ne_top {r : RangeRead} + (h : r.begin.key ≠ (⊤ : WithTop Key)) : + ∃ v : Key, r.begin.key = (↑v : WithTop Key) := by + cases hk : r.begin.key with + | top => exact absurd hk h + | coe v => exact ⟨v, rfl⟩ + +/-- A stored key strictly below `w` at index `i` of a strictly increasing list forces +more than `i` keys strictly below `w`, where `w` may be the sentinel. -/ +private theorem lt_countP_of_getElem_lt_top {l : List Key} (hs : l.SortedLT) {w : WithTop Key} + {i : Nat} (hi : i < l.length) (hlt : ((l[i] : Key) : WithTop Key) < w) : + i < l.countP (fun (k : Key) => decide (((k : Key) : WithTop Key) < w)) := by + have h1 : (l.take (i + 1)).countP (fun (k : Key) => decide (((k : Key) : WithTop Key) < w)) = + (l.take (i + 1)).length := by + rw [List.countP_eq_length] + intro k hk + rw [List.mem_take_iff_getElem] at hk + obtain ⟨j, hj, rfl⟩ := hk + rw [decide_eq_true_eq] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr ((hs.getElem_le_getElem_iff).mpr (by omega))) hlt + have h3 : (l.take (i + 1)).countP (fun (k : Key) => decide (((k : Key) : WithTop Key) < w)) ≤ + l.countP (fun (k : Key) => decide (((k : Key) : WithTop Key) < w)) := + List.Sublist.countP_le (List.take_prefix (i + 1) l).sublist + rw [h1, List.length_take, Nat.min_eq_left (by omega)] at h3 + omega + +/-- With an end offset of at least two and no reported key reaching `end_.key`, the +end selector resolves to the sentinel: the stored key just past the last key below +`end_.key` would lie in the window and be reported, unless the window ends at `⊤`. -/ +theorem resolve_end_eq_top_of_hhi {db : DB} {r : RangeRead} {res : Result} + (hread : RangeRead.Compatible db r res) (hoff : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (m : Key) (hmm : m ∈ res.keys) (hmaxm : ∀ k ∈ RangeRead.completeKeys db r, k ≤ m) : + resolve db r.end_ = (⊤ : WithTop Key) := by + have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hread + obtain ⟨hlo₀, hlt₀⟩ := (RangeRead.mem_completeKeys.mp (hpre.subset hmm)).2 + have hltm : ((m : Key) : WithTop Key) < r.end_.key := hhi m hmm + have hlo_lt : resolve db r.begin < r.end_.key := lt_of_le_of_lt hlo₀ hltm + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hn | ht | ⟨k₀, _, hk₀res⟩ + · exfalso + rw [hn, WithTop.coe_lt_coe] at hlt₀ + exact List.not_lt_nil m hlt₀ + · exact ht + · set j := db.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) with hj + have htoNat : (r.end_.anchor db).toNat = j + (r.end_.offset - 1).toNat := by + rw [anchor_toNat_eq (by omega : 1 ≤ r.end_.offset), ← hj] + have ho1 : 1 ≤ (r.end_.offset - 1).toNat := by omega + have hpos : 0 ≤ r.end_.anchor db := by + unfold Selector.anchor + rw [← hj] + omega + obtain ⟨hilt, hidx⟩ := List.getElem?_eq_some_iff.mp (getElem?_anchor_eq_some hpos hk₀res) + have hjlt : j < (r.end_.anchor db).toNat := by rw [htoNat]; omega + have hjlen : j < db.keys.length := lt_trans hjlt hilt + have hjge : r.end_.key ≤ ((db.keys[j]'hjlen : Key) : WithTop Key) := by + by_contra hlt + exact absurd (lt_countP_of_getElem_lt_top db.sorted hjlen (lt_of_not_ge hlt)) (by omega) + have hjlt₀ : db.keys[j] < k₀ := by + rw [← hidx] + exact db.sorted.getElem_lt_getElem_iff.mpr hjlt + by_cases hmem : resolve db r.begin ≤ ((db.keys[j] : Key) : WithTop Key) + · have hin : db.keys[j] ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨List.getElem_mem hjlen, hmem, + by rw [hk₀res]; exact WithTop.coe_lt_coe.mpr hjlt₀⟩ + exact absurd (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmaxm _ hin)) hltm) + (not_lt.mpr hjge) + · have hltj : ((db.keys[j] : Key) : WithTop Key) < resolve db r.begin := + lt_of_not_ge hmem + exact absurd (lt_trans (lt_of_lt_of_le hltj hlo₀) hltm) (not_lt.mpr hjge) + + +/-- Inserting a key at or above every stored key keeps the end overshoot: with +`end_.offset ≥ 2` and every stored key strictly below `end_.key`, the end selector +still resolves to the sentinel after the insertion. -/ +private theorem resolve_end_eq_top_of_insert {db₀ : DB} {r : RangeRead} {x : Key} + (hsort : db₀.keys.SortedLT) (hx0 : x ∉ db₀.keys) + (hall_lt : ∀ k ∈ db₀.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hoff : 2 ≤ r.end_.offset) : + resolve (db₀.insert x hx0) r.end_ = (⊤ : WithTop Key) := by + have hcnt₀ : db₀.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) = db₀.keys.length := + List.countP_eq_length.mpr fun k hk => by rw [decide_eq_true_eq]; exact hall_lt k hk + have hJ₁ : db₀.keys.length ≤ (db₀.insert x hx0).keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx hsort hx0] + rcases lt_or_ge (((x : Key) : WithTop Key)) r.end_.key with hlt | hge + · rw [countP_insertIdx_of_lt hsort hlt, hcnt₀] + omega + · rw [List.countP_eq_length_filter, filter_insertIdx_of_not _ _ _ (by simp [hge])] + rw [← List.countP_eq_length_filter, hcnt₀] + refine resolve_eq_top ?_ ?_ + · unfold Selector.anchor + omega + · have htoNat : (r.end_.anchor (db₀.insert x hx0)).toNat = + (db₀.insert x hx0).keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := anchor_toNat_eq (by omega : 1 ≤ r.end_.offset) + have ho1 : 1 ≤ (r.end_.offset - 1).toNat := by omega + have hlen : (db₀.insert x hx0).keys.length = db₀.keys.length + 1 := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx hsort hx0] + rw [List.length_insertIdx_of_le_length List.countP_le_length] + rw [htoNat, hlen] + omega + +/-- Inserting an unstored key `x` at or above every stored key of `db₀`, into a +database that a sound range omits `x` from, forces a complete non-reversed result +to report `x`: the inserted key joins the complete keys. -/ +theorem mem_result_of_insert {db₀ : DB} {r : RangeRead} {res : Result} + {cr : ConflictRange} {x : Key} (hread : RangeRead.Compatible db₀ r res) + (hs : Sound r res cr) (hcr : x ∉ cr) (hmore : res.more = false) (hne : res.keys ≠ []) + (hx0 : x ∉ db₀.keys) + (hsub : ∀ k ∈ db₀.keys, ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key)) + (hE : resolve (db₀.insert x hx0) r.end_ = (⊤ : WithTop Key)) : + x ∈ res.keys := by + have hagree : AgreeOn cr db₀ (db₀.insert x hx0) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hx0 hcr).symm + have hcompat : RangeRead.Compatible (db₀.insert x hx0) r res := (hs db₀ _ hagree).mp hread + have heq₁ : res.keys = RangeRead.completeKeys (db₀.insert x hx0) r := by + rcases hcompat.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsub₁ : ∀ k ∈ (db₀.insert x hx0).keys, + ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := fun k hk => + ((DB.mem_insert_iff hx0).mp hk).elim (fun he => by rw [he]) + (fun hk₀ => hsub k hk₀) + have hlo₁ : resolve (db₀.insert x hx0) r.begin ≤ ((x : Key) : WithTop Key) := + resolve_begin_le hsub₁ (by rw [← heq₁]; exact hne) + rw [heq₁] + exact RangeRead.mem_completeKeys.mpr + ⟨DB.mem_insert_self db₀ x hx0, hlo₁, by rw [hE]; exact WithTop.coe_lt_top x⟩ + +/-- The reversed twin: inserting an unstored key at or above every stored key makes +it head the reversed window, so the reported keys' head is the inserted key. -/ +private theorem head_mem_result_of_insert {db₀ : DB} {r : RangeRead} {res : Result} + {cr : ConflictRange} {x : Key} (hread : RangeRead.Compatible db₀ r res) + (hs : Sound r res cr) (hcr : x ∉ cr) (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hx0 : x ∉ db₀.keys) + (hsub : ∀ k ∈ db₀.keys, ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key)) + (hE : resolve (db₀.insert x hx0) r.end_ = (⊤ : WithTop Key)) : + x ∈ res.keys := by + have hagree : AgreeOn cr db₀ (db₀.insert x hx0) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hx0 hcr).symm + have hcompat : RangeRead.Compatible (db₀.insert x hx0) r res := (hs db₀ _ hagree).mp hread + have hpre₁ : res.keys <+: RangeRead.completeKeys (db₀.insert x hx0) r := + RangeRead.compatible_keys_prefix hcompat + have hne₁ : RangeRead.completeKeys (db₀.insert x hx0) r ≠ [] := hpre₁.ne_nil hne + have hsub₁ : ∀ k ∈ (db₀.insert x hx0).keys, + ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := fun k hk => + ((DB.mem_insert_iff hx0).mp hk).elim (fun he => by rw [he]) + (fun hk₀ => hsub k hk₀) + have hlo₁ : resolve (db₀.insert x hx0) r.begin ≤ ((x : Key) : WithTop Key) := + resolve_begin_le hsub₁ hne₁ + have hxwin : x ∈ RangeRead.inRange (db₀.insert x hx0).keys + (resolve (db₀.insert x hx0) r.begin) ((⊤ : WithTop Key)) := by + rw [mem_inRange] + exact ⟨DB.mem_insert_self db₀ x hx0, hlo₁, WithTop.coe_lt_top x⟩ + have hWne : RangeRead.inRange (db₀.insert x hx0).keys + (resolve (db₀.insert x hx0) r.begin) ((⊤ : WithTop Key)) ≠ [] := List.ne_nil_of_mem hxwin + have hmax : ∀ k ∈ RangeRead.inRange (db₀.insert x hx0).keys + (resolve (db₀.insert x hx0) r.begin) ((⊤ : WithTop Key)), + ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := fun k hk => + hsub₁ k (mem_inRange.mp hk).1 + have hxlast : (RangeRead.inRange (db₀.insert x hx0).keys + (resolve (db₀.insert x hx0) r.begin) ((⊤ : WithTop Key))).getLast hWne = x := + le_antisymm (WithTop.coe_le_coe.mp (hmax _ (List.getLast_mem hWne))) + (pairwise_le_getLast (RangeRead.inRange_pairwise (db₀.insert x hx0) _ _) hxwin) + have hhead : res.keys.head hne = + (RangeRead.inRange (db₀.insert x hx0).keys (resolve (db₀.insert x hx0) r.begin) + ((⊤ : WithTop Key))).getLast hWne := by + refine (List.head_eq_iff_head?_eq_some hne).mpr ?_ + have h1 : res.keys.head? = + (RangeRead.inRange (db₀.insert x hx0).keys (resolve (db₀.insert x hx0) r.begin) + (resolve (db₀.insert x hx0) r.end_)).getLast? := + head?_eq_getLast?_of_prefix_rev hrev hpre₁ hne + rw [hE] at h1 + rw [h1, List.getLast?_eq_getLast_of_ne_nil hWne] + rw [← hxlast, ← hhead] + exact List.head_mem hne + +/-- A stored key strictly below `l[i]`, with `1 ≤ i`, is at most `l[i-1]`. -/ +private theorem le_pred_of_lt_getElem {l : List Key} (hs : l.SortedLT) {a : Key} {i : Nat} + (hi1 : 1 ≤ i) (hilt : i < l.length) (hmem : a ∈ l) + (hlt : ((a : Key) : WithTop Key) < ((l[i] : Key) : WithTop Key)) : a ≤ l[i-1] := by + obtain ⟨j, hj, hjl⟩ := List.mem_iff_getElem.mp hmem + rw [← hjl] + have hji : j < i := hs.getElem_lt_getElem_iff.mp + (WithTop.coe_lt_coe.mp (by rw [hjl]; exact hlt)) + exact (hs.getElem_le_getElem_iff).mpr (by omega) + +/-- If a read's end selector resolves to the stored key at index `i + 1`, the key at index +`i` lies in the complete keys whenever they are nonempty: it is the greatest stored key +strictly below the resolved end, and the window's lower end is at most it since the window +contains some stored key below the resolved end. -/ +private theorem mem_completeKeys_of_end_getElem_succ {db : DB} {r : RangeRead} {i : Nat} + (hilt : i + 1 < db.keys.length) + (hres : resolve db r.end_ = ((db.keys[i + 1] : Key) : WithTop Key)) + (hne : RangeRead.completeKeys db r ≠ []) : + db.keys[i] ∈ RangeRead.completeKeys db r := by + obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne + obtain ⟨hkm, hkB, hklt⟩ := RangeRead.mem_completeKeys.mp hk + obtain ⟨idx, hidx, hidxk⟩ := List.mem_iff_getElem.mp hkm + have hklt' : ((k : Key) : WithTop Key) < ((db.keys[i + 1] : Key) : WithTop Key) := by + rw [← hres]; exact hklt + have hstep : db.keys[idx] < db.keys[i + 1] := by + rw [hidxk]; exact WithTop.coe_lt_coe.mp hklt' + have hidx_lt : idx < i + 1 := db.sorted.getElem_lt_getElem_iff.mp hstep + refine RangeRead.mem_completeKeys.mpr ⟨List.getElem_mem (by omega), ?_, ?_⟩ + · calc resolve db r.begin ≤ ((k : Key) : WithTop Key) := hkB + _ = ((db.keys[idx] : Key) : WithTop Key) := by rw [hidxk] + _ ≤ ((db.keys[i] : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr ((db.sorted.getElem_le_getElem_iff).mpr (by omega)) + · rw [hres] + exact WithTop.coe_lt_coe.mpr (db.sorted.getElem_lt_getElem_iff.mpr (by omega)) + +/-- In a non-reversed read whose end selector is keyed at `⊤` with a positive offset, so +the read's window ends at `⊤`, past every stored key of *every* database, every key at or +above the result's last key is necessary: every sound conflict range contains it, so every +sound range extends to `⊤`. -/ +private theorem necessary_of_end_key_top_of_one_le {db₀ : DB} {r : RangeRead} {res : Result} + {x : Key} (hread : RangeRead.Compatible db₀ r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hoff : 1 ≤ r.end_.offset) (htop : r.end_.key = (⊤ : WithTop Key)) + (hlo : res.keys.getLast hne ≤ x) + (cr : ConflictRange) (hs : Sound r res cr) (hx : x ∉ res.keys) : + x ∈ cr := by + by_contra hcr + have hcomp : res.keys = RangeRead.completeKeys db₀ r := by + rcases hread.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hE : ∀ db : DB, resolve db r.end_ = (⊤ : WithTop Key) := + fun db => resolve_eq_top_of_key_eq_top htop hoff + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hread hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hle₀ : resolve db₀ r.begin ≤ ((x : Key) : WithTop Key) := by + have h1 : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := by + rw [← hcomp]; exact List.getLast_mem hne + calc resolve db₀ r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp h1).2.1 + _ ≤ ((x : Key) : WithTop Key) := WithTop.coe_le_coe.mpr hlo + have hx0 : x ∉ db₀.keys := by + intro hxm + refine hx ?_ + rw [hcomp] + refine RangeRead.mem_completeKeys.mpr ⟨hxm, hle₀, ?_⟩ + rw [hE db₀] + exact WithTop.coe_lt_top x + have hall : ∀ k ∈ db₀.keys, k < x := by + intro k hk + by_contra hge + have h2 : x ≤ k := le_of_not_gt hge + have h1 : k ∈ RangeRead.completeKeys db₀ r := + RangeRead.mem_completeKeys.mpr ⟨hk, hle₀.trans (WithTop.coe_le_coe.mpr h2), + by rw [hE db₀]; exact WithTop.coe_lt_top k⟩ + have h4 : k = x := + le_antisymm (le_trans (hmaxres k (by rw [hcomp]; exact h1)) hlo) h2 + rw [h4] at hk + exact hx0 hk + exact absurd (mem_result_of_insert hread hs hcr hmore hne hx0 + (fun k hk => WithTop.coe_le_coe.mpr (le_of_lt (hall k hk))) (hE _)) hx + +/-- In a non-reversed read whose end selector is keyed at `⊤` with a nonpositive offset, +every key at or above the result's last key is necessary: every sound conflict range +contains it, so every sound range extends to `⊤`. Here `resolve end_` is the stored key +`db.keys[m]` at index `m = |db| + offset - 1` and the result's last key is the stored key +`db.keys[m-1]`: inserting an unstored key at or above the result's last key enlarges the +perturbed window's top to `db.keys[m+1]`, so the perturbed complete keys are nonempty and +end strictly above the reported last key, breaking the equality a complete result demands; +erasing an unreported stored key at or above the reported last key (possible only for a +nonpositive offset, since a positive offset ends the window at `⊤`) drops `resolve end_` +onto the reported last key itself, which then falls out of the window. -/ +private theorem necessary_of_end_key_top_of_offset_nonpos {db₀ : DB} {r : RangeRead} + {res : Result} {x : Key} (hread : RangeRead.Compatible db₀ r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hoff : r.end_.offset ≤ 0) (htop : r.end_.key = (⊤ : WithTop Key)) + (hlo : res.keys.getLast hne ≤ x) + (cr : ConflictRange) (hs : Sound r res cr) (hx : x ∉ res.keys) : + x ∈ cr := by + by_contra hcr + have hcomp : res.keys = RangeRead.completeKeys db₀ r := by + rcases hread.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hread hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hck₀ne : RangeRead.completeKeys db₀ r ≠ [] := by rw [← hcomp]; exact hne + have hAnc : r.end_.anchor db₀ = ((db₀.keys.length : ℤ) + r.end_.offset - 1) := + anchor_of_top htop + have hA1 : 1 ≤ r.end_.anchor db₀ := RangeRead.one_le_anchor_of_completeKeys_ne_nil hck₀ne + have hcast : ((r.end_.anchor db₀).toNat : ℤ) = r.end_.anchor db₀ := + Int.toNat_of_nonneg (by omega) + set m := (r.end_.anchor db₀).toNat with hm_def + have hm1 : 1 ≤ m := by omega + have hmlt : m < db₀.keys.length := by omega + have hmlt1 : m - 1 < db₀.keys.length := by omega + have hres₀ : resolve db₀ r.end_ = ((db₀.keys[m] : Key) : WithTop Key) := by + have h := resolve_eq_getElem (db := db₀) (s := r.end_) (h0 := by omega) + (h := by rw [← hm_def]; exact hmlt) + rw [h] + -- The result's last key is the stored key just below the resolved end. + have hmem_m1 : db₀.keys[m - 1] ∈ res.keys := by + have hck₀ : RangeRead.completeKeys db₀ r = + RangeRead.inRange db₀.keys (resolve db₀ r.begin) ((db₀.keys[m] : Key) : WithTop Key) := by + simp [RangeRead.completeKeys, hrev, hres₀] + rw [hcomp, hck₀, mem_inRange] + refine ⟨List.getElem_mem (by omega), ?_, ?_⟩ + · obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne + obtain ⟨hstored, hB, hC⟩ := RangeRead.mem_completeKeys.mp (by rw [← hcomp]; exact hk) + have hlt : ((k : Key) : WithTop Key) < ((db₀.keys[m] : Key) : WithTop Key) := by + rw [← hres₀]; exact hC + obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp + (RangeRead.compatible_keys_subset hread hk) + calc resolve db₀ r.begin ≤ ((k : Key) : WithTop Key) := hB + _ ≤ ((db₀.keys[m - 1] : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_pred_of_lt_getElem (a := k) db₀.sorted hm1 hmlt + (hjk ▸ List.getElem_mem hj) hlt) + · exact WithTop.coe_lt_coe.mpr (db₀.sorted.getElem_lt_getElem_iff.mpr (by omega)) + have hlast : res.keys.getLast hne = db₀.keys[m - 1] := by + refine le_antisymm ?_ (pairwise_le_getLast hsort.pairwise hmem_m1) + obtain ⟨hstored, _, hC⟩ := RangeRead.mem_completeKeys.mp + (by rw [← hcomp]; exact List.getLast_mem hne) + refine le_pred_of_lt_getElem db₀.sorted hm1 hmlt hstored ?_ + rw [← hres₀] + exact hC + have hxne : x ≠ db₀.keys[m - 1] := by + intro hcon + subst hcon + exact hx hmem_m1 + have hxlt : db₀.keys[m - 1] < x := by + refine lt_of_le_of_ne ?_ (Ne.symm hxne) + rw [← hlast] + exact hlo + by_cases hxm : x ∈ db₀.keys + · -- `x` is stored: erasing it drops `resolve end_` onto the result's last key itself. + obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hxm + have hjm : m ≤ j := by + have h1 : db₀.keys[m - 1] < db₀.keys[j] := by rw [hjx]; exact hxlt + have h2 := db₀.sorted.getElem_lt_getElem_iff.mp h1 + omega + let db₁ : DB := db₀.erase x + have hagree : AgreeOn cr db₀ db₁ := (ConflictRange.keys_erase_of_not_mem cr db₀ hcr).symm + have hkeys₁ : db₁.keys = db₀.keys.eraseIdx j := by + change db₀.keys.erase x = db₀.keys.eraseIdx j + rw [← hjx] + exact List.Nodup.erase_getElem db₀.nodup j hj + have hlen₁ : db₁.keys.length = db₀.keys.length - 1 := by + rw [hkeys₁, List.length_eraseIdx, ite_eq_left hj] + have hJ₁ : ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) = + ((db₀.keys.length : ℤ) - 1) := by + rw [htop, countP_lt_top, hlen₁] + omega + have hanc₁ : r.end_.anchor db₁ = (r.end_.anchor db₀ : ℤ) - 1 := by + have e1 : r.end_.anchor db₁ = ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) + + r.end_.offset - 1 := rfl + rw [e1, hJ₁, hAnc] + ring + have hidx : (r.end_.anchor db₁).toNat = m - 1 := by + rw [hanc₁, toNat_sub_one hA1, ← hm_def] + have hmlt1' : m - 1 < db₁.keys.length := by rw [hlen₁]; omega + have hget : db₁.keys[m - 1] = db₀.keys[m - 1] := by + have h1 : db₁.keys[m - 1]? = db₀.keys[m - 1]? := by + rw [hkeys₁, List.getElem?_eraseIdx_of_lt (show m - 1 < j by omega)] + rw [List.getElem?_eq_getElem hmlt1', List.getElem?_eq_getElem hmlt1] at h1 + exact Option.some.inj h1 + have hres₁ : resolve db₁ r.end_ = ((db₀.keys[m - 1] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db₁ := by rw [hanc₁]; omega + have hlt : (r.end_.anchor db₁).toNat < db₁.keys.length := by rw [hidx]; omega + have h := resolve_eq_getElem (db := db₁) (s := r.end_) h0 hlt + refine h.trans ?_ + have h1' : db₁.keys[(r.end_.anchor db₁).toNat]? = db₁.keys[m - 1]? := by rw [hidx] + rw [List.getElem?_eq_getElem hlt, List.getElem?_eq_getElem hmlt1', hget] at h1' + exact congrArg _ (Option.some.inj h1') + have hin : ¬ RangeRead.Compatible db₁ r res := by + intro hcomp₁ + have hsub₁ : res.keys ⊆ RangeRead.completeKeys db₁ r := + (RangeRead.compatible_keys_prefix hcomp₁).subset + have hmem : res.keys.getLast hne ∈ RangeRead.completeKeys db₁ r := + hsub₁ (List.getLast_mem hne) + obtain ⟨_, _, hlt⟩ := RangeRead.mem_completeKeys.mp hmem + rw [hres₁, hlast] at hlt + exact lt_irrefl _ hlt + exact hin ((hs db₀ db₁ hagree).mp hread) + · -- `x` is unstored: inserting it enlarges the window's top past the result's last key. + let db₁ : DB := db₀.insert x hxm + have hagree : AgreeOn cr db₀ db₁ := (ConflictRange.keys_insert_of_not_mem cr db₀ hxm hcr).symm + have hcomp₁ : RangeRead.Compatible db₁ r res := (hs db₀ db₁ hagree).mp hread + have heq₁ : res.keys = RangeRead.completeKeys db₁ r := by + rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hne₁ : RangeRead.completeKeys db₁ r ≠ [] := by rw [← heq₁]; exact hne + have hlen₁ : db₁.keys.length = db₀.keys.length + 1 := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx db₀.sorted hxm] + rw [List.length_insertIdx_of_le_length List.countP_le_length] + have hmlt' : m < db₁.keys.length := by rw [hlen₁]; omega + have hmlt1' : m - 1 < db₁.keys.length := by rw [hlen₁]; omega + have hmltp : m + 1 < db₁.keys.length := by rw [hlen₁]; omega + have hJ₁ : ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) = + ((db₀.keys.length : ℤ) + 1) := by + rw [htop, countP_lt_top, hlen₁] + omega + have hanc₁ : r.end_.anchor db₁ = (r.end_.anchor db₀ : ℤ) + 1 := by + have e1 : r.end_.anchor db₁ = ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) + + r.end_.offset - 1 := rfl + rw [e1, hJ₁, hAnc] + ring + have hidx : (r.end_.anchor db₁).toNat = m + 1 := by + rw [hanc₁] + have h2 : (((r.end_.anchor db₀ + 1 : ℤ).toNat : ℤ)) = r.end_.anchor db₀ + 1 := + Int.toNat_of_nonneg (by omega) + omega + have hres₁ : resolve db₁ r.end_ = ((db₁.keys[m + 1] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db₁ := by rw [hanc₁]; omega + have hlt : (r.end_.anchor db₁).toNat < db₁.keys.length := by rw [hidx]; omega + have h := resolve_eq_getElem (db := db₁) (s := r.end_) h0 hlt + refine h.trans ?_ + have h1 : db₁.keys[(r.end_.anchor db₁).toNat]? = db₁.keys[m + 1]? := by rw [hidx] + rw [List.getElem?_eq_getElem hlt, List.getElem?_eq_getElem hmltp] at h1 + exact congrArg _ (Option.some.inj h1) + have hmem : db₁.keys[m] ∈ RangeRead.completeKeys db₁ r := + mem_completeKeys_of_end_getElem_succ hmltp hres₁ hne₁ + have hmem' : db₁.keys[m] ∈ res.keys := by rw [heq₁]; exact hmem + have hgt : db₀.keys[m - 1] < db₁.keys[m] := by + have hcge : m ≤ db₀.keys.countP (fun k => decide (k < x)) := by + have h := lt_countP_of_getElem_lt db₀.sorted hmlt1 hxlt + omega + have hkeys₁ : db₁.keys = db₀.keys.insertIdx + (db₀.keys.countP (fun k => decide (k < x))) x := by + rw [DB.insert_keys]; exact orderedInsert_eq_insertIdx db₀.sorted hxm + have hget : db₁.keys[m - 1] = db₀.keys[m - 1] := by + have h1 : db₁.keys[m - 1]? = db₀.keys[m - 1]? := by + rw [hkeys₁, List.getElem?_insertIdx_of_lt + (show m - 1 < db₀.keys.countP (fun k => decide (k < x)) by omega)] + rw [List.getElem?_eq_getElem hmlt1', List.getElem?_eq_getElem hmlt1] at h1 + exact Option.some.inj h1 + have h2 : db₁.keys[m - 1] < db₁.keys[m] := db₁.sorted.getElem_lt_getElem_iff.mpr + (by omega) + rw [hget] at h2 + exact h2 + have hle : db₁.keys[m] ≤ db₀.keys[m - 1] := by rw [← hlast]; exact hmaxres _ hmem' + exact absurd hle (not_le.mpr hgt) + +/-- In a reversed read whose end selector is keyed at `⊤` with a nonpositive offset, every +key at or above the result's head is necessary: every sound conflict range contains it, so +every sound range extends to `⊤`. Here `resolve end_` is the stored key `db.keys[m]` at +index `m = |db| + offset - 1` and the result's head is the stored key `db.keys[m-1]`: +inserting an unstored key at or above the reported head enlarges the perturbed window's top +to `db.keys[m+1]`, so the key at index `m` joins the reversed complete keys above the +reported head and breaks the required prefix; erasing an unreported stored key at or above +the reported head (possible only for a nonpositive offset, since a positive offset ends the +window at `⊤`) drops `resolve end_` onto the reported head itself, which then falls out +of the window. -/ +private theorem necessary_of_end_key_top_rev_of_offset_nonpos {db₀ : DB} {r : RangeRead} + {res : Result} {x : Key} (hread : RangeRead.Compatible db₀ r res) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hoff : r.end_.offset ≤ 0) (htop : r.end_.key = (⊤ : WithTop Key)) + (hlo : res.keys.head hne ≤ x) + (cr : ConflictRange) (hs : Sound r res cr) (hx : x ∉ res.keys) : + x ∈ cr := by + by_contra hcr + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hread + have hck₀ne : RangeRead.completeKeys db₀ r ≠ [] := hpre.ne_nil hne + have hAnc : r.end_.anchor db₀ = ((db₀.keys.length : ℤ) + r.end_.offset - 1) := + anchor_of_top htop + have hA1 : 1 ≤ r.end_.anchor db₀ := RangeRead.one_le_anchor_of_completeKeys_ne_nil hck₀ne + have hcast : ((r.end_.anchor db₀).toNat : ℤ) = r.end_.anchor db₀ := + Int.toNat_of_nonneg (by omega) + set m := (r.end_.anchor db₀).toNat with hm_def + have hm1 : 1 ≤ m := by omega + have hmlt : m < db₀.keys.length := by omega + have hmlt1 : m - 1 < db₀.keys.length := by omega + have hres₀ : resolve db₀ r.end_ = ((db₀.keys[m] : Key) : WithTop Key) := by + have h := resolve_eq_getElem (db := db₀) (s := r.end_) (h0 := by omega) + (h := by rw [← hm_def]; exact hmlt) + rw [h] + have hhead : res.keys.head hne = db₀.keys[m - 1] := by + have hpref : (RangeRead.completeKeys db₀ r).head? = res.keys.head? := by + obtain ⟨t, ht⟩ := hpre + rw [← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl, + Option.some_or] + refine (List.head_eq_iff_head?_eq_some hne).mpr ?_ + rw [← hpref, RangeRead.completeKeys_head?_rev hrev (by omega : r.end_.offset ≤ 1) hck₀ne, + toNat_sub_one hA1, ← hm_def, List.getElem?_eq_getElem hmlt1] + have hxne : x ≠ db₀.keys[m - 1] := by + intro hcon + subst hcon + exact hx (hhead ▸ List.head_mem hne) + have hxlt : db₀.keys[m - 1] < x := by + refine lt_of_le_of_ne ?_ (Ne.symm hxne) + rw [← hhead] + exact hlo + by_cases hxm : x ∈ db₀.keys + · -- `x` is stored: erasing it drops `resolve end_` onto the result's head itself. + obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hxm + have hjm : m ≤ j := by + have h1 : db₀.keys[m - 1] < db₀.keys[j] := by rw [hjx]; exact hxlt + have h2 := db₀.sorted.getElem_lt_getElem_iff.mp h1 + omega + let db₁ : DB := db₀.erase x + have hagree : AgreeOn cr db₀ db₁ := (ConflictRange.keys_erase_of_not_mem cr db₀ hcr).symm + have hkeys₁ : db₁.keys = db₀.keys.eraseIdx j := by + change db₀.keys.erase x = db₀.keys.eraseIdx j + rw [← hjx] + exact List.Nodup.erase_getElem db₀.nodup j hj + have hlen₁ : db₁.keys.length = db₀.keys.length - 1 := by + rw [hkeys₁, List.length_eraseIdx, ite_eq_left hj] + have hJ₁ : ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) = + ((db₀.keys.length : ℤ) - 1) := by + rw [htop, countP_lt_top, hlen₁] + omega + have hanc₁ : r.end_.anchor db₁ = (r.end_.anchor db₀ : ℤ) - 1 := by + have e1 : r.end_.anchor db₁ = ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) + + r.end_.offset - 1 := rfl + rw [e1, hJ₁, hAnc] + ring + have hidx : (r.end_.anchor db₁).toNat = m - 1 := by + rw [hanc₁, toNat_sub_one hA1, ← hm_def] + have hmlt1' : m - 1 < db₁.keys.length := by rw [hlen₁]; omega + have hget : db₁.keys[m - 1] = db₀.keys[m - 1] := by + have h1 : db₁.keys[m - 1]? = db₀.keys[m - 1]? := by + rw [hkeys₁, List.getElem?_eraseIdx_of_lt (show m - 1 < j by omega)] + rw [List.getElem?_eq_getElem hmlt1', List.getElem?_eq_getElem hmlt1] at h1 + exact Option.some.inj h1 + have hres₁ : resolve db₁ r.end_ = ((db₀.keys[m - 1] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db₁ := by rw [hanc₁]; omega + have hlt : (r.end_.anchor db₁).toNat < db₁.keys.length := by rw [hidx]; omega + have h := resolve_eq_getElem (db := db₁) (s := r.end_) h0 hlt + refine h.trans ?_ + have h1 : db₁.keys[(r.end_.anchor db₁).toNat]? = db₁.keys[m - 1]? := by rw [hidx] + rw [List.getElem?_eq_getElem hlt, List.getElem?_eq_getElem hmlt1', hget] at h1 + exact congrArg _ (Option.some.inj h1) + have hin : ¬ RangeRead.Compatible db₁ r res := by + intro hcomp₁ + have hsub₁ : res.keys ⊆ RangeRead.completeKeys db₁ r := + (RangeRead.compatible_keys_prefix hcomp₁).subset + have hmem : res.keys.head hne ∈ RangeRead.completeKeys db₁ r := + hsub₁ (List.head_mem hne) + obtain ⟨_, _, hlt⟩ := RangeRead.mem_completeKeys.mp hmem + rw [hres₁, hhead] at hlt + exact lt_irrefl _ hlt + exact hin ((hs db₀ db₁ hagree).mp hread) + · -- `x` is unstored: inserting it enlarges the window's top past the result's head. + let db₁ : DB := db₀.insert x hxm + have hagree : AgreeOn cr db₀ db₁ := (ConflictRange.keys_insert_of_not_mem cr db₀ hxm hcr).symm + have hcomp₁ : RangeRead.Compatible db₁ r res := (hs db₀ db₁ hagree).mp hread + have hpre₁ : res.keys <+: RangeRead.completeKeys db₁ r := + RangeRead.compatible_keys_prefix hcomp₁ + have hne₁ : RangeRead.completeKeys db₁ r ≠ [] := hpre₁.ne_nil hne + have hlen₁ : db₁.keys.length = db₀.keys.length + 1 := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx db₀.sorted hxm] + rw [List.length_insertIdx_of_le_length List.countP_le_length] + have hmlt' : m < db₁.keys.length := by rw [hlen₁]; omega + have hmlt1' : m - 1 < db₁.keys.length := by rw [hlen₁]; omega + have hmltp : m + 1 < db₁.keys.length := by rw [hlen₁]; omega + have hJ₁ : ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) = + ((db₀.keys.length : ℤ) + 1) := by + rw [htop, countP_lt_top, hlen₁] + omega + have hanc₁ : r.end_.anchor db₁ = (r.end_.anchor db₀ : ℤ) + 1 := by + have e1 : r.end_.anchor db₁ = ((db₁.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) : ℤ)) + + r.end_.offset - 1 := rfl + rw [e1, hJ₁, hAnc] + ring + have hidx : (r.end_.anchor db₁).toNat = m + 1 := by + rw [hanc₁] + have h2 : (((r.end_.anchor db₀ + 1 : ℤ).toNat : ℤ)) = r.end_.anchor db₀ + 1 := + Int.toNat_of_nonneg (by omega) + omega + have hres₁ : resolve db₁ r.end_ = ((db₁.keys[m + 1] : Key) : WithTop Key) := by + have h0 : 0 ≤ r.end_.anchor db₁ := by rw [hanc₁]; omega + have hlt : (r.end_.anchor db₁).toNat < db₁.keys.length := by rw [hidx]; omega + have h := resolve_eq_getElem (db := db₁) (s := r.end_) h0 hlt + refine h.trans ?_ + have h1 : db₁.keys[(r.end_.anchor db₁).toNat]? = db₁.keys[m + 1]? := by rw [hidx] + rw [List.getElem?_eq_getElem hlt, List.getElem?_eq_getElem hmltp] at h1 + exact congrArg _ (Option.some.inj h1) + have hmem : db₁.keys[m] ∈ RangeRead.completeKeys db₁ r := + mem_completeKeys_of_end_getElem_succ hmltp hres₁ hne₁ + have hle := le_head_of_mem_completeKeys_rev hrev hpre₁ hne hmem + have hgt : db₀.keys[m - 1] < db₁.keys[m] := by + have hcge : m ≤ db₀.keys.countP (fun k => decide (k < x)) := by + have h := lt_countP_of_getElem_lt db₀.sorted hmlt1 hxlt + omega + have hkeys₁ : db₁.keys = db₀.keys.insertIdx + (db₀.keys.countP (fun k => decide (k < x))) x := by + rw [DB.insert_keys]; exact orderedInsert_eq_insertIdx db₀.sorted hxm + have hget : db₁.keys[m - 1] = db₀.keys[m - 1] := by + have h1 : db₁.keys[m - 1]? = db₀.keys[m - 1]? := by + rw [hkeys₁, List.getElem?_insertIdx_of_lt + (show m - 1 < db₀.keys.countP (fun k => decide (k < x)) by omega)] + rw [List.getElem?_eq_getElem hmlt1', List.getElem?_eq_getElem hmlt1] at h1 + exact Option.some.inj h1 + have h2 : db₁.keys[m - 1] < db₁.keys[m] := db₁.sorted.getElem_lt_getElem_iff.mpr + (by omega) + rw [hget] at h2 + exact h2 + rw [hhead] at hle + exact absurd hle (not_le.mpr hgt) + +/-- In a reversed read whose end selector is keyed at `⊤` with a positive offset, every key +at or above the result's head is necessary: every sound conflict range contains it, so every +sound range extends to `⊤`. -/ +private theorem necessary_of_end_key_top_rev_of_one_le {db₀ : DB} {r : RangeRead} {res : Result} + {x : Key} (hread : RangeRead.Compatible db₀ r res) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hoff : 1 ≤ r.end_.offset) + (htop : r.end_.key = (⊤ : WithTop Key)) + (hlo : res.keys.head hne ≤ x) + (cr : ConflictRange) (hs : Sound r res cr) (hx : x ∉ res.keys) : + x ∈ cr := by + by_contra hcr + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hread + have hE : ∀ db : DB, resolve db r.end_ = (⊤ : WithTop Key) := + fun db => resolve_eq_top_of_key_eq_top htop hoff + have hmax₀ : ∀ k ∈ RangeRead.completeKeys db₀ r, k ≤ res.keys.head hne := + fun k hk => le_head_of_mem_completeKeys_rev hrev hpre hne hk + have hlo₀ : resolve db₀ r.begin ≤ ((res.keys.head hne : Key) : WithTop Key) := by + have h1 : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := + hpre.subset (List.head_mem hne) + exact (RangeRead.mem_completeKeys.mp h1).2.1 + have hle₀ : resolve db₀ r.begin ≤ ((x : Key) : WithTop Key) := + hlo₀.trans (WithTop.coe_le_coe.mpr hlo) + have hx0 : x ∉ db₀.keys := by + intro hxm + have hxin : x ∈ RangeRead.completeKeys db₀ r := by + refine RangeRead.mem_completeKeys.mpr ⟨hxm, hle₀, ?_⟩ + rw [hE db₀] + exact WithTop.coe_lt_top x + refine hx ?_ + rw [le_antisymm (hmax₀ x hxin) hlo] + exact List.head_mem hne + have hall : ∀ k ∈ db₀.keys, k < x := by + intro k hk + by_contra hge + have h2 : x ≤ k := le_of_not_gt hge + have h1 : k ∈ RangeRead.completeKeys db₀ r := + RangeRead.mem_completeKeys.mpr ⟨hk, hle₀.trans (WithTop.coe_le_coe.mpr h2), + by rw [hE db₀]; exact WithTop.coe_lt_top k⟩ + have h4 : k = x := + le_antisymm (le_trans (hmax₀ k h1) hlo) h2 + rw [h4] at hk + exact hx0 hk + exact absurd (head_mem_result_of_insert hread hs hcr hrev hne hx0 + (fun k hk => WithTop.coe_le_coe.mpr (le_of_lt (hall k hk))) (hE _)) hx + +/-- In a complete non-reversed read whose end selector is keyed at `⊤`, every key at or +above the result's last key is necessary: every sound conflict range contains it, so every +sound range extends to `⊤`. This is the analog of `necessary_nil_of_begin_offset_nonpos` +for the top of the key space, and it holds for every offset. For a positive offset the +window ends at `⊤` past every stored key of *every* database +(`resolve_eq_top_of_key_eq_top`), and inserting an unstored key at or above the result's +last key into a database a range omits it from joins the complete keys and breaks the +equality a complete result demands. For a nonpositive offset the window ends at a stored +key, and the same conclusion follows from the count- and endpoint-shift perturbations +recorded in `necessary_of_end_key_top_of_offset_nonpos`: inserting an unstored key at or +above the result's last key enlarges the perturbed window's top past the reported last key, +breaking that equality, and erasing an unreported stored key at or above the reported last +key — possible only for a nonpositive offset, since a positive offset ends the window at +`⊤` — drops `resolve end_` onto the reported last key itself, which then falls out of +the window. The bottom of `WithTop Key` is the empty key, a genuine key, so an unbounded +lower end is witnessed by the single necessary empty key, while no key lies above every +stored key, so an unbounded upper end is witnessed by the unbounded family of keys beyond +the result's last. Completeness is what confines necessity to that family: past a truncated +result the complete keys merely extend the reported prefix, so inserting an unstored key +into a database a range omits it from only appends it to the complete keys and leaves the +reported keys a prefix of them, keeping the truncated result compatible; only a complete +result, whose keys are all of the complete keys, is broken by that insertion. +An inference sees only `(r, res)`, so any one witness database suffices. -/ +theorem necessary_of_end_key_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (htop : r.end_.key = (⊤ : WithTop Key)) + {x : Key} (hlo : res.keys.getLast hne ≤ x) : + Necessary r res x := by + obtain ⟨db₀, hread⟩ := hread + intro cr hs + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hread⟩ hx cr hs + · rcases lt_or_ge r.end_.offset 1 with hoff | hoff + · exact necessary_of_end_key_top_of_offset_nonpos hread hrev hmore hne + (by omega : r.end_.offset ≤ 0) htop hlo cr hs hx + · exact necessary_of_end_key_top_of_one_le hread hrev hmore hne + (by omega : 1 ≤ r.end_.offset) htop hlo cr hs hx + +/-- In a reversed read whose end selector is keyed at `⊤`, every key at or above the +result's head is necessary: every sound conflict range contains it, so every sound range +extends to `⊤`. This is the reversal-symmetric twin of `necessary_of_end_key_top`, with +the roles of head and last exchanged as in `necessary_nil_of_begin_offset_nonpos_rev`, and +no completeness is needed, matching `necessary_head_to_end_key`. For a positive offset the +window ends at `⊤` for every database, and inserting an unstored key above the reported +head makes the new key head the reversed complete keys instead, breaking the required +prefix whether or not the result is truncated. For a nonpositive offset the window ends at +a stored key, and the same conclusion follows from the endpoint-shift perturbations +recorded in `necessary_of_end_key_top_rev_of_offset_nonpos`: inserting an unstored key at +or above the reported head breaks the prefix, and erasing an unreported stored key at or +above the reported head — possible only for a nonpositive offset, since a positive +offset ends the window at `⊤` — drops `resolve end_` onto the reported head itself, +which then falls out of the window. An inference sees only `(r, res)`, so any one +witness database suffices. -/ +theorem necessary_of_end_key_top_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (htop : r.end_.key = (⊤ : WithTop Key)) + {x : Key} (hlo : res.keys.head hne ≤ x) : + Necessary r res x := by + obtain ⟨db₀, hread⟩ := hread + intro cr hs + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hread⟩ hx cr hs + · rcases lt_or_ge r.end_.offset 1 with hoff | hoff + · exact necessary_of_end_key_top_rev_of_offset_nonpos hread hrev hne + (by omega : r.end_.offset ≤ 0) htop hlo cr hs hx + · exact necessary_of_end_key_top_rev_of_one_le hread hrev hne + (by omega : 1 ≤ r.end_.offset) htop hlo cr hs hx + +/-- In a complete non-reversed read whose end offset is at least two and none of +whose reported keys reaches `end_.key`, every key at or above the result's last +key is necessary: every sound conflict range contains it, so every sound range +extends to `⊤`. This is `necessary_of_end_key_top` without requiring the +selector to be keyed at the sentinel: the offset overshoot puts `resolve end_` +strictly past the first stored key at or above `end_.key`, and that key would +itself be reported since the window begins below `end_.key`, contradicting the +hypothesis, so the window must end at `⊤`; completeness then reports every +stored key of the window, forcing `end_.key` above every stored key — the +property the sentinel `⊤` has by construction. The perturbation is the usual +one: a reported key is necessary by `necessary_of_mem_result`, and an unstored +key at or above the result's last key, inserted into a database a range omits +it from, still resolves the end to `⊤` (the offset overshoot survives the +insertion), so it joins the complete keys and breaks the equality a complete +result demands. An inference sees only `(r, res)`, so any one witness database +suffices. -/ +theorem necessary_of_end_offset_ge_two {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hoff : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + {x : Key} (hlo : res.keys.getLast hne ≤ x) : + Necessary r res x := by + obtain ⟨db₀, hread⟩ := hread + intro cr hs + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hread⟩ hx cr hs + · by_contra hcr + have hcomp : res.keys = RangeRead.completeKeys db₀ r := by + rcases hread.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hread hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hlo_lt : resolve db₀ r.begin < r.end_.key := by + have h1 : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := by + rw [← hcomp]; exact List.getLast_mem hne + calc resolve db₀ r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp h1).2.1 + _ < r.end_.key := hhi _ (List.getLast_mem hne) + have hE₀ : resolve db₀ r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hread hoff hhi (res.keys.getLast hne) + (List.getLast_mem hne) (fun k hk => hmaxres k (by rw [hcomp]; exact hk)) + have hle₀ : resolve db₀ r.begin ≤ ((x : Key) : WithTop Key) := by + have h1 : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := by + rw [← hcomp]; exact List.getLast_mem hne + calc resolve db₀ r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp h1).2.1 + _ ≤ ((x : Key) : WithTop Key) := WithTop.coe_le_coe.mpr hlo + have hx0 : x ∉ db₀.keys := by + intro hxm + refine hx ?_ + rw [hcomp] + refine RangeRead.mem_completeKeys.mpr ⟨hxm, hle₀, ?_⟩ + rw [hE₀] + exact WithTop.coe_lt_top x + have hall_lt : ∀ k ∈ db₀.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_contra hge + have h1 : k ∈ RangeRead.completeKeys db₀ r := + RangeRead.mem_completeKeys.mpr + ⟨hk, le_of_lt (lt_of_lt_of_le hlo_lt (le_of_not_gt hge)), + by rw [hE₀]; exact WithTop.coe_lt_top k⟩ + exact hge (hhi k (by rw [hcomp]; exact h1)) + have hall : ∀ k ∈ db₀.keys, k < x := by + intro k hk + by_contra hge + have h2 : x ≤ k := le_of_not_gt hge + have h1 : k ∈ RangeRead.completeKeys db₀ r := + RangeRead.mem_completeKeys.mpr ⟨hk, hle₀.trans (WithTop.coe_le_coe.mpr h2), + by rw [hE₀]; exact WithTop.coe_lt_top k⟩ + have h4 : k = x := + le_antisymm (le_trans (hmaxres k (by rw [hcomp]; exact h1)) hlo) h2 + rw [h4] at hk + exact hx0 hk + exact absurd (mem_result_of_insert hread hs hcr hmore hne hx0 + (fun k hk => WithTop.coe_le_coe.mpr (le_of_lt (hall k hk))) + (resolve_end_eq_top_of_insert db₀.sorted hx0 hall_lt hoff)) hx + +/-- In a reversed read whose end offset is at least two and none of whose +reported keys reaches `end_.key`, every key at or above the result's head is +necessary: every sound conflict range contains it, so every sound range extends +to `⊤`. This is the reversal-symmetric twin of `necessary_of_end_offset_ge_two` +and, as in `necessary_of_end_key_top_rev`, no completeness is needed: the +reported keys head the complete keys, whose head is the greatest stored key of +the window, so the hypotheses again force `resolve end_` to be `⊤` with +`end_.key` above every stored key, and inserting an unstored key at or above +the reported head makes the new key head the complete keys instead, breaking +the required prefix whether or not the result is truncated. An inference sees +only `(r, res)`, so any one witness database suffices. -/ +theorem necessary_of_end_offset_ge_two_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hoff : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + {x : Key} (hlo : res.keys.head hne ≤ x) : + Necessary r res x := by + obtain ⟨db₀, hread⟩ := hread + intro cr hs + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hread⟩ hx cr hs + · by_contra hcr + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hread + have hck : RangeRead.completeKeys db₀ r = + (RangeRead.inRange db₀.keys (resolve db₀ r.begin) (resolve db₀ r.end_)).reverse := by + simp [RangeRead.completeKeys, hrev] + have hWne : RangeRead.inRange db₀.keys (resolve db₀ r.begin) + (resolve db₀ r.end_) ≠ [] := + fun h => hpre.ne_nil hne (hck.trans (List.reverse_eq_nil_iff.mpr h)) + have hmaxW : ∀ k ∈ RangeRead.completeKeys db₀ r, + k ≤ (RangeRead.inRange db₀.keys (resolve db₀ r.begin) + (resolve db₀ r.end_)).getLast hWne := by + intro k hk + rw [hck, List.mem_reverse] at hk + exact pairwise_le_getLast (RangeRead.inRange_pairwise db₀ _ _) hk + have hhead : res.keys.head hne = + (RangeRead.inRange db₀.keys (resolve db₀ r.begin) + (resolve db₀ r.end_)).getLast hWne := by + refine (List.head_eq_iff_head?_eq_some hne).mpr ?_ + rw [head?_eq_getLast?_of_prefix_rev hrev hpre hne, + List.getLast?_eq_getLast_of_ne_nil hWne] + have hmax₀ : ∀ k ∈ RangeRead.completeKeys db₀ r, k ≤ res.keys.head hne := by + intro k hk + rw [hhead] + exact hmaxW k hk + have hlo₀ : resolve db₀ r.begin ≤ ((res.keys.head hne : Key) : WithTop Key) := by + have h1 : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := + hpre.subset (List.head_mem hne) + exact (RangeRead.mem_completeKeys.mp h1).2.1 + have hle₀ : resolve db₀ r.begin ≤ ((x : Key) : WithTop Key) := + hlo₀.trans (WithTop.coe_le_coe.mpr hlo) + have hlo_lt : resolve db₀ r.begin < r.end_.key := + lt_of_le_of_lt hlo₀ (hhi _ (List.head_mem hne)) + have hE₀ : resolve db₀ r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hread hoff hhi (res.keys.head hne) + (List.head_mem hne) hmax₀ + have hx0 : x ∉ db₀.keys := by + intro hxm + have hxin : x ∈ RangeRead.completeKeys db₀ r := by + refine RangeRead.mem_completeKeys.mpr ⟨hxm, hle₀, ?_⟩ + rw [hE₀] + exact WithTop.coe_lt_top x + refine hx ?_ + rw [le_antisymm (hmax₀ x hxin) hlo] + exact List.head_mem hne + have hall_lt : ∀ k ∈ db₀.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_contra hge + have h1 : k ∈ RangeRead.completeKeys db₀ r := + RangeRead.mem_completeKeys.mpr + ⟨hk, le_of_lt (lt_of_lt_of_le hlo_lt (le_of_not_gt hge)), + by rw [hE₀]; exact WithTop.coe_lt_top k⟩ + exact absurd + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmax₀ k h1)) + (hhi _ (List.head_mem hne))) hge + have hall : ∀ k ∈ db₀.keys, k < x := by + intro k hk + by_contra hge + have h2 : x ≤ k := le_of_not_gt hge + have h1 : k ∈ RangeRead.completeKeys db₀ r := + RangeRead.mem_completeKeys.mpr ⟨hk, hle₀.trans (WithTop.coe_le_coe.mpr h2), + by rw [hE₀]; exact WithTop.coe_lt_top k⟩ + have h4 : k = x := + le_antisymm (le_trans (hmax₀ k h1) hlo) h2 + rw [h4] at hk + exact hx0 hk + exact absurd (head_mem_result_of_insert hread hs hcr hrev hne hx0 + (fun k hk => WithTop.coe_le_coe.mpr (le_of_lt (hall k hk))) + (resolve_end_eq_top_of_insert db₀.sorted hx0 hall_lt hoff)) hx + +/-- When a conflict range captures the whole database, agreement on the range +means agreement on the database, so every read and result is sound. -/ +theorem sound_of_keys_eq {r : RangeRead} {res : Result} {cr : ConflictRange} + (hcr : ∀ db, cr.keys db = db.keys) : Sound r res cr := by + intro db₁ db₂ hagree + rw [AgreeOn] at hagree + rw [hcr db₁, hcr db₂] at hagree + exact RangeRead.compatible_congr + (RangeRead.completeKeys_congr hagree r) + +/-- Soundness is monotone in the conflict range: if agreement on `cr'` implies +agreement on the sound range `cr`, then `cr'` is sound too. -/ +theorem Sound.mono {r : RangeRead} {res : Result} + {cr cr' : ConflictRange} + (hfin : ∀ db₁ db₂, AgreeOn cr' db₁ db₂ → AgreeOn cr db₁ db₂) + (h : Sound r res cr) : Sound r res cr' := by + intro db₁ db₂ hagree + exact h db₁ db₂ (hfin db₁ db₂ hagree) + +/-- A conflict range is minimal for a read and result when it is sound and no +proper subrange of it is sound: every key in the range is needed. -/ +def Minimal (r : RangeRead) (res : Result) (cr : ConflictRange) : Prop := + Sound r res cr ∧ ∀ cr', ProperSubrange cr' cr → ¬ Sound r res cr' + +/-- A minimal conflict range is sound. -/ +theorem Minimal.sound {r : RangeRead} {res : Result} {cr : ConflictRange} + (h : Minimal r res cr) : Sound r res cr := h.1 + +/-- A sound range is minimal when every proper subrange fails to be sound. This +is the introduction rule matching the definition. -/ +theorem minimal_of_sound_of_no_proper {r : RangeRead} {res : Result} + {cr : ConflictRange} (hs : Sound r res cr) + (hsub : ∀ cr', ProperSubrange cr' cr → ¬ Sound r res cr') : Minimal r res cr := + ⟨hs, hsub⟩ + +/-- No proper subrange of a minimal range is sound. -/ +theorem Minimal.not_sound_of_proper {r : RangeRead} {res : Result} {cr : ConflictRange} + (h : Minimal r res cr) {cr' : ConflictRange} (hsub : ProperSubrange cr' cr) : + ¬ Sound r res cr' := h.2 cr' hsub + +/-- No sound range is a proper subrange of a minimal range. -/ +theorem Minimal.not_properSubrange_of_sound {r : RangeRead} {res : Result} + {cr cr' : ConflictRange} (h : Minimal r res cr) (hs : Sound r res cr') + (hsub : ProperSubrange cr' cr) : False := + h.not_sound_of_proper hsub hs + +/-- Minimality is invariant under flipping the traversal order and reversing the +reported keys when the result is complete: soundness is `sound_flip`, the conflict +range is unchanged, and the proper-subrange condition is its own converse. -/ +theorem minimal_flip {r : RangeRead} {res : Result} {cr : ConflictRange} {b : Bool} + (hrev : r.reverse = b) (hmore : res.more = false) : + (Minimal {r with reverse := !b} {keys := res.keys.reverse, more := false} cr ↔ + Minimal r res cr) := by + constructor + · intro h + refine ⟨(sound_flip (b := b) hrev hmore).mp h.sound, fun cr' hsub => ?_⟩ + exact fun hs => h.not_sound_of_proper hsub ((sound_flip (b := b) hrev hmore).mpr hs) + · intro h + refine ⟨(sound_flip (b := b) hrev hmore).mpr h.sound, fun cr' hsub => ?_⟩ + exact fun hs => h.not_sound_of_proper hsub ((sound_flip (b := b) hrev hmore).mp hs) + +/-- Soundness is closed under intersection of conflict ranges. Given agreement on +`cr₁ ⊓ cr₂`, agreement on `cr₁` and on `cr₂` transfer along a bridge database +holding exactly `(db₁ ∩ cr₁) ∪ (db₂ ∩ cr₂)`: on `cr₁` that database agrees with +`db₁`, and on `cr₂` it agrees with `db₂` because the two databases already agree on +the intersection. An inference sees only `(r, res)`, so any one witness database +suffices. -/ +theorem Sound.inter {r : RangeRead} {res : Result} {cr₁ cr₂ : ConflictRange} + (h₁ : Sound r res cr₁) (h₂ : Sound r res cr₂) : + Sound r res (ConflictRange.inter cr₁ cr₂) := by + classical + intro db₁ db₂ hagree + let s : Finset Key := db₁.keys.toFinset.filter (fun k => k ∈ cr₁) ∪ + db₂.keys.toFinset.filter (fun k => k ∈ cr₂) + let db : DB := ⟨s.sort (· ≤ ·), Finset.sortedLT_sort s⟩ + have hmem (k : Key) : k ∈ db.keys ↔ (k ∈ db₁.keys ∧ k ∈ cr₁) ∨ (k ∈ db₂.keys ∧ k ∈ cr₂) := by + simp only [db, s, Finset.mem_sort, Finset.mem_union, Finset.mem_filter, List.mem_toFinset] + -- On `cr₁` the bridge stores exactly what `db₁` stores; the agreement + -- hypothesis is used only for keys lying in both ranges. + have hkey₁ : cr₁.keys db₁ = cr₁.keys db := by + refine eq_of_forall_mem_iff_of_pairwise (ConflictRange.keys_pairwise _ _) + (ConflictRange.keys_pairwise _ _) fun k => ?_ + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff, hmem k] + constructor + · rintro ⟨hk₁, hk₁'⟩ + exact ⟨Or.inl ⟨hk₁, hk₁'⟩, hk₁'⟩ + · rintro ⟨hd, hk₁'⟩ + rcases hd with ⟨hk₁, _⟩ | ⟨hk₂, hk₂'⟩ + · exact ⟨hk₁, hk₁'⟩ + · have hinter : k ∈ (ConflictRange.inter cr₁ cr₂).keys db₂ := by + rw [ConflictRange.mem_keys_iff] + exact ⟨hk₂, ConflictRange.mem_inter_iff.mpr ⟨hk₁', hk₂'⟩⟩ + rw [← hagree] at hinter + exact ⟨(ConflictRange.mem_keys_iff.mp hinter).1, hk₁'⟩ + -- Dually, on `cr₂` the bridge stores exactly what `db₂` stores. + have hkey₂ : cr₂.keys db = cr₂.keys db₂ := by + refine eq_of_forall_mem_iff_of_pairwise (ConflictRange.keys_pairwise _ _) + (ConflictRange.keys_pairwise _ _) fun k => ?_ + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff, hmem k] + constructor + · rintro ⟨hd, hk₂'⟩ + rcases hd with ⟨hk₁, hk₁'⟩ | ⟨hk₂, _⟩ + · have hinter : k ∈ (ConflictRange.inter cr₁ cr₂).keys db₁ := by + rw [ConflictRange.mem_keys_iff] + exact ⟨hk₁, ConflictRange.mem_inter_iff.mpr ⟨hk₁', hk₂'⟩⟩ + rw [hagree] at hinter + exact ⟨(ConflictRange.mem_keys_iff.mp hinter).1, hk₂'⟩ + · exact ⟨hk₂, hk₂'⟩ + · rintro ⟨hk₂, hk₂'⟩ + exact ⟨Or.inr ⟨hk₂, hk₂'⟩, hk₂'⟩ + exact (h₁ db₁ db (show AgreeOn cr₁ db₁ db from hkey₁)).trans + (h₂ db db₂ (show AgreeOn cr₂ db db₂ from hkey₂)) + +/-- A nonincreasing sequence of nonempty subsets of a finite set has nonempty +intersection. This is the finiteness fact that makes soundness survive a descending +sequence: the set of keys on which two databases disagree is finite. -/ +private theorem iInter_nonempty_of_antitone {D : Set Key} (hD : D.Finite) + {T : ℕ → Set Key} (hT : ∀ n, (T n).Nonempty) (hsub : ∀ n, T n ⊆ D) + (hmono : ∀ n, T (n + 1) ⊆ T n) : ∃ x, x ∈ ⋂ n, T n := by + classical + have hle : ∀ {m n : ℕ}, m ≤ n → T n ⊆ T m := by + intro m n hmn + induction hmn with + | refl => exact subset_rfl + | step _ ih => exact (hmono _).trans ih + obtain ⟨N, hN⟩ : ∃ N, (T N).ncard = sInf (Set.range fun n => (T n).ncard) := by + have hmem := Nat.sInf_mem (s := Set.range fun n => (T n).ncard) + ⟨(T 0).ncard, ⟨0, rfl⟩⟩ + rwa [Set.mem_range] at hmem + obtain ⟨x, hx⟩ := hT N + refine ⟨x, ?_⟩ + rw [Set.mem_iInter] + intro n + by_cases h : N ≤ n + · have hsub' : T n ⊆ T N := hle h + have heqc : (T n).ncard = (T N).ncard := + le_antisymm (Set.ncard_le_ncard hsub' (hD.subset (hsub N))) + (by rw [hN]; exact Nat.sInf_le ⟨n, rfl⟩) + have heq : T n = T N := + Set.eq_of_subset_of_ncard_le hsub' (le_of_eq heqc.symm) (hD.subset (hsub N)) + rw [heq]; exact hx + · exact Set.mem_of_subset_of_mem (hle (le_of_lt (Nat.lt_of_not_le h))) hx + +/-- **The intersection of a nonincreasing sequence of sound key sets is sound.** The +key set on which two databases disagree is finite, so along a descending sequence of +sound sets the disagreements cannot all be discarded: the sequence of disagreement +sets, being a descending sequence of nonempty subsets of a finite set, stabilizes, and +its eventual value lies in the intersection. Hence taking a cofinal descending sequence +of the sound ranges, their set intersection is sound; a minimal range exists exactly +when that intersection is realized by a conflict range. This isolates the obstruction +to the existence headline as representability of the sound filter's intersection. -/ +theorem soundSet_iInter_antitone {r : RangeRead} {res : Result} {S : ℕ → Set Key} + (hS : ∀ n, SoundSet r res (S n)) (hmono : ∀ n, S (n + 1) ⊆ S n) : + SoundSet r res (⋂ n, S n) := by + classical + intro db₁ db₂ hagree + by_contra hne + set D : Set Key := {k | (k ∈ db₁.keys) ≠ (k ∈ db₂.keys)} with hD_def + have hDfin : D.Finite := by + refine ((List.finite_toSet db₁.keys).union (List.finite_toSet db₂.keys)).subset ?_ + intro k hk + by_cases h1 : k ∈ db₁.keys + · exact Or.inl h1 + · by_cases h2 : k ∈ db₂.keys + · exact Or.inr h2 + · exact absurd hk (by simp [hD_def, h1, h2]) + have hTn : ∀ n, (D ∩ S n).Nonempty := by + intro n + by_contra hemp + have hpoint : ∀ k, k ∈ S n → (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + intro k hk + by_contra hiff + exact hemp ⟨k, (fun he => hiff (Iff.of_eq he)), hk⟩ + exact hne (hS n db₁ db₂ hpoint) + obtain ⟨k, hk⟩ := iInter_nonempty_of_antitone hDfin hTn + (fun n => Set.inter_subset_left) (fun n => Set.inter_subset_inter subset_rfl (hmono n)) + rw [Set.mem_iInter] at hk + have hkD : k ∈ D := (hk 0).1 + have hkS : k ∈ ⋂ n, S n := Set.mem_iInter.mpr (fun n => (hk n).2) + exact hkD (propext (hagree k hkS)) + +/-- The full conflict range `[⊥, ⊤)` contains every key, so agreement on it is +equality of the stored keys; hence it is sound for every read and result. This is the +base case of the finite-intersection argument. -/ +theorem sound_fullRange (r : RangeRead) (res : Result) : + Sound r res (⟨((↑([] : Key)) : WithTop Key), (⊤ : WithTop Key)⟩ : ConflictRange) := by + have hkey (db : DB) : + (⟨((↑([] : Key)) : WithTop Key), (⊤ : WithTop Key)⟩ : ConflictRange).keys db = db.keys := by + rw [ConflictRange.keys_eq_inRange] + exact RangeRead.inRange_unbounded db.keys + intro db₁ db₂ hagree + have hkeys : db₁.keys = db₂.keys := by + have h := hagree + unfold AgreeOn at h + rw [hkey db₁, hkey db₂] at h + exact h + exact RangeRead.compatible_congr (RangeRead.completeKeys_congr hkeys r) + +/-- A finite set of keys, each omitted by some sound range, is omitted by one sound +range: intersect the finitely many witnesses. This is the step that turns the +finiteness of the disagreement set into soundness of the intersection of *all* sound +ranges, and it needs only that sound ranges are closed under binary intersection, not +that the whole filter is closed under arbitrary intersections. -/ +theorem exists_sound_avoiding_finset (r : RangeRead) (res : Result) (D : Finset Key) + (h : ∀ d ∈ D, ∃ cr : ConflictRange, Sound r res cr ∧ d ∉ cr) : + ∃ cr : ConflictRange, Sound r res cr ∧ ∀ d ∈ D, d ∉ cr := by + classical + induction D using Finset.induction with + | empty => + exact ⟨⟨((↑([] : Key)) : WithTop Key), ⊤⟩, sound_fullRange r res, by simp⟩ + | insert a s _ha ih => + obtain ⟨cra, hsa, hda⟩ := h a (Finset.mem_insert_self a s) + obtain ⟨crs, hss, hds⟩ := ih fun d hd => h d (Finset.mem_insert_of_mem hd) + refine ⟨ConflictRange.inter cra crs, hsa.inter hss, ?_⟩ + intro d hd hmem + rcases Finset.mem_insert.mp hd with rfl | hd + · exact hda (ConflictRange.mem_inter_iff.mp hmem).1 + · exact hds d hd (ConflictRange.mem_inter_iff.mp hmem).2 + +/-- **The necessary keys, as a key set, are sound: the intersection of *all* sound +ranges is sound.** Any two databases that agree on exactly the necessary keys agree on +compatibility. The set of keys on which two databases disagree is finite; each +disagreement key is omitted by some sound range, and finitely many such witnesses +intersect (by `Sound.inter`) to a single sound range the two databases agree on. This +removes the cofinal-descending-sequence caveat of `soundSet_iInter_antitone`: the +intersection is over every sound range, with no countability or cofinality assumption. -/ +theorem soundSet_necessarySet {r : RangeRead} {res : Result} : + SoundSet r res (necessarySet r res) := by + classical + intro db₁ db₂ hagree + set D : Finset Key := + (db₁.keys ++ db₂.keys).toFinset.filter + (fun k => ¬ (k ∈ db₁.keys ↔ k ∈ db₂.keys)) with hD + have hDmem (k : Key) : k ∈ D ↔ ¬ (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + rw [hD, Finset.mem_filter, List.mem_toFinset, List.mem_append] + constructor + · rintro ⟨_, h⟩; exact h + · intro h + refine ⟨?_, h⟩ + by_cases h1 : k ∈ db₁.keys + · exact Or.inl h1 + · refine Or.inr ?_ + by_contra h2 + exact h ⟨fun a => absurd a h1, fun b => absurd b h2⟩ + have avoid : ∀ d ∈ D, ∃ cr : ConflictRange, Sound r res cr ∧ d ∉ cr := by + intro d hd + have hdiff : ¬ (d ∈ db₁.keys ↔ d ∈ db₂.keys) := (hDmem d).mp hd + have hnotnec : ¬ Necessary r res d := fun hnec => hdiff (hagree d hnec) + by_contra hcon + exact hnotnec fun cr hs => by + by_contra hd' + exact hcon ⟨cr, hs, hd'⟩ + obtain ⟨cr, hsound, hcr⟩ := exists_sound_avoiding_finset r res D avoid + have hag : AgreeOn cr db₁ db₂ := by + rw [agreeOn_iff] + intro k hkcr + by_contra hiff + exact hcr k ((hDmem k).mpr hiff) hkcr + exact ⟨fun h => (hsound db₁ db₂ hag).mp h, fun h => (hsound db₁ db₂ hag).mpr h⟩ + +/-- The necessary keys are exactly the intersection of every sound range, made +explicit as a set intersection over the sound ranges. This is the object whose +representability by an interval is the headline. -/ +theorem necessarySet_eq_iInter (r : RangeRead) (res : Result) : + necessarySet r res = ⋂ cr : {cr : ConflictRange // Sound r res cr}, cr.1.toSet := by + ext k + simp only [necessarySet, Set.mem_ofPred_eq, Set.mem_iInter] + constructor + · intro h cr + exact h cr.1 cr.2 + · intro h cr hs + exact h ⟨cr, hs⟩ + +/-- Set-level soundness is monotone under enlargement: a superset of a sound key set +is sound, because agreement on the superset implies agreement on the subset. This is +the key-set form of `Sound.mono`. -/ +theorem soundSet_mono {r : RangeRead} {res : Result} {S T : Set Key} (hST : S ⊆ T) + (hS : SoundSet r res S) : SoundSet r res T := + fun db₁ db₂ hagree => hS db₁ db₂ fun k hkS => hagree k (hST hkS) + +/-- **A range is sound exactly when it contains every necessary key.** The forward +direction is the definition of necessity; the reverse holds because the necessary set +is sound (`soundSet_necessarySet`) and soundness is monotone under enlargement +(`soundSet_mono`). This exhibits the sound filter as the principal up-set of the +necessary set: the headline is exactly that this up-set has a least interval element. -/ +theorem sound_iff_necessarySet_subset {r : RangeRead} {res : Result} {cr : ConflictRange} : + Sound r res cr ↔ necessarySet r res ⊆ cr.toSet := by + constructor + · intro hs k hk + exact hk cr hs + · intro hsub + rw [sound_iff_soundSet] + exact soundSet_mono hsub soundSet_necessarySet + +/-- Every key of a minimal sound conflict range is necessary: intersecting a +minimal range with any sound range keeps soundness, and minimality forbids the +intersection from dropping keys, so the sound range contains the whole minimal +range. An inference sees only `(r, res)`, so any one witness database suffices. -/ +theorem necessary_of_minimal {r : RangeRead} {res : Result} {cr : ConflictRange} + (h : Minimal r res cr) {k : Key} (hk : k ∈ cr) : Necessary r res k := by + intro cr' hs' + have hs : Sound r res (ConflictRange.inter cr cr') := h.sound.inter hs' + have hle : Subrange (ConflictRange.inter cr cr') cr := + subrange_of_le (le_max_left _ _) (min_le_left _ _) + have hge : Subrange cr (ConflictRange.inter cr cr') := by + by_contra hnot + exact h.not_sound_of_proper ⟨hle, hnot⟩ hs + exact subrange_of_le (le_max_right _ _) (min_le_right _ _) k (hge k hk) + +/-- **A minimal range denotes exactly the necessary set.** The keys of a minimal sound +range are all necessary (`necessary_of_minimal`) and, being sound, the range contains +every necessary key; so its key set is the necessary set. This is the concrete form of +the forward direction of `exists_minimal_iff_necessarySet_representable`, and the bridge +that turns a `Minimal` witness into a representability witness for the headline. -/ +theorem necessarySet_eq_of_minimal {r : RangeRead} {res : Result} {cr : ConflictRange} + (h : Minimal r res cr) : necessarySet r res = cr.toSet := by + ext k + exact ⟨fun hk => hk cr h.sound, fun hk => necessary_of_minimal h hk⟩ + +/-- The geometric definition of minimality is equivalent to soundness together +with every key of the range being necessary. `necessary_of_minimal` gives one +direction; conversely, if a sound range's keys are all necessary then any proper +subrange omits a necessary key and hence cannot be sound. This is the form that +matches the algorithm's output: a range tracking exactly the necessary keys. -/ +theorem minimal_iff_sound_and_necessary {r : RangeRead} {res : Result} {cr : ConflictRange} : + Minimal r res cr ↔ Sound r res cr ∧ ∀ k, k ∈ cr → Necessary r res k := by + constructor + · intro h + exact ⟨h.sound, fun k hk => necessary_of_minimal h hk⟩ + · intro ⟨hs, hnec⟩ + refine ⟨hs, fun cr' hsub => ?_⟩ + by_contra hsound + obtain ⟨hle, hnle⟩ := hsub + have hex : ∃ k, k ∈ cr ∧ k ∉ cr' := by + by_contra h + exact hnle fun k hk => by_contra fun hk' => h ⟨k, hk, hk'⟩ + obtain ⟨k, hk, hk'⟩ := hex + exact hk' (hnec k hk cr' hsound) + +/-- A sound range whose keys are all necessary is minimal, phrased through +`NecessaryRange`: this is `minimal_iff_sound_and_necessary` with the pointwise +necessity unfolded into the range's endpoints. It is the form the interval +perturbations match, since those establish a whole `[a, b)` at once. -/ +theorem minimal_iff_sound_and_necessaryRange {r : RangeRead} {res : Result} + {cr : ConflictRange} : + Minimal r res cr ↔ Sound r res cr ∧ NecessaryRange r res cr.begin cr.end_ := by + rw [minimal_iff_sound_and_necessary] + constructor + · rintro ⟨hs, hnec⟩ + exact ⟨hs, fun k hlo hhi => hnec k (ConflictRange.mem_iff.mpr ⟨hlo, hhi⟩)⟩ + · rintro ⟨hs, hnec⟩ + exact ⟨hs, fun k hk => hnec k (ConflictRange.mem_iff.mp hk).1 + (ConflictRange.mem_iff.mp hk).2⟩ + +/-- A minimal sound range is contained in every sound range: intersecting it with +another sound range stays sound and is a subrange, so minimality forbids the +intersection from being proper. This is the least-element half of the +least-sound-range conjecture (issue #14), and it is unconditional — no +nonemptiness or realizability hypothesis is needed. -/ +theorem Minimal.subrange_of_sound {r : RangeRead} {res : Result} {cr : ConflictRange} + (h : Minimal r res cr) {cr' : ConflictRange} (hs : Sound r res cr') : + Subrange cr cr' := by + have hs' : Sound r res (ConflictRange.inter cr cr') := h.sound.inter hs + have hle1 : Subrange (ConflictRange.inter cr cr') cr := + subrange_of_le (le_max_left _ _) (min_le_left _ _) + have hle2 : Subrange (ConflictRange.inter cr cr') cr' := + subrange_of_le (le_max_right _ _) (min_le_right _ _) + have hge : Subrange cr (ConflictRange.inter cr cr') := by + by_contra hnot + exact h.not_sound_of_proper ⟨hle1, hnot⟩ hs' + exact hge.trans hle2 + +/-- **Existence of a minimal range is exactly principality of the sound filter.** A +minimal sound range is precisely a sound range contained in every sound range, so the +open existence headline is equivalent to the filter of sound ranges having a least +element under key-set containment. This is the sharpened form of the goal: a minimal +range exists iff the intersection of all sound ranges — which is sound as a key set by +`soundSet_necessarySet` — is realized by a conflict range. -/ +theorem exists_minimal_iff_exists_least_sound {r : RangeRead} {res : Result} : + (∃ cr : ConflictRange, Minimal r res cr) ↔ + ∃ cr : ConflictRange, Sound r res cr ∧ + ∀ cr' : ConflictRange, Sound r res cr' → Subrange cr cr' := by + constructor + · rintro ⟨cr, h⟩ + exact ⟨cr, h.sound, fun cr' hs => Minimal.subrange_of_sound h hs⟩ + · rintro ⟨cr, hs, hleast⟩ + exact ⟨cr, hs, fun cr' hsub => fun hs' => hsub.2 (hleast cr' hs')⟩ + +/-- **The headline, conditional only on representability of the necessary keys.** If the +set of necessary keys is realized by some conflict range, then a minimal range exists: +that range is sound by `soundSet_necessarySet`, and it is contained in every sound range +because every necessary key lies in every sound range. This isolates the whole existence +headline to the single set-theoretic question of whether the sound filter's intersection +is an interval whose lower end is an attained key. -/ +theorem exists_minimal_of_necessarySet_representable {r : RangeRead} {res : Result} + (h : ∃ cr : ConflictRange, cr.toSet = necessarySet r res) : + ∃ cr : ConflictRange, Minimal r res cr := by + obtain ⟨cr, hcr⟩ := h + refine (exists_minimal_iff_exists_least_sound (r := r) (res := res)).mpr + ⟨cr, ?_, fun cr' hs' => ?_⟩ + · rw [sound_iff_soundSet, hcr]; exact soundSet_necessarySet + · rw [subrange_iff_subset, hcr] + intro k hk + exact hk cr' hs' + +/-- **The headline is exactly representability of the necessary set.** A minimal range +exists for a read and result precisely when the set of necessary keys — sound as a key +set by `soundSet_necessarySet` — is the key set of some conflict range. A minimal range's +keys are all necessary (`necessary_of_minimal`) and contain the necessary keys because it +is sound, so the forward direction is set equality; the reverse is +`exists_minimal_of_necessarySet_representable`. -/ +theorem exists_minimal_iff_necessarySet_representable {r : RangeRead} {res : Result} : + (∃ cr : ConflictRange, Minimal r res cr) ↔ + ∃ cr : ConflictRange, cr.toSet = necessarySet r res := by + constructor + · rintro ⟨cr, hmin⟩ + refine ⟨cr, Set.Subset.antisymm ?_ ?_⟩ + · intro k hk + exact necessary_of_minimal hmin hk + · intro k hk + exact hk cr hmin.sound + · exact exists_minimal_of_necessarySet_representable + +/-- Any two minimal sound ranges for the same read and result denote the same set +of keys. Both are sound, so the intersection is sound and contained in each; +minimality of each then forces the two to contain each other. This is +unconditional: it needs neither a nonempty result nor a realizability witness, +so uniqueness is a theorem rather than a conjecture. -/ +theorem Minimal.toSet_eq {r : RangeRead} {res : Result} {cr cr' : ConflictRange} + (h : Minimal r res cr) (h' : Minimal r res cr') : cr.toSet = cr'.toSet := by + have hs : Sound r res (ConflictRange.inter cr cr') := h.sound.inter h'.sound + have hle1 : Subrange (ConflictRange.inter cr cr') cr := + subrange_of_le (le_max_left _ _) (min_le_left _ _) + have hle2 : Subrange (ConflictRange.inter cr cr') cr' := + subrange_of_le (le_max_right _ _) (min_le_right _ _) + have hge1 : Subrange cr (ConflictRange.inter cr cr') := by + by_contra hnot + exact h.not_sound_of_proper ⟨hle1, hnot⟩ hs + have hge2 : Subrange cr' (ConflictRange.inter cr cr') := by + by_contra hnot + exact h'.not_sound_of_proper ⟨hle2, hnot⟩ hs + exact Set.Subset.antisymm + (subrange_iff_subset.mp (hge1.trans hle2)) + (subrange_iff_subset.mp (hge2.trans hle1)) + +/-- A minimal range packages into the unique-minimal-range statement, with `cr` +itself as the canonical witness: any other minimal range denotes the same set by +`Minimal.toSet_eq`. This is the shared shape of the concrete uniqueness +theorems. -/ +theorem exists_unique_minimal_of_minimal {r : RangeRead} {res : Result} + {cr : ConflictRange} (h : Minimal r res cr) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + ⟨cr, h, fun _ hmin' => Minimal.toSet_eq hmin' h⟩ + +/-! +## The canonical range at offset one + +The first concrete instance of the minimal-range conjecture: when both selectors +have `offset = 1` and the result is complete (`more = false`), the minimal sound +conflict range is the canonical range `[begin.key, end.key)` named directly by +the read. At offset one the resolved window `[resolve begin, resolve end)` cuts a +key exactly when the canonical interval does, so the two filters agree; soundness +is then immediate from `compatible_congr`, and a canonical key is necessary +because toggling it changes the complete keys that a complete result must equal. +-/ + +/-- The canonical conflict range named directly by the read's selector keys. -/ +def canonicalRange (r : RangeRead) : ConflictRange := ⟨r.begin.key, r.end_.key⟩ + +/-- The canonical range is minimal exactly when it is sound and every key in +`[begin.key, end.key)` is necessary. This is the target the interval +perturbations address: `exists_db_resolve_chain_of_disjoint` drives the begin +resolution to any `chain (begin.key) j` inside `[begin.key, end.key)`, so a run +of keys can be shown necessary in one step rather than one toggle at a time. -/ +theorem minimal_canonicalRange_iff {r : RangeRead} {res : Result} : + Minimal r res (canonicalRange r) ↔ + Sound r res (canonicalRange r) ∧ NecessaryRange r res r.begin.key r.end_.key := by + rw [minimal_iff_sound_and_necessaryRange] + rfl + +/-- At a positive offset the selector key is at or below its resolution: the +anchor lands at index `count (· < key) + offset - 1`, and every stored key at or +after the count of keys strictly below `key` is at or above `key`. -/ +theorem key_le_resolve_of_one_le {db : DB} {s : Selector} (hoff : 1 ≤ s.offset) : + s.key ≤ resolve db s := by + rcases eq_or_ne (resolve db s) (⊤ : WithTop Key) with htop | hne + · rw [htop]; exact le_top + · cases hkey : s.key with + | top => exact absurd (resolve_eq_top_of_key_eq_top hkey hoff) hne + | coe k₀ => + obtain ⟨k, hk⟩ : ∃ k : Key, resolve db s = (↑k : WithTop Key) := by + cases h : resolve db s with + | top => exact absurd h hne + | coe k => exact ⟨k, rfl⟩ + rw [hk] + exact WithTop.coe_le_coe.mpr + (le_trans (chain_le k₀ (s.offset - 1).toNat) + (chain_le_of_resolve hkey hoff hk)) + +/-- At offset one the selector key is at or below its resolution. -/ +private theorem key_le_resolve_of_offset_one {db : DB} {s : Selector} (hoff : s.offset = 1) : + s.key ≤ resolve db s := + key_le_resolve_of_one_le (by omega) + +/-- **A positive begin offset is cross-free.** When `begin.offset ≥ 1` the begin selector +resolves at or above `begin.key` (`key_le_resolve_of_one_le`), and the reported head is a +complete key, hence lies at or above the resolution: so `begin.key ≤ res.keys.head`. This +is the general fact that rules out the crossed subcases (`begin.key` above the least +reported key) at every positive begin offset, leaving them only at nonpositive offsets. -/ +theorem begin_key_le_head_of_one_le_offset {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) (hoff : 1 ≤ r.begin.offset) : + r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key) := by + obtain ⟨db₀, hcomp⟩ := hread + have hhead_mem : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.head_mem hne) + exact le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) hoff) + (RangeRead.mem_completeKeys.mp hhead_mem).2.1 + +/-- At offset one, for a stored key, lying at or above the selector key is the +same as lying at or above its resolution: both cut at the first stored key at or +above `s.key`. -/ +theorem key_le_resolve_iff_of_offset_one {db : DB} {s : Selector} + (hoff : s.offset = 1) {k : Key} (hk : k ∈ db.keys) : + s.key ≤ (↑k : WithTop Key) ↔ resolve db s ≤ (↑k : WithTop Key) := by + refine ⟨?_, fun hr => le_trans (key_le_resolve_of_offset_one hoff) hr⟩ + intro hsk + have h1 : 1 ≤ s.offset := by omega + have h0 := anchor_nonneg_of_one_le (db := db) (s := s) h1 + obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hk + set c := db.keys.countP (fun (x : Key) => decide ((↑x : WithTop Key) < s.key)) with hc + have hc_le : c ≤ i := by + cases hkey : s.key with + | top => + exfalso; rw [hkey] at hsk; exact absurd hsk (not_le.mpr (WithTop.coe_lt_top k)) + | coe v => + rw [hkey] at hsk + have hvk : v ≤ k := WithTop.coe_le_coe.mp hsk + have hcv : c = db.keys.countP (fun (x : Key) => decide (x < v)) := by + rw [hc, hkey, countP_lt_coe] + rw [hcv] + have hm : db.keys.countP (fun (x : Key) => decide (x < v)) + ≤ db.keys.countP (fun (x : Key) => decide (x < k)) := + List.countP_mono_left fun x _ hx => by + rw [decide_eq_true_eq] at hx ⊢; exact lt_of_lt_of_le hx hvk + have he : db.keys.countP (fun (x : Key) => decide (x < k)) = i := + countP_lt_eq_of_getElem db.sorted (x := k) hi hik + omega + have htoNat : (s.anchor db).toNat = c := by + rw [anchor_toNat_eq h1, hoff] + simp only [Int.reduceSub, Int.toNat_zero, Nat.add_zero] + exact hc.symm + have hclt : c < db.keys.length := lt_of_le_of_lt hc_le hi + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0), htoNat, List.getElem?_eq_getElem hclt] + refine WithTop.coe_le_coe.mpr ?_ + have hle : db.keys[c]'(lt_of_le_of_lt hc_le hi) ≤ db.keys[i]'hi := + (db.sorted.getElem_le_getElem_iff).mpr hc_le + simpa only [hik] using hle + +/-- At offset one, a stored key lies strictly below the resolution exactly when it +lies strictly below the selector key. -/ +private theorem resolve_lt_iff_of_offset_one {db : DB} {s : Selector} + (hoff : s.offset = 1) {k : Key} (hk : k ∈ db.keys) : + (↑k : WithTop Key) < resolve db s ↔ (↑k : WithTop Key) < s.key := by + rw [lt_iff_not_ge, lt_iff_not_ge, (key_le_resolve_iff_of_offset_one hoff hk)] + +/-- At offset one the resolved in-range keys are exactly the canonical range's +keys: the two filters agree on every stored key by the two cut lemmas. -/ +private theorem inRange_eq_canonical_keys {db : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + (canonicalRange r).keys db := by + refine List.filter_congr fun k hk => ?_ + rw [decide_eq_decide] + change (resolve db r.begin ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < resolve db r.end_) ↔ + (r.begin.key ≤ (↑k : WithTop Key) ∧ (↑k : WithTop Key) < r.end_.key) + rw [key_le_resolve_iff_of_offset_one hbegin hk, resolve_lt_iff_of_offset_one hend hk] + +/-- At offset one the complete keys are the canonical range's keys, reversed when +the read is. -/ +private theorem completeKeys_eq_canonical {db : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + RangeRead.completeKeys db r = + (if r.reverse then ((canonicalRange r).keys db).reverse else (canonicalRange r).keys db) := by + simp only [RangeRead.completeKeys] + rw [inRange_eq_canonical_keys hbegin hend] + +/-! ### Filters and counts, keyed at `Key` + +The generic list lemmas are stated for an abstract element type, so the +elaborator can instantiate their element variable with `WithTop Key` and their +predicate with the coerced one. These wrappers pin both to `Key`. -/ + +/-- Counting an ascending list below a threshold, with the head below it. -/ +private theorem countP_lt_cons_pos {a : Key} {rest : List Key} {t : WithTop Key} + (ha : (↑a : WithTop Key) < t) : + (a :: rest).countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + rest.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) + 1 := + List.countP_cons_of_pos (p := fun (k : Key) => decide ((↑k : WithTop Key) < t)) (a := a) + (by simpa using ha) + +/-- Counting an ascending list below a threshold, with the head not below it. -/ +private theorem countP_lt_cons_neg {a : Key} {rest : List Key} {t : WithTop Key} + (ha : ¬ ((↑a : WithTop Key) < t)) : + (a :: rest).countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + rest.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) := + List.countP_cons_of_neg (p := fun (k : Key) => decide ((↑k : WithTop Key) < t)) (a := a) + (by simpa using ha) + +/-- Filtering below a threshold, with the head below it. -/ +private theorem filter_lt_cons_pos {a : Key} {rest : List Key} {t : WithTop Key} + (ha : (↑a : WithTop Key) < t) : + (a :: rest).filter (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + a :: rest.filter (fun (k : Key) => decide ((↑k : WithTop Key) < t)) := + List.filter_cons_of_pos (p := fun (k : Key) => decide ((↑k : WithTop Key) < t)) (a := a) + (by simpa using ha) + +/-- Filtering below a threshold, with the head not below it. -/ +private theorem filter_lt_cons_neg {a : Key} {rest : List Key} {t : WithTop Key} + (ha : ¬ ((↑a : WithTop Key) < t)) : + (a :: rest).filter (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + rest.filter (fun (k : Key) => decide ((↑k : WithTop Key) < t)) := + List.filter_cons_of_neg (p := fun (k : Key) => decide ((↑k : WithTop Key) < t)) (a := a) + (by simpa using ha) + +/-- Filtering at or above a threshold, with the head below it. -/ +private theorem filter_not_lt_cons_pos {a : Key} {rest : List Key} {t : WithTop Key} + (ha : (↑a : WithTop Key) < t) : + (a :: rest).filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) = + rest.filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) := + List.filter_cons_of_neg (p := fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) (a := a) + (by simpa using ha) + +/-- Filtering at or above a threshold, with the head not below it. -/ +private theorem filter_not_lt_cons_neg {a : Key} {rest : List Key} {t : WithTop Key} + (ha : ¬ ((↑a : WithTop Key) < t)) : + (a :: rest).filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) = + a :: rest.filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) := + List.filter_cons_of_pos (p := fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) (a := a) + (by simpa using ha) + +/-- A list with nothing below a threshold filters below it to nothing. -/ +private theorem filter_lt_eq_nil_of_forall {l : List Key} {t : WithTop Key} + (h : ∀ k ∈ l, ¬ ((↑k : WithTop Key) < t)) : + l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk + simpa using h k hk + +/-- A list with nothing below a threshold counts nothing below it. -/ +private theorem countP_lt_eq_zero_of_forall {l : List Key} {t : WithTop Key} + (h : ∀ k ∈ l, ¬ ((↑k : WithTop Key) < t)) : + l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + simpa using h k hk + +/-- A list with nothing below a threshold filters at or above it to itself. -/ +private theorem filter_not_lt_eq_self_of_forall {l : List Key} {t : WithTop Key} + (h : ∀ k ∈ l, ¬ ((↑k : WithTop Key) < t)) : + l.filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) = l := by + rw [List.filter_eq_self] + intro k hk + simpa using h k hk + +/-- Filtering an ascending list at a threshold yields its take at the count of +elements below the threshold. -/ +theorem filter_lt_eq_take_countP {l : List Key} (hs : l.SortedLT) (t : WithTop Key) : + l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + l.take (l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t))) := by + induction l with + | nil => rfl + | cons a rest ih => + have hs' : rest.SortedLT := + List.sortedLT_iff_pairwise.mpr (List.pairwise_cons.mp hs.pairwise).2 + by_cases ha : (↑a : WithTop Key) < t + · rw [filter_lt_cons_pos ha, countP_lt_cons_pos ha, List.take_succ_cons] + exact congrArg (a :: ·) (ih hs') + · have hnp : ∀ k ∈ rest, ¬ ((↑k : WithTop Key) < t) := by + intro k hk hklt + exact ha (lt_trans + (WithTop.coe_lt_coe.mpr ((List.pairwise_cons.mp hs.pairwise).1 k hk)) hklt) + rw [filter_lt_cons_neg ha, filter_lt_eq_nil_of_forall hnp, + countP_lt_cons_neg ha, countP_lt_eq_zero_of_forall hnp] + rfl + +/-- Filtering an ascending list at or above a threshold yields its drop at the +count of elements below the threshold. -/ +theorem filter_not_lt_eq_drop_countP {l : List Key} (hs : l.SortedLT) + (t : WithTop Key) : + l.filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < t))) = + l.drop (l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t))) := by + induction l with + | nil => rfl + | cons a rest ih => + have hs' : rest.SortedLT := + List.sortedLT_iff_pairwise.mpr (List.pairwise_cons.mp hs.pairwise).2 + by_cases ha : (↑a : WithTop Key) < t + · rw [filter_not_lt_cons_pos ha, countP_lt_cons_pos ha, List.drop_succ_cons] + exact ih hs' + · have hnp : ∀ k ∈ rest, ¬ ((↑k : WithTop Key) < t) := by + intro k hk hklt + exact ha (lt_trans + (WithTop.coe_lt_coe.mpr ((List.pairwise_cons.mp hs.pairwise).1 k hk)) hklt) + rw [filter_not_lt_cons_neg ha, filter_not_lt_eq_self_of_forall hnp, + countP_lt_cons_neg ha, countP_lt_eq_zero_of_forall hnp, List.drop_zero] + +/-- At a positive offset the resolution is the `(offset - 1)`-th stored key at or above +the selector key: the keys at or above `s.key` are exactly the drop at the count of keys +strictly below it, and the anchor lands `offset - 1` places into that drop. -/ +theorem resolve_eq_filter_getElem? {db : DB} {s : Selector} (hoff : 1 ≤ s.offset) : + resolve db s = + (match (db.keys.filter + (fun (k : Key) => decide (s.key ≤ (k : WithTop Key))))[(s.offset - 1).toNat]? with + | some k => (↑k : WithTop Key) + | none => ⊤) := by + cases hk : s.key with + | top => + rw [resolve_eq_top_of_key_eq_top hk hoff] + have hnil : db.keys.filter + (fun (k : Key) => decide ((⊤ : WithTop Key) ≤ (k : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro k _ + simp + rw [hnil, List.getElem?_nil] + | coe k₀ => + have hp : (fun (k : Key) => decide ((↑k₀ : WithTop Key) ≤ (k : WithTop Key))) = + (fun (k : Key) => decide (¬ ((k : WithTop Key) < (↑k₀ : WithTop Key)))) := by + funext k + exact decide_eq_decide.mpr + ⟨fun h hlt => absurd hlt (not_lt.mpr h), fun h => le_of_not_gt h⟩ + rw [hp, filter_not_lt_eq_drop_countP db.sorted (↑k₀ : WithTop Key), List.getElem?_drop] + have h0 : 0 ≤ s.anchor db := by + rw [anchor_of_coe hk] + have := Int.natCast_nonneg (db.keys.countP (fun (k : Key) => decide (k < k₀))) + omega + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0), anchor_toNat_eq hoff, hk] + rfl + +/-- **At a positive offset, resolution depends only on the stored keys at or above the +selector key.** If two databases agree on every stored key at or above `s.key`, then at +offset `≥ 1` the anchor index is `offset - 1` places into the keys at or above `s.key` +(`resolve_eq_filter_getElem?`), and those keys coincide, so `resolve` is the same. This is +the database-relative counterpart of `mem_footprint_iff`'s positive-offset case, and the +key tool for proving soundness of the top range `[begin.key, ⊤)` at positive begin offset. -/ +theorem resolve_eq_of_agree_ge {db₁ db₂ : DB} {s : Selector} (hoff : 1 ≤ s.offset) + (hagree : ∀ (k : Key), s.key ≤ (k : WithTop Key) → (k ∈ db₁.keys ↔ k ∈ db₂.keys)) : + resolve db₁ s = resolve db₂ s := by + have hL : db₁.keys.filter (fun (k : Key) => decide (s.key ≤ (k : WithTop Key))) = + db₂.keys.filter (fun (k : Key) => decide (s.key ≤ (k : WithTop Key))) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + · intro k + rw [List.mem_filter, List.mem_filter] + exact ⟨fun hk => ⟨(hagree k (of_decide_eq_true hk.2)).mp hk.1, hk.2⟩, + fun hk => ⟨(hagree k (of_decide_eq_true hk.2)).mpr hk.1, hk.2⟩⟩ + rw [resolve_eq_filter_getElem? hoff (db := db₁), + resolve_eq_filter_getElem? hoff (db := db₂), hL] + +/-- Agreement on the top range `[begin.key, ⊤)` is exactly agreement on every stored key +at or above `begin.key`: the top range contains every such key and no other. -/ +theorem agreeOn_topRange_iff {db₁ db₂ : DB} {r : RangeRead} : + AgreeOn ⟨r.begin.key, ⊤⟩ db₁ db₂ ↔ + ∀ (k : Key), r.begin.key ≤ (k : WithTop Key) → (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + rw [AgreeOn] + constructor + · intro h k hk + have h1 := congrArg (fun l : List Key => k ∈ l) h + have hcr : k ∈ (⟨r.begin.key, ⊤⟩ : ConflictRange) := ⟨hk, WithTop.coe_lt_top k⟩ + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff] at h1 + exact ⟨fun hk1 => (h1.mp ⟨hk1, hcr⟩).1, fun hk2 => (h1.mpr ⟨hk2, hcr⟩).1⟩ + · intro h + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + · intro k + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff] + exact ⟨fun hk => ⟨(h k hk.2.1).mp hk.1, hk.2⟩, + fun hk => ⟨(h k hk.2.1).mpr hk.1, hk.2⟩⟩ + +/-- **The top range is sound at positive offsets when `begin.key ≤ end_.key`.** Agreement +on `[begin.key, ⊤)` fixes `resolve begin` (`resolve_eq_of_agree_ge`) and, since +`begin.key ≤ end_.key`, also fixes `resolve end_`; the resolved window lies at or above +`begin.key` (`key_le_resolve_of_one_le`), so its stored contents are fixed as well, and the +complete keys coincide. -/ +theorem sound_topRange_of_one_le_le {r : RangeRead} {res : Result} + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hk : r.begin.key ≤ r.end_.key) : + Sound r res ⟨r.begin.key, ⊤⟩ := by + intro db₁ db₂ hag + have hagree : ∀ (k : Key), r.begin.key ≤ (k : WithTop Key) → + (k ∈ db₁.keys ↔ k ∈ db₂.keys) := agreeOn_topRange_iff.mp hag + have hrb : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_agree_ge hbegin hagree + have hre : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_agree_ge hend fun k hk' => hagree k (le_trans hk hk') + have hwin : (db₁.keys.filter (fun (k : Key) => decide + (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_))) = + (db₂.keys.filter (fun (k : Key) => decide + (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_))) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + · intro k + rw [List.mem_filter, List.mem_filter] + refine ⟨fun hk1 => ⟨?_, hk1.2⟩, fun hk2 => ⟨?_, hk2.2⟩⟩ + · exact (hagree k (le_trans + (key_le_resolve_of_one_le (db := db₁) (s := r.begin) hbegin) + (of_decide_eq_true hk1.2).1)).mp hk1.1 + · exact (hagree k (le_trans + (key_le_resolve_of_one_le (db := db₁) (s := r.begin) hbegin) + (of_decide_eq_true hk2.2).1)).mpr hk2.1 + have hin : RangeRead.inRange db₁.keys (resolve db₁ r.begin) (resolve db₁ r.end_) = + RangeRead.inRange db₂.keys (resolve db₂ r.begin) (resolve db₂ r.end_) := by + unfold RangeRead.inRange + rw [hwin, hrb, hre] + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys + rw [hin] + exact RangeRead.compatible_congr hck + +/-- **Lower endpoint attained at positive offsets when `begin.key ≤ end_.key`.** For a +realizable forward read with a nonempty result, `1 ≤ begin.offset`, `1 ≤ end_.offset`, and +`begin.key ≤ end_.key`, the least necessary key is `begin.key`: it is necessary +(`necessary_begin_key`) and lies below every necessary key, since the top range +`[begin.key, ⊤)` is sound (`sound_topRange_of_one_le_le`) and hence contains them all. -/ +theorem exists_least_necessary_of_one_le_le {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hk : r.begin.key ≤ r.end_.key) : + ∃ A : Key, Necessary r res A ∧ ∀ k : Key, Necessary r res k → A ≤ k := by + have htop : Sound r res ⟨r.begin.key, ⊤⟩ := sound_topRange_of_one_le_le hbegin hend hk + have hhead : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key) := + begin_key_le_head_of_one_le_offset hread hne hbegin + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := fun h => by + rw [h] at hhead + exact absurd hhead (not_le.mpr (WithTop.coe_lt_top (res.keys.head hne))) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + refine ⟨v, necessary_begin_key hread hrev (List.length_pos_of_ne_nil hne) hbegin hv, ?_⟩ + intro k hk' + have hmem := (sound_iff_necessarySet_subset.mp htop) hk' + have hle : r.begin.key ≤ (k : WithTop Key) := hmem.1 + rw [hv] at hle + exact WithTop.coe_le_coe.mp hle + +/-- **Lower endpoint attained at positive offsets for a complete read, in either +traversal order.** This drops the `reverse = false` hypothesis of +`exists_least_necessary_of_one_le_le`: a complete reversed read is flipped to a complete +forward one (`necessary_flip`), where the same least key `begin.key` is found, and +necessity is carried back across the flip. The value is unchanged because flipping +preserves `begin.key`. -/ +theorem exists_least_necessary_of_one_le_le_complete {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hk : r.begin.key ≤ r.end_.key) : + ∃ A : Key, Necessary r res A ∧ ∀ k : Key, Necessary r res k → A ≤ k := by + cases hr : r.reverse + · exact exists_least_necessary_of_one_le_le hread hr hne hbegin hend hk + · obtain ⟨db₀, hcomp⟩ := hread + have hread' : Readable {r with reverse := false} + {keys := res.keys.reverse, more := false} := + ⟨db₀, (compatible_flip (db := db₀) (b := true) hr hmore).mpr hcomp⟩ + obtain ⟨A, hnec', hminimal'⟩ := + exists_least_necessary_of_one_le_le (r := {r with reverse := false}) + (res := {keys := res.keys.reverse, more := false}) hread' rfl + (by simpa using hne) hbegin hend hk + refine ⟨A, (necessary_flip (k := A) (b := true) hr hmore).mp hnec', ?_⟩ + intro k hk' + exact hminimal' k ((necessary_flip (k := k) (b := true) hr hmore).mpr hk') + +/-- The count of elements below a threshold in a prefix of an ascending list is +the minimum of the prefix length and the whole-list count. -/ +theorem countP_take_eq_min {l : List Key} (hs : l.SortedLT) (t : WithTop Key) + (c : Nat) : + (l.take c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + min c (l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t))) := by + induction l generalizing c with + | nil => simp + | cons a rest ih => + have hs' : rest.SortedLT := + List.sortedLT_iff_pairwise.mpr (List.pairwise_cons.mp hs.pairwise).2 + rcases c with _ | c' + · simp + · rw [List.take_succ_cons] + by_cases ha : (↑a : WithTop Key) < t + · rw [countP_lt_cons_pos ha, ih hs' c', countP_lt_cons_pos ha] + rw [Nat.min_def] + split <;> omega + · have hnp : ∀ k ∈ rest, ¬ ((↑k : WithTop Key) < t) := by + intro k hk hklt + exact ha (lt_trans + (WithTop.coe_lt_coe.mpr ((List.pairwise_cons.mp hs.pairwise).1 k hk)) hklt) + rw [countP_lt_cons_neg ha, ih hs' c', countP_lt_eq_zero_of_forall hnp, + countP_lt_cons_neg ha, countP_lt_eq_zero_of_forall hnp, + Nat.min_eq_right (Nat.zero_le c'), Nat.min_eq_right (Nat.zero_le (c' + 1))] + +/-- A prefix long enough to contain every element below the threshold counts them +all. -/ +private theorem countP_take_eq_of_le {l : List Key} (hs : l.SortedLT) {t : WithTop Key} + {c : Nat} (hc : l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) ≤ c) : + (l.take c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) := by + rw [countP_take_eq_min hs t c, Nat.min_eq_right hc] + +/-- The count of elements below a threshold in a suffix of an ascending list. -/ +theorem countP_drop_eq {l : List Key} (hs : l.SortedLT) (t : WithTop Key) (c : Nat) : + (l.drop c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) - + min c (l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t))) := by + have hsplit : l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) = + (l.take c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) + + (l.drop c).countP (fun (k : Key) => decide ((↑k : WithTop Key) < t)) := by + conv_lhs => rw [← List.take_append_drop c l] + rw [List.countP_append] + rw [countP_take_eq_min hs t c] at hsplit + omega + +/-- Truncated subtraction ignores a larger minuend. -/ +private theorem nat_sub_min_right (a b : Nat) : a - min b a = a - b := by + rcases le_total b a with h | h + · rw [Nat.min_eq_left h] + · rw [Nat.min_eq_right h, Nat.sub_eq_zero_of_le h, Nat.sub_self] + +/-- Taking a prefix and dropping a prefix gives the same as dropping the larger. -/ +private theorem take_drop_min {α : Type*} {l : List α} (A B : Nat) : + (l.take A).drop (min A B) = (l.drop B).take (A - B) := by + rcases le_total A B with h | h + · rw [Nat.min_eq_left h, List.drop_take_self, Nat.sub_eq_zero_of_le h, List.take_zero] + · rw [Nat.min_eq_right h, List.drop_take] + +/-- In a strictly increasing list, if a suffix `l.drop b` is nonempty and all its +elements are at most `m`, then all the elements of `l` are at most `m`: the dropped +prefix sits below the first surviving element. -/ +private theorem forall_le_of_drop_eq {l g : List Key} (hs : l.SortedLT) {m : Key} {b : Nat} + (h : l.drop b = g) (hgne : g ≠ []) (hg : ∀ k ∈ g, k ≤ m) : ∀ k ∈ l, k ≤ m := by + intro k hk + have hdrop_ne : l.drop b ≠ [] := h ▸ hgne + obtain ⟨x, rest, hx⟩ := List.exists_cons_of_ne_nil hdrop_ne + have hxdrop : x ∈ l.drop b := by rw [hx]; exact List.mem_cons_self + have hxmem : x ∈ g := by rw [← h]; exact hxdrop + have hpw : (l.take b ++ l.drop b).Pairwise (· < ·) := by + rw [List.take_append_drop]; exact hs.pairwise + rw [List.pairwise_append] at hpw + obtain ⟨_, _, hcross⟩ := hpw + rw [← List.take_append_drop b l] at hk + rcases List.mem_append.mp hk with hk | hk + · exact le_trans (le_of_lt (hcross k hk x hxdrop)) (hg x hxmem) + · exact hg k (h ▸ hk) + +/-- The count of stored keys strictly below a selector's resolution is the +resolved index, clamped to the database length. -/ +theorem countP_lt_resolve {db : DB} {s : Selector} : + db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < resolve db s)) = + min (s.anchor db).toNat db.keys.length := by + by_cases h0 : 0 ≤ s.anchor db + · by_cases hlt : (s.anchor db).toNat < db.keys.length + · rw [resolve_eq_getElem h0 hlt, countP_lt_coe, + countP_lt_eq_of_getElem db.sorted hlt rfl, Nat.min_eq_left (le_of_lt hlt)] + · have hge : db.keys.length ≤ (s.anchor db).toNat := le_of_not_gt hlt + rw [resolve_eq_top h0 hge, countP_lt_top, Nat.min_eq_right hge] + · have hneg : s.anchor db < 0 := lt_of_not_ge h0 + rw [resolve_eq_nil hneg, countP_lt_nil, Int.toNat_of_nonpos (le_of_lt hneg)] + simp + +/-- Nothing at all lies at or above `⊤` in `WithTop Key`. -/ +private theorem filter_top_le_eq_nil {l : List Key} : + l.filter (fun (k : Key) => decide ((⊤ : WithTop Key) ≤ (↑k : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro k _ + simp only [decide_eq_true_eq, not_le] + exact WithTop.coe_lt_top k + +/-- The empty key is at or below every `WithTop Key` bound. -/ +private theorem coe_nil_le (t : WithTop Key) : ((([] : Key) : WithTop Key)) ≤ t := by + cases t with + | top => exact le_top + | coe k => exact WithTop.coe_le_coe.mpr (nil_le_key k) + +/-- Filtering an ascending list at the bottom of `WithTop Key` keeps it all. -/ +private theorem filter_coe_nil_le_eq_self {l : List Key} : + l.filter (fun (k : Key) => decide ((([] : Key) : WithTop Key) ≤ (↑k : WithTop Key))) = l := by + rw [List.filter_eq_self] + intro k _ + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr (nil_le_key k) + +/-- The stored keys at or above a selector's resolution are the stored keys from +the resolved index on. -/ +theorem filter_resolve_le_eq_drop {db : DB} {s : Selector} : + db.keys.filter (fun (k : Key) => decide (resolve db s ≤ (↑k : WithTop Key))) = + db.keys.drop (s.anchor db).toNat := by + by_cases h0 : 0 ≤ s.anchor db + · by_cases hlt : (s.anchor db).toNat < db.keys.length + · rw [resolve_eq_getElem h0 hlt] + have hcongr : db.keys.filter (fun (k : Key) => decide + ((↑db.keys[(s.anchor db).toNat] : WithTop Key) ≤ (↑k : WithTop Key))) = + db.keys.filter (fun (k : Key) => decide + (¬ ((↑k : WithTop Key) < (↑db.keys[(s.anchor db).toNat] : WithTop Key)))) := + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt] + rw [hcongr, filter_not_lt_eq_drop_countP db.sorted, countP_lt_coe, + countP_lt_eq_of_getElem db.sorted hlt rfl] + · have hge : db.keys.length ≤ (s.anchor db).toNat := le_of_not_gt hlt + rw [resolve_eq_top h0 hge, filter_top_le_eq_nil, List.drop_eq_nil_of_le hge] + · have hneg : s.anchor db < 0 := lt_of_not_ge h0 + rw [resolve_eq_nil hneg, Int.toNat_of_nonpos (le_of_lt hneg), + filter_coe_nil_le_eq_self, List.drop_zero] + +/-- A selector at a nonpositive offset resolves at or below its own key. -/ +theorem resolve_le_key_of_offset_nonpos {db : DB} {s : Selector} + (hoff : s.offset ≤ 0) : resolve db s ≤ s.key := by + rcases lt_or_ge (s.anchor db) 0 with hneg | hpos + · rw [resolve_eq_nil hneg] + exact coe_nil_le s.key + · have hcnt : (s.anchor db).toNat < + db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) := + (Int.toNat_lt hpos).mpr (by simp only [Selector.anchor]; omega) + have hlt : (s.anchor db).toNat < db.keys.length := + lt_of_lt_of_le hcnt (List.countP_le_length) + rw [resolve_eq_getElem hpos hlt] + exact le_of_lt (getElem_lt_of_lt_countP_top db.sorted hlt hcnt) + +/-- **The end-offset-at-most-one window.** A non-reversed read whose begin is at +offset one and whose end offset is at most one has complete keys equal to the +canonical keys with a fixed number of trailing keys removed: exactly +`(1 - end_.offset).toNat` of them. That count depends only on `end_.offset`, and +the length it truncates to does not mention the database, so the complete keys +are determined by the canonical keys alone. -/ +private theorem completeKeys_eq_canonical_take {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) : + RangeRead.completeKeys db r = + ((canonicalRange r).keys db).take + (((canonicalRange r).keys db).length - (1 - r.end_.offset).toNat) := by + set l := db.keys with hl + set p := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) with hp + set c₀ := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) with hc₀ + set k := (1 - r.end_.offset).toNat with hk + set d := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < resolve db r.end_)) with hd + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hc₀_le : c₀ ≤ l.length := by rw [hc₀]; exact List.countP_le_length + have hp_anchor : (r.begin.anchor db).toNat = p := by + rw [anchor_toNat_eq (s := r.begin) (by omega : 1 ≤ r.begin.offset), hbegin, hp, hl] + norm_num + have hk_int : ((k : Nat) : Int) = 1 - r.end_.offset := by + rw [hk] + exact Int.toNat_of_nonneg (by omega) + have ha : r.end_.anchor db = (c₀ : Int) - k := by + unfold Selector.anchor + rw [← hl, ← hc₀, hk_int] + ring + have hd_eq : d = c₀ - k := by + have hmin : d = min (r.end_.anchor db).toNat l.length := by + rw [hd, hl] + exact countP_lt_resolve + have htoNat : (r.end_.anchor db).toNat = c₀ - k := by + rw [ha] + exact Int.toNat_sub c₀ k + rw [hmin, htoNat, Nat.min_eq_left (le_trans (Nat.sub_le _ _) hc₀_le)] + have hA : l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key))) = + l.drop p := by + rw [hl, filter_resolve_le_eq_drop (db := db) (s := r.begin), hp_anchor] + have hB₀ : l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + l.take c₀ := by + rw [hc₀] + exact filter_lt_eq_take_countP hls r.end_.key + have hcanon : (canonicalRange r).keys db = (l.drop p).take (c₀ - p) := by + have hsplit : (canonicalRange r).keys db = + l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key)) && + decide ((↑k : WithTop Key) < r.end_.key)) := by + rw [canonicalRange, ConflictRange.keys, ← hl] + have hstep : l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key)) = + l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key)) && + decide ((↑k : WithTop Key) < r.end_.key)) := + List.filter_congr fun k _ => by rw [Bool.decide_and] + rw [hstep, ← List.filter_filter] + have htake : (l.take c₀).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.take (i := c₀) hls.pairwise) + have hct : (l.take c₀).countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) = + min c₀ p := by + rw [countP_take_eq_min hls r.begin.key c₀, ← hp] + rw [hsplit, ← List.filter_filter, hB₀] + rw [show (l.take c₀).filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) = + (l.take c₀).filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < r.begin.key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt]] + rw [filter_not_lt_eq_drop_countP htake r.begin.key, hct, take_drop_min] + have hLHS : RangeRead.completeKeys db r = (l.drop p).take (d - p) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + have hdrop : (l.drop p).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter + (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.filter + (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) = + l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (↑k : WithTop Key))) from by + rw [← hl] + exact List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hA, filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) p, ← hd, nat_sub_min_right] + rw [hLHS, hcanon] + have hlen : ((l.drop p).take (c₀ - p)).length = c₀ - p := by + rw [List.length_take, List.length_drop, + Nat.min_eq_left (Nat.sub_le_sub_right hc₀_le p)] + rw [hlen, List.take_take, Nat.min_eq_left (Nat.sub_le (c₀ - p) k), hd_eq] + congr 1 + omega + +/-- **The end-offset-at-most-one window at a general positive begin offset.** A +non-reversed read with `begin.offset ≥ 1` and `end_.offset ≤ 1` has complete keys +equal to a left-and-right truncation of the canonical keys: drop the first +`(begin.offset - 1).toNat` canonical keys (the begin selector overshoots that many +keys past its own key) and keep only the first `length - (1 - end_.offset).toNat` +of what remains. Both counts depend only on the offsets, so the complete keys are +determined by the canonical keys alone. This generalizes +`completeKeys_eq_canonical_take` (the `begin.offset = 1` case, where the left drop +is zero). -/ +theorem completeKeys_eq_canonical_slice {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbeginB : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : + RangeRead.completeKeys db r = + (((canonicalRange r).keys db).drop (r.begin.offset - 1).toNat).take + ((((canonicalRange r).keys db).length - (r.begin.offset - 1).toNat) - + (1 - r.end_.offset).toNat) := by + set l := db.keys with hl + set p := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) with hp + set c₀ := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) with hc₀ + set b := (r.begin.offset - 1).toNat with hb + set k := (1 - r.end_.offset).toNat with hk + set d := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < resolve db r.end_)) with hd + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hc₀_le : c₀ ≤ l.length := by rw [hc₀]; exact List.countP_le_length + have hp_anchor : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) hbeginB, hp, hb, hl] + have hk_int : ((k : Nat) : Int) = 1 - r.end_.offset := by + rw [hk]; exact Int.toNat_of_nonneg (by omega) + have ha : r.end_.anchor db = (c₀ : Int) - k := by + unfold Selector.anchor + rw [← hl, ← hc₀, hk_int] + ring + have hd_eq : d = c₀ - k := by + have hmin : d = min (r.end_.anchor db).toNat l.length := by + rw [hd, hl]; exact countP_lt_resolve + have htoNat : (r.end_.anchor db).toNat = c₀ - k := by + rw [ha]; exact Int.toNat_sub c₀ k + rw [hmin, htoNat, Nat.min_eq_left (le_trans (Nat.sub_le _ _) hc₀_le)] + have hA : l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key))) = + l.drop (p + b) := by + rw [hl, filter_resolve_le_eq_drop (db := db) (s := r.begin), hp_anchor] + have hB₀ : l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + l.take c₀ := by + rw [hc₀]; exact filter_lt_eq_take_countP hls r.end_.key + have hcanon : (canonicalRange r).keys db = (l.drop p).take (c₀ - p) := by + have hsplit : (canonicalRange r).keys db = + l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key)) && + decide ((↑k : WithTop Key) < r.end_.key)) := by + rw [canonicalRange, ConflictRange.keys, ← hl] + have hstep : l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key)) = + l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key)) && + decide ((↑k : WithTop Key) < r.end_.key)) := + List.filter_congr fun k _ => by rw [Bool.decide_and] + rw [hstep, ← List.filter_filter] + have htake : (l.take c₀).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.take (i := c₀) hls.pairwise) + have hct : (l.take c₀).countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) = + min c₀ p := by + rw [countP_take_eq_min hls r.begin.key c₀, ← hp] + rw [hsplit, ← List.filter_filter, hB₀] + rw [show (l.take c₀).filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) = + (l.take c₀).filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < r.begin.key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt]] + rw [filter_not_lt_eq_drop_countP htake r.begin.key, hct, take_drop_min] + have hLHS : RangeRead.completeKeys db r = (l.drop (p + b)).take (d - (p + b)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + have hdrop : (l.drop (p + b)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p + b) hls.pairwise) + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter + (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.filter + (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) = + l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (↑k : WithTop Key))) from by + rw [← hl] + exact List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hA, filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) (p + b), ← hd, nat_sub_min_right] + rw [hLHS, hcanon] + have hlen : ((l.drop p).take (c₀ - p)).length = c₀ - p := by + rw [List.length_take, List.length_drop, + Nat.min_eq_left (Nat.sub_le_sub_right hc₀_le p)] + rw [hlen, hd_eq, List.drop_take, List.drop_drop, List.take_take] + congr 1 + omega + +/-- **The end-offset-at-most-one window, reversed, general positive begin offset.** A +reversed read with `begin.offset ≥ 1` and `end_.offset ≤ 1` has complete keys equal to +the *reverse* of the non-reversed slice of `completeKeys_eq_canonical_slice`: reversal +only reverses the in-range keys, so the same left-and-right truncation of the canonical +keys determines the complete keys in descending order. -/ +theorem completeKeys_eq_canonical_slice_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbeginB : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : + RangeRead.completeKeys db r = + ((((canonicalRange r).keys db).drop (r.begin.offset - 1).toNat).take + ((((canonicalRange r).keys db).length - (r.begin.offset - 1).toNat) - + (1 - r.end_.offset).toNat)).reverse := by + have hck : RangeRead.completeKeys db r = + (RangeRead.completeKeys db {r with reverse := false}).reverse := by + simp [RangeRead.completeKeys, hrev] + rw [hck, completeKeys_eq_canonical_slice (db := db) (r := {r with reverse := false}) + rfl hbeginB hend] + rfl + +/-- **Soundness of the canonical range at end offsets at most one.** A +non-reversed read whose begin is at offset one and whose end offset is at most one +has complete keys equal to a fixed-length truncation of the canonical keys +(`completeKeys_eq_canonical_take`), so databases agreeing on `[begin.key, end.key)` +have identical complete keys and hence identical compatibility with every +result. -/ +theorem sound_canonicalRange_of_end_offset_le_one {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) : + Sound r res (canonicalRange r) := by + intro db₁ db₂ hagree + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_canonical_take (db := db₁) hrev hbegin hend, + completeKeys_eq_canonical_take (db := db₂) hrev hbegin hend, + show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree] + exact RangeRead.compatible_congr hck + +/-- **The empty result at end offsets at most one.** A non-reversed read whose +begin is at offset one and whose end offset is at most one is compatible with the +empty complete result exactly when the canonical keys number at most +`(1 - end_.offset).toNat`: the complete keys are a truncation of the canonical +keys to `length - (1 - end_.offset).toNat` (`completeKeys_eq_canonical_take`), +and an empty result demands that truncation be empty. -/ +theorem compatible_empty_iff_length_le {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + ((canonicalRange r).keys db).length ≤ (1 - r.end_.offset).toNat := by + unfold RangeRead.Compatible RangeRead.withinLimit + rw [completeKeys_eq_canonical_take (db := db) hrev hbegin hend] + constructor + · intro hc + obtain ⟨-, h2⟩ := hc + rcases h2 with ⟨hm, -⟩ | ⟨-, heq⟩ + · simp at hm + · rw [eq_comm, List.take_eq_nil_iff] at heq + rcases heq with h | h + · exact Nat.le_of_sub_eq_zero h + · simp [h] + · intro h + refine ⟨?_, Or.inr ⟨rfl, ?_⟩⟩ + · cases r.limit <;> simp + rw [eq_comm, List.take_eq_nil_iff] + exact Or.inl (Nat.sub_eq_zero_of_le h) + +/-- A compatible result is the empty one exactly when the complete keys are +empty. -/ +theorem compatible_empty_iff_complete_nil {db : DB} {r : RangeRead} : + RangeRead.Compatible db r { keys := [], more := false } ↔ + RangeRead.completeKeys db r = [] := by + unfold RangeRead.Compatible RangeRead.withinLimit + constructor + · rintro ⟨-, h | h⟩ + · exact absurd h.1 (by simp) + · exact h.2.symm + · intro h + exact ⟨by cases r.limit <;> simp, Or.inr ⟨rfl, h.symm⟩⟩ + +/-- **The empty result, any traversal.** At begin offset one and end offset at most +one, compatibility with the empty result is equivalent to the canonical keys +numbering at most `(1 - end_.offset).toNat`, for both traversal orders: reversal +only reverses the complete keys, which are empty on the same databases. -/ +theorem compatible_empty_iff_length_le_any {db : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + ((canonicalRange r).keys db).length ≤ (1 - r.end_.offset).toNat := by + cases hrev : r.reverse with + | false => exact compatible_empty_iff_length_le hrev hbegin hend + | true => + have hcomm : RangeRead.completeKeys db r = + (RangeRead.completeKeys db {r with reverse := false}).reverse := by + simp [RangeRead.completeKeys, hrev] + rw [compatible_empty_iff_complete_nil, hcomm, List.reverse_eq_nil_iff, + ← compatible_empty_iff_complete_nil] + exact compatible_empty_iff_length_le (r := {r with reverse := false}) rfl hbegin hend + +/-- **The empty result at end offsets at most one, general positive begin offset.** +A non-reversed read with `begin.offset ≥ 1` and `end_.offset ≤ 1` is compatible with +the empty complete result exactly when the canonical keys, after dropping the +`(begin.offset - 1).toNat` keys the begin selector overshoots, number at most +`(1 - end_.offset).toNat`. This is the off-one analogue of +`compatible_empty_iff_length_le`. -/ +theorem compatible_empty_iff_length_sub_le {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + ((canonicalRange r).keys db).length - (r.begin.offset - 1).toNat ≤ + (1 - r.end_.offset).toNat := by + rw [compatible_empty_iff_complete_nil, + completeKeys_eq_canonical_slice (db := db) hrev hbegin hend, + List.take_eq_nil_iff, List.drop_eq_nil_iff] + constructor + · rintro (h | h) <;> omega + · intro h; left; omega + +/-- **The empty result at end offsets at most one, general positive begin offset, +any traversal.** Reversal only reverses the complete keys, which are empty on the +same databases, so the forward bound equivalence transfers. -/ +theorem compatible_empty_iff_length_sub_le_any {db : DB} {r : RangeRead} + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : + RangeRead.Compatible db r { keys := [], more := false } ↔ + ((canonicalRange r).keys db).length - (r.begin.offset - 1).toNat ≤ + (1 - r.end_.offset).toNat := by + cases hrev : r.reverse with + | false => exact compatible_empty_iff_length_sub_le hrev hbegin hend + | true => + have hcomm : RangeRead.completeKeys db r = + (RangeRead.completeKeys db {r with reverse := false}).reverse := by + simp [RangeRead.completeKeys, hrev] + rw [compatible_empty_iff_complete_nil, hcomm, List.reverse_eq_nil_iff, + ← compatible_empty_iff_complete_nil] + exact compatible_empty_iff_length_sub_le (r := {r with reverse := false}) rfl hbegin hend + +/-- **Soundness of the canonical range at end offsets at most one, general positive +begin offset.** A non-reversed read with `begin.offset ≥ 1` and `end_.offset ≤ 1` +has complete keys equal to a left-and-right truncation of the canonical keys +(`completeKeys_eq_canonical_slice`), fixed by the two offsets, so databases agreeing +on `[begin.key, end.key)` have identical complete keys. -/ +theorem sound_canonicalRange_of_end_offset_le_one_general {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : + Sound r res (canonicalRange r) := by + intro db₁ db₂ hagree + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_canonical_slice (db := db₁) hrev hbegin hend, + completeKeys_eq_canonical_slice (db := db₂) hrev hbegin hend, + show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree] + exact RangeRead.compatible_congr hck + +/-- **Soundness of the canonical range at end offsets at most one, reversed, general +positive begin offset.** A reversed read with `begin.offset ≥ 1` and `end_.offset ≤ 1` +has complete keys equal to a fixed-length reversed truncation of the canonical keys +(`completeKeys_eq_canonical_slice_rev`), fixed by the two offsets, so databases agreeing +on `[begin.key, end.key)` have identical complete keys. -/ +theorem sound_canonicalRange_of_end_offset_le_one_rev {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : + Sound r res (canonicalRange r) := by + intro db₁ db₂ hagree + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_canonical_slice_rev (db := db₁) hrev hbegin hend, + completeKeys_eq_canonical_slice_rev (db := db₂) hrev hbegin hend, + show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree] + exact RangeRead.compatible_congr hck + +/-- If every stored key lies in the canonical range, the canonical keys are all the +stored keys. -/ +theorem canonicalRange_keys_eq_self_of_forall {db : DB} {r : RangeRead} + (h : ∀ x ∈ db.keys, x ∈ canonicalRange r) : + (canonicalRange r).keys db = db.keys := by + unfold canonicalRange ConflictRange.keys + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact h x hx + +/-- **The forward case.** At begin offset one, a complete nonempty non-reversed +result whose end offset is at most one has the canonical range as its minimal +sound conflict range. Soundness is `sound_canonicalRange_of_end_offset_le_one`; +necessity splits at the reported last, using `necessary_of_mem_result`, +`necessary_begin_key` with convexity below it, and `necessary_last_to_end_key` +above it. -/ +private theorem minimal_canonicalRange_of_end_offset_le_one_forward {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) : + Minimal r res (canonicalRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hres₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_canonicalRange_of_end_offset_le_one hrev hbegin hend + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + by_cases hkle : k ≤ res.keys.getLast hne + · -- At or below the reported last: the begin key's necessity and convexity. + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil (db := db₀) hrev + (by rw [← hres₀]; exact hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + · -- Above the reported last: the last-to-end rule applies. + exact necessary_last_to_end_key (r := r) (res := res) ⟨db₀, hcomp⟩ hrev hmore hne hend + cr hs (le_of_lt (lt_of_not_ge hkle)) hk.2 + +/-- **The forward case at a general positive begin offset.** A non-reversed read with +`begin.offset ≥ 1`, a complete nonempty result, and `end_.offset ≤ 1` has the canonical +range `[begin.key, end.key)` as its minimal sound conflict range. Soundness is the general +`sound_canonicalRange_of_end_offset_le_one_general`; necessity is the same split as the +offset-one case — the reported last is necessary, the begin key is necessary with convexity +up to the last, and `necessary_last_to_end_key` covers the keys above it. -/ +private theorem minimal_canonicalRange_of_end_offset_le_one_forward_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) (hend : r.end_.offset ≤ 1) : + Minimal r res (canonicalRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hres₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_canonicalRange_of_end_offset_le_one_general hrev hbegin hend + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + by_cases hkle : k ≤ res.keys.getLast hne + · -- At or below the reported last: the begin key's necessity and convexity. + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil (db := db₀) hrev + (by rw [← hres₀]; exact hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + · -- Above the reported last: the last-to-end rule applies. + exact necessary_last_to_end_key (r := r) (res := res) ⟨db₀, hcomp⟩ hrev hmore hne hend + cr hs (le_of_lt (lt_of_not_ge hkle)) hk.2 + +/-- **At end offsets at most one, a general positive begin offset: the canonical +range is minimal.** A non-reversed complete nonempty realizable read with +`begin.offset ≥ 1` and `end_.offset ≤ 1` has `[begin.key, end.key)` as its minimal +sound conflict range. Soundness is the general `sound_canonicalRange_of_end_offset_le_one_general`; +necessity is the forward split, and the reversed case flips via `minimal_flip`. -/ +theorem minimal_canonicalRange_of_end_offset_le_one_general {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) : + Minimal r res (canonicalRange r) := by + cases hrev : r.reverse with + | false => + exact minimal_canonicalRange_of_end_offset_le_one_forward_general + hread hmore hrev hne hbegin hend + | true => + obtain ⟨db₀, hcomp⟩ := hread + have hflip : Readable {r with reverse := false} {keys := res.keys.reverse, more := false} := + ⟨db₀, (compatible_flip (db := db₀) (b := true) hrev hmore).mpr hcomp⟩ + exact (minimal_flip (b := true) hrev hmore).mp + (minimal_canonicalRange_of_end_offset_le_one_forward_general + (r := {r with reverse := false}) (res := {keys := res.keys.reverse, more := false}) + hflip (by simp) (by simp) (by simpa using hne) hbegin hend) + +/-- At end offsets at most one and a general positive begin offset, the canonical range +`[begin.key, end.key)` is *the* minimal sound conflict range up to set equality: it is +minimal by `minimal_canonicalRange_of_end_offset_le_one_general`, and every minimal range +denotes the same set by the unconditional `Minimal.toSet_eq`. -/ +theorem unique_minimal_canonicalRange_of_end_offset_le_one_general {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) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend) + +/-- **The endpoint bridge in a certified region.** For a complete nonempty realizable read +with `begin.offset ≥ 1` and `end_.offset ≤ 1`, the necessary set is exactly the canonical +range's keys `[begin.key, end.key)`. This instantiates `necessarySet_eq_of_minimal` with +`minimal_canonicalRange_of_end_offset_le_one_general`: the lower endpoint is the attained +key `begin.key` and the upper endpoint the key `end_.key`, and the interval between them is +exactly the necessary set. It is the headline's set-theoretic content in this region, and +the shape the endpoint-attainment route produces whenever both ends are attained. -/ +theorem necessarySet_eq_canonicalRange_of_end_offset_le_one_general {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) : + necessarySet r res = (canonicalRange r).toSet := + necessarySet_eq_of_minimal + (minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend) + +/-- Erasing a stored key of the canonical range changes its canonical keys. -/ +private theorem canonical_keys_erase_ne {r : RangeRead} {db : DB} {k : Key} + (hk : k ∈ canonicalRange r) (hdb : k ∈ db.keys) : + (canonicalRange r).keys (db.erase k) ≠ (canonicalRange r).keys db := by + intro heq + have hmem : k ∈ (canonicalRange r).keys db := ConflictRange.mem_keys_iff.mpr ⟨hdb, hk⟩ + have hnot : k ∉ (canonicalRange r).keys (db.erase k) := by + rw [ConflictRange.mem_keys_iff] + exact fun h => DB.not_mem_erase_self db k h.1 + rw [heq] at hnot + exact hnot hmem + +/-- Inserting an unstored key of the canonical range changes its canonical keys. -/ +private theorem canonical_keys_insert_ne {r : RangeRead} {db : DB} {k : Key} + (hk : k ∈ canonicalRange r) (hdb : k ∉ db.keys) : + (canonicalRange r).keys (db.insert k hdb) ≠ (canonicalRange r).keys db := by + intro heq + have hmem : k ∈ (canonicalRange r).keys (db.insert k hdb) := + ConflictRange.mem_keys_iff.mpr ⟨DB.mem_insert_self db k hdb, hk⟩ + have hnot : k ∉ (canonicalRange r).keys db := by + rw [ConflictRange.mem_keys_iff]; exact fun h => hdb h.1 + rw [heq] at hmem + exact hnot hmem + +/-- Differing canonical keys at offset one force differing complete keys. -/ +private theorem completeKeys_ne_of_canonical_ne {db₁ db₂ : DB} {r : RangeRead} + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) + (hne : (canonicalRange r).keys db₁ ≠ (canonicalRange r).keys db₂) : + RangeRead.completeKeys db₁ r ≠ RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_canonical hbegin hend, completeKeys_eq_canonical hbegin hend] + cases hrev : r.reverse with + | false => simpa only [hrev, Bool.false_eq_true, ite_false] using hne + | true => simpa only [hrev, ite_true] using fun h => hne (List.reverse_inj.mp h) + +/-- For a complete result and selectors at offset one, the minimal sound conflict +range is the canonical range `[begin.key, end.key)`: the resolved window +`[resolve begin, resolve end)` cuts a key exactly when the canonical interval +does. Soundness follows since compatibility depends only on the canonical keys; +necessity follows since toggling a canonical key changes the complete keys and so +breaks a complete result. -/ +theorem minimal_canonicalRange_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + Minimal r res (canonicalRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · -- Agreement on the canonical range gives equal complete keys. + intro db₁ db₂ hagree + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + rw [completeKeys_eq_canonical hbegin hend, completeKeys_eq_canonical hbegin hend, + show (canonicalRange r).keys db₁ = (canonicalRange r).keys db₂ from hagree] + exact RangeRead.compatible_congr hck + · intro k hk cr hs + by_contra hcr + by_cases hmem : k ∈ db₀.keys + · -- Erasing the stored key preserves agreement on `cr` but changes complete keys. + have hagree : AgreeOn cr db₀ (db₀.erase k) := + (ConflictRange.keys_erase_of_not_mem cr db₀ hcr).symm + have hcompat' : RangeRead.Compatible (db₀.erase k) r res := (hs db₀ _ hagree).mp hcomp + have hres' : res.keys = RangeRead.completeKeys (db₀.erase k) r := by + rcases hcompat'.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + exact completeKeys_ne_of_canonical_ne hbegin hend + (canonical_keys_erase_ne hk hmem) (by rw [← hres, ← hres']) + · -- Inserting the unstored key likewise. + have hagree : AgreeOn cr db₀ (db₀.insert k hmem) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hmem hcr).symm + have hcompat' : RangeRead.Compatible (db₀.insert k hmem) r res := + (hs db₀ _ hagree).mp hcomp + have hres' : res.keys = RangeRead.completeKeys (db₀.insert k hmem) r := by + rcases hcompat'.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + exact completeKeys_ne_of_canonical_ne hbegin hend + (canonical_keys_insert_ne hk hmem) (by rw [← hres, ← hres']) + +/-- At offset one with a complete result, the canonical range is *the* minimal +sound conflict range up to set equality: it is minimal by +`minimal_canonicalRange_of_offset_one`, and every minimal range denotes the same +set by the unconditional `Minimal.toSet_eq`. -/ +theorem unique_minimal_canonicalRange_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_canonicalRange_of_offset_one hread hmore hbegin hend) + +/-- At begin offset one, a complete nonempty result whose end offset is at most +one has the canonical range `[begin.key, end.key)` as its minimal sound conflict +range. At end offset one this is `minimal_canonicalRange_of_offset_one`. At end +offset at most zero the resolved end targets a stored key `1 - offset` positions +below the last stored key strictly below `end_.key`, so the complete keys are the +canonical keys with the trailing `(1 - end_.offset).toNat` keys removed — a +truncation to a length depending only on `end_.offset`, hence database-independent. +Those trailing keys are outside the resolved window and so are not directly +reported, but they are necessary all the same: the reported keys are necessary by +`necessary_of_mem_result`, the beginning by `necessary_begin_key`, and the rest by +convexity up to the last stored key below `end_.key`, above which +`necessary_last_to_end_key` applies. Soundness survives because the complete keys +are that fixed-length truncation of the canonical keys. `canonicalRange` ignores +`reverse`, so the reversed case flips via `minimal_flip`. -/ +theorem minimal_canonicalRange_of_end_offset_le_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) : + Minimal r res (canonicalRange r) := by + cases hrev : r.reverse with + | false => + by_cases hend1 : r.end_.offset = 1 + · exact minimal_canonicalRange_of_offset_one hread hmore hbegin hend1 + · exact minimal_canonicalRange_of_end_offset_le_one_forward hread hmore hrev hne hbegin hend + | true => + obtain ⟨db₀, hcomp⟩ := hread + have hflip : Readable {r with reverse := false} {keys := res.keys.reverse, more := false} := + ⟨db₀, (compatible_flip (db := db₀) (b := true) hrev hmore).mpr hcomp⟩ + exact (minimal_flip (b := true) hrev hmore).mp + (minimal_canonicalRange_of_end_offset_le_one_forward + (r := {r with reverse := false}) (res := {keys := res.keys.reverse, more := false}) + hflip (by simp) (by simp) (by simpa using hne) hbegin hend) + +/-- The minimal canonical range at end offsets at most one is unique up to set +equality. -/ +theorem unique_minimal_canonicalRange_of_end_offset_le_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 1) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_canonicalRange_of_end_offset_le_one hread hmore hne hbegin hend) + +/-! +## The canonical range at offset one, truncated + +When the result is truncated (`more = true`), a compatible result only reports a +prefix of the complete keys, so the read gives no information past the last +reported key: the minimal sound range runs from `begin.key` up to just past that +key, `keyAfter (res.keys.getLast hne)`. The reported keys themselves stay +necessary (`necessary_of_mem_result`), the begin key stays necessary +(`necessary_begin_key`), and keys between them are necessary by convexity; no key +at or beyond `keyAfter (res.keys.getLast hne)` is necessary, because agreement on +the truncated range fixes exactly the reported prefix and the limit — enough for +compatibility whether or not `more` remains. +-/ + +/-- Filtering an ascending list at or below a key yields a prefix. -/ +private theorem filter_le_prefix {α : Type*} [LinearOrder α] {l : List α} + (hs : l.Pairwise (· < ·)) (b : α) : + (l.filter (fun k => decide (k ≤ b))) <+: l := by + induction l with + | nil => exact List.nil_prefix + | cons a t ih => + rw [List.pairwise_cons] at hs + obtain ⟨hhead, htail⟩ := hs + by_cases ha : a ≤ b + · rw [List.filter_cons_of_pos (by simpa using ha), List.cons_prefix_cons] + exact ⟨rfl, ih htail⟩ + · have hb : b < a := lt_of_not_ge ha + rw [List.filter_cons_of_neg (by simpa using ha)] + have hnil : t.filter (fun k => decide (k ≤ b)) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk hkb + exact absurd (lt_of_lt_of_le hb (le_of_lt (hhead k hk))) + (not_lt.mpr (of_decide_eq_true hkb)) + rw [hnil] + exact List.nil_prefix + +/-- For a prefix whose last element bounds everything after it, filtering the +whole ascending list at or below that last element recovers the prefix. -/ +theorem filter_le_getLast_eq_of_prefix {α : Type*} [LinearOrder α] + {g W : List α} (hW : W.Pairwise (· < ·)) (hpre : g <+: W) (hne : g ≠ []) : + W.filter (fun k => decide (k ≤ g.getLast hne)) = g := by + obtain ⟨rest, rfl⟩ := hpre + have hpw : (g ++ rest).Pairwise (· < ·) := hW + rw [List.pairwise_append] at hpw + obtain ⟨hgasc, _, hcross⟩ := hpw + rw [List.filter_append] + have hg : g.filter (fun k => decide (k ≤ g.getLast hne)) = g := by + rw [List.filter_eq_self] + intro k hk + rw [decide_eq_true_eq] + exact (hgasc.imp le_of_lt).rel_getLast hk + have hr : rest.filter (fun k => decide (k ≤ g.getLast hne)) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk hkb + exact absurd (hcross (g.getLast hne) (List.getLast_mem hne) k hk) + (not_lt.mpr (of_decide_eq_true hkb)) + rw [hg, hr, List.append_nil] + +/-- Conversely, an ascending list whose filter at or below `g`'s last element +equals `g` has `g` as a prefix, so that filter determines `g`. -/ +private theorem prefix_of_filter_le_getLast_eq {α : Type*} [LinearOrder α] + {g W : List α} (hW : W.Pairwise (· < ·)) (hne : g ≠ []) + (heq : W.filter (fun k => decide (k ≤ g.getLast hne)) = g) : + g <+: W := by + rw [← heq] + exact filter_le_prefix hW (g.getLast hne) + +/-- The truncated conflict range: the begin key unchanged, ending just past the +last reported key. -/ +def truncatedRange (r : RangeRead) (last : Key) : ConflictRange := + ⟨r.begin.key, (keyAfter last : WithTop Key)⟩ + +/-- At begin offset one, the truncated range's stored keys are exactly the complete +keys at or below the last reported key, whenever the truncated range's successor +end lies at or below the resolved end. This is the offset-free form of +`truncatedRange_keys_eq`: only the begin offset is needed to identify `resolve +begin` with the first stored key at or above `begin.key`. -/ +private theorem truncatedRange_keys_eq_of_resolve {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hle : (keyAfter last : WithTop Key) ≤ resolve db r.end_) : + (truncatedRange r last).keys db = + (RangeRead.completeKeys db r).filter (fun (k : Key) => decide (k ≤ last)) := by + unfold truncatedRange ConflictRange.keys + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + unfold RangeRead.inRange + rw [List.filter_filter] + apply List.filter_congr + intro k hk + rw [← Bool.decide_and, decide_eq_decide] + constructor + · rintro ⟨hb, hlt⟩ + have hkle := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hlt) + exact ⟨hkle, (key_le_resolve_iff_of_offset_one hbegin hk).mp hb, + lt_of_lt_of_le hlt hle⟩ + · rintro ⟨hkle, hb, _⟩ + exact ⟨(key_le_resolve_iff_of_offset_one hbegin hk).mpr hb, + WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle)⟩ + +/-- At offset one and non-reversed, the truncated range's stored keys are exactly +the canonical keys at or below the last reported key. This is the offset-one +instance of `truncatedRange_keys_eq_of_resolve`: the end offset one puts +`end_.key` at or below `resolve end_`, so the successor bound transfers. -/ +private theorem truncatedRange_keys_eq {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) + (hle : (keyAfter last : WithTop Key) ≤ r.end_.key) : + (truncatedRange r last).keys db = + (RangeRead.completeKeys db r).filter (fun (k : Key) => decide (k ≤ last)) := + truncatedRange_keys_eq_of_resolve hrev hbegin + (le_trans hle (key_le_resolve_of_one_le (by omega))) + +/-- For a truncated non-reversed offset-one read, compatibility is equivalent to +fitting the limit and having exactly the truncated range's stored keys: agreement +on the range fixes the reported prefix, and nothing past the last reported key +can be inferred. -/ +theorem compatible_iff_truncated_keys {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) : + RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (RangeRead.completeKeys db r).filter + (fun (k : Key) => decide (k ≤ res.keys.getLast hne)) = res.keys := by + have hW : (RangeRead.completeKeys db r).Pairwise (· < ·) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + exact RangeRead.inRange_pairwise db _ _ + constructor + · rintro ⟨hlim, hdisj⟩ + rcases hdisj with ⟨_, hpre, _⟩ | ⟨hm, _⟩ + · exact ⟨hlim, filter_le_getLast_eq_of_prefix hW hpre hne⟩ + · simp [hmore] at hm + · rintro ⟨hlim, heq⟩ + exact ⟨hlim, Or.inl ⟨hmore, prefix_of_filter_le_getLast_eq hW hne heq, hne⟩⟩ + +/-- At offset one with a truncated non-reversed result, the minimal sound conflict +range is `[begin.key, keyAfter (last reported key))`. The reported keys and the +begin key are necessary, convexity fills the gap, and no key at or past the +successor of the last reported key is necessary because the truncated range +already pins down the reported prefix. A `more = true` result is nonempty, so the +`getLastD` fallback is never reached. -/ +theorem minimal_truncatedRange_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + hpre₀.subset (List.getLast_mem hne) + have hlast_stored : res.keys.getLast hne ∈ db₀.keys := + (RangeRead.mem_completeKeys.mp hlast_ck).1 + have hlast_lt_res : (res.keys.getLast hne : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + -- The last reported key lies strictly below `end_.key`, so its successor does too. + have hlast_end : res.keys.getLast hne < r.end_.key := by + by_contra hcon + have hge : r.end_.key ≤ (↑(res.keys.getLast hne) : WithTop Key) := not_lt.mp hcon + have hrle : resolve db₀ r.end_ ≤ (↑(res.keys.getLast hne) : WithTop Key) := + (key_le_resolve_iff_of_offset_one hend hlast_stored).mp hge + exact absurd hlast_lt_res (not_lt.mpr hrle) + have hka_le : (keyAfter (res.keys.getLast hne) : WithTop Key) ≤ r.end_.key := + keyAfter_le_of_lt hlast_end + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hf₁ := truncatedRange_keys_eq (db := db₁) hrev hbegin hend hka_le + have hf₂ := truncatedRange_keys_eq (db := db₂) hrev hbegin hend hka_le + rw [compatible_iff_truncated_keys hrev hmore hne, + compatible_iff_truncated_keys (db := db₂) hrev hmore hne, ← hf₁, ← hf₂, hagree] + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + -- A nonempty result forces the begin key to be a genuine key, not `⊤`. + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- **The canonical range at a nonpositive end offset, truncated, general begin +offset.** A truncated non-reversed realizable read with `1 ≤ begin.offset` and +`end_.offset ≤ 0` has the canonical range `[begin.key, end.key)` as its minimal sound +conflict range. Putting `b = (begin.offset - 1).toNat` and `m = (1 - end_.offset).toNat`, +the complete keys are `(C.drop b).take (|C| - b - m)` where `C` is the canonical keys +(`completeKeys_eq_canonical_slice`); soundness is +`sound_canonicalRange_of_end_offset_le_one_general`. Necessity below the reported last is +the begin key's necessity and convexity. For a key `k` strictly above the reported last +we build a witness whose canonical keys are `C.take b ++ res.keys ++ F`, `F` a filler of +exactly `m` canonical keys above the reported last: dropping the first `b` and the last +`m` leaves exactly the reported keys (compatible), while erasing one filler drops that +below the reported prefix. The `m` filler keys exist because the reported last sits among +the complete keys, `m` above it. At `b = 0` this is the offset-one statement. -/ +theorem minimal_canonicalRange_of_truncated_end_offset_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 0) : + Minimal r res (canonicalRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + set b : ℕ := (r.begin.offset - 1).toNat with hb + set m : ℕ := (1 - r.end_.offset).toNat with hm + have hm1 : 1 ≤ m := by rw [hm]; omega + have hsub : (1 - r.end_.offset).toNat = m := hm + have hbsub : (r.begin.offset - 1).toNat = b := hb + -- Every reported key lies in the canonical range. + have hres_canon : ∀ x ∈ res.keys, x ∈ canonicalRange r := by + intro x hx + have hx' : x ∈ RangeRead.completeKeys db₀ r := hpre₀.subset hx + rw [completeKeys_eq_canonical_slice (db := db₀) (r := r) hrev hbegin (by omega)] at hx' + exact (ConflictRange.mem_keys_iff.mp + (List.mem_of_mem_drop (List.mem_of_mem_take hx'))).2 + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_canonicalRange_of_end_offset_le_one_general hrev hbegin (by omega), ?_⟩ + intro k hk + by_cases hkle : k ≤ res.keys.getLast hne + · -- At or below the reported last: convex between the begin key and the last. + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + exact hbegin_nec.of_between hlast_nec hvk hkle + · -- Strictly above the reported last (and below `end_.key`). + have hkgt : res.keys.getLast hne < k := lt_of_not_ge hkle + have hknotmem : k ∉ res.keys := fun hm' => + absurd ((hsort.pairwise.imp le_of_lt).rel_getLast hm') (not_le.mpr hkgt) + set C : List Key := (canonicalRange r).keys db₀ with hC + have hCsort : C.SortedLT := + List.sortedLT_iff_pairwise.mpr (by rw [hC]; exact ConflictRange.keys_pairwise _ _) + have hCcanon : ∀ x ∈ C, x ∈ canonicalRange r := fun x hx => + (ConflictRange.mem_keys_iff.mp (by rw [hC] at hx; exact hx)).2 + have hck_eq : RangeRead.completeKeys db₀ r = (C.drop b).take (C.length - b - m) := by + rw [completeKeys_eq_canonical_slice (db := db₀) (r := r) hrev hbegin (by omega), + hbsub, hsub, hC] + have hlastmem : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + hpre₀.subset (List.getLast_mem hne) + rw [hck_eq] at hlastmem + obtain ⟨j', hj', hjk'⟩ := List.mem_take_iff_getElem.mp hlastmem + have hj'lt : j' < C.length - b - m := lt_of_lt_of_le hj' (Nat.min_le_left _ _) + set j : ℕ := b + j' with hj + have hjC : j < C.length := by rw [hj]; omega + have hjk : C[j] = res.keys.getLast hne := by + change C[b + j'] = res.keys.getLast hne + rw [← List.getElem_drop (i := b) (j := j')] + exact hjk' + -- The reported keys are a prefix of the complete keys, so their head is `C[b]`. + have hbC : b < C.length := by omega + have hhead_res : res.keys.head hne = C[b] := by + have hpre_b : res.keys <+: (C.drop b).take (C.length - b - m) := hck_eq ▸ hpre₀ + have hne_take : (C.drop b).take (C.length - b - m) ≠ [] := hpre_b.ne_nil hne + rw [hpre_b.head hne, List.head_eq_getElem hne_take] + simpa only [List.getElem_take, Nat.add_zero] using + (List.getElem_drop (i := b) (j := 0) (h := by rw [List.length_drop]; omega)) + -- The `m` filler keys above the reported last, all below `end_.key`. + have hGlen : m ≤ (C.drop (j + 1)).length := by rw [List.length_drop]; omega + have hGsort : (C.drop (j + 1)).SortedLT := by + rw [List.sortedLT_iff_pairwise] + exact List.Pairwise.drop (i := j + 1) (List.sortedLT_iff_pairwise.mp hCsort) + have hGgt : ∀ x ∈ C.drop (j + 1), res.keys.getLast hne < x := by + intro x hx + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hx + rw [List.length_drop] at hi + have hxeq : x = C[j + 1 + i] := by rw [← hix, List.getElem_drop] + rw [hxeq, ← hjk] + exact hCsort.getElem_lt_getElem_iff.mpr (by omega) + have hGerase_sub : (C.drop (j + 1)).erase k ⊆ C := fun x hx => + List.drop_subset (j + 1) C (List.erase_subset (a := k) (l := C.drop (j + 1)) hx) + have hGerase_gt : ∀ x ∈ (C.drop (j + 1)).erase k, res.keys.getLast hne < x := + fun x hx => hGgt x ((List.erase_subset (a := k) (l := C.drop (j + 1))) hx) + have hcons_nodup : (k :: (C.drop (j + 1)).erase k).Nodup := + List.nodup_cons.mpr ⟨hGsort.nodup.not_mem_erase, + hGsort.nodup.sublist (List.erase_sublist (a := k))⟩ + have hcons_len : m ≤ (k :: (C.drop (j + 1)).erase k).length := by + rw [List.length_cons] + by_cases hkG : k ∈ C.drop (j + 1) + · rw [List.length_erase_of_mem hkG]; omega + · rw [List.erase_of_not_mem hkG]; omega + -- assemble the filler: `k` together with enough of `k`'s canonical successors. + let raw : List Key := (k :: (C.drop (j + 1)).erase k).take m + have hrawlen : raw.length = m := by + rw [List.length_take, Nat.min_eq_left hcons_len] + have hraw_sub : ∀ x ∈ raw, x = k ∨ x ∈ (C.drop (j + 1)).erase k := by + intro x hx + rcases List.mem_cons.mp (List.mem_of_mem_take hx) with h | h + · exact Or.inl h + · exact Or.inr h + have hrawk : k ∈ raw := by + have hm1' : m = (m - 1) + 1 := by omega + change k ∈ List.take m (k :: (C.drop (j + 1)).erase k) + rw [hm1', List.take_succ_cons] + exact List.mem_cons_self .. + have hrawCanon : ∀ x ∈ raw, x ∈ canonicalRange r := by + intro x hx + rcases hraw_sub x hx with rfl | hx + · exact hk + · exact hCcanon x (hGerase_sub hx) + have hrawGt : ∀ x ∈ raw, res.keys.getLast hne < x := by + intro x hx + rcases hraw_sub x hx with rfl | hx + · exact hkgt + · exact hGerase_gt x hx + let F : List Key := raw.insertionSort (· ≤ ·) + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp (hcons_nodup.sublist (List.take_sublist _ _))) + have hFlen : F.length = m := by rw [List.length_insertionSort, hrawlen] + have hFk : k ∈ F := hFperm.mem_iff.mp hrawk + have hFmem : ∀ x ∈ F, x ∈ canonicalRange r ∧ res.keys.getLast hne < x := + fun x hx => let hx' := hFperm.mem_iff.mpr hx + ⟨hrawCanon x hx', hrawGt x hx'⟩ + have hFF : (res.keys ++ F).SortedLT := by + refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩ + intro a ha b hb + exact lt_of_le_of_lt ((hsort.pairwise.imp le_of_lt).rel_getLast ha) (hFmem b hb).2 + -- The witness's canonical list: the `b` canonical keys below the reported prefix, + -- then the reported keys and the filler. + set D : List Key := C.take b ++ (res.keys ++ F) with hD + have hCb_sort : (C.take b).SortedLT := + List.sortedLT_iff_pairwise.mpr ((List.sortedLT_iff_pairwise.mp hCsort).sublist + (List.take_prefix b C).sublist) + have hCb_lt : ∀ x ∈ C.take b, x < res.keys.head hne := by + intro x hx + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hx + have hib : i < b := by rw [List.length_take] at hi; omega + rw [← hix, hhead_res, List.getElem_take] + exact hCsort.getElem_lt_getElem_iff.mpr hib + have hhead_le : ∀ x ∈ res.keys, res.keys.head hne ≤ x := by + intro x hx + obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hx + rw [← hix, List.head_eq_getElem hne] + exact hsort.getElem_le_getElem_iff.mpr (Nat.zero_le i) + have hheadF_le : ∀ c ∈ F, res.keys.head hne ≤ c := fun c hc => + le_of_lt (lt_of_le_of_lt (hhead_le _ (List.getLast_mem hne)) (hFmem c hc).2) + have hDP : D.SortedLT := by + refine List.sortedLT_append.mpr ⟨hCb_sort, hFF, ?_⟩ + intro a ha c hc + exact lt_of_lt_of_le (hCb_lt a ha) (by + rcases List.mem_append.mp hc with hc | hc + · exact hhead_le c hc + · exact hheadF_le c hc) + have hDcanon : ∀ x ∈ D, x ∈ canonicalRange r := fun x hx => by + rw [hD, List.mem_append, List.mem_append] at hx + rcases hx with hx | hx | hx + · exact hCcanon x (List.take_subset _ _ hx) + · exact hres_canon x hx + · exact (hFmem x hx).1 + let dbw : DB := ⟨D, hDP⟩ + have hlenP : (C.take b).length = b := by + rw [List.length_take, Nat.min_eq_left (by omega)] + have hlenD : D.length = b + (res.keys.length + m) := by + rw [hD, List.length_append, List.length_append, hlenP, hFlen] + have hdropD : D.drop b = res.keys ++ F := by + rw [hD, List.drop_left' hlenP] + have hcanw : (canonicalRange r).keys dbw = D := + canonicalRange_keys_eq_self_of_forall (db := dbw) (r := r) hDcanon + have hlen : D.length - b - m = res.keys.length := by rw [hlenD]; omega + have hckw : RangeRead.completeKeys dbw r = res.keys := by + rw [completeKeys_eq_canonical_slice (db := dbw) (r := r) hrev hbegin (by omega), + hcanw, hbsub, hsub, hlen, hdropD, List.take_left] + have hcompw : RangeRead.Compatible dbw r res := + ⟨hcomp.1, Or.inl ⟨hmore, by rw [hckw], hne⟩⟩ + -- Erasing `k` removes one filler key, dropping the complete keys below the prefix. + have hkCb : k ∉ C.take b := fun hx => absurd (hCb_lt k hx) (not_lt.mpr + (le_trans (hhead_le _ (List.getLast_mem hne)) (le_of_lt hkgt))) + have hkeys_erase : (dbw.erase k).keys = D.erase k := DB.erase_keys dbw k + have hcan_erase : (canonicalRange r).keys (dbw.erase k) = D.erase k := by + rw [← hkeys_erase] + exact canonicalRange_keys_eq_self_of_forall (db := dbw.erase k) (r := r) + (fun x hx => hDcanon x (List.erase_subset hx)) + have hD_erase : D.erase k = C.take b ++ (res.keys ++ F.erase k) := by + rw [hD, List.erase_append_right _ hkCb, List.erase_append_right _ hknotmem] + have hFlener : (F.erase k).length = m - 1 := by + have := List.length_erase_of_mem hFk; omega + have hdropD_erase : (D.erase k).drop b = res.keys ++ F.erase k := by + rw [hD_erase, List.drop_left' hlenP] + have hlenD_erase : (D.erase k).length = b + (res.keys.length + (m - 1)) := by + rw [hD_erase, List.length_append, List.length_append, hlenP, hFlener] + have hck_erase : RangeRead.completeKeys (dbw.erase k) r = + res.keys.take (res.keys.length - 1) := by + rw [completeKeys_eq_canonical_slice (db := dbw.erase k) (r := r) hrev hbegin (by omega), + hcan_erase, hbsub, hsub, hdropD_erase] + have hidx : (D.erase k).length - b - m = res.keys.length - 1 := by + rw [hlenD_erase]; omega + rw [hidx, List.take_append_of_le_length (by omega : res.keys.length - 1 ≤ res.keys.length)] + have hnot_erase : ¬ RangeRead.Compatible (dbw.erase k) r res := by + intro hc + have hlen := (RangeRead.compatible_keys_prefix hc).length_le + rw [hck_erase, List.length_take, Nat.min_eq_left (Nat.sub_le _ _)] at hlen + have hlt : res.keys.length - 1 < res.keys.length := + Nat.sub_lt (List.length_pos_of_ne_nil hne) (by norm_num) + omega + intro cr hs + by_contra hkcr + exact hnot_erase ((hs dbw (dbw.erase k) + (ConflictRange.keys_erase_of_not_mem cr dbw hkcr).symm).mp hcompw) + + +/-- At offset one with a truncated non-reversed result, the truncated range is *the* +minimal sound conflict range up to set equality: it is minimal by +`minimal_truncatedRange_of_offset_one`, and every minimal range denotes the same +set by the unconditional `Minimal.toSet_eq`. -/ +theorem unique_minimal_truncatedRange_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_truncatedRange_of_offset_one hread hmore hrev hbegin hend) + +/-! +### The reversed truncated case + +A reversed truncated result reports the top of the window downward, so the known +side is the end: the minimal sound range runs from the last reported key up to +`end.key`, dually to the non-reversed case. The descending-order filter lemmas +below mirror the ascending ones. +-/ + +/-- The descending-order twin of `filter_le_prefix`, obtained by reversing the +order: `(· > ·)` is `(· < ·)` and `b ≤ ·` is `· ≤ b` under `OrderDual`. -/ +private theorem filter_ge_prefix {α : Type*} [LinearOrder α] {l : List α} + (hs : l.Pairwise (· > ·)) (b : α) : + (l.filter (fun k => decide (b ≤ k))) <+: l := + filter_le_prefix (α := αᵒᵈ) (l := l) hs b + +/-- The descending-order twin of `filter_le_getLast_eq_of_prefix`. -/ +private theorem filter_ge_getLast_eq_of_prefix {α : Type*} [LinearOrder α] + {g W : List α} (hW : W.Pairwise (· > ·)) (hpre : g <+: W) (hne : g ≠ []) : + W.filter (fun k => decide (g.getLast hne ≤ k)) = g := + filter_le_getLast_eq_of_prefix (α := αᵒᵈ) (g := g) (W := W) hW hpre hne + +/-- The descending-order twin of `prefix_of_filter_le_getLast_eq`. -/ +private theorem prefix_of_filter_ge_getLast_eq {α : Type*} [LinearOrder α] + {g W : List α} (hW : W.Pairwise (· > ·)) (hne : g ≠ []) + (heq : W.filter (fun k => decide (g.getLast hne ≤ k)) = g) : + g <+: W := + prefix_of_filter_le_getLast_eq (α := αᵒᵈ) (g := g) (W := W) hW hne heq + +/-- For an ascending list, filtering a threshold `last ≤ ·` commutes with dropping +the largest `m` elements: the kept keys of `take (length - m)` are the kept keys with +the largest `m` removed, because the kept keys form a suffix of the list. -/ +private theorem filter_ge_take {α : Type*} [LinearOrder α] (l : List α) + (hs : l.Pairwise (· < ·)) (last : α) (m : ℕ) : + (l.take (l.length - m)).filter (fun k => decide (last ≤ k)) = + (l.filter (fun k => decide (last ≤ k))).take + ((l.filter (fun k => decide (last ≤ k))).length - m) := by + induction l with + | nil => simp + | cons a t ih => + rw [List.pairwise_cons] at hs + obtain ⟨hhead, htail⟩ := hs + by_cases ha : last ≤ a + · have hall : ∀ k ∈ a :: t, decide (last ≤ k) = true := by + intro k hk + rw [decide_eq_true_eq] + rcases List.mem_cons.mp hk with rfl | hk + · exact ha + · exact le_trans ha (le_of_lt (hhead k hk)) + have hfil : (a :: t).filter (fun k => decide (last ≤ k)) = (a :: t) := + List.filter_eq_self.mpr hall + rw [hfil] + have htake : ((a :: t).take ((a :: t).length - m)).filter + (fun k => decide (last ≤ k)) = (a :: t).take ((a :: t).length - m) := + List.filter_eq_self.mpr (fun k hk => hall k (List.mem_of_mem_take hk)) + rw [htake] + · have haf : decide (last ≤ a) = false := decide_eq_false_iff_not.mpr ha + have hfil : (a :: t).filter (fun k => decide (last ≤ k)) = + t.filter (fun k => decide (last ≤ k)) := by + rw [List.filter_cons, haf]; simp + rw [hfil] + by_cases hle : (a :: t).length - m = 0 + · have hflen : (t.filter (fun k => decide (last ≤ k))).length ≤ m := by + have h := List.length_filter_le (fun k => decide (last ≤ k)) t + rw [List.length_cons] at hle + omega + rw [hle, List.take_zero, List.filter_nil, Nat.sub_eq_zero_of_le hflen, + List.take_zero] + · have hpos : 0 < (a :: t).length - m := Nat.pos_of_ne_zero hle + rw [List.take_cons hpos, List.filter_cons, haf] + have harg : (a :: t).length - m - 1 = t.length - m := by + rw [List.length_cons]; omega + simp only [harg, Bool.false_eq_true, ite_false] + rw [ih htail] + +/-- The reversed truncated conflict range: from the last reported key to the end +key unchanged. -/ +def truncatedRangeRev (r : RangeRead) (last : Key) : ConflictRange := + ⟨(last : WithTop Key), r.end_.key⟩ + +/-- The reversed truncated range's stored keys are the canonical keys at or above +the last reported key. -/ +private theorem truncatedRangeRev_keys_eq {db : DB} {r : RangeRead} {last : Key} + (hbl : (r.begin.key : WithTop Key) ≤ (last : WithTop Key)) : + (truncatedRangeRev r last).keys db = + ((canonicalRange r).keys db).filter + (fun (k : Key) => decide (last ≤ k)) := by + unfold truncatedRangeRev canonicalRange ConflictRange.keys + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [← Bool.decide_and, decide_eq_decide] + constructor + · rintro ⟨hlk, hke⟩ + exact ⟨WithTop.coe_le_coe.mp hlk, le_trans hbl hlk, hke⟩ + · rintro ⟨hlk, _, hke⟩ + exact ⟨WithTop.coe_le_coe.mpr hlk, hke⟩ + +/-- At offset one and reversed, filtering the complete keys at or above the last +reported key gives the reversed truncated range's stored keys, reversed. -/ +private theorem truncatedRangeRev_filter_eq {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) + (hbl : (r.begin.key : WithTop Key) ≤ (last : WithTop Key)) : + (RangeRead.completeKeys db r).filter (fun (k : Key) => decide (last ≤ k)) = + ((truncatedRangeRev r last).keys db).reverse := by + rw [RangeRead.completeKeys]; simp only [hrev, ite_true] + rw [inRange_eq_canonical_keys hbegin hend, truncatedRangeRev_keys_eq hbl, + List.filter_reverse] + +/-- For a truncated reversed offset-one read, compatibility is equivalent to fitting +the limit and having exactly the reversed truncated range's stored keys: agreement +on the range fixes the reported prefix, and nothing past the last reported key can +be inferred. -/ +theorem compatible_iff_truncated_keys_rev {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ []) : + RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (RangeRead.completeKeys db r).filter + (fun (k : Key) => decide (res.keys.getLast hne ≤ k)) = res.keys := by + have hW : (RangeRead.completeKeys db r).Pairwise (· > ·) := by + have h := RangeRead.completeKeys_pairwise (db := db) (r := r) + rw [hrev] at h + exact h + constructor + · rintro ⟨hlim, hdisj⟩ + rcases hdisj with ⟨_, hpre, _⟩ | ⟨hm, _⟩ + · exact ⟨hlim, filter_ge_getLast_eq_of_prefix hW hpre hne⟩ + · simp [hmore] at hm + · rintro ⟨hlim, heq⟩ + exact ⟨hlim, Or.inl ⟨hmore, prefix_of_filter_ge_getLast_eq hW hne heq, hne⟩⟩ + +/-- At begin offset one and a nonpositive end offset, agreement on +`[last, end.key)` fixes the reversed truncated range's stored keys with the largest +`(1 - end.offset)` canonical keys removed. The complete keys are the canonical keys +with the largest `(1 - end.offset)` removed, reversed +(`completeKeys_eq_canonical_slice_rev`); filtering at or above the last reported key +then selects the kept suffix, which `filter_ge_take` turns into the truncated range's +stored keys with the largest `(1 - end.offset)` keys removed. The begin offset is one, +so no canonical key below `begin.key` is dropped and `begin.key ≤ last` makes the +result a function of `[last, end.key)`. -/ +private theorem completeKeys_filter_ge_eq {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 0) + (hbl : (r.begin.key : WithTop Key) ≤ (last : WithTop Key)) : + (RangeRead.completeKeys db r).filter (fun k => decide (last ≤ k)) = + (((truncatedRangeRev r last).keys db).take + (((truncatedRangeRev r last).keys db).length - + (1 - r.end_.offset).toNat)).reverse := by + have hslice := completeKeys_eq_canonical_slice_rev (db := db) (r := r) hrev + (by omega) (by omega) + have hb0 : (r.begin.offset - 1).toNat = 0 := by rw [hbegin]; rfl + rw [hb0] at hslice + have hslice' : RangeRead.completeKeys db r = + (((canonicalRange r).keys db).take + (((canonicalRange r).keys db).length - (1 - r.end_.offset).toNat)).reverse := by + simpa using hslice + rw [hslice', List.filter_reverse, truncatedRangeRev_keys_eq hbl] + congr 1 + exact filter_ge_take ((canonicalRange r).keys db) (ConflictRange.keys_pairwise _ db) + last (1 - r.end_.offset).toNat + +/-- A nonempty complete-keys list forces the begin selector's key to be genuine, not `⊤`, +for a reversed read. -/ +private theorem begin_key_ne_top_of_completeKeys_ne_nil_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hne : RangeRead.completeKeys db r ≠ []) + (hoff : 1 ≤ r.begin.offset) : r.begin.key ≠ (⊤ : WithTop Key) := by + intro htop + have hresolve : resolve db r.begin = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top htop hoff + have hnil : RangeRead.completeKeys db r = [] := by + rw [RangeRead.completeKeys, ite_eq_left (by simp [hrev])] + simp only [RangeRead.inRange, hresolve] + rw [List.reverse_eq_nil_iff, List.filter_eq_nil_iff] + intro x _ hx + exact absurd (of_decide_eq_true hx).1 (not_le.mpr (WithTop.coe_lt_top x)) + exact hne hnil + +/-- **Begin-key necessity for a truncated reversed result.** -/ +private theorem necessary_begin_key_rev_more {r : RangeRead} {res : Result} {v : Key} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = true) + (hlen : 0 < res.keys.length) (hbegin : 2 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) + (hkey : r.begin.key = (↑v : WithTop Key)) : + Necessary r res v := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := List.ne_nil_of_length_pos hlen + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + set C : List Key := (canonicalRange r).keys db₀ with hC + have hCsort : C.SortedLT := by + rw [hC]; exact List.sortedLT_iff_pairwise.mpr (ConflictRange.keys_pairwise _ _) + have hCcanon : ∀ x ∈ C, x ∈ canonicalRange r := fun x hx => + (ConflictRange.mem_keys_iff.mp (hC ▸ hx)).2 + set b : ℕ := (r.begin.offset - 1).toNat with hb + set m : ℕ := (1 - r.end_.offset).toNat with hm + have hbpos : 1 ≤ b := by rw [hb]; omega + set W : List Key := (C.drop b).take (C.length - b - m) with hW + have hslice : RangeRead.completeKeys db₀ r = W.reverse := by + rw [completeKeys_eq_canonical_slice_rev (db := db₀) (r := r) hrev (by omega) hend, + ← hC, ← hb, ← hm, ← hW] + have hpre₀' : res.keys <+: RangeRead.completeKeys db₀ r := hpre₀ + obtain ⟨t, ht⟩ := hpre₀' + have hWrev : W.reverse = res.keys ++ t := by rw [hslice] at ht; exact ht.symm + have hWdecomp : W = t.reverse ++ res.keys.reverse := by + have h := congrArg List.reverse hWrev + simpa [List.reverse_append] using h + have hWlen : W.length = C.length - b - m := by + rw [hW, List.length_take, List.length_drop] + exact Nat.min_eq_left (Nat.sub_le _ _) + have hres_le_W : res.keys.length ≤ W.length := by + have h := hpre₀.length_le + rwa [hslice, List.length_reverse] at h + have hClen : b + m + res.keys.length ≤ C.length := by + rw [hWlen] at hres_le_W; omega + have hdrop : (C.drop b).drop (C.length - b - m) = C.drop (C.length - m) := by + rw [List.drop_drop]; congr 1; omega + have hsplit : W ++ C.drop (C.length - m) = C.drop b := by + rw [hW]; rw [← hdrop]; exact List.take_append_drop _ _ + set G : List Key := C.drop (C.length - m) with hG + have hGlen : G.length = m := by rw [hG, List.length_drop]; omega + have hGs : G.SortedLT := by + rw [hG, List.sortedLT_iff_pairwise] + exact List.Pairwise.drop (List.sortedLT_iff_pairwise.mp hCsort) + have hGcanon : ∀ x ∈ G, x ∈ canonicalRange r := + fun x hx => hCcanon x (List.drop_subset _ _ hx) + have hCdecomp : C = (C.take b ++ W) ++ G := by + calc C = C.take b ++ C.drop b := (List.take_append_drop b C).symm + _ = C.take b ++ (W ++ G) := by rw [hsplit] + _ = (C.take b ++ W) ++ G := (List.append_assoc _ _ _).symm + have hlastW : res.keys.getLast hne ∈ W := by + rw [hWdecomp] + exact List.mem_append_right _ (List.mem_reverse.mpr (List.getLast_mem hne)) + have hWG : ∀ x ∈ W, ∀ y ∈ G, x < y := by + have hA : ((C.take b ++ W) ++ G).SortedLT := hCdecomp ▸ hCsort + have hcross := (List.sortedLT_append.mp hA).2.2 + intro x hx y hy + exact hcross x (List.mem_append_right _ hx) y hy + have hG_gt_last : ∀ x ∈ G, res.keys.getLast hne < x := + fun x hx => hWG _ hlastW x hx + have hlast_end : (↑(res.keys.getLast hne) : WithTop Key) < r.end_.key := + (ConflictRange.mem_iff.mp (hCcanon (res.keys.getLast hne) + (List.drop_subset b C (List.take_subset _ _ (hW ▸ hlastW))))).2 + -- chain bound + have hresolve_coe : ∃ k : Key, resolve db₀ r.begin = (↑k : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.begin) with h | h | h + · exact ⟨[], h⟩ + · exfalso + have hnil : RangeRead.completeKeys db₀ r = [] := by + rw [RangeRead.completeKeys, ite_eq_left (by simp [hrev])] + simp only [RangeRead.inRange, h] + rw [List.reverse_eq_nil_iff, List.filter_eq_nil_iff] + intro x _ hx + exact absurd (of_decide_eq_true hx).1 (not_le.mpr (WithTop.coe_lt_top x)) + exact List.IsPrefix.ne_nil hpre₀ hne hnil + · obtain ⟨k, _, hk⟩ := h; exact ⟨k, hk⟩ + obtain ⟨k, hk⟩ := hresolve_coe + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + List.IsPrefix.subset hpre₀ (List.getLast_mem hne) + have hk_le_last : k ≤ res.keys.getLast hne := by + have h := (RangeRead.mem_completeKeys.mp hlast_ck).2.1 + rw [hk] at h + exact WithTop.coe_le_coe.mp h + have hchain_b_le_k : chain v b ≤ k := by + have h := chain_le_of_resolve (s := r.begin) (k₀ := v) (k := k) hkey (by omega) hk + rwa [show (r.begin.offset - 1).toNat = b from hb] at h + have hchain_b_le_last : chain v b ≤ res.keys.getLast hne := le_trans hchain_b_le_k hk_le_last + have hchain_lt_last : ∀ i < b, chain v i < res.keys.getLast hne := by + intro i hi + exact lt_of_lt_of_le (chain_lt_mono (b := v) (by omega)) hchain_b_le_last + -- Fp + set Fp : List Key := (List.range b).map (chain v) with hFp + have hFplen : Fp.length = b := by rw [hFp, List.length_map, List.length_range] + have hFpsort : Fp.SortedLT := by rw [hFp]; exact sortedLT_map_chain v b + have hFp_lt_last : ∀ x ∈ Fp, x < res.keys.getLast hne := by + intro x hx + rw [hFp, List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchain_lt_last i (List.mem_range.mp hi) + have hFp_v : v ∈ Fp := by + rw [hFp, List.mem_map] + exact ⟨0, List.mem_range.mpr (by omega), chain_zero v⟩ + have hFp_canon : ∀ x ∈ Fp, x ∈ canonicalRange r := by + intro x hx + have hxlt := hFp_lt_last x hx + rw [hFp, List.mem_map] at hx + obtain ⟨i, _, rfl⟩ := hx + exact ⟨le_trans (le_of_eq hkey) (WithTop.coe_le_coe.mpr (chain_le v i)), + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hxlt) (le_of_lt hlast_end)⟩ + have hresrev_canon : ∀ y ∈ res.keys.reverse, y ∈ canonicalRange r := by + intro y hy + have hyW : y ∈ W := by + rw [hWdecomp] + exact List.mem_append_right _ hy + exact hCcanon y (List.drop_subset b C (List.take_subset _ _ (hW ▸ hyW))) + have hresrev_sort : res.keys.reverse.SortedLT := + (RangeRead.compatible_sortedGT hcomp hrev).reverse + have hlast_le : ∀ y ∈ res.keys.reverse, res.keys.getLast hne ≤ y := by + intro y hy + have hb : y ∈ res.keys := List.mem_reverse.mp hy + have hge : res.keys.Pairwise (fun a b => a ≥ b) := + (RangeRead.compatible_pairwise hcomp).2 hrev |>.imp (fun h => le_of_lt h) + exact hge.rel_getLast hb + -- D + set D : List Key := Fp ++ res.keys.reverse ++ G with hD + have hDcanon : ∀ x ∈ D, x ∈ canonicalRange r := by + intro x hx + rw [hD, List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact hFp_canon x hx + · exact hresrev_canon x hx + · exact hGcanon x hx + have hDsort : D.SortedLT := by + rw [hD] + refine List.sortedLT_append.mpr ⟨?_, hGs, ?_⟩ + · refine List.sortedLT_append.mpr ⟨hFpsort, hresrev_sort, ?_⟩ + intro a ha b hb + exact lt_of_lt_of_le (hFp_lt_last a ha) (hlast_le b hb) + · intro a ha b hb + rw [List.mem_append] at ha + rcases ha with ha | ha + · exact lt_of_lt_of_le (hFp_lt_last a ha) (le_of_lt (hG_gt_last b hb)) + · have haW : a ∈ W := by + rw [hWdecomp]; exact List.mem_append_right _ ha + exact hWG a haW b hb + let dbw : DB := ⟨D, hDsort⟩ + have hcanw : (canonicalRange r).keys dbw = D := + canonicalRange_keys_eq_self_of_forall (db := dbw) (r := r) hDcanon + have hDdrop : D.drop b = res.keys.reverse ++ G := by + rw [hD, List.append_assoc, List.drop_left' hFplen] + have hDlen : D.length = b + res.keys.length + m := by + rw [hD, List.length_append, List.length_append, hFplen, hGlen, List.length_reverse] + have hidx : D.length - b - m = res.keys.length := by rw [hDlen]; omega + have hckw : RangeRead.completeKeys dbw r = res.keys := by + rw [completeKeys_eq_canonical_slice_rev (db := dbw) (r := r) hrev (by omega) hend, hcanw, + hDdrop, hidx, List.take_append_of_le_length (by simp), List.take_of_length_le (by simp), + List.reverse_reverse] + have hcompw : RangeRead.Compatible dbw r res := + ⟨hcomp.1, Or.inl ⟨hmore, by rw [hckw], hne⟩⟩ + -- erase v + have hcan_erase : (canonicalRange r).keys (dbw.erase v) = D.erase v := by + have h1 := canonicalRange_keys_eq_self_of_forall (db := dbw.erase v) (r := r) + (fun x hx => by + rw [DB.erase_keys] at hx + exact hDcanon x (List.erase_subset hx)) + rwa [DB.erase_keys] at h1 + have hDerase : D.erase v = Fp.erase v ++ res.keys.reverse ++ G := by + rw [hD, List.erase_append_left G (List.mem_append_left res.keys.reverse hFp_v), + List.erase_append_left res.keys.reverse hFp_v] + have hFperase_len : (Fp.erase v).length = b - 1 := by + rw [List.length_erase_of_mem hFp_v, hFplen] + have hnot_erase : ¬ RangeRead.Compatible (dbw.erase v) r res := by + intro hc + have hpre := RangeRead.compatible_keys_prefix hc + have hck_erase : RangeRead.completeKeys (dbw.erase v) r = res.keys.dropLast := by + rw [completeKeys_eq_canonical_slice_rev (db := dbw.erase v) (r := r) hrev (by omega) hend, + hcan_erase, hDerase] + rw [show Fp.erase v ++ res.keys.reverse ++ G = Fp.erase v ++ (res.keys.reverse ++ G) from + List.append_assoc _ _ _] + have hdrop1 : (Fp.erase v ++ (res.keys.reverse ++ G)).drop b = + (res.keys.reverse ++ G).drop 1 := by + rw [show b = (b - 1) + 1 by omega, ← List.drop_drop] + rw [List.drop_left' hFperase_len] + rw [hdrop1] + have hidx' : (Fp.erase v ++ (res.keys.reverse ++ G)).length - b - m = + res.keys.length - 1 := by + rw [List.length_append, List.length_append, hFperase_len, hGlen, List.length_reverse] + omega + rw [hidx'] + have hda : (res.keys.reverse ++ G).drop 1 = res.keys.reverse.drop 1 ++ G := + List.drop_append_of_le_length (by rw [List.length_reverse]; exact hlen) + rw [hda, List.take_append_of_le_length (by rw [List.length_drop, List.length_reverse]), + List.take_of_length_le (by rw [List.length_drop, List.length_reverse]), + List.drop_reverse, List.reverse_reverse, List.dropLast_eq_take] + rw [hck_erase] at hpre + have hle := hpre.length_le + rw [List.length_dropLast] at hle + omega + intro cr hs + by_contra hvcr + exact hnot_erase ((hs dbw (dbw.erase v) + (ConflictRange.keys_erase_of_not_mem cr dbw hvcr).symm).mp hcompw) + +/-- **The reversed truncated cell at `begin.offset ≥ 2`, `end.offset ≤ 1`.** -/ +theorem minimal_canonicalRange_of_end_offset_le_one_rev_more + {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbegin : 2 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) : + Minimal r res (canonicalRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hckne : RangeRead.completeKeys db₀ r ≠ [] := + List.IsPrefix.ne_nil (RangeRead.compatible_keys_prefix hcomp) hne + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil_rev hrev hckne (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_canonicalRange_of_end_offset_le_one_rev hrev (by omega) hend, ?_⟩ + intro k hk + have hk' := ConflictRange.mem_iff.mp hk + have h1 : r.begin.key ≤ (↑k : WithTop Key) := hk'.1 + have hvk : v ≤ k := by + rw [hv] at h1 + exact WithTop.coe_le_coe.mp h1 + have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ hlast_mem + by_cases hkl : k ≤ res.keys.getLast hne + · have hv_nec : Necessary r res v := + necessary_begin_key_rev_more ⟨db₀, hcomp⟩ hrev hmore (List.length_pos_of_ne_nil hne) + hbegin hend hv + exact hv_nec.of_between hlast_nec hvk hkl + · have hkgt : res.keys.getLast hne < k := lt_of_not_ge hkl + by_cases hkh : k ≤ res.keys.head hne + · exact necessary_of_between_result ⟨db₀, hcomp⟩ hlast_mem hhead_mem (le_of_lt hkgt) hkh + · exact fun cr hs => + necessary_head_to_end_key ⟨db₀, hcomp⟩ hrev hne hend cr hs + (le_of_lt (lt_of_not_ge hkh)) hk'.2 + + +/-- At begin offset one with a truncated reversed result and a *nonpositive* end +offset, the minimal sound conflict range is `[last reported key, end.key)`, the same +value as at end offset one. Soundness: agreement on `[last, end.key)` fixes the +truncated range's stored keys with the largest `(1 - end.offset)` canonical keys +removed (`completeKeys_filter_ge_eq`), hence fixes `filter (completeKeys) (· ≥ last)`, +which `compatible_iff_truncated_keys_rev` shows is equivalent to compatibility. +Necessity is the same convexity-and-end-side argument as +`minimal_truncatedRangeRev_of_offset_one`. -/ +theorem minimal_truncatedRangeRev_of_end_offset_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset ≤ 0) : + Minimal r res (truncatedRangeRev r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.getLast_mem hne) + have hbl : (r.begin.key : WithTop Key) ≤ (↑(res.keys.getLast hne) : WithTop Key) := + le_trans (key_le_resolve_of_offset_one hbegin) + ((RangeRead.mem_completeKeys.mp hlast_ck).2.1) + have hf₁ := completeKeys_filter_ge_eq (db := db₁) hrev hbegin hend hbl + have hf₂ := completeKeys_filter_ge_eq (db := db₂) hrev hbegin hend hbl + rw [compatible_iff_truncated_keys_rev hrev hmore hne, + compatible_iff_truncated_keys_rev (db := db₂) hrev hmore hne, + hf₁, hf₂, hagree] + · intro k hk cr hs + have hkend : (k : WithTop Key) < r.end_.key := hk.2 + have hklast : res.keys.getLast hne ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + (List.head_mem hne) hklast hkhead cr hs + · have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact necessary_head_to_end_key ⟨db₀, hcomp⟩ hrev hne (by omega) cr hs hheadk hkend + +/-- At offset one with a truncated reversed result, the minimal sound conflict +range is `[last reported key, end.key)`. The reported keys and the end side are +necessary, and no key below the last reported key is necessary because the +truncated range already pins down the reported prefix. -/ +theorem minimal_truncatedRangeRev_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + Minimal r res (truncatedRangeRev r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp).subset hlast_mem + -- The begin key lies at or below the last reported key, so it bounds the range. + have hbl : (r.begin.key : WithTop Key) ≤ (↑(res.keys.getLast hne) : WithTop Key) := + le_trans (key_le_resolve_of_offset_one hbegin) + ((RangeRead.mem_completeKeys.mp hlast_ck).2.1) + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hf₁ := truncatedRangeRev_filter_eq (db := db₁) hrev hbegin hend hbl + have hf₂ := truncatedRangeRev_filter_eq (db := db₂) hrev hbegin hend hbl + rw [compatible_iff_truncated_keys_rev hrev hmore hne, + compatible_iff_truncated_keys_rev (db := db₂) hrev hmore hne] + rw [hf₁, hf₂, hagree] + · intro k hk cr hs + have hkend : (k : WithTop Key) < r.end_.key := hk.2 + have hklast : res.keys.getLast hne ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkhead : k ≤ res.keys.head hne + · -- A key between the last and the head is convex between two reported keys. + exact necessary_of_between_result ⟨db₀, hcomp⟩ hlast_mem (List.head_mem hne) + hklast hkhead cr hs + · -- A key at or above the head and below `end_.key` is necessary by the + -- reversed head-to-end rule. + have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact necessary_head_to_end_key ⟨db₀, hcomp⟩ hrev hne (by omega) cr hs hheadk hkend + +/-- At offset one with a truncated reversed result, the reversed truncated range is +*the* minimal sound conflict range up to set equality. -/ +theorem unique_minimal_truncatedRangeRev_of_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_truncatedRangeRev_of_offset_one hread hmore hrev hbegin hend) + +/-- The offset-one algorithm's value. For a truncated result it cuts just past the +last reported key on the side the traversal has already left behind: non-reversed +it is `[begin.key, keyAfter (last reported key))`, reversed it is +`[last reported key, end.key)`. Otherwise it is the canonical range +`[begin.key, end.key)`. This is a plain `def` with no proof packed in, so it is +computable and `#eval`-able; the correctness certificates are the separate +`conflictRangeOffsetOne_spec`, `conflictRangeOffsetOne_spec_truncated`, and +`conflictRangeOffsetOne_spec_truncated_rev`, which may reason classically. Keeping +the value free of the proof is what makes the algorithm runnable: Lean's +noncomputability check taints any term that embeds a choice-using proof, so a +subtype-valued definition would be noncomputable as a whole even though the proof +component is propositionally irrelevant. The empty fallback in `getLastD` is never +used by the certified cases, where the result is nonempty. -/ +def conflictRangeOffsetOne (r : RangeRead) (res : Result) : ConflictRange := + if res.more = true then + if r.reverse = true then + truncatedRangeRev r (res.keys.getLastD ([] : Key)) + else + truncatedRange r (res.keys.getLastD ([] : Key)) + else + canonicalRange r + +/-- The offset-one algorithm is correct on a complete result: with `more = false` +the reported keys are all the complete keys, so the canonical range +`[begin.key, end.key)` is minimal, whatever the traversal order. The proof is +allowed to use classical logic — only the value must be choice-free. -/ +theorem conflictRangeOffsetOne_spec {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + Minimal r res (conflictRangeOffsetOne r res) := by + simp only [conflictRangeOffsetOne, hmore, Bool.false_eq_true, ite_false] + exact minimal_canonicalRange_of_offset_one hread hmore hbegin hend + +/-- The offset-one algorithm is correct on a truncated non-reversed result: with +`more = true` only a prefix of the complete keys is reported, so the minimal sound +range runs from `begin.key` up to just past the last reported key. -/ +theorem conflictRangeOffsetOne_spec_truncated {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + Minimal r res (conflictRangeOffsetOne r res) := by + simp only [conflictRangeOffsetOne, hmore, hrev, ite_true, Bool.false_eq_true, + ite_false] + exact minimal_truncatedRange_of_offset_one hread hmore hrev hbegin hend + +/-- The offset-one algorithm is correct on a truncated reversed result: with +`more = true` only a prefix of the complete keys is reported, so the minimal sound +range runs from the last reported key up to `end.key`. -/ +theorem conflictRangeOffsetOne_spec_truncated_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) : + Minimal r res (conflictRangeOffsetOne r res) := by + simp only [conflictRangeOffsetOne, hmore, hrev, ite_true] + exact minimal_truncatedRangeRev_of_offset_one hread hmore hrev hbegin hend + +/-! +## The unbounded top at end offsets beyond one + +At offset one the resolved window cuts a key exactly when the canonical interval +does. Beyond offset one the end selector overshoots past the first stored key at +or above `end_.key`, so a complete result that never reaches `end_.key` can only +be reading a window that ends at the sentinel: every key of the witness database +lies below `end_.key`, and then `resolve end_` is `⊤` +(`resolve_end_eq_top_of_hhi`). The minimal sound range is the unbounded top +`[begin.key, ⊤)`. Soundness holds because agreement on the range fixes the +database's keys at or above `begin.key`, which is all the read's window depends +on; necessity chains the reported keys, the begin key, convexity, and +`necessary_of_end_offset_ge_two` for the keys beyond the reported last. +-/ + +/-- The conflict range from the begin key up to the sentinel `⊤`. -/ +def topRange (r : RangeRead) : ConflictRange := ⟨r.begin.key, ⊤⟩ + +/-- At begin offset one, when the window ends at the sentinel the complete keys +are exactly the top range's stored keys: the resolved begin is the first stored +key at or above `begin.key`, so restricting the canonical keys to the window +changes nothing. -/ +private theorem completeKeys_eq_topRange_keys {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hE : resolve db r.end_ = (⊤ : WithTop Key)) : + RangeRead.completeKeys db r = (topRange r).keys db := by + have hbl := key_le_resolve_of_offset_one (db := db) (s := r.begin) hbegin + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev]), hE] + unfold topRange ConflictRange.keys RangeRead.inRange + apply List.filter_congr + intro k hk + rw [decide_eq_decide] + constructor + · rintro ⟨h1, _⟩ + exact ⟨le_trans hbl h1, WithTop.coe_lt_top k⟩ + · rintro ⟨h1, _⟩ + exact ⟨(key_le_resolve_iff_of_offset_one hbegin hk).mp h1, WithTop.coe_lt_top k⟩ + +/-- When the window ends at the sentinel, the complete keys are the top range's +stored keys dropped by `(begin.offset - 1).toNat`: the begin selector overshoots +that many stored keys past the first key at or above `begin.key`. -/ +theorem completeKeys_eq_topRange_keys_drop {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hE : resolve db r.end_ = (⊤ : WithTop Key)) : + RangeRead.completeKeys db r = + ((topRange r).keys db).drop (r.begin.offset - 1).toNat := by + set l := db.keys with hl + set p := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hanchor : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) hbegin, hp, hb, hl] + have hLHS : RangeRead.completeKeys db r = l.drop (p + b) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev]), hE] + unfold RangeRead.inRange + rw [show l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < (⊤ : WithTop Key))) = + l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide]; exact ⟨fun h => h.1, + fun h => ⟨h, WithTop.coe_lt_top k⟩⟩] + rw [filter_resolve_le_eq_drop (db := db) (s := r.begin), hanchor] + have hRHS : (topRange r).keys db = l.drop p := by + unfold topRange ConflictRange.keys + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < (⊤ : WithTop Key))) = + l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide]; exact ⟨fun h => h.1, + fun h => ⟨h, WithTop.coe_lt_top k⟩⟩] + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) = + l.filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < r.begin.key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt]] + rw [filter_not_lt_eq_drop_countP hls r.begin.key, hp] + rw [hLHS, hRHS, List.drop_drop] + +/-- When the window ends at the sentinel, the *reversed* complete keys are the +reversal of the top range's stored keys dropped by `(begin.offset - 1).toNat`: the +begin selector overshoots that many stored keys past the first key at or above +`begin.key`, and reversal only reorders the window. This is the reversal companion +of `completeKeys_eq_topRange_keys_drop`. -/ +private theorem completeKeys_eq_topRange_keys_drop_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : 1 ≤ r.begin.offset) + (hE : resolve db r.end_ = (⊤ : WithTop Key)) : + RangeRead.completeKeys db r = + (((topRange r).keys db).drop (r.begin.offset - 1).toNat).reverse := by + set l := db.keys with hl + set p := l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hanchor : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) hbegin, hp, hb, hl] + have hLHS : RangeRead.completeKeys db r = (l.drop (p + b)).reverse := by + rw [RangeRead.completeKeys, ite_eq_left (by simp [hrev]), hE] + unfold RangeRead.inRange + rw [show l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < (⊤ : WithTop Key))) = + l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide]; exact ⟨fun h => h.1, + fun h => ⟨h, WithTop.coe_lt_top k⟩⟩] + rw [filter_resolve_le_eq_drop (db := db) (s := r.begin), hanchor] + have hRHS : (topRange r).keys db = l.drop p := by + unfold topRange ConflictRange.keys + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < (⊤ : WithTop Key))) = + l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide]; exact ⟨fun h => h.1, + fun h => ⟨h, WithTop.coe_lt_top k⟩⟩] + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) = + l.filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < r.begin.key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt]] + rw [filter_not_lt_eq_drop_countP hls r.begin.key, hp] + rw [hLHS, hRHS, List.drop_drop] + +/-- At begin offset one and end offset at least two, a complete non-reversed result +whose reported keys all lie below `end_.key` has the unbounded top +`[begin.key, ⊤)` as its minimal sound conflict range. The overshoot forces the +witness window to end at the sentinel, and then compatibility depends only on the +keys at or above `begin.key`; the begin key, the reported keys, and everything +convexly between them are necessary, as is every key at or above the reported +last by `necessary_of_end_offset_ge_two`. -/ +theorem minimal_topRange_of_end_offset_ge_two {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hres₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + -- The begin key is strictly below `end_.key`, witnessed by any reported key. + have hbegk_end : r.begin.key < r.end_.key := by + obtain ⟨k₀, hk₀⟩ := List.exists_mem_of_ne_nil _ hne + have h1 : r.begin.key ≤ resolve db₀ r.begin := key_le_resolve_of_offset_one hbegin + have h2 : resolve db₀ r.begin ≤ (k₀ : WithTop Key) := by + have : k₀ ∈ RangeRead.completeKeys db₀ r := hres₀ ▸ hk₀ + exact (RangeRead.mem_completeKeys.mp this).2.1 + exact lt_of_le_of_lt (le_trans h1 h2) (hhi k₀ hk₀) + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · -- Agreement on the top range fixes the keys the read's window can observe. + intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (topRange r) db db' → RangeRead.Compatible db r res → + RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp_db.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + -- A compatible witness has every key below `end_.key`, so its window ends + -- at the sentinel. + have hmaxm : ∀ k ∈ RangeRead.completeKeys db r, k ≤ res.keys.getLast hne := by + intro k hk + rw [← hres] at hk + exact ((RangeRead.compatible_sortedLT hcomp_db hrev).pairwise.imp le_of_lt).rel_getLast hk + have hE : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hcomp_db hend hhi (res.keys.getLast hne) + (List.getLast_mem hne) hmaxm + have hck : RangeRead.completeKeys db r = (topRange r).keys db := + completeKeys_eq_topRange_keys hrev hbegin hE + have hS : (topRange r).keys db = res.keys := by rw [← hck, hres] + have hall : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hbk : r.begin.key ≤ (k : WithTop Key) + · exact hhi k (hS ▸ ConflictRange.mem_keys_iff.mpr + ⟨hk, hbk, WithTop.coe_lt_top k⟩) + · exact lt_of_lt_of_le (lt_of_not_ge hbk) (le_of_lt hbegk_end) + -- The agreeing database shares those keys, so it also overshoots. + have hall' : ∀ k ∈ db'.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hbk : r.begin.key ≤ (k : WithTop Key) + · have hkmem : k ∈ (topRange r).keys db' := + ConflictRange.mem_keys_iff.mpr ⟨hk, hbk, WithTop.coe_lt_top k⟩ + rw [← hag'] at hkmem + rw [hS] at hkmem + exact hhi k hkmem + · exact lt_of_lt_of_le (lt_of_not_ge hbk) (le_of_lt hbegk_end) + have hE' : resolve db' r.end_ = (⊤ : WithTop Key) := by + have hcnt : db'.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) = + db'.keys.length := + List.countP_eq_length.mpr fun k hk => by + rw [decide_eq_true_eq]; exact hall' k hk + have hann : r.end_.anchor db' = (db'.keys.length : Int) + r.end_.offset - 1 := by + unfold Selector.anchor + rw [hcnt] + refine resolve_eq_top ?_ ?_ + · rw [hann]; omega + · have htoNat : (r.end_.anchor db').toNat = + db'.keys.length + (r.end_.offset - 1).toNat := by + rw [hann, show ((db'.keys.length : Int) + r.end_.offset - 1) = + ((db'.keys.length : Int) + (r.end_.offset - 1)) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + rw [htoNat] + omega + have hck' : RangeRead.completeKeys db' r = (topRange r).keys db' := + completeKeys_eq_topRange_keys hrev hbegin hE' + refine ⟨hlim, Or.inr ⟨hmore, ?_⟩⟩ + rw [hck', ← hag', hS] + exact ⟨fun h => step db₁ db₂ hagree h, fun h => step db₂ db₁ hagree.symm h⟩ + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + by_cases hkle : k ≤ res.keys.getLast hne + · -- Between the begin key and the reported last: the begin key's necessity + -- and convexity supply the gap. + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil (db := db₀) hrev + (by rw [← hres₀]; exact hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + · -- At or above the reported last: the end-overrun rule applies. + exact necessary_of_end_offset_ge_two ⟨db₀, hcomp⟩ hrev hmore hne hend hhi + (le_of_lt (lt_of_not_ge hkle)) cr hs + +/-- **The overshoot top range, general positive begin offset.** A complete non-reversed +result whose reported keys all lie below `end_.key` and whose end offset is at least two +has the unbounded top `[begin.key, ⊤)` as its minimal sound conflict range, for any +`begin.offset ≥ 1`. The resolved begin is `(begin.offset - 1)` stored keys past the first +key at or above `begin.key`, so the complete keys are the top range's keys with that many +dropped; agreement on the top range therefore fixes them (both databases overshoot, so +both drops are the same reported keys), and the begin key, the reported keys, and every +key above the reported last are necessary. -/ +theorem minimal_topRange_of_end_offset_ge_two_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) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hres₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + -- The begin key is strictly below `end_.key`, witnessed by any reported key. + have hbegk_end : r.begin.key < r.end_.key := by + obtain ⟨k₀, hk₀⟩ := List.exists_mem_of_ne_nil _ hne + have h1 : r.begin.key ≤ resolve db₀ r.begin := key_le_resolve_of_one_le hbegin + have h2 : resolve db₀ r.begin ≤ (k₀ : WithTop Key) := by + have : k₀ ∈ RangeRead.completeKeys db₀ r := hres₀ ▸ hk₀ + exact (RangeRead.mem_completeKeys.mp this).2.1 + exact lt_of_le_of_lt (le_trans h1 h2) (hhi k₀ hk₀) + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (topRange r) db db' → RangeRead.Compatible db r res → + RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp_db.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hmaxm : ∀ k ∈ RangeRead.completeKeys db r, k ≤ res.keys.getLast hne := by + intro k hk + rw [← hres] at hk + exact ((RangeRead.compatible_sortedLT hcomp_db hrev).pairwise.imp le_of_lt).rel_getLast hk + have hE : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hcomp_db hend hhi (res.keys.getLast hne) + (List.getLast_mem hne) hmaxm + have hck : RangeRead.completeKeys db r = + ((topRange r).keys db).drop (r.begin.offset - 1).toNat := + completeKeys_eq_topRange_keys_drop hrev hbegin hE + have hdrop : ((topRange r).keys db).drop (r.begin.offset - 1).toNat = res.keys := by + rw [← hck, ← hres] + have hdrop' : ((topRange r).keys db').drop (r.begin.offset - 1).toNat = res.keys := by + rw [← hag', hdrop] + have hpk : ((topRange r).keys db).Pairwise (· < ·) := ConflictRange.keys_pairwise _ _ + have hpk' : ((topRange r).keys db').Pairwise (· < ·) := ConflictRange.keys_pairwise _ _ + have hall : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hbk : r.begin.key ≤ (k : WithTop Key) + · have hkmem : k ∈ (topRange r).keys db := + ConflictRange.mem_keys_iff.mpr ⟨hk, hbk, WithTop.coe_lt_top k⟩ + have hle' : k ≤ res.keys.getLast hne := + forall_le_of_drop_eq (List.sortedLT_iff_pairwise.mpr hpk) hdrop hne + (fun y hy => (hsort.pairwise.imp le_of_lt).rel_getLast hy) k hkmem + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle') (hhi _ (List.getLast_mem hne)) + · exact lt_of_lt_of_le (lt_of_not_ge hbk) (le_of_lt hbegk_end) + have hall' : ∀ k ∈ db'.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hbk : r.begin.key ≤ (k : WithTop Key) + · have hkmem : k ∈ (topRange r).keys db' := + ConflictRange.mem_keys_iff.mpr ⟨hk, hbk, WithTop.coe_lt_top k⟩ + have hle' : k ≤ res.keys.getLast hne := + forall_le_of_drop_eq (List.sortedLT_iff_pairwise.mpr hpk') hdrop' hne + (fun y hy => (hsort.pairwise.imp le_of_lt).rel_getLast hy) k hkmem + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle') (hhi _ (List.getLast_mem hne)) + · exact lt_of_lt_of_le (lt_of_not_ge hbk) (le_of_lt hbegk_end) + have hE' : resolve db' r.end_ = (⊤ : WithTop Key) := by + have hcnt : db'.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) = + db'.keys.length := + List.countP_eq_length.mpr fun k hk => by + rw [decide_eq_true_eq]; exact hall' k hk + have hann : r.end_.anchor db' = (db'.keys.length : Int) + r.end_.offset - 1 := by + unfold Selector.anchor + rw [hcnt] + refine resolve_eq_top ?_ ?_ + · rw [hann]; omega + · have htoNat : (r.end_.anchor db').toNat = + db'.keys.length + (r.end_.offset - 1).toNat := by + rw [hann, show ((db'.keys.length : Int) + r.end_.offset - 1) = + ((db'.keys.length : Int) + (r.end_.offset - 1)) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + rw [htoNat] + omega + have hck' : RangeRead.completeKeys db' r = + ((topRange r).keys db').drop (r.begin.offset - 1).toNat := + completeKeys_eq_topRange_keys_drop hrev hbegin hE' + refine ⟨hlim, Or.inr ⟨hmore, ?_⟩⟩ + exact (hck'.trans hdrop').symm + exact ⟨fun h => step db₁ db₂ hagree h, fun h => step db₂ db₁ hagree.symm h⟩ + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + by_cases hkle : k ≤ res.keys.getLast hne + · have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil (db := db₀) hrev + (by rw [← hres₀]; exact hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + · exact necessary_of_end_offset_ge_two ⟨db₀, hcomp⟩ hrev hmore hne hend hhi + (le_of_lt (lt_of_not_ge hkle)) cr hs + +/-- At begin offset one and end offset at least two, the unbounded top range is +*the* minimal sound conflict range up to set equality: it is minimal by +`minimal_topRange_of_end_offset_ge_two`, and every minimal range denotes the same +set by the unconditional `Minimal.toSet_eq`. -/ +theorem unique_minimal_topRange_of_end_offset_ge_two {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = false) + (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topRange_of_end_offset_ge_two hread hmore hrev hne hbegin hend hhi) + +/-- At begin offset one and end offset at least two, a complete reversed result +whose reported keys all lie below `end_.key` has the same unbounded top +`[begin.key, ⊤)` as the non-reversed case: reversal only reorders the reported +keys, so flipping to the forward read and applying +`minimal_topRange_of_end_offset_ge_two` gives the result. -/ +theorem minimal_topRange_of_end_offset_ge_two_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hflip : Readable {r with reverse := false} + {keys := res.keys.reverse, more := false} := + ⟨db₀, (compatible_flip (db := db₀) (b := true) hrev hmore).mpr hcomp⟩ + exact (minimal_flip (b := true) hrev hmore).mp + (minimal_topRange_of_end_offset_ge_two + (r := {r with reverse := false}) (res := {keys := res.keys.reverse, more := false}) + hflip rfl rfl (by simpa using hne) hbegin hend (by simpa using hhi)) + +/-- **The overshoot top range, general positive begin offset, reversed.** The +reversal-symmetric twin of `minimal_topRange_of_end_offset_ge_two_general`, obtained +by flipping the traversal with `minimal_flip`. -/ +theorem minimal_topRange_of_end_offset_ge_two_rev_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hflip : Readable {r with reverse := false} + {keys := res.keys.reverse, more := false} := + ⟨db₀, (compatible_flip (db := db₀) (b := true) hrev hmore).mpr hcomp⟩ + exact (minimal_flip (b := true) hrev hmore).mp + (minimal_topRange_of_end_offset_ge_two_general + (r := {r with reverse := false}) (res := {keys := res.keys.reverse, more := false}) + hflip rfl rfl (by simpa using hne) hbegin hend (by simpa using hhi)) + +/-- **The overshoot top range, general positive begin offset, unique.** The unbounded top +`[begin.key, ⊤)` is *the* minimal sound conflict range up to set equality, by +`minimal_topRange_of_end_offset_ge_two_general` and `Minimal.toSet_eq`. -/ +theorem unique_minimal_topRange_of_end_offset_ge_two_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) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topRange_of_end_offset_ge_two_general hread hmore hrev hne hbegin hend hhi) + +/-- **The overshoot top range, general positive begin offset, reversed, unique.** The +reversal-symmetric twin of `unique_minimal_topRange_of_end_offset_ge_two_general`. -/ +theorem unique_minimal_topRange_of_end_offset_ge_two_rev_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topRange_of_end_offset_ge_two_rev_general hread hmore hrev hne hbegin hend hhi) + +/-- At begin offset one and end offset at least two, the unbounded top range is +*the* minimal sound conflict range up to set equality for a complete reversed +result as well. -/ +theorem unique_minimal_topRange_of_end_offset_ge_two_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) (hrev : r.reverse = true) + (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topRange_of_end_offset_ge_two_rev hread hmore hrev hne hbegin hend hhi) + +/-- At begin offset one and a positive end offset, a truncated non-reversed result +whose reported keys all lie below `end_.key` is compatible exactly when it fits the +limit and equals the truncated range's stored keys. The overshoot hypothesis keeps +the successor of the last reported key below `resolve end_`, so the truncated range +coincides with the complete keys up to the reported last. -/ +private theorem compatible_iff_truncatedRange_keys {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hend : 1 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (truncatedRange r (res.keys.getLast hne)).keys db = res.keys := by + have hka : (keyAfter (res.keys.getLast hne) : WithTop Key) ≤ resolve db r.end_ := + keyAfter_le_of_lt (lt_of_lt_of_le (hhi _ (List.getLast_mem hne)) + (key_le_resolve_of_one_le hend)) + rw [compatible_iff_truncated_keys hrev hmore hne, + truncatedRange_keys_eq_of_resolve hrev hbegin hka] + +/-- At begin offset one and end offset at least two, a truncated non-reversed result +whose reported keys all lie below `end_.key` has the truncated range +`[begin.key, keyAfter (last reported key))` as its minimal sound conflict range, +just as at offset one. The overshoot hypothesis is what keeps this sound: it forces +`resolve end_` above the successor of the last reported key, so keys beyond the +reported last cannot creep into the window. -/ +theorem minimal_truncatedRange_of_end_offset_ge_two {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + rw [compatible_iff_truncatedRange_keys (db := db₁) hrev hmore hne hbegin (by omega) hhi, + compatible_iff_truncatedRange_keys (db := db₂) hrev hmore hne hbegin (by omega) hhi, + hagree] + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- At begin offset one and end offset at least two, the truncated range is *the* +minimal sound conflict range up to set equality for a truncated non-reversed +result. -/ +theorem unique_minimal_truncatedRange_of_end_offset_ge_two {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_truncatedRange_of_end_offset_ge_two hread hmore hrev hbegin hend hhi) + +/-- Dropping a prefix before filtering by a downward-closed predicate is the same +as filtering first: for a strictly increasing list, `(l.drop m).filter (· ≤ t)` is +`(l.filter (· ≤ t)).drop m`. -/ +private theorem filter_le_drop_eq {l : List Key} (hs : l.SortedLT) (t : Key) (m : Nat) : + (l.drop m).filter (fun k => decide (k ≤ t)) = + (l.filter (fun k => decide (k ≤ t))).drop m := by + induction l generalizing m with + | nil => simp + | cons a rest ih => + have hs' : rest.SortedLT := + List.sortedLT_iff_pairwise.mpr (List.pairwise_cons.mp hs.pairwise).2 + rcases m with _ | m + · rfl + · simp only [List.drop_succ_cons] + by_cases ha : a ≤ t + · rw [List.filter_cons_of_pos (by simpa using ha)] + exact ih hs' m + · have hrest : ∀ k ∈ rest, ¬ (k ≤ t) := by + intro k hk hklt + exact ha (le_trans (le_of_lt ((List.pairwise_cons.mp hs.pairwise).1 k hk)) hklt) + have hfilnil : List.filter (fun k => decide (k ≤ t)) rest = [] := + List.filter_eq_nil_iff.mpr (fun k hk => by simpa using hrest k hk) + rw [List.filter_cons_of_neg (by simpa using ha), hfilnil, List.drop_nil] + rw [List.filter_eq_nil_iff] + intro k hk + exact fun hc => hrest k (List.mem_of_mem_drop hk) (of_decide_eq_true hc) + +/-- **Count bound from an overshooting resolved end.** If the reported `last` lies +strictly below `resolve end_`, then at most `(end_.offset - 1).toNat` stored keys lie +in `[end_.key, last]`. At positive end offset the resolution is the +`(end_.offset - 1)`-th stored key at or above `end_.key` +(`resolve_eq_filter_getElem?`), so fewer than `end_.offset` such keys can be at or +below `last`. -/ +theorem filter_length_le_offset_of_resolve_end_gt {db : DB} {r : RangeRead} + {last : Key} (hend : 1 ≤ r.end_.offset) + (hlr : (last : WithTop Key) < resolve db r.end_) : + ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + set L := db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hL + set M := L.filter (fun k : Key => decide (k ≤ last)) with hM + set c := (r.end_.offset - 1).toNat with hc + have hLs : L.SortedLT := by + rw [hL] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hMlen : M.length = L.countP (fun k : Key => decide (k < keyAfter last)) := by + rw [hM, ← List.countP_eq_length_filter] + apply List.countP_congr + intro k _ + simp [lt_keyAfter_iff] + have hiff : ∀ (i : Nat) (hi : i < L.length), (L[i] < keyAfter last ↔ i < M.length) := by + intro i hi + rw [hMlen] + exact ⟨lt_countP_of_getElem_lt hLs hi, getElem_lt_of_lt_countP hLs hi⟩ + have hres := resolve_eq_filter_getElem? (db := db) (s := r.end_) hend + rw [hres] at hlr + by_cases hcL : c < L.length + · rw [List.getElem?_eq_getElem hcL] at hlr + have hlt : last < L[c] := WithTop.coe_lt_coe.mp hlr + have hnlt : ¬ L[c] < keyAfter last := by + rw [lt_keyAfter_iff] + exact not_le.mpr hlt + have hnc : ¬ c < M.length := fun hcm => hnlt ((hiff c hcL).mpr hcm) + have hMc : M.length ≤ c := not_lt.mp hnc + simpa [hM, hL, hc] using hMc + · have hle : M.length ≤ L.length := by + rw [hM] + exact List.length_filter_le .. + have hMc : M.length ≤ c := le_trans hle (le_of_not_gt hcL) + simpa [hM, hL, hc] using hMc + +/-- **Overshooting resolved end from a count bound.** If at most +`(end_.offset - 1).toNat` stored keys lie in `[end_.key, last]`, then `last` lies +strictly below `resolve end_`. This is the converse of +`filter_length_le_offset_of_resolve_end_gt`: the resolution is the +`(end_.offset - 1)`-th key at or above `end_.key`, so a short interval below `last` +cannot reach it. -/ +theorem resolve_end_gt_of_filter_length_le {db : DB} {r : RangeRead} {last : Key} + (hend : 1 ≤ r.end_.offset) + (hcnt : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat) : + (last : WithTop Key) < resolve db r.end_ := by + set L := db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hL + set M := L.filter (fun k : Key => decide (k ≤ last)) with hM + set c := (r.end_.offset - 1).toNat with hc + have hLs : L.SortedLT := by + rw [hL] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hMlen : M.length = L.countP (fun k : Key => decide (k < keyAfter last)) := by + rw [hM, ← List.countP_eq_length_filter] + apply List.countP_congr + intro k _ + simp [lt_keyAfter_iff] + have hiff : ∀ (i : Nat) (hi : i < L.length), (L[i] < keyAfter last ↔ i < M.length) := by + intro i hi + rw [hMlen] + exact ⟨lt_countP_of_getElem_lt hLs hi, getElem_lt_of_lt_countP hLs hi⟩ + have hcnt' : M.length ≤ c := by simpa [hM, hL, hc] using hcnt + have hres := resolve_eq_filter_getElem? (db := db) (s := r.end_) hend + rw [hres] + by_cases hcL : c < L.length + · rw [List.getElem?_eq_getElem hcL] + have hnot : ¬ L[c] < keyAfter last := by + intro hlt + have hcm : c < M.length := (hiff c hcL).mp hlt + omega + have hlt : last < L[c] := by + rw [lt_keyAfter_iff] at hnot + exact not_le.mp hnot + exact WithTop.coe_lt_coe.mpr hlt + · rw [List.getElem?_eq_none (le_of_not_gt hcL)] + exact WithTop.coe_lt_top last + +/-- Monotonicity of `countP` under a pointwise implication. -/ +theorem countP_le_countP_of_imp {l : List Key} {p q : Key → Bool} + (h : ∀ k ∈ l, p k = true → q k = true) : l.countP p ≤ l.countP q := by + induction l with + | nil => simp + | cons a rest ih => + have h' : ∀ k ∈ rest, p k = true → q k = true := + fun k hk => h k (by simp [hk]) + by_cases hp : p a = true + · rw [List.countP_cons_of_pos hp] + have hq : q a = true := h a (by simp) hp + rw [List.countP_cons_of_pos hq] + exact Nat.succ_le_succ (ih h') + · rw [List.countP_cons_of_neg hp] + have hqle : rest.countP q ≤ (a :: rest).countP q := by + by_cases hq : q a = true + · rw [List.countP_cons_of_pos hq]; omega + · rw [List.countP_cons_of_neg hq] + exact le_trans (ih h') hqle + +/-- **The end-count bound at any begin offset and either traversal.** The number of +complete keys at or above `end_.key` is at most `(end_.offset - 1).toNat`. The end +resolution is the `end_.offset`-th stored key at or above `end_.key`, so the window +contains at most the first `(end_.offset - 1)` such keys; a higher begin resolution only +removes more of them. Unlike `completeKeys_count_ge_le_general`, no hypothesis on the +begin selector or the traversal order is needed. -/ +theorem completeKeys_count_ge_le_end_offset {db : DB} {r : RangeRead} + (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + set l := db.keys with hl + set Q := l.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hQ + set e := (r.end_.offset - 1).toNat with he + have hQs : Q.SortedLT := by + rw [hQ] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hperm := RangeRead.completeKeys_perm_inRange db r + have hcount : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := + hperm.countP_eq _ + rw [hcount] + have hcnt : (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (Q.filter (fun k : Key => + decide ((k : WithTop Key) < resolve db r.end_))).length := by + rw [RangeRead.inRange, List.countP_filter] + rw [← List.countP_eq_length_filter] + rw [hQ] + rw [List.countP_filter] + apply countP_le_countP_of_imp + intro k _ hp + simp only [Bool.and_eq_true] at hp ⊢ + exact ⟨decide_eq_true (of_decide_eq_true hp.2).2, hp.1⟩ + have hQle : (Q.filter (fun k : Key => + decide ((k : WithTop Key) < resolve db r.end_))).length ≤ e := by + have hres := resolve_eq_filter_getElem? (db := db) (s := r.end_) hend + rw [← hl, ← hQ] at hres + by_cases hQe : e < Q.length + · rw [List.getElem?_eq_getElem hQe] at hres + rw [hres] + have htake : Q.filter (fun k : Key => + decide ((k : WithTop Key) < (↑(Q[e]) : WithTop Key))) = Q.take e := by + rw [filter_lt_eq_take_countP hQs (↑(Q[e]) : WithTop Key)] + congr 1 + rw [show Q.countP (fun (k : Key) => + decide ((k : WithTop Key) < (↑(Q[e]) : WithTop Key))) = + Q.countP (fun k : Key => decide (k < Q[e])) from by + apply List.countP_congr + intro k _ + rw [decide_eq_true_eq, decide_eq_true_eq, WithTop.coe_lt_coe]] + exact countP_lt_eq_of_getElem hQs hQe rfl + rw [htake, List.length_take, Nat.min_eq_left (le_of_lt hQe)] + · rw [List.getElem?_eq_none (le_of_not_gt hQe)] at hres + rw [hres] + have hall : Q.filter (fun k : Key => + decide ((k : WithTop Key) < (⊤ : WithTop Key))) = Q := by + rw [List.filter_eq_self] + intro k _ + simp + rw [hall] + exact le_of_not_gt hQe + exact le_trans hcnt hQle + +/-- The number of *reported* keys at or above `end_.key` is at most +`(end_.offset - 1).toNat`, for a truncated result and any read whose end offset is +positive. -/ +theorem reported_count_ge_le_end_offset {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hne : res.keys ≠ []) + (hend : 1 ≤ r.end_.offset) : + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + obtain ⟨db, hcomp⟩ := hread + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + exact le_trans (List.Sublist.countP_le hpre.sublist) + (completeKeys_count_ge_le_end_offset (db := db) (r := r) hend) + +/-- Agreement on the truncated range fixes the stored keys in `[end_.key, last]`, +because `begin.key ≤ end_.key` puts that interval inside `[begin.key, last]`. -/ +private theorem filter_end_ge_le_eq_of_agree {db₁ db₂ : DB} {r : RangeRead} {last : Key} + (hcross : r.begin.key ≤ r.end_.key) + (hagree : AgreeOn (truncatedRange r last) db₁ db₂) : + (db₁.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + (db₂.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + have hT : (truncatedRange r last).keys db₁ = (truncatedRange r last).keys db₂ := hagree + have hkeys_of : ∀ (dbA dbB : DB), + (truncatedRange r last).keys dbA = (truncatedRange r last).keys dbB → + ∀ {k : Key}, r.end_.key ≤ (k : WithTop Key) → k ≤ last → + (k ∈ dbA.keys ↔ k ∈ dbB.keys) := by + intro dbA dbB hTAB k hEk hkle + have hmemT : ∀ (db : DB), k ∈ db.keys → k ∈ (truncatedRange r last).keys db := by + intro db hk + rw [ConflictRange.mem_keys_iff] + exact ⟨hk, ConflictRange.mem_iff.mpr + ⟨le_trans hcross hEk, + WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle)⟩⟩ + constructor + · intro hk + have h1 : k ∈ (truncatedRange r last).keys dbA := hmemT dbA hk + have h2 : k ∈ (truncatedRange r last).keys dbB := hTAB ▸ h1 + exact (ConflictRange.mem_keys_iff.mp h2).1 + · intro hk + have h1 : k ∈ (truncatedRange r last).keys dbB := hmemT dbB hk + have h2 : k ∈ (truncatedRange r last).keys dbA := hTAB.symm ▸ h1 + exact (ConflictRange.mem_keys_iff.mp h2).1 + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr ((db₁.sorted.pairwise.filter _).filter _) + · exact List.sortedLT_iff_pairwise.mpr ((db₂.sorted.pairwise.filter _).filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk₁, hEk⟩, hkle⟩ + exact ⟨⟨(hkeys_of db₁ db₂ hT hEk hkle).mp hk₁, hEk⟩, hkle⟩ + · rintro ⟨⟨hk₂, hEk⟩, hkle⟩ + exact ⟨⟨(hkeys_of db₁ db₂ hT hEk hkle).mpr hk₂, hEk⟩, hkle⟩ + +/-- **The resolved end overshoots exactly when agreement does.** With `begin.key ≤ +end_.key`, agreement on the truncated range fixes the count of stored keys in +`[end_.key, last]`, so the condition `last < resolve end_` holds for one database +exactly when it holds for the other. -/ +private theorem lt_resolve_end_iff_of_agree {db₁ db₂ : DB} {r : RangeRead} {last : Key} + (hend : 1 ≤ r.end_.offset) (hcross : r.begin.key ≤ r.end_.key) + (hagree : AgreeOn (truncatedRange r last) db₁ db₂) : + ((last : WithTop Key) < resolve db₁ r.end_) ↔ + ((last : WithTop Key) < resolve db₂ r.end_) := by + have heq := filter_end_ge_le_eq_of_agree (r := r) (last := last) hcross hagree + constructor + · intro h1 + have hc1 := filter_length_le_offset_of_resolve_end_gt (db := db₁) hend h1 + have hc2 : ((db₂.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [← heq] + exact hc1 + exact resolve_end_gt_of_filter_length_le (db := db₂) hend hc2 + · intro h2 + have hc2 := filter_length_le_offset_of_resolve_end_gt (db := db₂) hend h2 + have hc1 : ((db₁.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [heq] + exact hc2 + exact resolve_end_gt_of_filter_length_le (db := db₁) hend hc1 + +/-- **The truncated window at a general positive begin offset.** For a non-reversed +read with `begin.offset ≥ 1` and a reported `last` below `end_.key`, filtering the +complete keys at or below `last` is the truncated range's stored keys with the first +`(begin.offset - 1).toNat` dropped. The begin selector skips that many stored keys +at or above `begin.key`, so `completeKeys` is the stored keys at or above +`begin.key` dropped by that count (`filter_ge_...`), and `last < end_.key` puts +`last` below `resolve end_` (`key_le_resolve_of_one_le`), so the upper window cut is +inactive on the keys at or below `last`. -/ +theorem completeKeys_filter_le_drop {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hlr : (last : WithTop Key) < resolve db r.end_) : + (RangeRead.completeKeys db r).filter (fun k => decide (k ≤ last)) = + ((truncatedRange r last).keys db).drop (r.begin.offset - 1).toNat := by + set l := db.keys with hl + set p := l.countP (fun k => decide ((k : Key) < r.begin.key)) with hp + set b := (r.begin.offset - 1).toNat with hb + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hA : l.filter (fun (k : Key) => decide (r.begin.key ≤ (k : WithTop Key))) = l.drop p := by + have hcongr : l.filter (fun (k : Key) => decide (r.begin.key ≤ (k : WithTop Key))) = + l.filter (fun (k : Key) => decide (¬ ((k : Key) : WithTop Key) < r.begin.key)) := + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt] + rw [hcongr, filter_not_lt_eq_drop_countP hls r.begin.key, hp] + have hR : l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (k : WithTop Key))) = + l.drop (p + b) := by + rw [filter_resolve_le_eq_drop (db := db) (s := r.begin), + anchor_toNat_eq (s := r.begin) hbegin, ← hp, ← hb] + have hC : (truncatedRange r last).keys db = + (l.drop p).filter (fun k => decide (k ≤ last)) := by + unfold truncatedRange ConflictRange.keys + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (keyAfter last : Key))) = + l.filter (fun (k : Key) => decide ((k : WithTop Key) < (keyAfter last : Key)) && + decide (r.begin.key ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hA, + show (fun (k : Key) => decide ((k : WithTop Key) < (keyAfter last : Key))) = + (fun (k : Key) => decide (k ≤ last)) from + funext fun k => by rw [decide_eq_decide, WithTop.coe_lt_coe]; exact lt_keyAfter_iff k last] + have hck : RangeRead.completeKeys db r = + (l.drop (p + b)).filter + (fun (k : Key) => decide ((k : WithTop Key) < resolve db r.end_)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + unfold RangeRead.inRange + rw [show l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) = + l.filter (fun (k : Key) => decide ((k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hR] + calc (RangeRead.completeKeys db r).filter (fun k => decide (k ≤ last)) + = ((l.drop (p + b)).filter + (fun (k : Key) => decide ((k : WithTop Key) < resolve db r.end_))).filter + (fun k => decide (k ≤ last)) := by rw [hck] + _ = (l.drop (p + b)).filter (fun (k : Key) => + decide (k ≤ last) && decide ((k : WithTop Key) < resolve db r.end_)) := by + rw [List.filter_filter] + _ = (l.drop (p + b)).filter (fun k => decide (k ≤ last)) := by + apply List.filter_congr + intro k _ + by_cases hle : k ≤ last + · rw [decide_eq_true hle, + decide_eq_true (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlr)] + rfl + · rw [decide_eq_false hle, Bool.false_and] + _ = ((l.drop p).filter (fun k => decide (k ≤ last))).drop b := by + rw [show l.drop (p + b) = (l.drop p).drop b from (List.drop_drop (i := b) (j := p)).symm] + exact filter_le_drop_eq (by + rw [List.sortedLT_iff_pairwise] + exact List.Pairwise.drop (i := p) (List.sortedLT_iff_pairwise.mp hls)) last b + _ = ((truncatedRange r last).keys db).drop b := by rw [hC] + +/-- **The truncated forward value at a general positive begin offset.** A truncated +non-reversed result whose reported keys all lie below `end_.key` is compatible +exactly when it fits the limit and its keys are the truncated range's stored keys +with the begin selector's skipped prefix dropped. The overshoot hypothesis keeps +every reported key below `resolve end_`, and the begin selector at offset `1 + b` +skips the first `b` stored keys at or above `begin.key`; agreement on the truncated +range fixes the rest. -/ +private theorem compatible_iff_truncatedRange_keys_general {db : DB} {r : RangeRead} + {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + ((truncatedRange r (res.keys.getLast hne)).keys db).drop + (r.begin.offset - 1).toNat = res.keys := by + rw [compatible_iff_truncated_keys hrev hmore hne, + completeKeys_filter_le_drop (db := db) hrev hbegin + (lt_of_lt_of_le (hhi _ (List.getLast_mem hne)) (key_le_resolve_of_one_le hend))] + +/-- **The truncated forward overshoot range, general positive begin offset.** A +truncated non-reversed result whose reported keys all lie below `end_.key` has the +truncated range `[begin.key, keyAfter (last reported key))` as its minimal sound +conflict range, for any `begin.offset ≥ 1` and `end.offset ≥ 2`. At begin offset +one this is `minimal_truncatedRange_of_end_offset_ge_two`; at higher offsets the +begin selector skips the first `(begin.offset - 1).toNat` stored keys at or above +`begin.key`, but those keys lie between `begin.key` and the reported last and so +are necessary by convexity, while agreement on the truncated range still fixes +`completeKeys` up to the reported last +(`compatible_iff_truncatedRange_keys_general`). -/ +theorem minimal_truncatedRange_of_end_offset_ge_two_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + rw [compatible_iff_truncatedRange_keys_general (db := db₁) hrev hmore hne hbegin + (by omega) hhi, + compatible_iff_truncatedRange_keys_general (db := db₂) hrev hmore hne hbegin + (by omega) hhi, hagree] + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- At a general positive begin offset and end offset at least two, the truncated +range is *the* minimal sound conflict range up to set equality for a truncated +non-reversed result. -/ +theorem unique_minimal_truncatedRange_of_end_offset_ge_two_general {r : RangeRead} + {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_truncatedRange_of_end_offset_ge_two_general hread hmore hrev hbegin hend hhi) + +/-- **The truncated forward `end.offset = 1` cell at a general positive begin offset.** +A truncated non-reversed result with `1 ≤ begin.offset` and `end_.offset = 1` has the +truncated range `[begin.key, keyAfter (last reported key))` as its minimal sound conflict +range. The end offset one puts the window end at the first stored key at or above +`end_.key`, so the reported last lies strictly below `end_.key` (else `resolve end_` would +not exceed it), and `keyAfter last ≤ resolve end_` follows; agreement on the truncated +range then fixes the complete keys up to the reported last +(`completeKeys_filter_le_drop`, dropping the begin selector's skipped prefix), so +soundness holds exactly as in the overshoot case. Necessity is the begin key's necessity +and convexity up to the reported last. This certifies the boundary case at +`end_.offset = 1` and `begin.offset ≥ 2`, where the resolved begin can land above +`begin.key`. -/ +theorem minimal_truncatedRange_of_end_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset = 1) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + hpre₀.subset (List.getLast_mem hne) + have hlast_stored : res.keys.getLast hne ∈ db₀.keys := + (RangeRead.mem_completeKeys.mp hlast_ck).1 + have hlast_lt_res : (res.keys.getLast hne : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + -- At offset one the resolved end is the first stored key at or above `end_.key`, so + -- the reported last (which the result reaches) must lie strictly below it. + have hlast_end : (res.keys.getLast hne : WithTop Key) < r.end_.key := by + by_contra hcon + have hge : r.end_.key ≤ (↑(res.keys.getLast hne) : WithTop Key) := not_lt.mp hcon + have hrle : resolve db₀ r.end_ ≤ (↑(res.keys.getLast hne) : WithTop Key) := + (key_le_resolve_iff_of_offset_one hend hlast_stored).mp hge + exact absurd hlast_lt_res (not_lt.mpr hrle) + -- Compatibility is decided by the truncated range's stored keys with the begin + -- selector's skipped prefix dropped, for any database with the same `(r, res)`. + have hac : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + ((truncatedRange r (res.keys.getLast hne)).keys db).drop + (r.begin.offset - 1).toNat = res.keys := fun db => by + rw [compatible_iff_truncated_keys (db := db) hrev hmore hne, + completeKeys_filter_le_drop (db := db) hrev hbegin + (lt_of_lt_of_le hlast_end (key_le_resolve_of_one_le (by omega)))] + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + rw [hac db₁, hac db₂, hagree] + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- At a general positive begin offset and `end.offset = 1`, the truncated range is *the* +minimal sound conflict range up to set equality for a truncated non-reversed result. -/ +theorem unique_minimal_truncatedRange_of_end_offset_one {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset = 1) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_truncatedRange_of_end_offset_one hread hmore hrev hbegin hend) + +/-- **The truncated forward value, cross-free, at any positive end offset.** For a +non-reversed truncated result with `begin.key ≤ end_.key`, `begin.offset ≥ 1` and +`end.offset ≥ 1`, the minimal sound conflict range is +`[begin.key, keyAfter (last reported))`. This unifies the overshoot cell +(`minimal_truncatedRange_of_end_offset_ge_two_general`) and the offset-one cell; the +new content is the non-overshooting cell `end_.key ≤ last` at `begin.offset ≥ 2`. +Agreement on the truncated range fixes the stored keys in `[end_.key, last]`, which is +exactly the count deciding whether `resolve end_` overshoots `last` +(`lt_resolve_end_iff_of_agree`), so compatibility is agreement-stable. -/ +theorem minimal_truncatedRange_of_more_crossfree {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hcross : r.begin.key ≤ r.end_.key) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + -- The complete-keys filter is determined by the truncated range together with the + -- overshoot condition `last < resolve end_`. + have keyF : ∀ db : DB, + ((RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = res.keys) ↔ + (((truncatedRange r (res.keys.getLast hne)).keys db).drop + (r.begin.offset - 1).toNat = res.keys ∧ + (res.keys.getLast hne : WithTop Key) < resolve db r.end_) := by + intro db + constructor + · intro hF + have hmem : res.keys.getLast hne ∈ (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [hF]; exact List.getLast_mem hne + have hlr : (res.keys.getLast hne : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp (List.mem_of_mem_filter hmem)).2.2 + have hT := completeKeys_filter_le_drop (db := db) hrev hbegin hlr + rw [hF] at hT + exact ⟨hT.symm, hlr⟩ + · rintro ⟨hT, hlr⟩ + have hF := completeKeys_filter_le_drop (db := db) hrev hbegin hlr + rw [hF] + exact hT + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hT : (truncatedRange r (res.keys.getLast hne)).keys db₁ = + (truncatedRange r (res.keys.getLast hne)).keys db₂ := hagree + have hlrIff := lt_resolve_end_iff_of_agree (db₁ := db₁) (db₂ := db₂) + (r := r) (last := res.keys.getLast hne) hend hcross hagree + rw [compatible_iff_truncated_keys hrev hmore hne, + compatible_iff_truncated_keys (db := db₂) hrev hmore hne, + keyF db₁, keyF db₂, hT] + exact ⟨fun h => ⟨h.1, h.2.1, hlrIff.mp h.2.2⟩, + fun h => ⟨h.1, h.2.1, hlrIff.mpr h.2.2⟩⟩ + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- **The forward-truncated closed form for every positive begin offset.** A +non-reversed truncated result with `begin.offset ≥ 1`, `begin.key ≤ end_.key` and +`end.offset ≥ 1` has the truncated range `[begin.key, keyAfter (last reported))` as its +minimal sound conflict range, with no residual cell in the cross-free forward-truncated +region. -/ +theorem minimal_truncatedRange_of_more_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hcross : r.begin.key ≤ r.end_.key) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := + minimal_truncatedRange_of_more_crossfree hread hmore hrev hbegin hend hcross + +/-- The cross-free forward-truncated value is unique up to the key set of a minimal +sound range. -/ +theorem unique_minimal_truncatedRange_of_more_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false) + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hcross : r.begin.key ≤ r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_truncatedRange_of_more_general hread hmore hrev hbegin hend hcross) + +/-- If every stored key lies strictly below `end_.key` and the end offset is at +least two, the end selector resolves to the sentinel: the anchor lands past every +stored key. This is the offset hypothesis form of the argument in +`minimal_topRange_of_end_offset_ge_two`. -/ +theorem resolve_eq_top_of_all_lt {db : DB} {r : RangeRead} + (hall : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hoff : 2 ≤ r.end_.offset) : + resolve db r.end_ = (⊤ : WithTop Key) := by + have hcnt : db.keys.countP + (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.end_.key)) = + db.keys.length := + List.countP_eq_length.mpr fun k hk => by rw [decide_eq_true_eq]; exact hall k hk + have hann : r.end_.anchor db = (db.keys.length : Int) + r.end_.offset - 1 := by + unfold Selector.anchor + rw [hcnt] + refine resolve_eq_top ?_ ?_ + · rw [hann]; omega + · have htoNat : (r.end_.anchor db).toNat = + db.keys.length + (r.end_.offset - 1).toNat := by + rw [hann, show ((db.keys.length : Int) + r.end_.offset - 1) = + ((db.keys.length : Int) + (r.end_.offset - 1)) by ring, + Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast] + rw [htoNat] + omega + +/-- The reversed overshoot range at `begin.offset = 1`: from the last (smallest) +reported key up to the sentinel `⊤`. -/ +def topOfLast (last : Key) : ConflictRange := ⟨(last : WithTop Key), ⊤⟩ + +/-- The top-of-last range's stored keys are exactly the stored keys at or above +`last`. -/ +theorem topOfLast_keys (last : Key) (db : DB) : + (topOfLast last).keys db = db.keys.filter (fun k => decide (last ≤ k)) := by + unfold topOfLast ConflictRange.keys + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => WithTop.coe_le_coe.mp h.1, + fun h => ⟨WithTop.coe_le_coe.mpr h, WithTop.coe_lt_top k⟩⟩ + +/-- With an unbounded window, filtering the reversed complete keys at or above the +last reported key is the reversal of the stored keys at or above it. -/ +private theorem completeKeys_filter_ge_rev {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = true) (hE : resolve db r.end_ = (⊤ : WithTop Key)) + (hL : resolve db r.begin ≤ (last : WithTop Key)) : + (RangeRead.completeKeys db r).filter (fun k => decide (last ≤ k)) = + (db.keys.filter (fun k => decide (last ≤ k))).reverse := by + have hin : RangeRead.inRange db.keys (resolve db r.begin) (⊤ : WithTop Key) = + db.keys.filter (fun (k : Key) => decide (resolve db r.begin ≤ (k : WithTop Key))) := by + unfold RangeRead.inRange + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.1, fun h => ⟨h, WithTop.coe_lt_top k⟩⟩ + rw [RangeRead.completeKeys] + simp only [hrev, hE, ite_true, hin] + rw [List.filter_reverse, List.filter_filter] + congr 1 + apply List.filter_congr + intro k _ + by_cases hLk : last ≤ k + · have hbeg : resolve db r.begin ≤ (k : WithTop Key) := + le_trans hL (by exact_mod_cast hLk) + simp [hLk, hbeg] + · simp [hLk] + +/-- At begin offset one and end offset at least two, a truncated reversed result +whose reported keys all lie below `end_.key` has the unbounded top from its last +(smallest) reported key, `[last, ⊤)`, as its minimal sound conflict range. The +overshoot forces every witness window to end at the sentinel +(`resolve_end_eq_top_of_hhi`), so compatibility depends only on the stored keys at +or above the last reported key; those keys, the reported ones, and everything +convexly between them are necessary, as is every key at or above the reported head +by `necessary_of_end_offset_ge_two_rev`. -/ +theorem minimal_topOfLast_of_end_offset_ge_two_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topOfLast (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · -- Agreement on `[last, ⊤)` fixes exactly the stored keys the reversed prefix + -- can observe. + intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (topOfLast (res.keys.getLast hne)) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp_db + have hL : resolve db r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (hpre.subset (List.getLast_mem hne))).2.1 + have hE : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hcomp_db hend hhi (res.keys.head hne) + (List.head_mem hne) fun k hk => le_head_of_mem_completeKeys_rev hrev hpre hne hk + have hfilter : (RangeRead.completeKeys db r).filter + (fun k => decide (res.keys.getLast hne ≤ k)) = res.keys := + ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp_db).2 + have hdbEq : db.keys.filter (fun k => decide (res.keys.getLast hne ≤ k)) = + res.keys.reverse := by + have h := completeKeys_filter_ge_rev (db := db) hrev hE hL + rw [hfilter] at h + have hrevh := congrArg List.reverse h + simpa only [List.reverse_reverse] using hrevh.symm + have hag'' : (topOfLast (res.keys.getLast hne)).keys db = + (topOfLast (res.keys.getLast hne)).keys db' := hag' + rw [topOfLast_keys, topOfLast_keys] at hag'' + have hdbEq' : db'.keys.filter (fun k => decide (res.keys.getLast hne ≤ k)) = + res.keys.reverse := by + rw [← hag'', hdbEq] + -- The last reported key is stored in `db'`, so `resolve begin ≤ last`. + have hlast_mem : res.keys.getLast hne ∈ db'.keys := by + have h1 : res.keys.getLast hne ∈ res.keys.reverse := + List.mem_reverse.mpr (List.getLast_mem hne) + rw [← hdbEq'] at h1 + exact (List.mem_filter.mp h1).1 + have hkble : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + le_trans (key_le_resolve_of_offset_one hbegin) hL + have hL' : resolve db' r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (key_le_resolve_iff_of_offset_one hbegin hlast_mem).mp hkble + -- Every stored key of `db'` lies below `end_.key`, so the window is unbounded. + have hall' : ∀ k ∈ db'.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hklast : res.keys.getLast hne ≤ k + · have hkf : k ∈ db'.keys.filter (fun k => decide (res.keys.getLast hne ≤ k)) := + List.mem_filter.mpr ⟨hk, by rw [decide_eq_true_eq]; exact hklast⟩ + rw [hdbEq'] at hkf + exact hhi k (List.mem_reverse.mp hkf) + · have hklt : k < res.keys.getLast hne := lt_of_not_ge hklast + exact lt_trans (by exact_mod_cast hklt) (hhi _ (List.getLast_mem hne)) + have hE' : resolve db' r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hall' hend + have hfilter' : (RangeRead.completeKeys db' r).filter + (fun k => decide (res.keys.getLast hne ≤ k)) = res.keys := by + rw [completeKeys_filter_ge_rev (db := db') hrev hE' hL', hdbEq', List.reverse_reverse] + exact (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr ⟨hlim, hfilter'⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hk + have hklast : res.keys.getLast hne ≤ k := + WithTop.coe_le_coe.mp (show ((res.keys.getLast hne : Key) : WithTop Key) ≤ + (k : WithTop Key) from hk.1) + by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne) + (List.head_mem hne) hklast hkhead + · have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact necessary_of_end_offset_ge_two_rev ⟨db₀, hcomp₀⟩ hrev hne hend hhi hheadk + +/-- At begin offset one and end offset at least two, the top-from-last range is *the* +minimal sound conflict range up to set equality for a truncated reversed result. -/ +theorem unique_minimal_topOfLast_of_end_offset_ge_two_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topOfLast_of_end_offset_ge_two_rev hread hmore hrev hbegin hend hhi) + +/-- **The cross-free nonpositive-begin reversed truncated overshoot.** A reversed +truncated result at a *nonpositive* begin offset whose begin key lies at or below the +least (last) reported key and whose end overshoots (`2 ≤ end_.offset`, every reported +key strictly below `end_.key`) has the unbounded top from its least reported key, +`[last, ⊤)`, as its minimal sound conflict range. This is the nonpositive-begin +companion of `minimal_topOfLast_of_end_offset_ge_two_rev`: instead of a positive begin +offset resolving onto the reported block, the cross-free hypothesis `begin.key ≤ last` +gives `resolve begin ≤ last` directly (`resolve_le_key_of_offset_nonpos`), and the +overshoot still forces every witness window to end at the sentinel +(`resolve_end_eq_top_of_hhi`). The reported keys and everything between them are +necessary (`necessary_of_between_result`), and every key at or above the reported head +by `necessary_of_end_offset_ge_two_rev`. -/ +theorem minimal_topOfLast_of_end_offset_ge_two_rev_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hend : 2 ≤ r.end_.offset) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topOfLast (res.keys.getLast hne)) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hreadable : Readable r res := ⟨db₀, hcomp₀⟩ + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (topOfLast (res.keys.getLast hne)) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp_db + have hL : resolve db r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (hpre.subset (List.getLast_mem hne))).2.1 + have hE : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hcomp_db hend hhi (res.keys.head hne) + (List.head_mem hne) fun k hk => le_head_of_mem_completeKeys_rev hrev hpre hne hk + have hfilter : (RangeRead.completeKeys db r).filter + (fun k => decide (res.keys.getLast hne ≤ k)) = res.keys := + ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp_db).2 + have hdbEq : db.keys.filter (fun k => decide (res.keys.getLast hne ≤ k)) = + res.keys.reverse := by + have h := completeKeys_filter_ge_rev (db := db) hrev hE hL + rw [hfilter] at h + have hrevh := congrArg List.reverse h + simpa only [List.reverse_reverse] using hrevh.symm + have hag'' : (topOfLast (res.keys.getLast hne)).keys db = + (topOfLast (res.keys.getLast hne)).keys db' := hag' + rw [topOfLast_keys, topOfLast_keys] at hag'' + have hdbEq' : db'.keys.filter (fun k => decide (res.keys.getLast hne ≤ k)) = + res.keys.reverse := by + rw [← hag'', hdbEq] + -- A nonpositive begin offset resolves at or below the begin key, which the + -- cross-free hypothesis puts at or below the last reported key. + have hL' : resolve db' r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + le_trans (resolve_le_key_of_offset_nonpos hbegin) hlo + -- Every stored key of `db'` lies below `end_.key`, so the window is unbounded. + have hall' : ∀ k ∈ db'.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hklast : res.keys.getLast hne ≤ k + · have hkf : k ∈ db'.keys.filter (fun k => decide (res.keys.getLast hne ≤ k)) := + List.mem_filter.mpr ⟨hk, by rw [decide_eq_true_eq]; exact hklast⟩ + rw [hdbEq'] at hkf + exact hhi k (List.mem_reverse.mp hkf) + · have hklt : k < res.keys.getLast hne := lt_of_not_ge hklast + exact lt_trans (by exact_mod_cast hklt) (hhi _ (List.getLast_mem hne)) + have hE' : resolve db' r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hall' hend + have hfilter' : (RangeRead.completeKeys db' r).filter + (fun k => decide (res.keys.getLast hne ≤ k)) = res.keys := by + rw [completeKeys_filter_ge_rev (db := db') hrev hE' hL', hdbEq', List.reverse_reverse] + exact (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr ⟨hlim, hfilter'⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hk + have hklast : res.keys.getLast hne ≤ k := + WithTop.coe_le_coe.mp (show ((res.keys.getLast hne : Key) : WithTop Key) ≤ + (k : WithTop Key) from hk.1) + by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result hreadable (List.getLast_mem hne) + (List.head_mem hne) hklast hkhead + · have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact necessary_of_end_offset_ge_two_rev hreadable hrev hne hend hhi hheadk + +/-- At a nonpositive begin offset, the top-from-last range is *the* minimal sound +conflict range up to set equality for a cross-free truncated reversed overshoot. -/ +theorem unique_minimal_topOfLast_of_end_offset_ge_two_rev_nonpos {r : RangeRead} + {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hend : 2 ≤ r.end_.offset) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topOfLast_of_end_offset_ge_two_rev_nonpos hread hmore hrev hne hbegin + hend hlo hhi) + +/-! +## The nonpositive-begin truncated reversed under-end cell + +A truncated reversed result at a nonpositive begin offset, cross-free (`begin.key` at +or below the reported last) and with `end_.offset = 1`, has the reversed truncated +range `[last reported, end_.key)` as its minimal sound conflict range. This is the +under-end companion of `minimal_topOfLast_of_end_offset_ge_two_rev_nonpos`: instead of +an overshooting end forcing the window to the sentinel, the end resolves at +`end_.key`, and the cut at `resolve end_` is the cut at `end_.key` +(`resolve_lt_iff_of_offset_one`), so agreement on `[last, end_.key)` fixes exactly the +stored keys the reversed prefix can observe. The reported keys and everything between +them are necessary by convexity, and every key at or above the reported head by +`necessary_head_to_end_key`. +-/ + +/-- **The nonpositive-begin reversed truncated filter identity at `end_.offset = 1`.** +For a reversed read at a nonpositive begin offset with `end_.offset = 1` and +`begin.key ≤ last`, filtering the complete keys at or above `last` is the reversal of +the stored keys at or above `last` and below `end_.key`. A nonpositive begin offset +resolves at or below `begin.key ≤ last` (`resolve_le_key_of_offset_nonpos`), so the +lower cut drops out, and at `end_.offset = 1` the cut at `resolve end_` is the cut at +`end_.key` (`resolve_lt_iff_of_offset_one`). -/ +private theorem completeKeys_filter_ge_eq_end_one_nonpos {db : DB} {r : RangeRead} + {last : Key} (hrev : r.reverse = true) (hbegin : r.begin.offset ≤ 0) + (hend : r.end_.offset = 1) + (hbl : (r.begin.key : WithTop Key) ≤ (last : WithTop Key)) : + (RangeRead.completeKeys db r).filter (fun k => decide (last ≤ k)) = + ((truncatedRangeRev r last).keys db).reverse := by + have hL : resolve db r.begin ≤ (last : WithTop Key) := + le_trans (resolve_le_key_of_offset_nonpos hbegin) hbl + simp only [RangeRead.completeKeys, hrev, ite_true] + rw [List.filter_reverse, List.reverse_inj] + unfold RangeRead.inRange truncatedRangeRev ConflictRange.keys + rw [List.filter_filter] + apply List.filter_congr + intro k hk + have hSE : ((k : WithTop Key) < resolve db r.end_) ↔ ((k : WithTop Key) < r.end_.key) := + resolve_lt_iff_of_offset_one hend hk + by_cases hlast : last ≤ (k : WithTop Key) + · have hlastK : last ≤ k := WithTop.coe_le_coe.mp hlast + have hbeg : resolve db r.begin ≤ (k : WithTop Key) := le_trans hL hlast + have hdec : decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) = decide ((k : WithTop Key) < r.end_.key) := by + rw [decide_eq_decide] + exact ⟨fun h => hSE.mp h.2, fun h => ⟨hbeg, hSE.mpr h⟩⟩ + rw [decide_eq_true hlastK, Bool.true_and, hdec] + rw [show decide ((last : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.end_.key) = decide ((k : WithTop Key) < r.end_.key) by + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr hlastK, h⟩⟩] + · have hlastK : ¬ last ≤ k := fun h => hlast (WithTop.coe_le_coe.mpr h) + rw [decide_eq_false_iff_not.mpr hlastK, Bool.false_and] + rw [show decide ((last : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.end_.key) = false by + rw [Bool.decide_and, decide_eq_false_iff_not.mpr hlast, Bool.false_and]] + +/-- **The cross-free nonpositive-begin reversed truncated under-end.** A reversed +truncated result at a *nonpositive* begin offset whose begin key lies at or below the +least (last) reported key and whose end is at offset one has the reversed truncated +range `[last reported, end_.key)` as its minimal sound conflict range. This is the +under-end companion of `minimal_topOfLast_of_end_offset_ge_two_rev_nonpos` and the +nonpositive-begin companion of `minimal_truncatedRangeRev_of_offset_one`. -/ +theorem minimal_truncatedRangeRev_of_end_offset_one_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) : + Minimal r res (truncatedRangeRev r (res.keys.getLast hne)) := by + obtain ⟨db₀, hcomp⟩ := hread + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have hbl : (r.begin.key : WithTop Key) ≤ (↑(res.keys.getLast hne) : WithTop Key) := hlo + have hf₁ := completeKeys_filter_ge_eq_end_one_nonpos (db := db₁) hrev hbegin hend hbl + have hf₂ := completeKeys_filter_ge_eq_end_one_nonpos (db := db₂) hrev hbegin hend hbl + rw [compatible_iff_truncated_keys_rev hrev hmore hne, + compatible_iff_truncated_keys_rev (db := db₂) hrev hmore hne] + rw [hf₁, hf₂, hagree] + · intro k hk cr hs + have hkend : (k : WithTop Key) < r.end_.key := hk.2 + have hklast : res.keys.getLast hne ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkhead : k ≤ res.keys.head hne + · exact necessary_of_between_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + (List.head_mem hne) hklast hkhead cr hs + · have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead) + exact necessary_head_to_end_key ⟨db₀, hcomp⟩ hrev hne (by omega) cr hs hheadk hkend + +/-- At a nonpositive begin offset, the reversed truncated range is *the* minimal sound +conflict range up to set equality for a cross-free truncated reversed under-end. -/ +theorem unique_minimal_truncatedRangeRev_of_end_offset_one_nonpos {r : RangeRead} + {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset = 1) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_truncatedRangeRev_of_end_offset_one_nonpos hread hmore hrev hne hbegin + hend hlo) + +/-! +## The crossed nonpositive-begin complete overshoot at begin offset zero + +A forward complete result at `begin.offset = 0` whose `begin.key` lies strictly above the +least reported key (the crossed subcase) and whose end overshoots has the top range from +the least reported key, `[head, ⊤)`, as its minimal sound conflict range. At offset zero +the begin selector resolves onto the greatest stored key strictly below `begin.key`; +compatibility forces that key to be the reported head, so the window is `[head, ⊤)` and +agreement on `[head, ⊤)` fixes the complete keys. Convexity from the empty key is not +available, but the head itself is the lower end; everything at or above the reported last +is necessary by `necessary_of_end_offset_ge_two` and convexity fills the gap between the +head and the last. This is the crossed companion of +`minimal_fullRange_of_nonpos_overshoot_nonempty`. +-/ + +/-- For a forward read whose window ends at the sentinel, if the begin selector resolves +exactly to `last` then the complete keys are the stored keys at or above `last`. -/ +theorem completeKeys_eq_topOfLast_keys {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hE : resolve db r.end_ = (⊤ : WithTop Key)) + (hL : resolve db r.begin = (last : WithTop Key)) : + RangeRead.completeKeys db r = db.keys.filter (fun k => decide (last ≤ k)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev]), hE] + unfold RangeRead.inRange + apply List.filter_congr + intro k _ + rw [hL, decide_eq_decide] + exact ⟨fun h => WithTop.coe_le_coe.mp h.1, + fun h => ⟨WithTop.coe_le_coe.mpr h, WithTop.coe_lt_top k⟩⟩ + +/-- At begin offset zero, with some stored key strictly below `begin.key`, every stored +key strictly below `begin.key` is at or below the begin resolution: the begin selector +targets the greatest stored key below `begin.key`. -/ +theorem resolve_zero_begin_le_of_mem_below {db : DB} {r : RangeRead} + (h0 : r.begin.offset = 0) {b : Key} (hb : b ∈ db.keys) + (hb_lt : (b : WithTop Key) < r.begin.key) : + ∀ a ∈ db.keys, ((a : Key) : WithTop Key) < r.begin.key → + ((a : Key) : WithTop Key) ≤ resolve db r.begin := by + intro a ha ha_lt + set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) with hc + have hcanch : r.begin.anchor db = (c : ℤ) + 0 - 1 := by + unfold Selector.anchor + rw [← hc, h0] + have hcpos : 0 < c := countP_pos_of_mem hb (by rw [decide_eq_true_eq]; exact hb_lt) + have hanc0 : 0 ≤ r.begin.anchor db := by rw [hcanch]; omega + have htoNat : (r.begin.anchor db).toNat = c - 1 := by + rw [hcanch] + have hz : ((c : ℤ) + 0 - 1) = ((c : ℤ) - 1) := by ring + rw [hz, toNat_sub_one (by omega : (1 : ℤ) ≤ (c : ℤ)), Int.toNat_natCast] + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + rw [htoNat] + have hcle : c ≤ db.keys.length := List.countP_le_length + omega + rw [resolve_eq_getElem hanc0 hltlen] + obtain ⟨i, hi, hieq⟩ := List.mem_iff_getElem.mp ha + have hic : i < c := by + rw [hc] + refine lt_countP_of_getElem_lt_top db.sorted hi ?_ + rw [hieq]; exact ha_lt + have hile : i ≤ (r.begin.anchor db).toNat := by rw [htoNat]; omega + rw [← hieq] + exact WithTop.coe_le_coe.mpr ((db.sorted.getElem_le_getElem_iff).mpr hile) + +/-- At begin offset zero, with some stored key strictly below `begin.key`, the begin +resolution is strictly below `begin.key`. -/ +theorem resolve_zero_begin_lt_key {db : DB} {r : RangeRead} + (h0 : r.begin.offset = 0) {b : Key} (hb : b ∈ db.keys) + (hb_lt : (b : WithTop Key) < r.begin.key) : + resolve db r.begin < r.begin.key := by + set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.begin.key)) with hc + have hcanch : r.begin.anchor db = (c : ℤ) + 0 - 1 := by + unfold Selector.anchor + rw [← hc, h0] + have hcpos : 0 < c := countP_pos_of_mem hb (by rw [decide_eq_true_eq]; exact hb_lt) + have hanc0 : 0 ≤ r.begin.anchor db := by rw [hcanch]; omega + have htoNat : (r.begin.anchor db).toNat = c - 1 := by + rw [hcanch] + have hz : ((c : ℤ) + 0 - 1) = ((c : ℤ) - 1) := by ring + rw [hz, toNat_sub_one (by omega : (1 : ℤ) ≤ (c : ℤ)), Int.toNat_natCast] + have hltlen : (r.begin.anchor db).toNat < db.keys.length := by + rw [htoNat] + have hcle : c ≤ db.keys.length := List.countP_le_length + omega + have hltc : (r.begin.anchor db).toNat < c := by rw [htoNat]; omega + rw [resolve_eq_getElem hanc0 hltlen] + exact getElem_lt_of_lt_countP_top db.sorted hltlen (by rw [← hc]; exact hltc) + +/-- **The crossed nonpositive-begin complete overshoot at begin offset zero.** A forward +complete result at `begin.offset = 0` whose `begin.key` lies strictly above the least +reported key and whose end overshoots has the top range from the least reported key, +`[head, ⊤)`, as its minimal sound conflict range. -/ +theorem minimal_topOfHead_of_zero_begin_crossed_overshoot {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topOfLast (res.keys.head hne)) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hreadable : Readable r res := ⟨db₀, hcomp₀⟩ + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (topOfLast (res.keys.head hne)) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp_db.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp_db hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hE : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hcomp_db hend hhi (res.keys.getLast hne) + (List.getLast_mem hne) (fun k hk => hmaxres k (by rw [hres_eq]; exact hk)) + have hhead_stored : res.keys.head hne ∈ db.keys := + RangeRead.compatible_keys_subset hcomp_db hhead_mem + have hagree_keys : db.keys.filter (fun k => decide (res.keys.head hne ≤ k)) = + db'.keys.filter (fun k => decide (res.keys.head hne ≤ k)) := by + have h := hag' + rw [AgreeOn, topOfLast_keys, topOfLast_keys] at h + exact h + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · exact (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.1 + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross + (res.keys.head hne) hhead_stored hcross + have hhead_stored' : res.keys.head hne ∈ db'.keys := by + have hmf : res.keys.head hne ∈ + db'.keys.filter (fun k => decide (res.keys.head hne ≤ k)) := by + rw [← hagree_keys] + exact List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]⟩ + exact List.mem_of_mem_filter hmf + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k => decide (res.keys.head hne ≤ k)) := + completeKeys_eq_topOfLast_keys hrev hE hL + have hres_filter : res.keys = + db.keys.filter (fun k => decide (res.keys.head hne ≤ k)) := + hres_eq.trans hck + have hnoE' : ∀ k ∈ db'.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hkh : res.keys.head hne ≤ k + · have hmf : k ∈ db'.keys.filter (fun k => decide (res.keys.head hne ≤ k)) := + List.mem_filter.mpr ⟨hk, by rw [decide_eq_true_eq]; exact hkh⟩ + rw [← hagree_keys] at hmf + have hkdb : k ∈ db.keys := List.mem_of_mem_filter hmf + have hkres : k ∈ res.keys := by + rw [hres_filter] + exact List.mem_filter.mpr ⟨hkdb, by rw [decide_eq_true_eq]; exact hkh⟩ + exact hhi k hkres + · exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (lt_of_not_ge hkh)) + (le_of_lt (hhi _ hhead_mem)) + have hE' : resolve db' r.end_ = (⊤ : WithTop Key) := by + have hcp : db'.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + db'.keys.length := by + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + exact hnoE' k hk + refine resolve_eq_top ?_ ?_ + · unfold Selector.anchor; rw [hcp]; omega + · rw [anchor_toNat_eq (by omega : 1 ≤ r.end_.offset), hcp]; omega + have hL' : resolve db' r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · by_contra hcon + have hgt : ((res.keys.head hne : Key) : WithTop Key) < resolve db' r.begin := + lt_of_not_ge hcon + have hlt' : resolve db' r.begin < r.begin.key := + resolve_zero_begin_lt_key (db := db') hbegin hhead_stored' hcross + rcases resolve_eq_nil_or_eq_top_or_mem (db := db') (s := r.begin) with + hn | ht | ⟨k, hkdb', hkres⟩ + · rw [hn] at hgt + exact absurd hgt (not_lt.mpr (coe_nil_le ((res.keys.head hne : Key) : WithTop Key))) + · rw [ht] at hlt' + exact absurd hlt' (not_lt.mpr le_top) + · have hkgt : res.keys.head hne < k := by + rw [hkres] at hgt + exact WithTop.coe_lt_coe.mp hgt + have hk_ge : res.keys.head hne ≤ k := le_of_lt hkgt + have hk_in : k ∈ db'.keys.filter (fun k => decide (res.keys.head hne ≤ k)) := + List.mem_filter.mpr ⟨hkdb', by rw [decide_eq_true_eq]; exact hk_ge⟩ + rw [← hagree_keys] at hk_in + have hkdb : k ∈ db.keys := List.mem_of_mem_filter hk_in + have hk_lt_begin : (k : WithTop Key) < r.begin.key := by + rw [hkres] at hlt' + exact hlt' + have hb := resolve_zero_begin_le_of_mem_below (db := db) hbegin hhead_stored + hcross k hkdb hk_lt_begin + rw [hL] at hb + exact absurd hb (not_le.mpr (WithTop.coe_lt_coe.mpr hkgt)) + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored' hcross + (res.keys.head hne) hhead_stored' hcross + have hck' : RangeRead.completeKeys db' r = + db'.keys.filter (fun k => decide (res.keys.head hne ≤ k)) := + completeKeys_eq_topOfLast_keys hrev hE' hL' + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_filter, hagree_keys, ← hck']⟩⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hk + have hheadk : res.keys.head hne ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hreadable (List.head_mem hne) + (List.getLast_mem hne) hheadk hklast + · exact necessary_of_end_offset_ge_two hreadable hrev hmore hne hend hhi + (le_of_lt (lt_of_not_ge hklast)) + +/-- At begin offset zero, the top-from-head range is *the* minimal sound conflict range up +to set equality for a crossed complete overshoot. -/ +theorem unique_minimal_topOfHead_of_zero_begin_crossed_overshoot {r : RangeRead} + {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topOfHead_of_zero_begin_crossed_overshoot hread hrev hmore hne hbegin hcross + hend hhi) + +/-- **Soundness of the top range for a truncated reversed overshoot at a positive +begin offset.** A reversed read with `begin.offset ≥ 1`, `end.offset ≥ 2` and every +reported key below `end_.key` has complete keys equal to a reversal of the top +range's stored keys dropped by `(begin.offset - 1).toNat` +(`completeKeys_eq_topRange_keys_drop_rev`), a function of the top range's key set +alone, so databases agreeing on `[begin.key, ⊤)` have identical complete keys and +hence identical compatibility with every result. -/ +theorem sound_topRange_of_end_offset_ge_two_rev_more {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Sound r res (topRange r) := by + intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (topRange r) db db' → RangeRead.Compatible db r res → + RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp_db hmore + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp_db + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hmax : ∀ k ∈ RangeRead.completeKeys db r, k ≤ res.keys.head hne := + fun k hk => le_head_of_mem_completeKeys_rev hrev hpre hne hk + have hE : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_hhi hcomp_db hend hhi (res.keys.head hne) hhead_mem hmax + have hbk_le_head : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key) := + le_trans (key_le_resolve_of_one_le hbegin) + (RangeRead.mem_completeKeys.mp (hpre.subset hhead_mem)).2.1 + have hbk_lt_end : r.begin.key < r.end_.key := + lt_of_le_of_lt hbk_le_head (hhi _ hhead_mem) + -- No stored key of `db` reaches `end_.key`: a stored key at or above `end_.key` + -- lies strictly above `begin.key`, and the resolved begin sits at or below the + -- reported head, so the key is either inside the window (contradicting the head + -- being its maximum) or below the resolved begin (contradicting `end_.key`). + have hall : ∀ k ∈ db.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_contra hge + have hge' : r.end_.key ≤ ((k : Key) : WithTop Key) := not_lt.mp hge + by_cases hle : resolve db r.begin ≤ ((k : Key) : WithTop Key) + · have hkin : k ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨hk, hle, by rw [hE]; exact WithTop.coe_lt_top k⟩ + exact absurd (lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmax k hkin)) (hhi _ hhead_mem)) + (not_lt.mpr hge') + · have h1 : ((k : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le (lt_of_not_ge hle) (le_of_lt (lt_of_le_of_lt + (RangeRead.mem_completeKeys.mp (hpre.subset hhead_mem)).2.1 (hhi _ hhead_mem))) + exact absurd h1 (not_lt.mpr hge') + have hall' : ∀ k ∈ db'.keys, ((k : Key) : WithTop Key) < r.end_.key := by + intro k hk + by_cases hkb : r.begin.key ≤ ((k : Key) : WithTop Key) + · have hkmem : k ∈ db.keys := + ((agreeOn_iff (topRange r) db db').mp hag' k + (ConflictRange.mem_iff.mpr ⟨hkb, WithTop.coe_lt_top k⟩)).mpr hk + exact hall k hkmem + · exact lt_of_lt_of_le (lt_of_not_ge hkb) (le_of_lt hbk_lt_end) + have hE' : resolve db' r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hall' hend + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db' r := by + rw [completeKeys_eq_topRange_keys_drop_rev (db := db) hrev hbegin hE, + completeKeys_eq_topRange_keys_drop_rev (db := db') hrev hbegin hE', + show (topRange r).keys db = (topRange r).keys db' from hag'] + exact (RangeRead.compatible_congr hck).mp hcomp_db + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Begin-key necessity for a truncated reversed overshoot.** With `begin.offset ≥ +2` and an overshooting end (`end.offset ≥ 2`, every reported key below `end_.key`), +the begin key is necessary. The witness database stores the `begin.offset - 1` keys +the begin selector skips, starting at the begin key, followed by the reported keys +ascending; its complete keys are exactly the reported ones, while erasing the begin +key drops one skipped key and shifts the resolved begin up, so the reported keys are +no longer a prefix. -/ +private theorem necessary_begin_key_rev_top_of_end_offset_ge_two {r : RangeRead} {res : Result} + {v : Key} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = true) + (hlen : 0 < res.keys.length) (hbegin : 2 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hkey : r.begin.key = (↑v : WithTop Key)) : + Necessary r res v := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := List.ne_nil_of_length_pos hlen + set b : ℕ := (r.begin.offset - 1).toNat with hb + have hbpos : 1 ≤ b := by rw [hb]; omega + have hresrev_sort : res.keys.reverse.SortedLT := + (RangeRead.compatible_sortedGT hcomp hrev).reverse + have hlast_le : ∀ y ∈ res.keys.reverse, res.keys.getLast hne ≤ y := by + intro y hy + have hy' : y ∈ res.keys := List.mem_reverse.mp hy + have hge : res.keys.Pairwise (fun a b => a ≥ b) := + (RangeRead.compatible_pairwise hcomp).2 hrev |>.imp (fun h => le_of_lt h) + exact hge.rel_getLast hy' + have hresolve_coe : ∃ k : Key, resolve db₀ r.begin = (↑k : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.begin) with h | h | h + · exact ⟨[], h⟩ + · exfalso + have hnil : RangeRead.completeKeys db₀ r = [] := by + rw [RangeRead.completeKeys, ite_eq_left (by simp [hrev])] + simp only [RangeRead.inRange, h] + rw [List.reverse_eq_nil_iff, List.filter_eq_nil_iff] + intro x _ hx + exact absurd (of_decide_eq_true hx).1 (not_le.mpr (WithTop.coe_lt_top x)) + exact List.IsPrefix.ne_nil (RangeRead.compatible_keys_prefix hcomp) hne hnil + · obtain ⟨k, _, hk⟩ := h; exact ⟨k, hk⟩ + obtain ⟨k, hk⟩ := hresolve_coe + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp).subset (List.getLast_mem hne) + have hk_le_last : k ≤ res.keys.getLast hne := by + have h := (RangeRead.mem_completeKeys.mp hlast_ck).2.1 + rw [hk] at h + exact WithTop.coe_le_coe.mp h + have hchain_b_le_k : chain v b ≤ k := by + have h := chain_le_of_resolve (s := r.begin) (k₀ := v) (k := k) hkey (by omega) hk + rwa [show (r.begin.offset - 1).toNat = b from hb] at h + have hchain_lt_last : ∀ i < b, chain v i < res.keys.getLast hne := + fun i _ => lt_of_lt_of_le (chain_lt_mono (b := v) (by omega)) + (le_trans hchain_b_le_k hk_le_last) + set Fp : List Key := (List.range b).map (chain v) with hFp + have hFplen : Fp.length = b := by rw [hFp, List.length_map, List.length_range] + have hFpsort : Fp.SortedLT := by rw [hFp]; exact sortedLT_map_chain v b + have hFp_lt_last : ∀ x ∈ Fp, x < res.keys.getLast hne := by + intro x hx + rw [hFp, List.mem_map] at hx + obtain ⟨i, hi, rfl⟩ := hx + exact hchain_lt_last i (List.mem_range.mp hi) + have hFp_ge_v : ∀ x ∈ Fp, v ≤ x := by + intro x hx + rw [hFp, List.mem_map] at hx + obtain ⟨i, _, rfl⟩ := hx + exact chain_le v i + have hFp_v : v ∈ Fp := by + rw [hFp, List.mem_map]; exact ⟨0, List.mem_range.mpr (by omega), chain_zero v⟩ + set D : List Key := Fp ++ res.keys.reverse with hD + have hDs : D.SortedLT := by + rw [hD] + refine List.sortedLT_append.mpr ⟨hFpsort, hresrev_sort, ?_⟩ + intro a ha c hc + exact lt_of_lt_of_le (hFp_lt_last a ha) (hlast_le c hc) + let dbw : DB := ⟨D, hDs⟩ + have hD_ge_v : ∀ x ∈ D, v ≤ x := by + intro x hx + rw [hD, List.mem_append] at hx + rcases hx with hx | hx + · exact hFp_ge_v x hx + · exact le_trans (le_of_lt (hFp_lt_last _ hFp_v)) (hlast_le x hx) + have htopw : (topRange r).keys dbw = D := by + unfold topRange ConflictRange.keys + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨by rw [hkey]; exact WithTop.coe_le_coe.mpr (hD_ge_v x hx), WithTop.coe_lt_top x⟩ + have hD_lt_end : ∀ x ∈ D, ((x : Key) : WithTop Key) < r.end_.key := by + intro x hx + rw [hD, List.mem_append] at hx + rcases hx with hx | hx + · exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (hFp_lt_last x hx)) + (le_of_lt (hhi _ (List.getLast_mem hne))) + · exact hhi x (List.mem_reverse.mp hx) + have hEw : resolve dbw r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hD_lt_end hend + have hckw : RangeRead.completeKeys dbw r = res.keys := by + rw [completeKeys_eq_topRange_keys_drop_rev (db := dbw) hrev (by omega) hEw, htopw, hD, + List.drop_left' hFplen, List.reverse_reverse] + have hcompw : RangeRead.Compatible dbw r res := + ⟨hcomp.1, Or.inl ⟨hmore, by rw [hckw], hne⟩⟩ + have herase_top : (topRange r).keys (dbw.erase v) = D.erase v := by + unfold topRange ConflictRange.keys + rw [DB.erase_keys] + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨by rw [hkey]; exact WithTop.coe_le_coe.mpr (hD_ge_v x (List.erase_subset hx)), + WithTop.coe_lt_top x⟩ + have hDerase : D.erase v = Fp.erase v ++ res.keys.reverse := by + rw [hD, List.erase_append_left res.keys.reverse hFp_v] + have hD_erase_lt_end : ∀ x ∈ (dbw.erase v).keys, ((x : Key) : WithTop Key) < r.end_.key := by + rw [DB.erase_keys] + exact fun x hx => hD_lt_end x (List.erase_subset hx) + have hEerase : resolve (dbw.erase v) r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_all_lt hD_erase_lt_end hend + have hnot : ¬ RangeRead.Compatible (dbw.erase v) r res := by + intro hc + have hpre := RangeRead.compatible_keys_prefix hc + have hck_erase : RangeRead.completeKeys (dbw.erase v) r = res.keys.dropLast := by + rw [completeKeys_eq_topRange_keys_drop_rev (db := dbw.erase v) hrev (by omega) hEerase, + herase_top, hDerase, + show (r.begin.offset - 1).toNat = ((r.begin.offset - 1).toNat - 1) + 1 by omega, + ← List.drop_drop] + rw [show ((r.begin.offset - 1).toNat - 1) = (Fp.erase v).length by + rw [List.length_erase_of_mem hFp_v, hFplen, hb]] + rw [List.drop_left' rfl, List.drop_reverse, List.reverse_reverse, ← List.dropLast_eq_take] + rw [hck_erase] at hpre + have hle := hpre.length_le + rw [List.length_dropLast] at hle + omega + intro cr hs + by_contra hvcr + exact hnot ((hs dbw (dbw.erase v) + (ConflictRange.keys_erase_of_not_mem cr dbw hvcr).symm).mp hcompw) + +/-- **The truncated reversed overshoot cell at `begin.offset ≥ 2`.** A reversed +truncated result with `begin.offset ≥ 2`, `end.offset ≥ 2` and every reported key +below `end_.key` has the unbounded top `[begin.key, ⊤)` as its minimal sound +conflict range. The top range is sound (`sound_topRange_of_end_offset_ge_two_rev_more`); +for necessity, the begin key is necessary +(`necessary_begin_key_rev_top_of_end_offset_ge_two`), as is the least reported key, +and convexity fills `[begin.key, head]`, while `necessary_of_end_offset_ge_two_rev` +covers everything at or above the reported head. Unlike the offset-one cell +(`minimal_topOfLast_of_end_offset_ge_two_rev`, value `[last, ⊤)`), the begin selector's +skip here forces the whole interval from the begin key up. -/ +theorem minimal_topRange_of_end_offset_ge_two_rev_more {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hckne : RangeRead.completeKeys db₀ r ≠ [] := + List.IsPrefix.ne_nil (RangeRead.compatible_keys_prefix hcomp) hne + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_topRange_of_end_offset_ge_two_rev_more hmore hrev (by omega) hend hhi, ?_⟩ + intro k hk + have hk' := ConflictRange.mem_iff.mp hk + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil_rev hrev hckne (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hvk : v ≤ k := by + have h1 : r.begin.key ≤ (↑k : WithTop Key) := hk'.1 + rw [hv] at h1 + exact WithTop.coe_le_coe.mp h1 + have hv_nec : Necessary r res v := + necessary_begin_key_rev_top_of_end_offset_ge_two ⟨db₀, hcomp⟩ hrev hmore + (List.length_pos_of_ne_nil hne) hbegin hend hhi hv + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + by_cases hkl : k ≤ res.keys.getLast hne + · exact hv_nec.of_between hlast_nec hvk hkl + · have hkgt : res.keys.getLast hne < k := lt_of_not_ge hkl + by_cases hkh : k ≤ res.keys.head hne + · exact necessary_of_between_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + (List.head_mem hne) (le_of_lt hkgt) hkh + · exact necessary_of_end_offset_ge_two_rev ⟨db₀, hcomp⟩ hrev hne hend hhi + (le_of_lt (lt_of_not_ge hkh)) + +/-- At begin offset at least two and end offset at least two, the top range is *the* +minimal sound conflict range up to set equality for a truncated reversed overshoot. -/ +theorem unique_minimal_topRange_of_end_offset_ge_two_rev_more {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : 2 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_topRange_of_end_offset_ge_two_rev_more hread hmore hrev hbegin hend hhi) + +/-! +### The nonpositive begin offset, nonempty complete result + +At a nonpositive begin offset the begin anchor can clamp, so the resolved begin may land +below `begin.key`, and the empty key can be forced into every sound range. In the +cross-free overshoot region — every reported key below `end_.key` and `begin.key` at or +below the least reported key — the necessary keys are *all* keys: the empty key by +`necessary_nil_of_begin_offset_nonpos` (or its reversed twin), the reported keys and +everything between them by convexity, and every key at or above the least reported key by +the overshoot necessity `necessary_of_end_offset_ge_two` (or its reversed twin). Since +`[⊥, ⊤)` is unconditionally sound (`sound_fullRange`), it is minimal there. +-/ + +/-- **The nonpositive-begin overshoot case, nonempty complete result, cross-free.** A +read with `begin.offset ≤ 0`, `end.offset ≥ 2` whose reported keys all lie below +`end_.key`, `begin.key ≤ head`, and a nonempty complete result has `[⊥, ⊤)` as its +minimal sound conflict range. -/ +theorem minimal_fullRange_of_nonpos_overshoot_nonempty {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_fullRange r res, ?_⟩ + intro k _ _ + by_cases hk_last : res.keys.getLast hne ≤ k + · exact necessary_of_end_offset_ge_two hread hrev hmore hne hend hhi hk_last + · have hklt : k < res.keys.getLast hne := not_le.mp hk_last + by_cases hk_head : ((k : Key) : WithTop Key) < + ((res.keys.head hne : Key) : WithTop Key) + · exact (necessary_nil_of_begin_offset_nonpos hread hrev hne hbegin hlo).of_between + (necessary_of_mem_result hread (List.head_mem hne)) + (nil_le_key k) (le_of_lt (WithTop.coe_lt_coe.mp hk_head)) + · exact necessary_of_between_result hread (List.head_mem hne) + (List.getLast_mem hne) + (WithTop.coe_le_coe.mp (not_lt.mp hk_head)) (le_of_lt hklt) + +/-- **The nonpositive-begin overshoot case, nonempty complete reversed result.** A read +with `begin.offset ≤ 0`, `end.offset ≥ 2` whose reported keys all lie below `end_.key`, +`begin.key ≤ last`, and a nonempty complete reversed result has `[⊥, ⊤)` as its minimal +sound conflict range. The reversal-symmetric twin of +`minimal_fullRange_of_nonpos_overshoot_nonempty`, with `necessary_nil_of_begin_offset_nonpos_rev` +and `necessary_of_end_offset_ge_two_rev` in place of their forward forms and the roles of +head and last exchanged. -/ +theorem minimal_fullRange_of_nonpos_overshoot_nonempty_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hend : 2 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) : + Minimal r res ⟨(([] : Key) : WithTop Key), (⊤ : WithTop Key)⟩ := by + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_fullRange r res, ?_⟩ + intro k _ _ + by_cases hk_last : res.keys.getLast hne ≤ k + · by_cases hk_head : res.keys.head hne ≤ k + · exact necessary_of_end_offset_ge_two_rev hread hrev hne hend hhi hk_head + · exact necessary_of_between_result hread (List.getLast_mem hne) (List.head_mem hne) + hk_last (le_of_lt (not_le.mp hk_head)) + · have hklt : k < res.keys.getLast hne := not_le.mp hk_last + exact (necessary_nil_of_begin_offset_nonpos_rev hread hrev hmore hne hbegin hlo).of_between + (necessary_of_mem_result hread (List.getLast_mem hne)) + (nil_le_key k) (le_of_lt hklt) + +/-! +### The nonpositive begin offset, complete result at end offset at most one + +At a nonpositive begin offset the begin anchor can clamp, so the resolved begin may land +below `begin.key` and the empty key can be forced into every sound range. In the cross-free +region — `begin.key` at or below the reported head — a nonempty *complete* result with +`end_.offset ≤ 1` has `[⊥, end_.key)` as its minimal sound conflict range. The empty key is +forced by `necessary_nil_of_begin_offset_nonpos`, the reported keys and everything between +them by convexity, and every key from the reported last up to (but not including) `end_.key` +by `necessary_last_to_end_key`. Soundness is +`sound_bottomRange_of_nonpos_underEnd_nonempty`: agreement on `[⊥, end_.key)` fixes the +stored keys below `end_.key`, hence both resolved selectors and the complete keys. +-/ + +/-- The keys of the bottom range `[⊥, E)` are the stored keys strictly below `E`. -/ +theorem keys_bottomRange_eq_filter (db : DB) (E : WithTop Key) : + (⟨(([] : Key) : WithTop Key), E⟩ : ConflictRange).keys db = + db.keys.filter (fun (k : Key) => decide ((k : WithTop Key) < E)) := by + unfold ConflictRange.keys + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨WithTop.coe_le_coe.mpr (nil_le_key k), h⟩⟩ + +/-- Counting the stored keys below `w` agrees between `db` and its projection to the keys +below `E`, when `w ≤ E`. -/ +theorem countP_lt_eq_countP_filter {db : DB} {E w : WithTop Key} (hw : w ≤ E) : + db.keys.countP (fun (k : Key) => decide ((k : WithTop Key) < w)) = + (db.keys.filter (fun (k : Key) => decide ((k : WithTop Key) < E))).countP + (fun (k : Key) => decide ((k : WithTop Key) < w)) := by + have hfilter : (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < E))).filter + (fun (k : Key) => decide ((↑k : WithTop Key) < w)) = + db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < w)) := by + rw [List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hkw : (k : WithTop Key) < w + · have hkE : (k : WithTop Key) < E := lt_of_lt_of_le hkw hw + simp [hkw, hkE] + · simp [hkw] + rw [List.countP_eq_length_filter, List.countP_eq_length_filter] + exact (congrArg List.length hfilter).symm + +/-- The key at an index below the count of keys below `E` is unchanged by projecting the +list to the keys below `E`. -/ +theorem getElem?_filter_of_lt_countP {l : List Key} (hs : l.SortedLT) + {E : WithTop Key} {i : ℕ} + (hi : i < l.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E))) : + (l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < E)))[i]? = l[i]? := by + rw [filter_lt_eq_take_countP hs E, List.getElem?_take, ite_eq_left hi] + +/-- A nonpositive-offset selector keyed at or below `E` resolves identically against +databases agreeing on the keys below `E`. -/ +theorem resolve_eq_of_keys_below {db₁ db₂ : DB} {E : WithTop Key} {s : Selector} + (hoff : s.offset ≤ 0) (hskey : s.key ≤ E) + (hb : db₁.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < E)) = + db₂.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < E))) : + resolve db₁ s = resolve db₂ s := by + have hcnt : db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) = + db₂.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) := by + rw [countP_lt_eq_countP_filter (db := db₁) (E := E) hskey, + countP_lt_eq_countP_filter (db := db₂) (E := E) hskey, hb] + have hanchor : s.anchor db₁ = s.anchor db₂ := by + unfold Selector.anchor + rw [hcnt] + have hcE : db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) = + db₂.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := by + rw [List.countP_eq_length_filter, List.countP_eq_length_filter] + exact congrArg List.length hb + have hcount_le₁ : db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) ≤ + db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := by + calc db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) + = (db₁.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < E))).countP + (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) := + countP_lt_eq_countP_filter (db := db₁) (E := E) hskey + _ ≤ (db₁.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < E))).length := + List.countP_le_length + _ = db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := + List.countP_eq_length_filter.symm + rw [resolve_eq_ite, resolve_eq_ite, hanchor] + by_cases hneg : s.anchor db₂ < 0 + · rw [ite_eq_left hneg, ite_eq_left hneg] + · rw [ite_eq_right hneg, ite_eq_right hneg] + have hpos : 0 ≤ s.anchor db₂ := not_lt.mp hneg + have hbase : (s.anchor db₂).toNat < + db₂.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) := + (Int.toNat_lt hpos).mpr (by unfold Selector.anchor; omega) + have ha₁ : (s.anchor db₂).toNat < + db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := by + refine lt_of_lt_of_le hbase ?_ + calc db₂.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) + = db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) := hcnt.symm + _ ≤ db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := hcount_le₁ + have ha₂ : (s.anchor db₂).toNat < + db₂.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := by + refine lt_of_lt_of_le hbase ?_ + calc db₂.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) + = db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) := hcnt.symm + _ ≤ db₁.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := hcount_le₁ + _ = db₂.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < E)) := hcE + rw [← getElem?_filter_of_lt_countP db₁.sorted ha₁, + ← getElem?_filter_of_lt_countP db₂.sorted ha₂, hb] + +/-- For `end_.offset = 1`, a stored key strictly below `resolve end_` is strictly below +`end_.key`: the window ends at the first stored key at or above `end_.key`. -/ +theorem lt_end_key_of_lt_resolve_end_one {db : DB} {r : RangeRead} + (hend : r.end_.offset = 1) {k : Key} (hk : k ∈ db.keys) + (h : (k : WithTop Key) < resolve db r.end_) : + (k : WithTop Key) < r.end_.key := by + set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) with hc + have hanc : r.end_.anchor db = (c : ℤ) := by + unfold Selector.anchor; rw [hc]; omega + by_cases hlt : c < db.keys.length + · have hres : resolve db r.end_ = (((db.keys[c]'hlt : Key)) : WithTop Key) := by + rw [resolve_eq_ite, ite_eq_right (by rw [hanc]; exact not_lt.mpr (Int.natCast_nonneg _)), + hanc, Int.toNat_natCast, List.getElem?_eq_getElem hlt] + rw [hres] at h + by_contra hkE + push Not at hkE + obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hk + have hcj : c ≤ j := by + by_contra hcon + have hjc : j < c := not_le.mp hcon + have hklt := getElem_lt_of_lt_countP_top (w := r.end_.key) db.sorted hj + (by rw [← hc]; exact hjc) + rw [hjx] at hklt + exact absurd hkE (not_le.mpr hklt) + have hle0 : db.keys[c] ≤ db.keys[j] := (db.sorted.getElem_le_getElem_iff).mpr hcj + rw [hjx] at hle0 + exact absurd h (not_lt.mpr (WithTop.coe_le_coe.mpr hle0)) + · have hclen : c = db.keys.length := + le_antisymm (by rw [hc]; exact List.countP_le_length) (not_lt.mp hlt) + have : decide ((k : WithTop Key) < r.end_.key) = true := + (List.countP_eq_length.mp hclen) k hk + exact of_decide_eq_true this + +/-- For `end_.offset = 1`, every stored key strictly below `end_.key` is strictly below +`resolve end_`: the window ends at or past `end_.key`. -/ +theorem lt_resolve_end_of_end_one {db : DB} {r : RangeRead} + (hend : r.end_.offset = 1) {k : Key} (hk : (k : WithTop Key) < r.end_.key) : + (k : WithTop Key) < resolve db r.end_ := by + set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) with hc + have hanc : r.end_.anchor db = (c : ℤ) := by + unfold Selector.anchor; rw [hc]; omega + by_cases hlt : c < db.keys.length + · have hres : resolve db r.end_ = (((db.keys[c]'hlt : Key)) : WithTop Key) := by + rw [resolve_eq_ite, ite_eq_right (by rw [hanc]; exact not_lt.mpr (Int.natCast_nonneg _)), + hanc, Int.toNat_natCast, List.getElem?_eq_getElem hlt] + have hEnetop : r.end_.key ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop, countP_lt_top] at hc + omega + obtain ⟨e, he⟩ := WithTop.ne_top_iff_exists.mp hEnetop + have hcc : c = db.keys.countP (fun k => decide (k < e)) := by + rw [hc, ← he, countP_lt_coe] + have hEc : r.end_.key ≤ (((db.keys[c]'hlt : Key)) : WithTop Key) := by + rw [← he] + exact WithTop.coe_le_coe.mpr (le_getElem_of_countP_le db.sorted (le_of_eq hcc.symm) hlt) + rw [hres] + exact lt_of_lt_of_le hk hEc + · have htop : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_eq_top (by rw [hanc]; exact Int.natCast_nonneg _) + (by rw [hanc, Int.toNat_natCast]; exact not_lt.mp hlt) + rw [htop]; exact WithTop.coe_lt_top k + +/-- For `end_.offset ≤ 0`, a stored key strictly below `resolve end_` is strictly below +`end_.key`: the window ends at a stored key strictly below `end_.key`. -/ +theorem lt_end_key_of_lt_resolve_end_nonpos {db : DB} {r : RangeRead} + (hend : r.end_.offset ≤ 0) {k : Key} (_hk : k ∈ db.keys) + (h : (k : WithTop Key) < resolve db r.end_) : + (k : WithTop Key) < r.end_.key := by + set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) with hc + have hanc : r.end_.anchor db ≤ (c : ℤ) - 1 := by + unfold Selector.anchor; rw [hc]; omega + by_cases hneg : r.end_.anchor db < 0 + · rw [resolve_eq_nil hneg] at h + exact absurd h (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k))) + · have hpos : 0 ≤ r.end_.anchor db := not_lt.mp hneg + have hcpos : 1 ≤ c := by + have hle0 : (r.end_.anchor db).toNat ≤ c - 1 := by + rw [Int.toNat_le, Nat.cast_sub (by omega : 1 ≤ c)]; exact hanc + omega + have hlt : (r.end_.anchor db).toNat ≤ c - 1 := by + rw [Int.toNat_le, Nat.cast_sub hcpos]; exact hanc + have hclen : c ≤ db.keys.length := by rw [hc]; exact List.countP_le_length + have hlen : (r.end_.anchor db).toNat < db.keys.length := by omega + have hic : (r.end_.anchor db).toNat < + db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) := by omega + rw [resolve_eq_getElem hpos hlen] at h + exact lt_trans h (getElem_lt_of_lt_countP_top (w := r.end_.key) db.sorted hlen hic) + +/-- **The complete-key window at `end_.offset = 1`, nonpositive begin offset.** The +complete keys are the stored keys below `end_.key` at or above `resolve begin`. The +end selector reaches at or past `end_.key`, so the window never includes a key at or +above it. -/ +theorem completeKeys_eq_filter_of_end_one {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hend : r.end_.offset = 1) : + RangeRead.completeKeys db r = + (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))).filter + (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key))) := by + have hiff : ∀ k ∈ db.keys, + ((k : WithTop Key) < resolve db r.end_ ↔ (k : WithTop Key) < r.end_.key) := + fun k hk => ⟨lt_end_key_of_lt_resolve_end_one hend hk, lt_resolve_end_of_end_one hend⟩ + unfold RangeRead.completeKeys + rw [ite_eq_right (by simp [hrev])] + unfold RangeRead.inRange + rw [List.filter_filter] + apply List.filter_congr + intro k hk + have hd : decide ((k : WithTop Key) < resolve db r.end_) = + decide ((k : WithTop Key) < r.end_.key) := decide_eq_decide.mpr (hiff k hk) + rw [Bool.decide_and, hd] + +/-- **The complete-key window at `end_.offset ≤ 0`, nonpositive begin offset.** The +complete keys are the stored keys below `end_.key` in `[resolve begin, resolve end)`. +The end selector stops at a stored key strictly below `end_.key`, so the window is a +prefix of the keys below `end_.key`. -/ +theorem completeKeys_eq_filter_of_end_nonpos {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hend : r.end_.offset ≤ 0) : + RangeRead.completeKeys db r = + (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))).filter + (fun (k : Key) => decide (resolve db r.begin ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys + rw [ite_eq_right (by simp [hrev])] + unfold RangeRead.inRange + rw [List.filter_filter] + apply List.filter_congr + intro k hk + by_cases hP : resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_ + · have hE : (k : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_nonpos hend hk hP.2 + simp [hP, hE] + · simp [hP] + +/-- **Soundness of `[⊥, end_.key)` for the nonpositive-begin complete under-end case.** +Agreement on `[⊥, end_.key)` fixes the stored keys below `end_.key`, hence the begin +resolution (by `resolve_eq_of_keys_below`) and the complete keys +(`completeKeys_eq_filter_of_end_one`/`_of_end_nonpos`). -/ +theorem sound_bottomRange_of_nonpos_underEnd_nonempty {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) + (hle : r.begin.key ≤ r.end_.key) : + Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + intro db₁ db₂ hagree + have hb : db₁.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) := by + have h : (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange).keys db₁ = + (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange).keys db₂ := hagree + rwa [keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + have hlo : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below hbegin hle hb + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + by_cases hend1 : r.end_.offset = 1 + · rw [completeKeys_eq_filter_of_end_one hrev hend1, + completeKeys_eq_filter_of_end_one (db := db₂) hrev hend1, hlo, hb] + · have hend0 : r.end_.offset ≤ 0 := by omega + have hV : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_keys_below hend0 (le_refl _) hb + rw [completeKeys_eq_filter_of_end_nonpos hrev hend0, + completeKeys_eq_filter_of_end_nonpos (db := db₂) hrev hend0, hlo, hb, hV] + exact RangeRead.compatible_congr hck + +/-- **The nonpositive-begin complete under-end case, cross-free.** A read with +`begin.offset ≤ 0`, `end_.offset ≤ 1`, `begin.key` at or below the reported head, and a +nonempty complete result has `[⊥, end_.key)` as its minimal sound conflict range. The +empty key is `necessary_nil_of_begin_offset_nonpos`, the reported keys and everything +between them `necessary_of_between_result`, and the keys from the reported last up to +`end_.key` `necessary_last_to_end_key`. -/ +theorem minimal_bottomRange_of_nonpos_underEnd_nonempty {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) : + Minimal r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + obtain ⟨db₀, hcomp⟩ := hread + have hreadable : Readable r res := ⟨db₀, hcomp⟩ + have hck_eq : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨hm, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db₀ r := by + rw [← hck_eq]; exact List.head_mem hne + have hhead_mem : res.keys.head hne ∈ db₀.keys := + (RangeRead.mem_completeKeys.mp hhead_ck).1 + have hhead_hi := (RangeRead.mem_completeKeys.mp hhead_ck).2.2 + have hheadE : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := by + by_cases hend1 : r.end_.offset = 1 + · exact lt_end_key_of_lt_resolve_end_one hend1 hhead_mem hhead_hi + · exact lt_end_key_of_lt_resolve_end_nonpos (by omega) hhead_mem hhead_hi + have hle : r.begin.key ≤ r.end_.key := le_trans hlo (le_of_lt hheadE) + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomRange_of_nonpos_underEnd_nonempty hrev hbegin hend hle, ?_⟩ + intro k _ hk + by_cases hk_last : res.keys.getLast hne ≤ k + · exact fun cr hs => necessary_last_to_end_key hreadable hrev hmore hne hend cr hs hk_last hk + · have hklt : k < res.keys.getLast hne := not_le.mp hk_last + by_cases hk_head : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · exact (necessary_nil_of_begin_offset_nonpos hreadable hrev hne hbegin hlo).of_between + (necessary_of_mem_result hreadable (List.head_mem hne)) (nil_le_key k) + (le_of_lt (WithTop.coe_lt_coe.mp hk_head)) + · exact necessary_of_between_result hreadable (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp (not_lt.mp hk_head)) (le_of_lt hklt) + +/-- **Soundness of `[⊥, end_.key)` for the nonpositive-begin complete under-end case, +reversed.** Reversal only reverses the complete keys, so agreement on `[⊥, end_.key)` +fixes the non-reversed complete keys and hence the reversed ones. -/ +theorem sound_bottomRange_of_nonpos_underEnd_nonempty_rev {r : RangeRead} {res : Result} + (hrev : r.reverse = true) (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) + (hle : r.begin.key ≤ r.end_.key) : + Sound r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + intro db₁ db₂ hagree + have hb : db₁.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + db₂.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) := by + have h : (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange).keys db₁ = + (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange).keys db₂ := hagree + rwa [keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + have hlo : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below hbegin hle hb + have hfw : RangeRead.completeKeys db₁ {r with reverse := false} = + RangeRead.completeKeys db₂ {r with reverse := false} := by + by_cases hend1 : r.end_.offset = 1 + · rw [completeKeys_eq_filter_of_end_one (r := {r with reverse := false}) rfl hend1, + completeKeys_eq_filter_of_end_one (r := {r with reverse := false}) (db := db₂) rfl hend1, + hlo, hb] + · have hend0 : r.end_.offset ≤ 0 := by omega + have hV : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_keys_below hend0 (le_refl _) hb + rw [completeKeys_eq_filter_of_end_nonpos (r := {r with reverse := false}) rfl hend0, + completeKeys_eq_filter_of_end_nonpos (r := {r with reverse := false}) (db := db₂) rfl hend0, + hlo, hb, hV] + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + have h₁ : RangeRead.completeKeys db₁ r = + (RangeRead.completeKeys db₁ {r with reverse := false}).reverse := by + simp [RangeRead.completeKeys, hrev] + have h₂ : RangeRead.completeKeys db₂ r = + (RangeRead.completeKeys db₂ {r with reverse := false}).reverse := by + simp [RangeRead.completeKeys, hrev] + rw [h₁, h₂, hfw] + exact RangeRead.compatible_congr hck + +/-- **The nonpositive-begin complete under-end case, cross-free, reversed.** The +reversal-symmetric twin of `minimal_bottomRange_of_nonpos_underEnd_nonempty`, with +`necessary_nil_of_begin_offset_nonpos_rev` and `necessary_head_to_end_key` in place of +their forward forms and the roles of head and last exchanged. -/ +theorem minimal_bottomRange_of_nonpos_underEnd_nonempty_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = true) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) (hend : r.end_.offset ≤ 1) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) : + Minimal r res ⟨(([] : Key) : WithTop Key), r.end_.key⟩ := by + obtain ⟨db₀, hcomp⟩ := hread + have hreadable : Readable r res := ⟨db₀, hcomp⟩ + have hck_eq : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨hm, heq⟩ + · simp [hmore] at hm + · exact heq + have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r := by + rw [← hck_eq]; exact List.getLast_mem hne + have hlast_mem : res.keys.getLast hne ∈ db₀.keys := + (RangeRead.mem_completeKeys.mp hlast_ck).1 + have hlast_hi := (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hlastE : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := by + by_cases hend1 : r.end_.offset = 1 + · exact lt_end_key_of_lt_resolve_end_one hend1 hlast_mem hlast_hi + · exact lt_end_key_of_lt_resolve_end_nonpos (by omega) hlast_mem hlast_hi + have hle : r.begin.key ≤ r.end_.key := le_trans hlo (le_of_lt hlastE) + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_bottomRange_of_nonpos_underEnd_nonempty_rev hrev hbegin hend hle, ?_⟩ + intro k _ hk + by_cases hk_last : res.keys.getLast hne ≤ k + · by_cases hk_head : k ≤ res.keys.head hne + · exact necessary_of_between_result hreadable (List.getLast_mem hne) (List.head_mem hne) + hk_last hk_head + · have hhead_lt : res.keys.head hne < k := lt_of_not_ge hk_head + exact fun cr hs => necessary_head_to_end_key hreadable hrev hne hend cr hs + (le_of_lt hhead_lt) hk + · have hklt : k < res.keys.getLast hne := not_le.mp hk_last + exact (necessary_nil_of_begin_offset_nonpos_rev hreadable hrev hmore hne hbegin hlo).of_between + (necessary_of_mem_result hreadable (List.getLast_mem hne)) + (nil_le_key k) (le_of_lt hklt) + +/-! +### The crossed nonpositive begin offset, complete result at end offset one + +At `begin.offset = 0` the begin anchor is `c - 1`, one below the count of stored keys under +`begin.key`, so the begin selector lands on the greatest stored key strictly below `begin.key`. +When that key is the reported head — the *crossed* subcase, `begin.key` above the head — the +window begins at the head, and with `end.offset = 1` the end selector stops at the first stored +key at or above `end_.key`, so the complete keys are exactly the stored keys in `[head, +end_.key)`. Unlike the cross-free under-end cell, the empty key is *not* forced (the begin +selector never reaches `⊥`), so the value is `[head, end_.key)` rather than `[⊥, end_.key)`. +This needs `begin.key ≤ end_.key`: otherwise a compatible database can store keys between +`end_.key` and `begin.key` that agreement on `[head, end_.key)` cannot see, and `[head, end_.key)` +is not sound. +-/ + +/-- **The crossed nonpositive-begin complete under-end at `begin.offset = 0`, `end.offset = 1`.** +A forward complete result at `begin.offset = 0` whose `begin.key` lies strictly above the least +reported key, with `end.offset = 1` and `begin.key ≤ end_.key`, has the range from the least +reported key up to `end_.key`, `[head, end_.key)`, as its minimal sound conflict range. -/ +theorem minimal_headToEnd_of_zero_begin_crossed_endOne {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset = 1) (hkey_le : r.begin.key ≤ r.end_.key) : + Minimal r res ⟨((res.keys.head hne : Key) : WithTop Key), r.end_.key⟩ := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hreadable : Readable r res := ⟨db₀, hcomp₀⟩ + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have step : ∀ db db', + AgreeOn ⟨((res.keys.head hne : Key) : WithTop Key), r.end_.key⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp_db.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored : res.keys.head hne ∈ db.keys := + RangeRead.compatible_keys_subset hcomp_db hhead_mem + have hhead_lt_end : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := by + have hmem := RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem) + exact lt_end_key_of_lt_resolve_end_one hend hhead_stored hmem.2.2 + have hfilter : db.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.end_.key)) = + db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.end_.key)) := hag' + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · exact (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.1 + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross + (res.keys.head hne) hhead_stored hcross + have hhead_stored' : res.keys.head hne ∈ db'.keys := by + have hmf : res.keys.head hne ∈ db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.end_.key)) := by + rw [← hfilter] + exact List.mem_filter.mpr + ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hhead_lt_end⟩⟩ + exact List.mem_of_mem_filter hmf + have hL' : resolve db' r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · by_contra hcon + have hgt : ((res.keys.head hne : Key) : WithTop Key) < resolve db' r.begin := + lt_of_not_ge hcon + have hlt' : resolve db' r.begin < r.begin.key := + resolve_zero_begin_lt_key (db := db') hbegin hhead_stored' hcross + rcases resolve_eq_nil_or_eq_top_or_mem (db := db') (s := r.begin) with + hn | ht | ⟨k, hkdb', hkres⟩ + · rw [hn] at hgt + exact absurd hgt (not_lt.mpr (coe_nil_le ((res.keys.head hne : Key) : WithTop Key))) + · rw [ht] at hlt' + exact absurd hlt' (not_lt.mpr le_top) + · have hkgt : res.keys.head hne < k := by + rw [hkres] at hgt + exact WithTop.coe_lt_coe.mp hgt + have hk_lt_begin : (k : WithTop Key) < r.begin.key := by + rw [hkres] at hlt' + exact hlt' + have hk_lt_end : (k : WithTop Key) < r.end_.key := + lt_of_lt_of_le hk_lt_begin hkey_le + have hk_in : k ∈ db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.end_.key)) := + List.mem_filter.mpr ⟨hkdb', by + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (le_of_lt hkgt), hk_lt_end⟩⟩ + rw [← hfilter] at hk_in + have hkdb : k ∈ db.keys := List.mem_of_mem_filter hk_in + have hb := resolve_zero_begin_le_of_mem_below (db := db) hbegin hhead_stored + hcross k hkdb hk_lt_begin + rw [hL] at hb + exact absurd hb (not_le.mpr (WithTop.coe_lt_coe.mpr hkgt)) + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored' hcross + (res.keys.head hne) hhead_stored' hcross + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db' r := by + rw [completeKeys_eq_filter_of_end_one hrev hend, + completeKeys_eq_filter_of_end_one (db := db') hrev hend, hL, hL', + List.filter_filter, List.filter_filter] + simpa only [Bool.decide_and] using hfilter + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_le : res.keys.head hne ≤ k := WithTop.coe_le_coe.mp hlo + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hreadable (List.head_mem hne) (List.getLast_mem hne) + hk_le hklast + · exact fun cr hs => necessary_last_to_end_key hreadable hrev hmore hne (le_of_eq hend) + cr hs (le_of_lt (lt_of_not_ge hklast)) hhi + +/-- At `begin.offset = 0`, the head-to-`end_.key` range is *the* minimal sound conflict range up +to set equality for a crossed complete under-end result at `end.offset = 1`. -/ +theorem unique_minimal_headToEnd_of_zero_begin_crossed_endOne {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset = 1) (hkey_le : r.begin.key ≤ r.end_.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_headToEnd_of_zero_begin_crossed_endOne hread hrev hmore hne hbegin hcross hend + hkey_le) + +/-- **The crossed nonpositive-begin complete long under-end at `begin.offset = 0`, +`end.offset = 1`.** The `end_.key < begin.key` companion of +`minimal_headToEnd_of_zero_begin_crossed_endOne`. A forward +complete result at `begin.offset = 0` whose `begin.key` lies strictly above both the least reported +key and `end_.key` has the range from the least reported key up to `begin.key`, `[head, begin.key)`, +as its minimal sound conflict range. The keys between `end_.key` and `begin.key` are forced: storing +one lifts the begin resolution above the reported head, so the value ends at `begin.key` rather than +`end_.key`. -/ +theorem minimal_headToBeginKey_of_zero_begin_crossed_endOne_long {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset = 1) (hlong : r.end_.key < r.begin.key) : + Minimal r res ⟨((res.keys.head hne : Key) : WithTop Key), r.begin.key⟩ := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hreadable : Readable r res := ⟨db₀, hcomp₀⟩ + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have step : ∀ db db', + AgreeOn ⟨((res.keys.head hne : Key) : WithTop Key), r.begin.key⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp_db.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored : res.keys.head hne ∈ db.keys := + RangeRead.compatible_keys_subset hcomp_db hhead_mem + have hfilter : db.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) = + db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := hag' + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · exact (RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.1 + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross + (res.keys.head hne) hhead_stored hcross + have hhead_stored' : res.keys.head hne ∈ db'.keys := by + have hmf : res.keys.head hne ∈ db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := by + rw [← hfilter] + exact List.mem_filter.mpr + ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩ + exact List.mem_of_mem_filter hmf + have hL' : resolve db' r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · by_contra hcon + have hgt : ((res.keys.head hne : Key) : WithTop Key) < resolve db' r.begin := + lt_of_not_ge hcon + have hlt' : resolve db' r.begin < r.begin.key := + resolve_zero_begin_lt_key (db := db') hbegin hhead_stored' hcross + rcases resolve_eq_nil_or_eq_top_or_mem (db := db') (s := r.begin) with + hn | ht | ⟨k, hkdb', hkres⟩ + · rw [hn] at hgt + exact absurd hgt (not_lt.mpr (coe_nil_le ((res.keys.head hne : Key) : WithTop Key))) + · rw [ht] at hlt' + exact absurd hlt' (not_lt.mpr le_top) + · have hkgt : res.keys.head hne < k := by + rw [hkres] at hgt + exact WithTop.coe_lt_coe.mp hgt + have hk_lt_begin : (k : WithTop Key) < r.begin.key := by + rw [hkres] at hlt' + exact hlt' + have hk_in : k ∈ db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := + List.mem_filter.mpr ⟨hkdb', by + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr (le_of_lt hkgt), hk_lt_begin⟩⟩ + rw [← hfilter] at hk_in + have hkdb : k ∈ db.keys := List.mem_of_mem_filter hk_in + have hb := resolve_zero_begin_le_of_mem_below (db := db) hbegin hhead_stored + hcross k hkdb hk_lt_begin + rw [hL] at hb + exact absurd hb (not_le.mpr (WithTop.coe_lt_coe.mpr hkgt)) + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored' hcross + (res.keys.head hne) hhead_stored' hcross + have hmemP : ∀ k : Key, + (k ∈ db.keys ∧ (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) ↔ + (k ∈ db'.keys ∧ (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) := by + intro k + have h := congrArg (fun l : List Key => k ∈ l) hfilter + exact iff_of_eq (by simpa only [List.mem_filter, decide_eq_true_eq] using h) + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db' r := by + have hckd : RangeRead.completeKeys db r = + (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))).filter + (fun (k : Key) => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key))) := by + rw [completeKeys_eq_filter_of_end_one hrev hend, hL] + rw [hckd, completeKeys_eq_filter_of_end_one (db := db') hrev hend, hL'] + apply eq_of_forall_mem_iff_of_pairwise + · exact db.sorted.pairwise.sublist (List.filter_sublist.trans List.filter_sublist) + · exact db'.sorted.pairwise.sublist (List.filter_sublist.trans List.filter_sublist) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk, hkE⟩, hkh⟩ + exact ⟨⟨((hmemP k).mp ⟨hk, hkh, lt_of_lt_of_le hkE (le_of_lt hlong)⟩).1, hkE⟩, hkh⟩ + · rintro ⟨⟨hk, hkE⟩, hkh⟩ + exact ⟨⟨((hmemP k).mpr ⟨hk, hkh, lt_of_lt_of_le hkE (le_of_lt hlong)⟩).1, hkE⟩, hkh⟩ + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_le : res.keys.head hne ≤ k := WithTop.coe_le_coe.mp hlo + by_cases hk : k ∈ res.keys + · exact necessary_of_mem_result hreadable hk + · have hk_gt : res.keys.head hne < k := + lt_of_le_of_ne hk_le (fun h => hk (h ▸ hhead_mem)) + intro cr hs + by_contra hcr + have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored₀ : res.keys.head hne ∈ db₀.keys := + RangeRead.compatible_keys_subset hcomp₀ hhead_mem + have hL₀ : resolve db₀ r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · exact (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hhead_mem)).2.1 + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored₀ hcross + (res.keys.head hne) hhead_stored₀ hcross + have hk0 : k ∉ db₀.keys := by + intro hkdb + have hle := resolve_zero_begin_le_of_mem_below (db := db₀) hbegin hhead_stored₀ + hcross k hkdb hhi + rw [hL₀] at hle + exact absurd hle (not_le.mpr (WithTop.coe_lt_coe.mpr hk_gt)) + have hagree : AgreeOn cr db₀ (db₀.insert k hk0) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hk0 hcr).symm + have hcomp₁ : RangeRead.Compatible (db₀.insert k hk0) r res := + (hs db₀ _ hagree).mp hcomp₀ + have hk_in : k ∈ (db₀.insert k hk0).keys := DB.mem_insert_self db₀ k hk0 + have hk_le_res : (k : WithTop Key) ≤ resolve (db₀.insert k hk0) r.begin := + resolve_zero_begin_le_of_mem_below hbegin hk_in hhi k hk_in hhi + have hhead_notin : res.keys.head hne ∉ RangeRead.completeKeys (db₀.insert k hk0) r := by + rw [RangeRead.mem_completeKeys] + rintro ⟨-, hlo', -⟩ + exact absurd hlo' (not_le.mpr (lt_of_lt_of_le (WithTop.coe_lt_coe.mpr hk_gt) hk_le_res)) + have hneq : res.keys ≠ RangeRead.completeKeys (db₀.insert k hk0) r := + fun heq => hhead_notin (heq ▸ hhead_mem) + rcases hcomp₁.2 with ⟨hm', _, _⟩ | ⟨hm', heq'⟩ + · rw [hmore] at hm'; exact absurd hm' (by simp) + · exact hneq heq' + +/-- At `begin.offset = 0`, the head-to-`begin.key` range is *the* minimal sound conflict range up +to set equality for a crossed complete under-end result at `end.offset = 1` with `begin.key` +strictly above `end_.key`. -/ +theorem unique_minimal_headToBeginKey_of_zero_begin_crossed_endOne_long {r : RangeRead} + {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset = 1) (hlong : r.end_.key < r.begin.key) : + ∃ cr : ConflictRange, Minimal r res cr ∧ + ∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet := + exists_unique_minimal_of_minimal + (minimal_headToBeginKey_of_zero_begin_crossed_endOne_long hread hrev hmore hne hbegin hcross + hend hlong) + +/-! +## The nonpositive-begin truncated forward cell + +A truncated forward result at a nonpositive begin offset, cross-free (`begin.key` at or +below the reported last) and with the end pinned above the reported keys (`1 ≤ end_.offset` +and every reported key strictly below `end_.key`), has the bottom truncated range +`[⊥, keyAfter (last reported))` as its minimal sound conflict range. The lower end is the +empty key rather than `begin.key`, because the resolved begin can land below `begin.key`; the +empty key is `necessary_nil_of_begin_offset_nonpos`, the reported keys and everything between +them `necessary_of_between_result`, and nothing past the successor of the reported last +because the result reports a prefix. Soundness: agreement on `[⊥, keyAfter last)` fixes the +stored keys at or below the reported last, hence the begin resolution +(`resolve_eq_of_keys_below`) and the complete keys up to the reported last, since +`end_.key ≤ resolve end_`. +-/ + +/-- The bottom truncated range `[⊥, keyAfter last)`: the truncated forward value at a +nonpositive begin offset, where the lower end is the empty key rather than `begin.key`. -/ +def truncatedRangeBottom (last : Key) : ConflictRange := + ⟨(([] : Key) : WithTop Key), (keyAfter last : WithTop Key)⟩ + +/-- **Dropping the end bound from the filtered complete keys.** When the reported last lies +strictly below `resolve end_`, filtering the complete keys at or below `last` is the stored keys +at or above `resolve begin` and at or below `last`: every key at or below `last` is automatically +below `resolve end_`. -/ +theorem completeKeys_filter_le_eq {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hlt : (last : WithTop Key) < resolve db r.end_) : + (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ k ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · have hcp : (RangeRead.completeKeys db r).Pairwise (· < ·) := by + have h := RangeRead.completeKeys_pairwise (db := db) (r := r) + rw [hrev] at h + exact h + exact hcp.sublist List.filter_sublist + · exact db.sorted.pairwise.sublist List.filter_sublist + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + rw [RangeRead.mem_completeKeys] + constructor + · rintro ⟨⟨hk, hb, _⟩, hlast⟩ + exact ⟨hk, hb, hlast⟩ + · rintro ⟨hk, hb, hlast⟩ + exact ⟨⟨hk, hb, lt_of_le_of_lt (WithTop.coe_le_coe.mpr hlast) hlt⟩, hlast⟩ + +/-- **Soundness of `[⊥, keyAfter last)` for the nonpositive-begin truncated forward case.** +Agreement on `[⊥, keyAfter last)` fixes the stored keys at or below the reported last, hence the +begin resolution (`resolve_eq_of_keys_below`) and the complete keys up to the reported last +(`completeKeys_filter_le_eq`), because `end_.key ≤ resolve end_` puts every key at or below the +reported last below `resolve end_`. -/ +theorem sound_truncatedRangeBottom_of_nonpos {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset ≤ 0) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hlo : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key)) + (hge : 1 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Sound r res (truncatedRangeBottom (res.keys.getLast hne)) := by + intro db₁ db₂ hagree + -- Agreement on the bottom truncated range is equality of the stored keys at or below + -- the reported last, since `< keyAfter last` is `≤ last`. + have hbE : db₁.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter (res.keys.getLast hne) : WithTop Key))) = + db₂.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter (res.keys.getLast hne) : WithTop Key))) := by + have h : (truncatedRangeBottom (res.keys.getLast hne)).keys db₁ = + (truncatedRangeBottom (res.keys.getLast hne)).keys db₂ := hagree + unfold truncatedRangeBottom at h + rwa [keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h + have hb : db₁.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + db₂.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + have hc : ∀ db : DB, db.keys.filter + (fun k : Key => decide ((k : WithTop Key) < + (keyAfter (res.keys.getLast hne) : WithTop Key))) = + db.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + intro db + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + rwa [hc db₁, hc db₂] at hbE + -- The begin resolution agrees: `begin.key ≤ last < keyAfter last`. + have hkey_ka : r.begin.key ≤ (keyAfter (res.keys.getLast hne) : WithTop Key) := + le_trans hlo (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt (res.keys.getLast hne)))) + have hres : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_keys_below hbegin hkey_ka hbE + -- Every key at or below the reported last lies below `resolve end_`, since + -- `last < end_.key ≤ resolve end_`. + have hlast_end : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key := + hhi _ (List.getLast_mem hne) + have hlt : ∀ db : DB, ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + fun db => lt_of_lt_of_le hlast_end (key_le_resolve_of_one_le (db := db) (s := r.end_) hge) + have hA : ∀ db : DB, db.keys.filter + (fun k : Key => + decide (resolve db r.begin ≤ (k : WithTop Key) ∧ k ≤ res.keys.getLast hne)) = + (db.keys.filter (fun k : Key => decide (k ≤ res.keys.getLast hne))).filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key))) := by + intro db + rw [List.filter_filter] + apply List.filter_congr + intro k _ + rw [Bool.decide_and] + have hck : (RangeRead.completeKeys db₁ r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = + (RangeRead.completeKeys db₂ r).filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) := by + rw [completeKeys_filter_le_eq (db := db₁) hrev (hlt db₁), + completeKeys_filter_le_eq (db := db₂) hrev (hlt db₂)] + rw [hA db₁, hA db₂, hres, hb] + rw [compatible_iff_truncated_keys hrev hmore hne, + compatible_iff_truncated_keys (db := db₂) hrev hmore hne, hck] + +/-- **The nonpositive-begin truncated forward case, cross-free and end-pinned.** A read with +`begin.offset ≤ 0`, `begin.key` at or below the reported head, `1 ≤ end_.offset` and every +reported key strictly below `end_.key`, and a nonempty truncated forward result has +`[⊥, keyAfter (last reported))` as its minimal sound conflict range. The empty key is +`necessary_nil_of_begin_offset_nonpos`, the reported keys and everything between them +`necessary_of_between_result`, and nothing past the reported last's successor because the result +reports a prefix. -/ +theorem minimal_truncatedRangeBottom_of_nonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset ≤ 0) + (hlo : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hge : 1 ≤ r.end_.offset) + (hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) : + Minimal r res (truncatedRangeBottom (res.keys.getLast hne)) := by + obtain ⟨db₀, hcomp⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hhead_last : res.keys.head hne ≤ res.keys.getLast hne := + (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hlo' : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + le_trans hlo (WithTop.coe_le_coe.mpr hhead_last) + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_truncatedRangeBottom_of_nonpos hrev hbegin hmore hne hlo' hge hhi, ?_⟩ + intro k _ hk + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk) + exact (necessary_nil_of_begin_offset_nonpos ⟨db₀, hcomp⟩ hrev hne hbegin hlo).of_between + (necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne)) (nil_le_key k) hkle + +/-! +## The empty conflict range + +The empty interval has no keys to track. It is the certificate for reads that are +always empty: `FormalConflictRange.Empty` proves it sound and minimal for any +`AlwaysEmpty` read, using the exact database-free characterization developed there. +-/ + +/-- The empty conflict range: no keys, nothing to track. -/ +def emptyRange (r : RangeRead) : ConflictRange := ⟨r.begin.key, r.begin.key⟩ + +/-- The empty range contains no keys. -/ +theorem not_mem_emptyRange (r : RangeRead) (k : Key) : k ∉ emptyRange r := + ConflictRange.not_mem_of_isEmpty (cr := emptyRange r) (le_refl r.begin.key) k + +/-! +## The compact closed form at `begin.offset = 1` + +This section proves the closed form for a complete non-reversed result at +`begin.offset = 1` and a cross-free read `begin.key ≤ end_.key`. Writing +`e = end_.offset` and `t = reportedAtOrAbove end_.key res`, the minimal range is + +* `[begin.key, end_.key)` when `e ≤ 1`; +* `[begin.key, keyAfter (last reported))` when `e = t + 1`; +* `[begin.key, ⊤)` when `t + 1 < e`. + +The key observation is that the complete keys are the stored keys at or above +`begin.key`, cut after the first `(e - 1)` stored keys at or above `end_.key`. +-/ + +/-- Filtering an ascending list at or below a threshold gives its take at the count +of elements at or below the threshold. -/ +theorem filter_le_eq_take_countP {l : List Key} (hs : l.SortedLT) (t : Key) : + l.filter (fun k : Key => decide (k ≤ t)) = + l.take (l.countP (fun k : Key => decide (k ≤ t))) := by + have hkey : (fun k : Key => decide (k ≤ t)) = + (fun k : Key => decide ((k : WithTop Key) < ((keyAfter t : Key) : WithTop Key))) := by + funext k + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + conv_lhs => rw [hkey] + rw [filter_lt_eq_take_countP hs ((keyAfter t : Key) : WithTop Key)] + congr 1 + apply List.countP_congr + intro k _ + simp only [decide_eq_true_eq, WithTop.coe_lt_coe, lt_keyAfter_iff] + +/-- Filtering a strictly increasing list at or below its `n`-th key gives the first +`n + 1` elements. -/ +theorem filter_le_eq_take_succ_of_getElem {l : List Key} (hs : l.SortedLT) + {x : Key} {n : Nat} (hn : n < l.length) (hnx : l[n] = x) : + l.filter (fun k : Key => decide (k ≤ x)) = l.take (n + 1) := by + have hcnt : l.countP (fun k : Key => decide (k ≤ x)) = n + 1 := by + have hlt : l.countP (fun k : Key => decide (k < keyAfter x)) = + l.countP (fun k : Key => decide (k ≤ x)) := by + apply List.countP_congr + intro k _ + rw [decide_eq_true_eq, decide_eq_true_eq, lt_keyAfter_iff] + have hlow : n < l.countP (fun k : Key => decide (k < keyAfter x)) := + lt_countP_of_getElem_lt hs hn (by rw [hnx]; exact keyAfter_self_lt x) + have hhigh : l.countP (fun k : Key => decide (k < keyAfter x)) ≤ n + 1 := by + by_contra hle + have hgt : n + 1 < l.countP (fun k : Key => decide (k < keyAfter x)) := by omega + have hi : n + 1 < l.length := lt_of_lt_of_le hgt (List.countP_le_length) + have hltn := getElem_lt_of_lt_countP hs hi (x := keyAfter x) hgt + have hxlt : x < l[n + 1] := by + rw [← hnx] + exact hs.getElem_lt_getElem_iff.mpr (by omega) + have hxle : l[n + 1] ≤ x := (lt_keyAfter_iff (l[n + 1]) x).mp hltn + exact absurd hxlt (not_lt.mpr hxle) + omega + rw [filter_le_eq_take_countP hs x, hcnt] + +/-- At begin offset one and end offset at least one, the complete keys of a forward +read are the stored keys at or above `begin.key`, truncated after the first +`(end.offset - 1)` stored keys at or above `end_.key`. -/ +theorem completeKeys_eq_filter_take_of_offset_one {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hend : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).take + (min (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db.keys.length + - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key))) := by + set l := db.keys with hl + set p := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.end_.key)) with hcE + set e' := (r.end_.offset - 1).toNat with he' + set d := l.countP (fun (k : Key) => decide ((k : WithTop Key) < resolve db r.end_)) with hd + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hp_anchor : (r.begin.anchor db).toNat = p := by + rw [anchor_toNat_eq (s := r.begin) (by omega), hbegin, hp] + rw [← hl] + simp + have hd_eq : d = min (cE + e') l.length := by + rw [hd, hl, countP_lt_resolve, anchor_toNat_eq (s := r.end_) hend, hcE, he'] + have hA : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) + = l.drop p := by + have hdrop : l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) + = l.drop p := by + rw [hp] + exact filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hLHS : RangeRead.completeKeys db r = (l.drop p).take (d - p) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + dsimp only [RangeRead.inRange] + rw [show l.filter (fun (k : Key) => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) = + l.filter (fun (k : Key) => decide ((k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, + filter_resolve_le_eq_drop (db := db) (s := r.begin), hp_anchor] + have hdrop : (l.drop p).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + rw [filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) p, ← hd, nat_sub_min_right] + rw [hLHS] + rw [← hA] + rw [hd_eq] + +/-- Among a list of keys, the number at or above `E` and the number below `E` +add up to the length. -/ +theorem countP_ge_add_countP_lt {l : List Key} (E : WithTop Key) : + l.countP (fun k : Key => decide (E ≤ (k : WithTop Key))) + + l.countP (fun k : Key => decide ((k : WithTop Key) < E)) = l.length := by + induction l with + | nil => rfl + | cons a rest ih => + by_cases h : ((a : Key) : WithTop Key) < E + · have hge : ¬ E ≤ ((a : Key) : WithTop Key) := not_le.mpr h + rw [List.countP_cons_of_neg (by simpa using hge), + List.countP_cons_of_pos (by simpa using h)] + simp only [List.length_cons] + omega + · have hge : E ≤ ((a : Key) : WithTop Key) := le_of_not_gt h + rw [List.countP_cons_of_pos (by simpa using hge), + List.countP_cons_of_neg (by simpa using h)] + simp only [List.length_cons] + omega + +/-- Among the first `M` elements of an ascending key list, the number at or above +`E` is `M` minus the number below `E` that survive truncation. -/ +theorem countP_ge_take {l : List Key} (hs : l.SortedLT) (E : WithTop Key) + {M : Nat} (hM : M ≤ l.length) : + (l.take M).countP (fun k : Key => decide (E ≤ (k : WithTop Key))) = + M - min M (l.countP (fun k : Key => decide ((k : WithTop Key) < E))) := by + have hsum := countP_ge_add_countP_lt (l := l.take M) E + have hlen : (l.take M).length = M := by + rw [List.length_take, Nat.min_eq_left hM] + rw [hlen] at hsum + have hlt : (l.take M).countP (fun k : Key => decide ((k : WithTop Key) < E)) = + min M (l.countP (fun k : Key => decide ((k : WithTop Key) < E))) := + countP_take_eq_min hs E M + omega + +/-- The number of complete keys at or above `end_.key` is at most +`(end_.offset - 1)`. -/ +theorem completeKeys_count_ge_le {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + set l := db.keys with hl + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + set p := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.end_.key)) with hcE + set e' := (r.end_.offset - 1).toNat with he' + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hp_le_cE : p ≤ cE := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hcross + have hA_eq : A = l.drop p := by + rw [hAdef] + have hdrop : l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) + = l.drop p := by + rw [hp] + exact filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hAs : A.SortedLT := by + rw [hA_eq] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hA_len : A.length = l.length - p := by + rw [hA_eq, List.length_drop] + have hq : A.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = cE - p := by + rw [hA_eq, countP_drop_eq hls r.end_.key p, ← hcE, Nat.min_eq_left hp_le_cE] + -- Rewrite the complete keys into a prefix of `A`. + have hck : RangeRead.completeKeys db r = + A.take (min (cE + e') l.length - p) := by + rw [completeKeys_eq_filter_take_of_offset_one (db := db) hrev hbegin hend] + have hM_le : min (cE + e') l.length - p ≤ A.length := by + rw [hA_len] + exact Nat.sub_le_sub_right (Nat.min_le_right _ _) p + have hcount : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (min (cE + e') l.length - p) - + min (min (cE + e') l.length - p) (cE - p) := by + rw [hck] + have := countP_ge_take hAs r.end_.key hM_le + rwa [hq] at this + have hpL : p ≤ l.length := by rw [hp]; exact List.countP_le_length + have hcEL : cE ≤ l.length := by rw [hcE]; exact List.countP_le_length + by_cases hcase : cE + e' ≤ l.length + · have hmin : min (cE + e') l.length = cE + e' := Nat.min_eq_left hcase + have hM : min (cE + e') l.length - p = (cE - p) + e' := by + rw [hmin] + omega + rw [hcount, hM] + rw [Nat.min_eq_right (Nat.le_add_right (cE - p) e'), Nat.add_sub_cancel_left] + · have hmin : min (cE + e') l.length = l.length := + Nat.min_eq_right (le_of_not_ge hcase) + have hM : min (cE + e') l.length - p = l.length - p := by rw [hmin] + have hqle : cE - p ≤ l.length - p := by omega + rw [hcount, hM, Nat.min_eq_right hqle] + have heq : l.length - p - (cE - p) = l.length - cE := by omega + rw [heq] + omega + +/-- **The count bound at any positive begin offset.** The number of complete keys at +or above `end_.key` is at most `(end_.offset - 1).toNat`, for every `begin.offset ≥ 1`. +The end resolution is the `(end_.offset)`-th stored key at or above `end_.key`, so the +window contains at most the first `(end_.offset - 1)` such keys; a higher begin +resolution only removes more of them. -/ +theorem completeKeys_count_ge_le_general {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + set l := db.keys with hl + set Q := l.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hQ + set e := (r.end_.offset - 1).toNat with he + have hQs : Q.SortedLT := by + rw [hQ] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + -- The complete keys, then their filter at or above `end_.key`. + have hW : RangeRead.completeKeys db r = + l.filter (fun k : Key => + decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + have hcnt : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (Q.filter (fun k : Key => + decide ((k : WithTop Key) < resolve db r.end_))).length := by + rw [hW, List.countP_filter] + rw [← List.countP_eq_length_filter] + rw [hQ] + rw [List.countP_filter] + apply countP_le_countP_of_imp + intro k _ hp + simp only [Bool.and_eq_true] at hp ⊢ + exact ⟨decide_eq_true (of_decide_eq_true hp.2).2, hp.1⟩ + -- Bound the filtered `Q` by `e`. + have hQle : (Q.filter (fun k : Key => + decide ((k : WithTop Key) < resolve db r.end_))).length ≤ e := by + have hres := resolve_eq_filter_getElem? (db := db) (s := r.end_) hend + rw [← hl, ← hQ] at hres + by_cases hQe : e < Q.length + · rw [List.getElem?_eq_getElem hQe] at hres + rw [hres] + have htake : Q.filter (fun k : Key => + decide ((k : WithTop Key) < (↑(Q[e]) : WithTop Key))) = Q.take e := by + rw [filter_lt_eq_take_countP hQs (↑(Q[e]) : WithTop Key)] + congr 1 + rw [show Q.countP (fun (k : Key) => + decide ((k : WithTop Key) < (↑(Q[e]) : WithTop Key))) = + Q.countP (fun k : Key => decide (k < Q[e])) from by + apply List.countP_congr + intro k _ + rw [decide_eq_true_eq, decide_eq_true_eq, WithTop.coe_lt_coe]] + exact countP_lt_eq_of_getElem hQs hQe rfl + rw [htake, List.length_take, Nat.min_eq_left (le_of_lt hQe)] + · rw [List.getElem?_eq_none (le_of_not_gt hQe)] at hres + rw [hres] + have hall : Q.filter (fun k : Key => + decide ((k : WithTop Key) < (⊤ : WithTop Key))) = Q := by + rw [List.filter_eq_self] + intro k _ + simp + rw [hall] + exact le_of_not_gt hQe + exact le_trans hcnt hQle + +/-- **Exact count of complete keys at or above `end_.key`.** For a non-reversed +cross-free read at any positive begin offset, the number of complete keys at or above +`end_.key` is the size of the top window `[end_.key, …)` truncated to `end.offset - 1` +keys, with the begin offset's overshoot past the interval gap removed. Writing +`p = #{keys < begin.key}`, `cE = #{keys < end_.key}` and `b = begin.offset - 1`, the +window begins at stored index `p + b` and the count is +`min e (#stored at or above end_.key) - (b - (cE - p))`. In particular the existing +upper bound `completeKeys_count_ge_le_general` is the same identity capped at `e`. -/ +theorem completeKeys_count_ge_eq_general {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + min ((r.end_.offset - 1).toNat) + (db.keys.length - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) + - ((r.begin.offset - 1).toNat - + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)))) := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hp_le_cE : p ≤ cE := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hcross + have hcE_le : cE ≤ l.length := by rw [hcE]; exact List.countP_le_length + have hanB : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) hbegin, hp, hb, hl] + have hA : l.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key))) = + l.drop (p + b) := by + rw [hl, filter_resolve_le_eq_drop (db := db) (s := r.begin), hanB] + set d := l.countP (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) with hd + have hd_eq : d = min (cE + e) l.length := by + rw [hd, countP_lt_resolve] + rw [anchor_toNat_eq (s := r.end_) hend, hcE, he, hl] + have hck : RangeRead.completeKeys db r = (l.drop (p + b)).take (d - (p + b)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + have hdrop : (l.drop (p + b)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p + b) hls.pairwise) + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) = + l.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (k : WithTop Key))) from by + rw [← hl] + exact List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hA, filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) (p + b), ← hd, nat_sub_min_right] + set A := l.length - cE with hAdef + set N := cE - p with hNdef + have hMle : d - (p + b) ≤ (l.drop (p + b)).length := by + rw [List.length_drop] + have : d ≤ l.length := by rw [hd_eq]; exact Nat.min_le_right _ _ + omega + have hQ : (l.drop (p + b)).countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = N - min b N := by + rw [countP_drop_eq hls r.end_.key (p + b)] + have hmin : min (p + b) cE = p + min b N := by omega + rw [hmin] + omega + rw [hck, countP_ge_take (List.sortedLT_iff_pairwise.mpr + (List.Pairwise.drop (i := p + b) hls.pairwise)) r.end_.key hMle, hQ] + have hAeq : A = l.length - cE := rfl + by_cases hbN : b ≤ N + · have hM : d - (p + b) = min e A + (N - b) := by + rw [hd_eq] + have hmin : min (cE + e) l.length = cE + min e A := by + rw [hAeq]; omega + rw [hmin]; omega + have hQ0 : N - min b N = N - b := by rw [Nat.min_eq_left hbN] + rw [hM, hQ0] + have hQle : N - b ≤ min e A + (N - b) := Nat.le_add_left _ _ + rw [Nat.min_eq_right hQle] + omega + · have hbN' : N < b := Nat.lt_of_not_le hbN + have hM : d - (p + b) = min e A - (b - N) := by + rw [hd_eq] + have hmin : min (cE + e) l.length = cE + min e A := by + rw [hAeq]; omega + rw [hmin]; omega + have hQ0 : N - min b N = 0 := by + rw [Nat.min_eq_right (le_of_lt hbN'), Nat.sub_self] + rw [hM, hQ0] + simp + +/-- **The complete window as a shifted slice above `end_.key`.** When the begin +selector does not start past the top window (`N ≤ b`) and the top window is long +enough (`e ≤ #stored at or above end_.key`), the complete keys are exactly the first +`e` stored keys at or above `end_.key`, with the begin overshoot +`b - N` dropped from the front. This is the list-level form of +`completeKeys_count_ge_eq_general`; it makes the window depend only on the stored keys +at or above `end_.key` and on the two counts. -/ +theorem completeKeys_eq_above_take_drop {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hbN : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) ≤ + (r.begin.offset - 1).toNat) + (hAle : (r.end_.offset - 1).toNat ≤ + db.keys.length - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) : + RangeRead.completeKeys db r = + ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).take + (r.end_.offset - 1).toNat).drop + ((r.begin.offset - 1).toNat - + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)))) := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set N := cE - p with hN + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hp_le_cE : p ≤ cE := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hcross + have hbN' : N ≤ b := by rw [hN]; exact hbN + have hAle' : e ≤ l.length - cE := hAle + have hcE_le : cE ≤ l.length := by rw [hcE]; exact List.countP_le_length + have hanB : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) hbegin, hp, hb, hl] + have hA : l.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key))) = + l.drop (p + b) := by + rw [hl, filter_resolve_le_eq_drop (db := db) (s := r.begin), hanB] + set d := l.countP (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) with hd + have hd_eq : d = min (cE + e) l.length := by + rw [hd, countP_lt_resolve] + rw [anchor_toNat_eq (s := r.end_) hend, hcE, he, hl] + have hck : RangeRead.completeKeys db r = (l.drop (p + b)).take (d - (p + b)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + have hdrop : (l.drop (p + b)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p + b) hls.pairwise) + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) = + l.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (k : WithTop Key))) from by + rw [← hl] + exact List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hA, filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) (p + b), ← hd, nat_sub_min_right] + have hAE : l.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = l.drop cE := by + have h := filter_not_lt_eq_drop_countP hls r.end_.key + rw [show l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = cE from hcE.symm] at h + rw [← h] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hmin : min (cE + e) l.length = cE + e := by omega + have hdrop : l.drop (p + b) = (l.drop cE).drop (b - N) := by + rw [List.drop_drop] + congr 1 + omega + rw [hck, hAE, hdrop, hd_eq, hmin] + rw [show cE + e - (p + b) = e - (b - N) by omega] + rw [← List.drop_take] + +/-- **Nothing lies above the reported last in the finite branch.** If the complete keys +filtered at or below the reported `last` are exactly the reported keys, and the number +of reported keys at or above `end_.key` is `end_.offset - 1`, then the complete keys +*are* the reported keys. Any complete key above `last` would be at or above `end_.key` +and would push the count past the bound `completeKeys_count_ge_le_general`. -/ +private theorem completeKeys_eq_of_filter_le_eq {db : DB} {r : RangeRead} {res : Result} + {last : Key} (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hlast : last = res.keys.getLast hne) + (hres : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) = + res.keys) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + RangeRead.completeKeys db r = res.keys := by + set W := RangeRead.completeKeys db r with hW + set P : Key → Bool := fun k => decide (r.end_.key ≤ (k : WithTop Key)) with hP + set e := (r.end_.offset - 1).toNat with he + have hW_sorted : W.SortedLT := by + rw [hW] + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev])] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hres_sorted : res.keys.SortedLT := by + rw [← hres] + exact List.sortedLT_iff_pairwise.mpr (hW_sorted.pairwise.filter _) + have hepos : 0 < e := by + rw [he] + omega + have hresP : res.keys.countP P = e := by rw [hP]; exact ht + -- The reported last is at or above `end_.key`. + have hlast_ge : r.end_.key ≤ (last : WithTop Key) := by + rw [hlast] + by_contra hnot + have hzero : res.keys.countP P = 0 := by + apply List.countP_eq_zero.mpr + intro k hk + by_contra hpk + have htrue : P k = true := by cases h : P k <;> simp_all + have hk_le_last : k ≤ res.keys.getLast hne := + (hres_sorted.pairwise.imp le_of_lt).rel_getLast hk + exact hnot (le_trans (of_decide_eq_true htrue) (by exact_mod_cast hk_le_last)) + rw [hresP] at hzero + omega + -- If any complete key exceeded `last`, the count bound would be violated. + have hno : ∀ k ∈ W, k ≤ last := by + intro k hk + by_contra hkle + have hlt : last < k := lt_of_not_ge hkle + have hk_ge : r.end_.key ≤ (k : WithTop Key) := + le_trans hlast_ge (by exact_mod_cast le_of_lt hlt) + have hk_fil : k ∈ W.filter P := + List.mem_filter.mpr ⟨hk, by simp [hP, hk_ge]⟩ + have hk_not_le : k ∉ W.filter (fun k : Key => decide (k ≤ last)) := by + intro hmem + exact (not_le.mpr hlt) (of_decide_eq_true (List.mem_filter.mp hmem).2) + have hlen_le : ((W.filter (fun k : Key => decide (k ≤ last))).filter P).length ≤ + (W.filter P).length := + (List.Sublist.filter P List.filter_sublist).length_le + have hneq : ((W.filter (fun k : Key => decide (k ≤ last))).filter P).length ≠ + (W.filter P).length := by + intro heq + have heqlist := (List.Sublist.filter P List.filter_sublist).eq_of_length heq + have : k ∈ (W.filter (fun k : Key => decide (k ≤ last))).filter P := by + rw [heqlist]; exact hk_fil + exact hk_not_le (List.mem_of_mem_filter this) + have hlt_len : ((W.filter (fun k : Key => decide (k ≤ last))).filter P).length < + (W.filter P).length := lt_of_le_of_ne hlen_le hneq + have hWcnt : (W.filter P).length ≤ e := by + rw [← List.countP_eq_length_filter] + rw [hW] + have hbound := completeKeys_count_ge_le_general (db := db) (r := r) hrev hbegin + hcross (by omega) + simpa [hP, he] using hbound + have hrescnt : (List.filter P + (List.filter (fun k : Key => decide (k ≤ last)) W)).length = e := by + rw [hres, ← List.countP_eq_length_filter] + exact hresP + omega + rw [← hres] + exact (List.filter_eq_self.mpr + (fun k hk => decide_eq_true (hno k hk))).symm + +/-- **Finite-branch soundness at any positive begin offset.** In the finite branch of +the complete closed form, agreement on `[begin.key, keyAfter last)` fixes the complete +keys. The key new input is `completeKeys_eq_of_filter_le_eq`: the overshoot condition +is decided by agreement, and the count bound then rules out any complete key above the +reported last. -/ +theorem sound_truncatedRange_of_finite_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + Sound r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + intro db₁ db₂ hagree + have hlast_fil : res.keys.filter + (fun k : Key => decide (k ≤ res.keys.getLast hne)) = res.keys := + filter_le_getLast_eq_of_prefix hsort.pairwise (List.prefix_refl _) hne + have hcomp : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ RangeRead.completeKeys db r = res.keys := by + intro db + constructor + · rintro ⟨hlim, hdisj | ⟨_, heq⟩⟩ + · rw [hmore] at hdisj + exact absurd hdisj.1 (by simp) + · exact ⟨hlim, heq.symm⟩ + · rintro ⟨hlim, heq⟩ + exact ⟨hlim, Or.inr ⟨hmore, heq.symm⟩⟩ + rw [hcomp db₁, hcomp db₂] + have hlr := lt_resolve_end_iff_of_agree (db₁ := db₁) (db₂ := db₂) (r := r) + (last := res.keys.getLast hne) (by omega) hcross hagree + have hdrop : (truncatedRange r (res.keys.getLast hne)).keys db₁ = + (truncatedRange r (res.keys.getLast hne)).keys db₂ := hagree + have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + constructor + · rintro ⟨hlim, hck₁⟩ + refine ⟨hlim, ?_⟩ + have hlt₁ : (res.keys.getLast hne : WithTop Key) < resolve db₁ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [hck₁]; exact hlast_mem)).2.2 + have hlt₂ : (res.keys.getLast hne : WithTop Key) < resolve db₂ r.end_ := hlr.mp hlt₁ + have hF₁ := completeKeys_filter_le_drop (db := db₁) (r := r) hrev hbegin hlt₁ + have hF₂ := completeKeys_filter_le_drop (db := db₂) (r := r) hrev hbegin hlt₂ + rw [hck₁, hlast_fil, hdrop] at hF₁ + exact completeKeys_eq_of_filter_le_eq (db := db₂) hrev hbegin hcross hend hne rfl + (by rw [hF₂, ← hF₁]) ht + · rintro ⟨hlim, hck₂⟩ + refine ⟨hlim, ?_⟩ + have hlt₂ : (res.keys.getLast hne : WithTop Key) < resolve db₂ r.end_ := + (RangeRead.mem_completeKeys.mp (by rw [hck₂]; exact hlast_mem)).2.2 + have hlt₁ : (res.keys.getLast hne : WithTop Key) < resolve db₁ r.end_ := hlr.mpr hlt₂ + have hF₁ := completeKeys_filter_le_drop (db := db₁) (r := r) hrev hbegin hlt₁ + have hF₂ := completeKeys_filter_le_drop (db := db₂) (r := r) hrev hbegin hlt₂ + rw [hck₂, hlast_fil, ← hdrop] at hF₂ + exact completeKeys_eq_of_filter_le_eq (db := db₁) hrev hbegin hcross hend hne rfl + (by rw [hF₁, ← hF₂]) ht + +/-- **Finite-branch minimality at any positive begin offset.** The truncated range +`[begin.key, keyAfter (last reported))` is the minimal sound conflict range in the +finite branch, for every `begin.offset ≥ 1`. Soundness is +`sound_truncatedRange_of_finite_general`; necessity is the begin key and the reported +last, with convexity filling the interval. -/ +theorem minimal_truncatedRange_of_finite_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) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · rw [← hgetLast] + exact sound_truncatedRange_of_finite_general ⟨db₀, hcomp⟩ hrev hmore hne hbegin + hcross hend ht + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- At begin offset one, the keys of the truncated range `[begin.key, keyAfter last)` +are the stored keys at or above `begin.key` followed by those at or below `last`. -/ +theorem truncatedRange_keys_eq_filter_take {db : DB} {r : RangeRead} {last : Key} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hBlelast : r.begin.key ≤ (last : WithTop Key)) : + (truncatedRange r last).keys db = + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).take + (db.keys.countP (fun k : Key => decide (k ≤ last)) - + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key))) := by + set l := db.keys with hl + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + set p := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.begin.key)) with hp + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hA_eq : A = l.drop p := by + rw [hAdef] + have hdrop : l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) + = l.drop p := by + rw [hp] + exact filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hAs : A.SortedLT := by + rw [hA_eq] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + -- The truncated range's keys are `A` filtered at or below `last`. + have hkeys : (truncatedRange r last).keys db = + A.filter (fun k : Key => decide (k ≤ last)) := by + unfold truncatedRange ConflictRange.keys + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + l.filter (fun (k : Key) => decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key)) && + decide (r.begin.key ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, ← hAdef] + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + have hK : A.countP (fun k : Key => decide (k ≤ last)) = + l.countP (fun k : Key => decide (k ≤ last)) - p := by + have hcongr : (fun k : Key => decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + (fun k : Key => decide (k ≤ last)) := by + funext k + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + rw [← hcongr] + rw [hA_eq, countP_drop_eq hls ((keyAfter last : Key) : WithTop Key) p] + have hpK : p ≤ l.countP (fun k : Key => decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) := by + rw [hp] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + have hkl : k ≤ last := + WithTop.coe_le_coe.mp (le_trans (le_of_lt hk) hBlelast) + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkl (keyAfter_self_lt last)) + rw [Nat.min_eq_left hpK] + rw [hkeys, filter_le_eq_take_countP hAs last, hK] + +/-- A positive count yields an element satisfying the predicate. -/ +private theorem exists_of_countP_pos {l : List Key} {p : Key → Bool} + (h : 0 < l.countP p) : ∃ a ∈ l, p a = true := by + by_contra hcon + push_neg at hcon + have hzero : l.countP p = 0 := by + rw [List.countP_eq_zero] + intro a ha + have := hcon a ha + simpa using this + omega + +/-- If a list's take equals another list, then taking as many elements as that +list has also gives it. -/ +private theorem take_of_eq_take {l g : List Key} {K : Nat} (h : l.take K = g) : + l.take g.length = g := by + have hK : g.length ≤ K := by + rw [← h, List.length_take] + exact Nat.min_le_left _ _ + calc l.take g.length = (l.take K).take g.length := by + rw [List.take_take, Nat.min_eq_left hK] + _ = (l.take K).take (l.take K).length := by rw [← h] + _ = l.take K := List.take_length + _ = g := h + +/-- Elements beyond a prefix ending at `last` are strictly greater than `last`. -/ +private theorem lt_of_take_eq_getLast {l g : List Key} (hs : l.SortedLT) {n : Nat} + (h : l.take n = g) (hgne : g ≠ []) {last : Key} (hlast : g.getLast hgne = last) : + ∀ k ∈ l.drop n, last < k := by + intro k hk + have hl : l = l.take n ++ l.drop n := (List.take_append_drop n l).symm + have hpair : (l.take n ++ l.drop n).Pairwise (· < ·) := by + rw [← hl]; exact hs.pairwise + rw [List.pairwise_append] at hpair + obtain ⟨_, _, hcross⟩ := hpair + have hne_take : l.take n ≠ [] := by rw [h]; exact hgne + have hx : (l.take n).getLast hne_take ∈ l.take n := List.getLast_mem hne_take + have hxlast : (l.take n).getLast hne_take = last := by + have h1 : (l.take n).getLast? = some ((l.take n).getLast hne_take) := + List.getLast?_eq_getLast_of_ne_nil hne_take + have h2 : (l.take n).getLast? = some last := by + rw [h, List.getLast?_eq_getLast_of_ne_nil hgne, hlast] + rw [h1] at h2 + exact Option.some.inj h2 + exact hxlast ▸ hcross _ hx k hk + +/-- Elements past a truncation point do not contribute to the count below `E` +when all of them are at or above `E`. -/ +private theorem countP_lt_drop_eq_zero {l : List Key} {E : WithTop Key} {n : Nat} + (hge : ∀ k ∈ l.drop n, E ≤ (k : WithTop Key)) : + l.countP (fun k : Key => decide ((k : WithTop Key) < E)) = + (l.take n).countP (fun k : Key => decide ((k : WithTop Key) < E)) := by + conv_lhs => rw [← List.take_append_drop n l] + rw [List.countP_append] + have hzero : (l.drop n).countP (fun k : Key => decide ((k : WithTop Key) < E)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [decide_eq_true_eq] + exact not_lt.mpr (hge k hk) + rw [hzero, Nat.add_zero] + +/-- A sorted list whose prefix of length `g.length` is `g` has exactly +`g.length - 1` elements strictly below the last element of `g`. -/ +private theorem countP_lt_getLast_of_take {l g : List Key} (hs : l.SortedLT) + (h : l.take g.length = g) (hne : g ≠ []) : + l.countP (fun k : Key => decide (k < g.getLast hne)) = g.length - 1 := by + have hlen : 0 < g.length := List.length_pos_of_ne_nil hne + have hgt : ∀ k ∈ l.drop g.length, g.getLast hne < k := + lt_of_take_eq_getLast hs h hne rfl + rw [← List.take_append_drop g.length l, List.countP_append] + have hzero : (l.drop g.length).countP + (fun k : Key => decide (k < g.getLast hne)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [decide_eq_true_eq] + exact not_lt.mpr (le_of_lt (hgt k hk)) + rw [hzero, Nat.add_zero, h] + have hgs : g.SortedLT := by + rw [← h] + exact List.sortedLT_iff_pairwise.mpr + (List.Pairwise.take (i := g.length) (List.sortedLT_iff_pairwise.mp hs)) + have hlast : g[g.length - 1]'(by omega) = g.getLast hne := by + rw [List.getLast_eq_getElem hne] + rw [← hlast] + exact countP_lt_eq_of_getElem hgs (by omega) rfl + +/-- A stored key at index `j` lies strictly below the resolution of `s` exactly when +`j` is below the clamped anchor index. This is the local form of the same statement +in `Empty`; the two modules do not import each other. -/ +private theorem getElem_lt_resolve_iff' {db : DB} {s : Selector} {j : ℕ} + (hj : j < db.keys.length) : + (↑(db.keys[j]) : WithTop Key) < resolve db s ↔ + j < min (s.anchor db).toNat db.keys.length := by + by_cases hneg : s.anchor db < 0 + · have h0 : (s.anchor db).toNat = 0 := by omega + rw [resolve_eq_nil hneg, h0, Nat.zero_min] + exact iff_of_false + (fun hc => List.not_lt_nil (db.keys[j]) (WithTop.coe_lt_coe.mp hc)) (by omega) + · have h0 : 0 ≤ s.anchor db := by omega + rcases lt_or_ge (s.anchor db).toNat db.keys.length with hlt | hge + · rw [resolve_eq_getElem h0 hlt, WithTop.coe_lt_coe, + db.sorted.getElem_lt_getElem_iff, Nat.min_eq_left (le_of_lt hlt)] + · rw [resolve_eq_top h0 hge, Nat.min_eq_right hge] + exact iff_of_true (WithTop.coe_lt_top _) hj + +/-- **The last reported key of a truncated result lies below the resolved end.** +At `begin.offset = 1`, cross-free, and `end.offset ≥ 1`, any database whose truncated +range stores exactly the reported keys has the last reported key below `resolve end_`. +The count `t` of reported keys at or above `end_.key` is at most `end_.offset - 1`, and +the stored keys below `end_.key` are the `p` keys below `begin.key` together with the +`n - t` reported keys below `end_.key`, so the resolved end lies past the last reported +key. -/ +private theorem truncated_keys_last_lt_resolve {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (hT : (truncatedRange r (res.keys.getLast hne)).keys db = res.keys) + (ht_le : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat) : + ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := by + have hlast_mem_res : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + have hmem : res.keys.getLast hne ∈ + (truncatedRange r (res.keys.getLast hne)).keys db := by + rw [hT]; exact hlast_mem_res + have hBlelast : r.begin.key ≤ (res.keys.getLast hne : WithTop Key) := + (ConflictRange.mem_keys_iff.mp hmem).2.1 + have htake₀ := truncatedRange_keys_eq_filter_take (db := db) (r := r) + (last := res.keys.getLast hne) hrev hbegin hBlelast + rw [hT] at htake₀ + set p := db.keys.countP (fun (k : Key) => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := db.keys.countP (fun (k : Key) => decide ((k : WithTop Key) < r.end_.key)) with hcE + set e' := (r.end_.offset - 1).toNat with he' + set n := res.keys.length with hn + set t := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with ht + have hls : db.keys.SortedLT := db.sorted + have hfilter_drop : db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) + = db.keys.drop p := by + have hdrop : db.keys.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) + = db.keys.drop p := by + rw [hp] + exact filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + rw [hfilter_drop] at htake₀ + let B : List Key := db.keys.drop p + have hBs : B.SortedLT := by + dsimp only [B] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hBtake : B.take n = res.keys := by + dsimp only [B] + simpa [hn] using take_of_eq_take htake₀.symm + have hp_le_cE : p ≤ cE := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hcross + have hBcount : B.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = cE - p := by + dsimp only [B] + rw [countP_drop_eq hls r.end_.key p, ← hcE, Nat.min_eq_left hp_le_cE] + -- The reported keys below `end_.key` number `n - t` and are a sublist of `B`. + have hres_lt : res.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = n - t := by + have hsum := countP_ge_add_countP_lt (l := res.keys) r.end_.key + rw [← ht, ← hn] at hsum + omega + have htake_lt : (B.take n).countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = n - t := by + rw [hBtake, hres_lt] + have hAge : n - t ≤ B.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [← htake_lt] + exact List.Sublist.countP_le (List.take_sublist n B) + -- The rank of the last reported key inside `B` is at most `n - 1`. + have hrank_le : B.countP (fun k : Key => decide (k < res.keys.getLast hne)) ≤ n - 1 := by + have hzero : (B.drop n).countP + (fun k : Key => decide (k < res.keys.getLast hne)) = 0 := by + rw [List.countP_eq_zero] + intro k hk + rw [decide_eq_true_eq] + exact not_lt.mpr (le_of_lt (lt_of_take_eq_getLast hBs hBtake hne rfl k hk)) + have hsplit : B.countP (fun k : Key => decide (k < res.keys.getLast hne)) = + (B.take n).countP (fun k : Key => decide (k < res.keys.getLast hne)) := by + conv_lhs => rw [← List.take_append_drop n B] + rw [List.countP_append, hzero, Nat.add_zero] + rw [hsplit, hBtake] + exact Nat.le_sub_one_of_lt (List.countP_lt_length_iff.mpr + ⟨res.keys.getLast hne, List.getLast_mem hne, by simp⟩) + -- Locate the last reported key as a stored key of `db`. + have hBlast_mem : res.keys.getLast hne ∈ B := by + have hmem_take : res.keys.getLast hne ∈ B.take n := by + rw [hBtake] + exact List.getLast_mem hne + exact List.mem_of_mem_take hmem_take + let i : Nat := B.countP (fun k : Key => decide (k < res.keys.getLast hne)) + have hi_le : i ≤ n - 1 := by + dsimp only [i] + exact hrank_le + have hi_lt : i < B.length := by + dsimp only [i] + exact List.countP_lt_length_iff.mpr + ⟨res.keys.getLast hne, hBlast_mem, by simp⟩ + have hBget : B[i] = res.keys.getLast hne := by + dsimp only [i] + exact getElem_countP_lt hBs hBlast_mem + have hj_lt : p + i < db.keys.length := by + have : p + i < p + B.length := Nat.add_lt_add_left hi_lt p + dsimp only [B] at this + rw [List.length_drop] at this + omega + have hlget : db.keys[p + i] = res.keys.getLast hne := by + have hBget' : (db.keys.drop p)[i]'(by rw [List.length_drop]; omega) = + res.keys.getLast hne := by + dsimp only [B] at hBget + exact hBget + rw [← List.getElem_drop (i := p) (j := i)] + exact hBget' + -- Count the stored keys below `end_.key` from below. + have hcase : cE ≥ p + (n - t) := by + have hc : cE = p + B.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by + rw [hBcount] + omega + rw [hc] + omega + have he_ge_t : t ≤ e' := by + rw [ht, he'] + exact ht_le + have ht_le_n : t ≤ n := by + rw [ht, hn] + exact List.countP_le_length + have hnpos : 0 < n := by rw [hn]; exact List.length_pos_of_ne_nil hne + have hi_lt_n : i < n := Nat.lt_of_le_sub_one hnpos hi_le + have hlt : p + i < cE + e' := by + have h1 : p + i < p + n := Nat.add_lt_add_left hi_lt_n p + have h2 : p + n ≤ cE + t := by omega + have h3 : cE + t ≤ cE + e' := by omega + omega + have hclamp : p + i < min (r.end_.anchor db).toNat db.keys.length := by + have hd : (r.end_.anchor db).toNat = cE + e' := by + rw [anchor_toNat_eq (s := r.end_) hend, hcE, he'] + rw [hd] + exact lt_min hlt hj_lt + have hres := (getElem_lt_resolve_iff' (db := db) (s := r.end_) hj_lt).mpr hclamp + rw [hlget] at hres + exact hres + +/-- **The truncated range at begin offset one, cross-free.** For a non-reversed +truncated result at `begin.offset = 1`, `begin.key ≤ end_.key`, and `end_.offset ≥ 1`, +the minimal sound conflict range is `[begin.key, keyAfter (last reported))`. Unlike the +complete case, no count equality is needed: truncation already fixes the reported +prefix, and the last reported key is always below the resolved end. The `end_.offset = +1` case is the earlier theorem; for `end_.offset ≥ 2` the last-key bound is +`truncated_keys_last_lt_resolve`. -/ +theorem minimal_truncatedRange_of_offset_one_of_more {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) + (hend : 1 ≤ r.end_.offset) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + by_cases h1 : r.end_.offset = 1 + · exact minimal_truncatedRange_of_offset_one hread hmore hrev hbegin h1 + have h2 : 2 ≤ r.end_.offset := by omega + obtain ⟨db₀, hcomp⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp hmore + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have ht_le : 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 + exact le_trans (List.Sublist.countP_le hpre.sublist) + (completeKeys_count_ge_le (db := db₀) hrev hbegin hcross (by omega)) + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have key (db : DB) : RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (truncatedRange r (res.keys.getLast hne)).keys db = res.keys := by + constructor + · intro hc + have hlim := hc.1 + have hlast_mem : res.keys.getLast hne ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hc).subset (List.getLast_mem hne) + have hlt : (res.keys.getLast hne : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hlast_mem).2.2 + have hT := truncatedRange_keys_eq_of_resolve (db := db) hrev hbegin + (keyAfter_le_of_lt hlt) + have hW : (RangeRead.completeKeys db r).Pairwise (· < ·) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + exact RangeRead.inRange_pairwise db _ _ + refine ⟨hlim, ?_⟩ + rw [hT] + exact filter_le_getLast_eq_of_prefix hW (RangeRead.compatible_keys_prefix hc) hne + · rintro ⟨hlim, hT⟩ + have hlt := truncated_keys_last_lt_resolve (db := db) hrev hbegin hcross + (by omega) hne hT ht_le + have hT' := truncatedRange_keys_eq_of_resolve (db := db) hrev hbegin + (keyAfter_le_of_lt hlt) + exact (compatible_iff_truncated_keys (db := db) hrev hmore hne).mpr + ⟨hlim, by rw [← hT', hT]⟩ + rw [key db₁, key db₂, hagree] + · intro k hk cr hs + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- In the finite branch `e = t + 1`, agreement on the truncated range forces the +complete keys: it fixes the stored keys at or above `begin.key` through the last +reported key, and the `(e - 1)`-count then reaches exactly that prefix. -/ +private theorem completeKeys_eq_of_truncated_keys_eq {db : DB} {r : RangeRead} {res : Result} + {last : Key} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) (hlast : last = res.keys.getLast hne) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (r.end_.offset - 1).toNat) + (hT : (truncatedRange r last).keys db = res.keys) : + RangeRead.completeKeys db r = res.keys := by + set l := db.keys with hl + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + set p := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.end_.key)) with hcE + set e' := (r.end_.offset - 1).toNat with he' + set n := res.keys.length with hn + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hA_eq : A = l.drop p := by + rw [hAdef] + have hdrop : l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) + = l.drop p := by + rw [hp] + exact filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hAs : A.SortedLT := by + rw [hA_eq] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hp_le_cE : p ≤ cE := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hcross + have ht' : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = e' := ht + have he'pos : 0 < e' := by omega + have htpos : 0 < res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by rw [ht']; exact he'pos + obtain ⟨k₀, hk₀, hk₀ge⟩ := exists_of_countP_pos htpos + rw [decide_eq_true_eq] at hk₀ge + have hsort_res : res.keys.SortedLT := by + rw [← hT] + exact List.sortedLT_iff_pairwise.mpr (ConflictRange.keys_pairwise (truncatedRange r last) db) + have hlelast : ((k₀ : Key) : WithTop Key) ≤ (last : WithTop Key) := by + rw [hlast] + exact WithTop.coe_le_coe.mpr ((hsort_res.pairwise.imp le_of_lt).rel_getLast hk₀) + have hBlelast : r.begin.key ≤ (last : WithTop Key) := + le_trans hcross (le_trans hk₀ge hlelast) + have hEle : r.end_.key ≤ (last : WithTop Key) := le_trans hk₀ge hlelast + -- The truncated range's keys are a prefix of `A`. + have hT' : A.take (l.countP (fun k : Key => decide (k ≤ last)) - p) = res.keys := by + have := truncatedRange_keys_eq_filter_take (db := db) (r := r) (last := last) + hrev hbegin hBlelast + rw [← hl, ← hAdef] at this + rw [← this, hT] + have hAtake : A.take n = res.keys := take_of_eq_take hT' + have hlen_n : A.length = l.length - p := by rw [hA_eq, List.length_drop] + have hpL : p ≤ l.length := by rw [hp]; exact List.countP_le_length + have hn_le : n ≤ A.length := by + have hmin : min n A.length = n := by + have := congrArg List.length hAtake + rw [List.length_take, hn] at this + exact this + exact hmin ▸ Nat.min_le_right n A.length + have he'_le_n : e' ≤ n := by + rw [← ht', hn] + exact List.countP_le_length + -- Count the keys below `E` in `A`: none occur past the prefix. + have hgt : ∀ k ∈ A.drop n, last < k := + lt_of_take_eq_getLast hAs hAtake hne (hlast.symm) + have hge_drop : ∀ k ∈ A.drop n, r.end_.key ≤ (k : WithTop Key) := by + intro k hk + exact le_trans hEle (WithTop.coe_le_coe.mpr (le_of_lt (hgt k hk))) + have hq : A.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = n - e' := by + rw [countP_lt_drop_eq_zero (l := A) (E := r.end_.key) (n := n) hge_drop, hAtake] + have hsum := countP_ge_add_countP_lt (l := res.keys) r.end_.key + rw [ht'] at hsum + omega + have hq' : A.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = cE - p := by + rw [hA_eq, countP_drop_eq hls r.end_.key p, ← hcE, Nat.min_eq_left hp_le_cE] + have hM : min (cE + e') l.length - p = n := by + have hce : cE + e' = n + p := by omega + have hL : l.length = A.length + p := by rw [hlen_n]; omega + rw [hce, hL, Nat.min_eq_left (by omega)] + omega + -- Assemble. + rw [completeKeys_eq_filter_take_of_offset_one (db := db) hrev hbegin (by omega)] + rw [← hl, ← hAdef] + rw [hM, hAtake] + +/-- Conversely, in the finite branch, once the complete keys are the reported keys, +the truncated range's stored keys are the reported keys as well: the last reported +key is a complete key, so its successor sits at or below the resolved end, and the +only keys at or below the last reported key are the reported keys themselves. -/ +private theorem truncated_keys_eq_of_completeKeys_eq {db : DB} {r : RangeRead} {res : Result} + {last : Key} (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hne : res.keys ≠ []) (hlast : last = res.keys.getLast hne) + (hck : RangeRead.completeKeys db r = res.keys) : + (truncatedRange r last).keys db = res.keys := by + have hmem : last ∈ RangeRead.completeKeys db r := by + rw [hck, hlast]; exact List.getLast_mem hne + have hlt : (last : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hmem).2.2 + have hW : res.keys.Pairwise (· < ·) := by + rw [← hck, RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + exact RangeRead.inRange_pairwise db _ _ + rw [truncatedRange_keys_eq_of_resolve hrev hbegin (keyAfter_le_of_lt hlt), hck, hlast] + exact filter_le_getLast_eq_of_prefix hW (List.prefix_refl _) hne + +/-- **The finite branch of the offset-one closed form.** In the finite branch +`#{\rm reported} \ge \texttt{end.key} = \texttt{end.offset} - 1`, a complete +non-reversed result is compatible with a database exactly when the database stores +the reported keys in the truncated range `[begin.key, keyAfter last)`. Both +directions are the two lemmas above, so agreement on the truncated range fixes +compatibility. -/ +theorem compatible_iff_truncatedRange_keys_finite {db : DB} {r : RangeRead} + {res : Result} (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + {last : Key} (hlast : last = res.keys.getLast hne) : + RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ (truncatedRange r last).keys db = res.keys := by + constructor + · rintro ⟨hlim, hdisj | ⟨_, heq⟩⟩ + · simp [hmore] at hdisj + · exact ⟨hlim, truncated_keys_eq_of_completeKeys_eq hrev hbegin hne hlast heq.symm⟩ + · rintro ⟨hlim, hT⟩ + exact ⟨hlim, Or.inr ⟨hmore, + (completeKeys_eq_of_truncated_keys_eq hrev hbegin hcross hend hne hlast ht hT).symm⟩⟩ + +/-- **Finite-branch soundness.** At `begin.offset = 1`, cross-free, end offset at +least two, and in the finite branch of the closed form, the truncated range is +sound: agreement on `[begin.key, keyAfter last)` fixes the reported keys. -/ +theorem sound_truncatedRange_of_finite {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + Sound r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + intro db₁ db₂ hagree + have key := compatible_iff_truncatedRange_keys_finite (db := db₁) hrev hmore hne + hbegin hcross hend ht (last := res.keys.getLast hne) rfl + have key₂ := compatible_iff_truncatedRange_keys_finite (db := db₂) hrev hmore hne + hbegin hcross hend ht (last := res.keys.getLast hne) rfl + rw [key, key₂, hagree] + +/-- **Finite-branch minimality.** In the finite branch of the offset-one closed +form, the truncated range `[begin.key, keyAfter (last reported))` is the minimal +sound conflict range. Soundness is `sound_truncatedRange_of_finite`; necessity is +the begin key and the reported last, with convexity filling the interval. -/ +theorem minimal_truncatedRange_of_finite {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) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · rw [← hgetLast] + exact sound_truncatedRange_of_finite hrev hmore hne hbegin hcross hend ht + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- **The number of complete keys at or above `end_.key`.** At begin offset one and +end offset at least one, that count is `min (end_.offset - 1) (#stored - #stored +below end_.key)`: the window contains the first `(end.offset - 1)` stored keys at or +above `end_.key` when there are enough of them, and otherwise every stored key at or +above it. -/ +private theorem completeKeys_count_ge_eq_min {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + min ((r.end_.offset - 1).toNat) + (db.keys.length - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) := by + set l := db.keys with hl + set A := l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hAdef + set p := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun (k : Key) => decide ((k : WithTop Key) < r.end_.key)) with hcE + set e' := (r.end_.offset - 1).toNat with he' + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hp_le_cE : p ≤ cE := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk hcross + have hA_eq : A = l.drop p := by + rw [hAdef] + have hdrop : l.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) + = l.drop p := by + rw [hp] + exact filter_not_lt_eq_drop_countP hls r.begin.key + rw [← hdrop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + have hAs : A.SortedLT := by + rw [hA_eq] + exact List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p) hls.pairwise) + have hA_len : A.length = l.length - p := by + rw [hA_eq, List.length_drop] + have hq : A.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = cE - p := by + rw [hA_eq, countP_drop_eq hls r.end_.key p, ← hcE, Nat.min_eq_left hp_le_cE] + have hck : RangeRead.completeKeys db r = + A.take (min (cE + e') l.length - p) := by + rw [completeKeys_eq_filter_take_of_offset_one (db := db) hrev hbegin hend] + have hM_le : min (cE + e') l.length - p ≤ A.length := by + rw [hA_len] + exact Nat.sub_le_sub_right (Nat.min_le_right _ _) p + have hcount : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (min (cE + e') l.length - p) - + min (min (cE + e') l.length - p) (cE - p) := by + rw [hck] + have := countP_ge_take hAs r.end_.key hM_le + rwa [hq] at this + by_cases hcase : cE + e' ≤ l.length + · have hmin : min (cE + e') l.length = cE + e' := Nat.min_eq_left hcase + have hM : min (cE + e') l.length - p = (cE - p) + e' := by + rw [hmin]; omega + rw [hcount, hM, Nat.min_eq_right (Nat.le_add_right (cE - p) e'), Nat.add_sub_cancel_left] + rw [Nat.min_eq_left (by omega)] + · have hmin : min (cE + e') l.length = l.length := + Nat.min_eq_right (le_of_not_ge hcase) + have hM : min (cE + e') l.length - p = l.length - p := by rw [hmin] + have hqle : cE - p ≤ l.length - p := by + have hcEL : cE ≤ l.length := by rw [hcE]; exact List.countP_le_length + omega + rw [hcount, hM, Nat.min_eq_right hqle] + have heq : l.length - p - (cE - p) = l.length - cE := by omega + rw [heq, Nat.min_eq_right (by omega)] + +/-- In the finite branch of the closed form, the complete keys at or above +`end_.key` number exactly `(end_.offset - 1)`, provided the resolved end is a stored +key (not the sentinel). This is the counting form used to identify the sentinel. -/ +private theorem completeKeys_count_ge_eq_of_resolve_ne_top {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (hnotop : resolve db r.end_ ≠ (⊤ : WithTop Key)) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat := by + refine (completeKeys_count_ge_eq_min (db := db) hrev hbegin hcross (by omega)).trans ?_ + have hann := anchor_toNat_eq (db := db) (s := r.end_) (by omega : 1 ≤ r.end_.offset) + have hlt : (r.end_.anchor db).toNat < db.keys.length := by + by_contra hge + exact hnotop (resolve_eq_top (by omega) (le_of_not_gt hge)) + rw [hann] at hlt + have hlt' : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat < db.keys.length := hlt + rw [Nat.min_eq_left (by omega)] + +/-- In the top branch of the offset-one closed form, the resolved end of a compatible +database is the sentinel `⊤`: otherwise the complete window would contain exactly +`(end.offset - 1)` stored keys at or above `end_.key`, contradicting the strict +inequality defining the branch. -/ +private theorem resolve_end_eq_top_of_top_branch {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hnotop + have hck : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have heq := completeKeys_count_ge_eq_of_resolve_ne_top + (db := db) (r := r) hrev hbegin hcross hend hnotop + rw [← hck] at heq + omega + +/-- Inserting a fresh key into a database increases the count of stored keys below a +threshold by one exactly when the inserted key is below it. -/ +private theorem countP_lt_insert {db : DB} {x : Key} (hx : x ∉ db.keys) (E : WithTop Key) : + (db.insert x hx).keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + + (if ((x : Key) : WithTop Key) < E then 1 else 0) := by + rw [DB.insert_keys, List.Perm.countP_eq _ + (List.perm_orderedInsert (· ≤ ·) x db.keys)] + by_cases h : ((x : Key) : WithTop Key) < E + · rw [List.countP_cons_of_pos (by simpa using h), if_pos h] + · rw [List.countP_cons_of_neg (by simpa using h), if_neg h, Nat.add_zero] + +/-- Inserting a fresh key into a database increases its key count by one. -/ +private theorem insert_keys_length {db : DB} {x : Key} (hx : x ∉ db.keys) : + (db.insert x hx).keys.length = db.keys.length + 1 := by + rw [DB.insert_keys] + exact (List.perm_orderedInsert (· ≤ ·) x db.keys).length_eq.trans (by simp) + +/-- In the top branch, inserting a fresh key above the reported last keeps the +resolved end at the sentinel. The strict margin `|db| < #stored below end_.key + +(end.offset - 1)` survives the insertion, so the anchor still lands past every key. -/ +theorem resolve_end_eq_top_of_insert_top {db₀ : DB} {r : RangeRead} {x : Key} + (hx0 : x ∉ db₀.keys) + (hL_lt : db₀.keys.length < + db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) + (hend : 2 ≤ r.end_.offset) : + resolve (db₀.insert x hx0) r.end_ = (⊤ : WithTop Key) := by + refine resolve_eq_top (anchor_nonneg_of_one_le (by omega)) ?_ + rw [anchor_toNat_eq (s := r.end_) (by omega), countP_lt_insert hx0 r.end_.key] + rw [insert_keys_length hx0] + by_cases h : ((x : Key) : WithTop Key) < r.end_.key + · rw [if_pos h]; omega + · rw [if_neg h]; omega + +/-- **Top-branch necessity above the reported last.** In the top branch of the +offset-one closed form, every key at or above the last reported key is necessary. +The compatible database's window ends at the sentinel, so all its stored keys lie at +or below the reported last. Inserting a fresh key above the last keeps the sentinel +end and adds the key to the complete window, breaking the complete result. -/ +private theorem necessary_of_top_branch {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + {x : Key} (hlo : res.keys.getLast hne ≤ x) : + Necessary r res x := by + obtain ⟨db₀, hcomp⟩ := hread + intro cr hs + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hcomp⟩ hx cr hs + · by_contra hcr + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hE₀ : resolve db₀ r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_top_branch hcomp hmore hrev hne hbegin hcross hend ht + -- The strict margin `|db| < cE + e'` supplied by the top branch. + have hL_lt : db₀.keys.length < + db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + have hcount := completeKeys_count_ge_eq_min (db := db₀) hrev hbegin hcross (by omega) + rw [← hres] at hcount + have hmin : min ((r.end_.offset - 1).toNat) + (db₀.keys.length - db₀.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) < + (r.end_.offset - 1).toNat := by rw [← hcount]; exact ht + have hcEL : db₀.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) ≤ db₀.keys.length := + List.countP_le_length + have hm : db₀.keys.length - db₀.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) < + (r.end_.offset - 1).toNat := by + by_contra hge + rw [Nat.min_eq_left (le_of_not_gt hge)] at hmin + exact absurd hmin (lt_irrefl _) + omega + -- Every stored key lies at or below the reported last, hence at or below `x`. + have hckTop : RangeRead.completeKeys db₀ r = (topRange r).keys db₀ := + completeKeys_eq_topRange_keys hrev hbegin hE₀ + have hhead : r.begin.key ≤ ((res.keys.head hne : Key) : WithTop Key) := + begin_key_le_head_of_one_le_offset ⟨db₀, hcomp⟩ hne (by omega) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (by rw [← hres]; exact hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hBle_last : v ≤ res.keys.getLast hne := by + rw [hv] at hhead + exact le_trans (WithTop.coe_le_coe.mp hhead) + ((hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)) + have hall : ∀ k ∈ db₀.keys, k ≤ x := by + intro k hk + by_cases hbk : r.begin.key ≤ ((k : Key) : WithTop Key) + · have hkmem : k ∈ (topRange r).keys db₀ := + ConflictRange.mem_keys_iff.mpr ⟨hk, hbk, WithTop.coe_lt_top k⟩ + have hkres : k ∈ res.keys := by rw [hres, hckTop]; exact hkmem + exact le_trans (hmaxres k hkres) hlo + · have hklt : k < v := by + have h : ¬ (v ≤ k) := fun hle => hbk (by rw [hv]; exact WithTop.coe_le_coe.mpr hle) + exact lt_of_not_ge h + exact le_trans (le_of_lt hklt) (le_trans hBle_last hlo) + have hx0 : x ∉ db₀.keys := by + intro hxm + by_cases hbk : r.begin.key ≤ ((x : Key) : WithTop Key) + · apply hx + rw [hres, hckTop] + exact ConflictRange.mem_keys_iff.mpr ⟨hxm, hbk, WithTop.coe_lt_top x⟩ + · have hxlt : x < v := by + have h : ¬ (v ≤ x) := fun hle => hbk (by rw [hv]; exact WithTop.coe_le_coe.mpr hle) + exact lt_of_not_ge h + exact absurd (lt_of_lt_of_le hxlt (le_trans hBle_last hlo)) + (not_lt.mpr (le_refl x)) + have hEins : resolve (db₀.insert x hx0) r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_insert_top hx0 hL_lt hend + exact absurd (mem_result_of_insert hcomp hs hcr hmore hne hx0 + (fun k hk => WithTop.coe_le_coe.mpr (hall k hk)) hEins) hx + +/-- **Top-branch minimality.** In the top branch of the offset-one closed form, the +unbounded top range `[begin.key, ⊤)` is the minimal sound conflict range. Soundness is +`sound_topRange_of_one_le_le`; keys up to the reported last are necessary by the begin +key, the reported last, and convexity, while keys above it are +`necessary_of_top_branch`. -/ +theorem minimal_topRange_of_top_branch {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = false) + (hrev : r.reverse = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (by rw [← hres]; exact hne) (by omega) + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_topRange_of_one_le_le (by omega) (by omega) hcross + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + by_cases hkle : k ≤ res.keys.getLast hne + · have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) (by omega) hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + · exact necessary_of_top_branch ⟨db₀, hcomp⟩ hrev hmore hne hbegin hcross hend ht + (le_of_lt (lt_of_not_ge hkle)) cr hs + +/-- **The compact closed form at begin offset one.** For a complete, nonempty, +non-reversed read with `begin.offset = 1` and `begin.key ≤ end_.key`, the minimal +conflict range is + +* `[begin.key, end_.key)` when `end_.offset ≤ 1`; +* `[begin.key, keyAfter (last reported))` when the number `t` of reported keys at or + above `end_.key` equals `end_.offset - 1`; +* `[begin.key, ⊤)` when `t < end_.offset - 1`. + +The three branches are `minimal_canonicalRange_of_end_offset_le_one_general`, +`minimal_truncatedRange_of_finite`, and `minimal_topRange_of_top_branch`. -/ +def closedFormOffsetOne (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 1 then canonicalRange r + else + let t := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + let e := (r.end_.offset - 1).toNat + if t = e then truncatedRange r (res.keys.getLastD ([] : Key)) else topRange r + +theorem minimal_closedForm_of_offset_one {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) : + Minimal r res (closedFormOffsetOne r res) := by + unfold closedFormOffsetOne + by_cases hend1 : r.end_.offset ≤ 1 + · rw [if_pos hend1] + exact minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne (by omega) hend1 + · rw [if_neg hend1] + have hend : 2 ≤ r.end_.offset := by omega + by_cases ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat + · rw [if_pos ht] + exact minimal_truncatedRange_of_finite hread hmore hne hrev hbegin hcross hend ht + · rw [if_neg ht] + obtain ⟨db₀, hcomp⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hle : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + rw [hres] + exact completeKeys_count_ge_le hrev hbegin hcross (by omega) + exact minimal_topRange_of_top_branch ⟨db₀, hcomp⟩ hmore hrev hne hbegin hcross hend + (by omega) + +/-- `getLastD` of a reversed list is the `headD` of the original. -/ +theorem getLastD_reverse {α : Type*} (l : List α) (d : α) : + l.reverse.getLastD d = l.headD d := by + rw [List.getLastD_eq_getLast?, List.getLast?_reverse, List.headD_eq_head?] + +/-- **The reversed closed form at begin offset one.** A complete reversed result at +`begin.offset = 1` and `begin.key ≤ end_.key` reports from the top of the window +downward, so the side pinned by a truncated window is the result's *head*. Writing +`t = #{reported keys ≥ end_.key}` and `e = (end_.offset - 1).toNat`, the minimal range is +`[begin.key, end_.key)` when `end_.offset ≤ 1`, `[begin.key, keyAfter (head reported))` +when `t = e`, and `[begin.key, ⊤)` when `t < e`. -/ +def closedFormOffsetOneRev (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 1 then canonicalRange r + else + let t := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + let e := (r.end_.offset - 1).toNat + if t = e then truncatedRange r (res.keys.headD ([] : Key)) else topRange r + +/-- The reversed closed form is minimal on the offset-one cross-free complete region. +Flipping the traversal order and reversing the reported keys turns it into the forward +closed form, whose head is the reversed result's last; `minimal_flip` transfers +minimality back. -/ +theorem minimal_closedForm_of_offset_one_rev {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) : + Minimal r res (closedFormOffsetOneRev r res) := by + 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' (closedFormOffsetOne r' res') := + minimal_closedForm_of_offset_one hread' rfl hne' rfl hbegin hcross + have hflip : Minimal r' res' (closedFormOffsetOne r' res') ↔ + Minimal r res (closedFormOffsetOne r' res') := by + simpa [r', res'] using + (minimal_flip (r := r) (res := res) + (cr := closedFormOffsetOne r' res') (b := true) hrev hmore) + have hformula : closedFormOffsetOne r' res' = closedFormOffsetOneRev r res := by + unfold closedFormOffsetOne closedFormOffsetOneRev + dsimp only [r', res'] + rw [List.countP_reverse, getLastD_reverse] + rfl + simpa [hformula] using hflip.mp hmin' + +/-- **The traversal-aware compact closed form at begin offset one.** One rule covers +both traversal orders of a complete result: use the forward form when the read is not +reversed and the reversed form when it is. -/ +def closedFormOffsetOneAny (r : RangeRead) (res : Result) : ConflictRange := + if r.reverse then closedFormOffsetOneRev r res else closedFormOffsetOne r res + +/-- The traversal-aware closed form is minimal on the offset-one cross-free complete +region, for either traversal order. -/ +theorem minimal_closedForm_of_offset_one_any {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) : + Minimal r res (closedFormOffsetOneAny r res) := by + unfold closedFormOffsetOneAny + by_cases h : r.reverse = true + · rw [if_pos h] + exact minimal_closedForm_of_offset_one_rev hread hmore hne h hbegin hcross + · rw [if_neg h] + have hf : r.reverse = false := by + cases hr : r.reverse <;> simp_all + exact minimal_closedForm_of_offset_one hread hmore hne hf hbegin hcross + +/-- When `end_.offset ≤ 1`, the traversal-aware closed form reduces to the canonical +range and is minimal for every positive begin offset, crossed or not, in either +traversal. This widens the certified region of the formula beyond the offset-one +cross-free hypotheses on that half-plane. -/ +theorem minimal_closedForm_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) : + Minimal r res (closedFormOffsetOneAny r res) := by + unfold closedFormOffsetOneAny + by_cases h : r.reverse = true + · rw [if_pos h] + unfold closedFormOffsetOneRev + rw [if_pos hend] + exact minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend + · rw [if_neg h] + unfold closedFormOffsetOne + rw [if_pos hend] + exact minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend + +/-- **The overshoot region whose reported keys lie below `end_.key`.** For a complete +result at any positive begin offset and `end_.offset ≥ 2`, if every reported key is +strictly below `end_.key` then both forms of the closed rule return the top range, and +it is minimal in either traversal. -/ +theorem minimal_closedForm_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) : + Minimal r res (closedFormOffsetOneAny r res) := by + have ht0 : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = 0 := by + rw [List.countP_eq_zero] + intro k hk hge + rw [decide_eq_true_eq] at hge + exact absurd hge (not_le.mpr (hhi k hk)) + have hepos : 0 < (r.end_.offset - 1).toNat := by omega + unfold closedFormOffsetOneAny + by_cases h : r.reverse = true + · rw [if_pos h] + unfold closedFormOffsetOneRev + rw [if_neg (by omega : ¬ r.end_.offset ≤ 1), ht0, + if_neg (by omega : ¬ (0 = (r.end_.offset - 1).toNat))] + exact minimal_topRange_of_end_offset_ge_two_rev_general hread hmore h hne hbegin hend hhi + · rw [if_neg h] + unfold closedFormOffsetOne + rw [if_neg (by omega : ¬ r.end_.offset ≤ 1), ht0, + if_neg (by omega : ¬ (0 = (r.end_.offset - 1).toNat))] + have hf : r.reverse = false := by + cases hr : r.reverse <;> simp_all + exact minimal_topRange_of_end_offset_ge_two_general hread hmore hf hne hbegin hend hhi + +/-- **The complete-result closed form at any positive begin offset, on the automatic +region.** For a non-reversed complete result with `begin.offset ≥ 1` and +`begin.key ≤ end_.key`, the three-branch rule is minimal whenever `end.offset ≤ 1`, +the finite branch `t = end.offset - 1` holds, or every reported key lies below +`end_.key`. The new ingredient is the finite branch at arbitrary begin offset +(`minimal_truncatedRange_of_finite_general`); the below-end case is +`minimal_closedForm_of_reported_below`. -/ +theorem minimal_closedForm_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)) : + Minimal r res (closedFormOffsetOne r res) := by + unfold closedFormOffsetOne + by_cases hend1 : r.end_.offset ≤ 1 + · rw [if_pos hend1] + exact minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend1 + · rw [if_neg hend1] + have hend : 2 ≤ r.end_.offset := by omega + by_cases ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat + · rw [if_pos ht] + exact minimal_truncatedRange_of_finite_general hread hmore hne hrev hbegin hcross + hend ht + · rw [if_neg ht] + have hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by + rcases hcase with h | h | h + · exact absurd h hend1 + · exact absurd h ht + · exact h + have h := minimal_closedForm_of_reported_below hread hmore hne hbegin hend hhi + have h' : Minimal r res (topRange r) := by + unfold closedFormOffsetOneAny at h + rw [if_neg (by simp [hrev])] at h + unfold closedFormOffsetOne at h + rw [if_neg hend1] at h + rw [if_neg (by + intro hc + exact ht (by rw [hc]))] at h + exact h + exact h' + +/-- **The truncated-result closed form at begin offset one.** For a non-reversed +truncation with `begin.offset = 1` and `begin.key ≤ end_.key`, the minimal range is +`[begin.key, end.key)` when `end.offset ≤ 0` (the complete window has already ended), +and `[begin.key, keyAfter (last reported))` when `end.offset ≥ 1`. -/ +def closedFormOffsetOneTruncated (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 0 then canonicalRange r + else truncatedRange r (res.keys.getLastD ([] : Key)) + +/-- The truncated closed form is minimal on the offset-one cross-free truncated region. -/ +theorem minimal_closedForm_of_offset_one_truncated {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) : + Minimal r res (closedFormOffsetOneTruncated r res) := by + unfold closedFormOffsetOneTruncated + by_cases hle : r.end_.offset ≤ 0 + · rw [if_pos hle] + exact minimal_canonicalRange_of_truncated_end_offset_nonpos hread hmore hrev + (by omega) hle + · rw [if_neg hle] + exact minimal_truncatedRange_of_offset_one_of_more hread hmore hrev hbegin hcross + (by omega) + +/-- **The total cross-free forward-truncated rule.** For a non-reversed truncated +result with `begin.offset ≥ 1` and `begin.key ≤ end_.key`, the minimal range is +`[begin.key, end.key)` when `end.offset ≤ 0` and `[begin.key, keyAfter (last +reported))` for every positive end offset. There is no residual cell in the cross-free +forward-truncated region. -/ +def closedFormTruncatedGeneral (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 0 then canonicalRange r + else truncatedRange r (res.keys.getLastD ([] : Key)) + +/-- The total forward-truncated rule is minimal on the whole cross-free truncated +region. -/ +theorem minimal_closedForm_of_truncated_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) : + Minimal r res (closedFormTruncatedGeneral r res) := by + unfold closedFormTruncatedGeneral + by_cases hle : r.end_.offset ≤ 0 + · rw [if_pos hle] + exact minimal_canonicalRange_of_truncated_end_offset_nonpos hread hmore hrev hbegin hle + · rw [if_neg hle] + exact minimal_truncatedRange_of_more_general hread hmore hrev hbegin (by omega) hcross + +/-- **A compact closed form for every result at begin offset one.** For a non-reversed +read with `begin.offset = 1` and `begin.key ≤ end_.key`, the minimal conflict range is +read off from the result in one step: use the truncated rule when `more = true` and the +complete three-branch rule otherwise. -/ +def closedFormOffsetOneResult (r : RangeRead) (res : Result) : ConflictRange := + if res.more = true then closedFormOffsetOneTruncated r res + else closedFormOffsetOne r res + +/-- The result-aware closed form is minimal for every readable result at begin offset +one, truncated or complete, on the cross-free region. -/ +theorem minimal_closedForm_of_offset_one_result {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) : + Minimal r res (closedFormOffsetOneResult r res) := by + unfold closedFormOffsetOneResult + by_cases hmore : res.more = true + · rw [if_pos hmore] + exact minimal_closedForm_of_offset_one_truncated hread hmore hrev hbegin hcross + · rw [if_neg hmore] + have hfalse : res.more = false := by + cases hm : res.more <;> simp_all + exact minimal_closedForm_of_offset_one hread hfalse hne hrev hbegin hcross + +/-- **A compact reversed truncated rule at begin offset one.** When the end offset is +at most one, use `[last reported, end.key)`; when the end overshoots and every reported +key lies below `end.key`, use `[last reported, ⊤)`. The rule is stated with the +disjunction as an explicit hypothesis because the remaining reversed truncated +cell---`end.offset ≥ 2` with a reported key at or above `end.key`---is not certified by +this formula. -/ +def closedFormOffsetOneTruncatedRev (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.offset ≤ 1 then truncatedRangeRev r (res.keys.getLastD ([] : Key)) + else topOfLast (res.keys.getLastD ([] : Key)) + +/-- The reversed truncated rule is minimal on the two certified reversed cells. -/ +theorem minimal_closedForm_of_offset_one_truncated_rev {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)) : + Minimal r res (closedFormOffsetOneTruncatedRev r res) := by + unfold closedFormOffsetOneTruncatedRev + rcases hcase with hle | ⟨hge, hhi⟩ + · rw [if_pos hle] + by_cases h1 : r.end_.offset = 1 + · exact minimal_truncatedRangeRev_of_offset_one hread hmore hrev hbegin h1 + · exact minimal_truncatedRangeRev_of_end_offset_nonpos hread hmore hrev hbegin + (by omega) + · rw [if_neg (by omega)] + exact minimal_topOfLast_of_end_offset_ge_two_rev hread hmore hrev hbegin hge hhi + +/-- **The earlier full reversed truncated rule at begin offset one.** Read off the +reported keys in one pass. With `E = end_.key`, `L` the last (least) reported key, `H` +the head (greatest) reported key, `t` the number of reported keys at or above `E`, and +`e = (end_.offset - 1).toNat`: + +* if `end_.offset ≤ 1`, the range is `[L, E)`; +* otherwise, if `t = e`, it is `[min L E, keyAfter H)`; +* otherwise it is `[min L E, ⊤)`. + +This rule is superseded on the last branch by `closedFormOffsetOneTruncatedRevFullC`; +it remains for the under-end and finite branches, where the two agree. -/ +def closedFormOffsetOneTruncatedRevFull (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) + let t := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + let e := (r.end_.offset - 1).toNat + if t = e then ⟨min L r.end_.key, ((keyAfter H : Key) : WithTop Key)⟩ + else ⟨min L r.end_.key, ⊤⟩ + +/-- Iterating the successor `keyAfter`. -/ +def keyAfterN : Nat → Key → Key + | 0, k => k + | n+1, k => keyAfter (keyAfterN n k) + +@[simp] theorem keyAfterN_zero (k : Key) : keyAfterN 0 k = k := rfl + +@[simp] theorem keyAfterN_succ (n : Nat) (k : Key) : + keyAfterN (n+1) k = keyAfter (keyAfterN n k) := rfl + +/-- The iterated successor stays at or above the starting key. -/ +theorem keyAfterN_le_self (E : Key) : ∀ n, E ≤ keyAfterN n E := by + intro n; induction n with + | zero => simp + | succ n ih => exact le_trans ih (le_of_lt (keyAfter_self_lt _)) + +/-- The iterated successor is strictly increasing. -/ +theorem keyAfterN_lt_succ (E : Key) (n : Nat) : + keyAfterN n E < keyAfterN (n+1) E := by + induction n with + | zero => simpa using keyAfter_self_lt E + | succ n ih => simpa using keyAfter_self_lt (keyAfter (keyAfterN n E)) + +/-- The keys in `[E, keyAfterN n E)` are exactly the first `n` iterated +successors of `E`. This is the combinatorial content of the top-branch closed +form: an interval ending at the `n`-th successor has only `n` keys above `E`. -/ +theorem chain_of_lt {E k : Key} : + ∀ n, E ≤ k → k < keyAfterN n E → ∃ i, i < n ∧ keyAfterN i E = k := by + intro n + induction n with + | zero => + intro hE h + exact absurd h (not_lt.mpr hE) + | succ n ih => + intro hE hlt + have hle : k ≤ keyAfterN n E := by + have h' : k < keyAfter (keyAfterN n E) := by simpa using hlt + exact (lt_keyAfter_iff k (keyAfterN n E)).mp h' + rcases lt_or_eq_of_le hle with hlt' | heq + · obtain ⟨i, hi, hik⟩ := ih hE hlt' + exact ⟨i, Nat.lt_succ_of_lt hi, hik⟩ + · exact ⟨n, Nat.lt_succ_self n, heq.symm⟩ + +/-- The iterated successor `keyAfterN` is the zero-run `chain`. -/ +theorem chain_eq_keyAfterN (b : Key) : ∀ n, chain b n = keyAfterN n b + | 0 => by rw [chain_zero, keyAfterN_zero] + | n + 1 => by rw [chain_succ, chain_eq_keyAfterN b n, keyAfterN_succ] + +/-- `keyAfterN` is strictly increasing in its index. -/ +theorem keyAfterN_lt_mono {E : Key} {i j : Nat} (h : i < j) : + keyAfterN i E < keyAfterN j E := by + rw [← chain_eq_keyAfterN E i, ← chain_eq_keyAfterN E j] + exact chain_lt_mono h + +/-- A database can store at most `n` keys in `[E, keyAfterN n E)`: the interval is +enumerated by the first `n` successors of `E`. -/ +theorem length_filter_between_le_keyAfterN {l : List Key} (hnd : l.Nodup) + (E : Key) (n : Nat) : + (l.filter (fun k => decide (E ≤ k ∧ k < keyAfterN n E))).length ≤ n := by + have hsub : ((l.filter (fun k => decide (E ≤ k ∧ k < keyAfterN n E))).toFinset : Set Key) ⊆ + keysIn (↑E : WithTop Key) (↑(keyAfterN n E) : WithTop Key) := by + intro x hx + simp only [Finset.mem_coe, List.mem_toFinset, List.mem_filter] at hx + have hx' := of_decide_eq_true hx.2 + exact ⟨WithTop.coe_le_coe.mpr hx'.1, + WithTop.coe_lt_coe.mpr hx'.2⟩ + have hnd' : (l.filter (fun k => decide (E ≤ k ∧ k < keyAfterN n E))).Nodup := + hnd.filter _ + have h1 := Set.encard_le_encard hsub + rw [Set.encard_coe_eq_coe_finsetCard, List.toFinset_card_of_nodup hnd'] at h1 + have h2 : (keysIn (↑E : WithTop Key) (↑(keyAfterN n E) : WithTop Key)).encard = + (n : ℕ∞) := by + rw [← chain_eq_keyAfterN, keysIn_chain, chainSet_encard] + rw [h2] at h1 + exact_mod_cast h1 + +/-- A stronger Boolean filter is a sublist of a weaker one. -/ +theorem filter_sublist_filter_of_imp {α : Type*} {l : List α} {p q : α → Bool} + (h : ∀ a, p a = true → q a = true) : List.Sublist (l.filter p) (l.filter q) := by + induction l with + | nil => simp + | cons a l ih => + rw [List.filter_cons, List.filter_cons] + by_cases hp : p a = true + · rw [if_pos hp] + have hq : q a = true := h a hp + rw [if_pos hq] + exact ih.cons₂ a + · rw [if_neg hp] + by_cases hq : q a = true + · rw [if_pos hq] + exact ih.cons a + · rw [if_neg hq] + exact ih + +/-- If `L ≤ keyAfterN n E`, then a database stores at most `n` keys in `[E, L)`. -/ +theorem length_filter_between_le_of_le_keyAfterN {l : List Key} (hnd : l.Nodup) + {E L : Key} {n : Nat} (hL : L ≤ keyAfterN n E) : + (l.filter (fun k => decide (E ≤ k ∧ k < L))).length ≤ n := by + have hsub : List.Sublist (l.filter (fun k => decide (E ≤ k ∧ k < L))) + (l.filter (fun k => decide (E ≤ k ∧ k < keyAfterN n E))) := by + apply filter_sublist_filter_of_imp + intro k hk + have hk' := of_decide_eq_true hk + exact decide_eq_true ⟨hk'.1, lt_of_lt_of_le hk'.2 hL⟩ + exact le_trans hsub.length_le (length_filter_between_le_keyAfterN hnd E n) + +/-- For a compatible truncated reversed result, the stored keys at or above the +reported last and below the resolved end are exactly the reported keys, reversed. +This is the unfiltered form of `compatible_iff_truncated_keys_rev`. -/ +private theorem compatible_filter_range_rev {db : DB} {r : RangeRead} {res : Result} {L : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) + (hL : L = res.keys.getLast hne) : + db.keys.filter (fun k : Key => decide (L ≤ k ∧ + (k : WithTop Key) < resolve db r.end_)) = res.keys.reverse := by + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hLmem : L ∈ RangeRead.completeKeys db r := by + rw [hL]; exact hpre.subset (List.getLast_mem hne) + have hLlo : resolve db r.begin ≤ (L : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLmem).2.1 + have hfilter := ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp).2 + have hck : RangeRead.completeKeys db r = + (db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_))).reverse := by + simp [RangeRead.completeKeys, RangeRead.inRange, hrev] + rw [hck, List.filter_reverse] at hfilter + have hrevEq : (db.keys.filter (fun k : Key => + decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_))).filter + (fun k : Key => decide (L ≤ k)) = res.keys.reverse := by + have := congrArg List.reverse hfilter + simpa [hL] using this + rw [List.filter_filter] at hrevEq + rw [← hrevEq] + apply List.filter_congr + intro k _ + by_cases hLk : L ≤ k + · have hlk : resolve db r.begin ≤ (k : WithTop Key) := + le_trans hLlo (by exact_mod_cast hLk) + simp [hLk, hlk] + · simp [hLk] + +/-- The full reversed truncated rule is minimal on the under-end cell and on the +overshoot cell whose reported head lies below `end_.key`. Both reductions are to the +already certified partial rules: the first to `truncatedRangeRev`, the second to +`topOfLast` (where `t = 0` forced the top branch). -/ +theorem minimal_closedForm_of_offset_one_truncated_rev_full {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 ∨ + ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.end_.key) : + Minimal r res (closedFormOffsetOneTruncatedRevFull r res) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLH : res.keys.getLast hne ≤ res.keys.head hne := + (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + unfold closedFormOffsetOneTruncatedRevFull + by_cases hle : r.end_.offset ≤ 1 + · rw [if_pos hle] + by_cases h1 : r.end_.offset = 1 + · exact minimal_truncatedRangeRev_of_offset_one ⟨db₀, hcomp₀⟩ hmore hrev hbegin h1 + · exact minimal_truncatedRangeRev_of_end_offset_nonpos ⟨db₀, hcomp₀⟩ hmore hrev hbegin + (by omega) + · have hge : 2 ≤ r.end_.offset := by omega + rw [if_neg hle] + have hHlt : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.end_.key := by + rcases hcase with h | h + · exact absurd h hle + · exact h + have ht0 : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = 0 := by + rw [List.countP_eq_zero] + intro k hk hgek + rw [decide_eq_true_eq] at hgek + have hkhead : k ≤ res.keys.headD ([] : Key) := by + rw [hhead] + exact (hsort.pairwise.imp le_of_lt).rel_head hk + exact absurd hgek + (not_le.mpr (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkhead) hHlt)) + have hepos : 0 < (r.end_.offset - 1).toNat := by omega + dsimp only + rw [ht0] + rw [if_neg (by omega : ¬ (0 = (r.end_.offset - 1).toNat))] + have hheadLt : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := by + rw [← hhead]; exact hHlt + have hLE : ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.end_.key := by + rw [hlast] + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hLH) hheadLt + have hmin : min ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.end_.key = + ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := + min_eq_left (le_of_lt hLE) + rw [hmin] + exact minimal_topOfLast_of_end_offset_ge_two_rev ⟨db₀, hcomp₀⟩ hmore hrev hbegin hge + (fun k hk => by + have hkhead : k ≤ res.keys.head hne := + (hsort.pairwise.imp le_of_lt).rel_head hk + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkhead) hheadLt) + +/-- The reversed complete-keys count at or above `end_.key` is the same as the +forward count. -/ +private theorem completeKeys_count_ge_eq_min_rev {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + min ((r.end_.offset - 1).toNat) + (db.keys.length - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key))) := by + have h := completeKeys_count_ge_eq_min (db := db) (r := {r with reverse := false}) + (by simp) hbegin hcross hend + have hck : RangeRead.completeKeys db {r with reverse := false} = + (RangeRead.completeKeys db r).reverse := by + simp [RangeRead.completeKeys, hrev] + rw [hck] at h + rw [List.Perm.countP_eq _ (List.reverse_perm _)] at h + exact h + +/-- The number of reported keys at or above `end_.key` is at most +`(end_.offset - 1)` for a readable truncated reversed result at begin offset one. -/ +private theorem reported_count_ge_le_offset_sub_one_rev {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) + (hend : 2 ≤ r.end_.offset) : + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + obtain ⟨db, hcomp⟩ := hread + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hle : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := + List.Sublist.countP_le hpre.sublist + have hmin := completeKeys_count_ge_eq_min_rev (db := db) hrev hbegin hcross + (by omega : 1 ≤ r.end_.offset) + have hle2 : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + rw [hmin] + exact Nat.min_le_left _ _ + exact le_trans hle hle2 + + +private theorem rev_truncated_filter_between {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hne : res.keys ≠ []) : + db.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k ∧ + k ≤ res.keys.head hne)) = res.keys.reverse := by + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hfilter : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (res.keys.getLast hne ≤ k)) = res.keys := + ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp).2 + have hLres : resolve db r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp (hpre.subset (List.getLast_mem hne))).2.1 + have hHlt : ((res.keys.head hne : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp (hpre.subset (List.head_mem hne))).2.2 + have hmax : ∀ k ∈ RangeRead.completeKeys db r, k ≤ res.keys.head hne := + fun k hk => le_head_of_mem_completeKeys_rev hrev hpre hne hk + have hW : RangeRead.completeKeys db r = + (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).reverse := by + simp [RangeRead.completeKeys, hrev] + have h1 : (RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).filter + (fun k : Key => decide (res.keys.getLast hne ≤ k)) = res.keys.reverse := by + have h := hfilter + rw [hW, List.filter_reverse] at h + simpa using congrArg List.reverse h + have h2 : (db.keys.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_))).filter + (fun k : Key => decide (res.keys.getLast hne ≤ k)) = res.keys.reverse := by + simpa [RangeRead.inRange] using h1 + rw [← h2, List.filter_filter] + apply List.filter_congr + intro k hk + rw [Bool.decide_and] + by_cases hLk : res.keys.getLast hne ≤ k + · rw [decide_eq_true hLk, Bool.true_and] + apply decide_eq_decide.mpr + constructor + · intro hkH + refine ⟨?_, ?_⟩ + · exact le_trans hLres (WithTop.coe_le_coe.mpr hLk) + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkH) hHlt + · rintro ⟨hbeg, hlt⟩ + by_contra hkH + have hHk : res.keys.head hne < k := lt_of_not_ge hkH + have hkD : k ∈ RangeRead.completeKeys db r := by + rw [hW, List.mem_reverse] + exact List.mem_filter.mpr ⟨hk, decide_eq_true ⟨hbeg, hlt⟩⟩ + exact absurd (hmax k hkD) (not_le.mpr hHk) + · rw [decide_eq_false hLk] + rfl + + +/-- Under the finite reversed-truncated branch (`t = e`, `end.offset ≥ 2`), no stored +key of the database lies in `[end_.key, last reported)`. The reported keys are a +prefix of the descending complete keys; if a complete key sat below the last reported +key but at or above `end_.key`, the complete keys would contain strictly more keys at +or above `end_.key` than the count bound allows. -/ +private theorem rev_truncated_no_key_between {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + {k : Key} (hkdb : k ∈ db.keys) + (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : k < res.keys.getLast hne) : + False := by + set P : Key → Bool := fun k => decide (r.end_.key ≤ (k : WithTop Key)) with hP + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hLmem : res.keys.getLast hne ∈ RangeRead.completeKeys db r := + hpre.subset (List.getLast_mem hne) + have hLlt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hLmem).2.2 + have hkcomp : k ∈ RangeRead.completeKeys db r := by + rw [RangeRead.mem_completeKeys] + refine ⟨hkdb, ?_, ?_⟩ + · exact (key_le_resolve_iff_of_offset_one hbegin hkdb).mp (le_trans hcross hEk) + · exact lt_trans (WithTop.coe_lt_coe.mpr hkL) hLlt + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLle : ∀ x ∈ res.keys, res.keys.getLast hne ≤ x := + fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hEltL : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key) := + lt_of_le_of_lt hEk (WithTop.coe_lt_coe.mpr hkL) + have hall : ∀ x ∈ res.keys, P x = true := by + intro x hx + rw [hP] + exact decide_eq_true (le_trans (le_of_lt hEltL) (WithTop.coe_le_coe.mpr (hLle x hx))) + have hcount_res : res.keys.countP P = res.keys.length := + List.countP_eq_length.mpr hall + have hlen_eq : res.keys.length = (r.end_.offset - 1).toNat := by + rw [← hcount_res] + exact ht + have hprefix : res.keys <+: (RangeRead.completeKeys db r).filter P := by + have h := hpre.filter P + rwa [List.filter_eq_self.mpr hall] at h + have hkfilter : k ∈ (RangeRead.completeKeys db r).filter P := + List.mem_filter.mpr ⟨hkcomp, by rw [hP]; exact decide_eq_true hEk⟩ + have hknot : k ∉ res.keys := fun hkres => absurd (hLle k hkres) (not_le.mpr hkL) + have hltlen : res.keys.length < ((RangeRead.completeKeys db r).filter P).length := by + by_contra hnot + have heq : res.keys.length = ((RangeRead.completeKeys db r).filter P).length := by + have hle := hprefix.length_le + omega + have hEq := hprefix.eq_of_length heq + exact hknot (by rw [hEq]; exact hkfilter) + have hcount_complete_ge : res.keys.length + 1 ≤ + (RangeRead.completeKeys db r).countP P := by + rw [List.countP_eq_length_filter] + omega + have hcount_complete_le : (RangeRead.completeKeys db r).countP P ≤ + (r.end_.offset - 1).toNat := by + rw [hP, completeKeys_count_ge_eq_min_rev (db := db) hrev hbegin hcross (by omega)] + exact Nat.min_le_left _ _ + omega + +/-- In the finite reversed-truncated branch, the candidate `[min L E, keyAfter H)` has +exactly the reported keys, reversed, as its stored keys. -/ +private theorem rev_truncated_finite_keys_eq {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + (closedFormOffsetOneTruncatedRevFull r res).keys db = res.keys.reverse := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + have hbase := rev_truncated_filter_between (db := db) hcomp hmore hrev hne + unfold closedFormOffsetOneTruncatedRevFull + rw [if_neg (by omega : ¬ r.end_.offset ≤ 1)] + dsimp only + rw [if_pos ht] + rw [hlast, hhead, ← hbase] + unfold ConflictRange.keys + apply List.filter_congr + intro k hk + rw [decide_eq_decide] + constructor + · rintro ⟨hmin, hkafter⟩ + have hkH : k ≤ res.keys.head hne := + (lt_keyAfter_iff k (res.keys.head hne)).mp (WithTop.coe_lt_coe.mp hkafter) + have hLk : ((res.keys.getLast hne : Key) : WithTop Key) ≤ (k : WithTop Key) := by + by_contra hcon + have hkL : k < res.keys.getLast hne := + lt_of_not_ge (fun h : res.keys.getLast hne ≤ k => hcon (WithTop.coe_le_coe.mpr h)) + rcases min_le_iff.mp hmin with h | h + · exact hcon h + · exact (rev_truncated_no_key_between (db := db) hcomp hmore hrev hbegin hcross + hend hne ht hk h hkL).elim + exact ⟨WithTop.coe_le_coe.mp hLk, hkH⟩ + · rintro ⟨hLk, hkH⟩ + exact ⟨le_trans (min_le_left _ _) (WithTop.coe_le_coe.mpr hLk), + WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k (res.keys.head hne)).mpr hkH)⟩ + + +/-- In the finite reversed-truncated branch, the candidate equality fixes the +complete keys at or above the reported last key, hence compatibility. -/ +private theorem rev_finite_filter_eq {db : DB} {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) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + (hT : (closedFormOffsetOneTruncatedRevFull r res).keys db = res.keys.reverse) : + (RangeRead.completeKeys db r).filter + (fun k : Key => decide (res.keys.getLast hne ≤ k)) = res.keys := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + let L : Key := res.keys.getLast hne + let H : Key := res.keys.head hne + let E : WithTop Key := r.end_.key + let e : Nat := (r.end_.offset - 1).toNat + let P : Key → Bool := fun k => decide (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) ≤ ((H : Key) : WithTop Key)) + let Q : Key → Bool := fun k => decide (E ≤ (k : WithTop Key)) + let BT : Key → Bool := fun k => decide (min ((L : Key) : WithTop Key) E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < ((keyAfter H : Key) : WithTop Key)) + have hT' : db.keys.filter BT = res.keys.reverse := by + simpa only [closedFormOffsetOneTruncatedRevFull, if_neg (by omega : ¬ r.end_.offset ≤ 1), + if_pos ht, hlast, hhead, BT, L, H, E, ConflictRange.keys] using hT + have hresnodup : res.keys.Nodup := by + have h := db.nodup.filter BT + rw [hT'] at h + exact List.nodup_reverse.mp h + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLH : L ≤ H := by + dsimp only [L, H] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hepos : 0 < e := by dsimp only [e]; omega + -- `H ≥ E`, from a reported key at or above `E`. + have htpos : 0 < res.keys.countP Q := by + dsimp only [Q] + rw [ht] + exact hepos + obtain ⟨k₀, hk₀mem, hk₀Q⟩ := List.countP_pos_iff.mp htpos + have hk₀E : E ≤ (k₀ : WithTop Key) := by + dsimp only [Q] at hk₀Q + exact of_decide_eq_true hk₀Q + have hk₀H : k₀ ≤ H := by + dsimp only [H] + exact (hsort.pairwise.imp le_of_lt).rel_head hk₀mem + have hHE : E ≤ ((H : Key) : WithTop Key) := le_trans hk₀E (WithTop.coe_le_coe.mpr hk₀H) + -- `begin.key ≤ L`, using readability. + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + have hLmem₀ : L ∈ RangeRead.completeKeys db₀ r := by + dsimp only [L] + exact hpre₀.subset (List.getLast_mem hne) + have hLdb₀ : L ∈ db₀.keys := (RangeRead.mem_completeKeys.mp hLmem₀).1 + have hLres₀ : resolve db₀ r.begin ≤ ((L : Key) : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLmem₀).2.1 + have hkeyL : r.begin.key ≤ ((L : Key) : WithTop Key) := + (key_le_resolve_iff_of_offset_one (db := db₀) hbegin hLdb₀).mpr hLres₀ + have hLmemC : L ∈ res.keys.reverse := by + dsimp only [L] + exact List.mem_reverse.mpr (List.getLast_mem hne) + have hLdb : L ∈ db.keys := by + have h := hLmemC + rw [← hT'] at h + exact List.mem_of_mem_filter h + have hresL : resolve db r.begin ≤ ((L : Key) : WithTop Key) := + (key_le_resolve_iff_of_offset_one hbegin hLdb).mp hkeyL + have hHmemC : H ∈ res.keys.reverse := by + dsimp only [H] + exact List.mem_reverse.mpr (List.head_mem hne) + have hHdb : H ∈ db.keys := by + have h := hHmemC + rw [← hT'] at h + exact List.mem_of_mem_filter h + -- The number of stored keys in `[E, H]` is `e`. + have hcountEH : db.keys.countP P = e := by + have h1 : db.keys.countP P = db.keys.countP (fun k => Q k && BT k) := by + apply List.countP_congr + intro k hk + dsimp only [P, Q, BT] + simp only [decide_eq_true_eq, Bool.and_eq_true] + constructor + · rintro ⟨hEk, hkH⟩ + refine ⟨hEk, le_trans (min_le_right _ _) hEk, ?_⟩ + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k H).mpr (WithTop.coe_le_coe.mp hkH)) + · rintro ⟨hEk, -, hkH⟩ + exact ⟨hEk, WithTop.coe_le_coe.mpr + ((lt_keyAfter_iff k H).mp (WithTop.coe_lt_coe.mp hkH))⟩ + have h2 : db.keys.countP (fun k => Q k && BT k) = e := by + rw [← List.countP_filter] + rw [hT', List.Perm.countP_eq _ (List.reverse_perm res.keys)] + dsimp only [Q, e] + exact ht + rw [h1, h2] + -- Split at `E` and `H` to compute the index of `H`. + have hEne : E ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hHE + exact absurd hHE (not_le.mpr (WithTop.coe_lt_top H)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hEne + have hvH : v ≤ H := by + rw [← hv] at hHE + exact WithTop.coe_le_coe.mp hHE + have hcountP_split : db.keys.countP P = + (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length + 1 := by + rw [List.countP_eq_length_filter] + have hlist : db.keys.filter P = + (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))) ++ [H] := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · apply List.sortedLT_append.mpr + refine ⟨List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _), + sortedLT_singleton H, ?_⟩ + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + exact (of_decide_eq_true (List.mem_filter.mp ha).2).2 + · intro x + dsimp only [P] + rw [← hv] + simp only [List.mem_filter, List.mem_append, List.mem_singleton, + decide_eq_true_eq, WithTop.coe_le_coe] + constructor + · rintro ⟨hx, hvx, hxH⟩ + by_cases hxH' : x = H + · exact Or.inr hxH' + · exact Or.inl ⟨hx, hvx, lt_of_le_of_ne hxH hxH'⟩ + · rintro (⟨hx, hvx, hxH⟩ | rfl) + · exact ⟨hx, hvx, le_of_lt hxH⟩ + · exact ⟨hHdb, hvH, le_refl H⟩ + rw [hlist, List.length_append, List.length_singleton] + have hfilter_len : (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length = e - 1 := by + omega + have hsplit := countP_lt_eq_add_filter db.keys db.nodup hvH + have hcE : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) = + db.keys.countP (fun k : Key => decide (k < v)) := by + rw [← hv] + exact countP_lt_coe v + have hcH : db.keys.countP (fun k : Key => decide (k < H)) = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by + have h := hsplit + rw [← hcE, hfilter_len] at h + omega + obtain ⟨j, hjlt, hj⟩ := List.mem_iff_getElem.mp hHdb + have hcj : db.keys.countP (fun k : Key => decide (k < H)) = j := + countP_lt_eq_of_getElem db.sorted hjlt hj + have hj_eq : j = db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by + rw [← hcj] + exact hcH + have hEanchor : (r.end_.anchor db).toNat = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega)] + have hanchor_nonneg : 0 ≤ r.end_.anchor db := by + unfold Selector.anchor + have hc : (0 : ℤ) ≤ (db.keys.countP + (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) : ℤ) := by + exact_mod_cast Nat.zero_le _ + omega + -- `H < resolve end_`, so no complete key above `H` survives. + have hHlt : ((H : Key) : WithTop Key) < resolve db r.end_ := by + by_cases hlt : (r.end_.anchor db).toNat < db.keys.length + · rw [resolve_eq_getElem hanchor_nonneg hlt] + apply WithTop.coe_lt_coe.mpr + have hidx : (r.end_.anchor db).toNat = j + 1 := by + rw [hEanchor] + omega + have hj1 : j + 1 < db.keys.length := by rw [← hidx]; exact hlt + have hstep : db.keys[j] < db.keys[j+1] := + db.sorted.getElem_lt_getElem_of_lt (i := j) (j := j+1) + (hi := hjlt) (hj := hj1) (Nat.lt_succ_self j) + rw [hj] at hstep + have hget : db.keys[(r.end_.anchor db).toNat]? = some (db.keys[j+1]) := by + rw [hidx] + exact List.getElem?_eq_getElem hj1 + obtain ⟨_, hEq⟩ := List.getElem?_eq_some_iff.mp hget + rw [hEq] + exact hstep + · rw [resolve_eq_top hanchor_nonneg (le_of_not_gt hlt)] + exact WithTop.coe_lt_top H + -- No stored key lies strictly between `H` and `resolve end_`. + have hnoHre : ∀ k ∈ db.keys, ((H : Key) : WithTop Key) < (k : WithTop Key) → + (k : WithTop Key) < resolve db r.end_ → False := by + intro k hk hHk hkre + have hEk : E ≤ (k : WithTop Key) := le_trans hHE (le_of_lt hHk) + have hkcomp : k ∈ RangeRead.completeKeys db r := by + rw [RangeRead.mem_completeKeys] + exact ⟨hk, le_trans hresL (le_trans (WithTop.coe_le_coe.mpr hLH) (le_of_lt hHk)), + hkre⟩ + have hall_complete : ∀ x ∈ res.keys, x ∈ RangeRead.completeKeys db r := by + intro x hx + rw [RangeRead.mem_completeKeys] + refine ⟨?_, ?_, ?_⟩ + · have hxC : x ∈ res.keys.reverse := List.mem_reverse.mpr hx + rw [← hT'] at hxC + exact List.mem_of_mem_filter hxC + · exact le_trans hresL (WithTop.coe_le_coe.mpr + ((hsort.pairwise.imp le_of_lt).rel_getLast hx)) + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr + ((hsort.pairwise.imp le_of_lt).rel_head hx)) hHlt + have hknot : k ∉ res.keys := fun hkres => + absurd ((hsort.pairwise.imp le_of_lt).rel_head hkres) + (not_le.mpr (WithTop.coe_lt_coe.mp hHk)) + have hSsub : ((res.keys.filter Q) ++ [k]).Subperm + ((RangeRead.completeKeys db r).filter Q) := by + apply List.subperm_of_subset + · apply List.nodup_append.mpr + refine ⟨hresnodup.filter Q, List.nodup_singleton k, ?_⟩ + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + intro hab + have ha' := List.mem_filter.mp ha + exact hknot (hab ▸ ha'.1) + · intro x hx + simp only [List.mem_append, List.mem_singleton, List.mem_filter] at hx ⊢ + rcases hx with ⟨hxres, hxQ⟩ | rfl + · exact ⟨hall_complete x hxres, hxQ⟩ + · exact ⟨hkcomp, by dsimp only [Q]; exact decide_eq_true hEk⟩ + have hle := List.Subperm.countP_le Q hSsub + have hQcomplete : (RangeRead.completeKeys db r).countP Q = + ((RangeRead.completeKeys db r).filter Q).length := + List.countP_eq_length_filter + have hQS : ((res.keys.filter Q) ++ [k]).countP Q = + ((res.keys.filter Q) ++ [k]).length := by + apply List.countP_eq_length.mpr + intro x hx + simp only [List.mem_append, List.mem_singleton, List.mem_filter] at hx + rcases hx with ⟨_, hxQ⟩ | rfl + · exact hxQ + · dsimp only [Q]; exact decide_eq_true hEk + have hlenS : ((res.keys.filter Q) ++ [k]).length = e + 1 := by + rw [List.length_append, List.length_singleton, ← List.countP_eq_length_filter] + dsimp only [Q, e] + rw [ht] + have hrescount : res.keys.countP Q = e := by + dsimp only [Q, e] + exact ht + have hbound : (RangeRead.completeKeys db r).countP Q ≤ e := by + dsimp only [Q] + rw [completeKeys_count_ge_eq_min_rev (db := db) hrev hbegin hcross (by omega)] + refine le_trans (Nat.min_le_left _ _) ?_ + rfl + rw [hQS] at hle + have hQcf : ((RangeRead.completeKeys db r).filter Q).countP Q = + ((RangeRead.completeKeys db r).filter Q).length := by + apply List.countP_eq_length.mpr + intro x hx + exact (List.mem_filter.mp hx).2 + rw [hQcf] at hle + rw [hlenS] at hle + have hb : ((RangeRead.completeKeys db r).filter Q).length ≤ e := by + rw [← hQcomplete] + exact hbound + omega + -- No stored key lies in `[E, L)`. + have hnoEL : ∀ k ∈ db.keys, E ≤ (k : WithTop Key) → + (k : WithTop Key) < ((L : Key) : WithTop Key) → False := by + intro k hk hEk hkL + have hkB : BT k = true := by + dsimp only [BT] + rw [decide_eq_true_eq] + refine ⟨le_trans (min_le_right _ _) hEk, ?_⟩ + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k H).mpr + (WithTop.coe_le_coe.mp (le_trans (le_of_lt hkL) (WithTop.coe_le_coe.mpr hLH)))) + have hkC := List.mem_filter.mpr ⟨hk, hkB⟩ + rw [hT'] at hkC + have hkres : k ∈ res.keys := List.mem_reverse.mp hkC + have hLk : L ≤ k := (hsort.pairwise.imp le_of_lt).rel_getLast hkres + exact absurd (WithTop.coe_le_coe.mpr hLk) (not_le.mpr hkL) + + -- The complete keys at or above `L` are exactly the reported keys. + have hfilter : (RangeRead.completeKeys db r).filter + (fun k : Key => decide ((L : Key) ≤ k)) = res.keys := by + let W : Key → Bool := fun k => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) + have hW : RangeRead.completeKeys db r = (db.keys.filter W).reverse := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [if_pos hrev] + have hstep : (db.keys.filter W).filter (fun k : Key => decide ((L : Key) ≤ k)) = + res.keys.reverse := by + rw [List.filter_filter, ← hT'] + apply List.filter_congr + intro k hk + apply Bool.eq_iff_iff.mpr + dsimp only [W, BT] + simp only [Bool.and_eq_true, decide_eq_true_eq] + constructor + · rintro ⟨hLk, hresb, hkre⟩ + refine ⟨le_trans (min_le_left _ _) (WithTop.coe_le_coe.mpr hLk), ?_⟩ + have hkH : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) := by + by_contra hcon + exact (hnoHre k hk (lt_of_not_ge hcon) hkre).elim + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k H).mpr (WithTop.coe_le_coe.mp hkH)) + · rintro ⟨hmin, hkafter⟩ + have hkH : (k : WithTop Key) ≤ ((H : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr + ((lt_keyAfter_iff k H).mp (WithTop.coe_lt_coe.mp hkafter)) + have hLk : (L : Key) ≤ k := by + by_cases hLE : ((L : Key) : WithTop Key) ≤ E + · exact WithTop.coe_le_coe.mp (min_eq_left hLE ▸ hmin) + · have hEL : E < ((L : Key) : WithTop Key) := lt_of_not_ge hLE + by_contra hcon + have hkL : (k : Key) < L := lt_of_not_ge hcon + exact (hnoEL k hk (min_eq_right (le_of_lt hEL) ▸ hmin) + (WithTop.coe_lt_coe.mpr hkL)).elim + exact ⟨hLk, le_trans hresL (WithTop.coe_le_coe.mpr hLk), + lt_of_le_of_lt hkH hHlt⟩ + rw [hW, List.filter_reverse, hstep, List.reverse_reverse] + simpa only [L] using hfilter + + +/-- In the finite reversed-truncated branch, inserting an unstored key `k` with +`end_.key ≤ k < last reported` makes the end selector resolve onto the reported head. +The head then leaves the complete keys, so the truncated result can no longer be +compatible. -/ +private theorem rev_insert_between_incompatible {db : DB} {r : RangeRead} {res : Result} + {k : Key} + (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true) + (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + (hk0 : k ∉ db.keys) + (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + ¬ RangeRead.Compatible (db.insert k hk0) r res := by + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + let L : Key := res.keys.getLast hne + let H : Key := res.keys.head hne + let E : WithTop Key := r.end_.key + let e : Nat := (r.end_.offset - 1).toNat + let P : Key → Bool := fun k => decide (E ≤ (k : WithTop Key) ∧ + (k : WithTop Key) ≤ ((H : Key) : WithTop Key)) + let Q : Key → Bool := fun k => decide (E ≤ (k : WithTop Key)) + let BT : Key → Bool := fun k => decide (min ((L : Key) : WithTop Key) E ≤ + (k : WithTop Key) ∧ (k : WithTop Key) < ((keyAfter H : Key) : WithTop Key)) + have hT : (closedFormOffsetOneTruncatedRevFull r res).keys db = res.keys.reverse := + rev_truncated_finite_keys_eq (db := db) hcomp hmore hrev hbegin hcross hend hne ht + have hT' : db.keys.filter BT = res.keys.reverse := by + simpa only [closedFormOffsetOneTruncatedRevFull, if_neg (by omega : ¬ r.end_.offset ≤ 1), + if_pos ht, hlast, hhead, BT, L, H, E, ConflictRange.keys] using hT + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLH : L ≤ H := by + dsimp only [L, H] + exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne) + have hepos : 0 < e := by dsimp only [e]; omega + -- `H ≥ E`, from a reported key at or above `E`. + have htpos : 0 < res.keys.countP Q := by + dsimp only [Q] + rw [ht] + exact hepos + obtain ⟨k₀, hk₀mem, hk₀Q⟩ := List.countP_pos_iff.mp htpos + have hk₀E : E ≤ (k₀ : WithTop Key) := by + dsimp only [Q] at hk₀Q + exact of_decide_eq_true hk₀Q + have hk₀H : k₀ ≤ H := by + dsimp only [H] + exact (hsort.pairwise.imp le_of_lt).rel_head hk₀mem + have hHE : E ≤ ((H : Key) : WithTop Key) := le_trans hk₀E (WithTop.coe_le_coe.mpr hk₀H) + have hHmemC : H ∈ res.keys.reverse := by + dsimp only [H] + exact List.mem_reverse.mpr (List.head_mem hne) + have hHdb : H ∈ db.keys := by + have h := hHmemC + rw [← hT'] at h + exact List.mem_of_mem_filter h + -- The number of stored keys in `[E, H]` is `e`. + have hcountEH : db.keys.countP P = e := by + have h1 : db.keys.countP P = db.keys.countP (fun k => Q k && BT k) := by + apply List.countP_congr + intro k hk + dsimp only [P, Q, BT] + simp only [decide_eq_true_eq, Bool.and_eq_true] + constructor + · rintro ⟨hEk, hkH⟩ + refine ⟨hEk, le_trans (min_le_right _ _) hEk, ?_⟩ + exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k H).mpr (WithTop.coe_le_coe.mp hkH)) + · rintro ⟨hEk, -, hkH⟩ + exact ⟨hEk, WithTop.coe_le_coe.mpr + ((lt_keyAfter_iff k H).mp (WithTop.coe_lt_coe.mp hkH))⟩ + have h2 : db.keys.countP (fun k => Q k && BT k) = e := by + rw [← List.countP_filter] + rw [hT', List.Perm.countP_eq _ (List.reverse_perm res.keys)] + dsimp only [Q, e] + exact ht + rw [h1, h2] + -- Split at `E` and `H` to compute the index of `H`. + have hEne : E ≠ (⊤ : WithTop Key) := by + intro htop + rw [htop] at hHE + exact absurd hHE (not_le.mpr (WithTop.coe_lt_top H)) + obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hEne + have hvH : v ≤ H := by + rw [← hv] at hHE + exact WithTop.coe_le_coe.mp hHE + have hcountP_split : db.keys.countP P = + (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length + 1 := by + rw [List.countP_eq_length_filter] + have hlist : db.keys.filter P = + (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))) ++ [H] := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · apply List.sortedLT_append.mpr + refine ⟨List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _), + sortedLT_singleton H, ?_⟩ + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + exact (of_decide_eq_true (List.mem_filter.mp ha).2).2 + · intro x + dsimp only [P] + rw [← hv] + simp only [List.mem_filter, List.mem_append, List.mem_singleton, + decide_eq_true_eq, WithTop.coe_le_coe] + constructor + · rintro ⟨hx, hvx, hxH⟩ + by_cases hxH' : x = H + · exact Or.inr hxH' + · exact Or.inl ⟨hx, hvx, lt_of_le_of_ne hxH hxH'⟩ + · rintro (⟨hx, hvx, hxH⟩ | rfl) + · exact ⟨hx, hvx, le_of_lt hxH⟩ + · exact ⟨hHdb, hvH, le_refl H⟩ + rw [hlist, List.length_append, List.length_singleton] + have hfilter_len : (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length = e - 1 := by + omega + have hsplit := countP_lt_eq_add_filter db.keys db.nodup hvH + have hcE : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) = + db.keys.countP (fun k : Key => decide (k < v)) := by + rw [← hv] + exact countP_lt_coe v + have hcH : db.keys.countP (fun k : Key => decide (k < H)) = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by + have h := hsplit + rw [← hcE, hfilter_len] at h + omega + obtain ⟨j, hjlt, hj⟩ := List.mem_iff_getElem.mp hHdb + have hcj : db.keys.countP (fun k : Key => decide (k < H)) = j := + countP_lt_eq_of_getElem db.sorted hjlt hj + have hj_eq : j = db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by + rw [← hcj] + exact hcH + have hEanchor : (r.end_.anchor db).toNat = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega)] + -- Inserting `k` at or above `E` does not change the count below `E`, so the + -- anchor still targets the successor of `H`; the new key lies below `H`, which + -- shifts `H` into that target index. + have hinskeys : (db.insert k hk0).keys = + db.keys.insertIdx (db.keys.countP (fun x => decide (x < k))) k := by + rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hk0] + have hcountnew : (db.insert k hk0).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [hinskeys] + exact countP_insertIdx _ _ _ (by + rw [decide_eq_false_iff_not] + exact not_lt.mpr hEk) + have hanchornew : (r.end_.anchor (db.insert k hk0)).toNat = + db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e := by + rw [anchor_toNat_eq (s := r.end_) (by omega), hcountnew] + have hJeq : (r.end_.anchor (db.insert k hk0)).toNat = j + 1 := by + rw [hanchornew] + have hb : db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e = + (db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e - 1) + 1 := by + omega + rw [hb, ← hj_eq] + have hpj : db.keys.countP (fun x : Key => decide (x < k)) ≤ j := by + rw [← hcj] + apply countP_le_countP_of_imp + intro x hx hlt + rw [decide_eq_true_eq] at hlt ⊢ + have hkH : k < H := lt_of_lt_of_le (WithTop.coe_lt_coe.mp hkL) hLH + exact lt_trans hlt hkH + have hnewlen : (r.end_.anchor (db.insert k hk0)).toNat < + (db.insert k hk0).keys.length := by + rw [hinskeys, hJeq, List.length_insertIdx_of_le_length List.countP_le_length] + omega + have hnewget? : (db.insert k hk0).keys[(r.end_.anchor (db.insert k hk0)).toNat]? = + some H := by + rw [hinskeys, hJeq, List.getElem?_insertIdx] + have hlt : ¬ j + 1 < db.keys.countP (fun x => decide (x < k)) := by omega + have heq : ¬ j + 1 = db.keys.countP (fun x => decide (x < k)) := by omega + rw [if_neg hlt, if_neg heq, Nat.add_sub_cancel, List.getElem?_eq_getElem hjlt, hj] + obtain ⟨_, hnewget⟩ := List.getElem?_eq_some_iff.mp hnewget? + have hHresolve : resolve (db.insert k hk0) r.end_ = ((H : Key) : WithTop Key) := by + rw [resolve_eq_getElem (anchor_nonneg_of_one_le (by omega)) hnewlen] + exact congrArg (fun x : Key => (x : WithTop Key)) hnewget + intro hcompNew + have hpre := RangeRead.compatible_keys_prefix hcompNew + have hHmem : H ∈ res.keys := by + dsimp only [H] + exact List.head_mem hne + have hHcomp : H ∈ RangeRead.completeKeys (db.insert k hk0) r := hpre.subset hHmem + have hlt := (RangeRead.mem_completeKeys.mp hHcomp).2.2 + rw [hHresolve] at hlt + exact (lt_irrefl ((H : Key) : WithTop Key)) hlt + + +/-- In the finite reversed-truncated branch, every key in `[end_.key, last reported)` +is necessary. A compatible database stores no key in that interval +(`rev_truncated_no_key_between`); inserting one, in a range that omits it, moves +`resolve end_` down onto the reported head, so the truncated result becomes +incompatible (`rev_insert_between_incompatible`). -/ +private theorem necessary_finite_below_last_rev {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) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) + {k : Key} (hEk : r.end_.key ≤ (k : WithTop Key)) + (hkL : ((k : Key) : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + intro cr hs + by_contra hkcr + have hk0 : k ∉ db₀.keys := by + intro hkdb + exact rev_truncated_no_key_between (db := db₀) hcomp₀ hmore hrev hbegin hcross + hend hne ht hkdb hEk (WithTop.coe_lt_coe.mp hkL) + have hagree : AgreeOn cr db₀ (db₀.insert k hk0) := + (ConflictRange.keys_insert_of_not_mem cr db₀ hk0 hkcr).symm + have hcompNew : RangeRead.Compatible (db₀.insert k hk0) r res := + (hs db₀ _ hagree).mp hcomp₀ + exact rev_insert_between_incompatible (db := db₀) hcomp₀ hmore hrev hbegin hcross + hend hne ht hk0 hEk hkL hcompNew + +/-- **The finite branch of the full reversed offset-one closed form.** When +`t = e` with `e ≥ 1`, the candidate `[min last end_.key, keyAfter head)` is minimal. +Soundness is the finite-branch filter identity: agreement on the candidate fixes the +complete keys at or above the reported last, which is exactly compatibility for a +truncated reversed result. Necessity splits at the reported last: keys in +`[last, head]` are reported-or-between, and keys in `[end_.key, last)` are supplied by +`necessary_finite_below_last_rev`. -/ +theorem minimal_closedForm_of_offset_one_truncated_rev_full_finite {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) + (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + Minimal r res (closedFormOffsetOneTruncatedRevFull r res) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn (closedFormOffsetOneTruncatedRevFull r res) db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hT : (closedFormOffsetOneTruncatedRevFull r res).keys db = res.keys.reverse := + rev_truncated_finite_keys_eq (db := db) hcomp_db hmore hrev hbegin hcross + hend hne ht + have hT' : (closedFormOffsetOneTruncatedRevFull r res).keys db' = res.keys.reverse := by + rw [← hag'] + exact hT + have hfilter := rev_finite_filter_eq (db := db') hread' hmore hrev hbegin hcross + hend hne ht hT' + exact (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr + ⟨hcomp_db.1, hfilter⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hk + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + unfold closedFormOffsetOneTruncatedRevFull at hk + rw [if_neg (by omega : ¬ r.end_.offset ≤ 1), if_pos ht] at hk + rw [hlast, hhead] at hk + have hkH : k ≤ res.keys.head hne := + (lt_keyAfter_iff k (res.keys.head hne)).mp (WithTop.coe_lt_coe.mp hk.2) + by_cases hkL : res.keys.getLast hne ≤ k + · exact necessary_of_between_result hread' (List.getLast_mem hne) (List.head_mem hne) + hkL hkH + · have hkL' : ((k : Key) : WithTop Key) < + ((res.keys.getLast hne : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr (lt_of_not_ge hkL) + have hEk : r.end_.key ≤ (k : WithTop Key) := by + rcases min_le_iff.mp hk.1 with h | h + · exact absurd (WithTop.coe_le_coe.mp h) hkL + · exact h + exact necessary_finite_below_last_rev hread' hmore hrev hbegin hcross hend hne ht + hEk hkL' + +/-- The full reversed truncated rule is also minimal on the finite branch, and still on +the under-end and below-head cells. -/ +theorem minimal_closedForm_of_offset_one_truncated_rev_full_of_cases {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) : + Minimal r res (closedFormOffsetOneTruncatedRevFull r res) := by + by_cases hle : r.end_.offset ≤ 1 + · exact minimal_closedForm_of_offset_one_truncated_rev_full hread hmore hrev hbegin + (Or.inl hle) + · by_cases ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat + · exact minimal_closedForm_of_offset_one_truncated_rev_full_finite hread hmore hrev + hbegin hcross (by omega : 2 ≤ r.end_.offset) ht + · have hH : ((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.end_.key := by + rcases hcase with h | h | h + · exact absurd h hle + · exact absurd h ht + · exact h + exact minimal_closedForm_of_offset_one_truncated_rev_full hread hmore hrev hbegin + (Or.inr hH) + + +/-- The closed form at begin offset one denotes exactly the necessary set. -/ +theorem closedFormOffsetOne_toSet_eq_necessarySet {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 = necessarySet r res := + (necessarySet_eq_of_minimal + (minimal_closedForm_of_offset_one hread hmore hne hrev hbegin hcross)).symm + +/-! +## The corrected reversed truncated top branch + +The earlier full reversed truncated rule used `min L E` on the top branch. That is too +small when `end_.key ≤ end_.key` and not enough keys lie between them: the resolved +begin must then start at the reported last key rather than at `end_.key`. The number of +available successor keys in `[E, L)` is exactly what `keyAfterN` measures, and the +corrected rule is `begin = E` only when `keyAfterN (e - t - 1) E < L`. +-/ + +/-- The corrected full reversed truncated rule. On the top branch it starts at +`end_.key` only when the `(e - t - 1)`-th successor of `end_.key` is still below the +reported last key; otherwise it starts at the reported last key. -/ +def closedFormOffsetOneTruncatedRevFullC (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) + let t := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + let e := (r.end_.offset - 1).toNat + if t = e then ⟨min L r.end_.key, ((keyAfter H : Key) : WithTop Key)⟩ + else + match r.end_.key with + | some E => + if keyAfterN (e - t - 1) E < res.keys.getLastD ([] : Key) then + ⟨(E : WithTop Key), ⊤⟩ + else ⟨L, ⊤⟩ + | none => ⟨L, ⊤⟩ + +/-- **Soundness of the corrected top branch starting at `end_.key`.** When the reported +last key lies strictly above `end_.key`, agreement on `[end_.key, ⊤)` fixes `resolve +end_` (`resolve_eq_of_agree_ge`), hence the stored keys in `[last, resolve end_)`, which +are exactly the reported keys reversed (`compatible_filter_range_rev`). The reported +last key is stored, so it also bounds `resolve begin`. -/ +theorem sound_topRev_begin_end {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hend : 1 ≤ r.end_.offset) + (hcross : r.begin.key ≤ r.end_.key) (hne : res.keys ≠ []) + {E₀ : Key} (hE : r.end_.key = (E₀ : WithTop Key)) + (hEL : (E₀ : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) : + Sound r res ⟨(E₀ : WithTop Key), ⊤⟩ := by + intro db₁ db₂ hagree + have hEagree : ∀ k : Key, (E₀ : WithTop Key) ≤ (k : WithTop Key) → + (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + intro k hk + exact (agreeOn_iff ⟨(E₀ : WithTop Key), ⊤⟩ db₁ db₂).mp hagree k + (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) + have step : ∀ db db', AgreeOn ⟨(E₀ : WithTop Key), ⊤⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hEagree' : ∀ k : Key, (E₀ : WithTop Key) ≤ (k : WithTop Key) → + (k ∈ db.keys ↔ k ∈ db'.keys) := by + intro k hk + exact (agreeOn_iff ⟨(E₀ : WithTop Key), ⊤⟩ db db').mp hag' k + (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) + have hlim : RangeRead.withinLimit r res.keys := hcomp.1 + have hEL' : r.end_.key < ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [hE]; exact hEL + have hre : resolve db r.end_ = resolve db' r.end_ := by + refine resolve_eq_of_agree_ge (s := r.end_) hend ?_ + intro k hk + rw [hE] at hk + exact hEagree' k hk + have hcomp_filter := compatible_filter_range_rev (db := db) hcomp hmore hrev hne rfl + have hLfilter : db'.keys.filter (fun k : Key => decide (res.keys.getLast hne ≤ k ∧ + (k : WithTop Key) < resolve db' r.end_)) = res.keys.reverse := by + rw [← hre, ← hcomp_filter] + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db'.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · intro k + rw [List.mem_filter, List.mem_filter] + constructor + · rintro ⟨hk', hpred⟩ + have hpred' := of_decide_eq_true hpred + exact ⟨(hEagree' k (le_trans (le_of_lt hEL) + (WithTop.coe_le_coe.mpr hpred'.1))).mpr hk', hpred⟩ + · rintro ⟨hk, hpred⟩ + have hpred' := of_decide_eq_true hpred + exact ⟨(hEagree' k (le_trans (le_of_lt hEL) + (WithTop.coe_le_coe.mpr hpred'.1))).mp hk, hpred⟩ + have hLmem' : res.keys.getLast hne ∈ db'.keys := by + have h := List.mem_reverse.mpr (List.getLast_mem hne) + rw [← hLfilter] at h + exact List.mem_of_mem_filter h + have hrb' : resolve db' r.begin ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + (key_le_resolve_iff_of_offset_one hbegin hLmem').mp + (le_trans hcross (le_of_lt hEL')) + have hfinal : (RangeRead.completeKeys db' r).filter + (fun k : Key => decide (res.keys.getLast hne ≤ k)) = res.keys := by + have hck : RangeRead.completeKeys db' r = + (RangeRead.inRange db'.keys (resolve db' r.begin) (resolve db' r.end_)).reverse := by + simp [RangeRead.completeKeys, hrev] + rw [hck, List.filter_reverse] + unfold RangeRead.inRange + rw [List.filter_filter] + have hcongr : db'.keys.filter (fun a : Key => decide (res.keys.getLast hne ≤ a) && + decide (resolve db' r.begin ≤ (a : WithTop Key) ∧ + (a : WithTop Key) < resolve db' r.end_)) = + db'.keys.filter (fun a : Key => decide (res.keys.getLast hne ≤ a ∧ + (a : WithTop Key) < resolve db' r.end_)) := by + apply List.filter_congr + intro a _ + by_cases hLa : res.keys.getLast hne ≤ a + · have hbeg : resolve db' r.begin ≤ (a : WithTop Key) := + le_trans hrb' (WithTop.coe_le_coe.mpr hLa) + simp [hLa, hbeg] + · simp [hLa] + rw [hcongr, hLfilter, List.reverse_reverse] + exact (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr + ⟨hlim, hfinal⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Padding necessity below the reported last in the reversed top branch.** If the +`n`-th successor of `end_.key` still lies below the reported last key `L`, then every +key `k ∈ [end_.key, L)` is necessary. The witness database pads the successor chain +below `L` so that inserting `k` raises the count of keys at or above `end_.key` just +enough to make the resolved end land on the reported head `keyAfter H`, while without +`k` the window stays unbounded and the extra padding key heads the complete keys. -/ +private theorem necessary_pad_top_rev {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) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + {E : Key} (hE : r.end_.key = (E : WithTop Key)) + (hEL : (E : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) + {n : Nat} + (hlt : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hroom : keyAfterN n E < res.keys.getLast hne) + {k : Key} (hEk : E ≤ k) (hkL : k < res.keys.getLast hne) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + set e : Nat := (r.end_.offset - 1).toNat with hedef + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx + rw [hLdef] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hHge : ∀ x ∈ res.keys, x ≤ H := by + intro x hx + rw [hHdef] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hLH : L ≤ H := hLle H (by rw [hHdef]; exact List.head_mem hne) + have hkL' : k < L := by rw [hLdef]; exact hkL + have hk_notin_res : k ∉ res.keys := by + intro hx + exact absurd (hLle k hx) (not_le.mpr hkL') + have hk_notin_rev : k ∉ res.keys.reverse := by + rw [List.mem_reverse]; exact hk_notin_res + have htlen : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq, hE] + exact le_trans (le_of_lt hEL) (WithTop.coe_le_coe.mpr (hLle x hx)) + have hn_e : e = n + res.keys.length + 1 := by + rw [hedef] + omega + -- the successor chain below `L` + set S : List Key := (List.range (n + 1)).map (chain E) with hSdef + have hS_len : S.length = n + 1 := by + rw [hSdef, List.length_map, List.length_range] + have hS_sorted : S.SortedLT := by + rw [hSdef]; exact sortedLT_map_chain E (n + 1) + have hS_nodup : S.Nodup := hS_sorted.pairwise.nodup + have hS_mem : ∀ x, x ∈ S ↔ ∃ i, i < n + 1 ∧ chain E i = x := by + intro x + rw [hSdef, List.mem_map] + constructor + · rintro ⟨i, hi, hix⟩; exact ⟨i, List.mem_range.mp hi, hix⟩ + · rintro ⟨i, hi, hix⟩; exact ⟨i, List.mem_range.mpr hi, hix⟩ + have hS_le : ∀ x ∈ S, x ≤ chain E n := by + intro x hx + obtain ⟨i, hi, rfl⟩ := (hS_mem x).mp hx + rcases lt_or_eq_of_le (by omega : i ≤ n) with h | h + · exact le_of_lt (chain_lt_mono h) + · rw [h] + have hS_lt_L : ∀ x ∈ S, x < L := by + intro x hx + have h := hS_le x hx + rw [chain_eq_keyAfterN E n] at h + exact lt_of_le_of_lt h hroom + have hS_ge_E : ∀ x ∈ S, E ≤ x := by + intro x hx + obtain ⟨i, hi, rfl⟩ := (hS_mem x).mp hx + exact chain_le E i + set P : List Key := (S.erase k).take n with hPdef + have hP_len : P.length = n := by + rw [hPdef, List.length_take] + have hge : n ≤ (S.erase k).length := by + by_cases hkS : k ∈ S + · rw [List.length_erase_of_mem hkS, hS_len]; omega + · rw [List.erase_of_not_mem hkS, hS_len]; omega + omega + have hP_sorted : P.SortedLT := by + rw [hPdef] + exact List.sortedLT_iff_pairwise.mpr + (List.Pairwise.take (i := n) (List.Pairwise.erase k (List.sortedLT_iff_pairwise.mp hS_sorted))) + have hP_sub : P ⊆ S := by + intro x hx + rw [hPdef] at hx + exact List.erase_subset ((List.take_sublist n (S.erase k)).subset hx) + have hP_lt_L : ∀ x ∈ P, x < L := fun x hx => hS_lt_L x (hP_sub hx) + have hP_ge_E : ∀ x ∈ P, E ≤ x := fun x hx => hS_ge_E x (hP_sub hx) + have hk_notin_P : k ∉ P := by + intro hx + have hx' : k ∈ S.erase k := (List.take_sublist n (S.erase k)).subset hx + exact (hS_nodup.mem_erase_iff.mp hx').1 rfl + set u : Key := keyAfter H with hudef + have hHlt_u : H < u := by rw [hudef]; exact keyAfter_self_lt H + have hres_lt_u : ∀ x ∈ res.keys.reverse, x < u := by + intro x hx + exact lt_of_le_of_lt (hHge x (List.mem_reverse.mp hx)) hHlt_u + have hk_notin_a : k ∉ P ++ res.keys.reverse ++ [u] := by + intro hx + rw [List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact hk_notin_P hx + · exact hk_notin_rev hx + · rw [List.mem_singleton] at hx + exact (ne_of_lt (lt_trans (lt_of_lt_of_le hkL' hLH) hHlt_u)) hx + set aList : List Key := P ++ res.keys.reverse ++ [u] with hadef + have hres_rev_sorted : res.keys.reverse.SortedLT := hsort.reverse + have hP_lt_res : ∀ x ∈ P, ∀ y ∈ res.keys.reverse, x < y := by + intro x hx y hy + exact lt_of_lt_of_le (hP_lt_L x hx) (hLle y (List.mem_reverse.mp hy)) + have ha_sorted : aList.SortedLT := by + rw [hadef] + refine List.sortedLT_append.mpr ⟨?_, ?_, ?_⟩ + · exact List.sortedLT_append.mpr ⟨hP_sorted, hres_rev_sorted, hP_lt_res⟩ + · exact List.sortedLT_iff_pairwise.mpr (by simp) + · intro x hx y hy + rw [List.mem_singleton] at hy + rw [hy] + rw [List.mem_append] at hx + rcases hx with hx | hx + · exact lt_of_lt_of_le (hP_lt_L x hx) (le_trans hLH (le_of_lt hHlt_u)) + · exact hres_lt_u x hx + let db_a : DB := ⟨aList, ha_sorted⟩ + have hlen_a : aList.length = e := by + rw [hadef, List.length_append, List.length_append, hP_len, + List.length_reverse, List.length_singleton] + omega + have ha_ge_E : ∀ x ∈ aList, E ≤ x := by + intro x hx + rw [hadef, List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact hP_ge_E x hx + · exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hEL)) + (hLle x (List.mem_reverse.mp hx)) + · rw [List.mem_singleton] at hx + rw [hx] + exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hEL)) + (le_trans hLH (le_of_lt hHlt_u)) + have hcount_a : aList.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx hlt + rw [decide_eq_true_eq] at hlt + rw [hE] at hlt + exact absurd (WithTop.coe_le_coe.mpr (ha_ge_E x hx)) (not_le.mpr hlt) + have hanc_a : (r.end_.anchor db_a).toNat = e := by + rw [anchor_toNat_eq (db := db_a) (s := r.end_) (by omega), hcount_a] + omega + have hresend_a : resolve db_a r.end_ = (⊤ : WithTop Key) := + resolve_eq_top (anchor_nonneg_of_one_le (db := db_a) (s := r.end_) (by omega)) + (by rw [hlen_a, hanc_a]) + have hin_a : RangeRead.inRange aList (resolve db_a r.begin) (resolve db_a r.end_) = aList := by + rw [hresend_a] + unfold RangeRead.inRange + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have hx' : r.begin.key ≤ (↑x : WithTop Key) := + le_trans hcross (by rw [hE]; exact WithTop.coe_le_coe.mpr (ha_ge_E x hx)) + exact ⟨(key_le_resolve_iff_of_offset_one (db := db_a) hbegin hx).mp hx', + WithTop.coe_lt_top x⟩ + have hck_a : RangeRead.completeKeys db_a r = aList.reverse := by + unfold RangeRead.completeKeys + rw [hrev] + simp only [if_true] + change (RangeRead.inRange aList (resolve db_a r.begin) (resolve db_a r.end_)).reverse = + aList.reverse + rw [hin_a] + have hnotcomp_a : ¬ RangeRead.Compatible db_a r res := by + intro hc + have hpre : res.keys <+: RangeRead.completeKeys db_a r := + RangeRead.compatible_keys_prefix hc + rw [hck_a] at hpre + have hrev_a : aList.reverse = u :: (res.keys ++ P.reverse) := by + rw [hadef, List.reverse_append, List.reverse_append, List.reverse_singleton, + List.reverse_reverse] + rfl + have hane : aList.reverse ≠ [] := by rw [hrev_a]; simp + have hhead : aList.reverse.head hane = u := by + have hh2 : aList.reverse.head? = some u := by rw [hrev_a]; rfl + exact Option.some.inj ((List.head?_eq_some_head hane).symm.trans hh2) + have hH_eq_u : H = u := hHdef.trans ((hpre.head hne).trans hhead) + exact (ne_of_lt hHlt_u) hH_eq_u + -- insert the target key + let db_b : DB := db_a.insert k hk_notin_a + have hb_keys : db_b.keys = aList.orderedInsert (· ≤ ·) k := rfl + have hb_mem : ∀ x, x ∈ db_b.keys ↔ x = k ∨ x ∈ aList := by + intro x + rw [hb_keys, List.mem_orderedInsert] + have hb_len : db_b.keys.length = e + 1 := by + rw [hb_keys] + have hp := List.Perm.length_eq (List.perm_orderedInsert (· ≤ ·) k aList) + simp only [List.length_cons] at hp + rw [hp, hlen_a] + have hb_ge_E : ∀ x ∈ db_b.keys, E ≤ x := by + intro x hx + rcases (hb_mem x).mp hx with rfl | hx + · exact hEk + · exact ha_ge_E x hx + have hcount_b : db_b.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx hlt + rw [decide_eq_true_eq] at hlt + rw [hE] at hlt + exact absurd (WithTop.coe_le_coe.mpr (hb_ge_E x hx)) (not_le.mpr hlt) + have hanc_b : (r.end_.anchor db_b).toNat = e := by + rw [anchor_toNat_eq (db := db_b) (s := r.end_) (by omega), hcount_b] + omega + have hresend_b : resolve db_b r.end_ = (↑u : WithTop Key) := by + have h0 := anchor_nonneg_of_one_le (db := db_b) (s := r.end_) (by omega) + have hidx : (r.end_.anchor db_b).toNat < db_b.keys.length := by + rw [hanc_b, hb_len]; omega + have hu_mem : u ∈ db_b.keys := by + rw [hb_mem]; exact Or.inr (by rw [hadef]; simp) + have hall_le_u : ∀ x ∈ db_b.keys, x ≤ u := by + intro x hx + rcases (hb_mem x).mp hx with rfl | hx + · exact le_of_lt (lt_trans (lt_of_lt_of_le hkL' hLH) hHlt_u) + · rw [hadef] at hx + rw [List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact le_of_lt (lt_of_lt_of_le (hP_lt_L x hx) + (le_trans hLH (le_of_lt hHlt_u))) + · exact le_of_lt (hres_lt_u x hx) + · rw [List.mem_singleton] at hx + rw [hx] + have hbne : db_b.keys ≠ [] := List.ne_nil_of_length_pos (by rw [hb_len]; omega) + have hlast : db_b.keys.getLast hbne = u := by + apply le_antisymm + · exact hall_le_u _ (List.getLast_mem hbne) + · exact (List.sortedLT_iff_pairwise.mp db_b.sorted).imp le_of_lt + |>.rel_getLast hu_mem + have hidx3 : db_b.keys.length - 1 = e := by rw [hb_len]; omega + have hgl := List.getLast_eq_getElem (l := db_b.keys) (h := hbne) + have hidx_e : e < db_b.keys.length := by rw [hb_len]; omega + have hkeyu : db_b.keys[e] = u := by + rw [List.getElem_eq_iff hidx_e, ← hidx3, + List.getElem?_eq_getElem (by omega), ← hgl, hlast] + have hkey_anchor : db_b.keys[(r.end_.anchor db_b).toNat] = u := by + rw [List.getElem_eq_iff hidx, hanc_b, List.getElem?_eq_getElem hidx_e, hkeyu] + rw [resolve_eq_getElem h0 hidx, hkey_anchor] + have hcomp_b : RangeRead.Compatible db_b r res := by + refine (compatible_iff_truncated_keys_rev (db := db_b) hrev hmore hne).mpr + ⟨hcomp₀.1, ?_⟩ + have hbegin_le_L : r.begin.key ≤ (↑L : WithTop Key) := by + rw [hE] at hcross + exact le_trans hcross (le_of_lt hEL) + have hL_mem_res : L ∈ res.keys := by + rw [hLdef]; exact List.getLast_mem hne + have hL_mem_b : L ∈ db_b.keys := by + rw [hb_mem]; exact Or.inr (by rw [hadef]; simp [hL_mem_res]) + have hrb_b : resolve db_b r.begin ≤ (↑L : WithTop Key) := + (key_le_resolve_iff_of_offset_one (db := db_b) hbegin hL_mem_b).mp hbegin_le_L + have hck_b : RangeRead.completeKeys db_b r = + (RangeRead.inRange db_b.keys (resolve db_b r.begin) (↑u : WithTop Key)).reverse := by + simp [RangeRead.completeKeys, hrev, hresend_b] + rw [hck_b, List.filter_reverse] + unfold RangeRead.inRange + rw [List.filter_filter] + have hpred : ∀ a ∈ db_b.keys, + (decide (L ≤ a) && decide (resolve db_b r.begin ≤ (↑a : WithTop Key) ∧ + (↑a : WithTop Key) < (↑u : WithTop Key))) = decide (L ≤ a ∧ a < u) := by + intro a _ + by_cases hLa : L ≤ a + · have hres : resolve db_b r.begin ≤ (↑a : WithTop Key) := + le_trans hrb_b (WithTop.coe_le_coe.mpr hLa) + rw [decide_eq_true hLa, Bool.true_and, decide_eq_decide] + exact ⟨fun h => ⟨hLa, WithTop.coe_lt_coe.mp h.2⟩, + fun h => ⟨hres, WithTop.coe_lt_coe.mpr h.2⟩⟩ + · rw [decide_eq_false hLa] + simp [hLa] + rw [List.filter_congr hpred] + have hstep : db_b.keys.filter (fun a => decide (L ≤ a ∧ a < u)) = res.keys.reverse := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db_b.sorted.pairwise.filter _) + · exact hres_rev_sorted + · intro x + rw [List.mem_filter, List.mem_reverse] + constructor + · rintro ⟨hx, hpred⟩ + have hpred' := of_decide_eq_true hpred + rcases (hb_mem x).mp hx with rfl | hx + · exact absurd hpred'.1 (not_le.mpr hkL') + · rw [hadef] at hx + rw [List.mem_append, List.mem_append] at hx + rcases hx with (hx | hx) | hx + · exact absurd hpred'.1 (not_le.mpr (hP_lt_L x hx)) + · exact List.mem_reverse.mp hx + · rw [List.mem_singleton] at hx + rw [hx] at hpred' + exact absurd hpred'.2 (lt_irrefl u) + · intro hx + refine ⟨?_, ?_⟩ + · rw [hb_mem]; exact Or.inr (by rw [hadef]; simp [hx]) + · exact decide_eq_true ⟨hLle x hx, hres_lt_u x (List.mem_reverse.mpr hx)⟩ + rw [hstep, List.reverse_reverse] + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db_a db_b := + (ConflictRange.keys_insert_of_not_mem cr db_a hk_notin_a hkcr).symm + exact hnotcomp_a ((hs db_a db_b hagree).mpr hcomp_b) + +/-- The number of keys below a `WithTop` threshold plus the number at or above it is the +length. -/ +private theorem countP_lt_add_countP_ge (l : List Key) (E : WithTop Key) : + l.countP (fun k : Key => decide ((k : WithTop Key) < E)) + + l.countP (fun k : Key => decide (E ≤ (k : WithTop Key))) = l.length := by + induction l with + | nil => simp + | cons a l ih => + by_cases h : ((a : WithTop Key) < E) + · have hneg : ¬ E ≤ (a : WithTop Key) := not_le.mpr h + rw [List.countP_cons_of_pos (by rw [decide_eq_true_eq]; exact h), + List.countP_cons_of_neg (by rw [decide_eq_true_eq]; exact hneg)] + simp only [List.length_cons] + omega + · have hge : E ≤ (a : WithTop Key) := not_lt.mp h + rw [List.countP_cons_of_neg (by rw [decide_eq_true_eq]; exact h), + List.countP_cons_of_pos (by rw [decide_eq_true_eq]; exact hge)] + simp only [List.length_cons] + omega + +/-- Inserting a key strictly above every element of a sorted list appends it. -/ +private theorem orderedInsert_append_of_forall_lt {l : List Key} {k : Key} + (h : ∀ x ∈ l, x < k) : + l.orderedInsert (· ≤ ·) k = l ++ [k] := by + induction l with + | nil => simp + | cons a l ih => + have hka : ¬ k ≤ a := not_le.mpr (h a (by simp)) + rw [List.orderedInsert_of_not_le (· ≤ ·) l hka, + ih (fun x hx => h x (by simp [hx]))] + rfl + +/-- **Above-head necessity in the reversed truncated top branch.** Every key strictly +above the reported head is necessary. The database made of the reported keys in +ascending order is compatible (its complete keys are exactly `res.keys`); inserting a +fresh key above the reported head puts it at the front of the complete keys, so the +reported keys are no longer a prefix. This holds in every top-branch subcase and needs +only the strict count inequality `t < e`. -/ +theorem necessary_above_head_top_rev {r : RangeRead} {res : Result} + (hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hne : res.keys ≠ []) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + {k : Key} (hkH : res.keys.head hne < k) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set L : Key := res.keys.getLast hne with hLdef + set H : Key := res.keys.head hne with hHdef + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx; rw [hLdef] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hHge : ∀ x ∈ res.keys, x ≤ H := by + intro x hx; rw [hHdef] + exact (hsort.pairwise.imp le_of_lt).rel_head hx + have hpre : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp₀ + have hLmem_ck : L ∈ RangeRead.completeKeys db₀ r := by + rw [hLdef]; exact hpre.subset (List.getLast_mem hne) + have hbegin_le_L : r.begin.key ≤ (↑L : WithTop Key) := + le_trans (key_le_resolve_of_offset_one hbegin) + (RangeRead.mem_completeKeys.mp hLmem_ck).2.1 + have hLH : L ≤ H := hLle H (List.head_mem hne) + have hbegin_le_k : r.begin.key ≤ (↑k : WithTop Key) := + le_trans hbegin_le_L (WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hLH hkH))) + let db₁ : DB := ⟨res.keys.reverse, hsort.reverse⟩ + have hdb₁_keys : db₁.keys = res.keys.reverse := rfl + have hcount0 : db₁.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [hdb₁_keys, List.countP_eq_zero] + intro x hx hbad + rw [decide_eq_true_eq] at hbad + have hx' : x ∈ res.keys := List.mem_reverse.mp hx + exact absurd (le_trans hbegin_le_L (WithTop.coe_le_coe.mpr (hLle x hx'))) + (not_le.mpr hbad) + have hne₁ : db₁.keys ≠ [] := by + rw [hdb₁_keys] + rwa [List.reverse_ne_nil_iff] + have hlen₁ : 0 < db₁.keys.length := List.length_pos_of_ne_nil hne₁ + have hresb : resolve db₁ r.begin = (↑L : WithTop Key) := by + have h0 := anchor_nonneg_of_one_le (db := db₁) (s := r.begin) (by omega) + have hancb : (r.begin.anchor db₁).toNat = 0 := by + rw [anchor_toNat_eq (db := db₁) (s := r.begin) (by omega), hbegin] + simp [hcount0] + have hidx : (r.begin.anchor db₁).toNat < db₁.keys.length := by + rw [hancb]; exact hlen₁ + have hhead : db₁.keys.head hne₁ = L := by + change res.keys.reverse.head hne₁ = L + rw [List.head_reverse] + have hget0 : db₁.keys[0] = L := by + rw [List.getElem_zero hlen₁, hhead] + have hkey : db₁.keys[(r.begin.anchor db₁).toNat] = L := by + rw [List.getElem_eq_iff hidx, hancb, List.getElem?_eq_getElem hlen₁, hget0] + rw [resolve_eq_getElem h0 hidx, hkey] + have hcountres : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + + res.keys.countP (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) = + res.keys.length := + countP_lt_add_countP_ge res.keys r.end_.key + have hcount₁ : db₁.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [hdb₁_keys] + exact List.Perm.countP_eq _ (List.reverse_perm _) + have hresend : resolve db₁ r.end_ = (⊤ : WithTop Key) := by + have h0 := anchor_nonneg_of_one_le (db := db₁) (s := r.end_) (by omega) + rw [resolve_eq_top h0] + have hanc : (r.end_.anchor db₁).toNat = db₁.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := + anchor_toNat_eq (db := db₁) (s := r.end_) (by omega) + rw [hanc, hcount₁, hdb₁_keys, List.length_reverse] + omega + have hck₁ : RangeRead.completeKeys db₁ r = res.keys := by + have hin : RangeRead.inRange db₁.keys (resolve db₁ r.begin) (resolve db₁ r.end_) = + db₁.keys := by + rw [hresend] + unfold RangeRead.inRange + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have hx' : x ∈ res.keys := List.mem_reverse.mp hx + exact ⟨by rw [hresb]; exact WithTop.coe_le_coe.mpr (hLle x hx'), + WithTop.coe_lt_top x⟩ + unfold RangeRead.completeKeys + rw [hrev] + simp only [if_true] + rw [hin, hdb₁_keys, List.reverse_reverse] + have hcomp₁ : RangeRead.Compatible db₁ r res := by + refine (compatible_iff_truncated_keys_rev (db := db₁) hrev hmore hne).mpr + ⟨hcomp₀.1, ?_⟩ + rw [hck₁, List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact hLle x hx + have hk_notin : k ∉ db₁.keys := by + intro hx + have hx' : k ∈ res.keys := List.mem_reverse.mp hx + exact absurd (hHge k hx') (not_le.mpr hkH) + have hnot : ¬ RangeRead.Compatible (db₁.insert k hk_notin) r res := by + intro hc + have hpre₂ : res.keys <+: RangeRead.completeKeys (db₁.insert k hk_notin) r := + RangeRead.compatible_keys_prefix hc + have hforall : ∀ x ∈ db₁.keys, x < k := by + intro x hx + have hx' : x ∈ res.keys := List.mem_reverse.mp hx + exact lt_of_le_of_lt (hHge x hx') hkH + have hkeys₂ : (db₁.insert k hk_notin).keys = db₁.keys ++ [k] := by + rw [DB.insert_keys] + exact orderedInsert_append_of_forall_lt hforall + have hcount0₂ : (db₁.insert k hk_notin).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.begin.key)) = 0 := by + rw [hkeys₂, List.countP_append, hcount0] + have hsingle : (decide ((k : WithTop Key) < r.begin.key)) = false := by + rw [decide_eq_false_iff_not] + exact not_lt.mpr hbegin_le_k + simp [hsingle] + have hresb₂ : resolve (db₁.insert k hk_notin) r.begin = (↑L : WithTop Key) := by + have h0 := anchor_nonneg_of_one_le (db := db₁.insert k hk_notin) (s := r.begin) + (by omega) + have hanc : (r.begin.anchor (db₁.insert k hk_notin)).toNat = 0 := by + have h1 : 1 ≤ r.begin.offset := by omega + rw [anchor_toNat_eq (db := db₁.insert k hk_notin) (s := r.begin) h1, + hbegin, hcount0₂] + simp + have hpos : 0 < (db₁.insert k hk_notin).keys.length := by + rw [hkeys₂, List.length_append, List.length_singleton]; omega + have hidx : (r.begin.anchor (db₁.insert k hk_notin)).toNat < + (db₁.insert k hk_notin).keys.length := by + rw [hanc]; exact hpos + have hget0₂ : (db₁.insert k hk_notin).keys[0] = L := by + have hpos' : 0 < (db₁.insert k hk_notin).keys.length := by + rw [hkeys₂, List.length_append, List.length_singleton]; omega + have hbase : db₁.keys[0] = L := by + have h0 := anchor_nonneg_of_one_le (db := db₁) (s := r.begin) (by omega) + have hancb : (r.begin.anchor db₁).toNat = 0 := by + have h1 : 1 ≤ r.begin.offset := by omega + rw [anchor_toNat_eq (db := db₁) (s := r.begin) h1, hbegin, hcount0] + simp + have hidx' : (r.begin.anchor db₁).toNat < db₁.keys.length := by + rw [hancb]; exact hlen₁ + have hres := hresb + rw [resolve_eq_getElem h0 hidx'] at hres + have hkey' : db₁.keys[(r.begin.anchor db₁).toNat] = L := + WithTop.coe_inj.mp hres + have hsome : db₁.keys[0]? = some L := by + have h1 : db₁.keys[(r.begin.anchor db₁).toNat]? = some L := by + rw [List.getElem?_eq_getElem hidx'] + exact congrArg some hkey' + rwa [hancb] at h1 + exact Option.some.inj ((List.getElem?_eq_getElem hlen₁).symm.trans hsome) + calc (db₁.insert k hk_notin).keys[0] + = (db₁.keys ++ [k])[0] := List.getElem_of_eq hkeys₂ hpos' + _ = db₁.keys[0] := List.getElem_append_left (i := 0) hlen₁ + _ = L := hbase + have hkey : (db₁.insert k hk_notin).keys[(r.begin.anchor (db₁.insert k hk_notin)).toNat] = L := by + rw [List.getElem_eq_iff hidx, hanc, List.getElem?_eq_getElem hpos, hget0₂] + rw [resolve_eq_getElem h0 hidx, hkey] + have hcount₁₂ : db₁.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) ≤ + (db₁.insert k hk_notin).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [hkeys₂, List.countP_append] + exact Nat.le_add_right (db₁.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key))) + (List.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) [k]) + have hresend₂ : resolve (db₁.insert k hk_notin) r.end_ = (⊤ : WithTop Key) := by + have h0 := anchor_nonneg_of_one_le (db := db₁.insert k hk_notin) (s := r.end_) + (by omega) + rw [resolve_eq_top h0] + have hanc := anchor_toNat_eq (db := db₁.insert k hk_notin) (s := r.end_) (by omega) + rw [hanc, hkeys₂, List.length_append, List.length_singleton] + have hlen1 : db₁.keys.length = res.keys.length := by + rw [hdb₁_keys, List.length_reverse] + have hcount_le : res.keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) ≤ + (db₁.keys ++ [k]).countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by + rw [List.countP_append] + calc res.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + = db₁.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := + hcount₁.symm + _ ≤ db₁.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + + [k].countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := + Nat.le_add_right _ _ + omega + have hck₂ : RangeRead.completeKeys (db₁.insert k hk_notin) r = k :: res.keys := by + have hin : RangeRead.inRange (db₁.insert k hk_notin).keys + (resolve (db₁.insert k hk_notin) r.begin) + (resolve (db₁.insert k hk_notin) r.end_) = (db₁.insert k hk_notin).keys := by + rw [hresend₂] + unfold RangeRead.inRange + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [hkeys₂] at hx + have hx' : x ∈ db₁.keys ∨ x = k := by + rw [List.mem_append, List.mem_singleton] at hx; exact hx + refine ⟨?_, WithTop.coe_lt_top x⟩ + rcases hx' with hx' | rfl + · rw [hresb₂] + have hx'' : x ∈ res.keys := List.mem_reverse.mp hx' + exact WithTop.coe_le_coe.mpr (hLle x hx'') + · rw [hresb₂] + exact WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hLH hkH)) + unfold RangeRead.completeKeys + rw [hrev] + simp only [if_true] + rw [hin, hkeys₂, List.reverse_append, List.reverse_singleton, hdb₁_keys, + List.reverse_reverse] + rfl + rw [hck₂] at hpre₂ + have hhead_eq : res.keys.head hne = k := hpre₂.head hne + have hhead_eq' : H = k := by rw [hHdef]; exact hhead_eq + exact (ne_of_lt hkH) hhead_eq' + intro cr hs + by_contra hkcr + have hagree : AgreeOn cr db₁ (db₁.insert k hk_notin) := + (ConflictRange.keys_insert_of_not_mem cr db₁ hk_notin hkcr).symm + exact hnot ((hs db₁ (db₁.insert k hk_notin) hagree).mp hcomp₁) + +/-- Restricting a count to a stronger predicate equals counting on the filtered list. -/ +private theorem countP_eq_countP_filter_of_imp {α : Type*} {l : List α} + {p q : α → Bool} (h : ∀ a, p a = true → q a = true) : + l.countP p = (l.filter q).countP p := by + rw [List.countP_eq_length_filter, List.countP_eq_length_filter, List.filter_filter] + congr 1 + apply List.filter_congr + intro a _ + by_cases hp : p a = true + · have hq : q a = true := h a hp + simp [hp, hq] + · simp [hp] + +/-- Filtering the complete keys by any predicate cannot increase the number of stored +keys satisfying that predicate. -/ +private theorem completeKeys_filter_length_le {db : DB} {r : RangeRead} + (hrev : r.reverse = true) (q : Key → Bool) : + ((RangeRead.completeKeys db r).filter q).length ≤ (db.keys.filter q).length := by + have hwin : List.Sublist + ((RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_)).filter q) + (db.keys.filter q) := by + unfold RangeRead.inRange + rw [List.filter_filter] + apply filter_sublist_filter_of_imp + intro a ha + by_cases hq : q a = true + · exact hq + · exfalso + have hqf : q a = false := by simpa using hq + rw [hqf] at ha + simp at ha + rw [RangeRead.completeKeys, hrev] + simp only [if_true] + rw [List.filter_reverse, List.length_reverse] + exact hwin.length_le + +/-- **The resolved end is unbounded on the remaining top-branch cells.** If `t < e` and +either the reported last key is at or below `end_.key`, or it lies above `end_.key` but +at or below the `(e - t - 1)`-th successor of `end_.key`, then every compatible database +resolves its end selector to the sentinel. A finite resolution would put exactly `e` +stored keys at or above `end_.key`; comparing with the reported keys at or above the +reported last leaves `e - t` (or, when the reported keys all lie above `end_.key`, +exactly `n + 1`) stored keys in `[end_.key, last)`, too many for the successor count. -/ +private theorem resolve_end_eq_top_of_top_branch_rev {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) + (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + {E L : Key} (hE : r.end_.key = (E : WithTop Key)) + (hL : L = res.keys.getLast hne) + {n : Nat} + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hcase : L ≤ E ∨ L ≤ keyAfterN n E) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hfin + have hanc0 : 0 ≤ r.end_.anchor db := + anchor_nonneg_of_one_le (db := db) (s := r.end_) (by omega) + have hidx : (r.end_.anchor db).toNat < db.keys.length := by + by_contra hge + exact hfin (resolve_eq_top hanc0 (not_lt.mp hge)) + set e : Nat := (r.end_.offset - 1).toNat with hedef + set c : Nat := db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcdef + have hanc : (r.end_.anchor db).toNat = c + e := by + rw [anchor_toNat_eq (db := db) (s := r.end_) (by omega), hcdef, hedef] + have hidx' : c + e < db.keys.length := by + rw [← hanc]; exact hidx + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx + rw [hL] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hfilter : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (L ≤ k)) = res.keys := by + rw [hL] + exact ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp).2 + have hcntL : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (L ≤ k)) = res.keys.length := by + rw [List.countP_eq_length_filter, hfilter] + have hcntE : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = e := by + have h := completeKeys_count_ge_eq_min_rev (db := db) hrev hbegin hcross + (by omega : 1 ≤ r.end_.offset) + have hle : e ≤ db.keys.length - c := by omega + rw [min_eq_left hle] at h + simpa only [← hedef, ← hcdef] using h + by_cases hLE : L ≤ E + · -- The reported last key does not exceed `end_.key`: `t = e`, contradiction. + have himp : ∀ k : Key, (decide (r.end_.key ≤ (k : WithTop Key)) = true) → + (decide (L ≤ k) = true) := by + intro k hk + rw [decide_eq_true_eq] at hk ⊢ + rw [hE] at hk + exact le_trans hLE (WithTop.coe_le_coe.mp hk) + have h1 := countP_eq_countP_filter_of_imp + (l := RangeRead.completeKeys db r) + (p := fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + (q := fun k : Key => decide (L ≤ k)) himp + rw [hcntE, hfilter] at h1 + exact (ne_of_lt hlt) h1.symm + · -- The reported last key exceeds `end_.key`: count the keys in `[end_.key, L)`. + have hkey : L ≤ keyAfterN n E := hcase.resolve_left hLE + have hEL : E < L := lt_of_not_ge hLE + have htR : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq, hE] + exact WithTop.coe_le_coe.mpr (le_trans (le_of_lt hEL) (hLle x hx)) + set D : Nat := ((RangeRead.completeKeys db r).filter + (fun k : Key => decide ((k : WithTop Key) < L))).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) with hDdef + have hDeq : e = D + res.keys.length := by + have h := List.countP_eq_countP_filter_add (RangeRead.completeKeys db r) + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + (fun k : Key => decide ((k : WithTop Key) < L)) + rw [hcntE] at h + have hsecond : (RangeRead.completeKeys db r).filter + (fun a : Key => !decide ((a : WithTop Key) < L)) = res.keys := by + rw [← hfilter] + apply List.filter_congr + intro a _ + by_cases h : ((a : WithTop Key) < L) + · have hnl : ¬ L ≤ a := fun hk => absurd (WithTop.coe_le_coe.mpr hk) (not_le.mpr h) + simp [h, hnl] + · have hnl : L ≤ a := by + exact WithTop.coe_le_coe.mp (not_lt.mp h) + simp [h, hnl] + have hlen : ((RangeRead.completeKeys db r).filter + (fun a : Key => !decide ((a : WithTop Key) < L))).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [hsecond, List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq, hE] + exact WithTop.coe_le_coe.mpr (le_trans (le_of_lt hEL) (hLle x hx)) + rw [hlen] at h + simpa only [hDdef] using h + have hDle : D ≤ n := by + have hp_eq : (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)) && + decide ((k : WithTop Key) < L)) = fun k : Key => decide (E ≤ k ∧ k < L) := by + funext k + by_cases h1 : (r.end_.key ≤ (k : WithTop Key)) + · by_cases h2 : ((k : WithTop Key) < L) + · have h1' : E ≤ k := by + rw [hE] at h1; exact WithTop.coe_le_coe.mp h1 + have h2' : k < L := WithTop.coe_lt_coe.mp h2 + simp [h1, h2, h1', h2'] + · have h2' : ¬ k < L := fun hk => h2 (WithTop.coe_lt_coe.mpr hk) + simp [h1, h2, h2'] + · have h1' : ¬ E ≤ k := fun hk => h1 (by rw [hE]; exact WithTop.coe_le_coe.mpr hk) + simp [h1, h1'] + have hDlen : D = ((RangeRead.completeKeys db r).filter + (fun k : Key => decide (E ≤ k ∧ k < L))).length := by + rw [hDdef, List.countP_eq_length_filter, List.filter_filter, hp_eq] + rw [hDlen] + exact le_trans (completeKeys_filter_length_le (db := db) (r := r) hrev _) + (length_filter_between_le_of_le_keyAfterN db.nodup hkey) + have hn' : n = e - res.keys.length - 1 := by + rw [hn, htR] + omega + +/-- **Agreement on `[last, ⊤)` preserves the unbounded end in the corrected top +branch.** A compatible database with an unbounded end has its keys at or above the +reported last exactly the reported keys reversed. Any other database agreeing there has +the same keys at or above the last; the strict top-branch inequality then bounds its +keys at or above `end_.key` by `e - 1`, so its end is also unbounded. -/ +private theorem resolve_end_eq_top_of_agree_begin_last {db db' : DB} {r : RangeRead} + {res : Result} {E L : Key} {n : Nat} + (hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1) + (hcross : r.begin.key ≤ r.end_.key) (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + (hE : r.end_.key = (E : WithTop Key)) (hL : L = res.keys.getLast hne) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hcase : L ≤ E ∨ L ≤ keyAfterN n E) + (hcomp : RangeRead.Compatible db r res) + (hEt : resolve db r.end_ = (⊤ : WithTop Key)) + (hagmem : ∀ k : Key, (L : WithTop Key) ≤ (k : WithTop Key) → + (k ∈ db.keys ↔ k ∈ db'.keys)) : + resolve db' r.end_ = (⊤ : WithTop Key) := by + have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev + have hLle : ∀ x ∈ res.keys, L ≤ x := by + intro x hx + rw [hL] + exact (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hpre := RangeRead.compatible_keys_prefix hcomp + have hLmem : L ∈ RangeRead.completeKeys db r := by + rw [hL]; exact hpre.subset (List.getLast_mem hne) + have hLlo : resolve db r.begin ≤ (L : WithTop Key) := + (RangeRead.mem_completeKeys.mp hLmem).2.1 + have hfilter : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (L ≤ k)) = res.keys := by + simpa only [hL] using + ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp).2 + have hdbEq : db.keys.filter (fun k : Key => decide (L ≤ k)) = res.keys.reverse := by + have h := completeKeys_filter_ge_rev (db := db) (r := r) (last := L) hrev hEt hLlo + rw [hfilter] at h + have hrevh := congrArg List.reverse h + simpa only [List.reverse_reverse] using hrevh.symm + have hdbEq' : db'.keys.filter (fun k : Key => decide (L ≤ k)) = res.keys.reverse := by + rw [← hdbEq] + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db'.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · intro k + rw [List.mem_filter, List.mem_filter] + constructor + · rintro ⟨hk', hpred⟩ + have hk : k ∈ db.keys := + (hagmem k (WithTop.coe_le_coe.mpr (of_decide_eq_true hpred))).mpr hk' + exact ⟨hk, hpred⟩ + · rintro ⟨hk, hpred⟩ + have hk' : k ∈ db'.keys := + (hagmem k (WithTop.coe_le_coe.mpr (of_decide_eq_true hpred))).mp hk + exact ⟨hk', hpred⟩ + -- The number of keys of `db'` at or above `end_.key` is at most `e`. + have hcountE : db'.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := by + by_cases hLE : L ≤ E + · -- All such keys are reported keys, since `E ≥ L`. + have himp : ∀ k : Key, + (decide (r.end_.key ≤ (k : WithTop Key)) = true) → + (decide (L ≤ k) = true) := by + intro k hk + rw [decide_eq_true_eq] at hk ⊢ + rw [hE] at hk + exact le_trans hLE (WithTop.coe_le_coe.mp hk) + have h1 := countP_eq_countP_filter_of_imp + (l := db'.keys) + (p := fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + (q := fun k : Key => decide (L ≤ k)) himp + rw [hdbEq'] at h1 + rw [h1, List.countP_reverse] + exact le_of_lt hlt + · -- Split the keys at `L`: the reported keys above, at most `n` keys below. + have hEL : E < L := lt_of_not_ge hLE + have hkey : L ≤ keyAfterN n E := hcase.resolve_left hLE + have ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq, hE] + exact WithTop.coe_le_coe.mpr (le_trans (le_of_lt hEL) (hLle x hx)) + have hsplit := List.countP_eq_countP_filter_add db'.keys + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + (fun k : Key => decide ((k : WithTop Key) < L)) + -- The part below `L` is counted by the interval `[E, L)`. + have h1 : (db'.keys.filter (fun k : Key => decide ((k : WithTop Key) < L))).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (db'.keys.filter (fun k : Key => decide (E ≤ k ∧ k < L))).length := by + rw [List.countP_eq_length_filter, List.filter_filter] + congr 1 + apply List.filter_congr + intro k _ + by_cases hp : (r.end_.key ≤ (k : WithTop Key)) + · by_cases hq : ((k : WithTop Key) < L) + · have hp' : E ≤ k := by rw [hE] at hp; exact WithTop.coe_le_coe.mp hp + have hq' : k < L := WithTop.coe_lt_coe.mp hq + simp [hp, hq, hp', hq'] + · have hq' : ¬ k < L := fun hk => hq (WithTop.coe_lt_coe.mpr hk) + simp [hp, hq, hq'] + · have hp' : ¬ E ≤ k := fun hk => hp (by rw [hE]; exact WithTop.coe_le_coe.mpr hk) + simp [hp, hp'] + have hbelow : (db'.keys.filter (fun k : Key => decide (E ≤ k ∧ k < L))).length ≤ n := + length_filter_between_le_of_le_keyAfterN db'.nodup hkey + -- The part above `L` is exactly the reported keys. + have hfilt2 : db'.keys.filter (fun k : Key => !decide ((k : WithTop Key) < L)) = + db'.keys.filter (fun k : Key => decide (L ≤ k)) := by + apply List.filter_congr + intro k _ + by_cases h : ((k : WithTop Key) < L) + · have hnl : ¬ L ≤ k := fun hk => + absurd (WithTop.coe_le_coe.mpr hk) (not_le.mpr h) + simp [h, hnl] + · have hnl : L ≤ k := WithTop.coe_le_coe.mp (not_lt.mp h) + simp [h, hnl] + have hlen2 : (db'.keys.filter (fun k : Key => !decide ((k : WithTop Key) < L))).length = + res.keys.length := by + rw [hfilt2, hdbEq', List.length_reverse] + have h2 : (db'.keys.filter (fun k : Key => !decide ((k : WithTop Key) < L))).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + have h2c : (db'.keys.filter (fun k : Key => !decide ((k : WithTop Key) < L))).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (db'.keys.filter (fun k : Key => !decide ((k : WithTop Key) < L))).length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq, hE] + have hnot : ¬ ((x : WithTop Key) < L) := by + have hx' := (List.mem_filter.mp hx).2 + by_contra hcon + rw [decide_eq_true hcon] at hx' + simp at hx' + have hLx : L ≤ x := WithTop.coe_le_coe.mp (not_lt.mp hnot) + exact WithTop.coe_le_coe.mpr (le_trans (le_of_lt hEL) hLx) + rw [h2c, hlen2] + rw [h1, h2] at hsplit + have hn' : n = (r.end_.offset - 1).toNat - res.keys.length - 1 := by + rw [hn, ht] + have hlen_le : res.keys.length ≤ (r.end_.offset - 1).toNat := by + rw [← ht]; exact le_of_lt hlt + omega + -- Now `resolve db' r.end_` cannot land inside the database. + set e : Nat := (r.end_.offset - 1).toNat with hedef + set c : Nat := db'.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + with hcdef + have hlen_eq : db'.keys.length = c + db'.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + rw [hcdef] + exact (countP_lt_add_countP_ge db'.keys r.end_.key).symm + have hlen_le : db'.keys.length ≤ c + e := by + rw [hlen_eq] + exact Nat.add_le_add_left hcountE c + have hanc0 : 0 ≤ r.end_.anchor db' := + anchor_nonneg_of_one_le (db := db') (s := r.end_) (by omega) + have hanc : (r.end_.anchor db').toNat = c + e := by + rw [anchor_toNat_eq (db := db') (s := r.end_) (by omega), hcdef, hedef] + rw [resolve_eq_top hanc0] + rw [hanc] + exact hlen_le + +/-- **Soundness of the corrected top branch beginning at the reported last key.** +Once the end resolves to the sentinel, agreement on `[last, ⊤)` fixes the stored keys +at or above `last`; the corrected top-branch hypotheses keep the end unbounded for the +agreed database as well (`resolve_end_eq_top_of_agree_begin_last`), so compatibility is +preserved. -/ +private theorem sound_topRev_begin_last {r : RangeRead} {res : Result} + (hmore : res.more = true) (hrev : r.reverse = true) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + {E L : Key} {n : Nat} + (hE : r.end_.key = (E : WithTop Key)) (hL : L = res.keys.getLast hne) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hcase : L ≤ E ∨ L ≤ keyAfterN n E) : + Sound r res ⟨(L : WithTop Key), ⊤⟩ := by + intro db₁ db₂ hagree + have step : ∀ db db', AgreeOn ⟨(L : WithTop Key), ⊤⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp + have hlim : RangeRead.withinLimit r res.keys := hcomp.1 + have hpre : res.keys <+: RangeRead.completeKeys db r := + RangeRead.compatible_keys_prefix hcomp + have hLmem : L ∈ RangeRead.completeKeys db r := by + rw [hL]; exact hpre.subset (List.getLast_mem hne) + have hbegL : r.begin.key ≤ (L : WithTop Key) := + le_trans (key_le_resolve_of_offset_one hbegin) + (RangeRead.mem_completeKeys.mp hLmem).2.1 + have hEt : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_top_branch_rev hcomp hmore hrev hbegin hcross hend hne + hE hL hn hlt hcase + have hagmem : ∀ k : Key, (L : WithTop Key) ≤ (k : WithTop Key) → + (k ∈ db.keys ↔ k ∈ db'.keys) := by + intro k hk + exact (agreeOn_iff ⟨(L : WithTop Key), ⊤⟩ db db').mp hag' k + (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) + have hEt' := resolve_end_eq_top_of_agree_begin_last hmore hrev hbegin hcross hend hne + hE hL hn hlt hcase hcomp hEt hagmem + have hfilter : (RangeRead.completeKeys db r).filter + (fun k : Key => decide (L ≤ k)) = res.keys := by + simpa only [hL] using + ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp).2 + have hdbEq : db.keys.filter (fun k : Key => decide (L ≤ k)) = res.keys.reverse := by + have h := completeKeys_filter_ge_rev (db := db) (r := r) (last := L) hrev hEt + (RangeRead.mem_completeKeys.mp hLmem).2.1 + rw [hfilter] at h + have hrevh := congrArg List.reverse h + simpa only [List.reverse_reverse] using hrevh.symm + have hdbEq' : db'.keys.filter (fun k : Key => decide (L ≤ k)) = res.keys.reverse := by + rw [← hdbEq] + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db'.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · intro k + rw [List.mem_filter, List.mem_filter] + constructor + · rintro ⟨hk', hpred⟩ + exact ⟨(hagmem k (WithTop.coe_le_coe.mpr (of_decide_eq_true hpred))).mpr hk', + hpred⟩ + · rintro ⟨hk, hpred⟩ + exact ⟨(hagmem k (WithTop.coe_le_coe.mpr (of_decide_eq_true hpred))).mp hk, + hpred⟩ + have hLmem' : L ∈ db'.keys := by + have h1 : L ∈ res.keys.reverse := + List.mem_reverse.mpr (by rw [hL]; exact List.getLast_mem hne) + rw [← hdbEq'] at h1 + exact List.mem_of_mem_filter h1 + have hL' : resolve db' r.begin ≤ (L : WithTop Key) := + (key_le_resolve_iff_of_offset_one hbegin hLmem').mp hbegL + have hfilter' : (RangeRead.completeKeys db' r).filter + (fun k : Key => decide (L ≤ k)) = res.keys := by + rw [completeKeys_filter_ge_rev (db := db') hrev hEt' hL', hdbEq', List.reverse_reverse] + exact (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr + ⟨hlim, by simpa only [hL] using hfilter'⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Minimality of the corrected top branch beginning at the reported last key.** +Soundness is `sound_topRev_begin_last`. Keys between the reported last and head are +reported-or-between; keys above the head are necessary by +`necessary_above_head_top_rev`. -/ +private theorem minimal_topRev_begin_last {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) + (hend : 2 ≤ r.end_.offset) + {E L : Key} {n : Nat} + (hE : r.end_.key = (E : WithTop Key)) + (hne : res.keys ≠ []) (hL : L = res.keys.getLast hne) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hcase : L ≤ E ∨ L ≤ keyAfterN n E) : + Minimal r res ⟨(L : WithTop Key), ⊤⟩ := by + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_topRev_begin_last hmore hrev hbegin hcross hend hne hE hL hn hlt hcase, ?_⟩ + intro k hk + have hLk : L ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkH : k ≤ res.keys.head hne + · exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne) + (List.head_mem hne) hLk hkH + · exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH) + +/-- **Minimality of the corrected top branch beginning at `end_.key`.** This is the +case where the successor chain from `end_.key` still reaches below the reported last +key. Soundness is `sound_topRev_begin_end`; keys in `[end_.key, last)` are supplied by +`necessary_pad_top_rev`, and the remaining keys by the between/above-head lemmas. -/ +private theorem minimal_topRev_begin_end {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) + (hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ []) + {E L : Key} {n : Nat} + (hE : r.end_.key = (E : WithTop Key)) (hL : L = res.keys.getLast hne) + (hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1) + (hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat) + (hroom : keyAfterN n E < L) : + Minimal r res ⟨(E : WithTop Key), ⊤⟩ := by + have hEL : (E : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key) := by + rw [← hL] + exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (keyAfterN_le_self E n) hroom) + have hroom' : keyAfterN n E < res.keys.getLast hne := by + rw [← hL]; exact hroom + refine (minimal_iff_sound_and_necessary).mpr + ⟨sound_topRev_begin_end hmore hrev hbegin (by omega) hcross hne hE hEL, ?_⟩ + intro k hk + have hEk : E ≤ k := WithTop.coe_le_coe.mp hk.1 + by_cases hkL : k < L + · exact necessary_pad_top_rev hread hmore hrev hbegin hcross hend hne hE hEL hlt hn + hroom' hEk (by rw [← hL]; exact hkL) + · have hLk : L ≤ k := le_of_not_gt hkL + by_cases hkH : k ≤ res.keys.head hne + · exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne) + (List.head_mem hne) hLk hkH + · exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH) + +/-- **Minimality of the corrected full reversed truncated rule.** The under-end and +finite branches coincide with the earlier rule; the top branch splits on whether the +successor chain from `end_.key` reaches below the reported last, using +`minimal_topRev_begin_end` or `minimal_topRev_begin_last`. When `end_.key` is the +sentinel every key is below it, so the top-of-last theorem applies. -/ +theorem minimal_closedForm_of_offset_one_truncated_rev_fullC {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) : + Minimal r res (closedFormOffsetOneTruncatedRevFullC r res) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by + rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl + by_cases hle : r.end_.offset ≤ 1 + · have h := minimal_closedForm_of_offset_one_truncated_rev_full hread' hmore hrev hbegin + (Or.inl hle) + unfold closedFormOffsetOneTruncatedRevFull at h + rw [if_pos hle] at h + unfold closedFormOffsetOneTruncatedRevFullC + rw [if_pos hle] + exact h + · have hcount_le : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤ + (r.end_.offset - 1).toNat := + reported_count_ge_le_offset_sub_one_rev hread' hmore hrev hbegin hcross + (by omega : 2 ≤ r.end_.offset) + by_cases ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat + · have h := minimal_closedForm_of_offset_one_truncated_rev_full_finite hread' hmore hrev + hbegin hcross (by omega : 2 ≤ r.end_.offset) ht + unfold closedFormOffsetOneTruncatedRevFull at h + rw [if_neg hle, if_pos ht] at h + unfold closedFormOffsetOneTruncatedRevFullC + rw [if_neg hle] + dsimp only + rw [if_pos ht] + simpa only [hlast, hhead] using h + · have hlt : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) < + (r.end_.offset - 1).toNat := lt_of_le_of_ne hcount_le ht + set n : Nat := (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1 + with hn_def + have hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1 := + hn_def + unfold closedFormOffsetOneTruncatedRevFullC + rw [if_neg hle] + dsimp only + rw [if_neg ht] + by_cases htop : r.end_.key = (⊤ : WithTop Key) + · rw [htop] + have h := minimal_topOfLast_of_end_offset_ge_two_rev hread' hmore hrev hbegin + (by omega : 2 ≤ r.end_.offset) + (fun k _ => by rw [htop]; exact WithTop.coe_lt_top k) + unfold topOfLast at h + exact h + · obtain ⟨E, hE⟩ := WithTop.ne_top_iff_exists.mp htop + rw [← hE] + dsimp only + set n : Nat := (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1 + with hn_def + have hn : n = (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1 := by + rw [hn_def, hE] + by_cases hroom : keyAfterN n E < res.keys.getLastD ([] : Key) + · have hroom' : keyAfterN n E < res.keys.getLast hne := by + rw [← hlast]; exact hroom + have h := minimal_topRev_begin_end hread' hmore hrev hbegin hcross + (by omega : 2 ≤ r.end_.offset) hne hE.symm rfl hn hlt hroom' + rw [if_pos hroom] + exact h + · have hge : res.keys.getLast hne ≤ keyAfterN n E := by + rw [← hlast] + exact le_of_not_gt hroom + have h := minimal_topRev_begin_last hread' hmore hrev hbegin hcross + (by omega : 2 ≤ r.end_.offset) hE.symm hne rfl hn hlt (Or.inr hge) + rw [if_neg hroom] + simpa only [← hlast] using h + +/-- **One compact rule for every result at begin offset one.** Dispatch on `res.more` +and on the traversal order: complete results use the three-branch rule (forward or +reversed), truncated forward results use `closedFormOffsetOneTruncated`, and truncated +reversed results use the corrected full rule `closedFormOffsetOneTruncatedRevFullC`. -/ +def closedFormOffsetOneUniversal (r : RangeRead) (res : Result) : ConflictRange := + if res.more = true then + if r.reverse = true then closedFormOffsetOneTruncatedRevFullC r res + else closedFormOffsetOneTruncated r res + else closedFormOffsetOneAny r res + +/-- The universal rule is minimal on the offset-one cross-free region, with no residual +case: the corrected reversed truncated top branch closes the last cell. -/ +theorem minimal_closedForm_of_offset_one_universal {r : RangeRead} {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) : + Minimal r res (closedFormOffsetOneUniversal r res) := by + unfold closedFormOffsetOneUniversal + by_cases hmore : res.more = true + · rw [if_pos hmore] + by_cases hrev : r.reverse = true + · rw [if_pos hrev] + exact minimal_closedForm_of_offset_one_truncated_rev_fullC hread hmore hrev + hbegin hcross + · rw [if_neg hrev] + have hfalse : r.reverse = false := by + cases hr : r.reverse <;> simp_all + exact minimal_closedForm_of_offset_one_truncated hread hmore hfalse hbegin hcross + · rw [if_neg hmore] + have hfalse : res.more = false := by + cases hm : res.more <;> simp_all + exact minimal_closedForm_of_offset_one_any hread hfalse hne hbegin hcross + +/-- The universal rule is unconditional on the automatic region; the extra `hauto` +hypothesis is retained for compatibility with earlier statements. -/ +theorem minimal_closedForm_of_offset_one_universal_of_auto {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)) : + Minimal r res (closedFormOffsetOneUniversal r res) := + minimal_closedForm_of_offset_one_universal hread hne hbegin hcross + +/-- The region version of the universal rule; the extra `hcell` hypothesis is retained +for compatibility with earlier statements. -/ +theorem minimal_closedForm_of_offset_one_universal_of_not_cell {r : RangeRead} + {res : Result} + (hread : Readable r res) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 1) (hcross : r.begin.key ≤ r.end_.key) + (hcell : ¬ (r.reverse = true ∧ res.more = true ∧ 2 ≤ r.end_.offset ∧ + r.end_.key ≤ ((res.keys.headD ([] : Key) : Key) : WithTop Key))) : + Minimal r res (closedFormOffsetOneUniversal r res) := + minimal_closedForm_of_offset_one_universal hread hne hbegin hcross + +/-! +## The crossed region at begin offset one + +The remaining cell of the offset-one closed form is the *crossed* selector pair +`end_.key < begin_.key`. There the complete window starts at the first stored key at or +above `begin_.key`, while the end resolution counts stored keys from `end_.key`; the +minimal range therefore starts at `end_.key`, not at `begin_.key`. The lemmas below +supply the crossed counterparts of the finite/top branch analysis. +-/ + +/-- **The length of the complete window at begin offset one.** This is the general +(not necessarily cross-free) form of `completeKeys_count_ge_eq_min`: the window length +is the end anchor index minus the count of stored keys below `begin_.key`. -/ +theorem completeKeys_length_of_offset_one {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).length = + min (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db.keys.length + - db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by + rw [completeKeys_eq_filter_take_of_offset_one hrev hbegin hend, List.length_take] + have hflen : (db.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).length = + db.keys.length - db.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by + have hEq : db.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + db.keys.filter (fun k : Key => decide (¬ ((k : WithTop Key) < r.begin.key))) := by + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact not_lt.symm + rw [hEq, filter_not_lt_eq_drop_countP db.sorted r.begin.key, List.length_drop] + rw [hflen] + exact Nat.min_eq_left (Nat.sub_le_sub_right (Nat.min_le_right _ _) _) + +/-- **The complete window length at any positive offsets.** For a non-reversed read, +the number of complete keys is the end anchor's stored-key index minus the begin +anchor's index. This is the length counterpart of `completeKeys_count_ge_eq_general` +and is valid for crossed selectors as well. -/ +theorem completeKeys_length_general {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).length = + min (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db.keys.length + - (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) + + (r.begin.offset - 1).toNat) := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hanB : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) hbegin, hp, hb, hl] + have hA : l.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key))) = + l.drop (p + b) := by + rw [hl, filter_resolve_le_eq_drop (db := db) (s := r.begin), hanB] + set d := l.countP (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) with hd + have hd_eq : d = min (cE + e) l.length := by + rw [hd, countP_lt_resolve] + rw [anchor_toNat_eq (s := r.end_) hend, hcE, he, hl] + have hck : RangeRead.completeKeys db r = (l.drop (p + b)).take (d - (p + b)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + have hdrop : (l.drop (p + b)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p + b) hls.pairwise) + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) = + l.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (k : WithTop Key))) from by + rw [← hl] + exact List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hA, filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) (p + b), ← hd, nat_sub_min_right] + rw [hck, List.length_take, List.length_drop, hd_eq] + exact Nat.min_eq_left (Nat.sub_le_sub_right (Nat.min_le_right _ _) _) + +/-- **In the crossed case every complete key lies at or above `end_.key`.** The begin +resolution is at or above `begin_.key > end_.key`, so the count at or above `end_.key` +is the whole window length. -/ +theorem completeKeys_count_ge_eq_length_of_crossed {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (RangeRead.completeKeys db r).length := by + rw [completeKeys_eq_filter_take_of_offset_one hrev hbegin hend, + List.countP_eq_length_filter, List.filter_eq_self.mpr] + intro k hk + rw [decide_eq_true_eq] + have hk' : k ∈ db.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) := + List.mem_of_mem_take hk + exact le_trans (le_of_lt hcross) + (of_decide_eq_true (List.mem_filter.mp hk').2) + +/-- **The crossed finite branch has an empty `[end_.key, begin_.key)` gap.** If the +number of reported keys at or above `end_.key` equals `end_.offset - 1`, then in a +crossed read at begin offset one no stored key lies in `[end_.key, begin_.key)` and the +end anchor still fits inside the database. -/ +theorem crossed_counts_of_t_eq_e {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hres : res.keys = RangeRead.completeKeys db r) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) ∧ + 0 < (r.end_.offset - 1).toNat := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set e' := (r.end_.offset - 1).toNat with he' + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hcEp : cE ≤ p := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_of_lt_of_le hk (le_of_lt hcross) + have htlen : res.keys.length = e' := by + have hcount := completeKeys_count_ge_eq_length_of_crossed + (db := db) (r := r) hrev hbegin hcross (by omega) + rw [← hres] at hcount + omega + have hlen : min (cE + e') l.length - p = e' := by + have h := completeKeys_length_of_offset_one (db := db) (r := r) hrev hbegin (by omega) + rw [← hres] at h + rw [← hl, ← hp, ← hcE, ← he'] at h + omega + have hp_le_cE : p ≤ cE := by + have h1 : min (cE + e') l.length - p ≤ cE + e' - p := + Nat.sub_le_sub_right (Nat.min_le_left _ _) _ + omega + refine ⟨le_antisymm hcEp hp_le_cE, ?_⟩ + omega + +/-- **The complete window as a drop-and-take at any positive offsets.** For a +non-reversed read, the complete keys are the stored keys after the begin anchor, +truncated after the end anchor. -/ +theorem completeKeys_eq_drop_take_general {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hend : 1 ≤ r.end_.offset) : + RangeRead.completeKeys db r = + (db.keys.drop + (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) + + (r.begin.offset - 1).toNat)).take + (min (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat) db.keys.length + - (db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) + + (r.begin.offset - 1).toNat)) := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hanB : (r.begin.anchor db).toNat = p + b := by + rw [anchor_toNat_eq (s := r.begin) hbegin, hp, hb, hl] + have hA : l.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key))) = + l.drop (p + b) := by + rw [hl, filter_resolve_le_eq_drop (db := db) (s := r.begin), hanB] + set d := l.countP (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) with hd + have hd_eq : d = min (cE + e) l.length := by + rw [hd, countP_lt_resolve] + rw [anchor_toNat_eq (s := r.end_) hend, hcE, he, hl] + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + have hdrop : (l.drop (p + b)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := p + b) hls.pairwise) + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) = + l.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (k : WithTop Key))) from by + rw [← hl] + exact List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter, hA, filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) (p + b), ← hd, nat_sub_min_right, hd_eq] + +/-- **In the crossed case every complete key lies at or above `end_.key` at any +positive begin offset.** -/ +theorem completeKeys_count_ge_eq_length_of_crossed_general {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) : + (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (RangeRead.completeKeys db r).length := by + rw [List.countP_eq_length_filter, List.filter_eq_self.mpr] + intro k hk + rw [decide_eq_true_eq] + have hk' := RangeRead.mem_completeKeys.mp hk + exact le_trans (le_of_lt hcross) + (le_trans (key_le_resolve_of_one_le (db := db) (s := r.begin) hbegin) hk'.2.1) + +/-- **The crossed finite branch at any positive begin offset.** If the number of +reported keys at or above `end_.key`, plus `begin.offset - 1`, is `end_.offset - 1`, +then no stored key lies in the selector gap and the end anchor fits inside the +database. This generalizes `crossed_counts_of_t_eq_e` beyond begin offset one. -/ +theorem crossed_counts_of_t_add_b_eq_e {db : DB} {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) + (hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) + (hne : res.keys ≠ []) + (hres : res.keys = RangeRead.completeKeys db r) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) ∧ + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) + + (r.end_.offset - 1).toNat ≤ db.keys.length := by + set l := db.keys with hl + set p := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hp + set cE := l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) with hcE + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hcEp : cE ≤ p := by + rw [hp, hcE] + apply countP_le_countP_of_imp + intro k _ hk + rw [decide_eq_true_eq] at hk ⊢ + exact lt_trans hk hcross + have htlen : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by + rw [hres] + exact completeKeys_count_ge_eq_length_of_crossed_general (db := db) hrev hbegin hcross hend + have hlen : min (cE + e) l.length - (p + b) = res.keys.length := by + have h := completeKeys_length_general (db := db) (r := r) hrev hbegin hend + rw [← hl, ← hp, ← hcE, ← hb, ← he, ← hres] at h + exact h.symm + have htb : res.keys.length + b = e := by omega + have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne + have hlt : p + b < min (cE + e) l.length := by + have hpos : 0 < min (cE + e) l.length - (p + b) := by omega + exact Nat.lt_of_sub_pos hpos + have hM : min (cE + e) l.length = p + e := by + have hle : p + b ≤ min (cE + e) l.length := le_of_lt hlt + have heq : min (cE + e) l.length = res.keys.length + (p + b) := + (Nat.sub_eq_iff_eq_add hle).mp hlen + omega + have hp_e_le_cEe : p + e ≤ cE + e := by + calc p + e = min (cE + e) l.length := hM.symm + _ ≤ cE + e := Nat.min_le_left _ _ + have hp_e_le_len : p + e ≤ l.length := by + calc p + e = min (cE + e) l.length := hM.symm + _ ≤ l.length := Nat.min_le_right _ _ + refine ⟨le_antisymm hcEp (by omega), ?_⟩ + omega + +/-- The counts below `E`, between `E` and `B`, and at or above `B` partition the list. -/ +theorem countP_lt_add_between {l : List Key} {E B : WithTop Key} (hEB : E ≤ B) : + l.countP (fun k : Key => decide ((k : WithTop Key) < E)) + + l.countP (fun k : Key => decide (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B)) = + l.countP (fun k : Key => decide ((k : WithTop Key) < B)) := by + induction l with + | nil => simp + | cons a t ih => + by_cases haE : ((a : Key) : WithTop Key) < E + · have haB : ((a : Key) : WithTop Key) < B := lt_of_lt_of_le haE hEB + have hbetween : ¬ (E ≤ ((a : Key) : WithTop Key) ∧ ((a : Key) : WithTop Key) < B) := + fun h => not_le.mpr haE h.1 + rw [List.countP_cons_of_pos (by simpa using haE), + List.countP_cons_of_neg (by simpa using hbetween), + List.countP_cons_of_pos (by simpa using haB)] + omega + · by_cases haB : ((a : Key) : WithTop Key) < B + · have hbetween : E ≤ ((a : Key) : WithTop Key) ∧ ((a : Key) : WithTop Key) < B := + ⟨le_of_not_gt haE, haB⟩ + rw [List.countP_cons_of_neg (by simpa using haE), + List.countP_cons_of_pos (by simpa using hbetween), + List.countP_cons_of_pos (by simpa using haB)] + omega + · have hbetween : ¬ (E ≤ ((a : Key) : WithTop Key) ∧ ((a : Key) : WithTop Key) < B) := + fun h => haB h.2 + rw [List.countP_cons_of_neg (by simpa using haE), + List.countP_cons_of_neg (by simpa using hbetween), + List.countP_cons_of_neg (by simpa using haB)] + omega + +/-- If the counts below `E` and below `B` agree (with `E ≤ B`), no stored key lies in +`[E, B)`. -/ +private theorem not_mem_between_of_countP_lt_eq {l : List Key} {E B : WithTop Key} + (hEB : E ≤ B) + (h : l.countP (fun k : Key => decide ((k : WithTop Key) < E)) = + l.countP (fun k : Key => decide ((k : WithTop Key) < B))) : + ∀ k ∈ l, ¬ (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B) := by + have hsum := countP_lt_add_between (l := l) hEB + rw [h] at hsum + have hzero : l.countP (fun k : Key => decide + (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B)) = 0 := by omega + intro k hk hkb + have hpos : 0 < l.countP (fun k : Key => decide + (E ≤ (k : WithTop Key) ∧ (k : WithTop Key) < B)) := + List.countP_pos_iff.mpr ⟨k, hk, by rw [decide_eq_true_eq]; exact hkb⟩ + omega + +/-- **The top range starting at or below both selector keys is sound at positive +offsets.** Agreement on `[lo, ⊤)` fixes both resolutions, hence the complete window. -/ +theorem sound_topRange_of_lo_le {r : RangeRead} {res : Result} {lo : WithTop Key} + (hbegin : 1 ≤ r.begin.offset) (hend : 1 ≤ r.end_.offset) + (hBl : lo ≤ r.begin.key) (hEl : lo ≤ r.end_.key) : + Sound r res ⟨lo, ⊤⟩ := by + intro db₁ db₂ hag + have hagree : ∀ (k : Key), lo ≤ (k : WithTop Key) → + (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + intro k hk + have h1 := congrArg (fun l : List Key => k ∈ l) hag + have hcr : k ∈ (⟨lo, ⊤⟩ : ConflictRange) := ⟨hk, WithTop.coe_lt_top k⟩ + rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff] at h1 + exact ⟨fun hk1 => (h1.mp ⟨hk1, hcr⟩).1, fun hk2 => (h1.mpr ⟨hk2, hcr⟩).1⟩ + have hrb : resolve db₁ r.begin = resolve db₂ r.begin := + resolve_eq_of_agree_ge hbegin fun k hk => hagree k (le_trans hBl hk) + have hre : resolve db₁ r.end_ = resolve db₂ r.end_ := + resolve_eq_of_agree_ge hend fun k hk => hagree k (le_trans hEl hk) + have hwin : (db₁.keys.filter (fun (k : Key) => decide + (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_))) = + (db₂.keys.filter (fun (k : Key) => decide + (resolve db₁ r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db₁ r.end_))) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + · intro k + rw [List.mem_filter, List.mem_filter] + refine ⟨fun hk1 => ⟨?_, hk1.2⟩, fun hk2 => ⟨?_, hk2.2⟩⟩ + · have hklo : lo ≤ (k : WithTop Key) := + le_trans hBl (le_trans (key_le_resolve_of_one_le (db := db₁) (s := r.begin) hbegin) + (of_decide_eq_true hk1.2).1) + exact (hagree k hklo).mp hk1.1 + · have hklo : lo ≤ (k : WithTop Key) := + le_trans hBl (le_trans (key_le_resolve_of_one_le (db := db₁) (s := r.begin) hbegin) + (of_decide_eq_true hk2.2).1) + exact (hagree k hklo).mpr hk2.1 + have hin : RangeRead.inRange db₁.keys (resolve db₁ r.begin) (resolve db₁ r.end_) = + RangeRead.inRange db₂.keys (resolve db₂ r.begin) (resolve db₂ r.end_) := by + unfold RangeRead.inRange + rw [hwin, hrb, hre] + have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by + unfold RangeRead.completeKeys + rw [hin] + exact RangeRead.compatible_congr hck + +def crossFreeAnchor (r : RangeRead) : RangeRead := + { r with begin := { key := r.end_.key, offset := 1 } } + +theorem compatible_crossed_iff_crossFree {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 1) + (hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (crossFreeAnchor r) res := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hres₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hc₀ := crossed_counts_of_t_eq_e (db := db₀) hrev hbegin hcross hend hres₀ ht + have hres₀len : res.keys.length = (r.end_.offset - 1).toNat := by + have hcount := completeKeys_count_ge_eq_length_of_crossed + (db := db₀) (r := r) hrev hbegin hcross (by omega) + rw [← hres₀] at hcount + omega + have hres₀ne : res.keys ≠ [] := hne + intro db + constructor + · intro hcomp + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hc := crossed_counts_of_t_eq_e (db := db) hrev hbegin hcross hend hres ht + have hanchor : r.begin.anchor db = (crossFreeAnchor r).begin.anchor db := by + unfold Selector.anchor crossFreeAnchor + simp only + omega + have hresolve : resolve db r.begin = resolve db ({ key := r.end_.key, offset := 1 } : Selector) := by + unfold resolve + rw [hanchor] + rfl + refine ⟨?_, ?_⟩ + · change r.withinLimit res.keys + exact hcomp.1 + · right + refine ⟨by simp [hmore], ?_⟩ + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db (crossFreeAnchor r) := by + unfold RangeRead.completeKeys RangeRead.inRange crossFreeAnchor + simp only + rw [hresolve] + rw [← hck] + exact hres + · intro hcomp + have hres : res.keys = RangeRead.completeKeys db (crossFreeAnchor r) := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + -- All reported keys, hence all complete keys of the anchor read, are at least E. + have hall_ge : ∀ k ∈ res.keys, r.end_.key ≤ (k : WithTop Key) := by + intro k hk + have hkanchor : k ∈ RangeRead.completeKeys db (crossFreeAnchor r) := by + rw [← hres]; exact hk + have h1 : (crossFreeAnchor r).begin.key ≤ resolve db (crossFreeAnchor r).begin := + key_le_resolve_of_one_le (db := db) (s := (crossFreeAnchor r).begin) + (by simp [crossFreeAnchor]) + have h2 : (crossFreeAnchor r).begin.key = r.end_.key := rfl + exact le_trans (h2 ▸ h1) (RangeRead.mem_completeKeys.mp hkanchor).2.1 + -- and strictly at least `B` (since `res` comes from a crossed read and `t = e`) + have hall_ge_B : ∀ k ∈ res.keys, (k : WithTop Key) ≥ r.begin.key := by + intro k hk + have hk₀ : k ∈ RangeRead.completeKeys db₀ r := by + rw [← hres₀]; exact hk + exact le_trans (key_le_resolve_of_one_le (db := db₀) (s := r.begin) (by omega)) + (RangeRead.mem_completeKeys.mp hk₀).2.1 + -- show no stored key lies in [E, B) + have hno : ∀ k ∈ db.keys, ¬ (r.end_.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by + intro k hk hkb + have hkwin : k ∈ RangeRead.completeKeys db (crossFreeAnchor r) := by + refine RangeRead.mem_completeKeys.mpr ⟨hk, ?_, ?_⟩ + · exact (key_le_resolve_iff_of_offset_one (db := db) (s := (crossFreeAnchor r).begin) + (by simp [crossFreeAnchor]) hk).mp (by simpa [crossFreeAnchor] using hkb.1) + · have hresne : res.keys ≠ [] := hne + have hfirst : (res.keys.head hresne : Key) ∈ res.keys := List.head_mem hresne + have hfirst_anchor : res.keys.head hresne ∈ + RangeRead.completeKeys db (crossFreeAnchor r) := by + rw [← hres]; exact hfirst + have hltres := (RangeRead.mem_completeKeys.mp hfirst_anchor).2.2 + have hfirstB : r.begin.key ≤ ((res.keys.head hresne : Key) : WithTop Key) := + hall_ge_B _ hfirst + exact lt_of_lt_of_le hkb.2 (le_trans hfirstB (le_of_lt hltres)) + rw [← hres] at hkwin + exact absurd (hall_ge_B k hkwin) (not_le.mpr hkb.2) + -- hence the counts below E and below B agree + have hcount : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) := by + have hsum := countP_lt_add_between (l := db.keys) (E := r.end_.key) (B := r.begin.key) + (le_of_lt hcross) + have hzero : db.keys.countP (fun k : Key => decide + (r.end_.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key)) = 0 := by + by_contra hpos + obtain ⟨k, hk, hkdec⟩ := List.countP_pos_iff.mp (Nat.pos_of_ne_zero hpos) + exact hno k hk (of_decide_eq_true hkdec) + omega + have hanchor : r.begin.anchor db = (crossFreeAnchor r).begin.anchor db := by + unfold Selector.anchor crossFreeAnchor + simp only + omega + have hresolve : resolve db r.begin = resolve db ({ key := r.end_.key, offset := 1 } : Selector) := by + unfold resolve + rw [hanchor] + rfl + refine ⟨?_, ?_⟩ + · change r.withinLimit res.keys + exact hcomp.1 + · right + refine ⟨by simp [hmore], ?_⟩ + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db (crossFreeAnchor r) := by + unfold RangeRead.completeKeys RangeRead.inRange crossFreeAnchor + simp only + rw [hresolve] + rw [hck] + exact hres + + + +/-- Soundness is invariant under pointwise equivalence of compatibility. -/ +theorem sound_of_compatible_iff {r r' : RangeRead} {res : Result} {cr : ConflictRange} + (h : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db r' res) : + Sound r res cr ↔ Sound r' res cr := by + constructor + · intro hs db₁ db₂ hagree + rw [← h db₁, ← h db₂] + exact hs db₁ db₂ hagree + · intro hs db₁ db₂ hagree + rw [h db₁, h db₂] + exact hs db₁ db₂ hagree + +/-- Minimality is invariant under pointwise equivalence of compatibility. -/ +theorem minimal_of_compatible_iff {r r' : RangeRead} {res : Result} {cr : ConflictRange} + (h : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db r' res) : + Minimal r' res cr → Minimal r res cr := by + intro hmin + refine ⟨(sound_of_compatible_iff h).mpr hmin.1, ?_⟩ + intro cr' hsub hs + exact hmin.2 cr' hsub ((sound_of_compatible_iff h).mp hs) + +/-- **Crossed finite-branch minimality.** At begin offset one, with crossed selectors and +`t = e`, the finite branch's range starts at `end_.key` rather than `begin_.key`. The +cross-free rule applied to the read whose begin key is moved down to `end_.key` gives +exactly this range, and the two reads have the same compatible databases in this branch. -/ +theorem minimal_truncatedRange_of_finite_crossed {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.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (r.end_.offset - 1).toNat) : + Minimal r res ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + let r' := crossFreeAnchor r + have hiff := compatible_crossed_iff_crossFree hread hrev hmore hne hbegin hcross hend ht + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r' res := ⟨db₀, (hiff db₀).mp hcomp₀⟩ + have hmin : Minimal r' res + ⟨r'.begin.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := + minimal_truncatedRange_of_finite (r := r') hread' hmore hne + (by simpa [r', crossFreeAnchor] using hrev) (by simp [r', crossFreeAnchor]) + (by simp [r', crossFreeAnchor]) hend + (by simpa [r', crossFreeAnchor] using ht) + have hmin' : Minimal r' res + ⟨r.end_.key, (keyAfter (res.keys.getLastD ([] : Key)) : WithTop Key)⟩ := by + simpa [r', crossFreeAnchor] using hmin + exact minimal_of_compatible_iff hiff hmin' + + +/-- The truncated range's stored keys are the stored keys at or above the begin key, +filtered at or below the last reported key. This holds for every forward read; no +offset or resolve condition is needed. -/ +theorem truncatedRange_keys_eq_filter_le {db : DB} {r : RangeRead} {last : Key} : + (truncatedRange r last).keys db = + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + unfold truncatedRange ConflictRange.keys + rw [show db.keys.filter (fun (k : Key) => decide (r.begin.key ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) = + db.keys.filter (fun (k : Key) => + decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key)) && + decide (r.begin.key ≤ (k : WithTop Key))) from + List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter] + apply List.filter_congr + intro k _ + rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff] + +/-- If filtering an ascending list at or below a threshold is a take, then filtering +any prefix of it at or below the threshold is the corresponding shorter take. -/ +theorem filter_take_eq_take_min {α : Type*} {p : α → Bool} {l : List α} {q : Nat} + (h : l.filter p = l.take q) (m : Nat) : + (l.take m).filter p = l.take (min m q) := by + induction l generalizing q m with + | nil => + simp + | cons a rest ih => + cases q with + | zero => + simp only [List.take_zero, List.filter_cons] at h ⊢ + cases hpa : p a with + | true => + simp only [hpa] at h + exact absurd h (by simp) + | false => + simp only [hpa] at h + have hrest : rest.filter p = rest.take 0 := by simpa using h + cases m with + | zero => simp + | succ m => + simp only [List.take_succ_cons, List.filter_cons, hpa, Bool.false_eq_true, + ite_false] + rw [ih hrest m] + simp + | succ q => + cases m with + | zero => simp + | succ m => + simp only [List.take_succ_cons, List.filter_cons] at h ⊢ + cases hpa : p a with + | true => + simp only [hpa] at h ⊢ + have hrest : rest.filter p = rest.take q := (List.cons.inj h).2 + rw [ih hrest m] + simp [Nat.succ_min_succ] + | false => + simp only [hpa, Bool.false_eq_true, ite_false] at h ⊢ + have ha : a ∈ rest.filter p := by + rw [h] + exact List.mem_cons_self .. + exact absurd (List.mem_filter.mp ha).2 (by simp [hpa]) + +/-- **The equal-selector window.** When `begin.key = end_.key` and at least +`(end_.offset - 1).toNat` stored keys lie at or above it, the complete keys are the +stored keys at or above `begin.key`, dropped by `(begin.offset - 1).toNat` and cut to +length `(end_.offset - 1).toNat - (begin.offset - 1).toNat`. Both selectors anchor at +the same count `c` of stored keys below the shared key, so the window is the rank +interval `[c + (begin.offset - 1).toNat, c + (end_.offset - 1).toNat)`. -/ +private theorem completeKeys_eq_drop_take_of_key_eq {db : DB} {r : RangeRead} + (hrev : r.reverse = false) (hbeginB : 1 ≤ r.begin.offset) + (hkey : r.begin.key = r.end_.key) (hend : 1 ≤ r.end_.offset) + (hlen : (r.end_.offset - 1).toNat ≤ + (db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).length) : + RangeRead.completeKeys db r = + ((db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).drop + (r.begin.offset - 1).toNat).take + ((r.end_.offset - 1).toNat - (r.begin.offset - 1).toNat) := by + set l := db.keys with hl + set c := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hc + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hls : l.SortedLT := by rw [hl]; exact db.sorted + have hcE : l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = c := by + rw [hc] + exact List.countP_congr (fun k _ => by rw [hkey]) + have hA : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = l.drop c := by + have hcongr : l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) = + l.filter (fun k : Key => decide (¬ ((k : Key) : WithTop Key) < r.begin.key)) := + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt] + rw [hcongr, filter_not_lt_eq_drop_countP hls r.begin.key, hc] + have hanB : (r.begin.anchor db).toNat = c + b := by + rw [anchor_toNat_eq (s := r.begin) hbeginB, hb] + have hanE : (r.end_.anchor db).toNat = c + e := by + rw [anchor_toNat_eq (s := r.end_) hend, he] + rw [show db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = c from by + rw [← hcE, hl]] + set d := l.countP (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) with hd + have hd_eq : d = c + e := by + have hmin : d = min (r.end_.anchor db).toNat l.length := by + rw [hd, hl] + exact countP_lt_resolve + rw [hmin, hanE] + have hAlen : (l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).length = + l.length - c := by rw [hA, List.length_drop] + have hle : c + e ≤ l.length := by + have hlen' : e ≤ (l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).length := by + simpa [hl] using hlen + rw [hAlen] at hlen' + have hc_le : c ≤ l.length := by + rw [hc] + exact List.countP_le_length + omega + exact Nat.min_eq_left hle + have hck : RangeRead.completeKeys db r = (l.drop (c + b)).take (d - (c + b)) := by + rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])] + have hdrop : (l.drop (c + b)).SortedLT := + List.sortedLT_iff_pairwise.mpr (List.Pairwise.drop (i := c + b) hls.pairwise) + rw [show RangeRead.inRange db.keys (resolve db r.begin) (resolve db r.end_) = + db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) from rfl] + rw [show db.keys.filter + (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) = + l.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_) && + decide (resolve db r.begin ≤ (k : WithTop Key))) from by + rw [← hl] + exact List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]] + rw [← List.filter_filter] + rw [show l.filter (fun k : Key => decide (resolve db r.begin ≤ (k : WithTop Key))) = + l.drop (c + b) from by + rw [hl, filter_resolve_le_eq_drop (db := db) (s := r.begin), hanB]] + rw [filter_lt_eq_take_countP hdrop (resolve db r.end_), + countP_drop_eq hls (resolve db r.end_) (c + b), ← hd, nat_sub_min_right] + rw [hck, hd_eq] + have hdrop_eq : l.drop (c + b) = + ((l.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).drop b) := by + rw [hA, List.drop_drop] + rw [hdrop_eq] + congr 1 + omega + +/-- **Finite branch of the equal-selector complete cell.** When `begin.key = end_.key` +and the reported keys at or above that shared key fill exactly +`(end_.offset - 1) - (begin.offset - 1)` slots, agreement on the truncated range +`[begin.key, keyAfter last)` fixes the complete keys. Both selectors anchor at the +same count of stored keys below the shared key, so the complete window is the rank +interval between the two offsets, while the truncated range sees the shared prefix up +to `last`. -/ +private theorem sound_truncatedRange_of_key_eq_finite_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : 1 ≤ r.begin.offset) + (hkey : r.begin.key = r.end_.key) (hend : 2 ≤ r.end_.offset) + (htpos : 0 < res.keys.length) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + Sound r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hlast] + have hcompof : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ RangeRead.completeKeys db r = res.keys := by + intro db + constructor + · rintro ⟨hlim, hdisj | ⟨_, heq⟩⟩ + · rw [hmore] at hdisj + exact absurd hdisj.1 (by simp) + · exact ⟨hlim, heq.symm⟩ + · rintro ⟨hlim, heq⟩ + exact ⟨hlim, Or.inr ⟨hmore, heq.symm⟩⟩ + set last := res.keys.getLast hne with hlastdef + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have step : ∀ db₁ db₂, AgreeOn (truncatedRange r last) db₁ db₂ → + RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by + intro db₁ db₂ hagree hcomp₁ + rw [hcompof db₁] at hcomp₁ + obtain ⟨hlim, hck₁⟩ := hcomp₁ + rw [hcompof db₂] + -- Every reported key lies at or above the shared selector key. + have hE_all : ∀ k ∈ res.keys, r.end_.key ≤ (k : WithTop Key) := by + intro k hk + have hkck : k ∈ RangeRead.completeKeys db₁ r := by rw [hck₁]; exact hk + have hmem := (RangeRead.mem_completeKeys.mp hkck).2.1 + have hbl : r.begin.key ≤ resolve db₁ r.begin := + key_le_resolve_of_one_le (db := db₁) (s := r.begin) hbegin + have hle : r.begin.key ≤ (k : WithTop Key) := le_trans hbl hmem + rwa [hkey] at hle + have hcount_len : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := + List.countP_eq_length.mpr (fun k hk => by rw [decide_eq_true_eq]; exact hE_all k hk) + have hn : res.keys.length + b = e := by + rw [← hcount_len] + exact ht + have hb_le_e : b ≤ e := by omega + -- The reported last is below the resolved end of `db₁`. + have hlast_mem : last ∈ res.keys := by + rw [hlastdef]; exact List.getLast_mem hne + have hlr₁ : (last : WithTop Key) < resolve db₁ r.end_ := by + have hkck : last ∈ RangeRead.completeKeys db₁ r := by rw [hck₁]; exact hlast_mem + exact (RangeRead.mem_completeKeys.mp hkck).2.2 + have hfil₁ : (RangeRead.completeKeys db₁ r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck₁] + exact filter_le_getLast_eq_of_prefix hsort.pairwise (List.prefix_refl _) hne + have hF₁ := completeKeys_filter_le_drop (db := db₁) (r := r) (last := last) hrev hbegin hlr₁ + rw [hfil₁] at hF₁ + have hTdrop₁ : ((truncatedRange r last).keys db₁).drop b = res.keys := hF₁.symm + -- The truncated range of `db₁` has exactly `e` stored keys. + have hTlen_lower : ((truncatedRange r last).keys db₁).length = b + res.keys.length := by + have hld := congrArg List.length hTdrop₁ + rw [List.length_drop] at hld + have hb_le : b ≤ ((truncatedRange r last).keys db₁).length := by + by_contra hb' + have hzero : ((truncatedRange r last).keys db₁).length - b = 0 := + Nat.sub_eq_zero_of_le (le_of_not_ge hb') + omega + omega + have hTlen_upper : ((truncatedRange r last).keys db₁).length ≤ e := by + have h := filter_length_le_offset_of_resolve_end_gt (db := db₁) (r := r) + (last := last) (by omega) hlr₁ + rw [← hkey] at h + rw [← truncatedRange_keys_eq_filter_le (db := db₁) (r := r) (last := last)] at h + simpa [he] using h + have hTlen₁ : ((truncatedRange r last).keys db₁).length = e := by omega + have hT₂ : (truncatedRange r last).keys db₂ = (truncatedRange r last).keys db₁ := + hagree.symm + have hTlen₂ : ((truncatedRange r last).keys db₂).length = e := by rw [hT₂, hTlen₁] + -- Enough stored keys at or above the shared key for the end selector. + have hlenA₂ : e ≤ (db₂.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).length := by + have hle := List.length_filter_le + (fun k : Key => decide (k ≤ last)) + (db₂.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))) + rw [← truncatedRange_keys_eq_filter_le (db := db₂) (r := r) (last := last)] at hle + omega + have hAsort₂ : (db₂.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).SortedLT := + List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + -- The truncated range is exactly the first `e` keys at or above the shared key. + have htake₂ : (db₂.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).take e = + (truncatedRange r last).keys db₂ := by + have hq : (db₂.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key)))).countP + (fun k : Key => decide (k ≤ last)) = e := by + rw [List.countP_eq_length_filter] + rw [← truncatedRange_keys_eq_filter_le (db := db₂) (r := r) (last := last), hTlen₂] + have hfil := filter_le_eq_take_countP hAsort₂ last + rw [hq] at hfil + exact hfil.symm.trans (truncatedRange_keys_eq_filter_le (db := db₂) (r := r) (last := last)).symm + -- Assemble the complete window of `db₂`. + have hck₂ : RangeRead.completeKeys db₂ r = res.keys := by + rw [completeKeys_eq_drop_take_of_key_eq (db := db₂) hrev hbegin hkey (by omega) hlenA₂] + rw [List.take_drop, show b + (e - b) = e from Nat.add_sub_cancel' hb_le_e] + rw [htake₂, hT₂, hTdrop₁] + exact ⟨hlim, hck₂⟩ + intro db₁ db₂ hagree + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + +/-- **Finite branch of the equal-selector complete cell, minimality.** Under the +count identity `t + (begin.offset - 1) = end.offset - 1`, the truncated range is the +minimal sound range; necessity is the begin key and the last reported key, exactly as +in the general finite branch. -/ +theorem minimal_truncatedRange_of_key_eq_finite_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) + (hkey : r.begin.key = r.end_.key) (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat) : + Minimal r res (truncatedRange r (res.keys.getLastD ([] : Key))) := by + obtain ⟨db₀, hcomp⟩ := hread + have hgetLast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by + rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl + rw [hgetLast] + have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := + RangeRead.compatible_keys_prefix hcomp + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · rw [← hgetLast] + exact sound_truncatedRange_of_key_eq_finite_general ⟨db₀, hcomp⟩ hrev hmore hne hbegin + hkey hend (List.length_pos_of_ne_nil hne) ht + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (hpre₀.ne_nil hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + have hkle : k ≤ res.keys.getLast hne := + (lt_keyAfter_iff k (res.keys.getLast hne)).mp (WithTop.coe_lt_coe.mp hk.2) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + +/-- **Sentinel end in the equal-selector top branch.** When `begin.key = end_.key` +and the reported keys at or above the shared key fill fewer than +`(end_.offset - 1) - (begin.offset - 1)` slots, the resolved end of every compatible +database is the sentinel. Otherwise the complete window would be the rank interval +`[c + b, c + e)` of the stored keys at or above the shared key, so its reported keys +would number exactly `e - b`. -/ +private theorem resolve_end_eq_top_of_key_eq_top_general {db : DB} {r : RangeRead} + {res : Result} (hcomp : RangeRead.Compatible db 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) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) : + resolve db r.end_ = (⊤ : WithTop Key) := by + by_contra hnotop + have hck : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + set A := db.keys.filter + (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hA + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hb_lt_e : b < e := by omega + -- The end selector's filtered list is `A`, since the two keys agree. + have hres_end := resolve_eq_filter_getElem? (db := db) (s := r.end_) + (by omega : 1 ≤ r.end_.offset) + have hf : (db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))) = A := by + rw [hA, hkey] + rw [hf] at hres_end + -- A finite end means the `e`-th key of `A` exists. + have hlen : e ≤ A.length := by + cases hget : A[e]? with + | none => + rw [hget] at hres_end + exact absurd hres_end hnotop + | some k => + have hlt := (List.getElem?_eq_some_iff.mp hget).1 + omega + have hW := completeKeys_eq_drop_take_of_key_eq (db := db) hrev hbegin hkey + (by omega) hlen + have hWlen : (RangeRead.completeKeys db r).length = e - b := by + rw [hW, List.length_take, List.length_drop] + have hle : e - b ≤ A.length - b := by omega + rw [Nat.min_eq_left hle] + have hcountW : (RangeRead.completeKeys db r).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (RangeRead.completeKeys db r).length := by + rw [hW, List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + have hkA : k ∈ A := + List.mem_of_mem_drop (List.mem_of_mem_take hk) + have hkb : r.begin.key ≤ (k : WithTop Key) := + of_decide_eq_true (List.mem_filter.mp hkA).2 + rwa [← hkey] + have ht' : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = e - b := by + rw [hck, hcountW, hWlen] + omega + +/-- **Strict top margin in the equal-selector top branch.** If the reported keys at or +above the shared key fall short of the offset gap, the compatible database has strictly +fewer stored keys than the count below `end_.key` plus the end offset. This is the margin +needed to keep the end selector at the sentinel after inserting a fresh key. -/ +private theorem key_eq_top_strict_margin {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db 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) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) : + db.keys.length < + db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := by + have hend : 2 ≤ r.end_.offset := by omega + have hE := resolve_end_eq_top_of_key_eq_top_general hcomp hmore hrev hne hbegin hkey hend ht + have hck : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + set l := db.keys with hl + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + set c := l.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) with hc + set A := (topRange r).keys db with hA + have hAs : A.SortedLT := by + rw [hA] + unfold topRange ConflictRange.keys + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + have hckDrop : RangeRead.completeKeys db r = A.drop b := by + rw [completeKeys_eq_topRange_keys_drop (db := db) hrev hbegin hE, hA] + have hb_le : b ≤ A.length := by + have hdne : A.drop b ≠ [] := by + intro hnil + apply hne + rw [hck, hckDrop, hnil] + by_contra h + exact hdne (List.drop_eq_nil_iff.mpr (by omega)) + have hcount : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = A.length - b := by + rw [hck, hckDrop] + have hcl : (A.drop b).countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = (A.drop b).length := by + rw [List.countP_eq_length] + intro k hk + rw [decide_eq_true_eq] + have hkA : k ∈ A := List.mem_of_mem_drop hk + have hkb : r.begin.key ≤ (k : WithTop Key) := by + rw [hA] at hkA + have h := of_decide_eq_true (List.mem_filter.mp hkA).2 + simpa [topRange] using h.1 + rwa [← hkey] + rw [hcl, List.length_drop] + have hAlen : A.length = l.length - c := by + rw [hA] + unfold topRange ConflictRange.keys + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < (⊤ : WithTop Key))) = + l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide]; exact ⟨fun h => h.1, + fun h => ⟨h, WithTop.coe_lt_top k⟩⟩] + rw [show l.filter (fun (k : Key) => decide (r.begin.key ≤ (↑k : WithTop Key))) = + l.filter (fun (k : Key) => decide (¬ ((↑k : WithTop Key) < r.begin.key))) from + List.filter_congr fun k _ => by rw [decide_eq_decide, not_lt]] + rw [filter_not_lt_eq_drop_countP (by rw [hl]; exact db.sorted) r.begin.key, hc, + List.length_drop] + have hcE : l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = c := by + rw [hc] + exact List.countP_congr (fun k _ => by rw [hkey]) + have hA_lt : A.length < e := by + have := ht + rw [hcount] at this + omega + have hlen_eq : l.length = c + A.length := by + rw [hAlen] + have hc_le : c ≤ l.length := by rw [hc]; exact List.countP_le_length + omega + calc l.length = c + A.length := hlen_eq + _ < c + e := by omega + _ = l.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + e := by rw [hcE] + +/-- **Top-branch necessity above the reported last, equal selectors.** When +`begin.key = end_.key` and the reported keys at or above the shared key fall short of +the offset gap, every key at or above the last reported key is necessary. The sentinel +end is forced by `resolve_end_eq_top_of_key_eq_top_general`, the strict margin by +`key_eq_top_strict_margin`, and inserting a fresh key above the last then preserves the +sentinel while extending the complete window. -/ +private theorem necessary_of_key_eq_top_general {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : 1 ≤ r.begin.offset) (hkey : r.begin.key = r.end_.key) + (hend : 2 ≤ r.end_.offset) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) + {x : Key} (hlo : res.keys.getLast hne ≤ x) : + Necessary r res x := by + obtain ⟨db₀, hcomp⟩ := hread + intro cr hs + by_cases hx : x ∈ res.keys + · exact necessary_of_mem_result ⟨db₀, hcomp⟩ hx cr hs + · by_contra hcr + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hmaxres : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hE₀ : resolve db₀ r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_key_eq_top_general hcomp hmore hrev hne hbegin hkey hend ht + have hL_lt : db₀.keys.length < + db₀.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) + + (r.end_.offset - 1).toNat := + key_eq_top_strict_margin hcomp hmore hrev hne hbegin hkey ht + set b := (r.begin.offset - 1).toNat with hb + set A := (topRange r).keys db₀ with hA + have hAs : A.SortedLT := by + rw [hA] + unfold topRange ConflictRange.keys + exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _) + have hckDrop : RangeRead.completeKeys db₀ r = A.drop b := by + rw [completeKeys_eq_topRange_keys_drop (db := db₀) hrev hbegin hE₀, hA] + have hdne : A.drop b ≠ [] := by + intro hnil + apply hne + rw [hres, hckDrop, hnil] + have hb_lt : b < A.length := by + by_contra h + exact hdne (List.drop_eq_nil_iff.mpr (by omega)) + have hhead_drop : (A.drop b).head hdne = A[b] := List.head_drop hdne + have hhead_mem : A[b] ∈ res.keys := by + rw [← hhead_drop, hres, hckDrop] + exact List.head_mem hdne + have hbegin_le_last : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := + le_trans (begin_key_le_head_of_one_le_offset ⟨db₀, hcomp⟩ hne hbegin) + (WithTop.coe_le_coe.mpr (hmaxres _ (List.head_mem hne))) + have hall : ∀ k ∈ db₀.keys, k ≤ x := by + intro k hk + by_cases hbk : r.begin.key ≤ ((k : Key) : WithTop Key) + · have hkA : k ∈ A := by + rw [hA, ConflictRange.mem_keys_iff] + exact ⟨hk, hbk, WithTop.coe_lt_top k⟩ + have hkappend : k ∈ A.take b ++ A.drop b := by + rw [List.take_append_drop] + exact hkA + rcases List.mem_append.mp hkappend with hktake | hkdrop + · obtain ⟨i, hi, hik⟩ := List.mem_take_iff_getElem.mp hktake + have hi_lt_b : i < b := lt_of_lt_of_le hi (Nat.min_le_left _ _) + have hi_lt_A : i < A.length := lt_of_lt_of_le hi (Nat.min_le_right _ _) + have hile : A[i] ≤ A[b] := (hAs.getElem_le_getElem_iff).mpr (by omega) + have hk_le_head : k ≤ A[b] := by rw [← hik]; exact hile + exact le_trans hk_le_head (le_trans (hmaxres _ hhead_mem) hlo) + · have hkres : k ∈ res.keys := by rw [hres, hckDrop]; exact hkdrop + exact le_trans (hmaxres k hkres) hlo + · have hklt : ((k : Key) : WithTop Key) < r.begin.key := lt_of_not_ge hbk + have hkx : ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := + le_trans (le_of_lt hklt) (le_trans hbegin_le_last (WithTop.coe_le_coe.mpr hlo)) + exact WithTop.coe_le_coe.mp hkx + have hx0 : x ∉ db₀.keys := by + intro hxm + by_cases hbx : r.begin.key ≤ ((x : Key) : WithTop Key) + · have hxA : x ∈ A := by + rw [hA, ConflictRange.mem_keys_iff] + exact ⟨hxm, hbx, WithTop.coe_lt_top x⟩ + have hxappend : x ∈ A.take b ++ A.drop b := by + rw [List.take_append_drop] + exact hxA + rcases List.mem_append.mp hxappend with hxtake | hxdrop + · obtain ⟨i, hi, hix⟩ := List.mem_take_iff_getElem.mp hxtake + have hi_lt_b : i < b := lt_of_lt_of_le hi (Nat.min_le_left _ _) + have hi_lt_A : i < A.length := lt_of_lt_of_le hi (Nat.min_le_right _ _) + have hile : A[i] ≤ A[b] := (hAs.getElem_le_getElem_iff).mpr (by omega) + have hx_le_head : x ≤ A[b] := by rw [← hix]; exact hile + have hx_eq_head : x = A[b] := + le_antisymm hx_le_head (le_trans (hmaxres _ hhead_mem) hlo) + exact hx (hx_eq_head ▸ hhead_mem) + · exact hx (by rw [hres, hckDrop]; exact hxdrop) + · have hxlt : ((x : Key) : WithTop Key) < r.begin.key := lt_of_not_ge hbx + have hxltlast : x < res.keys.getLast hne := + WithTop.coe_lt_coe.mp (lt_of_lt_of_le hxlt hbegin_le_last) + exact absurd hlo (not_le.mpr hxltlast) + have hEins : resolve (db₀.insert x hx0) r.end_ = (⊤ : WithTop Key) := + resolve_end_eq_top_of_insert_top hx0 hL_lt hend + exact absurd (mem_result_of_insert hcomp hs hcr hmore hne hx0 + (fun k hk => WithTop.coe_le_coe.mpr (hall k hk)) hEins) hx + +/-- **Equal-selector top branch, minimality.** When `begin.key = end_.key` and the +reported keys at or above the shared key satisfy `t + (begin.offset - 1) < +end_.offset - 1`, the unbounded top range `[begin.key, ⊤)` is the minimal sound +conflict range. -/ +theorem minimal_topRange_of_key_eq_top_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) + (ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat < (r.end_.offset - 1).toNat) : + Minimal r res (topRange r) := by + obtain ⟨db₀, hcomp⟩ := hread + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hbk_ne_top : r.begin.key ≠ (⊤ : WithTop Key) := + begin_key_ne_top_of_completeKeys_ne_nil hrev (by rw [← hres]; exact hne) hbegin + obtain ⟨v, hv⟩ := exists_begin_key_of_ne_top hbk_ne_top + refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩ + · exact sound_topRange_of_one_le_le hbegin (by omega) (le_of_eq hkey) + · intro k hk cr hs + have hlast_nec : Necessary r res (res.keys.getLast hne) := + necessary_of_mem_result ⟨db₀, hcomp⟩ (List.getLast_mem hne) + by_cases hkle : k ≤ res.keys.getLast hne + · have hbegin_nec : Necessary r res v := + necessary_begin_key ⟨db₀, hcomp⟩ hrev (List.length_pos_of_ne_nil hne) hbegin hv + have hvk : v ≤ k := WithTop.coe_le_coe.mp (hv ▸ hk.1) + exact hbegin_nec.of_between hlast_nec hvk hkle cr hs + · exact necessary_of_key_eq_top_general ⟨db₀, hcomp⟩ hrev hmore hne hbegin hkey hend ht + (le_of_lt (lt_of_not_ge hkle)) cr hs + +/-- **Equal-selector count bound.** For a nonempty complete result with +`begin.key = end_.key`, the number `t` of reported keys at or above the shared key plus +`(begin.offset - 1)` is at most `(end_.offset - 1)`. The end selector either lands at a +finite key, where the complete window is a `take` of `A.drop b`, or overshoots, where the +window is all of `A.drop b` and the overshoot gives `|A| ≤ e`. -/ +private theorem key_eq_count_add_le {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db 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 : 1 ≤ r.end_.offset) : + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat ≤ (r.end_.offset - 1).toNat := by + have hck : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + set A := db.keys.filter (fun k : Key => decide (r.begin.key ≤ (k : WithTop Key))) with hA + set b := (r.begin.offset - 1).toNat with hb + set e := (r.end_.offset - 1).toNat with he + have hres_end := resolve_eq_filter_getElem? (db := db) (s := r.end_) hend + have hf : (db.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))) = A := by + rw [hA, hkey] + rw [hf] at hres_end + have hcountA : ∀ x ∈ A, r.begin.key ≤ (x : WithTop Key) := by + intro x hx + rw [hA] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + cases hget : A[e]? with + | some k => + have hlen : e ≤ A.length := by + have := (List.getElem?_eq_some_iff.mp hget).1 + omega + have hW := completeKeys_eq_drop_take_of_key_eq (db := db) hrev hbegin hkey + (by omega) hlen + have ht_eq : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + ((A.drop b).take (e - b)).length := by + rw [hck, hW] + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + have hxA : x ∈ A := List.mem_of_mem_drop (List.mem_of_mem_take hx) + have hxb : r.begin.key ≤ (x : WithTop Key) := hcountA x hxA + rwa [← hkey] + have hb_lt_e : b < e := by + by_contra h + have htake : (A.drop b).take (e - b) = [] := by + rw [show e - b = 0 by omega] + rfl + apply hne + rw [hck, hW] + simpa [← he, ← hb] using htake + rw [ht_eq, List.length_take] + have hle : min (e - b) (A.length - b) ≤ e - b := Nat.min_le_left _ _ + omega + | none => + have hlen_none : A.length ≤ e := List.getElem?_eq_none_iff.mp hget + have hE : resolve db r.end_ = (⊤ : WithTop Key) := by + rw [hres_end, hget] + have hAtop : (topRange r).keys db = A := by + rw [hA] + unfold topRange ConflictRange.keys + exact List.filter_congr fun k _ => by + rw [decide_eq_decide] + exact ⟨fun h => h.1, fun h => ⟨h, WithTop.coe_lt_top k⟩⟩ + have hckDrop : RangeRead.completeKeys db r = A.drop b := by + rw [completeKeys_eq_topRange_keys_drop (db := db) hrev hbegin hE, hAtop, ← hb] + have hb_le : b ≤ A.length := by + by_contra h + have hnil : A.drop b = [] := List.drop_eq_nil_iff.mpr (by omega) + apply hne + rw [hck, hckDrop, hnil] + have ht_eq : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (A.drop b).length := by + rw [hck, hckDrop] + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + have hxA : x ∈ A := List.mem_of_mem_drop hx + have hxb : r.begin.key ≤ (x : WithTop Key) := hcountA x hxA + rwa [← hkey] + rw [ht_eq, List.length_drop] + omega + +/-- **The equal-selector complete closed form.** When `begin.key = end_.key`, the +minimal complete-result conflict range is `[begin.key, keyAfter (last reported))` when +`t + (begin.offset - 1) = end_.offset - 1`, and `[begin.key, ⊤)` otherwise. -/ +def closedFormKeyEqComplete (r : RangeRead) (res : Result) : ConflictRange := + if res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat + then truncatedRange r (res.keys.getLastD ([] : Key)) + else topRange r + +/-- **The equal-selector complete closed form is minimal.** The finite branch is +`minimal_truncatedRange_of_key_eq_finite_general`; the complementary branch has +`t + (begin.offset - 1) < end_.offset - 1` by `key_eq_count_add_le`, so it is the top +branch `minimal_topRange_of_key_eq_top_general`. -/ +theorem minimal_closedForm_of_key_eq_complete_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) : + Minimal r res (closedFormKeyEqComplete r res) := by + have hread' := hread + obtain ⟨db₀, hcomp⟩ := hread + unfold closedFormKeyEqComplete + by_cases ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + + (r.begin.offset - 1).toNat = (r.end_.offset - 1).toNat + · rw [if_pos ht] + exact minimal_truncatedRange_of_key_eq_finite_general hread' hmore hne hrev hbegin + hkey hend ht + · rw [if_neg ht] + have hle := key_eq_count_add_le (db := db₀) hcomp hmore hrev hne hbegin hkey + (by omega) + exact minimal_topRange_of_key_eq_top_general hread' hmore hrev hne hbegin hkey hend + (by omega) + +/-! +## The crossed complete cell at begin offset zero with a nonpositive end offset + +At `begin.offset = 0` a crossed complete result resolves its begin selector onto the +reported head. When the end offset is nonpositive, `resolve end_` is a finite key at or +below `end_.key`, and the complete window is the stored keys in `[head, resolve end_)`. +Agreement on `[head, end_.key)` fixes the top `(-end_.offset)` stored keys below +`end_.key`, hence fixes `resolve end_`; the extra keys between `resolve end_` and +`end_.key` do not enter the window. The range `[head, end_.key)` is therefore sound, and +every key in it is necessary by convexity and by the end-selector necessity +`necessary_last_to_end_key`. +-/ + +/-- **Resolution at a nonpositive offset, read from the top.** At `offset ≤ 0`, the +resolution is the `(-offset)`-th stored key strictly below the selector key, counted from +the top, or the empty key when there are too few. -/ +theorem resolve_eq_filter_lt_reverse {db : DB} {s : Selector} (hoff : s.offset ≤ 0) : + resolve db s = + (match (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < s.key))).reverse[ + (-s.offset).toNat]? with + | some k => ((k : Key) : WithTop Key) + | none => (([] : Key) : WithTop Key)) := by + set m := (-s.offset).toNat with hm + have hoff' : s.offset = -(m : ℤ) := by + rw [hm, Int.toNat_of_nonneg (by omega : (0 : ℤ) ≤ -s.offset)]; ring + set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) with hc + have hanchor : s.anchor db = (c : ℤ) - (m : ℤ) - 1 := by + unfold Selector.anchor; rw [← hc, hoff']; ring + have hflen : (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < s.key))).length = c := by + rw [← List.countP_eq_length_filter, ← hc] + have hclen : c ≤ db.keys.length := by rw [hc]; exact List.countP_le_length + by_cases hmc : m < c + · have h0 : 0 ≤ s.anchor db := by rw [hanchor]; omega + have hancEq : s.anchor db = ((c - 1 - m : ℕ) : ℤ) := by + rw [hanchor] + have : (m : ℤ) + 1 ≤ (c : ℤ) := by exact_mod_cast hmc + omega + have hidx : (s.anchor db).toNat = c - 1 - m := by + rw [hancEq, Int.toNat_natCast] + have hlen : (s.anchor db).toNat < db.keys.length := by rw [hidx]; omega + have hres : resolve db s = ((db.keys[c - 1 - m]'(by omega) : Key) : WithTop Key) := by + rw [resolve_eq_getElem h0 hlen] + apply WithTop.coe_inj.mp + simp only [hidx] + have hsome : (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < s.key))).reverse[m]? = + some (db.keys[c - 1 - m]'(by omega)) := by + rw [List.getElem?_reverse (by rw [hflen]; omega)] + rw [hflen] + rw [getElem?_filter_of_lt_countP db.sorted (by rw [← hc]; omega)] + exact List.getElem?_eq_getElem (by omega) + rw [hres, hsome] + · have hle : c ≤ m := by omega + have hneg : s.anchor db < 0 := by rw [hanchor]; omega + rw [resolve_eq_nil hneg] + have hnone : (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < s.key))).reverse[m]? = none := + List.getElem?_eq_none (by rw [List.length_reverse, hflen]; exact hle) + rw [hnone] + +/-- Splitting the keys strictly below `E` at a genuine key `a` into those below `a` and +those in `[a, E)`. -/ +theorem filter_lt_eq_filter_lt_append_between {l : List Key} (hs : l.SortedLT) + {a : Key} {E : WithTop Key} (haE : (a : WithTop Key) ≤ E) : + l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < E)) = + l.filter (fun (k : Key) => decide ((↑k : WithTop Key) < (a : WithTop Key))) ++ + l.filter (fun (k : Key) => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) := by + induction l with + | nil => rfl + | cons x xs ih => + have hs' : xs.SortedLT := + List.sortedLT_iff_pairwise.mpr (List.pairwise_cons.mp hs.pairwise).2 + have hih := ih hs' + have hx_le : ∀ k ∈ xs, (x : Key) < k := (List.pairwise_cons.mp hs.pairwise).1 + by_cases hxE : ((x : Key) : WithTop Key) < E + · have hFE_pos : (x :: xs).filter (fun k : Key => decide ((↑k : WithTop Key) < E)) = + x :: xs.filter (fun k : Key => decide ((↑k : WithTop Key) < E)) := + List.filter_cons_of_pos (p := fun k : Key => decide ((↑k : WithTop Key) < E)) (a := x) + (decide_eq_true hxE) + by_cases hxa : ((x : Key) : WithTop Key) < ((a : Key) : WithTop Key) + · have hFa_pos : (x :: xs).filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) = + x :: xs.filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) := + List.filter_cons_of_pos (p := fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) (a := x) + (decide_eq_true hxa) + have hG_neg : (x :: xs).filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) = + xs.filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) := + List.filter_cons_of_neg (p := fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) (a := x) (by simp [not_le.mpr hxa]) + rw [hFE_pos, hFa_pos, hG_neg, hih] + rfl + · have hax : ((a : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := le_of_not_gt hxa + have hFa_nil : xs.filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro k hk + have hxk : ((x : Key) : WithTop Key) < (k : WithTop Key) := WithTop.coe_lt_coe.mpr (hx_le k hk) + simp only [decide_eq_true_eq] + exact not_lt.mpr (le_trans hax (le_of_lt hxk)) + have hFa_neg : (x :: xs).filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) = + xs.filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) := + List.filter_cons_of_neg (p := fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) (a := x) + (by simp [hxa]) + have hG_pos : (x :: xs).filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) = + x :: xs.filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) := + List.filter_cons_of_pos (p := fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) (a := x) (decide_eq_true ⟨hax, hxE⟩) + rw [hFE_pos, hFa_neg, hG_pos, hFa_nil, List.nil_append, hih, hFa_nil, List.nil_append] + · have hEx : E ≤ ((x : Key) : WithTop Key) := le_of_not_gt hxE + have hnot_a : ¬ ((x : Key) : WithTop Key) < ((a : Key) : WithTop Key) := + not_lt.mpr (le_trans haE hEx) + have hnot_b : ¬ ((a : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧ + ((x : Key) : WithTop Key) < E) := fun h => hxE h.2 + have hFE_neg : (x :: xs).filter (fun k : Key => decide ((↑k : WithTop Key) < E)) = + xs.filter (fun k : Key => decide ((↑k : WithTop Key) < E)) := + List.filter_cons_of_neg (p := fun k : Key => decide ((↑k : WithTop Key) < E)) (a := x) + (by simp [hxE]) + have hFa_neg : (x :: xs).filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) = + xs.filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) := + List.filter_cons_of_neg (p := fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) (a := x) + (by simp [hnot_a]) + have hG_neg : (x :: xs).filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) = + xs.filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) := + List.filter_cons_of_neg (p := fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) (a := x) (by simp [hxE]) + rw [hFE_neg, hFa_neg, hG_neg, hih] + +/-- A nonpositive-offset selector whose resolution lands at a key `q ≥ a` resolves the same +against databases agreeing on the stored keys in `[a, E)`. -/ +theorem resolve_eq_of_agree_between {db₁ db₂ : DB} {s : Selector} + (hoff : s.offset ≤ 0) {a : Key} {E : WithTop Key} + (hskey : s.key = E) (haE : (a : WithTop Key) ≤ E) + (hagree : db₁.keys.filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) = + db₂.keys.filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E))) + {q : Key} + (hq : (db₁.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < s.key))).reverse[ + (-s.offset).toNat]? = some q) + (hqa : (a : WithTop Key) ≤ ((q : Key) : WithTop Key)) : + resolve db₂ s = ((q : Key) : WithTop Key) := by + set m := (-s.offset).toNat with hm + set A₁ := db₁.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) with hA₁ + set B₁ := db₁.keys.filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) with hB₁ + set F₁ := db₁.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < E)) with hF₁ + have hsplit₁ : F₁ = A₁ ++ B₁ := by + rw [hF₁, hA₁, hB₁] + exact filter_lt_eq_filter_lt_append_between db₁.sorted haE + rw [hskey] at hq + rw [← hF₁] at hq + have hrev₁ : F₁.reverse = B₁.reverse ++ A₁.reverse := by rw [hsplit₁, List.reverse_append] + have hm_lt : m < B₁.length := by + by_contra hcon + have hmge : B₁.length ≤ m := le_of_not_gt hcon + have hright : F₁.reverse[m]? = A₁.reverse[m - B₁.length]? := by + rw [hrev₁, List.getElem?_append_right (l₁ := B₁.reverse) (l₂ := A₁.reverse) (i := m) + (by rwa [List.length_reverse]), List.length_reverse] + rw [hright] at hq + cases hxA : A₁.reverse[m - B₁.length]? with + | none => rw [hxA] at hq; exact absurd hq (by simp) + | some x => + rw [hxA] at hq + have hxmemA : x ∈ A₁ := List.mem_reverse.mp (List.mem_of_getElem? hxA) + have hxa : (x : WithTop Key) < (a : WithTop Key) := + of_decide_eq_true (List.mem_filter.mp hxmemA).2 + have hxq : x = q := by injection hq + rw [hxq] at hxa + exact absurd hqa (not_le.mpr hxa) + have hBq : B₁.reverse[m]? = some q := by + have hleft : F₁.reverse[m]? = B₁.reverse[m]? := by + rw [hrev₁] + exact List.getElem?_append_left (l₁ := B₁.reverse) (l₂ := A₁.reverse) (i := m) + (by rwa [List.length_reverse]) + rwa [hleft] at hq + set A₂ := db₂.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < (a : WithTop Key))) with hA₂ + set B₂ := db₂.keys.filter (fun k : Key => decide ((a : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < E)) with hB₂ + set F₂ := db₂.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < E)) with hF₂ + have hB₂_eq : B₂ = B₁ := by rw [hB₂, hB₁]; exact hagree.symm + have hsplit₂ : F₂ = A₂ ++ B₂ := by + rw [hF₂, hA₂, hB₂] + exact filter_lt_eq_filter_lt_append_between db₂.sorted haE + have hFq : F₂.reverse[m]? = some q := by + have hrev₂ : F₂.reverse = B₂.reverse ++ A₂.reverse := by rw [hsplit₂, List.reverse_append] + rw [hrev₂] + rw [List.getElem?_append_left (l₁ := B₂.reverse) (l₂ := A₂.reverse) (i := m) + (by rw [List.length_reverse, hB₂_eq]; exact hm_lt)] + rw [hB₂_eq] + exact hBq + rw [resolve_eq_filter_lt_reverse (db := db₂) (s := s) hoff, hskey, ← hF₂, ← hm, hFq] + +/-- A resolution at a nonpositive offset that is a genuine key is the entry at the +corresponding index of the reversed filter of keys below the selector key. -/ +theorem resolve_filter_reverse_eq_some_of_ne_nil {db : DB} {s : Selector} + (hoff : s.offset ≤ 0) {q : Key} (hqne : q ≠ []) + (hq : resolve db s = ((q : Key) : WithTop Key)) : + (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < s.key))).reverse[ + (-s.offset).toNat]? = some q := by + rw [resolve_eq_filter_lt_reverse (db := db) (s := s) hoff] at hq + cases h : (db.keys.filter (fun (k : Key) => decide ((↑k : WithTop Key) < s.key))).reverse[ + (-s.offset).toNat]? with + | none => + rw [h] at hq + exact absurd (WithTop.coe_inj.mp hq).symm hqne + | some k => + rw [h] at hq + have hk : k = q := WithTop.coe_inj.mp hq + rw [hk] + +/-- At a nonpositive offset the resolution is never the sentinel `⊤`. -/ +theorem resolve_ne_top_of_offset_nonpos {db : DB} {s : Selector} (hoff : s.offset ≤ 0) : + resolve db s ≠ (⊤ : WithTop Key) := by + intro htop + by_cases hneg : s.anchor db < 0 + · rw [resolve_eq_ite, ite_eq_left hneg] at htop + exact absurd htop (by simp) + · rw [resolve_eq_ite, ite_eq_right hneg] at htop + have h0 : 0 ≤ s.anchor db := le_of_not_gt hneg + have hanc : s.anchor db ≤ (db.keys.length : ℤ) - 1 := by + unfold Selector.anchor + have hc : (db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : ℤ) ≤ + (db.keys.length : ℤ) := by exact_mod_cast List.countP_le_length + omega + have hlt : (s.anchor db).toNat < db.keys.length := by + have hlt' : ((s.anchor db).toNat : ℤ) < (db.keys.length : ℤ) := by + rw [Int.toNat_of_nonneg h0]; omega + exact_mod_cast hlt' + split at htop + · exact absurd htop (by simp) + · rename_i hnone + rw [List.getElem?_eq_getElem hlt] at hnone + exact absurd hnone (by simp) + +/-- **The crossed nonpositive-begin complete under-end at `begin.offset = 0` with a +nonpositive end offset.** A forward complete result at `begin.offset = 0` whose +`begin.key` lies strictly above the reported head, with `end.offset ≤ 0`, has the range +from the reported head up to `end_.key`, `[head, end_.key)`, as its minimal sound conflict +range. -/ +theorem minimal_headToEnd_of_zero_begin_crossed_endNonpos {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset ≤ 0) : + Minimal r res ⟨((res.keys.head hne : Key) : WithTop Key), r.end_.key⟩ := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hreadable : Readable r res := ⟨db₀, hcomp₀⟩ + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db₁ db₂ hagree + have step : ∀ db db', + AgreeOn ⟨((res.keys.head hne : Key) : WithTop Key), r.end_.key⟩ db db' → + RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by + intro db db' hag' hcomp_db + have hlim : RangeRead.withinLimit r res.keys := hcomp_db.1 + have hres_eq : res.keys = RangeRead.completeKeys db r := by + rcases hcomp_db.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored : res.keys.head hne ∈ db.keys := + RangeRead.compatible_keys_subset hcomp_db hhead_mem + 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 + have hhead_lt_E : ((res.keys.head hne : Key) : WithTop Key) < r.end_.key := + lt_of_lt_of_le hhead_lt_res hres_le_E + have hB_le_E : r.begin.key ≤ r.end_.key := by + 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 _)) + · 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) + have hno : ∀ k ∈ db.keys, ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) → + (k : WithTop Key) < r.begin.key → False := by + intro k hk hheadk hkB + have hb := resolve_zero_begin_le_of_mem_below (db := db) hbegin hhead_stored hcross k hk hkB + rw [hL] at hb + exact absurd hb (not_le.mpr hheadk) + have hfilt : db.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key)) = + db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key)) := by + simpa [AgreeOn, ConflictRange.keys] using hag' + have hhead_in_filt : res.keys.head hne ∈ db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key)) := by + rw [← hfilt] + exact List.mem_filter.mpr ⟨hhead_stored, by + rw [decide_eq_true_eq]; exact ⟨le_refl _, hhead_lt_E⟩⟩ + have hhead_stored' : res.keys.head hne ∈ db'.keys := List.mem_of_mem_filter hhead_in_filt + have hno' : ∀ k ∈ db'.keys, ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) → + (k : WithTop Key) < r.begin.key → False := by + intro k hk' hheadk hkB + have hk_in : k ∈ db'.keys.filter (fun k : Key => + decide (((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key)) := + List.mem_filter.mpr ⟨hk', by + rw [decide_eq_true_eq] + exact ⟨le_of_lt hheadk, lt_of_lt_of_le hkB hB_le_E⟩⟩ + rw [← hfilt] at hk_in + exact hno k (List.mem_of_mem_filter hk_in) hheadk hkB + have hL' : resolve db' r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · rcases resolve_eq_nil_or_eq_top_or_mem (db := db') (s := r.begin) with hn | ht | ⟨k, hkdb', hkres⟩ + · rw [hn]; exact coe_nil_le _ + · exact absurd ht (resolve_ne_top_of_offset_nonpos (by rw [hbegin])) + · rw [hkres] + by_contra hnot + have hheadk : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) := + lt_of_not_ge hnot + have hk_lt_B : (k : WithTop Key) < r.begin.key := by + have hb := resolve_zero_begin_lt_key (db := db') hbegin hhead_stored' hcross + rwa [hkres] at hb + exact absurd (hno' k hkdb' hheadk hk_lt_B) (by simp) + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored' hcross + (res.keys.head hne) hhead_stored' hcross + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hn | ht | ⟨q, hqdb, hqeq⟩ + · rw [hn] at hhead_lt_res + exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le _)) + · exact absurd ht (resolve_ne_top_of_offset_nonpos hend) + · have hqne : q ≠ [] := by + intro hqnil + rw [hqnil] at hqeq + rw [hqeq] at hhead_lt_res + exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le _)) + have hqspec : (db.keys.filter (fun k : Key => + decide ((↑k : WithTop Key) < r.end_.key))).reverse[ + (-r.end_.offset).toNat]? = some q := + resolve_filter_reverse_eq_some_of_ne_nil hend hqne hqeq + have hqa : ((res.keys.head hne : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := by + rw [← hqeq]; exact le_of_lt hhead_lt_res + have hR' : resolve db' r.end_ = ((q : Key) : WithTop Key) := + resolve_eq_of_agree_between (db₁ := db) (db₂ := db') (s := r.end_) hend rfl + (le_of_lt hhead_lt_E) hfilt hqspec hqa + have hR : resolve db' r.end_ = resolve db r.end_ := by rw [hR', hqeq] + have hmemE : ∀ k : Key, + (k ∈ db.keys ∧ ((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key) ↔ + (k ∈ db'.keys ∧ ((res.keys.head hne : Key) : WithTop Key) ≤ (↑k : WithTop Key) ∧ + (↑k : WithTop Key) < r.end_.key) := by + intro k + have h := congrArg (fun l : List Key => k ∈ l) hfilt + simp only [List.mem_filter, decide_eq_true_eq] at h + exact Iff.of_eq h + have hck : RangeRead.completeKeys db r = RangeRead.completeKeys db' r := by + unfold RangeRead.completeKeys + rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev])] + unfold RangeRead.inRange + rw [hL, hL', hR] + apply eq_of_forall_mem_iff_of_pairwise + · exact db.sorted.pairwise.filter _ + · exact db'.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk, h1, h2⟩ + exact ⟨((hmemE k).mp ⟨hk, h1, lt_of_lt_of_le h2 hres_le_E⟩).1, h1, h2⟩ + · rintro ⟨hk, h1, h2⟩ + exact ⟨((hmemE k).mpr ⟨hk, h1, lt_of_lt_of_le h2 hres_le_E⟩).1, h1, h2⟩ + exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩ + exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩ + · intro k hlo hhi + have hk_le : res.keys.head hne ≤ k := WithTop.coe_le_coe.mp hlo + by_cases hklast : k ≤ res.keys.getLast hne + · exact necessary_of_between_result hreadable (List.head_mem hne) (List.getLast_mem hne) + hk_le hklast + · exact fun cr hs => necessary_last_to_end_key hreadable hrev hmore hne (by omega) + cr hs (le_of_lt (lt_of_not_ge hklast)) hhi + +/-! +### Reducing a crossed complete read at begin offset zero to offset one + +When the begin offset is zero and the begin key lies strictly above the reported head, +the begin selector resolves onto the head. If in addition the begin key is at or below +`end_.key`, every reported key lies below the begin key, so a database can only store a +key strictly between the head and the begin key if the end window reaches it; that key +would then be reported. Consequently the crossed read has exactly the same compatible +databases as the read whose begin selector is moved to offset one on the reported head. +This lets the offset-one closed form answer the cell. +-/ + +/-- At offset one keyed at a stored key, the selector resolves to that key. -/ +theorem resolve_offset_one_key_of_mem {db : DB} {k : Key} (hk : k ∈ db.keys) : + resolve db ({ key := ((k : Key) : WithTop Key), offset := 1 } : Selector) = + ((k : Key) : WithTop Key) := by + apply le_antisymm + · exact (key_le_resolve_iff_of_offset_one (db := db) + (s := ({ key := ((k : Key) : WithTop Key), offset := 1 } : Selector)) rfl hk).mp + (le_refl _) + · exact key_le_resolve_of_one_le (db := db) + (s := ({ key := ((k : Key) : WithTop Key), offset := 1 } : Selector)) + (by norm_num : (1 : ℤ) ≤ 1) + +/-- `headD` agrees with `head` on a nonempty list. -/ +theorem headD_eq_head_sound {l : List Key} (h : l ≠ []) : + l.headD ([] : Key) = l.head h := by + cases l with + | nil => exact absurd rfl h + | cons a t => rfl + +/-- The offset-one read anchored at the least reported key. -/ +def zeroBeginAnchor (r : RangeRead) (res : Result) : RangeRead := + { r with begin := { key := ((res.keys.headD ([] : Key) : Key) : WithTop Key), offset := 1 } } + +/-- **Reduction of the crossed complete begin-zero read.** For a nonempty complete +forward result at `begin.offset = 0` with `head < begin.key ≤ end.key` and a positive +end offset, the compatible databases are exactly those of `zeroBeginAnchor`, whose begin +selector is at offset one on the reported head. -/ +theorem compatible_zero_crossed_iff_anchor {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hBE : r.begin.key ≤ r.end_.key) (hend : 1 ≤ r.end_.offset) : + ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.Compatible db (zeroBeginAnchor r res) res := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hhead_mem₀ : res.keys.head hne ∈ res.keys := List.head_mem hne + have hres₀ : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_stored₀ : res.keys.head hne ∈ db₀.keys := + (RangeRead.mem_completeKeys.mp (by rw [← hres₀]; exact hhead_mem₀)).1 + have hL₀ : resolve db₀ r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · exact (RangeRead.mem_completeKeys.mp (by rw [← hres₀]; exact hhead_mem₀)).2.1 + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored₀ hcross + (res.keys.head hne) hhead_stored₀ hcross + have hno₀ : ∀ k ∈ db₀.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < + (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by + intro k hk hkb + have h := resolve_zero_begin_le_of_mem_below hbegin hhead_stored₀ hcross k hk hkb.2 + rw [hL₀] at h + exact absurd h (not_le.mpr hkb.1) + have hres_avoid : ∀ k ∈ res.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < + (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by + intro k hk hkb + have hk₀ : k ∈ RangeRead.completeKeys db₀ r := by rw [← hres₀]; exact hk + exact hno₀ k (RangeRead.mem_completeKeys.mp hk₀).1 hkb + have hanchor_key : (zeroBeginAnchor r res).begin.key = + ((res.keys.head hne : Key) : WithTop Key) := by + unfold zeroBeginAnchor + simp only + rw [headD_eq_head_sound hne] + have hanchor_begin : (zeroBeginAnchor r res).begin = + ({ key := ((res.keys.head hne : Key) : WithTop Key), offset := 1 } : Selector) := by + unfold zeroBeginAnchor + simp only + rw [headD_eq_head_sound hne] + have hE_resolve : r.end_.key ≤ resolve db₀ r.end_ := + key_le_resolve_of_one_le (db := db₀) (s := r.end_) hend + intro db + constructor + · intro hcomp + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_stored : res.keys.head hne ∈ db.keys := + (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).1 + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · exact (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).2.1 + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross + (res.keys.head hne) hhead_stored hcross + have hL' : resolve db (zeroBeginAnchor r res).begin = + ((res.keys.head hne : Key) : WithTop Key) := by + rw [hanchor_begin] + exact resolve_offset_one_key_of_mem hhead_stored + refine ⟨?_, ?_⟩ + · simpa [RangeRead.withinLimit, zeroBeginAnchor] using hcomp.1 + · right + refine ⟨hmore, ?_⟩ + have hck : RangeRead.completeKeys db r = + RangeRead.completeKeys db (zeroBeginAnchor r res) := by + unfold RangeRead.completeKeys RangeRead.inRange zeroBeginAnchor + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hL] + congr 1 + funext k + show decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) = + decide (resolve db (zeroBeginAnchor r res).begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) + rw [hL'] + rw [← hck] + exact hres + · intro hcomp + have hres : res.keys = RangeRead.completeKeys db (zeroBeginAnchor r res) := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_stored : res.keys.head hne ∈ db.keys := + (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).1 + have hL' : resolve db (zeroBeginAnchor r res).begin = + ((res.keys.head hne : Key) : WithTop Key) := by + rw [hanchor_begin] + exact resolve_offset_one_key_of_mem hhead_stored + have hE_resolve' : r.end_.key ≤ resolve db (zeroBeginAnchor r res).end_ := by + simpa [zeroBeginAnchor] using key_le_resolve_of_one_le (db := db) (s := r.end_) hend + have hno : ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < + (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by + intro k hk hkb + have hkck : k ∈ RangeRead.completeKeys db (zeroBeginAnchor r res) := by + refine RangeRead.mem_completeKeys.mpr ⟨hk, ?_, ?_⟩ + · rw [hL'] + exact le_of_lt hkb.1 + · exact lt_of_lt_of_le hkb.2 (le_trans hBE hE_resolve') + rw [← hres] at hkck + exact hres_avoid k hkck hkb + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with hn | ht | ⟨q, hq, hqeq⟩ + · rw [hn] + exact coe_nil_le _ + · exact absurd ht (resolve_ne_top_of_offset_nonpos (by rw [hbegin])) + · rw [hqeq] + by_contra hcon + have hqgt : ((res.keys.head hne : Key) : WithTop Key) < (q : WithTop Key) := + lt_of_not_ge hcon + have hqB : (q : WithTop Key) < r.begin.key := by + have hlt := resolve_zero_begin_lt_key (db := db) hbegin hhead_stored hcross + rwa [hqeq] at hlt + exact absurd ⟨hqgt, hqB⟩ (hno q hq) + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross + (res.keys.head hne) hhead_stored hcross + refine ⟨?_, ?_⟩ + · simpa [RangeRead.withinLimit, zeroBeginAnchor] using hcomp.1 + · right + refine ⟨hmore, ?_⟩ + have hck : RangeRead.completeKeys db r = + RangeRead.completeKeys db (zeroBeginAnchor r res) := by + unfold RangeRead.completeKeys RangeRead.inRange zeroBeginAnchor + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hL] + congr 1 + funext k + show decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) = + decide (resolve db (zeroBeginAnchor r res).begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) + rw [hL'] + rw [hck] + exact hres + + +end FormalConflictRange diff --git a/FormalConflictRange/ZeroCrossLong.lean b/FormalConflictRange/ZeroCrossLong.lean new file mode 100644 index 0000000..b4ace89 --- /dev/null +++ b/FormalConflictRange/ZeroCrossLong.lean @@ -0,0 +1,1894 @@ +/- +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.Sound +import FormalConflictRange.Empty +import FormalConflictRange.CrossedGeneral + +/-! +# The crossed complete begin-zero cell with `end.key < begin.key` + +At `begin.offset = 0` a crossed complete result (`begin.key` strictly above the +reported head) whose `end.key` lies below `begin.key` is the last complete-result +cell at a nonpositive begin offset. Writing + +* `H` for the reported head and `L` for the reported last, +* `E = end_.key`, `B = begin.key`, `e = (end_.offset - 1).toNat`, +* `t = residualT r res` for the number of reported keys at or above `E`, and +* `g = intervalCard E H`, + +the minimal range is + +* `[min E H, ⊤)` when `t < e` and `e ≤ t + g`, +* `[H, ⊤)` when `t < e` and `e > t + g`, +* `[min E H, max (keyAfter L) B)` when `t = e`. + +The structural core is `completeKeys_eq_filter_zero_crossed`: in every compatible +database the complete window is exactly the stored keys in `[H, keyAfter L)`. The +gap `[H, B)` cannot contain a stored key, and the end resolution is the first stored +key at or above `keyAfter L` (or `⊤`). The lower endpoint is governed by whether the +`e - t` keys needed in `[E, H)` can be placed there without filling the gap; the +upper endpoint is `⊤` exactly when the un-filled gap lets one insert a key above `L` +without changing the end resolution. +-/ + +namespace FormalConflictRange + +/-- The empty key is at or below every `WithTop Key` bound. -/ +theorem coe_nil_le_zero_cross_long (t : WithTop Key) : + ((([] : Key) : WithTop Key)) ≤ t := by + cases t with + | top => exact le_top + | coe k => exact WithTop.coe_le_coe.mpr (nil_le_key k) + +/-- `getLastD` agrees with `getLast` on a nonempty list. -/ +theorem getLastD_eq_getLast_zero_cross_long {l : List Key} (h : l ≠ []) : + l.getLastD ([] : Key) = l.getLast h := by + cases l with + | nil => exact absurd rfl h + | cons a t => + cases t with + | nil => rfl + | cons b t => simp [List.getLastD, List.getLast] + +/-- At begin offset zero, the crossed complete begin resolution is the reported head. -/ +theorem resolve_begin_eq_head_of_zero_crossed {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (_hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db r := by + rw [← hres]; exact hhead_mem + have hhead_db : res.keys.head hne ∈ db.keys := + (RangeRead.mem_completeKeys.mp hhead_ck).1 + exact le_antisymm + (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + (resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross + (res.keys.head hne) hhead_db hcross) + +/-- In a crossed complete begin-zero read, no stored key lies strictly between the +reported head and `begin.key`. -/ +theorem no_stored_between_head_begin_of_zero_crossed {db : DB} {r : RangeRead} + {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : + ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_db : res.keys.head hne ∈ db.keys := + RangeRead.compatible_keys_subset hcomp hhead_mem + have hL := resolve_begin_eq_head_of_zero_crossed hcomp hrev hmore hne hbegin hcross + intro k hk hkb + have hle := resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross k hk hkb.2 + rw [hL] at hle + exact absurd hle (not_le.mpr hkb.1) + +/-- If a database stores `H` below `begin.key`, has no key in `(H, begin.key)`, and +the begin offset is zero, then its begin selector resolves to `H`. -/ +theorem resolve_begin_eq_of_no_between_zero {db : DB} {r : RangeRead} {H : Key} + (h0 : r.begin.offset = 0) (hH : H ∈ db.keys) + (hlt : ((H : Key) : WithTop Key) < r.begin.key) + (hno : ∀ k ∈ db.keys, ¬ ((((H : Key) : WithTop Key)) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) : + resolve db r.begin = ((H : Key) : WithTop Key) := by + apply le_antisymm + · rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with hn | ht | ⟨k, hk, hke⟩ + · rw [hn]; exact coe_nil_le_zero_cross_long _ + · exact absurd ht (resolve_ne_top_of_offset_nonpos (by rw [h0])) + · rw [hke] + by_contra hcon + have hgt : ((H : Key) : WithTop Key) < (k : WithTop Key) := lt_of_not_ge hcon + have hres := resolve_zero_begin_lt_key (db := db) h0 hH hlt + have hkB : (k : WithTop Key) < r.begin.key := by + rw [hke] at hres; exact hres + exact absurd ⟨hgt, hkB⟩ (hno k hk) + · exact resolve_zero_begin_le_of_mem_below h0 hH hlt H hH hlt + +/-- **The crossed complete begin-zero window.** In a compatible database the complete +keys are exactly the stored keys in `[head, keyAfter (last reported))`. In particular +the end resolution is the first stored key at or above `keyAfter (last reported)`, or +`⊤`. -/ +theorem completeKeys_eq_filter_zero_crossed {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : + RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < + (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := by + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hmax : ∀ k ∈ res.keys, k ≤ res.keys.getLast hne := + fun k hk => (hsort.pairwise.imp le_of_lt).rel_getLast hk + have hHL : (res.keys.head hne : Key) ≤ res.keys.getLast hne := + hmax _ hhead_mem + have hLck : res.keys.getLast hne ∈ RangeRead.completeKeys db r := by + rw [← hres]; exact hlast_mem + have hLlt : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db r.end_ := + (RangeRead.mem_completeKeys.mp hLck).2.2 + have hLresolve := resolve_begin_eq_head_of_zero_crossed hcomp hrev hmore hne hbegin hcross + have hno : ∀ k ∈ db.keys, ¬ (((res.keys.getLast hne : Key) : WithTop Key) < + (k : WithTop Key) ∧ (k : WithTop Key) < resolve db r.end_) := by + intro k hk hkb + have hHle : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := + le_trans (WithTop.coe_le_coe.mpr hHL) (le_of_lt hkb.1) + have hkck : k ∈ RangeRead.completeKeys db r := + RangeRead.mem_completeKeys.mpr ⟨hk, by rw [hLresolve]; exact hHle, hkb.2⟩ + have hkres : k ∈ res.keys := by rw [hres]; exact hkck + exact absurd (hmax k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkb.1)) + have hkeyAfter_le : ((keyAfter (res.keys.getLast hne) : Key) : WithTop Key) ≤ + resolve db r.end_ := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hn | ht | ⟨q, hq, hqe⟩ + · rw [hn] at hLlt + exact absurd hLlt (not_lt.mpr (coe_nil_le_zero_cross_long _)) + · rw [ht]; exact le_top + · rw [hqe] + exact keyAfter_le_of_lt (by rw [hqe] at hLlt; exact hLlt) + have hck : RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hLresolve] + rw [hck] + apply List.filter_congr + intro k hk + have hiff : ((((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) ↔ + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := by + constructor + · rintro ⟨h1, h2⟩ + refine ⟨h1, ?_⟩ + by_contra hcon + have hge : (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) ≤ + (k : WithTop Key) := le_of_not_gt hcon + have hLk : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key) := + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt _)) hge + exact absurd ⟨hLk, h2⟩ (hno k hk) + · rintro ⟨h1, h2⟩ + exact ⟨h1, lt_of_lt_of_le h2 hkeyAfter_le⟩ + rw [decide_eq_decide.mpr (hiff)] + +/-! ### Compatibility with the offset-one anchor + +At begin offset zero, a crossed complete read and its offset-one anchor at the +reported head have the same compatible databases on the long-cell region: the read +forbids stored keys in `(head, begin.key)`, and the anchor is the same read with the +begin selector moved to the reported head. -/ + +/-- The begin selector of the offset-one anchor is the offset-one selector at the +reported head. -/ +theorem zeroBeginAnchor_begin_zero_cross_long (r : RangeRead) (res : Result) + (hne : res.keys ≠ []) : + (zeroBeginAnchor r res).begin = + ({ key := ((res.keys.head hne : Key) : WithTop Key), offset := 1 } : Selector) := by + unfold zeroBeginAnchor + simp only + rw [headD_eq_head_sound hne] + +/-- Compatibility passes from a crossed complete begin-zero read to its offset-one +anchor at the reported head. -/ +theorem compatible_anchor_of_zero_crossed {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : + RangeRead.Compatible db (zeroBeginAnchor r res) res := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_stored : res.keys.head hne ∈ db.keys := + (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).1 + have hanchor_begin := zeroBeginAnchor_begin_zero_cross_long r res hne + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply le_antisymm + · exact (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).2.1 + · exact resolve_zero_begin_le_of_mem_below hbegin hhead_stored hcross + (res.keys.head hne) hhead_stored hcross + have hL' : resolve db (zeroBeginAnchor r res).begin = + ((res.keys.head hne : Key) : WithTop Key) := by + rw [hanchor_begin] + exact resolve_offset_one_key_of_mem hhead_stored + refine ⟨?_, ?_⟩ + · simpa [RangeRead.withinLimit, zeroBeginAnchor] using hcomp.1 + · right + refine ⟨hmore, ?_⟩ + have hck : RangeRead.completeKeys db r = + RangeRead.completeKeys db (zeroBeginAnchor r res) := by + unfold RangeRead.completeKeys RangeRead.inRange zeroBeginAnchor + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hL] + congr 1 + funext k + show decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) = + decide (resolve db (zeroBeginAnchor r res).begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) + rw [hL'] + rw [← hck] + exact hres + +/-- Compatibility passes from the offset-one anchor back to a crossed complete +begin-zero read, provided the database stores nothing in `(head, begin.key)`. -/ +theorem compatible_of_anchor_of_no_between {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db (zeroBeginAnchor r res) res) + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hno : ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key)) : + RangeRead.Compatible db r res := by + have hres : res.keys = RangeRead.completeKeys db (zeroBeginAnchor r res) := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_stored : res.keys.head hne ∈ db.keys := + (RangeRead.mem_completeKeys.mp (by rw [← hres]; exact hhead_mem)).1 + have hanchor_begin := zeroBeginAnchor_begin_zero_cross_long r res hne + have hL' : resolve db (zeroBeginAnchor r res).begin = + ((res.keys.head hne : Key) : WithTop Key) := by + rw [hanchor_begin] + exact resolve_offset_one_key_of_mem hhead_stored + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_stored hcross hno + refine ⟨?_, ?_⟩ + · simpa [RangeRead.withinLimit, zeroBeginAnchor] using hcomp.1 + · right + refine ⟨hmore, ?_⟩ + have hck : RangeRead.completeKeys db r = + RangeRead.completeKeys db (zeroBeginAnchor r res) := by + unfold RangeRead.completeKeys RangeRead.inRange zeroBeginAnchor + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hL] + congr 1 + funext k + show decide (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) = + decide (resolve db (zeroBeginAnchor r res).begin ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < resolve db r.end_) + rw [hL'] + rw [hck] + exact hres + +/-- Agreement on a range passes to any subrange. -/ +theorem agreeOn_of_subrange {cr cr' : ConflictRange} (h : Subrange cr' cr) + {db₁ db₂ : DB} (hagree : AgreeOn cr db₁ db₂) : AgreeOn cr' db₁ db₂ := by + rw [agreeOn_iff] at hagree ⊢ + intro k hk + exact hagree k (h k hk) + +/-- **Soundness transfer from the offset-one anchor.** If the anchor's range is sound +for the anchor read and is contained in `cr`, and `cr` covers the reported head and the +whole gap up to `begin.key`, then `cr` is sound for the original read. -/ +theorem sound_of_anchor_sound {r : RangeRead} {res : Result} {cr crA : ConflictRange} + (hrev : r.reverse = false) (hmore : res.more = false) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hlo : cr.begin ≤ ((res.keys.head hne : Key) : WithTop Key)) + (hhi : r.begin.key ≤ cr.end_) + (hsub : Subrange crA cr) + (hsoundA : Sound (zeroBeginAnchor r res) res crA) : + Sound r res cr := by + intro db₁ db₂ hagree + have hAgreeA : AgreeOn crA db₁ db₂ := agreeOn_of_subrange hsub hagree + have hmem_cr : ∀ k : Key, ((res.keys.head hne : Key) : WithTop Key) < + (k : WithTop Key) → (k : WithTop Key) < r.begin.key → k ∈ cr := by + intro k h1 h2 + exact ConflictRange.mem_iff.mpr ⟨le_trans hlo (le_of_lt h1), lt_of_lt_of_le h2 hhi⟩ + have htransfer : ∀ {db db' : DB}, RangeRead.Compatible db r res → + AgreeOn cr db db' → + RangeRead.Compatible db' (zeroBeginAnchor r res) res → + RangeRead.Compatible db' r res := by + intro db db' hc hag hcA' + have hno : ∀ k ∈ db'.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < + (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by + intro k hk hkb + have hno_db := no_stored_between_head_begin_of_zero_crossed hc hrev hmore hne + hbegin hcross + have hkbd : k ∈ db.keys := ((agreeOn_iff cr db db').mp hag) k (hmem_cr k hkb.1 hkb.2) |>.mpr hk + exact hno_db k hkbd hkb + exact compatible_of_anchor_of_no_between hcA' hrev hmore hne hbegin hcross hno + constructor + · intro hc₁ + have hcA₁ := compatible_anchor_of_zero_crossed hc₁ hrev hmore hne hbegin hcross + have hcA₂ := (hsoundA db₁ db₂ hAgreeA).mp hcA₁ + exact htransfer hc₁ hagree hcA₂ + · intro hc₂ + have hcA₂ := compatible_anchor_of_zero_crossed hc₂ hrev hmore hne hbegin hcross + have hcA₁ := (hsoundA db₁ db₂ hAgreeA).mpr hcA₂ + have hagree' : AgreeOn cr db₂ db₁ := AgreeOn.symm hagree + exact htransfer hc₂ hagree' hcA₁ + +/-! ### The long-cell closed form and its anchor + +The explicit rule is the anchor's minimal range with the upper endpoint raised to at +least `begin.key`. Writing `H` for the head, `L` for the last, `E = end_.key`, +`e = (end_.offset - 1).toNat`, `t` for the reported keys at or above `E`, and +`g = intervalCard E H`, it is + +* `[min E H, ⊤)` when `t < e` and `e ≤ t + g`, +* `[H, ⊤)` when `t < e` and `e > t + g`, +* `[min E H, max (keyAfter L) begin.key)` when `t = e`. +-/ + +/-- The anchor-based form of the long-cell value: the offset-one crossed or cross-free +complete rule at the reported head. -/ +noncomputable def zeroCrossLongAnchor (r : RangeRead) (res : Result) : ConflictRange := + if r.end_.key < ((res.keys.headD ([] : Key) : Key) : WithTop Key) then + completeForwardCrossedGeneral (zeroBeginAnchor r res) res + else closedFormOffsetOne (zeroBeginAnchor r res) res + +/-- The crossed complete begin-zero long-cell closed form. It is the anchor's +minimal range with the upper endpoint raised to at least `begin.key`, because keys in +`(head, begin.key)` are never stored but inserting one shifts the begin resolution off +the reported head. Writing `H` for the head, `L` for the last, `E = end_.key`, +`e = (end_.offset - 1).toNat`, `t` for the reported keys at or above `E`, and +`g = intervalCard E H`, the value is + +* `[min E H, ⊤)` when `t < e` and `e ≤ t + g`, +* `[H, ⊤)` when `t < e` and `e > t + g`, +* `[min E H, max (keyAfter L) begin.key)` when `t = e`. +-/ +noncomputable def closedFormZeroCrossLong (r : RangeRead) (res : Result) : ConflictRange := + if r.begin.key = (⊤ : WithTop Key) then + let H : WithTop Key := ((res.keys.headD ([] : Key) : Key) : WithTop Key) + let e := (r.end_.offset - 1).toNat + let t := res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + let g := intervalCard r.end_.key H + ⟨if r.end_.key < H ∧ e - t < g then r.end_.key else H, ⊤⟩ + else + ⟨(zeroCrossLongAnchor r res).begin, max (zeroCrossLongAnchor r res).end_ r.begin.key⟩ + +/-- The anchor-based long-cell value is minimal on the long-cell region. -/ +theorem minimal_zeroCrossLongAnchor {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + Minimal (zeroBeginAnchor r res) res (zeroCrossLongAnchor r res) := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hH : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_sound hne + have hreadA : Readable (zeroBeginAnchor r res) res := + ⟨db₀, compatible_anchor_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross⟩ + have hbeginA : (zeroBeginAnchor r res).begin.offset = 1 := by simp [zeroBeginAnchor] + have hrevA : (zeroBeginAnchor r res).reverse = false := by simp [zeroBeginAnchor, hrev] + unfold zeroCrossLongAnchor + rw [hH] + by_cases hE : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) + · rw [if_pos hE] + have hEA : (zeroBeginAnchor r res).end_.key < (zeroBeginAnchor r res).begin.key := by + change r.end_.key < (zeroBeginAnchor r res).begin.key + rw [zeroBeginAnchor_begin_zero_cross_long r res hne] + exact hE + exact minimal_completeForwardCrossedGeneral hreadA hmore hne hrevA (by omega) hEA + · rw [if_neg hE] + have hle : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key := not_lt.mp hE + have hleA : (zeroBeginAnchor r res).begin.key ≤ (zeroBeginAnchor r res).end_.key := by + change (zeroBeginAnchor r res).begin.key ≤ r.end_.key + rw [zeroBeginAnchor_begin_zero_cross_long r res hne] + exact hle + exact minimal_closedForm_of_offset_one hreadA hmore hne hrevA hbeginA hleA + +/-! ### Endpoint structure of the anchor + +The anchor is either the offset-one crossed complete rule (when `end_.key` lies below +the reported head) or the offset-one cross-free rule. These lemmas record its two +endpoints in terms of the reported head `H`, the reported last `L`, the number `t` of +reported keys at or above `end_.key`, and the end overshoot `e`. -/ + +theorem zeroBeginAnchor_end_key_eq (r : RangeRead) (res : Result) : + (zeroBeginAnchor r res).end_.key = r.end_.key := rfl + +theorem zeroBeginAnchor_end_offset_eq (r : RangeRead) (res : Result) : + (zeroBeginAnchor r res).end_.offset = r.end_.offset := rfl + +theorem zeroBeginAnchor_reported_eq (r : RangeRead) (res : Result) : + reportedAtEnd (zeroBeginAnchor r res) res = reportedAtEnd r res := rfl + +theorem zeroBeginAnchor_overshoot_eq (r : RangeRead) (res : Result) : + endOvershoot (zeroBeginAnchor r res) = endOvershoot r := rfl + +theorem zeroBeginAnchor_interval_eq (r : RangeRead) (res : Result) (H : WithTop Key) : + intervalCard (zeroBeginAnchor r res).end_.key H = intervalCard r.end_.key H := rfl + +theorem zeroBeginAnchor_begin_key_eq (r : RangeRead) (res : Result) + (hne : res.keys ≠ []) : + (zeroBeginAnchor r res).begin.key = ((res.keys.head hne : Key) : WithTop Key) := + congrArg Selector.key (zeroBeginAnchor_begin_zero_cross_long r res hne) + +theorem zeroBeginAnchor_begin_offset_eq (r : RangeRead) (res : Result) : + (zeroBeginAnchor r res).begin.offset = 1 := by simp [zeroBeginAnchor] + +/-- The begin endpoint of the long-cell anchor, with `E < H`. -/ +theorem zeroCrossLongAnchor_begin_crossed {r : RangeRead} {res : Result} + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : + (zeroCrossLongAnchor r res).begin = + if reportedAtEnd r res = endOvershoot r ∨ + (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) + then r.end_.key else ((res.keys.head hne : Key) : WithTop Key) := by + unfold zeroCrossLongAnchor + rw [headD_eq_head_sound hne, if_pos hEH] + show (completeForwardCrossedGeneral (zeroBeginAnchor r res) res).begin = _ + unfold completeForwardCrossedGeneral + rw [if_neg (by rw [zeroBeginAnchor_end_offset_eq]; omega)] + rw [zeroBeginAnchor_reported_eq, zeroBeginAnchor_overshoot_eq, + zeroBeginAnchor_begin_offset_eq, show (((1 : ℤ) - 1).toNat) = 0 by norm_num, + Nat.add_zero, zeroBeginAnchor_begin_key_eq r res hne] + rw [zeroBeginAnchor_interval_eq] + by_cases ht : reportedAtEnd r res = endOvershoot r + · rw [if_pos ht, if_pos (Or.inl ht), zeroBeginAnchor_end_key_eq] + · rw [if_neg ht] + have hso : crossedOvershootGeneral (zeroBeginAnchor r res) res = + endOvershoot r - reportedAtEnd r res := by + unfold crossedOvershootGeneral + rw [zeroBeginAnchor_overshoot_eq, zeroBeginAnchor_reported_eq, + zeroBeginAnchor_begin_offset_eq, show (((1 : ℤ) - 1).toNat) = 0 by norm_num, + Nat.sub_zero] + rw [hso] + by_cases hc : (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) + · rw [if_pos hc, if_pos (Or.inr hc), zeroBeginAnchor_end_key_eq] + · rw [if_neg hc, if_neg (fun h => h.elim ht hc)] + +/-- The end endpoint of the long-cell anchor, with `E < H`. -/ +theorem zeroCrossLongAnchor_end_crossed {r : RangeRead} {res : Result} + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) + (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : + (zeroCrossLongAnchor r res).end_ = + if reportedAtEnd r res = endOvershoot r + then (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) else ⊤ := by + unfold zeroCrossLongAnchor + rw [headD_eq_head_sound hne, if_pos hEH] + show (completeForwardCrossedGeneral (zeroBeginAnchor r res) res).end_ = _ + unfold completeForwardCrossedGeneral + rw [if_neg (by rw [zeroBeginAnchor_end_offset_eq]; omega)] + rw [zeroBeginAnchor_reported_eq, zeroBeginAnchor_overshoot_eq, + zeroBeginAnchor_begin_offset_eq, show (((1 : ℤ) - 1).toNat) = 0 by norm_num, + Nat.add_zero] + by_cases ht : reportedAtEnd r res = endOvershoot r + · rw [if_pos ht, if_pos ht] + · rw [if_neg ht, if_neg ht] + +/-- The begin endpoint of the long-cell anchor, with `H ≤ E`. -/ +theorem zeroCrossLongAnchor_begin_crossfree {r : RangeRead} {res : Result} + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) + (hEH : ¬ (r.end_.key < ((res.keys.head hne : Key) : WithTop Key))) : + (zeroCrossLongAnchor r res).begin = (((res.keys.head hne : Key)) : WithTop Key) := by + unfold zeroCrossLongAnchor + rw [headD_eq_head_sound hne, if_neg hEH] + show (closedFormOffsetOne (zeroBeginAnchor r res) res).begin = _ + unfold closedFormOffsetOne + rw [if_neg (by rw [zeroBeginAnchor_end_offset_eq]; omega)] + by_cases ht : res.keys.countP + (fun k : Key => decide ((zeroBeginAnchor r res).end_.key ≤ (k : WithTop Key))) = + ((zeroBeginAnchor r res).end_.offset - 1).toNat + · rw [if_pos ht] + simp [truncatedRange, zeroBeginAnchor_begin_key_eq r res hne] + · rw [if_neg ht] + simp [topRange, zeroBeginAnchor_begin_key_eq r res hne] + +/-- The end endpoint of the long-cell anchor, with `H ≤ E`. -/ +theorem zeroCrossLongAnchor_end_crossfree {r : RangeRead} {res : Result} + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) (hend : 2 ≤ r.end_.offset) + (hEH : ¬ (r.end_.key < ((res.keys.head hne : Key) : WithTop Key))) : + (zeroCrossLongAnchor r res).end_ = + if reportedAtEnd r res = endOvershoot r + then (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) else ⊤ := by + unfold zeroCrossLongAnchor + rw [headD_eq_head_sound hne, if_neg hEH] + show (closedFormOffsetOne (zeroBeginAnchor r res) res).end_ = _ + unfold closedFormOffsetOne + rw [if_neg (by rw [zeroBeginAnchor_end_offset_eq]; omega)] + have hcnt : res.keys.countP + (fun k : Key => decide ((zeroBeginAnchor r res).end_.key ≤ (k : WithTop Key))) = + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by + rw [zeroBeginAnchor_end_key_eq] + have hoff : ((zeroBeginAnchor r res).end_.offset - 1).toNat = endOvershoot r := by + rw [zeroBeginAnchor_end_offset_eq]; rfl + by_cases ht : res.keys.countP + (fun k : Key => decide ((zeroBeginAnchor r res).end_.key ≤ (k : WithTop Key))) = + ((zeroBeginAnchor r res).end_.offset - 1).toNat + · rw [if_pos ht, if_pos (by unfold reportedAtEnd; rw [← hcnt, ← hoff]; exact ht)] + simp [truncatedRange] + · rw [if_neg ht, if_neg (by unfold reportedAtEnd; rw [← hcnt, ← hoff]; exact ht)] + simp [topRange] + +/-- At `begin.offset = 0` and `begin.key = ⊤`, the begin selector resolves to the +greatest stored key. -/ +theorem resolve_begin_eq_getLast_of_top {db : DB} {r : RangeRead} + (h0 : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) + (hpos : 0 < db.keys.length) : + resolve db r.begin = + ((db.keys.getLast (List.ne_nil_of_length_pos hpos) : Key) : WithTop Key) := by + set hne : db.keys ≠ [] := List.ne_nil_of_length_pos hpos with hne_def + set M : Key := db.keys.getLast hne with hM + have hanc : Selector.anchor db r.begin = (db.keys.length : Int) - 1 := by + rw [anchor_of_top htop]; omega + have h0le : 0 ≤ Selector.anchor db r.begin := by rw [hanc]; omega + have hidx : (Selector.anchor db r.begin).toNat = db.keys.length - 1 := by + rw [hanc] + have hcast : (db.keys.length : Int) - 1 = ((db.keys.length - 1 : ℕ) : Int) := by omega + rw [hcast, Int.toNat_natCast] + have hget? : db.keys[(Selector.anchor db r.begin).toNat]? = some M := by + rw [hidx, ← List.getLast?_eq_getElem?] + rw [hM] + exact (List.getLast_eq_iff_getLast?_eq_some hne).mp rfl + rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0le), hget?] + +/-- `resolve` is `⊤` exactly when the anchor index is at or past the end. -/ +theorem resolve_eq_top_iff_le_anchor {db : DB} {s : Selector} + (h0 : 0 ≤ Selector.anchor db s) : + resolve db s = (⊤ : WithTop Key) ↔ db.keys.length ≤ (Selector.anchor db s).toNat := by + constructor + · intro h + by_contra hlt + have hlt' : (Selector.anchor db s).toNat < db.keys.length := not_le.mp hlt + rw [resolve_eq_getElem h0 hlt'] at h + exact WithTop.coe_ne_top h + · intro h; exact resolve_eq_top h0 h + +/-- The end selector resolves past the end exactly when at most `end.offset - 1` stored +keys lie at or above `end.key`. -/ +theorem resolve_end_eq_top_iff_above_le {db : DB} {r : RangeRead} + (hend : 1 ≤ r.end_.offset) : + resolve db r.end_ = (⊤ : WithTop Key) ↔ + (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := by + rw [resolve_eq_top_iff_le_anchor (anchor_nonneg_of_one_le hend), anchor_toNat_eq hend] + have hco : db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) = + cnt db r.end_.key := rfl + rw [hco] + exact length_le_cnt_add_iff_above_le db r.end_.key _ + +/-- A list all of whose elements equal `H`, containing `H`, and with no duplicates, is +the singleton `[H]`. -/ +theorem eq_singleton_of_forall_eq {l : List Key} {H : Key} (hnd : l.Nodup) + (hH : H ∈ l) (hall : ∀ k ∈ l, k = H) : l = [H] := by + have hrep : l = List.replicate l.length H := List.eq_replicate_of_mem hall + have hnodup : l.Nodup := hnd + rw [hrep] at hnodup + have hle : l.length ≤ 1 := List.nodup_replicate.mp hnodup + have hpos : 0 < l.length := List.length_pos_of_ne_nil (List.ne_nil_of_mem hH) + have hlen : l.length = 1 := by omega + rw [hrep, hlen, List.replicate_one] + +/-- At begin offset zero with `begin.key = ⊤` in a complete forward read, the result is +the singleton reported head, the head is the greatest stored key, and the end selector +resolves past the end. -/ +theorem zeroCrossLongTop_structure {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = false) (hne : res.keys ≠ []) + (h0 : r.begin.offset = 0) (htop : r.begin.key = (⊤ : WithTop Key)) : + res.keys = [res.keys.head hne] ∧ + (∀ k ∈ db.keys, k ≤ res.keys.head hne) ∧ + res.keys.head hne ∈ db.keys ∧ + resolve db r.end_ = (⊤ : WithTop Key) := by + have hres : res.keys = RangeRead.completeKeys db r := by + rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hHmem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hHck : res.keys.head hne ∈ RangeRead.completeKeys db r := by + rw [← hres]; exact hHmem + have hHdb : res.keys.head hne ∈ db.keys := (RangeRead.mem_completeKeys.mp hHck).1 + have hpos : 0 < db.keys.length := List.length_pos_of_ne_nil (List.ne_nil_of_mem hHdb) + set M : Key := db.keys.getLast (List.ne_nil_of_length_pos hpos) with hM + have hMax : ∀ k ∈ db.keys, k ≤ M := by + intro k hk + exact (db.sorted.pairwise.imp le_of_lt).rel_getLast hk + have hL : resolve db r.begin = ((M : Key) : WithTop Key) := by + rw [hM]; exact resolve_begin_eq_getLast_of_top h0 htop hpos + have hall : ∀ k ∈ res.keys, k = M := by + intro k hk + have hkck : k ∈ RangeRead.completeKeys db r := by rw [← hres]; exact hk + have hle := (RangeRead.mem_completeKeys.mp hkck).2.1 + rw [hL] at hle + have hMle : M ≤ k := WithTop.coe_le_coe.mp hle + exact le_antisymm (hMax k (RangeRead.mem_completeKeys.mp hkck).1) hMle + have hH_eq : res.keys.head hne = M := hall _ hHmem + have hsingle : res.keys = [M] := by + have hnodup : res.keys.Nodup := + (RangeRead.compatible_sortedLT hcomp hrev).pairwise.nodup + exact eq_singleton_of_forall_eq hnodup (hH_eq ▸ hHmem) hall + have hend_gt : ((M : Key) : WithTop Key) < resolve db r.end_ := by + have h := (RangeRead.mem_completeKeys.mp hHck).2.2 + rwa [hH_eq] at h + have hend_top : resolve db r.end_ = (⊤ : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.end_) with hn | ht | ⟨k, hk, hke⟩ + · rw [hn] at hend_gt + exact absurd hend_gt (not_lt.mpr (coe_nil_le_zero_cross_long _)) + · exact ht + · rw [hke] at hend_gt + have hkM := hMax k hk + exact absurd (WithTop.coe_lt_coe.mp hend_gt) (not_lt.mpr hkM) + refine ⟨?_, ?_, ?_, hend_top⟩ + · calc res.keys = [M] := hsingle + _ = [res.keys.head hne] := by rw [hH_eq] + · intro k hk; rw [hH_eq]; exact hMax k hk + · simpa [hH_eq] using (RangeRead.mem_completeKeys.mp hHck).1 + +/-- The result of a complete begin-zero `begin.key = ⊤` read is the reported head. -/ +theorem zeroCrossLongTop_keys_singleton {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) : + res.keys = [res.keys.head hne] := by + obtain ⟨db, hcomp⟩ := hread + exact (zeroCrossLongTop_structure hcomp hrev hmore hne hbegin htop).1 + +/-- The stored keys at or above `H` are `[H]` when `H` is stored and is the maximum. -/ +theorem above_eq_singleton_of_max {db : DB} {H : Key} + (hH : H ∈ db.keys) (hall : ∀ k ∈ db.keys, k ≤ H) : + above db (H : WithTop Key) = [H] := by + apply eq_singleton_of_forall_eq + · exact db.nodup.filter _ + · rw [mem_above]; exact ⟨hH, le_rfl⟩ + · intro k hk + rw [mem_above] at hk + exact le_antisymm (hall k hk.1) (WithTop.coe_le_coe.mp hk.2) + +/-- When every stored key is at or below `H`, the keys at or above `E` are exactly the +keys in `[E, keyAfter H)`. -/ +theorem above_eq_between_keyAfter (db : DB) (E : WithTop Key) (H : Key) + (hall : ∀ k ∈ db.keys, k ≤ H) : + above db E = between db E ((keyAfter H : Key) : WithTop Key) := by + rw [above_eq_filter, between] + apply List.filter_congr + intro k hk + have hkH : k ≤ H := hall k hk + have hlt : ((k : Key) : WithTop Key) < ((keyAfter H : Key) : WithTop Key) := + WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkH (keyAfter_self_lt H)) + by_cases hE : E ≤ (k : WithTop Key) <;> simp [hE, hlt] + +/-- The interval `[E, keyAfter H)` has at most one more key than `[E, H)`. -/ +theorem intervalCard_keyAfter_le (E : WithTop Key) (H : Key) : + intervalCard E ((keyAfter H : Key) : WithTop Key) ≤ + intervalCard E (H : WithTop Key) + 1 := by + by_cases hEH : E ≤ (H : WithTop Key) + · have hadd := intervalCard_add (b := E) (m := (H : WithTop Key)) + (e := ((keyAfter H : Key) : WithTop Key)) hEH + (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt H))) + rw [hadd] + have hone : intervalCard (H : WithTop Key) ((keyAfter H : Key) : WithTop Key) = 1 := by + have := intervalCard_succChain H 1 + simpa [keyAfter, List.replicate_one] using this + rw [hone] + · have hHltE : (H : WithTop Key) < E := lt_of_not_ge hEH + have hkeyE : ((keyAfter H : Key) : WithTop Key) ≤ E := keyAfter_le_of_lt hHltE + rw [intervalCard_eq_zero_of_le hkeyE] + simp + +/-- The number of stored keys at or above `E` is at most `intervalCard E (keyAfter H)` +when every stored key is at or below `H`. -/ +theorem above_length_le_intervalCard_keyAfter {db : DB} {E : WithTop Key} {H : Key} + (hall : ∀ k ∈ db.keys, k ≤ H) : + ((above db E).length : ℕ∞) ≤ intervalCard E ((keyAfter H : Key) : WithTop Key) := by + rw [above_eq_between_keyAfter db E H hall] + by_cases hEB : E ≤ ((keyAfter H : Key) : WithTop Key) + · exact between_length_le_intervalCard hEB + · have hnil : between db E ((keyAfter H : Key) : WithTop Key) = [] := by + rw [between, List.filter_eq_nil_iff] + intro k hk hcond + rw [decide_eq_true_eq] at hcond + have hkeyE : ((keyAfter H : Key) : WithTop Key) ≤ E := le_of_not_ge hEB + exact absurd (le_trans hkeyE hcond.1) (not_le.mpr hcond.2) + rw [hnil] + simp + +/-- If every stored key is at or below `H`, and `intervalCard E H + 1 ≤ n`, then at most +`n` stored keys lie at or above `E`. -/ +theorem above_length_le_of_max {db : DB} {E : WithTop Key} {H : Key} + (hall : ∀ k ∈ db.keys, k ≤ H) {n : ℕ} + (hge : intervalCard E (H : WithTop Key) + 1 ≤ (n : ℕ∞)) : + (above db E).length ≤ n := by + have h1 := above_length_le_intervalCard_keyAfter (db := db) (E := E) (H := H) hall + have h2 := intervalCard_keyAfter_le E H + have h3 : ((above db E).length : ℕ∞) ≤ (n : ℕ∞) := le_trans h1 (le_trans h2 hge) + exact_mod_cast h3 + +/-- In the complete begin-zero `begin.key = ⊤` cell, compatibility is exactly: every +stored key is at or below the reported head, the head is stored, and at most +`end.offset - 1` stored keys lie at or above `end.key`. -/ +theorem compatible_zero_cross_long_top_iff {db : DB} {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 1 ≤ r.end_.offset) : + RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (∀ k ∈ db.keys, k ≤ res.keys.head hne) ∧ + res.keys.head hne ∈ db.keys ∧ + (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := by + have hsingle : res.keys = [res.keys.head hne] := + zeroCrossLongTop_keys_singleton hread hrev hmore hne hbegin htop + constructor + · intro hcomp + obtain ⟨-, hmax, hHmem, hRtop⟩ := + zeroCrossLongTop_structure hcomp hrev hmore hne hbegin htop + exact ⟨hcomp.1, hmax, hHmem, + (resolve_end_eq_top_iff_above_le (db := db) (r := r) hend).mp hRtop⟩ + · rintro ⟨hW, hmax, hHmem, habove⟩ + have hpos : 0 < db.keys.length := List.length_pos_of_ne_nil (List.ne_nil_of_mem hHmem) + have hHlast : db.keys.getLast (List.ne_nil_of_length_pos hpos) = res.keys.head hne := by + apply le_antisymm + · exact hmax _ (List.getLast_mem _) + · exact (db.sorted.pairwise.imp le_of_lt).rel_getLast hHmem + have hLbegin : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + rw [resolve_begin_eq_getLast_of_top hbegin htop hpos, hHlast] + have hLend : resolve db r.end_ = (⊤ : WithTop Key) := + (resolve_end_eq_top_iff_above_le (db := db) (r := r) hend).mpr habove + have hck : RangeRead.completeKeys db r = [res.keys.head hne] := by + unfold RangeRead.completeKeys + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + unfold RangeRead.inRange + rw [hLbegin, hLend] + apply eq_singleton_of_forall_eq + · exact db.nodup.filter _ + · rw [List.mem_filter] + exact ⟨hHmem, by simp⟩ + · intro k hk + rw [List.mem_filter] at hk + rw [decide_eq_true_eq] at hk + exact le_antisymm (hmax k hk.1) (WithTop.coe_le_coe.mp hk.2.1) + refine ⟨hW, Or.inr ⟨hmore, ?_⟩⟩ + rw [hsingle, hck] + +/-- Agreement on the keys at or above `E` determines the keys at or above `E`. -/ +theorem above_eq_of_agree {db₁ db₂ : DB} {E : WithTop Key} + (h : ∀ (k : Key), E ≤ (k : WithTop Key) → (k ∈ db₁.keys ↔ k ∈ db₂.keys)) : + above db₂ E = above db₁ E := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db₂.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db₁.sorted.pairwise.filter _) + · intro k + rw [mem_above, mem_above] + constructor + · rintro ⟨hk2, hge⟩; exact ⟨(h k hge).mpr hk2, hge⟩ + · rintro ⟨hk1, hge⟩; exact ⟨(h k hge).mp hk1, hge⟩ + +/-- In the crossed long cell the end overshoot is positive. -/ +theorem zero_cross_long_top_e_pos {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) + (hElth : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : + 1 ≤ (r.end_.offset - 1).toNat := by + obtain ⟨db₀, hcomp₀⟩ := hread + obtain ⟨-, -, hHmem₀, hRtop₀⟩ := zeroCrossLongTop_structure hcomp₀ hrev hmore hne hbegin htop + have hHabove : res.keys.head hne ∈ above db₀ r.end_.key := by + rw [mem_above]; exact ⟨hHmem₀, le_of_lt hElth⟩ + have hpos := List.length_pos_of_ne_nil (List.ne_nil_of_mem hHabove) + have habove₀ := (resolve_end_eq_top_iff_above_le (db := db₀) (r := r) (by omega)).mp hRtop₀ + omega + +/-- **Soundness of `[end.key, ⊤)` in the crossed long cell with room.** -/ +theorem sound_zero_cross_long_top_E {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) + (hElth : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) : + Sound r res ⟨r.end_.key, ⊤⟩ := by + intro db₁ db₂ hagree + have hg : ∀ (k : Key), r.end_.key ≤ (k : WithTop Key) → + (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + intro k hk + exact (agreeOn_iff ⟨r.end_.key, ⊤⟩ db₁ db₂).mp hagree k + (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) + have hiff : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (∀ (k : Key), k ∈ db.keys → k ≤ res.keys.head hne) ∧ + res.keys.head hne ∈ db.keys ∧ + (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := fun db => + compatible_zero_cross_long_top_iff (db := db) hread hrev hmore hne hbegin htop (by omega) + have habove12 : above db₂ r.end_.key = above db₁ r.end_.key := above_eq_of_agree hg + constructor + · intro hc1 + obtain ⟨hW, hmax1, hHmem1, habove1⟩ := (hiff db₁).mp hc1 + refine (hiff db₂).mpr ⟨hW, ?_, ?_, ?_⟩ + · intro k hk + by_cases hkE : r.end_.key ≤ (k : WithTop Key) + · exact hmax1 k ((hg k hkE).mpr hk) + · exact WithTop.coe_le_coe.mp (le_trans (le_of_lt (lt_of_not_ge hkE)) (le_of_lt hElth)) + · exact (hg _ (le_of_lt hElth)).mp hHmem1 + · rw [habove12]; exact habove1 + · intro hc2 + obtain ⟨hW, hmax2, hHmem2, habove2⟩ := (hiff db₂).mp hc2 + refine (hiff db₁).mpr ⟨hW, ?_, ?_, ?_⟩ + · intro k hk + by_cases hkE : r.end_.key ≤ (k : WithTop Key) + · exact hmax2 k ((hg k hkE).mp hk) + · exact WithTop.coe_le_coe.mp (le_trans (le_of_lt (lt_of_not_ge hkE)) (le_of_lt hElth)) + · exact (hg _ (le_of_lt hElth)).mpr hHmem2 + · rw [← habove12]; exact habove2 + +/-- **Soundness of `[head, ⊤)` in the crossed long cell without room.** -/ +theorem sound_zero_cross_long_top_H {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) + (hbound : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 1 ≤ + (((r.end_.offset - 1).toNat : ℕ) : ℕ∞)) : + Sound r res ⟨((res.keys.head hne : Key) : WithTop Key), ⊤⟩ := by + intro db₁ db₂ hagree + have hg : ∀ (k : Key), ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k ∈ db₁.keys ↔ k ∈ db₂.keys) := by + intro k hk + exact (agreeOn_iff ⟨((res.keys.head hne : Key) : WithTop Key), ⊤⟩ db₁ db₂).mp hagree k + (ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩) + have hiff : ∀ db : DB, RangeRead.Compatible db r res ↔ + RangeRead.withinLimit r res.keys ∧ + (∀ (k : Key), k ∈ db.keys → k ≤ res.keys.head hne) ∧ + res.keys.head hne ∈ db.keys ∧ + (above db r.end_.key).length ≤ (r.end_.offset - 1).toNat := fun db => + compatible_zero_cross_long_top_iff (db := db) hread hrev hmore hne hbegin htop (by omega) + constructor + · intro hc1 + obtain ⟨hW, hmax1, hHmem1, -⟩ := (hiff db₁).mp hc1 + have hmax2 : ∀ k ∈ db₂.keys, k ≤ res.keys.head hne := by + intro k hk + by_cases hkH : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) + · exact hmax1 k ((hg k hkH).mpr hk) + · exact WithTop.coe_le_coe.mp (le_of_lt (lt_of_not_ge hkH)) + refine (hiff db₂).mpr ⟨hW, hmax2, ?_, ?_⟩ + · exact (hg _ le_rfl).mp hHmem1 + · exact above_length_le_of_max (db := db₂) (E := r.end_.key) (H := res.keys.head hne) + hmax2 hbound + · intro hc2 + obtain ⟨hW, hmax2, hHmem2, -⟩ := (hiff db₂).mp hc2 + have hmax1 : ∀ k ∈ db₁.keys, k ≤ res.keys.head hne := by + intro k hk + by_cases hkH : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) + · exact hmax2 k ((hg k hkH).mp hk) + · exact WithTop.coe_le_coe.mp (le_of_lt (lt_of_not_ge hkH)) + refine (hiff db₁).mpr ⟨hW, hmax1, ?_, ?_⟩ + · exact (hg _ le_rfl).mpr hHmem2 + · exact above_length_le_of_max (db := db₁) (E := r.end_.key) (H := res.keys.head hne) + hmax1 hbound + +/-- **Necessity of `[end.key, ⊤)` in the crossed long cell with room.** -/ +theorem necessary_zero_cross_long_top_E {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) + (hElth : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hcond : (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) : + NecessaryRange r res r.end_.key ⊤ := by + intro k hlo hhi + obtain ⟨db₀, hcomp₀⟩ := hread + have hrd : Readable r res := ⟨db₀, hcomp₀⟩ + have hsingle := zeroCrossLongTop_keys_singleton hrd hrev hmore hne hbegin htop + obtain ⟨-, hmax₀, hHmem₀, hRtop₀⟩ := + zeroCrossLongTop_structure hcomp₀ hrev hmore hne hbegin htop + have hepos : 1 ≤ (r.end_.offset - 1).toNat := + zero_cross_long_top_e_pos hrd hrev hmore hne hbegin htop hend hElth + have ht1 : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = 1 := by + rw [hsingle]; simp [le_of_lt hElth] + have hge : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by + rw [ht1] at hcond + have hadd := (ENat.add_one_le_iff + (m := (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞)) (by simp)).mpr hcond + have hsucc : (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) + 1 = + (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + rw [show (1 : ℕ∞) = ((1 : ℕ) : ℕ∞) by simp, ← Nat.cast_add, + Nat.sub_add_cancel hepos] + rwa [hsucc] at hadd + have hcard : (((r.end_.offset - 1).toNat - 1 + 1 : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by + rwa [Nat.sub_add_cancel hepos] + by_cases hkH : k = res.keys.head hne + · subst hkH + exact necessary_of_mem_result hrd (List.head_mem hne) + · by_cases hkgt : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) + · -- the key lies above the head + have hknotin : k ∉ db₀.keys := by + intro hk + exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hmax₀ k hk))) + refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ + intro hct + have hC := (compatible_zero_cross_long_top_iff (db := toggle db₀ k) hrd hrev hmore + hne hbegin htop (by omega)).mp hct + have hkint : k ∈ (toggle db₀ k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknotin⟩) + exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hC.2.1 k hkint))) + · -- `end.key ≤ k < head`: fill `[end.key, head)` maximally + obtain ⟨d, hdmem, hdk, hdlen⟩ := exists_db_keysIn_avoid (B := r.end_.key) + (E := ((res.keys.head hne : Key) : WithTop Key)) (k := k) + (m := (r.end_.offset - 1).toNat - 1) hcard + let db1 : DB := ⟨d.keys ++ [res.keys.head hne], by + refine List.sortedLT_append.mpr ⟨d.sorted, sortedLT_singleton _, ?_⟩ + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + exact WithTop.coe_lt_coe.mp (hdmem a ha).2⟩ + have hdb1keys : db1.keys = d.keys ++ [res.keys.head hne] := rfl + have hdb1_le : ∀ x ∈ db1.keys, x ≤ res.keys.head hne := by + intro x hx + rw [hdb1keys, List.mem_append, List.mem_singleton] at hx + rcases hx with hx | rfl + · exact le_of_lt (WithTop.coe_lt_coe.mp (hdmem x hx).2) + · exact le_rfl + have hHdb1 : res.keys.head hne ∈ db1.keys := by + rw [hdb1keys]; exact List.mem_append.mpr (Or.inr (List.mem_singleton.mpr rfl)) + have hknot1 : k ∉ db1.keys := by + rw [hdb1keys, List.mem_append, List.mem_singleton] + rintro (hk | hk) + · exact hdk hk + · exact hkH hk + have habove1 : above db1 r.end_.key = d.keys ++ [res.keys.head hne] := by + rw [above_eq_filter, hdb1keys, List.filter_append] + congr 1 + · rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact (hdmem x hx).1 + · simp [le_of_lt hElth] + have habove_len : (above db1 r.end_.key).length = (r.end_.offset - 1).toNat := by + rw [habove1, List.length_append, List.length_singleton, hdlen, + Nat.sub_add_cancel hepos] + have hcomp1 : RangeRead.Compatible db1 r res := + (compatible_zero_cross_long_top_iff (db := db1) hrd hrev hmore hne hbegin htop + (by omega)).mpr + ⟨hcomp₀.1, hdb1_le, hHdb1, by rw [habove_len]⟩ + refine necessary_of_toggle ⟨db1, hcomp1, ?_⟩ + intro hct + have hC := (compatible_zero_cross_long_top_iff (db := toggle db1 k) hrd hrev hmore + hne hbegin htop (by omega)).mp hct + have hkint : k ∈ (toggle db1 k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknot1⟩) + have hkabove : k ∈ above (toggle db1 k) r.end_.key := by + rw [mem_above]; exact ⟨hkint, hlo⟩ + have hsub : (above db1 r.end_.key).Sublist (above (toggle db1 k) r.end_.key) := by + unfold above + apply List.Sublist.filter + rw [toggle_eq_insert_of_not_mem hknot1, DB.insert_keys] + exact List.sublist_orderedInsert k db1.keys + have hnotmem : k ∉ above db1 r.end_.key := by + rw [mem_above]; intro h; exact hknot1 h.1 + have hne_len : (above db1 r.end_.key).length ≠ + (above (toggle db1 k) r.end_.key).length := by + intro heq + have heqlist := hsub.eq_of_length heq + exact hnotmem (heqlist ▸ hkabove) + have hlt : (above db1 r.end_.key).length < + (above (toggle db1 k) r.end_.key).length := + lt_of_le_of_ne hsub.length_le hne_len + have hlow := hC.2.2.2 + rw [habove_len] at hlt + omega + +/-- **Necessity of `[head, ⊤)` in the crossed long cell without room.** -/ +theorem necessary_zero_cross_long_top_H {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) : + NecessaryRange r res ((res.keys.head hne : Key) : WithTop Key) ⊤ := by + intro k hlo hhi + obtain ⟨db₀, hcomp₀⟩ := hread + have hrd : Readable r res := ⟨db₀, hcomp₀⟩ + obtain ⟨-, hmax₀, -, -⟩ := zeroCrossLongTop_structure hcomp₀ hrev hmore hne hbegin htop + by_cases hkH : k = res.keys.head hne + · subst hkH + exact necessary_of_mem_result hrd (List.head_mem hne) + · have hkgt : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) := by + rcases lt_or_eq_of_le hlo with h | h + · exact h + · exact absurd (WithTop.coe_inj.mp h).symm hkH + have hknotin : k ∉ db₀.keys := by + intro hk + exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hmax₀ k hk))) + refine necessary_of_toggle ⟨db₀, hcomp₀, ?_⟩ + intro hct + have hC := (compatible_zero_cross_long_top_iff (db := toggle db₀ k) hrd hrev hmore + hne hbegin htop (by omega)).mp hct + have hkint : k ∈ (toggle db₀ k).keys := mem_toggle_keys.mpr (Or.inl ⟨rfl, hknotin⟩) + exact absurd hkgt (not_lt.mpr (WithTop.coe_le_coe.mpr (hC.2.1 k hkint))) + +/-- In the crossed long cell with `begin.key = ⊤`, the closed form is minimal. -/ +theorem minimal_closedForm_of_zero_cross_long_top {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (htop : r.begin.key = (⊤ : WithTop Key)) (hend : 2 ≤ r.end_.offset) : + Minimal r res (closedFormZeroCrossLong r res) := by + have hHd : res.keys.headD ([] : Key) = res.keys.head hne := headD_eq_head_sound hne + have hsingle : res.keys = [res.keys.head hne] := + zeroCrossLongTop_keys_singleton hread hrev hmore hne hbegin htop + have ht : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = + (if r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) then 1 else 0) := by + conv_lhs => rw [hsingle] + by_cases hEH : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) <;> simp [hEH] + unfold closedFormZeroCrossLong + rw [if_pos htop, hHd] + dsimp only + rw [ht] + by_cases hcond : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) ∧ + (((r.end_.offset - 1).toNat : ℕ∞) - + ((if r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) then 1 else 0 : ℕ) : ℕ∞)) + < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + · rw [if_pos hcond] + have hcle : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) := le_of_lt hcond.1 + have ht1 : res.keys.countP + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = 1 := by + conv_lhs => rw [hsingle] + simp [hcle] + have hcond' : (r.end_.offset - 1).toNat - + res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) + < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by + rw [ht1] + simpa [hcle] + using hcond.2 + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_zero_cross_long_top_E hread hrev hmore hne hbegin htop hend hcond.1, + necessary_zero_cross_long_top_E hread hrev hmore hne hbegin htop hend hcond.1 hcond'⟩ + · rw [if_neg hcond] + have hepos : 1 ≤ (r.end_.offset - 1).toNat := by omega + have hbound : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 1 ≤ + (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) + · have hcle : r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) := le_of_lt hEH + have hnot : ¬ (((r.end_.offset - 1).toNat : ℕ∞) - + ((if r.end_.key ≤ ((res.keys.head hne : Key) : WithTop Key) then 1 else 0 : ℕ) : ℕ∞) + < intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) := + fun hc => hcond ⟨hEH, hc⟩ + have hge : intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) ≤ + (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) := by + rw [ENat.natCast_sub] + simpa [hcle] using not_lt.mp hnot + have hsucc : (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) + 1 = + (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + rw [show (1 : ℕ∞) = ((1 : ℕ) : ℕ∞) by simp, ← Nat.cast_add, + Nat.sub_add_cancel hepos] + calc intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + 1 + ≤ (((r.end_.offset - 1).toNat - 1 : ℕ) : ℕ∞) + 1 := by + simpa [add_comm] using add_le_add_right hge 1 + _ = (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := hsucc + · have hHE : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key := + le_of_not_gt hEH + rw [intervalCard_eq_zero_of_le hHE, zero_add] + exact_mod_cast hepos + refine minimal_iff_sound_and_necessaryRange.mpr + ⟨sound_zero_cross_long_top_H hread hrev hmore hne hbegin htop hend hbound, + necessary_zero_cross_long_top_H hread hrev hmore hne hbegin htop hend⟩ + + +end FormalConflictRange + +namespace FormalConflictRange + +/-- In the crossed complete begin-zero long cell, the number of reported keys at or +above `end_.key` is at most the end overshoot. -/ +theorem reportedAtEnd_le_endOvershoot_zero_cross_long {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + reportedAtEnd r res ≤ endOvershoot r := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hcompA₀ : RangeRead.Compatible db₀ (zeroBeginAnchor r res) res := + compatible_anchor_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross + have hrevA : (zeroBeginAnchor r res).reverse = false := by simp [zeroBeginAnchor, hrev] + have hbeginA : (zeroBeginAnchor r res).begin.offset = 1 := by simp [zeroBeginAnchor] + have hendA : 2 ≤ (zeroBeginAnchor r res).end_.offset := by simpa [zeroBeginAnchor] using hend + have hres : res.keys = RangeRead.completeKeys db₀ (zeroBeginAnchor r res) := by + rcases hcompA₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + unfold reportedAtEnd endOvershoot + by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) + · have hEH' : (zeroBeginAnchor r res).end_.key < (zeroBeginAnchor r res).begin.key := by + rw [zeroBeginAnchor_end_key_eq, zeroBeginAnchor_begin_key_eq r res hne] + exact hEH + have h := crossed_complete_t_le_e_general (r := zeroBeginAnchor r res) ⟨db₀, hcompA₀⟩ + hmore hrevA hne (by omega) hEH' (by omega) + simpa [zeroBeginAnchor, endOvershoot] using h + · have hle : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key := not_lt.mp hEH + have hleA : (zeroBeginAnchor r res).begin.key ≤ (zeroBeginAnchor r res).end_.key := by + rw [zeroBeginAnchor_begin_key_eq r res hne, zeroBeginAnchor_end_key_eq] + exact hle + have h := completeKeys_count_ge_le_general (db := db₀) (r := zeroBeginAnchor r res) + hrevA (by omega) hleA (by omega) + rw [hres] + simpa [zeroBeginAnchor, endOvershoot] using h + +/-- In the crossed complete begin-zero long cell, `res.keys` itself is a compatible +database: it stores exactly the reported keys, the begin selector resolves to the +reported head, and the end selector resolves past the end. -/ +theorem compatible_res_keys_zero_cross_long {r : RangeRead} {res : Result} + (hsort : res.keys.SortedLT) + (hr2 : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) : + RangeRead.Compatible (⟨res.keys, hsort⟩ : DB) r res := by + obtain ⟨db₀, hcomp₀⟩ := hr2 + let db : DB := ⟨res.keys, hsort⟩ + have hr2' : Readable r res := ⟨db₀, hcomp₀⟩ + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hfilter := completeKeys_eq_filter_zero_crossed hcomp₀ hrev hmore hne hbegin hcross + have hresfil : res.keys = db₀.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < + (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := + hres.trans hfilter + have hHmem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hHdb : res.keys.head hne ∈ db.keys := hHmem + have hno : ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < + (k : WithTop Key) ∧ (k : WithTop Key) < r.begin.key) := by + intro k hk + have hkdb : k ∈ db₀.keys := by + have hk' : k ∈ RangeRead.completeKeys db₀ r := hres ▸ hk + exact (RangeRead.mem_completeKeys.mp hk').1 + exact no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross + k hkdb + have hL : resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hHdb hcross hno + have htle := reportedAtEnd_le_endOvershoot_zero_cross_long hr2' hrev hmore hne hbegin + hcross hend + have habove : (above db r.end_.key).length ≤ endOvershoot r := by + unfold above + rw [← List.countP_eq_length_filter] + simpa [reportedAtEnd, endOvershoot] using htle + have htop : resolve db r.end_ = (⊤ : WithTop Key) := + (resolve_end_eq_top_iff_above_le (db := db) (r := r) (by omega)).mpr habove + refine ⟨?_, ?_⟩ + · simpa [RangeRead.withinLimit] using hcomp₀.1 + · right + refine ⟨hmore, ?_⟩ + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hL, htop] + symm + apply List.filter_eq_self.mpr + intro k hk + rw [decide_eq_true_eq] + have hkfil : k ∈ db₀.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < + (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key))) := by + rw [← hresfil]; exact hk + exact ⟨(List.mem_filter.mp hkfil).2 |> of_decide_eq_true |> And.left, + WithTop.coe_lt_top k⟩ + +end FormalConflictRange + +namespace FormalConflictRange + +/-- A key between the reported head and `begin.key` is necessary in the crossed +complete begin-zero long cell. -/ +theorem necessary_zero_cross_long_gap {r : RangeRead} {res : Result} {k : Key} + (hsort : res.keys.SortedLT) + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) + (hkH : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key)) + (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hres : res.keys = RangeRead.completeKeys db₀ r := by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + let db : DB := ⟨res.keys, hsort⟩ + have hcompR : RangeRead.Compatible db r res := + compatible_res_keys_zero_cross_long hsort hread' hrev hmore hne hbegin hcross hend + have hknot : k ∉ db.keys := by + intro hk + have hk' : k ∈ RangeRead.completeKeys db₀ r := by + rw [← hres]; exact hk + have hkdb : k ∈ db₀.keys := (RangeRead.mem_completeKeys.mp hk').1 + exact no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin hcross + k hkdb ⟨hkH, hkB⟩ + refine necessary_of_toggle ⟨db, hcompR, ?_⟩ + intro hct + have hkres' : res.keys = RangeRead.completeKeys (toggle db k) r := by + rcases hct.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hkmem : k ∈ (toggle db k).keys := + mem_toggle_keys.mpr (Or.inl ⟨rfl, hknot⟩) + have hHnot : res.keys.head hne ∉ RangeRead.completeKeys (toggle db k) r := by + intro hmem + unfold RangeRead.completeKeys RangeRead.inRange at hmem + rw [hrev] at hmem + simp only [Bool.false_eq_true, ite_false] at hmem + have hcond := (List.mem_filter.mp hmem).2 + rw [decide_eq_true_eq] at hcond + have hkr : ((k : Key) : WithTop Key) ≤ resolve (toggle db k) r.begin := + resolve_zero_begin_le_of_mem_below hbegin hkmem hkB k hkmem hkB + exact absurd (le_trans hkr hcond.1) (not_le.mpr hkH) + exact hHnot (hkres' ▸ List.head_mem hne) + +/-- A key between `begin.key` and the reported last is necessary in the crossed +complete begin-zero long cell. -/ +theorem necessary_zero_cross_long_between_last {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hne : res.keys ≠ []) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hkB : r.begin.key ≤ (k : WithTop Key)) + (hkL : k ≤ res.keys.getLast hne) : + Necessary r res k := + necessary_of_between_result hread (List.head_mem hne) (List.getLast_mem hne) + (WithTop.coe_le_coe.mp (le_trans (le_of_lt hcross) hkB)) hkL + +end FormalConflictRange + +namespace FormalConflictRange + +/-- A key above the reported last is necessary in the crossed complete begin-zero +long cell when the end overshoots (`t < e`). -/ +theorem necessary_zero_cross_long_above_last {r : RangeRead} {res : Result} {k : Key} + (hsort : res.keys.SortedLT) + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hlong : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (ht : reportedAtEnd r res < endOvershoot r) + (hkB : r.begin.key ≤ (k : WithTop Key)) + (hkL : ((res.keys.getLast hne : Key) : WithTop Key) < (k : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + let db : DB := ⟨res.keys, hsort⟩ + have hcompR : RangeRead.Compatible db r res := + compatible_res_keys_zero_cross_long hsort hread' hrev hmore hne hbegin hcross hend + have hkLle : ∀ a ∈ res.keys, a ≤ res.keys.getLast hne := by + intro a ha + simpa using (hsort.pairwise.imp le_of_lt).rel_getLast ha + have hknot : k ∉ db.keys := by + intro hk + exact absurd (WithTop.coe_le_coe.mpr (hkLle k hk)) (not_le.mpr hkL) + have hkE : r.end_.key ≤ (k : WithTop Key) := le_trans (le_of_lt hlong) hkB + refine necessary_of_toggle ⟨db, hcompR, ?_⟩ + intro hct + have hres' : res.keys = RangeRead.completeKeys (toggle db k) r := by + rcases hct.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have htk : toggle db k = db.insert k hknot := toggle_eq_insert_of_not_mem hknot + have hab : (above db r.end_.key).length = reportedAtEnd r res := by + unfold reportedAtEnd above + rw [List.countP_eq_length_filter] + have hlen' : (above (toggle db k) r.end_.key).length ≤ endOvershoot r := by + rw [← length_le_cnt_add_iff_above_le (toggle db k) r.end_.key (endOvershoot r)] + rw [htk, cnt_insert_not_lt db k hknot (not_lt.mpr hkE), DB.insert_keys, + List.orderedInsert_length] + have hcnt := cnt_add_above_length db r.end_.key + rw [hab] at hcnt + omega + have hktop : resolve (toggle db k) r.end_ = (⊤ : WithTop Key) := + (resolve_end_eq_top_iff_above_le (db := toggle db k) (r := r) (by omega)).mpr hlen' + have hHdb : res.keys.head hne ∈ (toggle db k).keys := + mem_toggle_keys.mpr (Or.inr ⟨by + intro h + exact absurd hkL (not_lt.mpr (WithTop.coe_le_coe.mpr (h ▸ (hkLle _ (List.head_mem hne))))), + List.head_mem hne⟩) + have hresbeg : resolve (toggle db k) r.begin < r.begin.key := + resolve_zero_begin_lt_key hbegin hHdb hcross + have hkdb' : k ∈ (toggle db k).keys := + mem_toggle_keys.mpr (Or.inl ⟨rfl, hknot⟩) + have hkck : k ∈ RangeRead.completeKeys (toggle db k) r := + RangeRead.mem_completeKeys.mpr + ⟨hkdb', le_trans (le_of_lt hresbeg) hkB, by rw [hktop]; exact WithTop.coe_lt_top k⟩ + have : k ∈ res.keys := by rw [hres']; exact hkck + exact hknot this + +end FormalConflictRange + +namespace FormalConflictRange + +/-- A key between `end_.key` and the reported head is necessary in the crossed +complete begin-zero long cell when the result is finite (`t = e`). -/ +theorem necessary_zero_cross_long_below_head_eq {r : RangeRead} {res : Result} {k : Key} + (hsort : res.keys.SortedLT) + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hlong : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset) + (hte : reportedAtEnd r res = endOvershoot r) + (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + let db : DB := ⟨res.keys, hsort⟩ + have hcompR : RangeRead.Compatible db r res := + compatible_res_keys_zero_cross_long hsort hread' hrev hmore hne hbegin hcross hend + have hhead_le : ∀ q ∈ res.keys, ((res.keys.head hne : Key) : WithTop Key) ≤ (q : WithTop Key) := by + intro q hq + have := (hsort.pairwise.imp le_of_lt).rel_head hq + simpa using this + have hkLle : ∀ a ∈ res.keys, a ≤ res.keys.getLast hne := by + intro a ha + simpa using (hsort.pairwise.imp le_of_lt).rel_getLast ha + have hknot : k ∉ db.keys := by + intro hk + exact absurd (hhead_le k hk) (not_le.mpr hkH) + have hkres : k ∉ res.keys := hknot + have htk : toggle db k = db.insert k hknot := toggle_eq_insert_of_not_mem hknot + refine necessary_of_toggle ⟨db, hcompR, ?_⟩ + intro hct + have hres' : res.keys = RangeRead.completeKeys (toggle db k) r := by + rcases hct.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq + have hHmem : res.keys.head hne ∈ (toggle db k).keys := + mem_toggle_keys.mpr (Or.inr ⟨by + intro h; exact (lt_irrefl (k : WithTop Key)) (h ▸ hkH), List.head_mem hne⟩) + have hno : ∀ q ∈ (toggle db k).keys, + ¬ (((res.keys.head hne : Key) : WithTop Key) < (q : WithTop Key) ∧ + (q : WithTop Key) < r.begin.key) := by + intro q hq + rcases (mem_toggle_keys.mp hq) with ⟨rfl, _⟩ | ⟨_, hqmem⟩ + · exact fun h => absurd h.1 (not_lt.mpr (le_of_lt hkH)) + · have hqdb : q ∈ db₀.keys := + (RangeRead.mem_completeKeys.mp (by + rw [← (by + rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · exact heq : res.keys = RangeRead.completeKeys db₀ r)] + exact hqmem)).1 + exact no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin + hcross q hqdb + have hLbegin : resolve (toggle db k) r.begin = + ((res.keys.head hne : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hHmem hcross hno + have hHL : res.keys.head hne ≤ res.keys.getLast hne := hkLle _ (List.head_mem hne) + have hmax : ∀ q ∈ (toggle db k).keys, q ≤ res.keys.getLast hne := by + intro q hq + rcases (mem_toggle_keys.mp hq) with ⟨rfl, _⟩ | ⟨_, hqmem⟩ + · exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkH)) hHL + · exact hkLle q hqmem + have habove_all : above (toggle db k) r.end_.key = (toggle db k).keys := by + unfold above + apply List.filter_eq_self.mpr + intro q hq + rw [decide_eq_true_eq] + rcases (mem_toggle_keys.mp hq) with ⟨rfl, _⟩ | ⟨_, hqmem⟩ + · exact hkE + · exact le_trans (le_of_lt hEH) (hhead_le q hqmem) + have habove_len : (above (toggle db k) r.end_.key).length = res.keys.length + 1 := by + rw [habove_all, htk, DB.insert_keys, List.orderedInsert_length] + have htlen : reportedAtEnd r res = res.keys.length := by + unfold reportedAtEnd + rw [List.countP_eq_length] + intro q hq + rw [decide_eq_true_eq] + exact le_trans (le_of_lt hEH) (hhead_le q hq) + have hnot_top : resolve (toggle db k) r.end_ ≠ (⊤ : WithTop Key) := by + intro htop + have hiff := (resolve_end_eq_top_iff_above_le (db := toggle db k) (r := r) + (by omega)).mp htop + rw [habove_len] at hiff + have hte' : res.keys.length = (r.end_.offset - 1).toNat := by + unfold endOvershoot at hte + exact htlen.symm.trans hte + omega + have hresle : resolve (toggle db k) r.end_ ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by + rcases resolve_eq_nil_or_eq_top_or_mem (db := toggle db k) (s := r.end_) with hn | ht' | ⟨q, hq, hqe⟩ + · rw [hn]; exact coe_nil_le_zero_cross_long _ + · exact absurd ht' hnot_top + · rw [hqe]; exact WithTop.coe_le_coe.mpr (hmax q hq) + have hLnot : res.keys.getLast hne ∉ RangeRead.completeKeys (toggle db k) r := by + intro hmem + unfold RangeRead.completeKeys RangeRead.inRange at hmem + rw [hrev] at hmem + simp only [Bool.false_eq_true, ite_false] at hmem + have hcond := (List.mem_filter.mp hmem).2 + rw [decide_eq_true_eq] at hcond + exact absurd hcond.2 (not_lt.mpr hresle) + have : res.keys.getLast hne ∈ RangeRead.completeKeys (toggle db k) r := by + rw [← hres']; exact List.getLast_mem hne + exact hLnot this + + +/-- A key in `[end_.key, head)` is necessary in the crossed complete begin-zero +long cell when the end overshoots (`t < e`) and there is enough room (`e - t ≤ g`). -/ +theorem necessary_zero_cross_long_below_head_lt {r : RangeRead} {res : Result} {k : Key} + (hsort : res.keys.SortedLT) + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) + (ht : reportedAtEnd r res < endOvershoot r) + (hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hroom : (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) + (hB : r.begin.key ≠ (⊤ : WithTop Key)) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨Bk, hBk⟩ : ∃ Bk : Key, r.begin.key = ((Bk : Key) : WithTop Key) := by + cases h : r.begin.key with + | top => exact absurd h hB + | coe b => exact ⟨b, rfl⟩ + obtain ⟨db₀, hcomp₀⟩ := hread + have hread' : Readable r res := ⟨db₀, hcomp₀⟩ + have hhead_le : ∀ q ∈ res.keys, + ((res.keys.head hne : Key) : WithTop Key) ≤ (q : WithTop Key) := by + intro q hq + simpa using (hsort.pairwise.imp le_of_lt).rel_head hq + have htlen : reportedAtEnd r res = res.keys.length := by + unfold reportedAtEnd + rw [List.countP_eq_length] + intro q hq + rw [decide_eq_true_eq] + exact le_trans (le_of_lt hEH) (hhead_le q hq) + have hrest_ge_B : ∀ q ∈ res.keys, q ≠ res.keys.head hne → + ((Bk : Key) : WithTop Key) ≤ (q : WithTop Key) := by + intro q hq hqH + have hqdb : q ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hq + have hno := no_stored_between_head_begin_of_zero_crossed hcomp₀ hrev hmore hne hbegin + hcross q hqdb + have hleH := hhead_le q hq + have hltH : ((res.keys.head hne : Key) : WithTop Key) < (q : WithTop Key) := + lt_of_le_of_ne hleH (fun hEq => hqH (WithTop.coe_injective hEq.symm)) + have hnotlt : ¬ (q : WithTop Key) < r.begin.key := fun h => hno ⟨hltH, h⟩ + rw [hBk] at hnotlt + exact not_lt.mp hnotlt + have hHltBk : res.keys.head hne < Bk := by + have h := hcross + rw [hBk] at h + exact WithTop.coe_lt_coe.mp h + set s : ℕ := endOvershoot r - reportedAtEnd r res with hsdef + have hs : 0 < s := by + unfold s + omega + have hroom' : ((s : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by + simpa [s] using hroom + have hcard : (((s - 1) + 1 : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) := by + rw [Nat.sub_add_cancel (by omega : 1 ≤ s)] + exact hroom' + obtain ⟨dbH0, hH0range, hkH0, hH0len⟩ := + exists_db_keysIn_avoid (B := r.end_.key) + (E := ((res.keys.head hne : Key) : WithTop Key)) (k := k) (m := s - 1) hcard + have hH0geE : ∀ x ∈ dbH0.keys, r.end_.key ≤ (x : WithTop Key) := + fun x hx => (hH0range x hx).1 + have hH0ltH : ∀ x ∈ dbH0.keys, + (x : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := + fun x hx => (hH0range x hx).2 + obtain ⟨C, hCge, hCnotin⟩ := + exists_le_not_mem (res.keys ++ dbH0.keys) (max Bk (res.keys.getLast hne)) + have hCnotres : C ∉ res.keys := fun h => hCnotin (List.mem_append_left _ h) + have hCnotH0 : C ∉ dbH0.keys := fun h => hCnotin (List.mem_append_right _ h) + have hCBk : Bk ≤ C := le_trans (le_max_left _ _) hCge + have hCL : res.keys.getLast hne ≤ C := le_trans (le_max_right _ _) hCge + have hCH : ((res.keys.head hne : Key) : WithTop Key) ≤ (C : WithTop Key) := + le_trans (le_of_lt hcross) (by rw [hBk]; exact WithTop.coe_le_coe.mpr hCBk) + have hCgtRes : ∀ q ∈ res.keys, q < C := by + intro q hq + have hle : q ≤ res.keys.getLast hne := by + simpa using (hsort.pairwise.imp le_of_lt).rel_getLast hq + exact lt_of_le_of_ne (le_trans hle hCL) (fun h => hCnotres (h ▸ hq)) + have hkC : k < C := WithTop.coe_lt_coe.mp (lt_of_lt_of_le hkH hCH) + -- membership facts for the extended head block + let dbH : DB := dbH0.insert k hkH0 + have hmem_dbH : ∀ x, x ∈ dbH.keys ↔ x = k ∨ x ∈ dbH0.keys := by + intro x + rw [show dbH.keys = dbH0.keys.orderedInsert (· ≤ ·) k from DB.insert_keys dbH0 k hkH0] + rw [List.mem_orderedInsert (· ≤ ·)] + have hHgeE : ∀ x ∈ dbH.keys, r.end_.key ≤ (x : WithTop Key) := by + intro x hx + rcases (hmem_dbH x).mp hx with rfl | hx0 + · exact hkE + · exact hH0geE x hx0 + have hHltH : ∀ x ∈ dbH.keys, + (x : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := by + intro x hx + rcases (hmem_dbH x).mp hx with rfl | hx0 + · exact hkH + · exact hH0ltH x hx0 + have hHnotin_res : ∀ x ∈ dbH.keys, x ∉ res.keys := by + intro x hx hxr + have hxH := hHltH x hx + have hHx := hhead_le x hxr + exact absurd hHx (not_le.mpr hxH) + have hHsort : dbH.keys.SortedLT := dbH.sorted + have hdbH_res : (dbH.keys ++ res.keys).SortedLT := + List.sortedLT_append.mpr ⟨hHsort, hsort, by + intro a ha c hc + exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltH a ha) (hhead_le c hc))⟩ + have hsingle : ([C] : List Key).SortedLT := by + rw [List.sortedLT_cons] + exact ⟨by simp, List.sortedLT_nil⟩ + have hcrossList : ∀ a ∈ (dbH.keys ++ res.keys), ∀ b ∈ ([C] : List Key), a < b := by + intro a ha b hb + rw [List.mem_singleton] at hb + subst hb + rcases List.mem_append.mp ha with haH | haR + · exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hHltH a haH) hCH) + · exact hCgtRes a haR + have hsortBig : (dbH.keys ++ res.keys ++ [C]).SortedLT := + List.sortedLT_append.mpr ⟨hdbH_res, hsingle, hcrossList⟩ + let dbBig : DB := ⟨dbH.keys ++ res.keys ++ [C], hsortBig⟩ + have hkeysBig : dbBig.keys = dbH.keys ++ res.keys ++ [C] := rfl + have hkBig : k ∈ dbBig.keys := by + rw [hkeysBig] + exact List.mem_append_left _ (List.mem_append_left _ ((hmem_dbH k).mpr (Or.inl rfl))) + have hHBig : res.keys.head hne ∈ dbBig.keys := by + rw [hkeysBig] + exact List.mem_append_left _ (List.mem_append_right _ (List.head_mem hne)) + have hnoBig : ∀ q ∈ dbBig.keys, + ¬ (((res.keys.head hne : Key) : WithTop Key) < (q : WithTop Key) ∧ + (q : WithTop Key) < r.begin.key) := by + intro q hq hqb + rw [hkeysBig] at hq + rcases List.mem_append.mp hq with hq' | hqC + · rcases List.mem_append.mp hq' with hqH | hqR + · exact absurd hqb.1 (not_lt.mpr (le_of_lt (hHltH q hqH))) + · have hqeq : q = res.keys.head hne := by + by_contra hne' + have hge := hrest_ge_B q hqR hne' + rw [hBk] at hqb + exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (WithTop.coe_le_coe.mp hge))) + exact absurd (hqeq ▸ hqb.1) (lt_irrefl _) + · rw [List.mem_singleton] at hqC + subst hqC + rw [hBk] at hqb + exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr hCBk)) + have hLbegin : resolve dbBig r.begin = ((res.keys.head hne : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hHBig hcross hnoBig + -- the end selector resolves to `C` + have hcEBig : cnt dbBig r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeysBig] at hx + rcases List.mem_append.mp hx with hx' | hxC + · rcases List.mem_append.mp hx' with hxH | hxR + · exact hHgeE x hxH + · exact le_trans (le_of_lt hEH) (hhead_le x hxR) + · rw [List.mem_singleton] at hxC + subst hxC + exact le_trans (le_of_lt hEH) hCH + have hlenBig : dbBig.keys.length = endOvershoot r + 1 := by + have hHlen : dbH.keys.length = s := by + rw [show dbH.keys = dbH0.keys.orderedInsert (· ≤ ·) k from DB.insert_keys dbH0 k hkH0, + List.orderedInsert_length, hH0len] + omega + rw [hkeysBig, List.length_append, List.length_append, hHlen, List.length_singleton] + have : reportedAtEnd r res + s = endOvershoot r := by + unfold s + omega + omega + have hanchorBig : (r.end_.anchor dbBig).toNat = endOvershoot r := by + have h := anchor_toNat_eq (s := r.end_) (db := dbBig) (by omega : 1 ≤ r.end_.offset) + rw [h] + have hc : dbBig.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by + simpa [cnt] using hcEBig + rw [hc, Nat.zero_add] + rfl + have hresend : resolve dbBig r.end_ = ((C : Key) : WithTop Key) := by + have hbig : dbBig.keys = (dbH.keys ++ res.keys) ++ [C] := by + rw [hkeysBig, List.append_assoc] + have hlenpre : (dbH.keys ++ res.keys).length = endOvershoot r := by + rw [List.length_append] + have hHlen : dbH.keys.length = s := by + rw [show dbH.keys = dbH0.keys.orderedInsert (· ≤ ·) k from DB.insert_keys dbH0 k hkH0, + List.orderedInsert_length, hH0len] + omega + rw [hHlen] + unfold s + omega + have hget : dbBig.keys[endOvershoot r]? = some C := by + rw [hbig, show endOvershoot r = (dbH.keys ++ res.keys).length from hlenpre.symm] + simp + rw [resolve, ite_eq_right (not_lt.mpr (anchor_nonneg_of_one_le (by omega : 1 ≤ r.end_.offset)))] + simp only [hanchorBig] + rw [hget] + have hckBig : RangeRead.completeKeys dbBig r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hLbegin, hresend] + rw [hkeysBig] + have hfil : (dbH.keys ++ res.keys ++ [C]).filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((C : Key) : WithTop Key))) = res.keys := by + rw [List.filter_append, List.filter_append] + have hHnil : dbH.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((C : Key) : WithTop Key))) = [] := by + apply List.filter_eq_nil_iff.mpr + intro x hx hcond + rw [decide_eq_true_eq] at hcond + exact absurd hcond.1 (not_le.mpr (hHltH x hx)) + rw [hHnil, List.nil_append] + have hresfil : res.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((C : Key) : WithTop Key))) = res.keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact ⟨hhead_le x hx, WithTop.coe_lt_coe.mpr (hCgtRes x hx)⟩ + rw [hresfil] + have hCnil : ([C] : List Key).filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < ((C : Key) : WithTop Key))) = [] := by + apply List.filter_eq_nil_iff.mpr + intro x hx hcond + rw [List.mem_singleton] at hx + rw [hx] at hcond + rw [decide_eq_true_eq] at hcond + exact absurd hcond.2 (lt_irrefl (C : WithTop Key)) + rw [hCnil] + simp + rw [hfil] + have hcompBig : RangeRead.Compatible dbBig r res := + ⟨by simpa [RangeRead.withinLimit, hkeysBig] using hcomp₀.1, Or.inr ⟨hmore, hckBig.symm⟩⟩ + -- erasing `k` breaks compatibility + have hEraseKeys : (dbBig.erase k).keys = dbH0.keys ++ res.keys ++ [C] := by + rw [DB.erase_keys, hkeysBig] + have hHerase : (dbH.keys).erase k = dbH0.keys := + DB.erase_insert_keys dbH0 k hkH0 + have hnotinRest : k ∉ res.keys ++ [C] := by + simp only [List.mem_append, List.mem_singleton] + rintro (hkr | rfl) + · exact hHnotin_res k ((hmem_dbH k).mpr (Or.inl rfl)) hkr + · exact absurd hkC (lt_irrefl k) + rw [List.append_assoc, List.erase_append, + if_pos ((hmem_dbH k).mpr (Or.inl rfl)), hHerase] + simp [List.append_assoc] + let dbErase : DB := dbBig.erase k + have hkeysErase : dbErase.keys = dbH0.keys ++ res.keys ++ [C] := hEraseKeys + have hnotcompErase : ¬ RangeRead.Compatible dbErase r res := by + intro hc + have hck := hc.2 + rcases hck with ⟨hm, _, _⟩ | ⟨_, heq⟩ + · simp [hmore] at hm + · have hlenEq : (RangeRead.completeKeys dbErase r).length = res.keys.length := by + rw [heq] + -- compute completeKeys length on dbErase + have hcEe : cnt dbErase r.end_.key = 0 := by + apply cnt_eq_zero_of_forall_ge + intro x hx + rw [hkeysErase] at hx + rcases List.mem_append.mp hx with hx' | hxC + · rcases List.mem_append.mp hx' with hxH | hxR + · exact hH0geE x hxH + · exact le_trans (le_of_lt hEH) (hhead_le x hxR) + · rw [List.mem_singleton] at hxC + subst hxC + exact le_trans (le_of_lt hEH) hCH + have hlenErase : dbErase.keys.length = endOvershoot r := by + rw [hkeysErase, List.length_append, List.length_append, hH0len, List.length_singleton] + have : reportedAtEnd r res + s = endOvershoot r := by + unfold s + omega + omega + have hanchorE : (r.end_.anchor dbErase).toNat = endOvershoot r := by + have h := anchor_toNat_eq (s := r.end_) (db := dbErase) (by omega : 1 ≤ r.end_.offset) + rw [h] + have hce : dbErase.keys.countP + (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = 0 := by + simpa [cnt] using hcEe + rw [hce, Nat.zero_add] + rfl + have htopErase : resolve dbErase r.end_ = (⊤ : WithTop Key) := by + rw [resolve_eq_top (anchor_nonneg_of_one_le (by omega : 1 ≤ r.end_.offset)) (by + rw [hanchorE, hlenErase])] + have hLerase : resolve dbErase r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + apply resolve_begin_eq_of_no_between_zero hbegin + · rw [hkeysErase] + exact List.mem_append_left _ (List.mem_append_right _ (List.head_mem hne)) + · exact hcross + · intro q hq hqb + rw [hkeysErase] at hq + rcases List.mem_append.mp hq with hq' | hqC + · rcases List.mem_append.mp hq' with hqH | hqR + · exact absurd hqb.1 (not_lt.mpr (le_of_lt (hH0ltH q hqH))) + · have hqeq : q = res.keys.head hne := by + by_contra hne' + have hge := hrest_ge_B q hqR hne' + rw [hBk] at hqb + exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (WithTop.coe_le_coe.mp hge))) + exact absurd (hqeq ▸ hqb.1) (lt_irrefl _) + · rw [List.mem_singleton] at hqC + subst hqC + rw [hBk] at hqb + exact absurd hqb.2 (not_lt.mpr (WithTop.coe_le_coe.mpr hCBk)) + have hckErase : RangeRead.completeKeys dbErase r = res.keys ++ [C] := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [hrev] + simp only [Bool.false_eq_true, ite_false] + rw [hLerase, htopErase, hkeysErase] + have hfil : (dbH0.keys ++ res.keys ++ [C]).filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (⊤ : WithTop Key))) = res.keys ++ [C] := by + rw [List.filter_append, List.filter_append] + have hHnil : dbH0.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (⊤ : WithTop Key))) = [] := by + apply List.filter_eq_nil_iff.mpr + intro x hx hcond + rw [decide_eq_true_eq] at hcond + exact absurd hcond.1 (not_le.mpr (hH0ltH x hx)) + rw [hHnil, List.nil_append] + have hresfil : res.keys.filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (⊤ : WithTop Key))) = res.keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact ⟨hhead_le x hx, WithTop.coe_lt_top x⟩ + rw [hresfil] + have hCsingle : ([C] : List Key).filter (fun k : Key => decide + (((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ + (k : WithTop Key) < (⊤ : WithTop Key))) = [C] := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + rw [List.mem_singleton] at hx + rw [hx] + exact ⟨hCH, WithTop.coe_lt_top C⟩ + rw [hCsingle] + rw [hfil] + rw [hckErase, List.length_append, List.length_singleton] at hlenEq + omega + refine necessary_of_toggle ⟨dbBig, hcompBig, ?_⟩ + rw [toggle_eq_erase_of_mem hkBig] + exact hnotcompErase + + +/-- The end endpoint of the long-cell anchor, in terms of the reported last. -/ +theorem zeroCrossLongAnchor_end_eq (r : RangeRead) (res : Result) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hend : 2 ≤ r.end_.offset) : + (zeroCrossLongAnchor r res).end_ = + if reportedAtEnd r res = endOvershoot r + then (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) else ⊤ := by + by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) + · rw [zeroCrossLongAnchor_end_crossed hne hbegin hend hEH, + getLastD_eq_getLast_zero_cross_long hne] + · rw [zeroCrossLongAnchor_end_crossfree hne hbegin hend hEH, + getLastD_eq_getLast_zero_cross_long hne] + +/-- The crossed complete begin-zero long cell at finite `begin.key`: the closed form +`closedFormZeroCrossLong` is minimal. -/ +theorem minimal_closedForm_of_zero_cross_long_finite {r : RangeRead} {res : Result} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = false) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : 2 ≤ r.end_.offset) + (hlong : r.end_.key < r.begin.key) + (hB : r.begin.key ≠ (⊤ : WithTop Key)) : + Minimal r res (closedFormZeroCrossLong r res) := by + have hsort : res.keys.SortedLT := by + obtain ⟨db, hc⟩ := hread + exact RangeRead.compatible_sortedLT hc hrev + have hminA := minimal_zeroCrossLongAnchor hread hrev hmore hne hbegin hcross hend + have hsoundA : Sound (zeroBeginAnchor r res) res (zeroCrossLongAnchor r res) := hminA.1 + have hsub : Subrange (zeroCrossLongAnchor r res) + ⟨(zeroCrossLongAnchor r res).begin, + max (zeroCrossLongAnchor r res).end_ r.begin.key⟩ := + subrange_of_le le_rfl (le_max_left _ _) + have hsound : Sound r res (closedFormZeroCrossLong r res) := by + unfold closedFormZeroCrossLong + rw [if_neg hB] + refine sound_of_anchor_sound hrev hmore hne hbegin hcross ?_ ?_ hsub hsoundA + · by_cases hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) + · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH] + by_cases hc : (reportedAtEnd r res = endOvershoot r ∨ + (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) + · rw [if_pos hc] + exact le_of_lt hEH + · rw [if_neg hc] + · rw [zeroCrossLongAnchor_begin_crossfree hne hbegin hend hEH] + · exact le_max_right _ _ + refine minimal_iff_sound_and_necessaryRange.mpr ⟨hsound, ?_⟩ + intro k hklo hkhi + unfold closedFormZeroCrossLong at hklo hkhi + rw [if_neg hB] at hklo hkhi + dsimp only at hklo hkhi + have htle := reportedAtEnd_le_endOvershoot_zero_cross_long hread hrev hmore hne hbegin + hcross hend + by_cases hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) + · -- below the reported head + have hEH : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) := by + by_contra hcon + rw [zeroCrossLongAnchor_begin_crossfree hne hbegin hend hcon] at hklo + exact absurd hklo (not_le.mpr hkH) + have hkE : r.end_.key ≤ (k : WithTop Key) := by + by_cases hc : (reportedAtEnd r res = endOvershoot r ∨ + (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) + · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH, if_pos hc] at hklo + exact hklo + · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH, if_neg hc] at hklo + exact absurd hklo (not_le.mpr hkH) + by_cases hte : reportedAtEnd r res = endOvershoot r + · exact necessary_zero_cross_long_below_head_eq hsort hread hrev hmore hne hbegin + hcross hlong hend hte hEH hkE hkH + · have hroom : (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key)) := by + by_cases hc : (reportedAtEnd r res = endOvershoot r ∨ + (((endOvershoot r - reportedAtEnd r res : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key))) + · exact hc.resolve_left hte + · rw [zeroCrossLongAnchor_begin_crossed hne hbegin hend hEH, if_neg hc] at hklo + exact absurd hklo (not_le.mpr hkH) + exact necessary_zero_cross_long_below_head_lt hsort hread hrev hmore hne hbegin + hcross hend (lt_of_le_of_ne htle hte) hEH hroom hB hkE hkH + · push_neg at hkH + by_cases hkB : (k : WithTop Key) < r.begin.key + · by_cases hkHeq : ((res.keys.head hne : Key) : WithTop Key) = (k : WithTop Key) + · have hkEq : k = res.keys.head hne := WithTop.coe_injective hkHeq.symm + exact necessary_of_mem_result hread (hkEq ▸ List.head_mem hne) + · have hHlt : ((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) := + lt_of_le_of_ne hkH hkHeq + exact necessary_zero_cross_long_gap hsort hread hrev hmore hne hbegin hcross + hend hHlt hkB + · push_neg at hkB + by_cases hkL : (k : WithTop Key) ≤ + ((res.keys.getLast hne : Key) : WithTop Key) + · exact necessary_zero_cross_long_between_last hread hne hcross hkB + (WithTop.coe_le_coe.mp hkL) + · push_neg at hkL + have ht : reportedAtEnd r res < endOvershoot r := by + by_contra hcon + push_neg at hcon + have hte : reportedAtEnd r res = endOvershoot r := le_antisymm htle hcon + have hltmax := hkhi + rw [lt_max_iff] at hltmax + rcases hltmax with hlt | hlt + · rw [zeroCrossLongAnchor_end_eq r res hne hbegin hend, if_pos hte] at hlt + exact absurd (lt_keyAfter_iff k (res.keys.getLast hne) |>.mp + (WithTop.coe_lt_coe.mp hlt)) (not_le.mpr (WithTop.coe_lt_coe.mp hkL)) + · exact absurd hlt (not_lt.mpr hkB) + exact necessary_zero_cross_long_above_last hsort hread hrev hmore hne hbegin + hcross hlong hend ht hkB hkL + +end FormalConflictRange diff --git a/FormalConflictRange/ZeroCrossTrunc.lean b/FormalConflictRange/ZeroCrossTrunc.lean new file mode 100644 index 0000000..4043fae --- /dev/null +++ b/FormalConflictRange/ZeroCrossTrunc.lean @@ -0,0 +1,2363 @@ +/- +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.ZeroCrossLong +import FormalConflictRange.Reduction +import FormalConflictRange.NonposClosedForm + +/-! +# Crossed truncated results at begin offset zero + +At `begin.offset = 0` the begin anchor is `cnt begin.key - 1`, so a crossed read +(`begin.key` strictly above the reported head) resolves its begin selector exactly onto +the greatest stored key below `begin.key`, which compatibility forces to be the reported +head. No stored key can lie strictly between the head and `begin.key`. + +This module closes the family with an unbounded end selector (`end.key = ⊤` and +`end.offset ≥ 1`). Writing `head` for the reported head and `last` for the reported +last, the minimal range is + +`[head, max begin.key (keyAfter last))`. + +The lower endpoint is the reported head. The upper endpoint is `begin.key` when the +reported keys stop below it (every key in `(last, begin.key)` would lift the begin +resolution off the head), and `keyAfter last` when the reported keys already reach it. +-/ + +namespace FormalConflictRange + +/-- A forward truncated result at begin offset zero, crossed, with `end.offset = 1` +or an unbounded end selector (`end.key = ⊤`, `end.offset ≥ 1`). -/ +abbrev zeroCrossTruncReadAllEnd (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + (r.end_.offset = 1 ∨ (r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset)) + +/-- A forward truncated result at begin offset zero, crossed, with positive end offset +and every reported key strictly below `end.key`. -/ +abbrev zeroCrossTruncBelowEnd (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + 1 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key + +/-- A forward truncated result at begin offset zero, crossed, with positive end offset +and the end key at or above the reported head. -/ +abbrev zeroCrossTruncEndGeHead (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + 1 ≤ r.end_.offset ∧ + ((leastReported r res : Key) : WithTop Key) ≤ r.end_.key + +/-- A forward truncated result at begin offset zero, crossed, with the end key strictly +below the reported head and enough capacity in the gap `[end.key, head)` to absorb the +reported keys up to `end.offset - 1`. -/ +abbrev zeroCrossTruncLowEndSmall (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + 2 ≤ r.end_.offset ∧ + r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧ + intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) + + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) + +/-- A forward truncated result at begin offset zero, crossed, with the end key strictly +below the reported head and more keys available in the gap `[end.key, head)` than the +end offset can absorb. In this cell the whole gap up to the head is necessary. -/ +abbrev zeroCrossTruncLowEndBig (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + 2 ≤ r.end_.offset ∧ + r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧ + (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < + intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) + + (res.keys.length : ℕ∞) + +/-- The crossed truncated begin-zero closed form. -/ +noncomputable def closedFormZeroCrossTrunc (r : RangeRead) (res : Result) : ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), + max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩ + +/-- The crossed truncated begin-zero closed form when the gap `[end.key, head)` is +saturated: the lower endpoint drops to `end.key`. -/ +noncomputable def closedFormZeroCrossTruncLowEnd (r : RangeRead) (res : Result) : + ConflictRange := + ⟨r.end_.key, + max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩ + +/-- A forward truncated result at begin offset zero, crossed, with a nonpositive end +offset. -/ +abbrev zeroCrossTruncNonposEnd (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.end_.offset ≤ 0 + +/-- A forward truncated result at begin offset zero, crossed, with `end.offset = 0`. -/ +abbrev zeroCrossTruncZeroEnd (r : RangeRead) (res : Result) : Prop := + r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧ + ((leastReported r res : Key) : WithTop Key) < r.begin.key ∧ + r.end_.offset = 0 + +/-- The crossed truncated begin-zero closed form with a nonpositive end offset: the +reported head up to the larger of `begin.key` and `end_.key`. -/ +noncomputable def closedFormZeroCrossTruncNonposEnd (r : RangeRead) (res : Result) : + ConflictRange := + ⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), + max r.begin.key r.end_.key⟩ + +/-- `getLastD` agrees with `getLast` on a nonempty list. -/ +theorem getLastD_eq_getLast_zero_cross_trunc {l : List Key} (h : l ≠ []) : + l.getLastD ([] : Key) = l.getLast h := by + cases l with + | nil => exact absurd rfl h + | cons a t => + cases t with + | nil => rfl + | cons b t => simp [List.getLastD, List.getLast] + +/-- `headD` agrees with `head` on a nonempty list. -/ +theorem headD_eq_head_zero_cross_trunc {l : List Key} (h : l ≠ []) : + l.headD ([] : Key) = l.head h := by + rw [List.headD_eq_head?, List.head?_eq_some_head h]; rfl + +/-- At begin offset zero, a compatible crossed truncated read resolves its begin +selector onto the reported head. -/ +theorem resolve_begin_eq_head_of_zero_crossed_more {db : DB} {r : RangeRead} {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : + resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db r := + (RangeRead.compatible_keys_prefix hcomp).subset hhead_mem + have hhead_db : res.keys.head hne ∈ db.keys := + (RangeRead.mem_completeKeys.mp hhead_ck).1 + exact le_antisymm + (RangeRead.mem_completeKeys.mp hhead_ck).2.1 + (resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross + (res.keys.head hne) hhead_db hcross) + +/-- In a compatible crossed truncated begin-zero read, no stored key lies strictly +between the reported head and `begin.key`. -/ +theorem no_stored_between_head_begin_of_zero_crossed_more {db : DB} {r : RangeRead} + {res : Result} + (hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false) + (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) : + ∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne + have hhead_db : res.keys.head hne ∈ db.keys := + RangeRead.compatible_keys_subset hcomp hhead_mem + have hL := resolve_begin_eq_head_of_zero_crossed_more hcomp hrev hmore hne hbegin hcross + intro k hk hkb + have hle := resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross k hk hkb.2 + rw [hL] at hle + exact absurd hle (not_le.mpr hkb.1) + +/-- With an unbounded end selector, the complete keys are exactly the stored keys at +or above the begin resolution. -/ +theorem completeKeys_eq_filter_ge_of_end_top {db : DB} {r : RangeRead} {H : Key} + (hrev : r.reverse = false) (hendkey : r.end_.key = (⊤ : WithTop Key)) + (hendoff : 1 ≤ r.end_.offset) + (hbegin : resolve db r.begin = ((H : Key) : WithTop Key)) : + RangeRead.completeKeys db r = + db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ (k : WithTop Key))) := by + have htop : resolve db r.end_ = (⊤ : WithTop Key) := + resolve_eq_top_of_key_eq_top hendkey hendoff + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hbegin, htop] + apply List.filter_congr + intro k _ + rw [decide_eq_decide] + exact ⟨fun h => h.1, fun h => ⟨h, WithTop.coe_lt_top k⟩⟩ + +/-- The stored keys in `[H, last]` in a crossed truncated begin-zero read. -/ +theorem completeKeys_filter_le_of_end_top {db : DB} {r : RangeRead} {H last : Key} + (hrev : r.reverse = false) (hendkey : r.end_.key = (⊤ : WithTop Key)) + (hendoff : 1 ≤ r.end_.offset) + (hbegin : resolve db r.begin = ((H : Key) : WithTop Key)) : + (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) = + (db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + rw [completeKeys_eq_filter_ge_of_end_top (db := db) hrev hendkey hendoff hbegin] + +/-- For `end_.offset = 1`, a stored key strictly below the resolved end is strictly +below `end_.key`. -/ +theorem lt_end_key_of_lt_resolve_end_offset_one {db : DB} {r : RangeRead} + (hend : r.end_.offset = 1) {k : Key} (hk : k ∈ db.keys) + (h : (k : WithTop Key) < resolve db r.end_) : + (k : WithTop Key) < r.end_.key := by + set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) with hc + have hanc : r.end_.anchor db = (c : ℤ) := by + unfold Selector.anchor; rw [hc]; omega + by_cases hlt : c < db.keys.length + · have hres : resolve db r.end_ = (((db.keys[c]'hlt : Key)) : WithTop Key) := by + rw [resolve_eq_ite, ite_eq_right (by rw [hanc]; exact not_lt.mpr (Int.natCast_nonneg _)), + hanc, Int.toNat_natCast, List.getElem?_eq_getElem hlt] + rw [hres] at h + by_contra hkE + push Not at hkE + obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hk + have hcj : c ≤ j := by + by_contra hcon + have hjc : j < c := not_le.mp hcon + have hklt := getElem_lt_of_lt_countP_top (w := r.end_.key) db.sorted hj + (by rw [← hc]; exact hjc) + rw [hjx] at hklt + exact absurd hkE (not_le.mpr hklt) + have hle0 : db.keys[c] ≤ db.keys[j] := (db.sorted.getElem_le_getElem_iff).mpr hcj + rw [hjx] at hle0 + exact absurd h (not_lt.mpr (WithTop.coe_le_coe.mpr hle0)) + · have hclen : c = db.keys.length := + le_antisymm (by rw [hc]; exact List.countP_le_length) (not_lt.mp hlt) + have : decide ((k : WithTop Key) < r.end_.key) = true := + (List.countP_eq_length.mp hclen) k hk + exact of_decide_eq_true this + +/-- Filtering the complete keys at or below `last` reads off the stored keys in +`[H, last]`, provided the begin resolution is `H` and `last` lies below the end +resolution. This does not require the end resolution to be unbounded. -/ +theorem completeKeys_filter_le_of_resolve_begin {db : DB} {r : RangeRead} {H last : Key} + (hrev : r.reverse = false) + (hbegin : resolve db r.begin = ((H : Key) : WithTop Key)) + (hlt : ((last : Key) : WithTop Key) < resolve db r.end_) : + (RangeRead.completeKeys db r).filter + (fun k : Key => decide (k ≤ last)) = + db.keys.filter (fun k : Key => decide + (((H : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hbegin, List.filter_filter] + apply List.filter_congr + intro k _ + by_cases hH : ((H : Key) : WithTop Key) ≤ (k : WithTop Key) + · by_cases hL : k ≤ last + · have hR : (k : WithTop Key) < resolve db r.end_ := + lt_of_le_of_lt (WithTop.coe_le_coe.mpr hL) hlt + simp [hH, hL, hR] + · simp [hH, hL] + · simp [hH] + +/-- Splitting the stored keys in `[E, last]` at `head`. -/ +theorem filter_end_le_split_between {db : DB} {E : WithTop Key} {head last : Key} + (hEh : E ≤ (head : WithTop Key)) (hhl : head ≤ last) : + (db.keys.filter (fun (k : Key) => decide (E ≤ (k : WithTop Key)))).filter + (fun (k : Key) => decide (k ≤ last)) = + between db E ((head : Key) : WithTop Key) ++ + db.keys.filter (fun (k : Key) => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr ((db.sorted.pairwise.filter _).filter _) + · rw [List.sortedLT_iff_pairwise, List.pairwise_append] + refine ⟨?_, ?_, ?_⟩ + · rw [← List.sortedLT_iff_pairwise] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · rw [← List.sortedLT_iff_pairwise] + exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _) + · intro x hx y hy + simp only [between, List.mem_filter, decide_eq_true_eq] at hx + simp only [List.mem_filter, decide_eq_true_eq] at hy + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mp hx.2.2) (WithTop.coe_le_coe.mp hy.2.1) + · intro k + rw [List.mem_filter, List.mem_filter, List.mem_append, between, List.mem_filter, List.mem_filter] + simp only [decide_eq_true_eq] + constructor + · rintro ⟨⟨hkdb, hEk⟩, hklast⟩ + by_cases hkh : (k : WithTop Key) < (head : WithTop Key) + · exact Or.inl ⟨hkdb, hEk, hkh⟩ + · exact Or.inr ⟨hkdb, not_lt.mp hkh, hklast⟩ + · rintro (⟨hkdb, hEk, hkh⟩ | ⟨hkdb, hHk, hklast⟩) + · exact ⟨⟨hkdb, hEk⟩, le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkh)) hhl⟩ + · exact ⟨⟨hkdb, le_trans hEh hHk⟩, hklast⟩ + +/-- **Compatibility transfers across agreement on the crossed truncated begin-zero +range.** -/ +theorem compatible_of_agree_zeroCrossTrunc {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset = 1 ∨ (r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset)) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_upper : ((last : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross' + have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + intro k hk2 hkb + have hk1 : k ∈ db1.keys := + (hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2 + exact hno1 k hk1 hkb + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2 + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by + rcases hend with hend | ⟨hendkey, hendoff⟩ + · have hlast_db1 : last ∈ db1.keys := + (RangeRead.mem_completeKeys.mp hlast_ck1).1 + have hlastE : ((last : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_offset_one hend hlast_db1 hlt1 + have hcnt : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + have hzero : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = 0 := by + rw [List.length_eq_zero_iff, List.filter_eq_nil_iff] + intro k hk + have hkge : r.end_.key ≤ (k : WithTop Key) := + decide_eq_true_eq.mp (List.mem_filter.mp hk).2 + have hlastk : last < k := + WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlastE hkge) + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le] + exact hlastk + rw [hzero, hend] + simp + exact resolve_end_gt_of_filter_length_le (by rw [hend]) hcnt + · rw [resolve_eq_top_of_key_eq_top hendkey hendoff] + exact WithTop.coe_lt_top last + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hck1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hck2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + have heq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge, hle⟩ + · rintro ⟨hk2, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge, hle⟩ + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck2, ← heq, ← hck1, hres1len] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- **Compatibility transfers across agreement on the crossed truncated begin-zero +range when every reported key is below `end.key` and the end offset is at least one.** -/ +theorem compatible_of_agree_zeroCrossTruncBelowEnd {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hge : 1 ≤ r.end_.offset) + (hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_upper : ((last : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross' + have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + intro k hk2 hkb + have hk1 : k ∈ db1.keys := + (hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2 + exact hno1 k hk1 hkb + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2 + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hlastE : ((last : Key) : WithTop Key) < r.end_.key := by + rw [hlast] + exact hbelow (res.keys.getLast hne) (List.getLast_mem hne) + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + lt_of_lt_of_le hlastE (key_le_resolve_of_one_le (db := db2) (s := r.end_) hge) + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hck1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hck2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + have heq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge', hle⟩ + exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge', hle⟩ + · rintro ⟨hk2, hge', hle⟩ + exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge', hle⟩ + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck2, ← heq, ← hck1, hres1len] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- **Compatibility transfers across agreement on the crossed truncated begin-zero +range when the end key is at or above the reported head and the end offset is at +least one.** -/ +theorem compatible_of_agree_zeroCrossTruncEndGeHead {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hge : 1 ≤ r.end_.offset) + (hheadE : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hheadE' : ((head : Key) : WithTop Key) ≤ r.end_.key := by + rw [hhead]; exact hheadE + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_upper : ((last : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross' + have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + intro k hk2 hkb + have hk1 : k ∈ db1.keys := + (hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2 + exact hno1 k hk1 hkb + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2 + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hfiltereq : (db1.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) = + (db2.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last)) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _) + · exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk1, hEk⟩, hkle⟩ + exact ⟨⟨(hmk k (le_trans hheadE' hEk) + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mp hk1, hEk⟩, hkle⟩ + · rintro ⟨⟨hk2, hEk⟩, hkle⟩ + exact ⟨⟨(hmk k (le_trans hheadE' hEk) + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mpr hk2, hEk⟩, hkle⟩ + have hcnt1 : ((db1.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := + filter_length_le_offset_of_resolve_end_gt (db := db1) hge hlt1 + have hcnt2 : ((db2.keys.filter + (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [← hfiltereq] + exact hcnt1 + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + resolve_end_gt_of_filter_length_le (db := db2) hge hcnt2 + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hck1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hck2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + have heq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge', hle⟩ + exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge', hle⟩ + · rintro ⟨hk2, hge', hle⟩ + exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge', hle⟩ + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck2, ← heq, ← hck1, hres1len] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- **Compatibility transfers across agreement on the crossed truncated begin-zero +range when the end key is below the reported head but the gap to the head absorbs the +reported keys.** -/ +theorem compatible_of_agree_zeroCrossTruncLowEndSmall {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hge : 2 ≤ r.end_.offset) + (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hsmall : intervalCard r.end_.key (((res.keys.head hne : Key)) : WithTop Key) + + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞)) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by + rw [hhead]; exact hEhead + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_upper : ((last : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross' + have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + intro k hk2 hkb + have hk1 : k ∈ db1.keys := + (hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2 + exact hno1 k hk1 hkb + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2 + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hpart1 : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + have hck1 := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + rw [← hck1]; exact hres1len + have hpart2 : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + have heq : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db2.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk2, hHk, hklast⟩ + exact ⟨(hmk k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2, + hHk, hklast⟩ + · rintro ⟨hk1, hHk, hklast⟩ + exact ⟨(hmk k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1, + hHk, hklast⟩ + rw [heq, hpart1] + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + have hsplit := filter_end_le_split_between (db := db2) (E := r.end_.key) + (head := head) (last := last) (le_of_lt hEhead') hhead_le_last + have hbetween : (((between db2 r.end_.key ((head : Key) : WithTop Key)).length : ℕ∞)) ≤ + intervalCard r.end_.key ((head : Key) : WithTop Key) := + between_length_le_intervalCard (db := db2) (le_of_lt hEhead') + have hsum : ((((between db2 r.end_.key ((head : Key) : WithTop Key)).length + + res.keys.length : ℕ)) : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + rw [Nat.cast_add] + exact le_trans (add_le_add hbetween le_rfl) hsmall + have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + have hlen : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length = + (between db2 r.end_.key ((head : Key) : WithTop Key)).length + res.keys.length := by + rw [hsplit, hpart2, List.length_append] + rw [hlen] + exact_mod_cast hsum + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2 + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + rw [hck2, hpart2] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- **Compatibility transfers across agreement on the saturated crossed truncated +begin-zero range `[end.key, max begin.key (keyAfter last))`.** Here the end gap is +explicitly tracked, so the count of stored keys in `[end.key, last]` is the same in +both databases. -/ +theorem compatible_of_agree_zeroCrossTruncLowEndBig {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hge : 2 ≤ r.end_.offset) + (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormZeroCrossTruncLowEnd r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by + rw [hhead]; exact hEhead + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross + have hcr : closedFormZeroCrossTruncLowEnd r res = + ⟨r.end_.key, max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTruncLowEnd + rw [hlastD] + set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper + have hE_upper : r.end_.key < upper := lt_trans hEhead' (lt_of_lt_of_le hcross' (le_max_left _ _)) + have hhead_upper : ((head : Key) : WithTop Key) < upper := + lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_upper : ((last : Key) : WithTop Key) < upper := + lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _) + have hmem := (agreeOn_iff (closedFormZeroCrossTruncLowEnd r res) db1 db2).mp hagree + have hmk : ∀ k : Key, r.end_.key ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := + (hmk head (le_of_lt hEhead') hhead_upper).mp hhead_db1 + have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross' + have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + intro k hk2 hkb + have hk1 : k ∈ db1.keys := + (hmk k (le_trans (le_of_lt hEhead') (le_of_lt hkb.1)) + (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2 + exact hno1 k hk1 hkb + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2 + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hpart1 : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + have hck1 := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + rw [← hck1]; exact hres1len + have hpart2 : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by + have heq : db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr (db2.sorted.pairwise.filter _) + · exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk2, hHk, hklast⟩ + exact ⟨(hmk k (le_trans (le_of_lt hEhead') hHk) + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2, + hHk, hklast⟩ + · rintro ⟨hk1, hHk, hklast⟩ + exact ⟨(hmk k (le_trans (le_of_lt hEhead') hHk) + (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1, + hHk, hklast⟩ + rw [heq, hpart1] + have hcnt1 : ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := + filter_length_le_offset_of_resolve_end_gt (db := db1) (by omega) hlt1 + have hcnt_eq : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))) = + ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))) := by + apply List.SortedLT.eq_of_mem_iff + · exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _) + · exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _) + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨⟨hk2, hEk⟩, hklast⟩ + exact ⟨⟨(hmk k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2, + hEk⟩, hklast⟩ + · rintro ⟨⟨hk1, hEk⟩, hklast⟩ + exact ⟨⟨(hmk k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1, + hEk⟩, hklast⟩ + have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter + (fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by + rw [hcnt_eq]; exact hcnt1 + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := + resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2 + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + rw [hck2, hpart2] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- **The crossed truncated begin-zero closed form is minimal** when the end selector +is unbounded. -/ +theorem minimal_closedForm_of_zeroCross_trunc_readAllEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncReadAllEnd r res) : + Minimal r res (closedFormZeroCrossTrunc r res) := by + obtain ⟨hbegin, hrev, hmore, hcrossD, hend⟩ := h + have hread' : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread' + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD + exact hcrossD + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_zeroCrossTrunc hrev hmore hne hbegin hcross hend hc1 hagree + · intro hc2 + exact compatible_of_agree_zeroCrossTrunc hrev hmore hne hbegin hcross hend hc2 + (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle + · -- `last < k < begin.key` + have hk_gt_last : last < k := lt_of_not_ge hkle + have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last + have hk_lt_B : (k : WithTop Key) < r.begin.key := by + by_contra hnot + have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot + by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + (((keyAfter last : Key)) : WithTop Key) := max_eq_right hc + rw [hupper] at hkhi + have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi) + exact absurd hkle' (not_le.mpr hk_gt_last) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + r.begin.key := max_eq_left (le_of_not_ge hc) + rw [hupper] at hkhi + exact absurd hBle (not_le.mpr hkhi) + have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne + hbegin hcross' + have hk_notmem : k ∉ db₀.keys := by + intro hkdb + exact hno k hkdb ⟨by exact WithTop.coe_lt_coe.mpr hhead_lt_k, hk_lt_B⟩ + let db' : DB := toggle db₀ k + have hdb' : db' = db₀.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem + have hk_mem' : k ∈ db'.keys := by + rw [hdb'] + exact DB.mem_insert_self db₀ k hk_notmem + have hno' : ∀ z ∈ db'.keys, ¬ (((k : Key) : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + rw [hdb', DB.insert_keys, List.mem_orderedInsert] at hz + rcases hz with hzk | hzold + · subst hzk + exact absurd hzb.1 (not_lt.mpr le_rfl) + · have hz0 : z ∈ db₀.keys := hzold + exact hno z hz0 ⟨lt_trans (WithTop.coe_lt_coe.mpr hhead_lt_k) hzb.1, hzb.2⟩ + have hresb : resolve db' r.begin = ((k : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hk_mem' hk_lt_B hno' + have hnot : ¬ RangeRead.Compatible db' r res := by + intro hcomp' + have h := ((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcomp').2 + have hempty : (RangeRead.completeKeys db' r).filter + (fun x : Key => decide (x ≤ last)) = [] := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hresb] + rw [List.filter_eq_nil_iff] + intro x hx + have hx' : ((k : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db' r.end_ := + decide_eq_true_eq.mp (List.mem_filter.mp hx).2 + have hkx : k ≤ x := WithTop.coe_le_coe.mp hx'.1 + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le] + exact lt_of_lt_of_le hk_gt_last hkx + rw [hempty] at h + exact hne h.symm + exact necessary_of_toggle ⟨db₀, hcomp₀, hnot⟩ + +/-- **The crossed truncated begin-zero closed form is minimal** when every reported key is +below `end.key` and `end.offset ≥ 1`. -/ +theorem minimal_closedForm_of_zeroCross_trunc_belowEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncBelowEnd r res) : + Minimal r res (closedFormZeroCrossTrunc r res) := by + obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hbelow⟩ := h + have hread' : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread' + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD + exact hcrossD + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_zeroCrossTruncBelowEnd hrev hmore hne hbegin hcross hge + hbelow hc1 hagree + · intro hc2 + exact compatible_of_agree_zeroCrossTruncBelowEnd hrev hmore hne hbegin hcross hge + hbelow hc2 (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle + · have hk_gt_last : last < k := lt_of_not_ge hkle + have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last + have hk_lt_B : (k : WithTop Key) < r.begin.key := by + by_contra hnot + have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot + by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + (((keyAfter last : Key)) : WithTop Key) := max_eq_right hc + rw [hupper] at hkhi + have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi) + exact absurd hkle' (not_le.mpr hk_gt_last) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + r.begin.key := max_eq_left (le_of_not_ge hc) + rw [hupper] at hkhi + exact absurd hBle (not_le.mpr hkhi) + -- insert `k` into the gap `(last, begin.key)` + have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne + hbegin hcross' + have hk_notmem : k ∉ db₀.keys := by + intro hkdb + exact hno k hkdb ⟨by exact WithTop.coe_lt_coe.mpr hhead_lt_k, hk_lt_B⟩ + let db' : DB := toggle db₀ k + have hdb' : db' = db₀.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem + have hk_mem' : k ∈ db'.keys := by + rw [hdb'] + exact DB.mem_insert_self db₀ k hk_notmem + have hno' : ∀ z ∈ db'.keys, ¬ (((k : Key) : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + rw [hdb', DB.insert_keys, List.mem_orderedInsert] at hz + rcases hz with hzk | hzold + · subst hzk + exact absurd hzb.1 (not_lt.mpr le_rfl) + · have hz0 : z ∈ db₀.keys := hzold + exact hno z hz0 ⟨lt_trans (WithTop.coe_lt_coe.mpr hhead_lt_k) hzb.1, hzb.2⟩ + have hresb : resolve db' r.begin = ((k : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hk_mem' hk_lt_B hno' + have hnot : ¬ RangeRead.Compatible db' r res := by + intro hcomp' + have h := ((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcomp').2 + have hempty : (RangeRead.completeKeys db' r).filter + (fun x : Key => decide (x ≤ last)) = [] := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hresb] + rw [List.filter_eq_nil_iff] + intro x hx + have hx' : ((k : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db' r.end_ := + decide_eq_true_eq.mp (List.mem_filter.mp hx).2 + have hkx : k ≤ x := WithTop.coe_le_coe.mp hx'.1 + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le] + exact lt_of_lt_of_le hk_gt_last hkx + rw [hempty] at h + exact hne h.symm + exact necessary_of_toggle ⟨db₀, hcomp₀, hnot⟩ + +/-- **Compatibility transfers across agreement on the nonpositive-end crossed +truncated begin-zero range.** -/ +theorem compatible_of_agree_zeroCrossTruncNonposEnd {r : RangeRead} {res : Result} + (hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ []) + (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset ≤ 0) + {db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res) + (hagree : AgreeOn (closedFormZeroCrossTruncNonposEnd r res) db1 db2) : + RangeRead.Compatible db2 r res := by + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by + rw [hhead] + exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross + have hcr : closedFormZeroCrossTruncNonposEnd r res = + ⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by + unfold closedFormZeroCrossTruncNonposEnd + rw [hheadD] + set upper : WithTop Key := max r.begin.key r.end_.key with hupper + have hhead_upper : ((head : Key) : WithTop Key) < upper := by + rw [hupper] + exact lt_of_lt_of_le hcross' (le_max_left _ _) + have hlast_mem_res : last ∈ res.keys := by + rw [hlast]; exact List.getLast_mem hne + have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r := + (RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res + have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck1).2.2 + have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + have hlast_upper : ((last : Key) : WithTop Key) < upper := by + have hle := resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend + exact lt_of_lt_of_le (lt_of_lt_of_le hlt1 hle) (by rw [hupper]; exact le_max_right _ _) + have hmem := (agreeOn_iff (closedFormZeroCrossTruncNonposEnd r res) db1 db2).mp hagree + have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) → + (k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by + intro k hk1 hk2 + refine hmem k ?_ + rw [hcr] + exact ⟨hk1, hk2⟩ + have hhead_db1 : head ∈ db1.keys := + RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne) + have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1 + have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross' + have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧ + (k : WithTop Key) < r.begin.key) := by + intro k hk2 hkb + have hk1 : k ∈ db1.keys := + (hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (by rw [hupper]; exact le_max_left _ _))).mpr hk2 + exact hno1 k hk1 hkb + have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2 + have hne_top : resolve db1 r.end_ ≠ (⊤ : WithTop Key) := + resolve_ne_top_of_offset_nonpos hend + obtain ⟨q, hq⟩ := WithTop.ne_top_iff_exists.mp hne_top + have hlast_q : last < q := by + have h := hlt1 + rw [← hq] at h + exact WithTop.coe_lt_coe.mp h + have hqne : q ≠ ([] : Key) := by + intro hnil + have h : last < ([] : Key) := by rwa [hnil] at hlast_q + exact absurd h (List.not_lt_nil last) + have hq_le_E : ((q : Key) : WithTop Key) ≤ r.end_.key := by + rw [hq] + exact resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend + have hhead_q : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := + WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hhead_le_last hlast_q)) + have hEnd_upper : r.end_.key ≤ upper := by + rw [hupper]; exact le_max_right _ _ + have hagreeEnd : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mp hk1, hge, hlt⟩ + · rintro ⟨hk2, hge, hlt⟩ + exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mpr hk2, hge, hlt⟩ + have hqget := resolve_filter_reverse_eq_some_of_ne_nil (db := db1) (s := r.end_) hend hqne hq.symm + have hresEnd2 : resolve db2 r.end_ = ((q : Key) : WithTop Key) := + resolve_eq_of_agree_between (db₁ := db1) (db₂ := db2) (s := r.end_) hend + (E := r.end_.key) rfl (le_trans hhead_q hq_le_E) hagreeEnd hqget hhead_q + have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by + rw [hresEnd2] + exact WithTop.coe_lt_coe.mpr hlast_q + have hres1len : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2 + rwa [← hlast] at h + have hck1 : (RangeRead.completeKeys db1 r).filter + (fun k : Key => decide (k ≤ last)) = + db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1 + have hck2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := + completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2 + have heq : db1.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = + db2.keys.filter (fun k : Key => decide + (((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by + apply eq_of_forall_mem_iff_of_pairwise + · exact db1.sorted.pairwise.filter _ + · exact db2.sorted.pairwise.filter _ + · intro k + simp only [List.mem_filter, decide_eq_true_eq] + constructor + · rintro ⟨hk1, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1, + hge, hle⟩ + · rintro ⟨hk2, hge, hle⟩ + exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2, + hge, hle⟩ + have h2 : (RangeRead.completeKeys db2 r).filter + (fun k : Key => decide (k ≤ last)) = res.keys := by + rw [hck2, ← heq, ← hck1, hres1len] + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hc1.1, h2⟩ + +/-- A key strictly between the reported last and `begin.key` is necessary in a crossed +truncated begin-zero read. -/ +theorem necessary_zeroCross_trunc_gap {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hkgt : res.keys.getLast hne < k) (hkB : (k : WithTop Key) < r.begin.key) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hkgt + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne + hbegin hcross' + have hk_notmem : k ∉ db₀.keys := by + intro hkdb + exact hno k hkdb ⟨WithTop.coe_lt_coe.mpr hhead_lt_k, hkB⟩ + let db' : DB := toggle db₀ k + have hdb' : db' = db₀.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem + have hk_mem' : k ∈ db'.keys := by + rw [hdb'] + exact DB.mem_insert_self db₀ k hk_notmem + have hno' : ∀ z ∈ db'.keys, ¬ (((k : Key) : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + rw [hdb', DB.insert_keys, List.mem_orderedInsert] at hz + rcases hz with hzk | hzold + · subst hzk + exact absurd hzb.1 (not_lt.mpr le_rfl) + · have hz0 : z ∈ db₀.keys := hzold + exact hno z hz0 ⟨lt_trans (WithTop.coe_lt_coe.mpr hhead_lt_k) hzb.1, hzb.2⟩ + have hresb : resolve db' r.begin = ((k : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hk_mem' hkB hno' + have hnot : ¬ RangeRead.Compatible db' r res := by + intro hcomp' + have h := ((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcomp').2 + have hempty : (RangeRead.completeKeys db' r).filter + (fun x : Key => decide (x ≤ last)) = [] := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hresb] + rw [List.filter_eq_nil_iff] + intro x hx + have hx' : ((k : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < resolve db' r.end_ := + decide_eq_true_eq.mp (List.mem_filter.mp hx).2 + have hkx : k ≤ x := WithTop.coe_le_coe.mp hx'.1 + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le] + exact lt_of_lt_of_le hkgt hkx + rw [hempty] at h + exact hne h.symm + exact necessary_of_toggle ⟨db₀, hcomp₀, hnot⟩ + +/-- **The crossed truncated begin-zero closed form is minimal** when the end key is at +or above the reported head and `end.offset ≥ 1`. -/ +theorem minimal_closedForm_of_zeroCross_trunc_endGeHead {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncEndGeHead r res) : + Minimal r res (closedFormZeroCrossTrunc r res) := by + obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hheadE⟩ := h + have hread' : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread' + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD + exact hcrossD + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hheadE' : ((head : Key) : WithTop Key) ≤ r.end_.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, hheadD] at hheadE + exact hheadE + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_zeroCrossTruncEndGeHead hrev hmore hne hbegin hcross hge + hheadE' hc1 hagree + · intro hc2 + exact compatible_of_agree_zeroCrossTruncEndGeHead hrev hmore hne hbegin hcross hge + hheadE' hc2 (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle + · have hk_gt_last : last < k := lt_of_not_ge hkle + have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last + have hk_lt_B : (k : WithTop Key) < r.begin.key := by + by_contra hnot + have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot + by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + (((keyAfter last : Key)) : WithTop Key) := max_eq_right hc + rw [hupper] at hkhi + have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi) + exact absurd hkle' (not_le.mpr hk_gt_last) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + r.begin.key := max_eq_left (le_of_not_ge hc) + rw [hupper] at hkhi + exact absurd hBle (not_le.mpr hkhi) + exact necessary_zeroCross_trunc_gap hread hrev hmore hne hbegin hcross hk_gt_last hk_lt_B + +/-- A bounded gap with more keys than the end offset can absorb makes every key in +`[end.key, head)` necessary: filling the gap to the offset and then adding the target +overflows the end resolution. -/ +theorem necessary_zeroCross_trunc_between_end_head {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hge : 2 ≤ r.end_.offset) + (hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key)) + (hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞)) + (hkE : r.end_.key ≤ (k : WithTop Key)) + (hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + set e1 : ℕ := (r.end_.offset - 1).toNat with he1 + set rl : ℕ := res.keys.length with hrl + set n : ℕ := e1 - rl with hn + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by rw [hhead]; exact hcross + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead + have hkH' : (k : WithTop Key) < ((head : Key) : WithTop Key) := by rw [hhead]; exact hkH + have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hres_pair : res.keys.Pairwise (· ≤ ·) := hres_sorted.pairwise.imp le_of_lt + have hhead_le : ∀ x ∈ res.keys, head ≤ x := by + intro x hx + have := hres_pair.rel_head hx + simpa [hhead] using this + have hle_last : ∀ x ∈ res.keys, x ≤ last := by + intro x hx + have := hres_pair.rel_getLast hx + simpa [hlast] using this + have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne + have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne + have hhead_le_last : head ≤ last := hhead_le last hlast_mem + have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne hbegin hcross' + -- The reported length is bounded by the end offset. + have hlast_ck0 : last ∈ RangeRead.completeKeys db₀ r := + (RangeRead.compatible_keys_prefix hcomp₀).subset hlast_mem + have hlt0 : ((last : Key) : WithTop Key) < resolve db₀ r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck0).2.2 + have hcnt0 : ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ last))).length ≤ e1 := by + simpa [he1] using filter_length_le_offset_of_resolve_end_gt (db := db₀) (by omega) hlt0 + have hres_sub : res.keys ⊆ + ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ last))) := by + intro x hx + have hxdb : x ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hx + have hxE : r.end_.key ≤ (x : WithTop Key) := + le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)) + have hxl : x ≤ last := hle_last x hx + rw [List.mem_filter, List.mem_filter] + exact ⟨⟨hxdb, by simpa using hxE⟩, by simpa using hxl⟩ + have hrle : rl ≤ e1 := by + have := hres_sorted.nodup.length_le_of_subset hres_sub + exact le_trans (by simpa [hrl] using this) hcnt0 + -- Capacity in the gap to build the witness. + have hbig' : (e1 : ℕ∞) < intervalCard r.end_.key ((head : Key) : WithTop Key) + + (rl : ℕ∞) := by + simpa [he1, hrl, hhead] using hbig + have hcard : ((n + 1 : ℕ) : ℕ∞) ≤ + intervalCard r.end_.key ((head : Key) : WithTop Key) := by + by_cases htop : intervalCard r.end_.key ((head : Key) : WithTop Key) = (⊤ : ℕ∞) + · rw [htop]; exact le_top + · obtain ⟨G, hG⟩ := ENat.ne_top_iff_exists.mp htop + have hlt : e1 < G + rl := by + have hb := hbig' + rw [← hG] at hb + rw [← ENat.coe_add] at hb + exact ENat.coe_lt_coe.mp hb + have hGle : n + 1 ≤ G := by + rw [hn]; omega + rw [← hG] + exact ENat.coe_le_coe.mpr hGle + obtain ⟨dbK, hKrange, hkK, hKlen⟩ := + exists_db_keysIn_avoid (B := r.end_.key) (E := ((head : Key) : WithTop Key)) + (k := k) (m := n) hcard + have hK_ge : ∀ x ∈ dbK.keys, r.end_.key ≤ (x : WithTop Key) := + fun x hx => (hKrange x hx).1 + have hK_lt : ∀ x ∈ dbK.keys, x < head := + fun x hx => WithTop.coe_lt_coe.mp (hKrange x hx).2 + -- The witness database. + have hsort_append : (dbK.keys ++ res.keys).SortedLT := by + refine List.sortedLT_append.mpr ⟨dbK.sorted, hres_sorted, ?_⟩ + intro a ha b hb + exact lt_of_lt_of_le (hK_lt a ha) (hhead_le b hb) + let dbw : DB := ⟨dbK.keys ++ res.keys, hsort_append⟩ + have hdbw : dbw.keys = dbK.keys ++ res.keys := rfl + have hk_notmem : k ∉ dbw.keys := by + rw [hdbw, List.mem_append] + rintro (hkK' | hkres) + · exact hkK hkK' + · exact absurd (hhead_le k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkH')) + have hhead_dbw : head ∈ dbw.keys := by + rw [hdbw]; exact List.mem_append.mpr (Or.inr hhead_mem) + have hno_dbw : ∀ z ∈ dbw.keys, ¬ (((head : Key) : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + rw [hdbw, List.mem_append] at hz + rcases hz with hz | hz + · exact absurd hzb.1 (not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_lt (hK_lt z hz)))) + · exact hno z (RangeRead.compatible_keys_subset hcomp₀ hz) hzb + have hresb : resolve dbw r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_dbw hcross' hno_dbw + have hmem_all_E : ∀ x ∈ dbw.keys, r.end_.key ≤ (x : WithTop Key) := by + intro x hx + rw [hdbw, List.mem_append] at hx + rcases hx with hx | hx + · exact hK_ge x hx + · exact le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)) + have hcntE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by + rw [List.countP_eq_zero] + intro x hx + rw [decide_eq_true_eq] + exact not_lt.mpr (hmem_all_E x hx) + have hdbw_len : dbw.keys.length = e1 := by + rw [hdbw, List.length_append, hKlen, ← hrl, hn] + exact Nat.sub_add_cancel hrle + have hanchor : (r.end_.anchor dbw).toNat = e1 := by + rw [anchor_toNat_eq (db := dbw) (s := r.end_) (by omega), hcntE, ← he1] + omega + have hrestop : resolve dbw r.end_ = (⊤ : WithTop Key) := by + apply resolve_eq_top + · exact anchor_nonneg_of_one_le (db := dbw) (s := r.end_) (by omega) + · rw [hanchor, hdbw_len] + have hck : RangeRead.completeKeys dbw r = res.keys := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hresb, hrestop] + have hpred : (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < (⊤ : WithTop Key))) = + (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) := by + funext x; simp + rw [hpred, hdbw, List.filter_append] + have hleft : dbK.keys.filter + (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + rw [decide_eq_true_eq] + exact not_le.mpr (WithTop.coe_lt_coe.mpr (hK_lt x hx)) + have hright : res.keys.filter + (fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = res.keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact WithTop.coe_le_coe.mpr (hhead_le x hx) + rw [hleft, hright, List.nil_append] + have hfilter_le : res.keys.filter (fun x : Key => decide (x ≤ last)) = res.keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact hle_last x hx + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨by simpa using hcomp₀.1, by rw [hck, hfilter_le]⟩ + -- Toggling the target on overflows the end offset. + have hdb' : toggle dbw k = dbw.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem + have hkeys_toggle : (toggle dbw k).keys = dbw.keys.orderedInsert (· ≤ ·) k := by + rw [hdb', DB.insert_keys] + have hlen_toggle : (toggle dbw k).keys.length = e1 + 1 := by + rw [hkeys_toggle, List.orderedInsert_length, hdbw_len] + have hmem_all : ∀ x ∈ (toggle dbw k).keys, + r.end_.key ≤ (x : WithTop Key) ∧ x ≤ last := by + intro x hx + rw [hkeys_toggle, List.mem_orderedInsert] at hx + rcases hx with rfl | hx + · exact ⟨hkE, le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkH')) hhead_le_last⟩ + · rw [hdbw, List.mem_append] at hx + rcases hx with hx | hx + · exact ⟨hK_ge x hx, le_trans (le_of_lt (hK_lt x hx)) hhead_le_last⟩ + · exact ⟨le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)), + hle_last x hx⟩ + have hcount_toggle : + ((toggle dbw k).keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter + (fun x : Key => decide (x ≤ last)) = (toggle dbw k).keys := by + have h1 : (toggle dbw k).keys.filter + (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) = (toggle dbw k).keys := by + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact (hmem_all x hx).1 + rw [h1] + apply List.filter_eq_self.mpr + intro x hx + rw [decide_eq_true_eq] + exact (hmem_all x hx).2 + have hnot : ¬ RangeRead.Compatible (toggle dbw k) r res := by + intro hc + have hlast_ck : last ∈ RangeRead.completeKeys (toggle dbw k) r := + (RangeRead.compatible_keys_prefix hc).subset hlast_mem + have hlt : ((last : Key) : WithTop Key) < resolve (toggle dbw k) r.end_ := + (RangeRead.mem_completeKeys.mp hlast_ck).2.2 + have hcnt := filter_length_le_offset_of_resolve_end_gt (db := toggle dbw k) (by omega) hlt + rw [hcount_toggle, hlen_toggle, ← he1] at hcnt + omega + exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩ + +/-- **The crossed truncated begin-zero closed form is minimal** when the end gap is +saturated: the whole gap `[end.key, head)` is necessary, so the lower endpoint drops to +`end.key`. -/ +theorem minimal_closedForm_of_zeroCross_trunc_lowEndBig {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncLowEndBig r res) : + Minimal r res (closedFormZeroCrossTruncLowEnd r res) := by + obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hEheadD, hbigD⟩ := h + have hread' : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread' + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD + exact hcrossD + have hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hEheadD + exact hEheadD + have hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < + intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) + + (res.keys.length : ℕ∞) := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hbigD + exact hbigD + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by + rw [hhead]; exact hEhead + have hbig' : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) < + intervalCard r.end_.key ((head : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by + simpa [hhead] using hbig + have hcr : closedFormZeroCrossTruncLowEnd r res = + ⟨r.end_.key, + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTruncLowEnd + rw [hlastD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_zeroCrossTruncLowEndBig hrev hmore hne hbegin hcross hge + hEhead' hc1 hagree + · intro hc2 + exact compatible_of_agree_zeroCrossTruncLowEndBig hrev hmore hne hbegin hcross hge + hEhead' hc2 (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · by_cases hkhead : k < head + · exact necessary_zeroCross_trunc_between_end_head hread hrev hmore hne hbegin hcross hge + hEhead' hbig' hklo (WithTop.coe_lt_coe.mpr hkhead) + · exact Necessary.of_between hhead_nec hlast_nec (le_of_not_gt hkhead) hkle + · have hk_gt_last : last < k := lt_of_not_ge hkle + have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last + have hk_lt_B : (k : WithTop Key) < r.begin.key := by + by_contra hnot + have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot + by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + (((keyAfter last : Key)) : WithTop Key) := max_eq_right hc + rw [hupper] at hkhi + have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi) + exact absurd hkle' (not_le.mpr hk_gt_last) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + r.begin.key := max_eq_left (le_of_not_ge hc) + rw [hupper] at hkhi + exact absurd hBle (not_le.mpr hkhi) + exact necessary_zeroCross_trunc_gap hread hrev hmore hne hbegin hcross hk_gt_last hk_lt_B + +/-- **The crossed truncated begin-zero closed form is minimal** when the end key is +below the reported head but the gap to the head absorbs the reported keys. -/ +theorem minimal_closedForm_of_zeroCross_trunc_lowEndSmall {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncLowEndSmall r res) : + Minimal r res (closedFormZeroCrossTrunc r res) := by + obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hEheadD, hsmallD⟩ := h + have hread' : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread' + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD + exact hcrossD + have hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hEheadD + exact hEheadD + have hsmall : intervalCard r.end_.key (((res.keys.head hne : Key)) : WithTop Key) + + (res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hsmallD + exact hsmallD + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hlastD : res.keys.getLastD ([] : Key) = last := by + rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by + rw [hhead]; exact hEhead + have hcr : closedFormZeroCrossTrunc r res = + ⟨((head : Key) : WithTop Key), + max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by + unfold closedFormZeroCrossTrunc + rw [hheadD, hlastD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_zeroCrossTruncLowEndSmall hrev hmore hne hbegin hcross hge + hEhead' hsmall hc1 hagree + · intro hc2 + exact compatible_of_agree_zeroCrossTruncLowEndSmall hrev hmore hne hbegin hcross hge + hEhead' hsmall hc2 (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle + · have hk_gt_last : last < k := lt_of_not_ge hkle + have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last + have hk_lt_B : (k : WithTop Key) < r.begin.key := by + by_contra hnot + have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot + by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + (((keyAfter last : Key)) : WithTop Key) := max_eq_right hc + rw [hupper] at hkhi + have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi) + exact absurd hkle' (not_le.mpr hk_gt_last) + · have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) = + r.begin.key := max_eq_left (le_of_not_ge hc) + rw [hupper] at hkhi + exact absurd hBle (not_le.mpr hkhi) + exact necessary_zeroCross_trunc_gap hread hrev hmore hne hbegin hcross hk_gt_last hk_lt_B + +/-- A key at or above `begin.key` and below `end.key` is necessary in the crossed +truncated begin-zero read with `end.offset = 0`. -/ +theorem necessary_zeroCross_trunc_above_begin {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset = 0) + (hkgt : res.keys.getLast hne < k) + (hkE : (k : WithTop Key) < r.end_.key) + (hkge : r.begin.key ≤ (k : WithTop Key)) : + Necessary r res k := by + obtain ⟨db₀, hcomp₀⟩ := hread + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hk_notmem : k ∉ res.keys := by + intro hk + exact absurd (hle_last k hk) (not_le.mpr hkgt) + have hsortD : (res.keys ++ [k]).SortedLT := by + refine List.sortedLT_append.mpr ⟨hsort, ?_, ?_⟩ + · rw [List.sortedLT_iff_pairwise] + exact List.pairwise_singleton _ k + intro a ha b hb + simp only [List.mem_singleton] at hb + subst hb + exact lt_of_le_of_lt (hle_last a ha) hkgt + let dbw : DB := ⟨res.keys ++ [k], hsortD⟩ + have hdbw : dbw.keys = res.keys ++ [k] := rfl + have hk_mem : k ∈ dbw.keys := by rw [hdbw]; simp + have hhead_mem : head ∈ dbw.keys := by + rw [hdbw, hhead] + exact List.mem_append.mpr (Or.inl (List.head_mem hne)) + have hno : ∀ z ∈ dbw.keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + rw [hdbw] at hz + rcases List.mem_append.mp hz with hz | hz + · have hz0 : z ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hz + exact no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne hbegin hcross' + z hz0 hzb + · simp only [List.mem_singleton] at hz + subst hz + exact absurd hkge (not_le.mpr hzb.2) + have hresb : resolve dbw r.begin = (↑head : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_mem hcross' hno + have hDlt : ∀ x ∈ dbw.keys, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hdbw] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) + (lt_trans (WithTop.coe_lt_coe.mpr hkgt) hkE) + · simp only [List.mem_singleton] at hx + subst hx + exact hkE + have hcount_end : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + dbw.keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hDlt x hx + have hlen : dbw.keys.length = res.keys.length + 1 := by + rw [hdbw, List.length_append] + simp + have hanchor : r.end_.anchor dbw = (res.keys.length : Int) := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlen] + push_cast + rw [hend] + ring + | coe e => + have hce : dbw.keys.countP (fun x : Key => decide (x < e)) = dbw.keys.length := by + rw [← countP_lt_coe (l := dbw.keys) e, ← hE] + exact hcount_end + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlen] + push_cast + rw [hend] + ring + have hget : dbw.keys[res.keys.length]? = some k := by + rw [hdbw, List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + simp + have hre : resolve dbw r.end_ = ((k : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor]; omega)] + rw [hanchor, Int.toNat_natCast, hget] + have hltk : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [hre] + exact WithTop.coe_lt_coe.mpr hkgt + have hckw : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ last)) = res.keys := by + rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hresb hltk] + rw [hdbw, List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx), hle_last x hx⟩ + have hk_fil : [k].filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [List.mem_singleton] at hx + subst hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr hkgt + rw [hres_fil, hk_fil, List.append_nil] + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨hcomp₀.1, hckw⟩ + have hD' : (dbw.erase k).keys = res.keys := by + rw [DB.erase_keys, hdbw, List.erase_append_right [k] hk_notmem] + simp + have hsort' : (dbw.erase k).keys.SortedLT := (dbw.erase k).sorted + have hcount_end' : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + rw [hD'] at hx + exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) + (lt_trans (WithTop.coe_lt_coe.mpr hkgt) hkE) + have hlen' : (dbw.erase k).keys.length = res.keys.length := by rw [hD'] + have hanchor' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlen'] + push_cast + rw [hend] + ring + | coe e => + have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) = + (dbw.erase k).keys.length := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_end' + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlen'] + push_cast + rw [hend] + ring + have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne + have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by + rw [Nat.cast_sub ht1] + norm_num + have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor', hEqInt, Int.toNat_natCast] + have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by + rw [hD', List.getElem?_eq_getElem (by omega)] + congr 1 + rw [hlast, List.getLast_eq_getElem hne] + have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor']; omega)] + rw [hanchor', hEqInt, Int.toNat_natCast, hD'get?] + have hresb' : resolve (dbw.erase k) r.begin = (↑head : WithTop Key) := by + have hhead_mem' : head ∈ (dbw.erase k).keys := by + rw [hD', hhead] + exact List.head_mem hne + have hno' : ∀ z ∈ (dbw.erase k).keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + rw [hD'] at hz + exact hno z (by rw [hdbw]; exact List.mem_append.mpr (Or.inl hz)) hzb + exact resolve_begin_eq_of_no_between_zero hbegin hhead_mem' hcross' hno' + have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hresb', hre'] + have hpred : ∀ x ∈ (dbw.erase k).keys, + decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((↑last : Key) : WithTop Key)) = + decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ x < last) := by + intro x _ + rw [decide_eq_decide] + exact and_congr_right fun _ => WithTop.coe_lt_coe + rw [List.filter_congr hpred, hD'] + have hhead_le_all : ∀ x ∈ res.keys, ((head : Key) : WithTop Key) ≤ (x : WithTop Key) := by + intro x hx + exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx) + rw [show res.keys.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) = + res.keys.filter (fun x : Key => decide (x < last)) from by + apply List.filter_congr + intro x hx + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hhead_le_all x hx, h⟩⟩] + conv_lhs => rw [← List.dropLast_append_getLast hne, hlast] + rw [List.filter_append] + have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have h := hsort.pairwise.rel_dropLast_getLast hx + simpa [hlast] using h + rw [hfil] + simp + have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + intro h + have heq := h.2 + rw [hckE] at heq + have hfil : res.keys.dropLast.filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [← hlast] + exact hle_last x (List.mem_of_mem_dropLast hx) + rw [hfil] at heq + have hlen := congrArg List.length heq + rw [List.length_dropLast] at hlen + omega + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_erase_of_mem hk_mem] + exact hnot⟩ + +/-- For a crossed truncated begin-zero read with a nonpositive end offset, a key in +`[begin.key, end_.key)` above the reported last has a toggle witness. The witness adds +the `(1 - end.offset)` filler keys needed to land the end resolution just above the +reported last. -/ +theorem exists_witness_zeroCross_trunc_nonposEnd {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset ≤ 0) + (hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key) + (hkge : r.begin.key ≤ (k : WithTop Key)) : + ∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧ + ¬ RangeRead.Compatible (dbw.erase k) r res := by + obtain ⟨db, hcomp⟩ := hread + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev + have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => + (hsort.pairwise.imp le_of_lt).rel_getLast hx + have hhead_le_last : head ≤ last := by + simp only [hhead, hlast] + exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne) + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hlast_ck : last ∈ 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 : ((last : Key) : WithTop Key) < r.end_.key := + lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res + have hfill := filler_count_nonpos_more_nonpos_end hcomp hrev hmore hne (le_of_eq hbegin) hend + set m := (1 - r.end_.offset).toNat with hm + have hm1 : 1 ≤ m := by rw [hm]; omega + set G : List Key := db.keys.filter + (fun x : Key => decide (last < x ∧ (x : WithTop Key) < r.end_.key)) with hG + have hGlen : m ≤ G.length := hfill + have hGnodup : G.Nodup := by + rw [hG]; exact db.sorted.pairwise.nodup.filter _ + have hGmem : ∀ x ∈ G, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hG] at hx + exact of_decide_eq_true (List.mem_filter.mp hx).2 + set raw : List Key := (k :: G.erase k).take m with hraw + have hrawNodup : raw.Nodup := by + rw [hraw] + refine (List.take_sublist m (k :: G.erase k)).nodup ?_ + exact List.nodup_cons.mpr ⟨hGnodup.not_mem_erase, hGnodup.erase k⟩ + have hrawLen : raw.length = m := by + rw [hraw, List.length_take] + apply Nat.min_eq_left + by_cases hkG : k ∈ G + · rw [List.length_cons, List.length_erase_of_mem hkG]; omega + · rw [List.length_cons, List.erase_of_not_mem hkG]; omega + have hrawSub : ∀ x ∈ raw, x = k ∨ x ∈ G := by + intro x hx + rw [hraw] at hx + rcases List.mem_cons.mp (List.mem_of_mem_take hx) with rfl | hx' + · exact Or.inl rfl + · exact Or.inr (List.mem_of_mem_erase hx') + have hrawk : k ∈ raw := by + rw [hraw, List.mem_take_iff_getElem] + refine ⟨0, ?_, rfl⟩ + rw [List.length_cons] + omega + have hrawGt : ∀ x ∈ raw, last < x := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkgt + · exact (hGmem x hx').1 + have hrawLt : ∀ x ∈ raw, (x : WithTop Key) < r.end_.key := by + intro x hx + rcases hrawSub x hx with rfl | hx' + · exact hkE + · exact (hGmem x hx').2 + set F : List Key := raw.insertionSort (· ≤ ·) with hF + have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm + have hFsorted : F.SortedLT := by + rw [hF, List.sortedLT_iff_pairwise] + exact DB.pairwise_lt_of_le_of_nodup + (List.pairwise_insertionSort (r := (· ≤ ·)) raw) + (hFperm.nodup_iff.mp hrawNodup) + have hFlen : F.length = m := by rw [hF, List.length_insertionSort, hrawLen] + have hFk : k ∈ F := hFperm.mem_iff.mp hrawk + have hFmem : ∀ x ∈ F, last < x ∧ (x : WithTop Key) < r.end_.key := by + intro x hx + exact ⟨hrawGt x (hFperm.mem_iff.mpr hx), hrawLt x (hFperm.mem_iff.mpr hx)⟩ + have hFne : F ≠ [] := by + intro h; rw [h] at hFlen; simp at hFlen; omega + set D : List Key := res.keys ++ F with hD + have hDsort : D.SortedLT := by + rw [hD] + refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩ + intro a ha b hb + exact lt_of_le_of_lt (hle_last a ha) (hFmem b hb).1 + let dbw : DB := ⟨D, hDsort⟩ + have hdbw_keys : dbw.keys = D := rfl + -- the begin selector resolves to the reported head + have hhead_mem : head ∈ dbw.keys := by + rw [hdbw_keys, hD, hhead] + exact List.mem_append.mpr (Or.inl (List.head_mem hne)) + have hno : ∀ z ∈ dbw.keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + rw [hdbw_keys, hD] at hz + rcases List.mem_append.mp hz with hz | hz + · have hz0 : z ∈ db.keys := RangeRead.compatible_keys_subset hcomp hz + exact no_stored_between_head_begin_of_zero_crossed_more hcomp hrev hmore hne hbegin hcross' + z hz0 hzb + · have hzraw : z ∈ raw := hFperm.mem_iff.mpr hz + have hzsub : z ∈ G := by + rw [hraw] at hzraw + rcases List.mem_cons.mp (List.mem_of_mem_take hzraw) with hzk | hzG + · subst hzk + exact absurd hzb.2 (not_lt.mpr hkge) + · exact List.mem_of_mem_erase hzG + have hz0 : z ∈ db.keys := by + rw [hG] at hzsub + exact (List.mem_filter.mp hzsub).1 + exact no_stored_between_head_begin_of_zero_crossed_more hcomp hrev hmore hne hbegin hcross' + z hz0 ⟨lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last) + (WithTop.coe_lt_coe.mpr (hGmem z hzsub).1), hzb.2⟩ + have hrb : resolve dbw r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_mem hcross' hno + -- every stored key is below the end key + have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by + intro x hx + rw [hD] at hx + rcases List.mem_append.mp hx with hx | hx + · exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE + · exact (hFmem x hx).2 + have hlenD : D.length = res.keys.length + m := by + rw [hD, List.length_append, hFlen] + have hm_int : (m : Int) = 1 - r.end_.offset := by + rw [hm]; exact Int.toNat_of_nonneg (by omega) + have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = + D.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hDlt x hx + have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD] + push_cast + rw [hm_int] + ring + | coe e => + have hce : D.countP (fun x : Key => decide (x < e)) = D.length := by + rw [← countP_lt_coe (l := D) e, ← hE] + exact hcount_end + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD] + push_cast + rw [hm_int] + ring + have ht_lt : res.keys.length < D.length := by rw [hlenD]; omega + have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by + change D[res.keys.length]? = some (F.head hFne) + rw [hD, List.getElem?_append] + rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self] + rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne] + have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)] + rw [hanchor_end, Int.toNat_natCast, hDget?] + have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by + rw [hre] + exact WithTop.coe_lt_coe.mpr (hFmem (F.head hFne) (List.head_mem hFne)).1 + -- complete keys of the witness are the reported keys + have hckw : (RangeRead.completeKeys dbw r).filter + (fun x : Key => decide (x ≤ last)) = res.keys := by + rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF] + rw [hdbw_keys, hD, List.filter_append] + have hres_fil : res.keys.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + exact ⟨WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx), hle_last x hx⟩ + have hF_fil : F.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + exact fun _ => not_le.mpr (hFmem x hx).1 + rw [hres_fil, hF_fil, List.append_nil] + have hcompw : RangeRead.Compatible dbw r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + exact ⟨hcomp.1, hckw⟩ + have hkdbw : k ∈ dbw.keys := by + rw [hdbw_keys, hD] + exact List.mem_append.mpr (Or.inr hFk) + -- now the erased database + have hk_not_mem_res : k ∉ res.keys := by + intro hk + exact absurd (hle_last k hk) (not_le.mpr hkgt) + have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by + rw [DB.erase_keys, hdbw_keys, hD] + exact List.erase_append_right F hk_not_mem_res + have hhead_mem' : head ∈ (dbw.erase k).keys := by + rw [hD', hhead] + exact List.mem_append.mpr (Or.inl (List.head_mem hne)) + have hno' : ∀ z ∈ (dbw.erase k).keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧ + (z : WithTop Key) < r.begin.key) := by + intro z hz hzb + exact hno z (by + rw [DB.erase_keys, hdbw_keys] at hz + exact List.mem_of_mem_erase hz) hzb + have hrb' : resolve (dbw.erase k) r.begin = ((head : Key) : WithTop Key) := + resolve_begin_eq_of_no_between_zero hbegin hhead_mem' hcross' hno' + have hD'lt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by + intro x hx + exact hDlt x (by + rw [DB.erase_keys, hdbw_keys] at hx + exact List.mem_of_mem_erase hx) + have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by + rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen] + omega + have hcount_end' : (dbw.erase k).keys.countP + (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by + rw [List.countP_eq_length] + intro x hx + rw [decide_eq_true_eq] + exact hD'lt x hx + have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by + cases hE : r.end_.key with + | top => + rw [anchor_of_top (s := r.end_) hE, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + | coe e => + have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) = + (dbw.erase k).keys.length := by + rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE] + exact hcount_end' + rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD'] + rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)] + push_cast + rw [hm_int] + ring + have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne + have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by + rw [Nat.cast_sub ht1]; norm_num + have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by + rw [hanchor_end', hEqInt, Int.toNat_natCast] + have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by + rw [hD', List.getElem?_append] + rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)] + rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)] + congr 1 + rw [hlast, List.getLast_eq_getElem hne] + have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by + rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)] + rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?] + have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by + unfold RangeRead.completeKeys RangeRead.inRange + rw [ite_eq_right (by simp [hrev]), hrb', hre'] + have hpred : ∀ x ∈ (dbw.erase k).keys, + decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ + (x : WithTop Key) < ((↑last : Key) : WithTop Key)) = + decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ x < last) := by + intro x _ + rw [decide_eq_decide] + exact and_congr_right fun _ => WithTop.coe_lt_coe + rw [List.filter_congr hpred, hD', List.filter_append] + have hhead_le_all : ∀ x ∈ res.keys, ((head : Key) : WithTop Key) ≤ (x : WithTop Key) := by + intro x hx + exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx) + rw [show res.keys.filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) = + res.keys.filter (fun x : Key => decide (x < last)) from by + apply List.filter_congr + intro x hx + rw [decide_eq_decide] + exact ⟨fun h => h.2, fun h => ⟨hhead_le_all x hx, h⟩⟩] + conv_lhs => rw [← List.dropLast_append_getLast hne, hlast] + rw [List.filter_append] + have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + have h := hsort.pairwise.rel_dropLast_getLast hx + simpa [hlast] using h + rw [hfil] + have hFnil : (F.erase k).filter (fun x : Key => decide + (((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) = [] := by + rw [List.filter_eq_nil_iff] + intro x hx + simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and] + intro _ + exact not_lt.mpr (le_of_lt (hFmem x (List.mem_of_mem_erase hx)).1) + rw [hFnil, List.append_nil] + simp + have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by + rw [compatible_iff_truncated_keys hrev hmore hne] + intro h + have heq := h.2 + rw [hckE] at heq + have hfil : res.keys.dropLast.filter + (fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by + rw [List.filter_eq_self] + intro x hx + rw [decide_eq_true_eq] + rw [← hlast] + exact hle_last x (List.mem_of_mem_dropLast hx) + rw [hfil] at heq + have hlen := congrArg List.length heq + rw [List.length_dropLast] at hlen + omega + exact ⟨dbw, hcompw, hkdbw, hnot⟩ + +/-- A key at or above `begin.key` and below `end.key` is necessary in a crossed +truncated begin-zero read with a nonpositive end offset. -/ +theorem necessary_zeroCross_trunc_above_begin_nonposEnd {r : RangeRead} {res : Result} {k : Key} + (hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true) + (hne : res.keys ≠ []) (hbegin : r.begin.offset = 0) + (hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) + (hend : r.end_.offset ≤ 0) + (hkgt : res.keys.getLast hne < k) + (hkE : (k : WithTop Key) < r.end_.key) + (hkge : r.begin.key ≤ (k : WithTop Key)) : + Necessary r res k := by + obtain ⟨db, hcomp⟩ := hread + obtain ⟨dbw, hcompw, hkdbw, hnot⟩ := + exists_witness_zeroCross_trunc_nonposEnd ⟨db, hcomp⟩ hrev hmore hne hbegin hcross hend + hkgt hkE hkge + exact necessary_of_toggle ⟨dbw, hcompw, by + rw [toggle_eq_erase_of_mem hkdbw] + exact hnot⟩ + +/-- **The crossed truncated begin-zero closed form is minimal** when `end.offset = 0`. -/ +theorem minimal_closedForm_of_zeroCross_trunc_zeroEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncZeroEnd r res) : + Minimal r res (closedFormZeroCrossTruncNonposEnd r res) := by + obtain ⟨hbegin, hrev, hmore, hcrossD, hend⟩ := h + have hread' : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD + exact hcrossD + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hcr : closedFormZeroCrossTruncNonposEnd r res = + ⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by + unfold closedFormZeroCrossTruncNonposEnd + rw [hheadD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross + (le_of_eq hend) hc1 hagree + · intro hc2 + exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross + (le_of_eq hend) hc2 (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle + · have hkgt : last < k := lt_of_not_ge hkle + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_zeroCross_trunc_gap hread' hrev hmore hne hbegin hcross hkgt hkB + · have hkge : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hkB + by_cases hleE : r.begin.key ≤ r.end_.key + · have hmax : max r.begin.key r.end_.key = r.end_.key := max_eq_right hleE + have hkE : (k : WithTop Key) < r.end_.key := by rw [← hmax]; exact hkhi + exact necessary_zeroCross_trunc_above_begin hread' hrev hmore hne hbegin hcross hend + (by rw [← hlast]; exact hkgt) hkE hkge + · exfalso + have hltE : r.end_.key < r.begin.key := lt_of_not_ge hleE + have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hltE) + rw [hmax] at hkhi + exact absurd hkhi (not_lt.mpr hkge) + +/-- **The crossed truncated begin-zero closed form is minimal** for every nonpositive +end offset. -/ +theorem minimal_closedForm_of_zeroCross_trunc_nonposEnd {r : RangeRead} {res : Result} + (hread : Readable r res) (h : zeroCrossTruncNonposEnd r res) : + Minimal r res (closedFormZeroCrossTruncNonposEnd r res) := by + obtain ⟨hbegin, hrev, hmore, hcrossD, hend⟩ := h + have hread' : Readable r res := hread + obtain ⟨db₀, hcomp₀⟩ := hread + have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore + have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by + have hleast : leastReported r res = res.keys.headD ([] : Key) := by + simp [leastReported, hrev] + rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD + exact hcrossD + set head : Key := res.keys.head hne with hhead + set last : Key := res.keys.getLast hne with hlast + have hheadD : res.keys.headD ([] : Key) = head := by + rw [hhead, headD_eq_head_zero_cross_trunc hne] + have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by + rw [hhead]; exact hcross + have hcr : closedFormZeroCrossTruncNonposEnd r res = + ⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by + unfold closedFormZeroCrossTruncNonposEnd + rw [hheadD] + refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩ + · intro db1 db2 hagree + constructor + · intro hc1 + exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross + hend hc1 hagree + · intro hc2 + exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross + hend hc2 (AgreeOn.symm hagree) + · intro k hklo hkhi + rw [hcr] at hklo hkhi + have hhead_nec : Necessary r res head := + necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne) + have hlast_nec : Necessary r res last := + necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne) + have hhead_le_last : head ≤ last := by + have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt + simp only [hhead, hlast] + exact hsort.rel_getLast (List.head_mem hne) + by_cases hkle : k ≤ last + · exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle + · have hkgt : last < k := lt_of_not_ge hkle + by_cases hkB : (k : WithTop Key) < r.begin.key + · exact necessary_zeroCross_trunc_gap hread' hrev hmore hne hbegin hcross hkgt hkB + · have hkge : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hkB + by_cases hleE : r.begin.key ≤ r.end_.key + · have hmax : max r.begin.key r.end_.key = r.end_.key := max_eq_right hleE + have hkE : (k : WithTop Key) < r.end_.key := by rw [← hmax]; exact hkhi + exact necessary_zeroCross_trunc_above_begin_nonposEnd hread' hrev hmore hne hbegin + hcross hend (by rw [← hlast]; exact hkgt) hkE hkge + · exfalso + have hltE : r.end_.key < r.begin.key := lt_of_not_ge hleE + have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hltE) + rw [hmax] at hkhi + exact absurd hkhi (not_lt.mpr hkge) + +end FormalConflictRange diff --git a/Proof.lean b/Proof.lean new file mode 100644 index 0000000..7a92be3 --- /dev/null +++ b/Proof.lean @@ -0,0 +1,123 @@ +import Bridge + +/-! +# Transporting the reference theorem to this repository's statement + +`Bridge` proves, for each definition of `Statement`, that its reference +counterpart agrees with it under the translation `toRef*`. This file uses those +bridge lemmas to transport the reference library's minimality theorem for the +closed form to `Statement.ClosedFormMinimal`. +-/ + +namespace Statement +open FormalConflictRange + +/-! ## Compatibility and readability -/ + +theorem compatible_iff (db : DB) (r : RangeRead) (res : Result) : + FormalConflictRange.RangeRead.Compatible (toRefDB db) (toRefRead r) (toRefRes res) ↔ + RangeRead.Compatible db r res := by + unfold FormalConflictRange.RangeRead.Compatible RangeRead.Compatible + simp [FormalConflictRange.RangeRead.withinLimit, FormalConflictRange.RangeRead.completeKeys, + RangeRead.completeKeys, FormalConflictRange.RangeRead.inRange, RangeRead.inRange] + +theorem readable_iff (r : RangeRead) (res : Result) : + FormalConflictRange.Readable (toRefRead r) (toRefRes res) ↔ Readable r res := by + unfold FormalConflictRange.Readable Readable + constructor + · rintro ⟨db, hdb⟩ + exact ⟨ofRefDB db, (compatible_iff (ofRefDB db) r res).mp (by simpa using hdb)⟩ + · rintro ⟨db, hdb⟩ + exact ⟨toRefDB db, (compatible_iff db r res).mpr hdb⟩ + +/-! ## Conflict-range keys, agreement and subranges -/ + +theorem keys_eq (cr : ConflictRange) (db : DB) : + FormalConflictRange.ConflictRange.keys (toRefCR cr) (toRefDB db) = + ConflictRange.keys cr db := by + unfold FormalConflictRange.ConflictRange.keys ConflictRange.keys + unfold RangeRead.inRange + rfl + +theorem agreeOn_iff (cr : ConflictRange) (db₁ db₂ : DB) : + FormalConflictRange.AgreeOn (toRefCR cr) (toRefDB db₁) (toRefDB db₂) ↔ + AgreeOn cr db₁ db₂ := by + unfold FormalConflictRange.AgreeOn AgreeOn + rw [keys_eq, keys_eq] + +theorem subrange_iff (cr' cr : ConflictRange) : + FormalConflictRange.Subrange (toRefCR cr') (toRefCR cr) ↔ Subrange cr' cr := by + unfold FormalConflictRange.Subrange Subrange + rfl + +theorem properSubrange_iff (cr' cr : ConflictRange) : + FormalConflictRange.ProperSubrange (toRefCR cr') (toRefCR cr) ↔ + ProperSubrange cr' cr := by + unfold FormalConflictRange.ProperSubrange ProperSubrange + rw [subrange_iff, subrange_iff] + +/-! ## Soundness and minimality -/ + +theorem sound_iff (r : RangeRead) (res : Result) (cr : ConflictRange) : + FormalConflictRange.Sound (toRefRead r) (toRefRes res) (toRefCR cr) ↔ + Sound r res cr := by + unfold FormalConflictRange.Sound Sound + constructor + · intro h db₁ db₂ hagree + have h' := h (toRefDB db₁) (toRefDB db₂) ((agreeOn_iff cr db₁ db₂).mpr hagree) + exact ((compatible_iff db₁ r res).symm.trans (h'.trans (compatible_iff db₂ r res))) + · intro h db₁ db₂ hagree + have h' := h (ofRefDB db₁) (ofRefDB db₂) + ((agreeOn_iff cr (ofRefDB db₁) (ofRefDB db₂)).mp (by simpa using hagree)) + have hc₁ : FormalConflictRange.RangeRead.Compatible db₁ (toRefRead r) (toRefRes res) ↔ + RangeRead.Compatible (ofRefDB db₁) r res := by + simpa using compatible_iff (ofRefDB db₁) r res + have hc₂ : FormalConflictRange.RangeRead.Compatible db₂ (toRefRead r) (toRefRes res) ↔ + RangeRead.Compatible (ofRefDB db₂) r res := by + simpa using compatible_iff (ofRefDB db₂) r res + exact hc₁.trans (h'.trans hc₂.symm) + +theorem minimal_iff (r : RangeRead) (res : Result) (cr : ConflictRange) : + FormalConflictRange.Minimal (toRefRead r) (toRefRes res) (toRefCR cr) ↔ + Minimal r res cr := by + unfold FormalConflictRange.Minimal Minimal + constructor + · rintro ⟨hs, hmin⟩ + refine ⟨(sound_iff r res cr).mp hs, ?_⟩ + intro cr' hproper hs' + exact hmin (toRefCR cr') ((properSubrange_iff cr' cr).mpr hproper) + ((sound_iff r res cr').mpr hs') + · rintro ⟨hs, hmin⟩ + refine ⟨(sound_iff r res cr).mpr hs, ?_⟩ + intro cr' hproper hs' + have hproper' : ProperSubrange (ofRefCR cr') cr := + (properSubrange_iff (ofRefCR cr') cr).mp (by simpa using hproper) + have hs'' : Sound r res (ofRefCR cr') := + (sound_iff r res (ofRefCR cr')).mp (by simpa using hs') + exact hmin (ofRefCR cr') hproper' hs'' + +/-! ## The closed form -/ + +theorem closedForm_eq (r : RangeRead) (res : Result) : + FormalConflictRange.conflictRangeClosedFormClosed (toRefRead r) (toRefRes res) = + toRefCR (closedForm r res) := by + by_cases hne : res.keys = [] + · unfold FormalConflictRange.conflictRangeClosedFormClosed closedForm + simp [hne] + · unfold FormalConflictRange.conflictRangeClosedFormClosed closedForm + simp [hne] + +/-! ## The headline -/ + +/-- **The closed form is minimal.** This is the reference theorem +`minimal_conflictRangeClosedFormClosed`, transported across the definitional +bridge. -/ +theorem closedFormMinimal : ClosedFormMinimal := by + intro r res hread + have hread' : FormalConflictRange.Readable (toRefRead r) (toRefRes res) := + (readable_iff r res).mpr hread + have hmin := FormalConflictRange.minimal_conflictRangeClosedFormClosed hread' + rw [closedForm_eq r res] at hmin + exact (minimal_iff r res (closedForm r res)).mp hmin + +end Statement diff --git a/lake-manifest.json b/lake-manifest.json index 4dcfc1b..9866e24 100644 --- a/lake-manifest.json +++ b/lake-manifest.json @@ -91,6 +91,6 @@ "inputRev": "v4.34.0", "inherited": true, "configFile": "lakefile.toml"}], - "name": "«formal-conflict-range»", + "name": "«conflict-range»", "lakeDir": ".lake", "fixedToolchain": false} diff --git a/lakefile.toml b/lakefile.toml index 4df62d1..4115290 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -1,6 +1,11 @@ name = "conflict-range" version = "0.1.0" -defaultTargets = ["Statement"] +defaultTargets = ["Statement", "Proof"] + +[leanOptions] +relaxedAutoImplicit = false +weak.linter.mathlibStandardSet = true +maxSynthPendingDepth = 3 [[require]] name = "mathlib" @@ -9,3 +14,11 @@ rev = "v4.34.0" [[lean_lib]] name = "Statement" + +[[lean_lib]] +name = "FormalConflictRange" +globs = ["FormalConflictRange.*"] + +[[lean_lib]] +name = "Proof" +globs = ["Proof", "Bridge"]