]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/check.ml
better debuggin output
[helm.git] / helm / software / components / ng_refiner / 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 let print_object = false
19
20 let indent = ref 0;;
21
22 let load_graph, get_graph =
23  let oldg = ref CicUniv.empty_ugraph in
24   (function uri -> 
25     let _,g = CicEnvironment.get_obj !oldg uri in
26      oldg := g),
27   (function _ -> !oldg)
28 ;;
29
30 let logger =
31     let do_indent () = String.make !indent ' ' in  
32     (function 
33       | `Start_type_checking s ->
34           if debug then
35            prerr_endline (do_indent () ^ "Start: " ^ NUri.string_of_uri s); 
36           incr indent
37       | `Type_checking_completed s ->
38           decr indent;
39           if debug then
40            prerr_endline (do_indent () ^ "End: " ^ NUri.string_of_uri s)
41       | `Type_checking_interrupted s ->
42           decr indent;
43           if debug then
44            prerr_endline (do_indent () ^ "Break: " ^ NUri.string_of_uri s)
45       | `Type_checking_failed s ->
46           decr indent;
47           if debug then
48            prerr_endline (do_indent () ^ "Fail: " ^ NUri.string_of_uri s)
49       | `Trust_obj s ->
50           if debug then
51            prerr_endline (do_indent () ^ "Trust: " ^ NUri.string_of_uri s))
52 ;;
53
54 let mk_type n = 
55   if n = 0 then
56      [false, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
57   else
58      [false, NUri.uri_of_string ("cic:/matita/pts/Type"^string_of_int n^".univ")]
59 ;;
60 let mk_cprop n = 
61   if n = 0 then 
62     [false, NUri.uri_of_string ("cic:/matita/pts/CProp.univ")]
63   else
64     [false, NUri.uri_of_string ("cic:/matita/pts/CProp"^string_of_int n^".univ")]
65 ;;
66
67
68 let _ =
69   let do_old_logging = ref true in
70   HelmLogger.register_log_callback
71    (fun ?append_NL html_msg ->
72      if !do_old_logging then
73       prerr_endline (HelmLogger.string_of_html_msg html_msg));
74   CicParser.impredicative_set := false;
75   NCicTypeChecker.set_logger logger;
76   Helm_registry.load_from "conf.xml";
77   let alluris = 
78     try
79       let s = Sys.argv.(1) in
80       if s = "-alluris" then
81        begin
82         let uri_re = Str.regexp ".*\\(ind\\|con\\)$" in
83         let uris = Http_getter.getalluris () in
84         let alluris = List.filter (fun u -> Str.string_match uri_re u 0) uris in
85         let oc = open_out "alluris.txt" in
86         List.iter (fun s -> output_string oc (s^"\n")) alluris;
87         close_out oc; 
88         []
89        end
90       else [s]
91     with Invalid_argument _ -> 
92       let r = ref [] in
93       let ic = open_in "alluris.txt" in
94       try while true do r := input_line ic :: !r; done; []
95       with _ -> List.rev !r
96   in
97   let alluris = 
98     HExtlib.filter_map
99       (fun u -> try Some (UriManager.uri_of_string u) with _ -> None) alluris 
100   in
101   (* brutal *)
102   prerr_endline "computing graphs to load...";
103   let roots_alluris = 
104    if not rank_all_dependencies then
105     alluris
106    else (
107     let dbd = HSql.quick_connect (LibraryDb.parse_dbd_conf ()) in
108      MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
109     let uniq l = 
110      HExtlib.list_uniq (List.sort UriManager.compare l) in
111     let who_uses u = 
112      uniq (List.map (fun (uri,_) -> UriManager.strip_xpointer uri)
113       (MetadataDeps.inverse_deps ~dbd u)) in
114     let rec fix acc l = 
115      let acc, todo = 
116       List.fold_left (fun (acc,todo) x ->
117         let w = who_uses x in
118         if w = [] then (x::acc,todo) else (acc,uniq (todo@w)))
119       (acc,[]) l
120      in
121      if todo = [] then uniq acc else fix acc todo
122     in
123      fix [] alluris)
124   in
125   prerr_endline "generating Coq graphs...";
126   CicEnvironment.set_trust (fun _ -> trust_environment);
127   List.iter
128    (fun u ->
129      prerr_endline (" - " ^ UriManager.string_of_uri u);
130      try
131        ignore(CicTypeChecker.typecheck u);
132      with 
133      | CicTypeChecker.AssertFailure s
134      | CicTypeChecker.TypeCheckerFailure s ->
135         prerr_endline (Lazy.force s);
136         assert false
137     ) roots_alluris;
138   prerr_endline "loading...";
139   List.iter 
140     (fun u -> 
141        prerr_endline ("  - "^UriManager.string_of_uri u);
142        try load_graph u with exn -> ())
143     roots_alluris;
144   prerr_endline "finished....";
145   let lll, uuu =(CicUniv.do_rank (get_graph ())) in
146   let lll = List.sort compare lll in
147   List.iter (fun k -> 
148     prerr_endline (CicUniv.string_of_universe k ^ " = " ^ string_of_int (CicUniv.get_rank k))) uuu;
149   let _ = 
150     try
151     let rec aux = function
152       | a::(b::_ as tl) ->
153          NCicEnvironment.add_constraint true (mk_type a) (mk_type b);
154          NCicEnvironment.add_constraint true (mk_cprop a) (mk_cprop b);
155          NCicEnvironment.add_constraint true (mk_cprop a) (mk_type b);
156          NCicEnvironment.add_constraint true (mk_type a) (mk_cprop b);
157          NCicEnvironment.add_constraint false (mk_cprop a) (mk_type a);
158          NCicEnvironment.add_constraint false (mk_type a) (mk_cprop a);
159          aux tl
160       | [a] -> 
161          NCicEnvironment.add_constraint false (mk_type a) (mk_cprop a);
162          NCicEnvironment.add_constraint false (mk_cprop a) (mk_type a);
163       | _ -> ()
164     in
165        aux lll
166     with NCicEnvironment.BadConstraint s as e ->
167       prerr_endline (Lazy.force s); raise e
168   in
169   prerr_endline "ranked....";
170   prerr_endline (NCicEnvironment.pp_constraints ());
171   HExtlib.profiling_enabled := false;
172   List.iter (fun uu ->
173     let uu= OCic2NCic.nuri_of_ouri uu in
174     indent := 0;
175     let o = NCicLibrary.get_obj uu in
176     if print_object then prerr_endline (NCicPp.ppobj o); 
177     try 
178       NCicTypeChecker.typecheck_obj o
179     with 
180     | NCicTypeChecker.AssertFailure s 
181     | NCicTypeChecker.TypeCheckerFailure s
182     | NCicEnvironment.ObjectNotFound s
183     | NCicEnvironment.BadConstraint s
184     | NCicEnvironment.BadDependency s as e -> 
185        prerr_endline ("######### " ^ Lazy.force s);
186        if not ignore_exc then raise e
187     )
188     alluris;
189   NCicEnvironment.invalidate ();
190   Gc.compact ();
191   HExtlib.profiling_enabled := true;
192   NCicTypeChecker.set_logger (fun _ -> ());
193   do_old_logging := false;
194   prerr_endline "typechecking, first with the new and then with the old kernel";
195   List.iter 
196     (fun u ->
197       let u = OCic2NCic.nuri_of_ouri u in
198       indent := 0;
199       match NCicLibrary.get_obj u with
200       | _,_,_,_,NCic.Constant (_,_,Some bo, ty, _) ->
201           let rec intros = function
202             | NCic.Prod (name, s, t) ->
203                 let ctx, t = intros t in
204                 ctx @ [(name, (NCic.Decl s))] , t
205             | t -> [], t
206           in
207           let ctx, ity = intros ty in
208           let sty, menv, _ = 
209             NCicMetaSubst.saturate ~delta:max_int [] ctx ty 0
210           in
211           let ctx, ty = intros ty in
212           let ity =
213             match ity with
214             | NCic.Appl [eq;t;a;b] ->
215                NCic.Appl [eq;t;NCicReduction.whd ~delta:0 ctx a;b]
216             | t -> t
217           in
218                 prerr_endline 
219                  (Printf.sprintf "%s == %s"
220                  (NCicPp.ppterm ~metasenv:menv ~subst:[] ~context:ctx ity)
221                  (NCicPp.ppterm ~metasenv:menv ~subst:[] ~context:ctx sty));
222           let metasenv, subst = 
223             try 
224                     prerr_endline ("INIZIO: " ^ NUri.string_of_uri u);
225               NCicUnification.unify menv [] ctx ity sty 
226             with
227             | NCicUnification.Uncertain msg 
228             | NCicUnification.UnificationFailure msg 
229             | NCicMetaSubst.MetaSubstFailure msg ->
230                 prerr_endline (Lazy.force msg); 
231                 prerr_endline 
232                  (Printf.sprintf "RESULT OF UNIF\n%s\n%s == %s\n"
233                  (NCicPp.ppcontext ~metasenv:menv ~subst:[] ctx)
234                  (NCicPp.ppterm ~metasenv:menv ~subst:[] ~context:ctx ity)
235                  (NCicPp.ppterm ~metasenv:menv ~subst:[] ~context:ctx sty));
236                 [], []
237           in
238           if (NCicReduction.are_convertible 
239              ~subst (fun ~subst:_ _ _ _ ->[]) ctx ity sty)
240           then
241             prerr_endline ("OK: " ^ NUri.string_of_uri u)
242           else
243             prerr_endline ("FAIL: " ^ NUri.string_of_uri u);
244           (*let inferred_ty = 
245             NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] bo
246           in*)
247       | _ -> ())
248     alluris;
249 ;;