Prove conflict-range closed-form minimality

- Vendor the FormalConflictRange reference library needed by the proof
- Add Bridge.lean with simp lemmas connecting the Statement definitions
  to the reference library
- Prove Statement.closedFormMinimal in Proof.lean
- Register the new libraries in the Lake config
This commit is contained in:
andrew committed 2026-11-16 21:53:59 -05:00
1 parent 7a0826e873
commit a54a5f82f8
78 files changed
+91636 -2

No files matched your search

+617
View File
@@ -0,0 +1,617 @@
/-
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.General
import FormalConflictRange.Prototype
import FormalConflictRange.Realization
/-!
# From databases to finite interval counts
This file connects the database-realization theorem of `Realization.lean` to
the difference-constraint systems of the prototype. A system is satisfiable by
a database exactly when a finite list of interval counts fits the interval
capacities and makes every constraint hold under the cumulative count
environment. The remaining arithmetic is therefore entirely on natural-number
interval counts, which is the shape an active-cell decider consumes.
-/
namespace FormalConflictRange
def EndpointCovered (e : WithTop Key) (bounds : List Key) : Prop :=
e = (([] : Key) : WithTop Key) ∨ e = ⊤ ∨ ∃ b ∈ bounds, e = (b : WithTop Key)
/-- The count environment satisfies a constraint system when every difference
constraint holds under `evalCount`. -/
def SystemHoldsWithCounts (S : ConstraintSystem) (bounds : List Key)
(counts : List ℕ) (topCount : ℕ) : Prop :=
∀ c ∈ S, evalCount bounds counts topCount c.hi -
evalCount bounds counts topCount c.lo ≤ c.bound
/-- `CountsFit` implies that the endpoint chain is nondecreasing. -/
theorem boundsLe_of_CountsFit {start : Key} {bounds : List Key} {counts : List ℕ}
(h : CountsFit start bounds counts) : BoundsLe start bounds := by
induction bounds generalizing start counts with
| nil => trivial
| cons b bs ih =>
cases counts with
| nil => simp [CountsFit] at h
| cons d ds =>
rcases h with ⟨hstartb, -, htail⟩
exact ⟨hstartb, ih htail⟩
/-- The count environment agrees with the database on every covered endpoint. -/
theorem evalCount_eq_cnt_of_covered {db : DB} {bounds : List Key} {counts : List ℕ}
{topCount : ℕ} (h : CountsAt db 0 bounds counts)
(htop : cnt db ⊤ = counts.sum + topCount) (hnoBot : ([] : Key) ∉ bounds)
{e : WithTop Key} (he : EndpointCovered e bounds) :
evalCount bounds counts topCount e = cnt db e := by
rcases he with rfl | rfl | ⟨b, hb, rfl⟩
· exact evalCount_bot
· exact evalCount_top htop
· exact evalCount_coe_of_mem h hb (by
intro hbBot
exact hnoBot (hbBot ▸ hb))
/-- **Satisfiability bridge.** A constraint system is satisfied by some database
exactly when its finite count relaxation is satisfiable: choose consecutive
interval counts that fit the interval capacities and make every constraint hold
under the cumulative count environment.
This turns the existential over databases into a finite count problem. It is
the bridge used by an active-cell decider; the remaining arithmetic is entirely
on `ℕ`-valued interval counts. -/
theorem exists_db_systemHoldsAt_iff_counts (S : ConstraintSystem) (bounds : List Key)
(hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds)
(hcover : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds) :
(∃ db : DB, SystemHoldsAt S db) ↔
∃ counts : List ℕ, ∃ topCount : ℕ,
CountsFit ([] : Key) bounds counts ∧
SystemHoldsWithCounts S bounds counts topCount := by
constructor
· rintro ⟨db, hS⟩
let counts : List ℕ := intervalCounts db ([] : Key) bounds
have hfit : CountsFit ([] : Key) bounds counts :=
intervalCounts_fit db ([] : Key) bounds hle
have hcounts : CountsAt db 0 bounds counts :=
countsAt_intervalCounts db ([] : Key) bounds hle 0 (by simp [cnt_nil])
have hsum : counts.sum ≤ cnt db ⊤ := intervalCounts_sum_le_top db bounds hle
let topCount : ℕ := cnt db ⊤ - counts.sum
have htop : cnt db ⊤ = counts.sum + topCount := by
dsimp [topCount]
omega
refine ⟨counts, topCount, hfit, ?_⟩
intro c hc
have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).2
have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).1
rw [hhi, hlo]
exact hS c hc
· rintro ⟨counts, topCount, hfit, hholds⟩
obtain ⟨db, hcounts, htop⟩ := exists_db_realizing_counts bounds counts topCount hfit
refine ⟨db, ?_⟩
intro c hc
have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).2
have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcover c hc).1
change (cnt db c.hi : ℤ) - (cnt db c.lo : ℤ) ≤ c.bound
rw [← hhi, ← hlo]
exact hholds c hc
/-! ## Toggling one key in the count environment -/
/-- The interval `[k, keyAfter k)` contains exactly `k` when it is stored and
nothing otherwise. -/
theorem between_keyAfter_length (db : DB) (k : Key) :
(between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key)).length =
if k ∈ db.keys then 1 else 0 := by
set l := between db (k : WithTop Key) ((keyAfter k : Key) : WithTop Key) with hl
have hnodup : l.Nodup := db.nodup.filter _
have hsubset : l ⊆ [k] := by
intro x hx
simp only [hl, between, List.mem_filter, decide_eq_true_eq] at hx
have hxk : x = k := eq_of_le_of_lt_keyAfter (WithTop.coe_le_coe.mp hx.2.1)
(WithTop.coe_lt_coe.mp hx.2.2)
simp [hxk]
have hle : l.length ≤ 1 := by
have := hnodup.length_le_of_subset hsubset
simpa using this
by_cases hk : k ∈ db.keys
· have hmem : k ∈ l := by
simp [hl, between, hk, keyAfter_self_lt]
have hpos : 0 < l.length := List.length_pos_of_mem hmem
rw [ite_eq_left hk]
omega
· have hnil : l = [] := by
rw [hl, between, List.filter_eq_nil_iff]
intro x hx hcon
simp only [decide_eq_true_eq] at hcon
have hxmem : x ∈ db.keys := by simpa using hx
have hxk : x = k := eq_of_le_of_lt_keyAfter (WithTop.coe_le_coe.mp hcon.1)
(WithTop.coe_lt_coe.mp hcon.2)
exact hk (hxk ▸ hxmem)
simp [hnil, hk]
/-- The difference of the counts at `k` and `keyAfter k` is the indicator of
`k` being stored. -/
theorem cnt_keyAfter_sub_cnt (db : DB) (k : Key) :
(cnt db ((keyAfter k : Key) : WithTop Key) : ℤ) - cnt db (k : WithTop Key) =
if k ∈ db.keys then 1 else 0 := by
have hle : (k : WithTop Key) ≤ ((keyAfter k : Key) : WithTop Key) :=
WithTop.coe_le_coe.mpr (le_of_lt (keyAfter_self_lt k))
have hmono : cnt db (k : WithTop Key) ≤ cnt db ((keyAfter k : Key) : WithTop Key) :=
cnt_le_cnt_of_le db hle
have hcnt := cnt_sub_eq_between_length (db := db) hle
have hlen := between_keyAfter_length db k
rw [← Nat.cast_sub hmono, hcnt, hlen]
by_cases hk : k ∈ db.keys <;> simp [hk]
/-- The additive change in a count when the membership of `k` is toggled. The
variable `x` is the indicator that `k` is stored. -/
def toggleShift (bounds : List Key) (counts : List ℕ) (topCount : ℕ) (k : Key)
(e : WithTop Key) : ℤ :=
let x : ℤ := evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) -
evalCount bounds counts topCount (k : WithTop Key)
if (k : WithTop Key) < e then 1 - 2 * x else 0
/-- The count environment after toggling `k`. -/
def evalCountToggled (bounds : List Key) (counts : List ℕ) (topCount : ℕ) (k : Key)
(e : WithTop Key) : ℤ :=
evalCount bounds counts topCount e +
toggleShift bounds counts topCount k e
/-- The toggled count environment agrees with the toggled database. -/
theorem evalCountToggled_eq_cnt_toggle {db : DB} {bounds : List Key}
{counts : List ℕ} {topCount : ℕ} (hcounts : CountsAt db 0 bounds counts)
(htop : cnt db ⊤ = counts.sum + topCount) (hnoBot : ([] : Key) ∉ bounds)
{k : Key} (hk : k = ([] : Key) ∨ k ∈ bounds)
(hka : keyAfter k ∈ bounds) {e : WithTop Key} (he : EndpointCovered e bounds) :
evalCountToggled bounds counts topCount k e = cnt (toggle db k) e := by
have hka_bot : keyAfter k ≠ ([] : Key) := by
intro h
have : (keyAfter k).length = 0 := by simp [h]
simp [keyAfter] at this
have he_cnt := evalCount_eq_cnt_of_covered hcounts htop hnoBot he
have hka_cnt : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) =
cnt db ((keyAfter k : Key) : WithTop Key) :=
evalCount_coe_of_mem hcounts hka hka_bot
have hk_cnt : evalCount bounds counts topCount (k : WithTop Key) = cnt db (k : WithTop Key) := by
rcases hk with rfl | hk
· exact evalCount_bot
· exact evalCount_coe_of_mem hcounts hk (by
intro h
exact hnoBot (h ▸ hk))
have hx : evalCount bounds counts topCount ((keyAfter k : Key) : WithTop Key) -
evalCount bounds counts topCount (k : WithTop Key) =
if k ∈ db.keys then 1 else 0 := by
rw [hka_cnt, hk_cnt]
exact cnt_keyAfter_sub_cnt db k
unfold evalCountToggled toggleShift
rw [he_cnt, hx]
unfold toggle
by_cases hmem : k ∈ db.keys
· rw [dite_eq_left hmem, cnt_erase_eq db k hmem e]
by_cases hlt : (k : WithTop Key) < e
· simp [hlt, hmem]
try ring
· simp [hlt]
try ring
· rw [dite_eq_right hmem, cnt_insert_eq db k hmem e]
by_cases hlt : (k : WithTop Key) < e
· simp [hlt, hmem]
try ring
· simp [hlt]
try ring
/-- **One toggle witness in finite count form.** A database satisfying a system
`S` while violating every constraint in a chosen list `bad` after toggling `k`
exists exactly when the finite count relaxation has a solution that satisfies
`S` and makes every toggled constraint in `bad` fail. The list `bad` is the
finite choice of one violated constraint per DNF alternative. -/
theorem exists_db_systemHoldsAt_and_violates_iff_counts
(S : ConstraintSystem) (bad : List Constraint) (bounds : List Key)
(hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds)
(hcoverS : ∀ c ∈ S, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds)
(hcoverBad : ∀ c ∈ bad, EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds)
{k : Key} (hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) :
(∃ db : DB, SystemHoldsAt S db ∧ ∀ c ∈ bad, ¬ c.HoldsAt (toggle db k)) ↔
∃ counts : List ℕ, ∃ topCount : ℕ,
CountsFit ([] : Key) bounds counts ∧
SystemHoldsWithCounts S bounds counts topCount ∧
∀ c ∈ bad, c.bound <
evalCountToggled bounds counts topCount k c.hi -
evalCountToggled bounds counts topCount k c.lo := by
constructor
· rintro ⟨db, hS, hbad⟩
let counts : List ℕ := intervalCounts db ([] : Key) bounds
have hfit : CountsFit ([] : Key) bounds counts :=
intervalCounts_fit db ([] : Key) bounds hle
have hcounts : CountsAt db 0 bounds counts :=
countsAt_intervalCounts db ([] : Key) bounds hle 0 (by simp [cnt_nil])
have hsum : counts.sum ≤ cnt db ⊤ := intervalCounts_sum_le_top db bounds hle
let topCount : ℕ := cnt db ⊤ - counts.sum
have htop : cnt db ⊤ = counts.sum + topCount := by
dsimp [topCount]
omega
refine ⟨counts, topCount, hfit, ?_, ?_⟩
· intro c hc
have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).2
have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).1
rw [hhi, hlo]
exact hS c hc
· intro c hc
have hhi := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka
(hcoverBad c hc).2
have hlo := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka
(hcoverBad c hc).1
have hnc := hbad c hc
change ¬ ((cnt (toggle db k) c.hi : ℤ) -
(cnt (toggle db k) c.lo : ℤ) ≤ c.bound) at hnc
rw [hhi, hlo]
omega
· rintro ⟨counts, topCount, hfit, hS, hbad⟩
obtain ⟨db, hcounts, htop⟩ := exists_db_realizing_counts bounds counts topCount hfit
refine ⟨db, ?_, ?_⟩
· intro c hc
have hhi := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).2
have hlo := evalCount_eq_cnt_of_covered hcounts htop hnoBot (hcoverS c hc).1
change (cnt db c.hi : ℤ) - (cnt db c.lo : ℤ) ≤ c.bound
rw [← hhi, ← hlo]
exact hS c hc
· intro c hc
have hhi := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka
(hcoverBad c hc).2
have hlo := evalCountToggled_eq_cnt_toggle hcounts htop hnoBot hk hka
(hcoverBad c hc).1
have hnc := hbad c hc
change ¬ ((cnt (toggle db k) c.hi : ℤ) -
(cnt (toggle db k) c.lo : ℤ) ≤ c.bound)
rw [← hhi, ← hlo]
omega
/-- A finite choice of one violated constraint from each DNF alternative. -/
def ViolatesAll : List Constraint → ConstraintDNF → DB → Prop
| [], [], _ => True
| c :: cs, S :: Ss, db => c ∈ S ∧ ¬ c.HoldsAt db ∧ ViolatesAll cs Ss db
| _, _, _ => False
/-- A DNF fails exactly when one can choose a violated constraint from every
alternative. This is the finite disjunction used by the count encoding. -/
theorem not_anyHolds_iff_exists_violating_choice {D : ConstraintDNF} {db : DB} :
¬ AnyHolds D db ↔ ∃ bad : List Constraint, ViolatesAll bad D db := by
induction D with
| nil =>
constructor
· intro _
exact ⟨[], by trivial⟩
· intro _ h
simp [AnyHolds] at h
| cons S Ss ih =>
cases S with
| nil =>
have hAny : AnyHolds ([] :: Ss) db := by
refine ⟨[], by simp, ?_⟩
intro c hc
simp at hc
constructor
· intro h
exact absurd hAny h
· rintro ⟨bad, hbad⟩
cases bad with
| nil =>
unfold ViolatesAll at hbad
exact False.elim hbad
| cons b bs =>
unfold ViolatesAll at hbad
exact False.elim (by simpa using hbad.1)
| cons c cs =>
have hAny : AnyHolds ((c :: cs) :: Ss) db ↔
SystemHoldsAt (c :: cs) db ∨ AnyHolds Ss db := by
simp [AnyHolds, exists_eq_or_imp]
constructor
· intro h
rw [hAny] at h
have hS : ¬ SystemHoldsAt (c :: cs) db := fun hh => h (Or.inl hh)
have hTail : ¬ AnyHolds Ss db := fun hh => h (Or.inr hh)
unfold SystemHoldsAt at hS
push Not at hS
rcases hS with ⟨c', hc', hnc'⟩
obtain ⟨bad, hbad⟩ := ih.mp hTail
refine ⟨c' :: bad, ?_⟩
unfold ViolatesAll
exact ⟨hc', hnc', hbad⟩
· rintro ⟨bad, hbad⟩
rw [hAny]
intro h
cases bad with
| nil =>
unfold ViolatesAll at hbad
exact False.elim hbad
| cons b bs =>
unfold ViolatesAll at hbad
rcases hbad with ⟨hb, hnb, htail⟩
rcases h with hS | hTail
· exact hnb (hS b hb)
· exact (ih.mpr ⟨bs, htail⟩) hTail
/-- A finite choice of one constraint from each DNF alternative, independent of
a database. -/
def ChoosesAll : List Constraint → ConstraintDNF → Prop
| [], [] => True
| c :: cs, S :: Ss => c ∈ S ∧ ChoosesAll cs Ss
| _, _ => False
/-- The database-dependent violation predicate is a choice of constraints plus
their failure. -/
theorem violatesAll_iff_choosesAll {bad : List Constraint} {D : ConstraintDNF} {db : DB} :
ViolatesAll bad D db ↔ ChoosesAll bad D ∧ ∀ c ∈ bad, ¬ c.HoldsAt db := by
induction bad generalizing D with
| nil =>
cases D with
| nil => simp [ViolatesAll, ChoosesAll]
| cons S Ss => simp [ViolatesAll, ChoosesAll]
| cons c cs ih =>
cases D with
| nil => simp [ViolatesAll, ChoosesAll]
| cons S Ss =>
simp only [ViolatesAll, ChoosesAll, List.forall_mem_cons, ih, and_assoc]
tauto
/-- Every chosen constraint lies in one of the DNF alternatives. -/
theorem choosesAll_mem {bad : List Constraint} {D : ConstraintDNF} (h : ChoosesAll bad D)
{c : Constraint} (hc : c ∈ bad) : ∃ S ∈ D, c ∈ S := by
induction bad generalizing D with
| nil => simp at hc
| cons c' cs ih =>
cases D with
| nil => simp [ChoosesAll] at h
| cons S Ss =>
rcases h with ⟨hc', htail⟩
rcases List.mem_cons.mp hc with rfl | hcs
· exact ⟨S, by simp, hc'⟩
· obtain ⟨S', hS', hcS'⟩ := ih htail hcs
exact ⟨S', by simp [hS'], hcS'⟩
/-! ## Canonical finite endpoint lists for the count description -/
/-- The finite, nonempty part of a `WithTop Key` endpoint, as an option. -/
def finiteEndpointOption : WithTop Key → Option Key
| some k => if k = ([] : Key) then none else some k
| none => none
theorem finiteEndpointOption_eq_some {e : WithTop Key} {k : Key} :
finiteEndpointOption e = some k ↔ e = (k : WithTop Key) ∧ k ≠ ([] : Key) := by
cases e with
| top => simp [finiteEndpointOption]
| coe a =>
by_cases ha : a = ([] : Key)
· simp [finiteEndpointOption, ha]
· simp only [finiteEndpointOption, ha, ↓reduceIte]
constructor
· intro h
refine ⟨h, ?_⟩
intro hk
exact ha (Option.some.inj h ▸ hk)
· rintro ⟨h, _⟩
exact h
theorem finiteEndpointOption_injective_on {a a' : WithTop Key} {b : Key}
(ha : b ∈ finiteEndpointOption a) (ha' : b ∈ finiteEndpointOption a') : a = a' := by
cases a with
| top => simp [finiteEndpointOption] at ha
| coe ka =>
cases a' with
| top => simp [finiteEndpointOption] at ha'
| coe ka' =>
by_cases hka : ka = ([] : Key)
· simp [finiteEndpointOption, hka] at ha
· by_cases hka' : ka' = ([] : Key)
· simp [finiteEndpointOption, hka'] at ha'
· simp only [finiteEndpointOption, hka, hka', ↓reduceIte] at ha ha'
simp only [Option.mem_some] at ha ha'
rw [ha, ha']
/-- All finite, nonempty endpoints occurring in a DNF. -/
def finiteKeyEndpoints (D : ConstraintDNF) : Finset Key :=
(cellEndpoints D).filterMap finiteEndpointOption
(by
intro a a' b ha ha'
exact finiteEndpointOption_injective_on ha ha')
theorem mem_finiteKeyEndpoints {D : ConstraintDNF} {k : Key} :
k ∈ finiteKeyEndpoints D ↔
(k : WithTop Key) ∈ cellEndpoints D ∧ k ≠ ([] : Key) := by
rw [finiteKeyEndpoints, Finset.mem_filterMap]
constructor
· rintro ⟨a, ha, hfa⟩
rw [finiteEndpointOption_eq_some] at hfa
exact ⟨by simpa [hfa.1] using ha, hfa.2⟩
· rintro ⟨ha, hk⟩
exact ⟨(k : WithTop Key), ha, (finiteEndpointOption_eq_some).mpr ⟨rfl, hk⟩⟩
/-- The canonical finite endpoint list for the active-cell count description: all
finite nonempty DNF endpoints, together with `k` (when nonempty) and `keyAfter k`,
sorted increasingly. -/
def activeBounds (D : ConstraintDNF) (k : Key) : List Key :=
let s := insert (keyAfter k) (finiteKeyEndpoints D)
(if k = ([] : Key) then s else insert k s).sort (· ≤ ·)
theorem keyAfter_ne_nil (k : Key) : keyAfter k ≠ ([] : Key) := by
intro h
have : (keyAfter k).length = 0 := by simp [h]
simp [keyAfter] at this
theorem mem_activeBounds {D : ConstraintDNF} {k e : Key} :
e ∈ activeBounds D k ↔
e = keyAfter k ∨ e ∈ finiteKeyEndpoints D ∨
(k ≠ ([] : Key) ∧ e = k) := by
unfold activeBounds
by_cases hk : k = ([] : Key)
· simp [hk, Finset.mem_sort, or_comm]
· simp [hk, Finset.mem_sort, or_left_comm, or_comm]
theorem not_mem_activeBounds_bot (D : ConstraintDNF) (k : Key) :
([] : Key) ∉ activeBounds D k := by
rw [mem_activeBounds]
rintro (h | h | ⟨hk, hk'⟩)
· exact keyAfter_ne_nil k h.symm
· exact (mem_finiteKeyEndpoints.mp h).2 rfl
· exact hk hk'.symm
/-- A sorted list whose elements are all above `start` has nondecreasing bounds. -/
theorem boundsLe_of_sortedLE {start : Key} {l : List Key}
(hs : l.SortedLE) (h : ∀ b ∈ l, start ≤ b) : BoundsLe start l := by
induction l generalizing start with
| nil => trivial
| cons a t ih =>
change start ≤ a ∧ BoundsLe a t
constructor
· exact h a (by simp)
· apply ih (start := a)
· exact (List.sortedLE_cons.mp hs).2
· intro b hb
exact (List.sortedLE_cons.mp hs).1 b hb
theorem activeBounds_boundsLe (D : ConstraintDNF) (k : Key) :
BoundsLe ([] : Key) (activeBounds D k) := by
apply boundsLe_of_sortedLE
· unfold activeBounds
by_cases hk : k = ([] : Key)
· simpa [hk] using
(Finset.sortedLT_sort (insert (keyAfter k) (finiteKeyEndpoints D))).sortedLE
· simpa [hk] using
(Finset.sortedLT_sort (insert k (insert (keyAfter k) (finiteKeyEndpoints D)))).sortedLE
· intro b hb
exact nil_le_key b
theorem endpointCovered_activeBounds {D : ConstraintDNF} {k : Key} {e : WithTop Key}
(he : e ∈ cellEndpoints D) : EndpointCovered e (activeBounds D k) := by
unfold EndpointCovered
cases e with
| top => exact Or.inr (Or.inl rfl)
| coe a =>
by_cases ha : a = ([] : Key)
· exact Or.inl (by simp [ha])
· refine Or.inr (Or.inr ⟨a, ?_, rfl⟩)
rw [mem_activeBounds]
exact Or.inr (Or.inl ((mem_finiteKeyEndpoints).mpr ⟨by simpa using he, ha⟩))
theorem dnfEndpointCovered_activeBounds {D : ConstraintDNF} {k : Key}
{S : ConstraintSystem} {c : Constraint} (hS : S ∈ D) (hc : c ∈ S) :
EndpointCovered c.lo (activeBounds D k) ∧
EndpointCovered c.hi (activeBounds D k) :=
⟨endpointCovered_activeBounds
(dnfEndpoints_subset_cellEndpoints D (mem_dnfEndpoints_lo hS hc)),
endpointCovered_activeBounds
(dnfEndpoints_subset_cellEndpoints D (mem_dnfEndpoints_hi hS hc))⟩
/-- **Flip witnesses as finite count disjunctions.** Under a characterizing DNF,
a one-key toggle witness exists exactly when the limit is respected and there is
an alternative `S` of the DNF, a finite choice `bad` of one constraint from every
alternative, and a finite interval-count assignment that satisfies `S` while
making every constraint in `bad` fail after toggling `k`. All quantifiers here
are finite except the count feasibility, which is the remaining decision
problem. -/
theorem flipWitness_iff_exists_counts
{D : ConstraintDNF} {r : RangeRead} {res : Result}
(hchar : CharacterizesDNF D r res) {k : Key}
(bounds : List Key) (hnoBot : ([] : Key) ∉ bounds) (hle : BoundsLe ([] : Key) bounds)
(hcoverD : ∀ S ∈ D, ∀ c ∈ S,
EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds)
(hk : k = ([] : Key) ∨ k ∈ bounds) (hka : keyAfter k ∈ bounds) :
FlipWitness r res k ↔
RangeRead.withinLimit r res.keys ∧
∃ S ∈ D, ∃ bad : List Constraint, ChoosesAll bad D ∧
∃ counts : List ℕ, ∃ topCount : ℕ,
CountsFit ([] : Key) bounds counts ∧
SystemHoldsWithCounts S bounds counts topCount ∧
∀ c ∈ bad, c.bound <
evalCountToggled bounds counts topCount k c.hi -
evalCountToggled bounds counts topCount k c.lo := by
rw [flipWitness_iff_exists_not_anyHolds hchar]
constructor
· rintro ⟨db, hcomp, hnot⟩
rcases (hchar db).mp hcomp with ⟨hwithin, hany⟩
obtain ⟨S, hS, hSholds⟩ := hany
obtain ⟨bad, hbad⟩ := (not_anyHolds_iff_exists_violating_choice).mp hnot
rcases (violatesAll_iff_choosesAll).mp hbad with ⟨hchooses, hviol⟩
have hcoverBad : ∀ c ∈ bad,
EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by
intro c hc
obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc
exact hcoverD S' hS' c hcS'
obtain ⟨counts, topCount, hfit, hScounts, hbadcounts⟩ :=
(exists_db_systemHoldsAt_and_violates_iff_counts S bad bounds hnoBot hle
(hcoverD S hS) hcoverBad hk hka).mp ⟨db, hSholds, hviol⟩
exact ⟨hwithin, S, hS, bad, hchooses, counts, topCount, hfit, hScounts, hbadcounts⟩
· rintro ⟨hwithin, S, hS, bad, hchooses, counts, topCount, hfit, hScounts, hbadcounts⟩
have hcoverBad : ∀ c ∈ bad,
EndpointCovered c.lo bounds ∧ EndpointCovered c.hi bounds := by
intro c hc
obtain ⟨S', hS', hcS'⟩ := choosesAll_mem hchooses hc
exact hcoverD S' hS' c hcS'
obtain ⟨db, hSholds, hviol⟩ :=
(exists_db_systemHoldsAt_and_violates_iff_counts S bad bounds hnoBot hle
(hcoverD S hS) hcoverBad hk hka).mpr
⟨counts, topCount, hfit, hScounts, hbadcounts⟩
have hnot : ¬ AnyHolds D (toggle db k) :=
(not_anyHolds_iff_exists_violating_choice).mpr
⟨bad, (violatesAll_iff_choosesAll).mpr ⟨hchooses, hviol⟩⟩
exact ⟨db, (hchar db).mpr ⟨hwithin, ⟨S, hS, hSholds⟩⟩, hnot⟩
/-- **Canonical finite-count form of `FlipWitness`.** The endpoint list is computed
from the DNF itself, so no coverage hypothesis remains. -/
theorem flipWitness_iff_exists_counts_canonical
{D : ConstraintDNF} {r : RangeRead} {res : Result}
(hchar : CharacterizesDNF D r res) {k : Key} :
FlipWitness r res k ↔
RangeRead.withinLimit r res.keys ∧
∃ S ∈ D, ∃ bad : List Constraint, ChoosesAll bad D ∧
∃ counts : List ℕ, ∃ topCount : ℕ,
CountsFit ([] : Key) (activeBounds D k) counts ∧
SystemHoldsWithCounts S (activeBounds D k) counts topCount ∧
∀ c ∈ bad, c.bound <
evalCountToggled (activeBounds D k) counts topCount k c.hi -
evalCountToggled (activeBounds D k) counts topCount k c.lo :=
flipWitness_iff_exists_counts (D := D) hchar (bounds := activeBounds D k)
(not_mem_activeBounds_bot D k) (activeBounds_boundsLe D k)
(fun S hS c hc => dnfEndpointCovered_activeBounds hS hc)
(by
by_cases hk : k = ([] : Key)
· exact Or.inl hk
· exact Or.inr (by
rw [mem_activeBounds]
exact Or.inr (Or.inr ⟨hk, rfl⟩)))
(by
rw [mem_activeBounds]
exact Or.inl rfl)
/-! ## A total activity oracle and the unconditional cellwise algorithm -/
/-- The total classical activity oracle: decide `FlipWitness` by excluded middle.
This closes the algebraic interface. Making it *computable* is exactly the finite
count-feasibility decision problem isolated by `flipWitness_iff_exists_counts`. -/
noncomputable def activeClassical (r : RangeRead) (res : Result) : Key → Bool :=
fun k => @decide (FlipWitness r res k) (Classical.propDecidable _)
/-- The classical oracle is exact. -/
theorem activeClassical_spec (r : RangeRead) (res : Result) :
ActiveDecider r res (activeClassical r res) := by
intro k
simp [activeClassical]
/-- With the classical total oracle, the cellwise algorithm is minimal on every
readable pair. This is the unconditional form of `conflictRangeCellwise_minimal`;
the only remaining *computability* obligation is the count-feasibility decider. -/
theorem conflictRangeCellwise_minimal_classical {r : RangeRead} {res : Result}
(hread : Readable r res) :
Minimal r res (conflictRangeCellwise r res (activeClassical r res)) :=
conflictRangeCellwise_minimal (activeClassical_spec r res) hread
/-- The unconditional classical algorithm computes exactly the necessary set. -/
theorem conflictRangeCellwise_toSet_eq_necessarySet_classical {r : RangeRead} {res : Result}
(hread : Readable r res) :
(conflictRangeCellwise r res (activeClassical r res)).toSet = necessarySet r res :=
conflictRangeCellwise_toSet_eq_necessarySet (activeClassical_spec r res) hread
end FormalConflictRange