-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalan.lean
2268 lines (2139 loc) · 74 KB
/
catalan.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import project.identities
import tactic.linarith
import tactic.tidy
import data.list
import data.list.basic
import data.int.basic
import tactic.ring
import data.nat.gcd
/- Collaboration between
Travis Hance (thance)
Katherine Cordwell (kcordwell)
-/
/-
Catalan numbers.
In this file we:
- Define `balanced`: balanced strings of parentheses
(`tt` is and open paren, `ff` is a close paren).
- Define `catalan`: the catalan numbers by recurrence
- Show that the set of balanced strings of length 2*n
is catalan n. (theorem `has_card_set_balanced`)
by induction, by showing that a balanced string of
length 2*(m+1) corresponds to a pair of balanced
strings whose lengths sum to 2*m.
- Define `below_diagonal_path`, a proposition which
indicates that a path of length (2n+1) goes from
(0,0) to (n,n+1) while never going above the
(0,0)--(n,n+1) diagonal.
- Show that balanced strings of length 2*n are in bijection
with below_diagonal_path strings of length 2*n+1.
(theorem `has_card_set_below_diagonal_path_catalan`)
- Take all 2*n+1 rotations of all below_diagonal_path
strings, and show that this gives *all* paths
from (0,0) to (n,n+1)
(theorem `theorem has_card_set_n_choose_k_catalan`)
- Finally, show that
catalan n = (choose (2*n+1) n) / (2*n + 1)
(theorem `catalan_identity`)
We define `catalan` by recurence, then show that `catalan n`
is the cardinality of the set of balanced strings of length 2n.
(theorem `has_card_set_balanced`)
Then by doing a bijection through paths from (0,0) to (n,n+1),
we prove our main result, the theorem
`catalan_identity`.
-/
/-
Define balanced strings of parentheses.
`tt` represents an open paren, `ff` represents a closed paren.
-/
def balanced_aux : (list bool) → (ℕ) → Prop
| [] 0 := true
| [] (d + 1) := false
| (tt :: l) d := balanced_aux l (d + 1)
| (ff :: l) 0 := false
| (ff :: l) (d + 1) := balanced_aux l d
def balanced (l : list bool) : Prop := balanced_aux l 0
/-
Define the set of balanced parentheses of length 2*n
(that is, n pairs of parentheses)
-/
def set_balanced (n : ℕ) : set (list bool) :=
{ l : list bool | list.length l = 2 * n ∧ balanced l }
/-
Define the catalan numbers
-/
def sum_to : Π (n:ℕ) , (Π (x:ℕ) , (x<n) → ℕ) → ℕ
| 0 f := 0
| (n+1) f :=
have h : n < (n+1) , by linarith ,
have f' : (Π (x:ℕ) , (x<n) → ℕ) , from
(λ x , λ ineq , f x (by linarith)) ,
f n h + sum_to n f'
def catalan : ℕ → ℕ
| 0 := 1
| (n+1) := sum_to (n+1) (λ i , λ i_le_n ,
have nmi_le_n : (n-i < (n+1)),
by apply nat.sub_lt_succ ,
catalan i * catalan (n-i)
)
/- split (A)B into A, B -/
def split_parens_aux : (list bool) → (ℕ) → (list bool × list bool)
| ([]) n := ([],[])
| (ff :: l) 0 := ⟨ [], [] ⟩ /- doesn't matter -/
| (ff :: l) 1 := ⟨ [], l ⟩
| (ff :: l) (d + 2) := let p := split_parens_aux l (d+1) in ⟨ ff :: p.1 , p.2 ⟩
| (tt :: l) (d) := let p := split_parens_aux l (d+1) in ⟨ tt :: p.1 , p.2 ⟩
def split_parens : (list bool) → (list bool × list bool)
| [] := ([],[]) /- doesn't matter -/
| (tt :: l) := split_parens_aux l 1
| (ff :: l) := ([],[]) /- doesn't matter -/
/- combined A, B into (A)B -/
def combine_parens (l : list bool) (m : list bool) : (list bool) :=
tt :: l ++ ff :: m
/- lemmas about balanced parentheses -/
theorem balanced_split_parens_2_aux : ∀ (l:list bool) (d:ℕ) ,
balanced_aux l d -> balanced (split_parens_aux l d).2
| [] 0 :=
begin
intros ,
rw [split_parens_aux] , simp ,
end
| [] (d + 1) :=
begin
intros ,
rw [balanced_aux] at * , contradiction ,
end
| (tt :: l) d :=
begin
intros , rw [balanced_aux] at * ,
rw [split_parens_aux] ,
simp ,
apply balanced_split_parens_2_aux , assumption ,
end
| (ff :: l) 0 :=
begin
intros ,
rw [split_parens_aux, balanced], simp ,
end
| (ff :: l) (d + 1) :=
begin
intros ,
cases d,
{
simp , rw [split_parens_aux] , simp ,
rw [balanced_aux] at a , rw [balanced] , assumption ,
},
{
rw [split_parens_aux] , simp ,
apply balanced_split_parens_2_aux ,
rw [balanced_aux] at a,
have h : (d + 1) = nat.succ d := rfl ,
rw [h] , assumption ,
}
end
theorem balanced_split_parens_1_aux : ∀ (l:list bool) (d:ℕ) ,
balanced_aux l (d+1) -> balanced_aux (split_parens_aux l (d+1)).1 d
| [] 0 :=
begin
intros ,
rw [split_parens_aux] , simp ,
end
| [] (d + 1) :=
begin
intros ,
rw [balanced_aux] at * , contradiction ,
end
| (tt :: l) d :=
begin
intros ,
rw [split_parens_aux] , simp ,
apply balanced_split_parens_1_aux ,
rw [balanced_aux] at a , assumption ,
end
| (ff :: l) 0 :=
begin
intros ,
rw [split_parens_aux] , simp ,
end
| (ff :: l) (d + 1) :=
begin
intros ,
rw [split_parens_aux] , simp , rw [balanced_aux] ,
apply balanced_split_parens_1_aux ,
rw [balanced_aux] at a ,
assumption ,
end
theorem balanced_split_parens_1 : ∀ (l : list bool) ,
balanced l -> balanced (split_parens l).1 :=
begin
intros ,
rw [balanced] ,
cases l ,
{
rw [split_parens, balanced_aux] , trivial ,
},
cases l_hd ,
{
rw [balanced] at a,
rw [balanced_aux] at a ,
contradiction ,
},
{
rw [split_parens] ,
apply balanced_split_parens_1_aux ,
rw [balanced] at a , rw [balanced_aux] at a ,
assumption ,
}
end
theorem balanced_split_parens_2 : ∀ (l : list bool) ,
balanced l -> balanced (split_parens l).2 :=
begin
intros ,
rw [balanced] ,
cases l ,
{
rw split_parens , simp ,
},
cases l_hd ,
{
rw [balanced] at a,
rw [balanced_aux] at a ,
contradiction ,
},
{
apply balanced_split_parens_2_aux ,
rw [balanced, balanced_aux] at a , simp at a , assumption ,
}
end
theorem balanced_combine_aux : ∀ (l : list bool) (m : list bool) (d:ℕ) ,
balanced_aux l d →
balanced m →
balanced_aux (l ++ ff :: m) (d+1)
| [] m :=
begin
intros , simp , rw [balanced_aux] ,
cases d ,
{
rw [balanced] at a_1 , assumption ,
},
{
rw [balanced_aux] at a , contradiction ,
}
end
| (x :: l) m :=
begin
intros ,
cases x ,
{
have h : (ff :: l ++ ff :: m = ff :: (l ++ ff :: m)) := by simp,
rw h ,
rw [balanced_aux] ,
cases d ,
{
rw [balanced_aux] at a , contradiction ,
},
{
apply balanced_combine_aux ,
rw [balanced_aux] at a , assumption ,
assumption ,
}
},
{
have h : (tt :: l ++ ff :: m = tt :: (l ++ ff :: m)) := by simp,
rw h ,
rw [balanced_aux] ,
apply balanced_combine_aux ,
rw [balanced_aux] at a , assumption,
assumption,
}
end
theorem balanced_combine : ∀ (l : list bool) (m : list bool) ,
balanced l →
balanced m →
balanced (combine_parens l m) :=
begin
intros , rw [combine_parens] ,
rw [balanced] ,
have h : (tt :: l ++ ff :: m = tt :: (l ++ ff :: m)) := by simp,
rw h ,
rw [balanced_aux] ,
apply balanced_combine_aux ,
rw [balanced] at a ,
assumption ,
assumption ,
end
theorem split_parens_combine_parens_aux : ∀ (l : list bool) (m : list bool) (d:ℕ) ,
balanced_aux l d →
balanced m →
split_parens_aux (l ++ ff :: m) (d+1) = ⟨l, m⟩
| [] m 0 :=
begin
intros , simp [split_parens_aux] ,
end
| [] m (d + 1) :=
begin
intros , simp [balanced_aux] at a , contradiction ,
end
| (tt :: l) m d :=
begin
intros ,
simp [split_parens_aux] ,
have q : balanced_aux l (d+1) := begin
rw [balanced_aux] at a , assumption ,
end,
have h := split_parens_combine_parens_aux l m (d+1) q a_1,
simp at h , rw h , simp ,
end
| (ff :: l) m 0 :=
begin
intros ,
simp [balanced_aux] at a , contradiction ,
end
| (ff :: l) m (d + 1) :=
begin
intros ,
simp [split_parens_aux] ,
have q : balanced_aux l d := begin
rw [balanced_aux] at a , assumption ,
end,
have h := split_parens_combine_parens_aux l m d q a_1,
rw h , simp ,
end
theorem split_parens_combine_parens : ∀ (l : list bool) (m : list bool) ,
balanced l →
balanced m →
split_parens (combine_parens l m) = ⟨l, m⟩ :=
begin
intros ,
rw [combine_parens],
have h : (tt :: l ++ ff :: m = tt :: (l ++ ff :: m)) := by simp,
rw h,
rw [split_parens] ,
apply split_parens_combine_parens_aux ,
rw [balanced] at a , assumption ,
assumption ,
end
theorem combine_parens_split_parens_aux : ∀ (l : list bool) (d:ℕ) ,
balanced_aux l (d+1) →
(split_parens_aux l (d+1)).1 ++ ff :: (split_parens_aux l (d+1)).2 = l
| [] d :=
begin
intros , rw [balanced_aux] at a , contradiction ,
end
| (tt :: l) d :=
begin
intros ,
rw [split_parens_aux] , simp ,
rw [balanced_aux] at a ,
have h := combine_parens_split_parens_aux l (d+1) a,
simp at a h, assumption,
end
| (ff :: l) 0 :=
begin
intros , rw [balanced_aux] at a ,
rw [split_parens_aux] , simp ,
end
| (ff :: l) (d+1) :=
begin
intros ,
rw [split_parens_aux] , simp ,
rw [balanced_aux] at a ,
have h := combine_parens_split_parens_aux l d a,
assumption,
end
theorem combine_parens_split_parens : ∀ (l : list bool) ,
balanced l →
¬(l = list.nil) →
combine_parens (split_parens l).1 (split_parens l).2 = l :=
begin
intros ,
cases l ,
simp at a_1, contradiction ,
cases l_hd ,
rw [balanced, balanced_aux] at a , contradiction ,
rw [split_parens] , rw [combine_parens] , simp ,
apply combine_parens_split_parens_aux ,
simp , rw [balanced] at a , rw [balanced_aux] at a , simp at a ,
assumption ,
end
theorem length_combine_parens : ∀ (l : list bool) (m : list bool) ,
balanced l →
balanced m →
list.length (combine_parens l m) = list.length l + list.length m + 2
:=
begin
intros , rw [combine_parens] , simp , rw [<- add_assoc] , simp ,
end
theorem length_split_parens_eq_minus : ∀ (l : list bool) (n:ℕ) (a:ℕ) ,
list.length l = 2 * (n + 1) →
balanced l →
list.length ((split_parens l).1) = 2 * a →
list.length ((split_parens l).2) = 2 * (n - a) :=
begin
intros ,
have h :
list.length (combine_parens (split_parens l).1 (split_parens l).2) = list.length (split_parens l).1 +
list.length (split_parens l).2 + 2 :=
begin
apply length_combine_parens ,
apply balanced_split_parens_1 , assumption ,
apply balanced_split_parens_2 , assumption ,
end,
calc list.length ((split_parens l).2) =
list.length (split_parens l).1 + list.length (split_parens l).2 + 2 -
(list.length (split_parens l).1 + 2) : begin
rw [@nat.add_comm (list.length ((split_parens l).fst)) _] ,
rw nat.add_assoc ,
rw nat.add_sub_cancel ,
end
... = list.length (
combine_parens (split_parens l).1 (split_parens l).2) - (list.length (split_parens l).1 + 2) : by rw h
... = list.length l - (list.length (split_parens l).1 + 2) :
begin
rw combine_parens_split_parens , assumption ,
apply not.intro , intros , subst l , simp at a_1,
contradiction ,
end
... = list.length l - (2 * a + 2) : by rw a_3
... = 2 * (n + 1) - (2 * a + 2) : by rw a_1
... = 2 * (n + 1) - (2 * a + 2 * 1) : by simp
... = 2 * (n + 1) - 2 * (a + 1) : by rw [mul_add 2 a 1]
... = 2 * ((n+1) - (a+1)) : by rw [nat.mul_sub_left_distrib 2 (n+1) (a+1)]
... = 2 * (n - a) : by simp
end
theorem even_length_of_balanced_aux : ∀ (l : list bool) (d : ℕ) ,
balanced_aux l d →
(∃ m , list.length l + d = 2 * m)
| [] 0 :=
begin
intros , existsi 0 , simp ,
end
| [] (d + 1) :=
begin
intros , rw [balanced_aux] at a , contradiction ,
end
| (tt :: l) d :=
begin
intros ,
rw [balanced_aux] at a ,
have h := even_length_of_balanced_aux l (d+1) a ,
cases h ,
existsi h_w ,
simp ,
simp at h_h , assumption ,
end
| (ff :: l) 0 :=
begin
intros , rw [balanced_aux] at a , contradiction ,
end
| (ff :: l) (d + 1) :=
begin
intros ,
rw [balanced_aux] at a ,
have h := even_length_of_balanced_aux l d a ,
cases h ,
existsi (h_w + 1) ,
rw [mul_add] , simp , rw <- h_h , simp ,
rw [<- add_assoc] , simp ,
end
theorem even_length_of_balanced : ∀ (l : list bool) ,
balanced l →
2 ∣ list.length l
:=
begin
intros ,
rw [balanced] at a ,
have h := even_length_of_balanced_aux l 0 a ,
cases h ,
simp at h_h ,
rw h_h ,
apply dvd_mul_right ,
end
theorem length_split_parens_1_le : ∀ (l : list bool) ,
balanced l →
¬(l = list.nil) →
list.length (split_parens l).1 ≤ list.length l - 2 :=
begin
intros ,
have h : (combine_parens (split_parens l).1 (split_parens l).2 = l)
:= combine_parens_split_parens l a a_1 ,
have h2 : (list.length (combine_parens (split_parens l).1 (split_parens l).2) = list.length (split_parens l).1 + list.length (split_parens l).2 + 2)
:=
begin
apply length_combine_parens ,
apply balanced_split_parens_1 , assumption ,
apply balanced_split_parens_2 , assumption ,
end,
rw h at h2 ,
calc list.length ((split_parens l).fst)
≤ list.length ((split_parens l).fst) + list.length ((split_parens l).snd) : by linarith
... = list.length ((split_parens l).fst) + list.length ((split_parens l).snd) + 2 - 2 : by simp
... = list.length l - 2 : by rw h2
end
lemma catalan_set_eq_with_bound : ∀ (n:ℕ) ,
{l : list bool | list.length l = 2 * (n + 1) ∧ balanced l} =
{l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧
list.length (split_parens l).1 < 2*(n+1)} :=
begin
intros ,
apply set.ext ,
intros , split ,
{
intros ,
split ,
cases a, assumption ,
split ,
cases a , assumption ,
simp at a , cases a ,
calc list.length (split_parens x).1 ≤ (list.length x) - 2
: (begin
apply length_split_parens_1_le , assumption ,
apply not.intro , intros , subst x , simp at a_left , trivial ,
end)
... = 2 * n
: (begin
rw a_left,
rw mul_add , simp ,
end)
... < 2 * (n + 1)
: by linarith
},
{
intros ,
cases a , cases a_right , simp , split , assumption ,
assumption ,
}
end
/- annoying arithmetic lemmas -/
theorem nat_lt_of_not_eq : ∀ (n:ℕ) (m:ℕ) ,
n < m+1 → ¬(n = m) → n < m :=
begin
intros ,
by_contradiction ,
have h : (n = m) := nat.eq_of_lt_succ_of_not_lt a a_2 ,
contradiction ,
end
theorem even_nat_lt : ∀ (n:ℕ) (m:ℕ) ,
(n) < 2*(m + 1) →
¬ ((n) = 2*m) →
2 ∣ n →
n < 2*m :=
begin
intros ,
by_cases (n = 2 * m + 1) ,
{
subst n ,
/- derive a contradiction from 2 | 2*m + 1
(surely there was an easier way to do this?) -/
have h : (2 ∣ (2 * int.of_nat m)) := dvd_mul_right _ _,
have h1 : (2 ∣ (2 * int.of_nat m) + 1) := begin
have h3 : (2 ∣ int.of_nat (2*m + 1)) :=
begin
apply int.of_nat_dvd_of_dvd_nat_abs ,
simp , simp at a_2 , assumption ,
end,
have two_eq : 2 = int.of_nat 2 := by refl ,
rw two_eq ,
have one_eq : 1 = int.of_nat 1 := by refl ,
rw one_eq ,
rw <- int.of_nat_mul ,
rw <- int.of_nat_add ,
assumption ,
end,
have h2 : (2 ∣ ((2 * int.of_nat m) + 1) - (2 * int.of_nat m)) :=
begin
apply dvd_sub , assumption , assumption ,
end,
simp at h2,
have h3 : ((1:int) % 2 = 0) := begin
apply int.mod_eq_zero_of_dvd , assumption ,
end,
have h4 : ((1:int) % 2 = 1) := rfl ,
rw h4 at h3 ,
simp at h3 ,
contradiction ,
},
{
have i : (2*(m+1) = (2*m + 1) + 1) := begin
rw [mul_add] , simp , rw [<- @nat.add_assoc 1 _] ,
end,
have j : (n < ((2*m) + 1) + 1) := begin
rw <- i , assumption ,
end ,
have k : (n < (2*m) + 1) := nat_lt_of_not_eq _ _ j h ,
have l : (n < (2*m)) := nat_lt_of_not_eq _ _ k a_1 ,
assumption ,
}
end
lemma catalan_set_induction : ∀ (n:ℕ) (i:ℕ) (a:ℕ) (b:ℕ) ,
has_card {l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧
list.length (split_parens l).1 < 2*i} a →
has_card {l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧
list.length (split_parens l).1 = 2*i} b →
has_card {l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧
list.length (split_parens l).1 < 2*(i+1)} (a+b) :=
begin
intros ,
apply card_split _
{l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧
list.length (split_parens l).1 < 2*i}
{l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧
list.length (split_parens l).1 = 2*i}
a b ,
{
intros , simp , simp at a_3 , cases a_3 , cases a_3_right ,
split, assumption , split, assumption , linarith ,
},
{
intros , simp , simp at a_3 , cases a_3 , cases a_3_right ,
split, assumption , split, assumption , linarith ,
},
{
simp ,
intros ,
by_cases (list.length (split_parens x).1 = 2*i) ,
{
right , split, assumption, split, assumption, assumption ,
},
{
left, split, assumption, split, assumption,
apply even_nat_lt ,
assumption ,
assumption ,
apply even_length_of_balanced ,
apply balanced_split_parens_1 ,
assumption ,
}
},
{
simp , intros ,
apply not.intro , intros ,
rw a_8 at a_5 ,
linarith ,
},
{
assumption ,
},
{
assumption ,
},
end
lemma catalan_set_base : ∀ (n:ℕ) ,
has_card {l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧
list.length (split_parens l).1 < 2*0} 0 :=
begin
intros,
apply card_0 ,
simp ,
end
/-
Show that balanced parentheses have cardinality the catalan
numbers by splitting the parentheses strings into pairs.
We do induction on `bound`.
-/
lemma has_card_set_balanced_aux : ∀ bound n ,
n < bound →
has_card (set_balanced n) (catalan n)
| 0 n :=
begin
intros ,
linarith ,
end
| (bound+1) 0 :=
begin
intros ,
rw [set_balanced, catalan] ,
apply (card_1 _ []) ,
simp ,
intros , simp at a_1 , cases a_1 , cases y ; trivial ,
end
| (bound+1) (n+1) :=
begin
intros ,
rw catalan ,
rw set_balanced ,
/- add the bound that the first part of the split is < 2*(n+1) -/
rw catalan_set_eq_with_bound ,
/- replace n+1 with j (why is this so annoying omg) -/
have j' : (∃ j , j = n+1) , existsi (n+1), trivial, cases j', rename j'_w j ,
have e : (
{l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧ list.length ((split_parens l).fst) < 2 * (n + 1)} =
{l : list bool | list.length l = 2 * (n + 1) ∧ balanced l ∧ list.length ((split_parens l).fst) < 2 * j}) ,
rw j'_h ,
rw e ,
clear e ,
have e : (
sum_to (n + 1) (λ (i : ℕ) (i_le_n : i < n + 1), catalan i * catalan (n - i))
=
sum_to j (λ (i : ℕ) (i_le_n : i < j), catalan i * catalan (n - i))) := by rw j'_h,
rw e , clear e,
have n_bound : (n+1) < (bound+1) := a , /- copy this -/
have j_bound : j ≤ (n+1) :=
begin
subst j ,
end ,
clear a , clear j'_h ,
/- do induction on j for the summation of the recursion -/
induction j,
{
rw [sum_to] ,
apply catalan_set_base ,
},
{
rw [sum_to] , simp ,
rw [@nat.add_comm (catalan j_n * catalan (n - j_n)) _] ,
apply catalan_set_induction ,
{
apply j_ih ,
calc j_n ≤ j_n + 1 : by linarith
... = nat.succ j_n : by refl
... ≤ n + 1 : by assumption
},
{
/- Show that the length of balanced strings
of the form (A)B where |A|=2*j_n is
catalan j_n * catalan (n-j_n). -/
clear j_ih ,
apply (card_product
{l : list bool | list.length l = 2 * j_n ∧
balanced l}
{l : list bool | list.length l = 2 * (n - j_n) ∧
balanced l}
_
(catalan j_n)
(catalan (n - j_n))
combine_parens
) ,
{
simp, intros,
split ,
rw [combine_parens] , simp , rw a , rw a_2 ,
{
calc 1 + (1 + (2 * j_n + 2 * (n - j_n))) = 2 * (n + 1) : begin
rw [<- mul_add] ,
rw [add_comm j_n] ,
rw [nat.sub_add_cancel] , ring,
have h : (nat.succ j_n = j_n + 1) := rfl ,
rw h at j_bound , linarith ,
end
},
{
split,
apply balanced_combine , assumption, assumption ,
rw split_parens_combine_parens , simp , assumption ,
assumption, assumption,
}
},
{
simp, intros ,
existsi (split_parens z).1 ,
split ,
split , assumption ,
apply balanced_split_parens_1 , assumption ,
existsi (split_parens z).2 ,
split ,
split ,
apply length_split_parens_eq_minus ; assumption ,
apply balanced_split_parens_2 , assumption ,
apply combine_parens_split_parens ,
assumption ,
apply not.intro , intros , subst z , simp at a ,linarith ,
},
{
simp, intros ,
have e : (( x, y ) = ( x', y' )) := (
calc ( x, y ) = split_parens (combine_parens x y) :
(begin
rw split_parens_combine_parens ,
assumption, assumption ,
end)
... = split_parens (combine_parens x' y') :
(begin
rw a_8 ,
end)
... = ( x', y' ) :
(begin
rw split_parens_combine_parens ,
assumption, assumption ,
end)),
simp at e ,
assumption ,
},
{
apply (has_card_set_balanced_aux bound) ,
have h : (nat.succ j_n = j_n + 1) := rfl ,
rw h at j_bound , linarith ,
},
{
apply (has_card_set_balanced_aux bound) ,
have h : (nat.succ j_n = j_n + 1) := rfl ,
rw h at j_bound ,
have t : (n - j_n ≤ n) := nat.sub_le_self _ _ ,
linarith ,
},
}
}
end
/- main theorem that balanced parentheses strings of length 2*n
has cardinality catalan n -/
theorem has_card_set_balanced : ∀ n ,
has_card (set_balanced n) (catalan n) :=
begin
intros ,
apply (has_card_set_balanced_aux (n+1) n) ,
linarith ,
end
/- below_diagonal_path is a proposition that indicates
a sequence represents a path
from (0,0) to (n, n+1) (where tt is +1 in x direction
and ff is +1 in y direction) which always stays below
the diagonal.
We will biject such paths with the balanced parentheses,
(theorem `has_card_set_below_diagonal_path_catalan`)
which will show that they have cardinality `catalan n`.
Then we will show that (2n+1) rotations of
of these paths will be the set of all paths from (0,0) to (n,n+1),
which has number (2n+1 choose n).
-/
def below_diagonal_path (n : ℕ) (l : list bool) :=
list.length l = 2*n + 1 ∧
count_tt l = n ∧
(forall (i:ℕ) , i ≤ (2*n + 1) →
((int.of_nat i) * (int.of_nat n) -
(2*(int.of_nat n)+1) * (int.of_nat (count_tt (list.take i l))) ≤ 0))
def argmax : (ℕ → ℤ) → ℕ → ℕ
| f 0 := 0
| f (n+1) := if f (n+1) > f (argmax f n) then (n+1) else argmax f n
/-
Gets the point on a path from (0,0) to (n,n+1)
which is farthest above the (0,0)--(n,n+1) diagonal.
This is important because if we rotate the path to this
point, then it will be a below_diagonal_path.
(theorem below_diagonal_path_rotate_best_point).
-/
def best_point (n:ℕ) (l : list bool) :=
argmax (λ i ,
(int.of_nat n) * (int.of_nat i) -
(2*(int.of_nat n) + 1) * (int.of_nat (count_tt (list.take i l)))
) (2*n)
/-
A bunch of lemmas dealing with rotations and argmax and
miscellaneous. We represent rotations as numbers i
where 0 ≤ i < n. And implement them as
`list.drop i l + list.take i l`.
(In retrospect it might have made more sense to use list.rotate
more heavily.)
-/
def negate_rotation (n:ℕ) (i:ℕ) :=
if i = 0 then 0 else n - i
def compose_rotation (n:ℕ) (i:ℕ) (j:ℕ) :=
(i + j) % n
theorem argmax_lt_length : ∀ (f : ℕ → ℤ) (n : ℕ) ,
argmax f n < n+1
| f 0 := begin intros, rw [argmax] , linarith , end
| f (n+1) :=
begin
rw argmax ,
split_ifs ,
linarith ,
have h : argmax f n < n + 1 := argmax_lt_length f n ,
linarith ,
end
theorem func_argmax_ge : ∀ (f: ℕ → ℤ) (n : ℕ) (i : ℕ) ,
i ≤ n →
f (argmax f n) ≥ f i
| f 0 0 :=
begin
intros , rw [argmax] , linarith ,
end
| f 0 (i+1) :=
begin
intros , linarith ,
end
| f (n+1) i :=
begin
intros ,
{
rw [argmax] , split_ifs ,
{
rename h h' ,
by_cases (i = n+1) ,
{
subst i , linarith ,
},
{
apply le_of_lt ,
have h2 : (i < n+1) := begin
apply nat_lt_of_not_eq, linarith , assumption ,
end,
calc f i ≤ f (argmax f n) :
(begin
apply func_argmax_ge ,
rw <- nat.lt_succ_iff ,
assumption ,
end)
... < f (n+1) : h'
}
},
{
have h' : (f (argmax f n) ≥ f (n + 1)) := begin
apply le_of_not_gt , assumption ,
end,
by_cases (i=n+1) ,
{
subst i , assumption ,
},
{
have h2 : (i < n+1) := begin
apply nat_lt_of_not_eq, linarith , assumption ,
end,
rw nat.lt_succ_iff at h2 ,
apply func_argmax_ge , assumption,
}
},
}
end
theorem best_point_gt (n:ℕ) (l : list bool) (j:ℕ) :
j < 2 * n + 1 →
(int.of_nat n) * (int.of_nat (best_point n l)) -
(2*(int.of_nat n) + 1) * (int.of_nat (count_tt (list.take (best_point n l) l)))
≥
(int.of_nat n) * (int.of_nat j) -
(2*(int.of_nat n) + 1) * (int.of_nat (count_tt (list.take j l)))
:=
begin
rw [best_point] ,
intros ,
have ineq : j ≤ (2*n) := begin
rw <- nat.lt_succ_iff , assumption ,
end,
have h := func_argmax_ge (λ (i : ℕ),
int.of_nat n * int.of_nat i - (2 * int.of_nat n + 1) * int.of_nat (count_tt (list.take i l)))
(2*n) j ineq ,
simp at h ,
rw [add_comm] , assumption ,
end
theorem negate_rotation_lt : ∀ (n:ℕ) (i:ℕ) ,
0 < n → negate_rotation n i < n :=
begin
intros, rw [negate_rotation] ,
split_ifs , assumption , apply nat.sub_lt_self , assumption ,
cases i , contradiction ,
have h : (nat.succ i = i + 1) := rfl ,
rw h , linarith ,
end
theorem compose_rotation_lt : ∀ (n:ℕ) (i:ℕ) (j:ℕ) ,
0 < n → compose_rotation n i j < n :=
begin
intros , rw [compose_rotation] ,
apply nat.mod_lt , assumption ,
end
theorem eq_0_of_dvd_of_lt : ∀ (n:ℕ) (m:ℕ) ,