2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
28 module AA = AutProcess
31 module XL = XmlLibrary
36 module BR = BrgReduction
37 module BU = BrgUntrusted
38 module BG = BrgGrafite
39 module BA = BrgGallina
44 module ZU = BagUntrusted
55 mst: B.manager option;
60 let bag_error st s msg =
61 L.error st.kst ZO.specs (L.Warn s :: msg)
63 let brg_error st s msg =
64 L.error st.kst BR.specs (L.Warn s :: msg)
66 let initial_status () = {
67 kst = N.initial_status ();
68 tst = TD.initial_status ();
69 pst = AA.initial_status ();
70 ast = AD.initial_status ();
71 ac = AO.initial_counters;
72 dc = DO.initial_counters;
73 bc = BO.initial_counters;
74 zc = ZO.initial_counters;
78 let refresh_status st = {st with
79 kst = N.refresh_status st.kst;
80 tst = TD.refresh_status st.tst;
81 ast = AD.refresh_status st.ast;
84 (* kernel related ***********************************************************)
86 type kernel_entity = BrgEntity of Brg.entity
87 | BagEntity of Bag.entity
88 | CrgEntity of Crg.entity
90 let print_counters st = function
91 | G.V4 -> DO.print_counters C.start st.dc
92 | G.V3 -> BO.print_counters C.start st.bc
93 | G.V0 -> ZO.print_counters C.start st.zc
95 let xlate_entity st entity = match !G.kernel, entity with
96 | G.V3, CrgEntity e ->
97 let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
98 | G.V0, CrgEntity e ->
99 let f e = (BagEntity e) in E.xlate f (ZD.bag_of_crg st.kst) e
100 | _, entity -> entity
104 let s = U.string_of_uri u in
105 L.warn 2 (KP.sprintf "[%u] <%s>" na.E.n_apix s);
107 Y.utime_stamp "intermediate";
109 | CrgEntity e -> E.common f e
110 | BrgEntity e -> E.common f e
111 | BagEntity e -> E.common f e
113 let count_entity st = function
114 | BrgEntity e -> {st with bc = BO.count_entity C.start st.bc e}
115 | BagEntity e -> {st with zc = ZO.count_entity C.start st.zc e}
116 | CrgEntity e -> {st with dc = DO.count_entity C.start st.dc e}
118 let export_entity st = function
119 | CrgEntity e -> XL.export_entity (XD.export_term st.kst) e
120 | BrgEntity e -> XL.export_entity (BO.export_term st.kst) e
121 | BagEntity e -> XL.export_entity (ZO.export_term st.kst) e
123 let type_check st k =
124 let brg_err msg = brg_error st "Type Error" msg; failwith "Interrupted" in
125 let bag_err msg = bag_error st "Type Error" msg; failwith "Interrupted" in
128 | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
129 | BagEntity entity -> ZU.type_check bag_err ok st.kst entity
133 let brg_err msg = brg_error st "Validation Error" msg; failwith "Interrupted" in
136 | BrgEntity entity -> BU.validate brg_err ok st.kst entity
140 let manager st output_entity = function
141 | BrgEntity entity ->
142 if output_entity st.kst entity then st else
143 begin L.warn level "manager exportation stopped"; {st with mst = None} end
147 (* extended lexer ***********************************************************)
149 type 'token lexer = {
150 parse : Lexing.lexbuf -> 'token;
151 mutable tokbuf: 'token option;
155 let initial_lexer parse = {
156 parse = parse; tokbuf = None; unget = false
159 let token xl lexbuf = match xl.tokbuf with
160 | Some token when xl.unget ->
161 xl.unget <- false; token
163 let token = xl.parse lexbuf in
164 xl.tokbuf <- Some token; token
166 (* input related ************************************************************)
168 type input = Text | Automath
170 type input_entity = TxtEntity of Txt.command
171 | AutEntity of Aut.command
174 let type_of_input name =
175 if KF.check_suffix name ".hln" then Text
176 else if KF.check_suffix name ".aut" then
177 let _ = H.set_sorts 0 ["Set"; "Prop"] in
178 assert (H.set_graph "Z2");
181 L.warn level (KP.sprintf "Unknown file type: %s" name); exit 2
184 let txt_xl = initial_lexer TxtLexer.token
186 let aut_xl = initial_lexer AutLexer.token
188 let parbuf = ref [] (* parser buffer *)
190 let gen_text command =
191 parbuf := TxtEntity command :: !parbuf
193 let entity_of_input lexbuf i = match i, !parbuf with
195 begin match AutParser.entry (token aut_xl) lexbuf with
196 | Some e -> aut_xl.unget <- true; AutEntity e
200 begin match TxtParser.entry (token txt_xl) lexbuf with
201 | Some e -> txt_xl.unget <- true; TxtEntity e
207 let process_input f st = function
209 let f pst e = f {st with pst = pst} (AutEntity e) in
210 AA.process_command f st.pst e
213 let count_input st = function
214 | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
217 (****************************************************************************)
219 let version = ref true
220 let preprocess = ref false
222 let export = ref false
223 let st = ref (initial_status ())
224 let streaming = ref false (* parsing style (temporary) *)
226 let process_2 st entity =
227 let st = if !G.summary then count_entity st entity else st in
229 if !G.stage >= 3 then
230 let f = if !version then validate else type_check in f st entity
233 if !export then export_entity st entity;
236 | Some (export_entity, _) -> manager st export_entity entity
238 let process_1 st entity =
239 if !G.ct >= 3 then pp_progress entity;
240 let st = if !G.summary then count_entity st entity else st in
241 if !export && !G.stage = 1 then export_entity st entity;
242 if !G.stage >= 2 then process_2 st (xlate_entity st entity) else st
244 let process_0 st entity =
246 if !G.stage = 0 then st else
249 let err ast = {st with ast = ast} in
250 let g ast e = process_1 {st with ast = ast} (CrgEntity e) in
251 AD.crg_of_aut err g st.kst st.ast e
253 let crr tst = {st with tst = tst} in
254 let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
255 TD.crg_of_txt crr d gen_text st.tst e
256 | NoEntity -> assert false
258 let st = if !G.summary then count_input st entity else st in
259 if !preprocess then process_input f st entity else f st entity
261 let process_nostreaming st lexbuf input =
263 let rec aux1 book = match entity_of_input lexbuf input with
264 | NoEntity -> List.rev book
265 | e -> aux1 (id e :: book)
267 let rec aux2 st = function
269 | entity :: tl -> aux2 (process_0 st entity) tl
273 let process_streaming st lexbuf input =
274 let rec aux st = match entity_of_input lexbuf input with
276 | e -> aux (process_0 st e)
280 (****************************************************************************)
282 let process st name =
283 let process = if !streaming then process_streaming else process_nostreaming in
284 let input = type_of_input name in
285 let ich = open_in name in
286 let lexbuf = Lexing.from_channel ich in
287 let st = process st lexbuf input in
292 let print_version () = L.warn level (G.version_string ^ "\n"); exit 0 in
293 let set_hierarchy s =
294 if H.set_graph s then () else
295 L.warn level (KP.sprintf "Unknown type hierarchy: %s" s)
297 let set_kernel = function
298 | "V3" -> G.kernel := G.V3
299 | "V0" -> G.kernel := G.V0
300 | s -> L.warn level (KP.sprintf "Unknown kernel version: %s" s)
303 if !G.trace = 0 && i > 0 then Y.gmtime G.version_string;
304 if !G.trace > 0 && i = 0 then Y.utime_stamp "at exit";
306 if i <= 1 then G.summary := false;
307 if i <= 1 then preprocess := false
310 if !G.trace >= 2 then G.summary := true
312 let set_preprocess () =
313 if !G.trace >= 2 then begin preprocess := true; G.summary := true end
315 let set_manager s = match KS.lowercase s with
316 | "v8" -> G.manager := G.Coq
317 | "ma2" -> G.manager := G.Matita
318 | "elpi1" -> G.manager := G.ELPI1
319 | "elpi2" -> G.manager := G.ELPI2
320 | s -> L.warn level (KP.sprintf "Unknown manager: %s" s)
322 let clear_options () =
323 export := false; preprocess := false;
325 G.clear (); H.clear (); O.clear_reductions ();
329 let process_file name =
330 if !G.trace >= 2 then L.warn 1 (KP.sprintf "Processing file: %s" name);
331 if !G.trace >= 2 then Y.utime_stamp "started";
332 let base_name = Filename.chop_extension (Filename.basename name) in
333 let cover = KF.concat !root base_name in
334 if !G.stage <= 1 then G.kernel := G.V4;
336 begin match !G.manager with
337 | G.Coq -> st := {!st with mst = Some (BA.open_out base_name)}
338 | G.Matita -> st := {!st with mst = Some (BG.open_out base_name)}
339 | G.ELPI1 -> st := {!st with mst = Some (BP.open_out_1 base_name)}
340 | G.ELPI2 -> st := {!st with mst = Some (BP.open_out_2 base_name)}
344 let sst, input = process (refresh_status !st) name in
345 st := begin match sst.mst with
347 | Some (_, close_out) -> close_out (); {sst with mst = None}
349 if !G.trace >= 2 then Y.utime_stamp "processed";
350 if !G.summary then begin
351 AO.print_counters C.start !st.ac;
352 if !preprocess then AO.print_process_counters C.start !st.pst;
353 if !G.stage >= 1 then print_counters !st G.V4;
354 if !G.stage >= 2 then print_counters !st !G.kernel;
355 if !G.stage >= 3 then O.print_reductions ()
359 if !G.trace >= 1 then Y.utime_stamp "at exit"
362 "Usage: helena [ -LPVXdgilnoqtux01 | -Ts <number> | -MO <dir> | -p <file> | -ahkmr <string> | -be <age> ]* [ <file> ]*\n\n" ^
363 "Trace levels: 0 just errors (default), 1 time stamps, 2 processed files, 3 processed objects,\n" ^
364 " 4 typing information, 5 conversion information, 6 reduction information,\n" ^
365 " 7 level disambiguation\n\n" ^
366 "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n\n" ^
367 "Supported manages: \"ma2\" (Grafite NG), \"v8\" (Gallina 8), \"elpi1\" \"elpi2\" (lambda-Prolog)\n"
369 let help_L = " [lexer] Show lexer debug information" in
370 let help_M = "<dir> [manager] Set location of output directory (manager) to <dir> (default: current directory)" in
371 let help_O = "<dir> [output] Set location of output directory (XML) to <dir> (default: current directory)" in
372 let help_P = " [parser] Show parser debug information" in
373 let help_T = "<number> [trace] Set trace level (see above)" in
374 let help_V = " [version] Show version information" in
375 let help_X = " Clear options" in
377 let help_a = "<string> [alpha] Set prefix of numeric identifiers (default: empty)" in
378 let help_b = "<age> [begin] Begin trace at this global constant (default: first)" in
379 let help_d = " [data] Show summary information (requires trace >= 2)" in
380 let help_e = "<age> [end] End trace at this global constant (default: last)" in
381 let help_g = " [global] Always expand global definitions (default: false)" in
382 let help_h = "<string> [hierarchy] Set type hierarchy (default: \"Z1\")" in
383 let help_i = " [indexes] Show local references by index" in
384 let help_k = "<string> [kernel] Set kernel version (default: \"V3\")" in
385 let help_l = " [layer] Disambiguate binders layer (Automath)" in
386 let help_m = "<string> [manager] Export kernel entities for this manager (see above, default: no manager)" in
387 let help_n = " [names] Show short constants (default: qualified constants)" in
388 let help_o = " [objects] Export kernel entities (XML)" in
389 let help_p = "<file> [preamble] Set preamble to this file (default: empty)" in
390 let help_q = " [quote] Disable quotation of identifiers (default: false)" in
391 let help_r = "<string> [root] Set initial segment of URI hierarchy (default: empty)" in
392 let help_s = "<number> [stage] Set translation stage (see above)" in
393 let help_t = " [type] Type check (default: validate)" in
394 let help_u = " [upsilon] Activate type comparison by sort inclusion (default: false)" in
395 let help_x = " [extended] Use extended applications (Automath)" in
396 let help_0 = " [zero] Preprocess source (Automath)" in
397 let help_1 = " [one] parse files with streaming policy" in
400 ("-L", Arg.Set G.debug_lexer, help_L);
401 ("-M", Arg.String ((:=) G.manager_dir), help_M);
402 ("-O", Arg.String ((:=) G.xdir), help_O);
403 ("-P", Arg.Set G.debug_parser, help_P);
404 ("-T", Arg.Int set_trace, help_T);
405 ("-V", Arg.Unit print_version, help_V);
406 ("-X", Arg.Unit clear_options, help_X);
407 ("-a", Arg.String ((:=) G.alpha), help_a);
408 ("-b", Arg.Int ((:=) G.first), help_b);
409 ("-d", Arg.Unit set_summary, help_d);
410 ("-e", Arg.Int ((:=) G.last), help_e);
411 ("-g", Arg.Set G.expand, help_g);
412 ("-h", Arg.String set_hierarchy, help_h);
413 ("-i", Arg.Set G.indexes, help_i);
414 ("-k", Arg.String set_kernel, help_k);
415 ("-l", Arg.Set G.cc, help_l);
416 ("-m", Arg.String set_manager, help_m);
417 ("-n", Arg.Set G.short, help_n);
418 ("-o", Arg.Set export, help_o);
419 ("-p", Arg.String ((:=) G.preamble), help_p);
420 ("-q", Arg.Set G.unquote, help_q);
421 ("-r", Arg.String ((:=) root), help_r);
422 ("-s", Arg.Int ((:=) G.stage), help_s);
423 ("-t", Arg.Clear version, help_t);
424 ("-u", Arg.Set G.si, help_u);
425 ("-x", Arg.Set G.extended, help_x);
426 ("-0", Arg.Unit set_preprocess, help_0);
427 ("-1", Arg.Set streaming, help_1);