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