1 (* Title: Doc/antiquote_setup.ML
4 Auxiliary antiquotations for the Isabelle manuals.
7 structure AntiquoteSetup: sig end =
12 fun translate f = Symbol.explode #> map f #> implode;
14 val clean_string = translate
25 fun clean_name "\\<dots>" = "dots"
26 | clean_name ".." = "ddot"
27 | clean_name "." = "dot"
28 | clean_name "_" = "underscore"
29 | clean_name "{" = "braceleft"
30 | clean_name "}" = "braceright"
31 | clean_name s = s |> translate (fn "_" => "-" | "\\<dash>" => "-" | c => c);
36 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
38 val _ = ThyOutput.antiquotation "verbatim" (fn _ => fn _ =>
39 Scan.lift Args.name >> (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
46 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
47 | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
49 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
50 | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
52 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
53 | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
55 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
57 fun ml_functor _ = "val _ = ();"; (*no check!*)
59 fun index_ml kind ml src ctxt (txt1, txt2) =
61 val txt = if txt2 = "" then txt1 else
62 if kind = "type" then txt1 ^ " = " ^ txt2
63 else if kind = "exception" then txt1 ^ " of " ^ txt2
64 else txt1 ^ ": " ^ txt2;
65 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
66 val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
67 val kind' = if kind = "" then "ML" else "ML " ^ kind;
69 "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
71 |> (if ! ThyOutput.quotes then quote else I)
72 |> (if ! ThyOutput.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
73 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
76 fun output_ml ctxt src =
77 if ! ThyOutput.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
80 #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
81 #> space_implode "\\isasep\\isanewline%\n";
83 fun ml_args x = ThyOutput.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
87 val _ = ThyOutput.add_commands
88 [("index_ML", ml_args (index_ml "" ml_val)),
89 ("index_ML_type", ml_args (index_ml "type" ml_type)),
90 ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
91 ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
92 ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
93 ("ML_functor", ThyOutput.args (Scan.lift Args.name) output_ml),
94 ("ML_text", ThyOutput.args (Scan.lift Args.name) output_ml)];
99 (* theorems with names *)
103 fun output_named_thms src ctxt xs =
104 map (apfst (ThyOutput.pretty_thm ctxt)) xs (*always pretty in order to exhibit errors!*)
105 |> (if ! ThyOutput.quotes then map (apfst Pretty.quote) else I)
106 |> (if ! ThyOutput.display then
108 Output.output (Pretty.string_of (Pretty.indent (! ThyOutput.indent) p)) ^
109 "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
110 #> space_implode "\\par\\smallskip%\n"
111 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
114 Output.output (Pretty.str_of p) ^
115 "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
116 #> space_implode "\\par\\smallskip%\n"
117 #> enclose "\\isa{" "}");
121 val _ = ThyOutput.add_commands
122 [("named_thms", ThyOutput.args (Scan.repeat (Attrib.thm --
123 Scan.lift (Args.parens Args.name))) output_named_thms)];
130 val _ = ThyOutput.add_commands
131 [("thy_file", ThyOutput.args (Scan.lift Args.name) (ThyOutput.output (fn _ => fn name =>
132 (ThyLoad.check_thy Path.current name; Pretty.str name))))];
135 (* Isabelle entities (with index) *)
139 fun no_check _ _ = true;
141 fun thy_check intern defined ctxt =
142 let val thy = ProofContext.theory_of ctxt
143 in defined thy o intern thy end;
145 fun check_tool name =
146 File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
148 val arg = enclose "{" "}" o clean_string;
150 fun output_entity check markup index kind ctxt (logic, name) =
152 val hyper_name = "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
154 enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
155 index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
160 "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
162 if check ctxt name then
165 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
166 |> (if ! ThyOutput.quotes then quote else I)
167 |> (if ! ThyOutput.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
168 else hyper o enclose "\\mbox{\\isa{" "}}"))
169 else error ("Bad " ^ kind ^ " " ^ quote name)
172 fun entity check markup index kind =
173 ThyOutput.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
174 (K (output_entity check markup index kind));
176 fun entity_antiqs check markup kind =
177 [(kind, entity check markup NONE kind),
178 (kind ^ "_def", entity check markup (SOME true) kind),
179 (kind ^ "_ref", entity check markup (SOME false) kind)];
183 val _ = ThyOutput.add_commands
184 (entity_antiqs no_check "" "syntax" @
185 entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @
186 entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @
187 entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @
188 entity_antiqs (thy_check Method.intern Method.defined) "" "method" @
189 entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @
190 entity_antiqs no_check "" "fact" @
191 entity_antiqs no_check "" "variable" @
192 entity_antiqs no_check "" "case" @
193 entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @
194 entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @
195 entity_antiqs no_check "" "inference" @
196 entity_antiqs no_check "isatt" "executable" @
197 entity_antiqs (K check_tool) "isatt" "tool" @
198 entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @
199 entity_antiqs (K ThyInfo.known_thy) "" "theory");