Tutorial
Proving with cas
How to state a goal so that cas and cega close it, and what to do when they refuse.
The tactics work on the goal you wrote. You state the result with Mathlib’s functions and the hypotheses your argument needs, and a tactic proves exactly that statement, or fails and leaves the goal as it was. For me that’s the property that matters most.
Mathlib’s Real.log is defined everywhere, with and . So is , or , depending on what you know about , and cas proves whichever one your hypotheses support:
example (x : ℝ) (hx : 0 < x) : Real.exp (Real.log x) = x := by cas example (x : ℝ) (hx : x ≠ 0) : Real.exp (Real.log x) = |x| := by cas example (x : ℝ) (hx : x < 0) : Real.exp (Real.log x) = -x := by cas
Lean accepts the proof.
Nothing is assumed along the way. If a condition is missing, it’s reported, never added.
Choosing a tactic#
| Goal | Tactic |
|---|---|
| exact algebra, identities, derivatives, regularity, integral formulas | cas |
| a side condition that a hypothesis supplies | cas using h |
| numerical bounds on a box (each variable between two rational bounds), on an integral or on a constant | cega |
| a rewrite inside a hypothesis or a goal | cas_nf |
| not sure | cas? or cega?, which suggest the call to keep |
The one place to be careful is an integral. There cas looks for an exact formula and cega proves a numerical bound, and neither switches to the other, so pick the one that matches what your goal states.
Compute first, then state#
The habit I’d recommend is to compute with #cas until you know what’s true, and only then write the statement. Say I want to show that is increasing on all of . By Mathlib’s strictMono_of_deriv_pos, it is if its derivative is positive everywhere. #cas computes the derivative and shows that it has no real zero, so the derivative keeps the sign of its leading coefficient:
#cas [derive x] (x ^ 3 - 3 * x ^ 2 + 4 * x)
3*x^2 - 6*x + 4Checked by Lean’s kernel: Derivative of the input
∀ (x : ℝ), HasDerivAt (fun x ↦ x ^ 3 - 3 * x ^ 2 + 4 * x) (3 * x ^ 2 - 6 * x + 4) x
from CAS.Command.hasDerivAt_of_displayRoute
The certificate: 5 checked steps
Derivative of the input
∀ (x : ℝ), HasDerivAt (fun x ↦ x ^ 3 - 3 * x ^ 2 + 4 * x) (3 * x ^ 2 - 6 * x + 4) x
Partial derivative in x: indexed rules compute Expr.diff
(CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0]))).rawDerivative = CAS.Verified.Expr.diff 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0]))
Partial derivative in x: normalization preserves evaluation
∀ (ρ : Fin 1 → ℝ), CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0]))).simplifiedDerivative = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0]))).rawDerivative
Partial derivative in x: side-conditioned display normalization
∀ (ρ : Fin 1 → ℝ), CAS.Verified.Expr.Alg.SideHolds ρ (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0])))).nonzero → CAS.Verified.Expr.eval ρ (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0])))).rhs = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0]))).simplifiedDerivative
Partial derivative in x: analytic semantics of the displayed result
∀ (x : ℝ^1), CAS.Verified.Expr.ValidAt x.ofLp (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0])) → CAS.Verified.Expr.Alg.SideHolds x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0])))).nonzero → (fderiv ℝ (fun y ↦ CAS.Verified.Expr.eval y.ofLp (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0]))) x) (EuclideanSpace.single 0 1) = CAS.Verified.Expr.eval x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (((𝕏[0].pow 3).sub ((CAS.Verified.Expr.nat 3).mul (𝕏[0].pow 2))).add ((CAS.Verified.Expr.nat 4).mul 𝕏[0])))).rhs
The computation
Variables: x
Analytic domain conditions
Raw checked nonzero requirements: (none) Raw checked positivity requirements: (none)
Partial derivative in x
1. Apply symbolic differentiation rules
raw result: 3*x^2*1 - 0*x^2 - 3*2*x^1*1 + 0*x + 4*1
2. Normalize the symbolic derivative unconditionally
normalized result: 3*x^2 - 6*x + 4
3. Apply checked side-conditioned cleanup
displayed result: 3*x^2 - 6*x + 4 additional nonzero requirements: (none)
#cas [roots x] (3 * x ^ 2 - 6 * x + 4 = 0)
complete list of real solutions
no real solutions- Input (polynomial equation)
(3 * x ^ 2 - 6 * x + 4 = 0)
Checked by Lean’s kernel: Complete list of real solutions
∀ (x : ℝ), 3 * x ^ 2 - 6 * x + 4 = 0 → False
from CAS.Polynomial.RealRoot.of_exprRootCheck
The certificate: 1 checked step
Complete list of real solutions
∀ (x : ℝ), 3 * x ^ 2 - 6 * x + 4 = 0 → False
The computation
Certified isolation
Square-free polynomial of degree 2, Cauchy bound 13/3, 3 cells Guarantee: the listed solutions are distinct, each rational is a solution, each decimal interval contains exactly one solution, and there are no other real solutions
In the proof I state the derivative and let cas prove it, then show that it is positive by writing it as , an identity that cas proves too. And since a strictly monotone function is injective, the cubic takes no value twice:
theorem cubic_strictMono : StrictMono (fun x : ℝ ↦ x ^ 3 - 3 * x ^ 2 + 4 * x) := by apply strictMono_of_deriv_pos intro x have h : deriv (fun x : ℝ ↦ x ^ 3 - 3 * x ^ 2 + 4 * x) x = 3 * x ^ 2 - 6 * x + 4 := by cas rw [h] calc (0 : ℝ) < 3 * (x - 1) ^ 2 + 1 := by positivity _ = 3 * x ^ 2 - 6 * x + 4 := by cas theorem cubic_injective (a b : ℝ) (h : a ^ 3 - 3 * a ^ 2 + 4 * a = b ^ 3 - 3 * b ^ 2 + 4 * b) : a = b := cubic_strictMono.injective h #print axioms cubic_injective
cubic_injective depends on the axioms propext, Classical.choice, Quot.sound.
Conditions from hypotheses#
When a rule needs a condition, you hand over the hypothesis that proves it with using. A logarithm of needs , which gives, and needs on the whole interval:
example (x : ℝ) (hx : x < 0) : deriv (fun t : ℝ ↦ Real.log (-t)) x = 1 / x := by cas using hx example (a b : ℝ) (hnonzero : ∀ t ∈ Set.uIcc a b, t ≠ 0) : (∫ t in a..b, 1 / t) = Real.log b - Real.log a := by cas using hnonzero
Lean accepts the proof.
Without the condition the call fails, and the message names what it needed. Here is false at , where Lean’s division gives , and the message lists as the condition to supply:
example (x : ℝ) : x / x = 1 := by cas
Error
cas could not close this ordinary arithmetic goal. This goal contains branch-sensitive operations and may need nonzero denominator hypotheses. Put those facts in the local context; pass one explicit fact as `cas using h` when needed. The corresponding `#cas [simplify]` report shows the exact generated conditions. Detected candidate conditions: x ≠ 0.
The rest of the message
Polynomial equation route:
`grind` failed
case grind
x : ℝ
h : ¬x * x⁻¹ = 1
⊢ False
[grind] Goal diagnostics
[facts] Asserted facts
[prop] ¬x * x⁻¹ = 1
[prop] if x = 0 then x⁻¹ = 0 else x * x⁻¹ = 1
[eqc] True propositions
[prop] if x = 0 then x⁻¹ = 0 else x * x⁻¹ = 1
[eqc] False propositions
[prop] x * x⁻¹ = 1
[eqc] Equivalence classes
[eqc] {1, (x * x⁻¹ - 1) * (x * x⁻¹ - 1)⁻¹}
[ring] Ring `ℝ`
[basis] Basis
[_] x * x⁻¹ * (x * x⁻¹ - 1)⁻¹ + -1 * (x * x⁻¹ - 1)⁻¹ + -1 = 0
[limits] Thresholds reached
[limit] maximum number of E-matching rounds has been reached, threshold: `(ematch := 0)`
[limit] maximum number of case-splits has been reached, threshold: `(splits := 0)`
Checked identity route:
cas could not prove the real analytic domain conditions.
A denominator or logarithm argument may need to be nonzero, a square-root argument may need to be positive, or an inverse-trigonometric argument may need to avoid a branch endpoint. Supply the relevant ordinary hypothesis with `cas using h`.
Unproved conditions:
x ≠ 0
Grind fallback:
`grind` failed (`set_option trace.cas.fallback true` shows its diagnostics)Numbers inside a proof#
Suppose I want to know how large is. The first integral is , which #cas evaluates exactly. The second has no elementary closed form, so for an estimate I need its digits:
#cas [integrate x in [0, 1]] (1 / (1 + x ^ 2))
definite integral
1/4*pi- Primitive
atan(x)
Checked by Lean’s kernel: Definite integral, proved by cas
∫ (x : ℝ) in 0..1, 1 / (1 + x ^ 2) = 1 / 4 * Real.pi
The certificate: 1 checked step
Definite integral, proved by
cas∫ (x : ℝ) in 0..1, 1 / (1 + x ^ 2) = 1 / 4 * Real.pi
The computation
Variables: x
#cas [nintegrate x in [0, 1]] (exp (-x^2))
certified value of the integral
0.746824 ± 10^-6- Domain
x ∈ [0, 1]
Checked by Lean’s kernel: Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6
The certificate: 1 checked step
Certified Simpson panels in one kernel check
|(∫ (x : ℝ) in 0..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6
The computation
Certified quadrature
Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative
So the sum is , and is a safe bound to prove. In the proof, cas proves the exact value of the first integral, cega proves a bound on the second and one on , and linarith adds them up:
theorem integral_estimate : (∫ t in (0 : ℝ)..1, 1 / (1 + t ^ 2)) + (∫ x in (0 : ℝ)..1, Real.exp (-x ^ 2)) < 1.54 := by have h₁ : (∫ t in (0 : ℝ)..1, 1 / (1 + t ^ 2)) = Real.pi / 4 := by cas have h₂ : (∫ x in (0 : ℝ)..1, Real.exp (-x ^ 2)) ≤ 3 / 4 := by cega have hπ : Real.pi < 3.1416 := by cega rw [h₁] linarith #print axioms integral_estimate
integral_estimate depends on the axioms propext, Classical.choice, Quot.sound.
Behind the second have, cega encloses the Gaussian integral by Simpson’s rule, with the error on each panel bounded by the remainder (3.5.6) of DLMF §3.5, and it doubles the number of panels until the enclosure proves .
A primitive of my own#
When I already know a primitive, I hand it over, and cas checks it by differentiating. Here it is :
example (a b : ℝ) (h : ∀ t ∈ Set.uIcc a b, t ≠ 0) : (∫ t in a..b, Real.log t / t) = Real.log b ^ 2 / 2 - Real.log a ^ 2 / 2 := by let F : ℝ → ℝ := fun t ↦ Real.log t ^ 2 / 2 cas [integrate primitive := F using h]
Lean accepts the proof.
Keeping the call that worked#
Once a call works, keep the simplest one. cas? proves the goal as cas does and suggests the quieter call to keep in the source:
example (x : ℝ) : Real.cos (3 * x) = 4 * Real.cos x ^ 3 - 3 * Real.cos x := by cas?
Try this
cas Lean checked the suggested replacement against the original goal. Selected route: CAS route: elementary rewrites, then ordinary arithmetic.
When a call refuses#
A refusal leaves the goal as it was. For a numerical bound that is false or too tight, cega says which values its queries certified. This one is false, since is at , so no setting will prove it. The message reports the certified upper bound , which is exactly the maximum:
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : x * (1 - x) ≤ 1 / 5 := by cega
Error
cega box route: comparison with the stated bound: the certified value does not give the stated bound.
↑1 / ↑4 ≤ 1 / 5
Exact rational arithmetic did not close this comparison.
The query run was `cega [enclose x in [0, 1]]
(x *
(1 -
x))`; its certified upper bound 1/4 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]] (x * (1 - x))`) did not prove it either:
cega box route: comparison with the stated bound: the certified value does not give the stated bound.
↑1 / ↑4 ≤ 1 / 5
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: the enclosure of ((x * (1 - x)) - (1/5)) is [-11061966584728781/4611686018427387904, 27809727699012813/18446744073709551616]; its upper endpoint does not establish ≤ 0.
Box: x in [35/128, 71/256]. Subdivision depth 8 exhausted after 8 splits on this branch.
Try tighter input bounds or more subdivision depth.
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.When a call fails, read the message first. It names what was missing and, where there is one, the call or setting to try. After that, these are the things I reach for:
#casanswers the question itself:#cas [derive x]prints the derivative with its conditions,#cas [integrate x]a primitive and#cas [N 20]the digits of a constant, so that the goal can state what SymLean computes.cas?andcega?show the route that closed a goal and suggest the call to keep.- When a message names an option,
Optionslists it with its default, andset_option name value insets it for one call. set_option trace.cas.fallback true inshows the full diagnostics of thegrindandnorm_numfallbacks ofcas.
Every theorem on this page depends only on propext, Classical.choice and Quot.sound, as #print axioms shows, and What you trust explains why.
