author | wenzelm |
Thu, 05 Mar 2009 10:54:03 +0100 | |
changeset 30278 | 18ce07e05a95 |
parent 30242 | aea5d7fa7ef5 |
child 30358 | f7fea73b97a6 |
permissions | -rw-r--r-- |
24584 | 1 |
(* Title: Doc/antiquote_setup.ML |
21375 | 2 |
Author: Makarius |
3 |
||
26742 | 4 |
Auxiliary antiquotations for the Isabelle manuals. |
21375 | 5 |
*) |
6 |
||
26742 | 7 |
structure AntiquoteSetup: sig end = |
8 |
struct |
|
21375 | 9 |
|
22094 | 10 |
structure O = ThyOutput; |
21375 | 11 |
|
26742 | 12 |
|
13 |
(* misc utils *) |
|
14 |
||
29736
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
15 |
fun translate f = Symbol.explode #> map f #> implode; |
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
16 |
|
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
17 |
val clean_string = translate |
26853
52cb0e965041
clean_string: map "_" to "\\_" (best used with underscore.sty);
wenzelm
parents:
26843
diff
changeset
|
18 |
(fn "_" => "\\_" |
30120
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents:
29736
diff
changeset
|
19 |
| "#" => "\\#" |
26897 | 20 |
| "<" => "$<$" |
26768 | 21 |
| ">" => "$>$" |
22 |
| "{" => "\\{" |
|
30120
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents:
29736
diff
changeset
|
23 |
| "|" => "$\\mid$" |
26768 | 24 |
| "}" => "\\}" |
29736
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
25 |
| "\\<dash>" => "-" |
26768 | 26 |
| c => c); |
26751 | 27 |
|
26897 | 28 |
fun clean_name "\\<dots>" = "dots" |
29 |
| clean_name ".." = "ddot" |
|
30 |
| clean_name "." = "dot" |
|
26910 | 31 |
| clean_name "_" = "underscore" |
26897 | 32 |
| clean_name "{" = "braceleft" |
33 |
| clean_name "}" = "braceright" |
|
29736
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents:
29726
diff
changeset
|
34 |
| clean_name s = s |> translate (fn "_" => "-" | "\\<dash>" => "-" | c => c); |
26897 | 35 |
|
26742 | 36 |
|
37 |
(* verbatim text *) |
|
38 |
||
39 |
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; |
|
40 |
||
41 |
val _ = O.add_commands |
|
42 |
[("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ => |
|
43 |
split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))]; |
|
44 |
||
45 |
||
46 |
(* ML text *) |
|
47 |
||
48 |
local |
|
49 |
||
21375 | 50 |
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");" |
51 |
| ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");"; |
|
52 |
||
53 |
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;" |
|
54 |
| ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];"; |
|
22289 | 55 |
|
23651 | 56 |
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);" |
57 |
| ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);"; |
|
22289 | 58 |
|
21375 | 59 |
fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;" |
60 |
||
61 |
fun ml_functor _ = "val _ = ();"; (*no check!*) |
|
62 |
||
63 |
fun index_ml kind ml src ctxt (txt1, txt2) = |
|
64 |
let |
|
65 |
val txt = if txt2 = "" then txt1 else |
|
26742 | 66 |
if kind = "type" then txt1 ^ " = " ^ txt2 |
67 |
else if kind = "exception" then txt1 ^ " of " ^ txt2 |
|
22289 | 68 |
else txt1 ^ ": " ^ txt2; |
21375 | 69 |
val txt' = if kind = "" then txt else kind ^ " " ^ txt; |
22289 | 70 |
val _ = writeln (ml (txt1, txt2)); |
28273
17f6aa02ded3
simplified ML_Context.eval_in -- expect immutable Proof.context value;
wenzelm
parents:
28237
diff
changeset
|
71 |
val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2)); |
30120
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents:
29736
diff
changeset
|
72 |
val kind' = if kind = "" then "ML" else "ML " ^ kind; |
21375 | 73 |
in |
30120
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents:
29736
diff
changeset
|
74 |
"\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^ |
28759 | 75 |
(txt' |
21375 | 76 |
|> (if ! O.quotes then quote else I) |
77 |
|> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" |
|
26751 | 78 |
else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")) |
21375 | 79 |
end; |
80 |
||
26461 | 81 |
fun output_ml ctxt src = |
82 |
if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" |
|
26742 | 83 |
else |
26461 | 84 |
split_lines |
85 |
#> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|") |
|
86 |
#> space_implode "\\isasep\\isanewline%\n"; |
|
87 |
||
26742 | 88 |
fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x; |
89 |
||
90 |
in |
|
21375 | 91 |
|
26742 | 92 |
val _ = O.add_commands |
93 |
[("index_ML", ml_args (index_ml "" ml_val)), |
|
94 |
("index_ML_type", ml_args (index_ml "type" ml_type)), |
|
95 |
("index_ML_exn", ml_args (index_ml "exception" ml_exn)), |
|
96 |
("index_ML_structure", ml_args (index_ml "structure" ml_structure)), |
|
97 |
("index_ML_functor", ml_args (index_ml "functor" ml_functor)), |
|
98 |
("ML_functor", O.args (Scan.lift Args.name) output_ml), |
|
99 |
("ML_text", O.args (Scan.lift Args.name) output_ml)]; |
|
21375 | 100 |
|
26742 | 101 |
end; |
21375 | 102 |
|
23846 | 103 |
|
104 |
(* theorems with names *) |
|
105 |
||
26742 | 106 |
local |
23846 | 107 |
|
26742 | 108 |
fun output_named_thms src ctxt xs = |
28644 | 109 |
map (apfst (ThyOutput.pretty_thm ctxt)) xs (*always pretty in order to exhibit errors!*) |
23846 | 110 |
|> (if ! O.quotes then map (apfst Pretty.quote) else I) |
111 |
|> (if ! O.display then |
|
112 |
map (fn (p, name) => |
|
113 |
Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^ |
|
28644 | 114 |
"\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}") |
23846 | 115 |
#> space_implode "\\par\\smallskip%\n" |
116 |
#> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
|
117 |
else |
|
118 |
map (fn (p, name) => |
|
119 |
Output.output (Pretty.str_of p) ^ |
|
28644 | 120 |
"\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}") |
23846 | 121 |
#> space_implode "\\par\\smallskip%\n" |
122 |
#> enclose "\\isa{" "}"); |
|
123 |
||
21375 | 124 |
in |
125 |
||
126 |
val _ = O.add_commands |
|
26742 | 127 |
[("named_thms", O.args (Scan.repeat (Attrib.thm -- |
26751 | 128 |
Scan.lift (Args.parens Args.name))) output_named_thms)]; |
21375 | 129 |
|
130 |
end; |
|
26742 | 131 |
|
132 |
||
133 |
(* theory files *) |
|
134 |
||
135 |
val _ = O.add_commands |
|
136 |
[("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name => |
|
137 |
(ThyLoad.check_thy Path.current name; Pretty.str name))))]; |
|
138 |
||
26751 | 139 |
|
28218 | 140 |
(* Isabelle entities (with index) *) |
26751 | 141 |
|
142 |
local |
|
143 |
||
26894 | 144 |
fun no_check _ _ = true; |
145 |
||
146 |
fun thy_check intern defined ctxt = |
|
147 |
let val thy = ProofContext.theory_of ctxt |
|
148 |
in defined thy o intern thy end; |
|
149 |
||
28237 | 150 |
fun check_tool name = |
151 |
File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name)); |
|
152 |
||
26751 | 153 |
val arg = enclose "{" "}" o clean_string; |
154 |
||
26894 | 155 |
fun output_entity check markup index kind ctxt (logic, name) = |
26897 | 156 |
let |
157 |
val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}"; |
|
158 |
val hyper = |
|
159 |
enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #> |
|
160 |
index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}"; |
|
161 |
val idx = |
|
162 |
(case index of |
|
163 |
NONE => "" |
|
164 |
| SOME is_def => |
|
165 |
"\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); |
|
166 |
in |
|
167 |
if check ctxt name then |
|
168 |
idx ^ |
|
169 |
(Output.output name |
|
170 |
|> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}") |
|
171 |
|> (if ! O.quotes then quote else I) |
|
172 |
|> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" |
|
173 |
else hyper o enclose "\\mbox{\\isa{" "}}")) |
|
28237 | 174 |
else error ("Bad " ^ kind ^ " " ^ quote name) |
26897 | 175 |
end; |
26751 | 176 |
|
26894 | 177 |
fun entity check markup index kind = |
26751 | 178 |
O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name)) |
26894 | 179 |
(K (output_entity check markup index kind)); |
26897 | 180 |
|
26894 | 181 |
fun entity_antiqs check markup kind = |
182 |
[(kind, entity check markup NONE kind), |
|
26897 | 183 |
(kind ^ "_def", entity check markup (SOME true) kind), |
26894 | 184 |
(kind ^ "_ref", entity check markup (SOME false) kind)]; |
26751 | 185 |
|
186 |
in |
|
187 |
||
188 |
val _ = O.add_commands |
|
26894 | 189 |
(entity_antiqs no_check "" "syntax" @ |
27353
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26910
diff
changeset
|
190 |
entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @ |
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26910
diff
changeset
|
191 |
entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @ |
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26910
diff
changeset
|
192 |
entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @ |
26894 | 193 |
entity_antiqs (thy_check Method.intern Method.defined) "" "method" @ |
194 |
entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @ |
|
195 |
entity_antiqs no_check "" "fact" @ |
|
196 |
entity_antiqs no_check "" "variable" @ |
|
197 |
entity_antiqs no_check "" "case" @ |
|
28217
21f0c2de0a38
added formal markup for setting, executable, tool;
wenzelm
parents:
27809
diff
changeset
|
198 |
entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @ |
28237 | 199 |
entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @ |
29726 | 200 |
entity_antiqs no_check "" "inference" @ |
28217
21f0c2de0a38
added formal markup for setting, executable, tool;
wenzelm
parents:
27809
diff
changeset
|
201 |
entity_antiqs no_check "isatt" "executable" @ |
28237 | 202 |
entity_antiqs (K check_tool) "isatt" "tool" @ |
28399 | 203 |
entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @ |
204 |
entity_antiqs (K ThyInfo.known_thy) "" "theory"); |
|
26751 | 205 |
|
26742 | 206 |
end; |
26751 | 207 |
|
208 |
end; |