Chapter 3Weights
Main result
Bounded weights give a bounded total
If every number in a list is at most , the total weight of the list is at most times its length.
The Lean statement
/-- The total weight of a list of numbers that are all at most `b` is at most `b` times its length. -/theorem totalNat_le (b : Nat) (xs : List Nat) (h : All (· ≤ b) xs) : totalNat xs ≤ b * xs.length := … by have : (xs.map Weight.weight).sum = xs.sum := by congr 1 exact List.map_id' xs show (xs.map Weight.weight).sum ≤ b * xs.length rw [this] exact Lemmas.sum_le_mul b xs hStatement reads only
Abstract.lean and its imports4 definitions in 1 chapter · reading list
3.1 Weighing
/-- A way to weigh the elements of a type by natural numbers. -/class Weight (α : Type u) where /-- The weight of one element. -/ weight : α → Nat/-- A natural number weighs itself. -/instance instWeightNat : Weight Nat := ⟨fun n => n⟩3.2 Totals
/-- The total weight of a list: the sum of the weights of its elements. -/def total {α : Type u} [Weight α] (xs : List α) : Nat := (xs.map Weight.weight).sum/-- The total weight of a list of natural numbers, each weighing itself. -/def totalNat (xs : List Nat) : Nat := total xs/-- Every element of the list satisfies the predicate. -/def All {α : Type u} (p : α → Prop) (xs : List α) : Prop := ∀ x, x ∈ xs → p x/-- The total weight of a list of numbers that are all at most `b` is at most `b` times its length. -/theorem totalNat_le (b : Nat) (xs : List Nat) (h : All (· ≤ b) xs) : totalNat xs ≤ b * xs.length := … by have : (xs.map Weight.weight).sum = xs.sum := by congr 1 exact List.map_id' xs show (xs.map Weight.weight).sum ≤ b * xs.length rw [this] exact Lemmas.sum_le_mul b xs hStatement reads only
Abstract.lean and its imports