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