]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
507d119084457538d0cbab41e452a5a6b21949f8
[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 MBrg = MetaBrg
24 module BrgO = BrgOutput
25 module BrgR = BrgReduction
26 module BrgU = BrgUntrusted
27 module MBag = MetaBag
28 module BagO = BagOutput
29 module BagR = BagReduction
30 module BagU = BagUntrusted
31
32 type status = {
33    ast : AP.status;
34    mst : MA.status;
35    ac  : AO.counters;
36    mc  : MO.counters;
37    brgc: BrgO.counters;
38    bagc: BagO.counters;
39    si  : bool
40 }
41
42 let initial_status cover si = {
43    ac   = AO.initial_counters;
44    mc   = MO.initial_counters;
45    brgc = BrgO.initial_counters;
46    bagc = BagO.initial_counters;
47    mst  = MA.initial_status ~cover ();
48    ast  = AP.initial_status;
49    si   = si
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 type_check f st g k =
93    let f _ = function
94       | None           -> f st None
95       | Some (i, u, _) -> f st (Some (i, u))
96    in
97    match k with
98       | BrgItem item -> BrgU.type_check f ~si:st.si g item
99       | BagItem item -> BagU.type_check f ~si:st.si g item
100
101 (****************************************************************************)
102
103 let main =
104 try 
105    let version_string = "Helena 0.8.0 M - August 2009" in
106    let stage = ref 3 in
107    let meta_file = ref None in
108    let progress = ref false in
109    let use_cover = ref true in
110    let si = ref false in
111    let set_hierarchy s = 
112       let f = function
113          | Some g -> H.graph := g
114          | None   -> L.warn (P.sprintf "Unknown type hierarchy: %s" s)
115       in
116       H.graph_of_string f s
117    in
118    let set_kernel = function
119       | "brg" -> kernel := Brg
120       | "bag" -> kernel := Bag
121       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
122    in
123    let set_summary i = L.level := i in
124    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
125    let set_stage i = stage := i in
126    let close = function
127       | None          -> ()
128       | Some (och, _) -> close_out och
129    in
130    let set_meta_file name =
131       close !meta_file;
132       let och = open_out name in
133       let frm = Format.formatter_of_out_channel och in
134       Format.pp_set_margin frm max_int;
135       meta_file := Some (och, frm)
136    in
137    let read_file name =
138       if !L.level > 0 then T.gmtime version_string;      
139       if !L.level > 1 then
140          L.warn (P.sprintf "Processing file: %s" name);
141       if !L.level > 0 then T.utime_stamp "started";
142       let ich = open_in name in
143       let lexbuf = Lexing.from_channel ich in
144       let book = AutParser.book AutLexer.token lexbuf in
145       close_in ich;
146       if !L.level > 0 then T.utime_stamp "parsed";
147       let rec aux st = function
148          | []         -> st
149          | item :: tl ->
150 (* stage 3 *)
151             let f st = function
152                | None        -> st
153                | Some (i, u) -> 
154                   if !progress then 
155                      L.warn (P.sprintf "[%u] %s" i (U.string_of_uri u)); 
156                   st
157             in
158 (* stage 2 *)       
159             let f st item =
160                let st = count_item st item in
161                if !stage > 2 then type_check f st !H.graph item else st
162             in
163 (* stage 1 *)       
164             let f st mst item = 
165                let st = {st with
166                   mst = mst; mc = count MO.count_item st.mc item
167                } in
168                begin match !meta_file with
169                   | None          -> ()
170                   | Some (_, frm) -> MO.pp_item C.start frm item
171                end;
172                if !stage > 1 then kernel_of_meta f st item else st 
173             in  
174 (* stage 0 *)       
175             let st = {st with ac = count AO.count_item st.ac item} in
176             let f st item =
177                let st = 
178                   if !stage > 0 then MA.meta_of_aut (f st) st.mst item else st
179                in
180                aux st tl
181             in
182             process_item f st item
183       in
184       O.clear_reductions ();
185       let cover = 
186          if !use_cover then Filename.chop_extension (Filename.basename name)
187          else ""
188       in
189       let st = aux (initial_status cover !si) book in
190       if !L.level > 0 then T.utime_stamp "processed";
191       if !L.level > 2 then AO.print_counters C.start st.ac;
192       if !L.level > 2 then AO.print_process_counters C.start st.ast;
193       if !L.level > 2 && !stage > 0 then MO.print_counters C.start st.mc;
194       if !L.level > 2 && !stage > 1 then print_counters st;
195       if !L.level > 2 && !stage > 1 then O.print_reductions ()
196    in
197    let help = 
198       "Usage: helena [ -Vcipu | -Ss <number> | -m <file> | -hk <string> ] <file> ...\n\n" ^
199       "Summary levels: 0 just errors, 1 time stamps, 2 processed file names, \
200        3 data information, 4 typing information, 5 reduction information\n\n" ^
201        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted\n"
202    in
203    let help_S = "<number>  set summary level" in
204    let help_V = " show version information" in   
205    let help_c = " disable initial segment of URI hierarchy" in
206    let help_h = "<string>  set type hierarchy" in
207    let help_i = " show local references by index" in
208    let help_k = "<string>  set kernel version" in
209    let help_m = "<file>  output intermediate representation" in
210    let help_p = " activate progress indicator" in
211    let help_u = " activate sort inclusion" in
212    let help_s = "<number>  Set translation stage" in
213    L.box 0; L.box_err ();
214    H.set_new_sorts ignore ["Set"; "Prop"];
215    Arg.parse [
216       ("-S", Arg.Int set_summary, help_S);
217       ("-V", Arg.Unit print_version, help_V);
218       ("-c", Arg.Clear use_cover, help_c);
219       ("-h", Arg.String set_hierarchy, help_h);
220       ("-i", Arg.Set O.indexes, help_i);
221       ("-k", Arg.String set_kernel, help_k);
222       ("-m", Arg.String set_meta_file, help_m); 
223       ("-p", Arg.Set progress, help_p);
224       ("-u", Arg.Set si, help_u);
225       ("-s", Arg.Int set_stage, help_s)
226    ] read_file help;
227    if !L.level > 0 then T.utime_stamp "at exit";
228    flush ()
229 with BagR.TypeError msg -> bag_error "Type Error" msg
230    | BrgR.TypeError msg -> brg_error "Type Error" msg