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