]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/check.ml
Preparing for 0.5.9 release.
[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      [`Type, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
57   else
58      [`Type, 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     [`CProp, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
63   else
64     [`CProp, NUri.uri_of_string ("cic:/matita/pts/Type"^string_of_int n^".univ")]
65 ;;
66
67
68 let _ =
69   Sys.catch_break true;
70   let do_old_logging = ref true in
71   HelmLogger.register_log_callback
72    (fun ?append_NL html_msg ->
73      if !do_old_logging then
74       prerr_endline (HelmLogger.string_of_html_msg html_msg));
75   CicParser.impredicative_set := false;
76   NCicTypeChecker.set_logger logger;
77   Helm_registry.load_from "conf.xml";
78   let alluris = 
79     try
80       let s = Sys.argv.(1) in
81       if s = "-alluris" then
82        begin
83         let uri_re = Str.regexp ".*\\(ind\\|con\\)$" in
84         let uris = Http_getter.getalluris () in
85         let alluris = List.filter (fun u -> Str.string_match uri_re u 0) uris in
86         let oc = open_out "alluris.txt" in
87         List.iter (fun s -> output_string oc (s^"\n")) alluris;
88         close_out oc; 
89         []
90        end
91       else [s]
92     with Invalid_argument _ -> 
93       let r = ref [] in
94       let ic = open_in "alluris.txt" in
95       try while true do r := input_line ic :: !r; done; []
96       with _ -> List.rev !r
97   in
98   let alluris = 
99     HExtlib.filter_map
100       (fun u -> try Some (UriManager.uri_of_string u) with _ -> None) alluris 
101   in
102   (* brutal *)
103   prerr_endline "computing graphs to load...";
104   let roots_alluris = 
105    if not rank_all_dependencies then
106     alluris
107    else (
108     let dbd = HSql.quick_connect (LibraryDb.parse_dbd_conf ()) in
109      MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
110     let uniq l = 
111      HExtlib.list_uniq (List.sort UriManager.compare l) in
112     let who_uses u = 
113      uniq (List.map (fun (uri,_) -> UriManager.strip_xpointer uri)
114       (MetadataDeps.inverse_deps ~dbd u)) in
115     let rec fix acc l = 
116      let acc, todo = 
117       List.fold_left (fun (acc,todo) x ->
118         let w = who_uses x in
119         if w = [] then (x::acc,todo) else (acc,uniq (todo@w)))
120       (acc,[]) l
121      in
122      if todo = [] then uniq acc else fix acc todo
123     in
124      fix [] alluris)
125   in
126   prerr_endline "generating Coq graphs...";
127   CicEnvironment.set_trust (fun _ -> trust_environment);
128   List.iter
129    (fun u ->
130      prerr_endline (" - " ^ UriManager.string_of_uri u);
131      try
132        ignore(CicTypeChecker.typecheck u);
133      with 
134      | CicTypeChecker.AssertFailure s
135      | CicTypeChecker.TypeCheckerFailure s ->
136         prerr_endline (Lazy.force s);
137         assert false
138     ) roots_alluris;
139   prerr_endline "loading...";
140   List.iter 
141     (fun u -> 
142        prerr_endline ("  - "^UriManager.string_of_uri u);
143        try load_graph u with exn -> ())
144     roots_alluris;
145   prerr_endline "finished....";
146   let lll, uuu =(CicUniv.do_rank (get_graph ())) in
147   let lll = List.sort compare lll in
148   List.iter (fun k -> 
149     prerr_endline (CicUniv.string_of_universe k ^ " = " ^ string_of_int (CicUniv.get_rank k))) uuu;
150   let _ = 
151     try
152     let rec aux = function
153       | a::(b::_ as tl) ->
154          NCicEnvironment.add_lt_constraint (mk_type a) (mk_type b);
155          NCicEnvironment.add_lt_constraint (mk_cprop a) (mk_cprop b);
156          aux tl
157       | _ -> ()
158     in
159        aux lll
160     with NCicEnvironment.BadConstraint s as e ->
161       prerr_endline (Lazy.force s); raise e
162   in
163   prerr_endline "ranked....";
164   prerr_endline (NCicEnvironment.pp_constraints ());
165 (*
166   let [_,type0_uri] = mk_type 0 in
167   let [_,type1_uri] = mk_type 1 in
168   let [_,type2_uri] = mk_type 2 in
169   prerr_endline 
170    ("Min:" ^ 
171    (match NCicEnvironment.sup [true,type0_uri;true,type2_uri] with
172        | None -> "NO SUP"
173        | Some t -> NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] 
174           (NCic.Sort (NCic.Type t))));
175 *)
176   HExtlib.profiling_enabled := false;
177   List.iter (fun uu ->
178     let uu= OCic2NCic.nuri_of_ouri uu in
179     indent := 0;
180     let o = NCicLibrary.get_obj uu in
181     if print_object then prerr_endline (NCicPp.ppobj o); 
182     try 
183       NCicEnvironment.check_and_add_obj o
184     with 
185     | NCicTypeChecker.AssertFailure s 
186     | NCicTypeChecker.TypeCheckerFailure s
187     | NCicEnvironment.ObjectNotFound s
188     | NCicEnvironment.BadConstraint s
189     | NCicEnvironment.BadDependency (s,_) as e -> 
190        prerr_endline ("######### " ^ Lazy.force s);
191        if not ignore_exc then raise e
192     )
193     alluris;
194   NCicEnvironment.invalidate ();
195   Gc.compact ();
196   HExtlib.profiling_enabled := true;
197   NCicTypeChecker.set_logger (fun _ -> ());
198   do_old_logging := false;
199   prerr_endline "typechecking, first with the new and then with the old kernel";
200   List.iter 
201     (fun u ->
202       let u = OCic2NCic.nuri_of_ouri u in
203       indent := 0;
204       match NCicLibrary.get_obj u with
205       | _,_,_,_,NCic.Constant (_,_,Some bo, ty, _) ->
206           let rec intros = function
207             | NCic.Prod (name, s, t) ->
208                 let ctx, t = intros t in
209                 ctx @ [(name, (NCic.Decl s))] , t
210             | t -> [], t
211           in
212           let rec perforate ctx metasenv = function
213             | NCic.Appl (NCic.Const (NReference.Ref (u,_))::ty::_)
214               when NUri.string_of_uri u = "cic:/matita/tests/hole.con" ->
215                 let metasenv, ty =  perforate ctx metasenv ty in
216                 let a,_,b,_ = 
217                   NCicMetaSubst.mk_meta metasenv ctx (`WithType ty) in a,b
218             | t -> 
219                 NCicUntrusted.map_term_fold_a
220                  (fun e ctx -> e::ctx) ctx perforate metasenv t
221           in
222           let rec curryfy ctx = function
223             | NCic.Lambda (name, (NCic.Sort _ as s), tgt) ->
224                 NCic.Lambda (name, s, curryfy ((name,NCic.Decl s) :: ctx) tgt)
225             | NCic.Lambda (name, s, tgt) ->
226                 let tgt = curryfy ((name,NCic.Decl s) :: ctx) tgt in
227                 NCic.Lambda (name, NCic.Implicit `Type, tgt)
228             | t -> 
229                 NCicUtils.map
230                  (fun e ctx -> e::ctx) ctx curryfy t
231           in
232 (*
233           let ctx, pty = intros ty in
234           let metasenv, pty = perforate ctx [] pty in
235 *)
236 (*
237           let sty, metasenv, _ = 
238             NCicMetaSubst.saturate ~delta:max_int [] ctx ty 0
239           in
240 *)
241 (*           let ctx, ty = intros ty in *)
242 (*
243           let left, right = 
244             match  NCicReduction.whd ~delta:max_int ctx pty with
245             | NCic.Appl [eq;t;a;b] -> a, b
246             | _-> assert false
247           in
248 *)             
249
250 (*
251           let whd ty =
252             match ty with
253             | NCic.Appl [eq;t;a;b] ->
254                NCic.Appl [eq;t;NCicReduction.whd ~delta:0 ctx a;b]
255             | t -> NCicReduction.whd ~delta:0 ctx t
256           in
257 *)
258 (*
259                 prerr_endline 
260                  (Printf.sprintf "%s == %s"
261                  (NCicPp.ppterm ~metasenv:metasenv ~subst:[] ~context:ctx ity)
262                  (NCicPp.ppterm ~metasenv:metasenv ~subst:[] ~context:ctx sty));
263 *)
264           prerr_endline ("start: " ^ NUri.string_of_uri u);
265           let bo = curryfy [] bo in
266           (try 
267             let rdb = new NRstatus.status in
268             let metasenv, subst, bo, infty = 
269               NCicRefiner.typeof rdb [] [] [] bo None
270             in
271             let metasenv, subst = 
272               try 
273                 NCicUnification.unify rdb metasenv subst [] infty ty
274               with
275               | NCicUnification.Uncertain msg 
276               | NCicUnification.UnificationFailure msg 
277               | NCicMetaSubst.MetaSubstFailure msg ->
278                   prerr_endline (Lazy.force msg); 
279                   metasenv, subst
280               | Sys.Break -> metasenv, subst
281             in
282             if (NCicReduction.are_convertible ~metasenv ~subst [] infty ty)
283             then
284               prerr_endline ("OK: " ^ NUri.string_of_uri u)
285             else
286               (
287             let ctx = [] in
288             let right = infty in
289             let left = ty in
290                       
291                       prerr_endline ("FAIL: " ^ NUri.string_of_uri u);
292                   prerr_endline 
293                    (Printf.sprintf 
294                      ("\t\tRESULT OF UNIF\n\nsubst:\n%s\nmetasenv:\n%s\n" ^^ 
295                      "context:\n%s\nTERMS NO SUBST:\n%s\n==\n%s\n"^^
296                      "TERMS:\n%s\n==\n%s\n")
297                    (NCicPp.ppsubst ~metasenv subst)
298                    (NCicPp.ppmetasenv ~subst metasenv)
299                    (NCicPp.ppcontext ~metasenv ~subst ctx)
300                    (NCicPp.ppterm ~metasenv ~subst:[] ~context:ctx left)
301                    (NCicPp.ppterm ~metasenv ~subst:[] ~context:ctx right)
302                    (NCicPp.ppterm ~metasenv ~subst ~context:ctx left)
303                    (NCicPp.ppterm ~metasenv ~subst ~context:ctx right) ))
304             (*let inferred_ty = 
305               NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] bo
306             in*)
307           with
308           | Sys.Break -> ()
309           | NCicRefiner.RefineFailure msg 
310           | NCicRefiner.Uncertain msg ->
311              let _, msg = Lazy.force msg in
312              prerr_endline msg;
313              prerr_endline ("FAIL: " ^ NUri.string_of_uri u)
314           | e -> 
315              prerr_endline (Printexc.to_string e); 
316              prerr_endline ("FAIL: " ^ NUri.string_of_uri u)
317              )
318       | _ -> ())
319     alluris;
320 ;;