src/Pure/PIDE/resources.ML
author wenzelm
Fri Apr 07 19:35:39 2017 +0200 (2017-04-07 ago)
changeset 65431 4a3e6cda3b94
parent 65058 3e9f382fb67e
child 65433 a260181505c1
permissions -rw-r--r--
provide session base for "isabelle build" and "isabelle console" ML process;
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@54525
    85
    val path = thy_path (Path.basic thy_name);
wenzelm@54525
    86
    val master_file = check_file dir path;
wenzelm@54525
    87
    val text = File.read master_file;
wenzelm@54525
    88
wenzelm@54525
    89
    val {name = (name, pos), imports, keywords} =
wenzelm@54525
    90
      Thy_Header.read (Path.position master_file) text;
wenzelm@54525
    91
    val _ = thy_name <> name andalso
wenzelm@59718
    92
      error ("Bad theory name " ^ quote name ^
wenzelm@59718
    93
        " for file " ^ Path.print path ^ Position.here pos);
wenzelm@54525
    94
  in
wenzelm@54525
    95
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm@54525
    96
    imports = imports, keywords = keywords}
wenzelm@54525
    97
  end;
wenzelm@54525
    98
wenzelm@54525
    99
wenzelm@54526
   100
(* load files *)
wenzelm@48898
   101
wenzelm@48898
   102
fun parse_files cmd =
wenzelm@48898
   103
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
   104
    (case Token.get_files tok of
wenzelm@54526
   105
      [] =>
wenzelm@54526
   106
        let
wenzelm@58928
   107
          val keywords = Thy_Header.get_keywords thy;
wenzelm@54526
   108
          val master_dir = master_directory thy;
wenzelm@55708
   109
          val pos = Token.pos_of tok;
wenzelm@58923
   110
          val src_paths = Keyword.command_files keywords cmd (Path.explode name);
wenzelm@54526
   111
        in map (Command.read_file master_dir pos) src_paths end
wenzelm@54520
   112
    | files => map Exn.release files));
wenzelm@48869
   113
wenzelm@48906
   114
fun provide (src_path, id) =
wenzelm@48906
   115
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
   116
    if AList.defined (op =) provided src_path then
wenzelm@48906
   117
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
   118
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
   119
wenzelm@48906
   120
fun provide_parse_files cmd =
wenzelm@48906
   121
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   122
    let
wenzelm@48906
   123
      val fs = files thy;
wenzelm@55788
   124
      val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
