Mode of #cas

fullSimplify

Tries many forms of a real expression and gives the simplest one that is proved equal to it, with the conditions the identity needs.

#cas [fullSimplify] e
gives the simplest form of the real expression e by leafCount that it proves equal to e, with the conditions under which it holds.
#cas [fullSimplify c] e
does the same with the complexity function c, one of leafCount, depth and length.

An expression usually has many equivalent forms, and fullSimplify looks for the simplest one. It gathers candidate forms, ranks them by size, and reports the simplest one whose identity with the input the kernel accepts. It plays the part of Mathematica’s FullSimplify with LeafCount, except that the answer is a theorem.

It works the way the rest of SymLean does: the search only proposes. A candidate that can’t be proved costs some time and nothing else, and the next one in the ranking is tried. So a bad guess can make the command slower, but it can’t make the answer wrong.

Details and Options

  • The theorem has the form ∀x1,…,xn,c1→…→ck→e=e′ about Mathlib’s real functions, where e′ is the printed form and c1,…,ck are the printed conditions. Its proof is the rewrite that produced the form (the trigonometric or logarithm lemmas, or Real.sqrt_sq_eq_abs), reflexivity, Mathlib’s tactic ring, the tactic cas, or Mathlib’s field_simp followed by ring.
  • The candidates are the input, the results of the checked simplification routes, the expanded and the factored polynomial, the trigonometric expansion of sums and double angles, the expansion of logarithms of products, quotients and powers, and |t| for the square root of a perfect square t2.
  • leafCount, the default, counts atoms and operations, after Mathematica’s LeafCount with weights of its own. A variable, an integer, π and e count 1, and a fraction such as 3/2 counts 3. A sum, difference, product, quotient, real power or function counts 1 plus its operands, and a negation or a natural power ak counts 2 plus a. So (x+1)5 counts 5 and its expansion counts 27.
  • depth is the depth of the expression tree, and length is the length of the printed form.
  • Ties go to the candidate with fewer conditions, then to the shorter printed form. The candidates are tried in the order of their rank, and the first one proved is reported. “The computation” lists the ranked candidates with their sizes, and these are proposals, not proof.
  • The variables and functions are those of simplify. expand, factor and trigExpand run the same search with fewer candidates.
  • The tactic cas proves most of these identities as goals, with the conditions as hypotheses. An identity with a square root needs sign hypotheses there (see Possible Issues).

Examples (15)#

Basic Examples (3)

A polynomial that is a fifth power. The factored form counts 5, the input 27:

In[1]:=
#cas [fullSimplify] (x^5 + 5*x^4 + 10*x^3 + 10*x^2 + 5*x + 1)
Out[1]=
(x+1)5
(x + 1)^5

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), x ^ 5 + 5 * x ^ 4 + 10 * x ^ 3 + 10 * x ^ 2 + 5 * x + 1 = (x + 1) ^ 5
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), x ^ 5 + 5 * x ^ 4 + 10 * x ^ 3 + 10 * x ^ 2 + 5 * x + 1 = (x + 1) ^ 5

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: (x + 1)^5  [leafCount 5, conditions 0]
    input: x^5 + 5*x^4 + 10*x^3 + 10*x^2 + 5*x + 1  [leafCount 27, conditions 0]
    exact passes: x^5 + 5*(x^4 + 2*(x^3 + x^2 + 1/2*(x + 1/5)))  [leafCount 29, conditions 0]

Mathlib’s logarithm is log|x|, with log0=0, so the product rule needs only x≠0 and y≠0, not positivity:

In[2]:=
#cas [fullSimplify] (log (x*y) - log x - log y)
Out[2]=
0
Conditions
x≠0, y≠0

Checked by Lean’s kernel: Identity with the logarithm expansion and simplification

∀ (x y : ℝ), x ≠ 0 → y ≠ 0 → Real.log (x * y) - Real.log x - Real.log y = 0
The certificate: 1 checked step
  1. Identity with the logarithm expansion and simplification

    ∀ (x y : ℝ), x ≠ 0 → y ≠ 0 → Real.log (x * y) - Real.log x - Real.log y = 0

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    logarithm expansion and simplification: 0  [leafCount 1, conditions 2]
    input: log(x*y) - log(x) - log(y)  [leafCount 10, conditions 0]
    configured simplification: -log(x) - log(y) + log(x*y)  [leafCount 12, conditions 0]
    exact passes: log(x*y) + -log(x) - log(y)  [leafCount 12, conditions 0]

