Option of #cas and cas

oracle

Asks SymPy, SageMath, Maxima, PARI/GP, FLINT, Giac, Singular or Gappa for a proposal, and proves it in Lean or refuses.

#cas [mode, oracle := tool] e
asks tool for the answer of the mode, then proves it in Lean or refuses. The modes are integrate, factor, groebner, solve, nsolve, nintegrate, eigenvalues, enclose, dsolve, sum and sos.
#cas (digits := d) [mode, oracle := tool] e
the same with d digits, for nsolve, nintegrate, eigenvalues and enclose.
cas [oracle := tool]
proves a definite integral, a membership in an ideal, an inequality between polynomials or an equation that follows from the hypotheses, from the proposal of tool. It suggests a proof that needs no oracle, except for a membership, which gets no suggestion, and an inequality whose script doesn’t check as printed, which gets the call itself.

Some searches are better done by a mature computer algebra system, and I’d rather hand those to one than write them again in Lean. With oracle := sympy, SymLean asks SymPy for a proposal (an antiderivative, a factorization, cofactors, a solution, a closed form or a decimal) and then proves the statement in Lean, or refuses. Nothing the oracle says is trusted. If SymPy proposes something wrong, the proof fails, and you get a refusal instead of a theorem.

The examples here are quoted from the oracle tests (tests/oracle), which call the real tools, and each output is the one that #guard_msgs pins there. The module docstrings of CASOracle name the version that each tool was tested with. On 24 November 2026 all of these tests passed, with SymPy 1.14.0, SageMath 10.10, Maxima 5.50.0, PARI/GP 2.17.4, FLINT 3.5.0, Gappa 1.8.3, Giac 2.0.0 and Singular 4.4.1. The site build runs none of these tests.

Details and Options

  • The tools, the programs they run and their tasks:
ToolProgramModes of #casGoals of cas
sympySymPyintegrate, factor, groebner, solve, nsolve, nintegrate, eigenvalues, dsolve, sum, sosintegrals, memberships, inequalities, equations
sageSageMathintegrate, factor, groebner, solve, nintegrate, eigenvalues, dsolve, sumintegrals, memberships, equations
maximaMaximaintegrate, factorintegrals
pariPARI/GPfactor in one variable, nsolve on a polynomial, nintegrate, eigenvaluesnone
flintFLINTfactor, nsolve on a polynomial, nintegrate, eigenvaluesnone
giacGiacintegrate, solve, dsolve, sumintegrals
singularSingularfactor, groebnermemberships, equations
gappaGappaenclosenone
  • Nothing an oracle says is trusted. A command builds its theorem from its own input and the proposal, proves it with the checkers of SymLean, refuses a proof that uses an axiom beyond propext, Classical.choice and Quot.sound, and has the kernel check it before anything is reported.
  • A missing program, a timeout, or an answer outside the checked expression language is a refusal with the reason.
  • What can’t be checked is printed as a hint and isn’t part of the theorem: that a factor is irreducible, that a list of solutions is complete, that a Gröbner basis is reduced, that a family of solutions is the general solution.
  • #cas [nsolve x in [a, b], oracle := tool] (lhs = rhs) asks for the roots of an equation in an interval, which a polynomial equation may leave out. nsolve proves each root by the intermediate value theorem on an interval of width 2·10−d around the oracle’s decimal, and a root at a rational point, such as an end of [a,b], by the equation at that point. The oracles compare the roots of a polynomial with the ends exactly, so that rounding does not drop such a root. The oracle finds the roots with its own numerics, which evaluate log, sqrt, real powers and quotients as complex functions, so it misses the roots that only Lean’s conventions give, such as those of Real.sqrt (x - 1/3) on [0,1/3], where Lean’s square root is 0.
  • eigenvalues proves each eigenvalue by a sign change of the square-free part of the characteristic polynomial on an interval of width 2·10−d. Two eigenvalues closer than about 10−d share such an interval and are refused, and more digits separate them.
  • An inequality is proved by a sum of squares S, found by the Gram-matrix method and made exact as in Peyrl and Parrilo 2008, Section 3.1. cvxpy finds positive semidefinite Gram matrices numerically, and we round them to rationals, project them exactly onto the linear conditions of the identity and keep them when an exact LDL𝖳 factorization shows them positive semidefinite. Lean then checks the identity with ring and the signs with positivity.
  • For 0<p, some term of S is a positive constant, or a positive constant times strict premises. When no such sum equals p, the oracle looks for a multiplier M of degree at most 2, then at most 4, such that Mp is one.
  • tests/oracle/run.sh runs one example per oracle and task with the real tools, and the edge cases of the numerical searches with SymPy and FLINT: roots at the ends of the interval and at the points of SymPy’s grid, a multiple root, and repeated and defective eigenvalues. Then it runs the same commands against programs that answer plausibly but wrongly, never answer, print garbage, answer outside the checked language, answer with something too large to read, answer on the wrong stream, or stop with an error and still exit normally. Every one of those is refused.

