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