1 (* Title: Doc/antiquote_setup.ML
5 Auxiliary antiquotations for the Isabelle manuals.
8 structure AntiquoteSetup: sig end =
11 structure O = ThyOutput;
17 translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c);
19 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
22 if ! O.display then s else Symbol.strip_blanks s;
24 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
26 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
27 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
32 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
34 val _ = O.add_commands
35 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
36 split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
43 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
44 | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
46 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
47 | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
49 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
50 | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
52 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
54 fun ml_functor _ = "val _ = ();"; (*no check!*)
56 fun index_ml kind ml src ctxt (txt1, txt2) =
58 val txt = if txt2 = "" then txt1 else
59 if kind = "type" then txt1 ^ " = " ^ txt2
60 else if kind = "exception" then txt1 ^ " of " ^ txt2
61 else txt1 ^ ": " ^ txt2;
62 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
63 val _ = writeln (ml (txt1, txt2));
64 val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
66 "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
67 ((if ! O.source then str_of_source src else txt')
68 |> (if ! O.quotes then quote else I)
69 |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
70 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
73 fun output_ml ctxt src =
74 if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
77 #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
78 #> space_implode "\\isasep\\isanewline%\n";
80 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
84 val _ = O.add_commands
85 [("index_ML", ml_args (index_ml "" ml_val)),
86 ("index_ML_type", ml_args (index_ml "type" ml_type)),
87 ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
88 ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
89 ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
90 ("ML_functor", O.args (Scan.lift Args.name) output_ml),
91 ("ML_text", O.args (Scan.lift Args.name) output_ml)];
96 (* theorems with names *)
100 fun output_named_thms src ctxt xs =
101 map (apfst (pretty_thm ctxt)) xs (*always pretty in order to exhibit errors!*)
102 |> (if ! O.quotes then map (apfst Pretty.quote) else I)
103 |> (if ! O.display then
105 Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
106 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
107 #> space_implode "\\par\\smallskip%\n"
108 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
111 Output.output (Pretty.str_of p) ^
112 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
113 #> space_implode "\\par\\smallskip%\n"
114 #> enclose "\\isa{" "}");
118 val _ = O.add_commands
119 [("named_thms", O.args (Scan.repeat (Attrib.thm --
120 Scan.lift (Args.parens Args.name))) output_named_thms)];
127 val _ = O.add_commands
128 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
129 (ThyLoad.check_thy Path.current name; Pretty.str name))))];
132 (* Isar entities (with index) *)
136 val arg = enclose "{" "}" o clean_string;
138 fun output_entity markup index kind src ctxt (logic, name) =
142 "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name)
144 (Output.output (if ! O.source then str_of_source src else name)
145 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
146 |> (if ! O.quotes then quote else I)
147 |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
148 else enclose "\\isa{" "}"));
150 fun entity markup index kind =
151 O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
152 (output_entity markup index kind);
154 fun entity_antiqs markup kind =
155 [(kind, entity markup NONE kind),
156 (kind ^ "_def", entity markup (SOME true) kind),
157 (kind ^ "_ref", entity markup (SOME false) kind)];
161 val _ = O.add_commands
162 (entity_antiqs "" "syntax" @
163 entity_antiqs "isacommand" "command" @
164 entity_antiqs "isakeyword" "keyword" @
165 entity_antiqs "" "element" @
166 entity_antiqs "" "method" @
167 entity_antiqs "" "attribute" @
168 entity_antiqs "" "fact" @
169 entity_antiqs "" "variable" @
170 entity_antiqs "" "case" @
171 entity_antiqs "" "antiquotation");