Command

#cas

The calculator: evaluates a call, simplifies an expression or runs a mode, and proves its answer.

#cas e
evaluates e when it is a call of a special or number-theoretic function, such as gamma (5/2), and otherwise simplifies it as #cas [simplify] does.
#cas [mode] e
runs a mode on e, such as derive x, factor or solve x.
#cas (options) [mode] e
the same, with the options of the mode, such as (digits := 30).

#cas is the calculator, and for me it is where most work with SymLean starts. You type an expression and get the answer, together with the theorem behind it, checked by Lean’s kernel. Nothing needs setting up beyond import CAS, and names like x and t are variables, as they would be on paper.

Details and Options

  • A report gives the output and its kind, the domain of the variables and the conditions under which the output holds, such as x2−1≠0.
  • The details of a report, in the InfoView (the editor panel where Lean shows its messages), hold the computation and the checked steps. Each step is a proposition, the theorem that proves it, and the check that it passed.
  • #cas e evaluates e when it is a call of ChebyshevT, ChebyshevU, HermiteH, HermiteHe, LegendreP, BernoulliB, zeta, gamma, eulerPhi, divisorSigma, binomial, factorial, fibonacci, powerMod, modularInverse or jacobiSymbol. erf and BesselJ have digits only, by #cas [N d].
  • The call is the whole input, up to parentheses. In #cas (2 * gamma (5/2)) the call is not evaluated, and the input is refused.
  • Integer arguments are closed integer expressions such as 2 ^ 10 - 1, and rational arguments closed rational expressions such as 5/2. A call is refused when an argument or its value needs a number with more than 10000 digits.
  • For any other input, the theorem is that of #cas [simplify] e: ∀x1,…,xn, c1→⋯→ck→e=e′, with the conditions ci of the report.
  • A bracket right after #cas always opens a mode. A word that is no mode is refused. The message names the nearest modes or, when the word names one of the functions above, the commands that evaluate it.
  • The modes are listed by area in the guides: Algebra, Polynomials, Calculus, Series and limits, Equations, Sums and recurrences, Number theory, Linear algebra, Differential equations and Numerics.
  • #cas e has no options. The settings that shape a report are on Options.

Examples (20)#

Basic Examples (3)

A call is evaluated exactly:

In[1]:=
#cas (gamma (5/2))
Out[1]=

exact value of Γ

3π4
3√π/4
Input (rational number)
5/2

Checked by Lean’s kernel: Gamma by a closed formula

Real.Gamma (5 / 2) = 3 * √Real.pi / 4
The certificate: 1 checked step
  1. Gamma by a closed formula

    Real.Gamma (5 / 2) = 3 * √Real.pi / 4

    Checked by Lean’s kernel


A binomial coefficient:

In[2]:=
#cas (binomial 10 3)
Out[2]=

binomial

120
Domain
ℕ
Input (natural numbers)
(10, 3)

Checked by Lean’s kernel: binomial

Nat.choose 10 3 = 120
The certificate: 1 checked step
  1. binomial

    Nat.choose 10 3 = 120

    Checked by Lean’s kernel


Any other input is simplified, with the conditions that the answer needs:

