Tactic

cas [integrate primitive := F]

Proves a definite integral from a primitive that you supply: the derivative is checked, then the fundamental theorem of calculus applies.

cas [integrate primitive := F]
proves ∫abf=v from the primitive F: it checks that F′=f on the interval, and applies the fundamental theorem of calculus.
cas [integrate primitive := F using h]
the same, with h as evidence for the conditions that F and f need on the whole interval.

Sometimes I already know the antiderivative and only want Lean to accept it. Maybe it came from a book, from SymPy, or from a page of my own algebra. This form takes that F, checks its derivative against the integrand, and turns the check into the value of the integral by the fundamental theorem of calculus. If F is wrong, the check fails and nothing is proved.

Details and Options

  • The goal is an equality with one bounded interval integral on one side, (∫ x in a..b, f x) = v or v = ∫ x in a..b, f x.
  • F is a term of type ℝ → ℝ, such as fun t ↦ Real.log t ^ 2 / 2, or a local definition made with let.
  • The derivative of F minus the integrand must reduce to zero. That check is decided by the kernel, and then Mathlib’s intervalIntegral.integral_eq_sub_of_hasDerivAt gives ∫abf=F(b)−F(a).
  • With using h, the conditions hold on the whole interval, stated as ∀ t ∈ Set.uIcc a b, …, so the endpoints may come in either order.
  • Improper integrals, inequalities and goals with several integrals are refused with a message. Bare cas proves many of them.
  • There are no other options.

Examples (12)#

Basic Examples (2)

A primitive for 2x:

In[1]:=
example : (∫ x in (0 : ℝ)..1, 2 * x) = 1 := by
  cas [integrate primitive := fun x ↦ x ^ 2]
Out[1]=

Lean accepts the proof.


A primitive that needs t≠0 on the interval, given by using:

In[2]:=
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
  cas [integrate primitive := fun t ↦ Real.log t ^ 2 / 2 using h]
Out[2]=

Lean accepts the proof.

Scope (2)

Parameters in the integrand and in the primitive:

In[3]:=
example (a b c d : ℝ) (h : ∀ t ∈ Set.uIcc a b, t ≠ 0) :
    (∫ t in a..b, c * Real.log t / t + d) =
      (d * b + c * Real.log b ^ 2 / 2) - (d * a + c * Real.log a ^ 2 / 2) := by
  cas [integrate primitive := (fun t : ℝ ↦ d * t + c * Real.log t ^ 2 / 2) using h]
Out[3]=

Lean accepts the proof.


A primitive named with let:

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

Lean accepts the proof.

Applications (1)

The integral of xex from a primitive found by integrating by parts on paper. Only the three standard axioms are used:

In[5]:=
theorem integral_x_exp : (∫ x in (0 : ℝ)..1, x * Real.exp x) = 1 := by
  cas [integrate primitive := fun x ↦ (x - 1) * Real.exp x]

#print axioms integral_x_exp
Out[5]=

integral_x_exp depends on the axioms propext, Classical.choice, Quot.sound.

Properties and Relations (2)

integrate finds the primitive itself when it can, and prints it with its theorem:

In[6]:=
#cas [integrate x] (x * exp x)
Out[6]=

antiderivative

ex(x−1)
exp(x)*(x - 1)

Checked by Lean’s kernel: Integral of the input

∀ (a b : ℝ), ∫ (x : ℝ) in a..b, x * Real.exp x = Real.exp b * (b - 1) - Real.exp a * (a - 1)

from CAS.IntegrationCheck.AntiderivativeCert.intervalIntegral_eq_sub_of_line

