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