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