src/Pure/Thy/present.ML
author wenzelm
Mon Feb 23 14:50:30 2015 +0100 (2015-02-23)
changeset 59564 fdc03c8daacc
parent 59448 149d2bc5ddb6
child 60082 d3573eb7728f
permissions -rw-r--r--
Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm@6203
     1
(*  Title:      Pure/Thy/present.ML
wenzelm@9416
     2
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
wenzelm@6203
     3
wenzelm@59448
     4
Theory presentation: HTML and PDF-LaTeX documents.
wenzelm@6203
     5
*)
wenzelm@6203
     6
wenzelm@6203
     7
signature PRESENT =
wenzelm@6203
     8
sig
wenzelm@24561
     9
  val session_name: theory -> string
wenzelm@56614
    10
  val document_enabled: string -> bool
wenzelm@56612
    11
  val document_variants: string -> (string * string) list
wenzelm@59446
    12
  val init: bool -> bool -> Path.T -> string -> string -> (string * string) list ->
wenzelm@59448
    13
    (Path.T * Path.T) list -> Path.T -> string * string -> bool -> unit
wenzelm@59445
    14
  val finish: unit -> unit
wenzelm@9136
    15
  val theory_output: string -> string -> unit
wenzelm@54456
    16
  val begin_theory: int -> (unit -> HTML.text) -> theory -> theory
wenzelm@52549
    17
  val display_drafts: Path.T list -> int
wenzelm@6203
    18
end;
wenzelm@6203
    19
wenzelm@7685
    20
structure Present: PRESENT =
wenzelm@7685
    21
struct
wenzelm@7685
    22
wenzelm@7727
    23
wenzelm@7727
    24
(** paths **)
wenzelm@7727
    25
wenzelm@7727
    26
val tex_ext = Path.ext "tex";
wenzelm@7727
    27
val tex_path = tex_ext o Path.basic;
wenzelm@7727
    28
val html_ext = Path.ext "html";
wenzelm@7727
    29
val html_path = html_ext o Path.basic;
wenzelm@7727
    30
val index_path = Path.basic "index.html";
wenzelm@11856
    31
val readme_html_path = Path.basic "README.html";
wenzelm@17082
    32
val documentN = "document";
wenzelm@17082
    33
val document_path = Path.basic documentN;
wenzelm@8196
    34
val doc_indexN = "session";
wenzelm@59448
    35
val session_graph_path = Path.basic "session_graph.pdf";
wenzelm@7727
    36
wenzelm@51415
    37
fun show_path path = Path.implode (Path.expand (Path.append (File.pwd ()) path));
wenzelm@11856
    38
wenzelm@7727
    39
wenzelm@14922
    40
wenzelm@7727
    41
(** additional theory data **)
wenzelm@7727
    42
wenzelm@42008
    43
structure Browser_Info = Theory_Data
wenzelm@22846
    44
(
wenzelm@51399
    45
  type T = {chapter: string, name: string};
wenzelm@51567
    46
  val empty = {chapter = "Unsorted", name = "Unknown"}: T;
wenzelm@16503
    47
  fun extend _ = empty;
wenzelm@33522
    48
  fun merge _ = empty;
wenzelm@22846
    49
);
wenzelm@7727
    50
wenzelm@53171
    51
val _ = Theory.setup
wenzelm@53171
    52
  (Browser_Info.put {chapter = Context.PureN, name = Context.PureN});
wenzelm@51567
    53
wenzelm@51399
    54
val session_name = #name o Browser_Info.get;
wenzelm@7727
    55
wenzelm@7727
    56
wenzelm@7727
    57
wenzelm@7727
    58
(** global browser info state **)
wenzelm@7727
    59
wenzelm@7727
    60
(* type theory_info *)
wenzelm@7727
    61
wenzelm@54456
    62
type theory_info = {tex_source: string, html_source: string};
wenzelm@7727
    63
wenzelm@54456
    64
fun make_theory_info (tex_source, html_source) =
wenzelm@54456
    65
  {tex_source = tex_source, html_source = html_source}: theory_info;
wenzelm@7727
    66
