src/Pure/PIDE/resources.ML
author wenzelm
Fri Apr 21 14:09:03 2017 +0200 (2017-04-21 ago)
changeset 65532 febfd9f78bd4
parent 65505 741fad555d82
child 66711 80fa1401cf76
permissions -rw-r--r--
eliminated default_qualifier: just a constant;
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@65532
     9
  val default_qualifier: string
wenzelm@65478
    10
  val init_session_base:
wenzelm@65532
    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@65457
    15
  val global_theory: string -> string option
wenzelm@65471
    16
  val loaded_theory: string -> string option
wenzelm@65431
    17
  val known_theory: string -> Path.T option
wenzelm@37949
    18
  val master_directory: theory -> Path.T
wenzelm@48927
    19
  val imports_of: theory -> (string * Position.T) list
wenzelm@65505
    20
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
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@6168
    32
end;
wenzelm@6168
    33
wenzelm@56208
    34
structure Resources: RESOURCES =
wenzelm@6168
    35
struct
wenzelm@6168
    36
wenzelm@65431
    37
(* session base *)
wenzelm@65431
    38
wenzelm@65532
    39
val default_qualifier = "Draft";
wenzelm@65532
    40
wenzelm@65478
    41
val empty_session_base =
wenzelm@65532
    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@65532
    49
fun init_session_base {global_theories, loaded_theories, known_theories} =
wenzelm@65431
    50
  Synchronized.change global_session_base
