/- Copyright (c) 2026 FormalConflictRange contributors. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: FormalConflictRange contributors -/ import Mathlib /-! # A computable decision procedure for difference constraints -/ namespace FormalConflictRange /-- A difference constraint `x v - x u ≤ w`. -/ structure DEdge where u : ℕ v : ℕ w : ℤ namespace DEdge /-- The constraint holds at the assignment `x`. -/ def Holds (x : ℕ → ℤ) (e : DEdge) : Prop := x e.v - x e.u ≤ e.w end DEdge /-- All constraints hold at `x`. -/ def HoldsAll (x : ℕ → ℤ) (es : List DEdge) : Prop := ∀ e ∈ es, e.Holds x /-- All collected constant constraints `0 ≤ w` hold. -/ def HoldsConsts (cs : List ℤ) : Prop := ∀ w ∈ cs, 0 ≤ w /-- A state is feasible when it has an assignment satisfying all constants and edges. -/ def DiffFeasible (cs : List ℤ) (es : List DEdge) : Prop := ∃ x : ℕ → ℤ, HoldsConsts cs ∧ HoldsAll x es /-- Eliminate variable `k` from a difference-constraint state. -/ def eliminate (k : ℕ) (cs : List ℤ) (es : List DEdge) : List ℤ × List DEdge := let selfs0 := es.filter (fun e => decide (e.u = e.v)) let inc := es.filter (fun e => decide (e.u ≠ e.v ∧ e.v = k)) let out := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u = k)) let keep := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k)) let comb := inc.flatMap fun a => out.map fun b => (⟨a.u, b.v, a.w + b.w⟩ : DEdge) let all := keep ++ comb let selfs := all.filter (fun e => decide (e.u = e.v)) let rest := all.filter (fun e => decide (e.u ≠ e.v)) (cs ++ selfs0.map (fun e => e.w) ++ selfs.map (fun e => e.w), rest) /-- Eliminate variables `0, …, n-1`, highest first. -/ def elimAll : ℕ → List ℤ → List DEdge → List ℤ × List DEdge | 0, cs, es => (cs, es) | n + 1, cs, es => let p := eliminate n cs es elimAll n p.1 p.2 /-- The decision procedure: eliminate all variables, then check the constants. -/ def diffFeasibleBool (n : ℕ) (cs : List ℤ) (es : List DEdge) : Bool := let p := elimAll n cs es p.1.all (fun w => decide (0 ≤ w)) /-- A finite separation lemma for integers. -/ theorem exists_between_finset (L U : Finset ℤ) (h : ∀ l ∈ L, ∀ u ∈ U, l ≤ u) : ∃ x : ℤ, (∀ l ∈ L, l ≤ x) ∧ (∀ u ∈ U, x ≤ u) := by by_cases hL : L.Nonempty · by_cases hU : U.Nonempty · refine ⟨L.max' hL, ?_, ?_⟩ · intro l hl exact Finset.le_max' L l hl · intro u hu exact h _ (Finset.max'_mem L hL) u hu · refine ⟨L.max' hL, ?_, ?_⟩ · intro l hl exact Finset.le_max' L l hl · intro u hu exact absurd ⟨u, hu⟩ hU · by_cases hU : U.Nonempty · refine ⟨U.min' hU, ?_, ?_⟩ · intro l hl exact absurd ⟨l, hl⟩ hL · intro u hu exact Finset.min'_le U u hu · refine ⟨0, ?_, ?_⟩ · intro l hl exact absurd ⟨l, hl⟩ hL · intro u hu exact absurd ⟨u, hu⟩ hU theorem self_holds_iff {x : ℕ → ℤ} {e : DEdge} (h : e.u = e.v) : e.Holds x ↔ 0 ≤ e.w := by simp [DEdge.Holds, h] /-- Eliminating one variable preserves feasibility. -/ theorem diffFeasible_eliminate (k : ℕ) (cs : List ℤ) (es : List DEdge) : DiffFeasible cs es ↔ DiffFeasible (eliminate k cs es).1 (eliminate k cs es).2 := by classical unfold DiffFeasible constructor · rintro ⟨x, hcs, hes⟩ have hself0 : ∀ e ∈ es, e.u = e.v → 0 ≤ e.w := by intro e he huv have := hes e he rwa [self_holds_iff huv] at this have hkeep : HoldsAll x (es.filter (fun e => decide (e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k))) := by intro e he simp only [List.mem_filter, decide_eq_true_eq] at he exact hes e he.1 have hcomb : HoldsAll x ((es.filter (fun e => decide (e.u ≠ e.v ∧ e.v = k))).flatMap fun a => (es.filter (fun e => decide (e.u ≠ e.v ∧ e.u = k))).map fun b => (⟨a.u, b.v, a.w + b.w⟩ : DEdge)) := by intro e he rw [List.mem_flatMap] at he obtain ⟨a, ha, he⟩ := he rw [List.mem_map] at he obtain ⟨b, hb, rfl⟩ := he simp only [List.mem_filter, decide_eq_true_eq] at ha hb have hae := hes a ha.1 have hbe := hes b hb.1 simp only [DEdge.Holds] at hae hbe ⊢ rw [ha.2.2] at hae rw [hb.2.2] at hbe linarith refine ⟨x, ?_, ?_⟩ · intro w hw simp only [eliminate, List.mem_append, List.mem_map, List.mem_filter, decide_eq_true_eq] at hw rcases hw with (hw | hw) | hw · exact hcs w hw · obtain ⟨e, he, rfl⟩ := hw exact hself0 e he.1 he.2 · obtain ⟨e, he, rfl⟩ := hw rcases he.1 with hek | hec · exact hself0 e hek.1 he.2 · rw [List.mem_flatMap] at hec obtain ⟨a, ha, hec⟩ := hec rw [List.mem_map] at hec obtain ⟨b, hb, rfl⟩ := hec simp only [List.mem_filter, decide_eq_true_eq] at ha hb have hae := hes a ha.1 have hbe := hes b hb.1 simp only [DEdge.Holds] at hae hbe ⊢ rw [ha.2.2] at hae rw [hb.2.2] at hbe -- self means `a.u = b.v`, so the left side is zero have hab : a.u = b.v := he.2 have hzero : x b.v - x a.u = 0 := by rw [← hab]; ring linarith · intro e he simp only [eliminate, List.mem_append, List.mem_filter, decide_eq_true_eq] at he rcases he.1 with hek | hec · exact hkeep e (by simpa only [List.mem_filter, decide_eq_true_eq] using hek) · exact hcomb e hec · rintro ⟨y, hcs, hes⟩ let selfs0 := es.filter (fun e => decide (e.u = e.v)) let inc := es.filter (fun e => decide (e.u ≠ e.v ∧ e.v = k)) let out := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u = k)) let keep := es.filter (fun e => decide (e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k)) let comb := inc.flatMap fun a => out.map fun b => (⟨a.u, b.v, a.w + b.w⟩ : DEdge) let all := keep ++ comb let selfs := all.filter (fun e => decide (e.u = e.v)) have hcs' : HoldsConsts (cs ++ selfs0.map (fun e => e.w) ++ selfs.map (fun e => e.w)) := by simpa only [eliminate, selfs0, inc, out, keep, comb, all, selfs] using hcs have hes' : HoldsAll y (all.filter (fun e => decide (e.u ≠ e.v))) := by simpa only [eliminate, selfs0, inc, out, keep, comb, all, selfs] using hes have hkeep : HoldsAll y keep := by intro e he have hek : e ∈ es ∧ e.u ≠ e.v ∧ e.u ≠ k ∧ e.v ≠ k := by simpa [keep] using he apply hes' e simp only [List.mem_filter, decide_eq_true_eq] exact ⟨List.mem_append.mpr (Or.inl he), hek.2.1⟩ have hcomb : HoldsAll y comb := by intro e he by_cases hself : e.u = e.v · have hw : e.w ∈ cs ++ selfs0.map (fun e => e.w) ++ selfs.map (fun e => e.w) := by simp only [List.mem_append, List.mem_map] exact Or.inr ⟨e, by change e ∈ all.filter (fun e => decide (e.u = e.v)) simp only [List.mem_filter, decide_eq_true_eq] exact ⟨List.mem_append.mpr (Or.inr he), hself⟩, rfl⟩ have := hcs' e.w hw simpa [DEdge.Holds, hself] using this · apply hes' e simp only [List.mem_filter, decide_eq_true_eq] exact ⟨List.mem_append.mpr (Or.inr he), hself⟩ let L : Finset ℤ := (out.map fun b => y b.v - b.w).toFinset let U : Finset ℤ := (inc.map fun a => y a.u + a.w).toFinset have hsep : ∀ l ∈ L, ∀ u ∈ U, l ≤ u := by intro l hl u hu rw [show L = (out.map fun b => y b.v - b.w).toFinset by rfl] at hl rw [show U = (inc.map fun a => y a.u + a.w).toFinset by rfl] at hu rw [List.mem_toFinset] at hl hu obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hl obtain ⟨a, ha, rfl⟩ := List.mem_map.mp hu have hcombEdge : (⟨a.u, b.v, a.w + b.w⟩ : DEdge) ∈ comb := by unfold comb rw [List.mem_flatMap] exact ⟨a, ha, List.mem_map.mpr ⟨b, hb, rfl⟩⟩ have hh := hcomb _ hcombEdge simp only [DEdge.Holds] at hh linarith obtain ⟨xk, hxkL, hxkU⟩ := exists_between_finset L U hsep let x : ℕ → ℤ := Function.update y k xk have hx_eq : ∀ i, i ≠ k → x i = y i := by intro i hi simp [x, Function.update_of_ne hi] have hx_self : x k = xk := by simp [x] refine ⟨x, ?_, ?_⟩ · intro w hw have hw' : w ∈ (cs ++ selfs0.map (fun e => e.w)) ++ selfs.map (fun e => e.w) := List.mem_append.mpr (Or.inl (List.mem_append.mpr (Or.inl hw))) exact hcs' w hw' · intro e he by_cases huv : e.u = e.v · have hw : e.w ∈ cs ++ selfs0.map (fun e => e.w) ++ selfs.map (fun e => e.w) := by simp only [List.mem_append, List.mem_map] exact Or.inl (Or.inr ⟨e, by simp only [selfs0, List.mem_filter, decide_eq_true_eq] exact ⟨he, huv⟩, rfl⟩) exact (self_holds_iff huv).mpr (hcs' e.w hw) · by_cases huk : e.u = k · have hmem : e ∈ out := by simp only [out, List.mem_filter, decide_eq_true_eq] exact ⟨he, huv, huk⟩ have hlow : y e.v - e.w ≤ xk := hxkL _ (by rw [show L = (out.map fun b => y b.v - b.w).toFinset by rfl] exact List.mem_toFinset.mpr (List.mem_map.mpr ⟨e, hmem, rfl⟩)) have hve : e.v ≠ k := by intro h; exact huv (huk.trans h.symm) simp only [DEdge.Holds] rw [show e.u = k from huk, hx_self, hx_eq e.v hve] linarith · by_cases hvk : e.v = k · have hmem : e ∈ inc := by simp only [inc, List.mem_filter, decide_eq_true_eq] exact ⟨he, huv, hvk⟩ have hup : xk ≤ y e.u + e.w := hxkU _ (by rw [show U = (inc.map fun a => y a.u + a.w).toFinset by rfl] exact List.mem_toFinset.mpr (List.mem_map.mpr ⟨e, hmem, rfl⟩)) have hue : e.u ≠ k := by intro h; exact huv (h.trans hvk.symm) simp only [DEdge.Holds] rw [show e.v = k from hvk, hx_self, hx_eq e.u hue] linarith · have hmem : e ∈ keep := by simp only [keep, List.mem_filter, decide_eq_true_eq] exact ⟨he, huv, huk, hvk⟩ have hh := hkeep e hmem simpa only [DEdge.Holds, hx_eq e.u huk, hx_eq e.v hvk] using hh /-- Eliminating all variables preserves feasibility. -/ theorem diffFeasible_elimAll (n : ℕ) (cs : List ℤ) (es : List DEdge) : DiffFeasible cs es ↔ DiffFeasible (elimAll n cs es).1 (elimAll n cs es).2 := by induction n generalizing cs es with | zero => simp [elimAll] | succ n ih => rw [elimAll, diffFeasible_eliminate] exact ih _ _ /-- After eliminating `0, …, n-1`, no nonself edge with endpoints below `n` remains. -/ theorem elimAll_snd_eq_nil_of_endpoints_lt {n : ℕ} {cs : List ℤ} {es : List DEdge} (h : ∀ e ∈ es, e.u < n ∧ e.v < n) : (elimAll n cs es).2 = [] := by induction n generalizing cs es with | zero => have hes : es = [] := by rw [List.eq_nil_iff_forall_not_mem] intro e he have := h e he omega simp [elimAll, hes] | succ n ih => let p := eliminate n cs es have hp : ∀ e ∈ p.2, e.u < n ∧ e.v < n := by intro e he dsimp [p] at he simp only [eliminate, List.mem_filter, decide_eq_true_eq, List.mem_append] at he rcases he with ⟨hek | hec, hne⟩ · have hb := h e hek.1 exact ⟨by omega, by omega⟩ · rw [List.mem_flatMap] at hec obtain ⟨a, ha, hec⟩ := hec rw [List.mem_map] at hec obtain ⟨b, hb, rfl⟩ := hec simp only [List.mem_filter, decide_eq_true_eq] at ha hb have hua := h a ha.1 have hvb := h b hb.1 change a.u < n ∧ b.v < n exact ⟨by omega, by omega⟩ have := ih (cs := p.1) (es := p.2) hp simpa [elimAll, p] using this /-- The Boolean elimination procedure is correct for edge lists whose endpoints all lie below `n`. -/ theorem diffFeasibleBool_spec (n : ℕ) (cs : List ℤ) (es : List DEdge) (h : ∀ e ∈ es, e.u < n ∧ e.v < n) : diffFeasibleBool n cs es = true ↔ DiffFeasible cs es := by have hnil := elimAll_snd_eq_nil_of_endpoints_lt (cs := cs) (es := es) h have hconst : HoldsConsts (elimAll n cs es).1 ↔ DiffFeasible (elimAll n cs es).1 (elimAll n cs es).2 := by constructor · intro hc exact ⟨fun _ => 0, hc, by simp [HoldsAll, hnil]⟩ · rintro ⟨x, hc, -⟩ exact hc rw [diffFeasibleBool, List.all_eq_true, diffFeasible_elimAll] constructor · intro hall exact hconst.mp fun w hw => of_decide_eq_true (hall w hw) · intro hf w hw exact decide_eq_true_eq.mpr (hconst.mpr hf w hw) /-- A safe upper bound on all variable indices occurring in an edge list. -/ def edgeBound : List DEdge → ℕ | [] => 0 | e :: es => max (max e.u e.v + 1) (edgeBound es) theorem edgeBound_spec (es : List DEdge) : ∀ e ∈ es, e.u < edgeBound es ∧ e.v < edgeBound es := by induction es with | nil => intro e he; simp at he | cons d ds ih => intro e he simp only [edgeBound] rw [List.mem_cons] at he rcases he with rfl | he · omega · have := ih e he omega /-- Feasibility of an arbitrary edge list, using its own index bound. -/ def diffFeasibleBoolAuto (es : List DEdge) : Bool := diffFeasibleBool (edgeBound es) [] es theorem diffFeasibleBoolAuto_spec (es : List DEdge) : diffFeasibleBoolAuto es = true ↔ DiffFeasible [] es := diffFeasibleBool_spec (edgeBound es) [] es (edgeBound_spec es) end FormalConflictRange