Stage 06 · Step 28 of 50

Follow the inequalities.

Needs Mathlib12 minutesLean 4.36.0

01The idea

Two short routes can make one longer route. Inequalities can chain in the same way.

Tacto · Your guide for this idea
Tacto pointing

linarith combines linear facts to establish the target inequality.

02In maths

For rational x and y, if x ≤ y and y ≤ 7, then x ≤ 7.

Picture it

linarith: two short hops make one long routeTop: a number line with three points, x, then y, then 7. A teal hop from x to y is labelled h₁ : x ≤ y, and a gold hop from y to 7 is labelled h₂ : y ≤ 7. A long green route underneath goes straight from x to 7 and is labelled x ≤ 7. Bottom: the two facts are added like a column sum: x ≤ y plus y ≤ 7 gives x + y ≤ y + 7. Taking y off both sides leaves x ≤ 7. linarith finds a sum like this and closes the goal.h₁ : x ≤ yh₂ : y ≤ 7xy7x ≤ 7two short hops make one long routex≤yh₁+y≤7h₂x + y≤y + 7take y off both sidesx ≤ 7linarithfinds a sum like thisand closes the goal

Swipe sideways to see the whole picture.

If x is at or before y, and y is at or before 7, then x is at or before 7: two short hops make one long route. Behind the scenes, linarith adds up the facts you have until the goal drops out. Here the sum x + y ≤ y + 7 becomes x ≤ 7 once the y on each side cancels.

03In Lean

This code comes directly from the file checked by Lean.

E28.leanTry it live (opens official Lean editor)
module

import Mathlib.Tactic.Linarith

example (x y : ℚ) (h₁ : x ≤ y) (h₂ : y ≤ 7) : x ≤ 7 := by
  linarith

An inequality compares values using an order relation. The symbol ≤ means “less than or equal”; type it with \le.

The tactic linarith searches for a suitable linear consequence of the arithmetic hypotheses.

Here the variables are rational numbers. Unlike omega, linarith also supports common ordered-field settings such as the reals.

04Step by step

Choose a line to see Lean’s recorded goal state after that line.

After line 5
x y : ℚ
h₁ : x ≤ y
h₂ : y ≤ 7
⊢ x ≤ 7
After line 5: example (x y : ℚ) (h₁ : x ≤ y) (h₂ : y ≤ 7) : x ≤ 7 := by
x y : ℚ
h₁ : x ≤ y
h₂ : y ≤ 7
⊢ x ≤ 7
After line 6: linarith
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

    Derive the bound on x from x plus three being at most eight.

  2. A little stretch

    Solve for x using the two supplied linear equalities.

Exercises/E28.leanTry it live (opens official Lean editor)
module

import Mathlib.Tactic.Linarith

example (x : ℚ) (h : x + 3 ≤ 8) : x ≤ 5 := by
  sorry

example (x y : ℚ) (h₁ : 2*x + y = 8) (h₂ : y = 2) : x = 3 := by
  sorry

08A quick check

1. What kind of hypotheses does this use of linarith combine?

Read the answer and explanation

Linear equalities and inequalities. It reasons with linear combinations of arithmetic constraints.

2. Does x ≤ y imply x < y?

Read the answer and explanation

Not in general. Equality is allowed by ≤. A strict conclusion needs enough additional information.

One more idea in your toolkit.

Take your time. Progress is yours to mark.