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