46
|
1 |
|
|
2 |
theory Antiquotes
|
346
|
3 |
imports "../Appendix"
|
46
|
4 |
begin
|
|
5 |
|
565
|
6 |
section \<open>Useful Document Antiquotations\label{rec:docantiquotations}\<close>
|
46
|
7 |
|
565
|
8 |
text \<open>
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
9 |
{\bf Problem:}
|
51
|
10 |
How to keep your ML-code inside a document synchronised with the actual code?\smallskip
|
46
|
11 |
|
191
|
12 |
{\bf Solution:} This can be achieved with document antiquotations.\smallskip
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
13 |
|
51
|
14 |
Document antiquotations can be used for ensuring consistent type-setting of
|
|
15 |
various entities in a document. They can also be used for sophisticated
|
191
|
16 |
\LaTeX-hacking. If you type on the Isabelle level
|
565
|
17 |
\<close>
|
191
|
18 |
|
|
19 |
print_antiquotations
|
153
|
20 |
|
565
|
21 |
text \<open>
|
191
|
22 |
you obtain a list of all currently available document antiquotations and
|
|
23 |
their options.
|
46
|
24 |
|
191
|
25 |
Below we will give the code for two additional document
|
|
26 |
antiquotations both of which are intended to typeset ML-code. The crucial point
|
|
27 |
of these document antiquotations is that they not just print the ML-code, but also
|
|
28 |
check whether it compiles. This will provide a sanity check for the code
|
|
29 |
and also allows you to keep documents in sync with other code, for example
|
|
30 |
Isabelle.
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
31 |
|
565
|
32 |
We first describe the antiquotation \<open>ML_checked\<close> with the syntax:
|
74
|
33 |
|
569
|
34 |
@{text [display] \<open>@{ML_checked "a_piece_of_code"}\<close>}
|
74
|
35 |
|
|
36 |
The code is checked by sending the ML-expression @{text [quotes] "val _ =
|
569
|
37 |
a_piece_of_code"} to the ML-compiler (i.e.~the function @{ML \<open>ML_Context.eval_source_in\<close>} in Line 7 below). The complete code of the
|
175
|
38 |
document antiquotation is as follows:
|
51
|
39 |
|
565
|
40 |
\<close>
|
562
|
41 |
ML \<open>Input.pos_of\<close>
|
565
|
42 |
ML%linenosgray\<open>fun ml_enclose bg en source =
|
573
|
43 |
ML_Lex.read bg @ ML_Lex.read_source source @ ML_Lex.read en;\<close>
|
46
|
44 |
|
565
|
45 |
ML%linenosgray\<open>fun ml_val code_txt = (ml_enclose "val _ = " "" code_txt)
|
46
|
46 |
|
562
|
47 |
fun output_ml ctxt code_txt =
|
553
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
48 |
let
|
574
|
49 |
val _ = ML_Context.eval_in (SOME ctxt) ML_Compiler.flags
|
|
50 |
(Input.pos_of code_txt) (ml_val code_txt)
|
562
|
51 |
in
|
573
|
52 |
Pretty.str (fst (Input.source_content code_txt))
|
553
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
53 |
end
|
46
|
54 |
|
574
|
55 |
val ml_checked_setup = Thy_Output.antiquotation_pretty_source
|
|
56 |
@{binding "ML_checked"} (Scan.lift Args.text_input) output_ml\<close>
|
471
|
57 |
|
565
|
58 |
setup \<open>ml_checked_setup\<close>
|
46
|
59 |
|
562
|
60 |
|
565
|
61 |
text \<open>
|
569
|
62 |
The parser @{ML \<open>(Scan.lift Args.name)\<close>} in Line 7 parses a string, in this
|
191
|
63 |
case the code, and then calls the function @{ML output_ml}. As mentioned
|
|
64 |
before, the parsed code is sent to the ML-compiler in Line 4 using the
|
|
65 |
function @{ML ml_val}, which constructs the appropriate ML-expression, and
|
569
|
66 |
using @{ML \<open>eval_in\<close> in ML_Context}, which calls the compiler. If the code is
|
|
67 |
``approved'' by the compiler, then the output function @{ML \<open>output\<close> in
|
562
|
68 |
Document_Antiquotation} in the next line pretty prints the code. This function expects
|
191
|
69 |
that the code is a list of (pretty)strings where each string correspond to a
|
569
|
70 |
line in the output. Therefore the use of @{ML \<open>(space_explode "\\n" txt)\<close>
|
191
|
71 |
for txt} which produces such a list according to linebreaks. There are a
|
|
72 |
number of options for antiquotations that are observed by the function
|
569
|
73 |
@{ML \<open>output\<close> in Document_Antiquotation} when printing the code (including \<open>[display]\<close>
|
|
74 |
and \<open>[quotes]\<close>). The function @{ML \<open>antiquotation_raw\<close> in Thy_Output} in
|
191
|
75 |
Line 7 sets up the new document antiquotation.
|
51
|
76 |
|
|
77 |
\begin{readmore}
|
175
|
78 |
For more information about options of document antiquotations see \rsccite{sec:antiq}).
|
51
|
79 |
\end{readmore}
|
46
|
80 |
|
569
|
81 |
Since we used the argument @{ML \<open>Position.none\<close>}, the compiler cannot give specific
|
59
|
82 |
information about the line number, in case an error is detected. We
|
|
83 |
can improve the code above slightly by writing
|
565
|
84 |
\<close>
|
46
|
85 |
|
565
|
86 |
text \<open>
|
171
|
87 |
where in Lines 1 and 2 the positional information is properly treated. The
|
426
|
88 |
parser @{ML Parse.position} encodes the positional information in the
|
171
|
89 |
result.
|
58
|
90 |
|
565
|
91 |
We can now write \<open>@{ML_checked2 "2 + 3"}\<close> in a document in order to
|
562
|
92 |
obtain @{ML_checked "2 + 3"} and be sure that this code compiles until
|
191
|
93 |
somebody changes the definition of addition.
|
51
|
94 |
|
|
95 |
|
175
|
96 |
The second document antiquotation we describe extends the first by a pattern
|
191
|
97 |
that specifies what the result of the ML-code should be and checks the
|
175
|
98 |
consistency of the actual result with the given pattern. For this we are
|
191
|
99 |
going to implement the document antiquotation:
|
175
|
100 |
|
74
|
101 |
|
569
|
102 |
@{text [display] \<open>@{ML_resp "a_piece_of_code" "a_pattern"}\<close>}
|
74
|
103 |
|
168
|
104 |
To add some convenience and also to deal with large outputs, the user can
|
565
|
105 |
give a partial specification by using ellipses. For example \<open>(\<dots>, \<dots>)\<close>
|
191
|
106 |
for specifying a pair. In order to check consistency between the pattern
|
|
107 |
and the output of the code, we have to change the ML-expression that is sent
|
565
|
108 |
to the compiler: in \<open>ML_checked2\<close> we sent the expression @{text [quotes]
|
|
109 |
"val _ = a_piece_of_code"} to the compiler; now the wildcard \<open>_\<close>
|
191
|
110 |
must be be replaced by the given pattern. However, we have to remove all
|
|
111 |
ellipses from it and replace them by @{text [quotes] "_"}. The following
|
|
112 |
function will do this:
|
565
|
113 |
\<close>
|
46
|
114 |
|
565
|
115 |
ML%linenosgray\<open>fun ml_pat pat code =
|
574
|
116 |
ML_Lex.read "val" @
|
|
117 |
ML_Lex.read_source pat @
|
|
118 |
ML_Lex.read " = " @
|
|
119 |
ML_Lex.read_source code\<close>
|
562
|
120 |
|
565
|
121 |
text \<open>
|
191
|
122 |
Next we add a response indicator to the result using:
|
565
|
123 |
\<close>
|
51
|
124 |
|
|
125 |
|
565
|
126 |
ML %grayML\<open>fun add_resp pat = map (fn s => "> " ^ s) pat\<close>
|
46
|
127 |
|
565
|
128 |
text \<open>
|
|
129 |
The rest of the code of \<open>ML_resp\<close> is:
|
|
130 |
\<close>
|
51
|
131 |
|
562
|
132 |
ML %linenosgray\<open>
|
|
133 |
fun output_ml_resp ctxt (code_txt, pat) =
|
|
134 |
let
|
574
|
135 |
val _ = ML_Context.eval_in (SOME ctxt) ML_Compiler.flags
|
|
136 |
(Input.pos_of code_txt) (ml_pat pat code_txt)
|
573
|
137 |
val code = space_explode "\n" (fst (Input.source_content code_txt))
|
|
138 |
val resp = add_resp (space_explode "\n" (fst (Input.source_content pat)))
|
562
|
139 |
in
|
|
140 |
Pretty.str (cat_lines (code @ resp))
|
|
141 |
end
|
|
142 |
|
574
|
143 |
val ml_response_setup = Thy_Output.antiquotation_pretty_source
|
|
144 |
@{binding "ML_resp"}
|
|
145 |
(Scan.lift (Args.text_input -- Args.text_input))
|
|
146 |
output_ml_resp
|
562
|
147 |
|
|
148 |
\<close>
|
|
149 |
|
565
|
150 |
setup \<open>ml_response_setup\<close>
|
46
|
151 |
|
562
|
152 |
(* FIXME *)
|
565
|
153 |
text \<open>
|
|
154 |
In comparison with \<open>ML_checked\<close>, we only changed the line about
|
191
|
155 |
the compiler (Line~2), the lines about
|
|
156 |
the output (Lines 4 to 7) and the parser in the setup (Line 11). Now
|
|
157 |
you can write
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
158 |
|
569
|
159 |
@{text [display] \<open>@{ML_resp [display] "true andalso false" "false"}\<close>}
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
160 |
|
51
|
161 |
to obtain
|
46
|
162 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
163 |
@{ML_resp [display] "true andalso false" "false"}
|
46
|
164 |
|
51
|
165 |
or
|
|
166 |
|
569
|
167 |
@{text [display] \<open>@{ML_resp [display] "let val i = 3 in (i * i, "foo") end" "(9, \<dots>)"}\<close>}
|
51
|
168 |
|
|
169 |
to obtain
|
|
170 |
|
562
|
171 |
@{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, _)"}
|
51
|
172 |
|
168
|
173 |
In both cases, the check by the compiler ensures that code and result
|
175
|
174 |
match. A limitation of this document antiquotation, however, is that the
|
|
175 |
pattern can only be given for values that can be constructed. This excludes
|
|
176 |
values that are abstract datatypes, like @{ML_type thm}s and @{ML_type cterm}s.
|
|
177 |
|
565
|
178 |
\<close>
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
179 |
end
|