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