src/Pure/PIDE/resources.ML
author wenzelm
Tue Apr 18 16:34:58 2017 +0200 (2017-04-18 ago)
changeset 65505 741fad555d82
parent 65503 a3fffad8f217
child 65532 febfd9f78bd4
permissions -rw-r--r--
exclude theories from other sessions;
clarified modules;
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@65505
    21
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@46737
    22
  val thy_path: Path.T -> Path.T
wenzelm@65445
    23
  val theory_qualifier: string -> string
wenzelm@65454
    24
  val import_name: string -> Path.T -> string ->
wenzelm@65454
    25
    {node_name: Path.T, master_dir: Path.T, theory_name: string}
wenzelm@48898
    26
  val check_thy: Path.T -> string ->
wenzelm@48928
    27
   {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
wenzelm@51293
    28
    imports: (string * Position.T) list, keywords: Thy_Header.keywords}
wenzelm@54525
    29
  val parse_files: string -> (theory -> Token.file list) parser
wenzelm@48906
    30
  val provide: Path.T * SHA1.digest -> theory -> theory
wenzelm@48906
    31
  val provide_parse_files: string -> (theory -> Token.file list * theory) parser
wenzelm@54446
    32
  val loaded_files_current: theory -> bool
wenzelm@6168
    33
end;
wenzelm@6168
    34
wenzelm@56208
    35
structure Resources: RESOURCES =
wenzelm@6168
    36
struct
wenzelm@6168
    37
wenzelm@65431
    38
(* session base *)
wenzelm@65431
    39
wenzelm@65478
    40
val empty_session_base =
wenzelm@65445
    41
  {default_qualifier = "Draft",
wenzelm@65457
    42
   global_theories = Symtab.empty: string Symtab.table,
wenzelm@65471
    43
   loaded_theories = Symtab.empty: string Symtab.table,
wenzelm@65441
    44
   known_theories = Symtab.empty: Path.T Symtab.table};
wenzelm@65431
    45
wenzelm@65441
    46
val global_session_base =
wenzelm@65478
    47
  Synchronized.var "Sessions.base" empty_session_base;
wenzelm@65441
    48
wenzelm@65478
    49
fun init_session_base {default_qualifier, global_theories, loaded_theories, known_theories} =
wenzelm@65431
    50
  Synchronized.change global_session_base
