(* Well-formedness of a program *)
Fixpoint vars_of_term (t : term) : list variable :=
match t with
| var x => [x]
| capply _ lt => flat_map vars_of_term lt
| fapply _ lt => flat_map vars_of_term lt
end.
Fixpoint vars_of_pattern (p : pattern) : list variable :=
match p with
| p_var x => [x]
| p_capply _ lp => flat_map vars_of_pattern lp
end.

Lemma vars_of_pattern_term p : vars_of_pattern p = vars_of_term (term_from_pattern p).
Fixpoint functions_of_term (t:term) : list function :=
match t with
| var _       => []
| capply _ lt => flat_map functions_of_term lt
| fapply f lt => f :: flat_map functions_of_term lt
end.