The square root of a perfect square is an absolute value, for every x:

In[3]:=
#cas [fullSimplify] (sqrt (x^2 + 2*x + 1))
Out[3]=
|x+1|
|x + 1|

Checked by Lean’s kernel: Identity with the square root of a square

∀ (x : ℝ), √(x ^ 2 + 2 * x + 1) = |x + 1|
The certificate: 1 checked step
  1. Identity with the square root of a square

    ∀ (x : ℝ), √(x ^ 2 + 2 * x + 1) = |x + 1|

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    square root of a square: |x + 1|  [leafCount 4, conditions 0]
    input: sqrt(x^2 + 2*x + 1)  [leafCount 10, conditions 0]
    configured simplification: sqrt(x^2 + 2*(x + 1/2))  [leafCount 12, conditions 0]

Scope (4)

Trigonometric identities that reduce to 0:

In[4]:=
#cas [fullSimplify] (sin (2*x) - 2*sin x*cos x)
Out[4]=
0

Checked by Lean’s kernel: Identity with the trigonometric expansion and simplification

∀ (x : ℝ), Real.sin (2 * x) - 2 * Real.sin x * Real.cos x = 0
The certificate: 1 checked step
  1. Identity with the trigonometric expansion and simplification

    ∀ (x : ℝ), Real.sin (2 * x) - 2 * Real.sin x * Real.cos x = 0

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    trigonometric expansion and simplification: 0  [leafCount 1, conditions 0]
    input: sin(2*x) - 2*sin(x)*cos(x)  [leafCount 12, conditions 0]
    configured simplification: sin(2*x) - 2*(sin(x)*cos(x))  [leafCount 12, conditions 0]
    trigonometric expansion: 2*sin(x)*cos(x) - 2*sin(x)*cos(x)  [leafCount 15, conditions 0]
In[5]:=
#cas [fullSimplify] (cos x ^ 2 - sin x ^ 2 - cos (2*x))
Out[5]=
0

Checked by Lean’s kernel: Identity with the trigonometric expansion and simplification

∀ (x : ℝ), Real.cos x ^ 2 - Real.sin x ^ 2 - Real.cos (2 * x) = 0
The certificate: 1 checked step
  1. Identity with the trigonometric expansion and simplification

    ∀ (x : ℝ), Real.cos x ^ 2 - Real.sin x ^ 2 - Real.cos (2 * x) = 0

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    trigonometric expansion and simplification: 0  [leafCount 1, conditions 0]
    configured simplification: 1 + -2*sin(x)^2 - cos(2*x)  [leafCount 13, conditions 0]
    input: cos(x)^2 - sin(x)^2 - cos(2*x)  [leafCount 14, conditions 0]
    exact passes: cos(x)^2 + -sin(x)^2 - cos(2*x)  [leafCount 16, conditions 0]
    trigonometric expansion: cos(x)^2 - sin(x)^2 - (cos(x)^2 - sin(x)^2)  [leafCount 19, conditions 0]

A quotient that cancels, with its condition:

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

Checked by Lean’s kernel: Identity with the configured simplification

∀ (x : ℝ), x ^ 2 - 1 ≠ 0 → (x ^ 4 - 1) / (x ^ 2 - 1) = x ^ 2 + 1
The certificate: 1 checked step
  1. Identity with the configured simplification

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

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    configured simplification: x^2 + 1  [leafCount 5, conditions 1]
    input: (x^4 - 1)/(x^2 - 1)  [leafCount 11, conditions 0]

Several variables:

In[7]:=
#cas [fullSimplify] (x^3 + 3*x^2*y + 3*x*y^2 + y^3)
Out[7]=
(x+y)3
(x + y)^3

Checked by Lean’s kernel: Identity with the factored homogeneous polynomial

