src/Pure/PIDE/resources.ML
author wenzelm
Mon Feb 27 16:29:52 2017 +0100 (2017-02-27 ago)
changeset 65058 3e9f382fb67e
parent 63675 e217525d6b64
child 65431 4a3e6cda3b94
permissions -rw-r--r--
absent timing information means zero, according to 0070053570c4, f235646b1b73;
wenzelm@56208
     1
(*  Title:      Pure/PIDE/resources.ML
wenzelm@42002
     2
    Author:     Makarius
wenzelm@6168
     3
wenzelm@56208
     4
Resources for theories and auxiliary files.
wenzelm@6168
     5
*)
wenzelm@6168
     6
wenzelm@56208
     7
signature RESOURCES =
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@54446
    18
  val loaded_files_current: theory -> bool
wenzelm@46938
    19
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@65058
    20
  val load_thy: bool -> HTML.symbols -> (Toplevel.transition -> Time.time) -> int ->
wenzelm@61381
    21
    Path.T -> Thy_Header.header -> Position.T -> string -> theory list ->
wenzelm@61381
    22
    theory * (unit -> unit) * int
wenzelm@6168
    23
end;
wenzelm@6168
    24
wenzelm@56208
    25
structure Resources: RESOURCES =
wenzelm@6168
    26
struct
wenzelm@6168
    27
wenzelm@37949
    28
(* manage source files *)
wenzelm@37949
    29
wenzelm@37949
    30
type files =
wenzelm@40741
    31
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@48927
    32
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
    33
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    34
wenzelm@48886
    35
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    36
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    37
wenzelm@37949
    38
structure Files = Theory_Data
wenzelm@37949
    39
(
wenzelm@37949
    40
  type T = files;
wenzelm@48886
    41
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    42
  fun extend _ = empty;
wenzelm@37949
    43
  fun merge _ = empty;
wenzelm@37949
    44
);
wenzelm@37949
    45
wenzelm@37949
    46
fun map_files f =
wenzelm@48886
    47
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    48
    make_files (f (master_dir, imports, provided)));
wenzelm@37949
    49
wenzelm@37949
    50
wenzelm@37949
    51
val master_directory = #master_dir o Files.get;
wenzelm@41548
    52
val imports_of = #imports o Files.get;
wenzelm@37949
    53
wenzelm@48888
    54
fun put_deps master_dir imports = map_files (fn _ => (master_dir, imports, []));
wenzelm@37949
    55
wenzelm@37949
    56
wenzelm@54525
    57
(* theory files *)
wenzelm@54525
    58
wenzelm@54525
    59
val thy_path = Path.ext "thy";
wenzelm@48869
    60
wenzelm@48878
    61
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
    62
wenzelm@54525
    63
fun check_thy dir thy_name =
wenzelm@54525
    64
  let
wenzelm@54525
    65
    val path = thy_path (Path.basic thy_name);
wenzelm@54525
    66
    val master_file = check_file dir path;
wenzelm@54525
    67
    val text = File.read master_file;
wenzelm@54525
    68
wenzelm@54525
    69
    val {name = (name, pos), imports, keywords} =
wenzelm@54525
    70
      Thy_Header.read (Path.position master_file) text;
wenzelm@54525
    71
    val _ = thy_name <> name andalso
wenzelm@59718
    72
      error ("Bad theory name " ^ quote name ^
wenzelm@59718
    73
        " for file " ^ Path.print path ^ 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@58928
    87
          val keywords = Thy_Header.get_keywords thy;
wenzelm@54526
    88
          val master_dir = master_directory thy;
wenzelm@55708
    89
          val pos = Token.pos_of tok;
wenzelm@58923
    90
          val src_paths = Keyword.command_files keywords cmd (Path.explode name);
wenzelm@54526
    91
        in map (Command.read_file master_dir pos) src_paths end
wenzelm@54520
    92
    | files => map Exn.release files));
wenzelm@48869
    93
wenzelm@48906
    94
fun provide (src_path, id) =
wenzelm@48906
    95
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
    96
    if AList.defined (op =) provided src_path then
wenzelm@48906
    97
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
    98
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
    99
wenzelm@48906
   100
fun provide_parse_files cmd =
wenzelm@48906
   101
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   102
    let
wenzelm@48906
   103
      val fs = files thy;
wenzelm@55788
   104
      val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
