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