]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/top.ml
new kernel basic_rg: implements ufficial lambda-delta with de Bruijn indexes
[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 H    = Hierarchy
17 module AO   = AutOutput
18 module MA   = MetaAut
19 module MO   = MetaOutput
20 module MBrg = MetaBrg
21 module BrgO = BrgOutput
22 module BrgT = BrgType
23 module BrgU = BrgUntrusted
24 module MBag = MetaBag
25 module BagO = BagOutput
26 module BagR = BagReduction
27 module BagU = BagUntrusted
28
29 type status = {
30    mst : MA.status;
31    ac  : AO.counters;
32    mc  : MO.counters;
33    brgc: BrgO.counters;
34    bagc: BagO.counters
35 }
36
37 let initial_status = {
38    ac   = AO.initial_counters;
39    mc   = MO.initial_counters;
40    brgc = BrgO.initial_counters;
41    bagc = BagO.initial_counters;
42    mst  = MA.initial_status
43 }
44
45 let count count_fun c item =
46    if !L.level > 2 then count_fun C.start c item else c
47
48 let flush () = L.flush 0; L.flush_err ()
49
50 let bag_error s msg =
51    L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush () 
52
53 let brg_error s msg =
54    L.error BrgO.specs (L.Warn s :: L.Loc :: msg); flush () 
55
56 (* kernel related ***********************************************************)
57
58 type kernel = Brg | Bag
59
60 type kernel_item = BrgItem of Brg.item
61                  | BagItem of Bag.item
62
63 let kernel = ref Brg
64
65 let print_counters st = match !kernel with
66    | Brg -> BrgO.print_counters C.start st.brgc
67    | Bag -> BagO.print_counters C.start st.bagc
68
69 let kernel_of_meta f st item = match !kernel with
70    | Brg -> 
71       let f item = f st (BrgItem item) in
72       MBrg.brg_of_meta f item
73    | Bag -> 
74       let f item = f st (BagItem item) in
75       MBag.bag_of_meta f item  
76
77 let count_item st = function
78    | BrgItem item -> {st with brgc = count BrgO.count_item st.brgc item}
79    | BagItem item -> {st with bagc = count BagO.count_item st.bagc item}
80
81 let type_check f st g = function
82    | BrgItem item -> 
83       let f _ = function
84          | None -> f st None
85          | Some (i, u, _) -> f st (Some (i, u))
86       in
87       BrgU.type_check f g item
88    | BagItem item -> 
89       let f _ = function
90          | None -> f st None
91          | Some (i, u, _) -> f st (Some (i, u))
92       in
93       BagU.type_check f g item
94
95 let indexes () = match !kernel with
96    | Brg -> BrgO.indexes := true
97    | Bag -> BagO.indexes := true
98
99 let si () = match !kernel with
100    | Brg -> BrgT.si := true
101    | Bag -> BagR.nsi := true
102
103 (****************************************************************************)
104
105 let main =
106 try 
107    let version_string = "Helena 0.8.0 M - June 2009" in
108    let stage = ref 3 in
109    let meta_file = ref None in
110    let set_hierarchy s = 
111       let f = function
112          | Some g -> H.graph := g
113          | None   -> L.warn (P.sprintf "Unknown type hierarchy: %s" s)
114       in
115       H.graph_of_string f s
116    in
117    let set_kernel = function
118       | "brg" -> kernel := Brg
119       | "bag" -> kernel := Bag
120       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
121    in
122    let set_summary i = L.level := i in
123    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
124    let set_stage i = stage := i in
125    let close = function
126       | None          -> ()
127       | Some (och, _) -> close_out och
128    in
129    let set_meta_file name =
130       close !meta_file;
131       let och = open_out name in
132       let frm = Format.formatter_of_out_channel och in
133       Format.pp_set_margin frm max_int;
134       meta_file := Some (och, frm)
135    in
136    let read_file name =
137       if !L.level > 0 then Time.gmtime version_string;      
138       if !L.level > 1 then
139          L.warn (P.sprintf "Processing file: %s" name);
140       if !L.level > 0 then Time.utime_stamp "started";
141       let ich = open_in name in
142       let lexbuf = Lexing.from_channel ich in
143       let book = AutParser.book AutLexer.token lexbuf in
144       close_in ich;
145       if !L.level > 0 then Time.utime_stamp "parsed";
146       let rec aux st = function
147          | []         -> st
148          | item :: tl ->
149 (* stage 3 *)
150             let f st = function
151                | None        -> st
152                | Some (i, u) -> 
153                   Log.warn (P.sprintf "[%u] %s" i (U.string_of_uri u)); 
154                   st
155             in
156 (* stage 2 *)       
157             let f st item =
158                let st = count_item st item in
159                if !stage > 2 then type_check f st !H.graph item else st
160             in
161 (* stage 1 *)       
162             let f st mst item = 
163                let st = {st with
164                   mst = mst; mc = count MO.count_item st.mc item
165                } in
166                begin match !meta_file with
167                   | None          -> ()
168                   | Some (_, frm) -> MO.pp_item C.start frm item
169                end;
170                if !stage > 1 then kernel_of_meta f st item else st 
171             in  
172 (* stage 0 *)       
173             let st = {st with ac = count AO.count_item st.ac item} in
174             let st =
175                if !stage > 0 then MA.meta_of_aut (f st) st.mst item else st
176             in
177             aux st tl
178       in 
179       let st = aux initial_status book in
180       if !L.level > 0 then Time.utime_stamp "processed";
181       if !L.level > 2 then AO.print_counters C.start st.ac;
182       if !L.level > 2 && !stage > 0 then MO.print_counters C.start st.mc;
183       if !L.level > 2 && !stage > 1 then print_counters st
184    in
185    let help = 
186       "Usage: helena [ -Viu | -Ss <number> | -m <file> | -hk <string> ] <file> ...\n\n" ^
187       "Summary levels: 0 just errors, 1 time stamps, 2 processed file names, \
188        3 data information, 4 typing information, 5 reduction information\n\n" ^
189        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted\n"
190    in
191    let help_S = "<number>  set summary level" in
192    let help_V = " show version information" in   
193    let help_h = "<string>  set type hierarchy" in
194    let help_i = " show local references by index" in
195    let help_k = "<string>  set kernel version" in
196    let help_m = "<file>  output intermediate representation" in
197    let help_u = " activate sort inclusion" in
198    let help_s = "<number>  Set translation stage" in
199    L.box 0; L.box_err ();
200    H.set_new_sorts ignore ["Set"; "Prop"];
201    Arg.parse [
202       ("-S", Arg.Int set_summary, help_S);
203       ("-V", Arg.Unit print_version, help_V);
204       ("-h", Arg.String set_hierarchy, help_h);
205       ("-i", Arg.Unit indexes, help_i);
206       ("-k", Arg.String set_kernel, help_k);
207       ("-m", Arg.String set_meta_file, help_m); 
208       ("-u", Arg.Unit si, help_u);
209       ("-s", Arg.Int set_stage, help_s)
210    ] read_file help;
211    if !L.level > 0 then Time.utime_stamp "at exit";
212    flush ()
213 with BagType.TypeError msg -> bag_error "Type Error" msg
214    | BrgT.TypeError msg -> brg_error "Type Error" msg