Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 483a6c2

Browse files
minchaowurobertylewis
authored andcommitted
feat(data/list/min_max): add minimum (#892)
1 parent 891c050 commit 483a6c2

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

src/data/list/min_max.lean

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,40 @@ variables {α : Type u} [inhabited α] [decidable_linear_order α]
1111

1212
@[simp] def maximum (l : list α) : α := l.foldl max l.head
1313

14+
@[simp] def minimum (l : list α) : α := l.foldl min l.head
15+
1416
def maximum_aux (l : list α) : α := l.foldr max l.head
1517

18+
def minimum_aux (l : list α) : α := l.foldr min l.head
19+
1620
@[simp] def maximum_singleton {a : α} : maximum [a] = a := by simp
1721

22+
@[simp] def minimum_singleton {a : α} : minimum [a] = a := by simp
23+
1824
theorem le_of_foldr_max : Π {a b : α} {l}, a ∈ l → a ≤ foldr max b l
1925
| a b [] h := absurd h $ not_mem_nil _
2026
| a b (hd::tl) h :=
2127
begin
2228
cases h,
23-
{ simp [h, le_max_left] },
29+
{ simp [h, le_refl] },
2430
{ simp [le_max_right_of_le, le_of_foldr_max h] }
2531
end
2632

33+
theorem le_of_foldr_min : Π {a b : α} {l}, a ∈ l → foldr min b l ≤ a
34+
| a b [] h := absurd h $ not_mem_nil _
35+
| a b (hd::tl) h :=
36+
begin
37+
cases h,
38+
{ simp [h, le_refl] },
39+
{ simp [min_le_left_of_le, le_of_foldr_min h] }
40+
end
41+
2742
theorem le_of_foldl_max {a b : α} {l} (h : a ∈ l) : a ≤ foldl max b l :=
2843
by { rw foldl_eq_foldr max_comm max_assoc, apply le_of_foldr_max h }
2944

45+
theorem le_of_foldl_min {a b : α} {l} (h : a ∈ l) : foldl min b l ≤ a :=
46+
by { rw foldl_eq_foldr min_comm min_assoc, apply le_of_foldr_min h }
47+
3048
theorem mem_foldr_max : Π {a : α} {l}, foldr max a l ∈ a :: l
3149
| a [] := by simp
3250
| a (hd::tl) :=
@@ -39,9 +57,24 @@ begin
3957
cases hmem, { simp [hmem] }, { right, right, exact hmem } }
4058
end
4159

60+
theorem mem_foldr_min : Π {a : α} {l}, foldr min a l ∈ a :: l
61+
| a [] := by simp
62+
| a (hd::tl) :=
63+
begin
64+
simp only [foldr_cons],
65+
cases (@min_choice _ _ hd (foldr min a tl)),
66+
{ simp [h] },
67+
{ rw h,
68+
have hmem := @mem_foldr_min a tl,
69+
cases hmem, { simp [hmem] }, { right, right, exact hmem } }
70+
end
71+
4272
theorem mem_foldl_max {a : α} {l} : foldl max a l ∈ a :: l :=
4373
by { rw foldl_eq_foldr max_comm max_assoc, apply mem_foldr_max }
4474

75+
theorem mem_foldl_min {a : α} {l} : foldl min a l ∈ a :: l :=
76+
by { rw foldl_eq_foldr min_comm min_assoc, apply mem_foldr_min }
77+
4578
theorem mem_maximum_aux : Π {l : list α}, l ≠ [] → maximum_aux l ∈ l
4679
| [] h := by contradiction
4780
| (hd::tl) h :=
@@ -51,9 +84,21 @@ begin
5184
cases hc, { simp [hc] }, { simp [hc, mem_foldr_max] }
5285
end
5386

87+
theorem mem_minimum_aux : Π {l : list α}, l ≠ [] → minimum_aux l ∈ l
88+
| [] h := by contradiction
89+
| (hd::tl) h :=
90+
begin
91+
dsimp [minimum_aux],
92+
have hc := @min_choice _ _ hd (foldr min hd tl),
93+
cases hc, { simp [hc] }, { simp [hc, mem_foldr_min] }
94+
end
95+
5496
theorem mem_maximum {l : list α} (h : l ≠ []) : maximum l ∈ l :=
5597
by { dsimp, rw foldl_eq_foldr max_comm max_assoc, apply mem_maximum_aux h }
5698

