3090 lines
163 KiB
Lean4
3090 lines
163 KiB
Lean4
/-
|
||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
Authors: FormalConflictRange contributors
|
||
-/
|
||
|
||
import FormalConflictRange.Algorithm
|
||
|
||
/-!
|
||
# Conflict-range constraints: a prototype
|
||
|
||
This file prototypes collecting the rank/difference information a transaction's
|
||
reads and results expose, and reading a candidate minimal set of conflict ranges
|
||
back off it. It is built on top of the existing development (`cnt`, `resolve`,
|
||
`toggle`, `bridge`, `emptyValue`, `completeRange`); no existing definition is
|
||
changed.
|
||
|
||
## The rank function
|
||
|
||
For a database `db` and `w : WithTop Key`, let `ρ db w = cnt db w`, the number of
|
||
stored keys strictly below `w`. The key identity (`Empty.lean`,
|
||
`cnt_eq_add_between`) is that for `w₁ ≤ w₂`
|
||
|
||
`ρ w₂ − ρ w₁ = # { stored keys in [w₁, w₂) }`.
|
||
|
||
A difference `ρ hi − ρ lo` is therefore *fixed* by agreement on any conflict
|
||
range containing `[lo, hi)`; this is the `supp`ort idea.
|
||
|
||
## Difference constraints
|
||
|
||
A `Constraint` is one inequality `ρ hi − ρ lo ≤ bound`. A `ConstraintSystem` is a
|
||
list of them: the information one read (or a whole transaction) exposes. It is a
|
||
conjunction of linear inequalities in the rank variables, i.e. a difference
|
||
constraint system. Disjunctions enter only through the empty result's `ψ`; the
|
||
existing empty theory resolves those by a read-level branch, so the prototype
|
||
carries a `ConstraintDNF`, a list of conjunctive systems paired with `AnyHolds`, one
|
||
alternative per branch. `ConstraintDNF` must not be handed to `hullSupports`, which
|
||
conjoins its argument.
|
||
|
||
## What is proved
|
||
|
||
* `constraint_eval_toggle` — a flip at `k` changes `ρ hi − ρ lo` by `±1` exactly
|
||
on the support and by `0` off it.
|
||
* `system_holdsAt_of_agree` — databases agreeing on a range covering every
|
||
support satisfy the system alike; this is soundness as support containment.
|
||
* `system_holdsAt_of_supports_hull`, `readOff_sound` — support containment gives
|
||
soundness at the `hullSupports` read-off.
|
||
* `cap_holdsAt`, `shortGap_not_le` — finite capacities are tautologies, and the
|
||
computed short-gap selector makes the second empty disjunct vacuous.
|
||
* `necessary_of_tight_support` — a key in the support of a *tight* constraint
|
||
whose flip violates the system is necessary, given `Characterizes`.
|
||
* `minimal_of_characterizing_system` / `minimal_of_tight_supports` — the direct
|
||
assembly: a characterizing system whose read-off `hullSupports` has every key
|
||
witnessed by a violated flip is minimal, with no boundary/interior/boundary
|
||
decomposition. `unique_minimal_of_characterizing_system` is the up-to-set form.
|
||
* `CharacterizesDNF`, `necessary_of_tight_alt`, `minimal_of_characterizing_DNF` —
|
||
the disjunctive assembly for the actual collector: necessity breaks *every*
|
||
alternative, and `characterizesDNF_collectOf` discharges the characterization from
|
||
`preciseCharacterizes`.
|
||
* `sound_of_live_supports`, `minimal_of_live_supports_DNF` — the soundness cover over
|
||
the alternatives live at each *compatible* database, not over globally feasible
|
||
alternatives. The latter (`sound_of_feasible_supports`, kept for the record) is
|
||
false on the empty read, because the `nilDb` alternative is feasible via the empty
|
||
database with full support (`emptyConstraints_cover_false_on_readPinned`).
|
||
* `Observable`, `sound_of_observable`, `observable_of_sound`, `minimal_of_observable_DNF`
|
||
— the *exact* soundness criterion. Support containment (feasible or live) is only
|
||
sufficient: the crossed always-empty read `crossRead` has empty minimal range yet no
|
||
alternative with empty support (`crossRead_support_cover_false`), because a change of
|
||
branch compensates. Soundness is observability of the disjunction on `R`.
|
||
* `characterizesDNF_emptyConstraints`, `minimal_emptyValue_via_observable` — the
|
||
validation: the DNF assembly *reproduces* the empty theory, deriving `emptyValue`'s
|
||
minimality from observability plus the witness oracle rather than the dedicated empty
|
||
proofs.
|
||
* `Observable.mono`, `observable_conjDNFs`, `observable_append_of_support`,
|
||
`observable_collectOf_of_boundary`, `observable_collectOf_of_emptyValue` — observability
|
||
is a congruence for the collector's construction, so the general soundness obligation
|
||
reduces to the required boundary reads' *proven* empty theory plus support containment
|
||
of the bracket/core middle.
|
||
* `hullRanges`, `collectReadOff`, `sound_collectReadOff` — the concrete computable
|
||
read-off (hull of the required boundary `emptyValue`s and the bracket/core supports)
|
||
and its **unconditional** soundness, for any realizable read/result with nonempty
|
||
ascending reported keys. It validates on the old counterexample `ceRead`, whose
|
||
`completeRange` was unsound.
|
||
* `sound_congr_toSet`, `minimal_congr_toSet`, `minimal_collectReadOff_of_subrange` —
|
||
soundness/minimality depend on a range only through its key set, so the read-off is
|
||
minimal once it is a subrange of a known-minimal range. The sole residual is the
|
||
containment `read-off ⊆ minimal`, i.e. that the hull does not overshoot (the per-key
|
||
flip witnesses in hull form).
|
||
* `hull_subrange_of`, `subrange_hullRanges_of`, `subrange_collectReadOff_iff` — the hull
|
||
is the least upper bound (`hull_subrange_of`), so the containment residual splits into
|
||
per-constituent containments: each boundary `emptyValue` and bracket/core support must
|
||
lie in the minimal range.
|
||
* `minimal_collectReadOff_of_necessary`, `minimal_collectReadOff_of_necessaryRange` — the
|
||
necessity route, the form the main development's per-key/interval necessity theorems
|
||
target: since soundness is unconditional, minimality follows from the read-off's keys
|
||
(equivalently endpoints, by convexity) being necessary.
|
||
* `minimal_inter_of_sound_of_minimal` — meet closure: intersecting a sound range with a
|
||
known-minimal one is minimal. This repairs an over-collecting candidate without proving
|
||
it contained in the minimal range; `minimal_inter_collectReadOff_of_minimal` is the
|
||
wrapper for the read-off, and `minimal_inter_collectReadOff_truncated_fwd`/`_rev` (+ the
|
||
overshoot variant) apply it to the truncated branch, where the read-off alone overshoots
|
||
(`Counterexample.ct_collectReadOff_not_minimal`).
|
||
* `reported_keys_bounds`, `subrange_collectReadOff_canonical`,
|
||
`collectReadOff_toSet_eq_canonical`, `minimal_collectReadOff_canonical` — the
|
||
canonical-region containment, proved. For a complete nonempty realizable pair in
|
||
`1 ≤ begin.offset ∧ end_.offset ≤ 1`, every reported key lies in `[begin.key, end.key)`,
|
||
so each boundary `emptyValue` and bracket/core support is contained in the canonical
|
||
range; the read-off is contained in it and, being sound, shares its key set. Thus
|
||
`collectReadOff` is minimal there, extending end-to-end minimality beyond `ceRead`.
|
||
* `subrange_collectReadOff_topRange`, `minimal_collectReadOff_topRange` — the
|
||
overshoot-region containment, proved. For a complete nonempty realizable pair in
|
||
`1 ≤ begin.offset ∧ 2 ≤ end.offset` with every reported key below `end.key`, the
|
||
algorithm's value is the unbounded top `[begin.key, ⊤)` (both traversals), and the
|
||
read-off is contained in it: the bracket/core supports and the lower boundary all
|
||
start at or above `begin.key`, while the upper boundary read's contribution does too
|
||
because its begin selector `⟨keyAfter kₙ, 1⟩` is keyed above the reported last key.
|
||
This is the region outside the canonical block, the other half of `completeValue`'s
|
||
non-hull answers.
|
||
* `collectReadOff_toSet_eq_topRange` — the overshoot counterpart of
|
||
`collectReadOff_toSet_eq_canonical`: the read-off and the top range share their key set.
|
||
* `minimal_collectReadOff_complete_general` — packages the two non-hull complete regions
|
||
(canonical and overshoot) behind one disjunction; the crossed selector order is the only
|
||
uncertified complete region.
|
||
* `collectOf_holds_of_compatible` — the precise general collector's forward half: a
|
||
compatible database satisfies the collected DNF, for every traversal, truncation,
|
||
and offset pair.
|
||
* `preciseCharacterizes` — precise collection, fully proved: `collectOf`, read at the
|
||
ascending reported keys, characterizes compatibility (with the limit factored out).
|
||
The reverse half is `precise_reverse`, whose core step `midEq_of_coreConstraints`
|
||
recovers the middle from storedness plus the total.
|
||
* `intervalCard_headD_getLastD_eq_capSum` — sorted endpoints telescope.
|
||
|
||
The read-off is `hullSupports`, a computable fold over the supports, and the
|
||
`#eval` demo at the end exercises it.
|
||
-/
|
||
|
||
namespace FormalConflictRange
|
||
|
||
open RangeRead
|
||
|
||
/-! ## Count changes under a single-key flip -/
|
||
|
||
/-- Erasing a stored key `k` lowers every count below a bound `w > k` by one and
|
||
leaves the others unchanged, as an integer identity. -/
|
||
theorem cnt_erase_eq (db : DB) (k : Key) (hmem : k ∈ db.keys) (w : WithTop Key) :
|
||
(cnt (db.erase k) w : ℤ) =
|
||
(cnt db w : ℤ) - (if (k : WithTop Key) < w then 1 else 0) := by
|
||
unfold cnt
|
||
rw [DB.erase_keys]
|
||
have h := List.countP_erase (fun y : Key => decide ((y : WithTop Key) < w)) db.keys k
|
||
by_cases hw : (k : WithTop Key) < w
|
||
· rw [ite_eq_left hw]
|
||
have hc : (if k ∈ db.keys ∧ decide ((k : WithTop Key) < w) = true then (1:ℕ) else 0) = 1 := by
|
||
simp [hmem, hw]
|
||
rw [hc] at h
|
||
have h1 : 1 ≤ db.keys.countP (fun y : Key => decide ((y : WithTop Key) < w)) :=
|
||
countP_pos_of_mem hmem (by simp [hw])
|
||
rw [h]; push_cast [Nat.cast_sub h1]; ring
|
||
· rw [ite_eq_right hw]
|
||
have hc : (if k ∈ db.keys ∧ decide ((k : WithTop Key) < w) = true then (1:ℕ) else 0) = 0 := by
|
||
simp [hw]
|
||
rw [hc] at h
|
||
rw [h]; simp
|
||
|
||
/-- Inserting an absent key `k` raises every count below a bound `w > k` by one
|
||
and leaves the others unchanged, as an integer identity. -/
|
||
theorem cnt_insert_eq (db : DB) (k : Key) (hk : k ∉ db.keys) (w : WithTop Key) :
|
||
(cnt (db.insert k hk) w : ℤ) =
|
||
(cnt db w : ℤ) + (if (k : WithTop Key) < w then 1 else 0) := by
|
||
unfold cnt
|
||
rw [DB.insert_keys, orderedInsert_eq_insertIdx db.sorted hk]
|
||
by_cases hw : (k : WithTop Key) < w
|
||
· rw [ite_eq_left hw]
|
||
rw [countP_insertIdx_of_lt db.sorted (by simpa using hw)]
|
||
push_cast; ring
|
||
· rw [ite_eq_right hw]
|
||
rw [countP_insertIdx (fun y : Key => decide ((y : WithTop Key) < w)) _ k (by simp [hw])]
|
||
simp
|
||
|
||
/-! ## The constraint system -/
|
||
|
||
/-- One difference constraint on the rank function: `ρ hi − ρ lo ≤ bound`. The
|
||
orientation is raw; `supp` takes the interval between the endpoints, which is
|
||
what the bound's sign is relative to. -/
|
||
structure Constraint where
|
||
lo : WithTop Key
|
||
hi : WithTop Key
|
||
bound : ℤ
|
||
deriving DecidableEq, Repr
|
||
|
||
/-- The interval a constraint constrains: `[min lo hi, max lo hi)`. Membership
|
||
here flips the difference `ρ hi − ρ lo`, so this is exactly the interval a sound
|
||
range must contain to see the constraint. -/
|
||
def Constraint.supp (c : Constraint) : ConflictRange :=
|
||
⟨min c.lo c.hi, max c.lo c.hi⟩
|
||
|
||
/-- The value of the constraint at a database: `ρ hi − ρ lo`. -/
|
||
def Constraint.eval (c : Constraint) (db : DB) : ℤ :=
|
||
(cnt db c.hi : ℤ) - (cnt db c.lo : ℤ)
|
||
|
||
/-- A constraint holds at a database when its value is within its bound. -/
|
||
def Constraint.HoldsAt (c : Constraint) (db : DB) : Prop := c.eval db ≤ c.bound
|
||
|
||
/-- `ρ hi − ρ lo ≤ n`. -/
|
||
def Constraint.le (lo hi : WithTop Key) (n : ℤ) : Constraint := ⟨lo, hi, n⟩
|
||
|
||
/-- `ρ hi − ρ lo ≥ n`, stated as `ρ lo − ρ hi ≤ -n`. -/
|
||
def Constraint.ge (lo hi : WithTop Key) (n : ℤ) : Constraint := ⟨hi, lo, -n⟩
|
||
|
||
/-- A finite-range capacity constraint `ρ hi − ρ lo ≤ n`, for `n` the (finite)
|
||
cardinality of `[lo, hi)`. The finiteness is the hypothesis `hfin`, so this is
|
||
never confused with an infinite range: an infinite interval contributes no cap.
|
||
The cap is a tautology at every database (`Constraint.cap_holdsAt`), but it is the
|
||
*tightness threshold* the empty theory's branch uses: when `n` is below the gap
|
||
`e − b`, the corresponding disjunct is unsatisfiable for every database. -/
|
||
def Constraint.cap (lo hi : WithTop Key) (n : ℕ) : Constraint :=
|
||
⟨lo, hi, (n : ℤ)⟩
|
||
|
||
/-- The capacity constraint holds at every database: no database stores more keys
|
||
in `[lo, hi)` than the finite cardinality `n` of the interval. When `hi ≤ lo` the
|
||
interval is empty, `n = 0`, and the difference is nonpositive. -/
|
||
theorem Constraint.cap_holdsAt (db : DB) {lo hi : WithTop Key} {n : ℕ}
|
||
(hfin : intervalCard lo hi = (n : ℕ∞)) :
|
||
(Constraint.cap lo hi n).HoldsAt db := by
|
||
rcases le_total lo hi with hle | hle
|
||
· have h := cnt_sub_le_intervalCard db hle
|
||
rw [hfin] at h
|
||
have hd : cnt db hi - cnt db lo ≤ n := by exact_mod_cast h
|
||
change ((cnt db hi : ℤ) - (cnt db lo : ℤ)) ≤ (n : ℤ)
|
||
rw [← Nat.cast_sub (cnt_le_cnt_of_le db hle)]
|
||
exact_mod_cast hd
|
||
· have hzero : intervalCard lo hi = 0 := intervalCard_eq_zero_of_le hle
|
||
rw [hzero] at hfin
|
||
have hn : n = 0 := by simpa using hfin.symm
|
||
subst hn
|
||
have hmono : cnt db hi ≤ cnt db lo := cnt_le_cnt_of_le db hle
|
||
change ((cnt db hi : ℤ) - (cnt db lo : ℤ)) ≤ (0 : ℤ)
|
||
omega
|
||
|
||
/-- A family of constraints: what a read or a transaction exposes in difference
|
||
form. -/
|
||
abbrev ConstraintSystem := List Constraint
|
||
|
||
/-- A system holds at a database when every constraint in it does; a conjunction,
|
||
i.e. one difference constraint system. -/
|
||
def SystemHoldsAt (S : ConstraintSystem) (db : DB) : Prop := ∀ c ∈ S, c.HoldsAt db
|
||
|
||
/-- A system is satisfiable when some database satisfies it. -/
|
||
def Feasible (S : ConstraintSystem) : Prop := ∃ db : DB, SystemHoldsAt S db
|
||
|
||
/-- A database violates a system when it fails some constraint. -/
|
||
def SystemViolated (S : ConstraintSystem) (db : DB) : Prop := ∃ c ∈ S, ¬ c.HoldsAt db
|
||
|
||
/-- A disjunction of conjunctive difference systems, in disjunctive normal form:
|
||
each element of the list is one alternative's conjunction. Unlike `ConstraintSystem`
|
||
this is *not* a conjunction, so it must never be fed to `hullSupports`, which would
|
||
conjoin the alternatives. It is the carrier for a read whose observation is a
|
||
disjunction, such as an empty result's `ψ`. -/
|
||
abbrev ConstraintDNF := List ConstraintSystem
|
||
|
||
/-- The DNF holds at a database when some alternative's conjunction does. This is a
|
||
union of the alternatives' feasible sets, not their intersection. -/
|
||
def AnyHolds (D : ConstraintDNF) (db : DB) : Prop := ∃ S ∈ D, SystemHoldsAt S db
|
||
|
||
/-- A DNF is satisfiable when some database satisfies some alternative. -/
|
||
def FeasibleDNF (D : ConstraintDNF) : Prop := ∃ db : DB, AnyHolds D db
|
||
|
||
/-- **A flip off the support leaves the rank difference unchanged.** If `k` is
|
||
outside `[min lo hi, max lo hi)`, both endpoints' counts move together, so
|
||
`ρ hi − ρ lo` is fixed. This is the complement of `constraint_eval_toggle`. -/
|
||
theorem constraint_eval_toggle_eq_of_not_mem {db : DB} {k : Key} {c : Constraint}
|
||
(hk : k ∉ c.supp) : c.eval (toggle db k) = c.eval db := by
|
||
rw [Constraint.supp, ConflictRange.mem_iff, not_and_or] at hk
|
||
simp only [Constraint.eval]
|
||
unfold toggle
|
||
by_cases hmem : k ∈ db.keys
|
||
· rw [dite_eq_left hmem, cnt_erase_eq db k hmem c.hi, cnt_erase_eq db k hmem c.lo]
|
||
rcases hk with hmin | hmax
|
||
· -- `k < min lo hi`: both endpoints lie above `k`.
|
||
have hltmin := not_le.mp hmin
|
||
have hhi : (k : WithTop Key) < c.hi := lt_of_lt_of_le hltmin (min_le_right _ _)
|
||
have hlo : (k : WithTop Key) < c.lo := lt_of_lt_of_le hltmin (min_le_left _ _)
|
||
rw [ite_eq_left hhi, ite_eq_left hlo]; ring
|
||
· -- `max lo hi ≤ k`: both endpoints lie at or below `k`.
|
||
have hlemax := not_lt.mp hmax
|
||
have hhi : ¬ ((k : WithTop Key) < c.hi) := not_lt.mpr (le_trans (le_max_right _ _) hlemax)
|
||
have hlo : ¬ ((k : WithTop Key) < c.lo) := not_lt.mpr (le_trans (le_max_left _ _) hlemax)
|
||
rw [ite_eq_right hhi, ite_eq_right hlo]; ring
|
||
· rw [dite_eq_right hmem, cnt_insert_eq db k hmem c.hi, cnt_insert_eq db k hmem c.lo]
|
||
rcases hk with hmin | hmax
|
||
· have hltmin := not_le.mp hmin
|
||
have hhi : (k : WithTop Key) < c.hi := lt_of_lt_of_le hltmin (min_le_right _ _)
|
||
have hlo : (k : WithTop Key) < c.lo := lt_of_lt_of_le hltmin (min_le_left _ _)
|
||
rw [ite_eq_left hhi, ite_eq_left hlo]; ring
|
||
· have hlemax := not_lt.mp hmax
|
||
have hhi : ¬ ((k : WithTop Key) < c.hi) := not_lt.mpr (le_trans (le_max_right _ _) hlemax)
|
||
have hlo : ¬ ((k : WithTop Key) < c.lo) := not_lt.mpr (le_trans (le_max_left _ _) hlemax)
|
||
rw [ite_eq_right hhi, ite_eq_right hlo]; ring
|
||
|
||
/-- **A flip changes a rank difference exactly on the support.** Flipping the
|
||
membership of `k` in `db` changes `ρ hi − ρ lo` by `±1` when `k` lies in the open
|
||
support interval `[min lo hi, max lo hi)` and by `0` otherwise. -/
|
||
theorem constraint_eval_toggle {db : DB} {k : Key} {c : Constraint} (hk : k ∈ c.supp) :
|
||
(c.eval (toggle db k) = c.eval db + 1) ∨ (c.eval (toggle db k) = c.eval db - 1) := by
|
||
rw [Constraint.supp, ConflictRange.mem_iff] at hk
|
||
obtain ⟨hk1, hk2⟩ := hk
|
||
simp only [Constraint.eval]
|
||
unfold toggle
|
||
by_cases hmem : k ∈ db.keys
|
||
· rw [dite_eq_left hmem, cnt_erase_eq db k hmem c.hi, cnt_erase_eq db k hmem c.lo]
|
||
rcases le_total c.lo c.hi with hle | hle
|
||
· have hki : (k : WithTop Key) < c.hi := by rwa [max_eq_right hle] at hk2
|
||
have hkj : c.lo ≤ (k : WithTop Key) := by rwa [min_eq_left hle] at hk1
|
||
rw [ite_eq_left hki, ite_eq_right (not_lt.mpr hkj)]; right; ring
|
||
· have hki : (k : WithTop Key) < c.lo := by rwa [max_eq_left hle] at hk2
|
||
have hkj : c.hi ≤ (k : WithTop Key) := by rwa [min_eq_right hle] at hk1
|
||
rw [ite_eq_right (not_lt.mpr hkj), ite_eq_left hki]; left; ring
|
||
· rw [dite_eq_right hmem, cnt_insert_eq db k hmem c.hi, cnt_insert_eq db k hmem c.lo]
|
||
rcases le_total c.lo c.hi with hle | hle
|
||
· have hki : (k : WithTop Key) < c.hi := by rwa [max_eq_right hle] at hk2
|
||
have hkj : c.lo ≤ (k : WithTop Key) := by rwa [min_eq_left hle] at hk1
|
||
rw [ite_eq_left hki, ite_eq_right (not_lt.mpr hkj)]; left; ring
|
||
· have hki : (k : WithTop Key) < c.lo := by rwa [max_eq_left hle] at hk2
|
||
have hkj : c.hi ≤ (k : WithTop Key) := by rwa [min_eq_right hle] at hk1
|
||
rw [ite_eq_right (not_lt.mpr hkj), ite_eq_left hki]; right; ring
|
||
|
||
/-! ## Support containment gives soundness -/
|
||
|
||
/-- **A constraint whose support lies inside a range is seen by that range.**
|
||
Two databases agreeing on `R` evaluate the constraint equally when `supp c ⊆ R`. -/
|
||
theorem constraint_eval_eq_of_agree {R : ConflictRange} {db₁ db₂ : DB}
|
||
(h : AgreeOn R db₁ db₂) {c : Constraint} (hsub : Subrange c.supp R) :
|
||
c.eval db₁ = c.eval db₂ := by
|
||
have hkeys : c.supp.keys db₁ = c.supp.keys db₂ := keys_eq_of_subrange_agree hsub h
|
||
rcases le_total c.lo c.hi with hle | hle
|
||
· -- `lo ≤ hi`: `ρ hi − ρ lo` counts exactly the support's stored keys.
|
||
have hsupp : ∀ db : DB, c.supp.keys db = between db c.lo c.hi := by
|
||
intro db
|
||
unfold ConflictRange.keys between Constraint.supp
|
||
rw [min_eq_left hle, max_eq_right hle]
|
||
have hb : ∀ db : DB, (cnt db c.lo : ℤ) +
|
||
((c.supp.keys db).length : ℤ) = (cnt db c.hi : ℤ) := by
|
||
intro db
|
||
rw [hsupp db, cnt_eq_add_between db hle]
|
||
push_cast; ring
|
||
have hb₁ := hb db₁
|
||
have hb₂ := hb db₂
|
||
rw [hkeys] at hb₁
|
||
simp only [Constraint.eval]
|
||
omega
|
||
· -- `hi ≤ lo`: the roles reverse, and the same count argument applies to `-`.
|
||
have hsupp : ∀ db : DB, c.supp.keys db = between db c.hi c.lo := by
|
||
intro db
|
||
unfold ConflictRange.keys between Constraint.supp
|
||
rw [min_eq_right hle, max_eq_left hle]
|
||
have hb : ∀ db : DB, (cnt db c.hi : ℤ) +
|
||
((c.supp.keys db).length : ℤ) = (cnt db c.lo : ℤ) := by
|
||
intro db
|
||
rw [hsupp db, cnt_eq_add_between db hle]
|
||
push_cast; ring
|
||
have hb₁ := hb db₁
|
||
have hb₂ := hb db₂
|
||
rw [hkeys] at hb₁
|
||
simp only [Constraint.eval]
|
||
omega
|
||
|
||
/-- **Support containment gives soundness.** Two databases agreeing on a range
|
||
`R` that contains every support in `S` satisfy `S` alike. -/
|
||
theorem system_holdsAt_of_agree {S : ConstraintSystem} {R : ConflictRange} {db₁ db₂ : DB}
|
||
(h : AgreeOn R db₁ db₂) (hsub : ∀ c ∈ S, Subrange c.supp R) :
|
||
(SystemHoldsAt S db₁ ↔ SystemHoldsAt S db₂) := by
|
||
constructor <;> intro hh c hc
|
||
· change c.eval db₂ ≤ c.bound
|
||
rw [← constraint_eval_eq_of_agree h (hsub c hc)]
|
||
exact hh c hc
|
||
· change c.eval db₁ ≤ c.bound
|
||
rw [constraint_eval_eq_of_agree h (hsub c hc)]
|
||
exact hh c hc
|
||
|
||
/-- The hull of the supports of a system: a candidate sound conflict range. The
|
||
reported interval, when present, is added as an explicit component by the caller.
|
||
Structural recursion keeps the induction principle available; the hull operation
|
||
is associative and commutative, so the order does not matter. -/
|
||
def hullSupports : ConstraintSystem → Option ConflictRange
|
||
| [] => none
|
||
| c :: S => some (match hullSupports S with
|
||
| none => c.supp
|
||
| some R => hull R c.supp)
|
||
|
||
/-- The hull of the supports of every system in a transaction. -/
|
||
def transactionRanges (reads : List ConstraintSystem) : Option ConflictRange :=
|
||
reads.foldl (fun acc S => some (match acc with
|
||
| none => hullSupports S |>.getD ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩
|
||
| some R => hull R (hullSupports S |>.getD
|
||
⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩))) none
|
||
|
||
/-! ## Soundness at the read-off range -/
|
||
|
||
/-- `hullSupports` is `none` only for the empty system. -/
|
||
theorem hullSupports_eq_none_iff {S : ConstraintSystem} :
|
||
hullSupports S = none ↔ S = [] := by
|
||
constructor
|
||
· intro h
|
||
cases S with
|
||
| nil => rfl
|
||
| cons d S => simp only [hullSupports, Option.some_ne_none] at h
|
||
· rintro rfl; rfl
|
||
|
||
/-- Every support of a system is contained in the hull of its supports, so the
|
||
read-off range is sound for the system. -/
|
||
theorem subrange_supp_hullSupports {S : ConstraintSystem} {R : ConflictRange}
|
||
(hR : hullSupports S = some R) : ∀ c ∈ S, Subrange c.supp R := by
|
||
induction S generalizing R with
|
||
| nil => intro c hc; exact absurd hc (by simp)
|
||
| cons d S ih =>
|
||
intro c hc
|
||
simp only [hullSupports] at hR
|
||
rcases htail : hullSupports S with _ | R'
|
||
· -- the tail is empty, so `d` is the only constraint
|
||
rw [htail] at hR
|
||
have hReq : d.supp = R := Option.some.inj hR
|
||
rw [← hReq]
|
||
rcases List.mem_cons.mp hc with rfl | hc'
|
||
· exact Subrange.refl _
|
||
· rw [hullSupports_eq_none_iff.mp htail] at hc'
|
||
exact absurd hc' (by simp)
|
||
· rw [htail] at hR
|
||
have hReq : hull R' d.supp = R := Option.some.inj hR
|
||
rcases List.mem_cons.mp hc with rfl | hc'
|
||
· rw [← hReq]; exact subrange_hull_right _ _
|
||
· rw [← hReq]
|
||
exact Subrange.trans (ih htail c hc') (subrange_hull_left _ _)
|
||
|
||
/-- **The read-off range is sound.** If `hullSupports S = some R`, then any two
|
||
databases agreeing on `R` satisfy `S` alike, because every support is inside `R`.
|
||
This is `Sound` at the level of the constraint system. -/
|
||
theorem system_holdsAt_of_supports_hull {S : ConstraintSystem} {R : ConflictRange}
|
||
(hR : hullSupports S = some R) {db₁ db₂ : DB} (h : AgreeOn R db₁ db₂) :
|
||
(SystemHoldsAt S db₁ ↔ SystemHoldsAt S db₂) :=
|
||
system_holdsAt_of_agree h (subrange_supp_hullSupports hR)
|
||
|
||
/-! ## Finite-capacity constraints and the branch selector -/
|
||
|
||
/-- The finite-capacity constraint of an interval, if the interval is finite. An
|
||
infinite range contributes nothing. -/
|
||
def Constraint.capOf (lo hi : WithTop Key) : Option Constraint :=
|
||
match intervalCard lo hi with
|
||
| ⊤ => none
|
||
| (n : ℕ) => some (Constraint.cap lo hi n)
|
||
|
||
/-- The capacity constraint of an interval, as a (possibly empty) system. -/
|
||
def capOfSystem (lo hi : WithTop Key) : ConstraintSystem :=
|
||
(Constraint.capOf lo hi).toList
|
||
|
||
/-- The **short-gap selector**: the second disjunct `f ≥ g` of the empty predicate
|
||
is unsatisfiable for every database exactly when the interval `[lo, hi)` holds
|
||
fewer than `g` keys. Computable from the read alone, with no database. -/
|
||
def shortGap (g : ℕ) (lo hi : WithTop Key) : Bool :=
|
||
match intervalCard lo hi with
|
||
| ⊤ => false
|
||
| (n : ℕ) => decide (n < g)
|
||
|
||
/-- **The computed short-gap selector makes the second disjunct vacuous.** If
|
||
`shortGap g lo hi` is `true`, then *no* database stores `g` or more keys in
|
||
`[lo, hi)`. `shortGap` is the universal (read-only) statement `intervalCard < g`;
|
||
each individual database's gap is subordinate to it. -/
|
||
theorem shortGap_not_le (db : DB) {g : ℕ} {lo hi : WithTop Key} (hfin : lo ≤ hi)
|
||
(h : shortGap g lo hi = true) :
|
||
¬ (g ≤ (between db lo hi).length) := by
|
||
unfold shortGap at h
|
||
split at h
|
||
· exact absurd h (by simp)
|
||
· rename_i n hcard
|
||
rw [decide_eq_true_eq] at h
|
||
have hcap := between_length_le_intervalCard (db := db) hfin
|
||
rw [hcard] at hcap
|
||
have hcap' : (between db lo hi).length ≤ n := by
|
||
rw [← ENat.natCast_le_natCast]; exact hcap
|
||
omega
|
||
|
||
/-! ## Collecting the constraints of a read -/
|
||
|
||
/-- The constraints a complete nonempty result exposes, in difference form. `B`
|
||
and `E` are the selector keys, `oB`/`oE` their offsets, `L` the reported keys.
|
||
|
||
The binding directions are chosen from the bridge analysis of the main notes,
|
||
displayed in the single normal form `ρ hi − ρ lo ≤ n` (`ge` is `le` with the
|
||
endpoints swapped and the bound negated):
|
||
|
||
* each reported key `k` is stored: `ρ (keyAfter k) − ρ k ≥ 1`, i.e. the equality
|
||
`ρ (keyAfter k) − ρ k = 1` (the `≤ 1` is free, the interval `[k, keyAfter k)`
|
||
having cardinality one, so the `≥` is the binding half);
|
||
* the reported interval has exactly `|L|` keys: `ρ (keyAfter kₙ) − ρ k₀ = |L|`;
|
||
* each adjacent pair is contiguous: the equality `ρ k_{i+1} − ρ k_i = 1` (given that
|
||
`k_i` is stored, its `≤ 1` half forbids any intermediate key);
|
||
* the window is bracketed: `ρ begin.key − ρ k₀ ≤ 1 − oB` and
|
||
`ρ kₙ − ρ end.key ≤ oE − 2` (the strict inequality `kₙ < resolve end` is `≤ oE−2`);
|
||
* each adjacent reported-key gap carries its finite capacity. -/
|
||
def constraintsOf (r : RangeRead) (L : List Key) : ConstraintSystem :=
|
||
match L with
|
||
| [] => []
|
||
| k₀ :: _ =>
|
||
let kₙ := L.getLastD k₀
|
||
(L.map (fun (k : Key) =>
|
||
Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1)) ++
|
||
[Constraint.le (k₀ : WithTop Key)
|
||
((keyAfter kₙ : Key) : WithTop Key) (L.length : ℤ),
|
||
Constraint.ge (k₀ : WithTop Key)
|
||
((keyAfter kₙ : Key) : WithTop Key) (L.length : ℤ)] ++
|
||
(L.zip (L.drop 1)).flatMap (fun p : Key × Key =>
|
||
[Constraint.le (p.1 : WithTop Key) (p.2 : WithTop Key) 1,
|
||
Constraint.ge (p.1 : WithTop Key) (p.2 : WithTop Key) 1]) ++
|
||
(L.zip (L.drop 1)).flatMap (fun p : Key × Key =>
|
||
capOfSystem (p.1 : WithTop Key) (p.2 : WithTop Key)) ++
|
||
[Constraint.ge r.begin.key k₀ (r.begin.offset - 1)] ++
|
||
[Constraint.le r.end_.key kₙ (r.end_.offset - 2)]
|
||
|
||
/-- The constraints a truncated non-reversed result exposes: a begin-side prefix
|
||
of `constraintsOf`, with the far end dropped. -/
|
||
def truncatedConstraints (r : RangeRead) (L : List Key) : ConstraintSystem :=
|
||
match L with
|
||
| [] => []
|
||
| k₀ :: _ =>
|
||
(L.map (fun (k : Key) =>
|
||
Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1)) ++
|
||
(L.zip (L.drop 1)).flatMap (fun p : Key × Key =>
|
||
[Constraint.le (p.1 : WithTop Key) (p.2 : WithTop Key) 1,
|
||
Constraint.ge (p.1 : WithTop Key) (p.2 : WithTop Key) 1]) ++
|
||
[Constraint.ge r.begin.key k₀ (r.begin.offset - 1)]
|
||
|
||
/-! ## Constraints collected for an empty read
|
||
|
||
For an empty complete result the observation is the disjunction `ψ`, read off
|
||
`resolve` (via `clampIndex`). The complete keys are empty exactly when the begin
|
||
clamped anchor is not below the end clamped anchor, and for arbitrary offsets the
|
||
negation of that strict comparison is the four-way disjunction
|
||
|
||
* `ρ(E) − ρ(B) ≤ oB − oE`, the end anchor not above the begin anchor: the resolved
|
||
window closes because `resolve end ≤ resolve begin`;
|
||
* `ρ(⊤) − ρ(B) ≤ oB − 1`, the begin anchor running past the end: the window is
|
||
empty because `resolve begin = ⊤` (no key to point at);
|
||
* `ρ(B) ≤ 1 − oB ∧ ρ(E) ≤ 1 − oE`, both anchors negative: each selector clamps to
|
||
the empty key, so the begin anchor cannot lie below the end anchor either;
|
||
* `ρ(⊤) ≤ 0`, the empty database: the window is empty vacuously.
|
||
|
||
The first two are the positive-offset disjuncts the empty theory already named;
|
||
the last two are the genuinely nonpositive-offset (and empty-database) cases that
|
||
`resolve`'s clamp introduces. At positive offsets the last two are unsatisfiable
|
||
and the disjunction collapses to the classical pair.
|
||
|
||
The four disjuncts are recorded as alternatives of a `ConstraintDNF`, so an empty
|
||
read can be handled in the same difference language as a nonempty one. Because
|
||
emptiness is a disjunction, the carrier is a *list of alternatives*, not a
|
||
conjunction: the read is empty iff at least one alternative holds, and the minimal
|
||
range is the hull of the support of whichever alternative is live. -/
|
||
|
||
/-- The first empty disjunct: `resolve end ≤ resolve begin`, i.e.
|
||
`ρ(E) − ρ(B) ≤ oB − oE`. -/
|
||
def emptyConstraintEndLe (r : RangeRead) : Constraint :=
|
||
Constraint.le r.begin.key r.end_.key (r.begin.offset - r.end_.offset)
|
||
|
||
/-- The second empty disjunct: `resolve begin = ⊤`, i.e. `ρ(⊤) − ρ(B) ≤ oB − 1`. -/
|
||
def emptyConstraintBeginTop (r : RangeRead) : Constraint :=
|
||
⟨r.begin.key, ⊤, r.begin.offset - 1⟩
|
||
|
||
/-- The third empty disjunct's begin half: the begin anchor is negative, i.e. the
|
||
count below `begin.key` is at most `1 − oB`. -/
|
||
def emptyConstraintBeginNonpos (r : RangeRead) : Constraint :=
|
||
Constraint.le (([] : Key) : WithTop Key) r.begin.key (1 - r.begin.offset)
|
||
|
||
/-- The third empty disjunct's end half: the end anchor is negative, i.e. the count
|
||
below `end_.key` is at most `1 − oE`. -/
|
||
def emptyConstraintEndNonpos (r : RangeRead) : Constraint :=
|
||
Constraint.le (([] : Key) : WithTop Key) r.end_.key (1 - r.end_.offset)
|
||
|
||
/-- The fourth empty disjunct: the database is empty, i.e. `ρ(⊤) ≤ 0`. -/
|
||
def emptyConstraintNilDb (_r : RangeRead) : Constraint :=
|
||
Constraint.le (([] : Key) : WithTop Key) ⊤ 0
|
||
|
||
/-- The four-alternative system of an empty read, the first two a single constraint
|
||
each and the third a two-constraint conjunction. It is *not* a conjunction: the read
|
||
is empty when any member holds, i.e. under `AnyHolds`. The last two alternatives are
|
||
unsatisfiable at positive offsets, so there it reduces to the classical two. -/
|
||
def emptyConstraints (r : RangeRead) : ConstraintDNF :=
|
||
[[emptyConstraintEndLe r],
|
||
[emptyConstraintBeginTop r],
|
||
[emptyConstraintBeginNonpos r, emptyConstraintEndNonpos r],
|
||
[emptyConstraintNilDb r]]
|
||
|
||
/-- `AnyHolds` of the four-alternative empty DNF is the explicit disjunction of its
|
||
alternatives. -/
|
||
theorem anyHolds_emptyConstraints (r : RangeRead) (db : DB) :
|
||
AnyHolds (emptyConstraints r) db ↔
|
||
(emptyConstraintEndLe r).HoldsAt db ∨ (emptyConstraintBeginTop r).HoldsAt db ∨
|
||
((emptyConstraintBeginNonpos r).HoldsAt db ∧ (emptyConstraintEndNonpos r).HoldsAt db) ∨
|
||
(emptyConstraintNilDb r).HoldsAt db := by
|
||
simp [AnyHolds, SystemHoldsAt, emptyConstraints]
|
||
|
||
/-- **Arithmetic core: the negation of the clamped anchor comparison is a four-way
|
||
disjunction.** For integers `a, b` and a natural `n`, `min a.toNat n` is not below
|
||
`min b.toNat n` exactly when `b ≤ a`, or the right clamp `n` is already at most `a`,
|
||
or both `a` and `b` are nonpositive (both clamp to the empty key), or `n` is zero. -/
|
||
theorem not_lt_min_toNat_dnf (a b : ℤ) (n : ℕ) :
|
||
¬ (min a.toNat n < min b.toNat n) ↔
|
||
(b ≤ a) ∨ ((n : ℤ) ≤ a) ∨ (a ≤ 0 ∧ b ≤ 0) ∨ n = 0 := by
|
||
have hA : (a.toNat : ℤ) = max a 0 := Int.toNat_eq_max a
|
||
have hB : (b.toNat : ℤ) = max b 0 := Int.toNat_eq_max b
|
||
constructor
|
||
· intro h
|
||
have hle : min b.toNat n ≤ min a.toNat n := not_lt.mp h
|
||
by_cases ha : a < 0
|
||
· by_cases hn0 : n = 0
|
||
· exact Or.inr (Or.inr (Or.inr hn0))
|
||
· have hA0 : a.toNat = 0 := by omega
|
||
have hminA : min a.toNat n = 0 := by rw [hA0, Nat.zero_min]
|
||
have hminB : min b.toNat n = 0 := by omega
|
||
have hb0 : b ≤ 0 := by
|
||
by_contra hc
|
||
have hbpos : 0 < b := lt_of_not_ge hc
|
||
have hBpos : 0 < b.toNat := by omega
|
||
have : 0 < min b.toNat n := Nat.lt_min.mpr (by omega)
|
||
omega
|
||
exact Or.inr (Or.inr (Or.inl ⟨le_of_lt ha, hb0⟩))
|
||
· push Not at ha
|
||
by_cases hna : (n : ℤ) ≤ a
|
||
· exact Or.inr (Or.inl hna)
|
||
· push Not at hna
|
||
have haN : a.toNat < n := by omega
|
||
have hminA : min a.toNat n = a.toNat := Nat.min_eq_left (le_of_lt haN)
|
||
have hba : b ≤ a := by
|
||
by_contra hc
|
||
have hbgt : a < b := lt_of_not_ge hc
|
||
have hbpos : 0 < b.toNat := by omega
|
||
have hbBn : a.toNat < b.toNat := by omega
|
||
have : a.toNat < min b.toNat n := Nat.lt_min.mpr ⟨hbBn, haN⟩
|
||
omega
|
||
exact Or.inl hba
|
||
· rintro (hba | hna | ⟨ha, hb⟩ | hn0)
|
||
· exact not_lt.mpr (min_le_min (Int.toNat_le_toNat hba) le_rfl)
|
||
· have hminA : min a.toNat n = n := by rw [Nat.min_eq_right]; omega
|
||
have : min b.toNat n ≤ n := Nat.min_le_right _ _
|
||
omega
|
||
· have hA0 : a.toNat = 0 := by omega
|
||
have hB0 : b.toNat = 0 := by omega
|
||
rw [hA0, hB0]; simp
|
||
· subst hn0; simp
|
||
|
||
/-- Arithmetic: a clamp below a stored index bounds the unclamped anchor above by it. -/
|
||
theorem le_of_min_toNat_le {a : ℤ} {j n : ℕ} (hj : j < n) (h : min a.toNat n ≤ j) :
|
||
a ≤ (j : ℤ) := by
|
||
have hres' : (min a.toNat n : ℤ) ≤ (j : ℤ) := by exact_mod_cast h
|
||
have hminle : (min a.toNat n : ℤ) ≤ (a.toNat : ℤ) := by
|
||
exact_mod_cast Nat.min_le_left a.toNat n
|
||
have hmax := Int.toNat_eq_max a
|
||
omega
|
||
|
||
/-- Arithmetic: an index strictly below a clamp is strictly below the unclamped anchor. -/
|
||
theorem lt_of_lt_min_toNat {a : ℤ} {j n : ℕ} (h : j < min a.toNat n) : (j : ℤ) < a := by
|
||
have h1 : j < a.toNat := lt_of_lt_of_le h (Nat.min_le_left _ _)
|
||
have h2 : (j : ℤ) < (a.toNat : ℤ) := by exact_mod_cast h1
|
||
have h3 := Int.toNat_eq_max a
|
||
omega
|
||
|
||
/-- **The empty disjunction, read off `resolve`, unconditionally.** A complete result
|
||
is empty exactly when one of the four empty alternatives holds. The two extra
|
||
alternatives are the clamped cases at nonpositive offsets (both anchors negative)
|
||
and the empty database; at positive offsets they are unsatisfiable and this reduces
|
||
to the classical disjunction. -/
|
||
theorem completeKeys_eq_nil_iff_emptyConstraints {db : DB} {r : RangeRead} :
|
||
RangeRead.completeKeys db r = [] ↔ AnyHolds (emptyConstraints r) db := by
|
||
have hAc : clampIndex db r.begin =
|
||
min (((cnt db r.begin.key : ℤ) + r.begin.offset - 1).toNat) db.keys.length :=
|
||
clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl
|
||
have hEc : clampIndex db r.end_ =
|
||
min (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat) db.keys.length :=
|
||
clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl
|
||
have key : (RangeRead.completeKeys db r = []) ↔
|
||
¬ (clampIndex db r.begin < clampIndex db r.end_) := by
|
||
rw [← completeKeys_ne_nil_iff]; simp only [not_ne_iff]
|
||
rw [key, hAc, hEc, not_lt_min_toNat_dnf, anyHolds_emptyConstraints]
|
||
have hEnd : (emptyConstraintEndLe r).HoldsAt db ↔
|
||
(cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤
|
||
(cnt db r.begin.key : ℤ) + r.begin.offset - 1 := by
|
||
simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintEndLe, Constraint.le]
|
||
omega
|
||
have hBeginTop : (emptyConstraintBeginTop r).HoldsAt db ↔
|
||
(db.keys.length : ℤ) ≤ (cnt db r.begin.key : ℤ) + r.begin.offset - 1 := by
|
||
simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintBeginTop, cnt_top]
|
||
omega
|
||
have hBeginNonpos : (emptyConstraintBeginNonpos r).HoldsAt db ↔
|
||
(cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤ 0 := by
|
||
simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintBeginNonpos, Constraint.le,
|
||
cnt_nil]
|
||
omega
|
||
have hEndNonpos : (emptyConstraintEndNonpos r).HoldsAt db ↔
|
||
(cnt db r.end_.key : ℤ) + r.end_.offset - 1 ≤ 0 := by
|
||
simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintEndNonpos, Constraint.le,
|
||
cnt_nil]
|
||
omega
|
||
have hNilDb : (emptyConstraintNilDb r).HoldsAt db ↔ db.keys.length = 0 := by
|
||
simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintNilDb, Constraint.le,
|
||
cnt_top, cnt_nil]
|
||
omega
|
||
simp only [hEnd, hBeginTop, hBeginNonpos, hEndNonpos, hNilDb]
|
||
|
||
/-! ## The general collector
|
||
|
||
The three shapes above (`constraintsOf` for a complete result, `truncatedConstraints`
|
||
for a one-sided one, `emptyConstraints` for the empty result) are not yet the
|
||
precise collector for a *general* read/result pair. The precise statement is the
|
||
main development's general bridge `compatible_iff_bridge_general`: for the reported
|
||
keys `A` in ascending order, compatibility factors as the limit and
|
||
`bridgeOf r A more`, whose conjuncts are
|
||
|
||
* the boundary read on the reported side being empty (`boundaryReadLo r k₀` for a
|
||
non-reversed read, `boundaryReadHi r kₙ` for a reversed one), dropped on a
|
||
truncated result's free side;
|
||
* `midEq A`, the interval equality `[k₀, keyAfter kₙ).keys(db) = A`;
|
||
* `bracket r A`, the two bracketing inequalities.
|
||
|
||
Each conjunct is a rank statement, so the collector is a *choice* of which
|
||
conjuncts to emit. The middle equality is exactly the pair of total-count
|
||
constraints of `constraintsOf`; the boundary read's emptiness is the empty read's
|
||
`emptyConstraints` DNF; the bracket is the two bracket constraints. Composing them
|
||
gives a `ConstraintDNF` whose alternatives are the ways the boundary DNF can be
|
||
satisfied. This section builds that collector and proves *both* halves: a compatible
|
||
database satisfies the collected constraints, and conversely the constraints pin the
|
||
complete keys down, so the collector is exact.
|
||
|
||
The middle-equality constraints are `constraintsOf`'s total pair; to avoid
|
||
duplicating the storedness/contiguity/capacity rows, the general collector is
|
||
defined by selecting the boundary DNF, prepending the bracket, and appending the
|
||
middle pair. -/
|
||
|
||
/-- The middle-equality system of a reported ascending list `A`: the two halves of
|
||
`ρ(keyAfter kₙ) − ρ(k₀) = |A|`, normalized to `≤ |A|` and `≤ -|A|`. This is the
|
||
sub-system of `constraintsOf A` that encodes `midEq A`, isolated so the general
|
||
collector can attach the boundary DNF without re-emitting the per-key rows. -/
|
||
def midConstraints (A : List Key) : ConstraintSystem :=
|
||
match A with
|
||
| [] => []
|
||
| k₀ :: _ =>
|
||
let kₙ := A.getLastD k₀
|
||
[Constraint.le (k₀ : WithTop Key) ((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ),
|
||
Constraint.ge (k₀ : WithTop Key) ((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ)]
|
||
|
||
/-- The traversal-independent **core** of the collected constraints: each reported
|
||
key is stored (`ρ(keyAfter k) − ρ k ≥ 1`, its `≤ 1` half being free), and the total
|
||
is `|A|` over `[k₀, keyAfter kₙ)`, both halves. Storedness plus the total already pin
|
||
the middle down to `A` (there are at least `|A|` stored keys, and exactly `|A|`), so
|
||
contiguity is redundant for exactness; the adjacent gap capacities are tautologies.
|
||
This is the content `midEq A` exposes, and the left conjunct of the reverse
|
||
obligation. -/
|
||
def coreConstraints (A : List Key) : ConstraintSystem :=
|
||
match A with
|
||
| [] => []
|
||
| k₀ :: _ =>
|
||
let kₙ := A.getLastD k₀
|
||
(A.map (fun (k : Key) =>
|
||
Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1)) ++
|
||
[Constraint.le (k₀ : WithTop Key)
|
||
((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ),
|
||
Constraint.ge (k₀ : WithTop Key)
|
||
((keyAfter kₙ : Key) : WithTop Key) (A.length : ℤ)]
|
||
|
||
/-- **Each reported key is stored.** Under `midEq A`, every `k ∈ A` satisfies
|
||
`ρ(keyAfter k) − ρ k ≥ 1`: the interval `[k, keyAfter k)` contains `k` itself, so
|
||
it stores at least one key. -/
|
||
theorem coreStored_holds {db : DB} {A : List Key} (_hA : A ≠ []) (hmid : midEq A db)
|
||
{k : Key} (hk : k ∈ A) :
|
||
(Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1).HoldsAt db := by
|
||
have hkstore : k ∈ db.keys :=
|
||
(inRange_mem_iff.mp (by rw [hmid]; exact hk)).1
|
||
have hle : (k : WithTop Key) ≤ ((keyAfter k : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k))
|
||
have hmem : k ∈ between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) := by
|
||
rw [between, List.mem_filter]
|
||
exact ⟨hkstore, by simp [keyAfter_self_lt]⟩
|
||
have hpos : 0 < (between db (k : WithTop Key)
|
||
((keyAfter k : Key) : WithTop Key)).length := List.length_pos_of_mem hmem
|
||
have hcnt := cnt_eq_add_between db hle
|
||
change (cnt db (k : WithTop Key) : ℤ) -
|
||
(cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) ≤ -1
|
||
have : (cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) =
|
||
cnt db (k : WithTop Key) +
|
||
((between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key)).length : ℤ) := by
|
||
rw [hcnt]; push_cast; ring
|
||
omega
|
||
|
||
/-- The bracket system of a read against `A`: the two bracketing inequalities
|
||
`ρ(begin.key) − ρ(k₀) ≤ 1 − o_B` and `ρ(kₙ) − ρ(end.key) ≤ o_E − 2`. Empty when `A`
|
||
is empty (no reported key to bracket). -/
|
||
def bracketConstraints (r : RangeRead) (A : List Key) : ConstraintSystem :=
|
||
match A with
|
||
| [] => []
|
||
| k₀ :: _ =>
|
||
let kₙ := A.getLastD k₀
|
||
[Constraint.ge r.begin.key k₀ (r.begin.offset - 1),
|
||
Constraint.le r.end_.key kₙ (r.end_.offset - 2)]
|
||
|
||
/-- The boundary read on the reported side: lower when not reversed, upper when
|
||
reversed. This is the read whose emptiness the truncated bridge asserts. -/
|
||
def reportedBoundaryRead (r : RangeRead) (A : List Key) : RangeRead :=
|
||
if r.reverse then boundaryReadHi r (A.getLastD ([] : Key))
|
||
else boundaryReadLo r (A.headD ([] : Key))
|
||
|
||
/-- The boundary reads the bridge requires: both ends for a complete result, only
|
||
the reported side for a truncated one. This is the traversal/truncation dispatch of
|
||
`bridgeOf`, stated as the list of reads whose emptiness is a conjunct. -/
|
||
def boundaryReads (r : RangeRead) (A : List Key) (more : Bool) : List RangeRead :=
|
||
if more then [reportedBoundaryRead r A]
|
||
else [boundaryReadLo r (A.headD ([] : Key)), boundaryReadHi r (A.getLastD ([] : Key))]
|
||
|
||
/-- Conjunction of two constraint DNFs: the alternatives are the pairs of one
|
||
alternative from each, concatenated. The empty conjunction is the one-element DNF
|
||
`[[]]`. -/
|
||
def conjDNF (D₁ D₂ : ConstraintDNF) : ConstraintDNF :=
|
||
D₁.flatMap (fun S₁ => D₂.map (fun S₂ => S₁ ++ S₂))
|
||
|
||
/-- Conjunction of a list of constraint DNFs, left to right. -/
|
||
def conjDNFs : List ConstraintDNF → ConstraintDNF
|
||
| [] => [[]]
|
||
| D :: Ds => conjDNF D (conjDNFs Ds)
|
||
|
||
/-- The precise constraint DNF collected from a general read and result, given the
|
||
reported keys `A` in ascending order and the truncation flag `more`. It conjoins the
|
||
empty DNF of every boundary read the bridge requires (both ends for a complete
|
||
result, the reported side only for a truncated one) with the bracket and core
|
||
systems. Each conjunction is one alternative of the resulting DNF, so the
|
||
alternatives enumerate the ways the boundary disjunctions can be resolved. -/
|
||
def collectOf (r : RangeRead) (A : List Key) (more : Bool) : ConstraintDNF :=
|
||
(conjDNFs ((boundaryReads r A more).map emptyConstraints)).map
|
||
(fun S => bracketConstraints r A ++ coreConstraints A ++ S)
|
||
|
||
/-- Membership rewrites the boundary-read list: a complete result requires both
|
||
boundary reads, a truncated one only the reported side. -/
|
||
theorem mem_boundaryReads {r : RangeRead} {A : List Key} {more : Bool} {s : RangeRead} :
|
||
s ∈ boundaryReads r A more ↔
|
||
(if more then s = reportedBoundaryRead r A
|
||
else s = boundaryReadLo r (A.headD ([] : Key)) ∨
|
||
s = boundaryReadHi r (A.getLastD ([] : Key))) := by
|
||
cases more <;> simp [boundaryReads]
|
||
|
||
/-- `bridgeOf` as an explicit `if`, so rewriting on `more` and `reverse` reduces it.
|
||
The imported definition uses `match`, which is the same function. -/
|
||
theorem bridgeOf_eq (r : RangeRead) (A : List Key) (more : Bool) (db : DB) :
|
||
bridgeOf r A more db ↔
|
||
(if more then (if r.reverse then bridgeRevTrunc r A db else bridgeFwdTrunc r A db)
|
||
else bridge r A db) := by
|
||
cases more <;> cases hr : r.reverse <;> unfold bridgeOf <;> simp [hr]
|
||
|
||
/-- The middle equality is a conjunct of every general bridge. -/
|
||
theorem midEq_of_bridgeOf {db : DB} {r : RangeRead} {A : List Key} {more : Bool}
|
||
(h : bridgeOf r A more db) : midEq A db := by
|
||
rw [bridgeOf_eq] at h
|
||
by_cases hm : more
|
||
· by_cases hr : r.reverse
|
||
· simp only [hm, hr, bridgeRevTrunc] at h; exact h.2.1
|
||
· simp only [hm, hr, bridgeFwdTrunc] at h; exact h.2.1
|
||
· simp only [hm, bridge] at h; exact h.2.2.1
|
||
|
||
/-- The bracketing is a conjunct of every general bridge. -/
|
||
theorem bracket_of_bridgeOf {db : DB} {r : RangeRead} {A : List Key} {more : Bool}
|
||
(h : bridgeOf r A more db) : bracket r A db := by
|
||
rw [bridgeOf_eq] at h
|
||
by_cases hm : more
|
||
· by_cases hr : r.reverse
|
||
· simp only [hm, hr, bridgeRevTrunc] at h; exact h.2.2
|
||
· simp only [hm, hr, bridgeFwdTrunc] at h; exact h.2.2
|
||
· simp only [hm, bridge] at h; exact h.2.2.2
|
||
|
||
/-- Every boundary read the collector requires is empty at a bridge-satisfying
|
||
database: the lower boundary read for a complete result, and the reported side for
|
||
a truncated one. -/
|
||
theorem boundaryReads_empty_of_bridgeOf {db : DB} {r : RangeRead} {A : List Key} {more : Bool}
|
||
(h : bridgeOf r A more db) :
|
||
∀ s ∈ boundaryReads r A more, completeKeys db s = [] := by
|
||
rw [bridgeOf_eq] at h
|
||
intro s hs
|
||
by_cases hmore : more
|
||
· -- truncated: only the reported side
|
||
rw [boundaryReads, ite_eq_left hmore, List.mem_singleton] at hs
|
||
subst hs
|
||
by_cases hr : r.reverse
|
||
· simp only [hmore, hr, bridgeRevTrunc, reportedBoundaryRead] at h ⊢
|
||
exact h.1
|
||
· simp only [hmore, hr, Bool.false_eq_true, bridgeFwdTrunc, reportedBoundaryRead] at h ⊢
|
||
exact h.1
|
||
· -- complete: both ends
|
||
rw [boundaryReads, ite_eq_right hmore] at hs
|
||
simp only [List.mem_cons, List.not_mem_nil, or_false] at hs
|
||
simp only [hmore, bridge] at h
|
||
rcases hs with rfl | rfl
|
||
· simpa only [List.headD_cons] using h.1
|
||
· simpa only [List.getLastD_cons] using h.2.1
|
||
|
||
/-- The least reported key of a `midEq` interval is stored. -/
|
||
theorem midEq_head_mem {db : DB} {A : List Key} (hA : A ≠ []) (hmid : midEq A db) :
|
||
A.headD ([] : Key) ∈ db.keys :=
|
||
(inRange_mem_iff.mp (by rw [hmid]; exact headD_mem hA)).1
|
||
|
||
/-- The greatest reported key of a `midEq` interval is stored. -/
|
||
theorem midEq_getLast_mem {db : DB} {A : List Key} (hA : A ≠ []) (hmid : midEq A db) :
|
||
A.getLastD ([] : Key) ∈ db.keys :=
|
||
(inRange_mem_iff.mp (by rw [hmid]; exact getLastD_mem hA)).1
|
||
|
||
/-- **The lower bracket as a rank inequality.** If `resolve begin ≤ k₀` and `k₀` is
|
||
the least key of a `midEq` interval, then `ρ begin.key + (o_B − 1) ≤ ρ k₀`, i.e. the
|
||
constraint `Constraint.ge begin.key k₀ (o_B − 1)` holds. This is the counting form of
|
||
`resolve begin ≤ k₀` used by the general collector, and it is total in the offset:
|
||
`clampIndex`'s `min` with the database length only lowers the anchor, and
|
||
`Int.self_le_toNat` recovers the clamp at the bottom. -/
|
||
theorem cnt_begin_add_offset_sub_one_le_of_resolve_le {db : DB} {r : RangeRead} {A : List Key}
|
||
(hA : A ≠ []) (hmid : midEq A db)
|
||
(hb : resolve db r.begin ≤ (↑(A.headD ([] : Key)) : WithTop Key)) :
|
||
(cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤
|
||
(cnt db (A.headD ([] : Key)) : ℤ) := by
|
||
set k₀ := A.headD ([] : Key) with hk₀
|
||
have hk₀mem : k₀ ∈ db.keys := midEq_head_mem hA hmid
|
||
obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hk₀mem
|
||
have hcnt : db.keys.countP (fun x => decide (x < k₀)) = j :=
|
||
countP_lt_eq_of_getElem db.sorted hj hjk
|
||
have hres : clampIndex db r.begin ≤ j :=
|
||
(resolve_le_getElem_iff (db := db) (s := r.begin) hj).mp (by simpa [hjk] using hb)
|
||
have hge : (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤ (j : ℤ) := by
|
||
rw [clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl] at hres
|
||
exact le_of_min_toNat_le hj hres
|
||
have hk₀cnt : (cnt db k₀ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt
|
||
rw [hk₀cnt]
|
||
exact hge
|
||
|
||
/-- **The upper bracket as a rank inequality.** If `kₙ < resolve end` and `kₙ` is the
|
||
greatest key of a `midEq` interval, then `ρ kₙ − ρ end.key ≤ o_E − 2`, i.e. the
|
||
constraint `Constraint.le end.key kₙ (o_E − 2)` holds. Total in the offset, by the
|
||
same `min`-lowering and `Int.self_le_toNat` argument as the lower bracket. -/
|
||
theorem cnt_sub_end_le_of_lt_resolve {db : DB} {r : RangeRead} {A : List Key}
|
||
(hA : A ≠ []) (hmid : midEq A db)
|
||
(hb : (↑(A.getLastD ([] : Key)) : WithTop Key) < resolve db r.end_) :
|
||
(cnt db (A.getLastD ([] : Key)) : ℤ) - (cnt db r.end_.key : ℤ) ≤
|
||
r.end_.offset - 2 := by
|
||
set kₙ := A.getLastD ([] : Key) with hkₙ
|
||
have hkₙmem : kₙ ∈ db.keys := midEq_getLast_mem hA hmid
|
||
obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hkₙmem
|
||
have hcnt : db.keys.countP (fun x => decide (x < kₙ)) = j :=
|
||
countP_lt_eq_of_getElem db.sorted hj hjk
|
||
have hres : j < clampIndex db r.end_ :=
|
||
(getElem_lt_resolve_iff (db := db) (s := r.end_) hj).mp (by simpa [hjk] using hb)
|
||
have hlt : (j : ℤ) < (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by
|
||
rw [clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl] at hres
|
||
exact lt_of_lt_min_toNat hres
|
||
have hkₙcnt : (cnt db kₙ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt
|
||
rw [hkₙcnt]
|
||
omega
|
||
|
||
|
||
/-- **The middle system holds.** If `midEq A db`, the two total-count constraints of
|
||
`midConstraints A` hold: `ρ(keyAfter kₙ) − ρ(k₀) = |A|`, stated as its two `≤`
|
||
halves. -/
|
||
theorem midConstraints_holds {db : DB} {A : List Key} (hA : A ≠ [])
|
||
(hpair : A.Pairwise (· < ·)) (hmid : midEq A db) :
|
||
SystemHoldsAt (midConstraints A) db := by
|
||
obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA
|
||
have hpair' : (k₀ :: t).Pairwise (· < ·) := hpair
|
||
have hle : k₀ ≤ t.getLastD k₀ := by
|
||
simpa only [List.headD_cons, List.getLastD_cons] using
|
||
headD_le_of_pairwise hpair' (getLastD_mem (by simp))
|
||
have hleW : (↑k₀ : WithTop Key) ≤ (↑(keyAfter (t.getLastD k₀)) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (le_of_lt (lt_of_le_of_lt hle (keyAfter_self_lt _)))
|
||
have hcount : (cnt db (keyAfter (t.getLastD k₀)) : ℤ) - (cnt db k₀ : ℤ) =
|
||
((k₀ :: t).length : ℤ) := by
|
||
have hbetween : between db (↑k₀ : WithTop Key) (↑(keyAfter (t.getLastD k₀)) : WithTop Key) =
|
||
inRange db.keys (↑k₀ : WithTop Key) (↑(keyAfter (t.getLastD k₀)) : WithTop Key) := rfl
|
||
have hmid' : inRange db.keys (↑k₀ : WithTop Key)
|
||
(↑(keyAfter (t.getLastD k₀)) : WithTop Key) = k₀ :: t := by
|
||
have := hmid
|
||
simp only [midEq, List.headD_cons, List.getLastD_cons] at this
|
||
exact this
|
||
have hcnt := cnt_eq_add_between db hleW
|
||
rw [hbetween, hmid'] at hcnt
|
||
have : (cnt db (↑(keyAfter (t.getLastD k₀)) : WithTop Key) : ℤ) =
|
||
cnt db k₀ + ((k₀ :: t).length : ℤ) := by
|
||
rw [hcnt]; push_cast; ring
|
||
omega
|
||
intro c hc
|
||
simp only [midConstraints, List.getLastD_cons, List.mem_cons,
|
||
List.not_mem_nil, or_false] at hc
|
||
rcases hc with rfl | rfl
|
||
· change (cnt db (↑(keyAfter (t.getLastD k₀)) : WithTop Key) : ℤ) -
|
||
(cnt db (↑k₀ : WithTop Key) : ℤ) ≤ ((k₀ :: t).length : ℤ)
|
||
omega
|
||
· change (cnt db (↑k₀ : WithTop Key) : ℤ) -
|
||
(cnt db (↑(keyAfter (t.getLastD k₀)) : WithTop Key) : ℤ) ≤ -(((k₀ :: t).length : ℤ))
|
||
omega
|
||
|
||
/-- **The core system holds under `midEq`.** Every constraint of `coreConstraints A`
|
||
holds at a database satisfying `midEq A`: storedness for each reported key
|
||
(`coreStored_holds`) and the total pair (`midConstraints_holds`). -/
|
||
theorem coreConstraints_holds {db : DB} {A : List Key} (hA : A ≠ [])
|
||
(hpair : A.Pairwise (· < ·)) (hmid : midEq A db) :
|
||
SystemHoldsAt (coreConstraints A) db := by
|
||
obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA
|
||
have hmidS := midConstraints_holds (A := k₀ :: t) (by simp) hpair hmid
|
||
intro c hc
|
||
rw [coreConstraints] at hc
|
||
rcases List.mem_append.mp hc with hc | hc
|
||
· rcases List.mem_map.mp hc with ⟨k, hk, rfl⟩
|
||
exact coreStored_holds (by simp) hmid hk
|
||
· exact hmidS c hc
|
||
|
||
/-- **A storedness constraint forces its key stored.** If
|
||
`ρ(keyAfter k) − ρ k ≥ 1` holds, then `k ∈ db.keys`: the only key in
|
||
`[k, keyAfter k)` is `k` itself (`eq_of_le_of_lt_keyAfter`), and the interval stores
|
||
at least one. -/
|
||
theorem mem_of_coreStored {db : DB} {k : Key}
|
||
(h : (Constraint.ge (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) 1).HoldsAt db) :
|
||
k ∈ db.keys := by
|
||
have hle : (k : WithTop Key) ≤ ((keyAfter k : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k))
|
||
have hcnt := cnt_eq_add_between db hle
|
||
have hge : cnt db (k : WithTop Key) + 1 ≤
|
||
cnt db ((keyAfter k : Key) : WithTop Key) := by
|
||
change (cnt db (k : WithTop Key) : ℤ) -
|
||
(cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) ≤ -1 at h
|
||
omega
|
||
have hpos : 0 < (between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key)).length := by
|
||
omega
|
||
obtain ⟨y, hy⟩ := List.exists_mem_of_ne_nil _
|
||
(List.ne_nil_of_length_pos hpos)
|
||
simp only [between, List.mem_filter, decide_eq_true_eq] at hy
|
||
have hyk : y = k := eq_of_le_of_lt_keyAfter (WithTop.coe_le_coe.mp hy.2.1)
|
||
(WithTop.coe_lt_coe.mp hy.2.2)
|
||
rw [hyk] at hy
|
||
exact hy.1
|
||
|
||
/-- **The core recovers the middle equality.** If `coreConstraints A` holds at `db`
|
||
for an ascending `A`, then `midEq A db`. Storedness forces every reported key stored;
|
||
the total caps the middle interval's stored count at `|A|`; since `A` itself lies in
|
||
that interval, the two ascending duplicate-free lists of equal length coincide. This
|
||
is the injectivity step of precise collection, and it is unconditional (the
|
||
boundary-read converse is what remains of the reverse). -/
|
||
theorem midEq_of_coreConstraints {db : DB} {A : List Key} (hA : A ≠ [])
|
||
(hpair : A.Pairwise (· < ·)) (h : SystemHoldsAt (coreConstraints A) db) :
|
||
midEq A db := by
|
||
obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA
|
||
have hpair' : (k₀ :: t).Pairwise (· < ·) := hpair
|
||
have hk₀le_kn : k₀ ≤ (k₀ :: t).getLastD k₀ :=
|
||
headD_le_of_pairwise hpair' (getLastD_mem (by simp))
|
||
have hleW : (k₀ : WithTop Key) ≤
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr
|
||
(le_of_lt (lt_of_le_of_lt hk₀le_kn (keyAfter_self_lt _)))
|
||
-- storedness: every reported key is stored
|
||
have hstored : ∀ k ∈ (k₀ :: t), k ∈ db.keys := by
|
||
intro k hk
|
||
apply mem_of_coreStored
|
||
apply h
|
||
rw [coreConstraints]
|
||
exact List.mem_append.mpr (Or.inl (List.mem_map.mpr ⟨k, hk, rfl⟩))
|
||
-- the total constraint: the middle interval stores exactly |A| keys
|
||
have htotal_le : (cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) -
|
||
(cnt db (k₀ : WithTop Key) : ℤ) ≤ (((k₀ :: t).length : ℕ) : ℤ) := by
|
||
have hc := h (Constraint.le (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)
|
||
(((k₀ :: t).length : ℕ) : ℤ))
|
||
(by rw [coreConstraints]; simp)
|
||
simpa only [Constraint.HoldsAt, Constraint.eval, Constraint.le] using hc
|
||
have htotal_ge : ((k₀ :: t).length : ℤ) ≤
|
||
(cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) -
|
||
(cnt db (k₀ : WithTop Key) : ℤ) := by
|
||
have hc := h (Constraint.ge (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)
|
||
(((k₀ :: t).length : ℕ) : ℤ))
|
||
(by rw [coreConstraints]; simp)
|
||
change (cnt db (k₀ : WithTop Key) : ℤ) -
|
||
(cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) ≤
|
||
-(((k₀ :: t).length : ℕ) : ℤ) at hc
|
||
omega
|
||
-- the interval's stored length is |A|
|
||
have hlen : (inRange db.keys (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)).length =
|
||
(k₀ :: t).length := by
|
||
have hcnt := cnt_eq_add_between db hleW
|
||
rw [show between db (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) =
|
||
inRange db.keys (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) from rfl] at hcnt
|
||
have : (cnt db ((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) : ℤ) =
|
||
cnt db (k₀ : WithTop Key) +
|
||
((inRange db.keys (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)).length : ℤ) := by
|
||
rw [hcnt]; push_cast; ring
|
||
omega
|
||
-- A lies in the interval
|
||
have hsub : (k₀ :: t) ⊆ inRange db.keys (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key) := by
|
||
intro k hk
|
||
rw [inRange_mem_iff]
|
||
refine ⟨hstored k hk, WithTop.coe_le_coe.mpr (headD_le_of_pairwise hpair' hk), ?_⟩
|
||
have hkle : k ≤ (k₀ :: t).getLastD k₀ := le_getLastD_of_pairwise hpair' hk
|
||
exact WithTop.coe_lt_coe.mpr (lt_of_le_of_lt hkle (keyAfter_self_lt _))
|
||
-- equal ascending duplicate-free lists
|
||
have hperm : List.Perm (k₀ :: t) (inRange db.keys (k₀ : WithTop Key)
|
||
((keyAfter ((k₀ :: t).getLastD k₀) : Key) : WithTop Key)) :=
|
||
(List.subperm_of_subset hpair'.nodup hsub).perm_of_length_le (by rw [hlen])
|
||
exact eq_of_mem_iff_pairwise (RangeRead.inRange_pairwise db _ _) hpair'
|
||
(fun x => ⟨fun hx => hperm.symm.subset hx, fun hx => hsub hx⟩)
|
||
|
||
/-- **The lower bracket constraint recovers the resolved begin.** Conversely to
|
||
`cnt_begin_add_offset_sub_one_le_of_resolve_le`: if `ρ begin.key + (o_B−1) ≤ ρ k₀` and
|
||
`k₀` is the least key of a `midEq` interval, then `resolve begin ≤ k₀`. Total in the
|
||
offset, since `min` only lowers the clamp. -/
|
||
theorem resolve_le_of_cnt_begin_add_offset_sub_one_le {db : DB} {r : RangeRead}
|
||
{A : List Key} (hA : A ≠ []) (hmid : midEq A db)
|
||
(hcon : (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤
|
||
(cnt db (A.headD ([] : Key)) : ℤ)) :
|
||
resolve db r.begin ≤ (↑(A.headD ([] : Key)) : WithTop Key) := by
|
||
set k₀ := A.headD ([] : Key) with hk₀
|
||
have hk₀mem : k₀ ∈ db.keys := midEq_head_mem hA hmid
|
||
obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hk₀mem
|
||
have hcnt : db.keys.countP (fun x => decide (x < k₀)) = j :=
|
||
countP_lt_eq_of_getElem db.sorted hj hjk
|
||
have hk₀cnt : (cnt db k₀ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt
|
||
rw [hk₀cnt] at hcon
|
||
set a : ℤ := (cnt db r.begin.key : ℤ) + r.begin.offset - 1 with ha
|
||
have hclamp : clampIndex db r.begin ≤ j := by
|
||
rw [clampIndex_eq db r.begin.key r.begin.offset (cnt db r.begin.key) rfl, ← ha]
|
||
calc min a.toNat db.keys.length ≤ a.toNat := Nat.min_le_left _ _
|
||
_ ≤ j := by
|
||
have hmax : (a.toNat : ℤ) = max a 0 := Int.toNat_eq_max a
|
||
have : (a.toNat : ℤ) ≤ (j : ℤ) := by
|
||
rw [hmax]; exact max_le hcon (by positivity)
|
||
exact_mod_cast this
|
||
have := (resolve_le_getElem_iff (db := db) (s := r.begin) hj).mpr (by simpa [hjk] using hclamp)
|
||
rwa [hjk] at this
|
||
|
||
/-- **The upper bracket constraint recovers the resolved end.** Conversely to
|
||
`cnt_sub_end_le_of_lt_resolve`: if `ρ kₙ − ρ end.key ≤ o_E−2` and `kₙ` is the greatest
|
||
key of a `midEq` interval, then `kₙ < resolve end`. Total in the offset. -/
|
||
theorem lt_resolve_of_cnt_sub_end_le {db : DB} {r : RangeRead}
|
||
{A : List Key} (hA : A ≠ []) (hmid : midEq A db)
|
||
(hcon : (cnt db (A.getLastD ([] : Key)) : ℤ) - (cnt db r.end_.key : ℤ) ≤
|
||
r.end_.offset - 2) :
|
||
(↑(A.getLastD ([] : Key)) : WithTop Key) < resolve db r.end_ := by
|
||
set kₙ := A.getLastD ([] : Key) with hkₙ
|
||
have hkₙmem : kₙ ∈ db.keys := midEq_getLast_mem hA hmid
|
||
obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hkₙmem
|
||
have hcnt : db.keys.countP (fun x => decide (x < kₙ)) = j :=
|
||
countP_lt_eq_of_getElem db.sorted hj hjk
|
||
have hkₙcnt : (cnt db kₙ : ℤ) = j := by rw [cnt_coe]; exact_mod_cast hcnt
|
||
rw [hkₙcnt] at hcon
|
||
have hclamp : j < clampIndex db r.end_ := by
|
||
rw [clampIndex_eq db r.end_.key r.end_.offset (cnt db r.end_.key) rfl]
|
||
refine Nat.lt_min.mpr ⟨?_, hj⟩
|
||
have ha : (j : ℤ) < (cnt db r.end_.key : ℤ) + r.end_.offset - 1 := by omega
|
||
have hmax : (((cnt db r.end_.key : ℤ) + r.end_.offset - 1).toNat : ℤ) =
|
||
max ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) 0 := Int.toNat_eq_max _
|
||
have h1 : (j : ℤ) <
|
||
max ((cnt db r.end_.key : ℤ) + r.end_.offset - 1) 0 :=
|
||
lt_of_lt_of_le ha (Int.le_max_left _ _)
|
||
rw [← hmax] at h1
|
||
exact_mod_cast h1
|
||
have := (getElem_lt_resolve_iff (db := db) (s := r.end_) hj).mpr (by simpa [hjk] using hclamp)
|
||
rwa [hjk] at this
|
||
|
||
/-- **A member of a conjunction of DNFs projects to an alternative of each.** If a
|
||
specific alternative `S` of `conjDNFs Ds` holds at `db`, then every `D ∈ Ds` has some
|
||
alternative holding at `db`. This is the membership form of `anyHolds_conjDNFs`, and
|
||
it is what lets the reverse direction read each boundary read's disjunction off the
|
||
collected alternative. -/
|
||
theorem anyHolds_of_mem_conjDNFs {Ds : List ConstraintDNF} {S : ConstraintSystem}
|
||
{db : DB} (hS : S ∈ conjDNFs Ds) (hh : SystemHoldsAt S db) :
|
||
∀ D ∈ Ds, AnyHolds D db := by
|
||
induction Ds generalizing S with
|
||
| nil =>
|
||
rw [conjDNFs, List.mem_singleton] at hS
|
||
subst hS
|
||
intro D hD
|
||
exact absurd hD (by simp)
|
||
| cons D Ds ih =>
|
||
rw [conjDNFs, conjDNF, List.mem_flatMap] at hS
|
||
obtain ⟨S₁, hS₁, hS₂⟩ := hS
|
||
rw [List.mem_map] at hS₂
|
||
obtain ⟨S₂, hS₂E, rfl⟩ := hS₂
|
||
have hS₁' : SystemHoldsAt S₁ db :=
|
||
fun c hc => hh c (List.mem_append.mpr (Or.inl hc))
|
||
have hS₂' : SystemHoldsAt S₂ db :=
|
||
fun c hc => hh c (List.mem_append.mpr (Or.inr hc))
|
||
intro E hE
|
||
rcases List.mem_cons.mp hE with rfl | hE
|
||
· exact ⟨S₁, hS₁, hS₁'⟩
|
||
· exact ih hS₂E hS₂' E hE
|
||
|
||
/-- **The bracket system recovers the bracketing.** Conversely to
|
||
`bracketConstraints_holds`: if the two bracketing constraints hold and `midEq A db`,
|
||
then `bracket r A db`, using the converse count lemmas. Total in the offsets. -/
|
||
theorem bracket_of_bracketConstraints_holds {db : DB} {r : RangeRead} {A : List Key}
|
||
(hA : A ≠ []) (hmid : midEq A db) (h : SystemHoldsAt (bracketConstraints r A) db) :
|
||
bracket r A db := by
|
||
obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA
|
||
have hlo : (cnt db r.begin.key : ℤ) -
|
||
(cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) ≤ -(r.begin.offset - 1) := by
|
||
have hc := h (Constraint.ge r.begin.key ((k₀ :: t).headD ([] : Key))
|
||
(r.begin.offset - 1))
|
||
(by rw [bracketConstraints]; simp)
|
||
change (cnt db r.begin.key : ℤ) -
|
||
(cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) ≤
|
||
-(r.begin.offset - 1) at hc
|
||
exact hc
|
||
have hhi : (cnt db (↑((k₀ :: t).getLastD k₀) : WithTop Key) : ℤ) -
|
||
(cnt db r.end_.key : ℤ) ≤ r.end_.offset - 2 := by
|
||
have hc := h (Constraint.le r.end_.key ((k₀ :: t).getLastD k₀) (r.end_.offset - 2))
|
||
(by rw [bracketConstraints]; simp)
|
||
exact hc
|
||
have hge : (cnt db r.begin.key : ℤ) + r.begin.offset - 1 ≤
|
||
(cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) := by omega
|
||
exact ⟨resolve_le_of_cnt_begin_add_offset_sub_one_le (A := k₀ :: t) (by simp) hmid hge,
|
||
lt_resolve_of_cnt_sub_end_le (A := k₀ :: t) (by simp) hmid hhi⟩
|
||
|
||
/-- **`bridgeOf` from its three factor families.** If `midEq` and `bracket` hold and
|
||
every boundary read required by the dispatch is empty, then `bridgeOf` holds. This
|
||
packages the four traversal/truncation cases into one statement. -/
|
||
theorem bridgeOf_of_factors {db : DB} {r : RangeRead} {A : List Key} {more : Bool}
|
||
(hmid : midEq A db) (hbrack : bracket r A db)
|
||
(hempty : ∀ s ∈ boundaryReads r A more, completeKeys db s = []) :
|
||
bridgeOf r A more db := by
|
||
rw [bridgeOf_eq]
|
||
by_cases hm : more
|
||
· by_cases hr : r.reverse
|
||
· have h := hempty (reportedBoundaryRead r A) (by
|
||
rw [boundaryReads, ite_eq_left hm]; exact List.mem_singleton.mpr rfl)
|
||
rw [reportedBoundaryRead, ite_eq_left hr] at h
|
||
simp only [hm, hr, ↓reduceIte]
|
||
exact ⟨h, hmid, hbrack⟩
|
||
· have h := hempty (reportedBoundaryRead r A) (by
|
||
rw [boundaryReads, ite_eq_left hm]; exact List.mem_singleton.mpr rfl)
|
||
rw [reportedBoundaryRead, ite_eq_right (by simpa using hr)] at h
|
||
simp only [hm, hr, Bool.false_eq_true, ↓reduceIte]
|
||
exact ⟨h, hmid, hbrack⟩
|
||
· have hlo := hempty (boundaryReadLo r (A.headD ([] : Key))) (by
|
||
rw [boundaryReads, ite_eq_right hm]; exact List.mem_cons_self)
|
||
have hhi := hempty (boundaryReadHi r (A.getLastD ([] : Key))) (by
|
||
rw [boundaryReads, ite_eq_right hm]
|
||
exact List.mem_cons_of_mem _ (List.mem_singleton.mpr rfl))
|
||
simp only [hm, bridge]
|
||
exact ⟨hlo, hhi, hmid, hbrack⟩
|
||
|
||
/-- **The bracket system holds.** If `midEq A db` and `bracket r A db`, the two
|
||
bracketing constraints of `bracketConstraints r A` hold, for every offset pair:
|
||
the counting forms are total once the clamp is absorbed by `Int.self_le_toNat`. -/
|
||
theorem bracketConstraints_holds {db : DB} {r : RangeRead} {A : List Key}
|
||
(hA : A ≠ []) (hmid : midEq A db) (hbrack : bracket r A db) :
|
||
SystemHoldsAt (bracketConstraints r A) db := by
|
||
obtain ⟨k₀, t, rfl⟩ := List.exists_cons_of_ne_nil hA
|
||
have hb : resolve db r.begin ≤ (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) := hbrack.1
|
||
have hb' : (↑((k₀ :: t).getLastD k₀) : WithTop Key) < resolve db r.end_ := hbrack.2
|
||
intro c hc
|
||
simp only [bracketConstraints, List.mem_cons, List.not_mem_nil, or_false] at hc
|
||
rcases hc with rfl | rfl
|
||
· change (cnt db r.begin.key : ℤ) -
|
||
(cnt db (↑((k₀ :: t).headD ([] : Key)) : WithTop Key) : ℤ) ≤ -(r.begin.offset - 1)
|
||
have h := cnt_begin_add_offset_sub_one_le_of_resolve_le (r := r) (A := k₀ :: t)
|
||
(by simp) hmid hb
|
||
omega
|
||
· have h := cnt_sub_end_le_of_lt_resolve (r := r) (A := k₀ :: t) (by simp) hmid hb'
|
||
change (cnt db (↑((k₀ :: t).getLastD k₀) : WithTop Key) : ℤ) -
|
||
(cnt db r.end_.key : ℤ) ≤ r.end_.offset - 2
|
||
exact h
|
||
|
||
/-- **The conjunction of two DNFs holds iff both do.** -/
|
||
theorem anyHolds_conjDNF {D₁ D₂ : ConstraintDNF} {db : DB} :
|
||
AnyHolds (conjDNF D₁ D₂) db ↔ (AnyHolds D₁ db ∧ AnyHolds D₂ db) := by
|
||
simp only [AnyHolds, conjDNF, List.mem_flatMap, List.mem_map]
|
||
constructor
|
||
· rintro ⟨S, ⟨S₁, hS₁, S₂, hS₂, rfl⟩, hS⟩
|
||
refine ⟨⟨S₁, hS₁, fun c hc => hS c (List.mem_append.mpr (Or.inl hc))⟩,
|
||
⟨S₂, hS₂, fun c hc => hS c (List.mem_append.mpr (Or.inr hc))⟩⟩
|
||
· rintro ⟨⟨S₁, hS₁, h₁⟩, ⟨S₂, hS₂, h₂⟩⟩
|
||
refine ⟨S₁ ++ S₂, ⟨S₁, hS₁, S₂, hS₂, rfl⟩, ?_⟩
|
||
intro c hc
|
||
rcases List.mem_append.mp hc with hc | hc
|
||
· exact h₁ c hc
|
||
· exact h₂ c hc
|
||
|
||
/-- **The conjunction of a list of DNFs holds iff every one does.** This is the
|
||
`conjDNFs` counterpart of `anyHolds_conjDNF`: the empty conjunction `[[]]` holds
|
||
trivially, and the alternatives of the result are the tuples of alternatives. -/
|
||
theorem anyHolds_conjDNFs {Ds : List ConstraintDNF} {db : DB} :
|
||
AnyHolds (conjDNFs Ds) db ↔ ∀ D ∈ Ds, AnyHolds D db := by
|
||
induction Ds with
|
||
| nil => simp [conjDNFs, AnyHolds, SystemHoldsAt]
|
||
| cons D Ds ih =>
|
||
rw [conjDNFs, anyHolds_conjDNF, ih]
|
||
constructor
|
||
· rintro ⟨hD, hrest⟩ E hE
|
||
rcases List.mem_cons.mp hE with rfl | hE
|
||
· exact hD
|
||
· exact hrest E hE
|
||
· intro h
|
||
exact ⟨h D (by simp), fun E hE => h E (by simp [hE])⟩
|
||
|
||
/-- **The general collection is sound (forward half).** If `db` is compatible with
|
||
the result `⟨reportedKeys r A, more⟩`, then `db` satisfies `collectOf r A more`: there
|
||
is one alternative of the collected DNF that holds. This is the precise-collection
|
||
*forward* direction, the part that is mechanical counting from the bridge. It is the
|
||
`AnyHolds` analogue of `Characterizes`'s `→`, instantiated at the general
|
||
traversal/truncation dispatch.
|
||
|
||
The proof unwinds `compatible_iff_bridge_general`, sends the middle equality to the
|
||
`midConstraints` pair and the bracketing to `bracketConstraints`, and turns each
|
||
required boundary read's emptiness into its `emptyConstraints` alternative via
|
||
`completeKeys_eq_nil_iff_emptyConstraints`. Both boundary reads carry the empty
|
||
theory's offset hypotheses. -/
|
||
theorem collectOf_holds_of_compatible {db : DB} {r : RangeRead} {A : List Key} {more : Bool}
|
||
(hA : A ≠ []) (hpair : A.Pairwise (· < ·))
|
||
(hlim : withinLimit r (reportedList r A))
|
||
(hcomp : RangeRead.Compatible db r { keys := reportedList r A, more := more }) :
|
||
AnyHolds (collectOf r A more) db := by
|
||
have hbo := (compatible_iff_bridge_general (db := db) (r := r) (A := A) (more := more)
|
||
hA hpair hlim).mp hcomp |>.2
|
||
have hmid := midEq_of_bridgeOf hbo
|
||
have hbrack := bracket_of_bridgeOf hbo
|
||
have hmidS := coreConstraints_holds hA hpair hmid
|
||
have hbrS := bracketConstraints_holds hA hmid hbrack
|
||
-- each required boundary read's emptiness gives its `emptyConstraints` alternative
|
||
have hbalts : ∀ s ∈ boundaryReads r A more, AnyHolds (emptyConstraints s) db := by
|
||
intro s hs
|
||
have hempty := boundaryReads_empty_of_bridgeOf hbo s hs
|
||
by_cases hm : more
|
||
· -- truncated: `s` is the reported side
|
||
rw [boundaryReads, ite_eq_left hm] at hs
|
||
simp only [List.mem_singleton] at hs
|
||
subst hs
|
||
by_cases hr : r.reverse
|
||
· rw [reportedBoundaryRead, ite_eq_left hr] at hempty ⊢
|
||
exact (completeKeys_eq_nil_iff_emptyConstraints (db := db)
|
||
(r := boundaryReadHi r (A.getLastD ([] : Key)))).mp hempty
|
||
· rw [reportedBoundaryRead, ite_eq_right (by simpa using hr)] at hempty ⊢
|
||
exact (completeKeys_eq_nil_iff_emptyConstraints (db := db)
|
||
(r := boundaryReadLo r (A.headD ([] : Key)))).mp hempty
|
||
· -- complete: both ends
|
||
rw [boundaryReads, ite_eq_right (by simpa using hm)] at hs
|
||
simp only [List.mem_cons, List.not_mem_nil, or_false] at hs
|
||
rcases hs with rfl | rfl
|
||
· exact (completeKeys_eq_nil_iff_emptyConstraints (db := db)
|
||
(r := boundaryReadLo r (A.headD ([] : Key)))).mp hempty
|
||
· exact (completeKeys_eq_nil_iff_emptyConstraints (db := db)
|
||
(r := boundaryReadHi r (A.getLastD ([] : Key)))).mp hempty
|
||
have hconj : AnyHolds (conjDNFs ((boundaryReads r A more).map emptyConstraints)) db := by
|
||
rw [anyHolds_conjDNFs]
|
||
intro D hD
|
||
rw [List.mem_map] at hD
|
||
obtain ⟨s, hs, rfl⟩ := hD
|
||
exact hbalts s hs
|
||
rw [collectOf]
|
||
simp only [AnyHolds, List.mem_map] at hconj ⊢
|
||
obtain ⟨S, hS, hSh⟩ := hconj
|
||
refine ⟨bracketConstraints r A ++ coreConstraints A ++ S, ⟨S, hS, rfl⟩, ?_⟩
|
||
intro c hc
|
||
rcases List.mem_append.mp hc with hc | hc
|
||
· rcases List.mem_append.mp hc with hc | hc
|
||
· exact hbrS c hc
|
||
· exact hmidS c hc
|
||
· exact hSh c hc
|
||
|
||
/-- The reported keys of a result in ascending order: `res.keys` unchanged for a
|
||
forward read, reversed for a reversed one. This is the `A` the collector expects. -/
|
||
def ascendingKeys (r : RangeRead) (res : Result) : List Key :=
|
||
if r.reverse then res.keys.reverse else res.keys
|
||
|
||
/-- `reportedList` recovers `res.keys` from its ascending order: the two
|
||
traversal-dependent flips cancel. -/
|
||
theorem reportedList_ascendingKeys (r : RangeRead) (res : Result) :
|
||
reportedList r (ascendingKeys r res) = res.keys := by
|
||
unfold reportedList ascendingKeys
|
||
by_cases hr : r.reverse <;> simp [hr]
|
||
|
||
/-- **Precise collection.** The collected DNF characterizes the read/result pair
|
||
exactly, together with the limit: a database is compatible iff the result respects
|
||
the limit and some alternative of `collectOf` at the ascending reported keys holds.
|
||
The limit appears because it is not a rank statement and is not collected. -/
|
||
def PreciseCharacterizes (r : RangeRead) (res : Result) : Prop :=
|
||
∀ db : DB,
|
||
RangeRead.Compatible db r res ↔
|
||
withinLimit r res.keys ∧ AnyHolds (collectOf r (ascendingKeys r res) res.more) db
|
||
|
||
/-- The forward half of precise collection, stated on a result: compatibility
|
||
implies the collected DNF holds. This is `collectOf_holds_of_compatible` with `A`
|
||
recovered from `res`, and is unconditional. -/
|
||
theorem precise_forward {db : DB} {r : RangeRead} {res : Result}
|
||
(hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·))
|
||
(hlim : withinLimit r res.keys)
|
||
(hcomp : RangeRead.Compatible db r res) :
|
||
AnyHolds (collectOf r (ascendingKeys r res) res.more) db :=
|
||
collectOf_holds_of_compatible hA hpair
|
||
(by rw [reportedList_ascendingKeys]; exact hlim)
|
||
(by rw [reportedList_ascendingKeys]; exact hcomp)
|
||
|
||
/-- **The reverse half of precise collection.** If a database satisfies the
|
||
collected DNF, respects the limit, and the reported keys are nonempty and ascending,
|
||
then it is compatible. The core recovers `midEq` (`midEq_of_coreConstraints`), the
|
||
bracket constraints recover the bracketing
|
||
(`bracket_of_bracketConstraints_holds`), each collected boundary disjunct gives its
|
||
read's emptiness, and `bridgeOf_of_factors` assembles the bridge that
|
||
`compatible_iff_bridge_general` turns into compatibility. Unlike `Characterizes`,
|
||
this direction is now proved, for every offset pair: the core's storedness plus total
|
||
pin the middle down, and the total empty DNF's extra clamp branches supply the
|
||
boundary emptiness. -/
|
||
theorem precise_reverse {db : DB} {r : RangeRead} {res : Result}
|
||
(hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·))
|
||
(hlim : withinLimit r res.keys)
|
||
(hany : AnyHolds (collectOf r (ascendingKeys r res) res.more) db) :
|
||
RangeRead.Compatible db r res := by
|
||
set A := ascendingKeys r res with hAdef
|
||
-- decompose the alternative: bracket ++ core ++ boundary-disjuncts
|
||
rw [collectOf] at hany
|
||
simp only [AnyHolds, List.mem_map] at hany
|
||
obtain ⟨S, ⟨S₀, hS₀, rfl⟩, hSh⟩ := hany
|
||
have hbr : SystemHoldsAt (bracketConstraints r A) db :=
|
||
fun c hc => hSh c (List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl hc))))
|
||
have hcore : SystemHoldsAt (coreConstraints A) db :=
|
||
fun c hc => hSh c (List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inr hc))))
|
||
have hS' : SystemHoldsAt S₀ db :=
|
||
fun c hc => hSh c (List.mem_append.mpr (Or.inr hc))
|
||
have hmid := midEq_of_coreConstraints hA hpair hcore
|
||
have hbrack := bracket_of_bracketConstraints_holds hA hmid hbr
|
||
-- each required boundary read is empty
|
||
have hempty : ∀ s ∈ boundaryReads r A res.more, completeKeys db s = [] := by
|
||
intro s hs
|
||
have hD : emptyConstraints s ∈ (boundaryReads r A res.more).map emptyConstraints :=
|
||
List.mem_map.mpr ⟨s, hs, rfl⟩
|
||
have hany' := anyHolds_of_mem_conjDNFs hS₀ hS' (emptyConstraints s) hD
|
||
exact (completeKeys_eq_nil_iff_emptyConstraints (db := db) (r := s)).mpr hany'
|
||
have hbo : bridgeOf r A res.more db := bridgeOf_of_factors hmid hbrack hempty
|
||
have hres : res = { keys := reportedList r A, more := res.more } := by
|
||
rw [reportedList_ascendingKeys]
|
||
rw [hres]
|
||
exact (compatible_iff_bridge_general (db := db) (r := r) (A := A) (more := res.more)
|
||
hA hpair (by rw [reportedList_ascendingKeys]; exact hlim)).mpr
|
||
⟨by rw [reportedList_ascendingKeys]; exact hlim, hbo⟩
|
||
|
||
/-- **Precise collection, assembled.** The forward and reverse halves together give
|
||
`PreciseCharacterizes`. Both are now proved, for every offset pair: the collected DNF,
|
||
read at the ascending reported keys, characterizes compatibility. -/
|
||
theorem preciseCharacterizes {r : RangeRead} {res : Result}
|
||
(hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·)) :
|
||
PreciseCharacterizes r res := by
|
||
intro db
|
||
constructor
|
||
· intro hcomp
|
||
exact ⟨compatible_withinLimit hcomp,
|
||
precise_forward hA hpair (compatible_withinLimit hcomp) hcomp⟩
|
||
· rintro ⟨hlim, hany⟩
|
||
exact precise_reverse hA hpair hlim hany
|
||
|
||
/-! ## The shared witness satisfies the collected system
|
||
|
||
The collection soundness we actually use is local: the system is a conjunction of
|
||
rank identities, and a *single* database that realizes the read's result satisfies
|
||
all of them at once. The prototype checks this computationally below; the
|
||
framework theorem `system_holdsAt_of_supports_hull` turns it into soundness of the
|
||
read-off range. -/
|
||
|
||
/-- **Constraint collection is sound at a shared witness.** Fix one database and
|
||
one constraint system satisfied there; the read-off range built from the system's
|
||
supports is sound: any database agreeing on it satisfies the same system. This is
|
||
the single-witness soundness the transaction needs, independent of how the
|
||
constraints were collected. -/
|
||
theorem readOff_sound {S : ConstraintSystem} {db : DB}
|
||
(h : SystemHoldsAt S db) :
|
||
∀ db', AgreeOn (hullSupports S |>.getD
|
||
⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩) db db' →
|
||
(SystemHoldsAt S db') := by
|
||
intro db' hagree
|
||
cases hR : hullSupports S with
|
||
| none =>
|
||
-- no constraints: the system is empty and holds trivially
|
||
intro c hc
|
||
rw [hullSupports_eq_none_iff.mp hR] at hc
|
||
exact absurd hc (by simp)
|
||
| some R =>
|
||
rw [hR, Option.getD_some] at hagree
|
||
exact (system_holdsAt_of_supports_hull hR hagree).mp h
|
||
|
||
/-! ## Necessity: a tight support is necessary
|
||
|
||
The constructive direction of necessity. If a collected system *characterizes*
|
||
the read's compatibility, and the flip at `k` violates a constraint whose support
|
||
contains `k`, then `k` lies in every sound range for the read: any sound range
|
||
omitting `k` would see the witness and its flip as agreeing, transferring the
|
||
compatibility that the flip destroys. -/
|
||
|
||
/-- A constraint system characterizes a read/result pair when satisfying the
|
||
system is exactly being compatible with the result. -/
|
||
def Characterizes (S : ConstraintSystem) (r : RangeRead) (res : Result) : Prop :=
|
||
∀ db : DB, RangeRead.Compatible db r res ↔ SystemHoldsAt S db
|
||
|
||
/-- **A key whose flip violates a tight constraint is necessary.** Given a
|
||
characterizing system, a witness `db` satisfying it, a constraint `c ∈ S`, and a
|
||
flip at `k` that violates `c`, the key `k` is `Necessary` for the read/result
|
||
pair. This is the constructive form of necessity: the active set is the union,
|
||
over the shared witness, of the supports of the constraints the witness makes
|
||
tight — and `k` lies in `c`'s support automatically, since a flip off the support
|
||
leaves the constraint unchanged (`constraint_eval_toggle_eq_of_not_mem`). -/
|
||
theorem necessary_of_tight_support {r : RangeRead} {res : Result}
|
||
{S : ConstraintSystem} {db : DB} {k : Key} {c : Constraint}
|
||
(hchar : Characterizes S r res)
|
||
(hc : c ∈ S) (hdb : SystemHoldsAt S db)
|
||
(hviol : ¬ c.HoldsAt (toggle db k)) :
|
||
Necessary r res k := by
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hagree : AgreeOn cr db (toggle db k) := agreeOn_toggle_of_not_mem cr db hkcr
|
||
have hcomp : RangeRead.Compatible db r res := (hchar db).mpr hdb
|
||
have hcomp₁ : RangeRead.Compatible (toggle db k) r res :=
|
||
(hs db (toggle db k) hagree).mp hcomp
|
||
exact hviol (((hchar (toggle db k)).mp hcomp₁) c hc)
|
||
|
||
/-- **Tight-support minimality.** Let `S` characterize `(r, res)`, let the witness
|
||
`db` satisfy `S`, and let `R` be the hull of `S`'s supports. If every key of `R` is
|
||
*witnessed* by a constraint of `S` that the single-key flip at it violates, then `R`
|
||
is the minimal sound conflict range. This is the direct assembly: soundness is
|
||
support containment (`system_holdsAt_of_supports_hull` through the characterization),
|
||
and necessity is `necessary_of_tight_support` at the witnessing constraint.
|
||
|
||
The keys the observations force are exactly the union, over the shared witness, of
|
||
the supports of the constraints the witness makes tight; a key in the support of a
|
||
slack constraint does nothing, and the theorem asks only for a violating constraint
|
||
per key, not that each support be tight throughout. The containment of the slack
|
||
supports (that they do not drag `R` past the witnessed keys) is folded into the
|
||
witness hypothesis, which is what a collector discharges case by case. -/
|
||
theorem minimal_of_characterizing_system {r : RangeRead} {res : Result}
|
||
{S : ConstraintSystem} {db : DB} {R : ConflictRange}
|
||
(hchar : Characterizes S r res) (hdb : SystemHoldsAt S db)
|
||
(hR : hullSupports S = some R)
|
||
(hnec : ∀ k, k ∈ R → ∃ c ∈ S, ¬ c.HoldsAt (toggle db k)) :
|
||
Minimal r res R := by
|
||
refine (minimal_iff_sound_and_necessary).mpr ⟨?_, ?_⟩
|
||
· intro db₁ db₂ hagree
|
||
have hsys : SystemHoldsAt S db₁ ↔ SystemHoldsAt S db₂ :=
|
||
system_holdsAt_of_supports_hull hR hagree
|
||
rw [hchar db₁, hchar db₂, hsys]
|
||
· intro k hk
|
||
obtain ⟨c, hcS, hviol⟩ := hnec k hk
|
||
exact necessary_of_tight_support hchar hcS hdb hviol
|
||
|
||
/-- **Tight-support minimality, support form.** A corollary of
|
||
`minimal_of_characterizing_system` phrased at the supports: if every key of every
|
||
support of `S` is witnessed by a violating flip, and the supports of `S` cover `R`
|
||
(every key of `R` lies in some support), then `R = hullSupports S` is minimal. -/
|
||
theorem minimal_of_tight_supports {r : RangeRead} {res : Result}
|
||
{S : ConstraintSystem} {db : DB} {R : ConflictRange}
|
||
(hchar : Characterizes S r res) (hdb : SystemHoldsAt S db)
|
||
(hR : hullSupports S = some R)
|
||
(hwit : ∀ c ∈ S, ∀ k ∈ c.supp, ∃ c' ∈ S, ¬ c'.HoldsAt (toggle db k))
|
||
(hcover : ∀ k, k ∈ R → ∃ c ∈ S, k ∈ c.supp) :
|
||
Minimal r res R :=
|
||
minimal_of_characterizing_system hchar hdb hR fun k hk =>
|
||
let ⟨c, hcS, hksupp⟩ := hcover k hk
|
||
hwit c hcS k hksupp
|
||
|
||
/-- **The tight-support read-off is *the* minimal range, up to set equality.** The
|
||
uniqueness companion of `minimal_of_characterizing_system`: any other minimal sound
|
||
range denotes the same key set, by the unconditional `Minimal.toSet_eq`. -/
|
||
theorem unique_minimal_of_characterizing_system {r : RangeRead} {res : Result}
|
||
{S : ConstraintSystem} {db : DB} {R : ConflictRange}
|
||
(hchar : Characterizes S r res) (hdb : SystemHoldsAt S db)
|
||
(hR : hullSupports S = some R)
|
||
(hnec : ∀ k, k ∈ R → ∃ c ∈ S, ¬ c.HoldsAt (toggle db k)) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal
|
||
(minimal_of_characterizing_system hchar hdb hR hnec)
|
||
|
||
/-! ## The disjunctive collector: feasibility-gated read-off
|
||
|
||
A general read's collected object is a `ConstraintDNF`, not a conjunction: an empty
|
||
result's `ψ` is a disjunction, and its alternatives become conjunctive alternatives
|
||
of the collector. The candidate read-off must therefore be formed over the
|
||
alternatives that *could be live* --- those some database satisfies --- rather than a
|
||
single live alternative. Hulling only the live alternative's support is unsound: for
|
||
a crossed empty read the live alternative `ρ(E) − ρ(B) ≤ oB − oE` has support
|
||
`[E, B)`, yet the minimal range is `[E, ⊤)`, because the *other* feasible
|
||
alternative `ρ(⊤) − ρ(B) ≤ oB − 1` (support `[B, ⊤)`) can be the live one at another
|
||
compatible database. Since compatibility is the disjunction over the feasible
|
||
alternatives, agreement on the hull of their supports fixes every feasible
|
||
alternative, hence the disjunction.
|
||
|
||
Necessity is correspondingly disjunctive: a key is forced when the single-key flip
|
||
breaks *every* alternative (`¬ AnyHolds`), since the flip could otherwise switch to a
|
||
different feasible alternative. -/
|
||
|
||
/-- A DNF characterizes a read/result pair when compatibility is exactly the limit
|
||
together with the disjunction holding. This is the `ConstraintDNF` analogue of
|
||
`Characterizes`; the collector's `preciseCharacterizes` discharges it. -/
|
||
def CharacterizesDNF (D : ConstraintDNF) (r : RangeRead) (res : Result) : Prop :=
|
||
∀ db : DB, RangeRead.Compatible db r res ↔
|
||
withinLimit r res.keys ∧ AnyHolds D db
|
||
|
||
/-- **Disjunctive necessity: a flip that breaks every alternative forces its key.**
|
||
Given a characterizing DNF and a compatible witness, if the single-key flip at `k`
|
||
makes `AnyHolds` fail, then `k` is necessary. The proof is the toggle argument once
|
||
more: any sound range omitting `k` sees the witness and its flip as agreeing, so
|
||
soundness would carry compatibility to the flip, contradicting the failed
|
||
disjunction. -/
|
||
theorem necessary_of_tight_alt {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{db : DB} {k : Key}
|
||
(hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hviol : ¬ AnyHolds D (toggle db k)) :
|
||
Necessary r res k := by
|
||
intro cr hs
|
||
by_contra hkcr
|
||
have hagree : AgreeOn cr db (toggle db k) := agreeOn_toggle_of_not_mem cr db hkcr
|
||
have hcomp' : RangeRead.Compatible (toggle db k) r res :=
|
||
(hs db (toggle db k) hagree).mp hcomp
|
||
exact hviol ((hchar (toggle db k)).mp hcomp').2
|
||
|
||
/-- **Disjunctive soundness from a feasible-support cover.** If every constraint of
|
||
every *feasible* alternative has its support inside `R`, then `R` is sound: a
|
||
compatible database has some live alternative, which is feasible, so agreement on
|
||
`R` fixes all of that alternative's constraints and hence keeps the disjunction
|
||
true.
|
||
|
||
The hypothesis is *not* the one to target at the concrete collector: for
|
||
`emptyConstraints` the globally-feasible `nilDb` alternative has full support, making
|
||
this cover false on a trivially-empty read (see
|
||
`emptyConstraints_cover_false_on_readPinned`). Use `sound_of_live_supports` instead. -/
|
||
theorem sound_of_feasible_supports {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{R : ConflictRange} (hchar : CharacterizesDNF D r res)
|
||
(hsub : ∀ S, S ∈ D → (∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp R) :
|
||
Sound r res R := by
|
||
intro db₁ db₂ hagree
|
||
rw [hchar db₁, hchar db₂]
|
||
refine ⟨fun h => ?_, fun h => ?_⟩
|
||
· obtain ⟨hlim, S, hS, hSh⟩ := h
|
||
exact ⟨hlim, S, hS, fun c hc => by
|
||
change c.eval db₂ ≤ c.bound
|
||
rw [← constraint_eval_eq_of_agree hagree (hsub S hS ⟨db₁, hSh⟩ c hc)]
|
||
exact hSh c hc⟩
|
||
· obtain ⟨hlim, S, hS, hSh⟩ := h
|
||
exact ⟨hlim, S, hS, fun c hc => by
|
||
change c.eval db₁ ≤ c.bound
|
||
rw [constraint_eval_eq_of_agree hagree (hsub S hS ⟨db₂, hSh⟩ c hc)]
|
||
exact hSh c hc⟩
|
||
|
||
/-- **Disjunctive tight-support minimality.** Given a characterizing DNF, a
|
||
compatible witness, soundness of `R`, and that every key of `R` has its flip break
|
||
the whole disjunction, `R` is minimal. Soundness and necessity are the two halves of
|
||
`minimal_iff_sound_and_necessary`. -/
|
||
theorem minimal_of_characterizing_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{db : DB} {R : ConflictRange}
|
||
(hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hsound : Sound r res R)
|
||
(hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) :
|
||
Minimal r res R :=
|
||
(minimal_iff_sound_and_necessary).mpr
|
||
⟨hsound, fun k hk => necessary_of_tight_alt hchar hcomp (hnec k hk)⟩
|
||
|
||
/-- **Disjunctive tight-support minimality, assembled.** The feasible-support cover
|
||
supplies soundness, so the only hypotheses are the characterization, a compatible
|
||
witness, the cover, and the flip-breaking necessity for `R`'s keys. -/
|
||
theorem minimal_of_feasible_supports_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{db : DB} {R : ConflictRange}
|
||
(hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hsub : ∀ S, S ∈ D → (∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp R)
|
||
(hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) :
|
||
Minimal r res R :=
|
||
minimal_of_characterizing_DNF hchar hcomp (sound_of_feasible_supports hchar hsub) hnec
|
||
|
||
/-- **Disjunctive soundness from a live-support cover.** The correct cover is not
|
||
over globally feasible alternatives but over the alternatives *live at each compatible
|
||
database*: for every compatible `db` there is an alternative holding at `db` whose
|
||
supports lie in `R`. Then `R` is sound — the transfer from `db₁` to an agreeing `db₂`
|
||
uses the alternative live at `db₁`, whose constraints `db₂` fixes. This avoids the
|
||
`nilDb` trap: the globally-feasible full-support alternative is never forced on a
|
||
compatible database that has another live disjunct. -/
|
||
theorem sound_of_live_supports {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{R : ConflictRange} (hchar : CharacterizesDNF D r res)
|
||
(hsub : ∀ db, RangeRead.Compatible db r res → ∃ S ∈ D,
|
||
SystemHoldsAt S db ∧ ∀ c ∈ S, Subrange c.supp R) :
|
||
Sound r res R := by
|
||
intro db₁ db₂ hagree
|
||
rw [hchar db₁, hchar db₂]
|
||
refine ⟨fun h => ?_, fun h => ?_⟩
|
||
· obtain ⟨S, hS, hSh, hsupp⟩ := hsub db₁ ((hchar db₁).mpr h)
|
||
exact ⟨h.1, S, hS, fun c hc => by
|
||
change c.eval db₂ ≤ c.bound
|
||
rw [← constraint_eval_eq_of_agree hagree (hsupp c hc)]
|
||
exact hSh c hc⟩
|
||
· obtain ⟨S, hS, hSh, hsupp⟩ := hsub db₂ ((hchar db₂).mpr h)
|
||
exact ⟨h.1, S, hS, fun c hc => by
|
||
change c.eval db₁ ≤ c.bound
|
||
rw [constraint_eval_eq_of_agree hagree (hsupp c hc)]
|
||
exact hSh c hc⟩
|
||
|
||
/-- **Disjunctive tight-support minimality, live-support form.** The corrected
|
||
assembly: the live-support cover supplies soundness, the flip-breaking condition
|
||
supplies necessity. This is the DNF companion of `minimal_of_characterizing_system`,
|
||
and the form a concrete read-off must target. -/
|
||
theorem minimal_of_live_supports_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{db : DB} {R : ConflictRange}
|
||
(hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hsub : ∀ db', RangeRead.Compatible db' r res → ∃ S ∈ D,
|
||
SystemHoldsAt S db' ∧ ∀ c ∈ S, Subrange c.supp R)
|
||
(hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) :
|
||
Minimal r res R :=
|
||
minimal_of_characterizing_DNF hchar hcomp (sound_of_live_supports hchar hsub) hnec
|
||
|
||
/-- **The collector characterizes.** `preciseCharacterizes` reads exactly as
|
||
`CharacterizesDNF` at the collector, for nonempty ascending reported keys, with the
|
||
limit factored out. -/
|
||
theorem characterizesDNF_collectOf {r : RangeRead} {res : Result}
|
||
(hA : ascendingKeys r res ≠ []) (hpair : (ascendingKeys r res).Pairwise (· < ·)) :
|
||
CharacterizesDNF (collectOf r (ascendingKeys r res) res.more) r res :=
|
||
fun db => preciseCharacterizes hA hpair db
|
||
|
||
/-- **The disjunctive read-off is *the* minimal range, up to set equality.** The
|
||
uniqueness companion of `minimal_of_characterizing_DNF`. -/
|
||
theorem unique_minimal_of_characterizing_DNF {r : RangeRead} {res : Result}
|
||
{D : ConstraintDNF} {db : DB} {R : ConflictRange}
|
||
(hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hsound : Sound r res R)
|
||
(hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) :
|
||
∃ cr : ConflictRange, Minimal r res cr ∧
|
||
∀ cr' : ConflictRange, Minimal r res cr' → cr'.toSet = cr.toSet :=
|
||
exists_unique_minimal_of_minimal
|
||
(minimal_of_characterizing_DNF hchar hcomp hsound hnec)
|
||
|
||
/-! ## The `nilDb` alternative blocks the feasible-support cover
|
||
|
||
The disjunctive assembly `sound_of_feasible_supports` asks that every constraint of
|
||
every *feasible* alternative have its support inside `R`. For the empty read's
|
||
`emptyConstraints`, the fourth alternative `emptyConstraintNilDb` is feasible — the
|
||
empty database satisfies `ρ(⊤) − ρ(⊥) ≤ 0` — and its single constraint has full
|
||
support `[⊥, ⊤)`. So the cover forces every key into `R`. But a trivially-empty read
|
||
has `emptyRange` as its minimal range, which contains no key. Hence the cover is
|
||
*false* for `emptyConstraints` on a trivially-empty read, and the feasible-support
|
||
form of the direct assembly cannot be instantiated there as it stands.
|
||
|
||
The obstacle is structural: `Feasible` (`∃ db, SystemHoldsAt S db`) is *not* the right
|
||
weakening, because the empty database — which satisfies `nilDb` — is not compatible
|
||
with any nonempty read and pins down no key. The cover must hull the supports of the
|
||
alternatives that are live at a database *compatible with the read*, not merely at
|
||
some database. This is the read-level feasibility the plan already isolated for
|
||
`shortGap`; the `nilDb` alternative shows the two notions are not equivalent, and it
|
||
is exactly why the concrete read-off `R` cannot be `hullSupports` over all feasible
|
||
alternatives. -/
|
||
|
||
/-- **The empty-database alternative is feasible with full support.** Its one
|
||
constraint `ρ(⊤) − ρ(⊥) ≤ 0` holds at the empty database, and its support is
|
||
`[⊥, ⊤)`, so it contains every key. -/
|
||
theorem emptyConstraintNilDb_feasible_and_supp (r : RangeRead) :
|
||
SystemHoldsAt [emptyConstraintNilDb r] emptyDB ∧
|
||
∀ k : Key, k ∈ (emptyConstraintNilDb r).supp := by
|
||
refine ⟨?_, fun k => ?_⟩
|
||
· intro c hc
|
||
rw [List.mem_singleton] at hc
|
||
subst hc
|
||
simp only [emptyConstraintNilDb, Constraint.HoldsAt, Constraint.eval, Constraint.le,
|
||
emptyDB, cnt_top, cnt_nil]
|
||
norm_num
|
||
· simp only [emptyConstraintNilDb, Constraint.le, Constraint.supp]
|
||
rw [ConflictRange.mem_iff]
|
||
exact ⟨le_trans (min_le_left _ _) (WithTop.coe_le_coe.mpr (nil_le_key k)),
|
||
by rw [max_eq_right le_top]; exact WithTop.coe_lt_top k⟩
|
||
|
||
/-- **The feasible-support cover is false for `emptyConstraints` on a trivially-empty
|
||
read.** The `nilDb` alternative is feasible via the empty database but its support is
|
||
full, while `emptyRange` holds no key. -/
|
||
theorem emptyConstraints_cover_false_on_readPinned :
|
||
¬ (∀ S, S ∈ emptyConstraints readPinned →
|
||
(∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp (emptyRange readPinned)) := by
|
||
intro h
|
||
have hsupp := (emptyConstraintNilDb_feasible_and_supp readPinned).2
|
||
have hmem : [emptyConstraintNilDb readPinned] ∈ emptyConstraints readPinned := by
|
||
simp [emptyConstraints]
|
||
have hfeas : ∃ d : DB, SystemHoldsAt [emptyConstraintNilDb readPinned] d :=
|
||
⟨emptyDB, (emptyConstraintNilDb_feasible_and_supp readPinned).1⟩
|
||
exact not_mem_emptyRange readPinned ([] : Key)
|
||
(h _ hmem hfeas _ (List.mem_singleton.mpr rfl) ([] : Key) (hsupp ([] : Key)))
|
||
|
||
/-- **The intended empty-read instantiation is blocked.** There is a realizable
|
||
trivially-empty read whose minimal range is the empty range, yet for which the
|
||
feasible-support cover `hsub` of `sound_of_feasible_supports` is false. So the
|
||
feasible-support form of the direct assembly cannot be discharged against
|
||
`emptyConstraints` without refining "feasible" to read-level feasibility. -/
|
||
theorem emptyConstraints_feasible_cover_blocked :
|
||
∃ r : RangeRead, Readable r emptyRes ∧ Minimal r emptyRes (emptyRange r) ∧
|
||
¬ (∀ S, S ∈ emptyConstraints r →
|
||
(∃ d : DB, SystemHoldsAt S d) → ∀ c ∈ S, Subrange c.supp (emptyRange r)) :=
|
||
⟨readPinned, ⟨emptyDB, compatible_emptyDB_emptyRes readPinned⟩,
|
||
minimal_emptyRange_of_alwaysEmpty alwaysEmpty_readPinned,
|
||
emptyConstraints_cover_false_on_readPinned⟩
|
||
|
||
/-- **The live-support cover is *satisfied* at `readPinned`, by the nonpositive
|
||
alternative.** Every compatible database (there is only the empty one, and in fact any
|
||
database at all) makes the third alternative
|
||
`begin_nonpos ∧ end_nonpos` hold; both its constraints have empty support, which is
|
||
contained in the empty range. So the corrected cover is not vacuous — the same read
|
||
that refutes the feasible cover satisfies the live one. -/
|
||
theorem emptyConstraints_live_cover_readPinned :
|
||
∀ db, RangeRead.Compatible db readPinned emptyRes →
|
||
∃ S ∈ emptyConstraints readPinned, SystemHoldsAt S db ∧
|
||
∀ c ∈ S, Subrange c.supp (emptyRange readPinned) := by
|
||
intro db _
|
||
refine ⟨[emptyConstraintBeginNonpos readPinned, emptyConstraintEndNonpos readPinned],
|
||
by simp [emptyConstraints], ?_, ?_⟩
|
||
· intro c hc
|
||
simp only [List.mem_cons, List.not_mem_nil, or_false] at hc
|
||
rcases hc with rfl | rfl
|
||
· simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintBeginNonpos,
|
||
Constraint.le, readPinned, cnt_nil]
|
||
norm_num
|
||
· simp only [Constraint.HoldsAt, Constraint.eval, emptyConstraintEndNonpos,
|
||
Constraint.le, readPinned, cnt_nil]
|
||
norm_num
|
||
· intro c hc
|
||
simp only [List.mem_cons, List.not_mem_nil, or_false] at hc
|
||
rcases hc with rfl | rfl <;>
|
||
intro k hk <;>
|
||
· simp only [emptyConstraintBeginNonpos, emptyConstraintEndNonpos, Constraint.le,
|
||
Constraint.supp, readPinned, min_self, max_self, emptyRange] at hk ⊢
|
||
exact absurd hk.2 (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k)))
|
||
|
||
/-! ## Support containment is only sufficient: the exact criterion is observability
|
||
|
||
The live-support cover repairs the `nilDb` trap, but it is *still* too strong, and the
|
||
gap is not an artefact of one alternative. Soundness of a DNF is a statement about
|
||
*whether the disjunction holds*, not about where its supports lie: two databases can
|
||
disagree on the supports of every alternative while still agreeing on the truth value
|
||
of the disjunction, because a *change of branch* can compensate. Support containment is
|
||
a sufficient condition — `observable_of_support` — but not a necessary one.
|
||
|
||
The clean example is an always-empty read with **crossed nonempty selectors**:
|
||
`begin = ⟨[1], 1⟩`, `end = ⟨[0], 1⟩`. Every database has `completeKeys = []`
|
||
(`alwaysEmpty_crossRead`), because the begin anchor `ρ([1])` is at least the end anchor
|
||
`ρ([0])`, so the read is trivially empty and its minimal range is `emptyRange`, which
|
||
holds no key. Yet `emptyConstraints` has no alternative with empty support: `endLe`
|
||
spans `[[0,0], [1])`, `beginTop` spans `[[1], ⊤)`, and the two nonpositive alternatives
|
||
each span up to `[0]` (`crossRead_no_alt_supports_empty`). Hence even the live-support
|
||
cover is *false* here (`crossRead_support_cover_false`), while the empty range is sound
|
||
because the disjunction is constantly true.
|
||
|
||
The correct criterion is therefore not support containment but **observability**:
|
||
agreement on `R` fixes whether the disjunction holds. -/
|
||
|
||
/-- A DNF is **observable** on a range `R` when agreement on `R` fixes whether the
|
||
disjunction holds. This is the exact soundness content for a DNF, weaker than support
|
||
containment: a range can miss an alternative's support yet still see the disjunction,
|
||
because the missed information only ever moves a database between alternatives. -/
|
||
def Observable (D : ConstraintDNF) (R : ConflictRange) : Prop :=
|
||
∀ db₁ db₂, AgreeOn R db₁ db₂ → (AnyHolds D db₁ ↔ AnyHolds D db₂)
|
||
|
||
/-- **Observability gives soundness.** For a characterizing DNF, if agreement on `R`
|
||
fixes the disjunction then `R` is sound. This is the exact form; support containment is
|
||
the special case `Observable.of_support`. -/
|
||
theorem sound_of_observable {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{R : ConflictRange} (hchar : CharacterizesDNF D r res) (hobs : Observable D R) :
|
||
Sound r res R := by
|
||
intro db₁ db₂ hagree
|
||
rw [hchar db₁, hchar db₂]
|
||
exact ⟨fun h => ⟨h.1, (hobs db₁ db₂ hagree).mp h.2⟩,
|
||
fun h => ⟨h.1, (hobs db₁ db₂ hagree).mpr h.2⟩⟩
|
||
|
||
/-- **Soundness gives observability**, given the limit is fixed. For a characterizing
|
||
DNF and a result within the limit, the range's soundness is exactly observability of
|
||
the disjunction. The limit side condition is unavoidable: `Sound` also transports the
|
||
limit, which the disjunction does not see. -/
|
||
theorem observable_of_sound {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{R : ConflictRange} (hchar : CharacterizesDNF D r res)
|
||
(hwl : RangeRead.withinLimit r res.keys) (hs : Sound r res R) :
|
||
Observable D R := by
|
||
intro db₁ db₂ hagree
|
||
have h1 := hs db₁ db₂ hagree
|
||
rw [hchar db₁, hchar db₂] at h1
|
||
constructor
|
||
· intro h; exact (h1.mp ⟨hwl, h⟩).2
|
||
· intro h; exact (h1.mpr ⟨hwl, h⟩).2
|
||
|
||
/-- **Support containment is sufficient for observability.** If every alternative's
|
||
every support lies in `R`, agreement on `R` fixes each constraint and hence the
|
||
disjunction. The converse is false (`crossRead_support_cover_false`). -/
|
||
theorem observable_of_support {D : ConstraintDNF} {R : ConflictRange}
|
||
(hsub : ∀ S ∈ D, ∀ c ∈ S, Subrange c.supp R) : Observable D R := by
|
||
intro db₁ db₂ hagree
|
||
constructor <;> rintro ⟨S, hS, hSh⟩
|
||
· exact ⟨S, hS, fun c hc => by
|
||
change c.eval db₂ ≤ c.bound
|
||
rw [← constraint_eval_eq_of_agree hagree (hsub S hS c hc)]
|
||
exact hSh c hc⟩
|
||
· exact ⟨S, hS, fun c hc => by
|
||
change c.eval db₁ ≤ c.bound
|
||
rw [constraint_eval_eq_of_agree hagree (hsub S hS c hc)]
|
||
exact hSh c hc⟩
|
||
|
||
/-- **Disjunctive tight-support minimality, observability form.** The exact assembly:
|
||
observability supplies soundness (via `sound_of_observable`), the flip-breaking
|
||
condition supplies necessity. A concrete read-off must establish observability of the
|
||
collector's DNF on `R`, not support containment. -/
|
||
theorem minimal_of_observable_DNF {r : RangeRead} {res : Result} {D : ConstraintDNF}
|
||
{db : DB} {R : ConflictRange}
|
||
(hchar : CharacterizesDNF D r res) (hcomp : RangeRead.Compatible db r res)
|
||
(hobs : Observable D R)
|
||
(hnec : ∀ k, k ∈ R → ¬ AnyHolds D (toggle db k)) :
|
||
Minimal r res R :=
|
||
minimal_of_characterizing_DNF hchar hcomp (sound_of_observable hchar hobs) hnec
|
||
|
||
/-! ### A crossed always-empty read refutes support containment
|
||
|
||
`begin = ⟨[1], 1⟩`, `end = ⟨[0], 1⟩`: crossed nonempty selectors, trivially empty. -/
|
||
|
||
/-- The key `[0]`. -/
|
||
def crossKey0 : Key := [(0 : Byte)]
|
||
|
||
/-- The key `[1]`. -/
|
||
def crossKey1 : Key := [(1 : Byte)]
|
||
|
||
/-- The key `[0, 0]`, strictly between `[0]` and `[1]`. -/
|
||
def crossKey00 : Key := [(0 : Byte), (0 : Byte)]
|
||
|
||
/-- A crossed read: begin keyed at `[1]`, end keyed at `[0]`, both at offset one. -/
|
||
def crossRead : RangeRead where
|
||
begin := ⟨(crossKey1 : WithTop Key), 1⟩
|
||
end_ := ⟨(crossKey0 : WithTop Key), 1⟩
|
||
limit := none
|
||
reverse := false
|
||
|
||
/-- **The crossed read is trivially empty.** Its begin anchor `ρ([1])` is at least its
|
||
end anchor `ρ([0])`, so the resolved window is always empty. -/
|
||
theorem alwaysEmpty_crossRead : AlwaysEmpty crossRead := by
|
||
intro db
|
||
rw [show RangeRead.completeKeys db crossRead = [] ↔
|
||
¬ (clampIndex db crossRead.begin < clampIndex db crossRead.end_) from by
|
||
rw [← completeKeys_ne_nil_iff]; simp]
|
||
intro hlt
|
||
rw [clampIndex_begin_eq (db := db) (r := crossRead) rfl,
|
||
clampIndex_end_eq (db := db) (r := crossRead)] at hlt
|
||
simp only [crossRead] at hlt
|
||
have hle : cnt db (↑crossKey0) ≤ cnt db (↑crossKey1) :=
|
||
cnt_le_cnt_of_le db (by decide)
|
||
omega
|
||
|
||
/-- The collector characterizes the crossed read's empty result. -/
|
||
theorem crossRead_characterizes :
|
||
CharacterizesDNF (emptyConstraints crossRead) crossRead emptyRes := by
|
||
intro db
|
||
change RangeRead.Compatible db crossRead { keys := [], more := false } ↔ _
|
||
rw [compatible_empty_iff_complete_nil]
|
||
refine ⟨fun h => ⟨?_, completeKeys_eq_nil_iff_emptyConstraints.mp h⟩,
|
||
fun h => completeKeys_eq_nil_iff_emptyConstraints.mpr h.2⟩
|
||
unfold RangeRead.withinLimit; simp [crossRead]
|
||
|
||
/-- **The empty range is observable on the crossed read**, because the collector is
|
||
constantly satisfied there. -/
|
||
theorem crossRead_observable_emptyRange :
|
||
Observable (emptyConstraints crossRead) (emptyRange crossRead) :=
|
||
observable_of_sound crossRead_characterizes trivial
|
||
(sound_emptyRange_of_alwaysEmpty alwaysEmpty_crossRead)
|
||
|
||
/-- A range containing a key cannot be a subrange of the empty range. -/
|
||
theorem not_subrange_emptyRange_of_mem {r : RangeRead} {cr : ConflictRange} {k : Key}
|
||
(hk : k ∈ cr) : ¬ Subrange cr (emptyRange r) := by
|
||
intro h
|
||
have hk' := h k hk
|
||
simp only [emptyRange, ConflictRange.mem_iff] at hk'
|
||
exact absurd hk'.2 (not_lt.mpr hk'.1)
|
||
|
||
/-- **No alternative of `emptyConstraints crossRead` has all supports in the empty
|
||
range.** The four alternatives have supports `[[0,0],[1))`, `[[1],⊤)`, `[⊥,[0])`, and
|
||
`[⊥,⊤)` respectively, each nonempty. -/
|
||
theorem crossRead_no_alt_supports_empty :
|
||
∀ S ∈ emptyConstraints crossRead, ¬ (∀ c ∈ S, Subrange c.supp (emptyRange crossRead)) := by
|
||
intro S hS h
|
||
simp only [emptyConstraints, List.mem_cons, List.not_mem_nil, or_false] at hS
|
||
rcases hS with rfl | rfl | rfl | rfl
|
||
· exact not_subrange_emptyRange_of_mem (r := crossRead)
|
||
(cr := (emptyConstraintEndLe crossRead).supp) (k := crossKey00) (by
|
||
simp only [emptyConstraintEndLe, Constraint.le, Constraint.supp, ConflictRange.mem_iff,
|
||
crossRead, crossKey0, crossKey1, crossKey00]
|
||
constructor <;> decide)
|
||
(h _ (List.mem_singleton.mpr rfl))
|
||
· exact not_subrange_emptyRange_of_mem (r := crossRead)
|
||
(cr := (emptyConstraintBeginTop crossRead).supp) (k := crossKey1) (by
|
||
simp only [emptyConstraintBeginTop, Constraint.supp, ConflictRange.mem_iff,
|
||
crossRead, crossKey0, crossKey1]
|
||
constructor <;> decide)
|
||
(h _ (List.mem_singleton.mpr rfl))
|
||
· exact not_subrange_emptyRange_of_mem (r := crossRead)
|
||
(cr := (emptyConstraintBeginNonpos crossRead).supp) (k := ([] : Key)) (by
|
||
simp only [emptyConstraintBeginNonpos, Constraint.le, Constraint.supp,
|
||
ConflictRange.mem_iff, crossRead, crossKey0, crossKey1]
|
||
constructor <;> decide)
|
||
(h _ (by simp))
|
||
· exact not_subrange_emptyRange_of_mem (r := crossRead)
|
||
(cr := (emptyConstraintNilDb crossRead).supp) (k := ([] : Key)) (by
|
||
simp only [emptyConstraintNilDb, Constraint.le, Constraint.supp,
|
||
ConflictRange.mem_iff]
|
||
exact ⟨by simp, WithTop.coe_lt_top _⟩)
|
||
(h _ (List.mem_singleton.mpr rfl))
|
||
|
||
/-- **The support cover (feasible or live) is false on the crossed read.** No
|
||
alternative of `emptyConstraints` has all its supports in the minimal range
|
||
`emptyRange`, yet that range is sound — so support containment is not necessary for
|
||
soundness. -/
|
||
theorem crossRead_support_cover_false :
|
||
¬ (∀ S ∈ emptyConstraints crossRead, ∀ c ∈ S, Subrange c.supp (emptyRange crossRead)) :=
|
||
fun h => crossRead_no_alt_supports_empty [emptyConstraintEndLe crossRead]
|
||
(by simp [emptyConstraints]) (h _ (by simp [emptyConstraints]))
|
||
|
||
/-! ### The observability assembly reproduces the empty theory
|
||
|
||
With the exact criterion in hand, the direct assembly closes the empty-read instance
|
||
end-to-end: `emptyConstraints` characterizes the empty result, its `Observable` is
|
||
soundness of `emptyValue` (read off the *proven* witness oracle), and the keys of
|
||
`emptyValue` are necessary by their flip witnesses. The result is `emptyValue`'s
|
||
minimality, but reached through the DNF assembly rather than the dedicated empty-theory
|
||
route — the validation that the direct formulation is not merely a certificate rule. -/
|
||
|
||
/-- **`emptyConstraints` characterizes the empty result**, with the limit factored out:
|
||
an empty result is compatible exactly when the four-way disjunction holds
|
||
(`completeKeys_eq_nil_iff_emptyConstraints`). -/
|
||
theorem characterizesDNF_emptyConstraints (r : RangeRead) :
|
||
CharacterizesDNF (emptyConstraints r) r emptyRes := by
|
||
intro db
|
||
change RangeRead.Compatible db r { keys := [], more := false } ↔ _
|
||
rw [compatible_empty_iff_complete_nil]
|
||
refine ⟨fun h => ⟨?_, completeKeys_eq_nil_iff_emptyConstraints.mp h⟩,
|
||
fun h => completeKeys_eq_nil_iff_emptyConstraints.mpr h.2⟩
|
||
unfold RangeRead.withinLimit
|
||
cases r.limit with
|
||
| none => trivial
|
||
| some n => exact Nat.zero_le _
|
||
|
||
/-- **Observability assembly, necessity in range form.** Like
|
||
`minimal_of_observable_DNF` but taking the range's keys as *necessary* directly, which
|
||
is the form the proven empty theory supplies. -/
|
||
theorem minimal_of_observable_necessary_DNF {r : RangeRead} {res : Result}
|
||
{D : ConstraintDNF} {R : ConflictRange}
|
||
(hchar : CharacterizesDNF D r res) (hobs : Observable D R)
|
||
(hnec : ∀ k, k ∈ R → Necessary r res k) :
|
||
Minimal r res R :=
|
||
(minimal_iff_sound_and_necessary).mpr ⟨sound_of_observable hchar hobs, hnec⟩
|
||
|
||
/-- **The empty read closes through the observability assembly.** For every realizable
|
||
empty read, `emptyValue` is the minimal sound range, derived from the DNF assembly
|
||
rather than the dedicated empty-theory proofs: `emptyConstraints` characterizes, the
|
||
witness oracle gives soundness and hence observability, and each key of `emptyValue` is
|
||
necessary by its flip witness. -/
|
||
theorem minimal_emptyValue_via_observable {r : RangeRead} (hread : Readable r emptyRes) :
|
||
Minimal r emptyRes (emptyValue r) := by
|
||
have hchar := characterizesDNF_emptyConstraints r
|
||
obtain ⟨hsnd, hwit⟩ := emptyInferenceWitness r hread
|
||
have hwl : RangeRead.withinLimit r emptyRes.keys := by
|
||
unfold RangeRead.withinLimit
|
||
cases r.limit with
|
||
| none => trivial
|
||
| some n => exact Nat.zero_le _
|
||
exact minimal_of_observable_necessary_DNF hchar
|
||
(observable_of_sound hchar hwl hsnd)
|
||
(fun _ hk => necessary_of_toggle (hwit _ hk))
|
||
|
||
/-! ### Composition: the collector's observability reduces to its parts
|
||
|
||
Observability is a congruence for the collector's construction, so the general
|
||
soundness obligation can be pushed onto the boundary reads and the middle:
|
||
|
||
* `Observable.mono` — enlarging `R` preserves observability;
|
||
* `observable_conjDNF` / `observable_conjDNFs` — a conjunction of observable DNFs is
|
||
observable (the disjunction is the conjunction of the alternatives' disjunctions);
|
||
* `observable_append_of_support` — prepending a fixed system whose supports lie in `R`
|
||
preserves observability;
|
||
* `observable_emptyConstraints_iff_sound` — a boundary read's DNF is observable exactly
|
||
when that read's empty theory is sound.
|
||
|
||
Together these give `observable_collectOf_of_boundary`: `collectOf` is observable on `R`
|
||
as soon as every required boundary read's empty theory is sound on `R` and the
|
||
bracket/core supports lie in `R`. The boundary side is the *proven* empty theory, so the
|
||
residual soundness obligation for the general read is only the middle's support
|
||
containment. -/
|
||
|
||
/-- Observability is monotone in the range: a range containing an observable one sees
|
||
the disjunction too. -/
|
||
theorem Observable.mono {D : ConstraintDNF} {R R' : ConflictRange}
|
||
(h : Observable D R) (hsub : Subrange R R') : Observable D R' :=
|
||
fun db₁ db₂ hagree => h db₁ db₂ (keys_eq_of_subrange_agree hsub hagree)
|
||
|
||
/-- A conjunction of observables is observable. -/
|
||
theorem observable_conjDNF {D₁ D₂ : ConstraintDNF} {R : ConflictRange}
|
||
(h₁ : Observable D₁ R) (h₂ : Observable D₂ R) :
|
||
Observable (conjDNF D₁ D₂) R := by
|
||
intro db₁ db₂ hagree
|
||
rw [anyHolds_conjDNF, anyHolds_conjDNF, h₁ db₁ db₂ hagree, h₂ db₁ db₂ hagree]
|
||
|
||
/-- A finite conjunction of observables is observable. -/
|
||
theorem observable_conjDNFs {Ds : List ConstraintDNF} {R : ConflictRange}
|
||
(h : ∀ D ∈ Ds, Observable D R) : Observable (conjDNFs Ds) R := by
|
||
induction Ds with
|
||
| nil => intro db₁ db₂ _; simp [conjDNFs, AnyHolds, SystemHoldsAt]
|
||
| cons D Ds ih =>
|
||
rw [conjDNFs]
|
||
exact observable_conjDNF (h D (by simp)) (ih fun D' hD' => h D' (by simp [hD']))
|
||
|
||
/-- Appending a fixed system to each alternative distributes over `AnyHolds`. -/
|
||
theorem anyHolds_map_append {D : ConstraintDNF} {F : ConstraintSystem} {db : DB} :
|
||
AnyHolds (D.map (fun S => F ++ S)) db ↔ AnyHolds [F] db ∧ AnyHolds D db := by
|
||
constructor
|
||
· rintro ⟨S, hS, hSh⟩
|
||
rw [List.mem_map] at hS
|
||
obtain ⟨S', hS', rfl⟩ := hS
|
||
exact ⟨⟨F, by simp, fun c hc => hSh c (by simp [hc])⟩,
|
||
⟨S', hS', fun c hc => hSh c (by simp [hc])⟩⟩
|
||
· rintro ⟨⟨F', hF', hF'h⟩, ⟨S', hS', hS'h⟩⟩
|
||
have hFeq : F' = F := List.mem_singleton.mp hF'
|
||
rw [hFeq] at hF'h
|
||
exact ⟨F ++ S', List.mem_map.mpr ⟨S', hS', rfl⟩, fun c hc => by
|
||
rcases List.mem_append.mp hc with hc | hc
|
||
· exact hF'h c hc
|
||
· exact hS'h c hc⟩
|
||
|
||
/-- Prepending a fixed system whose supports lie in `R` preserves observability. -/
|
||
theorem observable_append_of_support {D : ConstraintDNF} {F : ConstraintSystem}
|
||
{R : ConflictRange} (hsub : ∀ c ∈ F, Subrange c.supp R)
|
||
(h : Observable D R) : Observable (D.map (fun S => F ++ S)) R := by
|
||
have hF : Observable [F] R :=
|
||
observable_of_support (fun S' hS' => (List.mem_singleton.mp hS').symm ▸ hsub)
|
||
intro db₁ db₂ hagree
|
||
rw [anyHolds_map_append, anyHolds_map_append, hF db₁ db₂ hagree, h db₁ db₂ hagree]
|
||
|
||
/-- A boundary read's collected DNF is observable on `R` exactly when that read's empty
|
||
theory is sound on `R`. -/
|
||
theorem observable_emptyConstraints_iff_sound {s : RangeRead} {R : ConflictRange} :
|
||
Observable (emptyConstraints s) R ↔ Sound s emptyRes R := by
|
||
refine ⟨sound_of_observable (characterizesDNF_emptyConstraints s), fun hs => ?_⟩
|
||
have hwl : RangeRead.withinLimit s emptyRes.keys := by
|
||
unfold RangeRead.withinLimit
|
||
cases s.limit with
|
||
| none => trivial
|
||
| some n => exact Nat.zero_le _
|
||
exact observable_of_sound (characterizesDNF_emptyConstraints s) hwl hs
|
||
|
||
/-- **The collector's observability reduces to its parts.** If every required boundary
|
||
read's empty theory is sound on `R` and the bracket/core supports lie in `R`, then
|
||
`collectOf` is observable on `R`. This pushes the general soundness obligation onto the
|
||
*proven* empty theory plus support containment of the middle. -/
|
||
theorem observable_collectOf_of_boundary {r : RangeRead} {A : List Key} {more : Bool}
|
||
{R : ConflictRange}
|
||
(hsnd : ∀ s ∈ boundaryReads r A more, Sound s emptyRes R)
|
||
(hsubF : ∀ c ∈ bracketConstraints r A ++ coreConstraints A, Subrange c.supp R) :
|
||
Observable (collectOf r A more) R := by
|
||
have hb : Observable (conjDNFs ((boundaryReads r A more).map emptyConstraints)) R :=
|
||
observable_conjDNFs fun D hD => by
|
||
rw [List.mem_map] at hD
|
||
obtain ⟨s, hs, rfl⟩ := hD
|
||
exact (observable_emptyConstraints_iff_sound).mpr (hsnd s hs)
|
||
have hmap : collectOf r A more =
|
||
(conjDNFs ((boundaryReads r A more).map emptyConstraints)).map
|
||
(fun S => (bracketConstraints r A ++ coreConstraints A) ++ S) := rfl
|
||
rw [hmap]
|
||
exact observable_append_of_support hsubF hb
|
||
|
||
/-- Soundness of an empty read is preserved by enlarging the range. -/
|
||
theorem sound_emptyRes_mono {s : RangeRead} {cr cr' : ConflictRange}
|
||
(hsub : Subrange cr cr') (h : Sound s emptyRes cr) : Sound s emptyRes cr' :=
|
||
Sound.mono (fun _ _ hagree => keys_eq_of_subrange_agree hsub hagree) h
|
||
|
||
/-- **The collector's observability, from the proven empty theory.** The boundary
|
||
soundness is discharged by the *proven* `emptyValue` minimality: with every required
|
||
boundary read realizable, `R` containing each boundary `emptyValue`, and `R` containing
|
||
the bracket/core supports, `collectOf` is observable on `R`. The residual obligation for
|
||
a general read is thus only the middle's support containment and the boundary
|
||
readability — no boundary-theory content beyond `emptyInferenceWitness`. -/
|
||
theorem observable_collectOf_of_emptyValue {r : RangeRead} {A : List Key} {more : Bool}
|
||
{R : ConflictRange}
|
||
(hread : ∀ s ∈ boundaryReads r A more, Readable s emptyRes)
|
||
(hsubE : ∀ s ∈ boundaryReads r A more, Subrange (emptyValue s) R)
|
||
(hsubF : ∀ c ∈ bracketConstraints r A ++ coreConstraints A, Subrange c.supp R) :
|
||
Observable (collectOf r A more) R := by
|
||
refine observable_collectOf_of_boundary (fun s hs => ?_) hsubF
|
||
obtain ⟨hsnd, _⟩ := emptyInferenceWitness s (hread s hs)
|
||
exact sound_emptyRes_mono (hsubE s hs) hsnd
|
||
|
||
/-! ### The concrete computable read-off, and its unconditional soundness
|
||
|
||
The composition above still takes the range `R` and the two support conditions as
|
||
hypotheses. The concrete read-off discharges them by construction: `collectReadOff` is
|
||
the hull of the required boundary reads' `emptyValue`s together with the supports of the
|
||
bracket/core middle. Its subrange lemmas are immediate, and the boundary reads of a
|
||
*realizable* pair are themselves realizable with the empty result (the shared compatible
|
||
database makes each boundary read empty), so the readability hypothesis is automatic.
|
||
The result, `sound_collectReadOff`, is unconditional in every theory hypothesis: for any
|
||
realizable read/result pair with nonempty ascending reported keys, the computed read-off
|
||
is sound. -/
|
||
|
||
/-- The hull of a list of conflict ranges. -/
|
||
def hullRanges : List ConflictRange → ConflictRange
|
||
| [] => ⟨(([] : Key) : WithTop Key), (([] : Key) : WithTop Key)⟩
|
||
| cr :: rest => hull cr (hullRanges rest)
|
||
|
||
/-- Every range in the list is a subrange of the hull. -/
|
||
theorem subrange_hullRanges {cr : ConflictRange} {l : List ConflictRange}
|
||
(hmem : cr ∈ l) : Subrange cr (hullRanges l) := by
|
||
induction l with
|
||
| nil => exact absurd hmem (by simp)
|
||
| cons cr' rest ih =>
|
||
rw [List.mem_cons] at hmem
|
||
rw [hullRanges]
|
||
rcases hmem with rfl | hmem
|
||
· exact subrange_hull_left _ _
|
||
· exact Subrange.trans (ih hmem) (subrange_hull_right _ _)
|
||
|
||
/-- The supports of a system, as a list of conflict ranges. -/
|
||
def systemSupports (S : ConstraintSystem) : List ConflictRange := S.map Constraint.supp
|
||
|
||
/-- **The concrete read-off.** The hull of the required boundary reads' `emptyValue`s
|
||
and the supports of the bracket/core middle. This is the computable candidate sound
|
||
range the DNF assembly targets. -/
|
||
def collectReadOff (r : RangeRead) (A : List Key) (more : Bool) : ConflictRange :=
|
||
hullRanges (((boundaryReads r A more).map emptyValue) ++
|
||
systemSupports (bracketConstraints r A ++ coreConstraints A))
|
||
|
||
/-- Each required boundary read's `emptyValue` is a subrange of the read-off. -/
|
||
theorem subrange_emptyValue_collectReadOff {r : RangeRead} {A : List Key} {more : Bool}
|
||
{s : RangeRead} (hs : s ∈ boundaryReads r A more) :
|
||
Subrange (emptyValue s) (collectReadOff r A more) := by
|
||
apply subrange_hullRanges
|
||
rw [List.mem_append]; left
|
||
exact List.mem_map.mpr ⟨s, hs, rfl⟩
|
||
|
||
/-- Each bracket/core support is a subrange of the read-off. -/
|
||
theorem subrange_supp_collectReadOff {r : RangeRead} {A : List Key} {more : Bool}
|
||
{c : Constraint} (hc : c ∈ bracketConstraints r A ++ coreConstraints A) :
|
||
Subrange c.supp (collectReadOff r A more) := by
|
||
apply subrange_hullRanges
|
||
rw [List.mem_append]; right
|
||
exact List.mem_map.mpr ⟨c, hc, rfl⟩
|
||
|
||
/-- `ascendingKeys` is invariant under replacing the result by its reported form. -/
|
||
theorem ascendingKeys_reportedList (r : RangeRead) (A : List Key) (more : Bool) :
|
||
ascendingKeys r { keys := reportedList r A, more := more } = A := by
|
||
unfold ascendingKeys reportedList
|
||
by_cases hr : r.reverse <;> simp [hr]
|
||
|
||
/-- The required boundary reads of a realizable pair are themselves realizable with the
|
||
empty result: the shared compatible database makes each boundary read empty. -/
|
||
theorem readable_boundaryReads {r : RangeRead} {A : List Key} {more : Bool}
|
||
(hA : A ≠ []) (hpair : A.Pairwise (· < ·))
|
||
(hread : Readable r { keys := reportedList r A, more := more }) :
|
||
∀ s ∈ boundaryReads r A more, Readable s emptyRes := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
have hlim : RangeRead.withinLimit r (reportedList r A) := hcomp.1
|
||
have hbo := (compatible_iff_bridge_general (db := db) (r := r) (A := A) (more := more)
|
||
hA hpair hlim).mp hcomp |>.2
|
||
intro s hs
|
||
refine ⟨db, ?_⟩
|
||
rw [show emptyRes = ({ keys := [], more := false } : Result) from rfl,
|
||
compatible_empty_iff_complete_nil]
|
||
exact boundaryReads_empty_of_bridgeOf hbo s hs
|
||
|
||
/-- **General soundness of the concrete read-off, fully proved.** For any realizable
|
||
read/result pair with nonempty ascending reported keys, the hull of the required
|
||
boundary reads' `emptyValue`s and the bracket/core supports is a sound conflict range.
|
||
No boundary-theory hypothesis remains: the boundary half is the proven
|
||
`emptyInferenceWitness`, and the middle supports lie in the read-off by construction. -/
|
||
theorem sound_collectReadOff {r : RangeRead} {res : Result}
|
||
(hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·))
|
||
(hread : Readable r res) :
|
||
Sound r res (collectReadOff r (ascendingKeys r res) res.more) := by
|
||
apply sound_of_observable (characterizesDNF_collectOf (r := r) (res := res) hA hpair)
|
||
refine observable_collectOf_of_emptyValue (A := ascendingKeys r res) (more := res.more)
|
||
?_ ?_ ?_
|
||
· have h : Readable r
|
||
{ keys := reportedList r (ascendingKeys r res), more := res.more } := by
|
||
rwa [reportedList_ascendingKeys]
|
||
exact readable_boundaryReads hA hpair h
|
||
· intro s hs
|
||
exact subrange_emptyValue_collectReadOff hs
|
||
· intro c hc
|
||
exact subrange_supp_collectReadOff hc
|
||
|
||
/-! ### Minimality transfer and the residual
|
||
|
||
Soundness and minimality depend on a range only through its key set, so a range with
|
||
the same `toSet` as a known-minimal one is minimal too. Hence the read-off is minimal as
|
||
soon as it is a subrange of an already-minimal range: soundness then forces the reverse
|
||
containment (`Minimal.subrange_of_sound`), pinning the two key sets equal. This is
|
||
`minimal_collectReadOff_of_subrange`, the cleanest route to the read-off's minimality
|
||
whenever the algorithm's value is known minimal. The one residual for the general case
|
||
is *not* a theory hypothesis: it is that the computed read-off be contained in the
|
||
minimal range, i.e. that the hull does not overshoot — the per-key flip witnesses in
|
||
hull form. -/
|
||
|
||
/-- Soundness depends on a range only through its key set. -/
|
||
theorem sound_congr_toSet {r : RangeRead} {res : Result} {cr cr' : ConflictRange}
|
||
(h : cr.toSet = cr'.toSet) : Sound r res cr ↔ Sound r res cr' := by
|
||
have hkeys : ∀ db : DB, cr.keys db = cr'.keys db := by
|
||
intro db
|
||
apply eq_of_mem_iff_pairwise (ConflictRange.keys_pairwise cr db)
|
||
(ConflictRange.keys_pairwise cr' db)
|
||
intro k
|
||
rw [ConflictRange.mem_keys_iff, ConflictRange.mem_keys_iff,
|
||
← ConflictRange.mem_toSet, ← ConflictRange.mem_toSet, h]
|
||
constructor <;> intro hs db₁ db₂ hagree
|
||
· refine hs db₁ db₂ ?_
|
||
unfold AgreeOn at hagree ⊢
|
||
rw [hkeys db₁, hkeys db₂]; exact hagree
|
||
· refine hs db₁ db₂ ?_
|
||
unfold AgreeOn at hagree ⊢
|
||
rw [← hkeys db₁, ← hkeys db₂]; exact hagree
|
||
|
||
/-- Minimality depends on a range only through its key set. -/
|
||
theorem minimal_congr_toSet {r : RangeRead} {res : Result} {cr cr' : ConflictRange}
|
||
(h : cr.toSet = cr'.toSet) : Minimal r res cr ↔ Minimal r res cr' := by
|
||
have hsound := sound_congr_toSet (r := r) (res := res) h
|
||
have hsubrange : ∀ a, Subrange a cr ↔ Subrange a cr' := by
|
||
intro a; rw [subrange_iff_subset, subrange_iff_subset, h]
|
||
have hsubrange' : ∀ a, Subrange cr a ↔ Subrange cr' a := by
|
||
intro a; rw [subrange_iff_subset, subrange_iff_subset, h]
|
||
have hproper : ∀ cr'', ProperSubrange cr'' cr ↔ ProperSubrange cr'' cr' := by
|
||
intro cr''
|
||
unfold ProperSubrange
|
||
rw [hsubrange cr'', hsubrange' cr'']
|
||
constructor
|
||
· intro hm
|
||
exact ⟨hsound.mp hm.1, fun cr'' hsub => hm.2 cr'' ((hproper cr'').mpr hsub)⟩
|
||
· intro hm
|
||
exact ⟨hsound.mpr hm.1, fun cr'' hsub => hm.2 cr'' ((hproper cr'').mp hsub)⟩
|
||
|
||
/-- **Minimality of the read-off from a known-minimal range.** If the read-off is a
|
||
subrange of an already-minimal range `m`, then it is minimal: it is sound
|
||
(`sound_collectReadOff`), so `Minimal.subrange_of_sound` gives `m ⊆ read-off`, and with
|
||
the assumed containment the two have equal key sets. The residual is only the
|
||
containment `read-off ⊆ m`, i.e. that the hull does not overshoot. -/
|
||
theorem minimal_collectReadOff_of_subrange {r : RangeRead} {res : Result}
|
||
{m : ConflictRange}
|
||
(hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·))
|
||
(hread : Readable r res) (hm : Minimal r res m)
|
||
(hsub : Subrange (collectReadOff r (ascendingKeys r res) res.more) m) :
|
||
Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := by
|
||
have hsound := sound_collectReadOff hA hpair hread
|
||
have hle : Subrange m (collectReadOff r (ascendingKeys r res) res.more) :=
|
||
hm.subrange_of_sound hsound
|
||
have htoSet : m.toSet = (collectReadOff r (ascendingKeys r res) res.more).toSet :=
|
||
Set.Subset.antisymm (subrange_iff_subset.mp hle) (subrange_iff_subset.mp hsub)
|
||
exact (minimal_congr_toSet htoSet).mp hm
|
||
|
||
/-- **Meet closure of minimality.** Intersecting a *sound* range with a known-minimal
|
||
one is minimal. The intersection is sound (`Sound.inter`), so `Minimal.subrange_of_sound`
|
||
forces the minimal range into it, pinning the two key sets equal. This is the closure
|
||
that repairs an over-collecting candidate: even when the read-off is not contained in the
|
||
minimal range (as in the truncated branch), its meet with the algorithm's proven-minimal
|
||
value is minimal. -/
|
||
theorem minimal_inter_of_sound_of_minimal {r : RangeRead} {res : Result}
|
||
{R m : ConflictRange} (hR : Sound r res R) (hm : Minimal r res m) :
|
||
Minimal r res (ConflictRange.inter R m) := by
|
||
have hs : Sound r res (ConflictRange.inter R m) := hR.inter hm.sound
|
||
have hle : Subrange (ConflictRange.inter R m) m :=
|
||
subrange_of_le (le_max_right _ _) (min_le_right _ _)
|
||
have hge : Subrange m (ConflictRange.inter R m) := hm.subrange_of_sound hs
|
||
have htoSet : (ConflictRange.inter R m).toSet = m.toSet :=
|
||
Set.Subset.antisymm (subrange_iff_subset.mp hle) (subrange_iff_subset.mp hge)
|
||
exact (minimal_congr_toSet htoSet.symm).mp hm
|
||
|
||
/-- **Minimality from necessity.** Soundness of the read-off is unconditional
|
||
(`sound_collectReadOff`), so it is minimal as soon as every key it contains is necessary.
|
||
This is the form the main development's necessity theorems target: they supply the
|
||
per-key flip witnesses, and the read-off's keys are exactly the hull's key set. -/
|
||
theorem minimal_collectReadOff_of_necessary {r : RangeRead} {res : Result}
|
||
(hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·))
|
||
(hread : Readable r res)
|
||
(hnec : ∀ k ∈ collectReadOff r (ascendingKeys r res) res.more, Necessary r res k) :
|
||
Minimal r res (collectReadOff r (ascendingKeys r res) res.more) :=
|
||
(minimal_iff_sound_and_necessary).mpr
|
||
⟨sound_collectReadOff hA hpair hread, hnec⟩
|
||
|
||
/-- **Minimality of the read-off from necessity of its endpoints, range form.** Since
|
||
necessity is convex (`Necessary.of_between`), it suffices that the read-off's two
|
||
endpoints bracket necessary keys — the `NecessaryRange` form the main development's
|
||
interval-perturbation theorems produce. -/
|
||
theorem minimal_collectReadOff_of_necessaryRange {r : RangeRead} {res : Result}
|
||
(hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·))
|
||
(hread : Readable r res)
|
||
(hnec : NecessaryRange r res (collectReadOff r (ascendingKeys r res) res.more).begin
|
||
(collectReadOff r (ascendingKeys r res) res.more).end_) :
|
||
Minimal r res (collectReadOff r (ascendingKeys r res) res.more) :=
|
||
(minimal_iff_sound_and_necessaryRange).mpr
|
||
⟨sound_collectReadOff hA hpair hread, hnec⟩
|
||
|
||
/-- A nonempty range's begin is at or above the begin of any subrange of it. -/
|
||
theorem le_begin_of_subrange_of_not_isEmpty {cr m : ConflictRange}
|
||
(h : Subrange cr m) (hne : ¬ cr.IsEmpty) : m.begin ≤ cr.begin := by
|
||
cases hb : cr.begin with
|
||
| top => rw [ConflictRange.IsEmpty, hb] at hne; exact absurd le_top hne
|
||
| coe b =>
|
||
have hlt : (cr.begin : WithTop Key) < cr.end_ := lt_of_not_ge hne
|
||
have hmem : b ∈ cr := ConflictRange.mem_iff.mpr ⟨by rw [hb], by rw [← hb]; exact hlt⟩
|
||
have hthis := (h b hmem).1
|
||
simpa only [hb] using hthis
|
||
|
||
/-- A nonempty range's end is at or below the end of any subrange of it. -/
|
||
theorem end_le_of_subrange_of_not_isEmpty {cr m : ConflictRange}
|
||
(h : Subrange cr m) (hne : ¬ cr.IsEmpty) : cr.end_ ≤ m.end_ := by
|
||
by_contra hlt
|
||
rw [not_le] at hlt
|
||
have hme : m.end_ ≠ ⊤ := fun htop => absurd (htop ▸ hlt) (not_lt.mpr le_top)
|
||
cases hm : m.end_ with
|
||
| top => exact absurd hm hme
|
||
| coe e =>
|
||
cases hb : cr.begin with
|
||
| top => rw [ConflictRange.IsEmpty, hb] at hne; exact absurd le_top hne
|
||
| coe b =>
|
||
have hcrlt : (cr.begin : WithTop Key) < cr.end_ := lt_of_not_ge hne
|
||
have hbm : b ∈ m :=
|
||
h b (ConflictRange.mem_iff.mpr ⟨by rw [hb], by rw [← hb]; exact hcrlt⟩)
|
||
have hb_e : (b : WithTop Key) < (e : WithTop Key) := by
|
||
have := hbm.2; rwa [hm] at this
|
||
have hecr : e ∈ cr := by
|
||
refine ConflictRange.mem_iff.mpr ⟨?_, ?_⟩
|
||
· rw [hb]; exact le_of_lt hb_e
|
||
· rw [hm] at hlt; exact hlt
|
||
have hem := (h e hecr).2
|
||
rw [hm] at hem
|
||
exact absurd hem (lt_irrefl _)
|
||
|
||
/-- **The hull is the least upper bound.** A range containing both factors is a
|
||
superrange of their hull. -/
|
||
theorem hull_subrange_of {cr₁ cr₂ m : ConflictRange}
|
||
(h₁ : Subrange cr₁ m) (h₂ : Subrange cr₂ m) : Subrange (hull cr₁ cr₂) m := by
|
||
unfold hull
|
||
by_cases he₁ : cr₁.IsEmpty
|
||
· rw [ite_eq_left he₁]; exact h₂
|
||
· rw [ite_eq_right he₁]
|
||
by_cases he₂ : cr₂.IsEmpty
|
||
· rw [ite_eq_left he₂]; exact h₁
|
||
· rw [ite_eq_right he₂]
|
||
exact subrange_of_le
|
||
(le_min (le_begin_of_subrange_of_not_isEmpty h₁ he₁)
|
||
(le_begin_of_subrange_of_not_isEmpty h₂ he₂))
|
||
(max_le (end_le_of_subrange_of_not_isEmpty h₁ he₁)
|
||
(end_le_of_subrange_of_not_isEmpty h₂ he₂))
|
||
|
||
/-- A list-hull is a subrange of `m` when every member is. -/
|
||
theorem subrange_hullRanges_of {l : List ConflictRange} {m : ConflictRange}
|
||
(h : ∀ cr ∈ l, Subrange cr m) : Subrange (hullRanges l) m := by
|
||
induction l with
|
||
| nil => intro k hk; exact absurd hk (by simp [hullRanges, ConflictRange.mem_iff])
|
||
| cons cr rest ih =>
|
||
rw [hullRanges]
|
||
exact hull_subrange_of (h cr (by simp))
|
||
(ih fun cr' hcr' => h cr' (List.mem_cons_of_mem _ hcr'))
|
||
|
||
/-- **The residual reduced to its constituents.** The read-off is a subrange of `m`
|
||
exactly when every boundary `emptyValue` and every bracket/core support is. So the
|
||
single containment obligation of `minimal_collectReadOff_of_subrange` splits into
|
||
per-constituent containments, each a case of a read-level analysis. -/
|
||
theorem subrange_collectReadOff_iff {r : RangeRead} {A : List Key} {more : Bool}
|
||
{m : ConflictRange} :
|
||
Subrange (collectReadOff r A more) m ↔
|
||
(∀ s ∈ boundaryReads r A more, Subrange (emptyValue s) m) ∧
|
||
(∀ c ∈ bracketConstraints r A ++ coreConstraints A, Subrange c.supp m) := by
|
||
unfold collectReadOff
|
||
constructor
|
||
· intro h
|
||
refine ⟨fun s hs => ?_, fun c hc => ?_⟩
|
||
· exact Subrange.trans
|
||
(subrange_hullRanges (List.mem_append.mpr
|
||
(Or.inl (List.mem_map.mpr ⟨s, hs, rfl⟩)))) h
|
||
· exact Subrange.trans
|
||
(subrange_hullRanges (List.mem_append.mpr
|
||
(Or.inr (List.mem_map.mpr ⟨c, hc, rfl⟩)))) h
|
||
· rintro ⟨h₁, h₂⟩
|
||
apply subrange_hullRanges_of
|
||
intro cr hcr
|
||
rw [List.mem_append] at hcr
|
||
rcases hcr with hcr | hcr
|
||
· rw [List.mem_map] at hcr
|
||
obtain ⟨s, hs, rfl⟩ := hcr
|
||
exact h₁ s hs
|
||
· rw [systemSupports, List.mem_map] at hcr
|
||
obtain ⟨c, hc, rfl⟩ := hcr
|
||
exact h₂ c hc
|
||
|
||
/-! ### The canonical-region containment
|
||
|
||
In the canonical region `1 ≤ begin.offset ∧ end_.offset ≤ 1`, a complete nonempty
|
||
realizable result has every reported key inside the canonical range `[begin.key, end.key)`:
|
||
each reported key is stored (`completeKeys ⊆ db.keys`) and lies between the resolutions
|
||
(`resolve begin ≤ k < resolve end_`), the begin offset pushes `begin.key` below every
|
||
reported key (`key_le_resolve_of_one_le`), and the end offset at most one keeps every
|
||
stored key below `resolve end_` below `end.key` (`stored_lt_selector_key_of_lt_resolve`).
|
||
From this the constituents' containments follow directly, so the read-off is contained in
|
||
the canonical range and, being sound, shares its key set. -/
|
||
|
||
/-- **Every stored key below `resolve s` is below `s.key`,** when `s.offset ≤ 1`. A stored
|
||
key at or above `s.key` sits at index `≥ count (· < s.key)`, while the resolution's index
|
||
is that count shifted up by the (nonpositive) offset. -/
|
||
theorem stored_lt_selector_key_of_lt_resolve {db : DB} {s : Selector} (hoff : s.offset ≤ 1)
|
||
{k : Key} (hk : k ∈ db.keys) (hlt : (k : WithTop Key) < resolve db s) :
|
||
(k : WithTop Key) < s.key := by
|
||
by_contra hnot
|
||
have hkge : s.key ≤ (k : WithTop Key) := le_of_not_gt hnot
|
||
set c : ℕ := db.keys.countP (fun x => decide ((x : Key) < s.key)) with hc
|
||
have hanc : s.anchor db = (c : ℤ) + s.offset - 1 := by
|
||
unfold Selector.anchor; rw [← hc]
|
||
obtain ⟨j, hj, hjk⟩ := List.mem_iff_getElem.mp hk
|
||
have hcj : c ≤ j := by
|
||
have hmono : c ≤ db.keys.countP (fun x => decide (x < k)) := by
|
||
rw [hc]
|
||
apply List.countP_mono_left
|
||
intro x _ hx
|
||
rw [decide_eq_true_eq] at hx ⊢
|
||
exact WithTop.coe_lt_coe.mp (lt_of_lt_of_le hx hkge)
|
||
have hj' : db.keys.countP (fun x => decide (x < k)) = j :=
|
||
countP_lt_eq_of_getElem db.sorted hj hjk
|
||
omega
|
||
rcases lt_or_ge (s.anchor db) 0 with hneg | hpos
|
||
· rw [resolve_eq_nil hneg] at hlt
|
||
exact absurd hlt (not_lt.mpr (WithTop.coe_le_coe.mpr (nil_le_key k)))
|
||
· rcases lt_or_ge (s.anchor db).toNat db.keys.length with hlti | hge
|
||
· rw [resolve_eq_getElem hpos hlti] at hlt
|
||
have hklt : k < db.keys[(s.anchor db).toNat] := WithTop.coe_lt_coe.mp hlt
|
||
have hjlt : j < (s.anchor db).toNat := by
|
||
rw [← hjk] at hklt
|
||
exact (db.sorted.getElem_lt_getElem_iff).mp hklt
|
||
have hle : (s.anchor db).toNat ≤ c := by
|
||
rw [Int.toNat_le, hanc]; omega
|
||
omega
|
||
· have hclen : db.keys.length ≤ c := by
|
||
have : (s.anchor db).toNat ≤ c := by rw [Int.toNat_le, hanc]; omega
|
||
omega
|
||
omega
|
||
|
||
/-- Membership of the reported keys in the result, through `ascendingKeys`. -/
|
||
theorem mem_res_keys_of_mem_ascendingKeys {r : RangeRead} {res : Result} {k : Key}
|
||
(hk : k ∈ ascendingKeys r res) : k ∈ res.keys := by
|
||
rw [← reportedList_ascendingKeys (r := r) (res := res)]
|
||
unfold reportedList
|
||
by_cases hr : r.reverse
|
||
· simp only [hr, ↓reduceIte]; exact List.mem_reverse.mpr hk
|
||
· simp only [hr, Bool.false_eq_true, ↓reduceIte]; exact hk
|
||
|
||
/-- **Every reported key lies in the canonical range**, in the canonical region with a
|
||
realizable result. (Completeness is not needed: a truncated result's keys are still a
|
||
prefix of the complete keys.) -/
|
||
theorem reported_keys_bounds {r : RangeRead} {res : Result}
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1)
|
||
(hread : Readable r res) {k : Key} (hk : k ∈ res.keys) :
|
||
r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key := by
|
||
obtain ⟨db₀, hcomp⟩ := hread
|
||
have hpre := RangeRead.compatible_keys_prefix hcomp
|
||
have hck : k ∈ RangeRead.completeKeys db₀ r := hpre.subset hk
|
||
obtain ⟨hkmem, hlo, hhi⟩ := RangeRead.mem_completeKeys.mp hck
|
||
exact ⟨le_trans (key_le_resolve_of_one_le hbegin) hlo,
|
||
stored_lt_selector_key_of_lt_resolve hend hkmem hhi⟩
|
||
|
||
/-- A constraint whose lower and upper ends lie in `[B, E)` has its support inside
|
||
`[B, E)`. Both orders are allowed, since `supp` takes `min`/`max`. -/
|
||
theorem subrange_supp_of_four {c : Constraint} {B E : WithTop Key}
|
||
(h1 : B ≤ c.lo) (h2 : c.hi ≤ E) (h3 : B ≤ c.hi) (h4 : c.lo ≤ E) :
|
||
Subrange c.supp ⟨B, E⟩ := by
|
||
rw [Constraint.supp]
|
||
exact subrange_of_le (le_min h1 h3) (max_le h4 h2)
|
||
|
||
/-- The defaulted last of a nonempty cons is its undefaulted last. -/
|
||
theorem getLastD_cons_default {a : Key} {l : List Key} :
|
||
(a :: l).getLastD a = (a :: l).getLastD ([] : Key) := by
|
||
rw [List.getLastD_cons, List.getLastD_cons]
|
||
|
||
/-- Every bracket/core constraint support is contained in the canonical range, in the
|
||
canonical region with a complete realizable result. -/
|
||
theorem supps_subrange_canonical {r : RangeRead} {res : Result}
|
||
(_hmore : res.more = false) (hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1)
|
||
(hread : Readable r res) (hne : res.keys ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·)) {c : Constraint}
|
||
(hc : c ∈ bracketConstraints r (ascendingKeys r res) ++ coreConstraints (ascendingKeys r res)) :
|
||
Subrange c.supp (canonicalRange r) := by
|
||
have hb := fun {k : Key} (hk : k ∈ res.keys) => reported_keys_bounds hbegin hend hread hk
|
||
have hmem := fun {k : Key} (hk : k ∈ ascendingKeys r res) => mem_res_keys_of_mem_ascendingKeys hk
|
||
have hAne : ascendingKeys r res ≠ [] := by
|
||
unfold ascendingKeys; by_cases hr : r.reverse <;> simp [hr, hne]
|
||
obtain ⟨k₀, rest, hAeq⟩ := List.exists_cons_of_ne_nil hAne
|
||
set kₙ : Key := (k₀ :: rest).getLastD k₀ with hkₙdef
|
||
have hk0res : k₀ ∈ res.keys := hmem (by rw [hAeq]; exact List.mem_cons_self ..)
|
||
have hkn_mem : kₙ ∈ k₀ :: rest := by
|
||
rw [hkₙdef, List.getLastD_cons]
|
||
exact List.getLastD_mem_cons
|
||
have hknres : kₙ ∈ res.keys := hmem (by rw [hAeq]; exact hkn_mem)
|
||
have hb0 := hb hk0res
|
||
have hbn := hb hknres
|
||
have hk0le : (k₀ : WithTop Key) ≤ (kₙ : WithTop Key) :=
|
||
WithTop.coe_le_coe.mpr (headD_le_of_pairwise (by rw [← hAeq]; exact hpair) hkn_mem)
|
||
have hbe : r.begin.key ≤ r.end_.key := by
|
||
obtain ⟨k, hk⟩ := List.exists_mem_of_ne_nil _ hne
|
||
exact le_of_lt (lt_of_le_of_lt (hb hk).1 (hb hk).2)
|
||
have hkn' : (kₙ : WithTop Key) < r.end_.key := hbn.2
|
||
have hk0' : (k₀ : WithTop Key) < r.end_.key := hb0.2
|
||
have hbeg0 : r.begin.key ≤ (k₀ : WithTop Key) := hb0.1
|
||
have hbegn : r.begin.key ≤ (kₙ : WithTop Key) := hbn.1
|
||
have e2 : r.begin.key ≤ ((keyAfter kₙ : Key) : WithTop Key) :=
|
||
le_trans hbegn (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt kₙ)))
|
||
rw [hAeq] at hc
|
||
rw [List.mem_append] at hc
|
||
rcases hc with hc | hc
|
||
· -- bracket constraints
|
||
simp only [bracketConstraints, List.mem_cons, List.not_mem_nil, or_false] at hc
|
||
rcases hc with rfl | rfl
|
||
· exact subrange_supp_of_four hbeg0 hbe le_rfl (le_of_lt hk0')
|
||
· exact subrange_supp_of_four hbe (le_of_lt hkn') hbegn le_rfl
|
||
· -- core constraints
|
||
simp only [coreConstraints, List.mem_append, List.mem_map, List.mem_cons,
|
||
List.not_mem_nil, or_false] at hc
|
||
rcases hc with hc | hc | hc
|
||
· obtain ⟨k, hkA, rfl⟩ := hc
|
||
have hkA' : k ∈ (k₀ :: rest) := List.mem_cons.mpr hkA
|
||
have hbk := hb (hmem (by rw [hAeq]; exact hkA'))
|
||
exact subrange_supp_of_four
|
||
(le_trans hbk.1 (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k))))
|
||
(le_of_lt hbk.2) hbk.1 (keyAfter_le_of_lt hbk.2)
|
||
· rw [hc]; exact subrange_supp_of_four hbeg0 (keyAfter_le_of_lt hkn') e2 (le_of_lt hk0')
|
||
· rw [hc]; exact subrange_supp_of_four e2 (le_of_lt hk0') hbeg0 (keyAfter_le_of_lt hkn')
|
||
|
||
/-- `emptyValue` of the lower boundary read lies in the canonical range. -/
|
||
theorem emptyValue_boundaryReadLo_subrange {r : RangeRead} {k₀ : Key}
|
||
(hbegin : 1 ≤ r.begin.offset) (hbeg0 : r.begin.key ≤ (k₀ : WithTop Key))
|
||
(hlt : (k₀ : WithTop Key) < r.end_.key) :
|
||
Subrange (emptyValue (boundaryReadLo r k₀)) (canonicalRange r) := by
|
||
have hbeginL : ¬ (boundaryReadLo r k₀).begin.offset ≤ 0 := by
|
||
simp only [boundaryReadLo]; omega
|
||
have hendL : (boundaryReadLo r k₀).end_.offset ≤ 1 := le_of_eq rfl
|
||
by_cases hAE : AlwaysEmpty (boundaryReadLo r k₀)
|
||
· have hv : emptyValue (boundaryReadLo r k₀) = emptyRange (boundaryReadLo r k₀) := by
|
||
unfold emptyValue; rw [ite_eq_left hAE]
|
||
rw [hv]
|
||
exact subrange_of_le le_rfl (le_trans hbeg0 (le_of_lt hlt))
|
||
· have hv : emptyValue (boundaryReadLo r k₀) = canonicalRange (boundaryReadLo r k₀) := by
|
||
unfold emptyValue
|
||
rw [ite_eq_right hAE, ite_eq_right hbeginL, ite_eq_left hendL]
|
||
rw [hv]
|
||
exact subrange_of_le le_rfl (le_of_lt hlt)
|
||
|
||
/-- `emptyValue` of the upper boundary read lies in the canonical range. -/
|
||
theorem emptyValue_boundaryReadHi_subrange {r : RangeRead} {kₙ : Key}
|
||
(hend : r.end_.offset ≤ 1) (hbegn : r.begin.key ≤ (kₙ : WithTop Key))
|
||
(hlt : ((keyAfter kₙ : Key) : WithTop Key) ≤ r.end_.key) :
|
||
Subrange (emptyValue (boundaryReadHi r kₙ)) (canonicalRange r) := by
|
||
have hbeginH : ¬ (boundaryReadHi r kₙ).begin.offset ≤ 0 := by
|
||
simp only [boundaryReadHi, selAt_offset]; norm_num
|
||
have hendH : (boundaryReadHi r kₙ).end_.offset ≤ 1 := hend
|
||
have hbeg : r.begin.key ≤ ((keyAfter kₙ : Key) : WithTop Key) :=
|
||
le_trans hbegn (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt kₙ)))
|
||
by_cases hAE : AlwaysEmpty (boundaryReadHi r kₙ)
|
||
· have hv : emptyValue (boundaryReadHi r kₙ) = emptyRange (boundaryReadHi r kₙ) := by
|
||
unfold emptyValue; rw [ite_eq_left hAE]
|
||
rw [hv]
|
||
exact subrange_of_le hbeg hlt
|
||
· have hv : emptyValue (boundaryReadHi r kₙ) = canonicalRange (boundaryReadHi r kₙ) := by
|
||
unfold emptyValue
|
||
rw [ite_eq_right hAE, ite_eq_right hbeginH, ite_eq_left hendH]
|
||
rw [hv]
|
||
exact subrange_of_le hbeg le_rfl
|
||
|
||
/-- A realizable nonempty result's ascending keys are strictly increasing. -/
|
||
theorem pairwise_ascendingKeys {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (_hne : res.keys ≠ []) :
|
||
(ascendingKeys r res).Pairwise (· < ·) := by
|
||
obtain ⟨db, hcomp⟩ := hread
|
||
unfold ascendingKeys
|
||
by_cases hr : r.reverse
|
||
· simp only [hr, ↓reduceIte]
|
||
exact ((RangeRead.compatible_pairwise hcomp).2 hr).reverse
|
||
· simp only [hr, Bool.false_eq_true, ↓reduceIte]
|
||
exact (RangeRead.compatible_pairwise hcomp).1 (by simpa using hr)
|
||
|
||
/-- The ascending keys of a nonempty result are nonempty. -/
|
||
theorem ascendingKeys_ne_nil {r : RangeRead} {res : Result} (hne : res.keys ≠ []) :
|
||
ascendingKeys r res ≠ [] := by
|
||
unfold ascendingKeys; by_cases hr : r.reverse <;> simp [hr, hne]
|
||
|
||
/-- **The read-off is contained in the canonical range** in the canonical region, for a
|
||
complete nonempty realizable result. -/
|
||
theorem subrange_collectReadOff_canonical {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) :
|
||
Subrange (collectReadOff r (ascendingKeys r res) res.more) (canonicalRange r) := by
|
||
have hb := fun {k : Key} (hk : k ∈ res.keys) => reported_keys_bounds hbegin hend hread hk
|
||
have hmem := fun {k : Key} (hk : k ∈ ascendingKeys r res) => mem_res_keys_of_mem_ascendingKeys hk
|
||
have hAne : ascendingKeys r res ≠ [] := ascendingKeys_ne_nil hne
|
||
have hpair : (ascendingKeys r res).Pairwise (· < ·) := pairwise_ascendingKeys hread hne
|
||
rw [subrange_collectReadOff_iff]
|
||
refine ⟨?_, ?_⟩
|
||
· intro s hs
|
||
have hhead_b := hb (hmem (headD_mem hAne))
|
||
have hlast_b := hb (hmem (getLastD_mem hAne))
|
||
rw [hmore] at hs
|
||
unfold boundaryReads at hs
|
||
simp only [Bool.false_eq_true, ↓reduceIte, List.mem_cons, List.not_mem_nil, or_false] at hs
|
||
rcases hs with rfl | rfl
|
||
· exact emptyValue_boundaryReadLo_subrange hbegin hhead_b.1 hhead_b.2
|
||
· exact emptyValue_boundaryReadHi_subrange hend hlast_b.1 (keyAfter_le_of_lt hlast_b.2)
|
||
· intro c hc
|
||
exact supps_subrange_canonical hmore hbegin hend hread hne hpair hc
|
||
|
||
/-- **The read-off computes the canonical range's key set** in the canonical region, for
|
||
a complete nonempty realizable result: it is sound and contained in the canonical range,
|
||
so the canonical range's minimality pins the two to the same key set. -/
|
||
theorem collectReadOff_toSet_eq_canonical {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) :
|
||
(collectReadOff r (ascendingKeys r res) res.more).toSet = (canonicalRange r).toSet := by
|
||
have hsub := subrange_collectReadOff_canonical hread hmore hne hbegin hend
|
||
have hmin := minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend
|
||
have hle : Subrange (canonicalRange r)
|
||
(collectReadOff r (ascendingKeys r res) res.more) :=
|
||
hmin.subrange_of_sound
|
||
(sound_collectReadOff (ascendingKeys_ne_nil hne) (pairwise_ascendingKeys hread hne) hread)
|
||
exact Set.Subset.antisymm (subrange_iff_subset.mp hsub) (subrange_iff_subset.mp hle)
|
||
|
||
/-- **Minimality of the concrete read-off in the canonical region.** For a complete
|
||
nonempty realizable read/result pair with `1 ≤ begin.offset` and `end_.offset ≤ 1`, the
|
||
computable hull `collectReadOff` is a minimal sound conflict range. This extends
|
||
end-to-end minimality beyond the counterexample `ceRead`. -/
|
||
theorem minimal_collectReadOff_canonical {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) :
|
||
Minimal r res (collectReadOff r (ascendingKeys r res) res.more) :=
|
||
minimal_collectReadOff_of_subrange (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread
|
||
(minimal_canonicalRange_of_end_offset_le_one_general hread hmore hne hbegin hend)
|
||
(subrange_collectReadOff_canonical hread hmore hne hbegin hend)
|
||
|
||
/-! ### The overshoot-region containment
|
||
|
||
In the overshoot region `1 ≤ begin.offset ∧ 2 ≤ end.offset` with every reported key
|
||
below `end.key`, a complete nonempty realizable result has the unbounded top
|
||
`[begin.key, ⊤)` as its minimal range (`minimal_topRange_of_end_offset_ge_two_general`
|
||
and its reversal twin), so the containment residual is that the read-off lie inside
|
||
the top range. The begin side is forced by the reported-key bounds, and the upper
|
||
boundary read's contribution stays above `begin.key` because its begin selector
|
||
`⟨keyAfter kₙ, 1⟩` is keyed strictly above the reported last key. This is the region
|
||
the canonical block does not reach: the other half of `completeValue`'s non-hull
|
||
answers. -/
|
||
|
||
/-- `emptyValue` is the empty range on a trivially empty read. -/
|
||
theorem emptyValue_eq_emptyRange_of_alwaysEmpty {s : RangeRead} (h : AlwaysEmpty s) :
|
||
emptyValue s = emptyRange s := by
|
||
unfold emptyValue; rw [ite_eq_left h]
|
||
|
||
/-- `emptyValue` is the top range away from `AlwaysEmpty` when the begin offset is
|
||
positive, the end offset is at least two, and the selector keys are ordered. -/
|
||
theorem emptyValue_eq_topRange_of {s : RangeRead}
|
||
(hAE : ¬ AlwaysEmpty s) (hb : ¬ s.begin.offset ≤ 0) (he : ¬ s.end_.offset ≤ 1)
|
||
(hk : s.begin.key ≤ s.end_.key) : emptyValue s = topRange s := by
|
||
unfold emptyValue
|
||
rw [ite_eq_right hAE, ite_eq_right hb, ite_eq_right he, ite_eq_left hk]
|
||
|
||
/-- The lower end of the upper boundary read's `emptyValue` stays at or above
|
||
`begin.key`: its begin selector is keyed at `keyAfter kₙ`, which lies at or above the
|
||
begin key. -/
|
||
theorem begin_key_le_emptyValue_boundaryReadHi_begin {r : RangeRead} {kₙ : Key}
|
||
(_hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset)
|
||
(hbk : r.begin.key ≤ (kₙ : WithTop Key)) (hke : (kₙ : WithTop Key) < r.end_.key) :
|
||
r.begin.key ≤ (emptyValue (boundaryReadHi r kₙ)).begin := by
|
||
classical
|
||
have hka : r.begin.key ≤ ((keyAfter kₙ : Key) : WithTop Key) :=
|
||
le_trans hbk (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt kₙ)))
|
||
by_cases hAE : AlwaysEmpty (boundaryReadHi r kₙ)
|
||
· rw [emptyValue_eq_emptyRange_of_alwaysEmpty hAE]
|
||
simp only [emptyRange, boundaryReadHi, selAt_key]
|
||
exact hka
|
||
· rw [emptyValue_eq_topRange_of hAE ?_ ?_ ?_]
|
||
· simp only [topRange, boundaryReadHi, selAt_key]
|
||
exact hka
|
||
· simp only [boundaryReadHi, selAt_offset]; norm_num
|
||
· simp only [boundaryReadHi]; omega
|
||
· simp only [boundaryReadHi, selAt_key]; exact keyAfter_le_of_lt hke
|
||
|
||
/-- A constraint whose lower and upper ends both lie at or above `B` has its support
|
||
inside `[B, ⊤)`. -/
|
||
theorem subrange_supp_topRange_of_bounds {c : Constraint} {B : WithTop Key}
|
||
(h1 : B ≤ c.lo) (h2 : B ≤ c.hi) : Subrange c.supp ⟨B, ⊤⟩ := by
|
||
rw [Constraint.supp]
|
||
exact subrange_of_le (le_min h1 h2) le_top
|
||
|
||
/-- Every bracket/core support is contained in the top range `[begin.key, ⊤)`, in
|
||
the overshoot region with a complete realizable result. -/
|
||
theorem supps_subrange_topRange {r : RangeRead} {k₀ : Key} {rest : List Key}
|
||
(hpair : (k₀ :: rest).Pairwise (· < ·))
|
||
(hbnd : ∀ {k : Key}, k ∈ k₀ :: rest →
|
||
r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key)
|
||
{c : Constraint}
|
||
(hc : c ∈ bracketConstraints r (k₀ :: rest) ++ coreConstraints (k₀ :: rest)) :
|
||
Subrange c.supp (topRange r) := by
|
||
have hk0 : k₀ ∈ k₀ :: rest := List.mem_cons_self ..
|
||
have hkn : rest.getLastD k₀ ∈ k₀ :: rest := List.getLastD_mem_cons
|
||
have hb0 := hbnd hk0
|
||
have hbn := hbnd hkn
|
||
have hk0le : k₀ ≤ rest.getLastD k₀ := by
|
||
simpa only [List.headD_cons] using headD_le_of_pairwise hpair hkn
|
||
rw [List.mem_append] at hc
|
||
rcases hc with hc | hc
|
||
· simp only [bracketConstraints, List.getLastD_cons, List.mem_cons,
|
||
List.not_mem_nil, or_false] at hc
|
||
rcases hc with rfl | rfl
|
||
· exact subrange_supp_topRange_of_bounds hb0.1 (le_refl _)
|
||
· exact subrange_supp_topRange_of_bounds (le_of_lt (lt_of_le_of_lt hbn.1 hbn.2)) hbn.1
|
||
· simp only [coreConstraints, List.getLastD_cons, List.mem_append, List.mem_map,
|
||
List.mem_cons, List.not_mem_nil, or_false] at hc
|
||
rcases hc with ⟨k, hkA, rfl⟩ | rfl | rfl
|
||
· have hbk := hbnd (List.mem_cons.mpr hkA)
|
||
exact subrange_supp_topRange_of_bounds
|
||
(le_trans hbk.1 (WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k)))) hbk.1
|
||
· exact subrange_supp_topRange_of_bounds hb0.1
|
||
(le_trans hb0.1 (WithTop.coe_le_coe.mpr
|
||
(le_of_lt (lt_of_le_of_lt hk0le (keyAfter_self_lt (rest.getLastD k₀))))))
|
||
· exact subrange_supp_topRange_of_bounds
|
||
(le_trans hb0.1 (WithTop.coe_le_coe.mpr
|
||
(le_of_lt (lt_of_le_of_lt hk0le (keyAfter_self_lt (rest.getLastD k₀))))))
|
||
hb0.1
|
||
|
||
/-- **The read-off is contained in the top range** in the overshoot region, for a
|
||
complete nonempty realizable result. -/
|
||
theorem subrange_collectReadOff_topRange {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset)
|
||
(hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) :
|
||
Subrange (collectReadOff r (ascendingKeys r res) res.more) (topRange r) := by
|
||
have hAne : ascendingKeys r res ≠ [] := ascendingKeys_ne_nil hne
|
||
obtain ⟨k₀, rest, hAeq⟩ := List.exists_cons_of_ne_nil hAne
|
||
have hpair : (k₀ :: rest).Pairwise (· < ·) := by
|
||
rw [← hAeq]; exact pairwise_ascendingKeys hread hne
|
||
have hbnd : ∀ {k : Key}, k ∈ k₀ :: rest →
|
||
r.begin.key ≤ (k : WithTop Key) ∧ (k : WithTop Key) < r.end_.key := by
|
||
intro k hk
|
||
have hkres : k ∈ res.keys :=
|
||
mem_res_keys_of_mem_ascendingKeys (r := r) (res := res) (by rw [hAeq]; exact hk)
|
||
obtain ⟨db₀, hcomp₀⟩ := hread
|
||
have hck := (RangeRead.compatible_keys_prefix hcomp₀).subset hkres
|
||
exact ⟨le_trans (key_le_resolve_of_one_le hbegin)
|
||
(RangeRead.mem_completeKeys.mp hck).2.1, hhi k hkres⟩
|
||
rw [hAeq]
|
||
rw [subrange_collectReadOff_iff]
|
||
refine ⟨?_, ?_⟩
|
||
· intro s hs
|
||
rw [hmore] at hs
|
||
simp only [boundaryReads, Bool.false_eq_true, ↓reduceIte, List.mem_cons,
|
||
List.not_mem_nil, or_false] at hs
|
||
rcases hs with rfl | rfl
|
||
· rw [List.headD_cons]
|
||
exact Subrange.trans
|
||
(emptyValue_boundaryReadLo_subrange hbegin (hbnd (List.mem_cons_self ..)).1
|
||
(hbnd (List.mem_cons_self ..)).2) (subrange_of_le le_rfl le_top)
|
||
· have hlast : (k₀ :: rest).getLastD ([] : Key) = rest.getLastD k₀ :=
|
||
List.getLastD_cons
|
||
rw [hlast]
|
||
have hkn : rest.getLastD k₀ ∈ k₀ :: rest := List.getLastD_mem_cons
|
||
exact subrange_of_le
|
||
(begin_key_le_emptyValue_boundaryReadHi_begin hbegin hend
|
||
(hbnd hkn).1 (hbnd hkn).2) le_top
|
||
· intro c hc
|
||
exact supps_subrange_topRange hpair hbnd hc
|
||
|
||
/-- **Minimality of the concrete read-off in the overshoot region.** For a complete
|
||
nonempty realizable pair with `1 ≤ begin.offset`, `2 ≤ end.offset`, and every reported
|
||
key below `end.key`, the computable hull `collectReadOff` is a minimal sound conflict
|
||
range. The algorithm's value is the top range, and the read-off is contained in it. -/
|
||
theorem minimal_collectReadOff_topRange {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset)
|
||
(hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) :
|
||
Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := by
|
||
have htop : Minimal r res (topRange r) := by
|
||
cases hrev : r.reverse with
|
||
| false =>
|
||
exact minimal_topRange_of_end_offset_ge_two_general hread hmore hrev hne hbegin hend hhi
|
||
| true =>
|
||
exact minimal_topRange_of_end_offset_ge_two_rev_general hread hmore hrev hne hbegin hend hhi
|
||
exact minimal_collectReadOff_of_subrange (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread htop
|
||
(subrange_collectReadOff_topRange hread hmore hne hbegin hend hhi)
|
||
|
||
/-- **The read-off computes the top range's key set** in the overshoot region, for a
|
||
complete nonempty realizable result: it is sound and contained in the top range, so the
|
||
top range's minimality (either traversal) pins the two to the same key set. This is the
|
||
overshoot counterpart of `collectReadOff_toSet_eq_canonical`. -/
|
||
theorem collectReadOff_toSet_eq_topRange {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : 1 ≤ r.begin.offset) (hend : 2 ≤ r.end_.offset)
|
||
(hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) :
|
||
(collectReadOff r (ascendingKeys r res) res.more).toSet = (topRange r).toSet := by
|
||
have hsub := subrange_collectReadOff_topRange hread hmore hne hbegin hend hhi
|
||
have hle : Subrange (topRange r) (collectReadOff r (ascendingKeys r res) res.more) := by
|
||
cases hrev : r.reverse with
|
||
| false =>
|
||
exact (minimal_topRange_of_end_offset_ge_two_general hread hmore hrev hne hbegin hend hhi
|
||
).subrange_of_sound (sound_collectReadOff (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread)
|
||
| true =>
|
||
exact (minimal_topRange_of_end_offset_ge_two_rev_general hread hmore hrev hne hbegin hend hhi
|
||
).subrange_of_sound (sound_collectReadOff (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread)
|
||
exact Set.Subset.antisymm (subrange_iff_subset.mp hsub) (subrange_iff_subset.mp hle)
|
||
|
||
/-- **Complete-branch read-off minimality, `begin.offset ≥ 1`.** The read-off is minimal
|
||
on both non-hull regions of `completeValue`: the canonical region `end.offset ≤ 1` and
|
||
the overshoot region (`2 ≤ end.offset` with every reported key below `end.key`). This
|
||
packages the two region theorems behind one disjunctive hypothesis; the crossed selector
|
||
order remains the only uncertified complete region. -/
|
||
theorem minimal_collectReadOff_complete_general {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||
(hbegin : 1 ≤ r.begin.offset)
|
||
(hcase : r.end_.offset ≤ 1 ∨
|
||
(2 ≤ r.end_.offset ∧ ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key)) :
|
||
Minimal r res (collectReadOff r (ascendingKeys r res) res.more) := by
|
||
rcases hcase with h1 | ⟨h2, hhi⟩
|
||
· exact minimal_collectReadOff_canonical hread hmore hne hbegin h1
|
||
· exact minimal_collectReadOff_topRange hread hmore hne hbegin h2 hhi
|
||
|
||
/-! ### The truncated branch through the meet
|
||
|
||
The read-off over-collects on a truncated result (`Counterexample.ct_collectReadOff_not_minimal`),
|
||
but its meet with the algorithm's proven-minimal truncated value is still minimal
|
||
(`minimal_inter_of_sound_of_minimal`): the meet is sound, so it sits inside the minimal
|
||
range, and being a subrange of it (the minimal range is itself a subrange of the
|
||
truncated interval) it shares the minimal key set. This gives a certified range on the
|
||
truncated branch without the read-off's overshoot. -/
|
||
|
||
/-- The forward truncated value's lower end is the begin key for any positive begin
|
||
offset: the lower boundary read is not trivially empty there, so its empty value is
|
||
the canonical range on the begin selector. -/
|
||
theorem lowBound_begin_eq_of_one_le {r : RangeRead} {res : Result}
|
||
(hbegin : 1 ≤ r.begin.offset) :
|
||
(lowBound r res).begin = r.begin.key := by
|
||
unfold lowBound emptyValue
|
||
set s := boundaryReadLo r (leastReported r res) with hs
|
||
have hb : ¬ s.begin.offset ≤ 0 := by
|
||
rw [show s.begin.offset = r.begin.offset from rfl]; omega
|
||
have he : s.end_.offset ≤ 1 := by rw [show s.end_.offset = 1 from rfl]
|
||
by_cases hAE : AlwaysEmpty s
|
||
· rw [ite_eq_left hAE]; simp only [emptyRange, hs, boundaryReadLo]
|
||
· rw [ite_eq_right hAE, ite_eq_right hb, ite_eq_left he]
|
||
simp only [canonicalRange, hs, boundaryReadLo]
|
||
|
||
/-- **Forward truncated value equals the certified truncated range**, without a
|
||
boundary-emptiness side condition: at a positive begin offset the lower end is always
|
||
the begin key. -/
|
||
theorem forwardTruncatedRange_eq_truncatedRange_general {r : RangeRead} {res : Result}
|
||
(hrev : r.reverse = false) (hbegin : 1 ≤ r.begin.offset) :
|
||
forwardTruncatedRange r res = truncatedRange r (res.keys.getLastD ([] : Key)) := by
|
||
have hgreat : greatestReported r res = res.keys.getLastD ([] : Key) := by
|
||
simp [greatestReported, hrev]
|
||
rw [forwardTruncatedRange, hgreat, truncatedRange, lowBound_begin_eq_of_one_le hbegin]
|
||
|
||
/-- **Forward truncated, offset one, unconditionally.** The forward truncated value is
|
||
minimal at offset one with no boundary side condition. -/
|
||
theorem minimal_forwardTruncatedRange_of_offset_one_free {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false)
|
||
(hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) :
|
||
Minimal r res (forwardTruncatedRange r res) := by
|
||
rw [forwardTruncatedRange_eq_truncatedRange_general hrev (by omega)]
|
||
exact minimal_truncatedRange_of_offset_one hread hmore hrev hbegin hend
|
||
|
||
/-- **Forward truncated, overshoot end, unconditionally.** With `end.offset ≥ 2` and every
|
||
reported key below `end.key`, the forward truncated value is minimal. -/
|
||
theorem minimal_forwardTruncatedRange_of_end_offset_ge_two_free {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false)
|
||
(hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset)
|
||
(hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) :
|
||
Minimal r res (forwardTruncatedRange r res) := by
|
||
rw [forwardTruncatedRange_eq_truncatedRange_general hrev (by omega)]
|
||
exact minimal_truncatedRange_of_end_offset_ge_two hread hmore hrev hbegin hend hhi
|
||
|
||
/-- **The meet of the read-off with any known-minimal range is minimal.** This is the
|
||
general repair used to certify over-collecting read-offs without proving containment. -/
|
||
theorem minimal_inter_collectReadOff_of_minimal {r : RangeRead} {res : Result}
|
||
{m : ConflictRange} (hA : ascendingKeys r res ≠ [])
|
||
(hpair : (ascendingKeys r res).Pairwise (· < ·))
|
||
(hread : Readable r res) (hm : Minimal r res m) :
|
||
Minimal r res
|
||
(ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more) m) :=
|
||
minimal_inter_of_sound_of_minimal (sound_collectReadOff hA hpair hread) hm
|
||
|
||
/-- **Truncated forward, offset one, the meet.** At offset one with a truncated
|
||
non-reversed result the meet of the read-off with the forward truncated value is minimal,
|
||
though the read-off alone need not be. -/
|
||
theorem minimal_inter_collectReadOff_truncated_fwd {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false)
|
||
(hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1) (hne : res.keys ≠ []) :
|
||
Minimal r res
|
||
(ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more)
|
||
(forwardTruncatedRange r res)) :=
|
||
minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread
|
||
(minimal_forwardTruncatedRange_of_offset_one_free hread hmore hrev hbegin hend)
|
||
|
||
/-- **Truncated forward, overshoot end, the meet.** With `end.offset ≥ 2` and every
|
||
reported key below `end.key`, the meet of the read-off with the forward truncated value
|
||
is minimal. -/
|
||
theorem minimal_inter_collectReadOff_truncated_fwd_overshoot {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = false)
|
||
(hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset)
|
||
(hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) (hne : res.keys ≠ []) :
|
||
Minimal r res
|
||
(ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more)
|
||
(forwardTruncatedRange r res)) :=
|
||
minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread
|
||
(minimal_forwardTruncatedRange_of_end_offset_ge_two_free hread hmore hrev hbegin hend hhi)
|
||
|
||
/-- **Truncated reversed, the meet.** At offset one with a truncated reversed result
|
||
and a nonempty upper boundary read, the meet of the read-off with the reverse truncated
|
||
value is minimal. -/
|
||
theorem minimal_inter_collectReadOff_truncated_rev {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hend : r.end_.offset = 1)
|
||
(hempty : ¬ AlwaysEmpty (boundaryReadHi r (greatestReported r res)))
|
||
(hne : res.keys ≠ []) :
|
||
Minimal r res
|
||
(ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more)
|
||
(reverseTruncatedRange r res)) :=
|
||
minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread
|
||
(minimal_reverseTruncatedRange_of_offset_one hread hmore hrev hbegin hend hempty)
|
||
|
||
/-- **Truncated reversed, overshoot end, the meet.** With `end.offset ≥ 2` and every
|
||
reported key below `end.key`, the meet of the read-off with the top-from-last value is
|
||
minimal. -/
|
||
theorem minimal_inter_collectReadOff_truncated_rev_overshoot {r : RangeRead} {res : Result}
|
||
(hread : Readable r res) (hmore : res.more = true) (hrev : r.reverse = true)
|
||
(hbegin : r.begin.offset = 1) (hend : 2 ≤ r.end_.offset)
|
||
(hhi : ∀ k ∈ res.keys, ((k : Key) : WithTop Key) < r.end_.key) (hne : res.keys ≠ []) :
|
||
Minimal r res
|
||
(ConflictRange.inter (collectReadOff r (ascendingKeys r res) res.more)
|
||
(topOfLast (res.keys.getLastD ([] : Key)))) :=
|
||
minimal_inter_collectReadOff_of_minimal (ascendingKeys_ne_nil hne)
|
||
(pairwise_ascendingKeys hread hne) hread
|
||
(minimal_topOfLast_of_end_offset_ge_two_rev hread hmore hrev hbegin hend hhi)
|
||
|
||
/-! ## Sorted endpoints telescope
|
||
|
||
If all the keys mentioned by a family of finite-range capacities are sorted, the
|
||
capacities of adjacent intervals add to the whole (`intervalCard_add`). So a
|
||
system of interval capacities over a common, sorted set of endpoints collapses to
|
||
the capacities of the consecutive gaps — the normalization that makes the caps
|
||
first-class. -/
|
||
|
||
/-- The sum of the capacities of the consecutive gaps of a sorted boundary list. -/
|
||
def capSum : List (WithTop Key) → ℕ∞
|
||
| [] => 0
|
||
| [_] => 0
|
||
| a :: b :: rest => intervalCard a b + capSum (b :: rest)
|
||
|
||
/-- A member of a `≤`-sorted list is at most its last element. -/
|
||
theorem le_getLastD_of_pairwise_le {M : List (WithTop Key)} (h : M.Pairwise (· ≤ ·))
|
||
{x : WithTop Key} (hx : x ∈ M) (d : WithTop Key) : x ≤ M.getLastD d := by
|
||
have hne : M ≠ [] := by rintro rfl; exact absurd hx (by simp)
|
||
have hlast : x ≤ M.getLast hne := h.rel_getLast hx
|
||
have hD : M.getLastD d = M.getLast hne := by
|
||
rw [List.getLastD_eq_getLast?, List.getLast?_eq_some_getLast hne, Option.getD_some]
|
||
rw [hD]; exact hlast
|
||
|
||
/-- **Sorted capacities telescope.** For a `≤`-sorted list of boundary keys, the
|
||
capacity of the whole span is the sum of the adjacent capacities:
|
||
`intervalCard (head) (last) = Σ intervalCard mᵢ mᵢ₊₁`. Sorting the keys of any
|
||
finite-range constraint family therefore turns it into a chain of adjacent caps. -/
|
||
theorem intervalCard_headD_getLastD_eq_capSum {M : List (WithTop Key)}
|
||
(h : M.Pairwise (· ≤ ·)) (d : WithTop Key) :
|
||
intervalCard (M.headD d) (M.getLastD d) = capSum M := by
|
||
induction M generalizing d with
|
||
| nil => rw [capSum]; exact intervalCard_eq_zero_of_le le_rfl
|
||
| cons a t ih =>
|
||
cases t with
|
||
| nil => rw [capSum]; exact intervalCard_eq_zero_of_le le_rfl
|
||
| cons b rest =>
|
||
have hab : a ≤ b := (List.pairwise_cons.mp h).1 b (by simp)
|
||
have ht : (b :: rest).Pairwise (· ≤ ·) := (List.pairwise_cons.mp h).2
|
||
have hble : b ≤ (b :: rest).getLastD a :=
|
||
le_getLastD_of_pairwise_le ht (by simp) a
|
||
change intervalCard a ((b :: rest).getLastD a) = capSum (a :: b :: rest)
|
||
rw [intervalCard_add hab hble]
|
||
have hih := ih ht a
|
||
simp only [List.headD_cons] at hih
|
||
rw [hih, capSum]
|
||
|
||
end FormalConflictRange
|