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