doc-src/antiquote_setup.ML
author wenzelm
Sat, 26 Apr 2008 13:20:16 +0200
changeset 26751 2b97ea3130c2
parent 26742 5a86bc79431c
child 26756 6634a641b961
permissions -rw-r--r--
added setup for Isar entities;
tuned;
haftmann@24584
     1
(*  Title:      Doc/antiquote_setup.ML
wenzelm@21375
     2
    ID:         $Id$
wenzelm@21375
     3
    Author:     Makarius
wenzelm@21375
     4
wenzelm@26742
     5
Auxiliary antiquotations for the Isabelle manuals.
wenzelm@21375
     6
*)
wenzelm@21375
     7
wenzelm@26742
     8
structure AntiquoteSetup: sig end =
wenzelm@26742
     9
struct
wenzelm@21375
    10
wenzelm@22094
    11
structure O = ThyOutput;
wenzelm@21375
    12
wenzelm@26742
    13
wenzelm@26742
    14
(* misc utils *)
wenzelm@26742
    15
wenzelm@26751
    16
val clean_string =
wenzelm@26751
    17
  translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c);
wenzelm@26751
    18
wenzelm@21375
    19
val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
wenzelm@21375
    20
wenzelm@26742
    21
fun tweak_line s =
wenzelm@26742
    22
  if ! O.display then s else Symbol.strip_blanks s;
wenzelm@26742
    23
wenzelm@26742
    24
val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
wenzelm@26742
    25
wenzelm@26742
    26
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
wenzelm@26742
    27
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
wenzelm@26742
    28
wenzelm@26742
    29
wenzelm@26742
    30
(* verbatim text *)
wenzelm@26742
    31
wenzelm@26742
    32
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@26742
    33
wenzelm@26742
    34
val _ = O.add_commands
wenzelm@26742
    35
 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
wenzelm@26742
    36
      split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
wenzelm@26742
    37
wenzelm@26742
    38
wenzelm@26742
    39
(* ML text *)
wenzelm@26742
    40
wenzelm@26742
    41
local
wenzelm@26742
    42
wenzelm@21375
    43
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
wenzelm@21375
    44
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
wenzelm@21375
    45
wenzelm@21375
    46
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
wenzelm@21375
    47
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
haftmann@22289
    48
wenzelm@23651
    49
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
wenzelm@23651
    50
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
haftmann@22289
    51
wenzelm@21375
    52
fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
wenzelm@21375
    53
wenzelm@21375
    54
fun ml_functor _ = "val _ = ();";  (*no check!*)
wenzelm@21375
    55
wenzelm@21375
    56
fun index_ml kind ml src ctxt (txt1, txt2) =
wenzelm@21375
    57
  let
wenzelm@21375
    58
    val txt = if txt2 = "" then txt1 else
wenzelm@26742
    59
      if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@26742
    60
      else if kind = "exception" then txt1 ^ " of " ^ txt2
haftmann@22289
    61
      else txt1 ^ ": " ^ txt2;
wenzelm@21375
    62
    val txt' = if kind = "" then txt else kind ^ " " ^ txt;
haftmann@22289
    63
    val _ = writeln (ml (txt1, txt2));
wenzelm@26455
    64
    val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
wenzelm@21375
    65
  in
wenzelm@26751
    66
    "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
