4 type jump_kind = [ `TAG of Tag.t | `CONTAINS of string | `NOTHING ]
6 (* Todo : move elsewhere *)
7 external vb : bool -> int = "%identity"
11 include Sigs.T with type t = int
21 external hash : t -> int = "%identity"
22 let print fmt x = Format.fprintf fmt "%i" x
23 let dump fmt x = print fmt x
25 if x < 0 then failwith (Printf.sprintf "State: Assertion %i < 0 failed" x)
28 module StateSet = struct
31 Format.pp_print_string ppf "{ ";
32 iter (fun i -> Format.fprintf ppf "%i " i) s;
33 Format.pp_print_string ppf "}";
34 Format.pp_print_flush ppf ()
41 | Or of 'hcons * 'hcons
42 | And of 'hcons * 'hcons
43 | Atom of ([ `Left | `Right | `LLeft | `RRight ]*bool*State.t)
47 st : (StateSet.t*StateSet.t*StateSet.t)*(StateSet.t*StateSet.t*StateSet.t);
48 size: int; (* Todo check if this is needed *)
51 external hash_const_variant : [> ] -> int = "%identity"
52 module rec HNode : Hcons.S with type data = Node.t = Hcons.Make (Node)
53 and Node : Hashtbl.HashedType with type t = HNode.t node =
56 let equal x y = x.size == y.size &&
57 match x.pos,y.pos with
60 | Or(xf1,xf2),Or(yf1,yf2)
61 | And(xf1,xf2),And(yf1,yf2) -> (HNode.equal xf1 yf1) && (HNode.equal xf2 yf2)
62 | Atom(d1,p1,s1), Atom(d2,p2,s2) -> d1 == d2 && (p1==p2) && s1 == s2
68 | Or (f1,f2) -> HASHINT3(PRIME2,HNode.uid f1,HNode.uid f2)
69 | And (f1,f2) -> HASHINT3(PRIME3,HNode.uid f1,HNode.uid f2)
70 | Atom(d,p,s) -> HASHINT4(PRIME4,hash_const_variant d,vb p,s)
76 let equal = HNode.equal
77 let expr f = (HNode.node f).pos
78 let st f = (HNode.node f ).st
79 let size f = (HNode.node f).size
88 let rec print ?(parent=false) ppf f =
89 if parent then Format.fprintf ppf "(";
90 let _ = match expr f with
91 | True -> Format.fprintf ppf "T"
92 | False -> Format.fprintf ppf "F"
94 print ~parent:(prio f > prio f1) ppf f1;
95 Format.fprintf ppf " ∧ ";
96 print ~parent:(prio f > prio f2) ppf f2;
99 Format.fprintf ppf " ∨ ";
101 | Atom(dir,b,s) -> Format.fprintf ppf "%s%s[%i]"
102 (if b then "" else "¬")
109 if parent then Format.fprintf ppf ")"
111 let print ppf f = print ~parent:false ppf f
113 let is_true f = (expr f) == True
114 let is_false f = (expr f) == False
117 let cons pos neg s1 s2 size1 size2 =
118 let nnode = HNode.make { pos = neg; neg = (Obj.magic 0); st = s2; size = size2 } in
119 let pnode = HNode.make { pos = pos; neg = nnode ; st = s1; size = size1 }
121 (HNode.node nnode).neg <- pnode; (* works because the neg field isn't taken into
122 account for hashing ! *)
125 let empty_triple = StateSet.empty,StateSet.empty,StateSet.empty
126 let empty_hex = empty_triple,empty_triple
127 let true_,false_ = cons True False empty_hex empty_hex 0 0
129 let si = StateSet.singleton s in
130 let ss = match d with
131 | `Left -> (si,StateSet.empty,si),empty_triple
132 | `Right -> empty_triple,(si,StateSet.empty,si)
133 | `LLeft -> (StateSet.empty,si,si),empty_triple
134 | `RRight -> empty_triple,(StateSet.empty,si,si)
135 in fst (cons (Atom(d,p,s)) (Atom(d,not p,s)) ss ss 1 1)
137 let not_ f = (HNode.node f).neg
138 let union_hex ((l1,ll1,lll1),(r1,rr1,rrr1)) ((l2,ll2,lll2),(r2,rr2,rrr2)) =
139 (StateSet.mem_union l1 l2 ,StateSet.mem_union ll1 ll2,StateSet.mem_union lll1 lll2),
140 (StateSet.mem_union r1 r2 ,StateSet.mem_union rr1 rr2,StateSet.mem_union rrr1 rrr2)
142 let merge_states f1 f2 =
144 union_hex (st f1) (st f2)
146 union_hex (st (not_ f1)) (st (not_ f2))
150 let order f1 f2 = if uid f1 < uid f2 then f2,f1 else f1,f2
153 (* Tautologies: x|x, x|not(x) *)
155 if equal f1 f2 then f1 else
156 if equal f1 (not_ f2) then true_ else
159 if is_true f1 || is_true f2 then true_ else
160 if is_false f1 && is_false f2 then false_ else
161 if is_false f1 then f2 else
162 if is_false f2 then f1 else
164 (* commutativity of | *)
166 let f1,f2 = order f1 f2 in
167 let psize = (size f1) + (size f2) in
168 let nsize = (size (not_ f1)) + (size (not_ f2)) in
169 let sp,sn = merge_states f1 f2 in
170 fst (cons (Or(f1,f2)) (And(not_ f1,not_ f2)) sp sn psize nsize)
175 (* Tautologies: x&x, x¬(x) *)
177 if equal f1 f2 then f1 else
178 if equal f1 (not_ f2) then false_ else
180 (* simplifications *)
182 if is_true f1 && is_true f2 then true_ else
183 if is_false f1 || is_false f2 then false_ else
184 if is_true f1 then f2 else
185 if is_true f2 then f1 else
187 (* commutativity of & *)
189 let f1,f2 = order f1 f2 in
190 let psize = (size f1) + (size f2) in
191 let nsize = (size (not_ f1)) + (size (not_ f2)) in
192 let sp,sn = merge_states f1 f2 in
193 fst (cons (And(f1,f2)) (Or(not_ f1,not_ f2)) sp sn psize nsize)
194 module Infix = struct
195 let ( +| ) f1 f2 = or_ f1 f2
196 let ( *& ) f1 f2 = and_ f1 f2
197 let ( *+ ) d s = atom_ d true s
198 let ( *- ) d s = atom_ d false s
202 module Transition = struct
204 type node = State.t*bool*Formula.t*bool
205 include Hcons.Make(struct
207 let hash (s,m,f,b) = HASHINT4(s,Formula.uid f,vb m,vb b)
208 let equal (s,b,f,m) (s',b',f',m') =
209 s == s' && b==b' && m==m' && Formula.equal f f'
212 let print ppf f = let (st,mark,form,b) = node f in
213 Format.fprintf ppf "%i %s" st (if mark then "⇒" else "→");
214 Formula.print ppf form;
215 Format.fprintf ppf "%s%!" (if b then " (b)" else "")
218 module Infix = struct
220 let ( >< ) state (l,mark) = state,(l,mark,false)
221 let ( ><@ ) state (l,mark) = state,(l,mark,true)
222 let ( >=> ) (state,(label,mark,bur)) form = (state,label,(make (state,mark,form,bur)))
227 module TransTable = Hashtbl
229 module Formlist = struct
230 include Hlist.Make(Transition)
232 let make _ = failwith "make"
234 iter (fun t -> Transition.print ppf t; Format.pp_print_newline ppf ()) fl
240 mutable states : Ptset.Int.t;
242 starstate : Ptset.Int.t option;
243 (* Transitions of the Alternating automaton *)
244 trans : (State.t,(TagSet.t*Transition.t) list) Hashtbl.t;
245 query_string: string;
250 Format.fprintf ppf "Automaton (%i) :\n" a.id;
251 Format.fprintf ppf "States : "; StateSet.print ppf a.states;
252 Format.fprintf ppf "\nInitial states : "; StateSet.print ppf a.init;
253 Format.fprintf ppf "\nAlternating transitions :\n";
254 let l = Hashtbl.fold (fun k t acc ->
255 (List.map (fun (ts,tr) -> (ts,k),Transition.node tr) t) @ acc) a.trans [] in
256 let l = List.sort (fun ((tsx,x),_) ((tsy,y),_) ->
257 if y-x == 0 then TagSet.compare tsy tsx else y-x) l in
258 let maxh,maxt,l_print =
260 fun (maxh,maxt,l) ((ts,q),(_,b,f,_)) ->
262 if TagSet.is_finite ts
263 then "{" ^ (TagSet.fold (fun t a -> a ^ " '" ^ (Tag.to_string t)^"'") ts "") ^" }"
264 else let cts = TagSet.neg ts in
265 if TagSet.is_empty cts then "*" else
266 (TagSet.fold (fun t a -> a ^ " " ^ (Tag.to_string t)) cts "*\\{"
269 let s = Printf.sprintf "(%s,%i)" s q in
271 Formula.print Format.str_formatter f;
272 Format.flush_str_formatter()
274 (max (String.length s) maxh, max (String.length s_frm) maxt,
275 (s,(if b then "⇒" else "→"),s_frm)::l)) (0,0,[]) l
277 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_');
278 List.iter (fun (s,m,f) -> let s = s ^ (String.make (maxh-(String.length s)) ' ') in
279 Format.fprintf ppf "%s %s %s\n" s m f) l_print;
280 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_')
283 module FormTable = Hashtbl.Make(struct
284 type t = Formula.t*StateSet.t*StateSet.t
285 let equal (f1,s1,t1) (f2,s2,t2) =
286 f1 == f2 && s1 == s2 && t1 == t2
288 HASHINT3(Formula.uid f ,StateSet.uid s,StateSet.uid t)
291 module MemoForm = Memoizer.Make(
297 fun eval (f, ((s1,s2) as sets)) ->
299 | F.True -> true,true,true
300 | F.False -> false,false,false
301 | F.Atom((`Left|`LLeft),b,q) ->
302 if b == (StateSet.mem q s1)
303 then (true,true,false)
304 else false,false,false
306 if b == (StateSet.mem q s2)
307 then (true,false,true)
308 else false,false,false
310 let b1,rl1,rr1 = eval (f1,sets)
312 if b1 && rl1 && rr1 then (true,true,true) else
313 let b2,rl2,rr2 = eval (f2,sets) in
314 let rl1,rr1 = if b1 then rl1,rr1 else false,false
315 and rl2,rr2 = if b2 then rl2,rr2 else false,false
316 in (b1 || b2, rl1||rl2,rr1||rr2)
319 let b1,rl1,rr1 = eval (f1,sets) in
320 if b1 && rl1 && rr1 then (true,true,true) else
322 let b2,rl2,rr2 = eval (f2,sets) in
323 if b2 then (true,rl1||rl2,rr1||rr2) else (false,false,false)
324 else (false,false,false)
331 let h_f = FormTable.create BIG_H_SIZE in
335 | F.True -> true,true,true
336 | F.False -> false,false,false
337 | F.Atom((`Left|`LLeft),b,q) ->
338 if b == (StateSet.mem q s1)
339 then (true,true,false)
340 else false,false,false
342 if b == (StateSet.mem q s2)
343 then (true,false,true)
344 else false,false,false
346 try FormTable.find h_f (f,s1,s2)
347 with Not_found -> let r =
350 let b1,rl1,rr1 = loop f1
352 if b1 && rl1 && rr1 then (true,true,true) else
353 let b2,rl2,rr2 = loop f2 in
354 let rl1,rr1 = if b1 then rl1,rr1 else false,false
355 and rl2,rr2 = if b2 then rl2,rr2 else false,false
356 in (b1 || b2, rl1||rl2,rr1||rr2)
359 let b1,rl1,rr1 = loop f1 in
360 if b1 && rl1 && rr1 then (true,true,true) else
362 let b2,rl2,rr2 = loop f2 in
363 if b2 then (true,rl1||rl2,rr1||rr2) else (false,false,false)
364 else (false,false,false)
366 in FormTable.add h_f (f,s1,s2) r;r
370 module FTable = Hashtbl.Make( struct
371 type t = Formlist.t*StateSet.t*StateSet.t
372 let equal (f1,s1,t1) (f2,s2,t2) =
373 f1 == f2 && s1 == s2 && t1 == t2;;
374 let hash (f,s,t) = HASHINT3(Formlist.uid f ,StateSet.uid s,StateSet.uid t);;
378 module MemoFormlist = Memoizer.Make(FTable)
381 let eval_formlist = MemoFormlist.make_rec (
382 fun eval (fl,((s1,s2)as sets)) ->
383 match Formlist.node fl with
384 | Formlist.Nil -> StateSet.empty,false,false,false,false
385 | Formlist.Cons(f,fll) ->
386 let q,mark,f,_ = Transition.node f in
387 let b,b1,b2 = eval_form_bool f s1 s2 in
388 let s,b',b1',b2',amark = eval (fll,sets) in
389 if b then (StateSet.add q s, b, b1'||b1,b2'||b2,mark||amark)
390 else s,b',b1',b2',amark )
394 let h_f = FTable.create BIG_H_SIZE
396 let eval_formlist s1 s2 fl =
399 FTable.find h_f (fl,s1,s2)
402 match Formlist.node fl with
403 | Formlist.Cons(f,fll) ->
404 let q,mark,f,_ = Transition.node f in
405 let b,b1,b2 = eval_form_bool f s1 s2 in
406 let s,b',b1',b2',amark = loop fll in
407 let r = if b then (StateSet.add q s, b, b1'||b1,b2'||b2,mark||amark)
408 else s,b',b1',b2',amark
409 in FTable.add h_f (fl,s1,s2) r;r
410 | Formlist.Nil -> StateSet.empty,false,false,false,false
413 let tags_of_state a q =
416 if p == q then List.fold_left
418 let _,_,_,aux = Transition.node t in
420 TagSet.cup ts acc) acc l
422 else acc) a.trans TagSet.empty
427 let ts = Ptset.Int.fold (fun q acc -> TagSet.cup acc (tags_of_state a q)) qs TagSet.empty
429 if TagSet.is_finite ts
430 then `Positive(TagSet.positive ts)
431 else `Negative(TagSet.negative ts)
435 | `Positive s -> let r = Ptset.Int.inter a s in (r,Ptset.Int.mem Tag.pcdata r, true)
436 | `Negative s -> let r = Ptset.Int.diff a s in (r, Ptset.Int.mem Tag.pcdata r, false)
438 let mk_nil_ctx x _ = Tree.mk_nil x
439 let next_sibling_ctx x _ = Tree.next_sibling x
443 module type ResultSet =
447 val cons : Tree.t -> t -> t
448 val concat : t -> t -> t
449 val iter : (Tree.t -> unit) -> t -> unit
450 val fold : (Tree.t -> 'a -> 'a) -> t -> 'a -> 'a
451 val map : (Tree.t -> Tree.t) -> t -> t
452 val length : t -> int
455 module Integer : ResultSet =
460 let concat x y = x + y
461 let iter _ _ = failwith "iter not implemented"
462 let fold _ _ _ = failwith "fold not implemented"
463 let map _ _ = failwith "map not implemented"
467 module IdSet : ResultSet =
470 | Cons of Tree.t * node
471 | Concat of node*node
473 and t = { node : node;
476 let empty = { node = Nil; length = 0 }
478 let cons e t = { node = Cons(e,t.node); length = t.length+1 }
479 let concat t1 t2 = { node = Concat(t1.node,t2.node); length = t1.length+t2.length }
480 let append e t = { node = Concat(t.node,Cons(e,Nil)); length = t.length+1 }
483 let rec loop acc t = match t with
485 | Cons (e,t) -> loop (f e acc) t
486 | Concat (t1,t2) -> loop (loop acc t1) t2
490 let length l = l.length
494 let rec loop = function
496 | Cons (e,t) -> f e; loop t
497 | Concat(t1,t2) -> loop t1;loop t2
501 let rec loop = function
503 | Cons(e,t) -> Cons(f e, loop t)
504 | Concat(t1,t2) -> Concat(loop t1,loop t2)
506 { l with node = loop l.node }
511 module Run (RS : ResultSet) =
514 module SList = struct
515 include Hlist.Make (StateSet)
517 let make _ = failwith "make"
524 module IntSet = Set.Make(struct type t = int let compare = (-) end)
525 INCLUDE "html_trace.ml"
529 let mk_fun f s = D_IGNORE_(register_funname f s,f)
530 let mk_app_fun f arg s = let g = f arg in
531 D_IGNORE_(register_funname g ((get_funname f) ^ " " ^ s), g)
533 let string_of_ts tags = (Ptset.Int.fold (fun t a -> a ^ " " ^ (Tag.to_string t) ) tags "{")^ " }"
536 let choose_jump tagset qtags1 qtagsn a f_nil f_text f_t1 f_s1 f_tn f_sn f_notext =
537 let tags1,hastext1,fin1 = inter_text tagset (tags a qtags1) in
538 let tagsn,hastextn,finn = inter_text tagset (tags a qtagsn) in
539 if (hastext1||hastextn) then (`ANY,f_text) (* jumping to text nodes doesn't work really well *)
540 else if (Ptset.Int.is_empty tags1) && (Ptset.Int.is_empty tagsn) then (`NIL,f_nil)
541 else if (Ptset.Int.is_empty tagsn) then
542 if (Ptset.Int.is_singleton tags1)
543 then (* TaggedChild/Sibling *)
544 let tag = (Ptset.Int.choose tags1) in (`TAG(tag),mk_app_fun f_t1 tag (Tag.to_string tag))
545 else (* SelectChild/Sibling *)
546 (`ANY,mk_app_fun f_s1 tags1 (string_of_ts tags1))
547 else if (Ptset.Int.is_empty tags1) then
548 if (Ptset.Int.is_singleton tagsn)
549 then (* TaggedDesc/Following *)
550 let tag = (Ptset.Int.choose tagsn) in (`TAG(tag),mk_app_fun f_tn tag (Tag.to_string tag))
551 else (* SelectDesc/Following *)
552 (`ANY,mk_app_fun f_sn tagsn (string_of_ts tagsn))
555 let choose_jump_down a b c d =
557 (mk_fun (Tree.mk_nil) "Tree.mk_nil")
558 (mk_fun (Tree.text_below) "Tree.text_below")
559 (mk_fun (fun _ -> Tree.node_child) "[TaggedChild]Tree.node_child") (* !! no tagged_child in Tree.ml *)
560 (mk_fun (fun _ -> Tree.node_child) "[SelectChild]Tree.node_child") (* !! no select_child in Tree.ml *)
561 (mk_fun (Tree.tagged_desc) "Tree.tagged_desc")
562 (mk_fun (fun _ -> Tree.node_child ) "[SelectDesc]Tree.node_child") (* !! no select_desc *)
563 (mk_fun (Tree.node_child) "Tree.node_child")
565 let choose_jump_next a b c d =
567 (mk_fun (fun t _ -> Tree.mk_nil t) "Tree.mk_nil2")
568 (mk_fun (Tree.text_next) "Tree.text_next")
569 (mk_fun (fun _ -> Tree.node_sibling_ctx) "[TaggedSibling]Tree.node_sibling_ctx")(* !! no tagged_sibling in Tree.ml *)
570 (mk_fun (fun _ -> Tree.node_sibling_ctx) "[SelectSibling]Tree.node_sibling_ctx")(* !! no select_sibling in Tree.ml *)
571 (mk_fun (Tree.tagged_foll_ctx) "Tree.tagged_foll_ctx")
572 (mk_fun (fun _ -> Tree.node_sibling_ctx) "[SelectFoll]Tree.node_sibling_ctx")(* !! no select_foll *)
573 (mk_fun (Tree.node_sibling_ctx) "Tree.node_sibling_ctx")
578 type t = Tag.t*SList.t
579 let equal (t1,s1) (t2,s2) = t1 == t2 && s1 == s2
580 let hash (t,s) = HASHINT2(t,SList.uid s)
583 module CachedTransTable = Hashtbl.Make(SetTagKey)
584 let td_trans = CachedTransTable.create 4093
586 let merge rb rb1 rb2 mark t res1 res2 =
589 let res1 = if rb1 then res1 else RS.empty
590 and res2 = if rb2 then res2 else RS.empty
592 if mark then RS.cons t (RS.concat res1 res2)
593 else RS.concat res1 res2
597 let rec loop acc = function 0 -> acc
598 | n -> loop (SList.cons StateSet.empty acc) (n-1)
602 let top_down ?(noright=false) a t slist ctx slot_size =
603 let pempty = empty_size slot_size in
604 (* evaluation starts from the right so we put sl1,res1 at the end *)
605 let eval_fold2_slist fll t (sl2,res2) (sl1,res1) =
606 let res = Array.copy res1 in
607 let rec fold l1 l2 fll i aq =
608 match SList.node l1,SList.node l2, fll with
609 | SList.Cons(s1,ll1),
612 let r',rb,rb1,rb2,mark = eval_formlist s1 s2 fl in
613 let _ = res.(i) <- merge rb rb1 rb2 mark t res1.(i) res2.(i)
615 fold ll1 ll2 fll (i+1) (SList.cons r' aq)
617 | SList.Nil, SList.Nil,[] -> aq,res
620 fold sl1 sl2 fll 0 SList.nil
622 let null_result() = (pempty,Array.make slot_size RS.empty) in
624 let rec loop t slist ctx =
625 if Tree.is_nil t then null_result() else get_trans t slist (Tree.tag t) ctx
627 and loop_tag tag t slist ctx =
628 if Tree.is_nil t then null_result() else get_trans t slist tag ctx
629 and loop_no_right t slist ctx =
630 if Tree.is_nil t then null_result() else get_trans ~noright:true t slist (Tree.tag t) ctx
631 and get_trans ?(noright=false) t slist tag ctx =
634 CachedTransTable.find td_trans (tag,slist)
637 let fl_list,llist,rlist,ca,da,sa,fa =
639 (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
640 let fl,ll,rr,ca,da,sa,fa =
644 (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
646 if (TagSet.mem tag ts)
648 let _,_,f,_ = Transition.node t in
649 let (child,desc,below),(sibl,foll,after) = Formula.st f in
650 (Formlist.cons t fl_acc,
651 StateSet.union ll_acc below,
652 StateSet.union rl_acc after,
653 StateSet.union child c_acc,
654 StateSet.union desc d_acc,
655 StateSet.union sibl s_acc,
656 StateSet.union foll f_acc)
658 try Hashtbl.find a.trans q
660 Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
664 ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
665 in fl::fll_acc, (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
666 slist ([],SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
668 (* Logic to chose the first and next function *)
669 let tags_below,tags_after = Tree.tags t tag in
670 let f_kind,first = choose_jump_down tags_below ca da a
671 and n_kind,next = if noright then (`NIL, fun t _ -> Tree.mk_nil t )
672 else choose_jump_next tags_after sa fa a in
673 let empty_res = null_result() in
675 match f_kind,n_kind with
676 | `NIL,`NIL -> (fun _ _ -> null_result())
680 (fun t _ -> eval_fold2_slist fl_list t empty_res
681 (loop_tag tag (first t) llist t))
683 (fun t _ -> eval_fold2_slist fl_list t empty_res
684 (loop (first t) llist t))
690 (fun t ctx -> eval_fold2_slist fl_list t
691 (loop_tag tag (next t ctx) rlist ctx) empty_res)
694 (fun t ctx -> eval_fold2_slist fl_list t
695 (loop (next t ctx) rlist ctx) empty_res)
699 | `TAG(tag1),`TAG(tag2) ->
700 (fun t ctx -> eval_fold2_slist fl_list t
701 (loop (next t ctx) rlist ctx)
702 (loop (first t) llist t))
706 eval_fold2_slist fl_list t
707 (loop (next t ctx) rlist ctx)
708 (loop_tag tag (first t) llist t))
711 eval_fold2_slist fl_list t
712 (loop_tag tag (next t ctx) rlist ctx)
713 (loop (first t) llist t) )
716 eval_fold2_slist fl_list t
717 (loop (next t ctx) rlist ctx)
718 (loop (first t) llist t) )
721 (CachedTransTable.add td_trans (tag,slist) cont;cont)
724 (if noright then loop_no_right else loop) t slist ctx
727 let run_top_down a t =
728 let init = SList.cons a.init SList.nil in
729 let _,res = top_down a t init t 1
732 output_trace a t "trace.html"
733 (RS.fold (fun t a -> IntSet.add (Tree.id t) a) res.(0) IntSet.empty),
737 module Configuration =
739 module Ptss = Set.Make(StateSet)
740 module IMap = Map.Make(StateSet)
741 type t = { hash : int;
743 results : RS.t IMap.t }
744 let empty = { hash = 0;
746 results = IMap.empty;
748 let is_empty c = Ptss.is_empty c.sets
750 if Ptss.mem s c.sets then
751 { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
753 { hash = HASHINT2(c.hash,Ptset.Int.uid s);
754 sets = Ptss.add s c.sets;
755 results = IMap.add s r c.results
758 let pr fmt c = Format.fprintf fmt "{";
759 Ptss.iter (fun s -> StateSet.print fmt s;
760 Format.fprintf fmt " ") c.sets;
761 Format.fprintf fmt "}\n%!";
762 IMap.iter (fun k d ->
763 StateSet.print fmt k;
764 Format.fprintf fmt "-> %i\n" (RS.length d)) c.results;
765 Format.fprintf fmt "\n%!"
768 let acc1 = IMap.fold (fun s r acc ->
771 RS.concat r (IMap.find s acc)
773 | Not_found -> r) acc) c1.results IMap.empty
776 IMap.fold (fun s r acc ->
779 RS.concat r (IMap.find s acc)
781 | Not_found -> r) acc) c2.results acc1
785 (fun s (ah,ass) -> (HASHINT2(ah,Ptset.Int.uid s),
787 (Ptss.union c1.sets c2.sets) (0,Ptss.empty)
795 let h_fold = Hashtbl.create 511
797 let fold_f_conf t slist fl_list conf dir=
798 let rec loop sl fl acc =
799 match SList.node sl,fl with
801 |SList.Cons(s,sll), formlist::fll ->
802 let r',rb,rb1,rb2,mark =
803 let key = SList.hash sl,Formlist.hash formlist,dir in
805 Hashtbl.find h_fold key
807 Not_found -> let res =
808 if dir then eval_formlist s Ptset.Int.empty formlist
809 else eval_formlist Ptset.Int.empty s formlist
810 in (Hashtbl.add h_fold key res;res)
812 if rb && ((dir&&rb1)|| ((not dir) && rb2))
816 try Configuration.IMap.find s conf.Configuration.results
817 with Not_found -> RS.empty
819 Configuration.add acc r' (if mark then RS.cons t old_r else old_r)
822 else loop sll fll acc
825 loop slist fl_list Configuration.empty
827 let h_trans = Hashtbl.create 4096
829 let get_up_trans slist ptag a tree =
830 let key = (HASHINT2(SList.uid slist,ptag)) in
832 Hashtbl.find h_trans key
836 Hashtbl.fold (fun q l acc ->
837 List.fold_left (fun fl_acc (ts,t) ->
838 if TagSet.mem ptag ts then Formlist.cons t fl_acc
844 let res = SList.fold (fun _ acc -> f_list::acc) slist []
846 (Hashtbl.add h_trans key res;res)
849 let h_tdconf = Hashtbl.create 511
850 let rec bottom_up a tree conf next jump_fun root dotd init accu =
851 if (not dotd) && (Configuration.is_empty conf ) then
856 let below_right = Tree.is_below_right tree next in
858 let accu,rightconf,next_of_next =
859 if below_right then (* jump to the next *)
860 bottom_up a next conf (jump_fun next) jump_fun (Tree.next_sibling tree) true init accu
861 else accu,Configuration.empty,next
865 if below_right then prepare_topdown a tree true
866 else prepare_topdown a tree false
870 (Configuration.merge rightconf sub, next_of_next)
872 if Tree.equal tree root then accu,conf,next
874 let parent = Tree.binary_parent tree in
875 let ptag = Tree.tag parent in
876 let dir = Tree.is_left tree in
877 let slist = Configuration.Ptss.fold (fun e a -> SList.cons e a) conf.Configuration.sets SList.nil in
878 let fl_list = get_up_trans slist ptag a parent in
879 let slist = SList.rev (slist) in
880 let newconf = fold_f_conf parent slist fl_list conf dir in
881 let accu,newconf = Configuration.IMap.fold (fun s res (ar,nc) ->
882 if Ptset.Int.intersect s init then
883 ( RS.concat res ar ,nc)
884 else (ar,Configuration.add nc s res))
885 (newconf.Configuration.results) (accu,Configuration.empty)
888 bottom_up a parent newconf next jump_fun root false init accu
890 and prepare_topdown a t noright =
891 let tag = Tree.tag t in
892 (* pr "Going top down on tree with tag %s = %s "
893 (if Tree.is_nil t then "###" else (Tag.to_string(Tree.tag t))) (Tree.dump_node t); *)
896 Hashtbl.find h_tdconf tag
899 let res = Hashtbl.fold (fun q l acc ->
900 if List.exists (fun (ts,_) -> TagSet.mem tag ts) l
901 then Ptset.Int.add q acc
902 else acc) a.trans Ptset.Int.empty
903 in Hashtbl.add h_tdconf tag res;res
905 (* let _ = pr ", among ";
906 StateSet.print fmt (Ptset.Int.elements r);
909 let r = SList.cons r SList.nil in
910 let set,res = top_down (~noright:noright) a t r t 1 in
911 let set = match SList.node set with
912 | SList.Cons(x,_) ->x
915 (* pr "Result of topdown run is %!";
916 StateSet.print fmt (Ptset.Int.elements set);
917 pr ", number is %i\n%!" (RS.length res.(0)); *)
918 Configuration.add Configuration.empty set res.(0)
922 let run_bottom_up a t k =
923 let trlist = Hashtbl.find a.trans (Ptset.Int.choose a.init)
925 let init = List.fold_left
927 let _,_,f,_ = Transition.node t in
928 let _,_,l = fst ( Formula.st f ) in
929 Ptset.Int.union acc l)
930 Ptset.Int.empty trlist
935 (*Tree.tagged_lowest t tag, fun tree -> Tree.tagged_next tree tag*)
936 (Tree.tagged_desc tag t, fun tree -> Tree.tagged_foll_ctx tag tree t)
937 | `CONTAINS(_) -> (Tree.text_below t,fun tree -> Tree.text_next tree t)
940 let tree2 = jump_fun tree1 in
941 let rec loop tree next acc =
942 (* let _ = pr "\n_________________________\nNew iteration\n" in
943 let _ = pr "Jumping to %s\n%!" (Tree.dump_node tree) in *)
944 let acc,conf,next_of_next = bottom_up a tree
945 Configuration.empty next jump_fun (Tree.root tree) true init acc
947 (* let _ = pr "End of first iteration, conf is:\n%!";
948 Configuration.pr fmt conf
950 let acc = Configuration.IMap.fold
951 ( fun s res acc -> if Ptset.Int.intersect init s
952 then RS.concat res acc else acc) conf.Configuration.results acc
954 if Tree.is_nil next_of_next (*|| Tree.equal next next_of_next *)then
956 else loop next_of_next (jump_fun next_of_next) acc
958 loop tree1 tree2 RS.empty
963 let top_down_count a t = let module RI = Run(Integer) in Integer.length (RI.run_top_down a t)
964 let top_down a t = let module RI = Run(IdSet) in (RI.run_top_down a t)
965 let bottom_up_count a t k = let module RI = Run(Integer) in Integer.length (RI.run_bottom_up a t k)