Tactic
cega
Proves numerical bounds: inequalities on a box of real variables, closed constants and interval integrals.
cegacega (prec := n) (depth := n) (panels := n)cega [fast]cega.nativeCertificates is on, and is bare cega otherwise.cega [use_floats]cega [use_floats] (arith := F) chooses the IEEE format F, and cega [use_floats := false] selects exact rational endpoints.cega [batch := n]∀ i : Fin m, p i by checking n indices at a time in the kernel.cega [fast, batch := n]cega.nativeCertificates is on.cega is the tactic I use for numbers. A bound like for is true, obvious from a plot and tedious to prove by hand. cega encloses the expression on the box that your hypotheses describe, compares the enclosure with the bound by exact rational arithmetic, and hands the kernel a certificate for the whole argument.
The name is short for certified enclosures, guarantees and approximations.
Examples (27)#
Basic Examples (3)
A bound on a box, from the hypotheses on x:
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.sin x ≤ 0.85 := by cega
Lean accepts the proof.
A bound on a constant:
example : Real.exp 1 ≤ 2.72 := by cega
Lean accepts the proof.
A bound on an integral with no elementary antiderivative:
example : (∫ x in (0 : ℝ)..1, Real.exp (-x ^ 2)) ≤ 3 / 4 := by cega
Lean accepts the proof.
Scope (5)
A strict two-sided bound, and membership in an open interval:
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : 0 < x * (1 - x) + 1 ∧ x * (1 - x) + 1 < 2 := by cega
Lean accepts the proof.
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : x * (1 - x) ∈ Set.Ioo (-1 : ℝ) 1 := by cega
Lean accepts the proof.
Several bounds at once. The three expressions differ, so each bound is proved on its own:
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : 0 ≤ x / (1 + x) ∧ Real.exp x ≤ 3 ∧ |Real.sin x| < 1 := by cega
Lean accepts the proof.
A hypothesis |x| ≤ r gives the box . Here it also keeps the denominator away from zero:
example (x : ℝ) (hx : |x| ≤ 1 / 2) : 1 / (1 - x) ≤ 2 := by cega
Lean accepts the proof.
A trigonometric polynomial needs no bounds on its variable. FloatLib’s interval bounds by its coefficients, :
example (x : ℝ) : Real.sin x + Real.cos (2 * x) / 2 ≤ 3 / 2 := by cega
Lean accepts the proof.
Integrals with the limits reversed, and with an integrand that has a corner:
example : (∫ x in (1 : ℝ)..0, Real.exp x) < -1 := by cega
Lean accepts the proof.
example : 1 / 2 < ∫ x in (-1 : ℝ)..1, |x| := by cega
Lean accepts the proof.
Options (6)
(panels := 2) fixes the number of Simpson panels instead of doubling them:
example : |(∫ x in (0 : ℝ)..1, Real.exp x) - 17182818 / 10000000| ≤ 1 / 10000 := by cega (panels := 2)
Lean accepts the proof.
(depth := 3) allows the box to be cut in halves to depth 3:
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : x * (1 - x) ≤ 1 / 4 := by cega (depth := 3)
Lean accepts the proof.
cega [fast] with the default settings is bare cega, checked in the kernel:
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : x * x ≤ 1 := by cega [fast]
Lean accepts the proof.
[use_floats] computes the same bound with binary64 floats, and (arith := binary128) with binary128:
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.exp x ≤ 2.72 := by cega [use_floats]
Lean accepts the proof.
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.exp x ≤ 2.72 := by cega (arith := binary128)
Lean accepts the proof.
With floats, (depth := 5) evaluates the sine on 32 pieces of :
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.sin x ≤ 0.85 := by cega [use_floats] (depth := 5)
Lean accepts the proof.
A finite statement, checked three indices at a time, and then four at a time with the fast form:
example : ∀ i : Fin 10, i.val * i.val < 100 := by cega [batch := 3]
Lean accepts the proof.
example : ∀ i : Fin 10, i.val * i.val < 100 := by cega [fast, batch := 4]
Lean accepts the proof.
Applications (2)
cega proves the numerical fact and linarith finishes. Only the three standard axioms are used:
theorem cega_sin_lt_one (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.sin x < 1 := by have h : Real.sin x ≤ 17 / 20 := by cega linarith #print axioms cega_sin_lt_one
cega_sin_lt_one depends on the axioms propext, Classical.choice, Quot.sound.
The digits that N prints, as a bound:
example : Real.sqrt 2 ≤ 1.415 := by cega
Lean accepts the proof.
Properties and Relations (3)
cega? proves the goal and tells you the explicit query that did it:
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.sin x ≤ 0.85 := by cega?
Try this
cega Lean checked the suggested replacement against the original goal. Selected route: CEGA route: checked box enclosure of an ordinary inequality, arithmetic: exact rationals.
That query is the one enclose runs, and it prints the interval behind the proof:
#cas [enclose x in [0, 1]] (sin x)
certified interval in ℝ
[0, 0.841471]- Domain
x ∈ [0, 1] ⊆ ℝ- Upper bound
0.841471- Absolute bound
0.841471- Center and radius
≈0.420735 ± 0.420736
Checked by Lean’s kernel: Enclosure of the input
∀ x ∈ Set.Icc 0 1, 0 ≤ Real.sin x ∧ Real.sin x ≤ 0.841471
from CAS.Tactic.BoxInequality.mem_of_enclosedOn_box
The certificate: 8 checked steps
Enclosure of the input
∀ x ∈ Set.Icc 0 1, 0 ≤ Real.sin x ∧ Real.sin x ≤ 0.841471
Upper bound of the input
∀ x ∈ Set.Icc 0 1, Real.sin x ≤ 0.841471
Absolute bound of the input
∀ x ∈ Set.Icc 0 1, |Real.sin x| ≤ 0.841471
Center and radius of the input
∀ x ∈ Set.Icc 0 1, |Real.sin x - 0.420735| ≤ 0.420736
Certified enclosure
CAS.Certificate.EnclosedOn (CAS.Exact.exprFn 𝕏[0].sin) (CAS.Continuum.RationalInterval.boxDomain fun coordinate ↦ [CAS.Continuum.RationalInterval.ofBounds 0 1 ⋯].get coordinate) (↑0 / ↑1) (↑286337738405724074564845076175058320435 / ↑340282366920938463463374607431768211456)
Certified upper bound
CAS.Certificate.BoundOn (CAS.Exact.exprFn 𝕏[0].sin) (CAS.Continuum.RationalInterval.boxDomain fun coordinate ↦ [CAS.Continuum.RationalInterval.ofBounds 0 1 ⋯].get coordinate) (↑286337738405724074564845076175058320435 / ↑340282366920938463463374607431768211456)
Certified absolute bound
CAS.Certificate.BoundOn (fun x ↦ |CAS.Exact.exprFn 𝕏[0].sin x|) (CAS.Continuum.RationalInterval.boxDomain fun coordinate ↦ [CAS.Continuum.RationalInterval.ofBounds 0 1 ⋯].get coordinate) (↑286337738405724074564845076175058320435 / ↑340282366920938463463374607431768211456)
Certified center and radius
CAS.Certificate.ApproxOn (CAS.Exact.exprFn 𝕏[0].sin) (fun x ↦ ↑286337738405724074564845076175058320435 / ↑680564733841876926926749214863536422912) (CAS.Continuum.RationalInterval.boxDomain fun coordinate ↦ [CAS.Continuum.RationalInterval.ofBounds 0 1 ⋯].get coordinate) (↑286337738405724074564845076175058320435 / ↑680564733841876926926749214863536422912)
The computation
Coordinates: x Certified domain: x ∈ [0, 1] ⊆ ℝ
Certified numerical guarantee
Guarantee: the input lies in the output interval throughout the box Decimal preview: outward-rounded to 6 places where it is a bound Exact certified output: [0, 286337738405724074564845076175058320435/340282366920938463463374607431768211456]
cas tries the same box route on ordinary inequalities:
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.sin x ≤ 0.85 := by cas
Lean accepts the proof.
Possible Issues (6)
A false bound is refused. Here :
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.sin x ≤ 0.8 := by cega
Error
cega box route: comparison with the stated bound: the certified value does not give the stated bound.
↑286337738405724074564845076175058320435 / ↑340282366920938463463374607431768211456 ≤ 0.8
Exact rational arithmetic did not close this comparison.
The query run was `cega [enclose x in [0, 1]]
(Real.sin
x)`; its certified upper bound 286337738405724074564845076175058320435/340282366920938463463374607431768211456 may be too weak for the stated bound. If the bound is true, a deeper subdivision or a higher precision may prove it, as in `cega (depth := 4)` or `cega (prec := 24)`. FloatLib's check, which `cega` tries first, evaluates at most 4096 pieces of the box and cuts a branch at most 32 times.The rest of the message
Subdivision to depth 2 (`cega (depth := 2) [enclose x in [0, 1]] (Real.sin x)`) did not prove it either:
cega box route: comparison with the stated bound: the certified value does not give the stated bound.
↑286337738405724074564845076175058320435 / ↑340282366920938463463374607431768211456 ≤ 0.8
Exact rational arithmetic did not close this comparison.
Other routes:
carrier search:
cega found no registered checked carrier or direct certificate matching the target
interval route:
interval could not certify: on the original box, the expression is enclosed by [-3784231770697581387/4611686018427387904, 1654908495459613035/9223372036854775808]; its upper endpoint does not establish ≤ 0.
Box: x in [0, 1]. Verification used subdivision depth 8. Try tighter input bounds, more subdivision, or higher approximation settings.
The coefficient route did not prove it either: interval: the coefficients alone give the range [-1, 1] for the polynomial, which does not prove the goal; a sharp bound takes a sum-of-squares certificate `(upper := h)`, which `scripts/coeff_cert.py` finds
Bare `cega` proves an ordinary real inequality (`f x ≤ c`, `f x < c`, `c ≤ f x`, `c < f x` (also with `≥` and `>`), `|f x| ≤ c`, `|f x| < c`, `a ≤ f x ∧ f x ≤ b` (also with `<`), `f x ∈ Set.Icc a b` (also `Set.Ioo`, `Set.Ico`, `Set.Ioc`), `f x ≤ g x`, `f x < g x`, and conjunctions of these, with `c`, `a`, `b` closed and every real variable bounded by hypotheses `a ≤ x` and `x ≤ b`, `x ∈ Set.Icc a b`, or `|x| ≤ r` with rational literal endpoints), a bound on an interval integral `∫ x in a..b, f x` with rational limits, a certificate goal (`EnclosedOn f s lo hi`, `BoundOn f s c`, `ApproxOn f g s ε`, `ResidualBound r s ε`) on a coordinate box or from a checked carrier in the local context or the goal, a closed decidable statement about floats, which `float_decide` decides, a real inequality that FloatLib's `interval` proves, on finite sums and norms too, and a closed goal that interval evaluation proves.No hypothesis bounds here, so there is no box. The bound holds for every real , since , and nlinarith [sq_nonneg (x - 1 / 2)] proves it:
example (x : ℝ) : x * (1 - x) ≤ 1 / 4 := by cega
Error
cega box route: no rational bounds on `x` in the hypotheses; the route needs hypotheses `a ≤ x` and `x ≤ b`, `x ∈ Set.Icc a b`, or `|x| ≤ r` with rational literal endpoints
Other routes:
carrier search:
cega found no registered checked carrier or direct certificate matching the target
interval route:
interval needs a rational lower bound for x
Bare `cega` proves an ordinary real inequality (`f x ≤ c`, `f x < c`, `c ≤ f x`, `c < f x` (also with `≥` and `>`), `|f x| ≤ c`, `|f x| < c`, `a ≤ f x ∧ f x ≤ b` (also with `<`), `f x ∈ Set.Icc a b` (also `Set.Ioo`, `Set.Ico`, `Set.Ioc`), `f x ≤ g x`, `f x < g x`, and conjunctions of these, with `c`, `a`, `b` closed and every real variable bounded by hypotheses `a ≤ x` and `x ≤ b`, `x ∈ Set.Icc a b`, or `|x| ≤ r` with rational literal endpoints), a bound on an interval integral `∫ x in a..b, f x` with rational limits, a certificate goal (`EnclosedOn f s lo hi`, `BoundOn f s c`, `ApproxOn f g s ε`, `ResidualBound r s ε`) on a coordinate box or from a checked carrier in the local context or the goal, a closed decidable statement about floats, which `float_decide` decides, a real inequality that FloatLib's `interval` proves, on finite sums and norms too, and a closed goal that interval evaluation proves.Too few panels. The message gives the interval that the search proposes and the call to try next:
example : |(∫ x in (0 : ℝ)..1, Real.exp x) - 17182818 / 10000000| ≤ 1 / 10000 := by cega (panels := 1)
Error
cega: with 1 Simpson panel the search proposes [1.717917, 1.719805] for the integral, an interval of width less than 1.9e-3 that does not prove the stated bound. Each doubling of the panels divides the Simpson remainder by 16; try `cega (panels := 2)`.
The rest of the message
Other routes:
carrier search:
cega found no registered checked carrier or direct certificate matching the target
box route:
cega box route: unsupported closed subterm
∫ (x : ℝ) in 0..1, Real.exp x
The checked elementary expression language cannot evaluate this subterm, and no rational enclosure of it was supplied by hypotheses.
interval route:
interval needs a rational lower bound for ∫ (x : ℝ) in 0..1, Real.exp x
dyadic_interval route:
The proposition was not proven true or false.
Bare `cega` proves an ordinary real inequality (`f x ≤ c`, `f x < c`, `c ≤ f x`, `c < f x` (also with `≥` and `>`), `|f x| ≤ c`, `|f x| < c`, `a ≤ f x ∧ f x ≤ b` (also with `<`), `f x ∈ Set.Icc a b` (also `Set.Ioo`, `Set.Ico`, `Set.Ioc`), `f x ≤ g x`, `f x < g x`, and conjunctions of these, with `c`, `a`, `b` closed and every real variable bounded by hypotheses `a ≤ x` and `x ≤ b`, `x ∈ Set.Icc a b`, or `|x| ≤ r` with rational literal endpoints), a bound on an interval integral `∫ x in a..b, f x` with rational limits, a certificate goal (`EnclosedOn f s lo hi`, `BoundOn f s c`, `ApproxOn f g s ε`, `ResidualBound r s ε`) on a coordinate box or from a checked carrier in the local context or the goal, a closed decidable statement about floats, which `float_decide` decides, a real inequality that FloatLib's `interval` proves, on finite sums and norms too, and a closed goal that interval evaluation proves.
CEGA finite certificate could not certify a checked computation: the kernel did not reduce the decision procedure of
|(∫ (x : ℝ) in 0..1, Real.exp x) - 17182818 / 10000000| ≤ 1 / 10000
to `true`. Its decision procedure mentions `Real.decidableLE`, which is classical and never reduces in the kernel. Whether or not that is what stopped this check, the failure says nothing about the truth of the statement.An overflow. fits within binary64’s finite range but its square does not, so the call fails and names the format. cega (arith := binary128) and plain cega prove this bound:
example : (10 : ℝ) ^ 200 * 10 ^ 200 ≤ 10 ^ 401 := by cega [use_floats]
Error
cega box route: the query `cega (arith := binary64) [enclose]
(10 ^ 200 *
10 ^
200)` found no certificate: the binary64 evaluation found no enclosure: the enclosure of a product has an end beyond the largest finite binary64 number (overflow). A wider format, as in `(arith := binary128)`, or exact arithmetic, `cega [use_floats := false]`, may prove it.The rest of the message
Other routes:
carrier search:
cega found no registered checked carrier or direct certificate matching the target
interval route:
cega interval route: FloatLib's `interval` computes with exact rationals, so it is not run in binary64 arithmetic. `cega [use_floats := false]` runs it.
dyadic_interval route:
`dyadic_interval` computes with exact dyadic rationals, so it is not run in binary64 arithmetic. `cega [use_floats := false]` runs it.
Bare `cega` proves an ordinary real inequality (`f x ≤ c`, `f x < c`, `c ≤ f x`, `c < f x` (also with `≥` and `>`), `|f x| ≤ c`, `|f x| < c`, `a ≤ f x ∧ f x ≤ b` (also with `<`), `f x ∈ Set.Icc a b` (also `Set.Ioo`, `Set.Ico`, `Set.Ioc`), `f x ≤ g x`, `f x < g x`, and conjunctions of these, with `c`, `a`, `b` closed and every real variable bounded by hypotheses `a ≤ x` and `x ≤ b`, `x ∈ Set.Icc a b`, or `|x| ≤ r` with rational literal endpoints), a bound on an interval integral `∫ x in a..b, f x` with rational limits, a certificate goal (`EnclosedOn f s lo hi`, `BoundOn f s c`, `ApproxOn f g s ε`, `ResidualBound r s ε`) on a coordinate box or from a checked carrier in the local context or the goal, a closed decidable statement about floats, which `float_decide` decides, a real inequality that FloatLib's `interval` proves, on finite sums and norms too, and a closed goal that interval evaluation proves.Thirty digits of are more than binary64 has. cega (arith := binary128) proves this one:
example : Real.sqrt 2 ≤ 1.41421356237309504880168872421 := by cega [use_floats]
Error
cega box route: comparison with the stated bound: the certified value does not give the stated bound.
↑6369051672525773 / ↑4503599627370496 ≤ 1.41421356237309504880168872421
Exact rational arithmetic did not close this comparison.
The query run was `cega (arith := binary64) [enclose]
(√2)`; its certified upper bound 6369051672525773/4503599627370496 may be too weak for the stated bound. The goal has no variables, so subdivision does not apply, and the arithmetic is binary64. A wider format, as in `(arith := binary128)`, or exact arithmetic, `cega [use_floats := false]`, may prove it.The rest of the message
Other routes:
carrier search:
cega found no registered checked carrier or direct certificate matching the target
interval route:
cega interval route: FloatLib's `interval` computes with exact rationals, so it is not run in binary64 arithmetic. `cega [use_floats := false]` runs it.
dyadic_interval route:
`dyadic_interval` computes with exact dyadic rationals, so it is not run in binary64 arithmetic. `cega [use_floats := false]` runs it.
Bare `cega` proves an ordinary real inequality (`f x ≤ c`, `f x < c`, `c ≤ f x`, `c < f x` (also with `≥` and `>`), `|f x| ≤ c`, `|f x| < c`, `a ≤ f x ∧ f x ≤ b` (also with `<`), `f x ∈ Set.Icc a b` (also `Set.Ioo`, `Set.Ico`, `Set.Ioc`), `f x ≤ g x`, `f x < g x`, and conjunctions of these, with `c`, `a`, `b` closed and every real variable bounded by hypotheses `a ≤ x` and `x ≤ b`, `x ∈ Set.Icc a b`, or `|x| ≤ r` with rational literal endpoints), a bound on an interval integral `∫ x in a..b, f x` with rational limits, a certificate goal (`EnclosedOn f s lo hi`, `BoundOn f s c`, `ApproxOn f g s ε`, `ResidualBound r s ε`) on a coordinate box or from a checked carrier in the local context or the goal, a closed decidable statement about floats, which `float_decide` decides, a real inequality that FloatLib's `interval` proves, on finite sums and norms too, and a closed goal that interval evaluation proves.A batch that fails names its indices. A failed batch is not a counterexample. Either the check did not reduce to true, as here, or the kernel reached its recursion limit, and then the message proposes a larger maxRecDepth or smaller batches:
example : ∀ i : Fin 2, i.val = 0 := by cega [batch := 1]
Error
cega [batch := 1] could not certify batch 2 of 2, the indices i with 1 ≤ i < 2, of ∀ (i : Fin 2), ↑i = 0 The kernel did not reduce the check of this batch to `true`: the statement may be false for an index in this range, or its decision procedure may not reduce in the kernel.
Neat Examples (2)
The examples from here on run after open CAS.Certificate.
The exact range of on , stated as a certificate proposition on a box in . The first query falls short, and bare cega then bisects to depth 2, which cuts at 0 and at the turning points :
example : EnclosedOn (fun p : ℝ^1 ↦ p 0 / (1 + p 0 ^ 2)) {p | -2 ≤ p 0 ∧ p 0 ≤ 2} (-1 / 2 : ℝ) (1 / 2 : ℝ) := by cega
Lean accepts the proof.
The cosine on a short interval near 100:
example (x : ℝ) (h0 : 100 ≤ x) (h1 : x ≤ 100001 / 1000) : 0.86 ≤ Real.cos x := by cega
Lean accepts the proof.
See Also
cega? · cega using · enclose · nintegrate · N · cas · Options