In[3]:=
#cas ((x ^ 4 - 1) / (x ^ 2 - 1))
Out[3]=
x2+1
x^2 + 1
Conditions
x2−1≠0

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
  1. Identity with the simplified form

    ∀ (x : ℝ), x * x - 1 ≠ 0 → (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1

    Checked by Lean’s kernel · CAS.Command.eq_of_simplifyFinDisplayRoute

  2. 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

    Checked by Lean’s kernel · CAS.Verified.Expr.SideClaimRoute.sound

The computation
  • Variables: x
  • Checked symbolic computation

    Raw checked nonzero requirements: x^2 - 1
    Guarantee: these assumptions justify conditional rewrites and preserve real evaluation

Scope (4)

A family of polynomials takes the degree and the variable:

In[4]:=
#cas (ChebyshevT 5 x)
Out[4]=

polynomial with integer coefficients

16x5−20x3+5x
16*x^5 - 20*x^3 + 5*x
Domain
Polynomial.Chebyshev.T R 5, every commutative ring R : Type
Input (polynomial in x)
ChebyshevT 5 x

Checked by Lean’s kernel: Monomial expansion of ChebyshevT 5

∀ (R : Type) [inst : CommRing R],
  Polynomial.Chebyshev.T R 5 = 16 * Polynomial.X ^ 5 - 20 * Polynomial.X ^ 3 + 5 * Polynomial.X
The certificate: 1 checked step
  1. Monomial expansion of ChebyshevT 5

    ∀ (R : Type) [inst : CommRing R],
      Polynomial.Chebyshev.T R 5 = 16 * Polynomial.X ^ 5 - 20 * Polynomial.X ^ 3 + 5 * Polynomial.X

    Checked by Lean’s kernel


An exact value of the zeta function:

In[5]:=
#cas (zeta 4)
Out[5]=

exact value of ζ

π490
pi^4/90
Domain
ℂ (Mathlib's riemannZeta)
Input (even positive integer)
s = 4

Checked by Lean’s kernel: ζ(2k) by Bernoulli numbers (riemannZeta_two_mul_nat)

riemannZeta 4 = ↑Real.pi ^ 4 / 90
The certificate: 1 checked step
  1. ζ(2k) by Bernoulli numbers (riemannZeta_two_mul_nat)

    riemannZeta 4 = ↑Real.pi ^ 4 / 90

    Checked by Lean’s kernel


Large arguments, and closed expressions as arguments:

In[6]:=
#cas (powerMod 2 123456789123456789123456789 1000000007)
Out[6]=

modular power

612059118
Domain
ℕ
Input (natural numbers)
2^123456789123456789123456789 mod 1000000007

Checked by Lean’s kernel: modular power

2 ^ 123456789123456789123456789 % 1000000007 = 612059118
The certificate: 1 checked step
  1. modular power

    2 ^ 123456789123456789123456789 % 1000000007 = 612059118

    Checked by Lean’s kernel

In[7]:=
#cas (eulerPhi (2 ^ 10))
Out[7]=

eulerPhi

512
Domain
ℕ
Input (natural numbers)
1024

Checked by Lean’s kernel: eulerPhi

Nat.totient 1024 = 512
The certificate: 1 checked step
  1. eulerPhi

    Nat.totient 1024 = 512

    Checked by Lean’s kernel


A mode in brackets. Here an antiderivative, which needs the error function:

In[8]:=
#cas [integrate x] (exp (-x ^ 2))
Out[8]=

antiderivative (with the interval integrals)

12πerf(x)
1/2*sqrt(pi)*erf(x)
Input (c e ^ (-α x ^ 2 + β x + γ), α > 0)
(exp (-x ^ 2))

Checked by Lean’s kernel: Integral of the input

∀ (a b : ℝ),
  ∫ (x : ℝ) in a..b, Real.exp (-x ^ 2) = 1 / 2 * √Real.pi * CAS.erf b - 1 / 2 * √Real.pi * CAS.erf a

from CAS.integral_gaussian_eq

The certificate: 2 checked steps
  1. Integral of the input

    ∀ (a b : ℝ),
      ∫ (x : ℝ) in a..b, Real.exp (-x ^ 2) = 1 / 2 * √Real.pi * CAS.erf b - 1 / 2 * √Real.pi * CAS.erf a

    Checked by Lean’s kernel · CAS.integral_gaussian_eq

  2. Derivative of the primitive

    ∀ (x : ℝ), HasDerivAt (fun x ↦ 1 / 2 * √Real.pi * CAS.erf x) (Real.exp (-x ^ 2)) x

    Checked by Lean’s kernel · CAS.hasDerivAt_gaussianPrimitive

Options (2)

#cas e has no options, and a mode takes its own in parentheses before the bracket:

In[9]:=
#cas (digits := 30) [findRoot x in [1, 2]] (x^2 - 2)
Out[9]=

certified simple root

x=1.414213562373095048801688724210±10−30
x = 1.414213562373095048801688724210 ± 10^-30
Domain
x ∈ [1, 2]

Checked by Lean’s kernel: Unique root in the interval

∃! x,
  x ∈ Set.Icc (1414213562373095048801688724209 / 10 ^ 30) (1414213562373095048801688724211 / 10 ^ 30) ∧
    x ^ 2 - 2 = 0
The certificate: 1 checked step
  1. Unique root in the interval

    ∃! x,
      x ∈ Set.Icc (1414213562373095048801688724209 / 10 ^ 30) (1414213562373095048801688724211 / 10 ^ 30) ∧
        x ^ 2 - 2 = 0

    Checked by Lean’s kernel

The computation
  • Certified root

    Sign change found by interval sampling and bisection (not proof)
    Unique root in [1.414213562373095048801688724209, 1.414213562373095048801688724211], within 10^-30 of 1.414213562373095048801688724210: endpoint signs and a derivative of one sign, checked by the kernel

With cas.suggest, the report also offers its theorem as an example to paste:

In[10]:=
set_option cas.suggest true in
#cas ((x ^ 4 - 1) / (x ^ 2 - 1))
Out[10]=
x2+1
x^2 + 1
Conditions
x2−1≠0

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
  1. Identity with the simplified form

    ∀ (x : ℝ), x * x - 1 ≠ 0 → (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1

    Checked by Lean’s kernel · CAS.Command.eq_of_simplifyFinDisplayRoute

  2. 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

    Checked by Lean’s kernel · CAS.Verified.Expr.SideClaimRoute.sound

The computation
  • Variables: x
  • Checked symbolic computation

    Raw checked nonzero requirements: x^2 - 1
    Guarantee: these assumptions justify conditional rewrites and preserve real evaluation

Try this

example (x : ℝ) (h : x * x - 1 ≠ 0) : (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1 := by cas

Applications (2)

The report of #cas (gamma (5/2)) states Γ(5/2)=34π, and cas proves the statement. Only the three standard axioms are used:

In[11]:=
theorem gamma_five_halves : Real.Gamma (5 / 2) = 3 * √Real.pi / 4 := by
  cas

#print axioms gamma_five_halves
Out[11]=

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


The quotient from Basic Examples, as a theorem with its condition:

In[12]:=
theorem simplify_quotient (x : ℝ) (h : x ^ 2 - 1 ≠ 0) :
    (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1 := by
  cas
Out[12]=

Lean accepts the proof.

Properties and Relations (2)

#cas e is #cas [simplify] e when e is not a call:

In[13]:=
#cas [simplify] ((x ^ 4 - 1) / (x ^ 2 - 1))
Out[13]=
x2+1
x^2 + 1
Conditions
x2−1≠0

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
  1. Identity with the simplified form

    ∀ (x : ℝ), x * x - 1 ≠ 0 → (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1

    Checked by Lean’s kernel · CAS.Command.eq_of_simplifyFinDisplayRoute

  2. 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

    Checked by Lean’s kernel · CAS.Verified.Expr.SideClaimRoute.sound

The computation
  • Variables: x
  • Checked symbolic computation

    Raw checked nonzero requirements: x^2 - 1
    Guarantee: these assumptions justify conditional rewrites and preserve real evaluation

A function with digits only is evaluated by #cas [N d]:

In[14]:=
#cas [N 30] (erf 1)
Out[14]=

decimal enclosure of width at most 10^-30, with 30 terms of the series

[0.8427007929497148693412206350826, 0.8427007929497148693412206350827]
[0.8427007929497148693412206350826, 0.8427007929497148693412206350827]
Domain
error function erf x = 2/√π ∫₀ˣ e^(-t²) dt at a rational point

Checked by Lean’s kernel: Enclosure of erf 1

0.8427007929497148693412206350826 ≤ CAS.erf 1 ∧
  CAS.erf 1 ≤ 0.8427007929497148693412206350827 ∧
    0.8427007929497148693412206350827 - 0.8427007929497148693412206350826 ≤ 1 / 10 ^ 30

from CAS.erfCheck_sound

The certificate: 1 checked step
  1. Enclosure of erf 1

    0.8427007929497148693412206350826 ≤ CAS.erf 1 ∧
      CAS.erf 1 ≤ 0.8427007929497148693412206350827 ∧
        0.8427007929497148693412206350827 - 0.8427007929497148693412206350826 ≤ 1 / 10 ^ 30

    Checked by Lean’s kernel · CAS.erfCheck_sound

Possible Issues (6)

The call must be the whole input:

In[15]:=
#cas (2 * gamma (5/2))
Out[15]=

Error

unsupported symbolic function 'gamma'; expected abs, sin, cos, tan, sinh, cosh, tanh, atan/arctan, asin/arcsin, acos/arccos, exp, log, or sqrt. `gamma x` is evaluated as the whole input of `#cas (gamma x)`

A family of polynomials needs the variable too:

In[16]:=
#cas (ChebyshevT 5)
Out[16]=

Error

ChebyshevT takes 2 arguments, as in `ChebyshevT n x`; the call has 1

#cas computes no exact value of the error function, only its digits:

In[17]:=
#cas (erf 1)
Out[17]=

Error

`#cas` computes no exact value of `erf x`; `#cas [N d] (erf x)` gives its digits

Mode words are case sensitive:

In[18]:=
#cas [Simplify] (x + x)
Out[18]=

Error

`Simplify` is no mode of `#cas`; the nearest mode is `simplify`

A function in the bracket gets the commands that evaluate it:

In[19]:=
#cas [LegendreP 5]
Out[19]=

Error

`LegendreP` is no mode of `#cas`; write `#cas (LegendreP n x)`, `#cas [expand] (LegendreP n x)` or `#cas [N d] (LegendreP n x)`

A value with more than 10000 digits is refused:

In[20]:=
#cas (factorial 5000)
Out[20]=

Error

factorial: the value has more than 10000 digits, the most that a call computes

Neat Examples (1)

The hundredth Fibonacci number, with the theorem that proves it:

In[21]:=
#cas (fibonacci 100)
Out[21]=

fibonacci

354224848179261915075
Domain
ℕ
Input (natural numbers)
100

Checked by Lean’s kernel: fibonacci

Nat.fib 100 = 354224848179261915075
The certificate: 1 checked step
  1. fibonacci

    Nat.fib 100 = 354224848179261915075

    Checked by Lean’s kernel

See Also

simplify · cas · derive · integrate · N · Options