1 (* Title: Doc/antiquote_setup.ML
5 Auxiliary antiquotations for Isabelle manuals.
10 structure O = ThyOutput;
12 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
14 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
15 | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
17 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
18 | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
20 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
21 | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
23 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
25 fun ml_functor _ = "val _ = ();"; (*no check!*)
27 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
29 fun index_ml kind ml src ctxt (txt1, txt2) =
31 val txt = if txt2 = "" then txt1 else
33 then txt1 ^ " = " ^ txt2
34 else if kind = "exception"
35 then txt1 ^ " of " ^ txt2
36 else txt1 ^ ": " ^ txt2;
37 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
38 val _ = writeln (ml (txt1, txt2));
39 val _ = ML_Context.use_mltext (ml (txt1, txt2)) false (SOME (Context.Proof ctxt));
41 "\\indexml" ^ kind ^ enclose "{" "}"
42 (translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c) txt1) ^
43 ((if ! O.source then str_of_source src else txt')
44 |> (if ! O.quotes then quote else I)
45 |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
46 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
49 fun output_verbatim _ _ = split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n";
51 fun arguments x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
53 fun pretty_thy_file name = (ThyLoad.check_thy Path.current name; Pretty.str name);
56 (* theorems with names *)
59 if ! O.display then s else Symbol.strip_blanks s;
61 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
63 fun output_named_list pretty src ctxt xs =
64 map (apfst (pretty ctxt)) xs (*always pretty in order to exhibit errors!*)
65 |> (if ! O.quotes then map (apfst Pretty.quote) else I)
66 |> (if ! O.display then
68 Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
69 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
70 #> space_implode "\\par\\smallskip%\n"
71 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
74 Output.output (Pretty.str_of p) ^
75 "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
76 #> space_implode "\\par\\smallskip%\n"
77 #> enclose "\\isa{" "}");
79 fun pretty_term ctxt = ProofContext.pretty_term ctxt o ProofContext.revert_skolems ctxt;
80 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
84 val _ = O.add_commands
85 [("index_ML", arguments (index_ml "" ml_val)),
86 ("index_ML_type", arguments (index_ml "type" ml_type)),
87 ("index_ML_exn", arguments (index_ml "exception" ml_exn)),
88 ("index_ML_structure", arguments (index_ml "structure" ml_structure)),
89 ("index_ML_functor", arguments (index_ml "functor" ml_functor)),
90 ("ML_functor", O.args (Scan.lift Args.name) output_verbatim),
91 ("verbatim", O.args (Scan.lift Args.name) output_verbatim),
92 ("thy_file", O.args (Scan.lift Args.name) (O.output (K pretty_thy_file))),
93 ("named_thms", O.args (Scan.repeat (Attrib.thm --
94 Scan.lift (Args.$$$ "(" |-- Args.name --| Args.$$$ ")")))
95 (output_named_list pretty_thm))];