author | wenzelm |
Thu, 08 May 2014 19:29:01 +0200 | |
changeset 56916 | b00a861d8f16 |
parent 56594 | e3a06699a13f |
child 57347 | 5c8f4a35d8d7 |
permissions | -rw-r--r-- |
29755 | 1 |
theory "ML" |
2 |
imports Base |
|
3 |
begin |
|
18538 | 4 |
|
39822 | 5 |
chapter {* Isabelle/ML *} |
20489 | 6 |
|
39823 | 7 |
text {* Isabelle/ML is best understood as a certain culture based on |
8 |
Standard ML. Thus it is not a new programming language, but a |
|
9 |
certain way to use SML at an advanced level within the Isabelle |
|
10 |
environment. This covers a variety of aspects that are geared |
|
11 |
towards an efficient and robust platform for applications of formal |
|
12 |
logic with fully foundational proof construction --- according to |
|
40126 | 13 |
the well-known \emph{LCF principle}. There is specific |
14 |
infrastructure with library modules to address the needs of this |
|
15 |
difficult task. For example, the raw parallel programming model of |
|
16 |
Poly/ML is presented as considerably more abstract concept of |
|
17 |
\emph{future values}, which is then used to augment the inference |
|
18 |
kernel, proof interpreter, and theory loader accordingly. |
|
39823 | 19 |
|
20 |
The main aspects of Isabelle/ML are introduced below. These |
|
21 |
first-hand explanations should help to understand how proper |
|
22 |
Isabelle/ML is to be read and written, and to get access to the |
|
23 |
wealth of experience that is expressed in the source text and its |
|
24 |
history of changes.\footnote{See |
|
54703 | 25 |
@{url "http://isabelle.in.tum.de/repos/isabelle"} for the full |
39823 | 26 |
Mercurial history. There are symbolic tags to refer to official |
27 |
Isabelle releases, as opposed to arbitrary \emph{tip} versions that |
|
28 |
merely reflect snapshots that are never really up-to-date.} *} |
|
29 |
||
30 |
||
39878 | 31 |
section {* Style and orthography *} |
32 |
||
39879 | 33 |
text {* The sources of Isabelle/Isar are optimized for |
34 |
\emph{readability} and \emph{maintainability}. The main purpose is |
|
35 |
to tell an informed reader what is really going on and how things |
|
36 |
really work. This is a non-trivial aim, but it is supported by a |
|
37 |
certain style of writing Isabelle/ML that has emerged from long |
|
38 |
years of system development.\footnote{See also the interesting style |
|
39 |
guide for OCaml |
|
54703 | 40 |
@{url "http://caml.inria.fr/resources/doc/guides/guidelines.en.html"} |
39878 | 41 |
which shares many of our means and ends.} |
42 |
||
43 |
The main principle behind any coding style is \emph{consistency}. |
|
39879 | 44 |
For a single author of a small program this merely means ``choose |
39878 | 45 |
your style and stick to it''. A complex project like Isabelle, with |
39879 | 46 |
long years of development and different contributors, requires more |
47 |
standardization. A coding style that is changed every few years or |
|
48 |
with every new contributor is no style at all, because consistency |
|
49 |
is quickly lost. Global consistency is hard to achieve, though. |
|
40126 | 50 |
Nonetheless, one should always strive at least for local consistency |
51 |
of modules and sub-systems, without deviating from some general |
|
52 |
principles how to write Isabelle/ML. |
|
39878 | 53 |
|
40126 | 54 |
In a sense, good coding style is like an \emph{orthography} for the |
55 |
sources: it helps to read quickly over the text and see through the |
|
56 |
main points, without getting distracted by accidental presentation |
|
57 |
of free-style code. |
|
39878 | 58 |
*} |
59 |
||
60 |
||
61 |
subsection {* Header and sectioning *} |
|
62 |
||
39879 | 63 |
text {* Isabelle source files have a certain standardized header |
64 |
format (with precise spacing) that follows ancient traditions |
|
65 |
reaching back to the earliest versions of the system by Larry |
|
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40508
diff
changeset
|
66 |
Paulson. See @{file "~~/src/Pure/thm.ML"}, for example. |
39878 | 67 |
|
68 |
The header includes at least @{verbatim Title} and @{verbatim |
|
69 |
Author} entries, followed by a prose description of the purpose of |
|
70 |
the module. The latter can range from a single line to several |
|
71 |
paragraphs of explanations. |
|
72 |
||
73 |
The rest of the file is divided into sections, subsections, |
|
40126 | 74 |
subsubsections, paragraphs etc.\ using a simple layout via ML |
75 |
comments as follows. |
|
39878 | 76 |
|
77 |
\begin{verbatim} |
|
78 |
(*** section ***) |
|
79 |
||
80 |
(** subsection **) |
|
81 |
||
82 |
(* subsubsection *) |
|
83 |
||
84 |
(*short paragraph*) |
|
85 |
||
86 |
(* |
|
40126 | 87 |
long paragraph, |
88 |
with more text |
|
39878 | 89 |
*) |
90 |
\end{verbatim} |
|
91 |
||
92 |
As in regular typography, there is some extra space \emph{before} |
|
93 |
section headings that are adjacent to plain text (not other headings |
|
94 |
as in the example above). |
|
95 |
||
96 |
\medskip The precise wording of the prose text given in these |
|
40126 | 97 |
headings is chosen carefully to introduce the main theme of the |
39879 | 98 |
subsequent formal ML text. |
99 |
*} |
|
100 |
||
101 |
||
39880 | 102 |
subsection {* Naming conventions *} |
39879 | 103 |
|
104 |
text {* Since ML is the primary medium to express the meaning of the |
|
105 |
source text, naming of ML entities requires special care. |
|
106 |
||
39881 | 107 |
\paragraph{Notation.} A name consists of 1--3 \emph{words} (rarely |
108 |
4, but not more) that are separated by underscore. There are three |
|
40126 | 109 |
variants concerning upper or lower case letters, which are used for |
39881 | 110 |
certain ML categories as follows: |
39880 | 111 |
|
112 |
\medskip |
|
113 |
\begin{tabular}{lll} |
|
114 |
variant & example & ML categories \\\hline |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
115 |
lower-case & @{ML_text foo_bar} & values, types, record fields \\ |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
116 |
capitalized & @{ML_text Foo_Bar} & datatype constructors, structures, functors \\ |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
117 |
upper-case & @{ML_text FOO_BAR} & special values, exception constructors, signatures \\ |
39880 | 118 |
\end{tabular} |
119 |
\medskip |
|
120 |
||
121 |
For historical reasons, many capitalized names omit underscores, |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
122 |
e.g.\ old-style @{ML_text FooBar} instead of @{ML_text Foo_Bar}. |
47180 | 123 |
Genuine mixed-case names are \emph{not} used, because clear division |
40126 | 124 |
of words is essential for readability.\footnote{Camel-case was |
125 |
invented to workaround the lack of underscore in some early |
|
126 |
non-ASCII character sets. Later it became habitual in some language |
|
127 |
communities that are now strong in numbers.} |
|
39880 | 128 |
|
39881 | 129 |
A single (capital) character does not count as ``word'' in this |
40126 | 130 |
respect: some Isabelle/ML names are suffixed by extra markers like |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
131 |
this: @{ML_text foo_barT}. |
39881 | 132 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
133 |
Name variants are produced by adding 1--3 primes, e.g.\ @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
134 |
foo'}, @{ML_text foo''}, or @{ML_text foo'''}, but not @{ML_text |
39881 | 135 |
foo''''} or more. Decimal digits scale better to larger numbers, |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
136 |
e.g.\ @{ML_text foo0}, @{ML_text foo1}, @{ML_text foo42}. |
39880 | 137 |
|
138 |
\paragraph{Scopes.} Apart from very basic library modules, ML |
|
139 |
structures are not ``opened'', but names are referenced with |
|
39881 | 140 |
explicit qualification, as in @{ML Syntax.string_of_term} for |
39880 | 141 |
example. When devising names for structures and their components it |
142 |
is important aim at eye-catching compositions of both parts, because |
|
40126 | 143 |
this is how they are seen in the sources and documentation. For the |
144 |
same reasons, aliases of well-known library functions should be |
|
145 |
avoided. |
|
39880 | 146 |
|
40126 | 147 |
Local names of function abstraction or case/let bindings are |
39880 | 148 |
typically shorter, sometimes using only rudiments of ``words'', |
149 |
while still avoiding cryptic shorthands. An auxiliary function |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
150 |
called @{ML_text helper}, @{ML_text aux}, or @{ML_text f} is |
39880 | 151 |
considered bad style. |
152 |
||
153 |
Example: |
|
154 |
||
155 |
\begin{verbatim} |
|
156 |
(* RIGHT *) |
|
157 |
||
158 |
fun print_foo ctxt foo = |
|
159 |
let |
|
39881 | 160 |
fun print t = ... Syntax.string_of_term ctxt t ... |
161 |
in ... end; |
|
162 |
||
163 |
||
164 |
(* RIGHT *) |
|
165 |
||
166 |
fun print_foo ctxt foo = |
|
167 |
let |
|
39880 | 168 |
val string_of_term = Syntax.string_of_term ctxt; |
169 |
fun print t = ... string_of_term t ... |
|
170 |
in ... end; |
|
171 |
||
172 |
||
173 |
(* WRONG *) |
|
174 |
||
175 |
val string_of_term = Syntax.string_of_term; |
|
176 |
||
177 |
fun print_foo ctxt foo = |
|
178 |
let |
|
179 |
fun aux t = ... string_of_term ctxt t ... |
|
180 |
in ... end; |
|
181 |
||
182 |
\end{verbatim} |
|
183 |
||
184 |
||
40126 | 185 |
\paragraph{Specific conventions.} Here are some specific name forms |
186 |
that occur frequently in the sources. |
|
39881 | 187 |
|
188 |
\begin{itemize} |
|
189 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
190 |
\item A function that maps @{ML_text foo} to @{ML_text bar} is |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
191 |
called @{ML_text foo_to_bar} or @{ML_text bar_of_foo} (never |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
192 |
@{ML_text foo2bar}, @{ML_text bar_from_foo}, @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
193 |
bar_for_foo}, or @{ML_text bar4foo}). |
39881 | 194 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
195 |
\item The name component @{ML_text legacy} means that the operation |
39881 | 196 |
is about to be discontinued soon. |
197 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
198 |
\item The name component @{ML_text old} means that this is historic |
39881 | 199 |
material that might disappear at some later stage. |
200 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
201 |
\item The name component @{ML_text global} means that this works |
39881 | 202 |
with the background theory instead of the regular local context |
203 |
(\secref{sec:context}), sometimes for historical reasons, sometimes |
|
204 |
due a genuine lack of locality of the concept involved, sometimes as |
|
205 |
a fall-back for the lack of a proper context in the application |
|
206 |
code. Whenever there is a non-global variant available, the |
|
207 |
application should be migrated to use it with a proper local |
|
208 |
context. |
|
209 |
||
210 |
\item Variables of the main context types of the Isabelle/Isar |
|
211 |
framework (\secref{sec:context} and \chref{ch:local-theory}) have |
|
40126 | 212 |
firm naming conventions as follows: |
39881 | 213 |
|
214 |
\begin{itemize} |
|
215 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
216 |
\item theories are called @{ML_text thy}, rarely @{ML_text theory} |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
217 |
(never @{ML_text thry}) |
39881 | 218 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
219 |
\item proof contexts are called @{ML_text ctxt}, rarely @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
220 |
context} (never @{ML_text ctx}) |
39881 | 221 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
222 |
\item generic contexts are called @{ML_text context}, rarely |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
223 |
@{ML_text ctxt} |
39881 | 224 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
225 |
\item local theories are called @{ML_text lthy}, except for local |
40126 | 226 |
theories that are treated as proof context (which is a semantic |
227 |
super-type) |
|
39881 | 228 |
|
229 |
\end{itemize} |
|
230 |
||
231 |
Variations with primed or decimal numbers are always possible, as |
|
56579 | 232 |
well as semantic prefixes like @{ML_text foo_thy} or @{ML_text |
39881 | 233 |
bar_ctxt}, but the base conventions above need to be preserved. |
40126 | 234 |
This allows to visualize the their data flow via plain regular |
235 |
expressions in the editor. |
|
39881 | 236 |
|
40126 | 237 |
\item The main logical entities (\secref{ch:logic}) have established |
238 |
naming convention as follows: |
|
39881 | 239 |
|
240 |
\begin{itemize} |
|
241 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
242 |
\item sorts are called @{ML_text S} |
39881 | 243 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
244 |
\item types are called @{ML_text T}, @{ML_text U}, or @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
245 |
ty} (never @{ML_text t}) |
39881 | 246 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
247 |
\item terms are called @{ML_text t}, @{ML_text u}, or @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
248 |
tm} (never @{ML_text trm}) |
39881 | 249 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
250 |
\item certified types are called @{ML_text cT}, rarely @{ML_text |
39881 | 251 |
T}, with variants as for types |
252 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
253 |
\item certified terms are called @{ML_text ct}, rarely @{ML_text |
52733 | 254 |
t}, with variants as for terms (never @{ML_text ctrm}) |
39881 | 255 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
256 |
\item theorems are called @{ML_text th}, or @{ML_text thm} |
39881 | 257 |
|
258 |
\end{itemize} |
|
259 |
||
260 |
Proper semantic names override these conventions completely. For |
|
261 |
example, the left-hand side of an equation (as a term) can be called |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
262 |
@{ML_text lhs} (not @{ML_text lhs_tm}). Or a term that is known |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
263 |
to be a variable can be called @{ML_text v} or @{ML_text x}. |
39881 | 264 |
|
40310 | 265 |
\item Tactics (\secref{sec:tactics}) are sufficiently important to |
266 |
have specific naming conventions. The name of a basic tactic |
|
267 |
definition always has a @{ML_text "_tac"} suffix, the subgoal index |
|
268 |
(if applicable) is always called @{ML_text i}, and the goal state |
|
269 |
(if made explicit) is usually called @{ML_text st} instead of the |
|
270 |
somewhat misleading @{ML_text thm}. Any other arguments are given |
|
271 |
before the latter two, and the general context is given first. |
|
272 |
Example: |
|
273 |
||
274 |
\begin{verbatim} |
|
275 |
fun my_tac ctxt arg1 arg2 i st = ... |
|
276 |
\end{verbatim} |
|
277 |
||
278 |
Note that the goal state @{ML_text st} above is rarely made |
|
279 |
explicit, if tactic combinators (tacticals) are used as usual. |
|
280 |
||
39881 | 281 |
\end{itemize} |
39878 | 282 |
*} |
283 |
||
284 |
||
285 |
subsection {* General source layout *} |
|
286 |
||
287 |
text {* The general Isabelle/ML source layout imitates regular |
|
288 |
type-setting to some extent, augmented by the requirements for |
|
289 |
deeply nested expressions that are commonplace in functional |
|
290 |
programming. |
|
291 |
||
292 |
\paragraph{Line length} is 80 characters according to ancient |
|
40126 | 293 |
standards, but we allow as much as 100 characters (not |
294 |
more).\footnote{Readability requires to keep the beginning of a line |
|
39881 | 295 |
in view while watching its end. Modern wide-screen displays do not |
40126 | 296 |
change the way how the human brain works. Sources also need to be |
297 |
printable on plain paper with reasonable font-size.} The extra 20 |
|
39880 | 298 |
characters acknowledge the space requirements due to qualified |
299 |
library references in Isabelle/ML. |
|
39878 | 300 |
|
301 |
\paragraph{White-space} is used to emphasize the structure of |
|
302 |
expressions, following mostly standard conventions for mathematical |
|
303 |
typesetting, as can be seen in plain {\TeX} or {\LaTeX}. This |
|
39879 | 304 |
defines positioning of spaces for parentheses, punctuation, and |
305 |
infixes as illustrated here: |
|
39878 | 306 |
|
307 |
\begin{verbatim} |
|
308 |
val x = y + z * (a + b); |
|
309 |
val pair = (a, b); |
|
310 |
val record = {foo = 1, bar = 2}; |
|
311 |
\end{verbatim} |
|
312 |
||
39879 | 313 |
Lines are normally broken \emph{after} an infix operator or |
314 |
punctuation character. For example: |
|
39878 | 315 |
|
316 |
\begin{verbatim} |
|
317 |
val x = |
|
318 |
a + |
|
319 |
b + |
|
320 |
c; |
|
321 |
||
322 |
val tuple = |
|
323 |
(a, |
|
324 |
b, |
|
325 |
c); |
|
326 |
\end{verbatim} |
|
327 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
328 |
Some special infixes (e.g.\ @{ML_text "|>"}) work better at the |
39879 | 329 |
start of the line, but punctuation is always at the end. |
39878 | 330 |
|
331 |
Function application follows the tradition of @{text "\<lambda>"}-calculus, |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
332 |
not informal mathematics. For example: @{ML_text "f a b"} for a |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
333 |
curried function, or @{ML_text "g (a, b)"} for a tupled function. |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
334 |
Note that the space between @{ML_text g} and the pair @{ML_text |
39879 | 335 |
"(a, b)"} follows the important principle of |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
336 |
\emph{compositionality}: the layout of @{ML_text "g p"} does not |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
337 |
change when @{ML_text "p"} is refined to the concrete pair |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
338 |
@{ML_text "(a, b)"}. |
39878 | 339 |
|
340 |
\paragraph{Indentation} uses plain spaces, never hard |
|
341 |
tabulators.\footnote{Tabulators were invented to move the carriage |
|
342 |
of a type-writer to certain predefined positions. In software they |
|
343 |
could be used as a primitive run-length compression of consecutive |
|
344 |
spaces, but the precise result would depend on non-standardized |
|
345 |
editor configuration.} |
|
346 |
||
39879 | 347 |
Each level of nesting is indented by 2 spaces, sometimes 1, very |
40126 | 348 |
rarely 4, never 8 or any other odd number. |
39878 | 349 |
|
39879 | 350 |
Indentation follows a simple logical format that only depends on the |
351 |
nesting depth, not the accidental length of the text that initiates |
|
352 |
a level of nesting. Example: |
|
39878 | 353 |
|
354 |
\begin{verbatim} |
|
39880 | 355 |
(* RIGHT *) |
356 |
||
39878 | 357 |
if b then |
39879 | 358 |
expr1_part1 |
359 |
expr1_part2 |
|
39878 | 360 |
else |
39879 | 361 |
expr2_part1 |
362 |
expr2_part2 |
|
39878 | 363 |
|
39880 | 364 |
|
365 |
(* WRONG *) |
|
366 |
||
39879 | 367 |
if b then expr1_part1 |
368 |
expr1_part2 |
|
369 |
else expr2_part1 |
|
370 |
expr2_part2 |
|
39878 | 371 |
\end{verbatim} |
372 |
||
373 |
The second form has many problems: it assumes a fixed-width font |
|
39879 | 374 |
when viewing the sources, it uses more space on the line and thus |
375 |
makes it hard to observe its strict length limit (working against |
|
39878 | 376 |
\emph{readability}), it requires extra editing to adapt the layout |
39879 | 377 |
to changes of the initial text (working against |
39878 | 378 |
\emph{maintainability}) etc. |
379 |
||
39879 | 380 |
\medskip For similar reasons, any kind of two-dimensional or tabular |
40126 | 381 |
layouts, ASCII-art with lines or boxes of asterisks etc.\ should be |
39879 | 382 |
avoided. |
39881 | 383 |
|
40126 | 384 |
\paragraph{Complex expressions} that consist of multi-clausal |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
385 |
function definitions, @{ML_text handle}, @{ML_text case}, |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
386 |
@{ML_text let} (and combinations) require special attention. The |
40126 | 387 |
syntax of Standard ML is quite ambitious and admits a lot of |
388 |
variance that can distort the meaning of the text. |
|
39881 | 389 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
390 |
Clauses of @{ML_text fun}, @{ML_text fn}, @{ML_text handle}, |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
391 |
@{ML_text case} get extra indentation to indicate the nesting |
40126 | 392 |
clearly. Example: |
39881 | 393 |
|
394 |
\begin{verbatim} |
|
395 |
(* RIGHT *) |
|
396 |
||
397 |
fun foo p1 = |
|
398 |
expr1 |
|
399 |
| foo p2 = |
|
400 |
expr2 |
|
401 |
||
402 |
||
403 |
(* WRONG *) |
|
404 |
||
405 |
fun foo p1 = |
|
406 |
expr1 |
|
407 |
| foo p2 = |
|
408 |
expr2 |
|
409 |
\end{verbatim} |
|
410 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
411 |
Body expressions consisting of @{ML_text case} or @{ML_text let} |
39881 | 412 |
require care to maintain compositionality, to prevent loss of |
40126 | 413 |
logical indentation where it is especially important to see the |
414 |
structure of the text. Example: |
|
39881 | 415 |
|
416 |
\begin{verbatim} |
|
417 |
(* RIGHT *) |
|
418 |
||
419 |
fun foo p1 = |
|
420 |
(case e of |
|
421 |
q1 => ... |
|
422 |
| q2 => ...) |
|
423 |
| foo p2 = |
|
424 |
let |
|
425 |
... |
|
426 |
in |
|
427 |
... |
|
428 |
end |
|
429 |
||
430 |
||
431 |
(* WRONG *) |
|
432 |
||
433 |
fun foo p1 = case e of |
|
434 |
q1 => ... |
|
435 |
| q2 => ... |
|
436 |
| foo p2 = |
|
437 |
let |
|
438 |
... |
|
439 |
in |
|
440 |
... |
|
441 |
end |
|
442 |
\end{verbatim} |
|
443 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
444 |
Extra parentheses around @{ML_text case} expressions are optional, |
40126 | 445 |
but help to analyse the nesting based on character matching in the |
446 |
editor. |
|
39881 | 447 |
|
448 |
\medskip There are two main exceptions to the overall principle of |
|
449 |
compositionality in the layout of complex expressions. |
|
450 |
||
451 |
\begin{enumerate} |
|
452 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
453 |
\item @{ML_text "if"} expressions are iterated as if there would be |
39881 | 454 |
a multi-branch conditional in SML, e.g. |
455 |
||
456 |
\begin{verbatim} |
|
457 |
(* RIGHT *) |
|
458 |
||
459 |
if b1 then e1 |
|
460 |
else if b2 then e2 |
|
461 |
else e3 |
|
462 |
\end{verbatim} |
|
463 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
464 |
\item @{ML_text fn} abstractions are often layed-out as if they |
39881 | 465 |
would lack any structure by themselves. This traditional form is |
466 |
motivated by the possibility to shift function arguments back and |
|
40126 | 467 |
forth wrt.\ additional combinators. Example: |
39881 | 468 |
|
469 |
\begin{verbatim} |
|
470 |
(* RIGHT *) |
|
471 |
||
472 |
fun foo x y = fold (fn z => |
|
473 |
expr) |
|
474 |
\end{verbatim} |
|
475 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
476 |
Here the visual appearance is that of three arguments @{ML_text x}, |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
477 |
@{ML_text y}, @{ML_text z}. |
39881 | 478 |
|
479 |
\end{enumerate} |
|
480 |
||
481 |
Such weakly structured layout should be use with great care. Here |
|
40153 | 482 |
are some counter-examples involving @{ML_text let} expressions: |
39881 | 483 |
|
484 |
\begin{verbatim} |
|
485 |
(* WRONG *) |
|
486 |
||
487 |
fun foo x = let |
|
488 |
val y = ... |
|
489 |
in ... end |
|
490 |
||
41162 | 491 |
|
492 |
(* WRONG *) |
|
493 |
||
40153 | 494 |
fun foo x = let |
495 |
val y = ... |
|
496 |
in ... end |
|
497 |
||
41162 | 498 |
|
499 |
(* WRONG *) |
|
500 |
||
39881 | 501 |
fun foo x = |
502 |
let |
|
503 |
val y = ... |
|
504 |
in ... end |
|
505 |
\end{verbatim} |
|
506 |
||
507 |
\medskip In general the source layout is meant to emphasize the |
|
508 |
structure of complex language expressions, not to pretend that SML |
|
509 |
had a completely different syntax (say that of Haskell or Java). |
|
39878 | 510 |
*} |
511 |
||
512 |
||
39823 | 513 |
section {* SML embedded into Isabelle/Isar *} |
514 |
||
39824 | 515 |
text {* ML and Isar are intertwined via an open-ended bootstrap |
516 |
process that provides more and more programming facilities and |
|
517 |
logical content in an alternating manner. Bootstrapping starts from |
|
518 |
the raw environment of existing implementations of Standard ML |
|
519 |
(mainly Poly/ML, but also SML/NJ). |
|
39823 | 520 |
|
39824 | 521 |
Isabelle/Pure marks the point where the original ML toplevel is |
40126 | 522 |
superseded by the Isar toplevel that maintains a uniform context for |
523 |
arbitrary ML values (see also \secref{sec:context}). This formal |
|
524 |
environment holds ML compiler bindings, logical entities, and many |
|
525 |
other things. Raw SML is never encountered again after the initial |
|
526 |
bootstrap of Isabelle/Pure. |
|
39823 | 527 |
|
40126 | 528 |
Object-logics like Isabelle/HOL are built within the |
529 |
Isabelle/ML/Isar environment by introducing suitable theories with |
|
530 |
associated ML modules, either inlined or as separate files. Thus |
|
531 |
Isabelle/HOL is defined as a regular user-space application within |
|
532 |
the Isabelle framework. Further add-on tools can be implemented in |
|
533 |
ML within the Isar context in the same manner: ML is part of the |
|
534 |
standard repertoire of Isabelle, and there is no distinction between |
|
535 |
``user'' and ``developer'' in this respect. |
|
39823 | 536 |
*} |
537 |
||
39824 | 538 |
|
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
539 |
subsection {* Isar ML commands *} |
39823 | 540 |
|
40126 | 541 |
text {* The primary Isar source language provides facilities to ``open |
542 |
a window'' to the underlying ML compiler. Especially see the Isar |
|
51295 | 543 |
commands @{command_ref "ML_file"} and @{command_ref "ML"}: both work the |
39824 | 544 |
same way, only the source text is provided via a file vs.\ inlined, |
545 |
respectively. Apart from embedding ML into the main theory |
|
546 |
definition like that, there are many more commands that refer to ML |
|
547 |
source, such as @{command_ref setup} or @{command_ref declaration}. |
|
40126 | 548 |
Even more fine-grained embedding of ML into Isar is encountered in |
549 |
the proof method @{method_ref tactic}, which refines the pending |
|
550 |
goal state via a given expression of type @{ML_type tactic}. |
|
39824 | 551 |
*} |
39823 | 552 |
|
39824 | 553 |
text %mlex {* The following artificial example demonstrates some ML |
554 |
toplevel declarations within the implicit Isar theory context. This |
|
555 |
is regular functional programming without referring to logical |
|
556 |
entities yet. |
|
39823 | 557 |
*} |
558 |
||
559 |
ML {* |
|
560 |
fun factorial 0 = 1 |
|
561 |
| factorial n = n * factorial (n - 1) |
|
562 |
*} |
|
563 |
||
40126 | 564 |
text {* Here the ML environment is already managed by Isabelle, i.e.\ |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
565 |
the @{ML factorial} function is not yet accessible in the preceding |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
566 |
paragraph, nor in a different theory that is independent from the |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
567 |
current one in the import hierarchy. |
39823 | 568 |
|
569 |
Removing the above ML declaration from the source text will remove |
|
570 |
any trace of this definition as expected. The Isabelle/ML toplevel |
|
571 |
environment is managed in a \emph{stateless} way: unlike the raw ML |
|
40126 | 572 |
toplevel there are no global side-effects involved |
573 |
here.\footnote{Such a stateless compilation environment is also a |
|
574 |
prerequisite for robust parallel compilation within independent |
|
575 |
nodes of the implicit theory development graph.} |
|
39823 | 576 |
|
40126 | 577 |
\medskip The next example shows how to embed ML into Isar proofs, using |
578 |
@{command_ref "ML_prf"} instead of Instead of @{command_ref "ML"}. |
|
579 |
As illustrated below, the effect on the ML environment is local to |
|
580 |
the whole proof body, ignoring the block structure. |
|
581 |
*} |
|
39823 | 582 |
|
40964 | 583 |
notepad |
584 |
begin |
|
39851 | 585 |
ML_prf %"ML" {* val a = 1 *} |
40126 | 586 |
{ |
39851 | 587 |
ML_prf %"ML" {* val b = a + 1 *} |
39824 | 588 |
} -- {* Isar block structure ignored by ML environment *} |
39851 | 589 |
ML_prf %"ML" {* val c = b + 1 *} |
40964 | 590 |
end |
39823 | 591 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
592 |
text {* By side-stepping the normal scoping rules for Isar proof |
40126 | 593 |
blocks, embedded ML code can refer to the different contexts and |
594 |
manipulate corresponding entities, e.g.\ export a fact from a block |
|
595 |
context. |
|
39823 | 596 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
597 |
\medskip Two further ML commands are useful in certain situations: |
40126 | 598 |
@{command_ref ML_val} and @{command_ref ML_command} are |
39824 | 599 |
\emph{diagnostic} in the sense that there is no effect on the |
600 |
underlying environment, and can thus used anywhere (even outside a |
|
601 |
theory). The examples below produce long strings of digits by |
|
602 |
invoking @{ML factorial}: @{command ML_val} already takes care of |
|
603 |
printing the ML toplevel result, but @{command ML_command} is silent |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
604 |
so we produce an explicit output message. *} |
39823 | 605 |
|
606 |
ML_val {* factorial 100 *} |
|
607 |
ML_command {* writeln (string_of_int (factorial 100)) *} |
|
608 |
||
40964 | 609 |
notepad |
610 |
begin |
|
52417 | 611 |
ML_val {* factorial 100 *} |
39823 | 612 |
ML_command {* writeln (string_of_int (factorial 100)) *} |
40964 | 613 |
end |
39823 | 614 |
|
615 |
||
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
616 |
subsection {* Compile-time context *} |
39823 | 617 |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
618 |
text {* Whenever the ML compiler is invoked within Isabelle/Isar, the |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
619 |
formal context is passed as a thread-local reference variable. Thus |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
620 |
ML code may access the theory context during compilation, by reading |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
621 |
or writing the (local) theory under construction. Note that such |
40126 | 622 |
direct access to the compile-time context is rare. In practice it |
623 |
is typically done via some derived ML functions instead. |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
624 |
*} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
625 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
626 |
text %mlref {* |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
627 |
\begin{mldecls} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
628 |
@{index_ML ML_Context.the_generic_context: "unit -> Context.generic"} \\ |
40126 | 629 |
@{index_ML "Context.>>": "(Context.generic -> Context.generic) -> unit"} \\ |
56199 | 630 |
@{index_ML ML_Thms.bind_thms: "string * thm list -> unit"} \\ |
631 |
@{index_ML ML_Thms.bind_thm: "string * thm -> unit"} \\ |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
632 |
\end{mldecls} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
633 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
634 |
\begin{description} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
635 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
636 |
\item @{ML "ML_Context.the_generic_context ()"} refers to the theory |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
637 |
context of the ML toplevel --- at compile time. ML code needs to |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
638 |
take care to refer to @{ML "ML_Context.the_generic_context ()"} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
639 |
correctly. Recall that evaluation of a function body is delayed |
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
640 |
until actual run-time. |
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
641 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
642 |
\item @{ML "Context.>>"}~@{text f} applies context transformation |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
643 |
@{text f} to the implicit context of the ML toplevel. |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
644 |
|
56199 | 645 |
\item @{ML ML_Thms.bind_thms}~@{text "(name, thms)"} stores a list of |
39850 | 646 |
theorems produced in ML both in the (global) theory context and the |
647 |
ML toplevel, associating it with the provided name. Theorems are |
|
648 |
put into a global ``standard'' format before being stored. |
|
649 |
||
56199 | 650 |
\item @{ML ML_Thms.bind_thm} is similar to @{ML ML_Thms.bind_thms} but refers to a |
40126 | 651 |
singleton fact. |
39850 | 652 |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
653 |
\end{description} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
654 |
|
40126 | 655 |
It is important to note that the above functions are really |
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
656 |
restricted to the compile time, even though the ML compiler is |
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
657 |
invoked at run-time. The majority of ML code either uses static |
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
658 |
antiquotations (\secref{sec:ML-antiq}) or refers to the theory or |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
659 |
proof context at run-time, by explicit functional abstraction. |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
660 |
*} |
39823 | 661 |
|
662 |
||
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
663 |
subsection {* Antiquotations \label{sec:ML-antiq} *} |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
664 |
|
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
665 |
text {* A very important consequence of embedding SML into Isar is the |
40126 | 666 |
concept of \emph{ML antiquotation}. The standard token language of |
667 |
ML is augmented by special syntactic entities of the following form: |
|
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
668 |
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
669 |
@{rail \<open> |
53167 | 670 |
@{syntax_def antiquote}: '@{' nameref args '}' |
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
671 |
\<close>} |
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
672 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
673 |
Here @{syntax nameref} and @{syntax args} are regular outer syntax |
40126 | 674 |
categories \cite{isabelle-isar-ref}. Attributes and proof methods |
675 |
use similar syntax. |
|
39823 | 676 |
|
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
677 |
\medskip A regular antiquotation @{text "@{name args}"} processes |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
678 |
its arguments by the usual means of the Isar source language, and |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
679 |
produces corresponding ML source text, either as literal |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
680 |
\emph{inline} text (e.g. @{text "@{term t}"}) or abstract |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
681 |
\emph{value} (e.g. @{text "@{thm th}"}). This pre-compilation |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
682 |
scheme allows to refer to formal entities in a robust manner, with |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
683 |
proper static scoping and with some degree of logical checking of |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
684 |
small portions of the code. |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
685 |
*} |
39823 | 686 |
|
39835 | 687 |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
688 |
subsection {* Printing ML values *} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
689 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
690 |
text {* The ML compiler knows about the structure of values according |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
691 |
to their static type, and can print them in the manner of the |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
692 |
toplevel loop, although the details are non-portable. The |
56399 | 693 |
antiquotations @{ML_antiquotation_def "make_string"} and |
694 |
@{ML_antiquotation_def "print"} provide a quasi-portable way to |
|
695 |
refer to this potential capability of the underlying ML system in |
|
696 |
generic Isabelle/ML sources. |
|
697 |
||
698 |
This is occasionally useful for diagnostic or demonstration |
|
699 |
purposes. Note that production-quality tools require proper |
|
700 |
user-level error messages. *} |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
701 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
702 |
text %mlantiq {* |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
703 |
\begin{matharray}{rcl} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
704 |
@{ML_antiquotation_def "make_string"} & : & @{text ML_antiquotation} \\ |
56399 | 705 |
@{ML_antiquotation_def "print"} & : & @{text ML_antiquotation} \\ |
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
706 |
\end{matharray} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
707 |
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
708 |
@{rail \<open> |
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
709 |
@@{ML_antiquotation make_string} |
56399 | 710 |
; |
711 |
@@{ML_antiquotation print} @{syntax name}? |
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
712 |
\<close>} |
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
713 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
714 |
\begin{description} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
715 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
716 |
\item @{text "@{make_string}"} inlines a function to print arbitrary |
56399 | 717 |
values similar to the ML toplevel. The result is compiler dependent |
718 |
and may fall back on "?" in certain situations. |
|
719 |
||
720 |
\item @{text "@{print f}"} uses the ML function @{text "f: string -> |
|
721 |
unit"} to output the result of @{text "@{make_string}"} above, |
|
722 |
together with the source position of the antiquotation. The default |
|
723 |
output function is @{ML writeln}. |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
724 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
725 |
\end{description} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
726 |
*} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
727 |
|
56399 | 728 |
text %mlex {* The following artificial examples show how to produce |
729 |
adhoc output of ML values for debugging purposes. *} |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
730 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
731 |
ML {* |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
732 |
val x = 42; |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
733 |
val y = true; |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
734 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
735 |
writeln (@{make_string} {x = x, y = y}); |
56399 | 736 |
|
737 |
@{print} {x = x, y = y}; |
|
738 |
@{print tracing} {x = x, y = y}; |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
739 |
*} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
740 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
741 |
|
39883 | 742 |
section {* Canonical argument order \label{sec:canonical-argument-order} *} |
743 |
||
744 |
text {* Standard ML is a language in the tradition of @{text |
|
745 |
"\<lambda>"}-calculus and \emph{higher-order functional programming}, |
|
746 |
similar to OCaml, Haskell, or Isabelle/Pure and HOL as logical |
|
747 |
languages. Getting acquainted with the native style of representing |
|
748 |
functions in that setting can save a lot of extra boiler-plate of |
|
749 |
redundant shuffling of arguments, auxiliary abstractions etc. |
|
750 |
||
40126 | 751 |
Functions are usually \emph{curried}: the idea of turning arguments |
752 |
of type @{text "\<tau>\<^sub>i"} (for @{text "i \<in> {1, \<dots> n}"}) into a result of |
|
753 |
type @{text "\<tau>"} is represented by the iterated function space |
|
754 |
@{text "\<tau>\<^sub>1 \<rightarrow> \<dots> \<rightarrow> \<tau>\<^sub>n \<rightarrow> \<tau>"}. This is isomorphic to the well-known |
|
755 |
encoding via tuples @{text "\<tau>\<^sub>1 \<times> \<dots> \<times> \<tau>\<^sub>n \<rightarrow> \<tau>"}, but the curried |
|
756 |
version fits more smoothly into the basic calculus.\footnote{The |
|
757 |
difference is even more significant in higher-order logic, because |
|
758 |
the redundant tuple structure needs to be accommodated by formal |
|
759 |
reasoning.} |
|
39883 | 760 |
|
56594 | 761 |
Currying gives some flexibility due to \emph{partial application}. A |
53071 | 762 |
function @{text "f: \<tau>\<^sub>1 \<rightarrow> \<tau>\<^sub>2 \<rightarrow> \<tau>"} can be applied to @{text "x: \<tau>\<^sub>1"} |
40126 | 763 |
and the remaining @{text "(f x): \<tau>\<^sub>2 \<rightarrow> \<tau>"} passed to another function |
39883 | 764 |
etc. How well this works in practice depends on the order of |
765 |
arguments. In the worst case, arguments are arranged erratically, |
|
766 |
and using a function in a certain situation always requires some |
|
56579 | 767 |
glue code. Thus we would get exponentially many opportunities to |
39883 | 768 |
decorate the code with meaningless permutations of arguments. |
769 |
||
770 |
This can be avoided by \emph{canonical argument order}, which |
|
40126 | 771 |
observes certain standard patterns and minimizes adhoc permutations |
40229 | 772 |
in their application. In Isabelle/ML, large portions of text can be |
52416 | 773 |
written without auxiliary operations like @{text "swap: \<alpha> \<times> \<beta> \<rightarrow> \<beta> \<times> |
774 |
\<alpha>"} or @{text "C: (\<alpha> \<rightarrow> \<beta> \<rightarrow> \<gamma>) \<rightarrow> (\<beta> \<rightarrow> \<alpha> \<rightarrow> \<gamma>)"} (the latter not |
|
775 |
present in the Isabelle/ML library). |
|
39883 | 776 |
|
777 |
\medskip The basic idea is that arguments that vary less are moved |
|
778 |
further to the left than those that vary more. Two particularly |
|
779 |
important categories of functions are \emph{selectors} and |
|
780 |
\emph{updates}. |
|
781 |
||
782 |
The subsequent scheme is based on a hypothetical set-like container |
|
783 |
of type @{text "\<beta>"} that manages elements of type @{text "\<alpha>"}. Both |
|
784 |
the names and types of the associated operations are canonical for |
|
785 |
Isabelle/ML. |
|
786 |
||
52416 | 787 |
\begin{center} |
39883 | 788 |
\begin{tabular}{ll} |
789 |
kind & canonical name and type \\\hline |
|
790 |
selector & @{text "member: \<beta> \<rightarrow> \<alpha> \<rightarrow> bool"} \\ |
|
791 |
update & @{text "insert: \<alpha> \<rightarrow> \<beta> \<rightarrow> \<beta>"} \\ |
|
792 |
\end{tabular} |
|
52416 | 793 |
\end{center} |
39883 | 794 |
|
795 |
Given a container @{text "B: \<beta>"}, the partially applied @{text |
|
796 |
"member B"} is a predicate over elements @{text "\<alpha> \<rightarrow> bool"}, and |
|
797 |
thus represents the intended denotation directly. It is customary |
|
798 |
to pass the abstract predicate to further operations, not the |
|
799 |
concrete container. The argument order makes it easy to use other |
|
800 |
combinators: @{text "forall (member B) list"} will check a list of |
|
801 |
elements for membership in @{text "B"} etc. Often the explicit |
|
40126 | 802 |
@{text "list"} is pointless and can be contracted to @{text "forall |
803 |
(member B)"} to get directly a predicate again. |
|
39883 | 804 |
|
40126 | 805 |
In contrast, an update operation varies the container, so it moves |
39883 | 806 |
to the right: @{text "insert a"} is a function @{text "\<beta> \<rightarrow> \<beta>"} to |
807 |
insert a value @{text "a"}. These can be composed naturally as |
|
40126 | 808 |
@{text "insert c \<circ> insert b \<circ> insert a"}. The slightly awkward |
40229 | 809 |
inversion of the composition order is due to conventional |
40126 | 810 |
mathematical notation, which can be easily amended as explained |
811 |
below. |
|
39883 | 812 |
*} |
813 |
||
814 |
||
815 |
subsection {* Forward application and composition *} |
|
816 |
||
817 |
text {* Regular function application and infix notation works best for |
|
818 |
relatively deeply structured expressions, e.g.\ @{text "h (f x y + g |
|
40126 | 819 |
z)"}. The important special case of \emph{linear transformation} |
820 |
applies a cascade of functions @{text "f\<^sub>n (\<dots> (f\<^sub>1 x))"}. This |
|
821 |
becomes hard to read and maintain if the functions are themselves |
|
822 |
given as complex expressions. The notation can be significantly |
|
39883 | 823 |
improved by introducing \emph{forward} versions of application and |
824 |
composition as follows: |
|
825 |
||
826 |
\medskip |
|
827 |
\begin{tabular}{lll} |
|
828 |
@{text "x |> f"} & @{text "\<equiv>"} & @{text "f x"} \\ |
|
41162 | 829 |
@{text "(f #> g) x"} & @{text "\<equiv>"} & @{text "x |> f |> g"} \\ |
39883 | 830 |
\end{tabular} |
831 |
\medskip |
|
832 |
||
833 |
This enables to write conveniently @{text "x |> f\<^sub>1 |> \<dots> |> f\<^sub>n"} or |
|
834 |
@{text "f\<^sub>1 #> \<dots> #> f\<^sub>n"} for its functional abstraction over @{text |
|
835 |
"x"}. |
|
836 |
||
837 |
\medskip There is an additional set of combinators to accommodate |
|
838 |
multiple results (via pairs) that are passed on as multiple |
|
839 |
arguments (via currying). |
|
840 |
||
841 |
\medskip |
|
842 |
\begin{tabular}{lll} |
|
843 |
@{text "(x, y) |-> f"} & @{text "\<equiv>"} & @{text "f x y"} \\ |
|
41162 | 844 |
@{text "(f #-> g) x"} & @{text "\<equiv>"} & @{text "x |> f |-> g"} \\ |
39883 | 845 |
\end{tabular} |
846 |
\medskip |
|
847 |
*} |
|
848 |
||
849 |
text %mlref {* |
|
850 |
\begin{mldecls} |
|
46262 | 851 |
@{index_ML_op "|> ": "'a * ('a -> 'b) -> 'b"} \\ |
852 |
@{index_ML_op "|-> ": "('c * 'a) * ('c -> 'a -> 'b) -> 'b"} \\ |
|
853 |
@{index_ML_op "#> ": "('a -> 'b) * ('b -> 'c) -> 'a -> 'c"} \\ |
|
854 |
@{index_ML_op "#-> ": "('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd"} \\ |
|
39883 | 855 |
\end{mldecls} |
856 |
*} |
|
857 |
||
858 |
||
859 |
subsection {* Canonical iteration *} |
|
860 |
||
861 |
text {* As explained above, a function @{text "f: \<alpha> \<rightarrow> \<beta> \<rightarrow> \<beta>"} can be |
|
40126 | 862 |
understood as update on a configuration of type @{text "\<beta>"}, |
39883 | 863 |
parametrized by arguments of type @{text "\<alpha>"}. Given @{text "a: \<alpha>"} |
864 |
the partial application @{text "(f a): \<beta> \<rightarrow> \<beta>"} operates |
|
865 |
homogeneously on @{text "\<beta>"}. This can be iterated naturally over a |
|
53071 | 866 |
list of parameters @{text "[a\<^sub>1, \<dots>, a\<^sub>n]"} as @{text "f a\<^sub>1 #> \<dots> #> f a\<^sub>n"}. |
867 |
The latter expression is again a function @{text "\<beta> \<rightarrow> \<beta>"}. |
|
39883 | 868 |
It can be applied to an initial configuration @{text "b: \<beta>"} to |
869 |
start the iteration over the given list of arguments: each @{text |
|
870 |
"a"} in @{text "a\<^sub>1, \<dots>, a\<^sub>n"} is applied consecutively by updating a |
|
871 |
cumulative configuration. |
|
872 |
||
873 |
The @{text fold} combinator in Isabelle/ML lifts a function @{text |
|
874 |
"f"} as above to its iterated version over a list of arguments. |
|
875 |
Lifting can be repeated, e.g.\ @{text "(fold \<circ> fold) f"} iterates |
|
876 |
over a list of lists as expected. |
|
877 |
||
878 |
The variant @{text "fold_rev"} works inside-out over the list of |
|
879 |
arguments, such that @{text "fold_rev f \<equiv> fold f \<circ> rev"} holds. |
|
880 |
||
881 |
The @{text "fold_map"} combinator essentially performs @{text |
|
882 |
"fold"} and @{text "map"} simultaneously: each application of @{text |
|
883 |
"f"} produces an updated configuration together with a side-result; |
|
884 |
the iteration collects all such side-results as a separate list. |
|
885 |
*} |
|
886 |
||
887 |
text %mlref {* |
|
888 |
\begin{mldecls} |
|
889 |
@{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\ |
|
890 |
@{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\ |
|
891 |
@{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\ |
|
892 |
\end{mldecls} |
|
893 |
||
894 |
\begin{description} |
|
895 |
||
896 |
\item @{ML fold}~@{text f} lifts the parametrized update function |
|
897 |
@{text "f"} to a list of parameters. |
|
898 |
||
899 |
\item @{ML fold_rev}~@{text "f"} is similar to @{ML fold}~@{text |
|
900 |
"f"}, but works inside-out. |
|
901 |
||
902 |
\item @{ML fold_map}~@{text "f"} lifts the parametrized update |
|
903 |
function @{text "f"} (with side-result) to a list of parameters and |
|
904 |
cumulative side-results. |
|
905 |
||
906 |
\end{description} |
|
907 |
||
908 |
\begin{warn} |
|
909 |
The literature on functional programming provides a multitude of |
|
910 |
combinators called @{text "foldl"}, @{text "foldr"} etc. SML97 |
|
911 |
provides its own variations as @{ML List.foldl} and @{ML |
|
40126 | 912 |
List.foldr}, while the classic Isabelle library also has the |
913 |
historic @{ML Library.foldl} and @{ML Library.foldr}. To avoid |
|
52416 | 914 |
unnecessary complication and confusion, all these historical |
915 |
versions should be ignored, and @{ML fold} (or @{ML fold_rev}) used |
|
916 |
exclusively. |
|
39883 | 917 |
\end{warn} |
918 |
*} |
|
919 |
||
920 |
text %mlex {* The following example shows how to fill a text buffer |
|
921 |
incrementally by adding strings, either individually or from a given |
|
922 |
list. |
|
923 |
*} |
|
924 |
||
925 |
ML {* |
|
926 |
val s = |
|
927 |
Buffer.empty |
|
928 |
|> Buffer.add "digits: " |
|
929 |
|> fold (Buffer.add o string_of_int) (0 upto 9) |
|
930 |
|> Buffer.content; |
|
931 |
||
932 |
@{assert} (s = "digits: 0123456789"); |
|
933 |
*} |
|
934 |
||
935 |
text {* Note how @{ML "fold (Buffer.add o string_of_int)"} above saves |
|
936 |
an extra @{ML "map"} over the given list. This kind of peephole |
|
937 |
optimization reduces both the code size and the tree structures in |
|
52416 | 938 |
memory (``deforestation''), but it requires some practice to read |
939 |
and write fluently. |
|
39883 | 940 |
|
40126 | 941 |
\medskip The next example elaborates the idea of canonical |
942 |
iteration, demonstrating fast accumulation of tree content using a |
|
943 |
text buffer. |
|
39883 | 944 |
*} |
945 |
||
946 |
ML {* |
|
947 |
datatype tree = Text of string | Elem of string * tree list; |
|
948 |
||
949 |
fun slow_content (Text txt) = txt |
|
950 |
| slow_content (Elem (name, ts)) = |
|
951 |
"<" ^ name ^ ">" ^ |
|
952 |
implode (map slow_content ts) ^ |
|
953 |
"</" ^ name ^ ">" |
|
954 |
||
955 |
fun add_content (Text txt) = Buffer.add txt |
|
956 |
| add_content (Elem (name, ts)) = |
|
957 |
Buffer.add ("<" ^ name ^ ">") #> |
|
958 |
fold add_content ts #> |
|
959 |
Buffer.add ("</" ^ name ^ ">"); |
|
960 |
||
961 |
fun fast_content tree = |
|
962 |
Buffer.empty |> add_content tree |> Buffer.content; |
|
963 |
*} |
|
964 |
||
965 |
text {* The slow part of @{ML slow_content} is the @{ML implode} of |
|
966 |
the recursive results, because it copies previously produced strings |
|
40126 | 967 |
again. |
39883 | 968 |
|
969 |
The incremental @{ML add_content} avoids this by operating on a |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
970 |
buffer that is passed through in a linear fashion. Using @{ML_text |
40126 | 971 |
"#>"} and contraction over the actual buffer argument saves some |
972 |
additional boiler-plate. Of course, the two @{ML "Buffer.add"} |
|
973 |
invocations with concatenated strings could have been split into |
|
974 |
smaller parts, but this would have obfuscated the source without |
|
975 |
making a big difference in allocations. Here we have done some |
|
39883 | 976 |
peephole-optimization for the sake of readability. |
977 |
||
978 |
Another benefit of @{ML add_content} is its ``open'' form as a |
|
40126 | 979 |
function on buffers that can be continued in further linear |
980 |
transformations, folding etc. Thus it is more compositional than |
|
981 |
the naive @{ML slow_content}. As realistic example, compare the |
|
982 |
old-style @{ML "Term.maxidx_of_term: term -> int"} with the newer |
|
983 |
@{ML "Term.maxidx_term: term -> int -> int"} in Isabelle/Pure. |
|
39883 | 984 |
|
40126 | 985 |
Note that @{ML fast_content} above is only defined as example. In |
986 |
many practical situations, it is customary to provide the |
|
987 |
incremental @{ML add_content} only and leave the initialization and |
|
988 |
termination to the concrete application by the user. |
|
39883 | 989 |
*} |
990 |
||
991 |
||
39854 | 992 |
section {* Message output channels \label{sec:message-channels} *} |
39835 | 993 |
|
994 |
text {* Isabelle provides output channels for different kinds of |
|
995 |
messages: regular output, high-volume tracing information, warnings, |
|
996 |
and errors. |
|
997 |
||
998 |
Depending on the user interface involved, these messages may appear |
|
999 |
in different text styles or colours. The standard output for |
|
1000 |
terminal sessions prefixes each line of warnings by @{verbatim |
|
1001 |