Section While.
Variable n : nat.
Variable sig : finType.
Variable F : finType.
(* Label [None] indicates continueing, [Some f] means breaking out of the loop and terminating in the label [f]. *)
Variable pM : pTM sig (option F) n.
Definition While_trans :
(TM.state (projT1 pM)) * Vector.t (option sig) n ->
(TM.state (projT1 pM)) * Vector.t (option sig * move) n :=
fun '(q,s) =>
if halt q
then (start (projT1 pM), nop_action)
else trans (q,s).
Definition WhileTM : TM sig n :=
Build_TM While_trans (start (projT1 pM))
(fun q => halt q && match projT2 pM q with
| Some _ => true
| None => false
end).
Hypothesis (defF : inhabitedC F).
Definition While_part : state (projT1 pM) -> F :=
fun q =>
match projT2 pM q with
| Some y => y
| None => default
end.
Definition While : pTM sig F n :=
(WhileTM; While_part).
Local Arguments loopM {_ _} _ _ _.
Local Arguments halt {_ _} _ _.
Local Arguments step {_ _} _ _.

Lemma While_merge_repeat k1 k2 (c1 c2 c3 : mconfig sig (state WhileTM) n) :
loopM (projT1 pM) c1 k1 = Some c2 ->
(projT2 pM) (cstate c2) = None ->
loopM WhileTM (initc WhileTM (ctapes c2)) k2 = Some c3 ->
loopM WhileTM c1 (k1+(1+k2)) = Some c3.