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