Stage 06 · Step 30 of 50

A square is never negative.

Needs Mathlib12 minutesLean 4.36.0

01The idea

A squared distance cannot be negative. That single fact can unlock a useful inequality.

Tacto · Your guide for this idea
Tacto pointing

Apply it to the difference of two numbers, then expand.

02In maths

For real a and b, 2ab ≤ a² + b².

Picture it

From “a square is never negative” to 2ab ≤ a² + b²Left: the graph of (a − b) squared against a − b. It is a U-shaped curve whose lowest point touches zero, when a − b = 0. The point at a − b = −2 has height (−2)² = 4: a negative input still gives a positive square. The region below the axis is shaded red: no square ever lands there. Right: three steps. The fact h : 0 ≤ (a - b) ^ 2, added with have. Expanding the square gives 0 ≤ a² − 2ab + b². Adding 2ab to both sides gives 2ab ≤ a² + b², which is the goal 2 * a * b ≤ a ^ 2 + b ^ 2. A bracket labelled nlinarith covers the expanding and adding steps.SQUARES NEVER DIP BELOW ZEROlowest value: 0² = 0no square ever lands down herea − b(a − b)²(−2)² = 4THE ONE FACT WE ADD (have)h : 0 ≤ (a - b) ^ 2expand the square0 ≤ a² − 2ab + b²add 2ab to both sides2ab ≤ a² + b²nlinarithwhich is the goal in Lean:⊢ 2 * a * b ≤ a ^ 2 + b ^ 2

Swipe sideways to see the whole picture.

Squaring never gives a negative answer, even for negative inputs: (−2)² = 4. So the one fact 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.

E30.leanTry it live (opens official Lean editor)
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.

After line 6
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

Captured from Lean 4.36.0
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Prove that the square of a real number is nonnegative.

  2. A little stretch

    Prove that four times a times b is at most the square of a plus b.

Exercises/E30.leanTry it live (opens official Lean editor)
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

08A quick check

1. Why is the square of a − b nonnegative?

Read the answer and explanation

Every real square is nonnegative. The fact applies to every real input, including negative differences.

2. Does this theorem require a and b to be positive?

Read the answer and explanation

No. The squared-difference proof works for all real a and b.

One more idea in your toolkit.

Take your time. Progress is yours to mark.