1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
31 class status baseuri =
33 inherit GrafiteTypes.status baseuri
34 inherit ApplyTransformation.status
37 exception TryingToAdd of string Lazy.t
38 exception EnrichedWithStatus of exn * status
39 exception AlreadyLoaded of string Lazy.t
40 exception FailureCompiling of string * exn
41 exception CircularDependency of string
44 let debug_print = if debug then prerr_endline else ignore ;;
46 let slash_n_RE = Pcre.regexp "\\n" ;;
48 let first_line = ref true ;;
50 let cases_or_induction_context stack =
53 | (_g,_t,_k,_tag,p)::_tl -> try
54 let s = List.assoc "context" p in
55 s = "cases" || s = "induction"
60 let has_focused_goal stack =
63 | (g,_t,_k,_tag,_p)::_tl -> (List.length g) > 0
66 let get_indentation status _statement =
68 match status#stack with
70 | s -> List.length(s) * 2
72 if cases_or_induction_context status#stack then
74 if has_focused_goal status#stack then
87 | n -> " " ^ (aux s (n-1))
91 let write_ast_to_file status fname statement =
92 let indentation = get_indentation status statement in
93 let str = match statement with
94 G.Comment _ -> GrafiteAstPp.pp_statement status statement
95 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
96 | G.Executable (_,code) ->
99 G.NTactic _ -> GrafiteAstPp.pp_statement status statement
100 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
101 | G.NCommand (_,cmd) ->
104 | G.NObj (_,obj,_) ->
107 NotationPt.Theorem _ -> "\n" ^ GrafiteAstPp.pp_statement status statement
108 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
111 | G.NQed _ -> GrafiteAstPp.pp_statement status statement
112 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
120 let s = pp_ind str indentation in
121 let flaglist = if !first_line = false then [Open_wronly; Open_append; Open_creat]
122 else (first_line := false; [Open_wronly; Open_trunc; Open_creat])
125 open_out_gen flaglist 0o0644 fname in
126 let _ = output_string out_channel ((if str.[0] <> '\n' then s else str) ^ "\n") in
127 let _ = close_out out_channel in
134 let pp_ast_statement status stm ~fname =
135 let stm = write_ast_to_file status (fname ^ ".parsed.ma") stm in
138 let stm = Pcre.replace ~rex:slash_n_RE stm in
140 if String.length stm > 50 then String.sub stm 0 50 ^ " ..."
143 HLog.debug ("Executing: ``" ^ stm ^ "''")
146 HLog.debug ("Executing: `` Unprintable statement ''")
149 let clean_exit baseuri exn =
150 LibraryClean.clean_baseuris ~verbose:false [baseuri];
151 raise (FailureCompiling (baseuri,exn))
155 let lenp = String.length prefix in
156 let lens = String.length s in
157 assert (lens > lenp);
158 assert (String.sub s 0 lenp = prefix);
159 String.sub s lenp (lens-lenp)
163 let indent = ref 0 in
164 let print_string ~right_justify s =
166 match right_justify with
168 | Some (ss,len_ss) ->
169 let i = 80 - !indent - len_ss - String.length s in
170 if i > 0 then String.make i ' ' ^ ss else ss
172 assert (!indent >=0);
173 print_string (String.make !indent ' ' ^ s ^ ss) in
174 fun enter ?right_justify s ->
175 if enter then (print_string ~right_justify s; incr indent) else (decr indent; print_string ~right_justify s)
178 let pp_times ss fname rc big_bang big_bang_u big_bang_s =
179 if not (Helm_registry.get_bool "matita.verbose") then
180 let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
181 let r = Unix.gettimeofday () -. big_bang in
182 let u = u -. big_bang_u in
183 let s = s -. big_bang_s in
184 let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
186 if rc then "
\e[0;32mOK
\e[0m" else "
\e[0;31mFAIL
\e[0m" in
189 let seconds = int_of_float t in
190 let cents = int_of_float ((t -. floor t) *. 100.0) in
191 let minutes = seconds / 60 in
192 let seconds = seconds mod 60 in
193 Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
195 Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
197 let s = Printf.sprintf "%-14s %s %s\n" rc times extra in
198 print_string false ~right_justify:(s,31) ss;
200 HLog.message ("Compilation of "^Filename.basename fname^": "^rc)
203 let eval_ast ~include_paths ?do_heavy_checks status (text,prefix_len,ast) =
204 let baseuri = status#baseuri in
205 let new_aliases,new_status =
206 GrafiteDisambiguate.eval_with_new_aliases status
208 let time0 = Unix.gettimeofday () in
210 GrafiteEngine.eval_ast ~include_paths ?do_heavy_checks status
211 (text,prefix_len,ast) in
212 let time1 = Unix.gettimeofday () in
213 HLog.debug ("... grafite_engine done in " ^ string_of_float (time1 -. time0) ^ "s");
216 let _,intermediate_states =
218 (fun (status,acc) (k,value) ->
219 let v = GrafiteAst.description_of_alias value in
222 let NReference.Ref (uri,_) = NReference.reference_of_string v in
223 NUri.baseuri_of_uri uri = baseuri
225 NReference.IllFormedReference _ ->
226 false (* v is a description, not a URI *)
232 GrafiteDisambiguate.set_proof_aliases status ~implicit_aliases:false
233 GrafiteAst.WithPreferences [k,value]
235 status, (status ,Some (k,value))::acc
236 ) (status,[]) new_aliases (* WARNING: this must be the old status! *)
238 (new_status,None)::intermediate_states
241 let baseuri_of_script ~include_paths fname =
242 try Librarian.baseuri_of_script ~include_paths fname
244 Librarian.NoRootFor _ ->
245 HLog.error ("The included file '"^fname^"' has no root file,");
246 HLog.error "please create it.";
247 raise (Failure ("No root file for "^fname))
248 | Librarian.FileNotFound _ ->
249 raise (Failure ("File not found: "^fname))
252 (* given a path to a ma file inside the include_paths, returns the
253 new include_paths associated to that file *)
254 let read_include_paths ~include_paths:_ file =
256 let root, _buri, _fname, _tgt =
257 Librarian.baseuri_of_script ~include_paths:[] file in
260 Str.split (Str.regexp " ")
261 (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
264 let rc = root :: includes in
265 List.iter (HLog.debug) rc; rc
266 with Librarian.NoRootFor _ | Librarian.FileNotFound _ ->
270 let rec get_ast status ~compiling ~asserted ~include_paths strm =
271 match GrafiteParser.parse_statement status strm with
272 (GrafiteAst.Executable
273 (_,GrafiteAst.NCommand (_,GrafiteAst.Include (_,_,mafilename)))) as cmd
275 let already_included = NCicLibrary.get_transitively_included status in
277 assert_ng ~already_included ~compiling ~asserted ~include_paths
281 | cmd -> asserted,cmd
283 and eval_from_stream ~compiling ~asserted ~include_paths ?do_heavy_checks status str cb =
284 let matita_debug = Helm_registry.get_bool "matita.debug" in
285 let rec loop asserted status str =
286 let asserted,stop,status,str =
289 try Some (get_ast status ~compiling ~asserted ~include_paths str)
290 with End_of_file -> None in
292 | None -> asserted, true, status, str
293 | Some (asserted,ast) ->
296 eval_ast ~include_paths ?do_heavy_checks status ("",0,ast) in
298 match new_statuses with
300 | _::(_,Some (_,value))::_ ->
301 raise (TryingToAdd (lazy (GrafiteAstPp.pp_alias value)))
302 | _ -> assert false in
303 (* CSC: complex patch to re-build the lexer since the tokens may
304 have changed. Note: this way we loose look-ahead tokens.
305 Hence the "include" command must be terminated (no look-ahead) *)
308 (GrafiteAst.Executable
309 (_,GrafiteAst.NCommand
310 (_,(GrafiteAst.Include _ | GrafiteAst.Notation _)))) ->
311 GrafiteParser.parsable_statement status
312 (GrafiteParser.strm_of_parsable str)
315 asserted, false, status, str
316 with exn when not matita_debug ->
317 raise (EnrichedWithStatus (exn, status))
319 if stop then asserted,status else loop asserted status str
321 loop asserted status str
323 and compile ~compiling ~asserted ~include_paths fname =
324 if List.mem fname compiling then raise (CircularDependency fname);
325 let compiling = fname::compiling in
326 let matita_debug = Helm_registry.get_bool "matita.debug" in
327 let root,baseuri,fname,_tgt =
328 Librarian.baseuri_of_script ~include_paths fname in
329 if Http_getter_storage.is_read_only baseuri then assert false;
330 (* MATITA 1.0: debbo fare time_travel sulla ng_library? *)
331 let status = new status baseuri in
332 (*CSC: bad, one imperative bit is still there!
333 to be moved into functional status *)
334 NCicMetaSubst.pushmaxmeta ();
335 let ocamldirname = Filename.dirname fname in
336 let ocamlfname = Filename.chop_extension (Filename.basename fname) in
337 let status,ocamlfname =
338 Common.modname_of_filename status false ocamlfname in
339 let ocamlfname = ocamldirname ^ "/" ^ ocamlfname ^ ".ml" in
340 let status = OcamlExtraction.open_file status ~baseuri ocamlfname in
341 let big_bang = Unix.gettimeofday () in
342 let { Unix.tms_utime = big_bang_u ; Unix.tms_stime = big_bang_s} =
345 let time = Unix.time () in
347 let rex = Str.regexp ".*opt$" in
348 if Str.string_match rex Sys.argv.(0) 0 then "matitac.opt"
350 let s = Printf.sprintf "%s %s" cc (cut (root^"/") fname) in
352 (* cleanup of previously compiled objects *)
353 if (not (Http_getter_storage.is_empty ~local:true baseuri))
355 HLog.message ("baseuri " ^ baseuri ^ " is not empty");
356 HLog.message ("cleaning baseuri " ^ baseuri);
357 LibraryClean.clean_baseuris [baseuri];
359 HLog.message ("compiling " ^ Filename.basename fname ^ " in " ^ baseuri);
360 if not (Helm_registry.get_bool "matita.verbose") then
361 (print_string true (s ^ "\n"); flush stdout);
362 (* we dalay this error check until we print 'matitac file ' *)
363 assert (Http_getter_storage.is_empty ~local:true baseuri);
364 (* create dir for XML files *)
365 if not (Helm_registry.get_opt_default Helm_registry.bool "matita.nodisk"
370 (Http_getter.filename ~local:true ~writable:true (baseuri ^
373 GrafiteParser.parsable_statement status
374 (Ulexing.from_utf8_channel (open_in fname))
377 if not (Helm_registry.get_bool "matita.verbose") then fun _ _ -> ()
378 else pp_ast_statement ~fname
380 let asserted, status =
381 eval_from_stream ~compiling ~asserted ~include_paths status buf print_cb in
382 let status = OcamlExtraction.close_file status in
383 let elapsed = Unix.time () -. time in
384 (if Helm_registry.get_bool "matita.moo" then begin
385 GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
388 let tm = Unix.gmtime elapsed in
389 let sec = string_of_int tm.Unix.tm_sec ^ "''" in
391 if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min^"' ") else ""
394 if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour^"h ") else ""
397 (sprintf "execution of %s completed in %s." fname (hou^min^sec));
398 pp_times s fname true big_bang big_bang_u big_bang_s;
399 (*CSC: bad, one imperative bit is still there!
400 to be moved into functional status *)
401 NCicMetaSubst.pushmaxmeta ();
402 (* MATITA 1.0: debbo fare time_travel sulla ng_library?
403 LexiconSync.time_travel
404 ~present:lexicon_status ~past:initial_lexicon_status;
408 (* all exceptions should be wrapped to allow lexicon-undo (LS.time_travel) *)
409 | exn when not matita_debug ->
410 (* MATITA 1.0: debbo fare time_travel sulla ng_library?
411 LexiconSync.time_travel ~present:lexicon ~past:initial_lexicon_status;
413 (*CSC: bad, one imperative bit is still there!
414 to be moved into functional status *)
415 NCicMetaSubst.pushmaxmeta ();
416 pp_times s fname false big_bang big_bang_u big_bang_s;
417 clean_exit baseuri exn
419 and assert_ng ~already_included ~compiling ~asserted ~include_paths mapath =
420 let root,baseuri,fullmapath,_ =
421 Librarian.baseuri_of_script ~include_paths mapath in
422 if List.mem fullmapath asserted then asserted,false
428 Str.split (Str.regexp " ")
429 (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
433 Helm_registry.get_list Helm_registry.string "matita.includes" in
434 let baseuri = NUri.uri_of_string baseuri in
435 let ngtime_of baseuri =
436 let ngpath = NCicLibrary.ng_path_of_baseuri baseuri in
438 Some (Unix.stat ngpath).Unix.st_mtime
439 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = ngpath -> None in
441 try (Unix.stat fullmapath).Unix.st_mtime
442 with Unix.Unix_error (Unix.ENOENT, "stat", f) when f = fullmapath -> assert false
444 let ngtime = ngtime_of baseuri in
445 let asserted,to_be_compiled =
448 let preamble = GrafiteTypes.Serializer.dependencies_of baseuri in
449 let asserted,children_bad =
451 (fun (asserted,b) mapath ->
454 assert_ng ~already_included ~compiling ~asserted ~include_paths
456 with Librarian.NoRootFor _ | Librarian.FileNotFound _ ->
460 || let _,baseuri,_,_ =
461 (*CSC: bug here? include_paths should be empty and
462 mapath should be absolute *)
463 Librarian.baseuri_of_script ~include_paths mapath in
464 let baseuri = NUri.uri_of_string baseuri in
465 (match ngtime_of baseuri with
466 Some child_ngtime -> child_ngtime > ngtime
467 | None -> assert false)
468 ) (asserted,false) preamble
470 asserted, children_bad || matime > ngtime
471 | None -> asserted,true
473 if not to_be_compiled then fullmapath::asserted,false
475 if List.mem baseuri already_included then
476 (* maybe recompiling it I would get the same... *)
477 raise (AlreadyLoaded (lazy mapath))
479 let asserted = compile ~compiling ~asserted ~include_paths fullmapath in
480 fullmapath::asserted,true
484 let assert_ng ~include_paths mapath =
485 snd (assert_ng ~include_paths ~already_included:[] ~compiling:[] ~asserted:[]
487 let get_ast status ~include_paths strm =
488 snd (get_ast status ~compiling:[] ~asserted:[] ~include_paths strm)