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 = Ptset.Int
34 | Or of 'hcons * 'hcons
35 | And of 'hcons * 'hcons
36 | Atom of ([ `Left | `Right | `LLeft | `RRight ]*bool*State.t)
41 st : (StateSet.t*StateSet.t*StateSet.t)*(StateSet.t*StateSet.t*StateSet.t);
42 size: int; (* Todo check if this is needed *)
45 external hash_const_variant : [> ] -> int = "%identity"
46 module rec Node : Hcons.S with type data = Data.t = Hcons.Make (Data)
47 and Data : Hashtbl.HashedType with type t = Node.t node =
50 let equal x y = x.size == y.size &&
51 match x.pos,y.pos with
52 | a,b when a == b -> true
53 | Or(xf1,xf2),Or(yf1,yf2)
54 | And(xf1,xf2),And(yf1,yf2) -> (xf1 == yf1) && (xf2 == yf2)
55 | Atom(d1,p1,s1), Atom(d2,p2,s2) -> d1 == d2 && (p1==p2) && s1 == s2
61 | Or (f1,f2) -> HASHINT3(PRIME2,f1.Node.id, f2.Node.id)
62 | And (f1,f2) -> HASHINT3(PRIME3,f1.Node.id,f2.Node.id)
63 | Atom(d,p,s) -> HASHINT4(PRIME4,hash_const_variant d,vb p,s)
67 let hash x = x.Node.key
69 let equal = Node.equal
70 let expr f = f.Node.node.pos
71 let st f = f.Node.node.st
72 let size f = f.Node.node.size
81 let rec print ?(parent=false) ppf f =
82 if parent then Format.fprintf ppf "(";
83 let _ = match expr f with
84 | True -> Format.fprintf ppf "T"
85 | False -> Format.fprintf ppf "F"
87 print ~parent:(prio f > prio f1) ppf f1;
88 Format.fprintf ppf " ∧ ";
89 print ~parent:(prio f > prio f2) ppf f2;
92 Format.fprintf ppf " ∨ ";
94 | Atom(dir,b,s) -> Format.fprintf ppf "%s%s[%i]"
95 (if b then "" else "¬")
102 if parent then Format.fprintf ppf ")"
104 let print ppf f = print ~parent:false ppf f
106 let is_true f = (expr f) == True
107 let is_false f = (expr f) == False
110 let cons pos neg s1 s2 size1 size2 =
111 let nnode = Node.make { pos = neg; neg = (Obj.magic 0); st = s2; size = size2 } in
112 let pnode = Node.make { pos = pos; neg = nnode ; st = s1; size = size1 }
114 (Node.node nnode).neg <- pnode; (* works because the neg field isn't taken into
115 account for hashing ! *)
118 let empty_triple = StateSet.empty,StateSet.empty,StateSet.empty
119 let empty_hex = empty_triple,empty_triple
120 let true_,false_ = cons True False empty_hex empty_hex 0 0
122 let si = StateSet.singleton s in
123 let ss = match d with
124 | `Left -> (si,StateSet.empty,si),empty_triple
125 | `Right -> empty_triple,(si,StateSet.empty,si)
126 | `LLeft -> (StateSet.empty,si,si),empty_triple
127 | `RRight -> empty_triple,(StateSet.empty,si,si)
128 in fst (cons (Atom(d,p,s)) (Atom(d,not p,s)) ss ss 1 1)
130 let not_ f = f.Node.node.neg
131 let union_hex ((l1,ll1,lll1),(r1,rr1,rrr1)) ((l2,ll2,lll2),(r2,rr2,rrr2)) =
132 (StateSet.mem_union l1 l2 ,StateSet.mem_union ll1 ll2,StateSet.mem_union lll1 lll2),
133 (StateSet.mem_union r1 r2 ,StateSet.mem_union rr1 rr2,StateSet.mem_union rrr1 rrr2)
135 let merge_states f1 f2 =
137 union_hex (st f1) (st f2)
139 union_hex (st (not_ f1)) (st (not_ f2))
143 let order f1 f2 = if uid f1 < uid f2 then f2,f1 else f1,f2
146 (* Tautologies: x|x, x|not(x) *)
148 if equal f1 f2 then f1 else
149 if equal f1 (not_ f2) then true_ else
152 if is_true f1 || is_true f2 then true_ else
153 if is_false f1 && is_false f2 then false_ else
154 if is_false f1 then f2 else
155 if is_false f2 then f1 else
157 (* commutativity of | *)
159 let f1,f2 = order f1 f2 in
160 let psize = (size f1) + (size f2) in
161 let nsize = (size (not_ f1)) + (size (not_ f2)) in
162 let sp,sn = merge_states f1 f2 in
163 fst (cons (Or(f1,f2)) (And(not_ f1,not_ f2)) sp sn psize nsize)
168 (* Tautologies: x&x, x¬(x) *)
170 if equal f1 f2 then f1 else
171 if equal f1 (not_ f2) then false_ else
173 (* simplifications *)
175 if is_true f1 && is_true f2 then true_ else
176 if is_false f1 || is_false f2 then false_ else
177 if is_true f1 then f2 else
178 if is_true f2 then f1 else
180 (* commutativity of & *)
182 let f1,f2 = order f1 f2 in
183 let psize = (size f1) + (size f2) in
184 let nsize = (size (not_ f1)) + (size (not_ f2)) in
185 let sp,sn = merge_states f1 f2 in
186 fst (cons (And(f1,f2)) (Or(not_ f1,not_ f2)) sp sn psize nsize)
187 module Infix = struct
188 let ( +| ) f1 f2 = or_ f1 f2
189 let ( *& ) f1 f2 = and_ f1 f2
190 let ( *+ ) d s = atom_ d true s
191 let ( *- ) d s = atom_ d false s
195 module Transition = struct
197 type node = State.t*bool*Formula.t*bool
198 include Hcons.Make(struct
200 let hash (s,m,f,b) = HASHINT4(s,Formula.uid f,vb m,vb b)
201 let equal (s,b,f,m) (s',b',f',m') =
202 s == s' && b==b' && m==m' && Formula.equal f f'
205 let print ppf f = let (st,mark,form,b) = node f in
206 Format.fprintf ppf "%i %s" st (if mark then "⇒" else "→");
207 Formula.print ppf form;
208 Format.fprintf ppf "%s%!" (if b then " (b)" else "")
211 module Infix = struct
213 let ( >< ) state (l,mark) = state,(l,mark,false)
214 let ( ><@ ) state (l,mark) = state,(l,mark,true)
215 let ( >=> ) (state,(label,mark,bur)) form = (state,label,(make (state,mark,form,bur)))
220 module TransTable = Hashtbl
222 module Formlist = struct
223 include Hlist.Make(Transition)
225 iter (fun t -> Transition.print ppf t; Format.pp_print_newline ppf ()) fl
228 module Formlistlist =
230 include Hlist.Make(Formlist)
232 iter (fun fl -> Formlist.print ppf fl; Format.pp_print_newline ppf ())fll
237 mutable states : Ptset.Int.t;
239 starstate : Ptset.Int.t option;
240 (* Transitions of the Alternating automaton *)
241 trans : (State.t,(TagSet.t*Transition.t) list) Hashtbl.t;
242 query_string: string;
247 Format.fprintf ppf "Automaton (%i) :\n" a.id;
248 Format.fprintf ppf "States : "; StateSet.print ppf a.states;
249 Format.fprintf ppf "\nInitial states : "; StateSet.print ppf a.init;
250 Format.fprintf ppf "\nAlternating transitions :\n";
251 let l = Hashtbl.fold (fun k t acc ->
252 (List.map (fun (ts,tr) -> (ts,k),Transition.node tr) t) @ acc) a.trans [] in
253 let l = List.sort (fun ((tsx,x),_) ((tsy,y),_) ->
254 if y-x == 0 then TagSet.compare tsy tsx else y-x) l in
255 let maxh,maxt,l_print =
257 fun (maxh,maxt,l) ((ts,q),(_,b,f,_)) ->
259 if TagSet.is_finite ts
260 then "{" ^ (TagSet.fold (fun t a -> a ^ " '" ^ (Tag.to_string t)^"'") ts "") ^" }"
261 else let cts = TagSet.neg ts in
262 if TagSet.is_empty cts then "*" else
263 (TagSet.fold (fun t a -> a ^ " " ^ (Tag.to_string t)) cts "*\\{"
266 let s = Printf.sprintf "(%s,%i)" s q in
268 Formula.print Format.str_formatter f;
269 Format.flush_str_formatter()
271 (max (String.length s) maxh, max (String.length s_frm) maxt,
272 (s,(if b then "⇒" else "→"),s_frm)::l)) (0,0,[]) l
274 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_');
275 List.iter (fun (s,m,f) -> let s = s ^ (String.make (maxh-(String.length s)) ' ') in
276 Format.fprintf ppf "%s %s %s\n" s m f) l_print;
277 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_')
280 module FormTable = Hashtbl.Make(struct
281 type t = Formula.t*StateSet.t*StateSet.t
282 let equal (f1,s1,t1) (f2,s2,t2) =
283 f1 == f2 && s1 == s2 && t1 == t2
285 HASHINT3(Formula.uid f ,StateSet.uid s,StateSet.uid t)
290 let h_f = FormTable.create BIG_H_SIZE in
294 | F.True -> true,true,true
295 | F.False -> false,false,false
296 | F.Atom((`Left|`LLeft),b,q) ->
297 if b == (StateSet.mem q s1)
298 then (true,true,false)
299 else false,false,false
301 if b == (StateSet.mem q s2)
302 then (true,false,true)
303 else false,false,false
305 try FormTable.find h_f (f,s1,s2)
306 with Not_found -> let r =
309 let b1,rl1,rr1 = loop f1
311 if b1 && rl1 && rr1 then (true,true,true) else
312 let b2,rl2,rr2 = loop f2 in
313 let rl1,rr1 = if b1 then rl1,rr1 else false,false
314 and rl2,rr2 = if b2 then rl2,rr2 else false,false
315 in (b1 || b2, rl1||rl2,rr1||rr2)
318 let b1,rl1,rr1 = loop f1 in
319 if b1 && rl1 && rr1 then (true,true,true) else
321 let b2,rl2,rr2 = loop f2 in
322 if b2 then (true,rl1||rl2,rr1||rr2) else (false,false,false)
323 else (false,false,false)
325 in FormTable.add h_f (f,s1,s2) r;r
329 module FTable = Hashtbl.Make( struct
330 type t = Formlist.t*StateSet.t*StateSet.t
331 let equal (f1,s1,t1) (f2,s2,t2) =
332 f1 == f2 && s1 == s2 && t1 == t2;;
333 let hash (f,s,t) = HASHINT3(Formlist.uid f ,StateSet.uid s,StateSet.uid t);;
337 let h_f = FTable.create BIG_H_SIZE
339 let eval_formlist s1 s2 fl =
342 FTable.find h_f (fl,s1,s2)
345 match Formlist.node fl with
346 | Formlist.Cons(f,fll) ->
347 let q,mark,f,_ = Transition.node f in
348 let b,b1,b2 = eval_form_bool f s1 s2 in
349 let (s,(b',b1',b2',amark)) as res = loop fll in
350 let r = if b then (StateSet.add q s, (b, b1'||b1,b2'||b2,mark||amark))
352 in FTable.add h_f (fl,s1,s2) r;r
353 | Formlist.Nil -> StateSet.empty,(false,false,false,false)
356 let tags_of_state a q =
359 if p == q then List.fold_left
361 let _,_,_,aux = Transition.node t in
363 TagSet.cup ts acc) acc l
365 else acc) a.trans TagSet.empty
370 let ts = Ptset.Int.fold (fun q acc -> TagSet.cup acc (tags_of_state a q)) qs TagSet.empty
372 if TagSet.is_finite ts
373 then `Positive(TagSet.positive ts)
374 else `Negative(TagSet.negative ts)
378 | `Positive s -> let r = Ptset.Int.inter a s in (r,Ptset.Int.mem Tag.pcdata r, true)
379 | `Negative s -> let r = Ptset.Int.diff a s in (r, Ptset.Int.mem Tag.pcdata r, false)
382 module type ResultSet =
385 type elt = [` Tree] Tree.node
387 val cons : elt -> t -> t
388 val concat : t -> t -> t
389 val iter : ( elt -> unit) -> t -> unit
390 val fold : ( elt -> 'a -> 'a) -> t -> 'a -> 'a
391 val map : ( elt -> elt) -> t -> t
392 val length : t -> int
393 val merge : (bool*bool*bool*bool) -> elt -> t -> t -> t
396 module Integer : ResultSet =
399 type elt = [`Tree] Tree.node
402 let concat x y = x + y
403 let iter _ _ = failwith "iter not implemented"
404 let fold _ _ _ = failwith "fold not implemented"
405 let map _ _ = failwith "map not implemented"
407 let merge (rb,rb1,rb2,mark) t res1 res2 =
409 let res1 = if rb1 then res1 else 0
410 and res2 = if rb2 then res2 else 0
412 if mark then 1+res1+res2
417 module IdSet : ResultSet =
419 type elt = [`Tree] Tree.node
422 | Concat of node*node
424 and t = { node : node;
427 let empty = { node = Nil; length = 0 }
429 let cons e t = { node = Cons(e,t.node); length = t.length+1 }
430 let concat t1 t2 = { node = Concat(t1.node,t2.node); length = t1.length+t2.length }
431 let append e t = { node = Concat(t.node,Cons(e,Nil)); length = t.length+1 }
434 let rec loop acc t = match t with
436 | Cons (e,t) -> loop (f e acc) t
437 | Concat (t1,t2) -> loop (loop acc t1) t2
441 let length l = l.length
445 let rec loop = function
447 | Cons (e,t) -> f e; loop t
448 | Concat(t1,t2) -> loop t1;loop t2
452 let rec loop = function
454 | Cons(e,t) -> Cons(f e, loop t)
455 | Concat(t1,t2) -> Concat(loop t1,loop t2)
457 { l with node = loop l.node }
459 let merge (rb,rb1,rb2,mark) t res1 res2 =
461 let res1 = if rb1 then res1 else empty
462 and res2 = if rb2 then res2 else empty
464 if mark then { node = Cons(t,(Concat(res1.node,res2.node)));
465 length = res1.length + res2.length + 1;}
467 { node = (Concat(res1.node,res2.node));
468 length = res1.length + res2.length ;}
473 module GResult = struct
475 type elt = [` Tree] Tree.node
476 external create_empty : int -> t = "caml_result_set_create"
477 external set : t -> int -> t = "caml_result_set_set"
478 external next : t -> int -> int = "caml_result_set_next"
479 external clear : t -> int -> int -> unit = "caml_result_set_clear"
480 let empty = create_empty 100000000
482 let cons e t = set t (Obj.magic e)
487 else (f (Obj.magic i);loop (next t i))
490 let fold _ _ _ = failwith "noop"
491 let map _ _ = failwith "noop"
492 let length t = let cpt = ref ~-1 in
493 iter (fun _ -> incr cpt) t; !cpt
495 let merge (rb,rb1,rb2,mark) elt t1 t2 =
496 if mark then (set t1 (Obj.magic elt) ; t1) else t1
499 module Run (RS : ResultSet) =
502 module SList = Hlist.Make (StateSet)
508 module IntSet = Set.Make(struct type t = int let compare = (-) end)
509 INCLUDE "html_trace.ml"
512 let mk_fun f s = D_IGNORE_(register_funname f s,f)
513 let mk_app_fun f arg s = let g = f arg in
514 D_IGNORE_(register_funname g ((get_funname f) ^ " " ^ s), g)
516 let string_of_ts tags = (Ptset.Int.fold (fun t a -> a ^ " " ^ (Tag.to_string t) ) tags "{")^ " }"
522 type jump = [ `LONG | `CLOSE | `NIL ]
523 type t = jump*Ptset.Int.t
525 let merge_jump (j1,l1) (j2,l2) =
527 | _ when j1 = j2 -> (j1,Ptset.Int.union l1 l2)
530 | _,_ -> (`CLOSE, Ptset.Int.union l1 l2)
532 let merge_jump_list = function
533 | [] -> `NIL,Ptset.Int.empty
534 | p::r -> List.fold_left (merge_jump) p r
545 let _,_,_,bur = Transition.node f in
546 if bur then acc else TagSet.cup acc ts)
548 else acc ) a.trans TagSet.empty
551 let is_rec a s access =
553 (fun (_,t) -> let _,_,f,_ = Transition.node t in
554 StateSet.mem s (access f)) (Hashtbl.find a.trans s)
557 let decide a c_label l_label dir_states access =
559 let l = StateSet.fold
561 let s_rec= is_rec a s access in
563 if s_rec then l_label,`LONG
564 else c_label,`CLOSE in
565 let slabels = TagSet.positive ((TagSet.cap (labels a s) tlabels))
567 (if Ptset.Int.is_empty slabels
568 then `NIL,Ptset.Int.empty
569 else jmp,slabels)::l) dir_states []
579 let choose_jump tagset qtags1 qtagsn a f_nil f_t1 f_s1 f_tn f_sn f_notext f_maytext =
580 let tags1,hastext1,fin1 = inter_text tagset (tags a qtags1) in
581 let tagsn,hastextn,finn = inter_text tagset (tags a qtagsn) in
582 (*if (hastext1||hastextn) then (`ANY,f_text) (* jumping to text nodes doesn't work really well *)
584 if (Ptset.Int.is_empty tags1) && (Ptset.Int.is_empty tagsn) then (`NIL,f_nil)
585 else if (Ptset.Int.is_empty tagsn) then
586 if (Ptset.Int.is_singleton tags1)
587 then (* TaggedChild/Sibling *)
588 let tag = (Ptset.Int.choose tags1) in (`TAG(tag),mk_app_fun f_t1 tag (Tag.to_string tag))
589 else (* SelectChild/Sibling *)
590 (`ANY,mk_app_fun f_s1 tags1 (string_of_ts tags1))
591 else if (Ptset.Int.is_empty tags1) then
592 if (Ptset.Int.is_singleton tagsn)
593 then (* TaggedDesc/Following *)
594 let tag = (Ptset.Int.choose tagsn) in (`TAG(tag),mk_app_fun f_tn tag (Tag.to_string tag))
595 else (* SelectDesc/Following *)
596 (`ANY,mk_app_fun f_sn tagsn (string_of_ts tagsn))
597 else if (hastext1||hastextn) then (`ANY,f_maytext)
600 let choose_jump_down tree a b c d =
602 (mk_fun (fun _ -> Tree.nil) "Tree.mk_nil")
603 (mk_fun (Tree.tagged_child tree) "Tree.tagged_child")
604 (mk_fun (Tree.select_child tree) "Tree.select_child")
605 (mk_fun (Tree.tagged_desc tree) "Tree.tagged_desc")
606 (mk_fun (Tree.select_desc tree) "Tree.select_desc")
607 (mk_fun (Tree.first_element tree) "Tree.first_element")
608 (mk_fun (Tree.first_child tree) "Tree.first_child")
610 let choose_jump_next tree a b c d =
612 (mk_fun (fun _ _ -> Tree.nil) "Tree.mk_nil2")
613 (mk_fun (Tree.tagged_sibling_ctx tree) "Tree.tagged_sibling_ctx")
614 (mk_fun (Tree.select_sibling_ctx tree) "Tree.select_sibling_ctx")
615 (mk_fun (Tree.tagged_foll_ctx tree) "Tree.tagged_foll_ctx")
616 (mk_fun (Tree.select_foll_ctx tree) "Tree.select_foll_ctx")
617 (mk_fun (Tree.next_element_ctx tree) "Tree.node_element_ctx")
618 (mk_fun (Tree.next_sibling_ctx tree) "Tree.node_sibling_ctx")
622 type t = Tag.t*SList.t
623 let equal (t1,s1) (t2,s2) = t1 == t2 && s1 == s2
624 let hash (t,s) = HASHINT2(t,s.SList.Node.id)
627 module CachedTransTable = Hashtbl.Make(SetTagKey)
628 let td_trans = CachedTransTable.create 4093
632 let rec loop acc = function 0 -> acc
633 | n -> loop (SList.cons StateSet.empty acc) (n-1)
637 module Fold2Res = Hashtbl.Make(struct
638 type t = Formlistlist.t*SList.t*SList.t
639 let hash (f,s,t) = HASHINT3(f.Formlistlist.Node.id,
642 let equal (a,b,c) (d,e,f) = a==d && b == e && c == f
645 let h_fold2 = Fold2Res.create BIG_H_SIZE
647 let top_down ?(noright=false) a tree t slist ctx slot_size =
648 let pempty = empty_size slot_size in
649 let rempty = Array.make slot_size RS.empty in
650 (* evaluation starts from the right so we put sl1,res1 at the end *)
651 let eval_fold2_slist fll t (sl2,res2) (sl1,res1) =
652 let res = Array.copy rempty in
654 let r,b,btab = Fold2Res.find h_fold2 (fll,sl1,sl2) in
655 if b then for i=0 to slot_size - 1 do
656 res.(i) <- RS.merge btab.(i) t res1.(i) res2.(i);
661 let btab = Array.make slot_size (false,false,false,false) in
662 let rec fold l1 l2 fll i aq ab =
663 match fll.Formlistlist.Node.node,
667 | Formlistlist.Cons(fl,fll),
669 SList.Cons(s2,ll2) ->
670 let r',((b,_,_,_) as flags) = eval_formlist s1 s2 fl in
671 let _ = btab.(i) <- flags
673 fold ll1 ll2 fll (i+1) (SList.cons r' aq) (b||ab)
676 let r,b = fold sl1 sl2 fll 0 SList.nil false in
677 Fold2Res.add h_fold2 (fll,sl1,sl2) (r,b,btab);
678 if b then for i=0 to slot_size - 1 do
679 res.(i) <- RS.merge btab.(i) t res1.(i) res2.(i);
684 let null_result = (pempty,Array.copy rempty) in
685 let rec loop t slist ctx=
686 if t == Tree.nil then null_result else get_trans t slist (Tree.tag tree t) ctx
687 and loop_tag tag t slist ctx =
688 if t == Tree.nil then null_result else get_trans t slist tag ctx
689 and loop_no_right t slist ctx =
690 if t == Tree.nil then null_result else get_trans ~noright:true t slist (Tree.tag tree t) ctx
691 and get_trans ?(noright=false) t slist tag ctx =
694 CachedTransTable.find td_trans (tag,slist)
697 let fl_list,llist,rlist,ca,da,sa,fa =
699 (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
700 let fl,ll,rr,ca,da,sa,fa =
704 (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
706 if (TagSet.mem tag ts)
708 let _,_,f,_ = Transition.node t in
709 let (child,desc,below),(sibl,foll,after) = Formula.st f in
710 (Formlist.cons t fl_acc,
711 StateSet.union ll_acc below,
712 StateSet.union rl_acc after,
713 StateSet.union child c_acc,
714 StateSet.union desc d_acc,
715 StateSet.union sibl s_acc,
716 StateSet.union foll f_acc)
718 try Hashtbl.find a.trans q
720 Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
724 ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
725 in (Formlistlist.cons fl fll_acc), (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
726 slist (Formlistlist.nil,SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
728 (* Logic to chose the first and next function *)
729 let _,tags_below,_,tags_after = Tree.tags tree tag in
730 (* let _ = Printf.eprintf "Tags below %s are : \n" (Tag.to_string tag) in
731 let _ = Ptset.Int.iter (fun i -> Printf.eprintf "%s " (Tag.to_string i)) tags_below in
732 let _ = Printf.eprintf "\n%!" in *)
733 let tags_below = Ptset.Int.remove tag tags_below in
734 let f_kind,first = choose_jump_down tree tags_below ca da a
735 and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
736 else choose_jump_next tree tags_after sa fa a in
737 let empty_res = null_result in
739 match f_kind,n_kind with
741 (fun t _ -> eval_fold2_slist fl_list t empty_res empty_res )
745 (fun t _ -> eval_fold2_slist fl_list t empty_res
746 (loop_tag tag (first t) llist t))
748 (fun t _ -> eval_fold2_slist fl_list t empty_res
749 (loop (first t) llist t))
755 (fun t ctx -> eval_fold2_slist fl_list t
756 (loop_tag tag (next t ctx) rlist ctx) empty_res)
759 (fun t ctx -> eval_fold2_slist fl_list t
760 (loop (next t ctx) rlist ctx) empty_res)
764 | `TAG(tag1),`TAG(tag2) ->
765 (fun t ctx -> eval_fold2_slist fl_list t
766 (loop_tag tag2 (next t ctx) rlist ctx)
767 (loop_tag tag1 (first t) llist t))
770 (fun t ctx -> eval_fold2_slist fl_list t
771 (loop (next t ctx) rlist ctx)
772 (loop_tag tag (first t) llist t))
775 eval_fold2_slist fl_list t
776 (loop_tag tag (next t ctx) rlist ctx)
777 (loop (first t) llist t) )
780 eval_fold2_slist fl_list t
781 (loop (next t ctx) rlist ctx)
782 (loop (first t) llist t) )
785 let cont = D_IF_( (fun t ctx ->
786 let a,b = cont t ctx in
787 register_trace tree t (slist,a,fl_list,first,next,ctx);
791 (CachedTransTable.add td_trans (tag,slist) cont;cont)
795 (if noright then loop_no_right else loop) t slist ctx
798 let run_top_down a tree =
799 let init = SList.cons a.init SList.nil in
800 let _,res = top_down a tree Tree.root init Tree.root 1
803 output_trace a tree "trace.html"
804 (RS.fold (fun t a -> IntSet.add (Tree.id tree t) a) res.(0) IntSet.empty),
808 module Configuration =
810 module Ptss = Set.Make(StateSet)
811 module IMap = Map.Make(StateSet)
812 type t = { hash : int;
814 results : RS.t IMap.t }
815 let empty = { hash = 0;
817 results = IMap.empty;
819 let is_empty c = Ptss.is_empty c.sets
821 if Ptss.mem s c.sets then
822 { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
824 { hash = HASHINT2(c.hash,Ptset.Int.uid s);
825 sets = Ptss.add s c.sets;
826 results = IMap.add s r c.results
829 let pr fmt c = Format.fprintf fmt "{";
830 Ptss.iter (fun s -> StateSet.print fmt s;
831 Format.fprintf fmt " ") c.sets;
832 Format.fprintf fmt "}\n%!";
833 IMap.iter (fun k d ->
834 StateSet.print fmt k;
835 Format.fprintf fmt "-> %i\n" (RS.length d)) c.results;
836 Format.fprintf fmt "\n%!"
844 RS.concat r (IMap.find s acc)
846 | Not_found -> r) acc) c1.results IMap.empty
849 IMap.fold (fun s r acc ->
852 RS.concat r (IMap.find s acc)
854 | Not_found -> r) acc) c2.results acc1
858 (fun s (ah,ass) -> (HASHINT2(ah,Ptset.Int.uid s),
860 (Ptss.union c1.sets c2.sets) (0,Ptss.empty)
868 let h_fold = Hashtbl.create 511
870 let fold_f_conf t slist fl_list conf dir=
871 let rec loop sl fl acc =
872 match SList.node sl,fl with
874 |SList.Cons(s,sll), formlist::fll ->
875 let r',(rb,rb1,rb2,mark) =
876 let key = SList.hash sl,Formlist.hash formlist,dir in
878 Hashtbl.find h_fold key
880 Not_found -> let res =
881 if dir then eval_formlist s Ptset.Int.empty formlist
882 else eval_formlist Ptset.Int.empty s formlist
883 in (Hashtbl.add h_fold key res;res)
885 if rb && ((dir&&rb1)|| ((not dir) && rb2))
889 try Configuration.IMap.find s conf.Configuration.results
890 with Not_found -> RS.empty
892 Configuration.add acc r' (if mark then RS.cons t old_r else old_r)
895 else loop sll fll acc
898 loop slist fl_list Configuration.empty
900 let h_trans = Hashtbl.create 4096
902 let get_up_trans slist ptag a tree =
903 let key = (HASHINT2(SList.uid slist,ptag)) in
905 Hashtbl.find h_trans key
909 Hashtbl.fold (fun q l acc ->
910 List.fold_left (fun fl_acc (ts,t) ->
911 if TagSet.mem ptag ts then Formlist.cons t fl_acc
917 let res = SList.fold (fun _ acc -> f_list::acc) slist []
919 (Hashtbl.add h_trans key res;res)
923 let h_tdconf = Hashtbl.create 511
924 let rec bottom_up a tree t conf next jump_fun root dotd init accu =
925 if (not dotd) && (Configuration.is_empty conf ) then
929 let below_right = Tree.is_below_right tree t next in
931 let accu,rightconf,next_of_next =
932 if below_right then (* jump to the next *)
933 bottom_up a tree next conf (jump_fun next) jump_fun (Tree.next_sibling tree t) true init accu
934 else accu,Configuration.empty,next
938 if below_right then prepare_topdown a tree t true
939 else prepare_topdown a tree t false
943 (Configuration.merge rightconf sub, next_of_next)
945 if t == root then accu,conf,next else
946 let parent = Tree.binary_parent tree t in
947 let ptag = Tree.tag tree parent in
948 let dir = Tree.is_left tree t in
949 let slist = Configuration.Ptss.fold (fun e a -> SList.cons e a) conf.Configuration.sets SList.nil in
950 let fl_list = get_up_trans slist ptag a parent in
951 let slist = SList.rev (slist) in
952 let newconf = fold_f_conf parent slist fl_list conf dir in
953 let accu,newconf = Configuration.IMap.fold (fun s res (ar,nc) ->
954 if Ptset.Int.intersect s init then
955 ( RS.concat res ar ,nc)
956 else (ar,Configuration.add nc s res))
957 (newconf.Configuration.results) (accu,Configuration.empty)
960 bottom_up a tree parent newconf next jump_fun root false init accu
962 and prepare_topdown a tree t noright =
963 let tag = Tree.tag tree t in
966 Hashtbl.find h_tdconf tag
969 let res = Hashtbl.fold (fun q l acc ->
970 if List.exists (fun (ts,_) -> TagSet.mem tag ts) l
971 then Ptset.Int.add q acc
972 else acc) a.trans Ptset.Int.empty
973 in Hashtbl.add h_tdconf tag res;res
975 (* let _ = pr ", among ";
976 StateSet.print fmt (Ptset.Int.elements r);
979 let r = SList.cons r SList.nil in
980 let set,res = top_down (~noright:noright) a tree t r t 1 in
981 let set = match SList.node set with
982 | SList.Cons(x,_) ->x
985 Configuration.add Configuration.empty set res.(0)
989 let run_bottom_up a tree k =
991 let trlist = Hashtbl.find a.trans (StateSet.choose a.init)
993 let init = List.fold_left
995 let _,_,f,_ = Transition.node t in
996 let _,_,l = fst ( Formula.st f ) in
997 StateSet.union acc l)
998 StateSet.empty trlist
1000 let tree1,jump_fun =
1003 (*Tree.tagged_lowest t tag, fun tree -> Tree.tagged_next tree tag*)
1004 (Tree.tagged_desc tree tag t, let jump = Tree.tagged_foll_ctx tree tag
1005 in fun n -> jump n t )
1006 | `CONTAINS(_) -> (Tree.text_below tree t,let jump = Tree.text_next tree
1007 in fun n -> jump n t)
1010 let tree2 = jump_fun tree1 in
1011 let rec loop t next acc =
1012 let acc,conf,next_of_next = bottom_up a tree t
1013 Configuration.empty next jump_fun (Tree.root) true init acc
1015 let acc = Configuration.IMap.fold
1016 ( fun s res acc -> if StateSet.intersect init s
1017 then RS.concat res acc else acc) conf.Configuration.results acc
1019 if Tree.is_nil next_of_next (*|| Tree.equal next next_of_next *)then
1021 else loop next_of_next (jump_fun next_of_next) acc
1023 loop tree1 tree2 RS.empty
1028 let top_down_count a t = let module RI = Run(Integer) in Integer.length (RI.run_top_down a t)
1029 let top_down a t = let module RI = Run(IdSet) in (RI.run_top_down a t)
1030 let bottom_up_count a t k = let module RI = Run(Integer) in Integer.length (RI.run_bottom_up a t k)