1116 lines
57 KiB
Lean4
1116 lines
57 KiB
Lean4
/-
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
-/
|
||
|
||
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
|
||
|
||
|
||
/-! ### 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, 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, 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 [ite_eq_left 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
|