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