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