Building#

ModuleWhat it adds
CASOracle.Integrate#cas [integrate x, oracle := tool]
CASOracle.Factor#cas [factor, oracle := tool]
CASOracle.Solve#cas [solve x, oracle := tool]
CASOracle.Numericnsolve, nintegrate, eigenvalues and enclose with an oracle
CASOracle.DSolve#cas [dsolve y x, oracle := tool]
CASOracle.Sum#cas [sum k 1 n, oracle := tool]
CASOracle.Sos#cas [sos x, oracle := tool]
CASOracle.Ideal#cas [groebner x, y, oracle := tool] and the tactic cas [oracle := tool]
  • CASOracle is an optional library, and no module of CAS imports it. Build it with lake -R -Koracle=on build CASOracle, and lake -R build switches back.
  • Lake fetches DSLean only with -Koracle=on. An update without it drops DSLean from lake-manifest.json, and lake -R -Koracle=on update DSLean puts it back.
  • The files of CASOracle are ordinary Lean files, not modules, because DSLean’s are, so a module can’t import them.
  • The tools are needed only when a command that names one is elaborated. SymPy, PARI and FLINT run in the Python of SYMLEAN_SYMPY_PYTHON, SYMLEAN_PARI_PYTHON or SYMLEAN_FLINT_PYTHON, else SYMLEAN_ORACLE_PYTHON, else python3. That Python needs sympy, cvxpy for sums of squares, and cypari2 for PARI or python-flint for FLINT. SageMath, Maxima, Giac, Singular and Gappa are the programs of SYMLEAN_SAGE_PATH, SYMLEAN_MAXIMA_PATH, SYMLEAN_GIAC_PATH, SYMLEAN_SINGULAR_PATH and SYMLEAN_GAPPA_PATH.
  • A SageMath from conda-forge needs its environment active, or a script as SYMLEAN_SAGE_PATH that puts the environment’s bin first on PATH and then runs sage, since it starts GAP by name.
  • Giac’s linear algebra library starts one thread per core unless OPENBLAS_NUM_THREADS or OMP_NUM_THREADS limits it. Our tests set both to 1. Giac also looks for its help index in doc/local/ of the working directory, and if that directory exists it creates or empties three files in it. With XCAS_ROOT set to another directory, it uses doc/local/ of that directory instead.
  • The deadline of one run is the option cas.oracle.timeout, in milliseconds, two minutes by default.

Built on DSLean#

We built CASOracle on DSLean, by taterowney and the DSLean contributors, released under CC BY 4.0. Its grammars write each request in the tool’s own language and read the answer back into Lean. That translation is the fiddly part of talking to another system, and I’m grateful we didn’t have to write it ourselves. SymLean requires the port in Robertboy18/DSLean, tag v4.36.0, which updates DSLean to Lean 4.36.0 with the five changes that NOTICE lists. No DSLean file is copied into SymLean.

Examples (24)#

Basic Examples (3)

SymPy factors a polynomial and the identity is proved. That the factors are irreducible is not, and the report says so:

In[1]:=
#cas [factor, oracle := sympy] (x ^ 4 + 4)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[1]=

the identity is checked, irreducibility is not

(x2−2x+2)(x2+2x+2)
(x^2 - 2*x + 2)*(x^2 + 2*x + 2)

In a proof, SymPy finds how the goal follows from the hypotheses. The suggestion is the proof to keep, and it needs no oracle:

In[2]:=
theorem combination_by_sympy (x y : ℝ) (h1 : x * y = 1) (h2 : x = y) : x ^ 2 = 1 := by
  cas [oracle := sympy]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[2]=

Try this

linear_combination h1 + x * h2

An antiderivative from SymPy, proved by differentiating it back:

In[3]:=
#cas [integrate x, oracle := sympy] (x ^ 2 * sin x)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[3]=

antiderivative

−x2cos(x)+2xsin(x)+2cos(x)
-(x^2*cos(x)) + 2*x*sin(x) + 2*cos(x)

