src/Pure/Thy/thy_load.ML
author wenzelm
Sat Nov 16 13:12:02 2013 +0100 (2013-11-16 ago)
changeset 54446 31884c67d73a
parent 53403 c09f4005d6bd
child 54448 7110476960f7
permissions -rw-r--r--
tuned signature;
wenzelm@6168
     1
(*  Title:      Pure/Thy/thy_load.ML
wenzelm@42002
     2
    Author:     Makarius
wenzelm@6168
     3
wenzelm@48888
     4
Loading files that contribute to a theory.  Global master path for TTY loop.
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@48927
    10
  val imports_of: theory -> (string * Position.T) list
wenzelm@46737
    11
  val thy_path: Path.T -> Path.T
wenzelm@48906
    12
  val parse_files: string -> (theory -> Token.file list) parser
wenzelm@48898
    13
  val check_thy: Path.T -> string ->
wenzelm@48928
    14
   {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
wenzelm@51293
    15
    imports: (string * Position.T) list, keywords: Thy_Header.keywords}
wenzelm@48906
    16
  val provide: Path.T * SHA1.digest -> theory -> theory
wenzelm@48906
    17
  val provide_parse_files: string -> (theory -> Token.file list * theory) parser
wenzelm@43702
    18
  val load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
wenzelm@43702
    19
  val use_file: Path.T -> theory -> string * theory
wenzelm@37949
    20
  val loaded_files: theory -> Path.T list
wenzelm@54446
    21
  val loaded_files_current: theory -> bool
wenzelm@37949
    22
  val use_ml: Path.T -> unit
wenzelm@37949
    23
  val exec_ml: Path.T -> generic_theory -> generic_theory
wenzelm@46938
    24
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@51423
    25
  val load_thy: (Toplevel.transition -> Time.time option) -> int -> Path.T -> Thy_Header.header ->
wenzelm@51331
    26
    Position.T -> string -> theory list -> theory * (unit -> unit) * int
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@48927
    38
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
    39
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    40
wenzelm@48886
    41
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    42
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    43
wenzelm@37949
    44
structure Files = Theory_Data
wenzelm@37949
    45
(
wenzelm@37949
    46
  type T = files;
wenzelm@48886
    47
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    48
  fun extend _ = empty;
wenzelm@37949
    49
  fun merge _ = empty;
wenzelm@37949
    50
);
wenzelm@37949
    51
wenzelm@37949
    52
fun map_files f =
wenzelm@48886
    53
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    54
    make_files (f (master_dir, imports, provided)));
wenzelm@37949
    55
wenzelm@37949
    56
wenzelm@37949
    57
val master_directory = #master_dir o Files.get;
wenzelm@41548
    58
val imports_of = #imports o Files.get;
wenzelm@37949
    59
wenzelm@48888
    60
fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
wenzelm@37949
    61
wenzelm@37949
    62
wenzelm@48869
    63
(* inlined files *)
wenzelm@48869
    64
wenzelm@48878
    65
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
    66
wenzelm@48919
    67
fun read_files cmd dir (path, pos) =
wenzelm@48898
    68
  let
wenzelm@48919
    69
    fun make_file file =
wenzelm@48919
    70
      let
wenzelm@50201
    71
        val _ = Position.report pos (Markup.path (Path.implode file));
wenzelm@48919
    72
        val full_path = check_file dir file;
wenzelm@48919
    73
      in {src_path = file, text = File.read full_path, pos = Path.position full_path} end;
wenzelm@48898
    74
    val paths =
wenzelm@48898
    75
      (case Keyword.command_files cmd of
wenzelm@48898
    76
        [] => [path]
wenzelm@48898
    77
      | exts => map (fn ext => Path.ext ext path) exts);
wenzelm@48919
    78
  in map make_file paths end;
wenzelm@48898
    79
wenzelm@48898
    80
