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