99+
theorem mem_minimum {l : list α} (h : l ≠ []) : minimum l ∈ l :=
100+
by { dsimp, rw foldl_eq_foldr min_comm min_assoc, apply mem_minimum_aux h }
101+
57102
theorem le_maximum_aux_of_mem : Π {a : α} {l}, a ∈ l → a ≤ maximum_aux l
58103
| a [] h := absurd h $ not_mem_nil _
59104
| a (hd::tl) h :=
@@ -63,9 +108,21 @@ begin
63108
{ dsimp [maximum_aux], apply le_max_right_of_le, apply le_of_foldr_max h }
64109
end
65110

111+
theorem le_minimum_aux_of_mem : Π {a : α} {l}, a ∈ l → minimum_aux l ≤ a
112+
| a [] h := absurd h $ not_mem_nil _
113+
| a (hd::tl) h :=
114+
begin
115+
cases h,
116+
{ rw h, apply le_of_foldr_min, simp },
117+
{ dsimp [minimum_aux], apply min_le_right_of_le, apply le_of_foldr_min h }
118+
end
119+
66120
theorem le_maximum_of_mem {a : α} {l} (h : a ∈ l) : a ≤ maximum l :=
67121
by { dsimp, rw foldl_eq_foldr max_comm max_assoc, apply le_maximum_aux_of_mem h }
68122

123+
theorem le_minimum_of_mem {a : α} {l} (h : a ∈ l) : minimum l ≤ a :=
124+
by { dsimp, rw foldl_eq_foldr min_comm min_assoc, apply le_minimum_aux_of_mem h }
125+
69126
def maximum_aux_cons : Π {a : α} {l}, l ≠ [] → maximum_aux (a :: l) = max a (maximum_aux l)
70127
| a [] h := by contradiction
71128
| a (hd::tl) h :=
@@ -82,13 +139,38 @@ begin
82139
{ simp [hc, le_maximum_aux_of_mem, mem_maximum_aux h] } }
83140
end
84141

142+
def minimum_aux_cons : Π {a : α} {l}, l ≠ [] → minimum_aux (a :: l) = min a (minimum_aux l)
143+
| a [] h := by contradiction
144+
| a (hd::tl) h :=
145+
begin
146+
apply le_antisymm,
147+
{ have hc := @min_choice _ _ a (minimum_aux $ hd :: tl),
148+
cases hc,
149+
{ simp [hc, le_minimum_aux_of_mem] },
150+
{ simp [hc, le_minimum_aux_of_mem, mem_minimum_aux h] } },
151+
{ have : a :: hd :: tl ≠ [], { simp [h] },
152+
have hle := mem_minimum_aux this,
153+
cases hle,
154+
{ simp [hle, min_le_left] },
155+
{ apply min_le_right_of_le, apply le_minimum_aux_of_mem, exact hle } }
156+
end
157+
85158
def maximum_cons {a : α} {l} (h : l ≠ []) : maximum (a :: l) = max a (maximum l) :=
86159
begin
87160
dsimp only [maximum],
88-
repeat {rw foldl_eq_foldr max_comm max_assoc},
161+
repeat { rw foldl_eq_foldr max_comm max_assoc },
89162
have := maximum_aux_cons h,
90163
dsimp only [maximum_aux] at this,
91164
exact this
92165
end
93166

167+
def minimum_cons {a : α} {l} (h : l ≠ []) : minimum (a :: l) = min a (minimum l) :=
168+
begin
169+
dsimp only [minimum],
170+
repeat { rw foldl_eq_foldr min_comm min_assoc },
171+
have := minimum_aux_cons h,
172+
dsimp only [minimum_aux] at this,
173+
exact this
174+
end
175+
94176
end list

0 commit comments

Comments
 (0)