1 (* Title: doc-src/antiquote_setup.ML
4 Auxiliary antiquotations for the Isabelle manuals.
7 structure Antiquote_Setup: 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 "_" => "-" | "\<hyphen>" => "-" | c => c);
36 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
38 val _ = Thy_Output.antiquotation "verbatim" (Scan.lift Args.name)
39 (K (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 XXX() = struct structure XX = " ^ txt ^ " end;";
57 fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
59 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
62 (case filter is_name (ML_Lex.tokenize txt) of
63 toks as [_] => ML_Lex.flatten toks
64 | _ => error ("Single ML name expected in input: " ^ quote txt));
66 fun index_ml name kind ml = Thy_Output.antiquotation name
67 (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
68 (fn {context = ctxt, ...} => fn (txt1, txt2) =>
71 if txt2 = "" then txt1
72 else if kind = "type" then txt1 ^ " = " ^ txt2
73 else if kind = "exception" then txt1 ^ " of " ^ txt2
74 else if Lexicon.is_identifier (Long_Name.base_name (ml_name txt1))
75 then txt1 ^ ": " ^ txt2
76 else txt1 ^ " : " ^ txt2;
77 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
78 val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2)); (* ML_Lex.read (!?) *)
79 val kind' = if kind = "" then "ML" else "ML " ^ kind;
81 "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
83 |> (if Config.get ctxt Thy_Output.quotes then quote else I)
84 |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
85 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
90 val _ = index_ml "index_ML" "" ml_val;
91 val _ = index_ml "index_ML_type" "type" ml_type;
92 val _ = index_ml "index_ML_exn" "exception" ml_exn;
93 val _ = index_ml "index_ML_structure" "structure" ml_structure;
94 val _ = index_ml "index_ML_functor" "functor" ml_functor;
101 val _ = Thy_Output.antiquotation "named_thms"
102 (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
103 (fn {context = ctxt, ...} =>
104 map (apfst (Thy_Output.pretty_thm ctxt))
105 #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
106 #> (if Config.get ctxt Thy_Output.display
109 Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
110 "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
111 #> space_implode "\\par\\smallskip%\n"
112 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
115 Output.output (Pretty.str_of p) ^
116 "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
117 #> space_implode "\\par\\smallskip%\n"
118 #> enclose "\\isa{" "}"));
123 val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name)
124 (fn {context = ctxt, ...} =>
125 fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
128 (* Isabelle/Isar entities (with index) *)
132 fun no_check _ _ = true;
134 fun thy_check intern defined ctxt =
135 let val thy = Proof_Context.theory_of ctxt
136 in defined thy o intern thy end;
138 fun check_tool name =
139 File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
141 val arg = enclose "{" "}" o clean_string;
143 fun entity check markup kind index =
144 Thy_Output.antiquotation
145 (translate (fn " " => "_" | c => c) kind ^
146 (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))
147 (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
148 (fn {context = ctxt, ...} => fn (logic, name) =>
151 "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
153 enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
154 index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
159 "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
161 if check ctxt name then
164 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
165 |> (if Config.get ctxt Thy_Output.quotes then quote else I)
166 |> (if Config.get ctxt Thy_Output.display
167 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_antiqs check markup kind =
173 ((entity check markup kind NONE);
174 (entity check markup kind (SOME true));
175 (entity check markup kind (SOME false)));
179 val _ = entity_antiqs no_check "" "syntax";
180 val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command";
181 val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword";
182 val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element";
183 val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
184 val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
185 val _ = entity_antiqs no_check "" "fact";
186 val _ = entity_antiqs no_check "" "variable";
187 val _ = entity_antiqs no_check "" "case";
188 val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation";
189 val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option";
190 val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
191 val _ = entity_antiqs no_check "" "inference";
192 val _ = entity_antiqs no_check "isatt" "executable";
193 val _ = entity_antiqs (K check_tool) "isatt" "tool";
194 val _ = entity_antiqs (K (can Thy_Info.get_theory)) "" "theory";
197 (thy_check ML_Context.intern_antiq ML_Context.defined_antiq) "" Markup.ML_antiquotationN;