1 (* Title: Doc/antiquote_setup.ML
5 Auxiliary antiquotations for the Isabelle manuals.
8 structure AntiquoteSetup: sig end =
11 structure O = ThyOutput;
16 val clean_string = translate_string
24 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
27 if ! O.display then s else Symbol.strip_blanks s;
29 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
31 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
32 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
37 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
39 val _ = O.add_commands
40 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
41 split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
48 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
49 | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
51 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
52 | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
54 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
55 | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
57 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
59 fun ml_functor _ = "val _ = ();"; (*no check!*)
61 fun index_ml kind ml src ctxt (txt1, txt2) =
63 val txt = if txt2 = "" then txt1 else
64 if kind = "type" then txt1 ^ " = " ^ txt2
65 else if kind = "exception" then txt1 ^ " of " ^ txt2
66 else txt1 ^ ": " ^ txt2;
67 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
68 val _ = writeln (ml (txt1, txt2));
69 val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
71 "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
72 ((if ! O.source then str_of_source src else txt')
73 |> (if ! O.quotes then quote else I)
74 |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
75 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
78 fun output_ml ctxt src =
79 if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
82 #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
83 #> space_implode "\\isasep\\isanewline%\n";
85 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
89 val _ = O.add_commands
90 [("index_ML", ml_args (index_ml "" ml_val)),
91 ("index_ML_type", ml_args (index_ml "type" ml_type)),
92 ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
93 ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
94 ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
95 ("ML_functor", O.args (Scan.lift Args.name) output_ml),
96 ("ML_text", O.args (Scan.lift Args.name) output_ml)];
101 (* theorems with names *)
105 fun output_named_thms src ctxt xs =
106 map (apfst (pretty_thm ctxt)) xs (*always pretty in order to exhibit errors!*)
107 |> (if ! O.quotes then map (apfst Pretty.quote) else I)
108 |> (if ! O.display then
110 Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
111 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
112 #> space_implode "\\par\\smallskip%\n"
113 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
116 Output.output (Pretty.str_of p) ^
117 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
118 #> space_implode "\\par\\smallskip%\n"
119 #> enclose "\\isa{" "}");
123 val _ = O.add_commands
124 [("named_thms", O.args (Scan.repeat (Attrib.thm --
125 Scan.lift (Args.parens Args.name))) output_named_thms)];
132 val _ = O.add_commands
133 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
134 (ThyLoad.check_thy Path.current name; Pretty.str name))))];
137 (* Isar entities (with index) *)
141 val arg = enclose "{" "}" o clean_string;
143 fun output_entity markup index kind src ctxt (logic, name) =
147 "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name)
149 (Output.output (if ! O.source then str_of_source src else name)
150 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
151 |> (if ! O.quotes then quote else I)
152 |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
153 else enclose "\\mbox{\\isa{" "}}"));
155 fun entity markup index kind =
156 O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
157 (output_entity markup index kind);
159 fun entity_antiqs markup kind =
160 [(kind, entity markup NONE kind),
161 (kind ^ "_def", entity markup (SOME true) kind),
162 (kind ^ "_ref", entity markup (SOME false) kind)];
166 val _ = O.add_commands
167 (entity_antiqs "" "syntax" @
168 entity_antiqs "isacommand" "command" @
169 entity_antiqs "isakeyword" "keyword" @
170 entity_antiqs "isakeyword" "element" @
171 entity_antiqs "" "method" @
172 entity_antiqs "" "attribute" @
173 entity_antiqs "" "fact" @
174 entity_antiqs "" "variable" @
175 entity_antiqs "" "case" @
176 entity_antiqs "" "antiquotation");