Guide

Why SymLean

What can go wrong in the output of a computer algebra system, mistakes on record from Mathematica to SymPy, Sage, Maxima, FLINT and PARI/GP, and how proving the answer catches them.

A computer algebra system answers with an expression, and whether that expression is right depends on conditions the output rarely states: a domain, a branch, an interval without poles, a precision. When I use one for a quick calculation, I can usually fill them in myself. In a proof those conditions are the point, so SymLean answers with a theorem that carries them, and Lean’s kernel checks it.

What has to be proved#

For each kind of operation, here is what a system prints, what a proof needs on top of it, and what SymLean proves:

OperationA system printsWhat has to be proved as wellIn SymLean
Simplificationan equal expressionthat the two agree on the stated domain: for real x, x2=|x|, and x2=x needs x≥0simplify proves that they agree under the conditions it prints
Differentiationa formula for the derivativethat the function has this derivative wherever the theorem claims it, with the conditions of the denominators and branchesderive proves HasDerivAt under its conditions
Definite integrationa valuethat the antiderivative F satisfies F′=f on the whole interval, poles and endpoints included, so that the fundamental theorem of calculus appliesintegrate proves F′=f there first
Root findinga list of rootsthat each listed value is a root, and for all roots, that there are no othersroots proves the list complete
Numerical computationdigitsthat the exact value lies within a proved bound, rounding and truncation includedN proves every printed digit

In an analytic proof, the last row decides whether a number can be part of the argument at all. A decimal value of a constant is evidence. A proved inequality between the constant and a threshold is a step of the proof, and that’s the one I need.

Mistakes on record#

The list below comes from the projects’ own trackers and from published papers. Each entry names the version and the date of its report, and none is a claim about a later version. I don’t mean it as a scorecard. These are good systems kept up by careful people, and several of the bugs have been fixed since they were reported. What it shows is that even mature tools sometimes print a wrong answer that looks like any other.

The two I find most telling are failed checks. Sage’s echelon form tested its own result, and primecertisvalid exists to validate certificates. So a check is only as good as the program that runs it. In SymLean the check is a proof, and the program that accepts it is Lean’s kernel, the same one that checks every proof in Mathlib.

The same inputs in SymLean#

Here is SymLean on four of the same inputs. On Fateman’s integral, SymLean’s own search makes the old mistake and proposes −2. I like this example because the search really is wrong and nothing bad comes of it: the proof needs x≠0 on all of [−1,1], so the value is refused, and the message names the condition that failed:

In[1]:=
#cas [integrate x in [-1, 1]] (1 / x ^ 2)
Out[1]=

Error

`cas` did not prove the value -2 of the integral: it could not prove that the conditions of the primitive -(1/x) hold on the interval (x ≠ 0). The endpoint calculation needs a primitive valid throughout the interval.

Next, Sage’s matrix. Sage’s algorithm checked its result against a bound computed from the wrong matrix. The theorem here needs no such bound: it gives the row operations E with EA=R, E invertible and R in reduced echelon form, and those three facts determine R:

In[2]:=
#cas [rowReduce] !![1000001000, -1/501000500, 3; 1, -1, 1]
Out[2]=

reduced row echelon form, pivot columns [0, 1]

(10150300149950100100100049999901−501000999497498500501001001000499999)
!![1, 0, 1503001499/501001001000499999; 0, 1, -501000999497498500/501001001000499999]
Domain
ℚ^(2×3)
Input (matrix)
!![1000001000, -1/501000500, 3; 1, -1, 1]

Checked by Lean’s kernel: Row operations E with E * A = R

!![501000500 / 501001001000499999, -(1 / 501001001000499999);
        501000500 / 501001001000499999, -(501001001000500000 / 501001001000499999)] *
      !![1000001000, -(1 / 501000500), 3; 1, -1, 1] =
    !![1, 0, 1503001499 / 501001001000499999; 0, 1, -(501000999497498500 / 501001001000499999)] ∧
  !![501000500 / 501001001000499999, -(1 / 501001001000499999);
          501000500 / 501001001000499999, -(501001001000500000 / 501001001000499999)].det ≠
      0 ∧
    !![1, 0, 1503001499 / 501001001000499999;
        0, 1, -(501000999497498500 / 501001001000499999)].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![501000500 / 501001001000499999, -(1 / 501001001000499999);
            501000500 / 501001001000499999, -(501001001000500000 / 501001001000499999)] *
          !![1000001000, -(1 / 501000500), 3; 1, -1, 1] =
        !![1, 0, 1503001499 / 501001001000499999; 0, 1, -(501000999497498500 / 501001001000499999)] ∧
      !![501000500 / 501001001000499999, -(1 / 501001001000499999);
              501000500 / 501001001000499999, -(501001001000500000 / 501001001000499999)].det ≠
          0 ∧
        !![1, 0, 1503001499 / 501001001000499999;
            0, 1, -(501000999497498500 / 501001001000499999)].IsReducedRowEchelon

    Checked by Lean’s kernel

