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