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