- Vendor the FormalConflictRange reference library needed by the proof - Add Bridge.lean with simp lemmas connecting the Statement definitions to the reference library - Prove Statement.closedFormMinimal in Proof.lean - Register the new libraries in the Lake config
971 lines
51 KiB
Lean4
971 lines
51 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.NegCrossTruncRevCrossedLarge
|
||
|
||
/-!
|
||
# Reversed crossed negative-begin truncated cells at a nonpositive end offset
|
||
|
||
This module closes the last reversed truncated crossed cell: the end selector has
|
||
nonpositive offset, so the window ends at a stored key strictly below `end_.key`.
|
||
|
||
The setting is
|
||
|
||
* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`;
|
||
* the crossed selectors `end_.key < begin.key`;
|
||
* `end_.offset ≤ 0`.
|
||
|
||
Writing `L` for the reported last, `t = |res.keys|`, `p = (1 - end_.offset).toNat` and
|
||
`cap = intervalCard end_.key begin.key`, the minimal range is `[L, end_.key)` when
|
||
`cap + t + p ≤ beginSlack + 1` and `[L, begin.key)` otherwise. The pad count `p` is the
|
||
number of stored keys in `[resolve end_, end_.key)`, which the count identity
|
||
`between_last_end_key_length_eq_of_end_nonpos` adds to the reported keys.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
set_option maxHeartbeats 1200000
|
||
|
||
/-- **The reported keys are the stored keys in `[last, resolve end_)` read backwards.**
|
||
Unlike the end-offset-one version, this does not simplify the resolved end to `end_.key`;
|
||
it is the count of that window that the nonpositive-end count identity needs. -/
|
||
theorem res_keys_eq_reverse_between_resolve_end {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hne : res.keys ≠ []) :
|
||
res.keys = (between db ((res.keys.getLast hne : Key) : WithTop Key)
|
||
(resolve db r.end_)).reverse := by
|
||
set L : Key := res.keys.getLast hne with hLdef
|
||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||
have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys :=
|
||
(compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
|
||
have hLfil : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by
|
||
rw [hfil]; exact hLmem
|
||
have hLck : L ∈ RangeRead.completeKeys db r := (List.mem_filter.mp hLfil).1
|
||
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
|
||
(RangeRead.mem_completeKeys.mp hLck).2.1
|
||
have hid := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) (last := L) hrev hrb
|
||
rw [hfil] at hid
|
||
rw [hid]
|
||
congr 1
|
||
unfold between
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
conv_lhs => rw [← Bool.decide_and]
|
||
rw [decide_eq_decide]
|
||
constructor
|
||
· rintro ⟨hk, hL⟩
|
||
exact ⟨WithTop.coe_le_coe.mpr hL, hk⟩
|
||
· rintro ⟨hL, hk⟩
|
||
exact ⟨hk, WithTop.coe_le_coe.mp hL⟩
|
||
|
||
/-- **The stored keys in `[last, resolve end_)` number the reported keys.** Immediate
|
||
from the reversed-window identity. -/
|
||
theorem between_last_resolve_end_length {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hne : res.keys ≠ []) :
|
||
(between db ((res.keys.getLast hne : Key) : WithTop Key) (resolve db r.end_)).length =
|
||
res.keys.length := by
|
||
have h := res_keys_eq_reverse_between_resolve_end (db := db) hcomp hmore hrev hne
|
||
have h2 := congrArg List.length h
|
||
rw [List.length_reverse] at h2
|
||
exact h2.symm
|
||
|
||
/-- **The pad count is at most the end count.** A nonempty truncated result at a
|
||
nonpositive end offset has its end anchor at a nonnegative index, so the
|
||
`(1 - end_.offset)` padding keys fit below `end_.key`. -/
|
||
theorem pad_le_cnt_end_of_end_nonpos {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0) :
|
||
(1 - r.end_.offset).toNat ≤ cnt db r.end_.key := by
|
||
set p : ℕ := (1 - r.end_.offset).toNat with hpdef
|
||
have hpcast : (p : Int) = 1 - r.end_.offset := by
|
||
rw [hpdef, Int.toNat_of_nonneg (by omega)]
|
||
set c : ℕ := cnt db r.end_.key with hcdef
|
||
have hc_eq : (db.keys.countP
|
||
(fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))) = c := by
|
||
rw [hcdef]; rfl
|
||
have hanc : r.end_.anchor db = (c : Int) - (p : Int) := by
|
||
unfold Selector.anchor
|
||
rw [hc_eq, hpcast]
|
||
ring
|
||
have hnonnil : between db ((res.keys.getLast hne : Key) : WithTop Key)
|
||
(resolve db r.end_) ≠ [] := by
|
||
intro hnil
|
||
have h := res_keys_eq_reverse_between_resolve_end (db := db) hcomp hmore hrev hne
|
||
rw [hnil] at h
|
||
exact hne (by simpa using h.symm)
|
||
have hanchor_nonneg : 0 ≤ r.end_.anchor db := by
|
||
by_contra hneg
|
||
have := resolve_eq_nil (db := db) (s := r.end_) (lt_of_not_ge hneg)
|
||
rw [this] at hnonnil
|
||
apply hnonnil
|
||
unfold between
|
||
rw [List.filter_eq_nil_iff]
|
||
intro k _
|
||
rw [decide_eq_true_eq, not_and]
|
||
intro _
|
||
exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k))
|
||
have : (0 : Int) ≤ (c : Int) - (p : Int) := by rwa [← hanc]
|
||
omega
|
||
|
||
/-- **The count below the end resolution at a nonpositive end offset.** With
|
||
`c = cnt db end_.key` stored keys below `end_.key`, the end anchor is `c - p` where
|
||
`p = (1 - end_.offset).toNat`, and the resolved index is at least `0` because a
|
||
nonempty truncated result forces the resolution above the empty key. Hence
|
||
`cnt db (resolve end_) = c - p`. -/
|
||
theorem cnt_resolve_end_eq_of_end_nonpos {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0) :
|
||
cnt db (resolve db r.end_) = cnt db r.end_.key - (1 - r.end_.offset).toNat := by
|
||
set p : ℕ := (1 - r.end_.offset).toNat with hpdef
|
||
have hpcast : (p : Int) = 1 - r.end_.offset := by
|
||
rw [hpdef, Int.toNat_of_nonneg (by omega)]
|
||
set c : ℕ := cnt db r.end_.key with hcdef
|
||
have hc_eq : (db.keys.countP
|
||
(fun (k : Key) => decide ((↑k : WithTop Key) < r.end_.key))) = c := by
|
||
rw [hcdef]; rfl
|
||
have hanc : r.end_.anchor db = (c : Int) - (p : Int) := by
|
||
unfold Selector.anchor
|
||
rw [hc_eq, hpcast]
|
||
ring
|
||
-- The resolution is not the empty key: a nonempty reported window lies strictly
|
||
-- above it.
|
||
have hnonnil : between db ((res.keys.getLast hne : Key) : WithTop Key)
|
||
(resolve db r.end_) ≠ [] := by
|
||
intro hnil
|
||
have h := res_keys_eq_reverse_between_resolve_end (db := db) hcomp hmore hrev hne
|
||
rw [hnil] at h
|
||
exact hne (by simpa using h.symm)
|
||
have hanchor_nonneg : 0 ≤ r.end_.anchor db := by
|
||
by_contra hneg
|
||
have := resolve_eq_nil (db := db) (s := r.end_) (lt_of_not_ge hneg)
|
||
rw [this] at hnonnil
|
||
apply hnonnil
|
||
unfold between
|
||
rw [List.filter_eq_nil_iff]
|
||
intro k _
|
||
rw [decide_eq_true_eq, not_and]
|
||
intro _
|
||
exact not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k))
|
||
have hcge : p ≤ c := by
|
||
have : (0 : Int) ≤ (c : Int) - (p : Int) := by rwa [← hanc]
|
||
omega
|
||
have hmin : cnt db (resolve db r.end_) = min (r.end_.anchor db).toNat db.keys.length :=
|
||
countP_lt_resolve
|
||
have hclen : c ≤ db.keys.length := by rw [hcdef]; exact cnt_le_length db r.end_.key
|
||
have htoNat : (r.end_.anchor db).toNat = c - p := by
|
||
rw [hanc]; omega
|
||
rw [hmin, htoNat, Nat.min_eq_left (by omega)]
|
||
|
||
/-- **The nonpositive-end count identity.** For a compatible reversed truncated result
|
||
with `end_.offset ≤ 0`, the stored keys in `[last, end_.key)` are exactly the reported
|
||
keys together with the `(1 - end_.offset)` stored keys in `[resolve end_, end_.key)`. -/
|
||
theorem between_last_end_key_length_eq_of_end_nonpos {db : DB} {r : RangeRead} {res : Result}
|
||
(hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||
(hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset ≤ 0) :
|
||
(between db ((res.keys.getLast hne : Key) : WithTop Key) r.end_.key).length =
|
||
res.keys.length + (1 - r.end_.offset).toNat := by
|
||
set L : Key := res.keys.getLast hne with hLdef
|
||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||
have hLck : L ∈ RangeRead.completeKeys db r :=
|
||
(RangeRead.compatible_keys_prefix hcomp).subset hLmem
|
||
have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp hLck).2.2
|
||
have hLleRes : ((L : Key) : WithTop Key) ≤ resolve db r.end_ := le_of_lt hLres
|
||
have hresEnd_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
have hLleEnd : ((L : Key) : WithTop Key) ≤ r.end_.key := le_trans hLleRes hresEnd_le
|
||
have hresLen := between_last_resolve_end_length (db := db) hcomp hmore hrev hne
|
||
have hcntRes := cnt_resolve_end_eq_of_end_nonpos (db := db) hcomp hmore hrev hne hend
|
||
have hsplit := cnt_sub_eq_between_length (db := db) (E := ((L : Key) : WithTop Key))
|
||
(B := r.end_.key) hLleEnd
|
||
have hsplitRes := cnt_sub_eq_between_length (db := db) (E := ((L : Key) : WithTop Key))
|
||
(B := resolve db r.end_) hLleRes
|
||
rw [hresLen] at hsplitRes
|
||
have hmono : cnt db ((L : Key) : WithTop Key) ≤ cnt db (resolve db r.end_) :=
|
||
cnt_le_cnt_of_le db hLleRes
|
||
have hp_le := pad_le_cnt_end_of_end_nonpos (db := db) hcomp hmore hrev hne hend
|
||
omega
|
||
|
||
/-- **The stored keys in `[last, resolve end_)` are the first `t` keys of
|
||
`[last, end_.key)`.** When the end resolution lies at or below `end_.key`, filtering
|
||
`[last, end_.key)` at the resolution is a prefix of the sorted interval, and its length
|
||
is the number of reported keys. -/
|
||
theorem between_resolve_eq_take_of_end_le {db : DB} {r : RangeRead} {L : Key} {t : ℕ}
|
||
(hresEnd_le : resolve db r.end_ ≤ r.end_.key)
|
||
(hlen : (between db ((L : Key) : WithTop Key) (resolve db r.end_)).length = t) :
|
||
between db ((L : Key) : WithTop Key) (resolve db r.end_) =
|
||
(between db ((L : Key) : WithTop Key) r.end_.key).take t := by
|
||
set V : List Key := between db ((L : Key) : WithTop Key) r.end_.key with hV
|
||
have hVsorted : V.SortedLT := by
|
||
rw [hV]; unfold between
|
||
exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
|
||
have hfilter : V.filter (fun k : Key => decide ((k : WithTop Key) < resolve db r.end_)) =
|
||
between db ((L : Key) : WithTop Key) (resolve db r.end_) := by
|
||
rw [hV]; unfold between
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hres : ((k : WithTop Key) < resolve db r.end_)
|
||
· have hE : ((k : WithTop Key) < r.end_.key) := lt_of_lt_of_le hres hresEnd_le
|
||
simp [hres, hE]
|
||
· simp [hres, Bool.decide_and]
|
||
rw [← hfilter, filter_lt_eq_take_countP hVsorted]
|
||
congr 1
|
||
rw [List.countP_eq_length_filter, hfilter, hlen]
|
||
|
||
/-- **Necessity of the reversed truncated range at a nonpositive end offset.** Every key
|
||
in `[last reported, end_.key)` is necessary: reported keys by
|
||
`necessary_of_mem_result`, keys up to the head by convexity, and keys from the head to
|
||
`end_.key` by `necessary_head_to_end_key`. -/
|
||
theorem necessary_of_mem_truncatedRangeRev_of_end_nonpos {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hrev : r.reverse = true) (hne : res.keys ≠ [])
|
||
(hend : r.end_.offset ≤ 0) {k : Key}
|
||
(hkL : ((res.keys.getLast hne : Key) : WithTop Key) ≤ (k : WithTop Key))
|
||
(hkE : (k : WithTop Key) < r.end_.key) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
by_cases hkhead : k ≤ res.keys.head hne
|
||
· exact necessary_of_between_result ⟨db₀, hcomp₀⟩ (List.getLast_mem hne)
|
||
(List.head_mem hne) (WithTop.coe_le_coe.mp hkL) hkhead
|
||
· have hheadk : res.keys.head hne ≤ k := le_of_lt (lt_of_not_ge hkhead)
|
||
exact fun cr hs =>
|
||
necessary_head_to_end_key ⟨db₀, hcomp₀⟩ hrev hne (by omega) cr hs hheadk hkE
|
||
|
||
/-- **Reduction to the read whose begin selector is the reported last at offset one.** A
|
||
compatible database for the crossed negative-begin truncated read is compatible with the
|
||
reduced read: both see exactly the reported keys at or above the reported last. -/
|
||
theorem compatible_reduce_begin_last {r : RangeRead} {res : Result} {L : Key}
|
||
(hrev : r.reverse = true) (hmore : res.more = true) (hne : res.keys ≠ [])
|
||
(hLmem : L ∈ res.keys) (hLgetLast : L = res.keys.getLast hne) :
|
||
∀ db, RangeRead.Compatible db r res →
|
||
RangeRead.Compatible db
|
||
{ r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } res := by
|
||
intro db hcomp
|
||
have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys := by
|
||
have h := (compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
|
||
rw [← hLgetLast] at h
|
||
exact h
|
||
have hLfilt : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by
|
||
rw [hfil]; exact hLmem
|
||
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
|
||
(RangeRead.mem_completeKeys.mp (List.mem_filter.mp hLfilt).1).2.1
|
||
have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem
|
||
have hres' : resolve db ({ r with begin :=
|
||
({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } : RangeRead).begin =
|
||
((L : Key) : WithTop Key) :=
|
||
resolve_offset_one_key_of_mem hLdb
|
||
have hLck_filter : (RangeRead.completeKeys db
|
||
({ r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } :
|
||
RangeRead)).filter (fun k => decide (L ≤ k)) =
|
||
RangeRead.completeKeys db
|
||
({ r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } :
|
||
RangeRead) := by
|
||
rw [List.filter_eq_self]
|
||
intro k hk
|
||
rw [decide_eq_true_eq]
|
||
have hk' := (RangeRead.mem_completeKeys.mp hk).2.1
|
||
exact WithTop.coe_le_coe.mp (by rwa [hres'] at hk')
|
||
have hck' : RangeRead.completeKeys db
|
||
({ r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) } :
|
||
RangeRead) = res.keys := by
|
||
rw [← hLck_filter]
|
||
exact (completeKeys_filter_ge_eq_of_both_le (db := db) (r₁ := r)
|
||
(r₂ := { r with begin := ({ key := ((L : Key) : WithTop Key), offset := 1 } :
|
||
Selector) }) hrev (by simpa using hrev) (by simp) hrb (le_of_eq hres')).symm.trans hfil
|
||
refine ⟨by simpa [RangeRead.withinLimit] using hcomp.1, Or.inl ⟨hmore, ?_, hne⟩⟩
|
||
rw [hck']
|
||
|
||
/-- **Small-capacity soundness.** Under `cap + t + p ≤ beginSlack + 1`, agreement on
|
||
`[last, end_.key)` keeps the begin resolution at or below the reported last, so
|
||
compatibility transfers. -/
|
||
theorem sound_truncatedRangeRev_of_negCross_crossedNonpos_small {r : RangeRead}
|
||
{res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbeg : r.begin.offset < 0) (hne : res.keys ≠ [])
|
||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0)
|
||
(hcap : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) +
|
||
((1 - r.end_.offset).toNat : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1) :
|
||
Sound r res (truncatedRangeRev r (res.keys.getLast hne)) := by
|
||
set L : Key := res.keys.getLast hne with hLdef
|
||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||
have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross
|
||
have hLgetLast : L = res.keys.getLast hne := hLdef.symm
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
let r' : RangeRead :=
|
||
{ r with begin := { key := ((L : Key) : WithTop Key), offset := 1 } }
|
||
have hr'_rev : r'.reverse = true := by simp [r', hrev]
|
||
have hr'_end : r'.end_ = r.end_ := rfl
|
||
have hr'_beg : r'.begin = ({ key := ((L : Key) : WithTop Key), offset := 1 } : Selector) := rfl
|
||
have hr'_off : r'.begin.offset = 1 := rfl
|
||
have hreduce : ∀ db : DB, RangeRead.Compatible db r res → RangeRead.Compatible db r' res :=
|
||
compatible_reduce_begin_last hrev hmore hne hLmem hLgetLast
|
||
have hread' : Readable r' res := ⟨db₀, hreduce db₀ hcomp₀⟩
|
||
have hgetLastD : res.keys.getLastD ([] : Key) = L := by
|
||
rw [hLdef, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||
have hmin' : Minimal r' res (truncatedRangeRev r L) := by
|
||
have hm := minimal_truncatedRangeRev_of_end_offset_nonpos (r := r') hread' hmore
|
||
hr'_rev hr'_off hend
|
||
rwa [hgetLastD] at hm
|
||
have hS' : Sound r' res (truncatedRangeRev r L) := hmin'.sound
|
||
-- `truncatedRangeRev r' L` is definitionally `truncatedRangeRev r L`.
|
||
have step : ∀ db db', AgreeOn (truncatedRangeRev r L) db db' →
|
||
RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by
|
||
intro db db' hag' hcomp
|
||
have hcomp' : RangeRead.Compatible db r' res := hreduce db hcomp
|
||
have hcomp'' : RangeRead.Compatible db' r' res := (hS' db db' hag').mp hcomp'
|
||
have hbetweenEq' : between db ((L : Key) : WithTop Key) r.end_.key =
|
||
between db' ((L : Key) : WithTop Key) r.end_.key := by
|
||
have h := hag'
|
||
unfold AgreeOn ConflictRange.keys at h
|
||
unfold between
|
||
exact h
|
||
have hbetweenLen : (between db ((L : Key) : WithTop Key) r.end_.key).length =
|
||
res.keys.length + (1 - r.end_.offset).toNat :=
|
||
between_last_end_key_length_eq_of_end_nonpos (db := db) hcomp hmore hrev hne hend
|
||
have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp
|
||
((RangeRead.compatible_keys_prefix hcomp).subset hLmem)).2.2
|
||
have hresEnd_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
have hLE : ((L : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le hLres hresEnd_le
|
||
have hcapNat : (between db' r.end_.key r.begin.key).length + res.keys.length +
|
||
(1 - r.end_.offset).toNat ≤ beginSlack r + 1 := by
|
||
have hb : ((between db' r.end_.key r.begin.key).length : ℕ∞) ≤
|
||
intervalCard r.end_.key r.begin.key :=
|
||
between_length_le_intervalCard (le_of_lt hcrossE)
|
||
have hsum : (((between db' r.end_.key r.begin.key).length + res.keys.length +
|
||
(1 - r.end_.offset).toNat : ℕ) : ℕ∞) ≤ ((beginSlack r + 1 : ℕ) : ℕ∞) := by
|
||
rw [Nat.cast_add, Nat.cast_add]
|
||
calc ((between db' r.end_.key r.begin.key).length : ℕ∞) +
|
||
(res.keys.length : ℕ∞) + ((1 - r.end_.offset).toNat : ℕ∞)
|
||
≤ intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) +
|
||
((1 - r.end_.offset).toNat : ℕ∞) := by gcongr
|
||
_ ≤ (beginSlack r : ℕ∞) + 1 := hcap
|
||
_ = ((beginSlack r + 1 : ℕ) : ℕ∞) := by rw [Nat.cast_add, Nat.cast_one]
|
||
exact_mod_cast hsum
|
||
have hsplit := between_length_add (db := db') (L := ((L : Key) : WithTop Key))
|
||
(E := r.end_.key) (B := r.begin.key) (le_of_lt hLE) (le_of_lt hcrossE)
|
||
have hboundLen : (between db' ((L : Key) : WithTop Key) r.begin.key).length ≤
|
||
beginSlack r + 1 := by
|
||
rw [hsplit, ← hbetweenEq', hbetweenLen]; omega
|
||
have hboundInt :
|
||
((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) +
|
||
r.begin.offset - 1 ≤ 0 := by
|
||
have hs : (beginSlack r : Int) = -r.begin.offset := beginSlack_cast r (le_of_lt hbeg)
|
||
have hble : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤
|
||
(beginSlack r : Int) + 1 := by exact_mod_cast hboundLen
|
||
linarith
|
||
have hLdb' : L ∈ db'.keys := RangeRead.compatible_keys_subset hcomp'' hLmem
|
||
have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) :=
|
||
resolve_begin_le_of_between_bound hLdb' hLcross hboundInt
|
||
have hres'_db' : resolve db' r'.begin = ((L : Key) : WithTop Key) := by
|
||
rw [hr'_beg]; exact resolve_offset_one_key_of_mem hLdb'
|
||
rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne]
|
||
refine ⟨by simpa [r', RangeRead.withinLimit] using hcomp''.1, ?_⟩
|
||
have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) hrev hrb'
|
||
have h2' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r') hr'_rev
|
||
(le_of_eq hres'_db')
|
||
have h2 := (compatible_iff_truncated_keys_rev (db := db') hr'_rev hmore hne).mp
|
||
hcomp'' |>.2
|
||
rw [h1, ← h2']
|
||
exact h2
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
|
||
/-- **Rewriting `between` as a double filter at a genuine key.** -/
|
||
theorem between_eq_filter_filter (db : DB) (L : Key) (B : WithTop Key) :
|
||
between db ((L : Key) : WithTop Key) B =
|
||
(db.keys.filter (fun k : Key => decide (L ≤ k))).filter
|
||
(fun k => decide (((k : Key) : WithTop Key) < B)) := by
|
||
unfold between
|
||
rw [List.filter_filter]
|
||
apply List.filter_congr
|
||
intro k _
|
||
by_cases hL : L ≤ k
|
||
· have hL' : ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr hL
|
||
by_cases hB : ((k : Key) : WithTop Key) < B
|
||
· simp [hL, hL', hB]
|
||
· simp [hB, hL']
|
||
· have hL' : ¬ ((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
|
||
fun h => hL (WithTop.coe_le_coe.mp h)
|
||
simp [hL, hL']
|
||
|
||
/-- **Large-capacity soundness.** Agreement on `[last, begin.key)` preserves
|
||
compatibility; the count identity locates the end resolution at a fixed relative position
|
||
inside that interval, so both databases resolve to the same reported window. -/
|
||
theorem sound_beginKeyRange_of_negCross_crossedNonpos {r : RangeRead} {res : Result}
|
||
(hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hne : res.keys ≠ [])
|
||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0) :
|
||
Sound r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by
|
||
set L : Key := res.keys.getLast hne with hLdef
|
||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||
have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross
|
||
have step : ∀ db db', AgreeOn ⟨((L : Key) : WithTop Key), r.begin.key⟩ db db' →
|
||
RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by
|
||
intro db db' hag' hcomp
|
||
have hbetweenEq : between db ((L : Key) : WithTop Key) r.begin.key =
|
||
between db' ((L : Key) : WithTop Key) r.begin.key := by
|
||
have h := hag'
|
||
unfold AgreeOn ConflictRange.keys at h
|
||
unfold between
|
||
exact h
|
||
have hLres : ((L : Key) : WithTop Key) < resolve db r.end_ :=
|
||
(RangeRead.mem_completeKeys.mp
|
||
((RangeRead.compatible_keys_prefix hcomp).subset hLmem)).2.2
|
||
have hresEnd_le : resolve db r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
have hLE : ((L : Key) : WithTop Key) < r.end_.key := lt_of_lt_of_le hLres hresEnd_le
|
||
have hVeq : between db ((L : Key) : WithTop Key) r.end_.key =
|
||
between db' ((L : Key) : WithTop Key) r.end_.key :=
|
||
between_eq_of_agree (db := db) (db' := db') (last := ((L : Key) : WithTop Key))
|
||
(beginKey := r.end_.key) (endKey := r.begin.key) hag' (le_of_lt hLE) (le_of_lt hcrossE)
|
||
have hbound := between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross
|
||
have hbound' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) +
|
||
r.begin.offset - 1 ≤ 0 := by
|
||
rw [← hbetweenEq]; exact hbound
|
||
have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem
|
||
have hLdb' : L ∈ db'.keys := by
|
||
have hmem : L ∈ between db ((L : Key) : WithTop Key) r.begin.key := by
|
||
unfold between
|
||
exact List.mem_filter.mpr ⟨hLdb, by simp [hLcross]⟩
|
||
rw [hbetweenEq] at hmem
|
||
exact (List.mem_filter.mp hmem).1
|
||
have hrb' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) :=
|
||
resolve_begin_le_of_between_bound hLdb' hLcross hbound'
|
||
set p : ℕ := (1 - r.end_.offset).toNat with hpdef
|
||
have hpcast : (p : Int) = 1 - r.end_.offset := by
|
||
rw [hpdef, Int.toNat_of_nonneg (by omega)]
|
||
have hVlen : (between db ((L : Key) : WithTop Key) r.end_.key).length =
|
||
res.keys.length + p :=
|
||
between_last_end_key_length_eq_of_end_nonpos (db := db) hcomp hmore hrev hne hend
|
||
have hVlen' : (between db' ((L : Key) : WithTop Key) r.end_.key).length =
|
||
res.keys.length + p := by rw [← hVeq]; exact hVlen
|
||
have hcntEqB : cnt db' r.end_.key = cnt db' ((L : Key) : WithTop Key) +
|
||
(between db' ((L : Key) : WithTop Key) r.end_.key).length :=
|
||
cnt_eq_add_between db' (le_of_lt hLE)
|
||
have hanc : r.end_.anchor db' =
|
||
(cnt db' ((L : Key) : WithTop Key) : Int) + res.keys.length := by
|
||
change (cnt db' r.end_.key : Int) + r.end_.offset - 1 = _
|
||
rw [hcntEqB, hVlen']
|
||
push_cast
|
||
rw [hpcast]; ring
|
||
have hp_pos : 0 < p := by
|
||
rw [hpdef]
|
||
have : (0 : Int) < 1 - r.end_.offset := by omega
|
||
omega
|
||
have hlen_ge : cnt db' ((L : Key) : WithTop Key) + res.keys.length < db'.keys.length := by
|
||
have h := cnt_eq_add_between db' (le_of_lt hLE)
|
||
rw [hVlen'] at h
|
||
have := cnt_le_length db' r.end_.key
|
||
omega
|
||
have hanc_nonneg : 0 ≤ r.end_.anchor db' := by rw [hanc]; positivity
|
||
have hanc_toNat : (r.end_.anchor db').toNat =
|
||
cnt db' ((L : Key) : WithTop Key) + res.keys.length := by
|
||
rw [hanc]; omega
|
||
have hanc_len : (r.end_.anchor db').toNat < db'.keys.length := by
|
||
rw [hanc_toNat]; exact hlen_ge
|
||
have hcntRes : cnt db' (resolve db' r.end_) = (r.end_.anchor db').toNat := by
|
||
change db'.keys.countP
|
||
(fun k => decide (((k : Key) : WithTop Key) < resolve db' r.end_)) = _
|
||
rw [countP_lt_resolve, Nat.min_eq_left (le_of_lt hanc_len)]
|
||
have hLleRes : ((L : Key) : WithTop Key) ≤ resolve db' r.end_ := by
|
||
have hres := resolve_eq_getElem (db := db') (s := r.end_) hanc_nonneg hanc_len
|
||
rw [hres]
|
||
refine WithTop.coe_le_coe.mpr (le_getElem_of_countP_le db'.sorted ?_ hanc_len)
|
||
rw [← cnt_coe db' L, hanc_toNat]; omega
|
||
have hresLen' : (between db' ((L : Key) : WithTop Key) (resolve db' r.end_)).length =
|
||
res.keys.length := by
|
||
rw [← cnt_sub_eq_between_length (db := db') hLleRes, hcntRes, hanc_toNat,
|
||
Nat.add_sub_cancel_left]
|
||
have htakeDb : between db ((L : Key) : WithTop Key) (resolve db r.end_) =
|
||
(between db ((L : Key) : WithTop Key) r.end_.key).take res.keys.length :=
|
||
between_resolve_eq_take_of_end_le (db := db) (r := r) (L := L) (t := res.keys.length)
|
||
hresEnd_le (between_last_resolve_end_length (db := db) hcomp hmore hrev hne)
|
||
have htakeDb' : between db' ((L : Key) : WithTop Key) (resolve db' r.end_) =
|
||
(between db' ((L : Key) : WithTop Key) r.end_.key).take res.keys.length :=
|
||
between_resolve_eq_take_of_end_le (db := db') (r := r) (L := L) (t := res.keys.length)
|
||
(resolve_le_key_of_offset_nonpos hend) hresLen'
|
||
have hbetween_res_eq : between db ((L : Key) : WithTop Key) (resolve db r.end_) =
|
||
between db' ((L : Key) : WithTop Key) (resolve db' r.end_) := by
|
||
rw [htakeDb, htakeDb', hVeq]
|
||
rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne]
|
||
refine ⟨by simpa [RangeRead.withinLimit] using hcomp.1, ?_⟩
|
||
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
|
||
resolve_begin_le_of_between_bound hLdb hLcross hbound
|
||
have hfilDb : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys :=
|
||
(compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
|
||
have hidDb := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) (last := L) hrev hrb
|
||
have hidDb' := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) (last := L) hrev hrb'
|
||
have hbridgeDb : ((db.keys.filter (fun k : Key => decide (L ≤ k))).filter
|
||
(fun k => decide (((k : Key) : WithTop Key) < resolve db r.end_))) =
|
||
between db ((L : Key) : WithTop Key) (resolve db r.end_) :=
|
||
(between_eq_filter_filter db L (resolve db r.end_)).symm
|
||
have hbridgeDb' : ((db'.keys.filter (fun k : Key => decide (L ≤ k))).filter
|
||
(fun k => decide (((k : Key) : WithTop Key) < resolve db' r.end_))) =
|
||
between db' ((L : Key) : WithTop Key) (resolve db' r.end_) :=
|
||
(between_eq_filter_filter db' L (resolve db' r.end_)).symm
|
||
rw [hidDb', hbridgeDb', ← hbetween_res_eq, ← hbridgeDb, ← hidDb, hfilDb]
|
||
intro db₁ db₂ hagree
|
||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||
|
||
/-- **Gap-key necessity in the large-capacity nonpositive-end crossed cell.** The
|
||
witness database stores the reported keys, the `p` pad keys that any compatible database
|
||
stores in `[resolve end, end_.key)`, and `beginSlack + 1 - t - p` gap keys in
|
||
`[end_.key, begin_.key)`. Its begin anchor is zero and it resolves onto the reported
|
||
last. Inserting the target raises the anchor to one, so the reported last leaves the
|
||
window and the reported keys can no longer be a prefix of the complete keys. -/
|
||
theorem necessary_of_mem_gap_of_negCross_crossedNonpos_large {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hne : res.keys ≠ []) (hbeg : r.begin.offset < 0)
|
||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0)
|
||
(hlarge : (beginSlack r : ℕ∞) + 2 ≤
|
||
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) +
|
||
((1 - r.end_.offset).toNat : ℕ∞))
|
||
{k : Key} (hkE : r.end_.key ≤ (k : WithTop Key))
|
||
(hkB : (k : WithTop Key) < r.begin.key) :
|
||
Necessary r res k := by
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
set L : Key := res.keys.getLast hne with hLdef
|
||
set t : ℕ := res.keys.length with htdef
|
||
set s : ℕ := beginSlack r with hsdef
|
||
set p : ℕ := (1 - r.end_.offset).toNat with hpdef
|
||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev
|
||
have hLle : ∀ x ∈ res.keys, L ≤ x := by
|
||
intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||
have hpre₀ : res.keys <+: RangeRead.completeKeys db₀ r :=
|
||
RangeRead.compatible_keys_prefix hcomp₀
|
||
have hreported_lt_R : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < resolve db₀ r.end_ := by
|
||
intro x hx
|
||
exact (RangeRead.mem_completeKeys.mp (hpre₀.subset hx)).2.2
|
||
have hreported_lt_E : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
exact lt_of_lt_of_le (hreported_lt_R x hx)
|
||
(resolve_le_key_of_offset_nonpos (db := db₀) (s := r.end_) hend)
|
||
have hLltE : ((L : Key) : WithTop Key) < r.end_.key := hreported_lt_E L hLmem
|
||
have hEcross : r.end_.key ≤ r.begin.key := le_of_lt hcrossE
|
||
have hbound : ((between db₀ ((L : Key) : WithTop Key) r.begin.key).length : Int) +
|
||
r.begin.offset - 1 ≤ 0 :=
|
||
between_add_offset_le_one (db := db₀) hcomp₀ hmore hrev hne hcross
|
||
have hbLen_le : (between db₀ ((L : Key) : WithTop Key) r.begin.key).length ≤ s + 1 := by
|
||
have hs : (beginSlack r : Int) = -r.begin.offset := beginSlack_cast r (le_of_lt hbeg)
|
||
have hb2 : ((between db₀ ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤
|
||
(beginSlack r : Int) + 1 := by linarith
|
||
have : ((between db₀ ((L : Key) : WithTop Key) r.begin.key).length : Int) ≤
|
||
(s : Int) + 1 := by rw [hsdef]; exact hb2
|
||
exact_mod_cast this
|
||
have hVlen : (between db₀ ((L : Key) : WithTop Key) r.end_.key).length = t + p := by
|
||
rw [htdef, hpdef]
|
||
exact between_last_end_key_length_eq_of_end_nonpos (db := db₀) hcomp₀ hmore hrev hne hend
|
||
have hsplit := between_length_add (db := db₀) (L := ((L : Key) : WithTop Key))
|
||
(E := r.end_.key) (B := r.begin.key) (le_of_lt hLltE) hEcross
|
||
have ht_le : t + p ≤ s + 1 := by omega
|
||
-- enough room in the crossed gap for the witness keys and the target
|
||
have hcard : (((s + 1 - t - p) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by
|
||
cases hcap : intervalCard r.end_.key r.begin.key with
|
||
| top => exact le_top
|
||
| coe n =>
|
||
have hnat : s + 2 ≤ n + t + p := by
|
||
have h' : ((s + 2 : ℕ) : ℕ∞) ≤ ((n + t + p : ℕ) : ℕ∞) := by
|
||
rw [hcap] at hlarge
|
||
simpa [ENat.natCast_add] using hlarge
|
||
exact_mod_cast h'
|
||
have hn : (s + 1 - t - p) + 1 ≤ n := by omega
|
||
exact_mod_cast hn
|
||
-- the pad keys taken from `db₀`
|
||
have hLres : ((L : Key) : WithTop Key) < resolve db₀ r.end_ := hreported_lt_R L hLmem
|
||
have hresEnd_le : resolve db₀ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||
set Pad : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hPaddef
|
||
have hPadLen : Pad.length = p := by
|
||
rw [hPaddef]
|
||
have hsplitP := between_length_add (db := db₀) (L := ((L : Key) : WithTop Key))
|
||
(E := resolve db₀ r.end_) (B := r.end_.key) (le_of_lt hLres) hresEnd_le
|
||
have hresLen : (between db₀ ((L : Key) : WithTop Key) (resolve db₀ r.end_)).length = t := by
|
||
rw [htdef]
|
||
exact between_last_resolve_end_length (db := db₀) hcomp₀ hmore hrev hne
|
||
rw [hVlen, hresLen] at hsplitP
|
||
omega
|
||
have hPadPos : 0 < Pad.length := by rw [hPadLen, hpdef]; omega
|
||
have hPadSorted : Pad.SortedLT := by
|
||
rw [hPaddef]; unfold between
|
||
exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)
|
||
have hPadMem : ∀ x ∈ Pad, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.end_.key := by
|
||
intro x hx
|
||
rw [hPaddef] at hx
|
||
unfold between at hx
|
||
rw [List.mem_filter] at hx
|
||
exact of_decide_eq_true hx.2
|
||
set P0 : Key := Pad[(0 : ℕ)]'hPadPos with hP0def
|
||
have hP0mem : P0 ∈ Pad := List.getElem_mem hPadPos
|
||
have hP0min : ∀ x ∈ Pad, P0 ≤ x := by
|
||
intro x hx
|
||
obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hx
|
||
have hle : Pad[(0 : ℕ)]'hPadPos ≤ Pad[j]'hj :=
|
||
hPadSorted.getElem_le_getElem_iff.mpr (Nat.zero_le j)
|
||
rw [hP0def]
|
||
rwa [hjx] at hle
|
||
obtain ⟨dbG, hGrange, hkG, hGlen⟩ :=
|
||
exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k)
|
||
(m := s + 1 - t - p) hcard
|
||
set G : List Key := dbG.keys with hGdef
|
||
have hGmem : ∀ x ∈ G, r.end_.key ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < r.begin.key := by
|
||
intro x hx
|
||
simpa only [G, keysIn, Set.mem_ofPred_eq] using hGrange x (by simpa [hGdef] using hx)
|
||
have hGlen' : G.length = s + 1 - t - p := by rw [hGdef]; exact hGlen
|
||
-- assemble the ascending witness database
|
||
have hresSort : res.keys.reverse.SortedLT := hsort.reverse
|
||
have hGsorted : G.SortedLT := by rw [hGdef]; exact dbG.sorted
|
||
have hcrossRP : ∀ x ∈ res.keys.reverse, ∀ y ∈ Pad, x < y := by
|
||
intro x hx y hy
|
||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_R x (List.mem_reverse.mp hx))
|
||
(hPadMem y hy).1)
|
||
have hcrossPG : ∀ x ∈ Pad, ∀ y ∈ G, x < y := by
|
||
intro x hx y hy
|
||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hPadMem x hx).2 (hGmem y hy).1)
|
||
have hcrossRG : ∀ x ∈ res.keys.reverse, ∀ y ∈ G, x < y := by
|
||
intro x hx y hy
|
||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx))
|
||
(hGmem y hy).1)
|
||
have hRPsorted : (res.keys.reverse ++ Pad).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hresSort, hPadSorted, hcrossRP⟩
|
||
have hcrossAllG : ∀ x ∈ (res.keys.reverse ++ Pad), ∀ y ∈ G, x < y := by
|
||
intro x hx y hy
|
||
rcases List.mem_append.mp hx with hx' | hx'
|
||
· exact hcrossRG x hx' y hy
|
||
· exact hcrossPG x hx' y hy
|
||
have hkeysSorted : (res.keys.reverse ++ Pad ++ G).SortedLT :=
|
||
List.sortedLT_append.mpr ⟨hRPsorted, hGsorted, hcrossAllG⟩
|
||
let db_a : DB := ⟨res.keys.reverse ++ Pad ++ G, hkeysSorted⟩
|
||
have hdb_a : db_a.keys = res.keys.reverse ++ Pad ++ G := rfl
|
||
have hlen_a : db_a.keys.length = s + 1 := by
|
||
rw [hdb_a, List.length_append, List.length_append, List.length_reverse, hGlen']
|
||
omega
|
||
have hcntB : db_a.keys.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.begin.key)) = t + p + G.length := by
|
||
rw [hdb_a, List.countP_append, List.countP_append, List.countP_reverse]
|
||
have h1 : res.keys.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.begin.key)) = t := by
|
||
rw [htdef, List.countP_eq_length]
|
||
intro x hx; rw [decide_eq_true_eq]
|
||
exact lt_trans (hreported_lt_E x hx) hcrossE
|
||
have h2 : Pad.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.begin.key)) = p := by
|
||
rw [← hPadLen, List.countP_eq_length]
|
||
intro x hx; rw [decide_eq_true_eq]
|
||
exact lt_trans (hPadMem x hx).2 hcrossE
|
||
have h3 : G.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.begin.key)) = G.length := by
|
||
rw [List.countP_eq_length]
|
||
intro x hx; rw [decide_eq_true_eq]; exact (hGmem x hx).2
|
||
rw [h1, h2, h3]
|
||
have hanchor : r.begin.anchor db_a = 0 := by
|
||
rw [anchor_eq_count_sub_slack db_a r (le_of_lt hbeg), hcntB, hGlen', ← hsdef]
|
||
have hts : t + p + (s + 1 - t - p) = s + 1 := by omega
|
||
rw [hts]; push_cast; ring
|
||
have h0lt : (0 : ℕ) < db_a.keys.length := by rw [hlen_a]; omega
|
||
have hmin_a : ∀ x ∈ db_a.keys, L ≤ x := by
|
||
intro x hx
|
||
rw [hdb_a] at hx
|
||
rcases List.mem_append.mp hx with hx' | hxG
|
||
· rcases List.mem_append.mp hx' with hxr | hxP
|
||
· exact hLle x (List.mem_reverse.mp hxr)
|
||
· exact le_of_lt (WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (hreported_lt_R L hLmem) (hPadMem x hxP).1))
|
||
· exact le_of_lt (WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (hreported_lt_E L hLmem) (hGmem x hxG).1))
|
||
have hLmem_a : L ∈ db_a.keys := by
|
||
rw [hdb_a]
|
||
exact List.mem_append_left _ (List.mem_append_left _ (List.mem_reverse.mpr hLmem))
|
||
have hget0 : db_a.keys[(0 : ℕ)] = L :=
|
||
getElem_zero_eq_of_forall_le hkeysSorted hmin_a hLmem_a h0lt
|
||
have hresBegin : resolve db_a r.begin = ((L : Key) : WithTop Key) := by
|
||
have h0 : 0 ≤ r.begin.anchor db_a := by rw [hanchor]
|
||
have hlt : (r.begin.anchor db_a).toNat < db_a.keys.length := by
|
||
rw [hanchor]; simpa using h0lt
|
||
rw [resolve_eq_getElem h0 hlt]
|
||
have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanchor]; rfl
|
||
simp [hidx, hget0]
|
||
have hcntEnd : cnt db_a r.end_.key = t + p := by
|
||
unfold cnt
|
||
rw [hdb_a, List.countP_append, List.countP_append, List.countP_reverse]
|
||
have h1 : res.keys.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.end_.key)) = t := by
|
||
rw [htdef, List.countP_eq_length]
|
||
intro x hx; rw [decide_eq_true_eq]; exact hreported_lt_E x hx
|
||
have h2 : Pad.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.end_.key)) = p := by
|
||
rw [← hPadLen, List.countP_eq_length]
|
||
intro x hx; rw [decide_eq_true_eq]; exact (hPadMem x hx).2
|
||
have h3 : G.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.end_.key)) = 0 := by
|
||
rw [List.countP_eq_zero]
|
||
intro x hx
|
||
rw [decide_eq_true_eq, not_lt]
|
||
exact (hGmem x hx).1
|
||
rw [h1, h2, h3]
|
||
omega
|
||
have hancEnd : r.end_.anchor db_a = (t : Int) := by
|
||
change (cnt db_a r.end_.key : Int) + r.end_.offset - 1 = _
|
||
rw [hcntEnd]
|
||
have hpcast : (p : Int) = 1 - r.end_.offset := by
|
||
rw [hpdef, Int.toNat_of_nonneg (by omega)]
|
||
push_cast; rw [hpcast]; ring
|
||
have hresEnd : resolve db_a r.end_ = ((P0 : Key) : WithTop Key) := by
|
||
have h0 : 0 ≤ r.end_.anchor db_a := by rw [hancEnd]; positivity
|
||
have hlt : (r.end_.anchor db_a).toNat < db_a.keys.length := by
|
||
rw [hancEnd, hlen_a]; omega
|
||
have hlen1 : res.keys.reverse.length = t := by rw [List.length_reverse, htdef]
|
||
have hidx : (r.end_.anchor db_a).toNat = t := by rw [hancEnd]; simp
|
||
have hget : db_a.keys[(r.end_.anchor db_a).toNat]'hlt = P0 := by
|
||
simp only [hdb_a] at hlt ⊢
|
||
simp only [hidx, ← hlen1]
|
||
simp only [List.append_assoc]
|
||
rw [List.getElem_append_right (as := res.keys.reverse) (bs := Pad ++ G)
|
||
(i := res.keys.reverse.length) (by simp)]
|
||
simp only [Nat.sub_self]
|
||
rw [List.getElem_append_left (i := 0) (as := Pad) (bs := G) hPadPos]
|
||
rw [resolve_eq_getElem h0 hlt, hget]
|
||
have hfilter : db_a.keys.filter (fun x : Key => decide
|
||
(((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by
|
||
rw [hdb_a, List.filter_append, List.filter_append, hresEnd]
|
||
have h1 : (res.keys.reverse).filter (fun x : Key => decide
|
||
(((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < ((P0 : Key) : WithTop Key))) = res.keys.reverse := by
|
||
rw [List.filter_eq_self]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
exact ⟨WithTop.coe_le_coe.mpr (hLle x (List.mem_reverse.mp hx)),
|
||
lt_of_lt_of_le (hreported_lt_R x (List.mem_reverse.mp hx)) (hPadMem P0 hP0mem).1⟩
|
||
have h2 : Pad.filter (fun x : Key => decide
|
||
(((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < ((P0 : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hP
|
||
exact absurd hP.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (hP0min x hx)))
|
||
have h3 : G.filter (fun x : Key => decide
|
||
(((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||
(x : WithTop Key) < ((P0 : Key) : WithTop Key))) = [] := by
|
||
rw [List.filter_eq_nil_iff]
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
intro hP
|
||
have hle : ((P0 : Key) : WithTop Key) ≤ (x : WithTop Key) :=
|
||
le_trans (hPadMem P0 hP0mem).2.le (hGmem x hx).1
|
||
exact absurd hP.2 (not_lt.mpr hle)
|
||
rw [h1, h2, h3]
|
||
simp
|
||
have hcomplete : RangeRead.completeKeys db_a r = res.keys := by
|
||
unfold RangeRead.completeKeys RangeRead.inRange
|
||
rw [hrev]
|
||
simp only [ite_true]
|
||
rw [hresBegin, hfilter, List.reverse_reverse]
|
||
have hcompat_a : RangeRead.Compatible db_a r res :=
|
||
⟨hcomp₀.1, Or.inl ⟨hmore, by rw [hcomplete], hne⟩⟩
|
||
have hk_a : k ∉ db_a.keys := by
|
||
rw [hdb_a]
|
||
intro hmem
|
||
rcases List.mem_append.mp hmem with hx | hx
|
||
· rcases List.mem_append.mp hx with hxr | hxP
|
||
· exact absurd (hreported_lt_E k (List.mem_reverse.mp hxr)) (not_lt.mpr hkE)
|
||
· exact absurd (hPadMem k hxP).2 (not_lt.mpr hkE)
|
||
· exact hkG (by simpa [hGdef] using hx)
|
||
-- insert the target: the first two sorted keys are the reported last and something above
|
||
let db_b : DB := db_a.insert k hk_a
|
||
have hins : (res.keys.reverse ++ Pad ++ G).orderedInsert (· ≤ ·) k =
|
||
res.keys.reverse ++ Pad ++ G.orderedInsert (· ≤ ·) k := by
|
||
refine orderedInsert_append_of_forall_lt_left ?_
|
||
intro x hx
|
||
rcases List.mem_append.mp hx with hx' | hxP
|
||
· exact WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx')) hkE)
|
||
· exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hPadMem x hxP).2 hkE)
|
||
have hb_keys : db_b.keys = res.keys.reverse ++ Pad ++ G.orderedInsert (· ≤ ·) k := by
|
||
rw [show db_b.keys = db_a.keys.orderedInsert (· ≤ ·) k from rfl, hdb_a, hins]
|
||
have hb_len : db_b.keys.length = s + 2 := by
|
||
rw [hb_keys, List.length_append, List.length_append, List.length_reverse,
|
||
List.orderedInsert_length, hGlen', htdef, hsdef]
|
||
omega
|
||
have hb_all : ∀ x ∈ db_b.keys,
|
||
decide (((x : Key) : WithTop Key) < r.begin.key) = true := by
|
||
intro x hx
|
||
rw [decide_eq_true_eq]
|
||
rw [hb_keys] at hx
|
||
rcases List.mem_append.mp hx with hx' | hx'
|
||
· rcases List.mem_append.mp hx' with hxr | hxP
|
||
· exact lt_trans (hreported_lt_E x (List.mem_reverse.mp hxr)) hcrossE
|
||
· exact lt_trans (hPadMem x hxP).2 hcrossE
|
||
· rw [List.mem_orderedInsert] at hx'
|
||
rcases hx' with rfl | hxG
|
||
· exact hkB
|
||
· exact (hGmem x hxG).2
|
||
have hb_cntB : db_b.keys.countP (fun x : Key =>
|
||
decide (((x : Key) : WithTop Key) < r.begin.key)) = s + 2 := by
|
||
rw [← hb_len, List.countP_eq_length]
|
||
exact hb_all
|
||
have hanchor_b : r.begin.anchor db_b = 1 := by
|
||
rw [anchor_eq_count_sub_slack db_b r (le_of_lt hbeg), hb_cntB, ← hsdef]
|
||
push_cast; ring
|
||
have hget0_b : db_b.keys[(0 : ℕ)] = L := by
|
||
have hmin_b : ∀ x ∈ db_b.keys, L ≤ x := by
|
||
intro x hx
|
||
rw [hb_keys] at hx
|
||
rcases List.mem_append.mp hx with hx' | hx'
|
||
· rcases List.mem_append.mp hx' with hxr | hxP
|
||
· exact hLle x (List.mem_reverse.mp hxr)
|
||
· exact le_of_lt (WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (hreported_lt_R L hLmem) (hPadMem x hxP).1))
|
||
· rw [List.mem_orderedInsert] at hx'
|
||
rcases hx' with rfl | hxG
|
||
· exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_E L hLmem) hkE))
|
||
· exact le_of_lt (WithTop.coe_lt_coe.mp
|
||
(lt_of_lt_of_le (hreported_lt_E L hLmem) (hGmem x hxG).1))
|
||
have hLmem_b : L ∈ db_b.keys := by
|
||
rw [hb_keys]
|
||
exact List.mem_append_left _ (List.mem_append_left _ (List.mem_reverse.mpr hLmem))
|
||
have h0lt_b : 0 < db_b.keys.length := by rw [hb_len]; omega
|
||
exact getElem_zero_eq_of_forall_le db_b.sorted hmin_b hLmem_b h0lt_b
|
||
have h1lt : (1 : ℕ) < db_b.keys.length := by rw [hb_len]; omega
|
||
have hresBegin_b : resolve db_b r.begin = ((db_b.keys[(1 : ℕ)] : Key) : WithTop Key) := by
|
||
have h0 : 0 ≤ r.begin.anchor db_b := by rw [hanchor_b]; norm_num
|
||
have hlt : (r.begin.anchor db_b).toNat < db_b.keys.length := by
|
||
rw [hanchor_b]; simpa using h1lt
|
||
rw [resolve_eq_getElem h0 hlt]
|
||
have hidx : (r.begin.anchor db_b).toNat = 1 := by rw [hanchor_b]; rfl
|
||
simp [hidx]
|
||
have hLlt : L < db_b.keys[(1 : ℕ)] := by
|
||
have h01 : (0 : ℕ) < db_b.keys.length := by omega
|
||
have h := (List.SortedLT.getElem_lt_getElem_iff db_b.sorted (hi := h01)
|
||
(hj := h1lt)).mpr (show (0 : ℕ) < 1 by omega)
|
||
rwa [hget0_b] at h
|
||
have hLnot_ck : L ∉ RangeRead.completeKeys db_b r := by
|
||
intro hmem
|
||
have hle := (RangeRead.mem_completeKeys.mp hmem).2.1
|
||
rw [hresBegin_b] at hle
|
||
exact absurd hle (not_le.mpr (WithTop.coe_lt_coe.mpr hLlt))
|
||
have hnotCompat_b : ¬ RangeRead.Compatible db_b r res := by
|
||
intro hc
|
||
rcases hc.2 with ⟨_, hpre, _⟩ | ⟨hm, _⟩
|
||
· exact hLnot_ck (hpre.subset hLmem)
|
||
· rw [hmore] at hm; exact absurd hm (by simp)
|
||
refine necessary_of_toggle ⟨db_a, hcompat_a, ?_⟩
|
||
rw [toggle_eq_insert_of_not_mem hk_a]
|
||
exact hnotCompat_b
|
||
|
||
/-- **Minimality of the reversed truncated range in the small-capacity nonpositive-end
|
||
crossed cell.** Soundness is `sound_truncatedRangeRev_of_negCross_crossedNonpos_small`;
|
||
necessity of `[last, end_.key)` is `necessary_of_mem_truncatedRangeRev_of_end_nonpos`. -/
|
||
theorem minimal_truncatedRangeRev_of_negCross_crossedNonpos_small {r : RangeRead}
|
||
{res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbeg : r.begin.offset < 0) (hne : res.keys ≠ [])
|
||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0)
|
||
(hcap : intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) +
|
||
((1 - r.end_.offset).toNat : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1) :
|
||
Minimal r res (truncatedRangeRev r (res.keys.getLast hne)) := by
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
|
||
· exact sound_truncatedRangeRev_of_negCross_crossedNonpos_small hread hmore hrev hbeg hne
|
||
hcross hcrossE hend hcap
|
||
· intro k hk
|
||
rw [ConflictRange.mem_iff] at hk
|
||
obtain ⟨hkL, hkE⟩ := hk
|
||
exact necessary_of_mem_truncatedRangeRev_of_end_nonpos hread hrev hne hend hkL hkE
|
||
|
||
/-- **Minimality of `[last, begin_.key)` in the large-capacity nonpositive-end crossed
|
||
cell.** Soundness is `sound_beginKeyRange_of_negCross_crossedNonpos`; necessity below
|
||
`end_.key` is the truncated-range necessity, and at or above it the gap witness applies. -/
|
||
theorem minimal_beginKeyRange_of_negCross_crossedNonpos_large {r : RangeRead}
|
||
{res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbeg : r.begin.offset < 0) (hne : res.keys ≠ [])
|
||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset ≤ 0)
|
||
(hlarge : (beginSlack r : ℕ∞) + 2 ≤
|
||
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) +
|
||
((1 - r.end_.offset).toNat : ℕ∞)) :
|
||
Minimal r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
|
||
· exact sound_beginKeyRange_of_negCross_crossedNonpos hmore hrev hne hcross hcrossE hend
|
||
· intro k hk
|
||
rw [ConflictRange.mem_iff] at hk
|
||
obtain ⟨hkL, hkB⟩ := hk
|
||
by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key
|
||
· exact necessary_of_mem_truncatedRangeRev_of_end_nonpos hread hrev hne hend hkL hkE
|
||
· exact necessary_of_mem_gap_of_negCross_crossedNonpos_large hread hmore hrev hne hbeg
|
||
hcross hcrossE hend hlarge (not_lt.mp hkE) hkB
|
||
|
||
/-- The reversed crossed negative-begin truncated small-capacity cell at a nonpositive
|
||
end offset. -/
|
||
abbrev negCrossTruncRevCrossedNonposSmall (r : RangeRead) (res : Result) : Prop :=
|
||
res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
r.end_.key < r.begin.key ∧ r.end_.offset ≤ 0 ∧
|
||
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) +
|
||
((1 - r.end_.offset).toNat : ℕ∞) ≤ (beginSlack r : ℕ∞) + 1
|
||
|
||
/-- The closed form of the small-capacity nonpositive-end crossed cell:
|
||
`[last reported, end_.key)`. -/
|
||
noncomputable def closedFormNegCrossTruncRevCrossedNonposSmall (_r : RangeRead)
|
||
(res : Result) : ConflictRange :=
|
||
truncatedRangeRev _r (res.keys.getLastD ([] : Key))
|
||
|
||
/-- **The small-capacity nonpositive-end crossed closed form is minimal.** -/
|
||
theorem minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_small {r : RangeRead}
|
||
{res : Result} (hread : Readable r res)
|
||
(h : negCrossTruncRevCrossedNonposSmall r res) :
|
||
Minimal r res (closedFormNegCrossTruncRevCrossedNonposSmall r res) := by
|
||
obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hcap⟩ := h
|
||
have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||
have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hlast]; exact hcrossD
|
||
unfold closedFormNegCrossTruncRevCrossedNonposSmall
|
||
rw [hlast]
|
||
exact minimal_truncatedRangeRev_of_negCross_crossedNonpos_small hread hmore hrev hbeg hne
|
||
hcross hcrossE hend hcap
|
||
|
||
/-- The reversed crossed negative-begin truncated large-capacity cell at a nonpositive
|
||
end offset. -/
|
||
abbrev negCrossTruncRevCrossedNonposLarge (r : RangeRead) (res : Result) : Prop :=
|
||
res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
|
||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||
r.end_.key < r.begin.key ∧ r.end_.offset ≤ 0 ∧
|
||
(beginSlack r : ℕ∞) + 2 ≤
|
||
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞) +
|
||
((1 - r.end_.offset).toNat : ℕ∞)
|
||
|
||
/-- The closed form of the large-capacity nonpositive-end crossed cell:
|
||
`[last reported, begin_.key)`. -/
|
||
noncomputable def closedFormNegCrossTruncRevCrossedNonposLarge (_r : RangeRead)
|
||
(res : Result) : ConflictRange :=
|
||
⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), _r.begin.key⟩
|
||
|
||
/-- **The large-capacity nonpositive-end crossed closed form is minimal.** -/
|
||
theorem minimal_closedForm_of_negCross_trunc_rev_crossedNonpos_large {r : RangeRead}
|
||
{res : Result} (hread : Readable r res)
|
||
(h : negCrossTruncRevCrossedNonposLarge r res) :
|
||
Minimal r res (closedFormNegCrossTruncRevCrossedNonposLarge r res) := by
|
||
obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hlarge⟩ := h
|
||
have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||
have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
|
||
rw [← hlast]; exact hcrossD
|
||
unfold closedFormNegCrossTruncRevCrossedNonposLarge
|
||
rw [hlast]
|
||
exact minimal_beginKeyRange_of_negCross_crossedNonpos_large hread hmore hrev hbeg hne
|
||
hcross hcrossE hend hlarge
|
||
|
||
end FormalConflictRange
|