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