4c58e3cc531dc501f811cd93357837711996ed08
[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 | `Epsilon | `Up1 | `Up2 ]
19 type 'formula expr =
20   | False
21   | True
22   | Or of 'formula * 'formula
23   | And of 'formula * 'formula
24   | Move of (move * bool * State.t)
25   | Label of QNameSet.t
26
27 type 'hcons node = {
28   pos : 'hcons expr;
29   mutable neg : 'hcons;
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       | Move(d1, p1, s1), Move(d2 ,p2 ,s2) -> d1 == d2 && p1 == p2 && s1 == s2
46       | Label s1, Label s2 -> s1 == s2
47       | _ -> false
48
49     let hash f =
50       match f.pos with
51       | False -> 0
52       | True -> 1
53       | Or (f1, f2) ->
54         HASHINT3 (PRIME1, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
55       | And (f1, f2) ->
56         HASHINT3(PRIME3, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
57
58       | Move(d, p, s) -> HASHINT4(PRIME5, hash_const_variant d,vb p,s)
59       | Label s -> HASHINT2(PRIME7, Uid.to_int s.QNameSet.id)
60   end
61
62 type t = Node.t
63 let hash x = x.Node.hash
64 let uid x = x.Node.id
65 let equal = Node.equal
66 let expr f = f.Node.node.pos
67 (*let st f = f.Node.node.st*)
68 (*let size f = f.Node.node.size*)
69 let compare f1 f2 = compare f1.Node.id  f2.Node.id
70 let prio f =
71   match expr f with
72     | True | False -> 10
73     | Move _ -> 8
74     | Label _ -> 7
75     | And _ -> 6
76     | Or _ -> 1
77
78 let rec print ?(parent=false) ppf f =
79   if parent then fprintf ppf "(";
80   let _ = match expr f with
81     | True -> fprintf ppf "%s" Pretty.top
82     | False -> fprintf ppf "%s" Pretty.bottom
83     | And(f1,f2) ->
84       print ~parent:(prio f > prio f1) ppf f1;
85       fprintf ppf " %s "  Pretty.wedge;
86       print ~parent:(prio f > prio f2) ppf f2;
87     | Or(f1,f2) ->
88       (print ppf f1);
89       fprintf ppf " %s " Pretty.vee;
90       (print ppf f2);
91     | Label s -> fprintf ppf "%a" QNameSet.print s
92     | Move(dir, b, s) ->
93       let _ = flush_str_formatter() in
94       let fmt = str_formatter in
95         let a_str, d_str =
96           match  dir with
97           | `Left ->  Pretty.down_arrow, Pretty.subscript 1
98           | `Right -> Pretty.down_arrow, Pretty.subscript 2
99           | `Epsilon -> Pretty.epsilon, ""
100           | `Up1 -> Pretty.up_arrow, Pretty.subscript 1
101           | `Up2 -> Pretty.up_arrow, Pretty.subscript 2
102         in
103         fprintf fmt "%s%s" a_str d_str;
104         State.print fmt s;
105         let str = flush_str_formatter() in
106         if b then fprintf ppf "%s" str
107         else Pretty.pp_overline ppf str
108   in
109     if parent then fprintf ppf ")"
110
111 let print ppf f =  print ~parent:false ppf f
112
113 let is_true f = (expr f) == True
114 let is_false f = (expr f) == False
115
116
117 let cons pos neg =
118   let nnode = Node.make { pos = neg; neg = (Obj.magic 0); } in
119   let pnode = Node.make { pos = pos; neg = nnode } in
120     (Node.node nnode).neg <- pnode; (* works because the neg field isn't taken into
121                                        account for hashing ! *)
122     pnode,nnode
123
124
125 let true_,false_ = cons True False
126 let atom_ d p s =
127   fst (cons (Move(d,p,s)) (Move(d,not p,s)))
128
129 let not_ f = f.Node.node.neg
130
131
132 let order f1 f2 = if uid f1  < uid f2 then f2,f1 else f1,f2
133
134 let or_ f1 f2 =
135   (* Tautologies: x|x, x|not(x) *)
136
137   if equal f1 f2 then f1
138   else if equal f1 (not_ f2) then true_
139
140   (* simplification *)
141   else if is_true f1 || is_true f2 then true_
142   else if is_false f1 && is_false f2 then false_
143   else if is_false f1 then f2
144   else if is_false f2 then f1
145
146   (* commutativity of | *)
147   else
148     let f1, f2 = order f1 f2 in
149       fst (cons (Or(f1,f2)) (And(not_ f1, not_ f2)))
150
151
152 let and_ f1 f2 =
153   not_ (or_ (not_ f1) (not_ f2))
154
155
156 let of_bool = function true -> true_ | false -> false_
157
158
159 module Infix = struct
160   let ( +| ) f1 f2 = or_ f1 f2
161
162   let ( *& ) f1 f2 = and_ f1 f2
163
164   let ( *+ ) d s = atom_ d true s
165   let ( *- ) d s = atom_ d false s
166 end