1 (* Title: Doc/antiquote_setup.ML
5 Auxiliary antiquotations for the Isabelle manuals.
8 structure AntiquoteSetup: sig end =
11 structure O = ThyOutput;
16 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
19 if ! O.display then s else Symbol.strip_blanks s;
21 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
23 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
24 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
29 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
31 val _ = O.add_commands
32 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
33 split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
40 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
41 | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
43 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
44 | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
46 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
47 | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
49 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
51 fun ml_functor _ = "val _ = ();"; (*no check!*)
53 fun index_ml kind ml src ctxt (txt1, txt2) =
55 val txt = if txt2 = "" then txt1 else
56 if kind = "type" then txt1 ^ " = " ^ txt2
57 else if kind = "exception" then txt1 ^ " of " ^ txt2
58 else txt1 ^ ": " ^ txt2;
59 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
60 val _ = writeln (ml (txt1, txt2));
61 val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
63 "\\indexml" ^ kind ^ enclose "{" "}"
64 (translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c) txt1) ^
65 ((if ! O.source then str_of_source src else txt')
66 |> (if ! O.quotes then quote else I)
67 |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
68 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
71 fun output_ml ctxt src =
72 if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
75 #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
76 #> space_implode "\\isasep\\isanewline%\n";
78 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
82 val _ = O.add_commands
83 [("index_ML", ml_args (index_ml "" ml_val)),
84 ("index_ML_type", ml_args (index_ml "type" ml_type)),
85 ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
86 ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
87 ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
88 ("ML_functor", O.args (Scan.lift Args.name) output_ml),
89 ("ML_text", O.args (Scan.lift Args.name) output_ml)];
94 (* theorems with names *)
98 fun output_named_thms src ctxt xs =
99 map (apfst (pretty_thm ctxt)) xs (*always pretty in order to exhibit errors!*)
100 |> (if ! O.quotes then map (apfst Pretty.quote) else I)
101 |> (if ! O.display then
103 Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
104 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
105 #> space_implode "\\par\\smallskip%\n"
106 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
109 Output.output (Pretty.str_of p) ^
110 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
111 #> space_implode "\\par\\smallskip%\n"
112 #> enclose "\\isa{" "}");
116 val _ = O.add_commands
117 [("named_thms", O.args (Scan.repeat (Attrib.thm --
118 Scan.lift (Args.$$$ "(" |-- Args.name --| Args.$$$ ")"))) output_named_thms)];
125 val _ = O.add_commands
126 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
127 (ThyLoad.check_thy Path.current name; Pretty.str name))))];