]> matita.cs.unibo.it Git - helm.git/blobdiff - matita/matita/matitaEngine.ml
BIG BUG FIXED (???): in place of using Grammar.Entry.parse we should have
[helm.git] / matita / matita / matitaEngine.ml
index c13004c24917f51fcbe1609607656636e745ed9d..4429f963651f7e8fa9839626fb8aa955b119d0ec 100644 (file)
 (* $Id$ *)
 
 module G = GrafiteAst
+open GrafiteTypes
+open Printf
+
+exception TryingToAdd of string Lazy.t
+exception EnrichedWithStatus of exn * GrafiteTypes.status
+exception AlreadyLoaded of string Lazy.t
+exception FailureCompiling of string * exn
+exception CircularDependency of string
 
 let debug = false ;;
 let debug_print = if debug then prerr_endline else ignore ;;
 
-let disambiguate_tactic text prefix_len lexicon_status_ref grafite_status goal tac =
- let metasenv,tac =
-  GrafiteDisambiguate.disambiguate_tactic
-   lexicon_status_ref
-   (GrafiteTypes.get_proof_context grafite_status goal)
-   (GrafiteTypes.get_proof_metasenv grafite_status) (Some goal)
-   tac
- in
-  GrafiteTypes.set_metasenv metasenv grafite_status,tac
+let slash_n_RE = Pcre.regexp "\\n" ;;
+
+let pp_ast_statement grafite_status stm =
+  let stm = GrafiteAstPp.pp_statement stm
+    ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
+  in
+  let stm = Pcre.replace ~rex:slash_n_RE stm in
+  let stm =
+      if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
+      else stm
+  in
+    HLog.debug ("Executing: ``" ^ stm ^ "''")
+;;
+
+let clean_exit baseuri exn =
+  LibraryClean.clean_baseuris ~verbose:false [baseuri];
+  raise (FailureCompiling (baseuri,exn))
+;;
+
+let pp_times fname rc big_bang big_bang_u big_bang_s = 
+  if not (Helm_registry.get_bool "matita.verbose") then
+    let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
+    let r = Unix.gettimeofday () -. big_bang in
+    let u = u -. big_bang_u in
+    let s = s -. big_bang_s in
+    let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
+    let rc,rcascii = 
+      if rc then "\e[0;32mOK\e[0m","Ok" else "\e[0;31mFAIL\e[0m","Fail" in
+    let times = 
+      let fmt t = 
+        let seconds = int_of_float t in
+        let cents = int_of_float ((t -. floor t) *. 100.0) in
+        let minutes = seconds / 60 in
+        let seconds = seconds mod 60 in
+        Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
+      in
+      Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
+    in
+    let s = Printf.sprintf "%-4s %s %s" rc times extra in
+    print_endline s;
+    flush stdout;
+    HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
+;;
+
+let cut prefix s = 
+  let lenp = String.length prefix in
+  let lens = String.length s in
+  assert (lens > lenp);
+  assert (String.sub s 0 lenp = prefix);
+  String.sub s lenp (lens-lenp)
+;;
+
+let activate_extraction baseuri fname =
+  ()
+  (* MATITA 1.0
+ if Helm_registry.get_bool "matita.extract" then
+  let mangled_baseuri =
+   let baseuri = String.sub baseuri 5 (String.length baseuri - 5) in
+     let baseuri = Pcre.replace ~pat:"/" ~templ:"_" baseuri in
+      String.uncapitalize baseuri in
+  let f =
+    open_out
+     (Filename.dirname fname ^ "/" ^ mangled_baseuri ^ ".ml") in
+   LibrarySync.add_object_declaration_hook
+    (fun ~add_obj ~add_coercion _ obj ->
+      output_string f (CicExportation.ppobj baseuri obj);
+      flush f; []);
+      *)
+;;
 
