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