@@ -11,22 +11,40 @@ variables {α : Type u} [inhabited α] [decidable_linear_order α]
11
11
12
12
@[simp] def maximum (l : list α) : α := l.foldl max l.head
13
13
14
+ @[simp] def minimum (l : list α) : α := l.foldl min l.head
15
+
14
16
def maximum_aux (l : list α) : α := l.foldr max l.head
15
17
18
+ def minimum_aux (l : list α) : α := l.foldr min l.head
19
+
16
20
@[simp] def maximum_singleton {a : α} : maximum [a] = a := by simp
17
21
22
+ @[simp] def minimum_singleton {a : α} : minimum [a] = a := by simp
23
+
18
24
theorem le_of_foldr_max : Π {a b : α} {l}, a ∈ l → a ≤ foldr max b l
19
25
| a b [] h := absurd h $ not_mem_nil _
20
26
| a b (hd::tl) h :=
21
27
begin
22
28
cases h,
23
- { simp [h, le_max_left ] },
29
+ { simp [h, le_refl ] },
24
30
{ simp [le_max_right_of_le, le_of_foldr_max h] }
25
31
end
26
32
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
+
27
42
theorem le_of_foldl_max {a b : α} {l} (h : a ∈ l) : a ≤ foldl max b l :=
28
43
by { rw foldl_eq_foldr max_comm max_assoc, apply le_of_foldr_max h }
29
44
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
+
30
48
theorem mem_foldr_max : Π {a : α} {l}, foldr max a l ∈ a :: l
31
49
| a [] := by simp
32
50
| a (hd::tl) :=
39
57
cases hmem, { simp [hmem] }, { right, right, exact hmem } }
40
58
end
41
59
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
+
42
72
theorem mem_foldl_max {a : α} {l} : foldl max a l ∈ a :: l :=
43
73
by { rw foldl_eq_foldr max_comm max_assoc, apply mem_foldr_max }
44
74
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
+
45
78
theorem mem_maximum_aux : Π {l : list α}, l ≠ [] → maximum_aux l ∈ l
46
79
| [] h := by contradiction
47
80
| (hd::tl) h :=
51
84
cases hc, { simp [hc] }, { simp [hc, mem_foldr_max] }
52
85
end
53
86
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
+
54
96
theorem mem_maximum {l : list α} (h : l ≠ []) : maximum l ∈ l :=
55
97
by { dsimp, rw foldl_eq_foldr max_comm max_assoc, apply mem_maximum_aux h }
56
98
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
+
57
102
theorem le_maximum_aux_of_mem : Π {a : α} {l}, a ∈ l → a ≤ maximum_aux l
58
103
| a [] h := absurd h $ not_mem_nil _
59
104
| a (hd::tl) h :=
@@ -63,9 +108,21 @@ begin
63
108
{ dsimp [maximum_aux], apply le_max_right_of_le, apply le_of_foldr_max h }
64
109
end
65
110
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
+
66
120
theorem le_maximum_of_mem {a : α} {l} (h : a ∈ l) : a ≤ maximum l :=
67
121
by { dsimp, rw foldl_eq_foldr max_comm max_assoc, apply le_maximum_aux_of_mem h }
68
122
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
+
69
126
def maximum_aux_cons : Π {a : α} {l}, l ≠ [] → maximum_aux (a :: l) = max a (maximum_aux l)
70
127
| a [] h := by contradiction
71
128
| a (hd::tl) h :=
@@ -82,13 +139,38 @@ begin
82
139
{ simp [hc, le_maximum_aux_of_mem, mem_maximum_aux h] } }
83
140
end
84
141
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
+
85
158
def maximum_cons {a : α} {l} (h : l ≠ []) : maximum (a :: l) = max a (maximum l) :=
86
159
begin
87
160
dsimp only [maximum],
88
- repeat {rw foldl_eq_foldr max_comm max_assoc},
161
+ repeat { rw foldl_eq_foldr max_comm max_assoc },
89
162
have := maximum_aux_cons h,
90
163
dsimp only [maximum_aux] at this ,
91
164
exact this
92
165
end
93
166
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
+
94
176
end list
0 commit comments