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 (***********************************************************************)
20 module Make (T : Tree.S) =
25 (* Pack into an integer the result of the is_* and has_ predicates
38 let has_right (s : t) : bool =
41 let has_left (s : t) : bool =
42 Obj.magic ((s lsr 1) land 1)
44 let is_right (s : t) : bool =
45 Obj.magic ((s lsr 2) land 1)
47 let is_left (s : t) : bool =
48 Obj.magic ((s lsr 3) land 1)
50 let kind (s : t) : Tree.NodeKind.t =
53 let make is_left is_right has_left has_right kind =
54 ((Obj.magic kind) lsl 4) lor
55 ((int_of_bool is_left) lsl 3) lor
56 ((int_of_bool is_right) lsl 2) lor
57 ((int_of_bool has_left) lsl 1) lor
58 (int_of_bool has_right)
63 sat : StateSet.t; (* States that are satisfied at the current node *)
64 todo : StateSet.t; (* States that remain to be proven *)
65 (* For every node_status and automaton a:
66 a.states - (sat U todo) = unsat *)
67 summary : NodeSummary.t; (* Summary of the shape of the node *)
69 (* Describe what is kept at each node for a run *)
73 include Hcons.Make(struct
79 c.summary == d.summary
82 HASHINT3((c.sat.StateSet.id :> int),
83 (c.todo.StateSet.id :> int),
89 "{ sat: %a; todo: %a; summary: _ }"
90 StateSet.print s.node.sat
91 StateSet.print s.node.todo
95 NodeStatus.make { sat = StateSet.empty;
96 todo = StateSet.empty;
97 summary = NodeSummary.dummy;
103 (* The argument of the run *)
105 (* The automaton to be run *)
106 status : NodeStatus.t array;
107 (* A mapping from node preorders to NodeStatus *)
108 unstable : Bitvector.t;
109 (* A bitvector remembering whether a subtree is stable *)
111 (* A boolean indicating whether the run is incomplete *)
113 (* The number of times this run was updated *)
114 mutable cache2 : Ata.Formula.t Cache.N2.t;
115 (* A cache from states * label to list of transitions *)
116 mutable cache5 : NodeStatus.t Cache.N5.t;
120 let stable r = not r.redo
125 let dummy_form = Ata.Formula.stay State.dummy
128 let len = T.size tree in
132 status = Array.create len dummy_status;
133 unstable = Bitvector.create ~init:true len;
136 cache2 = Cache.N2.create dummy_form;
137 cache5 = Cache.N5.create dummy_status;
141 if i < 0 then dummy_status else Array.get a i
143 let unsafe_get_status a i =
144 if i < 0 then dummy_status else Array.unsafe_get a i
148 DEFINE TRACE(e) = (e)
153 let html tree node i config msg =
154 let config = config.NodeStatus.node in
155 Html.trace (T.preorder tree node) i
156 "node: %i<br/>%s<br/>sat: %a<br/>todo: %a<br/>round: %i<br/>"
157 (T.preorder tree node)
159 StateSet.print config.sat
160 StateSet.print config.todo
164 let debug msg tree node i config =
165 let config = config.NodeStatus.node in
167 "DEBUG:%s node: %i\nsat: %a\ntodo: %a\nround: %i\n"
169 (T.preorder tree node)
170 StateSet.print config.sat
171 StateSet.print config.todo
174 let get_form cache2 auto tag q =
176 Cache.N2.find cache2 (tag.QName.id :> int) (q :> int)
178 if phi == dummy_form then
179 let phi = Ata.get_form auto tag q in
183 (tag.QName.id :> int)
188 type trivalent = False | True | Unknown
189 let of_bool = function false -> False | true -> True
194 | Unknown -> if t2 == True then True else Unknown
200 | Unknown -> if t2 == False then False else Unknown
202 (* Define as macros to get lazyness *)
208 | Unknown -> if (t2) == True then True else Unknown
215 | Unknown -> if (t2) == False then False else Unknown
218 let eval_form phi fcs nss ps ss summary =
221 begin match Formula.expr phi with
222 | Boolean.False -> False
223 | Boolean.True -> True
224 | Boolean.Atom (a, b) ->
226 let open NodeSummary in
227 match a.Atom.node with
229 let { NodeStatus.node = n_sum; _ } as sum =
232 | `Next_sibling -> nss
233 | `Parent | `Previous_sibling -> ps
236 if sum == dummy_status || StateSet.mem q n_sum.todo then
239 of_bool (b == StateSet.mem q n_sum.sat)
240 | Is_first_child -> of_bool (b == is_left summary)
241 | Is_next_sibling -> of_bool (b == is_right summary)
242 | Is k -> of_bool (b == (k == kind summary))
243 | Has_first_child -> of_bool (b == has_left summary)
244 | Has_next_sibling -> of_bool (b == has_right summary)
246 | Boolean.And(phi1, phi2) -> AND_ (loop phi1, loop phi2)
247 | Boolean.Or (phi1, phi2) -> OR_ (loop phi1, loop phi2)
253 let eval_trans_aux auto cache2 tag fcs nss ps old_status =
256 summary = old_summary } as os_node = old_status.NodeStatus.node
259 StateSet.fold (fun q ((a_sat, a_todo) as acc) ->
261 get_form cache2 auto tag q
263 let v = eval_form phi fcs nss ps old_status old_summary in
265 True -> StateSet.add q a_sat, a_todo
267 | Unknown -> a_sat, StateSet.add q a_todo
268 ) old_todo (old_sat, StateSet.empty)
270 if old_sat != sat || old_todo != todo then
271 NodeStatus.make { os_node with sat; todo }
275 let eval_trans auto cache2 cache5 tag fcs nss ps ss =
276 let rec loop old_status =
278 eval_trans_aux auto cache2 tag fcs nss ps old_status
280 if new_status == old_status then old_status else loop new_status
282 let fcsid = (fcs.NodeStatus.id :> int) in
283 let nssid = (nss.NodeStatus.id :> int) in
284 let psid = (ps.NodeStatus.id :> int) in
285 let ssid = (ss.NodeStatus.id :> int) in
286 let tagid = (tag.QName.id :> int) in
287 let res = Cache.N5.find cache5 tagid ssid fcsid nssid psid in
288 if res != dummy_status then res
289 else let new_status = loop ss in
290 Cache.N5.add cache5 tagid ssid fcsid nssid psid new_status;
296 let tree = run.tree in
297 let auto = run.auto in
298 let status = run.status in
299 let cache2 = run.cache2 in
300 let cache5 = run.cache5 in
301 let unstable = run.unstable in
302 let init_todo = StateSet.diff (Ata.get_states auto) (Ata.get_starting_states auto) in
304 let node_id = T.preorder tree node in
305 if node == T.nil || not (Bitvector.get unstable node_id) then false else begin
306 let parent = T.parent tree node in
307 let fc = T.first_child tree node in
308 let fc_id = T.preorder tree fc in
309 let ns = T.next_sibling tree node in
310 let ns_id = T.preorder tree ns in
311 let tag = T.tag tree node in
312 (* We enter the node from its parent *)
315 let c = unsafe_get_status status node_id in
316 if c == dummy_status then
317 (* first time we visit the node *)
319 { sat = StateSet.empty;
321 summary = NodeSummary.make
322 (node == T.first_child tree parent) (* is_left *)
323 (node == T.next_sibling tree parent) (* is_right *)
324 (fc != T.nil) (* has_left *)
325 (ns != T.nil) (* has_right *)
326 (T.kind tree node) (* kind *)
330 TRACE(html tree node _i config0 "Entering node");
332 (* get the node_statuses for the first child, next sibling and parent *)
333 let ps = unsafe_get_status status (T.preorder tree parent) in
334 let fcs = unsafe_get_status status fc_id in
335 let nss = unsafe_get_status status ns_id in
336 (* evaluate the transitions with all this statuses *)
337 let status1 = eval_trans auto cache2 cache5 tag fcs nss ps status0 in
338 TRACE(html tree node _i config1 "Updating transitions");
340 (* update the cache if the status of the node changed *)
342 if status1 != status0 then status.(node_id) <- status1;
343 (* recursively traverse the first child *)
344 let unstable_left = loop fc in
345 (* here we re-enter the node from its first child,
346 get the new status of the first child *)
347 let fcs1 = unsafe_get_status status fc_id in
348 (* update the status *)
349 let status2 = eval_trans auto cache2 cache5 tag fcs1 nss ps status1 in
351 TRACE(html tree node _i config2 "Updating transitions (after first-child)");
353 if status2 != status1 then status.(node_id) <- status2;
354 let unstable_right = loop ns in
355 let nss1 = unsafe_get_status status ns_id in
356 let status3 = eval_trans auto cache2 cache5 tag fcs1 nss1 ps status2 in
358 TRACE(html tree node _i config3 "Updating transitions (after next-sibling)");
360 if status3 != status2 then status.(node_id) <- status3;
363 (* if either our left or right child is unstable or if we still have transitions
364 pending, the current node is unstable *)
367 || StateSet.empty != status3.NodeStatus.node.todo
369 Bitvector.unsafe_set unstable node_id unstable_self;
370 TRACE((if not unstable_self then
374 Ata.(StateSet.intersect config3.Config.node.sat auto.selection_states)));
378 run.redo <- loop (T.root tree);
379 run.pass <- run.pass + 1
384 let len = Bitvector.length run.unstable in
385 for i = 0 to len - 1 do
386 if not (Bitvector.unsafe_get run.unstable i) then
390 "%i nodes over %i were skipped in iteration %i (%.2f %%), redo is: %b"
391 !count len run.pass (100. *. (float !count /. float len))
395 let eval auto tree node =
396 let len = T.size tree in
397 let run = { config = Array.create len Ata.dummy_config;
398 unstable = Bitvector.create ~init:true len;
405 Ata.reset auto; (* prevents the .cache2 and .cache4 memoization tables from growing too much *)
406 run.redo <- top_down_run auto tree node run;
408 run.pass <- run.pass + 1;
410 at_exit (fun () -> Logger.msg `STATS "%i iterations" run.pass);
411 at_exit (fun () -> stats run);
412 let r = get_results auto tree node run.config in
414 TRACE(Html.gen_trace (module T : Tree.S with type t = T.t) (tree));
419 let get_results run =
420 let cache = run.status in
421 let auto = run.auto in
422 let tree = run.tree in
423 let rec loop node acc =
424 if node == T.nil then acc
426 let acc0 = loop (T.next_sibling tree node) acc in
427 let acc1 = loop (T.first_child tree node) acc0 in
431 cache.(T.preorder tree node).NodeStatus.node.sat
432 (get_selecting_states auto)) then node::acc1
435 loop (T.root tree) []
438 let get_full_results run =
439 let cache = run.status in
440 let auto = run.auto in
441 let tree = run.tree in
442 let res_mapper = Hashtbl.create MED_H_SIZE in
445 (fun q -> Hashtbl.add res_mapper q [])
446 (Ata.get_selecting_states auto)
449 if node != T.nil then
450 let () = loop (T.next_sibling tree node) in
451 let () = loop (T.first_child tree node) in
455 let acc = Hashtbl.find res_mapper q in
456 Hashtbl.replace res_mapper q (node::acc)
459 cache.(T.preorder tree node).NodeStatus.node.sat
462 List.rev (StateSet.fold
463 (fun q acc -> (q, Hashtbl.find res_mapper q)::acc)
464 (Ata.get_selecting_states auto) [])
466 let prepare_run run list =
467 let tree = run.tree in
468 let auto = run.auto in
469 let status = run.status in
470 List.iter (fun node ->
471 let parent = T.parent tree node in
472 let fc = T.first_child tree node in
473 let ns = T.next_sibling tree node in
476 { sat = Ata.get_starting_states auto;
478 StateSet.diff (Ata.get_states auto) (Ata.get_starting_states auto);
479 summary = NodeSummary.make
480 (node == T.first_child tree parent) (* is_left *)
481 (node == T.next_sibling tree parent) (* is_right *)
482 (fc != T.nil) (* has_left *)
483 (ns != T.nil) (* has_right *)
484 (T.kind tree node) (* kind *)
487 let node_id = T.preorder tree node in
488 status.(node_id) <- status0) list
491 let eval full auto tree nodes =
492 let run = make auto tree in
493 prepare_run run nodes;
497 if full then `Full (get_full_results run)
498 else `Normal (get_results run)
501 let full_eval auto tree nodes =
502 match eval true auto tree nodes with
506 let eval auto tree nodes =
507 match eval false auto tree nodes with