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