∀ (x y : ℝ), x ^ 3 + 3 * x ^ 2 * y + 3 * x * y ^ 2 + y ^ 3 = (x + y) ^ 3
The certificate: 1 checked step
  1. Identity with the factored homogeneous polynomial

    ∀ (x y : ℝ), x ^ 3 + 3 * x ^ 2 * y + 3 * x * y ^ 2 + y ^ 3 = (x + y) ^ 3

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored homogeneous polynomial: (x + y)^3  [leafCount 5, conditions 0]
    factored homogeneous polynomial: (y + x)^3  [leafCount 5, conditions 0]
    exact passes: x^3 + y*(3*x^2 + 3*(x*y) + y^2)  [leafCount 21, conditions 0]
    input: x^3 + 3*x^2*y + 3*x*y^2 + y^3  [leafCount 23, conditions 0]
    configured simplification: x^3 + 3*(x^2*y) + 3*(x*y^2) + y^3  [leafCount 23, conditions 0]

In Mathlib exp(logx)=|x| for x≠0, and exp(log0)=1, so getting x back needs x>0:

In[8]:=
#cas [fullSimplify] (exp (log x))
Out[8]=
x
Conditions
x>0

Checked by Lean’s kernel: Identity with the configured simplification

∀ (x : ℝ), 0 < x → Real.exp (Real.log x) = x
The certificate: 1 checked step
  1. Identity with the configured simplification

    ∀ (x : ℝ), 0 < x → Real.exp (Real.log x) = x

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    configured simplification: x  [leafCount 1, conditions 1]
    input: exp(log(x))  [leafCount 3, conditions 0]

Options (1)

With depth, the depth of the expression tree decides:

In[9]:=
#cas [fullSimplify depth] (x^2 + 2*x + 1)
Out[9]=
(x+1)2
(x + 1)^2

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), x ^ 2 + 2 * x + 1 = (x + 1) ^ 2
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), x ^ 2 + 2 * x + 1 = (x + 1) ^ 2

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: (x + 1)^2  [depth 3, conditions 0]
    input: x^2 + 2*x + 1  [depth 4, conditions 0]
    exact passes: x^2 + 2*(x + 1/2)  [depth 4, conditions 0]
In[10]:=
#cas [fullSimplify depth] (sin (2*x) - 2*sin x*cos x)
Out[10]=
0

Checked by Lean’s kernel: Identity with the trigonometric expansion and simplification

∀ (x : ℝ), Real.sin (2 * x) - 2 * Real.sin x * Real.cos x = 0
The certificate: 1 checked step
  1. Identity with the trigonometric expansion and simplification

    ∀ (x : ℝ), Real.sin (2 * x) - 2 * Real.sin x * Real.cos x = 0

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    trigonometric expansion and simplification: 0  [depth 1, conditions 0]
    input: sin(2*x) - 2*sin(x)*cos(x)  [depth 5, conditions 0]
    configured simplification: sin(2*x) - 2*(sin(x)*cos(x))  [depth 5, conditions 0]
    trigonometric expansion: 2*sin(x)*cos(x) - 2*sin(x)*cos(x)  [depth 5, conditions 0]

Applications (2)

The logarithm identity as a theorem, with the printed conditions as hypotheses. Only the three standard axioms are used:

In[11]:=
theorem log_mul_sub_logs (x y : ℝ) (hx : x ≠ 0) (hy : y ≠ 0) :
    Real.log (x * y) - Real.log x - Real.log y = 0 := by
  cas

#print axioms log_mul_sub_logs
Out[11]=

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


Away from 0 the logarithm sees |x|, and cas proves the identity with the absolute value:

In[12]:=
example (x : ℝ) (hx : x ≠ 0) : Real.exp (3 * Real.log x) = |x| ^ 3 := by
  cas
Out[12]=

Lean accepts the proof.

Properties and Relations (1)

fullSimplify keeps x3−x, which counts 5, over x(x−1)(x+1), which counts 9. factor gives the factored form whatever its size:

In[13]:=
#cas [fullSimplify] (x^3 - x)
Out[13]=
x3−x
x^3 - x

Checked by Lean’s kernel: Identity with the input

∀ (x : ℝ), x ^ 3 - x = x ^ 3 - x
The certificate: 1 checked step
  1. Identity with the input

    ∀ (x : ℝ), x ^ 3 - x = x ^ 3 - x

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    input: x^3 - x  [leafCount 5, conditions 0]
    exact passes: x*(x^2 - 1)  [leafCount 7, conditions 0]
    factored polynomial: x*(x - 1)*(x + 1)  [leafCount 9, conditions 0]
