Library FlipG: Hofstadter's flipped G tree
Require Import Arith Omega Wf_nat List Program Program.Wf NPeano.
Require Import DeltaList Fib FunG.
Set Implicit Arguments.
See first the file FunG for the study of:
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 ?
References:
9 10 11 12 13 \/ | \ / 6 7 8 \ \ / 4 5 \ / 3 | 2 | 1
- Hofstadter's book: Goedel, Escher, Bach, page 137.
- Sequence A123070 on the Online Encyclopedia of Integer Sequences http://oeis.org/A123070
The flip function
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, leb_le.
Lemma flip_depth n : depth (flip n) = depth n.
Lemma flip_eqn0 n : depth n ≠ 0 →
flip n = S (fib (S (S (S (depth n))))) - n.
Lemma flip_eqn k n : 1 ≤ n ≤ fib k →
flip (fib (S k) + n) = S (fib (S (S k))) - n.
Two special cases : leftmost and rightmost node at a given depth
Lemma flip_Sfib k : 1<k → flip (S (fib k)) = fib (S k).
Lemma flip_fib k : 1<k → flip (fib (S k)) = S (fib k).
flip is involutive (and hence a bijection)
Lemma flip_flip n : flip (flip n) = n.
Lemma flip_eq n m : flip n = flip m ↔ n = m.
Lemma flip_swap n m : flip n = m ↔ n = flip m.
Lemma flip_low n : n ≤ 1 ↔ flip n ≤ 1.
Lemma flip_high n : 1 < n ↔ 1 < flip n.
flip and neighbors
Lemma flip_S n : 1<n → depth (S n) = depth n →
flip (S n) = flip n - 1.
Lemma flip_pred n : 1<n → depth (n-1) = depth n →
flip (n-1) = S (flip n).
Definition fg n := flip (g (flip n)).
Lemma fg_depth n : depth (fg n) = depth n - 1.
Lemma fg_fib k : k≠0 → fg (fib (S k)) = fib k.
Lemma fg_Sfib k : 1<k → fg (S (fib (S k))) = S (fib k).
Lemma fg_fib´ k : 1<k → fg (fib k) = fib (k-1).
Lemma fg_Sfib´ k : 2<k → fg (S (fib k)) = S (fib (k-1)).
Lemma fg_step n : fg (S n) = fg n ∨ fg (S n) = S (fg n).
Lemma fg_mono_S n : fg n ≤ fg (S n).
Lemma fg_mono n m : n≤m → fg n ≤ fg m.
Lemma fg_lipschitz n m : fg m - fg n ≤ m - n.
Lemma fg_nonzero n : 0 < n → 0 < fg n.
Lemma fg_0_inv n : fg n = 0 → n = 0.
Lemma fg_nz n : n ≠ 0 → fg n ≠ 0.
Lemma fg_fix n : fg n = n ↔ n ≤ 1.
Lemma fg_le n : fg n ≤ n.
Lemma fg_lt n : 1<n → fg n < n.
Lemma fg_onto a : ∃ n, fg n = a.
Lemma fg_nonflat n : fg (S n) = fg n → fg (S (S n)) = S (fg n).
Lemma fg_max_two_antecedents n m :
fg n = fg m → n < m → m = S n.
Lemma fg_inv n m : fg n = fg m → n = m ∨ n = S m ∨ m = S n.
Lemma fg_eqn n : 3 < n → fg n + fg (S (fg (n-1))) = S n.
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 →
(∀ n, 3<n → f n + f (S (f (n-1))) = S n) →
∀ n, f n = fg n.
fg and the shape of its tree
Lemma unary_flip a : Unary fg a ↔ Unary g (flip a).
Lemma multary_flip a : Multary fg a ↔ Multary g (flip a).
Lemma fg_multary_binary a : Multary fg a ↔ Binary fg a.
Lemma unary_or_multary n : Unary fg n ∨ Multary fg n.
Lemma unary_xor_multary n : Unary fg n → Multary fg n → False.
We could even exhibit at least one child for each node
rightmost son, always there
Lemma rightmost_child_carac a n : fg n = a →
(fg (S n) = S a ↔ n = rchild a).
Lemma fg_onto_eqn a : fg (rchild a) = a.
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)).
Lemma fg_onto_eqn´ n : fg (lchild n) = n.
Lemma lchild_leftmost n : fg (lchild n - 1) = n - 1.
Lemma lchild_leftmost´ n a :
fg n = a → n = lchild a ∨ n = S (lchild a).
Lemma rchild_lchild n :
rchild n = lchild n ∨ rchild n = S (lchild n).
Lemma lchild_rchild n : lchild n ≤ rchild n.
Lemma fg_children a n :
fg n = a → (n = lchild a ∨ n = rchild a).
Lemma binary_lchild_is_unary n :
1<n → Multary fg n → Unary fg (lchild n).
Lemma rightmost_son_is_binary n :
1<n → Multary fg (rchild n).
Lemma unary_child_is_binary n :
n ≠ 0 → Unary fg n → Multary fg (rchild n).
Lemma binary_rchild_is_binary n :
1<n → Multary fg n → Multary fg (rchild n).
Hence the shape of the fg tree is a repetition of
this pattern:
As expected, this is the mirror of the G tree.
We hence retrieve the fractal aspect of G, flipped.
r | p q \ / nwhere n and q and r are binary nodes and p is unary.
Lemma fg_g_S_inv n : 3<n →
fg (S (fg (n-1))) = g (g (n-1)) →
fg n = S (g n).
Lemma fg_g_eq_inv n : 3<n →
fg (S (fg (n-1))) = S (g (g (n-1))) →
fg n = g n.
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 :=
∀ m, m<n → ¬Fib.ThreeOdd 2 m → fg m = g m.
Definition IHsucc n :=
∀ 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).
Lemma fg_g_aux1 n : IHeq n → 3<n → Fib.Two 2 n → fg n = g n.
Lemma fg_g_aux2 n :
IHeq n → IHsucc n → 3<n → Fib.ThreeEven 2 n → fg n = g n.
Lemma fg_g_aux3 n : IHeq n → IHsucc n → 3<n →
Fib.High 2 n → fg n = g n.
The main result:
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:
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:
r s t r s t \ / | | \ / p q becomes p q \ / \ / n n
fg and "delta" equations
- a) if d(n-1) = 0 then d(n) = 1
- b) if d(n-1) ≠ 0 and d(fg(n)) = 0 then d(n) = 1
- c) if d(n-1) ≠ 0 and d(fg(n)) ≠ 0 then d(n) = 0
Inductive FD : nat → nat → Prop :=
| FD_0 : FD 0 0
| FD_1 : FD 1 1
| FD_2 : FD 2 1
| FD_3 : FD 3 2
| FD_4 : FD 4 3
| FD_a n x : 4<n → FD (n-2) x → FD (n-1) x → FD n (S x)
| FD_b n x y z : 4<n → FD (n-2) x → FD (n-1) y → x≠y →
FD y z → FD (S y) z → FD n (S y)
| FD_c n x y z t : 4<n → FD (n-2) x → FD (n-1) y → x≠y →
FD y z → FD (S y) t → z ≠ t → FD n y.
Hint Constructors FD.
Lemma FD_le n k : FD n k → k ≤ n.
Lemma FD_nz n k : FD n k → 0<n → 0<k.
Lemma FD_lt n k : FD n k → 1<n → 0<k<n.
Lemma FD_unique n k k´ : FD n k → FD n k´ → k = k´.
Lemma FD_step n k k´ : FD n k → FD (S n) k´ → k´=k ∨ k´ = S k.
The three situations a) b) c) expressed in terms of fg.
Lemma fg_a n : 0<n → fg (n-2) = fg (n-1) → fg n = S (fg (n-1)).
Lemma fg_b n y : 4<n →
y = fg (n-1) →
fg (n-2) ≠ y →
fg (S y) = fg y →
fg n = S y.
Lemma fg_c n y : 4<n →
y = fg (n-1) →
fg (n-2) ≠ y →
fg (S y) ≠ fg y →
fg n = y.
An old auxiliary lemma stating the converse of the c) case
Presentation via a "delta" function
Definition d n := fg (S n) - fg n.
Lemma delta_0_1 n : d n = 0 ∨ d n = 1.
Lemma delta_a n : n≠0 → d (n-1) = 0 → d n = 1.
Lemma delta_b n : 4≤n →
d (n-1) = 1 → d (fg n) = 0 → d n = 1.
Lemma delta_c n : 4≤n →
d (n-1) = 1 → d (fg n) = 1 → d n = 0.
Lemma delta_bc n : 4≤n → d (n-1) = 1 → d n = 1 - d (fg n).
Lemma delta_eqn n : 4≤n →
d n = 1 - d (n-1) × d (fg n).
An alternative equation for fg
This last equation f(f(n)) + f(n-1) = n for n > 3
is nice and short, but unfortunately it doesn't define
a unique function, even if the first values are fixed to
0 1 1 2. For instance:
Definition oups n :=
match n with
| 0 ⇒ 0
| 1 ⇒ 1
| 2 ⇒ 1
| 3 ⇒ 2
| 4 ⇒ 3
| 5 ⇒ 3
| 6 ⇒ 5
| 7 ⇒ 3
| _ ⇒ if even n then n-2 else 4
end.
Lemma oups_def n : 7<n → oups n = if even n then n-2 else 4.
Lemma oups_alt_eqn n : 3<n → oups (oups n) + oups (n-1) = n.
We will show below that if we require this equation along
with a monotonicity constraint, then there is a unique
solution (which is hence fg).
Study of the alternative equation and its consequences.
Definition AltSpec (f:nat→nat) :=
(f 0 = 0 ∧ f 1 = 1 ∧ f 2 = 1 ∧ f 3 = 2) ∧
(∀ n, 3<n → f (f n) + f (n-1) = n).
Lemma alt_spec_fg : AltSpec fg.
Lemma alt_spec_oups : AltSpec oups.
Lemma alt_bound f : AltSpec f → ∀ n, 1<n → 0 < f n < n.
Lemma alt_bound´ f : AltSpec f → ∀ n, 0 ≤ f n ≤ n.
Lemma alt_4 f : AltSpec f → f 4 = 3.
Lemma alt_5 f : AltSpec f → f 5 = 3.
Lemma monotone_equiv f :
(∀ n, f n ≤ f (S n)) →
(∀ n m, n ≤ m → f n ≤ f m).
Lemma alt_mono_bound f : AltSpec f →
(∀ n, f n ≤ f (S n)) →
∀ n m, n ≤ m → f m - f n ≤ m-n.
Lemma alt_mono_unique f1 f2 :
AltSpec f1 → (∀ n, f1 n ≤ f1 (S n)) →
AltSpec f2 → (∀ n, f2 n ≤ f2 (S n)) →
∀ n, f1 n = f2 n.
Lemma alt_mono_is_fg f :
AltSpec f → (∀ n, f n ≤ f (S n)) →
∀ n, f n = fg n.