fun parse_files cmd =
wenzelm@48898
    81
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
    82
    (case Token.get_files tok of
wenzelm@48898
    83
      SOME files => files
wenzelm@48919
    84
    | NONE => read_files cmd (master_directory thy) (Path.explode name, Token.position_of tok)));
wenzelm@48898
    85
wenzelm@48874
    86
local
wenzelm@48874
    87
wenzelm@48874
    88
fun clean ((i1, t1) :: (i2, t2) :: toks) =
wenzelm@48874
    89
      if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean toks
wenzelm@48874
    90
      else (i1, t1) :: clean ((i2, t2) :: toks)
wenzelm@48874
    91
  | clean toks = toks;
wenzelm@48874
    92
wenzelm@48874
    93
fun clean_tokens toks =
wenzelm@48874
    94
  ((0 upto length toks - 1) ~~ toks)
wenzelm@48874
    95
  |> filter (fn (_, tok) => Token.is_proper tok)
wenzelm@48874
    96
  |> clean;
wenzelm@48874
    97
wenzelm@48874
    98
fun find_file toks =
wenzelm@48874
    99
  rev (clean_tokens toks) |> get_first (fn (i, tok) =>
wenzelm@48881
   100
    if Token.is_name tok then
wenzelm@48919
   101
      SOME (i, (Path.explode (Token.content_of tok), Token.position_of tok))
wenzelm@48886
   102
        handle ERROR msg => error (msg ^ Token.pos_of tok)
wenzelm@48874
   103
    else NONE);
wenzelm@48874
   104
wenzelm@48874
   105
in
wenzelm@48874
   106
wenzelm@48888
   107
fun resolve_files master_dir span =
wenzelm@48874
   108
  (case span of
wenzelm@48878
   109
    Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks) =>
wenzelm@48869
   110
      if Keyword.is_theory_load cmd then
wenzelm@48880
   111
        (case find_file toks of
wenzelm@48992
   112
          NONE => error ("Bad file argument of command " ^ quote cmd ^ Position.here pos)
wenzelm@48874
   113
        | SOME (i, path) =>
wenzelm@48869
   114
            let
wenzelm@48869
   115
              val toks' = toks |> map_index (fn (j, tok) =>
wenzelm@48888
   116
                if i = j then Token.put_files (read_files cmd master_dir path) tok
wenzelm@48869
   117
                else tok);
wenzelm@48878
   118
            in Thy_Syntax.Span (Thy_Syntax.Command (cmd, pos), toks') end)
wenzelm@48869
   119
      else span
wenzelm@48874
   120
  | span => span);
wenzelm@48869
   121
wenzelm@48869
   122
end;
wenzelm@48869
   123
wenzelm@48869
   124
wenzelm@23872
   125
(* check files *)
wenzelm@6232
   126
wenzelm@46737
   127
val thy_path = Path.ext "thy";
wenzelm@46737
   128
wenzelm@48898
   129
fun check_thy dir thy_name =
wenzelm@23872
   130
  let
wenzelm@48876
   131
    val path = thy_path (Path.basic thy_name);
wenzelm@46737
   132
    val master_file = check_file dir path;
wenzelm@42003
   133
    val text = File.read master_file;
wenzelm@48876
   134
wenzelm@51294
   135
    val {name = (name, pos), imports, keywords} =
wenzelm@48927
   136
      Thy_Header.read (Path.position master_file) text;
wenzelm@48876
   137
    val _ = thy_name <> name andalso
wenzelm@48992
   138
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
wenzelm@48874
   139
  in
wenzelm@48928
   140
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm@51293
   141
    imports = imports, keywords = keywords}
wenzelm@48874
   142
  end;
wenzelm@6168
   143
wenzelm@37949
   144
wenzelm@43702
   145
(* load files *)
wenzelm@43702
   146
wenzelm@48906
   147
fun provide (src_path, id) =
wenzelm@48906
   148
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
   149
    if AList.defined (op =) provided src_path then
wenzelm@48906
   150
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
   151
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
   152
wenzelm@48906
   153
fun provide_parse_files cmd =
wenzelm@48906
   154
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   155
    let
