Module Flip := Flip Ctx. Export Flip.
(* [f q = q'], where [q] is a state in [tm] and [q'] is the equivalent state in [tm']. *)
(* Note that this definition also admits mapping two equivalent states in [tm] to the same state in [tm']. This is intentional, though the name isn't perfect. *)
Definition Perm (tm tm' : TM) (f : Q -> Q) :=
(forall q s,
tm (q, s) = None ->
tm' (f q, s) = None) /\
(forall q s s' d q',
tm (q, s) = Some (s', d, q') ->
tm' (f q, s) = Some (s', d, f q')).
Local Hint Resolve perm_halted perm_halted' : core.
Local Hint Resolve perm_step : core.
Local Hint Resolve perm_multistep : core.

Lemma perm_halts_in : forall tm tm' f q t n,
Perm tm tm' f ->
halts_in tm (q;; t) n ->
halts_in tm' (f q;; t) n.