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