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