(* 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 BDDvar_le := Nleb.
Definition BDDvar_max (x y : BDDvar) := if BDDvar_le x y then y else x.
Definition bool_fun_if (x : BDDvar) (bf bf' : bool_fun) : bool_fun :=
fun vb : var_binding => ifb (vb x) (bf vb) (bf' vb).
(* Lemma BDD_EGAL_complete : (x,y:BDDvar) (BDDcompare x y)=EGAL -> x=y. *)
Definition BDDor_memo := Map (Map ad).
Definition initBDDor_memo := newMap (Map ad).
Definition BDDor_memo_put (memo : BDDor_memo) (node1 node2 node : ad) :=
let m1 :=
match MapGet _ memo node1 with
| Some y => y
| None => newMap ad
end in
let m1' := MapPut _ m1 node2 node in MapPut _ memo node1 m1'.
Definition BDDor_memo_lookup (memo : BDDor_memo) (node1 node2 : ad) :=
match MapGet _ memo node1 with
| None => None
| Some m1 =>
match MapGet _ m1 node2 with
| None => None
| Some node => Some node
end
end.
Definition BDDor_memo_OK (cfg : BDDconfig) (memo : BDDor_memo) :=
forall node1 node2 node : ad,
BDDor_memo_lookup memo node1 node2 = Some node ->
config_node_OK cfg node1 /\
config_node_OK cfg node2 /\
config_node_OK cfg node /\
BDDvar_le (var cfg node) (BDDvar_max (var cfg node1) (var cfg node2)) =
true /\
bool_fun_eq (bool_fun_of_BDD cfg node)
(bool_fun_or (bool_fun_of_BDD cfg node1) (bool_fun_of_BDD cfg node2)).

Lemma BDDor_memo_lookup_semantics :
forall (memo : BDDor_memo) (node1 node2 node node1' node2' : ad),
BDDor_memo_lookup (BDDor_memo_put memo node1 node2 node) node1' node2' =
(if N.eqb node1 node1' && N.eqb node2 node2'
then Some node
else BDDor_memo_lookup memo node1' node2').