wenzelm@48906
   105
    in (fs, thy') end);
wenzelm@48906
   106
wenzelm@43702
   107
fun load_file thy src_path =
wenzelm@43702
   108
  let
wenzelm@43702
   109
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   110
    val text = File.read full_path;
wenzelm@43702
   111
    val id = SHA1.digest text;
wenzelm@43702
   112
  in ((full_path, id), text) end;
wenzelm@43702
   113
wenzelm@54446
   114
fun loaded_files_current thy =
wenzelm@48886
   115
  #provided (Files.get thy) |>
wenzelm@48896
   116
    forall (fn (src_path, id) =>
wenzelm@43702
   117
      (case try (load_file thy) src_path of
wenzelm@37949
   118
        NONE => false
wenzelm@48886
   119
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   120
wenzelm@37949
   121
wenzelm@54525
   122
(* load theory *)
wenzelm@37977
   123
wenzelm@51294
   124
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@37977
   125
  Theory.begin_theory name parents
wenzelm@48888
   126
  |> put_deps master_dir imports
wenzelm@58928
   127
  |> Thy_Header.add_keywords keywords;
wenzelm@37977
   128
wenzelm@58923
   129
fun excursion keywords master_dir last_timing init elements =
wenzelm@46959
   130
  let
wenzelm@58928
   131
    fun prepare_span st span =
wenzelm@57905
   132
      Command_Span.content span
wenzelm@59689
   133
      |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
wenzelm@51268
   134
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@51217
   135
wenzelm@51268
   136
    fun element_result span_elem (st, _) =
wenzelm@46959
   137
      let
wenzelm@58928
   138
        val elem = Thy_Syntax.map_element (prepare_span st) span_elem;
wenzelm@58923
   139
        val (results, st') = Toplevel.element_result keywords elem st;
wenzelm@51273
   140
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@51268
   141
      in (results, (st', pos')) end;
wenzelm@46959
   142
wenzelm@46959
   143
    val (results, (end_state, end_pos)) =
wenzelm@46959
   144
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   145
wenzelm@46959
   146
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@51273
   147
  in (results, thy) end;
wenzelm@46959
   148
wenzelm@61381
   149
fun load_thy document symbols last_timing update_time master_dir header text_pos text parents =
wenzelm@43712
   150
  let
wenzelm@58918
   151
    val (name, _) = #name header;
wenzelm@58928
   152
    val keywords =
wenzelm@58928
   153
      fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
wenzelm@58928
   154
        (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
wenzelm@43712
   155
wenzelm@59083
   156
    val toks = Token.explode keywords text_pos text;
wenzelm@57905
   157
    val spans = Outer_Syntax.parse_spans toks;
wenzelm@58923
   158
    val elements = Thy_Syntax.parse_elements keywords spans;
wenzelm@43712
   159
wenzelm@54456
   160
    fun init () =
wenzelm@54456
   161
      begin_theory master_dir header parents
wenzelm@54456
   162
      |> Present.begin_theory update_time
wenzelm@61381
   163
        (fn () => implode (map (HTML.present_span symbols keywords) spans));
wenzelm@43712
   164
wenzelm@54526
   165
    val (results, thy) =
wenzelm@58849
   166
      cond_timeit true ("theory " ^ quote name)
wenzelm@58923
   167
        (fn () => excursion keywords master_dir last_timing init elements);
wenzelm@43712
   168
wenzelm@51331
   169
    fun present () =
wenzelm@51331
   170
      let
wenzelm@51332
   171
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@51331
   172
      in
wenzelm@51556
   173
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@51556
   174
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@51556
   175
        else
wenzelm@58928
   176
          let val tex_source = Thy_Output.present_thy thy res toks |> Buffer.content;
wenzelm@54458
   177
          in if document then Present.theory_output name tex_source else () end
wenzelm@51331
   178
      end;
wenzelm@43712
   179
wenzelm@51331
   180
  in (thy, present, size text) end;
wenzelm@43712
   181
wenzelm@42002
   182
wenzelm@56135
   183
(* antiquotations *)
wenzelm@49244
   184
wenzelm@56135
   185
local
wenzelm@56135
   186
wenzelm@63669
   187
fun err msg pos = error (msg ^ Position.here pos);
wenzelm@63669
   188
wenzelm@63675
   189
fun check_path check_file ctxt dir (name, pos) =
wenzelm@54705
   190
  let
wenzelm@56034
   191
    val _ = Context_Position.report ctxt pos Markup.language_path;
wenzelm@56034
   192
wenzelm@63669
   193
    val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
wenzelm@63669
   194
    val _ = Path.expand path handle ERROR msg => err msg pos;
wenzelm@56134
   195
    val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
wenzelm@63669
   196
    val _ =
wenzelm@63669
   197
      (case check_file of
wenzelm@63669
   198
        NONE => path
wenzelm@63669
   199
      | SOME check => (check path handle ERROR msg => err msg pos));
wenzelm@63675
   200
  in path end;
wenzelm@63675
   201
wenzelm@63675
   202
fun document_antiq check_file ctxt (name, pos) =
wenzelm@63675
   203
  let
wenzelm@63675
   204
    val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@63675
   205
    val _ = check_path check_file ctxt dir (name, pos);
wenzelm@54705
   206
  in
wenzelm@54705
   207
    space_explode "/" name
wenzelm@58716
   208
    |> map Latex.output_ascii
wenzelm@58716
   209
    |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
wenzelm@58741
   210
    |> enclose "\\isatt{" "}"
wenzelm@54705
   211
  end;
wenzelm@54705
   212
wenzelm@63675
   213
fun ML_antiq check_file ctxt (name, pos) =
wenzelm@63675
   214
  let val path = check_path check_file ctxt Path.current (name, pos);
wenzelm@63675
   215
  in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
wenzelm@63675
   216
wenzelm@56135
   217
in
wenzelm@56135
   218
wenzelm@53171
   219
val _ = Theory.setup
wenzelm@63669
   220
 (Thy_Output.antiquotation @{binding path} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   221
    (document_antiq NONE o #context) #>
wenzelm@63669
   222
  Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   223
    (document_antiq (SOME File.check_file) o #context) #>
wenzelm@63669
   224
  Thy_Output.antiquotation @{binding dir} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   225
    (document_antiq (SOME File.check_dir) o #context) #>
wenzelm@63675
   226
  ML_Antiquotation.value @{binding path}
wenzelm@63675
   227
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   228
      >> uncurry (ML_antiq NONE)) #>
wenzelm@63669
   229
  ML_Antiquotation.value @{binding file}
wenzelm@63675
   230
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   231
      >> uncurry (ML_antiq (SOME File.check_file))) #>
wenzelm@63675
   232
  ML_Antiquotation.value @{binding dir}
wenzelm@63675
   233
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   234
      >> uncurry (ML_antiq (SOME File.check_dir))));
wenzelm@49244
   235
wenzelm@6168
   236
end;
wenzelm@56135
   237
wenzelm@56135
   238
end;