wenzelm@65441
    51
    (fn _ =>
wenzelm@65445
    52
      {default_qualifier =
wenzelm@65445
    53
        if default_qualifier <> "" then default_qualifier
wenzelm@65478
    54
        else #default_qualifier empty_session_base,
wenzelm@65457
    55
       global_theories = Symtab.make global_theories,
wenzelm@65471
    56
       loaded_theories = Symtab.make loaded_theories,
wenzelm@65441
    57
       known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
wenzelm@65431
    58
wenzelm@65478
    59
fun finish_session_base () =
wenzelm@65478
    60
  Synchronized.change global_session_base
wenzelm@65478
    61
    (fn {global_theories, loaded_theories, ...} =>
wenzelm@65478
    62
      {default_qualifier = #default_qualifier empty_session_base,
wenzelm@65478
    63
       global_theories = global_theories,
wenzelm@65478
    64
       loaded_theories = loaded_theories,
wenzelm@65478
    65
       known_theories = #known_theories empty_session_base});
wenzelm@65441
    66
wenzelm@65441
    67
fun get_session_base f = f (Synchronized.value global_session_base);
wenzelm@65431
    68
wenzelm@65441
    69
fun default_qualifier () = get_session_base #default_qualifier;
wenzelm@65457
    70
fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
wenzelm@65441
    71
fun loaded_theory a = Symtab.lookup (get_session_base #loaded_theories) a;
wenzelm@65441
    72
fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
wenzelm@65431
    73
wenzelm@65431
    74
wenzelm@37949
    75
(* manage source files *)
wenzelm@37949
    76
wenzelm@37949
    77
type files =
wenzelm@40741
    78
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@48927
    79
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
    80
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    81
wenzelm@48886
    82
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    83
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    84
wenzelm@37949
    85
structure Files = Theory_Data
wenzelm@37949
    86
(
wenzelm@37949
    87
  type T = files;
wenzelm@48886
    88
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    89
  fun extend _ = empty;
wenzelm@37949
    90
  fun merge _ = empty;
wenzelm@37949
    91
);
wenzelm@37949
    92
wenzelm@37949
    93
fun map_files f =
wenzelm@48886
    94
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    95
    make_files (f (master_dir, imports, provided)));
wenzelm@37949
    96
wenzelm@37949
    97
wenzelm@37949
    98
val master_directory = #master_dir o Files.get;
wenzelm@41548
    99
val imports_of = #imports o Files.get;
wenzelm@37949
   100
wenzelm@65505
   101
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@65505
   102
  Theory.begin_theory name parents
wenzelm@65505
   103
  |> map_files (fn _ => (master_dir, imports, []))
wenzelm@65505
   104
  |> Thy_Header.add_keywords keywords;
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@65503
   122
        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@56135
   202
(* antiquotations *)
wenzelm@49244
   203
wenzelm@56135
   204
local
wenzelm@56135
   205
wenzelm@63669
   206
fun err msg pos = error (msg ^ Position.here pos);
wenzelm@63669
   207
wenzelm@63675
   208
fun check_path check_file ctxt dir (name, pos) =
wenzelm@54705
   209
  let
wenzelm@56034
   210
    val _ = Context_Position.report ctxt pos Markup.language_path;
wenzelm@56034
   211
wenzelm@63669
   212
    val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
wenzelm@63669
   213
    val _ = Path.expand path handle ERROR msg => err msg pos;
wenzelm@56134
   214
    val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
wenzelm@63669
   215
    val _ =
wenzelm@63669
   216
      (case check_file of
wenzelm@63669
   217
        NONE => path
wenzelm@63669
   218
      | SOME check => (check path handle ERROR msg => err msg pos));
wenzelm@63675
   219
  in path end;
wenzelm@63675
   220
wenzelm@63675
   221
fun document_antiq check_file ctxt (name, pos) =
wenzelm@63675
   222
  let
wenzelm@63675
   223
    val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@63675
   224
    val _ = check_path check_file ctxt dir (name, pos);
wenzelm@54705
   225
  in
wenzelm@54705
   226
    space_explode "/" name
wenzelm@58716
   227
    |> map Latex.output_ascii
wenzelm@58716
   228
    |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
wenzelm@58741
   229
    |> enclose "\\isatt{" "}"
wenzelm@54705
   230
  end;
wenzelm@54705
   231
wenzelm@63675
   232
fun ML_antiq check_file ctxt (name, pos) =
wenzelm@63675
   233
  let val path = check_path check_file ctxt Path.current (name, pos);
wenzelm@63675
   234
  in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
wenzelm@63675
   235
wenzelm@56135
   236
in
wenzelm@56135
   237
wenzelm@53171
   238
val _ = Theory.setup
wenzelm@63669
   239
 (Thy_Output.antiquotation @{binding path} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   240
    (document_antiq NONE o #context) #>
wenzelm@63669
   241
  Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   242
    (document_antiq (SOME File.check_file) o #context) #>
wenzelm@63669
   243
  Thy_Output.antiquotation @{binding dir} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   244
    (document_antiq (SOME File.check_dir) o #context) #>
wenzelm@63675
   245
  ML_Antiquotation.value @{binding path}
wenzelm@63675
   246
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   247
      >> uncurry (ML_antiq NONE)) #>
wenzelm@63669
   248
  ML_Antiquotation.value @{binding file}
wenzelm@63675
   249
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   250
      >> uncurry (ML_antiq (SOME File.check_file))) #>
wenzelm@63675
   251
  ML_Antiquotation.value @{binding dir}
wenzelm@63675
   252
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   253
      >> uncurry (ML_antiq (SOME File.check_dir))));
wenzelm@49244
   254
wenzelm@6168
   255
end;
wenzelm@56135
   256
wenzelm@56135
   257
end;