- 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
2364 lines
119 KiB
Lean4
2364 lines
119 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.ZeroCrossLong
|
||
import FormalConflictRange.Reduction
|
||
import FormalConflictRange.NonposClosedForm
|
||
|
||
/-!
|
||
# Crossed truncated results at begin offset zero
|
||
|
||
At `begin.offset = 0` the begin anchor is `cnt begin.key - 1`, so a crossed read
|
||
(`begin.key` strictly above the reported head) resolves its begin selector exactly onto
|
||
the greatest stored key below `begin.key`, which compatibility forces to be the reported
|
||
head. No stored key can lie strictly between the head and `begin.key`.
|
||
|
||
This module closes the family with an unbounded end selector (`end.key = ⊤` and
|
||
`end.offset ≥ 1`). Writing `head` for the reported head and `last` for the reported
|
||
last, the minimal range is
|
||
|
||
`[head, max begin.key (keyAfter last))`.
|
||
|
||
The lower endpoint is the reported head. The upper endpoint is `begin.key` when the
|
||
reported keys stop below it (every key in `(last, begin.key)` would lift the begin
|
||
resolution off the head), and `keyAfter last` when the reported keys already reach it.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
/-- A forward truncated result at begin offset zero, crossed, with `end.offset = 1`
|
||
or an unbounded end selector (`end.key = ⊤`, `end.offset ≥ 1`). -/
|
||
abbrev zeroCrossTruncReadAllEnd (r : RangeRead) (res : Result) : Prop :=
|
||
r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧
|
||
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
|
||
(r.end_.offset = 1 ∨ (r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset))
|
||
|
||
/-- A forward truncated result at begin offset zero, crossed, with positive end offset
|
||
and every reported key strictly below `end.key`. -/
|
||
abbrev zeroCrossTruncBelowEnd (r : RangeRead) (res : Result) : Prop :=
|
||
r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧
|
||
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
|
||
1 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key
|
||
|
||
/-- A forward truncated result at begin offset zero, crossed, with positive end offset
|
||
and the end key at or above the reported head. -/
|
||
abbrev zeroCrossTruncEndGeHead (r : RangeRead) (res : Result) : Prop :=
|
||
r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧
|
||
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
|
||
1 ≤ r.end_.offset ∧
|
||
((leastReported r res : Key) : WithTop Key) ≤ r.end_.key
|
||
|
||
/-- A forward truncated result at begin offset zero, crossed, with the end key strictly
|
||
below the reported head and enough capacity in the gap `[end.key, head)` to absorb the
|
||
reported keys up to `end.offset - 1`. -/
|
||
abbrev zeroCrossTruncLowEndSmall (r : RangeRead) (res : Result) : Prop :=
|
||
r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧
|
||
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
|
||
2 ≤ r.end_.offset ∧
|
||
r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧
|
||
intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) +
|
||
(res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞)
|
||
|
||
/-- A forward truncated result at begin offset zero, crossed, with the end key strictly
|
||
below the reported head and more keys available in the gap `[end.key, head)` than the
|
||
end offset can absorb. In this cell the whole gap up to the head is necessary. -/
|
||
abbrev zeroCrossTruncLowEndBig (r : RangeRead) (res : Result) : Prop :=
|
||
r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧
|
||
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
|
||
2 ≤ r.end_.offset ∧
|
||
r.end_.key < ((leastReported r res : Key) : WithTop Key) ∧
|
||
(((r.end_.offset - 1).toNat : ℕ) : ℕ∞) <
|
||
intervalCard r.end_.key ((leastReported r res : Key) : WithTop Key) +
|
||
(res.keys.length : ℕ∞)
|
||
|
||
/-- The crossed truncated begin-zero closed form. -/
|
||
noncomputable def closedFormZeroCrossTrunc (r : RangeRead) (res : Result) : ConflictRange :=
|
||
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩
|
||
|
||
/-- The crossed truncated begin-zero closed form when the gap `[end.key, head)` is
|
||
saturated: the lower endpoint drops to `end.key`. -/
|
||
noncomputable def closedFormZeroCrossTruncLowEnd (r : RangeRead) (res : Result) :
|
||
ConflictRange :=
|
||
⟨r.end_.key,
|
||
max r.begin.key (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)⟩
|
||
|
||
/-- A forward truncated result at begin offset zero, crossed, with a nonpositive end
|
||
offset. -/
|
||
abbrev zeroCrossTruncNonposEnd (r : RangeRead) (res : Result) : Prop :=
|
||
r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧
|
||
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
|
||
r.end_.offset ≤ 0
|
||
|
||
/-- A forward truncated result at begin offset zero, crossed, with `end.offset = 0`. -/
|
||
abbrev zeroCrossTruncZeroEnd (r : RangeRead) (res : Result) : Prop :=
|
||
r.begin.offset = 0 ∧ r.reverse = false ∧ res.more = true ∧
|
||
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
|
||
r.end_.offset = 0
|
||
|
||
/-- The crossed truncated begin-zero closed form with a nonpositive end offset: the
|
||
reported head up to the larger of `begin.key` and `end_.key`. -/
|
||
noncomputable def closedFormZeroCrossTruncNonposEnd (r : RangeRead) (res : Result) :
|
||
ConflictRange :=
|
||
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key),
|
||
max r.begin.key r.end_.key⟩
|
||
|
||
/-- `getLastD` agrees with `getLast` on a nonempty list. -/
|
||
theorem getLastD_eq_getLast_zero_cross_trunc {l : List Key} (h : l ≠ []) :
|
||
l.getLastD ([] : Key) = l.getLast h := by
|
||
cases l with
|
||
| nil => exact absurd rfl h
|
||
| cons a t =>
|
||
cases t with
|
||
| nil => rfl
|
||
| cons b t => simp [List.getLastD, List.getLast]
|
||
|
||
/-- `headD` agrees with `head` on a nonempty list. -/
|
||
theorem headD_eq_head_zero_cross_trunc {l : List Key} (h : l ≠ []) :
|
||
l.headD ([] : Key) = l.head h := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head h]; rfl
|
||
|
||
/-- At begin offset zero, a compatible crossed truncated read resolves its begin
|
||
selector onto the reported head. -/
|
||
theorem resolve_begin_eq_head_of_zero_crossed_more {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) :
|
||
resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
|
||
have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne
|
||
have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db r :=
|
||
(RangeRead.compatible_keys_prefix hcomp).subset hhead_mem
|
||
have hhead_db : res.keys.head hne ∈ db.keys :=
|
||
(RangeRead.mem_completeKeys.mp hhead_ck).1
|
||
exact le_antisymm
|
||
(RangeRead.mem_completeKeys.mp hhead_ck).2.1
|
||
(resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross
|
||
(res.keys.head hne) hhead_db hcross)
|
||
|
||
/-- In a compatible crossed truncated begin-zero read, no stored key lies strictly
|
||
between the reported head and `begin.key`. -/
|
||
theorem no_stored_between_head_begin_of_zero_crossed_more {db : DB} {r : RangeRead}
|
||
{res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key) :
|
||
∀ k ∈ db.keys, ¬ (((res.keys.head hne : Key) : WithTop Key) < (k : WithTop Key) ∧
|
||
(k : WithTop Key) < r.begin.key) := by
|
||
have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne
|
||
have hhead_db : res.keys.head hne ∈ db.keys :=
|
||
RangeRead.compatible_keys_subset hcomp hhead_mem
|
||
have hL := resolve_begin_eq_head_of_zero_crossed_more hcomp hrev hmore hne hbegin hcross
|
||
intro k hk hkb
|
||
have hle := resolve_zero_begin_le_of_mem_below hbegin hhead_db hcross k hk hkb.2
|
||
rw [hL] at hle
|
||
exact absurd hle (not_le.mpr hkb.1)
|
||
|
||
/-- With an unbounded end selector, the complete keys are exactly the stored keys at
|
||
or above the begin resolution. -/
|
||
theorem completeKeys_eq_filter_ge_of_end_top {db : DB} {r : RangeRead} {H : Key}
|
||
(hrev : r.reverse = false) (hendkey : r.end_.key = (⊤ : WithTop Key))
|
||
(hendoff : 1 ≤ r.end_.offset)
|
||
(hbegin : resolve db r.begin = ((H : Key) : WithTop Key)) :
|
||
RangeRead.completeKeys db r =
|
||
db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ (k : WithTop Key))) := by
|
||
have htop : resolve db r.end_ = (⊤ : WithTop Key) :=
|
||
resolve_eq_top_of_key_eq_top hendkey hendoff
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hbegin, htop]
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.1, fun h => ⟨h, WithTop.coe_lt_top k⟩⟩
|
||
|
||
/-- The stored keys in `[H, last]` in a crossed truncated begin-zero read. -/
|
||
theorem completeKeys_filter_le_of_end_top {db : DB} {r : RangeRead} {H last : Key}
|
||
(hrev : r.reverse = false) (hendkey : r.end_.key = (⊤ : WithTop Key))
|
||
(hendoff : 1 ≤ r.end_.offset)
|
||
(hbegin : resolve db r.begin = ((H : Key) : WithTop Key)) :
|
||
(RangeRead.completeKeys db r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
(db.keys.filter (fun k : Key => decide (((H : Key) : WithTop Key) ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last)) := by
|
||
rw [completeKeys_eq_filter_ge_of_end_top (db := db) hrev hendkey hendoff hbegin]
|
||
|
||
/-- For `end_.offset = 1`, a stored key strictly below the resolved end is strictly
|
||
below `end_.key`. -/
|
||
theorem lt_end_key_of_lt_resolve_end_offset_one {db : DB} {r : RangeRead}
|
||
(hend : r.end_.offset = 1) {k : Key} (hk : k ∈ db.keys)
|
||
(h : (k : WithTop Key) < resolve db r.end_) :
|
||
(k : WithTop Key) < r.end_.key := by
|
||
set c := db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key)) with hc
|
||
have hanc : r.end_.anchor db = (c : ℤ) := by
|
||
unfold Selector.anchor; rw [hc]; omega
|
||
by_cases hlt : c < db.keys.length
|
||
· have hres : resolve db r.end_ = (((db.keys[c]'hlt : Key)) : WithTop Key) := by
|
||
rw [resolve_eq_ite, ite_eq_right (by rw [hanc]; exact not_lt.mpr (Int.natCast_nonneg _)),
|
||
hanc, Int.toNat_natCast, List.getElem?_eq_getElem hlt]
|
||
rw [hres] at h
|
||
by_contra hkE
|
||
push Not at hkE
|
||
obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hk
|
||
have hcj : c ≤ j := by
|
||
by_contra hcon
|
||
have hjc : j < c := not_le.mp hcon
|
||
have hklt := getElem_lt_of_lt_countP_top (w := r.end_.key) db.sorted hj
|
||
(by rw [← hc]; exact hjc)
|
||
rw [hjx] at hklt
|
||
exact absurd hkE (not_le.mpr hklt)
|
||
have hle0 : db.keys[c] ≤ db.keys[j] := (db.sorted.getElem_le_getElem_iff).mpr hcj
|
||
rw [hjx] at hle0
|
||
exact absurd h (not_lt.mpr (WithTop.coe_le_coe.mpr hle0))
|
||
· have hclen : c = db.keys.length :=
|
||
le_antisymm (by rw [hc]; exact List.countP_le_length) (not_lt.mp hlt)
|
||
have : decide ((k : WithTop Key) < r.end_.key) = true :=
|
||
(List.countP_eq_length.mp hclen) k hk
|
||
exact of_decide_eq_true this
|
||
|
||
/-- Filtering the complete keys at or below `last` reads off the stored keys in
|
||
`[H, last]`, provided the begin resolution is `H` and `last` lies below the end
|
||
resolution. This does not require the end resolution to be unbounded. -/
|
||
theorem completeKeys_filter_le_of_resolve_begin {db : DB} {r : RangeRead} {H last : Key}
|
||
(hrev : r.reverse = false)
|
||
(hbegin : resolve db r.begin = ((H : Key) : WithTop Key))
|
||
(hlt : ((last : Key) : WithTop Key) < resolve db r.end_) :
|
||
(RangeRead.completeKeys db r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db.keys.filter (fun k : Key => decide
|
||
(((H : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hbegin, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hH : ((H : Key) : WithTop Key) ≤ (k : WithTop Key)
|
||
· by_cases hL : k ≤ last
|
||
· have hR : (k : WithTop Key) < resolve db r.end_ :=
|
||
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hL) hlt
|
||
simp [hH, hL, hR]
|
||
· simp [hH, hL]
|
||
· simp [hH]
|
||
|
||
/-- Splitting the stored keys in `[E, last]` at `head`. -/
|
||
theorem filter_end_le_split_between {db : DB} {E : WithTop Key} {head last : Key}
|
||
(hEh : E ≤ (head : WithTop Key)) (hhl : head ≤ last) :
|
||
(db.keys.filter (fun (k : Key) => decide (E ≤ (k : WithTop Key)))).filter
|
||
(fun (k : Key) => decide (k ≤ last)) =
|
||
between db E ((head : Key) : WithTop Key) ++
|
||
db.keys.filter (fun (k : Key) => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr ((db.sorted.pairwise.filter _).filter _)
|
||
· rw [List.sortedLT_iff_pairwise, List.pairwise_append]
|
||
refine ⟨?_, ?_, ?_⟩
|
||
· rw [← List.sortedLT_iff_pairwise]
|
||
exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
· rw [← List.sortedLT_iff_pairwise]
|
||
exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
· intro x hx y hy
|
||
simp only [between, List.mem_filter, decide_eq_true_eq] at hx
|
||
simp only [List.mem_filter, decide_eq_true_eq] at hy
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mp hx.2.2) (WithTop.coe_le_coe.mp hy.2.1)
|
||
· intro k
|
||
rw [List.mem_filter, List.mem_filter, List.mem_append, between, List.mem_filter, List.mem_filter]
|
||
simp only [decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨⟨hkdb, hEk⟩, hklast⟩
|
||
by_cases hkh : (k : WithTop Key) < (head : WithTop Key)
|
||
· exact Or.inl ⟨hkdb, hEk, hkh⟩
|
||
· exact Or.inr ⟨hkdb, not_lt.mp hkh, hklast⟩
|
||
· rintro (⟨hkdb, hEk, hkh⟩ | ⟨hkdb, hHk, hklast⟩)
|
||
· exact ⟨⟨hkdb, hEk⟩, le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkh)) hhl⟩
|
||
· exact ⟨⟨hkdb, le_trans hEh hHk⟩, hklast⟩
|
||
|
||
/-- **Compatibility transfers across agreement on the crossed truncated begin-zero
|
||
range.** -/
|
||
theorem compatible_of_agree_zeroCrossTrunc {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hend : r.end_.offset = 1 ∨ (r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset))
|
||
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
|
||
(hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) :
|
||
RangeRead.Compatible db2 r res := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by
|
||
rw [hhead]
|
||
exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper
|
||
have hhead_upper : ((head : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le hcross' (le_max_left _ _)
|
||
have hlast_upper : ((last : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _)
|
||
have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree
|
||
have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) →
|
||
(k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by
|
||
intro k hk1 hk2
|
||
refine hmem k ?_
|
||
rw [hcr]
|
||
exact ⟨hk1, hk2⟩
|
||
have hhead_db1 : head ∈ db1.keys :=
|
||
RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne)
|
||
have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1
|
||
have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross'
|
||
have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧
|
||
(k : WithTop Key) < r.begin.key) := by
|
||
intro k hk2 hkb
|
||
have hk1 : k ∈ db1.keys :=
|
||
(hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2
|
||
exact hno1 k hk1 hkb
|
||
have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2
|
||
have hlast_mem_res : last ∈ res.keys := by
|
||
rw [hlast]; exact List.getLast_mem hne
|
||
have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r :=
|
||
(RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res
|
||
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck1).2.2
|
||
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by
|
||
rcases hend with hend | ⟨hendkey, hendoff⟩
|
||
· have hlast_db1 : last ∈ db1.keys :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck1).1
|
||
have hlastE : ((last : Key) : WithTop Key) < r.end_.key :=
|
||
lt_end_key_of_lt_resolve_end_offset_one hend hlast_db1 hlt1
|
||
have hcnt : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by
|
||
have hzero : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length = 0 := by
|
||
rw [List.length_eq_zero_iff, List.filter_eq_nil_iff]
|
||
intro k hk
|
||
have hkge : r.end_.key ≤ (k : WithTop Key) :=
|
||
decide_eq_true_eq.mp (List.mem_filter.mp hk).2
|
||
have hlastk : last < k :=
|
||
WithTop.coe_lt_coe.mp (lt_of_lt_of_le hlastE hkge)
|
||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le]
|
||
exact hlastk
|
||
rw [hzero, hend]
|
||
simp
|
||
exact resolve_end_gt_of_filter_length_le (by rw [hend]) hcnt
|
||
· rw [resolve_eq_top_of_key_eq_top hendkey hendoff]
|
||
exact WithTop.coe_lt_top last
|
||
have hres1len : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
|
||
rwa [← hlast] at h
|
||
have hck1 : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1
|
||
have hck2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2
|
||
have heq : db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
apply eq_of_forall_mem_iff_of_pairwise
|
||
· exact db1.sorted.pairwise.filter _
|
||
· exact db2.sorted.pairwise.filter _
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨hk1, hge, hle⟩
|
||
exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1,
|
||
hge, hle⟩
|
||
· rintro ⟨hk2, hge, hle⟩
|
||
exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2,
|
||
hge, hle⟩
|
||
have h2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
rw [hck2, ← heq, ← hck1, hres1len]
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨by simpa using hc1.1, h2⟩
|
||
|
||
/-- **Compatibility transfers across agreement on the crossed truncated begin-zero
|
||
range when every reported key is below `end.key` and the end offset is at least one.** -/
|
||
theorem compatible_of_agree_zeroCrossTruncBelowEnd {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hge : 1 ≤ r.end_.offset)
|
||
(hbelow : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)
|
||
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
|
||
(hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) :
|
||
RangeRead.Compatible db2 r res := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by
|
||
rw [hhead]
|
||
exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper
|
||
have hhead_upper : ((head : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le hcross' (le_max_left _ _)
|
||
have hlast_upper : ((last : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _)
|
||
have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree
|
||
have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) →
|
||
(k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by
|
||
intro k hk1 hk2
|
||
refine hmem k ?_
|
||
rw [hcr]
|
||
exact ⟨hk1, hk2⟩
|
||
have hhead_db1 : head ∈ db1.keys :=
|
||
RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne)
|
||
have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1
|
||
have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross'
|
||
have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧
|
||
(k : WithTop Key) < r.begin.key) := by
|
||
intro k hk2 hkb
|
||
have hk1 : k ∈ db1.keys :=
|
||
(hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2
|
||
exact hno1 k hk1 hkb
|
||
have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2
|
||
have hlast_mem_res : last ∈ res.keys := by
|
||
rw [hlast]; exact List.getLast_mem hne
|
||
have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r :=
|
||
(RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res
|
||
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck1).2.2
|
||
have hlastE : ((last : Key) : WithTop Key) < r.end_.key := by
|
||
rw [hlast]
|
||
exact hbelow (res.keys.getLast hne) (List.getLast_mem hne)
|
||
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ :=
|
||
lt_of_lt_of_le hlastE (key_le_resolve_of_one_le (db := db2) (s := r.end_) hge)
|
||
have hres1len : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
|
||
rwa [← hlast] at h
|
||
have hck1 : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1
|
||
have hck2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2
|
||
have heq : db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
apply eq_of_forall_mem_iff_of_pairwise
|
||
· exact db1.sorted.pairwise.filter _
|
||
· exact db2.sorted.pairwise.filter _
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨hk1, hge', hle⟩
|
||
exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1,
|
||
hge', hle⟩
|
||
· rintro ⟨hk2, hge', hle⟩
|
||
exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2,
|
||
hge', hle⟩
|
||
have h2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
rw [hck2, ← heq, ← hck1, hres1len]
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨by simpa using hc1.1, h2⟩
|
||
|
||
/-- **Compatibility transfers across agreement on the crossed truncated begin-zero
|
||
range when the end key is at or above the reported head and the end offset is at
|
||
least one.** -/
|
||
theorem compatible_of_agree_zeroCrossTruncEndGeHead {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hge : 1 ≤ r.end_.offset)
|
||
(hheadE : ((res.keys.head hne : Key) : WithTop Key) ≤ r.end_.key)
|
||
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
|
||
(hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) :
|
||
RangeRead.Compatible db2 r res := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hheadE' : ((head : Key) : WithTop Key) ≤ r.end_.key := by
|
||
rw [hhead]; exact hheadE
|
||
have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by
|
||
rw [hhead]
|
||
exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper
|
||
have hhead_upper : ((head : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le hcross' (le_max_left _ _)
|
||
have hlast_upper : ((last : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _)
|
||
have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree
|
||
have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) →
|
||
(k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by
|
||
intro k hk1 hk2
|
||
refine hmem k ?_
|
||
rw [hcr]
|
||
exact ⟨hk1, hk2⟩
|
||
have hhead_db1 : head ∈ db1.keys :=
|
||
RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne)
|
||
have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1
|
||
have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross'
|
||
have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧
|
||
(k : WithTop Key) < r.begin.key) := by
|
||
intro k hk2 hkb
|
||
have hk1 : k ∈ db1.keys :=
|
||
(hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2
|
||
exact hno1 k hk1 hkb
|
||
have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2
|
||
have hlast_mem_res : last ∈ res.keys := by
|
||
rw [hlast]; exact List.getLast_mem hne
|
||
have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r :=
|
||
(RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res
|
||
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck1).2.2
|
||
have hfiltereq : (db1.keys.filter
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
(db2.keys.filter
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last)) := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _)
|
||
· exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _)
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨⟨hk1, hEk⟩, hkle⟩
|
||
exact ⟨⟨(hmk k (le_trans hheadE' hEk)
|
||
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mp hk1, hEk⟩, hkle⟩
|
||
· rintro ⟨⟨hk2, hEk⟩, hkle⟩
|
||
exact ⟨⟨(hmk k (le_trans hheadE' hEk)
|
||
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_upper)).mpr hk2, hEk⟩, hkle⟩
|
||
have hcnt1 : ((db1.keys.filter
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat :=
|
||
filter_length_le_offset_of_resolve_end_gt (db := db1) hge hlt1
|
||
have hcnt2 : ((db2.keys.filter
|
||
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by
|
||
rw [← hfiltereq]
|
||
exact hcnt1
|
||
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ :=
|
||
resolve_end_gt_of_filter_length_le (db := db2) hge hcnt2
|
||
have hres1len : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
|
||
rwa [← hlast] at h
|
||
have hck1 : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1
|
||
have hck2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2
|
||
have heq : db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
apply eq_of_forall_mem_iff_of_pairwise
|
||
· exact db1.sorted.pairwise.filter _
|
||
· exact db2.sorted.pairwise.filter _
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨hk1, hge', hle⟩
|
||
exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1,
|
||
hge', hle⟩
|
||
· rintro ⟨hk2, hge', hle⟩
|
||
exact ⟨(hmk k hge' (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2,
|
||
hge', hle⟩
|
||
have h2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
rw [hck2, ← heq, ← hck1, hres1len]
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨by simpa using hc1.1, h2⟩
|
||
|
||
/-- **Compatibility transfers across agreement on the crossed truncated begin-zero
|
||
range when the end key is below the reported head but the gap to the head absorbs the
|
||
reported keys.** -/
|
||
theorem compatible_of_agree_zeroCrossTruncLowEndSmall {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hge : 2 ≤ r.end_.offset)
|
||
(hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key))
|
||
(hsmall : intervalCard r.end_.key (((res.keys.head hne : Key)) : WithTop Key) +
|
||
(res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞))
|
||
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
|
||
(hagree : AgreeOn (closedFormZeroCrossTrunc r res) db1 db2) :
|
||
RangeRead.Compatible db2 r res := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by
|
||
rw [hhead]; exact hEhead
|
||
have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by
|
||
rw [hhead]
|
||
exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper
|
||
have hhead_upper : ((head : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le hcross' (le_max_left _ _)
|
||
have hlast_upper : ((last : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _)
|
||
have hmem := (agreeOn_iff (closedFormZeroCrossTrunc r res) db1 db2).mp hagree
|
||
have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) →
|
||
(k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by
|
||
intro k hk1 hk2
|
||
refine hmem k ?_
|
||
rw [hcr]
|
||
exact ⟨hk1, hk2⟩
|
||
have hhead_db1 : head ∈ db1.keys :=
|
||
RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne)
|
||
have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1
|
||
have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross'
|
||
have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧
|
||
(k : WithTop Key) < r.begin.key) := by
|
||
intro k hk2 hkb
|
||
have hk1 : k ∈ db1.keys :=
|
||
(hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2
|
||
exact hno1 k hk1 hkb
|
||
have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2
|
||
have hlast_mem_res : last ∈ res.keys := by
|
||
rw [hlast]; exact List.getLast_mem hne
|
||
have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r :=
|
||
(RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res
|
||
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck1).2.2
|
||
have hpart1 : db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by
|
||
have hck1 := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1
|
||
have hres1len : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
|
||
rwa [← hlast] at h
|
||
rw [← hck1]; exact hres1len
|
||
have hpart2 : db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by
|
||
have heq : db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) =
|
||
db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr (db2.sorted.pairwise.filter _)
|
||
· exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _)
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨hk2, hHk, hklast⟩
|
||
exact ⟨(hmk k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2,
|
||
hHk, hklast⟩
|
||
· rintro ⟨hk1, hHk, hklast⟩
|
||
exact ⟨(hmk k hHk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1,
|
||
hHk, hklast⟩
|
||
rw [heq, hpart1]
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
have hsplit := filter_end_le_split_between (db := db2) (E := r.end_.key)
|
||
(head := head) (last := last) (le_of_lt hEhead') hhead_le_last
|
||
have hbetween : (((between db2 r.end_.key ((head : Key) : WithTop Key)).length : ℕ∞)) ≤
|
||
intervalCard r.end_.key ((head : Key) : WithTop Key) :=
|
||
between_length_le_intervalCard (db := db2) (le_of_lt hEhead')
|
||
have hsum : ((((between db2 r.end_.key ((head : Key) : WithTop Key)).length +
|
||
res.keys.length : ℕ)) : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by
|
||
rw [Nat.cast_add]
|
||
exact le_trans (add_le_add hbetween le_rfl) hsmall
|
||
have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by
|
||
have hlen : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length =
|
||
(between db2 r.end_.key ((head : Key) : WithTop Key)).length + res.keys.length := by
|
||
rw [hsplit, hpart2, List.length_append]
|
||
rw [hlen]
|
||
exact_mod_cast hsum
|
||
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ :=
|
||
resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2
|
||
have h2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2
|
||
rw [hck2, hpart2]
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨by simpa using hc1.1, h2⟩
|
||
|
||
/-- **Compatibility transfers across agreement on the saturated crossed truncated
|
||
begin-zero range `[end.key, max begin.key (keyAfter last))`.** Here the end gap is
|
||
explicitly tracked, so the count of stored keys in `[end.key, last]` is the same in
|
||
both databases. -/
|
||
theorem compatible_of_agree_zeroCrossTruncLowEndBig {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hge : 2 ≤ r.end_.offset)
|
||
(hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key))
|
||
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
|
||
(hagree : AgreeOn (closedFormZeroCrossTruncLowEnd r res) db1 db2) :
|
||
RangeRead.Compatible db2 r res := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by
|
||
rw [hhead]; exact hEhead
|
||
have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by
|
||
rw [hhead]
|
||
exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross
|
||
have hcr : closedFormZeroCrossTruncLowEnd r res =
|
||
⟨r.end_.key, max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTruncLowEnd
|
||
rw [hlastD]
|
||
set upper : WithTop Key := max r.begin.key (((keyAfter last : Key)) : WithTop Key) with hupper
|
||
have hE_upper : r.end_.key < upper := lt_trans hEhead' (lt_of_lt_of_le hcross' (le_max_left _ _))
|
||
have hhead_upper : ((head : Key) : WithTop Key) < upper :=
|
||
lt_of_lt_of_le hcross' (le_max_left _ _)
|
||
have hlast_upper : ((last : Key) : WithTop Key) < upper :=
|
||
lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_max_right _ _)
|
||
have hmem := (agreeOn_iff (closedFormZeroCrossTruncLowEnd r res) db1 db2).mp hagree
|
||
have hmk : ∀ k : Key, r.end_.key ≤ (k : WithTop Key) →
|
||
(k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by
|
||
intro k hk1 hk2
|
||
refine hmem k ?_
|
||
rw [hcr]
|
||
exact ⟨hk1, hk2⟩
|
||
have hhead_db1 : head ∈ db1.keys :=
|
||
RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne)
|
||
have hhead_db2 : head ∈ db2.keys :=
|
||
(hmk head (le_of_lt hEhead') hhead_upper).mp hhead_db1
|
||
have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross'
|
||
have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧
|
||
(k : WithTop Key) < r.begin.key) := by
|
||
intro k hk2 hkb
|
||
have hk1 : k ∈ db1.keys :=
|
||
(hmk k (le_trans (le_of_lt hEhead') (le_of_lt hkb.1))
|
||
(lt_of_lt_of_le hkb.2 (le_max_left _ _))).mpr hk2
|
||
exact hno1 k hk1 hkb
|
||
have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2
|
||
have hlast_mem_res : last ∈ res.keys := by
|
||
rw [hlast]; exact List.getLast_mem hne
|
||
have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r :=
|
||
(RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res
|
||
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck1).2.2
|
||
have hpart1 : db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by
|
||
have hck1 := completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1
|
||
have hres1len : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
|
||
rwa [← hlast] at h
|
||
rw [← hck1]; exact hres1len
|
||
have hpart2 : db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) = res.keys := by
|
||
have heq : db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) =
|
||
db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr (db2.sorted.pairwise.filter _)
|
||
· exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _)
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨hk2, hHk, hklast⟩
|
||
exact ⟨(hmk k (le_trans (le_of_lt hEhead') hHk)
|
||
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2,
|
||
hHk, hklast⟩
|
||
· rintro ⟨hk1, hHk, hklast⟩
|
||
exact ⟨(hmk k (le_trans (le_of_lt hEhead') hHk)
|
||
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1,
|
||
hHk, hklast⟩
|
||
rw [heq, hpart1]
|
||
have hcnt1 : ((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat :=
|
||
filter_length_le_offset_of_resolve_end_gt (db := db1) (by omega) hlt1
|
||
have hcnt_eq : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))) =
|
||
((db1.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))) := by
|
||
apply List.SortedLT.eq_of_mem_iff
|
||
· exact List.sortedLT_iff_pairwise.mpr ((db2.sorted.pairwise.filter _).filter _)
|
||
· exact List.sortedLT_iff_pairwise.mpr ((db1.sorted.pairwise.filter _).filter _)
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨⟨hk2, hEk⟩, hklast⟩
|
||
exact ⟨⟨(hmk k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mpr hk2,
|
||
hEk⟩, hklast⟩
|
||
· rintro ⟨⟨hk1, hEk⟩, hklast⟩
|
||
exact ⟨⟨(hmk k hEk (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hklast) hlast_upper)).mp hk1,
|
||
hEk⟩, hklast⟩
|
||
have hcnt2 : ((db2.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
|
||
(fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by
|
||
rw [hcnt_eq]; exact hcnt1
|
||
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ :=
|
||
resolve_end_gt_of_filter_length_le (db := db2) (by omega) hcnt2
|
||
have h2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have hck2 := completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2
|
||
rw [hck2, hpart2]
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨by simpa using hc1.1, h2⟩
|
||
|
||
/-- **The crossed truncated begin-zero closed form is minimal** when the end selector
|
||
is unbounded. -/
|
||
theorem minimal_closedForm_of_zeroCross_trunc_readAllEnd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (h : zeroCrossTruncReadAllEnd r res) :
|
||
Minimal r res (closedFormZeroCrossTrunc r res) := by
|
||
obtain ⟨hbegin, hrev, hmore, hcrossD, hend⟩ := h
|
||
have hread' : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread'
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD
|
||
exact hcrossD
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
constructor
|
||
· intro hc1
|
||
exact compatible_of_agree_zeroCrossTrunc hrev hmore hne hbegin hcross hend hc1 hagree
|
||
· intro hc2
|
||
exact compatible_of_agree_zeroCrossTrunc hrev hmore hne hbegin hcross hend hc2
|
||
(AgreeOn.symm hagree)
|
||
· intro k hklo hkhi
|
||
rw [hcr] at hklo hkhi
|
||
have hhead_nec : Necessary r res head :=
|
||
necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne)
|
||
have hlast_nec : Necessary r res last :=
|
||
necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne)
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
by_cases hkle : k ≤ last
|
||
· exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle
|
||
· -- `last < k < begin.key`
|
||
have hk_gt_last : last < k := lt_of_not_ge hkle
|
||
have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last
|
||
have hk_lt_B : (k : WithTop Key) < r.begin.key := by
|
||
by_contra hnot
|
||
have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot
|
||
by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
(((keyAfter last : Key)) : WithTop Key) := max_eq_right hc
|
||
rw [hupper] at hkhi
|
||
have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi)
|
||
exact absurd hkle' (not_le.mpr hk_gt_last)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
r.begin.key := max_eq_left (le_of_not_ge hc)
|
||
rw [hupper] at hkhi
|
||
exact absurd hBle (not_le.mpr hkhi)
|
||
have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne
|
||
hbegin hcross'
|
||
have hk_notmem : k ∉ db₀.keys := by
|
||
intro hkdb
|
||
exact hno k hkdb ⟨by exact WithTop.coe_lt_coe.mpr hhead_lt_k, hk_lt_B⟩
|
||
let db' : DB := toggle db₀ k
|
||
have hdb' : db' = db₀.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem
|
||
have hk_mem' : k ∈ db'.keys := by
|
||
rw [hdb']
|
||
exact DB.mem_insert_self db₀ k hk_notmem
|
||
have hno' : ∀ z ∈ db'.keys, ¬ (((k : Key) : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
rw [hdb', DB.insert_keys, List.mem_orderedInsert] at hz
|
||
rcases hz with hzk | hzold
|
||
· subst hzk
|
||
exact absurd hzb.1 (not_lt.mpr le_rfl)
|
||
· have hz0 : z ∈ db₀.keys := hzold
|
||
exact hno z hz0 ⟨lt_trans (WithTop.coe_lt_coe.mpr hhead_lt_k) hzb.1, hzb.2⟩
|
||
have hresb : resolve db' r.begin = ((k : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hk_mem' hk_lt_B hno'
|
||
have hnot : ¬ RangeRead.Compatible db' r res := by
|
||
intro hcomp'
|
||
have h := ((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcomp').2
|
||
have hempty : (RangeRead.completeKeys db' r).filter
|
||
(fun x : Key => decide (x ≤ last)) = [] := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hresb]
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
have hx' : ((k : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < resolve db' r.end_ :=
|
||
decide_eq_true_eq.mp (List.mem_filter.mp hx).2
|
||
have hkx : k ≤ x := WithTop.coe_le_coe.mp hx'.1
|
||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le]
|
||
exact lt_of_lt_of_le hk_gt_last hkx
|
||
rw [hempty] at h
|
||
exact hne h.symm
|
||
exact necessary_of_toggle ⟨db₀, hcomp₀, hnot⟩
|
||
|
||
/-- **The crossed truncated begin-zero closed form is minimal** when every reported key is
|
||
below `end.key` and `end.offset ≥ 1`. -/
|
||
theorem minimal_closedForm_of_zeroCross_trunc_belowEnd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (h : zeroCrossTruncBelowEnd r res) :
|
||
Minimal r res (closedFormZeroCrossTrunc r res) := by
|
||
obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hbelow⟩ := h
|
||
have hread' : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread'
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD
|
||
exact hcrossD
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
constructor
|
||
· intro hc1
|
||
exact compatible_of_agree_zeroCrossTruncBelowEnd hrev hmore hne hbegin hcross hge
|
||
hbelow hc1 hagree
|
||
· intro hc2
|
||
exact compatible_of_agree_zeroCrossTruncBelowEnd hrev hmore hne hbegin hcross hge
|
||
hbelow hc2 (AgreeOn.symm hagree)
|
||
· intro k hklo hkhi
|
||
rw [hcr] at hklo hkhi
|
||
have hhead_nec : Necessary r res head :=
|
||
necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne)
|
||
have hlast_nec : Necessary r res last :=
|
||
necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne)
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
by_cases hkle : k ≤ last
|
||
· exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle
|
||
· have hk_gt_last : last < k := lt_of_not_ge hkle
|
||
have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last
|
||
have hk_lt_B : (k : WithTop Key) < r.begin.key := by
|
||
by_contra hnot
|
||
have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot
|
||
by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
(((keyAfter last : Key)) : WithTop Key) := max_eq_right hc
|
||
rw [hupper] at hkhi
|
||
have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi)
|
||
exact absurd hkle' (not_le.mpr hk_gt_last)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
r.begin.key := max_eq_left (le_of_not_ge hc)
|
||
rw [hupper] at hkhi
|
||
exact absurd hBle (not_le.mpr hkhi)
|
||
-- insert `k` into the gap `(last, begin.key)`
|
||
have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne
|
||
hbegin hcross'
|
||
have hk_notmem : k ∉ db₀.keys := by
|
||
intro hkdb
|
||
exact hno k hkdb ⟨by exact WithTop.coe_lt_coe.mpr hhead_lt_k, hk_lt_B⟩
|
||
let db' : DB := toggle db₀ k
|
||
have hdb' : db' = db₀.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem
|
||
have hk_mem' : k ∈ db'.keys := by
|
||
rw [hdb']
|
||
exact DB.mem_insert_self db₀ k hk_notmem
|
||
have hno' : ∀ z ∈ db'.keys, ¬ (((k : Key) : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
rw [hdb', DB.insert_keys, List.mem_orderedInsert] at hz
|
||
rcases hz with hzk | hzold
|
||
· subst hzk
|
||
exact absurd hzb.1 (not_lt.mpr le_rfl)
|
||
· have hz0 : z ∈ db₀.keys := hzold
|
||
exact hno z hz0 ⟨lt_trans (WithTop.coe_lt_coe.mpr hhead_lt_k) hzb.1, hzb.2⟩
|
||
have hresb : resolve db' r.begin = ((k : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hk_mem' hk_lt_B hno'
|
||
have hnot : ¬ RangeRead.Compatible db' r res := by
|
||
intro hcomp'
|
||
have h := ((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcomp').2
|
||
have hempty : (RangeRead.completeKeys db' r).filter
|
||
(fun x : Key => decide (x ≤ last)) = [] := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hresb]
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
have hx' : ((k : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < resolve db' r.end_ :=
|
||
decide_eq_true_eq.mp (List.mem_filter.mp hx).2
|
||
have hkx : k ≤ x := WithTop.coe_le_coe.mp hx'.1
|
||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le]
|
||
exact lt_of_lt_of_le hk_gt_last hkx
|
||
rw [hempty] at h
|
||
exact hne h.symm
|
||
exact necessary_of_toggle ⟨db₀, hcomp₀, hnot⟩
|
||
|
||
/-- **Compatibility transfers across agreement on the nonpositive-end crossed
|
||
truncated begin-zero range.** -/
|
||
theorem compatible_of_agree_zeroCrossTruncNonposEnd {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hend : r.end_.offset ≤ 0)
|
||
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
|
||
(hagree : AgreeOn (closedFormZeroCrossTruncNonposEnd r res) db1 db2) :
|
||
RangeRead.Compatible db2 r res := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hres1 : resolve db1 r.begin = ((head : Key) : WithTop Key) := by
|
||
rw [hhead]
|
||
exact resolve_begin_eq_head_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross
|
||
have hcr : closedFormZeroCrossTruncNonposEnd r res =
|
||
⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by
|
||
unfold closedFormZeroCrossTruncNonposEnd
|
||
rw [hheadD]
|
||
set upper : WithTop Key := max r.begin.key r.end_.key with hupper
|
||
have hhead_upper : ((head : Key) : WithTop Key) < upper := by
|
||
rw [hupper]
|
||
exact lt_of_lt_of_le hcross' (le_max_left _ _)
|
||
have hlast_mem_res : last ∈ res.keys := by
|
||
rw [hlast]; exact List.getLast_mem hne
|
||
have hlast_ck1 : last ∈ RangeRead.completeKeys db1 r :=
|
||
(RangeRead.compatible_keys_prefix hc1).subset hlast_mem_res
|
||
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck1).2.2
|
||
have hsort := (RangeRead.compatible_sortedLT hc1 hrev).pairwise.imp le_of_lt
|
||
have hhead_le_last : head ≤ last := by
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
have hlast_upper : ((last : Key) : WithTop Key) < upper := by
|
||
have hle := resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend
|
||
exact lt_of_lt_of_le (lt_of_lt_of_le hlt1 hle) (by rw [hupper]; exact le_max_right _ _)
|
||
have hmem := (agreeOn_iff (closedFormZeroCrossTruncNonposEnd r res) db1 db2).mp hagree
|
||
have hmk : ∀ k : Key, ((head : Key) : WithTop Key) ≤ (k : WithTop Key) →
|
||
(k : WithTop Key) < upper → (k ∈ db1.keys ↔ k ∈ db2.keys) := by
|
||
intro k hk1 hk2
|
||
refine hmem k ?_
|
||
rw [hcr]
|
||
exact ⟨hk1, hk2⟩
|
||
have hhead_db1 : head ∈ db1.keys :=
|
||
RangeRead.compatible_keys_subset hc1 (by rw [hhead]; exact List.head_mem hne)
|
||
have hhead_db2 : head ∈ db2.keys := (hmk head le_rfl hhead_upper).mp hhead_db1
|
||
have hno1 := no_stored_between_head_begin_of_zero_crossed_more hc1 hrev hmore hne hbegin hcross'
|
||
have hno2 : ∀ k ∈ db2.keys, ¬ (((head : Key) : WithTop Key) < (k : WithTop Key) ∧
|
||
(k : WithTop Key) < r.begin.key) := by
|
||
intro k hk2 hkb
|
||
have hk1 : k ∈ db1.keys :=
|
||
(hmk k (le_of_lt hkb.1) (lt_of_lt_of_le hkb.2 (by rw [hupper]; exact le_max_left _ _))).mpr hk2
|
||
exact hno1 k hk1 hkb
|
||
have hres2 : resolve db2 r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_db2 hcross' hno2
|
||
have hne_top : resolve db1 r.end_ ≠ (⊤ : WithTop Key) :=
|
||
resolve_ne_top_of_offset_nonpos hend
|
||
obtain ⟨q, hq⟩ := WithTop.ne_top_iff_exists.mp hne_top
|
||
have hlast_q : last < q := by
|
||
have h := hlt1
|
||
rw [← hq] at h
|
||
exact WithTop.coe_lt_coe.mp h
|
||
have hqne : q ≠ ([] : Key) := by
|
||
intro hnil
|
||
have h : last < ([] : Key) := by rwa [hnil] at hlast_q
|
||
exact absurd h (List.not_lt_nil last)
|
||
have hq_le_E : ((q : Key) : WithTop Key) ≤ r.end_.key := by
|
||
rw [hq]
|
||
exact resolve_le_key_of_offset_nonpos (db := db1) (s := r.end_) hend
|
||
have hhead_q : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hhead_le_last hlast_q))
|
||
have hEnd_upper : r.end_.key ≤ upper := by
|
||
rw [hupper]; exact le_max_right _ _
|
||
have hagreeEnd : db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) := by
|
||
apply eq_of_forall_mem_iff_of_pairwise
|
||
· exact db1.sorted.pairwise.filter _
|
||
· exact db2.sorted.pairwise.filter _
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨hk1, hge, hlt⟩
|
||
exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mp hk1, hge, hlt⟩
|
||
· rintro ⟨hk2, hge, hlt⟩
|
||
exact ⟨(hmk k hge (lt_of_lt_of_le hlt hEnd_upper)).mpr hk2, hge, hlt⟩
|
||
have hqget := resolve_filter_reverse_eq_some_of_ne_nil (db := db1) (s := r.end_) hend hqne hq.symm
|
||
have hresEnd2 : resolve db2 r.end_ = ((q : Key) : WithTop Key) :=
|
||
resolve_eq_of_agree_between (db₁ := db1) (db₂ := db2) (s := r.end_) hend
|
||
(E := r.end_.key) rfl (le_trans hhead_q hq_le_E) hagreeEnd hqget hhead_q
|
||
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ := by
|
||
rw [hresEnd2]
|
||
exact WithTop.coe_lt_coe.mpr hlast_q
|
||
have hres1len : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
|
||
rwa [← hlast] at h
|
||
have hck1 : (RangeRead.completeKeys db1 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db1) hrev hres1 hlt1
|
||
have hck2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) :=
|
||
completeKeys_filter_le_of_resolve_begin (db := db2) hrev hres2 hlt2
|
||
have heq : db1.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) =
|
||
db2.keys.filter (fun k : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (k : WithTop Key) ∧ (k : Key) ≤ last)) := by
|
||
apply eq_of_forall_mem_iff_of_pairwise
|
||
· exact db1.sorted.pairwise.filter _
|
||
· exact db2.sorted.pairwise.filter _
|
||
· intro k
|
||
simp only [List.mem_filter, decide_eq_true_eq]
|
||
constructor
|
||
· rintro ⟨hk1, hge, hle⟩
|
||
exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mp hk1,
|
||
hge, hle⟩
|
||
· rintro ⟨hk2, hge, hle⟩
|
||
exact ⟨(hmk k hge (lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlast_upper)).mpr hk2,
|
||
hge, hle⟩
|
||
have h2 : (RangeRead.completeKeys db2 r).filter
|
||
(fun k : Key => decide (k ≤ last)) = res.keys := by
|
||
rw [hck2, ← heq, ← hck1, hres1len]
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨by simpa using hc1.1, h2⟩
|
||
|
||
/-- A key strictly between the reported last and `begin.key` is necessary in a crossed
|
||
truncated begin-zero read. -/
|
||
theorem necessary_zeroCross_trunc_gap {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hkgt : res.keys.getLast hne < k) (hkB : (k : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
have hhead_le_last : head ≤ last := by
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hkgt
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne
|
||
hbegin hcross'
|
||
have hk_notmem : k ∉ db₀.keys := by
|
||
intro hkdb
|
||
exact hno k hkdb ⟨WithTop.coe_lt_coe.mpr hhead_lt_k, hkB⟩
|
||
let db' : DB := toggle db₀ k
|
||
have hdb' : db' = db₀.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem
|
||
have hk_mem' : k ∈ db'.keys := by
|
||
rw [hdb']
|
||
exact DB.mem_insert_self db₀ k hk_notmem
|
||
have hno' : ∀ z ∈ db'.keys, ¬ (((k : Key) : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
rw [hdb', DB.insert_keys, List.mem_orderedInsert] at hz
|
||
rcases hz with hzk | hzold
|
||
· subst hzk
|
||
exact absurd hzb.1 (not_lt.mpr le_rfl)
|
||
· have hz0 : z ∈ db₀.keys := hzold
|
||
exact hno z hz0 ⟨lt_trans (WithTop.coe_lt_coe.mpr hhead_lt_k) hzb.1, hzb.2⟩
|
||
have hresb : resolve db' r.begin = ((k : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hk_mem' hkB hno'
|
||
have hnot : ¬ RangeRead.Compatible db' r res := by
|
||
intro hcomp'
|
||
have h := ((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcomp').2
|
||
have hempty : (RangeRead.completeKeys db' r).filter
|
||
(fun x : Key => decide (x ≤ last)) = [] := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hresb]
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
have hx' : ((k : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < resolve db' r.end_ :=
|
||
decide_eq_true_eq.mp (List.mem_filter.mp hx).2
|
||
have hkx : k ≤ x := WithTop.coe_le_coe.mp hx'.1
|
||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_le]
|
||
exact lt_of_lt_of_le hkgt hkx
|
||
rw [hempty] at h
|
||
exact hne h.symm
|
||
exact necessary_of_toggle ⟨db₀, hcomp₀, hnot⟩
|
||
|
||
/-- **The crossed truncated begin-zero closed form is minimal** when the end key is at
|
||
or above the reported head and `end.offset ≥ 1`. -/
|
||
theorem minimal_closedForm_of_zeroCross_trunc_endGeHead {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (h : zeroCrossTruncEndGeHead r res) :
|
||
Minimal r res (closedFormZeroCrossTrunc r res) := by
|
||
obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hheadE⟩ := h
|
||
have hread' : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread'
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD
|
||
exact hcrossD
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hheadE' : ((head : Key) : WithTop Key) ≤ r.end_.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, hheadD] at hheadE
|
||
exact hheadE
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
constructor
|
||
· intro hc1
|
||
exact compatible_of_agree_zeroCrossTruncEndGeHead hrev hmore hne hbegin hcross hge
|
||
hheadE' hc1 hagree
|
||
· intro hc2
|
||
exact compatible_of_agree_zeroCrossTruncEndGeHead hrev hmore hne hbegin hcross hge
|
||
hheadE' hc2 (AgreeOn.symm hagree)
|
||
· intro k hklo hkhi
|
||
rw [hcr] at hklo hkhi
|
||
have hhead_nec : Necessary r res head :=
|
||
necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne)
|
||
have hlast_nec : Necessary r res last :=
|
||
necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne)
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
by_cases hkle : k ≤ last
|
||
· exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle
|
||
· have hk_gt_last : last < k := lt_of_not_ge hkle
|
||
have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last
|
||
have hk_lt_B : (k : WithTop Key) < r.begin.key := by
|
||
by_contra hnot
|
||
have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot
|
||
by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
(((keyAfter last : Key)) : WithTop Key) := max_eq_right hc
|
||
rw [hupper] at hkhi
|
||
have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi)
|
||
exact absurd hkle' (not_le.mpr hk_gt_last)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
r.begin.key := max_eq_left (le_of_not_ge hc)
|
||
rw [hupper] at hkhi
|
||
exact absurd hBle (not_le.mpr hkhi)
|
||
exact necessary_zeroCross_trunc_gap hread hrev hmore hne hbegin hcross hk_gt_last hk_lt_B
|
||
|
||
/-- A bounded gap with more keys than the end offset can absorb makes every key in
|
||
`[end.key, head)` necessary: filling the gap to the offset and then adding the target
|
||
overflows the end resolution. -/
|
||
theorem necessary_zeroCross_trunc_between_end_head {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hge : 2 ≤ r.end_.offset)
|
||
(hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key))
|
||
(hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) <
|
||
intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) +
|
||
(res.keys.length : ℕ∞))
|
||
(hkE : r.end_.key ≤ (k : WithTop Key))
|
||
(hkH : (k : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
set e1 : ℕ := (r.end_.offset - 1).toNat with he1
|
||
set rl : ℕ := res.keys.length with hrl
|
||
set n : ℕ := e1 - rl with hn
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by rw [hhead]; exact hcross
|
||
have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by rw [hhead]; exact hEhead
|
||
have hkH' : (k : WithTop Key) < ((head : Key) : WithTop Key) := by rw [hhead]; exact hkH
|
||
have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hres_pair : res.keys.Pairwise (· ≤ ·) := hres_sorted.pairwise.imp le_of_lt
|
||
have hhead_le : ∀ x ∈ res.keys, head ≤ x := by
|
||
intro x hx
|
||
have := hres_pair.rel_head hx
|
||
simpa [hhead] using this
|
||
have hle_last : ∀ x ∈ res.keys, x ≤ last := by
|
||
intro x hx
|
||
have := hres_pair.rel_getLast hx
|
||
simpa [hlast] using this
|
||
have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne
|
||
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
|
||
have hhead_le_last : head ≤ last := hhead_le last hlast_mem
|
||
have hno := no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne hbegin hcross'
|
||
-- The reported length is bounded by the end offset.
|
||
have hlast_ck0 : last ∈ RangeRead.completeKeys db₀ r :=
|
||
(RangeRead.compatible_keys_prefix hcomp₀).subset hlast_mem
|
||
have hlt0 : ((last : Key) : WithTop Key) < resolve db₀ r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck0).2.2
|
||
have hcnt0 : ((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
|
||
(fun x : Key => decide (x ≤ last))).length ≤ e1 := by
|
||
simpa [he1] using filter_length_le_offset_of_resolve_end_gt (db := db₀) (by omega) hlt0
|
||
have hres_sub : res.keys ⊆
|
||
((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
|
||
(fun x : Key => decide (x ≤ last))) := by
|
||
intro x hx
|
||
have hxdb : x ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hx
|
||
have hxE : r.end_.key ≤ (x : WithTop Key) :=
|
||
le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx))
|
||
have hxl : x ≤ last := hle_last x hx
|
||
rw [List.mem_filter, List.mem_filter]
|
||
exact ⟨⟨hxdb, by simpa using hxE⟩, by simpa using hxl⟩
|
||
have hrle : rl ≤ e1 := by
|
||
have := hres_sorted.nodup.length_le_of_subset hres_sub
|
||
exact le_trans (by simpa [hrl] using this) hcnt0
|
||
-- Capacity in the gap to build the witness.
|
||
have hbig' : (e1 : ℕ∞) < intervalCard r.end_.key ((head : Key) : WithTop Key) +
|
||
(rl : ℕ∞) := by
|
||
simpa [he1, hrl, hhead] using hbig
|
||
have hcard : ((n + 1 : ℕ) : ℕ∞) ≤
|
||
intervalCard r.end_.key ((head : Key) : WithTop Key) := by
|
||
by_cases htop : intervalCard r.end_.key ((head : Key) : WithTop Key) = (⊤ : ℕ∞)
|
||
· rw [htop]; exact le_top
|
||
· obtain ⟨G, hG⟩ := ENat.ne_top_iff_exists.mp htop
|
||
have hlt : e1 < G + rl := by
|
||
have hb := hbig'
|
||
rw [← hG] at hb
|
||
rw [← ENat.coe_add] at hb
|
||
exact ENat.coe_lt_coe.mp hb
|
||
have hGle : n + 1 ≤ G := by
|
||
rw [hn]; omega
|
||
rw [← hG]
|
||
exact ENat.coe_le_coe.mpr hGle
|
||
obtain ⟨dbK, hKrange, hkK, hKlen⟩ :=
|
||
exists_db_keysIn_avoid (B := r.end_.key) (E := ((head : Key) : WithTop Key))
|
||
(k := k) (m := n) hcard
|
||
have hK_ge : ∀ x ∈ dbK.keys, r.end_.key ≤ (x : WithTop Key) :=
|
||
fun x hx => (hKrange x hx).1
|
||
have hK_lt : ∀ x ∈ dbK.keys, x < head :=
|
||
fun x hx => WithTop.coe_lt_coe.mp (hKrange x hx).2
|
||
-- The witness database.
|
||
have hsort_append : (dbK.keys ++ res.keys).SortedLT := by
|
||
refine List.sortedLT_append.mpr ⟨dbK.sorted, hres_sorted, ?_⟩
|
||
intro a ha b hb
|
||
exact lt_of_lt_of_le (hK_lt a ha) (hhead_le b hb)
|
||
let dbw : DB := ⟨dbK.keys ++ res.keys, hsort_append⟩
|
||
have hdbw : dbw.keys = dbK.keys ++ res.keys := rfl
|
||
have hk_notmem : k ∉ dbw.keys := by
|
||
rw [hdbw, List.mem_append]
|
||
rintro (hkK' | hkres)
|
||
· exact hkK hkK'
|
||
· exact absurd (hhead_le k hkres) (not_le.mpr (WithTop.coe_lt_coe.mp hkH'))
|
||
have hhead_dbw : head ∈ dbw.keys := by
|
||
rw [hdbw]; exact List.mem_append.mpr (Or.inr hhead_mem)
|
||
have hno_dbw : ∀ z ∈ dbw.keys, ¬ (((head : Key) : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
rw [hdbw, List.mem_append] at hz
|
||
rcases hz with hz | hz
|
||
· exact absurd hzb.1 (not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_lt (hK_lt z hz))))
|
||
· exact hno z (RangeRead.compatible_keys_subset hcomp₀ hz) hzb
|
||
have hresb : resolve dbw r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_dbw hcross' hno_dbw
|
||
have hmem_all_E : ∀ x ∈ dbw.keys, r.end_.key ≤ (x : WithTop Key) := by
|
||
intro x hx
|
||
rw [hdbw, List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· exact hK_ge x hx
|
||
· exact le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx))
|
||
have hcntE : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hmem_all_E x hx)
|
||
have hdbw_len : dbw.keys.length = e1 := by
|
||
rw [hdbw, List.length_append, hKlen, ← hrl, hn]
|
||
exact Nat.sub_add_cancel hrle
|
||
have hanchor : (r.end_.anchor dbw).toNat = e1 := by
|
||
rw [anchor_toNat_eq (db := dbw) (s := r.end_) (by omega), hcntE, ← he1]
|
||
omega
|
||
have hrestop : resolve dbw r.end_ = (⊤ : WithTop Key) := by
|
||
apply resolve_eq_top
|
||
· exact anchor_nonneg_of_one_le (db := dbw) (s := r.end_) (by omega)
|
||
· rw [hanchor, hdbw_len]
|
||
have hck : RangeRead.completeKeys dbw r = res.keys := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hresb, hrestop]
|
||
have hpred : (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < (⊤ : WithTop Key))) =
|
||
(fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) := by
|
||
funext x; simp
|
||
rw [hpred, hdbw, List.filter_append]
|
||
have hleft : dbK.keys.filter
|
||
(fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_le.mpr (WithTop.coe_lt_coe.mpr (hK_lt x hx))
|
||
have hright : res.keys.filter
|
||
(fun x : Key => decide (((head : Key) : WithTop Key) ≤ (x : WithTop Key))) = res.keys := by
|
||
apply List.filter_eq_self.mpr
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_le_coe.mpr (hhead_le x hx)
|
||
rw [hleft, hright, List.nil_append]
|
||
have hfilter_le : res.keys.filter (fun x : Key => decide (x ≤ last)) = res.keys := by
|
||
apply List.filter_eq_self.mpr
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hle_last x hx
|
||
have hcompw : RangeRead.Compatible dbw r res := by
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨by simpa using hcomp₀.1, by rw [hck, hfilter_le]⟩
|
||
-- Toggling the target on overflows the end offset.
|
||
have hdb' : toggle dbw k = dbw.insert k hk_notmem := toggle_eq_insert_of_not_mem hk_notmem
|
||
have hkeys_toggle : (toggle dbw k).keys = dbw.keys.orderedInsert (· ≤ ·) k := by
|
||
rw [hdb', DB.insert_keys]
|
||
have hlen_toggle : (toggle dbw k).keys.length = e1 + 1 := by
|
||
rw [hkeys_toggle, List.orderedInsert_length, hdbw_len]
|
||
have hmem_all : ∀ x ∈ (toggle dbw k).keys,
|
||
r.end_.key ≤ (x : WithTop Key) ∧ x ≤ last := by
|
||
intro x hx
|
||
rw [hkeys_toggle, List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact ⟨hkE, le_trans (le_of_lt (WithTop.coe_lt_coe.mp hkH')) hhead_le_last⟩
|
||
· rw [hdbw, List.mem_append] at hx
|
||
rcases hx with hx | hx
|
||
· exact ⟨hK_ge x hx, le_trans (le_of_lt (hK_lt x hx)) hhead_le_last⟩
|
||
· exact ⟨le_trans (le_of_lt hEhead') (WithTop.coe_le_coe.mpr (hhead_le x hx)),
|
||
hle_last x hx⟩
|
||
have hcount_toggle :
|
||
((toggle dbw k).keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
|
||
(fun x : Key => decide (x ≤ last)) = (toggle dbw k).keys := by
|
||
have h1 : (toggle dbw k).keys.filter
|
||
(fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) = (toggle dbw k).keys := by
|
||
apply List.filter_eq_self.mpr
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact (hmem_all x hx).1
|
||
rw [h1]
|
||
apply List.filter_eq_self.mpr
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact (hmem_all x hx).2
|
||
have hnot : ¬ RangeRead.Compatible (toggle dbw k) r res := by
|
||
intro hc
|
||
have hlast_ck : last ∈ RangeRead.completeKeys (toggle dbw k) r :=
|
||
(RangeRead.compatible_keys_prefix hc).subset hlast_mem
|
||
have hlt : ((last : Key) : WithTop Key) < resolve (toggle dbw k) r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hlast_ck).2.2
|
||
have hcnt := filter_length_le_offset_of_resolve_end_gt (db := toggle dbw k) (by omega) hlt
|
||
rw [hcount_toggle, hlen_toggle, ← he1] at hcnt
|
||
omega
|
||
exact necessary_of_toggle ⟨dbw, hcompw, hnot⟩
|
||
|
||
/-- **The crossed truncated begin-zero closed form is minimal** when the end gap is
|
||
saturated: the whole gap `[end.key, head)` is necessary, so the lower endpoint drops to
|
||
`end.key`. -/
|
||
theorem minimal_closedForm_of_zeroCross_trunc_lowEndBig {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (h : zeroCrossTruncLowEndBig r res) :
|
||
Minimal r res (closedFormZeroCrossTruncLowEnd r res) := by
|
||
obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hEheadD, hbigD⟩ := h
|
||
have hread' : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread'
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD
|
||
exact hcrossD
|
||
have hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hEheadD
|
||
exact hEheadD
|
||
have hbig : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) <
|
||
intervalCard r.end_.key ((res.keys.head hne : Key) : WithTop Key) +
|
||
(res.keys.length : ℕ∞) := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hbigD
|
||
exact hbigD
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by
|
||
rw [hhead]; exact hEhead
|
||
have hbig' : (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) <
|
||
intervalCard r.end_.key ((head : Key) : WithTop Key) + (res.keys.length : ℕ∞) := by
|
||
simpa [hhead] using hbig
|
||
have hcr : closedFormZeroCrossTruncLowEnd r res =
|
||
⟨r.end_.key,
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTruncLowEnd
|
||
rw [hlastD]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
constructor
|
||
· intro hc1
|
||
exact compatible_of_agree_zeroCrossTruncLowEndBig hrev hmore hne hbegin hcross hge
|
||
hEhead' hc1 hagree
|
||
· intro hc2
|
||
exact compatible_of_agree_zeroCrossTruncLowEndBig hrev hmore hne hbegin hcross hge
|
||
hEhead' hc2 (AgreeOn.symm hagree)
|
||
· intro k hklo hkhi
|
||
rw [hcr] at hklo hkhi
|
||
have hhead_nec : Necessary r res head :=
|
||
necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne)
|
||
have hlast_nec : Necessary r res last :=
|
||
necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne)
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
by_cases hkle : k ≤ last
|
||
· by_cases hkhead : k < head
|
||
· exact necessary_zeroCross_trunc_between_end_head hread hrev hmore hne hbegin hcross hge
|
||
hEhead' hbig' hklo (WithTop.coe_lt_coe.mpr hkhead)
|
||
· exact Necessary.of_between hhead_nec hlast_nec (le_of_not_gt hkhead) hkle
|
||
· have hk_gt_last : last < k := lt_of_not_ge hkle
|
||
have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last
|
||
have hk_lt_B : (k : WithTop Key) < r.begin.key := by
|
||
by_contra hnot
|
||
have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot
|
||
by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
(((keyAfter last : Key)) : WithTop Key) := max_eq_right hc
|
||
rw [hupper] at hkhi
|
||
have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi)
|
||
exact absurd hkle' (not_le.mpr hk_gt_last)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
r.begin.key := max_eq_left (le_of_not_ge hc)
|
||
rw [hupper] at hkhi
|
||
exact absurd hBle (not_le.mpr hkhi)
|
||
exact necessary_zeroCross_trunc_gap hread hrev hmore hne hbegin hcross hk_gt_last hk_lt_B
|
||
|
||
/-- **The crossed truncated begin-zero closed form is minimal** when the end key is
|
||
below the reported head but the gap to the head absorbs the reported keys. -/
|
||
theorem minimal_closedForm_of_zeroCross_trunc_lowEndSmall {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (h : zeroCrossTruncLowEndSmall r res) :
|
||
Minimal r res (closedFormZeroCrossTrunc r res) := by
|
||
obtain ⟨hbegin, hrev, hmore, hcrossD, hge, hEheadD, hsmallD⟩ := h
|
||
have hread' : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread'
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD
|
||
exact hcrossD
|
||
have hEhead : r.end_.key < ((res.keys.head hne : Key) : WithTop Key) := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hEheadD
|
||
exact hEheadD
|
||
have hsmall : intervalCard r.end_.key (((res.keys.head hne : Key)) : WithTop Key) +
|
||
(res.keys.length : ℕ∞) ≤ (((r.end_.offset - 1).toNat : ℕ) : ℕ∞) := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hsmallD
|
||
exact hsmallD
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||
rw [hlast, getLastD_eq_getLast_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hEhead' : r.end_.key < ((head : Key) : WithTop Key) := by
|
||
rw [hhead]; exact hEhead
|
||
have hcr : closedFormZeroCrossTrunc r res =
|
||
⟨((head : Key) : WithTop Key),
|
||
max r.begin.key (((keyAfter last : Key)) : WithTop Key)⟩ := by
|
||
unfold closedFormZeroCrossTrunc
|
||
rw [hheadD, hlastD]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
constructor
|
||
· intro hc1
|
||
exact compatible_of_agree_zeroCrossTruncLowEndSmall hrev hmore hne hbegin hcross hge
|
||
hEhead' hsmall hc1 hagree
|
||
· intro hc2
|
||
exact compatible_of_agree_zeroCrossTruncLowEndSmall hrev hmore hne hbegin hcross hge
|
||
hEhead' hsmall hc2 (AgreeOn.symm hagree)
|
||
· intro k hklo hkhi
|
||
rw [hcr] at hklo hkhi
|
||
have hhead_nec : Necessary r res head :=
|
||
necessary_of_mem_result hread (by rw [hhead]; exact List.head_mem hne)
|
||
have hlast_nec : Necessary r res last :=
|
||
necessary_of_mem_result hread (by rw [hlast]; exact List.getLast_mem hne)
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
by_cases hkle : k ≤ last
|
||
· exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle
|
||
· have hk_gt_last : last < k := lt_of_not_ge hkle
|
||
have hhead_lt_k : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last
|
||
have hk_lt_B : (k : WithTop Key) < r.begin.key := by
|
||
by_contra hnot
|
||
have hBle : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hnot
|
||
by_cases hc : r.begin.key ≤ (((keyAfter last : Key)) : WithTop Key)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
(((keyAfter last : Key)) : WithTop Key) := max_eq_right hc
|
||
rw [hupper] at hkhi
|
||
have hkle' : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hkhi)
|
||
exact absurd hkle' (not_le.mpr hk_gt_last)
|
||
· have hupper : max r.begin.key (((keyAfter last : Key)) : WithTop Key) =
|
||
r.begin.key := max_eq_left (le_of_not_ge hc)
|
||
rw [hupper] at hkhi
|
||
exact absurd hBle (not_le.mpr hkhi)
|
||
exact necessary_zeroCross_trunc_gap hread hrev hmore hne hbegin hcross hk_gt_last hk_lt_B
|
||
|
||
/-- A key at or above `begin.key` and below `end.key` is necessary in the crossed
|
||
truncated begin-zero read with `end.offset = 0`. -/
|
||
theorem necessary_zeroCross_trunc_above_begin {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hend : r.end_.offset = 0)
|
||
(hkgt : res.keys.getLast hne < k)
|
||
(hkE : (k : WithTop Key) < r.end_.key)
|
||
(hkge : r.begin.key ≤ (k : WithTop Key)) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hk_notmem : k ∉ res.keys := by
|
||
intro hk
|
||
exact absurd (hle_last k hk) (not_le.mpr hkgt)
|
||
have hsortD : (res.keys ++ [k]).SortedLT := by
|
||
refine List.sortedLT_append.mpr ⟨hsort, ?_, ?_⟩
|
||
· rw [List.sortedLT_iff_pairwise]
|
||
exact List.pairwise_singleton _ k
|
||
intro a ha b hb
|
||
simp only [List.mem_singleton] at hb
|
||
subst hb
|
||
exact lt_of_le_of_lt (hle_last a ha) hkgt
|
||
let dbw : DB := ⟨res.keys ++ [k], hsortD⟩
|
||
have hdbw : dbw.keys = res.keys ++ [k] := rfl
|
||
have hk_mem : k ∈ dbw.keys := by rw [hdbw]; simp
|
||
have hhead_mem : head ∈ dbw.keys := by
|
||
rw [hdbw, hhead]
|
||
exact List.mem_append.mpr (Or.inl (List.head_mem hne))
|
||
have hno : ∀ z ∈ dbw.keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
rw [hdbw] at hz
|
||
rcases List.mem_append.mp hz with hz | hz
|
||
· have hz0 : z ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hz
|
||
exact no_stored_between_head_begin_of_zero_crossed_more hcomp₀ hrev hmore hne hbegin hcross'
|
||
z hz0 hzb
|
||
· simp only [List.mem_singleton] at hz
|
||
subst hz
|
||
exact absurd hkge (not_le.mpr hzb.2)
|
||
have hresb : resolve dbw r.begin = (↑head : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_mem hcross' hno
|
||
have hDlt : ∀ x ∈ dbw.keys, (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hdbw] at hx
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx))
|
||
(lt_trans (WithTop.coe_lt_coe.mpr hkgt) hkE)
|
||
· simp only [List.mem_singleton] at hx
|
||
subst hx
|
||
exact hkE
|
||
have hcount_end : dbw.keys.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
|
||
dbw.keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hDlt x hx
|
||
have hlen : dbw.keys.length = res.keys.length + 1 := by
|
||
rw [hdbw, List.length_append]
|
||
simp
|
||
have hanchor : r.end_.anchor dbw = (res.keys.length : Int) := by
|
||
cases hE : r.end_.key with
|
||
| top =>
|
||
rw [anchor_of_top (s := r.end_) hE, hlen]
|
||
push_cast
|
||
rw [hend]
|
||
ring
|
||
| coe e =>
|
||
have hce : dbw.keys.countP (fun x : Key => decide (x < e)) = dbw.keys.length := by
|
||
rw [← countP_lt_coe (l := dbw.keys) e, ← hE]
|
||
exact hcount_end
|
||
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlen]
|
||
push_cast
|
||
rw [hend]
|
||
ring
|
||
have hget : dbw.keys[res.keys.length]? = some k := by
|
||
rw [hdbw, List.getElem?_append]
|
||
rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self]
|
||
simp
|
||
have hre : resolve dbw r.end_ = ((k : Key) : WithTop Key) := by
|
||
rw [resolve, ite_eq_right (by rw [hanchor]; omega)]
|
||
rw [hanchor, Int.toNat_natCast, hget]
|
||
have hltk : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by
|
||
rw [hre]
|
||
exact WithTop.coe_lt_coe.mpr hkgt
|
||
have hckw : (RangeRead.completeKeys dbw r).filter
|
||
(fun x : Key => decide (x ≤ last)) = res.keys := by
|
||
rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hresb hltk]
|
||
rw [hdbw, List.filter_append]
|
||
have hres_fil : res.keys.filter (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact ⟨WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx), hle_last x hx⟩
|
||
have hk_fil : [k].filter (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [List.mem_singleton] at hx
|
||
subst hx
|
||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and]
|
||
exact fun _ => not_le.mpr hkgt
|
||
rw [hres_fil, hk_fil, List.append_nil]
|
||
have hcompw : RangeRead.Compatible dbw r res := by
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨hcomp₀.1, hckw⟩
|
||
have hD' : (dbw.erase k).keys = res.keys := by
|
||
rw [DB.erase_keys, hdbw, List.erase_append_right [k] hk_notmem]
|
||
simp
|
||
have hsort' : (dbw.erase k).keys.SortedLT := (dbw.erase k).sorted
|
||
have hcount_end' : (dbw.erase k).keys.countP
|
||
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [hD'] at hx
|
||
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx))
|
||
(lt_trans (WithTop.coe_lt_coe.mpr hkgt) hkE)
|
||
have hlen' : (dbw.erase k).keys.length = res.keys.length := by rw [hD']
|
||
have hanchor' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by
|
||
cases hE : r.end_.key with
|
||
| top =>
|
||
rw [anchor_of_top (s := r.end_) hE, hlen']
|
||
push_cast
|
||
rw [hend]
|
||
ring
|
||
| coe e =>
|
||
have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) =
|
||
(dbw.erase k).keys.length := by
|
||
rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE]
|
||
exact hcount_end'
|
||
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlen']
|
||
push_cast
|
||
rw [hend]
|
||
ring
|
||
have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne
|
||
have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by
|
||
rw [Nat.cast_sub ht1]
|
||
norm_num
|
||
have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by
|
||
rw [hanchor', hEqInt, Int.toNat_natCast]
|
||
have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by
|
||
rw [hD', List.getElem?_eq_getElem (by omega)]
|
||
congr 1
|
||
rw [hlast, List.getLast_eq_getElem hne]
|
||
have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by
|
||
rw [resolve, ite_eq_right (by rw [hanchor']; omega)]
|
||
rw [hanchor', hEqInt, Int.toNat_natCast, hD'get?]
|
||
have hresb' : resolve (dbw.erase k) r.begin = (↑head : WithTop Key) := by
|
||
have hhead_mem' : head ∈ (dbw.erase k).keys := by
|
||
rw [hD', hhead]
|
||
exact List.head_mem hne
|
||
have hno' : ∀ z ∈ (dbw.erase k).keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
rw [hD'] at hz
|
||
exact hno z (by rw [hdbw]; exact List.mem_append.mpr (Or.inl hz)) hzb
|
||
exact resolve_begin_eq_of_no_between_zero hbegin hhead_mem' hcross' hno'
|
||
have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hresb', hre']
|
||
have hpred : ∀ x ∈ (dbw.erase k).keys,
|
||
decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < ((↑last : Key) : WithTop Key)) =
|
||
decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ x < last) := by
|
||
intro x _
|
||
rw [decide_eq_decide]
|
||
exact and_congr_right fun _ => WithTop.coe_lt_coe
|
||
rw [List.filter_congr hpred, hD']
|
||
have hhead_le_all : ∀ x ∈ res.keys, ((head : Key) : WithTop Key) ≤ (x : WithTop Key) := by
|
||
intro x hx
|
||
exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx)
|
||
rw [show res.keys.filter (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) =
|
||
res.keys.filter (fun x : Key => decide (x < last)) from by
|
||
apply List.filter_congr
|
||
intro x hx
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨hhead_le_all x hx, h⟩⟩]
|
||
conv_lhs => rw [← List.dropLast_append_getLast hne, hlast]
|
||
rw [List.filter_append]
|
||
have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
have h := hsort.pairwise.rel_dropLast_getLast hx
|
||
simpa [hlast] using h
|
||
rw [hfil]
|
||
simp
|
||
have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
intro h
|
||
have heq := h.2
|
||
rw [hckE] at heq
|
||
have hfil : res.keys.dropLast.filter
|
||
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [← hlast]
|
||
exact hle_last x (List.mem_of_mem_dropLast hx)
|
||
rw [hfil] at heq
|
||
have hlen := congrArg List.length heq
|
||
rw [List.length_dropLast] at hlen
|
||
omega
|
||
exact necessary_of_toggle ⟨dbw, hcompw, by
|
||
rw [toggle_eq_erase_of_mem hk_mem]
|
||
exact hnot⟩
|
||
|
||
/-- For a crossed truncated begin-zero read with a nonpositive end offset, a key in
|
||
`[begin.key, end_.key)` above the reported last has a toggle witness. The witness adds
|
||
the `(1 - end.offset)` filler keys needed to land the end resolution just above the
|
||
reported last. -/
|
||
theorem exists_witness_zeroCross_trunc_nonposEnd {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hend : r.end_.offset ≤ 0)
|
||
(hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key)
|
||
(hkge : r.begin.key ≤ (k : WithTop Key)) :
|
||
∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧
|
||
¬ RangeRead.Compatible (dbw.erase k) r res := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
|
||
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
|
||
(hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hhead_le_last : head ≤ last := by
|
||
simp only [hhead, hlast]
|
||
exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hlast_ck : last ∈ RangeRead.completeKeys db r :=
|
||
(RangeRead.compatible_keys_prefix hcomp).subset (List.getLast_mem hne)
|
||
obtain ⟨hlast_mem, -, hlast_res⟩ := RangeRead.mem_completeKeys.mp hlast_ck
|
||
have hlastE : ((last : Key) : WithTop Key) < r.end_.key :=
|
||
lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res
|
||
have hfill := filler_count_nonpos_more_nonpos_end hcomp hrev hmore hne (le_of_eq hbegin) hend
|
||
set m := (1 - r.end_.offset).toNat with hm
|
||
have hm1 : 1 ≤ m := by rw [hm]; omega
|
||
set G : List Key := db.keys.filter
|
||
(fun x : Key => decide (last < x ∧ (x : WithTop Key) < r.end_.key)) with hG
|
||
have hGlen : m ≤ G.length := hfill
|
||
have hGnodup : G.Nodup := by
|
||
rw [hG]; exact db.sorted.pairwise.nodup.filter _
|
||
have hGmem : ∀ x ∈ G, last < x ∧ (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hG] at hx
|
||
exact of_decide_eq_true (List.mem_filter.mp hx).2
|
||
set raw : List Key := (k :: G.erase k).take m with hraw
|
||
have hrawNodup : raw.Nodup := by
|
||
rw [hraw]
|
||
refine (List.take_sublist m (k :: G.erase k)).nodup ?_
|
||
exact List.nodup_cons.mpr ⟨hGnodup.not_mem_erase, hGnodup.erase k⟩
|
||
have hrawLen : raw.length = m := by
|
||
rw [hraw, List.length_take]
|
||
apply Nat.min_eq_left
|
||
by_cases hkG : k ∈ G
|
||
· rw [List.length_cons, List.length_erase_of_mem hkG]; omega
|
||
· rw [List.length_cons, List.erase_of_not_mem hkG]; omega
|
||
have hrawSub : ∀ x ∈ raw, x = k ∨ x ∈ G := by
|
||
intro x hx
|
||
rw [hraw] at hx
|
||
rcases List.mem_cons.mp (List.mem_of_mem_take hx) with rfl | hx'
|
||
· exact Or.inl rfl
|
||
· exact Or.inr (List.mem_of_mem_erase hx')
|
||
have hrawk : k ∈ raw := by
|
||
rw [hraw, List.mem_take_iff_getElem]
|
||
refine ⟨0, ?_, rfl⟩
|
||
rw [List.length_cons]
|
||
omega
|
||
have hrawGt : ∀ x ∈ raw, last < x := by
|
||
intro x hx
|
||
rcases hrawSub x hx with rfl | hx'
|
||
· exact hkgt
|
||
· exact (hGmem x hx').1
|
||
have hrawLt : ∀ x ∈ raw, (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rcases hrawSub x hx with rfl | hx'
|
||
· exact hkE
|
||
· exact (hGmem x hx').2
|
||
set F : List Key := raw.insertionSort (· ≤ ·) with hF
|
||
have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm
|
||
have hFsorted : F.SortedLT := by
|
||
rw [hF, List.sortedLT_iff_pairwise]
|
||
exact DB.pairwise_lt_of_le_of_nodup
|
||
(List.pairwise_insertionSort (r := (· ≤ ·)) raw)
|
||
(hFperm.nodup_iff.mp hrawNodup)
|
||
have hFlen : F.length = m := by rw [hF, List.length_insertionSort, hrawLen]
|
||
have hFk : k ∈ F := hFperm.mem_iff.mp hrawk
|
||
have hFmem : ∀ x ∈ F, last < x ∧ (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
exact ⟨hrawGt x (hFperm.mem_iff.mpr hx), hrawLt x (hFperm.mem_iff.mpr hx)⟩
|
||
have hFne : F ≠ [] := by
|
||
intro h; rw [h] at hFlen; simp at hFlen; omega
|
||
set D : List Key := res.keys ++ F with hD
|
||
have hDsort : D.SortedLT := by
|
||
rw [hD]
|
||
refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩
|
||
intro a ha b hb
|
||
exact lt_of_le_of_lt (hle_last a ha) (hFmem b hb).1
|
||
let dbw : DB := ⟨D, hDsort⟩
|
||
have hdbw_keys : dbw.keys = D := rfl
|
||
-- the begin selector resolves to the reported head
|
||
have hhead_mem : head ∈ dbw.keys := by
|
||
rw [hdbw_keys, hD, hhead]
|
||
exact List.mem_append.mpr (Or.inl (List.head_mem hne))
|
||
have hno : ∀ z ∈ dbw.keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
rw [hdbw_keys, hD] at hz
|
||
rcases List.mem_append.mp hz with hz | hz
|
||
· have hz0 : z ∈ db.keys := RangeRead.compatible_keys_subset hcomp hz
|
||
exact no_stored_between_head_begin_of_zero_crossed_more hcomp hrev hmore hne hbegin hcross'
|
||
z hz0 hzb
|
||
· have hzraw : z ∈ raw := hFperm.mem_iff.mpr hz
|
||
have hzsub : z ∈ G := by
|
||
rw [hraw] at hzraw
|
||
rcases List.mem_cons.mp (List.mem_of_mem_take hzraw) with hzk | hzG
|
||
· subst hzk
|
||
exact absurd hzb.2 (not_lt.mpr hkge)
|
||
· exact List.mem_of_mem_erase hzG
|
||
have hz0 : z ∈ db.keys := by
|
||
rw [hG] at hzsub
|
||
exact (List.mem_filter.mp hzsub).1
|
||
exact no_stored_between_head_begin_of_zero_crossed_more hcomp hrev hmore hne hbegin hcross'
|
||
z hz0 ⟨lt_of_le_of_lt (WithTop.coe_le_coe.mpr hhead_le_last)
|
||
(WithTop.coe_lt_coe.mpr (hGmem z hzsub).1), hzb.2⟩
|
||
have hrb : resolve dbw r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_mem hcross' hno
|
||
-- every stored key is below the end key
|
||
have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hD] at hx
|
||
rcases List.mem_append.mp hx with hx | hx
|
||
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE
|
||
· exact (hFmem x hx).2
|
||
have hlenD : D.length = res.keys.length + m := by
|
||
rw [hD, List.length_append, hFlen]
|
||
have hm_int : (m : Int) = 1 - r.end_.offset := by
|
||
rw [hm]; exact Int.toNat_of_nonneg (by omega)
|
||
have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
|
||
D.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hDlt x hx
|
||
have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by
|
||
cases hE : r.end_.key with
|
||
| top =>
|
||
rw [anchor_of_top (s := r.end_) hE, hlenD]
|
||
push_cast
|
||
rw [hm_int]
|
||
ring
|
||
| coe e =>
|
||
have hce : D.countP (fun x : Key => decide (x < e)) = D.length := by
|
||
rw [← countP_lt_coe (l := D) e, ← hE]
|
||
exact hcount_end
|
||
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD]
|
||
push_cast
|
||
rw [hm_int]
|
||
ring
|
||
have ht_lt : res.keys.length < D.length := by rw [hlenD]; omega
|
||
have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by
|
||
change D[res.keys.length]? = some (F.head hFne)
|
||
rw [hD, List.getElem?_append]
|
||
rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self]
|
||
rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne]
|
||
have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by
|
||
rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)]
|
||
rw [hanchor_end, Int.toNat_natCast, hDget?]
|
||
have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by
|
||
rw [hre]
|
||
exact WithTop.coe_lt_coe.mpr (hFmem (F.head hFne) (List.head_mem hFne)).1
|
||
-- complete keys of the witness are the reported keys
|
||
have hckw : (RangeRead.completeKeys dbw r).filter
|
||
(fun x : Key => decide (x ≤ last)) = res.keys := by
|
||
rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF]
|
||
rw [hdbw_keys, hD, List.filter_append]
|
||
have hres_fil : res.keys.filter (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact ⟨WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx), hle_last x hx⟩
|
||
have hF_fil : F.filter (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and]
|
||
exact fun _ => not_le.mpr (hFmem x hx).1
|
||
rw [hres_fil, hF_fil, List.append_nil]
|
||
have hcompw : RangeRead.Compatible dbw r res := by
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
exact ⟨hcomp.1, hckw⟩
|
||
have hkdbw : k ∈ dbw.keys := by
|
||
rw [hdbw_keys, hD]
|
||
exact List.mem_append.mpr (Or.inr hFk)
|
||
-- now the erased database
|
||
have hk_not_mem_res : k ∉ res.keys := by
|
||
intro hk
|
||
exact absurd (hle_last k hk) (not_le.mpr hkgt)
|
||
have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by
|
||
rw [DB.erase_keys, hdbw_keys, hD]
|
||
exact List.erase_append_right F hk_not_mem_res
|
||
have hhead_mem' : head ∈ (dbw.erase k).keys := by
|
||
rw [hD', hhead]
|
||
exact List.mem_append.mpr (Or.inl (List.head_mem hne))
|
||
have hno' : ∀ z ∈ (dbw.erase k).keys, ¬ ((↑head : WithTop Key) < (z : WithTop Key) ∧
|
||
(z : WithTop Key) < r.begin.key) := by
|
||
intro z hz hzb
|
||
exact hno z (by
|
||
rw [DB.erase_keys, hdbw_keys] at hz
|
||
exact List.mem_of_mem_erase hz) hzb
|
||
have hrb' : resolve (dbw.erase k) r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_of_no_between_zero hbegin hhead_mem' hcross' hno'
|
||
have hD'lt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
exact hDlt x (by
|
||
rw [DB.erase_keys, hdbw_keys] at hx
|
||
exact List.mem_of_mem_erase hx)
|
||
have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by
|
||
rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen]
|
||
omega
|
||
have hcount_end' : (dbw.erase k).keys.countP
|
||
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hD'lt x hx
|
||
have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by
|
||
cases hE : r.end_.key with
|
||
| top =>
|
||
rw [anchor_of_top (s := r.end_) hE, hlenD']
|
||
rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)]
|
||
push_cast
|
||
rw [hm_int]
|
||
ring
|
||
| coe e =>
|
||
have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) =
|
||
(dbw.erase k).keys.length := by
|
||
rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE]
|
||
exact hcount_end'
|
||
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD']
|
||
rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)]
|
||
push_cast
|
||
rw [hm_int]
|
||
ring
|
||
have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne
|
||
have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by
|
||
rw [Nat.cast_sub ht1]; norm_num
|
||
have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by
|
||
rw [hanchor_end', hEqInt, Int.toNat_natCast]
|
||
have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by
|
||
rw [hD', List.getElem?_append]
|
||
rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)]
|
||
rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)]
|
||
congr 1
|
||
rw [hlast, List.getLast_eq_getElem hne]
|
||
have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by
|
||
rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)]
|
||
rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?]
|
||
have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), hrb', hre']
|
||
have hpred : ∀ x ∈ (dbw.erase k).keys,
|
||
decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < ((↑last : Key) : WithTop Key)) =
|
||
decide (((↑head : WithTop Key)) ≤ (x : WithTop Key) ∧ x < last) := by
|
||
intro x _
|
||
rw [decide_eq_decide]
|
||
exact and_congr_right fun _ => WithTop.coe_lt_coe
|
||
rw [List.filter_congr hpred, hD', List.filter_append]
|
||
have hhead_le_all : ∀ x ∈ res.keys, ((head : Key) : WithTop Key) ≤ (x : WithTop Key) := by
|
||
intro x hx
|
||
exact WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hx)
|
||
rw [show res.keys.filter (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) =
|
||
res.keys.filter (fun x : Key => decide (x < last)) from by
|
||
apply List.filter_congr
|
||
intro x hx
|
||
rw [decide_eq_decide]
|
||
exact ⟨fun h => h.2, fun h => ⟨hhead_le_all x hx, h⟩⟩]
|
||
conv_lhs => rw [← List.dropLast_append_getLast hne, hlast]
|
||
rw [List.filter_append]
|
||
have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
have h := hsort.pairwise.rel_dropLast_getLast hx
|
||
simpa [hlast] using h
|
||
rw [hfil]
|
||
have hFnil : (F.erase k).filter (fun x : Key => decide
|
||
(((head : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ x < last)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and]
|
||
intro _
|
||
exact not_lt.mpr (le_of_lt (hFmem x (List.mem_of_mem_erase hx)).1)
|
||
rw [hFnil, List.append_nil]
|
||
simp
|
||
have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by
|
||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||
intro h
|
||
have heq := h.2
|
||
rw [hckE] at heq
|
||
have hfil : res.keys.dropLast.filter
|
||
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [← hlast]
|
||
exact hle_last x (List.mem_of_mem_dropLast hx)
|
||
rw [hfil] at heq
|
||
have hlen := congrArg List.length heq
|
||
rw [List.length_dropLast] at hlen
|
||
omega
|
||
exact ⟨dbw, hcompw, hkdbw, hnot⟩
|
||
|
||
/-- A key at or above `begin.key` and below `end.key` is necessary in a crossed
|
||
truncated begin-zero read with a nonpositive end offset. -/
|
||
theorem necessary_zeroCross_trunc_above_begin_nonposEnd {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
|
||
(hne : res.keys ≠ []) (hbegin : r.begin.offset = 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hend : r.end_.offset ≤ 0)
|
||
(hkgt : res.keys.getLast hne < k)
|
||
(hkE : (k : WithTop Key) < r.end_.key)
|
||
(hkge : r.begin.key ≤ (k : WithTop Key)) :
|
||
Necessary r res k := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
obtain ⟨dbw, hcompw, hkdbw, hnot⟩ :=
|
||
exists_witness_zeroCross_trunc_nonposEnd ⟨db, hcomp⟩ hrev hmore hne hbegin hcross hend
|
||
hkgt hkE hkge
|
||
exact necessary_of_toggle ⟨dbw, hcompw, by
|
||
rw [toggle_eq_erase_of_mem hkdbw]
|
||
exact hnot⟩
|
||
|
||
/-- **The crossed truncated begin-zero closed form is minimal** when `end.offset = 0`. -/
|
||
theorem minimal_closedForm_of_zeroCross_trunc_zeroEnd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (h : zeroCrossTruncZeroEnd r res) :
|
||
Minimal r res (closedFormZeroCrossTruncNonposEnd r res) := by
|
||
obtain ⟨hbegin, hrev, hmore, hcrossD, hend⟩ := h
|
||
have hread' : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD
|
||
exact hcrossD
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hcr : closedFormZeroCrossTruncNonposEnd r res =
|
||
⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by
|
||
unfold closedFormZeroCrossTruncNonposEnd
|
||
rw [hheadD]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
constructor
|
||
· intro hc1
|
||
exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross
|
||
(le_of_eq hend) hc1 hagree
|
||
· intro hc2
|
||
exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross
|
||
(le_of_eq hend) hc2 (AgreeOn.symm hagree)
|
||
· intro k hklo hkhi
|
||
rw [hcr] at hklo hkhi
|
||
have hhead_nec : Necessary r res head :=
|
||
necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne)
|
||
have hlast_nec : Necessary r res last :=
|
||
necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne)
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
by_cases hkle : k ≤ last
|
||
· exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle
|
||
· have hkgt : last < k := lt_of_not_ge hkle
|
||
by_cases hkB : (k : WithTop Key) < r.begin.key
|
||
· exact necessary_zeroCross_trunc_gap hread' hrev hmore hne hbegin hcross hkgt hkB
|
||
· have hkge : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hkB
|
||
by_cases hleE : r.begin.key ≤ r.end_.key
|
||
· have hmax : max r.begin.key r.end_.key = r.end_.key := max_eq_right hleE
|
||
have hkE : (k : WithTop Key) < r.end_.key := by rw [← hmax]; exact hkhi
|
||
exact necessary_zeroCross_trunc_above_begin hread' hrev hmore hne hbegin hcross hend
|
||
(by rw [← hlast]; exact hkgt) hkE hkge
|
||
· exfalso
|
||
have hltE : r.end_.key < r.begin.key := lt_of_not_ge hleE
|
||
have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hltE)
|
||
rw [hmax] at hkhi
|
||
exact absurd hkhi (not_lt.mpr hkge)
|
||
|
||
/-- **The crossed truncated begin-zero closed form is minimal** for every nonpositive
|
||
end offset. -/
|
||
theorem minimal_closedForm_of_zeroCross_trunc_nonposEnd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (h : zeroCrossTruncNonposEnd r res) :
|
||
Minimal r res (closedFormZeroCrossTruncNonposEnd r res) := by
|
||
obtain ⟨hbegin, hrev, hmore, hcrossD, hend⟩ := h
|
||
have hread' : Readable r res := hread
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hne : res.keys ≠ [] := RangeRead.compatible_ne_nil_of_more hcomp₀ hmore
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
have hleast : leastReported r res = res.keys.headD ([] : Key) := by
|
||
simp [leastReported, hrev]
|
||
rw [hleast, headD_eq_head_zero_cross_trunc hne] at hcrossD
|
||
exact hcrossD
|
||
set head : Key := res.keys.head hne with hhead
|
||
set last : Key := res.keys.getLast hne with hlast
|
||
have hheadD : res.keys.headD ([] : Key) = head := by
|
||
rw [hhead, headD_eq_head_zero_cross_trunc hne]
|
||
have hcross' : ((head : Key) : WithTop Key) < r.begin.key := by
|
||
rw [hhead]; exact hcross
|
||
have hcr : closedFormZeroCrossTruncNonposEnd r res =
|
||
⟨((head : Key) : WithTop Key), max r.begin.key r.end_.key⟩ := by
|
||
unfold closedFormZeroCrossTruncNonposEnd
|
||
rw [hheadD]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· intro db1 db2 hagree
|
||
constructor
|
||
· intro hc1
|
||
exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross
|
||
hend hc1 hagree
|
||
· intro hc2
|
||
exact compatible_of_agree_zeroCrossTruncNonposEnd hrev hmore hne hbegin hcross
|
||
hend hc2 (AgreeOn.symm hagree)
|
||
· intro k hklo hkhi
|
||
rw [hcr] at hklo hkhi
|
||
have hhead_nec : Necessary r res head :=
|
||
necessary_of_mem_result hread' (by rw [hhead]; exact List.head_mem hne)
|
||
have hlast_nec : Necessary r res last :=
|
||
necessary_of_mem_result hread' (by rw [hlast]; exact List.getLast_mem hne)
|
||
have hhead_le_last : head ≤ last := by
|
||
have hsort := (RangeRead.compatible_sortedLT hcomp₀ hrev).pairwise.imp le_of_lt
|
||
simp only [hhead, hlast]
|
||
exact hsort.rel_getLast (List.head_mem hne)
|
||
by_cases hkle : k ≤ last
|
||
· exact Necessary.of_between hhead_nec hlast_nec (WithTop.coe_le_coe.mp hklo) hkle
|
||
· have hkgt : last < k := lt_of_not_ge hkle
|
||
by_cases hkB : (k : WithTop Key) < r.begin.key
|
||
· exact necessary_zeroCross_trunc_gap hread' hrev hmore hne hbegin hcross hkgt hkB
|
||
· have hkge : r.begin.key ≤ (k : WithTop Key) := le_of_not_gt hkB
|
||
by_cases hleE : r.begin.key ≤ r.end_.key
|
||
· have hmax : max r.begin.key r.end_.key = r.end_.key := max_eq_right hleE
|
||
have hkE : (k : WithTop Key) < r.end_.key := by rw [← hmax]; exact hkhi
|
||
exact necessary_zeroCross_trunc_above_begin_nonposEnd hread' hrev hmore hne hbegin
|
||
hcross hend (by rw [← hlast]; exact hkgt) hkE hkge
|
||
· exfalso
|
||
have hltE : r.end_.key < r.begin.key := lt_of_not_ge hleE
|
||
have hmax : max r.begin.key r.end_.key = r.begin.key := max_eq_left (le_of_lt hltE)
|
||
rw [hmax] at hkhi
|
||
exact absurd hkhi (not_lt.mpr hkge)
|
||
|
||
end FormalConflictRange
|