Guide

Calculus

Derivatives, gradients, antiderivatives, definite integrals and extrema, each with a theorem about Mathlib’s definitions.

When a proof needs a derivative or an integral, I don’t want to work it out on paper and then convince Lean of it line by line. Here you can compute derivatives, gradients, antiderivatives, definite integrals and the extrema of polynomials, and use them on their own or as steps in a proof. A derivative comes with a theorem about Mathlib’s HasDerivAt, an antiderivative with a theorem about the interval integral, and both carry the conditions they need, such as x≠0 under a logarithm.

Derivatives

  • deriveThe derivative of an expression, with the conditions it needs and a theorem about Mathlib’s HasDerivAt.
  • gradThe gradient of an expression in several variables, with a theorem about Mathlib’s HasGradientAt.

Integrals

  • integrateAn antiderivative or a definite integral, checked by differentiation, with a theorem about Mathlib’s interval integral.
  • nintegrateA definite integral to a fixed number of decimal places, with a theorem that bounds the error.
  • 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.

Extrema

  • maximizeThe maximum of a polynomial on the real line or on an interval, exact or enclosed, with a theorem.
  • minimizeThe minimum of a polynomial on the real line or on an interval, exact or enclosed, with a theorem.

Integration shows the idea best. Finding an antiderivative is a search, and a search can go wrong. Checking one is easy: differentiate it and compare with the integrand. So SymLean prints a candidate only after its derivative matches, and the fundamental theorem of calculus turns that into the theorem. It holds on every interval where the conditions hold, which here means every interval that avoids x=0:

In[1]:=
#cas [integrate x] (1 / (x ^ 3 + x))
Out[1]=

antiderivative

log(x)−12log(x2+1)
log(x) - 1/2*log(x^2 + 1)
Conditions
x≠0

Checked by Lean’s kernel: Integral of the input

∀ (a b : ℝ),
  (∀ x ∈ Set.uIcc a b, x ≠ 0) →
    ∫ (x : ℝ) in a..b, 1 / (x ^ 3 + x) =
      Real.log b - 1 / 2 * Real.log (b ^ 2 + 1) - (Real.log a - 1 / 2 * Real.log (a ^ 2 + 1))

from CAS.IntegrationCheck.AntiderivativeCert.intervalIntegral_eq_sub_of_line

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

    ∀ (a b : ℝ),
      (∀ x ∈ Set.uIcc a b, x ≠ 0) →
        ∫ (x : ℝ) in a..b, 1 / (x ^ 3 + x) =
          Real.log b - 1 / 2 * Real.log (b ^ 2 + 1) - (Real.log a - 1 / 2 * Real.log (a ^ 2 + 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].log.add
            ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log))).rawDerivative =
      CAS.Verified.Expr.diff 0
        (𝕏[0].log.add ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log))

    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].log.add
                ((CAS.Verified.Expr.rat (-1) 2).mul
                  ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log))).simplifiedDerivative =
        CAS.Verified.Expr.eval ρ
          (CAS.Verified.Expr.DiffDerivation.build 0
              (𝕏[0].log.add
                ((CAS.Verified.Expr.rat (-1) 2).mul
                  ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log))).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].log.add
                  ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log)))).nonzero →
        CAS.Verified.Expr.eval ρ
            (CAS.Command.displayDerivativeRoute
                (CAS.Verified.Expr.DiffDerivation.build 0
                  (𝕏[0].log.add
                    ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log)))).rhs =
          CAS.Verified.Expr.eval ρ
            (CAS.Verified.Expr.DiffDerivation.build 0
                (𝕏[0].log.add
                  ((CAS.Verified.Expr.rat (-1) 2).mul
                    ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log))).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].log.add ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log)) →
        CAS.Verified.Expr.Alg.SideHolds x.ofLp
            (CAS.Command.displayDerivativeRoute
                (CAS.Verified.Expr.DiffDerivation.build 0
                  (𝕏[0].log.add
                    ((CAS.Verified.Expr.rat (-1) 2).mul
                      ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log)))).nonzero →
          (fderiv ℝ
                (fun y ↦
                  CAS.Verified.Expr.eval y.ofLp
                    (𝕏[0].log.add
                      ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log)))
                x)
              (EuclideanSpace.single 0 1) =
            CAS.Verified.Expr.eval x.ofLp
              (CAS.Command.displayDerivativeRoute
                  (CAS.Verified.Expr.DiffDerivation.build 0
                    (𝕏[0].log.add
                      ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log)))).rhs

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

  6. Residual route reaches zero

    (CAS.IntegrationCheck.route 0 ((CAS.Verified.Expr.nat 1).div ((𝕏[0].pow 3).add 𝕏[0]))
          {
              candidate :=
                𝕏[0].log.add ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
              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].log.add
                    ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
                routeZero := ⋯ }.requirements.Holds
          x.ofLp →
        CAS.Verified.Expr.eval x.ofLp
            (CAS.Verified.Expr.diff 0
              {
                  candidate :=
                    𝕏[0].log.add
                      ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
                  routeZero := ⋯ }.candidate) =
          CAS.Verified.Expr.eval x.ofLp ((CAS.Verified.Expr.nat 1).div ((𝕏[0].pow 3).add 𝕏[0]))

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

  8. Coordinate-line derivative theorem

    ∀ (x : ℝ^1) (t : ℝ),
      {
                candidate :=
                  𝕏[0].log.add
                    ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
                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].log.add
                      ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
                  routeZero := ⋯ }.candidate)
          (CAS.Verified.Expr.eval (x + t • EuclideanSpace.single 0 1).ofLp
            ((CAS.Verified.Expr.nat 1).div ((𝕏[0].pow 3).add 𝕏[0])))
          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].log.add
                      ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
                  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
              ((CAS.Verified.Expr.nat 1).div ((𝕏[0].pow 3).add 𝕏[0])) =
          CAS.Verified.Expr.eval (x + b • EuclideanSpace.single 0 1).ofLp
              {
                  candidate :=
                    𝕏[0].log.add
                      ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
                  routeZero := ⋯ }.candidate -
            CAS.Verified.Expr.eval (x + a • EuclideanSpace.single 0 1).ofLp
              {
                  candidate :=
                    𝕏[0].log.add
                      ((CAS.Verified.Expr.rat (-1) 2).mul ((𝕏[0].pow 2).add (CAS.Verified.Expr.nat 1)).log),
                  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: 1/x + 0*log(x^2 + 1) - 1/2*(2*x^1*1 + 0)/(x^2 + 1)
    • 2. Form the derivative residual

      Raw residual: 1/x + 0*log(x^2 + 1) - 1/2*(2*x^1*1 + 0)/(x^2 + 1) - 1/(x^3 + 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): x^2 + 1, x
      Certificate positivity requirements (compacted): (none)

The search is bounded. It covers polynomials and a table of elementary antiderivatives, substitution, rational functions, integration by parts, polynomials times exponentials, sines and cosines, and rational functions of sinx and cosx. When no candidate passes the derivative check, the command stops with an error and proves nothing. ex2 is the classic case, since it has no elementary antiderivative, and nintegrate gives a definite integral of it as a decimal with a proved error bound instead. When I already have a primitive, from a book or from SymPy, cas [integrate primitive := F] checks mine. The extrema modes take a polynomial in one variable, and for other functions on an interval the tactic cega proves the bounds.

The same statements, written in Mathlib’s notation, are goals that the tactic cas proves.