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 {_ _} _ _.
(* ** Correctness of [While] *)
Variable R : pRel sig (option F) n.
Inductive While_Rel : pRel sig F n :=
| While_Rel''_one :
forall tin yout tout, R tin (Some yout, tout) -> While_Rel tin (yout, tout)
| While_Rel''_loop :
forall tin tmid yout tout,
R tin (None, tmid) ->
While_Rel tmid (yout, tout) ->
While_Rel tin (yout, tout).

Lemma While_Realise :
pM ⊨ R -> While ⊨ While_Rel.