]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
new xml exportation procedure for basic_rg (10 times faster than previous). the stati...
[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 P    = Printf
13 module U    = NUri
14 module C    = Cps
15 module L    = Log
16 module T    = Time
17 module H    = Hierarchy
18 module O    = Output
19 module Y    = Entity
20 module X    = Library
21 module AP   = AutProcess
22 module AO   = AutOutput
23 module DA   = DrgAut
24 module MA   = MetaAut
25 module MO   = MetaOutput
26 module ML   = MetaLibrary
27 module DO   = DrgOutput
28 module DBrg = DrgBrg
29 module MBrg = MetaBrg
30 module BrgO = BrgOutput
31 module BrgR = BrgReduction
32 module BrgU = BrgUntrusted
33 module MBag = MetaBag
34 module BagO = BagOutput
35 module BagT = BagType
36 module BagU = BagUntrusted
37
38 type status = {
39    ast : AP.status;
40    dst : DA.status;
41    mst : MA.status;
42    ac  : AO.counters;
43    mc  : MO.counters;
44    brgc: BrgO.counters;
45    bagc: BagO.counters
46 }
47
48 let initial_status mk_uri cover = {
49    ac   = AO.initial_counters;
50    mc   = MO.initial_counters;
51    brgc = BrgO.initial_counters;
52    bagc = BagO.initial_counters;
53    mst  = MA.initial_status ~cover ();
54    dst  = DA.initial_status (mk_uri cover);
55    ast  = AP.initial_status
56 }
57
58 let count count_fun c entity =
59    if !L.level > 2 then count_fun C.start c entity else c
60
61 let flush_all () = L.flush 0; L.flush_err ()
62
63 let bag_error s msg =
64    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
65
66 let brg_error s msg =
67    L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
68
69 let process_entity f st =
70    let f ast = f {st with ast = ast} in
71    AP.process_entity f st.ast
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                    | DrgEntity  of Drg.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 f st entity = match !kernel, entity with
89    | Brg, DrgEntity e  -> 
90       let f e = f st (BrgEntity e) in Y.xlate f DBrg.brg_of_drg e
91    | Brg, MetaEntity e -> 
92       let f e = f st (BrgEntity e) in Y.xlate f MBrg.brg_of_meta e
93    | Bag, MetaEntity e -> 
94       let f e = f st (BagEntity e) in Y.xlate f MBag.bag_of_meta e  
95    | _, entity         -> f st 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       | DrgEntity 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 = count MO.count_entity st.mc e} 
112    | BrgEntity e  -> {st with brgc = count BrgO.count_entity st.brgc e}
113    | BagEntity e  -> {st with bagc = count BagO.count_entity st.bagc e}
114    | _            -> st
115
116 let export_entity si g moch = function
117    | DrgEntity 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 f st si g k =
127    let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
128    let ok _ (a, u, _) = f st a u in
129    match k with
130       | BrgEntity entity     -> BrgU.type_check brg_err ok ~si g entity
131       | BagEntity entity     -> BagU.type_check ok ~si g entity
132       | DrgEntity (a, u, _)
133       | MetaEntity (a, u, _) -> f st a u
134
135 (****************************************************************************)
136
137 let stage = ref 3
138 let moch = ref None
139 let meta = ref false
140 let progress = ref false
141 let preprocess = ref false
142 let use_cover = ref true
143 let si = ref false
144 let cc = ref false
145 let export = ref false
146 let graph = ref (H.graph_of_string C.err C.start "Z2")
147 let old = ref false
148
149 let process_3 f st a u =
150    f st
151
152 let process_2 f st entity =
153    let st = count_entity st entity in
154    if !export then export_entity !si !graph !moch entity;
155    if !stage > 2 then type_check (process_3 f) st !si !graph entity else f st
156             
157 let process_1 f st entity = 
158    if !progress then pp_progress entity;
159    let st = count_entity st entity in
160    if !export && !stage = 1 then export_entity !si !graph !moch entity;
161    if !stage > 1 then xlate (process_2 f) st entity else f st 
162
163 let process_0 f st entity = 
164    let f st entity =
165       if !stage = 0 then f st else
166       let frr mst = f {st with mst = mst} in
167       let h mst e = process_1 f {st with mst = mst} (MetaEntity e) in
168       let err dst = f {st with dst = dst} in
169       let g dst e = process_1 f {st with dst = dst} (DrgEntity e) in
170       if !old then MA.meta_of_aut frr h st.mst entity else 
171       DA.drg_of_aut err g st.dst entity
172    in
173    let st = {st with ac = count AO.count_entity st.ac entity} in 
174    if !preprocess then process_entity f st entity else f st entity
175
176 let rec process f book st = match book with
177    | []           -> f st
178    | entity :: tl -> 
179 (* we exploit tail recursion rather than CPS *)
180       process f tl (process_0 C.start st entity)
181
182 (****************************************************************************)
183
184 let main =
185 try 
186    let version_string = "Helena 0.8.1 M - October 2009" in
187    let set_hierarchy s = 
188       let err () = L.warn (P.sprintf "Unknown type hierarchy: %s" s) in
189       let f g = graph := g in
190       H.graph_of_string err f s
191    in
192    let set_kernel = function
193       | "brg" -> kernel := Brg
194       | "bag" -> kernel := Bag
195       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
196    in
197    let set_summary i = L.level := i in
198    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
199    let set_stage i = stage := i in
200    let set_meta_file name =
201       let f och = moch := Some och in
202       ML.open_out f name
203    in
204    let close = function
205       | None     -> ()
206       | Some och -> ML.close_out C.start och
207    in
208    let read_file name =
209       if !L.level > 0 then T.gmtime version_string;      
210       if !L.level > 1 then
211          L.warn (P.sprintf "Processing file: %s" name);
212       if !L.level > 0 then T.utime_stamp "started";
213       let base_name = Filename.chop_extension (Filename.basename name) in
214       if !meta then set_meta_file base_name;
215       let ich = open_in name in
216       let lexbuf = Lexing.from_channel ich in
217       let book = AutParser.book AutLexer.token lexbuf in
218       close_in ich;
219       if !L.level > 0 then T.utime_stamp "parsed";
220       O.clear_reductions ();
221       let mk_uri =
222          if !stage < 2 then Drg.mk_uri else
223          match !kernel with
224             | Brg -> Brg.mk_uri
225             | Bag -> Bag.mk_uri
226       in
227       let cover = if !use_cover then base_name else "" in
228       let f st = 
229          if !L.level > 0 then T.utime_stamp "processed";
230          if !L.level > 2 then AO.print_counters C.start st.ac;
231          if !L.level > 2 && !preprocess then AO.print_process_counters C.start st.ast;
232          if !L.level > 2 && !stage > 0 then MO.print_counters C.start st.mc;
233          if !L.level > 2 && !stage > 1 then print_counters st;
234          if !L.level > 2 && !stage > 1 then O.print_reductions ()
235       in
236       process f book (initial_status mk_uri cover)
237    in
238    let exit () =
239       close !moch;
240       if !L.level > 0 then T.utime_stamp "at exit";
241       flush_all ()
242    in
243    let help = 
244       "Usage: helena [ -Vcijmopux | -Ss <number> | -hk <string> ] <file> ...\n\n" ^
245       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
246        3 data information, 4 typing information, 5 reduction information\n\n" ^
247        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
248    in
249    let help_S = "<number>  set summary level (see above)" in
250    let help_V = " show version information" in   
251
252    let help_c = " output conversion constraints" in
253    let help_h = "<string>  set type hierarchy (default: Z2)" in
254    let help_i = " show local references by index" in
255    let help_j = " show URI of processed kernel objects" in
256    let help_k = "<string>  set kernel version (default: brg)" in
257    let help_m = " output intermediate representation (HAL)" in
258    let help_o = " use old abstract language instead of drg" in   
259    let help_p = " preprocess Automath source" in
260    let help_r = " disable initial segment of URI hierarchy" in
261    let help_s = "<number>  set translation stage (see above)" in
262    let help_u = " activate sort inclusion" in
263    let help_x = " export kernel entities (XML)" in
264    L.box 0; L.box_err ();
265    H.set_sorts ignore ["Set"; "Prop"] 0;
266    at_exit exit;
267    Arg.parse [
268       ("-S", Arg.Int set_summary, help_S);
269       ("-V", Arg.Unit print_version, help_V);
270       ("-c", Arg.Set cc, help_c);
271       ("-h", Arg.String set_hierarchy, help_h);
272       ("-i", Arg.Set O.indexes, help_i);
273       ("-j", Arg.Set progress, help_j);      
274       ("-k", Arg.String set_kernel, help_k);
275       ("-m", Arg.Set meta, help_m); 
276       ("-o", Arg.Set old, help_o);      
277       ("-p", Arg.Set preprocess, help_p);      
278       ("-r", Arg.Clear use_cover, help_r);
279       ("-s", Arg.Int set_stage, help_s);
280       ("-u", Arg.Set si, help_u);
281       ("-x", Arg.Set export, help_x)
282    ] read_file help;
283 with BagT.TypeError msg -> bag_error "Type Error" msg