9ad2b129f544226813f665b983664e2326456184
[tatoo.git] / src / ata.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
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   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 INCLUDE "utils.ml"
17 open Format
18 open Misc
19 type move = [ `First_child
20             | `Next_sibling
21             | `Parent
22             | `Previous_sibling
23             | `Stay ]
24
25 module Move =
26 struct
27   type t = move
28   type 'a table = 'a array
29   let idx = function
30     | `First_child -> 0
31     | `Next_sibling -> 1
32     | `Parent -> 2
33     | `Previous_sibling -> 3
34     | `Stay -> 4
35   let ridx = function
36     | 0 -> `First_child
37     | 1 -> `Next_sibling
38     | 2 -> `Parent
39     | 3 -> `Previous_sibling
40     | 4 -> `Stay
41     | _ -> assert false
42
43   let create_table a = Array.make 5 a
44   let get m k = m.(idx k)
45   let set m k v = m.(idx k) <- v
46   let iter f m = Array.iteri (fun i v -> f (ridx i) v) m
47   let fold f m acc =
48     let acc = ref acc in
49     iter (fun i v -> acc := f i v !acc) m;
50     !acc
51   let for_all p m =
52     try
53       iter (fun i v -> if not (p i v) then raise Exit) m;
54       true
55     with
56       Exit -> false
57   let for_all2 p m1 m2 =
58     try
59       for i = 0 to 4 do
60         let v1 = m1.(i)
61         and v2 = m2.(i) in
62         if not (p (ridx i) v1 v2) then raise Exit
63       done;
64       true
65     with
66       Exit -> false
67
68   let exists p m =
69     try
70       iter (fun i v -> if p i v then raise Exit) m;
71       false
72     with
73       Exit -> true
74   let print ppf m =
75     match m with
76       `First_child -> fprintf ppf "%s" Pretty.down_arrow
77     | `Next_sibling -> fprintf ppf "%s" Pretty.right_arrow
78     | `Parent -> fprintf ppf "%s" Pretty.up_arrow
79     | `Previous_sibling -> fprintf ppf "%s" Pretty.left_arrow
80     | `Stay -> fprintf ppf "%s" Pretty.bullet
81
82   let print_table pr_e ppf m =
83     iter (fun i v -> fprintf ppf "%a: %a" print i pr_e v;
84       if (idx i) < 4 then fprintf ppf ", ") m
85 end
86
87 type predicate = Move of move * State.t
88                  | Is_first_child
89                  | Is_next_sibling
90                  | Is of Tree.NodeKind.t
91                  | Has_first_child
92                  | Has_next_sibling
93
94 module Atom =
95 struct
96
97   module Node =
98   struct
99     type t = predicate
100     let equal n1 n2 = n1 = n2
101     let hash n = Hashtbl.hash n
102   end
103
104   include Hcons.Make(Node)
105
106   let print ppf a =
107     match a.node with
108     | Move (m, q) ->
109       fprintf ppf "%a%a" Move.print m State.print q
110     | Is_first_child -> fprintf ppf "%s?" Pretty.up_arrow
111     | Is_next_sibling -> fprintf ppf "%s?" Pretty.left_arrow
112     | Is k -> fprintf ppf "is-%a?" Tree.NodeKind.print k
113     | Has_first_child -> fprintf ppf "%s?" Pretty.down_arrow
114     | Has_next_sibling -> fprintf ppf "%s?" Pretty.right_arrow
115
116 end
117
118
119 module Formula =
120 struct
121   include Boolean.Make(Atom)
122   open Tree.NodeKind
123   let mk_atom a = atom_ (Atom.make a)
124   let is k = mk_atom (Is k)
125
126   let has_first_child = mk_atom Has_first_child
127
128   let has_next_sibling = mk_atom Has_next_sibling
129
130   let is_first_child = mk_atom Is_first_child
131
132   let is_next_sibling = mk_atom Is_next_sibling
133
134   let is_attribute = mk_atom (Is Attribute)
135
136   let is_element = mk_atom (Is Element)
137
138   let is_processing_instruction = mk_atom (Is ProcessingInstruction)
139
140   let is_comment = mk_atom (Is Comment)
141
142   let mk_move m q = mk_atom (Move(m,q))
143   let first_child q =
144     and_
145       (mk_move `First_child q)
146       has_first_child
147
148   let next_sibling q =
149     and_
150       (mk_move `Next_sibling q)
151       has_next_sibling
152
153   let parent q =
154     and_
155       (mk_move `Parent  q)
156       is_first_child
157
158   let previous_sibling q =
159     and_
160       (mk_move `Previous_sibling q)
161       is_next_sibling
162
163   let stay q = mk_move `Stay q
164
165   let get_states_by_move phi =
166     let table = Move.create_table StateSet.empty in
167     iter (fun phi ->
168       match expr phi with
169       | Boolean.Atom ({ Atom.node = Move(v,q) ; _ }, _) ->
170         let s = Move.get table v in
171         Move.set table v (StateSet.add q s)
172       | _ -> ()
173     ) phi;
174     table
175   let get_states phi =
176     let table = get_states_by_move phi in
177     Move.fold (fun _ s acc -> StateSet.union s acc) table StateSet.empty
178
179 end
180
181 module Transition =
182 struct
183   include Hcons.Make (struct
184     type t = State.t * QNameSet.t * Formula.t
185     let equal (a, b, c) (d, e, f) =
186       a == d && b == e && c == f
187     let hash (a, b, c) =
188       HASHINT4 (PRIME1, a, ((QNameSet.uid b) :> int), ((Formula.uid c) :> int))
189   end)
190   let print ppf t =
191     let q, l, f = t.node in
192     fprintf ppf "%a, %a %s %a"
193       State.print q
194       QNameSet.print l
195       Pretty.double_right_arrow
196       Formula.print f
197 end
198
199
200 module TransList : sig
201   include Hlist.S with type elt = Transition.t
202   val print : Format.formatter -> ?sep:string -> t -> unit
203 end =
204 struct
205   include Hlist.Make(Transition)
206   let print ppf ?(sep="\n") l =
207     iter (fun t ->
208       let q, lab, f = Transition.node t in
209       fprintf ppf "%a, %a → %a%s"
210         State.print q
211         QNameSet.print lab
212         Formula.print f sep) l
213 end
214
215
216
217 type t = {
218   id : Uid.t;
219   mutable states : StateSet.t;
220   mutable starting_states : StateSet.t;
221   mutable selecting_states: StateSet.t;
222   transitions: (State.t, (QNameSet.t*Formula.t) list) Hashtbl.t;
223   mutable ranked_states : (StateSet.t*StateSet.t) array
224 }
225
226 let uid t = t.id
227
228 let get_states a = a.states
229 let get_starting_states a = a.starting_states
230 let get_selecting_states a = a.selecting_states
231 let get_states_by_rank a = a.ranked_states
232 let get_max_rank a = Array.length a.ranked_states - 1
233
234 let _pr_buff = Buffer.create 50
235 let _str_fmt = formatter_of_buffer _pr_buff
236 let _flush_str_fmt () = pp_print_flush _str_fmt ();
237   let s = Buffer.contents _pr_buff in
238   Buffer.clear _pr_buff; s
239
240 let print fmt a =
241   let _ = _flush_str_fmt() in
242   fprintf fmt
243     "Internal UID: %i@\n\
244      States: %a@\n\
245      Number of states: %i@\n\
246      Starting states: %a@\n\
247      Selection states: %a@\n\
248      Ranked states: %a@\n\
249      Alternating transitions:@\n"
250     (a.id :> int)
251     StateSet.print a.states
252     (StateSet.cardinal a.states)
253     StateSet.print a.starting_states
254     StateSet.print a.selecting_states
255     (let r = ref 0 in Pretty.print_array ~sep:", " (fun ppf (s1,s2) ->
256       fprintf ppf "(%i:%a,%a)" !r StateSet.print s1 StateSet.print s2; incr r)) a.ranked_states;
257   let trs =
258     Hashtbl.fold
259       (fun q t acc -> List.fold_left (fun acc (s , f) -> (q,s,f)::acc) acc t)
260       a.transitions
261       []
262   in
263   let sorted_trs = List.stable_sort (fun (q1, s1, _) (q2, s2, _) ->
264     let c = State.compare q2 q1 in if c == 0 then QNameSet.compare s2 s1 else c)
265     trs
266   in
267   let _ = _flush_str_fmt () in
268   let strs_strings, max_pre, max_all =
269     List.fold_left (fun (accl, accp, acca) (q, s, f) ->
270       let s1 = State.print _str_fmt q; _flush_str_fmt () in
271       let s2 = QNameSet.print _str_fmt s;  _flush_str_fmt () in
272       let s3 = Formula.print _str_fmt f;  _flush_str_fmt () in
273       let pre = Pretty.length s1 + Pretty.length s2 in
274       let all = Pretty.length s3 in
275       ( (q, s1, s2, s3) :: accl, max accp pre, max acca all)
276     ) ([], 0, 0) sorted_trs
277   in
278   let line = Pretty.line (max_all + max_pre + 6) in
279   let prev_q = ref State.dummy in
280   fprintf fmt "%s@\n" line;
281   List.iter (fun (q, s1, s2, s3) ->
282     if !prev_q != q && !prev_q != State.dummy then fprintf fmt "%s@\n"  line;
283     prev_q := q;
284     fprintf fmt "%s, %s" s1 s2;
285     fprintf fmt "%s"
286       (Pretty.padding (max_pre - Pretty.length s1 - Pretty.length s2));
287     fprintf fmt " %s  %s@\n" Pretty.right_arrow s3;
288   ) strs_strings;
289   fprintf fmt "%s@\n" line
290
291
292 let get_trans a tag states =
293   StateSet.fold (fun q acc0 ->
294     try
295       let trs = Hashtbl.find a.transitions q in
296       List.fold_left (fun acc1 (labs, phi) ->
297         if QNameSet.mem tag labs then
298           TransList.cons (Transition.make (q, labs, phi)) acc1
299         else acc1) acc0 trs
300     with Not_found -> acc0
301   ) states TransList.nil
302
303
304 let get_form a tag q =
305   try
306     let trs = Hashtbl.find a.transitions q in
307     List.fold_left (fun aphi (labs, phi) ->
308       if QNameSet.mem tag labs then Formula.or_ aphi phi else aphi
309     ) Formula.false_ trs
310   with
311     Not_found -> Formula.false_
312
313 (*
314   [complete transitions a] ensures that for each state q
315   and each symbols s in the alphabet, a transition q, s exists.
316   (adding q, s -> F when necessary).
317 *)
318
319 let complete_transitions a =
320   StateSet.iter (fun q ->
321     if StateSet.mem q a.starting_states then ()
322     else
323       let qtrans = Hashtbl.find a.transitions q in
324       let rem =
325         List.fold_left (fun rem (labels, _) ->
326           QNameSet.diff rem labels) QNameSet.any qtrans
327       in
328       let nqtrans =
329         if QNameSet.is_empty rem then qtrans
330         else
331           (rem, Formula.false_) :: qtrans
332       in
333       Hashtbl.replace a.transitions q nqtrans
334   ) a.states
335
336 (* [cleanup_states] remove states that do not lead to a
337    selecting states *)
338
339 let cleanup_states a =
340   let memo = ref StateSet.empty in
341   let rec loop q =
342     if not (StateSet.mem q !memo) then begin
343       memo := StateSet.add q !memo;
344       let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
345       List.iter (fun (_, phi) ->
346         StateSet.iter loop (Formula.get_states phi)) trs
347     end
348   in
349   StateSet.iter loop a.selecting_states;
350   let unused = StateSet.diff a.states !memo in
351   StateSet.iter (fun q -> Hashtbl.remove a.transitions q) unused;
352   a.states <- !memo
353
354 (* [normalize_negations a] removes negative atoms in the formula
355    complementing the sub-automaton in the negative states.
356    [TODO check the meaning of negative upward arrows]
357 *)
358
359 let normalize_negations auto =
360   let memo_state = Hashtbl.create 17 in
361   let todo = Queue.create () in
362   let rec flip b f =
363     match Formula.expr f with
364       Boolean.True | Boolean.False -> if b then f else Formula.not_ f
365     | Boolean.Or(f1, f2) ->
366       (if b then Formula.or_ else Formula.and_)(flip b f1) (flip b f2)
367     | Boolean.And(f1, f2) ->
368       (if b then Formula.and_ else Formula.or_)(flip b f1) (flip b f2)
369     | Boolean.Atom(a, b') -> begin
370       match a.Atom.node with
371       | Move (m,  q) ->
372         if b == b' then begin
373             (* a appears positively, either no negation or double negation *)
374           if not (Hashtbl.mem memo_state (q,b)) then Queue.add (q,true) todo;
375           Formula.mk_atom (Move(m, q))
376         end else begin
377             (* need to reverse the atom
378                either we have a positive state deep below a negation
379                or we have a negative state in a positive formula
380                b' = sign of the state
381                b = sign of the enclosing formula
382             *)
383           let not_q =
384             try
385                 (* does the inverted state of q exist ? *)
386               Hashtbl.find memo_state (q, false)
387             with
388               Not_found ->
389                   (* create a new state and add it to the todo queue *)
390                 let nq = State.make () in
391                 auto.states <- StateSet.add nq auto.states;
392                 Hashtbl.add memo_state (q, false) nq;
393                 Queue.add (q, false) todo; nq
394           in
395           Formula.mk_atom (Move (m,not_q))
396         end
397       | _ -> if b then f else Formula.not_ f
398     end
399   in
400   (* states that are not reachable from a selection stat are not interesting *)
401   StateSet.iter (fun q -> Queue.add (q, true) todo) auto.selecting_states;
402
403   while not (Queue.is_empty todo) do
404     let (q, b) as key = Queue.pop todo in
405     if not (StateSet.mem q auto.starting_states) then
406       let q' =
407         try
408           Hashtbl.find memo_state key
409         with
410           Not_found ->
411             let nq = if b then q else
412                 let nq = State.make () in
413                 auto.states <- StateSet.add nq auto.states;
414                 nq
415             in
416             Hashtbl.add memo_state key nq; nq
417       in
418       let trans = try Hashtbl.find auto.transitions q with Not_found -> [] in
419       let trans' = List.map (fun (lab, f) -> lab, flip b f) trans in
420       Hashtbl.replace auto.transitions q' trans';
421   done;
422   cleanup_states auto
423
424 (* [compute_dependencies auto] returns a hash table storing for each
425    states [q] a Move.table containing the set of states on which [q]
426    depends (loosely). [q] depends on [q'] if there is a transition
427    [q, {...} -> phi], where [q'] occurs in [phi].
428 *)
429 let compute_dependencies auto =
430   let edges = Hashtbl.create 17 in
431   StateSet.iter
432     (fun q -> Hashtbl.add edges q (Move.create_table StateSet.empty))
433     auto.starting_states;
434   Hashtbl.iter (fun q trans ->
435     let moves = try Hashtbl.find edges q with Not_found ->
436       let m = Move.create_table StateSet.empty in
437       Hashtbl.add edges q m;
438       m
439     in
440     List.iter (fun (_, phi) ->
441       let m_phi = Formula.get_states_by_move phi in
442       Move.iter (fun m set ->
443         Move.set moves m (StateSet.union set (Move.get moves m)))
444         m_phi) trans) auto.transitions;
445
446   edges
447
448 let state_prerequisites dir auto q =
449   let trans = Hashtbl.find auto.transitions q in
450   List.fold_left (fun acc (_, phi) ->
451     let m_phi = Formula.get_states_by_move phi in
452     let prereq = Move.get m_phi dir in
453     StateSet.union prereq acc)
454     StateSet.empty trans
455
456 let compute_rank auto =
457   let dependencies = compute_dependencies auto in
458   let upward = [ `Stay ; `Parent ; `Previous_sibling ] in
459   let downward = [ `Stay; `First_child; `Next_sibling ] in
460   let swap dir = if dir == upward then downward else upward in
461   let is_satisfied dir q t =
462     Move.for_all (fun d set ->
463       if List.mem d dir then
464         StateSet.(is_empty (remove q set))
465       else StateSet.is_empty set) t
466   in
467   let update_dependencies dir initacc =
468     let rec loop acc =
469       let new_acc =
470         Hashtbl.fold (fun q deps acc ->
471           let to_remove = StateSet.union acc initacc in
472           List.iter
473             (fun m ->
474               Move.set deps m (StateSet.diff (Move.get deps m) to_remove)
475             )
476             dir;
477           if is_satisfied dir q deps then StateSet.add q acc else acc
478         ) dependencies acc
479       in
480       if acc == new_acc then new_acc else loop new_acc
481     in
482     let satisfied = loop StateSet.empty in
483     StateSet.iter (fun q ->
484       Hashtbl.remove dependencies q) satisfied;
485     satisfied
486   in
487   let current_states = ref StateSet.empty in
488   let rank_list = ref [] in
489   let rank = ref 0 in
490   let current_dir = ref upward in
491   let detect_cycle = ref 0 in
492   while Hashtbl.length dependencies != 0 do
493     let new_sat = update_dependencies !current_dir !current_states in
494     if StateSet.is_empty new_sat then incr detect_cycle;
495     if !detect_cycle > 2 then assert false;
496     rank_list := (!rank, new_sat) :: !rank_list;
497     rank := !rank + 1;
498     current_dir := swap !current_dir;
499     current_states := StateSet.union new_sat !current_states;
500   done;
501   let by_rank = Hashtbl.create 17 in
502   List.iter (fun (r,s) ->
503     let set = try Hashtbl.find by_rank r with Not_found -> StateSet.empty in
504     Hashtbl.replace by_rank r (StateSet.union s set)) !rank_list;
505   let rank = Hashtbl.length by_rank in
506   auto.ranked_states <-
507     Array.init rank
508     (fun i ->
509       let set = try Hashtbl.find by_rank i with Not_found -> StateSet.empty in
510       let source =
511         if i + 1 == rank then auto.selecting_states else
512           let post_set = Hashtbl.find by_rank (i+1) in
513           let source = if i + 1 == rank then post_set else
514               StateSet.fold (fun q acc ->
515                 List.fold_left (fun acc m ->
516                   StateSet.union acc (state_prerequisites m auto q ))
517                   acc [`First_child; `Next_sibling; `Parent; `Previous_sibling; `Stay]
518               ) post_set StateSet.empty
519           in
520           StateSet.inter set source
521       in
522       (source, set)
523     )
524
525
526 module Builder =
527 struct
528   type auto = t
529   type t = auto
530   let next = Uid.make_maker ()
531
532   let make () =
533     let auto =
534       {
535         id = next ();
536         states = StateSet.empty;
537         starting_states = StateSet.empty;
538         selecting_states = StateSet.empty;
539         transitions = Hashtbl.create MED_H_SIZE;
540         ranked_states = [| |]
541       }
542     in
543     auto
544
545   let add_state a ?(starting=false) ?(selecting=false) q =
546     a.states <- StateSet.add q a.states;
547     if starting then a.starting_states <- StateSet.add q a.starting_states;
548     if selecting then a.selecting_states <- StateSet.add q a.selecting_states
549
550   let add_trans a q s f =
551     if not (StateSet.mem q a.states) then add_state a q;
552     let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
553     let cup, ntrs =
554       List.fold_left (fun (acup, atrs) (labs, phi) ->
555         let lab1 = QNameSet.inter labs s in
556         let lab2 = QNameSet.diff labs s in
557         let tr1 =
558           if QNameSet.is_empty lab1 then []
559           else [ (lab1, Formula.or_ phi f) ]
560         in
561         let tr2 =
562           if QNameSet.is_empty lab2 then []
563           else [ (lab2, Formula.or_ phi f) ]
564         in
565         (QNameSet.union acup labs, tr1@ tr2 @ atrs)
566       ) (QNameSet.empty, []) trs
567     in
568     let rem = QNameSet.diff s cup in
569     let ntrs = if QNameSet.is_empty rem then ntrs
570       else (rem, f) :: ntrs
571     in
572     Hashtbl.replace a.transitions q ntrs
573
574   let finalize a =
575     complete_transitions a;
576     normalize_negations a;
577     compute_rank a;
578     a
579 end
580
581
582 let map_set f s =
583   StateSet.fold (fun q a -> StateSet.add (f q) a) s StateSet.empty
584
585 let map_hash fk fv h =
586   let h' = Hashtbl.create (Hashtbl.length h) in
587   let () = Hashtbl.iter (fun k v -> Hashtbl.add h' (fk k) (fv v)) h in
588   h'
589
590 let rec map_form f phi =
591   match Formula.expr phi with
592   | Boolean.Or(phi1, phi2) -> Formula.or_ (map_form f phi1) (map_form f phi2)
593   | Boolean.And(phi1, phi2) -> Formula.and_ (map_form f phi1) (map_form f phi2)
594   | Boolean.Atom({ Atom.node = Move(m,q); _}, b) ->
595     let a = Formula.mk_atom (Move (m,f q)) in
596     if b then a else Formula.not_ a
597   | _ -> phi
598
599 let rename_states mapper a =
600   let rename q = try Hashtbl.find mapper q with Not_found -> q in
601   { Builder.make () with
602     states = map_set rename a.states;
603     starting_states = map_set rename a.starting_states;
604     selecting_states = map_set rename a.selecting_states;
605     transitions =
606       map_hash
607         rename
608         (fun l ->
609           (List.map (fun (labels, form) -> (labels, map_form rename form)) l))
610         a.transitions;
611     ranked_states = Array.map (fun (a,b) -> map_set rename a, map_set rename b) a.ranked_states
612   }
613
614 let copy a =
615   let mapper = Hashtbl.create MED_H_SIZE in
616   let () =
617     StateSet.iter (fun q -> Hashtbl.add mapper q (State.make())) a.states
618   in
619   rename_states mapper a
620
621
622 let concat a1 a2 =
623   let a1 = copy a1 in
624   let a2 = copy a2 in
625   let link_phi =
626     StateSet.fold
627       (fun q phi -> Formula.(or_ (stay q) phi))
628       a1.selecting_states Formula.false_
629   in
630   Hashtbl.iter (fun q trs -> Hashtbl.add a1.transitions q trs)
631     a2.transitions;
632   StateSet.iter
633     (fun q ->
634       Hashtbl.replace a1.transitions q [(QNameSet.any, link_phi)])
635     a2.starting_states;
636   let a = { a1 with
637     states = StateSet.union a1.states a2.states;
638     selecting_states = a2.selecting_states;
639     transitions = a1.transitions;
640   }
641   in compute_rank a; a
642
643 let merge a1 a2 =
644   let a1 = copy a1 in
645   let a2 = copy a2 in
646   let a = { a1 with
647     states = StateSet.union a1.states a2.states;
648     selecting_states = StateSet.union a1.selecting_states a2.selecting_states;
649     starting_states = StateSet.union a1.starting_states a2.starting_states;
650     transitions =
651       let () =
652         Hashtbl.iter (fun k v -> Hashtbl.add a1.transitions k v) a2.transitions
653       in
654       a1.transitions
655   } in
656   compute_rank a ; a
657
658
659 let link a1 a2 q link_phi =
660   let a = { a1 with
661     states = StateSet.union a1.states a2.states;
662     selecting_states = StateSet.singleton q;
663     starting_states = StateSet.union a1.starting_states a2.starting_states;
664     transitions =
665       let () =
666         Hashtbl.iter (fun k v -> Hashtbl.add a1.transitions k v) a2.transitions
667       in
668       Hashtbl.add a1.transitions q [(QNameSet.any, link_phi)];
669       a1.transitions
670   }
671   in
672   compute_rank a; a
673
674 let union a1 a2 =
675   let a1 = copy a1 in
676   let a2 = copy a2 in
677   let q = State.make () in
678   let link_phi =
679     StateSet.fold
680       (fun q phi -> Formula.(or_ (stay q) phi))
681       (StateSet.union a1.selecting_states a2.selecting_states)
682       Formula.false_
683   in
684   link a1 a2 q link_phi
685
686 let inter a1 a2 =
687   let a1 = copy a1 in
688   let a2 = copy a2 in
689   let q = State.make () in
690   let link_phi =
691     StateSet.fold
692       (fun q phi -> Formula.(and_ (stay q) phi))
693       (StateSet.union a1.selecting_states a2.selecting_states)
694       Formula.true_
695   in
696   link a1 a2 q link_phi
697
698 let neg a =
699   let a = copy a in
700   let q = State.make () in
701   let link_phi =
702     StateSet.fold
703       (fun q phi -> Formula.(and_ (not_(stay q)) phi))
704       a.selecting_states
705       Formula.true_
706   in
707   let () = Hashtbl.add a.transitions q [(QNameSet.any, link_phi)] in
708   let a =
709     { a with
710       selecting_states = StateSet.singleton q;
711     }
712   in
713   normalize_negations a; compute_rank a; a
714
715 let diff a1 a2 = inter a1 (neg a2)