Section Semantics.
Implicit Types H : Heap.
Implicit Types T V : list HClos.
Implicit Types n m : nat.
Implicit Types a b c : HAdd.
Implicit Types g : HClos.
Implicit Types P Q : Pro.
Definition put H e : HAdd * Heap := (|H|, H++[e]).
Definition tailRecursion : HClos -> list HClos -> list HClos :=
fun '(a, P) T =>
match P with
| [] => T
| _ => (a, P) :: T
end.
Definition state : Type := list HClos * list HClos * Heap.
Fixpoint jumpTarget (k:nat) (Q:Pro) (P:Pro) : option (Pro*Pro) :=
match P with
| retT :: P' => match k with
| 0 => Some (Q,P')
| S k' => jumpTarget k' (Q++[retT]) P'
end
| lamT :: P' => jumpTarget (S k) (Q++[lamT]) P'
| t :: P' => jumpTarget k (Q++[t]) P' (* either [varT n] or [appT] *)
| [] => None
end.
Fixpoint lookup H a n : option HClos :=
match nth_error H a with
| Some (Some (g, b)) =>
match n with
| 0 => Some g
| S n' => lookup H b n'
end
| _ => None
end.

Lemma lookup_eq H a n :
lookup H a n =
match nth_error H a with
| Some (Some (g, b)) =>
match n with
| 0 => Some g
| S n' => lookup H b n'
end
| _ => None
end.