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;
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@48869
    76
(* inlined files *)
wenzelm@48869
    77
wenzelm@48869
    78
fun command_files cmd path =
wenzelm@48869
    79
  (case Keyword.command_files cmd of
wenzelm@48869
    80
    [] => [path]
wenzelm@48869
    81
  | exts => map (fn ext => Path.ext ext path) exts);
wenzelm@48869
    82
wenzelm@48869
    83
fun read_files cmd dir tok =
wenzelm@48869
    84
  let
wenzelm@48869
    85
    val path = Path.explode (Token.content_of tok);
wenzelm@48869
    86
    val files =
wenzelm@48869
    87
      command_files cmd path
wenzelm@48869
    88
      |> map (Path.append dir #> (fn p => (File.read p, Path.position p)));
wenzelm@48869
    89
  in (dir, files) end;
wenzelm@48869
    90
wenzelm@48869
    91
fun parse_files cmd =
wenzelm@48869
    92
  Scan.ahead Parse.not_eof -- Parse.group (fn () => "file name/path specification") Parse.name
wenzelm@48869
    93
    >> (fn (tok, name) => fn thy =>
wenzelm@48869
    94
      (case Token.get_files tok of
wenzelm@48869
    95
        SOME files => files
wenzelm@48869
    96
      | NONE =>
wenzelm@48869
    97
          (warning ("Dynamic loading of files: " ^ quote name ^ Token.pos_of tok);
wenzelm@48869
    98
            read_files cmd (master_directory thy) tok)));
wenzelm@48869
    99
wenzelm@48869
   100
local
wenzelm@48869
   101
wenzelm@48869
   102
fun clean ((i1, t1) :: (i2, t2) :: toks) =
wenzelm@48869
   103
      if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean toks
wenzelm@48869
   104
      else (i1, t1) :: clean ((i2, t2) :: toks)
wenzelm@48869
   105
  | clean toks = toks;
wenzelm@48869
   106
wenzelm@48869
   107
fun clean_tokens toks =
wenzelm@48869
   108
  ((0 upto length toks - 1) ~~ toks)
wenzelm@48869
   109
  |> filter (fn (_, tok) => Token.is_proper tok) |> clean;
wenzelm@48869
   110
wenzelm@48869
   111
in
wenzelm@48869
   112
wenzelm@48869
   113
fun resolve_files dir (span as Thy_Syntax.Span (Thy_Syntax.Command cmd, toks)) =
wenzelm@48869
   114
      if Keyword.is_theory_load cmd then
wenzelm@48869
   115
        (case find_first (Token.is_name o #2) (rev (clean_tokens toks)) of
wenzelm@48869
   116
          NONE => span
wenzelm@48869
   117
        | SOME (i, _) =>
wenzelm@48869
   118
            let
wenzelm@48869
   119
              val toks' = toks |> map_index (fn (j, tok) =>
wenzelm@48869
   120
                if i = j then Token.put_files (read_files cmd dir tok) tok
wenzelm@48869
   121
                else tok);
wenzelm@48869
   122
            in Thy_Syntax.Span (Thy_Syntax.Command cmd, toks') end)
wenzelm@48869
   123
      else span
wenzelm@48869
   124
  | resolve_files _ span = span;
wenzelm@48869
   125
wenzelm@48869
   126
end;
wenzelm@48869
   127
wenzelm@48869
   128
wenzelm@23872
   129
(* check files *)
wenzelm@6232
   130
wenzelm@42003
   131
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@42003
   132
wenzelm@46737
   133
val thy_path = Path.ext "thy";
wenzelm@46737
   134
wenzelm@41886
   135
fun check_thy dir name =
wenzelm@23872
   136
  let
wenzelm@46737
   137
    val path = thy_path (Path.basic name);
wenzelm@46737
   138
    val master_file = check_file dir path;
wenzelm@42003
   139
    val text = File.read master_file;
wenzelm@42003
   140
    val (name', imports, uses) =
wenzelm@42003
   141
      if name = Context.PureN then (Context.PureN, [], [])
wenzelm@46938
   142
      else
wenzelm@46938
   143
        let val {name, imports, uses, ...} = Thy_Header.read (Path.position master_file) text
wenzelm@46938
   144
        in (name, imports, uses) end;
wenzelm@46737
   145
    val _ = name <> name' andalso
wenzelm@46737
   146
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name');
wenzelm@42003
   147
  in {master = (master_file, SHA1.digest text), text = text, imports = imports, uses = uses} end;
wenzelm@6168
   148
wenzelm@37949
   149
wenzelm@43702
   150
(* load files *)
wenzelm@43702
   151
wenzelm@43702
   152
fun load_file thy src_path =
wenzelm@43702
   153
  let
wenzelm@43702
   154
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   155
    val text = File.read full_path;
wenzelm@43702
   156
    val id = SHA1.digest text;
wenzelm@43702
   157
  in ((full_path, id), text) end;
wenzelm@43702
   158
wenzelm@43702
   159
fun use_file src_path thy =
wenzelm@43702
   160
  let
wenzelm@43702
   161
    val (path_id, text) = load_file thy src_path;
wenzelm@43702
   162
    val thy' = provide (src_path, path_id) thy;
wenzelm@43702
   163
  in (text, thy') end;
wenzelm@37949
   164
wenzelm@37949
   165
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   166
wenzelm@37949
   167
fun check_loaded thy =
wenzelm@37949
   168
  let
wenzelm@37949
   169
    val {required, provided, ...} = Files.get thy;
wenzelm@37949
   170
    val provided_paths = map #1 provided;
wenzelm@37949
   171
    val _ =
wenzelm@37949
   172
      (case subtract (op =) provided_paths required of
wenzelm@37949
   173
        [] => NONE
wenzelm@41944
   174
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.print (rev bad))));
wenzelm@37949
   175
    val _ =
wenzelm@37949
   176
      (case subtract (op =) required provided_paths of
wenzelm@37949
   177
        [] => NONE
wenzelm@41944
   178
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.print (rev bad))));
wenzelm@37949
   179
  in () end;
wenzelm@37949
   180
wenzelm@37949
   181
fun all_current thy =
wenzelm@37939
   182
  let
wenzelm@43702
   183
    val {provided, ...} = Files.get thy;
wenzelm@37978
   184
    fun current (src_path, (_, id)) =
wenzelm@43702
   185
      (case try (load_file thy) src_path of
wenzelm@37949
   186
        NONE => false
wenzelm@43702
   187
      | SOME ((_, id'), _) => id = id');
wenzelm@37949
   188
  in can check_loaded thy andalso forall current provided end;
wenzelm@37949
   189
wenzelm@37952
   190
val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
wenzelm@37952
   191
wenzelm@37949
   192
wenzelm@37949
   193
(* provide files *)
wenzelm@37949
   194
wenzelm@43700
   195
fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
wenzelm@37949
   196
wenzelm@37949
   197
fun use_ml src_path =
wenzelm@37949
   198
  if is_none (Context.thread_data ()) then
wenzelm@43700
   199
    let val path = check_file Path.current src_path
wenzelm@43700
   200
    in eval_file path (File.read path) end
wenzelm@37949
   201
  else
wenzelm@37949
   202
    let
wenzelm@37949
   203
      val thy = ML_Context.the_global_context ();
wenzelm@37949
   204
wenzelm@43702
   205
      val ((path, id), text) = load_file thy src_path;
wenzelm@43700
   206
      val _ = eval_file path text;
wenzelm@37949
   207
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   208
wenzelm@37978
   209
      val provide = provide (src_path, (path, id));
wenzelm@38757
   210
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   211
    in () end;
wenzelm@37949
   212
wenzelm@37949
   213
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   214
wenzelm@37977
   215
wenzelm@43712
   216
(* load_thy *)
wenzelm@37977
   217
wenzelm@46938
   218
fun begin_theory dir {name, imports, keywords, uses} parents =
wenzelm@37977
   219
  Theory.begin_theory name parents
wenzelm@41548
   220
  |> put_deps dir imports
wenzelm@46938
   221
  |> fold Thy_Header.declare_keyword keywords
wenzelm@37977
   222
  |> fold (require o fst) uses
wenzelm@37977
   223
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   224
  |> Theory.checkpoint;
wenzelm@37977
   225
wenzelm@46959
   226
fun excursion init elements =
wenzelm@46959
   227
  let
wenzelm@46959
   228
    val immediate = not (Goal.future_enabled ());
wenzelm@46959
   229
wenzelm@46959
   230
    fun proof_result (tr, trs) (st, _) =
wenzelm@46959
   231
      let
wenzelm@46959
   232
        val (result, st') = Toplevel.proof_result immediate (tr, trs) st;
wenzelm@46959
   233
        val pos' = Toplevel.pos_of (List.last (tr :: trs));
wenzelm@46959
   234
      in (result, (st', pos')) end;
wenzelm@46959
   235
wenzelm@46959
   236
    fun element_result elem x =
wenzelm@46959
   237
      fold_map proof_result
wenzelm@46959
   238
        (Outer_Syntax.read_element (#2 (Outer_Syntax.get_syntax ())) init elem) x;
wenzelm@46959
   239
wenzelm@46959
   240
    val (results, (end_state, end_pos)) =
wenzelm@46959
   241
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   242
wenzelm@46959
   243
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@46959
   244
  in (flat results, thy) end;
wenzelm@46959
   245
wenzelm@46938
   246
fun load_thy update_time dir header pos text parents =
wenzelm@43712
   247
  let
wenzelm@43712
   248
    val time = ! Toplevel.timing;
wenzelm@43712
   249
wenzelm@48867
   250
    val {name, uses, ...} = header;
wenzelm@46958
   251
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   252
    val _ = Present.init_theory name;
wenzelm@44186
   253
    fun init () =
wenzelm@46938
   254
      begin_theory dir header parents
wenzelm@43712
   255
      |> Present.begin_theory update_time dir uses;
wenzelm@43712
   256
wenzelm@46959
   257
    val lexs = Keyword.get_lexicons ();
wenzelm@46958
   258
wenzelm@46811
   259
    val toks = Thy_Syntax.parse_tokens lexs pos text;
wenzelm@48867
   260
    val spans = map (resolve_files dir) (Thy_Syntax.parse_spans toks);
wenzelm@46959
   261
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   262
wenzelm@43712
   263
    val _ = Present.theory_source name
wenzelm@43712
   264
      (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   265
wenzelm@43712
   266
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@46959
   267
    val (results, thy) = cond_timeit time "" (fn () => excursion init elements);
wenzelm@43712
   268
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   269
wenzelm@43712
   270
    val present =
wenzelm@43712
   271
      singleton (Future.cond_forks {name = "Outer_Syntax.present:" ^ name, group = NONE,
wenzelm@44113
   272
        deps = map Future.task_of results, pri = 0, interrupts = true})
wenzelm@43712
   273
      (fn () =>
wenzelm@46959
   274
        let val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax () in
wenzelm@46959
   275
          Thy_Output.present_thy minor Keyword.command_tags
wenzelm@46959
   276
            (Outer_Syntax.is_markup outer_syntax)
wenzelm@46959
   277
            (maps Future.join results) toks
wenzelm@46959
   278
          |> Buffer.content
wenzelm@46959
   279
          |> Present.theory_output name
wenzelm@46959
   280
        end);
wenzelm@43712
   281
wenzelm@43712
   282
  in (thy, present) end;
wenzelm@43712
   283
wenzelm@42002
   284
wenzelm@42002
   285
(* global master path *)
wenzelm@42002
   286
wenzelm@42002
   287
local
wenzelm@42002
   288
  val master_path = Unsynchronized.ref Path.current;
wenzelm@42002
   289
in
wenzelm@42002
   290
wenzelm@42002
   291
fun set_master_path path = master_path := path;
wenzelm@42002
   292
fun get_master_path () = ! master_path;
wenzelm@42002
   293
wenzelm@6168
   294
end;
wenzelm@6168
   295
wenzelm@42002
   296
end;