(* 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 BDDneg_memo_OK_1 (cfg : BDDconfig) (memo : BDDneg_memo) :=
forall (node node' : ad) (bound : nat),
config_node_OK cfg node ->
BDDneg_memo_lookup memo node = Some node' ->
(is_internal_node cfg node -> nat_of_N (var cfg node) < bound) ->
BDDneg_2 cfg node bound = (cfg, node').
Definition BDDneg_memo_OK_2 (cfg : BDDconfig) (memo : BDDneg_memo) :=
forall (node node' : ad) (bound : nat),
BDDneg_memo_lookup memo node = Some node' ->
(is_internal_node cfg node -> nat_of_N (var cfg node) < bound) ->
config_node_OK cfg node /\ BDDneg_2 cfg node bound = (cfg, node').
Fixpoint BDDneg_1 (arg : BDDconfig * ad * BDDneg_memo)
(bound : nat) {struct bound} : BDDconfig * ad * BDDneg_memo :=
match BDDneg_memo_lookup (snd arg) (snd (fst arg)) with
| Some node => (fst (fst arg), node, snd arg)
| None =>
match MapGet _ (fst (fst (fst arg))) (snd (fst arg)) with
| None =>
if N.eqb (snd (fst arg)) BDDzero
then
(fst (fst arg), BDDone, BDDneg_memo_put (snd arg) BDDzero BDDone)
else
(fst (fst arg), BDDzero, BDDneg_memo_put (snd arg) BDDone BDDzero)
| Some (x, (l, r)) =>
match bound with
| O => (initBDDconfig, BDDzero, newMap ad)
| S bound' =>
(BDDmake
(fst
(fst
(BDDneg_1
(fst
(fst
(BDDneg_1 (fst (fst arg), l, snd arg) bound')),
r,
snd (BDDneg_1 (fst (fst arg), l, snd arg) bound'))
bound'))) x
(snd (fst (BDDneg_1 (fst (fst arg), l, snd arg) bound')))
(snd
(fst
(BDDneg_1
(fst
(fst
(BDDneg_1 (fst (fst arg), l, snd arg) bound')),
r,
snd (BDDneg_1 (fst (fst arg), l, snd arg) bound'))
bound'))),
BDDneg_memo_put
(snd
(BDDneg_1
(fst
(fst (BDDneg_1 (fst (fst arg), l, snd arg) bound')),
r, snd (BDDneg_1 (fst (fst arg), l, snd arg) bound'))
bound')) (snd (fst arg))
(snd
(BDDmake
(fst
(fst
(BDDneg_1
(fst
(fst
(BDDneg_1 (fst (fst arg), l, snd arg)
bound')), r,
snd
(BDDneg_1 (fst (fst arg), l, snd arg) bound'))
bound'))) x
(snd
(fst (BDDneg_1 (fst (fst arg), l, snd arg) bound')))
(snd
(fst
(BDDneg_1
(fst
(fst
(BDDneg_1 (fst (fst arg), l, snd arg)
bound')), r,
snd
(BDDneg_1 (fst (fst arg), l, snd arg) bound'))
bound'))))))
end
end
end.
Definition nodes_preserved (cfg cfg' : BDDconfig) :=
forall (x : BDDvar) (l r node : ad),
MapGet _ (fst cfg) node = Some (x, (l, r)) ->
MapGet _ (fst cfg') node = Some (x, (l, r)).

Lemma BDDneg_2_config_OK_lemma_2 :
forall (cfg : BDDconfig) (node : ad) (x : BDDvar) (l r : ad) (n m : nat),
BDDconfig_OK cfg ->
MapGet _ (fst cfg) node = Some (x, (l, r)) ->
nat_of_N (var cfg node) < n ->
n = S m -> BDDconfig_OK (fst (BDDneg_2 (fst (BDDneg_2 cfg l m)) r m)).