Section Free_Variables.
Fixpoint freeVarTerm (s : fol.Term L) : list nat :=
match s with
| fol.var v => v :: nil
| fol.apply f ts => freeVarTerms (arity L (inr _ f)) ts
end
with freeVarTerms (n : nat) (ss : fol.Terms L n) {struct ss} :
list nat :=
match ss with
| Tnil => nil (A:=nat)
| Tcons m t ts => freeVarTerm t ++ freeVarTerms m ts
end.
Fixpoint freeVarFormula (A : fol.Formula L) : list nat :=
match A with
| fol.equal t s => freeVarTerm t ++ freeVarTerm s
| fol.atomic r ts => freeVarTerms _ ts
| fol.impH A B => freeVarFormula A ++ freeVarFormula B
| fol.notH A => freeVarFormula A
| fol.forallH v A => list_remove _ eq_nat_dec v (freeVarFormula A)
end.
Definition ClosedSystem (T : fol.System L) :=
forall (v : nat) (f : fol.Formula L),
mem _ T f -> ~ In v (freeVarFormula f).
Definition closeList (l : list nat) (x : fol.Formula L) :
fol.Formula L :=
list_rec (fun _ => fol.Formula L) x
(fun (a : nat) _ (rec : fol.Formula L) => forallH a rec) l.
Definition close (x : fol.Formula L) : fol.Formula L :=
closeList (no_dup _ eq_nat_dec (freeVarFormula x)) x.

Lemma freeVarClosedList1 :
forall (l : list nat) (v : nat) (x : fol.Formula L),
In v l -> ~ In v (freeVarFormula (closeList l x)).