]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/cic/cicUniv.ml
some tests patched
[helm.git] / helm / software / components / cic / cicUniv.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (*****************************************************************************)
27 (*                                                                           *)
28 (*                               PROJECT HELM                                *)
29 (*                                                                           *)
30 (*                      Enrico Tassi <tassi@cs.unibo.it>                     *)
31 (*                                 23/04/2004                                *)
32 (*                                                                           *)
33 (* This module implements the aciclic graph of universes.                    *)
34 (*                                                                           *)
35 (*****************************************************************************)
36
37 (* $Id$ *)
38
39 (*****************************************************************************)
40 (** switch implementation                                                   **)
41 (*****************************************************************************)
42
43 let fast_implementation = ref true ;;
44
45 (*****************************************************************************)
46 (** open                                                                    **)
47 (*****************************************************************************)
48
49 open Printf
50
51 (*****************************************************************************)
52 (** Types and default values                                                **)
53 (*****************************************************************************)
54
55 type universe = int * UriManager.uri option 
56     
57 module UniverseType = struct
58   type t = universe
59   let compare = Pervasives.compare
60 end
61   
62 module SOF = Set.Make(UniverseType)
63   
64 type entry = {
65   eq_closure : SOF.t;
66   ge_closure : SOF.t;
67   gt_closure : SOF.t;
68   in_gegt_of   : SOF.t;
69   one_s_eq   : SOF.t;
70   one_s_ge   : SOF.t;
71   one_s_gt   : SOF.t;
72 }
73     
74 module MAL = Map.Make(UniverseType)
75   
76 type arc_type = GE | GT | EQ
77     
78 type bag = entry MAL.t 
79     
80 let empty_entry = {
81   eq_closure=SOF.empty;
82   ge_closure=SOF.empty;
83   gt_closure=SOF.empty;
84   in_gegt_of=SOF.empty;
85   one_s_eq=SOF.empty;
86   one_s_ge=SOF.empty;
87   one_s_gt=SOF.empty;
88 }
89 let empty_bag = MAL.empty
90
91 let are_set_eq s1 s2 = 
92   SOF.equal s1 s2
93
94 let are_entry_eq v1 v2 =
95   (are_set_eq v1.gt_closure v2.gt_closure ) &&
96   (are_set_eq v1.ge_closure v2.ge_closure ) &&
97   (are_set_eq v1.eq_closure v2.eq_closure ) &&
98   (*(are_set_eq v1.in_gegt_of v2.in_gegt_of ) &&*)
99   (are_set_eq v1.one_s_ge v2.one_s_ge ) &&
100   (are_set_eq v1.one_s_gt v2.one_s_gt ) &&
101   (are_set_eq v1.one_s_eq v2.one_s_eq )
102
103 let are_ugraph_eq = MAL.equal are_entry_eq
104
105 (*****************************************************************************)
106 (** Pretty printings                                                        **)
107 (*****************************************************************************)
108
109 let string_of_universe (i,u) = 
110   match u with
111       Some u ->
112         "(" ^ ((string_of_int i) ^ "," ^ (UriManager.string_of_uri u) ^ ")")
113     | None -> "(" ^ (string_of_int i) ^ ",None)"
114
115 let string_of_universe_set l = 
116   SOF.fold (fun x s -> s ^ (string_of_universe x) ^ " ") l ""
117
118 let string_of_node n =
119   "{"^
120   "eq_c: " ^ (string_of_universe_set n.eq_closure) ^ "; " ^ 
121   "ge_c: " ^ (string_of_universe_set n.ge_closure) ^ "; " ^ 
122   "gt_c: " ^ (string_of_universe_set n.gt_closure) ^ "; " ^ 
123   "i_gegt: " ^ (string_of_universe_set n.in_gegt_of) ^ "}\n"
124
125 let string_of_arc (a,u,v) = 
126   (string_of_universe u) ^ " " ^ a ^ " " ^ (string_of_universe v)
127   
128 let string_of_mal m =
129   let rc = ref "" in
130   MAL.iter (fun k v ->  
131     rc := !rc ^ sprintf "%s --> %s" (string_of_universe k) 
132               (string_of_node v)) m;
133   !rc
134
135 let string_of_bag b = 
136   string_of_mal b
137
138 (*****************************************************************************)
139 (** Benchmarking                                                            **)
140 (*****************************************************************************)
141 let time_spent = ref 0.0;;
142 let partial = ref 0.0 ;;
143
144 let reset_spent_time () = time_spent := 0.0;;
145 let get_spent_time () = !time_spent ;;
146 let begin_spending () =
147   (*assert (!partial = 0.0);*)
148   partial := Unix.gettimeofday ()
149 ;;
150
151 let end_spending () =
152   assert (!partial > 0.0);
153   let interval = (Unix.gettimeofday ()) -. !partial in
154     partial := 0.0;
155     time_spent := !time_spent +. interval
156 ;;
157
158
159 (*****************************************************************************)
160 (** Helpers                                                                 **)
161 (*****************************************************************************)
162
163 (* find the repr *)
164 let repr u m =
165   try 
166     MAL.find u m
167   with
168     Not_found -> empty_entry
169     
170 (* FIXME: May be faster if we make it by hand *)
171 let merge_closures f nodes m =  
172   SOF.fold (fun x i -> SOF.union (f (repr x m)) i ) nodes SOF.empty
173
174 \f
175 (*****************************************************************************)
176 (** _fats implementation                                                    **)
177 (*****************************************************************************)
178
179 let rec closure_of_fast ru m =
180   let eq_c = closure_eq_fast ru m in
181   let ge_c = closure_ge_fast ru m in
182   let gt_c = closure_gt_fast ru m in
183     {
184       eq_closure = eq_c;
185       ge_closure = ge_c;
186       gt_closure = gt_c;
187       in_gegt_of = ru.in_gegt_of;
188       one_s_eq = ru.one_s_eq;
189       one_s_ge = ru.one_s_ge;
190       one_s_gt = ru.one_s_gt
191     }
192       
193 and closure_eq_fast ru m = 
194   let eq_c =
195     let j = ru.one_s_eq in
196     let _Uj = merge_closures (fun x -> x.eq_closure) j m in
197     let one_step_eq = ru.one_s_eq in
198       (SOF.union one_step_eq _Uj)
199   in
200     eq_c
201       
202 and closure_ge_fast ru m =
203   let ge_c = 
204     let j = SOF.union ru.one_s_ge (SOF.union ru.one_s_gt ru.one_s_eq) in
205     let _Uj = merge_closures (fun x -> x.ge_closure) j m in
206     let _Ux = j in
207       (SOF.union _Uj _Ux)
208   in
209     ge_c
210       
211 and closure_gt_fast ru m =
212   let gt_c =
213     let j = ru.one_s_gt in
214     let k = ru.one_s_ge in
215     let l = ru.one_s_eq in
216     let _Uj = merge_closures (fun x -> x.ge_closure) j m in
217     let _Uk = merge_closures (fun x -> x.gt_closure) k m in
218     let _Ul = merge_closures (fun x -> x.gt_closure) l m in
219     let one_step_gt = ru.one_s_gt in
220       (SOF.union (SOF.union (SOF.union _Ul one_step_gt) _Uk) _Uj)
221   in
222     gt_c
223       
224 and print_rec_status u ru =
225   print_endline ("Aggiusto " ^ (string_of_universe u) ^ 
226                  "e ottengo questa chiusura\n " ^ (string_of_node ru))
227
228 and adjust_fast u m =
229   let ru = repr u m in
230   let gt_c = closure_gt_fast ru m in
231   let ge_c = closure_ge_fast ru m in
232   let eq_c = closure_eq_fast ru m in
233   let changed_eq = not (are_set_eq eq_c ru.eq_closure) in
234   let changed_gegt = 
235     (not (are_set_eq gt_c ru.gt_closure)) || 
236     (not (are_set_eq ge_c ru.ge_closure))
237   in
238     if ((not changed_gegt) &&  (not changed_eq)) then
239       m
240     else
241       begin
242         let ru' = {
243           eq_closure = eq_c;
244           ge_closure = ge_c;
245           gt_closure = gt_c;
246           in_gegt_of = ru.in_gegt_of;
247           one_s_eq = ru.one_s_eq;
248           one_s_ge = ru.one_s_ge;
249           one_s_gt = ru.one_s_gt}
250         in
251         let m = MAL.add u ru' m in
252         let m =
253             SOF.fold (fun x m -> adjust_fast  x m) 
254               (SOF.union ru'.eq_closure ru'.in_gegt_of) m
255               (* TESI: 
256                    ru'.in_gegt_of m 
257               *)
258         in
259           m (*adjust_fast  u m*)
260       end
261         
262 and add_gt_arc_fast u v m =
263   let ru = repr u m in
264   let ru' = {ru with one_s_gt = SOF.add v ru.one_s_gt} in
265   let m' = MAL.add u ru' m in
266   let rv = repr v m' in
267   let rv' = {rv with in_gegt_of = SOF.add u rv.in_gegt_of} in
268   let m'' = MAL.add v rv' m' in
269     adjust_fast u m''
270       
271 and add_ge_arc_fast u v m =
272   let ru = repr u m in
273   let ru' = { ru with one_s_ge = SOF.add v ru.one_s_ge} in
274   let m' = MAL.add u ru' m in
275   let rv = repr v m' in
276   let rv' = {rv with in_gegt_of = SOF.add u rv.in_gegt_of} in
277   let m'' = MAL.add v rv' m' in
278   adjust_fast u m''
279
280 and add_eq_arc_fast u v m =
281   let ru = repr u m in
282   let rv = repr v m in 
283   let ru' = {ru  with one_s_eq = SOF.add v ru.one_s_eq} in
284   (*TESI: let ru' = {ru' with in_gegt_of = SOF.add v ru.in_gegt_of} in *)
285   let m' = MAL.add u ru' m in
286   let rv' = {rv  with one_s_eq = SOF.add u rv.one_s_eq} in
287   (*TESI: let rv' = {rv' with in_gegt_of = SOF.add u rv.in_gegt_of} in *)
288   let m'' = MAL.add v rv' m' in
289     adjust_fast v (*(adjust_fast u*) m'' (* ) *)
290 ;;
291
292 \f
293 (*****************************************************************************)
294 (** safe implementation                                                     **)
295 (*****************************************************************************)
296
297 let closure_of u m =
298   let ru = repr u m in
299   let eq_c =
300     let j = ru.one_s_eq in
301     let _Uj = merge_closures (fun x -> x.eq_closure) j m in
302     let one_step_eq = ru.one_s_eq in
303             (SOF.union one_step_eq _Uj)
304   in
305   let ge_c = 
306     let j = SOF.union ru.one_s_ge (SOF.union ru.one_s_gt ru.one_s_eq) in
307     let _Uj = merge_closures (fun x -> x.ge_closure) j m in
308     let _Ux = j in
309       (SOF.union _Uj _Ux)
310   in
311   let gt_c =
312     let j = ru.one_s_gt in
313     let k = ru.one_s_ge in
314     let l = ru.one_s_eq in
315     let _Uj = merge_closures (fun x -> x.ge_closure) j m in
316     let _Uk = merge_closures (fun x -> x.gt_closure) k m in
317     let _Ul = merge_closures (fun x -> x.gt_closure) l m in
318     let one_step_gt = ru.one_s_gt in
319       (SOF.union (SOF.union (SOF.union _Ul one_step_gt) _Uk) _Uj)
320   in
321     {
322       eq_closure = eq_c;
323       ge_closure = ge_c;
324       gt_closure = gt_c;
325       in_gegt_of = ru.in_gegt_of;
326       one_s_eq = ru.one_s_eq;
327       one_s_ge = ru.one_s_ge;
328       one_s_gt = ru.one_s_gt
329     }
330
331 let rec simple_adjust m =
332   let m' = 
333     MAL.mapi (fun x _ -> closure_of x m) m
334   in
335     if not (are_ugraph_eq m  m') then(
336       simple_adjust m')
337     else
338       m'
339
340 let add_eq_arc u v m =
341   let ru = repr u m in
342   let rv = repr v m in
343   let ru' = {ru with one_s_eq = SOF.add v ru.one_s_eq} in
344   let m' = MAL.add u ru' m in
345   let rv' = {rv with one_s_eq = SOF.add u rv.one_s_eq} in
346   let m'' = MAL.add v rv' m' in
347     simple_adjust m''
348
349 let add_ge_arc u v m =
350   let ru = repr u m in
351   let ru' = { ru with one_s_ge = SOF.add v ru.one_s_ge} in
352   let m' = MAL.add u ru' m in
353     simple_adjust m'
354
355 let add_gt_arc u v m =
356   let ru = repr u m in
357   let ru' = {ru with one_s_gt = SOF.add v ru.one_s_gt} in
358   let m' = MAL.add u ru' m in
359     simple_adjust m'
360
361 \f
362 (*****************************************************************************)
363 (** Outhern interface, that chooses between _fast and safe                  **)
364 (*****************************************************************************)
365
366 (*                                                                            
367     given the 2 nodes plus the current bag, adds the arc, recomputes the 
368     closures and returns the new map
369 *) 
370 let add_eq fast u v b =
371   if fast then
372     add_eq_arc_fast u v b
373   else
374     add_eq_arc u v b
375
376 (*                                                                            
377     given the 2 nodes plus the current bag, adds the arc, recomputes the 
378     closures and returns the new map
379 *) 
380 let add_ge fast u v b =
381   if fast then
382     add_ge_arc_fast u v b
383   else
384     add_ge_arc u v b
385 (*                                                                            
386     given the 2 nodes plus the current bag, adds the arc, recomputes the 
387     closures and returns the new map
388 *)                                                                            
389 let add_gt fast u v b =
390   if fast then
391     add_gt_arc_fast u v b
392   else
393     add_gt_arc u v b
394
395
396 (*****************************************************************************)
397 (** Other real code                                                         **)
398 (*****************************************************************************)
399
400 exception UniverseInconsistency of string Lazy.t
401
402 let error arc node1 closure_type node2 closure =
403   let s =
404    lazy
405     ("\n  ===== Universe Inconsistency detected =====\n\n" ^
406      "   Unable to add\n" ^ 
407      "\t" ^ (string_of_arc arc) ^ "\n" ^
408      "   cause\n" ^ 
409      "\t" ^ (string_of_universe node1) ^ "\n" ^
410      "   is in the " ^ closure_type ^ " closure\n" ^
411      "\t{" ^ (string_of_universe_set closure) ^ "}\n" ^ 
412      "   of\n" ^ 
413      "\t" ^ (string_of_universe node2) ^ "\n\n" ^
414      "  ===== Universe Inconsistency detected =====\n") in
415   prerr_endline (Lazy.force s);
416   raise (UniverseInconsistency s)
417
418
419 let fill_empty_nodes_with_uri (g, already_contained) l uri =
420   let fill_empty_universe u =
421     match u with
422         (i,None) -> (i,Some uri)
423       | (i,Some _) as u -> u
424   in
425   let fill_empty_set s =
426     SOF.fold (fun e s -> SOF.add (fill_empty_universe e) s) s SOF.empty 
427   in
428   let fill_empty_entry e = {
429     eq_closure = (fill_empty_set e.eq_closure) ;
430     ge_closure = (fill_empty_set e.ge_closure) ;
431     gt_closure = (fill_empty_set e.gt_closure) ;
432     in_gegt_of = (fill_empty_set e.in_gegt_of) ;
433     one_s_eq = (fill_empty_set e.one_s_eq) ;
434     one_s_ge = (fill_empty_set e.one_s_ge) ;
435     one_s_gt = (fill_empty_set e.one_s_gt) ;
436   } in  
437   let m = g in
438   let m' = MAL.fold (
439     fun k v m -> 
440       MAL.add (fill_empty_universe k) (fill_empty_entry v) m) m MAL.empty
441   in
442   let l' = List.map fill_empty_universe l in
443     (m', already_contained),l'
444
445
446 (*****************************************************************************)
447 (** World interface                                                         **)
448 (*****************************************************************************)
449
450 type universe_graph = bag * UriManager.UriSet.t 
451 (* the graph , the cache of already merged ugraphs *)
452
453 let empty_ugraph = empty_bag, UriManager.UriSet.empty
454
455 let current_index_anon = ref (-1)
456 let current_index_named = ref (-1)
457
458 let restart_numbering () = current_index_named := (-1) 
459
460 let fresh ?uri ?id () =
461   let i =
462     match uri,id with
463     | None,None -> 
464         current_index_anon := !current_index_anon + 1;
465         !current_index_anon
466     | None, Some _ -> assert false
467     | Some _, None -> 
468         current_index_named := !current_index_named + 1;
469         !current_index_named
470     | Some _, Some id -> id
471   in
472   (i,uri)
473
474 let name_universe u uri =
475   match u with
476   | (i, None) -> (i, Some uri)
477   | _ -> u
478   
479 let print_ugraph (g, _) = 
480   prerr_endline (string_of_bag g)
481
482 let add_eq ?(fast=(!fast_implementation)) u v b =
483   (* should we check to no add twice the same?? *)
484   let m = b in
485   let ru = repr u m in
486   if SOF.mem v ru.gt_closure then
487     error ("EQ",u,v) v "GT" u ru.gt_closure
488   else
489     begin
490     let rv = repr v m in
491     if SOF.mem u rv.gt_closure then
492       error ("EQ",u,v) u "GT" v rv.gt_closure
493     else
494       add_eq fast u v b
495     end
496
497 let add_ge ?(fast=(!fast_implementation)) u v b =
498   (* should we check to no add twice the same?? *)
499   let m = b in
500   let rv = repr v m in
501   if SOF.mem u rv.gt_closure then
502     error ("GE",u,v) u "GT" v rv.gt_closure
503   else
504     add_ge fast u v b
505   
506 let add_gt ?(fast=(!fast_implementation)) u v b =
507   (* should we check to no add twice the same?? *)
508   (* 
509      FIXME : check the thesis... no need to check GT and EQ closure since the 
510      GE is a superset of both 
511   *)
512   let m = b in
513   let rv = repr v m in
514
515   if u = v then
516     error ("GT",u,v) u "==" v SOF.empty
517   else
518   
519   (*if SOF.mem u rv.gt_closure then
520     error ("GT",u,v) u "GT" v rv.gt_closure
521   else
522     begin*)
523       if SOF.mem u rv.ge_closure then
524         error ("GT",u,v) u "GE" v rv.ge_closure
525       else
526 (*        begin
527           if SOF.mem u rv.eq_closure then
528             error ("GT",u,v) u "EQ" v rv.eq_closure
529           else*)
530             add_gt fast u v b
531 (*        end
532     end*)
533
534 (*****************************************************************************)
535 (** START: Decomment this for performance comparisons                       **)
536 (*****************************************************************************)
537
538 let add_eq ?(fast=(!fast_implementation))  u v (b,already_contained) =
539   (*prerr_endline "add_eq";*)
540   begin_spending ();
541   let rc = add_eq ~fast u v b in
542   end_spending ();
543     rc,already_contained
544
545 let add_ge ?(fast=(!fast_implementation)) u v (b,already_contained) =
546 (*   prerr_endline "add_ge"; *)
547   begin_spending ();
548   let rc = add_ge ~fast u v b in
549   end_spending ();
550     rc,already_contained
551     
552 let add_gt ?(fast=(!fast_implementation)) u v (b,already_contained) =
553 (*   prerr_endline "add_gt"; *)
554   begin_spending ();
555   let rc = add_gt ~fast u v b in
556   end_spending ();
557     rc,already_contained
558     
559 (* profiling code
560 let profiler_eq = HExtlib.profile "CicUniv.add_eq"
561 let profiler_ge = HExtlib.profile "CicUniv.add_ge"
562 let profiler_gt = HExtlib.profile "CicUniv.add_gt"
563 let add_gt ?fast u v b = 
564   profiler_gt.HExtlib.profile (fun _ -> add_gt ?fast u v b) ()
565 let add_ge ?fast u v b = 
566   profiler_ge.HExtlib.profile (fun _ -> add_ge ?fast u v b) ()
567 let add_eq ?fast u v b = 
568   profiler_eq.HExtlib.profile (fun _ -> add_eq ?fast u v b) ()
569 *)
570
571 (*****************************************************************************)
572 (** END: Decomment this for performance comparisons                         **)
573 (*****************************************************************************)
574
575 let merge_ugraphs ~base_ugraph ~increment:(increment, uri_of_increment) =
576   let merge_brutal (u,_) v =
577     let m1 = u in 
578     let m2 = v in 
579       MAL.fold (
580         fun k v x -> 
581           (SOF.fold (
582              fun u x -> 
583                let m = add_gt k u x in m) 
584                 (SOF.union v.one_s_gt v.gt_closure)
585              (SOF.fold (
586                 fun u x -> 
587                   let m = add_ge k u x in m) 
588                     (SOF.union v.one_s_ge v.ge_closure)
589                 (SOF.fold (
590                    fun u x ->
591                      let m = add_eq k u x in m) 
592                       (SOF.union v.one_s_eq v.eq_closure) x)))
593           ) m1 m2
594   in
595   let base, already_contained = base_ugraph in
596   if MAL.is_empty base then
597     increment
598   else if 
599     MAL.is_empty (fst increment) || 
600     UriManager.UriSet.mem uri_of_increment already_contained 
601   then
602     base_ugraph
603   else
604     fst (merge_brutal increment base_ugraph), 
605     UriManager.UriSet.add uri_of_increment already_contained
606
607 (* profiling code; WARNING: the time spent during profiling can be
608    greater than the profiled time
609 let profiler_merge = HExtlib.profile "CicUniv.merge_ugraphs"
610 let merge_ugraphs ~base_ugraph ~increment =
611   profiler_merge.HExtlib.profile 
612   (fun _ -> merge_ugraphs ~base_ugraph ~increment) ()
613 *)
614
615 (*****************************************************************************)
616 (** Xml sesialization and parsing                                           **)
617 (*****************************************************************************)
618
619 let xml_of_universe name u = 
620   match u with
621   | (i,Some u) -> 
622       Xml.xml_empty name [
623         None,"id",(string_of_int i) ;
624         None,"uri",(UriManager.string_of_uri u)]
625   | (_,None) -> 
626       raise (Failure "we can serialize only universes with uri")
627
628 let xml_of_set s =
629   let l = 
630     List.map (xml_of_universe "node") (SOF.elements s) 
631   in
632     List.fold_left (fun s x -> [< s ; x >] ) [<>] l
633       
634 let xml_of_entry_content e =
635   let stream_of_field f name =
636     let eq_c = xml_of_set f in
637     if eq_c != [<>] then
638       Xml.xml_nempty name [] eq_c
639     else
640       [<>]
641   in
642   [<
643     (stream_of_field e.eq_closure "eq_closure");
644     (stream_of_field e.gt_closure "gt_closure");
645     (stream_of_field e.ge_closure "ge_closure");
646     (stream_of_field e.in_gegt_of "in_gegt_of");
647     (stream_of_field e.one_s_eq "one_s_eq");
648     (stream_of_field e.one_s_gt "one_s_gt");
649     (stream_of_field e.one_s_ge "one_s_ge")
650   >]
651
652 let xml_of_entry u e =
653   let (i,u') = u in
654   let u'' = 
655     match u' with 
656         Some x -> x 
657       | None -> 
658           raise (Failure "we can serialize only universes (entry) with uri")
659   in
660   let ent = Xml.xml_nempty "entry" [
661     None,"id",(string_of_int i) ; 
662     None,"uri",(UriManager.string_of_uri u'')] in
663   let content = xml_of_entry_content e in
664   ent content
665
666 let write_xml_of_ugraph filename (m,_) l =
667     let tokens = 
668       [< 
669         Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
670         Xml.xml_nempty "ugraph" [] 
671           ([< (MAL.fold ( fun k v s -> [< s ; (xml_of_entry k v) >]) m [<>]) ; 
672            (List.fold_left 
673              (fun s u -> [< s ; xml_of_universe "owned_node" u >]) [<>] l) >])>]
674     in
675     Xml.pp ~gzip:true tokens (Some filename)
676
677 let univno = fst
678
679  
680 let rec clean_ugraph (m,already_contained) f =
681   let m' = 
682     MAL.fold (fun k v x -> if (f k) then MAL.add k v x else x ) m MAL.empty in
683   let m'' =  MAL.fold (fun k v x -> 
684     let v' = {
685       eq_closure = SOF.filter f v.eq_closure;
686       ge_closure = SOF.filter f v.ge_closure;
687       gt_closure = SOF.filter f v.gt_closure;
688       in_gegt_of = SOF.filter f v.in_gegt_of;
689       one_s_eq = SOF.filter f v.one_s_eq;
690       one_s_ge = SOF.filter f v.one_s_ge;
691       one_s_gt = SOF.filter f v.one_s_gt
692     } in 
693     MAL.add k v' x ) m' MAL.empty in
694   let e_l = 
695     MAL.fold (fun k v l -> if v = empty_entry && not(f k) then
696       begin
697       k::l end else l) m'' []
698   in
699     if e_l != [] then
700       clean_ugraph 
701         (m'', already_contained) (fun u -> (f u) && not (List.mem u e_l))
702     else
703       MAL.fold 
704         (fun k v x -> if v <> empty_entry then MAL.add k v x else x) 
705         m'' MAL.empty,
706       already_contained
707
708 let clean_ugraph g l =
709   clean_ugraph g (fun u -> List.mem u l)
710
711 let assigner_of = 
712   function
713     "ge_closure" -> (fun e u->{e with ge_closure=SOF.add u e.ge_closure})
714   | "gt_closure" -> (fun e u->{e with gt_closure=SOF.add u e.gt_closure})
715   | "eq_closure" -> (fun e u->{e with eq_closure=SOF.add u e.eq_closure})
716   | "in_gegt_of"   -> (fun e u->{e with in_gegt_of  =SOF.add u e.in_gegt_of})
717   | "one_s_ge"   -> (fun e u->{e with one_s_ge  =SOF.add u e.one_s_ge})
718   | "one_s_gt"   -> (fun e u->{e with one_s_gt  =SOF.add u e.one_s_gt})
719   | "one_s_eq"   -> (fun e u->{e with one_s_eq  =SOF.add u e.one_s_eq})
720   | s -> raise (Failure ("unsupported tag " ^ s))
721 ;;
722
723 let cb_factory m l = 
724   let module XPP = XmlPushParser in
725   let current_node = ref (0,None) in
726   let current_entry = ref empty_entry in
727   let current_assign = ref (assigner_of "in_gegt_of") in
728   { XPP.default_callbacks with
729     XPP.end_element = Some( fun name ->
730       match name with
731       | "entry" -> 
732           m := MAL.add !current_node !current_entry !m;
733           current_entry := empty_entry
734       | _ -> ()
735     );
736     XPP.start_element = Some( fun name attlist ->
737       match name with
738       | "ugraph" -> ()
739       | "entry" -> 
740           let id = List.assoc "id" attlist in      
741           let uri = List.assoc "uri" attlist in
742           current_node := (int_of_string id,Some (UriManager.uri_of_string uri))
743       | "node" -> 
744           let id = int_of_string (List.assoc "id" attlist) in
745           let uri = List.assoc "uri" attlist in        
746             current_entry := !current_assign !current_entry 
747               (id,Some (UriManager.uri_of_string uri))
748       | "owned_node" -> 
749           let id = int_of_string (List.assoc "id" attlist) in
750           let uri = List.assoc "uri" attlist in        
751           l := (id,Some (UriManager.uri_of_string uri)) :: !l
752       | s -> current_assign := assigner_of s
753     )
754   }
755 ;; 
756
757 let ugraph_and_univlist_of_xml filename =
758   let module XPP = XmlPushParser in
759   let result_map = ref MAL.empty in
760   let result_list = ref [] in
761   let cb = cb_factory result_map result_list in
762   let xml_parser = XPP.create_parser cb in
763   let xml_source = `Gzip_file filename in
764   (try XPP.parse xml_parser xml_source
765    with (XPP.Parse_error err) as exn -> raise exn);
766   (!result_map,UriManager.UriSet.empty), !result_list
767
768 \f
769 (*****************************************************************************)
770 (** the main, only for testing                                              **)
771 (*****************************************************************************)
772
773 (* 
774
775 type arc = Ge | Gt | Eq ;;
776
777 let randomize_actionlist n m =
778   let ge_percent = 0.7 in
779   let gt_percent = 0.15 in
780   let random_step () =
781     let node1 = Random.int m in
782     let node2 = Random.int m in
783     let op = 
784       let r = Random.float 1.0 in
785         if r < ge_percent then 
786           Ge 
787         else (if r < (ge_percent +. gt_percent) then 
788           Gt 
789         else 
790           Eq) 
791     in
792       op,node1,node2      
793   in
794   let rec aux n =
795     match n with 
796         0 -> []
797       | n -> (random_step ())::(aux (n-1))
798   in
799     aux n
800
801 let print_action_list l =
802   let string_of_step (op,node1,node2) =
803     (match op with
804          Ge -> "Ge"
805        | Gt -> "Gt"
806        | Eq -> "Eq") ^ 
807     "," ^ (string_of_int node1) ^ ","   ^ (string_of_int node2) 
808   in
809   let rec aux l =
810     match l with 
811         [] -> "]"
812       | a::tl ->
813           ";" ^ (string_of_step a) ^ (aux tl)
814   in
815   let body = aux l in
816   let l_body = (String.length body) - 1 in
817     prerr_endline ("[" ^ (String.sub body 1 l_body))
818   
819 let debug = false
820 let d_print_endline = if debug then print_endline else ignore 
821 let d_print_ugraph = if debug then print_ugraph else ignore
822
823 let _ = 
824   (if Array.length Sys.argv < 2 then
825     prerr_endline ("Usage " ^ Sys.argv.(0) ^ " max_edges max_nodes"));
826   Random.self_init ();
827   let max_edges = int_of_string Sys.argv.(1) in
828   let max_nodes = int_of_string Sys.argv.(2) in
829   let action_listR = randomize_actionlist max_edges max_nodes in
830
831   let action_list = [Ge,1,4;Ge,2,6;Ge,1,1;Eq,6,4;Gt,6,3] in
832   let action_list = action_listR in
833   
834   print_action_list action_list;
835   let prform_step ?(fast=false) (t,u,v) g =
836     let f,str = 
837       match t with
838           Ge -> add_ge,">="
839         | Gt -> add_gt,">"
840         | Eq -> add_eq,"="
841     in
842       d_print_endline (
843         "Aggiungo " ^ 
844         (string_of_int u) ^
845         " " ^ str ^ " " ^ 
846         (string_of_int v));
847       let g' = f ~fast (u,None) (v,None) g in
848         (*print_ugraph g' ;*)
849         g'
850   in
851   let fail = ref false in
852   let time1 = Unix.gettimeofday () in
853   let n_safe = ref 0 in
854   let g_safe =  
855     try 
856       d_print_endline "SAFE";
857       List.fold_left (
858         fun g e -> 
859           n_safe := !n_safe + 1;
860           prform_step e g
861       ) empty_ugraph action_list
862     with
863         UniverseInconsistency s -> fail:=true;empty_bag
864   in
865   let time2 = Unix.gettimeofday () in
866   d_print_ugraph g_safe;
867   let time3 = Unix.gettimeofday () in
868   let n_test = ref 0 in
869   let g_test = 
870     try
871       d_print_endline "FAST";
872       List.fold_left (
873         fun g e ->
874           n_test := !n_test + 1;
875           prform_step ~fast:true e g
876       ) empty_ugraph action_list
877     with
878         UniverseInconsistency s -> empty_bag
879   in
880   let time4 = Unix.gettimeofday () in
881   d_print_ugraph g_test;
882     if are_ugraph_eq g_safe g_test && !n_test = !n_safe then
883       begin
884         let num_eq = 
885           List.fold_left (
886             fun s (e,_,_) -> 
887               if e = Eq then s+1 else s 
888           ) 0 action_list 
889         in
890         let num_gt = 
891           List.fold_left (
892             fun s (e,_,_) ->
893               if e = Gt then s+1 else s
894           ) 0 action_list
895         in
896         let num_ge = max_edges - num_gt - num_eq in
897         let time_fast = (time4 -. time3) in
898         let time_safe = (time2 -. time1) in
899         let gap = ((time_safe -. time_fast) *. 100.0) /. time_safe in
900         let fail = if !fail then 1 else 0 in
901           print_endline 
902             (sprintf 
903                "OK %d safe %1.4f fast %1.4f %% %1.2f #eq %d #gt %d #ge %d %d" 
904                fail time_safe time_fast gap num_eq num_gt num_ge !n_safe);
905           exit 0
906       end
907     else
908       begin
909         print_endline "FAIL";
910         print_ugraph g_safe;
911         print_ugraph g_test;
912         exit 1
913       end
914 ;;
915
916  *)
917
918 let recons_univ u =
919   match u with
920   | i, None -> u
921   | i, Some uri ->
922       i, Some (UriManager.uri_of_string (UriManager.string_of_uri uri))
923
924 let recons_entry entry =
925   let recons_set set =
926     SOF.fold (fun univ set -> SOF.add (recons_univ univ) set) set SOF.empty
927   in
928   {
929     eq_closure = recons_set entry.eq_closure;
930     ge_closure = recons_set entry.ge_closure;
931     gt_closure = recons_set entry.gt_closure;
932     in_gegt_of = recons_set entry.in_gegt_of;
933     one_s_eq = recons_set entry.one_s_eq;
934     one_s_ge = recons_set entry.one_s_ge;
935     one_s_gt = recons_set entry.one_s_gt;
936   }
937
938 let recons_graph (graph,uriset) =
939   MAL.fold
940     (fun universe entry map ->
941       MAL.add (recons_univ universe) (recons_entry entry) map)
942     graph 
943     MAL.empty,
944   UriManager.UriSet.fold 
945     (fun u acc -> 
946       UriManager.UriSet.add 
947         (UriManager.uri_of_string (UriManager.string_of_uri u)) acc) 
948     uriset UriManager.UriSet.empty 
949
950 let assert_univ u =
951     match u with 
952     | (_,None) ->
953        raise (UniverseInconsistency (lazy "This universe graph has a hole"))
954     | _ -> ()
955     
956 let assert_univs_have_uri (graph,_) univlist =
957   let assert_set s =
958     SOF.iter (fun u -> assert_univ u) s
959   in
960   let assert_entry e =
961     assert_set e.eq_closure;
962     assert_set e.ge_closure;
963     assert_set e.gt_closure;
964     assert_set e.in_gegt_of;
965     assert_set e.one_s_eq;
966     assert_set e.one_s_ge;
967     assert_set e.one_s_gt;
968   in
969   MAL.iter (fun k v -> assert_univ k; assert_entry v)graph;
970   List.iter assert_univ univlist
971   
972 let eq u1 u2 = 
973   match u1,u2 with
974   | (id1, Some uri1),(id2, Some uri2) -> 
975       id1 = id2 && UriManager.eq uri1 uri2
976   | (id1, None),(id2, None) -> id1 = id2
977   | _ -> false
978   
979 let compare (id1, uri1) (id2, uri2) = 
980   let cmp = id1 - id2 in
981   if cmp = 0 then
982     match uri1,uri2 with
983     | None, None -> 0 
984     | Some _, None -> 1
985     | None, Some _ -> ~-1
986     | Some uri1, Some uri2 -> UriManager.compare uri1 uri2
987   else
988     cmp
989   
990 (* EOF *)