Remove AbsoluteDOS from the XPath AST.
[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
26 type 'hcons node = {
27   pos : 'hcons expr;
28   mutable neg : 'hcons;
29 }
30
31 external hash_const_variant : [> ] -> int = "%identity"
32 external vb : bool -> int = "%identity"
33
34 module rec Node : Hcons.S
35   with type data = Data.t = Hcons.Make (Data)
36   and Data : Hashtbl.HashedType  with type t = Node.t node =
37   struct
38     type t =  Node.t node
39     let equal x y = (*x.size == y.size &&*)
40       match x.pos, y.pos with
41       | a,b when a == b -> true
42       | Or(xf1, xf2), Or(yf1, yf2)
43       | And(xf1, xf2), And(yf1,yf2)  -> (xf1 == yf1) && (xf2 == yf2)
44       | Move(d1, p1, s1), Move(d2 ,p2 ,s2) -> d1 == d2 && p1 == p2 && s1 == s2
45       | _ -> false
46
47     let hash f =
48       match f.pos with
49       | False -> 0
50       | True -> 1
51       | Or (f1, f2) ->
52         HASHINT3 (PRIME1, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
53       | And (f1, f2) ->
54         HASHINT3(PRIME3, Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
55
56       | Move(d, p, s) -> HASHINT4(PRIME5, hash_const_variant d,vb p,s)
57   end
58
59 type t = Node.t
60 let hash x = x.Node.hash
61 let uid x = x.Node.id
62 let equal = Node.equal
63 let expr f = f.Node.node.pos
64
65 let compare f1 f2 = compare f1.Node.id  f2.Node.id
66 let prio f =
67   match expr f with
68     | True | False -> 10
69     | Move _ -> 8
70     | And _ -> 6
71     | Or _ -> 1
72
73 let rec print ?(parent=false) ppf f =
74   if parent then fprintf ppf "(";
75   let _ = match expr f with
76     | True -> fprintf ppf "%s" Pretty.top
77     | False -> fprintf ppf "%s" Pretty.bottom
78     | And(f1,f2) ->
79       print ~parent:(prio f > prio f1) ppf f1;
80       fprintf ppf " %s "  Pretty.wedge;
81       print ~parent:(prio f > prio f2) ppf f2;
82     | Or(f1,f2) ->
83       (print ppf f1);
84       fprintf ppf " %s " Pretty.vee;
85       (print ppf f2);
86     | Move(dir, b, s) ->
87       let _ = flush_str_formatter() in
88       let fmt = str_formatter in
89         let a_str, d_str =
90           match  dir with
91           | `Left ->  Pretty.down_arrow, Pretty.subscript 1
92           | `Right -> Pretty.down_arrow, Pretty.subscript 2
93           | `Epsilon -> Pretty.epsilon, ""
94           | `Up1 -> Pretty.up_arrow, Pretty.subscript 1
95           | `Up2 -> Pretty.up_arrow, Pretty.subscript 2
96         in
97         fprintf fmt "%s%s" a_str d_str;
98         State.print fmt s;
99         let str = flush_str_formatter() in
100         if b then fprintf ppf "%s" str
101         else Pretty.pp_overline ppf str
102   in
103     if parent then fprintf ppf ")"
104
105 let print ppf f =  print ~parent:false ppf f
106
107 let is_true f = (expr f) == True
108 let is_false f = (expr f) == False
109
110
111 let cons pos neg =
112   let nnode = Node.make { pos = neg; neg = (Obj.magic 0); } in
113   let pnode = Node.make { pos = pos; neg = nnode } in
114     (Node.node nnode).neg <- pnode; (* works because the neg field isn't taken into
115                                        account for hashing ! *)
116     pnode,nnode
117
118
119 let true_,false_ = cons True False
120 let atom_ d p s =
121   fst (cons (Move(d,p,s)) (Move(d,not p,s)))
122
123 let not_ f = f.Node.node.neg
124
125
126 let order f1 f2 = if uid f1  < uid f2 then f2,f1 else f1,f2
127
128 let or_ f1 f2 =
129   (* Tautologies: x|x, x|not(x) *)
130
131   if equal f1 f2 then f1
132   else if equal f1 (not_ f2) then true_
133
134   (* simplification *)
135   else if is_true f1 || is_true f2 then true_
136   else if is_false f1 && is_false f2 then false_
137   else if is_false f1 then f2
138   else if is_false f2 then f1
139
140   (* commutativity of | *)
141   else
142     let f1, f2 = order f1 f2 in
143       fst (cons (Or(f1,f2)) (And(not_ f1, not_ f2)))
144
145
146 let and_ f1 f2 =
147   not_ (or_ (not_ f1) (not_ f2))
148
149
150 let of_bool = function true -> true_ | false -> false_
151