Load loadpath.
Fixpoint freshmax_expr (e: expr) : var :=
match e with
| Nil => minid
| Var v => v
end.
Definition var_max (x y : var) : var :=
match Ident.compare x y with Lt => y | _ => x end.
Definition freshmax_pn_atom (a: pn_atom) : var :=
match a with
| Equ e1 e2 => var_max (freshmax_expr e1) (freshmax_expr e2)
| Nequ e1 e2 => var_max (freshmax_expr e1) (freshmax_expr e2)
end.
Definition freshmax_space_atom (a: space_atom) : var :=
match a with
| Next e1 e2 =>  var_max (freshmax_expr e1) (freshmax_expr e2)
| Lseg e1 e2 =>  var_max (freshmax_expr e1) (freshmax_expr e2)
end.
Definition freshmax_list {A} (f: A -> var) (l: list A) : var :=
fold_right (fun a i => var_max (f a) i) minid l.
Definition freshmax_assertion (a: assertion) : var :=
match a with
| Assertion pi sigma =>
var_max (freshmax_list freshmax_pn_atom pi)
(freshmax_list freshmax_space_atom sigma)
end.
Definition freshmax_pure_atom (a: pure_atom) : var :=
match a with
| Eqv e1 e2 => var_max (freshmax_expr e1) (freshmax_expr e2)
end.
Definition freshmax_clause (c : clause) : var :=
match c with
| PureClause pi pi' _ _ =>  var_max (freshmax_list freshmax_pure_atom pi)
(freshmax_list freshmax_pure_atom pi')
| PosSpaceClause pi pi' sigma =>
var_max (var_max (freshmax_list freshmax_pure_atom pi)
(freshmax_list freshmax_pure_atom pi'))
(freshmax_list freshmax_space_atom sigma)
| NegSpaceClause pi sigma pi' =>
var_max (var_max (freshmax_list freshmax_pure_atom pi)
(freshmax_list freshmax_pure_atom pi'))
(freshmax_list freshmax_space_atom sigma)
end.

Lemma freshmax_list_rev:
forall {A} (f: A -> var) l,
freshmax_list f (rev l) = freshmax_list f l.