Section TM.
Hypothesis Σ:Set.
Hypothesis Σ0:Σ.
Record Trans:Set := {
nxt: St;
dir: Dir;
out: Σ;
}.
Definition TM:Set := St->Σ->option Trans.
Definition ExecState:Set := St*(Z->Σ).
Definition InitES:ExecState := (St0,fun _=>Σ0).
Definition upd(t:(Z->Σ))(o:Σ) :=
fun x:Z => if Z.eqb x Z0 then o else t x.
Definition mov(t:(Z->Σ))(d:Dir) :=
fun x:Z => t ((x+(Dir_to_Z d))%Z).
Definition step(tm:TM)(st:ExecState): option ExecState :=
let (s,t):=st in
match tm s (t Z0) with
| None => None
| Some tr =>
let (s',d,o):=tr in
Some (s',mov (upd t o) d)
end.
Inductive Steps: TM->nat->ExecState->ExecState->Prop :=
| steps_O tm st:
Steps tm O st st
| steps_S tm n st st0 st1:
Steps tm n st st0 ->
step tm st0 = Some st1 ->
Steps tm (S n) st st1.
Definition HaltsAt(tm:TM)(n:nat)(st:ExecState): Prop :=
exists st', Steps tm n st st' /\ step tm st' = None.
Definition Halts(tm:TM)(st:ExecState): Prop :=
exists n, HaltsAt tm n st.
Definition HaltsFromInit(tm:TM): Prop :=
Halts tm InitES.
Definition NonHalt tm st :=
forall n, exists st', Steps tm n st st'.
Definition LE(tm tm':TM): Prop :=
forall (s:St)(i:Σ),
tm s i = tm' s i \/
tm s i = None.
Hypothesis BB:nat.
Definition HaltTimeUpperBound(st:ExecState)(P:TM->Prop):Prop :=
forall (tm:TM)(n0:nat), P tm -> HaltsAt tm n0 st -> n0<=BB.
Hypothesis Σ_eqb:Σ->Σ->bool.
Hypothesis Σ_eqb_spec: forall i1 i2, if Σ_eqb i1 i2 then i1=i2 else i1<>i2.
Definition TM_upd tm s i t: TM :=
fun s0 i0 =>
if (andb (St_eqb s0 s) (Σ_eqb i0 i)) then t else tm s0 i0.
Ltac Σ_eq_dec s1 s2 :=
eq_dec Σ_eqb_spec Σ_eqb s1 s2.

Lemma HaltTimeUpperBound_LE_Halt st tm n s t:
HaltsAt tm n st ->
Steps tm n st (s,t) ->
n<=BB ->
(forall tr, HaltTimeUpperBound st (LE (TM_upd tm s (t Z0) (Some tr)))) ->
HaltTimeUpperBound st (LE tm).