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,479 @@
|
||||
/-
|
||||
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.NegCrossTruncRevCrossedSmall
|
||||
|
||||
/-!
|
||||
# Reversed crossed negative-begin truncated cells with a large crossed gap
|
||||
|
||||
This module closes the large-capacity companion of the small-gap cell. The setting is
|
||||
|
||||
* `reverse = true`, `more = true`, `res.keys ≠ []`, `begin.offset < 0`;
|
||||
* the crossed selectors `end_.key < begin.key`, with the reported last `L < begin.key`;
|
||||
* `end_.offset = 1`;
|
||||
* `beginSlack + 2 ≤ intervalCard end_.key begin.key + |res.keys|`.
|
||||
|
||||
The minimal range is `[L, begin.key)`.
|
||||
|
||||
The soundness direction is the same count identity as the small-gap cell: agreement on
|
||||
`[L, begin.key)` fixes the stored keys in `[L, end_.key)` (the reported keys) and the
|
||||
stored keys in `[end_.key, begin.key)`, so the unresolved begin anchor keeps the same
|
||||
position relative to `L`. Necessity is new: every key in `[end_.key, begin.key)` must
|
||||
be in every sound range. The capacity condition supplies enough room in the gap for a
|
||||
witness database that stores `beginSlack + 2 - |res.keys|` keys there, which pushes the
|
||||
begin resolution past `L` and breaks the reported prefix.
|
||||
-/
|
||||
|
||||
namespace FormalConflictRange
|
||||
|
||||
set_option maxHeartbeats 1200000
|
||||
|
||||
/-- **The inner double filter of the reversed complete keys is the stored keys in
|
||||
`[L, end_.key)`.** At end offset one, a stored key lies below the end resolution exactly
|
||||
when it lies below `end_.key`. -/
|
||||
theorem filter_ge_filter_lt_resolve_end_eq_between {db : DB} {r : RangeRead}
|
||||
(hend : r.end_.offset = 1) (L : Key) :
|
||||
(db.keys.filter (fun k : Key => decide (L ≤ k))).filter
|
||||
(fun k : Key => decide (((k : Key) : WithTop Key) < resolve db r.end_)) =
|
||||
between db ((L : Key) : WithTop Key) r.end_.key := by
|
||||
unfold between
|
||||
rw [List.filter_filter]
|
||||
apply List.filter_congr
|
||||
intro k hk
|
||||
have hL : decide (L ≤ k) = decide (((L : Key) : WithTop Key) ≤ ((k : Key) : WithTop Key)) := by
|
||||
rw [decide_eq_decide]
|
||||
exact WithTop.coe_le_coe.symm
|
||||
rw [hL, Bool.and_comm]
|
||||
conv_rhs => rw [Bool.decide_and]
|
||||
congr 1
|
||||
rw [decide_eq_decide]
|
||||
constructor
|
||||
· intro h
|
||||
exact lt_end_key_of_lt_resolve_end_one hend hk h
|
||||
· intro h
|
||||
exact lt_of_lt_of_le h (key_le_resolve_of_one_le (db := db) (by omega))
|
||||
|
||||
/-- **The reported keys of a compatible reversed truncated result are the stored keys
|
||||
in `[last, end_.key)` read backwards.** -/
|
||||
theorem res_keys_eq_reverse_between_of_truncated_rev {db : DB} {r : RangeRead}
|
||||
{res : Result} (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||||
(hrev : r.reverse = true) (hne : res.keys ≠ []) (hend : r.end_.offset = 1) :
|
||||
res.keys = (between db ((res.keys.getLast hne : Key) : WithTop Key) r.end_.key).reverse := by
|
||||
set L : Key := res.keys.getLast hne with hLdef
|
||||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||||
have hfil : (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) = res.keys :=
|
||||
(compatible_iff_truncated_keys_rev (db := db) hrev hmore hne).mp hcomp |>.2
|
||||
have hLfil : L ∈ (RangeRead.completeKeys db r).filter (fun k => decide (L ≤ k)) := by
|
||||
rw [hfil]; exact hLmem
|
||||
have hLck : L ∈ RangeRead.completeKeys db r := (List.mem_filter.mp hLfil).1
|
||||
have hrb : resolve db r.begin ≤ ((L : Key) : WithTop Key) :=
|
||||
(RangeRead.mem_completeKeys.mp hLck).2.1
|
||||
have hid := completeKeys_filter_ge_eq_resolve_end (db := db) (r := r) (last := L) hrev hrb
|
||||
have hinner := filter_ge_filter_lt_resolve_end_eq_between (db := db) (r := r) (L := L) hend
|
||||
rw [hfil] at hid
|
||||
rw [hid, hinner]
|
||||
|
||||
/-- **Soundness of the begin-key range in the large-gap reversed crossed cell.**
|
||||
Agreement on `[last, begin.key)` preserves compatibility. -/
|
||||
theorem sound_beginKeyRange_of_negCross_crossedLarge {r : RangeRead} {res : Result}
|
||||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||||
(hne : res.keys ≠ []) (hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1) :
|
||||
Sound r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by
|
||||
set L : Key := res.keys.getLast hne with hLdef
|
||||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||||
have hLcross : ((L : Key) : WithTop Key) < r.begin.key := by rw [hLdef]; exact hcross
|
||||
have cle : L < r.end_.key := by
|
||||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||||
have hpre : res.keys <+: RangeRead.completeKeys db₀ r :=
|
||||
RangeRead.compatible_keys_prefix hcomp₀
|
||||
have hck := hpre.subset hLmem
|
||||
exact lt_end_key_of_lt_resolve_end_one hend (RangeRead.mem_completeKeys.mp hck).1
|
||||
(RangeRead.mem_completeKeys.mp hck).2.2
|
||||
have step : ∀ db db', AgreeOn ⟨((L : Key) : WithTop Key), r.begin.key⟩ db db' →
|
||||
RangeRead.Compatible db r res → RangeRead.Compatible db' r res := by
|
||||
intro db db' hag' hcomp
|
||||
have hbetweenEq : between db ((L : Key) : WithTop Key) r.begin.key =
|
||||
between db' ((L : Key) : WithTop Key) r.begin.key := by
|
||||
have h := hag'
|
||||
unfold AgreeOn ConflictRange.keys at h
|
||||
unfold between
|
||||
exact h
|
||||
have hLdb : L ∈ db.keys := RangeRead.compatible_keys_subset hcomp hLmem
|
||||
have hLdb' : L ∈ db'.keys := by
|
||||
have hmem : L ∈ between db ((L : Key) : WithTop Key) r.begin.key := by
|
||||
unfold between
|
||||
exact List.mem_filter.mpr ⟨hLdb, by simp [hLcross]⟩
|
||||
rw [hbetweenEq] at hmem
|
||||
exact (List.mem_filter.mp hmem).1
|
||||
have hbound := between_add_offset_le_one (db := db) hcomp hmore hrev hne hcross
|
||||
have hbound' : ((between db' ((L : Key) : WithTop Key) r.begin.key).length : Int) +
|
||||
r.begin.offset - 1 ≤ 0 := by
|
||||
rw [← hbetweenEq]; exact hbound
|
||||
have hres' : resolve db' r.begin ≤ ((L : Key) : WithTop Key) :=
|
||||
resolve_begin_le_of_between_bound hLdb' hLcross hbound'
|
||||
have hbetweenE : between db ((L : Key) : WithTop Key) r.end_.key =
|
||||
between db' ((L : Key) : WithTop Key) r.end_.key :=
|
||||
between_eq_of_agree (db := db) (db' := db') (last := ((L : Key) : WithTop Key))
|
||||
(beginKey := r.end_.key) (endKey := r.begin.key) hag' (le_of_lt cle) (le_of_lt hcrossE)
|
||||
have hresBetween := res_keys_eq_reverse_between_of_truncated_rev (db := db) (r := r)
|
||||
(res := res) hcomp hmore hrev hne hend
|
||||
have h1 := completeKeys_filter_ge_eq_resolve_end (db := db') (r := r) (last := L) hrev hres'
|
||||
have h2 := filter_ge_filter_lt_resolve_end_eq_between (db := db') (r := r) (L := L) hend
|
||||
rw [compatible_iff_truncated_keys_rev (db := db') hrev hmore hne]
|
||||
refine ⟨by simpa [RangeRead.withinLimit] using hcomp.1, ?_⟩
|
||||
rw [← hLdef]
|
||||
rw [h1, h2, ← hbetweenE]
|
||||
rw [hLdef]
|
||||
rw [← hresBetween]
|
||||
intro db₁ db₂ hag
|
||||
exact ⟨step db₁ db₂ hag, step db₂ db₁ hag.symm⟩
|
||||
|
||||
/-- **The reported length is at most `beginSlack + 1`.** The reported keys are the
|
||||
stored keys in `[last, end_.key)`, which is contained in `[last, begin.key)`, and the
|
||||
begin anchor bound caps that interval at `beginSlack + 1`. -/
|
||||
theorem res_length_le_beginSlack_succ_of_negCross_trunc_rev {r : RangeRead} {res : Result}
|
||||
{db : DB} (hcomp : RangeRead.Compatible db r res) (hmore : res.more = true)
|
||||
(hrev : r.reverse = true) (hne : res.keys ≠ [])
|
||||
(hbeg : r.begin.offset < 0)
|
||||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1) :
|
||||
res.keys.length ≤ beginSlack r + 1 := by
|
||||
set L : Key := res.keys.getLast hne with hLdef
|
||||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||||
have hLck : L ∈ RangeRead.completeKeys db r :=
|
||||
(RangeRead.compatible_keys_prefix hcomp).subset hLmem
|
||||
have hLltE : ((L : Key) : WithTop Key) < r.end_.key :=
|
||||
lt_end_key_of_lt_resolve_end_one hend (RangeRead.mem_completeKeys.mp hLck).1
|
||||
(RangeRead.mem_completeKeys.mp hLck).2.2
|
||||
have ht_eq : res.keys.length =
|
||||
(between db ((L : Key) : WithTop Key) r.end_.key).length := by
|
||||
rw [res_keys_eq_reverse_between_of_truncated_rev (db := db) (r := r) (res := res)
|
||||
hcomp hmore hrev hne hend, List.length_reverse]
|
||||
have hsplit := between_length_add (db := db) (L := ((L : Key) : WithTop Key))
|
||||
(E := r.end_.key) (B := r.begin.key) (le_of_lt hLltE) (le_of_lt hcrossE)
|
||||
have hlen_le : (between db ((L : Key) : WithTop Key) r.end_.key).length ≤
|
||||
(between db ((L : Key) : WithTop Key) r.begin.key).length := by
|
||||
rw [hsplit]; omega
|
||||
have hbound := between_add_offset_le_one (db := db) hcomp hmore hrev hne
|
||||
(by rw [← hLdef]; exact hcross)
|
||||
rw [← hLdef] at hbound
|
||||
have hs : (beginSlack r : ℤ) = -r.begin.offset := by
|
||||
unfold beginSlack
|
||||
rw [Int.toNat_of_nonneg (show (0 : ℤ) ≤ 0 - r.begin.offset by omega)]
|
||||
ring
|
||||
have h1 : (res.keys.length : ℤ) ≤ (beginSlack r : ℤ) + 1 := by
|
||||
have h2 : (res.keys.length : ℤ) =
|
||||
((between db ((L : Key) : WithTop Key) r.end_.key).length : ℤ) := by
|
||||
exact_mod_cast ht_eq
|
||||
have h3 : ((between db ((L : Key) : WithTop Key) r.end_.key).length : ℤ) ≤
|
||||
((between db ((L : Key) : WithTop Key) r.begin.key).length : ℤ) := by
|
||||
exact_mod_cast hlen_le
|
||||
omega
|
||||
exact_mod_cast h1
|
||||
|
||||
/-- Inserting a key above every element of a prefix leaves the prefix untouched. -/
|
||||
theorem orderedInsert_append_of_forall_lt_left {l r : List Key} {k : Key}
|
||||
(h : ∀ x ∈ l, x < k) :
|
||||
(l ++ r).orderedInsert (· ≤ ·) k = l ++ r.orderedInsert (· ≤ ·) k := by
|
||||
induction l with
|
||||
| nil => simp
|
||||
| cons a l ih =>
|
||||
have hka : a < k := h a (by simp)
|
||||
rw [List.cons_append, List.orderedInsert_cons, ite_eq_right (not_le.mpr hka)]
|
||||
rw [ih (fun x hx => h x (by simp [hx]))]
|
||||
rfl
|
||||
|
||||
/-- In a strictly increasing list, the least key (which is the first entry) is any
|
||||
element that is at most every element and itself appears in the list. -/
|
||||
theorem getElem_zero_eq_of_forall_le {l : List Key} (hs : l.SortedLT) {L : Key}
|
||||
(hmin : ∀ x ∈ l, L ≤ x) (hL : L ∈ l) (h0 : 0 < l.length) :
|
||||
l[(0 : ℕ)] = L := by
|
||||
obtain ⟨j, hj, hjx⟩ := List.mem_iff_getElem.mp hL
|
||||
have h1 : l[(0 : ℕ)] ≤ l[j] :=
|
||||
hs.getElem_le_getElem_iff.mpr (Nat.zero_le j)
|
||||
rw [hjx] at h1
|
||||
have h2 : L ≤ l[(0 : ℕ)] := hmin _ (List.getElem_mem h0)
|
||||
exact le_antisymm h1 h2
|
||||
|
||||
/-- **Gap-key necessity in the large-gap reversed crossed truncated cell.** The witness
|
||||
database stores the reported keys in ascending order together with `beginSlack + 1 - t`
|
||||
gap keys in `[end_.key, begin_.key)`, so its begin anchor is exactly zero and it resolves
|
||||
onto the reported last. Inserting the target raises the anchor to one: the reported last
|
||||
leaves the window and the reported keys can no longer be a prefix of the complete keys.
|
||||
The capacity condition supplies the `beginSlack + 2 - t` distinct gap keys (the witness
|
||||
keys plus the target) needed. -/
|
||||
theorem necessary_of_mem_gap_of_negCross_trunc_rev_crossedLarge {r : RangeRead} {res : Result}
|
||||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||||
(hne : res.keys ≠ []) (hbeg : r.begin.offset < 0)
|
||||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1)
|
||||
(hlarge : (beginSlack r : ℕ∞) + 2 ≤
|
||||
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞))
|
||||
{k : Key} (hkE : r.end_.key ≤ (k : WithTop Key))
|
||||
(hkB : (k : WithTop Key) < r.begin.key) :
|
||||
Necessary r res k := by
|
||||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||||
set L : Key := res.keys.getLast hne with hLdef
|
||||
set t : ℕ := res.keys.length with htdef
|
||||
set s : ℕ := beginSlack r with hsdef
|
||||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||||
have hsort : res.keys.SortedGT := RangeRead.compatible_sortedGT hcomp₀ hrev
|
||||
have hLle : ∀ x ∈ res.keys, L ≤ x := by
|
||||
intro x hx; rw [hLdef]; exact (hsort.pairwise.imp le_of_lt).rel_getLast hx
|
||||
have hreported_lt_E : ∀ x ∈ res.keys, ((x : Key) : WithTop Key) < r.end_.key := by
|
||||
intro x hx
|
||||
have hxck : x ∈ RangeRead.completeKeys db₀ r :=
|
||||
(RangeRead.compatible_keys_prefix hcomp₀).subset hx
|
||||
exact lt_end_key_of_lt_resolve_end_one hend (RangeRead.mem_completeKeys.mp hxck).1
|
||||
(RangeRead.mem_completeKeys.mp hxck).2.2
|
||||
have hLltE : ((L : Key) : WithTop Key) < r.end_.key := hreported_lt_E L hLmem
|
||||
have ht_le : t ≤ s + 1 := by
|
||||
rw [htdef, hsdef]
|
||||
exact res_length_le_beginSlack_succ_of_negCross_trunc_rev hcomp₀ hmore hrev hne
|
||||
hbeg hcross hcrossE hend
|
||||
-- enough room in the crossed gap for the witness keys and the target
|
||||
have hcard : (((s + 1 - t) + 1 : ℕ) : ℕ∞) ≤ intervalCard r.end_.key r.begin.key := by
|
||||
cases hcap : intervalCard r.end_.key r.begin.key with
|
||||
| top => exact le_top
|
||||
| coe n =>
|
||||
have hnat : s + 2 ≤ n + t := by
|
||||
have h' : ((s + 2 : ℕ) : ℕ∞) ≤ ((n + t : ℕ) : ℕ∞) := by
|
||||
rw [hcap] at hlarge
|
||||
simpa [ENat.natCast_add] using hlarge
|
||||
exact_mod_cast h'
|
||||
have hn : (s + 1 - t) + 1 ≤ n := by omega
|
||||
exact_mod_cast hn
|
||||
obtain ⟨dbG, hGrange, hkG, hGlen⟩ :=
|
||||
exists_db_keysIn_avoid (B := r.end_.key) (E := r.begin.key) (k := k)
|
||||
(m := s + 1 - t) hcard
|
||||
set G : List Key := dbG.keys with hGdef
|
||||
have hGmem : ∀ x ∈ G, r.end_.key ≤ (x : WithTop Key) ∧
|
||||
(x : WithTop Key) < r.begin.key := by
|
||||
intro x hx
|
||||
simpa only [G, keysIn, Set.mem_ofPred_eq] using hGrange x (by simpa [hGdef] using hx)
|
||||
have hGlen' : G.length = s + 1 - t := by rw [hGdef]; exact hGlen
|
||||
-- assemble the ascending witness database
|
||||
have hresSort : res.keys.reverse.SortedLT := hsort.reverse
|
||||
have hGsorted : G.SortedLT := by rw [hGdef]; exact dbG.sorted
|
||||
have hcrossG : ∀ x ∈ res.keys.reverse, ∀ y ∈ G, x < y := by
|
||||
intro x hx y hy
|
||||
have hx' : x ∈ res.keys := List.mem_reverse.mp hx
|
||||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le (hreported_lt_E x hx') (hGmem y hy).1)
|
||||
have hkeysSorted : (res.keys.reverse ++ G).SortedLT :=
|
||||
List.sortedLT_append.mpr ⟨hresSort, hGsorted, hcrossG⟩
|
||||
let db_a : DB := ⟨res.keys.reverse ++ G, hkeysSorted⟩
|
||||
have hdb_a : db_a.keys = res.keys.reverse ++ G := rfl
|
||||
have hcntB : db_a.keys.countP (fun x : Key =>
|
||||
decide (((x : Key) : WithTop Key) < r.begin.key)) = t + G.length := by
|
||||
rw [hdb_a, List.countP_append, List.countP_reverse]
|
||||
have h1 : res.keys.countP (fun x : Key =>
|
||||
decide (((x : Key) : WithTop Key) < r.begin.key)) = t := by
|
||||
rw [htdef, List.countP_eq_length]
|
||||
intro x hx; rw [decide_eq_true_eq]
|
||||
exact lt_trans (hreported_lt_E x hx) hcrossE
|
||||
have h2 : G.countP (fun x : Key =>
|
||||
decide (((x : Key) : WithTop Key) < r.begin.key)) = G.length := by
|
||||
rw [List.countP_eq_length]
|
||||
intro x hx; rw [decide_eq_true_eq]; exact (hGmem x hx).2
|
||||
rw [h1, h2]
|
||||
have hanchor : r.begin.anchor db_a = 0 := by
|
||||
rw [anchor_eq_count_sub_slack db_a r (le_of_lt hbeg), hcntB, hGlen']
|
||||
have hts : t + (s + 1 - t) = s + 1 := Nat.add_sub_of_le ht_le
|
||||
rw [hts]; push_cast; ring
|
||||
have h0lt : (0 : ℕ) < db_a.keys.length := by
|
||||
rw [hdb_a, List.length_append, List.length_reverse]
|
||||
have htpos : 0 < res.keys.length := List.length_pos_of_ne_nil hne
|
||||
omega
|
||||
have hmin_a : ∀ x ∈ db_a.keys, L ≤ x := by
|
||||
intro x hx
|
||||
rw [hdb_a] at hx
|
||||
rcases List.mem_append.mp hx with hxr | hxG
|
||||
· exact hLle x (List.mem_reverse.mp hxr)
|
||||
· exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hLltE (hGmem x hxG).1))
|
||||
have hLmem_a : L ∈ db_a.keys := by
|
||||
rw [hdb_a]; exact List.mem_append_left _ (List.mem_reverse.mpr hLmem)
|
||||
have hget0 : db_a.keys[(0 : ℕ)] = L :=
|
||||
getElem_zero_eq_of_forall_le hkeysSorted hmin_a hLmem_a h0lt
|
||||
have hresBegin : resolve db_a r.begin = ((L : Key) : WithTop Key) := by
|
||||
have h0 : 0 ≤ r.begin.anchor db_a := by rw [hanchor]
|
||||
have hlt : (r.begin.anchor db_a).toNat < db_a.keys.length := by
|
||||
rw [hanchor]; simpa using h0lt
|
||||
rw [resolve_eq_getElem h0 hlt]
|
||||
have hidx : (r.begin.anchor db_a).toNat = 0 := by rw [hanchor]; rfl
|
||||
simpa [hidx] using hget0
|
||||
have hresEnd_ge : r.end_.key ≤ resolve db_a r.end_ :=
|
||||
key_le_resolve_of_one_le (db := db_a) (s := r.end_) (by omega)
|
||||
have hfilter : db_a.keys.filter (fun x : Key => decide
|
||||
(((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||||
(x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by
|
||||
rw [hdb_a, List.filter_append]
|
||||
have h1 : (res.keys.reverse).filter (fun x : Key => decide
|
||||
(((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||||
(x : WithTop Key) < resolve db_a r.end_)) = res.keys.reverse := by
|
||||
rw [List.filter_eq_self]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
exact ⟨WithTop.coe_le_coe.mpr (hLle x (List.mem_reverse.mp hx)),
|
||||
lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx)) hresEnd_ge⟩
|
||||
have h2 : G.filter (fun x : Key => decide
|
||||
(((L : Key) : WithTop Key) ≤ (x : WithTop Key) ∧
|
||||
(x : WithTop Key) < resolve db_a r.end_)) = [] := by
|
||||
rw [List.filter_eq_nil_iff]
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
intro hP
|
||||
have hxmem : x ∈ db_a.keys := by rw [hdb_a]; exact List.mem_append_right _ hx
|
||||
have hrele : resolve db_a r.end_ ≤ (x : WithTop Key) :=
|
||||
(key_le_resolve_iff_of_offset_one (db := db_a) (s := r.end_) hend hxmem).mp
|
||||
(hGmem x hx).1
|
||||
exact not_lt.mpr hrele hP.2
|
||||
rw [h1, h2, List.append_nil]
|
||||
have hcomplete : RangeRead.completeKeys db_a r = res.keys := by
|
||||
unfold RangeRead.completeKeys RangeRead.inRange
|
||||
rw [hrev]
|
||||
simp only [ite_true]
|
||||
rw [hresBegin, hfilter, List.reverse_reverse]
|
||||
have hcompat_a : RangeRead.Compatible db_a r res :=
|
||||
⟨hcomp₀.1, Or.inl ⟨hmore, by rw [hcomplete], hne⟩⟩
|
||||
have hk_a : k ∉ db_a.keys := by
|
||||
rw [hdb_a]
|
||||
intro hmem
|
||||
rcases List.mem_append.mp hmem with hx | hx
|
||||
· exact absurd (hreported_lt_E k (List.mem_reverse.mp hx)) (not_lt.mpr hkE)
|
||||
· exact hkG (by simpa [hGdef] using hx)
|
||||
-- insert the target: the first two sorted keys are the reported last and something above
|
||||
let db_b : DB := db_a.insert k hk_a
|
||||
have hins : (res.keys.reverse ++ G).orderedInsert (· ≤ ·) k =
|
||||
res.keys.reverse ++ G.orderedInsert (· ≤ ·) k := by
|
||||
refine orderedInsert_append_of_forall_lt_left ?_
|
||||
intro x hx
|
||||
exact WithTop.coe_lt_coe.mp
|
||||
(lt_of_lt_of_le (hreported_lt_E x (List.mem_reverse.mp hx)) hkE)
|
||||
have hb_keys : db_b.keys = res.keys.reverse ++ G.orderedInsert (· ≤ ·) k := by
|
||||
rw [show db_b.keys = db_a.keys.orderedInsert (· ≤ ·) k from rfl, hdb_a, hins]
|
||||
have hb_len : db_b.keys.length = s + 2 := by
|
||||
rw [hb_keys, List.length_append, List.length_reverse, List.orderedInsert_length,
|
||||
hGlen', htdef, hsdef]
|
||||
omega
|
||||
have hb_all : ∀ x ∈ db_b.keys,
|
||||
decide (((x : Key) : WithTop Key) < r.begin.key) = true := by
|
||||
intro x hx
|
||||
rw [decide_eq_true_eq]
|
||||
rw [hb_keys] at hx
|
||||
rcases List.mem_append.mp hx with hx' | hx'
|
||||
· exact lt_trans (hreported_lt_E x (List.mem_reverse.mp hx')) hcrossE
|
||||
· rw [List.mem_orderedInsert] at hx'
|
||||
rcases hx' with rfl | hxG
|
||||
· exact hkB
|
||||
· exact (hGmem x hxG).2
|
||||
have hb_cntB : db_b.keys.countP (fun x : Key =>
|
||||
decide (((x : Key) : WithTop Key) < r.begin.key)) = s + 2 := by
|
||||
rw [← hb_len, List.countP_eq_length]
|
||||
exact hb_all
|
||||
have hanchor_b : r.begin.anchor db_b = 1 := by
|
||||
rw [anchor_eq_count_sub_slack db_b r (le_of_lt hbeg), hb_cntB]; push_cast; ring
|
||||
have hget0_b : db_b.keys[(0 : ℕ)] = L := by
|
||||
have hmin_b : ∀ x ∈ db_b.keys, L ≤ x := by
|
||||
intro x hx
|
||||
rw [hb_keys] at hx
|
||||
rcases List.mem_append.mp hx with hxr | hxG
|
||||
· exact hLle x (List.mem_reverse.mp hxr)
|
||||
· rw [List.mem_orderedInsert] at hxG
|
||||
rcases hxG with rfl | hxG
|
||||
· exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hLltE hkE))
|
||||
· exact le_of_lt (WithTop.coe_lt_coe.mp (lt_of_lt_of_le hLltE (hGmem x hxG).1))
|
||||
have hLmem_b : L ∈ db_b.keys := by
|
||||
rw [hb_keys]; exact List.mem_append_left _ (List.mem_reverse.mpr hLmem)
|
||||
have h0lt_b : 0 < db_b.keys.length := by rw [hb_len]; omega
|
||||
exact getElem_zero_eq_of_forall_le db_b.sorted hmin_b hLmem_b h0lt_b
|
||||
have h1lt : (1 : ℕ) < db_b.keys.length := by
|
||||
rw [hb_len]; omega
|
||||
have hresBegin_b : resolve db_b r.begin = ((db_b.keys[(1 : ℕ)] : Key) : WithTop Key) := by
|
||||
have h0 : 0 ≤ r.begin.anchor db_b := by rw [hanchor_b]; norm_num
|
||||
have hlt : (r.begin.anchor db_b).toNat < db_b.keys.length := by
|
||||
rw [hanchor_b]; simpa using h1lt
|
||||
rw [resolve_eq_getElem h0 hlt]
|
||||
have hidx : (r.begin.anchor db_b).toNat = 1 := by rw [hanchor_b]; rfl
|
||||
simp [hidx]
|
||||
have hLlt : L < db_b.keys[(1 : ℕ)] := by
|
||||
have h01 : (0 : ℕ) < db_b.keys.length := by omega
|
||||
have h := (List.SortedLT.getElem_lt_getElem_iff db_b.sorted (hi := h01)
|
||||
(hj := h1lt)).mpr (show (0 : ℕ) < 1 by omega)
|
||||
rwa [hget0_b] at h
|
||||
have hLnot_ck : L ∉ RangeRead.completeKeys db_b r := by
|
||||
intro hmem
|
||||
have hle := (RangeRead.mem_completeKeys.mp hmem).2.1
|
||||
rw [hresBegin_b] at hle
|
||||
exact absurd hle (not_le.mpr (WithTop.coe_lt_coe.mpr hLlt))
|
||||
have hnotCompat_b : ¬ RangeRead.Compatible db_b r res := by
|
||||
intro hc
|
||||
rcases hc.2 with ⟨_, hpre, _⟩ | ⟨hm, _⟩
|
||||
· exact hLnot_ck (hpre.subset hLmem)
|
||||
· rw [hmore] at hm; exact absurd hm (by simp)
|
||||
refine necessary_of_toggle ⟨db_a, hcompat_a, ?_⟩
|
||||
rw [toggle_eq_insert_of_not_mem hk_a]
|
||||
exact hnotCompat_b
|
||||
|
||||
/-- **Minimality of `[last observed, begin.key)` in the large-gap reversed crossed
|
||||
truncated cell.** Soundness is the count identity; necessity splits the range at
|
||||
`end_.key`: below it the reported keys and the above-head rule cover every key, and at or
|
||||
above it the gap witness applies. -/
|
||||
theorem minimal_beginKeyRange_of_negCross_crossedLarge {r : RangeRead} {res : Result}
|
||||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||||
(hne : res.keys ≠ []) (hbeg : r.begin.offset < 0)
|
||||
(hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key)
|
||||
(hcrossE : r.end_.key < r.begin.key) (hend : r.end_.offset = 1)
|
||||
(hlarge : (beginSlack r : ℕ∞) + 2 ≤
|
||||
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞)) :
|
||||
Minimal r res ⟨((res.keys.getLast hne : Key) : WithTop Key), r.begin.key⟩ := by
|
||||
set L : Key := res.keys.getLast hne with hLdef
|
||||
have hLmem : L ∈ res.keys := by rw [hLdef]; exact List.getLast_mem hne
|
||||
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
|
||||
· exact sound_beginKeyRange_of_negCross_crossedLarge hread hmore hrev hne hcross hcrossE hend
|
||||
· intro k hk
|
||||
rw [ConflictRange.mem_iff] at hk
|
||||
obtain ⟨hkL, hkB⟩ := hk
|
||||
by_cases hkE : ((k : Key) : WithTop Key) < r.end_.key
|
||||
· by_cases hkhead : k ≤ res.keys.head hne
|
||||
· exact necessary_of_between_result hread hLmem (List.head_mem hne)
|
||||
(WithTop.coe_le_coe.mp hkL) hkhead
|
||||
· intro cr hs
|
||||
exact necessary_head_to_end_key hread hrev hne (le_of_eq hend) cr hs
|
||||
(le_of_lt (lt_of_not_ge hkhead)) hkE
|
||||
· exact necessary_of_mem_gap_of_negCross_trunc_rev_crossedLarge hread hmore hrev hne hbeg
|
||||
hcross hcrossE hend hlarge (not_lt.mp hkE) hkB
|
||||
|
||||
/-- The reversed crossed negative-begin truncated large-gap cell. -/
|
||||
abbrev negCrossTruncRevCrossedLarge (r : RangeRead) (res : Result) : Prop :=
|
||||
res.more = true ∧ r.reverse = true ∧ r.begin.offset < 0 ∧ res.keys ≠ [] ∧
|
||||
((res.keys.getLastD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||||
r.end_.key < r.begin.key ∧ r.end_.offset = 1 ∧
|
||||
(beginSlack r : ℕ∞) + 2 ≤
|
||||
intervalCard r.end_.key r.begin.key + (res.keys.length : ℕ∞)
|
||||
|
||||
/-- The closed form of the reversed crossed negative-begin truncated large-gap cell:
|
||||
`[last reported, begin.key)`. -/
|
||||
noncomputable def closedFormNegCrossTruncRevCrossedLarge (_r : RangeRead) (res : Result) :
|
||||
ConflictRange :=
|
||||
⟨((res.keys.getLastD ([] : Key) : Key) : WithTop Key), _r.begin.key⟩
|
||||
|
||||
/-- **The reversed crossed negative-begin truncated large-gap closed form is
|
||||
minimal.** -/
|
||||
theorem minimal_closedForm_of_negCross_trunc_rev_crossedLarge {r : RangeRead}
|
||||
{res : Result} (hread : Readable r res) (h : negCrossTruncRevCrossedLarge r res) :
|
||||
Minimal r res (closedFormNegCrossTruncRevCrossedLarge r res) := by
|
||||
obtain ⟨hmore, hrev, hbeg, hne, hcrossD, hcrossE, hend, hlarge⟩ := h
|
||||
have hlast : res.keys.getLastD ([] : Key) = res.keys.getLast hne := by
|
||||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne]; rfl
|
||||
have hcross : ((res.keys.getLast hne : Key) : WithTop Key) < r.begin.key := by
|
||||
rw [← hlast]; exact hcrossD
|
||||
unfold closedFormNegCrossTruncRevCrossedLarge
|
||||
rw [hlast]
|
||||
exact minimal_beginKeyRange_of_negCross_crossedLarge hread hmore hrev hne hbeg hcross
|
||||
hcrossE hend hlarge
|
||||
|
||||
end FormalConflictRange
|
||||
Reference in new issue
Block a user