1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
28 let interp_file = ref "interp.cic"
30 let log_file = ref "MQGenLog.htm"
32 let show_queries = ref false
34 let int_options = ref ""
38 module MQI = MQueryInterpreter
40 module MQG = MQueryGenerator
43 MQIC.init (MQIC.flags_of_string ! int_options)
44 (fun s -> print_string s; flush stdout)
47 let module U = MQueryUtil in
48 let mode = [Open_wronly; Open_append; Open_creat; Open_text] in
49 let perm = 64 * 6 + 8 * 6 + 4 in
51 let lt = Unix.localtime (Unix.time ()) in
53 string_of_int (lt.Unix.tm_mon + 1) ^ "-" ^
54 string_of_int (lt.Unix.tm_mday + 1) ^ "-" ^
55 string_of_int (lt.Unix.tm_year + 1900) ^ " " ^
56 string_of_int (lt.Unix.tm_hour) ^ ":" ^
57 string_of_int (lt.Unix.tm_min) ^ ":" ^
58 string_of_int (lt.Unix.tm_sec)
61 let och = open_out_gen mode perm ! log_file in
62 let out = output_string och in
63 if ! query_num = 1 then out (time () ^ nl);
64 out ("Query: " ^ string_of_int ! query_num ^ nl);
65 U.text_of_query out q nl;
66 out ("Result: " ^ nl);
67 U.text_of_result out r nl;
70 if ! show_queries then U.text_of_query (output_string stdout) q nl;
71 let r = MQI.execute handle q in
72 U.text_of_result (output_string stdout) r nl;
73 if ! log_file <> "" then log q r;
78 let module L = CicTextualLexer in
79 let module T = CicTextualParser in
80 let module P = MQGTopParser in
83 | T.CONURI u -> P.CONURI u
84 | T.VARURI u -> P.VARURI u
85 | T.INDTYURI (u, p) -> P.INDTYURI (u, p)
86 | T.INDCONURI (u, p, s) -> P.INDCONURI (u, p, s)
91 let ich = open_in ! interp_file in
92 let lexbuf = Lexing.from_channel ich in
93 let f = P.interp (fun x -> lexer (L.token x)) lexbuf in
96 let get_terms interp =
97 let interp = get_interp () in
98 let lexbuf = Lexing.from_channel stdin in
102 CicTextualParserContext.main [] [] CicTextualLexer.token lexbuf
104 (snd (mk_term interp)) :: aux ()
106 CicTextualParser0.Eof -> []
111 let u = UriManager.uri_of_string uri in
112 let s = match (CicEnvironment.get_obj u) with
113 | Cic.Constant (_, _, ty, _) -> CicPp.ppterm ty
114 | Cic.Variable (_, _, ty, _) -> CicPp.ppterm ty
115 | _ -> "Current proof or inductive definition."
117 | Cic.CurrentProof (_,conjs,te,ty) ->
118 | C.InductiveDefinition _ ->
120 in print_endline s; flush stdout
122 let rec display = function
126 print_string ("? " ^ CicPp.ppterm term ^ nl);
130 let module U = MQueryUtil in
131 let lexbuf = Lexing.from_channel ich in
132 let handle = get_handle () in
133 let rec execute_aux () =
135 let q = U.query_of_text lexbuf in
136 issue handle q; execute_aux ()
137 with End_of_file -> ()
143 let module P = MQGTopParser in
144 let module L = MQGTopLexer in
145 let module G = MQueryGeneratorNew in
146 let cl = P.specs L.spec_token (Lexing.from_channel stdin) in
150 let handle = get_handle () in
151 issue handle (MQG.locate name);
155 let module C = MQueryLevels2 in
156 let queries = ref [] in
157 let handle = get_handle () in
158 let rec pattern level = function
162 print_string ("? " ^ CicPp.ppterm term ^ nl);
163 let t0 = Sys.time () in
164 let om,rm,sm = C.get_constraints term in
165 let oml,rml,sml = List.length om, List.length rm, List.length sm in
166 let oo, ool = if level land 1 = 0 then None, 0 else Some om, oml in
167 let ro, rol = if level land 2 = 0 then None, 0 else Some rm, rml in
168 let so, sol = if level land 4 = 0 then None, 0 else Some sm, sml in
169 let q = MQG.searchPattern (om,rm,sm) (oo,ro,so) in
170 if not (List.mem q ! queries) then
173 let t1 = Sys.time () -. t0 in
174 Printf.eprintf "[%i] " (pred ! query_num); flush stderr;
175 Printf.printf "%i GEN = %i: %.2fs%s"
176 (pred ! query_num) (oml + rml + sml + ool + rol + sol) t1 nl;
177 flush stdout; queries := q :: ! queries
180 for level = max m n downto min m n do
181 Printf.eprintf "\nmqgtop: pattern: trying level %i\n" level;
182 flush stderr; pattern level l
184 Printf.eprintf "\nmqgtop: pattern: %i queries issued\n"
185 (List.length ! queries);
189 let mbackward n m l =
190 let module C = MQueryLevels in
191 let module G = MQueryGenerator in
192 let queries = ref [] in
193 let torigth_restriction (u, b) =
195 then "http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion"
196 else "http://www.cs.unibo.it/helm/schemas/schema-helm#InConclusion"
200 let rec backward level = function
204 print_string ("? " ^ CicPp.ppterm term ^ nl);
205 let t0 = Sys.time () in
206 let list_of_must, only = C.out_restr [] [] term in
207 let max_level = pred (List.length list_of_must) in
208 let must = List.nth list_of_must (min level max_level) in
209 let rigth_must = List.map torigth_restriction must in
210 let rigth_only = Some (List.map torigth_restriction only) in
211 let q = G.searchPattern (rigth_must, [], []) (rigth_only , None, None) in
212 if not (List.mem q ! queries) then
215 let t1 = Sys.time () -. t0 in
216 Printf.eprintf "[%i] " (pred ! query_num); flush stderr;
217 Printf.printf "%i GEN = %i: %.2fs%s"
218 (pred ! query_num) (List.length must) t1 nl;
219 flush stdout; queries := q :: ! queries
222 for level = max m n downto min m n do
223 Printf.eprintf "\nmqgtop: backward: trying level %i\n" level;
224 flush stderr; backward level l
226 Printf.eprintf "\nmqgtop: backward: %i queries issued\n"
227 (List.length ! queries);
231 let set_options s = int_options := s
234 let handle = get_handle () in
236 "mqgtop: current options: %s, connection: %s\n"
237 ! int_options (if MQIC.connected handle then "on" else "off");
241 prerr_endline "\nUSAGE: mqgtop.opt OPTIONS < INPUTFILE\n";
242 prerr_endline "The tool provides a textual interface to the HELM Query Generator, used for";
243 prerr_endline "testing purposes. mqgtop reads its input from stdin and produces ith output";
244 prerr_endline "in HTML on stdout. The options can be one ore more of the following.\n";
245 prerr_endline "OPTIONS:\n";
246 prerr_endline "-h -help shows this help message";
247 prerr_endline "-q -show-queries outputs generated queries";
248 prerr_endline "-o -options STRING sets the interpreter options";
249 prerr_endline "-c -check checks the database connection";
250 prerr_endline "-t -typeof URI outputs the CIC type of the given HELM object";
251 prerr_endline "-x -execute issues a query given in the input file";
252 prerr_endline "-i -interp FILE sets the CIC short names interpretation file";
253 prerr_endline "-d -disply outputs the CIC terms given in the input file";
254 prerr_endline "-C -compose issues the \"Compose\" query reading its specifications";
255 prerr_endline " from the input file";
256 prerr_endline "-L -locate ALIAS issues the \"Locate\" query for the given alias";
257 prerr_endline "-B -backward LEVEL issues the \"Backward\" query for the given level on all";
258 prerr_endline " CIC terms in the input file";
259 prerr_endline "-MB -multi-backward MAX issues the \"Backward\" query for each level from max to 0";
260 prerr_endline " on all CIC terms in the input file";
261 prerr_endline "-P -pattern LEVEL issues the \"Pattern\" query for the given level on all";
262 prerr_endline " CIC terms in the input file";
263 prerr_endline "-MP -multi-pattern issues the \"Pattern\" query for each level from 7 to 0";
264 prerr_endline " on all CIC terms in the input file\n";
265 prerr_endline "NOTES: * current interpreter options are:";
266 prerr_endline " P (postgres), G (Galax), S (show statistics), Q (quiet)";
267 prerr_endline " * CIC terms are read with the HELM CIC Textual Parser";
268 prerr_endline " * -typeof does not work with inductive types and proofs in progress\n"
270 let rec parse = function
272 | ("-h"|"-help") :: rem -> prerr_help (); parse rem
273 | ("-i"|"-interp") :: arg :: rem -> interp_file := arg; parse rem
274 | ("-d"|"-display") :: rem -> display (get_terms ()); parse rem
275 | ("-t"|"-typeof") :: arg :: rem -> pp_type_of arg; parse rem
276 | ("-x"|"-execute") :: rem -> execute stdin; parse rem
277 | ("-q"|"-show-queries") :: rem -> show_queries := true; parse rem
278 | ("-o"|"-options") :: arg :: rem -> set_options arg; parse rem
279 | ("-c"|"-check") :: rem -> check (); parse rem
280 (* | ("-C"|"-compose") :: rem -> compose (); parse rem
281 *) | ("-L"|"-locate") :: arg :: rem -> locate arg; parse rem
282 (* | ("-M"|"-backward") :: arg :: rem ->
283 let m = (int_of_string arg) in mbackward m m (get_terms ()); parse rem
284 | ("-MB"|"-multi-backward") :: arg :: rem ->
285 let m = (int_of_string arg) in mbackward m 0 (get_terms ()); parse rem
286 *) | ("-P"|"-pattern") :: arg :: rem ->
287 let m = (int_of_string arg) in mpattern m m (get_terms ()); parse rem
288 | ("-MP"|"-multi-pattern") :: rem -> mpattern 7 0 (get_terms ()); parse rem
289 | _ :: rem -> parse rem
292 Logger.log_callback :=
294 ~print_and_flush:(function s -> print_string s ; flush stdout)) ;
295 parse (List.tl (Array.to_list Sys.argv));
296 prerr_endline ("mqgtop: done in " ^ string_of_float (Sys.time ()) ^