wenzelm@48906
   125
    in (fs, thy') end);
wenzelm@48906
   126
wenzelm@43702
   127
fun load_file thy src_path =
wenzelm@43702
   128
  let
wenzelm@43702
   129
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   130
    val text = File.read full_path;
wenzelm@43702
   131
    val id = SHA1.digest text;
wenzelm@43702
   132
  in ((full_path, id), text) end;
wenzelm@43702
   133
wenzelm@54446
   134
fun loaded_files_current thy =
wenzelm@48886
   135
  #provided (Files.get thy) |>
wenzelm@48896
   136
    forall (fn (src_path, id) =>
wenzelm@43702
   137
      (case try (load_file thy) src_path of
wenzelm@37949
   138
        NONE => false
wenzelm@48886
   139
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   140
wenzelm@37949
   141
wenzelm@54525
   142
(* load theory *)
wenzelm@37977
   143
wenzelm@51294
   144
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@37977
   145
  Theory.begin_theory name parents
wenzelm@48888
   146
  |> put_deps master_dir imports
wenzelm@58928
   147
  |> Thy_Header.add_keywords keywords;
wenzelm@37977
   148
wenzelm@58923
   149
fun excursion keywords master_dir last_timing init elements =
wenzelm@46959
   150
  let
wenzelm@58928
   151
    fun prepare_span st span =
wenzelm@57905
   152
      Command_Span.content span
wenzelm@59689
   153
      |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
wenzelm@51268
   154
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@51217
   155
wenzelm@51268
   156
    fun element_result span_elem (st, _) =
wenzelm@46959
   157
      let
wenzelm@58928
   158
        val elem = Thy_Syntax.map_element (prepare_span st) span_elem;
wenzelm@58923
   159
        val (results, st') = Toplevel.element_result keywords elem st;
wenzelm@51273
   160
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@51268
   161
      in (results, (st', pos')) end;
wenzelm@46959
   162
wenzelm@46959
   163
    val (results, (end_state, end_pos)) =
wenzelm@46959
   164
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@46959
   165
wenzelm@46959
   166
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@51273
   167
  in (results, thy) end;
wenzelm@46959
   168
wenzelm@61381
   169
fun load_thy document symbols last_timing update_time master_dir header text_pos text parents =
wenzelm@43712
   170
  let
wenzelm@58918
   171
    val (name, _) = #name header;
wenzelm@58928
   172
    val keywords =
wenzelm@58928
   173
      fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
wenzelm@58928
   174
        (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
wenzelm@43712
   175
wenzelm@59083
   176
    val toks = Token.explode keywords text_pos text;
wenzelm@57905
   177
    val spans = Outer_Syntax.parse_spans toks;
wenzelm@58923
   178
    val elements = Thy_Syntax.parse_elements keywords spans;
wenzelm@43712
   179
wenzelm@54456
   180
    fun init () =
wenzelm@54456
   181
      begin_theory master_dir header parents
wenzelm@54456
   182
      |> Present.begin_theory update_time
wenzelm@61381
   183
        (fn () => implode (map (HTML.present_span symbols keywords) spans));
wenzelm@43712
   184
wenzelm@54526
   185
    val (results, thy) =
wenzelm@58849
   186
      cond_timeit true ("theory " ^ quote name)
wenzelm@58923
   187
        (fn () => excursion keywords master_dir last_timing init elements);
wenzelm@43712
   188
wenzelm@51331
   189
    fun present () =
wenzelm@51331
   190
      let
wenzelm@51332
   191
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@51331
   192
      in
wenzelm@51556
   193
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@51556
   194
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@51556
   195
        else
wenzelm@58928
   196
          let val tex_source = Thy_Output.present_thy thy res toks |> Buffer.content;
wenzelm@54458
   197
          in if document then Present.theory_output name tex_source else () end
wenzelm@51331
   198
      end;
wenzelm@43712
   199
wenzelm@51331
   200
  in (thy, present, size text) end;
wenzelm@43712
   201
wenzelm@42002
   202
wenzelm@56135
   203
(* antiquotations *)
wenzelm@49244
   204
wenzelm@56135
   205
local
wenzelm@56135
   206
wenzelm@63669
   207
fun err msg pos = error (msg ^ Position.here pos);
wenzelm@63669
   208
wenzelm@63675
   209
fun check_path check_file ctxt dir (name, pos) =
wenzelm@54705
   210
  let
wenzelm@56034
   211
    val _ = Context_Position.report ctxt pos Markup.language_path;
wenzelm@56034
   212
wenzelm@63669
   213
    val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
wenzelm@63669
   214
    val _ = Path.expand path handle ERROR msg => err msg pos;
wenzelm@56134
   215
    val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
wenzelm@63669
   216
    val _ =
wenzelm@63669
   217
      (case check_file of
wenzelm@63669
   218
        NONE => path
wenzelm@63669
   219
      | SOME check => (check path handle ERROR msg => err msg pos));
wenzelm@63675
   220
  in path end;
wenzelm@63675
   221
wenzelm@63675
   222
fun document_antiq check_file ctxt (name, pos) =
wenzelm@63675
   223
  let
wenzelm@63675
   224
    val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@63675
   225
    val _ = check_path check_file ctxt dir (name, pos);
wenzelm@54705
   226
  in
wenzelm@54705
   227
    space_explode "/" name
wenzelm@58716
   228
    |> map Latex.output_ascii
wenzelm@58716
   229
    |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
wenzelm@58741
   230
    |> enclose "\\isatt{" "}"
wenzelm@54705
   231
  end;
wenzelm@54705
   232
wenzelm@63675
   233
fun ML_antiq check_file ctxt (name, pos) =
wenzelm@63675
   234
  let val path = check_path check_file ctxt Path.current (name, pos);
wenzelm@63675
   235
  in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
wenzelm@63675
   236
wenzelm@56135
   237
in
wenzelm@56135
   238
wenzelm@53171
   239
val _ = Theory.setup
wenzelm@63669
   240
 (Thy_Output.antiquotation @{binding path} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   241
    (document_antiq NONE o #context) #>
wenzelm@63669
   242
  Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   243
    (document_antiq (SOME File.check_file) o #context) #>
wenzelm@63669
   244
  Thy_Output.antiquotation @{binding dir} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   245
    (document_antiq (SOME File.check_dir) o #context) #>
wenzelm@63675
   246
  ML_Antiquotation.value @{binding path}
wenzelm@63675
   247
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   248
      >> uncurry (ML_antiq NONE)) #>
wenzelm@63669
   249
  ML_Antiquotation.value @{binding file}
wenzelm@63675
   250
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   251
      >> uncurry (ML_antiq (SOME File.check_file))) #>
wenzelm@63675
   252
  ML_Antiquotation.value @{binding dir}
wenzelm@63675
   253
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   254
      >> uncurry (ML_antiq (SOME File.check_dir))));
wenzelm@49244
   255
wenzelm@6168
   256
end;
wenzelm@56135
   257
wenzelm@56135
   258
end;