1495 lines
73 KiB
Lean4
1495 lines
73 KiB
Lean4
/-
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
-/
|
||
|
||
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
|
||
|
||
|
||
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 []
|
||
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_natCast, ENat.toNat_natCast] 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, ite_eq_right (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 [ite_eq_left 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 [ite_eq_right 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 [ite_eq_left hc']; simp [hc']
|
||
· rw [ite_eq_right 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 [ite_eq_left hc']; simp
|
||
· rw [ite_eq_right 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.natCast_le_natCast.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 [ hc']
|
||
· rw [ite_eq_right 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 [ite_eq_left hc']
|
||
rw [residualD, ite_eq_left hc'] at htop'
|
||
omega
|
||
· rw [ite_eq_right hc']
|
||
obtain ⟨n, hn⟩ := ENat.ne_top_iff_exists.mp hc'
|
||
have hD : residualD r c = b - n := by
|
||
rw [residualD, ite_eq_right 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
|