Mode of #cas

cancel

Cancels common factors in each summand of a rational expression, with the conditions under which the result equals the input.

#cas [cancel] e
cancels the common factors of numerator and denominator in each summand of the rational expression e and gives the conditions under which the result equals e.

cancel reduces each fraction in a sum on its own and keeps the terms apart, which is what I want when a sum reads better term by term. together would make one fraction of it.

The catch is the factor you cancel. Cancelling a factor d is valid only where d≠0, and in Lean this matters, because division is total. At x=1 the quotient (x2−1)/(x−1) is 0/0=0, while x+1 is 2. So the theorem has a condition for each divisor of the input.

Details and Options

  • The theorem has the form ∀x1,…,xn,d1≠0→…→dk≠0→e=e′, where d1,…,dk are the divisors of e as written and e′ is the printed sum. It is proved by field_simp and ring.
  • The input is a rational expression as for together: real variables and rational numbers combined by +, -, *, / and natural powers, with polynomial divisors.
  • The summands are not brought to a common denominator.
  • The conditions are all the divisors, also those of summands where nothing cancels. A proof may need fewer (see Applications).
  • simplify cancels common factors in expressions with elementary functions as well.
  • The tactic cas proves these identities, with the conditions as hypotheses.
  • There are no options.

Examples (9)#

Basic Examples (1)

The first summand reduces to x+1, and 1/x stays:

In[1]:=
#cas [cancel] ((x^2-1)/(x-1) + 1/x)
Out[1]=

cancelled

x+1+1x
x + 1 + 1/x
Input (rational function)
((x^2-1)/(x-1) + 1/x)
Conditions
x−1≠0, x≠0

Checked by Lean’s kernel: Identity with the cancelled form

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

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

    Checked by Lean’s kernel

Scope (2)

Two variables:

In[2]:=
#cas [cancel] ((x^3 - y^3)/(x - y) + 1/y)
Out[2]=

cancelled

x2+xy+y2+1y
x^2 + x*y + y^2 + 1/y
Input (rational function)
((x^3 - y^3)/(x - y) + 1/y)
Conditions
x−y≠0, y≠0

Checked by Lean’s kernel: Identity with the cancelled form

∀ (x y : ℝ), x - y ≠ 0 → y ≠ 0 → (x ^ 3 - y ^ 3) / (x - y) + 1 / y = x ^ 2 + x * y + y ^ 2 + 1 / y
The certificate: 1 checked step
  1. Identity with the cancelled form

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

    Checked by Lean’s kernel


A difference of two quotients, each reduced on its own:

In[3]:=
#cas [cancel] ((x^2 - y^2)/(x - y) - (x^3 - 1)/(x - 1))
Out[3]=

cancelled

x+y−(x2+x+1)
x + y - (x^2 + x + 1)
Input (rational function)
((x^2 - y^2)/(x - y) - (x^3 - 1)/(x - 1))
Conditions
x−y≠0, x−1≠0

Checked by Lean’s kernel: Identity with the cancelled form

∀ (x y : ℝ),
  x - y ≠ 0 → x - 1 ≠ 0 → (x ^ 2 - y ^ 2) / (x - y) - (x ^ 3 - 1) / (x - 1) = x + y - (x ^ 2 + x + 1)
The certificate: 1 checked step
  1. Identity with the cancelled form

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

    Checked by Lean’s kernel

Applications (2)

The first example as a theorem. Only x−1≠0 is needed, since 1/x is on both sides, and only the three standard axioms are used:

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

#print axioms cancel_then_add
Out[4]=

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


Without that condition the cancellation is false in Lean. At x=1 the left side is 0/0=0 and the right side is 2:

In[5]:=
example : ¬ ∀ x : ℝ, (x ^ 2 - 1) / (x - 1) = x + 1 := by
  intro h
  have := h 1
  norm_num at this
Out[5]=

Lean accepts the proof.

Properties and Relations (2)

together makes one fraction of the same sum:

In[6]:=
#cas [together] ((x^2-1)/(x-1) + 1/x)
Out[6]=

common denominator

x2+x+1x
(x^2 + x + 1)/x
Input (rational function)
((x^2-1)/(x-1) + 1/x)
Conditions
x−1≠0, x≠0

Checked by Lean’s kernel: Identity with the common-denominator form

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

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

    Checked by Lean’s kernel


simplify cancels a quotient as well:

In[7]:=
#cas [simplify] ((x ^ 4 - 1) / (x ^ 2 - 1))
Out[7]=
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

Possible Issues (2)

A function other than the field operations is refused:

In[8]:=
#cas [cancel] (sin x / x)
Out[8]=

Error

cancel: the expression is not a rational function: it has a function other than the field operations and natural powers

simplify cancels inside functions:

In[9]:=
#cas [simplify] (exp ((x^2 - y^2)/(x - y)))
Out[9]=
ex+y
exp(x + y)
Conditions
x−y≠0

Checked by Lean’s kernel: Identity with the simplified form

∀ (x y : ℝ), x - y ≠ 0 → Real.exp ((x ^ 2 - y ^ 2) / (x - y)) = Real.exp (x + y)

from CAS.Command.eq_of_simplifyFinDisplayRoute

The certificate: 2 checked steps
  1. Identity with the simplified form

    ∀ (x y : ℝ), x - y ≠ 0 → Real.exp ((x ^ 2 - y ^ 2) / (x - y)) = Real.exp (x + y)

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

  2. Simplification under generated rewrite assumptions

    ∀ (ρ : Fin 2 → ℝ),
      CAS.Verified.Expr.Alg.SideHolds ρ
          (((𝕏[0].pow 2).sub (𝕏[1].pow 2)).div (𝕏[0].sub 𝕏[1])).exp.simplifyFinDisplayRoute.nonzero →
        CAS.Verified.Expr.eval ρ
            (((𝕏[0].pow 2).sub (𝕏[1].pow 2)).div (𝕏[0].sub 𝕏[1])).exp.simplifyFinDisplayRoute.rhs =
          CAS.Verified.Expr.eval ρ
            (((𝕏[0].pow 2).sub (𝕏[1].pow 2)).div (𝕏[0].sub 𝕏[1])).exp.simplifyFinDisplayRoute.lhs

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

The computation
  • Variables: x, y
  • Checked symbolic computation

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

See Also

together · simplify · factor · polynomialGCD