if x < 0 then failwith (Printf.sprintf "State: Assertion %i < 0 failed" x)
end
-module StateSet = struct
- include Ptset.Int
- let print ppf s =
- Format.pp_print_string ppf "{ ";
- iter (fun i -> Format.fprintf ppf "%i " i) s;
- Format.pp_print_string ppf "}";
- Format.pp_print_flush ppf ()
-end
+module StateSet = Ptset.Int
module Formula =
struct
let hash (f,s,t) = HASHINT3(Formlist.uid f ,StateSet.uid s,StateSet.uid t);;
end)
-(*
-module MemoFormlist = Memoizer.Make(FTable)
-
- Too slow
- let eval_formlist = MemoFormlist.make_rec (
- fun eval (fl,((s1,s2)as sets)) ->
- match Formlist.node fl with
- | Formlist.Nil -> StateSet.empty,false,false,false,false
- | Formlist.Cons(f,fll) ->
- let q,mark,f,_ = Transition.node f in
- let b,b1,b2 = eval_form_bool f s1 s2 in
- let s,b',b1',b2',amark = eval (fll,sets) in
- if b then (StateSet.add q s, b, b1'||b1,b2'||b2,mark||amark)
- else s,b',b1',b2',amark )
-*)
-
let h_f = FTable.create BIG_H_SIZE
| Formlist.Cons(f,fll) ->
let q,mark,f,_ = Transition.node f in
let b,b1,b2 = eval_form_bool f s1 s2 in
- let s,b',b1',b2',amark = loop fll in
- let r = if b then (StateSet.add q s, b, b1'||b1,b2'||b2,mark||amark)
- else s,b',b1',b2',amark
+ let (s,(b',b1',b2',amark)) as res = loop fll in
+ let r = if b then (StateSet.add q s, (b, b1'||b1,b2'||b2,mark||amark))
+ else res
in FTable.add h_f (fl,s1,s2) r;r
- | Formlist.Nil -> StateSet.empty,false,false,false,false
+ | Formlist.Nil -> StateSet.empty,(false,false,false,false)
in loop fl
let tags_of_state a q =
val fold : ( elt -> 'a -> 'a) -> t -> 'a -> 'a
val map : ( elt -> elt) -> t -> t
val length : t -> int
- val merge : bool -> bool -> bool -> bool -> elt -> t -> t -> t
+ val merge : (bool*bool*bool*bool) -> elt -> t -> t -> t
end
module Integer : ResultSet =
let fold _ _ _ = failwith "fold not implemented"
let map _ _ = failwith "map not implemented"
let length x = x
- let merge rb rb1 rb2 mark t res1 res2 =
+ let merge (rb,rb1,rb2,mark) t res1 res2 =
if rb then
let res1 = if rb1 then res1 else 0
and res2 = if rb2 then res2 else 0
in
{ l with node = loop l.node }
- let merge rb rb1 rb2 mark t res1 res2 =
+ let merge (rb,rb1,rb2,mark) t res1 res2 =
if rb then
let res1 = if rb1 then res1 else empty
and res2 = if rb2 then res2 else empty
let string_of_ts tags = (Ptset.Int.fold (fun t a -> a ^ " " ^ (Tag.to_string t) ) tags "{")^ " }"
- let choose_jump tagset qtags1 qtagsn a f_nil f_t1 f_s1 f_tn f_sn f_notext =
+ let choose_jump tagset qtags1 qtagsn a f_nil f_t1 f_s1 f_tn f_sn f_notext f_maytext =
let tags1,hastext1,fin1 = inter_text tagset (tags a qtags1) in
let tagsn,hastextn,finn = inter_text tagset (tags a qtagsn) in
(*if (hastext1||hastextn) then (`ANY,f_text) (* jumping to text nodes doesn't work really well *)
let tag = (Ptset.Int.choose tagsn) in (`TAG(tag),mk_app_fun f_tn tag (Tag.to_string tag))
else (* SelectDesc/Following *)
(`ANY,mk_app_fun f_sn tagsn (string_of_ts tagsn))
+ else if (hastext1||hastextn) then (`ANY,f_maytext)
else (`ANY,f_notext)
let choose_jump_down tree a b c d =
choose_jump a b c d
(mk_fun (fun _ -> Tree.nil) "Tree.mk_nil")
(mk_fun (Tree.tagged_child tree) "Tree.tagged_child")
- (mk_fun (Tree.select_child tree) "Tree.select_child") (* !! no select_child in Tree.ml *)
+ (mk_fun (Tree.select_child tree) "Tree.select_child")
(mk_fun (Tree.tagged_desc tree) "Tree.tagged_desc")
- (mk_fun (Tree.select_desc tree) "Tree.select_desc") (* !! no select_desc *)
+ (mk_fun (Tree.select_desc tree) "Tree.select_desc")
+ (mk_fun (Tree.first_element tree) "Tree.first_element")
(mk_fun (Tree.first_child tree) "Tree.first_child")
let choose_jump_next tree a b c d =
choose_jump a b c d
(mk_fun (fun _ _ -> Tree.nil) "Tree.mk_nil2")
- (mk_fun (Tree.tagged_sibling_ctx tree) "Tree.tagged_sibling_ctx")(* !! no tagged_sibling in Tree.ml *)
- (mk_fun (Tree.select_sibling_ctx tree) "Tree.select_sibling_ctx")(* !! no select_sibling in Tree.ml *)
+ (mk_fun (Tree.tagged_sibling_ctx tree) "Tree.tagged_sibling_ctx")
+ (mk_fun (Tree.select_sibling_ctx tree) "Tree.select_sibling_ctx")
(mk_fun (Tree.tagged_foll_ctx tree) "Tree.tagged_foll_ctx")
- (mk_fun (Tree.select_foll_ctx tree) "Tree.select_foll_ctx")(* !! no select_foll *)
+ (mk_fun (Tree.select_foll_ctx tree) "Tree.select_foll_ctx")
+ (mk_fun (Tree.next_element_ctx tree) "Tree.node_element_ctx")
(mk_fun (Tree.next_sibling_ctx tree) "Tree.node_sibling_ctx")
if mark then RS.cons t (RS.concat res1 res2)
else RS.concat res1 res2
else RS.empty
-
+
+
let top_down ?(noright=false) a tree t slist ctx slot_size =
let pempty = empty_size slot_size in
(* evaluation starts from the right so we put sl1,res1 at the end *)
| SList.Cons(s1,ll1),
SList.Cons(s2,ll2),
fl::fll ->
- let r',rb,rb1,rb2,mark = eval_formlist s1 s2 fl in
- let _ = res.(i) <- RS.merge rb rb1 rb2 mark t res1.(i) res2.(i)
+ let r',flags = eval_formlist s1 s2 fl in
+ let _ = res.(i) <- RS.merge flags t res1.(i) res2.(i)
in
fold ll1 ll2 fll (i+1) (SList.cons r' aq)
try
CachedTransTable.find td_trans (tag,slist)
with
- | Not_found ->
- let fl_list,llist,rlist,ca,da,sa,fa =
- SList.fold
- (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
- let fl,ll,rr,ca,da,sa,fa =
- StateSet.fold
- (fun q acc ->
- List.fold_left
- (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
- (ts,t) ->
- if (TagSet.mem tag ts)
- then
- let _,_,f,_ = Transition.node t in
- let (child,desc,below),(sibl,foll,after) = Formula.st f in
- (Formlist.cons t fl_acc,
- StateSet.union ll_acc below,
- StateSet.union rl_acc after,
- StateSet.union child c_acc,
- StateSet.union desc d_acc,
- StateSet.union sibl s_acc,
- StateSet.union foll f_acc)
- else acc ) acc (
- try Hashtbl.find a.trans q
- with
- Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
- q;[]
- )
-
- ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
- in fl::fll_acc, (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
- slist ([],SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
- in
- (* Logic to chose the first and next function *)
- let tags_below,tags_after = Tree.tags tree tag in
- let f_kind,first = choose_jump_down tree tags_below ca da a
- and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
- else choose_jump_next tree tags_after sa fa a in
- let empty_res = null_result() in
- let cont =
- match f_kind,n_kind with
- | `NIL,`NIL ->
- (fun _ _ -> eval_fold2_slist fl_list t empty_res empty_res )
- | _,`NIL -> (
- match f_kind with
- |`TAG(tag) ->
- (fun t _ -> eval_fold2_slist fl_list t empty_res
- (loop_tag tag (first t) llist t))
- | `ANY ->
- (fun t _ -> eval_fold2_slist fl_list t empty_res
- (loop (first t) llist t))
- | _ -> assert false)
-
+ | Not_found ->
+ let fl_list,llist,rlist,ca,da,sa,fa =
+ SList.fold
+ (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
+ let fl,ll,rr,ca,da,sa,fa =
+ StateSet.fold
+ (fun q acc ->
+ List.fold_left
+ (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
+ (ts,t) ->
+ if (TagSet.mem tag ts)
+ then
+ let _,_,f,_ = Transition.node t in
+ let (child,desc,below),(sibl,foll,after) = Formula.st f in
+ (Formlist.cons t fl_acc,
+ StateSet.union ll_acc below,
+ StateSet.union rl_acc after,
+ StateSet.union child c_acc,
+ StateSet.union desc d_acc,
+ StateSet.union sibl s_acc,
+ StateSet.union foll f_acc)
+ else acc ) acc (
+ try Hashtbl.find a.trans q
+ with
+ Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
+ q;[]
+ )
+
+ ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
+ in fl::fll_acc, (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
+ slist ([],SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
+ in
+ (* Logic to chose the first and next function *)
+ let _,tags_below,_,tags_after = Tree.tags tree tag in
+ let f_kind,first = choose_jump_down tree tags_below ca da a
+ and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
+ else choose_jump_next tree tags_after sa fa a in
+ let empty_res = null_result() in
+ let cont =
+ match f_kind,n_kind with
+ | `NIL,`NIL ->
+ (fun _ _ -> eval_fold2_slist fl_list t empty_res empty_res )
+ | _,`NIL -> (
+ match f_kind with
+ |`TAG(tag) ->
+ (fun t _ -> eval_fold2_slist fl_list t empty_res
+ (loop_tag tag (first t) llist t))
+ | `ANY ->
+ (fun t _ -> eval_fold2_slist fl_list t empty_res
+ (loop (first t) llist t))
+ | _ -> assert false)
+
| `NIL,_ -> (
match n_kind with
|`TAG(tag) ->
(fun t ctx -> eval_fold2_slist fl_list t
(loop_tag tag (next t ctx) rlist ctx) empty_res)
-
+
| `ANY ->
(fun t ctx -> eval_fold2_slist fl_list t
(loop (next t ctx) rlist ctx) empty_res)
-
+
| _ -> assert false)
| `TAG(tag1),`TAG(tag2) ->
(loop (next t ctx) rlist ctx)
(loop (first t) llist t) )
| _ -> assert false
- in
- let cont = D_IF_( (fun t ctx ->
- let a,b = cont t ctx in
- register_trace t (slist,a,fl_list,first,next,ctx);
- (a,b)
- ) ,cont)
- in
- (CachedTransTable.add td_trans (tag,slist) cont;cont)
+ in
+ let cont = D_IF_( (fun t ctx ->
+ let a,b = cont t ctx in
+ register_trace t (slist,a,fl_list,first,next,ctx);
+ (a,b)
+ ) ,cont)
+ in
+ (CachedTransTable.add td_trans (tag,slist) cont;cont)
in cont t ctx
+
in
(if noright then loop_no_right else loop) t slist ctx
-
-
+
+
let run_top_down a tree =
let init = SList.cons a.init SList.nil in
let _,res = top_down a tree Tree.root init Tree.root 1
match SList.node sl,fl with
|SList.Nil,[] -> acc
|SList.Cons(s,sll), formlist::fll ->
- let r',rb,rb1,rb2,mark =
+ let r',(rb,rb1,rb2,mark) =
let key = SList.hash sl,Formlist.hash formlist,dir in
try
Hashtbl.find h_fold key