]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/toplevel/top.ml
- bugfix is refreshed state of AutCrg: now we return a fresh state
[helm.git] / helm / software / helena / src / toplevel / top.ml
index d0476f78ac85e9d2cf7144a0112dfe932ec5ff17..66baa8658c49a6fe6cbe3feebebd4a14517521d4 100644 (file)
@@ -9,17 +9,19 @@
      \ /   This software is distributed as is, NO WARRANTY.              
       V_______________________________________________________________ *)
 
-module F  = Filename
-module P  = Printf
+module KF = Filename
+module KP = Printf
+
 module U  = NUri
 module C  = Cps
 module L  = Log
 module Y  = Time
+module P  = Marks
 module G  = Options
 module H  = Hierarchy
-module O  = Output
+module N  = Layer
 module E  = Entity
-module S  = Status
+module O  = Output
 module DO = CrgOutput
 module TD = TxtCrg
 module AA = AutProcess
@@ -31,13 +33,14 @@ module BD = BrgCrg
 module BO = BrgOutput
 module BR = BrgReduction
 module BU = BrgUntrusted
+module BG = BrgGrafite
 module ZD = BagCrg
 module ZO = BagOutput
 module ZT = BagType
 module ZU = BagUntrusted
 
 type status = {
-   kst: S.status;
+   kst: N.status;
    tst: TD.status;
    pst: AA.status;
    ast: AD.status;
@@ -45,18 +48,19 @@ type status = {
    dc : DO.counters;
    bc : BO.counters;
    zc : ZO.counters;
+   och: out_channel option;
 }
 
-let flush_all () = L.flush 0; L.flush_err ()
+let level = 0
 
-let bag_error s msg =
-   L.error ZO.specs (L.Warn s :: L.Loc :: msg); flush_all (
+let bag_error st s msg =
+   L.error st.kst ZO.specs (L.Warn s :: msg
 
-let brg_error s msg =
-   L.error BR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
+let brg_error st s msg =
+   L.error st.kst BR.specs (L.Warn s :: msg)
 
 let initial_status () = {
-   kst = S.initial_status ();
+   kst = N.initial_status ();
    tst = TD.initial_status ();
    pst = AA.initial_status ();
    ast = AD.initial_status ();
@@ -64,10 +68,11 @@ let initial_status () = {
    dc  = DO.initial_counters;
    bc  = BO.initial_counters;
    zc  = ZO.initial_counters;
+   och = None;
 }
 
 let refresh_status st = {st with
-   kst = S.refresh_status st.kst;
+   kst = N.refresh_status st.kst;
    tst = TD.refresh_status st.tst;
    ast = AD.refresh_status st.ast;
 }
@@ -79,24 +84,23 @@ type kernel_entity = BrgEntity of Brg.entity
                   | CrgEntity of Crg.entity
 
 let print_counters st = function
-   | G.Crg -> DO.print_counters C.start st.dc
-   | G.Brg -> BO.print_counters C.start st.bc
-   | G.Bag -> ZO.print_counters C.start st.zc
+   | G.V4 -> DO.print_counters C.start st.dc
+   | G.V3 -> BO.print_counters C.start st.bc
+   | G.V0 -> ZO.print_counters C.start st.zc
 
-let xlate_entity entity = match !G.kernel, entity with
-   | G.Brg, CrgEntity e -> 
+let xlate_entity st entity = match !G.kernel, entity with
+   | G.V3, 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
+   | G.V0, CrgEntity e -> 
+      let f e = (BagEntity e) in E.xlate f (ZD.bag_of_crg st.kst) e
    | _, entity          -> entity
 
 let pp_progress e =
-   let f a u =
+   let f _ na u =
       let s = U.string_of_uri u in
-      let err () = L.warn (P.sprintf "%s" s) in
-      let f i = L.warn (P.sprintf "[%u] %s" i s) in
-      E.mark err f a
+      L.warn 2 (KP.sprintf "[%u] <%s>" na.E.n_apix s);
    in
+   Y.utime_stamp "intermediate";
    match e with
       | CrgEntity e -> E.common f e
       | BrgEntity e -> E.common f e
@@ -107,19 +111,35 @@ 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) e
+   | BrgEntity e -> XL.export_entity (BO.export_term st.kst) e
+   | BagEntity e -> XL.export_entity (ZO.export_term st.kst) e
 
 let type_check st k =
-   let brg_err msg = brg_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
-      | BagEntity entity -> ZU.type_check ok st.kst entity
+      | BagEntity entity -> ZU.type_check bag_err ok st.kst entity
+      | CrgEntity _      -> st
+
+let validate st k =
+   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
+      | BagEntity _      -> st
       | CrgEntity _      -> st
 
+let grafite st och = function
+   | BrgEntity entity -> 
+      if BG.output_entity st.kst och entity then st else
+      begin L.warn level "Matita exportation stopped"; {st with och = None} end
+   | BagEntity _      -> st
+   | CrgEntity _      -> st
+
 (* extended lexer ***********************************************************)
 
 type 'token lexer = {
@@ -148,13 +168,13 @@ type input_entity = TxtEntity of Txt.command
                  | NoEntity
 
 let type_of_input name =
-   if F.check_suffix name ".hln" then Text 
-   else if F.check_suffix name ".aut" then 
+   if KF.check_suffix name ".hln" then Text 
+   else if KF.check_suffix name ".aut" then 
       let _ = H.set_sorts 0 ["Set"; "Prop"] in
       assert (H.set_graph "Z2");
       Automath
    else begin
-      L.warn (P.sprintf "Unknown file type: %s" name); exit 2
+      L.warn level (KP.sprintf "Unknown file type: %s" name); exit 2
    end
 
 let txt_xl = initial_lexer TxtLexer.token 
@@ -192,8 +212,7 @@ let count_input st = function
 
 (****************************************************************************)
 
-let stage = ref 3
-let progress = ref false
+let version = ref true
 let preprocess = ref false
 let root = ref ""
 let export = ref false
@@ -201,31 +220,38 @@ let st = ref (initial_status ())
 let streaming = ref false (* parsing style (temporary) *)
 
 let process_2 st entity =
-   let st = if !L.level > 2 then count_entity st entity else st in
-   if !export then export_entity entity;
-   if !stage > 2 then type_check st entity else st
-           
+   let st = if !G.summary then count_entity st entity else st in
+   let st = 
+      if !G.stage >= 3 then 
+         let f = if !version then validate else type_check in f st entity
+      else st
+   in
+   if !export then export_entity st entity;
+   match st.och with
+     | None     -> st
+     | Some och -> grafite st och entity
+
 let process_1 st entity = 
-   if !progress then pp_progress entity;
-   let st = if !L.level > 2 then count_entity st entity else st in
-   if !export && !stage = 1 then export_entity entity;
-   if !stage > 1 then process_2 st (xlate_entity entity) else st 
+   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 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 =
-      if !stage = 0 then st else
+      if !G.stage = 0 then st else
       match entity with
          | AutEntity e -> 
             let err ast = {st with ast = ast} in
             let g ast e = process_1 {st with ast = ast} (CrgEntity e) in
-           AD.crg_of_aut err g st.ast e
+           AD.crg_of_aut err g st.kst st.ast e
          | TxtEntity e -> 
             let crr tst = {st with tst = tst} in
             let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
            TD.crg_of_txt crr d gen_text st.tst e
         | NoEntity    -> assert false
    in
-   let st = if !L.level > 2 then count_input st entity else st in 
+   let st = if !G.summary then count_input st entity else st in 
    if !preprocess then process_input f st entity else f st entity
 
 let process_nostreaming st lexbuf input =
@@ -255,106 +281,121 @@ let process st name =
    let ich = open_in name in
    let lexbuf = Lexing.from_channel ich in 
    let st = process st lexbuf input in
-   close_in ich; 
-   if !stage > 2 && !G.cc && !G.si then XL.export_csys st.kst.S.cc; 
+   close_in ich;
    st, input
 
-let main =
-try 
-   let version_string = "Helena 0.8.2 M - January 2011" in
-   let print_version () = L.warn (version_string ^ "\n"); exit 0 in
+let main = 
+   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 (P.sprintf "Unknown type hierarchy: %s" s)
+         L.warn level (KP.sprintf "Unknown type hierarchy: %s" s)
    in
    let set_kernel = function
-      | "brg" -> G.kernel := G.Brg
-      | "bag" -> G.kernel := G.Bag
-      | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
+      | "V3" -> G.kernel := G.V3
+      | "V0" -> G.kernel := G.V0
+      | s    -> L.warn level (KP.sprintf "Unknown kernel version: %s" s)
+   in
+   let set_trace i = 
+      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;
+      if i <= 1 then preprocess := false
    in
-   let set_summary i = L.level := i in
-   let set_stage i = stage := i in
-   let set_xdir s = G.xdir := s in
-   let set_root s = root := s in
+   let set_summary () = 
+      if !G.trace >= 2 then G.summary := true
+   in 
+   let set_preprocess () = 
+      if !G.trace >= 2 then begin preprocess := true; G.summary := true end 
+   in 
    let clear_options () =
-      progress := false; export := false; preprocess := false;
-      stage := 3; root := "";
-      st := initial_status ();
-      L.clear (); G.clear (); H.clear (); O.clear_reductions ();
+      export := false; preprocess := false;
+      root := "";
+      G.clear (); H.clear (); O.clear_reductions ();
       streaming := false;
+      version := true
    in
    let process_file name =
-      if !L.level > 0 then Y.gmtime version_string;      
-      if !L.level > 1 then
-         L.warn (P.sprintf "Processing file: %s" name);
-      if !L.level > 0 then Y.utime_stamp "started";
+      if !G.trace >= 2 then L.warn 1 (KP.sprintf "Processing file: %s" name);
+      if !G.trace >= 2 then Y.utime_stamp "started";
       let base_name = Filename.chop_extension (Filename.basename name) in
-      let cover = F.concat !root base_name in
-      if !stage < 2 then G.kernel := G.Crg;
+      let cover = KF.concat !root base_name in
+      if !G.stage <= 1 then G.kernel := G.V4;
       G.cover := cover;
+      if !G.ma_preamble <> "" then st := {!st with och = Some (BG.open_out base_name)};
+      P.clear_marks ();
       let sst, input = process (refresh_status !st) name in
-      st := sst;
-      if !L.level > 0 then Y.utime_stamp "processed";
-      if !L.level > 2 then begin
+      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;
          if !preprocess then AO.print_process_counters C.start !st.pst;
-         if !stage > 0 then print_counters !st G.Crg;
-         if !stage > 1 then print_counters !st !G.kernel;
-         if !stage > 2 then O.print_reductions ()
+         if !G.stage >= 1 then print_counters !st G.V4;
+         if !G.stage >= 2 then print_counters !st !G.kernel;
+         if !G.stage >= 3 then O.print_reductions ()
       end
    in
    let exit () =
-      if !L.level > 0 then Y.utime_stamp "at exit";
-      flush_all ()
+      if !G.trace >= 1 then Y.utime_stamp "at exit"
    in
    let help = 
-      "Usage: helena [ -LPVXcgijopqx1 | -Ss <number> | -O <dir> | -hkr <string> ]* [ <file> ]*\n\n" ^
-      "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
-       3 data information, 4 typing information, 5 reduction information\n\n" ^
-       "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\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 processed objects,\n" ^
+      "              4 typing information, 5 conversion information, 6 reduction information,\n" ^
+      "              7 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_S = "<number>  set summary level (see above)" 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_c = " read/write conversion constraints" 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_j = " show URI of processed kernel objects" in
-   let help_k = "<string>  set kernel version (default: \"brg\")" in
-   let help_o = " activate sort inclusion" in
-   let help_p = " preprocess source" in
+   let help_k = "<string>  set kernel version (default: \"V3\")" 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
    let help_r = "<string>  set initial segment of URI hierarchy (default: empty)" in
    let help_s = "<number>  set translation stage (see above)" in
+   let help_t = " type check [version 1]" in
    let help_x = " export kernel entities (XML)" in
    
    let help_1 = " parse files with streaming policy" in
-   L.box 0; L.box_err ();
    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); 
-      ("-S", Arg.Int set_summary, help_S);
+      ("-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);
-      ("-c", Arg.Set G.cc, help_c);
+      ("-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);
-      ("-j", Arg.Set progress, help_j);      
       ("-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.Set preprocess, help_p);
+      ("-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);      
-   ] process_file help;
-with ZT.TypeError msg -> bag_error "Type Error" msg
+   ] process_file help