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 logx=log|x| and log0=0. So exp(logx) is x, |x| or −x, depending on what you know about x, and cas proves whichever one your hypotheses support:

In[1]:=
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
Out[1]=

Lean accepts the proof.

Nothing is assumed along the way. If a condition is missing, it’s reported, never added.

Choosing a tactic#

GoalTactic
exact algebra, identities, derivatives, regularity, integral formulascas
a side condition that a hypothesis suppliescas using h
numerical bounds on a box (each variable between two rational bounds), on an integral or on a constantcega
a rewrite inside a hypothesis or a goalcas_nf
not surecas? 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 x3−3x2+4x 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:

In[2]:=
#cas [derive x] (x ^ 3 - 3 * x ^ 2 + 4 * x)
Out[2]=
3x2−6x+4
3*x^2 - 6*x + 4

Checked 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
  1. Derivative of the input

    ∀ (x : ℝ), HasDerivAt (fun x ↦ x ^ 3 - 3 * x ^ 2 + 4 * x) (3 * x ^ 2 - 6 * x + 4) x

    Checked by Lean’s kernel · CAS.Command.hasDerivAt_of_displayRoute

  2. 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]))

    Checked by Lean’s kernel · CAS.Verified.Expr.DiffDerivation.rawDerivative_eq_diff

  3. 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

    Checked by Lean’s kernel · CAS.Verified.Expr.DiffDerivation.eval_simplifiedDerivative_eq_rawDerivative

  4. 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

    Checked by Lean’s kernel · CAS.Command.displayDerivativeRoute_sound

  5. 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

    Checked by Lean’s kernel · CAS.Command.fderiv_apply_single_eq_eval_displayDerivative

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)
In[3]:=
#cas [roots x] (3 * x ^ 2 - 6 * x + 4 = 0)
Out[3]=

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
  1. Complete list of real solutions

    ∀ (x : ℝ), 3 * x ^ 2 - 6 * x + 4 = 0 → False

    Checked by Lean’s kernel · CAS.Polynomial.RealRoot.of_exprRootCheck

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 3(x−1)2+1, an identity that cas proves too. And since a strictly monotone function is injective, the cubic takes no value twice:

In[4]:=
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
Out[4]=

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 −t needs −t≠0, which x<0 gives, and ∫abdt/t needs t≠0 on the whole interval:

In[5]:=
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
Out[5]=

Lean accepts the proof.

Without the condition the call fails, and the message names what it needed. Here x/x=1 is false at x=0, where Lean’s division gives 0, and the message lists x≠0 as the condition to supply:

In[6]:=
example (x : ℝ) : x / x = 1 := by
  cas
Out[6]=

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 ∫01dt1+t2+∫01e−t2dt is. The first integral is π/4, which #cas evaluates exactly. The second has no elementary closed form, so for an estimate I need its digits:

In[7]:=
#cas [integrate x in [0, 1]] (1 / (1 + x ^ 2))
Out[7]=

definite integral

14π
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
  1. Definite integral, proved by cas

    ∫ (x : ℝ) in 0..1, 1 / (1 + x ^ 2) = 1 / 4 * Real.pi

    Checked by Lean’s kernel

The computation
  • Variables: x
In[8]:=
#cas [nintegrate x in [0, 1]] (exp (-x^2))
Out[8]=

certified value of the integral

0.746824±10−6
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
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

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 1.5322…, and 1.54 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:

In[9]:=
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
Out[9]=

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 3/4.

A primitive of my own#

When I already know a primitive, I hand it over, and cas checks it by differentiating. Here it is F(t)=(logt)2/2:

In[10]:=
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]
Out[10]=

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:

In[11]:=
example (x : ℝ) : Real.cos (3 * x) = 4 * Real.cos x ^ 3 - 3 * Real.cos x := by
  cas?
Out[11]=

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 x(1−x) is 1/4 at x=1/2, so no setting will prove it. The message reports the certified upper bound 1/4, which is exactly the maximum:

In[12]:=
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : x * (1 - x) ≤ 1 / 5 := by
  cega
Out[12]=

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:

Every theorem on this page depends only on propext, Classical.choice and Quot.sound, as #print axioms shows, and What you trust explains why.