Section rev.
Definition Trans_rev(tr:Trans) :=
let (s',d,o):=tr in
{| nxt:=s'; dir:=Dir_rev d; out:=o |}.
Definition option_Trans_rev(o:option Trans) :=
match o with
| None => None
| Some tr => Some (Trans_rev tr)
end.
Definition TM_rev(tm:TM) :=
fun s i => option_Trans_rev (tm s i).
Definition Tape_rev(t:Z->Σ) :=
fun x:Z => t (-x)%Z.
Definition ExecState_rev(st:ExecState) :=
let (s,t):=st in
(s,Tape_rev t).
End rev.
End TM.
Definition UnusedState(tm:TM Σ)(s:St): Prop :=
(forall s0 i,
match tm s0 i with
| None => True
| Some tr => nxt Σ tr <> s
end) /\
(forall i, tm s i = None) /\
s <> St0.
Definition isUnusedState tm s: bool :=
forallb_St (fun s0 =>
forallb_Σ (fun i =>
match tm s0 i with
| None => true
| Some tr => negb (St_eqb (nxt Σ tr) s)
end)) &&&
forallb_Σ (fun i => match tm s i with None => true | _ => false end) &&&
negb (St_eqb s St0).
Ltac Σ_eq_dec s1 s2 :=
eq_dec Σ_eqb_spec Σ_eqb s1 s2.
Definition TM0: TM Σ :=
fun x i => None.
Definition St_to_nat(x:St):nat:=
match x with
| St0 => 0
| St1 => 1
| St2 => 2
| St3 => 3
end.
Definition St_suc(x:St):St :=
match x with
| St0 => St1
| St1 => St2
| St2 => St3
| St3 => St3
end.
Definition St_le(s1 s0:St):Prop :=
St_to_nat s0 <= St_to_nat s1.
Definition UnusedState_ptr tm s1:=
(forall s0, UnusedState tm s0 <-> St_le s0 s1) \/
((forall s0, ~UnusedState tm s0) /\ forall s0, St_le s1 s0).

Lemma HaltTimeUpperBound_LE_HaltAtES_UnusedState_ptr {tm n s t s1 BB}:
HaltsAt _ tm n (InitES Σ Σ0) ->
Steps _ tm n (InitES Σ Σ0) (s,t) ->
n<=BB ->
UnusedState_ptr tm s1 ->
(forall tr,
St_le s1 (nxt _ tr) ->
HaltTimeUpperBound _ BB (InitES Σ Σ0) (LE _ (TM_upd Σ Σ_eqb tm s (t Z0) (Some tr)))) ->
HaltTimeUpperBound _ BB (InitES Σ Σ0) (LE _ tm).