(** * Skelet #26 *)
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, D)
| B, 0 => Some (1, R, C)  | B, 1 => Some (0, R, B)
| C, 0 => Some (1, L, A)  | C, 1 => Some (1, R, C)
| D, 0 => Some (1, L, E)  | D, 1 => Some (0, L, A)
| E, 0 => Some (1, L, C)  | E, 1 => None
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 a m : Q * tape :=
L n <{{D}} 1 >> 0 >> 1 >> a >> R m.
Fixpoint J' (n : positive) : side :=
match n with
| xH =>    const 0 << 1
| xO n => J' n << 0 << 0 << 0 << 0
| xI n => J' n << 0 << 0 << 0 << 1
end.
Definition J (n : N) : side :=
match n with
| N0 => const 0
| Npos n => J' n
end.
Definition E0 n a m : Q * tape :=
K n <{{D}} 1 >> 0 >> 1 >> a >> R m.
Definition E1 n a m : Q * tape :=
J n <{{D}} 1 >> 0 >> 1 >> a >> R m.
Fixpoint Lk {k} (n : bin k) (l : side) :=
match n with
| bb => l
| b0 n => Lk n l << 0 << 0 << 0 << 0
| b1 n => Lk n l << 1 << 0 << 0 << 0
end.
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.
Definition f1 (m : positive) (a : sym) (k : nat) : positive :=
let t := (pow2 k - 1 :+ m) in
match a with
| 0 => t~0
| 1 => t~1
end.
Definition reset_invariant (m : positive) :=
(m >= 2)%positive /\
exists k n', N.succ (b m) = (pow2 k + pow2 (S k) * n')%N /\ (n' >= 2)%N.

Theorem step_reset1 : forall n m a,
(n <= 4 * b m)%N ->
(exists (k : nat) (n' : N),
(n = (pow2 (S k) + pow2 (S (S k)) * n'))%N /\
(pow2 (S k) <= b m)%N) ->
exists (n' : N) (m' : positive),
E1 n a m -->* E0 n' 0 m' /\
(n' < n)%N /\ (n' <= b m')%N /\ (reset_invariant m').