- Vendor the FormalConflictRange reference library needed by the proof - Add Bridge.lean with simp lemmas connecting the Statement definitions to the reference library - Prove Statement.closedFormMinimal in Proof.lean - Register the new libraries in the Lake config
1479 lines
70 KiB
Lean4
1479 lines
70 KiB
Lean4
/-
|
||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
Authors: FormalConflictRange contributors
|
||
-/
|
||
|
||
import FormalConflictRange.CrossedTruncated
|
||
|
||
/-!
|
||
# Crossed reversed truncated reads at begin offset one
|
||
|
||
This module closes the last cell of the offset-one closed form: a truncated result
|
||
whose traversal is reversed and whose selector keys are crossed, `end_.key <
|
||
begin_.key`.
|
||
|
||
Because the traversal is reversed, the reported keys are the largest keys of the
|
||
complete window. Writing `E = end_.key`, `B = begin_.key`, `e = (end_.offset -
|
||
1).toNat`, and `t = res.keys.length`, all reported keys lie at or above `B > E`, so
|
||
`t` is also the number of reported keys at or above `E`. The complete window,
|
||
relative to the stored keys at or above `E`, is the block obtained by dropping the
|
||
gap `[E, B)` and taking the first `e` keys.
|
||
|
||
A compatible result is a prefix of the reversed window. Compatibility forces the
|
||
gap to be empty when `t = e`; then the window is exactly the reported keys and the
|
||
minimal range is `[E, keyAfter (head reported))`. When `t < e` the resolved end
|
||
outruns the reported block, and the minimal range is the unbounded `[E, ⊤)`.
|
||
-/
|
||
|
||
open FormalConflictRange
|
||
|
||
namespace FormalConflictRange
|
||
|
||
theorem completeKeys_rev_eq_reverse (db : DB) (r : RangeRead) (hrev : r.reverse = true) :
|
||
RangeRead.completeKeys db r =
|
||
(RangeRead.completeKeys db { r with reverse := false }).reverse := by
|
||
simp [RangeRead.completeKeys, hrev]
|
||
|
||
/-- A compatible crossed reversed result has all reported keys at or above `end_.key`. -/
|
||
theorem crossed_rev_res_ge_end {db : DB} {r : RangeRead} {res : Result}
|
||
(hc : RangeRead.Compatible db r res)
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) :
|
||
∀ k ∈ res.keys, r.end_.key ≤ (k : WithTop Key) := by
|
||
intro k hk
|
||
have hmem := (RangeRead.compatible_keys_prefix hc).subset hk
|
||
rw [completeKeys_rev_eq_reverse db r hrev] at hmem
|
||
have hmem' := List.mem_reverse.mp hmem
|
||
rw [completeKeys_eq_filter_take_of_offset_one (db := db)
|
||
(r := { r with reverse := false }) (by simp) hbegin hend] at hmem'
|
||
have hfil := List.mem_of_mem_take hmem'
|
||
have hge := (List.mem_filter.mp hfil).2
|
||
rw [decide_eq_true_eq] at hge
|
||
exact le_trans (le_of_lt hcross) hge
|
||
|
||
theorem crossed_rev_count_ge_eq_length {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hread : Readable r res) :
|
||
res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) =
|
||
res.keys.length := by
|
||
obtain ⟨db, hc⟩ := hread
|
||
rw [List.countP_eq_length]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
exact crossed_rev_res_ge_end hc hrev hbegin hcross hend k hk
|
||
|
||
/-- The keys at or above `B` are the keys at or above `E` with the gap `[E, B)` dropped. -/
|
||
theorem above_eq_drop_between {db : DB} {E B : WithTop Key} (hEB : E ≤ B) :
|
||
above db B = (above db E).drop (between db E B).length := by
|
||
rw [above_B_eq_filter hEB, between_eq_filter_above]
|
||
have hs : (above db E).SortedLT :=
|
||
List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
have hL : (above db E).filter (fun k : Key => decide (B ≤ (k : WithTop Key))) =
|
||
(above db E).filter (fun k : Key => decide (¬ ((k : WithTop Key) < B))) := by
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact not_lt.symm
|
||
rw [hL, filter_not_lt_eq_drop_countP hs B, List.countP_eq_length_filter]
|
||
|
||
private theorem take_sub_drop {α : Type*} (l : List α) (e g : Nat) :
|
||
(l.drop g).take (min e l.length - g) = (l.take e).drop g := by
|
||
rw [List.drop_take, List.take_eq_take_iff]
|
||
simp only [List.length_drop]
|
||
omega
|
||
|
||
/-- The complete keys of a crossed reversed read are the reversed block of the
|
||
keys at or above `end_.key` from which the gap is dropped. -/
|
||
theorem completeKeys_crossed_rev_eq {db : DB} {r : RangeRead}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) :
|
||
RangeRead.completeKeys db r =
|
||
(((above db r.end_.key).take ((r.end_.offset - 1).toNat)).drop
|
||
(between db r.end_.key r.begin.key).length).reverse := by
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set B : WithTop Key := r.begin.key with hB
|
||
set e : Nat := (r.end_.offset - 1).toNat with he
|
||
have hEB : E ≤ B := le_of_lt hcross
|
||
have hfwd : RangeRead.completeKeys db r =
|
||
((above db B).take (min (cnt db E + e) db.keys.length - cnt db B)).reverse := by
|
||
rw [completeKeys_rev_eq_reverse db r hrev]
|
||
rw [completeKeys_eq_filter_take_of_offset_one (db := db)
|
||
(r := { r with reverse := false }) (by simp) hbegin hend]
|
||
have hfilB : db.keys.filter (fun k : Key =>
|
||
decide (({ r with reverse := false } : RangeRead).begin.key ≤
|
||
(k : WithTop Key))) = above db B := rfl
|
||
rw [hfilB]
|
||
rfl
|
||
have hcalc : min (cnt db E + e) db.keys.length - cnt db B =
|
||
min e (above db E).length - (between db E B).length := by
|
||
rw [← cnt_add_above_length db E, cnt_eq_add_between db hEB,
|
||
Nat.add_min_add_left, Nat.add_sub_add_left]
|
||
rw [hfwd, hcalc, above_eq_drop_between hEB, take_sub_drop]
|
||
|
||
/-- Agreement on the keys at or above `end_.key` fixes the complete keys of a
|
||
crossed reversed read. -/
|
||
theorem completeKeys_eq_of_agree_above_crossed_rev {db₁ db₂ : DB} {r : RangeRead}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(habove : above db₁ r.end_.key = above db₂ r.end_.key) :
|
||
RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set B : WithTop Key := r.begin.key with hB
|
||
set e : Nat := (r.end_.offset - 1).toNat with he
|
||
have hEB : E ≤ B := le_of_lt hcross
|
||
have hfwd (db : DB) :
|
||
RangeRead.completeKeys db r =
|
||
((above db B).take
|
||
(min (cnt db E + e) db.keys.length - cnt db B)).reverse := by
|
||
rw [completeKeys_rev_eq_reverse db r hrev]
|
||
rw [completeKeys_eq_filter_take_of_offset_one (db := db)
|
||
(r := { r with reverse := false }) (by simp) hbegin hend]
|
||
have hfilB : db.keys.filter (fun k : Key =>
|
||
decide (({ r with reverse := false } : RangeRead).begin.key ≤
|
||
(k : WithTop Key))) = above db B := rfl
|
||
rw [hfilB]
|
||
rfl
|
||
have hU : above db₁ E = above db₂ E := habove
|
||
have hB_eq : above db₁ B = above db₂ B := by
|
||
rw [above_B_eq_filter (db := db₁) hEB, above_B_eq_filter (db := db₂) hEB, hU]
|
||
have hg_eq : (between db₁ E B).length = (between db₂ E B).length := by
|
||
rw [between_eq_filter_above (db := db₁) E B,
|
||
between_eq_filter_above (db := db₂) E B, hU]
|
||
have hUlen : (above db₁ E).length = (above db₂ E).length := by rw [hU]
|
||
have hcalc (db : DB) :
|
||
min (cnt db E + e) db.keys.length - cnt db B =
|
||
min e (above db E).length - (between db E B).length := by
|
||
rw [← cnt_add_above_length db E, cnt_eq_add_between db hEB,
|
||
Nat.add_min_add_left, Nat.add_sub_add_left]
|
||
have hlen_eq :
|
||
min (cnt db₁ E + e) db₁.keys.length - cnt db₁ B =
|
||
min (cnt db₂ E + e) db₂.keys.length - cnt db₂ B := by
|
||
rw [hcalc db₁, hcalc db₂, hUlen, hg_eq]
|
||
rw [hfwd db₁, hfwd db₂, hB_eq, hlen_eq]
|
||
|
||
/-- The stored keys of `[lo, keyAfter last)` are the keys at or above `lo` that are
|
||
at most `last`. -/
|
||
private theorem keys_lo_keyAfter_eq_filter_le {db : DB} {lo : WithTop Key} {last : Key} :
|
||
(⟨lo, (keyAfter last : Key)⟩ : ConflictRange).keys db =
|
||
(db.keys.filter (fun k : Key => decide (lo ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last)) := by
|
||
unfold ConflictRange.keys
|
||
rw [show db.keys.filter (fun (k : Key) => decide (lo ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < ((keyAfter last : Key) : WithTop Key))) =
|
||
db.keys.filter (fun (k : Key) =>
|
||
decide ((k : WithTop Key) < ((keyAfter last : Key) : WithTop Key)) &&
|
||
decide (lo ≤ (k : WithTop Key))) from
|
||
List.filter_congr fun k _ => by rw [Bool.decide_and, Bool.and_comm]]
|
||
rw [← List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide, WithTop.coe_lt_coe, lt_keyAfter_iff]
|
||
|
||
/-- In the finite branch the candidate range contains exactly the reported keys,
|
||
reversed. -/
|
||
theorem crossed_rev_finite_keys_eq {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
(ht : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
(⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ : ConflictRange).keys db =
|
||
res.keys.reverse := by
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set B : WithTop Key := r.begin.key with hB
|
||
set e : Nat := (r.end_.offset - 1).toNat with he
|
||
set lE : List Key := above db E with hlE
|
||
set g : Nat := (between db E B).length with hg
|
||
have hEB : E ≤ B := le_of_lt hcross
|
||
have hformula : RangeRead.completeKeys db r = ((lE.take e).drop g).reverse := by
|
||
simpa [E, B, e, lE, g] using
|
||
completeKeys_crossed_rev_eq (db := db) (r := r) hrev hbegin hcross hend
|
||
have hpre : res.keys <+: RangeRead.completeKeys db r :=
|
||
RangeRead.compatible_keys_prefix hcomp
|
||
have hlen_ck : (RangeRead.completeKeys db r).length = min e lE.length - g := by
|
||
rw [hformula, List.length_reverse, List.length_drop, List.length_take]
|
||
have hepos : 0 < e := by
|
||
have h := List.length_pos_of_ne_nil hne
|
||
omega
|
||
have htle : e ≤ min e lE.length - g := by
|
||
have h := hpre.length_le
|
||
rw [hlen_ck, ht] at h
|
||
exact h
|
||
have hg0 : g = 0 := by omega
|
||
have hmin : min e lE.length = e := by omega
|
||
have hckeq : RangeRead.completeKeys db r = (lE.take e).reverse := by
|
||
rw [hformula, hg0, List.drop_zero]
|
||
have hlenck_e : (RangeRead.completeKeys db r).length = e := by
|
||
rw [hckeq, List.length_reverse, List.length_take, hmin]
|
||
have hreseq : res.keys = (lE.take e).reverse := by
|
||
have h := hpre.eq_of_length (by rw [ht, hlenck_e])
|
||
rw [h] at *
|
||
exact hckeq
|
||
have htake_ne : lE.take e ≠ [] := by
|
||
intro hnil
|
||
rw [hnil, List.reverse_nil] at hreseq
|
||
exact hne hreseq
|
||
have hH : res.keys.head hne = (lE.take e).getLast htake_ne := by
|
||
apply (List.head_eq_iff_head?_eq_some hne).mpr
|
||
rw [hreseq, List.head?_reverse]
|
||
exact List.getLast?_eq_getLast_of_ne_nil htake_ne
|
||
have hsE : lE.SortedLT := by
|
||
rw [hlE]
|
||
exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
have htake_sorted : (lE.take e).SortedLT :=
|
||
List.sortedLT_iff_pairwise.mpr
|
||
(List.Pairwise.take (i := e) (List.sortedLT_iff_pairwise.mp hsE))
|
||
have hle_take : ∀ x ∈ lE.take e, x ≤ (lE.take e).getLast htake_ne :=
|
||
fun x hx => (htake_sorted.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hsplit : lE = lE.take e ++ lE.drop e := (List.take_append_drop e lE).symm
|
||
have hcross_lt : ∀ x ∈ lE.take e, ∀ y ∈ lE.drop e, x < y := by
|
||
have hs' : (lE.take e ++ lE.drop e).SortedLT := by rw [← hsplit]; exact hsE
|
||
exact (List.sortedLT_append.mp hs').2.2
|
||
have hfil_take : (lE.take e).filter (fun k : Key => decide (k ≤ res.keys.head hne)) =
|
||
lE.take e := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [hH]
|
||
exact decide_eq_true (hle_take x hx)
|
||
have hfil_drop : (lE.drop e).filter (fun k : Key => decide (k ≤ res.keys.head hne)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [hH, decide_eq_true_eq]
|
||
exact not_le.mpr (hcross_lt _ (List.getLast_mem htake_ne) x hx)
|
||
rw [keys_lo_keyAfter_eq_filter_le]
|
||
change lE.filter (fun k : Key => decide (k ≤ res.keys.head hne)) = res.keys.reverse
|
||
conv_lhs => rw [hsplit]
|
||
rw [List.filter_append, hfil_take, hfil_drop, List.append_nil]
|
||
rw [hreseq, List.reverse_reverse]
|
||
|
||
/-- A compatible crossed reversed result has all reported keys at or above `begin.key`. -/
|
||
theorem crossed_rev_res_ge_begin {db : DB} {r : RangeRead} {res : Result}
|
||
(hc : RangeRead.Compatible db r res) (hbegin : r.begin.offset = 1) :
|
||
∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key) := by
|
||
intro k hk
|
||
have hmem := (RangeRead.compatible_keys_prefix hc).subset hk
|
||
exact le_trans (key_le_resolve_of_one_le (db := db) (s := r.begin) (by omega))
|
||
(RangeRead.mem_completeKeys.mp hmem).2.1
|
||
|
||
/-- If the range `[E, keyAfter H)` contains exactly the reported keys reversed, then
|
||
the first `e` keys at or above `E` are those reported keys and the gap `[E, B)` is
|
||
empty. -/
|
||
theorem take_above_eq_of_range_keys_eq {db : DB} {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
(ht : res.keys.length = (r.end_.offset - 1).toNat)
|
||
(hBR : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key))
|
||
(hR : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ :
|
||
ConflictRange).keys db = res.keys.reverse) :
|
||
(above db r.end_.key).take ((r.end_.offset - 1).toNat) = res.keys.reverse ∧
|
||
between db r.end_.key r.begin.key = [] := by
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set B : WithTop Key := r.begin.key with hB
|
||
set e : Nat := (r.end_.offset - 1).toNat with he
|
||
set H : Key := res.keys.head hne with hHdef
|
||
set lE : List Key := above db E with hlE
|
||
have hEB : E ≤ B := le_of_lt hcross
|
||
have hsE : lE.SortedLT := by
|
||
rw [hlE]
|
||
exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
have hRfil : lE.filter (fun k : Key => decide (k ≤ H)) = res.keys.reverse := by
|
||
have h := hR
|
||
rw [keys_lo_keyAfter_eq_filter_le] at h
|
||
simpa only [lE, above] using h
|
||
have htake_c : lE.take (lE.countP (fun k : Key => decide (k ≤ H))) = res.keys.reverse := by
|
||
rw [← filter_le_eq_take_countP hsE H, hRfil]
|
||
have hc_le : lE.countP (fun k : Key => decide (k ≤ H)) ≤ lE.length :=
|
||
List.countP_le_length
|
||
have hc_eq : lE.countP (fun k : Key => decide (k ≤ H)) = e := by
|
||
have hlen := congrArg List.length htake_c
|
||
rw [List.length_take, List.length_reverse, ht] at hlen
|
||
omega
|
||
have htakeE : lE.take e = res.keys.reverse := by
|
||
rw [← hc_eq]
|
||
exact htake_c
|
||
have hbnil : between db E B = [] := by
|
||
rw [between_eq_filter_above]
|
||
change lE.filter (fun k : Key => decide ((k : WithTop Key) < B)) = []
|
||
have hsplit : lE = res.keys.reverse ++ lE.drop e := by
|
||
rw [← htakeE]
|
||
exact (List.take_append_drop e lE).symm
|
||
rw [hsplit]
|
||
have hRnil : res.keys.reverse.filter
|
||
(fun k : Key => decide ((k : WithTop Key) < B)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hBR x (List.mem_reverse.mp hx))
|
||
have htailnil : (lE.drop e).filter
|
||
(fun k : Key => decide ((k : WithTop Key) < B)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro y hy
|
||
rw [decide_eq_true_eq]
|
||
have hs' : (res.keys.reverse ++ lE.drop e).SortedLT := by
|
||
rw [← hsplit]; exact hsE
|
||
have hcross := (List.sortedLT_append.mp hs').2.2
|
||
have hrevne : res.keys.reverse ≠ [] := by
|
||
rwa [List.reverse_ne_nil_iff]
|
||
obtain ⟨x, hx⟩ := List.exists_mem_of_ne_nil _ hrevne
|
||
exact not_lt.mpr (le_of_lt (lt_of_le_of_lt (hBR x (List.mem_reverse.mp hx))
|
||
(WithTop.coe_lt_coe.mpr (hcross x hx y hy))))
|
||
rw [List.filter_append, hRnil, htailnil]
|
||
rfl
|
||
refine ⟨by simpa [E, e, lE] using htakeE, by simpa [E, B] using hbnil⟩
|
||
|
||
/-- If the finite candidate range contains exactly the reported keys reversed, then the
|
||
complete keys are exactly the reported keys. -/
|
||
theorem completeKeys_eq_of_range_keys_eq_crossed_rev {db : DB} {r : RangeRead}
|
||
{res : Result}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
(ht : res.keys.length = (r.end_.offset - 1).toNat)
|
||
(hBR : ∀ k ∈ res.keys, r.begin.key ≤ (k : WithTop Key))
|
||
(hR : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ :
|
||
ConflictRange).keys db = res.keys.reverse) :
|
||
RangeRead.completeKeys db r = res.keys := by
|
||
obtain ⟨htake, hbnil⟩ := take_above_eq_of_range_keys_eq hrev hbegin hcross hend hne ht hBR hR
|
||
rw [completeKeys_crossed_rev_eq (db := db) (r := r) hrev hbegin hcross hend]
|
||
rw [htake, hbnil]
|
||
simp
|
||
|
||
/-- Soundness of the finite crossed reversed branch. -/
|
||
theorem sound_truncatedRevCrossed_finite {r : RangeRead} {res : Result}
|
||
(hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
(ht : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
Sound r res ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ := by
|
||
have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
rw [hhead]
|
||
intro db₁ db₂ hagree
|
||
have hBR₁ := crossed_rev_res_ge_begin (db := db₁) (r := r) (res := res)
|
||
have hBR₂ := crossed_rev_res_ge_begin (db := db₂) (r := r) (res := res)
|
||
constructor
|
||
· intro hc₁
|
||
have hBR := hBR₁ hc₁ hbegin
|
||
have hT₁ := crossed_rev_finite_keys_eq (db := db₁) hc₁ hmore hrev hbegin hcross
|
||
hend hne ht
|
||
have hT₂ : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ :
|
||
ConflictRange).keys db₂ = res.keys.reverse := by
|
||
rw [← hagree]; exact hT₁
|
||
have hck := completeKeys_eq_of_range_keys_eq_crossed_rev hrev hbegin hcross hend
|
||
hne ht hBR hT₂
|
||
exact ⟨hc₁.1, Or.inl ⟨hmore, by rw [hck], hne⟩⟩
|
||
· intro hc₂
|
||
have hBR := hBR₂ hc₂ hbegin
|
||
have hT₂ := crossed_rev_finite_keys_eq (db := db₂) hc₂ hmore hrev hbegin hcross
|
||
hend hne ht
|
||
have hT₁ : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ :
|
||
ConflictRange).keys db₁ = res.keys.reverse := by
|
||
rw [hagree]; exact hT₂
|
||
have hck := completeKeys_eq_of_range_keys_eq_crossed_rev hrev hbegin hcross hend
|
||
hne ht hBR hT₁
|
||
exact ⟨hc₂.1, Or.inl ⟨hmore, by rw [hck], hne⟩⟩
|
||
|
||
/-- Soundness of the top crossed reversed branch: agreement on `[end_.key, ⊤)` fixes
|
||
the complete keys. -/
|
||
theorem sound_truncatedRevCrossed_top {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) :
|
||
Sound r res ⟨r.end_.key, ⊤⟩ := by
|
||
intro db₁ db₂ hagree
|
||
have habove : above db₁ r.end_.key = above db₂ r.end_.key := by
|
||
have h := hagree
|
||
rw [AgreeOn] at h
|
||
simpa [ConflictRange.keys, above] using h
|
||
have hck := completeKeys_eq_of_agree_above_crossed_rev (db₁ := db₁) (db₂ := db₂)
|
||
hrev hbegin hcross hend habove
|
||
constructor
|
||
· intro hc
|
||
simpa [RangeRead.Compatible, hck] using hc
|
||
· intro hc
|
||
simpa [RangeRead.Compatible, hck] using hc
|
||
|
||
|
||
|
||
/-- In the finite reversed-truncated branch, inserting an unstored key `k` with
|
||
`end_.key ≤ k < last reported` makes the end selector resolve onto the reported head.
|
||
The head then leaves the complete keys, so the truncated result can no longer be
|
||
compatible. -/
|
||
theorem crossed_rev_insert_between_incompatible {db : DB} {r : RangeRead} {res : Result}
|
||
{k : Key}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
(ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) =
|
||
(r.end_.offset - 1).toNat)
|
||
(hk0 : k ∉ db.keys)
|
||
(hEk : r.end_.key ≤ (k : WithTop Key))
|
||
(hkL : (k : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) :
|
||
¬ RangeRead.Compatible (db.insert k hk0) r res := by
|
||
have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||
have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
let L : Key := res.keys.getLast hne
|
||
let H : Key := res.keys.head hne
|
||
let E : WithTop Key := r.end_.key
|
||
let e : Nat := (r.end_.offset - 1).toNat
|
||
let P : Key → Bool := fun k => decide (E ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) ≤ ((H : Key) : WithTop Key))
|
||
let Q : Key → Bool := fun k => decide (E ≤ (k : WithTop Key))
|
||
let BT : Key → Bool := fun k => decide (E ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < ((keyAfter H : Key) : WithTop Key))
|
||
have htlen : res.keys.length = (r.end_.offset - 1).toNat := by
|
||
have hcount := crossed_rev_count_ge_eq_length (r := r) (res := res) hrev hbegin
|
||
hcross (by omega) ⟨db, hcomp⟩
|
||
rw [← hcount]
|
||
exact ht
|
||
have hT : (⟨r.end_.key, (keyAfter (res.keys.head hne) : WithTop Key)⟩ :
|
||
ConflictRange).keys db = res.keys.reverse :=
|
||
crossed_rev_finite_keys_eq (db := db) hcomp hmore hrev hbegin hcross (by omega) hne htlen
|
||
have hT' : db.keys.filter BT = res.keys.reverse := by
|
||
simpa only [BT, L, H, E, ConflictRange.keys] using hT
|
||
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev
|
||
have hLH : L ≤ H := by
|
||
dsimp only [L, H]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_head (List.getLast_mem hne)
|
||
have hepos : 0 < e := by dsimp only [e]; omega
|
||
-- `H ≥ E`, from a reported key at or above `E`.
|
||
have htpos : 0 < res.keys.countP Q := by
|
||
dsimp only [Q]
|
||
rw [ht]
|
||
exact hepos
|
||
obtain ⟨k₀, hk₀mem, hk₀Q⟩ := List.countP_pos_iff.mp htpos
|
||
have hk₀E : E ≤ (k₀ : WithTop Key) := by
|
||
dsimp only [Q] at hk₀Q
|
||
exact of_decide_eq_true hk₀Q
|
||
have hk₀H : k₀ ≤ H := by
|
||
dsimp only [H]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_head hk₀mem
|
||
have hHE : E ≤ ((H : Key) : WithTop Key) := le_trans hk₀E (WithTop.coe_le_coe.mpr hk₀H)
|
||
have hHmemC : H ∈ res.keys.reverse := by
|
||
dsimp only [H]
|
||
exact List.mem_reverse.mpr (List.head_mem hne)
|
||
have hHdb : H ∈ db.keys := by
|
||
have h := hHmemC
|
||
rw [← hT'] at h
|
||
exact List.mem_of_mem_filter h
|
||
-- The number of stored keys in `[E, H]` is `e`.
|
||
have hcountEH : db.keys.countP P = e := by
|
||
have h1 : db.keys.countP P = db.keys.countP (fun k => Q k && BT k) := by
|
||
apply List.countP_congr
|
||
intro k hk
|
||
dsimp only [P, Q, BT]
|
||
simp only [decide_eq_true_eq, Bool.and_eq_true]
|
||
constructor
|
||
· rintro ⟨hEk, hkH⟩
|
||
refine ⟨hEk, hEk, ?_⟩
|
||
exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k H).mpr (WithTop.coe_le_coe.mp hkH))
|
||
· rintro ⟨hEk, -, hkH⟩
|
||
exact ⟨hEk, WithTop.coe_le_coe.mpr
|
||
((lt_keyAfter_iff k H).mp (WithTop.coe_lt_coe.mp hkH))⟩
|
||
have h2 : db.keys.countP (fun k => Q k && BT k) = e := by
|
||
rw [← List.countP_filter]
|
||
rw [hT', List.Perm.countP_eq _ (List.reverse_perm res.keys)]
|
||
dsimp only [Q, e]
|
||
exact ht
|
||
rw [h1, h2]
|
||
-- Split at `E` and `H` to compute the index of `H`.
|
||
have hEne : E ≠ (⊤ : WithTop Key) := by
|
||
intro htop
|
||
rw [htop] at hHE
|
||
exact absurd hHE (not_le.mpr (WithTop.coe_lt_top H))
|
||
obtain ⟨v, hv⟩ := WithTop.ne_top_iff_exists.mp hEne
|
||
have hvH : v ≤ H := by
|
||
rw [← hv] at hHE
|
||
exact WithTop.coe_le_coe.mp hHE
|
||
have hcountP_split : db.keys.countP P =
|
||
(db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length + 1 := by
|
||
rw [List.countP_eq_length_filter]
|
||
have hlist : db.keys.filter P =
|
||
(db.keys.filter (fun k => decide (v ≤ k ∧ k < H))) ++ [H] := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
· apply List.sortedLT_append.mpr
|
||
refine ⟨List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _),
|
||
sortedLT_singleton H, ?_⟩
|
||
intro a ha b hb
|
||
rw [List.mem_singleton] at hb
|
||
subst hb
|
||
exact (of_decide_eq_true (List.mem_filter.mp ha).2).2
|
||
· intro x
|
||
dsimp only [P]
|
||
rw [← hv]
|
||
simp only [List.mem_filter, List.mem_append, List.mem_singleton,
|
||
decide_eq_true_eq, WithTop.coe_le_coe]
|
||
constructor
|
||
· rintro ⟨hx, hvx, hxH⟩
|
||
by_cases hxH' : x = H
|
||
· exact Or.inr hxH'
|
||
· exact Or.inl ⟨hx, hvx, lt_of_le_of_ne hxH hxH'⟩
|
||
· rintro (⟨hx, hvx, hxH⟩ | rfl)
|
||
· exact ⟨hx, hvx, le_of_lt hxH⟩
|
||
· exact ⟨hHdb, hvH, le_refl H⟩
|
||
rw [hlist, List.length_append, List.length_singleton]
|
||
have hfilter_len : (db.keys.filter (fun k => decide (v ≤ k ∧ k < H))).length = e - 1 := by
|
||
omega
|
||
have hsplit := countP_lt_eq_add_filter db.keys db.nodup hvH
|
||
have hcE : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) =
|
||
db.keys.countP (fun k : Key => decide (k < v)) := by
|
||
rw [← hv]
|
||
exact countP_lt_coe v
|
||
have hcH : db.keys.countP (fun k : Key => decide (k < H)) =
|
||
db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by
|
||
have h := hsplit
|
||
rw [← hcE, hfilter_len] at h
|
||
omega
|
||
obtain ⟨j, hjlt, hj⟩ := List.mem_iff_getElem.mp hHdb
|
||
have hcj : db.keys.countP (fun k : Key => decide (k < H)) = j :=
|
||
countP_lt_eq_of_getElem db.sorted hjlt hj
|
||
have hj_eq : j = db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e - 1 := by
|
||
rw [← hcj]
|
||
exact hcH
|
||
have hEanchor : (r.end_.anchor db).toNat =
|
||
db.keys.countP (fun k : Key => decide ((k : WithTop Key) < E)) + e := by
|
||
rw [anchor_toNat_eq (s := r.end_) (by omega)]
|
||
-- Inserting `k` at or above `E` does not change the count below `E`, so the
|
||
-- anchor still targets the successor of `H`; the new key lies below `H`, which
|
||
-- shifts `H` into that target index.
|
||
have hinskeys : (db.insert k hk0).keys =
|
||
db.keys.insertIdx (db.keys.countP (fun x => decide (x < k))) k := by
|
||
rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hk0]
|
||
have hcountnew : (db.insert k hk0).keys.countP
|
||
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
|
||
db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) := by
|
||
rw [hinskeys]
|
||
exact countP_insertIdx _ _ _ (by
|
||
rw [decide_eq_false_iff_not]
|
||
exact not_lt.mpr hEk)
|
||
have hanchornew : (r.end_.anchor (db.insert k hk0)).toNat =
|
||
db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e := by
|
||
rw [anchor_toNat_eq (s := r.end_) (by omega), hcountnew]
|
||
have hJeq : (r.end_.anchor (db.insert k hk0)).toNat = j + 1 := by
|
||
rw [hanchornew]
|
||
have hb : db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e =
|
||
(db.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) + e - 1) + 1 := by
|
||
omega
|
||
rw [hb, ← hj_eq]
|
||
have hpj : db.keys.countP (fun x : Key => decide (x < k)) ≤ j := by
|
||
rw [← hcj]
|
||
apply countP_le_countP_of_imp
|
||
intro x hx hlt
|
||
rw [decide_eq_true_eq] at hlt ⊢
|
||
have hkH : k < H := lt_of_lt_of_le (WithTop.coe_lt_coe.mp hkL) hLH
|
||
exact lt_trans hlt hkH
|
||
have hnewlen : (r.end_.anchor (db.insert k hk0)).toNat <
|
||
(db.insert k hk0).keys.length := by
|
||
rw [hinskeys, hJeq, List.length_insertIdx_of_le_length List.countP_le_length]
|
||
omega
|
||
have hnewget? : (db.insert k hk0).keys[(r.end_.anchor (db.insert k hk0)).toNat]? =
|
||
some H := by
|
||
rw [hinskeys, hJeq, List.getElem?_insertIdx]
|
||
have hlt : ¬ j + 1 < db.keys.countP (fun x => decide (x < k)) := by omega
|
||
have heq : ¬ j + 1 = db.keys.countP (fun x => decide (x < k)) := by omega
|
||
rw [if_neg hlt, if_neg heq, Nat.add_sub_cancel, List.getElem?_eq_getElem hjlt, hj]
|
||
obtain ⟨_, hnewget⟩ := List.getElem?_eq_some_iff.mp hnewget?
|
||
have hHresolve : resolve (db.insert k hk0) r.end_ = ((H : Key) : WithTop Key) := by
|
||
rw [resolve_eq_getElem (anchor_nonneg_of_one_le (by omega)) hnewlen]
|
||
exact congrArg (fun x : Key => (x : WithTop Key)) hnewget
|
||
intro hcompNew
|
||
have hpre := RangeRead.compatible_keys_prefix hcompNew
|
||
have hHmem : H ∈ res.keys := by
|
||
dsimp only [H]
|
||
exact List.head_mem hne
|
||
have hHcomp : H ∈ RangeRead.completeKeys (db.insert k hk0) r := hpre.subset hHmem
|
||
have hlt := (RangeRead.mem_completeKeys.mp hHcomp).2.2
|
||
rw [hHresolve] at hlt
|
||
exact (lt_irrefl ((H : Key) : WithTop Key)) hlt
|
||
|
||
/-- In the finite crossed reversed branch no compatible database stores a key in
|
||
`[end_.key, last reported)`: the candidate range `[end_.key, keyAfter head)` contains
|
||
exactly the reported keys reversed, so any stored key below the reported last would be
|
||
one of those reported keys. -/
|
||
theorem crossed_rev_no_key_between {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 2 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
(ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) =
|
||
(r.end_.offset - 1).toNat)
|
||
{k : Key} (hkdb : k ∈ db.keys)
|
||
(hEk : r.end_.key ≤ (k : WithTop Key))
|
||
(hkL : k < res.keys.getLast hne) : False := by
|
||
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp hrev
|
||
have hLle : ∀ x ∈ res.keys, res.keys.getLast hne ≤ x :=
|
||
fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hLH : res.keys.getLast hne ≤ res.keys.head hne :=
|
||
hLle _ (List.head_mem hne)
|
||
have hkH : k < res.keys.head hne := lt_of_lt_of_le hkL hLH
|
||
have hklt : (k : WithTop Key) <
|
||
((keyAfter (res.keys.head hne) : Key) : WithTop Key) :=
|
||
WithTop.coe_lt_coe.mpr (lt_trans hkH (keyAfter_self_lt _))
|
||
have htlen : res.keys.length = (r.end_.offset - 1).toNat := by
|
||
have hcount := crossed_rev_count_ge_eq_length (r := r) (res := res) hrev hbegin
|
||
hcross (by omega) ⟨db, hcomp⟩
|
||
rw [← hcount]
|
||
exact ht
|
||
have hT := crossed_rev_finite_keys_eq (db := db) hcomp hmore hrev hbegin hcross (by omega) hne htlen
|
||
have hmem : k ∈ (⟨r.end_.key,
|
||
(keyAfter (res.keys.head hne) : WithTop Key)⟩ : ConflictRange).keys db := by
|
||
rw [ConflictRange.mem_keys_iff]
|
||
exact ⟨hkdb, hEk, hklt⟩
|
||
rw [hT] at hmem
|
||
have hkres : k ∈ res.keys := List.mem_reverse.mp hmem
|
||
exact absurd (hLle k hkres) (not_le.mpr hkL)
|
||
|
||
/-- In the finite crossed reversed branch, every key in `[end_.key, last reported)` is
|
||
necessary. -/
|
||
theorem necessary_finite_below_last_crossed_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key)
|
||
(hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ [])
|
||
(ht : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) =
|
||
(r.end_.offset - 1).toNat)
|
||
{k : Key} (hEk : r.end_.key ≤ (k : WithTop Key))
|
||
(hkL : ((k : Key) : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key)) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hk0 : k ∉ db₀.keys := by
|
||
intro hkdb
|
||
exact crossed_rev_no_key_between (db := db₀) hcomp₀ hmore hrev hbegin hcross
|
||
hend hne ht hkdb hEk (WithTop.coe_lt_coe.mp hkL)
|
||
have hagree : AgreeOn cr db₀ (db₀.insert k hk0) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₀ hk0 hkcr).symm
|
||
have hcompNew : RangeRead.Compatible (db₀.insert k hk0) r res :=
|
||
(hs db₀ _ hagree).mp hcomp₀
|
||
exact crossed_rev_insert_between_incompatible (db := db₀) hcomp₀ hmore hrev hbegin
|
||
hcross hend hne ht hk0 hEk hkL hcompNew
|
||
|
||
/-- Length partition of `above E` at `L`. -/
|
||
theorem length_above_eq_between_add_above {db : DB} {E L : WithTop Key}
|
||
(hEL : E ≤ L) :
|
||
(above db E).length = (between db E L).length + (above db L).length := by
|
||
have h1 := cnt_eq_add_between db hEL
|
||
have h2 := cnt_add_above_length db E
|
||
have h3 := cnt_add_above_length db L
|
||
omega
|
||
|
||
/-- Under the room condition, the complete crossed reversed window is all keys at or
|
||
above `begin.key`, reversed. -/
|
||
theorem completeKeys_eq_above_begin_rev_of_room {db : DB} {r : RangeRead}
|
||
{res : Result} {L : Key}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hBL : r.begin.key ≤ (L : WithTop Key))
|
||
(habove : above db (L : WithTop Key) = res.keys.reverse)
|
||
(hroom : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length <
|
||
(r.end_.offset - 1).toNat) :
|
||
RangeRead.completeKeys db r = (above db r.begin.key).reverse := by
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set B : WithTop Key := r.begin.key with hB
|
||
set e : Nat := (r.end_.offset - 1).toNat with he
|
||
have hEB : E ≤ B := le_of_lt hcross
|
||
have hEL : E ≤ (L : WithTop Key) := le_trans hEB hBL
|
||
have hbet : between db E (L : WithTop Key) =
|
||
db.keys.filter (fun k : Key => decide (E ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < (L : WithTop Key))) := rfl
|
||
have hlenE : (above db E).length = (between db E (L : WithTop Key)).length +
|
||
res.keys.length := by
|
||
rw [length_above_eq_between_add_above hEL, habove, List.length_reverse]
|
||
have hroom' : (between db E (L : WithTop Key)).length + res.keys.length < e := by
|
||
rw [hbet]; exact hroom
|
||
have htake : (above db E).take e = above db E := by
|
||
rw [List.take_eq_self_iff]
|
||
omega
|
||
rw [completeKeys_crossed_rev_eq (db := db) (r := r) hrev hbegin hcross hend, htake,
|
||
← above_eq_drop_between hEB]
|
||
|
||
/-- The filter at or above `L` of the complete crossed reversed window is `res`. -/
|
||
theorem completeKeys_filter_ge_crossed_rev_of_room {db : DB} {r : RangeRead}
|
||
{res : Result} {L : Key}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hBL : r.begin.key ≤ (L : WithTop Key))
|
||
(habove : above db (L : WithTop Key) = res.keys.reverse)
|
||
(hroom : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length <
|
||
(r.end_.offset - 1).toNat) :
|
||
(RangeRead.completeKeys db r).filter (fun k : Key => decide (L ≤ k)) = res.keys := by
|
||
rw [completeKeys_eq_above_begin_rev_of_room hrev hbegin hcross hend hBL habove hroom,
|
||
List.filter_reverse]
|
||
have hfilter : (above db r.begin.key).filter (fun k : Key => decide (L ≤ k)) =
|
||
above db (L : WithTop Key) := by
|
||
rw [above_eq_filter, above_eq_filter, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hL : L ≤ k
|
||
· have hB : r.begin.key ≤ (k : WithTop Key) := le_trans hBL (WithTop.coe_le_coe.mpr hL)
|
||
simp [hL, hB]
|
||
· simp [hL]
|
||
rw [hfilter, habove, List.reverse_reverse]
|
||
|
||
|
||
|
||
/-- The complete keys of a crossed reversed read as a reversed prefix of the keys at
|
||
or above `begin.key`. -/
|
||
theorem completeKeys_crossed_rev_eq_take_above_begin {db : DB} {r : RangeRead}
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset) :
|
||
RangeRead.completeKeys db r =
|
||
((above db r.begin.key).take
|
||
(min (cnt db r.end_.key + (r.end_.offset - 1).toNat) db.keys.length -
|
||
cnt db r.begin.key)).reverse := by
|
||
set B : WithTop Key := r.begin.key with hB
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set e : Nat := (r.end_.offset - 1).toNat with he
|
||
rw [completeKeys_rev_eq_reverse db r hrev]
|
||
rw [completeKeys_eq_filter_take_of_offset_one (db := db)
|
||
(r := { r with reverse := false }) (by simp) hbegin hend]
|
||
have hfilB : db.keys.filter (fun k : Key =>
|
||
decide (({ r with reverse := false } : RangeRead).begin.key ≤
|
||
(k : WithTop Key))) = above db B := rfl
|
||
rw [hfilB]
|
||
rfl
|
||
|
||
private theorem length_filter_lt_eq_sub {l : List Key} {L : Key} :
|
||
(l.filter (fun k : Key => !decide (L ≤ k))).length =
|
||
l.length - (l.filter (fun k : Key => decide (L ≤ k))).length := by
|
||
have h : (l.filter (fun k : Key => decide (L ≤ k))).length +
|
||
(l.filter (fun k : Key => !decide (L ≤ k))).length = l.length := by
|
||
have hp := List.filter_append_perm (fun k : Key => decide (L ≤ k)) l
|
||
have := hp.length_eq
|
||
simpa only [List.length_append] using this
|
||
omega
|
||
|
||
/-- A compatible crossed reversed result with the room condition has its keys at or
|
||
above the reported last exactly the reported keys, reversed. -/
|
||
theorem above_last_eq_reverse_of_compatible_crossed_rev {db : DB} {r : RangeRead}
|
||
{res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
{L : Key} (hL : L = res.keys.getLast hne)
|
||
(hroom : (db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length <
|
||
(r.end_.offset - 1).toNat) :
|
||
above db (L : WithTop Key) = res.keys.reverse := by
|
||
set E : WithTop Key := r.end_.key with hE
|
||
set B : WithTop Key := r.begin.key with hB
|
||
set e : Nat := (r.end_.offset - 1).toNat with he
|
||
set P : List Key := above db B with hPdef
|
||
set g : Nat := (between db E B).length with hgdef
|
||
set h : Nat := (between db E (L : WithTop Key)).length with hhdef
|
||
set t : Nat := res.keys.length with htdef
|
||
set m : Nat := min (cnt db E + e) db.keys.length - cnt db B with hmdef
|
||
set b : Nat := ((P.take m).filter (fun k : Key => decide (k < L))).length with hbdef
|
||
have hEB : E ≤ B := le_of_lt hcross
|
||
have hBL : B ≤ (L : WithTop Key) := by
|
||
have := crossed_rev_res_ge_begin hcomp hbegin L (by rw [hL]; exact List.getLast_mem hne)
|
||
simpa only [B] using this
|
||
have hW : RangeRead.completeKeys db r = (P.take m).reverse := by
|
||
rw [completeKeys_crossed_rev_eq_take_above_begin (db := db) (r := r) hrev hbegin
|
||
hcross hend]
|
||
have hWf := ((compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp).2
|
||
have hPf : (P.take m).filter (fun k : Key => decide (L ≤ k)) = res.keys.reverse := by
|
||
have h := hWf
|
||
rw [hW, List.filter_reverse, ← hL] at h
|
||
exact List.reverse_injective (by simpa only [List.reverse_reverse] using h)
|
||
have hcalc : m = min e (above db E).length - g := by
|
||
rw [hmdef, ← cnt_add_above_length db E, cnt_eq_add_between db hEB,
|
||
Nat.add_min_add_left, Nat.add_sub_add_left]
|
||
have hlenE : (above db E).length = g + P.length := by
|
||
have h1 := cnt_add_above_length db E
|
||
have h2 := cnt_add_above_length db B
|
||
have h3 := cnt_eq_add_between db hEB
|
||
dsimp only [g, P]
|
||
omega
|
||
have hm : m = min (e - g) P.length := by
|
||
rw [hcalc, hlenE, ← Nat.sub_min_sub_right e (g + P.length) g, Nat.add_sub_cancel_left]
|
||
have hm_le : m ≤ P.length := by rw [hm]; exact Nat.min_le_right _ _
|
||
have htake_len : (P.take m).length = m := by
|
||
rw [List.length_take, Nat.min_eq_left hm_le]
|
||
have hbt : m = t + b := by
|
||
have hp := List.filter_append_perm (fun k : Key => decide (L ≤ k)) (P.take m)
|
||
have hlen := hp.length_eq
|
||
rw [List.length_append] at hlen
|
||
have h1 : (P.take m).filter (fun k : Key => !decide (L ≤ k)) =
|
||
(P.take m).filter (fun k : Key => decide (k < L)) := by
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases h : L ≤ k
|
||
· have hlt : ¬ k < L := not_lt.mpr h
|
||
simp [h, hlt]
|
||
· have hlt : k < L := not_le.mp h
|
||
simp [h, hlt]
|
||
rw [hPf, List.length_reverse, ← htdef, h1, ← hbdef, htake_len] at hlen
|
||
omega
|
||
have hgj : h = g + (between db B (L : WithTop Key)).length := by
|
||
have h1 := cnt_eq_add_between (db := db) hEB
|
||
have h2 := cnt_eq_add_between (db := db) hBL
|
||
have h3 := cnt_eq_add_between (db := db) (le_trans hEB hBL)
|
||
unfold h g
|
||
omega
|
||
have hgj' : h = (cnt db B - cnt db E) + (between db B (L : WithTop Key)).length := by
|
||
have hc := cnt_sub_eq_between_length (db := db) hEB
|
||
omega
|
||
have hb_le : b ≤ (between db B (L : WithTop Key)).length := by
|
||
have hsub : (P.take m).Sublist db.keys := by
|
||
rw [hPdef]
|
||
exact (List.take_sublist m (above db B)).trans List.filter_sublist
|
||
have hall : ∀ k ∈ P.take m, B ≤ (k : WithTop Key) := by
|
||
intro k hk
|
||
have hkP : k ∈ P := List.mem_of_mem_take hk
|
||
rw [hPdef] at hkP
|
||
exact of_decide_eq_true (List.mem_filter.mp hkP).2
|
||
have hb_eq : ((P.take m).filter (fun k : Key => decide (k < L))).length =
|
||
((P.take m).filter (fun k : Key => decide (B ≤ (k : WithTop Key) ∧
|
||
(k : Key) < L))).length := by
|
||
congr 1
|
||
apply List.filter_congr
|
||
intro k hk
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun hkL => ⟨hall k hk, hkL⟩, fun hk => hk.2⟩
|
||
rw [hbdef, hb_eq]
|
||
have hb2 := (List.Sublist.filter (fun k : Key => decide (B ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < (L : WithTop Key))) hsub).length_le
|
||
simpa only [between, WithTop.coe_lt_coe] using hb2
|
||
by_cases hPlen : P.length ≤ m
|
||
· have htakeP : P.take m = P := List.take_of_length_le hPlen
|
||
rw [htakeP] at hPf
|
||
have hfilter : P.filter (fun k : Key => decide (L ≤ k)) = above db (L : WithTop Key) := by
|
||
rw [hPdef, above_eq_filter, above_eq_filter, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hLk : L ≤ k
|
||
· have hBk : B ≤ (k : WithTop Key) := le_trans hBL (WithTop.coe_le_coe.mpr hLk)
|
||
simp [hLk, hBk]
|
||
· simp [hLk]
|
||
rwa [hfilter] at hPf
|
||
· have hPlt : m < P.length := lt_of_not_ge hPlen
|
||
have hle : e - g ≤ P.length := by
|
||
by_contra hcon
|
||
have hmin : min (e - g) P.length = P.length := Nat.min_eq_right (le_of_not_ge hcon)
|
||
rw [hm, hmin] at hPlt
|
||
omega
|
||
have hmE : m = e - g := by rw [hm, Nat.min_eq_left hle]
|
||
have hb_val : b = e - g - t := by omega
|
||
have hroom' : h + t < e := by simpa only [h, between] using hroom
|
||
have hhle : h ≤ e - t - 1 := by omega
|
||
have hble : b ≤ (between db B (L : WithTop Key)).length := hb_le
|
||
have hgf : g ≤ e - t - 1 := by omega
|
||
have hdf : (between db B (L : WithTop Key)).length ≤ e - t - 1 - g := by omega
|
||
have hb_f : b = e - t - 1 - g + 1 := by omega
|
||
omega
|
||
|
||
|
||
|
||
/-- **Soundness of the crossed reversed truncated top branch starting at the reported
|
||
last key.** When the successor chain from `end_.key` reaches the reported last, the
|
||
room between `end_.key` and the last fits inside the end overshoot, so agreement on
|
||
`[last, ⊤)` determines all keys above `begin_.key` inside the complete window. -/
|
||
theorem sound_truncatedRevCrossed_top_last {r : RangeRead} {res : Result}
|
||
(hmore : res.more = true) (hrev : r.reverse = true) (hbegin : r.begin.offset = 1)
|
||
(hcross : r.end_.key < r.begin.key) (hend : 1 ≤ r.end_.offset)
|
||
(hne : res.keys ≠ [])
|
||
{E L : Key} {n : Nat}
|
||
(hE : r.end_.key = (E : WithTop Key)) (hL : L = res.keys.getLast hne)
|
||
(hn : n = (r.end_.offset - 1).toNat - res.keys.length - 1)
|
||
(hno : L ≤ keyAfterN n E) :
|
||
Sound r res ⟨(L : WithTop Key), ⊤⟩ := by
|
||
intro db₁ db₂ hagree
|
||
have hno' : res.keys.getLast hne ≤ keyAfterN n E := by rw [← hL]; exact hno
|
||
have step : ∀ db db', AgreeOn ⟨(L : WithTop Key), ⊤⟩ db db' →
|
||
RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by
|
||
intro db db' hag' hcomp
|
||
have hBL : r.begin.key ≤ (L : WithTop Key) := by
|
||
have h := crossed_rev_res_ge_begin hcomp hbegin L
|
||
(by rw [hL]; exact List.getLast_mem hne)
|
||
simpa only [hL] using h
|
||
have hroom_of : ∀ (d : DB),
|
||
(d.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < (L : WithTop Key)))).length + res.keys.length <
|
||
(r.end_.offset - 1).toNat := by
|
||
intro d
|
||
have hle := length_filter_between_le_of_le_keyAfterN d.nodup
|
||
(E := E) (L := res.keys.getLast hne) (n := n) hno'
|
||
have hEq : (d.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key) ∧
|
||
(k : WithTop Key) < (L : WithTop Key)))) =
|
||
(d.keys.filter (fun k : Key => decide (E ≤ k ∧ k < res.keys.getLast hne))) := by
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
rw [hE, hL]
|
||
simp
|
||
rw [hEq]
|
||
have hElt : E < res.keys.getLast hne := by
|
||
have h1 : (E : WithTop Key) < (L : WithTop Key) := by
|
||
rw [← hE]
|
||
exact lt_of_lt_of_le hcross hBL
|
||
exact WithTop.coe_lt_coe.mp (by simpa only [hL] using h1)
|
||
have hnpos : 0 < n := by
|
||
by_contra hcon
|
||
have hn0 : n = 0 := Nat.eq_zero_of_not_pos hcon
|
||
rw [hn0, keyAfterN_zero] at hno'
|
||
exact absurd hno' (not_le.mpr hElt)
|
||
have he : res.keys.length + 1 < (r.end_.offset - 1).toNat := by omega
|
||
omega
|
||
have hroom_db : (db.keys.filter (fun k : Key => decide (r.end_.key ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < (L : WithTop Key)))).length +
|
||
res.keys.length < (r.end_.offset - 1).toNat := hroom_of db
|
||
have hroom_db' : (db'.keys.filter (fun k : Key => decide (r.end_.key ≤
|
||
(k : WithTop Key) ∧ (k : WithTop Key) < (L : WithTop Key)))).length +
|
||
res.keys.length < (r.end_.offset - 1).toNat := hroom_of db'
|
||
have habove_db := above_last_eq_reverse_of_compatible_crossed_rev hcomp hmore hrev
|
||
hbegin hcross hend hne hL hroom_db
|
||
have hagmem : ∀ k : Key, (L : WithTop Key) ≤ (k : WithTop Key) →
|
||
(k ∈ db.keys ↔ k ∈ db'.keys) := by
|
||
intro k hk
|
||
exact (agreeOn_iff ⟨(L : WithTop Key), ⊤⟩ db db').mp hag' k
|
||
(ConflictRange.mem_iff.mpr ⟨hk, WithTop.coe_lt_top k⟩)
|
||
have habove_eq : above db (L : WithTop Key) = above db' (L : WithTop Key) := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
· exact List.sortedLT_iff_pairwise.mpr (db'.sorted.pairwise.filter _)
|
||
· intro k
|
||
rw [mem_above, mem_above]
|
||
constructor
|
||
· rintro ⟨hk, hLk⟩
|
||
exact ⟨(hagmem k hLk).mp hk, hLk⟩
|
||
· rintro ⟨hk, hLk⟩
|
||
exact ⟨(hagmem k hLk).mpr hk, hLk⟩
|
||
have habove_db' : above db' (L : WithTop Key) = res.keys.reverse := by
|
||
rw [← habove_eq, habove_db]
|
||
have hPf := completeKeys_filter_ge_crossed_rev_of_room (db := db') hrev hbegin
|
||
hcross hend hBL habove_db' hroom_db'
|
||
exact (compatible_iff_truncated_keys_rev (db := db') hrev hmore hne).mpr
|
||
⟨hcomp.1, by simpa only [hL] using hPf⟩
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
|
||
|
||
|
||
theorem crossed_rev_pad_top_necessary {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key)
|
||
(hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ [])
|
||
{E : Key} (hE : r.end_.key = (E : WithTop Key))
|
||
(hEL : (E : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key))
|
||
{n : Nat}
|
||
(hlt : res.keys.countP
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) <
|
||
(r.end_.offset - 1).toNat)
|
||
(hn : n = (r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1)
|
||
(hroom : keyAfterN n E < res.keys.getLast hne)
|
||
{k : Key} (hEk : E ≤ k) (hkL : k < res.keys.getLast hne) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set L : Key := res.keys.getLast hne with hLdef
|
||
set H : Key := res.keys.head hne with hHdef
|
||
set e : Nat := (r.end_.offset - 1).toNat with hedef
|
||
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev
|
||
have hLle : ∀ x ∈ res.keys, L ≤ x := by
|
||
intro x hx
|
||
rw [hLdef]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hHge : ∀ x ∈ res.keys, x ≤ H := by
|
||
intro x hx
|
||
rw [hHdef]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_head hx
|
||
have hLH : L ≤ H := hLle H (by rw [hHdef]; exact List.head_mem hne)
|
||
have hkL' : k < L := by rw [hLdef]; exact hkL
|
||
have hk_notin_res : k ∉ res.keys := by
|
||
intro hx
|
||
exact absurd (hLle k hx) (not_le.mpr hkL')
|
||
have hk_notin_rev : k ∉ res.keys.reverse := by
|
||
rw [List.mem_reverse]; exact hk_notin_res
|
||
have htlen : res.keys.countP
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq, hE]
|
||
exact le_trans (le_of_lt hEL) (WithTop.coe_le_coe.mpr (hLle x hx))
|
||
have hn_e : e = n + res.keys.length + 1 := by
|
||
rw [hedef]
|
||
omega
|
||
-- the successor chain below `L`
|
||
set S : List Key := (List.range (n + 1)).map (chain E) with hSdef
|
||
have hS_len : S.length = n + 1 := by
|
||
rw [hSdef, List.length_map, List.length_range]
|
||
have hS_sorted : S.SortedLT := by
|
||
rw [hSdef]; exact sortedLT_map_chain E (n + 1)
|
||
have hS_nodup : S.Nodup := hS_sorted.pairwise.nodup
|
||
have hS_mem : ∀ x, x ∈ S ↔ ∃ i, i < n + 1 ∧ chain E i = x := by
|
||
intro x
|
||
rw [hSdef, List.mem_map]
|
||
constructor
|
||
· rintro ⟨i, hi, hix⟩; exact ⟨i, List.mem_range.mp hi, hix⟩
|
||
· rintro ⟨i, hi, hix⟩; exact ⟨i, List.mem_range.mpr hi, hix⟩
|
||
have hS_le : ∀ x ∈ S, x ≤ chain E n := by
|
||
intro x hx
|
||
obtain ⟨i, hi, rfl⟩ := (hS_mem x).mp hx
|
||
rcases lt_or_eq_of_le (by omega : i ≤ n) with h | h
|
||
· exact le_of_lt (chain_lt_mono h)
|
||
· rw [h]
|
||
have hS_lt_L : ∀ x ∈ S, x < L := by
|
||
intro x hx
|
||
have h := hS_le x hx
|
||
rw [chain_eq_keyAfterN E n] at h
|
||
exact lt_of_le_of_lt h hroom
|
||
have hS_ge_E : ∀ x ∈ S, E ≤ x := by
|
||
intro x hx
|
||
obtain ⟨i, hi, rfl⟩ := (hS_mem x).mp hx
|
||
exact chain_le E i
|
||
set P : List Key := (S.erase k).take n with hPdef
|
||
have hP_len : P.length = n := by
|
||
rw [hPdef, List.length_take]
|
||
have hge : n ≤ (S.erase k).length := by
|
||
by_cases hkS : k ∈ S
|
||
· rw [List.length_erase_of_mem hkS, hS_len]; omega
|
||
· rw [List.erase_of_not_mem hkS, hS_len]; omega
|
||
omega
|
||
have hP_sorted : P.SortedLT := by
|
||
rw [hPdef]
|
||
exact List.sortedLT_iff_pairwise.mpr
|
||
(List.Pairwise.take (i := n) (List.Pairwise.erase k (List.sortedLT_iff_pairwise.mp hS_sorted)))
|
||
have hP_sub : P ⊆ S := by
|
||
intro x hx
|
||
rw [hPdef] at hx
|
||
exact List.erase_subset ((List.take_sublist n (S.erase k)).subset hx)
|
||
have hP_lt_L : ∀ x ∈ P, x < L := fun x hx => hS_lt_L x (hP_sub hx)
|
||
have hP_ge_E : ∀ x ∈ P, E ≤ x := fun x hx => hS_ge_E x (hP_sub hx)
|
||
have hk_notin_P : k ∉ P := by
|
||
intro hx
|
||
have hx' : k ∈ S.erase k := (List.take_sublist n (S.erase k)).subset hx
|
||
exact (hS_nodup.mem_erase_iff.mp hx').1 rfl
|
||
set u : Key := keyAfter H with hudef
|
||
have hHlt_u : H < u := by rw [hudef]; exact keyAfter_self_lt H
|
||
have hres_lt_u : ∀ x ∈ res.keys.reverse, x < u := by
|
||
intro x hx
|
||
exact lt_of_le_of_lt (hHge x (List.mem_reverse.mp hx)) hHlt_u
|
||
have hk_notin_a : k ∉ P ++ res.keys.reverse ++ [u] := by
|
||
intro hx
|
||
rw [List.mem_append, List.mem_append] at hx
|
||
rcases hx with (hx | hx) | hx
|
||
· exact hk_notin_P hx
|
||
· exact hk_notin_rev hx
|
||
· rw [List.mem_singleton] at hx
|
||
exact (ne_of_lt (lt_trans (lt_of_lt_of_le hkL' hLH) hHlt_u)) hx
|
||
set aList : List Key := P ++ res.keys.reverse ++ [u] with hadef
|
||
have hres_rev_sorted : res.keys.reverse.SortedLT := hsort.reverse
|
||
have hP_lt_res : ∀ x ∈ P, ∀ y ∈ res.keys.reverse, x < y := by
|
||
intro x hx y hy
|
||
exact lt_of_lt_of_le (hP_lt_L x hx) (hLle y (List.mem_reverse.mp hy))
|
||
have ha_sorted : aList.SortedLT := by
|
||
rw [hadef]
|
||
refine List.sortedLT_append.mpr ⟨?_, ?_, ?_⟩
|
||
· exact List.sortedLT_append.mpr ⟨hP_sorted, hres_rev_sorted, hP_lt_res⟩
|
||
· exact List.sortedLT_iff_pairwise.mpr (by simp)
|
||
· intro x hx y hy
|
||
rw [List.mem_singleton] at hy
|
||
rw [hy]
|
||
rw [List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· exact lt_of_lt_of_le (hP_lt_L x hx) (le_trans hLH (le_of_lt hHlt_u))
|
||
· exact hres_lt_u x hx
|
||
let db_a : DB := ⟨aList, ha_sorted⟩
|
||
have hlen_a : aList.length = e := by
|
||
rw [hadef, List.length_append, List.length_append, hP_len,
|
||
List.length_reverse, List.length_singleton]
|
||
omega
|
||
have ha_ge_E : ∀ x ∈ aList, E ≤ x := by
|
||
intro x hx
|
||
rw [hadef, List.mem_append, List.mem_append] at hx
|
||
rcases hx with (hx | hx) | hx
|
||
· exact hP_ge_E x hx
|
||
· exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hEL))
|
||
(hLle x (List.mem_reverse.mp hx))
|
||
· rw [List.mem_singleton] at hx
|
||
rw [hx]
|
||
exact le_trans (le_of_lt (WithTop.coe_lt_coe.mp hEL))
|
||
(le_trans hLH (le_of_lt hHlt_u))
|
||
have hcount_a : aList.countP
|
||
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx hlt
|
||
rw [decide_eq_true_eq] at hlt
|
||
rw [hE] at hlt
|
||
exact absurd (WithTop.coe_le_coe.mpr (ha_ge_E x hx)) (not_le.mpr hlt)
|
||
have hanc_a : (r.end_.anchor db_a).toNat = e := by
|
||
rw [anchor_toNat_eq (db := db_a) (s := r.end_) (by omega), hcount_a]
|
||
omega
|
||
have hresend_a : resolve db_a r.end_ = (⊤ : WithTop Key) :=
|
||
resolve_eq_top (anchor_nonneg_of_one_le (db := db_a) (s := r.end_) (by omega))
|
||
(by rw [hlen_a, hanc_a])
|
||
have hbegin_le_L : r.begin.key ≤ (↑L : WithTop Key) := by
|
||
rw [hLdef]
|
||
exact crossed_rev_res_ge_begin hcomp₀ hbegin L
|
||
(by rw [hLdef]; exact List.getLast_mem hne)
|
||
have hL_mem_a : L ∈ db_a.keys := by
|
||
change L ∈ aList
|
||
rw [hadef]
|
||
simp only [List.mem_append, List.mem_singleton]
|
||
exact Or.inl (Or.inr (List.mem_reverse.mpr
|
||
(by rw [hLdef]; exact List.getLast_mem hne)))
|
||
have hrb_a : resolve db_a r.begin ≤ (↑L : WithTop Key) :=
|
||
(key_le_resolve_iff_of_offset_one (db := db_a) hbegin hL_mem_a).mp hbegin_le_L
|
||
have hck_a : RangeRead.completeKeys db_a r =
|
||
(RangeRead.inRange db_a.keys (resolve db_a r.begin) (⊤ : WithTop Key)).reverse := by
|
||
simp [RangeRead.completeKeys, hrev, hresend_a]
|
||
have hfilter_a : aList.filter (fun a : Key => decide (L ≤ a)) =
|
||
res.keys.reverse ++ [u] := by
|
||
rw [hadef, List.filter_append, List.filter_append]
|
||
have hPnil : P.filter (fun a : Key => decide (L ≤ a)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_le.mpr (hP_lt_L x hx)
|
||
have hRself : res.keys.reverse.filter (fun a : Key => decide (L ≤ a)) =
|
||
res.keys.reverse := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hLle x (List.mem_reverse.mp hx)
|
||
have hU : [u].filter (fun a : Key => decide (L ≤ a)) = [u] := by
|
||
simp only [List.filter_cons, List.filter_nil]
|
||
rw [decide_eq_true (le_of_lt (lt_of_le_of_lt hLH hHlt_u))]
|
||
simp
|
||
rw [hPnil, hRself, hU]
|
||
simp
|
||
have hnotcomp_a : ¬ RangeRead.Compatible db_a r res := by
|
||
intro hc
|
||
have hf := ((compatible_iff_truncated_keys_rev (db := db_a) hrev hmore hne).mp hc).2
|
||
rw [hck_a, List.filter_reverse] at hf
|
||
unfold RangeRead.inRange at hf
|
||
rw [List.filter_filter] at hf
|
||
have hpred : ∀ a ∈ db_a.keys,
|
||
(decide (L ≤ a) && decide (resolve db_a r.begin ≤ (↑a : WithTop Key) ∧
|
||
(↑a : WithTop Key) < (⊤ : WithTop Key))) = decide (L ≤ a) := by
|
||
intro a _
|
||
by_cases hLa : L ≤ a
|
||
· have hres : resolve db_a r.begin ≤ (↑a : WithTop Key) :=
|
||
le_trans hrb_a (WithTop.coe_le_coe.mpr hLa)
|
||
rw [decide_eq_true hLa, Bool.true_and, decide_eq_true]
|
||
exact ⟨hres, WithTop.coe_lt_top a⟩
|
||
· rw [decide_eq_false hLa, Bool.false_and]
|
||
rw [List.filter_congr hpred] at hf
|
||
have hda : db_a.keys = aList := rfl
|
||
rw [hda, hfilter_a, List.reverse_append, List.reverse_singleton,
|
||
List.reverse_reverse, List.singleton_append] at hf
|
||
have hlen := congrArg List.length hf
|
||
simp only [List.length_cons] at hlen
|
||
omega
|
||
-- insert the target key
|
||
let db_b : DB := db_a.insert k hk_notin_a
|
||
have hb_keys : db_b.keys = aList.orderedInsert (· ≤ ·) k := rfl
|
||
have hb_mem : ∀ x, x ∈ db_b.keys ↔ x = k ∨ x ∈ aList := by
|
||
intro x
|
||
rw [hb_keys, List.mem_orderedInsert]
|
||
have hb_len : db_b.keys.length = e + 1 := by
|
||
rw [hb_keys]
|
||
have hp := List.Perm.length_eq (List.perm_orderedInsert (· ≤ ·) k aList)
|
||
simp only [List.length_cons] at hp
|
||
rw [hp, hlen_a]
|
||
have hb_ge_E : ∀ x ∈ db_b.keys, E ≤ x := by
|
||
intro x hx
|
||
rcases (hb_mem x).mp hx with rfl | hx
|
||
· exact hEk
|
||
· exact ha_ge_E x hx
|
||
have hcount_b : db_b.keys.countP
|
||
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx hlt
|
||
rw [decide_eq_true_eq] at hlt
|
||
rw [hE] at hlt
|
||
exact absurd (WithTop.coe_le_coe.mpr (hb_ge_E x hx)) (not_le.mpr hlt)
|
||
have hanc_b : (r.end_.anchor db_b).toNat = e := by
|
||
rw [anchor_toNat_eq (db := db_b) (s := r.end_) (by omega), hcount_b]
|
||
omega
|
||
have hresend_b : resolve db_b r.end_ = (↑u : WithTop Key) := by
|
||
have h0 := anchor_nonneg_of_one_le (db := db_b) (s := r.end_) (by omega)
|
||
have hidx : (r.end_.anchor db_b).toNat < db_b.keys.length := by
|
||
rw [hanc_b, hb_len]; omega
|
||
have hu_mem : u ∈ db_b.keys := by
|
||
rw [hb_mem]; exact Or.inr (by rw [hadef]; simp)
|
||
have hall_le_u : ∀ x ∈ db_b.keys, x ≤ u := by
|
||
intro x hx
|
||
rcases (hb_mem x).mp hx with rfl | hx
|
||
· exact le_of_lt (lt_trans (lt_of_lt_of_le hkL' hLH) hHlt_u)
|
||
· rw [hadef] at hx
|
||
rw [List.mem_append, List.mem_append] at hx
|
||
rcases hx with (hx | hx) | hx
|
||
· exact le_of_lt (lt_of_lt_of_le (hP_lt_L x hx)
|
||
(le_trans hLH (le_of_lt hHlt_u)))
|
||
· exact le_of_lt (hres_lt_u x hx)
|
||
· rw [List.mem_singleton] at hx
|
||
rw [hx]
|
||
have hbne : db_b.keys ≠ [] := List.ne_nil_of_length_pos (by rw [hb_len]; omega)
|
||
have hlast : db_b.keys.getLast hbne = u := by
|
||
apply le_antisymm
|
||
· exact hall_le_u _ (List.getLast_mem hbne)
|
||
· exact (List.sortedLT_iff_pairwise.mp db_b.sorted).imp le_of_lt
|
||
|>.rel_getLast hu_mem
|
||
have hidx3 : db_b.keys.length - 1 = e := by rw [hb_len]; omega
|
||
have hgl := List.getLast_eq_getElem (l := db_b.keys) (h := hbne)
|
||
have hidx_e : e < db_b.keys.length := by rw [hb_len]; omega
|
||
have hkeyu : db_b.keys[e] = u := by
|
||
rw [List.getElem_eq_iff hidx_e, ← hidx3,
|
||
List.getElem?_eq_getElem (by omega), ← hgl, hlast]
|
||
have hkey_anchor : db_b.keys[(r.end_.anchor db_b).toNat] = u := by
|
||
rw [List.getElem_eq_iff hidx, hanc_b, List.getElem?_eq_getElem hidx_e, hkeyu]
|
||
rw [resolve_eq_getElem h0 hidx, hkey_anchor]
|
||
have hcomp_b : RangeRead.Compatible db_b r res := by
|
||
refine (compatible_iff_truncated_keys_rev (db := db_b) hrev hmore hne).mpr
|
||
⟨hcomp₀.1, ?_⟩
|
||
have hL_mem_res : L ∈ res.keys := by
|
||
rw [hLdef]; exact List.getLast_mem hne
|
||
have hL_mem_b : L ∈ db_b.keys := by
|
||
rw [hb_mem]; exact Or.inr (by rw [hadef]; simp [hL_mem_res])
|
||
have hrb_b : resolve db_b r.begin ≤ (↑L : WithTop Key) :=
|
||
(key_le_resolve_iff_of_offset_one (db := db_b) hbegin hL_mem_b).mp hbegin_le_L
|
||
have hck_b : RangeRead.completeKeys db_b r =
|
||
(RangeRead.inRange db_b.keys (resolve db_b r.begin) (↑u : WithTop Key)).reverse := by
|
||
simp [RangeRead.completeKeys, hrev, hresend_b]
|
||
rw [hck_b, List.filter_reverse]
|
||
unfold RangeRead.inRange
|
||
rw [List.filter_filter]
|
||
have hpred : ∀ a ∈ db_b.keys,
|
||
(decide (L ≤ a) && decide (resolve db_b r.begin ≤ (↑a : WithTop Key) ∧
|
||
(↑a : WithTop Key) < (↑u : WithTop Key))) = decide (L ≤ a ∧ a < u) := by
|
||
intro a _
|
||
by_cases hLa : L ≤ a
|
||
· have hres : resolve db_b r.begin ≤ (↑a : WithTop Key) :=
|
||
le_trans hrb_b (WithTop.coe_le_coe.mpr hLa)
|
||
rw [decide_eq_true hLa, Bool.true_and, decide_eq_decide]
|
||
exact ⟨fun h => ⟨hLa, WithTop.coe_lt_coe.mp h.2⟩,
|
||
fun h => ⟨hres, WithTop.coe_lt_coe.mpr h.2⟩⟩
|
||
· rw [decide_eq_false hLa]
|
||
simp [hLa]
|
||
rw [List.filter_congr hpred]
|
||
have hstep : db_b.keys.filter (fun a => decide (L ≤ a ∧ a < u)) = res.keys.reverse := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr (db_b.sorted.pairwise.filter _)
|
||
· exact hres_rev_sorted
|
||
· intro x
|
||
rw [List.mem_filter, List.mem_reverse]
|
||
constructor
|
||
· rintro ⟨hx, hpred⟩
|
||
have hpred' := of_decide_eq_true hpred
|
||
rcases (hb_mem x).mp hx with rfl | hx
|
||
· exact absurd hpred'.1 (not_le.mpr hkL')
|
||
· rw [hadef] at hx
|
||
rw [List.mem_append, List.mem_append] at hx
|
||
rcases hx with (hx | hx) | hx
|
||
· exact absurd hpred'.1 (not_le.mpr (hP_lt_L x hx))
|
||
· exact List.mem_reverse.mp hx
|
||
· rw [List.mem_singleton] at hx
|
||
rw [hx] at hpred'
|
||
exact absurd hpred'.2 (lt_irrefl u)
|
||
· intro hx
|
||
refine ⟨?_, ?_⟩
|
||
· rw [hb_mem]; exact Or.inr (by rw [hadef]; simp [hx])
|
||
· exact decide_eq_true ⟨hLle x hx, hres_lt_u x (List.mem_reverse.mpr hx)⟩
|
||
rw [hstep, List.reverse_reverse]
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hagree : AgreeOn cr db_a db_b :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db_a hk_notin_a hkcr).symm
|
||
exact hnotcomp_a ((hs db_a db_b hagree).mpr hcomp_b)
|
||
|
||
|
||
|
||
/-- **Minimality of the crossed top branch beginning at `end_.key`.** -/
|
||
theorem minimal_topRevCrossed_begin_end {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key)
|
||
(hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ [])
|
||
{E L : Key} {n : Nat}
|
||
(hE : r.end_.key = (E : WithTop Key)) (hL : L = res.keys.getLast hne)
|
||
(hn : n = (r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1)
|
||
(hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) <
|
||
(r.end_.offset - 1).toNat)
|
||
(hroom : keyAfterN n E < L) :
|
||
Minimal r res ⟨(E : WithTop Key), ⊤⟩ := by
|
||
have hEL : (E : WithTop Key) < ((res.keys.getLast hne : Key) : WithTop Key) := by
|
||
rw [← hL]
|
||
exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (keyAfterN_le_self E n) hroom)
|
||
have hroom' : keyAfterN n E < res.keys.getLast hne := by rw [← hL]; exact hroom
|
||
have hs : Sound r res ⟨(E : WithTop Key), ⊤⟩ := by
|
||
rw [← hE]
|
||
exact sound_truncatedRevCrossed_top hrev hbegin hcross (by omega)
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨hs, ?_⟩
|
||
intro k hk
|
||
have hEk : E ≤ k := WithTop.coe_le_coe.mp hk.1
|
||
by_cases hkL : k < L
|
||
· exact crossed_rev_pad_top_necessary hread hmore hrev hbegin hcross hend hne hE hEL hlt hn
|
||
hroom' hEk (by rw [← hL]; exact hkL)
|
||
· have hLk : L ≤ k := le_of_not_gt hkL
|
||
by_cases hkH : k ≤ res.keys.head hne
|
||
· exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne)
|
||
(List.head_mem hne) hLk hkH
|
||
· exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH)
|
||
|
||
/-- **Minimality of the crossed top branch beginning at the reported last key.** -/
|
||
theorem minimal_topRevCrossed_begin_last {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key)
|
||
(hend : 2 ≤ r.end_.offset)
|
||
{E L : Key} {n : Nat}
|
||
(hE : r.end_.key = (E : WithTop Key))
|
||
(hne : res.keys ≠ []) (hL : L = res.keys.getLast hne)
|
||
(hn : n = (r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) - 1)
|
||
(hlt : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) <
|
||
(r.end_.offset - 1).toNat)
|
||
(hcase : L ≤ E ∨ L ≤ keyAfterN n E) :
|
||
Minimal r res ⟨(L : WithTop Key), ⊤⟩ := by
|
||
have hcount : res.keys.countP
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length :=
|
||
crossed_rev_count_ge_eq_length hrev hbegin hcross (by omega) hread
|
||
have hn' : n = (r.end_.offset - 1).toNat - res.keys.length - 1 := by
|
||
rw [hn, hcount]
|
||
rcases hcase with hLE | hno
|
||
· have hLno : L ≤ keyAfterN n E :=
|
||
le_trans hLE (keyAfterN_le_self E n)
|
||
refine (minimal_iff_sound_and_necessary).mpr
|
||
⟨sound_truncatedRevCrossed_top_last hmore hrev hbegin hcross (by omega) hne hE hL hn' hLno, ?_⟩
|
||
intro k hk
|
||
have hLk : L ≤ k := WithTop.coe_le_coe.mp hk.1
|
||
by_cases hkH : k ≤ res.keys.head hne
|
||
· exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne)
|
||
(List.head_mem hne) hLk hkH
|
||
· exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH)
|
||
· refine (minimal_iff_sound_and_necessary).mpr
|
||
⟨sound_truncatedRevCrossed_top_last hmore hrev hbegin hcross (by omega) hne hE hL hn' hno, ?_⟩
|
||
intro k hk
|
||
have hLk : L ≤ k := WithTop.coe_le_coe.mp hk.1
|
||
by_cases hkH : k ≤ res.keys.head hne
|
||
· exact necessary_of_between_result hread (by rw [hL]; exact List.getLast_mem hne)
|
||
(List.head_mem hne) hLk hkH
|
||
· exact necessary_above_head_top_rev hread hmore hrev hbegin hne hlt (lt_of_not_ge hkH)
|
||
|
||
|
||
|
||
/-- **Minimality of the crossed finite reversed truncated branch.** -/
|
||
theorem minimal_truncatedRevCrossed_finite {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key)
|
||
(hend : 2 ≤ r.end_.offset) (hne : res.keys ≠ [])
|
||
(ht : res.keys.length = (r.end_.offset - 1).toNat) :
|
||
Minimal r res ⟨r.end_.key, (keyAfter (res.keys.headD ([] : Key)) : WithTop Key)⟩ := by
|
||
have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
have ht' : res.keys.countP (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) =
|
||
(r.end_.offset - 1).toNat := by
|
||
rw [crossed_rev_count_ge_eq_length hrev hbegin hcross (by omega) hread, ht]
|
||
refine (minimal_iff_sound_and_necessary).mpr
|
||
⟨sound_truncatedRevCrossed_finite hmore hrev hbegin hcross (by omega) hne ht, ?_⟩
|
||
intro k hk
|
||
rw [ConflictRange.mem_iff] at hk
|
||
have hkH : k ≤ res.keys.head hne := by
|
||
have h2 : (k : WithTop Key) < (keyAfter (res.keys.headD ([] : Key)) : WithTop Key) := hk.2
|
||
rw [hhead] at h2
|
||
exact (lt_keyAfter_iff k (res.keys.head hne)).mp (WithTop.coe_lt_coe.mp h2)
|
||
by_cases hkL : k < res.keys.getLast hne
|
||
· exact necessary_finite_below_last_crossed_rev hread hmore hrev hbegin hcross hend hne ht'
|
||
hk.1 (WithTop.coe_lt_coe.mpr hkL)
|
||
· have hLk : res.keys.getLast hne ≤ k := le_of_not_gt hkL
|
||
exact necessary_of_between_result hread (List.getLast_mem hne) (List.head_mem hne)
|
||
hLk hkH
|
||
|
||
|
||
|
||
/-- **Minimality of the corrected reversed truncated rule on crossed reads.** -/
|
||
theorem minimal_closedForm_of_offset_one_truncated_rev_fullC_crossed
|
||
{r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hcross : r.end_.key < r.begin.key)
|
||
(hend : 2 ≤ r.end_.offset) :
|
||
Minimal r res (closedFormOffsetOneTruncatedRevFullC r res) := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hread' : Readable r res := ⟨db₀, hcomp₀⟩
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||
have hhead : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
have hcount : res.keys.countP
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) = res.keys.length :=
|
||
crossed_rev_count_ge_eq_length hrev hbegin hcross (by omega) hread'
|
||
have hlen_le_e : res.keys.length ≤ (r.end_.offset - 1).toNat := by
|
||
have hpre := (RangeRead.compatible_keys_prefix hcomp₀).length_le
|
||
rw [completeKeys_crossed_rev_eq hrev hbegin hcross (by omega)] at hpre
|
||
rw [List.length_reverse, List.length_drop] at hpre
|
||
have htake : (List.take (r.end_.offset - 1).toNat (above db₀ r.end_.key)).length ≤
|
||
(r.end_.offset - 1).toNat := by
|
||
rw [List.length_take]
|
||
exact Nat.min_le_left _ _
|
||
omega
|
||
have hcount_le : res.keys.countP
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) ≤
|
||
(r.end_.offset - 1).toNat := by
|
||
rw [hcount]; exact hlen_le_e
|
||
unfold closedFormOffsetOneTruncatedRevFullC
|
||
rw [if_neg (by omega : ¬ r.end_.offset ≤ 1)]
|
||
dsimp only
|
||
by_cases ht : res.keys.countP
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) =
|
||
(r.end_.offset - 1).toNat
|
||
· rw [if_pos ht]
|
||
have htlen : res.keys.length = (r.end_.offset - 1).toNat := by
|
||
rw [← hcount]; exact ht
|
||
have hEL : r.end_.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) := by
|
||
rw [hlast]
|
||
exact le_trans (le_of_lt hcross)
|
||
(crossed_rev_res_ge_begin hcomp₀ hbegin (res.keys.getLast hne)
|
||
(List.getLast_mem hne))
|
||
have hmin_eq : min ((res.keys.getLastD ([] : Key) : Key) : WithTop Key) r.end_.key =
|
||
r.end_.key := min_eq_right hEL
|
||
rw [hmin_eq]
|
||
exact minimal_truncatedRevCrossed_finite hread' hmore hrev hbegin hcross hend hne htlen
|
||
· rw [if_neg ht]
|
||
have hlt : res.keys.countP
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) <
|
||
(r.end_.offset - 1).toNat := lt_of_le_of_ne hcount_le ht
|
||
cases hkey : r.end_.key with
|
||
| top =>
|
||
rw [hkey] at hcross
|
||
exact absurd hcross (not_lt_of_ge le_top)
|
||
| coe E =>
|
||
change Minimal r res (if keyAfterN ((r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1)
|
||
E < res.keys.getLastD ([] : Key) then ⟨(E : WithTop Key), ⊤⟩
|
||
else ⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), ⊤⟩)
|
||
by_cases hroom : keyAfterN ((r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1)
|
||
E < res.keys.getLastD ([] : Key)
|
||
· rw [if_pos hroom]
|
||
have hroom' : keyAfterN ((r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1)
|
||
E < res.keys.getLast hne := by
|
||
rw [← hlast]; exact hroom
|
||
exact minimal_topRevCrossed_begin_end hread' hmore hrev hbegin hcross hend hne hkey
|
||
rfl (by rw [hkey]) hlt hroom'
|
||
· rw [if_neg hroom]
|
||
have hge : res.keys.getLastD ([] : Key) ≤ keyAfterN ((r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1)
|
||
E := le_of_not_gt hroom
|
||
have hge' : res.keys.getLast hne ≤ keyAfterN ((r.end_.offset - 1).toNat -
|
||
res.keys.countP (fun k : Key => decide ((↑E : WithTop Key) ≤ (k : WithTop Key))) - 1)
|
||
E := by
|
||
rw [← hlast]; exact hge
|
||
rw [hlast]
|
||
exact minimal_topRevCrossed_begin_last hread' hmore hrev hbegin hcross hend hkey
|
||
hne rfl (by rw [hkey]) hlt (Or.inr hge')
|
||
|
||
|
||
|
||
end FormalConflictRange
|