]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/testlibrary.ml
- better error message on "unknown identifier"
[helm.git] / helm / gTopLevel / testlibrary.ml
1
2 open Printf
3
4 let mqi_debug_fun = ignore
5 let mqi_flags = []
6 let mqi_handle = MQIConn.init mqi_flags mqi_debug_fun
7
8 let verbose = false
9
10 exception Failure of string
11 exception Multiple_interpretations
12 let fail msg = raise (Failure msg)
13
14 let uri_predicate = ref BatchParser.constants_only
15
16 module DisambiguateCallbacks =
17  struct
18   let interactive_user_uri_choice
19    ~selection_mode ?ok ?enable_button_for_non_vars ~title ~msg ~id choices =
20      List.filter !uri_predicate choices
21
22   let interactive_interpretation_choice _ = raise Multiple_interpretations
23   let input_or_locate_uri ~title = fail ("Unknown identifier: " ^ title)
24  end
25
26 module Disambiguate' = Disambiguate.Make (DisambiguateCallbacks)
27
28 let debug_print s = prerr_endline ("^^^^^^ " ^ s)
29
30 let test_uri uri =
31   let obj = CicEnvironment.get_obj uri in
32   let (annobj, _, _, ids_to_inner_sorts, _, _, _) =
33     Cic2acic.acic_object_of_cic_object ~eta_fix:false obj
34   in
35   let ids_to_uris = Hashtbl.create 1023 in
36   let round_trip annterm =
37     debug_print "(1) acic -> ast";
38     let (ast, _) =
39       Acic2Ast.ast_of_acic ids_to_inner_sorts ids_to_uris annterm
40     in
41     let new_pp = BoxPp.pp_term ast in
42     debug_print ("ast:\n" ^ new_pp);
43     let new_ast = CicTextualParser2.parse_term (Stream.of_string new_pp) in
44     debug_print ("new_ast:\n" ^ CicAstPp.pp_term ast);
45     let (_, _, term) =
46       Disambiguate'.disambiguate_term mqi_handle [] [] new_ast
47         DisambiguateTypes.Environment.empty
48     in
49     debug_print ("term: " ^ CicPp.ppterm term)
50   in
51   match annobj with
52   | Cic.AConstant (_, _, _, None, ty, _) ->
53       debug_print "Cic.AConstant (ty)";
54       round_trip ty
55   | Cic.AConstant (_, _, _, Some bo, ty, _) ->
56 (*
57       debug_print "Cic.AConstant (bo)";
58       round_trip bo;
59 *)
60       debug_print "Cic.AConstant (ty)";
61       round_trip ty
62   | Cic.AVariable (_, _, None, ty, _) ->
63       debug_print "Cic.AVariable (ty)";
64       round_trip ty
65   | Cic.AVariable (_, _, Some bo, ty, _) ->
66       debug_print "Cic.AVariable (bo)";
67       round_trip bo;
68       debug_print "Cic.AVariable (ty)";
69       round_trip ty
70   | Cic.ACurrentProof (_, _, _, _, proof, ty, _) ->
71       debug_print "Cic.ACurrentProof (proof)";
72       round_trip proof;
73       debug_print "Cic.ACurrentProof (ty)";
74       round_trip ty
75   | Cic.AInductiveDefinition _ ->
76       debug_print "AInductiveDefinition: boh ..."
77
78 let test_uri uri =
79   try
80     test_uri uri;
81     `Ok
82   with
83   | Multiple_interpretations -> `Maybe
84   | exn ->
85       prerr_endline (sprintf "Top Level Uncaught Exception: %s"
86         (Printexc.to_string exn));
87       `Nok
88
89 let report (ok,nok,maybe) =
90   print_newline ();
91   print_endline "TestLibrary report";
92   print_endline "Succeeded URIs:";
93   List.iter (fun s -> print_endline ("\t" ^ s)) (List.rev !ok);
94   print_endline "Failed URIs:";
95   List.iter (fun s -> print_endline ("\t" ^ s)) (List.rev !nok);
96   print_endline "Multiple answers URIs:";
97   List.iter (fun s -> print_endline ("\t" ^ s)) (List.rev !maybe);
98   print_newline ()
99
100 let do_uri (ok, nok, maybe) uri =
101   let uri_str = UriManager.string_of_uri uri in
102   printf "Testing URI: %-55s %!" (uri_str ^ " ...");
103   match test_uri uri with
104   | `Ok ->
105       print_endline "\e[01;32m[   OK   ]\e[00m";
106       ok := uri_str :: !ok
107   | `Nok ->
108       print_endline "\e[01;31m[ FAILED ]\e[00m";
109       nok := uri_str :: !nok
110   | `Maybe ->
111       print_endline "\e[01;33m[  MANY  ]\e[00m";
112       maybe := uri_str :: !maybe
113
114 let do_file status fname =
115   try
116     let ic = open_in fname in
117     (try
118       while true do
119         let line = input_line ic in
120         try
121           let uri = UriManager.uri_of_string line in
122           do_uri status uri
123         with UriManager.IllFormedUri _ ->
124           printf "Error parsing URI '%s', ignoring it" line
125       done
126     with End_of_file ->
127      close_in ic)
128   with exn ->
129     printf "Error trying to access '%s' (%s), skipping the file\n%!"
130       fname (Printexc.to_string exn)
131
132 let _ =
133   Helm_registry.load_from "triciclo.conf.xml";
134   HelmLogger.register_log_callback
135    (fun ?(append_NL = true) msg ->
136      (if append_NL then prerr_endline else prerr_string)
137        (HelmLogger.string_of_html_msg msg));
138   let names = ref [] in
139   let tryvars = ref false in
140   let varsprefix = ref "" in
141   let usage = "testlibrary [OPTION] ... (uri1 | file1) (uri2 | file2) ..." in
142   let spec =
143     [ "-vars", Arg.Set tryvars, "try also variables" ;
144       "-novars", Arg.Clear tryvars, "do not try variables (default)" ;
145       "-varsprefix", Arg.Set_string varsprefix,
146         "limit variable choices to URIs beginning with prefix" ;
147     ]
148   in
149   Arg.parse spec (fun name -> names := name :: !names) usage;
150   let names = List.rev !names in
151   uri_predicate := BatchParser.uri_pred_of_conf !tryvars !varsprefix;
152   let status = (ref [], ref [], ref []) in  (* <ok, nok, maybe> URIs *)
153   List.iter
154     (fun name ->
155       try
156         let uri = UriManager.uri_of_string name in
157         do_uri status uri
158       with UriManager.IllFormedUri _ ->
159         if Sys.file_exists name then
160           do_file status name
161         else
162           printf "Don't know what to do with '%s', ignoring it\n%!" name)
163     names ;
164   report status