]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
3fa3c908ce66ccee70ca5c3756167af76323b775
[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 AP   = AutProcess
20 module AO   = AutOutput
21 module MA   = MetaAut
22 module MO   = MetaOutput
23 module ML   = MetaLibrary
24 module MBrg = MetaBrg
25 module G    = Library
26 module BrgO = BrgOutput
27 module BrgR = BrgReduction
28 module BrgU = BrgUntrusted
29 module MBag = MetaBag
30 module BagO = BagOutput
31 module BagR = BagReduction
32 module BagU = BagUntrusted
33
34 type status = {
35    ast : AP.status;
36    mst : MA.status;
37    ac  : AO.counters;
38    mc  : MO.counters;
39    brgc: BrgO.counters;
40    bagc: BagO.counters
41 }
42
43 let initial_status cover = {
44    ac   = AO.initial_counters;
45    mc   = MO.initial_counters;
46    brgc = BrgO.initial_counters;
47    bagc = BagO.initial_counters;
48    mst  = MA.initial_status ~cover ();
49    ast  = AP.initial_status
50 }
51
52 let count count_fun c item =
53    if !L.level > 2 then count_fun C.start c item else c
54
55 let flush () = L.flush 0; L.flush_err ()
56
57 let bag_error s msg =
58    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush () 
59
60 let brg_error s msg =
61    L.error BrgO.specs (L.Warn s :: L.Loc :: msg); flush () 
62
63 let process_item f st =
64    let f ast = f {st with ast = ast} in
65    AP.process_item f st.ast
66
67 (* kernel related ***********************************************************)
68
69 type kernel = Brg | Bag
70
71 type kernel_item = BrgItem of Brg.item
72                  | BagItem of Bag.item
73
74 let kernel = ref Brg
75
76 let print_counters st = match !kernel with
77    | Brg -> BrgO.print_counters C.start st.brgc
78    | Bag -> BagO.print_counters C.start st.bagc
79
80 let kernel_of_meta f st item = match !kernel with
81    | Brg -> 
82       let f item = f st (BrgItem item) in
83       MBrg.brg_of_meta f item
84    | Bag -> 
85       let f item = f st (BagItem item) in
86       MBag.bag_of_meta f item  
87
88 let count_item st = function
89    | BrgItem item -> {st with brgc = count BrgO.count_item st.brgc item}
90    | BagItem item -> {st with bagc = count BagO.count_item st.bagc item}
91
92 let export_item si g = function
93    | BrgItem item -> G.export_item BrgO.export_obj si g item
94    | BagItem _    -> ()
95
96 let type_check f st si g k =
97    let f _ = function
98       | None           -> f st None
99       | Some (i, u, _) -> f st (Some (i, u))
100    in
101    match k with
102       | BrgItem item -> BrgU.type_check f ~si g item
103       | BagItem item -> BagU.type_check f ~si g item
104
105 (****************************************************************************)
106
107 let main =
108 try 
109    let version_string = "Helena 0.8.0 M - August 2009" in
110    let stage = ref 3 in
111    let moch = ref None in
112    let meta = ref false in
113    let progress = ref false in
114    let process = ref false in
115    let use_cover = ref true in
116    let si = ref false in
117    let export = ref false in
118    let graph = ref (H.graph_of_string C.err C.start "Z2") in
119    let set_hierarchy s = 
120       let err () = L.warn (P.sprintf "Unknown type hierarchy: %s" s) in
121       let f g = graph := g in
122       H.graph_of_string err f s
123    in
124    let set_kernel = function
125       | "brg" -> kernel := Brg
126       | "bag" -> kernel := Bag
127       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
128    in
129    let set_summary i = L.level := i in
130    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
131    let set_stage i = stage := i in
132    let set_meta_file name =
133       let f och = moch := Some och in
134       ML.open_out f name
135    in
136    let close = function
137       | None     -> ()
138       | Some och -> ML.close_out C.start och
139    in
140    let read_file name =
141       if !L.level > 0 then T.gmtime version_string;      
142       if !L.level > 1 then
143          L.warn (P.sprintf "Processing file: %s" name);
144       if !L.level > 0 then T.utime_stamp "started";
145       let base_name = Filename.chop_extension (Filename.basename name) in
146       if !meta then set_meta_file base_name;
147       let ich = open_in name in
148       let lexbuf = Lexing.from_channel ich in
149       let book = AutParser.book AutLexer.token lexbuf in
150       close_in ich;
151       if !L.level > 0 then T.utime_stamp "parsed";
152       let rec aux st = function
153          | []         -> st
154          | item :: tl ->
155 (* stage 3 *)
156             let f st = function
157                | None        -> st
158                | Some (i, u) -> 
159                   if !progress then 
160                      L.warn (P.sprintf "[%u] %s" i (U.string_of_uri u)); 
161                   st
162             in
163 (* stage 2 *)       
164             let f st item =
165                let st = count_item st item in
166                if !export then export_item !si !graph item;
167                if !stage > 2 then type_check f st !si !graph item else st
168             in
169 (* stage 1 *)       
170             let f st mst item = 
171                let st = {st with
172                   mst = mst; mc = count MO.count_item st.mc item
173                } in
174                begin match !moch with
175                   | None     -> ()
176                   | Some och -> ML.write_item C.start och item
177                end;
178                if !stage > 1 then kernel_of_meta f st item else st 
179             in  
180 (* stage 0 *)       
181             let st = {st with ac = count AO.count_item st.ac item} in
182             let f st item =
183                let st = 
184                   if !stage > 0 then MA.meta_of_aut (f st) st.mst item else st
185                in
186                aux st tl
187             in
188             if !process then process_item f st item else f st item
189       in
190       O.clear_reductions ();
191       let cover = if !use_cover then base_name else "" in
192       let st = aux (initial_status cover) book in
193       if !L.level > 0 then T.utime_stamp "processed";
194       if !L.level > 2 then AO.print_counters C.start st.ac;
195       if !L.level > 2 && !process then AO.print_process_counters C.start st.ast;
196       if !L.level > 2 && !stage > 0 then MO.print_counters C.start st.mc;
197       if !L.level > 2 && !stage > 1 then print_counters st;
198       if !L.level > 2 && !stage > 1 then O.print_reductions ()
199    in
200    let exit () =
201       close !moch;
202       if !L.level > 0 then T.utime_stamp "at exit";
203       flush ()
204    in
205    let help = 
206       "Usage: helena [ -Vacimpux | -Ss <number> | -hk <string> ] <file> ...\n\n" ^
207       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
208        3 data information, 4 typing information, 5 reduction information\n\n" ^
209        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
210    in
211    let help_S = "<number>  set summary level (see above)" in
212    let help_V = " show version information" in   
213
214    let help_a = " analyze source files" in
215    let help_c = " disable initial segment of URI hierarchy" in
216    let help_h = "<string>  set type hierarchy (default: Z2)" in
217    let help_i = " show local references by index" in
218    let help_k = "<string>  set kernel version (default: brg)" in
219    let help_m = " output intermediate representation (HAL)" in
220    let help_p = " activate progress indicator" in
221    let help_u = " activate sort inclusion" in
222    let help_s = "<number>  set translation stage (see above)" in
223    let help_x = " export kernel objects (XML)" in
224    L.box 0; L.box_err ();
225    H.set_new_sorts ignore ["Set"; "Prop"];
226    at_exit exit;
227    Arg.parse [
228       ("-S", Arg.Int set_summary, help_S);
229       ("-V", Arg.Unit print_version, help_V);
230       ("-a", Arg.Set process, help_a);      
231       ("-c", Arg.Clear use_cover, help_c);
232       ("-h", Arg.String set_hierarchy, help_h);
233       ("-i", Arg.Set O.indexes, help_i);
234       ("-k", Arg.String set_kernel, help_k);
235       ("-m", Arg.Set meta, help_m); 
236       ("-p", Arg.Set progress, help_p);
237       ("-u", Arg.Set si, help_u);
238       ("-s", Arg.Int set_stage, help_s);
239       ("-x", Arg.Set export, help_x)
240    ] read_file help;
241 with BagR.TypeError msg -> bag_error "Type Error" msg
242    | BrgR.TypeError msg -> brg_error "Type Error" msg