Section FreeVariables.
Context {X: Const}.
Fixpoint vars (s: exp X) :=
match s with
| var x => [x]
| const x => nil
| lambda s => map pred (remove eq_dec 0 (vars s))
| app s1 s2 => vars s1 ++ vars s2
end.
Inductive varof (x: nat) : exp X -> Prop :=
| varofVar: varof x (var x)
| varofAppL (s t: exp X): varof x s -> varof x (s t)
| varofAppR (s t: exp X): varof x t -> varof x (s t)
| varofLambda s: varof (S x) s -> varof x (lambda s).
Hint Constructors varof : core.
Hint Resolve varof_vars vars_varof : core.
Global Instance dec_varof: Dec2 (varof).

Lemma vars_subst sigma x s:
x ∈ vars (sigma • s) -> exists y, y ∈ vars s /\ x ∈ vars (sigma y).