]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
e9ae20eeb8c00067fc7e1aee83ceaf75be4b6d17
[helm.git] / helm / software / lambda-delta / 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 T    = Time
18 module H    = Hierarchy
19 module O    = Output
20 module Y    = Entity
21 module X    = Library
22 module AL   = AutLexer
23 module AP   = AutProcess
24 module AO   = AutOutput
25 module DT   = CrgTxt
26 module DA   = CrgAut
27 module MA   = MetaAut
28 module MO   = MetaOutput
29 module ML   = MetaLibrary
30 module DO   = CrgOutput
31 module DBrg = CrgBrg
32 module MBrg = MetaBrg
33 module BrgO = BrgOutput
34 module BrgR = BrgReduction
35 module BrgU = BrgUntrusted
36 module MBag = MetaBag
37 module BagO = BagOutput
38 module BagT = BagType
39 module BagU = BagUntrusted
40
41 type status = {
42    ast : AP.status;
43    dst : DA.status;
44    mst : MA.status;
45    tst : DT.status;
46    ac  : AO.counters;
47    mc  : MO.counters;
48    brgc: BrgO.counters;
49    bagc: BagO.counters;
50    kst : Y.status
51 }
52
53 let flush_all () = L.flush 0; L.flush_err ()
54
55 let bag_error s msg =
56    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
57
58 let brg_error s msg =
59    L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
60
61 let initial_status mk_uri cover g expand si = {
62    ac   = AO.initial_counters;
63    mc   = MO.initial_counters;
64    brgc = BrgO.initial_counters;
65    bagc = BagO.initial_counters;
66    mst  = MA.initial_status ~cover ();
67    dst  = DA.initial_status (mk_uri si cover);
68    tst  = DT.initial_status (mk_uri si cover);
69    ast  = AP.initial_status;
70    kst  = Y.initial_status g expand si
71 }
72
73 (* kernel related ***********************************************************)
74
75 type kernel = Brg | Bag
76
77 type kernel_entity = BrgEntity  of Brg.entity
78                    | BagEntity  of Bag.entity
79                    | CrgEntity  of Crg.entity
80                    | MetaEntity of Meta.entity
81
82 let kernel = ref Brg
83
84 let print_counters st = match !kernel with
85    | Brg -> BrgO.print_counters C.start st.brgc
86    | Bag -> BagO.print_counters C.start st.bagc
87
88 let xlate_entity entity = match !kernel, entity with
89    | Brg, CrgEntity e  -> 
90       let f e = (BrgEntity e) in Y.xlate f DBrg.brg_of_crg e
91    | Brg, MetaEntity e -> 
92       let f e = (BrgEntity e) in Y.xlate f MBrg.brg_of_meta e
93    | Bag, MetaEntity e -> 
94       let f e = (BagEntity e) in Y.xlate f MBag.bag_of_meta e  
95    | _, entity         -> entity
96
97 let pp_progress e =
98    let f a u =
99       let s = U.string_of_uri u in
100       let err () = L.warn (P.sprintf "%s" s) in
101       let f i = L.warn (P.sprintf "[%u] %s" i s) in
102       Y.mark err f a
103    in
104    match e with
105       | CrgEntity e -> Y.common f e
106       | BrgEntity e -> Y.common f e
107       | BagEntity e -> Y.common f e      
108       | MetaEntity e -> Y.common f e
109
110 let count_entity st = function
111    | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e} 
112    | BrgEntity e  -> {st with brgc = BrgO.count_entity C.start st.brgc e}
113    | BagEntity e  -> {st with bagc = BagO.count_entity C.start st.bagc e}
114    | _            -> st
115
116 let export_entity si g moch = function
117    | CrgEntity e  -> X.export_entity DO.export_term si g e
118    | BrgEntity e  -> X.export_entity BrgO.export_term si g e
119    | MetaEntity e ->
120       begin match moch with
121          | None     -> ()
122          | Some och -> ML.write_entity C.start och e
123       end
124    | BagEntity _  -> ()
125
126 let type_check st k =
127    let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
128    let ok _ _ = st in
129    match k with
130       | BrgEntity entity -> BrgU.type_check brg_err ok st.kst entity
131       | BagEntity entity -> BagU.type_check ok st.kst entity
132       | CrgEntity _
133       | MetaEntity _     -> st
134
135 (* extended lexer ***********************************************************)
136
137 type 'token lexer = {
138    parse : Lexing.lexbuf -> 'token;
139    mutable tokbuf: 'token option;
140    mutable unget : bool
141 }
142
143 let initial_lexer parse = {
144    parse = parse; tokbuf = None; unget = false
145 }
146
147 let token xl lexbuf = match xl.tokbuf with
148    | Some token when xl.unget ->   
149       xl.unget <- false; token
150    | _                        ->
151       let token = xl.parse lexbuf in
152       xl.tokbuf <- Some token; token
153
154 (* input related ************************************************************)
155
156 type input = Text | Automath
157
158 type input_entity = TxtEntity of Txt.entity
159                   | AutEntity of Aut.entity
160
161 let type_of_input name =
162    if F.check_suffix name ".hln" then Text 
163    else if F.check_suffix name ".aut" then Automath
164    else begin
165       L.warn (P.sprintf "Unknown file type: %s" name); exit 2
166    end
167
168 let txt_xl = initial_lexer TxtLexer.token 
169
170 let aut_xl = initial_lexer AutLexer.token 
171
172 let entity_of_input lexbuf = function
173    | Text     -> 
174       begin match TxtParser.entry (token txt_xl) lexbuf with
175          | Some e, unget -> txt_xl.unget <- unget; Some (TxtEntity e)
176          | None, _       -> None
177       end
178    | Automath -> 
179       begin match AutParser.entry (token aut_xl) lexbuf with
180          | Some e, unget -> aut_xl.unget <- unget; Some (AutEntity e)
181          | None, _       -> None
182       end
183
184 let process_input f st = function 
185    | AutEntity e     ->
186       let f ast e = f {st with ast = ast} (AutEntity e) in
187       AP.process_entity f st.ast e
188    | xe              -> f st xe
189
190 let count_input st = function
191    | AutEntity e -> {st with ac = AO.count_entity C.start st.ac e}
192    | xe          -> st
193
194 (****************************************************************************)
195
196 let stage = ref 3
197 let moch = ref None
198 let meta = ref false
199 let progress = ref false
200 let preprocess = ref false
201 let use_cover = ref true
202 let si = ref false
203 let expand = ref false
204 let cc = ref false
205 let export = ref false
206 let graph = ref (H.graph_of_string C.err C.start "Z2")
207 let old = ref false
208
209 let process_2 st entity =
210    let st = if !L.level > 2 then count_entity st entity else st in
211    if !export then export_entity !si !graph !moch entity;
212    if !stage > 2 then type_check st entity else st
213             
214 let process_1 st entity = 
215    if !progress then pp_progress entity;
216    let st = if !L.level > 2 then count_entity st entity else st in
217    if !export && !stage = 1 then export_entity !si !graph !moch entity;
218    if !stage > 1 then process_2 st (xlate_entity entity) else st 
219
220 let process_0 st entity = 
221    let f st entity =
222       if !stage = 0 then st else
223       let frr mst = {st with mst = mst} in
224       let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
225       let err dst = {st with dst = dst} in
226       let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
227       let crr tst = {st with tst = tst} in
228       let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
229       match entity, !old with
230          | AutEntity e, true  -> MA.meta_of_aut frr h st.mst e 
231          | AutEntity e, false -> DA.crg_of_aut err g st.dst e
232          | TxtEntity e, false -> DT.crg_of_txt crr d st.tst e
233          | _                  -> assert false
234    in
235    let st = if !L.level > 2 then count_input st entity else st in 
236    if !preprocess then process_input f st entity else f st entity
237
238 let process st name =
239    let input = type_of_input name in
240    let ich = open_in name in
241    let lexbuf = Lexing.from_channel ich in 
242    let rec aux st = match entity_of_input lexbuf input with
243       | None   -> close_in ich; st, input
244       | Some e -> aux (process_0 st e)
245    in
246    aux st
247
248 (****************************************************************************)
249
250 let main =
251 try 
252    let version_string = "Helena 0.8.1 M - February 2010" in
253    let set_hierarchy s = 
254       let err () = L.warn (P.sprintf "Unknown type hierarchy: %s" s) in
255       let f g = graph := g in
256       H.graph_of_string err f s
257    in
258    let set_kernel = function
259       | "brg" -> kernel := Brg
260       | "bag" -> kernel := Bag
261       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
262    in
263    let set_summary i = L.level := i in
264    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
265    let set_stage i = stage := i in
266    let set_meta_file name =
267       let f och = moch := Some och in
268       ML.open_out f name
269    in
270    let unquote () =
271       AL.unquote := true
272    in
273    let close = function
274       | None     -> ()
275       | Some och -> ML.close_out C.start och
276    in
277    let process_file name =
278       if !L.level > 0 then T.gmtime version_string;      
279       if !L.level > 1 then
280          L.warn (P.sprintf "Processing file: %s" name);
281       if !L.level > 0 then T.utime_stamp "started";
282       let base_name = Filename.chop_extension (Filename.basename name) in
283       if !meta then set_meta_file base_name;
284       O.clear_reductions ();
285       let mk_uri =
286          if !stage < 2 then Crg.mk_uri else
287          match !kernel with
288             | Brg -> Brg.mk_uri
289             | Bag -> Bag.mk_uri
290       in
291       let cover = if !use_cover then base_name else "" in
292       let st = initial_status mk_uri cover !graph !expand !si in
293       let st, input = process st name in
294       if !L.level > 0 then T.utime_stamp "processed";
295       if !L.level > 2 then begin
296          AO.print_counters C.start st.ac;
297          if !preprocess then AO.print_process_counters C.start st.ast;
298          if !stage > 0 then MO.print_counters C.start st.mc;
299          if !stage > 1 then print_counters st;
300          if !stage > 2 then O.print_reductions ()
301       end
302    in
303    let exit () =
304       close !moch;
305       if !L.level > 0 then T.utime_stamp "at exit";
306       flush_all ()
307    in
308    let help = 
309       "Usage: helena [ -Vcgijmopqrux | -Ss <number> | -hk <string> ] <file> ...\n\n" ^
310       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
311        3 data information, 4 typing information, 5 reduction information\n\n" ^
312        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
313    in
314    let help_S = "<number>  set summary level (see above)" in
315    let help_V = " show version information" in   
316
317    let help_c = " output conversion constraints" in
318    let help_g = " always expand global definitions" in
319    let help_h = "<string>  set type hierarchy (default: Z2)" in
320    let help_i = " show local references by index" in
321    let help_j = " show URI of processed kernel objects" in
322    let help_k = "<string>  set kernel version (default: brg)" in
323    let help_m = " output intermediate representation (HAL)" in
324    let help_o = " use old abstract language instead of crg" in   
325    let help_p = " preprocess Automath source" in
326    let help_q = " disable quotation of identifiers" in
327    let help_r = " disable initial segment of URI hierarchy" in
328    let help_s = "<number>  set translation stage (see above)" in
329    let help_u = " activate sort inclusion" in
330    let help_x = " export kernel entities (XML)" in
331    L.box 0; L.box_err ();
332    let _ = H.set_sorts ["Set"; "Prop"] 0 in
333    at_exit exit;
334    Arg.parse [
335       ("-S", Arg.Int set_summary, help_S);
336       ("-V", Arg.Unit print_version, help_V);
337       ("-c", Arg.Set cc, help_c);
338       ("-g", Arg.Set expand, help_g);
339       ("-h", Arg.String set_hierarchy, help_h);
340       ("-i", Arg.Set O.indexes, help_i);
341       ("-j", Arg.Set progress, help_j);      
342       ("-k", Arg.String set_kernel, help_k);
343       ("-m", Arg.Set meta, help_m); 
344       ("-o", Arg.Set old, help_o);      
345       ("-p", Arg.Set preprocess, help_p);      
346       ("-q", Arg.Unit unquote, help_q);      
347       ("-r", Arg.Clear use_cover, help_r);
348       ("-s", Arg.Int set_stage, help_s);
349       ("-u", Arg.Set si, help_u);
350       ("-x", Arg.Set export, help_x)
351    ] process_file help;
352 with BagT.TypeError msg -> bag_error "Type Error" msg