]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/toplevel/top.ml
- the disambiguation of unified binders continues
[helm.git] / helm / software / helena / src / toplevel / top.ml
index 72ab254cd9193f5aff0981164e7bfe4cefab6ef1..d91ba1724180ce2a448b3d5db5d0d9aaf0839730 100644 (file)
@@ -31,6 +31,7 @@ module BD = BrgCrg
 module BO = BrgOutput
 module BR = BrgReduction
 module BU = BrgUntrusted
+module BG = BrgGrafite
 module ZD = BagCrg
 module ZO = BagOutput
 module ZT = BagType
@@ -45,15 +46,16 @@ type status = {
    dc : DO.counters;
    bc : BO.counters;
    zc : ZO.counters;
+   och: out_channel option;
 }
 
 let level = 0
 
-let bag_error s msg =
-   L.error ZO.specs (L.Warn s :: msg) 
+let bag_error st s msg =
+   L.error st.kst.S.lenv ZO.specs (L.Warn s :: msg) 
 
-let brg_error s msg =
-   L.error BR.specs (L.Warn s :: msg)
+let brg_error st s msg =
+   L.error st.kst.S.lenv BR.specs (L.Warn s :: msg)
 
 let initial_status () = {
    kst = S.initial_status ();
@@ -64,6 +66,7 @@ let initial_status () = {
    dc  = DO.initial_counters;
    bc  = BO.initial_counters;
    zc  = ZO.initial_counters;
+   och = None;
 }
 
 let refresh_status st = {st with
@@ -83,11 +86,11 @@ let print_counters st = function
    | G.Brg -> BO.print_counters C.start st.bc
    | G.Bag -> ZO.print_counters C.start st.zc
 
-let xlate_entity entity = match !G.kernel, entity with
+let xlate_entity st entity = match !G.kernel, entity with
    | G.Brg, CrgEntity e -> 
       let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
    | G.Bag, CrgEntity e -> 
-      let f e = (BagEntity e) in E.xlate f ZD.bag_of_crg e
+      let f e = (BagEntity e) in E.xlate f (ZD.bag_of_crg st.kst.S.lenv) e
    | _, entity          -> entity
 
 let pp_progress e =
@@ -107,14 +110,14 @@ let count_entity st = function
    | BagEntity e -> {st with zc = ZO.count_entity C.start st.zc e}
    | CrgEntity e -> {st with dc = DO.count_entity C.start st.dc e}
 
-let export_entity = function
-   | CrgEntity e -> XL.export_entity XD.export_term e
-   | BrgEntity e -> XL.export_entity BO.export_term e
-   | BagEntity e -> XL.export_entity ZO.export_term e
+let export_entity st = function
+   | CrgEntity e -> XL.export_entity (XD.export_term st.kst.S.lenv) e
+   | BrgEntity e -> XL.export_entity (BO.export_term st.kst.S.lenv) e
+   | BagEntity e -> XL.export_entity (ZO.export_term st.kst.S.lenv) e
 
 let type_check st k =
-   let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
-   let bag_err msg = bag_error "Type Error" msg; failwith "Interrupted" in
+   let brg_err msg = brg_error st "Type Error" msg; failwith "Interrupted" in
+   let bag_err msg = bag_error st "Type Error" msg; failwith "Interrupted" in
    let ok _ _ = st in
    match k with
       | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
@@ -122,10 +125,17 @@ let type_check st k =
       | CrgEntity _      -> st
 
 let validate st k =
-   let brg_err msg = brg_error "Validation Error" msg; failwith "Interrupted" in
+   let brg_err msg = brg_error st "Validation Error" msg; failwith "Interrupted" in
    let ok _ = st in
    match k with
-      | BrgEntity entity -> BU.validate brg_err ok st.kst entity
+      | BrgEntity entity -> 
+         let st = BU.validate brg_err ok st.kst entity in
+         begin match st.och with
+            | None     -> st
+            | Some och -> 
+               if BG.output_entity st.kst.S.lenv och entity then st
+               else begin L.warn level "Matita exportation stopped"; {st with och = None} end
+         end
       | BagEntity _      -> st
       | CrgEntity _      -> st
 
@@ -210,7 +220,7 @@ let streaming = ref false (* parsing style (temporary) *)
 
 let process_2 st entity =
    let st = if !G.summary then count_entity st entity else st in
-   if !export then export_entity entity;
+   if !export then export_entity st entity;
    if !G.stage >= 3 then 
       let f = if !version then validate else type_check in 
       f st entity
@@ -219,8 +229,8 @@ let process_2 st entity =
 let process_1 st entity = 
    if !G.trace >= 3 then pp_progress entity;
    let st = if !G.summary then count_entity st entity else st in
-   if !export && !G.stage = 1 then export_entity entity;
-   if !G.stage >= 2 then process_2 st (xlate_entity entity) else st 
+   if !export && !G.stage = 1 then export_entity st entity;
+   if !G.stage >= 2 then process_2 st (xlate_entity st entity) else st 
 
 let process_0 st entity = 
    let f st entity =
@@ -271,8 +281,7 @@ let process st name =
    st, input
 
 let main = 
-   let version_string = "Helena 0.8.2 M - November 2014" in
-   let print_version () = L.warn level (version_string ^ "\n"); exit 0 in
+   let print_version () = L.warn level (G.version_string ^ "\n"); exit 0 in
    let set_hierarchy s = 
       if H.set_graph s then () else 
          L.warn level (P.sprintf "Unknown type hierarchy: %s" s)
@@ -283,7 +292,7 @@ let main =
       | s     -> L.warn level (P.sprintf "Unknown kernel version: %s" s)
    in
    let set_trace i = 
-      if !G.trace = 0 && i > 0 then Y.gmtime version_string;
+      if !G.trace = 0 && i > 0 then Y.gmtime G.version_string;
       if !G.trace > 0 && i = 0 then Y.utime_stamp "at exit";
       G.trace := i;
       if i <= 1 then G.summary := false;
@@ -306,6 +315,7 @@ let main =
       streaming := false;
       version := true
    in
+   let set_ma_preamble fname = G.ma_preamble := fname in
    let process_file name =
       if !G.trace >= 2 then L.warn 1 (P.sprintf "Processing file: %s" name);
       if !G.trace >= 2 then Y.utime_stamp "started";
@@ -313,8 +323,12 @@ let main =
       let cover = F.concat !root base_name in
       if !G.stage <= 1 then G.kernel := G.Crg;
       G.cover := cover;
+      if !G.ma_preamble <> "" then st := {!st with och = Some (BG.open_out base_name)};
       let sst, input = process (refresh_status !st) name in
-      st := sst;
+      st := begin match sst.och with 
+         | None     -> sst
+         | Some och -> BG.close_out och; {sst with och = None}
+      end;
       if !G.trace >= 2 then Y.utime_stamp "processed";
       if !G.summary then begin
          AO.print_counters C.start !st.ac;
@@ -328,7 +342,7 @@ let main =
       if !G.trace >= 1 then Y.utime_stamp "at exit"
    in
    let help = 
-      "Usage: helena [ -LPVXdgilopqtx1 | -Ts <number> | -O <dir> | -hkr <string> ]* [ <file> ]*\n\n" ^
+      "Usage: helena [ -LPVXdgilopqtx1 | -Ts <number> | -O <dir> | -m <file> | -hkr <string> ]* [ <file> ]*\n\n" ^
       "Trace levels: 0 just errors (default), 1 time stamps, 2 processed files, \
        3 typing information, 4 reduction information\n\n" ^
       "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
@@ -346,6 +360,7 @@ let main =
    let help_i = " show local references by index" in
    let help_k = "<string>  set kernel version (default: \"brg\")" in
    let help_l = " disambiguate binders level (Automath)" in
+   let help_m = "<file>  set location of Matita preamble to <file> (default: empty)" in   
    let help_o = " activate sort inclusion (default: false)" in
    let help_p = " preprocess source (Automath)" in
    let help_q = " disable quotation of identifiers" in
@@ -369,6 +384,7 @@ let main =
       ("-i", Arg.Set G.indexes, help_i);
       ("-k", Arg.String set_kernel, help_k);
       ("-l", Arg.Set G.cc, help_l);
+      ("-m", Arg.String set_ma_preamble, help_m);      
       ("-o", Arg.Set G.si, help_o);
       ("-p", Arg.Unit set_preprocess, help_p);
       ("-q", Arg.Set G.unquote, help_q);