src/Pure/Thy/thy_load.ML
author wenzelm
Tue Aug 21 11:00:54 2012 +0200 (2012-08-21 ago)
changeset 48869 4371a8d029f2
parent 48868 aeea516440c8
child 48874 ff9cd47be39b
permissions -rw-r--r--
tuned;
     1 (*  Title:      Pure/Thy/thy_load.ML
     2     Author:     Makarius
     3 
     4 Loading files that contribute to a theory.  Global master path.
     5 *)
     6 
     7 signature THY_LOAD =
     8 sig
     9   val master_directory: theory -> Path.T
    10   val imports_of: theory -> string list
    11   val provide: Path.T * (Path.T * SHA1.digest) -> theory -> theory
    12   val check_file: Path.T -> Path.T -> Path.T
    13   val thy_path: Path.T -> Path.T
    14   val check_thy: Path.T -> string ->
    15     {master: Path.T * SHA1.digest, text: string, imports: string list, uses: (Path.T * bool) list}
    16   val load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
    17   val use_file: Path.T -> theory -> string * theory
    18   val loaded_files: theory -> Path.T list
    19   val all_current: theory -> bool
    20   val use_ml: Path.T -> unit
    21   val exec_ml: Path.T -> generic_theory -> generic_theory
    22   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    23   val load_thy: int -> Path.T -> Thy_Header.header -> Position.T -> string ->
    24     theory list -> theory * unit future
    25   val resolve_files: Path.T -> Thy_Syntax.span -> Thy_Syntax.span
    26   val parse_files: string -> (theory -> Token.files) parser
    27   val set_master_path: Path.T -> unit
    28   val get_master_path: unit -> Path.T
    29 end;
    30 
    31 structure Thy_Load: THY_LOAD =
    32 struct
    33 
    34 (* manage source files *)
    35 
    36 type files =
    37  {master_dir: Path.T,  (*master directory of theory source*)
    38   imports: string list,  (*source specification of imports*)
    39   required: Path.T list,  (*source path*)
    40   provided: (Path.T * (Path.T * SHA1.digest)) list};  (*source path, physical path, digest*)
    41 
    42 fun make_files (master_dir, imports, required, provided): files =
    43  {master_dir = master_dir, imports = imports, required = required, provided = provided};
    44 
    45 structure Files = Theory_Data
    46 (
    47   type T = files;
    48   val empty = make_files (Path.current, [], [], []);
    49   fun extend _ = empty;
    50   fun merge _ = empty;
    51 );
    52 
    53 fun map_files f =
    54   Files.map (fn {master_dir, imports, required, provided} =>
    55     make_files (f (master_dir, imports, required, provided)));
    56 
    57 
    58 val master_directory = #master_dir o Files.get;
    59 val imports_of = #imports o Files.get;
    60 
    61 fun put_deps dir imports = map_files (fn _ => (dir, imports, [], []));
    62 
    63 fun require src_path =
    64   map_files (fn (master_dir, imports, required, provided) =>
    65     if member (op =) required src_path then
    66       error ("Duplicate source file dependency: " ^ Path.print src_path)
    67     else (master_dir, imports, src_path :: required, provided));
    68 
    69 fun provide (src_path, path_id) =
    70   map_files (fn (master_dir, imports, required, provided) =>
    71     if AList.defined (op =) provided src_path then
    72       error ("Duplicate resolution of source file dependency: " ^ Path.print src_path)
    73     else (master_dir, imports, required, (src_path, path_id) :: provided));
    74 
    75 
    76 (* inlined files *)
    77 
    78 fun command_files cmd path =
    79   (case Keyword.command_files cmd of
    80     [] => [path]
    81   | exts => map (fn ext => Path.ext ext path) exts);
    82 
    83 fun read_files cmd dir tok =
    84   let
    85     val path = Path.explode (Token.content_of tok);
    86     val files =
    87       command_files cmd path
    88       |> map (Path.append dir #> (fn p => (File.read p, Path.position p)));
    89   in (dir, files) end;
    90 
    91 fun parse_files cmd =
    92   Scan.ahead Parse.not_eof -- Parse.group (fn () => "file name/path specification") Parse.name
    93     >> (fn (tok, name) => fn thy =>
    94       (case Token.get_files tok of
    95         SOME files => files
    96       | NONE =>
    97           (warning ("Dynamic loading of files: " ^ quote name ^ Token.pos_of tok);
    98             read_files cmd (master_directory thy) tok)));
    99 
   100 local
   101 
   102 fun clean ((i1, t1) :: (i2, t2) :: toks) =
   103       if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean toks
   104       else (i1, t1) :: clean ((i2, t2) :: toks)
   105   | clean toks = toks;
   106 
   107 fun clean_tokens toks =
   108   ((0 upto length toks - 1) ~~ toks)
   109   |> filter (fn (_, tok) => Token.is_proper tok) |> clean;
   110 
   111 in
   112 
   113 fun resolve_files dir (span as Thy_Syntax.Span (Thy_Syntax.Command cmd, toks)) =
   114       if Keyword.is_theory_load cmd then
   115         (case find_first (Token.is_name o #2) (rev (clean_tokens toks)) of
   116           NONE => span
   117         | SOME (i, _) =>
   118             let
   119               val toks' = toks |> map_index (fn (j, tok) =>
   120                 if i = j then Token.put_files (read_files cmd dir tok) tok
   121                 else tok);
   122             in Thy_Syntax.Span (Thy_Syntax.Command cmd, toks') end)
   123       else span
   124   | resolve_files _ span = span;
   125 
   126 end;
   127 
   128 
   129 (* check files *)
   130 
   131 fun check_file dir file = File.check_file (File.full_path dir file);
   132 
   133 val thy_path = Path.ext "thy";
   134 
   135 fun check_thy dir name =
   136   let
   137     val path = thy_path (Path.basic name);
   138     val master_file = check_file dir path;
   139     val text = File.read master_file;
   140     val (name', imports, uses) =
   141       if name = Context.PureN then (Context.PureN, [], [])
   142       else
   143         let val {name, imports, uses, ...} = Thy_Header.read (Path.position master_file) text
   144         in (name, imports, uses) end;
   145     val _ = name <> name' andalso
   146       error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name');
   147   in {master = (master_file, SHA1.digest text), text = text, imports = imports, uses = uses} end;
   148 
   149 
   150 (* load files *)
   151 
   152 fun load_file thy src_path =
   153   let
   154     val full_path = check_file (master_directory thy) src_path;
   155     val text = File.read full_path;
   156     val id = SHA1.digest text;
   157   in ((full_path, id), text) end;
   158 
   159 fun use_file src_path thy =
   160   let
   161     val (path_id, text) = load_file thy src_path;
   162     val thy' = provide (src_path, path_id) thy;
   163   in (text, thy') end;
   164 
   165 val loaded_files = map (#1 o #2) o #provided o Files.get;
   166 
   167 fun check_loaded thy =
   168   let
   169     val {required, provided, ...} = Files.get thy;
   170     val provided_paths = map #1 provided;
   171     val _ =
   172       (case subtract (op =) provided_paths required of
   173         [] => NONE
   174       | bad => error ("Pending source file dependencies: " ^ commas (map Path.print (rev bad))));
   175     val _ =
   176       (case subtract (op =) required provided_paths of
   177         [] => NONE
   178       | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.print (rev bad))));
   179   in () end;
   180 
   181 fun all_current thy =
   182   let
   183     val {provided, ...} = Files.get thy;
   184     fun current (src_path, (_, id)) =
   185       (case try (load_file thy) src_path of
   186         NONE => false
   187       | SOME ((_, id'), _) => id = id');
   188   in can check_loaded thy andalso forall current provided end;
   189 
   190 val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
   191 
   192 
   193 (* provide files *)
   194 
   195 fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
   196 
   197 fun use_ml src_path =
   198   if is_none (Context.thread_data ()) then
   199     let val path = check_file Path.current src_path
   200     in eval_file path (File.read path) end
   201   else
   202     let
   203       val thy = ML_Context.the_global_context ();
   204 
   205       val ((path, id), text) = load_file thy src_path;
   206       val _ = eval_file path text;
   207       val _ = Context.>> Local_Theory.propagate_ml_env;
   208 
   209       val provide = provide (src_path, (path, id));
   210       val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
   211     in () end;
   212 
   213 fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
   214 
   215 
   216 (* load_thy *)
   217 
   218 fun begin_theory dir {name, imports, keywords, uses} parents =
   219   Theory.begin_theory name parents
   220   |> put_deps dir imports
   221   |> fold Thy_Header.declare_keyword keywords
   222   |> fold (require o fst) uses
   223   |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
   224   |> Theory.checkpoint;
   225 
   226 fun excursion init elements =
   227   let
   228     val immediate = not (Goal.future_enabled ());
   229 
   230     fun proof_result (tr, trs) (st, _) =
   231       let
   232         val (result, st') = Toplevel.proof_result immediate (tr, trs) st;
   233         val pos' = Toplevel.pos_of (List.last (tr :: trs));
   234       in (result, (st', pos')) end;
   235 
   236     fun element_result elem x =
   237       fold_map proof_result
   238         (Outer_Syntax.read_element (#2 (Outer_Syntax.get_syntax ())) init elem) x;
   239 
   240     val (results, (end_state, end_pos)) =
   241       fold_map element_result elements (Toplevel.toplevel, Position.none);
   242 
   243     val thy = Toplevel.end_theory end_pos end_state;
   244   in (flat results, thy) end;
   245 
   246 fun load_thy update_time dir header pos text parents =
   247   let
   248     val time = ! Toplevel.timing;
   249 
   250     val {name, uses, ...} = header;
   251     val _ = Thy_Header.define_keywords header;
   252     val _ = Present.init_theory name;
   253     fun init () =
   254       begin_theory dir header parents
   255       |> Present.begin_theory update_time dir uses;
   256 
   257     val lexs = Keyword.get_lexicons ();
   258 
   259     val toks = Thy_Syntax.parse_tokens lexs pos text;
   260     val spans = map (resolve_files dir) (Thy_Syntax.parse_spans toks);
   261     val elements = Thy_Syntax.parse_elements spans;
   262 
   263     val _ = Present.theory_source name
   264       (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   265 
   266     val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
   267     val (results, thy) = cond_timeit time "" (fn () => excursion init elements);
   268     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   269 
   270     val present =
   271       singleton (Future.cond_forks {name = "Outer_Syntax.present:" ^ name, group = NONE,
   272         deps = map Future.task_of results, pri = 0, interrupts = true})
   273       (fn () =>
   274         let val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax () in
   275           Thy_Output.present_thy minor Keyword.command_tags
   276             (Outer_Syntax.is_markup outer_syntax)
   277             (maps Future.join results) toks
   278           |> Buffer.content
   279           |> Present.theory_output name
   280         end);
   281 
   282   in (thy, present) end;
   283 
   284 
   285 (* global master path *)
   286 
   287 local
   288   val master_path = Unsynchronized.ref Path.current;
   289 in
   290 
   291 fun set_master_path path = master_path := path;
   292 fun get_master_path () = ! master_path;
   293 
   294 end;
   295 
   296 end;