Stage 06 · Step 30 of 50
A square is never negative.
01The idea
A squared distance cannot be negative. That single fact can unlock a useful inequality.
Apply it to the difference of two numbers, then expand.
02In maths
For real a and b, 2ab ≤ a² + b².
Picture it
Swipe sideways to see the whole picture.
sq_nonneg (a - b) gives us 0 ≤ (a - b) ^ 2 for every real a and b. Expand the square and move 2ab across, and the inequality appears. nlinarith does that expanding and rearranging for us.03In Lean
This code comes directly from the file checked by Lean.
module
import Mathlib.Basic.Real.Basic
import Mathlib.Tactic.Linarith
example (a b : ℝ) : 2*a*b ≤ a^2 + b^2 := by
have h := sq_nonneg (a - b)
nlinarith
The command have inside a tactic proof adds a local proved fact.
The theorem sq_nonneg says that a real square is nonnegative. We choose the difference of the two variables as its input.
The tactic nlinarith extends linear arithmetic reasoning with useful nonlinear preprocessing. It does not solve every possible nonlinear goal.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
a b : ℝ ⊢ 2 * a * b ≤ a ^ 2 + b ^ 2
After line 6: example (a b : ℝ) : 2*a*b ≤ a^2 + b^2 := by
a b : ℝ ⊢ 2 * a * b ≤ a ^ 2 + b ^ 2
After line 7: have h := sq_nonneg (a - b)
a b : ℝ h : 0 ≤ (a - b) ^ 2 ⊢ 2 * a * b ≤ a ^ 2 + b ^ 2
After line 8: nlinarith
no goals
05Lean says
no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
module
import Mathlib.Basic.Real.Basic
import Mathlib.Tactic.Linarith
example (a b : ℝ) : a^2 + b^2 ≤ 2*a*b := by
have h := sq_nonneg (a - b)
nlinarith
linarith failed to find a contradiction a b : ℝ h : 0 ≤ (a - b) ^ 2 a✝ : 2 * a * b < a ^ 2 + b ^ 2 ⊢ False failed
The inequality is reversed. It is not true for all real a and b.
The fix: Put twice a times b on the smaller side, as in the checked theorem.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Prove that the square of a real number is nonnegative.
Show a hint
Mathlib already has this theorem.
One more hint
Apply sq_nonneg to the supplied real number.
- A little stretch
Prove that four times a times b is at most the square of a plus b.
Show a hint
Start with the nonnegative square of a minus b.
One more hint
nlinarith can expand and combine the resulting polynomial inequality.
module
import Mathlib.Basic.Real.Basic
import Mathlib.Tactic.Linarith
example (a : ℝ) : 0 ≤ a^2 := by
sorry
example (a b : ℝ) : 4*a*b ≤ (a + b)^2 := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
module
import Mathlib.Basic.Real.Basic
import Mathlib.Tactic.Linarith
example (a : ℝ) : 0 ≤ a^2 := by
exact sq_nonneg a
example (a b : ℝ) : 4*a*b ≤ (a + b)^2 := by
have h := sq_nonneg (a - b)
nlinarith
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.