]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/check.ml
nuri_of_ouri, ouri_of_nuri, reference_of_ouri, ouri_of_reference moved
[helm.git] / helm / software / components / ng_kernel / check.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id$ *)
13
14 let debug = true
15 let ignore_exc = false
16 let rank_all_dependencies = true
17 let trust_environment = false
18
19 let indent = ref 0;;
20
21 let load_graph, get_graph =
22  let oldg = ref CicUniv.empty_ugraph in
23   (function uri -> 
24     let _,g = CicEnvironment.get_obj !oldg uri in
25      oldg := g),
26   (function _ -> !oldg)
27 ;;
28
29 let logger =
30     let do_indent () = String.make !indent ' ' in  
31     (function 
32       | `Start_type_checking s ->
33           if debug then
34            prerr_endline (do_indent () ^ "Start: " ^ NUri.string_of_uri s); 
35           incr indent
36       | `Type_checking_completed s ->
37           decr indent;
38           if debug then
39            prerr_endline (do_indent () ^ "End: " ^ NUri.string_of_uri s)
40       | `Type_checking_interrupted s ->
41           decr indent;
42           if debug then
43            prerr_endline (do_indent () ^ "Break: " ^ NUri.string_of_uri s)
44       | `Type_checking_failed s ->
45           decr indent;
46           if debug then
47            prerr_endline (do_indent () ^ "Fail: " ^ NUri.string_of_uri s)
48       | `Trust_obj s ->
49           if debug then
50            prerr_endline (do_indent () ^ "Trust: " ^ NUri.string_of_uri s))
51 ;;
52
53 let _ =
54   let do_old_logging = ref true in
55   HelmLogger.register_log_callback
56    (fun ?append_NL html_msg ->
57      if !do_old_logging then
58       prerr_endline (HelmLogger.string_of_html_msg html_msg));
59   CicParser.impredicative_set := false;
60   NCicTypeChecker.set_logger logger;
61   NCicPp.set_ppterm NCicPp.trivial_pp_term;
62   Helm_registry.load_from "conf.xml";
63   let alluris = 
64     try
65       let s = Sys.argv.(1) in
66       if s = "-alluris" then
67        begin
68         let uri_re = Str.regexp ".*\\(ind\\|con\\)$" in
69         let uris = Http_getter.getalluris () in
70         let alluris = List.filter (fun u -> Str.string_match uri_re u 0) uris in
71         let oc = open_out "alluris.txt" in
72         List.iter (fun s -> output_string oc (s^"\n")) alluris;
73         close_out oc; 
74         []
75        end
76       else [s]
77     with Invalid_argument _ -> 
78       let r = ref [] in
79       let ic = open_in "alluris.txt" in
80       try while true do r := input_line ic :: !r; done; []
81       with _ -> List.rev !r
82   in
83   let alluris = 
84     HExtlib.filter_map
85       (fun u -> try Some (UriManager.uri_of_string u) with _ -> None) alluris 
86   in
87   (* brutal *)
88   prerr_endline "computing graphs to load...";
89   let roots_alluris = 
90    if not rank_all_dependencies then
91     alluris
92    else (
93     let dbd = HSql.quick_connect (LibraryDb.parse_dbd_conf ()) in
94      MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
95     let uniq l = 
96      HExtlib.list_uniq (List.sort UriManager.compare l) in
97     let who_uses u = 
98      uniq (List.map (fun (uri,_) -> UriManager.strip_xpointer uri)
99       (MetadataDeps.inverse_deps ~dbd u)) in
100     let rec fix acc l = 
101      let acc, todo = 
102       List.fold_left (fun (acc,todo) x ->
103         let w = who_uses x in
104         if w = [] then (x::acc,todo) else (acc,uniq (todo@w)))
105       (acc,[]) l
106      in
107      if todo = [] then uniq acc else fix acc todo
108     in
109      fix [] alluris)
110   in
111   prerr_endline "generating Coq graphs...";
112   CicEnvironment.set_trust (fun _ -> trust_environment);
113   List.iter
114    (fun u ->
115      prerr_endline (" - " ^ UriManager.string_of_uri u);
116      try
117        ignore(CicTypeChecker.typecheck u);
118      with 
119      | CicTypeChecker.AssertFailure s
120      | CicTypeChecker.TypeCheckerFailure s ->
121         prerr_endline (Lazy.force s);
122         assert false
123     ) roots_alluris;
124   prerr_endline "loading...";
125   List.iter 
126     (fun u -> 
127        prerr_endline ("  - "^UriManager.string_of_uri u);
128        try load_graph u with exn -> ())
129     roots_alluris;
130   prerr_endline "finished....";
131   CicUniv.do_rank (get_graph ());
132   prerr_endline "caching objects";
133   prerr_endline "ranked....";
134   HExtlib.profiling_enabled := false;
135   List.iter (fun uu ->
136     let uu= OCic2NCic.nuri_of_ouri uu in
137     indent := 0;
138     let o = NCicLibrary.get_obj uu in
139     try 
140       NCicTypeChecker.typecheck_obj o
141     with 
142     | NCicTypeChecker.AssertFailure s 
143     | NCicTypeChecker.TypeCheckerFailure s
144     | NCicEnvironment.ObjectNotFound s
145     | NCicEnvironment.BadDependency s as e -> 
146        prerr_endline ("######### " ^ Lazy.force s);
147        if not ignore_exc then raise e
148     )
149     alluris;
150   NCicEnvironment.invalidate ();
151   Gc.compact ();
152   HExtlib.profiling_enabled := true;
153   NCicTypeChecker.set_logger (fun _ -> ());
154   do_old_logging := false;
155   prerr_endline "typechecking, first with the new and then with the old kernel";
156   let prima = Unix.gettimeofday () in
157   List.iter 
158     (fun u ->
159        let u= OCic2NCic.nuri_of_ouri u in
160       indent := 0;
161       NCicTypeChecker.typecheck_obj (NCicLibrary.get_obj u))
162     alluris;
163   let dopo = Unix.gettimeofday () in
164   Gc.compact ();
165   let dopo2 = Unix.gettimeofday () in
166   Printf.eprintf "NEW typing: %3.2f, gc: %3.2f\n%!" (dopo -. prima) (dopo2 -.  dopo);
167   CicEnvironment.invalidate ();
168   Gc.compact ();
169   let prima = Unix.gettimeofday () in
170   List.iter (fun u -> ignore (CicTypeChecker.typecheck u)) alluris;
171   let dopo = Unix.gettimeofday () in
172   Gc.compact ();
173   let dopo2 = Unix.gettimeofday () in
174   Printf.eprintf "OLD typing: %3.2f, gc: %3.2f\n%!" (dopo -. prima) (dopo2 -. dopo)
175 ;;