wenzelm@54456
    67
fun map_theory_info f {tex_source, html_source} =
wenzelm@54456
    68
  make_theory_info (f (tex_source, html_source));
wenzelm@7727
    69
wenzelm@7727
    70
wenzelm@7727
    71
(* type browser_info *)
wenzelm@7727
    72
wenzelm@54455
    73
type browser_info =
wenzelm@54455
    74
 {theories: theory_info Symtab.table,
wenzelm@54455
    75
  tex_index: (int * string) list,
wenzelm@59448
    76
  html_index: (int * string) list};
wenzelm@7727
    77
wenzelm@59448
    78
fun make_browser_info (theories, tex_index, html_index) : browser_info =
wenzelm@59448
    79
  {theories = theories, tex_index = tex_index, html_index = html_index};
wenzelm@7727
    80
wenzelm@59448
    81
val empty_browser_info = make_browser_info (Symtab.empty, [], []);
wenzelm@7727
    82
wenzelm@59448
    83
fun map_browser_info f {theories, tex_index, html_index} =
wenzelm@59448
    84
  make_browser_info (f (theories, tex_index, html_index));
wenzelm@7727
    85
wenzelm@7727
    86
wenzelm@7727
    87
(* state *)
wenzelm@7727
    88
wenzelm@59173
    89
val browser_info = Synchronized.var "browser_info" empty_browser_info;
wenzelm@59173
    90
fun change_browser_info f = Synchronized.change browser_info (map_browser_info f);
wenzelm@7727
    91
wenzelm@7727
    92
fun init_theory_info name info =
wenzelm@59448
    93
  change_browser_info (fn (theories, tex_index, html_index) =>
wenzelm@59448
    94
    (Symtab.update (name, info) theories, tex_index, html_index));
wenzelm@7727
    95
wenzelm@7727
    96
fun change_theory_info name f =
wenzelm@59448
    97
  change_browser_info (fn (theories, tex_index, html_index) =>
wenzelm@17412
    98
    (case Symtab.lookup theories name of
wenzelm@27491
    99
      NONE => error ("Browser info: cannot access theory document " ^ quote name)
wenzelm@59448
   100
    | SOME info => (Symtab.update (name, map_theory_info f info) theories, tex_index, html_index)));
wenzelm@7727
   101
wenzelm@7727
   102
wenzelm@7727
   103
fun add_tex_index txt =
wenzelm@59448
   104
  change_browser_info (fn (theories, tex_index, html_index) =>
wenzelm@59448
   105
    (theories, txt :: tex_index, html_index));
wenzelm@7727
   106
wenzelm@7727
   107
fun add_html_index txt =
wenzelm@59448
   108
  change_browser_info (fn (theories, tex_index, html_index) =>
wenzelm@59448
   109
    (theories, tex_index, txt :: html_index));
wenzelm@7727
   110
wenzelm@7727
   111
wenzelm@7727
   112
wenzelm@7727
   113
(** global session state **)
wenzelm@7727
   114
wenzelm@7727
   115
(* session_info *)
wenzelm@7727
   116
wenzelm@7727
   117
type session_info =
wenzelm@59446
   118
  {name: string, chapter: string, info_path: Path.T, info: bool, doc_format: string,
wenzelm@59446
   119
    doc_output: Path.T option, doc_files: (Path.T * Path.T) list, graph_file: Path.T,
wenzelm@59446
   120
    documents: (string * string) list, verbose: bool, readme: Path.T option};
wenzelm@7727
   121
wenzelm@9416
   122
fun make_session_info
wenzelm@59446
   123
  (name, chapter, info_path, info, doc_format, doc_output, doc_files,
wenzelm@59446
   124
    graph_file, documents, verbose, readme) =
wenzelm@59446
   125
  {name = name, chapter = chapter, info_path = info_path, info = info, doc_format = doc_format,
wenzelm@59446
   126
    doc_output = doc_output, doc_files = doc_files, graph_file = graph_file,
wenzelm@59446
   127
    documents = documents, verbose = verbose, readme = readme}: session_info;
wenzelm@7685
   128