-let disambiguate_command lexicon_status_ref grafite_status cmd =
- let baseuri = grafite_status#baseuri in
- let lexicon_status,metasenv,cmd =
-  GrafiteDisambiguate.disambiguate_command ~baseuri
-   !lexicon_status_ref (GrafiteTypes.get_proof_metasenv grafite_status) cmd
- in
-  lexicon_status_ref := lexicon_status;
-  GrafiteTypes.set_metasenv metasenv grafite_status,cmd
-
-let disambiguate_macro lexicon_status_ref grafite_status macro context =
- let metasenv,macro =
-  GrafiteDisambiguate.disambiguate_macro
-   lexicon_status_ref
-   (GrafiteTypes.get_proof_metasenv grafite_status)
-   context macro
- in
-  GrafiteTypes.set_metasenv metasenv grafite_status,macro
 
 let eval_macro_screenshot (status : GrafiteTypes.status) name =
+  assert false (* MATITA 1.0
   let _,_,metasenv,subst,_ = status#obj in
   let sequent = List.hd metasenv in
   let mathml = 
@@ -75,110 +127,202 @@ let eval_macro_screenshot (status : GrafiteTypes.status) name =
     Filename.quote (name^".png")));
   HLog.debug ("generated " ^ name ^ ".png");
   status, `New []
+  *)
 ;;
 
-let eval_ast ?do_heavy_checks status (text,prefix_len,ast) =
- let dump = not (Helm_registry.get_bool "matita.moo") in
- let lexicon_status_ref = ref (status :> LexiconEngine.status) in
+let eval_ast ~include_paths ?do_heavy_checks status (text,prefix_len,ast) =
  let baseuri = status#baseuri in
- let new_status,new_objs =
-  match ast with 
-     | G.Executable (_, G.Command (_, G.Coercion _)) when dump ->
-(* FG: some commands can not be executed when mmas are parsed *************)
-(* To be removed when mmas will be executed                               *)
-        status, `Old []
-     | ast -> 
-  GrafiteEngine.eval_ast
-   ~disambiguate_tactic:(disambiguate_tactic text prefix_len lexicon_status_ref)
-   ~disambiguate_command:(disambiguate_command lexicon_status_ref)
-   ~disambiguate_macro:(disambiguate_macro lexicon_status_ref)
-   ?do_heavy_checks status (text,prefix_len,ast)
- in
- let new_status =
-  if !lexicon_status_ref#lstatus != status#lstatus then
-   new_status#set_lstatus (!lexicon_status_ref#lstatus)
-  else
-   new_status in
- let new_status = LexiconSync.add_aliases_for_objs new_status new_objs in
- let new_aliases = LexiconSync.alias_diff ~from:status new_status in
+ let new_aliases,new_status =
+  GrafiteDisambiguate.eval_with_new_aliases status
+   (fun status ->
+     GrafiteEngine.eval_ast ~include_paths ?do_heavy_checks status
+      (text,prefix_len,ast)) in
  let _,intermediate_states = 
   List.fold_left
    (fun (status,acc) (k,value) -> 
-     let v = LexiconAst.description_of_alias value in
+     let v = GrafiteAst.description_of_alias value in
      let b =
       try
-       (* this hack really sucks! *)
-       UriManager.buri_of_uri (UriManager.uri_of_string v) = baseuri
+       let NReference.Ref (uri,_) = NReference.reference_of_string v in
+        NUri.baseuri_of_uri uri = baseuri
       with
-       UriManager.IllFormedUri _ ->
-        try
-         (* this too! *)
-         let NReference.Ref (uri,_) = NReference.reference_of_string v in
-         let ouri = NCic2OCic.ouri_of_nuri uri in
-          UriManager.buri_of_uri ouri = baseuri
-        with
-         NReference.IllFormedReference _ ->
-          false (* v is a description, not a URI *)
+       NReference.IllFormedReference _ ->
+        false (* v is a description, not a URI *)
      in
       if b then 
        status,acc
       else
-       let new_status =
-        LexiconEngine.set_proof_aliases status [k,value]
+       let status =
+        GrafiteDisambiguate.set_proof_aliases status ~implicit_aliases:false
+         GrafiteAst.WithPreferences [k,value]
        in
-        new_status, (new_status ,Some (k,value))::acc
-   ) (status,[]) new_aliases
+        status, (status ,Some (k,value))::acc
+   ) (status,[]) new_aliases (* WARNING: this must be the old status! *)
  in
-  ((new_status),None)::intermediate_states
+  (new_status,None)::intermediate_states
 ;;
 
