@@ -66,73 +66,44 @@ This represents vote sequences where candidate `+1` receives `p` votes and candi
66
66
def counted_sequence (p q : ℕ) : set (list ℤ) :=
67
67
{l | l.count 1 = p ∧ l.count (-1 ) = q ∧ ∀ x ∈ l, x = (1 : ℤ) ∨ x = -1 }
68
68
69
- @[simp] lemma counted_right_zero (p : ℕ) : counted_sequence p 0 = {list.repeat 1 p} :=
69
+ /-- An alternative definition of `counted_sequence` that uses `list.perm`. -/
70
+ lemma mem_counted_sequence_iff_perm {p q l} :
71
+ l ∈ counted_sequence p q ↔ l ~ list.repeat (1 : ℤ) p ++ list.repeat (-1 ) q :=
70
72
begin
71
- ext l,
72
- rw [counted_sequence, mem_singleton_iff],
73
- split,
74
- { rintro ⟨hl₀, hl₁, hl₂⟩,
75
- rw list.eq_repeat,
76
- have : ∀ x ∈ l, (1 : ℤ) = x,
77
- { intros x hx,
78
- obtain rfl | rfl := hl₂ x hx,
79
- { refl },
80
- { exact false.elim (list.not_mem_of_count_eq_zero hl₁ hx) } },
81
- split,
82
- { rwa ← list.count_eq_length.2 this },
83
- { exact λ x hx, (this x hx).symm } },
84
- { rintro rfl,
85
- simp only [mem_set_of_eq, list.count_repeat_self, eq_self_iff_true, true_and],
86
- refine ⟨list.count_eq_zero_of_not_mem _, λ x, _⟩; rw list.mem_repeat,
87
- { norm_num },
88
- { rintro ⟨-, rfl⟩,
89
- exact or.inl rfl } }
73
+ rw [list.perm_repeat_append_repeat],
74
+ { simp only [counted_sequence, list.subset_def, mem_set_of_eq, list.mem_cons_iff,
75
+ list.mem_singleton] },
76
+ { norm_num1 }
90
77
end
91
78
79
+ @[simp] lemma counted_right_zero (p : ℕ) : counted_sequence p 0 = {list.repeat 1 p} :=
80
+ by { ext l, simp [mem_counted_sequence_iff_perm] }
81
+
92
82
@[simp] lemma counted_left_zero (q : ℕ) : counted_sequence 0 q = {list.repeat (-1 ) q} :=
93
- begin
94
- ext l,
95
- rw [counted_sequence, mem_singleton_iff],
96
- split,
97
- { rintro ⟨hl₀, hl₁, hl₂⟩,
98
- rw list.eq_repeat,
99
- have : ∀ x ∈ l, (-1 : ℤ) = x,
100
- { intros x hx,
101
- obtain rfl | rfl := hl₂ x hx,
102
- { exact false.elim (list.not_mem_of_count_eq_zero hl₀ hx) },
103
- { refl } },
104
- split,
105
- { rwa ← list.count_eq_length.2 this },
106
- { exact λ x hx, (this x hx).symm } },
107
- { rintro rfl,
108
- simp only [mem_set_of_eq, list.count_repeat_self, eq_self_iff_true, true_and],
109
- refine ⟨list.count_eq_zero_of_not_mem _, λ x, _⟩; rw list.mem_repeat,
110
- { norm_num },
111
- { rintro ⟨-, rfl⟩,
112
- exact or.inr rfl } }
113
- end
83
+ by { ext l, simp [mem_counted_sequence_iff_perm] }
84
+
85
+ lemma mem_of_mem_counted_sequence {p q} {l} (hl : l ∈ counted_sequence p q) {x : ℤ} (hx : x ∈ l) :
86
+ x = 1 ∨ x = -1 :=
87
+ hl.2 .2 x hx
88
+
89
+ lemma length_of_mem_counted_sequence {p q} {l : list ℤ} (hl : l ∈ counted_sequence p q) :
90
+ l.length = p + q :=
91
+ by simp [(mem_counted_sequence_iff_perm.1 hl).length_eq]
92
+
93
+ lemma counted_eq_nil_iff {p q : ℕ} {l : list ℤ} (hl : l ∈ counted_sequence p q) :
94
+ l = [] ↔ p = 0 ∧ q = 0 :=
95
+ list.length_eq_zero.symm.trans $ by simp [length_of_mem_counted_sequence hl]
114
96
115
97
lemma counted_ne_nil_left {p q : ℕ} (hp : p ≠ 0 ) {l : list ℤ} (hl : l ∈ counted_sequence p q) :
116
- l ≠ list.nil :=
117
- begin
118
- obtain ⟨hl₀, hl₁, hl₂⟩ := hl,
119
- rintro rfl,
120
- rw list.count_nil at hl₀,
121
- exact hp hl₀.symm,
122
- end
98
+ l ≠ [] :=
99
+ by simp [counted_eq_nil_iff hl, hp]
123
100
124
- lemma counted_ne_nil_right {p q : ℕ} (hp : q ≠ 0 ) {l : list ℤ} (hl : l ∈ counted_sequence p q) :
125
- l ≠ list.nil :=
126
- begin
127
- obtain ⟨hl₀, hl₁, hl₂⟩ := hl,
128
- rintro rfl,
129
- rw list.count_nil at hl₁,
130
- exact hp hl₁.symm,
131
- end
101
+ lemma counted_ne_nil_right {p q : ℕ} (hq : q ≠ 0 ) {l : list ℤ} (hl : l ∈ counted_sequence p q) :
102
+ l ≠ [] :=
103
+ by simp [counted_eq_nil_iff hl, hq]
132
104
133
105
lemma counted_succ_succ (p q : ℕ) : counted_sequence (p + 1 ) (q + 1 ) =
134
- (counted_sequence p (q + 1 )).image (list.cons 1 ) ∪
135
- (counted_sequence (p + 1 ) q).image (list.cons (-1 )) :=
106
+ list.cons 1 '' counted_sequence p (q + 1 ) ∪ list.cons (-1 ) '' counted_sequence (p + 1 ) q :=
136
107
begin
137
108
ext l,
138
109
rw [counted_sequence, counted_sequence, counted_sequence],
@@ -193,79 +164,13 @@ lemma counted_sequence_nonempty : ∀ (p q : ℕ), (counted_sequence p q).nonemp
193
164
exact or.inl (counted_sequence_nonempty _ _),
194
165
end
195
166
196
- lemma sum_of_mem_counted_sequence :
197
- ∀ {p q : ℕ} {l : list ℤ} (hl : l ∈ counted_sequence p q), l.sum = p - q
198
- | 0 q l hl :=
199
- begin
200
- rw [counted_left_zero, mem_singleton_iff] at hl,
201
- simp [hl],
202
- end
203
- | p 0 l hl :=
204
- begin
205
- rw [counted_right_zero, mem_singleton_iff] at hl,
206
- simp [hl],
207
- end
208
- | (p + 1 ) (q + 1 ) l hl :=
209
- begin
210
- simp only [counted_succ_succ, mem_union, mem_image] at hl,
211
- rcases hl with (⟨l, hl, rfl⟩ | ⟨l, hl, rfl⟩),
212
- { rw [list.sum_cons, sum_of_mem_counted_sequence hl],
213
- push_cast,
214
- ring },
215
- { rw [list.sum_cons, sum_of_mem_counted_sequence hl],
216
- push_cast,
217
- ring }
218
- end
219
-
220
- lemma mem_of_mem_counted_sequence :
221
- ∀ {p q : ℕ} {l} (hl : l ∈ counted_sequence p q) {x : ℤ} (hx : x ∈ l), x = 1 ∨ x = -1
222
- | 0 q l hl x hx :=
223
- begin
224
- rw [counted_left_zero, mem_singleton_iff] at hl,
225
- subst hl,
226
- exact or.inr (list.eq_of_mem_repeat hx),
227
- end
228
- | p 0 l hl x hx :=
229
- begin
230
- rw [counted_right_zero, mem_singleton_iff] at hl,
231
- subst hl,
232
- exact or.inl (list.eq_of_mem_repeat hx),
233
- end
234
- | (p + 1 ) (q + 1 ) l hl x hx :=
235
- begin
236
- simp only [counted_succ_succ, mem_union, mem_image] at hl,
237
- rcases hl with (⟨l, hl, rfl⟩ | ⟨l, hl, rfl⟩);
238
- rcases hx with (rfl | hx),
239
- { left, refl },
240
- { exact mem_of_mem_counted_sequence hl hx },
241
- { right, refl },
242
- { exact mem_of_mem_counted_sequence hl hx },
243
- end
244
-
245
- lemma length_of_mem_counted_sequence :
246
- ∀ {p q : ℕ} {l : list ℤ} (hl : l ∈ counted_sequence p q), l.length = p + q
247
- | 0 q l hl :=
248
- begin
249
- rw [counted_left_zero, mem_singleton_iff] at hl,
250
- simp [hl],
251
- end
252
- | p 0 l hl :=
253
- begin
254
- rw [counted_right_zero, mem_singleton_iff] at hl,
255
- simp [hl],
256
- end
257
- | (p + 1 ) (q + 1 ) l hl :=
258
- begin
259
- simp only [counted_succ_succ, mem_union, mem_image] at hl,
260
- rcases hl with (⟨l, hl, rfl⟩ | ⟨l, hl, rfl⟩),
261
- { rw [list.length_cons, length_of_mem_counted_sequence hl, add_right_comm] },
262
- { rw [list.length_cons, length_of_mem_counted_sequence hl, ←add_assoc] }
263
- end
167
+ lemma sum_of_mem_counted_sequence {p q} {l : list ℤ} (hl : l ∈ counted_sequence p q) :
168
+ l.sum = p - q :=
169
+ by simp [(mem_counted_sequence_iff_perm.1 hl).sum_eq, sub_eq_add_neg]
264
170
265
171
lemma disjoint_bits (p q : ℕ) :
266
- disjoint
267
- ((counted_sequence p (q + 1 )).image (list.cons 1 ))
268
- ((counted_sequence (p + 1 ) q).image (list.cons (-1 ))) :=
172
+ disjoint (list.cons 1 '' counted_sequence p (q + 1 ))
173
+ (list.cons (-1 ) '' counted_sequence (p + 1 ) q) :=
269
174
begin
270
175
simp_rw [disjoint_left, mem_image, not_exists, exists_imp_distrib],
271
176
rintros _ _ ⟨_, rfl⟩ _ ⟨_, _, _⟩,
0 commit comments