-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlipG.v
1505 lines (1351 loc) · 42.1 KB
/
FlipG.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
(** * FlipG : Hofstadter's flipped G tree *)
Require Import DeltaList Fib FunG.
Set Implicit Arguments.
(** See first the file [FunG] for the study of:
- [G (S n) + G (G n) = S n]
- [G 0 = 0]
and the associated tree where nodes are labeled breadth-first
from left to right.
Now, question by Hofstadter: what if we still label the nodes
from right to left, but for the mirror tree ?
What is the algebraic definition of the "parent" function
for this flipped tree ?
<<
9 10 11 12 13
\/ | \ /
6 7 8
\ \ /
4 5
\ /
3
|
2
|
1
>>
References:
- Hofstadter's book: Goedel, Escher, Bach, page 137.
- Sequence A123070 on the Online Encyclopedia of Integer Sequences
#<a href="http://oeis.org/A123070">#http://oeis.org/A123070#</a>#
*)
(*=============================================================*)
(** * The [flip] function *)
(** If we label the node from right to left, the effect
on node numbers is the [flip] function below.
The idea is to map a row [ [1+fib (k+1);...;fib (k+2)] ]
to the flipped row [ [fib (k+2);...;1+fib (k+1)] ].
*)
Definition flip n :=
if n <=? 1 then n else S (fib (S (S (S (depth n))))) - n.
Ltac tac_leb := rewrite <- ?Bool.not_true_iff_false, Nat.leb_le.
Lemma flip_depth n : depth (flip n) = depth n.
Proof.
unfold flip.
case_eq (n <=? 1); tac_leb; trivial.
intros.
assert (depth n <> 0) by (rewrite depth_0; lia).
apply depth_carac; trivial.
set (k := depth n) in *.
assert (S (fib (S k)) <= n <= fib (S (S k)))
by now apply depth_carac.
rewrite fib_eqn.
lia.
Qed.
Lemma flip_eqn0 n : depth n <> 0 ->
flip n = S (fib (S (S (S (depth n))))) - n.
Proof.
intros.
rewrite depth_0 in *.
unfold flip.
case_eq (n <=? 1); tac_leb; lia.
Qed.
Lemma flip_eqn k n : 1 <= n <= fib k ->
flip (fib (S k) + n) = S (fib (S (S k))) - n.
Proof.
intros Hn.
unfold flip.
case_eq (fib (S k) + n <=? 1); tac_leb.
- generalize (@fib_nz (S k)); lia.
- intros H.
replace (depth (fib (S k) + n)) with k.
+ rewrite fib_eqn. lia.
+ assert (k<>0).
{ intros ->. simpl in Hn; lia. }
symmetry. apply depth_carac; auto.
rewrite fib_eqn; lia.
Qed.
(** Two special cases : leftmost and rightmost node at a given depth *)
Lemma flip_Sfib k : 1<k -> flip (S (fib k)) = fib (S k).
Proof.
intros H.
destruct k.
- lia.
- rewrite <- Nat.add_1_r.
rewrite flip_eqn.
lia.
split; trivial. apply fib_nz. lia.
Qed.
Lemma flip_fib k : 1<k -> flip (fib (S k)) = S (fib k).
Proof.
intros H.
destruct k.
- lia.
- rewrite fib_eqn' by lia.
rewrite flip_eqn; auto.
rewrite fib_eqn'; auto. lia.
replace (S k - 1) with k by lia.
split; auto. apply fib_nz. lia.
Qed.
(** flip is involutive (and hence a bijection) *)
Lemma flip_flip n : flip (flip n) = n.
Proof.
unfold flip at 2.
case_eq (n <=? 1).
- unfold flip. now intros ->.
- tac_leb.
intros Hn.
set (k := depth n).
assert (k<>0).
{ contradict Hn. unfold k in *. rewrite depth_0 in Hn. lia. }
assert (Hn' : S (fib (S k)) <= n <= fib (S (S k))).
{ apply depth_carac; auto. }
rewrite fib_eqn.
replace (S (fib (S (S k)) + fib (S k)) - n) with
(fib (S k) + (S (fib (S (S k))) - n)) by lia.
rewrite flip_eqn; auto. lia.
split. lia. rewrite fib_eqn'; auto.
replace (S k - 1) with k by lia.
lia.
Qed.
Lemma flip_eq n m : flip n = flip m <-> n = m.
Proof.
split; intros H.
- rewrite <- (flip_flip n), <- (flip_flip m). now f_equal.
- now subst.
Qed.
Lemma flip_swap n m : flip n = m <-> n = flip m.
Proof.
rewrite <- (flip_flip m) at 1. apply flip_eq.
Qed.
Lemma flip_low n : n <= 1 <-> flip n <= 1.
Proof.
split; intros.
- assert (EQ : n = 0 \/ n = 1) by lia.
destruct EQ as [-> | ->]; compute; auto.
- assert (EQ : flip n = 0 \/ flip n = 1) by lia.
rewrite !flip_swap in EQ. compute in EQ. lia.
Qed.
Lemma flip_high n : 1 < n <-> 1 < flip n.
Proof.
generalize (flip_low n). lia.
Qed.
(** flip and neighbors *)
Lemma flip_S n : 1<n -> depth (S n) = depth n ->
flip (S n) = flip n - 1.
Proof.
intros Hn EQ.
assert (depth n <> 0) by (rewrite depth_0; lia).
rewrite !flip_eqn0, EQ; lia.
Qed.
Lemma flip_pred n : 1<n -> depth (n-1) = depth n ->
flip (n-1) = S (flip n).
Proof.
intros Hn EQ.
assert (depth n <> 0) by (rewrite depth_0; lia).
rewrite !flip_eqn0, EQ; try lia.
assert (n <= fib (S (S (depth n)))) by (apply depth_carac; auto).
rewrite fib_eqn; lia.
Qed.
(*=============================================================*)
(** * The [fg] function corresponding to the flipped [G] tree *)
Definition fg n := flip (g (flip n)).
(* Compute take 10 fg. *)
Lemma fg_depth n : depth (fg n) = depth n - 1.
Proof.
unfold fg. now rewrite flip_depth, g_depth, flip_depth.
Qed.
Lemma fg_fib k : k<>0 -> fg (fib (S k)) = fib k.
Proof.
destruct k as [|[|[|k]]].
- lia.
- reflexivity.
- reflexivity.
- intros _.
unfold fg.
now rewrite flip_fib, g_Sfib, flip_Sfib by lia.
Qed.
Lemma fg_Sfib k : 1<k -> fg (S (fib (S k))) = S (fib k).
Proof.
intros Hk.
unfold fg.
rewrite flip_Sfib by lia.
rewrite g_fib by lia.
rewrite flip_fib; auto.
Qed.
Lemma fg_fib' k : 1<k -> fg (fib k) = fib (k-1).
Proof.
destruct k.
- lia.
- intros. rewrite fg_fib; f_equal; lia.
Qed.
Lemma fg_Sfib' k : 2<k -> fg (S (fib k)) = S (fib (k-1)).
Proof.
destruct k.
- inversion 1.
- intros. rewrite Nat.sub_1_r. apply fg_Sfib. simpl. lia.
Qed.
Lemma fg_step n : fg (S n) = fg n \/ fg (S n) = S (fg n).
Proof.
destruct (le_lt_dec n 1) as [LE|LT].
- assert (EQ : n = 0 \/ n = 1) by lia.
destruct EQ as [-> | ->]; compute; auto.
- set (k := depth n).
assert (k<>0) by (unfold k; rewrite depth_0; lia).
assert (S (fib (S k)) <= n <= fib (S (S k))).
{ apply depth_carac; auto. }
destruct (eq_nat_dec n (fib (S (S k)))) as [EQ|NE].
+ rewrite EQ. rewrite fg_Sfib, fg_fib; auto. lia.
+ assert (depth (S n) = k). { apply depth_carac; lia. }
assert (depth (flip (S n)) = k). { rewrite flip_depth; auto. }
assert (1 < flip n). { now apply (flip_high n). }
unfold fg.
rewrite flip_S in *; auto.
destruct (eq_nat_dec (g (flip n - 1)) (g (flip n))) as [EQ|NE'].
* left; f_equal; trivial.
* right.
rewrite g_prev in NE' by lia.
rewrite NE'.
apply flip_pred.
{ unfold lt. change 2 with (g 3). apply g_mono.
assert (flip n <> 2).
{ intros EQ. rewrite EQ in *. now compute in NE'. }
lia. }
{ rewrite <- NE'. rewrite !g_depth. rewrite flip_depth.
unfold k in *; lia. }
Qed.
Lemma fg_mono_S n : fg n <= fg (S n).
Proof.
generalize (fg_step n). lia.
Qed.
Lemma fg_mono n m : n<=m -> fg n <= fg m.
Proof.
induction 1.
- trivial.
- transitivity (fg m); auto using fg_mono_S.
Qed.
Lemma fg_lipschitz n m : fg m - fg n <= m - n.
Proof.
destruct (le_ge_dec n m) as [H|H].
- induction H; try generalize (fg_step m); lia.
- generalize (fg_mono H). lia.
Qed.
Lemma fg_nonzero n : 0 < n -> 0 < fg n.
Proof.
unfold lt. intros. change 1 with (fg 1). now apply fg_mono.
Qed.
Lemma fg_0_inv n : fg n = 0 -> n = 0.
Proof.
destruct n; trivial.
assert (0 < fg (S n)) by (apply fg_nonzero; auto with arith).
lia.
Qed.
Lemma fg_nz n : n <> 0 -> fg n <> 0.
Proof.
intros H. contradict H. now apply fg_0_inv.
Qed.
Lemma fg_fix n : fg n = n <-> n <= 1.
Proof.
unfold fg.
now rewrite flip_low, <- g_fix, flip_swap.
Qed.
Lemma fg_le n : fg n <= n.
Proof.
generalize (fg_lipschitz 0 n). change (fg 0) with 0. lia.
Qed.
Lemma fg_lt n : 1<n -> fg n < n.
Proof.
generalize (fg_le n) (fg_fix n); lia.
Qed.
Lemma fg_onto a : exists n, fg n = a.
Proof.
unfold fg. destruct (g_onto (flip a)) as (x,H).
exists (flip x). now rewrite flip_swap, flip_flip.
Qed.
Lemma fg_nonflat n : fg (S n) = fg n -> fg (S (S n)) = S (fg n).
Proof.
intros H.
destruct (le_lt_dec n 1) as [Hn|Hn].
- assert (EQ : n = 0 \/ n = 1) by lia.
destruct EQ as [-> | ->]; reflexivity.
- destruct (fg_step (S n)) as [H'|H']; [|lia].
exfalso.
set (k := depth n).
assert (Hk : k<>0) by (unfold k; rewrite depth_0; lia).
assert (Hnk : S (fib (S k)) <= n <= fib (S (S k))).
{ apply depth_carac; auto. }
destruct (eq_nat_dec n (fib (S (S k)))) as [EQ|NE].
+ rewrite EQ in H. rewrite fg_fib, fg_Sfib in H; lia.
+ destruct (eq_nat_dec (S n) (fib (S (S k)))) as [EQ|NE'].
* rewrite EQ in H'. rewrite fg_fib, fg_Sfib in H'; lia.
* revert H'. rewrite H; clear H. unfold fg. rewrite flip_eq.
assert (depth (S n) = k). { apply depth_carac; lia. }
assert (depth (flip (S n)) = k). { rewrite flip_depth; auto. }
assert (depth (S (S n)) = k). { apply depth_carac; lia. }
assert (depth (flip (S (S n))) = k). { rewrite flip_depth; auto. }
rewrite flip_S by lia.
rewrite flip_S by (unfold k in H; lia).
assert (HH : forall m, 1<m -> g (m-1-1) <> g m).
{ intros.
generalize (@g_max_two_antecedents (g m) (m-1-1) m).
lia. }
apply HH. apply flip_high in Hn. auto.
Qed.
Lemma fg_max_two_antecedents n m :
fg n = fg m -> n < m -> m = S n.
Proof.
intros H LT.
unfold lt in LT.
assert (LE := fg_mono LT).
rewrite <- H in LE.
destruct (fg_step n) as [EQ|EQ]; [|lia].
apply fg_nonflat in EQ.
destruct (le_lt_dec m (S n)) as [LE'|LT']; [lia|].
unfold lt in LT'. apply fg_mono in LT'. lia.
Qed.
Lemma fg_inv n m : fg n = fg m -> n = m \/ n = S m \/ m = S n.
Proof.
intros H.
destruct (lt_eq_lt_dec n m) as [[LT|EQ]|LT]; auto.
- apply fg_max_two_antecedents in LT; auto.
- apply fg_max_two_antecedents in LT; auto.
Qed.
Lemma fg_eqn n : 3 < n -> fg n + fg (S (fg (n-1))) = S n.
Proof.
intros Hn.
set (k := depth n).
assert (3<=k).
{ unfold k. change 3 with (depth 4).
apply depth_mono; auto. }
assert (LE : S (fib (S k)) <= n <= fib (S (S k))).
{ apply depth_carac. lia. auto. }
destruct (eq_nat_dec (S (fib (S k))) n) as [EQ|NE].
- (* n = S (fib (S k)) *)
replace (n-1) with (fib (S k)) by lia.
rewrite <- EQ.
rewrite fg_fib, !fg_Sfib' by lia.
replace (S k - 1) with k by lia.
rewrite fib_eqn'; lia.
- (* n > S (fib (S k)) *)
assert (Hk : depth (n-1) = k).
{ apply depth_carac; lia. }
assert (Hk' : depth (fg (n-1)) = k-1).
{ now rewrite fg_depth, Hk. }
assert (LE' : S (fib k) <= fg (n-1) <= fib (S k)).
{ replace k with (S (k-1)) by lia.
apply depth_carac; auto. lia. }
destruct (eq_nat_dec (fg (n-1)) (fib (S k))) as [EQ|NE'].
+ (* fg(n-1) = fib (S k) *)
rewrite EQ.
rewrite fg_Sfib' by lia.
assert (EQ' : fg n = fib (S k)).
{ destruct (fg_step (n-1)) as [EQ'|EQ'];
replace (S (n-1)) with n in EQ'; try lia.
rewrite EQ in EQ'.
assert (H' : depth (fg n) = k) by (apply depth_carac; lia).
rewrite fg_depth in H'. unfold k in *. lia. }
rewrite EQ'.
rewrite Nat.add_succ_r. rewrite <- fib_eqn' by lia.
f_equal.
assert (EQ'' : fg (n-1) = fg (fib (S (S k)))) by now rewrite EQ, fg_fib.
apply fg_max_two_antecedents in EQ''; lia.
+ (* fg(n-1) <> fib (S k) *)
assert (Hk'' : depth (S (fg (n-1))) = k-1).
{ apply depth_carac. lia.
replace (S (k-1)) with k by lia.
lia. }
unfold fg at 2.
rewrite flip_eqn0;
rewrite g_depth, flip_depth, Hk''; [|lia].
replace (S (S (k-1-1))) with k by lia.
assert (LT : 1 < fg (n-1)).
{ unfold lt. change 2 with (fg 3). apply fg_mono. lia. }
rewrite flip_S by lia.
unfold fg at 2. rewrite flip_flip.
rewrite flip_pred; try (unfold k in Hk; lia).
clear LT Hk'' NE' LE' Hk Hk'.
replace (g (g (S (flip n)) - 1)) with (flip n - g (flip n))
by (generalize (g_alt_eqn (flip n)); lia).
rewrite <- (flip_flip (g (flip n))).
fold (fg n).
rewrite !flip_eqn0 by (rewrite ?fg_depth; unfold k in H; lia).
rewrite fg_depth.
change (depth n) with k.
replace (S (k-1)) with k by lia.
rewrite fib_eqn.
assert (Hnk : depth (fg n) = k-1).
{ rewrite fg_depth. unfold k. lia. }
apply depth_carac in Hnk; [|lia].
replace (S (k-1)) with k in Hnk by lia.
replace (fib k) with (fib (S (S k)) - fib (S k)) in Hnk;
[|rewrite fib_eqn; lia].
set (FSSK := fib (S (S k))) in *.
set (FSK := fib (S k)) in *.
replace (S (FSSK+FSK) -n - (S FSSK - fg n))
with (FSK + fg n - n) by lia.
lia.
Qed.
(** This equation, along with initial values up to [n=3],
characterizes [fg] entirely. It can hence be used to
give an algebraic recursive definition to [fg], answering
the Hofstader's question. *)
Lemma fg_eqn_unique f :
f 0 = 0 ->
f 1 = 1 ->
f 2 = 1 ->
f 3 = 2 ->
(forall n, 3<n -> f n + f (S (f (n-1))) = S n) ->
forall n, f n = fg n.
Proof.
intros F0 F1 F2 F3 Fn.
induction n as [n IH] using lt_wf_rec.
destruct (le_lt_dec n 3) as [Hn|Hn].
- destruct n as [|[|[|n]]]; try assumption.
replace n with 0 by lia. assumption.
- assert (E := fg_eqn Hn).
specialize (Fn n Hn).
rewrite (IH (n-1)) in Fn by lia.
rewrite (IH (S (fg (n-1)))) in Fn.
+ generalize (@fg_lt n). lia.
+ generalize (@fg_lt (n-1)). lia.
Qed.
(*=============================================================*)
(** * [fg] and the shape of its tree
We already know that [fg] is onto, hence each node has
at least a child. Moreover, [fg_max_two_antecedents] says
that we have at most two children per node.
*)
Lemma unary_flip a : Unary fg a <-> Unary g (flip a).
Proof.
split; intros U n m Hn Hm; apply flip_eq; apply U;
unfold fg in *; rewrite ?flip_flip; now apply flip_swap.
Qed.
Lemma multary_flip a : Multary fg a <-> Multary g (flip a).
Proof.
unfold Multary.
now rewrite unary_flip.
Qed.
Lemma fg_multary_binary a : Multary fg a <-> Binary fg a.
Proof.
unfold Multary.
split.
- intros U.
assert (Ha : a<>0).
{ contradict U.
subst.
intros u v Hu Hv. apply fg_0_inv in Hu. apply fg_0_inv in Hv.
now subst. }
destruct (fg_onto a) as (n,Hn).
assert (Hn' : n<>0).
{ contradict Ha. now subst. }
destruct (eq_nat_dec (fg (S n)) a);
destruct (eq_nat_dec (fg (n-1)) a).
+ exfalso.
generalize (@fg_max_two_antecedents (n-1) (S n)). lia.
+ exists n; exists (S n); repeat split; auto.
intros k Hk.
destruct (fg_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 (fg_inv n k) as [H|[H|H]]; try lia.
subst k. lia.
+ elim U.
intros u v Hu Hv.
assert (u = n).
{ destruct (fg_inv n u) as [H|[H|H]]; subst;
simpl in *; rewrite ?Nat.sub_0_r in *; lia. }
assert (v = n).
{ destruct (fg_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.
Lemma unary_or_multary n : Unary fg n \/ Multary fg n.
Proof.
rewrite unary_flip, multary_flip.
apply unary_or_multary.
Qed.
Lemma unary_xor_multary n : Unary fg n -> Multary fg n -> False.
Proof.
unfold Multary. intuition.
Qed.
(** We could even exhibit at least one child for each node *)
Definition rchild n :=
if eq_nat_dec n 1 then 2 else n + fg (S n) - 1.
(** rightmost son, always there *)
Lemma rightmost_child_carac a n : fg n = a ->
(fg (S n) = S a <-> n = rchild a).
Proof.
intros Hn.
destruct (le_lt_dec n 2).
- rewrite <- Hn.
destruct n as [|[|n]].
+ compute. auto.
+ compute. auto.
+ replace n with 0 by lia. compute. auto.
- assert (2 <= a).
{ change 2 with (fg 3). rewrite <- Hn. apply fg_mono. assumption. }
unfold rchild.
destruct eq_nat_dec.
+ destruct a as [|[|[|a]]]; trivial; lia.
+ assert (Hn' : 3 < S n) by lia.
assert (H' := fg_eqn Hn').
replace (S n - 1) with n in H' by lia.
rewrite Hn in H'.
lia.
Qed.
Lemma fg_onto_eqn a : fg (rchild a) = a.
Proof.
destruct (fg_onto a) as (n,Hn).
destruct (fg_step n) as [H|H].
- assert (H' := fg_nonflat _ H).
rewrite Hn in *.
rewrite rightmost_child_carac in H'; auto.
now rewrite <- H'.
- rewrite Hn in *.
rewrite rightmost_child_carac in H; auto.
now rewrite <- H.
Qed.
Definition lchild n :=
if eq_nat_dec n 1 then 1 else flip (FunG.rchild (flip n)).
(** leftmost son, always there (but might be equal to
the rightmost son for unary nodes) *)
Lemma lchild'_alt n : n<>1 -> lchild n = flip (flip n + flip (fg n)).
Proof.
unfold lchild, FunG.rchild, fg.
destruct eq_nat_dec; [intros; lia|intros].
f_equal. f_equal.
symmetry. apply flip_flip.
Qed.
Lemma fg_onto_eqn' n : fg (lchild n) = n.
Proof.
unfold fg, lchild.
destruct eq_nat_dec.
- now subst.
- rewrite flip_flip, g_onto_eqn.
apply flip_flip.
Qed.
Lemma lchild_leftmost n : fg (lchild n - 1) = n - 1.
Proof.
destruct (le_lt_dec n 1).
- destruct n as [|n].
+ now compute.
+ replace n with 0 by lia; now compute.
- set (k:=depth n).
assert (k<>0) by (unfold k; rewrite depth_0; lia).
assert (S (fib (S k)) <= n <= fib (S (S k))).
{ apply depth_carac; auto. }
destruct (eq_nat_dec n (S (fib (S k)))) as [E|N].
+ rewrite E.
replace (S (fib (S k)) - 1) with (fib (S k)) by lia.
unfold lchild.
destruct eq_nat_dec.
* generalize (@fib_nz k); intros; lia.
* rewrite flip_Sfib by lia.
unfold FunG.rchild. rewrite g_fib by lia.
rewrite <- fib_eqn.
rewrite flip_fib by lia.
replace (S (fib (S (S k))) - 1) with (fib (S (S k))) by lia.
apply fg_fib; lia.
+ unfold fg. apply flip_swap.
assert (1 < lchild n).
{ generalize (fg_onto_eqn' n).
generalize (@fg_mono (lchild n) 2). change (fg 2) with 1.
lia. }
rewrite !flip_pred; auto.
* unfold lchild.
destruct eq_nat_dec; [lia|].
rewrite flip_flip.
rewrite FunG.rightmost_child_carac; auto.
apply g_onto_eqn.
* change (depth n) with k; apply depth_carac; lia.
* assert (D : depth (lchild n) = S k).
{ unfold k. rewrite <- (fg_onto_eqn' n) at 2.
rewrite fg_depth. generalize (depth_0 (lchild n)).
lia. }
rewrite D.
apply depth_carac in D; auto.
assert (lchild n <> S (fib (S (S k)))).
{ contradict N.
rewrite <- (fg_onto_eqn' n), N. rewrite fg_Sfib; lia. }
apply depth_carac; auto. lia.
Qed.
Lemma lchild_leftmost' n a :
fg n = a -> n = lchild a \/ n = S (lchild a).
Proof.
intros Hn.
destruct (fg_inv (lchild a) n) as [H|[H|H]]; try lia.
- rewrite <- Hn. apply fg_onto_eqn'.
- exfalso.
generalize (lchild_leftmost a).
rewrite H. simpl. rewrite Nat.sub_0_r, Hn.
intros. replace a with 0 in * by lia.
discriminate.
Qed.
Lemma rchild_lchild n :
rchild n = lchild n \/ rchild n = S (lchild n).
Proof.
apply lchild_leftmost'. apply fg_onto_eqn.
Qed.
Lemma lchild_rchild n : lchild n <= rchild n.
Proof.
destruct (rchild_lchild n); lia.
Qed.
Lemma fg_children a n :
fg n = a -> (n = lchild a \/ n = rchild a).
Proof.
intros H.
destruct (lchild_leftmost' _ H) as [Hn|Hn]; auto.
destruct (rchild_lchild a) as [Ha|Ha]; try lia.
exfalso.
symmetry in Ha. apply rightmost_child_carac in Ha.
rewrite <- Hn in Ha. lia.
apply fg_onto_eqn'.
Qed.
Lemma binary_lchild_is_unary n :
1<n -> Multary fg n -> Unary fg (lchild n).
Proof.
rewrite multary_flip, unary_flip.
unfold lchild.
destruct eq_nat_dec; try lia.
rewrite flip_flip.
intros. now apply binary_rchild_is_unary.
Qed.
Lemma rightmost_son_is_binary n :
1<n -> Multary fg (rchild n).
Proof.
intros.
rewrite multary_flip.
apply leftmost_son_is_binary with (flip n).
- apply flip_swap. apply fg_onto_eqn.
- rewrite <- flip_swap.
set (k:=depth n).
assert (k<>0) by (unfold k; rewrite depth_0; lia).
assert (S (fib (S k)) <= n <= fib (S (S k))).
{ apply depth_carac; auto. }
destruct (eq_nat_dec n (fib (S (S k)))) as [E|N].
+ assert (E' : rchild n = fib (S (S (S k)))).
{ symmetry.
apply rightmost_child_carac.
- now rewrite fg_fib.
- rewrite fg_Sfib; lia. }
rewrite E'.
rewrite flip_fib by lia.
replace (S (fib (S (S k))) - 1) with (fib (S (S k))) by lia.
rewrite g_fib by lia.
rewrite E, flip_swap, flip_fib; lia.
+ assert (1 < rchild n).
{ generalize (fg_onto_eqn n).
generalize (@fg_mono (rchild n) 2). change (fg 2) with 1.
lia. }
rewrite <- flip_S; auto.
* change (fg (S (rchild n)) <> n).
assert (H' := fg_onto_eqn n).
destruct (fg_step (rchild n)); try lia.
apply rightmost_child_carac in H'. lia.
* assert (D : depth (rchild n) = S k).
{ unfold k. rewrite <- (fg_onto_eqn n) at 2.
rewrite fg_depth. generalize (depth_0 (rchild n)).
lia. }
rewrite D.
apply depth_carac in D; auto.
assert (rchild n <> fib (S (S (S k)))).
{ contradict N.
rewrite <- (fg_onto_eqn n), N. now rewrite fg_fib. }
apply depth_carac; auto. lia.
Qed.
Lemma unary_child_is_binary n :
n <> 0 -> Unary fg n -> Multary fg (rchild n).
Proof.
intros Hn H.
destruct (le_lt_dec n 1).
- replace n with 1 in * by lia.
exfalso.
specialize (H 1 2). compute in H. lia.
- now apply rightmost_son_is_binary.
Qed.
Lemma binary_rchild_is_binary n :
1<n -> Multary fg n -> Multary fg (rchild n).
Proof.
intros. now apply rightmost_son_is_binary.
Qed.
(** Hence the shape of the [fg] tree is a repetition of
this pattern:
<<
r
|
p q
\ /
n
>>
where [n] and [q] and [r] are binary nodes and [p] is unary.
As expected, this is the mirror of the [G] tree.
We hence retrieve the fractal aspect of [G], flipped.
*)
(*=============================================================*)
(** * Comparison of [fg] and [g] *)
(** First, a few technical lemmas *)
Lemma fg_g_S_inv n : 3<n ->
fg (S (fg (n-1))) = g (g (n-1)) ->
fg n = S (g n).
Proof.
intros Hn H.
replace (fg n) with (S n - fg (S (fg (n-1))))
by (rewrite <- fg_eqn; lia).
replace n with (S (n-1)) at 3 by lia.
rewrite g_S.
replace (S (n-1)) with n by lia.
rewrite H.
assert (g (g (n-1)) <= n).
{ transitivity (g (n-1)). apply g_le.
generalize (g_le (n-1)); lia. }
lia.
Qed.
Lemma fg_g_eq_inv n : 3<n ->
fg (S (fg (n-1))) = S (g (g (n-1))) ->
fg n = g n.
Proof.
intros Hn H.
replace (fg n) with (S n - fg (S (fg (n-1))))
by (rewrite <- fg_eqn; lia).
replace n with (S (n-1)) at 3 by lia.
rewrite g_S.
replace (S (n-1)) with n by lia.
rewrite H. lia.
Qed.
(** Now, the proof that [fg(n)] is either [g(n)] or [g(n)+1].
This proof is split in many cases according to the shape
of the Fibonacci decomposition of [n]. *)
Definition IHeq n :=
forall m, m<n -> ~Fib.ThreeOdd 2 m -> fg m = g m.
Definition IHsucc n :=
forall m, m<n -> Fib.ThreeOdd 2 m -> fg m = S (g m).
Lemma fg_g_aux0 n : IHeq n -> Fib.ThreeOdd 2 n -> fg n = S (g n).
Proof.
intros IH TE.
assert (6 <= n) by now apply ThreeOdd_le.
apply fg_g_S_inv; try lia.
rewrite (IH (n-1)), IH; try (generalize (@g_lt (n-1)); lia).
- rewrite Odd_gP by autoh. apply g_Two, Three_g; autoh.
- rewrite Odd_gP by autoh.
now apply ThreeEven_not_ThreeOdd', ThreeOdd_Sg.
- apply Two_not_ThreeOdd, Odd_pred_Two; autoh.
Qed.
Lemma fg_g_aux1 n : IHeq n -> 3<n -> Fib.Two 2 n -> fg n = g n.
Proof.
intros IH N FO.
apply fg_g_eq_inv; auto.
assert (High 2 (n-1)). { apply Two_pred_High; auto; lia. }
assert (Even 2 (g n)) by now apply Two_g.
rewrite (IH (n-1)) by autoh.
rewrite 2 Even_gP; auto using Two_Even.
assert (g n <> 0) by (apply g_nz; lia).
assert (g (g n) <> 0) by now apply g_nz.
replace (S (g n - 1)) with (g n) by lia.
rewrite IH; autoh; try apply g_lt; lia.
Qed.
Lemma fg_g_aux2 n :
IHeq n -> IHsucc n -> 3<n -> Fib.ThreeEven 2 n -> fg n = g n.
Proof.
intros IH1 IH2 N TO.
apply fg_g_eq_inv; try lia.
assert (H' := @g_lt (n-1)).
rewrite (IH1 (n-1)), IH2; try lia.
- rewrite Odd_gP by autoh.
f_equal. apply g_Two. apply Three_g; autoh.
- rewrite Odd_gP by autoh. now apply ThreeEven_Sg.
- apply Two_not_ThreeOdd, Three_pred_Two; autoh.
Qed.
Lemma fg_g_aux3 n : IHeq n -> IHsucc n -> 3<n ->
Fib.High 2 n -> fg n = g n.
Proof.
intros IH1 IH2 Hn (k & K & L).
apply fg_g_eq_inv; auto.
destruct (Nat.Even_or_Odd k) as [(p,Hp)|(p,Hp)]; subst k.
- (* even *)
assert (E1 : g (n-1) = g n - 1) by (apply Even_gP; now exists p).
assert (S (fg (n-1)) < n) by (generalize (@fg_lt (n-1)); lia).
assert (g n <> 0) by (apply g_nz; lia).
assert (E2 : S (g n - 1) = g n) by lia.
destruct p as [|[|[|p]]].
+ lia.
+ lia.
+ (* four *)
simpl in *.
assert (T : Three 2 (g n)) by (revert L; apply g_Low; auto).
rewrite E1, Odd_gP by autoh.
destruct L as (l & E & D & _).
destruct l as [|k l]; [simpl in E; lia|].
destruct (Nat.Even_or_Odd k) as [(p,Hp)|(p,Hp)]; subst k.
* (* next term after 3 is even *)
assert (ThreeEven 2 (n-1)).
{ exists p; exists l; auto. subst; split; auto.
eapply Delta_low_hd with 4; auto. }
rewrite (IH1 (n-1)) in * by autoh.
rewrite E1, E2, IH2 in *; auto.
replace n with (S (n-1)) by lia.
rewrite g_not_Two. now apply ThreeEven_Sg.
intro. eapply Two_not_Three; eauto using ThreeEven_Three.
* (* next term after 4 is odd *)
assert (ThreeOdd 2 (n-1)).
{ exists p; exists l; auto. subst; split; auto.
eapply Delta_low_hd with 4; auto. }
rewrite (IH2 (n-1)) in * by autoh.
rewrite E1, E2, IH1 in *; auto using Odd_succ_Even with hof.
apply g_not_Two.
intro. eapply Even_xor_Odd; eauto using Two_Even with hof.
+ (* high odd *)
remember (S (S p)) as k eqn:K'.
assert (1<k) by lia. clear K K'.
assert (Even 2 n) by now exists (S k).
assert (~Two 2 n).
{ intro O. generalize (Low_unique L O). lia. }
assert (~Four 2 n).
{ intro T. generalize (Low_unique L T). lia. }
assert (ThreeOdd 2 (n-1)) by now apply EvenHigh_pred_ThreeOdd.
rewrite (IH2 (n-1)) in *; auto; try lia.
rewrite E1, E2 in *.
assert (Ev : Odd 2 (g n)) by now apply Even_g.
rewrite Odd_gP by auto.
rewrite IH1; auto using Odd_succ_Even with hof.
apply g_not_Two.
intro. apply Even_xor_Odd with (g n); eauto using Two_Even with hof.
- (* odd *)
assert (High 2 n) by (exists (2*p+1); auto).
assert (Odd 2 n) by now exists p.
rewrite (IH1 (n-1));
[ | lia | now apply Two_not_ThreeOdd, Odd_pred_Two ].
assert (S (g (n-1)) < n) by (generalize (@g_lt (n-1)); lia).
rewrite Odd_gP in * by auto.
rewrite IH1; try lia.
+ apply g_not_Two. now apply High_g.
+ now apply Even_not_ThreeOdd, High_Sg.
Qed.
(** The main result: *)
Lemma fg_g n :
(Fib.ThreeOdd 2 n -> fg n = S (g n)) /\
(~Fib.ThreeOdd 2 n -> fg n = g n).
Proof.
induction n as [n IH] using lt_wf_rec.
assert (IH1 := fun m (H:m<n) => proj1 (IH m H)).
assert (IH2 := fun m (H:m<n) => proj2 (IH m H)).
clear IH.
split.
- now apply fg_g_aux0.
- intros.
destruct (le_lt_dec n 3) as [LE|LT].
+ destruct n as [|[|[|n]]]; try reflexivity.
replace n with 0 by lia. reflexivity.
+ assert (LT' : 2 < n) by lia.
destruct (decomp_complete' LT') as [X|[X|[X|X]]].
* now apply fg_g_aux1.
* now apply fg_g_aux2.
* intuition.
* now apply fg_g_aux3.
Qed.
(** Note: the positions where [fg] and [g] differ start at 7
and then are separated by 5 or 8 (see [Fib.ThreeOdd_next]
and related lemmas). Moreover these positions are always
unary nodes in [G] (see [FunG.decomp_unary]).
In fact, the [g] tree can be turned into the [fg] tree
by repeating the following transformation whenever [s] below
is ThreeOdd:
<<
r s t r s t
\ / | | \ /
p q becomes p q
\ / \ /
n n
>>
In the left pattern above, [s] is ThreeOdd, hence [r] and
[p] and [n] are Two, [q] is ThreeEven and [t] is High.
*)
(** Some immediate consequences: *)
Lemma fg_g_step n : fg n = g n \/ fg n = S (g n).
Proof.
destruct (le_lt_dec n 3) as [LE|LT].
- left.
destruct n as [|[|[|n]]]; try reflexivity.
replace n with 0 by lia. reflexivity.
- assert (LT' : 2 < n) by lia.
destruct (decomp_complete' LT') as [X|[X|[X|X]]].
* left. apply fg_g. now apply Two_not_ThreeOdd.
* left. apply fg_g. now apply ThreeEven_not_ThreeOdd.
* right. now apply fg_g.
* left. apply fg_g. now apply High_not_ThreeOdd.
Qed.
Lemma g_le_fg n : g n <= fg n.
Proof.
destruct (fg_g_step n); lia.
Qed.
(*=============================================================*)
(** * [fg] and "delta" equations *)
(** We can characterize [fg] via its "delta" (a.k.a increments).
Let [d(n) = fg(n+1)-fg(n)]. For [n>3] :