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 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-04-04 18:46:09 CEST by Kim Nguyen>
26 (** Implementation of hashconsed Boolean formulae *)
33 include Hcons.Abstract with type t := t
34 include Common_sig.Printable with type t := t
37 type ('formula,'atom) expr =
40 | Or of 'formula * 'formula
41 | And of 'formula * 'formula
44 module Make (P: ATOM) =
49 pos : ('hcons,P.t) expr;
53 external hash_const_variant : [> ] -> int = "%identity"
54 external vb : bool -> int = "%identity"
56 module rec Node : Hcons.S
57 with type data = Data.t = Hcons.Make (Data)
58 and Data : Common_sig.HashedType with type t = Node.t node =
62 match x.pos, y.pos with
63 | a,b when a == b -> true
64 | Or(xf1, xf2), Or(yf1, yf2)
65 | And(xf1, xf2), And(yf1,yf2) -> xf1 == yf1 && xf2 == yf2
66 | Atom(p1), Atom(p2) -> p1 == p2
74 HASHINT3 (PRIME1, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
76 HASHINT3(PRIME3, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
77 | Atom(p) -> HASHINT2(PRIME5, Uid.to_int (P.uid p))
81 let hash x = x.Node.hash
83 let equal = Node.equal
84 let expr f = f.Node.node.pos
86 let compare f1 f2 = compare f1.Node.id f2.Node.id
94 let rec print ?(parent=false) ppf f =
95 if parent then fprintf ppf "(";
96 let _ = match expr f with
97 | True -> fprintf ppf "%s" Pretty.top
98 | False -> fprintf ppf "%s" Pretty.bottom
100 print ~parent:(prio f > prio f1) ppf f1;
101 fprintf ppf " %s " Pretty.wedge;
102 print ~parent:(prio f > prio f2) ppf f2;
105 fprintf ppf " %s " Pretty.vee;
107 | Atom(p) -> fprintf ppf "%a" P.print p
108 (* let _ = flush_str_formatter() in
109 let fmt = str_formatter in
112 | `Left -> Pretty.down_arrow, Pretty.subscript 1
113 | `Right -> Pretty.down_arrow, Pretty.subscript 2
114 | `Epsilon -> Pretty.epsilon, ""
115 | `Up1 -> Pretty.up_arrow, Pretty.subscript 1
116 | `Up2 -> Pretty.up_arrow, Pretty.subscript 2
118 fprintf fmt "%s%s" a_str d_str;
120 let str = flush_str_formatter() in
121 if b then fprintf ppf "%s" str
122 else Pretty.pp_overline ppf str *)
124 if parent then fprintf ppf ")"
126 let print ppf f = print ~parent:false ppf f
128 let is_true f = (expr f) == True
129 let is_false f = (expr f) == False
133 let nnode = Node.make { pos = neg; neg = Obj.magic 0 } in
134 let pnode = Node.make { pos = pos; neg = nnode } in
135 (Node.node nnode).neg <- pnode; (* works because the neg field isn't taken into
136 account for hashing ! *)
140 let true_,false_ = cons True False
142 let atom_ p = fst (cons (Atom(p)) (Atom(P.neg p)))
144 let not_ f = f.Node.node.neg
146 let order f1 f2 = if uid f1 < uid f2 then f2,f1 else f1,f2
149 (* Tautologies: x|x, x|not(x) *)
151 if equal f1 f2 then f1
152 else if equal f1 (not_ f2) then true_
155 else if is_true f1 || is_true f2 then true_
156 else if is_false f1 && is_false f2 then false_
157 else if is_false f1 then f2
158 else if is_false f2 then f1
160 (* commutativity of | *)
162 let f1, f2 = order f1 f2 in
163 fst (cons (Or(f1,f2)) (And(not_ f1, not_ f2)))
167 not_ (or_ (not_ f1) (not_ f2))
170 let of_bool = function true -> true_ | false -> false_
173 let rec loop phi acc =
175 | And (phi1, phi2) | Or(phi1, phi2) ->
176 loop phi2 (loop phi1 (f phi acc))