46
|
1 |
|
|
2 |
theory Antiquotes
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
3 |
imports "../Base"
|
46
|
4 |
begin
|
|
5 |
|
|
6 |
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
7 |
section {* Useful Document Antiquotations *}
|
46
|
8 |
|
|
9 |
text {*
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
10 |
{\bf Problem:}
|
51
|
11 |
How to keep your ML-code inside a document synchronised with the actual code?\smallskip
|
46
|
12 |
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
13 |
{\bf Solution:} This can be achieved using document antiquotations.\smallskip
|
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
14 |
|
51
|
15 |
Document antiquotations can be used for ensuring consistent type-setting of
|
|
16 |
various entities in a document. They can also be used for sophisticated
|
65
|
17 |
\LaTeX-hacking. If you type @{text "Ctrl-c Ctrl-a h A"} inside ProofGeneral, you
|
74
|
18 |
obtain a list of all currently available document antiquotations and their options.
|
153
|
19 |
You obtain the same list on the ML-level by typing
|
|
20 |
|
|
21 |
@{ML [display,gray] "ThyOutput.print_antiquotations ()"}
|
46
|
22 |
|
175
|
23 |
Below we give the code for two additional document antiquotations that can
|
|
24 |
be used to typeset ML-code and also to check whether the given code actually
|
|
25 |
compiles. This provides a sanity check for the code and also allows one to
|
|
26 |
keep documents in sync with other code, for example Isabelle.
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
27 |
|
74
|
28 |
We first describe the antiquotation @{text "ML_checked"} with the syntax:
|
|
29 |
|
|
30 |
@{text [display] "@{ML_checked \"a_piece_of_code\"}"}
|
|
31 |
|
|
32 |
The code is checked by sending the ML-expression @{text [quotes] "val _ =
|
|
33 |
a_piece_of_code"} to the ML-compiler (i.e.~the function @{ML
|
|
34 |
"ML_Context.eval_in"} in Line 4 below). The complete code of the
|
175
|
35 |
document antiquotation is as follows:
|
51
|
36 |
|
46
|
37 |
*}
|
|
38 |
|
114
|
39 |
ML%linenosgray{*fun ml_val code_txt = "val _ = " ^ code_txt
|
46
|
40 |
|
168
|
41 |
fun output_ml {context = ctxt, ...} code_txt =
|
59
|
42 |
(ML_Context.eval_in (SOME ctxt) false Position.none (ml_val code_txt);
|
168
|
43 |
ThyOutput.output (map Pretty.str (space_explode "\n" code_txt)))
|
46
|
44 |
|
165
|
45 |
val _ = ThyOutput.antiquotation "ML_checked" (Scan.lift Args.name) output_ml*}
|
46
|
46 |
|
|
47 |
text {*
|
175
|
48 |
The parser @{ML "(Scan.lift Args.name)"} in line 9 parses a string, in this
|
|
49 |
case the code. As mentioned before, the code is sent to the ML-compiler in
|
|
50 |
the line 4 using the function @{ML ml_val}, which constructs the appropriate
|
|
51 |
ML-expression. If the code is ``approved'' by the compiler, then the output
|
|
52 |
function @{ML "ThyOutput.output"} in the next line pretty prints the
|
|
53 |
code. This function expects that the code is a list of (pretty)strings where
|
|
54 |
each string correspond to a line in the output. Therefore the use of @{ML
|
|
55 |
"(space_explode \"\\n\" txt)" for txt} which produces this list according to
|
|
56 |
linebreaks. There are a number of options for antiquotations that are
|
|
57 |
observed by @{ML ThyOutput.output} when printing the code (including @{text
|
|
58 |
"[display]"} and @{text "[quotes]"}). Line 7 sets up the new document
|
|
59 |
antiquotation.
|
51
|
60 |
|
|
61 |
|
|
62 |
\begin{readmore}
|
175
|
63 |
For more information about options of document antiquotations see \rsccite{sec:antiq}).
|
51
|
64 |
\end{readmore}
|
46
|
65 |
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
66 |
Since we used the argument @{ML "Position.none"}, the compiler cannot give specific
|
59
|
67 |
information about the line number, in case an error is detected. We
|
|
68 |
can improve the code above slightly by writing
|
46
|
69 |
*}
|
|
70 |
|
171
|
71 |
ML%linenosgray{*fun output_ml {context = ctxt, ...} (code_txt, pos) =
|
59
|
72 |
(ML_Context.eval_in (SOME ctxt) false pos (ml_val code_txt);
|
168
|
73 |
ThyOutput.output (map Pretty.str (space_explode "\n" code_txt)))
|
46
|
74 |
|
165
|
75 |
val _ = ThyOutput.antiquotation "ML_checked"
|
168
|
76 |
(Scan.lift (OuterParse.position Args.name)) output_ml *}
|
46
|
77 |
|
|
78 |
text {*
|
171
|
79 |
where in Lines 1 and 2 the positional information is properly treated. The
|
|
80 |
parser @{ML OuterParse.position} encodes the positional information in the
|
|
81 |
result.
|
58
|
82 |
|
51
|
83 |
We can now write in a document @{text "@{ML_checked \"2 + 3\"}"} in order to
|
|
84 |
obtain @{ML_checked "2 + 3"} and be sure that this code compiles until
|
58
|
85 |
somebody changes the definition of \mbox{@{ML "(op +)"}}.
|
51
|
86 |
|
|
87 |
|
175
|
88 |
The second document antiquotation we describe extends the first by a pattern
|
|
89 |
that specifies what the result of the ML-code should be and check the
|
|
90 |
consistency of the actual result with the given pattern. For this we are
|
|
91 |
going to implement the document antiquotation
|
|
92 |
|
74
|
93 |
|
168
|
94 |
@{text [display] "@{ML_resp \"a_piece_of_code\" \"a_pattern\"}"}
|
74
|
95 |
|
168
|
96 |
To add some convenience and also to deal with large outputs, the user can
|
|
97 |
give a partial specification inside the pattern by giving abbreviations of
|
175
|
98 |
the form @{text [quotes] "\<dots>"}. For example @{text "(\<dots>, \<dots>)"} for specifying a
|
168
|
99 |
pair.
|
51
|
100 |
|
175
|
101 |
In the document antiquotation @{text "@{ML_checked \"piece_of_code\"}"}
|
|
102 |
above we have sent the expression @{text [quotes] "val _ = piece_of_code"}
|
|
103 |
to the compiler, now instead the wildcard @{text "_"} we will be replaced by
|
|
104 |
the given pattern. To do this we need to replace in the input the @{text
|
|
105 |
[quotes] "\<dots>"} by @{text [quotes] "_"} before sending the code to the
|
|
106 |
compiler. The following function will do this:
|
46
|
107 |
*}
|
|
108 |
|
69
|
109 |
ML{*fun ml_pat (code_txt, pat) =
|
168
|
110 |
let val pat' =
|
59
|
111 |
implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
|
168
|
112 |
in
|
|
113 |
"val " ^ pat' ^ " = " ^ code_txt
|
|
114 |
end*}
|
46
|
115 |
|
51
|
116 |
text {*
|
59
|
117 |
Next we like to add a response indicator to the result using:
|
51
|
118 |
*}
|
|
119 |
|
|
120 |
|
175
|
121 |
ML{*fun add_resp pat = map (fn s => "> " ^ s) pat*}
|
46
|
122 |
|
51
|
123 |
text {*
|
175
|
124 |
The rest of the code of the document antiquotation is
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
125 |
*}
|
51
|
126 |
|
168
|
127 |
ML{*fun output_ml_resp {context = ctxt, ...} ((code_txt, pat), pos) =
|
|
128 |
(ML_Context.eval_in (SOME ctxt) false pos (ml_pat (code_txt, pat));
|
59
|
129 |
let
|
175
|
130 |
val output1 = space_explode "\n" code_txt
|
|
131 |
val output2 = add_resp (space_explode "\n" pat)
|
59
|
132 |
in
|
175
|
133 |
ThyOutput.output (map Pretty.str (output1 @ output2))
|
59
|
134 |
end)
|
46
|
135 |
|
165
|
136 |
val _ = ThyOutput.antiquotation "ML_resp"
|
168
|
137 |
(Scan.lift (OuterParse.position (Args.name -- Args.name)))
|
|
138 |
output_ml_resp*}
|
46
|
139 |
|
51
|
140 |
text {*
|
175
|
141 |
This extended document antiquotation allows us to write
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
142 |
|
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
143 |
@{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>
diff
changeset
|
144 |
|
51
|
145 |
to obtain
|
46
|
146 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
147 |
@{ML_resp [display] "true andalso false" "false"}
|
46
|
148 |
|
51
|
149 |
or
|
|
150 |
|
168
|
151 |
@{text [display] "@{ML_resp [display] \"let val i = 3 in (i * i, \"foo\") end\" \"(9, \<dots>)\"}"}
|
51
|
152 |
|
|
153 |
to obtain
|
|
154 |
|
168
|
155 |
@{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, \<dots>)"}
|
51
|
156 |
|
168
|
157 |
In both cases, the check by the compiler ensures that code and result
|
175
|
158 |
match. A limitation of this document antiquotation, however, is that the
|
|
159 |
pattern can only be given for values that can be constructed. This excludes
|
|
160 |
values that are abstract datatypes, like @{ML_type thm}s and @{ML_type cterm}s.
|
|
161 |
|
51
|
162 |
*}
|
168
|
163 |
end |