Merge branch 'lucca-tests-bench' into lucca-extentions
[tatoo.git] / src / formula.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2012 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   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15 INCLUDE "utils.ml"
16
17 open Format
18 type move = [ `Left | `Right | `Self ]
19 type 'hcons expr =
20   | False | True
21   | Or of 'hcons * 'hcons
22   | And of 'hcons * 'hcons
23   | Atom of (move * bool * State.t)
24
25 type 'hcons node = {
26   pos : 'hcons expr;
27   mutable neg : 'hcons;
28   st : StateSet.t * StateSet.t * StateSet.t;
29   size: int; (* Todo check if this is needed *)
30 }
31
32 external hash_const_variant : [> ] -> int = "%identity"
33 external vb : bool -> int = "%identity"
34
35 module rec Node : Hcons.S
36   with type data = Data.t = Hcons.Make (Data)
37   and Data : Hashtbl.HashedType  with type t = Node.t node =
38   struct
39     type t =  Node.t node
40     let equal x y = x.size == y.size &&
41       match x.pos, y.pos with
42       | a,b when a == b -> true
43       | Or(xf1, xf2), Or(yf1, yf2)
44       | And(xf1, xf2), And(yf1,yf2)  -> (xf1 == yf1) && (xf2 == yf2)
45       | Atom(d1, p1, s1), Atom(d2 ,p2 ,s2) -> d1 == d2 && p1 == p2 && s1 == s2
46       | _ -> false
47
48     let hash f =
49       match f.pos with
50       | False -> 0
51       | True -> 1
52       | Or (f1, f2) ->
53         HASHINT3 (PRIME1, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
54       | And (f1, f2) ->
55         HASHINT3(PRIME3, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
56
57       | Atom(d, p, s) -> HASHINT4(PRIME5, hash_const_variant d,vb p,s)
58   end
59
60 type t = Node.t
61 let hash x = x.Node.key
62 let uid x = x.Node.id
63 let equal = Node.equal
64 let expr f = f.Node.node.pos
65 let st f = f.Node.node.st
66 let size f = f.Node.node.size
67 let compare f1 f2 = compare f1.Node.id  f2.Node.id
68 let prio f =
69   match expr f with
70     | True | False -> 10
71     | Atom _ -> 8
72     | And _ -> 6
73     | Or _ -> 1
74       
75 (* Begin Lucca Hirschi *)
76 let rec eval_form (q,qf,qn) f = match expr f with
77   | False -> false
78   | True -> true
79   | And(f1,f2) -> eval_form (q,qf,qn) f1 && eval_form (q,qf,qn) f2
80   | Or(f1,f2) -> eval_form (q,qf,qn) f1 || eval_form (q,qf,qn) f2
81   | Atom(dir, b, s) -> 
82     let set = match dir with
83       |`Left -> qf | `Right -> qn | `Self -> q in
84     if b then StateSet.mem s set
85     else not (StateSet.mem s set)
86
87 let rec infer_form sq sqf sqn f = match expr f with
88   | False -> false
89   | True -> true
90   | And(f1,f2) -> infer_form sq sqf sqn f1 && infer_form sq sqf sqn f2
91   | Or(f1,f2) -> infer_form sq sqf sqn f1 || infer_form sq sqf sqn f2
92   | Atom(dir, b, s) -> 
93     let setq, setr = match dir with
94       | `Left -> sqf | `Right -> sqn | `Self -> sq in
95     (* WG: WE SUPPOSE THAT Q^r and Q^q are disjoint ! *)
96     let mem =  StateSet.mem s setq || StateSet.mem s setr in
97     if b then mem else not mem
98 (* End *)
99       
100 let rec print ?(parent=false) ppf f =
101   if parent then fprintf ppf "(";
102   let _ = match expr f with
103     | True -> fprintf ppf "%s" Pretty.top
104     | False -> fprintf ppf "%s" Pretty.bottom
105     | And(f1,f2) ->
106       print ~parent:(prio f > prio f1) ppf f1;
107       fprintf ppf " %s "  Pretty.wedge;
108       print ~parent:(prio f > prio f2) ppf f2;
109     | Or(f1,f2) ->
110       (print ppf f1);
111       fprintf ppf " %s " Pretty.vee;
112       (print ppf f2);
113     | Atom(dir, b, s) ->
114       let _ = flush_str_formatter() in
115       let fmt = str_formatter in
116         let a_str, d_str =
117           match  dir with
118           | `Left ->  Pretty.down_arrow, Pretty.subscript 1
119           | `Right -> Pretty.down_arrow, Pretty.subscript 2
120           | `Self -> Pretty.down_arrow, Pretty.subscript 0
121         in
122         fprintf fmt "%s%s" a_str d_str;
123         State.print fmt s;
124         let str = flush_str_formatter() in
125         if b then fprintf ppf "%s" str
126         else Pretty.pp_overline ppf str
127   in
128     if parent then fprintf ppf ")"
129
130 let print ppf f =  print ~parent:false ppf f
131
132 let is_true f = (expr f) == True
133 let is_false f = (expr f) == False
134
135
136 let cons pos neg s1 s2 size1 size2 =
137   let nnode = Node.make { pos = neg; neg = (Obj.magic 0); st = s2; size = size2 } in
138   let pnode = Node.make { pos = pos; neg = nnode ; st = s1; size = size1 } in
139     (Node.node nnode).neg <- pnode; (* works because the neg field isn't taken into
140                                        account for hashing ! *)
141     pnode,nnode
142
143
144 let empty_triple = StateSet.empty, StateSet.empty, StateSet.empty
145 let true_,false_ = cons True False empty_triple empty_triple 0 0
146 let atom_ d p s =
147   let si = StateSet.singleton s in
148   let ss = match d with
149     | `Left -> StateSet.empty, si, StateSet.empty
150     | `Right -> StateSet.empty, StateSet.empty, si
151     | `Self -> si, StateSet.empty, StateSet.empty
152   in fst (cons (Atom(d,p,s)) (Atom(d,not p,s)) ss ss 1 1)
153
154 let not_ f = f.Node.node.neg
155
156 let union_triple (s1,l1,r1) (s2,l2, r2) =
157   StateSet.union s1 s2,
158   StateSet.union l1 l2,
159   StateSet.union r1 r2
160
161 let merge_states f1 f2 =
162   let sp =
163     union_triple (st f1) (st f2)
164   and sn =
165     union_triple (st (not_ f1)) (st (not_ f2))
166   in
167     sp,sn
168
169 let order f1 f2 = if uid f1  < uid f2 then f2,f1 else f1,f2
170
171 let or_ f1 f2 =
172   (* Tautologies: x|x, x|not(x) *)
173
174   if equal f1 f2 then f1
175   else if equal f1 (not_ f2) then true_
176
177   (* simplification *)
178   else if is_true f1 || is_true f2 then true_
179   else if is_false f1 && is_false f2 then false_
180   else if is_false f1 then f2
181   else if is_false f2 then f1
182
183   (* commutativity of | *)
184   else
185     let f1, f2 = order f1 f2 in
186     let psize = (size f1) + (size f2) in
187     let nsize = (size (not_ f1)) + (size (not_ f2)) in
188     let sp, sn = merge_states f1 f2 in
189       fst (cons (Or(f1,f2)) (And(not_ f1, not_ f2)) sp sn psize nsize)
190
191
192 let and_ f1 f2 =
193   not_ (or_ (not_ f1) (not_ f2))
194
195
196 let of_bool = function true -> true_ | false -> false_
197
198
199 module Infix = struct
200   let ( +| ) f1 f2 = or_ f1 f2
201
202   let ( *& ) f1 f2 = and_ f1 f2
203
204   let ( *+ ) d s = atom_ d true s
205   let ( *- ) d s = atom_ d false s
206 end