1 (* Title: Doc/antiquote_setup.ML
4 Auxiliary antiquotations for the Isabelle manuals.
7 structure AntiquoteSetup: sig end =
10 structure O = ThyOutput;
15 fun translate f = Symbol.explode #> map f #> implode;
17 val clean_string = translate
27 fun clean_name "\\<dots>" = "dots"
28 | clean_name ".." = "ddot"
29 | clean_name "." = "dot"
30 | clean_name "_" = "underscore"
31 | clean_name "{" = "braceleft"
32 | clean_name "}" = "braceright"
33 | clean_name s = s |> translate (fn "_" => "-" | "\\<dash>" => "-" | c => c);
38 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
40 val _ = O.add_commands
41 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
42 split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
49 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
50 | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
52 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
53 | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
55 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
56 | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
58 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
60 fun ml_functor _ = "val _ = ();"; (*no check!*)
62 fun index_ml kind ml src ctxt (txt1, txt2) =
64 val txt = if txt2 = "" then txt1 else
65 if kind = "type" then txt1 ^ " = " ^ txt2
66 else if kind = "exception" then txt1 ^ " of " ^ txt2
67 else txt1 ^ ": " ^ txt2;
68 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
69 val _ = writeln (ml (txt1, txt2));
70 val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
72 "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
74 |> (if ! O.quotes then quote else I)
75 |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
76 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
79 fun output_ml ctxt src =
80 if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
83 #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
84 #> space_implode "\\isasep\\isanewline%\n";
86 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
90 val _ = O.add_commands
91 [("index_ML", ml_args (index_ml "" ml_val)),
92 ("index_ML_type", ml_args (index_ml "type" ml_type)),
93 ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
94 ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
95 ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
96 ("ML_functor", O.args (Scan.lift Args.name) output_ml),
97 ("ML_text", O.args (Scan.lift Args.name) output_ml)];
102 (* theorems with names *)
106 fun output_named_thms src ctxt xs =
107 map (apfst (ThyOutput.pretty_thm ctxt)) xs (*always pretty in order to exhibit errors!*)
108 |> (if ! O.quotes then map (apfst Pretty.quote) else I)
109 |> (if ! O.display then
111 Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
112 "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
113 #> space_implode "\\par\\smallskip%\n"
114 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
117 Output.output (Pretty.str_of p) ^
118 "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
119 #> space_implode "\\par\\smallskip%\n"
120 #> enclose "\\isa{" "}");
124 val _ = O.add_commands
125 [("named_thms", O.args (Scan.repeat (Attrib.thm --
126 Scan.lift (Args.parens Args.name))) output_named_thms)];
133 val _ = O.add_commands
134 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
135 (ThyLoad.check_thy Path.current name; Pretty.str name))))];
138 (* Isabelle entities (with index) *)
142 fun no_check _ _ = true;
144 fun thy_check intern defined ctxt =
145 let val thy = ProofContext.theory_of ctxt
146 in defined thy o intern thy end;
148 fun check_tool name =
149 File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
151 val arg = enclose "{" "}" o clean_string;
153 fun output_entity check markup index kind ctxt (logic, name) =
155 val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}";
157 enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
158 index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
163 "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
165 if check ctxt name then
168 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
169 |> (if ! O.quotes then quote else I)
170 |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
171 else hyper o enclose "\\mbox{\\isa{" "}}"))
172 else error ("Bad " ^ kind ^ " " ^ quote name)
175 fun entity check markup index kind =
176 O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
177 (K (output_entity check markup index kind));
179 fun entity_antiqs check markup kind =
180 [(kind, entity check markup NONE kind),
181 (kind ^ "_def", entity check markup (SOME true) kind),
182 (kind ^ "_ref", entity check markup (SOME false) kind)];
186 val _ = O.add_commands
187 (entity_antiqs no_check "" "syntax" @
188 entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @
189 entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @
190 entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @
191 entity_antiqs (thy_check Method.intern Method.defined) "" "method" @
192 entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @
193 entity_antiqs no_check "" "fact" @
194 entity_antiqs no_check "" "variable" @
195 entity_antiqs no_check "" "case" @
196 entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @
197 entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @
198 entity_antiqs no_check "" "inference" @
199 entity_antiqs no_check "isatt" "executable" @
200 entity_antiqs (K check_tool) "isatt" "tool" @
201 entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @
202 entity_antiqs (K ThyInfo.known_thy) "" "theory");