Section ClosedSetSearcher.
Hypothesis T:Type.
Hypothesis T_enc:T->positive.
Hypothesis T_enc_inj: is_inj T_enc.
Hypothesis In_T: (ExecState Σ)->T->Prop.
Hypothesis T_InitES:T.
Hypothesis In_T_InitES: In_T (InitES Σ Σ0) T_InitES.
Hypothesis T_step: (TM Σ)->T->option (list T).
Hypothesis T_step_spec:
forall (tm : TM Σ) (x : T),
match T_step tm x with
| Some ls =>
forall st0 : ExecState Σ,
In_T st0 x ->
exists (n : nat) (st1 : ExecState Σ) (x1 : T),
Steps Σ tm (1 + n) st0 st1 /\ In_T st1 x1 /\ In x1 ls
| None => True
end.
Definition T_eqb t1 t2 := Pos.eqb (T_enc t1) (T_enc t2).
Fixpoint ins_all(q:list T)(st:PositiveMap.tree unit)(ls:list T) :=
match ls with
| nil => (q,st)
| h::ls0 =>
let (q',st') := fst (set_ins T_enc (q,st) h) in
ins_all q' st' ls0
end.
Fixpoint T_close_set_searcher(tm:TM Σ)(n:nat)(q:list T)(st:PositiveMap.tree unit):
option (list T * PositiveMap.tree unit) :=
match n with
| O => Some (q,st)
| S n0 =>
match q with
| nil => Some (q,st)
| x::q0 =>
match T_step tm x with
| None => None
| Some ls =>
let (q',st') := ins_all q0 st ls in
T_close_set_searcher tm n0 q' st'
end
end
end.
Definition T_decider0(n:nat)(tm:TM Σ):bool :=
let (q0,st0) := fst (set_ins T_enc (nil, PositiveMap.empty unit) T_InitES) in
match T_close_set_searcher tm n q0 st0 with
| Some (nil,q2') =>
match PositiveMap.find (T_enc T_InitES) q2' with
| Some _ => true
| None => false
end
| _ => false
end.
Definition T_decider(n:nat):HaltDecider :=
fun tm => if T_decider0 n tm then Result_NonHalt else Result_Unknown.
Definition search_state_WF tm (qst:(list T * PositiveMap.tree unit)):=
let (q,st):=qst in
forall x,
set_in T_enc qst x -> (In x q \/
match T_step tm x with
| None => False
| Some ls => forall y, In y ls -> set_in T_enc qst y
end).
Definition in_search_state(st0:ExecState Σ)(S:(list T * PositiveMap.tree unit)) :=
exists s0, set_in T_enc S s0 /\ In_T st0 s0.

Lemma T_decider_spec n BB:
HaltDecider_WF BB (T_decider n).