2
|
1 |
theory FirstSteps
|
|
2 |
imports Main
|
|
3 |
uses "antiquote_setup.ML"
|
15
|
4 |
"antiquote_setup_plus.ML"
|
2
|
5 |
begin
|
|
6 |
|
|
7 |
|
|
8 |
chapter {* First Steps *}
|
|
9 |
|
5
|
10 |
|
2
|
11 |
text {*
|
10
|
12 |
Isabelle programming is done in Standard ML.
|
6
|
13 |
Just like lemmas and proofs, code in Isabelle is part of a
|
|
14 |
theory. If you want to follow the code written in this chapter, we
|
|
15 |
assume you are working inside the theory defined by
|
2
|
16 |
|
6
|
17 |
\begin{center}
|
5
|
18 |
\begin{tabular}{@ {}l}
|
|
19 |
\isacommand{theory} CookBook\\
|
|
20 |
\isacommand{imports} Main\\
|
|
21 |
\isacommand{begin}\\
|
6
|
22 |
\ldots
|
5
|
23 |
\end{tabular}
|
6
|
24 |
\end{center}
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
25 |
*}
|
5
|
26 |
|
20
|
27 |
section {* Including ML-Code *}
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
28 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
29 |
text {*
|
5
|
30 |
The easiest and quickest way to include code in a theory is
|
|
31 |
by using the \isacommand{ML} command. For example
|
2
|
32 |
*}
|
|
33 |
|
|
34 |
ML {*
|
|
35 |
3 + 4
|
|
36 |
*}
|
|
37 |
|
|
38 |
text {*
|
19
|
39 |
Expressions inside \isacommand{ML} commands are immediately evaluated,
|
6
|
40 |
like ``normal'' Isabelle proof scripts, by using the advance and undo buttons of
|
5
|
41 |
your Isabelle environment. The code inside the \isacommand{ML} command
|
15
|
42 |
can also contain value and function bindings. However on such \isacommand{ML}
|
|
43 |
commands the undo operation behaves slightly counter-intuitive, because
|
|
44 |
if you define
|
10
|
45 |
*}
|
|
46 |
|
|
47 |
ML {*
|
|
48 |
val foo = true
|
2
|
49 |
*}
|
|
50 |
|
10
|
51 |
text {*
|
|
52 |
then Isabelle's undo operation has no effect on the definition of
|
|
53 |
@{ML "foo"}.
|
|
54 |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
55 |
Once a portion of code is relatively stable, one usually wants to
|
15
|
56 |
export it to a separate ML-file. Such files can then be included in a
|
|
57 |
theory by using \isacommand{uses} 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>
diff
changeset
|
58 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
59 |
\begin{center}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
60 |
\begin{tabular}{@ {}l}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
61 |
\isacommand{theory} CookBook\\
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
62 |
\isacommand{imports} Main\\
|
15
|
63 |
\isacommand{uses} @{ML_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>
diff
changeset
|
64 |
\isacommand{begin}\\
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
65 |
\ldots
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
66 |
\end{tabular}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
67 |
\end{center}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
68 |
*}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
69 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
70 |
section {* Debugging and Printing *}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
71 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
72 |
text {*
|
13
|
73 |
|
11
|
74 |
During developments you might find it necessary to quickly inspect some data
|
10
|
75 |
in your code. This can be done in a ``quick-and-dirty'' fashion using
|
12
|
76 |
the function @{ML "warning"}. For example
|
10
|
77 |
*}
|
|
78 |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
79 |
ML {* warning "any string" *}
|
10
|
80 |
|
|
81 |
text {*
|
12
|
82 |
will print out @{ML "\"any string\""} inside the response buffer of Isabelle.
|
19
|
83 |
If you develop under PolyML, then there is a convenient, though again
|
|
84 |
``quick-and-dirty'', method for converting values into strings,
|
|
85 |
for example:
|
10
|
86 |
*}
|
|
87 |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
88 |
ML {* warning (makestring 1) *}
|
12
|
89 |
|
|
90 |
text {*
|
19
|
91 |
However this only works if the type of what is converted is monomorphic and not
|
12
|
92 |
a function.
|
|
93 |
*}
|
|
94 |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
95 |
text {*
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
96 |
The funtion warning should only be used for testing purposes, because
|
19
|
97 |
any output this funtion generates will be overwritten, as soon as an error
|
|
98 |
is raised.
|
|
99 |
Therefore for printing anything more serious and elaborate, the function @{ML tracing}
|
|
100 |
should be used. This function writes all output into a separate buffer.
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
101 |
*}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
102 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
103 |
ML {* tracing "foo" *}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
104 |
|
15
|
105 |
text {*
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
106 |
|
15
|
107 |
It is also possible to redirect the channel where the @{ML_text "foo"} is
|
|
108 |
printed to a separate file, e.g. to prevent Proof General from choking on massive
|
|
109 |
amounts of trace output. This rediretion can be achieved using the code
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
110 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
111 |
*}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
112 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
113 |
ML {*
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
114 |
val strip_specials =
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
115 |
let
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
116 |
fun strip ("\^A" :: _ :: cs) = strip cs
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
117 |
| strip (c :: cs) = c :: strip cs
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
118 |
| strip [] = [];
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
119 |
in implode o strip o explode end;
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
120 |
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
121 |
fun redirect_tracing stream =
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
122 |
Output.tracing_fn := (fn s =>
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
123 |
(TextIO.output (stream, (strip_specials s));
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
124 |
TextIO.output (stream, "\n");
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
125 |
TextIO.flushOut stream));
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
126 |
*}
|
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
127 |
|
15
|
128 |
text {*
|
19
|
129 |
Calling @{ML_text "redirect_tracing"} with @{ML_text "(TextIO.openOut \"foo.bar\")"}
|
15
|
130 |
will cause that all tracing information is printed into the file @{ML_text "foo.bar"}.
|
|
131 |
|
|
132 |
*}
|
|
133 |
|
10
|
134 |
|
2
|
135 |
section {* Antiquotations *}
|
|
136 |
|
|
137 |
text {*
|
10
|
138 |
The main advantage of embedding all code
|
|
139 |
in a theory is that the code can contain references to entities defined
|
|
140 |
on the logical level of Isabelle. This is done using antiquotations.
|
5
|
141 |
For example, one can print out the name of
|
|
142 |
the current theory by typing
|
2
|
143 |
*}
|
|
144 |
|
|
145 |
ML {* Context.theory_name @{theory} *}
|
|
146 |
|
5
|
147 |
text {*
|
|
148 |
where @{text "@{theory}"} is an antiquotation that is substituted with the
|
|
149 |
current theory (remember that we assumed we are inside the theory CookBook).
|
13
|
150 |
The name of this theory can be extracted using the function @{ML "Context.theory_name"}.
|
5
|
151 |
So the code above returns the string @{ML "\"CookBook\""}.
|
|
152 |
|
10
|
153 |
Note, however, that antiquotations are statically scoped, that is the value is
|
12
|
154 |
determined at ``compile-time'', not ``run-time''. For example the function
|
5
|
155 |
|
2
|
156 |
*}
|
|
157 |
|
|
158 |
ML {*
|
19
|
159 |
fun not_current_thyname () = Context.theory_name @{theory}
|
2
|
160 |
*}
|
|
161 |
|
|
162 |
text {*
|
5
|
163 |
does \emph{not} return the name of the current theory, if it is run in a
|
|
164 |
different theory. Instead, the code above defines the constant function
|
|
165 |
that always returns the string @{ML "\"CookBook\""}, no matter where the
|
|
166 |
function is called. Operationally speaking, @{text "@{theory}"} is
|
|
167 |
\emph{not} replaced with code that will look up the current theory in
|
|
168 |
some data structure and return it. Instead, it is literally
|
|
169 |
replaced with the value representing the theory name.
|
2
|
170 |
|
21
|
171 |
In a similar way you can use antiquotations to refer to theorems or simpsets:
|
10
|
172 |
*}
|
|
173 |
|
|
174 |
ML {* @{thm allI} *}
|
21
|
175 |
ML {* @{simpset} *}
|
10
|
176 |
|
|
177 |
text {*
|
5
|
178 |
In the course of this introduction, we will learn more about
|
13
|
179 |
these antiquotations: they greatly simplify Isabelle programming since one
|
2
|
180 |
can directly access all kinds of logical elements from ML.
|
12
|
181 |
|
2
|
182 |
*}
|
|
183 |
|
15
|
184 |
section {* Terms and Types *}
|
2
|
185 |
|
|
186 |
text {*
|
11
|
187 |
One way to construct terms of Isabelle on the ML-level is by using the antiquotation
|
|
188 |
@{text "@{term \<dots>}"}:
|
2
|
189 |
*}
|
|
190 |
|
|
191 |
ML {* @{term "(a::nat) + b = c"} *}
|
|
192 |
|
|
193 |
text {*
|
10
|
194 |
This will show the term @{term "(a::nat) + b = c"}, but printed out using the internal
|
13
|
195 |
representation of this term. This internal representation corresponds to the
|
|
196 |
datatype @{ML_text "term"}.
|
2
|
197 |
|
12
|
198 |
The internal representation of terms uses the usual de-Bruijn index mechanism where bound
|
11
|
199 |
variables are represented by the constructor @{ML Bound}. The index in @{ML Bound} refers to
|
10
|
200 |
the number of Abstractions (@{ML Abs}) we have to skip until we hit the @{ML Abs} that
|
12
|
201 |
binds the corresponding variable. However, in Isabelle the names of bound variables are
|
11
|
202 |
kept at abstractions for printing purposes, and so should be treated only as comments.
|
10
|
203 |
|
2
|
204 |
\begin{readmore}
|
13
|
205 |
Terms are described in detail in \isccite{sec:terms}. Their
|
2
|
206 |
definition and many useful operations can be found in @{ML_file "Pure/term.ML"}.
|
|
207 |
\end{readmore}
|
|
208 |
|
13
|
209 |
Sometimes the internal representation of terms can be surprisingly different
|
12
|
210 |
from what you see at the user level, because the layers of
|
10
|
211 |
parsing/type checking/pretty printing can be quite elaborate.
|
13
|
212 |
*}
|
2
|
213 |
|
13
|
214 |
text {*
|
10
|
215 |
\begin{exercise}
|
2
|
216 |
Look at the internal term representation of the following terms, and
|
|
217 |
find out why they are represented like this.
|
|
218 |
|
|
219 |
\begin{itemize}
|
|
220 |
\item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}
|
|
221 |
\item @{term "\<lambda>(x,y). P y x"}
|
|
222 |
\item @{term "{ [x::int] | x. x \<le> -2 }"}
|
|
223 |
\end{itemize}
|
|
224 |
|
|
225 |
Hint: The third term is already quite big, and the pretty printer
|
|
226 |
may omit parts of it by default. If you want to see all of it, you
|
13
|
227 |
can use the following ML funtion to set the limit to a value high
|
|
228 |
enough:
|
10
|
229 |
\end{exercise}
|
13
|
230 |
*}
|
15
|
231 |
ML {* print_depth 50 *}
|
12
|
232 |
|
13
|
233 |
text {*
|
|
234 |
|
|
235 |
The antiquotation @{text "@{prop \<dots>}"} constructs terms of propositional type,
|
|
236 |
inserting the invisible @{text "Trueprop"} coercions whenever necessary.
|
12
|
237 |
Consider for example
|
|
238 |
|
2
|
239 |
*}
|
|
240 |
|
13
|
241 |
ML {* @{term "P x"} ; @{prop "P x"} *}
|
12
|
242 |
|
19
|
243 |
text {* which inserts the coercion in the latter case and *}
|
12
|
244 |
|
|
245 |
|
13
|
246 |
ML {* @{term "P x \<Longrightarrow> Q x"} ; @{prop "P x \<Longrightarrow> Q x"} *}
|
12
|
247 |
|
19
|
248 |
text {*
|
|
249 |
which does not.
|
|
250 |
|
|
251 |
Types can be constructed using the antiquotation @{text "@{typ \<dots>}"}. For example
|
|
252 |
|
|
253 |
*}
|
|
254 |
|
|
255 |
ML {* @{typ "bool \<Rightarrow> nat"} *}
|
12
|
256 |
|
19
|
257 |
text {*
|
|
258 |
(FIXME: Unlike the term antiquotation, @{text "@{typ \<dots>}"} does not print the
|
|
259 |
internal representation. Is there a reason for this, that needs to be explained
|
|
260 |
here?)
|
|
261 |
|
|
262 |
\begin{readmore}
|
|
263 |
Types are described in detail in \isccite{sec:types}. Their
|
|
264 |
definition and many useful operations can be found in @{ML_file "Pure/type.ML"}.
|
|
265 |
\end{readmore}
|
|
266 |
|
|
267 |
*}
|
|
268 |
|
|
269 |
|
|
270 |
|
15
|
271 |
|
|
272 |
section {* Constructing Terms and Types Manually *}
|
12
|
273 |
|
|
274 |
text {*
|
|
275 |
|
19
|
276 |
While antiquotations are very convenient for constructing terms and types,
|
15
|
277 |
they can only construct fixed terms. Unfortunately, one often needs to construct them
|
13
|
278 |
dynamically. For example, a function that returns the implication
|
19
|
279 |
@{text "\<And>(x::\<tau>). P x \<Longrightarrow> Q x"} taking @{term P}, @{term Q} and the typ @{term "\<tau>"}
|
|
280 |
as arguments can only be written as
|
12
|
281 |
*}
|
|
282 |
|
|
283 |
|
2
|
284 |
ML {*
|
19
|
285 |
fun make_imp P Q tau =
|
12
|
286 |
let
|
19
|
287 |
val x = Free ("x",tau)
|
12
|
288 |
in Logic.all x (Logic.mk_implies (HOLogic.mk_Trueprop (P $ x),
|
|
289 |
HOLogic.mk_Trueprop (Q $ x)))
|
|
290 |
end
|
2
|
291 |
*}
|
|
292 |
|
|
293 |
text {*
|
12
|
294 |
|
19
|
295 |
The reason is that one cannot pass the arguments @{term P}, @{term Q} and
|
20
|
296 |
@{term "tau"} into an antiquotation. For example the following does not work.
|
13
|
297 |
*}
|
|
298 |
|
|
299 |
ML {*
|
19
|
300 |
fun make_wrong_imp P Q tau = @{prop "\<And>x. P x \<Longrightarrow> Q x"}
|
12
|
301 |
*}
|
|
302 |
|
|
303 |
text {*
|
|
304 |
|
19
|
305 |
To see this apply @{text "@{term S}"}, @{text "@{term T}"} and @{text "@{typ nat}"}
|
|
306 |
to both functions.
|
|
307 |
|
|
308 |
One tricky point in constructing terms by hand is to obtain the
|
|
309 |
fully qualified name for constants. For example the names for @{text "zero"} or
|
|
310 |
@{text "+"} are more complex than one first expects, namely
|
15
|
311 |
|
|
312 |
\begin{center}
|
|
313 |
@{ML_text "HOL.zero_class.zero"} and @{ML_text "HOL.plus_class.plus"}.
|
|
314 |
\end{center}
|
|
315 |
|
|
316 |
The extra prefixes @{ML_text zero_class} and @{ML_text plus_class} are present because
|
|
317 |
these constants are defined within type classes; the prefix @{text "HOL"} indicates in
|
|
318 |
which theory they are defined. Guessing such internal names can sometimes be quite hard.
|
|
319 |
Therefore Isabellle provides the antiquotation @{text "@{const_name \<dots>}"} which does the
|
|
320 |
expansion automatically, for example:
|
10
|
321 |
*}
|
19
|
322 |
|
|
323 |
text {*
|
10
|
324 |
|
19
|
325 |
(FIXME: Is it useful to explain @{text "@{const_syntax}"}?)
|
11
|
326 |
|
20
|
327 |
(FIXME: how to construct types manually)
|
|
328 |
|
13
|
329 |
\begin{readmore}
|
|
330 |
There are many functions in @{ML_file "Pure/logic.ML"} and
|
12
|
331 |
@{ML_file "HOL/hologic.ML"} that make such manual constructions of terms
|
13
|
332 |
easier.\end{readmore}
|
|
333 |
|
|
334 |
Have a look at these files and try to solve the following two exercises:
|
11
|
335 |
|
|
336 |
*}
|
|
337 |
|
|
338 |
text {*
|
|
339 |
|
13
|
340 |
\begin{exercise}\label{fun:revsum}
|
11
|
341 |
Write a function @{ML_text "rev_sum : term -> term"} that takes a
|
|
342 |
term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "i"} might be zero)
|
|
343 |
and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume
|
|
344 |
the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"}
|
13
|
345 |
associates to the left. Try your function on some examples.
|
11
|
346 |
\end{exercise}
|
|
347 |
|
15
|
348 |
\begin{exercise}\label{fun:makesum}
|
11
|
349 |
Write a function which takes two terms representing natural numbers
|
|
350 |
in unary (like @{term "Suc (Suc (Suc 0))"}), and produce the unary
|
|
351 |
number representing their sum.
|
|
352 |
\end{exercise}
|
|
353 |
|
|
354 |
*}
|
|
355 |
|
10
|
356 |
|
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
357 |
section {* Type Checking *}
|
10
|
358 |
|
|
359 |
text {*
|
13
|
360 |
|
10
|
361 |
We can freely construct and manipulate terms, since they are just
|
|
362 |
arbitrary unchecked trees. However, we eventually want to see if a
|
13
|
363 |
term is well-formed, or type checks, relative to a theory.
|
10
|
364 |
Type checking is done via the function @{ML cterm_of}, which turns
|
|
365 |
a @{ML_type term} into a @{ML_type cterm}, a \emph{certified} term.
|
|
366 |
Unlike @{ML_type term}s, which are just trees, @{ML_type
|
|
367 |
"cterm"}s are abstract objects that are guaranteed to be
|
20
|
368 |
type-correct, and that can only be constructed via the official
|
10
|
369 |
interfaces.
|
2
|
370 |
|
10
|
371 |
Type checking is always relative to a theory context. For now we can use
|
|
372 |
the @{ML "@{theory}"} antiquotation to get hold of the current theory.
|
|
373 |
For example we can write:
|
2
|
374 |
*}
|
|
375 |
|
10
|
376 |
ML {* cterm_of @{theory} @{term "(a::nat) + b = c"} *}
|
|
377 |
|
20
|
378 |
text {* and *}
|
|
379 |
|
12
|
380 |
ML {*
|
|
381 |
let
|
|
382 |
val natT = @{typ "nat"}
|
|
383 |
val zero = @{term "0::nat"}
|
|
384 |
in
|
|
385 |
cterm_of @{theory}
|
|
386 |
(Const (@{const_name plus}, natT --> natT --> natT)
|
|
387 |
$ zero $ zero)
|
|
388 |
end
|
|
389 |
*}
|
|
390 |
|
13
|
391 |
text {*
|
|
392 |
|
|
393 |
\begin{exercise}
|
|
394 |
Check that the function defined in Exercise~\ref{fun:revsum} returns a
|
|
395 |
result that type checks.
|
|
396 |
\end{exercise}
|
|
397 |
|
|
398 |
*}
|
|
399 |
|
2
|
400 |
section {* Theorems *}
|
|
401 |
|
|
402 |
text {*
|
|
403 |
Just like @{ML_type cterm}s, theorems (of type @{ML_type thm}) are
|
|
404 |
abstract objects that can only be built by going through the kernel
|
13
|
405 |
interfaces, which means that all your proofs will be checked.
|
2
|
406 |
|
13
|
407 |
To see theorems in ``action'', let us give a proof for the following statement
|
10
|
408 |
*}
|
|
409 |
|
|
410 |
lemma
|
|
411 |
assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x"
|
|
412 |
and assm\<^isub>2: "P t"
|
13
|
413 |
shows "Q t" (*<*)oops(*>*)
|
10
|
414 |
|
|
415 |
text {*
|
21
|
416 |
on the ML level:\footnote{Note that @{text "|>"} is reverse
|
2
|
417 |
application. This combinator, and several variants are defined in
|
13
|
418 |
@{ML_file "Pure/General/basics.ML"}.}
|
10
|
419 |
|
2
|
420 |
*}
|
|
421 |
|
10
|
422 |
ML {*
|
|
423 |
let
|
|
424 |
val thy = @{theory}
|
|
425 |
|
|
426 |
val assm1 = cterm_of thy @{prop "\<And>(x::nat). P x \<Longrightarrow> Q x"}
|
|
427 |
val assm2 = cterm_of thy @{prop "((P::nat\<Rightarrow>bool) t)"}
|
|
428 |
|
|
429 |
val Pt_implies_Qt =
|
|
430 |
assume assm1
|
|
431 |
|> forall_elim (cterm_of thy @{term "t::nat"});
|
|
432 |
|
|
433 |
val Qt = implies_elim Pt_implies_Qt (assume assm2);
|
|
434 |
in
|
|
435 |
|
|
436 |
Qt
|
|
437 |
|> implies_intr assm2
|
|
438 |
|> implies_intr assm1
|
|
439 |
end
|
|
440 |
*}
|
|
441 |
|
|
442 |
text {*
|
12
|
443 |
|
21
|
444 |
This code-snippet constructs the following proof:
|
|
445 |
|
|
446 |
\[
|
|
447 |
\infer[(@{text "\<Longrightarrow>"}$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}}
|
|
448 |
{\infer[(@{text "\<Longrightarrow>"}$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
|
|
449 |
{\infer[(@{text "\<Longrightarrow>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}}
|
|
450 |
{\infer[(@{text "\<And>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
|
|
451 |
{\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}}
|
|
452 |
&
|
|
453 |
\infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{}
|
|
454 |
}
|
|
455 |
}
|
|
456 |
}
|
|
457 |
\]
|
|
458 |
|
|
459 |
|
13
|
460 |
\begin{readmore}
|
|
461 |
For how the functions @{text "assume"}, @{text "forall_elim"} etc work
|
|
462 |
see \isccite{sec:thms}. The basic functions for theorems are defined in
|
|
463 |
@{ML_file "Pure/thm.ML"}.
|
|
464 |
\end{readmore}
|
12
|
465 |
|
10
|
466 |
*}
|
|
467 |
|
|
468 |
|
12
|
469 |
section {* Tactical Reasoning *}
|
2
|
470 |
|
|
471 |
text {*
|
13
|
472 |
The goal-oriented tactical style reasoning of the ML level is similar
|
|
473 |
to the @{text apply}-style at the user level, i.e.~the reasoning is centred
|
|
474 |
around a \emph{goal}, which is modified in a sequence of proof steps
|
|
475 |
until it is solved.
|
2
|
476 |
|
|
477 |
A goal (or goal state) is a special @{ML_type thm}, which by
|
12
|
478 |
convention is an implication of the form:
|
|
479 |
|
2
|
480 |
@{text[display] "A\<^isub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^isub>n \<Longrightarrow> #(C)"}
|
12
|
481 |
|
13
|
482 |
where @{term C} is the goal to be proved and the @{term "A\<^isub>i"} are the open subgoals.
|
|
483 |
Since the goal @{term C} can potentially be an implication, there is a
|
12
|
484 |
@{text "#"} wrapped around it, which prevents that premises are
|
2
|
485 |
misinterpreted as open subgoals. The protection @{text "# :: prop \<Rightarrow>
|
12
|
486 |
prop"} is just the identity function and used as a syntactic marker.
|
13
|
487 |
|
21
|
488 |
(FIXME: maybe show how this is printed on the screen)
|
|
489 |
|
13
|
490 |
\begin{readmore}
|
|
491 |
For more on goals see \isccite{sec:tactical-goals}.
|
|
492 |
\end{readmore}
|
2
|
493 |
|
12
|
494 |
Tactics are functions that map a goal state to a (lazy)
|
2
|
495 |
sequence of successor states, hence the type of a tactic is
|
|
496 |
@{ML_type[display] "thm -> thm Seq.seq"}
|
12
|
497 |
|
|
498 |
\begin{readmore}
|
2
|
499 |
See @{ML_file "Pure/General/seq.ML"} for the implementation of lazy
|
13
|
500 |
sequences. However in day-to-day Isabelle programming, one rarely
|
|
501 |
constructs sequences explicitly, but uses the predefined tactic
|
|
502 |
combinators (tacticals) instead (see @{ML_file "Pure/tctical.ML"}).
|
|
503 |
(FIXME: Pointer to the old reference manual)
|
12
|
504 |
\end{readmore}
|
2
|
505 |
|
13
|
506 |
While tatics can operate on the subgoals (the @{text "A\<^isub>i"} above), they
|
|
507 |
are expected to leave the conclusion @{term C} intact, with the
|
|
508 |
exception of possibly instantiating schematic variables.
|
12
|
509 |
|
13
|
510 |
To see how tactics work, let us transcribe a simple @{text apply}-style
|
|
511 |
proof from the tutorial \cite{isa-tutorial} into ML:
|
2
|
512 |
*}
|
|
513 |
|
|
514 |
lemma disj_swap: "P \<or> Q \<Longrightarrow> Q \<or> P"
|
|
515 |
apply (erule disjE)
|
|
516 |
apply (rule disjI2)
|
|
517 |
apply assumption
|
|
518 |
apply (rule disjI1)
|
|
519 |
apply assumption
|
|
520 |
done
|
|
521 |
|
12
|
522 |
|
|
523 |
text {*
|
21
|
524 |
To start the proof, the function @{ML "Goal.prove"}~@{text "ctxt xs As C tac"} sets
|
|
525 |
up a goal state for proving the goal @{text C} under the assumptions @{text As}
|
|
526 |
(empty in the proof at hand)
|
|
527 |
with the variables @{text xs} that will be generalised once the
|
|
528 |
goal is proved. The @{text "tac"} is the tactic which proves the goal and which
|
|
529 |
can make use of the local assumptions.
|
12
|
530 |
*}
|
|
531 |
|
|
532 |
|
|
533 |
|
2
|
534 |
ML {*
|
|
535 |
let
|
|
536 |
val ctxt = @{context}
|
|
537 |
val goal = @{prop "P \<or> Q \<Longrightarrow> Q \<or> P"}
|
|
538 |
in
|
|
539 |
Goal.prove ctxt ["P", "Q"] [] goal (fn _ =>
|
|
540 |
eresolve_tac [disjE] 1
|
|
541 |
THEN resolve_tac [disjI2] 1
|
|
542 |
THEN assume_tac 1
|
|
543 |
THEN resolve_tac [disjI1] 1
|
|
544 |
THEN assume_tac 1)
|
|
545 |
end
|
|
546 |
*}
|
|
547 |
|
21
|
548 |
text {*
|
|
549 |
|
|
550 |
\begin{readmore}
|
|
551 |
To learn more about the function @{ML Goal.prove} see \isccite{sec:results}.
|
|
552 |
\end{readmore}
|
|
553 |
|
|
554 |
*}
|
|
555 |
|
|
556 |
|
12
|
557 |
text {* An alternative way to transcribe this proof is as follows *}
|
2
|
558 |
|
|
559 |
ML {*
|
12
|
560 |
let
|
|
561 |
val ctxt = @{context}
|
|
562 |
val goal = @{prop "P \<or> Q \<Longrightarrow> Q \<or> P"}
|
|
563 |
in
|
|
564 |
Goal.prove ctxt ["P", "Q"] [] goal (fn _ =>
|
|
565 |
(eresolve_tac [disjE]
|
|
566 |
THEN' resolve_tac [disjI2]
|
|
567 |
THEN' assume_tac
|
|
568 |
THEN' resolve_tac [disjI1]
|
|
569 |
THEN' assume_tac) 1)
|
|
570 |
end
|
2
|
571 |
*}
|
|
572 |
|
21
|
573 |
text {* (FIXME: are there any advantages/disadvantages about this way?) *}
|
|
574 |
|
20
|
575 |
section {* Storing Theorems *}
|
|
576 |
|
|
577 |
section {* Theorem Attributes *}
|
|
578 |
|
2
|
579 |
|
|
580 |
end |