Demo Trees in Lean
Tracks
Check a result Declarations Bibliography Lean source

Chapter 3Weights

Main result
Bounded weights give a bounded total

If every number in a list is at most bb, the total weight of the list is at most bb times its length.

The Lean statement
theoremtotalNat_lepropext · Quot.soundAbstract.lean:62
/-- 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 h
Statement reads only Abstract.lean and its imports
4 definitions in 1 chapter · reading list

3.1 Weighing

classWeightno axiomsAbstract.lean:30
/-- A way to weigh the elements of a type by natural numbers. -/class Weight (α : Type u) where  /-- The weight of one element. -/  weight : α → Nat
instanceinstWeightNatno axiomsAbstract.lean:35
/-- A natural number weighs itself. -/instance instWeightNat : Weight Nat := ⟨fun n => n⟩

3.2 Totals

deftotalno axiomsAbstract.lean:38
/-- 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
deftotalNatno axiomsAbstract.lean:41
/-- The total weight of a list of natural numbers, each weighing itself. -/def totalNat (xs : List Nat) : Nat := total xs
defAllno axiomsAbstract.lean:44
/-- Every element of the list satisfies the predicate. -/def All {α : Type u} (p : α → Prop) (xs : List α) : Prop := ∀ x, x ∈ xs → p x
theoremtotalNat_lepropext · Quot.soundAbstract.lean:62
/-- 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 h
Statement reads only Abstract.lean and its imports

3.3 Halving

defhalvingspropext · Quot.soundAbstract.lean:47
/-- The number of times a number can be halved before it drops below two. -/def halvings (n : Nat) : Nat :=  if h : n < 2 then 0 else halvings (n / 2) + 1termination_by ndecreasing_by omega