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
This commit is contained in:
1 parent
7a0826e873
commit
a54a5f82f8
78 files changed
+91636
-2
No files matched your search
@@ -0,0 +1,609 @@
|
||||
/-
|
||||
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.NegCrossClampNonpos
|
||||
import FormalConflictRange.ZeroCrossTrunc
|
||||
|
||||
/-!
|
||||
# Crossed negative-begin truncated clamping nonpositive-end cell
|
||||
|
||||
This module closes the forward, crossed, truncated, clamping cell at a negative begin
|
||||
offset with a nonpositive end offset, in the subcase where the reported last reaches
|
||||
`begin.key`. The minimal sound range is `[⊥, end_.key)`.
|
||||
|
||||
The two ingredients are the complete-result clamp certificate of
|
||||
`NegCrossClampNonpos.lean`, generalized here from complete to truncated results, and a
|
||||
tail witness: storing the reported keys together with `1 - end.offset` fillers above the
|
||||
reported last makes the end selector resolve onto the first filler, so erasing any filler
|
||||
drops the end resolution to the reported last and breaks the truncated read-off.
|
||||
-/
|
||||
|
||||
open FormalConflictRange
|
||||
|
||||
namespace FormalConflictRange
|
||||
|
||||
/-- A forward truncated, crossed, negative-begin read with a nonpositive end offset that
|
||||
clamps and whose reported last reaches `begin.key`. -/
|
||||
def negCrossTruncClampLastGe (r : RangeRead) (res : Result) : Prop :=
|
||||
res.more = true ∧ r.reverse = false ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
|
||||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||||
r.end_.offset ≤ 0 ∧ reportedBelowBegin r res ≤ beginSlack r ∧
|
||||
r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key)
|
||||
|
||||
/-- In the clamped cell, a compatible database exists with a negative begin anchor. This
|
||||
is the truncated analogue of `exists_clamp_compat_nonpos_lastGe`; the constructed database
|
||||
has `completeKeys` exactly equal to the reported keys, hence is compatible with either
|
||||
`more` flag. -/
|
||||
theorem exists_clamp_compat_nonpos_lastGe_prefix {r : RangeRead} {res : Result}
|
||||
(hread : Readable r res) (hrev : r.reverse = false)
|
||||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||||
(hend : r.end_.offset ≤ 0)
|
||||
(hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key))
|
||||
(hclamp : reportedBelowBegin r res ≤ beginSlack r) :
|
||||
∃ db : DB, RangeRead.Compatible db r res ∧ r.begin.anchor db < 0 := by
|
||||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||||
have hlim : RangeRead.withinLimit r res.keys := hcomp₀.1
|
||||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||||
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||||
rw [List.getLastD_eq_getLast?]
|
||||
simp [List.getLast?_eq_some_getLast hne]
|
||||
have hlastB : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by
|
||||
rw [← hlastD]; exact hB_le_last
|
||||
have hlast_ck₀ : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r :=
|
||||
(RangeRead.compatible_keys_prefix hcomp₀).subset (List.getLast_mem hne)
|
||||
have hlast_lt_R₀ : ((res.keys.getLast hne : Key) : WithTop Key) < resolve db₀ r.end_ :=
|
||||
(RangeRead.mem_completeKeys.mp hlast_ck₀).2.2
|
||||
have hR_ne_nil : resolve db₀ r.end_ ≠ (([] : Key) : WithTop Key) := by
|
||||
intro hbot
|
||||
rw [hbot] at hlast_lt_R₀
|
||||
exact absurd hlast_lt_R₀ (not_lt.mpr (coe_nil_le_key_withTop _))
|
||||
obtain ⟨r₀, hr₀⟩ : ∃ r₀ : Key, resolve db₀ r.end_ = ((r₀ : Key) : WithTop Key) := by
|
||||
rcases resolve_eq_nil_or_eq_top_or_mem (db := db₀) (s := r.end_) with hn | ht | ⟨q, _, hqeq⟩
|
||||
· exact absurd hn hR_ne_nil
|
||||
· exact absurd ht (resolve_ne_top_of_offset_nonpos hend)
|
||||
· exact ⟨q, hqeq⟩
|
||||
have hlast_lt_r₀ : ((res.keys.getLast hne : Key) : WithTop Key) < ((r₀ : Key) : WithTop Key) := by
|
||||
rw [← hr₀]; exact hlast_lt_R₀
|
||||
have hr₀_lt_E : ((r₀ : Key) : WithTop Key) < r.end_.key := by
|
||||
rw [← hr₀]
|
||||
exact resolve_lt_key_of_offset_nonpos_of_ne_nil hend hR_ne_nil
|
||||
set L : List Key := between db₀ (resolve db₀ r.end_) r.end_.key with hL
|
||||
have hLlen : L.length = clampNonposD r + 1 := by
|
||||
rw [hL]; exact between_resolve_end_length hend hR_ne_nil
|
||||
have hLsorted : L.SortedLT := by
|
||||
rw [hL]
|
||||
exact List.sortedLT_iff_pairwise.mpr (db₀.sorted.pairwise.filter _)
|
||||
have hLmem : ∀ x ∈ L, resolve db₀ r.end_ ≤ (x : WithTop Key) ∧
|
||||
(x : WithTop Key) < r.end_.key := by
|
||||
intro x hx
|
||||
rw [hL] at hx
|
||||
exact of_decide_eq_true (List.mem_filter.mp hx).2
|
||||
have hlast_le : ∀ x ∈ res.keys, x ≤ res.keys.getLast hne :=
|
||||
fun x hx => (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||||
have hresL : ∀ x ∈ res.keys, ∀ y ∈ L, x < y := by
|
||||
intro x hx y hy
|
||||
exact WithTop.coe_lt_coe.mp
|
||||
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx))
|
||||
(lt_of_lt_of_le hlast_lt_r₀ (le_trans (le_of_eq hr₀.symm) (hLmem y hy).1)))
|
||||
have hAppSorted : (res.keys ++ L).SortedLT :=
|
||||
List.sortedLT_append.mpr ⟨hsort, hLsorted, hresL⟩
|
||||
set db : DB := ⟨res.keys ++ L, hAppSorted⟩ with hdb
|
||||
have hL_ge_r₀ : ∀ x ∈ L, ((r₀ : Key) : WithTop Key) ≤ (x : WithTop Key) := by
|
||||
intro x hx
|
||||
rw [← hr₀]
|
||||
exact (hLmem x hx).1
|
||||
have hL_lt_E : ∀ x ∈ L, (x : WithTop Key) < r.end_.key := fun x hx => (hLmem x hx).2
|
||||
have hres_lt_r₀ : ∀ x ∈ res.keys, x < r₀ := by
|
||||
intro x hx
|
||||
exact WithTop.coe_lt_coe.mp
|
||||
(lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hlast_le x hx)) hlast_lt_r₀)
|
||||
have hfilt : db.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||||
rw [hdb]
|
||||
rw [List.filter_append]
|
||||
have h1 : res.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = [] := by
|
||||
rw [List.filter_eq_nil_iff]
|
||||
intro x hx
|
||||
simp only [decide_eq_true_eq]
|
||||
intro hcon
|
||||
exact absurd hcon.1 (not_le.mpr (WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx)))
|
||||
have h2 : L.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||||
rw [List.filter_eq_self]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
exact ⟨hL_ge_r₀ x hx, hL_lt_E x hx⟩
|
||||
rw [h1, h2]
|
||||
simp
|
||||
have hfilt₀ : db₀.keys.filter (fun k : Key => decide (((r₀ : Key) : WithTop Key) ≤
|
||||
(k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)) = L := by
|
||||
rw [hL, ← hr₀]
|
||||
rfl
|
||||
have hqspec : (db₀.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).reverse[
|
||||
(-r.end_.offset).toNat]? = some r₀ :=
|
||||
resolve_filter_reverse_eq_some_of_ne_nil hend (by simpa [hr₀] using hR_ne_nil) hr₀
|
||||
have hR : resolve db r.end_ = ((r₀ : Key) : WithTop Key) := by
|
||||
refine resolve_eq_of_agree_between (db₁ := db₀) (db₂ := db) (s := r.end_) hend rfl
|
||||
(le_of_lt hr₀_lt_E) ?_ hqspec (le_refl _)
|
||||
rw [hfilt₀, hfilt]
|
||||
have hcnt : db.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||||
reportedBelowBegin r res := by
|
||||
rw [hdb]
|
||||
rw [List.countP_append]
|
||||
have h1 : res.keys.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) =
|
||||
reportedBelowBegin r res := rfl
|
||||
have h2 : L.countP (fun k : Key => decide ((k : WithTop Key) < r.begin.key)) = 0 := by
|
||||
rw [List.countP_eq_zero]
|
||||
intro x hx
|
||||
simp only [decide_eq_true_eq]
|
||||
exact not_lt.mpr
|
||||
(le_trans hlastB (le_trans (le_of_lt hlast_lt_r₀)
|
||||
(le_trans (le_of_eq hr₀.symm) (hLmem x hx).1)))
|
||||
rw [h1, h2, Nat.add_zero]
|
||||
have hanchor : r.begin.anchor db < 0 := by
|
||||
rw [anchor_eq_count_sub_slack db r (le_of_lt hbegin), hcnt]
|
||||
have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
|
||||
omega
|
||||
have hL_db : resolve db r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor
|
||||
have hfilt_b : db.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||||
((r₀ : Key) : WithTop Key))) = res.keys := by
|
||||
rw [hdb]
|
||||
rw [List.filter_append]
|
||||
have h1 : res.keys.filter (fun k : Key => decide ((k : WithTop Key) <
|
||||
((r₀ : Key) : WithTop Key))) = res.keys := by
|
||||
rw [List.filter_eq_self]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
exact WithTop.coe_lt_coe.mpr (hres_lt_r₀ x hx)
|
||||
have h2 : L.filter (fun k : Key => decide ((k : WithTop Key) <
|
||||
((r₀ : Key) : WithTop Key))) = [] := by
|
||||
rw [List.filter_eq_nil_iff]
|
||||
intro x hx
|
||||
simp only [decide_eq_true_eq]
|
||||
rw [← hr₀]
|
||||
exact not_lt.mpr (hLmem x hx).1
|
||||
rw [h1, h2]
|
||||
simp
|
||||
have hck : RangeRead.completeKeys db r = res.keys := by
|
||||
unfold RangeRead.completeKeys RangeRead.inRange
|
||||
rw [ite_eq_right (by simp [hrev]), hL_db, hR]
|
||||
rw [filter_between_bot_eq]
|
||||
exact hfilt_b
|
||||
refine ⟨db, ⟨hlim, ?_⟩, hanchor⟩
|
||||
rcases hcomp₀.2 with ⟨hmore, -, -⟩ | ⟨hmore, -⟩
|
||||
· exact Or.inl ⟨hmore, by simpa [hck] using (List.prefix_refl res.keys), hne⟩
|
||||
· exact Or.inr ⟨hmore, hck.symm⟩
|
||||
|
||||
/-- **Soundness of `[⊥, end_.key)` for truncated results.** Agreement on every key below
|
||||
`end_.key` fixes both resolutions and the complete key list, so compatibility transfers
|
||||
for either `more` flag. -/
|
||||
theorem sound_bottomEnd_of_clamp_nonpos_prefix {r : RangeRead} {res : Result}
|
||||
(hrev : r.reverse = false) (hbegin : r.begin.offset < 0)
|
||||
(hend : r.end_.offset ≤ 0) (hB_le_E : r.begin.key ≤ r.end_.key) :
|
||||
Sound r res (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange) := by
|
||||
have step : ∀ db₁ db₂, AgreeOn (⟨(([] : Key) : WithTop Key), r.end_.key⟩ : ConflictRange)
|
||||
db₁ db₂ → RangeRead.Compatible db₁ r res → RangeRead.Compatible db₂ r res := by
|
||||
intro db₁ db₂ hagree hcomp₁
|
||||
have hlim : RangeRead.withinLimit r res.keys := hcomp₁.1
|
||||
have hpre : res.keys <+: RangeRead.completeKeys db₁ r :=
|
||||
RangeRead.compatible_keys_prefix hcomp₁
|
||||
have hfiltE : db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) =
|
||||
db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key)) := by
|
||||
have h := hagree
|
||||
rw [AgreeOn, keys_bottomRange_eq_filter, keys_bottomRange_eq_filter] at h
|
||||
exact h
|
||||
have hL : resolve db₁ r.begin = resolve db₂ r.begin :=
|
||||
resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.begin)
|
||||
(le_of_lt hbegin) hB_le_E hfiltE
|
||||
have hR : resolve db₁ r.end_ = resolve db₂ r.end_ :=
|
||||
resolve_eq_of_keys_below (db₁ := db₁) (db₂ := db₂) (E := r.end_.key) (s := r.end_)
|
||||
hend le_rfl hfiltE
|
||||
have hres_le : resolve db₁ r.end_ ≤ r.end_.key := resolve_le_key_of_offset_nonpos hend
|
||||
have hP : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_)) =
|
||||
db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_)) := by
|
||||
have h₁ : db₁.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_)) =
|
||||
(db₁.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter
|
||||
(fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_)) := by
|
||||
rw [List.filter_filter]
|
||||
apply List.filter_congr
|
||||
intro k _
|
||||
by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_
|
||||
· have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le
|
||||
simp [hk, hkE]
|
||||
· simp [hk]
|
||||
have h₂ : db₂.keys.filter (fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_)) =
|
||||
(db₂.keys.filter (fun k : Key => decide ((k : WithTop Key) < r.end_.key))).filter
|
||||
(fun k : Key => decide (resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_)) := by
|
||||
rw [List.filter_filter]
|
||||
apply List.filter_congr
|
||||
intro k _
|
||||
by_cases hk : resolve db₁ r.begin ≤ (k : WithTop Key) ∧
|
||||
(k : WithTop Key) < resolve db₁ r.end_
|
||||
· have hkE : (k : WithTop Key) < r.end_.key := lt_of_lt_of_le hk.2 hres_le
|
||||
simp [hk, hkE]
|
||||
· simp [hk]
|
||||
rw [h₁, h₂, hfiltE]
|
||||
have hck : RangeRead.completeKeys db₁ r = RangeRead.completeKeys db₂ r := by
|
||||
unfold RangeRead.completeKeys RangeRead.inRange
|
||||
rw [ite_eq_right (by simp [hrev]), ite_eq_right (by simp [hrev]), ← hL, ← hR]
|
||||
exact hP
|
||||
rcases hcomp₁.2 with ⟨hmore, -, hne⟩ | ⟨hmore, heq⟩
|
||||
· exact ⟨hlim, Or.inl ⟨hmore, by rw [← hck]; exact hpre, hne⟩⟩
|
||||
· exact ⟨hlim, Or.inr ⟨hmore, by rw [← hck]; exact heq⟩⟩
|
||||
intro db₁ db₂ hagree
|
||||
exact ⟨step db₁ db₂ hagree, step db₂ db₁ hagree.symm⟩
|
||||
|
||||
/-- Above the reported last, a stored filler is necessary in the clamped cell: take the
|
||||
reported keys together with `1 - end.offset` fillers, the first of which is the target.
|
||||
Erasing the target drops the end resolution onto the reported last, so the truncated
|
||||
read-off loses its last key. -/
|
||||
theorem exists_witness_negCross_trunc_clamp_above_last {r : RangeRead} {res : Result} {k : Key}
|
||||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
|
||||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||||
(hend : r.end_.offset ≤ 0)
|
||||
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
|
||||
(hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key))
|
||||
(hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key) :
|
||||
∃ dbw : DB, RangeRead.Compatible dbw r res ∧ k ∈ dbw.keys ∧
|
||||
¬ RangeRead.Compatible (dbw.erase k) r res := by
|
||||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||||
set head : Key := res.keys.head hne with hhead
|
||||
set last : Key := res.keys.getLast hne with hlast
|
||||
have hsort : res.keys.SortedLT := RangeRead.compatible_sortedLT hcomp₀ hrev
|
||||
have hle_last : ∀ x ∈ res.keys, x ≤ last := fun x hx =>
|
||||
(hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||||
have hhead_le_last : head ≤ last := by
|
||||
simp only [hhead, hlast]
|
||||
exact (hsort.pairwise.imp le_of_lt).rel_getLast (List.head_mem hne)
|
||||
have hlastD : res.keys.getLastD ([] : Key) = last := by
|
||||
rw [hlast, List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||||
have hlastB : r.begin.key ≤ ((last : Key) : WithTop Key) := by
|
||||
rw [← hlastD]; exact hB_le_last
|
||||
have hlast_lt_k : ((last : Key) : WithTop Key) < ((k : Key) : WithTop Key) :=
|
||||
WithTop.coe_lt_coe.mpr hkgt
|
||||
have hlast_ck : last ∈ RangeRead.completeKeys db₀ r :=
|
||||
(RangeRead.compatible_keys_prefix hcomp₀).subset (List.getLast_mem hne)
|
||||
obtain ⟨hlast_mem, -, hlast_res⟩ := RangeRead.mem_completeKeys.mp hlast_ck
|
||||
have hlastE : ((last : Key) : WithTop Key) < r.end_.key :=
|
||||
lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res
|
||||
have hfill := filler_count_nonpos_more_nonpos_end hcomp₀ hrev hmore hne
|
||||
(le_of_lt hbegin) hend
|
||||
set m := (1 - r.end_.offset).toNat with hm
|
||||
have hm1 : 1 ≤ m := by rw [hm]; omega
|
||||
set G : List Key := db₀.keys.filter
|
||||
(fun x : Key => decide (last < x ∧ (x : WithTop Key) < r.end_.key)) with hG
|
||||
have hGlen : m ≤ G.length := hfill
|
||||
have hGnodup : G.Nodup := by
|
||||
rw [hG]; exact db₀.sorted.pairwise.nodup.filter _
|
||||
have hGmem : ∀ x ∈ G, last < x ∧ (x : WithTop Key) < r.end_.key := by
|
||||
intro x hx
|
||||
rw [hG] at hx
|
||||
exact of_decide_eq_true (List.mem_filter.mp hx).2
|
||||
set raw : List Key := (k :: G.erase k).take m with hraw
|
||||
have hrawNodup : raw.Nodup := by
|
||||
rw [hraw]
|
||||
refine (List.take_sublist m (k :: G.erase k)).nodup ?_
|
||||
exact List.nodup_cons.mpr ⟨hGnodup.not_mem_erase, hGnodup.erase k⟩
|
||||
have hrawLen : raw.length = m := by
|
||||
rw [hraw, List.length_take]
|
||||
apply Nat.min_eq_left
|
||||
by_cases hkG : k ∈ G
|
||||
· rw [List.length_cons, List.length_erase_of_mem hkG]; omega
|
||||
· rw [List.length_cons, List.erase_of_not_mem hkG]; omega
|
||||
have hrawSub : ∀ x ∈ raw, x = k ∨ x ∈ G := by
|
||||
intro x hx
|
||||
rw [hraw] at hx
|
||||
rcases List.mem_cons.mp (List.mem_of_mem_take hx) with rfl | hx'
|
||||
· exact Or.inl rfl
|
||||
· exact Or.inr (List.mem_of_mem_erase hx')
|
||||
have hrawk : k ∈ raw := by
|
||||
rw [hraw, List.mem_take_iff_getElem]
|
||||
refine ⟨0, ?_, rfl⟩
|
||||
rw [List.length_cons]
|
||||
omega
|
||||
have hrawGt : ∀ x ∈ raw, last < x := by
|
||||
intro x hx
|
||||
rcases hrawSub x hx with rfl | hx'
|
||||
· exact hkgt
|
||||
· exact (hGmem x hx').1
|
||||
have hrawLt : ∀ x ∈ raw, (x : WithTop Key) < r.end_.key := by
|
||||
intro x hx
|
||||
rcases hrawSub x hx with rfl | hx'
|
||||
· exact hkE
|
||||
· exact (hGmem x hx').2
|
||||
have hrawGe : ∀ x ∈ raw, r.begin.key ≤ (x : WithTop Key) := by
|
||||
intro x hx
|
||||
exact le_trans hlastB (WithTop.coe_le_coe.mpr (le_of_lt (hrawGt x hx)))
|
||||
set F : List Key := raw.insertionSort (· ≤ ·) with hF
|
||||
have hFperm : raw.Perm F := (List.perm_insertionSort (· ≤ ·) raw).symm
|
||||
have hFsorted : F.SortedLT := by
|
||||
rw [hF, List.sortedLT_iff_pairwise]
|
||||
exact DB.pairwise_lt_of_le_of_nodup
|
||||
(List.pairwise_insertionSort (r := (· ≤ ·)) raw)
|
||||
(hFperm.nodup_iff.mp hrawNodup)
|
||||
have hFlen : F.length = m := by rw [hF, List.length_insertionSort, hrawLen]
|
||||
have hFk : k ∈ F := hFperm.mem_iff.mp hrawk
|
||||
have hFmem : ∀ x ∈ F, last < x ∧ (x : WithTop Key) < r.end_.key := by
|
||||
intro x hx
|
||||
exact ⟨hrawGt x (hFperm.mem_iff.mpr hx), hrawLt x (hFperm.mem_iff.mpr hx)⟩
|
||||
have hFge : ∀ x ∈ F, r.begin.key ≤ (x : WithTop Key) := fun x hx =>
|
||||
hrawGe x (hFperm.mem_iff.mpr hx)
|
||||
have hFne : F ≠ [] := by
|
||||
intro h; rw [h] at hFlen; simp at hFlen; omega
|
||||
set D : List Key := res.keys ++ F with hD
|
||||
have hDsort : D.SortedLT := by
|
||||
rw [hD]
|
||||
refine List.sortedLT_append.mpr ⟨hsort, hFsorted, ?_⟩
|
||||
intro a ha b hb
|
||||
exact lt_of_le_of_lt (hle_last a ha) (hFmem b hb).1
|
||||
let dbw : DB := ⟨D, hDsort⟩
|
||||
have hdbw_keys : dbw.keys = D := rfl
|
||||
-- the begin selector clamps
|
||||
have hcount : dbw.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||||
reportedBelowBegin r res := by
|
||||
rw [hdbw_keys, hD, List.countP_append]
|
||||
have h1 : res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||||
reportedBelowBegin r res := rfl
|
||||
have h2 : F.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by
|
||||
rw [List.countP_eq_zero]
|
||||
intro x hx
|
||||
simp only [decide_eq_true_eq]
|
||||
exact not_lt.mpr (hFge x hx)
|
||||
rw [h1, h2, Nat.add_zero]
|
||||
have hanchor : r.begin.anchor dbw < 0 := by
|
||||
rw [anchor_eq_count_sub_slack dbw r (le_of_lt hbegin), hcount]
|
||||
have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
|
||||
omega
|
||||
have hrb : resolve dbw r.begin = (([] : Key) : WithTop Key) := resolve_eq_nil hanchor
|
||||
-- every stored key is below the end key
|
||||
have hDlt : ∀ x ∈ D, (x : WithTop Key) < r.end_.key := by
|
||||
intro x hx
|
||||
rw [hD] at hx
|
||||
rcases List.mem_append.mp hx with hx | hx
|
||||
· exact lt_of_le_of_lt (WithTop.coe_le_coe.mpr (hle_last x hx)) hlastE
|
||||
· exact (hFmem x hx).2
|
||||
have hlenD : D.length = res.keys.length + m := by
|
||||
rw [hD, List.length_append, hFlen]
|
||||
have hm_int : (m : Int) = 1 - r.end_.offset := by
|
||||
rw [hm]; exact Int.toNat_of_nonneg (by omega)
|
||||
have hcount_end : D.countP (fun x : Key => decide ((x : WithTop Key) < r.end_.key)) =
|
||||
D.length := by
|
||||
rw [List.countP_eq_length]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
exact hDlt x hx
|
||||
have hanchor_end : r.end_.anchor dbw = (res.keys.length : Int) := by
|
||||
cases hE : r.end_.key with
|
||||
| top =>
|
||||
rw [anchor_of_top (s := r.end_) hE, hlenD]
|
||||
push_cast
|
||||
rw [hm_int]
|
||||
ring
|
||||
| coe e =>
|
||||
have hce : D.countP (fun x : Key => decide (x < e)) = D.length := by
|
||||
rw [← countP_lt_coe (l := D) e, ← hE]
|
||||
exact hcount_end
|
||||
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD]
|
||||
push_cast
|
||||
rw [hm_int]
|
||||
ring
|
||||
have ht_lt : res.keys.length < D.length := by rw [hlenD]; omega
|
||||
have hDget? : dbw.keys[res.keys.length]? = some (F.head hFne) := by
|
||||
change D[res.keys.length]? = some (F.head hFne)
|
||||
rw [hD, List.getElem?_append]
|
||||
rw [if_neg (not_lt.mpr (le_refl _)), Nat.sub_self]
|
||||
rw [List.getElem?_eq_getElem (by rw [hFlen]; omega), List.head_eq_getElem hFne]
|
||||
have hre : resolve dbw r.end_ = ((F.head hFne : Key) : WithTop Key) := by
|
||||
rw [resolve, ite_eq_right (by rw [hanchor_end]; omega)]
|
||||
rw [hanchor_end, Int.toNat_natCast, hDget?]
|
||||
have hltF : ((last : Key) : WithTop Key) < resolve dbw r.end_ := by
|
||||
rw [hre]
|
||||
exact WithTop.coe_lt_coe.mpr (hFmem (F.head hFne) (List.head_mem hFne)).1
|
||||
-- complete keys of the witness are the reported keys
|
||||
have hckw : (RangeRead.completeKeys dbw r).filter
|
||||
(fun x : Key => decide (x ≤ last)) = res.keys := by
|
||||
rw [completeKeys_filter_le_of_resolve_begin (db := dbw) hrev hrb hltF]
|
||||
rw [hdbw_keys, hD, List.filter_append]
|
||||
have hres_fil : res.keys.filter (fun x : Key => decide
|
||||
((([] : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = res.keys := by
|
||||
rw [List.filter_eq_self]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
exact ⟨coe_nil_le_key_withTop _, hle_last x hx⟩
|
||||
have hF_fil : F.filter (fun x : Key => decide
|
||||
((([] : Key) : WithTop Key) ≤ (x : WithTop Key) ∧ (x : Key) ≤ last)) = [] := by
|
||||
rw [List.filter_eq_nil_iff]
|
||||
intro x hx
|
||||
simp only [Bool.not_eq_true, decide_eq_false_iff_not, not_and]
|
||||
exact fun _ => not_le.mpr (hFmem x hx).1
|
||||
rw [hres_fil, hF_fil, List.append_nil]
|
||||
have hcompw : RangeRead.Compatible dbw r res := by
|
||||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||||
exact ⟨hcomp₀.1, hckw⟩
|
||||
have hkdbw : k ∈ dbw.keys := by
|
||||
rw [hdbw_keys, hD]
|
||||
exact List.mem_append.mpr (Or.inr hFk)
|
||||
-- the erased database
|
||||
have hk_not_mem_res : k ∉ res.keys := by
|
||||
intro hk
|
||||
exact absurd (hle_last k hk) (not_le.mpr hkgt)
|
||||
have hD' : (dbw.erase k).keys = res.keys ++ F.erase k := by
|
||||
rw [DB.erase_keys, hdbw_keys, hD]
|
||||
exact List.erase_append_right F hk_not_mem_res
|
||||
have hcount' : (dbw.erase k).keys.countP
|
||||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||||
reportedBelowBegin r res := by
|
||||
rw [hD', List.countP_append]
|
||||
have h1 : res.keys.countP (fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) =
|
||||
reportedBelowBegin r res := rfl
|
||||
have h2 : (F.erase k).countP
|
||||
(fun x : Key => decide (((x : Key) : WithTop Key) < r.begin.key)) = 0 := by
|
||||
rw [List.countP_eq_zero]
|
||||
intro x hx
|
||||
simp only [decide_eq_true_eq]
|
||||
exact not_lt.mpr (hFge x (List.mem_of_mem_erase hx))
|
||||
rw [h1, h2, Nat.add_zero]
|
||||
have hanchor' : r.begin.anchor (dbw.erase k) < 0 := by
|
||||
rw [anchor_eq_count_sub_slack (dbw.erase k) r (le_of_lt hbegin), hcount']
|
||||
have hle : (reportedBelowBegin r res : ℤ) ≤ (beginSlack r : ℤ) := by exact_mod_cast hclamp
|
||||
omega
|
||||
have hrb' : resolve (dbw.erase k) r.begin = (([] : Key) : WithTop Key) :=
|
||||
resolve_eq_nil hanchor'
|
||||
have hD'lt : ∀ x ∈ (dbw.erase k).keys, (x : WithTop Key) < r.end_.key := by
|
||||
intro x hx
|
||||
exact hDlt x (by
|
||||
rw [DB.erase_keys, hdbw_keys] at hx
|
||||
exact List.mem_of_mem_erase hx)
|
||||
have hlenD' : (dbw.erase k).keys.length = res.keys.length + m - 1 := by
|
||||
rw [hD', List.length_append, List.length_erase_of_mem hFk, hFlen]
|
||||
omega
|
||||
have hcount_end' : (dbw.erase k).keys.countP
|
||||
(fun x : Key => decide ((x : WithTop Key) < r.end_.key)) = (dbw.erase k).keys.length := by
|
||||
rw [List.countP_eq_length]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
exact hD'lt x hx
|
||||
have hanchor_end' : r.end_.anchor (dbw.erase k) = (res.keys.length : Int) - 1 := by
|
||||
cases hE : r.end_.key with
|
||||
| top =>
|
||||
rw [anchor_of_top (s := r.end_) hE, hlenD']
|
||||
rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)]
|
||||
push_cast
|
||||
rw [hm_int]
|
||||
ring
|
||||
| coe e =>
|
||||
have hce : (dbw.erase k).keys.countP (fun x : Key => decide (x < e)) =
|
||||
(dbw.erase k).keys.length := by
|
||||
rw [← countP_lt_coe (l := (dbw.erase k).keys) e, ← hE]
|
||||
exact hcount_end'
|
||||
rw [anchor_of_coe (s := r.end_) (v := e) hE, hce, hlenD']
|
||||
rw [Nat.cast_sub (by omega : 1 ≤ res.keys.length + m)]
|
||||
push_cast
|
||||
rw [hm_int]
|
||||
ring
|
||||
have ht1 : 1 ≤ res.keys.length := List.length_pos_of_ne_nil hne
|
||||
have hEqInt : (res.keys.length : Int) - 1 = ((res.keys.length - 1 : ℕ) : Int) := by
|
||||
rw [Nat.cast_sub ht1]; norm_num
|
||||
have htoNat' : (r.end_.anchor (dbw.erase k)).toNat = res.keys.length - 1 := by
|
||||
rw [hanchor_end', hEqInt, Int.toNat_natCast]
|
||||
have hD'get? : (dbw.erase k).keys[res.keys.length - 1]? = some last := by
|
||||
rw [hD', List.getElem?_append]
|
||||
rw [if_pos (by omega : res.keys.length - 1 < res.keys.length)]
|
||||
rw [List.getElem?_eq_getElem (by omega : res.keys.length - 1 < res.keys.length)]
|
||||
congr 1
|
||||
rw [hlast, List.getLast_eq_getElem hne]
|
||||
have hre' : resolve (dbw.erase k) r.end_ = ((last : Key) : WithTop Key) := by
|
||||
rw [resolve, ite_eq_right (by rw [hanchor_end']; omega)]
|
||||
rw [hanchor_end', hEqInt, Int.toNat_natCast, hD'get?]
|
||||
have hckE : RangeRead.completeKeys (dbw.erase k) r = res.keys.dropLast := by
|
||||
unfold RangeRead.completeKeys RangeRead.inRange
|
||||
rw [ite_eq_right (by simp [hrev]), hrb', hre']
|
||||
have hpred : ∀ x ∈ (dbw.erase k).keys,
|
||||
decide (((↑([] : Key) : WithTop Key)) ≤ (x : WithTop Key) ∧
|
||||
(x : WithTop Key) < ((↑last : Key) : WithTop Key)) =
|
||||
decide (x < last) := by
|
||||
intro x _
|
||||
rw [decide_eq_decide]
|
||||
exact ⟨fun h => WithTop.coe_lt_coe.mp h.2,
|
||||
fun h => ⟨coe_nil_le_key_withTop _, WithTop.coe_lt_coe.mpr h⟩⟩
|
||||
rw [List.filter_congr hpred, hD', List.filter_append]
|
||||
conv_lhs => rw [← List.dropLast_append_getLast hne, hlast]
|
||||
rw [List.filter_append]
|
||||
have hfil : res.keys.dropLast.filter (fun x : Key => decide (x < last)) = res.keys.dropLast := by
|
||||
rw [List.filter_eq_self]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
have h := hsort.pairwise.rel_dropLast_getLast hx
|
||||
simpa [hlast] using h
|
||||
rw [hfil]
|
||||
have hFnil : (F.erase k).filter (fun x : Key => decide (x < last)) = [] := by
|
||||
rw [List.filter_eq_nil_iff]
|
||||
intro x hx
|
||||
simp only [Bool.not_eq_true, decide_eq_false_iff_not]
|
||||
exact not_lt.mpr (le_of_lt (hFmem x (List.mem_of_mem_erase hx)).1)
|
||||
rw [hFnil, List.append_nil]
|
||||
simp
|
||||
have hnot : ¬ RangeRead.Compatible (dbw.erase k) r res := by
|
||||
rw [compatible_iff_truncated_keys hrev hmore hne]
|
||||
intro h
|
||||
have heq := h.2
|
||||
rw [hckE] at heq
|
||||
have hfil : res.keys.dropLast.filter
|
||||
(fun x : Key => decide (x ≤ res.keys.getLast hne)) = res.keys.dropLast := by
|
||||
rw [List.filter_eq_self]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
rw [← hlast]
|
||||
exact hle_last x (List.mem_of_mem_dropLast hx)
|
||||
rw [hfil] at heq
|
||||
have hlen := congrArg List.length heq
|
||||
rw [List.length_dropLast] at hlen
|
||||
omega
|
||||
exact ⟨dbw, hcompw, hkdbw, hnot⟩
|
||||
|
||||
/-- Above the reported last and below the end key, a key is necessary in the clamped cell. -/
|
||||
theorem necessary_negCross_trunc_clamp_above_last {r : RangeRead} {res : Result} {k : Key}
|
||||
(hread : Readable r res) (hrev : r.reverse = false) (hmore : res.more = true)
|
||||
(hne : res.keys ≠ []) (hbegin : r.begin.offset < 0)
|
||||
(hend : r.end_.offset ≤ 0)
|
||||
(hclamp : reportedBelowBegin r res ≤ beginSlack r)
|
||||
(hB_le_last : r.begin.key ≤ ((res.keys.getLastD ([] : Key) : Key) : WithTop Key))
|
||||
(hkgt : res.keys.getLast hne < k) (hkE : (k : WithTop Key) < r.end_.key) :
|
||||
Necessary r res k := by
|
||||
obtain ⟨dbw, hcompw, hkdbw, hnot⟩ :=
|
||||
exists_witness_negCross_trunc_clamp_above_last hread hrev hmore hne hbegin hend hclamp
|
||||
hB_le_last hkgt hkE
|
||||
exact necessary_of_toggle ⟨dbw, hcompw, by
|
||||
rw [toggle_eq_erase_of_mem hkdbw]
|
||||
exact hnot⟩
|
||||
|
||||
/-- The closed form of the cell: `[⊥, end_.key)`. -/
|
||||
noncomputable def closedFormNegCrossTruncClampLastGe (r : RangeRead) (res : Result) :
|
||||
ConflictRange :=
|
||||
⟨(([] : Key) : WithTop Key), r.end_.key⟩
|
||||
|
||||
/-- **The clamped nonpositive-end cell with the reported last reaching `begin.key` is
|
||||
minimal.** -/
|
||||
theorem minimal_closedForm_of_negCross_trunc_clamp_lastGe {r : RangeRead} {res : Result}
|
||||
(hread : Readable r res) (hcell : negCrossTruncClampLastGe r res) :
|
||||
Minimal r res (closedFormNegCrossTruncClampLastGe r res) := by
|
||||
obtain ⟨hmore, hrev, hbegin, hne, hcrossD, hend, hclamp, hB_le_last⟩ := hcell
|
||||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||||
have hread' : Readable r res := ⟨db₀, hcomp₀⟩
|
||||
have hlastD : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||||
have hB_le_last' : r.begin.key ≤ ((res.keys.getLast hne : Key) : WithTop Key) := by
|
||||
rw [← hlastD]; exact hB_le_last
|
||||
have hlast_ck : res.keys.getLast hne ∈ RangeRead.completeKeys db₀ r :=
|
||||
(RangeRead.compatible_keys_prefix hcomp₀).subset (List.getLast_mem hne)
|
||||
obtain ⟨hlast_mem, -, hlast_res⟩ := RangeRead.mem_completeKeys.mp hlast_ck
|
||||
have hlastE : ((res.keys.getLast hne : Key) : WithTop Key) < r.end_.key :=
|
||||
lt_end_key_of_lt_resolve_end_nonpos hend hlast_mem hlast_res
|
||||
have hB_le_E : r.begin.key ≤ r.end_.key := le_trans hB_le_last' (le_of_lt hlastE)
|
||||
obtain ⟨db, hcomp, hanchor⟩ :=
|
||||
exists_clamp_compat_nonpos_lastGe_prefix ⟨db₀, hcomp₀⟩ hrev hne hbegin hend hB_le_last hclamp
|
||||
refine minimal_iff_sound_and_necessaryRange.mpr
|
||||
⟨sound_bottomEnd_of_clamp_nonpos_prefix hrev hbegin hend hB_le_E, ?_⟩
|
||||
intro k hkbot hkE
|
||||
by_cases hkhead : ((k : Key) : WithTop Key) < ((res.keys.head hne : Key) : WithTop Key)
|
||||
· exact necessary_below_head_of_anchor_nonpos_negCross db hcomp hrev hne hanchor hkhead
|
||||
· push_neg at hkhead
|
||||
by_cases hklast : ((k : Key) : WithTop Key) ≤ ((res.keys.getLast hne : Key) : WithTop Key)
|
||||
· exact necessary_of_between_result hread' (List.head_mem hne) (List.getLast_mem hne)
|
||||
(WithTop.coe_le_coe.mp hkhead) (WithTop.coe_le_coe.mp hklast)
|
||||
· push_neg at hklast
|
||||
exact necessary_negCross_trunc_clamp_above_last hread' hrev hmore hne hbegin hend
|
||||
hclamp hB_le_last (WithTop.coe_lt_coe.mp hklast) hkE
|
||||
|
||||
end FormalConflictRange
|
||||
Reference in new issue
Block a user