(* 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 BDDimpl (cfg : BDDconfig) (negm : BDDneg_memo)
(orm : BDDor_memo) (node1 node2 : ad) :=
match BDDneg cfg negm node1 with
| (cfg', (node1', negm')) =>
match BDDor cfg' orm node1' node2 with
| (cfg'', (node, orm')) => (cfg'', (node, (negm', orm')))
end
end.
Definition bool_fun_impl (bf1 bf2 : bool_fun) : bool_fun :=
fun vb : var_binding => implb (bf1 vb) (bf2 vb).
Definition BDDiff (cfg : BDDconfig) (negm : BDDneg_memo)
(orm : BDDor_memo) (node1 node2 : ad) :=
match BDDimpl cfg negm orm node1 node2 with
| (cfg', (node1', (negm', orm'))) =>
match BDDimpl cfg' negm' orm' node2 node1 with
| (cfg'', (node2', (negm'', orm''))) =>
BDDand cfg'' negm'' orm'' node1' node2'
end
end.
Definition bool_fun_iff (bf1 bf2 : bool_fun) : bool_fun :=
fun vb : var_binding => eqb (bf1 vb) (bf2 vb).
Definition BDDvar_make (cfg : BDDconfig) (x : BDDvar) :=
BDDmake cfg x BDDzero BDDone.
Definition bool_fun_var (x : BDDvar) : bool_fun :=
fun vb : var_binding => vb x.
Inductive bool_expr : Set :=
| Zero : bool_expr
| One : bool_expr
| Var : BDDvar -> bool_expr
| Neg : bool_expr -> bool_expr
| Or : bool_expr -> bool_expr -> bool_expr
| ANd : bool_expr -> bool_expr -> bool_expr
| Impl : bool_expr -> bool_expr -> bool_expr
| Iff : bool_expr -> bool_expr -> bool_expr.
Fixpoint bool_fun_of_bool_expr (be : bool_expr) : bool_fun :=
match be with
| Zero => bool_fun_zero
| One => bool_fun_one
| Var x => bool_fun_var x
| Neg be' => bool_fun_neg (bool_fun_of_bool_expr be')
| Or be1 be2 =>
bool_fun_or (bool_fun_of_bool_expr be1) (bool_fun_of_bool_expr be2)
| ANd be1 be2 =>
bool_fun_and (bool_fun_of_bool_expr be1) (bool_fun_of_bool_expr be2)
| Impl be1 be2 =>
bool_fun_impl (bool_fun_of_bool_expr be1) (bool_fun_of_bool_expr be2)
| Iff be1 be2 =>
bool_fun_iff (bool_fun_of_bool_expr be1) (bool_fun_of_bool_expr be2)
end.
Fixpoint BDDof_bool_expr (cfg : BDDconfig) (negm : BDDneg_memo)
(orm : BDDor_memo) (be : bool_expr) {struct be} :
BDDconfig * (ad * (BDDneg_memo * BDDor_memo)) :=
match be with
| Zero => (cfg, (BDDzero, (negm, orm)))
| One => (cfg, (BDDone, (negm, orm)))
| Var x =>
match BDDvar_make cfg x with
| (cfg', node) => (cfg', (node, (negm, orm)))
end
| Neg be' =>
match BDDof_bool_expr cfg negm orm be' with
| (cfg', (node, (negm', orm'))) =>
match BDDneg cfg' negm' node with
| (cfg'', (node', negm'')) => (cfg'', (node', (negm'', orm')))
end
end
| Or be1 be2 =>
match BDDof_bool_expr cfg negm orm be1 with
| (cfg', (node1, (negm', orm'))) =>
match BDDof_bool_expr cfg' negm' orm' be2 with
| (cfg'', (node2, (negm'', orm''))) =>
match BDDor cfg'' orm'' node1 node2 with
| (cfg''', (node, orm''')) =>
(cfg''', (node, (negm'', orm''')))
end
end
end
| ANd be1 be2 =>
match BDDof_bool_expr cfg negm orm be1 with
| (cfg', (node1, (negm', orm'))) =>
match BDDof_bool_expr cfg' negm' orm' be2 with
| (cfg'', (node2, (negm'', orm''))) =>
BDDand cfg'' negm'' orm'' node1 node2
end
end
| Impl be1 be2 =>
match BDDof_bool_expr cfg negm orm be1 with
| (cfg', (node1, (negm', orm'))) =>
match BDDof_bool_expr cfg' negm' orm' be2 with
| (cfg'', (node2, (negm'', orm''))) =>
BDDimpl cfg'' negm'' orm'' node1 node2
end
end
| Iff be1 be2 =>
match BDDof_bool_expr cfg negm orm be1 with
| (cfg', (node1, (negm', orm'))) =>
match BDDof_bool_expr cfg' negm' orm' be2 with
| (cfg'', (node2, (negm'', orm''))) =>
BDDiff cfg'' negm'' orm'' node1 node2
end
end
end.

Lemma BDDof_bool_expr_correct :
forall (be : bool_expr) (cfg : BDDconfig) (negm : BDDneg_memo)
(orm : BDDor_memo),
BDDconfig_OK cfg ->
BDDneg_memo_OK_2 cfg negm ->
BDDor_memo_OK cfg orm ->
BDDconfig_OK (fst (BDDof_bool_expr cfg negm orm be)) /\
config_node_OK (fst (BDDof_bool_expr cfg negm orm be))
(fst (snd (BDDof_bool_expr cfg negm orm be))) /\
BDDneg_memo_OK_2 (fst (BDDof_bool_expr cfg negm orm be))
(fst (snd (snd (BDDof_bool_expr cfg negm orm be)))) /\
BDDor_memo_OK (fst (BDDof_bool_expr cfg negm orm be))
(snd (snd (snd (BDDof_bool_expr cfg negm orm be)))) /\
nodes_preserved cfg (fst (BDDof_bool_expr cfg negm orm be)) /\
bool_fun_eq
(bool_fun_of_BDD (fst (BDDof_bool_expr cfg negm orm be))
(fst (snd (BDDof_bool_expr cfg negm orm be))))
(bool_fun_of_bool_expr be).