src/Pure/PIDE/resources.ML
author wenzelm
Mon Nov 05 15:00:55 2018 +0100 (9 months ago ago)
changeset 69243 d98cfb369cbd
parent 68092 7c8ed28dd40a
child 69286 599b6d0d199b
permissions -rw-r--r--
prefer symbolic path: it is potentially persisted;
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@67493
    11
    {sessions: (string * Properties.T) list,
wenzelm@67493
    12
     docs: string list,
wenzelm@67219
    13
     global_theories: (string * string) list,
wenzelm@66712
    14
     loaded_theories: string list,
wenzelm@65441
    15
     known_theories: (string * string) list} -> unit
wenzelm@65478
    16
  val finish_session_base: unit -> unit
wenzelm@65457
    17
  val global_theory: string -> string option
wenzelm@66712
    18
  val loaded_theory: string -> bool
wenzelm@65431
    19
  val known_theory: string -> Path.T option
wenzelm@67219
    20
  val check_session: Proof.context -> string * Position.T -> string
wenzelm@67472
    21
  val check_doc: Proof.context -> string * Position.T -> string
wenzelm@37949
    22
  val master_directory: theory -> Path.T
wenzelm@48927
    23
  val imports_of: theory -> (string * Position.T) list
wenzelm@65505
    24
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
wenzelm@46737
    25
  val thy_path: Path.T -> Path.T
wenzelm@65445
    26
  val theory_qualifier: string -> string
wenzelm@67215
    27
  val import_name: string -> Path.T -> string ->
wenzelm@67215
    28
    {node_name: Path.T, master_dir: Path.T, theory_name: string}
wenzelm@48898
    29
  val check_thy: Path.T -> string ->
wenzelm@48928
    30
   {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
wenzelm@51293
    31
    imports: (string * Position.T) list, keywords: Thy_Header.keywords}
wenzelm@54525
    32
  val parse_files: string -> (theory -> Token.file list) parser
wenzelm@48906
    33
  val provide: Path.T * SHA1.digest -> theory -> theory
wenzelm@48906
    34
  val provide_parse_files: string -> (theory -> Token.file list * theory) parser
wenzelm@54446
    35
  val loaded_files_current: theory -> bool
wenzelm@67209
    36
  val check_path: Proof.context -> Path.T -> string * Position.T -> Path.T
wenzelm@67209
    37
  val check_file: Proof.context -> Path.T -> string * Position.T -> Path.T
wenzelm@67209
    38
  val check_dir: Proof.context -> Path.T -> string * Position.T -> Path.T
wenzelm@6168
    39
end;
wenzelm@6168
    40
wenzelm@56208
    41
structure Resources: RESOURCES =
wenzelm@6168
    42
struct
wenzelm@6168
    43
wenzelm@65431
    44
(* session base *)
wenzelm@65431
    45
wenzelm@65532
    46
val default_qualifier = "Draft";
wenzelm@65532
    47
wenzelm@67493
    48
type entry = {pos: Position.T, serial: serial};
wenzelm@67493
    49
wenzelm@67493
    50
fun make_entry props : entry =
wenzelm@67493
    51
  {pos = Position.of_properties props, serial = serial ()};
wenzelm@67493
    52
wenzelm@65478
    53
val empty_session_base =
wenzelm@67493
    54
  {sessions = []: (string * entry) list,
wenzelm@67493
    55
   docs = []: (string * entry) list,
wenzelm@67219
    56
   global_theories = Symtab.empty: string Symtab.table,
wenzelm@66712
    57
   loaded_theories = Symtab.empty: unit Symtab.table,
wenzelm@65441
    58
   known_theories = Symtab.empty: Path.T Symtab.table};
wenzelm@65431
    59
wenzelm@65441
    60
val global_session_base =
wenzelm@65478
    61
  Synchronized.var "Sessions.base" empty_session_base;
wenzelm@65441
    62
wenzelm@67493
    63
fun init_session_base {sessions, docs, global_theories, loaded_theories, known_theories} =
wenzelm@65431
    64
  Synchronized.change global_session_base
