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