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