Tutorial
Getting started with SymLean
From an empty Lean file to derivatives, integrals, roots and a proof, one command at a time.
Let’s start with a few calculations and then use one of them in a proof. Everything here runs in an ordinary Lean file.
Setting up#
SymLean builds with Lean 4.36.0 and Mathlib v4.36.0. In a project on the same toolchain, add it to lakefile.lean:
require SymLean from git "https://github.com/lean-dojo/SymLean.git" @ "main"
Then fetch it, get Mathlib’s cache, and build:
lake update SymLean lake exe cache get lake build
Mathlib comes from its cache, but SymLean and the modules of FloatLib that it uses are compiled on the first build, and the kernel checks every proof in them, every numerical certificate included. Your project builds only the modules it imports. For comparison, the modules of SymLean’s own repository, examples included, take ten to thirteen minutes to build on 8 cores. I think it’s worth the wait, since every proof in the modules you import is then checked on your machine. After that, a file that uses SymLean starts with
import CAS
which brings in #cas and the tactics cas, cas?, cas_nf, cega and cega?.
A first calculation#
To differentiate, write the mode in brackets and the expression after it. Variables need no declaration: x, a and b are real numbers, and x is the one named in the mode. Multiplication is Lean’s *, so you write 3 * x and not 3x.
#cas [derive x] (a * x ^ 3 + b * x)
a*3*x^2 + bChecked by Lean’s kernel: Derivative of the input
∀ (x a b : ℝ), HasDerivAt (fun x ↦ a * x ^ 3 + b * x) (a * (3 * x ^ 2) + b) x
from CAS.Command.hasDerivAt_of_displayRoute
The certificate: 5 checked steps
Derivative of the input
∀ (x a b : ℝ), HasDerivAt (fun x ↦ a * x ^ 3 + b * x) (a * (3 * x ^ 2) + b) x
Partial derivative in x: indexed rules compute Expr.diff
(CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0]))).rawDerivative = CAS.Verified.Expr.diff 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0]))
Partial derivative in x: normalization preserves evaluation
∀ (ρ : Fin 3 → ℝ), CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0]))).simplifiedDerivative = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0]))).rawDerivative
Partial derivative in x: side-conditioned display normalization
∀ (ρ : Fin 3 → ℝ), CAS.Verified.Expr.Alg.SideHolds ρ (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0])))).nonzero → CAS.Verified.Expr.eval ρ (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0])))).rhs = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0]))).simplifiedDerivative
Partial derivative in x: analytic semantics of the displayed result
∀ (x : ℝ^3), CAS.Verified.Expr.ValidAt x.ofLp ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0])) → CAS.Verified.Expr.Alg.SideHolds x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0])))).nonzero → (fderiv ℝ (fun y ↦ CAS.Verified.Expr.eval y.ofLp ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0]))) x) (EuclideanSpace.single 0 1) = CAS.Verified.Expr.eval x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[1].mul (𝕏[0].pow 3)).add (𝕏[2].mul 𝕏[0])))).rhs
The computation
Variables: x, a, b
Analytic domain conditions
Raw checked nonzero requirements: (none) Raw checked positivity requirements: (none)
Partial derivative in x
1. Apply symbolic differentiation rules
raw result: 0*x^3 + a*3*x^2*1 + 0*x + b*1
2. Normalize the symbolic derivative unconditionally
normalized result: a*3*x^2 + b
3. Apply checked side-conditioned cleanup
displayed result: a*3*x^2 + b additional nonzero requirements: (none)
The answer comes with the theorem behind it, which the kernel checked. Look at that theorem for a moment. It is a HasDerivAt statement for every x, a and b, so it’s a fact you can use later, not just a formula on the screen. In the editor the evidence is folded under More details. Here it sits under the report, as the certificate and the computation.
When a rule needs a condition, the answer says so. The rule for a square root needs a positive argument, so this derivative holds where (the theorem writes the same condition as 0 < 2 * x + 2):
#cas [derive x] (sqrt (2 * x + 2))
1/sqrt(2*x + 2)- Conditions
Checked by Lean’s kernel: Derivative of the input
∀ (x : ℝ), 0 < 2 * x + 2 → HasDerivAt (fun x ↦ √(2 * x + 2)) (1 / √(2 * x + 2)) x
from CAS.Command.hasDerivAt_of_displayRoute
The certificate: 5 checked steps
Derivative of the input
∀ (x : ℝ), 0 < 2 * x + 2 → HasDerivAt (fun x ↦ √(2 * x + 2)) (1 / √(2 * x + 2)) x
Partial derivative in x: indexed rules compute Expr.diff
(CAS.Verified.Expr.DiffDerivation.build 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt).rawDerivative = CAS.Verified.Expr.diff 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt
Partial derivative in x: normalization preserves evaluation
∀ (ρ : Fin 1 → ℝ), CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt).simplifiedDerivative = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt).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 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt)).nonzero → CAS.Verified.Expr.eval ρ (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt)).rhs = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt).simplifiedDerivative
Partial derivative in x: analytic semantics of the displayed result
∀ (x : ℝ^1), CAS.Verified.Expr.ValidAt x.ofLp (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt → CAS.Verified.Expr.Alg.SideHolds x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt)).nonzero → (fderiv ℝ (fun y ↦ CAS.Verified.Expr.eval y.ofLp (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt) x) (EuclideanSpace.single 0 1) = CAS.Verified.Expr.eval x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 (((CAS.Verified.Expr.nat 2).mul 𝕏[0]).add (CAS.Verified.Expr.nat 2)).sqrt)).rhs
The computation
Variables: x
Analytic domain conditions
Raw checked nonzero requirements: (none) Raw checked positivity requirements: 2*x + 2
Partial derivative in x
1. Apply symbolic differentiation rules
raw result: (0*x + 2*1 + 0)/(2*sqrt(2*x + 2))
2. Normalize the symbolic derivative unconditionally
normalized result: 1/sqrt(2*x + 2)
3. Apply checked side-conditioned cleanup
displayed result: 1/sqrt(2*x + 2) additional nonzero requirements: (none)
The condition is enough for the rule to apply, and that’s all it claims. It isn’t meant to be the whole set where the derivative exists. The Conditions line of a report prints the conditions of the theorem in a display form, which can rescale a sum, as here, or reorder it, and the theorem states each one as it was checked.
Equations, integrals and digits#
The same command solves equations, and here the theorem is an if and only if: for every real , the equation holds exactly at the solutions:
#cas [solve x] (x ^ 2 - 3 * x + 2 = 0)
equivalent real solution predicate
x = 2 ∨ x = 1- Input (real relation)
(x ^ 2 - 3 * x + 2 = 0)
Checked by Lean’s kernel: Complete solution predicate for every real value
∀ (x : ℝ), x ^ 2 - 3 * x + 2 = 0 ↔ x = 2 ∨ x = 1
The certificate: 1 checked step
Complete solution predicate for every real value
∀ (x : ℝ), x ^ 2 - 3 * x + 2 = 0 ↔ x = 2 ∨ x = 1
The computation
Guarantee: the input and output are equivalent for every real value
An antiderivative is checked by differentiating it back, and a definite integral comes out exact when it can be:
#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)
#cas [integrate x in [0, 1]] (1 / (1 + x ^ 2))
definite integral
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
Definite integral, proved by
cas∫ (x : ℝ) in 0..1, 1 / (1 + x ^ 2) = 1 / 4 * Real.pi
The computation
Variables: x
Digits are proved too. Each of these twenty is a digit of , because the theorem traps between the truncated decimal and the next one up:
#cas [N 20] (exp 1)
first 20 significant digits, truncated
2.7182818284590452353- Domain
closed real expression
Checked by Lean’s kernel: Certified decimal digits
27182818284590452353 / 10 ^ 19 ≤ Real.exp 1 ∧ Real.exp 1 < (27182818284590452353 + 1) / 10 ^ 19
from CAS.Continuum.DigitsEval.eval_sound
The certificate: 1 checked step
Certified decimal digits
27182818284590452353 / 10 ^ 19 ≤ Real.exp 1 ∧ Real.exp 1 < (27182818284590452353 + 1) / 10 ^ 19
The computation
Certified numerical evaluation
Working precision: 106 bits, 44 series terms, 84 logarithm terms Guarantee: every printed digit is a digit of the real value, truncated toward zero
No formula in radicals gives the roots of . Its real root still has digits, though, and the theorem says it is the only real root:
#cas [roots x] (x ^ 5 - x - 1 = 0)
the only real solution, by 20 decimals, truncated
1.16730397826141868425- Input (polynomial equation)
(x ^ 5 - x - 1 = 0)
Checked by Lean’s kernel: Complete list of real solutions
(∃! x,
x ∈ Set.Ioo (116730397826141868425 / 10 ^ 20) (116730397826141868426 / 10 ^ 20) ∧ x ^ 5 - x - 1 = 0) ∧
∀ (x : ℝ), x ^ 5 - x - 1 = 0 → x ∈ Set.Ioo (116730397826141868425 / 10 ^ 20) (116730397826141868426 / 10 ^ 20)from CAS.Polynomial.RealRoot.of_exprRootCheck
The certificate: 1 checked step
Complete list of real solutions
(∃! x, x ∈ Set.Ioo (116730397826141868425 / 10 ^ 20) (116730397826141868426 / 10 ^ 20) ∧ x ^ 5 - x - 1 = 0) ∧ ∀ (x : ℝ), x ^ 5 - x - 1 = 0 → x ∈ Set.Ioo (116730397826141868425 / 10 ^ 20) (116730397826141868426 / 10 ^ 20)
The computation
Certified isolation
Square-free polynomial of degree 5, Cauchy bound 3, 7 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
From a calculation to a theorem#
This is the part I care about most. In a proof you state the result in Mathlib’s language and let a tactic prove it. The first derivative above becomes an ordinary HasDerivAt statement:
example (a b x : ℝ) : HasDerivAt (fun t : ℝ ↦ a * t ^ 3 + b * t) (3 * a * x ^ 2 + b) x := by cas
Lean accepts the proof.
When you don’t know which call to use, cas? proves the goal and suggests the one to keep in the source:
example : (∫ t in (0 : ℝ)..1, t * Real.exp t) = 1 := by cas?
Try this
cas
Lean checked the suggested replacement against the original goal. Selected route: CAS route: checked integration, bounded antiderivative search.Numerical goals go to cega, which reads the box, the rational bounds on each variable, from the hypotheses, here :
example (x : ℝ) (h0 : 0 ≤ x) (h1 : x ≤ 1) : Real.exp x ≤ 2 + x := by cega
Lean accepts the proof.
When a call fails#
Sooner or later a call fails, and I think it’s worth seeing what that looks like early. A goal that SymLean can’t prove stays as it was, and the message says what stopped the call. This statement is false, because Mathlib’s Real.log is defined everywhere and :
example (x : ℝ) : Real.exp (Real.log x) = x := by cas
Error
cas could not close this ordinary arithmetic goal. This goal contains branch-sensitive operations and may need appropriate logarithm-branch hypotheses (`x ≠ 0` gives `exp (log x) = |x|`; use `0 < x` to remove the absolute value). 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 (or 0 < x for the positive branch).
The rest of the message
Polynomial equation route:
`grind` failed
case grind
x : ℝ
h : ¬Real.exp (Real.log x) = x
⊢ False
[grind] Goal diagnostics
[facts] Asserted facts
[prop] ¬Real.exp (Real.log x) = x
[eqc] False propositions
[prop] Real.exp (Real.log x) = x
[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:
0 < x
Elementary rewrite route:
no elementary rewrite applies
Grind fallback:
`grind` failed (`set_option trace.cas.fallback true` shows its diagnostics)The message points at the fix. With the hypothesis , which makes the statement true, the same call works:
example (x : ℝ) (hx : 0 < x) : Real.exp (Real.log x) = x := by cas
Lean accepts the proof.
A refusal says only that no route of SymLean found a proof. It never adds an assumption or changes the statement, and I find that reassuring, because what you see in the file is what was proved. The reference page of each mode shows, under Possible Issues, the ways it can fail and the messages Lean prints. When you’re ready for longer proofs, Proving with cas is the next tutorial.
