]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/toplevel/top.ml
- we are moving from old (patched) management of sort inclusion
[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 F  = Filename
13 module P  = Printf
14
15 module U  = NUri
16 module C  = Cps
17 module L  = Log
18 module Y  = Time
19 module G  = Options
20 module H  = Hierarchy
21 module N  = Layer
22 module E  = Entity
23 module O  = Output
24 module DO = CrgOutput
25 module TD = TxtCrg
26 module AA = AutProcess
27 module AO = AutOutput
28 module AD = AutCrg
29 module XL = XmlLibrary
30 module XD = XmlCrg
31 module BD = BrgCrg
32 module BO = BrgOutput
33 module BR = BrgReduction
34 module BU = BrgUntrusted
35 module BG = BrgGrafite
36 module ZD = BagCrg
37 module ZO = BagOutput
38 module ZT = BagType
39 module ZU = BagUntrusted
40
41 type status = {
42    kst: N.status;
43    tst: TD.status;
44    pst: AA.status;
45    ast: AD.status;
46    ac : AO.counters;
47    dc : DO.counters;
48    bc : BO.counters;
49    zc : ZO.counters;
50    och: out_channel option;
51 }
52
53 let level = 0
54
55 let bag_error st s msg =
56    L.error st.kst ZO.specs (L.Warn s :: msg) 
57
58 let brg_error st s msg =
59    L.error st.kst BR.specs (L.Warn s :: msg)
60
61 let initial_status () = {
62    kst = N.initial_status ();
63    tst = TD.initial_status ();
64    pst = AA.initial_status ();
65    ast = AD.initial_status ();
66    ac  = AO.initial_counters;
67    dc  = DO.initial_counters;
68    bc  = BO.initial_counters;
69    zc  = ZO.initial_counters;
70    och = None;
71 }
72
73 let refresh_status st = {st with
74    kst = N.refresh_status st.kst;
75    tst = TD.refresh_status st.tst;
76    ast = AD.refresh_status st.ast;
77 }
78
79 (* kernel related ***********************************************************)
80
81 type kernel_entity = BrgEntity of Brg.entity
82                    | BagEntity of Bag.entity
83                    | CrgEntity of Crg.entity
84
85 let print_counters st = function
86    | G.Crg -> DO.print_counters C.start st.dc
87    | G.Brg -> BO.print_counters C.start st.bc
88    | G.Bag -> ZO.print_counters C.start st.zc
89
90 let xlate_entity st entity = match !G.kernel, entity with
91    | G.Brg, CrgEntity e -> 
92       let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
93    | G.Bag, CrgEntity e -> 
94       let f e = (BagEntity e) in E.xlate f (ZD.bag_of_crg st.kst) e
95    | _, entity          -> entity
96
97 let pp_progress e =
98    let f _ na u =
99       let s = U.string_of_uri u in
100       L.warn 2 (P.sprintf "[%u] <%s>" na.E.n_apix s)
101    in
102    match e with
103       | CrgEntity e -> E.common f e
104       | BrgEntity e -> E.common f e
105       | BagEntity e -> E.common f e      
106
107 let count_entity st = function
108    | BrgEntity e -> {st with bc = BO.count_entity C.start st.bc e}
109    | BagEntity e -> {st with zc = ZO.count_entity C.start st.zc e}
110    | CrgEntity e -> {st with dc = DO.count_entity C.start st.dc e}
111
112 let export_entity st = function
113    | CrgEntity e -> XL.export_entity (XD.export_term st.kst) e
114    | BrgEntity e -> XL.export_entity (BO.export_term st.kst) e
115    | BagEntity e -> XL.export_entity (ZO.export_term st.kst) e
116
117 let type_check st k =
118    let brg_err msg = brg_error st "Type Error" msg; failwith "Interrupted" in
119    let bag_err msg = bag_error st "Type Error" msg; failwith "Interrupted" in
120    let ok _ _ = st in
121    match k with
122       | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
123       | BagEntity entity -> ZU.type_check bag_err ok st.kst entity
124       | CrgEntity _      -> st
125
126 let validate st k =
127    let brg_err msg = brg_error st "Validation Error" msg; failwith "Interrupted" in
128    let ok _ = st in
129    match k with
130       | BrgEntity entity -> 
131          let st = BU.validate brg_err ok st.kst entity in
132          begin match st.och with
133             | None     -> st
134             | Some och -> 
135                if BG.output_entity st.kst och entity then st
136                else begin L.warn level "Matita exportation stopped"; {st with och = None} end
137          end
138       | BagEntity _      -> st
139       | CrgEntity _      -> st
140
141 (* extended lexer ***********************************************************)
142
143 type 'token lexer = {
144    parse : Lexing.lexbuf -> 'token;
145    mutable tokbuf: 'token option;
146    mutable unget : bool
147 }
148
149 let initial_lexer parse = {
150    parse = parse; tokbuf = None; unget = false
151 }
152
153 let token xl lexbuf = match xl.tokbuf with
154    | Some token when xl.unget ->   
155       xl.unget <- false; token
156    | _                        ->
157       let token = xl.parse lexbuf in
158       xl.tokbuf <- Some token; token
159
160 (* input related ************************************************************)
161
162 type input = Text | Automath
163
164 type input_entity = TxtEntity of Txt.command
165                   | AutEntity of Aut.command
166                   | NoEntity
167
168 let type_of_input name =
169    if F.check_suffix name ".hln" then Text 
170    else if F.check_suffix name ".aut" then 
171       let _ = H.set_sorts 0 ["Set"; "Prop"] in
172       assert (H.set_graph "Z2");
173       Automath
174    else begin
175       L.warn level (P.sprintf "Unknown file type: %s" name); exit 2
176    end
177
178 let txt_xl = initial_lexer TxtLexer.token 
179
180 let aut_xl = initial_lexer AutLexer.token 
181
182 let parbuf = ref [] (* parser buffer *)
183
184 let gen_text command = 
185    parbuf := TxtEntity command :: !parbuf
186
187 let entity_of_input lexbuf i = match i, !parbuf with
188    | Automath, _    -> 
189       begin match AutParser.entry (token aut_xl) lexbuf with
190          | Some e -> aut_xl.unget <- true; AutEntity e
191          | None   -> NoEntity
192       end     
193    | Text, []       -> 
194       begin match TxtParser.entry (token txt_xl) lexbuf with
195          | Some e -> txt_xl.unget <- true; TxtEntity e
196          | None   -> NoEntity
197       end
198    | Text, hd :: tl ->
199       parbuf := tl; hd
200
201 let process_input f st = function 
202    | AutEntity e     ->
203       let f pst e = f {st with pst = pst} (AutEntity e) in
204       AA.process_command f st.pst e
205    | xe              -> f st xe
206
207 let count_input st = function
208    | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
209    | xe          -> st
210
211 (****************************************************************************)
212
213 let version = ref true
214 let preprocess = ref false
215 let root = ref ""
216 let export = ref false
217 let st = ref (initial_status ())
218 let streaming = ref false (* parsing style (temporary) *)
219
220 let process_2 st entity =
221    let st = if !G.summary then count_entity st entity else st in
222    if !export then export_entity st entity;
223    if !G.stage >= 3 then 
224       let f = if !version then validate else type_check in 
225       f st entity
226    else st
227
228 let process_1 st entity = 
229    if !G.trace >= 3 then pp_progress entity;
230    let st = if !G.summary then count_entity st entity else st in
231    if !export && !G.stage = 1 then export_entity st entity;
232    if !G.stage >= 2 then process_2 st (xlate_entity st entity) else st 
233
234 let process_0 st entity = 
235    let f st entity =
236       if !G.stage = 0 then st else
237       match entity with
238          | AutEntity e -> 
239             let err ast = {st with ast = ast} in
240             let g ast e = process_1 {st with ast = ast} (CrgEntity e) in
241             AD.crg_of_aut err g st.kst st.ast e
242          | TxtEntity e -> 
243             let crr tst = {st with tst = tst} in
244             let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
245             TD.crg_of_txt crr d gen_text st.tst e
246          | NoEntity    -> assert false
247    in
248    let st = if !G.summary then count_input st entity else st in 
249    if !preprocess then process_input f st entity else f st entity
250
251 let process_nostreaming st lexbuf input =
252    let id x = x in
253    let rec aux1 book = match entity_of_input lexbuf input with
254       | NoEntity -> List.rev book
255       | e        -> aux1 (id e :: book)   
256    in
257    let rec aux2 st = function
258       | []           -> st
259       | entity :: tl -> aux2 (process_0 st entity) tl
260    in
261    aux2 st (aux1 [])
262
263 let process_streaming st lexbuf input =
264    let rec aux st = match entity_of_input lexbuf input with
265       | NoEntity -> st
266       | e        -> aux (process_0 st e)
267    in
268    aux st
269
270 (****************************************************************************)
271
272 let process st name =
273    let process = if !streaming then process_streaming else process_nostreaming in
274    let input = type_of_input name in
275    let ich = open_in name in
276    let lexbuf = Lexing.from_channel ich in 
277    let st = process st lexbuf input in
278    close_in ich; 
279 (*   if !G.cc then XL.export_csys st.kst.S.cc; *)
280    st, input
281
282 let main = 
283    let print_version () = L.warn level (G.version_string ^ "\n"); exit 0 in
284    let set_hierarchy s = 
285       if H.set_graph s then () else 
286          L.warn level (P.sprintf "Unknown type hierarchy: %s" s)
287    in
288    let set_kernel = function
289       | "brg" -> G.kernel := G.Brg
290       | "bag" -> G.kernel := G.Bag
291       | s     -> L.warn level (P.sprintf "Unknown kernel version: %s" s)
292    in
293    let set_trace i = 
294       if !G.trace = 0 && i > 0 then Y.gmtime G.version_string;
295       if !G.trace > 0 && i = 0 then Y.utime_stamp "at exit";
296       G.trace := i;
297       if i <= 1 then G.summary := false;
298       if i <= 1 then preprocess := false
299    in
300    let set_summary () = 
301       if !G.trace >= 2 then G.summary := true
302    in 
303    let set_preprocess () = 
304       if !G.trace >= 2 then begin preprocess := true; G.summary := true end 
305    in 
306    let clear_options () =
307       export := false; preprocess := false;
308       root := "";
309       st := initial_status ();
310       G.clear (); H.clear (); O.clear_reductions ();
311       streaming := false;
312       version := true
313    in
314    let process_file name =
315       if !G.trace >= 2 then L.warn 1 (P.sprintf "Processing file: %s" name);
316       if !G.trace >= 2 then Y.utime_stamp "started";
317       let base_name = Filename.chop_extension (Filename.basename name) in
318       let cover = F.concat !root base_name in
319       if !G.stage <= 1 then G.kernel := G.Crg;
320       G.cover := cover;
321       if !G.ma_preamble <> "" then st := {!st with och = Some (BG.open_out base_name)};
322       let sst, input = process (refresh_status !st) name in
323       st := begin match sst.och with 
324          | None     -> sst
325          | Some och -> BG.close_out och; {sst with och = None}
326       end;
327       if !G.trace >= 2 then Y.utime_stamp "processed";
328       if !G.summary then begin
329          AO.print_counters C.start !st.ac;
330          if !preprocess then AO.print_process_counters C.start !st.pst;
331          if !G.stage >= 1 then print_counters !st G.Crg;
332          if !G.stage >= 2 then print_counters !st !G.kernel;
333          if !G.stage >= 3 then O.print_reductions ()
334       end
335    in
336    let exit () =
337       if !G.trace >= 1 then Y.utime_stamp "at exit"
338    in
339    let help = 
340       "Usage: helena [ -LPVXdgilopqtx1 | -Ts <number> | -MO <dir> | -m <file> | -ahkr <string> ]* [ <file> ]*\n\n" ^
341       "Trace levels: 0 just errors (default), 1 time stamps, 2 processed files, 3 typing information,\n" ^
342       "              4 conversion information, 5 reduction information, 6 level disambiguation\n\n" ^
343       "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
344    in
345    let help_L = " show lexer debug information" in 
346    let help_M = "<dir>  set location of output directory (Grafite) to <dir> (default: current directory)" in
347    let help_O = "<dir>  set location of output directory (XML) to <dir> (default: current directory)" in
348    let help_P = " show parser debug information" in 
349    let help_T = "<number>  set trace level (see above)" in
350    let help_V = " show version information" in
351    let help_X = " clear options" in
352    
353    let help_a = "<string>  set prefix of numeric identifiers (default: empty)" in
354    let help_d = " show summary information (requires trace >= 2)" in
355    let help_g = " always expand global definitions" in
356    let help_h = "<string>  set type hierarchy (default: \"Z1\")" in
357    let help_i = " show local references by index" in
358    let help_k = "<string>  set kernel version (default: \"brg\")" in
359    let help_l = " disambiguate binders level (Automath)" in
360    let help_m = "<file>  export kernel entities (Grafite) setting location of preamble to <file> (default: empty)" in   
361    let help_o = " activate sort inclusion (default: false)" in
362    let help_p = " preprocess source (Automath)" in
363    let help_q = " disable quotation of identifiers" in
364    let help_r = "<string>  set initial segment of URI hierarchy (default: empty)" in
365    let help_s = "<number>  set translation stage (see above)" in
366    let help_t = " type check [version 1]" in
367    let help_x = " export kernel entities (XML)" in
368    
369    let help_1 = " parse files with streaming policy" in
370    at_exit exit;
371    Arg.parse [
372       ("-L", Arg.Set G.debug_lexer, help_L);
373       ("-M", Arg.String ((:=) G.ma_dir), help_M);
374       ("-O", Arg.String ((:=) G.xdir), help_O);
375       ("-P", Arg.Set G.debug_parser, help_P);
376       ("-T", Arg.Int set_trace, help_T);
377       ("-V", Arg.Unit print_version, help_V);
378       ("-X", Arg.Unit clear_options, help_X);
379       ("-a", Arg.String ((:=) G.alpha), help_a);
380       ("-d", Arg.Unit set_summary, help_d);
381       ("-g", Arg.Set G.expand, help_g);
382       ("-h", Arg.String set_hierarchy, help_h);
383       ("-i", Arg.Set G.indexes, help_i);
384       ("-k", Arg.String set_kernel, help_k);
385       ("-l", Arg.Set G.cc, help_l);
386       ("-m", Arg.String ((:=) G.ma_preamble), help_m);      
387       ("-o", Arg.Set G.si, help_o);
388       ("-p", Arg.Unit set_preprocess, help_p);
389       ("-q", Arg.Set G.unquote, help_q);      
390       ("-r", Arg.String ((:=) root), help_r);
391       ("-s", Arg.Int ((:=) G.stage), help_s);
392       ("-t", Arg.Clear version, help_t);      
393       ("-x", Arg.Set export, help_x);
394       ("-1", Arg.Set streaming, help_1);      
395    ] process_file help;