]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_test/mqgtop.ml
Ctr+Backspace is now enabled. Used to perform "alternative" deletion.
[helm.git] / helm / ocaml / 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
52 let get_handle () = 
53    C.init (C.flags_of_string ! int_options)
54           (fun s -> print_string s; flush stdout) 
55              
56 let issue handle q =
57    let mode = [Open_wronly; Open_append; Open_creat; Open_text] in
58    let perm = 64 * 6 + 8 * 6 + 4 in
59    let time () =
60       let lt = Unix.localtime (Unix.time ()) in
61       "NEW LOG: " ^
62       string_of_int (lt.Unix.tm_mon + 1) ^ "-" ^
63       string_of_int (lt.Unix.tm_mday + 1) ^ "-" ^
64       string_of_int (lt.Unix.tm_year + 1900) ^ " " ^
65       string_of_int (lt.Unix.tm_hour) ^ ":" ^
66       string_of_int (lt.Unix.tm_min) ^ ":" ^
67       string_of_int (lt.Unix.tm_sec) 
68    in
69    let log q r = 
70       let och = open_out_gen mode perm ! log_file in
71       let out = output_string och in 
72       if ! query_num = 1 then out (time () ^ nl);
73       out ("Query: " ^ string_of_int ! query_num ^ nl);
74       U.text_of_query out q nl;
75       out ("Result: " ^ nl);
76       U.text_of_result out r nl;
77       close_out och
78    in
79    if ! show_queries then U.text_of_query (output_string stdout) q nl;
80    let r = I.execute handle q in    
81    U.text_of_result (output_string stdout) r nl;
82    if ! log_file <> "" then log q r; 
83    incr query_num;
84    flush stdout
85
86 let get_interp () =
87    let lexer = function
88       | TP.ID s                -> P.ID s
89       | TP.CONURI u            -> P.CONURI u
90       | TP.VARURI u            -> P.VARURI u
91       | TP.INDTYURI (u, p)     -> P.INDTYURI (u, p)
92       | TP.INDCONURI (u, p, s) -> P.INDCONURI (u, p, s)
93       | TP.LETIN               -> P.ALIAS
94       | TP.EOF                 -> P.EOF
95       | _                     -> assert false
96    in
97    let ich = open_in ! interp_file in
98    let lexbuf = Lexing.from_channel ich in
99    let f = P.interp (fun x -> lexer (TL.token x)) lexbuf in
100    close_in ich; f
101    
102 let get_terms interp = 
103    let interp = get_interp () in
104    let lexbuf = Lexing.from_channel stdin in
105    let rec aux () =
106       try
107          let dom, mk_term =
108             CicTextualParserContext.main [] [] CicTextualLexer.token lexbuf
109          in
110          (snd (mk_term interp)) :: aux ()
111       with
112       CicTextualParser0.Eof -> []
113    in
114    aux ()
115
116 let pp_type_of uri = 
117    let u = UriManager.uri_of_string uri in  
118    let s = match (CicEnvironment.get_obj u) with
119       | Cic.Constant (_, _, ty, _) -> CicPp.ppterm ty
120       | Cic.Variable (_, _, ty, _) -> CicPp.ppterm ty
121       | _                          -> "Current proof or inductive definition."      
122 (*
123       | Cic.CurrentProof (_,conjs,te,ty) ->
124       | C.InductiveDefinition _ ->
125 *)
126    in print_endline s; flush stdout
127
128 let rec display = function
129    | []           -> ()
130    | term :: tail -> 
131       display tail;
132       print_string ("? " ^ CicPp.ppterm term ^ nl);
133       flush stdout
134
135 let execute ich =
136    let lexbuf = Lexing.from_channel ich in
137    let handle = get_handle () in
138    let rec execute_aux () =
139       try 
140          let q = U.query_of_text lexbuf in
141          issue handle q; execute_aux ()
142       with End_of_file -> ()
143    in
144    execute_aux ();
145    C.close handle
146
147 let compose () =
148    let handle = get_handle () in  
149    let cl = P.specs L.spec_token (Lexing.from_channel stdin) in
150    issue handle (G.compose cl);
151    C.close handle
152
153 let locate name =
154    let handle = get_handle () in  
155    issue handle (G.locate name); 
156    C.close handle
157
158 let mpattern n m l =
159    let queries = ref [] in
160    let univ = Some [G.builtin G.MainHypothesis; G.builtin G.InHypothesis;
161                     G.builtin G.MainConclusion; G.builtin G.InConclusion] 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 G.builtin G.MainConclusion 
198                    else G.builtin G.InConclusion in
199       (u, p, None)
200    in
201    let univ = Some [G.builtin G.MainConclusion; G.builtin G.InConclusion] in
202    let handle = get_handle () in
203    let rec backward level = function
204       | []           -> ()
205       | term :: tail -> 
206          backward level tail;
207          print_string ("? " ^ CicPp.ppterm term ^ nl);
208          let t = U.start_time () in
209          let list_of_must, only = C1.out_restr [] [] term in
210          let max_level = pred (List.length list_of_must) in 
211          let must = List.nth list_of_must (min level max_level) in 
212          let rigth_must = List.map torigth_restriction must in
213          let rigth_only = Some (List.map torigth_restriction only) in
214          let q = G.query_of_constraints univ (rigth_must, [], []) (rigth_only , None, None) in 
215          if not (List.mem q ! queries) then
216          begin
217             issue handle q;
218             Printf.eprintf "[%i] " (pred ! query_num); flush stderr;
219             Printf.printf "%i GEN = %i: %s"
220                (pred ! query_num) (List.length must) 
221                (U.stop_time t ^ nl);
222             flush stdout; queries := q :: ! queries
223          end
224    in 
225    for level = max m n downto min m n do
226       Printf.eprintf "\nmqgtop: backward: trying level %i\n" level; 
227       flush stderr; backward level l
228    done;
229    Printf.eprintf "\nmqgtop: backward: %i queries issued\n" 
230       (List.length ! queries);
231    flush stderr;
232    C.close handle
233
234 let check () =
235    let handle = get_handle () in
236    Printf.eprintf 
237       "mqgtop: current options: %s, connection: %s\n"  
238       ! int_options (if C.connected handle then "on" else "off");
239    C.close handle
240
241 let prerr_help () =
242    prerr_endline "\nUSAGE: mqgtop.opt OPTIONS < INPUTFILE\n"; 
243    prerr_endline "The tool provides a textual interface to the HELM Query Generator, used for";
244    prerr_endline "testing purposes. mqgtop reads its input from stdin and produces ith output";
245    prerr_endline "in HTML on stdout. The options can be one ore more of the following.\n";
246    prerr_endline "OPTIONS:\n";
247    prerr_endline "-h  -help               shows this help message";
248    prerr_endline "-q  -show-queries       outputs generated queries";
249    prerr_endline "-l  -log-file FILE      sets the log file";
250    prerr_endline "-o  -options STRING     sets the interpreter options";
251    prerr_endline "-c  -check              checks the database connection";
252    prerr_endline "-t  -typeof URI         outputs the CIC type of the given HELM object";
253    prerr_endline "-x  -execute            issues a query given in the input file";
254    prerr_endline "-i  -interp FILE        sets the CIC short names interpretation file";
255    prerr_endline "-d  -disply             outputs the CIC terms given in the input file";
256    prerr_endline "-L  -locate ALIAS       issues the \"Locate\" query for the given alias";
257    prerr_endline "-C  -compose            issues the \"Compose\" query reading its specifications";
258    prerr_endline "                        from the input file"; 
259    prerr_endline "-B  -backward LEVEL     issues the \"Backward\" query for the given level on all";
260    prerr_endline "                        CIC terms in the input file";
261    prerr_endline "-MB -multi-backward MAX issues the \"Backward\" query for each level from max to 0";
262    prerr_endline "                        on all CIC terms in the input file";
263    prerr_endline "-P  -pattern LEVEL      issues the \"Pattern\" query for the given level on all";
264    prerr_endline "                        CIC terms in the input file";
265    prerr_endline "-MP -multi-pattern      issues the \"Pattern\" query for each level from 7 to 0";
266    prerr_endline "                        on all CIC terms in the input file\n";
267    prerr_endline "NOTES: * current interpreter options are:";
268    prerr_endline "         P (postgres), G (Galax), S (show statistics), Q (quiet)";
269    prerr_endline "       * CIC terms are read with the HELM CIC Textual Parser";
270    prerr_endline "       * -typeof does not work with inductive types and proofs in progress\n"
271
272 let rec parse = function
273    | [] -> ()
274    | ("-h"|"-help") :: rem -> prerr_help (); parse rem
275    | ("-i"|"-interp") :: arg :: rem -> interp_file := arg; parse rem
276    | ("-d"|"-display") :: rem -> display (get_terms ()); parse rem
277    | ("-t"|"-typeof") :: arg :: rem -> pp_type_of arg; parse rem
278    | ("-x"|"-execute") :: rem -> execute stdin; parse rem
279    | ("-q"|"-show-queries") :: rem -> show_queries := true; parse rem
280    | ("-o"|"-options") :: arg :: rem -> int_options := arg; parse rem
281    | ("-c"|"-check") :: rem -> check (); parse rem
282    | ("-l"|"-log-file") :: arg :: rem -> log_file := arg; parse rem
283    | ("-L"|"-Locate") :: arg :: rem -> locate arg; parse rem
284    | ("-C"|"-compose") :: rem -> compose (); parse rem   
285    | ("-M"|"-backward") :: arg :: rem ->
286       let m = (int_of_string arg) in mbackward m m (get_terms ()); parse rem
287    | ("-MB"|"-multi-backward") :: arg :: rem ->
288       let m = (int_of_string arg) in mbackward m 0 (get_terms ()); parse rem
289    | ("-P"|"-pattern") :: arg :: rem ->
290       let m = (int_of_string arg) in mpattern m m (get_terms ()); parse rem
291    | ("-MP"|"-multi-pattern") :: rem -> mpattern 7 0 (get_terms ()); parse rem
292    | _ :: rem -> parse rem
293
294 let _ =
295    let t = U.start_time () in
296    Logger.log_callback :=
297       (Logger.log_to_html 
298        ~print_and_flush:(fun s -> print_string s; flush stdout)) ; 
299    parse (List.tl (Array.to_list Sys.argv)); 
300    prerr_endline ("mqgtop: done in " ^ (U.stop_time t));
301    exit 0