Tactic

cas_nf

Rewrites the real terms of a goal or a hypothesis to a normal form, with a checked proof of each rewrite.

cas_nf
rewrites the real terms of the goal to their normal form, and closes the goal when it becomes a reflexive equality.
cas_nf at h
rewrites the hypothesis h. at h ⊢ and at * choose locations as for simp.
cas_nf using h
the same, with h as evidence for the conditions a rewrite needs, such as a nonzero denominator.
conv => cas_nf
rewrites the focused term only, also as cas_nf using h.

cas closes a goal or leaves it alone. Sometimes I want something in between: the goal is not finished, but one quotient in it should cancel, or a hypothesis should look like the goal. cas_nf rewrites to a normal form and leaves the rest of the proof to me.

Details and Options

  • cas_nf turns the goal P e into P e', with a proof of e=e′. A polynomial gets the normal form of Mathlib’s ring_nf. An expression with quotients, roots, logarithms or trigonometric functions gets the normal form of the checked symbolic routes.
  • A rewrite that needs a condition, such as x−y≠0 to cancel (x2−y2)/(x−y), takes it from the local context or from using h. For two conditions, pass And.intro h₁ h₂.
  • Without the condition the rewrite is refused, never assumed. Lean’s division is total, and the normal form keeps that meaning: x/0 becomes 0.
  • cas_nf normalizes, and is not an equality solver: two equal analytic expressions can keep different normal forms.
  • It fails when it changes nothing.
  • There are no options.

Examples (15)#

Basic Examples (3)

A polynomial identity becomes reflexive, and the goal closes:

In[1]:=
example (x y : ℝ) : (x + y) ^ 2 = x ^ 2 + 2 * x * y + y ^ 2 := by
  cas_nf
Out[1]=

Lean accepts the proof.


A hypothesis and the goal brought to the same form:

In[2]:=
example (x y c : ℝ) (h : (x + y) ^ 2 ≤ c) : x ^ 2 + 2 * x * y + y ^ 2 ≤ c := by
  cas_nf at h ⊢
  exact h
Out[2]=

Lean accepts the proof.


A quotient that cancels, under a function you know nothing about, with the denominator condition passed by using:

In[3]:=
example (F : ℝ → ℝ) (x y z : ℝ) (hden : x - y ≠ 0) (h : F (x + y) = z) :
    F ((x ^ 2 - y ^ 2) / (x - y)) = z := by
  cas_nf using hden
  exact h
Out[3]=

Lean accepts the proof.

Scope (4)

The Pythagorean identity inside a hypothesis:

In[4]:=
example (P : ℝ → Prop) (x : ℝ) (h : P (Real.sin x ^ 2 + Real.cos x ^ 2)) : P 1 := by
  cas_nf at h
  exact h
Out[4]=

Lean accepts the proof.


The normal form of elogx depends on what you know about x:

In[5]:=
example (P : ℝ → Prop) (x : ℝ) (hx : 0 < x) (h : P (Real.exp (Real.log x))) : P x := by
  cas_nf using hx at h
  exact h
Out[5]=

Lean accepts the proof.

In[6]:=
example (P : ℝ → Prop) (x : ℝ) (hx : x ≠ 0) (h : P (Real.exp (Real.log x))) : P |x| := by
  cas_nf using hx at h
  exact h
Out[6]=

Lean accepts the proof.

In[7]:=
example (P : ℝ → Prop) (x : ℝ) (hx : x < 0) (h : P (Real.exp (Real.log x))) : P (-x) := by
  cas_nf using hx at h
  exact h
Out[7]=

Lean accepts the proof.


Square roots, with and without a sign condition:

In[8]:=
example (P : ℝ → Prop) (x : ℝ) (hx : 0 ≤ x) (h : P (√x ^ 2)) : P x := by
  cas_nf using hx at h
  exact h
Out[8]=

Lean accepts the proof.

In[9]:=
example (P : ℝ → Prop) (x : ℝ) (h : P (√(x ^ 2))) : P |x| := by
  cas_nf at h
  exact h
Out[9]=

Lean accepts the proof.


Two conditions at once:

In[10]:=
example (P : ℝ → Prop) (F : ℝ → ℝ → ℝ) (x y z : ℝ) (hz : 0 < z) (hden : x - y ≠ 0)
    (h : P (F (Real.exp (Real.log z)) ((x ^ 2 - y ^ 2) / (x - y)))) :
    P (F z (x + y)) := by
  cas_nf using And.intro hz hden at h
  exact h
Out[10]=

Lean accepts the proof.

Options (2)

In conv mode only the focused term changes. Here the first argument is rewritten and the second, the same term, is not:

In[11]:=
example (P : ℝ → ℝ → Prop) (x y : ℝ) (h : P (x * y * 2 + x ^ 2 + y ^ 2) ((x + y) ^ 2)) :
    P ((x + y) ^ 2) ((x + y) ^ 2) := by
  conv =>
    arg 1
    cas_nf
  exact h
Out[11]=

Lean accepts the proof.


cas_nf using h works under a binder too:

In[12]:=
example (F : ℝ → ℝ) (hden : ∀ t, F t ≠ 0) :
    (fun t ↦ F t / F t) = (fun _ ↦ (1 : ℝ)) := by
  conv =>
    lhs
    intro t
    cas_nf using hden t
Out[12]=

Lean accepts the proof.

Applications (1)

A theorem whose proof is one normalization and one exact. Only the three standard axioms are used:

In[13]:=
theorem square_le (x y c : ℝ) (h : (x + y) ^ 2 ≤ c) : x ^ 2 + 2 * x * y + y ^ 2 ≤ c := by
  cas_nf at h ⊢
  exact h

#print axioms square_le
Out[13]=

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

Properties and Relations (2)

cancel shows the same cancellation as a #cas report, with its condition:

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

cancelled

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

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

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

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

    Checked by Lean’s kernel


Lean’s division is total. cas_nf keeps that meaning, so x/0 is 0:

In[15]:=
example (P : ℝ → Prop) (x : ℝ) (h : P (x / 0)) : P 0 := by
  cas_nf at h
  exact h
Out[15]=

Lean accepts the proof.

Possible Issues (2)

cas_nf fails when it changes nothing. This goal is in normal form already, and exact h closes it:

In[16]:=
example (P : ℝ → Prop) (x : ℝ) (h : P x) : P x := by
  cas_nf
Out[16]=

Error

`cas_nf` made no progress on the goal

A cancellation needs its condition. Here x−1≠0 is missing. The message says what kind of condition to supply and lists the missing one, written x + ↑(-1) ≠ 0:

In[17]:=
example (P : ℝ → Prop) (x : ℝ) (h : P ((x ^ 2 - 1) / (x - 1))) : P (x + 1) := by
  cas_nf at h
  exact h
Out[17]=

Error

cas_nf could not prove the real analytic domain conditions.
A denominator or logarithm argument may need to be nonzero, a square-root argument may need to be positive, or an inverse-trigonometric argument may need to avoid a branch endpoint. Supply the relevant ordinary hypothesis with `cas_nf using h`.
Unproved conditions:
  x + ↑(-1) ≠ 0

Neat Examples (1)

Arguments are normalized before equal atoms are collected, so F(x+0)+F(x) becomes 2F(x):

In[18]:=
example (P : ℝ → Prop) (F : ℝ → ℝ) (x : ℝ) (h : P (F (x + 0) + F x)) : P (2 * F x) := by
  cas_nf at h
  exact h
Out[18]=

Lean accepts the proof.

See Also

cas · simplify · expand · together · cancel