]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/check.ml
...
[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 = false
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 let mk_type n = 
53   if n = 0 then
54      [false, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
55   else
56      [false, NUri.uri_of_string ("cic:/matita/pts/Type"^string_of_int n^".univ")]
57 ;;
58
59 let _ =
60   let do_old_logging = ref true in
61   HelmLogger.register_log_callback
62    (fun ?append_NL html_msg ->
63      if !do_old_logging then
64       prerr_endline (HelmLogger.string_of_html_msg html_msg));
65   CicParser.impredicative_set := false;
66   NCicTypeChecker.set_logger logger;
67   NCicPp.set_ppterm NCicPp.trivial_pp_term;
68   Helm_registry.load_from "conf.xml";
69   let alluris = 
70     try
71       let s = Sys.argv.(1) in
72       if s = "-alluris" then
73        begin
74         let uri_re = Str.regexp ".*\\(ind\\|con\\)$" in
75         let uris = Http_getter.getalluris () in
76         let alluris = List.filter (fun u -> Str.string_match uri_re u 0) uris in
77         let oc = open_out "alluris.txt" in
78         List.iter (fun s -> output_string oc (s^"\n")) alluris;
79         close_out oc; 
80         []
81        end
82       else [s]
83     with Invalid_argument _ -> 
84       let r = ref [] in
85       let ic = open_in "alluris.txt" in
86       try while true do r := input_line ic :: !r; done; []
87       with _ -> List.rev !r
88   in
89   let alluris = 
90     HExtlib.filter_map
91       (fun u -> try Some (UriManager.uri_of_string u) with _ -> None) alluris 
92   in
93   (* brutal *)
94   prerr_endline "computing graphs to load...";
95   let roots_alluris = 
96    if not rank_all_dependencies then
97     alluris
98    else (
99     let dbd = HSql.quick_connect (LibraryDb.parse_dbd_conf ()) in
100      MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
101     let uniq l = 
102      HExtlib.list_uniq (List.sort UriManager.compare l) in
103     let who_uses u = 
104      uniq (List.map (fun (uri,_) -> UriManager.strip_xpointer uri)
105       (MetadataDeps.inverse_deps ~dbd u)) in
106     let rec fix acc l = 
107      let acc, todo = 
108       List.fold_left (fun (acc,todo) x ->
109         let w = who_uses x in
110         if w = [] then (x::acc,todo) else (acc,uniq (todo@w)))
111       (acc,[]) l
112      in
113      if todo = [] then uniq acc else fix acc todo
114     in
115      fix [] alluris)
116   in
117   prerr_endline "generating Coq graphs...";
118   CicEnvironment.set_trust (fun _ -> trust_environment);
119   List.iter
120    (fun u ->
121      prerr_endline (" - " ^ UriManager.string_of_uri u);
122      try
123        ignore(CicTypeChecker.typecheck u);
124      with 
125      | CicTypeChecker.AssertFailure s
126      | CicTypeChecker.TypeCheckerFailure s ->
127         prerr_endline (Lazy.force s);
128         assert false
129     ) roots_alluris;
130   prerr_endline "loading...";
131   List.iter 
132     (fun u -> 
133        prerr_endline ("  - "^UriManager.string_of_uri u);
134        try load_graph u with exn -> ())
135     roots_alluris;
136   prerr_endline "finished....";
137   let lll = List.sort compare (CicUniv.do_rank (get_graph ())) in
138   prerr_endline "caching objects";
139   let _ = 
140     let rec aux = function
141       | a::(b::_ as tl) ->
142          NCicEnvironment.add_constraint true (mk_type a) (mk_type b);
143          aux tl
144       | _ -> ()
145     in
146        aux lll
147   in
148   prerr_endline "ranked....";
149   HExtlib.profiling_enabled := false;
150   List.iter (fun uu ->
151     let uu= OCic2NCic.nuri_of_ouri uu in
152     indent := 0;
153     let o = NCicLibrary.get_obj uu in
154     try 
155       NCicTypeChecker.typecheck_obj o
156     with 
157     | NCicTypeChecker.AssertFailure s 
158     | NCicTypeChecker.TypeCheckerFailure s
159     | NCicEnvironment.ObjectNotFound s
160     | NCicEnvironment.BadDependency s as e -> 
161        prerr_endline ("######### " ^ Lazy.force s);
162        if not ignore_exc then raise e
163     )
164     alluris;
165   NCicEnvironment.invalidate ();
166   Gc.compact ();
167   HExtlib.profiling_enabled := true;
168   NCicTypeChecker.set_logger (fun _ -> ());
169   do_old_logging := false;
170   prerr_endline "typechecking, first with the new and then with the old kernel";
171   let prima = Unix.gettimeofday () in
172   List.iter 
173     (fun u ->
174        let u= OCic2NCic.nuri_of_ouri u in
175       indent := 0;
176       NCicTypeChecker.typecheck_obj (NCicLibrary.get_obj u))
177     alluris;
178   let dopo = Unix.gettimeofday () in
179   Gc.compact ();
180   let dopo2 = Unix.gettimeofday () in
181   Printf.eprintf "NEW typing: %3.2f, gc: %3.2f\n%!" (dopo -. prima) (dopo2 -.  dopo);
182   CicEnvironment.invalidate ();
183   Gc.compact ();
184   let prima = Unix.gettimeofday () in
185   List.iter (fun u -> ignore (CicTypeChecker.typecheck u)) alluris;
186   let dopo = Unix.gettimeofday () in
187   Gc.compact ();
188   let dopo2 = Unix.gettimeofday () in
189   Printf.eprintf "OLD typing: %3.2f, gc: %3.2f\n%!" (dopo -. prima) (dopo2 -. dopo)
190 ;;