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_split k (c1 c3 : mconfig sig (state (projT1 pM)) n) :
loopM WhileTM c1 k = Some c3 ->
exists k1 k2 c2,
loopM (projT1 pM) c1 k1 = Some c2 /\
loopM WhileTM c2 k2 = Some c3 /\
k1 + k2 <= k.