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')).

Lemma perm_halted : forall tm tm' f q t,
Perm tm tm' f ->
halted tm (q;; t) ->
halted tm' (f q;; t).