Scope (11)

Maxima integrates too:

In[4]:=
#cas [integrate x, oracle := maxima] (x * exp x)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[4]=

antiderivative

ex(x−1)
exp(x)*(x - 1)

So does Giac:

In[5]:=
#cas [integrate x, oracle := giac] (x ^ 2 * sin x)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[5]=

antiderivative

2cos(x)+2xsin(x)−x2cos(x)
2*cos(x) + 2*x*sin(x) - x^2*cos(x)

A Gröbner basis of the same ideal. That the basis is reduced is a hint:

In[6]:=
#cas [groebner x, y, oracle := sympy] [x ^ 2 + y ^ 2 - 5, x * y - 2]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[6]=

Gröbner basis of the same ideal (checked)

2x+y3−5y,y4−5y2+4
2*x + y^3 - 5*y, y^4 - 5*y^2 + 4

Singular computes the same basis and lists it in its own order:

In[7]:=
#cas [groebner x, y, oracle := singular] [x ^ 2 + y ^ 2 - 5, x * y - 2]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[7]=

Gröbner basis of the same ideal (checked)

y4−5y2+4,2x+y3−5y
y^4 - 5*y^2 + 4, 2*x + y^3 - 5*y

Solutions, each checked by substitution. That the list is complete is not checked:

In[8]:=
#cas [solve x, y, oracle := sympy] (x ^ 2 + y ^ 2 = 5 ∧ x * y = 2)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[8]=

solutions checked by substitution; completeness not checked

(x=−2, y=−1)∨(x=−1, y=−2)∨(x=1, y=2)∨(x=2, y=1)
(x = -2, y = -1) ∨ (x = -1, y = -2) ∨ (x = 1, y = 2) ∨ (x = 2, y = 1)

A family of solutions of a differential equation, from SageMath:

In[9]:=
#cas [dsolve y x, oracle := sage] (y'' + y = 0)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[9]=

a family of solutions (checked); that it is the general solution is not

y(x)=c1sin(x)+c2cos(x)
y(x) = c1*sin(x) + c2*cos(x)

A nonlinear equation, from Giac:

In[10]:=
#cas [dsolve y x, oracle := giac] (y' = y ^ 2)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[10]=

a family of solutions (checked); that it is the general solution is not

y(x)=1−x+c1
y(x) = 1/(-x + c1)

A closed form of a sum, proved by induction:

In[11]:=
#cas [sum k 1 n, oracle := sympy] (1 / (k * (k + 1)))

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[11]=

closed form, checked by induction for n ≥ 1

nn+1
n/(n + 1)

Eigenvalues, each within 10−6:

In[12]:=
#cas [eigenvalues, oracle := sympy] !![2, 1, 0; 1, 3, 1; 0, 1, 4]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[12]=

a real eigenvalue in each interval (existence checked)

1.267949±10−6,3.000000±10−6,4.732051±10−6
1.267949 ± 10^-6, 3.000000 ± 10^-6, 4.732051 ± 10^-6

Gappa’s bounds on a box, checked by cega:

In[13]:=
#cas [enclose x in [1/3, 2], oracle := gappa] (sqrt x / (1 + x))

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[13]=

enclosure on the box, checked by cega

[0.192450, 1.060661]
[0.192450, 1.060661]

An inequality between polynomials, by a sum of squares. The suggested script checks the identity with ring and the signs with positivity. The test pins only its start, since the squares come from a numerical solver:

In[14]:=
theorem sos_quartic (x y : ℝ) : 0 ≤ x ^ 4 + y ^ 4 - 4 * x * y + 2 := by
  cas [oracle := sympy]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[14]=

Try this

