src/Pure/Thy/thy_load.ML
author wenzelm
Wed Nov 20 11:55:52 2013 +0100 (2013-11-20 ago)
changeset 54526 92961f196d9e
parent 54525 de7c13e25212
child 54705 0dff3326d12a
permissions -rw-r--r--
load files that are not provided by PIDE blobs;
uniform resolve_files via Command.read;
wenzelm@6168
     1
(*  Title:      Pure/Thy/thy_load.ML
wenzelm@42002
     2
    Author:     Makarius
wenzelm@6168
     3
wenzelm@54525
     4
Management of theory sources and auxiliary files.
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@48898
    12
  val check_thy: Path.T -> string ->
wenzelm@48928
    13
   {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
wenzelm@51293
    14
    imports: (string * Position.T) list, keywords: Thy_Header.keywords}
wenzelm@54525
    15
  val parse_files: string -> (theory -> Token.file list) parser
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@37949
    19
  val loaded_files: theory -> Path.T list
wenzelm@54446
    20
  val loaded_files_current: theory -> bool
wenzelm@46938
    21
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@54458
    22
  val load_thy: bool -> (Toplevel.transition -> Time.time option) -> int -> Path.T ->
wenzelm@54458
    23
    Thy_Header.header -> Position.T -> string -> theory list -> theory * (unit -> unit) * int
wenzelm@6168
    24
end;
wenzelm@6168
    25
wenzelm@37216
    26
structure Thy_Load: THY_LOAD =
wenzelm@6168
    27
struct
wenzelm@6168
    28
wenzelm@37949
    29
(* manage source files *)
wenzelm@37949
    30
wenzelm@37949
    31
type files =
wenzelm@40741
    32
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@48927
    33
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
    34
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    35
wenzelm@48886
    36
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    37
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    38
wenzelm@37949
    39
structure Files = Theory_Data
wenzelm@37949
    40
(
wenzelm@37949
    41
  type T = files;
wenzelm@48886
    42
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    43
  fun extend _ = empty;
wenzelm@37949
    44
  fun merge _ = empty;
wenzelm@37949
    45
);
wenzelm@37949
    46
wenzelm@37949
    47
fun map_files f =
wenzelm@48886
    48
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    49
    make_files (f (master_dir, imports, provided)));
wenzelm@37949
    50
wenzelm@37949
    51
wenzelm@37949
    52
val master_directory = #master_dir o Files.get;
wenzelm@41548
    53
val imports_of = #imports o Files.get;
wenzelm@37949
    54
wenzelm@48888
    55
fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
wenzelm@37949
    56
wenzelm@37949
    57
wenzelm@54525
    58
(* theory files *)
wenzelm@54525
    59
wenzelm@54525
    60
val thy_path = Path.ext "thy";
wenzelm@48869
    61
wenzelm@48878
    62
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
    63
wenzelm@54525
    64
fun check_thy dir thy_name =
wenzelm@54525
    65
  let
wenzelm@54525
    66
    val path = thy_path (Path.basic thy_name);
wenzelm@54525
    67
    val master_file = check_file dir path;
wenzelm@54525
    68
    val text = File.read master_file;
wenzelm@54525
    69
wenzelm@54525
    70
    val {name = (name, pos), imports, keywords} =
wenzelm@54525
    71
      Thy_Header.read (Path.position master_file) text;
wenzelm@54525
    72
    val _ = thy_name <> name andalso
wenzelm@54525
    73
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name ^ Position.here pos);
wenzelm@54525
    74
  in
wenzelm@54525
    75
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm@54525
    76
    imports = imports, keywords = keywords}
wenzelm@54525
    77
  end;
wenzelm@54525
    78
wenzelm@54525
    79
wenzelm@54526
    80
(* load files *)
wenzelm@48898
    81
wenzelm@48898
    82
fun parse_files cmd =
wenzelm@48898
    83
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
    84
    (case Token.get_files tok of
wenzelm@54526
    85
      [] =>
wenzelm@54526
    86
        let
wenzelm@54526
    87
          val master_dir = master_directory thy;
wenzelm@54526
    88
          val pos = Token.position_of tok;
wenzelm@54526
    89
          val src_paths = Keyword.command_files cmd (Path.explode name);
wenzelm@54526
    90
        in map (Command.read_file master_dir pos) src_paths end
wenzelm@54520
    91
    | files => map Exn.release files));
wenzelm@48869
    92
wenzelm@48906
    93
fun provide (src_path, id) =
wenzelm@48906
    94
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
    95
    if AList.defined (op =) provided src_path then
wenzelm@48906
    96
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
    97
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
    98
wenzelm@48906
    99
fun provide_parse_files cmd =
wenzelm@48906
   100
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   101
    let
wenzelm@48906
   102
      val fs = files thy;
wenzelm@48906
   103
      val thy' = fold (fn {src_path, text, ...} => provide (src_path, SHA1.digest text)) fs thy;
