(***********************************************************************)
(**  * Connecting nominal and LN semantics *)
(***********************************************************************)
(** Our final goal is to show that the abstract nominal machine implements the same semantics as the LN substitution-based small step relation.  We'll do this by proving that any time the abstract machine takes a step, we can decode the machine configuration before and after the step to LN expressions, and those expressions are either identical or related by the LN step relation.  << machine_step (h,t,s)  ------>  (h',t',s')            nominal terms |                  | | decode           | decode v                  v e       - - ->     e'           locally nameless terms step e e' or e = e' >>  This result is the [simulate_step] lemma near the end of this file.  *)
(***********************************************************************)
(** ** Translating nominal terms to LN terms *)
(***********************************************************************)
(** We decode named terms to LN terms through the use of the [close_exp_wrt_exp] function. This function replaces all occurrences of a given atom by a new bound variable.  This function is defined in the [Stlc.Lemmas] module and can be automatically generated by LNgen.  *)
Fixpoint nom_to_exp (ne : n_exp) : exp :=
match ne with
| n_var x => var_f x
| n_app e1 e2 => app (nom_to_exp e1) (nom_to_exp e2)
| n_abs x e1 => abs (close_exp_wrt_exp x (nom_to_exp e1))
end.
(** We also define a translation from machine configurations to LN terms. In this case we must substitute all definitions in the heap through the terms and create all of the applications in the stack. *)
Fixpoint apply_heap (h : heap) (e : exp) : exp  :=
match h with
| nil => e
| (x , e') :: h' => apply_heap h' ([x ~> nom_to_exp e'] e)
end.
(* Note that the stack could have some heap definitions in it, so we apply the substitutions in the heap there too. *)
Fixpoint apply_stack h (s : list frame) (e :exp) : exp :=
match s with
| nil => e
| n_app2 e' :: s' => apply_stack h s' (app e (apply_heap h (nom_to_exp e')))
end.
(** The full decode function puts all of these parts together. *)
Definition decode (c:configuration) : exp  :=
match c with
| (h,e,s) => apply_stack h s (apply_heap h (nom_to_exp e))
end.
(** Here's an example translation from the machine step demo. *)
Definition conf1 := ([(Y,n_var Z)], n_abs X (n_var X), [n_app2 (n_var Y)]).
Example decode1 : decode conf1 = (app (abs (var_b 0)) (var_f Z)).
(***********************************************************************)
(** ** Connecting free variable functions.                             *)
(***********************************************************************)
(** Here is the first result about our decoding: that the two free variable functions agree.  In this part of the file, we will take advantage of automation provided by the Lemmas module. In particular, this module defines a database of rewriting hints (called [lngen]) that can be used to automatically rewrite LN terms into simpler form. *)

Lemma fv_nom_fv_exp_eq : forall n,
fv_nom n [=] fv_exp (nom_to_exp n).