]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/toplevel/top.ml
when sort inclusion is enabled, we can produce conversion constraints in xml
[helm.git] / helm / software / lambda-delta / src / 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 F  = Filename
13 module P  = Printf
14 module U  = NUri
15 module C  = Cps
16 module L  = Log
17 module Y  = Time
18 module G  = Options
19 module H  = Hierarchy
20 module O  = Output
21 module E  = Entity
22 module S  = Status
23 module XL = XmlLibrary
24 module XD = XmlCrg
25 module AA = AutProcess
26 module AO = AutOutput
27 module TD = CrgTxt
28 module AD = CrgAut
29 module MA = MetaAut
30 module MO = MetaOutput
31 module MB = MetaBrg
32 module BD = BrgCrg
33 module BO = BrgOutput
34 module BR = BrgReduction
35 module BU = BrgUntrusted
36 module MZ = MetaBag
37 module ZO = BagOutput
38 module ZT = BagType
39 module ZU = BagUntrusted
40
41 type status = {
42    ast : AA.status;
43    dst : AD.status;
44    mst : MA.status;
45    tst : TD.status;
46    ac  : AO.counters;
47    mc  : MO.counters;
48    brgc: BO.counters;
49    bagc: ZO.counters;
50    kst : S.status
51 }
52
53 let flush_all () = L.flush 0; L.flush_err ()
54
55 let bag_error s msg =
56    L.error ZO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
57
58 let brg_error s msg =
59    L.error BR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
60
61 let initial_status () = {
62    ac   = AO.initial_counters;
63    mc   = MO.initial_counters;
64    brgc = BO.initial_counters;
65    bagc = ZO.initial_counters;
66    mst  = MA.initial_status ();
67    dst  = AD.initial_status ();
68    tst  = TD.initial_status ();
69    ast  = AA.initial_status ();
70    kst  = S.initial_status ()
71 }
72
73 let refresh_status st = {st with
74    mst = MA.refresh_status st.mst;
75    dst = AD.refresh_status st.dst;
76    tst = TD.refresh_status st.tst;
77    kst = S.refresh_status st.kst
78 }
79
80 (* kernel related ***********************************************************)
81
82 type kernel_entity = BrgEntity  of Brg.entity
83                    | BagEntity  of Bag.entity
84                    | CrgEntity  of Crg.entity
85                    | MetaEntity of Meta.entity
86
87 let print_counters st = function
88    | G.Brg -> BO.print_counters C.start st.brgc
89    | G.Bag -> ZO.print_counters C.start st.bagc
90    | G.Crg -> ()
91
92 let xlate_entity entity = match !G.kernel, entity with
93    | G.Brg, CrgEntity e  -> 
94       let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
95    | G.Brg, MetaEntity e -> 
96       let f e = (BrgEntity e) in E.xlate f MB.brg_of_meta e
97    | G.Bag, MetaEntity e -> 
98       let f e = (BagEntity e) in E.xlate f MZ.bag_of_meta e  
99    | _, entity         -> entity
100
101 let pp_progress e =
102    let f a u =
103       let s = U.string_of_uri u in
104       let err () = L.warn (P.sprintf "%s" s) in
105       let f i = L.warn (P.sprintf "[%u] %s" i s) in
106       E.mark err f a
107    in
108    match e with
109       | CrgEntity e -> E.common f e
110       | BrgEntity e -> E.common f e
111       | BagEntity e -> E.common f e      
112       | MetaEntity e -> E.common f e
113
114 let count_entity st = function
115    | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e} 
116    | BrgEntity e  -> {st with brgc = BO.count_entity C.start st.brgc e}
117    | BagEntity e  -> {st with bagc = ZO.count_entity C.start st.bagc e}
118    | _            -> st
119
120 let export_entity = function
121    | CrgEntity e  -> XL.export_entity XD.export_term e
122    | BrgEntity e  -> XL.export_entity BO.export_term e
123    | MetaEntity _
124    | BagEntity _  -> ()
125
126 let type_check st k =
127    let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
128    let ok _ _ = st in
129    match k with
130       | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
131       | BagEntity entity -> ZU.type_check ok st.kst entity
132       | CrgEntity _
133       | MetaEntity _     -> st
134
135 (* extended lexer ***********************************************************)
136
137 type 'token lexer = {
138    parse : Lexing.lexbuf -> 'token;
139    mutable tokbuf: 'token option;
140    mutable unget : bool
141 }
142
143 let initial_lexer parse = {
144    parse = parse; tokbuf = None; unget = false
145 }
146
147 let token xl lexbuf = match xl.tokbuf with
148    | Some token when xl.unget ->   
149       xl.unget <- false; token
150    | _                        ->
151       let token = xl.parse lexbuf in
152       xl.tokbuf <- Some token; token
153
154 (* input related ************************************************************)
155
156 type input = Text | Automath
157
158 type input_entity = TxtEntity of Txt.command
159                   | AutEntity of Aut.command
160                   | NoEntity
161
162 let type_of_input name =
163    if F.check_suffix name ".hln" then Text 
164    else if F.check_suffix name ".aut" then 
165       let _ = H.set_sorts 0 ["Set"; "Prop"] in
166       assert (H.set_graph "Z2");
167       Automath
168    else begin
169       L.warn (P.sprintf "Unknown file type: %s" name); exit 2
170    end
171
172 let txt_xl = initial_lexer TxtLexer.token 
173
174 let aut_xl = initial_lexer AutLexer.token 
175
176 let parbuf = ref [] (* parser buffer *)
177
178 let gen_text command = 
179    parbuf := TxtEntity command :: !parbuf
180
181 let entity_of_input lexbuf i = match i, !parbuf with
182    | Automath, _    -> 
183       begin match AutParser.entry (token aut_xl) lexbuf with
184          | Some e -> aut_xl.unget <- true; AutEntity e
185          | None   -> NoEntity
186       end     
187    | Text, []       -> 
188       begin match TxtParser.entry (token txt_xl) lexbuf with
189          | Some e -> txt_xl.unget <- true; TxtEntity e
190          | None   -> NoEntity
191       end
192    | Text, hd :: tl ->
193       parbuf := tl; hd
194
195 let process_input f st = function 
196    | AutEntity e     ->
197       let f ast e = f {st with ast = ast} (AutEntity e) in
198       AA.process_command f st.ast e
199    | xe              -> f st xe
200
201 let count_input st = function
202    | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
203    | xe          -> st
204
205 (****************************************************************************)
206
207 let stage = ref 3
208 let progress = ref false
209 let preprocess = ref false
210 let root = ref ""
211 let export = ref false
212 let old = ref false
213 let st = ref (initial_status ())
214 let streaming = ref false (* parsing style (temporary) *)
215
216 let process_2 st entity =
217    let st = if !L.level > 2 then count_entity st entity else st in
218    if !export then export_entity entity;
219    if !stage > 2 then type_check st entity else st
220             
221 let process_1 st entity = 
222    if !progress then pp_progress entity;
223    let st = if !L.level > 2 then count_entity st entity else st in
224    if !export && !stage = 1 then export_entity entity;
225    if !stage > 1 then process_2 st (xlate_entity entity) else st 
226
227 let process_0 st entity = 
228    let f st entity =
229       if !stage = 0 then st else
230       match entity, !old with
231          | AutEntity e, true  ->
232             let frr mst = {st with mst = mst} in
233             let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
234             MA.meta_of_aut frr h st.mst e 
235          | AutEntity e, false -> 
236             let err dst = {st with dst = dst} in
237             let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
238             AD.crg_of_aut err g st.dst e
239          | TxtEntity e, _     -> 
240             let crr tst = {st with tst = tst} in
241             let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
242             TD.crg_of_txt crr d gen_text st.tst e
243          | NoEntity, _        -> assert false
244    in
245    let st = if !L.level > 2 then count_input st entity else st in 
246    if !preprocess then process_input f st entity else f st entity
247
248 let process_nostreaming st lexbuf input =
249    let id x = x in
250    let rec aux1 book = match entity_of_input lexbuf input with
251       | NoEntity -> List.rev book
252       | e        -> aux1 (id e :: book)   
253    in
254    let rec aux2 st = function
255       | []           -> st
256       | entity :: tl -> aux2 (process_0 st entity) tl
257    in
258    aux2 st (aux1 [])
259
260 let process_streaming st lexbuf input =
261    let rec aux st = match entity_of_input lexbuf input with
262       | NoEntity -> st
263       | e        -> aux (process_0 st e)
264    in
265    aux st
266
267 (****************************************************************************)
268
269 let process st name =
270    let process = if !streaming then process_streaming else process_nostreaming in
271    let input = type_of_input name in
272    let ich = open_in name in
273    let lexbuf = Lexing.from_channel ich in 
274    let st = process st lexbuf input in
275    close_in ich; 
276    if !stage > 2 && !G.cc && !G.si then XL.export_csys st.kst.S.cc; 
277    st, input
278
279 let main =
280 try 
281    let version_string = "Helena 0.8.1 M - October 2010" in
282    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
283    let set_hierarchy s = 
284       if H.set_graph s then () else 
285          L.warn (P.sprintf "Unknown type hierarchy: %s" s)
286    in
287    let set_kernel = function
288       | "brg" -> G.kernel := G.Brg
289       | "bag" -> G.kernel := G.Bag
290       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
291    in
292    let set_summary i = L.level := i in
293    let set_stage i = stage := i in
294    let set_xdir s = G.xdir := s in
295    let set_root s = root := s in
296    let clear_options () =
297       progress := false; old := false; export := false; preprocess := false;
298       stage := 3; root := "";
299       st := initial_status ();
300       L.clear (); G.clear (); H.clear (); O.clear_reductions ();
301       streaming := false;
302    in
303    let process_file name =
304       if !L.level > 0 then Y.gmtime version_string;      
305       if !L.level > 1 then
306          L.warn (P.sprintf "Processing file: %s" name);
307       if !L.level > 0 then Y.utime_stamp "started";
308       let base_name = Filename.chop_extension (Filename.basename name) in
309       let cover = F.concat !root base_name in
310       if !stage < 2 then G.kernel := G.Crg;
311       G.cover := cover;
312       let sst, input = process (refresh_status !st) name in
313       st := sst;
314       if !L.level > 0 then Y.utime_stamp "processed";
315       if !L.level > 2 then begin
316          AO.print_counters C.start !st.ac;
317          if !preprocess then AO.print_process_counters C.start !st.ast;
318          if !stage > 0 then MO.print_counters C.start !st.mc;
319          if !stage > 1 then print_counters !st !G.kernel;
320          if !stage > 2 then O.print_reductions ()
321       end
322    in
323    let exit () =
324       if !L.level > 0 then Y.utime_stamp "at exit";
325       flush_all ()
326    in
327    let help = 
328       "Usage: helena [ -LPVXcgijopqux1 | -Ss <number> | -O <dir> | -hkr <string> ]* [ <file> ]*\n\n" ^
329       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
330        3 data information, 4 typing information, 5 reduction information\n\n" ^
331        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
332    in
333    let help_L = " show lexer debug information" in 
334    let help_O = "<dir>  set location of output directory (XML) to <dir> (default: current directory)" in
335    let help_P = " show parser debug information" in 
336    let help_S = "<number>  set summary level (see above)" in
337    let help_V = " show version information" in
338    let help_X = " clear options" in
339    
340    let help_c = " read/write conversion constraints" in
341    let help_g = " always expand global definitions" in
342    let help_h = "<string>  set type hierarchy (default: \"Z1\")" in
343    let help_i = " show local references by index" in
344    let help_j = " show URI of processed kernel objects" in
345    let help_k = "<string>  set kernel version (default: \"brg\")" in
346    let help_o = " use old abstract language instead of crg" in   
347    let help_p = " preprocess source" in
348    let help_q = " disable quotation of identifiers" in
349    let help_r = "<string>  set initial segment of URI hierarchy (default: empty)" in
350    let help_s = "<number>  set translation stage (see above)" in
351    let help_u = " activate sort inclusion" in
352    let help_x = " export kernel entities (XML)" in
353    
354    let help_1 = " parse files with streaming policy" in
355    L.box 0; L.box_err ();
356    at_exit exit;
357    Arg.parse [
358       ("-L", Arg.Set G.debug_lexer, help_L); 
359       ("-O", Arg.String set_xdir, help_O);       
360       ("-P", Arg.Set G.debug_parser, help_P); 
361       ("-S", Arg.Int set_summary, help_S);
362       ("-V", Arg.Unit print_version, help_V);
363       ("-X", Arg.Unit clear_options, help_X);
364       ("-c", Arg.Set G.cc, help_c);
365       ("-g", Arg.Set G.expand, help_g);
366       ("-h", Arg.String set_hierarchy, help_h);
367       ("-i", Arg.Set G.indexes, help_i);
368       ("-j", Arg.Set progress, help_j);      
369       ("-k", Arg.String set_kernel, help_k);
370       ("-o", Arg.Set old, help_o);      
371       ("-p", Arg.Set preprocess, help_p);
372       ("-q", Arg.Set G.unquote, help_q);      
373       ("-r", Arg.String set_root, help_r);
374       ("-s", Arg.Int set_stage, help_s);
375       ("-u", Arg.Set G.si, help_u);
376       ("-x", Arg.Set export, help_x);
377       ("-1", Arg.Set streaming, help_1);      
378    ] process_file help;
379 with ZT.TypeError msg -> bag_error "Type Error" msg