src/Pure/PIDE/resources.ML
author wenzelm
Sat Dec 16 21:53:07 2017 +0100 (22 months ago ago)
changeset 67219 81e9804b2014
parent 67217 53867014e299
child 67386 998e01d6f8fd
permissions -rw-r--r--
added document antiquotation @{session name};
renamed protocol function "Prover.session_base" to "Prover.init_session_base" according to the ML/Scala operation;
     1 (*  Title:      Pure/PIDE/resources.ML
     2     Author:     Makarius
     3 
     4 Resources for theories and auxiliary files.
     5 *)
     6 
     7 signature RESOURCES =
     8 sig
     9   val default_qualifier: string
    10   val init_session_base:
    11     {sessions: string list,
    12      global_theories: (string * string) list,
    13      loaded_theories: string list,
    14      known_theories: (string * string) list} -> unit
    15   val finish_session_base: unit -> unit
    16   val global_theory: string -> string option
    17   val loaded_theory: string -> bool
    18   val known_theory: string -> Path.T option
    19   val check_session: Proof.context -> string * Position.T -> string
    20   val master_directory: theory -> Path.T
    21   val imports_of: theory -> (string * Position.T) list
    22   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    23   val thy_path: Path.T -> Path.T
    24   val theory_qualifier: string -> string
    25   val import_name: string -> Path.T -> string ->
    26     {node_name: Path.T, master_dir: Path.T, theory_name: string}
    27   val check_thy: Path.T -> string ->
    28    {master: Path.T * SHA1.digest, text: string, theory_pos: Position.T,
    29     imports: (string * Position.T) list, keywords: Thy_Header.keywords}
    30   val parse_files: string -> (theory -> Token.file list) parser
    31   val provide: Path.T * SHA1.digest -> theory -> theory
    32   val provide_parse_files: string -> (theory -> Token.file list * theory) parser
    33   val loaded_files_current: theory -> bool
    34   val check_path: Proof.context -> Path.T -> string * Position.T -> Path.T
    35   val check_file: Proof.context -> Path.T -> string * Position.T -> Path.T
    36   val check_dir: Proof.context -> Path.T -> string * Position.T -> Path.T
    37 end;
    38 
    39 structure Resources: RESOURCES =
    40 struct
    41 
    42 (* session base *)
    43 
    44 val default_qualifier = "Draft";
    45 
    46 val empty_session_base =
    47   {sessions = []: string list,
    48    global_theories = Symtab.empty: string Symtab.table,
    49    loaded_theories = Symtab.empty: unit Symtab.table,
    50    known_theories = Symtab.empty: Path.T Symtab.table};
    51 
    52 val global_session_base =
    53   Synchronized.var "Sessions.base" empty_session_base;
    54 
    55 fun init_session_base {sessions, global_theories, loaded_theories, known_theories} =
    56   Synchronized.change global_session_base
    57     (fn _ =>
    58       {sessions = sort_strings sessions,
    59        global_theories = Symtab.make global_theories,
    60        loaded_theories = Symtab.make_set loaded_theories,
    61        known_theories = Symtab.make (map (apsnd Path.explode) known_theories)});
    62 
    63 fun finish_session_base () =
    64   Synchronized.change global_session_base
    65     (fn {global_theories, loaded_theories, ...} =>
    66       {sessions = [],
    67        global_theories = global_theories,
    68        loaded_theories = loaded_theories,
    69        known_theories = #known_theories empty_session_base});
    70 
    71 fun get_session_base f = f (Synchronized.value global_session_base);
    72 
    73 fun global_theory a = Symtab.lookup (get_session_base #global_theories) a;
    74 fun loaded_theory a = Symtab.defined (get_session_base #loaded_theories) a;
    75 fun known_theory a = Symtab.lookup (get_session_base #known_theories) a;
    76 
    77 fun check_session ctxt (name, pos) =
    78   let val sessions = get_session_base #sessions in
    79     if member (op =) sessions name then
    80       (Context_Position.report ctxt pos (Markup.entity Markup.sessionN name); name)
    81     else
    82       let
    83         val completion =
    84           Completion.make (name, pos) (fn completed =>
    85               sessions
    86               |> filter completed
    87               |> map (fn a => (a, (Markup.sessionN, a))));
    88         val report = Markup.markup_report (Completion.reported_text completion);
    89       in error ("Bad session " ^ quote name ^ Position.here pos ^ report) end
    90   end;
    91 
    92 
    93 (* manage source files *)
    94 
    95 type files =
    96  {master_dir: Path.T,  (*master directory of theory source*)
    97   imports: (string * Position.T) list,  (*source specification of imports*)
    98   provided: (Path.T * SHA1.digest) list};  (*source path, digest*)
    99 
   100 fun make_files (master_dir, imports, provided): files =
   101  {master_dir = master_dir, imports = imports, provided = provided};
   102 
   103 structure Files = Theory_Data
   104 (
   105   type T = files;
   106   val empty = make_files (Path.current, [], []);
   107   fun extend _ = empty;
   108   fun merge _ = empty;
   109 );
   110 
   111 fun map_files f =
   112   Files.map (fn {master_dir, imports, provided} =>
   113     make_files (f (master_dir, imports, provided)));
   114 
   115 
   116 val master_directory = #master_dir o Files.get;
   117 val imports_of = #imports o Files.get;
   118 
   119 fun begin_theory master_dir {name, imports, keywords} parents =
   120   Theory.begin_theory name parents
   121   |> map_files (fn _ => (master_dir, imports, []))
   122   |> Thy_Header.add_keywords keywords;
   123 
   124 
   125 (* theory files *)
   126 
   127 val thy_path = Path.ext "thy";
   128 
   129 fun theory_qualifier theory =
   130   (case global_theory theory of
   131     SOME qualifier => qualifier
   132   | NONE => Long_Name.qualifier theory);
   133 
   134 fun theory_name qualifier theory =
   135   if Long_Name.is_qualified theory orelse is_some (global_theory theory)
   136   then theory
   137   else Long_Name.qualify qualifier theory;
   138 
   139 fun import_name qualifier dir s =
   140   let val theory = theory_name qualifier (Thy_Header.import_name s) in
   141     if loaded_theory theory
   142     then {node_name = Path.basic theory, master_dir = Path.current, theory_name = theory}
   143     else
   144       let
   145         val node_name =
   146           (case known_theory theory of
   147             SOME node_name => node_name
   148           | NONE =>
   149               if Thy_Header.is_base_name s andalso Long_Name.is_qualified s
   150               then Path.explode s
   151               else File.full_path dir (thy_path (Path.expand (Path.explode s))));
   152       in {node_name = node_name, master_dir = Path.dir node_name, theory_name = theory} end
   153   end;
   154 
   155 fun check_file dir file = File.check_file (File.full_path dir file);
   156 
   157 fun check_thy dir thy_name =
   158   let
   159     val thy_base_name = Long_Name.base_name thy_name;
   160     val master_file =
   161       (case known_theory thy_name of
   162         SOME known_path => check_file Path.current known_path
   163       | NONE => check_file dir (thy_path (Path.basic thy_base_name)));
   164     val text = File.read master_file;
   165 
   166     val {name = (name, pos), imports, keywords} =
   167       Thy_Header.read (Path.position master_file) text;
   168     val _ =
   169       thy_base_name <> name andalso
   170         error ("Bad theory name " ^ quote name ^
   171           " for file " ^ Path.print (Path.base master_file) ^ Position.here pos);
   172   in
   173    {master = (master_file, SHA1.digest text), text = text, theory_pos = pos,
   174     imports = imports, keywords = keywords}
   175   end;
   176 
   177 
   178 (* load files *)
   179 
   180 fun parse_files cmd =
   181   Scan.ahead Parse.not_eof -- Parse.path >> (fn (tok, name) => fn thy =>
   182     (case Token.get_files tok of
   183       [] =>
   184         let
   185           val keywords = Thy_Header.get_keywords thy;
   186           val master_dir = master_directory thy;
   187           val pos = Token.pos_of tok;
   188           val src_paths = Keyword.command_files keywords cmd (Path.explode name);
   189         in map (Command.read_file master_dir pos) src_paths end
   190     | files => map Exn.release files));
   191 
   192 fun provide (src_path, id) =
   193   map_files (fn (master_dir, imports, provided) =>
   194     if AList.defined (op =) provided src_path then
   195       error ("Duplicate use of source file: " ^ Path.print src_path)
   196     else (master_dir, imports, (src_path, id) :: provided));
   197 
   198 fun provide_parse_files cmd =
   199   parse_files cmd >> (fn files => fn thy =>
   200     let
   201       val fs = files thy;
   202       val thy' = fold (fn {src_path, digest, ...} => provide (src_path, digest)) fs thy;
   203     in (fs, thy') end);
   204 
   205 fun load_file thy src_path =
   206   let
   207     val full_path = check_file (master_directory thy) src_path;
   208     val text = File.read full_path;
   209     val id = SHA1.digest text;
   210   in ((full_path, id), text) end;
   211 
   212 fun loaded_files_current thy =
   213   #provided (Files.get thy) |>
   214     forall (fn (src_path, id) =>
   215       (case try (load_file thy) src_path of
   216         NONE => false
   217       | SOME ((_, id'), _) => id = id'));
   218 
   219 
   220 (* formal check *)
   221 
   222 fun formal_check check_file ctxt dir (name, pos) =
   223   let
   224     fun err msg = error (msg ^ Position.here pos);
   225 
   226     val _ = Context_Position.report ctxt pos Markup.language_path;
   227     val path = Path.append dir (Path.explode name) handle ERROR msg => err msg;
   228     val _ = Path.expand path handle ERROR msg => err msg;
   229     val _ = Context_Position.report ctxt pos (Markup.path (Path.smart_implode path));
   230     val _ = check_file path handle ERROR msg => err msg;
   231   in path end;
   232 
   233 val check_path = formal_check I;
   234 val check_file = formal_check File.check_file;
   235 val check_dir = formal_check File.check_dir;
   236 
   237 
   238 (* antiquotations *)
   239 
   240 local
   241 
   242 fun document_antiq check ctxt (name, pos) =
   243   let
   244     val dir = master_directory (Proof_Context.theory_of ctxt);
   245     val _ = check ctxt dir (name, pos);
   246   in
   247     space_explode "/" name
   248     |> map Latex.output_ascii
   249     |> space_implode (Latex.output_ascii "/" ^ "\\discretionary{}{}{}")
   250     |> enclose "\\isatt{" "}"
   251   end;
   252 
   253 fun ML_antiq check ctxt (name, pos) =
   254   let val path = check ctxt Path.current (name, pos);
   255   in "Path.explode " ^ ML_Syntax.print_string (Path.implode path) end;
   256 
   257 in
   258 
   259 val _ = Theory.setup
   260  (Thy_Output.antiquotation \<^binding>\<open>session\<close> (Scan.lift (Parse.position Parse.embedded))
   261     (fn {context = ctxt, ...} => Thy_Output.verbatim_text ctxt o check_session ctxt) #>
   262   Thy_Output.antiquotation \<^binding>\<open>path\<close> (Scan.lift (Parse.position Parse.path))
   263     (document_antiq check_path o #context) #>
   264   Thy_Output.antiquotation \<^binding>\<open>file\<close> (Scan.lift (Parse.position Parse.path))
   265     (document_antiq check_file o #context) #>
   266   Thy_Output.antiquotation \<^binding>\<open>dir\<close> (Scan.lift (Parse.position Parse.path))
   267     (document_antiq check_dir o #context) #>
   268   ML_Antiquotation.value \<^binding>\<open>path\<close>
   269     (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_path)) #>
   270   ML_Antiquotation.value \<^binding>\<open>file\<close>
   271     (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_file)) #>
   272   ML_Antiquotation.value \<^binding>\<open>dir\<close>
   273     (Args.context -- Scan.lift (Parse.position Parse.path) >> uncurry (ML_antiq check_dir)));
   274 
   275 end;
   276 
   277 end;