src/Pure/Thy/browser_info.ML
author wenzelm
Wed Sep 01 21:06:57 1999 +0200 (1999-09-01)
changeset 7409 f8ce7b832598
parent 7236 e077484d50d8
child 7572 6e6dafacbc28
permissions -rw-r--r--
added theorems;
improved files;
     1 (*  Title:      Pure/Thy/browser_info.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4 
     5 Theory browsing information (HTML and graph files).
     6 *)
     7 
     8 signature BASIC_BROWSER_INFO =
     9 sig
    10   val section: string -> unit
    11 end;
    12 
    13 signature BROWSER_INFO =
    14 sig
    15   include BASIC_BROWSER_INFO
    16   val init: bool -> string list -> string -> Url.T option * bool -> unit
    17   val finish: unit -> unit
    18   val theory_source: string -> (string, 'a) Source.source -> unit
    19   val begin_theory: string -> string list -> (Path.T * bool) list -> theory -> theory
    20   val end_theory: string -> unit
    21   val theorem: string -> thm -> unit
    22   val theorems: string -> thm list -> unit
    23   val setup: (theory -> theory) list
    24 end;
    25 
    26 structure BrowserInfo: BROWSER_INFO =
    27 struct
    28 
    29 
    30 (** paths **)
    31 
    32 val output_path = Path.variable "ISABELLE_BROWSER_INFO";
    33 
    34 fun top_path sess_path offset = Path.append (Path.appends (replicate (offset + length sess_path) Path.parent));
    35 
    36 val html_ext = Path.ext "html";
    37 val html_path = html_ext o Path.basic;
    38 val graph_path = Path.ext "graph" o Path.basic;
    39 val index_path = Path.basic "index.html";
    40 
    41 val session_path = Path.basic ".session";
    42 val session_entries_path = Path.unpack ".session/entries";
    43 val pre_index_path = Path.unpack ".session/pre-index";
    44 
    45 
    46 
    47 (** additional theory data **)
    48 
    49 structure BrowserInfoArgs =
    50 struct
    51   val name = "Pure/browser_info";
    52   type T = {session: string list, is_local: bool};
    53 
    54   val empty = {session = [], is_local = false};
    55   val copy = I;
    56   val prep_ext  = I;
    57   fun merge _ = empty;
    58   fun print _ _ = ();
    59 end;
    60 
    61 structure BrowserInfoData = TheoryDataFun(BrowserInfoArgs);
    62 
    63 fun get_info thy =
    64   if Theory.eq_thy (thy, ProtoPure.thy) then BrowserInfoArgs.empty
    65   else BrowserInfoData.get thy;
    66 
    67 
    68 
    69 (** graphs **)
    70 
    71 type graph_node =
    72   {name: string, ID: string, dir: string, unfold: bool,
    73    path: string, parents: string list};
    74 
    75 fun get_graph path = map (fn (a :: b :: c :: d :: e :: r) =>
    76   {name = unenclose a, ID = unenclose b,
    77    dir = unenclose c, unfold = (d = "+"),
    78    path = unenclose (if d = "+" then e else d),
    79    parents = map unenclose (fst (split_last (if d = "+" then tl r else r)))})
    80      (map (filter_out (equal "") o space_explode " ") (split_lines (File.read path)));
    81 
    82 fun put_graph gr path = File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
    83   "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
    84   path ^ "\" > " ^ space_implode " " (map quote parents) ^ " ;") gr));
    85 
    86 fun dir_of sess = space_implode "/" (case sess of
    87   [] => ["Pure"] | [x] => [x, "base"] | xs => xs);
    88 
    89 fun ID_of sess s = dir_of sess ^ "/" ^ s;
    90 
    91 (* retrieve graph data from theory loader database *)
    92 
    93 fun make_graph remote_path unfold curr_sess = map (fn name =>
    94   let
    95     val {session, is_local} = get_info (ThyInfo.theory name);
    96     val path' = Path.append (Path.make session) (html_path name)
    97   in
    98     {name = name, ID = ID_of session name, dir = dir_of session,
    99      path = if null session then "" else
   100             if is_some remote_path andalso not is_local then
   101               Url.pack (Url.append (the remote_path) (Url.file path'))
   102             else Path.pack (top_path curr_sess 2 path'),
   103      unfold = unfold andalso (length session = 1),
   104      parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s)
   105        (ThyInfo.get_preds name)}
   106   end) (ThyInfo.names ());
   107 
   108 fun add_graph_entry' (entry as {ID, ...}) (gr : graph_node list) =
   109   filter_out (fn entry' => #ID entry' = ID) gr @ [entry];
   110 
   111 
   112 
   113 (** global browser info state **)
   114 
   115 (* type theory_info *)
   116 
   117 type theory_info = {source: Buffer.T, html: Buffer.T};
   118 
   119 fun make_theory_info (source, html) =
   120   {source = source, html = html}: theory_info;
   121 
   122 val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty);
   123 fun map_theory_info f {source, html} = make_theory_info (f (source, html));
   124 
   125  
   126 (* type browser_info *)
   127 
   128 type browser_info = {theories: theory_info Symtab.table, index: Buffer.T,
   129   graph: graph_node list, all_graph: graph_node list};
   130 
   131 fun make_browser_info (theories, index, graph, all_graph) =
   132   {theories = theories, index = index, graph = graph, all_graph = all_graph}: browser_info;
   133 
   134 val empty_browser_info = make_browser_info (Symtab.empty, Buffer.empty, [], []);
   135 
   136 fun initial_browser_info remote_path graph_path curr_sess = make_browser_info
   137   (Symtab.empty, Buffer.empty, make_graph remote_path false curr_sess,
   138    if_none (try get_graph graph_path) (make_graph remote_path true [""]));
   139 
   140 fun map_browser_info f {theories, index, graph, all_graph} =
   141   make_browser_info (f (theories, index, graph, all_graph));
   142 
   143 
   144 (* state *)
   145 
   146 val browser_info = ref empty_browser_info;
   147 
   148 fun change_browser_info f = browser_info := map_browser_info f (! browser_info);
   149 
   150 fun init_theory_info name info = change_browser_info (fn (theories, index, graph, all_graph) =>
   151   (Symtab.update ((name, info), theories), index, graph, all_graph));
   152 
   153 fun change_theory_info name f = change_browser_info (fn (theories, index, graph, all_graph) =>
   154   (case Symtab.lookup (theories, name) of
   155     None => (warning ("Browser info: cannot access theory document " ^ quote name);
   156       (theories, index, graph, all_graph))
   157   | Some info => (Symtab.update ((name, map_theory_info f info), theories), index, graph, all_graph)));
   158 
   159 
   160 fun add_index txt = change_browser_info (fn (theories, index, graph, all_graph) =>
   161   (theories, Buffer.add txt index, graph, all_graph));
   162 
   163 (*add entry to graph for current session*)
   164 fun add_graph_entry e = change_browser_info (fn (theories, index, graph, all_graph) =>
   165   (theories, index, add_graph_entry' e graph, all_graph));
   166 
   167 (*add entry to graph for all sessions*)
   168 fun add_all_graph_entry e = change_browser_info (fn (theories, index, graph, all_graph) =>
   169   (theories, index, graph, add_graph_entry' e all_graph));
   170 
   171 fun add_source name txt = change_theory_info name (fn (source, html) =>
   172   (Buffer.add txt source, html));
   173 
   174 fun add_html name txt = change_theory_info name (fn (source, html) =>
   175   (source, Buffer.add txt html));
   176 
   177 
   178 
   179 (** global session state **)
   180 
   181 (* session_info *)
   182 
   183 type session_info =
   184   {name: string, parent: string, session: string, path: string list,
   185     html_prefix: Path.T, graph_path: Path.T, all_graph_path: Path.T, remote_path: Url.T option};
   186 
   187 fun make_session_info (name, parent, session, path, html_prefix, graph_path, all_graph_path, remote_path) =
   188   {name = name, parent = parent, session = session, path = path,
   189     html_prefix = html_prefix, graph_path = graph_path,
   190     all_graph_path = all_graph_path, remote_path = remote_path}: session_info;
   191 
   192 
   193 (* state *)
   194 
   195 val session_info = ref (None: session_info option);
   196 
   197 fun with_session x f = (case ! session_info of None => x | Some info => f info);
   198 fun with_context f = f (PureThy.get_name (Context.the_context ()));
   199 
   200 
   201 
   202 (** HTML output **)
   203 
   204 (* maintain index *)
   205 
   206 val session_entries =
   207   HTML.session_entries o map (fn name => (Url.file (Path.append (Path.basic name) index_path), name));
   208 
   209 fun get_entries dir =
   210   split_lines (File.read (Path.append dir session_entries_path));
   211 
   212 fun put_entries entries dir =
   213   File.write (Path.append dir session_entries_path) (cat_lines entries);
   214 
   215 
   216 fun create_index dir =
   217   File.read (Path.append dir pre_index_path) ^
   218     session_entries (get_entries dir) ^ HTML.end_index
   219   |> File.write (Path.append dir index_path);
   220 
   221 fun update_index dir name =
   222   (case try get_entries dir of
   223     None => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
   224   | Some es => (put_entries ((es \ name) @ [name]) dir; create_index dir));
   225 
   226 
   227 (* init session *)
   228 
   229 fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
   230 
   231 fun could_copy inpath outpath =
   232   if File.exists inpath then (File.copy inpath outpath; true)
   233   else false;
   234 
   235 fun init false _ _ _ = (browser_info := empty_browser_info; session_info := None)
   236   | init true path name (remote_path, first_time) =
   237       let
   238         val parent_name = name_of_session (take (length path - 1, path));
   239         val session_name = name_of_session path;
   240         val sess_prefix = Path.make path;
   241         val out_path = Path.expand output_path;
   242         val html_prefix = Path.append out_path sess_prefix;
   243         val graph_prefix = Path.appends [out_path, Path.unpack "graph/data", sess_prefix];
   244         val graph_path = Path.append graph_prefix (Path.basic "session.graph");
   245         val graph_lnk = Url.file (top_path path 0 (Path.appends
   246           [Path.unpack "graph/data", sess_prefix, Path.basic "medium.html"]));
   247         val graph_up_lnk = (Url.file (top_path path 2 (Path.append sess_prefix index_path)), session_name);
   248         val codebase = Url.file (top_path path 1 Path.current);
   249         val all_graph_path = Path.appends [out_path, Path.unpack "graph/data",
   250           Path.basic (hd path), Path.basic "all_sessions.graph"];
   251 
   252         val _ = (File.mkdir html_prefix; File.mkdir graph_prefix);
   253         fun prep_readme readme =
   254           let val readme_path = Path.basic readme in
   255             if could_copy readme_path (Path.append html_prefix readme_path) then Some readme_path
   256             else None
   257           end;
   258         val opt_readme =
   259           (case prep_readme "README.html" of None => prep_readme "README" | some => some);
   260 
   261         val parent_index_path = Path.append Path.parent index_path;
   262         val index_up_lnk = if first_time then
   263             Url.append (the remote_path) (Url.file (Path.append sess_prefix parent_index_path))
   264           else Url.file parent_index_path;
   265 
   266         val index_text = HTML.begin_index (index_up_lnk, parent_name)
   267           (Url.file index_path, session_name) (apsome Url.file opt_readme) graph_lnk;
   268       in
   269         File.mkdir (Path.append html_prefix session_path);
   270         File.write (Path.append html_prefix session_entries_path) "";
   271         seq (fn (name, txt) => File.write (Path.append graph_prefix (Path.basic name)) txt)
   272           (HTML.applet_pages session_name codebase graph_up_lnk (length path = 1));
   273         session_info := Some (make_session_info (name, parent_name, session_name, path,
   274           html_prefix, graph_path, all_graph_path, remote_path));
   275         browser_info := initial_browser_info remote_path all_graph_path path;
   276         add_index index_text
   277       end;
   278 
   279 
   280 (* finish session *)
   281 
   282 fun finish () = with_session () (fn {name, html_prefix, graph_path, all_graph_path, path, ...} =>
   283   let
   284     val {theories, index, graph, all_graph} = ! browser_info;
   285 
   286     fun finish_node (a, {source = _, html}) =
   287       Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html);
   288   in
   289     seq finish_node (Symtab.dest theories);
   290     Buffer.write (Path.append html_prefix pre_index_path) index;
   291     put_graph graph graph_path;
   292     put_graph all_graph all_graph_path;
   293     create_index html_prefix;
   294     update_index (Path.append html_prefix Path.parent) name;
   295     browser_info := empty_browser_info;
   296     session_info := None
   297   end);
   298 
   299 
   300 (* theory elements *)
   301 
   302 fun theory_source name src = with_session () (fn _ =>
   303   (init_theory_info name empty_theory_info; add_source name (HTML.source src)));
   304 
   305 
   306 (* FIXME clean *)
   307 
   308 fun parent_link remote_path curr_session name =
   309   let
   310     val {session, is_local} = get_info (ThyInfo.theory name);
   311     val path = Path.append (Path.make session) (html_path name)
   312   in (if null session then None else
   313      Some (if is_some remote_path andalso not is_local then
   314        Url.append (the remote_path) (Url.file path)
   315      else Url.file (top_path curr_session 0 path)), name)
   316   end;
   317 
   318 fun begin_theory name raw_parents orig_files thy =
   319   with_session thy (fn {session, path, html_prefix, remote_path, ...} =>
   320   let
   321     val parents = map (parent_link remote_path path) raw_parents;
   322     val ml_path = ThyLoad.ml_path name;
   323     val files = map (apsnd Some) orig_files @
   324       (if is_some (ThyLoad.check_file ml_path) then [(ml_path, None)] else []);
   325 
   326     fun prep_file (raw_path, loadit) =
   327       (case ThyLoad.check_file raw_path of
   328         Some (path, _) =>
   329           let
   330             val base = Path.base path;
   331             val base_html = html_ext base;
   332           in
   333             File.write (Path.append html_prefix base_html) (HTML.ml_file (Url.file base) (File.read path));
   334             (Some (Url.file base_html), Url.file raw_path, loadit)
   335           end
   336       | None => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
   337           (None, Url.file raw_path, loadit)));
   338 
   339     val files_html = map prep_file files;
   340 
   341     fun prep_source (source, html) =
   342       let val txt =
   343         HTML.begin_theory (Url.file index_path, session) name parents files_html (Buffer.content source)
   344       in (Buffer.empty, Buffer.add txt html) end;
   345 
   346     fun make_entry unfold all =
   347       {name = name, ID = ID_of path name, dir = dir_of path, unfold = unfold,
   348        path = Path.pack (top_path (if all then [""] else path) 2
   349          (Path.append (Path.make path) (html_path name))),
   350        parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) raw_parents};
   351 
   352   in
   353     change_theory_info name prep_source;
   354     add_all_graph_entry (make_entry (length path = 1) true);
   355     add_graph_entry (make_entry true false);
   356     add_index (HTML.theory_entry (Url.file (html_path name), name));
   357     BrowserInfoData.put {session = path, is_local = is_some remote_path} thy
   358   end);
   359 
   360 fun end_theory _ = ();
   361 
   362 fun theorem name thm = with_session () (fn _ => with_context add_html (HTML.theorem name thm));
   363 fun theorems name thms = with_session () (fn _ => with_context add_html (HTML.theorems name thms));
   364 fun section s = with_session () (fn _ => with_context add_html (HTML.section s));
   365 
   366 
   367 
   368 (** theory setup **)
   369 
   370 val setup = [BrowserInfoData.init];
   371 
   372 
   373 end;