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