
A verified computer algebra system in Lean.
Caltech
#cas [integrate x] (atan x)
antiderivative
x*atan(x) - 1/2*log(1 + x^2)Checked by Lean’s kernel: Integral of the input
∀ (a b : ℝ), ∫ (x : ℝ) in a..b, Real.arctan x = b * Real.arctan b - 1 / 2 * Real.log (1 + b ^ 2) - (a * Real.arctan a - 1 / 2 * Real.log (1 + a ^ 2))
from CAS.IntegrationCheck.AntiderivativeCert.intervalIntegral_eq_sub_of_line
The certificate: 9 checked steps
Integral of the input
∀ (a b : ℝ), ∫ (x : ℝ) in a..b, Real.arctan x = b * Real.arctan b - 1 / 2 * Real.log (1 + b ^ 2) - (a * Real.arctan a - 1 / 2 * Real.log (1 + a ^ 2))
Primitive derivative in x: indexed rules compute Expr.diff
(CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log))).rawDerivative = CAS.Verified.Expr.diff 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log))
Primitive derivative in x: normalization preserves evaluation
∀ (ρ : Fin 1 → ℝ), CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log))).simplifiedDerivative = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log))).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].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log)))).nonzero → CAS.Verified.Expr.eval ρ (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log)))).rhs = CAS.Verified.Expr.eval ρ (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log))).simplifiedDerivative
Primitive derivative in x: analytic semantics of the displayed result
∀ (x : ℝ^1), CAS.Verified.Expr.ValidAt x.ofLp ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log)) → CAS.Verified.Expr.Alg.SideHolds x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log)))).nonzero → (fderiv ℝ (fun y ↦ CAS.Verified.Expr.eval y.ofLp ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log))) x) (EuclideanSpace.single 0 1) = CAS.Verified.Expr.eval x.ofLp (CAS.Command.displayDerivativeRoute (CAS.Verified.Expr.DiffDerivation.build 0 ((𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log)))).rhs
Residual route reaches zero
(CAS.IntegrationCheck.route 0 𝕏[0].atan { candidate := (𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log), routeZero := ⋯ }.candidate).rhs = 0
Symbolic antiderivative identity under the displayed requirements
∀ (x : ℝ^1), { candidate := (𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log), routeZero := ⋯ }.requirements.Holds x.ofLp → CAS.Verified.Expr.eval x.ofLp (CAS.Verified.Expr.diff 0 { candidate := (𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log), routeZero := ⋯ }.candidate) = CAS.Verified.Expr.eval x.ofLp 𝕏[0].atan
Coordinate-line derivative theorem
∀ (x : ℝ^1) (t : ℝ), { candidate := (𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).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].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log), routeZero := ⋯ }.candidate) (CAS.Verified.Expr.eval (x + t • EuclideanSpace.single 0 1).ofLp 𝕏[0].atan) t
Coordinate-line fundamental theorem of calculus
∀ (x : ℝ^1) (a b : ℝ), (∀ t ∈ Set.uIcc a b, { candidate := (𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).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 𝕏[0].atan = CAS.Verified.Expr.eval (x + b • EuclideanSpace.single 0 1).ofLp { candidate := (𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log), routeZero := ⋯ }.candidate - CAS.Verified.Expr.eval (x + a • EuclideanSpace.single 0 1).ofLp { candidate := (𝕏[0].mul 𝕏[0].atan).sub ((CAS.Verified.Expr.rat 1 2).mul ((CAS.Verified.Expr.nat 1).add (𝕏[0].pow 2)).log), routeZero := ⋯ }.candidate
The computation
Variables: x
Checked antiderivative computation
1. Differentiate the proposed primitive
Raw derivative: 1*atan(x) + x*1/(1 + x^2) - 0*log(1 + x^2) - 1/2*(0 + 2*x^1*1)/(1 + x^2)
2. Form the derivative residual
Raw residual: 1*atan(x) + x*1/(1 + x^2) - 0*log(1 + x^2) - 1/2*(0 + 2*x^1*1)/(1 + x^2) - atan(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): 1 + x^2 Certificate positivity requirements (compacted): (none)
Introduction#
Computer algebra systems such as Mathematica, Maple and SymPy have long been part of how we do mathematics. Whether we are deriving an equation, working out an integral or simplifying a particularly unpleasant expression, it is often much easier to ask a computer to do the calculation. But when formalizing mathematics in Lean, we still need to prove that the resulting expression is correct. This can take considerably more effort than finding the answer itself. SymLean grew out of our interest in making these two processes work together. We wanted something that feels like a conventional computer algebra system but produces Lean theorems that we can use directly in our proofs.
The library covers symbolic algebra, differentiation, integration, series expansions, limits, equation solving, special functions and certified numerical computation. Calculations can be performed interactively using #cas or within an existing Lean proof using the cas and cega tactics.
An important distinction is that the algorithms used to find an answer do not themselves need to be trusted. They can be heuristic, and we can even delegate computations to external computer algebra systems. Consider, for instance, the integral of over . Mathematica has previously returned for this integral, even though it diverges. Interestingly, our own search also finds ! The difference is that SymLean cannot certify this answer: the argument requires a nonzero denominator throughout the interval, which is not the case here.
What can we compute?#
Much of the interface should feel familiar to anyone who has worked with Mathematica or SymPy. We can factor a polynomial, integrate a function, compute a Taylor expansion or find the roots of an equation with a single command. Below are a few examples from different parts of the library. Variables in these commands are interpreted as real numbers, so we do not need to declare them beforehand.
Algebra
#cas [factor] (2*x^3 - 3*x^2 + 1)
(x - 1)^2*(2*x + 1)Checked by Lean’s kernel: Identity with the factored polynomial
∀ (x : ℝ), 2 * x ^ 3 - 3 * x ^ 2 + 1 = (x - 1) ^ 2 * (2 * x + 1)
Calculus
#cas [integrate x in [0, 1]] (x * exp x)
definite integral
1 Checked by Lean’s kernel: Definite integral, proved by cas
∫ (x : ℝ) in 0..1, x * Real.exp x = 1
Series and limits
#cas [series x 0 7] (sin x)
Taylor polynomial with exact coefficients
x - 1/6*x^3 + 1/120*x^5 - 1/5040*x^7 + o(x^7)Checked by Lean’s kernel: Taylor expansion with a little-o remainder
(fun x ↦ Real.sin x - (x - 1 / 6 * x ^ 3 + 1 / 120 * x ^ 5 - 1 / 5040 * x ^ 7)) =o[nhds 0] fun x ↦ x ^ 7
Equations
#cas [roots x] (x ^ 5 - x - 1 = 0)
the only real solution, by 20 decimals, truncated
1.16730397826141868425Checked 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)
Special functions
#cas (zeta 4)
exact value of ζ
pi^4/90Checked by Lean’s kernel: ζ(2k) by Bernoulli numbers (riemannZeta_two_mul_nat)
riemannZeta 4 = ↑Real.pi ^ 4 / 90
Certified numerics
#cas [nintegrate x in [0, 1]] (exp (-x^2))
certified value of the integral
0.746824 ± 10^-6Checked 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 last two numerical examples are particularly useful in formal proofs. Rather than simply returning a decimal approximation, SymLean establishes a precise bound on the quantity being computed. This is something we frequently need when formalizing arguments in analysis, differential equations and scientific computing.
The getting started guide covers how to use these commands in your own Lean files, and the documentation goes into more detail on the algorithms, options and supported operations.
From computation to proof#
Finding a mathematical expression and checking that it is correct are often two very different problems. Integration is perhaps the most familiar example: finding an antiderivative can be quite difficult, while checking one usually amounts to differentiation and simplification. We make use of this asymmetry throughout SymLean.
For the integral at the top of this page, the search finds the candidate
A verified differentiator computes its derivative, and a verified normalizer establishes that . We can then use the fundamental theorem of calculus to obtain the corresponding result for any finite interval.
There are also some interesting subtleties when working with symbolic expressions in a theorem prover. Division in Lean, for instance, is a total operation, with . This means that seemingly straightforward simplifications can require additional hypotheses. Take
We would ordinarily cancel the common factor and move on. In Lean, however, the equality is false at and . SymLean therefore includes the necessary condition in the resulting theorem.
#cas [simplify] ((x ^ 4 - 1) / (x ^ 2 - 1))
x^2 + 1- Conditions
Checked by Lean’s kernel: Identity with the simplified form
∀ (x : ℝ), x * x - 1 ≠ 0 → (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1
from CAS.Command.eq_of_simplifyFinDisplayRoute
The certificate: 2 checked steps
Identity with the simplified form
∀ (x : ℝ), x * x - 1 ≠ 0 → (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1
Simplification under generated rewrite assumptions
∀ (ρ : Fin 1 → ℝ), CAS.Verified.Expr.Alg.SideHolds ρ (((𝕏[0].pow 4).sub (CAS.Verified.Expr.nat 1)).div ((𝕏[0].pow 2).sub (CAS.Verified.Expr.nat 1))).simplifyFinDisplayRoute.nonzero → CAS.Verified.Expr.eval ρ (((𝕏[0].pow 4).sub (CAS.Verified.Expr.nat 1)).div ((𝕏[0].pow 2).sub (CAS.Verified.Expr.nat 1))).simplifyFinDisplayRoute.rhs = CAS.Verified.Expr.eval ρ (((𝕏[0].pow 4).sub (CAS.Verified.Expr.nat 1)).div ((𝕏[0].pow 2).sub (CAS.Verified.Expr.nat 1))).simplifyFinDisplayRoute.lhs
The computation
Variables: x
Checked symbolic computation
Raw checked nonzero requirements: x^2 - 1 Guarantee: these assumptions justify conditional rewrites and preserve real evaluation
These conditions are particularly important when symbolic calculations form part of a larger mathematical argument, where a missing hypothesis can invalidate much more than the calculation itself.
What do we need to trust?#
By default, the proofs produced by SymLean are checked by Lean's kernel and depend only on the three standard axioms used throughout Mathlib: propext, Classical.choice and Quot.sound. We can inspect this ourselves using #print axioms.
theorem integral_x_exp : ∫ x in (0 : ℝ)..1, x * Real.exp x = 1 := by cas #print axioms integral_x_exp
integral_x_exp depends on the axioms propext, Classical.choice, Quot.sound.
This separation between computation and verification gives us considerable freedom in how we implement the search algorithms. We can experiment with different symbolic methods, use numerical heuristics or call external software without adding those implementations to our trusted computing base.
For larger computations, SymLean also provides an optional native-certificate mode. This uses compiled evaluation to check finite certificates more efficiently, at the cost of additionally trusting Lean's compiler. The option is disabled by default, and the additional axiom is visible through #print axioms. We discuss these choices and their implications in more detail in the documentation.
Mathlib and other computer algebra systems#
SymLean is intended to complement existing mathematical software rather than replace it. There is already decades of excellent work on symbolic computation, and we would like to make more of it accessible from within formal mathematics.
Mathlib. All of our results are expressed using Mathlib's mathematical definitions, including its notions of derivatives, interval integrals, special functions and matrices. This allows the resulting theorems to be used directly in existing formalizations. Internally, SymLean also makes extensive use of established Mathlib tactics such as ring, field_simp, norm_num, positivity and linarith.
External computer algebra systems. Through DSLean and the optional CASOracle library, SymLean can work with SymPy, SageMath, Maxima, PARI/GP, FLINT, Giac, Singular and Gappa. These systems can do much of the computationally difficult work of finding a result, while SymLean constructs the corresponding proof. SymPy and SageMath cover most of the modes, from integrals and factorizations to Gröbner bases, equations, sums and differential equations. Maxima and Giac integrate, PARI/GP and FLINT factor polynomials and find their roots, Singular computes Gröbner bases and ideal memberships, doing the work of Mathlib's polyrith with a local tool, and Gappa proposes the interval bounds that cega checks.
#cas [factor, oracle := pari] (x ^ 6 - 1)
Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).
the identity is checked, irreducibility is not
(x - 1)*(x + 1)*(x^2 - x + 1)*(x^2 + x + 1)In this example, we certify the factorization identity returned by PARI/GP, but not the irreducibility of the individual factors. The distinction is reflected in the output.
Hex. Hex, the Lean FRO's library of verified computational algebra, proves factorization, determinants and primality in Lean itself. On Lean 4.35, SymLean called it for Berlekamp–Zassenhaus factoring, Bareiss determinants and primality certificates, and that route comes back as soon as Hex is released for Lean 4.36. We are in touch with its developers.
Why are we interested in this?#
As we formalize more mathematics, particularly in areas such as analysis, differential equations and numerical analysis, symbolic and numerical calculations become increasingly difficult to avoid. A substantial part of a proof may consist of calculations that would ordinarily be delegated to mathematical software. We have encountered this ourselves while working on the formalization of partial differential equations. Sometimes the mathematical argument is already well understood, and the remaining difficulty is simply getting a long calculation through Lean.
There is also an interesting opportunity here for AI-assisted mathematics. LLMs are becoming increasingly capable of finding mathematical arguments and proposing symbolic manipulations, but verifying the intermediate calculations remains important. A computer algebra system that produces formal proofs could be useful both to mathematicians working interactively and to AI systems exploring new proofs. We hope SymLean will make this kind of work a little easier.
Credits#
SymLean is developed at Caltech by Robert Joseph George, Changhe Yang, Peicong Song, Thomas Y. Hou and Anima Anandkumar. The library builds on Mathlib, uses FloatLib for certified numerical computation and integrates with external computer algebra systems through DSLean. SymLean is also used in PDEAtlas, our ongoing effort to formalize partial differential equations.
Many thanks to the Lean and Mathlib communities for the mathematical foundations, software and helpful discussions. SymLean is open source under the Apache 2.0 license. Contributions, feedback and suggestions for new mathematical applications are very welcome.