The certificate: 9 checked steps
  1. Integral of the input

    ∀ (a b : ℝ), ∫ (x : ℝ) in a..b, x * Real.exp x = Real.exp b * (b - 1) - Real.exp a * (a - 1)

    Checked by Lean’s kernel · CAS.IntegrationCheck.AntiderivativeCert.intervalIntegral_eq_sub_of_line

  2. Primitive derivative in x: indexed rules compute Expr.diff

    (CAS.Verified.Expr.DiffDerivation.build 0
          (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))))).rawDerivative =
      CAS.Verified.Expr.diff 0 (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))))

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

  3. Primitive derivative in x: normalization preserves evaluation

    ∀ (ρ : Fin 1 → ℝ),
      CAS.Verified.Expr.eval ρ
          (CAS.Verified.Expr.DiffDerivation.build 0
              (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))))).simplifiedDerivative =
        CAS.Verified.Expr.eval ρ
          (CAS.Verified.Expr.DiffDerivation.build 0
              (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))))).rawDerivative

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

  4. Primitive derivative in x: side-conditioned display normalization

    ∀ (ρ : Fin 1 → ℝ),
      CAS.Verified.Expr.Alg.SideHolds ρ
          (CAS.Command.displayDerivativeRoute
              (CAS.Verified.Expr.DiffDerivation.build 0
                (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1)))))).nonzero →
        CAS.Verified.Expr.eval ρ
            (CAS.Command.displayDerivativeRoute
                (CAS.Verified.Expr.DiffDerivation.build 0
                  (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1)))))).rhs =
          CAS.Verified.Expr.eval ρ
            (CAS.Verified.Expr.DiffDerivation.build 0
                (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))))).simplifiedDerivative

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

  5. Primitive derivative in x: analytic semantics of the displayed result

    ∀ (x : ℝ^1),
      CAS.Verified.Expr.ValidAt x.ofLp (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1)))) →
        CAS.Verified.Expr.Alg.SideHolds x.ofLp
            (CAS.Command.displayDerivativeRoute
                (CAS.Verified.Expr.DiffDerivation.build 0
                  (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1)))))).nonzero →
          (fderiv ℝ (fun y ↦ CAS.Verified.Expr.eval y.ofLp (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1)))))
                x)
              (EuclideanSpace.single 0 1) =
            CAS.Verified.Expr.eval x.ofLp
              (CAS.Command.displayDerivativeRoute
                  (CAS.Verified.Expr.DiffDerivation.build 0
                    (𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1)))))).rhs

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

  6. Residual route reaches zero

    (CAS.IntegrationCheck.route 0 (𝕏[0].mul 𝕏[0].exp)
          { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.candidate).rhs =
      0

    Checked by Lean’s kernel · CAS.IntegrationCheck.AntiderivativeCert.routeZero

  7. Symbolic antiderivative identity under the displayed requirements

    ∀ (x : ℝ^1),
      { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.requirements.Holds
          x.ofLp →
        CAS.Verified.Expr.eval x.ofLp
            (CAS.Verified.Expr.diff 0
              { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.candidate) =
          CAS.Verified.Expr.eval x.ofLp (𝕏[0].mul 𝕏[0].exp)

    Checked by Lean’s kernel · CAS.IntegrationCheck.AntiderivativeCert.eval_diff_eq

  8. Coordinate-line derivative theorem

    ∀ (x : ℝ^1) (t : ℝ),
      { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.requirements.Holds
          (x.ofLp + t • (EuclideanSpace.single 0 1).ofLp) →
        HasDerivAt
          (fun s ↦
            CAS.Verified.Expr.eval (x + s • EuclideanSpace.single 0 1).ofLp
              { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.candidate)
          (CAS.Verified.Expr.eval (x + t • EuclideanSpace.single 0 1).ofLp (𝕏[0].mul 𝕏[0].exp)) t

    Checked by Lean’s kernel · CAS.IntegrationCheck.AntiderivativeCert.hasDerivAt_eval_add_smul_single

  9. Coordinate-line fundamental theorem of calculus

    ∀ (x : ℝ^1) (a b : ℝ),
      (∀ t ∈ Set.uIcc a b,
          { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.requirements.Holds
            (x.ofLp + t • (EuclideanSpace.single 0 1).ofLp)) →
        ∫ (t : ℝ) in a..b, CAS.Verified.Expr.eval (x + t • EuclideanSpace.single 0 1).ofLp (𝕏[0].mul 𝕏[0].exp) =
          CAS.Verified.Expr.eval (x + b • EuclideanSpace.single 0 1).ofLp
              { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.candidate -
            CAS.Verified.Expr.eval (x + a • EuclideanSpace.single 0 1).ofLp
              { candidate := 𝕏[0].exp.mul (𝕏[0].add (CAS.Verified.Expr.int (-1))), routeZero := ⋯ }.candidate

    Checked by Lean’s kernel · CAS.IntegrationCheck.AntiderivativeCert.intervalIntegral_eq_sub_eval_add_smul_single

The computation
  • Variables: x
  • Checked antiderivative computation

    • 1. Differentiate the proposed primitive

      Raw derivative: exp(x)*1*(x - 1) + exp(x)*(1 + 0)
    • 2. Form the derivative residual

      Raw residual: exp(x)*1*(x - 1) + exp(x)*(1 + 0) - x*exp(x)
      Definition: CAS.IntegrationCheck.residual
    • 3. Check the residual normal form

      Checked residual normal form: 0
      Certificate condition: checked residual normal form = 0
    • 4. Record analytic domain requirements

      Certificate nonzero requirements (compacted): (none)
      Certificate positivity requirements (compacted): (none)

With the optional oracles, cas [oracle := sympy] asks SymPy for a primitive, checks it, and suggests this form. sage, maxima and giac work the same way. The suggestion needs no oracle:

In[7]:=
theorem integral_by_oracle : ∫ x in (0 : ℝ)..1, x * Real.exp x = 1 := by
  cas [oracle := sympy]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[7]=
cas [integrate primitive := fun x => (x - 1) * Real.exp x]

Possible Issues (4)

A wrong primitive is refused. The derivative of x3 is not 2x:

In[8]:=
example : (∫ x in (0 : ℝ)..1, 2 * x) = 1 := by
  cas [integrate primitive := fun x ↦ x ^ 3]
Out[8]=

Error

cas found a candidate primitive but could not certify its residual and domain requirements. This does not rule out another primitive.
Certification detail:
  cas could not certify a checked computation: the kernel did not reduce the decision procedure of
    (CAS.IntegrationCheck.route 0 ((CAS.Verified.Expr.nat 2).mul 𝕏[0]) (𝕏[0].pow 3)).rhs = CAS.Verified.Expr.nat 0 ∧
      CAS.IntegrationCheck.AntiderivativeCert.candidateRequirements 0 ((CAS.Verified.Expr.nat 2).mul 𝕏[0])
          (𝕏[0].pow 3) =
        { nonzero := [], positive := [] }
  to `true`. The statement may be false, or its decision procedure may not reduce in the kernel.

A primitive that is right only where it is defined. Without using h, nothing says that t≠0 on the interval. In this form, the evidence that the message asks for goes inside the bracket, as in the second basic example:

In[9]:=
example (a b : ℝ) :
    (∫ t in a..b, Real.log t / t) = Real.log b ^ 2 / 2 - Real.log a ^ 2 / 2 := by
  cas [integrate primitive := fun t ↦ Real.log t ^ 2 / 2]
Out[9]=

Error

cas needs pointwise domain evidence on the whole integration domain. Supply `h : ∀ t ∈ Set.uIcc a
  b, ...` and write `cas using h`, or `cas [integrate primitive := F using h]` when the primitive is given; denominators and logarithm arguments must be nonzero, and square-root arguments must be positive.
Detail:
  symbolic_assumptions could not prove the generated analytic condition from the local context. Unproved facts:
    a b t✝ : ℝ
    hcas✝ : t✝ ∈ Set.uIcc a b
    ⊢ t✝ ≠ 0

Only equalities. For a bound on an integral, cega is the tactic:

In[10]:=
example : (∫ x in (0 : ℝ)..1, x) ≤ 1 := by
  cas [integrate primitive := fun x ↦ x ^ 2 / 2]
Out[10]=

Error

cas [integrate primitive := ...] expects a bounded interval-integral equality

Only bounded intervals:

In[11]:=
example : (∫ x in Set.Ioi (0 : ℝ), Real.exp (-x)) = 1 := by
  cas [integrate primitive := fun x ↦ -Real.exp (-x)]
Out[11]=

Error

cas [integrate primitive := ...] requires a bounded interval integral

Neat Examples (1)

The arctangent as a primitive, on any interval:

In[12]:=
example (a b : ℝ) : (∫ t in a..b, 1 / (1 + t ^ 2)) = Real.arctan b - Real.arctan a := by
  cas [integrate primitive := fun t ↦ Real.arctan t]
Out[12]=

Lean accepts the proof.

See Also

cas · cas using · integrate · cas? · nintegrate · oracle