1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2013 Université Paris-Sud and Centre National de la *)
9 (* Recherche Scientifique. All rights reserved. This file is *)
10 (* distributed under the terms of the GNU Lesser General Public *)
11 (* License, with the special exception on linking described in file *)
14 (***********************************************************************)
22 (** Implementation of hashconsed Boolean formulae *)
29 include Hcons.Abstract with type t := t
30 include Common_sig.Printable with type t := t
33 type ('formula,'atom) expr =
36 | Or of 'formula * 'formula
37 | And of 'formula * 'formula
40 module Make (P: ATOM) =
45 pos : ('hcons,P.t) expr;
49 external hash_const_variant : [> ] -> int = "%identity"
50 external vb : bool -> int = "%identity"
52 module rec Node : Hcons.S
53 with type data = Data.t = Hcons.Make (Data)
54 and Data : Common_sig.HashedType with type t = Node.t node =
58 match x.pos, y.pos with
59 | a,b when a == b -> true
60 | Or(xf1, xf2), Or(yf1, yf2)
61 | And(xf1, xf2), And(yf1,yf2) -> xf1 == yf1 && xf2 == yf2
62 | Atom(p1), Atom(p2) -> p1 == p2
70 HASHINT3 (PRIME1, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
72 HASHINT3(PRIME3, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
73 | Atom(p) -> HASHINT2(PRIME5, Uid.to_int (P.uid p))
77 let hash x = x.Node.hash
79 let equal = Node.equal
80 let expr f = f.Node.node.pos
82 let compare f1 f2 = compare f1.Node.id f2.Node.id
90 let rec print ?(parent=false) ppf f =
91 if parent then fprintf ppf "(";
92 let _ = match expr f with
93 | True -> fprintf ppf "%s" Pretty.top
94 | False -> fprintf ppf "%s" Pretty.bottom
96 print ~parent:(prio f > prio f1) ppf f1;
97 fprintf ppf " %s " Pretty.wedge;
98 print ~parent:(prio f > prio f2) ppf f2;
101 fprintf ppf " %s " Pretty.vee;
103 | Atom(p) -> fprintf ppf "%a" P.print p
104 (* let _ = flush_str_formatter() in
105 let fmt = str_formatter in
108 | `Left -> Pretty.down_arrow, Pretty.subscript 1
109 | `Right -> Pretty.down_arrow, Pretty.subscript 2
110 | `Epsilon -> Pretty.epsilon, ""
111 | `Up1 -> Pretty.up_arrow, Pretty.subscript 1
112 | `Up2 -> Pretty.up_arrow, Pretty.subscript 2
114 fprintf fmt "%s%s" a_str d_str;
116 let str = flush_str_formatter() in
117 if b then fprintf ppf "%s" str
118 else Pretty.pp_overline ppf str *)
120 if parent then fprintf ppf ")"
122 let print ppf f = print ~parent:false ppf f
124 let is_true f = (expr f) == True
125 let is_false f = (expr f) == False
129 let nnode = Node.make { pos = neg; neg = Obj.magic 0 } in
130 let pnode = Node.make { pos = pos; neg = nnode } in
131 (Node.node nnode).neg <- pnode; (* works because the neg field isn't taken into
132 account for hashing ! *)
136 let true_,false_ = cons True False
138 let atom_ p = fst (cons (Atom(p)) (Atom(P.neg p)))
140 let not_ f = f.Node.node.neg
142 let order f1 f2 = if uid f1 < uid f2 then f2,f1 else f1,f2
145 (* Tautologies: x|x, x|not(x) *)
147 if equal f1 f2 then f1
148 else if equal f1 (not_ f2) then true_
151 else if is_true f1 || is_true f2 then true_
152 else if is_false f1 && is_false f2 then false_
153 else if is_false f1 then f2
154 else if is_false f2 then f1
156 (* commutativity of | *)
158 let f1, f2 = order f1 f2 in
159 fst (cons (Or(f1,f2)) (And(not_ f1, not_ f2)))
163 not_ (or_ (not_ f1) (not_ f2))
166 let of_bool = function true -> true_ | false -> false_
169 let rec loop phi acc =
171 | And (phi1, phi2) | Or(phi1, phi2) ->
172 loop phi2 (loop phi1 (f phi acc))