]> matita.cs.unibo.it Git - helm.git/blob - helm/mathql_test/mqgtop.ml
9a7c8d9db87814db876f1deacd63cf4e866a7780
[helm.git] / helm / mathql_test / mqgtop.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 let query_num = ref 1
30
31 let interp_file = ref "interp.cic" 
32
33 let log_file = ref ""
34
35 let show_queries = ref false
36
37 let int_options = ref ""
38
39 let nl = " <p>\n"
40
41 module U  = MQueryUtil
42 module I  = MQueryInterpreter
43 module C  = MQIConn
44 module G  = MQueryGenerator
45 module L  = MQGTopLexer
46 module P  = MQGTopParser
47 module TL = CicTextualLexer
48 module TP = CicTextualParser
49 module C2 = MQueryLevels2
50 module C1 = MQueryLevels
51 module GU = MQGUtil
52
53 let get_handle () = 
54    C.init (C.flags_of_string ! int_options)
55           (fun s -> print_string s; flush stdout) 
56              
57 let issue handle q =
58    let mode = [Open_wronly; Open_append; Open_creat; Open_text] in
59    let perm = 64 * 6 + 8 * 6 + 4 in
60    let time () =
61       let lt = Unix.localtime (Unix.time ()) in
62       "NEW LOG: " ^
63       string_of_int (lt.Unix.tm_mon + 1) ^ "-" ^
64       string_of_int (lt.Unix.tm_mday + 1) ^ "-" ^
65       string_of_int (lt.Unix.tm_year + 1900) ^ " " ^
66       string_of_int (lt.Unix.tm_hour) ^ ":" ^
67       string_of_int (lt.Unix.tm_min) ^ ":" ^
68       string_of_int (lt.Unix.tm_sec) 
69    in
70    let log q r = 
71       let och = open_out_gen mode perm ! log_file in
72       let out = output_string och in 
73       if ! query_num = 1 then out (time () ^ nl);
74       out ("Query: " ^ string_of_int ! query_num ^ nl);
75       U.text_of_query out q nl;
76       out ("Result: " ^ nl);
77       U.text_of_result out r nl;
78       close_out och
79    in
80    if ! show_queries then U.text_of_query (output_string stdout) q nl;
81    let r = I.execute handle q in    
82    U.text_of_result (output_string stdout) r nl;
83    if ! log_file <> "" then log q r; 
84    incr query_num;
85    flush stdout
86
87 let get_interp () =
88    let lexer = function
89       | TP.ID s                -> P.ID s
90       | TP.CONURI u            -> P.CONURI u
91       | TP.VARURI u            -> P.VARURI u
92       | TP.INDTYURI (u, p)     -> P.INDTYURI (u, p)
93       | TP.INDCONURI (u, p, s) -> P.INDCONURI (u, p, s)
94       | TP.LETIN               -> P.ALIAS
95       | TP.EOF                 -> P.EOF
96       | _                     -> assert false
97    in
98    let ich = open_in ! interp_file in
99    let lexbuf = Lexing.from_channel ich in
100    let f = P.interp (fun x -> lexer (TL.token x)) lexbuf in
101    close_in ich; f
102    
103 let get_terms interp = 
104    let interp = get_interp () in
105    let lexbuf = Lexing.from_channel stdin in
106    let rec aux () =
107       try
108          let dom, mk_term =
109             CicTextualParserContext.main [] [] CicTextualLexer.token lexbuf
110          in
111          (snd (mk_term interp)) :: aux ()
112       with
113       CicTextualParser0.Eof -> []
114    in
115    aux ()
116
117 let pp_type_of uri = 
118    let u = UriManager.uri_of_string uri in  
119    let s = match (CicEnvironment.get_obj u) with
120       | Cic.Constant (_, _, ty, _) -> CicPp.ppterm ty
121       | Cic.Variable (_, _, ty, _) -> CicPp.ppterm ty
122       | _                          -> "Current proof or inductive definition."      
123 (*
124       | Cic.CurrentProof (_,conjs,te,ty) ->
125       | C.InductiveDefinition _ ->
126 *)
127    in print_endline s; flush stdout
128
129 let rec display = function
130    | []           -> ()
131    | term :: tail -> 
132       display tail;
133       print_string ("? " ^ CicPp.ppterm term ^ nl);
134       flush stdout
135
136 let execute ich =
137    let lexbuf = Lexing.from_channel ich in
138    let handle = get_handle () in
139    let rec execute_aux () =
140       try 
141          let q = U.query_of_text lexbuf in
142          issue handle q; execute_aux ()
143       with End_of_file -> ()
144    in
145    execute_aux ();
146    C.close handle
147
148 let compose () =
149    let handle = get_handle () in  
150    let cl = P.specs L.spec_token (Lexing.from_channel stdin) in
151    issue handle (G.compose cl);
152    C.close handle
153
154 let locate name =
155    let handle = get_handle () in  
156    issue handle (G.locate name); 
157    C.close handle
158
159 let mpattern n m l =
160    let queries = ref [] in
161    let univ = Some GU.universe_for_search_pattern in 
162    let handle = get_handle () in
163    let rec pattern level = function
164       | []           -> ()
165       | term :: tail -> 
166          pattern level tail;
167          print_string ("? " ^ CicPp.ppterm term ^ nl);
168          let t = U.start_time () in
169          let om,rm,sm = C2.get_constraints term in
170          let oml,rml,sml = List.length om, List.length rm, List.length sm in 
171          let oo, ool = if level land 1 = 0 then None, 0 else Some om, oml in
172          let ro, rol = if level land 2 = 0 then None, 0 else Some rm, rml in
173          let so, sol = if level land 4 = 0 then None, 0 else Some sm, sml in
174          let q = G.query_of_constraints univ (om,rm,sm) (oo,ro,so) in 
175          if not (List.mem q ! queries) then
176          begin
177             issue handle q;
178             Printf.eprintf "[%i] " (pred ! query_num); flush stderr;
179             Printf.printf "%i GEN = %i: %s"
180                (pred ! query_num) (oml + rml + sml + ool + rol + sol) 
181                (U.stop_time t ^ nl);
182             flush stdout; queries := q :: ! queries
183          end
184    in 
185    for level = max m n downto min m n do
186       Printf.eprintf "\nmqgtop: pattern: trying level %i\n" level; 
187       flush stderr; pattern level l
188    done;
189    Printf.eprintf "\nmqgtop: pattern: %i queries issued\n" 
190       (List.length ! queries);
191    flush stderr;
192    C.close handle
193
194 let mbackward n m l =
195    let queries = ref [] in
196    let torigth_restriction (u, b) =
197       let p = if b then `MainConclusion None else `InConclusion in (p, u)
198    in
199    let univ = Some GU.universe_for_match_conclusion in
200    let handle = get_handle () in
201    let rec backward level = function
202       | []           -> ()
203       | term :: tail -> 
204          backward level tail;
205          print_string ("? " ^ CicPp.ppterm term ^ nl);
206          let t = U.start_time () in
207          let list_of_must, only = C1.out_restr [] [] term in
208          let max_level = pred (List.length list_of_must) in 
209          let must = List.nth list_of_must (min level max_level) in 
210          let rigth_must = List.map torigth_restriction must in
211          let rigth_only = Some (List.map torigth_restriction only) in
212          let q = G.query_of_constraints univ (rigth_must, [], []) (rigth_only , None, None) in 
213          if not (List.mem q ! queries) then
214          begin
215             issue handle q;
216             Printf.eprintf "[%i] " (pred ! query_num); flush stderr;
217             Printf.printf "%i GEN = %i: %s"
218                (pred ! query_num) (List.length must) 
219                (U.stop_time t ^ nl);
220             flush stdout; queries := q :: ! queries
221          end
222    in 
223    for level = max m n downto min m n do
224       Printf.eprintf "\nmqgtop: backward: trying level %i\n" level; 
225       flush stderr; backward level l
226    done;
227    Printf.eprintf "\nmqgtop: backward: %i queries issued\n" 
228       (List.length ! queries);
229    flush stderr;
230    C.close handle
231
232 let check () =
233    let handle = get_handle () in
234    Printf.eprintf 
235       "mqgtop: current options: %s, connection: %s\n"  
236       ! int_options (if C.connected handle then "on" else "off");
237    C.close handle
238
239 let prerr_help () =
240    prerr_endline "\nUSAGE: mqgtop.opt OPTIONS < INPUTFILE\n"; 
241    prerr_endline "The tool provides a textual interface to the HELM Query Generator, used for";
242    prerr_endline "testing purposes. mqgtop reads its input from stdin and produces ith output";
243    prerr_endline "in HTML on stdout. The options can be one ore more of the following.\n";
244    prerr_endline "OPTIONS:\n";
245    prerr_endline "-h  -help               shows this help message";
246    prerr_endline "-q  -show-queries       outputs generated queries";
247    prerr_endline "-l  -log-file FILE      sets the log file";
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 "-L  -locate ALIAS       issues the \"Locate\" query for the given alias";
255    prerr_endline "-C  -compose            issues the \"Compose\" query reading its specifications";
256    prerr_endline "                        from the input file"; 
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"
269
270 let rec parse = function
271    | [] -> ()
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 -> int_options := arg; parse rem
279    | ("-c"|"-check") :: rem -> check (); parse rem
280    | ("-l"|"-log-file") :: arg :: rem -> log_file := arg; parse rem
281    | ("-L"|"-Locate") :: arg :: rem -> locate arg; parse rem
282    | ("-C"|"-compose") :: rem -> compose (); parse rem   
283    | ("-M"|"-backward") :: arg :: rem ->
284       let m = (int_of_string arg) in mbackward m m (get_terms ()); parse rem
285    | ("-MB"|"-multi-backward") :: arg :: rem ->
286       let m = (int_of_string arg) in mbackward m 0 (get_terms ()); parse rem
287    | ("-P"|"-pattern") :: arg :: rem ->
288       let m = (int_of_string arg) in mpattern m m (get_terms ()); parse rem
289    | ("-MP"|"-multi-pattern") :: rem -> mpattern 7 0 (get_terms ()); parse rem
290    | _ :: rem -> parse rem
291
292 let _ =
293    let t = U.start_time () in
294    Logger.log_callback :=
295       (Logger.log_to_html 
296        ~print_and_flush:(fun s -> print_string s; flush stdout)) ; 
297    parse (List.tl (Array.to_list Sys.argv)); 
298    prerr_endline ("mqgtop: done in " ^ (U.stop_time t));
299    exit 0