-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunG.v
1799 lines (1587 loc) · 50.9 KB
/
FunG.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
(** * FunG : Hofstadter's G function and tree *)
From Coq Require Import Program Program.Wf.
Require Import MoreFun MoreList DeltaList Fib.
Import ListNotations.
Set Implicit Arguments.
(** Study of the functional equation:
- [G (S n) + G (G n) = S n]
- [G 0 = 0]
and its relationship with the Fibonacci sequence.
References:
- Hofstadter's book: Goedel, Escher, Bach, page 137.
- Sequence A005206 on the Online Encyclopedia of Integer Sequences
#<a href="http://oeis.org/A005206">#http://oeis.org/A005206#</a>#
*)
(** * Statement of the [G] equations as an inductive relation. *)
Inductive G : nat -> nat -> Prop :=
| G0 : G 0 0
| GS n a b c : G n a -> G a b -> S n = c+b -> G (S n) c.
#[global] Hint Constructors G : hof.
Lemma G1 : G 1 1.
Proof.
eautoh.
Qed.
#[global] Hint Resolve G1 : hof.
Lemma GS_inv n a : G (S n) a ->
exists b c, G n b /\ G b c /\ S n = a + c.
Proof.
inversion_clear 1 as [|? b c ? Hb Hc Hn].
exists b; exists c. auto.
Qed.
(** A first upper bound on [G].
It is used for proving that [G] is a total function. *)
Lemma G_le n a : G n a -> a <= n.
Proof.
revert a.
induction n using lt_wf_rec.
destruct n; inversion_clear 1; lia.
Qed.
#[global] Hint Resolve G_le : hof.
Lemma G_rec (P:nat->Set) :
P 0 ->
(forall n, P n -> (forall a, G n a -> P a) -> P (S n)) ->
forall n, P n.
Proof.
intros P_0 P_S.
induction n as [[|n] IH] using lt_wf_rec.
- apply P_0.
- apply P_S.
+ apply IH. auto.
+ intros. apply IH. auto with arith hof.
Defined.
(** The [G] relation is indeed functional: *)
Lemma G_fun n a a' : G n a -> G n a' -> a = a'.
Proof.
revert a a'.
induction n as [|n IH IH'] using G_rec; intros a a' Ha Ha'.
- inversion_clear Ha; inversion_clear Ha'; trivial.
- destruct (GS_inv Ha) as (b & c & Hb & Hc & H).
destruct (GS_inv Ha') as (b' & c' & Hb' & Hc' & H').
replace b' with b in * by (apply IH; auto).
replace c' with c in * by (apply (IH' b); auto).
lia.
Qed.
(** * The [g] function, implementing the [G] relation. *)
Definition g_spec n : { a : nat | G n a }.
Proof.
induction n as [|n IH IH'] using G_rec.
- exists 0; autoh.
- destruct IH as (a,Ha).
destruct (IH' a Ha) as (b,Hb).
assert (a <= n) by now apply G_le.
assert (b <= a) by now apply G_le.
exists (S n - b).
eapply GS; eauto; lia.
Defined.
Definition g n := let (a,_) := (g_spec n) in a.
(*
Extraction Inline G_rec lt_wf_rec induction_ltof2.
Recursive Extraction g. (* TODO: des let-in parasites *)
*)
(* Compute take 10 g. *)
(*
= 0 :: 1 :: 1 :: 2 :: 3 :: 3 :: 4 :: 4 :: 5 :: 6 :: nil
: list nat
*)
Lemma g_correct n : G n (g n).
Proof.
unfold g; now destruct (g_spec n).
Qed.
#[global] Hint Resolve g_correct : hof.
Lemma g_complete n p : G n p <-> p = g n.
Proof.
split; intros H.
- apply (G_fun H (g_correct n)).
- subst. apply g_correct.
Qed.
(** The initial equations, formulated for [g] *)
Lemma g_0 : g 0 = 0.
Proof.
reflexivity.
Qed.
Lemma g_eqn n : g (S n) + g (g n) = S n.
Proof.
unfold g.
destruct (g_spec (S n)) as (a,Ha).
destruct (g_spec n) as (b,Hb).
destruct (g_spec b) as (c,Hc).
destruct (GS_inv Ha) as (b' & c' & Hb' & Hc' & H).
rewrite (G_fun Hb Hb') in *.
rewrite (G_fun Hc Hc') in *. lia.
Qed.
(** Same, with subtraction *)
Lemma g_S n : g (S n) = S n - g (g n).
Proof.
generalize (g_eqn n); lia.
Qed.
(** * Properties of [g] *)
Lemma g_unique f :
f 0 = 0 ->
(forall n, S n = f (S n)+f(f n)) ->
forall n, f n = g n.
Proof.
intros f_0 f_S.
induction n as [|n IH IH'] using G_rec.
- now rewrite f_0, g_0.
- specialize (f_S n).
rewrite IH in *.
rewrite (IH' (g n)) in * by autoh.
generalize (g_eqn n). lia.
Qed.
Lemma g_step n : g (S n) = g n \/ g (S n) = S (g n).
Proof.
induction n as [|n IH IH'] using G_rec.
- compute; auto.
- rewrite (g_S n), (g_S (S n)).
destruct IH as [-> | ->]; [lia|].
destruct (IH' (g n)) as [-> | ->]; autoh; lia.
Qed.
Lemma g_mono_S n : g n <= g (S n).
Proof.
generalize (g_step n). lia.
Qed.
Lemma g_mono n m : n <= m -> g n <= g m.
Proof.
induction 1.
- trivial.
- transitivity (g m); auto using g_mono_S.
Qed.
Lemma g_le_S n : g (S n) <= S (g n).
Proof.
generalize (g_step n). lia.
Qed.
Lemma g_le_add n m : g (n+m) <= n + g m.
Proof.
induction n; trivial.
- simpl. generalize (g_le_S (n+m)). lia.
Qed.
(** NB : in Coq, for natural numbers, 3-5 = 0 (truncated subtraction) *)
Lemma g_lipschitz n m : g m - g n <= m - n.
Proof.
destruct (le_ge_dec n m) as [H|H].
- induction H; try generalize (g_step m); lia.
- generalize (g_mono H). lia.
Qed.
Lemma g_nonzero n : 0 < n -> 0 < g n.
Proof.
unfold lt. intros. change 1 with (g 1). now apply g_mono.
Qed.
Lemma g_0_inv n : g n = 0 -> n = 0.
Proof.
destruct n; trivial.
assert (0 < g (S n)) by (apply g_nonzero; auto with arith).
lia.
Qed.
Lemma g_nz n : n <> 0 -> g n <> 0.
Proof.
intros H. contradict H. now apply g_0_inv.
Qed.
Lemma g_fix n : g n = n <-> n <= 1.
Proof.
split.
- destruct n; auto.
assert (H := g_eqn n).
intros Eq; rewrite Eq in H; clear Eq.
assert (H' : g (g n) = 0) by lia.
do 2 apply g_0_inv in H'. now subst.
- inversion_clear 1 as [|? H0]; auto.
inversion_clear H0; auto.
Qed.
Lemma g_le n : g n <= n.
Proof.
apply G_le; autoh.
Qed.
Lemma g_lt n : 1<n -> g n < n.
Proof.
generalize (g_le n) (g_fix n); lia.
Qed.
#[global] Hint Resolve g_lt : hof.
(** Two special formulations for [g_step] *)
Lemma g_next n a : g n = a -> (g (S n) <> a <-> g (S n) = S a).
Proof.
generalize (g_step n). lia.
Qed.
Lemma g_prev n a : n <> 0 -> g n = a ->
(g (n-1) <> a <-> g (n-1) = a - 1).
Proof.
intros H Ha.
assert (Ha' := g_nz H).
generalize (g_step (n-1)). replace (S (n-1)) with n by lia.
lia.
Qed.
(** [g] cannot stay flat very long *)
Lemma g_nonflat n : g (S n) = g n -> g (S (S n)) = S (g n).
Proof.
intros H. generalize (g_eqn (S n)) (g_eqn n). rewrite H. lia.
Qed.
Lemma g_nonflat' n : g (S n) = g n -> g (n-1) = g n - 1.
Proof.
destruct n.
- now compute.
- replace (S n - 1) with n by lia.
intros H.
destruct (g_step n) as [H'|H'].
+ apply g_nonflat in H'. lia.
+ lia.
Qed.
Lemma g_SS n : S (g n) <= g (S (S n)).
Proof.
destruct (g_step n) as [E|E].
- generalize (g_nonflat _ E). lia.
- transitivity (g (S n)). lia. auto using g_mono_S.
Qed.
Lemma g_double_pred_le n : n <= g (2*n-1).
Proof.
induction n as [|[|n] IH]; trivial.
replace (_ - _) with (S (2*n)) in IH by lia.
replace (_ - _) with (S (S (S (2*n)))) by lia.
etransitivity; [|apply g_SS].
rewrite <- Nat.succ_le_mono. apply IH.
Qed.
Lemma g_double_le n : n <= g (2*n).
Proof.
etransitivity; [apply g_double_pred_le|apply g_mono; lia].
Qed.
Lemma g_Sdiv2_le n : (S n)/2 <= g n.
Proof.
etransitivity; [apply g_double_pred_le|].
apply g_mono.
apply Nat.le_sub_le_add_l. simpl "+".
rewrite (Nat.div2_odd (S n)) at 2.
rewrite Nat.div2_div. lia.
Qed.
Lemma g_div2_le n : n/2 <= g n.
Proof.
etransitivity; [|apply g_Sdiv2_le].
apply Nat.div_le_mono; lia.
Qed.
(* Two consecutive steps are possible, but not three *)
Lemma g_maxsteps n : g (2+n) = 2 + g n -> g (3+n) = 2 + g n.
Proof.
intros H.
simpl in *.
destruct (g_step n) as [H0|H0], (g_step (S n)) as [H1|H1]; try lia.
assert (H2 := g_eqn n).
assert (H3 := g_eqn (S n)).
assert (H4 := g_eqn (S (S n))).
assert (H5 : g(g(S n)) = g(g n)) by lia.
rewrite H0 in H5.
assert (H6 := g_nonflat _ H5).
rewrite H1 in H4.
rewrite <- H0 in H6. lia.
Qed.
(** Said otherwise, g(3+n) cannot be 3+g(n) *)
Lemma g_3_2 n : g (3+n) <= 2 + g n.
Proof.
destruct (g_step n) as [H|H].
- rewrite <- H. apply (g_le_add 2 (S n)).
- destruct (g_step (S n)) as [H'|H'].
+ generalize (g_le_S (2+n)). simpl in *. lia.
+ rewrite H in H'. apply g_maxsteps in H'. lia.
Qed.
Lemma g_maxsteps_below n : g (2+n) = 2 + g n -> g (n-1) = g n.
Proof.
intros H.
assert (Nz : n<>0). { now intros ->. }
assert (H' := g_3_2 (n-1)).
replace (3+(n-1)) with (2+n) in * by lia.
generalize (@g_mono (n-1) n). lia.
Qed.
(*==============================================================*)
(** * Antecedents by [g]
Study of the reverse problem [g(x) = a] for some [a]. *)
Lemma g_max_two_antecedents a n m :
g n = a -> g m = a -> n<m -> m = S n.
Proof.
intros Hn Hm H.
destruct m as [|m]; [inversion H|].
destruct n as [|n].
- compute in Hn; subst. now apply g_0_inv in Hm.
- generalize
(g_eqn n) (g_eqn m) (g_step n) (g_step m)
(g_lipschitz (g n) (g m)).
lia.
Qed.
(** Another formulation of the same fact *)
Lemma g_inv n m :
g n = g m -> (n = m \/ n = S m \/ m = S n).
Proof.
intros.
destruct (lt_eq_lt_dec n m) as [[LT|EQ]|LT]; auto.
- generalize (@g_max_two_antecedents (g n) n m); auto.
- generalize (@g_max_two_antecedents (g m) m n); auto.
Qed.
(** [g] is an onto map *)
Lemma g_onto a : exists n, g n = a.
Proof.
induction a.
- exists 0; trivial.
- destruct IHa as (n,Ha).
destruct (g_step n); [ | exists (S n); lia].
destruct (g_step (S n)); [ | exists (S (S n)); lia].
exfalso.
generalize (@g_max_two_antecedents a n (S (S n))). lia.
Qed.
(** * The [G] tree *)
(** [g] can be related to a infinite tree where:
- nodes are labeled via a breadth-first traversal
- from the label of a child node, [g] give the label
of the father node.
<<
9 10 11 12 13
\/ | \ /
6 7 8
\ / /
4 5
\ /
3
|
2
|
1
>>
We already proved that g is onto, hence each node has at least
one child. A node is said to be unary if the node label has
exactly one antecedent, and the node is said multary otherwise.
We first prove that a multary node is actually binary.
*)
Definition Unary (g:nat->nat) a :=
forall n m, g n = a -> g m = a -> n = m.
Definition Multary g a := ~ Unary g a.
Definition Binary (g:nat->nat) a :=
exists n m,
g n = a /\ g m = a /\ n <> m /\
forall k, g k = a -> k = n \/ k = m.
Lemma multary_binary a : Multary g a <-> Binary g a.
Proof.
unfold Multary.
split.
- intros U.
assert (Ha : a<>0).
{ contradict U.
subst.
intros u v Hu Hv. apply g_0_inv in Hu. apply g_0_inv in Hv.
now subst. }
destruct (g_onto a) as (n,Hn).
assert (Hn' : n<>0).
{ contradict Ha. now subst. }
destruct (eq_nat_dec (g (S n)) a);
destruct (eq_nat_dec (g (n-1)) a).
+ exfalso.
generalize (@g_max_two_antecedents a (n-1) (S n)). lia.
+ exists n; exists (S n); repeat split; auto.
intros k Hk.
destruct (g_inv n k) as [H|[H|H]]; try lia.
subst n. simpl in *. rewrite Nat.sub_0_r in *. lia.
+ exists n; exists (n-1); repeat split; auto; try lia.
intros k Hk.
destruct (g_inv n k) as [H|[H|H]]; try lia.
subst k. lia.
+ elim U.
intros u v Hu Hv.
assert (u = n).
{ destruct (g_inv n u) as [H|[H|H]]; subst;
simpl in *; rewrite ?Nat.sub_0_r in *; lia. }
assert (v = n).
{ destruct (g_inv n v) as [H'|[H'|H']]; subst;
simpl in *; rewrite ?Nat.sub_0_r in *; lia. }
lia.
- intros (n & m & Hn & Hm & Hnm & H) U.
apply Hnm. now apply (U n m).
Qed.
(** We could even exhibit at least one child for each node *)
Definition rchild n := n + g n. (** rightmost son, always there *)
Definition lchild n := n + g n - 1. (** left son, if there's one *)
Lemma rightmost_child_carac a n : g n = a ->
(g (S n) = S a <-> n = rchild a).
Proof.
intros Hn.
assert (H' := g_eqn n).
rewrite Hn in H'.
unfold rchild; lia.
Qed.
Lemma g_onto_eqn a : g (rchild a) = a.
Proof.
destruct (g_onto a) as (n,Hn).
destruct (g_step n) as [H|H].
- unfold rchild.
rewrite <- Hn. rewrite <- H at 1 3. f_equal. apply g_eqn.
- rewrite Hn in H.
rewrite rightmost_child_carac in H; trivial. congruence.
Qed.
(** This provides easily a first relationship between g and
Fibonacci numbers *)
Lemma g_fib n : n <> 0 -> g (fib (S n)) = fib n.
Proof.
induction n.
- now destruct 1.
- destruct n.
+ reflexivity.
+ intros _. rewrite fib_eqn.
rewrite <- IHn; auto.
apply g_onto_eqn.
Qed.
Lemma g_fib' n : 1 < n -> g (fib n) = fib (n-1).
Proof.
destruct n.
- lia.
- intros. rewrite g_fib; f_equal; lia.
Qed.
Lemma g_Sfib n : 1 < n -> g (S (fib (S n))) = S (fib n).
Proof.
intros.
rewrite <- (@g_fib n) by lia.
apply rightmost_child_carac; trivial.
unfold rchild.
now rewrite g_fib, g_fib', fib_eqn' by lia.
Qed.
Lemma g_Sfib' n : 2 < n -> g (S (fib n)) = S (fib (n-1)).
Proof.
destruct n.
- lia.
- intros. rewrite g_Sfib; do 2 f_equal; lia.
Qed.
(*==============================================================*)
(** * Shape of the [G] tree *)
(** Let's study now the shape of the G tree.
First, we prove various characterisation of [Unary] and [Binary] *)
Lemma g_children a n : g n = a ->
n = rchild a \/ n = lchild a.
Proof.
intros Hn.
destruct (g_step n) as [H|H].
- right.
destruct (g_step (S n)) as [H'|H'].
+ exfalso.
generalize (@g_max_two_antecedents a 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 a n); lia.
Qed.
Lemma g_lchild a :
g (lchild a) = a - 1 \/ g (lchild a) = a.
Proof.
destruct (le_gt_dec a 0).
+ replace a with 0 by lia. compute. auto.
+ assert (0 < rchild a) by (unfold rchild; generalize (@g_nonzero a); lia).
destruct (g_step (lchild a)) as [H'|H'];
replace (S (lchild a)) with (rchild a) in * by
(unfold lchild, rchild in *; lia);
rewrite g_onto_eqn in *; lia.
Qed.
Lemma unary_carac1 a :
Unary g a <-> forall n, g n = a -> n = rchild a.
Proof.
split; intros H.
- intros n Hn. apply H; trivial. apply g_onto_eqn.
- intros n m Hn Hm. apply H in Hn. apply H in Hm. lia.
Qed.
Lemma unary_carac2 a :
Unary g a <-> g (lchild a) = a - 1.
Proof.
rewrite unary_carac1.
split; intros H.
- destruct (g_lchild a); trivial.
assert (lchild a = rchild a) by (apply H; lia).
unfold rchild, lchild in *; lia.
- intros n Hn.
destruct (g_children _ Hn) as [H'|H']; trivial.
rewrite <- H' in H.
replace a with 0 in * by lia. exact H'.
Qed.
Lemma binary_carac1 a :
Multary g a <-> a <> 0 /\ forall n, (g n = a <-> n = rchild a \/ n = lchild a).
Proof.
unfold Multary; rewrite unary_carac2.
split.
- intros H.
assert (a<>0). { contradict H; now subst. }
split; trivial.
destruct (g_lchild a) as [H'|H']; [intros; lia|].
clear H.
split.
+ apply g_children.
+ destruct 1; subst n. apply g_onto_eqn. auto.
- intros (Ha,H) H'.
assert (g (lchild a) = a). { apply H; now right. }
lia.
Qed.
Lemma binary_carac2 a :
Multary g a <-> (a <> 0 /\ g (lchild a) = a).
Proof.
unfold Multary; rewrite unary_carac2.
split.
- intros H.
assert (a<>0). { contradict H; now subst. }
split; trivial.
destruct (g_lchild a); lia.
- lia.
Qed.
Lemma unary_or_multary n : Unary g n \/ Multary g n.
Proof.
destruct (eq_nat_dec n 0).
- left. subst. apply unary_carac2. reflexivity.
- destruct (eq_nat_dec (g (lchild n)) n).
+ right. apply binary_carac2; auto.
+ left. apply unary_carac2. apply g_prev; auto. lia.
apply g_onto_eqn.
Qed.
Lemma unary_xor_multary n : Unary g n -> Multary g n -> False.
Proof.
intuition.
Qed.
(** Now we state the arity of node children *)
Lemma leftmost_son_is_binary n p :
g p = n -> g (p-1) <> n -> Multary g p.
Proof.
intros Hp Hp'.
assert (Hp0 : p<>0). { intros Eq. rewrite Eq in *. auto. }
assert (Hn0 := g_nz Hp0).
rewrite g_prev in Hp'; auto.
destruct (g_lchild p) as [Hq1|Hq1]; [|apply binary_carac2; auto].
assert (Hq := g_onto_eqn p).
change (lchild p) with (rchild p - 1) in *.
set (q:=rchild p) in *.
assert (q<>0) by (unfold q, rchild; lia).
clearbody q.
assert (Eq := g_eqn (q-1)).
replace (S (q-1)) with q in Eq by lia.
assert (Eq' := g_eqn q).
rewrite Hq1,Hp' in Eq.
rewrite Hq,Hp in Eq'.
assert (Hq' : g (S q) = p) by lia.
intro U. specialize (U q (S q) Hq Hq'). lia.
Qed.
Lemma unary_rchild_is_binary n : n <> 0 ->
Unary g n -> Multary g (rchild n).
Proof.
intros H U. apply (@leftmost_son_is_binary n).
- apply g_onto_eqn.
- rewrite unary_carac2 in U. unfold lchild, rchild in *; lia.
Qed.
Lemma binary_lchild_is_binary n :
Multary g n -> Multary g (lchild n).
Proof.
rewrite binary_carac2. intros (B0,B1).
apply (@leftmost_son_is_binary n); trivial.
intros Eq.
generalize (@g_max_two_antecedents n _ _ Eq (g_onto_eqn n)).
assert (H := g_nz B0).
unfold lchild, rchild in *. lia.
Qed.
Lemma binary_rchild_is_unary n :
Multary g n -> Unary g (rchild n).
Proof.
rewrite binary_carac2. intros (B0,B1).
assert (Hp := g_onto_eqn n).
assert (Hq := g_onto_eqn (lchild n)).
set (p:=lchild n) in *.
assert (g (S (rchild p)) = S p). { apply rightmost_child_carac; auto. }
apply unary_carac2.
change (g (lchild (rchild n)) = p).
unfold lchild. rewrite Hp.
replace (rchild n) with (S p) by (unfold p, rchild, lchild; lia).
replace (S p + n -1) with (p + n) by lia.
rewrite <- B1. apply g_onto_eqn.
Qed.
(** Hence the shape of the [G] tree is a repetition of this pattern:
<<
r
|
p q
\ /
n
>>
where [n] and [p] and [r=n+q] are binary nodes and
[q=p+1=n+g(n)] is unary.
Fractal aspect : each binary nodes (e.g. [n], [p] and [r] above)
have the same infinite shape of tree above them
(which is the shape of [G] apart from special initial nodes 1 2):
<<
A A
| |
. A .
| \ /
G = . A = .
>>
*)
(*==============================================================*)
(** * Another equation about [g]
This one will be used later when flipping [G] left/right. *)
Lemma g_alt_eqn n : g n + g (g (S n) - 1) = n.
Proof.
destruct (eq_nat_dec n 0) as [Hn|Hn].
- now subst.
- assert (Hn' := g_nz Hn).
case (g_step n) as [H|H].
+ (* n left of a binary node *)
rewrite H.
generalize (g_eqn (n-1)).
case (g_step (n - 1));
replace (S (n - 1)) with n by lia.
* generalize (@g_max_two_antecedents (g n) (n-1) (S n)). lia.
* intros. replace (g n - 1) with (g (n-1)) by lia. lia.
+ (* n is rightmost child *)
generalize (g_eqn n). rewrite H. simpl. rewrite Nat.sub_0_r. lia.
Qed.
(*==============================================================*)
(** * Depth in the [G] tree *)
(** The depth of a node in the [G] tree is the number of
iteration of [g] needed before reaching node 1 *)
(* Compute (g^^3) 13. *)
Program Fixpoint depth (n:nat) { measure n } : nat :=
match n with
| 0 => 0
| 1 => 0
| _ => S (depth (g n))
end.
Next Obligation.
apply g_lt. lia.
Qed.
(* Compute depth 13. *)
Lemma depth_SS n : depth (S (S n)) = S (depth (g (S (S n)))).
Proof.
now WfExtensionality.unfold_sub depth (depth (S (S n))).
Qed.
Lemma depth_eqn n : 1<n -> depth n = S (depth (g n)).
Proof.
destruct n as [|[|n]].
- lia.
- lia.
- intros _. apply depth_SS.
Qed.
Lemma g_depth n : depth (g n) = depth n - 1.
Proof.
destruct (le_lt_dec n 1) as [LE|LT].
- assert (H : n=0 \/ n=1) by lia.
destruct H as [-> | ->]; reflexivity.
- rewrite (depth_eqn LT). lia.
Qed.
Lemma depth_correct n : n <> 0 -> (g^^(depth n)) n = 1.
Proof.
induction n as [[|[|n]] IH] using lt_wf_rec.
- lia.
- reflexivity.
- intros _. rewrite depth_SS.
set (n' := S (S n)) in *. rewrite iter_S. apply IH.
+ apply g_lt. unfold n'; lia.
+ apply g_nz. unfold n'; lia.
Qed.
Lemma depth_minimal n : 1<n -> 1 < ((g^^(depth n - 1)) n).
Proof.
induction n as [[|[|n]] IH] using lt_wf_rec.
- lia.
- lia.
- intros _. rewrite depth_SS.
simpl. rewrite Nat.sub_0_r.
set (n' := S (S n)) in *.
destruct (eq_nat_dec (g n') 1) as [->|NE].
+ simpl. unfold n'; lia.
+ assert (H : g n' <> 0) by (apply g_nz; unfold n'; lia).
assert (depth (g n') <> 0).
{ intro EQ. generalize (depth_correct H). now rewrite EQ. }
replace (depth (g n')) with (S (depth (g n') - 1)) by lia.
rewrite iter_S.
apply IH.
* apply g_lt. unfold n'; lia.
* lia.
Qed.
Lemma depth_mono n m : n <= m -> depth n <= depth m.
Proof.
revert m.
induction n as [[|[|n]] IH] using lt_wf_rec; intros m H.
- change (depth 0) with 0. auto with arith.
- change (depth 1) with 0. auto with arith.
- destruct m as [|[|m]]; try lia.
rewrite 2 depth_SS.
apply le_n_S.
apply IH.
+ apply g_lt. lia.
+ now apply g_mono.
Qed.
Lemma depth_fib k : depth (fib k) = k-2.
Proof.
induction k as [|[|[|k]] IH].
- reflexivity.
- reflexivity.
- reflexivity.
- rewrite depth_eqn.
+ rewrite g_fib, IH; auto. lia.
+ unfold lt. change 2 with (fib 3). apply fib_mono. lia.
Qed.
Lemma depth_Sfib k : 1 < k -> depth (S (fib k)) = k-1.
Proof.
induction k as [|[|[|k]] IH].
- lia.
- lia.
- reflexivity.
- intros _.
rewrite depth_eqn.
+ rewrite g_Sfib, IH; lia.
+ unfold lt. apply le_n_S. now apply fib_nz.
Qed.
Lemma depth_0 n : depth n = 0 <-> n <= 1.
Proof.
destruct n as [|[|n]].
- compute; auto with arith.
- compute; auto with arith.
- rewrite depth_SS. lia.
Qed.
Lemma depth_carac k n : k <> 0 ->
(depth n = k <-> S (fib (S k)) <= n <= fib (S (S k))).
Proof.
intros Hk.
split; intros H.
- split.
+ destruct (le_lt_dec n (fib (S k))) as [LE|LT]; trivial.
apply depth_mono in LE. rewrite depth_fib in LE. lia.
+ destruct (le_lt_dec n (fib (S (S k)))) as [LE|LT]; trivial.
unfold lt in LT. apply depth_mono in LT.
rewrite depth_Sfib in LT; lia.
- destruct H as (H1,H2).
apply depth_mono in H1. apply depth_mono in H2.
rewrite depth_fib in H2; rewrite depth_Sfib in H1; lia.
Qed.
(** Conclusion: for k>0,
- [1+fib (k+1)] is the leftmost node at depth [k]
- [fib (k+2)] is the rightmost node at depth [k]
- hence we have [fib (k+2) - fib (k+1) = fib k] nodes at depth [k].
*)
(** Alternatively, we could also have considered
- [U(k)] : number of unary nodes at depth [k]
- [B(k)] : number binary nodes at depth [k]
and their recursive equations:
- [U(k+1) = B(k)]
- [B(k+1) = U(k)+B(k)]
These numbers are also Fibonacci numbers (except when [k=0]),
along with the number of nodes at depth [k] which is
[U(k)+B(k)].
*)
(*==============================================================*)
(* begin hide *)
(* now in Coq stdlib's List.v in 8.5 *)
Lemma map_ext_in :
forall (A B : Type)(f g:A->B) l,
(forall a, In a l -> f a = g a) -> map f l = map g l.
Proof.
induction l; simpl; auto.
intros; rewrite H by intuition; rewrite IHl; auto.
Qed.
(* end hide *)
Lemma map_S_pred l : ~In 0 l -> map S (map pred l) = l.
Proof.
intros.
rewrite map_map. rewrite <- (map_id l) at 2.
apply map_ext_in.
intros a Ha. assert (a<>0) by congruence. lia.
Qed.
(** * [g] and Fibonacci decomposition.
We now prove that g is simply "shifting" the Fibonacci
decomposition of a number, i.e. removing 1 at all the ranks
in this decomposition.
For proving this result, we need to consider relaxed
decompositions where consecutive fibonacci terms may occur
(but still no [fib 0] nor [fib 1] in the decomposition).
These decompositions aren't unique. We consider here
these decomposition with the lowest terms first, to ease
the following proof.
*)
Lemma g_sumfib l :
Delta 1 (0::l) -> g (sumfib (List.map S l)) = sumfib l.
Proof.
remember (sumfib (List.map S l)) as n eqn:E.
revert l E.
induction n as [[|n] IH] using lt_wf_rec; intros [|k l].
- trivial.
- simpl map. rewrite sumfib_cons. generalize (fib_S_nz k). intros. lia.
- discriminate.
- simpl map. rewrite sumfib_cons.
intros E Hl.
rewrite g_S.
assert (Hk : k<>0) by (inversion Hl; lia).
assert (D : Delta 1 (k::l)) by now inversion Hl.
clear Hl.
assert (Hl : ~In 0 l) by (eapply Delta_nz'; eauto).
assert (E' : n = pred (fib (S k)) + sumfib (map S l)).
{ generalize (@fib_nz (S k)). lia. }
rewrite <- preds_ok, <- sumfib_app in E'.
assert (D2 := preds_delta (S k)).
assert (D1 : Delta 1 (1::preds (S k))) by eautoh.
rewrite <- (map_S_pred (preds (S k))) in E'
by (eauto using Delta_nz' with hof).
rewrite <- map_app in E'.
apply IH in E'; try lia.
2:{ change (Delta 1 (map pred (1::preds (S k))++l)).
eapply Delta_app; eauto using Delta_pred with hof.
intros y [<-|IN]; simpl; auto with arith.
rewrite in_map_iff in IN. destruct IN as (z & Hz & IN).
apply preds_lt in IN. lia. }
destruct (preds (S k)) as [|p ps] eqn:Hp.
+ (* preds (S k) = [], hence (k = 1) *)
simpl in E'.
replace k with 1 in * by (rewrite preds_nil in Hp; lia).
rewrite <- (map_S_pred l) in E'; auto.
apply IH in E'.
2:{ generalize (g_le n); lia. }
2:{ change (Delta 1 (map pred (1::l))).
apply Delta_pred; [|simpl;auto].
apply Delta_nz' with 1 0; simpl in *; autoh. }
clear D D1 D2.
simpl sumfib. simpl in E.
generalize (@fib_eqn' k) (sumfib_eqn l Hl). lia.
+ (* preds (S k) <> [], hence (1 < k) *)
assert (Hk' : 1 < k).
{ assert (~(S k <= 2)); try lia. now rewrite <- preds_nil, Hp. }
simpl in E'.
replace (fib (pred p)) with 1 in E'.
2:{ destruct (preds_low _ Hp); now subst. }
change (1+_) with (sumfib (map pred (3::ps)++l)) in E'.
assert (D3 : Delta 1 (map pred (3::ps) ++ l)).
{ eapply Delta_app; eauto.
- apply Delta_pred; eauto.
+ intros [[= ]|IN]. revert IN. apply Delta_nz' with 1 p; eautoh.
+ destruct (preds_low _ Hp); subst; eautoh.
- intros y [<-|IN]; simpl; try lia.
rewrite in_map_iff in IN. destruct IN as (z & Hz & IN).
assert (z < S k) by (apply preds_lt; rewrite Hp; now right). lia. }
rewrite <- (map_S_pred (_ ++ _)) in E'.
2:{ eapply Delta_nz' with 1 1; simpl in *; autoh. }
apply IH in E'.
2:{ generalize (g_le n); lia. }
2:{ change (Delta 1 (map pred (map pred (2::3::ps) ++ l))).
apply Delta_pred; [|simpl;autoh].
apply Delta_nz' with 1 0; simpl in *; autoh. }
clear D D1 D2 D3.
simpl sumfib.
rewrite map_app, sumfib_app in E'.
simpl in E'.
replace ps with (tl (preds (S k))) in E' by now rewrite Hp.
rewrite preds_shift, preds_ok in E'. simpl in E'.
generalize (@fib_eqn' k) (sumfib_eqn l Hl) (@fib_nz (k-1)). lia.
Qed.
Lemma g_sumfib' l : Delta 1 (1::l) ->
g (sumfib l) = sumfib (map Nat.pred l).
Proof.
intros.