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-24 23:14:46 CEST by Kim Nguyen>
23 type predicate = | First_child
30 | Is of (Tree.NodeKind.t)
34 let is_move p = match p with
35 | First_child | Next_sibling
36 | Parent | Previous_sibling | Stay -> true
40 type atom = predicate * bool * State.t
42 module Atom : (Formula.ATOM with type data = atom) =
48 let equal n1 n2 = n1 = n2
49 let hash n = Hashtbl.hash n
52 include Hcons.Make(Node)
55 let p, b, q = a.node in
56 if not b then fprintf ppf "%s" Pretty.lnot;
58 | First_child -> fprintf ppf "FC(%a)" State.print q
59 | Next_sibling -> fprintf ppf "NS(%a)" State.print q
60 | Parent -> fprintf ppf "FC%s(%a)" Pretty.inverse State.print q
61 | Previous_sibling -> fprintf ppf "NS%s(%a)" Pretty.inverse State.print q
62 | Stay -> fprintf ppf "%s(%a)" Pretty.epsilon State.print q
63 | Is_first_child -> fprintf ppf "FC%s?" Pretty.inverse
64 | Is_next_sibling -> fprintf ppf "NS%s?" Pretty.inverse
65 | Is k -> fprintf ppf "is-%a?" Tree.NodeKind.print k
66 | Has_first_child -> fprintf ppf "FC?"
67 | Has_next_sibling -> fprintf ppf "NS?"
70 let p, b, q = a.node in
78 include Formula.Make(Atom)
80 let mk_atom a b c = atom_ (Atom.make (a,b,c))
81 let mk_kind k = mk_atom (Is k) true State.dummy
83 (mk_atom Has_first_child true State.dummy)
85 let has_next_sibling =
86 (mk_atom Has_next_sibling true State.dummy)
89 (mk_atom Is_first_child true State.dummy)
92 (mk_atom Is_next_sibling true State.dummy)
95 (mk_atom (Is Attribute) true State.dummy)
98 (mk_atom (Is Element) true State.dummy)
100 let is_processing_instruction =
101 (mk_atom (Is ProcessingInstruction) true State.dummy)
104 (mk_atom (Is Comment) true State.dummy)
108 (mk_atom First_child true q)
113 (mk_atom Next_sibling true q)
118 (mk_atom Parent true q)
121 let previous_sibling q =
123 (mk_atom Previous_sibling true q)
127 (mk_atom Stay true q)
132 | Formula.Atom a -> let _, _, q = Atom.node a in
133 if q != State.dummy then StateSet.add q acc else acc
140 module Transition = Hcons.Make (struct
141 type t = State.t * QNameSet.t * SFormula.t
142 let equal (a, b, c) (d, e, f) =
143 a == d && b == e && c == f
145 HASHINT4 (PRIME1, a, ((QNameSet.uid b) :> int), ((SFormula.uid c) :> int))
149 module TransList : sig
150 include Hlist.S with type elt = Transition.t
151 val print : Format.formatter -> ?sep:string -> t -> unit
154 include Hlist.Make(Transition)
155 let print ppf ?(sep="\n") l =
157 let q, lab, f = Transition.node t in
158 fprintf ppf "%a, %a -> %a%s" State.print q QNameSet.print lab SFormula.print f sep) l
163 type node_summary = int
164 let dummy_summary = -1
174 let has_right (s : node_summary) : bool =
176 let has_left (s : node_summary) : bool =
177 Obj.magic ((s lsr 1) land 1)
179 let is_right (s : node_summary) : bool =
180 Obj.magic ((s lsr 2) land 1)
182 let is_left (s : node_summary) : bool =
183 Obj.magic ((s lsr 3) land 1)
185 let kind (s : node_summary ) : Tree.NodeKind.t =
188 let node_summary is_left is_right has_left has_right kind =
189 ((Obj.magic kind) lsl 4) lor
190 ((Obj.magic is_left) lsl 3) lor
191 ((Obj.magic is_right) lsl 2) lor
192 ((Obj.magic has_left) lsl 1) lor
193 (Obj.magic has_right)
201 summary : node_summary;
202 (** optimization infos,
203 not taken into account during hashconsing *)
207 module Config = Hcons.Make(struct
212 c.unsat == d.unsat &&
214 c.summary == d.summary
217 HASHINT4((c.sat.StateSet.id :> int),
218 (c.unsat.StateSet.id :> int),
219 (c.todo.TransList.id :> int),
226 mutable states : StateSet.t;
227 mutable selection_states: StateSet.t;
228 transitions: (State.t, (QNameSet.t*SFormula.t) list) Hashtbl.t;
229 mutable cache2 : TransList.t Cache.N2.t;
230 mutable cache4 : Config.t Cache.N4.t;
233 let next = Uid.make_maker ()
235 let dummy2 = TransList.cons
236 (Transition.make (State.dummy,QNameSet.empty, SFormula.false_))
241 let dummy_config = Config.make { sat = StateSet.empty;
242 unsat = StateSet.empty;
243 todo = TransList.nil;
244 summary = dummy_summary;
250 let auto = { id = next ();
252 selection_states = ss;
253 transitions = Hashtbl.create 17;
254 cache2 = Cache.N2.create dummy2;
255 cache4 = Cache.N4.create dummy_config;
261 Cache.N2.iteri (fun _ _ _ b -> if b then incr n2) auto.cache2;
262 Cache.N4.iteri (fun _ _ _ _ _ b -> if b then incr n4) auto.cache4;
263 Format.eprintf "STATS: automaton %i, cache2: %i entries, cache6: %i entries\n%!"
264 (auto.id :> int) !n2 !n4;
265 let c2l, c2u = Cache.N2.stats auto.cache2 in
266 let c4l, c4u = Cache.N4.stats auto.cache4 in
267 Format.eprintf "STATS: cache2: length: %i, used: %i, occupation: %f\n%!" c2l c2u (float c2u /. float c2l);
268 Format.eprintf "STATS: cache4: length: %i, used: %i, occupation: %f\n%!" c4l c4u (float c4u /. float c4l)
274 a.cache2 <- Cache.N2.create (Cache.N2.dummy a.cache2);
275 a.cache4 <- Cache.N4.create (Cache.N4.dummy a.cache4)
278 let get_trans_aux a tag states =
279 StateSet.fold (fun q acc0 ->
281 let trs = Hashtbl.find a.transitions q in
282 List.fold_left (fun acc1 (labs, phi) ->
283 if QNameSet.mem tag labs then TransList.cons (Transition.make (q, labs, phi)) acc1 else acc1) acc0 trs
284 with Not_found -> acc0
285 ) states TransList.nil
288 let get_trans a tag states =
290 Cache.N2.find a.cache2
291 (tag.QName.id :> int) (states.StateSet.id :> int)
293 if trs == dummy2 then
294 let trs = get_trans_aux a tag states in
297 (tag.QName.id :> int)
298 (states.StateSet.id :> int) trs; trs)
303 let eval_form phi fcs nss ps ss is_left is_right has_left has_right kind =
305 begin match SFormula.expr phi with
306 Formula.True | Formula.False -> phi
308 let p, b, q = Atom.node a in begin
311 if b == StateSet.mem q fcs then SFormula.true_ else phi
313 if b == StateSet.mem q nss then SFormula.true_ else phi
314 | Parent | Previous_sibling ->
315 if b == StateSet.mem q ps then SFormula.true_ else phi
317 if b == StateSet.mem q ss then SFormula.true_ else phi
318 | Is_first_child -> SFormula.of_bool (b == is_left)
319 | Is_next_sibling -> SFormula.of_bool (b == is_right)
320 | Is k -> SFormula.of_bool (b == (k == kind))
321 | Has_first_child -> SFormula.of_bool (b == has_left)
322 | Has_next_sibling -> SFormula.of_bool (b == has_right)
324 | Formula.And(phi1, phi2) -> SFormula.and_ (loop phi1) (loop phi2)
325 | Formula.Or (phi1, phi2) -> SFormula.or_ (loop phi1) (loop phi2)
330 let int_of_conf is_left is_right has_left has_right kind =
331 ((Obj.magic kind) lsl 4) lor
332 ((Obj.magic is_left) lsl 3) lor
333 ((Obj.magic is_right) lsl 2) lor
334 ((Obj.magic has_left) lsl 1) lor
335 (Obj.magic has_right)
337 let eval_trans auto ltrs fcs nss ps ss is_left is_right has_left has_right kind =
338 let n = int_of_conf is_left is_right has_left has_right kind
339 and k = (fcs.StateSet.id :> int)
340 and l = (nss.StateSet.id :> int)
341 and m = (ps.StateSet.id :> int) in
342 let rec loop ltrs ss =
343 let i = (ltrs.TransList.id :> int)
344 and j = (ss.StateSet.id :> int) in
345 let (new_ltrs, new_ss) as res =
346 let res = Cache.N6.find auto.cache6 i j k l m n in
347 if res == dummy6 then
349 TransList.fold (fun trs (acct, accs) ->
350 let q, lab, phi = Transition.node trs in
351 if StateSet.mem q accs then (acct, accs) else
355 is_left is_right has_left has_right kind
357 if SFormula.is_true new_phi then
358 (acct, StateSet.add q accs)
359 else if SFormula.is_false new_phi then
362 let new_tr = Transition.make (q, lab, new_phi) in
363 (TransList.cons new_tr acct, accs)
364 ) ltrs (TransList.nil, ss)
366 Cache.N6.add auto.cache6 i j k l m n res; res
370 if new_ss == ss then res else
377 let simplify_atom atom pos q { Config.node=config; _ } =
378 if (pos && StateSet.mem q config.sat)
379 || ((not pos) && StateSet.mem q config.unsat) then SFormula.true_
380 else if (pos && StateSet.mem q config.unsat)
381 || ((not pos) && StateSet.mem q config.sat) then SFormula.false_
384 let eval_form phi fcs nss ps ss summary =
386 begin match SFormula.expr phi with
387 Formula.True | Formula.False -> phi
389 let p, b, q = Atom.node a in begin
391 | First_child -> simplify_atom phi b q fcs
392 | Next_sibling -> simplify_atom phi b q nss
393 | Parent | Previous_sibling -> simplify_atom phi b q ps
394 | Stay -> simplify_atom phi b q ss
395 | Is_first_child -> SFormula.of_bool (b == (is_left summary))
396 | Is_next_sibling -> SFormula.of_bool (b == (is_right summary))
397 | Is k -> SFormula.of_bool (b == (k == (kind summary)))
398 | Has_first_child -> SFormula.of_bool (b == (has_left summary))
399 | Has_next_sibling -> SFormula.of_bool (b == (has_right summary))
401 | Formula.And(phi1, phi2) -> SFormula.and_ (loop phi1) (loop phi2)
402 | Formula.Or (phi1, phi2) -> SFormula.or_ (loop phi1) (loop phi2)
409 let eval_trans auto fcs nss ps ss =
410 let fcsid = (fcs.Config.id :> int) in
411 let nssid = (nss.Config.id :> int) in
412 let psid = (ps.Config.id :> int) in
413 let rec loop old_config =
414 let oid = (old_config.Config.id :> int) in
416 let res = Cache.N4.find auto.cache4 oid fcsid nssid psid in
417 if res != dummy_config then res
422 summary = old_summary } = old_config.Config.node
424 let sat, unsat, removed, kept, todo =
427 let q, lab, phi = Transition.node trs in
428 let a_sat, a_unsat, a_rem, a_kept, a_todo = acc in
429 if StateSet.mem q a_sat || StateSet.mem q a_unsat then acc else
431 eval_form phi fcs nss ps old_config old_summary
433 if SFormula.is_true new_phi then
434 StateSet.add q a_sat, a_unsat, StateSet.add q a_rem, a_kept, a_todo
435 else if SFormula.is_false new_phi then
436 a_sat, StateSet.add q a_unsat, StateSet.add q a_rem, a_kept, a_todo
438 let new_tr = Transition.make (q, lab, new_phi) in
439 (a_sat, a_unsat, a_rem, StateSet.add q a_kept, (TransList.cons new_tr a_todo))
440 ) old_todo (old_sat, old_unsat, StateSet.empty, StateSet.empty, TransList.nil)
442 (* States that have been removed from the todo list and not kept are now
444 let unsat = StateSet.union unsat (StateSet.diff removed kept) in
445 (* States that were found once to be satisfiable remain so *)
446 let unsat = StateSet.diff unsat sat in
447 let new_config = Config.make { old_config.Config.node with sat; unsat; todo; } in
448 Cache.N4.add auto.cache4 oid fcsid nssid psid new_config;
451 if res == old_config then res else loop res
456 [add_trans a q labels f] adds a transition [(q,labels) -> f] to the
457 automaton [a] but ensures that transitions remains pairwise disjoint
460 let add_trans a q s f =
461 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
463 List.fold_left (fun (acup, atrs) (labs, phi) ->
464 let lab1 = QNameSet.inter labs s in
465 let lab2 = QNameSet.diff labs s in
467 if QNameSet.is_empty lab1 then []
468 else [ (lab1, SFormula.or_ phi f) ]
471 if QNameSet.is_empty lab2 then []
472 else [ (lab2, SFormula.or_ phi f) ]
474 (QNameSet.union acup labs, tr1@ tr2 @ atrs)
475 ) (QNameSet.empty, []) trs
477 let rem = QNameSet.diff s cup in
478 let ntrs = if QNameSet.is_empty rem then ntrs
479 else (rem, f) :: ntrs
481 Hashtbl.replace a.transitions q ntrs
483 let _pr_buff = Buffer.create 50
484 let _str_fmt = formatter_of_buffer _pr_buff
485 let _flush_str_fmt () = pp_print_flush _str_fmt ();
486 let s = Buffer.contents _pr_buff in
487 Buffer.clear _pr_buff; s
491 "Internal UID: %i@\n\
493 Selection states: %a@\n\
494 Alternating transitions:@\n"
496 StateSet.print a.states
497 StateSet.print a.selection_states;
500 (fun q t acc -> List.fold_left (fun acc (s , f) -> (q,s,f)::acc) acc t)
504 let sorted_trs = List.stable_sort (fun (q1, s1, _) (q2, s2, _) ->
505 let c = State.compare q1 q2 in - (if c == 0 then QNameSet.compare s1 s2 else c))
508 let _ = _flush_str_fmt () in
509 let strs_strings, max_pre, max_all = List.fold_left (fun (accl, accp, acca) (q, s, f) ->
510 let s1 = State.print _str_fmt q; _flush_str_fmt () in
511 let s2 = QNameSet.print _str_fmt s; _flush_str_fmt () in
512 let s3 = SFormula.print _str_fmt f; _flush_str_fmt () in
513 let pre = Pretty.length s1 + Pretty.length s2 in
514 let all = Pretty.length s3 in
515 ( (q, s1, s2, s3) :: accl, max accp pre, max acca all)
516 ) ([], 0, 0) sorted_trs
518 let line = Pretty.line (max_all + max_pre + 6) in
519 let prev_q = ref State.dummy in
520 fprintf fmt "%s@\n" line;
521 List.iter (fun (q, s1, s2, s3) ->
522 if !prev_q != q && !prev_q != State.dummy then fprintf fmt "%s@\n" line;
524 fprintf fmt "%s, %s" s1 s2;
525 fprintf fmt "%s" (Pretty.padding (max_pre - Pretty.length s1 - Pretty.length s2));
526 fprintf fmt " %s %s@\n" Pretty.right_arrow s3;
528 fprintf fmt "%s@\n" line
531 [complete transitions a] ensures that for each state q
532 and each symbols s in the alphabet, a transition q, s exists.
533 (adding q, s -> F when necessary).
536 let complete_transitions a =
537 StateSet.iter (fun q ->
538 let qtrans = Hashtbl.find a.transitions q in
540 List.fold_left (fun rem (labels, _) ->
541 QNameSet.diff rem labels) QNameSet.any qtrans
544 if QNameSet.is_empty rem then qtrans
546 (rem, SFormula.false_) :: qtrans
548 Hashtbl.replace a.transitions q nqtrans
551 let cleanup_states a =
552 let memo = ref StateSet.empty in
554 if not (StateSet.mem q !memo) then begin
555 memo := StateSet.add q !memo;
556 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
557 List.iter (fun (_, phi) ->
558 StateSet.iter loop (SFormula.get_states phi)) trs
561 StateSet.iter loop a.selection_states;
562 let unused = StateSet.diff a.states !memo in
563 StateSet.iter (fun q -> Hashtbl.remove a.transitions q) unused;
566 (* [normalize_negations a] removes negative atoms in the formula
567 complementing the sub-automaton in the negative states.
568 [TODO check the meaning of negative upward arrows]
571 let normalize_negations auto =
572 let memo_state = Hashtbl.create 17 in
573 let todo = Queue.create () in
575 match SFormula.expr f with
576 Formula.True | Formula.False -> if b then f else SFormula.not_ f
577 | Formula.Or(f1, f2) -> (if b then SFormula.or_ else SFormula.and_)(flip b f1) (flip b f2)
578 | Formula.And(f1, f2) -> (if b then SFormula.and_ else SFormula.or_)(flip b f1) (flip b f2)
579 | Formula.Atom(a) -> begin
580 let l, b', q = Atom.node a in
581 if q == State.dummy then if b then f else SFormula.not_ f
583 if b == b' then begin
584 (* a appears positively, either no negation or double negation *)
585 if not (Hashtbl.mem memo_state (q,b)) then Queue.add (q,true) todo;
586 SFormula.atom_ (Atom.make (l, true, q))
588 (* need to reverse the atom
589 either we have a positive state deep below a negation
590 or we have a negative state in a positive formula
591 b' = sign of the state
592 b = sign of the enclosing formula
596 (* does the inverted state of q exist ? *)
597 Hashtbl.find memo_state (q, false)
600 (* create a new state and add it to the todo queue *)
601 let nq = State.make () in
602 auto.states <- StateSet.add nq auto.states;
603 Hashtbl.add memo_state (q, false) nq;
604 Queue.add (q, false) todo; nq
606 SFormula.atom_ (Atom.make (l, true, not_q))
610 (* states that are not reachable from a selection stat are not interesting *)
611 StateSet.iter (fun q -> Queue.add (q, true) todo) auto.selection_states;
613 while not (Queue.is_empty todo) do
614 let (q, b) as key = Queue.pop todo in
617 Hashtbl.find memo_state key
620 let nq = if b then q else
621 let nq = State.make () in
622 auto.states <- StateSet.add nq auto.states;
625 Hashtbl.add memo_state key nq; nq
627 let trans = Hashtbl.find auto.transitions q in
628 let trans' = List.map (fun (lab, f) -> lab, flip b f) trans in
629 Hashtbl.replace auto.transitions q' trans';