(* 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))).

Lemma bool_fun_of_BDD_semantics :
forall cfg : BDDconfig,
BDDconfig_OK cfg ->
bool_fun_of_BDD cfg BDDzero = bool_fun_zero /\
bool_fun_of_BDD cfg BDDone = bool_fun_one /\
(forall node : ad,
is_internal_node cfg node ->
bool_fun_of_BDD cfg node =
(fun vb : var_binding =>
if vb (var cfg node)
then bool_fun_of_BDD cfg (high cfg node) vb
else bool_fun_of_BDD cfg (low cfg node) vb)).