FLINT’s quartic has two real roots. Each interval in the answer holds exactly one, and the theorem says there are no others:

In[3]:=
#cas [roots x] (2*x^4 - 2*x^3 - 2*x^2 - 3*x - 4 = 0)
Out[3]=

all 2 real solutions; enclosed ones by 20 decimals, truncated

−0.91456483178822888407,2.06403079373484306477
-0.91456483178822888407, 2.06403079373484306477
Input (polynomial equation)
(2*x^4 - 2*x^3 - 2*x^2 - 3*x - 4 = 0)

Checked by Lean’s kernel: Complete list of real solutions

((∃! x,
      x ∈ Set.Ioo (-91456483178822888408 / 10 ^ 20) (-91456483178822888407 / 10 ^ 20) ∧
        2 * x ^ 4 - 2 * x ^ 3 - 2 * x ^ 2 - 3 * x - 4 = 0) ∧
    ∃! x,
      x ∈ Set.Ioo (206403079373484306477 / 10 ^ 20) (206403079373484306478 / 10 ^ 20) ∧
        2 * x ^ 4 - 2 * x ^ 3 - 2 * x ^ 2 - 3 * x - 4 = 0) ∧
  ∀ (x : ℝ),
    2 * x ^ 4 - 2 * x ^ 3 - 2 * x ^ 2 - 3 * x - 4 = 0 →
      x ∈ Set.Ioo (-91456483178822888408 / 10 ^ 20) (-91456483178822888407 / 10 ^ 20) ∨
        x ∈ Set.Ioo (206403079373484306477 / 10 ^ 20) (206403079373484306478 / 10 ^ 20)

from CAS.Polynomial.RealRoot.of_exprRootCheck

The certificate: 1 checked step
  1. Complete list of real solutions

    ((∃! x,
          x ∈ Set.Ioo (-91456483178822888408 / 10 ^ 20) (-91456483178822888407 / 10 ^ 20) ∧
            2 * x ^ 4 - 2 * x ^ 3 - 2 * x ^ 2 - 3 * x - 4 = 0) ∧
        ∃! x,
          x ∈ Set.Ioo (206403079373484306477 / 10 ^ 20) (206403079373484306478 / 10 ^ 20) ∧
            2 * x ^ 4 - 2 * x ^ 3 - 2 * x ^ 2 - 3 * x - 4 = 0) ∧
      ∀ (x : ℝ),
        2 * x ^ 4 - 2 * x ^ 3 - 2 * x ^ 2 - 3 * x - 4 = 0 →
          x ∈ Set.Ioo (-91456483178822888408 / 10 ^ 20) (-91456483178822888407 / 10 ^ 20) ∨
            x ∈ Set.Ioo (206403079373484306477 / 10 ^ 20) (206403079373484306478 / 10 ^ 20)

    Checked by Lean’s kernel · CAS.Polynomial.RealRoot.of_exprRootCheck

The computation
  • Certified isolation

    Square-free polynomial of degree 4, Cauchy bound 13/2, 10 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

And SymPy’s number, with csc written as 1/sin. Where SymPy printed 3.7e+2, each of these fifteen digits is proved:

In[4]:=
#cas [N 15] (1 / sin (231 * π / 500 + 26944))
Out[4]=

first 15 significant digits, truncated

-89.0810623457336
Domain
closed real expression

Checked by Lean’s kernel: Certified decimal digits

-(890810623457336 + 1) / 10 ^ 13 < 1 / Real.sin (231 * Real.pi / 500 + 26944) ∧
  1 / Real.sin (231 * Real.pi / 500 + 26944) ≤ -890810623457336 / 10 ^ 13

from CAS.Continuum.DigitsEval.eval_sound

The certificate: 1 checked step
  1. Certified decimal digits

    -(890810623457336 + 1) / 10 ^ 13 < 1 / Real.sin (231 * Real.pi / 500 + 26944) ∧
      1 / Real.sin (231 * Real.pi / 500 + 26944) ≤ -890810623457336 / 10 ^ 13

    Checked by Lean’s kernel · CAS.Continuum.DigitsEval.eval_sound

The computation
  • Certified numerical evaluation

    Working precision: 89 bits, 39 series terms, 73 logarithm terms
    Guarantee: every printed digit is a digit of the real value, truncated toward zero

Where the idea comes from#

None of this is a new idea, and I’m glad it isn’t. The design, in which an untrusted search proposes an answer and the prover checks it, is Harrison and Théry’s, and it has since been used in HOL Light, Rocq, Isabelle and Lean. A computer algebra system built on top of a proof assistant is Kaliszyk and Wiedijk’s. SymLean follows this design. What we added is reach: many kinds of calculation behind one Lean front end, checked in the kernel by default.

Inside provers, computer algebra and certified numerics have a history of their own:

What this does not mean#

To try it, start with Getting started, or browse the documentation.