- Update deprecated lemmas (ite_eq_left/right, ENat casts, List.Sublist, ...) - Replace push_neg with push Not and resolve unused-variable warnings - Convert flexible simp/simp_all calls to explicit simp only lists - Remove unscoped maxHeartbeats options, open scoped Classical, and unused tactics - Add missing module docstrings and clean up style linter hits - Wrap over-long lines and flatten the deeply nested minimal_conflictRangeClosedForm_total proof `lake build` now completes with zero warnings.
2494 lines
130 KiB
Lean4
2494 lines
130 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.NonposClosedForm
|
||
import FormalConflictRange.NegCrossClosedForm
|
||
|
||
/-!
|
||
# Crossed negative-begin complete cells
|
||
|
||
At a negative begin offset that is *nonclamping* (`beginSlack r < reportedBelowBegin r res`),
|
||
a compatible complete forward database resolves its begin selector exactly onto the reported
|
||
head, independently of the end selector. This module records that resolution and the induced
|
||
count identity `#stored in [head, begin.key) = beginSlack r + 1`, and uses them to close the
|
||
`end.offset ≤ 0` cell: the minimal range is `[head, end_.key)`.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
/-- Counting keys below a `WithTop` bound splits at a genuine key `a ≤ B` into those
|
||
below `a` and those in `[a, B)`. -/
|
||
theorem countP_lt_eq_add_filter_withTop (l : List Key) (hnd : l.Nodup) {a : Key}
|
||
{B : WithTop Key} (haB : (a : WithTop Key) ≤ B) :
|
||
l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < B)) =
|
||
l.countP (fun k : Key => decide (k < a)) +
|
||
(l.filter (fun k : Key => decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < B))).length := by
|
||
cases B with
|
||
| top =>
|
||
have htop : l.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (⊤ : WithTop Key))) =
|
||
l.length := countP_lt_top
|
||
have hfilter : l.filter (fun k : Key => decide ((a : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < (⊤ : WithTop Key))) =
|
||
l.filter (fun k : Key => decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key))) := by
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
exact and_iff_left (WithTop.coe_lt_top k)
|
||
have hlen := List.length_eq_length_filter_add (l := l)
|
||
(f := fun k : Key => decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key)))
|
||
have hcompl : (l.filter (fun k : Key => !decide ((a : WithTop Key) ≤
|
||
((k : Key) : WithTop Key)))).length = l.countP (fun k : Key => decide (k < a)) := by
|
||
rw [List.countP_eq_length_filter]
|
||
congr 1
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [show (!decide ((a : WithTop Key) ≤ ((k : Key) : WithTop Key))) =
|
||
decide (¬ ((a : WithTop Key) ≤ ((k : Key) : WithTop Key))) by rw [decide_not]]
|
||
rw [decide_eq_decide]
|
||
rw [WithTop.coe_le_coe]
|
||
exact not_le
|
||
rw [htop, hfilter, ← hcompl]
|
||
omega
|
||
| coe ke =>
|
||
have hab : a ≤ ke := WithTop.coe_le_coe.mp haB
|
||
have h := countP_lt_eq_add_filter l hnd hab
|
||
have hcoe : l.countP (fun k : Key => decide (((k : Key) : WithTop Key) <
|
||
((ke : Key) : WithTop Key))) = l.countP (fun k : Key => decide (k < ke)) :=
|
||
countP_lt_coe ke
|
||
have hf : (l.filter (fun k : Key => decide ((a : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < ((ke : Key) : WithTop
|
||
Key)))).length =
|
||
(l.filter (fun x : Key => decide (a ≤ x ∧ x < ke))).length := by
|
||
congr 1
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [decide_eq_decide]
|
||
simp only [WithTop.coe_le_coe, WithTop.coe_lt_coe]
|
||
rw [hcoe, h, hf]
|
||
|
||
/-- **The begin resolution in the nonclamping negative-offset complete cell.** When the
|
||
reported keys below `begin.key` outnumber the begin slack, every compatible complete
|
||
forward database resolves its begin selector exactly onto the reported head, whatever
|
||
the end selector is. -/
|
||
theorem resolve_begin_eq_head_of_negCross_compat' {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res) :
|
||
resolve db r.begin = ((res.keys.head hne : Key) : WithTop Key) := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))
|
||
with hcB
|
||
have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev
|
||
have hmin_le : reportedBelowBegin r res ≤ cB := List.Sublist.countP_le hsub
|
||
have hcB_ge : beginSlack r + 1 ≤ cB :=
|
||
Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le)
|
||
have hanchor_ge : 0 ≤ r.begin.anchor db := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
|
||
have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge
|
||
omega
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
|
||
have hhead_ck : head ∈ RangeRead.completeKeys db r := by
|
||
rw [← hres_eq]; exact hhead_mem
|
||
have hle_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) :=
|
||
(RangeRead.mem_completeKeys.mp hhead_ck).2.1
|
||
have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hhead_ck).2.2
|
||
have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
|
||
intro htop
|
||
rw [htop] at hle_head
|
||
exact absurd hle_head (not_le.mpr (WithTop.coe_lt_top head))
|
||
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
|
||
by_contra h
|
||
exact hnot_top (resolve_eq_top hanchor_ge (le_of_not_gt h))
|
||
have hresolve_get : resolve db r.begin =
|
||
((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) :=
|
||
resolve_eq_getElem hanchor_ge hltlen
|
||
set k : Key := db.keys[(r.begin.anchor db).toNat] with hk
|
||
have hk_stored : k ∈ db.keys := List.getElem_mem hltlen
|
||
have hk_le_head : k ≤ head := WithTop.coe_le_coe.mp (by rw [← hresolve_get]; exact hle_head)
|
||
have hk_le : resolve db r.begin ≤ ((k : Key) : WithTop Key) := by rw [hresolve_get]
|
||
have hk_lt : ((k : Key) : WithTop Key) < resolve db r.end_ := by
|
||
calc ((k : Key) : WithTop Key) = resolve db r.begin := hresolve_get.symm
|
||
_ ≤ ((head : Key) : WithTop Key) := hle_head
|
||
_ < resolve db r.end_ := hhead_lt_res
|
||
have hk_ck : k ∈ RangeRead.completeKeys db r :=
|
||
RangeRead.mem_completeKeys.mpr ⟨hk_stored, hk_le, hk_lt⟩
|
||
have hk_mem_res : k ∈ res.keys := by rw [hres_eq]; exact hk_ck
|
||
have hk_ge_head : head ≤ k :=
|
||
(RangeRead.compatible_sortedLT hcomp hrev).pairwise.imp le_of_lt |>.rel_head hk_mem_res
|
||
have hk_eq : k = head := le_antisymm hk_le_head hk_ge_head
|
||
rw [← hk_eq]
|
||
exact hresolve_get
|
||
|
||
/-- **The begin count identity in the nonclamping negative-offset complete cell.** Exactly
|
||
`beginSlack r + 1` stored keys lie in `[head, begin.key)`. -/
|
||
theorem negCross_filter_begin_length {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res) :
|
||
(db.keys.filter (fun k : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by
|
||
set head : Key := res.keys.head hne with hhead
|
||
set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))
|
||
with hcB
|
||
have hsub : List.Sublist res.keys db.keys := result_sublist_db_of_compatible hcomp hrev
|
||
have hmin_le : reportedBelowBegin r res ≤ cB := List.Sublist.countP_le hsub
|
||
have hcB_ge : beginSlack r + 1 ≤ cB :=
|
||
Nat.succ_le_of_lt (lt_of_lt_of_le hnonclamp hmin_le)
|
||
have hL := resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hanchor_ge : 0 ≤ r.begin.anchor db := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
|
||
have : (beginSlack r : ℤ) + 1 ≤ (cB : ℤ) := by exact_mod_cast hcB_ge
|
||
omega
|
||
have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
|
||
intro htop; rw [hL] at htop
|
||
exact absurd htop (WithTop.coe_ne_top (a := head))
|
||
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
|
||
by_contra h
|
||
exact hnot_top (resolve_eq_top hanchor_ge (le_of_not_gt h))
|
||
have hget := resolve_eq_getElem (db := db) (s := r.begin) hanchor_ge hltlen
|
||
have hidx : db.keys[(r.begin.anchor db).toNat] = head := by
|
||
have hcoe : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) =
|
||
((head : Key) : WithTop Key) := by rw [← hget, hL]
|
||
exact WithTop.coe_injective hcoe
|
||
have hrank : db.keys.countP (fun k => decide (k < head)) = (r.begin.anchor db).toNat :=
|
||
countP_lt_eq_of_getElem db.sorted hltlen hidx
|
||
have hanchor_cast : r.begin.anchor db = (cB : ℤ) - (beginSlack r : ℤ) - 1 := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB]
|
||
have htoNat : (r.begin.anchor db).toNat = cB - (beginSlack r + 1) := by
|
||
have hz : r.begin.anchor db = ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) := by
|
||
rw [hanchor_cast]; push_cast; ring
|
||
have hz2 : ((cB : ℤ) - ((beginSlack r + 1 : ℕ) : ℤ)) =
|
||
((cB - (beginSlack r + 1) : ℕ) : ℤ) := by
|
||
rw [← Nat.cast_sub hcB_ge]
|
||
rw [hz, hz2, Int.toNat_natCast]
|
||
have hcH_add : (db.keys.countP (fun k => decide (k < head))) + (beginSlack r + 1) = cB := by
|
||
rw [hrank, htoNat]
|
||
exact Nat.sub_add_cancel hcB_ge
|
||
have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup
|
||
(a := head) (B := r.begin.key) (le_of_lt hcross)
|
||
have hsplit' : cB = (db.keys.countP (fun k => decide (k < head))) +
|
||
(db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length := by
|
||
rw [hcB]; exact hsplit
|
||
omega
|
||
|
||
/-- **The begin resolution from the interval count.** If exactly `beginSlack r + 1`
|
||
stored keys lie in `[head, begin.key)` and the head itself is stored above the crossed
|
||
threshold, then the begin selector resolves exactly onto the head. Unlike
|
||
`resolve_begin_eq_head_of_negCross_compat'`, this needs no compatibility hypothesis, so
|
||
it transfers across databases agreeing on the interval. -/
|
||
theorem resolve_begin_eq_head_of_filter_len {db : DB} {r : RangeRead} {head : Key}
|
||
(hbegin : r.begin.offset < 0) (hhead : head ∈ db.keys)
|
||
(hcross : ((head : Key) : WithTop Key) < r.begin.key)
|
||
(hlen : (db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1) :
|
||
resolve db r.begin = ((head : Key) : WithTop Key) := by
|
||
set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))
|
||
with hcB
|
||
set cH := db.keys.countP (fun k : Key => decide (k < head)) with hcH
|
||
have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup
|
||
(a := head) (B := r.begin.key) (le_of_lt hcross)
|
||
rw [hlen] at hsplit
|
||
have hsplit' : cB = cH + (beginSlack r + 1) := by
|
||
rw [← hcB, ← hcH] at hsplit
|
||
exact hsplit
|
||
have hanchor : r.begin.anchor db = (cH : ℤ) := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), ← hcB, hsplit']
|
||
push_cast
|
||
ring
|
||
have hanchor_nonneg : 0 ≤ r.begin.anchor db := by rw [hanchor]; exact Int.natCast_nonneg _
|
||
set n := (r.begin.anchor db).toNat with hn
|
||
have htoNat : n = cH := by rw [hn, hanchor, Int.toNat_natCast]
|
||
have hlt : n < db.keys.length := by
|
||
rw [htoNat, hcH]
|
||
obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hhead
|
||
rw [countP_lt_eq_of_getElem db.sorted hi hix]
|
||
exact hi
|
||
have hkey : db.keys[n] = head := by
|
||
simp only [htoNat, hcH]
|
||
exact getElem_countP_lt db.sorted (x := head) hhead
|
||
rw [show resolve db r.begin = ((db.keys[n] : Key) : WithTop Key) by
|
||
rw [resolve_eq_getElem hanchor_nonneg (hn ▸ hlt)]]
|
||
rw [hkey]
|
||
|
||
/-- **The crossed threshold stays at or below the end key** in the nonclamping
|
||
negative-offset complete cell with a nonpositive end offset. Counting the reported keys
|
||
below `begin.key` from both the begin and the end resolution rules rules out
|
||
`end_.key < begin.key`. -/
|
||
theorem negCross_begin_le_end {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
|
||
(hnonclamp : beginSlack r < reportedBelowBegin r res)
|
||
(hend : r.end_.offset ≤ 0) :
|
||
r.begin.key ≤ r.end_.key := by
|
||
by_contra hcon
|
||
have hEB : r.end_.key < r.begin.key := lt_of_not_ge hcon
|
||
set head : Key := res.keys.head hne with hhead
|
||
set q := beginSlack r with hq
|
||
set e := (-r.end_.offset).toNat with he
|
||
set cH := db.keys.countP (fun k : Key => decide (k < head)) with hcH
|
||
set cE := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))
|
||
with hcE
|
||
set cB := db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))
|
||
with hcB
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
|
||
have hhead_ck := RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)
|
||
have hhead_stored : head ∈ db.keys := hhead_ck.1
|
||
have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db r.end_ := hhead_ck.2.2
|
||
have hL := resolve_begin_eq_head_of_negCross_compat' hcomp hrev hmore hne hbegin hnonclamp
|
||
have hlenB := negCross_filter_begin_length hcomp hrev hmore hne hbegin hcross hnonclamp
|
||
-- The number of stored keys below `begin.key` splits at the head.
|
||
have hsplitB := countP_lt_eq_add_filter_withTop db.keys db.nodup
|
||
(a := head) (B := r.begin.key) (le_of_lt hcross)
|
||
rw [hlenB] at hsplitB
|
||
have hcB_eq : cB = cH + (q + 1) := by
|
||
rw [← hcB, ← hcH, ← hq] at hsplitB
|
||
exact hsplitB
|
||
-- The end resolution is the `e`-th key from the top below `end_.key`.
|
||
have he_cast : (e : ℤ) = -r.end_.offset := by
|
||
rw [he]; exact Int.toNat_of_nonneg (by omega)
|
||
have hanchorE : r.end_.anchor db = (cE : ℤ) - (e : ℤ) - 1 := by
|
||
unfold Selector.anchor
|
||
rw [← hcE]
|
||
omega
|
||
have hres_not_nil : resolve db r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||
intro hnil
|
||
rw [hnil] at hhead_lt_res
|
||
exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _))
|
||
have hanchorE_nonneg : 0 ≤ r.end_.anchor db := by
|
||
by_contra hneg
|
||
exact hres_not_nil (resolve_eq_nil (lt_of_not_ge hneg))
|
||
have hres_not_top : resolve db r.end_ ≠ (⊤ : WithTop Key) :=
|
||
resolve_ne_top_of_offset_nonpos hend
|
||
have hltE : (r.end_.anchor db).toNat < db.keys.length := by
|
||
by_contra hge
|
||
exact hres_not_top (resolve_eq_top hanchorE_nonneg (le_of_not_gt hge))
|
||
have htoNatE : (r.end_.anchor db).toNat = cE - e - 1 := by
|
||
have h1 : e + 1 ≤ cE := by
|
||
by_contra hlt
|
||
have hec : cE ≤ e := by omega
|
||
have hanc : r.end_.anchor db < 0 := by
|
||
rw [hanchorE]; omega
|
||
exact hres_not_nil (resolve_eq_nil hanc)
|
||
have hz : r.end_.anchor db = ((cE - e - 1 : ℕ) : ℤ) := by
|
||
rw [hanchorE]
|
||
have hsub : cE - e - 1 = cE - (e + 1) := by omega
|
||
rw [hsub, Nat.cast_sub h1]
|
||
push_cast
|
||
ring
|
||
rw [hz, Int.toNat_natCast]
|
||
have hcountE : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) <
|
||
resolve db r.end_)) = cE - e - 1 := by
|
||
rw [countP_lt_resolve, htoNatE]
|
||
have hltE' : cE - e - 1 < db.keys.length := by rw [← htoNatE]; exact hltE
|
||
exact Nat.min_eq_left (le_of_lt hltE')
|
||
-- The reported keys are exactly the stored keys in `[head, resolve end)`.
|
||
have hfilter_eq : db.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys := by
|
||
rw [hres_eq]
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev])]
|
||
rw [hL]
|
||
have hsplitE := countP_lt_eq_add_filter_withTop db.keys db.nodup
|
||
(a := head) (B := resolve db r.end_) (le_of_lt hhead_lt_res)
|
||
rw [hfilter_eq, hcountE, ← hcH] at hsplitE
|
||
have hlenRes : res.keys.length = (cE - e - 1) - cH := by omega
|
||
-- Under `end_.key < begin.key` every reported key lies below `begin.key`.
|
||
have hall_lt_B : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.begin.key := by
|
||
intro k hk
|
||
have hk_lt_res : ((k : Key) : WithTop Key) < resolve db r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hk)).2.2
|
||
have hres_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
exact lt_trans (lt_of_lt_of_le hk_lt_res hres_le) hEB
|
||
have hreported : reportedBelowBegin r res = res.keys.length := by
|
||
unfold reportedBelowBegin
|
||
rw [List.countP_eq_length_filter]
|
||
congr 1
|
||
apply List.filter_eq_self.mpr
|
||
intro k hk
|
||
simpa using hall_lt_B k hk
|
||
have hstrict : q < res.keys.length := by
|
||
rw [← hreported]
|
||
exact hnonclamp
|
||
have hcE_le : cE ≤ cB := by
|
||
rw [hcE, hcB]
|
||
exact countP_le_countP_of_imp (fun k _ hk => by
|
||
rw [decide_eq_true_eq] at hk ⊢
|
||
exact lt_trans hk hEB)
|
||
omega
|
||
|
||
/-- Restricting an agreement on `[a, E)` to the smaller interval `[a, B)` when `B ≤ E`. -/
|
||
theorem filter_between_eq_of_filter_end_eq {db₁ db₂ : DB} {a : Key} {B E : WithTop Key}
|
||
(hBE : B ≤ E)
|
||
(h : db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E)) =
|
||
db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E))) :
|
||
db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) =
|
||
db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) := by
|
||
have h₁ : db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) =
|
||
(db₁.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E))).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < B)) := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [← Bool.decide_and, decide_eq_decide]
|
||
constructor
|
||
· rintro ⟨h1, h2⟩; exact ⟨h2, h1, lt_of_lt_of_le h2 hBE⟩
|
||
· rintro ⟨h2, h1, _⟩; exact ⟨h1, h2⟩
|
||
have h₂ : db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < B)) =
|
||
(db₂.keys.filter (fun k : Key => decide (((a : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < E))).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < B)) := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
rw [← Bool.decide_and, decide_eq_decide]
|
||
constructor
|
||
· rintro ⟨h1, h2⟩; exact ⟨h2, h1, lt_of_lt_of_le h2 hBE⟩
|
||
· rintro ⟨h2, h1, _⟩; exact ⟨h1, h2⟩
|
||
rw [h₁, h, ← h₂]
|
||
|
||
/-- The crossed negative-begin complete low cell: forward, complete, nonempty, with a
|
||
nonpositive end offset, `begin.key` above the reported head, and a nonclamping negative
|
||
begin offset. -/
|
||
abbrev negCrossLowFwd (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
beginSlack r < reportedBelowBegin r res
|
||
|
||
/-- The closed form for the crossed negative-begin complete low cell: `[head, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossLowFwd (r : RangeRead) (res : Result) : ConflictRange :=
|
||
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The crossed negative-begin complete low cell is minimal.** On the nonclamping
|
||
negative-offset region with `end_.offset ≤ 0`, the minimal conflict range is exactly the
|
||
reported head up to `end_.key`. The begin selector resolves onto the head, and the end
|
||
selector stays below `end_.key`, so agreement on `[head, end_.key)` fixes the whole
|
||
complete window; keys between the last reported key and `end_.key` are necessary by the
|
||
nonpositive-end rule. -/
|
||
theorem minimal_closedForm_of_negCross_lowFwd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossLowFwd r res) :
|
||
Minimal r res (closedFormNegCrossLowFwd r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp⟩ := hcell
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hheadD]; exact hcrossD
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set head : Key := res.keys.head hne with hhead
|
||
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
|
||
have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead
|
||
rw [show closedFormNegCrossLowFwd r res =
|
||
⟨((head : Key) : WithTop Key), r.end_.key⟩ by
|
||
unfold closedFormNegCrossLowFwd; rw [hheadD']]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ →
|
||
RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by
|
||
intro db₁ db₂ hagree hcomp₁
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1
|
||
have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by
|
||
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_stored : head ∈ db₁.keys :=
|
||
RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne)
|
||
have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_negCross_compat' hcomp₁ hrev hmore hne hbegin hnonclamp
|
||
have hB_le_E : r.begin.key ≤ r.end_.key :=
|
||
negCross_begin_le_end hcomp₁ hrev hmore hne hbegin hcross hnonclamp hend
|
||
have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 :=
|
||
negCross_filter_begin_length hcomp₁ hrev hmore hne hbegin hcross hnonclamp
|
||
-- Agreement on the larger window `[head, end_.key)`.
|
||
have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) =
|
||
db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by
|
||
simpa [AgreeOn, ConflictRange.keys] using hagree
|
||
-- Agreement on the begin window `[head, begin.key)`.
|
||
have hfiltB := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
|
||
(a := head) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt
|
||
-- The head is stored in `db₂`.
|
||
have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) :=
|
||
List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩
|
||
have hhead_stored₂ : head ∈ db₂.keys :=
|
||
List.mem_of_mem_filter (by rw [← hfiltB]; exact hhead_in₁)
|
||
have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by rw [← hfiltB]; exact hlen₁
|
||
have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂
|
||
-- The end resolution is fixed by agreement on `[head, end_.key)`.
|
||
have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db₁ r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.2
|
||
have hres_le : resolve db₁ r.end_ ≤ r.end_.key :=
|
||
resolve_le_key_of_offset_nonpos hend
|
||
have hhead_lt_E : ((head : Key) : WithTop Key) < r.end_.key :=
|
||
lt_of_lt_of_le hhead_lt_res hres_le
|
||
have hR : resolve db₂ r.end_ = resolve db₁ r.end_ := by
|
||
rcases resolve_eq_nil_or_eq_top_or_mem (db := db₁) (s := r.end_) with hn | ht | ⟨q, hqdb,
|
||
hqeq⟩
|
||
· rw [hn] at hhead_lt_res
|
||
exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _))
|
||
· exact absurd ht (resolve_ne_top_of_offset_nonpos hend)
|
||
· have hqne : q ≠ [] := by
|
||
intro hqnil
|
||
rw [hqnil] at hqeq
|
||
rw [hqeq] at hhead_lt_res
|
||
exact absurd hhead_lt_res (not_lt.mpr (coe_nil_le_key_withTop _))
|
||
have hqspec : (db₁.keys.filter (fun k : Key =>
|
||
decide (((k : Key) : WithTop Key) < r.end_.key))).reverse[
|
||
(-r.end_.offset).toNat]? = some q :=
|
||
resolve_filter_reverse_eq_some_of_ne_nil hend hqne hqeq
|
||
have hqa : ((head : Key) : WithTop Key) ≤ ((q : Key) : WithTop Key) := by
|
||
rw [← hqeq]; exact le_of_lt hhead_lt_res
|
||
have hR' : resolve db₂ r.end_ = ((q : Key) : WithTop Key) :=
|
||
resolve_eq_of_agree_between (db₁ := db₁) (db₂ := db₂) (s := r.end_) hend rfl
|
||
(le_of_lt hhead_lt_E) hfilt hqspec hqa
|
||
rw [hR', hqeq]
|
||
-- The complete windows agree.
|
||
have hP : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) =
|
||
db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < resolve db₁ r.end_)) :=
|
||
filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
|
||
(a := head) (B := resolve db₁ r.end_) (E := r.end_.key) hres_le hfilt
|
||
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), hL₁, hL₂, hR]
|
||
exact hP
|
||
exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
· intro k hlo hhi
|
||
have hk_le : head ≤ k := WithTop.coe_le_coe.mp hlo
|
||
by_cases hklast : k ≤ res.keys.getLast hne
|
||
· exact necessary_of_between_result ⟨db₀, hcomp₀⟩ hhead_mem (List.getLast_mem hne)
|
||
hk_le hklast
|
||
· exact fun cr hs => necessary_last_to_end_key ⟨db₀, hcomp₀⟩ hrev hmore hne (by omega)
|
||
cr hs (le_of_lt (lt_of_not_ge hklast)) hhi
|
||
|
||
/-- The crossed negative-begin complete low cell in reversed traversal. -/
|
||
abbrev negCrossLowFwdRev (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset ≤ 0 ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
beginSlack r < reportedBelowBegin r res
|
||
|
||
/-- The reversed closed form for the crossed negative-begin complete low cell. -/
|
||
noncomputable def closedFormNegCrossLowFwdRev (r : RangeRead) (res : Result) : ConflictRange :=
|
||
⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The reversed crossed negative-begin complete low cell is minimal**, by flipping the
|
||
traversal and applying the forward rule. -/
|
||
theorem minimal_closedForm_of_negCross_lowFwd_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossLowFwdRev r res) :
|
||
Minimal r res (closedFormNegCrossLowFwdRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by
|
||
rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse]
|
||
have hcount : reportedBelowBegin { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by
|
||
unfold reportedBelowBegin
|
||
exact List.Perm.countP_eq _ (List.reverse_perm res.keys)
|
||
have hcell' : negCrossLowFwd { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_⟩
|
||
· rw [hheadD]; exact hcrossD
|
||
· rw [hcount]; exact hnonclamp
|
||
have hread' : Readable { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩
|
||
have hmin := minimal_closedForm_of_negCross_lowFwd hread' hcell'
|
||
have hcr : closedFormNegCrossLowFwd { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = closedFormNegCrossLowFwdRev r res := by
|
||
unfold closedFormNegCrossLowFwd closedFormNegCrossLowFwdRev
|
||
rw [hheadD]
|
||
rw [hcr] at hmin
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
/-- The crossed negative-begin complete cell at `end.offset = 1` with `begin.key ≤ end_.key`. -/
|
||
abbrev negCrossEndOne (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset = 1 ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
beginSlack r < reportedBelowBegin r res ∧ r.begin.key ≤ r.end_.key
|
||
|
||
/-- The closed form for the crossed negative-begin complete cell at `end.offset = 1`:
|
||
`[head, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossEndOne (r : RangeRead) (res : Result) : ConflictRange :=
|
||
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The crossed negative-begin complete cell at `end.offset = 1` is minimal** when
|
||
`begin.key ≤ end_.key`. Agreement on `[head, end_.key)` fixes the begin resolution (through
|
||
the count identity on `[head, begin.key)`) and the complete window, which with end offset one
|
||
is exactly the stored keys in `[head, end_.key)`; keys above the reported last are necessary
|
||
by `necessary_last_to_end_key`. -/
|
||
theorem minimal_closedForm_of_negCross_endOne {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossEndOne r res) :
|
||
Minimal r res (closedFormNegCrossEndOne r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hB_le_E⟩ := hcell
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hheadD]; exact hcrossD
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set head : Key := res.keys.head hne with hhead
|
||
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
|
||
have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead
|
||
rw [show closedFormNegCrossEndOne r res =
|
||
⟨((head : Key) : WithTop Key), r.end_.key⟩ by
|
||
unfold closedFormNegCrossEndOne; rw [hheadD']]
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ →
|
||
RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by
|
||
intro db₁ db₂ hagree hcomp₁
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1
|
||
have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by
|
||
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_stored : head ∈ db₁.keys :=
|
||
RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne)
|
||
have hhead_lt_res : ((head : Key) : WithTop Key) < resolve db₁ r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp (by rw [← hres_eq]; exact hhead_mem)).2.2
|
||
have hhead_lt_E : ((head : Key) : WithTop Key) < r.end_.key :=
|
||
lt_end_key_of_lt_resolve_end_one hend hhead_stored hhead_lt_res
|
||
have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_negCross_compat' hcomp₁ hrev hmore hne hbegin hnonclamp
|
||
have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) =
|
||
db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) := by
|
||
simpa [AgreeOn, ConflictRange.keys] using hagree
|
||
have hfiltB := filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
|
||
(a := head) (B := r.begin.key) (E := r.end_.key) hB_le_E hfilt
|
||
have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 :=
|
||
negCross_filter_begin_length hcomp₁ hrev hmore hne hbegin hcross hnonclamp
|
||
have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) :=
|
||
List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩
|
||
have hhead_stored₂ : head ∈ db₂.keys :=
|
||
List.mem_of_mem_filter (by rw [← hfiltB]; exact hhead_in₁)
|
||
have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by rw [← hfiltB]; exact hlen₁
|
||
have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂
|
||
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend,
|
||
completeKeys_eq_filter_of_end_one (db := db₂) hrev hend, hL₁, hL₂,
|
||
List.filter_filter, List.filter_filter]
|
||
simpa only [Bool.decide_and] using hfilt
|
||
exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
· intro k hlo hhi
|
||
have hk_le : head ≤ k := WithTop.coe_le_coe.mp hlo
|
||
by_cases hklast : k ≤ res.keys.getLast hne
|
||
· exact necessary_of_between_result ⟨db₀, hcomp₀⟩ hhead_mem (List.getLast_mem hne)
|
||
hk_le hklast
|
||
· exact fun cr hs => necessary_last_to_end_key ⟨db₀, hcomp₀⟩ hrev hmore hne (by omega)
|
||
cr hs (le_of_lt (lt_of_not_ge hklast)) hhi
|
||
|
||
/-- The reversed crossed negative-begin complete cell at `end.offset = 1` with
|
||
`begin.key ≤ end_.key`. -/
|
||
abbrev negCrossEndOneRev (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset = 1 ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
beginSlack r < reportedBelowBegin r res ∧ r.begin.key ≤ r.end_.key
|
||
|
||
/-- The reversed closed form for the crossed negative-begin complete cell at
|
||
`end.offset = 1`: `[last reported, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossEndOneRev (r : RangeRead) (res : Result) : ConflictRange :=
|
||
⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.end_.key⟩
|
||
|
||
/-- **The reversed crossed negative-begin complete cell at `end.offset = 1` is minimal**,
|
||
by flipping the traversal. -/
|
||
theorem minimal_closedForm_of_negCross_endOne_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossEndOneRev r res) :
|
||
Minimal r res (closedFormNegCrossEndOneRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hB_le_E⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by
|
||
rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse]
|
||
have hcount : reportedBelowBegin { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by
|
||
unfold reportedBelowBegin
|
||
exact List.Perm.countP_eq _ (List.reverse_perm res.keys)
|
||
have hcell' : negCrossEndOne { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, hB_le_E⟩
|
||
· rw [hheadD]; exact hcrossD
|
||
· rw [hcount]; exact hnonclamp
|
||
have hread' : Readable { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩
|
||
have hmin := minimal_closedForm_of_negCross_endOne hread' hcell'
|
||
have hcr : closedFormNegCrossEndOne { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = closedFormNegCrossEndOneRev r res := by
|
||
unfold closedFormNegCrossEndOne closedFormNegCrossEndOneRev
|
||
rw [hheadD]
|
||
rw [hcr] at hmin
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
/-- Inserting a gap key strictly between the reported head and `begin.key` lifts the begin
|
||
resolution strictly above the head. This is the negative-offset analogue of
|
||
`resolve_zero_begin_le_of_mem_below` used for the crossed long cells: the head is the
|
||
`beginSlack r + 1`-st key below `begin.key`, so an extra key in the gap makes the resolved
|
||
index one past the head. -/
|
||
theorem resolve_begin_insert_gt {db : DB} {r : RangeRead} {head k : Key}
|
||
(hbegin : r.begin.offset < 0) (hhead : head ∈ db.keys)
|
||
(hcross : ((head : Key) : WithTop Key) < r.begin.key)
|
||
(hknot : k ∉ db.keys) (hk_gt : head < k)
|
||
(hk_lt : ((k : Key) : WithTop Key) < r.begin.key)
|
||
(hlen : (db.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1) :
|
||
((head : Key) : WithTop Key) < resolve (db.insert k hknot) r.begin := by
|
||
set db' := db.insert k hknot with hdb'
|
||
set cB := db.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key))
|
||
with hcB
|
||
set cH := db.keys.countP (fun x : Key => decide (x < head)) with hcH
|
||
have hsplitB := countP_lt_eq_add_filter_withTop db.keys db.nodup
|
||
(a := head) (B := r.begin.key) (le_of_lt hcross)
|
||
rw [hlen] at hsplitB
|
||
have hcB_eq : cB = cH + (beginSlack r + 1) := by
|
||
rw [← hcB, ← hcH] at hsplitB
|
||
exact hsplitB
|
||
have hcB' : (db'.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) <
|
||
r.begin.key))) = cB + 1 := by
|
||
rw [hdb']
|
||
change (db.keys.orderedInsert (· ≤ ·) k).countP _ = cB + 1
|
||
rw [List.Perm.countP_eq _ (List.perm_orderedInsert (· ≤ ·) k db.keys)]
|
||
rw [List.countP_cons]
|
||
simp only [decide_eq_true_eq, hk_lt, ite_true]
|
||
rw [hcB]
|
||
have hcH' : (db'.keys.countP (fun x : Key => decide (x < head))) = cH := by
|
||
rw [hdb']
|
||
change (db.keys.orderedInsert (· ≤ ·) k).countP _ = cH
|
||
rw [List.Perm.countP_eq _ (List.perm_orderedInsert (· ≤ ·) k db.keys)]
|
||
rw [List.countP_cons]
|
||
have hkf : (decide (k < head)) = false := by
|
||
rw [decide_eq_false_iff_not]
|
||
exact not_lt.mpr (le_of_lt hk_gt)
|
||
simp only [hkf, Bool.false_eq_true, ite_false]
|
||
rw [hcH]
|
||
omega
|
||
have hanchor : r.begin.anchor db' = ((cH : ℤ) + 1) := by
|
||
rw [anchor_eq_count_sub_slack db' r (le_of_lt hbegin), hcB']
|
||
have h : (cB : ℤ) = (cH : ℤ) + (beginSlack r : ℤ) + 1 := by
|
||
rw [hcB_eq]; push_cast; ring
|
||
push_cast
|
||
omega
|
||
have hanchor_nonneg : 0 ≤ r.begin.anchor db' := by rw [hanchor]; omega
|
||
have htoNat : (r.begin.anchor db').toNat = cH + 1 := by rw [hanchor]; simp
|
||
have hltlen : cH + 1 < db'.keys.length := by
|
||
have h1 : cH + 1 < cB + 1 := by omega
|
||
have h2 : cB + 1 ≤ db'.keys.length := by rw [← hcB']; exact List.countP_le_length
|
||
omega
|
||
have hres : resolve db' r.begin = (((db'.keys[cH + 1] : Key)) : WithTop Key) := by
|
||
rw [resolve_eq_getElem hanchor_nonneg (by rw [htoNat]; exact hltlen)]
|
||
simp only [htoNat]
|
||
have hhead' : head ∈ db'.keys := (DB.mem_insert_iff hknot).mpr (Or.inr hhead)
|
||
have hhead_get : db'.keys[cH] = head := by
|
||
have h := getElem_countP_lt db'.sorted (x := head) hhead'
|
||
simpa only [hcH'] using h
|
||
have hcH_lt : cH < db'.keys.length := by omega
|
||
have hmono : db'.keys[cH] < db'.keys[cH + 1] := by
|
||
rw [db'.sorted.getElem_lt_getElem_iff]
|
||
omega
|
||
rw [hres]
|
||
exact WithTop.coe_lt_coe.mpr (by simpa only [hhead_get] using hmono)
|
||
|
||
|
||
/-- The crossed negative-begin complete cell at `end.offset = 1` with `end_.key < begin.key`. -/
|
||
abbrev negCrossEndOneLong (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset = 1 ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
beginSlack r < reportedBelowBegin r res ∧ r.end_.key < r.begin.key
|
||
|
||
/-- The closed form for the crossed negative-begin complete long cell at `end.offset = 1`:
|
||
`[head, begin.key)`. -/
|
||
noncomputable def closedFormNegCrossEndOneLong (r : RangeRead) (res : Result) : ConflictRange :=
|
||
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.begin.key⟩
|
||
|
||
/-- **The crossed negative-begin complete long cell at `end.offset = 1` is minimal.** The
|
||
complete window is the stored keys in `[head, end_.key)`, so agreement on `[head, begin.key)`
|
||
restricts to fix it. Keys between `end_.key` and `begin.key` are forced: one is never
|
||
stored in a compatible database (all `beginSlack r + 1` stored keys of `[head, begin.key)`
|
||
are already reported below `end_.key`), and inserting it lifts the begin resolution above
|
||
the reported head. -/
|
||
theorem minimal_closedForm_of_negCross_endOne_long {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossEndOneLong r res) :
|
||
Minimal r res (closedFormNegCrossEndOneLong r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hlong⟩ := hcell
|
||
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hheadD]; exact hcrossD
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set head : Key := res.keys.head hne with hhead
|
||
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
|
||
have hheadD' : res.keys.headD ([] : Key) = head := hheadD.trans hhead
|
||
rw [show closedFormNegCrossEndOneLong r res =
|
||
⟨((head : Key) : WithTop Key), r.begin.key⟩ by
|
||
unfold closedFormNegCrossEndOneLong; rw [hheadD']]
|
||
have hreadable : Readable r res := ⟨db₀, hcomp₀⟩
|
||
have hres_eq₀ : res.keys = RangeRead.completeKeys db₀ r := by
|
||
rcases hcomp₀.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_stored₀ : head ∈ db₀.keys :=
|
||
RangeRead.compatible_keys_subset hcomp₀ (by rw [hhead]; exact List.head_mem hne)
|
||
have hL₀ : resolve db₀ r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_negCross_compat' hcomp₀ hrev hmore hne hbegin hnonclamp
|
||
have hlen₀ : (db₀.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 :=
|
||
negCross_filter_begin_length hcomp₀ hrev hmore hne hbegin hcross hnonclamp
|
||
have hhead_lt_E₀ : ((head : Key) : WithTop Key) < r.end_.key := by
|
||
have hh := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hhead_mem)).2.2
|
||
exact lt_end_key_of_lt_resolve_end_one hend hhead_stored₀ hh
|
||
have hall_reported_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by
|
||
intro k hk
|
||
have hkst : k ∈ db₀.keys := RangeRead.compatible_keys_subset hcomp₀ hk
|
||
have hklt := (RangeRead.mem_completeKeys.mp (by rw [← hres_eq₀]; exact hk)).2.2
|
||
exact lt_end_key_of_lt_resolve_end_one hend hkst hklt
|
||
have hall_reported_lt_B : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.begin.key :=
|
||
fun k hk => lt_trans (hall_reported_lt_E k hk) hlong
|
||
have hreported_eq : reportedBelowBegin r res = res.keys.length := by
|
||
unfold reportedBelowBegin
|
||
rw [List.countP_eq_length]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
exact hall_reported_lt_B k hk
|
||
have hq_lt : beginSlack r < res.keys.length := by
|
||
rw [← hreported_eq]; exact hnonclamp
|
||
have hsub₀ : List.Sublist res.keys db₀.keys := result_sublist_db_of_compatible hcomp₀ hrev
|
||
have hlen_le : res.keys.length ≤ beginSlack r + 1 := by
|
||
have hcount_res : res.keys.countP (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
obtain ⟨i, hi, hik⟩ := List.mem_iff_getElem.mp hk
|
||
have hs : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hhead_le : head ≤ k := by
|
||
rw [← hik, hhead, List.head_eq_getElem hne]
|
||
exact hs.getElem_le_getElem_iff.mpr (Nat.zero_le i)
|
||
exact ⟨WithTop.coe_le_coe.mpr hhead_le, hall_reported_lt_B k hk⟩
|
||
rw [← hcount_res]
|
||
calc res.keys.countP (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))
|
||
≤ db₀.keys.countP (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) :=
|
||
List.Sublist.countP_le hsub₀
|
||
_ = (db₀.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key))).length :=
|
||
List.countP_eq_length_filter
|
||
_ = beginSlack r + 1 := hlen₀
|
||
have hres_len : res.keys.length = beginSlack r + 1 := le_antisymm hlen_le (by omega)
|
||
refine minimal_iff_sound_and_necessaryRange.mpr ⟨?_, ?_⟩
|
||
· have step : ∀ db₁ db₂, AgreeOn ⟨((head : Key) : WithTop Key), r.begin.key⟩ db₁ db₂ →
|
||
RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by
|
||
intro db₁ db₂ hagree hcomp₁
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1
|
||
have hres_eq : res.keys = RangeRead.completeKeys db₁ r := by
|
||
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
have hhead_stored : head ∈ db₁.keys :=
|
||
RangeRead.compatible_keys_subset hcomp₁ (by rw [hhead]; exact List.head_mem hne)
|
||
have hL₁ : resolve db₁ r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_negCross_compat' hcomp₁ hrev hmore hne hbegin hnonclamp
|
||
have hfilt : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) =
|
||
db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
simpa [AgreeOn, ConflictRange.keys] using hagree
|
||
have hfiltE : db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) =
|
||
db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.end_.key)) :=
|
||
filter_between_eq_of_filter_end_eq (db₁ := db₁) (db₂ := db₂)
|
||
(a := head) (B := r.end_.key) (E := r.begin.key) (le_of_lt hlong) hfilt
|
||
have hlen₁ : (db₁.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 :=
|
||
negCross_filter_begin_length hcomp₁ hrev hmore hne hbegin hcross hnonclamp
|
||
have hhead_in₁ : head ∈ db₁.keys.filter (fun k : Key =>
|
||
decide (((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key)) :=
|
||
List.mem_filter.mpr ⟨hhead_stored, by rw [decide_eq_true_eq]; exact ⟨le_refl _, hcross⟩⟩
|
||
have hhead_stored₂ : head ∈ db₂.keys :=
|
||
List.mem_of_mem_filter (by rw [← hfilt]; exact hhead_in₁)
|
||
have hlen₂ : (db₂.keys.filter (fun k : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length =
|
||
beginSlack r + 1 := by rw [← hfilt]; exact hlen₁
|
||
have hL₂ : resolve db₂ r.begin = ((head : Key) : WithTop Key) :=
|
||
resolve_begin_eq_head_of_filter_len hbegin hhead_stored₂ hcross hlen₂
|
||
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend,
|
||
completeKeys_eq_filter_of_end_one (db := db₂) hrev hend, hL₁, hL₂,
|
||
List.filter_filter, List.filter_filter]
|
||
simpa only [Bool.decide_and] using hfiltE
|
||
exact ⟨hlim, Or.inr ⟨hmore, by rw [hres_eq, hck]⟩⟩
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
· intro k hlo hhi
|
||
have hk_head : head ≤ k := WithTop.coe_le_coe.mp hlo
|
||
have hk_lt_B : ((k : Key) : WithTop Key) < r.begin.key := hhi
|
||
by_cases hklast : k ≤ res.keys.getLast hne
|
||
· exact necessary_of_between_result hreadable hhead_mem (List.getLast_mem hne) hk_head hklast
|
||
· have hk_gt_last : res.keys.getLast hne < k := lt_of_not_ge hklast
|
||
have hhead_le_last : head ≤ res.keys.getLast hne := by
|
||
have hs : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
rw [hhead, List.head_eq_getElem hne, List.getLast_eq_getElem hne]
|
||
exact hs.getElem_le_getElem_iff.mpr (by
|
||
have := List.length_pos_of_ne_nil hne
|
||
omega)
|
||
have hk_gt_head : head < k := lt_of_le_of_lt hhead_le_last hk_gt_last
|
||
by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key
|
||
· exact fun cr hs => necessary_last_to_end_key hreadable hrev hmore hne (le_of_eq hend)
|
||
cr hs (le_of_lt hk_gt_last) hkE
|
||
· have hkE_le : r.end_.key ≤ ((k : Key) : WithTop Key) := le_of_not_gt hkE
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hknot : k ∉ db₀.keys := by
|
||
intro hkdb
|
||
have hPk : (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) k = true := by
|
||
rw [decide_eq_true_eq]
|
||
exact ⟨WithTop.coe_le_coe.mpr hk_head, hk_lt_B⟩
|
||
have hkfil : k ∈ db₀.keys.filter (fun x : Key => decide (((head : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)) :=
|
||
List.mem_filter.mpr ⟨hkdb, hPk⟩
|
||
have hres_filter_self : res.keys.filter (fun x : Key =>
|
||
decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < r.begin.key)) = res.keys := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
obtain ⟨i, hi, hix⟩ := List.mem_iff_getElem.mp hx
|
||
have hsrt : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||
have hhead_le : head ≤ x := by
|
||
rw [← hix, hhead, List.head_eq_getElem hne]
|
||
exact hsrt.getElem_le_getElem_iff.mpr (Nat.zero_le i)
|
||
exact ⟨WithTop.coe_le_coe.mpr hhead_le, hall_reported_lt_B x hx⟩
|
||
have hfilt_eq : db₀.keys.filter (fun x : Key =>
|
||
decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun x : Key =>
|
||
decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < r.begin.key)) := by
|
||
symm
|
||
apply List.Sublist.eq_of_length
|
||
· exact List.Sublist.filter _ hsub₀
|
||
· rw [hlen₀, hres_filter_self, hres_len]
|
||
have hkres : k ∈ res.keys := by
|
||
have hmf : k ∈ res.keys.filter (fun x : Key =>
|
||
decide (((head : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧
|
||
((x : Key) : WithTop Key) < r.begin.key)) := by
|
||
rw [← hfilt_eq]; exact hkfil
|
||
exact List.mem_of_mem_filter hmf
|
||
exact absurd (hall_reported_lt_E k hkres) (not_lt.mpr hkE_le)
|
||
have hhead' : head ∈ (db₀.insert k hknot).keys :=
|
||
(DB.mem_insert_iff hknot).mpr (Or.inr hhead_stored₀)
|
||
have hgt : ((head : Key) : WithTop Key) < resolve (db₀.insert k hknot) r.begin :=
|
||
resolve_begin_insert_gt hbegin hhead_stored₀ hcross hknot hk_gt_head hk_lt_B hlen₀
|
||
have hhead_notin : head ∉ RangeRead.completeKeys (db₀.insert k hknot) r := by
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend]
|
||
intro hmem
|
||
simp only [List.mem_filter, decide_eq_true_eq] at hmem
|
||
exact absurd hmem.2 (not_le.mpr hgt)
|
||
have hcomp₁ : RangeRead.Compatible (db₀.insert k hknot) r res :=
|
||
(hs db₀ _ (ConflictRange.keys_insert_of_not_mem cr db₀ hknot hkcr).symm).mp hcomp₀
|
||
have hres_eq₁ : res.keys = RangeRead.completeKeys (db₀.insert k hknot) r := by
|
||
rcases hcomp₁.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
exact hhead_notin (by rw [← hres_eq₁]; exact hhead_mem)
|
||
|
||
|
||
/-- The reversed crossed negative-begin complete long cell at `end.offset = 1`. -/
|
||
abbrev negCrossEndOneLongRev (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset = 1 ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
beginSlack r < reportedBelowBegin r res ∧ r.end_.key < r.begin.key
|
||
|
||
/-- The reversed closed form for the crossed negative-begin complete long cell at
|
||
`end.offset = 1`: `[last reported, begin.key)`. -/
|
||
noncomputable def closedFormNegCrossEndOneLongRev (r : RangeRead) (res : Result) : ConflictRange :=
|
||
⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), r.begin.key⟩
|
||
|
||
/-- **The reversed crossed negative-begin complete long cell at `end.offset = 1` is
|
||
minimal**, by flipping the traversal. -/
|
||
theorem minimal_closedForm_of_negCross_endOne_long_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossEndOneLongRev r res) :
|
||
Minimal r res (closedFormNegCrossEndOneLongRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hnonclamp, hlong⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by
|
||
rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse]
|
||
have hcount : reportedBelowBegin { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by
|
||
unfold reportedBelowBegin
|
||
exact List.Perm.countP_eq _ (List.reverse_perm res.keys)
|
||
have hcell' : negCrossEndOneLong { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_, hlong⟩
|
||
· rw [hheadD]; exact hcrossD
|
||
· rw [hcount]; exact hnonclamp
|
||
have hread' : Readable { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩
|
||
have hmin := minimal_closedForm_of_negCross_endOne_long hread' hcell'
|
||
have hcr : closedFormNegCrossEndOneLong { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = closedFormNegCrossEndOneLongRev r res := by
|
||
unfold closedFormNegCrossEndOneLong closedFormNegCrossEndOneLongRev
|
||
rw [hheadD]
|
||
rw [hcr] at hmin
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
/-- A key of a strictly increasing list is at or above the list's head. -/
|
||
private theorem head_le_of_pairwise_negCross {l : List Key} (h : l.Pairwise (· < ·))
|
||
(hne : l ≠ []) {a : Key} (ha : a ∈ l) : l.head hne ≤ a :=
|
||
(h.imp le_of_lt).rel_head ha
|
||
|
||
private theorem mem_inRange_negCross {db : DB} {lo hi : WithTop Key} {k : Key} :
|
||
k ∈ RangeRead.inRange db.keys lo hi ↔
|
||
k ∈ db.keys ∧ lo ≤ ((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < hi := by
|
||
unfold RangeRead.inRange
|
||
rw [List.mem_filter, decide_eq_true_eq]
|
||
|
||
/-- Every stored key of `db` at or above every key of the window `[⊥, hi)` is at or
|
||
above `hi` itself: a stored key below `hi` would lie in the window, so the window's
|
||
least key sits below it. -/
|
||
private theorem le_of_not_mem_inRange_nil_negCross {db : DB} {hi : WithTop Key} (m : Key)
|
||
(hne : RangeRead.inRange db.keys (([] : Key) : WithTop Key) hi ≠ [])
|
||
(hm : ∀ y ∈ RangeRead.inRange db.keys (([] : Key) : WithTop Key) hi, m ≤ y)
|
||
(x : Key) (hx : x ∈ db.keys) : m ≤ x := by
|
||
by_cases hxin : x ∈ RangeRead.inRange db.keys (([] : Key) : WithTop Key) hi
|
||
· exact hm x hxin
|
||
· have hmhi : ((m : Key) : WithTop Key) < hi :=
|
||
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hm _ (List.head_mem hne)))
|
||
(mem_inRange_negCross.mp (List.head_mem hne)).2.2
|
||
by_contra hcon
|
||
exact hxin (mem_inRange_negCross.mpr ⟨hx, coe_nil_le_key_withTop x,
|
||
lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (not_le.mp hcon)) (le_of_lt hmhi)⟩)
|
||
|
||
/-- In a non-reversed read whose begin selector is clamped to the empty key in a
|
||
compatible database, every key strictly below the reported head is necessary. -/
|
||
theorem necessary_below_head_of_anchor_nonpos_negCross {r : RangeRead} {res : Result} (db₀ : DB)
|
||
(hread : RangeRead.Compatible db₀ r res) (hrev : r.reverse = false) (hne : res.keys ≠ [])
|
||
(hclamp : r.begin.anchor db₀ < 0) {k : Key}
|
||
(hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) :
|
||
Necessary r res k := by
|
||
intro cr hs
|
||
by_contra hcr
|
||
have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r := RangeRead.compatible_keys_prefix hread
|
||
have hnil₀ : resolve db₀ r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hclamp
|
||
have hck₀ : RangeRead.completeKeys db₀ r =
|
||
RangeRead.inRange db₀.keys (([] : Key) : WithTop Key) (resolve db₀ r.end_) := by
|
||
simp only [RangeRead.completeKeys]
|
||
rw [hnil₀, hrev]
|
||
simp
|
||
have hwin₀ : RangeRead.inRange db₀.keys (([] : Key) : WithTop Key) (resolve db₀ r.end_) ≠ [] := by
|
||
rw [← hck₀]
|
||
exact hpre₀.ne_nil hne
|
||
have hheadwin : (RangeRead.inRange db₀.keys (([] : Key) : WithTop Key)
|
||
(resolve db₀ r.end_)).head hwin₀ = res.keys.head hne := by
|
||
have hhead?₀ : (RangeRead.inRange db₀.keys (([] : Key) : WithTop Key)
|
||
(resolve db₀ r.end_)).head? = res.keys.head? := by
|
||
obtain ⟨t, ht⟩ := hpre₀
|
||
rw [← hck₀, ← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl,
|
||
Option.some_or]
|
||
exact (List.head_eq_iff_head?_eq_some hwin₀).mpr
|
||
(hhead?₀.trans ((List.head_eq_iff_head?_eq_some hne).mp rfl))
|
||
have hheadle₀ : ∀ y ∈ RangeRead.inRange db₀.keys (([] : Key) : WithTop Key)
|
||
(resolve db₀ r.end_), res.keys.head hne ≤ y := by
|
||
intro y hy
|
||
rw [← hheadwin]
|
||
exact head_le_of_pairwise_negCross (RangeRead.inRange_pairwise db₀ _ _) hwin₀ hy
|
||
have hmin : ∀ x ∈ db₀.keys, res.keys.head hne ≤ x :=
|
||
fun x hx => le_of_not_mem_inRange_nil_negCross (res.keys.head hne) hwin₀ hheadle₀ x hx
|
||
have hk0 : k ∉ db₀.keys := fun hxm =>
|
||
absurd hk (not_lt.mpr (WithTop.coe_le_coe.mpr (hmin k hxm)))
|
||
have hkeys₁ : (db₀.insert k hk0).keys = k :: db₀.keys := by
|
||
rw [DB.insert_keys, orderedInsert_eq_insertIdx db₀.sorted hk0]
|
||
have hc0 : db₀.keys.countP (fun k' => decide (k' < k)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro k' hk'
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (le_of_lt (lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk) (hmin k' hk')))
|
||
rw [hc0]
|
||
simp
|
||
have hagree : AgreeOn cr db₀ (db₀.insert k hk0) :=
|
||
(ConflictRange.keys_insert_of_not_mem cr db₀ hk0 hcr).symm
|
||
have hcompat₁ : RangeRead.Compatible (db₀.insert k hk0) r res := (hs db₀ _ hagree).mp hread
|
||
have hpre₁ : res.keys <+: RangeRead.completeKeys (db₀.insert k hk0) r :=
|
||
RangeRead.compatible_keys_prefix hcompat₁
|
||
have hcnt : (db₀.insert k hk0).keys.countP
|
||
(fun k' => decide (((k' : Key) : WithTop Key) < r.begin.key)) =
|
||
db₀.keys.countP (fun k' => decide (((k' : Key) : WithTop Key) < r.begin.key)) +
|
||
(if ((k : Key) : WithTop Key) < r.begin.key then 1 else 0) := by
|
||
rw [hkeys₁, List.countP_cons]
|
||
by_cases hkk : ((k : Key) : WithTop Key) < r.begin.key <;> simp [hkk]
|
||
have hanchor : Selector.anchor (db₀.insert k hk0) r.begin ≤ 0 := by
|
||
have hneg₀ : Selector.anchor db₀ r.begin ≤ -1 := by
|
||
have := hclamp
|
||
omega
|
||
unfold Selector.anchor at hneg₀ ⊢
|
||
rw [hcnt]
|
||
push_cast
|
||
split <;> omega
|
||
have hbegin₁ : resolve (db₀.insert k hk0) r.begin ≤ ((k : Key) : WithTop Key) := by
|
||
rcases lt_or_eq_of_le hanchor with hneg | heq
|
||
· rw [resolve_eq_ite, ite_eq_left hneg]
|
||
exact coe_nil_le_key_withTop k
|
||
· have h0 : 0 ≤ Selector.anchor (db₀.insert k hk0) r.begin := le_of_eq heq.symm
|
||
have hidx : (Selector.anchor (db₀.insert k hk0) r.begin).toNat = 0 := by rw [heq]; rfl
|
||
have hget : (db₀.insert k hk0).keys[(Selector.anchor (db₀.insert k hk0) r.begin).toNat]? =
|
||
some k := by rw [hidx, hkeys₁]; rfl
|
||
rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0)]
|
||
simp only [hget]
|
||
exact le_of_eq rfl
|
||
by_cases hle : resolve (db₀.insert k hk0) r.end_ ≤ ((k : Key) : WithTop Key)
|
||
· have hlt₁ : ((res.keys.head hne : Key) : WithTop Key) <
|
||
resolve (db₀.insert k hk0) r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp (hpre₁.subset (List.head_mem hne))).2.2
|
||
exact absurd (lt_of_lt_of_le (lt_trans hk hlt₁) hle) (lt_irrefl (((k : Key) : WithTop Key)))
|
||
· have hck₁ : RangeRead.completeKeys (db₀.insert k hk0) r =
|
||
RangeRead.inRange (db₀.insert k hk0).keys
|
||
(resolve (db₀.insert k hk0) r.begin) (resolve (db₀.insert k hk0) r.end_) := by
|
||
simp only [RangeRead.completeKeys, hrev]
|
||
simp
|
||
have hpref : (RangeRead.completeKeys (db₀.insert k hk0) r).head? = res.keys.head? := by
|
||
obtain ⟨t, ht⟩ := hpre₁
|
||
rw [← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl, Option.some_or]
|
||
have hhead?₁ : (RangeRead.completeKeys (db₀.insert k hk0) r).head? = some k := by
|
||
rw [hck₁]
|
||
simp only [RangeRead.inRange, List.head?_filter]
|
||
refine RangeRead.find?_eq_some_of_pairwise
|
||
(List.sortedLT_iff_pairwise.mp (db₀.insert k hk0).sorted) ?_ ?_ ?_
|
||
· rw [hkeys₁]
|
||
simp
|
||
· rw [decide_eq_true_eq]
|
||
exact ⟨hbegin₁, lt_of_not_ge hle⟩
|
||
· intro x hx hxa
|
||
rw [decide_eq_false_iff_not]
|
||
intro hcon
|
||
rw [hkeys₁] at hx
|
||
have hx0 : x ∈ db₀.keys := by
|
||
rcases List.mem_cons.mp hx with hx1 | hx2
|
||
· subst hx1
|
||
exact absurd hxa (lt_irrefl _)
|
||
· exact hx2
|
||
exact absurd (lt_of_lt_of_le (lt_trans hxa (WithTop.coe_lt_coe.mp hk)) (hmin x hx0))
|
||
(lt_irrefl x)
|
||
have hkeq : k = res.keys.head hne := Option.some.inj
|
||
((hpref.symm.trans hhead?₁).symm.trans ((List.head_eq_iff_head?_eq_some hne).mp rfl))
|
||
exact absurd hk (not_lt.mpr (WithTop.coe_le_coe.mpr (le_of_eq hkeq.symm)))
|
||
|
||
/-!
|
||
# The crossed negative-begin complete cell at end offset one inside the clamp
|
||
|
||
This scratch file develops the missing closed form for the forward crossed
|
||
negative-begin complete result at `end.offset = 1` in the clamping regime
|
||
`reportedBelowBegin ≤ beginSlack`.
|
||
-/
|
||
|
||
|
||
/-- The crossed negative-begin complete cell at `end.offset = 1` in the clamp
|
||
regime. -/
|
||
abbrev negCrossClampEndOne (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = false ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset = 1 ∧
|
||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
reportedBelowBegin r res ≤ beginSlack r
|
||
|
||
/-- The slack beyond the reported keys below `begin.key`. -/
|
||
def clampEndOneSlack (r : RangeRead) (res : Result) : ℕ :=
|
||
beginSlack r + 1 - reportedBelowBegin r res
|
||
|
||
/-- The capacity of the key gap `[end_.key, begin.key)`. -/
|
||
def clampEndOneCap (r : RangeRead) : ℕ∞ :=
|
||
intervalCard r.end_.key r.begin.key
|
||
|
||
/-- The upper endpoint of the clamp end-one cell. -/
|
||
noncomputable def clampEndOneUpper (r : RangeRead) (res : Result) : WithTop Key :=
|
||
if ((clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r) ∧
|
||
(((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r) ∨
|
||
res.keys.headD ([] : Key) ≠ [])
|
||
then r.begin.key else r.end_.key
|
||
|
||
/-- The closed form for the crossed negative-begin complete clamp end-one cell. -/
|
||
noncomputable def closedFormNegCrossClampEndOne (r : RangeRead) (res : Result) :
|
||
ConflictRange :=
|
||
⟨(([] : Key) : WithTop Key), clampEndOneUpper r res⟩
|
||
|
||
section Structural
|
||
|
||
variable {r : RangeRead} {res : Result}
|
||
|
||
theorem clampEndOne_headD_eq {hne : res.keys ≠ []} :
|
||
res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
|
||
theorem clampEndOne_slack_pos (hcell : negCrossClampEndOne r res) :
|
||
0 < clampEndOneSlack r res := by
|
||
obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell
|
||
unfold clampEndOneSlack
|
||
omega
|
||
|
||
theorem clampEndOne_cap_nonneg (r : RangeRead) : (0 : ℕ∞) ≤ clampEndOneCap r :=
|
||
zero_le
|
||
|
||
/-- `U = begin.key` forces `end_.key ≤ begin.key`, because the gap capacity is zero
|
||
otherwise and the slack is positive. -/
|
||
theorem clampEndOne_end_le_begin_of_upper_begin (hcell : negCrossClampEndOne r res)
|
||
(hupper : clampEndOneUpper r res = r.begin.key) :
|
||
r.end_.key ≤ r.begin.key := by
|
||
by_contra hcon
|
||
have hlt : r.begin.key < r.end_.key := lt_of_not_ge hcon
|
||
have hzero : clampEndOneCap r = 0 := by
|
||
unfold clampEndOneCap
|
||
exact intervalCard_eq_zero_of_le (le_of_lt hlt)
|
||
have hs := clampEndOne_slack_pos hcell
|
||
unfold clampEndOneUpper at hupper
|
||
by_cases hc : ((clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r) ∧
|
||
(((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r) ∨
|
||
res.keys.headD ([] : Key) ≠ [])
|
||
· rw [ite_eq_left hc] at hupper
|
||
rw [hzero] at hc
|
||
exact absurd hc.1 (not_le.mpr (by exact_mod_cast hs))
|
||
· rw [ite_eq_right hc] at hupper
|
||
exact absurd hupper (ne_of_lt hlt).symm
|
||
|
||
end Structural
|
||
|
||
|
||
|
||
section CountBounds
|
||
|
||
variable {db : DB} {r : RangeRead} {res : Result}
|
||
|
||
theorem clampEndOne_reported_eq_length (_hcell : negCrossClampEndOne r res)
|
||
(hEB : r.end_.key ≤ r.begin.key)
|
||
(hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) :
|
||
reportedBelowBegin r res = res.keys.length := by
|
||
unfold reportedBelowBegin
|
||
rw [List.countP_eq_length]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
exact lt_of_lt_of_le (hres_lt_E k hk) hEB
|
||
|
||
theorem clampEndOne_count_le_of_between_bound {db : DB} {r : RangeRead} {res : Result}
|
||
(hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) =
|
||
res.keys)
|
||
(_hm : reportedBelowBegin r res = res.keys.length)
|
||
(hEB : r.end_.key ≤ r.begin.key) :
|
||
db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length +
|
||
(db.keys.filter (fun k : Key => decide (r.end_.key ≤ ((k : Key) : WithTop Key) ∧
|
||
((k : Key) : WithTop Key) < r.begin.key))).length := by
|
||
by_cases hEtop : r.end_.key = (⊤ : WithTop Key)
|
||
· -- `end_.key = ⊤`: the split key is `⊤`; the interval `[⊤, B)` is empty.
|
||
have hEB' : (⊤ : WithTop Key) ≤ r.begin.key := by rw [← hEtop]; exact hEB
|
||
have hB : r.begin.key = (⊤ : WithTop Key) := le_antisymm le_top hEB'
|
||
rw [hB]
|
||
have hcnt : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < (⊤ : WithTop
|
||
Key))) =
|
||
db.keys.length := countP_lt_top
|
||
have hlen : res.keys.length = db.keys.length := by
|
||
conv_rhs => rw [← hcnt]
|
||
rw [← hfilter, hEtop, List.countP_eq_length_filter]
|
||
have hempty : db.keys.filter (fun k : Key => decide ((⊤ : WithTop Key) ≤ ((k : Key) : WithTop
|
||
Key) ∧
|
||
((k : Key) : WithTop Key) < (⊤ : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro k _; simp
|
||
rw [hcnt, hEtop, hempty]
|
||
simp [hlen]
|
||
· -- `end_.key = ↑E`: split at the genuine key `E`.
|
||
obtain ⟨E, hE⟩ : ∃ E : Key, r.end_.key = (↑E : WithTop Key) := by
|
||
cases h : r.end_.key with
|
||
| top => exact absurd h hEtop
|
||
| coe E => exact ⟨E, rfl⟩
|
||
have hEB' : ((E : Key) : WithTop Key) ≤ r.begin.key := by rw [← hE]; exact hEB
|
||
have hsplit := countP_lt_eq_add_filter_withTop db.keys db.nodup
|
||
(a := E) (B := r.begin.key) hEB'
|
||
have hcntE : db.keys.countP (fun k : Key => decide (k < E)) = res.keys.length := by
|
||
rw [← countP_lt_coe E, List.countP_eq_length_filter]
|
||
have h := congrArg List.length hfilter
|
||
simpa [hE] using h
|
||
rw [hcntE] at hsplit
|
||
simpa [hE] using hsplit
|
||
|
||
theorem clampEndOne_count_le_slack_of_cap_lt (hcell : negCrossClampEndOne r res)
|
||
(hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) =
|
||
res.keys)
|
||
(hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)
|
||
(hcap : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞)) :
|
||
db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) ≤
|
||
beginSlack r := by
|
||
by_cases hEB : r.end_.key ≤ r.begin.key
|
||
· have hm := clampEndOne_reported_eq_length hcell hEB hres_lt_E
|
||
have hcount := clampEndOne_count_le_of_between_bound (db := db) hfilter hm hEB
|
||
have hbetween : (db.keys.filter (fun k : Key => decide (r.end_.key ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length <
|
||
clampEndOneSlack r res := by
|
||
have hle : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length : ℕ∞) ≤
|
||
clampEndOneCap r := by
|
||
unfold clampEndOneCap
|
||
exact between_length_le_intervalCard (db := db) hEB
|
||
exact_mod_cast lt_of_le_of_lt hle hcap
|
||
obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell
|
||
have hs : clampEndOneSlack r res = beginSlack r + 1 - reportedBelowBegin r res := rfl
|
||
omega
|
||
· -- `begin.key < end_.key`: no room above `end_.key`, and all reported keys are below it.
|
||
have hlt : r.begin.key < r.end_.key := lt_of_not_ge hEB
|
||
have h₁ : (db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
|
||
r.end_.key))).filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hkB : ((k : Key) : WithTop Key) < r.begin.key
|
||
· have hkE : ((k : Key) : WithTop Key) < r.end_.key := lt_trans hkB hlt
|
||
simp [hkB, hkE]
|
||
· simp [hkB]
|
||
have hBsubE : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
|
||
rw [← h₁, hfilter]
|
||
obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell
|
||
have hcB : db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) =
|
||
reportedBelowBegin r res := by
|
||
rw [List.countP_eq_length_filter]
|
||
unfold reportedBelowBegin
|
||
rw [List.countP_eq_length_filter, hBsubE]
|
||
omega
|
||
|
||
end CountBounds
|
||
|
||
|
||
|
||
/-- A member of a strictly increasing list is at or above its head. -/
|
||
theorem head_le_of_sortedLT' {l : List Key} (h : l.SortedLT) (hne : l ≠ []) {a : Key}
|
||
(ha : a ∈ l) : l.head hne ≤ a :=
|
||
(List.sortedLT_iff_pairwise.mp h).imp le_of_lt |>.rel_head ha
|
||
|
||
section ResolveNil
|
||
|
||
variable {db : DB} {r : RangeRead} {res : Result}
|
||
|
||
/-- Under `cap < slack`, any database whose keys below `end_.key` are exactly the reported
|
||
keys has a clamping begin selector. -/
|
||
theorem clampEndOne_resolve_begin_nil_of_cap_lt (hcell : negCrossClampEndOne r res)
|
||
(hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) =
|
||
res.keys)
|
||
(hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)
|
||
(hcap : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞)) :
|
||
resolve db r.begin = (([] : Key) : WithTop Key) := by
|
||
have hcB := clampEndOne_count_le_slack_of_cap_lt (db := db) hcell hfilter hres_lt_E hcap
|
||
have hbegin : r.begin.offset < 0 := hcell.2.1
|
||
apply resolve_eq_nil
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
|
||
have : (db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) ≤
|
||
(beginSlack r : ℤ) := by exact_mod_cast hcB
|
||
omega
|
||
|
||
/-- Under `end_.key ≤ begin.key`, the number of stored keys below `begin.key` is at most
|
||
`slack + 1` when the stored keys below `end_.key` are the reported keys. -/
|
||
theorem clampEndOne_count_le_succ_slack_of_cap_eq (hcell : negCrossClampEndOne r res)
|
||
(hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) =
|
||
res.keys)
|
||
(hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)
|
||
(hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞)) :
|
||
db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) ≤
|
||
beginSlack r + 1 := by
|
||
have hs : 0 < clampEndOneSlack r res := clampEndOne_slack_pos hcell
|
||
have hcap0 : clampEndOneCap r ≠ 0 := by rw [hcap]; exact ne_of_gt (by exact_mod_cast hs)
|
||
have hEB : r.end_.key ≤ r.begin.key := by
|
||
by_contra hcon
|
||
have hlt : r.begin.key < r.end_.key := lt_of_not_ge hcon
|
||
exact hcap0 (by unfold clampEndOneCap; exact intervalCard_eq_zero_of_le (le_of_lt hlt))
|
||
have hm := clampEndOne_reported_eq_length hcell hEB hres_lt_E
|
||
have hcount := clampEndOne_count_le_of_between_bound (db := db) hfilter hm hEB
|
||
obtain ⟨-, -, -, -, -, -, hclamp⟩ := hcell
|
||
have hbetween : (db.keys.filter (fun k : Key => decide (r.end_.key ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length ≤
|
||
clampEndOneSlack r res := by
|
||
have hle : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤
|
||
((k : Key) : WithTop Key) ∧ ((k : Key) : WithTop Key) < r.begin.key))).length : ℕ∞) ≤
|
||
clampEndOneCap r := by
|
||
unfold clampEndOneCap
|
||
exact between_length_le_intervalCard (db := db) hEB
|
||
rw [hcap] at hle
|
||
exact_mod_cast hle
|
||
have hsdef : clampEndOneSlack r res = beginSlack r + 1 - reportedBelowBegin r res := rfl
|
||
omega
|
||
|
||
/-- Under `cap = slack` with the empty key as the reported head, any database whose keys
|
||
below `end_.key` are exactly the reported keys has a clamping begin selector. -/
|
||
theorem clampEndOne_resolve_begin_nil_of_cap_eq_nil (hcell : negCrossClampEndOne r res)
|
||
(hfilter : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) =
|
||
res.keys)
|
||
(hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)
|
||
(hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞))
|
||
(hnil : res.keys.headD ([] : Key) = ([] : Key)) :
|
||
resolve db r.begin = (([] : Key) : WithTop Key) := by
|
||
have hcB := clampEndOne_count_le_succ_slack_of_cap_eq (db := db) hcell hfilter hres_lt_E hcap
|
||
have hbegin : r.begin.offset < 0 := hcell.2.1
|
||
have hne : res.keys ≠ [] := hcell.2.2.2.1
|
||
have hanchor_le : r.begin.anchor db ≤ 0 := by
|
||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
|
||
have : (db.keys.countP (fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key)) : ℤ) ≤
|
||
(beginSlack r : ℤ) + 1 := by exact_mod_cast hcB
|
||
omega
|
||
rcases lt_or_eq_of_le hanchor_le with hneg | heq
|
||
· exact resolve_eq_nil (db := db) (s := r.begin) hneg
|
||
· have hzero : r.begin.anchor db = 0 := heq
|
||
have hmem_nil : ([] : Key) ∈ db.keys := by
|
||
have hhead_mem : res.keys.head hne ∈ res.keys := List.head_mem hne
|
||
have hhead_nil : res.keys.head hne = ([] : Key) := by
|
||
rw [← clampEndOne_headD_eq (hne := hne)]
|
||
exact hnil
|
||
have hmem : res.keys.head hne ∈ db.keys := by
|
||
have : res.keys.head hne ∈ db.keys.filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) := by
|
||
rw [hfilter]; exact hhead_mem
|
||
exact (List.mem_filter.mp this).1
|
||
rwa [hhead_nil] at hmem
|
||
have hlen : 0 < db.keys.length := List.length_pos_of_mem hmem_nil
|
||
have hdbne : db.keys ≠ [] := by
|
||
intro h; rw [h] at hlen; simp at hlen
|
||
have hhead_nil : db.keys.head hdbne = ([] : Key) :=
|
||
le_antisymm (head_le_of_sortedLT' db.sorted hdbne hmem_nil) (nil_le_key _)
|
||
have h0 : db.keys[0]? = some ([] : Key) := by
|
||
rw [List.getElem?_eq_getElem hlen, ← List.head_eq_getElem hdbne, hhead_nil]
|
||
rw [resolve_eq_ite, ite_eq_right (not_lt.mpr (le_of_eq hzero.symm))]
|
||
rw [show (r.begin.anchor db).toNat = 0 by rw [hzero]; rfl, h0]
|
||
|
||
end ResolveNil
|
||
|
||
|
||
|
||
|
||
/-- Filtering below `lo` first, then below a larger `E`, agrees with filtering below `lo`
|
||
directly. -/
|
||
theorem filter_lt_of_le_filter_lt {db : DB} {lo E : WithTop Key} (hE : lo ≤ E) :
|
||
db.keys.filter (fun k : Key => decide ((k : WithTop Key) < lo)) =
|
||
(db.keys.filter (fun k : Key => decide ((k : WithTop Key) < E))).filter
|
||
(fun k : Key => decide ((k : WithTop Key) < lo)) := by
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hk : (k : WithTop Key) < lo
|
||
· have hkE : (k : WithTop Key) < E := lt_of_lt_of_le hk hE
|
||
simp [hk, hkE]
|
||
· simp [hk]
|
||
|
||
/-- **Agreement below a bound covering both selector keys fixes the complete keys at
|
||
`end.offset = 1`.** -/
|
||
theorem completeKeys_eq_of_agree_cover {r : RangeRead} {db₁ db₂ : DB} {E : WithTop Key}
|
||
(hrev : r.reverse = false) (hend : r.end_.offset = 1) (hbegin : r.begin.offset ≤ 0)
|
||
(hB : r.begin.key ≤ E) (hE : r.end_.key ≤ E)
|
||
(hagree : AgreeOn ⟨(([] : Key) : WithTop Key), E⟩ db₁ db₂) :
|
||
RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||
have hfE : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < E)) =
|
||
db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < E)) := by
|
||
have h := hagree
|
||
rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h
|
||
exact h
|
||
have hf_end : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
|
||
db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by
|
||
rw [filter_lt_of_le_filter_lt (db := db₁) hE,
|
||
filter_lt_of_le_filter_lt (db := db₂) hE, hfE]
|
||
have hres := resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := E) (s := r.begin)
|
||
hbegin hB hfE
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend,
|
||
completeKeys_eq_filter_of_end_one (db := db₂) hrev hend, hf_end, hres]
|
||
|
||
|
||
|
||
/-- **The `U = end_.key` long branch is compatible from its filter condition.** If the
|
||
stored keys below `end_.key` are exactly the reported keys and the capacity condition
|
||
fails (the gap is too short, or it is exactly `s` with an empty-key head), the begin
|
||
selector clamps to the empty key, so the complete window is exactly those stored keys.
|
||
-/
|
||
theorem clampEndOne_compat_of_filter {r : RangeRead} {res : Result} {db : DB}
|
||
(hcell : negCrossClampEndOne r res)
|
||
(_hlong : r.end_.key < r.begin.key)
|
||
(hcond : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞) ∨
|
||
(clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) ∧
|
||
res.keys.headD ([] : Key) = ([] : Key)))
|
||
(hlim : RangeRead.withinLimit r res.keys)
|
||
(hD : db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = res.keys) :
|
||
RangeRead.Compatible db r res := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
have hres_lt_E : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by
|
||
intro k hk
|
||
have hk' : k ∈ db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by
|
||
rw [hD]; exact hk
|
||
exact of_decide_eq_true (List.mem_filter.mp hk').2
|
||
have hnil : resolve db r.begin = (([] : Key) : WithTop Key) := by
|
||
rcases hcond with hcap | ⟨hcap, hheadnil⟩
|
||
· exact clampEndOne_resolve_begin_nil_of_cap_lt hcell_keep hD hres_lt_E hcap
|
||
· exact clampEndOne_resolve_begin_nil_of_cap_eq_nil hcell_keep hD hres_lt_E hcap hheadnil
|
||
have hself : (db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter
|
||
(fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) =
|
||
db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by
|
||
rw [hnil]
|
||
exact List.filter_eq_self.mpr (fun k _ => by
|
||
rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _)
|
||
refine ⟨hlim, Or.inr ⟨hmore, ?_⟩⟩
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend, hself, hD]
|
||
|
||
|
||
|
||
/-- **The `U = end_.key` long branch has its filter condition.** A compatible database
|
||
with a nonempty complete result stores exactly the reported keys below `end_.key`: a
|
||
positive begin anchor would force the begin resolution to consume exactly the slack, which
|
||
the capacity condition rules out (or, when the head is the empty key, contradicts that key
|
||
being minimal).
|
||
-/
|
||
theorem clampEndOne_filter_eq_of_compat {r : RangeRead} {res : Result} {db : DB}
|
||
(hcell : negCrossClampEndOne r res)
|
||
(hlong : r.end_.key < r.begin.key)
|
||
(hcond : clampEndOneCap r < (clampEndOneSlack r res : ℕ∞) ∨
|
||
(clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) ∧
|
||
res.keys.headD ([] : Key) = ([] : Key)))
|
||
(hcomp : RangeRead.Compatible db r res) :
|
||
db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = res.keys := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
set D : List Key := db.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))
|
||
with hDdef
|
||
have hDtake : D = db.keys.take (cnt db r.end_.key) := by
|
||
rw [hDdef, filter_lt_eq_take_countP db.sorted]
|
||
rfl
|
||
have hDlen : D.length = cnt db r.end_.key := by
|
||
rw [hDdef, cnt, List.countP_eq_length_filter]
|
||
have hDf : res.keys = D.filter
|
||
(fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) := by
|
||
rw [hres_eq, completeKeys_eq_filter_of_end_one hrev hend]
|
||
by_cases hbot : resolve db r.begin = (([] : Key) : WithTop Key)
|
||
· have hself : D.filter
|
||
(fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) = D := by
|
||
rw [hbot]
|
||
exact List.filter_eq_self.mpr (fun k _ => by
|
||
rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _)
|
||
rw [hself] at hDf
|
||
exact hDf.symm
|
||
· -- A genuine resolved begin key.
|
||
have hnot_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
|
||
intro htop
|
||
have hnil : D.filter
|
||
(fun k : Key => decide (resolve db r.begin ≤ ((k : Key) : WithTop Key))) = [] := by
|
||
rw [htop, List.filter_eq_nil_iff]
|
||
intro k _; simp
|
||
rw [hnil] at hDf
|
||
exact hne hDf
|
||
have hex : ∃ k ∈ db.keys, resolve db r.begin = ((k : Key) : WithTop Key) := by
|
||
rcases resolve_eq_nil_or_eq_top_or_mem (db := db) (s := r.begin) with h | h | h
|
||
· exact absurd h hbot
|
||
· exact absurd h hnot_top
|
||
· exact h
|
||
obtain ⟨k, hkdb, hkres⟩ := hex
|
||
have hanchor_nonneg : 0 ≤ r.begin.anchor db := by
|
||
by_contra hcon
|
||
exact hbot (resolve_eq_nil (lt_of_not_ge hcon))
|
||
have hget : db.keys[(r.begin.anchor db).toNat]? = some k := by
|
||
have hite := resolve_eq_ite db r.begin
|
||
rw [hite, ite_eq_right (not_lt.mpr hanchor_nonneg)] at hkres
|
||
cases h : db.keys[(r.begin.anchor db).toNat]? with
|
||
| none => rw [h] at hkres; exact absurd hkres (by simp)
|
||
| some k' =>
|
||
rw [h] at hkres
|
||
have hk' : k' = k := WithTop.coe_inj.mp hkres
|
||
simp [hk']
|
||
have hlt : (r.begin.anchor db).toNat < db.keys.length :=
|
||
List.getElem?_eq_some_iff.mp hget |>.1
|
||
have hk_eq : db.keys[(r.begin.anchor db).toNat] = k :=
|
||
List.getElem?_eq_some_iff.mp hget |>.2
|
||
-- `k` is the least reported key and lies strictly below `end_.key`.
|
||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
|
||
have hk_lt_E : ((k : Key) : WithTop Key) < r.end_.key := by
|
||
obtain ⟨x, hxmem⟩ := List.exists_mem_of_ne_nil _ hne
|
||
have hx' : x ∈ D.filter
|
||
(fun x : Key => decide (resolve db r.begin ≤ ((x : Key) : WithTop Key))) := by
|
||
rw [← hDf]; exact hxmem
|
||
have hxlt : ((x : Key) : WithTop Key) < r.end_.key :=
|
||
of_decide_eq_true (List.mem_filter.mp (List.mem_filter.mp hx').1).2
|
||
have hkx : ((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key) := by
|
||
rw [← hkres]
|
||
exact of_decide_eq_true (List.mem_filter.mp hx').2
|
||
exact lt_of_le_of_lt hkx hxlt
|
||
have hres_lt_E : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
have hx' : x ∈ D.filter
|
||
(fun x : Key => decide (resolve db r.begin ≤ ((x : Key) : WithTop Key))) := by
|
||
rw [← hDf]; exact hx
|
||
exact of_decide_eq_true (List.mem_filter.mp (List.mem_filter.mp hx').1).2
|
||
have hk_res : k ∈ res.keys := by
|
||
rw [hDf]
|
||
refine List.mem_filter.mpr ⟨?_, ?_⟩
|
||
· rw [hDdef]
|
||
exact List.mem_filter.mpr ⟨hkdb, by rw [decide_eq_true_eq]; exact hk_lt_E⟩
|
||
· rw [decide_eq_true_eq]; rw [hkres]
|
||
have hk_le_all : ∀ y ∈ res.keys, k ≤ y := by
|
||
intro y hy
|
||
have hy' : y ∈ D.filter
|
||
(fun x : Key => decide (resolve db r.begin ≤ ((x : Key) : WithTop Key))) := by
|
||
rw [← hDf]; exact hy
|
||
have hky := of_decide_eq_true (List.mem_filter.mp hy').2
|
||
rw [hkres] at hky
|
||
exact WithTop.coe_le_coe.mp hky
|
||
have hk_head : res.keys.head hne = k :=
|
||
le_antisymm (head_le_of_sortedLT' hsort hne hk_res)
|
||
(hk_le_all _ (List.head_mem hne))
|
||
-- The arithmetic of the genuine branch: the gap above `end_.key` has length `s`.
|
||
have hm_eq : reportedBelowBegin r res = res.keys.length :=
|
||
clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E
|
||
set cE : ℕ := cnt db r.end_.key with hcE
|
||
set cB : ℕ := cnt db r.begin.key with hcB
|
||
set g : ℕ := (between db r.end_.key r.begin.key).length with hg
|
||
set a : ℕ := (r.begin.anchor db).toNat with ha
|
||
have hDlen' : D.length = cE := by
|
||
rw [hDdef, hcE]; unfold cnt; rw [List.countP_eq_length_filter]
|
||
have hDsorted : D.SortedLT := by
|
||
rw [hDdef]
|
||
exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
have hcB_eq : cB = cE + g := by
|
||
rw [hcB, hcE, hg, cnt_eq_add_between db (le_of_lt hlong)]
|
||
have hca : db.keys.countP (fun x : Key => decide (x < k)) = a :=
|
||
countP_lt_eq_of_getElem db.sorted hlt hk_eq
|
||
have hcaW : db.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) = a := by
|
||
rw [countP_lt_coe k]
|
||
exact hca
|
||
have hDa : D.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) = a := by
|
||
have hfilt : D.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) =
|
||
db.keys.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < ((k : Key) : WithTop Key))) := by
|
||
rw [hDdef, List.filter_filter]
|
||
apply List.filter_congr
|
||
intro x _
|
||
by_cases hxk : ((x : Key) : WithTop Key) < ((k : Key) : WithTop Key)
|
||
· have hxE : ((x : Key) : WithTop Key) < r.end_.key := lt_trans hxk hk_lt_E
|
||
simp [hxk, hxE]
|
||
· simp [hxk]
|
||
rw [List.countP_eq_length_filter, hfilt, ← List.countP_eq_length_filter]
|
||
exact hcaW
|
||
have ha_cast : (a : ℤ) = (cB : ℤ) - (beginSlack r : ℤ) - 1 := by
|
||
rw [ha]
|
||
rw [Int.toNat_of_nonneg hanchor_nonneg]
|
||
rw [hcB]
|
||
exact anchor_eq_count_sub_slack db r (le_of_lt hbegin)
|
||
have ha_lt_cE : a < cE := by
|
||
have hlt' : ((k : Key) : WithTop Key) < r.end_.key := hk_lt_E
|
||
cases hE : r.end_.key with
|
||
| top =>
|
||
rw [hE] at hlong
|
||
exact absurd hlong (not_lt.mpr le_top)
|
||
| coe E =>
|
||
rw [hcE, hE]; unfold cnt; rw [countP_lt_coe]
|
||
have hkE : k < E := WithTop.coe_lt_coe.mp (by rw [hE] at hlt'; exact hlt')
|
||
exact lt_countP_of_getElem_lt db.sorted hlt (by rw [hk_eq]; exact hkE)
|
||
have hdropk : D.filter
|
||
(fun x : Key => decide (((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key))) = D.drop a
|
||
:= by
|
||
have h := filter_not_lt_eq_drop_countP hDsorted ((k : Key) : WithTop Key)
|
||
rw [hDa] at h
|
||
rw [← h]
|
||
apply List.filter_congr
|
||
intro x _
|
||
rw [decide_eq_decide]
|
||
exact not_lt.symm
|
||
have hres_drop : res.keys = D.drop a := by
|
||
have hDf_k : res.keys = D.filter
|
||
(fun x : Key => decide (((k : Key) : WithTop Key) ≤ ((x : Key) : WithTop Key))) := by
|
||
rw [hDf]
|
||
apply List.filter_congr
|
||
intro x _
|
||
rw [hkres, decide_eq_decide]
|
||
rw [hDf_k, hdropk]
|
||
have hm_len : reportedBelowBegin r res = cE - a := by
|
||
rw [hm_eq, hres_drop, List.length_drop, hDlen']
|
||
have hm_add_a : reportedBelowBegin r res + a = cE := by
|
||
rw [hm_len]
|
||
exact Nat.sub_add_cancel (le_of_lt ha_lt_cE)
|
||
have hgs : clampEndOneSlack r res = g := by
|
||
unfold clampEndOneSlack
|
||
have h1 : ((reportedBelowBegin r res + a : ℕ) : ℤ) = (cE : ℤ) := by
|
||
exact_mod_cast hm_add_a
|
||
have h2 : (cB : ℤ) = (cE : ℤ) + (g : ℤ) := by exact_mod_cast hcB_eq
|
||
omega
|
||
rcases hcond with hcap | ⟨hcap, hheadnil⟩
|
||
· have hgle : (g : ℕ∞) ≤ clampEndOneCap r := by
|
||
rw [hg]; unfold clampEndOneCap
|
||
exact between_length_le_intervalCard (db := db) (le_of_lt hlong)
|
||
rw [hgs] at hcap
|
||
exact absurd hcap (not_lt.mpr hgle)
|
||
· have hheadk : res.keys.headD ([] : Key) = k := by
|
||
have h0 : res.keys.headD ([] : Key) = res.keys.head hne := by
|
||
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
|
||
exact h0.trans hk_head
|
||
have hknil : k = [] := hheadk.symm.trans hheadnil
|
||
exfalso
|
||
exact hbot (by rw [hkres, hknil])
|
||
|
||
|
||
|
||
/-- If the upper endpoint of the clamp end-one cell is `end_.key`, the capacity
|
||
condition fails: either the gap is too short, or it is exactly the slack with an
|
||
empty-key head. -/
|
||
theorem clampEndOne_neg_cond {r : RangeRead} {res : Result}
|
||
(hU : clampEndOneUpper r res ≠ r.begin.key) :
|
||
clampEndOneCap r < (clampEndOneSlack r res : ℕ∞) ∨
|
||
(clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) ∧
|
||
res.keys.headD ([] : Key) = ([] : Key)) := by
|
||
by_cases hle : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r
|
||
· by_cases hstrict : (clampEndOneSlack r res : ℕ∞) < clampEndOneCap r
|
||
· exfalso
|
||
apply hU
|
||
unfold clampEndOneUpper
|
||
rw [ite_eq_left ⟨hle, Or.inl hstrict⟩]
|
||
· by_cases hhead : res.keys.headD ([] : Key) ≠ ([] : Key)
|
||
· exfalso
|
||
apply hU
|
||
unfold clampEndOneUpper
|
||
rw [ite_eq_left ⟨hle, Or.inr hhead⟩]
|
||
· exact Or.inr ⟨le_antisymm (not_lt.mp hstrict) hle, not_not.mp hhead⟩
|
||
· exact Or.inl (not_le.mp hle)
|
||
|
||
/-- The clamp end-one cell's closed form is sound. -/
|
||
theorem sound_clampEndOne {r : RangeRead} {res : Result}
|
||
(hcell : negCrossClampEndOne r res) :
|
||
Sound r res (closedFormNegCrossClampEndOne r res) := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
have step : ∀ db₁ db₂, AgreeOn (closedFormNegCrossClampEndOne r res) db₁ db₂ →
|
||
RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by
|
||
intro db₁ db₂ hagree hcomp₁
|
||
have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1
|
||
by_cases hU : clampEndOneUpper r res = r.begin.key
|
||
· have hEB := clampEndOne_end_le_begin_of_upper_begin hcell_keep hU
|
||
have hagree' : AgreeOn ⟨(([] : Key) : WithTop Key), r.begin.key⟩ db₁ db₂ := by
|
||
simpa [closedFormNegCrossClampEndOne, hU] using hagree
|
||
have hck := completeKeys_eq_of_agree_cover (r := r) (E := r.begin.key)
|
||
hrev hend (le_of_lt hbegin) (le_refl _) hEB hagree'
|
||
exact (RangeRead.compatible_congr hck).mp hcomp₁
|
||
· have hU' : clampEndOneUpper r res = r.end_.key := by
|
||
unfold clampEndOneUpper at hU ⊢
|
||
by_cases hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧
|
||
((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨
|
||
res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
· rw [ite_eq_left hP] at hU
|
||
exact absurd rfl hU
|
||
· rw [ite_eq_right hP] at hU ⊢
|
||
have hagree' : AgreeOn ⟨(([] : Key) : WithTop Key), r.end_.key⟩ db₁ db₂ := by
|
||
simpa [closedFormNegCrossClampEndOne, hU'] using hagree
|
||
by_cases hshort : r.begin.key ≤ r.end_.key
|
||
· have hck := completeKeys_eq_of_agree_cover (r := r) (E := r.end_.key)
|
||
hrev hend (le_of_lt hbegin) hshort (le_refl _) hagree'
|
||
exact (RangeRead.compatible_congr hck).mp hcomp₁
|
||
· have hlong : r.end_.key < r.begin.key := lt_of_not_ge hshort
|
||
have hcond := clampEndOne_neg_cond (r := r) (res := res) hU
|
||
have hD₁ := clampEndOne_filter_eq_of_compat hcell_keep hlong hcond hcomp₁
|
||
have hD₂ : db₂.keys.filter
|
||
(fun k : Key => decide ((k : WithTop Key) < r.end_.key)) = res.keys := by
|
||
have h := hagree'
|
||
rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h
|
||
exact h.symm.trans hD₁
|
||
exact clampEndOne_compat_of_filter hcell_keep hlong hcond hlim hD₂
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
|
||
|
||
|
||
/-- In the clamp end-one cell, the reported keys are all strictly below `end_.key`. -/
|
||
theorem clampEndOne_reported_lt_end {r : RangeRead} {res : Result} {db : DB}
|
||
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
|
||
(hmore : res.more = false) (hend : r.end_.offset = 1) :
|
||
∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by
|
||
have hres_eq : res.keys = RangeRead.completeKeys db r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· simp [hmore] at hm
|
||
· exact heq
|
||
intro k hk
|
||
rw [hres_eq, completeKeys_eq_filter_of_end_one hrev hend] at hk
|
||
exact of_decide_eq_true (List.mem_filter.mp (List.mem_filter.mp hk).1).2
|
||
|
||
/-- The database consisting of exactly the reported keys has a clamping begin
|
||
selector and is compatible. -/
|
||
theorem clampEndOne_res_db_compat {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res) :
|
||
∃ db₀ : DB, db₀.keys = res.keys ∧
|
||
RangeRead.Compatible db₀ r res ∧ r.begin.anchor db₀ < 0 := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
have hall := clampEndOne_reported_lt_end hcomp hrev hmore hend
|
||
have hs : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
|
||
have hanchor : r.begin.anchor ⟨res.keys, hs⟩ < 0 := by
|
||
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin)]
|
||
have hcount : res.keys.countP
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.begin.key))
|
||
= reportedBelowBegin r res := rfl
|
||
have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
|
||
rw [hcount]; omega
|
||
refine ⟨⟨res.keys, hs⟩, rfl, ?_, hanchor⟩
|
||
have hD : res.keys.filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key)) = res.keys :=
|
||
List.filter_eq_self.mpr (fun k hk => by rw [decide_eq_true_eq]; exact hall k hk)
|
||
have hnil : resolve ⟨res.keys, hs⟩ r.begin = (([] : Key) : WithTop Key) :=
|
||
resolve_eq_nil hanchor
|
||
refine ⟨hcomp.1, Or.inr ⟨hmore, ?_⟩⟩
|
||
show res.keys = RangeRead.completeKeys ⟨res.keys, hs⟩ r
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend]
|
||
change res.keys = (res.keys.filter
|
||
(fun k : Key => decide (((k : Key) : WithTop Key) < r.end_.key))).filter
|
||
(fun k : Key => decide (resolve ⟨res.keys, hs⟩ r.begin ≤ ((k : Key) : WithTop Key)))
|
||
rw [hD, hnil]
|
||
exact (List.filter_eq_self.mpr (fun k _ => by
|
||
rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _)).symm
|
||
|
||
/-- Every key strictly below `end_.key` is necessary in the clamp end-one cell. -/
|
||
theorem clampEndOne_necessary_below_end {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res)
|
||
{k : Key} (hk : ((k : Key) : WithTop Key) < r.end_.key) : Necessary r res k := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
set head : Key := res.keys.head hne with hhead
|
||
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
|
||
have hlast_mem : res.keys.getLast hne ∈ res.keys := List.getLast_mem hne
|
||
by_cases hle : head ≤ k
|
||
· by_cases hklast : k ≤ res.keys.getLast hne
|
||
· exact necessary_of_between_result hread hhead_mem hlast_mem hle hklast
|
||
· intro cr hs
|
||
exact necessary_last_to_end_key hread hrev hmore hne (le_of_eq hend) cr hs
|
||
(le_of_lt (lt_of_not_ge hklast)) hk
|
||
· have hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key) := by
|
||
rw [← hhead]
|
||
exact WithTop.coe_lt_coe.mpr (not_le.mp hle)
|
||
obtain ⟨db₀, hkeys, hcomp₀, hanchor⟩ := clampEndOne_res_db_compat hread hcell_keep
|
||
exact necessary_below_head_of_anchor_nonpos_negCross db₀ hcomp₀ hrev hne hanchor
|
||
(by rw [← hhead]; exact WithTop.coe_lt_coe.mpr (not_le.mp hle))
|
||
|
||
|
||
|
||
/-- A database whose keys are the reported keys followed by a sorted list `L` of
|
||
`s = clampEndOneSlack` gap keys is compatible, with begin anchor `0`. -/
|
||
theorem clampEndOne_gap_db {r : RangeRead} {res : Result} {L : List Key}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res)
|
||
(hlong : r.end_.key < r.begin.key)
|
||
(hLsorted : L.SortedLT)
|
||
(hLlen : L.length = clampEndOneSlack r res)
|
||
(hLlo : ∀ x ∈ L, r.end_.key ≤ ((x : Key) : WithTop Key))
|
||
(hLhi : ∀ x ∈ L, ((x : Key) : WithTop Key) < r.begin.key) :
|
||
∃ dbA : DB, dbA.keys = res.keys ++ L ∧ RangeRead.Compatible dbA r res ∧
|
||
r.begin.anchor dbA = 0 := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend
|
||
have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
|
||
have hcross : ∀ a ∈ res.keys, ∀ b ∈ L, a < b := by
|
||
intro a ha b hb
|
||
exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (hres_lt_E a ha) (hLlo b hb))
|
||
have hAppSorted : (res.keys ++ L).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hres_sorted, hLsorted, hcross⟩
|
||
have hm : reportedBelowBegin r res = res.keys.length :=
|
||
clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E
|
||
have hD : (res.keys ++ L).filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys := by
|
||
rw [List.filter_append]
|
||
have h1 : res.keys.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys :=
|
||
List.filter_eq_self.mpr (fun x hx => by rw [decide_eq_true_eq]; exact hres_lt_E x hx)
|
||
have h2 : L.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hLlo x hx)
|
||
rw [h1, h2, List.append_nil]
|
||
have hcount : (res.keys ++ L).countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length + L.length := by
|
||
rw [List.countP_append]
|
||
have h1 : res.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = res.keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong)
|
||
have h2 : L.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = L.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hLhi x hx
|
||
rw [h1, h2]
|
||
have hanchor : r.begin.anchor ⟨res.keys ++ L, hAppSorted⟩ = 0 := by
|
||
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount, hLlen]
|
||
have hsdef : clampEndOneSlack r res = beginSlack r + 1 - reportedBelowBegin r res := rfl
|
||
rw [hsdef, hm]
|
||
have hlen_le : res.keys.length ≤ beginSlack r + 1 := by
|
||
rw [← hm]; exact le_trans hclamp (Nat.le_succ _)
|
||
have hsimp : res.keys.length + (beginSlack r + 1 - res.keys.length) = beginSlack r + 1 := by
|
||
omega
|
||
rw [hsimp]
|
||
push_cast
|
||
ring
|
||
have hresolve : resolve ⟨res.keys ++ L, hAppSorted⟩ r.begin =
|
||
((res.keys.head hne : Key) : WithTop Key) := by
|
||
rw [resolve_eq_ite, ite_eq_right (by rw [hanchor]; exact not_lt.mpr (le_refl 0))]
|
||
rw [show (r.begin.anchor ⟨res.keys ++ L, hAppSorted⟩).toNat = 0 by rw [hanchor]; rfl]
|
||
have hget : (res.keys ++ L)[0]? = some (res.keys.head hne) := by
|
||
rw [← List.head?_eq_getElem?, List.head?_append, List.head?_eq_some_head hne]
|
||
rfl
|
||
rw [hget]
|
||
refine ⟨⟨res.keys ++ L, hAppSorted⟩, rfl, ?_, hanchor⟩
|
||
refine ⟨hcomp.1, Or.inr ⟨hmore, ?_⟩⟩
|
||
show res.keys = RangeRead.completeKeys ⟨res.keys ++ L, hAppSorted⟩ r
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend]
|
||
change res.keys = ((res.keys ++ L).filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key))).filter
|
||
(fun x : Key => decide (resolve ⟨res.keys ++ L, hAppSorted⟩ r.begin ≤
|
||
((x : Key) : WithTop Key)))
|
||
rw [hD, hresolve]
|
||
exact (List.filter_eq_self.mpr (fun x hx => by
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hres_sorted hne hx))).symm
|
||
|
||
|
||
|
||
/-- A database whose keys are the reported keys followed by `s+1` gap keys has
|
||
begin anchor `1` and is incompatible: the least reported key falls out of the
|
||
complete window. -/
|
||
theorem clampEndOne_succ_gap_not_compat {r : RangeRead} {res : Result} {Lb : List Key} {dbB : DB}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res)
|
||
(hlong : r.end_.key < r.begin.key)
|
||
(hkeys : dbB.keys = res.keys ++ Lb)
|
||
(hLblo : ∀ x ∈ Lb, r.end_.key ≤ ((x : Key) : WithTop Key))
|
||
(hLbhi : ∀ x ∈ Lb, ((x : Key) : WithTop Key) < r.begin.key)
|
||
(hLblen : Lb.length = clampEndOneSlack r res + 1) :
|
||
¬ RangeRead.Compatible dbB r res := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend
|
||
have hm : reportedBelowBegin r res = res.keys.length :=
|
||
clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E
|
||
have hD : dbB.keys.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys := by
|
||
rw [hkeys, List.filter_append]
|
||
have h1 : res.keys.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = res.keys :=
|
||
List.filter_eq_self.mpr (fun x hx => by rw [decide_eq_true_eq]; exact hres_lt_E x hx)
|
||
have h2 : Lb.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hLblo x hx)
|
||
rw [h1, h2, List.append_nil]
|
||
have hcount : dbB.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length + Lb.length := by
|
||
rw [hkeys, List.countP_append]
|
||
have h1 : res.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = res.keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong)
|
||
have h2 : Lb.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = Lb.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hLbhi x hx
|
||
rw [h1, h2]
|
||
have hanchor : r.begin.anchor dbB = 1 := by
|
||
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount]
|
||
unfold clampEndOneSlack at hLblen
|
||
rw [hLblen, hm]
|
||
have hsimp : res.keys.length + (beginSlack r + 1 - res.keys.length + 1) =
|
||
beginSlack r + 2 := by omega
|
||
rw [hsimp]
|
||
push_cast
|
||
ring
|
||
have hlenpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne
|
||
have h1lt : (1 : Nat) < dbB.keys.length := by
|
||
rw [hkeys, List.length_append, hLblen]
|
||
omega
|
||
have h0 : dbB.keys[0] = res.keys.head hne := by
|
||
have h0? : dbB.keys[0]? = some (res.keys.head hne) := by
|
||
rw [hkeys, List.getElem?_append_left hlenpos, ← List.head?_eq_getElem?,
|
||
List.head?_eq_some_head hne]
|
||
have hlen0 : 0 < dbB.keys.length := by
|
||
rw [hkeys, List.length_append]; omega
|
||
rw [List.getElem?_eq_getElem hlen0] at h0?
|
||
exact Option.some.inj h0?
|
||
have h1gt : ((dbB.keys[0] : Key) : WithTop Key) < ((dbB.keys[1] : Key) : WithTop Key) :=
|
||
WithTop.coe_lt_coe.mpr
|
||
((dbB.sorted.getElem_lt_getElem_iff).mpr (by omega : (0 : Nat) < 1))
|
||
have hresolve : resolve dbB r.begin = ((dbB.keys[1] : Key) : WithTop Key) := by
|
||
have hanchor_toNat : (r.begin.anchor dbB).toNat = 1 := by rw [hanchor]; rfl
|
||
rw [resolve_eq_ite, ite_eq_right (by rw [hanchor]; norm_num),
|
||
hanchor_toNat, List.getElem?_eq_getElem h1lt]
|
||
intro hcomp
|
||
have hres_eq : res.keys = RangeRead.completeKeys dbB r := by
|
||
rcases hcomp.2 with ⟨hmt, _, _⟩ | ⟨-, heq⟩
|
||
· rw [hmore] at hmt; exact absurd hmt (by simp)
|
||
· exact heq
|
||
have hheadmem : res.keys.head hne ∈ res.keys := List.head_mem hne
|
||
have hnot : res.keys.head hne ∉ RangeRead.completeKeys dbB r := by
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend, hD, hresolve]
|
||
intro hmem
|
||
rw [List.mem_filter] at hmem
|
||
rw [decide_eq_true_eq] at hmem
|
||
have hlt : ((res.keys.head hne : Key) : WithTop Key) < ((dbB.keys[1] : Key) : WithTop Key) := by
|
||
rw [← h0]; exact h1gt
|
||
exact absurd hmem.2 (not_le.mpr hlt)
|
||
exact hnot (by rw [← hres_eq]; exact hheadmem)
|
||
|
||
|
||
|
||
/-- A database with an extra bottom key below the reported head, followed by the
|
||
reported keys and `s` gap keys, is compatible with begin anchor `1`. -/
|
||
theorem clampEndOne_gap_db_extra {r : RangeRead} {res : Result} {L : List Key}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res)
|
||
(hlong : r.end_.key < r.begin.key)
|
||
(hheadne : res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
(hLsorted : L.SortedLT)
|
||
(hLlen : L.length = clampEndOneSlack r res)
|
||
(hLlo : ∀ x ∈ L, r.end_.key ≤ ((x : Key) : WithTop Key))
|
||
(hLhi : ∀ x ∈ L, ((x : Key) : WithTop Key) < r.begin.key) :
|
||
∃ dbA : DB, dbA.keys = (([] : Key) :: res.keys) ++ L ∧
|
||
RangeRead.Compatible dbA r res ∧ r.begin.anchor dbA = 1 := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend
|
||
have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
|
||
have hhead_nil : res.keys.head hne ≠ ([] : Key) := by
|
||
rw [← clampEndOne_headD_eq (hne := hne)]; exact hheadne
|
||
have hnil_lt_head : ([] : Key) < res.keys.head hne := by
|
||
cases h : res.keys.head hne with
|
||
| nil => exact absurd h hhead_nil
|
||
| cons a t => exact List.nil_lt_cons a t
|
||
have hnil_lt_res : ∀ x ∈ res.keys, ([] : Key) < x := by
|
||
intro x hx
|
||
exact lt_of_lt_of_le hnil_lt_head (head_le_of_sortedLT' hres_sorted hne hx)
|
||
have hnil_lt_E : (([] : Key) : WithTop Key) < r.end_.key := by
|
||
have hh := hres_lt_E _ (List.head_mem hne)
|
||
have : ((([] : Key)) : WithTop Key) ≤ ((res.keys.head hne : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (le_of_lt hnil_lt_head)
|
||
exact lt_of_le_of_lt this hh
|
||
have hPreSorted : (([] : Key) :: res.keys).SortedLT := by
|
||
rw [List.sortedLT_iff_pairwise]
|
||
exact List.pairwise_cons.mpr ⟨fun a ha => hnil_lt_res a ha, hres_sorted.pairwise⟩
|
||
have hcross : ∀ a ∈ (([] : Key) :: res.keys), ∀ b ∈ L, a < b := by
|
||
intro a ha b hb
|
||
rcases List.mem_cons.mp ha with rfl | ha'
|
||
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hnil_lt_E (hLlo b hb))
|
||
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hres_lt_E a ha') (hLlo b hb))
|
||
have hAppSorted : ((([] : Key) :: res.keys) ++ L).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hPreSorted, hLsorted, hcross⟩
|
||
have hm : reportedBelowBegin r res = res.keys.length :=
|
||
clampEndOne_reported_eq_length hcell_keep (le_of_lt hlong) hres_lt_E
|
||
have hD : ((([] : Key) :: res.keys) ++ L).filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) =
|
||
([] : Key) :: res.keys := by
|
||
rw [List.filter_append]
|
||
have h1 : (([] : Key) :: res.keys).filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) =
|
||
([] : Key) :: res.keys := by
|
||
rw [List.filter_cons_of_pos (by rw [decide_eq_true_eq]; exact hnil_lt_E)]
|
||
rw [List.filter_eq_self.mpr (fun x hx => by
|
||
rw [decide_eq_true_eq]; exact hres_lt_E x hx)]
|
||
have h2 : L.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hLlo x hx)
|
||
rw [h1, h2, List.append_nil]
|
||
have hcount : ((([] : Key) :: res.keys) ++ L).countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
1 + res.keys.length + L.length := by
|
||
rw [List.countP_append, List.countP_cons_of_pos]
|
||
· have h1 : res.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
res.keys.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact lt_of_lt_of_le (hres_lt_E x hx) (le_of_lt hlong)
|
||
have h2 : L.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = L.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact hLhi x hx
|
||
rw [h1, h2]
|
||
omega
|
||
· rw [decide_eq_true_eq]
|
||
exact lt_of_lt_of_le hnil_lt_E (le_of_lt hlong)
|
||
have hanchor : r.begin.anchor ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ = 1 := by
|
||
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin), hcount]
|
||
unfold clampEndOneSlack at hLlen
|
||
rw [hLlen, hm]
|
||
have hsimp : 1 + res.keys.length + (beginSlack r + 1 - res.keys.length) =
|
||
beginSlack r + 2 := by omega
|
||
rw [hsimp]
|
||
push_cast
|
||
ring
|
||
have hresolve : resolve ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ r.begin =
|
||
((res.keys.head hne : Key) : WithTop Key) := by
|
||
have hanchor_toNat :
|
||
(r.begin.anchor ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩).toNat = 1 := by
|
||
rw [hanchor]; rfl
|
||
have hlen1 : (1 : Nat) < ((([] : Key) :: res.keys) ++ L).length := by
|
||
have hpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne
|
||
rw [List.length_append, List.length_cons]
|
||
omega
|
||
have hget1 : ((([] : Key) :: res.keys) ++ L)[1] = res.keys.head hne := by
|
||
have h1? : ((([] : Key) :: res.keys) ++ L)[1]? = some (res.keys.head hne) := by
|
||
rw [List.getElem?_append_left (by simp [List.length_pos_of_ne_nil hne])]
|
||
rw [List.getElem?_cons_succ, List.getElem?_eq_getElem (List.length_pos_of_ne_nil hne)]
|
||
exact congrArg some (List.head_eq_getElem hne).symm
|
||
rw [List.getElem?_eq_getElem hlen1] at h1?
|
||
exact Option.some.inj h1?
|
||
rw [resolve_eq_ite, ite_eq_right (by rw [hanchor]; norm_num), hanchor_toNat,
|
||
List.getElem?_eq_getElem hlen1, hget1]
|
||
refine ⟨⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩, rfl, ?_, hanchor⟩
|
||
refine ⟨hcomp.1, Or.inr ⟨hmore, ?_⟩⟩
|
||
show res.keys = RangeRead.completeKeys ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ r
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend]
|
||
change res.keys = (((([] : Key) :: res.keys) ++ L).filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key))).filter
|
||
(fun x : Key => decide (resolve ⟨(([] : Key) :: res.keys) ++ L, hAppSorted⟩ r.begin ≤
|
||
((x : Key) : WithTop Key)))
|
||
rw [hD, hresolve]
|
||
have hstep : ((([] : Key) :: res.keys).filter
|
||
(fun x : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤
|
||
((x : Key) : WithTop Key)))) = res.keys := by
|
||
rw [List.filter_cons_of_neg (by
|
||
rw [decide_eq_true_eq]; exact not_le.mpr (WithTop.coe_lt_coe.mpr hnil_lt_head))]
|
||
exact List.filter_eq_self.mpr (fun x hx => by
|
||
rw [decide_eq_true_eq]
|
||
exact WithTop.coe_le_coe.mpr (head_le_of_sortedLT' hres_sorted hne hx))
|
||
rw [hstep]
|
||
|
||
|
||
|
||
/-- Inserting a key larger than every element of a list inserts it into the suffix
|
||
after that list, when appending another list. -/
|
||
theorem orderedInsert_append_of_all_lt {l₁ l₂ : List Key} {k : Key}
|
||
(h : ∀ a ∈ l₁, a < k) :
|
||
(l₁ ++ l₂).orderedInsert (· ≤ ·) k = l₁ ++ l₂.orderedInsert (· ≤ ·) k := by
|
||
induction l₁ with
|
||
| nil => simp
|
||
| cons a t ih =>
|
||
have ha : a < k := h a (by simp)
|
||
have ht : ∀ b ∈ t, b < k := fun b hb => h b (by simp [hb])
|
||
rw [List.cons_append, List.orderedInsert_cons]
|
||
rw [ite_eq_right (not_le.mpr ha)]
|
||
rw [ih ht, List.cons_append]
|
||
|
||
/-- Every gap key is necessary in the clamp end-one cell when the gap is strictly
|
||
larger than the slack. -/
|
||
theorem clampEndOne_necessary_gap_of_cap_lt {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res)
|
||
(hlt : (clampEndOneSlack r res : ℕ∞) < clampEndOneCap r)
|
||
(hkE : r.end_.key ≤ (k : WithTop Key))
|
||
(hkB : (k : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
have hread_keep : Readable r res := hread
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
have hlong : r.end_.key < r.begin.key := by
|
||
by_contra h
|
||
have hle : r.begin.key ≤ r.end_.key := le_of_not_gt h
|
||
have h0 : clampEndOneCap r = 0 := intervalCard_eq_zero_of_le hle
|
||
rw [h0] at hlt
|
||
exact not_lt.mpr (zero_le) hlt
|
||
have hcard : ((clampEndOneSlack r res + 1 : ℕ) : ℕ∞) ≤ clampEndOneCap r := by
|
||
rw [Nat.cast_add, Nat.cast_one]
|
||
exact (ENat.add_one_le_iff (by simp)).mpr hlt
|
||
obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ :=
|
||
exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k)
|
||
(m := clampEndOneSlack r res) hcard
|
||
have hLlo : ∀ x ∈ dbJ.keys, r.end_.key ≤ ((x : Key) : WithTop Key) :=
|
||
fun x hx => (hJrange x hx).1
|
||
have hLhi : ∀ x ∈ dbJ.keys, ((x : Key) : WithTop Key) < r.begin.key :=
|
||
fun x hx => (hJrange x hx).2
|
||
obtain ⟨dbA, hAkeys, hAcomp, hAanchor⟩ :=
|
||
clampEndOne_gap_db hread_keep hcell_keep hlong dbJ.sorted hJlen hLlo hLhi
|
||
have hres_lt_E := clampEndOne_reported_lt_end hcomp hrev hmore hend
|
||
have hkres : k ∉ res.keys := by
|
||
intro hk
|
||
exact absurd hkE (not_le.mpr (hres_lt_E k hk))
|
||
have hkA : k ∉ dbA.keys := by
|
||
rw [hAkeys]
|
||
intro hk
|
||
rcases List.mem_append.mp hk with hk' | hk'
|
||
· exact hkres hk'
|
||
· exact hkJ hk'
|
||
have hall_lt : ∀ a ∈ res.keys, a < k := by
|
||
intro a ha
|
||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hres_lt_E a ha) hkE)
|
||
let Lb : List Key := dbJ.keys.orderedInsert (· ≤ ·) k
|
||
have hBkeys : (toggle dbA k).keys = res.keys ++ Lb := by
|
||
rw [toggle_eq_insert_of_not_mem hkA, DB.insert_keys, hAkeys,
|
||
orderedInsert_append_of_all_lt hall_lt]
|
||
have hLblo : ∀ x ∈ Lb, r.end_.key ≤ ((x : Key) : WithTop Key) := by
|
||
intro x hx
|
||
rw [List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hkE
|
||
· exact hLlo x hx
|
||
have hLbhi : ∀ x ∈ Lb, ((x : Key) : WithTop Key) < r.begin.key := by
|
||
intro x hx
|
||
rw [List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hkB
|
||
· exact hLhi x hx
|
||
have hLblen : Lb.length = clampEndOneSlack r res + 1 := by
|
||
simp [Lb, List.orderedInsert_length, hJlen]
|
||
have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res :=
|
||
clampEndOne_succ_gap_not_compat hread_keep hcell_keep hlong hBkeys hLblo hLbhi hLblen
|
||
exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩
|
||
|
||
|
||
|
||
/-- Counting a key of a list that satisfies the predicate adds one after erasure. -/
|
||
theorem countP_erase_add_one {l : List Key} {k : Key} {p : Key → Bool}
|
||
(hk : k ∈ l) (hp : p k = true) :
|
||
l.countP p = (l.erase k).countP p + 1 := by
|
||
have hpos : 0 < l.countP p :=
|
||
List.countP_pos_iff.mpr ⟨k, hk, hp⟩
|
||
have h := List.countP_erase p l k
|
||
rw [ite_eq_left ⟨hk, hp⟩] at h
|
||
omega
|
||
|
||
/-- A database with the extra bottom key and the full gap list, compatible with begin
|
||
anchor one, becomes incompatible after erasing a gap key: the resolution clamps back
|
||
to bottom and the extra bottom key enters the complete result. -/
|
||
theorem clampEndOne_erase_extra_not_compat {r : RangeRead} {res : Result} {dbA : DB}
|
||
{k : Key} {L : List Key}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res)
|
||
(_hlong : r.end_.key < r.begin.key)
|
||
(hheadne : res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
(hAkeys : dbA.keys = (([] : Key) :: res.keys) ++ L)
|
||
(hLlo : ∀ x ∈ L, r.end_.key ≤ ((x : Key) : WithTop Key))
|
||
(hLhi : ∀ x ∈ L, ((x : Key) : WithTop Key) < r.begin.key)
|
||
(hkL : k ∈ L)
|
||
(hAanchor : r.begin.anchor dbA = 1) :
|
||
¬ RangeRead.Compatible (dbA.erase k) r res := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
obtain ⟨db0, hcomp0⟩ := hread
|
||
have hres_lt_E := clampEndOne_reported_lt_end hcomp0 hrev hmore hend
|
||
have hhead : res.keys.head hne ≠ ([] : Key) := by
|
||
rw [← clampEndOne_headD_eq (hne := hne)]; exact hheadne
|
||
have hbot_lt_head : ([] : Key) < res.keys.head hne := by
|
||
cases h : res.keys.head hne with
|
||
| nil => exact absurd h hhead
|
||
| cons a t => exact List.nil_lt_cons a t
|
||
have hres_sorted : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp0 hrev
|
||
have hbot_lt_res : ∀ x ∈ res.keys, ([] : Key) < x :=
|
||
fun x hx => lt_of_lt_of_le hbot_lt_head (head_le_of_sortedLT' hres_sorted hne hx)
|
||
have hbot_lt_E : (([] : Key) : WithTop Key) < r.end_.key := by
|
||
have hh := hres_lt_E _ (List.head_mem hne)
|
||
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (le_of_lt hbot_lt_head)) hh
|
||
have hk_ge_E : r.end_.key ≤ ((k : Key) : WithTop Key) := hLlo k hkL
|
||
have hk_lt_B := hLhi k hkL
|
||
have hkres : k ∉ res.keys := by
|
||
intro hk
|
||
exact absurd hk_ge_E (not_le.mpr (hres_lt_E k hk))
|
||
have hkbot : k ≠ ([] : Key) := by
|
||
intro hbot
|
||
rw [hbot] at hk_ge_E
|
||
exact absurd hk_ge_E (not_le.mpr hbot_lt_E)
|
||
have hknotpre : k ∉ (([] : Key) :: res.keys) := by
|
||
intro hk
|
||
rcases List.mem_cons.mp hk with hk' | hk'
|
||
· exact hkbot hk'
|
||
· exact hkres hk'
|
||
have hkA : k ∈ dbA.keys := by rw [hAkeys]; exact List.mem_append_right _ hkL
|
||
have herase : (dbA.erase k).keys = (([] : Key) :: res.keys) ++ L.erase k := by
|
||
rw [DB.erase_keys, hAkeys, List.erase_append_right _ hknotpre]
|
||
have hfilterCons : ((([] : Key) :: res.keys)).filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) =
|
||
([] : Key) :: res.keys := by
|
||
rw [List.filter_cons_of_pos (by rw [decide_eq_true_eq]; exact hbot_lt_E)]
|
||
rw [List.filter_eq_self.mpr (fun x hx => by
|
||
rw [decide_eq_true_eq]; exact hres_lt_E x hx)]
|
||
have hfilterL : L.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hLlo x hx)
|
||
have hfilterEraseL : (L.erase k).filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact not_lt.mpr (hLlo x (List.erase_sublist.subset hx))
|
||
have hfilterErase : (dbA.erase k).keys.filter
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.end_.key)) =
|
||
([] : Key) :: res.keys := by
|
||
rw [herase, List.filter_append, hfilterCons, hfilterEraseL, List.append_nil]
|
||
have hp : (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) k = true := by
|
||
rw [decide_eq_true_eq]; exact hk_lt_B
|
||
have hcountrel : dbA.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||
(dbA.erase k).keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) + 1 :=
|
||
countP_erase_add_one hkA hp
|
||
have hcountA : ((dbA.keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key))) : ℤ) =
|
||
(beginSlack r : ℤ) + 2 := by
|
||
have h := anchor_eq_count_sub_slack dbA r (le_of_lt hbegin)
|
||
rw [hAanchor] at h
|
||
omega
|
||
have hanchorErase : r.begin.anchor (dbA.erase k) = 0 := by
|
||
rw [anchor_eq_count_sub_slack _ r (le_of_lt hbegin)]
|
||
have h2 : (((dbA.erase k).keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key))) : ℤ) =
|
||
(beginSlack r : ℤ) + 1 := by
|
||
have h3 : (((dbA.erase k).keys.countP
|
||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key))) : ℤ) + 1 =
|
||
(beginSlack r : ℤ) + 2 := by
|
||
rw [← hcountA]
|
||
exact_mod_cast hcountrel.symm
|
||
omega
|
||
omega
|
||
have hresolve : resolve (dbA.erase k) r.begin = (([] : Key) : WithTop Key) := by
|
||
have hlen : 0 < (dbA.erase k).keys.length := by
|
||
rw [herase]; exact List.length_pos_of_ne_nil (by simp)
|
||
have h0? : (dbA.erase k).keys[0]? = some (([] : Key) : Key) := by
|
||
rw [DB.erase_keys, hAkeys, List.erase_append_right _ hknotpre]
|
||
simp
|
||
rw [resolve_eq_ite, ite_eq_right (by rw [hanchorErase]; norm_num),
|
||
show (r.begin.anchor (dbA.erase k)).toNat = 0 by rw [hanchorErase]; rfl, h0?]
|
||
have hcomplete : RangeRead.completeKeys (dbA.erase k) r = ([] : Key) :: res.keys := by
|
||
rw [completeKeys_eq_filter_of_end_one hrev hend, hfilterErase, hresolve]
|
||
exact List.filter_eq_self.mpr (fun x _ => by
|
||
rw [decide_eq_true_eq]; exact coe_nil_le_key_withTop _)
|
||
have hne_cons : ([] : Key) :: res.keys ≠ res.keys := by
|
||
intro h
|
||
have hlen := congrArg List.length h
|
||
simp at hlen
|
||
intro hcomp
|
||
have hres_eq : res.keys = RangeRead.completeKeys (dbA.erase k) r := by
|
||
rcases hcomp.2 with ⟨hm, _, _⟩ | ⟨-, heq⟩
|
||
· rw [hmore] at hm
|
||
exact absurd hm (by simp)
|
||
· exact heq
|
||
rw [hcomplete] at hres_eq
|
||
exact hne_cons hres_eq.symm
|
||
|
||
|
||
|
||
/-- Every gap key is necessary in the clamp end-one cell when the gap exactly matches
|
||
the slack and the reported head is a genuine key. -/
|
||
theorem clampEndOne_necessary_gap_of_cap_eq {r : RangeRead} {res : Result} {k : Key}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res)
|
||
(hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞))
|
||
(hheadne : res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
(hkE : r.end_.key ≤ (k : WithTop Key))
|
||
(hkB : (k : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
have hcell_keep : negCrossClampEndOne r res := hcell
|
||
have hread_keep : Readable r res := hread
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
have hs_pos := clampEndOne_slack_pos hcell_keep
|
||
have hcappos : (0 : ℕ∞) < clampEndOneCap r := by
|
||
rw [hcap]; exact_mod_cast hs_pos
|
||
have hlong : r.end_.key < r.begin.key := by
|
||
by_contra h
|
||
have hle : r.begin.key ≤ r.end_.key := le_of_not_gt h
|
||
have h0 : clampEndOneCap r = 0 := intervalCard_eq_zero_of_le hle
|
||
rw [h0] at hcappos
|
||
exact not_lt.mpr (zero_le) hcappos
|
||
have hcard : ((clampEndOneSlack r res - 1 + 1 : ℕ) : ℕ∞) ≤ clampEndOneCap r := by
|
||
rw [Nat.sub_add_cancel (by omega : 1 ≤ clampEndOneSlack r res), hcap]
|
||
obtain ⟨dbJ, hJrange, hkJ, hJlen⟩ :=
|
||
exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k)
|
||
(m := clampEndOneSlack r res - 1) hcard
|
||
let dbL : DB := dbJ.insert k hkJ
|
||
have hLkeys : dbL.keys = dbJ.keys.orderedInsert (· ≤ ·) k := rfl
|
||
have hLsorted : dbL.keys.SortedLT := dbL.sorted
|
||
have hLlen : dbL.keys.length = clampEndOneSlack r res := by
|
||
rw [hLkeys, List.orderedInsert_length, hJlen]
|
||
omega
|
||
have hLlo : ∀ x ∈ dbL.keys, r.end_.key ≤ ((x : Key) : WithTop Key) := by
|
||
intro x hx
|
||
rw [hLkeys, List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hkE
|
||
· exact (hJrange x hx).1
|
||
have hLhi : ∀ x ∈ dbL.keys, ((x : Key) : WithTop Key) < r.begin.key := by
|
||
intro x hx
|
||
rw [hLkeys, List.mem_orderedInsert] at hx
|
||
rcases hx with rfl | hx
|
||
· exact hkB
|
||
· exact (hJrange x hx).2
|
||
have hkL : k ∈ dbL.keys := by
|
||
rw [hLkeys, List.mem_orderedInsert]
|
||
exact Or.inl rfl
|
||
obtain ⟨dbA, hAkeys, hAcomp, hAanchor⟩ :=
|
||
clampEndOne_gap_db_extra hread_keep hcell_keep hlong hheadne hLsorted hLlen hLlo hLhi
|
||
have hkA : k ∈ dbA.keys := by
|
||
rw [hAkeys]; exact List.mem_append_right _ hkL
|
||
have hnot : ¬ RangeRead.Compatible (toggle dbA k) r res := by
|
||
rw [toggle_eq_erase_of_mem hkA]
|
||
exact clampEndOne_erase_extra_not_compat hread_keep hcell_keep hlong hheadne
|
||
hAkeys hLlo hLhi hkL hAanchor
|
||
exact necessary_of_toggle ⟨dbA, hAcomp, hnot⟩
|
||
|
||
|
||
|
||
/-- The closed form for the crossed negative-begin complete clamp end-one cell is
|
||
minimal. -/
|
||
theorem minimal_closedForm_of_negCross_clampEndOne {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOne r res) :
|
||
Minimal r res (closedFormNegCrossClampEndOne r res) := by
|
||
rw [minimal_iff_sound_and_necessaryRange]
|
||
refine ⟨sound_clampEndOne hcell, ?_⟩
|
||
intro k hlo hhi
|
||
by_cases hU : clampEndOneUpper r res = r.begin.key
|
||
· have hkltB : (k : WithTop Key) < r.begin.key := by
|
||
simpa [closedFormNegCrossClampEndOne, hU] using hhi
|
||
by_cases hkE : (k : WithTop Key) < r.end_.key
|
||
· exact clampEndOne_necessary_below_end hread hcell hkE
|
||
· have hkE' : r.end_.key ≤ (k : WithTop Key) := le_of_not_gt hkE
|
||
have hEB : r.end_.key < r.begin.key := lt_of_le_of_lt hkE' hkltB
|
||
have hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧
|
||
((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨
|
||
res.keys.headD ([] : Key) ≠ ([] : Key)) := by
|
||
unfold clampEndOneUpper at hU
|
||
by_cases hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧
|
||
((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨
|
||
res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
· exact hP
|
||
· rw [ite_eq_right hP] at hU
|
||
exact absurd hU (ne_of_lt hEB)
|
||
by_cases hstrict : (clampEndOneSlack r res : ℕ∞) < clampEndOneCap r
|
||
· exact clampEndOne_necessary_gap_of_cap_lt hread hcell hstrict hkE' hkltB
|
||
· have hcap : clampEndOneCap r = (clampEndOneSlack r res : ℕ∞) :=
|
||
le_antisymm (not_lt.mp hstrict) hP.1
|
||
have hheadne : res.keys.headD ([] : Key) ≠ ([] : Key) := by
|
||
rcases hP.2 with hstrict' | hhead
|
||
· exact absurd hstrict' hstrict
|
||
· exact hhead
|
||
exact clampEndOne_necessary_gap_of_cap_eq hread hcell hcap hheadne hkE' hkltB
|
||
· have hU' : clampEndOneUpper r res = r.end_.key := by
|
||
unfold clampEndOneUpper at hU ⊢
|
||
by_cases hP : (clampEndOneSlack r res : ℕ∞) ≤ clampEndOneCap r ∧
|
||
((clampEndOneSlack r res : ℕ∞) < clampEndOneCap r ∨
|
||
res.keys.headD ([] : Key) ≠ ([] : Key))
|
||
· rw [ite_eq_left hP] at hU
|
||
exact absurd rfl hU
|
||
· rw [ite_eq_right hP] at hU ⊢
|
||
have hkE : (k : WithTop Key) < r.end_.key := by
|
||
simpa [closedFormNegCrossClampEndOne, hU'] using hhi
|
||
exact clampEndOne_necessary_below_end hread hcell hkE
|
||
|
||
|
||
/-- The reversed crossed negative-begin complete clamp cell at `end.offset = 1`. -/
|
||
abbrev negCrossClampEndOneRev (r : RangeRead) (res : Result) : Prop :=
|
||
r.reverse = true ∧ r.begin.offset < 0 ∧ res.more = false ∧ res.keys ≠ [] ∧
|
||
r.end_.offset = 1 ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
reportedBelowBegin r res ≤ beginSlack r
|
||
|
||
/-- The reversed closed form for the crossed negative-begin complete clamp cell at
|
||
`end.offset = 1`. -/
|
||
noncomputable def closedFormNegCrossClampEndOneRev (r : RangeRead) (res : Result) :
|
||
ConflictRange :=
|
||
closedFormNegCrossClampEndOne { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false }
|
||
|
||
/-- **The reversed crossed negative-begin complete clamp cell at `end.offset = 1` is
|
||
minimal**, by flipping the traversal. -/
|
||
theorem minimal_closedForm_of_negCross_clampEndOne_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hcell : negCrossClampEndOneRev r res) :
|
||
Minimal r res (closedFormNegCrossClampEndOneRev r res) := by
|
||
obtain ⟨hrev, hbegin, hmore, hne, hend, hcrossD, hclamp⟩ := hcell
|
||
have hne' : res.keys.reverse ≠ [] := by simpa using hne
|
||
have hheadD : res.keys.reverse.headD ([] : Key) = res.keys.getLastD ([] : Key) := by
|
||
rw [List.headD_eq_head?, List.getLastD_eq_getLast?, List.head?_reverse]
|
||
have hcount : reportedBelowBegin { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } = reportedBelowBegin r res := by
|
||
unfold reportedBelowBegin
|
||
exact List.Perm.countP_eq _ (List.reverse_perm res.keys)
|
||
have hcell' : negCrossClampEndOne { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
refine ⟨rfl, hbegin, rfl, hne', hend, ?_, ?_⟩
|
||
· rw [hheadD]; exact hcrossD
|
||
· rw [hcount]; exact hclamp
|
||
have hread' : Readable { r with reverse := false }
|
||
{ keys := res.keys.reverse, more := false } := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
exact ⟨db, (compatible_flip (db := db) (b := true) hrev hmore).mpr hcomp⟩
|
||
have hmin := minimal_closedForm_of_negCross_clampEndOne hread' hcell'
|
||
exact (minimal_flip (b := true) hrev hmore).mp hmin
|
||
|
||
|
||
end FormalConflictRange
|