]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/toplevel/helena.ml
63a8c062116a2581c2cbf72402e172ab89be66c2
[helm.git] / helm / software / helena / src / toplevel / helena.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 module KF = Filename
13 module KL = List
14 module KP = Printf
15 module KS = String
16 module KT = String
17
18 module U  = NUri
19 module C  = Cps
20 module L  = Log
21 module Y  = Time
22 module P  = Marks
23 module G  = Options
24 module H  = Hierarchy
25 module N  = Layer
26 module E  = Entity
27 module O  = Output
28 module DO = CrgOutput
29 module TD = TxtCrg
30 module AA = AutProcess
31 module AO = AutOutput
32 module AD = AutCrg
33 module XL = XmlLibrary
34 module XD = XmlCrg
35 module B  = Brg
36 module BD = BrgCrg
37 module BO = BrgOutput
38 module BR = BrgReduction
39 module BU = BrgUntrusted
40 module BM = BrgMatita
41 module BQ = BrgCoq
42 module BH = BrgHelena
43 module BP = BrgPTS
44 module BY = BrgLYP
45 module ZD = BagCrg
46 module ZO = BagOutput
47 module ZT = BagType
48 module ZU = BagUntrusted
49
50 type status = {
51    kst: N.status;
52    tst: TD.status;
53    pst: AA.status;
54    ast: AD.status;
55    ac : AO.counters;
56    dc : DO.counters;
57    bc : BO.counters;
58    zc : ZO.counters;
59    mst: B.manager option;
60 }
61
62 let level = 0
63
64 let bag_error st s msg =
65    L.error st.kst ZO.specs (L.Warn s :: msg) 
66
67 let brg_error st s msg =
68    L.error st.kst BR.specs (L.Warn s :: msg)
69
70 let initial_status () = {
71    kst = N.initial_status ();
72    tst = TD.initial_status ();
73    pst = AA.initial_status ();
74    ast = AD.initial_status ();
75    ac  = AO.initial_counters;
76    dc  = DO.initial_counters;
77    bc  = BO.initial_counters;
78    zc  = ZO.initial_counters;
79    mst = None;
80 }
81
82 let refresh_status st = {st with
83    kst = N.refresh_status st.kst;
84    tst = TD.refresh_status st.tst;
85    ast = AD.refresh_status st.ast;
86 }
87
88 (* kernel related ***********************************************************)
89
90 type kernel_entity = BrgEntity of Brg.entity
91                    | BagEntity of Bag.entity
92                    | CrgEntity of Crg.entity
93
94 IFDEF SUMMARY THEN
95
96 let print_counters st = function
97    | G.V4 -> DO.print_counters C.start st.dc
98    | G.V3 -> BO.print_counters C.start st.bc
99    | G.V0 -> ZO.print_counters C.start st.zc
100
101 END
102
103 IFDEF TRACE THEN
104
105 let pp_progress e =
106    let f _ na u =
107       let s = U.string_of_uri u in
108       L.warn 2 (KP.sprintf "[%u] <%s>" na.E.n_apix s);
109    in
110    Y.utime_stamp "intermediate";
111    match e with
112       | CrgEntity e -> E.common f e
113       | BrgEntity e -> E.common f e
114       | BagEntity e -> E.common f e      
115
116 END
117
118 IFDEF SUMMARY THEN
119
120 let count_entity st = function
121    | BrgEntity e -> {st with bc = BO.count_entity C.start st.bc e}
122    | BagEntity e -> {st with zc = ZO.count_entity C.start st.zc e}
123    | CrgEntity e -> {st with dc = DO.count_entity C.start st.dc e}
124
125 END
126
127 IFDEF OBJECTS THEN
128
129 let export_entity st = function
130    | CrgEntity e -> XL.export_entity (XD.export_term st.kst) e
131    | BrgEntity e -> XL.export_entity (BO.export_term st.kst) e
132    | BagEntity e -> XL.export_entity (ZO.export_term st.kst) e
133
134 END
135
136 IFDEF TYPE THEN
137
138 let type_check st k =
139    let brg_err msg = brg_error st "Type Error" msg; failwith "Interrupted" in
140    let bag_err msg = bag_error st "Type Error" msg; failwith "Interrupted" in
141    let ok _ _ = st in
142    match k with
143       | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
144       | BagEntity entity -> ZU.type_check bag_err ok st.kst entity
145       | CrgEntity _      -> st
146
147 END
148
149 IFDEF MANAGER THEN
150
151 let manager st output_entity = function
152    | BrgEntity entity -> 
153       if output_entity st.kst entity then st else
154       begin L.warn level "manager exportation stopped"; {st with mst = None} end
155    | BagEntity _      -> st
156    | CrgEntity _      -> st
157
158 END
159
160 let xlate_entity st entity = match !G.kernel, entity with
161    | G.V3, CrgEntity e -> 
162       let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
163    | G.V0, CrgEntity e -> 
164       let f e = (BagEntity e) in E.xlate f (ZD.bag_of_crg st.kst) e
165    | _, entity         -> entity
166
167 let validate st k =
168    let brg_err msg = brg_error st "Validation Error" msg; failwith "Interrupted" in
169    let ok _ = st in
170    match k with
171       | BrgEntity entity -> BU.validate brg_err ok st.kst entity
172       | BagEntity _      -> st
173       | CrgEntity _      -> st
174
175 (* extended lexer ***********************************************************)
176
177 type 'token lexer = {
178    parse : Lexing.lexbuf -> 'token;
179    mutable tokbuf: 'token option;
180    mutable unget : bool
181 }
182
183 let initial_lexer parse = {
184    parse = parse; tokbuf = None; unget = false
185 }
186
187 let token xl lexbuf = match xl.tokbuf with
188    | Some token when xl.unget ->   
189       xl.unget <- false; token
190    | _                        ->
191       let token = xl.parse lexbuf in
192       xl.tokbuf <- Some token; token
193
194 (* input related ************************************************************)
195
196 type input = Text | Automath
197
198 type input_entity = TxtEntity of Txt.command
199                   | AutEntity of Aut.command
200                   | NoEntity
201
202 let type_of_input name =
203    if KF.check_suffix name ".hln" then Text 
204    else if KF.check_suffix name ".aut" then 
205       let _ = H.set_sorts 0 ["Set"; "Prop"] in
206       assert (H.set_graph "Z2");
207       Automath
208    else begin
209       L.warn level (KP.sprintf "Unknown file type: %s" name); exit 2
210    end
211
212 let txt_xl = initial_lexer TxtLexer.token 
213
214 let aut_xl = initial_lexer AutLexer.token 
215
216 let parbuf = ref [] (* parser buffer *)
217
218 let gen_text command = 
219    parbuf := TxtEntity command :: !parbuf
220
221 let entity_of_input lexbuf i = match i, !parbuf with
222    | Automath, _    -> 
223       begin match AutParser.entry (token aut_xl) lexbuf with
224          | Some e -> aut_xl.unget <- true; AutEntity e
225          | None   -> NoEntity
226       end     
227    | Text, []       -> 
228       begin match TxtParser.entry (token txt_xl) lexbuf with
229          | Some e -> txt_xl.unget <- true; TxtEntity e
230          | None   -> NoEntity
231       end
232    | Text, hd :: tl ->
233       parbuf := tl; hd
234
235 IFDEF PREPROCESS THEN
236
237 let process_input f st = function 
238    | AutEntity e     ->
239       let f pst e = f {st with pst = pst} (AutEntity e) in
240       AA.process_command f st.pst e
241    | xe              -> f st xe
242
243 END
244
245 IFDEF SUMMARY THEN
246
247 let count_input st = function
248    | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
249    | xe          -> st
250
251 END
252
253 (****************************************************************************)
254
255 let st = ref (initial_status ())
256 let streaming = ref false (* parsing style (temporary) *)
257
258 let process_2 st entity =
259    let st =
260 IFDEF SUMMARY THEN
261       if !G.summary then count_entity st entity else st
262 ELSE 
263       st
264 END
265    in
266    let st =
267 IFDEF STAGE THEN
268       if !G.stage >= 3 then
269 IFDEF TYPE THEN 
270          let f = if !G.validate then validate else type_check in f st entity
271 ELSE
272          validate st entity
273 END
274       else st
275 ELSE
276 IFDEF TYPE THEN 
277       let f = if !G.validate then validate else type_check in f st entity
278 ELSE
279       validate st entity
280 END
281 END
282    in
283 IFDEF OBJECTS THEN
284    if !G.export then export_entity st entity
285 ELSE () END;
286 IFDEF MANAGER THEN
287    match st.mst with
288      | None                    -> st
289      | Some (export_entity, _) -> manager st export_entity entity
290 ELSE
291      st
292 END
293
294 let process_1 st entity =
295 IFDEF TRACE THEN
296    if !G.ct >= 3 then pp_progress entity;
297 ELSE () END;
298    let st =
299 IFDEF SUMMARY THEN
300       if !G.summary then count_entity st entity else st
301 ELSE
302       st
303 END
304    in
305 IFDEF STAGE THEN
306 IFDEF OBJECTS THEN
307    if !G.export && !G.stage = 1 then export_entity st entity
308 ELSE () END;
309    if !G.stage >= 2 then process_2 st (xlate_entity st entity) else st 
310 ELSE
311    process_2 st (xlate_entity st entity)
312 END
313
314 let process_0 st entity = 
315    let f st entity =
316 IFDEF STAGE THEN
317       if !G.stage = 0 then st else
318       match entity with
319          | AutEntity e -> 
320             let err ast = {st with ast = ast} in
321             let g ast e = process_1 {st with ast = ast} (CrgEntity e) in
322             AD.crg_of_aut err g st.kst st.ast e
323          | TxtEntity e -> 
324             let crr tst = {st with tst = tst} in
325             let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
326             TD.crg_of_txt crr d gen_text st.tst e
327          | NoEntity    -> assert false
328 ELSE
329       match entity with
330          | AutEntity e -> 
331             let err ast = {st with ast = ast} in
332             let g ast e = process_1 {st with ast = ast} (CrgEntity e) in
333             AD.crg_of_aut err g st.kst st.ast e
334          | TxtEntity e -> 
335             let crr tst = {st with tst = tst} in
336             let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
337             TD.crg_of_txt crr d gen_text st.tst e
338          | NoEntity    -> assert false
339 END
340    in
341    let st =
342 IFDEF SUMMARY THEN
343       if !G.summary then count_input st entity else st
344 ELSE
345       st
346 END
347    in 
348 IFDEF PREPROCESS THEN
349    if !G.preprocess then process_input f st entity else f st entity
350 ELSE
351    f st entity
352 END
353
354 let process_nostreaming st lexbuf input =
355    let id x = x in
356    let rec aux1 book = match entity_of_input lexbuf input with
357       | NoEntity -> List.rev book
358       | e        -> aux1 (id e :: book)   
359    in
360    let rec aux2 st = function
361       | []           -> st
362       | entity :: tl -> aux2 (process_0 st entity) tl
363    in
364    aux2 st (aux1 [])
365
366 let process_streaming st lexbuf input =
367    let rec aux st = match entity_of_input lexbuf input with
368       | NoEntity -> st
369       | e        -> aux (process_0 st e)
370    in
371    aux st
372
373 (****************************************************************************)
374
375 IFDEF PREPROCESS THEN
376
377 let set_preprocess () = 
378    if !G.trace >= 2 then begin
379       G.preprocess := true;
380 IFDEF SUMMARY THEN
381       G.summary := true
382 ELSE () END
383    end
384
385 END
386
387 IFDEF MANAGER THEN
388
389 let set_manager s = match KS.lowercase s with
390    | "v8"  -> G.manager := G.Coq
391    | "ma2" -> G.manager := G.Matita
392    | "lp1" -> G.manager := G.LP1
393    | "lp2" -> G.manager := G.LP2
394    | "tj2" -> G.manager := G.TJ2
395    | "tj3" -> G.manager := G.TJ3
396    | "pts" -> G.manager := G.PTS
397    | "lyp" -> G.manager := G.LYP
398    | s     -> L.warn level (KP.sprintf "Unknown manager: %s" s)
399
400 END
401
402 IFDEF SUMMARY THEN
403
404 let set_summary () =
405    if !G.trace >= 2 then G.summary := true
406
407 END
408
409 let set_trace i = 
410    if !G.trace = 0 && i > 0 then Y.gmtime (G.version_string false);
411    if !G.trace > 0 && i = 0 then Y.utime_stamp "at exit";
412    G.trace := i;
413 IFDEF SUMMARY THEN
414    if i <= 1 then G.summary := false
415 ELSE () END;
416 IFDEF PREPROCESS THEN
417    if i <= 1 then G.preprocess := false
418 ELSE () END
419
420 let custom_exit () =
421    if !G.trace >= 1 then Y.utime_stamp "at exit"
422
423 let process st name =
424    let process = if !streaming then process_streaming else process_nostreaming in
425    let input = type_of_input name in
426    let ich = open_in name in
427    let lexbuf = Lexing.from_channel ich in 
428    let st = process st lexbuf input in
429    close_in ich;
430    st, input
431
432 let main = 
433    let print_version () =
434       let features = [
435 (IFDEF LEXER THEN "LEXER" ELSE "" END);
436 (IFDEF PARSER THEN "PARSER" ELSE "" END);
437 (IFDEF TRACE THEN "TRACE" ELSE "" END);
438 (IFDEF SUMMARY THEN "SUMMARY" ELSE "" END);
439 (IFDEF EXPAND THEN "EXPAND" ELSE "" END);
440 (IFDEF MANAGER THEN "MANAGER" ELSE "" END);
441 (IFDEF OBJECTS THEN "OBJECTS" ELSE "" END);
442 (IFDEF PREPROCESS THEN "PREPROCESS" ELSE "" END);
443 (IFDEF QUOTE THEN "QUOTE" ELSE "" END);
444 (IFDEF STAGE THEN "STAGE" ELSE "" END);
445 (IFDEF TYPE THEN "TYPE" ELSE "" END);
446 (IFDEF PROFV THEN "PROFV" ELSE "" END);
447       ] in
448       let map s = s <> "" in
449       let features_string = KT.concat " " (KL.filter map features) in
450       L.warn level (KP.sprintf "%s [%s]" (G.version_string true) features_string);
451       exit 0
452    in
453    let set_hierarchy s = 
454       if H.set_graph s then () else 
455          L.warn level (KP.sprintf "Unknown type hierarchy: %s" s)
456    in
457    let set_kernel = function
458       | "V3" -> G.kernel := G.V3
459       | "V0" -> G.kernel := G.V0
460       | s    -> L.warn level (KP.sprintf "Unknown kernel version: %s" s)
461    in
462    let clear_options () =
463       G.clear (); H.clear ();
464 IFDEF SUMMARY THEN
465       O.clear_reductions ()
466 ELSE () END;
467       streaming := false;
468    in
469    let undefined opt () =
470       L.warn level (KP.sprintf "%s was compiled without the support for option %s" (G.version_string true) opt);
471       exit 0
472    in
473    let arg_undefined opt = Arg.Unit (undefined opt) in
474    let process_file name =
475       if !G.trace >= 2 then begin
476          L.warn 1 (KP.sprintf "Processing file: %s" name);
477          Y.utime_stamp "started"
478       end;
479       let base_name = Filename.chop_extension (Filename.basename name) in
480       let cover = KF.concat !G.root base_name in
481       G.cover := cover;
482 IFDEF STAGE THEN
483       if !G.stage <= 1 then G.kernel := G.V4;
484 ELSE () END;
485 IFDEF MANAGER THEN
486       begin match !G.manager with
487          | G.Coq    -> st := {!st with mst = Some (BQ.open_out base_name)}
488          | G.Matita -> st := {!st with mst = Some (BM.open_out base_name)}
489          | G.LP1    -> st := {!st with mst = Some (BH.open_out_lp1 base_name)}
490          | G.LP2    -> st := {!st with mst = Some (BH.open_out_lp2 base_name)}
491          | G.TJ2    -> st := {!st with mst = Some (BH.open_out_tj2 base_name)}
492          | G.TJ3    -> st := {!st with mst = Some (BH.open_out_tj3 base_name)}
493          | G.PTS    -> st := {!st with mst = Some (BP.open_out_pts2 base_name)}
494          | G.LYP    -> st := {!st with mst = Some (BY.open_out_lyp2 base_name)}
495          | G.Quiet  -> ()
496       end
497 ELSE () END;
498       P.clear_marks ();
499       let sst, input = process (refresh_status !st) name in
500       st := begin match sst.mst with 
501          | None                -> sst
502          | Some (_, close_out) -> close_out (); {sst with mst = None}
503       end;
504       if !G.trace >= 2 then Y.utime_stamp "processed";
505 IFDEF SUMMARY THEN
506       if !G.summary then begin
507          AO.print_counters C.start !st.ac;
508 IFDEF PREPROCESS THEN
509          if !G.preprocess then AO.print_process_counters C.start !st.pst
510 ELSE () END;
511 IFDEF STAGE THEN
512          if !G.stage >= 1 then print_counters !st G.V4;
513          if !G.stage >= 2 then print_counters !st !G.kernel;
514          if !G.stage >= 3 then O.print_reductions ()
515 ELSE
516          print_counters !st G.V4;
517          print_counters !st !G.kernel;
518          O.print_reductions ()
519 END
520       end
521 ELSE () END
522    in
523    let help = 
524       "Usage: helena [ -LPVXdgilnoqtuxy01 | -Ts <number> | -MO <dir> | -p <file> | -ahkmr <string> | -be <age> ]* [ <file> ]*\n\n" ^
525       "Trace levels: 0 just errors (default), 1 time stamps, 2 processed files, 3 processed objects,\n" ^
526       "              4 typing information, 5 conversion information, 6 reduction information,\n" ^
527       "              7 level disambiguation\n\n" ^
528       "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n\n" ^
529       "Supported manages: \"ma2\" (Grafite NG), \"v8\" (Gallina 8), \"lp1\" \"lp2\" \"tj2\" \"tj3\" \"pts\" \"lyp\" (lambda-Prolog)\n" 
530    in
531    let help_L = "         [lexer]     Show lexer debug information" in 
532    let help_M = "<dir>    [manager]   Set location of output directory (manager) to <dir> (default: current directory)" in
533    let help_O = "<dir>    [output]    Set location of output directory (XML) to <dir> (default: current directory)" in
534    let help_P = "         [parser]    Show parser debug information" in 
535    let help_T = "<number> [trace]     Set trace level (see above)" in
536    let help_V = "         [version]   Show version information" in
537    let help_X = "                     Clear options" in
538    
539    let help_a = "<string> [alpha]     Set prefix of numeric identifiers (default: empty)" in
540    let help_b = "<age>    [begin]     Begin trace at this global constant (default: first)" in
541    let help_c = "         [cast]      Anticipate cast in global definitions (Automath) (default: posticipate)" in
542    let help_d = "         [data]      Show summary information (requires trace >= 2)" in
543    let help_e = "<age>    [end]       End trace at this global constant (default: last)" in
544    let help_g = "         [global]    Disable age-driven expansion of global definitions (default: enable)" in
545    let help_h = "<string> [hierarchy] Set type hierarchy (default: \"Z1\")" in
546    let help_i = "         [indexes]   Show local references by index" in
547    let help_k = "<string> [kernel]    Set kernel version (default: \"V3\")" in
548    let help_l = "         [layer]     Disambiguate binders layer (Automath)" in
549    let help_m = "<string> [manager]   Export kernel entities for this manager (see above, default: no manager)" in
550    let help_n = "         [names]     Show short constants (default: qualified constants)" in
551    let help_o = "         [objects]   Export kernel entities (XML)" in
552    let help_p = "<file>   [preamble]  Set preamble to this file (default: empty)" in
553    let help_q = "         [quote]     Quote identifiers (default: disable)" in
554    let help_r = "<string> [root]      Set initial segment of URI hierarchy (default: empty)" in
555    let help_s = "<number> [stage]     Set translation stage (see above)" in
556    let help_t = "         [type]      Type check (default: validate)" in
557    let help_u = "         [upsilon]   Activate type comparison by sort inclusion (default: deactivate)" in
558    let help_x = "         [extended]  Use extended applications (Automath)" in
559    let help_y = "         [infinity]  Use ∞-abstractions in contexts" in
560    let help_0 = "         [zero]      Preprocess source (Automath)" in
561    let help_1 = "         [one]       parse files with streaming policy" in
562    at_exit custom_exit;
563    Arg.parse [
564       ("-L", (IFDEF LEXER THEN Arg.Set G.debug_lexer ELSE arg_undefined "-L" END), help_L);
565       ("-M", (IFDEF MANAGER THEN Arg.String ((:=) G.manager_dir) ELSE arg_undefined "-M" END), help_M);
566       ("-O", (IFDEF OBJECTS THEN Arg.String ((:=) G.xdir) ELSE arg_undefined "-O" END), help_O);
567       ("-P", (IFDEF PARSER THEN Arg.Set G.debug_parser ELSE arg_undefined "-P" END), help_P);
568       ("-T", Arg.Int set_trace, help_T);
569       ("-V", Arg.Unit print_version, help_V);
570       ("-X", Arg.Unit clear_options, help_X);
571       ("-a", Arg.String ((:=) G.alpha), help_a);
572       ("-b", Arg.Int ((:=) G.first), help_b);
573       ("-c", Arg.Set G.cast, help_c);
574       ("-d", (IFDEF SUMMARY THEN Arg.Unit set_summary ELSE arg_undefined "-d" END), help_d);
575       ("-e", Arg.Int ((:=) G.last), help_e);
576       ("-g", (IFDEF EXPAND THEN Arg.Set G.expand ELSE arg_undefined "-g" END), help_g);
577       ("-h", Arg.String set_hierarchy, help_h);
578       ("-i", Arg.Set G.indexes, help_i);
579       ("-k", Arg.String set_kernel, help_k);
580       ("-l", Arg.Set G.cc, help_l);
581       ("-m", (IFDEF MANAGER THEN Arg.String set_manager ELSE arg_undefined "-m" END), help_m);      
582       ("-n", Arg.Set G.short, help_n);
583       ("-o", (IFDEF OBJECTS THEN Arg.Set G.export ELSE arg_undefined "-o" END), help_o);
584       ("-p", (IFDEF MANAGER THEN Arg.String ((:=) G.preamble) ELSE arg_undefined "-p" END), help_p);
585       ("-q", (IFDEF QUOTE THEN Arg.Set G.quote ELSE arg_undefined "-q" END), help_q);
586       ("-r", Arg.String ((:=) G.root), help_r);
587       ("-s", (IFDEF STAGE THEN Arg.Int ((:=) G.stage) ELSE arg_undefined "-s" END), help_s);
588       ("-t", (IFDEF TYPE THEN Arg.Clear G.validate ELSE arg_undefined "-t" END), help_t);
589       ("-u", Arg.Set G.si, help_u);
590       ("-x", Arg.Clear G.restricted, help_x);
591       ("-y", Arg.Set G.infinity, help_y);
592       ("-0", (IFDEF PREPROCESS THEN Arg.Unit set_preprocess ELSE arg_undefined "-0" END), help_0);
593       ("-1", Arg.Set streaming, help_1);      
594    ] process_file help;
595 IFDEF LEXER AND MANAGER AND OBJECTS AND PARSER AND SUMMARY AND EXPAND AND QUOTE AND STAGE AND TYPE AND PREPROCESS THEN
596    if false then ignore (arg_undefined "") (* invocation of arg_undefined to make the compiler happy *)
597 ELSE () END