Section fixH.
Variable H : Heap.
(* We define a function to unfold a closure, needed for the Turing machine M_unf. *)
(* function that unfolds if unfoldable *)
Definition unfoldTailRecStep '(stack,res) : (list (HClos*nat) * Pro) + option Pro :=
match stack with
| ((a,P),k)::stack =>
match P with
c::P =>
match c with
varT n =>
if ( k <=? n)
then
match lookup H a (n-k) with
| Some (b,Q) =>
inl (((b,Q),1)::(a,retT::P,S k)::stack,lamT::res)
| None => inr None
end
else
inl ((a,P,k)::stack,varT n::res)
| appT => inl ((a,P,k)::stack,appT::res)
| lamT => inl ((a,P,S k)::stack,lamT::res)
| retT => inl ((a,P,pred k)::stack,retT::res)
end
| [] => inl (stack,res)
end
| [] => inr (Some res)
end.

Lemma unfoldTailRecStep_complete a k s s' n:
unfolds H a k s s' ->
L_facts.size s' * 3 + 2 <= n ->
loopSum n unfoldTailRecStep ([(a,(compile s),k)],[])
= Some (Some (rev (compile s'))).