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
18 fun () -> incr id; !id
22 external hash : t -> int = "%identity"
23 let print fmt x = Format.fprintf fmt "%i" x
24 let dump fmt x = print fmt x
26 if x < 0 then failwith (Printf.sprintf "State: Assertion %i < 0 failed" x)
31 include Ptset.Make ( struct type t = int
33 external hash : t -> int = "%identity"
34 external uid : t -> Uid.t = "%identity"
35 external equal : t -> t -> bool = "%eq"
36 external make : t -> int = "%identity"
37 external node : t -> int = "%identity"
38 external with_id : Uid.t -> t = "%identity"
42 Format.pp_print_string ppf "{ ";
43 iter (fun i -> Format.fprintf ppf "%i " i) s;
44 Format.pp_print_string ppf "}";
45 Format.pp_print_flush ppf ()
52 | Or of 'hcons * 'hcons
53 | And of 'hcons * 'hcons
54 | Atom of ([ `Left | `Right | `LLeft | `RRight ]*bool*State.t)
59 st : (StateSet.t*StateSet.t*StateSet.t)*(StateSet.t*StateSet.t*StateSet.t);
60 size: int; (* Todo check if this is needed *)
63 external hash_const_variant : [> ] -> int = "%identity"
64 module rec Node : Hcons.S with type data = Data.t = Hcons.Make (Data)
65 and Data : Hashtbl.HashedType with type t = Node.t node =
68 let equal x y = x.size == y.size &&
69 match x.pos,y.pos with
70 | a,b when a == b -> true
71 | Or(xf1,xf2),Or(yf1,yf2)
72 | And(xf1,xf2),And(yf1,yf2) -> (xf1 == yf1) && (xf2 == yf2)
73 | Atom(d1,p1,s1), Atom(d2,p2,s2) -> d1 == d2 && (p1==p2) && s1 == s2
79 | Or (f1,f2) -> HASHINT3(PRIME2,Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
80 | And (f1,f2) -> HASHINT3(PRIME3,Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
81 | Atom(d,p,s) -> HASHINT4(PRIME4,hash_const_variant d,vb p,s)
85 let hash x = x.Node.key
87 let equal = Node.equal
88 let expr f = f.Node.node.pos
89 let st f = f.Node.node.st
90 let size f = f.Node.node.size
99 let rec print ?(parent=false) ppf f =
100 if parent then Format.fprintf ppf "(";
101 let _ = match expr f with
102 | True -> Format.fprintf ppf "T"
103 | False -> Format.fprintf ppf "F"
105 print ~parent:(prio f > prio f1) ppf f1;
106 Format.fprintf ppf " ∧ ";
107 print ~parent:(prio f > prio f2) ppf f2;
110 Format.fprintf ppf " ∨ ";
112 | Atom(dir,b,s) -> Format.fprintf ppf "%s%s[%i]"
113 (if b then "" else "¬")
120 if parent then Format.fprintf ppf ")"
122 let print ppf f = print ~parent:false ppf f
124 let is_true f = (expr f) == True
125 let is_false f = (expr f) == False
128 let cons pos neg s1 s2 size1 size2 =
129 let nnode = Node.make { pos = neg; neg = (Obj.magic 0); st = s2; size = size2 } in
130 let pnode = Node.make { pos = pos; neg = nnode ; st = s1; size = size1 }
132 (Node.node nnode).neg <- pnode; (* works because the neg field isn't taken into
133 account for hashing ! *)
136 let empty_triple = StateSet.empty,StateSet.empty,StateSet.empty
137 let empty_hex = empty_triple,empty_triple
138 let true_,false_ = cons True False empty_hex empty_hex 0 0
140 let si = StateSet.singleton s in
141 let ss = match d with
142 | `Left -> (si,StateSet.empty,si),empty_triple
143 | `Right -> empty_triple,(si,StateSet.empty,si)
144 | `LLeft -> (StateSet.empty,si,si),empty_triple
145 | `RRight -> empty_triple,(StateSet.empty,si,si)
146 in fst (cons (Atom(d,p,s)) (Atom(d,not p,s)) ss ss 1 1)
148 let not_ f = f.Node.node.neg
149 let union_hex ((l1,ll1,lll1),(r1,rr1,rrr1)) ((l2,ll2,lll2),(r2,rr2,rrr2)) =
150 (StateSet.mem_union l1 l2 ,StateSet.mem_union ll1 ll2,StateSet.mem_union lll1 lll2),
151 (StateSet.mem_union r1 r2 ,StateSet.mem_union rr1 rr2,StateSet.mem_union rrr1 rrr2)
153 let merge_states f1 f2 =
155 union_hex (st f1) (st f2)
157 union_hex (st (not_ f1)) (st (not_ f2))
161 let order f1 f2 = if uid f1 < uid f2 then f2,f1 else f1,f2
164 (* Tautologies: x|x, x|not(x) *)
166 if equal f1 f2 then f1 else
167 if equal f1 (not_ f2) then true_ else
170 if is_true f1 || is_true f2 then true_ else
171 if is_false f1 && is_false f2 then false_ else
172 if is_false f1 then f2 else
173 if is_false f2 then f1 else
175 (* commutativity of | *)
177 let f1,f2 = order f1 f2 in
178 let psize = (size f1) + (size f2) in
179 let nsize = (size (not_ f1)) + (size (not_ f2)) in
180 let sp,sn = merge_states f1 f2 in
181 fst (cons (Or(f1,f2)) (And(not_ f1,not_ f2)) sp sn psize nsize)
186 (* Tautologies: x&x, x¬(x) *)
188 if equal f1 f2 then f1 else
189 if equal f1 (not_ f2) then false_ else
191 (* simplifications *)
193 if is_true f1 && is_true f2 then true_ else
194 if is_false f1 || is_false f2 then false_ else
195 if is_true f1 then f2 else
196 if is_true f2 then f1 else
198 (* commutativity of & *)
200 let f1,f2 = order f1 f2 in
201 let psize = (size f1) + (size f2) in
202 let nsize = (size (not_ f1)) + (size (not_ f2)) in
203 let sp,sn = merge_states f1 f2 in
204 fst (cons (And(f1,f2)) (Or(not_ f1,not_ f2)) sp sn psize nsize)
205 module Infix = struct
206 let ( +| ) f1 f2 = or_ f1 f2
207 let ( *& ) f1 f2 = and_ f1 f2
208 let ( *+ ) d s = atom_ d true s
209 let ( *- ) d s = atom_ d false s
213 module Transition = struct
215 type node = State.t*TagSet.t*bool*Formula.t*bool
216 include Hcons.Make(struct
218 let hash (s,ts,m,f,b) = HASHINT5(s,Uid.to_int (TagSet.uid ts),
219 Uid.to_int (Formula.uid f),
221 let equal (s,ts,b,f,m) (s',ts',b',f',m') =
222 s == s' && ts == ts' && b==b' && m==m' && f == f'
225 let print ppf f = let (st,ts,mark,form,b) = node f in
226 Format.fprintf ppf "(%i, " st;
228 Format.fprintf ppf ") %s" (if mark then "⇒" else "→");
229 Formula.print ppf form;
230 Format.fprintf ppf "%s%!" (if b then " (b)" else "")
233 module Infix = struct
235 let ( >< ) state (l,mark) = state,(l,mark,false)
236 let ( ><@ ) state (l,mark) = state,(l,mark,true)
237 let ( >=> ) (state,(label,mark,bur)) form = (state,label,(make (state,label,mark,form,bur)))
242 module Formlist = struct
243 include Hlist.Make(Transition)
245 iter (fun t -> Transition.print ppf t; Format.pp_print_newline ppf ()) fl
248 module Formlistlist =
250 include Hlist.Make(Formlist)
252 iter (fun fl -> Formlist.print ppf fl; Format.pp_print_newline ppf ())fll
257 mutable states : StateSet.t;
259 starstate : StateSet.t option;
260 (* Transitions of the Alternating automaton *)
261 trans : (State.t,(TagSet.t*Transition.t) list) Hashtbl.t;
262 query_string: string;
267 Format.fprintf ppf "Automaton (%i) :\n" a.id;
268 Format.fprintf ppf "States : "; StateSet.print ppf a.states;
269 Format.fprintf ppf "\nInitial states : "; StateSet.print ppf a.init;
270 Format.fprintf ppf "\nAlternating transitions :\n";
271 let l = Hashtbl.fold (fun k t acc ->
272 (List.map (fun (ts,tr) -> (ts,k),Transition.node tr) t) @ acc) a.trans [] in
273 let l = List.sort (fun ((tsx,x),_) ((tsy,y),_) ->
274 if y-x == 0 then TagSet.compare tsy tsx else y-x) l in
275 let maxh,maxt,l_print =
277 fun (maxh,maxt,l) ((ts,q),(_,_,b,f,_)) ->
279 if TagSet.is_finite ts
280 then "{" ^ (TagSet.fold (fun t a -> a ^ " '" ^ (Tag.to_string t)^"'") ts "") ^" }"
281 else let cts = TagSet.neg ts in
282 if TagSet.is_empty cts then "*" else
283 (TagSet.fold (fun t a -> a ^ " " ^ (Tag.to_string t)) cts "*\\{"
286 let s = Printf.sprintf "(%s,%i)" s q in
288 Formula.print Format.str_formatter f;
289 Format.flush_str_formatter()
291 (max (String.length s) maxh, max (String.length s_frm) maxt,
292 (s,(if b then "⇒" else "→"),s_frm)::l)) (0,0,[]) l
294 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_');
295 List.iter (fun (s,m,f) -> let s = s ^ (String.make (maxh-(String.length s)) ' ') in
296 Format.fprintf ppf "%s %s %s\n" s m f) l_print;
297 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_')
300 module FormTable = Hashtbl.Make(struct
301 type t = Formula.t*StateSet.t*StateSet.t
302 let equal (f1,s1,t1) (f2,s2,t2) =
303 f1 == f2 && s1 == s2 && t1 == t2
305 HASHINT3(Uid.to_int (Formula.uid f),
306 Uid.to_int (StateSet.uid s),
307 Uid.to_int (StateSet.uid t))
312 let h_f = FormTable.create BIG_H_SIZE in
316 | F.True -> true,true,true
317 | F.False -> false,false,false
318 | F.Atom((`Left|`LLeft),b,q) ->
319 if b == (StateSet.mem q s1)
320 then (true,true,false)
321 else false,false,false
323 if b == (StateSet.mem q s2)
324 then (true,false,true)
325 else false,false,false
327 try FormTable.find h_f (f,s1,s2)
328 with Not_found -> let r =
331 let b1,rl1,rr1 = loop f1
333 if b1 && rl1 && rr1 then (true,true,true) else
334 let b2,rl2,rr2 = loop f2 in
335 let rl1,rr1 = if b1 then rl1,rr1 else false,false
336 and rl2,rr2 = if b2 then rl2,rr2 else false,false
337 in (b1 || b2, rl1||rl2,rr1||rr2)
340 let b1,rl1,rr1 = loop f1 in
341 if b1 && rl1 && rr1 then (true,true,true) else
343 let b2,rl2,rr2 = loop f2 in
344 if b2 then (true,rl1||rl2,rr1||rr2) else (false,false,false)
345 else (false,false,false)
347 in FormTable.add h_f (f,s1,s2) r;r
351 module FTable = Hashtbl.Make(struct
352 type t = Tag.t*Formlist.t*StateSet.t*StateSet.t
353 let equal (tg1,f1,s1,t1) (tg2,f2,s2,t2) =
354 tg1 == tg2 && f1 == f2 && s1 == s2 && t1 == t2;;
355 let hash (tg,f,s,t) =
356 HASHINT4(tg, Uid.to_int (Formlist.uid f),
357 Uid.to_int (StateSet.uid s),
358 Uid.to_int (StateSet.uid t))
362 let h_f = FTable.create BIG_H_SIZE
363 type merge_conf = NO | ONLY1 | ONLY2 | ONLY12 | MARK | MARK1 | MARK2 | MARK12
364 (* 000 001 010 011 100 101 110 111 *)
365 let eval_formlist tag s1 s2 fl =
368 FTable.find h_f (tag,fl,s1,s2)
371 match Formlist.node fl with
372 | Formlist.Cons(f,fll) ->
373 let q,ts,mark,f,_ = Transition.node f in
375 if TagSet.mem tag ts then eval_form_bool f s1 s2 else (false,false,false)
377 let (s,(b',b1',b2',amark)) as res = loop fll in
378 let r = if b then (StateSet.add q s, (b, b1'||b1,b2'||b2,mark||amark))
380 in FTable.add h_f (tag,fl,s1,s2) r;r
381 | Formlist.Nil -> StateSet.empty,(false,false,false,false)
386 | (false,_,_,_) -> NO
387 | (_,false,false,false) -> NO
388 | (_,true,false,false) -> ONLY1
389 | (_,false,true,false) -> ONLY2
390 | (_,true,true,false) -> ONLY12
391 | (_,false,false,true) -> MARK
392 | (_,true,false,true) -> MARK1
393 | (_,false,true,true) -> MARK2
396 let bool_of_merge conf =
398 | NO -> false,false,false,false
399 | ONLY1 -> true,true,false,false
400 | ONLY2 -> true,false,true,false
401 | ONLY12 -> true,true,true,false
402 | MARK -> true,false,false,true
403 | MARK1 -> true,true,false,true
404 | MARK2 -> true,false,true,true
405 | MARK12 -> true,true,true,true
408 let tags_of_state a q =
411 if p == q then List.fold_left
413 let _,_,_,_,aux = Transition.node t in
415 TagSet.cup ts acc) acc l
417 else acc) a.trans TagSet.empty
422 let ts = Ptset.Int.fold (fun q acc -> TagSet.cup acc (tags_of_state a q)) qs TagSet.empty
424 if TagSet.is_finite ts
425 then `Positive(TagSet.positive ts)
426 else `Negative(TagSet.negative ts)
430 | `Positive s -> let r = Ptset.Int.inter a s in (r,Ptset.Int.mem Tag.pcdata r, true)
431 | `Negative s -> let r = Ptset.Int.diff a s in (r, Ptset.Int.mem Tag.pcdata r, false)
434 module type ResultSet =
437 type elt = [` Tree ] Tree.node
439 val cons : elt -> t -> t
440 val concat : t -> t -> t
441 val iter : ( elt -> unit) -> t -> unit
442 val fold : ( elt -> 'a -> 'a) -> t -> 'a -> 'a
443 val map : ( elt -> elt) -> t -> t
444 val length : t -> int
445 val merge : merge_conf -> elt -> t -> t -> t
446 val mk_quick_tag_loop : (elt -> elt -> 'a*t array) -> 'a -> int -> Tree.t -> Tag.t -> (elt -> elt -> 'a*t array)
447 val mk_quick_star_loop : (elt -> elt -> 'a*t array) -> 'a -> int -> Tree.t -> (elt -> elt -> 'a*t array)
450 module Integer : ResultSet =
453 type elt = [`Tree] Tree.node
457 let concat x y = x + y
458 let iter _ _ = failwith "iter not implemented"
459 let fold _ _ _ = failwith "fold not implemented"
460 let map _ _ = failwith "map not implemented"
462 let merge2 conf t res1 res2 =
463 let rb,rb1,rb2,mark = conf in
465 let res1 = if rb1 then res1 else 0
466 and res2 = if rb2 then res2 else 0
468 if mark then 1+res1+res2
471 let merge conf t res1 res2 =
476 | ONLY12 -> res1+res2
480 | MARK12 -> res1+res2+1
481 let merge conf _ res1 res2 =
482 let conf = Obj.magic conf in
483 (conf lsr 2) + ((conf land 0b10) lsr 1)*res2 + (conf land 0b1)*res1
486 let mk_quick_tag_loop _ sl ss tree tag = ();
488 (sl, Array.make ss (Tree.subtree_tags tree tag t))
489 let mk_quick_star_loop _ sl ss tree = ();
491 (sl, Array.make ss (Tree.subtree_elements tree t))
495 module IdSet : ResultSet=
497 type elt = [`Tree] Tree.node
500 | Concat of node*node
502 and t = { node : node;
505 let empty = { node = Nil; length = 0 }
507 let cons e t = { node = Cons(e,t.node); length = t.length+1 }
508 let concat t1 t2 = { node = Concat(t1.node,t2.node); length = t1.length+t2.length }
509 let append e t = { node = Concat(t.node,Cons(e,Nil)); length = t.length+1 }
512 let rec loop acc t = match t with
514 | Cons (e,t) -> loop (f e acc) t
515 | Concat (t1,t2) -> loop (loop acc t1) t2
519 let length l = l.length
523 let rec loop = function
525 | Cons (e,t) -> f e; loop t
526 | Concat(t1,t2) -> loop t1;loop t2
530 let rec loop = function
532 | Cons(e,t) -> Cons(f e, loop t)
533 | Concat(t1,t2) -> Concat(loop t1,loop t2)
535 { l with node = loop l.node }
537 let merge conf t res1 res2 =
540 | MARK -> cons t empty
543 | ONLY12 -> { node = (Concat(res1.node,res2.node));
544 length = res1.length + res2.length ;}
545 | MARK12 -> { node = Cons(t,(Concat(res1.node,res2.node)));
546 length = res1.length + res2.length + 1;}
547 | MARK1 -> { node = Cons(t,res1.node);
548 length = res1.length + 1;}
549 | MARK2 -> { node = Cons(t,res2.node);
550 length = res2.length + 1;}
552 let mk_quick_tag_loop f _ _ _ _ = f
553 let mk_quick_star_loop f _ _ _ = f
555 module GResult(Doc : sig val doc : Tree.t end) = struct
557 type elt = [` Tree] Tree.node
558 external create_empty : int -> bits = "caml_result_set_create" "noalloc"
559 external set : bits -> int -> unit = "caml_result_set_set" "noalloc"
560 external next : bits -> int -> int = "caml_result_set_next" "noalloc"
561 external count : bits -> int = "caml_result_set_count" "noalloc"
562 external clear : bits -> elt -> elt -> unit = "caml_result_set_clear" "noalloc"
564 external set_tag_bits : bits -> Tag.t -> Tree.t -> elt -> elt = "caml_set_tag_bits" "noalloc"
566 { segments : elt list;
571 let size = (Tree.subtree_size Doc.doc Tree.root) in
572 create_empty (size*2+1)
574 let empty = { segments = [];
578 let rec loop l = match l with
579 | [] -> { bits = (set t.bits (Obj.magic e);t.bits);
582 if Tree.is_binary_ancestor Doc.doc e p then
585 { bits = (set t.bits (Obj.magic e);t.bits);
591 if t2.segments == [] then t1
593 if t1.segments == [] then t2
595 let h2 = List.hd t2.segments in
596 let rec loop l = match l with
599 if Tree.is_binary_ancestor Doc.doc p h2 then
605 segments = loop t1.segments
611 else (f ((Obj.magic i):elt);loop (next t.bits i))
612 in loop (next t.bits 0)
617 else loop (next t.bits i) (f ((Obj.magic i):elt) acc)
618 in loop (next t.bits 0) acc
620 let map _ _ = failwith "noop"
621 (*let length t = let cpt = ref 0 in
622 iter (fun _ -> incr cpt) t; !cpt *)
623 let length t = count t.bits
626 let rec loop l = match l with
629 clear t.bits idx (Tree.closing Doc.doc idx); loop ll
631 loop t.segments;empty
633 let merge (rb,rb1,rb2,mark) elt t1 t2 =
635 (* let _ = Printf.eprintf "Lenght before merging is %i %i\n"
636 (List.length t1.segments) (List.length t2.segments)
638 match t1.segments,t2.segments with
639 [],[] -> if mark then cons elt empty else empty
640 | [_],[] when rb1 -> if mark then cons elt t1 else t1
641 | [], [_] when rb2 -> if mark then cons elt t2 else t2
642 | [_],[_] when rb1 && rb2 -> if mark then cons elt empty else
645 let t1 = if rb1 then t1 else clear_bits t1
646 and t2 = if rb2 then t2 else clear_bits t2
648 (if mark then cons elt (concat t1 t2)
651 let _ = clear_bits t1 in
654 let merge conf t t1 t2 =
655 match t1.segments,t2.segments,conf with
656 | _,_,NO -> let _ = clear_bits t1 in clear_bits t2
657 | [],[],(MARK1|MARK2|MARK12|MARK) -> cons t empty
659 | [_],[],(ONLY1|ONLY12) -> t1
660 | [_],[],(MARK1|MARK12) -> cons t t1
661 | [],[_],(ONLY2|ONLY12) -> t2
662 | [],[_],(MARK2|MARK12) -> cons t t2
663 | [_],[_],ONLY12 -> concat t1 t2
664 | [_],[_],MARK12 -> cons t empty
665 | _,_,MARK -> let _ = clear_bits t2 in cons t (clear_bits t1)
666 | _,_,ONLY1 -> let _ = clear_bits t2 in t1
667 | _,_,ONLY2 -> let _ = clear_bits t1 in t2
668 | _,_,ONLY12 -> concat t1 t2
669 | _,_,MARK1 -> let _ = clear_bits t2 in cons t t1
670 | _,_,MARK2 -> let _ = clear_bits t1 in cons t t2
671 | _,_,MARK12 -> cons t (concat t1 t2)
673 let mk_quick_tag_loop _ sl ss tree tag = ();
676 let first = set_tag_bits empty.bits tag tree t in
678 if first == Tree.nil then res else
681 (sl, Array.make ss res)
683 let mk_quick_star_loop f _ _ _ = f
685 module Run (RS : ResultSet) =
688 module SList = struct
689 include Hlist.Make (StateSet)
691 Format.fprintf ppf "[ ";
693 match l.Node.node with
696 StateSet.print ppf s;
697 iter (fun s -> Format.fprintf ppf "; ";
698 StateSet.print ppf s) ll
700 Format.fprintf ppf "]%!"
708 module IntSet = Set.Make(struct type t = int let compare = (-) end)
709 INCLUDE "html_trace.ml"
712 let mk_fun f s = D_IGNORE_(register_funname f s,f)
713 let mk_app_fun f arg s = let g = f arg in
714 D_IGNORE_(register_funname g ((get_funname f) ^ " " ^ s), g)
715 let mk_app_fun f arg _ = f arg
716 let mk_app_fun2 f arg1 arg2 s = let g = f arg1 arg2 in
717 D_IGNORE_(register_funname g ((get_funname f) ^ " " ^ s), g)
718 (* let mk_app_fun2 f arg1 arg2 s = Printf.eprintf "Building f2 %s\n%!" s; f arg1 arg2 *)
720 let string_of_ts tags = (Ptset.Int.fold (fun t a -> a ^ " " ^ (Tag.to_string t) ) tags "{")^ " }"
725 type jump = [ `NIL | `ANY |`ANYNOTEXT | `JUMP ]
726 type t = jump*Ptset.Int.t*Ptset.Int.t
731 | `ANYNOTEXT -> "ANYNOTEXT"
732 let merge_jump (j1,c1,l1) (j2,c2,l2) =
734 | _,`NIL -> (j1,c1,l1)
735 | `NIL,_ -> (j2,c2,l2)
736 | `ANY,_ -> (`ANY,Ptset.Int.empty,Ptset.Int.empty)
737 | _,`ANY -> (`ANY,Ptset.Int.empty,Ptset.Int.empty)
739 if Ptset.Int.mem Tag.pcdata (Ptset.Int.union c2 l2) then
740 (`ANY,Ptset.Int.empty,Ptset.Int.empty)
742 (`ANYNOTEXT,Ptset.Int.empty,Ptset.Int.empty)
744 if Ptset.Int.mem Tag.pcdata (Ptset.Int.union c1 l1) then
745 (`ANY,Ptset.Int.empty,Ptset.Int.empty)
747 (`ANYNOTEXT,Ptset.Int.empty,Ptset.Int.empty)
748 | `JUMP,`JUMP -> (`JUMP, Ptset.Int.union c1 c2,Ptset.Int.union l1 l2)
750 let merge_jump_list = function
751 | [] -> `NIL,Ptset.Int.empty,Ptset.Int.empty
753 List.fold_left (merge_jump) p r
764 let _,_,_,_,bur = Transition.node f in
765 if bur then acc else TagSet.cup acc ts)
767 else acc ) a.trans TagSet.empty
770 let is_rec a s access =
772 (fun (_,t) -> let _,_,_,f,_ = Transition.node t in
773 StateSet.mem s ((fun (_,_,x) -> x) (access (Formula.st f)))) (Hashtbl.find a.trans s)
775 let is_final_marking a s =
776 List.exists (fun (_,t) -> let _,_,m,f,_ = Transition.node t in m&& (Formula.is_true f))
777 (Hashtbl.find a.trans s)
780 let decide a c_label l_label dir_states dir =
782 let l = StateSet.fold
784 let s_rec = is_rec a s (if dir then fst else snd) in
785 let s_rec = if dir then s_rec else
789 let s_lab = labels a s in
791 if (not (TagSet.is_finite s_lab)) then
792 if TagSet.mem Tag.pcdata s_lab then (`ANY,Ptset.Int.empty,Ptset.Int.empty)
793 else (`ANYNOTEXT,Ptset.Int.empty,Ptset.Int.empty)
796 then (`JUMP,Ptset.Int.empty, TagSet.positive
797 (TagSet.cap (TagSet.inj_positive l_label) s_lab))
798 else (`JUMP,TagSet.positive
799 (TagSet.cap (TagSet.inj_positive c_label) s_lab),
804 && Ptset.Int.is_empty cc
805 && Ptset.Int.is_empty ll
806 then (`NIL,Ptset.Int.empty,Ptset.Int.empty)
807 else (jmp,cc,ll))::l) dir_states []
815 let choose_jump (d,cl,ll) f_nil f_t1 f_s1 f_tn f_sn f_s1n f_notext f_maytext =
817 | `NIL -> (`NIL,f_nil)
818 | `ANYNOTEXT -> `ANY,f_notext
819 | `ANY -> `ANY,f_maytext
821 if Ptset.Int.is_empty cl then
822 if Ptset.Int.is_singleton ll then
823 let tag = Ptset.Int.choose ll in
824 (`TAG(tag),mk_app_fun f_tn tag (Tag.to_string tag))
826 (`MANY(ll),mk_app_fun f_sn ll (string_of_ts ll))
827 else if Ptset.Int.is_empty ll then
828 if Ptset.Int.is_singleton cl then
829 let tag = Ptset.Int.choose cl in
830 (`TAG(tag),mk_app_fun f_t1 tag (Tag.to_string tag))
832 (`MANY(cl),mk_app_fun f_s1 cl (string_of_ts cl))
834 (`ANY,mk_app_fun2 f_s1n cl ll ((string_of_ts cl) ^ " " ^ (string_of_ts ll)))
838 let choose_jump_down tree d =
840 (mk_fun (fun _ -> Tree.nil) "Tree.mk_nil")
841 (mk_fun (Tree.tagged_child tree) "Tree.tagged_child")
842 (mk_fun (Tree.select_child tree) "Tree.select_child")
843 (mk_fun (Tree.tagged_descendant tree) "Tree.tagged_desc")
844 (mk_fun (Tree.select_descendant tree) "Tree.select_desc")
845 (mk_fun (fun _ _ -> Tree.first_child tree) "[FIRSTCHILD]Tree.select_child_desc")
846 (mk_fun (Tree.first_element tree) "Tree.first_element")
847 (mk_fun (Tree.first_child tree) "Tree.first_child")
849 let choose_jump_next tree d =
851 (mk_fun (fun _ _ -> Tree.nil) "Tree.mk_nil2")
852 (mk_fun (Tree.tagged_following_sibling_below tree) "Tree.tagged_sibling_ctx")
853 (mk_fun (Tree.select_following_sibling_below tree) "Tree.select_sibling_ctx")
854 (mk_fun (Tree.tagged_following_below tree) "Tree.tagged_foll_ctx")
855 (mk_fun (Tree.select_following_below tree) "Tree.select_foll_ctx")
856 (mk_fun (fun _ _ -> Tree.next_sibling_below tree) "[NEXTSIBLING]Tree.select_sibling_foll_ctx")
857 (mk_fun (Tree.next_element_below tree) "Tree.next_element_ctx")
858 (mk_fun (Tree.next_sibling_below tree) "Tree.node_sibling_ctx")
865 let get = Array.unsafe_get
868 type fun_tree = [`Tree] Tree.node -> [`Tree] Tree.node -> SList.t -> Tag.t -> bool -> SList.t*RS.t array
869 type t = fun_tree array array
871 let dummy = fun _ _ _ _ _ -> failwith "Uninitializd CodeCache"
872 let default_line = Array.create 1024 dummy (* 1024 = max_tag *)
873 let create n = Array.create n default_line
875 for i = 0 to (Array.length default_line) - 1
877 default_line.(i) <- f
880 let get_fun h slist tag =
881 get (get h (Uid.to_int slist.SList.Node.id)) tag
883 let set_fun (h : t) slist tag (data : fun_tree) =
884 let tab = get h (Uid.to_int slist.SList.Node.id) in
885 let line = if tab == default_line then
886 let x = Array.copy tab in
887 (set h (Uid.to_int slist.SList.Node.id) x;x)
895 let rec loop acc = function 0 -> acc
896 | n -> loop (SList.cons StateSet.empty acc) (n-1)
900 module Fold2Res = struct
901 let get = Array.unsafe_get
903 external field1 : Obj.t -> int = "%field1"
904 type t = Obj.t array array array array
905 let dummy_val = Obj.repr ((),2,())
907 let default_line3 = Array.create BIG_A_SIZE dummy_val
908 let default_line2 = Array.create BIG_A_SIZE default_line3
909 let default_line1 = Array.create BIG_A_SIZE default_line2
911 let create n = Array.create n default_line1
913 let find h tag fl s1 s2 : SList.t*bool*(merge_conf array) =
914 let l1 = get h tag in
915 let l2 = get l1 (Uid.to_int fl.Formlistlist.Node.id) in
916 let l3 = get l2 (Uid.to_int s1.SList.Node.id) in
917 Obj.magic (get l3 (Uid.to_int s2.SList.Node.id))
919 let is_valid b = (Obj.magic b) != 2
920 let get_replace tab idx default =
921 let e = get tab idx in
923 let ne = Array.copy e in (set tab idx ne;ne)
926 let add h tag fl s1 s2 (data: SList.t*bool*(merge_conf array)) =
927 let l1 = get_replace h tag default_line1 in
928 let l2 = get_replace l1 (Uid.to_int fl.Formlistlist.Node.id) default_line2 in
929 let l3 = get_replace l2 (Uid.to_int s1.SList.Node.id) default_line3 in
930 set l3 (Uid.to_int s2.SList.Node.id) (Obj.repr data)
936 let top_down ?(noright=false) a tree t slist ctx slot_size td_trans h_fold2=
937 let pempty = empty_size slot_size in
938 let rempty = Array.make slot_size RS.empty in
939 (* evaluation starts from the right so we put sl1,res1 at the end *)
940 let eval_fold2_slist fll t tag (sl2,res2) (sl1,res1) =
941 let res = Array.copy rempty in
942 let r,b,btab = Fold2Res.find h_fold2 tag fll sl1 sl2 in
943 if Fold2Res.is_valid b then
945 if b then for i=0 to slot_size - 1 do
946 res.(0) <- RS.merge btab.(0) t res1.(0) res2.(0);
952 let btab = Array.make slot_size NO in
953 let rec fold l1 l2 fll i aq ab =
954 match fll.Formlistlist.Node.node,
958 | Formlistlist.Cons(fl,fll),
960 SList.Cons(s2,ll2) ->
961 let r',conf = eval_formlist tag s1 s2 fl in
962 let _ = btab.(i) <- conf
964 fold ll1 ll2 fll (i+1) (SList.cons r' aq) ((conf!=NO)||ab)
967 let r,b = fold sl1 sl2 fll 0 SList.nil false in
968 Fold2Res.add h_fold2 tag fll sl1 sl2 (r,b,btab);
969 if b then for i=0 to slot_size - 1 do
970 res.(i) <- RS.merge btab.(i) t res1.(i) res2.(i);
976 let null_result = (pempty,Array.copy rempty) in
977 let empty_res = null_result in
979 let rec loop t ctx slist _ =
980 if t == Tree.nil then null_result else
981 let tag = Tree.tag tree t in
982 (CodeCache.get_fun td_trans slist tag) t ctx slist tag false
983 (* get_trans t ctx slist tag false
984 (CodeCache.get_opcode td_trans slist tag)
986 and loop_tag t ctx slist tag =
987 if t == Tree.nil then null_result else
988 (CodeCache.get_fun td_trans slist tag) t ctx slist tag false
989 (* get_trans t ctx slist tag false
990 (CodeCache.get_opcode td_trans slist tag) *)
992 and loop_no_right t ctx slist _ =
993 if t == Tree.nil then null_result else
994 let tag = Tree.tag tree t in
995 (CodeCache.get_fun td_trans slist tag) t ctx slist tag true
996 (* get_trans t ctx slist tag true
997 (CodeCache.get_opcode td_trans slist tag) *)
999 and get_trans t ctx slist tag noright opcode =
1002 eval_fold2_slist fll t tag empty_res empty_res
1004 | OpCode.K1 (fll,first,llist,tag1) ->
1005 eval_fold2_slist fll t tag empty_res
1006 (loop_tag (first t) t llist tag1)
1008 | OpCode.K2 (fll,first,llist) ->
1009 eval_fold2_slist fll t tag empty_res
1010 (loop (first t) t llist)
1012 | OpCode.K3 (fll,next,rlist,tag2) ->
1013 eval_fold2_slist fll t tag
1014 (loop_tag (next t ctx) ctx rlist tag2)
1016 | OpCode.K4 (fll,next,rlist) ->
1017 eval_fold2_slist fll t tag
1018 (loop (next t ctx) ctx rlist)
1021 | OpCode.K5 (fll,next,rlist,tag2,first,llist,tag1) ->
1022 eval_fold2_slist fll t tag
1023 (loop_tag (next t ctx) ctx rlist tag2)
1024 (loop_tag (first t) t llist tag1)
1026 | OpCode.K6 (fll,next,rlist,first,llist,tag1) ->
1027 eval_fold2_slist fll t tag
1028 (loop (next t ctx) ctx rlist)
1029 (loop_tag (first t) t llist tag1)
1031 | OpCode.K7 (fll,next,rlist,tag2,first,llist) ->
1032 eval_fold2_slist fll t tag
1033 (loop_tag (next t ctx) ctx rlist tag2)
1034 (loop (first t) t llist)
1036 | OpCode.K8 (fll,next,rlist,first,llist) ->
1037 eval_fold2_slist fll t tag
1038 (loop (next t ctx) ctx rlist)
1039 (loop (first t) t llist)
1041 | OpCode.KDefault _ ->
1042 mk_trans t ctx tag slist noright
1044 and mk_trans t ctx slist tag noright =
1045 let fl_list,llist,rlist,ca,da,sa,fa =
1047 (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
1048 let fl,ll,rr,ca,da,sa,fa =
1052 (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
1054 if (TagSet.mem tag ts)
1056 let _,_,_,f,_ = t.Transition.node in
1057 let (child,desc,below),(sibl,foll,after) = Formula.st f in
1058 (Formlist.cons t fl_acc,
1059 StateSet.union ll_acc below,
1060 StateSet.union rl_acc after,
1061 StateSet.union child c_acc,
1062 StateSet.union desc d_acc,
1063 StateSet.union sibl s_acc,
1064 StateSet.union foll f_acc)
1066 try Hashtbl.find a.trans q
1068 Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
1072 ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
1073 in (Formlistlist.cons fl fll_acc), (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
1074 slist (Formlistlist.nil,SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
1076 (* Logic to chose the first and next function *)
1077 let tags_child,tags_below,tags_siblings,tags_after = Tree.tags tree tag in
1078 let d_f = Algebra.decide a tags_child tags_below (StateSet.union ca da) true in
1079 let d_n = Algebra.decide a tags_siblings tags_after (StateSet.union sa fa) false in
1080 let f_kind,first = choose_jump_down tree d_f
1081 and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
1082 else choose_jump_next tree d_n in
1083 let empty_res = null_result in
1084 let fll = fl_list in
1086 match f_kind,n_kind with
1087 | `NIL,`NIL -> (*OpCode.K0(fl_list) *)
1088 fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res empty_res
1092 |`TAG(tag1) -> (*OpCode.K1(fl_list,first,llist,tag1) *)
1093 fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res
1094 (loop_tag (first t) t llist tag1)
1095 | _ -> (* OpCode.K2(fl_list,first,llist) *)
1096 fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res
1097 (loop (first t) t llist tag)
1101 |`TAG(tag2) -> (*OpCode.K3(fl_list,next,rlist,tag2) *)
1102 fun t ctx _ tag _ ->
1103 eval_fold2_slist fll t tag
1104 (loop_tag (next t ctx) ctx rlist tag2)
1107 | _ -> (*OpCode.K4(fl_list,next,rlist) *)
1108 fun t ctx _ tag _ ->
1109 eval_fold2_slist fll t tag
1110 (loop (next t ctx) ctx rlist tag)
1115 | `TAG(tag1),`TAG(tag2) -> (*OpCode.K5(fl_list,next,rlist,tag2,first,llist,tag1) *)
1116 fun t ctx _ tag _ ->
1117 eval_fold2_slist fll t tag
1118 (loop_tag (next t ctx) ctx rlist tag2)
1119 (loop_tag (first t) t llist tag1)
1121 | `TAG(tag1),`ANY -> (* OpCode.K6(fl_list,next,rlist,first,llist,tag1) *)
1122 fun t ctx _ tag _ ->
1123 eval_fold2_slist fll t tag
1124 (loop (next t ctx) ctx rlist tag)
1125 (loop_tag (first t) t llist tag1)
1127 | `ANY,`TAG(tag2) -> (* OpCode.K7(fl_list,next,rlist,tag2,first,llist) *)
1128 fun t ctx _ tag _ ->
1129 eval_fold2_slist fll t tag
1130 (loop_tag (next t ctx) ctx rlist tag2)
1131 (loop (first t) t llist tag)
1134 | _,_ -> (*OpCode.K8(fl_list,next,rlist,first,llist) *)
1135 (*if SList.equal slist rlist && SList.equal slist llist
1137 let rec loop t ctx =
1138 if t == Tree.nil then empty_res else
1139 let r1 = loop (first t) t
1140 and r2 = loop (next t ctx) ctx
1142 eval_fold2_slist fl_list t (Tree.tag tree t) r2 r1
1145 fun t ctx _ tag _ ->
1146 eval_fold2_slist fll t tag
1147 (loop (next t ctx) ctx rlist tag)
1148 (loop (first t) t llist tag)
1153 CodeCache.set_fun td_trans slist tag cont;
1154 cont t ctx slist tag noright
1156 let _ = CodeCache.init mk_trans in
1157 (if noright then loop_no_right else loop) t ctx slist Tag.dummy
1160 let run_top_down a tree =
1161 let init = SList.cons a.init SList.nil in
1162 let _,res = top_down a tree Tree.root init Tree.root 1 (CodeCache.create BIG_A_SIZE) (Fold2Res.create 1024)
1165 output_trace a tree "trace.html"
1166 (RS.fold (fun t a -> IntSet.add (Tree.id tree t) a) res.(0) IntSet.empty),
1176 let get = Array.unsafe_get
1179 type fun_tree = [`Tree] Tree.node -> [`Tree] Tree.node -> StateSet.t -> Tag.t -> StateSet.t*RS.t
1180 type t = fun_tree array array
1182 let dummy = fun _ _ _ _ -> failwith "Uninitializd Code3Cache"
1183 let default_line = Array.create 1024 dummy (* 256 = max_tag *)
1184 let create n = Array.create n default_line
1186 for i = 0 to (Array.length default_line) - 1
1188 default_line.(i) <- f
1191 let get_fun h slist tag =
1192 get (get h (Uid.to_int slist.StateSet.Node.id)) tag
1194 let set_fun (h : t) slist tag (data : fun_tree) =
1195 let tab = get h (Uid.to_int slist.StateSet.Node.id) in
1196 let line = if tab == default_line then
1197 let x = Array.copy tab in
1198 (set h (Uid.to_int slist.StateSet.Node.id) x;x)
1207 module Fold3Res = struct
1208 let get = Array.unsafe_get
1210 external field1 : Obj.t -> int = "%field1"
1211 type t = Obj.t array array array array
1212 let dummy_val = Obj.repr ((),2,())
1214 let default_line3 = Array.create 1024 dummy_val
1215 let default_line2 = Array.create BIG_A_SIZE default_line3
1216 let default_line1 = Array.create BIG_A_SIZE default_line2
1218 let create n = Array.create n default_line1
1220 let find h tag fl s1 s2 : StateSet.t*bool*merge_conf =
1221 let l1 = get h (Uid.to_int fl.Formlist.Node.id) in
1222 let l2 = get l1 (Uid.to_int s1.StateSet.Node.id) in
1223 let l3 = get l2 (Uid.to_int s2.StateSet.Node.id) in
1224 Obj.magic (get l3 tag)
1226 let is_valid b = b != (Obj.magic dummy_val)
1227 let get_replace tab idx default =
1228 let e = get tab idx in
1229 if e == default then
1230 let ne = Array.copy e in (set tab idx ne;ne)
1233 let add h tag fl s1 s2 (data: StateSet.t*bool*merge_conf) =
1234 let l1 = get_replace h (Uid.to_int fl.Formlist.Node.id) default_line1 in
1235 let l2 = get_replace l1 (Uid.to_int s1.StateSet.Node.id) default_line2 in
1236 let l3 = get_replace l2 (Uid.to_int s2.StateSet.Node.id) default_line3 in
1237 set l3 tag (Obj.repr data)
1241 let empty_res = StateSet.empty,RS.empty
1243 let top_down1 a tree t slist ctx td_trans h_fold2 =
1244 (* evaluation starts from the right so we put sl1,res1 at the end *)
1245 let eval_fold2_slist fll t tag (sl2,res2) (sl1,res1) =
1246 let data = Fold3Res.find h_fold2 tag fll sl1 sl2 in
1247 if Fold3Res.is_valid data then
1248 let r,b,conf = data in
1249 (r,if b then RS.merge conf t res1 res2 else RS.empty)
1251 let r,conf = eval_formlist tag sl1 sl2 fll in
1252 let b = conf <> NO in
1253 (Fold3Res.add h_fold2 tag fll sl1 sl2 (r,b,conf);
1254 (r, if b then RS.merge conf t res1 res2 else RS.empty))
1257 let rec loop t ctx slist _ =
1258 if t == Tree.nil then empty_res else
1259 let tag = Tree.tag tree t in
1260 (Code3Cache.get_fun td_trans slist tag) t ctx slist tag
1262 and loop_tag t ctx slist tag =
1263 if t == Tree.nil then empty_res else
1264 (Code3Cache.get_fun td_trans slist tag) t ctx slist tag
1266 and mk_trans t ctx slist tag =
1267 let fl_list,llist,rlist,ca,da,sa,fa =
1271 (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
1273 if (TagSet.mem tag ts)
1275 let _,_,_,f,_ = t.Transition.node in
1276 let (child,desc,below),(sibl,foll,after) = Formula.st f in
1277 (Formlist.cons t fl_acc,
1278 StateSet.union ll_acc below,
1279 StateSet.union rl_acc after,
1280 StateSet.union child c_acc,
1281 StateSet.union desc d_acc,
1282 StateSet.union sibl s_acc,
1283 StateSet.union foll f_acc)
1285 try Hashtbl.find a.trans q
1287 Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
1291 ) slist (Formlist.nil,StateSet.empty,StateSet.empty,
1292 StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
1295 (* Logic to chose the first and next function *)
1296 let tags_child,tags_below,tags_siblings,tags_after = Tree.tags tree tag in
1297 let d_f = Algebra.decide a tags_child tags_below (StateSet.union ca da) true in
1298 let d_n = Algebra.decide a tags_siblings tags_after (StateSet.union sa fa) false in
1299 let f_kind,first = choose_jump_down tree d_f
1300 and n_kind,next = choose_jump_next tree d_n in
1303 match f_kind,n_kind with
1305 fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res empty_res
1310 (fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res
1311 (loop_tag (first t) t llist tag1))
1313 fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res
1314 (loop (first t) t llist tag)
1320 eval_fold2_slist fl_list t tag
1321 (loop_tag (next t ctx) ctx rlist tag2)
1326 eval_fold2_slist fl_list t tag
1327 (loop (next t ctx) ctx rlist tag)
1332 | `TAG(tag1),`TAG(tag2) ->
1334 eval_fold2_slist fl_list t tag
1335 (loop_tag (next t ctx) ctx rlist tag2)
1336 (loop_tag (first t) t llist tag1)
1338 | `TAG(tag1),`ANY ->
1340 eval_fold2_slist fl_list t tag
1341 (loop (next t ctx) ctx rlist tag)
1342 (loop_tag (first t) t llist tag1)
1344 | `ANY,`TAG(tag2) ->
1346 eval_fold2_slist fl_list t tag
1347 (loop_tag (next t ctx) ctx rlist tag2)
1348 (loop (first t) t llist tag)
1353 eval_fold2_slist fl_list t tag
1354 (loop (next t ctx) ctx rlist tag)
1355 (loop (first t) t llist tag)
1360 Code3Cache.set_fun td_trans slist tag cont;
1361 cont t ctx slist tag
1363 let _ = Code3Cache.init mk_trans in
1364 loop t ctx slist Tag.dummy
1367 let run_top_down1 a tree =
1368 let _,res = top_down1 a tree Tree.root a.init Tree.root (Code3Cache.create BIG_A_SIZE) (Fold3Res.create BIG_A_SIZE)
1372 module Configuration =
1374 module Ptss = Set.Make(StateSet)
1375 module IMap = Map.Make(StateSet)
1376 type t = { hash : int;
1378 results : RS.t IMap.t }
1379 let empty = { hash = 0;
1381 results = IMap.empty;
1383 let is_empty c = Ptss.is_empty c.sets
1385 if Ptss.mem s c.sets then
1386 { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
1388 { hash = HASHINT2(c.hash,Uid.to_int s.StateSet.Node.id);
1389 sets = Ptss.add s c.sets;
1390 results = IMap.add s r c.results
1393 let pr fmt c = Format.fprintf fmt "{";
1394 Ptss.iter (fun s -> StateSet.print fmt s;
1395 Format.fprintf fmt " ") c.sets;
1396 Format.fprintf fmt "}\n%!";
1397 IMap.iter (fun k d ->
1398 StateSet.print fmt k;
1399 Format.fprintf fmt "-> %i\n" (RS.length d)) c.results;
1400 Format.fprintf fmt "\n%!"
1408 RS.concat r (IMap.find s acc)
1410 | Not_found -> r) acc) c1.results IMap.empty
1413 IMap.fold (fun s r acc ->
1416 RS.concat r (IMap.find s acc)
1418 | Not_found -> r) acc) c2.results acc1
1422 (fun s (ah,ass) -> (HASHINT2(ah, Uid.to_int s.StateSet.Node.id ),
1424 (Ptss.union c1.sets c2.sets) (0,Ptss.empty)
1432 let h_fold = Hashtbl.create 511
1434 let fold_f_conf tree t slist fl_list conf dir=
1435 let tag = Tree.tag tree t in
1436 let rec loop sl fl acc =
1437 match SList.node sl,fl with
1438 |SList.Nil,[] -> acc
1439 |SList.Cons(s,sll), formlist::fll ->
1441 let key = SList.hash sl,Formlist.hash formlist,dir in
1443 Hashtbl.find h_fold key
1445 Not_found -> let res =
1446 if dir then eval_formlist tag s StateSet.empty formlist
1447 else eval_formlist tag StateSet.empty s formlist
1448 in (Hashtbl.add h_fold key res;res)
1450 let (rb,rb1,rb2,mark) = bool_of_merge mcnf in
1451 if rb && ((dir&&rb1)|| ((not dir) && rb2))
1455 try Configuration.IMap.find s conf.Configuration.results
1456 with Not_found -> RS.empty
1458 Configuration.add acc r' (if mark then RS.cons t old_r else old_r)
1461 else loop sll fll acc
1464 loop slist fl_list Configuration.empty
1466 let h_trans = Hashtbl.create 4096
1468 let get_up_trans slist ptag a tree =
1469 let key = (HASHINT2(Uid.to_int slist.SList.Node.id ,ptag)) in
1471 Hashtbl.find h_trans key
1475 Hashtbl.fold (fun q l acc ->
1476 List.fold_left (fun fl_acc (ts,t) ->
1477 if TagSet.mem ptag ts then Formlist.cons t fl_acc
1481 a.trans Formlist.nil
1483 let res = SList.fold (fun _ acc -> f_list::acc) slist []
1485 (Hashtbl.add h_trans key res;res)
1489 let h_tdconf = Hashtbl.create 511
1490 let rec bottom_up a tree t conf next jump_fun root dotd init accu =
1491 if (not dotd) && (Configuration.is_empty conf ) then
1495 let below_right = Tree.is_below_right tree t next in
1497 let accu,rightconf,next_of_next =
1498 if below_right then (* jump to the next *)
1499 bottom_up a tree next conf (jump_fun next) jump_fun (Tree.next_sibling tree t) true init accu
1500 else accu,Configuration.empty,next
1504 if below_right then prepare_topdown a tree t true
1505 else prepare_topdown a tree t false
1509 (Configuration.merge rightconf sub, next_of_next)
1511 if t == root then accu,conf,next else
1512 let parent = Tree.binary_parent tree t in
1513 let ptag = Tree.tag tree parent in
1514 let dir = Tree.is_left tree t in
1515 let slist = Configuration.Ptss.fold (fun e a -> SList.cons e a) conf.Configuration.sets SList.nil in
1516 let fl_list = get_up_trans slist ptag a parent in
1517 let slist = SList.rev (slist) in
1518 let newconf = fold_f_conf tree parent slist fl_list conf dir in
1519 let accu,newconf = Configuration.IMap.fold (fun s res (ar,nc) ->
1520 if StateSet.intersect s init then
1521 ( RS.concat res ar ,nc)
1522 else (ar,Configuration.add nc s res))
1523 (newconf.Configuration.results) (accu,Configuration.empty)
1526 bottom_up a tree parent newconf next jump_fun root false init accu
1528 and prepare_topdown a tree t noright =
1529 let tag = Tree.tag tree t in
1532 Hashtbl.find h_tdconf tag
1535 let res = Hashtbl.fold (fun q l acc ->
1536 if List.exists (fun (ts,_) -> TagSet.mem tag ts) l
1537 then StateSet.add q acc
1538 else acc) a.trans StateSet.empty
1539 in Hashtbl.add h_tdconf tag res;res
1541 (* let _ = pr ", among ";
1542 StateSet.print fmt (Ptset.Int.elements r);
1545 let r = SList.cons r SList.nil in
1546 let set,res = top_down (~noright:noright) a tree t r t 1 (CodeCache.create BIG_A_SIZE) (Fold2Res.create 1024) in
1547 let set = match SList.node set with
1548 | SList.Cons(x,_) ->x
1551 Configuration.add Configuration.empty set res.(0)
1555 let run_bottom_up a tree k =
1556 let t = Tree.root in
1557 let trlist = Hashtbl.find a.trans (StateSet.choose a.init)
1559 let init = List.fold_left
1561 let _,_,_,f,_ = Transition.node t in
1562 let _,_,l = fst ( Formula.st f ) in
1563 StateSet.union acc l)
1564 StateSet.empty trlist
1566 let tree1,jump_fun =
1569 (*Tree.tagged_lowest t tag, fun tree -> Tree.tagged_next tree tag*)
1570 (Tree.tagged_descendant tree tag t, let jump = Tree.tagged_following_below tree tag
1571 in fun n -> jump n t )
1572 | `CONTAINS(_) -> (Tree.text_below tree t,let jump = Tree.text_next tree
1573 in fun n -> jump n t)
1576 let tree2 = jump_fun tree1 in
1577 let rec loop t next acc =
1578 let acc,conf,next_of_next = bottom_up a tree t
1579 Configuration.empty next jump_fun (Tree.root) true init acc
1581 let acc = Configuration.IMap.fold
1582 ( fun s res acc -> if StateSet.intersect init s
1583 then RS.concat res acc else acc) conf.Configuration.results acc
1585 if Tree.is_nil next_of_next (*|| Tree.equal next next_of_next *)then
1587 else loop next_of_next (jump_fun next_of_next) acc
1589 loop tree1 tree2 RS.empty
1594 let top_down_count a t = let module RI = Run(Integer) in Integer.length (RI.run_top_down a t)
1595 let top_down_count1 a t = let module RI = Run(Integer) in Integer.length (RI.run_top_down1 a t)
1596 let top_down a t = let module RI = Run(IdSet) in (RI.run_top_down a t)
1597 let top_down1 a t = let module RI = Run(IdSet) in (RI.run_top_down1 a t)
1598 let bottom_up_count a t k = let module RI = Run(Integer) in Integer.length (RI.run_bottom_up a t k)
1599 let bottom_up a t k = let module RI = Run(IdSet) in (RI.run_bottom_up a t k)
1601 module Test (Doc : sig val doc : Tree.t end) =
1603 module Results = GResult(Doc)
1604 let top_down a t = let module R = Run(Results) in (R.run_top_down a t)
1605 let top_down1 a t = let module R = Run(Results) in (R.run_top_down1 a t)