author | Christian Urban <urbanc@in.tum.de> |
Mon, 02 Nov 2009 12:47:00 +0100 | |
changeset 370 | 2494b5b7a85d |
parent 369 | 74ba778b09c9 |
child 371 | e6f583366779 |
permissions | -rw-r--r-- |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1 |
theory FirstSteps |
25
e2f9f94b26d4
Antiquotation setup is now contained in theory Base.
berghofe
parents:
21
diff
changeset
|
2 |
imports Base |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
3 |
begin |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
5 |
(*<*) |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
6 |
setup{* |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
7 |
open_file_with_prelude |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
8 |
"FirstSteps_Code.thy" |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
9 |
["theory FirstSteps", "imports Main", "begin"] |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
10 |
*} |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
11 |
(*>*) |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
12 |
|
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
13 |
chapter {* First Steps *} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
14 |
|
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
15 |
text {* |
323 | 16 |
\begin{flushright} |
17 |
{\em |
|
18 |
``We will most likely never realize the full importance of painting the Tower,\\ |
|
19 |
that it is the essential element in the conservation of metal works and the\\ |
|
347 | 20 |
more meticulous the paint job, the longer the tower shall endure.''} \\[1ex] |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
21 |
Gustave Eiffel, In his book {\em The 300-Meter Tower}.\footnote{The Eiffel Tower has been |
323 | 22 |
re-painted 18 times since its initial construction, an average of once every |
350 | 23 |
seven years. It takes more than one year for a team of 25 painters to paint the tower |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
24 |
from top to bottom.} |
323 | 25 |
\end{flushright} |
26 |
||
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
27 |
\medskip |
311 | 28 |
Isabelle programming is done in ML. Just like lemmas and proofs, ML-code for |
29 |
Isabelle must be part of a theory. If you want to follow the code given in |
|
54
1783211b3494
tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
30 |
this chapter, we assume you are working inside the theory starting with |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
31 |
|
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
32 |
\begin{quote} |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
33 |
\begin{tabular}{@ {}l} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
34 |
\isacommand{theory} FirstSteps\\ |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
35 |
\isacommand{imports} Main\\ |
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
36 |
\isacommand{begin}\\ |
6 | 37 |
\ldots |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
38 |
\end{tabular} |
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
39 |
\end{quote} |
157
76cdc8f562fc
added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents:
156
diff
changeset
|
40 |
|
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
41 |
We also generally assume you are working with the logic HOL. The examples |
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
42 |
that will be given might need to be adapted if you work in a different logic. |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
43 |
*} |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
44 |
|
20 | 45 |
section {* Including ML-Code *} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
46 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
47 |
text {* |
311 | 48 |
The easiest and quickest way to include code in a theory is by using the |
49 |
\isacommand{ML}-command. For example: |
|
50 |
||
51 |
\begin{isabelle} |
|
52 |
\begin{graybox} |
|
53 |
\isacommand{ML}~@{text "\<verbopen>"}\isanewline |
|
54 |
\hspace{5mm}@{ML "3 + 4"}\isanewline |
|
55 |
@{text "\<verbclose>"}\isanewline |
|
56 |
@{text "> 7"}\smallskip |
|
57 |
\end{graybox} |
|
58 |
\end{isabelle} |
|
59 |
||
60 |
Like normal Isabelle scripts, \isacommand{ML}-commands can be evaluated by |
|
61 |
using the advance and undo buttons of your Isabelle environment. The code |
|
62 |
inside the \isacommand{ML}-command can also contain value and function |
|
63 |
bindings, for example |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
64 |
*} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
65 |
|
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
66 |
ML %gray {* |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
67 |
val r = Unsynchronized.ref 0 |
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
68 |
fun f n = n + 1 |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
69 |
*} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
70 |
|
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
71 |
text {* |
311 | 72 |
and even those can be undone when the proof script is retracted. As |
73 |
mentioned in the Introduction, we will drop the \isacommand{ML}~@{text |
|
74 |
"\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we show code. The lines |
|
75 |
prefixed with @{text [quotes] ">"} are not part of the code, rather they |
|
76 |
indicate what the response is when the code is evaluated. There are also |
|
77 |
the commands \isacommand{ML\_val} and \isacommand{ML\_prf} for including |
|
78 |
ML-code. The first evaluates the given code, but any effect on the theory, |
|
79 |
in which the code is embedded, is suppressed. The second needs to be used if |
|
80 |
ML-code is defined inside a proof. For example |
|
253
3cfd9a8a6de1
added comments about ML_prf and ML_val
Christian Urban <urbanc@in.tum.de>
parents:
252
diff
changeset
|
81 |
|
254 | 82 |
\begin{quote} |
83 |
\begin{isabelle} |
|
84 |
\isacommand{lemma}~@{text "test:"}\isanewline |
|
85 |
\isacommand{shows}~@{text [quotes] "True"}\isanewline |
|
86 |
\isacommand{ML\_prf}~@{text "\<verbopen>"}~@{ML "writeln \"Trivial!\""}~@{text "\<verbclose>"}\isanewline |
|
87 |
\isacommand{oops} |
|
88 |
\end{isabelle} |
|
89 |
\end{quote} |
|
253
3cfd9a8a6de1
added comments about ML_prf and ML_val
Christian Urban <urbanc@in.tum.de>
parents:
252
diff
changeset
|
90 |
|
311 | 91 |
However, both commands will only play minor roles in this tutorial (we will |
343 | 92 |
always arrange that the ML-code is defined outside of proofs). |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
93 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
94 |
Once a portion of code is relatively stable, you usually want to export it |
235
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
95 |
to a separate ML-file. Such files can then be included somewhere inside a |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
96 |
theory by using the command \isacommand{use}. For example |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
97 |
|
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
98 |
\begin{quote} |
235
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
99 |
\begin{tabular}{@ {}l} |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
100 |
\isacommand{theory} FirstSteps\\ |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
101 |
\isacommand{imports} Main\\ |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
102 |
\isacommand{uses}~@{text "(\"file_to_be_included.ML\")"} @{text "\<dots>"}\\ |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
103 |
\isacommand{begin}\\ |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
104 |
\ldots\\ |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
105 |
\isacommand{use}~@{text "\"file_to_be_included.ML\""}\\ |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
106 |
\ldots |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
107 |
\end{tabular} |
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
108 |
\end{quote} |
235
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
109 |
|
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
110 |
The \isacommand{uses}-command in the header of the theory is needed in order |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
111 |
to indicate the dependency of the theory on the ML-file. Alternatively, the |
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
112 |
file can be included by just writing in the header |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
113 |
|
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
114 |
\begin{quote} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
115 |
\begin{tabular}{@ {}l} |
54
1783211b3494
tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
116 |
\isacommand{theory} FirstSteps\\ |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
117 |
\isacommand{imports} Main\\ |
58 | 118 |
\isacommand{uses} @{text "\"file_to_be_included.ML\""} @{text "\<dots>"}\\ |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
119 |
\isacommand{begin}\\ |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
120 |
\ldots |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
121 |
\end{tabular} |
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
122 |
\end{quote} |
235
dc955603d813
explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents:
234
diff
changeset
|
123 |
|
311 | 124 |
Note that no parentheses are given this time. Note also that the included |
125 |
ML-file should not contain any \isacommand{use} itself. Otherwise Isabelle |
|
126 |
is unable to record all file dependencies, which is a nuisance if you have |
|
127 |
to track down errors. |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
128 |
*} |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
129 |
|
329 | 130 |
section {* Printing and Debugging\label{sec:printing} *} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
131 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
132 |
text {* |
311 | 133 |
During development you might find it necessary to inspect some data in your |
134 |
code. This can be done in a ``quick-and-dirty'' fashion using the function |
|
369 | 135 |
@{ML_ind writeln in Output}. For example |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
136 |
|
317
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
137 |
@{ML_response_eq [display,gray] "writeln \"any string\"" "\"any string\"" with "(op =)"} |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
138 |
|
311 | 139 |
will print out @{text [quotes] "any string"} inside the response buffer of |
140 |
Isabelle. This function expects a string as argument. If you develop under |
|
141 |
PolyML, then there is a convenient, though again ``quick-and-dirty'', method |
|
142 |
for converting values into strings, namely the function |
|
369 | 143 |
@{ML_ind makestring in PolyML}: |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
144 |
|
317
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
145 |
@{ML_response_eq [display,gray] "writeln (PolyML.makestring 1)" "\"1\"" with "(op =)"} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
146 |
|
311 | 147 |
However, @{ML makestring in PolyML} only works if the type of what |
148 |
is converted is monomorphic and not a function. |
|
149 |
||
150 |
The function @{ML "writeln"} should only be used for testing purposes, |
|
151 |
because any output this function generates will be overwritten as soon as an |
|
152 |
error is raised. For printing anything more serious and elaborate, the |
|
369 | 153 |
function @{ML_ind tracing in Output} is more appropriate. This function writes all |
311 | 154 |
output into a separate tracing buffer. For example: |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
155 |
|
317
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
156 |
@{ML_response_eq [display,gray] "tracing \"foo\"" "\"foo\"" with "(op =)"} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
157 |
|
311 | 158 |
It is also possible to redirect the ``channel'' where the string @{text |
159 |
"foo"} is printed to a separate file, e.g., to prevent ProofGeneral from |
|
160 |
choking on massive amounts of trace output. This redirection can be achieved |
|
161 |
with the code: |
|
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
162 |
*} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
163 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
164 |
ML{*val strip_specials = |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
165 |
let |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
166 |
fun strip ("\^A" :: _ :: cs) = strip cs |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
167 |
| strip (c :: cs) = c :: strip cs |
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
168 |
| strip [] = []; |
306
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
169 |
in |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
170 |
implode o strip o explode |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
171 |
end |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
172 |
|
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
173 |
fun redirect_tracing stream = |
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
174 |
Output.tracing_fn := (fn s => |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
175 |
(TextIO.output (stream, (strip_specials s)); |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
176 |
TextIO.output (stream, "\n"); |
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
177 |
TextIO.flushOut stream)) *} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
178 |
|
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
179 |
text {* |
307
f4fa6540e280
suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
180 |
Calling now |
f4fa6540e280
suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
181 |
|
f4fa6540e280
suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
182 |
@{ML [display,gray] "redirect_tracing (TextIO.openOut \"foo.bar\")"} |
f4fa6540e280
suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
183 |
|
58 | 184 |
will cause that all tracing information is printed into the file @{text "foo.bar"}. |
75 | 185 |
|
369 | 186 |
You can print out error messages with the function @{ML_ind error in Library}; for |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
187 |
example: |
75 | 188 |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
189 |
@{ML_response_fake [display,gray] |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
190 |
"if 0=1 then true else (error \"foo\")" |
122 | 191 |
"Exception- ERROR \"foo\" raised |
192 |
At command \"ML\"."} |
|
75 | 193 |
|
307
f4fa6540e280
suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
194 |
This function raises the exception @{text ERROR}, which will then |
304
14173c0e8688
polished comment for error function
Christian Urban <urbanc@in.tum.de>
parents:
301
diff
changeset
|
195 |
be displayed by the infrastructure. |
14173c0e8688
polished comment for error function
Christian Urban <urbanc@in.tum.de>
parents:
301
diff
changeset
|
196 |
|
14173c0e8688
polished comment for error function
Christian Urban <urbanc@in.tum.de>
parents:
301
diff
changeset
|
197 |
|
369 | 198 |
\footnote{\bf FIXME Mention how to work with @{ML_ind debug in Toplevel} and |
199 |
@{ML_ind profiling in Toplevel}.} |
|
192 | 200 |
*} |
201 |
||
322 | 202 |
(* FIXME |
207 | 203 |
ML {* reset Toplevel.debug *} |
204 |
||
205 |
ML {* fun dodgy_fun () = (raise TYPE ("",[],[]); 1) *} |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
206 |
|
207 | 207 |
ML {* fun innocent () = dodgy_fun () *} |
208 |
ML {* exception_trace (fn () => cterm_of @{theory} (Bound 0)) *} |
|
209 |
ML {* exception_trace (fn () => innocent ()) *} |
|
192 | 210 |
|
207 | 211 |
ML {* Toplevel.program (fn () => cterm_of @{theory} (Bound 0)) *} |
192 | 212 |
|
207 | 213 |
ML {* Toplevel.program (fn () => innocent ()) *} |
192 | 214 |
*) |
215 |
||
216 |
text {* |
|
347 | 217 |
Most often you want to inspect data of Isabelle's basic data |
311 | 218 |
structures, namely @{ML_type term}, @{ML_type cterm} and @{ML_type |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
219 |
thm}. Isabelle contains elaborate pretty-printing functions for printing |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
220 |
them (see Section \ref{sec:pretty}), but for quick-and-dirty solutions they |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
221 |
are a bit unwieldy. One way to transform a term into a string is to use the |
329 | 222 |
function @{ML_ind string_of_term in Syntax} in the structure @{ML_struct |
350 | 223 |
Syntax}. For more convenience, we bind this function to the toplevel. |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
224 |
*} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
225 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
226 |
ML{*val string_of_term = Syntax.string_of_term*} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
227 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
228 |
text {* |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
229 |
It can now be used as follows |
126 | 230 |
|
231 |
@{ML_response_fake [display,gray] |
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
232 |
"string_of_term @{context} @{term \"1::nat\"}" |
317
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
233 |
"\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""} |
126 | 234 |
|
350 | 235 |
We obtain a string corrsponding to the term @{term [show_types] "1::nat"} with some |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
236 |
additional information encoded in it. The string can be properly printed by |
369 | 237 |
using either the function @{ML writeln} or @{ML tracing}: |
126 | 238 |
|
239 |
@{ML_response_fake [display,gray] |
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
240 |
"writeln (string_of_term @{context} @{term \"1::nat\"})" |
126 | 241 |
"\"1\""} |
242 |
||
308
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
243 |
or |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
244 |
|
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
245 |
@{ML_response_fake [display,gray] |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
246 |
"tracing (string_of_term @{context} @{term \"1::nat\"})" |
308
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
247 |
"\"1\""} |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
248 |
|
343 | 249 |
If there are more than one term to be printed, you can use the |
369 | 250 |
function @{ML_ind commas in Library} to separate them. |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
251 |
*} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
252 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
253 |
ML{*fun string_of_terms ctxt ts = |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
254 |
commas (map (string_of_term ctxt) ts)*} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
255 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
256 |
text {* |
370
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
257 |
Sometimes you want to print out a term together with some type information. |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
258 |
This can be achieved by setting the reference |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
259 |
@{ML_ind show_types}\footnote{\bf FIXME: ``forgotten'' structure Printer, Mixfix etc.} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
260 |
to @{ML true}. |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
261 |
*} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
262 |
|
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
263 |
ML{*show_types := true*} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
264 |
|
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
265 |
text {* |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
266 |
Now @{ML string_of_term} prints out |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
267 |
|
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
268 |
@{ML_response_fake [display, gray] |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
269 |
"tracing (string_of_term @{context} @{term \"(1::nat, x)\"})" |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
270 |
"(1::nat, x::'a)"} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
271 |
|
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
272 |
where @{text 1} and @{text x} are displayed with their type. |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
273 |
Even more type information can be printed by setting |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
274 |
@{ML_ind show_all_types} to @{ML true}. We obtain |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
275 |
*} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
276 |
(*<*)ML %linenos {*show_all_types := true*} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
277 |
(*>*) |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
278 |
text {* |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
279 |
@{ML_response_fake [display, gray] |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
280 |
"tracing (string_of_term @{context} @{term \"(1::nat, x)\"})" |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
281 |
"(Pair::nat \<Rightarrow> 'a \<Rightarrow> nat \<times> 'a) (1::nat) (x::'a)"} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
282 |
|
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
283 |
Other references that influence printing are @{ML_ind show_brackets} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
284 |
and @{ML_ind show_sorts}. |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
285 |
*} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
286 |
(*<*)ML %linenos {*show_types := false; show_all_types := false*} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
287 |
(*>*) |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
288 |
text {* |
126 | 289 |
A @{ML_type cterm} can be transformed into a string by the following function. |
290 |
*} |
|
291 |
||
314 | 292 |
ML{*fun string_of_cterm ctxt ct = |
293 |
string_of_term ctxt (term_of ct)*} |
|
126 | 294 |
|
295 |
text {* |
|
369 | 296 |
In this example the function @{ML_ind term_of in Thm} extracts the @{ML_type |
314 | 297 |
term} from a @{ML_type cterm}. More than one @{ML_type cterm}s can again be |
369 | 298 |
printed with @{ML commas}. |
126 | 299 |
*} |
300 |
||
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
301 |
ML{*fun string_of_cterms ctxt cts = |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
302 |
commas (map (string_of_cterm ctxt) cts)*} |
126 | 303 |
|
304 |
text {* |
|
305 |
The easiest way to get the string of a theorem is to transform it |
|
369 | 306 |
into a @{ML_type term} using the function @{ML_ind prop_of in Thm}. |
190
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
307 |
*} |
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
308 |
|
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
309 |
ML{*fun string_of_thm ctxt thm = |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
310 |
string_of_term ctxt (prop_of thm)*} |
190
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
311 |
|
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
312 |
text {* |
350 | 313 |
Theorems include schematic variables, such as @{text "?P"}, |
343 | 314 |
@{text "?Q"} and so on. They are needed in Isabelle in order to able to |
314 | 315 |
instantiate theorems when they are applied. For example the theorem |
316 |
@{thm [source] conjI} shown below can be used for any (typable) |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
317 |
instantiation of @{text "?P"} and @{text "?Q"}. |
190
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
318 |
|
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
319 |
@{ML_response_fake [display, gray] |
301
2728e8daebc0
replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents:
299
diff
changeset
|
320 |
"tracing (string_of_thm @{context} @{thm conjI})" |
190
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
321 |
"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q"} |
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
322 |
|
314 | 323 |
However, in order to improve the readability when printing theorems, we |
324 |
convert these schematic variables into free variables using the function |
|
316
74f0a06f751f
further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents:
315
diff
changeset
|
325 |
@{ML_ind import in Variable}. This is similar to statements like @{text |
343 | 326 |
"conjI[no_vars]"} on Isabelle's user-level. |
126 | 327 |
*} |
328 |
||
158
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents:
157
diff
changeset
|
329 |
ML{*fun no_vars ctxt thm = |
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents:
157
diff
changeset
|
330 |
let |
263
195c4444dff7
added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
262
diff
changeset
|
331 |
val ((_, [thm']), _) = Variable.import true [thm] ctxt |
158
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents:
157
diff
changeset
|
332 |
in |
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents:
157
diff
changeset
|
333 |
thm' |
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents:
157
diff
changeset
|
334 |
end |
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents:
157
diff
changeset
|
335 |
|
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
336 |
fun string_of_thm_no_vars ctxt thm = |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
337 |
string_of_term ctxt (prop_of (no_vars ctxt thm))*} |
126 | 338 |
|
339 |
text {* |
|
207 | 340 |
Theorem @{thm [source] conjI} is now printed as follows: |
190
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
341 |
|
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
342 |
@{ML_response_fake [display, gray] |
301
2728e8daebc0
replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents:
299
diff
changeset
|
343 |
"tracing (string_of_thm_no_vars @{context} @{thm conjI})" |
196
840b49bfb1cf
fixed `str_of_thms' output in example + small changes
griff
parents:
192
diff
changeset
|
344 |
"\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> P \<and> Q"} |
190
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
345 |
|
126 | 346 |
Again the function @{ML commas} helps with printing more than one theorem. |
347 |
*} |
|
348 |
||
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
349 |
ML{*fun string_of_thms ctxt thms = |
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
350 |
commas (map (string_of_thm ctxt) thms) |
190
ca0ac2e75f6d
more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
189
diff
changeset
|
351 |
|
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
352 |
fun string_of_thms_no_vars ctxt thms = |
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
353 |
commas (map (string_of_thm_no_vars ctxt) thms) *} |
126 | 354 |
|
305
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
355 |
text {* |
370
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
356 |
\begin{readmore} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
357 |
The simple conversion functions from Isabelle's main datatypes to |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
358 |
@{ML_type string}s are implemented in @{ML_file "Pure/Syntax/syntax.ML"}. |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
359 |
The references that change the printing information are declared in |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
360 |
@{ML_file "Pure/Syntax/printer.ML"}. |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
361 |
\end{readmore} |
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
362 |
|
350 | 363 |
Note that when printing out several ``parcels'' of information that |
364 |
semantically belong together, like a warning message consisting of |
|
365 |
a term and its type, you should try to keep this information together in a |
|
343 | 366 |
single string. Therefore do \emph{not} print out information as |
306
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
367 |
|
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
368 |
@{ML_response_fake [display,gray] |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
369 |
"tracing \"First half,\"; |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
370 |
tracing \"and second half.\"" |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
371 |
"First half, |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
372 |
and second half."} |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
373 |
|
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
374 |
but as a single string with appropriate formatting. For example |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
375 |
|
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
376 |
@{ML_response_fake [display,gray] |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
377 |
"tracing (\"First half,\" ^ \"\\n\" ^ \"and second half.\")" |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
378 |
"First half, |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
379 |
and second half."} |
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
380 |
|
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
381 |
To ease this kind of string manipulations, there are a number |
369 | 382 |
of library functions. For example, the function @{ML_ind cat_lines in Library} |
305
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
383 |
concatenates a list of strings and inserts newlines. |
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
384 |
|
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
385 |
@{ML_response [display, gray] |
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
386 |
"cat_lines [\"foo\", \"bar\"]" |
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
387 |
"\"foo\\nbar\""} |
306
fe732e890d87
tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
388 |
|
343 | 389 |
Section \ref{sec:pretty} will also explain the infrastructure of Isabelle |
390 |
that helps with more elaborate pretty printing. |
|
350 | 391 |
|
392 |
\begin{readmore} |
|
393 |
Most of the basic string functions of Isabelle are defined in |
|
394 |
@{ML_file "Pure/library.ML"}. |
|
395 |
\end{readmore} |
|
396 |
||
305
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
397 |
*} |
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
398 |
|
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents:
304
diff
changeset
|
399 |
|
126 | 400 |
section {* Combinators\label{sec:combinators} *} |
401 |
||
402 |
text {* |
|
131 | 403 |
For beginners perhaps the most puzzling parts in the existing code of Isabelle are |
126 | 404 |
the combinators. At first they seem to greatly obstruct the |
405 |
comprehension of the code, but after getting familiar with them, they |
|
406 |
actually ease the understanding and also the programming. |
|
407 |
||
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
408 |
The simplest combinator is @{ML_ind I in Basic_Library}, which is just the |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
409 |
identity function defined as |
126 | 410 |
*} |
411 |
||
412 |
ML{*fun I x = x*} |
|
413 |
||
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
414 |
text {* |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
415 |
Another simple combinator is @{ML_ind K in Library}, defined as |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
416 |
*} |
126 | 417 |
|
418 |
ML{*fun K x = fn _ => x*} |
|
419 |
||
420 |
text {* |
|
350 | 421 |
@{ML K} ``wraps'' a function around @{text "x"} that ignores its argument. As a |
422 |
result, @{ML K} defines a constant function always returning @{text x}. |
|
126 | 423 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
424 |
The next combinator is reverse application, @{ML_ind "|>" in Basics}, defined as: |
126 | 425 |
*} |
426 |
||
427 |
ML{*fun x |> f = f x*} |
|
428 |
||
429 |
text {* While just syntactic sugar for the usual function application, |
|
430 |
the purpose of this combinator is to implement functions in a |
|
431 |
``waterfall fashion''. Consider for example the function *} |
|
432 |
||
433 |
ML %linenosgray{*fun inc_by_five x = |
|
434 |
x |> (fn x => x + 1) |
|
435 |
|> (fn x => (x, x)) |
|
436 |
|> fst |
|
437 |
|> (fn x => x + 4)*} |
|
438 |
||
439 |
text {* |
|
350 | 440 |
which increments its argument @{text x} by 5. It does this by first incrementing |
126 | 441 |
the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking |
442 |
the first component of the pair (Line 4) and finally incrementing the first |
|
443 |
component by 4 (Line 5). This kind of cascading manipulations of values is quite |
|
444 |
common when dealing with theories (for example by adding a definition, followed by |
|
445 |
lemmas and so on). The reverse application allows you to read what happens in |
|
446 |
a top-down manner. This kind of coding should also be familiar, |
|
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
447 |
if you have been exposed to Haskell's {\it do}-notation. Writing the function |
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
448 |
@{ML inc_by_five} using the reverse application is much clearer than writing |
126 | 449 |
*} |
450 |
||
451 |
ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*} |
|
452 |
||
453 |
text {* or *} |
|
454 |
||
455 |
ML{*fun inc_by_five x = |
|
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
456 |
((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*} |
126 | 457 |
|
458 |
text {* and typographically more economical than *} |
|
459 |
||
460 |
ML{*fun inc_by_five x = |
|
257
ce0f60d0351e
corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents:
256
diff
changeset
|
461 |
let val y1 = x + 1 |
ce0f60d0351e
corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents:
256
diff
changeset
|
462 |
val y2 = (y1, y1) |
ce0f60d0351e
corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents:
256
diff
changeset
|
463 |
val y3 = fst y2 |
ce0f60d0351e
corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents:
256
diff
changeset
|
464 |
val y4 = y3 + 4 |
ce0f60d0351e
corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents:
256
diff
changeset
|
465 |
in y4 end*} |
126 | 466 |
|
467 |
text {* |
|
468 |
Another reason why the let-bindings in the code above are better to be |
|
469 |
avoided: it is more than easy to get the intermediate values wrong, not to |
|
470 |
mention the nightmares the maintenance of this code causes! |
|
471 |
||
350 | 472 |
In Isabelle a ``real world'' example for a function written in |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
473 |
the waterfall fashion might be the following code: |
177 | 474 |
*} |
126 | 475 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
476 |
ML %linenosgray{*fun apply_fresh_args f ctxt = |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
477 |
f |> fastype_of |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
478 |
|> binder_types |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
479 |
|> map (pair "z") |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
480 |
|> Variable.variant_frees ctxt [f] |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
481 |
|> map Free |
257
ce0f60d0351e
corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents:
256
diff
changeset
|
482 |
|> curry list_comb f *} |
126 | 483 |
|
177 | 484 |
text {* |
266 | 485 |
This function takes a term and a context as argument. If the term is of function |
486 |
type, then @{ML "apply_fresh_args"} returns the term with distinct variables |
|
343 | 487 |
applied to it. For example below three variables are applied to the term |
298 | 488 |
@{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}: |
183
8bb4eaa2ec92
a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents:
178
diff
changeset
|
489 |
|
8bb4eaa2ec92
a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents:
178
diff
changeset
|
490 |
@{ML_response_fake [display,gray] |
266 | 491 |
"let |
492 |
val t = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"} |
|
493 |
val ctxt = @{context} |
|
494 |
in |
|
495 |
apply_fresh_args t ctxt |
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
496 |
|> string_of_term ctxt |
301
2728e8daebc0
replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents:
299
diff
changeset
|
497 |
|> tracing |
266 | 498 |
end" |
183
8bb4eaa2ec92
a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents:
178
diff
changeset
|
499 |
"P z za zb"} |
177 | 500 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
501 |
You can read off this behaviour from how @{ML apply_fresh_args} is coded: in |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
502 |
Line 2, the function @{ML_ind fastype_of in Term} calculates the type of the |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
503 |
term; @{ML_ind binder_types in Term} in the next line produces the list of |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
504 |
argument types (in the case above the list @{text "[nat, int, unit]"}); Line |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
505 |
4 pairs up each type with the string @{text "z"}; the function @{ML_ind |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
506 |
variant_frees in Variable} generates for each @{text "z"} a unique name |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
507 |
avoiding the given @{text f}; the list of name-type pairs is turned into a |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
508 |
list of variable terms in Line 6, which in the last line is applied by the |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
509 |
function @{ML_ind list_comb in Term} to the term. In this last step we have |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
510 |
to use the function @{ML_ind curry in Library}, because @{ML list_comb} |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
511 |
expects the function and the variables list as a pair. This kind of |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
512 |
functions is often needed when constructing terms with fresh variables. The |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
513 |
infrastructure helps tremendously to avoid any name clashes. Consider for |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
514 |
example: |
252 | 515 |
|
516 |
@{ML_response_fake [display,gray] |
|
266 | 517 |
"let |
518 |
val t = @{term \"za::'a \<Rightarrow> 'b \<Rightarrow> 'c\"} |
|
519 |
val ctxt = @{context} |
|
520 |
in |
|
521 |
apply_fresh_args t ctxt |
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
522 |
|> string_of_term ctxt |
301
2728e8daebc0
replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents:
299
diff
changeset
|
523 |
|> tracing |
266 | 524 |
end" |
252 | 525 |
"za z zb"} |
177 | 526 |
|
266 | 527 |
where the @{text "za"} is correctly avoided. |
528 |
||
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
529 |
The combinator @{ML_ind "#>" in Basics} is the reverse function composition. |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
530 |
It can be used to define the following function |
126 | 531 |
*} |
532 |
||
533 |
ML{*val inc_by_six = |
|
534 |
(fn x => x + 1) |
|
535 |
#> (fn x => x + 2) |
|
536 |
#> (fn x => x + 3)*} |
|
537 |
||
538 |
text {* |
|
539 |
which is the function composed of first the increment-by-one function and then |
|
540 |
increment-by-two, followed by increment-by-three. Again, the reverse function |
|
541 |
composition allows you to read the code top-down. |
|
542 |
||
350 | 543 |
The remaining combinators we describe in this section add convenience for the |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
544 |
``waterfall method'' of writing functions. The combinator @{ML_ind tap in |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
545 |
Basics} allows you to get hold of an intermediate result (to do some |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
546 |
side-calculations for instance). The function |
126 | 547 |
*} |
548 |
||
549 |
ML %linenosgray{*fun inc_by_three x = |
|
550 |
x |> (fn x => x + 1) |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
239
diff
changeset
|
551 |
|> tap (fn x => tracing (PolyML.makestring x)) |
126 | 552 |
|> (fn x => x + 2)*} |
553 |
||
554 |
text {* |
|
555 |
increments the argument first by @{text "1"} and then by @{text "2"}. In the |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
556 |
middle (Line 3), however, it uses @{ML tap} for printing the ``plus-one'' |
350 | 557 |
intermediate result. The function @{ML tap} can only be used for |
558 |
side-calculations, because any value that is computed cannot be merged back |
|
559 |
into the ``main waterfall''. To do this, you can use the next combinator. |
|
126 | 560 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
561 |
The combinator @{ML_ind "`" in Basics} (a backtick) is similar to @{ML tap}, |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
562 |
but applies a function to the value and returns the result together with the |
350 | 563 |
value (as a pair). It is defined as |
564 |
*} |
|
565 |
||
566 |
ML{*fun `f = fn x => (f x, x)*} |
|
567 |
||
568 |
text {* |
|
569 |
An example for this combinator is the function |
|
126 | 570 |
*} |
571 |
||
572 |
ML{*fun inc_as_pair x = |
|
573 |
x |> `(fn x => x + 1) |
|
574 |
|> (fn (x, y) => (x, y + 1))*} |
|
575 |
||
576 |
text {* |
|
350 | 577 |
which takes @{text x} as argument, and then increments @{text x}, but also keeps |
126 | 578 |
@{text x}. The intermediate result is therefore the pair @{ML "(x + 1, x)" |
579 |
for x}. After that, the function increments the right-hand component of the |
|
580 |
pair. So finally the result will be @{ML "(x + 1, x + 1)" for x}. |
|
581 |
||
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
582 |
The combinators @{ML_ind "|>>" in Basics} and @{ML_ind "||>" in Basics} are |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
583 |
defined for functions manipulating pairs. The first applies the function to |
126 | 584 |
the first component of the pair, defined as |
585 |
*} |
|
586 |
||
587 |
ML{*fun (x, y) |>> f = (f x, y)*} |
|
588 |
||
589 |
text {* |
|
590 |
and the second combinator to the second component, defined as |
|
591 |
*} |
|
592 |
||
593 |
ML{*fun (x, y) ||> f = (x, f y)*} |
|
594 |
||
595 |
text {* |
|
314 | 596 |
These two functions can, for example, be used to avoid explicit @{text "lets"} for |
597 |
intermediate values in functions that return pairs. As an example, suppose you |
|
308
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
598 |
want to separate a list of integers into two lists according to a |
311 | 599 |
treshold. If the treshold is @{ML "5"}, the list @{ML "[1,6,2,5,3,4]"} |
600 |
should be separated to @{ML "([1,2,3,4], [6,5])"}. This function can be |
|
601 |
implemented as |
|
308
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
602 |
*} |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
603 |
|
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
604 |
ML{*fun separate i [] = ([], []) |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
605 |
| separate i (x::xs) = |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
606 |
let |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
607 |
val (los, grs) = separate i xs |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
608 |
in |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
609 |
if i <= x then (los, x::grs) else (x::los, grs) |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
610 |
end*} |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
611 |
|
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
612 |
text {* |
350 | 613 |
where the return value of the recursive call is bound explicitly to |
309 | 614 |
the pair @{ML "(los, grs)" for los grs}. You can implement this function |
615 |
more concisely as |
|
308
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
616 |
*} |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
617 |
|
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
618 |
ML{*fun separate i [] = ([], []) |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
619 |
| separate i (x::xs) = |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
620 |
if i <= x |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
621 |
then separate i xs ||> cons x |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
622 |
else separate i xs |>> cons x*} |
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
623 |
|
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
624 |
text {* |
314 | 625 |
avoiding the explicit @{text "let"}. While in this example the gain in |
626 |
conciseness is only small, in more complicated situations the benefit of |
|
627 |
avoiding @{text "lets"} can be substantial. |
|
308
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
628 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
629 |
With the combinator @{ML_ind "|->" in Basics} you can re-combine the |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
630 |
elements from a pair. This combinator is defined as |
126 | 631 |
*} |
632 |
||
633 |
ML{*fun (x, y) |-> f = f x y*} |
|
634 |
||
215
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
635 |
text {* |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
636 |
and can be used to write the following roundabout version |
126 | 637 |
of the @{text double} function: |
638 |
*} |
|
639 |
||
640 |
ML{*fun double x = |
|
641 |
x |> (fn x => (x, x)) |
|
642 |
|-> (fn x => fn y => x + y)*} |
|
643 |
||
215
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
644 |
text {* |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
645 |
The combinator @{ML_ind ||>> in Basics} plays a central rôle whenever your |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
646 |
task is to update a theory and the update also produces a side-result (for |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
647 |
example a theorem). Functions for such tasks return a pair whose second |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
648 |
component is the theory and the fist component is the side-result. Using |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
649 |
@{ML ||>>}, you can do conveniently the update and also |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
650 |
accumulate the side-results. Consider the following simple function. |
215
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
651 |
*} |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
652 |
|
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
653 |
ML %linenosgray{*fun acc_incs x = |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
654 |
x |> (fn x => ("", x)) |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
655 |
||>> (fn x => (x, x + 1)) |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
656 |
||>> (fn x => (x, x + 1)) |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
657 |
||>> (fn x => (x, x + 1))*} |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
658 |
|
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
659 |
text {* |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
660 |
The purpose of Line 2 is to just pair up the argument with a dummy value (since |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
661 |
@{ML ||>>} operates on pairs). Each of the next three lines just increment |
280 | 662 |
the value by one, but also nest the intermediate results to the left. For example |
215
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
663 |
|
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
664 |
@{ML_response [display,gray] |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
665 |
"acc_incs 1" |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
666 |
"((((\"\", 1), 2), 3), 4)"} |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
667 |
|
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
668 |
You can continue this chain with: |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
669 |
|
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
670 |
@{ML_response [display,gray] |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
671 |
"acc_incs 1 ||>> (fn x => (x, x + 2))" |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
672 |
"(((((\"\", 1), 2), 3), 4), 6)"} |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
673 |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
674 |
\footnote{\bf FIXME: maybe give a ``real world'' example for this combinator.} |
215
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
675 |
*} |
8d1a344a621e
more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents:
210
diff
changeset
|
676 |
|
126 | 677 |
text {* |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
678 |
Recall that @{ML "|>"} is the reverse function application. Recall also that |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
679 |
the related reverse function composition is @{ML "#>"}. In fact all the |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
680 |
combinators @{ML "|->"}, @{ML "|>>"} , @{ML "||>"} and @{ML "||>>"} |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
681 |
described above have related combinators for function composition, namely |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
682 |
@{ML_ind "#->" in Basics}, @{ML_ind "#>>" in Basics}, @{ML_ind "##>" in |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
683 |
Basics} and @{ML_ind "##>>" in Basics}. Using @{ML "#->"}, for example, the |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
684 |
function @{text double} can also be written as: |
126 | 685 |
*} |
686 |
||
687 |
ML{*val double = |
|
688 |
(fn x => (x, x)) |
|
689 |
#-> (fn x => fn y => x + y)*} |
|
690 |
||
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
691 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
692 |
text {* |
314 | 693 |
When using combinators for writing functions in waterfall fashion, it is |
311 | 694 |
sometimes necessary to do some ``plumbing'' in order to fit functions |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
695 |
together. We have already seen such plumbing in the function @{ML |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
696 |
apply_fresh_args}, where @{ML curry} is needed for making the function @{ML |
350 | 697 |
list_comb}, which works over pairs to fit with the combinator @{ML "|>"}. Such |
329 | 698 |
plumbing is also needed in situations where a function operate over lists, |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
699 |
but one calculates only with a single element. An example is the function |
350 | 700 |
@{ML_ind check_terms in Syntax}, whose purpose is to simultaneously type-check |
701 |
a list of terms. Consider the code: |
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
702 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
703 |
@{ML_response_fake [display, gray] |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
704 |
"let |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
705 |
val ctxt = @{context} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
706 |
in |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
707 |
map (Syntax.parse_term ctxt) [\"m + n\", \"m * n\", \"m - (n::nat)\"] |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
708 |
|> Syntax.check_terms ctxt |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
709 |
|> string_of_terms ctxt |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
710 |
|> tracing |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
711 |
end" |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
712 |
"m + n, m * n, m - n"} |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
713 |
*} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
714 |
|
126 | 715 |
text {* |
361 | 716 |
In this example we obtain three terms (using @{ML_ind parse_term in Syntax}) whose |
717 |
variables @{text m} and @{text n} are of type @{typ "nat"}. If you have only |
|
718 |
a single term, then @{ML |
|
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
719 |
check_terms in Syntax} needs plumbing. This can be done with the function |
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
720 |
@{ML singleton}.\footnote{There is already a function @{ML check_term in |
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
721 |
Syntax} in the Isabelle sources that is defined in terms of @{ML singleton} |
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
722 |
and @{ML check_terms in Syntax}.} For example |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
723 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
724 |
@{ML_response_fake [display, gray] |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
725 |
"let |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
726 |
val ctxt = @{context} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
727 |
in |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
728 |
Syntax.parse_term ctxt \"m - (n::nat)\" |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
729 |
|> singleton (Syntax.check_terms ctxt) |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
730 |
|> string_of_term ctxt |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
731 |
|> tracing |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
732 |
end" |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
733 |
"m - n"} |
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
734 |
|
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
735 |
\begin{readmore} |
196
840b49bfb1cf
fixed `str_of_thms' output in example + small changes
griff
parents:
192
diff
changeset
|
736 |
The most frequently used combinators are defined in the files @{ML_file |
840b49bfb1cf
fixed `str_of_thms' output in example + small changes
griff
parents:
192
diff
changeset
|
737 |
"Pure/library.ML"} |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
738 |
and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
739 |
contains further information about combinators. |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
740 |
\end{readmore} |
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents:
309
diff
changeset
|
741 |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
742 |
\footnote{\bf FIXME: find a good exercise for combinators} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
743 |
\footnote{\bf FIXME: say something about calling conventions} |
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
14
diff
changeset
|
744 |
*} |
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
14
diff
changeset
|
745 |
|
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
746 |
|
323 | 747 |
section {* ML-Antiquotations *} |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
748 |
|
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
749 |
text {* |
343 | 750 |
Recall that code in Isabelle is always embedded in a theory. The main |
751 |
advantage of this is that the code can contain references to entities |
|
752 |
defined on the logical level of Isabelle. By this we mean definitions, |
|
753 |
theorems, terms and so on. This kind of reference is realised in Isabelle |
|
754 |
with ML-antiquotations, often just called antiquotations.\footnote{There are |
|
755 |
two kinds of antiquotations in Isabelle, which have very different purposes |
|
756 |
and infrastructures. The first kind, described in this section, are |
|
369 | 757 |
\emph{\index*{ML-antiquotation}}. They are used to refer to entities (like terms, |
343 | 758 |
types etc) from Isabelle's logic layer inside ML-code. The other kind of |
369 | 759 |
antiquotations are \emph{document}\index{document antiquotation} antiquotations. |
354 | 760 |
They are used only in the |
343 | 761 |
text parts of Isabelle and their purpose is to print logical entities inside |
762 |
\LaTeX-documents. Document antiquotations are part of the user level and |
|
763 |
therefore we are not interested in them in this Tutorial, except in Appendix |
|
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
764 |
\ref{rec:docantiquotations} where we show how to implement your own document |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
765 |
antiquotations.} For example, one can print out the name of the current |
350 | 766 |
theory with the code |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
767 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
768 |
@{ML_response [display,gray] "Context.theory_name @{theory}" "\"FirstSteps\""} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
769 |
|
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
770 |
where @{text "@{theory}"} is an antiquotation that is substituted with the |
49 | 771 |
current theory (remember that we assumed we are inside the theory |
89 | 772 |
@{text FirstSteps}). The name of this theory can be extracted using |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
773 |
the function @{ML_ind theory_name in Context}. |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
774 |
|
89 | 775 |
Note, however, that antiquotations are statically linked, that is their value is |
329 | 776 |
determined at ``compile-time'', not at ``run-time''. For example the function |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
777 |
*} |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
778 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
779 |
ML{*fun not_current_thyname () = Context.theory_name @{theory} *} |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
780 |
|
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
781 |
text {* |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
782 |
|
89 | 783 |
does \emph{not} return the name of the current theory, if it is run in a |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
784 |
different theory. Instead, the code above defines the constant function |
58 | 785 |
that always returns the string @{text [quotes] "FirstSteps"}, no matter where the |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
786 |
function is called. Operationally speaking, the antiquotation @{text "@{theory}"} is |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
787 |
\emph{not} replaced with code that will look up the current theory in |
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
788 |
some data structure and return it. Instead, it is literally |
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
789 |
replaced with the value representing the theory name. |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
790 |
|
132 | 791 |
In a similar way you can use antiquotations to refer to proved theorems: |
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
792 |
@{text "@{thm \<dots>}"} for a single theorem |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
793 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
794 |
@{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"} |
75 | 795 |
|
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
796 |
and @{text "@{thms \<dots>}"} for more than one |
132 | 797 |
|
798 |
@{ML_response_fake [display,gray] "@{thms conj_ac}" |
|
799 |
"(?P \<and> ?Q) = (?Q \<and> ?P) |
|
800 |
(?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R) |
|
801 |
((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"} |
|
802 |
||
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
803 |
The point of these antiquotations is that referring to theorems in this way |
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
804 |
makes your code independent from what theorems the user might have stored |
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
805 |
under this name (this becomes especially important when you deal with |
329 | 806 |
theorem lists; see Section \ref{sec:storing}). |
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
807 |
|
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
808 |
You can also refer to the current simpset via an antiquotation. To illustrate |
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
809 |
this we implement the function that extracts the theorem names stored in a |
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
810 |
simpset. |
131 | 811 |
*} |
75 | 812 |
|
149 | 813 |
ML{*fun get_thm_names_from_ss simpset = |
131 | 814 |
let |
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents:
162
diff
changeset
|
815 |
val {simps,...} = MetaSimplifier.dest_ss simpset |
70
bbb2d5f1d58d
deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
816 |
in |
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents:
162
diff
changeset
|
817 |
map #1 simps |
131 | 818 |
end*} |
54
1783211b3494
tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
819 |
|
131 | 820 |
text {* |
339
c588e8422737
used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents:
330
diff
changeset
|
821 |
The function @{ML_ind dest_ss in MetaSimplifier} returns a record containing all |
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
822 |
information stored in the simpset, but we are only interested in the names of the |
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
249
diff
changeset
|
823 |
simp-rules. Now you can feed in the current simpset into this function. |
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
824 |
The current simpset can be referred to using the antiquotation @{text "@{simpset}"}. |
81 | 825 |
|
131 | 826 |
@{ML_response_fake [display,gray] |
149 | 827 |
"get_thm_names_from_ss @{simpset}" |
828 |
"[\"Nat.of_nat_eq_id\", \"Int.of_int_eq_id\", \"Nat.One_nat_def\", \<dots>]"} |
|
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
829 |
|
196
840b49bfb1cf
fixed `str_of_thms' output in example + small changes
griff
parents:
192
diff
changeset
|
830 |
Again, this way of referencing simpsets makes you independent from additions |
350 | 831 |
of lemmas to the simpset by the user, which can potentially cause loops in your |
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
832 |
code. |
156 | 833 |
|
251 | 834 |
On the ML-level of Isabelle, you often have to work with qualified names. |
315
de49d5780f57
simplified a bit the index generation
Christian Urban <urbanc@in.tum.de>
parents:
314
diff
changeset
|
835 |
These are strings with some additional information, such as positional |
de49d5780f57
simplified a bit the index generation
Christian Urban <urbanc@in.tum.de>
parents:
314
diff
changeset
|
836 |
information and qualifiers. Such qualified names can be generated with the |
de49d5780f57
simplified a bit the index generation
Christian Urban <urbanc@in.tum.de>
parents:
314
diff
changeset
|
837 |
antiquotation @{text "@{binding \<dots>}"}. For example |
192 | 838 |
|
839 |
@{ML_response [display,gray] |
|
840 |
"@{binding \"name\"}" |
|
841 |
"name"} |
|
842 |
||
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
843 |
An example where a qualified name is needed is the function |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
343
diff
changeset
|
844 |
@{ML_ind define in LocalTheory}. This function is used below to define |
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
845 |
the constant @{term "TrueConj"} as the conjunction @{term "True \<and> True"}. |
192 | 846 |
*} |
329 | 847 |
|
192 | 848 |
local_setup %gray {* |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
849 |
LocalTheory.define Thm.internalK |
192 | 850 |
((@{binding "TrueConj"}, NoSyn), |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
851 |
(Attrib.empty_binding, @{term "True \<and> True"})) #> snd *} |
192 | 852 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
853 |
text {* |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
854 |
Now querying the definition you obtain: |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
855 |
|
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
856 |
\begin{isabelle} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
857 |
\isacommand{thm}~@{text "TrueConj_def"}\\ |
225
7859fc59249a
section for further material about simple inductive
Christian Urban <urbanc@in.tum.de>
parents:
215
diff
changeset
|
858 |
@{text "> "}~@{thm TrueConj_def} |
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
859 |
\end{isabelle} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
860 |
|
329 | 861 |
\begin{readmore} |
862 |
The basic operations on bindings are implemented in |
|
863 |
@{ML_file "Pure/General/binding.ML"}. |
|
864 |
\end{readmore} |
|
865 |
||
351 | 866 |
\footnote{\bf FIXME give a better example why bindings are important} |
867 |
\footnote{\bf FIXME give a pointer to \isacommand{local\_setup}; if not, then explain |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
868 |
why @{ML snd} is needed.} |
329 | 869 |
\footnote{\bf FIXME: There should probably a separate section on binding, long-names |
870 |
and sign.} |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
871 |
|
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
872 |
It is also possible to define your own antiquotations. But you should |
315
de49d5780f57
simplified a bit the index generation
Christian Urban <urbanc@in.tum.de>
parents:
314
diff
changeset
|
873 |
exercise care when introducing new ones, as they can also make your code |
350 | 874 |
also difficult to read. In the next chapter we describe how the (build in) |
875 |
antiquotation @{text "@{term \<dots>}"} for constructing terms. A |
|
315
de49d5780f57
simplified a bit the index generation
Christian Urban <urbanc@in.tum.de>
parents:
314
diff
changeset
|
876 |
restriction of this antiquotation is that it does not allow you to use |
350 | 877 |
schematic variables in terms. If you want to have an antiquotation that does not have |
323 | 878 |
this restriction, you can implement your own using the function @{ML_ind |
350 | 879 |
inline in ML_Antiquote} in the structure @{ML_struct ML_Antiquote}. The code |
880 |
for the antiquotation @{text "term_pat"} is as follows. |
|
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
881 |
*} |
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
882 |
|
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
883 |
ML %linenosgray{*let |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
884 |
val parser = Args.context -- Scan.lift Args.name_source |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
885 |
|
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
886 |
fun term_pat (ctxt, str) = |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
887 |
str |> ProofContext.read_term_pattern ctxt |
264 | 888 |
|> ML_Syntax.print_term |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
889 |
|> ML_Syntax.atomic |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
890 |
in |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
891 |
ML_Antiquote.inline "term_pat" (parser >> term_pat) |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
892 |
end*} |
263
195c4444dff7
added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
262
diff
changeset
|
893 |
|
195c4444dff7
added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
262
diff
changeset
|
894 |
text {* |
308
c90f4ec30d43
improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents:
307
diff
changeset
|
895 |
The parser in Line 2 provides us with a context and a string; this string is |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
323
diff
changeset
|
896 |
transformed into a term using the function @{ML_ind read_term_pattern in |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
897 |
ProofContext} (Line 5); the next two lines transform the term into a string |
350 | 898 |
so that the ML-system can understand it. An example for this antiquotation is: |
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
899 |
|
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
900 |
@{ML_response_fake [display,gray] |
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
901 |
"@{term_pat \"Suc (?x::nat)\"}" |
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
902 |
"Const (\"Suc\", \"nat \<Rightarrow> nat\") $ Var ((\"x\", 0), \"nat\")"} |
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
903 |
|
323 | 904 |
which shows the internal representation of the term @{text "Suc ?x"}. |
298 | 905 |
|
263
195c4444dff7
added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
262
diff
changeset
|
906 |
\begin{readmore} |
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
907 |
The file @{ML_file "Pure/ML/ml_antiquote.ML"} contains the the definitions |
323 | 908 |
for most antiquotations. Most of the basic operations on ML-syntax are implemented |
909 |
in @{ML_file "Pure/ML/ml_syntax.ML"}. |
|
263
195c4444dff7
added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
262
diff
changeset
|
910 |
\end{readmore} |
323 | 911 |
*} |
912 |
||
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
913 |
section {* Storing Data in Isabelle\label{sec:storing} *} |
292
41a802bbb7df
added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents:
290
diff
changeset
|
914 |
|
323 | 915 |
text {* |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
916 |
Isabelle provides mechanisms for storing (and retrieving) arbitrary |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
917 |
data. Before we delve into the details, let us digress a bit. Conventional |
350 | 918 |
wisdom has it that the type-system of ML ensures that an |
919 |
@{ML_type "'a list"}, say, can only hold elements of the same type, namely |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
920 |
@{ML_type "'a"}. Despite this wisdom, however, it is possible to implement a |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
921 |
universal type in ML, although by some arguably accidental features of ML. |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
922 |
This universal type can be used to store data of different type into a single list. |
350 | 923 |
In fact, it allows one to inject and to project data of \emph{arbitrary} type. This is |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
924 |
in contrast to datatypes, which only allow injection and projection of data for |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
925 |
some fixed collection of types. In light of the conventional wisdom cited |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
926 |
above it is important to keep in mind that the universal type does not |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
927 |
destroy type-safety of ML: storing and accessing the data can only be done |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
928 |
in a type-safe manner. |
323 | 929 |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
930 |
\begin{readmore} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
931 |
In Isabelle the universal type is implemented as the type @{ML_type |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
932 |
Universal.universal} in the file |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
933 |
@{ML_file "Pure/ML-Systems/universal.ML"}. |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
934 |
\end{readmore} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
935 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
936 |
We will show the usage of the universal type by storing an integer and |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
937 |
a boolean into a single list. Let us first define injection and projection |
350 | 938 |
functions for booleans and integers into and from the type @{ML_type Universal.universal}. |
323 | 939 |
*} |
940 |
||
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
941 |
ML{*local |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
942 |
val fn_int = Universal.tag () : int Universal.tag |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
943 |
val fn_bool = Universal.tag () : bool Universal.tag |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
944 |
in |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
945 |
val inject_int = Universal.tagInject fn_int; |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
946 |
val inject_bool = Universal.tagInject fn_bool; |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
947 |
val project_int = Universal.tagProject fn_int; |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
948 |
val project_bool = Universal.tagProject fn_bool |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
949 |
end*} |
298 | 950 |
|
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
951 |
text {* |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
952 |
Using the injection functions, we can inject the integer @{ML_text "13"} |
330 | 953 |
and the boolean value @{ML_text "true"} into @{ML_type Universal.universal}, and |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
954 |
then store them in a @{ML_type "Universal.universal list"} as follows: |
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
955 |
*} |
323 | 956 |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
957 |
ML{*val foo_list = |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
958 |
let |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
959 |
val thirteen = inject_int 13 |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
960 |
val truth_val = inject_bool true |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
961 |
in |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
962 |
[thirteen, truth_val] |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
963 |
end*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
964 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
965 |
text {* |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
966 |
The data can be retrieved using the projection functions. |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
967 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
968 |
@{ML_response [display, gray] |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
969 |
"(project_int (nth foo_list 0), project_bool (nth foo_list 1))" |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
970 |
"(13, true)"} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
971 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
972 |
Notice that we access the integer as an integer and the boolean as |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
973 |
a boolean. If we attempt to access the integer as a boolean, then we get |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
974 |
a runtime error. |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
975 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
976 |
@{ML_response_fake [display, gray] |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
977 |
"project_bool (nth foo_list 0)" |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
978 |
"*** Exception- Match raised"} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
979 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
980 |
This runtime error is the reason why ML is still type-sound despite |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
981 |
containing a universal type. |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
982 |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
983 |
Now, Isabelle heavily uses this mechanism for storing all sorts of |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
984 |
data: theorem lists, simpsets, facts etc. Roughly speaking, there are two |
350 | 985 |
places where data can be stored in Isabelle: in \emph{theories} and in \emph{proof |
986 |
contexts}. Data such as simpsets need to be stored |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
987 |
in a theory, since they need to be maintained across proofs and even across |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
988 |
theories. On the other hand, data such as facts change inside a proof and |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
989 |
are only relevant to the proof at hand. Therefore such data needs to be |
350 | 990 |
maintained inside a proof context. |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
991 |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
992 |
For theories and proof contexts there are, respectively, the functors |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
993 |
@{ML_funct_ind TheoryDataFun} and @{ML_funct_ind ProofDataFun} that help |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
994 |
with the data storage. Below we show how to implement a table in which we |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
995 |
can store theorems and look them up according to a string key. The |
350 | 996 |
intention in this example is to be able to look up introduction rules for logical |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
997 |
connectives. Such a table might be useful in an automatic proof procedure |
350 | 998 |
and therefore it makes sense to store this data inside a theory. |
999 |
Therefore we use the functor @{ML_funct TheoryDataFun}. |
|
1000 |
The code for the table is: |
|
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
1001 |
*} |
323 | 1002 |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1003 |
ML %linenosgray{*structure Data = TheoryDataFun |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1004 |
(type T = thm Symtab.table |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1005 |
val empty = Symtab.empty |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1006 |
val copy = I |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1007 |
val extend = I |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1008 |
fun merge _ = Symtab.merge (K true))*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1009 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1010 |
text {* |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1011 |
In order to store data in a theory, we have to specify the type of the data |
350 | 1012 |
(Line 2). In this case we specify the type @{ML_type "thm Symtab.table"}, |
1013 |
which stands for a table in which @{ML_type string}s can be looked up |
|
1014 |
producing an associated @{ML_type thm}. We also have to specify four |
|
1015 |
functions to use this functor: namely how to initialise the data storage |
|
1016 |
(Line 3), how to copy it (Line 4), how to extend it (Line 5) and how two |
|
1017 |
tables should be merged (Line 6). These functions correspond roughly to the |
|
1018 |
operations performed on theories and we just give some sensible |
|
1019 |
defaults\footnote{\bf FIXME: Say more about the |
|
1020 |
assumptions of these operations.} The result structure @{ML_text Data} |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1021 |
contains functions for accessing the table (@{ML Data.get}) and for updating |
350 | 1022 |
it (@{ML Data.map}). There are also two more functions (@{ML Data.init} and |
1023 |
@{ML Data.put}), which however are not relevant here. Below we define two |
|
1024 |
auxiliary functions, which help us with accessing the table. |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1025 |
*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1026 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1027 |
ML{*val lookup = Symtab.lookup o Data.get |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1028 |
fun update k v = Data.map (Symtab.update (k, v))*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1029 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1030 |
text {* |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1031 |
Since we want to store introduction rules associated with their |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1032 |
logical connective, we can fill the table as follows. |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1033 |
*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1034 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1035 |
setup %gray {* |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1036 |
update "op &" @{thm conjI} #> |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1037 |
update "op -->" @{thm impI} #> |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1038 |
update "All" @{thm allI} |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
325
diff
changeset
|
1039 |
*} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
325
diff
changeset
|
1040 |
|
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
325
diff
changeset
|
1041 |
text {* |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1042 |
The use of the command \isacommand{setup} makes sure the table in the |
350 | 1043 |
\emph{current} theory is updated (this is explained further in |
1044 |
section~\ref{sec:theories}). The lookup can now be performed as follows. |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1045 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1046 |
@{ML_response_fake [display, gray] |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1047 |
"lookup @{theory} \"op &\"" |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1048 |
"SOME \"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q\""} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1049 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1050 |
An important point to note is that these tables (and data in general) |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1051 |
need to be treated in a purely functional fashion. Although |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1052 |
we can update the table as follows |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1053 |
*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1054 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1055 |
setup %gray {* update "op &" @{thm TrueI} *} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1056 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1057 |
text {* |
350 | 1058 |
and accordingly, @{ML lookup} now produces the introduction rule for @{term "True"} |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1059 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1060 |
@{ML_response_fake [display, gray] |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1061 |
"lookup @{theory} \"op &\"" |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1062 |
"SOME \"True\""} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1063 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1064 |
there are no references involved. This is one of the most fundamental |
350 | 1065 |
coding conventions for programming in Isabelle. References |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1066 |
interfere with the multithreaded execution model of Isabelle and also |
350 | 1067 |
defeat its undo-mechanism. To see the latter, consider the |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1068 |
following data container where we maintain a reference to a list of |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1069 |
integers. |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1070 |
*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1071 |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1072 |
ML{*structure WrongRefData = TheoryDataFun |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1073 |
(type T = (int list) Unsynchronized.ref |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1074 |
val empty = Unsynchronized.ref [] |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1075 |
val copy = I |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1076 |
val extend = I |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1077 |
fun merge _ = fst)*} |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1078 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1079 |
text {* |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1080 |
We initialise the reference with the empty list. Consequently a first |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1081 |
lookup produces @{ML "ref []" in Unsynchronized}. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1082 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1083 |
@{ML_response_fake [display,gray] |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1084 |
"WrongRefData.get @{theory}" |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1085 |
"ref []"} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1086 |
|
329 | 1087 |
For updating the reference we use the following function |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1088 |
*} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1089 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1090 |
ML{*fun ref_update n = WrongRefData.map |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1091 |
(fn r => let val _ = r := n::(!r) in r end)*} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1092 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1093 |
text {* |
329 | 1094 |
which takes an integer and adds it to the content of the reference. |
350 | 1095 |
As before, we update the reference with the command |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1096 |
\isacommand{setup}. |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1097 |
*} |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1098 |
|
347 | 1099 |
setup %gray {* ref_update 1 *} |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1100 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1101 |
text {* |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1102 |
A lookup in the current theory gives then the expected list |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1103 |
@{ML "ref [1]" in Unsynchronized}. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1104 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1105 |
@{ML_response_fake [display,gray] |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1106 |
"WrongRefData.get @{theory}" |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1107 |
"ref [1]"} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1108 |
|
347 | 1109 |
So far everything is as expected. But, the trouble starts if we attempt to |
350 | 1110 |
backtrack to the ``point'' before the \isacommand{setup}-command. There, we |
347 | 1111 |
would expect that the list is empty again. But since it is stored in a |
1112 |
reference, Isabelle has no control over it. So it is not empty, but still |
|
1113 |
@{ML "ref [1]" in Unsynchronized}. Adding to the trouble, if we execute the |
|
1114 |
\isacommand{setup}-command again, we do not obtain @{ML "ref [1]" in |
|
1115 |
Unsynchronized}, but |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1116 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1117 |
@{ML_response_fake [display,gray] |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1118 |
"WrongRefData.get @{theory}" |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1119 |
"ref [1, 1]"} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1120 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1121 |
Now imagine how often you go backwards and forwards in your proof scripts. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1122 |
By using references in Isabelle code, you are bound to cause all |
329 | 1123 |
hell to break loose. Therefore observe the coding convention: |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1124 |
Do not use references for storing data! |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1125 |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1126 |
\begin{readmore} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1127 |
The functors for data storage are defined in @{ML_file "Pure/context.ML"}. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1128 |
Isabelle contains implementations of several container data structures, |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1129 |
including association lists in @{ML_file "Pure/General/alist.ML"}, |
347 | 1130 |
directed graphs in @{ML_file "Pure/General/graph.ML"}, and |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1131 |
tables and symtables in @{ML_file "Pure/General/table.ML"}. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1132 |
\end{readmore} |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1133 |
|
350 | 1134 |
Storing data in a proof context is done in a similar fashion. As mentioned |
1135 |
before, the corresponding functor is @{ML_funct_ind ProofDataFun}. With the |
|
1136 |
following code we can store a list of terms in a proof context. |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1137 |
*} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1138 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1139 |
ML{*structure Data = ProofDataFun |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1140 |
(type T = term list |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1141 |
fun init _ = [])*} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1142 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1143 |
text {* |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1144 |
The function we have to specify has to produce a list once a context |
350 | 1145 |
is initialised (possibly taking the theory into account from which the |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1146 |
context is derived). We choose to just return the empty list. Next |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1147 |
we define two auxiliary functions for updating the list with a given |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1148 |
term and printing the list. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1149 |
*} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1150 |
|
330 | 1151 |
ML{*fun update trm = Data.map (fn trms => trm::trms) |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1152 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1153 |
fun print ctxt = |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1154 |
case (Data.get ctxt) of |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1155 |
[] => tracing "Empty!" |
330 | 1156 |
| trms => tracing (string_of_terms ctxt trms)*} |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1157 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1158 |
text {* |
330 | 1159 |
Next we start with the context generated by the antiquotation |
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1160 |
@{text "@{context}"} and update it in various ways. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1161 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1162 |
@{ML_response_fake [display,gray] |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1163 |
"let |
347 | 1164 |
val ctxt0 = @{context} |
1165 |
val ctxt1 = ctxt0 |> update @{term \"False\"} |
|
1166 |
|> update @{term \"True \<and> True\"} |
|
1167 |
val ctxt2 = ctxt0 |> update @{term \"1::nat\"} |
|
1168 |
val ctxt3 = ctxt2 |> update @{term \"2::nat\"} |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1169 |
in |
347 | 1170 |
print ctxt0; |
1171 |
print ctxt1; |
|
1172 |
print ctxt2; |
|
1173 |
print ctxt3 |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1174 |
end" |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1175 |
"Empty! |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1176 |
True \<and> True, False |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1177 |
1 |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1178 |
2, 1"} |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1179 |
|
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1180 |
Many functions in Isabelle manage and update data in a similar |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1181 |
fashion. Consequently, such calculation with contexts occur frequently in |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1182 |
Isabelle code, although the ``context flow'' is usually only linear. |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1183 |
Note also that the calculation above has no effect on the underlying |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1184 |
theory. Once we throw away the contexts, we have no access to their |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1185 |
associated data. This is different to theories, where the command |
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1186 |
\isacommand{setup} registers the data with the current and future |
330 | 1187 |
theories, and therefore one can access the data potentially |
347 | 1188 |
indefinitely. |
329 | 1189 |
|
350 | 1190 |
For convenience there is an abstract layer, namely the type @{ML_type Context.generic}, |
1191 |
for treating theories and proof contexts more uniformly. This type is defined as follows |
|
330 | 1192 |
*} |
1193 |
||
1194 |
ML_val{*datatype generic = |
|
1195 |
Theory of theory |
|
1196 |
| Proof of proof*} |
|
1197 |
||
1198 |
text {* |
|
350 | 1199 |
\footnote{\bf FIXME: say more about generic contexts.} |
329 | 1200 |
|
1201 |
There are two special instances of the data storage mechanism described |
|
350 | 1202 |
above. The first instance implements named theorem lists using the functor |
1203 |
@{ML_funct_ind Named_Thms}. This is because storing theorems in a list |
|
1204 |
is such a common task. To obtain a named theorem list, you just declare |
|
329 | 1205 |
*} |
1206 |
||
1207 |
ML{*structure FooRules = Named_Thms |
|
1208 |
(val name = "foo" |
|
1209 |
val description = "Theorems for foo") *} |
|
1210 |
||
1211 |
text {* |
|
1212 |
and set up the @{ML_struct FooRules} with the command |
|
1213 |
*} |
|
1214 |
||
1215 |
setup %gray {* FooRules.setup *} |
|
1216 |
||
1217 |
text {* |
|
1218 |
This code declares a data container where the theorems are stored, |
|
1219 |
an attribute @{text foo} (with the @{text add} and @{text del} options |
|
1220 |
for adding and deleting theorems) and an internal ML-interface to retrieve and |
|
1221 |
modify the theorems. |
|
350 | 1222 |
Furthermore, the theorems are made available on the user-level under the name |
1223 |
@{text foo}. For example you can declare three lemmas to be a member of the |
|
1224 |
theorem list @{text foo} by: |
|
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
325
diff
changeset
|
1225 |
*} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
325
diff
changeset
|
1226 |
|
329 | 1227 |
lemma rule1[foo]: "A" sorry |
1228 |
lemma rule2[foo]: "B" sorry |
|
1229 |
lemma rule3[foo]: "C" sorry |
|
1230 |
||
1231 |
text {* and undeclare the first one by: *} |
|
1232 |
||
1233 |
declare rule1[foo del] |
|
1234 |
||
350 | 1235 |
text {* You can query the remaining ones with: |
329 | 1236 |
|
1237 |
\begin{isabelle} |
|
1238 |
\isacommand{thm}~@{text "foo"}\\ |
|
1239 |
@{text "> ?C"}\\ |
|
1240 |
@{text "> ?B"} |
|
1241 |
\end{isabelle} |
|
1242 |
||
1243 |
On the ML-level, we can add theorems to the list with @{ML FooRules.add_thm}: |
|
1244 |
*} |
|
1245 |
||
347 | 1246 |
setup %gray {* Context.theory_map (FooRules.add_thm @{thm TrueI}) *} |
329 | 1247 |
|
1248 |
text {* |
|
1249 |
The rules in the list can be retrieved using the function |
|
1250 |
@{ML FooRules.get}: |
|
1251 |
||
347 | 1252 |
@{ML_response_fake [display,gray] |
1253 |
"FooRules.get @{context}" |
|
1254 |
"[\"True\", \"?C\",\"?B\"]"} |
|
1255 |
||
1256 |
Note that this function takes a proof context as argument. This might be |
|
350 | 1257 |
confusing, since the theorem list is stored as theory data. It becomes clear by knowing |
1258 |
that the proof context contains the information about the current theory and so the function |
|
347 | 1259 |
can access the theorem list in the theory via the context. |
329 | 1260 |
|
1261 |
\begin{readmore} |
|
347 | 1262 |
For more information about named theorem lists see |
1263 |
@{ML_file "Pure/Tools/named_thms.ML"}. |
|
329 | 1264 |
\end{readmore} |
1265 |
||
1266 |
The second special instance of the data storage mechanism are configuration |
|
1267 |
values. They are used to enable users to configure tools without having to |
|
1268 |
resort to the ML-level (and also to avoid references). Assume you want the |
|
1269 |
user to control three values, say @{text bval} containing a boolean, @{text |
|
1270 |
ival} containing an integer and @{text sval} containing a string. These |
|
1271 |
values can be declared by |
|
1272 |
*} |
|
1273 |
||
1274 |
ML{*val (bval, setup_bval) = Attrib.config_bool "bval" false |
|
1275 |
val (ival, setup_ival) = Attrib.config_int "ival" 0 |
|
1276 |
val (sval, setup_sval) = Attrib.config_string "sval" "some string" *} |
|
1277 |
||
1278 |
text {* |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1279 |
where each value needs to be given a default. To enable these values on the |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1280 |
user-level, they need to be set up with |
329 | 1281 |
*} |
1282 |
||
1283 |
setup %gray {* |
|
1284 |
setup_bval #> |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1285 |
setup_ival #> |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1286 |
setup_sval |
329 | 1287 |
*} |
1288 |
||
1289 |
text {* |
|
1290 |
The user can now manipulate the values from the user-level of Isabelle |
|
1291 |
with the command |
|
1292 |
*} |
|
1293 |
||
1294 |
declare [[bval = true, ival = 3]] |
|
1295 |
||
1296 |
text {* |
|
1297 |
On the ML-level these values can be retrieved using the |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1298 |
function @{ML_ind get in Config} from a proof context |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1299 |
|
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1300 |
@{ML_response [display,gray] |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1301 |
"Config.get @{context} bval" |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1302 |
"true"} |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1303 |
|
347 | 1304 |
or directly from a theory using the function @{ML_ind get_thy in Config} |
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1305 |
|
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1306 |
@{ML_response [display,gray] |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1307 |
"Config.get_thy @{theory} bval" |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1308 |
"true"} |
329 | 1309 |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1310 |
It is also possible to manipulate the configuration values |
347 | 1311 |
from the ML-level with the functions @{ML_ind put in Config} |
1312 |
and @{ML_ind put_thy in Config}. For example |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1313 |
|
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1314 |
@{ML_response [display,gray] |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1315 |
"let |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1316 |
val ctxt = @{context} |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1317 |
val ctxt' = Config.put sval \"foo\" ctxt |
347 | 1318 |
val ctxt'' = Config.put sval \"bar\" ctxt' |
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1319 |
in |
350 | 1320 |
(Config.get ctxt sval, |
1321 |
Config.get ctxt' sval, |
|
1322 |
Config.get ctxt'' sval) |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1323 |
end" |
347 | 1324 |
"(\"some string\", \"foo\", \"bar\")"} |
329 | 1325 |
|
1326 |
\begin{readmore} |
|
1327 |
For more information about configuration values see |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1328 |
the files @{ML_file "Pure/Isar/attrib.ML"} and |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
1329 |
@{ML_file "Pure/config.ML"}. |
329 | 1330 |
\end{readmore} |
343 | 1331 |
*} |
1332 |
||
1333 |
section {* Summary *} |
|
1334 |
||
1335 |
text {* |
|
1336 |
This chapter describes the combinators that are used in Isabelle, as well |
|
1337 |
as a simple printing infrastructure for @{ML_type term}, @{ML_type cterm} |
|
1338 |
and @{ML_type thm}. The section on ML-antiquotations shows how to refer |
|
1339 |
statically to entities from the logic level of Isabelle. Isabelle also |
|
1340 |
contains mechanisms for storing arbitrary data in theory and proof |
|
1341 |
contexts. |
|
1342 |
||
347 | 1343 |
\begin{conventions} |
1344 |
\begin{itemize} |
|
370
2494b5b7a85d
added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
1345 |
\item Print messages that belong together in a single string. |
350 | 1346 |
\item Do not use references in Isabelle code. |
347 | 1347 |
\end{itemize} |
1348 |
\end{conventions} |
|
1349 |
||
329 | 1350 |
*} |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1351 |
|
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1352 |
|
325
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
1353 |
(**************************************************) |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
1354 |
(* bak *) |
352e31d9dacc
started section about storing data
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
1355 |
(**************************************************) |
263
195c4444dff7
added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
262
diff
changeset
|
1356 |
|
322 | 1357 |
(* |
1358 |
section {* Do Not Try This At Home! *} |
|
1359 |
||
1360 |
ML {* val my_thms = ref ([]: thm list) *} |
|
1361 |
||
1362 |
attribute_setup my_thm_bad = |
|
1363 |
{* Scan.succeed (Thm.declaration_attribute (fn th => fn ctxt => |
|
1364 |
(my_thms := th :: ! my_thms; ctxt))) *} "bad attribute" |
|
1365 |
||
1366 |
declare sym [my_thm_bad] |
|
1367 |
declare refl [my_thm_bad] |
|
1368 |
||
1369 |
ML "!my_thms" |
|
1370 |
*) |
|
196
840b49bfb1cf
fixed `str_of_thms' output in example + small changes
griff
parents:
192
diff
changeset
|
1371 |
end |