Tactic
cas
Proves the goal by computer algebra whose steps Lean’s kernel checks: identities, derivatives, integrals, limits and bounds.
cascas [l₁, l₂]Inside a proof, cas is the main entry point. I wanted one tactic to try on a calculation without first deciding what kind of calculation it is. You state what you want, in Mathlib’s own terms, and write cas. It tries a fixed list of routes, from determinants and ideals to derivatives, integrals and bounds on a box, and every route ends in a proof that Lean checks against your goal. If no route works, the goal stays as it was and the message tells you what was tried. That is all a refusal means: it is not a proof that the goal is false.
cas using h passes a hypothesis such as x ≠ 0 as evidence for the routes, and it has its own page. So does cas [integrate primitive := F], which checks a primitive that you supply.
Examples (24)#
Basic Examples (3)
The Pythagorean identity:
example (x : ℝ) : Real.sin x ^ 2 + Real.cos x ^ 2 = 1 := by cas
Lean accepts the proof.
A derivative, stated with Mathlib’s deriv:
example (x : ℝ) : deriv Real.sin x = Real.cos x := by cas
Lean accepts the proof.
A definite integral. cas finds an antiderivative and checks it:
example : (∫ x in (0 : ℝ)..1, x * Real.exp x) = 1 := by cas
Lean accepts the proof.
Scope (8)
An identity that needs a hypothesis. Without hx, the statement is false in Lean at :
example (x : ℝ) (hx : x ^ 2 - 1 ≠ 0) : (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1 := by cas
Lean accepts the proof.
Higher derivatives, nested ones included:
example (x : ℝ) : iteratedDeriv 3 (fun x ↦ Real.sin (2 * x)) x = -8 * Real.cos (2 * x) := by cas
Lean accepts the proof.
example (x : ℝ) (hx : 0 < x) : deriv (fun x ↦ deriv Real.sqrt x) x = -1 / (4 * x * Real.sqrt x) := by cas
Lean accepts the proof.
Smoothness and analyticity:
example : AnalyticAt ℝ (fun x ↦ Real.exp (Real.sin x) / (1 + x ^ 2)) 0 := by cas
Lean accepts the proof.
example : ContinuousOn (fun x ↦ Real.log x / x) (Set.Ioi 0) := by cas
Lean accepts the proof.
Integrals with symbolic limits, and over the whole line:
example (a b : ℝ) : (∫ t in a..b, Real.sin t) = Real.cos a - Real.cos b := by cas
Lean accepts the proof.
example : ∫ x : ℝ, 1 / (1 + x ^ 2) = Real.pi := by cas
Lean accepts the proof.
Limits, written with Mathlib’s filters:
open Filter Topology in example : Tendsto (fun x : ℝ ↦ Real.sin x / x) (𝓝[≠] 0) (𝓝 1) := by cas
Lean accepts the proof.
open Filter Topology in example : Tendsto (fun x : ℝ ↦ x * Real.exp (-x)) atTop (𝓝 0) := by cas
Lean accepts the proof.
The sign of a polynomial, and how many real roots it has:
example : ∀ x : ℝ, x ^ 4 - 4 * x + 3 ≥ 0 := by cas
Lean accepts the proof.
open Polynomial in example : Fintype.card ((X ^ 5 - 4 * X + 2 : ℚ[X]).rootSet ℝ) = 3 := by cas
Lean accepts the proof.
A bound on a box, from the hypotheses on x and y:
example (x y : ℝ) (hx : 0 ≤ x) (hx' : x ≤ 1) (hy : 2 ≤ y) (hy' : y ≤ 3) : Real.exp (x * y) ≤ 21 := by cas
Lean accepts the proof.
Matrices and polynomials over :
example : (!![(1 : ℚ), 2; 3, 4])⁻¹ = !![-2, 1; 3 / 2, -1 / 2] := by cas
Lean accepts the proof.
open Polynomial in example : gcd (X ^ 2 - 1 : ℚ[X]) (X ^ 2 - 2 * X + 1) = X - 1 := by cas
Lean accepts the proof.
Options (2)
cas [l₁, l₂] hands lemmas to the routes. Here an applied hypothesis rewrites f (x + 1) first:
example (f : ℝ → ℝ) (x : ℝ) (hf : ∀ t, f t = t ^ 2) : f (x + 1) = x ^ 2 + 2 * x + 1 := by cas [hf (x + 1)]
Lean accepts the proof.
A definition to unfold works the same way:
def shifted (t : ℝ) : ℝ := t ^ 2 + 2 * t example (x : ℝ) : shifted (x - 1) = x ^ 2 - 1 := by cas [shifted]
Lean accepts the proof.
Applications (2)
cas proves a bound that linarith then uses. Only the three standard axioms are used:
theorem exp_lt_four (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.exp x < 4 := by have h : Real.exp x ≤ 3 := by cas linarith #print axioms exp_lt_four
exp_lt_four depends on the axioms propext, Classical.choice, Quot.sound.
Nicomachus’s theorem, the sum of the first cubes:
example (n : ℕ) : ∑ k ∈ Finset.range (n + 1), (k : ℝ) ^ 3 = (n * (n + 1) / 2) ^ 2 := by cas
Lean accepts the proof.
Properties and Relations (2)
#cas prints a theorem, and cas proves the same statement in a proof:
#cas [derive x] (tan x)
1/cos(x)^2- Conditions
Checked by Lean’s kernel: Derivative of the input
∀ (x : ℝ), Real.cos x ≠ 0 → HasDerivAt (fun x ↦ Real.tan x) (1 / Real.cos x ^ 2) x
from CAS.Command.hasDerivAt_of_displayRoute
The certificate: 5 checked steps
Derivative of the input
∀ (x : ℝ), Real.cos x ≠ 0 → HasDerivAt (fun x ↦ Real.tan x) (1 / Real.cos x ^ 2) x
Partial derivative in x: indexed rules compute Expr.diff
(CAS.Verified.Expr.DiffDerivation.build 0 (𝕏[0].sin.div 𝕏[0].cos)).rawDerivative = CAS.Verified.Expr.diff 0 (𝕏[0].sin.div 𝕏[0].cos)
Partial derivative in x: normalization preserves evaluation
∀ (ρ : Fin 1 → ℝ), CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (𝕏[0].sin.div 𝕏[0].cos)).simplifiedDerivative = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (𝕏[0].sin.div 𝕏[0].cos)).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].sin.div 𝕏[0].cos))).nonzero → CAS.Verified.Expr.eval ρ (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (𝕏[0].sin.div 𝕏[0].cos))).rhs = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (𝕏[0].sin.div 𝕏[0].cos)).simplifiedDerivative
Partial derivative in x: analytic semantics of the displayed result
∀ (x : ℝ^1), CAS.Verified.Expr.ValidAt x.ofLp (𝕏[0].sin.div 𝕏[0].cos) → CAS.Verified.Expr.Alg.SideHolds x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (𝕏[0].sin.div 𝕏[0].cos))).nonzero → (fderiv ℝ (fun y ↦ CAS.Verified.Expr.eval y.ofLp (𝕏[0].sin.div 𝕏[0].cos)) x) (EuclideanSpace.single 0 1) = CAS.Verified.Expr.eval x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (𝕏[0].sin.div 𝕏[0].cos))).rhs
The computation
Variables: x
Analytic domain conditions
Raw checked nonzero requirements: cos(x) Raw checked positivity requirements: (none)
Partial derivative in x
1. Apply symbolic differentiation rules
raw result: (cos(x)*1*cos(x) - sin(x)*-sin(x)*1)/cos(x)^2
2. Normalize the symbolic derivative unconditionally
normalized result: 1/cos(x)^2
3. Apply checked side-conditioned cleanup
displayed result: 1/cos(x)^2 additional nonzero requirements: (none)
example (x : ℝ) (h : Real.cos x ≠ 0) : HasDerivAt (fun x ↦ Real.tan x) (1 / Real.cos x ^ 2) x := by cas
Lean accepts the proof.
cas? runs the same routes and tells you the call to keep in your proof:
example (x : ℝ) (h₀ : 0 ≤ x) (h₁ : x ≤ 1) : Real.exp x ≤ 3 := by cas?
Try this
cega
Lean checked the suggested replacement against the original goal. Selected route: CAS route: checked CEGA box enclosure of an ordinary inequality.Possible Issues (4)
A false statement is refused. The message lists what each route tried, and it does not prove that the goal is false:
example (x : ℝ) : Real.sin x ^ 2 + Real.cos x ^ 2 = 2 := by cas
Error
cas could not prove this arithmetic relation. The statement may be false, or it may lie outside the supported normalization and arithmetic fragment
The rest of the message
Polynomial equation route:
`grind` failed
case grind
x : ℝ
h : ¬Real.sin x ^ 2 + Real.cos x ^ 2 = 2
⊢ False
[grind] Goal diagnostics
[facts] Asserted facts
[prop] ¬Real.sin x ^ 2 + Real.cos x ^ 2 = 2
[eqc] False propositions
[prop] Real.sin x ^ 2 + Real.cos x ^ 2 = 2
[eqc] Equivalence classes
[eqc] {1, (Real.sin x ^ 2 + Real.cos x ^ 2 - 2) * (Real.sin x ^ 2 + Real.cos x ^ 2 - 2)⁻¹}
[ematch] E-matching patterns
[thm] Nat.pow_pos: [@HPow.hPow `[ℕ] `[ℕ] `[ℕ] `[instHPow] #2 #1]
[thm] Nat.div_pow_of_pos: [@HPow.hPow `[ℕ] `[ℕ] `[ℕ] `[instHPow] #2 #1]
[ring] Ring `ℝ`
[basis] Basis
[_] Real.sin x ^ 2 * (Real.sin x ^ 2 + Real.cos x ^ 2 - 2)⁻¹ +
Real.cos x ^ 2 * (Real.sin x ^ 2 + Real.cos x ^ 2 - 2)⁻¹ +
-2 * (Real.sin x ^ 2 + Real.cos x ^ 2 - 2)⁻¹ +
-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 identify the two sides: the checked normalizer does not reduce their difference to zero.
Normalized difference:
CAS.Verified.Expr.int (-1)
canonical form route: the canonical rational form of the difference is not zero.
Canonical difference:
((𝕏[0].sin.pow 2).add (𝕏[0].cos.pow 2)).sub (CAS.Verified.Expr.nat 2)
Elementary rewrite route:
no elementary rewrite applies
Grind fallback:
`grind` failed (`set_option trace.cas.fallback true` shows its diagnostics)A wrong derivative. #cas [derive x] (x ^ 2) prints the one that is right:
example (x : ℝ) : deriv (fun x ↦ x ^ 2) x = 3 * x := by cas
Error
cas computed a checked derivative, but it does not match the stated result. Check the claimed derivative, or run the corresponding `#cas [derive]` command to inspect the normalized result.
The rest of the message
Remaining conversion goals:
x = 0
Checked normalizer route:
cas could not identify the two sides: the checked normalizer does not reduce their difference to zero.
Normalized difference:
𝕏[0].neg
canonical form route: the canonical rational form of the difference is not zero.
Canonical difference:
𝕏[0].negcas has no modes. The words of #cas, such as derive and integrate, are not lemmas:
example (x : ℝ) : deriv Real.sin x = Real.cos x := by cas [derive]
Error
Unknown identifier `derive`
The box route needs rational bounds on every variable. For all real , this one is Mathlib’s Real.add_one_le_exp:
example (x : ℝ) : Real.exp x ≥ x + 1 := by cas
Error
cas could not prove this arithmetic relation. The statement may be false, or it may lie outside the supported normalization and arithmetic fragment Box enclosure route: cas 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 Command theorem route: cas: the expression is not a polynomial with rational coefficients Grind fallback: `grind` failed (`set_option trace.cas.fallback true` shows its diagnostics)
Neat Examples (3)
A power of 2 with a 27 digit exponent, modulo a prime:
example : (2 : ℕ) ^ 123456789123456789123456789 % 1000000007 = 612059118 := by cas
Lean accepts the proof.
You can teach cas a derivative. Here is a function it cannot read, the integral of a Gaussian from 0 to , with the fundamental theorem of calculus registered as a rule:
noncomputable def gaussInt (x : ℝ) : ℝ := ∫ t in (0 : ℝ)..x, Real.exp (-t ^ 2) theorem hasDerivAt_gaussInt (x : ℝ) : HasDerivAt gaussInt (Real.exp (-x ^ 2)) x := (Continuous.integral_hasStrictDerivAt (by fun_prop) 0 x).hasDerivAt attribute [cas_deriv] hasDerivAt_gaussInt
Lean accepts the proof.
Now cas applies the rule together with the chain rule:
example (x : ℝ) : deriv (fun x ↦ gaussInt (x ^ 2)) x = 2 * x * Real.exp (-x ^ 4) := by cas
Lean accepts the proof.
A product that telescopes:
example : ∀ n : ℕ, ∏ k ∈ Finset.Icc 1 n, ((k : ℝ) + 1) / k = n + 1 := by cas
Lean accepts the proof.
See Also
cas using · cas? · cas [integrate primitive := F] · cas_nf · cega · derive · integrate · limit
