395
|
1 |
theory Advanced
|
441
|
2 |
imports Base First_Steps
|
318
|
3 |
begin
|
|
4 |
|
346
|
5 |
(*<*)
|
|
6 |
setup{*
|
|
7 |
open_file_with_prelude
|
395
|
8 |
"Advanced_Code.thy"
|
441
|
9 |
["theory Advanced", "imports Base First_Steps", "begin"]
|
346
|
10 |
*}
|
|
11 |
(*>*)
|
|
12 |
|
|
13 |
|
414
|
14 |
chapter {* Advanced Isabelle\label{chp:advanced} *}
|
381
|
15 |
|
|
16 |
text {*
|
410
|
17 |
\begin{flushright}
|
|
18 |
{\em All things are difficult before they are easy.} \\[1ex]
|
|
19 |
proverb
|
|
20 |
\end{flushright}
|
|
21 |
|
|
22 |
\medskip
|
400
|
23 |
While terms, types and theorems are the most basic data structures in
|
|
24 |
Isabelle, there are a number of layers built on top of them. Most of these
|
408
|
25 |
layers are concerned with storing and manipulating data. Handling them
|
|
26 |
properly is an essential skill for programming on the ML-level of Isabelle
|
|
27 |
programming. The most basic layer are theories. They contain global data and
|
|
28 |
can be seen as the ``long-term memory'' of Isabelle. There is usually only
|
|
29 |
one theory active at each moment. Proof contexts and local theories, on the
|
400
|
30 |
other hand, store local data for a task at hand. They act like the
|
408
|
31 |
``short-term memory'' and there can be many of them that are active in
|
|
32 |
parallel.
|
318
|
33 |
*}
|
|
34 |
|
358
|
35 |
section {* Theories\label{sec:theories} (TBD) *}
|
|
36 |
|
|
37 |
text {*
|
401
|
38 |
Theories, as said above, are the most basic layer in Isabelle. They contain
|
400
|
39 |
definitions, syntax declarations, axioms, proofs etc. If a definition is
|
|
40 |
stated, it must be stored in a theory in order to be usable later
|
|
41 |
on. Similar with proofs: once a proof is finished, the proved theorem needs
|
|
42 |
to be stored in the theorem database of the theory in order to be
|
|
43 |
usable. All relevant data of a theory can be queried as follows.
|
358
|
44 |
|
|
45 |
\begin{isabelle}
|
|
46 |
\isacommand{print\_theory}\\
|
|
47 |
@{text "> names: Pure Code_Generator HOL \<dots>"}\\
|
|
48 |
@{text "> classes: Inf < type \<dots>"}\\
|
|
49 |
@{text "> default sort: type"}\\
|
|
50 |
@{text "> syntactic types: #prop \<dots>"}\\
|
|
51 |
@{text "> logical types: 'a \<times> 'b \<dots>"}\\
|
|
52 |
@{text "> type arities: * :: (random, random) random \<dots>"}\\
|
|
53 |
@{text "> logical constants: == :: 'a \<Rightarrow> 'a \<Rightarrow> prop \<dots>"}\\
|
|
54 |
@{text "> abbreviations: \<dots>"}\\
|
|
55 |
@{text "> axioms: \<dots>"}\\
|
|
56 |
@{text "> oracles: \<dots>"}\\
|
|
57 |
@{text "> definitions: \<dots>"}\\
|
|
58 |
@{text "> theorems: \<dots>"}
|
|
59 |
\end{isabelle}
|
|
60 |
|
400
|
61 |
|
|
62 |
|
358
|
63 |
\begin{center}
|
|
64 |
\begin{tikzpicture}
|
|
65 |
\node[top color=white, bottom color=gray!30, draw=black!100, drop shadow] {A};
|
|
66 |
\end{tikzpicture}
|
|
67 |
\end{center}
|
|
68 |
|
392
|
69 |
\footnote{\bf FIXME: list append in merge operations can cause
|
|
70 |
exponential blowups.}
|
358
|
71 |
*}
|
|
72 |
|
348
|
73 |
section {* Setups (TBD) *}
|
318
|
74 |
|
348
|
75 |
text {*
|
361
|
76 |
@{ML Sign.declare_const}
|
|
77 |
|
348
|
78 |
In the previous section we used \isacommand{setup} in order to make
|
|
79 |
a theorem attribute known to Isabelle. What happens behind the scenes
|
|
80 |
is that \isacommand{setup} expects a function of type
|
|
81 |
@{ML_type "theory -> theory"}: the input theory is the current theory and the
|
|
82 |
output the theory where the theory attribute has been stored.
|
|
83 |
|
|
84 |
This is a fundamental principle in Isabelle. A similar situation occurs
|
|
85 |
for example with declaring constants. The function that declares a
|
|
86 |
constant on the ML-level is @{ML_ind add_consts_i in Sign}.
|
|
87 |
If you write\footnote{Recall that ML-code needs to be
|
|
88 |
enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.}
|
|
89 |
*}
|
|
90 |
|
|
91 |
ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
|
|
92 |
|
|
93 |
text {*
|
|
94 |
for declaring the constant @{text "BAR"} with type @{typ nat} and
|
|
95 |
run the code, then you indeed obtain a theory as result. But if you
|
|
96 |
query the constant on the Isabelle level using the command \isacommand{term}
|
|
97 |
|
|
98 |
\begin{isabelle}
|
|
99 |
\isacommand{term}~@{text [quotes] "BAR"}\\
|
|
100 |
@{text "> \"BAR\" :: \"'a\""}
|
|
101 |
\end{isabelle}
|
|
102 |
|
|
103 |
you do not obtain a constant of type @{typ nat}, but a free variable (printed in
|
|
104 |
blue) of polymorphic type. The problem is that the ML-expression above did
|
|
105 |
not register the declaration with the current theory. This is what the command
|
|
106 |
\isacommand{setup} is for. The constant is properly declared with
|
|
107 |
*}
|
|
108 |
|
|
109 |
setup %gray {* Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] *}
|
|
110 |
|
|
111 |
text {*
|
|
112 |
Now
|
|
113 |
|
|
114 |
\begin{isabelle}
|
|
115 |
\isacommand{term}~@{text [quotes] "BAR"}\\
|
|
116 |
@{text "> \"BAR\" :: \"nat\""}
|
|
117 |
\end{isabelle}
|
|
118 |
|
|
119 |
returns a (black) constant with the type @{typ nat}.
|
|
120 |
|
|
121 |
A similar command is \isacommand{local\_setup}, which expects a function
|
|
122 |
of type @{ML_type "local_theory -> local_theory"}. Later on we will also
|
|
123 |
use the commands \isacommand{method\_setup} for installing methods in the
|
|
124 |
current theory and \isacommand{simproc\_setup} for adding new simprocs to
|
|
125 |
the current simpset.
|
|
126 |
*}
|
318
|
127 |
|
341
|
128 |
section {* Contexts (TBD) *}
|
|
129 |
|
413
|
130 |
ML_command "ProofContext.debug := true"
|
|
131 |
ML_command "ProofContext.verbose := true"
|
|
132 |
|
|
133 |
|
341
|
134 |
section {* Local Theories (TBD) *}
|
|
135 |
|
394
|
136 |
text {*
|
400
|
137 |
In contrast to an ordinary theory, which simply consists of a type
|
|
138 |
signature, as well as tables for constants, axioms and theorems, a local
|
|
139 |
theory contains additional context information, such as locally fixed
|
|
140 |
variables and local assumptions that may be used by the package. The type
|
|
141 |
@{ML_type local_theory} is identical to the type of \emph{proof contexts}
|
|
142 |
@{ML_type "Proof.context"}, although not every proof context constitutes a
|
|
143 |
valid local theory.
|
|
144 |
|
|
145 |
@{ML "Context.>> o Context.map_theory"}
|
394
|
146 |
@{ML_ind "Local_Theory.declaration"}
|
|
147 |
*}
|
318
|
148 |
|
|
149 |
(*
|
|
150 |
setup {*
|
|
151 |
Sign.add_consts_i [(Binding"bar", @{typ "nat"},NoSyn)]
|
|
152 |
*}
|
|
153 |
lemma "bar = (1::nat)"
|
|
154 |
oops
|
|
155 |
|
|
156 |
setup {*
|
|
157 |
Sign.add_consts_i [("foo", @{typ "nat"},NoSyn)]
|
|
158 |
#> PureThy.add_defs false [((@{binding "foo_def"},
|
|
159 |
Logic.mk_equals (Const ("FirstSteps.foo", @{typ "nat"}), @{term "1::nat"})), [])]
|
|
160 |
#> snd
|
|
161 |
*}
|
|
162 |
*)
|
|
163 |
(*
|
|
164 |
lemma "foo = (1::nat)"
|
|
165 |
apply(simp add: foo_def)
|
|
166 |
done
|
|
167 |
|
|
168 |
thm foo_def
|
|
169 |
*)
|
336
|
170 |
|
394
|
171 |
section {* Morphisms (TBD) *}
|
|
172 |
|
|
173 |
text {*
|
|
174 |
Morphisms are arbitrary transformations over terms, types, theorems and bindings.
|
|
175 |
They can be constructed using the function @{ML_ind morphism in Morphism},
|
|
176 |
which expects a record with functions of type
|
|
177 |
|
|
178 |
\begin{isabelle}
|
|
179 |
\begin{tabular}{rl}
|
|
180 |
@{text "binding:"} & @{text "binding -> binding"}\\
|
|
181 |
@{text "typ:"} & @{text "typ -> typ"}\\
|
|
182 |
@{text "term:"} & @{text "term -> term"}\\
|
|
183 |
@{text "fact:"} & @{text "thm list -> thm list"}
|
|
184 |
\end{tabular}
|
|
185 |
\end{isabelle}
|
|
186 |
|
|
187 |
The simplest morphism is the @{ML_ind identity in Morphism}-morphism defined as
|
|
188 |
*}
|
|
189 |
|
|
190 |
ML{*val identity = Morphism.morphism {binding = I, typ = I, term = I, fact = I}*}
|
|
191 |
|
|
192 |
text {*
|
|
193 |
Morphisms can be composed with the function @{ML_ind "$>" in Morphism}
|
|
194 |
*}
|
|
195 |
|
|
196 |
ML{*fun trm_phi (Free (x, T)) = Var ((x, 0), T)
|
|
197 |
| trm_phi (Abs (x, T, t)) = Abs (x, T, trm_phi t)
|
|
198 |
| trm_phi (t $ s) = (trm_phi t) $ (trm_phi s)
|
|
199 |
| trm_phi t = t*}
|
|
200 |
|
|
201 |
ML{*val phi = Morphism.term_morphism trm_phi*}
|
|
202 |
|
|
203 |
ML{*Morphism.term phi @{term "P x y"}*}
|
|
204 |
|
|
205 |
text {*
|
|
206 |
@{ML_ind term_morphism in Morphism}
|
|
207 |
|
|
208 |
@{ML_ind term in Morphism},
|
|
209 |
@{ML_ind thm in Morphism}
|
|
210 |
|
|
211 |
\begin{readmore}
|
|
212 |
Morphisms are implemented in the file @{ML_file "Pure/morphism.ML"}.
|
|
213 |
\end{readmore}
|
|
214 |
*}
|
318
|
215 |
|
|
216 |
section {* Misc (TBD) *}
|
|
217 |
|
|
218 |
ML {*Datatype.get_info @{theory} "List.list"*}
|
|
219 |
|
319
|
220 |
text {*
|
|
221 |
FIXME: association lists:
|
|
222 |
@{ML_file "Pure/General/alist.ML"}
|
|
223 |
|
|
224 |
FIXME: calling the ML-compiler
|
|
225 |
|
|
226 |
*}
|
|
227 |
|
414
|
228 |
section {* What Is In an Isabelle Name? (TBD) *}
|
|
229 |
|
|
230 |
text {*
|
|
231 |
On the ML-level of Isabelle, you often have to work with qualified names.
|
|
232 |
These are strings with some additional information, such as positional
|
|
233 |
information and qualifiers. Such qualified names can be generated with the
|
|
234 |
antiquotation @{text "@{binding \<dots>}"}. For example
|
|
235 |
|
|
236 |
@{ML_response [display,gray]
|
|
237 |
"@{binding \"name\"}"
|
|
238 |
"name"}
|
|
239 |
|
|
240 |
An example where a qualified name is needed is the function
|
|
241 |
@{ML_ind define in Local_Theory}. This function is used below to define
|
|
242 |
the constant @{term "TrueConj"} as the conjunction @{term "True \<and> True"}.
|
|
243 |
*}
|
|
244 |
|
|
245 |
local_setup %gray {*
|
|
246 |
Local_Theory.define ((@{binding "TrueConj"}, NoSyn),
|
|
247 |
(Attrib.empty_binding, @{term "True \<and> True"})) #> snd *}
|
|
248 |
|
|
249 |
text {*
|
|
250 |
Now querying the definition you obtain:
|
|
251 |
|
|
252 |
\begin{isabelle}
|
|
253 |
\isacommand{thm}~@{text "TrueConj_def"}\\
|
|
254 |
@{text "> "}~@{thm TrueConj_def}
|
|
255 |
\end{isabelle}
|
|
256 |
|
|
257 |
\begin{readmore}
|
|
258 |
The basic operations on bindings are implemented in
|
|
259 |
@{ML_file "Pure/General/binding.ML"}.
|
|
260 |
\end{readmore}
|
|
261 |
|
|
262 |
\footnote{\bf FIXME give a better example why bindings are important}
|
|
263 |
\footnote{\bf FIXME give a pointer to \isacommand{local\_setup}; if not, then explain
|
|
264 |
why @{ML snd} is needed.}
|
|
265 |
\footnote{\bf FIXME: There should probably a separate section on binding, long-names
|
|
266 |
and sign.}
|
|
267 |
|
|
268 |
*}
|
|
269 |
|
318
|
270 |
|
360
|
271 |
ML {* Sign.intern_type @{theory} "list" *}
|
|
272 |
ML {* Sign.intern_const @{theory} "prod_fun" *}
|
|
273 |
|
414
|
274 |
text {*
|
|
275 |
\footnote{\bf FIXME: Explain the following better; maybe put in a separate
|
|
276 |
section and link with the comment in the antiquotation section.}
|
|
277 |
|
|
278 |
Occasionally you have to calculate what the ``base'' name of a given
|
|
279 |
constant is. For this you can use the function @{ML_ind "Sign.extern_const"} or
|
|
280 |
@{ML_ind Long_Name.base_name}. For example:
|
|
281 |
|
|
282 |
@{ML_response [display,gray] "Sign.extern_const @{theory} \"List.list.Nil\"" "\"Nil\""}
|
|
283 |
|
|
284 |
The difference between both functions is that @{ML extern_const in Sign} returns
|
|
285 |
the smallest name that is still unique, whereas @{ML base_name in Long_Name} always
|
|
286 |
strips off all qualifiers.
|
|
287 |
|
|
288 |
\begin{readmore}
|
|
289 |
Functions about naming are implemented in @{ML_file "Pure/General/name_space.ML"};
|
|
290 |
functions about signatures in @{ML_file "Pure/sign.ML"}.
|
|
291 |
\end{readmore}
|
|
292 |
*}
|
387
|
293 |
|
|
294 |
text {*
|
|
295 |
@{ML_ind "Binding.str_of"} returns the string with markup;
|
|
296 |
@{ML_ind "Binding.name_of"} returns the string without markup
|
394
|
297 |
|
|
298 |
@{ML_ind "Binding.conceal"}
|
387
|
299 |
*}
|
|
300 |
|
388
|
301 |
section {* Concurrency (TBD) *}
|
|
302 |
|
|
303 |
text {*
|
|
304 |
@{ML_ind prove_future in Goal}
|
|
305 |
@{ML_ind future_result in Goal}
|
|
306 |
@{ML_ind fork_pri in Future}
|
|
307 |
*}
|
387
|
308 |
|
396
|
309 |
section {* Parse and Print Translations (TBD) *}
|
|
310 |
|
349
|
311 |
section {* Summary *}
|
|
312 |
|
|
313 |
text {*
|
395
|
314 |
TBD
|
349
|
315 |
*}
|
318
|
316 |
|
|
317 |
end
|