Files
conflict-range/FormalConflictRange/NegCrossTruncClamp.lean
T
andrew a54a5f82f8 Prove conflict-range closed-form minimality
- 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
2026-11-16 21:53:59 -05:00

2790 lines
147 KiB
Lean4
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/-
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.NegCrossTruncNonclamp
import FormalConflictRange.NegCrossClampLongNonpos
/-!
# Crossed negative-begin truncated clamping cells
This module begins closing the forward, crossed, truncated cells where the reported
keys below `begin.key` fit inside the begin slack (`reportedBelowBegin ≤ beginSlack`).
Unlike the nonclamping cells, the begin selector may clamp to the empty key, so the
minimal sound range starts at `⊥`.
The first subcell treated here is the *no-gap* case `begin.key = keyAfter last`: the
minimal sound range is `[⊥, begin.key)`, and every key below `begin.key` is necessary.
The proof splits keys below the reported head (a toggle witness adapted from the
complete clamping cell) from the reported and intermediate keys (convexity).
-/
open FormalConflictRange
set_option maxHeartbeats 1200000
namespace FormalConflictRange
/-- The forward, crossed, truncated, clamping negative-begin cell in which the begin key
is exactly the successor of the reported last and every reported key lies below the end
key. -/
abbrev negCrossTruncClampNoGapTop (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
r.end_.key = (⊤ : WithTop Key) ∧ 1 ≤ r.end_.offset ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ∧
reportedBelowBegin r res ≤ beginSlack r
/-- The closed form for the no-gap clamping cell: `[⊥, begin.key)`. -/
noncomputable def closedFormNegCrossTruncClampNoGapTop (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩
/-- **Every key below the reported head of a clamping truncated cell is necessary.**
The reported list alone is a compatible database: its end-filter count is bounded by the
count in any originally compatible database. Inserting a key below the head lands in the
`≤ last` filter of the complete keys. No relation between the end key and the reported
last is needed. -/
theorem necessary_below_head_of_negCross_trunc_clamp {r : RangeRead} {res : Result}
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
(hend : 1 ≤ r.end_.offset)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
{k : Key} (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) :
Necessary r res k := by
obtain ⟨db₀, hcomp₀⟩ := hread
intro cr hs
by_contra hkcr
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hk_not : k ∉ res.keys := by
intro hkmem
exact hkcr (necessary_of_mem_result ⟨db₀, hcomp₀⟩ hkmem cr hs)
let db₁ : DB := ⟨res.keys, hsort⟩
have hcB₁ : db₁.keys.countP
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
reportedBelowBegin r res := rfl
have hanchor₁ : r.begin.anchor db₁ < 0 := by
rw [anchor_eq_count_sub_slack db₁ r (le_of_lt hbegin), hcB₁]
have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
omega
have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) :=
resolve_eq_nil (db := db₁) (s := r.begin) hanchor₁
have hlast_ck₀ : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r :=
(RangeRead.compatible_keys_prefix hcomp₀).subset (List.getLast_mem hne)
have hlast_lt_res₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ :=
(RangeRead.mem_completeKeys.mp hlast_ck₀).2.2
have hcnt₀ := filter_length_le_offset_of_resolve_end_gt (db := db₀) hend hlast_lt_res₀
have hsub : res.keys.Sublist db₀.keys := result_sublist_db_of_compatible hcomp₀ hrev
have hsub' : ((res.keys.filter
(fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne))).Sublist
((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne))) :=
(hsub.filter _).filter _
have hself : (res.keys.filter
(fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) =
res.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key))) := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.mem_filter.mp hx).1
have hlast_lt_res₁ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₁ r.end_ := by
apply resolve_end_gt_of_filter_length_le (db := db₁) hend
change ((res.keys.filter
(fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne))).length ≤ _
rw [hself]
rw [hself] at hsub'
exact le_trans hsub'.length_le hcnt₀
have hck₁ : RangeRead.completeKeys db₁ r = res.keys := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev])]
rw [hL₁]
rw [show db₁.keys = res.keys from rfl]
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
refine ⟨WithTop.coe_le_coe.mpr (nil_le_key x), ?_⟩
exact lt_of_le_of_lt
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx))
hlast_lt_res₁
have hcomp₁ : RangeRead.Compatible db₁ r res := by
refine ⟨hcomp₀.1, Or.inl ⟨hmore, ?_, hne⟩⟩
simpa [hck₁] using (List.prefix_refl (res.keys))
let db₂ : DB := db₁.insert k hk_not
have hagree : AgreeOn cr db₁ db₂ :=
(ConflictRange.keys_insert_of_not_mem cr db₁ hk_not hkcr).symm
have hcomp₂ : RangeRead.Compatible db₂ r res := (hs db₁ db₂ hagree).mp hcomp₁
obtain ⟨-, heq₂⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp₂
have hdb₂_keys : db₂.keys = k :: res.keys := by
unfold db₂
rw [DB.insert_keys, show db₁.keys = res.keys from rfl]
cases hres : res.keys with
| nil => exact absurd hres hne
| cons a t =>
have hhead_a : res.keys.head hne = a := by simp [hres]
have hka : k ≤ a := by
rw [hhead_a] at hk
exact le_of_lt (WithTop.coe_lt_coe.mp hk)
rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka]
have hkltB : ((k : Key) : WithTop Key) < r.begin.key := lt_trans hk hcross
have hcB₂ : db₂.keys.countP
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
reportedBelowBegin r res + 1 := by
rw [hdb₂_keys, List.countP_cons]
rw [decide_eq_true_eq.mpr hkltB]
simp [reportedBelowBegin]
have hanchor₂ : r.begin.anchor db₂ ≤ 0 := by
rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hcB₂]
have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
omega
have hL₂_le : resolve db₂ r.begin ≤ ((k : Key) : WithTop Key) := by
by_cases hneg : r.begin.anchor db₂ < 0
· rw [resolve_eq_nil (db := db₂) (s := r.begin) hneg]
exact WithTop.coe_le_coe.mpr (nil_le_key k)
· have hzero : r.begin.anchor db₂ = 0 := by omega
have hltlen₂ : (r.begin.anchor db₂).toNat < db₂.keys.length := by
rw [hzero, hdb₂_keys]
simp
rw [resolve_eq_getElem (db := db₂) (s := r.begin) (by omega) hltlen₂]
have hval₂ : db₂.keys[(r.begin.anchor db₂).toNat]'hltlen₂ = k := by
simp [hzero, hdb₂_keys]
exact WithTop.coe_le_coe.mpr (le_of_eq hval₂)
have hk_lt_res₂ : ((k : Key) : WithTop Key) < resolve db₂ r.end_ := by
have hhead_ck₂ : res.keys.head hne ∈ RangeRead.completeKeys db₂ r :=
(RangeRead.compatible_keys_prefix hcomp₂).subset (List.head_mem hne)
exact lt_trans hk (RangeRead.mem_completeKeys.mp hhead_ck₂).2.2
have hk_ck₂ : k ∈ RangeRead.completeKeys db₂ r := by
rw [RangeRead.mem_completeKeys]
refine ⟨by rw [hdb₂_keys]; exact List.mem_cons_self .., hL₂_le, hk_lt_res₂⟩
have hkK : k < res.keys.head hne := WithTop.coe_lt_coe.mp hk
have hhead_le_last : res.keys.head hne ≤ res.keys.getLast hne :=
(hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)
have hk_le_last : k ≤ res.keys.getLast hne :=
le_trans (le_of_lt hkK) hhead_le_last
have hk_filter₂ : k ∈ (RangeRead.completeKeys db₂ r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) := by
rw [List.mem_filter]
exact ⟨hk_ck₂, by rw [decide_eq_true_eq]; exact hk_le_last⟩
rw [heq₂] at hk_filter₂
exact hk_not hk_filter₂
/-- **Every key below the reported head is necessary when the reported last reaches
`begin.key`, for an arbitrary positive-end key.** Filter a compatible database down to
the keys at or above the reported head. Every remaining key below `begin.key` is at or
below the reported last, hence is reported. The filtered database therefore clamps its
begin selector, and filtering cannot increase the end-selector count, so it remains
compatible. Inserting a key below the head then adds it to the truncated window. -/
theorem necessary_below_head_of_negCross_trunc_clamp_lastGe
{r : RangeRead} {res : Result}
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
(hend : 1 ≤ r.end_.offset)
(hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key)
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
(hBlast : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key))
{k : Key} (hk : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)) :
Necessary r res k := by
obtain ⟨db₀, hcomp₀⟩ := hread
intro cr hs
by_contra hkcr
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hpre : res.keys <+: RangeRead.completeKeys db₀ r :=
RangeRead.compatible_keys_prefix hcomp₀
have hres_filter : (RangeRead.completeKeys db₀ r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys :=
((compatible_iff_truncated_keys hrev hmore hne).mp hcomp₀).2
have hhead_ck : res.keys.head hne ∈ RangeRead.completeKeys db₀ r :=
hpre.subset (List.head_mem hne)
have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r :=
hpre.subset (List.getLast_mem hne)
have hrb_head : resolve db₀ r.begin ≤ ((res.keys.head hne : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp hhead_ck).2.1
have hlast_lt_re₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ :=
(RangeRead.mem_completeKeys.mp hlast_ck).2.2
have hmem_res_of_mem {x : Key} (hxdb : x ∈ db₀.keys)
(hxhead : ((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key))
(hxlast : x ≤ res.keys.getLast hne) : x ∈ res.keys := by
have hxck : x ∈ RangeRead.completeKeys db₀ r := by
rw [RangeRead.mem_completeKeys]
exact ⟨hxdb, le_trans hrb_head hxhead,
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hxlast) hlast_lt_re₀⟩
have hxfilt : x ∈ (RangeRead.completeKeys db₀ r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) := by
rw [List.mem_filter]
exact ⟨hxck, decide_eq_true_eq.mpr hxlast⟩
rwa [hres_filter] at hxfilt
let db₁ : DB := ⟨db₀.keys.filter
(fun x : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key))),
List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)⟩
have hmem_db₁_of_res {x : Key} (hx : x ∈ res.keys) : x ∈ db₁.keys := by
change x ∈ db₀.keys.filter
(fun x : Key => decide (((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key)))
rw [List.mem_filter]
exact ⟨(RangeRead.mem_completeKeys.mp (hpre.subset hx)).1,
decide_eq_true_eq.mpr (WithTop.coe_le_coe.mpr
((hsort.pairwise.imp le_of_lt).rel_head hx))⟩
have hk_not : k ∉ db₁.keys := by
intro hkmem
have hkhead : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) := by
have := (List.mem_filter.mp hkmem).2
exact of_decide_eq_true this
exact (not_le_of_gt hk) hkhead
have hcB₁ : db₁.keys.countP
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
reportedBelowBegin r res := by
unfold reportedBelowBegin
rw [List.countP_eq_length_filter, List.countP_eq_length_filter]
congr 1
apply eq_of_forall_mem_iff_of_pairwise
· exact db₁.sorted.pairwise.filter _
· exact hsort.pairwise.filter _
· intro x
simp only [db₁, List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨⟨hxdb, hxhead⟩, hxB⟩
refine ⟨hmem_res_of_mem hxdb hxhead ?_, hxB⟩
exact WithTop.coe_le_coe.mp (le_trans (le_of_lt hxB) hBlast)
· rintro ⟨hxres, hxB⟩
exact ⟨⟨(RangeRead.mem_completeKeys.mp (hpre.subset hxres)).1,
WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hxres)⟩, hxB⟩
have hanchor₁ : r.begin.anchor db₁ < 0 := by
rw [anchor_eq_count_sub_slack db₁ r (le_of_lt hbegin), hcB₁]
have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
omega
have hL₁ : resolve db₁ r.begin = (([] : Key) : WithTop Key) :=
resolve_eq_nil (db := db₁) (s := r.begin) hanchor₁
have hcnt₀ := filter_length_le_offset_of_resolve_end_gt (db := db₀) hend hlast_lt_re₀
have hsub : ((db₁.keys.filter
(fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne))).Sublist
((db₀.keys.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne))) := by
exact List.Sublist.filter (fun x : Key => decide (x ≤ res.keys.getLast hne))
(List.Sublist.filter (fun x : Key => decide (r.end_.key ≤ (x : WithTop Key)))
(show db₁.keys.Sublist db₀.keys from List.filter_sublist))
have hlast_lt_res₁ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₁ r.end_ :=
resolve_end_gt_of_filter_length_le (db := db₁) hend (le_trans hsub.length_le hcnt₀)
have hfilter₁ : (RangeRead.completeKeys db₁ r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys := by
rw [completeKeys_filter_le_eq hrev hlast_lt_res₁, hL₁]
apply eq_of_forall_mem_iff_of_pairwise
· exact db₁.sorted.pairwise.filter _
· exact hsort.pairwise
· intro x
simp only [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨hxdb₁, -, hxlast⟩
have hxparts : x ∈ db₀.keys ∧
((res.keys.head hne : Key) : WithTop Key) ≤ (x : WithTop Key) := by
simpa only [db₁, List.mem_filter, decide_eq_true_eq] using hxdb₁
exact hmem_res_of_mem hxparts.1 hxparts.2 hxlast
· intro hxres
exact ⟨hmem_db₁_of_res hxres, WithTop.coe_le_coe.mpr (nil_le_key x),
(hsort.pairwise.imp le_of_lt).rel_getLast hxres⟩
have hcomp₁ : RangeRead.Compatible db₁ r res :=
(compatible_iff_truncated_keys hrev hmore hne).mpr ⟨hcomp₀.1, hfilter₁⟩
let db₂ : DB := db₁.insert k hk_not
have hagree : AgreeOn cr db₁ db₂ :=
(ConflictRange.keys_insert_of_not_mem cr db₁ hk_not hkcr).symm
have hcomp₂ : RangeRead.Compatible db₂ r res := (hs db₁ db₂ hagree).mp hcomp₁
obtain ⟨-, heq₂⟩ := (compatible_iff_truncated_keys hrev hmore hne).mp hcomp₂
have hperm : db₂.keys.Perm (k :: db₁.keys) := by
unfold db₂
rw [DB.insert_keys]
exact List.perm_orderedInsert (· ≤ ·) k db₁.keys
have hcB₂ : db₂.keys.countP
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
reportedBelowBegin r res + 1 := by
rw [hperm.countP_eq, List.countP_cons, decide_eq_true_eq.mpr (lt_trans hk hcross), hcB₁]
simp [reportedBelowBegin]
have hanchor₂ : r.begin.anchor db₂ ≤ 0 := by
rw [anchor_eq_count_sub_slack db₂ r (le_of_lt hbegin), hcB₂]
have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
omega
have hkmem₂ : k ∈ db₂.keys := DB.mem_insert_self db₁ k hk_not
have hdb₂_keys : db₂.keys = k :: db₁.keys := by
unfold db₂
rw [DB.insert_keys]
cases hkeys : db₁.keys with
| nil => simp
| cons a t =>
have ha : a ∈ db₁.keys := by rw [hkeys]; exact List.mem_cons_self ..
have hHa : ((res.keys.head hne : Key) : WithTop Key) ≤ (a : WithTop Key) := by
have := (List.mem_filter.mp ha).2
exact of_decide_eq_true this
have hka : k ≤ a := le_of_lt (lt_of_lt_of_le (WithTop.coe_lt_coe.mp hk)
(WithTop.coe_le_coe.mp hHa))
rw [List.orderedInsert_cons_of_le (r := (· ≤ ·)) (a := k) (b := a) t hka]
have hL₂_le : resolve db₂ r.begin ≤ ((k : Key) : WithTop Key) := by
by_cases hneg : r.begin.anchor db₂ < 0
· rw [resolve_eq_nil (db := db₂) (s := r.begin) hneg]
exact WithTop.coe_le_coe.mpr (nil_le_key k)
· have hzero : r.begin.anchor db₂ = 0 := by omega
have hltlen₂ : (r.begin.anchor db₂).toNat < db₂.keys.length := by
rw [hzero, hdb₂_keys]
simp
rw [resolve_eq_getElem (db := db₂) (s := r.begin) (by omega) hltlen₂]
have hval₂ : db₂.keys[(r.begin.anchor db₂).toNat]'hltlen₂ = k := by
simp [hzero, hdb₂_keys]
exact WithTop.coe_le_coe.mpr (le_of_eq hval₂)
have hk_lt_res₂ : ((k : Key) : WithTop Key) < resolve db₂ r.end_ := by
have hhead_ck₂ : res.keys.head hne ∈ RangeRead.completeKeys db₂ r :=
(RangeRead.compatible_keys_prefix hcomp₂).subset (List.head_mem hne)
exact lt_trans hk (RangeRead.mem_completeKeys.mp hhead_ck₂).2.2
have hk_ck₂ : k ∈ RangeRead.completeKeys db₂ r := by
rw [RangeRead.mem_completeKeys]
exact ⟨hkmem₂, hL₂_le, hk_lt_res₂⟩
have hk_le_last : k ≤ res.keys.getLast hne :=
le_trans (le_of_lt (WithTop.coe_lt_coe.mp hk))
((hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne))
have hk_filter₂ : k ∈ (RangeRead.completeKeys db₂ r).filter
(fun x : Key => decide (x ≤ res.keys.getLast hne)) := by
rw [List.mem_filter]
exact ⟨hk_ck₂, decide_eq_true_eq.mpr hk_le_last⟩
rw [heq₂] at hk_filter₂
have hkres : k ∈ res.keys := hk_filter₂
have hkhead : ((res.keys.head hne : Key) : WithTop Key) ≤ (k : WithTop Key) :=
WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_head hkres)
exact (not_le_of_gt hk) hkhead
/-- **Compatibility transfers across agreement on the no-gap clamping range
`[⊥, begin.key)`.** Agreement fixes every stored key below `begin.key`, hence the
begin resolution (whose offset is negative) and the truncated complete keys, because
the end selector is `⊤` and the reported last lies below `begin.key`. -/
theorem compatible_of_agree_negCross_trunc_clamp_noGap_top {r : RangeRead} {res : Result}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hendkey : r.end_.key = (⊤ : WithTop Key)) (hendoff : 1 ≤ r.end_.offset)
(hgap : r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key))
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
(hagree : AgreeOn (closedFormNegCrossTruncClampNoGapTop r res) db1 db2) :
RangeRead.Compatible db2 r res := by
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by
rw [hgap, hlastD]
exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)
have hcr : closedFormNegCrossTruncClampNoGapTop r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hmem := (agreeOn_iff (closedFormNegCrossTruncClampNoGapTop r res) db1 db2).mp hagree
have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key →
(k ∈ db1.keys ↔ k ∈ db2.keys) := by
intro k hk
refine hmem k ?_
rw [hcr]
exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩
have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) =
db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩
have hbeg : resolve db1 r.begin = resolve db2 r.begin := by
rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin),
resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF]
have hE1 : resolve db1 r.end_ = (⊤ : WithTop Key) :=
resolve_eq_top_of_key_eq_top hendkey hendoff
have hE2 : resolve db2 r.end_ = (⊤ : WithTop Key) :=
resolve_eq_top_of_key_eq_top hendkey hendoff
have hfil : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) =
(RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) := by
apply eq_of_forall_mem_iff_of_pairwise
· unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev])]
exact (db1.sorted.pairwise.filter _).filter _
· unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev])]
exact (db2.sorted.pairwise.filter _).filter _
· intro k
rw [List.mem_filter, List.mem_filter, RangeRead.mem_completeKeys,
RangeRead.mem_completeKeys, hbeg, hE1, hE2]
constructor
· rintro ⟨⟨hdb, hlo, hhi⟩, hle⟩
have hle' : k ≤ last := of_decide_eq_true hle
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle') hlastB
exact ⟨⟨(hmk k hkB).mp hdb, hlo, hhi⟩, hle⟩
· rintro ⟨⟨hdb, hlo, hhi⟩, hle⟩
have hle' : k ≤ last := of_decide_eq_true hle
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle') hlastB
exact ⟨⟨(hmk k hkB).mpr hdb, hlo, hhi⟩, hle⟩
have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) =
res.keys := by
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
rwa [← hlast] at h
rw [compatible_iff_truncated_keys hrev hmore hne]
refine ⟨by simpa using hc1.1, ?_⟩
rw [← hlast, ← hfil, hres1]
/-- **Closed form for the forward crossed negative-begin truncated clamping no-gap
cell.** The minimal sound range is `[⊥, begin.key)`. -/
theorem minimal_closedForm_of_negCross_trunc_clamp_noGap_top {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampNoGapTop r res) :
Minimal r res (closedFormNegCrossTruncClampNoGapTop r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hendkey, hendoff, hcrossD, hgap, hclamp⟩ := hcell
obtain ⟨db₀, hcomp₀⟩ := hread
have hread' : Readable r res := ⟨db₀, hcomp₀⟩
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hheadD : res.keys.headD ([] : Key) = head := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hcross : ((head : Key) : WithTop Key) < r.begin.key := by
have hle : leastReported r res = res.keys.headD ([] : Key) := by
simp [leastReported, hrev]
rw [hle, hheadD] at hcrossD
exact hcrossD
have hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key := by
intro k _
rw [hendkey]
exact WithTop.coe_lt_top k
have hcr : closedFormNegCrossTruncClampNoGapTop r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hsound : Sound r res (closedFormNegCrossTruncClampNoGapTop r res) := by
intro db1 db2 hagree
exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp_noGap_top hrev hmore hne hbegin
hendkey hendoff hgap h hagree,
fun h => compatible_of_agree_negCross_trunc_clamp_noGap_top hrev hmore hne hbegin
hendkey hendoff hgap h hagree.symm⟩
rw [hcr]
refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩
intro k _ hkend
have hkend' : k < keyAfter last := by
rw [hgap, hlastD] at hkend
exact WithTop.coe_lt_coe.mp hkend
have hkle : k ≤ last := (lt_keyAfter_iff k last).mp hkend'
by_cases hlt : k < head
· exact necessary_below_head_of_negCross_trunc_clamp hread' hrev hmore hne hbegin hendoff
hcross hclamp (WithTop.coe_lt_coe.mpr hlt)
· exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne)
(by rw [hlast]; exact List.getLast_mem hne) (le_of_not_gt hlt) hkle
/-- The forward, crossed, truncated, clamping negative-begin cell in which the begin key
is exactly the successor of the reported last and every reported key lies below the end
key. -/
abbrev negCrossTruncClampBelowEnd (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
1 ≤ r.end_.offset ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ∧
reportedBelowBegin r res ≤ beginSlack r ∧
(∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)
/-- The closed form for the below-end clamping cell, by dropping the end bound. -/
noncomputable def closedFormNegCrossTruncClampBelowEnd (r : RangeRead) (res : Result) :
ConflictRange :=
closedFormNegCrossTruncClampNoGapTop r res
/-- **Closed form for the forward crossed negative-begin truncated clamping below-end
cell.** Every reported key lies below `end_.key`, so the end selector can be dropped to
`⊤` and the no-gap rule applies. -/
theorem minimal_closedForm_of_negCross_trunc_clamp_below {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampBelowEnd r res) :
Minimal r res (closedFormNegCrossTruncClampBelowEnd r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hend, hcrossD, hgap, hclamp, hbelow⟩ := hcell
obtain ⟨db, hcomp⟩ := hread
have hcompiff : ∀ db, RangeRead.Compatible db r res ↔
RangeRead.Compatible db { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res :=
fun db => compatible_iff_end_top_of_below hrev hmore hne hend hbelow
have hcellTop : negCrossTruncClampNoGapTop
{ r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res :=
⟨hmore, hrev, hbegin, hne, rfl, by simpa using hend, hcrossD,
by simpa using hgap, hclamp⟩
have hread' : Readable { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } } res :=
⟨db, (hcompiff db).mp hcomp⟩
have hmin := minimal_closedForm_of_negCross_trunc_clamp_noGap_top hread' hcellTop
unfold closedFormNegCrossTruncClampBelowEnd
exact (minimal_congr_of_compatible_iff (r := r)
(r' := { r with end_ := { r.end_ with key := (⊤ : WithTop Key) } })
(res := res) (cr := closedFormNegCrossTruncClampNoGapTop r res) hcompiff).mpr hmin
/-- **In the no-gap clamping cell every compatible database has a clamping begin
anchor.** The reported keys are exactly the stored keys in the truncated window, so the
count of stored keys below `begin.key` is the reported length plus the count below the
begin resolution; the anchor equation then forces the resolution to clamp. -/
theorem anchor_neg_of_negCross_trunc_clamp_noGap {db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hrev : r.reverse = false)
(hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hgap : r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key))
(hclamp : reportedBelowBegin r res ≤ beginSlack r) :
r.begin.anchor db < 0 := by
set last : Key := res.keys.getLast hne with hlast
set head : Key := res.keys.head hne with hhead
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hgap' : r.begin.key = (((keyAfter last : Key)) : WithTop Key) := by
rw [hgap, hlastD]
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by
intro k hk
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by
rw [hgap']
exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)
have hheadB : ((head : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne))) hlastB
have hcount_rep : 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_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB
have hlen_le : res.keys.length ≤ beginSlack r := by
rw [← hcount_rep]; exact hclamp
have hpre : res.keys <+: RangeRead.completeKeys db r := RangeRead.compatible_keys_prefix hcomp
have hck_ne : RangeRead.completeKeys db r ≠ [] := hpre.ne_nil hne
have hres_eq : (RangeRead.completeKeys db r).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
have h := ((compatible_iff_truncated_keys (db := db) hrev hmore hne).mp hcomp).2
rwa [← hlast] at h
have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := by
have hlast_ck : last ∈ RangeRead.completeKeys db r :=
hpre.subset (by rw [hlast]; exact List.getLast_mem hne)
exact (RangeRead.mem_completeKeys.mp hlast_ck).2.2
by_contra hnot
have hanchor : 0 ≤ r.begin.anchor db := le_of_not_gt hnot
have hhead_ck : head ∈ RangeRead.completeKeys db r := hpre.subset (List.head_mem hne)
have hrb_le_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp hhead_ck).2.1
have hrb_ne_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
intro htop
rw [htop] at hrb_le_head
exact absurd hrb_le_head (not_le.mpr (WithTop.coe_lt_top head))
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
by_contra h
exact hrb_ne_top (resolve_eq_top hanchor (le_of_not_gt h))
have hrb_get : resolve db r.begin =
(((db.keys[(r.begin.anchor db).toNat] : Key)) : WithTop Key) :=
resolve_eq_getElem hanchor hltlen
have hrbkey_le_head : db.keys[(r.begin.anchor db).toNat] ≤ head :=
WithTop.coe_le_coe.mp (by rw [← hrb_get]; exact hrb_le_head)
have hhead_lt_re : ((head : Key) : WithTop Key) < resolve db r.end_ :=
(RangeRead.mem_completeKeys.mp hhead_ck).2.2
have hrb_lt_re : ((db.keys[(r.begin.anchor db).toNat] : Key) : WithTop Key) <
resolve db r.end_ := by
rw [← hrb_get]
exact lt_of_le_of_lt hrb_le_head hhead_lt_re
have hrb_ck : db.keys[(r.begin.anchor db).toNat] ∈ RangeRead.completeKeys db r := by
rw [RangeRead.mem_completeKeys]
exact ⟨List.getElem_mem hltlen, by rw [← hrb_get], hrb_lt_re⟩
have hck_sorted : (RangeRead.completeKeys db r).SortedLT := by
rw [RangeRead.completeKeys, ite_eq_right (by simp [hrev])]
exact List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _)
have hhead_ck_eq : (RangeRead.completeKeys db r).head hck_ne = head := by
refine (List.head_eq_iff_head?_eq_some hck_ne).mpr ?_
obtain ⟨t, ht⟩ := hpre
rw [← ht, List.head?_append, (List.head_eq_iff_head?_eq_some hne).mp rfl,
Option.some_or]
have hhead_le_rbkey : head ≤ db.keys[(r.begin.anchor db).toNat] := by
have h := head_le_of_sortedLT' hck_sorted hck_ne hrb_ck
rwa [hhead_ck_eq] at h
have hrbeq : db.keys[(r.begin.anchor db).toNat] = head :=
le_antisymm hrbkey_le_head hhead_le_rbkey
have hrb_head : resolve db r.begin = ((head : Key) : WithTop Key) := by
rw [hrb_get, hrbeq]
have hcnt_rb : cnt db (resolve db r.begin) = (r.begin.anchor db).toNat := by
rw [cnt, countP_lt_resolve, Nat.min_eq_left (le_of_lt hltlen)]
have hbetween : between db ((head : Key) : WithTop Key) r.begin.key = res.keys := by
unfold between
rw [hgap', ← hres_eq]
unfold RangeRead.completeKeys RangeRead.inRange
rw [ite_eq_right (by simp [hrev]), hrb_head, List.filter_filter]
apply List.filter_congr
intro k _
by_cases hh : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)
· by_cases hl : k ≤ last
· have hkre : ((k : Key) : WithTop Key) < resolve db r.end_ :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hl) hlast_lt_re
have hklt : ((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key) :=
WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hl)
simp [hh, hl, hklt, hkre]
· have hklt : ¬ ((k : Key) : WithTop Key) < ((keyAfter last : Key) : WithTop Key) :=
fun h => hl ((lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h))
simp [hh, hl, hklt]
· simp [hh]
have hle_rb : resolve db r.begin ≤ r.begin.key := by
rw [hrb_head]; exact le_of_lt hheadB
have hcnt_split : cnt db r.begin.key = (r.begin.anchor db).toNat + res.keys.length := by
rw [cnt_eq_add_between db hle_rb, hcnt_rb, hrb_head, hbetween]
have hanchor_eq : (r.begin.anchor db : ℤ) =
(cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
rfl
have hcnt_splitz : (cnt db r.begin.key : ℤ) =
((r.begin.anchor db).toNat : ℤ) + (res.keys.length : ℤ) := by
rw [hcnt_split]; push_cast; ring
have htoNat : ((r.begin.anchor db).toNat : ℤ) = (r.begin.anchor db : ℤ) :=
Int.toNat_of_nonneg hanchor
have hcontra : (res.keys.length : ℤ) = (beginSlack r : ℤ) + 1 := by
have h := hanchor_eq
rw [hcnt_splitz, htoNat] at h
omega
have h3 : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hlen_le
omega
/-- **The resolved end overshoots the reported last on both sides of an agreement below
`begin.key`.** If `last` lies below `begin.key` and two databases agree on every stored
key below `begin.key`, then the end filter `[end_.key, last]` has the same length in
both, so one end resolution overshooting `last` forces the other to as well. -/
theorem lt_resolve_end_of_agree_below {db1 db2 : DB} {r : RangeRead} {last : Key}
(hend : 1 ≤ r.end_.offset)
(hlastB : ((last : Key) : WithTop Key) < r.begin.key)
(hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key →
(k ∈ db1.keys ↔ k ∈ db2.keys))
(h1 : ((last : Key) : WithTop Key) < resolve db1 r.end_) :
((last : Key) : WithTop Key) < resolve db2 r.end_ := by
have hM : (db1.keys.filter
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last)) =
(db2.keys.filter
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact (db1.sorted.pairwise.filter _).filter _
· exact (db2.sorted.pairwise.filter _).filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨⟨hdb, hE⟩, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨⟨(hmk k hkB).mp hdb, hE⟩, hle⟩
· rintro ⟨⟨hdb, hE⟩, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨⟨(hmk k hkB).mpr hdb, hE⟩, hle⟩
have hc1 := filter_length_le_offset_of_resolve_end_gt (db := db1) hend h1
have hc2 : ((db2.keys.filter
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last))).length ≤ (r.end_.offset - 1).toNat := by
rw [← hM]
exact hc1
exact resolve_end_gt_of_filter_length_le (db := db2) hend hc2
/-- **Compatibility transfers across agreement on the clamping range
`[⊥, begin.key)` for an arbitrary end key.** Agreement fixes every stored key below
`begin.key`, hence the begin resolution (negative offset) and both end-filter lengths;
the truncated complete keys below the reported last then coincide. The only geometric
input is `last < begin.key`, which holds both in the no-gap case and in a genuine gap. -/
theorem compatible_of_agree_negCross_trunc_clamp {r : RangeRead} {res : Result}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0)
(hendoff : 1 ≤ r.end_.offset)
(hlastB : (((res.keys.getLast hne : Key)) : WithTop Key) < r.begin.key)
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
(hagree : AgreeOn (closedFormNegCrossTruncClampNoGapTop r res) db1 db2) :
RangeRead.Compatible db2 r res := by
set last : Key := res.keys.getLast hne with hlast
have hcr : closedFormNegCrossTruncClampNoGapTop r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hmem := (agreeOn_iff (closedFormNegCrossTruncClampNoGapTop r res) db1 db2).mp hagree
have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key →
(k ∈ db1.keys ↔ k ∈ db2.keys) := by
intro k hk
refine hmem k ?_
rw [hcr]
exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩
have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) =
db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩
have hbeg : resolve db1 r.begin = resolve db2 r.begin := by
rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin),
resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF]
have hpre : res.keys <+: RangeRead.completeKeys db1 r := by
rcases hc1.2 with ⟨-, hpre, -⟩ | ⟨hm, -⟩
· exact hpre
· rw [hmore] at hm
exact absurd hm (by simp)
have hlast_ck : last ∈ RangeRead.completeKeys db1 r := by
refine hpre.subset ?_
rw [hlast]
exact List.getLast_mem hne
have hlt1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
(RangeRead.mem_completeKeys.mp hlast_ck).2.2
have hlt2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ :=
lt_resolve_end_of_agree_below (db1 := db1) (db2 := db2) (r := r) (last := last)
hendoff hlastB hmk hlt1
have hfil : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) =
(RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) := by
rw [completeKeys_filter_le_eq (db := db1) (r := r) hrev hlt1,
completeKeys_filter_le_eq (db := db2) (r := r) hrev hlt2, hbeg]
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨hdb, hlo, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨(hmk k hkB).mp hdb, hlo, hle⟩
· rintro ⟨hdb, hlo, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨(hmk k hkB).mpr hdb, hlo, hle⟩
have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) =
res.keys := by
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
rwa [← hlast] at h
rw [compatible_iff_truncated_keys hrev hmore hne]
refine ⟨by simpa using hc1.1, ?_⟩
rw [← hlast, ← hfil, hres1]
/-- The forward, crossed, truncated, clamping negative-begin cell in which the begin key
is exactly the successor of the reported last, with an arbitrary end key. -/
abbrev negCrossTruncClampNoGap (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
1 ≤ r.end_.offset ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
r.begin.key = (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ∧
reportedBelowBegin r res ≤ beginSlack r
/-- The closed form for the arbitrary-end no-gap clamping cell: `[⊥, begin.key)`. -/
noncomputable def closedFormNegCrossTruncClampNoGap (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩
/-- **Closed form for the forward crossed negative-begin truncated clamping no-gap
cell.** The minimal sound range is `[⊥, begin.key)`, for every end key. -/
theorem minimal_closedForm_of_negCross_trunc_clamp_noGap {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampNoGap r res) :
Minimal r res (closedFormNegCrossTruncClampNoGap r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp⟩ := hcell
obtain ⟨db₀, hcomp₀⟩ := hread
have hread' : Readable r res := ⟨db₀, hcomp₀⟩
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hheadD : res.keys.headD ([] : Key) = head := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hcross : ((head : Key) : WithTop Key) < r.begin.key := by
have hle : leastReported r res = res.keys.headD ([] : Key) := by
simp [leastReported, hrev]
rw [hle, hheadD] at hcrossD
exact hcrossD
have hcr : closedFormNegCrossTruncClampNoGap r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by
rw [hgap, hlastD]
exact WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)
have hsound : Sound r res (closedFormNegCrossTruncClampNoGap r res) := by
intro db1 db2 hagree
exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin
hendoff hlastB h hagree,
fun h => compatible_of_agree_negCross_trunc_clamp hrev hmore hne hbegin
hendoff hlastB h hagree.symm⟩
rw [hcr]
refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩
intro k _ hkend
have hkend' : k < keyAfter last := by
rw [hgap, hlastD] at hkend
exact WithTop.coe_lt_coe.mp hkend
have hkle : k ≤ last := (lt_keyAfter_iff k last).mp hkend'
by_cases hlt : k < head
· exact necessary_below_head_of_anchor_nonpos_negCross db₀ hcomp₀ hrev hne
(anchor_neg_of_negCross_trunc_clamp_noGap hcomp₀ hrev hmore hne hbegin hgap hclamp)
(WithTop.coe_lt_coe.mpr hlt)
· exact necessary_of_between_result hread' (by rw [hhead]; exact List.head_mem hne)
(by rw [hlast]; exact List.getLast_mem hne) (le_of_not_gt hlt) hkle
/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap and
insufficient gap capacity. Writing `q = beginSlack`, `t = #reported` and
`C = intervalCard last begin.key`, the capacity is insufficient when `C < q + 1 - t`.
The minimal range then collapses to `[⊥, keyAfter last)`: no gap key is necessary, and
compatibility is the same as for the read whose begin key is moved down to the immediate
successor of the reported last. -/
abbrev negCrossTruncClampGapLow (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
1 ≤ r.end_.offset ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧
reportedBelowBegin r res ≤ beginSlack r ∧
intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key ≤
((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞)
/-- The read with the begin key moved down to the immediate successor of the reported
last, used to reduce the insufficient-capacity gap cell to the no-gap cell. -/
def noGapRead (r : RangeRead) (res : Result) : RangeRead :=
{ r with begin := { r.begin with
key := (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) } }
/-- The closed form for the insufficient-capacity gap cell: `[⊥, keyAfter last)`. -/
noncomputable def closedFormNegCrossTruncClampGapLow (r : RangeRead) (res : Result) :
ConflictRange :=
closedFormNegCrossTruncClampNoGap (noGapRead r res) res
/-- **Every compatible database in the insufficient-capacity gap cell clamps its begin
selector.** Compatibility forces the stored keys at or below the reported last to be
exactly the reported keys; the gap above them has fewer than `q + 1 - t` stored keys, so
the count below `begin.key` is at most `q`, making the begin anchor negative. -/
theorem anchor_neg_of_negCross_trunc_clamp_gapLow {db : DB} {r : RangeRead} {res : Result}
(hcomp : RangeRead.Compatible db r res) (hcell : negCrossTruncClampGapLow r res) :
r.begin.anchor db < 0 := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hlow⟩ := hcell
set last : Key := res.keys.getLast hne with hlast
set head : Key := res.keys.head hne with hhead
set t : ℕ := res.keys.length with ht
set q : ℕ := beginSlack r with hq
set g : ℕ := (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length
with hg
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastD' : res.keys.getLast?.getD ([] : Key) = last := by
rw [List.getLast?_eq_some_getLast hne]; rfl
have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hgap
have hlastB : ((last : Key) : WithTop Key) < r.begin.key :=
lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap'
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp hrev
have hres_subl : res.keys <+: RangeRead.completeKeys db r :=
RangeRead.compatible_keys_prefix hcomp
have hres_eq : (RangeRead.completeKeys db r).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
have h := ((compatible_iff_truncated_keys (db := db) hrev hmore hne).mp hcomp).2
rwa [← hlast] at h
have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne
have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by
intro k hk
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ :=
(RangeRead.mem_completeKeys.mp (hres_subl.subset hlast_mem)).2.2
have hheadB : ((head : Key) : WithTop Key) < r.begin.key := by
have hle : head ≤ last := hmem_le_last head (by rw [hhead]; exact List.head_mem hne)
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
have hrb_le_head : resolve db r.begin ≤ ((head : Key) : WithTop Key) :=
(RangeRead.mem_completeKeys.mp
(hres_subl.subset (by rw [hhead]; exact List.head_mem hne))).2.1
have hrb_le_B : resolve db r.begin ≤ r.begin.key :=
lt_of_le_of_lt hrb_le_head hheadB |> le_of_lt
-- The stored keys in `[resolve begin, begin.key)` are the reported keys plus the
-- stored gap keys in `[keyAfter last, begin.key)`.
have hbetween_eq : between db (resolve db r.begin) r.begin.key =
res.keys ++ between db (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
apply eq_of_forall_mem_iff_of_pairwise
· simp only [between]
exact db.sorted.pairwise.filter _
· rw [← List.sortedLT_iff_pairwise, List.sortedLT_append]
refine ⟨hsort, List.sortedLT_iff_pairwise.mpr (db.sorted.pairwise.filter _), ?_⟩
intro a ha b hb
simp only [between, List.mem_filter, decide_eq_true_eq] at hb
have hale : a ≤ last := hmem_le_last a ha
exact lt_of_le_of_lt hale
(lt_of_lt_of_le (keyAfter_self_lt last) (WithTop.coe_le_coe.mp hb.2.1))
· intro k
simp only [between, List.mem_filter, List.mem_append, decide_eq_true_eq]
constructor
· rintro ⟨hkdb, hlo, hhi⟩
by_cases hkle : k ≤ last
· left
have hkre : ((k : Key) : WithTop Key) < resolve db r.end_ :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re
have hkck : k ∈ RangeRead.completeKeys db r :=
RangeRead.mem_completeKeys.mpr ⟨hkdb, hlo, hkre⟩
have : k ∈ (RangeRead.completeKeys db r).filter
(fun k : Key => decide (k ≤ last)) := by
rw [List.mem_filter]; exact ⟨hkck, by simpa using hkle⟩
rwa [hres_eq] at this
· right
refine ⟨hkdb, ?_, hhi⟩
exact le_of_not_gt (fun h => hkle
((lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp h)))
· rintro (hkres | ⟨hkdb, hlo, hhi⟩)
· refine ⟨result_sublist_db_of_compatible hcomp hrev |>.subset hkres, ?_, ?_⟩
· exact (RangeRead.mem_completeKeys.mp (hres_subl.subset hkres)).2.1
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hkres)) hlastB
· refine ⟨hkdb, ?_, hhi⟩
exact le_trans hrb_le_head (le_trans (WithTop.coe_le_coe.mpr (hmem_le_last head
(by rw [hhead]; exact List.head_mem hne)))
(le_trans (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt last))) hlo))
have hdec1 : cnt db r.begin.key =
cnt db (resolve db r.begin) + (between db (resolve db r.begin) r.begin.key).length :=
cnt_eq_add_between db hrb_le_B
have hIlen : (between db (resolve db r.begin) r.begin.key).length = t + g := by
rw [hbetween_eq, List.length_append, ht, hg]
-- If the anchor were nonnegative, the decomposition of `cnt begin.key` would force
-- `t + g = q + 1`, contradicting the insufficient-capacity bound.
by_contra hnot
have h0 : 0 ≤ r.begin.anchor db := le_of_not_gt hnot
have hrb_ne_top : resolve db r.begin ≠ (⊤ : WithTop Key) := by
intro htop
rw [htop] at hrb_le_head
exact absurd hrb_le_head (not_le.mpr (WithTop.coe_lt_top head))
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
by_contra hle
exact hrb_ne_top (resolve_eq_top h0 (le_of_not_gt hle))
have hcntres : cnt db (resolve db r.begin) = (r.begin.anchor db).toNat := by
rw [cnt, countP_lt_resolve, Nat.min_eq_left (le_of_lt hltlen)]
have hcnt_eq : cnt db r.begin.key = (r.begin.anchor db).toNat + t + g := by
rw [hdec1, hcntres, hIlen]
omega
have hanc : (r.begin.anchor db : ℤ) =
(cnt db r.begin.key : ℤ) - (q : ℤ) - 1 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
rfl
have htoNat : ((r.begin.anchor db).toNat : ℤ) = (r.begin.anchor db : ℤ) :=
Int.toNat_of_nonneg h0
have htg : t + g = q + 1 := by
have hcntz : (cnt db r.begin.key : ℤ) =
((r.begin.anchor db).toNat : ℤ) + (t : ℤ) + (g : ℤ) := by
rw [hcnt_eq]; push_cast; ring
have h := hanc
rw [hcntz, htoNat] at h
omega
have hgs : g = q + 1 - t := by omega
have hle_tq : t ≤ q := by
have : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
have hrep : reportedBelowBegin r res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB
omega
have hg_le_card : (g : ℕ∞) ≤ intervalCard (((keyAfter last : Key)) : WithTop Key)
r.begin.key :=
between_length_le_intervalCard (db := db) (le_of_lt hgap')
have hcard_one : intervalCard ((last : Key) : WithTop Key)
(((keyAfter last : Key)) : WithTop Key) = 1 := by
have := intervalCard_succChain last 1
simpa [keyAfter, List.replicate_one] using this
have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key =
intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key :=
intervalCard_add (b := ((last : Key) : WithTop Key))
(m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key)
(le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap')
have hg_succ_le : (g : ℕ∞) + 1 ≤ ((q + 1 - t : ℕ) : ℕ∞) := by
calc (g : ℕ∞) + 1
≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 :=
add_le_add_left hg_le_card 1
_ = intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
rw [hcard_one, add_comm]
_ = intervalCard ((last : Key) : WithTop Key) r.begin.key := hcard_add.symm
_ ≤ ((q + 1 - t : ℕ) : ℕ∞) := by simpa [hq, hlastD'] using hlow
have hg_lt : (g : ℕ∞) < ((q + 1 - t : ℕ) : ℕ∞) :=
(ENat.add_one_le_iff (by simp)).mp hg_succ_le
have hcontra : (g : ℕ∞) < (g : ℕ∞) := by
rw [← hgs] at hg_lt
exact hg_lt
exact absurd hcontra (lt_irrefl _)
/-- **Compatibility is unchanged by lowering the begin key to the successor of the
reported last in the insufficient-capacity gap cell.** The begin resolution clamps to
the empty key on both sides: for `r` by `anchor_neg_of_negCross_trunc_clamp_gapLow`,
and for the lowered read by `anchor_neg_of_negCross_trunc_clamp_noGap`, which also
pins the count below `keyAfter last` to the reported length. -/
theorem compatible_iff_noGapRead_of_gapLow {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapLow r res) (db : DB) :
RangeRead.Compatible db r res ↔ RangeRead.Compatible db (noGapRead r res) res := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hlow⟩ := hcell
set last : Key := res.keys.getLast hne with hlast
set head : Key := res.keys.head hne with hhead
set t : ℕ := res.keys.length with ht
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastD' : res.keys.getLast?.getD ([] : Key) = last := by
rw [List.getLast?_eq_some_getLast hne]; rfl
have hheadD : res.keys.headD ([] : Key) = head := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hheadD' : res.keys.head?.getD ([] : Key) = head := by
rw [List.head?_eq_some_head hne]; rfl
have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hgap
have hlastB : ((last : Key) : WithTop Key) < r.begin.key :=
lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap'
have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by
intro k hk
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
have hreported_r : reportedBelowBegin r res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB
have htq : t ≤ beginSlack r := by rw [← hreported_r]; exact hclamp
have hbetweenLen : (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length
= (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := rfl
constructor
· -- Forward: the original read clamps, hence so does the lowered read.
intro hcomp
have hanc_r : r.begin.anchor db < 0 :=
anchor_neg_of_negCross_trunc_clamp_gapLow hcomp ⟨hmore, hrev, hbegin, hne, hendoff,
hcrossD, hgap, hclamp, hlow⟩
have hres_r : resolve db r.begin = ((↑([] : Key) : WithTop Key)) := resolve_eq_nil hanc_r
have hcnt_le : cnt db (((keyAfter last : Key)) : WithTop Key) ≤ cnt db r.begin.key := by
unfold cnt
apply countP_le_countP_of_imp
intro k hk hlt
simp only [decide_eq_true_eq] at hlt ⊢
exact lt_trans hlt hgap'
have hanc_ng : (noGapRead r res).begin.anchor db < 0 := by
have h_eq_ng : (noGapRead r res).begin.anchor db =
(cnt db (((keyAfter last : Key)) : WithTop Key) : ℤ) - (beginSlack r : ℤ) - 1 := by
have hs : beginSlack (noGapRead r res) = beginSlack r := rfl
rw [anchor_eq_count_sub_slack db (noGapRead r res) (le_of_lt hbegin), hs]
simp [cnt, noGapRead, hlastD, hlastD']
have h_eq_r : r.begin.anchor db =
(cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
simp [cnt]
rw [h_eq_ng]
have hlt : (cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 < 0 := by
rw [← h_eq_r]; exact hanc_r
have : (cnt db (((keyAfter last : Key)) : WithTop Key) : ℤ) ≤ (cnt db r.begin.key : ℤ) := by
exact_mod_cast hcnt_le
omega
have hres_ng : resolve db (noGapRead r res).begin =
((↑([] : Key) : WithTop Key)) := resolve_eq_nil hanc_ng
have hck_eq : RangeRead.completeKeys db (noGapRead r res) = RangeRead.completeKeys db r := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_r, hres_ng]
rfl
rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne]
rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne] at hcomp
exact ⟨by change RangeRead.withinLimit r res.keys; exact hcomp.1, by rw [hck_eq]; exact hcomp.2⟩
· -- Reverse: the lowered read clamps, which forces the count below `begin.key` to fit.
intro hcompNG
have hcrossNG : ((leastReported (noGapRead r res) res : Key) : WithTop Key) <
(noGapRead r res).begin.key := by
have hle : leastReported (noGapRead r res) res = head := by
unfold leastReported
rw [show (noGapRead r res).reverse = false from hrev]
simp [hheadD']
rw [hle, show (noGapRead r res).begin.key =
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne)))
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))
have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
rw [show (noGapRead r res).begin.key =
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD]
exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk))
have hclampNG : reportedBelowBegin (noGapRead r res) res ≤ beginSlack (noGapRead r res) := by
rw [hreported_ng]
have hs : beginSlack (noGapRead r res) = beginSlack r := rfl
rw [hs]; exact htq
have hcellNG : negCrossTruncClampNoGap (noGapRead r res) res :=
⟨hmore, hrev, hbegin, hne, hendoff, hcrossNG, rfl, hclampNG⟩
have hancNG : (noGapRead r res).begin.anchor db < 0 :=
anchor_neg_of_negCross_trunc_clamp_noGap hcompNG hrev hmore hne hbegin rfl hclampNG
have hres_ng : resolve db (noGapRead r res).begin =
((↑([] : Key) : WithTop Key)) := resolve_eq_nil hancNG
have hres_eqNG : (RangeRead.completeKeys db (noGapRead r res)).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
have h := ((compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev
hmore hne).mp hcompNG).2
rwa [← hlast] at h
have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ := by
have hlast_mem : last ∈ res.keys := List.getLast_mem hne
have hmem : last ∈ (RangeRead.completeKeys db (noGapRead r res)).filter
(fun k : Key => decide (k ≤ last)) := by rw [hres_eqNG]; exact hlast_mem
exact (RangeRead.mem_completeKeys.mp (List.mem_filter.mp hmem).1).2.2
have hck_ng : RangeRead.completeKeys db (noGapRead r res) =
db.keys.filter (fun k : Key => decide ((↑k : WithTop Key) < resolve db r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [show (noGapRead r res).reverse = false from hrev, ite_eq_right (by simp), hres_ng]
simp [nil_le_key, noGapRead]
have hpred : ∀ k ∈ db.keys,
(((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key)) ↔
((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := by
intro k _
constructor
· intro hk
have hkle : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hk)
exact ⟨lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re, hkle⟩
· rintro ⟨hkre, hkle⟩
exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle)
have hcnt_keyAfter : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by
rw [cnt, List.countP_eq_length_filter]
have hl : db.keys.filter
(fun k : Key => decide (((k : Key) : WithTop Key) <
(((keyAfter last : Key)) : WithTop Key))) =
(RangeRead.completeKeys db (noGapRead r res)).filter
(fun k : Key => decide (k ≤ last)) := by
rw [hck_ng, List.filter_filter]
apply List.filter_congr
intro k hk
by_cases h1 : (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key))
· have h2 := (hpred k hk).mp h1
simp [h1, h2.1, h2.2]
· have h2 : ¬ ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) :=
fun h => h1 ((hpred k hk).mpr h)
by_cases hA : (((k : Key) : WithTop Key) < resolve db r.end_)
· have hB : ¬ k ≤ last := fun hB => h2 ⟨hA, hB⟩
simp [h1, hA, hB]
· simp [h1, hA]
rw [hl, hres_eqNG, ht]
have hcnt_split : cnt db r.begin.key = t +
(between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length := by
rw [cnt_eq_add_between db (le_of_lt hgap'), hcnt_keyAfter]
have hcard_one : intervalCard ((last : Key) : WithTop Key)
(((keyAfter last : Key)) : WithTop Key) = 1 := by
have := intervalCard_succChain last 1
simpa [keyAfter, List.replicate_one] using this
have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key =
intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key :=
intervalCard_add (b := ((last : Key) : WithTop Key))
(m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key)
(le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap')
have hg_succ_le : (((between db (((keyAfter last : Key)) : WithTop Key)
r.begin.key).length : ℕ) : ℕ∞) + 1 ≤ ((beginSlack r + 1 - t : ℕ) : ℕ∞) := by
calc (((between db (((keyAfter last : Key)) : WithTop Key)
r.begin.key).length : ℕ) : ℕ∞) + 1
≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 :=
add_le_add_left (between_length_le_intervalCard (db := db) (le_of_lt hgap')) 1
_ = intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
rw [hcard_one, add_comm]
_ = intervalCard ((last : Key) : WithTop Key) r.begin.key := hcard_add.symm
_ ≤ ((beginSlack r + 1 - t : ℕ) : ℕ∞) := by simpa [hlastD'] using hlow
have hg_lt : (((between db (((keyAfter last : Key)) : WithTop Key)
r.begin.key).length : ℕ) : ℕ∞) < ((beginSlack r + 1 - t : ℕ) : ℕ∞) :=
(ENat.add_one_le_iff (by simp)).mp hg_succ_le
have htg_le : t + (between db (((keyAfter last : Key)) : WithTop Key)
r.begin.key).length ≤ beginSlack r := by
have hgl : (between db (((keyAfter last : Key)) : WithTop Key)
r.begin.key).length < beginSlack r + 1 - t := ENat.coe_lt_coe.mp hg_lt
omega
have hanc_r : r.begin.anchor db < 0 := by
have h_eq_r : r.begin.anchor db =
(cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
simp [cnt]
rw [h_eq_r, hcnt_split]
have : ((t + (between db (((keyAfter last : Key)) : WithTop Key)
r.begin.key).length : ℕ) : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast htg_le
omega
have hres_r : resolve db r.begin = ((↑([] : Key) : WithTop Key)) := resolve_eq_nil hanc_r
have hck_eq : RangeRead.completeKeys db (noGapRead r res) = RangeRead.completeKeys db r := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_r, hres_ng]
rfl
rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne]
rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne] at hcompNG
exact ⟨hcompNG.1, by rw [← hck_eq]; exact hcompNG.2⟩
/-- The lowered read satisfies the no-gap clamping cell whenever the original read is in
the insufficient-capacity gap cell. -/
theorem negCrossTruncClampNoGap_noGapRead {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapLow r res) :
negCrossTruncClampNoGap (noGapRead r res) res := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hlow⟩ := hcell
set last : Key := res.keys.getLast hne with hlast
set head : Key := res.keys.head hne with hhead
set t : ℕ := res.keys.length with ht
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hheadD' : res.keys.head?.getD ([] : Key) = head := by
rw [List.head?_eq_some_head hne]; rfl
have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hgap
have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by
intro k hk
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
have hreported_r : reportedBelowBegin r res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk))
(lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap')
have htq : t ≤ beginSlack r := by rw [← hreported_r]; exact hclamp
have hcrossNG : ((leastReported (noGapRead r res) res : Key) : WithTop Key) <
(noGapRead r res).begin.key := by
have hle : leastReported (noGapRead r res) res = head := by
unfold leastReported
rw [show (noGapRead r res).reverse = false from hrev]
simp [hheadD']
rw [hle, show (noGapRead r res).begin.key =
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne)))
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))
have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
rw [show (noGapRead r res).begin.key =
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD]
exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk))
have hclampNG : reportedBelowBegin (noGapRead r res) res ≤ beginSlack (noGapRead r res) := by
rw [hreported_ng]
have hs : beginSlack (noGapRead r res) = beginSlack r := rfl
rw [hs]; exact htq
exact ⟨hmore, hrev, hbegin, hne, hendoff, hcrossNG, rfl, hclampNG⟩
/-- **Minimality of the insufficient-capacity gap closed form.** Compatibility is the
same as for the lowered no-gap read, so the no-gap closed form `[⊥, keyAfter last)` is
minimal here as well. -/
theorem minimal_closedForm_of_negCross_trunc_clamp_gapLow {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapLow r res) :
Minimal r res (closedFormNegCrossTruncClampGapLow r res) := by
have hiff : ∀ db, RangeRead.Compatible db r res ↔
RangeRead.Compatible db (noGapRead r res) res :=
fun db => compatible_iff_noGapRead_of_gapLow hread hcell db
have hreadNG : Readable (noGapRead r res) res := by
obtain ⟨db₀, hcomp₀⟩ := hread
exact ⟨db₀, (hiff db₀).mp hcomp₀⟩
have hcellNG : negCrossTruncClampNoGap (noGapRead r res) res :=
negCrossTruncClampNoGap_noGapRead hread hcell
exact (minimal_congr_of_compatible_iff hiff).mpr
(minimal_closedForm_of_negCross_trunc_clamp_noGap hreadNG hcellNG)
/-- For a truncated-compatible database whose complete window starts at the empty key,
the number of stored keys below `keyAfter last` is exactly the reported length. -/
private theorem cnt_keyAfter_getLast_eq_length_of_window {db : DB} {r : RangeRead} {res : Result}
(hne : res.keys ≠ [])
(hwin : RangeRead.completeKeys db r =
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)))
(hfil : (RangeRead.completeKeys db r).filter
(fun k : Key => decide (k ≤ res.keys.getLast hne)) = res.keys) :
cnt db (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) = res.keys.length := by
set last : Key := res.keys.getLast hne with hlast
have hlast_mem : last ∈ res.keys := by rw [hlast]; exact List.getLast_mem hne
have hmem : last ∈ (RangeRead.completeKeys db r).filter
(fun k : Key => decide (k ≤ last)) := by
rw [hfil]; exact hlast_mem
have hlast_lt_re : ((last : Key) : WithTop Key) < resolve db r.end_ :=
(RangeRead.mem_completeKeys.mp (List.mem_filter.mp hmem).1).2.2
have hpred : ∀ k ∈ db.keys,
(((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key)) ↔
((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) := by
intro k _
constructor
· intro hk
have hkle : k ≤ last := (lt_keyAfter_iff k last).mp (WithTop.coe_lt_coe.mp hk)
exact ⟨lt_of_le_of_lt (WithTop.coe_le_coe.mpr hkle) hlast_lt_re, hkle⟩
· rintro ⟨hkre, hkle⟩
exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr hkle)
rw [cnt, List.countP_eq_length_filter]
have hl : db.keys.filter
(fun k : Key => decide (((k : Key) : WithTop Key) <
(((keyAfter last : Key)) : WithTop Key))) =
(RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) := by
rw [hwin, List.filter_filter]
apply List.filter_congr
intro k hk
by_cases h1 : (((k : Key) : WithTop Key) < (((keyAfter last : Key)) : WithTop Key))
· have h2 := (hpred k hk).mp h1
simp [h1, h2.1, h2.2]
· have h2 : ¬ ((((k : Key) : WithTop Key) < resolve db r.end_) ∧ k ≤ last) :=
fun h => h1 ((hpred k hk).mpr h)
by_cases hA : (((k : Key) : WithTop Key) < resolve db r.end_)
· have hB : ¬ k ≤ last := fun hB => h2 ⟨hA, hB⟩
simp [h1, hA, hB]
· simp [h1, hA]
rw [hl, hfil]
/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap, the
reported head equal to the empty key, and gap capacity at most one more than the slack.
Then no key of the genuine gap is necessary and the minimal range is
`[⊥, keyAfter (last reported))`. -/
abbrev negCrossTruncClampGapBotMid (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
1 ≤ r.end_.offset ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧
reportedBelowBegin r res ≤ beginSlack r ∧
res.keys.headD ([] : Key) = ([] : Key) ∧
intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key ≤
(((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞)
/-- The closed form for the empty-head middle gap cell: `[⊥, keyAfter last)`. -/
noncomputable def closedFormNegCrossTruncClampGapBotMid (r : RangeRead) (res : Result) :
ConflictRange :=
closedFormNegCrossTruncClampNoGap (noGapRead r res) res
/-- **Compatibility is unchanged by lowering the begin key in the empty-head middle gap
cell.** The reported head is the empty key, which is the least key, so both reads resolve
their begin selector to `⊥`; the capacity bound keeps the number of stored keys below
`begin.key` at most `q + 1`, so the original read cannot resolve above `⊥` either. -/
theorem compatible_iff_noGapRead_of_gapBotMid {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapBotMid r res) (db : DB) :
RangeRead.Compatible db r res ↔ RangeRead.Compatible db (noGapRead r res) res := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hcap⟩ := hcell
set last : Key := res.keys.getLast hne with hlast
set head : Key := res.keys.head hne with hhead
set t : ℕ := res.keys.length with ht
set q : ℕ := beginSlack r with hq
set g : ℕ := (between db (((keyAfter last : Key)) : WithTop Key) r.begin.key).length
with hg
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastD' : res.keys.getLast?.getD ([] : Key) = last := by
rw [List.getLast?_eq_some_getLast hne]; rfl
have hheadD : res.keys.headD ([] : Key) = head := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hheadD' : res.keys.head?.getD ([] : Key) = head := by
rw [List.head?_eq_some_head hne]; rfl
have hhead_bot : head = ([] : Key) := by rw [← hheadD]; exact hheadbot
have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hgap
have hlastB : ((last : Key) : WithTop Key) < r.begin.key :=
lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap'
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by
intro k hk
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
have hreported_r : reportedBelowBegin r res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk)) hlastB
have htq : t ≤ q := by
rw [← hreported_r, hq]; exact hclamp
constructor
· -- Forward: the original read resolves its begin to `⊥` because its head is `⊥`.
intro hcomp
have hfil : (RangeRead.completeKeys db r).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
have h := ((compatible_iff_truncated_keys (db := db) hrev hmore hne).mp hcomp).2
rwa [← hlast] at h
have hhead_mem : res.keys.head hne ∈ RangeRead.completeKeys db r :=
(RangeRead.compatible_keys_prefix hcomp).subset (List.head_mem hne)
have hres_bot : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := by
have hle := (RangeRead.mem_completeKeys.mp hhead_mem).2.1
rw [← hhead] at hle
rw [hhead_bot] at hle
exact le_antisymm hle (coe_nil_le_key_withTop _)
have hwin : RangeRead.completeKeys db r =
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
resolve db r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), hres_bot]
simp [nil_le_key]
have hcntKA : cnt db (((keyAfter (res.keys.getLast hne) : Key)) : WithTop Key) =
res.keys.length :=
cnt_keyAfter_getLast_eq_length_of_window hne hwin hfil
have hcntKA' : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by
simpa [hlast, ht] using hcntKA
have hanchor_ng : (noGapRead r res).begin.anchor db < 0 := by
have h_eq : (noGapRead r res).begin.anchor db =
(cnt db (((keyAfter last : Key)) : WithTop Key) : ℤ) - (beginSlack r : ℤ) - 1 := by
have hs : beginSlack (noGapRead r res) = beginSlack r := rfl
rw [anchor_eq_count_sub_slack db (noGapRead r res) (le_of_lt hbegin), hs]
simp [cnt, noGapRead, hlastD, hlastD']
rw [h_eq, hcntKA']
have h1 : (t : ℤ) ≤ (q : ℤ) := by exact_mod_cast htq
rw [← hq]; omega
have hres_ng : resolve db (noGapRead r res).begin = ((↑([] : Key)) : WithTop Key) :=
resolve_eq_nil hanchor_ng
have hck_eq_ng : RangeRead.completeKeys db (noGapRead r res) =
RangeRead.completeKeys db r := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_ng, hres_bot]
simp [noGapRead]
rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne]
rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne] at hcomp
exact ⟨by change RangeRead.withinLimit r res.keys; exact hcomp.1, by rw [hck_eq_ng]; exact hcomp.2⟩
· -- Reverse: the lowered read resolves to `⊥`; the capacity bound keeps `r`'s anchor
-- nonpositive, and `⊥` is stored, so `r` resolves to `⊥` too.
intro hcompNG
have hcrossNG : ((leastReported (noGapRead r res) res : Key) : WithTop Key) <
(noGapRead r res).begin.key := by
have hle : leastReported (noGapRead r res) res = head := by
unfold leastReported
rw [show (noGapRead r res).reverse = false from hrev]
simp [hheadD']
rw [hle, show (noGapRead r res).begin.key =
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last head (List.head_mem hne)))
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))
have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
rw [show (noGapRead r res).begin.key =
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD]
exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk))
have hclampNG : reportedBelowBegin (noGapRead r res) res ≤ beginSlack (noGapRead r res) := by
rw [hreported_ng]
have hs : beginSlack (noGapRead r res) = beginSlack r := rfl
rw [hs]; exact htq
have hancNG : (noGapRead r res).begin.anchor db < 0 :=
anchor_neg_of_negCross_trunc_clamp_noGap hcompNG hrev hmore hne hbegin rfl hclampNG
have hres_ng : resolve db (noGapRead r res).begin =
((↑([] : Key)) : WithTop Key) := resolve_eq_nil hancNG
have hfilNG : (RangeRead.completeKeys db (noGapRead r res)).filter
(fun k : Key => decide (k ≤ last)) = res.keys := by
have h := ((compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev
hmore hne).mp hcompNG).2
rwa [← hlast] at h
have hwinNG : RangeRead.completeKeys db (noGapRead r res) =
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
resolve db r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [show (noGapRead r res).reverse = false from hrev, ite_eq_right (by simp), hres_ng]
simp [noGapRead, nil_le_key]
have hcntKA : cnt db (((keyAfter last : Key)) : WithTop Key) = t := by
have h := cnt_keyAfter_getLast_eq_length_of_window hne hwinNG hfilNG
simpa [hlast, ht] using h
have hcnt : cnt db r.begin.key = t + g := by
rw [cnt_eq_add_between db (le_of_lt hgap'), hcntKA]
-- Capacity bound: the gap fits at most `q + 1 - t` stored keys.
have hcard_one : intervalCard ((last : Key) : WithTop Key)
(((keyAfter last : Key)) : WithTop Key) = 1 := by
have := intervalCard_succChain last 1
simpa [keyAfter, List.replicate_one] using this
have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key =
intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key :=
intervalCard_add (b := ((last : Key) : WithTop Key))
(m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key)
(le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap')
have hg_le : (g : ℕ∞) ≤ intervalCard (((keyAfter last : Key)) : WithTop Key)
r.begin.key :=
between_length_le_intervalCard (db := db) (le_of_lt hgap')
have hcap' : (((g + 1 : ℕ) : ℕ∞)) ≤ (((q + 1 - t) + 1 : ℕ) : ℕ∞) := by
calc (((g + 1 : ℕ) : ℕ∞))
= ((g : ℕ∞) + 1) := by rw [Nat.cast_add, Nat.cast_one]
_ ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 :=
add_le_add_left hg_le 1
_ = intervalCard ((last : Key) : WithTop Key) r.begin.key := by
rw [hcard_add, hcard_one]
exact add_comm _ _
_ ≤ (((q + 1 - t) + 1 : ℕ) : ℕ∞) := by simpa [hlastD', hq, ht] using hcap
have hg1 : g + 1 ≤ (q + 1 - t) + 1 := ENat.coe_le_coe.mp hcap'
have htg : t + g ≤ q + 1 := by omega
have hanchor : r.begin.anchor db =
(cnt db r.begin.key : ℤ) - (beginSlack r : ℤ) - 1 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]; simp [cnt]
have hanchor_le : r.begin.anchor db ≤ 0 := by
rw [hanchor, hcnt]
have h1 : ((t + g : ℕ) : ℤ) ≤ (q : ℤ) + 1 := by exact_mod_cast htg
have hq' : (q : ℤ) = (beginSlack r : ℤ) := by rw [hq]
rw [hq'] at h1
omega
have hbot_mem : ([] : Key) ∈ db.keys := by
have hsub := RangeRead.compatible_keys_subset (db := db) (r := noGapRead r res) hcompNG
exact hsub (by rw [← hhead_bot]; exact List.head_mem hne)
have hres_r : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := by
rcases lt_or_eq_of_le hanchor_le with hlt | heq
· exact resolve_eq_nil hlt
· have hpos : 0 ≤ r.begin.anchor db := le_of_eq heq.symm
have hlen_pos : 0 < db.keys.length := List.length_pos_of_mem hbot_mem
have hltlen : (r.begin.anchor db).toNat < db.keys.length := by
rw [heq]; simpa using hlen_pos
rw [resolve_eq_getElem hpos hltlen]
simp only [heq, Int.toNat_zero]
rw [keys_getElem_zero_eq_nil_of_mem hbot_mem]
have hck_eq : RangeRead.completeKeys db (noGapRead r res) =
RangeRead.completeKeys db r := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [show (noGapRead r res).reverse = false from hrev, hrev, hres_ng, hres_r]
simp [noGapRead]
rw [compatible_iff_truncated_keys (db := db) (r := r) hrev hmore hne]
rw [compatible_iff_truncated_keys (db := db) (r := noGapRead r res) hrev hmore hne] at hcompNG
exact ⟨by change RangeRead.withinLimit r res.keys; exact hcompNG.1, by rw [← hck_eq]; exact hcompNG.2⟩
/-- The lowered read satisfies the no-gap clamping cell whenever the original read is in
the empty-head middle gap cell. -/
theorem negCrossTruncClampNoGap_noGapRead_of_gapBotMid {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapBotMid r res) :
negCrossTruncClampNoGap (noGapRead r res) res := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hcap⟩ := hcell
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
set last : Key := res.keys.getLast hne with hlast
set t : ℕ := res.keys.length with ht
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hgap
have hmem_le_last : ∀ k ∈ res.keys, k ≤ last := by
intro k hk
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hk
have hreported_r : reportedBelowBegin r res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hmem_le_last k hk))
(lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap')
have htq : t ≤ beginSlack r := by rw [← hreported_r]; exact hclamp
have hleast : leastReported (noGapRead r res) res = ([] : Key) := by
unfold leastReported
rw [show (noGapRead r res).reverse = false from hrev]
simpa [List.headD] using hheadbot
refine ⟨hmore, hrev, hbegin, hne, hendoff, ?_, rfl, ?_⟩
· rw [hleast]
change ((↑([] : Key) : WithTop Key)) <
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key)
rw [hlastD]
exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt (nil_le_key last)
(keyAfter_self_lt last))
· have hreported_ng : reportedBelowBegin (noGapRead r res) res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro k hk
rw [decide_eq_true_eq]
rw [show (noGapRead r res).begin.key =
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) from rfl, hlastD]
exact WithTop.coe_lt_coe.mpr ((lt_keyAfter_iff k last).mpr (hmem_le_last k hk))
rw [hreported_ng]
have hs : beginSlack (noGapRead r res) = beginSlack r := rfl
rw [hs]; exact htq
/-- **Minimality of the empty-head middle gap closed form.** Compatibility is the same
as for the lowered no-gap read, so the no-gap closed form `[⊥, keyAfter last)` is minimal
here as well. -/
theorem minimal_closedForm_of_negCross_trunc_clamp_gapBotMid {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapBotMid r res) :
Minimal r res (closedFormNegCrossTruncClampGapBotMid r res) := by
have hiff : ∀ db, RangeRead.Compatible db r res ↔
RangeRead.Compatible db (noGapRead r res) res :=
fun db => compatible_iff_noGapRead_of_gapBotMid hread hcell db
have hreadNG : Readable (noGapRead r res) res := by
obtain ⟨db₀, hcomp₀⟩ := hread
exact ⟨db₀, (hiff db₀).mp hcomp₀⟩
have hcellNG : negCrossTruncClampNoGap (noGapRead r res) res :=
negCrossTruncClampNoGap_noGapRead_of_gapBotMid hread hcell
exact (minimal_congr_of_compatible_iff hiff).mpr
(minimal_closedForm_of_negCross_trunc_clamp_noGap hreadNG hcellNG)
/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap, the
reported head equal to the empty key, and gap capacity strictly above the middle bound.
The minimal range is `[⊥, begin.key)`. -/
abbrev negCrossTruncClampGapHighBot (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
1 ≤ r.end_.offset ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧
reportedBelowBegin r res ≤ beginSlack r ∧
res.keys.headD ([] : Key) = ([] : Key) ∧
(((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) < r.end_.key ∧
(((beginSlack r + 1 - res.keys.length) + 1 : ℕ) : ℕ∞) <
intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key
/-- The closed form for the empty-head high-capacity gap cell: `[⊥, begin.key)`. -/
noncomputable def closedFormNegCrossTruncClampGapHighBot (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩
/-- **Compatibility transfers across agreement on the high-capacity gap range
`[⊥, begin.key)`.** The reported keys all lie below `begin.key`, the end selector lies
above the reported last, and every compatible database has begin resolution `⊥`, so the
truncated complete keys depend only on the stored keys below `begin.key`. -/
theorem compatible_of_agree_negCross_trunc_clamp_gapHighBot {r : RangeRead} {res : Result}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset)
(hheadbot : res.keys.headD ([] : Key) = ([] : Key))
(hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) <
r.begin.key)
(hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key)
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
(hagree : AgreeOn (closedFormNegCrossTruncClampGapHighBot r res) db1 db2) :
RangeRead.Compatible db2 r res := by
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hhead_bot : res.keys.head hne = ([] : Key) := by
rw [← hheadD]; exact hheadbot
have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by
rw [hlastD] at hgap
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt hgap)
have hcr : closedFormNegCrossTruncClampGapHighBot r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hmem := (agreeOn_iff (closedFormNegCrossTruncClampGapHighBot r res) db1 db2).mp hagree
have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key →
(k ∈ db1.keys ↔ k ∈ db2.keys) := by
intro k hk
refine hmem k ?_
rw [hcr]
exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩
have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) =
db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩
have hbeg : resolve db1 r.begin = resolve db2 r.begin := by
rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin),
resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF]
have hbot_res : ([] : Key) ∈ res.keys := by
rw [← hhead_bot]; exact List.head_mem hne
have hbeg1 : resolve db1 r.begin = ((↑([] : Key)) : WithTop Key) := by
have hmem_ck := (RangeRead.compatible_keys_prefix hc1).subset hbot_res
have hle := (RangeRead.mem_completeKeys.mp hmem_ck).2.1
exact le_antisymm hle (coe_nil_le_key_withTop _)
have hbeg2 : resolve db2 r.begin = ((↑([] : Key)) : WithTop Key) := by
rw [← hbeg]; exact hbeg1
have hE1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
lt_resolve_end_of_lt_end_key hendoff hlastE'
have hE2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ :=
lt_resolve_end_of_lt_end_key hendoff hlastE'
have hflt : db1.keys.filter (fun k : Key => decide (k ≤ last)) =
db2.keys.filter (fun k : Key => decide (k ≤ last)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
constructor
· rintro ⟨hdb, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨(hmk k hkB).mp hdb, hle⟩
· rintro ⟨hdb, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨(hmk k hkB).mpr hdb, hle⟩
have hck1 : RangeRead.completeKeys db1 r =
db1.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db1 r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), hbeg1]
simp [nil_le_key]
have hck2 : RangeRead.completeKeys db2 r =
db2.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) < resolve db2 r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), hbeg2]
simp [nil_le_key]
have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) =
res.keys := by
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
rwa [← hlast] at h
have hfil1 : db1.keys.filter (fun k : Key => decide (k ≤ last)) = res.keys := by
rw [hck1, List.filter_filter] at hres1
have hsame : db1.keys.filter (fun k : Key => decide (k ≤ last) && decide
(((k : Key) : WithTop Key) < resolve db1 r.end_)) =
db1.keys.filter (fun k : Key => decide (k ≤ last)) := by
apply List.filter_congr
intro k _
by_cases hle : k ≤ last
· have hlt := lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE1
simp [hle, hlt]
· simp [hle]
rw [hsame] at hres1
exact hres1
have hfil2 : db2.keys.filter (fun k : Key => decide (k ≤ last)) = res.keys := by
rw [← hflt, hfil1]
rw [compatible_iff_truncated_keys (db := db2) hrev hmore hne]
refine ⟨by simpa using hc1.1, ?_⟩
rw [hck2, List.filter_filter]
have hsame2 : db2.keys.filter (fun k : Key => decide (k ≤ last) && decide
(((k : Key) : WithTop Key) < resolve db2 r.end_)) =
db2.keys.filter (fun k : Key => decide (k ≤ last)) := by
apply List.filter_congr
intro k _
by_cases hle : k ≤ last
· have hlt := lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE2
simp [hle, hlt]
· simp [hle]
rw [hsame2, hfil2]
/-- A database formed by the reported keys followed by `T` gap keys in
`(keyAfter last, begin.key)` is compatible with the high-capacity gap cell. Its begin
anchor is `0`, it resolves the begin to the empty key, and its end resolution lies above
the reported last. -/
theorem compatible_of_append_gapKeys_highBot {r : RangeRead} {res : Result} {db : DB}
{J : List Key}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset)
(hlim : r.withinLimit res.keys)
(htq : res.keys.length ≤ beginSlack r)
(hheadbot : res.keys.headD ([] : Key) = ([] : Key))
(hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key)
(hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) <
r.begin.key)
(hsort : res.keys.SortedLT) (hJ : J.SortedLT)
(hJrange : ∀ x ∈ J, (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)
(hJlen : J.length = beginSlack r + 1 - res.keys.length)
(hkeys : db.keys = res.keys ++ J) :
RangeRead.Compatible db r res := by
set last : Key := res.keys.getLast hne with hlast
set t : ℕ := res.keys.length with ht
set q : ℕ := beginSlack r with hq
set T : ℕ := q + 1 - t with hT
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hheadD : res.keys.headD ([] : Key) = res.keys.head hne := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hhead_bot : res.keys.head hne = ([] : Key) := by
rw [← hheadD]; exact hheadbot
have hJlen' : J.length = T := by omega
have hTpos : 1 ≤ T := by
rw [hT]; omega
have hlast_le : ∀ x ∈ res.keys, x ≤ last := by
intro x hx
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
have hxle : ((x : Key) : WithTop Key) ≤ ((last : Key) : WithTop Key) :=
WithTop.coe_le_coe.mpr (hlast_le x hx)
exact lt_of_le_of_lt hxle (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt (by rw [← hlastD]; exact hgap)))
have hJgt_last : ∀ x ∈ J, last < x := by
intro x hx
have := (hJrange x hx).1
rw [hlastD] at this
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) this)
have hJlt_B : ∀ x ∈ J, ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
exact (hJrange x hx).2
have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by
intro x hx y hy
exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy)
have hcntB : cnt db r.begin.key = t + T := by
rw [cnt, List.countP_eq_length_filter]
have hf : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
r.begin.key)) = db.keys := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
rw [hkeys] at hx
rcases List.mem_append.mp hx with hx | hx
· exact hres_lt_B x hx
· exact hJlt_B x hx
rw [hf, hkeys, List.length_append, ht, hJlen']
have hanchorB : r.begin.anchor db = 0 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
change ((cnt db r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 0
rw [hcntB, ← hq]
change ((t + T : ℕ) : ℤ) - (q : ℤ) - 1 = 0
rw [hT]
omega
have hbot_mem : ([] : Key) ∈ db.keys := by
rw [hkeys, ← hhead_bot]
exact List.mem_append_left _ (List.head_mem hne)
have hltlenB : (r.begin.anchor db).toNat < db.keys.length := by
rw [hanchorB]; exact List.length_pos_of_mem hbot_mem
have hz? : db.keys[(r.begin.anchor db).toNat]? = some ([] : Key) := by
rw [hanchorB, Int.toNat_zero]
rw [List.getElem?_eq_getElem (h := List.length_pos_of_mem hbot_mem)]
exact congrArg some (keys_getElem_zero_eq_nil_of_mem hbot_mem)
have hz : db.keys[(r.begin.anchor db).toNat]'(hltlenB) = ([] : Key) := by
apply Option.some.inj
rw [← List.getElem?_eq_getElem (h := hltlenB)]
exact hz?
have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hresB : resolve db r.begin = ((↑([] : Key)) : WithTop Key) := by
rw [resolve_eq_getElem (by omega) hltlenB]
exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hz
have hemptyM : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last))).length = 0 := by
rw [List.length_eq_zero_iff, List.eq_nil_iff_forall_not_mem]
intro x hx
rw [List.mem_filter, List.mem_filter] at hx
obtain ⟨⟨hxdb, hxE⟩, hxle⟩ := hx
rw [hkeys] at hxdb
rcases List.mem_append.mp hxdb with hx | hx
· have hxB : ((x : Key) : WithTop Key) < r.end_.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
(lt_of_lt_of_le hlastE' le_rfl)
exact absurd (of_decide_eq_true hxE) (not_le.mpr hxB)
· exact absurd (of_decide_eq_true hxle) (not_le.mpr (hJgt_last x hx))
have hresE : ((last : Key) : WithTop Key) < resolve db r.end_ :=
resolve_end_gt_of_filter_length_le hendoff (by rw [hemptyM]; omega)
have hck : RangeRead.completeKeys db r =
db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
resolve db r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), hresB]
simp [nil_le_key]
have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) =
res.keys := by
rw [hck, hkeys, List.filter_filter, List.filter_append]
have h1 : res.keys.filter (fun k : Key => decide (k ≤ last) && decide
(((k : Key) : WithTop Key) < resolve db r.end_)) = res.keys := by
rw [List.filter_eq_self]
intro k hk
rw [Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq]
exact ⟨hlast_le k hk, lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le k hk)) hresE⟩
have h2 : J.filter (fun k : Key => decide (k ≤ last) && decide
(((k : Key) : WithTop Key) < resolve db r.end_)) = [] := by
rw [List.filter_eq_nil_iff]
intro k hk hcon
rw [Bool.and_eq_true] at hcon
exact absurd (of_decide_eq_true hcon.1) (not_le.mpr (hJgt_last k hk))
rw [h1, h2, List.append_nil]
rw [compatible_iff_truncated_keys (db := db) hrev hmore hne]
exact ⟨hlim, hfil⟩
/-- **Gap keys are necessary in the empty-head high-capacity gap cell.** A key between
the reported last and `begin.key` is necessary: a witness database with exactly
`beginSlack + 1 - #reported` gap keys resolves the begin to `⊥`, while inserting the
target raises the begin anchor to `1`, so the empty key drops out of the truncated
complete keys and the reported result is no longer compatible. -/
theorem necessary_gap_highBot {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncClampGapHighBot r res)
(hkgt : res.keys.getLastD ([] : Key) < k)
(hkB : ((k : Key) : WithTop Key) < r.begin.key) :
Necessary r res k := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hlastE,
hcap⟩ := hcell
obtain ⟨db₀, hcomp₀⟩ := hread
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : r.withinLimit res.keys := hcomp₀.1
set last : Key := res.keys.getLast hne with hlast
set q : ℕ := beginSlack r with hq
set t : ℕ := res.keys.length with ht
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by
rw [hlastD] at hlastE; exact hlastE
have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hgap
have hlastB : ((last : Key) : WithTop Key) < r.begin.key :=
lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap'
have hkgt' : last < k := by rw [← hlastD]; exact hkgt
have hlast_le : ∀ x ∈ res.keys, x ≤ last := by
intro x hx
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
have hreported : reportedBelowBegin r res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB
have htq : t ≤ q := by rw [← hreported, hq]; exact hclamp
have hTpos : 1 ≤ q + 1 - t := by omega
-- Capacity: one more than the number of chosen gap keys fits in `(keyAfter last, B)`.
have hcard_one : intervalCard ((last : Key) : WithTop Key)
(((keyAfter last : Key)) : WithTop Key) = 1 := by
have := intervalCard_succChain last 1
simpa [keyAfter, List.replicate_one] using this
have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key =
intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key :=
intervalCard_add (b := ((last : Key) : WithTop Key))
(m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key)
(le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap')
have hD : (((q + 1 - t : ℕ) : ℕ∞) + 1) ≤
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
rw [hlastD] at hcap
have hC : intervalCard ((last : Key) : WithTop Key) r.begin.key =
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 := by
rw [hcard_add, hcard_one, add_comm]
rw [hC] at hcap
have hcast : (((q + 1 - t : ℕ) + 1 : ℕ) : ℕ∞) = ((q + 1 - t : ℕ) : ℕ∞) + 1 := by
push_cast; ring
rw [hcast] at hcap
by_cases htop : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = ⊤
· rw [htop]; exact le_top
· exact (ENat.lt_add_one_iff
(n := intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key) htop).mp hcap
-- Choose the gap keys avoiding the target.
obtain ⟨dbJ, hJmem, hJavoid, hJlen⟩ := exists_db_keysIn_avoid
(B := (((keyAfter last : Key)) : WithTop Key)) (E := r.begin.key) (k := k)
(m := q + 1 - t) hD
have hJrange : ∀ x ∈ dbJ.keys,
(((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧
((x : Key) : WithTop Key) < r.begin.key :=
fun x hx => hJmem x hx
have hJgt_last : ∀ x ∈ dbJ.keys, last < x := by
intro x hx
have h1 := (hJrange x hx).1
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1)
have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ dbJ.keys, x < y := by
intro x hx y hy
exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy)
have hsorted : (res.keys ++ dbJ.keys).SortedLT :=
List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hres_lt_J⟩
let db : DB := ⟨res.keys ++ dbJ.keys, hsorted⟩
have hkeys : db.keys = res.keys ++ dbJ.keys := rfl
have hJrange' : ∀ x ∈ dbJ.keys,
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
have h := hJrange x hx
rw [← hlastD] at h
exact h
have hJlen' : dbJ.keys.length = beginSlack r + 1 - res.keys.length := by
rw [hJlen, ← hq, ← ht]
have hcomp : RangeRead.Compatible db r res :=
compatible_of_append_gapKeys_highBot hrev hmore hne hbegin hendoff hlim htq hheadbot
hlastE hgap hsort dbJ.sorted hJrange' hJlen' hkeys
have hknot : k ∉ db.keys := by
rw [hkeys]
intro hk
rcases List.mem_append.mp hk with hk | hk
· exact absurd (hlast_le k hk) (not_le.mpr hkgt')
· exact hJavoid hk
refine necessary_of_toggle ⟨db, hcomp, ?_⟩
intro hcompk
rw [toggle_eq_insert_of_not_mem hknot] at hcompk
-- The inserted database has begin anchor `1` and resolves the begin above `⊥`.
have hcntDb : cnt db r.begin.key = res.keys.length + dbJ.keys.length := by
rw [cnt, List.countP_eq_length_filter]
have hf : db.keys.filter (fun x : Key => decide (((x : Key) : WithTop Key) <
r.begin.key)) = db.keys := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
rw [hkeys] at hx
rcases List.mem_append.mp hx with hx | hx
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB
· exact (hJrange x hx).2
rw [hf, hkeys, List.length_append]
have hcntT : cnt db r.begin.key = q + 1 := by
rw [hcntDb, hJlen, ← ht]
omega
have hcnt' : cnt (db.insert k hknot) r.begin.key = q + 2 := by
rw [cnt_insert_lt db k hknot hkB, hcntT]
have hanchor' : r.begin.anchor (db.insert k hknot) = 1 := by
rw [anchor_eq_count_sub_slack (db.insert k hknot) r (le_of_lt hbegin)]
change ((cnt (db.insert k hknot) r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 1
rw [hcnt', ← hq]
omega
have hbot_res : ([] : Key) ∈ res.keys := by
rw [← hheadbot]; exact headD_mem hne
have hbot_mem : ([] : Key) ∈ db.keys := by
rw [hkeys]; exact List.mem_append_left _ hbot_res
let db' : DB := db.insert k hknot
have hbot_mem' : ([] : Key) ∈ db'.keys :=
(DB.mem_insert_iff hknot).mpr (Or.inr hbot_mem)
have hlen_db : 2 ≤ db.keys.length := by
rw [hkeys, List.length_append]
have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne
omega
have hlen1 : 1 < db'.keys.length := by
change 1 < (db.insert k hknot).keys.length
rw [DB.insert_keys, List.orderedInsert_length]; omega
have hg0 : db'.keys[0] = ([] : Key) :=
keys_getElem_zero_eq_nil_of_mem hbot_mem'
have h01 : db'.keys[0] < db'.keys[1] :=
(db'.sorted.getElem_lt_getElem_iff).mpr (by omega : (0 : Nat) < 1)
have hgt0 : (([] : Key) : WithTop Key) < ((db'.keys[1] : Key) : WithTop Key) := by
rw [← hg0]; exact WithTop.coe_lt_coe.mpr h01
have hanchor_nat : (r.begin.anchor db').toNat = 1 := by
change (r.begin.anchor (db.insert k hknot)).toNat = 1
rw [hanchor']; rfl
have hanc' : r.begin.anchor db' = 1 := by
change r.begin.anchor (db.insert k hknot) = 1
exact hanchor'
have hresB' : resolve db' r.begin = ((db'.keys[1] : Key) : WithTop Key) := by
have h0' : 0 ≤ r.begin.anchor db' := by rw [hanc']; norm_num
have hlt : (r.begin.anchor db').toNat < db'.keys.length := by
rw [hanchor_nat]; exact hlen1
have hget := resolve_eq_getElem (db := db') (s := r.begin) h0' hlt
rw [hget]
have hidx : db'.keys[(r.begin.anchor db').toNat] = db'.keys[1] := by
rw [getElem_congr_idx (h' := hanchor_nat)]
rw [hidx]
have hnotbot : ([] : Key) ∉ RangeRead.completeKeys db' r := by
rw [RangeRead.mem_completeKeys]
rintro ⟨_, hle, _⟩
rw [hresB'] at hle
exact (not_le.mpr hgt0) hle
have hcompk' : RangeRead.Compatible db' r res := hcompk
have hfilter :=
((compatible_iff_truncated_keys (db := db') hrev hmore hne).mp hcompk').2
rw [← hlast] at hfilter
have hbot_in : ([] : Key) ∈ (RangeRead.completeKeys db' r).filter
(fun x : Key => decide (x ≤ last)) := by
rw [hfilter]; exact hbot_res
exact hnotbot (List.mem_filter.mp hbot_in).1
/-- **Minimality of the empty-head high-capacity gap closed form.** The minimal range
is `[⊥, begin.key)`: soundness is transfer across agreement on that range, the reported
span is necessary by convexity, and every gap key is necessary by
`necessary_gap_highBot`. -/
theorem minimal_closedForm_of_negCross_trunc_clamp_gapHighBot {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapHighBot r res) :
Minimal r res (closedFormNegCrossTruncClampGapHighBot r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadbot, hlastE,
hcap⟩ := hcell
have hcr : closedFormNegCrossTruncClampGapHighBot r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hsound : Sound r res (closedFormNegCrossTruncClampGapHighBot r res) := by
intro db1 db2 hagree
exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp_gapHighBot hrev hmore hne
hbegin hendoff hheadbot hgap hlastE h hagree,
fun h => compatible_of_agree_negCross_trunc_clamp_gapHighBot hrev hmore hne
hbegin hendoff hheadbot hgap hlastE h hagree.symm⟩
rw [hcr] at hsound ⊢
refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩
intro k _ hkend
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hbot_res : ([] : Key) ∈ res.keys := by
rw [← hheadbot]; exact headD_mem hne
by_cases hkle : k ≤ res.keys.getLastD ([] : Key)
· have hkle' : k ≤ last := by rw [hlastD] at hkle; exact hkle
exact necessary_of_between_result hread hbot_res
(by rw [hlast]; exact List.getLast_mem hne) (nil_le_key k) hkle'
· have hkgt : res.keys.getLastD ([] : Key) < k := lt_of_not_ge hkle
exact necessary_gap_highBot hread ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap,
hclamp, hheadbot, hlastE, hcap⟩ hkgt hkend
/-- A database formed by the reported keys followed by `T` gap keys in
`(keyAfter last, begin.key)` is compatible with the general high-capacity gap cell. Its
begin anchor is `0`, it resolves the begin to the reported head, and its end resolution
lies above the reported last. -/
theorem compatible_of_append_gapKeys_high {r : RangeRead} {res : Result} {db : DB}
{J : List Key}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset)
(hlim : r.withinLimit res.keys)
(htq : res.keys.length ≤ beginSlack r)
(hcnt : (res.keys.filter
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).length ≤
(r.end_.offset - 1).toNat)
(hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) <
r.begin.key)
(hsort : res.keys.SortedLT) (hJ : J.SortedLT)
(hJrange : ∀ x ∈ J, (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)
(hJlen : J.length = beginSlack r + 1 - res.keys.length)
(hkeys : db.keys = res.keys ++ J) :
RangeRead.Compatible db r res := by
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
set t : ℕ := res.keys.length with ht
set q : ℕ := beginSlack r with hq
set T : ℕ := q + 1 - t with hT
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hJlen' : J.length = T := by omega
have hTpos : 1 ≤ T := by rw [hT]; omega
have hlast_le : ∀ x ∈ res.keys, x ≤ last := by
intro x hx
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
have hhead_le : ∀ x ∈ res.keys, head ≤ x := by
intro x hx
rw [hhead]
exact (hsort.pairwise.imp le_of_lt).rel_head hx
have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
have hxle : ((x : Key) : WithTop Key) ≤ ((last : Key) : WithTop Key) :=
WithTop.coe_le_coe.mpr (hlast_le x hx)
exact lt_of_le_of_lt hxle (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt (by rw [← hlastD]; exact hgap)))
have hJgt_last : ∀ x ∈ J, last < x := by
intro x hx
have := (hJrange x hx).1
rw [hlastD] at this
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) this)
have hJlt_B : ∀ x ∈ J, ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
exact (hJrange x hx).2
have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by
intro x hx y hy
exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy)
have hcntB : cnt db r.begin.key = t + T := by
rw [cnt, List.countP_eq_length_filter]
have hf : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
r.begin.key)) = db.keys := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
rw [hkeys] at hx
rcases List.mem_append.mp hx with hx | hx
· exact hres_lt_B x hx
· exact hJlt_B x hx
rw [hf, hkeys, List.length_append, ht, hJlen']
have hanchorB : r.begin.anchor db = 0 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
change ((cnt db r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 0
rw [hcntB, ← hq]
change ((t + T : ℕ) : ℤ) - (q : ℤ) - 1 = 0
rw [hT]
omega
have hhead_mem : head ∈ res.keys := by rw [hhead]; exact List.head_mem hne
have hz? : db.keys[(r.begin.anchor db).toNat]? = some head := by
rw [hanchorB, Int.toNat_zero, hkeys]
rw [List.getElem?_append_left (by rw [← ht]; exact List.length_pos_of_ne_nil hne)]
rw [List.getElem?_eq_getElem (h := List.length_pos_of_ne_nil hne)]
exact congrArg some (by rw [hhead]; exact (List.head_eq_getElem hne).symm)
have hltlenB : (r.begin.anchor db).toNat < db.keys.length := by
rw [hanchorB, Int.toNat_zero]
rw [hkeys, List.length_append]
exact Nat.lt_of_lt_of_le (List.length_pos_of_ne_nil hne) (Nat.le_add_right _ _)
have hz : db.keys[(r.begin.anchor db).toNat]'(hltlenB) = head := by
apply Option.some.inj
rw [← List.getElem?_eq_getElem (h := hltlenB)]
exact hz?
have hresB : resolve db r.begin = ((head : Key) : WithTop Key) := by
rw [resolve_eq_getElem (by omega) hltlenB]
exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hz
have hM : ((db.keys.filter
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last))) =
res.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by
rw [hkeys, List.filter_append, List.filter_append]
have h1 : (res.keys.filter
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last)) =
res.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key))) := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
exact hlast_le x (List.mem_filter.mp hx).1
have h2 : (J.filter
(fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last)) = [] := by
rw [List.filter_eq_nil_iff]
intro x hx hcon
exact (not_le.mpr (hJgt_last x (List.mem_filter.mp hx).1))
(of_decide_eq_true hcon)
rw [h1, h2, List.append_nil]
have hresE : ((last : Key) : WithTop Key) < resolve db r.end_ :=
resolve_end_gt_of_filter_length_le hendoff (by rw [hM]; exact hcnt)
have hck : RangeRead.completeKeys db r =
db.keys.filter (fun k : Key => decide (head ≤ k) && decide
(((k : Key) : WithTop Key) < resolve db r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), hresB]
simp [WithTop.coe_le_coe]
have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) =
res.keys := by
rw [hck, hkeys, List.filter_filter, List.filter_append]
have h1 : res.keys.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) &&
decide (((k : Key) : WithTop Key) < resolve db r.end_))) = res.keys := by
rw [List.filter_eq_self]
intro k hk
rw [Bool.and_eq_true, Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq,
decide_eq_true_eq]
exact ⟨hlast_le k hk, hhead_le k hk,
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le k hk)) hresE⟩
have h2 : J.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) &&
decide (((k : Key) : WithTop Key) < resolve db r.end_))) = [] := by
rw [List.filter_eq_nil_iff]
intro k hk hcon
rw [Bool.and_eq_true] at hcon
exact absurd (of_decide_eq_true hcon.1) (not_le.mpr (hJgt_last k hk))
rw [h1, h2, List.append_nil]
rw [compatible_iff_truncated_keys (db := db) hrev hmore hne]
exact ⟨hlim, hfil⟩
/-- The forward, crossed, truncated, clamping negative-begin cell with a genuine gap, a
nonempty reported head, and gap capacity strictly above the low bound (`T < C`). The
minimal range is `[⊥, begin.key)`. -/
abbrev negCrossTruncClampGapHigh (r : RangeRead) (res : Result) : Prop :=
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
1 ≤ r.end_.offset ∧
((leastReported r res : Key) : WithTop Key) < r.begin.key ∧
(((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) < r.begin.key ∧
reportedBelowBegin r res ≤ beginSlack r ∧
res.keys.headD ([] : Key) ≠ ([] : Key) ∧
(((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) < r.end_.key ∧
((beginSlack r + 1 - res.keys.length : ℕ) : ℕ∞) <
intervalCard (((res.keys.getLastD ([] : Key)) : Key) : WithTop Key) r.begin.key
/-- The closed form for the nonempty-head high-capacity gap cell: `[⊥, begin.key)`. -/
noncomputable def closedFormNegCrossTruncClampGapHigh (r : RangeRead) (res : Result) :
ConflictRange :=
⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩
/-- A database whose keys are the empty key, the reported keys, and `T` gap keys has
begin anchor `1`, resolves the begin to the reported head, and is compatible with the
nonempty-head high-capacity gap cell. -/
theorem compatible_of_nil_cons_gapKeys_high {r : RangeRead} {res : Result} {db : DB}
{J : List Key}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset)
(hlim : r.withinLimit res.keys)
(htq : res.keys.length ≤ beginSlack r)
(hheadne : res.keys.headD ([] : Key) ≠ ([] : Key))
(hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key)
(hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) <
r.begin.key)
(hsort : res.keys.SortedLT) (hJ : J.SortedLT)
(hJrange : ∀ x ∈ J, (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) ≤
((x : Key) : WithTop Key) ∧ ((x : Key) : WithTop Key) < r.begin.key)
(hJlen : J.length = beginSlack r + 1 - res.keys.length)
(hkeys : db.keys = ([] : Key) :: (res.keys ++ J)) :
RangeRead.Compatible db r res := by
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
set t : ℕ := res.keys.length with ht
set q : ℕ := beginSlack r with hq
set T : ℕ := q + 1 - t with hT
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hheadD : res.keys.headD ([] : Key) = head := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne
have hJlen' : J.length = T := by omega
have hlast_le : ∀ x ∈ res.keys, x ≤ last := by
intro x hx
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
have hhead_le : ∀ x ∈ res.keys, head ≤ x := by
intro x hx
rw [hhead]
exact (hsort.pairwise.imp le_of_lt).rel_head hx
have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
have hxle : ((x : Key) : WithTop Key) ≤ ((last : Key) : WithTop Key) :=
WithTop.coe_le_coe.mpr (hlast_le x hx)
exact lt_of_le_of_lt hxle (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt (by rw [← hlastD]; exact hgap)))
have hJgt_last : ∀ x ∈ J, last < x := by
intro x hx
have := (hJrange x hx).1
rw [hlastD] at this
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) this)
have hJlt_B : ∀ x ∈ J, ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
exact (hJrange x hx).2
have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by
intro x hx y hy
exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy)
have hnil_lt_B : (([] : Key) : WithTop Key) < r.begin.key := by
have hlt : (([] : Key) : WithTop Key) < ((head : Key) : WithTop Key) :=
WithTop.coe_lt_coe.mpr (lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne'))
exact lt_trans hlt (hres_lt_B head (List.head_mem hne))
have hcntB : cnt db r.begin.key = 1 + t + T := by
rw [cnt, List.countP_eq_length_filter]
have hf : db.keys.filter (fun k : Key => decide (((k : Key) : WithTop Key) <
r.begin.key)) = db.keys := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
rw [hkeys] at hx
rcases List.mem_cons.mp hx with rfl | hx
· exact hnil_lt_B
· rcases List.mem_append.mp hx with hx | hx
· exact hres_lt_B x hx
· exact hJlt_B x hx
rw [hf, hkeys, List.length_cons, List.length_append, ht, hJlen']
omega
have hanchorB : r.begin.anchor db = 1 := by
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin)]
change ((cnt db r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 1
rw [hcntB, ← hq]
have : ((1 + t + T : ℕ) : ℤ) - (q : ℤ) - 1 = 1 := by rw [hT]; omega
exact this
have hlt1 : 1 < db.keys.length := by
rw [hkeys, List.length_cons, List.length_append]
have := List.length_pos_of_ne_nil hne
omega
have hz? : db.keys[1]? = some head := by
rw [hkeys, List.getElem?_cons_succ]
rw [List.getElem?_append_left (by rw [← ht]; exact List.length_pos_of_ne_nil hne)]
rw [List.getElem?_eq_getElem (h := List.length_pos_of_ne_nil hne)]
exact congrArg some (by rw [hhead]; exact (List.head_eq_getElem hne).symm)
have hz : db.keys[1]'(hlt1) = head := by
apply Option.some.inj
rw [← List.getElem?_eq_getElem (h := hlt1)]
exact hz?
have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hresB : resolve db r.begin = ((head : Key) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor db := by rw [hanchorB]; norm_num
have hlt : (r.begin.anchor db).toNat < db.keys.length := by
rw [hanchorB]; simpa using hlt1
have hval : db.keys[(r.begin.anchor db).toNat]'(hlt) = head := by
apply Option.some.inj
rw [← List.getElem?_eq_getElem (h := hlt)]
have hidx : (r.begin.anchor db).toNat = 1 := by rw [hanchorB]; rfl
rw [hidx]
exact hz?
rw [resolve_eq_getElem h0 hlt]
exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval
have hemptyM : ((db.keys.filter (fun k : Key => decide (r.end_.key ≤ (k : WithTop Key)))).filter
(fun k : Key => decide (k ≤ last))).length = 0 := by
rw [List.length_eq_zero_iff, List.eq_nil_iff_forall_not_mem]
intro x hx
rw [List.mem_filter, List.mem_filter] at hx
obtain ⟨⟨hxdb, hxE⟩, hxle⟩ := hx
rw [hkeys] at hxdb
rcases List.mem_cons.mp hxdb with rfl | hxdb
· have hnil_lt_last : (([] : Key) : WithTop Key) < ((last : Key) : WithTop Key) :=
lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne')))
(WithTop.coe_le_coe.mpr (hlast_le head (List.head_mem hne)))
have hx0 : (([] : Key) : WithTop Key) < r.end_.key := lt_trans hnil_lt_last hlastE'
exact absurd (of_decide_eq_true hxE) (not_le.mpr hx0)
· rcases List.mem_append.mp hxdb with hx | hx
· have hxB : ((x : Key) : WithTop Key) < r.end_.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
(lt_of_lt_of_le hlastE' le_rfl)
exact absurd (of_decide_eq_true hxE) (not_le.mpr hxB)
· exact absurd (of_decide_eq_true hxle) (not_le.mpr (hJgt_last x hx))
have hresE : ((last : Key) : WithTop Key) < resolve db r.end_ :=
resolve_end_gt_of_filter_length_le hendoff (by rw [hemptyM]; omega)
have hck : RangeRead.completeKeys db r =
db.keys.filter (fun k : Key => decide (head ≤ k) && decide
(((k : Key) : WithTop Key) < resolve db r.end_)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), hresB]
simp [WithTop.coe_le_coe]
have hfil : (RangeRead.completeKeys db r).filter (fun k : Key => decide (k ≤ last)) =
res.keys := by
rw [hck, hkeys, List.filter_filter, List.filter_cons, List.filter_append]
have hnil_pred : ¬ (decide ((([] : Key)) ≤ last) && (decide (head ≤ (([] : Key))) &&
decide ((([] : Key) : WithTop Key) < resolve db r.end_))) = true := by
intro hcon
rw [Bool.and_eq_true, Bool.and_eq_true] at hcon
exact (not_le.mpr (lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne')))
(of_decide_eq_true hcon.2.1)
rw [if_neg hnil_pred]
have h1 : res.keys.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) &&
decide (((k : Key) : WithTop Key) < resolve db r.end_))) = res.keys := by
rw [List.filter_eq_self]
intro k hk
rw [Bool.and_eq_true, Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq,
decide_eq_true_eq]
exact ⟨hlast_le k hk, hhead_le k hk,
lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le k hk)) hresE⟩
have h2 : J.filter (fun k : Key => decide (k ≤ last) && (decide (head ≤ k) &&
decide (((k : Key) : WithTop Key) < resolve db r.end_))) = [] := by
rw [List.filter_eq_nil_iff]
intro k hk hcon
rw [Bool.and_eq_true] at hcon
exact absurd (of_decide_eq_true hcon.1) (not_le.mpr (hJgt_last k hk))
rw [h1, h2, List.append_nil]
rw [compatible_iff_truncated_keys (db := db) hrev hmore hne]
exact ⟨hlim, hfil⟩
/-- **Compatibility transfers across agreement on the nonempty-head high-capacity gap
range `[⊥, begin.key)`.** Agreement on the stored keys below `begin.key` fixes the begin
resolution and every reported key, and the end resolution lies above the reported last. -/
theorem compatible_of_agree_negCross_trunc_clamp_gapHigh {r : RangeRead} {res : Result}
(hrev : r.reverse = false) (hmore : res.more = true) (hne : res.keys ≠ [])
(hbegin : r.begin.offset < 0) (hendoff : 1 ≤ r.end_.offset)
(hgap : (((keyAfter (res.keys.getLastD ([] : Key)) : Key)) : WithTop Key) <
r.begin.key)
(hlastE : (((res.keys.getLastD ([] : Key)) : Key)) < r.end_.key)
{db1 db2 : DB} (hc1 : RangeRead.Compatible db1 r res)
(hagree : AgreeOn (closedFormNegCrossTruncClampGapHigh r res) db1 db2) :
RangeRead.Compatible db2 r res := by
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by
rw [← hlastD]; exact hlastE
have hlastB : ((last : Key) : WithTop Key) < r.begin.key := by
rw [hlastD] at hgap
exact lt_of_lt_of_le (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) (le_of_lt hgap)
have hcr : closedFormNegCrossTruncClampGapHigh r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hmem := (agreeOn_iff (closedFormNegCrossTruncClampGapHigh r res) db1 db2).mp hagree
have hmk : ∀ k : Key, ((k : Key) : WithTop Key) < r.begin.key →
(k ∈ db1.keys ↔ k ∈ db2.keys) := by
intro k hk
refine hmem k ?_
rw [hcr]
exact ⟨WithTop.coe_le_coe.mpr (nil_le_key k), hk⟩
have hF : db1.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) =
db2.keys.filter (fun (k : Key) => decide (((k : Key) : WithTop Key) < r.begin.key)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, decide_eq_true_eq]
exact ⟨fun h => ⟨(hmk k h.2).mp h.1, h.2⟩, fun h => ⟨(hmk k h.2).mpr h.1, h.2⟩⟩
have hbeg : resolve db1 r.begin = resolve db2 r.begin := by
rw [resolve_eq_filter_lt_reverse (db := db1) (s := r.begin) (le_of_lt hbegin),
resolve_eq_filter_lt_reverse (db := db2) (s := r.begin) (le_of_lt hbegin), hF]
have hE1 : ((last : Key) : WithTop Key) < resolve db1 r.end_ :=
lt_resolve_end_of_lt_end_key hendoff hlastE'
have hE2 : ((last : Key) : WithTop Key) < resolve db2 r.end_ :=
lt_resolve_end_of_lt_end_key hendoff hlastE'
have hred1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) =
db1.keys.filter (fun k : Key => decide (resolve db1 r.begin ≤ (k : WithTop Key)) &&
decide (k ≤ last)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), List.filter_filter]
apply List.filter_congr
intro k _
by_cases hle : k ≤ last
· have hlt : ((k : Key) : WithTop Key) < resolve db1 r.end_ :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE1
simp [hle, hlt]
· simp [hle]
have hred2 : (RangeRead.completeKeys db2 r).filter (fun k : Key => decide (k ≤ last)) =
db2.keys.filter (fun k : Key => decide (resolve db2 r.begin ≤ (k : WithTop Key)) &&
decide (k ≤ last)) := by
unfold RangeRead.completeKeys RangeRead.inRange
rw [hrev, ite_eq_right (by simp), List.filter_filter]
apply List.filter_congr
intro k _
by_cases hle : k ≤ last
· have hlt : ((k : Key) : WithTop Key) < resolve db2 r.end_ :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hE2
simp [hle, hlt]
· simp [hle]
have heq : db1.keys.filter (fun k : Key => decide (resolve db1 r.begin ≤ (k : WithTop Key)) &&
decide (k ≤ last)) =
db2.keys.filter (fun k : Key => decide (resolve db2 r.begin ≤ (k : WithTop Key)) &&
decide (k ≤ last)) := by
apply eq_of_forall_mem_iff_of_pairwise
· exact db1.sorted.pairwise.filter _
· exact db2.sorted.pairwise.filter _
· intro k
simp only [List.mem_filter, Bool.and_eq_true, decide_eq_true_eq]
constructor
· rintro ⟨hdb, hrb, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨(hmk k hkB).mp hdb, by rw [← hbeg]; exact hrb, hle⟩
· rintro ⟨hdb, hrb, hle⟩
have hkB : ((k : Key) : WithTop Key) < r.begin.key :=
lt_of_le_of_lt (WithTop.coe_le_coe.mpr hle) hlastB
exact ⟨(hmk k hkB).mpr hdb, by rw [hbeg]; exact hrb, hle⟩
have hres1 : (RangeRead.completeKeys db1 r).filter (fun k : Key => decide (k ≤ last)) =
res.keys := by
have h := ((compatible_iff_truncated_keys (db := db1) hrev hmore hne).mp hc1).2
rwa [← hlast] at h
rw [compatible_iff_truncated_keys (db := db2) hrev hmore hne]
refine ⟨by simpa using hc1.1, ?_⟩
rw [hred2, ← heq, ← hred1, hres1]
/-- **Gap keys are necessary in the nonempty-head high-capacity gap cell.** A key between
the reported last and `begin.key` is necessary: a witness database with the empty key, the
reported keys and `T` gap keys including the target resolves the begin to the reported
head, while erasing the target lowers the begin anchor so the empty key enters the
truncated complete keys. -/
theorem necessary_gap_high {r : RangeRead} {res : Result} {k : Key}
(hread : Readable r res) (hcell : negCrossTruncClampGapHigh r res)
(hkgt : res.keys.getLastD ([] : Key) < k)
(hkB : ((k : Key) : WithTop Key) < r.begin.key) :
Necessary r res k := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadne, hlastE,
hcap⟩ := hcell
obtain ⟨db₀, hcomp₀⟩ := hread
intro cr hs
by_contra hkcr
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
have hlim : r.withinLimit res.keys := hcomp₀.1
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
set t : ℕ := res.keys.length with ht
set q : ℕ := beginSlack r with hq
set T : ℕ := q + 1 - t with hT
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hheadD : res.keys.headD ([] : Key) = head := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne
have hlastE' : ((last : Key) : WithTop Key) < r.end_.key := by
rw [hlastD] at hlastE; exact hlastE
have hgap' : (((keyAfter last : Key)) : WithTop Key) < r.begin.key := by
rw [← hlastD]; exact hgap
have hlastB : ((last : Key) : WithTop Key) < r.begin.key :=
lt_trans (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) hgap'
have hkgt' : last < k := by rw [← hlastD]; exact hkgt
have hlast_le : ∀ x ∈ res.keys, x ≤ last := by
intro x hx
rw [hlast]
exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
have hreported : reportedBelowBegin r res = t := by
unfold reportedBelowBegin
rw [List.countP_eq_length]
intro x hx
rw [decide_eq_true_eq]
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB
have htq : t ≤ q := by rw [← hreported, hq]; exact hclamp
have hTpos : 1 ≤ q + 1 - t := by omega
have hres_lt_B : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlastB
have hnil_lt_head : ([] : Key) < head :=
lt_of_le_of_ne (nil_le_key head) (Ne.symm hheadne')
have hhead_le : ∀ x ∈ res.keys, head ≤ x := by
intro x hx
rw [hhead]
exact (hsort.pairwise.imp le_of_lt).rel_head hx
have hnil_lt_mem : ∀ x ∈ res.keys, ([] : Key) < x := by
intro x hx
exact lt_of_lt_of_le hnil_lt_head (hhead_le x hx)
have hnil_lt_B : (([] : Key) : WithTop Key) < r.begin.key :=
lt_trans (WithTop.coe_lt_coe.mpr hnil_lt_head)
(hres_lt_B head (List.head_mem hne))
have hnil_lt_lastK : ([] : Key) < last :=
lt_of_lt_of_le hnil_lt_head (hlast_le head (List.head_mem hne))
have hnil_lt_last : (([] : Key) : WithTop Key) < ((last : Key) : WithTop Key) :=
WithTop.coe_lt_coe.mpr hnil_lt_lastK
-- capacity: `T ≤ intervalCard (keyAfter last) begin.key`
have hcard_one : intervalCard ((last : Key) : WithTop Key)
(((keyAfter last : Key)) : WithTop Key) = 1 := by
have := intervalCard_succChain last 1
simpa [keyAfter, List.replicate_one] using this
have hcard_add : intervalCard ((last : Key) : WithTop Key) r.begin.key =
intervalCard ((last : Key) : WithTop Key) (((keyAfter last : Key)) : WithTop Key) +
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key :=
intervalCard_add (b := ((last : Key) : WithTop Key))
(m := (((keyAfter last : Key)) : WithTop Key)) (e := r.begin.key)
(le_of_lt (WithTop.coe_lt_coe.mpr (keyAfter_self_lt last))) (le_of_lt hgap')
have hD : ((T : ℕ) : ℕ∞) ≤ intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
rw [hlastD] at hcap
have hC : intervalCard ((last : Key) : WithTop Key) r.begin.key =
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key + 1 := by
rw [hcard_add, hcard_one, add_comm]
rw [hC] at hcap
by_cases htop : intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key = ⊤
· rw [htop]; exact le_top
· exact (ENat.lt_add_one_iff
(n := intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key) htop).mp hcap
-- choose `T - 1` gap keys avoiding the target
have hcard' : (((T - 1) + 1 : ℕ) : ℕ∞) ≤
intervalCard (((keyAfter last : Key)) : WithTop Key) r.begin.key := by
rw [Nat.sub_add_cancel hTpos]; exact hD
obtain ⟨dbJ, hJmem, hJavoid, hJlen⟩ := exists_db_keysIn_avoid
(B := (((keyAfter last : Key)) : WithTop Key)) (E := r.begin.key) (k := k)
(m := T - 1) hcard'
have hJ'range : ∀ x ∈ dbJ.keys,
(((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧
((x : Key) : WithTop Key) < r.begin.key :=
fun x hx => hJmem x hx
have hJ'gt_last : ∀ x ∈ dbJ.keys, last < x := by
intro x hx
have h1 := (hJ'range x hx).1
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1)
have hres_lt_J' : ∀ x ∈ res.keys, ∀ y ∈ dbJ.keys, x < y := by
intro x hx y hy
exact lt_of_le_of_lt (hlast_le x hx) (hJ'gt_last y hy)
-- the target is above the last and in the gap
have hk_le_keyAfter : keyAfter last ≤ k := by
by_contra hcon
rw [not_le] at hcon
exact absurd ((lt_keyAfter_iff k last).mp hcon) (not_le.mpr hkgt')
have hk_not_res : k ∉ res.keys := by
intro hk
exact absurd (hlast_le k hk) (not_le.mpr hkgt')
have hk_ne_nil : k ≠ ([] : Key) := by
intro h
have hnil_lt_k : ([] : Key) < k :=
lt_of_le_of_lt (nil_le_key last) hkgt'
rw [h] at hnil_lt_k
exact (lt_irrefl ([] : Key)) hnil_lt_k
have hk_not_dbJ : k ∉ dbJ.keys := hJavoid
let J : List Key := dbJ.keys.orderedInsert (· ≤ ·) k
have hJlen' : J.length = T := by
unfold J; rw [List.orderedInsert_length, hJlen]; omega
have hJnodup : J.Nodup := by
unfold J
exact (List.perm_orderedInsert (· ≤ ·) k dbJ.keys).nodup_iff.mpr
(List.nodup_cons.mpr ⟨hk_not_dbJ, dbJ.nodup⟩)
have hJle : J.Pairwise (· ≤ ·) := by
unfold J
exact (dbJ.sorted.pairwise.imp le_of_lt).orderedInsert k
have hJsorted : J.SortedLT :=
List.sortedLT_iff_pairwise.mpr (DB.pairwise_lt_of_le_of_nodup hJle hJnodup)
have hJrange : ∀ x ∈ J,
(((keyAfter last : Key)) : WithTop Key) ≤ ((x : Key) : WithTop Key) ∧
((x : Key) : WithTop Key) < r.begin.key := by
intro x hx
unfold J at hx
rw [List.mem_orderedInsert] at hx
rcases hx with rfl | hx
· exact ⟨WithTop.coe_le_coe.mpr hk_le_keyAfter, hkB⟩
· exact hJ'range x hx
have hJgt_last : ∀ x ∈ J, last < x := by
intro x hx
have h1 := (hJrange x hx).1
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le
(WithTop.coe_lt_coe.mpr (keyAfter_self_lt last)) h1)
have hres_lt_J : ∀ x ∈ res.keys, ∀ y ∈ J, x < y := by
intro x hx y hy
exact lt_of_le_of_lt (hlast_le x hx) (hJgt_last y hy)
-- sorted database with the empty key, the reported keys and `T - 1` gap keys
have hsorted0 : (([] : Key) :: (res.keys ++ dbJ.keys)).SortedLT := by
rw [List.sortedLT_cons]
refine ⟨?_, ?_⟩
· intro x hx
rcases List.mem_append.mp hx with hx | hx
· exact hnil_lt_mem x hx
· exact lt_trans hnil_lt_lastK (hJ'gt_last x hx)
· exact List.sortedLT_append.mpr ⟨hsort, dbJ.sorted, hres_lt_J'⟩
let db0 : DB := ⟨([] : Key) :: (res.keys ++ dbJ.keys), hsorted0⟩
-- sorted database with the empty key, the reported keys and `T` gap keys
have hsorted1 : (([] : Key) :: (res.keys ++ J)).SortedLT := by
rw [List.sortedLT_cons]
refine ⟨?_, ?_⟩
· intro x hx
rcases List.mem_append.mp hx with hx | hx
· exact hnil_lt_mem x hx
· exact lt_trans hnil_lt_lastK (hJgt_last x hx)
· exact List.sortedLT_append.mpr ⟨hsort, hJsorted, hres_lt_J⟩
let db1 : DB := ⟨([] : Key) :: (res.keys ++ J), hsorted1⟩
have hcomp1 : RangeRead.Compatible db1 r res :=
compatible_of_nil_cons_gapKeys_high hrev hmore hne hbegin hendoff hlim htq hheadne
hlastE hgap hsort hJsorted
(by intro x hx; rw [hlastD]; exact hJrange x hx)
(by rw [hJlen']) rfl
-- the database without the target is incompatible: the empty key enters the window
have hnotcomp0 : ¬ RangeRead.Compatible db0 r res := by
intro hc0
have hfil0 := ((compatible_iff_truncated_keys (db := db0) hrev hmore hne).mp hc0).2
rw [← hlast] at hfil0
have hcnt0 : cnt db0 r.begin.key = 1 + t + (T - 1) := by
rw [cnt, List.countP_eq_length_filter]
have hf : db0.keys.filter (fun x : Key => decide (((x : Key) : WithTop Key) <
r.begin.key)) = db0.keys := by
rw [List.filter_eq_self]
intro x hx
rw [decide_eq_true_eq]
rw [show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl] at hx
rcases List.mem_cons.mp hx with rfl | hx
· exact hnil_lt_B
· rcases List.mem_append.mp hx with hx | hx
· exact hres_lt_B x hx
· exact (hJ'range x hx).2
rw [hf, show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl]
rw [List.length_cons, List.length_append, ht, hJlen]
omega
have hanchor0 : r.begin.anchor db0 = 0 := by
rw [anchor_eq_count_sub_slack db0 r (le_of_lt hbegin)]
change ((cnt db0 r.begin.key : ℕ) : ℤ) - (beginSlack r : ℤ) - 1 = 0
rw [hcnt0, ← hq]
have : ((1 + t + (T - 1) : ℕ) : ℤ) - (q : ℤ) - 1 = 0 := by rw [hT]; omega
exact this
have hbot_mem : ([] : Key) ∈ db0.keys := by
rw [show db0.keys = ([] : Key) :: (res.keys ++ dbJ.keys) from rfl]
exact List.mem_cons_self ..
have hresB0 : resolve db0 r.begin = ((↑([] : Key)) : WithTop Key) := by
have h0 : 0 ≤ r.begin.anchor db0 := by rw [hanchor0]
have hlt : (r.begin.anchor db0).toNat < db0.keys.length := by
rw [hanchor0]; exact List.length_pos_of_mem hbot_mem
have hval : db0.keys[(r.begin.anchor db0).toNat]'(hlt) = ([] : Key) := by
apply Option.some.inj
rw [← List.getElem?_eq_getElem (h := hlt)]
have hidx : (r.begin.anchor db0).toNat = 0 := by rw [hanchor0]; rfl
rw [hidx, List.getElem?_eq_getElem (h := List.length_pos_of_mem hbot_mem)]
exact congrArg some (keys_getElem_zero_eq_nil_of_mem hbot_mem)
rw [resolve_eq_getElem h0 hlt]
exact congrArg (fun z : Key => ((z : Key) : WithTop Key)) hval
have hresE0 : ((last : Key) : WithTop Key) < resolve db0 r.end_ :=
lt_resolve_end_of_lt_end_key (db := db0) hendoff hlastE'
have hnil_ck : ([] : Key) ∈ RangeRead.completeKeys db0 r := by
rw [RangeRead.mem_completeKeys]
refine ⟨hbot_mem, ?_, ?_⟩
· rw [hresB0]
· exact lt_trans hnil_lt_last hresE0
have hnil_fil : ([] : Key) ∈ (RangeRead.completeKeys db0 r).filter
(fun x : Key => decide (x ≤ last)) := by
rw [List.mem_filter]
exact ⟨hnil_ck, by rw [decide_eq_true_eq]; exact nil_le_key last⟩
rw [hfil0] at hnil_fil
have hnil_not_mem : ([] : Key) ∉ res.keys := by
intro hmem
exact (not_le.mpr hnil_lt_head) (hhead_le [] hmem)
exact hnil_not_mem hnil_fil
-- the two databases differ only at the target
have hagree : AgreeOn cr db1 db0 := by
unfold AgreeOn ConflictRange.keys
apply List.SortedLT.eq_of_mem_iff
· exact List.sortedLT_iff_pairwise.mpr (db1.sorted.pairwise.filter _)
· exact List.sortedLT_iff_pairwise.mpr (db0.sorted.pairwise.filter _)
· intro x
simp only [List.mem_filter, decide_eq_true_eq]
have hdbiff : x ∈ db1.keys ↔ x = k ∨ x ∈ db0.keys := by
show x ∈ ([] : Key) :: (res.keys ++ J) ↔
x = k ∨ x ∈ ([] : Key) :: (res.keys ++ dbJ.keys)
unfold J
simp only [List.mem_cons, List.mem_append, List.mem_orderedInsert]
tauto
constructor
· rintro ⟨hdb, hcr⟩
rcases (hdbiff.mp hdb) with rfl | hdb0
· exact absurd hcr hkcr
· exact ⟨hdb0, hcr⟩
· rintro ⟨hdb, hcr⟩
exact ⟨hdbiff.mpr (Or.inr hdb), hcr⟩
exact hnotcomp0 ((hs db1 db0 hagree).mp hcomp1)
/-- **Minimality of the nonempty-head high-capacity gap closed form.** The minimal range
is `[⊥, begin.key)`: soundness transfers across agreement on that range, the reported span
is necessary by convexity, and every key below the reported head or in the gap
`[last, begin.key)` is necessary. -/
theorem minimal_closedForm_of_negCross_trunc_clamp_gapHigh {r : RangeRead} {res : Result}
(hread : Readable r res) (hcell : negCrossTruncClampGapHigh r res) :
Minimal r res (closedFormNegCrossTruncClampGapHigh r res) := by
obtain ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap, hclamp, hheadne, hlastE,
hcap⟩ := hcell
have hcr : closedFormNegCrossTruncClampGapHigh r res =
(⟨((↑([] : Key) : WithTop Key)), r.begin.key⟩ : ConflictRange) := rfl
have hsound : Sound r res (closedFormNegCrossTruncClampGapHigh r res) := by
intro db1 db2 hagree
exact ⟨fun h => compatible_of_agree_negCross_trunc_clamp_gapHigh hrev hmore hne
hbegin hendoff hgap hlastE h hagree,
fun h => compatible_of_agree_negCross_trunc_clamp_gapHigh hrev hmore hne
hbegin hendoff hgap hlastE h hagree.symm⟩
rw [hcr] at hsound ⊢
refine (minimal_iff_sound_and_necessaryRange).mpr ⟨hsound, ?_⟩
intro k _ hkend
set head : Key := res.keys.head hne with hhead
set last : Key := res.keys.getLast hne with hlast
have hlastD : res.keys.getLastD ([] : Key) = last := by
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
have hheadD : res.keys.headD ([] : Key) = head := by
rw [List.headD_eq_head?, List.head?_eq_some_head hne]; rfl
have hheadne' : head ≠ ([] : Key) := by rw [← hheadD]; exact hheadne
have hhi : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by
intro x hx
have hsort : res.keys.SortedLT :=
RangeRead.compatible_sortedLT hread.choose_spec hrev
have hlastGet : res.keys.getLastD ([] : Key) = res.keys.getLast hne :=
hlastD.trans hlast
exact lt_of_le_of_lt
(WithTop.coe_le_coe.mpr ((hsort.pairwise.imp le_of_lt).rel_getLast hx))
(by rw [← hlastGet]; exact hlastE)
have hcross : ((res.keys.head hne : Key) : WithTop Key) < r.begin.key := by
have hcd := hcrossD
simp only [leastReported, hrev] at hcd
rw [hheadD] at hcd
rw [hhead] at hcd
exact hcd
by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)
· exact necessary_below_head_of_negCross_trunc_clamp hread hrev hmore hne hbegin
hendoff hcross hclamp hkhead
· have hkhead' : ((head : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key) :=
le_of_not_gt hkhead
by_cases hkle : k ≤ res.keys.getLastD ([] : Key)
· have hkle' : k ≤ last := by rw [hlastD] at hkle; exact hkle
exact necessary_of_between_result hread (a := head) (b := last)
(by rw [hhead]; exact List.head_mem hne)
(by rw [hlast]; exact List.getLast_mem hne)
(WithTop.coe_le_coe.mp hkhead') hkle'
· have hkgt : res.keys.getLastD ([] : Key) < k := lt_of_not_ge hkle
exact necessary_gap_high hread ⟨hmore, hrev, hbegin, hne, hendoff, hcrossD, hgap,
hclamp, hheadne, hlastE, hcap⟩ hkgt hkend
end FormalConflictRange