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