In[14]:=
#cas [factor] (x ^ 3 - x)
Out[14]=
x*(x - 1)*(x + 1)

Checked by Lean’s kernel: Identity with the factored polynomial

∀ (x : ℝ), x ^ 3 - x = x * (x - 1) * (x + 1)
The certificate: 1 checked step
  1. Identity with the factored polynomial

    ∀ (x : ℝ), x ^ 3 - x = x * (x - 1) * (x + 1)

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    factored polynomial: x*(x - 1)*(x + 1)  [leafCount 9, conditions 0]
    input: x^3 - x  [leafCount 5, conditions 0]

Possible Issues (3)

The complexity function must be one of the three:

In[15]:=
#cas [fullSimplify size] (x + 1)
Out[15]=

Error

unknown complexity function size; use leafCount, depth or length

The tactic cas does not prove an identity with a square root without sign hypotheses, even one that fullSimplify proves for every x. Its message lists the candidate conditions:

In[16]:=
example (x : ℝ) : Real.sqrt (x ^ 2 + 2 * x + 1) = |x + 1| := by
  cas
Out[16]=

Error

cas could not close this ordinary arithmetic goal.
This goal contains branch-sensitive operations and may need nonnegative or positive square-root hypotheses. 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: 0 ≤ x ^ 2 + 2 * x + 1.
The rest of the message
Polynomial equation route:
  `grind` failed
  case grind
  x : ℝ
  h : ¬√(x * (x + 2) + 1) = |x + 1|
  ⊢ False
  [grind] Goal diagnostics
    [facts] Asserted facts
      [prop] ¬√(x * (x + 2) + 1) = |x + 1|
    [eqc] False propositions
      [prop] √(x * (x + 2) + 1) = |x + 1|
    [ematch] E-matching patterns
      [thm] Real.sq_sqrt': [Real.sqrt #0]
      [thm] Real.sqrt_one: [Real.sqrt `[1]]
      [thm] Real.sqrt_zero: [Real.sqrt `[0]]
      [thm] abs_mul: [@abs #5 _ _ (@HMul.hMul _ _ _ _ #1 #0)]
      [thm] abs.eq_1: [@abs #3 #2 #1 #0]
    [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 identify a side of the identity with its reflected form
Grind fallback:
  `grind` failed (`set_option trace.cas.fallback true` shows its diagnostics)

In a proof, Mathlib’s Real.sqrt_sq_eq_abs does it, the lemma behind the fullSimplify answer:

In[17]:=
example (x : ℝ) : Real.sqrt (x ^ 2 + 2 * x + 1) = |x + 1| := by
  rw [show x ^ 2 + 2 * x + 1 = (x + 1) ^ 2 by ring]
  exact Real.sqrt_sq_eq_abs (x + 1)
Out[17]=

Lean accepts the proof.

Neat Examples (1)

A quartic that is the square of a quadratic. The answer (x2−1)2 counts 7, while the full factorization (x−1)2(x+1)2 would count 11:

In[18]:=
#cas [fullSimplify] (x^4 - 2*x^2 + 1)
Out[18]=
(x2−1)2
(x^2 - 1)^2

Checked by Lean’s kernel: Identity with the perfect power

∀ (x : ℝ), x ^ 4 - 2 * x ^ 2 + 1 = (x ^ 2 - 1) ^ 2
The certificate: 1 checked step
  1. Identity with the perfect power

    ∀ (x : ℝ), x ^ 4 - 2 * x ^ 2 + 1 = (x ^ 2 - 1) ^ 2

    Checked by Lean’s kernel

The computation
  • Ranked candidates (proposals, not proof)

    perfect power: (x^2 - 1)^2  [leafCount 7, conditions 0]
    perfect power: ((x - 1)*(x + 1))^2  [leafCount 9, conditions 0]
    input: x^4 - 2*x^2 + 1  [leafCount 11, conditions 0]
    factored polynomial: (x - 1)^2*(x + 1)^2  [leafCount 11, conditions 0]
    exact passes: x^4 - 2*(x^2 - 1/2)  [leafCount 13, conditions 0]

See Also

simplify · expand · factor · trigExpand · cas