|
| 1 | +/- |
| 2 | +Copyright (c) 2019 Minchao Wu. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Author: Minchao Wu |
| 5 | +-/ |
| 6 | +import data.list algebra.order_functions |
| 7 | + |
| 8 | +namespace list |
| 9 | +universes u |
| 10 | +variables {α : Type u} [inhabited α] [decidable_linear_order α] |
| 11 | + |
| 12 | +@[simp] def maximum (l : list α) : α := l.foldl max l.head |
| 13 | + |
| 14 | +def maximum_aux (l : list α) : α := l.foldr max l.head |
| 15 | + |
| 16 | +@[simp] def maximum_singleton {a : α} : maximum [a] = a := by simp |
| 17 | + |
| 18 | +theorem le_of_foldr_max : Π {a b : α} {l}, a ∈ l → a ≤ foldr max b l |
| 19 | +| a b [] h := absurd h $ not_mem_nil _ |
| 20 | +| a b (hd::tl) h := |
| 21 | +begin |
| 22 | + cases h, |
| 23 | + { simp [h, le_max_left] }, |
| 24 | + { simp [le_max_right_of_le, le_of_foldr_max h] } |
| 25 | +end |
| 26 | + |
| 27 | +theorem le_of_foldl_max {a b : α} {l} (h : a ∈ l) : a ≤ foldl max b l := |
| 28 | +by { rw foldl_eq_foldr max_comm max_assoc, apply le_of_foldr_max h } |
| 29 | + |
| 30 | +theorem mem_foldr_max : Π {a : α} {l}, foldr max a l ∈ a :: l |
| 31 | +| a [] := by simp |
| 32 | +| a (hd::tl) := |
| 33 | +begin |
| 34 | + simp only [foldr_cons], |
| 35 | + cases (@max_choice _ _ hd (foldr max a tl)), |
| 36 | + { simp [h] }, |
| 37 | + { rw h, |
| 38 | + have hmem := @mem_foldr_max a tl, |
| 39 | + cases hmem, { simp [hmem] }, { right, right, exact hmem } } |
| 40 | +end |
| 41 | + |
| 42 | +theorem mem_foldl_max {a : α} {l} : foldl max a l ∈ a :: l := |
| 43 | +by { rw foldl_eq_foldr max_comm max_assoc, apply mem_foldr_max } |
| 44 | + |
| 45 | +theorem mem_maximum_aux : Π {l : list α}, l ≠ [] → maximum_aux l ∈ l |
| 46 | +| [] h := by contradiction |
| 47 | +| (hd::tl) h := |
| 48 | +begin |
| 49 | + dsimp [maximum_aux], |
| 50 | + have hc := @max_choice _ _ hd (foldr max hd tl), |
| 51 | + cases hc, { simp [hc] }, { simp [hc, mem_foldr_max] } |
| 52 | +end |
| 53 | + |
| 54 | +theorem mem_maximum {l : list α} (h : l ≠ []) : maximum l ∈ l := |
| 55 | +by { dsimp, rw foldl_eq_foldr max_comm max_assoc, apply mem_maximum_aux h } |
| 56 | + |
| 57 | +theorem le_maximum_aux_of_mem : Π {a : α} {l}, a ∈ l → a ≤ maximum_aux l |
| 58 | +| a [] h := absurd h $ not_mem_nil _ |
| 59 | +| a (hd::tl) h := |
| 60 | +begin |
| 61 | + cases h, |
| 62 | + { rw h, apply le_of_foldr_max, simp }, |
| 63 | + { dsimp [maximum_aux], apply le_max_right_of_le, apply le_of_foldr_max h } |
| 64 | +end |
| 65 | + |
| 66 | +theorem le_maximum_of_mem {a : α} {l} (h : a ∈ l) : a ≤ maximum l := |
| 67 | +by { dsimp, rw foldl_eq_foldr max_comm max_assoc, apply le_maximum_aux_of_mem h } |
| 68 | + |
| 69 | +def maximum_aux_cons : Π {a : α} {l}, l ≠ [] → maximum_aux (a :: l) = max a (maximum_aux l) |
| 70 | +| a [] h := by contradiction |
| 71 | +| a (hd::tl) h := |
| 72 | +begin |
| 73 | + apply le_antisymm, |
| 74 | + { have : a :: hd :: tl ≠ [], { simp [h] }, |
| 75 | + have hle := mem_maximum_aux this, |
| 76 | + cases hle, |
| 77 | + { simp [hle, le_max_left] }, |
| 78 | + { apply le_max_right_of_le, apply le_maximum_aux_of_mem, exact hle } }, |
| 79 | + { have hc := @max_choice _ _ a (maximum_aux $ hd :: tl), |
| 80 | + cases hc, |
| 81 | + { simp [hc, le_maximum_aux_of_mem] }, |
| 82 | + { simp [hc, le_maximum_aux_of_mem, mem_maximum_aux h] } } |
| 83 | +end |
| 84 | + |
| 85 | +def maximum_cons {a : α} {l} (h : l ≠ []) : maximum (a :: l) = max a (maximum l) := |
| 86 | +begin |
| 87 | + dsimp only [maximum], |
| 88 | + repeat {rw foldl_eq_foldr max_comm max_assoc}, |
| 89 | + have := maximum_aux_cons h, |
| 90 | + dsimp only [maximum_aux] at this, |
| 91 | + exact this |
| 92 | +end |
| 93 | + |
| 94 | +end list |
0 commit comments