src/Pure/Thy/thy_load.ML
author wenzelm
Wed Nov 20 10:51:47 2013 +0100 (2013-11-20 ago)
changeset 54525 de7c13e25212
parent 54523 11087efad95e
child 54526 92961f196d9e
permissions -rw-r--r--
tuned;
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@54525
    80
(* inlined files *)
wenzelm@54525
    81
wenzelm@54451
    82
fun read_files dir cmd (path, pos) =
wenzelm@48898
    83
  let
wenzelm@54523
    84
    fun make_file src_path =
wenzelm@48919
    85
      let
wenzelm@54523
    86
        val full_path = check_file dir src_path;
wenzelm@54523
    87
        val _ = Position.report pos (Markup.path (Path.implode full_path));
wenzelm@54523
    88
      in {src_path = src_path, text = File.read full_path, pos = Path.position full_path} end;
wenzelm@54523
    89
  in map make_file (Keyword.command_files cmd path) end;
wenzelm@48898
    90
wenzelm@48898
    91
fun parse_files cmd =
wenzelm@48898
    92
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
    93
    (case Token.get_files tok of
wenzelm@54519
    94
      [] => read_files (master_directory thy) cmd (Path.explode name, Token.position_of tok)
wenzelm@54520
    95
    | files => map Exn.release files));
wenzelm@48869
    96
wenzelm@54525
    97
fun resolve_files master_dir =
wenzelm@54525
    98
  Thy_Syntax.resolve_files (map Exn.Res oo read_files master_dir);
wenzelm@6168
    99
wenzelm@37949
   100
wenzelm@43702
   101
(* load files *)
wenzelm@43702
   102
wenzelm@48906
   103
fun provide (src_path, id) =
wenzelm@48906
   104
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
   105
    if AList.defined (op =) provided src_path then
wenzelm@48906
   106
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
   107
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
   108
wenzelm@48906
   109
fun provide_parse_files cmd =
wenzelm@48906
   110
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   111
    let
wenzelm@48906
   112
      val fs = files thy;
wenzelm@48906
   113
      val thy' = fold (fn {src_path, text, ...} => provide (src_path, SHA1.digest text)) fs thy;