wenzelm@21375
    67
    ((if ! O.source then str_of_source src else txt')
wenzelm@21375
    68
    |> (if ! O.quotes then quote else I)
wenzelm@21375
    69
    |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@26751
    70
        else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@21375
    71
  end;
wenzelm@21375
    72
haftmann@26461
    73
fun output_ml ctxt src =
haftmann@26461
    74
  if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@26742
    75
  else
haftmann@26461
    76
    split_lines
haftmann@26461
    77
    #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
haftmann@26461
    78
    #> space_implode "\\isasep\\isanewline%\n";
haftmann@26461
    79
wenzelm@26742
    80
fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
wenzelm@21375
    81
wenzelm@26742
    82
in
wenzelm@21375
    83
wenzelm@26742
    84
val _ = O.add_commands
wenzelm@26742
    85
 [("index_ML", ml_args (index_ml "" ml_val)),
wenzelm@26742
    86
  ("index_ML_type", ml_args (index_ml "type" ml_type)),
wenzelm@26742
    87
  ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
wenzelm@26742
    88
  ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
wenzelm@26742
    89
  ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
wenzelm@26742
    90
  ("ML_functor", O.args (Scan.lift Args.name) output_ml),
wenzelm@26742
    91
  ("ML_text", O.args (Scan.lift Args.name) output_ml)];
wenzelm@26742
    92
wenzelm@26742
    93
end;
wenzelm@21375
    94
berghofe@23846
    95
berghofe@23846
    96
(* theorems with names *)
berghofe@23846
    97
wenzelm@26742
    98
local
berghofe@23846
    99
wenzelm@26742
   100
fun output_named_thms src ctxt xs =
wenzelm@26742
   101
  map (apfst (pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
berghofe@23846
   102
  |> (if ! O.quotes then map (apfst Pretty.quote) else I)
berghofe@23846
   103
  |> (if ! O.display then
berghofe@23846
   104
    map (fn (p, name) =>
berghofe@23846
   105
      Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
berghofe@23846
   106
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
   107
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   108
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
berghofe@23846
   109
  else
berghofe@23846
   110
    map (fn (p, name) =>
berghofe@23846
   111
      Output.output (Pretty.str_of p) ^
berghofe@23846
   112
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
   113
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   114
    #> enclose "\\isa{" "}");
berghofe@23846
   115
wenzelm@21375
   116
in
wenzelm@21375
   117
wenzelm@21375
   118
val _ = O.add_commands
wenzelm@26742
   119
 [("named_thms", O.args (Scan.repeat (Attrib.thm --
wenzelm@26751
   120
      Scan.lift (Args.parens Args.name))) output_named_thms)];
wenzelm@21375
   121
wenzelm@21375
   122
end;
wenzelm@26742
   123
wenzelm@26742
   124
wenzelm@26742
   125
(* theory files *)
wenzelm@26742
   126
wenzelm@26742
   127
val _ = O.add_commands
wenzelm@26742
   128
 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
wenzelm@26742
   129
      (ThyLoad.check_thy Path.current name; Pretty.str name))))];
wenzelm@26742
   130
wenzelm@26751
   131
wenzelm@26751
   132
(* Isar entities (with index) *)
wenzelm@26751
   133
wenzelm@26751
   134
local
wenzelm@26751
   135
wenzelm@26751
   136
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   137
wenzelm@26751
   138
fun output_entity index kind src ctxt (logic, name) =
wenzelm@26751
   139
  (case index of
wenzelm@26751
   140
    NONE => ""
wenzelm@26751
   141
  | SOME is_def =>
wenzelm@26751
   142
      "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name)
wenzelm@26751
   143
  ^
wenzelm@26751
   144
  (Output.output (if ! O.source then str_of_source src else name)
wenzelm@26751
   145
    |> (if ! O.quotes then quote else I)
wenzelm@26751
   146
    |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@26751
   147
        else enclose "\\isa{" "}"));
wenzelm@26751
   148
wenzelm@26751
   149
fun entity index kind =
wenzelm@26751
   150
  O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
wenzelm@26751
   151
    (output_entity index kind);
wenzelm@26751
   152
    
wenzelm@26751
   153
fun entity_antiqs kind =
wenzelm@26751
   154
 [(kind, entity NONE kind),
wenzelm@26751
   155
  (kind ^ "_def", entity (SOME true) kind), 
wenzelm@26751
   156
  (kind ^ "_ref", entity (SOME false) kind)];
wenzelm@26751
   157
wenzelm@26751
   158
in
wenzelm@26751
   159
wenzelm@26751
   160
val _ = O.add_commands
wenzelm@26751
   161
 (entity_antiqs "syntax" @
wenzelm@26751
   162
  entity_antiqs "command" @
wenzelm@26751
   163
  entity_antiqs "keyword" @
wenzelm@26751
   164
  entity_antiqs "element" @
wenzelm@26751
   165
  entity_antiqs "method" @
wenzelm@26751
   166
  entity_antiqs "attribute" @
wenzelm@26751
   167
  entity_antiqs "fact" @
wenzelm@26751
   168
  entity_antiqs "term" @
wenzelm@26751
   169
  entity_antiqs "case" @
wenzelm@26751
   170
  entity_antiqs "antiquotation");
wenzelm@26751
   171
wenzelm@26742
   172
end;
wenzelm@26751
   173
wenzelm@26751
   174
end;