-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathballot.v
580 lines (520 loc) · 18.3 KB
/
ballot.v
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
(*
MIT License
Copyright (c) 2017 Jean-Marie Madiot, INRIA
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)
Require Import Arith.
Require Import ZArith.
Require Import String.
Require Import List.
Require Import Psatz.
Require Import Factorial.
(* Recurrence part of the inductive proof: *)
Lemma recurrence a b Afirst Bfirst all Afirst_ahead Bfirst_ahead ahead :
0 < b -> b < a ->
(a + b) * Afirst = a * all ->
(a + b) * Bfirst = b * all ->
Afirst + Bfirst = all ->
(a - 1 + b) * Afirst_ahead = (a - b - 1) * Afirst ->
(a - 1 + b) * Bfirst_ahead = (a - (b - 1)) * Bfirst ->
Afirst_ahead + Bfirst_ahead = ahead ->
(a + b) * ahead = (a - b) * all.
Proof.
destruct a. intros ? ?; exfalso; lia.
destruct b. inversion 1.
simpl (S a - 1).
simpl (S b - 1).
replace (S a - S b - 1) with (a - S b) by lia.
replace (b - 0) with b by lia.
replace (a - 0) with a by lia.
intros _ L HA HB <- IHA IHB <-.
apply Nat.mul_cancel_l with (p := (a + S b)). lia.
match goal with |- ?x * (?y * ?z) = _ => transitivity (y * (x * z)) end. ring.
rewrite Nat.mul_add_distr_l. rewrite IHA, IHB.
rewrite Nat.mul_add_distr_l.
match goal with
|- ?x * (?y * ?z) + ?x' * (?y' * ?z') = _ =>
transitivity (y * (x * z) + y' * (x' * z'))
end. ring.
rewrite HA, HB.
match goal with
|- ?x * (?y * ?z) + ?x' * (?y' * ?z) = _ =>
transitivity ((x * y + x' * y') * z)
end. ring.
rewrite Nat.mul_assoc.
f_equal.
replace (S a - S b) with (a - b) by lia.
replace (S a - b) with (1 + (a - b)) by lia.
replace (a - S b) with (a - b - 1) by lia.
replace a with (b + 1 + (a - b - 1)) by lia.
generalize (a - b - 1); intros c.
replace (b + 1 + c - b - 1) with c by lia.
replace (b + 1 + c - b) with (c + 1) by lia.
ring.
Qed.
Import ListNotations.
Fixpoint cartesian_product {A B} (xs : list A) (ys : list B) : list (A * B) :=
match xs with
| [] => []
| x :: xs => map (pair x) ys ++ cartesian_product xs ys
end.
Fixpoint picks {A} n (l : list A) : list (list A) :=
match n with
| O => [[]]
| S n => map (fun x => fst x :: snd x) (cartesian_product l (picks n l))
end.
Fixpoint pickbools n :=
match n with
| O => [[]]
| S n => map (cons true) (pickbools n) ++ map (cons false) (pickbools n)
end.
(** non empty suffixes *)
Fixpoint proper_suffixes {A} (l : list A) : list (list A) :=
match l with
| [] => []
| x :: l => (x :: l) :: proper_suffixes l
end.
(** For our notion of "throughout" we choose that lists represent the
last vote first, and hence we use suffixes *)
Definition throughout {A} f (l : list A) := forallb f (proper_suffixes l).
Definition countb b l := count_occ Bool.bool_dec l b.
Notation length := length.
Lemma countb_false l : countb false l = length l - countb true l.
Proof.
cut (countb false l + countb true l = length l). lia.
induction l as [| [|] l IHl]; simpl; lia.
Qed.
Definition winb votes := countb false votes <? countb true votes.
Definition sumtrue p l := countb true l =? p.
Lemma filter_app {A} f (l1 l2 : list A) :
filter f (l1 ++ l2) = filter f l1 ++ filter f l2.
Proof.
induction l1 as [| a l IHl]; simpl. reflexivity.
destruct (f a); simpl; congruence.
Qed.
Lemma map_filter {A B} f (g : A -> B) l :
filter f (map g l) = map g (filter (fun b => f (g b)) l).
Proof.
induction l; auto. simpl.
destruct (f (g a)); auto.
simpl. congruence.
Qed.
Lemma filter_filter {A} (f g : A -> bool) l :
filter f (filter g l) = filter (fun b => andb (g b) (f b)) l.
Proof.
induction l; auto. simpl. unfold andb.
destruct (g a); simpl; destruct (f a); simpl; auto.
rewrite IHl. auto.
Qed.
Lemma filter_ext {A} f g l : (forall x : A, f x = g x) -> filter f l = filter g l.
Proof.
intros E.
induction l; simpl; auto. rewrite E, IHl. auto.
Qed.
Lemma filter_sub {A} f g l : (forall x : A, f x = true -> g x = true) -> filter f l = filter g (filter f l).
Proof.
intros H.
induction l; simpl; auto. rewrite IHl. destruct (f a) eqn:E.
- simpl. rewrite H; auto. congruence.
- congruence.
Qed.
Fixpoint binomial (n k : nat) : nat :=
match n with
| 0 =>
match k with
| 0 => 1
| S _ => 0
end
| S n' =>
match k with
| 0 => 1
| S k' => (binomial n' k') + (binomial n' k)
end
end.
Lemma binomial_lt n p : n < p -> binomial n p = 0.
Proof.
revert p.
induction n; intros [ | p ] H.
- lia.
- reflexivity.
- lia.
- simpl. rewrite IHn. 2:lia. rewrite IHn; lia.
Qed.
Lemma binomial_0_r n : binomial n 0 = 1.
Proof.
destruct n; reflexivity.
Qed.
Lemma binomial_1_r n : binomial n 1 = n.
Proof.
induction n. reflexivity. simpl. rewrite binomial_0_r. lia.
Qed.
Lemma binomial_diag n : binomial n n = 1.
Proof.
induction n. reflexivity.
simpl. rewrite IHn. rewrite binomial_lt; lia.
Qed.
Lemma binomial_factorial n k : k <= n -> fact k * fact (n - k) * binomial n k = fact n.
Proof.
revert k; induction n; intros [ | k].
- reflexivity.
- lia.
- intros _. simpl. lia.
- intros L. simpl (binomial _ _).
rewrite Nat.mul_add_distr_l.
replace (fact (S k) * fact (S n - S k) * binomial n k)
with ((S k) * (fact k * fact (n - k) * binomial n k))
by (simpl; ring).
rewrite IHn. 2:lia.
assert (D : k = n \/ k < n) by lia. destruct D as [D | D].
+ subst k. rewrite binomial_lt; auto. simpl; ring.
+ replace (S n - S k) with (S (n - S k)) by lia.
replace (fact (S k) * fact (S (n - S k)) * binomial n (S k))
with (S (n - S k) * (fact (S k) * fact (n - S k) * binomial n (S k)))
by (simpl; ring).
rewrite IHn. 2:lia.
rewrite <-Nat.mul_add_distr_r.
change (fact (S n)) with (S n * fact n).
apply Nat.mul_cancel_r. apply fact_neq_0.
lia.
Qed.
Lemma binomial_complement n k : k <= n -> binomial n k = binomial n (n - k).
Proof.
intros L.
pose (x := fact k * fact (n - k)).
cut (x * binomial n k = x * binomial n (n - k)).
{ apply Nat.mul_cancel_l. subst x. apply Nat.neq_mul_0; split; apply fact_neq_0. }
subst x.
rewrite binomial_factorial; auto.
rewrite <-(binomial_factorial n (n - k)). 2:lia.
replace (n - (n - k)) with k by lia.
ring.
Qed.
Lemma binomial_S n k :
k <= n ->
S n * binomial n k = S k * binomial (S n) (S k).
Proof.
intros Hkn.
pose (x := fact k * fact (n - k)).
pose (y := fact (S k) * fact (S n - S k)).
cut (x * y * (S n * binomial n k) = x * y * (S k * binomial (S n) (S k))).
{ apply Nat.mul_cancel_l. subst x y. do 2 (apply Nat.neq_mul_0; split); apply fact_neq_0. }
subst x y.
cut ((fact (S k) * fact (S n - S k)) * (S n * (fact k * fact (n - k) * binomial n k)) =
fact k * fact (n - k) * (S k * ((fact (S k) * fact (S n - S k)) * binomial (S n) (S k)))).
{ intros E. etransitivity; [ | etransitivity ]; [ | apply E | ]; ring. }
rewrite binomial_factorial.
rewrite binomial_factorial.
simpl; ring.
lia.
auto.
Qed.
Lemma count_sumtrue_cons_true p l :
filter (sumtrue (S p)) (map (cons true) l) =
map (cons true) (filter (sumtrue p) l).
Proof.
induction l; auto. simpl.
change (sumtrue (S p) (true :: a)) with (sumtrue p a).
destruct (sumtrue p a); auto.
simpl. congruence.
Qed.
Lemma count_0_wins_cons_true l :
filter (sumtrue 0) (map (cons true) l) = [].
Proof.
induction l; auto.
Qed.
Lemma count_sumtrue_cons_false p l :
filter (sumtrue p) (map (cons false) l) =
map (cons false) (filter (sumtrue p) l).
Proof.
induction l; auto. simpl.
change (sumtrue p (false :: a)) with (sumtrue p a).
destruct (sumtrue p a); auto.
simpl. congruence.
Qed.
Lemma count_sumtrue p n :
length (filter (sumtrue p) (pickbools n)) = binomial n p.
Proof.
revert p.
induction n; intros p. simpl. destruct p; reflexivity.
simpl (pickbools _).
rewrite filter_app, app_length.
destruct p.
- replace (binomial (S n) 0) with (0 + binomial (S n) 0) by auto; f_equal.
+ clear. induction (pickbools n); auto.
+ rewrite count_sumtrue_cons_false, map_length.
rewrite IHn.
destruct n; auto.
- simpl.
f_equal; rewrite <-IHn.
all: rewrite map_filter, map_length; reflexivity.
Qed.
Lemma first_vote_split p q :
filter (sumtrue (1 + p)) (pickbools (1 + p + q)) =
map (cons true) (filter (sumtrue p) (pickbools (p + q))) ++
map (cons false) (filter (sumtrue (1 + p)) (pickbools (p + q))).
Proof.
(* true but easy *)
simpl (pickbools _).
rewrite filter_app.
rewrite map_filter.
rewrite map_filter.
reflexivity.
Qed.
Lemma pickbools_length n : pickbools n = filter (fun l => length l =? n) (pickbools n).
Proof.
induction n. reflexivity.
simpl. rewrite filter_app.
do 2 rewrite map_filter. f_equal; f_equal; f_equal.
all: etransitivity; [ apply IHn | reflexivity ].
Qed.
Lemma counting_wins p q :
q < p ->
filter winb (filter (sumtrue p) (pickbools (p + q))) =
filter (sumtrue p) (pickbools (p + q)).
Proof.
intros. rewrite pickbools_length.
repeat rewrite filter_filter. apply filter_ext.
intros x. unfold winb, sumtrue.
destruct (length x =? p + q) eqn:E. 2:reflexivity. simpl.
destruct (countb true x =? p) eqn:E2. 2:reflexivity. simpl.
rewrite countb_false.
rewrite Nat.ltb_lt.
rewrite Nat.eqb_eq in *.
lia.
Qed.
Lemma pickbools_wins_minus p q :
q <= p ->
(p - q) * length (filter (sumtrue p) (pickbools (p + q))) =
(p - q) * length (filter winb (filter (sumtrue p) (pickbools (p + q)))).
Proof.
intros L.
assert (D : q = p \/ q < p) by lia. destruct D as [D | D].
- replace (p - q) with 0 by lia. lia.
- clear L. f_equal. f_equal. rewrite counting_wins; auto.
Qed.
Lemma bertrand_ballot_bool_eq p q :
p <> 0 ->
p = q ->
let l := filter (fun votes => countb true votes =? p) (pickbools (p + q)) in
(p + q) * length (filter (throughout winb) l) =
(p - q) * length (filter winb l).
Proof.
intros L <-. replace (p - p) with 0 by lia. simpl.
match goal with |- ?a * ?b = 0 => cut (b = 0) end. intros ->; auto.
rewrite pickbools_length.
transitivity (length (filter (fun _ => false) (pickbools (p + p)))).
- f_equal. rewrite pickbools_length. repeat rewrite filter_filter.
apply filter_ext. intros x.
destruct (length x =? p + p) eqn:Hlen; auto. simpl.
destruct (countb true x =? p) eqn:Hp; auto. simpl.
rewrite Nat.eqb_eq in *.
destruct x. simpl in *. lia.
unfold throughout. simpl.
unfold winb. rewrite countb_false.
rewrite Hlen, Hp.
replace (p + p - p) with p by lia.
rewrite Nat.ltb_irrefl.
reflexivity.
- generalize (pickbools (p + p)). generalize (list bool). clear.
intros P l.
induction l; auto.
Qed.
Theorem bertrand_ballot_bool p q :
q <= p ->
let l := filter (sumtrue p) (pickbools (p + q)) in
(p + q) * length (filter (throughout winb) l) =
(p - q) * length (filter winb l).
Proof.
intros L l.
remember (p + q) as n.
revert p q L Heqn l.
induction n; intros p q L Heqn l.
(** the case n = 0 is easy *)
{ assert (p = 0) by lia. assert (q = 0) by lia. subst; simpl. reflexivity. }
(** the case p = q is easy *)
assert (D : q = p \/ q < p) by lia. destruct D as [D | D].
{ subst l. rewrite Heqn.
destruct p as [ | p ]. subst; reflexivity.
apply (bertrand_ballot_bool_eq (S p) q); auto. }
(** case p > q *)
destruct p as [ | p]; [ | destruct q as [ | q ] ].
- (** case p = 0: trivial because p > q *)
lia.
- (** case q = 0 *)
assert (n = p) by lia; subst n. f_equal.
subst l. clear. f_equal. rewrite pickbools_length.
repeat rewrite filter_filter. apply filter_ext. intros x.
destruct (length x =? S p) eqn:El. 2:reflexivity.
destruct (sumtrue (S p) x) eqn:Ep. 2:reflexivity. simpl.
unfold sumtrue in Ep.
rewrite Nat.eqb_eq in *.
assert (F : countb false x = 0).
{ rewrite countb_false. lia. }
assert (E : forall x, length x <> 0 -> countb false x = 0 -> winb x = true).
{ clear. intros. unfold winb. rewrite Nat.ltb_lt.
rewrite countb_false in *. lia. }
rewrite E; auto. 2:lia.
assert (E' : forall x, length x <> 0 -> countb false x = 0 -> throughout winb x = true).
{
unfold throughout.
clear -E. intros x L F. induction x as [| [ | ] l IHl].
- tauto.
- simpl.
rewrite E; try lia.
destruct l as [ | [ | ] l ].
+ reflexivity.
+ apply IHl. simpl. lia. auto.
+ discriminate.
- discriminate.
}
apply E'. lia. auto.
- (** case p, q > 0 *)
pose proof IHn (S p) q ltac:(lia) ltac:(lia) as Hsp.
pose proof IHn p (S q) ltac:(lia) ltac:(lia) as Hp.
clear IHn.
replace (filter winb l) with l; swap 1 2.
{ subst l. rewrite Heqn. rewrite counting_wins; auto. }
rewrite Heqn.
eapply recurrence; try lia; swap 1 3; swap 2 6.
{ subst l. rewrite Heqn. rewrite first_vote_split.
rewrite app_length. reflexivity. }
{ subst l. rewrite Heqn. rewrite first_vote_split.
rewrite filter_app. do 2 rewrite map_filter.
rewrite app_length. reflexivity. }
+ (* binomial things *)
subst l.
rewrite Heqn. clear.
rewrite map_length. do 2 rewrite count_sumtrue.
simpl (_ + _).
assert (L : p <= p + S q) by lia. revert L.
generalize (p + S q); intros n L.
apply binomial_S. auto.
+ (* throughout winnning, last vote being a true *)
injection Heqn as ->.
clear Hsp l.
simpl in Hp.
do 2 rewrite map_length.
replace (S p - 1 + S q) with (p + S q) by lia.
replace (S p - S q - 1) with (p - S q) by lia.
rewrite pickbools_wins_minus. 2:lia.
rewrite <-Hp. clear Hp.
f_equal.
f_equal.
repeat rewrite filter_filter.
apply filter_ext; intros x.
f_equal. unfold throughout. simpl.
destruct x. simpl. reflexivity.
simpl. rewrite Bool.andb_assoc. f_equal.
generalize (b :: x); clear; intros l.
unfold winb. simpl.
generalize (countb false l).
generalize (countb true l). clear.
intros a b.
destruct (b <? a) eqn:Ea, (b <? S a) eqn:Esa; auto.
rewrite Nat.ltb_lt in *.
rewrite Nat.ltb_nlt in *.
lia.
+ (* throughout winnning, last vote being a false *)
injection Heqn as ->. clear Hp l.
replace (S p - 1 + S q) with (p + S q) by lia.
replace (S p - (S q - 1)) with (S p - q) by lia.
do 2 rewrite map_length.
replace ((S p - q) * length (filter (sumtrue (1 + p)) (pickbools (p + S q))))
with ((S p - q) * length (filter winb (filter (sumtrue (1 + p)) (pickbools (p + S q))))); swap 1 2.
{ replace (p + S q) with (S p + q) by lia.
rewrite pickbools_wins_minus. 2:lia. auto. }
simpl in *.
rewrite <-Hsp.
f_equal.
f_equal.
rewrite pickbools_length.
repeat rewrite filter_filter.
apply filter_ext; intros x.
clear Hsp.
destruct (length x =? p + S q) eqn:Hlen. 2:reflexivity. simpl.
destruct (sumtrue (S p) x) eqn:Hp. 2:reflexivity. simpl.
unfold throughout. simpl.
rewrite Bool.andb_comm.
destruct (forallb winb (proper_suffixes x)) eqn:Hw. 2:reflexivity. simpl.
unfold winb.
unfold sumtrue in Hp.
repeat rewrite Nat.ltb_lt in *.
repeat rewrite Nat.eqb_eq in *.
simpl.
rewrite countb_false.
lia.
+ (* binomial things *)
subst l.
rewrite Heqn. clear.
rewrite map_length. do 2 rewrite count_sumtrue.
simpl (_ + _).
replace (S q) with (p + S q - p) at 3 by lia.
assert (L : p <= p + S q) by lia. revert L.
generalize (p + S q); intros n L.
assert (D : p = n \/ p < n) by lia. destruct D as [D | D].
* subst p. rewrite binomial_lt. rewrite binomial_diag. lia. lia.
* rewrite binomial_complement. 2:lia.
rewrite (binomial_complement (S n)). 2:lia.
rewrite (binomial_S n (n - S p)). 2:lia.
f_equal. lia.
f_equal. lia.
Qed.
Definition count_votes := count_occ string_dec.
Definition wins A B votes := count_votes votes B <? count_votes votes A.
Open Scope string_scope.
(** we enumerate all lists of votes with:
- p + q votes for (A or B)
- p votes for A *)
Theorem bertrand_ballot p q :
let l := filter (fun votes => count_votes votes "A" =? p)%nat (picks (p + q) ["A"; "B"]) in
p >= q ->
(p + q) * List.length (filter (throughout (wins "A" "B")) l) =
(p - q) * List.length (filter (wins "A" "B") l).
Proof.
intros l L; subst l.
pose (f := fun b : bool => if b then "A" else "B").
replace (picks (p + q) ["A"; "B"]) with (map (map f) (pickbools (p + q))); swap 1 2.
{
generalize (p + q). clear p q L. induction n. reflexivity.
simpl. do 2 rewrite map_app. f_equal. 2:rewrite app_nil_r.
all: rewrite <- IHn.
all: generalize (pickbools n); intros l; induction l; simpl; congruence.
}
repeat rewrite map_filter.
repeat rewrite map_length.
assert (A : forall b l, count_votes (map f l) (f b) = countb b l).
{ clear. intros b l; induction l; auto. simpl.
destruct a, b; simpl; auto. }
assert (E : forall l, wins "A" "B" (map f l) = winb l).
{ clear -A. unfold wins, winb. intros l. f_equal. apply (A false). apply (A true). }
etransitivity; [ | etransitivity ]; [ | apply (bertrand_ballot_bool p q); auto | ]; f_equal.
- f_equal.
repeat rewrite filter_filter.
apply filter_ext.
intros l. f_equal.
+ unfold sumtrue. f_equal.
induction l as [ | b l IHl]; auto. simpl. destruct b; auto. simpl. auto.
+ unfold throughout, proper_suffixes.
induction l as [ | b l IHl]; auto.
simpl. f_equal; auto.
rewrite <-E. auto.
- f_equal.
repeat rewrite filter_filter.
apply filter_ext.
intros l; f_equal. 2:now auto.
unfold sumtrue. f_equal. rewrite <-A. auto.
Qed.