wenzelm@48906
   114
    in (fs, thy') end);
wenzelm@48906
   115
wenzelm@43702
   116
fun load_file thy src_path =
wenzelm@43702
   117
  let
wenzelm@43702
   118
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   119
    val text = File.read full_path;
wenzelm@43702
   120
    val id = SHA1.digest text;
wenzelm@43702
   121
  in ((full_path, id), text) end;
wenzelm@43702
   122
wenzelm@54446
   123
fun loaded_files_current thy =
wenzelm@48886
   124
  #provided (Files.get thy) |>
wenzelm@48896
   125
    forall (fn (src_path, id) =>
wenzelm@43702
   126
      (case try (load_file thy) src_path of
wenzelm@37949
   127
        NONE => false
wenzelm@48886
   128
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   129
wenzelm@54454
   130
(*Proof General legacy*)
wenzelm@54454
   131
fun loaded_files thy =
wenzelm@54454
   132
  let val {master_dir, provided, ...} = Files.get thy
wenzelm@54454
   133
  in map (File.full_path master_dir o #1) provided end;
wenzelm@54454
   134
wenzelm@37949
   135
wenzelm@54525
   136
(* load theory *)
wenzelm@37977
   137
wenzelm@51294
   138
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@37977
   139
  Theory.begin_theory name parents
wenzelm@48888
   140
  |> put_deps master_dir imports
wenzelm@52788
   141
  |> fold Thy_Header.declare_keyword keywords;
wenzelm@37977
   142
wenzelm@51217
   143
fun excursion last_timing init elements =
wenzelm@46959
   144
  let
wenzelm@51268
   145
    fun prepare_span span =
wenzelm@51268
   146
      Thy_Syntax.span_content span
wenzelm@54519
   147
      |> Command.read init []
wenzelm@51268
   148
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@51217
   149
wenzelm@51268
   150
    fun element_result span_elem (st, _) =
wenzelm@46959
   151
      let
wenzelm@51273
   152
        val elem = Thy_Syntax.map_element prepare_span span_elem;
wenzelm@51273
   153
        val (results, st') = Toplevel.element_result elem st;
wenzelm@51273
   154
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@51268
   155
      in (results, (st', pos')) end;
wenzelm@46959
   156
wenzelm@46959
   157
    val (results, (end_state, end_pos)) =
wenzelm@46959
   158
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   159
wenzelm@46959
   160
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@51273
   161
  in (results, thy) end;
wenzelm@46959
   162
wenzelm@54458
   163
fun load_thy document last_timing update_time master_dir header text_pos text parents =
wenzelm@43712
   164
  let
wenzelm@43712
   165
    val time = ! Toplevel.timing;
wenzelm@43712
   166
wenzelm@51294
   167
    val {name = (name, _), ...} = header;
wenzelm@46958
   168
    val _ = Thy_Header.define_keywords header;
wenzelm@43712
   169
wenzelm@46959
   170
    val lexs = Keyword.get_lexicons ();
wenzelm@48927
   171
    val toks = Thy_Syntax.parse_tokens lexs text_pos text;
wenzelm@54525
   172
    val spans = map (resolve_files master_dir) (Thy_Syntax.parse_spans toks);
wenzelm@46959
   173
    val elements = Thy_Syntax.parse_elements spans;
wenzelm@43712
   174
wenzelm@54456
   175
    fun init () =
wenzelm@54456
   176
      begin_theory master_dir header parents
wenzelm@54456
   177
      |> Present.begin_theory update_time
wenzelm@54456
   178
          (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
wenzelm@43712
   179
wenzelm@43712
   180
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@51217
   181
    val (results, thy) = cond_timeit time "" (fn () => excursion last_timing init elements);
wenzelm@43712
   182
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@43712
   183
wenzelm@51331
   184
    fun present () =
wenzelm@51331
   185
      let
wenzelm@51332
   186
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@51331
   187
        val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax ();
wenzelm@51331
   188
      in
wenzelm@51556
   189
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@51556
   190
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@51556
   191
        else
wenzelm@54458
   192
          let val tex_source =
wenzelm@54458
   193
            Thy_Output.present_thy minor Keyword.command_tags
wenzelm@54458
   194
              (Outer_Syntax.is_markup outer_syntax) res toks
wenzelm@54458
   195
            |> Buffer.content;
wenzelm@54458
   196
          in if document then Present.theory_output name tex_source else () end
wenzelm@51331
   197
      end;
wenzelm@43712
   198
wenzelm@51331
   199
  in (thy, present, size text) end;
wenzelm@43712
   200
wenzelm@42002
   201
wenzelm@49244
   202
(* document antiquotation *)
wenzelm@49244
   203
wenzelm@53171
   204
val _ = Theory.setup
wenzelm@53171
   205
  (Thy_Output.antiquotation (Binding.name "file") (Scan.lift (Parse.position Parse.path))
wenzelm@49244
   206
    (fn {context = ctxt, ...} => fn (name, pos) =>
wenzelm@49244
   207
      let
wenzelm@49244
   208
        val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@49244
   209
        val path = Path.append dir (Path.explode name);
wenzelm@49244
   210
        val _ =
wenzelm@49244
   211
          if File.exists path then ()
wenzelm@49244
   212
          else error ("Bad file: " ^ Path.print (Path.expand path) ^ Position.here pos);
wenzelm@50201
   213
        val _ = Position.report pos (Markup.path name);
wenzelm@52752
   214
      in
wenzelm@52752
   215
        space_explode "/" name
wenzelm@52752
   216
        |> map Thy_Output.verb_text
wenzelm@52752
   217
        |> space_implode (Thy_Output.verb_text "/" ^ "\\discretionary{}{}{}")
wenzelm@53171
   218
      end));
wenzelm@49244
   219
wenzelm@6168
   220
end;