Verified interval branch-and-bound checker for polynomial inequalities on boxes
DefinitionBirkhoffGlobalSection_IntervalCheckerA small, self-contained verified checker for strict polynomial inequalities on boxes, generic over an exact scalar type with a real interpretation (CNum). Intervals are pairs of scalars; interval addition, multiplication and powers are proved to enclose the real operations, and a polynomial enclosure enc is called sound (EnclSound) if it encloses the real value on every point of the box. A certificate consists of goal polynomials , hypothesis polynomials , a starting box and a binary bisection tree; a leaf either shows every is positive on its box or shows some is negative there (so the box contains no admissible point). The auxiliary variable with is enclosed from the enclosure of . The file also contains the soundness theorem check_sound: if check enc C = true for a sound enclosure enc, then for every point of the box and every with and all , every goal satisfies . Everything is computable, so acceptance of a certificate is checked by the kernel with decide +kernel (no native_decide).
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Data.Rat.Cast.Order
import Mathlib.Tactic
/-!
# A verified box-subdivision checker (core)
Scalars, intervals and flat polynomials (`List (coeff × exponent vector)`), with interval
enclosures whose soundness is proved once. Everything executable is defined by structural
recursion, so certificates can be checked by kernel reduction (`decide +kernel`).
The scalar type is abstract (`CNum`), with a faithful map `toReal` to `ℝ`: instances for `ℚ`
and for dyadic numbers `m / 2^e` are in `Checker/Scalars.lean`.
-/
namespace BirkhoffGlobalSection.TangentialHessian.Checker
/-- Exact scalars for the checker. -/
class CNum (α : Type) where
add : α → α → α
mul : α → α → α
neg : α → α
zero : α
one : α
natCast : ℕ → α
half : α → α
ble : α → α → Bool
toReal : α → ℝ
toReal_add : ∀ a b, toReal (add a b) = toReal a + toReal b
toReal_mul : ∀ a b, toReal (mul a b) = toReal a * toReal b
toReal_neg : ∀ a, toReal (neg a) = -toReal a
toReal_zero : toReal zero = 0
toReal_one : toReal one = 1
toReal_natCast : ∀ n : ℕ, toReal (natCast n) = n
toReal_half : ∀ a, toReal (half a) = toReal a / 2
ble_iff : ∀ a b, ble a b = true ↔ toReal a ≤ toReal b
namespace CNum
variable {α : Type} [CNum α]
def sub (a b : α) : α := add a (neg b)
def pow (a : α) : ℕ → α
| 0 => one
| n + 1 => mul (pow a n) a
def blt (a b : α) : Bool := !(ble b a)
def amin (a b : α) : α := if ble a b then a else b
def amax (a b : α) : α := if ble a b then b else a
attribute [simp] toReal_add toReal_mul toReal_neg toReal_zero toReal_one toReal_natCast
toReal_half
@[simp] theorem toReal_sub (a b : α) : toReal (sub a b) = toReal a - toReal b := by
simp [sub, sub_eq_add_neg]
@[simp] theorem toReal_pow (a : α) (n : ℕ) : toReal (pow a n) = toReal a ^ n := by
induction n with
| zero => simp [pow]
| succ n ih => simp [pow, ih, pow_succ]
theorem blt_iff (a b : α) : blt a b = true ↔ toReal a < toReal b := by
unfold blt; rw [← not_le, ← ble_iff]; cases ble b a <;> simp
@[simp] theorem toReal_amin (a b : α) : toReal (amin a b) = min (toReal a) (toReal b) := by
unfold amin
split
· rename_i h; rw [min_eq_left ((ble_iff a b).mp h)]
· rename_i h
rw [min_eq_right (le_of_lt (not_le.mp (fun h' => h ((ble_iff a b).mpr h'))))]
@[simp] theorem toReal_amax (a b : α) : toReal (amax a b) = max (toReal a) (toReal b) := by
unfold amax
split
· rename_i h; rw [max_eq_right ((ble_iff a b).mp h)]
· rename_i h
rw [max_eq_left (le_of_lt (not_le.mp (fun h' => h ((ble_iff a b).mpr h'))))]
end CNum
open CNum
variable {α : Type} [CNum α]
/-! ## Intervals -/
/-- A closed interval `[lo, hi]`. -/
abbrev Iv (α : Type) := α × α
/-- `x ∈ [lo, hi]`. -/
def Mem (x : ℝ) (I : Iv α) : Prop := toReal I.1 ≤ x ∧ x ≤ toReal I.2
def iadd (I J : Iv α) : Iv α := (add I.1 J.1, add I.2 J.2)
def imul (I J : Iv α) : Iv α :=
let p₁ := mul I.1 J.1
let p₂ := mul I.1 J.2
let p₃ := mul I.2 J.1
let p₄ := mul I.2 J.2
(amin (amin p₁ p₂) (amin p₃ p₄), amax (amax p₁ p₂) (amax p₃ p₄))
def iscale (c : α) (I : Iv α) : Iv α :=
if ble zero c then (mul c I.1, mul c I.2) else (mul c I.2, mul c I.1)
/-- Exact enclosure of `x ^ e` for `x ∈ I` (even powers of intervals around `0` start at `0`). -/
def ipow (I : Iv α) (e : ℕ) : Iv α :=
if e = 0 then (one, one)
else if ble zero I.1 then (pow I.1 e, pow I.2 e)
else if e % 2 = 1 then (pow I.1 e, pow I.2 e)
else if ble I.2 zero then (pow I.2 e, pow I.1 e)
else (zero, amax (pow I.1 e) (pow I.2 e))
theorem mem_iadd {x y : ℝ} {I J : Iv α} (hx : Mem x I) (hy : Mem y J) :
Mem (x + y) (iadd I J) := by
unfold Mem iadd at *; simp only [toReal_add]; constructor <;> linarith [hx.1, hx.2, hy.1, hy.2]
/-- Products of reals in intervals. -/
theorem real_mul_mem {a b c d x y : ℝ} (hx : a ≤ x ∧ x ≤ b) (hy : c ≤ y ∧ y ≤ d) :
min (min (a * c) (a * d)) (min (b * c) (b * d)) ≤ x * y ∧
x * y ≤ max (max (a * c) (a * d)) (max (b * c) (b * d)) := by
-- `a * y` lies between `a c` and `a d`, similarly `b * y`, and `x * y` between `a y` and `b y`
have hay : min (a * c) (a * d) ≤ a * y ∧ a * y ≤ max (a * c) (a * d) := by
rcases le_total 0 a with ha | ha
· exact ⟨min_le_of_left_le (mul_le_mul_of_nonneg_left hy.1 ha),
le_max_of_le_right (mul_le_mul_of_nonneg_left hy.2 ha)⟩
· exact ⟨min_le_of_right_le (mul_le_mul_of_nonpos_left hy.2 ha),
le_max_of_le_left (mul_le_mul_of_nonpos_left hy.1 ha)⟩
have hby : min (b * c) (b * d) ≤ b * y ∧ b * y ≤ max (b * c) (b * d) := by
rcases le_total 0 b with hb | hb
· exact ⟨min_le_of_left_le (mul_le_mul_of_nonneg_left hy.1 hb),
le_max_of_le_right (mul_le_mul_of_nonneg_left hy.2 hb)⟩
· exact ⟨min_le_of_right_le (mul_le_mul_of_nonpos_left hy.2 hb),
le_max_of_le_left (mul_le_mul_of_nonpos_left hy.1 hb)⟩
have hxy : min (a * y) (b * y) ≤ x * y ∧ x * y ≤ max (a * y) (b * y) := by
rcases le_total 0 y with h | h
· exact ⟨min_le_of_left_le (mul_le_mul_of_nonneg_right hx.1 h),
le_max_of_le_right (mul_le_mul_of_nonneg_right hx.2 h)⟩
· exact ⟨min_le_of_right_le (mul_le_mul_of_nonpos_right hx.2 h),
le_max_of_le_left (mul_le_mul_of_nonpos_right hx.1 h)⟩
constructor
· calc min (min (a * c) (a * d)) (min (b * c) (b * d)) ≤ min (a * y) (b * y) :=
min_le_min hay.1 hby.1
_ ≤ x * y := hxy.1
· calc x * y ≤ max (a * y) (b * y) := hxy.2
_ ≤ max (max (a * c) (a * d)) (max (b * c) (b * d)) := max_le_max hay.2 hby.2
theorem mem_imul {x y : ℝ} {I J : Iv α} (hx : Mem x I) (hy : Mem y J) :
Mem (x * y) (imul I J) := by
unfold Mem imul at *
simp only [toReal_amin, toReal_amax, toReal_mul]
exact real_mul_mem hx hy
theorem mem_iscale {x : ℝ} {I : Iv α} (c : α) (hx : Mem x I) :
Mem (toReal c * x) (iscale c I) := by
unfold Mem iscale at *
split
· rename_i h
have hc := (ble_iff _ _).mp h
simp only [toReal_zero] at hc
simp only [toReal_mul]
exact ⟨mul_le_mul_of_nonneg_left hx.1 hc, mul_le_mul_of_nonneg_left hx.2 hc⟩
· rename_i h
have hc : toReal c ≤ 0 := by
have := mt (ble_iff zero c).mpr h; simp only [toReal_zero, not_le] at this; exact this.le
simp only [toReal_mul]
exact ⟨mul_le_mul_of_nonpos_left hx.2 hc, mul_le_mul_of_nonpos_left hx.1 hc⟩
theorem mem_ipow {x : ℝ} {I : Iv α} (hx : Mem x I) (e : ℕ) : Mem (x ^ e) (ipow I e) := by
unfold Mem ipow at *
obtain ⟨hlo, hhi⟩ := hx
split
· rename_i he; subst he; simp
split
· rename_i _ h
have h0 : 0 ≤ toReal I.1 := by simpa using (ble_iff _ _).mp h
simp only [toReal_pow]
exact ⟨pow_le_pow_left₀ h0 hlo e, pow_le_pow_left₀ (h0.trans hlo) hhi e⟩
split
· rename_i _ _ hodd
have ho : Odd e := Nat.odd_iff.mpr hodd
simp only [toReal_pow]
exact ⟨ho.pow_le_pow.mpr hlo, ho.pow_le_pow.mpr hhi⟩
have hev : Even e := by
rename_i _ _ hodd; exact Nat.even_iff.mpr (by omega)
split
· rename_i _ _ _ h
have h0 : toReal I.2 ≤ 0 := by simpa using (ble_iff _ _).mp h
simp only [toReal_pow]
have hx0 : x ≤ 0 := hhi.trans h0
rw [← hev.neg_pow x, ← hev.neg_pow (toReal I.2), ← hev.neg_pow (toReal I.1)]
exact ⟨pow_le_pow_left₀ (by linarith) (by linarith) e,
pow_le_pow_left₀ (by linarith) (by linarith) e⟩
· simp only [toReal_zero, toReal_amax, toReal_pow]
refine ⟨hev.pow_nonneg x, ?_⟩
rw [← hev.pow_abs x, ← hev.pow_abs (toReal I.1), ← hev.pow_abs (toReal I.2)]
rcases le_total 0 x with h | h
· exact le_max_of_le_right (pow_le_pow_left₀ (abs_nonneg _)
(by rw [abs_of_nonneg h]; exact hhi.trans (le_abs_self _)) e)
· exact le_max_of_le_left (pow_le_pow_left₀ (abs_nonneg _)
(by rw [abs_of_nonpos h]; exact (neg_le_neg hlo).trans (neg_le_abs _)) e)
/-! ## Flat polynomials -/
/-- A polynomial: a list of monomials `(coeff, exponents)`, one exponent per variable. -/
abbrev Poly (α : Type) := List (α × List ℕ)
/-- `∏ xᵢ ^ eᵢ` (exponents and variables zipped; surplus entries ignored). -/
def monoR : List ℕ → List ℝ → ℝ
| e :: es, x :: xs => x ^ e * monoR es xs
| _, _ => 1
def evalR : Poly α → List ℝ → ℝ
| [], _ => 0
| (c, es) :: p, x => toReal c * monoR es x + evalR p x
def imono : List ℕ → List (Iv α) → Iv α
| e :: es, I :: Is => imul (ipow I e) (imono es Is)
| _, _ => (one, one)
/-- Natural (monomial-wise) interval enclosure of a polynomial over a box. -/
def ipoly : Poly α → List (Iv α) → Iv α
| [], _ => (zero, zero)
| (c, es) :: p, B => iadd (iscale c (imono es B)) (ipoly p B)
/-- `x` lies in the box `B`. -/
def InBox (x : List ℝ) (B : List (Iv α)) : Prop := List.Forall₂ Mem x B
theorem mem_imono {x : List ℝ} {B : List (Iv α)} (hx : InBox x B) (es : List ℕ) :
Mem (monoR es x) (imono es B) := by
induction es generalizing x B with
| nil =>
cases x <;> cases B <;> simp [monoR, imono, Mem]
| cons e es ih =>
cases hx with
| nil => simp [monoR, imono, Mem]
| cons hxi hrest => exact mem_imul (mem_ipow hxi e) (ih hrest)
theorem mem_ipoly {x : List ℝ} {B : List (Iv α)} (hx : InBox x B) (p : Poly α) :
Mem (evalR p x) (ipoly p B) := by
induction p with
| nil => simp [evalR, ipoly, Mem]
| cons m p ih =>
obtain ⟨c, es⟩ := m
exact mem_iadd (mem_iscale c (mem_imono hx es)) ih
/-! ## Boxes and splitting -/
def mid (I : Iv α) : α := half (add I.1 I.2)
def lowerHalf (B : List (Iv α)) (i : ℕ) : List (Iv α) :=
match B[i]? with
| some I => B.set i (I.1, mid I)
| none => B
def upperHalf (B : List (Iv α)) (i : ℕ) : List (Iv α) :=
match B[i]? with
| some I => B.set i (mid I, I.2)
| none => B
theorem inBox_set {x : List ℝ} {B : List (Iv α)} (hx : InBox x B) (i : ℕ) (J : Iv α)
(hJ : ∀ y, x[i]? = some y → Mem y J) : InBox x (B.set i J) := by
induction hx generalizing i with
| nil => simp [InBox]
| cons h hrest ih =>
cases i with
| zero => exact List.Forall₂.cons (hJ _ rfl) hrest
| succ i => exact List.Forall₂.cons h (ih i (fun y hy => hJ y hy))
theorem inBox_get {x : List ℝ} {B : List (Iv α)} (hx : InBox x B) (i : ℕ) (I : Iv α)
(hI : B[i]? = some I) : ∀ y, x[i]? = some y → Mem y I := by
induction hx generalizing i with
| nil => simp at hI
| cons h hrest ih =>
cases i with
| zero =>
intro y hy
simp only [List.getElem?_cons_zero, Option.some.injEq] at hI hy
subst hI; subst hy; exact h
| succ i => simpa using ih i hI
theorem inBox_split {x : List ℝ} {B : List (Iv α)} (hx : InBox x B) (i : ℕ) :
InBox x (lowerHalf B i) ∨ InBox x (upperHalf B i) := by
unfold lowerHalf upperHalf
cases hBi : B[i]? with
| none => exact Or.inl hx
| some I =>
have hget := inBox_get hx i I hBi
have hm : toReal (mid I) = (toReal I.1 + toReal I.2) / 2 := by simp [mid]
by_cases hle : ∀ y, x[i]? = some y → y ≤ toReal (mid I)
· left
exact inBox_set hx i _ fun y hy => ⟨(hget y hy).1, hle y hy⟩
· right
simp only [not_forall, not_le] at hle
obtain ⟨y₀, hy₀, hlt⟩ := hle
exact inBox_set hx i _ fun y hy => by
rw [hy₀] at hy; cases hy
exact ⟨hlt.le, (hget y₀ hy₀).2⟩
theorem inBox_append {x : List ℝ} {B : List (Iv α)} (hx : InBox x B) {r : ℝ} {J : Iv α}
(hr : Mem r J) : InBox (x ++ [r]) (B ++ [J]) :=
List.rel_append hx (List.Forall₂.cons hr List.Forall₂.nil)
/-! ## Certificates -/
/-- `ok`: all goals are positive on the leaf; `out h`: hypothesis number `h` fails there. -/
inductive Verdict
| ok
| out (h : ℕ)
/-- A binary midpoint-subdivision tree; each leaf carries an enclosure `[a, b]` of `r`. -/
inductive Tree (α : Type)
| node (i : ℕ) (l r : Tree α)
| leaf (v : Verdict) (a b : α)
/-- A certificate over polynomials of type `P`: box of the subdivision variables, `r² = rSq`,
hypotheses `hyp ≥ 0` and goals `goal > 0` (their last variable is `r`), and the tree. -/
structure Cert (α P : Type) where
box : List (Iv α)
rSq : P
hyps : List P
goals : List P
tree : Tree α
variable {P : Type} (evalP : P → List ℝ → ℝ) (enc : P → List (Iv α) → Option (Iv α))
/-- An enclosure function is sound if every enclosure it returns contains the value. -/
def EnclSound : Prop :=
∀ (p : P) (x : List ℝ) (B : List (Iv α)) (I : Iv α), InBox x B → enc p B = some I →
Mem (evalP p x) I
/-- The natural (monomial-wise) enclosure of flat polynomials. -/
def ipolyEnc (p : Poly α) (B : List (Iv α)) : Option (Iv α) := some (ipoly p B)
theorem ipolyEnc_sound : EnclSound (α := α) evalR ipolyEnc := by
intro p x B I hx h
simp only [ipolyEnc, Option.some.injEq] at h
exact h ▸ mem_ipoly hx p
def rbOK (C : Cert α P) (B : List (Iv α)) (a b : α) : Bool :=
match enc C.rSq B with
| some R => ble zero a && ble zero b && (ble a zero || ble (mul a a) R.1) && ble R.2 (mul b b)
| none => false
def goalOK (B : List (Iv α)) (g : P) : Bool :=
match enc g B with
| some I => blt zero I.1
| none => false
def hypOut (B : List (Iv α)) (p : P) : Bool :=
match enc p B with
| some I => blt I.2 zero
| none => false
def leafOK (C : Cert α P) (B : List (Iv α)) : Verdict → α → α → Bool
| .ok, a, b => C.goals.all (goalOK enc (B ++ [(a, b)]))
| .out h, a, b =>
match C.hyps[h]? with
| some p => hypOut enc (B ++ [(a, b)]) p
| none => false
def checkTree (C : Cert α P) : Tree α → List (Iv α) → Bool
| .node i l r, B => checkTree C l (lowerHalf B i) && checkTree C r (upperHalf B i)
| .leaf v a b, B => rbOK enc C B a b && leafOK enc C B v a b
/-- The certificate checker, parametrized by the enclosure function. -/
def check (C : Cert α P) : Bool := checkTree enc C C.tree C.box
variable {evalP enc}
theorem r_mem_of_rbOK (henc : EnclSound evalP enc) {C : Cert α P} {B : List (Iv α)}
{a b : α} (h : rbOK enc C B a b = true) {x : List ℝ} (hx : InBox x B) {r : ℝ}
(hr : 0 < r) (hr2 : r ^ 2 = evalP C.rSq x) : Mem r ((a, b) : Iv α) := by
unfold rbOK at h
cases hR : enc C.rSq B with
| none => rw [hR] at h; simp at h
| some R =>
rw [hR] at h
simp only [Bool.and_eq_true, Bool.or_eq_true, ble_iff, toReal_zero, toReal_mul] at h
obtain ⟨⟨⟨ha, hb⟩, hlo⟩, hhi⟩ := h
have hmem := henc C.rSq x B R hx hR
unfold Mem at hmem ⊢
rw [← hr2] at hmem
constructor
· rcases hlo with ha0 | hlo
· linarith
· nlinarith [hmem.1]
· nlinarith [hmem.2]
theorem checkTree_sound (henc : EnclSound evalP enc) (C : Cert α P) (t : Tree α)
(B : List (Iv α)) (h : checkTree enc C t B = true) (x : List ℝ) (hx : InBox x B) (r : ℝ)
(hr : 0 < r) (hr2 : r ^ 2 = evalP C.rSq x)
(hhyp : ∀ p ∈ C.hyps, 0 ≤ evalP p (x ++ [r])) :
∀ g ∈ C.goals, 0 < evalP g (x ++ [r]) := by
induction t generalizing B with
| node i l rt ihl ihr =>
simp only [checkTree, Bool.and_eq_true] at h
rcases inBox_split hx i with hl | hr'
· exact ihl _ h.1 hl
· exact ihr _ h.2 hr'
| leaf v a b =>
simp only [checkTree, Bool.and_eq_true] at h
obtain ⟨hrb, hleaf⟩ := h
have hxr := inBox_append hx (r_mem_of_rbOK henc hrb hx hr hr2)
cases v with
| ok =>
intro g hg
simp only [leafOK, List.all_eq_true] at hleaf
have hg' := hleaf g hg
unfold goalOK at hg'
cases hI : enc g (B ++ [(a, b)]) with
| none => rw [hI] at hg'; simp at hg'
| some I =>
rw [hI] at hg'
have hpos := (blt_iff _ _).mp hg'
rw [toReal_zero] at hpos
exact lt_of_lt_of_le hpos (henc g _ _ I hxr hI).1
| out j =>
simp only [leafOK] at hleaf
split at hleaf
· rename_i p hj
unfold hypOut at hleaf
split at hleaf
· rename_i I hI
have hneg := (blt_iff _ _).mp hleaf
rw [toReal_zero] at hneg
have h1 := hhyp p (List.mem_of_getElem? hj)
have h2 := (henc p _ _ I hxr hI).2
intro g _
exfalso; linarith
· simp at hleaf
· simp at hleaf
/-- **Soundness of the checker.** If `check enc C = true` for a sound enclosure `enc`, then
for every point `x` of the box and every `r > 0` with `r² = rSq(x)` satisfying all
hypotheses, every goal is positive at `(x, r)`. -/
theorem check_sound (henc : EnclSound evalP enc) (C : Cert α P) (h : check enc C = true)
(x : List ℝ) (hx : InBox x C.box) (r : ℝ) (hr : 0 < r) (hr2 : r ^ 2 = evalP C.rSq x)
(hhyp : ∀ p ∈ C.hyps, 0 ≤ evalP p (x ++ [r])) :
∀ g ∈ C.goals, 0 < evalP g (x ++ [r]) :=
checkTree_sound henc C C.tree C.box h x hx r hr hr2 hhyp
end BirkhoffGlobalSection.TangentialHessian.Checker