-exception TryingToAdd of string
-exception EnrichedWithStatus of exn * GrafiteTypes.status
+let rec get_ast status ~compiling ~include_paths strm = 
+  match GrafiteParser.parse_statement status strm with
+     (GrafiteAst.Executable
+       (_,GrafiteAst.NCommand (_,GrafiteAst.Include (_,_,mafilename)))) as cmd
+     ->
+      let root, buri, _, tgt = 
+        try Librarian.baseuri_of_script ~include_paths mafilename
+        with Librarian.NoRootFor _ -> 
+          HLog.error ("The included file '"^mafilename^"' has no root file,");
+          HLog.error "please create it.";
+          raise (Failure ("No root file for "^mafilename))
+      in
+       ignore (assert_ng ~compiling ~include_paths ~root tgt);
+       cmd
+   | cmd -> cmd
 
-let eval_from_stream ~first_statement_only ~include_paths 
- ?do_heavy_checks ?(enforce_no_new_aliases=true)
- ?(watch_statuses=fun _ -> ()) status str cb 
-=
+and eval_from_stream ~compiling ~include_paths ?do_heavy_checks status str cb =
  let matita_debug = Helm_registry.get_bool "matita.debug" in
- let rec loop status statuses =
-  let loop =
-   if first_statement_only then fun _ statuses -> statuses
-   else loop
-  in
-  let stop,g,s = 
+ let rec loop status =
+  let stop,status = 
    try
      let cont =
-       try Some (GrafiteParser.parse_statement ~include_paths str status)
+       try Some (get_ast status ~compiling ~include_paths str)
        with End_of_file -> None in
      match cont with
-     | None -> true, status, statuses
-     | Some (status,ast) ->
-       (match ast with
-           GrafiteParser.LNone _ ->
-            watch_statuses status ;
-            false, status, ((status,None)::statuses)
-         | GrafiteParser.LSome ast ->
-            cb status ast;
-            let new_statuses = eval_ast ?do_heavy_checks status ("",0,ast) in
-            if enforce_no_new_aliases then
-             List.iter 
-              (fun (_,alias) ->
-                match alias with
-                  None -> ()
-                | Some (k,value) ->
-                   let newtxt = LexiconAstPp.pp_alias value in
-                    raise (TryingToAdd newtxt)) new_statuses;
-            let status =
-             match new_statuses with
-                [] -> assert false
-              | (s,_)::_ -> s
-            in
-             watch_statuses status ;
-             false, status, (new_statuses @ statuses))
+     | None -> true, status
+     | Some ast ->
+        cb status ast;
+        let new_statuses =
+          eval_ast ~include_paths ?do_heavy_checks status ("",0,ast) in
+        let status =
+         match new_statuses with
+            [s,None] -> s
+          | _::(_,Some (_,value))::_ ->
+                raise (TryingToAdd (lazy (GrafiteAstPp.pp_alias value)))
+          | _ -> assert false
+        in
+         false, status
    with exn when not matita_debug ->
      raise (EnrichedWithStatus (exn, status))
   in
-  if stop then s else loop g s
+  if stop then status else loop status
  in