wenzelm@48906
   104
    in (fs, thy') end);
wenzelm@48906
   105
wenzelm@43702
   106
fun load_file thy src_path =
wenzelm@43702
   107
  let
wenzelm@43702
   108
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   109
    val text = File.read full_path;
wenzelm@43702
   110
    val id = SHA1.digest text;
wenzelm@43702
   111
  in ((full_path, id), text) end;
wenzelm@43702
   112
wenzelm@54446
   113
fun loaded_files_current thy =
wenzelm@48886
   114
  #provided (Files.get thy) |>
wenzelm@48896
   115
    forall (fn (src_path, id) =>
wenzelm@43702
   116
      (case try (load_file thy) src_path of
wenzelm@37949
   117
        NONE => false
wenzelm@48886
   118
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   119
wenzelm@54454
   120
(*Proof General legacy*)
wenzelm@54454
   121
fun loaded_files thy =
wenzelm@54454
   122
  let val {master_dir, provided, ...} = Files.get thy
wenzelm@54454
   123
  in map (File.full_path master_dir o #1) provided end;
wenzelm@54454
   124
wenzelm@37949
   125
wenzelm@54525
   126
(* load theory *)
wenzelm@37977
   127
wenzelm@51294
   128
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@37977
   129
  Theory.begin_theory name parents
wenzelm@48888
   130
  |> put_deps master_dir imports
wenzelm@52788
   131
  |> fold Thy_Header.declare_keyword keywords;
wenzelm@37977
   132
wenzelm@54526
   133
fun excursion master_dir last_timing init elements =
wenzelm@46959
   134
  let
wenzelm@51268
   135
    fun prepare_span span =
wenzelm@51268
   136
      Thy_Syntax.span_content span
wenzelm@54526
   137
      |> Command.read init master_dir []
wenzelm@51268
   138
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@51217
   139
wenzelm@51268
   140
    fun element_result span_elem (st, _) =
wenzelm@46959
   141
      let
wenzelm@51273
   142
        val elem = Thy_Syntax.map_element prepare_span span_elem;
wenzelm@51273
   143
        val (results, st') = Toplevel.element_result elem st;
wenzelm@51273
   144
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@51268
   145
      in (results, (st', pos')) end;
wenzelm@46959
   146
wenzelm@46959
   147
    val (results, (end_state, end_pos)) =
wenzelm@46959
   148
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   149
wenzelm@46959
   150
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@51273
   151
  in (results, thy) end;
wenzelm@46959
   152
wenzelm@54458
   153
fun load_thy document last_timing update_time master_dir header text_pos text parents =
wenzelm@43712
   154
  let
wenzelm@43712
   155
    val time = ! Toplevel.timing;
wenzelm@43712
   156
wenzelm@51294
   157
    val {name = (name, _), ...} = header;
wenzelm@46958
   158
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   159
wenzelm@46959
   160
    val lexs = Keyword.get_lexicons ();
wenzelm@48927
   161
    val toks = Thy_Syntax.parse_tokens lexs text_pos text;
wenzelm@54526
   162
    val spans = Thy_Syntax.parse_spans toks;
wenzelm@46959
   163
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   164
wenzelm@54456
   165
    fun init () =
wenzelm@54456
   166
      begin_theory master_dir header parents
wenzelm@54456
   167
      |> Present.begin_theory update_time
wenzelm@54456
   168
          (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   169
wenzelm@43712
   170
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@54526
   171
    val (results, thy) =
wenzelm@54526
   172
      cond_timeit time "" (fn () => excursion master_dir last_timing init elements);
wenzelm@43712
   173
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   174
wenzelm@51331
   175
    fun present () =
wenzelm@51331
   176
      let
wenzelm@51332
   177
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@51331
   178
        val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
wenzelm@51331
   179
      in
wenzelm@51556
   180
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@51556
   181
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@51556
   182
        else
wenzelm@54458
   183
          let val tex_source =
wenzelm@54458
   184
            Thy_Output.present_thy minor Keyword.command_tags
wenzelm@54458
   185
              (Outer_Syntax.is_markup outer_syntax) res toks
wenzelm@54458
   186
            |> Buffer.content;
wenzelm@54458
   187
          in if document then Present.theory_output name tex_source else () end
wenzelm@51331
   188
      end;
wenzelm@43712
   189
wenzelm@51331
   190
  in (thy, present, size text) end;
wenzelm@43712
   191
wenzelm@42002
   192
wenzelm@49244
   193
(* document antiquotation *)
wenzelm@49244
   194
wenzelm@53171
   195
val _ = Theory.setup
wenzelm@53171
   196
  (Thy_Output.antiquotation (Binding.name "file") (Scan.lift (Parse.position Parse.path))
wenzelm@49244
   197
    (fn {context = ctxt, ...} => fn (name, pos) =>
wenzelm@49244
   198
      let
wenzelm@49244
   199
        val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@49244
   200
        val path = Path.append dir (Path.explode name);
wenzelm@49244
   201
        val _ =
wenzelm@49244
   202
          if File.exists path then ()
wenzelm@49244
   203
          else error ("Bad file: " ^ Path.print (Path.expand path) ^ Position.here pos);
wenzelm@50201
   204
        val _ = Position.report pos (Markup.path name);
wenzelm@52752
   205
      in
wenzelm@52752
   206
        space_explode "/" name
wenzelm@52752
   207
        |> map Thy_Output.verb_text
wenzelm@52752
   208
        |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
wenzelm@53171
   209
      end));
wenzelm@49244
   210
wenzelm@6168
   211
end;