]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
2509ffc845ef5cc7611aa793fe460e97395c2452
[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 MA   = MetaAut
24 module MO   = MetaOutput
25 module ML   = MetaLibrary
26 module MBrg = MetaBrg
27 module BrgO = BrgOutput
28 module BrgR = BrgReduction
29 module BrgU = BrgUntrusted
30 module MBag = MetaBag
31 module BagO = BagOutput
32 module BagT = BagType
33 module BagU = BagUntrusted
34
35 type status = {
36    ast : AP.status;
37    mst : MA.status;
38    ac  : AO.counters;
39    mc  : MO.counters;
40    brgc: BrgO.counters;
41    bagc: BagO.counters
42 }
43
44 let initial_status cover = {
45    ac   = AO.initial_counters;
46    mc   = MO.initial_counters;
47    brgc = BrgO.initial_counters;
48    bagc = BagO.initial_counters;
49    mst  = MA.initial_status ~cover ();
50    ast  = AP.initial_status
51 }
52
53 let count count_fun c entity =
54    if !L.level > 2 then count_fun C.start c entity else c
55
56 let flush () = L.flush 0; L.flush_err ()
57
58 let bag_error s msg =
59    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush () 
60
61 let brg_error s msg =
62    L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush () 
63
64 let process_entity f st =
65    let f ast = f {st with ast = ast} in
66    AP.process_entity f st.ast
67
68 (* kernel related ***********************************************************)
69
70 type kernel = Brg | Bag
71
72 type kernel_entity = BrgEntity of Brg.entity
73                    | BagEntity of Bag.entity
74
75 let kernel = ref Brg
76
77 let print_counters st = match !kernel with
78    | Brg -> BrgO.print_counters C.start st.brgc
79    | Bag -> BagO.print_counters C.start st.bagc
80
81 let kernel_of_meta f st entity = match !kernel with
82    | Brg -> 
83       let f entity = f st (BrgEntity entity) in
84       Y.xlate f MBrg.brg_of_meta entity
85    | Bag -> 
86       let f entity = f st (BagEntity entity) in
87       Y.xlate f MBag.bag_of_meta entity
88
89 let count_entity st = function
90    | BrgEntity entity -> {st with brgc = count BrgO.count_entity st.brgc entity}
91    | BagEntity entity -> {st with bagc = count BagO.count_entity st.bagc entity}
92
93 let export_entity si g = function
94    | BrgEntity entity -> X.old_export_entity BrgO.export_term si g entity
95    | BagEntity _      -> ()
96
97 let type_check f st si g k =
98    let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
99    let f _ (a, u, _) = f st a u in
100    match k with
101       | BrgEntity entity -> BrgU.type_check brg_err f ~si g entity
102       | BagEntity entity -> BagU.type_check f ~si g entity
103
104 (****************************************************************************)
105
106 let main =
107 try 
108    let version_string = "Helena 0.8.1 M - September 2009" in
109    let stage = ref 3 in
110    let moch = ref None in
111    let meta = ref false in
112    let progress = ref false in
113    let process = ref false in
114    let use_cover = ref true in
115    let si = ref false in
116    let cc = 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          | entity :: tl ->
155 (* stage 3 *)
156             let f st a u = 
157                if !progress then 
158                   let s = U.string_of_uri u in
159                   let err () = L.warn (P.sprintf "%s" s); st in
160                   let f i = L.warn (P.sprintf "[%u] %s" i s); st in
161                   Y.mark err f a
162                else
163                   st
164             in
165 (* stage 2 *)       
166             let f st entity =
167                let st = count_entity st entity in
168                if !export then export_entity !si !graph entity;
169                if !stage > 2 then type_check f st !si !graph entity else st
170             in
171 (* stage 1 *)       
172             let f st mst entity = 
173                let st = {st with
174                   mst = mst; mc = count MO.count_entity st.mc entity
175                } in
176                begin match !moch with
177                   | None     -> ()
178                   | Some och -> ML.write_entity C.start och entity
179                end;
180                if !stage > 1 then kernel_of_meta f st entity else st 
181             in  
182 (* stage 0 *)       
183             let st = {st with ac = count AO.count_entity st.ac entity} in
184             let err st mst = {st with mst = mst} in
185             let f st entity =
186                let st = 
187                   if !stage > 0 then
188                      MA.meta_of_aut (err st) (f st) st.mst entity
189                   else st
190                in
191                aux st tl
192             in
193             if !process then process_entity f st entity else f st entity
194       in
195       O.clear_reductions ();
196       let cover = if !use_cover then base_name else "" in
197       let st = aux (initial_status cover) book in
198       if !L.level > 0 then T.utime_stamp "processed";
199       if !L.level > 2 then AO.print_counters C.start st.ac;
200       if !L.level > 2 && !process then AO.print_process_counters C.start st.ast;
201       if !L.level > 2 && !stage > 0 then MO.print_counters C.start st.mc;
202       if !L.level > 2 && !stage > 1 then print_counters st;
203       if !L.level > 2 && !stage > 1 then O.print_reductions ()
204    in
205    let exit () =
206       close !moch;
207       if !L.level > 0 then T.utime_stamp "at exit";
208       flush ()
209    in
210    let help = 
211       "Usage: helena [ -Vcijmpux | -Ss <number> | -hk <string> ] <file> ...\n\n" ^
212       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
213        3 data information, 4 typing information, 5 reduction information\n\n" ^
214        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
215    in
216    let help_S = "<number>  set summary level (see above)" in
217    let help_V = " show version information" in   
218
219    let help_c = " output conversion constraints" in
220    let help_h = "<string>  set type hierarchy (default: Z2)" in
221    let help_i = " show local references by index" in
222    let help_j = " show URI of processed kernel objects" in
223    let help_k = "<string>  set kernel version (default: brg)" in
224    let help_m = " output intermediate representation (HAL)" in
225    let help_p = " preprocess Automath source" in
226    let help_r = " disable initial segment of URI hierarchy" in
227    let help_s = "<number>  set translation stage (see above)" in
228    let help_u = " activate sort inclusion" in
229    let help_x = " export kernel objects (XML)" in
230    L.box 0; L.box_err ();
231    H.set_sorts ignore ["Set"; "Prop"] 0;
232    at_exit exit;
233    Arg.parse [
234       ("-S", Arg.Int set_summary, help_S);
235       ("-V", Arg.Unit print_version, help_V);
236       ("-c", Arg.Set cc, help_c);
237       ("-h", Arg.String set_hierarchy, help_h);
238       ("-i", Arg.Set O.indexes, help_i);
239       ("-j", Arg.Set progress, help_j);      
240       ("-k", Arg.String set_kernel, help_k);
241       ("-m", Arg.Set meta, help_m); 
242       ("-p", Arg.Set process, help_p);      
243       ("-r", Arg.Clear use_cover, help_r);
244       ("-s", Arg.Int set_stage, help_s);
245       ("-u", Arg.Set si, help_u);
246       ("-x", Arg.Set export, help_x)
247    ] read_file help;
248 with BagT.TypeError msg -> bag_error "Type Error" msg