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