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]cas [integrate primitive := F using h]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 , checks its derivative against the integrand, and turns the check into the value of the integral by the fundamental theorem of calculus. If is wrong, the check fails and nothing is proved.
Examples (12)#
Basic Examples (2)
A primitive for :
example : (∫ x in (0 : ℝ)..1, 2 * x) = 1 := by cas [integrate primitive := fun x ↦ x ^ 2]
Lean accepts the proof.
A primitive that needs on the interval, given by using:
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]
Lean accepts the proof.
Scope (2)
Parameters in the integrand and in the primitive:
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]
Lean accepts the proof.
A primitive named with let:
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.
Applications (1)
The integral of from a primitive found by integrating by parts on paper. Only the three standard axioms are used:
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
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:
#cas [integrate x] (x * exp x)
antiderivative
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
Integral of the input
∀ (a b : ℝ), ∫ (x : ℝ) in a..b, x * Real.exp x = Real.exp b * (b - 1) - Real.exp a * (a - 1)
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))))
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
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
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
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
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)
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
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
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:
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).
cas [integrate primitive := fun x => (x - 1) * Real.exp x]
Possible Issues (4)
A wrong primitive is refused. The derivative of is not :
example : (∫ x in (0 : ℝ)..1, 2 * x) = 1 := by cas [integrate primitive := fun x ↦ x ^ 3]
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 on the interval. In this form, the evidence that the message asks for goes inside the bracket, as in the second basic example:
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]
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✝ ≠ 0Only equalities. For a bound on an integral, cega is the tactic:
example : (∫ x in (0 : ℝ)..1, x) ≤ 1 := by cas [integrate primitive := fun x ↦ x ^ 2 / 2]
Error
cas [integrate primitive := ...] expects a bounded interval-integral equality
Only bounded intervals:
example : (∫ x in Set.Ioi (0 : ℝ), Real.exp (-x)) = 1 := by cas [integrate primitive := fun x ↦ -Real.exp (-x)]
Error
cas [integrate primitive := ...] requires a bounded interval integral
Neat Examples (1)
The arctangent as a primitive, on any interval:
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]
Lean accepts the proof.
