Section NGramCPS.
Hypothesis Σ:Set.
Hypothesis len_l:nat.
Hypothesis len_r:nat.
Record MidWord:Set := {
l:list Σ;
r:list Σ;
m:Σ;
s:St;
}.
Fixpoint tape_seg(t:Z->Σ)(x:Z)(d:Dir)(n:nat):list Σ :=
match n with
| O => nil
| S n0 => (t x)::(tape_seg t (x+(Dir_to_Z d))%Z d n0)
end.
Definition MidWord_matches(st:ExecState Σ)(mw:MidWord):Prop :=
let (s,t) := st in
let (l0,r0,m0,s0):=mw in
s = s0 /\
m0 = t Z0 /\
l0 = tape_seg t ((-1)%Z) Dneg len_l /\
r0 = tape_seg t (1%Z) Dpos len_r.
Record AbstractES:Type := {
lset: (list Σ)->Prop;
rset: (list Σ)->Prop;
mset: MidWord->Prop;
}.
Definition xset_matches(t:Z->Σ)(xs:(list Σ)->Prop)(d:Dir)(len:nat):Prop :=
forall n:nat,
1<n ->
exists ls,
xs ls /\
ls = tape_seg t ((Z.of_nat n)*(Dir_to_Z d)%Z) d len.
Definition InAES(st:ExecState Σ)(S:AbstractES):Prop :=
let (s,t) := st in
let (ls,rs,ms) := S in
(exists mw, ms mw /\ MidWord_matches st mw) /\
xset_matches t ls Dneg len_l /\
xset_matches t rs Dpos len_r.
Definition AES_isClosed(tm:TM Σ)(S:AbstractES):Prop :=
forall st0,
InAES st0 S ->
exists st1,
step Σ tm st0 = Some st1 /\
InAES st1 S.

Lemma AES_Closed_NonHalt tm S st:
InAES st S ->
AES_isClosed tm S ->
~Halts _ tm st.