]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/toplevel/top.ml
- helena: the improved attribute system allows to export the sorts of Pi's
[helm.git] / helm / software / helena / src / toplevel / top.ml
index 72ab254cd9193f5aff0981164e7bfe4cefab6ef1..535450c1f155d7b6a2a556818833eaa6e77c0d10 100644 (file)
 
 module F  = Filename
 module P  = Printf
+
 module U  = NUri
 module C  = Cps
 module L  = Log
 module Y  = Time
 module G  = Options
 module H  = Hierarchy
-module O  = Output
 module E  = Entity
+module O  = Output
 module S  = Status
 module DO = CrgOutput
 module TD = TxtCrg
@@ -31,6 +32,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 +47,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 +67,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,19 +87,17 @@ 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 =
    let f _ na u =
       let s = U.string_of_uri u in
-      let err () = L.warn 2 (P.sprintf "<%s>" s) in
-      let f i = L.warn 2 (P.sprintf "[%u] <%s>" i s) in
-      E.apix err f na
+      L.warn 2 (P.sprintf "[%u] <%s>" na.E.n_apix s)
    in
    match e with
       | CrgEntity e -> E.common f e
@@ -107,14 +109,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 +124,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 +219,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 +228,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 +280,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 +291,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;
@@ -295,9 +303,6 @@ let main =
    let set_preprocess () = 
       if !G.trace >= 2 then begin preprocess := true; G.summary := true end 
    in 
-   let set_stage i = G.stage := i in
-   let set_xdir s = G.xdir := s in
-   let set_root s = root := s in
    let clear_options () =
       export := false; preprocess := false;
       root := "";
@@ -313,8 +318,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,24 +337,27 @@ 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" ^
-      "Trace levels: 0 just errors (default), 1 time stamps, 2 processed files, \
-       3 typing information, 4 reduction information\n\n" ^
+      "Usage: helena [ -LPVXdgilopqtx1 | -Ts <number> | -MO <dir> | -m <file> | -ahkr <string> ]* [ <file> ]*\n\n" ^
+      "Trace levels: 0 just errors (default), 1 time stamps, 2 processed files, 3 typing information,\n" ^
+      "              4 conversion information, 5 reduction information, 6 level disambiguation\n\n" ^
       "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
    in
    let help_L = " show lexer debug information" in 
+   let help_M = "<dir>  set location of output directory (Grafite) to <dir> (default: current directory)" in
    let help_O = "<dir>  set location of output directory (XML) to <dir> (default: current directory)" in
    let help_P = " show parser debug information" in 
    let help_T = "<number>  set trace level (see above)" in
    let help_V = " show version information" in
    let help_X = " clear options" in
    
+   let help_a = "<string>  set prefix of numeric identifiers (default: empty)" in
    let help_d = " show summary information (requires trace >= 2)" in
    let help_g = " always expand global definitions" in
    let help_h = "<string>  set type hierarchy (default: \"Z1\")" in
    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>  export kernel entities (Grafite) setting location of 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
@@ -357,23 +369,26 @@ let main =
    let help_1 = " parse files with streaming policy" in
    at_exit exit;
    Arg.parse [
-      ("-L", Arg.Set G.debug_lexer, help_L); 
-      ("-O", Arg.String set_xdir, help_O);       
-      ("-P", Arg.Set G.debug_parser, help_P); 
+      ("-L", Arg.Set G.debug_lexer, help_L);
+      ("-M", Arg.String ((:=) G.ma_dir), help_M);
+      ("-O", Arg.String ((:=) G.xdir), help_O);
+      ("-P", Arg.Set G.debug_parser, help_P);
       ("-T", Arg.Int set_trace, help_T);
       ("-V", Arg.Unit print_version, help_V);
       ("-X", Arg.Unit clear_options, help_X);
+      ("-a", Arg.String ((:=) G.alpha), help_a);
       ("-d", Arg.Unit set_summary, help_d);
       ("-g", Arg.Set G.expand, help_g);
       ("-h", Arg.String set_hierarchy, help_h);
       ("-i", Arg.Set G.indexes, help_i);
       ("-k", Arg.String set_kernel, help_k);
       ("-l", Arg.Set G.cc, help_l);
+      ("-m", Arg.String ((:=) G.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);      
-      ("-r", Arg.String set_root, help_r);
-      ("-s", Arg.Int set_stage, help_s);
+      ("-r", Arg.String ((:=) root), help_r);
+      ("-s", Arg.Int ((:=) G.stage), help_s);
       ("-t", Arg.Clear version, help_t);      
       ("-x", Arg.Set export, help_x);
       ("-1", Arg.Set streaming, help_1);