Files
conflict-range/FormalConflictRange/Selector.lean
T

359 lines
18 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.
/-
Released under Apache 2.0 license as described in the file LICENSE.
-/
import FormalConflictRange.Basic
/-!
# Selectors and resolution
A `Selector` names a key together with a signed offset. Resolving a selector
against a database targets the stored key at index
`countP (· < selector.key) + selector.offset - 1`: the last stored key strictly
below `selector.key` when the offset is zero, advancing one key per unit of
offset, so offset `1` is the first key at or above `selector.key`. Indices below
zero resolve to the empty key and indices past the end to `⊤`.
Resolution returns a `WithTop Key`, where `⊤` is the sentinel "no such key".
This makes the past-the-end case explicit in the type, so no concrete sentinel
key and no well-formedness side condition need to be threaded through the API.
The selector's `key` is a `WithTop Key` rather than a plain `Key`: it anchors
the offset in the key space by counting the stored keys strictly below it. The
bottom of `WithTop Key` is the empty key, which counts no keys, and the top
`⊤` counts every stored key, so a selector keyed at `⊤` anchors past the end
of the database whatever its size. This is what makes a database-independent
"read all keys" expressible: `key := ⊤, offset := 1` resolves to `⊤` for every
database, and `key := ↑[], offset := 0` resolves to the empty key, so the read
with those two selectors and no limit returns every stored key (see
`RangeRead.readAll`). No plain key could anchor past every stored key, since
`Key` has no greatest element, and only the empty key — the least key — anchors
at the very start, which is why the bottom needs no sentinel of its own.
Resolution still clamps negative anchors to the empty key, so a selector never
fails to resolve.
-/
namespace FormalConflictRange
/-- A selector names a key position in `WithTop Key`, with an integer offset.
The empty key anchors at the start of the key space, and `⊤` anchors past every
stored key. -/
structure Selector where
key : WithTop Key
offset : Int
/-- The signed index a selector targets: the number of stored keys strictly
below `selector.key` in `WithTop Key` — none for the empty key, all of them for
`⊤` — less one, shifted by `selector.offset`. Offset zero names the last stored
key strictly below `selector.key`, which is index `-1` when there is none;
offset `1` names the first stored key at or above it; offset `-1` names the key
before that. -/
def Selector.anchor (db : DB) (s : Selector) : Int :=
(db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) + s.offset - 1
/-- Resolve a selector against `db`: target the anchor key, clamping negative
indices to the empty key and indices past the end to the sentinel `⊤`. -/
def resolve (db : DB) (s : Selector) : WithTop Key :=
if s.anchor db < 0 then ([] : Key)
else match db.keys[(s.anchor db).toNat]? with
| some k => k
| none => ⊤
/-- A selector targeting a negative index resolves to the empty key. -/
theorem resolve_eq_nil {db : DB} {s : Selector} (h : s.anchor db < 0) :
resolve db s = ↑([] : Key) := by
rw [resolve, ite_eq_left h]
/-- A selector targeting an in-range index resolves to the stored key there. -/
theorem resolve_eq_getElem {db : DB} {s : Selector}
(h0 : 0 ≤ s.anchor db) (h : (s.anchor db).toNat < db.keys.length) :
resolve db s = ↑(db.keys[(s.anchor db).toNat]) := by
rw [resolve, ite_eq_right (not_lt.mpr h0), List.getElem?_eq_getElem h]
/-- A selector targeting an index at or past the end resolves to `⊤`. -/
theorem resolve_eq_top {db : DB} {s : Selector}
(h0 : 0 ≤ s.anchor db) (h : db.keys.length ≤ (s.anchor db).toNat) :
resolve db s = (⊤ : WithTop Key) := by
rw [resolve, ite_eq_right (not_lt.mpr h0), List.getElem?_eq_none h]
/-- Every resolution is the empty key, the sentinel `⊤`, or a stored key. -/
theorem resolve_eq_nil_or_eq_top_or_mem {db : DB} {s : Selector} :
resolve db s = ↑([] : Key) ∨ resolve db s = (⊤ : WithTop Key) ∨
∃ k ∈ db.keys, resolve db s = ↑k := by
rw [resolve]
split
· exact Or.inl rfl
· rename_i h
cases hlt : db.keys[(s.anchor db).toNat]? with
| none => exact Or.inr (Or.inl rfl)
| some k => exact Or.inr (Or.inr ⟨k, List.mem_of_getElem? hlt, rfl⟩)
/-- Resolution depends on a database only through its keys. -/
theorem resolve_congr {db₁ db₂ : DB} {s : Selector} (hk : db₁.keys = db₂.keys) :
resolve db₁ s = resolve db₂ s := by
unfold resolve Selector.anchor
rw [hk]
/-- Outside the negative clamp, `resolve` is a `getElem?`. -/
theorem resolve_eq_ite (db : DB) (s : Selector) :
resolve db s = (if s.anchor db < 0 then (↑([] : Key) : WithTop Key)
else match db.keys[(s.anchor db).toNat]? with
| some k => (↑k : WithTop Key)
| none => ⊤) := by
unfold resolve
split <;> rfl
/-- A selector keyed at a coerced key `↑v` anchors at the count of stored keys
strictly below `v`. -/
theorem anchor_of_coe {db : DB} {s : Selector} {v : Key} (h : s.key = (↑v : WithTop Key)) :
s.anchor db = (db.keys.countP (fun k => decide (k < v)) : Int) + s.offset - 1 := by
unfold Selector.anchor
rw [h, countP_lt_coe]
/-- A selector keyed at the empty key anchors at its offset less one: the
empty key counts no stored keys below it. -/
theorem anchor_of_nil {db : DB} {s : Selector} (h : s.key = (([] : Key) : WithTop Key)) :
s.anchor db = s.offset - 1 := by
unfold Selector.anchor
rw [h, countP_lt_nil]
omega
/-- A selector keyed at `⊤` anchors past every stored key: all of them lie
strictly below `⊤`. -/
theorem anchor_of_top {db : DB} {s : Selector} (h : s.key = (⊤ : WithTop Key)) :
s.anchor db = (db.keys.length : Int) + s.offset - 1 := by
unfold Selector.anchor
rw [h, countP_lt_top]
/-- The anchor is nonnegative when the offset is at least one. -/
theorem anchor_nonneg_of_one_le {db : DB} {s : Selector} (h : 1 ≤ s.offset) :
0 ≤ s.anchor db := by
unfold Selector.anchor
have : (0 : Int) ≤
(db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) :=
Int.natCast_nonneg _
omega
/-- The anchor's natural-number value splits as the count of keys strictly below
the selector key plus the nonnegative offset shift. -/
theorem anchor_toNat_eq {db : DB} {s : Selector} (h : 1 ≤ s.offset) :
(s.anchor db).toNat =
db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) +
(s.offset - 1).toNat := by
unfold Selector.anchor
rw [show ((db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) +
s.offset - 1) =
((db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int) +
(s.offset - 1)) by ring,
Int.toNat_add (Int.natCast_nonneg _) (by omega), Int.toNat_natCast]
/-- Inserting the successor chain of `[a, b)` when the selector is keyed at `a`
leaves the selector's anchor index unchanged: the chain starts at `a` and so
never lies strictly below the selector key. This is the interval analogue of the
anchor stability under a single-key insertion at the selector key. The resolution
itself may still change, since the chain replaces the erased keys at and above
`a`. -/
theorem anchor_insertInterval_below {db : DB} {s : Selector} {a b : Key}
(hkey : s.key = (↑a : WithTop Key)) (hab : a ≤ b) (n : ℕ)
(hn : ∀ i < n, chain a i < b) :
s.anchor (db.insertInterval a b hab n hn) = s.anchor db := by
unfold Selector.anchor
rw [hkey, countP_lt_coe, countP_lt_coe,
countP_lt_insertInterval_of_lt hab (le_refl a) n hn]
/-- Erasing the interval `[a, b)` when the selector is keyed at `a` leaves the
selector's anchor index unchanged: erased keys all lie at or above `a`. This is
the interval analogue of the anchor stability under a single-key erasure at the
selector key. The resolution itself may still change, since keys at and above `a`
are removed. -/
theorem anchor_eraseInterval_below {db : DB} {s : Selector} {a b : Key}
(hkey : s.key = (↑a : WithTop Key)) :
s.anchor (db.eraseInterval (↑a) (↑b)) = s.anchor db := by
unfold Selector.anchor
rw [hkey, countP_lt_coe, countP_lt_coe,
countP_lt_eraseInterval_of_le (x := a) (le_refl a)]
/-- When the selector is keyed at the upper end `b` of the replaced interval, the
interval insertion shifts its anchor by the chain length `n` less the number of
stored keys erased from `[a, b)`: the chain elements replace exactly the erased
keys below `b`. This is the anchor form of the interval perturbation's count
shift, the multi-key analogue of the single-key insertion's `+1`. -/
theorem anchor_insertInterval_self {db : DB} {s : Selector} {a b : Key}
(hkey : s.key = (↑b : WithTop Key)) (hab : a ≤ b) (n : ℕ)
(hn : ∀ i < n, chain a i < b) :
s.anchor (db.insertInterval a b hab n hn) =
s.anchor db + n - (db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by
have hsplit : db.keys.countP (fun k => decide (k < b)) =
(db.keys.filter (fun k => decide (k < a))).length +
(db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by
rw [countP_lt_eq_add_filter db.keys db.nodup hab, List.countP_eq_length_filter]
unfold Selector.anchor
rw [hkey, countP_lt_coe, countP_lt_coe, countP_lt_insertInterval_self hab hn, hsplit]
push_cast
ring
/-- When the selector is keyed at the upper end `b` of the erased interval, the
interval erasure lowers its anchor by the number of erased keys in `[a, b)`:
that is exactly the multi-key analogue of the single-key erasure's `-1`. -/
theorem anchor_eraseInterval_self {db : DB} {s : Selector} {a b : Key}
(hkey : s.key = (↑b : WithTop Key)) (hab : a ≤ b) :
s.anchor (db.eraseInterval (↑a) (↑b)) =
s.anchor db - (db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by
have hsplit : db.keys.countP (fun k => decide (k < b)) =
(db.keys.filter (fun k => decide (k < a))).length +
(db.keys.filter (fun k => decide (a ≤ k ∧ k < b))).length := by
rw [countP_lt_eq_add_filter db.keys db.nodup hab, List.countP_eq_length_filter]
unfold Selector.anchor
rw [hkey, countP_lt_coe, countP_lt_coe, countP_lt_eraseInterval_self hab, hsplit]
push_cast
ring
/-- For a positive integer, `toNat` commutes with subtracting one. -/
theorem toNat_sub_one {z : ℤ} (h : 1 ≤ z) : (z - 1).toNat = z.toNat - 1 := by
rw [← Int.toNat_of_nonneg (show (0 : ℤ) ≤ z by omega)]
exact Int.toNat_sub z.toNat 1
/-- For an integer at least two, `toNat` commutes with subtracting two. -/
theorem toNat_sub_two {z : ℤ} (h : 2 ≤ z) : (z - 2).toNat = z.toNat - 2 := by
have hz : (↑z.toNat : ℤ) = z := Int.toNat_of_nonneg (by omega)
conv_lhs => rw [← hz]
exact Int.toNat_sub z.toNat 2
/-- When a nonnegative anchor resolves to a stored key, the anchor's index holds
that key. -/
theorem getElem?_anchor_eq_some {db : DB} {s : Selector} {a : Key}
(h0 : 0 ≤ s.anchor db) (h : resolve db s = (↑a : WithTop Key)) :
db.keys[(s.anchor db).toNat]? = some a := by
rw [resolve_eq_ite, ite_eq_right (not_lt.mpr h0)] at h
cases hg : db.keys[(s.anchor db).toNat]? with
| none => rw [hg] at h; exact absurd h (by simp)
| some k => rw [hg] at h; rw [WithTop.coe_inj.mp h]
/-- A selector keyed at `⊤` with a positive offset resolves to `⊤`: it targets
past every stored key, whatever the database. -/
theorem resolve_eq_top_of_key_eq_top {db : DB} {s : Selector}
(h : s.key = (⊤ : WithTop Key)) (hoff : 1 ≤ s.offset) :
resolve db s = (⊤ : WithTop Key) := by
have h0 : 0 ≤ s.anchor db := by rw [anchor_of_top h]; omega
refine resolve_eq_top h0 ?_
rw [anchor_toNat_eq hoff, h, countP_lt_top]
have hpos' : 0 ≤ (s.offset - 1).toNat := Nat.zero_le _
omega
/-- A selector keyed at the empty key with offset zero resolves to the empty
key: it targets the start of the key space. -/
theorem resolve_eq_nil_of_key_eq_nil {db : DB} {s : Selector}
(h : s.key = (([] : Key) : WithTop Key)) (hoff : s.offset = 0) :
resolve db s = (↑([] : Key) : WithTop Key) := by
refine resolve_eq_nil ?_
rw [anchor_of_nil h, hoff]
omega
/-- Inserting the chain of `[a, b)` lets a selector keyed at `a` land on any
chosen chain element: with offset `1 + i` its anchor is the stored-key count
below `a` plus `i`, which the insertion fills with `chain a i`. This is the
resolve-level content of the interval perturbation, the multi-key analogue of a
single-key insertion placing the targeted key. -/
theorem resolve_insertInterval_chain {db : DB} {s : Selector} {a b : Key}
(hkey : s.key = (↑a : WithTop Key)) (hab : a ≤ b) {n i : ℕ}
(hn : ∀ j < n, chain a j < b) (hoff : s.offset = (i : ℤ) + 1) (hi : i < n) :
resolve (db.insertInterval a b hab n hn) s = (↑(chain a i) : WithTop Key) := by
have hanc : s.anchor (db.insertInterval a b hab n hn) =
(((db.keys.filter (fun k => decide (k < a))).length + i : ℕ) : ℤ) := by
unfold Selector.anchor
rw [hkey, countP_lt_coe, countP_lt_insertInterval_of_lt hab (le_refl a) n hn, hoff,
List.countP_eq_length_filter]
push_cast
ring
have htoNat : (s.anchor (db.insertInterval a b hab n hn)).toNat =
(db.keys.filter (fun k => decide (k < a))).length + i := by
rw [hanc]; exact Int.toNat_natCast _
have h0 : 0 ≤ s.anchor (db.insertInterval a b hab n hn) := by rw [hanc]; positivity
have hlt : (s.anchor (db.insertInterval a b hab n hn)).toNat <
(db.insertInterval a b hab n hn).keys.length := by
rw [htoNat, DB.insertInterval_keys, List.length_append, List.length_append, List.length_map,
List.length_range]
have hfb : (db.keys.filter (fun k => decide (k < b))).length ≤ db.keys.length := by
simpa using List.length_filter_le (fun k => decide (k < b)) db.keys
have hfa : (db.keys.filter (fun k => decide (k < a))).length ≤ db.keys.length := by
simpa using List.length_filter_le (fun k => decide (k < a)) db.keys
omega
rw [resolve_eq_getElem h0 hlt]
apply WithTop.coe_inj.mpr
have h2 : (db.insertInterval a b hab n hn).keys[
(s.anchor (db.insertInterval a b hab n hn)).toNat]? = some (chain a i) := by
rw [htoNat]
exact getElem?_insertInterval_chain hab hn hi
rw [List.getElem?_eq_getElem hlt] at h2
exact Option.some.inj h2
/-!
## Bounding a resolution by the selector's key
These bounds pin the value `resolve` can produce between the count of stored keys
below the selector key and the successor chain. They are the database-relative
half of the footprint specification and are stated here beside `resolve`.
-/
/-- A selector keyed at a genuine `k₀` with positive offset can only resolve to
keys at or above `chain k₀ (offset - 1)`: the anchor puts `off - 1` chain steps
between the count of keys below `k₀` and the resolved key. -/
theorem chain_le_of_resolve {s : Selector} {k₀ k : Key}
(hk : s.key = (↑k₀ : WithTop Key)) (hoff : 1 ≤ s.offset) {db : DB}
(h : resolve db s = (↑k : WithTop Key)) :
chain k₀ (s.offset - 1).toNat ≤ k := by
have h0 : 0 ≤ s.anchor db := by
rw [anchor_of_coe hk]
have := Int.natCast_nonneg (db.keys.countP (fun x => decide (x < k₀)))
omega
have hsome : db.keys[(s.anchor db).toNat]? = some k := getElem?_anchor_eq_some h0 h
obtain ⟨hidx, hget⟩ := List.getElem?_eq_some_iff.mp hsome
have ht : (s.anchor db).toNat =
db.keys.countP (fun x => decide (x < k₀)) + (s.offset - 1).toNat := by
rw [anchor_toNat_eq hoff, hk, countP_lt_coe]
have hidx' : db.keys.countP (fun x => decide (x < k₀)) + (s.offset - 1).toNat <
db.keys.length := by omega
have hstep := chain_le_getElem_countP db.sorted hidx'
have hsome' : db.keys[db.keys.countP (fun x => decide (x < k₀)) +
(s.offset - 1).toNat]? = some k := by
rw [← ht]
exact hsome
exact hstep.trans_eq (List.getElem?_eq_some_iff.mp hsome').2
/-- A selector keyed at a genuine `k₀` with a nonpositive offset can only
resolve to the empty key or to a key whose `-offset`-fold successor chain stays
strictly below `k₀`: the resolved key sits `-offset` indices below the count of
keys strictly below `k₀`, and a strictly increasing list has a distinct key
between consecutive chain elements. -/
theorem eq_nil_or_chain_lt_of_resolve {s : Selector} {k₀ k : Key}
(hk : s.key = (↑k₀ : WithTop Key)) (hoff : s.offset ≤ 0) {db : DB}
(h : resolve db s = (↑k : WithTop Key)) :
k = [] ∨ chain k (-s.offset).toNat < k₀ := by
by_cases hneg : s.anchor db < 0
· refine Or.inl ?_
exact WithTop.coe_inj.mp (h.symm.trans (resolve_eq_nil hneg))
· have h0 : 0 ≤ s.anchor db := by omega
have hsome : db.keys[(s.anchor db).toNat]? = some k := getElem?_anchor_eq_some h0 h
obtain ⟨hidx, hget⟩ := List.getElem?_eq_some_iff.mp hsome
have hCint : (db.keys.countP (fun x => decide (x < k₀)) : ℤ) =
((s.anchor db).toNat : ℤ) + ((-s.offset).toNat : ℤ) + 1 := by
have hanc : (s.anchor db : ℤ) =
(db.keys.countP (fun x => decide (x < k₀)) : ℤ) + s.offset - 1 := anchor_of_coe hk
have hnat : ((s.anchor db).toNat : ℤ) = s.anchor db := Int.toNat_of_nonneg h0
have hn : (((-s.offset).toNat : ℤ)) = -s.offset := Int.toNat_of_nonneg (by omega)
omega
have hC : db.keys.countP (fun x => decide (x < k₀)) =
(s.anchor db).toNat + (-s.offset).toNat + 1 := by omega
have hindex : (s.anchor db).toNat + (-s.offset).toNat < db.keys.length := by
have := List.countP_le_length (p := fun x : Key => decide (x < k₀)) (l := db.keys)
omega
have hle := chain_getElem_le db.sorted (i := (s.anchor db).toNat)
(j := (-s.offset).toNat) hidx hindex
have hltC : (s.anchor db).toNat + (-s.offset).toNat <
db.keys.countP (fun x => decide (x < k₀)) := by omega
have hlt : db.keys[(s.anchor db).toNat + (-s.offset).toNat] < k₀ :=
getElem_lt_of_lt_countP db.sorted hindex hltC
rw [hget] at hle
exact Or.inr (lt_of_le_of_lt hle hlt)
end FormalConflictRange