src/Pure/Thy/thy_load.ML
author wenzelm
Wed Aug 22 21:02:02 2012 +0200 (2012-08-22 ago)
changeset 48886 9604c6563226
parent 48881 46e053eda5dd
child 48887 c49eca45cbb0
permissions -rw-r--r--
discontinued separate list of required files -- maintain only provided files as they occur at runtime;
tuned signature;
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@48876
    14
  val check_thy: Thy_Header.keywords -> Path.T -> string ->
wenzelm@48874
    15
   {master: Path.T * SHA1.digest, text: string, imports: string list,
wenzelm@48876
    16
    uses: (Path.T * bool) list, keywords: Thy_Header.keywords}
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@48886
    20
  val load_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@41955
    40
  provided: (Path.T * (Path.T * SHA1.digest)) list};  (*source path, physical path, digest*)
wenzelm@37949
    41
wenzelm@48886
    42
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    43
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    44
wenzelm@37949
    45
structure Files = Theory_Data
wenzelm@37949
    46
(
wenzelm@37949
    47
  type T = files;
wenzelm@48886
    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@48886
    54
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    55
    make_files (f (master_dir, imports, 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@48886
    61
fun put_deps dir imports = map_files (fn _ => (dir, imports, []));
wenzelm@37949
    62
wenzelm@37978
    63
fun provide (src_path, path_id) =
wenzelm@48886
    64
  map_files (fn (master_dir, imports, provided) =>
wenzelm@37949
    65
    if AList.defined (op =) provided src_path then
wenzelm@48886
    66
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48886
    67
    else (master_dir, imports, (src_path, path_id) :: provided));
wenzelm@37949
    68
wenzelm@37949
    69
wenzelm@48869
    70
(* inlined files *)
wenzelm@48869
    71
wenzelm@48878
    72
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
    73
wenzelm@48874
    74
local
wenzelm@48874
    75
wenzelm@48874
    76
fun clean ((i1, t1) :: (i2, t2) :: toks) =
wenzelm@48874
    77
      if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean toks
wenzelm@48874
    78
      else (i1, t1) :: clean ((i2, t2) :: toks)
wenzelm@48874
    79
  | clean toks = toks;
wenzelm@48874
    80
wenzelm@48874
    81
fun clean_tokens toks =
wenzelm@48874
    82
  ((0 upto length toks - 1) ~~ toks)
wenzelm@48874
    83
  |> filter (fn (_, tok) => Token.is_proper tok)
wenzelm@48874
    84
  |> clean;
wenzelm@48874
    85
wenzelm@48874
    86
fun find_file toks =
wenzelm@48874
    87
  rev (clean_tokens toks) |> get_first (fn (i, tok) =>
wenzelm@48881
    88
    if Token.is_name tok then
wenzelm@48886
    89
      SOME (i, Path.explode (Token.content_of tok))
wenzelm@48886
    90
        handle ERROR msg => error (msg ^ Token.pos_of tok)
wenzelm@48874
    91
    else NONE);
wenzelm@48874
    92
wenzelm@48874
    93
fun command_files path exts =
wenzelm@48874
    94
  if null exts then [path]
wenzelm@48874
    95
  else map (fn ext => Path.ext ext path) exts;
wenzelm@48874
    96
wenzelm@48874
    97
in
wenzelm@48874
    98
wenzelm@48874
    99
fun find_files syntax text =
wenzelm@48874
   100
  let val thy_load_commands = Keyword.thy_load_commands syntax in
wenzelm@48874
   101
    if exists (fn (cmd, _) => String.isSubstring cmd text) thy_load_commands then
wenzelm@48874
   102
      Thy_Syntax.parse_tokens (Keyword.lexicons_of syntax) Position.none text
wenzelm@48874
   103
      |> Thy_Syntax.parse_spans
wenzelm@48874
   104
      |> maps
wenzelm@48878
   105
        (fn Thy_Syntax.Span (Thy_Syntax.Command (cmd, _), toks) =>
wenzelm@48874
   106
              (case AList.lookup (op =) thy_load_commands cmd of
wenzelm@48874
   107
                SOME exts =>
wenzelm@48874
   108
                  (case find_file toks of
wenzelm@48874
   109
                    SOME (_, path) => command_files path exts
wenzelm@48874
   110
                  | NONE => [])
wenzelm@48874
   111
              | NONE => [])
wenzelm@48874
   112
          | _ => [])
wenzelm@48874
   113
    else []
wenzelm@48874
   114
  end;
wenzelm@48869
   115
wenzelm@48880
   116
fun read_files cmd dir path =
wenzelm@48869
   117
  let
wenzelm@48869
   118
    val files =
wenzelm@48874
   119
      command_files path (Keyword.command_files cmd)
wenzelm@48880
   120
      |> map (check_file dir #> (fn p => (File.read p, Path.position p)));
wenzelm@48869
   121
  in (dir, files) end;
wenzelm@48869
   122
wenzelm@48869
   123
fun parse_files cmd =
wenzelm@48881
   124
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48880
   125
    (case Token.get_files tok of
wenzelm@48880
   126
      SOME files => files
wenzelm@48880
   127
    | NONE =>
wenzelm@48881
   128
        let
wenzelm@48881
   129
          val path = Path.explode name;
wenzelm@48881
   130
          val _ = warning ("Dynamic loading of files: " ^ Path.print path);
wenzelm@48881
   131
        in read_files cmd (master_directory thy) path end));
wenzelm@48869
   132
wenzelm@48874
   133
fun resolve_files dir span =
wenzelm@48874
   134
  (case span of
wenzelm@48878
   135
    Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks) =>
wenzelm@48869
   136
      if Keyword.is_theory_load cmd then
wenzelm@48880
   137
        (case find_file toks of
wenzelm@48880
   138
          NONE => error ("Bad file argument of command " ^ quote cmd ^ Position.str_of pos)
wenzelm@48874
   139
        | SOME (i, path) =>
wenzelm@48869
   140
            let
wenzelm@48869
   141
              val toks' = toks |> map_index (fn (j, tok) =>
wenzelm@48874
   142
                if i = j then Token.put_files (read_files cmd dir path) tok
wenzelm@48869
   143
                else tok);
wenzelm@48878
   144
            in Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks') end)
wenzelm@48869
   145
      else span
wenzelm@48874
   146
  | span => span);
wenzelm@48869
   147
wenzelm@48869
   148
end;
wenzelm@48869
   149
wenzelm@48869
   150
wenzelm@23872
   151
(* check files *)
wenzelm@6232
   152
wenzelm@46737
   153
val thy_path = Path.ext "thy";
wenzelm@46737
   154
wenzelm@48876
   155
fun check_thy base_keywords dir thy_name =
wenzelm@23872
   156
  let
wenzelm@48876
   157
    val path = thy_path (Path.basic thy_name);
wenzelm@46737
   158
    val master_file = check_file dir path;
wenzelm@42003
   159
    val text = File.read master_file;
wenzelm@48876
   160
wenzelm@48876
   161
    val {name, imports, uses, keywords} = Thy_Header.read (Path.position master_file) text;
wenzelm@48876
   162
    val _ = thy_name <> name andalso
wenzelm@48876
   163
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name);
wenzelm@48876
   164
wenzelm@48876
   165
    val syntax =
wenzelm@48876
   166
      fold (Keyword.define_keywords o apsnd (Option.map Keyword.spec))
wenzelm@48876
   167
        (keywords @ base_keywords) (Keyword.get_keywords ());
wenzelm@48876
   168
    val more_uses = map (rpair false) (find_files syntax text);
wenzelm@48874
   169
  in
wenzelm@48874
   170
   {master = (master_file, SHA1.digest text), text = text,
wenzelm@48876
   171
    imports = imports, uses = uses @ more_uses, keywords = keywords}
wenzelm@48874
   172
  end;
wenzelm@6168
   173
wenzelm@37949
   174
wenzelm@43702
   175
(* load files *)
wenzelm@43702
   176
wenzelm@43702
   177
fun load_file thy src_path =
wenzelm@43702
   178
  let
wenzelm@43702
   179
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   180
    val text = File.read full_path;
wenzelm@43702
   181
    val id = SHA1.digest text;
wenzelm@43702
   182
  in ((full_path, id), text) end;
wenzelm@43702
   183
wenzelm@43702
   184
fun use_file src_path thy =
wenzelm@43702
   185
  let
wenzelm@43702
   186
    val (path_id, text) = load_file thy src_path;
wenzelm@43702
   187
    val thy' = provide (src_path, path_id) thy;
wenzelm@43702
   188
  in (text, thy') end;
wenzelm@37949
   189
wenzelm@37949
   190
val loaded_files = map (#1 o #2) o #provided o Files.get;
wenzelm@6168
   191
wenzelm@48886
   192
fun load_current thy =
wenzelm@48886
   193
  #provided (Files.get thy) |>
wenzelm@48886
   194
    forall (fn (src_path, (_, id)) =>
wenzelm@43702
   195
      (case try (load_file thy) src_path of
wenzelm@37949
   196
        NONE => false
wenzelm@48886
   197
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   198
wenzelm@37949
   199
wenzelm@37949
   200
(* provide files *)
wenzelm@37949
   201
wenzelm@43700
   202
fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
wenzelm@37949
   203
wenzelm@37949
   204
fun use_ml src_path =
wenzelm@37949
   205
  if is_none (Context.thread_data ()) then
wenzelm@43700
   206
    let val path = check_file Path.current src_path
wenzelm@43700
   207
    in eval_file path (File.read path) end
wenzelm@37949
   208
  else
wenzelm@37949
   209
    let
wenzelm@37949
   210
      val thy = ML_Context.the_global_context ();
wenzelm@37949
   211
wenzelm@43702
   212
      val ((path, id), text) = load_file thy src_path;
wenzelm@43700
   213
      val _ = eval_file path text;
wenzelm@37949
   214
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   215
wenzelm@37978
   216
      val provide = provide (src_path, (path, id));
wenzelm@38757
   217
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   218
    in () end;
wenzelm@37949
   219
wenzelm@37949
   220
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   221
wenzelm@37977
   222
wenzelm@43712
   223
(* load_thy *)
wenzelm@37977
   224
wenzelm@46938
   225
fun begin_theory dir {name, imports, keywords, uses} parents =
wenzelm@37977
   226
  Theory.begin_theory name parents
wenzelm@41548
   227
  |> put_deps dir imports
wenzelm@46938
   228
  |> fold Thy_Header.declare_keyword keywords
wenzelm@37977
   229
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
wenzelm@37977
   230
  |> Theory.checkpoint;
wenzelm@37977
   231
wenzelm@46959
   232
fun excursion init elements =
wenzelm@46959
   233
  let
wenzelm@46959
   234
    val immediate = not (Goal.future_enabled ());
wenzelm@46959
   235
wenzelm@46959
   236
    fun proof_result (tr, trs) (st, _) =
wenzelm@46959
   237
      let
wenzelm@46959
   238
        val (result, st') = Toplevel.proof_result immediate (tr, trs) st;
wenzelm@46959
   239
        val pos' = Toplevel.pos_of (List.last (tr :: trs));
wenzelm@46959
   240
      in (result, (st', pos')) end;
wenzelm@46959
   241
wenzelm@46959
   242
    fun element_result elem x =
wenzelm@46959
   243
      fold_map proof_result
wenzelm@46959
   244
        (Outer_Syntax.read_element (#2 (Outer_Syntax.get_syntax ())) init elem) x;
wenzelm@46959
   245
wenzelm@46959
   246
    val (results, (end_state, end_pos)) =
wenzelm@46959
   247
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   248
wenzelm@46959
   249
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@46959
   250
  in (flat results, thy) end;
wenzelm@46959
   251
wenzelm@46938
   252
fun load_thy update_time dir header pos text parents =
wenzelm@43712
   253
  let
wenzelm@43712
   254
    val time = ! Toplevel.timing;
wenzelm@43712
   255
wenzelm@48867
   256
    val {name, uses, ...} = header;
wenzelm@46958
   257
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   258
    val _ = Present.init_theory name;
wenzelm@44186
   259
    fun init () =
wenzelm@46938
   260
      begin_theory dir header parents
wenzelm@43712
   261
      |> Present.begin_theory update_time dir uses;
wenzelm@43712
   262
wenzelm@46959
   263
    val lexs = Keyword.get_lexicons ();
wenzelm@46958
   264
wenzelm@46811
   265
    val toks = Thy_Syntax.parse_tokens lexs pos text;
wenzelm@48867
   266
    val spans = map (resolve_files dir) (Thy_Syntax.parse_spans toks);
wenzelm@46959
   267
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   268
wenzelm@43712
   269
    val _ = Present.theory_source name
wenzelm@43712
   270
      (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   271
wenzelm@43712
   272
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@46959
   273
    val (results, thy) = cond_timeit time "" (fn () => excursion init elements);
wenzelm@43712
   274
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   275
wenzelm@43712
   276
    val present =
wenzelm@43712
   277
      singleton (Future.cond_forks {name = "Outer_Syntax.present:" ^ name, group = NONE,
wenzelm@44113
   278
        deps = map Future.task_of results, pri = 0, interrupts = true})
wenzelm@43712
   279
      (fn () =>
wenzelm@46959
   280
        let val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax () in
wenzelm@46959
   281
          Thy_Output.present_thy minor Keyword.command_tags
wenzelm@46959
   282
            (Outer_Syntax.is_markup outer_syntax)
wenzelm@46959
   283
            (maps Future.join results) toks
wenzelm@46959
   284
          |> Buffer.content
wenzelm@46959
   285
          |> Present.theory_output name
wenzelm@46959
   286
        end);
wenzelm@43712
   287
wenzelm@43712
   288
  in (thy, present) end;
wenzelm@43712
   289
wenzelm@42002
   290
wenzelm@42002
   291
(* global master path *)
wenzelm@42002
   292
wenzelm@42002
   293
local
wenzelm@42002
   294
  val master_path = Unsynchronized.ref Path.current;
wenzelm@42002
   295
in
wenzelm@42002
   296
wenzelm@42002
   297
fun set_master_path path = master_path := path;
wenzelm@42002
   298
fun get_master_path () = ! master_path;
wenzelm@42002
   299
wenzelm@6168
   300
end;
wenzelm@6168
   301
wenzelm@42002
   302
end;