- struct
-
- module NodeSummary =
- struct
- (* Pack into an integer the result of the is_* and has_ predicates
- for a given node *)
- type t = int
- let dummy = -1
- (*
- ...44443210
- ...4444 -> kind
- 3 -> has_right
- 2 -> has_left
- 1 -> is_right
- 0 -> is_left
- *)
- let is_left (s : t) : bool =
- s land 1 != 0
-
- let is_right (s : t) : bool =
- s land 0b10 != 0
-
- let has_left (s : t) : bool =
- s land 0b100 != 0
-
- let has_right (s : t) : bool =
- s land 0b1000 != 0
-
- let kind (s : t) : Tree.NodeKind.t =
- Obj.magic (s lsr 4)
-
- let make is_left is_right has_left has_right kind =
- (int_of_bool is_left) lor
- ((int_of_bool is_right) lsl 1) lor
- ((int_of_bool has_left) lsl 2) lor
- ((int_of_bool has_right) lsl 3) lor
- ((Obj.magic kind) lsl 4)
- end
-
- let dummy_set = StateSet.singleton State.dummy
- open Bigarray
- type run = {
- tree : T.t ;
- (* The argument of the run *)
- auto : Ata.t;
- (* The automaton to be run *)
- sat: StateSet.t array;
- (* A mapping from node preorders to states satisfied at that node *)
- mutable pass : int;
- (* Number of run we have performed *)
- mutable fetch_trans_cache : Ata.Formula.t Cache.N2.t;
- (* A cache from states * label to list of transitions *)
- mutable td_cache : StateSet.t Cache.N6.t;
- mutable bu_cache : StateSet.t Cache.N6.t;
- (* Two 6-way caches used during the top-down and bottom-up phase
- label * self-set * fc-set * ns-set * parent-set * node-shape -> self-set
- *)
- node_summaries: (int, int16_unsigned_elt, c_layout) Array1.t;
- }
-
-
- let dummy_form = Ata.Formula.stay State.dummy
-
- let make auto tree =
- let len = T.size tree in
- {
- tree = tree;
- auto = auto;
- sat = Array.create len StateSet.empty;
- pass = 0;
- fetch_trans_cache = Cache.N2.create dummy_form;
- td_cache = Cache.N6.create dummy_set;
- bu_cache = Cache.N6.create dummy_set;
- node_summaries = let ba = Array1.create int16_unsigned c_layout len in
- Array1.fill ba 0; ba
- }
-
- let get_form fetch_trans_cache auto tag q =
- let phi =
- incr fetch_trans_cache_access;
- Cache.N2.find fetch_trans_cache (tag.QName.id :> int) (q :> int)
- in
- if phi == dummy_form then
- let phi = Ata.get_form auto tag q in
- let () =
- Cache.N2.add
- fetch_trans_cache
- (tag.QName.id :> int)
- (q :> int) phi
- in phi
- else begin
- incr fetch_trans_cache_hit;
- phi
- end
-
-
- let eval_form phi fcs nss ps ss summary =
- let open Ata in
- let rec loop phi =
- begin match Formula.expr phi with
- | Boolean.False -> false
- | Boolean.True -> true
- | Boolean.Atom (a, b) ->
- begin
- let open NodeSummary in
- match a.Atom.node with
- | Move (m, q) ->
- b && StateSet.mem q (
- match m with
- `First_child -> fcs
- | `Next_sibling -> nss
- | `Parent | `Previous_sibling -> ps
- | `Stay -> ss
- )
- | Is_first_child -> b == is_left summary
- | Is_next_sibling -> b == is_right summary
- | Is k -> b == (k == kind summary)
- | Has_first_child -> b == has_left summary
- | Has_next_sibling -> b == has_right summary
- end
- | Boolean.And(phi1, phi2) -> loop phi1 && loop phi2
- | Boolean.Or (phi1, phi2) -> loop phi1 || loop phi2
- end
- in
- loop phi
-
-
- let eval_trans_aux auto fetch_trans_cache tag fcs nss ps sat todo summary =
- StateSet.fold (fun q (a_sat) ->
- let phi =
- get_form fetch_trans_cache auto tag q
- in
- if eval_form phi fcs nss ps a_sat summary then
- StateSet.add q a_sat
- else a_sat
- ) todo sat
-
-
- let rec eval_trans_fix auto fetch_trans_cache tag fcs nss ps sat todo summary =
- let new_sat =
- eval_trans_aux auto fetch_trans_cache tag fcs nss ps sat todo summary
- in
- if new_sat == sat then sat else
- eval_trans_fix auto fetch_trans_cache tag fcs nss ps new_sat todo summary
-
-
- let eval_trans auto fetch_trans_cache eval_cache tag fcs nss ps ss todo summary =
- let fcsid = (fcs.StateSet.id :> int) in
- let nssid = (nss.StateSet.id :> int) in
- let psid = (ps.StateSet.id :> int) in
- let ssid = (ss.StateSet.id :> int) in
- let tagid = (tag.QName.id :> int) in
- let res = Cache.N6.find eval_cache tagid summary ssid fcsid nssid psid in
- incr eval_trans_cache_access;
- if res != dummy_set then begin incr eval_trans_cache_hit; res end
- else let new_sat =
- eval_trans_fix auto fetch_trans_cache tag fcs nss ps ss todo summary
+struct
+
+ let eval_form phi tree node fcs nss pars selfs =
+ let rec loop phi =
+ let open Boolean in
+ match Ata.Formula.expr phi with
+ False -> false
+ | True -> true
+ | Or (phi1, phi2) -> loop phi1 || loop phi2
+ | And (phi1, phi2) -> loop phi1 && loop phi2
+ | Atom (a, b) -> b == Ata.(
+ match Atom.node a with
+ Is_first_child -> let par = T.parent tree node in
+ (T.first_child tree par) == node
+ | Is_next_sibling -> let par = T.parent tree node in
+ (T.next_sibling tree par) == node
+ | Is k -> k == T.kind tree node
+ | Has_first_child -> T.nil != T.first_child tree node
+ | Has_next_sibling -> T.nil != T.next_sibling tree node
+ | Move (m, q) ->
+ let set =
+ match m with
+ `First_child -> fcs
+ | `Next_sibling -> nss
+ | `Parent
+ | `Previous_sibling -> pars
+ | `Stay -> selfs