wenzelm@65441
    51
    (fn _ =>
wenzelm@65532
    52
      {global_theories = Symtab.make global_theories,
wenzelm@65471
    53
       loaded_theories = Symtab.make loaded_theories,
wenzelm@65441
    54
       known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
wenzelm@65431
    55
wenzelm@65478
    56
fun finish_session_base () =
wenzelm@65478
    57
  Synchronized.change global_session_base
wenzelm@65478
    58
    (fn {global_theories, loaded_theories, ...} =>
wenzelm@65532
    59
      {global_theories = global_theories,
wenzelm@65478
    60
       loaded_theories = loaded_theories,
wenzelm@65478
    61
       known_theories = #known_theories empty_session_base});
wenzelm@65441
    62
wenzelm@65441
    63
fun get_session_base f = f (Synchronized.value global_session_base);
wenzelm@65431
    64
wenzelm@65457
    65
fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
wenzelm@65441
    66
fun loaded_theory a = Symtab.lookup (get_session_base #loaded_theories) a;
wenzelm@65441
    67
fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
wenzelm@65431
    68
wenzelm@65431
    69
wenzelm@37949
    70
(* manage source files *)
wenzelm@37949
    71
wenzelm@37949
    72
type files =
wenzelm@40741
    73
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@48927
    74
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
    75
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
    76
wenzelm@48886
    77
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
    78
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
    79
wenzelm@37949
    80
structure Files = Theory_Data
wenzelm@37949
    81
(
wenzelm@37949
    82
  type T = files;
wenzelm@48886
    83
  val empty = make_files (Path.current, [], []);
wenzelm@37949
    84
  fun extend _ = empty;
wenzelm@37949
    85
  fun merge _ = empty;
wenzelm@37949
    86
);
wenzelm@37949
    87
wenzelm@37949
    88
fun map_files f =
wenzelm@48886
    89
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
    90
    make_files (f (master_dir, imports, provided)));
wenzelm@37949
    91
wenzelm@37949
    92
wenzelm@37949
    93
val master_directory = #master_dir o Files.get;
wenzelm@41548
    94
val imports_of = #imports o Files.get;
wenzelm@37949
    95
wenzelm@65505
    96
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@65505
    97
  Theory.begin_theory name parents
wenzelm@65505
    98
  |> map_files (fn _ => (master_dir, imports, []))
wenzelm@65505
    99
  |> Thy_Header.add_keywords keywords;
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@65457
   107
  (case global_theory theory of
wenzelm@65457
   108
    SOME qualifier => qualifier
wenzelm@65457
   109
  | NONE => Long_Name.qualifier theory);
wenzelm@65445
   110
wenzelm@65476
   111
fun theory_name qualifier theory0 =
wenzelm@65471
   112
  (case loaded_theory theory0 of
wenzelm@65471
   113
    SOME theory => (true, theory)
wenzelm@65471
   114
  | NONE =>
wenzelm@65474
   115
      let val theory =
wenzelm@65471
   116
        if Long_Name.is_qualified theory0 orelse is_some (global_theory theory0)
wenzelm@65503
   117
        then theory0
wenzelm@65474
   118
        else Long_Name.qualify qualifier theory0
wenzelm@65474
   119
      in (false, theory) end);
wenzelm@65471
   120
wenzelm@65454
   121
fun import_name qualifier dir s =
wenzelm@65476
   122
  (case theory_name qualifier (Thy_Header.import_name s) of
wenzelm@65474
   123
    (true, theory) =>
wenzelm@65474
   124
      {node_name = Path.basic theory, master_dir = Path.current, theory_name = theory}
wenzelm@65474
   125
  | (false, theory) =>
wenzelm@65474
   126
      let val node_name =
wenzelm@65471
   127
        (case known_theory theory of
wenzelm@65471
   128
          SOME node_name => node_name
wenzelm@65474
   129
        | NONE => File.full_path dir (thy_path (Path.expand (Path.explode s))))
wenzelm@65474
   130
      in {node_name = node_name, master_dir = Path.dir node_name, theory_name = theory} end);
wenzelm@65443
   131
wenzelm@48878
   132
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
   133
wenzelm@54525
   134
fun check_thy dir thy_name =
wenzelm@54525
   135
  let
wenzelm@65433
   136
    val thy_base_name = Long_Name.base_name thy_name;
wenzelm@65433
   137
    val master_file =
wenzelm@65433
   138
      (case known_theory thy_name of
wenzelm@65442
   139
        SOME known_path => check_file Path.current known_path
wenzelm@65442
   140
      | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
wenzelm@54525
   141
    val text = File.read master_file;
wenzelm@54525
   142
wenzelm@54525
   143
    val {name = (name, pos), imports, keywords} =
wenzelm@54525
   144
      Thy_Header.read (Path.position master_file) text;
wenzelm@65433
   145
    val _ =
wenzelm@65433
   146
      thy_base_name <> name andalso
wenzelm@65433
   147
        error ("Bad theory name " ^ quote name ^
wenzelm@65442
   148
          " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
wenzelm@54525
   149
  in
wenzelm@54525
   150
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm@54525
   151
    imports = imports, keywords = keywords}
wenzelm@54525
   152
  end;
wenzelm@54525
   153
wenzelm@54525
   154
wenzelm@54526
   155
(* load files *)
wenzelm@48898
   156
wenzelm@48898
   157
fun parse_files cmd =
wenzelm@48898
   158
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
   159
    (case Token.get_files tok of
wenzelm@54526
   160
      [] =>
wenzelm@54526
   161
        let
wenzelm@58928
   162
          val keywords = Thy_Header.get_keywords thy;
wenzelm@54526
   163
          val master_dir = master_directory thy;
wenzelm@55708
   164
          val pos = Token.pos_of tok;
wenzelm@58923
   165
          val src_paths = Keyword.command_files keywords cmd (Path.explode name);
wenzelm@54526
   166
        in map (Command.read_file master_dir pos) src_paths end
wenzelm@54520
   167
    | files => map Exn.release files));
wenzelm@48869
   168
wenzelm@48906
   169
fun provide (src_path, id) =
wenzelm@48906
   170
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
   171
    if AList.defined (op =) provided src_path then
wenzelm@48906
   172
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
   173
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
   174
wenzelm@48906
   175
fun provide_parse_files cmd =
wenzelm@48906
   176
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   177
    let
wenzelm@48906
   178
      val fs = files thy;
wenzelm@55788
   179
      val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
wenzelm@48906
   180
    in (fs, thy') end);
wenzelm@48906
   181
wenzelm@43702
   182
fun load_file thy src_path =
wenzelm@43702
   183
  let
wenzelm@43702
   184
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   185
    val text = File.read full_path;
wenzelm@43702
   186
    val id = SHA1.digest text;
wenzelm@43702
   187
  in ((full_path, id), text) end;
wenzelm@43702
   188
wenzelm@54446
   189
fun loaded_files_current thy =
wenzelm@48886
   190
  #provided (Files.get thy) |>
wenzelm@48896
   191
    forall (fn (src_path, id) =>
wenzelm@43702
   192
      (case try (load_file thy) src_path of
wenzelm@37949
   193
        NONE => false
wenzelm@48886
   194
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   195
wenzelm@37949
   196
wenzelm@56135
   197
(* antiquotations *)
wenzelm@49244
   198
wenzelm@56135
   199
local
wenzelm@56135
   200
wenzelm@63669
   201
fun err msg pos = error (msg ^ Position.here pos);
wenzelm@63669
   202
wenzelm@63675
   203
fun check_path check_file ctxt dir (name, pos) =
wenzelm@54705
   204
  let
wenzelm@56034
   205
    val _ = Context_Position.report ctxt pos Markup.language_path;
wenzelm@56034
   206
wenzelm@63669
   207
    val path = Path.append dir (Path.explode name) handle ERROR msg => err msg pos;
wenzelm@63669
   208
    val _ = Path.expand path handle ERROR msg => err msg pos;
wenzelm@56134
   209
    val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
wenzelm@63669
   210
    val _ =
wenzelm@63669
   211
      (case check_file of
wenzelm@63669
   212
        NONE => path
wenzelm@63669
   213
      | SOME check => (check path handle ERROR msg => err msg pos));
wenzelm@63675
   214
  in path end;
wenzelm@63675
   215
wenzelm@63675
   216
fun document_antiq check_file ctxt (name, pos) =
wenzelm@63675
   217
  let
wenzelm@63675
   218
    val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@63675
   219
    val _ = check_path check_file ctxt dir (name, pos);
wenzelm@54705
   220
  in
wenzelm@54705
   221
    space_explode "/" name
wenzelm@58716
   222
    |> map Latex.output_ascii
wenzelm@58716
   223
    |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
wenzelm@58741
   224
    |> enclose "\\isatt{" "}"
wenzelm@54705
   225
  end;
wenzelm@54705
   226
wenzelm@63675
   227
fun ML_antiq check_file ctxt (name, pos) =
wenzelm@63675
   228
  let val path = check_path check_file ctxt Path.current (name, pos);
wenzelm@63675
   229
  in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
wenzelm@63675
   230
wenzelm@56135
   231
in
wenzelm@56135
   232
wenzelm@53171
   233
val _ = Theory.setup
wenzelm@63669
   234
 (Thy_Output.antiquotation @{binding path} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   235
    (document_antiq NONE o #context) #>
wenzelm@63669
   236
  Thy_Output.antiquotation @{binding file} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   237
    (document_antiq (SOME File.check_file) o #context) #>
wenzelm@63669
   238
  Thy_Output.antiquotation @{binding dir} (Scan.lift (Parse.position Parse.path))
wenzelm@63675
   239
    (document_antiq (SOME File.check_dir) o #context) #>
wenzelm@63675
   240
  ML_Antiquotation.value @{binding path}
wenzelm@63675
   241
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   242
      >> uncurry (ML_antiq NONE)) #>
wenzelm@63669
   243
  ML_Antiquotation.value @{binding file}
wenzelm@63675
   244
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   245
      >> uncurry (ML_antiq (SOME File.check_file))) #>
wenzelm@63675
   246
  ML_Antiquotation.value @{binding dir}
wenzelm@63675
   247
    (Args.context -- Scan.lift (Parse.position Parse.path)
wenzelm@63675
   248
      >> uncurry (ML_antiq (SOME File.check_dir))));
wenzelm@49244
   249
wenzelm@6168
   250
end;
wenzelm@56135
   251
wenzelm@56135
   252
end;