Formal mathematics laboratory
Lumenary.DataAndTallies
Data, tallies and picture graphs
Formal library
WaystructureDefinitionNo axioms
WayLean 4Formally verified /-- The four categories for the question "How did you come to school today?". Every child's answer is exactly one of them. -/ inductive Way where | walk | bus | bike | car deriving DecidableEq, RepranswersdefDefinitionNo axioms
answersLean 4Formally verified /-- The answers of the 24 children in Ms Wanjiru's class, in the order they were asked. -/ def answers : List Way := [.walk, .bus, .walk, .car, .bike, .walk, .bus, .walk, .bike, .bus, .walk, .car, .walk, .bus, .bike, .walk, .bus, .walk, .bike, .bus, .car, .walk, .bus, .bike]tallydefDefinitionNo axioms
tallyLean 4Formally verified /-- The tally for category `w`: how many answers in the list are `w`. -/ def tally (w : Way) (l : List Way) : Nat := l.count wclass_talliestheoremProved here from basic factsNo axioms
class_talliesLean 4Proved here from basic factsFormally verified /-- The class's tallies: 9 walk, 7 come by bus, 5 by bike and 3 by car — 24 answers in all. `decide` makes Lean count every category itself. -/ theorem class_tallies : tally .walk answers = 9 ∧ tally .bus answers = 7 ∧ tally .bike answers = 5 ∧ tally .car answers = 3 ∧ answers.length = 24 := by decidetotals_add_uptheoremProved here from basic factspropext, Quot.sound
totals_add_upLean 4Proved here from basic factsFormally verified /-- **The totals add up.** For *every* list of answers, the four tallies add up to the number of answers. Proof by induction on the list: one more answer adds one mark to the tally of its own category and none to the others, so both sides grow by one. -/ theorem totals_add_up (l : List Way) : tally .walk l + tally .bus l + tally .bike l + tally .car l = l.length := by induction l with | nil => rfl | cons w rest ih => unfold tally at ih ⊢ -- Look at which category the new answer `w` is in; `simp` uses `List.count_cons`. cases w <;> simp <;> omegawalk_is_mosttheoremProved here from basic factsNo axioms
walk_is_mostLean 4Proved here from basic factsFormally verified /-- Walking is the most common answer: no category has a bigger tally. We check each of the four categories in turn. -/ theorem walk_is_most : ∀ w : Way, tally w answers ≤ tally .walk answers := by intro w cases w <;> decidehow_many_moretheoremProved here from basic factsNo axioms
how_many_moreLean 4Proved here from basic factsFormally verified /-- "How many more children walk than come by bus?" Two more: `9 − 7 = 2`. -/ theorem how_many_more : tally .walk answers - tally .bus answers = 2 := by decidegatesdefDefinitionNo axioms
gatesLean 4Formally verified /-- The number of complete groups of five ("gates") in a tally of `n` marks. -/ def gates (n : Nat) : Nat := n / 5loosedefDefinitionNo axioms
looseLean 4Formally verified /-- The marks left over after the complete groups of five. -/ def loose (n : Nat) : Nat := n % 5tally_in_fivestheoremApplies a library theorempropext
tally_in_fivesLean 4Applies a library theoremFormally verified /-- Counting a tally by fives, then counting on the loose marks, always gives the number of marks: `5 × gates + loose = n`. This applies core Lean's `Nat.div_add_mod`. -/ theorem tally_in_fives (n : Nat) : 5 * gates n + loose n = n := Nat.div_add_mod n 5nine_in_fivestheoremProved here from basic factsNo axioms
nine_in_fivesLean 4Proved here from basic factsFormally verified /-- A tally of 9 is one gate of five and four loose marks. -/ theorem nine_in_fives : gates 9 = 1 ∧ loose 9 = 4 := ⟨rfl, rfl⟩FruitstructureDefinitionNo axioms
FruitLean 4Formally verified /-- Fruits for the question "Which fruits do you like?", where children may choose several. -/ inductive Fruit where | mango | banana | papaya deriving DecidableEq, ReprlikesdefDefinitionNo axioms
likesLean 4Formally verified /-- Four children's answers. The second and fourth child each chose two fruits. -/ def likes : List (List Fruit) := [[.mango], [.banana, .mango], [.papaya], [.mango, .papaya]]likersdefDefinitionNo axioms
likersLean 4Formally verified /-- How many children chose fruit `f`. -/ def likers (f : Fruit) (l : List (List Fruit)) : Nat := l.countP (fun choices => choices.contains f)likes_overcounttheoremProved here from basic factsNo axioms
likes_overcountLean 4Proved here from basic factsFormally verified /-- With several answers allowed, the totals no longer add up to the number of children: the three totals are 3, 1 and 2, which make 6, but only 4 children were asked. -/ theorem likes_overcount : likers .mango likes + likers .banana likes + likers .papaya likes = 6 ∧ likes.length = 4 := by decide