doc-src/antiquote_setup.ML
author wenzelm
Mon Oct 01 21:19:50 2007 +0200 (2007-10-01)
changeset 24803 38577b4b1fde
parent 24584 01e83ffa6c54
child 24920 2a45e400fdad
permissions -rw-r--r--
Norbert Schirmer: record improvements;
haftmann@24584
     1
(*  Title:      Doc/antiquote_setup.ML
wenzelm@21375
     2
    ID:         $Id$
wenzelm@21375
     3
    Author:     Makarius
wenzelm@21375
     4
wenzelm@21375
     5
Auxiliary antiquotations for Isabelle manuals.
wenzelm@21375
     6
*)
wenzelm@21375
     7
wenzelm@21375
     8
local
wenzelm@21375
     9
wenzelm@22094
    10
structure O = ThyOutput;
wenzelm@21375
    11
wenzelm@21375
    12
val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
wenzelm@21375
    13
wenzelm@21375
    14
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
wenzelm@21375
    15
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
wenzelm@21375
    16
wenzelm@21375
    17
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
wenzelm@21375
    18
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
haftmann@22289
    19
wenzelm@23651
    20
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
wenzelm@23651
    21
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
haftmann@22289
    22
wenzelm@21375
    23
fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
wenzelm@21375
    24
wenzelm@21375
    25
fun ml_functor _ = "val _ = ();";  (*no check!*)
wenzelm@21375
    26
wenzelm@21375
    27
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@21375
    28
wenzelm@21375
    29
fun index_ml kind ml src ctxt (txt1, txt2) =
wenzelm@21375
    30
  let
wenzelm@21375
    31
    val txt = if txt2 = "" then txt1 else
wenzelm@21375
    32
      if kind = "type"
wenzelm@21375
    33
        then txt1 ^ " = " ^ txt2
haftmann@22289
    34
      else if kind = "exception"
haftmann@22289
    35
        then txt1 ^ " of " ^ txt2
haftmann@22289
    36
      else txt1 ^ ": " ^ txt2;
wenzelm@21375
    37
    val txt' = if kind = "" then txt else kind ^ " " ^ txt;
haftmann@22289
    38
    val _ = writeln (ml (txt1, txt2));
wenzelm@22094
    39
    val _ = ML_Context.use_mltext (ml (txt1, txt2)) false (SOME (Context.Proof ctxt));
wenzelm@21375
    40
  in
wenzelm@21375
    41
    "\\indexml" ^ kind ^ enclose "{" "}"
haftmann@22289
    42
      (translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c) txt1) ^
wenzelm@21375
    43
    ((if ! O.source then str_of_source src else txt')
wenzelm@21375
    44
    |> (if ! O.quotes then quote else I)
wenzelm@21375
    45
    |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@21375
    46
    else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@21375
    47
  end;
wenzelm@21375
    48
wenzelm@21375
    49
fun output_verbatim _ _ = split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n";
wenzelm@21375
    50
wenzelm@21375
    51
fun arguments x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
wenzelm@21375
    52
wenzelm@24204
    53
fun pretty_thy_file name = (ThyLoad.check_thy Path.current name; Pretty.str name);
wenzelm@21375
    54
berghofe@23846
    55
berghofe@23846
    56
(* theorems with names *)
berghofe@23846
    57
berghofe@23846
    58
fun tweak_line s =
berghofe@23846
    59
  if ! O.display then s else Symbol.strip_blanks s;
berghofe@23846
    60
berghofe@23846
    61
val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
berghofe@23846
    62
berghofe@23846
    63
fun output_named_list pretty src ctxt xs =
berghofe@23846
    64
  map (apfst (pretty ctxt)) xs        (*always pretty in order to exhibit errors!*)
berghofe@23846
    65
  |> (if ! O.quotes then map (apfst Pretty.quote) else I)
berghofe@23846
    66
  |> (if ! O.display then
berghofe@23846
    67
    map (fn (p, name) =>
berghofe@23846
    68
      Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
berghofe@23846
    69
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
    70
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
    71
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
berghofe@23846
    72
  else
berghofe@23846
    73
    map (fn (p, name) =>
berghofe@23846
    74
      Output.output (Pretty.str_of p) ^
berghofe@23846
    75
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
    76
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
    77
    #> enclose "\\isa{" "}");
berghofe@23846
    78
berghofe@23846
    79
fun pretty_term ctxt = ProofContext.pretty_term ctxt o ProofContext.revert_skolems ctxt;
berghofe@23846
    80
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
berghofe@23846
    81
wenzelm@21375
    82
in
wenzelm@21375
    83
wenzelm@21375
    84
val _ = O.add_commands
wenzelm@21375
    85
 [("index_ML", arguments (index_ml "" ml_val)),
wenzelm@21375
    86
  ("index_ML_type", arguments (index_ml "type" ml_type)),
wenzelm@23651
    87
  ("index_ML_exn", arguments (index_ml "exception" ml_exn)),
wenzelm@21375
    88
  ("index_ML_structure", arguments (index_ml "structure" ml_structure)),
wenzelm@21375
    89
  ("index_ML_functor", arguments (index_ml "functor" ml_functor)),
wenzelm@21375
    90
  ("ML_functor", O.args (Scan.lift Args.name) output_verbatim),
wenzelm@21375
    91
  ("verbatim", O.args (Scan.lift Args.name) output_verbatim),
berghofe@23846
    92
  ("thy_file", O.args (Scan.lift Args.name) (O.output (K pretty_thy_file))),
berghofe@23846
    93
  ("named_thms", O.args (Scan.repeat (Attrib.thm --
berghofe@23846
    94
       Scan.lift (Args.$$$ "(" |-- Args.name --| Args.$$$ ")")))
berghofe@23846
    95
     (output_named_list pretty_thm))];
wenzelm@21375
    96
wenzelm@21375
    97
end;