wenzelm@48906
   156
      val fs = files thy;
wenzelm@48906
   157
      val thy' = fold (fn {src_path, text, ...} => provide (src_path, SHA1.digest text)) fs thy;
wenzelm@48906
   158
    in (fs, thy') end);
wenzelm@48906
   159
wenzelm@43702
   160
fun load_file thy src_path =
wenzelm@43702
   161
  let
wenzelm@43702
   162
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   163
    val text = File.read full_path;
wenzelm@43702
   164
    val id = SHA1.digest text;
wenzelm@43702
   165
  in ((full_path, id), text) end;
wenzelm@43702
   166
wenzelm@43702
   167
fun use_file src_path thy =
wenzelm@43702
   168
  let
wenzelm@48896
   169
    val ((_, id), text) = load_file thy src_path;
wenzelm@48896
   170
    val thy' = provide (src_path, id) thy;
wenzelm@43702
   171
  in (text, thy') end;
wenzelm@37949
   172
wenzelm@54446
   173
(*Proof General legacy*)
wenzelm@48896
   174
fun loaded_files thy =
wenzelm@48896
   175
  let val {master_dir, provided, ...} = Files.get thy
wenzelm@48896
   176
  in map (File.full_path master_dir o #1) provided end;
wenzelm@6168
   177
wenzelm@54446
   178
fun loaded_files_current thy =
wenzelm@48886
   179
  #provided (Files.get thy) |>
wenzelm@48896
   180
    forall (fn (src_path, id) =>
wenzelm@43702
   181
      (case try (load_file thy) src_path of
wenzelm@37949
   182
        NONE => false
wenzelm@48886
   183
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   184
wenzelm@37949
   185
wenzelm@37949
   186
(* provide files *)
wenzelm@37949
   187
wenzelm@43700
   188
fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
wenzelm@37949
   189
wenzelm@37949
   190
fun use_ml src_path =
wenzelm@37949
   191
  if is_none (Context.thread_data ()) then
wenzelm@43700
   192
    let val path = check_file Path.current src_path
wenzelm@43700
   193
    in eval_file path (File.read path) end
wenzelm@37949
   194
  else
wenzelm@37949
   195
    let
wenzelm@37949
   196
      val thy = ML_Context.the_global_context ();
wenzelm@37949
   197
wenzelm@43702
   198
      val ((path, id), text) = load_file thy src_path;
wenzelm@43700
   199
      val _ = eval_file path text;
wenzelm@37949
   200
      val _ = Context.>> Local_Theory.propagate_ml_env;
wenzelm@37949
   201
wenzelm@48896
   202
      val provide = provide (src_path, id);
wenzelm@38757
   203
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
wenzelm@37978
   204
    in () end;
wenzelm@37949
   205
wenzelm@37949
   206
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
wenzelm@6168
   207
wenzelm@37977
   208
wenzelm@43712
   209
(* load_thy *)
wenzelm@37977
   210
wenzelm@51294
   211
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@37977
   212
  Theory.begin_theory name parents
wenzelm@48888
   213
  |> put_deps master_dir imports
wenzelm@52788
   214
  |> fold Thy_Header.declare_keyword keywords;
wenzelm@37977
   215
wenzelm@51217
   216
fun excursion last_timing init elements =
wenzelm@46959
   217
  let
wenzelm@51268
   218
    fun prepare_span span =
wenzelm@51268
   219
      Thy_Syntax.span_content span
wenzelm@52534
   220
      |> Command.read init
wenzelm@51268
   221
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@51217
   222
wenzelm@51268
   223
    fun element_result span_elem (st, _) =
wenzelm@46959
   224
      let
wenzelm@51273
   225
        val elem = Thy_Syntax.map_element prepare_span span_elem;
wenzelm@51273
   226
        val (results, st') = Toplevel.element_result elem st;
wenzelm@51273
   227
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@51268
   228
      in (results, (st', pos')) end;
wenzelm@46959
   229
wenzelm@46959
   230
    val (results, (end_state, end_pos)) =
wenzelm@46959
   231
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   232
wenzelm@46959
   233
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@51273
   234
  in (results, thy) end;
wenzelm@46959
   235
wenzelm@51217
   236
fun load_thy last_timing update_time master_dir header text_pos text parents =
wenzelm@43712
   237
  let
wenzelm@43712
   238
    val time = ! Toplevel.timing;
wenzelm@43712
   239
wenzelm@51294
   240
    val {name = (name, _), ...} = header;
wenzelm@46958
   241
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   242
    val _ = Present.init_theory name;
wenzelm@44186
   243
    fun init () =
wenzelm@48888
   244
      begin_theory master_dir header parents
wenzelm@51293
   245
      |> Present.begin_theory update_time master_dir;
wenzelm@43712
   246
wenzelm@46959
   247
    val lexs = Keyword.get_lexicons ();
wenzelm@46958
   248
wenzelm@48927
   249
    val toks = Thy_Syntax.parse_tokens lexs text_pos text;
wenzelm@48888
   250
    val spans = map (resolve_files master_dir) (Thy_Syntax.parse_spans toks);
wenzelm@46959
   251
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   252
wenzelm@43712
   253
    val _ = Present.theory_source name
wenzelm@43712
   254
      (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   255
wenzelm@43712
   256
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@51217
   257
    val (results, thy) = cond_timeit time "" (fn () => excursion last_timing init elements);
wenzelm@43712
   258
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   259
wenzelm@51331
   260
    fun present () =
wenzelm@51331
   261
      let
wenzelm@51332
   262
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@51331
   263
        val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
wenzelm@51331
   264
      in
wenzelm@51556
   265
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@51556
   266
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@51556
   267
        else
wenzelm@51556
   268
          Thy_Output.present_thy minor Keyword.command_tags
wenzelm@51556
   269
            (Outer_Syntax.is_markup outer_syntax) res toks
wenzelm@51556
   270
          |> Buffer.content
wenzelm@51556
   271
          |> Present.theory_output name
wenzelm@51331
   272
      end;
wenzelm@43712
   273
wenzelm@51331
   274
  in (thy, present, size text) end;
wenzelm@43712
   275
wenzelm@42002
   276
wenzelm@49244
   277
(* document antiquotation *)
wenzelm@49244
   278
wenzelm@53171
   279
val _ = Theory.setup
wenzelm@53171
   280
  (Thy_Output.antiquotation (Binding.name "file") (Scan.lift (Parse.position Parse.path))
wenzelm@49244
   281
    (fn {context = ctxt, ...} => fn (name, pos) =>
wenzelm@49244
   282
      let
wenzelm@49244
   283
        val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@49244
   284
        val path = Path.append dir (Path.explode name);
wenzelm@49244
   285
        val _ =
wenzelm@49244
   286
          if File.exists path then ()
wenzelm@49244
   287
          else error ("Bad file: " ^ Path.print (Path.expand path) ^ Position.here pos);
wenzelm@50201
   288
        val _ = Position.report pos (Markup.path name);
wenzelm@52752
   289
      in
wenzelm@52752
   290
        space_explode "/" name
wenzelm@52752
   291
        |> map Thy_Output.verb_text
wenzelm@52752
   292
        |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
wenzelm@53171
   293
      end));
wenzelm@49244
   294
wenzelm@49244
   295
wenzelm@53403
   296
(* global master path *)  (*Proof General legacy*)
wenzelm@42002
   297
wenzelm@42002
   298
local
wenzelm@42002
   299
  val master_path = Unsynchronized.ref Path.current;
wenzelm@42002
   300
in
wenzelm@42002
   301
wenzelm@42002
   302
fun set_master_path path = master_path := path;
wenzelm@42002
   303
fun get_master_path () = ! master_path;
wenzelm@42002
   304
wenzelm@6168
   305
end;
wenzelm@6168
   306
wenzelm@42002
   307
end;