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