(rw [show x ^ 4 + y ^ 4 - 4 * x * y + 2 = …

Options (1)

The numerical modes take (digits := d), from 1 to 12 and 6 by default. Here a cube root from FLINT:

In[15]:=
#cas (digits := 8) [nsolve x, oracle := flint] (x ^ 3 = 2)

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[15]=

a root in each interval (existence checked)

1.25992105±10−8
1.25992105 ± 10^-8

Applications (3)

A definite integral, with SymPy’s primitive. The suggestion is a call of cas [integrate primitive := F], which needs no oracle, and the theorem uses only the three standard axioms:

In[16]:=
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).

Out[16]=

Try this

cas [integrate primitive := fun x => (x - 1) * Real.exp x]
In[17]:=
#print axioms integral_by_oracle

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output.

Out[17]=

integral_by_oracle depends on the axioms propext, Classical.choice, Quot.sound.


SageMath finds the combination behind a2+b2+c2=0 over ℚ:

In[18]:=
theorem combination_by_sage (a b c : ℚ) (h₁ : a + b + c = 0)
    (h₂ : a * b + b * c + c * a = 0) : a ^ 2 + b ^ 2 + c ^ 2 = 0 := by
  cas [oracle := sage]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[18]=

Try this

linear_combination (a + b + c) * h₁ + (-2) * h₂

Singular finds that a power of the goal follows: x is not in the ideal of x2, but x2 is.

In[19]:=
theorem power_by_singular (x : ℝ) (h : x ^ 2 = 0) : x = 0 := by
  cas [oracle := singular]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[19]=

Try this

linear_combination (exp := 2) h

Properties and Relations (2)

The suggested proofs stand on their own. Here two of them, SymPy’s combination and the integral, compiled for this page without any oracle:

In[20]:=
example (x y : ℝ) (h1 : x * y = 1) (h2 : x = y) : x ^ 2 = 1 := by
  linear_combination h1 + x * h2
Out[20]=

Lean accepts the proof.

In[21]:=
example : ∫ x in (0 : ℝ)..1, x * Real.exp x = 1 := by
  cas [integrate primitive := fun x => (x - 1) * Real.exp x]
Out[21]=

Lean accepts the proof.


SymPy’s digits for ∫01e−x2dx are the ones nintegrate proves without an oracle:

In[22]:=
#cas [nintegrate x in [0, 1], oracle := sympy] (exp (-x ^ 2))

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[22]=

certified value of the integral

0.746824±10−6
0.746824 ± 10^-6
In[23]:=
#cas [nintegrate x in [0, 1]] (exp (-x^2))
Out[23]=

certified value of the integral

0.746824±10−6
0.746824 ± 10^-6
Domain
x ∈ [0, 1]

Checked 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 certificate: 1 checked step
  1. Certified Simpson panels in one kernel check

    |(∫ (x : ℝ) in 0..1, Real.exp (-x ^ 2)) - 746824 / 10 ^ 6| ≤ 1 / 10 ^ 6

    Checked by Lean’s kernel

The computation
  • Certified quadrature

    Batch Simpson certificate with 16 panels; fourth-derivative bounds by interval evaluation of the simplified symbolic derivative

Possible Issues (3)

A nonnegative polynomial need not be a sum of squares. For Motzkin’s polynomial the oracle finds no certificate, and the refusal proves nothing about the inequality:

In[24]:=
example (x y : ℝ) : 0 ≤ x ^ 4 * y ^ 2 + x ^ 2 * y ^ 4 - 3 * x ^ 2 * y ^ 2 + 1 := by
  cas [oracle := sympy]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[24]=

Error

no positive semidefinite Gram matrix

The premise x≠0 is not an inequality, so it is not sent, and without it there is no sum with a positive term:

In[25]:=
example : ∀ x : ℝ, x ≠ 0 → 0 < x ^ 2 := by
  cas [oracle := sympy]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[25]=

Error

no sum of squares with a positive term

A variable printed as x✝ can’t be written back, so the script as printed doesn’t check. The goal is still proved from the same certificate, and the suggestion is the call itself:

In[26]:=
theorem sos_hidden_atom : ∀ x : ℝ, 0 ≤ x ^ 2 + 1 := by
  intro
  cas [oracle := sympy]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output.

Out[26]=
cas [oracle := sympy]: the script as printed does not check here, so no oracle-free script
is suggested; the goal is proved from the same certificate over the elaborated atoms

Try this

cas [oracle := sympy]

Neat Examples (1)

p=(xy−1)2+x2 is positive but takes values below every positive constant, at x=1/y for large y, so no sum of squares plus a positive constant equals it. A multiplier works, since (1+y2)p=1+(x−y+xy2)2, and the oracle finds one:

In[27]:=
theorem sos_not_attained : ∀ x y : ℝ, 0 < (x * y - 1) ^ 2 + x ^ 2 := by
  cas [oracle := sympy]

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output (the part shown).

Out[27]=

Try this

(intro x y; refine pos_of_mul_pos_right (a := …
In[28]:=
#print axioms sos_not_attained

Quoted from tests/oracle/Oracles.lean, where #guard_msgs pins the output.

Out[28]=

sos_not_attained depends on the axioms propext, Classical.choice, Quot.sound.

See Also

cas · cas [integrate primitive := F] · factor · nintegrate · enclose · Options