-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenFib.v
1463 lines (1284 loc) · 40.2 KB
/
GenFib.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
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
(** * Fib : Fibonacci sequence and decomposition *)
Require Import MoreList DeltaList.
Import ListNotations.
Set Implicit Arguments.
(** * Generalized Fibonacci sequence *)
Fixpoint A (q n : nat) :=
match n with
| O => 1
| S n => A q n + A q (n-q)
end.
(**
- q=0 : binary numbers
- q=1 : Fibonacci numbers (non-standard indexes)
1 2 3 5 8 ...
- q=2 : Narayana’s Cows, see OEIS A930 (shifted)
- q=3 : See OEIS A003269 (shifted)
*)
(*
Compute take 10 (A 0).
Compute take 10 (A 1).
Compute take 10 (A 2).
Compute take 10 (A 3).
*)
(*
A 0 : [1; 2; 4; 8; 16; 32; 64; 128; 256; 512]
A 1 : [1; 2; 3; 5; 8; 13; 21; 34; 55; 89]
A 2 : [1; 2; 3; 4; 6; 9; 13; 19; 28; 41]
A 3 : [1; 2; 3; 4; 5; 7; 10; 14; 19; 26]
*)
Lemma A_q_0 q : A q 0 = 1.
Proof.
reflexivity.
Qed.
Lemma A_S q n : A q (S n) = A q n + A q (n-q).
Proof.
reflexivity.
Qed.
Lemma A_base q n : n <= S q -> A q n = S n.
Proof.
induction n; auto.
simpl. intros.
replace (n-q) with 0 by lia. simpl.
rewrite IHn; lia.
Qed.
Lemma S_sub_1 p : S p - 1 = p.
Proof.
lia.
Qed.
Lemma A_sum q n : n<>0 -> A q n = A q (n-1) + A q (n-S q).
Proof.
destruct n.
- now destruct 1.
- intros _. now rewrite S_sub_1.
Qed.
Lemma A_sum' q n : A q (n+S q) = A q n + A q (n+q).
Proof.
intros.
rewrite A_sum by lia.
rewrite Nat.add_comm. f_equal; f_equal; lia.
Qed.
Lemma A_q_1 q : A q 1 = 2.
Proof.
reflexivity.
Qed.
Lemma A_0_pow2 n : A 0 n = 2^n.
Proof.
induction n.
- reflexivity.
- rewrite A_S, Nat.sub_0_r. simpl. lia.
Qed.
Lemma A_nz q n : 1 <= A q n.
Proof.
induction n; simpl; auto with arith.
Qed.
Lemma A_lt_S q n : A q n < A q (S n).
Proof.
simpl. generalize (A_nz q (n-q)). lia.
Qed.
Lemma A_lt q n n' : n < n' -> A q n < A q n'.
Proof.
induction 1.
- apply A_lt_S.
- transitivity (A q m); trivial. apply A_lt_S.
Qed.
Lemma A_mono q n n' : n <= n' -> A q n <= A q n'.
Proof.
induction 1; trivial.
transitivity (A q m); trivial. apply Nat.lt_le_incl, A_lt_S.
Qed.
Lemma A_S_le_twice q n : A q (S n) <= 2 * A q n.
Proof.
rewrite A_S. simpl. generalize (@A_mono q (n-q) n). lia.
Qed.
Lemma A_inj q n n' : A q n = A q n' -> n = n'.
Proof.
intros.
destruct (lt_eq_lt_dec n n') as [[LT|EQ]|LT]; trivial;
apply (A_lt q) in LT; lia.
Qed.
Lemma A_lt_inv q n n' : A q n < A q n' -> n < n'.
Proof.
intros.
destruct (le_lt_dec n' n) as [LE|LT]; trivial.
apply (A_mono q) in LE. lia.
Qed.
Lemma A_le_inv q x y : A q x <= A q y -> x <= y.
Proof.
rewrite Nat.lt_eq_cases. intros [LT|EQ].
- apply A_lt_inv in LT. lia.
- apply A_inj in EQ. lia.
Qed.
Lemma A_lt_id q n : n < A q n.
Proof.
induction n; simpl; auto.
generalize (A_nz q (n-q)). lia.
Qed.
Lemma A_base_iff q n : n <= S q <-> A q n = S n.
Proof.
split. apply A_base.
intros.
destruct n; auto with arith.
rewrite A_S in H.
generalize (A_lt_id q n).
generalize (A_lt_id q (n-q)).
lia.
Qed.
Lemma A_high q n : S q < n <-> S n < A q n.
Proof.
generalize (A_base_iff q n) (A_lt_id q n). lia.
Qed.
Lemma A_Sq_le q n : A (S q) n <= A q n.
Proof.
induction n as [[|n] IH] using lt_wf_ind; auto.
rewrite !A_S.
transitivity (A q n + A q (n - S q)).
- apply Nat.add_le_mono; apply IH; lia.
- apply Nat.add_le_mono; auto. apply A_mono. lia.
Qed.
Lemma A_Sq_lt q n : S q < n -> A (S q) n < A q n.
Proof.
intros LT.
replace n with (q + 2 + (n - q - 2)) by lia.
set (m := n - q - 2). clearbody m. clear LT n.
induction m.
- rewrite Nat.add_0_r.
rewrite A_base by lia.
rewrite Nat.add_succ_r.
rewrite A_S.
rewrite A_base by lia.
replace (q+1-q) with 1 by lia. rewrite A_q_1. lia.
- rewrite Nat.add_succ_r.
rewrite !A_S.
apply Nat.add_lt_le_mono; auto. clear IHm.
replace (q + 2 + m - S q) with (S m) by lia.
replace (q + 2 + m - q) with (S (S m)) by lia.
transitivity (A q (S m)). apply A_Sq_le. apply A_mono; lia.
Qed.
(* (A q) is sub-multiplicative
Note : Thanks to Fekete theorem, this implies that
[(ln (A q n))/n] has a finite limit when n grows. But without
extra details about the next term in the asymptotic development,
we cannot say much more this way, especially nothing on the limit
of the ratio [A q (S n) / A q n] or even its existence.
*)
Lemma A_submult q p n : A q (n+p) <= A q n * A q p.
Proof.
induction n as [[|n] IH] using lt_wf_ind.
- cbn. lia.
- destruct (Nat.le_gt_cases q n).
+ cbn.
assert (IHn := IH n).
assert (IHnq := IH (n-q)).
replace (n-q+p) with (n+p-q) in IHnq; lia.
+ rewrite (@A_base q (S n)) by lia.
(* apply A_shift_mult. lia. *)
cbn - ["*"].
assert (IHn := IH n).
rewrite (@A_base q n) in IHn by lia.
assert (LE : n + p - q <= p) by lia.
apply (A_mono q) in LE. lia.
Qed.
(* After the "base" zone, a "triangular" zone *)
Definition triangle q := q*(q+1)/2.
Lemma qSq_even q : q*(q+1) mod 2 = 0.
Proof.
destruct (Nat.Even_or_Odd q) as [(l,->)|(l,->)].
- rewrite <- Nat.mul_assoc, Nat.mul_comm.
apply Nat.mod_mul; auto.
- replace (2*l+1+1) with (2*(l+1)) by lia.
rewrite (Nat.mul_comm 2 (l+1)), Nat.mul_assoc.
apply Nat.mod_mul; auto.
Qed.
Lemma double_triangle q : 2 * triangle q = q*(q+1).
Proof.
rewrite (Nat.div_mod (q * (q+1)) 2); auto. now rewrite qSq_even.
Qed.
Lemma triangle_succ q : triangle (S q) = triangle q + S q.
Proof.
apply Nat.mul_cancel_l with 2; auto.
rewrite Nat.mul_add_distr_l.
rewrite !double_triangle. lia.
Qed.
Lemma triangle_aboveid q : q <= triangle q.
Proof.
induction q; auto. rewrite triangle_succ. lia.
Qed.
Lemma A_triangle q n : n <= q+2 -> A q (q + n) = S q + triangle n.
Proof.
induction n.
- intros _. rewrite A_base by lia. reflexivity.
- intros LE.
rewrite Nat.add_succ_r, A_S.
replace (q + n - q) with n by lia.
rewrite (@A_base q n) by lia.
rewrite triangle_succ.
rewrite IHn; lia.
Qed.
(* Just after the triangular zone : *)
Lemma A_2q3_eqn q : A q (2*q+3) = A q (2*q+2) + A q (S q) + 2.
Proof.
rewrite Nat.add_succ_r, A_S.
rewrite <- Nat.add_assoc. f_equal.
replace (2*q+2-q) with (S (S q)) by lia.
rewrite A_S. f_equal.
replace (S q - q) with 1 by lia.
apply A_q_1.
Qed.
Lemma A_2q3_tri q : A q (2*q+3) = triangle (q+4) - 2.
Proof.
rewrite A_2q3_eqn.
replace (2*q+2) with (q+S (S q)) by lia.
rewrite A_triangle, A_base by lia.
replace (q+4) with (S (S (S (S q)))) by lia.
rewrite (triangle_succ (S (S (S q)))).
rewrite (triangle_succ (S (S q))). lia.
Qed.
(* Behavior of A diagonals :
decreasing then flat at [n=2*q+3] then [+1] steps. *)
Lemma A_diag_eq q n : n = 2*q+3 -> A (S q) (S n) = A q n.
Proof.
intros ->.
replace (S (2*q + 3)) with (S q + S (q+2)) by lia.
rewrite A_triangle by lia.
rewrite A_2q3_tri.
replace (q+4) with (S (S (q+2))) by lia.
rewrite (triangle_succ (S (q+2))). lia.
Qed.
Lemma A_diag_step q n : n < 2*q+3 -> A (S q) (S n) = S (A q n).
Proof.
induction n.
- intros _. now rewrite A_q_1, A_q_0.
- intros LT.
rewrite A_S. simpl Nat.sub.
rewrite IHn by lia. rewrite A_S. simpl. f_equal. f_equal.
rewrite !A_base; lia.
Qed.
Lemma A_diag_decr q n : 2*q+3 < n -> A (S q) (S n) < A q n.
Proof.
intros LT.
replace n with (2*q+4+(n-2*q-4)) by lia.
set (m := n-2*q-4). clearbody m. clear n LT.
induction m.
- rewrite !Nat.add_0_r.
rewrite A_S. rewrite Nat.add_succ_r, A_diag_eq by lia.
replace (S(2*q+3)-S q) with (q+3) by lia.
rewrite A_S.
replace (2*q+3-q) with (q+3) by lia.
apply Nat.add_lt_mono_l.
apply A_Sq_lt. lia.
- rewrite Nat.add_succ_r, (A_S (S q)), (A_S q).
apply Nat.add_lt_le_mono; auto using A_Sq_le.
Qed.
Lemma A_diag_decr_exact q n : 2*q+3 <= n <= 3*q+3 ->
A q n = A (S q) (S n) + ((n-2*q-3)*(n-2*q))/2.
Proof.
induction n as [n IH] using lt_wf_ind.
intros Hn.
destruct (Nat.eq_dec n (2*q+3)) as [E|N].
- replace (n-2*q-3) with 0 by lia. simpl "/". now rewrite A_diag_eq.
- replace n with (S (n-1)) at 1 by lia. simpl A.
rewrite (IH (n-1)) by lia.
replace (S (n-1)) with n by lia.
rewrite <- !Nat.add_assoc. f_equal.
replace (n-S q) with (S (n-q-2)) by lia.
rewrite A_diag_step by lia.
replace (n-1-q) with (S (n-q-2)) by lia.
rewrite A_S. rewrite (@A_base q (n-q-2-q)) by lia.
rewrite Nat.add_shuffle3. rewrite !Nat.add_succ_r, Nat.add_succ_l.
f_equal. f_equal.
set (m := n-1-2*q-3).
replace (n-2*q-3) with (m+1) by lia.
replace (n-q-2-q) with (m+2) by lia.
replace (n-1-2*q) with (m+3) by lia.
replace (n-2*q) with (m+4) by lia.
replace ((m+1)*(m+4)) with (m*(m+3)+(m+2)*2) by ring.
rewrite Nat.div_add; lia.
Qed.
(* Inverting A *)
Fixpoint invA q n :=
match n with
| 0 => 0
| S n =>
let p := invA q n in
if S (S n) =? A q (S p) then S p else p
end.
Lemma invA_spec q n : A q (invA q n) <= S n < A q (S (invA q n)).
Proof.
induction n as [|n IH].
- simpl. auto.
- cbn -[A Nat.eqb].
case Nat.eqb_spec; [intros E|intros; lia].
split. lia. rewrite E. apply A_lt_S.
Qed.
Lemma invA_spec' q n p : A q p <= S n < A q (S p) -> invA q n = p.
Proof.
intros H.
assert (H' := invA_spec q n).
assert (p < S (invA q n)) by (apply (A_lt_inv q); lia).
assert (invA q n < S p) by (apply (A_lt_inv q); lia).
lia.
Qed.
Lemma A_inv q n : { p | A q p <= S n < A q (S p) }.
Proof.
exists (invA q n). apply invA_spec.
Defined.
Lemma A_inv' q n : n<>0 -> { p | A q p <= n < A q (S p) }.
Proof.
destruct n.
- now destruct 1.
- intros _. apply A_inv.
Defined.
Lemma invA_A q p : invA q (A q p - 1) = p.
Proof.
apply invA_spec'.
replace (S (A q p -1)) with (A q p) by (generalize (A_nz q p); lia).
split; auto. apply A_lt. lia.
Qed.
Lemma invA_A' q p : p<>0 -> invA q (A q p - 2) = p-1.
Proof.
intros Hq.
apply invA_spec'.
assert (2 <= A q p) by (apply (@A_mono q 1 p); lia).
replace (S (A q p - 2)) with (A q p - 1) by lia.
replace (S (p-1)) with p by lia.
split; try lia.
generalize (@A_lt q (p-1) p). lia.
Qed.
(** Any number has a [A] number above it. *)
Definition invA_up q n := S (invA q (n-2)).
Lemma invA_up_spec q n : n <= A q (invA_up q n).
Proof.
unfold invA_up. destruct (invA_spec q (n-2)) as (_,H). lia.
Qed.
Lemma invA_up_A q n : n<>0 -> invA_up q (A q n) = n.
Proof.
intros Hn. unfold invA_up. rewrite invA_A'; lia.
Qed.
(** * Decomposition via sums of Aq numbers.
Zeckendorf's theorem (actually discovered earlier by
Lekkerkerker) states that any natural number can be obtained
by a sum of distinct Fibonacci numbers, and this decomposition
is moreover unique when Fibonacci numbers in the decomposition
aren't consecutive.
This is the generalization to Aq numbers.
*)
(** ** The [sumA] function
We represent a decomposition by the list of indexes
of the Aq numbers in this decomposition.
The sumA function is the sum of the Fibonacci numbers of
these indexes. For the first results below, the indexes may be
arbitrary : redundant, in any order, ... *)
Definition sumA q l := fold_right (fun n acc => A q n + acc) 0 l.
Lemma sumA_cons q a l : sumA q (a::l) = A q a + sumA q l.
Proof.
reflexivity.
Qed.
Lemma sumA_eqn q l :
sumA q l + sumA q (map (decr q) l) = sumA q (map S l).
Proof.
induction l; trivial.
simpl map. rewrite !sumA_cons, <- IHl, A_S.
unfold decr at 1. lia.
Qed.
Lemma sumA_eqn' q l :
sumA q (map S l) - sumA q (map (decr q) l) = sumA q l.
Proof.
rewrite <- sumA_eqn. apply Nat.add_sub.
Qed.
Lemma sumA_eqn_pred q l :
~In 0 l ->
sumA q l = sumA q (map (decr 1) l) + sumA q (map (decr (S q)) l).
Proof.
induction l; trivial.
simpl map. simpl. intros. rewrite IHl by intuition.
unfold decr at 3 5.
rewrite (@A_sum q a); lia.
Qed.
Lemma sumA_app q l l' : sumA q (l++l') = sumA q l + sumA q l'.
Proof.
revert l'.
induction l; intros.
- trivial.
- simpl. rewrite IHl. lia.
Qed.
Lemma sumA_rev q l : sumA q (rev l) = sumA q l.
Proof.
induction l.
- trivial.
- simpl. rewrite sumA_app, IHl. simpl. lia.
Qed.
Lemma sumA_0 q l : sumA q l = 0 <-> l = [].
Proof.
split.
- intros H. destruct l; [ now elim H |].
simpl in *. generalize (A_nz q n). lia.
- intros; now subst.
Qed.
Lemma sumA_in_le q l x : In x l -> A q x <= sumA q l.
Proof.
induction l; simpl.
- inversion 1.
- intros [<-|IN]; auto with arith.
transitivity (sumA q l); auto with arith.
Qed.
(** ** Zeckendorf's Theorem *)
(** All numbers can be decomposed as a sum of A numbers.
Existence of the decomposition is given by the [decomp] function below
(small terms first). *)
Fixpoint decomp q n :=
match n with
| 0 => []
| S n =>
let p := invA q n in
decomp q (n - (A q p - 1)) ++ [p]
end.
Lemma decomp_sum q n :
sumA q (decomp q n) = n.
Proof.
induction n as [[|n] IH] using lt_wf_rec; trivial.
cbn - [invA sumA].
destruct (invA_spec q n) as (H,_).
set (p := invA q n) in *.
rewrite sumA_app. rewrite IH by lia. simpl.
generalize (A_nz q p). lia.
Qed.
Lemma decomp_in q n x : In x (decomp q n) -> A q x <= n.
Proof.
induction n as [[|n] IH] using lt_wf_rec; try easy.
cbn - [invA sumA]. rewrite in_app_iff. intros [IN|[<-|[ ]]].
apply IH in IN; lia.
apply invA_spec.
Qed.
Lemma decomp_delta q n : Delta (S q) (decomp q n).
Proof.
induction n as [[|n] IH] using lt_wf_rec; autoh.
cbn - [invA sumA].
destruct (invA_spec q n) as (H,H').
set (p := invA q n) in *.
apply Delta_app_iff; repeat split; autoh.
- apply IH. lia.
- intros x x' IN [<-|[ ]].
apply decomp_in in IN.
cut (x < p - q); [lia|].
apply (A_lt_inv q).
rewrite A_S in H'. lia.
Qed.
#[global] Hint Resolve decomp_sum decomp_delta : hof.
Lemma decomp_exists q n :
{ l | sumA q l = n /\ Delta (S q) l }.
Proof.
exists (decomp q n). split. apply decomp_sum. apply decomp_delta.
Qed.
(** Technical lemma for uniqueness of decomposition :
A canonical decomposition cannot excess the next Aq. *)
Lemma decomp_max q n l :
DeltaRev (S q) (n::l) ->
sumA q (n::l) < A q (S n).
Proof.
revert n.
induction l.
- intros n _. simpl sumA. rewrite Nat.add_0_r. apply A_lt_S.
- intros n.
inversion 1; subst. simpl sumA.
rewrite A_S.
apply Nat.add_lt_mono_l.
apply Nat.lt_le_trans with (A q (S a)).
+ apply IHl; auto.
+ apply A_mono; lia.
Qed.
Lemma rev_switch {A} (l l' : list A) : rev l = l' -> l = rev l'.
Proof.
intros. now rewrite <- (rev_involutive l), H.
Qed.
Lemma sumA_below q l p : Delta (S q) l -> Below l p -> sumA q l < A q p.
Proof.
intros D B.
destruct (rev l) as [|a rl'] eqn:E''.
- apply rev_switch in E''. subst l. simpl.
generalize (@A_nz q p). lia.
- rewrite <- sumA_rev, E''.
apply Nat.lt_le_trans with (A q (S a)).
+ apply decomp_max. rewrite <- E''. now apply DeltaRev_rev.
+ apply A_mono. specialize (B a).
rewrite in_rev, E'' in B. simpl in B. intuition.
Qed.
(** Uniqueness. Easier to prove on lists with large terms first. *)
Lemma decomp_unique_rev q l l' :
DeltaRev (S q) l ->
DeltaRev (S q) l' ->
sumA q l = sumA q l' -> l = l'.
Proof.
revert l'.
induction l as [|n l IH]; destruct l' as [|n' l'].
- trivial.
- intros _ Hn'. simpl in *. generalize (A_nz q n'); lia.
- intros Hn _. simpl in *. generalize (A_nz q n); lia.
- intros DR DR' EQ.
assert (n < S n').
{ apply (A_lt_inv q). simpl in EQ.
apply Nat.le_lt_trans with (A q n' + sumA q l'); [lia|].
now apply decomp_max. }
assert (n' < S n).
{ apply (A_lt_inv q). simpl in EQ.
apply Nat.le_lt_trans with (A q n + sumA q l); [lia|].
now apply decomp_max. }
replace n' with n in * by lia. clear H H0.
simpl in EQ.
f_equal.
apply IH; clear IH; simpl in *; try lia; intuition.
+ apply DeltaRev_alt in DR. intuition.
+ apply DeltaRev_alt in DR'. intuition.
Qed.
Lemma decomp_unique q l l' :
Delta (S q) l -> Delta (S q) l' -> sumA q l = sumA q l' -> l = l'.
Proof.
intros D D' EQ.
rewrite <- (rev_involutive l), <- (rev_involutive l'). f_equal.
apply (@decomp_unique_rev q).
- now apply DeltaRev_rev.
- now apply DeltaRev_rev.
- now rewrite !sumA_rev.
Qed.
Lemma decomp_carac q n l :
Delta (S q) l -> sumA q l = n -> decomp q n = l.
Proof.
intros D Eq. apply (@decomp_unique q); autoh.
now rewrite decomp_sum.
Qed.
#[global] Hint Resolve decomp_carac : hof.
Lemma decomp_sum' q l :
Delta (S q) l -> decomp q (sumA q l) = l.
Proof.
intros D. now apply decomp_carac.
Qed.
Lemma decomp_low q n : 1 <= n <= q+2 -> decomp q n = [n-1].
Proof.
intros.
apply decomp_carac. constructor. cbn. rewrite A_base; lia.
Qed.
Lemma decomp_plus_A q n p :
p < A q (n-q) -> decomp q (p + A q n) = decomp q p ++ [n].
Proof.
intros LT.
apply decomp_carac.
- apply Delta_app_iff; repeat split;
[apply decomp_delta|constructor|].
intros x x' Hx [<-|[ ]].
apply decomp_in in Hx.
assert (LT' := Nat.le_lt_trans _ _ _ Hx LT).
apply A_lt_inv in LT'. lia.
- rewrite sumA_app, decomp_sum. simpl. lia.
Qed.
(** ** Normalisation of a Fibonacci decomposition.
Starting from an relaxed decomposition (with gaps
of at least [q]), we can transform it into a canonical
decomposition (with gaps of at least [S q]),
by simply saturating the basic equation
[A q n + A q (n-q) = A q (S n)]
in the right order (highest terms first).
Termination isn't obvious for Coq, since we might have
to re-launch normalisation on by-products of a first
normalisation. The number of terms in the decomposition
decreases strictly during the process, we use that to
justify the termination.
Moreover, the lowest term in the decomposition grows by
steps of [S q] during the process (or stays equal).
*)
Fixpoint renorm_loop q l n :=
match n with
| 0 => []
| S n =>
match l with
| [] => []
| p::l =>
match renorm_loop q l n with
| [] => [p]
| p'::l' =>
if p+q =? p' then
renorm_loop q (S p' :: l') n
else
p::p'::l'
end
end
end.
Definition renorm q l := renorm_loop q l (length l).
(*
Compute renorm 1 [0;1;3;4;5;7]. (* [4; 8] *)
Compute renorm 2 [1;3;5;8]. (* [1; 9] *)
*)
Lemma renorm_loop_length q l n :
length l <= n -> length (renorm_loop q l n) <= length l.
Proof.
revert l.
induction n; simpl; auto with arith.
intros [|p l] LE; simpl in *; auto.
apply Nat.succ_le_mono in LE.
assert (Hl := IHn l LE).
destruct renorm_loop as [|p' l']. simpl in *; try lia.
case Nat.eqb_spec; intros; simpl in *; try lia.
etransitivity; try eapply IHn; simpl; lia.
Qed.
Lemma renorm_length q l : length (renorm q l) <= length l.
Proof.
unfold renorm. now apply renorm_loop_length.
Qed.
#[global] Hint Resolve renorm_length : hof.
Lemma renorm_loop_sum q l n :
length l <= n -> sumA q (renorm_loop q l n) = sumA q l.
Proof.
revert l.
induction n; intros [|p l]; simpl; auto.
- inversion 1.
- intros LE. apply Nat.succ_le_mono in LE.
assert (Hl := IHn l LE).
assert (L := renorm_loop_length q l LE).
destruct renorm_loop as [|p' l']; simpl in *; try lia.
case Nat.eqb_spec; simpl in *; intros; try lia.
rewrite IHn by (simpl;lia). simpl.
replace (p'-q) with p; lia.
Qed.
Lemma renorm_sum q l : sumA q (renorm q l) = sumA q l.
Proof.
unfold renorm. now apply renorm_loop_sum.
Qed.
#[global] Hint Resolve renorm_sum : hof.
Definition HeadStep q l l' := match l, l' with
| [], [] => True
| p::_, p'::_ => exists m, p' = p + m*(S q)
| _, _ => False
end.
Lemma renorm_loop_head q l n :
length l <= n -> HeadStep q l (renorm_loop q l n).
Proof.
revert l.
induction n; intros [|p l]; simpl; auto.
- inversion 1.
- intros LE. apply Nat.succ_le_mono in LE.
assert (Hd := IHn l LE).
assert (L := renorm_loop_length q l LE).
destruct renorm_loop as [|p' l']; simpl in *.
+ now exists 0.
+ case Nat.eqb_spec; simpl in *; intros.
* specialize (IHn (S p'::l')).
destruct renorm_loop; simpl in *; try lia.
destruct IHn as (m,E); try lia.
exists (S m). simpl. lia.
* exists 0; lia.
Qed.
Lemma renorm_head q l : HeadStep q l (renorm q l).
Proof.
unfold renorm. now apply renorm_loop_head.
Qed.
Lemma renorm_loop_delta q l n :
length l <= n -> Delta q l -> Delta (S q) (renorm_loop q l n).
Proof.
revert l.
induction n; intros [|p l] LE D; simpl in *; autoh.
apply Nat.succ_le_mono in LE.
apply Delta_alt in D. destruct D as (D,IN).
assert (D' := IHn l LE D).
assert (LE' := renorm_loop_length q l LE).
assert (Hd := renorm_loop_head q l LE).
destruct renorm_loop as [|p' l']; simpl in *; autoh.
case Nat.eqb_spec; simpl in *; intros.
- apply IHn; simpl; autoh; lia.
- destruct l as [|x l]; simpl in *; [intuition|].
destruct Hd as (m,Hd).
constructor; auto.
assert (p+q <= x). { apply IN; auto. }
lia.
Qed.
Lemma renorm_delta q l : Delta q l -> Delta (S q) (renorm q l).
Proof.
unfold renorm. now apply renorm_loop_delta.
Qed.
#[global] Hint Resolve renorm_delta : hof.
Lemma renorm_nop q l : Delta (S q) l -> renorm q l = l.
Proof.
intros.
rewrite <- (@decomp_carac q (sumA q l) (renorm q l)); auto with hof.
Qed.
Lemma renorm_le q x l : Delta q (x::l) ->
forall y, In y (renorm q (x::l)) -> x <= y.
Proof.
intros D y Hy.
apply renorm_delta in D.
assert (Hd := renorm_head q (x::l)).
destruct renorm as [|p l'].
- elim Hy.
- destruct Hd as (m,Hd).
transitivity p.
+ subst; auto with arith.
+ apply Delta_alt in D.
simpl in Hy. destruct Hy as [->|Hy]; auto.
destruct D as (_,IN'). specialize (IN' y Hy). lia.
Qed.
Lemma renorm_loop_mapS q l n :
map S (renorm_loop q l n) = renorm_loop q (map S l) n.
Proof.
revert l.
induction n; trivial; intros [|p l]; trivial.
simpl in *.
rewrite <- IHn.
destruct (renorm_loop q l n) eqn:E; simpl; trivial.
case Nat.eqb_spec; intros. apply IHn. trivial.
Qed.
Lemma renorm_mapS q l : map S (renorm q l) = renorm q (map S l).
Proof.
unfold renorm. rewrite map_length. apply renorm_loop_mapS.
Qed.
Lemma renorm_loop_mapdecr q m l n : m <= q -> length l <= n ->
sumA q (map (decr m) (renorm_loop q l n)) =
sumA q (map (decr m) l).
Proof.
intros Hm.
revert l.
induction n; intros [|p l] LE; simpl in *; auto.
- inversion LE.
- apply Nat.succ_le_mono in LE.
rewrite <- (IHn l LE).
assert (LE' := renorm_loop_length q l LE).
assert (Hd := renorm_loop_head q l LE).
destruct renorm_loop as [|p' l']; simpl in *; auto.
case Nat.eqb_spec; simpl in *; intros; auto.
rewrite IHn by (simpl; lia).
subst p'. rewrite <- Nat.add_succ_r. simpl.
rewrite Nat.add_assoc. f_equal.
unfold decr.
rewrite A_sum by lia.
rewrite Nat.add_comm. f_equal; f_equal; lia.
Qed.
Lemma renorm_mapdecr q m l : m <= q ->
sumA q (map (decr m) (renorm q l)) = sumA q (map (decr m) l).
Proof.
unfold renorm. intros. now apply renorm_loop_mapdecr.
Qed.
Definition LeHd m l :=
match l with [] => True | a::_ => m <= a end.
Lemma renorm_loop_mapdecr' q m l n :
length l <= n ->
Delta q l ->
LeHd (m-q) l ->
sumA q (map (decr m) (renorm_loop q l n)) =
sumA q (map (decr m) l).
Proof.
revert l.
induction n; intros [|a l] LE D H; simpl in *; try lia.
apply Nat.succ_le_mono in LE.
apply Delta_alt in D. destruct D as (D,IN).
assert (LH : LeHd (m-q) l).
{ destruct l as [|x l]; simpl in *; trivial.
assert (a+q <= x) by auto. lia. }
rewrite <- (IHn l LE D LH).
assert (LE' := renorm_loop_length q l LE).
assert (Hd := renorm_loop_head q l LE).
assert (D' := @renorm_loop_delta q l n LE D).
destruct renorm_loop as [|p' l']; simpl in *; auto.
case Nat.eqb_spec; simpl in *; intros; auto.
rewrite IHn; autoh; try (simpl; lia).
subst p'. rewrite <- Nat.add_succ_r. simpl.
rewrite Nat.add_assoc. f_equal.
unfold decr.
rewrite A_sum by lia.
rewrite Nat.add_comm. f_equal; f_equal; lia.
Qed.
Lemma renorm_mapdecr' q m l :
Delta q l -> LeHd (m-q) l ->
sumA q (map (decr m) (renorm q l)) = sumA q (map (decr m) l).
Proof.
unfold renorm. intros. now apply renorm_loop_mapdecr'.
Qed.
Lemma renorm_low q l : Delta (S q) (q::l) ->
renorm q (0 :: q :: l) = renorm q (S q :: l).
Proof.
intros D. transitivity (decomp q (sumA q (0::q::l))).
- symmetry. apply decomp_carac.
+ apply renorm_delta. constructor. lia. auto with hof.
+ now rewrite renorm_sum.
- apply decomp_carac.
+ apply renorm_delta. auto with hof.
+ rewrite renorm_sum. simpl. rewrite Nat.sub_diag. simpl. lia.
Qed.
(** Below, [renormS q a l] is a simplified version of [renorm q (S a :: l)].
Indeed, when a decomposition starts by one lax gap and is strict
afterwards, no need for the full renorm, a single
bottom-up pass is enough. This particular situation is used
in next_decomp below. *)
Fixpoint renormS q a l :=
match l with
| [] => [S a]
| b::l' => if b =? S (a+q) then renormS q b l' else S a :: l
end.
Lemma renormS_sum q a l : sumA q (renormS q a l) = sumA q (S a :: l).
Proof.
revert a. induction l as [|b l IH]; intros a; simpl; auto.
case Nat.eqb_spec; simpl; intros H; auto.
rewrite IH. simpl. replace (b-q) with (S a); simpl; lia.
Qed.
Lemma renormS_delta q a l :
Delta (S q) (a::l) -> Delta (S q) (renormS q a l).
Proof.
revert a. induction l as [|b l IH]; intros a; simpl; auto.
- intros. constructor.
- inversion 1; subst.
case Nat.eqb_spec; simpl; intros E; auto.
constructor; auto. lia.
Qed.
Lemma renormS_alt q a l : Delta (S q) (a::l) ->
renormS q a l = renorm q (S a :: l).
Proof.
intros D. eapply decomp_unique; eauto using renormS_delta.
- apply renorm_delta; auto with hof.
- now rewrite renormS_sum, renorm_sum.
Qed.
Lemma renormS_head q a l : HeadStep q (S a :: l) (renormS q a l).
Proof.
revert a. induction l as [|b l IH]; simpl.
- exists 0; lia.
- intros a. case Nat.eqb_spec; intros H.
+ specialize (IH b). destruct renormS; simpl in *; trivial.
destruct IH as (m & ->). exists (S m). simpl; lia.
+ exists 0; lia.
Qed.
(** ** Decomposition of the next number *)
Definition next_decomp q l :=
match l with
| [] => [0]
| a :: l' =>
if a <=? q then
renormS q a l' (* same here as renorm q (S a :: l') *)
else
0::l
end.
Lemma next_decomp_sum q l : sumA q (next_decomp q l) = S (sumA q l).
Proof.
destruct l; simpl; trivial.
case Nat.leb_spec; intros; rewrite ?renormS_sum; simpl; trivial.
replace (n-q) with 0; simpl; lia.
Qed.
Lemma next_decomp_delta q l : Delta (S q) l -> Delta (S q) (next_decomp q l).
Proof.
destruct l; simpl; autoh.
case Nat.leb_spec; intros; auto using renormS_delta with hof.
Qed.
Lemma decomp_S q n : decomp q (S n) = next_decomp q (decomp q n).
Proof.
apply decomp_carac.
- apply next_decomp_delta, decomp_delta.
- now rewrite next_decomp_sum, decomp_sum.
Qed.
(** ** Classification of decompositions *)
(** The q-rank of a number is the least index in its q-decomposition. *)