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