wenzelm@65441
    65
    (fn _ =>
wenzelm@67493
    66
      {sessions = sort_by #1 (map (apsnd make_entry) sessions),
wenzelm@67493
    67
       docs = sort_by #1 (map (apsnd make_entry o rpair []) docs),
wenzelm@67219
    68
       global_theories = Symtab.make global_theories,
wenzelm@66712
    69
       loaded_theories = Symtab.make_set loaded_theories,
wenzelm@65441
    70
       known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
wenzelm@65431
    71
wenzelm@65478
    72
fun finish_session_base () =
wenzelm@65478
    73
  Synchronized.change global_session_base
wenzelm@65478
    74
    (fn {global_theories, loaded_theories, ...} =>
wenzelm@67219
    75
      {sessions = [],
wenzelm@67493
    76
       docs = [],
wenzelm@67219
    77
       global_theories = global_theories,
wenzelm@65478
    78
       loaded_theories = loaded_theories,
wenzelm@65478
    79
       known_theories = #known_theories empty_session_base});
wenzelm@65441
    80
wenzelm@65441
    81
fun get_session_base f = f (Synchronized.value global_session_base);
wenzelm@65431
    82
wenzelm@65457
    83
fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
wenzelm@66712
    84
fun loaded_theory a = Symtab.defined (get_session_base #loaded_theories) a;
wenzelm@65441
    85
fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
wenzelm@65431
    86
wenzelm@67472
    87
fun check_name which kind markup ctxt (name, pos) =
wenzelm@67493
    88
  let val entries = get_session_base which in
wenzelm@67493
    89
    (case AList.lookup (op =) entries name of
wenzelm@67493
    90
      SOME entry => (Context_Position.report ctxt pos (markup name entry); name)
wenzelm@67493
    91
    | NONE =>
wenzelm@67493
    92
        let
wenzelm@67493
    93
          val completion =
wenzelm@67493
    94
            Completion.make (name, pos) (fn completed =>
wenzelm@67493
    95
                entries
wenzelm@67493
    96
                |> map #1
wenzelm@67493
    97
                |> filter completed
wenzelm@67493
    98
                |> sort_strings
wenzelm@67493
    99
                |> map (fn a => (a, (kind, a))));
wenzelm@67493
   100
          val report = Markup.markup_report (Completion.reported_text completion);
wenzelm@67493
   101
        in error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos ^ report) end)
wenzelm@67219
   102
  end;
wenzelm@67219
   103
wenzelm@67493
   104
fun markup_session name {pos, serial} =
wenzelm@67493
   105
  Markup.properties
wenzelm@67493
   106
    (Position.entity_properties_of false serial pos) (Markup.entity Markup.sessionN name);
wenzelm@67493
   107
wenzelm@67493
   108
val check_session = check_name #sessions "session" markup_session;
wenzelm@67493
   109
val check_doc = check_name #docs "documentation" (fn name => fn _ => Markup.doc name);
wenzelm@67472
   110
wenzelm@67472
   111
wenzelm@65431
   112
wenzelm@37949
   113
(* manage source files *)
wenzelm@37949
   114
wenzelm@37949
   115
type files =
wenzelm@40741
   116
 {master_dir: Path.T,  (*master directory of theory source*)
wenzelm@48927
   117
  imports: (string * Position.T) list,  (*source specification of imports*)
wenzelm@48896
   118
  provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
wenzelm@37949
   119
wenzelm@48886
   120
fun make_files (master_dir, imports, provided): files =
wenzelm@48886
   121
 {master_dir = master_dir, imports = imports, provided = provided};
wenzelm@37949
   122
wenzelm@37949
   123
structure Files = Theory_Data
wenzelm@37949
   124
(
wenzelm@37949
   125
  type T = files;
wenzelm@48886
   126
  val empty = make_files (Path.current, [], []);
wenzelm@37949
   127
  fun extend _ = empty;
wenzelm@37949
   128
  fun merge _ = empty;
wenzelm@37949
   129
);
wenzelm@37949
   130
wenzelm@37949
   131
fun map_files f =
wenzelm@48886
   132
  Files.map (fn {master_dir, imports, provided} =>
wenzelm@48886
   133
    make_files (f (master_dir, imports, provided)));
wenzelm@37949
   134
wenzelm@37949
   135
wenzelm@37949
   136
val master_directory = #master_dir o Files.get;
wenzelm@41548
   137
val imports_of = #imports o Files.get;
wenzelm@37949
   138
wenzelm@65505
   139
fun begin_theory master_dir {name, imports, keywords} parents =
wenzelm@65505
   140
  Theory.begin_theory name parents
wenzelm@69243
   141
  |> map_files (fn _ => (Path.explode (Path.smart_implode master_dir), imports, []))
wenzelm@65505
   142
  |> Thy_Header.add_keywords keywords;
wenzelm@37949
   143
wenzelm@37949
   144
wenzelm@54525
   145
(* theory files *)
wenzelm@54525
   146
wenzelm@54525
   147
val thy_path = Path.ext "thy";
wenzelm@48869
   148
wenzelm@65445
   149
fun theory_qualifier theory =
wenzelm@65457
   150
  (case global_theory theory of
wenzelm@65457
   151
    SOME qualifier => qualifier
wenzelm@65457
   152
  | NONE => Long_Name.qualifier theory);
wenzelm@65445
   153
wenzelm@66712
   154
fun theory_name qualifier theory =
wenzelm@67104
   155
  if Long_Name.is_qualified theory orelse is_some (global_theory theory)
wenzelm@67104
   156
  then theory
wenzelm@67104
   157
  else Long_Name.qualify qualifier theory;
wenzelm@65471
   158
wenzelm@65454
   159
fun import_name qualifier dir s =
wenzelm@67104
   160
  let val theory = theory_name qualifier (Thy_Header.import_name s) in
wenzelm@67215
   161
    if loaded_theory theory
wenzelm@67215
   162
    then {node_name = Path.basic theory, master_dir = Path.current, theory_name = theory}
wenzelm@67104
   163
    else
wenzelm@67104
   164
      let
wenzelm@67104
   165
        val node_name =
wenzelm@67104
   166
          (case known_theory theory of
wenzelm@67104
   167
            SOME node_name => node_name
wenzelm@67104
   168
          | NONE =>
wenzelm@67104
   169
              if Thy_Header.is_base_name s andalso Long_Name.is_qualified s
wenzelm@67104
   170
              then Path.explode s
wenzelm@67104
   171
              else File.full_path dir (thy_path (Path.expand (Path.explode s))));
wenzelm@67215
   172
      in {node_name = node_name, master_dir = Path.dir node_name, theory_name = theory} end
wenzelm@67104
   173
  end;
wenzelm@65443
   174
wenzelm@48878
   175
fun check_file dir file = File.check_file (File.full_path dir file);
wenzelm@48878
   176
wenzelm@54525
   177
fun check_thy dir thy_name =
wenzelm@54525
   178
  let
wenzelm@65433
   179
    val thy_base_name = Long_Name.base_name thy_name;
wenzelm@65433
   180
    val master_file =
wenzelm@65433
   181
      (case known_theory thy_name of
wenzelm@65442
   182
        SOME known_path => check_file Path.current known_path
wenzelm@65442
   183
      | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
wenzelm@54525
   184
    val text = File.read master_file;
wenzelm@54525
   185
wenzelm@54525
   186
    val {name = (name, pos), imports, keywords} =
wenzelm@54525
   187
      Thy_Header.read (Path.position master_file) text;
wenzelm@65433
   188
    val _ =
wenzelm@65433
   189
      thy_base_name <> name andalso
wenzelm@65433
   190
        error ("Bad theory name " ^ quote name ^
wenzelm@65442
   191
          " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
wenzelm@54525
   192
  in
wenzelm@54525
   193
   {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
wenzelm@54525
   194
    imports = imports, keywords = keywords}
wenzelm@54525
   195
  end;
wenzelm@54525
   196
wenzelm@54525
   197
wenzelm@54526
   198
(* load files *)
wenzelm@48898
   199
wenzelm@48898
   200
fun parse_files cmd =
wenzelm@48898
   201
  Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
wenzelm@48898
   202
    (case Token.get_files tok of
wenzelm@54526
   203
      [] =>
wenzelm@54526
   204
        let
wenzelm@58928
   205
          val keywords = Thy_Header.get_keywords thy;
wenzelm@54526
   206
          val master_dir = master_directory thy;
wenzelm@55708
   207
          val pos = Token.pos_of tok;
wenzelm@58923
   208
          val src_paths = Keyword.command_files keywords cmd (Path.explode name);
wenzelm@54526
   209
        in map (Command.read_file master_dir pos) src_paths end
wenzelm@54520
   210
    | files => map Exn.release files));
wenzelm@48869
   211
wenzelm@48906
   212
fun provide (src_path, id) =
wenzelm@48906
   213
  map_files (fn (master_dir, imports, provided) =>
wenzelm@48906
   214
    if AList.defined (op =) provided src_path then
wenzelm@48906
   215
      error ("Duplicate use of source file: " ^ Path.print src_path)
wenzelm@48906
   216
    else (master_dir, imports, (src_path, id) :: provided));
wenzelm@48906
   217
wenzelm@48906
   218
fun provide_parse_files cmd =
wenzelm@48906
   219
  parse_files cmd >> (fn files => fn thy =>
wenzelm@48906
   220
    let
wenzelm@48906
   221
      val fs = files thy;
wenzelm@55788
   222
      val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
wenzelm@48906
   223
    in (fs, thy') end);
wenzelm@48906
   224
wenzelm@43702
   225
fun load_file thy src_path =
wenzelm@43702
   226
  let
wenzelm@43702
   227
    val full_path = check_file (master_directory thy) src_path;
wenzelm@43702
   228
    val text = File.read full_path;
wenzelm@43702
   229
    val id = SHA1.digest text;
wenzelm@43702
   230
  in ((full_path, id), text) end;
wenzelm@43702
   231
wenzelm@54446
   232
fun loaded_files_current thy =
wenzelm@48886
   233
  #provided (Files.get thy) |>
wenzelm@48896
   234
    forall (fn (src_path, id) =>
wenzelm@43702
   235
      (case try (load_file thy) src_path of
wenzelm@37949
   236
        NONE => false
wenzelm@48886
   237
      | SOME ((_, id'), _) => id = id'));
wenzelm@37952
   238
wenzelm@37949
   239
wenzelm@67217
   240
(* formal check *)
wenzelm@49244
   241
wenzelm@67217
   242
fun formal_check check_file ctxt dir (name, pos) =
wenzelm@54705
   243
  let
wenzelm@67214
   244
    fun err msg = error (msg ^ Position.here pos);
wenzelm@56034
   245
wenzelm@67214
   246
    val _ = Context_Position.report ctxt pos Markup.language_path;
wenzelm@67214
   247
    val path = Path.append dir (Path.explode name) handle ERROR msg => err msg;
wenzelm@67214
   248
    val _ = Path.expand path handle ERROR msg => err msg;
wenzelm@56134
   249
    val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
wenzelm@67214
   250
    val _ = check_file path handle ERROR msg => err msg;
wenzelm@63675
   251
  in path end;
wenzelm@63675
   252
wenzelm@67217
   253
val check_path = formal_check I;
wenzelm@67217
   254
val check_file = formal_check File.check_file;
wenzelm@67217
   255
val check_dir = formal_check File.check_dir;
wenzelm@67217
   256
wenzelm@67217
   257
wenzelm@67217
   258
(* antiquotations *)
wenzelm@67217
   259
wenzelm@67217
   260
local
wenzelm@67217
   261
wenzelm@67209
   262
fun document_antiq check ctxt (name, pos) =
wenzelm@63675
   263
  let
wenzelm@63675
   264
    val dir = master_directory (Proof_Context.theory_of ctxt);
wenzelm@67209
   265
    val _ = check ctxt dir (name, pos);
wenzelm@67464
   266
    val latex =
wenzelm@67464
   267
      space_explode "/" name
wenzelm@67464
   268
      |> map Latex.output_ascii
wenzelm@67464
   269
      |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}");
wenzelm@67464
   270
  in Latex.enclose_block "\\isatt{" "}" [Latex.string latex] end;
wenzelm@54705
   271
wenzelm@67209
   272
fun ML_antiq check ctxt (name, pos) =
wenzelm@67209
   273
  let val path = check ctxt Path.current (name, pos);
wenzelm@63675
   274
  in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
wenzelm@63675
   275
wenzelm@56135
   276
in
wenzelm@56135
   277
wenzelm@53171
   278
val _ = Theory.setup
wenzelm@67464
   279
 (Thy_Output.antiquotation_verbatim \<^binding>\<open>session\<close>
wenzelm@67464
   280
    (Scan.lift (Parse.position Parse.embedded)) check_session #>
wenzelm@67472
   281
  Thy_Output.antiquotation_verbatim \<^binding>\<open>doc\<close>
wenzelm@67472
   282
    (Scan.lift (Parse.position Parse.embedded)) check_doc #>
wenzelm@67464
   283
  Thy_Output.antiquotation_raw \<^binding>\<open>path\<close>
wenzelm@67464
   284
    (Scan.lift (Parse.position Parse.path)) (document_antiq check_path) #>
wenzelm@67464
   285
  Thy_Output.antiquotation_raw \<^binding>\<open>file\<close>
wenzelm@67464
   286
    (Scan.lift (Parse.position Parse.path)) (document_antiq check_file) #>
wenzelm@67464
   287
  Thy_Output.antiquotation_raw \<^binding>\<open>dir\<close>
wenzelm@67464
   288
    (Scan.lift (Parse.position Parse.path)) (document_antiq check_dir) #>
wenzelm@67147
   289
  ML_Antiquotation.value \<^binding>\<open>path\<close>
wenzelm@67209
   290
    (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_path)) #>
wenzelm@67147
   291
  ML_Antiquotation.value \<^binding>\<open>file\<close>
wenzelm@67209
   292
    (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_file)) #>
wenzelm@67147
   293
  ML_Antiquotation.value \<^binding>\<open>dir\<close>
wenzelm@67209
   294
    (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_dir)));
wenzelm@49244
   295
wenzelm@6168
   296
end;
wenzelm@56135
   297
wenzelm@56135
   298
end;