(** * Skelet #34 *)
(** Following https://www.sligocki.com/2023/02/02/skelet-34.html *)
Set Default Goal Selector "!".
Definition tm : TM := fun '(q, s) =>
match q, s with
| A, 0 => Some (1, R, B)  | A, 1 => Some (1, L, C)
| B, 0 => Some (0, R, C)  | B, 1 => Some (0, R, B)
| C, 0 => Some (1, L, D)  | C, 1 => Some (0, L, A)
| D, 0 => Some (1, L, E)  | D, 1 => None
| E, 0 => Some (1, L, A)  | E, 1 => Some (1, R, A)
end.
Notation "c --> c'" := (c -[ tm ]-> c')   (at level 40).
Notation "c -->* c'" := (c -[ tm ]->* c') (at level 40).
Notation "c -->+ c'" := (c -[ tm ]->+ c') (at level 40).
Notation P := Pos.succ.
Definition D n m : Q * tape :=
L n <{{C}} [1; 0; 1; 0] *> R m.
Definition E n a m : Q * tape :=
K n <{{C}} [1; 0; 1; a] *> R m.
Definition f (m : positive) (a : sym) (k : nat) : positive :=
let t := (pow2 k - 1 :+ m) in
match a with
| 0 => t~0~0
| 1 => t~1~0
end.

Lemma prepare_K : forall (n : N), (n > 0)%N -> exists (k : nat) (n' : N),
K n = K n' <* <[0; 1; 0; 0] <* Lk (bin_min k)
/\ (n = pow2 k + pow2 (S k) * n')%N.