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