author | Christian Urban <urbanc@in.tum.de> |
Tue, 24 Feb 2009 01:30:15 +0000 | |
changeset 133 | 3e94ccc0f31e |
parent 132 | 2d9198bcb850 |
child 134 | 328370b75c33 |
permissions | -rw-r--r-- |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1 |
theory FirstSteps |
25
e2f9f94b26d4
Antiquotation setup is now contained in theory Base.
berghofe
parents:
21
diff
changeset
|
2 |
imports Base |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
3 |
begin |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
|
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
5 |
chapter {* First Steps *} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
6 |
|
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
7 |
text {* |
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
8 |
|
54
1783211b3494
tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
9 |
Isabelle programming is done in ML. Just like lemmas and proofs, ML-code |
89 | 10 |
in Isabelle is 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
|
11 |
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
|
12 |
|
6 | 13 |
\begin{center} |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
14 |
\begin{tabular}{@ {}l} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
15 |
\isacommand{theory} FirstSteps\\ |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
16 |
\isacommand{imports} Main\\ |
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
17 |
\isacommand{begin}\\ |
6 | 18 |
\ldots |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
19 |
\end{tabular} |
6 | 20 |
\end{center} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
21 |
*} |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
22 |
|
20 | 23 |
section {* Including ML-Code *} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
24 |
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
25 |
|
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
26 |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
27 |
text {* |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
28 |
The easiest and quickest way to include code in a theory is |
89 | 29 |
by using the \isacommand{ML}-command. For example: |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
30 |
|
75 | 31 |
\begin{isabelle} |
32 |
\begin{graybox} |
|
85
b02904872d6b
better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents:
84
diff
changeset
|
33 |
\isacommand{ML}~@{text "\<verbopen>"}\isanewline |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
34 |
\hspace{5mm}@{ML "3 + 4"}\isanewline |
85
b02904872d6b
better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents:
84
diff
changeset
|
35 |
@{text "\<verbclose>"}\isanewline |
b02904872d6b
better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents:
84
diff
changeset
|
36 |
@{text "> 7"}\smallskip |
75 | 37 |
\end{graybox} |
38 |
\end{isabelle} |
|
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
39 |
|
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
|
40 |
Like normal Isabelle proof scripts, \isacommand{ML}-commands can be |
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
|
41 |
evaluated by using the advance and undo buttons of your Isabelle |
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
|
42 |
environment. The code inside the \isacommand{ML}-command can also contain |
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
|
43 |
value and function bindings, and even those can be undone when the proof |
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
|
44 |
script is retracted. As mentioned earlier, we will drop the |
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
|
45 |
\isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we |
107
258ce361ba1b
polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents:
104
diff
changeset
|
46 |
show code. The lines prefixed with @{text [quotes] ">"} are not part of the |
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
|
47 |
code, rather they indicate what the response is when the code is evaluated. |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
48 |
|
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
|
49 |
Once a portion of code is relatively stable, you usually want to export it |
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
|
50 |
to a separate ML-file. Such files can then be included in a theory by using |
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
|
51 |
the \isacommand{uses}-command in the header of the theory, like: |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
52 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
53 |
\begin{center} |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
54 |
\begin{tabular}{@ {}l} |
54
1783211b3494
tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
55 |
\isacommand{theory} FirstSteps\\ |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
56 |
\isacommand{imports} Main\\ |
58 | 57 |
\isacommand{uses} @{text "\"file_to_be_included.ML\""} @{text "\<dots>"}\\ |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
58 |
\isacommand{begin}\\ |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
59 |
\ldots |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
60 |
\end{tabular} |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
61 |
\end{center} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
62 |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
63 |
*} |
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
64 |
|
126 | 65 |
section {* Debugging and Printing\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
|
66 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
67 |
text {* |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
68 |
|
50 | 69 |
During development you might find it necessary to inspect some data |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
70 |
in your code. This can be done in a ``quick-and-dirty'' fashion using |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
71 |
the function @{ML "warning"}. For example |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
72 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
73 |
@{ML_response_fake [display,gray] "warning \"any string\"" "\"any string\""} |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
74 |
|
58 | 75 |
will print out @{text [quotes] "any string"} inside the response buffer |
76 |
of Isabelle. This function expects a string as argument. If you develop under PolyML, |
|
50 | 77 |
then there is a convenient, though again ``quick-and-dirty'', method for |
107
258ce361ba1b
polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents:
104
diff
changeset
|
78 |
converting values into strings, namely the function @{ML makestring}: |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
79 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
80 |
@{ML_response_fake [display,gray] "warning (makestring 1)" "\"1\""} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
81 |
|
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
82 |
However @{ML makestring} only works if the type of what is converted is monomorphic |
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
83 |
and not a function. |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
84 |
|
52 | 85 |
The function @{ML "warning"} should only be used for testing purposes, because any |
86 |
output this function generates will be overwritten as soon as an error is |
|
50 | 87 |
raised. For printing anything more serious and elaborate, the |
54
1783211b3494
tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
88 |
function @{ML tracing} is more appropriate. This function writes all output into |
89 | 89 |
a separate tracing buffer. For example: |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
90 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
91 |
@{ML_response_fake [display,gray] "tracing \"foo\"" "\"foo\""} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
92 |
|
58 | 93 |
It is also possible to redirect the ``channel'' where the string @{text "foo"} is |
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
94 |
printed to a separate file, e.g.~to prevent ProofGeneral from choking on massive |
107
258ce361ba1b
polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents:
104
diff
changeset
|
95 |
amounts of trace output. This redirection can be achieved with the code: |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
96 |
*} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
97 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
98 |
ML{*val strip_specials = |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
99 |
let |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
100 |
fun strip ("\^A" :: _ :: cs) = strip cs |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
101 |
| strip (c :: cs) = c :: strip cs |
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
102 |
| strip [] = []; |
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
103 |
in implode o strip o explode end; |
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
104 |
|
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
105 |
fun redirect_tracing stream = |
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
106 |
Output.tracing_fn := (fn s => |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
107 |
(TextIO.output (stream, (strip_specials s)); |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
108 |
TextIO.output (stream, "\n"); |
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
109 |
TextIO.flushOut stream)) *} |
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents:
13
diff
changeset
|
110 |
|
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
|
111 |
text {* |
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
|
112 |
Calling @{ML "redirect_tracing"} with @{ML "(TextIO.openOut \"foo.bar\")"} |
58 | 113 |
will cause that all tracing information is printed into the file @{text "foo.bar"}. |
75 | 114 |
|
107
258ce361ba1b
polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents:
104
diff
changeset
|
115 |
You can print out error messages with the function @{ML error}; for example: |
75 | 116 |
|
122 | 117 |
@{ML_response_fake [display,gray] "if 0=1 then true else (error \"foo\")" |
118 |
"Exception- ERROR \"foo\" raised |
|
119 |
At command \"ML\"."} |
|
75 | 120 |
|
126 | 121 |
Most often you want to inspect data of type @{ML_type term}, @{ML_type cterm} |
122 |
or @{ML_type thm}. Isabelle contains elaborate pretty-printing functions for printing them, |
|
123 |
but for quick-and-dirty solutions they are far too unwieldy. A simple way to transform |
|
124 |
a term into a string is to use the function @{ML Syntax.string_of_term}. |
|
125 |
||
126 |
@{ML_response_fake [display,gray] |
|
127 |
"Syntax.string_of_term @{context} @{term \"1::nat\"}" |
|
128 |
"\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""} |
|
129 |
||
130 |
This produces a string with some additional information encoded in it. The string |
|
131 |
can be properly printed by using the function @{ML warning}. |
|
132 |
||
133 |
@{ML_response_fake [display,gray] |
|
134 |
"warning (Syntax.string_of_term @{context} @{term \"1::nat\"})" |
|
135 |
"\"1\""} |
|
136 |
||
137 |
A @{ML_type cterm} can be transformed into a string by the following function. |
|
138 |
*} |
|
139 |
||
140 |
ML{*fun str_of_cterm ctxt t = |
|
141 |
Syntax.string_of_term ctxt (term_of t)*} |
|
142 |
||
143 |
text {* |
|
144 |
If there are more than one @{ML_type cterm}s to be printed, you can use the |
|
145 |
function @{ML commas} to separate them. |
|
146 |
*} |
|
147 |
||
148 |
ML{*fun str_of_cterms ctxt ts = |
|
149 |
commas (map (str_of_cterm ctxt) ts)*} |
|
150 |
||
151 |
text {* |
|
152 |
The easiest way to get the string of a theorem is to transform it |
|
153 |
into a @{ML_type cterm} using the function @{ML crep_thm}. |
|
154 |
*} |
|
155 |
||
156 |
ML{*fun str_of_thm ctxt thm = |
|
157 |
let |
|
158 |
val {prop, ...} = crep_thm thm |
|
159 |
in |
|
160 |
str_of_cterm ctxt prop |
|
161 |
end*} |
|
162 |
||
163 |
text {* |
|
164 |
Again the function @{ML commas} helps with printing more than one theorem. |
|
165 |
*} |
|
166 |
||
167 |
ML{*fun str_of_thms ctxt thms = |
|
168 |
commas (map (str_of_thm ctxt) thms)*} |
|
169 |
||
170 |
||
171 |
section {* Combinators\label{sec:combinators} *} |
|
172 |
||
173 |
text {* |
|
131 | 174 |
For beginners perhaps the most puzzling parts in the existing code of Isabelle are |
126 | 175 |
the combinators. At first they seem to greatly obstruct the |
176 |
comprehension of the code, but after getting familiar with them, they |
|
177 |
actually ease the understanding and also the programming. |
|
178 |
||
179 |
The simplest combinator is @{ML I}, which is just the identity function defined as |
|
180 |
*} |
|
181 |
||
182 |
ML{*fun I x = x*} |
|
183 |
||
184 |
text {* Another simple combinator is @{ML K}, defined as *} |
|
185 |
||
186 |
ML{*fun K x = fn _ => x*} |
|
187 |
||
188 |
text {* |
|
189 |
@{ML K} ``wraps'' a function around the argument @{text "x"}. However, this |
|
190 |
function ignores its argument. As a result, @{ML K} defines a constant function |
|
191 |
always returning @{text x}. |
|
192 |
||
193 |
The next combinator is reverse application, @{ML "|>"}, defined as: |
|
194 |
*} |
|
195 |
||
196 |
ML{*fun x |> f = f x*} |
|
197 |
||
198 |
text {* While just syntactic sugar for the usual function application, |
|
199 |
the purpose of this combinator is to implement functions in a |
|
200 |
``waterfall fashion''. Consider for example the function *} |
|
201 |
||
202 |
ML %linenosgray{*fun inc_by_five x = |
|
203 |
x |> (fn x => x + 1) |
|
204 |
|> (fn x => (x, x)) |
|
205 |
|> fst |
|
206 |
|> (fn x => x + 4)*} |
|
207 |
||
208 |
text {* |
|
209 |
which increments its argument @{text x} by 5. It does this by first incrementing |
|
210 |
the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking |
|
211 |
the first component of the pair (Line 4) and finally incrementing the first |
|
212 |
component by 4 (Line 5). This kind of cascading manipulations of values is quite |
|
213 |
common when dealing with theories (for example by adding a definition, followed by |
|
214 |
lemmas and so on). The reverse application allows you to read what happens in |
|
215 |
a top-down manner. This kind of coding should also be familiar, |
|
131 | 216 |
if you used to Haskell's do-notation. Writing the function @{ML inc_by_five} using |
126 | 217 |
the reverse application is much clearer than writing |
218 |
*} |
|
219 |
||
220 |
ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*} |
|
221 |
||
222 |
text {* or *} |
|
223 |
||
224 |
ML{*fun inc_by_five x = |
|
225 |
((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*} |
|
226 |
||
227 |
text {* and typographically more economical than *} |
|
228 |
||
229 |
ML{*fun inc_by_five x = |
|
230 |
let val y1 = x + 1 |
|
231 |
val y2 = (y1, y1) |
|
232 |
val y3 = fst y2 |
|
233 |
val y4 = y3 + 4 |
|
234 |
in y4 end*} |
|
235 |
||
236 |
text {* |
|
237 |
Another reason why the let-bindings in the code above are better to be |
|
238 |
avoided: it is more than easy to get the intermediate values wrong, not to |
|
239 |
mention the nightmares the maintenance of this code causes! |
|
240 |
||
241 |
||
242 |
(FIXME: give a real world example involving theories) |
|
243 |
||
244 |
Similarly, the combinator @{ML "#>"} is the reverse function |
|
245 |
composition. It can be used to define the following function |
|
246 |
*} |
|
247 |
||
248 |
ML{*val inc_by_six = |
|
249 |
(fn x => x + 1) |
|
250 |
#> (fn x => x + 2) |
|
251 |
#> (fn x => x + 3)*} |
|
252 |
||
253 |
text {* |
|
254 |
which is the function composed of first the increment-by-one function and then |
|
255 |
increment-by-two, followed by increment-by-three. Again, the reverse function |
|
256 |
composition allows you to read the code top-down. |
|
257 |
||
258 |
The remaining combinators described in this section add convenience for the |
|
259 |
``waterfall method'' of writing functions. The combinator @{ML tap} allows |
|
260 |
you to get hold of an intermediate result (to do some side-calculations for |
|
261 |
instance). The function |
|
262 |
||
263 |
*} |
|
264 |
||
265 |
ML %linenosgray{*fun inc_by_three x = |
|
266 |
x |> (fn x => x + 1) |
|
267 |
|> tap (fn x => tracing (makestring x)) |
|
268 |
|> (fn x => x + 2)*} |
|
269 |
||
270 |
text {* |
|
271 |
increments the argument first by @{text "1"} and then by @{text "2"}. In the |
|
272 |
middle (Line 3), however, it uses @{ML tap} for printing the ``plus-one'' |
|
273 |
intermediate result inside the tracing buffer. The function @{ML tap} can |
|
274 |
only be used for side-calculations, because any value that is computed |
|
275 |
cannot be merged back into the ``main waterfall''. To do this, you can use |
|
276 |
the next combinator. |
|
277 |
||
278 |
The combinator @{ML "`"} is similar to @{ML tap}, but applies a function to the value |
|
279 |
and returns the result together with the value (as a pair). For example |
|
280 |
the function |
|
281 |
*} |
|
282 |
||
283 |
ML{*fun inc_as_pair x = |
|
284 |
x |> `(fn x => x + 1) |
|
285 |
|> (fn (x, y) => (x, y + 1))*} |
|
286 |
||
287 |
text {* |
|
288 |
takes @{text x} as argument, and then increments @{text x}, but also keeps |
|
289 |
@{text x}. The intermediate result is therefore the pair @{ML "(x + 1, x)" |
|
290 |
for x}. After that, the function increments the right-hand component of the |
|
291 |
pair. So finally the result will be @{ML "(x + 1, x + 1)" for x}. |
|
292 |
||
293 |
The combinators @{ML "|>>"} and @{ML "||>"} are defined for |
|
294 |
functions manipulating pairs. The first applies the function to |
|
295 |
the first component of the pair, defined as |
|
296 |
*} |
|
297 |
||
298 |
ML{*fun (x, y) |>> f = (f x, y)*} |
|
299 |
||
300 |
text {* |
|
301 |
and the second combinator to the second component, defined as |
|
302 |
*} |
|
303 |
||
304 |
ML{*fun (x, y) ||> f = (x, f y)*} |
|
305 |
||
306 |
text {* |
|
307 |
With the combinator @{ML "|->"} you can re-combine the elements from a pair. |
|
308 |
This combinator is defined as |
|
309 |
*} |
|
310 |
||
311 |
ML{*fun (x, y) |-> f = f x y*} |
|
312 |
||
313 |
text {* and can be used to write the following roundabout version |
|
314 |
of the @{text double} function: |
|
315 |
*} |
|
316 |
||
317 |
ML{*fun double x = |
|
318 |
x |> (fn x => (x, x)) |
|
319 |
|-> (fn x => fn y => x + y)*} |
|
320 |
||
321 |
text {* |
|
322 |
Recall that @{ML "|>"} is the reverse function applications. Recall also that the related |
|
323 |
reverse function composition is @{ML "#>"}. In fact all the combinators @{ML "|->"}, |
|
324 |
@{ML "|>>"} and @{ML "||>"} described above have related combinators for function |
|
325 |
composition, namely @{ML "#->"}, @{ML "#>>"} and @{ML "##>"}. Using @{ML "#->"}, |
|
326 |
for example, the function @{text double} can also be written as: |
|
327 |
*} |
|
328 |
||
329 |
ML{*val double = |
|
330 |
(fn x => (x, x)) |
|
331 |
#-> (fn x => fn y => x + y)*} |
|
332 |
||
333 |
text {* |
|
334 |
||
335 |
(FIXME: find a good exercise for combinators) |
|
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
336 |
|
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
337 |
\begin{readmore} |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
338 |
The most frequently used combinator are defined in the files @{ML_file "Pure/library.ML"} |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
339 |
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
|
340 |
contains further information about combinators. |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
341 |
\end{readmore} |
126 | 342 |
|
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
14
diff
changeset
|
343 |
*} |
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
14
diff
changeset
|
344 |
|
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
345 |
|
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
346 |
section {* Antiquotations *} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
347 |
|
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
348 |
text {* |
49 | 349 |
The main advantage of embedding all code in a theory is that the code can |
58 | 350 |
contain references to entities defined on the logical level of Isabelle. By |
351 |
this we mean definitions, theorems, terms and so on. This kind of reference is |
|
352 |
realised with antiquotations. For example, one can print out the name of the current |
|
49 | 353 |
theory by typing |
354 |
||
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
355 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
356 |
@{ML_response [display,gray] "Context.theory_name @{theory}" "\"FirstSteps\""} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
357 |
|
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
358 |
where @{text "@{theory}"} is an antiquotation that is substituted with the |
49 | 359 |
current theory (remember that we assumed we are inside the theory |
89 | 360 |
@{text FirstSteps}). The name of this theory can be extracted using |
49 | 361 |
the function @{ML "Context.theory_name"}. |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
362 |
|
89 | 363 |
Note, however, that antiquotations are statically linked, that is their value is |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
364 |
determined at ``compile-time'', not ``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
|
365 |
*} |
5
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
366 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
367 |
ML{*fun not_current_thyname () = Context.theory_name @{theory} *} |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
368 |
|
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
|
369 |
text {* |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
370 |
|
89 | 371 |
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
|
372 |
different theory. Instead, the code above defines the constant function |
58 | 373 |
that always returns the string @{text [quotes] "FirstSteps"}, no matter where the |
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents:
42
diff
changeset
|
374 |
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
|
375 |
\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
|
376 |
some data structure and return it. Instead, it is literally |
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents:
2
diff
changeset
|
377 |
replaced with the value representing the theory name. |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
378 |
|
132 | 379 |
In a similar way you can use antiquotations to refer to proved theorems: |
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
380 |
@{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
|
381 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
382 |
@{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"} |
75 | 383 |
|
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
384 |
and @{text "@{thms \<dots>}"} for more than one |
132 | 385 |
|
386 |
@{ML_response_fake [display,gray] "@{thms conj_ac}" |
|
387 |
"(?P \<and> ?Q) = (?Q \<and> ?P) |
|
388 |
(?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R) |
|
389 |
((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"} |
|
390 |
||
391 |
You can also refer to the current simpsets. To illustrate this we use the |
|
392 |
function that extracts the theorem names stored in a simpset. |
|
131 | 393 |
*} |
75 | 394 |
|
131 | 395 |
ML{*fun get_thm_names simpset = |
396 |
let |
|
397 |
val ({rules,...}, _) = MetaSimplifier.rep_ss simpset |
|
70
bbb2d5f1d58d
deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
398 |
in |
bbb2d5f1d58d
deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
399 |
map #name (Net.entries rules) |
131 | 400 |
end*} |
54
1783211b3494
tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
401 |
|
131 | 402 |
text {* |
403 |
The function @{ML rep_ss in MetaSimplifier} returns a record containing all |
|
404 |
information about the simpset. The rules of a simpset are stored in a |
|
405 |
\emph{discrimination net} (a data structure for fast indexing). From this |
|
406 |
net you can extract the entries using the function @{ML Net.entries}. |
|
407 |
You can now use @{ML get_thm_names} to obtain all names of theorems of |
|
408 |
the current simpset using the antiquotation @{text "@{simpset}"}.\footnote |
|
409 |
{FIXME: you cannot cleanly match against lists with ommited parts} |
|
81 | 410 |
|
131 | 411 |
@{ML_response_fake [display,gray] |
412 |
"get_thm_names @{simpset}" "[\"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
|
413 |
|
75 | 414 |
\begin{readmore} |
81 | 415 |
The infrastructure for simpsets is implemented in @{ML_file "Pure/meta_simplifier.ML"} |
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
416 |
and @{ML_file "Pure/simplifier.ML"}. Discrimination nets are implemented |
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
417 |
in @{ML_file "Pure/net.ML"}. |
75 | 418 |
\end{readmore} |
419 |
||
420 |
While antiquotations have many applications, they were originally introduced in order |
|
89 | 421 |
to avoid explicit bindings for theorems such as: |
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
|
422 |
*} |
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
|
423 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
424 |
ML{*val allI = thm "allI" *} |
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
|
425 |
|
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
|
426 |
text {* |
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
427 |
These bindings are difficult to maintain and also can be accidentally |
89 | 428 |
overwritten by the user. This often breakes Isabelle |
49 | 429 |
packages. Antiquotations solve this problem, since they are ``linked'' |
89 | 430 |
statically at compile-time. However, this static linkage also limits their |
431 |
usefulness in cases where data needs to be build up dynamically. In the |
|
122 | 432 |
course of this introduction, you will learn more about these antiquotations: |
433 |
they can simplify Isabelle programming since one can directly access all |
|
89 | 434 |
kinds of logical elements from th ML-level. |
49 | 435 |
|
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
436 |
*} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
437 |
|
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
14
diff
changeset
|
438 |
section {* Terms and Types *} |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
439 |
|
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
440 |
text {* |
122 | 441 |
One way to construct terms of Isabelle is by using the antiquotation |
89 | 442 |
\mbox{@{text "@{term \<dots>}"}}. For example: |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
443 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
444 |
@{ML_response [display,gray] |
75 | 445 |
"@{term \"(a::nat) + b = c\"}" |
446 |
"Const (\"op =\", \<dots>) $ |
|
126 | 447 |
(Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"} |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
448 |
|
50 | 449 |
This will show the term @{term "(a::nat) + b = c"}, but printed using the internal |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
450 |
representation of this term. This internal representation corresponds to the |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
451 |
datatype @{ML_type "term"}. |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
452 |
|
34 | 453 |
The internal representation of terms uses the usual de Bruijn index mechanism where bound |
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
454 |
variables are represented by the constructor @{ML Bound}. The index in @{ML Bound} refers to |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
455 |
the number of Abstractions (@{ML Abs}) we have to skip until we hit the @{ML Abs} that |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
456 |
binds the corresponding variable. However, in Isabelle the names of bound variables are |
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
457 |
kept at abstractions for printing purposes, and so should be treated only as comments. |
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
458 |
Application in Isabelle is realised with the term-constructor @{ML $}. |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
459 |
|
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
460 |
\begin{readmore} |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
461 |
Terms are described in detail in \isccite{sec:terms}. Their |
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
462 |
definition and many useful operations are implemented in @{ML_file "Pure/term.ML"}. |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
463 |
\end{readmore} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
464 |
|
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
465 |
Sometimes the internal representation of terms can be surprisingly different |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
466 |
from what you see at the user level, because the layers of |
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
467 |
parsing/type-checking/pretty printing can be quite elaborate. |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
468 |
|
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
469 |
\begin{exercise} |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
470 |
Look at the internal term representation of the following terms, and |
89 | 471 |
find out why they are represented like this: |
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
472 |
|
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
473 |
\begin{itemize} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
474 |
\item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
475 |
\item @{term "\<lambda>(x,y). P y x"} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
476 |
\item @{term "{ [x::int] | x. x \<le> -2 }"} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
477 |
\end{itemize} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
478 |
|
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
479 |
Hint: The third term is already quite big, and the pretty printer |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
480 |
may omit parts of it by default. If you want to see all of it, you |
122 | 481 |
can use the following ML-function to set the printing depth to a higher |
482 |
value: |
|
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
483 |
|
75 | 484 |
@{ML [display,gray] "print_depth 50"} |
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
485 |
\end{exercise} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
486 |
|
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
487 |
The antiquotation @{text "@{prop \<dots>}"} constructs terms of propositional type, |
50 | 488 |
inserting the invisible @{text "Trueprop"}-coercions whenever necessary. |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
66
diff
changeset
|
489 |
Consider for example the pairs |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
490 |
|
126 | 491 |
@{ML_response [display,gray] "(@{term \"P x\"}, @{prop \"P x\"})" |
492 |
"(Free (\"P\", \<dots>) $ Free (\"x\", \<dots>), |
|
493 |
Const (\"Trueprop\", \<dots>) $ (Free (\"P\", \<dots>) $ Free (\"x\", \<dots>)))"} |
|
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
494 |
|
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
parents:
107
diff
changeset
|
495 |
where a coercion is inserted in the second component and |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
496 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
497 |
@{ML_response [display,gray] "(@{term \"P x \<Longrightarrow> Q x\"}, @{prop \"P x \<Longrightarrow> Q x\"})" |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
498 |
"(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
499 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
500 |
where it is not (since it is already constructed by a meta-implication). |
19
34b93dbf8c3c
some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
15
diff
changeset
|
501 |
|
89 | 502 |
Types can be constructed using the antiquotation @{text "@{typ \<dots>}"}. For example: |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
503 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
504 |
@{ML_response_fake [display,gray] "@{typ \"bool \<Rightarrow> nat\"}" "bool \<Rightarrow> nat"} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
34
diff
changeset
|
505 |
|
19
34b93dbf8c3c
some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
15
diff
changeset
|
506 |
\begin{readmore} |
34b93dbf8c3c
some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
15
diff
changeset
|
507 |
Types are described in detail in \isccite{sec:types}. Their |
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
508 |
definition and many useful operations are implemented |
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
509 |
in @{ML_file "Pure/type.ML"}. |
19
34b93dbf8c3c
some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
15
diff
changeset
|
510 |
\end{readmore} |
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
511 |
*} |
19
34b93dbf8c3c
some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
15
diff
changeset
|
512 |
|
34b93dbf8c3c
some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
15
diff
changeset
|
513 |
|
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
14
diff
changeset
|
514 |
section {* Constructing Terms and Types Manually *} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
515 |
|
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
516 |
text {* |
81 | 517 |
While antiquotations are very convenient for constructing terms, they can |
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
|
518 |
only construct fixed terms (remember they are ``linked'' at compile-time). |
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
|
519 |
However, you often need to construct terms dynamically. For example, a |
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
|
520 |
function that returns the implication @{text "\<And>(x::\<tau>). P x \<Longrightarrow> Q x"} taking |
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
|
521 |
@{term P}, @{term Q} and the type @{term "\<tau>"} as arguments can only be |
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
|
522 |
written as: |
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
|
523 |
|
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
|
524 |
*} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
525 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
526 |
ML{*fun make_imp P Q tau = |
131 | 527 |
let |
528 |
val x = Free ("x",tau) |
|
529 |
in |
|
530 |
Logic.all x (Logic.mk_implies (P $ x, Q $ x)) |
|
531 |
end *} |
|
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
|
532 |
|
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
|
533 |
text {* |
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
|
534 |
The reason is that you cannot pass the arguments @{term P}, @{term Q} and |
104 | 535 |
@{term "tau"} into an antiquotation. For example the following does \emph{not} work. |
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
|
536 |
*} |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
537 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
538 |
ML{*fun make_wrong_imp P Q tau = @{prop "\<And>x. P x \<Longrightarrow> Q x"} *} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
539 |
|
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
|
540 |
text {* |
19
34b93dbf8c3c
some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
15
diff
changeset
|
541 |
To see this apply @{text "@{term S}"}, @{text "@{term T}"} and @{text "@{typ nat}"} |
75 | 542 |
to both functions. With @{ML make_imp} we obtain the intended term involving |
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
|
543 |
the given arguments |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
544 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
545 |
@{ML_response [display,gray] "make_imp @{term S} @{term T} @{typ nat}" |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
546 |
"Const \<dots> $ |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
547 |
Abs (\"x\", Type (\"nat\",[]), |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
548 |
Const \<dots> $ (Free (\"S\",\<dots>) $ \<dots>) $ |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
549 |
(Free (\"T\",\<dots>) $ \<dots>))"} |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
66
diff
changeset
|
550 |
|
81 | 551 |
whereas with @{ML make_wrong_imp} we obtain a term involving the @{term "P"} |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
66
diff
changeset
|
552 |
and @{text "Q"} from the antiquotation. |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
66
diff
changeset
|
553 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
554 |
@{ML_response [display,gray] "make_wrong_imp @{term S} @{term T} @{typ nat}" |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
555 |
"Const \<dots> $ |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
556 |
Abs (\"x\", \<dots>, |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
557 |
Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $ |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
558 |
(Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"} |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
559 |
|
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
parents:
107
diff
changeset
|
560 |
Although types of terms can often be inferred, there are many |
104 | 561 |
situations where you need to construct types manually, especially |
562 |
when defining constants. For example the function returning a function |
|
563 |
type is as follows: |
|
49 | 564 |
|
565 |
*} |
|
566 |
||
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
567 |
ML{*fun make_fun_type tau1 tau2 = Type ("fun",[tau1,tau2]) *} |
49 | 568 |
|
104 | 569 |
text {* This can be equally written as: *} |
49 | 570 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
571 |
ML{*fun make_fun_type tau1 tau2 = tau1 --> tau2 *} |
49 | 572 |
|
573 |
text {* |
|
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
574 |
\begin{readmore} |
89 | 575 |
There are many functions in @{ML_file "Pure/term.ML"}, @{ML_file "Pure/logic.ML"} and |
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
|
576 |
@{ML_file "HOL/Tools/hologic.ML"} that make such manual constructions of terms |
49 | 577 |
and types easier.\end{readmore} |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
578 |
|
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
579 |
Have a look at these files and try to solve the following two exercises: |
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
580 |
*} |
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
581 |
|
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
582 |
text {* |
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
583 |
|
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
584 |
\begin{exercise}\label{fun:revsum} |
58 | 585 |
Write a function @{text "rev_sum : term -> term"} that takes a |
122 | 586 |
term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "n"} might be zero) |
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
587 |
and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume |
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
588 |
the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
589 |
associates to the left. Try your function on some examples. |
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
590 |
\end{exercise} |
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
591 |
|
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
14
diff
changeset
|
592 |
\begin{exercise}\label{fun:makesum} |
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
593 |
Write a function which takes two terms representing natural numbers |
75 | 594 |
in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produce the |
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
595 |
number representing their sum. |
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
596 |
\end{exercise} |
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
597 |
|
107
258ce361ba1b
polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents:
104
diff
changeset
|
598 |
A handy function for manipulating terms is @{ML map_types}: it takes a |
258ce361ba1b
polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents:
104
diff
changeset
|
599 |
function and applies it to every type in the term. You can, for example, |
122 | 600 |
change every @{typ nat} in a term into an @{typ int} using the function: |
89 | 601 |
*} |
602 |
||
603 |
ML{*fun nat_to_int t = |
|
604 |
(case t of |
|
605 |
@{typ nat} => @{typ int} |
|
606 |
| Type (s, ts) => Type (s, map nat_to_int ts) |
|
607 |
| _ => t)*} |
|
608 |
||
609 |
text {* |
|
122 | 610 |
An example as follows: |
89 | 611 |
|
612 |
@{ML_response_fake [display,gray] |
|
613 |
"map_types nat_to_int @{term \"a = (1::nat)\"}" |
|
614 |
"Const (\"op =\", \"int \<Rightarrow> int \<Rightarrow> bool\") |
|
615 |
$ Free (\"a\", \"int\") $ Const (\"HOL.one_class.one\", \"int\")"} |
|
616 |
||
11
733614e236a3
tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents:
10
diff
changeset
|
617 |
|
122 | 618 |
There are a few subtle issues with constants. They usually crop up when |
131 | 619 |
pattern matching terms or types, or constructing them. While it is perfectly ok |
122 | 620 |
to write the function @{text is_true} as follows |
621 |
*} |
|
622 |
||
623 |
ML{*fun is_true @{term True} = true |
|
624 |
| is_true _ = false*} |
|
625 |
||
626 |
text {* |
|
627 |
this does not work for picking out @{text "\<forall>"}-quantified terms. Because |
|
628 |
the function |
|
629 |
*} |
|
630 |
||
631 |
ML{*fun is_all (@{term All} $ _) = true |
|
632 |
| is_all _ = false*} |
|
633 |
||
634 |
text {* |
|
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
635 |
will not correctly match the formula @{prop "\<forall>x::nat. P x"}: |
122 | 636 |
|
637 |
@{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "false"} |
|
638 |
||
639 |
The problem is that the @{text "@term"}-antiquotation in the pattern |
|
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
640 |
fixes the type of the constant @{term "All"} to be @{typ "('a \<Rightarrow> bool) \<Rightarrow> bool"} for |
122 | 641 |
an arbitrary, but fixed type @{typ "'a"}. A properly working alternative |
642 |
for this function is |
|
643 |
*} |
|
644 |
||
645 |
ML{*fun is_all (Const ("All", _) $ _) = true |
|
646 |
| is_all _ = false*} |
|
647 |
||
648 |
text {* |
|
649 |
because now |
|
650 |
||
651 |
@{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "true"} |
|
652 |
||
131 | 653 |
matches correctly (the first wildcard in the pattern matches any type). |
122 | 654 |
|
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
655 |
However there is still a problem: consider the similar function that |
131 | 656 |
attempts to pick out @{text "Nil"}-terms: |
122 | 657 |
*} |
658 |
||
659 |
ML{*fun is_nil (Const ("Nil", _)) = true |
|
660 |
| is_nil _ = false *} |
|
661 |
||
662 |
text {* |
|
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
663 |
Unfortunately, also this function does \emph{not} work as expected, since |
122 | 664 |
|
665 |
@{ML_response [display,gray] "is_nil @{term \"Nil\"}" "false"} |
|
666 |
||
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
667 |
The problem is that on the ML-level the name of a constant is more |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
668 |
subtle as you might expect. The function @{ML is_all} worked correctly, |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
669 |
because @{term "All"} is such a fundamental constant, which can be referenced |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
670 |
by @{ML "Const (\"All\", some_type)" for some_type}. However, if you look at |
122 | 671 |
|
672 |
@{ML_response [display,gray] "@{term \"Nil\"}" "Const (\"List.list.Nil\", \<dots>)"} |
|
673 |
||
131 | 674 |
the name of the constant @{text "Nil"} depends on the theory in which the |
128 | 675 |
term constructor is defined (@{text "List"}) and also in which datatype |
676 |
(@{text "list"}). Even worse, some constants have a name involving |
|
677 |
type-classes. Consider for example the constants for @{term "zero"} and |
|
131 | 678 |
\mbox{@{text "(op *)"}}: |
122 | 679 |
|
680 |
@{ML_response [display,gray] "(@{term \"0::nat\"}, @{term \"op *\"})" |
|
126 | 681 |
"(Const (\"HOL.zero_class.zero\", \<dots>), |
122 | 682 |
Const (\"HOL.times_class.times\", \<dots>))"} |
683 |
||
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
684 |
While you could use the complete name, for example |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
685 |
@{ML "Const (\"List.list.Nil\", some_type)" for some_type}, for referring to or |
122 | 686 |
matching against @{text "Nil"}, this would make the code rather brittle. |
687 |
The reason is that the theory and the name of the datatype can easily change. |
|
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
688 |
To make the code more robust, it is better to use the antiquotation |
122 | 689 |
@{text "@{const_name \<dots>}"}. With this antiquotation you can harness the |
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
690 |
variable parts of the constant's name. Therefore a functions for |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
691 |
matching against constants that have a polymorphic type should |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
692 |
be written as follows. |
122 | 693 |
*} |
694 |
||
695 |
ML{*fun is_nil_or_all (Const (@{const_name "Nil"}, _)) = true |
|
696 |
| is_nil_or_all (Const (@{const_name "All"}, _) $ _) = true |
|
697 |
| is_nil_or_all _ = false *} |
|
698 |
||
699 |
text {* |
|
131 | 700 |
Occasional you have to calculate what the ``base'' name of a given |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
701 |
constant is. For this you can use the function @{ML Sign.extern_const} or |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
702 |
@{ML Sign.base_name}. For example: |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
703 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
704 |
@{ML_response [display,gray] "Sign.extern_const @{theory} \"List.list.Nil\"" "\"Nil\""} |
122 | 705 |
|
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
706 |
The difference between both functions is that @{ML extern_const in Sign} returns |
126 | 707 |
the smallest name that is still unique, whereas @{ML base_name in Sign} always |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
708 |
strips off all qualifiers. |
122 | 709 |
|
710 |
\begin{readmore} |
|
711 |
FIXME |
|
712 |
\end{readmore} |
|
713 |
*} |
|
714 |
||
715 |
||
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
716 |
section {* Type-Checking *} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
717 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
718 |
text {* |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
719 |
|
131 | 720 |
You can freely construct and manipulate @{ML_type "term"}s and @{ML_type |
721 |
typ}es, since they are just arbitrary unchecked trees. However, you |
|
722 |
eventually want to see if a term is well-formed, or type-checks, relative to |
|
723 |
a theory. Type-checking is done via the function @{ML cterm_of}, which |
|
724 |
converts a @{ML_type term} into a @{ML_type cterm}, a \emph{certified} |
|
725 |
term. Unlike @{ML_type term}s, which are just trees, @{ML_type "cterm"}s are |
|
726 |
abstract objects that are guaranteed to be type-correct, and they can only |
|
727 |
be constructed via ``official interfaces''. |
|
728 |
||
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
729 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
730 |
Type-checking is always relative to a theory context. For now we use |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
731 |
the @{ML "@{theory}"} antiquotation to get hold of the current theory. |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
732 |
For example you can write: |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
733 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
734 |
@{ML_response_fake [display,gray] "cterm_of @{theory} @{term \"a + b = c\"}" "a + b = c"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
735 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
736 |
This can also be written with an antiquotation: |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
737 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
738 |
@{ML_response_fake [display,gray] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
739 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
740 |
Attempting to obtain the certified term for |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
741 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
742 |
@{ML_response_fake_both [display,gray] "@{cterm \"1 + True\"}" "Type unification failed \<dots>"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
743 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
744 |
yields an error (since the term is not typable). A slightly more elaborate |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
745 |
example that type-checks is: |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
746 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
747 |
@{ML_response_fake [display,gray] |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
748 |
"let |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
749 |
val natT = @{typ \"nat\"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
750 |
val zero = @{term \"0::nat\"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
751 |
in |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
752 |
cterm_of @{theory} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
753 |
(Const (@{const_name plus}, natT --> natT --> natT) $ zero $ zero) |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
754 |
end" "0 + 0"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
755 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
756 |
\begin{exercise} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
757 |
Check that the function defined in Exercise~\ref{fun:revsum} returns a |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
758 |
result that type-checks. |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
759 |
\end{exercise} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
760 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
761 |
Remember that in Isabelle a term contains enough typing information |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
762 |
(constants, free variables and abstractions all have typing |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
763 |
information) so that it is always clear what the type of a term is. |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
764 |
Given a well-typed term, the function @{ML type_of} returns the |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
765 |
type of a term. Consider for example: |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
766 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
767 |
@{ML_response [display,gray] |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
768 |
"type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
769 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
770 |
To calculate the type, this function traverses the whole term and will |
128 | 771 |
detect any typing inconsistency. For examle changing the type of the variable |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
772 |
from @{typ "nat"} to @{typ "int"} will result in the error message: |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
773 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
774 |
@{ML_response_fake [display,gray] |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
775 |
"type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
776 |
"*** Exception- TYPE (\"type_of: type mismatch in application\" \<dots>"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
777 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
778 |
Since the complete traversal might sometimes be too costly and |
131 | 779 |
not necessary, there is also the function @{ML fastype_of}, which |
128 | 780 |
returns the type of a term. |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
781 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
782 |
@{ML_response [display,gray] |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
783 |
"fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
784 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
785 |
However, efficiency is gained on the expense of skiping some tests. You |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
786 |
can see this in the following example |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
787 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
788 |
@{ML_response [display,gray] |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
789 |
"fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" "bool"} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
790 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
791 |
where no error is raised. |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
792 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
793 |
Sometimes it is a bit inconvenient to construct a term with |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
794 |
complete typing annotations, especially in cases where the typing |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
795 |
information is redundant. A short-cut is to use the ``place-holder'' |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
796 |
type @{ML "dummyT"} and then let type-inference figure out the |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
797 |
complete type. An example is as follows: |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
798 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
799 |
@{ML_response_fake [display,gray] |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
800 |
"let |
126 | 801 |
val c = Const (@{const_name \"plus\"}, dummyT) |
802 |
val o = @{term \"1::nat\"} |
|
803 |
val v = Free (\"x\", dummyT) |
|
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
804 |
in |
126 | 805 |
Syntax.check_term @{context} (c $ o $ v) |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
806 |
end" |
126 | 807 |
"Const (\"HOL.plus_class.plus\", \"nat \<Rightarrow> nat \<Rightarrow> nat\") $ |
808 |
Const (\"HOL.one_class.one\", \"nat\") $ Free (\"x\", \"nat\")"} |
|
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
809 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
810 |
Instead of giving explicitly the type for the constant @{text "plus"} and the free |
128 | 811 |
variable @{text "x"}, the type-inference filled in the missing information. |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
812 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
813 |
\begin{readmore} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
814 |
See @{ML_file "Pure/Syntax/syntax.ML"} where more functions about reading, |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
815 |
checking and pretty-printing of terms are defined. |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
816 |
\end{readmore} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
817 |
*} |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
818 |
|
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
819 |
|
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
820 |
section {* Theorems *} |
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
821 |
|
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
822 |
text {* |
50 | 823 |
Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} |
122 | 824 |
that can only be build by going through interfaces. As a consequence, every proof |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
825 |
in Isabelle is correct by construction. This follows the tradition of the LCF approach |
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
123
diff
changeset
|
826 |
\cite{GordonMilnerWadsworth79}. |
107
258ce361ba1b
polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents:
104
diff
changeset
|
827 |
|
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
828 |
|
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
829 |
To see theorems in ``action'', let us give a proof on the ML-level for the following |
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
830 |
statement: |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
831 |
*} |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
832 |
|
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
833 |
lemma |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
834 |
assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x" |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
835 |
and assm\<^isub>2: "P t" |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
836 |
shows "Q t" (*<*)oops(*>*) |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
837 |
|
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
838 |
text {* |
78
ef778679d3e0
added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents:
75
diff
changeset
|
839 |
The corresponding ML-code is as follows:\footnote{Note that @{text "|>"} is reverse |
75 | 840 |
application. See Section~\ref{sec:combinators}.} |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
841 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
71
diff
changeset
|
842 |
@{ML_response_fake [display,gray] |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
843 |
"let |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
844 |
val thy = @{theory} |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
845 |
|
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
846 |
val assm1 = cterm_of thy @{prop \"\<And>(x::nat). P x \<Longrightarrow> Q x\"} |
49 | 847 |
val assm2 = cterm_of thy @{prop \"(P::nat\<Rightarrow>bool) t\"} |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
848 |
|
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
849 |
val Pt_implies_Qt = |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
850 |
assume assm1 |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
851 |
|> forall_elim (cterm_of thy @{term \"t::nat\"}); |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
852 |
|
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
853 |
val Qt = implies_elim Pt_implies_Qt (assume assm2); |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
854 |
in |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
855 |
Qt |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
856 |
|> implies_intr assm2 |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
857 |
|> implies_intr assm1 |
48
609f9ef73494
fixed FIXME's in fake responses
Christian Urban <urbanc@in.tum.de>
parents:
47
diff
changeset
|
858 |
end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
859 |
|
21
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
860 |
This code-snippet constructs the following proof: |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
861 |
|
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
862 |
\[ |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
863 |
\infer[(@{text "\<Longrightarrow>"}$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
864 |
{\infer[(@{text "\<Longrightarrow>"}$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
865 |
{\infer[(@{text "\<Longrightarrow>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
866 |
{\infer[(@{text "\<And>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
867 |
{\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
868 |
& |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
869 |
\infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
870 |
} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
871 |
} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
872 |
} |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
873 |
\] |
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
874 |
|
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
|
875 |
However, while we obtained a theorem as result, this theorem is not |
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
|
876 |
yet stored in Isabelle's theorem database. So it cannot be referenced later |
128 | 877 |
on. How to store theorems will be explained in Section~\ref{sec:storing}. |
21
2356e5c70d98
added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents:
20
diff
changeset
|
878 |
|
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
879 |
\begin{readmore} |
50 | 880 |
For the functions @{text "assume"}, @{text "forall_elim"} etc |
13
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
881 |
see \isccite{sec:thms}. The basic functions for theorems are defined in |
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
882 |
@{ML_file "Pure/thm.ML"}. |
2b07da8b310d
polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents:
12
diff
changeset
|
883 |
\end{readmore} |
12
2f1736cb8f26
various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents:
11
diff
changeset
|
884 |
|
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
885 |
(FIXME: how to add case-names to goal states - maybe in the next section) |
10
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
886 |
*} |
df09e49b19bf
many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents:
6
diff
changeset
|
887 |
|
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
888 |
section {* Theorem Attributes *} |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
889 |
|
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
890 |
text {* |
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
891 |
Theorem attributes are @{text "[simp]"}, @{text "[OF \<dots>]"}, @{text |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
892 |
"[symmetric]"} and so on. Such attributes are \emph{neither} tags \emph{or} flags |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
893 |
annotated to theorems, but functions that do further processing once the |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
894 |
theorems are proven. In particular, it is not possible to find out |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
895 |
what are all theorems that have an given attribute in common, unless of course |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
896 |
the function behind the attribute stores the theorems in a retrivable |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
897 |
datastructure. This can be easily done by the recipe described in |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
898 |
\ref{rec:named}. |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
899 |
|
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
900 |
If you want to print out all currently known attributes a theorem |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
901 |
can have, you can use the function: |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
902 |
|
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
903 |
@{ML_response_fake [display,gray] "Attrib.print_attributes @{theory}" |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
904 |
"COMP: direct composition with rules (no lifting) |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
905 |
HOL.dest: declaration of Classical destruction rule |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
906 |
HOL.elim: declaration of Classical elimination rule |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
907 |
\<dots>"} |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
908 |
|
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
909 |
*} |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
910 |
|
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
911 |
ML{*val my_symmetric = Thm.rule_attribute (fn _ => fn thm => thm RS @{thm sym})*} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
912 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
913 |
text {* |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
914 |
the setup |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
915 |
*} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
916 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
917 |
setup {* |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
918 |
Attrib.add_attributes |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
919 |
[("my_sym", Attrib.no_args my_symmetric, "applying the sym rule")] |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
920 |
*} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
921 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
922 |
lemma test: "1 = Suc 0" by simp |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
923 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
924 |
text {* |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
925 |
@{thm test[my_sym]} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
926 |
*} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
927 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
928 |
text {* |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
929 |
What are: |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
930 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
931 |
@{text "rule_attribute"} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
932 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
933 |
@{text "declaration_attribute"} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
934 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
935 |
@{text "theory_attributes"} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
936 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
937 |
@{text "proof_attributes"} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
938 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
939 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
940 |
\begin{readmore} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
941 |
FIXME: @{ML_file "Pure/more_thm.ML"} @{ML_file "Pure/Isar/attrib.ML"} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
942 |
\end{readmore} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
943 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
944 |
|
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
945 |
*} |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
946 |
|
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
947 |
|
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
948 |
section {* Theories and Local Theories *} |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
949 |
|
126 | 950 |
text {* |
951 |
(FIXME: expand) |
|
952 |
||
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
953 |
(FIXME: explain \isacommand{setup}) |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
954 |
|
126 | 955 |
There are theories, proof contexts and local theories (in this order, if you |
956 |
want to order them). |
|
957 |
||
958 |
In contrast to an ordinary theory, which simply consists of a type |
|
959 |
signature, as well as tables for constants, axioms and theorems, a local |
|
960 |
theory also contains additional context information, such as locally fixed |
|
961 |
variables and local assumptions that may be used by the package. The type |
|
962 |
@{ML_type local_theory} is identical to the type of \emph{proof contexts} |
|
963 |
@{ML_type "Proof.context"}, although not every proof context constitutes a |
|
964 |
valid local theory. |
|
965 |
||
966 |
*} |
|
967 |
||
968 |
||
969 |
||
128 | 970 |
section {* Storing Theorems\label{sec:storing} *} |
123
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
971 |
|
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
972 |
text {* @{ML PureThy.add_thms_dynamic} *} |
460bc2ee14e9
some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
973 |
|
100 | 974 |
|
75 | 975 |
|
126 | 976 |
(* FIXME: some code below *) |
89 | 977 |
|
978 |
(*<*) |
|
979 |
setup {* |
|
980 |
Sign.add_consts_i [("bar", @{typ "nat"},NoSyn)] |
|
981 |
*} |
|
982 |
||
983 |
lemma "bar = (1::nat)" |
|
984 |
oops |
|
985 |
||
986 |
setup {* |
|
987 |
Sign.add_consts_i [("foo", @{typ "nat"},NoSyn)] |
|
988 |
#> PureThy.add_defs false [((Binding.name "foo_def", |
|
989 |
Logic.mk_equals (Const ("FirstSteps.foo", @{typ "nat"}), @{term "1::nat"})), [])] |
|
990 |
#> snd |
|
991 |
*} |
|
992 |
||
993 |
lemma "foo = (1::nat)" |
|
994 |
apply(simp add: foo_def) |
|
995 |
done |
|
996 |
||
997 |
thm foo_def |
|
998 |
(*>*) |
|
999 |
||
92 | 1000 |
section {* Misc *} |
1001 |
||
1002 |
ML {*DatatypePackage.get_datatype @{theory} "List.list"*} |
|
1003 |
||
2
978a3c2ed7ce
split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1004 |
end |