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