-  loop status []
+  loop status
+
+and compile ~compiling ~include_paths fname =
+  if List.mem fname compiling then raise (CircularDependency fname);
+  let compiling = fname::compiling in
+  let matita_debug = Helm_registry.get_bool "matita.debug" in
+  let root,baseuri,fname,_tgt = 
+    Librarian.baseuri_of_script ~include_paths fname in
+  if Http_getter_storage.is_read_only baseuri then assert false;
+  activate_extraction baseuri fname ;
+  (* MATITA 1.0: debbo fare time_travel sulla ng_library? *)
+  let grafite_status = new GrafiteTypes.status baseuri in
+  let big_bang = Unix.gettimeofday () in
+  let { Unix.tms_utime = big_bang_u ; Unix.tms_stime = big_bang_s} = 
+    Unix.times () 
+  in
+  let time = Unix.time () in
+  try
+    (* cleanup of previously compiled objects *)
+    if (not (Http_getter_storage.is_empty ~local:true baseuri))
+      then begin
+      HLog.message ("baseuri " ^ baseuri ^ " is not empty");
+      HLog.message ("cleaning baseuri " ^ baseuri);
+      LibraryClean.clean_baseuris [baseuri];
+    end;
+    HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
+    if not (Helm_registry.get_bool "matita.verbose") then
+      (let cc = 
+        let rex = Str.regexp ".*opt$" in
+        if Str.string_match rex Sys.argv.(0) 0 then "matitac.opt"
+        else "matitac" 
+      in
+      let s = Printf.sprintf "%s %-35s " cc (cut (root^"/") fname) in
+      print_string s; flush stdout);
+    (* we dalay this error check until we print 'matitac file ' *)
+    assert (Http_getter_storage.is_empty ~local:true baseuri);
+    (* create dir for XML files *)
+    if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
+              ~default:false) 
+    then
+      HExtlib.mkdir 
+        (Filename.dirname 
+          (Http_getter.filename ~local:true ~writable:true (baseuri ^
+          "foo.con")));
+    let grammar = CicNotationParser.level2_ast_grammar grafite_status in
+    let buf =
+     Grammar.parsable grammar
+      (Obj.magic (Ulexing.from_utf8_channel (open_in fname)))
+    in
+    let print_cb =
+      if not (Helm_registry.get_bool "matita.verbose") then (fun _ _ -> ())
+      else pp_ast_statement
+    in
+    let grafite_status =
+     eval_from_stream ~compiling ~include_paths grafite_status buf print_cb in
+    let elapsed = Unix.time () -. time in
+     (if Helm_registry.get_bool "matita.moo" then begin
+       GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
+        ~dependencies:grafite_status#dependencies grafite_status#dump
+     end;
+     let tm = Unix.gmtime elapsed in
+     let sec = string_of_int tm.Unix.tm_sec ^ "''" in
+     let min = 
+       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else "" 
+     in
+     let hou = 
+       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
+     in
+     HLog.message 
+       (sprintf "execution of %s completed in %s." fname (hou^min^sec));
+     pp_times fname true big_bang big_bang_u big_bang_s
+(* MATITA 1.0: debbo fare time_travel sulla ng_library?
+     LexiconSync.time_travel 
+       ~present:lexicon_status ~past:initial_lexicon_status;
+*))
+  with 
+  (* all exceptions should be wrapped to allow lexicon-undo (LS.time_travel) *)
+  | exn when not matita_debug ->
+(* MATITA 1.0: debbo fare time_travel sulla ng_library?
+       LexiconSync.time_travel ~present:lexicon ~past:initial_lexicon_status;
+ *       *)
+      pp_times fname false big_bang big_bang_u big_bang_s;
+      clean_exit baseuri exn
+
+(* BIG BUG: if a file recursively includes itself, anything can happen,
+ * from divergence to inclusion of an old copy of itself *)
+and assert_ng ~compiling ~include_paths ~root mapath =
+ let root',baseuri,fullmapath,_ = Librarian.baseuri_of_script ~include_paths mapath in
+ assert (root=root');
+ let baseuri = NUri.uri_of_string baseuri in
+ let ngpath = NCicLibrary.ng_path_of_baseuri baseuri in
+ let ngtime =
+  try
+   Some (Unix.stat ngpath).Unix.st_mtime
+  with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = ngpath -> None in
+ let matime =
+  try (Unix.stat fullmapath).Unix.st_mtime
+  with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = fullmapath -> assert false
+ in
+ let to_be_compiled =
+  match ngtime with
+     Some ngtime ->
+      let preamble = GrafiteTypes.Serializer.dependencies_of baseuri in
+      let children_bad =
+       List.exists (assert_ng ~compiling ~include_paths ~root) preamble
+      in
+       children_bad || matime > ngtime
+   | None -> true
+ in
+  if not to_be_compiled then false
+  else
+   (* MATITA 1.0: SHOULD TAKE THIS FROM THE STATUS *)
+   if List.mem baseuri (NCicLibrary.get_already_included ()) then
+     (* maybe recompiling it I would get the same... *)
+     raise (AlreadyLoaded (lazy mapath))
+   else
+    (compile ~compiling ~include_paths fullmapath; true)
 ;;
+
+let assert_ng = assert_ng ~compiling:[]
+let get_ast = get_ast ~compiling:[]