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*TagSet.t*bool*Formula.t*bool
198 include Hcons.Make(struct
200 let hash (s,ts,m,f,b) = HASHINT5(s,TagSet.uid ts,Formula.uid f,vb m,vb b)
201 let equal (s,ts,b,f,m) (s',ts',b',f',m') =
202 s == s' && ts == ts' && b==b' && m==m' && f == f'
205 let print ppf f = let (st,ts,mark,form,b) = node f in
206 Format.fprintf ppf "(%i, " st;
208 Format.fprintf ppf ") %s" (if mark then "⇒" else "→");
209 Formula.print ppf form;
210 Format.fprintf ppf "%s%!" (if b then " (b)" else "")
213 module Infix = struct
215 let ( >< ) state (l,mark) = state,(l,mark,false)
216 let ( ><@ ) state (l,mark) = state,(l,mark,true)
217 let ( >=> ) (state,(label,mark,bur)) form = (state,label,(make (state,label,mark,form,bur)))
222 module TransTable = Hashtbl
224 module Formlist = struct
225 include Hlist.Make(Transition)
227 iter (fun t -> Transition.print ppf t; Format.pp_print_newline ppf ()) fl
230 module Formlistlist =
232 include Hlist.Make(Formlist)
234 iter (fun fl -> Formlist.print ppf fl; Format.pp_print_newline ppf ())fll
239 mutable states : Ptset.Int.t;
241 starstate : Ptset.Int.t option;
242 (* Transitions of the Alternating automaton *)
243 trans : (State.t,(TagSet.t*Transition.t) list) Hashtbl.t;
244 query_string: string;
249 Format.fprintf ppf "Automaton (%i) :\n" a.id;
250 Format.fprintf ppf "States : "; StateSet.print ppf a.states;
251 Format.fprintf ppf "\nInitial states : "; StateSet.print ppf a.init;
252 Format.fprintf ppf "\nAlternating transitions :\n";
253 let l = Hashtbl.fold (fun k t acc ->
254 (List.map (fun (ts,tr) -> (ts,k),Transition.node tr) t) @ acc) a.trans [] in
255 let l = List.sort (fun ((tsx,x),_) ((tsy,y),_) ->
256 if y-x == 0 then TagSet.compare tsy tsx else y-x) l in
257 let maxh,maxt,l_print =
259 fun (maxh,maxt,l) ((ts,q),(_,_,b,f,_)) ->
261 if TagSet.is_finite ts
262 then "{" ^ (TagSet.fold (fun t a -> a ^ " '" ^ (Tag.to_string t)^"'") ts "") ^" }"
263 else let cts = TagSet.neg ts in
264 if TagSet.is_empty cts then "*" else
265 (TagSet.fold (fun t a -> a ^ " " ^ (Tag.to_string t)) cts "*\\{"
268 let s = Printf.sprintf "(%s,%i)" s q in
270 Formula.print Format.str_formatter f;
271 Format.flush_str_formatter()
273 (max (String.length s) maxh, max (String.length s_frm) maxt,
274 (s,(if b then "⇒" else "→"),s_frm)::l)) (0,0,[]) l
276 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_');
277 List.iter (fun (s,m,f) -> let s = s ^ (String.make (maxh-(String.length s)) ' ') in
278 Format.fprintf ppf "%s %s %s\n" s m f) l_print;
279 Format.fprintf ppf "%s\n%!" (String.make (maxt+maxh+3) '_')
282 module FormTable = Hashtbl.Make(struct
283 type t = Formula.t*StateSet.t*StateSet.t
284 let equal (f1,s1,t1) (f2,s2,t2) =
285 f1 == f2 && s1 == s2 && t1 == t2
287 HASHINT3(Formula.uid f ,StateSet.uid s,StateSet.uid t)
292 let h_f = FormTable.create BIG_H_SIZE in
296 | F.True -> true,true,true
297 | F.False -> false,false,false
298 | F.Atom((`Left|`LLeft),b,q) ->
299 if b == (StateSet.mem q s1)
300 then (true,true,false)
301 else false,false,false
303 if b == (StateSet.mem q s2)
304 then (true,false,true)
305 else false,false,false
307 try FormTable.find h_f (f,s1,s2)
308 with Not_found -> let r =
311 let b1,rl1,rr1 = loop f1
313 if b1 && rl1 && rr1 then (true,true,true) else
314 let b2,rl2,rr2 = loop f2 in
315 let rl1,rr1 = if b1 then rl1,rr1 else false,false
316 and rl2,rr2 = if b2 then rl2,rr2 else false,false
317 in (b1 || b2, rl1||rl2,rr1||rr2)
320 let b1,rl1,rr1 = loop f1 in
321 if b1 && rl1 && rr1 then (true,true,true) else
323 let b2,rl2,rr2 = loop f2 in
324 if b2 then (true,rl1||rl2,rr1||rr2) else (false,false,false)
325 else (false,false,false)
327 in FormTable.add h_f (f,s1,s2) r;r
331 module FTable = Hashtbl.Make(struct
332 type t = Tag.t*Formlist.t*StateSet.t*StateSet.t
333 let equal (tg1,f1,s1,t1) (tg2,f2,s2,t2) =
334 tg1 == tg2 && f1 == f2 && s1 == s2 && t1 == t2;;
335 let hash (tg,f,s,t) = HASHINT4(tg,Formlist.uid f ,StateSet.uid s,StateSet.uid t);;
339 let h_f = FTable.create BIG_H_SIZE
340 type merge_conf = NO | MARK | ONLY1 | ONLY2 | ONLY12 | MARK1 | MARK2 | MARK12
342 let eval_formlist tag s1 s2 fl =
345 FTable.find h_f (tag,fl,s1,s2)
348 match Formlist.node fl with
349 | Formlist.Cons(f,fll) ->
350 let q,ts,mark,f,_ = Transition.node f in
352 if TagSet.mem tag ts then eval_form_bool f s1 s2 else (false,false,false)
354 let (s,(b',b1',b2',amark)) as res = loop fll in
355 let r = if b then (StateSet.add q s, (b, b1'||b1,b2'||b2,mark||amark))
357 in FTable.add h_f (tag,fl,s1,s2) r;r
358 | Formlist.Nil -> StateSet.empty,(false,false,false,false)
363 | (false,_,_,_) -> NO
364 | (_,false,false,false) -> NO
365 | (_,true,false,false) -> ONLY1
366 | (_,false,true,false) -> ONLY2
367 | (_,true,true,false) -> ONLY12
368 | (_,false,false,true) -> MARK
369 | (_,true,false,true) -> MARK1
370 | (_,false,true,true) -> MARK2
373 let bool_of_merge conf =
375 | NO -> false,false,false,false
376 | ONLY1 -> true,true,false,false
377 | ONLY2 -> true,false,true,false
378 | ONLY12 -> true,true,true,false
379 | MARK -> true,false,false,true
380 | MARK1 -> true,true,false,true
381 | MARK2 -> true,false,true,true
382 | MARK12 -> true,true,true,true
385 let tags_of_state a q =
388 if p == q then List.fold_left
390 let _,_,_,_,aux = Transition.node t in
392 TagSet.cup ts acc) acc l
394 else acc) a.trans TagSet.empty
399 let ts = Ptset.Int.fold (fun q acc -> TagSet.cup acc (tags_of_state a q)) qs TagSet.empty
401 if TagSet.is_finite ts
402 then `Positive(TagSet.positive ts)
403 else `Negative(TagSet.negative ts)
407 | `Positive s -> let r = Ptset.Int.inter a s in (r,Ptset.Int.mem Tag.pcdata r, true)
408 | `Negative s -> let r = Ptset.Int.diff a s in (r, Ptset.Int.mem Tag.pcdata r, false)
411 module type ResultSet =
414 type elt = [` Tree ] Tree.node
416 val cons : elt -> t -> t
417 val concat : t -> t -> t
418 val iter : ( elt -> unit) -> t -> unit
419 val fold : ( elt -> 'a -> 'a) -> t -> 'a -> 'a
420 val map : ( elt -> elt) -> t -> t
421 val length : t -> int
422 val merge : merge_conf -> elt -> t -> t -> t
423 val mk_quick_tag_loop : (elt -> elt -> 'a*t array) -> 'a -> int -> Tree.t -> Tag.t -> (elt -> elt -> 'a*t array)
424 val mk_quick_star_loop : (elt -> elt -> 'a*t array) -> 'a -> int -> Tree.t -> (elt -> elt -> 'a*t array)
427 module Integer : ResultSet =
430 type elt = [`Tree] Tree.node
434 let concat x y = x + y
435 let iter _ _ = failwith "iter not implemented"
436 let fold _ _ _ = failwith "fold not implemented"
437 let map _ _ = failwith "map not implemented"
439 let merge2 conf t res1 res2 =
440 let rb,rb1,rb2,mark = conf in
442 let res1 = if rb1 then res1 else 0
443 and res2 = if rb2 then res2 else 0
445 if mark then 1+res1+res2
448 let merge conf t res1 res2 =
452 | ONLY12 -> res1+res2
455 | MARK12 -> res1+res2+1
459 let mk_quick_tag_loop _ sl ss tree tag = ();
461 (sl, Array.make ss (Tree.subtree_tags tree tag t))
462 let mk_quick_star_loop _ sl ss tree = ();
464 (sl, Array.make ss (Tree.subtree_elements tree t))
468 module IdSet : ResultSet=
470 type elt = [`Tree] Tree.node
473 | Concat of node*node
475 and t = { node : node;
478 let empty = { node = Nil; length = 0 }
480 let cons e t = { node = Cons(e,t.node); length = t.length+1 }
481 let concat t1 t2 = { node = Concat(t1.node,t2.node); length = t1.length+t2.length }
482 let append e t = { node = Concat(t.node,Cons(e,Nil)); length = t.length+1 }
485 let rec loop acc t = match t with
487 | Cons (e,t) -> loop (f e acc) t
488 | Concat (t1,t2) -> loop (loop acc t1) t2
492 let length l = l.length
496 let rec loop = function
498 | Cons (e,t) -> f e; loop t
499 | Concat(t1,t2) -> loop t1;loop t2
503 let rec loop = function
505 | Cons(e,t) -> Cons(f e, loop t)
506 | Concat(t1,t2) -> Concat(loop t1,loop t2)
508 { l with node = loop l.node }
510 let merge conf t res1 res2 =
513 | MARK -> cons t empty
516 | ONLY12 -> { node = (Concat(res1.node,res2.node));
517 length = res1.length + res2.length ;}
518 | MARK12 -> { node = Cons(t,(Concat(res1.node,res2.node)));
519 length = res1.length + res2.length + 1;}
520 | MARK1 -> { node = Cons(t,res1.node);
521 length = res1.length + 1;}
522 | MARK2 -> { node = Cons(t,res2.node);
523 length = res2.length + 1;}
525 let mk_quick_tag_loop f _ _ _ _ = f
526 let mk_quick_star_loop f _ _ _ = f
528 module GResult(Doc : sig val doc : Tree.t end) = struct
530 type elt = [` Tree] Tree.node
531 external create_empty : int -> bits = "caml_result_set_create"
532 external set : bits -> int -> unit = "caml_result_set_set"
533 external next : bits -> int -> int = "caml_result_set_next"
534 external clear : bits -> elt -> elt -> unit = "caml_result_set_clear"
535 external set_tag_bits : bits -> Tag.t -> Tree.t -> elt -> elt = "caml_set_tag_bits"
537 { segments : elt list;
542 let size = (Tree.subtree_size Doc.doc Tree.root) in
543 create_empty (size*2+1)
545 let empty = { segments = [];
549 let rec loop l = match l with
550 | [] -> { bits = (set t.bits (Obj.magic e);t.bits);
553 if Tree.is_binary_ancestor Doc.doc e p then
556 { bits = (set t.bits (Obj.magic e);t.bits);
562 if t2.segments == [] then t1
564 if t1.segments == [] then t2
566 let h2 = List.hd t2.segments in
567 let rec loop l = match l with
570 if Tree.is_binary_ancestor Doc.doc p h2 then
576 segments = loop t1.segments
582 else (f ((Obj.magic i):elt);loop (next t.bits i))
583 in loop (next t.bits 0)
588 else loop (next t.bits i) (f ((Obj.magic i):elt) acc)
589 in loop (next t.bits 0) acc
591 let map _ _ = failwith "noop"
592 let length t = let cpt = ref 0 in
593 iter (fun _ -> incr cpt) t; !cpt
596 let rec loop l = match l with
599 clear t.bits idx (Tree.closing Doc.doc idx); loop ll
601 loop t.segments;empty
603 let merge (rb,rb1,rb2,mark) elt t1 t2 =
605 (* let _ = Printf.eprintf "Lenght before merging is %i %i\n"
606 (List.length t1.segments) (List.length t2.segments)
608 match t1.segments,t2.segments with
609 [],[] -> if mark then cons elt empty else empty
610 | [_],[] when rb1 -> if mark then cons elt t1 else t1
611 | [], [_] when rb2 -> if mark then cons elt t2 else t2
612 | [_],[_] when rb1 && rb2 -> if mark then cons elt empty else
615 let t1 = if rb1 then t1 else clear_bits t1
616 and t2 = if rb2 then t2 else clear_bits t2
618 (if mark then cons elt (concat t1 t2)
621 let _ = clear_bits t1 in
624 let merge conf t t1 t2 =
625 match t1.segments,t2.segments,conf with
626 | _,_,NO -> let _ = clear_bits t1 in clear_bits t2
627 | [],[],(MARK1|MARK2|MARK12|MARK) -> cons t empty
629 | [_],[],(ONLY1|ONLY12) -> t1
630 | [_],[],(MARK1|MARK12) -> cons t t1
631 | [],[_],(ONLY2|ONLY12) -> t2
632 | [],[_],(MARK2|MARK12) -> cons t t2
633 | [_],[_],ONLY12 -> concat t1 t2
634 | [_],[_],MARK12 -> cons t empty
635 | _,_,MARK -> let _ = clear_bits t2 in cons t (clear_bits t1)
636 | _,_,ONLY1 -> let _ = clear_bits t2 in t1
637 | _,_,ONLY2 -> let _ = clear_bits t1 in t2
638 | _,_,ONLY12 -> concat t1 t2
639 | _,_,MARK1 -> let _ = clear_bits t2 in cons t t1
640 | _,_,MARK2 -> let _ = clear_bits t1 in cons t t2
641 | _,_,MARK12 -> cons t (concat t1 t2)
643 let mk_quick_tag_loop _ sl ss tree tag = ();
646 let first = set_tag_bits empty.bits tag tree t in
648 if first == Tree.nil then res else
651 (sl, Array.make ss res)
653 let mk_quick_star_loop f _ _ _ = f
655 module Run (RS : ResultSet) =
658 module SList = Hlist.Make (StateSet)
664 module IntSet = Set.Make(struct type t = int let compare = (-) end)
665 INCLUDE "html_trace.ml"
668 let mk_fun f s = D_IGNORE_(register_funname f s,f)
669 let mk_app_fun f arg s = let g = f arg in
670 D_IGNORE_(register_funname g ((get_funname f) ^ " " ^ s), g)
671 let mk_app_fun2 f arg1 arg2 s = let g = f arg1 arg2 in
672 D_IGNORE_(register_funname g ((get_funname f) ^ " " ^ s), g)
674 let string_of_ts tags = (Ptset.Int.fold (fun t a -> a ^ " " ^ (Tag.to_string t) ) tags "{")^ " }"
679 type jump = [ `NIL | `ANY |`ANYNOTEXT | `JUMP ]
680 type t = jump*Ptset.Int.t*Ptset.Int.t
685 | `ANYNOTEXT -> "ANYNOTEXT"
686 let merge_jump (j1,c1,l1) (j2,c2,l2) =
688 | _,`NIL -> (j1,c1,l1)
689 | `NIL,_ -> (j2,c2,l2)
690 | `ANY,_ -> (`ANY,Ptset.Int.empty,Ptset.Int.empty)
691 | _,`ANY -> (`ANY,Ptset.Int.empty,Ptset.Int.empty)
693 if Ptset.Int.mem Tag.pcdata (Ptset.Int.union c2 l2) then
694 (`ANY,Ptset.Int.empty,Ptset.Int.empty)
696 (`ANYNOTEXT,Ptset.Int.empty,Ptset.Int.empty)
698 if Ptset.Int.mem Tag.pcdata (Ptset.Int.union c1 l1) then
699 (`ANY,Ptset.Int.empty,Ptset.Int.empty)
701 (`ANYNOTEXT,Ptset.Int.empty,Ptset.Int.empty)
702 | `JUMP,`JUMP -> (`JUMP, Ptset.Int.union c1 c2,Ptset.Int.union l1 l2)
704 let merge_jump_list = function
705 | [] -> `NIL,Ptset.Int.empty,Ptset.Int.empty
707 List.fold_left (merge_jump) p r
718 let _,_,_,_,bur = Transition.node f in
719 if bur then acc else TagSet.cup acc ts)
721 else acc ) a.trans TagSet.empty
724 let is_rec a s access =
726 (fun (_,t) -> let _,_,_,f,_ = Transition.node t in
727 StateSet.mem s ((fun (_,_,x) -> x) (access (Formula.st f)))) (Hashtbl.find a.trans s)
729 let is_final_marking a s =
730 List.exists (fun (_,t) -> let _,_,m,f,_ = Transition.node t in m&& (Formula.is_true f))
731 (Hashtbl.find a.trans s)
734 let decide a c_label l_label dir_states dir =
736 let l = StateSet.fold
738 let s_rec = is_rec a s (if dir then fst else snd) in
739 let s_rec = if dir then s_rec else
743 let s_lab = labels a s in
745 if (not (TagSet.is_finite s_lab)) then
746 if TagSet.mem Tag.pcdata s_lab then (`ANY,Ptset.Int.empty,Ptset.Int.empty)
747 else (`ANYNOTEXT,Ptset.Int.empty,Ptset.Int.empty)
750 then (`JUMP,Ptset.Int.empty, TagSet.positive
751 (TagSet.cap (TagSet.inj_positive l_label) s_lab))
752 else (`JUMP,TagSet.positive
753 (TagSet.cap (TagSet.inj_positive c_label) s_lab),
758 && Ptset.Int.is_empty cc
759 && Ptset.Int.is_empty ll
760 then (`NIL,Ptset.Int.empty,Ptset.Int.empty)
761 else (jmp,cc,ll))::l) dir_states []
769 let choose_jump (d,cl,ll) f_nil f_t1 f_s1 f_tn f_sn f_s1n f_notext f_maytext =
771 | `NIL -> (`NIL,f_nil)
772 | `ANYNOTEXT -> `ANY,f_notext
773 | `ANY -> `ANY,f_maytext
775 if Ptset.Int.is_empty cl then
776 if Ptset.Int.is_singleton ll then
777 let tag = Ptset.Int.choose ll in
778 (`TAG(tag),mk_app_fun f_tn tag (Tag.to_string tag))
780 (`MANY(ll),mk_app_fun f_sn ll (string_of_ts ll))
781 else if Ptset.Int.is_empty ll then
782 if Ptset.Int.is_singleton cl then
783 let tag = Ptset.Int.choose cl in
784 (`TAG(tag),mk_app_fun f_t1 tag (Tag.to_string tag))
786 (`MANY(cl),mk_app_fun f_s1 cl (string_of_ts cl))
788 (`ANY,mk_app_fun2 f_s1n cl ll ((string_of_ts cl) ^ " " ^ (string_of_ts ll)))
792 let choose_jump_down tree d =
794 (mk_fun (fun _ -> Tree.nil) "Tree.mk_nil")
795 (mk_fun (Tree.tagged_child tree) "Tree.tagged_child")
796 (mk_fun (Tree.select_child tree) "Tree.select_child")
797 (mk_fun (Tree.tagged_desc tree) "Tree.tagged_desc")
798 (mk_fun (Tree.select_desc tree) "Tree.select_desc")
799 (mk_fun (fun _ _ -> Tree.first_child tree) "[FIRSTCHILD]Tree.select_child_desc")
800 (mk_fun (Tree.first_element tree) "Tree.first_element")
801 (mk_fun (Tree.first_child tree) "Tree.first_child")
803 let choose_jump_next tree d =
805 (mk_fun (fun _ _ -> Tree.nil) "Tree.mk_nil2")
806 (mk_fun (Tree.tagged_sibling_ctx tree) "Tree.tagged_sibling_ctx")
807 (mk_fun (Tree.select_sibling_ctx tree) "Tree.select_sibling_ctx")
808 (mk_fun (Tree.tagged_foll_ctx tree) "Tree.tagged_foll_ctx")
809 (mk_fun (Tree.select_foll_ctx tree) "Tree.select_foll_ctx")
810 (mk_fun (fun _ _ -> Tree.next_sibling_ctx tree) "[NEXTSIBLING]Tree.select_sibling_foll_ctx")
811 (mk_fun (Tree.next_element_ctx tree) "Tree.next_element_ctx")
812 (mk_fun (Tree.next_sibling_ctx tree) "Tree.node_sibling_ctx")
815 module SListTable = Hashtbl.Make(struct type t = SList.t
817 let hash t = t.SList.Node.id
821 module TransCacheOld =
823 type 'a t = Obj.t array SListTable.t
824 let create n = SListTable.create n
825 let dummy = Obj.repr (fun _ -> assert false)
826 let find (h :'a t) tag slist : 'a =
829 SListTable.find h slist
832 SListTable.add h slist (Array.create 10000 dummy);
835 let res = tab.(tag) in
836 if res == dummy then raise Not_found else (Obj.magic res)
838 let add (h : 'a t) tag slist (data : 'a) =
841 SListTable.find h slist
844 let arr = Array.create 10000 dummy in
845 SListTable.add h slist arr;
848 tab.(tag) <- (Obj.repr data)
855 external get : 'a array -> int ->'a = "%array_unsafe_get"
856 external set : 'a array -> int -> 'a -> unit = "%array_unsafe_set"
857 type fun_tree = [`Tree] Tree.node -> [`Tree] Tree.node -> SList.t*RS.t array
858 type t = fun_tree array array
859 let dummy_cell = [||]
860 let create n = Array.create n dummy_cell
861 let dummy = fun _ _-> assert false
862 let find h tag slist =
863 let tab = get h slist.SList.Node.id in
864 if tab == dummy_cell then raise Not_found
866 let res = get tab tag in
867 if res == dummy then raise Not_found else res
869 let add (h : t) tag slist (data : fun_tree) =
870 let tab = get h slist.SList.Node.id in
871 let tab = if tab == dummy_cell then
872 let x = Array.create 10000 dummy in
873 (set h slist.SList.Node.id x;x)
879 let td_trans = TransCache.create 10000 (* should be number of tags *number of states^2
883 let rec loop acc = function 0 -> acc
884 | n -> loop (SList.cons StateSet.empty acc) (n-1)
887 module FllTable = Hashtbl.Make (struct type t = Formlistlist.t
889 let hash t = t.Formlistlist.Node.id
894 type 'a t = 'a SListTable.t SListTable.t FllTable.t
895 let create n = Array.init 10000 (fun _ -> FllTable.create n)
897 let find h tag fl s1 s2 =
899 let hs1 = FllTable.find hf fl in
900 let hs2 = SListTable.find hs1 s1 in
901 SListTable.find hs2 s2
903 let add h tag fl s1 s2 data =
906 try FllTable.find hf fl with
908 let hs1 = SListTable.create SMALL_H_SIZE
909 in FllTable.add hf fl hs1;hs1
912 try SListTable.find hs1 s1
915 let hs2 = SListTable.create SMALL_H_SIZE
916 in SListTable.add hs1 s1 hs2;hs2
918 SListTable.add hs2 s2 data
921 module Fold2Res = struct
922 external get : 'a array -> int ->'a = "%array_unsafe_get"
923 external set : 'a array -> int -> 'a -> unit = "%array_unsafe_set"
924 external field1 : 'a -> 'b = "%field1"
925 type 'a t = 'a array array array array
928 let v = Obj.repr ((),2,()) in
931 let create n = Array.create n dummy
933 let find h tag fl s1 s2 =
934 let af = get h tag in
935 if af == dummy then raise Not_found
937 let as1 = get af fl.Formlistlist.Node.id in
938 if as1 == dummy then raise Not_found
940 let as2 = get as1 s1.SList.Node.id in
941 if as2 == dummy then raise Not_found
942 else let v = get as2 s2.SList.Node.id in
943 if field1 v == 2 then raise Not_found
946 let add h tag fl s1 s2 data =
951 let y = Array.make 10000 dummy in
957 let x = get af fl.Formlistlist.Node.id in
960 let y = Array.make 10000 dummy in
961 set af fl.Formlistlist.Node.id y;y
966 let x = get as1 s1.SList.Node.id in
969 let y = Array.make 10000 dummy_val in
970 set as1 s1.SList.Node.id y;y
974 set as2 s2.SList.Node.id data
978 let h_fold2 = Fold2Res.create 10000
980 let top_down ?(noright=false) a tree t slist ctx slot_size =
981 let pempty = empty_size slot_size in
982 let rempty = Array.make slot_size RS.empty in
983 (* evaluation starts from the right so we put sl1,res1 at the end *)
984 let eval_fold2_slist fll t tag (sl2,res2) (sl1,res1) =
985 let res = Array.copy rempty in
987 let r,b,btab = Fold2Res.find h_fold2 tag fll sl1 sl2 in
988 if b then for i=0 to slot_size - 1 do
989 res.(i) <- RS.merge btab.(i) t res1.(i) res2.(i);
994 let btab = Array.make slot_size NO in
995 let rec fold l1 l2 fll i aq ab =
996 match fll.Formlistlist.Node.node,
1000 | Formlistlist.Cons(fl,fll),
1002 SList.Cons(s2,ll2) ->
1003 let r',conf = eval_formlist tag s1 s2 fl in
1004 let _ = btab.(i) <- conf
1006 fold ll1 ll2 fll (i+1) (SList.cons r' aq) ((conf!=NO)||ab)
1009 let r,b = fold sl1 sl2 fll 0 SList.nil false in
1010 Fold2Res.add h_fold2 tag fll sl1 sl2 (r,b,btab);
1011 if b then for i=0 to slot_size - 1 do
1012 res.(i) <- RS.merge btab.(i) t res1.(i) res2.(i);
1017 let null_result = (pempty,Array.copy rempty) in
1018 let rec loop t slist ctx =
1019 if t == Tree.nil then null_result else get_trans t slist (Tree.tag tree t) ctx
1020 and loop_tag tag t slist ctx =
1021 if t == Tree.nil then null_result else get_trans t slist tag ctx
1022 and loop_no_right t slist ctx =
1023 if t == Tree.nil then null_result else get_trans ~noright:true t slist (Tree.tag tree t) ctx
1024 and get_trans ?(noright=false) t slist tag ctx =
1027 TransCache.find td_trans tag slist
1030 let fl_list,llist,rlist,ca,da,sa,fa =
1032 (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
1033 let fl,ll,rr,ca,da,sa,fa =
1037 (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
1039 if (TagSet.mem tag ts)
1041 let _,_,_,f,_ = Transition.node t in
1042 let (child,desc,below),(sibl,foll,after) = Formula.st f in
1043 (Formlist.cons t fl_acc,
1044 StateSet.union ll_acc below,
1045 StateSet.union rl_acc after,
1046 StateSet.union child c_acc,
1047 StateSet.union desc d_acc,
1048 StateSet.union sibl s_acc,
1049 StateSet.union foll f_acc)
1051 try Hashtbl.find a.trans q
1053 Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
1057 ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
1058 in (Formlistlist.cons fl fll_acc), (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
1059 slist (Formlistlist.nil,SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
1061 (* Logic to chose the first and next function *)
1062 let tags_child,tags_below,tags_siblings,tags_after = Tree.tags tree tag in
1063 let d_f = Algebra.decide a tags_child tags_below (StateSet.union ca da) true in
1064 let d_n = Algebra.decide a tags_siblings tags_after (StateSet.union sa fa) false in
1065 let f_kind,first = choose_jump_down tree d_f
1066 and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
1067 else choose_jump_next tree d_n in
1068 let empty_res = null_result in
1070 match f_kind,n_kind with
1072 Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__);
1073 (fun t _ -> eval_fold2_slist fl_list t (Tree.tag tree t) empty_res empty_res)
1077 let default = fun t _ -> eval_fold2_slist fl_list t (Tree.tag tree t) empty_res
1078 (loop_tag tag' (first t) llist t )
1080 let cf = SList.hd llist in
1081 if (slot_size == 1) && StateSet.is_singleton cf
1083 let s = StateSet.choose cf in
1084 if (Algebra.is_rec a s fst) && (Algebra.is_rec a s snd)
1085 && (Algebra.is_final_marking a s)
1087 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1088 RS.mk_quick_tag_loop default llist 1 tree tag'
1090 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1093 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1096 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1097 (fun t _ -> eval_fold2_slist fl_list t (Tree.tag tree t) empty_res
1098 (loop (first t) llist t ))
1103 if SList.equal rlist slist && tag == tag' then
1104 let rec loop t ctx =
1105 if t == Tree.nil then empty_res else
1106 let res2 = loop (next t ctx) ctx in
1107 eval_fold2_slist fl_list t tag res2 empty_res
1108 in Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__);loop
1110 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1111 (fun t ctx -> eval_fold2_slist fl_list t (Tree.tag tree t)
1112 (loop_tag tag' (next t ctx) rlist ctx ) empty_res)
1115 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1116 (fun t ctx -> eval_fold2_slist fl_list t (Tree.tag tree t)
1117 (loop (next t ctx) rlist ctx ) empty_res)
1120 | `TAG(tag1),`TAG(tag2) ->
1121 let _ = Printf.eprintf "Using %i %s %s\n" (Loc.start_line __LOCATION__)
1122 (Tag.to_string tag1)
1123 (Tag.to_string tag2)
1126 eval_fold2_slist fl_list t (Tree.tag tree t)
1127 (loop_tag tag2 (next t ctx) rlist ctx )
1128 (loop_tag tag1 (first t) llist t ))
1130 | `TAG(tag'),`ANY ->
1131 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1133 eval_fold2_slist fl_list t (Tree.tag tree t)
1134 (loop (next t ctx) rlist ctx )
1135 (loop_tag tag' (first t) llist t ))
1137 | `ANY,`TAG(tag') ->
1138 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1140 eval_fold2_slist fl_list t (Tree.tag tree t)
1141 (loop_tag tag' (next t ctx) rlist ctx )
1142 (loop (first t) llist t ))
1145 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1146 if SList.equal slist rlist && SList.equal slist llist
1148 let rec loop t ctx =
1149 if t == Tree.nil then empty_res else
1150 let r1 = loop (first t) t
1151 and r2 = loop (next t ctx) ctx
1153 eval_fold2_slist fl_list t (Tree.tag tree t) r2 r1
1155 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1158 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1160 eval_fold2_slist fl_list t (Tree.tag tree t)
1161 (loop (next t ctx) rlist ctx )
1162 (loop (first t) llist t ))
1164 let _ = Printf.eprintf "Using %i\n" (Loc.start_line __LOCATION__) in
1166 eval_fold2_slist fl_list t (Tree.tag tree t)
1167 (loop (next t ctx) rlist ctx )
1168 (loop (first t) llist t ))
1171 let cont = D_IF_( (fun t ctx ->
1172 let a,b = cont t ctx in
1173 register_trace tree t (slist,a,fl_list,first,next,ctx);
1177 (TransCache.add td_trans tag slist cont ;cont)
1181 (if noright then loop_no_right else loop) t slist ctx
1183 let run_top_down a tree =
1184 let init = SList.cons a.init SList.nil in
1185 let _,res = top_down a tree Tree.root init Tree.root 1
1188 output_trace a tree "trace.html"
1189 (RS.fold (fun t a -> IntSet.add (Tree.id tree t) a) res.(0) IntSet.empty),
1193 module Configuration =
1195 module Ptss = Set.Make(StateSet)
1196 module IMap = Map.Make(StateSet)
1197 type t = { hash : int;
1199 results : RS.t IMap.t }
1200 let empty = { hash = 0;
1202 results = IMap.empty;
1204 let is_empty c = Ptss.is_empty c.sets
1206 if Ptss.mem s c.sets then
1207 { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
1209 { hash = HASHINT2(c.hash,Ptset.Int.uid s);
1210 sets = Ptss.add s c.sets;
1211 results = IMap.add s r c.results
1214 let pr fmt c = Format.fprintf fmt "{";
1215 Ptss.iter (fun s -> StateSet.print fmt s;
1216 Format.fprintf fmt " ") c.sets;
1217 Format.fprintf fmt "}\n%!";
1218 IMap.iter (fun k d ->
1219 StateSet.print fmt k;
1220 Format.fprintf fmt "-> %i\n" (RS.length d)) c.results;
1221 Format.fprintf fmt "\n%!"
1229 RS.concat r (IMap.find s acc)
1231 | Not_found -> r) acc) c1.results IMap.empty
1234 IMap.fold (fun s r acc ->
1237 RS.concat r (IMap.find s acc)
1239 | Not_found -> r) acc) c2.results acc1
1243 (fun s (ah,ass) -> (HASHINT2(ah,Ptset.Int.uid s),
1245 (Ptss.union c1.sets c2.sets) (0,Ptss.empty)
1253 let h_fold = Hashtbl.create 511
1255 let fold_f_conf tree t slist fl_list conf dir=
1256 let tag = Tree.tag tree t in
1257 let rec loop sl fl acc =
1258 match SList.node sl,fl with
1259 |SList.Nil,[] -> acc
1260 |SList.Cons(s,sll), formlist::fll ->
1262 let key = SList.hash sl,Formlist.hash formlist,dir in
1264 Hashtbl.find h_fold key
1266 Not_found -> let res =
1267 if dir then eval_formlist tag s Ptset.Int.empty formlist
1268 else eval_formlist tag Ptset.Int.empty s formlist
1269 in (Hashtbl.add h_fold key res;res)
1271 let (rb,rb1,rb2,mark) = bool_of_merge mcnf in
1272 if rb && ((dir&&rb1)|| ((not dir) && rb2))
1276 try Configuration.IMap.find s conf.Configuration.results
1277 with Not_found -> RS.empty
1279 Configuration.add acc r' (if mark then RS.cons t old_r else old_r)
1282 else loop sll fll acc
1285 loop slist fl_list Configuration.empty
1287 let h_trans = Hashtbl.create 4096
1289 let get_up_trans slist ptag a tree =
1290 let key = (HASHINT2(SList.uid slist,ptag)) in
1292 Hashtbl.find h_trans key
1296 Hashtbl.fold (fun q l acc ->
1297 List.fold_left (fun fl_acc (ts,t) ->
1298 if TagSet.mem ptag ts then Formlist.cons t fl_acc
1302 a.trans Formlist.nil
1304 let res = SList.fold (fun _ acc -> f_list::acc) slist []
1306 (Hashtbl.add h_trans key res;res)
1310 let h_tdconf = Hashtbl.create 511
1311 let rec bottom_up a tree t conf next jump_fun root dotd init accu =
1312 if (not dotd) && (Configuration.is_empty conf ) then
1316 let below_right = Tree.is_below_right tree t next in
1318 let accu,rightconf,next_of_next =
1319 if below_right then (* jump to the next *)
1320 bottom_up a tree next conf (jump_fun next) jump_fun (Tree.next_sibling tree t) true init accu
1321 else accu,Configuration.empty,next
1325 if below_right then prepare_topdown a tree t true
1326 else prepare_topdown a tree t false
1330 (Configuration.merge rightconf sub, next_of_next)
1332 if t == root then accu,conf,next else
1333 let parent = Tree.binary_parent tree t in
1334 let ptag = Tree.tag tree parent in
1335 let dir = Tree.is_left tree t in
1336 let slist = Configuration.Ptss.fold (fun e a -> SList.cons e a) conf.Configuration.sets SList.nil in
1337 let fl_list = get_up_trans slist ptag a parent in
1338 let slist = SList.rev (slist) in
1339 let newconf = fold_f_conf tree parent slist fl_list conf dir in
1340 let accu,newconf = Configuration.IMap.fold (fun s res (ar,nc) ->
1341 if Ptset.Int.intersect s init then
1342 ( RS.concat res ar ,nc)
1343 else (ar,Configuration.add nc s res))
1344 (newconf.Configuration.results) (accu,Configuration.empty)
1347 bottom_up a tree parent newconf next jump_fun root false init accu
1349 and prepare_topdown a tree t noright =
1350 let tag = Tree.tag tree t in
1353 Hashtbl.find h_tdconf tag
1356 let res = Hashtbl.fold (fun q l acc ->
1357 if List.exists (fun (ts,_) -> TagSet.mem tag ts) l
1358 then Ptset.Int.add q acc
1359 else acc) a.trans Ptset.Int.empty
1360 in Hashtbl.add h_tdconf tag res;res
1362 (* let _ = pr ", among ";
1363 StateSet.print fmt (Ptset.Int.elements r);
1366 let r = SList.cons r SList.nil in
1367 let set,res = top_down (~noright:noright) a tree t r t 1 in
1368 let set = match SList.node set with
1369 | SList.Cons(x,_) ->x
1372 Configuration.add Configuration.empty set res.(0)
1376 let run_bottom_up a tree k =
1377 let t = Tree.root in
1378 let trlist = Hashtbl.find a.trans (StateSet.choose a.init)
1380 let init = List.fold_left
1382 let _,_,_,f,_ = Transition.node t in
1383 let _,_,l = fst ( Formula.st f ) in
1384 StateSet.union acc l)
1385 StateSet.empty trlist
1387 let tree1,jump_fun =
1390 (*Tree.tagged_lowest t tag, fun tree -> Tree.tagged_next tree tag*)
1391 (Tree.tagged_desc tree tag t, let jump = Tree.tagged_foll_ctx tree tag
1392 in fun n -> jump n t )
1393 | `CONTAINS(_) -> (Tree.text_below tree t,let jump = Tree.text_next tree
1394 in fun n -> jump n t)
1397 let tree2 = jump_fun tree1 in
1398 let rec loop t next acc =
1399 let acc,conf,next_of_next = bottom_up a tree t
1400 Configuration.empty next jump_fun (Tree.root) true init acc
1402 let acc = Configuration.IMap.fold
1403 ( fun s res acc -> if StateSet.intersect init s
1404 then RS.concat res acc else acc) conf.Configuration.results acc
1406 if Tree.is_nil next_of_next (*|| Tree.equal next next_of_next *)then
1408 else loop next_of_next (jump_fun next_of_next) acc
1410 loop tree1 tree2 RS.empty
1415 let top_down_count a t = let module RI = Run(Integer) in Integer.length (RI.run_top_down a t)
1416 let top_down a t = let module RI = Run(IdSet) in (RI.run_top_down a t)
1417 let bottom_up_count a t k = let module RI = Run(Integer) in Integer.length (RI.run_bottom_up a t k)
1420 module Test (Doc : sig val doc : Tree.t end) =
1422 module Results = GResult(Doc)
1423 let top_down a t = let module R = Run(Results) in (R.run_top_down a t)