wenzelm@7685
   129
wenzelm@7727
   130
(* state *)
wenzelm@7727
   131
wenzelm@32738
   132
val session_info = Unsynchronized.ref (NONE: session_info option);
wenzelm@7727
   133
wenzelm@51399
   134
fun with_session_info x f = (case ! session_info of NONE => x | SOME info => f info);
wenzelm@7727
   135
wenzelm@7727
   136
wenzelm@7727
   137
wenzelm@14922
   138
(** document preparation **)
wenzelm@7727
   139
wenzelm@56614
   140
(* options *)
wenzelm@56614
   141
wenzelm@56614
   142
fun document_enabled s = s <> "" andalso s <> "false";
wenzelm@17082
   143
wenzelm@56612
   144
fun document_variants str =
wenzelm@56612
   145
  let
wenzelm@56612
   146
    fun read_variant s =
wenzelm@56612
   147
      (case space_explode "=" s of
wenzelm@56612
   148
        [name] => (name, "")
wenzelm@56612
   149
      | [name, tags] => (name, tags)
wenzelm@56612
   150
      | _ => error ("Malformed document variant specification: " ^ quote s));
wenzelm@56612
   151
    val variants = map read_variant (space_explode ":" str);
wenzelm@56612
   152
    val _ =
wenzelm@56612
   153
      (case duplicates (op =) (map #1 variants) of
wenzelm@56612
   154
        [] => ()
wenzelm@56612
   155
      | dups => error ("Duplicate document variants: " ^ commas_quote dups));
wenzelm@56612
   156
  in variants end;
wenzelm@17082
   157
wenzelm@17082
   158
wenzelm@7727
   159
(* init session *)
wenzelm@7727
   160
wenzelm@59446
   161
fun init build info info_path doc document_output doc_variants doc_files graph_file
wenzelm@59448
   162
    (chapter, name) verbose =
wenzelm@56530
   163
  if not build andalso not info andalso doc = "" then
wenzelm@59173
   164
    (Synchronized.change browser_info (K empty_browser_info); session_info := NONE)
wenzelm@11856
   165
  else
wenzelm@11856
   166
    let
wenzelm@51399
   167
      val doc_output =
wenzelm@51399
   168
        if document_output = "" then NONE else SOME (Path.explode document_output);
wenzelm@7727
   169
wenzelm@42007
   170
      val documents =
wenzelm@42007
   171
        if doc = "" then []
wenzelm@56533
   172
        else if null doc_files andalso not (can File.check_dir document_path) then
wenzelm@44389
   173
          (if verbose then Output.physical_stderr "Warning: missing document directory\n"
wenzelm@44389
   174
           else (); [])
wenzelm@48804
   175
        else doc_variants;
wenzelm@7727
   176
wenzelm@51419
   177
      val readme = if File.exists readme_html_path then SOME readme_html_path else NONE;
wenzelm@7727
   178
wenzelm@17082
   179
      val docs =
wenzelm@17082
   180
        (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
wenzelm@42007
   181
          map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
wenzelm@11856
   182
    in
wenzelm@42007
   183
      session_info :=
wenzelm@51399
   184
        SOME (make_session_info (name, chapter, info_path, info, doc,
wenzelm@59446
   185
          doc_output, doc_files, graph_file, documents, verbose, readme));
wenzelm@59448
   186
      Synchronized.change browser_info (K empty_browser_info);
wenzelm@59448
   187
      add_html_index (0, HTML.begin_session_index name (Url.File session_graph_path) docs)
wenzelm@42008
   188
    end;
wenzelm@7727
   189
wenzelm@7727
   190
wenzelm@28496
   191
(* isabelle tool wrappers *)
wenzelm@17082
   192
wenzelm@48805
   193
fun isabelle_document {verbose, purge} format name tags dir =
wenzelm@17082
   194
  let
wenzelm@48805
   195
    val s = "\"$ISABELLE_TOOL\" document" ^ (if purge then " -c" else "") ^ " -o '" ^ format ^ "' \
wenzelm@48805
   196
      \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path dir ^ " 2>&1";
wenzelm@48805
   197
    val doc_path = Path.appends [dir, Path.parent, Path.basic name |> Path.ext format];
wenzelm@33682
   198
    val _ = if verbose then writeln s else ();
wenzelm@43850
   199
    val (out, rc) = Isabelle_System.bash_output s;
wenzelm@33682
   200
    val _ =
wenzelm@33682
   201
      if not (File.exists doc_path) orelse rc <> 0 then
wenzelm@33682
   202
        cat_error out ("Failed to build document " ^ quote (show_path doc_path))
wenzelm@33682
   203
      else if verbose then writeln out
wenzelm@33682
   204
      else ();
wenzelm@33682
   205
  in doc_path end;
wenzelm@17082
   206
wenzelm@17082
   207
wenzelm@11856
   208
(* finish session -- output all generated text *)
wenzelm@11856
   209
wenzelm@59058
   210
fun sorted_index index = map snd (sort (int_ord o apply2 fst) (rev index));
wenzelm@24150
   211
fun index_buffer index = Buffer.add (implode (sorted_index index)) Buffer.empty;
wenzelm@24150
   212
wenzelm@14922
   213
fun write_tex src name path =
wenzelm@28027
   214
  File.write_buffer (Path.append path (tex_path name)) src;
wenzelm@14922
   215
wenzelm@14922
   216
fun write_tex_index tex_index path =
wenzelm@24150
   217
  write_tex (index_buffer tex_index |> Buffer.add Latex.tex_trailer) doc_indexN path;
wenzelm@14922
   218
wenzelm@42008
   219
fun finish () =
wenzelm@59446
   220
  with_session_info () (fn {name, chapter, info, info_path, doc_format,
wenzelm@59445
   221
    doc_output, doc_files, graph_file, documents, verbose, readme, ...} =>
wenzelm@7727
   222
  let
wenzelm@59448
   223
    val {theories, tex_index, html_index} = Synchronized.value browser_info;
wenzelm@11856
   224
    val thys = Symtab.dest theories;
wenzelm@51399
   225
wenzelm@51399
   226
    val chapter_prefix = Path.append info_path (Path.basic chapter);
wenzelm@51399
   227
    val session_prefix = Path.append chapter_prefix (Path.basic name);
wenzelm@7727
   228
wenzelm@54456
   229
    fun finish_html (a, {html_source, ...}: theory_info) =
wenzelm@54456
   230
      File.write (Path.append session_prefix (html_path a)) html_source;
wenzelm@11856
   231
wenzelm@42007
   232
    val _ =
wenzelm@42007
   233
      if info then
wenzelm@51399
   234
       (Isabelle_System.mkdirs session_prefix;
wenzelm@51399
   235
        File.write_buffer (Path.append session_prefix index_path)
wenzelm@51399
   236
          (index_buffer html_index |> Buffer.add HTML.end_document);
wenzelm@59448
   237
        Isabelle_System.copy_file graph_file (Path.append session_prefix session_graph_path);
wenzelm@56533
   238
        (case readme of NONE => () | SOME path => Isabelle_System.copy_file path session_prefix);
wenzelm@56533
   239
        Isabelle_System.copy_file (Path.explode "~~/etc/isabelle.css") session_prefix;
wenzelm@42007
   240
        List.app finish_html thys;
wenzelm@54455
   241
        if verbose
wenzelm@54455
   242
        then Output.physical_stderr ("Browser info at " ^ show_path session_prefix ^ "\n")
wenzelm@42007
   243
        else ())
wenzelm@42007
   244
      else ();
wenzelm@42007
   245
wenzelm@56533
   246
    fun document_job doc_prefix backdrop (doc_name, tags) =
wenzelm@48935
   247
      let
wenzelm@56533
   248
        val doc_dir = Path.append doc_prefix (Path.basic doc_name);
wenzelm@56531
   249
        val _ = Isabelle_System.mkdirs doc_dir;
wenzelm@56531
   250
        val _ =
wenzelm@56531
   251
          Isabelle_System.isabelle_tool "latex"
wenzelm@56531
   252
            ("-o sty " ^ File.shell_path (Path.append doc_dir (Path.basic "root.tex")));
wenzelm@57885
   253
        val _ = List.app (fn file => Isabelle_System.copy_file_base file doc_dir) doc_files;
wenzelm@59448
   254
        val _ = Isabelle_System.copy_file graph_file (Path.append doc_dir session_graph_path);
wenzelm@56531
   255
        val _ = write_tex_index tex_index doc_dir;
wenzelm@56531
   256
        val _ =
wenzelm@56531
   257
          List.app (fn (a, {tex_source, ...}) =>
wenzelm@56531
   258
            write_tex (Buffer.add tex_source Buffer.empty) a doc_dir) thys;
wenzelm@48935
   259
      in
wenzelm@48935
   260
        fn () =>
wenzelm@56533
   261
          (isabelle_document {verbose = true, purge = backdrop} doc_format doc_name tags doc_dir,
wenzelm@56531
   262
            fn doc =>
wenzelm@56531
   263
              if verbose orelse not backdrop then
wenzelm@56531
   264
                Output.physical_stderr ("Document at " ^ show_path doc ^ "\n")
wenzelm@56531
   265
              else ())
wenzelm@48935
   266
      end;
wenzelm@48935
   267
wenzelm@48935
   268
    val jobs =
wenzelm@48935
   269
      (if info orelse is_none doc_output then
wenzelm@51399
   270
        map (document_job session_prefix true) documents
wenzelm@48935
   271
       else []) @
wenzelm@48935
   272
      (case doc_output of
wenzelm@48935
   273
        NONE => []
wenzelm@48935
   274
      | SOME path => map (document_job path false) documents);
wenzelm@48935
   275
wenzelm@48935
   276
    val _ = jobs |> Par_List.map (fn job => job ()) |> List.app (op |>);
wenzelm@7727
   277
  in
wenzelm@59173
   278
    Synchronized.change browser_info (K empty_browser_info);
skalberg@15531
   279
    session_info := NONE
wenzelm@42008
   280
  end);
wenzelm@7727
   281
wenzelm@7727
   282
wenzelm@7727
   283
(* theory elements *)
wenzelm@7727
   284
wenzelm@9136
   285
fun theory_output name s =
wenzelm@54458
   286
  with_session_info () (fn _ =>
wenzelm@58870
   287
    change_theory_info name (fn (_, html_source) => (Latex.isabelle_theory name s, html_source)));
wenzelm@7727
   288
wenzelm@59448
   289
fun theory_link (curr_chapter, curr_session) thy =
wenzelm@59448
   290
  let
wenzelm@59448
   291
    val {chapter, name = session} = Browser_Info.get thy;
wenzelm@59448
   292
    val link = html_path (Context.theory_name thy);
wenzelm@59448
   293
  in
wenzelm@59448
   294
    if curr_session = session then SOME link
wenzelm@59448
   295
    else if curr_chapter = chapter then
wenzelm@59448
   296
      SOME (Path.appends [Path.parent, Path.basic session, link])
wenzelm@59448
   297
    else if chapter = Context.PureN then NONE
wenzelm@59448
   298
    else SOME (Path.appends [Path.parent, Path.parent, Path.basic chapter, Path.basic session, link])
wenzelm@59448
   299
  end;
wenzelm@59448
   300
wenzelm@54456
   301
fun begin_theory update_time mk_text thy =
wenzelm@54456
   302
  with_session_info thy (fn {name = session_name, chapter, ...} =>
wenzelm@54456
   303
    let
wenzelm@54456
   304
      val name = Context.theory_name thy;
wenzelm@54456
   305
      val parents = Theory.parents_of thy;
wenzelm@54456
   306
wenzelm@54456
   307
      val parent_specs = parents |> map (fn parent =>
wenzelm@54456
   308
        (Option.map Url.File (theory_link (chapter, session_name) parent),
wenzelm@54456
   309
          (Context.theory_name parent)));
wenzelm@54456
   310
      val html_source = HTML.theory name parent_specs (mk_text ());
wenzelm@54456
   311
    in
wenzelm@54456
   312
      init_theory_info name (make_theory_info ("", html_source));
wenzelm@54456
   313
      add_html_index (update_time, HTML.theory_entry (Url.File (html_path name), name));
wenzelm@54456
   314
      add_tex_index (update_time, Latex.theory_entry name);
wenzelm@54456
   315
      Browser_Info.put {chapter = chapter, name = session_name} thy
wenzelm@54456
   316
    end);
wenzelm@7727
   317
wenzelm@7727
   318
wenzelm@7727
   319
wenzelm@42127
   320
(** draft document output **)
wenzelm@14922
   321
wenzelm@52549
   322
fun display_drafts src_paths = Isabelle_System.with_tmp_dir "drafts" (fn dir =>
wenzelm@14922
   323
  let
wenzelm@24150
   324
    fun prep_draft path i =
wenzelm@14935
   325
      let
wenzelm@14935
   326
        val base = Path.base path;
wenzelm@14972
   327
        val name =
wenzelm@24829
   328
          (case Path.implode (#1 (Path.split_ext base)) of
wenzelm@44986
   329
            "" => "DUMMY"
wenzelm@44986
   330
          | s => s)  ^ serial_string ();
wenzelm@14935
   331
      in
wenzelm@14935
   332
        if File.exists path then
wenzelm@24150
   333
          (((name, base, File.read path), (i, Latex.theory_entry name)), i + 1)
wenzelm@41944
   334
        else error ("Bad file: " ^ Path.print path)
wenzelm@14935
   335
      end;
wenzelm@24150
   336
    val (srcs, tex_index) = split_list (fst (fold_map prep_draft src_paths 0));
wenzelm@14935
   337
wenzelm@42127
   338
    val doc_path = Path.append dir document_path;
wenzelm@40743
   339
    val _ = Isabelle_System.mkdirs doc_path;
wenzelm@14922
   340
    val root_path = Path.append doc_path (Path.basic "root.tex");
wenzelm@56533
   341
    val _ = Isabelle_System.copy_file (Path.explode "~~/lib/texinputs/draft.tex") root_path;
wenzelm@40743
   342
    val _ = Isabelle_System.isabelle_tool "latex" ("-o sty " ^ File.shell_path root_path);
wenzelm@40743
   343
    val _ = Isabelle_System.isabelle_tool "latex" ("-o syms " ^ File.shell_path root_path);
wenzelm@14922
   344
wenzelm@14922
   345
    fun known name =
wenzelm@14922
   346
      let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
wenzelm@20664
   347
      in member (op =) ss end;
wenzelm@14922
   348
    val known_syms = known "syms.lst";
wenzelm@14922
   349
    val known_ctrls = known "ctrls.lst";
wenzelm@14922
   350
skalberg@15570
   351
    val _ = srcs |> List.app (fn (name, base, txt) =>
wenzelm@14935
   352
      Symbol.explode txt
wenzelm@21858
   353
      |> Latex.symbol_source (known_syms, known_ctrls) (Path.implode base)
wenzelm@14935
   354
      |> File.write (Path.append doc_path (tex_path name)));
wenzelm@14922
   355
    val _ = write_tex_index tex_index doc_path;
wenzelm@14922
   356
wenzelm@48805
   357
    val result =
wenzelm@52743
   358
      isabelle_document {verbose = false, purge = true} "pdf" documentN "" doc_path;
wenzelm@54683
   359
wenzelm@54683
   360
    val target_dir = Path.explode "$ISABELLE_HOME_USER/tmp";
wenzelm@54683
   361
    val target = Path.explode "$ISABELLE_HOME_USER/tmp/drafts.pdf"
wenzelm@54683
   362
    val _ = Isabelle_System.mkdirs target_dir;
wenzelm@56533
   363
    val _ = Isabelle_System.copy_file result target;
wenzelm@52549
   364
  in
wenzelm@54683
   365
    Isabelle_System.isabelle_tool "display" (File.shell_path target ^ " &")
wenzelm@52549
   366
  end);
wenzelm@14922
   367
wenzelm@7685
   368
end;
wenzelm@7685
   369