/
list_utils.lean
228 lines (192 loc) · 5.31 KB
/
list_utils.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import tactic
meta def in_list_explicit : tactic unit := `[
tactic.repeat `[
tactic.try `[apply list.mem_cons_self],
tactic.try `[apply list.mem_cons_of_mem]
]
]
meta def split_ile : tactic unit := `[
split,
{
in_list_explicit,
}
]
namespace list
variables {α β : Type*} {x y z : list α}
section list_append_append
lemma length_append_append :
list.length (x ++ y ++ z) = x.length + y.length + z.length :=
by rw [list.length_append, list.length_append]
lemma map_append_append {f : α → β} :
list.map f (x ++ y ++ z) = list.map f x ++ list.map f y ++ list.map f z :=
by rw [list.map_append, list.map_append]
lemma filter_map_append_append {f : α → option β} :
list.filter_map f (x ++ y ++ z) = list.filter_map f x ++ list.filter_map f y ++ list.filter_map f z :=
by rw [list.filter_map_append, list.filter_map_append]
lemma reverse_append_append :
list.reverse (x ++ y ++ z) = z.reverse ++ y.reverse ++ x.reverse :=
by rw [list.reverse_append, list.reverse_append, list.append_assoc]
lemma forall_mem_append_append {p : α → Prop} :
(∀ a ∈ x ++ y ++ z, p a) ↔ (∀ a ∈ x, p a) ∧ (∀ a ∈ y, p a) ∧ (∀ a ∈ z, p a) :=
by rw [list.forall_mem_append, list.forall_mem_append, and_assoc]
end list_append_append
section list_repeat
lemma repeat_zero (s : α) :
list.repeat s 0 = [] :=
rfl
lemma repeat_succ_eq_singleton_append (s : α) (n : ℕ) :
list.repeat s n.succ = [s] ++ list.repeat s n :=
rfl
lemma repeat_succ_eq_append_singleton (s : α) (n : ℕ) :
list.repeat s n.succ = list.repeat s n ++ [s] :=
begin
change list.repeat s (n + 1) = list.repeat s n ++ [s],
rw list.repeat_add,
refl,
end
end list_repeat
section list_join
lemma join_singleton : [x].join = x :=
by rw [list.join, list.join, list.append_nil]
-- proved in `https://github.com/user7230724/lean-projects/blob/master/src/list_take_join/main.lean#L117`
lemma take_join_of_lt {L : list (list α)} {n : ℕ} (notall : n < L.join.length) :
∃ m k : ℕ, ∃ mlt : m < L.length, k < (L.nth_le m mlt).length ∧
L.join.take n = (L.take m).join ++ (L.nth_le m mlt).take k :=
begin
sorry
end
-- hopefully similar to `take_join_of_lt`
lemma drop_join_of_lt {L : list (list α)} {n : ℕ} (notall : n < L.join.length) :
∃ m k : ℕ, ∃ mlt : m < L.length, k < (L.nth_le m mlt).length ∧
L.join.drop n = (L.nth_le m mlt).drop k ++ (L.drop m.succ).join :=
begin
sorry
end
-- should probably be added to mathlib
lemma append_join_append (L : list (list α)) :
x ++ (list.map (λ l, l ++ x) L).join = (list.map (λ l, x ++ l) L).join ++ x :=
begin
induction L,
{
rw [list.map_nil, list.join, list.append_nil, list.map_nil, list.join, list.nil_append],
},
{
rw [
list.map_cons, list.join, list.map_cons, list.join,
list.append_assoc, L_ih, list.append_assoc, list.append_assoc
],
},
end
-- should be added to mathlib
lemma reverse_join (L : list (list α)) :
L.join.reverse = (list.map list.reverse L).reverse.join :=
begin
induction L,
{
refl,
},
{
rw [list.join, list.reverse_append, L_ih, list.map_cons, list.reverse_cons, list.join_append, list.join_singleton],
},
end
def n_times (l : list α) (n : ℕ) : list α :=
(list.repeat l n).join
infix ` ^ ` : 100 := n_times
end list_join
section countin
variables [decidable_eq α]
def count_in (l : list α) (a : α) : ℕ :=
list.sum (list.map (λ s, ite (s = a) 1 0) l)
lemma count_in_repeat_eq (a : α) (n : ℕ) :
count_in (list.repeat a n) a = n :=
begin
unfold count_in,
induction n with m ih,
{
refl,
},
rw [list.repeat_succ, list.map_cons, list.sum_cons, ih],
rw if_pos rfl,
apply nat.one_add,
end
lemma count_in_repeat_neq {a : α} {b : α} (hyp : a ≠ b) (n : ℕ) :
count_in (list.repeat a n) b = 0 :=
begin
unfold count_in,
induction n with m ih,
{
refl,
},
rw [list.repeat_succ, list.map_cons, list.sum_cons, ih, add_zero],
rw ite_eq_right_iff,
intro impos,
exfalso,
exact hyp impos,
end
lemma count_in_append (a : α) :
count_in (x ++ y) a = count_in x a + count_in y a :=
begin
unfold count_in,
rw list.map_append,
rw list.sum_append,
end
lemma count_in_pos_of_in {a : α} (hyp : a ∈ x) :
count_in x a > 0 :=
begin
induction x with d l ih,
{
exfalso,
rw list.mem_nil_iff at hyp,
exact hyp,
},
by_contradiction contr,
rw not_lt at contr,
rw nat.le_zero_iff at contr,
rw list.mem_cons_eq at hyp,
unfold count_in at contr,
unfold list.map at contr,
simp at contr,
cases hyp,
{
exact contr.left hyp.symm,
},
specialize ih hyp,
have zero_in_tail : count_in l a = 0,
{
unfold count_in,
exact contr.right,
},
rw zero_in_tail at ih,
exact nat.lt_irrefl 0 ih,
end
lemma count_in_zero_of_notin {a : α} (hyp : a ∉ x) :
count_in x a = 0 :=
begin
induction x with d l ih,
{
refl,
},
unfold count_in,
rw [list.map_cons, list.sum_cons, add_eq_zero_iff, ite_eq_right_iff],
split,
{
simp only [nat.one_ne_zero],
exact (list.ne_of_not_mem_cons hyp).symm,
},
{
exact ih (list.not_mem_of_not_mem_cons hyp),
},
end
lemma count_in_join (L : list (list α)) (a : α) :
count_in L.join a = list.sum (list.map (λ w, count_in w a) L) :=
begin
induction L,
{
refl,
},
{
rw [list.join, count_in_append, list.map, list.sum_cons, L_ih],
},
end
end countin
end list