10 val var : (int*State.t) -> t
11 val close : ((int*State.t), t) Hashtbl.t -> t -> t
12 val is_open : t -> bool
13 val singleton : elt -> t
14 val cons : elt -> t -> t
15 val snoc : t -> elt -> t
16 val concat : t -> t -> t
17 val concat3 : t -> t -> t -> t
18 val concat4 : t -> t -> t -> t -> t
19 val conscat : elt -> t -> t -> t
20 val conscat3 : elt -> t -> t -> t -> t
21 val conscat4 : elt -> t -> t -> t -> t -> t
22 val subtree_tags : Tree.t -> elt -> Tag.t -> t
23 val subtree_elements : Tree.t -> elt -> t
24 val iter : ( elt -> unit) -> t -> unit
25 val fold : ( elt -> 'a -> 'a) -> t -> 'a -> 'a
27 val serialize : string -> Tree.t -> t -> unit
31 module Count : S with type t = int =
43 let concat x y = x + y
44 let concat3 x y z = x + y + z
45 let concat4 x y z t = x + y + z + t
46 let conscat _ x y = 1 + x + y
47 let conscat3 _ x y z = 1 + x + y + z
48 let conscat4 _ x y z t = 1 + x + y + z + t
49 let subtree_tags tree node tag = Tree.subtree_tags tree node tag
50 let subtree_elements tree node = Tree.subtree_elements tree node
51 let iter _ _ = failwith "iter not implemented"
52 let fold _ _ _ = failwith "fold not implemented"
53 let map _ _ = failwith "map not implemented"
57 output_string o "<xml_result>\n";
58 output_string o (string_of_int x);
59 output_string o "\n</xml_result>\n";
65 | Cons of Tree.node * clist
66 | Concat of clist * clist
67 | ConsCat of Tree.node * clist * clist
68 | SubtreeTags of Tree.t * Tree.node * Tag.t
69 | SubtreeElts of Tree.t * Tree.node
72 type 'a mat = { mutable clist : clist;
73 mutable length : int }
75 module Mat : S with type t = Tree.node mat =
77 type t = Tree.node mat
80 let empty = { clist = Nil; length = 0 }
81 let singleton e = { clist = Cons(e, Nil) ; length = 1 }
82 let cons e l = { clist = Cons(e, l.clist); length = l.length + 1 }
84 let ll1 = l1.length in
85 if ll1 == 0 then l2 else
86 let ll2 = l2.length in if ll2 == 0 then l1 else
87 { clist = Concat(l1.clist, l2.clist); length = ll1 + ll2 }
89 let snoc l e = concat l (singleton e)
90 let concat3 l1 l2 l3 = concat l1 (concat l2 l3)
91 let concat4 l1 l2 l3 l4 = concat (concat l1 l2) (concat l3 l4)
97 let ll1 = l1.length in
98 if ll1 == 0 then cons e l2 else
99 let ll2 = l2.length in if ll2 == 0 then cons e l1 else
100 { clist = ConsCat(e, l1.clist, l2.clist); length = 1 + ll1 + ll2 }
102 (* let conscat e l1 l2 = cons e (concat l1 l2) *)
104 let conscat3 e l1 l2 l3 = conscat e l1 (concat l2 l3)
105 let conscat4 e l1 l2 l3 l4 = conscat e l1 (concat l2 (concat l3 l4))
107 let subtree_tags tree node tag =
108 let len = Tree.subtree_tags tree node tag in
109 if len == 0 then empty
111 { clist = SubtreeTags(tree, node, tag);
114 let subtree_elements tree node =
115 let len = Tree.subtree_elements tree node in
116 if len == 0 then empty
118 { clist = SubtreeElts(tree, node);
121 let fst_tagged tree t tag =
122 if Tree.tag tree t == tag then t
123 else Tree.tagged_descendant tree t tag
126 let fst_element tree t =
127 let tag = Tree.tag tree t in
128 if tag == Tag.document_node then
129 Tree.first_element tree t
133 let element_fold f tree t acc =
134 let rec loop node acc =
135 if node == Tree.nil then acc
137 let acc = f node acc in
138 let acc' = loop (Tree.first_element tree node) acc in
139 loop (Tree.next_element tree node) acc'
141 let t' = Tree.first_element tree t in loop t' acc
143 let element_iter f tree t =
144 let newf = fun e () -> f e in
145 element_fold newf tree t ()
147 let tag_fold f tree t tag acc =
148 let rec loop close node acc =
149 if node > Tree.nil && node < close then acc
151 let acc = f node acc in
152 loop close (Tree.tagged_next tree node tag) acc
154 let t' = fst_tagged tree t tag in
155 loop (Tree.closing tree t) t' acc
157 let tag_iter f tree t tag =
158 let rec loop close node =
159 if node > Tree.nil && node < close then begin
161 loop close (Tree.tagged_next tree node tag);
164 let t' = fst_tagged tree t tag in
165 loop (Tree.closing tree t) t'
171 | Cons(e, ll) -> loop ll (f e acc)
172 | Concat(l1, l2) -> loop l2 (loop l1 acc)
173 | ConsCat(e, l1, l2) -> loop l2 (loop l1 (f e acc))
174 | SubtreeTags(tree, t, tag) -> tag_fold f tree t tag acc
175 | SubtreeElts(tree, t) -> element_fold f tree t acc
183 | Cons(e, l) -> f e; loop l
184 | Concat(l1, l2) -> loop l1; loop l2
185 | ConsCat(e, l1, l2) -> f e; loop l1; loop l2
186 | SubtreeTags(tree, t, tag) -> tag_iter f tree t tag
187 | SubtreeElts(tree, t) ->
188 element_iter f tree t
192 let length l = l.length
194 let serialize name v l =
196 if name = "-" then Unix.stdout, ignore
198 Unix.openfile name [ Unix.O_WRONLY; Unix.O_TRUNC; Unix.O_CREAT ] 0o666,
201 ignore (Unix.write fd "<xml_result>\n" 0 13);
202 if l.length > 0 then begin
203 iter (fun node -> Tree.print_xml v node fd) l;
206 ignore (Unix.write fd "</xml_result>\n" 0 14);
210 let rec debug_clist =
212 Nil -> Printf.eprintf "Nil"
214 Printf.eprintf "Cons(%i," (Obj.magic e);
217 | Concat(clist1, clist2) ->
218 Printf.eprintf "Concat(";
223 | ConsCat(_, clist1, clist2) ->
224 Printf.eprintf "Concat(";
230 | SubtreeTags(tree, node, tag) ->
231 Printf.eprintf "SubtreeTags(tree, %i, %s)"
234 | SubtreeElts(tree, node) ->
235 Printf.eprintf "SubtreeElts(tree, %i)"
238 let debug l = debug_clist l.clist
242 module Partial(N : S) : S =
246 type t = { env : ((int*State.t), t) Hashtbl.t;
251 | Var of (int * State.t)
254 | Concat of list * list
257 let dummy = Hashtbl.create 0
258 let empty = { env = dummy;
261 let is_open t = t.opened
266 Lambda { t with env = h; opened = false } }
268 let singleton i = { empty with elem = Cons(i, Nil) }
269 let cons e t = { t with elem = Cons(e, t.elem) }
271 { t1 with elem = Concat (t1.elem, t2.elem) }
273 let snoc t e = concat t (singleton e)
274 let concat3 t1 t2 t3 = concat t1 (concat t2 t3)
275 let concat4 t1 t2 t3 t4 = concat (concat t1 t2) (concat t3 t4)
276 let conscat e t1 t2 = cons e (concat t1 t2)
277 let conscat3 e t1 t2 t3 = cons e (concat3 t1 t2 t3)
278 let conscat4 e t1 t2 t3 t4 = cons e (concat4 t1 t2 t3 t4)
279 let subtree_tags _ = failwith "not implemented"
280 let subtree_elements _ = failwith "not_implemented"
284 loop_list t.env t.elem
285 and loop_list h = function
287 | Var i -> loop (Hashtbl.find h i)
288 | Cons (e, l) -> f e; loop_list h l
289 | Concat (l1, l2) -> loop_list h l1; loop_list h l2
296 loop_list t.env acc t.elem
297 and loop_list h acc = function
299 | Var i -> loop (try Hashtbl.find h i with Not_found -> let a,b = i in Printf.eprintf "%i,%i not found\n%!" a b; empty) acc
300 | Cons (e, l) -> loop_list h (f e acc) l
301 | Concat (l1, l2) -> loop_list h (loop_list h acc l1) l2
302 | Lambda t -> loop t acc
308 Hashtbl.iter (fun (i,j) t ->
309 Format.eprintf "%i, %a ->" i State.print j;
311 Format.eprintf "----------------\n%!";
314 and dump_list = function
316 | Var (i,j) -> Format.eprintf "Var(%i, %a) " i State.print j;
317 | Cons (e, l) -> Format.eprintf "%i " (Node.to_int e); dump_list l
318 | Concat (l1, l2) -> dump_list l1 ; dump_list l2
322 let length t = fold (fun _ acc -> 1 + acc) t 0
326 { empty with elem = Var i; opened = true }
328 let serialize _ = failwith "not implemented"