-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenG.v
2167 lines (1903 loc) · 59.7 KB
/
GenG.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
Require Import MoreFun MoreList DeltaList GenFib.
Require FunG.
Import ListNotations.
Set Implicit Arguments.
(** Study of the functional equation:
- [Fq 0 = 0]
- [Fq (S n) + Fq^(q+1) (n) = S n]
where [Fq^(q+1) (n)] is [q+1] repeated applications of [Fq] at [n].
Note: using [q+1] instead of [q] iterations allows to avoid
the case of [0] iterations, where Fq^0 is identity, and hence
Fq (S n) = 1 always.
With this setting:
- [F 0] is [fun x => floor((x+1)/2)] (see below).
- [F 1] is Hofstadter's [G] i.e. [fun x => floor((x+1)/phi]
See http://oeis.org/A5206
- [F 2] is Hofstadter's [H], see http://oeis.org/A5374
- [F 3] is http://oeis.org/A5375
- [F 4] is http://oeis.org/A5376
*)
(** Coq representation of [F] as an inductive relation. This way,
no need to convince Coq yet that [F] is indeed a function.
- [F q n a] means that [Fq(n) = a].
- [Fs q p n a] means that [Fq^p (n) = a].
*)
Inductive F (q:nat) : nat -> nat -> Prop :=
| F0 : F q 0 0
| FS n a b : Fs q (S q) n a -> S n = a+b -> F q (S n) b
with Fs (q:nat) : nat -> nat -> nat -> Prop :=
| Fs0 n : Fs q 0 n n
| FsS p a b c : Fs q p a b -> F q b c -> Fs q (S p) a c.
#[global] Hint Constructors F Fs : hof.
(** The early behavior of [F] and [Fs] when [n<=3] doesn't depend on q *)
Lemma Fs_0 q p : Fs q p 0 0.
Proof.
induction p; eautoh.
Qed.
#[global] Hint Resolve Fs_0 : hof.
Lemma F_1 q : F q 1 1.
Proof.
induction q; eautoh.
Qed.
#[global] Hint Resolve F_1 : hof.
Lemma Fs_1 q p : Fs q p 1 1.
Proof.
induction p; eautoh.
Qed.
#[global] Hint Resolve Fs_1 : hof.
Lemma F_2 q : F q 2 1.
Proof.
induction q; eautoh.
Qed.
#[global] Hint Resolve F_2 : hof.
Lemma Fs_2 q p : Fs q p 2 (1+(1-p)).
Proof.
induction p; eautoh.
simpl.
eapply FsS. apply IHp. destruct p; simpl; autoh.
Qed.
#[global] Hint Resolve Fs_2 : hof.
Lemma F_3 q : F q 3 2.
Proof.
induction q; eautoh.
Qed.
#[global] Hint Resolve F_3 : hof.
Lemma Fs_3 q p : Fs q p 3 (1+(2-p)).
Proof.
induction p; eautoh.
eapply FsS; eauto. destruct p as [|[|p]]; simpl; autoh.
Qed.
#[global] Hint Resolve Fs_3 : hof.
(** [F] and [Fs] aren't above the identity line *)
Lemma F_le q n a : F q n a -> a <= n.
Proof.
induction 1; lia.
Qed.
#[global] Hint Resolve F_le : hof.
Lemma Fs_le q p n a : Fs q p n a -> a <= n.
Proof.
induction 1; trivial.
transitivity b; eautoh.
Qed.
#[global] Hint Resolve Fs_le : hof.
(** [F] and [Fs] are functional relations : unique output *)
Scheme F_ind2 := Induction for F Sort Prop
with Fs_ind2 := Induction for Fs Sort Prop.
Combined Scheme F_Fs_ind from F_ind2, Fs_ind2.
Lemma F_Fs_fun q :
(forall n a, F q n a -> forall a', F q n a' -> a = a') /\
(forall p n a, Fs q p n a -> forall a', Fs q p n a' -> a = a').
Proof.
apply F_Fs_ind.
- inversion_clear 1; auto.
- intros n a b HFs IH Hab a' HF.
inversion_clear HF; auto.
apply IH in H; lia.
- inversion_clear 1; auto.
- intros p a b c HFs IH HF IH' a' HFs'.
inversion_clear HFs'; auto.
apply IH in H; subst; auto.
Qed.
Lemma F_fun q n a a' : F q n a -> F q n a' -> a = a'.
Proof.
intro. now apply F_Fs_fun.
Qed.
Lemma Fs_fun q p n a a' : Fs p q n a -> Fs p q n a' -> a = a'.
Proof.
intro. now apply F_Fs_fun.
Qed.
(** [F] does have an implementation : there exists a function [f]
satisfying these equations. One possible tricq to define [f]
via Coq structural recursion is to add an extra parameter [p]:
[recf q p] is [f q] below [p] and arbitrary above.
*)
Fixpoint recf q p n :=
match p, n with
| S p, S n => S n - ((recf q p)^^(S q)) n
| _, _ => 0
end.
Definition f q n := recf q n n.
Notation fs q p := (f q ^^p).
Lemma recf_le q p n : recf q p n <= n.
Proof.
induction p; cbn - ["-" "^^"]. lia. destruct n; lia.
Qed.
Lemma recfs_le a q p n : ((recf q p)^^a) n <= n.
Proof.
induction a; simpl; auto. etransitivity. apply recf_le. apply IHa.
Qed.
Lemma recf_sound q p n : n <= p -> F q n (recf q p n).
Proof.
revert n.
induction p.
- inversion 1. simpl. constructor.
- destruct n.
+ simpl. constructor.
+ cbn - ["-" "^^"].
assert (IHa : forall a m, m <= p -> Fs q a m ((recf q p ^^ a) m)).
{ induction a; intros; simpl; econstructor; eauto.
apply IHp. transitivity m; auto using recfs_le. }
econstructor. apply IHa. lia.
generalize (recfs_le (S q) q p n). lia.
Qed.
Lemma f_sound q n : F q n (f q n).
Proof.
now apply recf_sound.
Qed.
#[global] Hint Resolve f_sound : hof.
Lemma f_complete q n a : F q n a <-> f q n = a.
Proof.
split; intros H.
- apply (F_fun (f_sound q n) H).
- subst; autoh.
Qed.
(** A few examples *)
(*
Compute take 26 (f 0).
Compute take 26 (f 1).
Compute take 26 (f 2).
Compute take 26 (f 3).
*)
(*
f 0 : [0; 1; 1; 2; 2; 3; 3; 4; 4; 5; 5; 6; 6; 7; 7]
f 1 : [0; 1; 1; 2; 3; 3; 4; 4; 5; 6; 6; 7; 8; 8; 9]
f 2 : [0; 1; 1; 2; 3; 4; 4; 5; 5; 6; 7; 7; 8; 9; 10]
f 3 : [0; 1; 1; 2; 3; 4; 5; 5; 6; 6; 7; 8; 8; 9; 10]
*)
(* The first values of [f] when [n<=3] do not depend on [q] *)
Lemma f_q_0 q : f q 0 = 0.
Proof.
reflexivity.
Qed.
Lemma f_q_1 q : f q 1 = 1.
Proof.
apply f_complete; autoh.
Qed.
Lemma f_q_2 q : f q 2 = 1.
Proof.
apply f_complete; autoh.
Qed.
Lemma f_q_3 q : f q 3 = 2.
Proof.
apply f_complete; autoh.
Qed.
(** Basic equations over [f] : the same as [F] *)
Lemma Fs_iter_f p q n : Fs q p n (fs q p n).
Proof.
induction p.
- simpl. autoh.
- eapply FsS; eauto. simpl.
rewrite f_complete; autoh.
Qed.
Lemma fs_q_0 p q : fs q p 0 = 0.
Proof.
induction p; simpl; auto.
rewrite IHp. apply f_q_0.
Qed.
Lemma fs_q_1 p q : fs q p 1 = 1.
Proof.
induction p; simpl; auto.
rewrite IHp. apply f_q_1.
Qed.
Lemma fs_q_2 q p : 0<p -> fs q p 2 = 1.
Proof.
destruct p. easy. intros _. now rewrite iter_S, f_q_2, fs_q_1.
Qed.
Lemma f_eqn q n : f q (S n) + fs q (S q) n = S n.
Proof.
assert (H := f_sound q (S n)).
inversion_clear H.
assert (fs q (S q) n = a).
{ revert H0. apply Fs_fun. apply Fs_iter_f. }
lia.
Qed.
Lemma f_eqn_pred q n : f q n + fs q (S q) (pred n) = n.
Proof.
destruct n.
- now rewrite fs_q_0.
- apply f_eqn.
Qed.
Lemma f_S q n : f q (S n) = S n - fs q (S q) n.
Proof.
generalize (f_eqn q n). lia.
Qed.
Lemma f_pred q n : f q n = n - fs q (S q) (pred n).
Proof.
generalize (f_eqn_pred q n). lia.
Qed.
(** Particular case *)
Lemma f_1_g n : f 1 n = FunG.g n.
Proof.
revert n.
apply FunG.g_unique.
- reflexivity.
- intros n. symmetry. now apply f_eqn.
Qed.
Lemma f_0_half n : f 0 (2*n) = n /\ f 0 (S (2*n)) = S n.
Proof.
induction n.
- now compute.
- assert (f 0 (2*(S n)) = S n).
{ rewrite f_pred; auto.
simpl Nat.iter.
replace (n + (S (n+0))) with (S (2*n)); lia. }
split; auto.
rewrite f_pred; auto.
simpl Nat.iter.
replace (S (n + (S (n+0)))) with (2*(S n)); lia.
Qed.
Lemma f_0_div2 n : f 0 n = (S n) / 2.
Proof.
rewrite <- Nat.div2_div.
destruct (Nat.Even_or_Odd n) as [(m,->)|(m,->)].
- destruct (f_0_half m) as (->,_).
symmetry. apply Nat.div2_succ_double.
- rewrite Nat.add_comm, Nat.add_1_l.
destruct (f_0_half m) as (_,->).
simpl. f_equal.
symmetry. apply (Nat.div2_double m).
Qed.
Lemma f_unique q h :
h 0 = 0 ->
(forall n, S n = h (S n)+ (h^^S q) n) ->
forall n, h n = f q n.
Proof.
intros h_0 h_S.
induction n as [[|n] IH] using lt_wf_ind.
- now rewrite h_0.
- assert (forall p m, m <= n -> (h^^p) m = fs q p m).
{ induction p.
- now simpl.
- intros. simpl.
rewrite IHp; auto. apply IH.
rewrite Nat.lt_succ_r. apply Nat.le_trans with m; auto.
eapply Fs_le. eapply Fs_iter_f. }
rewrite f_S, <- H; auto. specialize (h_S n). lia.
Qed.
Lemma f_step q n : f q (S n) = f q n \/ f q (S n) = S (f q n).
Proof.
induction n as [n IH] using lt_wf_ind.
destruct n.
- rewrite f_q_0, f_q_1. now right.
- rewrite 2 f_S.
assert (forall p m, m <= n ->
fs q p (S m) = fs q p m \/
fs q p (S m) = S (fs q p m)).
{ induction p.
- simpl; now right.
- intros; simpl.
destruct (IHp m H) as [-> | ->].
+ now left.
+ apply IH.
rewrite Nat.lt_succ_r. apply Nat.le_trans with m; auto.
eapply Fs_le. eapply Fs_iter_f. }
specialize (H (S q) n). lia.
Qed.
Lemma fs_step q p n : fs q p (S n) = fs q p n \/
fs q p (S n) = S (fs q p n).
Proof.
induction p; simpl.
- now right.
- destruct IHp as [-> | ->]. now left. apply f_step.
Qed.
Lemma f_mono_S q n : f q n <= f q (S n).
Proof.
generalize (f_step q n). lia.
Qed.
Lemma fs_mono_S q p n : fs q p n <= fs q p (S n).
Proof.
generalize (fs_step q p n). lia.
Qed.
Lemma f_le_add q n m : f q (n+m) <= n + f q m.
Proof.
induction n; trivial.
simpl. destruct (f_step q (n+m)); lia.
Qed.
Lemma f_mono q n m : n <= m -> f q n <= f q m.
Proof.
induction 1.
- trivial.
- transitivity (f q m); auto using f_mono_S.
Qed.
Lemma fs_mono q p n m : n <= m -> fs q p n <= fs q p m.
Proof.
induction 1.
- trivial.
- transitivity (fs q p m); auto using fs_mono_S.
Qed.
(** NB : in Coq, for natural numbers, 3-5 = 0 (truncated subtraction) *)
Lemma f_lipschitz q n m : f q m - f q n <= m - n.
Proof.
destruct (le_ge_dec n m) as [H|H].
- induction H; try generalize (f_step q m); lia.
- generalize (f_mono q H). lia.
Qed.
Lemma fs_lipschitz q p n m : fs q p m - fs q p n <= m - n.
Proof.
revert n m. induction p; auto.
intros. simpl. etransitivity. eapply f_lipschitz. eapply IHp.
Qed.
Lemma f_nonzero q n : 0 < n -> 0 < f q n.
Proof.
unfold lt. intros. rewrite <- (f_q_1 q). now apply f_mono.
Qed.
Lemma f_nz q n : n <> 0 -> f q n <> 0.
Proof.
generalize (@f_nonzero q n). lia.
Qed.
Lemma f_0_inv q n : f q n = 0 -> n = 0.
Proof.
generalize (@f_nz q n). lia.
Qed.
Lemma fs_nonzero q n p : 0 < n -> 0 < fs q p n.
Proof.
revert n. induction p; simpl; auto using f_nonzero.
Qed.
Lemma fs_0_inv q n p : fs q p n = 0 -> n = 0.
Proof.
generalize (@fs_nonzero q n p). lia.
Qed.
Lemma f_fix q n : f q n = n <-> n <= 1.
Proof.
split.
- destruct n; auto.
assert (H := f_eqn q n).
intros.
assert (H' : fs q (S q) n = 0) by lia.
apply fs_0_inv in H'.
now subst.
- inversion_clear 1. apply f_q_1.
inversion_clear H0. apply f_q_0.
Qed.
Lemma f_le q n : f q n <= n.
Proof.
eapply F_le; eautoh.
Qed.
Lemma fs_le q p n : fs q p n <= n.
Proof.
eapply Fs_le, Fs_iter_f.
Qed.
Lemma f_lt q n : 1<n -> f q n < n.
Proof.
generalize (f_le q n) (f_fix q n); lia.
Qed.
#[global] Hint Resolve f_lt : hof.
Lemma fs_lt q p n : 0<p -> 1<n -> fs q p n < n.
Proof.
destruct p; [easy|intros _ Hn].
change (f q (fs q p n) < n).
destruct (Nat.eq_dec (fs q p n) 0) as [->|N0]; [cbn; lia| ].
destruct (Nat.eq_dec (fs q p n) 1) as [->|N1]; [now rewrite f_q_1| ].
apply Nat.lt_le_trans with (fs q p n); [|apply fs_le].
apply f_lt. lia.
Qed.
(** Two special formulations for [f_step] *)
Lemma f_next q n a : f q n = a -> (f q (S n) <> a <-> f q (S n) = S a).
Proof.
generalize (f_step q n). lia.
Qed.
Lemma f_prev q n a : n <> 0 -> f q n = a ->
(f q (n-1) <> a <-> f q (n-1) = a - 1).
Proof.
intros H Ha.
assert (Ha' := f_nz q H).
generalize (f_step q (n-1)). replace (S (n-1)) with n; lia.
Qed.
(** [f] cannot stay flat very long *)
Lemma f_nonflat q n : f q (1+n) = f q n -> f q (2+n) = S (f q n).
Proof.
generalize (f_eqn q (1+n)) (f_eqn q n).
rewrite !iter_S. intros. rewrite H1 in *. simpl in *. lia.
Qed.
Lemma f_nonflat' q n : f q (S n) = f q n -> f q (n-1) = f q n - 1.
Proof.
destruct n.
- now rewrite f_q_0, f_q_1.
- replace (S n - 1) with n by lia.
intros H.
destruct (f_step q n) as [H'|H'].
+ apply f_nonflat in H'; auto. simpl in *. lia.
+ lia.
Qed.
Lemma f_SS q n : f q n < f q (S (S n)).
Proof.
destruct (f_step q n) as [E|E].
- generalize (f_nonflat _ _ E). simpl in *. lia.
- apply Nat.lt_le_trans with (f q (S n)). lia. auto using f_mono_S.
Qed.
Lemma f_double_le q n : n <= f q (2*n).
Proof.
induction n.
- trivial.
- replace (2* S n) with (S (S (2*n))) by lia.
transitivity (S (f q (2*n))). lia. now apply f_SS.
Qed.
Lemma f_div2_le q n : n/2 <= f q n.
Proof.
rewrite <- Nat.div2_div.
rewrite (Nat.div2_odd n) at 2.
transitivity (f q (2*Nat.div2 n)).
now apply f_double_le.
apply f_mono. lia.
Qed.
Lemma fs_bound q n p :
1 < n -> 1 < fs q p n -> fs q p n <= n-p.
Proof.
revert n.
induction p.
- simpl. intros. lia.
- intros. simpl in *.
assert (LE : 1 <= fs q p n).
{ generalize (@fs_nonzero q n p). lia. }
assert (NE : fs q p n <> 1).
{ intros EQ; rewrite EQ, f_q_1 in *. lia. }
specialize (IHp n H).
generalize (@f_lt q (fs q p n)). lia.
Qed.
Lemma fs_init q n p : 1 <= n <= S p -> fs q p n = 1.
Proof.
intros N.
destruct (Nat.eq_dec n 1) as [->|N']; [ apply fs_q_1 |].
assert (H := @fs_nonzero q n p).
destruct (Nat.eq_dec (fs q p n) 1); trivial.
generalize (@fs_bound q n p). lia.
Qed.
Lemma f_init q n : 2 <= n <= q+3 -> f q n = n-1.
Proof.
intros. rewrite f_pred. rewrite fs_init; lia.
Qed.
(* Said otherwise : for any n, [f q n] will eventually be stationary
when q grows. More precisely, for [n>=2], [f q n = n-1] as soon as
[q>=n-3]. And for [n<2], we always have [f q n = n].
Hard theorem : at fixed n and growing q, [f q n] will be increasing.
See [Words.f_grows].
*)
(*==============================================================*)
(** * Faster computation of f *)
(** Auxiliary function : [countdown n = [n;n-1;...0]] *)
Fixpoint countdown n :=
match n with
| 0 => [0]
| S n' => n :: countdown n'
end.
Lemma countdown_in n x : In x (countdown n) <-> x <= n.
Proof.
induction n; simpl; rewrite ?IHn; lia.
Qed.
(** Auxiliary function : dropping [n] leftmost elements in a list *)
Fixpoint npop {A} n (l:list A) :=
match n with
| 0 => l
| S n' =>
match l with
| [] => []
| _::l' => npop n' l'
end
end.
Lemma npop_map {A B} (f:A->B) l p :
npop p (map f l) = map f (npop p l).
Proof.
revert l. induction p; destruct l; simpl in *; auto.
Qed.
Lemma npop_countdown x y : x <= y ->
npop (y - x) (countdown y) = countdown x.
Proof.
induction 1.
- now rewrite Nat.sub_diag.
- replace (S m - x) with (S (m-x)) by lia. simpl; auto.
Qed.
(** With [ftabulate], we will build at once the list
[[f q n; f q (n-1); ... ; f q 0]].
And [fdescend] visits this list to compute [fs q p n].
Even with nat values, doing this way is way faster than the current [f].
*)
Fixpoint fdescend stq p n :=
match p with
| 0 => n
| S p =>
match stq with
| [] => 0 (* normally won't occur *)
| a::_ => fdescend (npop (n-a) stq) p a
end
end.
Fixpoint ftabulate q n :=
match n with
| 0 => [0]
| S n =>
let stq := ftabulate q n in
(S n - fdescend stq (S q) n)::stq
end.
Lemma fdescend_spec q stq p n :
stq = map (f q) (countdown n) -> fdescend stq p n = fs q p n.
Proof.
revert stq n.
induction p; intros stq n E.
- simpl; auto.
- rewrite iter_S. simpl.
destruct stq as [|a stq'] eqn:Stq.
+ now destruct n.
+ assert (a = f q n).
{ destruct n; simpl in E; inversion E; auto. }
rewrite <- H.
apply IHp.
rewrite E. rewrite npop_map. f_equal.
apply npop_countdown. subst a. apply f_le.
Qed.
Lemma ftabulate_spec q n : ftabulate q n = map (f q) (countdown n).
Proof.
induction n.
- reflexivity.
- cbn -[minus fdescend].
rewrite (fdescend_spec q); auto.
rewrite <- f_S. f_equal; auto.
Qed.
(** Now comes a reasonably efficient [f] function
(moreover, [ftabulate] can always be used when multiples
images of [f] are needed at the same time). *)
Definition fopt q n := hd 0 (ftabulate q n).
Lemma fopt_spec q n : fopt q n = f q n.
Proof.
unfold fopt. rewrite ftabulate_spec. destruct n; simpl; auto.
Qed.
Lemma fopt_iter q p n : (fopt q ^^p) n = fs q p n.
Proof.
induction p; simpl; trivial. rewrite fopt_spec. now f_equal.
Qed.
Definition fsopt q p n := fdescend (ftabulate q n) p n.
Lemma fsopt_spec q p n : fsopt q p n = fs q p n.
Proof.
apply fdescend_spec, ftabulate_spec.
Qed.
(*==============================================================*)
(** * Antecedents by [f q]
Study of the reverse problem [f q n = a] for some [a]. *)
Lemma f_max_two_antecedents q n m :
f q n = f q m -> n<m -> m = S n.
Proof.
intros H H'.
destruct (le_lt_dec (2+n) m) as [LE|LT]; try lia.
apply (f_mono q) in LE.
rewrite (f_nonflat q n) in LE. lia.
apply Nat.le_antisymm.
- rewrite H. now apply f_mono.
- apply f_mono_S.
Qed.
(** Another formulation of the same fact *)
Lemma f_inv q n m :
f q n = f q m -> (n = m \/ n = S m \/ m = S n).
Proof.
intros.
destruct (lt_eq_lt_dec n m) as [[LT|EQ]|LT]; auto.
- generalize (@f_max_two_antecedents q n m); auto.
- generalize (@f_max_two_antecedents q m n); auto.
Qed.
(** [f] is an onto map *)
Lemma f_onto q a : exists n, f q n = a.
Proof.
induction a.
- exists 0; trivial.
- destruct IHa as (n,Ha).
destruct (f_step q n); [ | exists (S n); lia].
destruct (f_step q (S n)); [ | exists (S (S n)); lia].
exfalso.
generalize (@f_max_two_antecedents q n (S (S n))). lia.
Qed.
(** We even have an explicit expression of one antecedent *)
Definition rchild q n := n + fs q q n.
Definition lchild q n := n + fs q q n - 1. (** left son, if there's one *)
Lemma f_onto_eqn q a : f q (rchild q a) = a.
Proof.
destruct (f_onto q a) as (n,Hn).
unfold rchild. rewrite <- Hn, <- iter_S.
assert (E := f_eqn q n).
destruct (f_step q n) as [<-|H]; f_equal; lia.
Qed.
Lemma rightmost_child_carac q a n : f q n = a ->
(f q (S n) = S a <-> n = rchild q a).
Proof.
intros Hn.
assert (H' := f_eqn q n).
rewrite iter_S in H'.
rewrite Hn in H'.
unfold rchild; lia.
Qed.
Lemma f_children q a n : f q n = a ->
n = rchild q a \/ n = lchild q a.
Proof.
intros Hn.
destruct (f_step q n) as [H|H].
- right.
destruct (f_step q (S n)) as [H'|H'].
+ exfalso.
generalize (@f_max_two_antecedents q n (S (S n))). lia.
+ rewrite rightmost_child_carac in H'; trivial.
rewrite H, Hn in H'. unfold lchild, rchild in *; lia.
- rewrite <- (@rightmost_child_carac q a n); lia.
Qed.
Lemma f_lchild q a :
f q (lchild q a) = a - 1 \/ f q (lchild q a) = a.
Proof.
destruct (le_gt_dec a 0).
+ replace a with 0 by lia. unfold lchild.
rewrite fs_q_0. simpl. rewrite f_q_0. now left.
+ assert (0 < rchild q a)
by (unfold rchild; generalize (@f_nonzero q a); lia).
destruct (f_step q (lchild q a)) as [H'|H'];
replace (S (lchild q a)) with (rchild q a) in * by
(unfold lchild, rchild in *; lia);
rewrite f_onto_eqn in *; lia.
Qed.
(** This provides easily a first relationship between f and
generalized Fibonacci numbers *)
Lemma fs_A q n p : fs q p (A q n) = A q (n-p).
Proof.
revert p.
induction n; intros.
- simpl. apply fs_q_1.
- destruct p; auto.
rewrite iter_S; simpl. rewrite <- (IHn p). f_equal.
rewrite <- (IHn q). apply f_onto_eqn.
Qed.
Lemma f_A q n : f q (A q n) = A q (n-1).
Proof.
apply (fs_A q n 1).
Qed.
Lemma f_SA q n : n<>0 -> f q (S (A q n)) = S (A q (n-1)).
Proof.
intros.
rewrite <- (@f_A q n).
apply rightmost_child_carac; trivial.
unfold rchild.
rewrite f_A, fs_A.
replace (n-1-q) with (n-S q) by lia.
now apply A_sum.
Qed.
(** More generally, [f] is shifting down Zeckendorf decompositions *)
Definition fbis q n := sumA q (map pred (decomp q n)).
Lemma fbis_decomp q n :
decomp q (fbis q n) = renorm q (map pred (decomp q n)).
Proof.
apply decomp_carac.
- apply renorm_delta.
apply Delta_map with (S q).
intros; lia. apply decomp_delta.
- now rewrite renorm_sum.
Qed.
Lemma fsbis q p n : p <= S q ->
(fbis q^^p) n = sumA q (map (decr p) (decomp q n)).
Proof.
intros Hp.
revert n.
induction p; intros.
- simpl. now rewrite map_decr_0, decomp_sum.
- now rewrite iter_S, IHp, fbis_decomp, renorm_mapdecr, map_decr_S by lia.
Qed.
Lemma fbis_is_f q n : fbis q n = f q n.
Proof.
apply f_unique.
- reflexivity.
- clear n. intros n.
rewrite fsbis; auto.
assert (Hn := decomp_sum q n).
assert (D := decomp_delta q n).
remember (decomp q n) as l eqn:Hl.
destruct l as [|a l].
+ simpl in *. now subst.
+ unfold fbis. rewrite decomp_S, <- Hl. simpl.
case Nat.leb_spec; intros.
* rewrite <- map_decr_1.
rewrite renormS_alt, renorm_mapdecr'; simpl; auto with arith hof.
2: destruct q; lia.
rewrite Nat.add_shuffle1.
assert (~In 0 l).
{ apply (@Delta_nz' (S q) a); auto with arith. }
rewrite <- sumA_eqn_pred; auto.
rewrite decr_0.
unfold decr. replace (a-S q) with 0; simpl in *; lia.
* rewrite map_cons, sumA_cons.
rewrite <- Nat.add_assoc.
rewrite <- map_decr_1.
rewrite <- sumA_eqn_pred; auto.
eapply Delta_nz; eauto. lia.
Qed.
Lemma f_decomp q n : f q n = sumA q (map pred (decomp q n)).
Proof.
symmetry. apply fbis_is_f.
Qed.
Lemma decomp_f q n : decomp q (f q n) = renorm q (map pred (decomp q n)).
Proof.
now rewrite <- fbis_is_f, fbis_decomp.
Qed.
Lemma fs_decomp q p n :
p <= S q -> fs q p n = sumA q (map (decr p) (decomp q n)).
Proof.
intros. rewrite <- fsbis; auto. clear.
induction p; simpl; now rewrite ?IHp, <- ?fbis_is_f.
Qed.
Lemma f_sumA q l : Delta (S q) l ->
f q (sumA q l) = sumA q (map pred l).
Proof.
intros. rewrite f_decomp. f_equal. f_equal. autoh.
Qed.
Lemma fs_sumA q p l : p <= S q -> Delta (S q) l ->
fs q p (sumA q l) = sumA q (map (decr p) l).
Proof.
intros. rewrite fs_decomp; auto. f_equal. f_equal. autoh.
Qed.
Lemma f_sumA_lax q l : q<>0 -> Delta q l ->
f q (sumA q l) = sumA q (map pred l).
Proof.
intros. rewrite <- renorm_sum.
rewrite f_sumA; autoh.
rewrite <- !map_decr_1, renorm_mapdecr; auto. lia.
Qed.
Lemma fs_sumA_lax q p l : p < S q -> Delta q l ->
fs q p (sumA q l) = sumA q (map (decr p) l).
Proof.
intros. rewrite <- renorm_sum.
rewrite fs_sumA; auto with arith hof.
apply renorm_mapdecr; lia.
Qed.
(** Extending theorem [fs_decomp] to larger iterates than [p <= S q] *)
Definition succrank q n :=
match rank q n with
| None => 0
| Some r => S r
end.
Lemma f_succrank q n : succrank q n <= S (succrank q (f q n)).
Proof.
unfold succrank, rank. rewrite decomp_f.
assert (H := renorm_head q (map pred (decomp q n))).
destruct decomp as [|r l], renorm as [|r' l']; simpl in *; try lia.
destruct H as (m, H). lia.
Qed.
Lemma fs_decomp_gen q p n : n = 0 \/ p <= q + succrank q n ->
fs q p n = sumA q (map (decr p) (decomp q n)).
Proof.
intros [->|H].
- simpl. induction p; simpl; now rewrite ?IHp.
- revert n H.
induction p; intros.
+ simpl. now rewrite map_decr_0, decomp_sum.
+ rewrite iter_S, IHp; clear IHp.
2:{ generalize (f_succrank q n); lia. }
rewrite decomp_f.
unfold succrank, rank in H.
assert (D := decomp_delta q n).
destruct decomp as [|r l]; trivial.
destruct (Nat.eq_dec r 0) as [->|R].
* rewrite renorm_mapdecr by lia.
f_equal. symmetry. apply map_decr_S.
* rewrite renorm_nop.
{ f_equal. symmetry. apply map_decr_S. }
{ apply Delta_pred; trivial.
eapply Delta_nz; eauto with hof. lia. }
Qed.
(** Zone where [f q n = n-1].
Note that [f q n] cannot be [n] or more except when [n<=1], see [f_lt].
The conditions below are optimal, see [f_subid_inv] later. *)
Lemma f_subid q n : n<>1 -> n <= q+3 -> f q n = n-1.
Proof.
intros Hn Hn'.
destruct (Nat.eq_dec n 0).
- subst. now rewrite f_q_0.
- apply f_init; lia.
Qed.
(** Some particular cases : early diagonals *)
Lemma f_q_q q : q<>1 -> f q q = q-1.
Proof.
intros. apply f_subid; lia.
Qed.
Lemma f_q_Sq q : q<>0 -> f q (S q) = q.
Proof.
intros. rewrite f_subid; lia.
Qed.
Lemma f_q_plus_2 q : f q (2+q) = S q.
Proof.
rewrite f_subid; lia.
Qed.
Lemma f_q_plus_3 q : f q (3+q) = 2+q.
Proof.
rewrite f_subid; lia.
Qed.
Lemma f_q_plus_4 q : f q (4+q) = 2+q.
Proof.
replace (4+q) with (sumA q [S (S q)]).
2:{ cbn -[A]. rewrite A_S.
replace (S q - q) with 1 by lia.
rewrite !A_base; lia. }
rewrite f_sumA; autoh. cbn -[A]. rewrite A_base; lia.
Qed.
Lemma f_q_plus_5 q : f q (5+q) = 3+q.
Proof.
replace (5+q) with (sumA q [0;S (S q)]).
2:{ cbn -[A]. rewrite A_S.
replace (S q - q) with 1 by lia.
rewrite !A_base; lia. }
rewrite f_sumA; autoh. cbn -[A]. rewrite !A_base; lia.
Qed.
Lemma f_q_plus_6 q : f q (6+q) = 3+q.
Proof.
replace (6+q) with (sumA q [1;S (S q)]).
2:{ cbn -[A]. rewrite (A_S q (S q)).
replace (S q - q) with 1 by lia.
rewrite !A_base; lia. }
rewrite f_sumA; autoh. cbn -[A]. rewrite !A_base; lia.
Qed.