doc-src/antiquote_setup.ML
author huffman
Mon May 14 17:45:42 2007 +0200 (2007-05-14)
changeset 22967 0651b224528a
parent 22289 41ce4f5c97c9
child 23651 6e0b8b6012c9
permissions -rw-r--r--
added general sum-squares lemmas
wenzelm@21375
     1
(*  Title:      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
haftmann@22289
    20
fun ml_exc (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
haftmann@22289
    21
  | ml_exc (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@21375
    53
fun pretty_thy_file name = (ThyLoad.check_thy Path.current name false; Pretty.str name);
wenzelm@21375
    54
wenzelm@21375
    55
in
wenzelm@21375
    56
wenzelm@21375
    57
val _ = O.add_commands
wenzelm@21375
    58
 [("index_ML", arguments (index_ml "" ml_val)),
wenzelm@21375
    59
  ("index_ML_type", arguments (index_ml "type" ml_type)),
haftmann@22289
    60
  ("index_ML_exc", arguments (index_ml "exception" ml_exc)),
wenzelm@21375
    61
  ("index_ML_structure", arguments (index_ml "structure" ml_structure)),
wenzelm@21375
    62
  ("index_ML_functor", arguments (index_ml "functor" ml_functor)),
wenzelm@21375
    63
  ("ML_functor", O.args (Scan.lift Args.name) output_verbatim),
wenzelm@21375
    64
  ("verbatim", O.args (Scan.lift Args.name) output_verbatim),
wenzelm@21375
    65
  ("thy_file", O.args (Scan.lift Args.name) (O.output (K pretty_thy_file)))];
wenzelm@21375
    66
wenzelm@21375
    67
end;