/
one.lean
2435 lines (2162 loc) · 95 KB
/
one.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 Lean
import Mathlib.Tactic.Find
import Mathlib.Tactic.LibrarySearch
import Mathlib.Tactic.applyFun
import Init.Data.String.Basic
import Init.Data.Int.Basic
import Std.Data.Array.Init.Lemmas
import Std.Data.Array.Lemmas
import Std.Data.List.Init.Lemmas
import Std.Data.Nat.Lemmas
import Std.Data.Int.Lemmas
import Mathlib.Data.Nat.Log
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Ring
import Mathlib.Tactic.SolveByElim
import Mathlib.Data.List.MinMax
import Aesop
open Lean Parsec
set_option profiler true
lemma Array.ext_iff {α : Type u_1} {as bs : Array α} : as = bs ↔ as.data = bs.data := by
apply Iff.intro
. intro eq
simp only [eq]
. intro eq
exact Array.ext' eq
@[simp]
lemma List.modifyLast_singleton (f: α → α) (a: α): List.modifyLast f [a] = [f a] := by
rw [← nil_append [a], modifyLast_append_one, nil_append]
def String.toNatAux (s: List Char) (accum:ℕ): ℕ :=
match s with
| [] => accum
| head::tail => String.toNatAux tail (accum * 10 + (head.toNat - '0'.toNat))
def String.toNatΔ (s: List Char): ℕ :=
String.toNatAux s 0
lemma String.toNatAux_accumulates (s: List Char) (accum:ℕ):
String.toNatAux s accum = String.toNatAux s 0 + accum * 10^(List.length s) := by
induction s generalizing accum with
| nil => unfold toNatAux; simp
| cons head tail ih =>
unfold toNatAux
rw [ih]
conv => right; rw [ih]
simp [Nat.succ_eq_add_one]
ring
theorem String.toNatΔ_cons (head: Char) (tail: List Char):
String.toNatΔ (head::tail) = (head.toNat - '0'.toNat)*10^(List.length tail) + (String.toNatΔ tail) := by
unfold String.toNatΔ
rw [String.toNatAux, String.toNatAux_accumulates]
ring
def String.toIntΔ (s: List Char): ℤ :=
match s with
| [] => 0
| h::tail => if h = '-' then - String.toNatΔ tail else String.toNatΔ (h::tail)
def Int.reprΔ (i: ℤ): List Char :=
match i with
| Int.ofNat m => Nat.toDigits 10 m
| Int.negSucc m => ['-'] ++ Nat.toDigits 10 (Nat.succ m)
theorem Nat.toDigitsCore_ne_nil (P: f > n): Nat.toDigitsCore b f n a ≠ [] := by
unfold Nat.toDigitsCore
split
. case _ => contradiction
. case _ _ _ _ fuel =>
simp
have h: ∀x, List.length (Nat.toDigitsCore b fuel (n / b) (x :: a)) ≠ 0 := by
simp [Nat.to_digits_core_lens_eq]
split
case _ => simp
case _ =>
intro P₂
apply h
rw [P₂]
simp
lemma Nat.toDigits_ne_nil: Nat.toDigits b n ≠ [] := by
unfold Nat.toDigits
simp [Nat.toDigitsCore_ne_nil]
lemma Int.reprΔ_ne_nil (i: ℤ): Int.reprΔ i ≠ [] := by
unfold Int.reprΔ
cases i with
| ofNat m => simp only; apply Nat.toDigits_ne_nil
| negSucc m => simp only [List.singleton_append, ne_eq, not_false_iff]
@[simp]
lemma Nat.digitChar_is_digit (n: ℕ) (P: n < 10): Char.isDigit (Nat.digitChar n) = true := by
revert n
decide
lemma Nat.toDigitsCore_digits (b: ℕ) (n:ℕ) (P: b <= 10) (Q: b > 1): c ∈ (Nat.toDigitsCore b f n a) → (c.isDigit ∨ c ∈ a):= by
induction n using Nat.strong_induction_on generalizing f a with
| _ n h =>
have _: b>0 := by calc
b > 1 := Q
_ > 0 := by simp
have nmodb_le10: n % b < 10 := by calc
n % b < b := by apply Nat.mod_lt; simp [*]
_ ≤ 10 := by exact P
unfold Nat.toDigitsCore
split
next =>
intro h₂
simp [h₂]
next _ _ _ fuel=>
simp
intro h₂
cases h₃: n / b == 0 with
| true =>
have h₄:n/b = 0 := by apply LawfulBEq.eq_of_beq; assumption
simp [h₄] at h₂
cases h₂ with
| inr h₅ => simp [h₅]
| inl h₅ =>
left
rw [h₅]
simp [nmodb_le10, Nat.digitChar_is_digit]
| false =>
have h₄: n/b ≠ 0 := by apply ne_of_beq_false; assumption
simp [h₄] at h₂
have h₅: Char.isDigit c = true ∨ c ∈ Nat.digitChar (n % b) :: a := by
apply h (n/b) (f:= fuel) (a:=(Nat.digitChar (n % b) :: a))
next =>
have h₅: n ≠ 0 := by
intro x
unfold Ne at h₄
have h₆:= Nat.zero_div b
conv at h₆ =>
left
rw [← x]
contradiction
apply Nat.div_lt_self
. simp [h₅, Nat.pos_of_ne_zero]
. simp [Q]
next _ => exact h₂
simp at h₅
cases h₅ with
| inl h₆ => simp [h₆]
| inr h₆ => cases h₆ with
| inl h₇ => rw [h₇]; left; simp [nmodb_le10, Nat.digitChar_is_digit]
| inr h₇ => simp [h₇]
lemma Nat.toDigitsCore_accumulates: toDigitsCore b f n (start ++ rest) = toDigitsCore b f n start ++ rest := by
induction f using Nat.strong_induction_on generalizing start rest n with
| h f ih =>
unfold toDigitsCore
split
. case h.h_1 => simp
. case h.h_2 f _ _ _ q =>
simp
split
. case inl =>
simp
. case inr =>
rewrite [← List.cons_append]
rewrite [ih]
. rfl
. simp only [lt_succ_self]
lemma Nat.todigitsCore_accumulates_suffix: toDigitsCore b f n rest = toDigitsCore b f n [] ++ rest := by
have h: rest = [] ++ rest := by simp
conv=> left; rw [h]
apply Nat.toDigitsCore_accumulates
lemma Nat.toDigitsCore_fuel_irrelevant (P: f >= n+1) (Q: b > 1): toDigitsCore b f n rest = toDigitsCore b (n+1) n rest := by
induction f using Nat.strong_induction_on generalizing rest n
case h f ih =>
unfold toDigitsCore
simp
split
case h_1 =>
simp at P
case h_2 n' =>
conv =>
left; rw [Nat.todigitsCore_accumulates_suffix]
conv =>
right; rw [Nat.todigitsCore_accumulates_suffix]
split
case inl =>
rfl
case inr =>
simp
rw [ih]
. cases h: n == (n / b) + 1 with
| false =>
simp at h
rw [← Nat.toDigits, ih, ← Nat.toDigits]
. calc
succ n' ≥ n + 1 := P
_ > n := by simp only [gt_iff_lt, lt_add_iff_pos_right]
. simp [h]
have h₂: n ≥ n / b + 1 := by
simp
apply Nat.div_lt_self
. apply Nat.pos_of_ne_zero; intro h; simp only [gt_iff_lt, h, Nat.zero_div, not_true] at *
. exact Q
simp [ge_iff_le] at h₂
have h₃:= Nat.eq_or_lt_of_le h₂
cases h₃ with
| inl h₄ => exfalso; apply h; simp only [h₄]
| inr h₄ => exact h₂
| true =>
simp at h
rw [← h]
. simp
. simp [Nat.succ_eq_add_one] at P
calc
n' ≥ n := P
n ≥ n / b + 1 := by simp only [add_lt_add_iff_right]; apply Nat.div_lt_self; apply Nat.pos_of_ne_zero; intro h; simp only [gt_iff_lt, h, Nat.zero_div, not_true] at *; apply Q
lemma Nat.toDigits_digits (b: ℕ) (n:ℕ) (P: b <= 10) (Q: b > 1): List.all (Nat.toDigits b n) (Char.isDigit) == true := by
let h: ∀ c, c ∈ Nat.toDigitsCore b (n+1) n [] → Char.isDigit c = true ∨ c ∈ [] := by
intro c
apply Nat.toDigitsCore_digits _ _ P Q
simp
simp at h
unfold Nat.toDigits
apply h
lemma List.get?_cons {h: α} {tail : List α} {n : Nat} (hn: n>0): (h::tail).get? n = tail.get? (n-1) := by
conv => left; unfold List.get?
cases n with
| zero => simp only at hn
| succ n => simp only [ge_iff_le, Nat.succ_sub_succ_eq_sub, nonpos_iff_eq_zero, tsub_zero]
theorem Nat.toDigitsCore_shift' (b:ℕ) (n:ℕ) (P: b>1): ∀i:ℕ, (Nat.toDigits b n).reverse.getD (i+1) '0' = (Nat.toDigits b (n/b)).reverse.getD i '0':= by
intro i
rw [toDigits, toDigitsCore]
simp only [add_eq, add_zero]
split
. next heq =>
conv => left; unfold List.getD
simp only [List.get?, Option.getD_none]
rw [heq]
unfold toDigits toDigitsCore digitChar
simp only [Nat.zero_div, zero_mod, zero_ne_one, ite_false, ite_true, List.reverse_cons, List.reverse_nil,
List.nil_append, List.getD_singleton_default_eq]
. next heq =>
rw [Nat.todigitsCore_accumulates_suffix]
rw [List.getD, List.getD]
congr 1
simp only [List.reverse_append, List.reverse_cons, List.reverse_nil, List.nil_append, List.singleton_append,
List.cons.injEq, succ.injEq, and_imp, forall_apply_eq_imp_iff₂, forall_apply_eq_imp_iff', forall_eq',
List.get?, add_eq, add_zero]
rw [Nat.toDigitsCore_fuel_irrelevant, ← Nat.toDigits]
. simp only [ge_iff_le]
have h: n ≠ 0 := by
simp only [ne_eq]
intro h
rw [h] at heq
simp only [Nat.zero_div] at heq
apply Nat.div_lt_self
. simp only [ne_eq, h, not_false_iff, Nat.pos_of_ne_zero]
. exact P
. exact P
theorem Nat.toDigitsCore_shift (b:ℕ) (n:ℕ) (P: b>1): ∀i:ℕ, i>0 → (Nat.toDigits b n).reverse.getD i '0' = (Nat.toDigits b (n/b)).reverse.getD (i-1) '0':= by
intro i igt
generalize h: i - 1 = p
have heq: i = p + 1 := by cases i with | zero => contradiction | succ n => simp at h; rw [h]
rw [heq]
apply Nat.toDigitsCore_shift'
exact P
lemma Nat.toDigitsCore_shift_full (b:ℕ) (n:ℕ) (P: b>1): ∀i:ℕ, (Nat.toDigits b n).reverse.getD i '0' = (Nat.toDigits b (n/b^i)).reverse.getD 0 '0' := by
intro i
induction i generalizing n with
| zero =>
simp only [zero_eq, pow_zero, Nat.div_one]
| succ i ih =>
rw [Nat.toDigitsCore_shift]
. simp
rw [ih]
congr 3
rw [Nat.div_div_eq_div_mul]
congr 1
rw [Nat.pow_succ']
. exact P
. simp
def Nat.digit (base:ℕ) (n:ℕ) (index:ℕ): ℕ := (n / base^index) % base
@[simp]
theorem Nat.digit_lt_base {base n index: ℕ} (P: base > 0): Nat.digit base n index < base := by
unfold Nat.digit
apply Nat.mod_lt _ P
theorem Nat.toDigits_eq_digit_rev (b: ℕ) (n:ℕ) (P: b > 1):
∀ i:ℕ, (Nat.toDigits b n).reverse.getD i '0' = Nat.digitChar (Nat.digit b n i) := by
intro i
rw [Nat.toDigitsCore_shift_full]
. unfold toDigits toDigitsCore digit
simp only [add_eq, add_zero]
split
. next heq =>
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.getD._eq_1, List.get?, Option.getD_some]
. next heq =>
rw [Nat.todigitsCore_accumulates_suffix]
simp only [List.reverse_append, List.reverse_cons, List.reverse_nil, List.nil_append, List.singleton_append,
List.getD._eq_1, List.get?, Option.getD_some]
. exact P
theorem Nat.toDigitsCore_length_eq_log (b fuel n: ℕ ) (P: b>1) (R: fuel>n): List.length (Nat.toDigitsCore b fuel n accum) = Nat.log b n + 1 + List.length accum:= by
have heq: accum = [] ++ accum := by simp only [List.nil_append]
rw [heq, Nat.toDigitsCore_accumulates]
simp only [List.length_append, List.nil_append, add_left_inj]
induction n using Nat.strong_induction_on generalizing fuel accum
case h n ih =>
unfold toDigitsCore
split
. next i _ _ _=>
exfalso
apply Nat.not_lt_of_le (Nat.zero_le i)
apply R
. next w y p l =>
simp; split
. next i h₂=>
simp
left
have h: b > 0 := pos_of_gt P
apply (Nat.div_lt_one_iff h).1
simp only [h₂, zero_lt_one]
. next n heq =>
rw [Nat.todigitsCore_accumulates_suffix]
simp only [List.length_append, List.length_singleton, add_left_inj]
have h: n/b<n := by
apply Nat.div_lt_self
. apply Nat.pos_of_ne_zero
intro h
simp only [h, Nat.zero_div, not_true] at heq
. apply P
rw [ih]
. rw [Nat.log_div_base, Nat.sub_add_cancel]
apply Nat.log_pos
. apply P
. apply (Nat.one_le_div_iff (Nat.lt_of_succ_lt P)).1
apply Nat.succ_le_iff.2
apply Nat.pos_of_ne_zero
apply heq
. exact h
. exact []
. calc
l ≥ n := by exact le_of_lt_succ R
_ > n/b := h
. simp
lemma Nat.toDigits_length_eq_log {b n: ℕ} (P: b>1): List.length (Nat.toDigits b n) = Nat.log b n + 1:= by
unfold Nat.toDigits
rw [Nat.toDigitsCore_length_eq_log]
. simp only [List.length_nil, add_zero]
. exact P
. apply Nat.lt_succ_self
theorem Nat.toDigits_eq_digit (b n:ℕ) (P: b>1):
∀ i:ℕ, i < List.length (Nat.toDigits b n) → List.getD (Nat.toDigits b n) i '0' = Nat.digitChar (Nat.digit b n (List.length (Nat.toDigits b n) - 1 - i)) := by
intro i h
rw [← Nat.toDigits_eq_digit_rev b n P (List.length (Nat.toDigits b n) - 1 - i)]
rw [ List.getD, List.getD, List.get?_reverse]
congr
. have h₂: List.length (toDigits b n) - 1 ≥ (List.length (toDigits b n) - 1 - i) := by simp
have h₃: List.length (toDigits b n) ≥ 1 := by calc
List.length (toDigits b n) > i := h
_ ≥ 0 := by simp only [ge_iff_le, _root_.zero_le]
have h₄: i ≤ List.length (toDigits b n) - 1 := by apply Nat.le_pred_of_lt; exact h
zify [h₂, h₃, h₄]
apply Int.eq_of_sub_eq_zero
ring_nf
. rw [Nat.sub_sub]
apply Nat.sub_lt_self
. simp only [add_pos_iff, true_or]
. rw [Nat.add_comm]
apply Nat.lt_iff_add_one_le.1 h
theorem Nat.digit_gt_log_eq_zero (b n i:ℕ) (P: b>1) (Q: i > Nat.log b n ): Nat.digit b n i = 0 := by
unfold digit
convert Nat.zero_mod b
apply Nat.div_eq_of_lt
apply Nat.lt_pow_of_log_lt
. exact P
. exact Q
def List.lastN (n:ℕ) (l:List α): List α := List.drop (l.length-n) l
@[simp]
theorem List.lastN_zero (l:List α): List.lastN 0 l = [] := by
unfold List.lastN
simp
@[simp]
theorem List.lastN_length_eq_self (l: List α): List.lastN (length l) l = l := by
unfold List.lastN
simp
@[simp]
lemma List.lastN_length (l: List α) (i:ℕ): length (List.lastN i l) = min i (length l) := by
unfold lastN
simp only [ge_iff_le, length_drop]
cases h: decide (i ≤ length l) with
| true =>
simp at h
rw [Nat.sub_sub_self h, Nat.min_eq_left h]
| false =>
simp at h
have h₂: length l ≤ i := Nat.le_of_lt h
simp [h₂]
lemma List.lastN_cons (head: α) (tail: List α) (i: ℕ): List.lastN i (head::tail) = if (head::tail).length > i then lastN i tail else head::tail := by
unfold lastN
induction tail with
| nil =>
split
case inl heq => simp_all
case inr heq =>
simp only [length_singleton, gt_iff_lt, Nat.lt_one_iff, ← ne_eq] at heq
simp only [length_singleton, ge_iff_le, Nat.sub_eq_zero_of_le (Nat.succ_le_of_lt (Nat.pos_of_ne_zero heq)), drop]
| cons mid tail _ih=>
split
case inl heq =>
simp [Nat.succ_eq_one_add]
rw [Nat.add_sub_assoc, ←Nat.succ_eq_one_add, drop._eq_3]
simp_all only [Nat.le_of_lt_succ, length_cons, Nat.succ_eq_one_add, ge_iff_le, gt_iff_lt]
case inr heq =>
simp only [length_cons, gt_iff_lt, not_lt] at heq
simp only [length_cons, ge_iff_le, Nat.sub_eq_zero_of_le heq, drop]
@[simp]
lemma List.lastN_ge_length (l: List α) (h: n ≥ length l): List.lastN n l = l := by
unfold List.lastN
simp [h]
lemma List.lastN_one_eq_getLast (l:List α): l.lastN 1 = l.getLast?.toList:= by
induction l with
| nil => simp only [length_nil, ge_iff_le, lastN_ge_length, getLast?_nil, Option.to_list_none]
| cons head tail ih=>
rw [lastN_cons]
simp only [length_cons, gt_iff_lt, ge_iff_le]
split
case inl heq =>
have hne: tail ≠ [] := by
apply List.ne_nil_of_length_pos
apply Nat.succ_lt_succ_iff.1 heq
rw [getLast?_cons, ih, getLast?_eq_getLast _ hne, List.getLastD]
split
. contradiction
. simp
case inr heq =>
simp at heq
have hnil: tail = [] := List.eq_nil_of_length_eq_zero (Nat.eq_zero_of_le_zero (Nat.le_of_succ_le_succ heq))
subst tail
simp only [getLast?_singleton, Option.to_list_some]
lemma List.getLast?_some {α} {l: List α} {a:α} (h:List.getLast? l = some a):
List.getLast l (by have h₂:= congr_arg Option.isSome h; simp at h₂; simp [h₂]) = a := by
have h₂:= congr_arg Option.isSome h
simp only [Option.isSome_some, getLast?_isSome, ne_eq] at h₂
rw [ List.getLast?_eq_getLast l h₂] at h
simp_all only [Option.some.injEq]
lemma List.getLastD_ne_nil (h: l ≠ []): List.getLastD l a = List.getLast l h := by
cases l with
| nil => contradiction
| cons hd tl =>
rw [List.getLastD_cons, List.getLast_eq_getLastD]
@[simp]
lemma List.get_zero_cons_tail (l:List α) (h: 0 < l.length): List.get l {val:=0, isLt:=h} :: List.tail l = l := by
cases l with
| nil => simp only [zero_le, ge_iff_le, nonpos_iff_eq_zero, length_nil, lt_self_iff_false] at h
| cons => simp only [get, tail_cons]
@[simp]
theorem List.lastN_eq_cons_lastN (n) (l:List α) (P:n < l.length):
get l ⟨ l.length - 1 - n, Nat.sub_one_sub_lt P⟩::(List.lastN n l) = List.lastN (n+1) l := by
unfold lastN
have h: length l - (n + 1) < length l := by
apply Nat.sub_lt_self
. simp only [zero_le, ge_iff_le, nonpos_iff_eq_zero, add_pos_iff, or_true]
. simp only [Nat.succ_eq_add_one, P, Nat.succ_le_of_lt]
conv =>
right
rw [List.drop_eq_get_cons (h:=h)]
congr 2
. congr 1
rw [Nat.sub_sub, Nat.add_comm]
. rw [← Nat.sub_sub, Nat.sub_add_cancel]
apply Nat.le_of_add_le_add_right (b:=n)
rw [Nat.sub_add_cancel]
. rw [Nat.add_comm, ← Nat.succ_eq_add_one]
apply Nat.succ_le_of_lt P
. simp only [P, Nat.le_of_lt]
@[simp]
theorem List.drop_cons (n) (head:α) (tail:List α): List.drop (n+1) (head::tail) = List.drop n tail := by
simp only [drop, zero_le, ge_iff_le, nonpos_iff_eq_zero, Nat.add_eq, add_zero]
theorem List.lastN_eq_reverse_take (n:ℕ) (l: List α): List.lastN n l = (List.take n l.reverse).reverse := by
unfold List.lastN
induction l generalizing n with
| nil => simp only [length_nil, zero_le, ge_iff_le, nonpos_iff_eq_zero, Nat.zero_sub, tsub_eq_zero_of_le, drop_nil,
reverse_nil, take_nil]
| cons head tail ih =>
simp only [length_cons, tsub_le_iff_right, ge_iff_le, reverse_cons, length_reverse]
cases h: decide (n ≤ length tail) with
| false =>
simp only [decide_eq_false_iff_not, not_le] at h
rw [Nat.succ_eq_add_one, Nat.add_comm]
rw [List.take_length_le, List.reverse_append, List.reverse_reverse]
simp only [tsub_le_iff_right, ge_iff_le, reverse_cons, reverse_nil, nil_append, singleton_append]
have heq : 1 + length tail - n = 0 := by
simp only [tsub_le_iff_right, ge_iff_le, zero_le, nonpos_iff_eq_zero, tsub_eq_zero_iff_le]
rw [Nat.add_comm]
apply Nat.le_of_lt_succ
rw [Nat.succ_eq_add_one]
simp only [add_lt_add_iff_right, h]
rw [heq]
simp only [drop]
rw [List.length_append, List.length_reverse]
simp only [length_singleton]
exact h
| true =>
simp only [decide_eq_true_eq] at h
rw [Nat.succ_eq_add_one, Nat.add_comm, Nat.add_sub_assoc, Nat.add_comm, List.drop_cons, ih]
congr 1
rw [List.take_append_of_le_length]
. simp only [length_reverse]; apply h
. apply h
@[simp]
theorem Nat.digitChar_sub_zero_eq_self (n:ℕ) (P: n<10): Char.toNat (Nat.digitChar n) - Char.toNat '0' = n := by
revert n
decide
theorem Nat.sub_self_sub_eq_min (n k:ℕ): n - (n-k) = Nat.min n k := by
conv => left; right; rw [Nat.sub_eq_sub_min]
rw [Nat.sub_sub_self]
simp only [min_le_iff, ge_iff_le, le_refl, true_or]
@[simp]
theorem List.lastN_eq_tail (l: List α): List.lastN (List.length l - 1) l = List.tail l := by
unfold List.lastN
rw [Nat.sub_self_sub_eq_min]
cases l with
| nil => simp only [drop, tail_nil]
| cons hd tl =>
have h: Nat.succ (List.length tl) ≥ 1 := by
apply Nat.succ_le_succ
apply Nat.zero_le
simp only [length_cons, Nat.min_eq_right h, ge_iff_le, drop, tail_cons]
@[simp]
lemma Nat.toDigits_zero (b:ℕ): Nat.toDigits b 0 = ['0'] := by
unfold toDigits toDigitsCore
simp only [_root_.zero_le, ge_iff_le, nonpos_iff_eq_zero, Nat.zero_div, zero_mod, ite_true, List.cons.injEq]
lemma Nat.toDigits_modulo (b n p i:ℕ) (P: i<p) (Q: b>1):
List.getD (List.reverse (Nat.toDigits b (n % b^p))) i '0' = List.getD (List.reverse (Nat.toDigits b n)) i '0' := by
rw [Nat.toDigits_eq_digit_rev, Nat.toDigits_eq_digit_rev]
case P => exact Q
case P => exact Q
congr 1
unfold digit
have hpeq := Nat.sub_add_cancel (le_of_lt P)
conv => left; left; left; rw [← hpeq, pow_add]
rw [Nat.mod_mul_left_div_self, Nat.mod_mod_of_dvd]
apply dvd_pow
. apply dvd_refl
. simp only [min_le_iff, ge_iff_le, tsub_le_iff_right, le_min_iff, _root_.zero_le, nonpos_iff_eq_zero, ne_eq,
tsub_eq_zero_iff_le, not_and, not_le, P, implies_true]
lemma List.getD_ext (P: List.length a = List.length b) (Q: ∀ i, List.getD a i d = List.getD b i d): a = b := by
apply List.ext
intro n
have h:= Q n
unfold getD at h
cases hlt: decide (n < List.length a) with
| true =>
simp only [decide_eq_true_eq] at hlt
have hltb: n < length b := by rw [← P]; exact hlt
simp_all only [zero_le, ge_iff_le, nonpos_iff_eq_zero, hltb, get?_eq_get, Option.getD_some, gt_iff_lt, P]
| false =>
simp only [decide_eq_false_iff_not, not_lt] at hlt
have hltb: n ≥ length b := by rw [← P]; exact hlt
simp only [zero_le, ge_iff_le, nonpos_iff_eq_zero, hlt, List.get?_eq_none.2, hltb]
lemma List.getD_reverse (P: i < List.length l): List.getD (List.reverse l) i d = l[(List.length l - 1 - i)]'(Nat.sub_one_sub_lt P) := by
unfold List.getD
rw [List.get?_reverse, List.get?_eq_get]
simp only [tsub_le_iff_right, ge_iff_le, Option.getD_some, getElem_eq_get]
. exact Nat.sub_one_sub_lt P
. exact P
lemma String.toNatΔ_eq_of_rev_get_eq_aux (P: ∀ i, List.getD a.reverse i '0' = List.getD b.reverse i '0') (Q: List.length a ≤ List.length b): String.toNatΔ a = String.toNatΔ b := by
induction b with
| nil =>
simp only [List.length_nil, zero_le, ge_iff_le, nonpos_iff_eq_zero, List.length_eq_zero] at Q
simp only [Q]
| cons hd tl ih =>
cases heq: decide (List.length a = List.length (hd::tl))
case true =>
simp only [decide_eq_true_eq] at heq
have h: a = (hd::tl) := by
apply List.getD_ext heq (d:='0')
intro n
cases hlt: decide (n < List.length a) with
| true =>
simp only [decide_eq_true_eq] at hlt
have hblt: n < List.length (hd::tl) := by simp_all only [tsub_le_iff_right, ge_iff_le, zero_le, nonpos_iff_eq_zero, tsub_eq_zero_iff_le, heq]
simp only [gt_iff_lt, hlt, List.getD_eq_get, List.getElem_eq_get, hblt]
have Q:= P (List.length a -1 - n)
conv at Q => right; rw [heq]
rw [ List.getD_reverse (Nat.sub_one_sub_lt hlt),
List.getD_reverse (Nat.sub_one_sub_lt hblt)] at Q
simp only [tsub_le_iff_right, ge_iff_le, Nat.sub_sub_self (Nat.le_pred_of_lt hlt), List.getElem_eq_get,
Nat.sub_sub_self (Nat.le_pred_of_lt hblt)] at Q
apply Q
| false =>
simp only [decide_eq_false_iff_not, not_lt] at hlt
have hblt: n ≥ List.length (hd::tl) := by simp_all only [tsub_le_iff_right, ge_iff_le, zero_le, nonpos_iff_eq_zero, tsub_eq_zero_iff_le, heq]
simp only [List.getD_eq_get?, zero_le, ge_iff_le, nonpos_iff_eq_zero, hlt, List.get?_eq_none.2,
Option.getD_none, hblt]
simp only [h]
case false =>
simp only [decide_eq_false_iff_not] at heq
have R := P (List.length tl)
rw [List.getD_eq_default] at R
. rw [List.getD_reverse] at R
. conv => right; unfold toNatΔ toNatAux
simp only [List.length_cons, Nat.succ_sub_succ_eq_sub, tsub_zero, ge_iff_le, zero_le, nonpos_iff_eq_zero,
Nat.sub_self, le_refl, tsub_eq_zero_of_le, List.getElem_eq_get, List.get] at R
rw [String.toNatAux_accumulates, ← toNatΔ, ← R]
simp only [zero_le, ge_iff_le, nonpos_iff_eq_zero, zero_mul, Nat.sub_self, le_refl, tsub_eq_zero_of_le,
add_zero]
apply ih
. intro i
rw [P, List.reverse_cons]
cases h: decide ( i < List.length tl) with
| true =>
simp only [decide_eq_true_eq] at h
rw [List.getD_append]
simp only [List.length_reverse, h]
| false =>
simp only [decide_eq_false_iff_not, not_lt] at h
rw [List.getD_append_right, ←R, List.getD_singleton_default_eq, List.getD_eq_default] <;>
simp only [List.length_reverse, ge_iff_le, h]
. apply Nat.le_of_lt_succ
apply Nat.lt_of_le_of_ne Q heq
. simp only [List.length_cons, Nat.lt_succ_self]
. simp only [List.length_cons] at Q
simp only [List.length_reverse, ge_iff_le]
apply Nat.le_of_lt_succ
apply Nat.lt_of_le_of_ne Q heq
lemma String.toNatΔ_eq_of_rev_get_eq (P: ∀ i, List.getD a.reverse i '0' = List.getD b.reverse i '0'): String.toNatΔ a = String.toNatΔ b := by
cases h: decide (List.length a ≤ List.length b) with
| true =>
simp only [decide_eq_true_eq] at h
apply String.toNatΔ_eq_of_rev_get_eq_aux P h
| false =>
simp only [decide_eq_false_iff_not, not_le] at h
apply Eq.symm
apply (String.toNatΔ_eq_of_rev_get_eq_aux (a:=b) (b:=a) (Q:=le_of_lt h))
intro i
apply Eq.symm
apply P
@[simp]
lemma List.getD_take (P: i < n): List.getD (List.take n l) i d = List.getD l i d := by
conv => right; rw [← List.take_append_drop n l]
cases h: decide (i < List.length l) with
| true =>
simp only [decide_eq_true_eq] at h
rw [List.getD_append]
simp only [length_take, min_le_iff, ge_iff_le, lt_min_iff]
exact ⟨P,h⟩
| false =>
simp only [decide_eq_false_iff_not, not_lt] at h
rw [List.getD_eq_default, List.getD_eq_default]
. simp only [take_append_drop, ge_iff_le, h]
. simp only [length_take, min_le_iff, ge_iff_le, h, or_true]
lemma String.toNatΔ_inv_NattoDigits_tail (b n i:ℕ) (Q: b > 1): String.toNatΔ (List.lastN i (Nat.toDigits b n)) = String.toNatΔ (Nat.toDigits b (n % b^i)) := by
apply String.toNatΔ_eq_of_rev_get_eq
intro ind
simp only [ge_iff_le, List.lastN_eq_reverse_take, List.reverse_reverse]
cases i
case zero =>
simp only [List.take, List.length_nil, zero_le, ge_iff_le, nonpos_iff_eq_zero, List.getD_eq_default,
Nat.zero_eq, pow_zero, Nat.mod_one, Nat.toDigits_zero, List.reverse_cons, List.reverse_nil, List.nil_append,
List.length_singleton, List.getD_singleton_default_eq]
case succ i =>
cases h: decide (ind < Nat.succ i) with
| true =>
simp only [ge_iff_le, decide_eq_true_eq] at h
simp only [h, List.getD_take]
rw [Nat.toDigits_modulo] <;> assumption
| false =>
simp only [decide_eq_false_iff_not, not_lt] at h
rw [List.getD_eq_default, List.getD_eq_default]
. simp only [List.length_reverse, gt_iff_lt, ge_iff_le]
rw [Nat.toDigits_length_eq_log]
. calc
Nat.log b (n % b ^ Nat.succ i) + 1 ≤ Nat.succ i := by
{
apply Nat.succ_le_of_lt
cases heq: n % b ^ Nat.succ i with
| zero => simp only [Nat.zero_eq, zero_le, ge_iff_le, nonpos_iff_eq_zero, Nat.log_zero_right, Nat.succ_pos']
| succ k =>
rw [← heq]
apply Nat.log_lt_of_lt_pow
. simp only [heq, zero_le, ge_iff_le, nonpos_iff_eq_zero, ne_eq, Nat.succ_ne_zero, not_false_iff]
. apply Nat.mod_lt
apply Nat.pos_pow_of_pos
apply Nat.lt_trans Nat.zero_lt_one Q
}
_ ≤ ind := h
. exact Q
. simp only [List.length_take, List.length_reverse, min_le_iff, ge_iff_le, h, true_or]
lemma Nat.toDigits_single_digit (b:ℕ) (n:ℕ) (P: n<b): Nat.toDigits b n = [Nat.digitChar n] := by
unfold toDigits toDigitsCore
simp only [_root_.zero_le, ge_iff_le, nonpos_iff_eq_zero, add_eq, add_zero]
split
. next =>
have h:n % b = n := by exact mod_eq_of_lt P
simp only [h]
. next =>
unfold toDigitsCore
simp only [_root_.zero_le, ge_iff_le, nonpos_iff_eq_zero]
split
. simp only [_root_.zero_le, ge_iff_le, nonpos_iff_eq_zero, zero_mod]
. split
. next h _=> exfalso; apply h; exact div_eq_of_lt P
. next h _=> exfalso; apply h; exact div_eq_of_lt P
@[simp]
theorem String.toNatΔ_inv_NattoDigits (n:ℕ) : String.toNatΔ (Nat.toDigits 10 n) = n := by
induction n using Nat.strong_induction_on with
| h n ih =>
cases n
case zero => decide
case succ n=>
unfold toNatΔ toNatAux
simp only [zero_le, ge_iff_le, nonpos_iff_eq_zero, zero_mul, tsub_le_iff_right, zero_add]
split
. next heq => simp only [Nat.toDigits_ne_nil] at heq
. next s hd tl heq =>
have h: tl = List.lastN (List.length (Nat.toDigits 10 (Nat.succ n)) - 1) (Nat.toDigits 10 (Nat.succ n)) := by
simp only [tsub_le_iff_right, ge_iff_le, List.lastN_eq_tail]
simp only [heq, List.tail_cons]
apply_fun String.toNatΔ at h
rw [String.toNatΔ_inv_NattoDigits_tail] at h
rw [String.toNatAux_accumulates, ← String.toNatΔ]
rw [h, ih]
. simp only [gt_iff_lt, Nat.toDigits_length_eq_log, add_tsub_cancel_right, ge_iff_le, add_le_iff_nonpos_left,
nonpos_iff_eq_zero, Nat.log_eq_zero_iff, or_false, zero_le, tsub_le_iff_right]
apply Eq.symm
rw [Nat.add_comm]
apply Nat.eq_add_of_sub_eq
. apply Nat.mod_le
. conv => left; left; rw [← Nat.mod_add_div (Nat.succ n) (10^Nat.log 10 (Nat.succ n))]
simp only [add_tsub_cancel_left, ge_iff_le, add_le_iff_nonpos_right, nonpos_iff_eq_zero, mul_eq_zero, zero_le,
Nat.log_pos_iff, and_true, tsub_le_iff_right]
have h₂: List.getD (Nat.toDigits 10 (Nat.succ n)) 0 '0' = hd := by
unfold List.getD
simp only [heq, zero_le, ge_iff_le, nonpos_iff_eq_zero, List.cons.injEq, forall_true_left, and_imp,
forall_apply_eq_imp_iff', forall_eq', Option.getD_some, List.get?]
rw [Nat.toDigits_eq_digit] at h₂
have h₃: List.length tl = List.length (Nat.toDigits 10 (Nat.succ n)) -1 := by
simp only [heq, List.length_cons, Nat.succ_sub_succ_eq_sub, tsub_zero, ge_iff_le, zero_le, nonpos_iff_eq_zero]
rw [Nat.toDigits_length_eq_log] at h₃
rw [← h₂, h₃, Nat.digitChar_sub_zero_eq_self, Nat.toDigits_length_eq_log, Nat.digit, Nat.mul_comm]
simp only [add_tsub_cancel_right, ge_iff_le, add_le_iff_nonpos_left, nonpos_iff_eq_zero, Nat.log_eq_zero_iff,
or_false, zero_le, tsub_zero, mul_eq_mul_right_iff, Nat.log_pos_iff, and_true]
left
apply Eq.symm (Nat.mod_eq_of_lt _)
. apply (Nat.div_lt_iff_lt_mul _).2
. rw [← pow_succ]
apply Nat.lt_pow_of_log_lt
. simp only
. simp only [lt_add_iff_pos_right]
. simp only [zero_le, ge_iff_le, nonpos_iff_eq_zero, gt_iff_lt, pow_pos]
. simp only
. simp only [tsub_le_iff_right, ge_iff_le, zero_le, nonpos_iff_eq_zero, tsub_zero, tsub_eq_zero_iff_le,
gt_iff_lt, Nat.digit_lt_base]
. simp only
. simp only
. apply Nat.pos_of_ne_zero
intro hp
apply Nat.toDigits_ne_nil (List.length_eq_zero.1 hp)
. simp only [gt_iff_lt, Nat.toDigits_length_eq_log, add_tsub_cancel_right, ge_iff_le, add_le_iff_nonpos_left,
nonpos_iff_eq_zero, Nat.log_eq_zero_iff, or_false, zero_le]
calc
(Nat.succ n) % 10 ^ Nat.log 10 (Nat.succ n) < 10 ^ Nat.log 10 (Nat.succ n) := by apply Nat.mod_lt; apply Nat.pos_pow_of_pos; simp only
_ ≤ n + 1 := by apply Nat.pow_log_le_self; simp only [zero_le, ge_iff_le, nonpos_iff_eq_zero, ne_eq, Nat.succ_ne_zero, not_false_iff]
. simp only
@[simp]
theorem String.toIntΔ_inv_IntreprΔ (i:ℤ): String.toIntΔ (Int.reprΔ i) = i := by
unfold toIntΔ Int.reprΔ
cases i with
| ofNat n =>
simp only [Int.ofNat_eq_coe]
split
case h_1 s heq =>
simp only [Nat.toDigits_ne_nil
] at heq
case h_2 head tail heq =>
split
case inl h =>
have h₂: (List.all (head::tail) Char.isDigit == true) = true := by
rw [← heq]
apply Nat.toDigits_digits <;> decide
simp at h₂
have ⟨ h₃, _⟩ :=h₂
simp only [h] at h₃
. simp only [← heq, toNatΔ_inv_NattoDigits]
| negSucc n =>
simp only [List.singleton_append, toNatΔ_inv_NattoDigits, Nat.cast_succ, neg_add_rev, ite_true,
Int.negSucc_eq]
lemma List.eq_append_of_getRest [DecidableEq α] {l l₁ l₂: List α} (P: List.getRest l l₁ = some l₂): l = l₁ ++ l₂ := by
induction l₁ generalizing l l₂ with
| nil =>
unfold getRest at P
simp only [Option.some.injEq] at P
simp only [P, nil_append]
| cons head tail ih =>
unfold getRest at P
split at P
case h_1 heq => simp only at heq
case h_2 => simp only at P
case h_3 hd tl y l₁ heq =>
split at P
case inr heq₂ => contradiction
case inl heq₂ =>
injection heq
subst hd y l₁
simp only [cons_append, cons.injEq, true_and]
apply ih
apply P
@[simp]
lemma List.getRest_nil [DecidableEq α] {l: List α}: List.getRest l [] = l := by
unfold getRest
simp only
@[simp]
lemma List.getRest_delim_append [DecidableEq α] {l₁ l₂: List α}: List.getRest (l₁ ++ l₂) l₁ = some l₂ := by
induction l₁ with
| nil => simp only [nil_append, getRest_nil]
| cons head tail ih =>
unfold getRest
simp only [cons_append, ih, ite_true]
@[simp]
lemma List.nil_isInfix: [] <:+: l := by
unfold List.isInfix
exists []
exists l
@[simp]
lemma List.nil_isPrefix: [] <+: l := by
unfold List.isPrefix
exists l
@[simp]
lemma List.nil_isSuffix: [] <:+ l := by
unfold List.isSuffix
simp only [append_nil, exists_eq]
lemma List.isInfix_cons {head:α} {l tail: List α} (h: l <:+: tail): l <:+: head::tail := by
unfold List.isInfix at *
match h with
| ⟨s, t, P⟩ =>
exists head::s, t
simp only [cons_append, append_assoc, ← P]
@[simp]
lemma List.isInfix_append_left (l₁ l₂:List α): l₁ <:+: (l₁ ++ l₂) := by exact ⟨ [], l₂, rfl⟩
@[simp]
lemma List.isInfix_append_right (l₁ l₂:List α): l₂ <:+: (l₁ ++ l₂) := by exact ⟨ l₁, [], List.append_nil _⟩
@[simp]
lemma List.isInfix_self: l <:+: l := by exact ⟨[], [], List.append_nil _⟩
@[simp]
lemma List.getRest_none [DecidableEq α] {l₁ l₂:List α}: List.getRest l₁ l₂ = none ↔ ¬ l₂ <+: l₁ := by
apply iff_not_comm.1
rw [← ne_eq, Option.ne_none_iff_exists]
apply Iff.intro
. intro ⟨l₃, h⟩
subst h
exists l₃
exact Eq.symm getRest_delim_append
. intro ⟨l₃, h⟩
exists l₃
exact Eq.symm (eq_append_of_getRest (Eq.symm h))
theorem List.sizeOf_getRest [DecidableEq α] {l l₁ l₂: List α} (h: List.getRest l l₁ = some l₂) : sizeOf l₂ = 1 + sizeOf l - sizeOf l₁ := by
induction l generalizing l₁ l₂ with
| nil =>
unfold getRest at h
cases l₁
. simp only [Option.some.injEq] at h
subst h
simp only [nil.sizeOf_spec, add_tsub_cancel_right, ge_iff_le]
. simp only at h
| cons head tail ih =>
unfold getRest at h
split at h <;> try contradiction
case h_1 heq => injection h; simp_all only [tsub_le_iff_right, ge_iff_le, nil.sizeOf_spec, add_tsub_cancel_left, add_le_iff_nonpos_right,
nonpos_iff_eq_zero, zero_le]
case h_3 heq =>
split at h <;> try contradiction
case inl heq₂ =>
injection heq
subst_vars
simp only [ih h, tsub_le_iff_right, ge_iff_le, cons.sizeOf_spec, sizeOf_default, add_zero, zero_le,
nonpos_iff_eq_zero, Nat.add_sub_add_left, add_le_add_iff_left]
theorem List.sizeOf_pos (l:List α): sizeOf l > 0 := by
cases l <;> simp only [cons.sizeOf_spec, nil.sizeOf_spec, sizeOf_default, add_zero, zero_le, ge_iff_le, nonpos_iff_eq_zero, gt_iff_lt,
add_pos_iff, true_or]
def List.splitOnListAux [DecidableEq α] (delim: List α) (l:List α) (acc: Array α) (r: Array (Array α)) (delim_nonempty: delim ≠ []): (Array (Array α)) :=
match _h₀: l with
| [] => r.push acc
| head::tail =>
match h: getRest l delim with
| none =>
List.splitOnListAux delim tail (acc.push head) r delim_nonempty
| some rest =>
have _: sizeOf rest < sizeOf l := by
rw [List.sizeOf_getRest h]
cases delim with
| nil => contradiction
| cons hd tail =>
simp only [cons.sizeOf_spec, sizeOf_default, add_zero, zero_le, ge_iff_le, nonpos_iff_eq_zero,
Nat.add_sub_add_left, tsub_le_iff_right, add_le_add_iff_left]
apply Nat.sub_lt (List.sizeOf_pos l) (List.sizeOf_pos tail)
List.splitOnListAux delim rest #[] (r.push acc) delim_nonempty
decreasing_by try simp_wf; try decreasing_tactic
def List.splitOnList [DecidableEq α] (delim: List α) (l: List α): List (List α) :=
match delim with
| [] => [l]
| head::tail =>
Array.toList (Array.map Array.toList (splitOnListAux (head::tail) l #[] #[] (by simp only [ne_eq])))