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