]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
Additional contribs.
[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 DA   = CrgAut
24 module MA   = MetaAut
25 module MO   = MetaOutput
26 module ML   = MetaLibrary
27 module DO   = CrgOutput
28 module DBrg = CrgBrg
29 module MBrg = MetaBrg
30 module BrgO = BrgOutput
31 module BrgR = BrgReduction
32 module BrgU = BrgUntrusted
33 module MBag = MetaBag
34 module BagO = BagOutput
35 module BagT = BagType
36 module BagU = BagUntrusted
37
38 type status = {
39    ast : AP.status;
40    dst : DA.status;
41    mst : MA.status;
42    ac  : AO.counters;
43    mc  : MO.counters;
44    brgc: BrgO.counters;
45    bagc: BagO.counters
46 }
47
48 let initial_status mk_uri cover = {
49    ac   = AO.initial_counters;
50    mc   = MO.initial_counters;
51    brgc = BrgO.initial_counters;
52    bagc = BagO.initial_counters;
53    mst  = MA.initial_status ~cover ();
54    dst  = DA.initial_status (mk_uri cover);
55    ast  = AP.initial_status
56 }
57
58 let flush_all () = L.flush 0; L.flush_err ()
59
60 let bag_error s msg =
61    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
62
63 let brg_error s msg =
64    L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
65
66 let process_entity f st entity =
67    let f ast = f {st with ast = ast} in
68    AP.process_entity f st.ast entity
69
70 (* kernel related ***********************************************************)
71
72 type kernel = Brg | Bag
73
74 type kernel_entity = BrgEntity  of Brg.entity
75                    | BagEntity  of Bag.entity
76                    | CrgEntity  of Crg.entity
77                    | MetaEntity of Meta.entity
78
79 let kernel = ref Brg
80
81 let print_counters st = match !kernel with
82    | Brg -> BrgO.print_counters C.start st.brgc
83    | Bag -> BagO.print_counters C.start st.bagc
84
85 let xlate_entity entity = match !kernel, entity with
86    | Brg, CrgEntity e  -> 
87       let f e = (BrgEntity e) in Y.xlate f DBrg.brg_of_crg e
88    | Brg, MetaEntity e -> 
89       let f e = (BrgEntity e) in Y.xlate f MBrg.brg_of_meta e
90    | Bag, MetaEntity e -> 
91       let f e = (BagEntity e) in Y.xlate f MBag.bag_of_meta e  
92    | _, entity         -> entity
93
94 let pp_progress e =
95    let f a u =
96       let s = U.string_of_uri u in
97       let err () = L.warn (P.sprintf "%s" s) in
98       let f i = L.warn (P.sprintf "[%u] %s" i s) in
99       Y.mark err f a
100    in
101    match e with
102       | CrgEntity e -> Y.common f e
103       | BrgEntity e -> Y.common f e
104       | BagEntity e -> Y.common f e      
105       | MetaEntity e -> Y.common f e
106
107 let count_entity st = function
108    | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e} 
109    | BrgEntity e  -> {st with brgc = BrgO.count_entity C.start st.brgc e}
110    | BagEntity e  -> {st with bagc = BagO.count_entity C.start st.bagc e}
111    | _            -> st
112
113 let export_entity si g moch = function
114    | CrgEntity e  -> X.export_entity DO.export_term si g e
115    | BrgEntity e  -> X.export_entity BrgO.export_term si g e
116    | MetaEntity e ->
117       begin match moch with
118          | None     -> ()
119          | Some och -> ML.write_entity C.start och e
120       end
121    | BagEntity _  -> ()
122
123 let type_check st si g k =
124    let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
125    let ok _ _ = st in
126    match k with
127       | BrgEntity entity -> BrgU.type_check brg_err ok ~si g entity
128       | BagEntity entity -> BagU.type_check ok ~si g entity
129       | CrgEntity _
130       | MetaEntity _     -> st
131
132 (****************************************************************************)
133
134 let stage = ref 3
135 let moch = ref None
136 let meta = ref false
137 let progress = ref false
138 let preprocess = ref false
139 let use_cover = ref true
140 let si = ref false
141 let cc = ref false
142 let export = ref false
143 let graph = ref (H.graph_of_string C.err C.start "Z2")
144 let old = ref false
145
146 let process_2 st entity =
147    let st = if !L.level > 2 then count_entity st entity else st in
148    if !export then export_entity !si !graph !moch entity;
149    if !stage > 2 then type_check st !si !graph entity else st
150             
151 let process_1 st entity = 
152    if !progress then pp_progress entity;
153    let st = if !L.level > 2 then count_entity st entity else st in
154    if !export && !stage = 1 then export_entity !si !graph !moch entity;
155    if !stage > 1 then process_2 st (xlate_entity entity) else st 
156
157 let process_0 st entity = 
158    let f st entity =
159       if !stage = 0 then st else
160       let frr mst = {st with mst = mst} in
161       let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
162       let err dst = {st with dst = dst} in
163       let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
164       if !old then MA.meta_of_aut frr h st.mst entity else 
165       DA.crg_of_aut err g st.dst entity
166    in
167    let st = 
168       if !L.level > 2 then {st with ac = AO.count_entity C.start st.ac entity}
169       else st
170    in 
171    if !preprocess then process_entity f st entity else f st entity
172
173 let rec process st = function
174    | []           -> st
175    | entity :: tl -> process (process_0 st entity) tl
176
177 (****************************************************************************)
178
179 let main =
180 try 
181    let version_string = "Helena 0.8.1 M - October 2009" in
182    let set_hierarchy s = 
183       let err () = L.warn (P.sprintf "Unknown type hierarchy: %s" s) in
184       let f g = graph := g in
185       H.graph_of_string err f s
186    in
187    let set_kernel = function
188       | "brg" -> kernel := Brg
189       | "bag" -> kernel := Bag
190       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
191    in
192    let set_summary i = L.level := i in
193    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
194    let set_stage i = stage := i in
195    let set_meta_file name =
196       let f och = moch := Some och in
197       ML.open_out f name
198    in
199    let close = function
200       | None     -> ()
201       | Some och -> ML.close_out C.start och
202    in
203    let read_file name =
204       if !L.level > 0 then T.gmtime version_string;      
205       if !L.level > 1 then
206          L.warn (P.sprintf "Processing file: %s" name);
207       if !L.level > 0 then T.utime_stamp "started";
208       let base_name = Filename.chop_extension (Filename.basename name) in
209       if !meta then set_meta_file base_name;
210       let ich = open_in name in
211       let lexbuf = Lexing.from_channel ich in
212       let book = AutParser.book AutLexer.token lexbuf in
213       close_in ich;
214       if !L.level > 0 then T.utime_stamp "parsed";
215       O.clear_reductions ();
216       let mk_uri =
217          if !stage < 2 then Crg.mk_uri else
218          match !kernel with
219             | Brg -> Brg.mk_uri
220             | Bag -> Bag.mk_uri
221       in
222       let cover = if !use_cover then base_name else "" in
223       let st = process (initial_status mk_uri cover) book in
224       if !L.level > 0 then T.utime_stamp "processed";
225       if !L.level > 2 then begin
226          AO.print_counters C.start st.ac;
227          if !preprocess then AO.print_process_counters C.start st.ast;
228          if !stage > 0 then MO.print_counters C.start st.mc;
229          if !stage > 1 then print_counters st;
230          if !stage > 2 then O.print_reductions ()
231       end
232    in
233    let exit () =
234       close !moch;
235       if !L.level > 0 then T.utime_stamp "at exit";
236       flush_all ()
237    in
238    let help = 
239       "Usage: helena [ -Vcijmopux | -Ss <number> | -hk <string> ] <file> ...\n\n" ^
240       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
241        3 data information, 4 typing information, 5 reduction information\n\n" ^
242        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
243    in
244    let help_S = "<number>  set summary level (see above)" in
245    let help_V = " show version information" in   
246
247    let help_c = " output conversion constraints" in
248    let help_h = "<string>  set type hierarchy (default: Z2)" in
249    let help_i = " show local references by index" in
250    let help_j = " show URI of processed kernel objects" in
251    let help_k = "<string>  set kernel version (default: brg)" in
252    let help_m = " output intermediate representation (HAL)" in
253    let help_o = " use old abstract language instead of crg" in   
254    let help_p = " preprocess Automath source" in
255    let help_r = " disable initial segment of URI hierarchy" in
256    let help_s = "<number>  set translation stage (see above)" in
257    let help_u = " activate sort inclusion" in
258    let help_x = " export kernel entities (XML)" in
259    L.box 0; L.box_err ();
260    let _ = H.set_sorts ["Set"; "Prop"] 0 in
261    at_exit exit;
262    Arg.parse [
263       ("-S", Arg.Int set_summary, help_S);
264       ("-V", Arg.Unit print_version, help_V);
265       ("-c", Arg.Set cc, help_c);
266       ("-h", Arg.String set_hierarchy, help_h);
267       ("-i", Arg.Set O.indexes, help_i);
268       ("-j", Arg.Set progress, help_j);      
269       ("-k", Arg.String set_kernel, help_k);
270       ("-m", Arg.Set meta, help_m); 
271       ("-o", Arg.Set old, help_o);      
272       ("-p", Arg.Set preprocess, help_p);      
273       ("-r", Arg.Clear use_cover, help_r);
274       ("-s", Arg.Int set_stage, help_s);
275       ("-u", Arg.Set si, help_u);
276       ("-x", Arg.Set export, help_x)
277    ] read_file help;
278 with BagT.TypeError msg -> bag_error "Type Error" msg