(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)
Definition var (cfg : BDDconfig) (node : ad) :=
match MapGet _ (fst cfg) node with
| None => (* Error *)  BDDzero
| Some (x, (l, r)) => x
end.
Definition low (cfg : BDDconfig) (node : ad) :=
match MapGet _ (fst cfg) node with
| None => (* Error *)  N0
| Some (x, (l, r)) => l
end.
Definition high (cfg : BDDconfig) (node : ad) :=
match MapGet _ (fst cfg) node with
| None => (* Error *)  N0
| Some (x, (l, r)) => r
end.
Definition config_node_OK (cfg : BDDconfig) := node_OK (fst cfg).
Definition is_internal_node (cfg : BDDconfig) (node : ad) :=
exists x : BDDvar,
(exists l : ad,
(exists r : ad, MapGet _ (fst cfg) node = Some (x, (l, r)))).
(* An interpretation of BDDvar's *)
Definition var_binding := BDDvar -> bool.
(* A boolean function. However not all boolean functions represent a logical formula. *)
Definition bool_fun := var_binding -> bool.
(* Evaluation of a boolean function given a binding of variables *)
Definition bool_fun_eval (bf : bool_fun) (vb : var_binding) := bf vb.
(* The boolean functions representing ZERO and ONE *)
Definition bool_fun_zero (vb : var_binding) := false.
Definition bool_fun_one (vb : var_binding) := true.
Fixpoint bool_fun_of_BDD_1 (cfg : BDDconfig) (node : ad)
(bound : nat) {struct bound} : bool_fun :=
match MapGet _ (fst cfg) node with
| None => if N.eqb node BDDzero then bool_fun_zero else bool_fun_one
| Some (x, (l, r)) =>
match bound with
| O => (* Error *)  bool_fun_zero
| S bound' =>
let bfl := bool_fun_of_BDD_1 cfg l bound' in
let bfr := bool_fun_of_BDD_1 cfg r bound' in
fun vb : var_binding => if vb x then bfr vb else bfl vb
end
end.
Definition bool_fun_of_BDD (cfg : BDDconfig) (node : ad) :=
bool_fun_of_BDD_1 cfg node (S (nat_of_N (var cfg node))).
Definition bool_fun_eq (bf1 bf2 : bool_fun) :=
forall vb : var_binding, bool_fun_eval bf1 vb = bool_fun_eval bf2 vb.

Lemma bool_fun_eq_trans :
forall bf1 bf2 bf3 : bool_fun,
bool_fun_eq bf1 bf2 -> bool_fun_eq bf2 bf3 -> bool_fun_eq bf1 bf3.