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.
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_______________________________________________________________ *)
22 module AP = AutProcess
26 module MO = MetaOutput
27 module ML = MetaLibrary
31 module BrgO = BrgOutput
32 module BrgR = BrgReduction
33 module BrgU = BrgUntrusted
35 module BagO = BagOutput
37 module BagU = BagUntrusted
50 let initial_status mk_uri cover g expand si = {
51 ac = AO.initial_counters;
52 mc = MO.initial_counters;
53 brgc = BrgO.initial_counters;
54 bagc = BagO.initial_counters;
55 mst = MA.initial_status ~cover ();
56 dst = DA.initial_status (mk_uri si cover);
57 ast = AP.initial_status;
58 kst = Y.initial_status g expand si
61 let flush_all () = L.flush 0; L.flush_err ()
64 L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all ()
67 L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all ()
69 let process_entity f st entity =
70 let f ast = f {st with ast = ast} in
71 AP.process_entity f st.ast entity
73 (* kernel related ***********************************************************)
75 type kernel = Brg | Bag
77 type kernel_entity = BrgEntity of Brg.entity
78 | BagEntity of Bag.entity
79 | CrgEntity of Crg.entity
80 | MetaEntity of Meta.entity
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
88 let xlate_entity entity = match !kernel, entity with
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
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
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
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}
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
120 begin match moch with
122 | Some och -> ML.write_entity C.start och e
126 let type_check st k =
127 let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
130 | BrgEntity entity -> BrgU.type_check brg_err ok st.kst entity
131 | BagEntity entity -> BagU.type_check ok st.kst entity
135 (****************************************************************************)
140 let progress = ref false
141 let preprocess = ref false
142 let use_cover = ref true
144 let expand = ref false
146 let export = ref false
147 let graph = ref (H.graph_of_string C.err C.start "Z2")
150 let process_2 st entity =
151 let st = if !L.level > 2 then count_entity st entity else st in
152 if !export then export_entity !si !graph !moch entity;
153 if !stage > 2 then type_check st entity else st
155 let process_1 st entity =
156 if !progress then pp_progress entity;
157 let st = if !L.level > 2 then count_entity st entity else st in
158 if !export && !stage = 1 then export_entity !si !graph !moch entity;
159 if !stage > 1 then process_2 st (xlate_entity entity) else st
161 let process_0 st entity =
163 if !stage = 0 then st else
164 let frr mst = {st with mst = mst} in
165 let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
166 let err dst = {st with dst = dst} in
167 let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
168 if !old then MA.meta_of_aut frr h st.mst entity else
169 DA.crg_of_aut err g st.dst entity
172 if !L.level > 2 then {st with ac = AO.count_entity C.start st.ac entity}
175 if !preprocess then process_entity f st entity else f st entity
177 let rec process st = function
179 | entity :: tl -> process (process_0 st entity) tl
181 (****************************************************************************)
185 let version_string = "Helena 0.8.1 M - January 2010" in
186 let set_hierarchy s =
187 let err () = L.warn (P.sprintf "Unknown type hierarchy: %s" s) in
188 let f g = graph := g in
189 H.graph_of_string err f s
191 let set_kernel = function
192 | "brg" -> kernel := Brg
193 | "bag" -> kernel := Bag
194 | s -> L.warn (P.sprintf "Unknown kernel version: %s" s)
196 let set_summary i = L.level := i in
197 let print_version () = L.warn (version_string ^ "\n"); exit 0 in
198 let set_stage i = stage := i in
199 let set_meta_file name =
200 let f och = moch := Some och in
208 | Some och -> ML.close_out C.start och
211 if !L.level > 0 then T.gmtime version_string;
213 L.warn (P.sprintf "Processing file: %s" name);
214 if !L.level > 0 then T.utime_stamp "started";
215 let base_name = Filename.chop_extension (Filename.basename name) in
216 if !meta then set_meta_file base_name;
217 let ich = open_in name in
218 let lexbuf = Lexing.from_channel ich in
219 let book = AutParser.book AutLexer.token lexbuf in
221 if !L.level > 0 then T.utime_stamp "parsed";
222 O.clear_reductions ();
224 if !stage < 2 then Crg.mk_uri else
229 let cover = if !use_cover then base_name else "" in
230 let st = initial_status mk_uri cover !graph !expand !si in
231 let st = process st book in
232 if !L.level > 0 then T.utime_stamp "processed";
233 if !L.level > 2 then begin
234 AO.print_counters C.start st.ac;
235 if !preprocess then AO.print_process_counters C.start st.ast;
236 if !stage > 0 then MO.print_counters C.start st.mc;
237 if !stage > 1 then print_counters st;
238 if !stage > 2 then O.print_reductions ()
243 if !L.level > 0 then T.utime_stamp "at exit";
247 "Usage: helena [ -Vcgijmopqrux | -Ss <number> | -hk <string> ] <file> ...\n\n" ^
248 "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
249 3 data information, 4 typing information, 5 reduction information\n\n" ^
250 "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
252 let help_S = "<number> set summary level (see above)" in
253 let help_V = " show version information" in
255 let help_c = " output conversion constraints" in
256 let help_g = " always expand global definitions" in
257 let help_h = "<string> set type hierarchy (default: Z2)" in
258 let help_i = " show local references by index" in
259 let help_j = " show URI of processed kernel objects" in
260 let help_k = "<string> set kernel version (default: brg)" in
261 let help_m = " output intermediate representation (HAL)" in
262 let help_o = " use old abstract language instead of crg" in
263 let help_p = " preprocess Automath source" in
264 let help_q = " disable quotation of identifiers" in
265 let help_r = " disable initial segment of URI hierarchy" in
266 let help_s = "<number> set translation stage (see above)" in
267 let help_u = " activate sort inclusion" in
268 let help_x = " export kernel entities (XML)" in
269 L.box 0; L.box_err ();
270 let _ = H.set_sorts ["Set"; "Prop"] 0 in
273 ("-S", Arg.Int set_summary, help_S);
274 ("-V", Arg.Unit print_version, help_V);
275 ("-c", Arg.Set cc, help_c);
276 ("-g", Arg.Set expand, help_g);
277 ("-h", Arg.String set_hierarchy, help_h);
278 ("-i", Arg.Set O.indexes, help_i);
279 ("-j", Arg.Set progress, help_j);
280 ("-k", Arg.String set_kernel, help_k);
281 ("-m", Arg.Set meta, help_m);
282 ("-o", Arg.Set old, help_o);
283 ("-p", Arg.Set preprocess, help_p);
284 ("-q", Arg.Unit unquote, help_q);
285 ("-r", Arg.Clear use_cover, help_r);
286 ("-s", Arg.Int set_stage, help_s);
287 ("-u", Arg.Set si, help_u);
288 ("-x", Arg.Set export, help_x)
290 with BagT.TypeError msg -> bag_error "Type Error" msg